diff --git a/kernel/CMakeLists.txt b/kernel/CMakeLists.txt index 3c767fe401..84064de2d4 100644 --- a/kernel/CMakeLists.txt +++ b/kernel/CMakeLists.txt @@ -16,6 +16,7 @@ set(KERNEL_SOURCES kernel/Debug.cpp kernel/Device/Device.cpp kernel/Device/NullDevice.cpp + kernel/Device/ZeroDevice.cpp kernel/Errors.cpp kernel/Font.cpp kernel/FS/DevFS/FileSystem.cpp diff --git a/kernel/include/kernel/Device/ZeroDevice.h b/kernel/include/kernel/Device/ZeroDevice.h new file mode 100644 index 0000000000..c8be3c1902 --- /dev/null +++ b/kernel/include/kernel/Device/ZeroDevice.h @@ -0,0 +1,26 @@ +#include + +namespace Kernel +{ + + class ZeroDevice : public CharacterDevice + { + public: + static BAN::ErrorOr> create(mode_t, uid_t, gid_t); + + virtual dev_t rdev() const override { return m_rdev; } + + protected: + ZeroDevice(mode_t mode, uid_t uid, gid_t gid, dev_t rdev) + : CharacterDevice(mode, uid, gid) + , m_rdev(rdev) + { } + + virtual BAN::ErrorOr read_impl(off_t, void*, size_t) override; + virtual BAN::ErrorOr write_impl(off_t, const void*, size_t size) override { return size; }; + + private: + const dev_t m_rdev; + }; + +} \ No newline at end of file diff --git a/kernel/kernel/Device/ZeroDevice.cpp b/kernel/kernel/Device/ZeroDevice.cpp new file mode 100644 index 0000000000..38d90f9c9c --- /dev/null +++ b/kernel/kernel/Device/ZeroDevice.cpp @@ -0,0 +1,22 @@ +#include +#include + +namespace Kernel +{ + + BAN::ErrorOr> ZeroDevice::create(mode_t mode, uid_t uid, gid_t gid) + { + auto* result = new ZeroDevice(mode, uid, gid, DevFileSystem::get().get_next_dev()); + if (result == nullptr) + return BAN::Error::from_errno(ENOMEM); + return BAN::RefPtr::adopt(result); + } + + + BAN::ErrorOr ZeroDevice::read_impl(off_t, void* buffer, size_t bytes) + { + memset(buffer, 0, bytes); + return bytes; + } + +} \ No newline at end of file diff --git a/kernel/kernel/FS/DevFS/FileSystem.cpp b/kernel/kernel/FS/DevFS/FileSystem.cpp index e3a06dcf0b..9409d6dbab 100644 --- a/kernel/kernel/FS/DevFS/FileSystem.cpp +++ b/kernel/kernel/FS/DevFS/FileSystem.cpp @@ -1,5 +1,6 @@ #include #include +#include #include #include #include @@ -21,6 +22,7 @@ namespace Kernel MUST(s_instance->set_root_inode(root_inode)); s_instance->add_device("null", MUST(NullDevice::create(0666, 0, 0))); + s_instance->add_device("zero", MUST(ZeroDevice::create(0666, 0, 0))); } DevFileSystem& DevFileSystem::get()