From 841fc25137633ce50bfb12d337dfb25f7e61bd73 Mon Sep 17 00:00:00 2001 From: apio Date: Fri, 21 Apr 2023 18:18:15 +0200 Subject: [PATCH] kernel+init: Add a framebuffer special device file --- apps/init.cpp | 1 + kernel/CMakeLists.txt | 1 + kernel/src/fs/devices/Device.h | 5 +++ kernel/src/fs/devices/DeviceRegistry.cpp | 2 ++ kernel/src/fs/devices/DeviceRegistry.h | 3 +- kernel/src/fs/devices/FramebufferDevice.cpp | 37 +++++++++++++++++++++ kernel/src/fs/devices/FramebufferDevice.h | 21 ++++++++++++ kernel/src/fs/tmpfs/FileSystem.h | 2 +- 8 files changed, 70 insertions(+), 2 deletions(-) create mode 100644 kernel/src/fs/devices/FramebufferDevice.cpp create mode 100644 kernel/src/fs/devices/FramebufferDevice.h diff --git a/apps/init.cpp b/apps/init.cpp index 4ed10a06..3d7f4dc5 100644 --- a/apps/init.cpp +++ b/apps/init.cpp @@ -20,6 +20,7 @@ static void populate_devfs() xmknod("/dev/console", 0666, 1, 0); xmknod("/dev/null", 0666, 2, 0); xmknod("/dev/zero", 0666, 2, 1); + xmknod("/dev/fb0", 0222, 3, 0); } int main() diff --git a/kernel/CMakeLists.txt b/kernel/CMakeLists.txt index 725e5957..5a995f7d 100644 --- a/kernel/CMakeLists.txt +++ b/kernel/CMakeLists.txt @@ -43,6 +43,7 @@ set(SOURCES src/fs/devices/NullDevice.cpp src/fs/devices/ZeroDevice.cpp src/fs/devices/ConsoleDevice.cpp + src/fs/devices/FramebufferDevice.cpp src/InitRD.cpp src/ELF.cpp ) diff --git a/kernel/src/fs/devices/Device.h b/kernel/src/fs/devices/Device.h index a6d7dee0..a958e19a 100644 --- a/kernel/src/fs/devices/Device.h +++ b/kernel/src/fs/devices/Device.h @@ -13,6 +13,11 @@ class Device return err(ENOTTY); } + virtual usize size() const + { + return 0; + } + virtual bool blocking() const = 0; virtual ~Device() = default; diff --git a/kernel/src/fs/devices/DeviceRegistry.cpp b/kernel/src/fs/devices/DeviceRegistry.cpp index 47ecc182..67d5e6ed 100644 --- a/kernel/src/fs/devices/DeviceRegistry.cpp +++ b/kernel/src/fs/devices/DeviceRegistry.cpp @@ -1,6 +1,7 @@ #include "fs/devices/DeviceRegistry.h" #include "Log.h" #include "fs/devices/ConsoleDevice.h" +#include "fs/devices/FramebufferDevice.h" #include "fs/devices/NullDevice.h" #include "fs/devices/ZeroDevice.h" #include @@ -45,6 +46,7 @@ namespace DeviceRegistry TRY(register_special_device(DeviceMajorTypes::Memory, 0, NullDevice::create)); TRY(register_special_device(DeviceMajorTypes::Memory, 1, ZeroDevice::create)); TRY(register_special_device(DeviceMajorTypes::Console, 0, ConsoleDevice::create)); + TRY(register_special_device(DeviceMajorTypes::Framebuffer, 0, FramebufferDevice::create)); return {}; } diff --git a/kernel/src/fs/devices/DeviceRegistry.h b/kernel/src/fs/devices/DeviceRegistry.h index 72db9c58..b004246b 100644 --- a/kernel/src/fs/devices/DeviceRegistry.h +++ b/kernel/src/fs/devices/DeviceRegistry.h @@ -11,7 +11,8 @@ namespace DeviceRegistry { Null = 0, Console = 1, - Memory = 2 + Memory = 2, + Framebuffer = 3, }; Result> create_special_device(u32 major, u32 minor); diff --git a/kernel/src/fs/devices/FramebufferDevice.cpp b/kernel/src/fs/devices/FramebufferDevice.cpp new file mode 100644 index 00000000..ac51e298 --- /dev/null +++ b/kernel/src/fs/devices/FramebufferDevice.cpp @@ -0,0 +1,37 @@ +#include "fs/devices/FramebufferDevice.h" +#include "video/Framebuffer.h" +#include + +Result> FramebufferDevice::create() +{ + return (SharedPtr)TRY(make_shared()); +} + +Result FramebufferDevice::read(u8*, usize, usize) const +{ + return 0; +} + +Result FramebufferDevice::write(const u8* buf, usize offset, usize length) +{ + if ((offset + length) > size()) length = size() - offset; + + memcpy(Framebuffer::ptr() + offset, buf, length); + + return length; +} + +usize FramebufferDevice::size() const +{ + return Framebuffer::size(); +} + +bool FramebufferDevice::blocking() const +{ + return false; +} + +Result FramebufferDevice::ioctl(int, void*) +{ + return err(EINVAL); +} diff --git a/kernel/src/fs/devices/FramebufferDevice.h b/kernel/src/fs/devices/FramebufferDevice.h new file mode 100644 index 00000000..6d293725 --- /dev/null +++ b/kernel/src/fs/devices/FramebufferDevice.h @@ -0,0 +1,21 @@ +#pragma once +#include "fs/devices/DeviceRegistry.h" + +class FramebufferDevice : public Device +{ + public: + // Initializer for DeviceRegistry. + static Result> create(); + + Result read(u8*, usize, usize) const override; + + Result write(const u8*, usize, usize) override; + + bool blocking() const override; + + Result ioctl(int request, void* arg) override; + + usize size() const override; + + virtual ~FramebufferDevice() = default; +}; diff --git a/kernel/src/fs/tmpfs/FileSystem.h b/kernel/src/fs/tmpfs/FileSystem.h index 52b61883..cd3220a7 100644 --- a/kernel/src/fs/tmpfs/FileSystem.h +++ b/kernel/src/fs/tmpfs/FileSystem.h @@ -176,7 +176,7 @@ namespace TmpFS usize size() const override { - return 0; + return m_device->size(); } mode_t mode() const override