From b87dae7e7ce39392560caef9bc77a657aa81223c Mon Sep 17 00:00:00 2001 From: Bananymous Date: Mon, 3 Apr 2023 11:43:16 +0300 Subject: [PATCH] Kernel: Device dev and rdev number is done more properly Also hd* partitions are now 1 indexed instead of 0 --- base/boot/grub/grub.cfg | 8 +++---- kernel/include/kernel/Device.h | 20 ++++++++++++++-- kernel/include/kernel/DeviceManager.h | 24 ++++++++++++++----- kernel/include/kernel/Input/PS2Controller.h | 10 -------- kernel/include/kernel/Input/PS2Keyboard.h | 18 +++++++------- kernel/include/kernel/Storage/ATAController.h | 15 ++++++------ kernel/include/kernel/Storage/ATADevice.h | 16 ++++++------- kernel/include/kernel/Storage/StorageDevice.h | 8 +------ kernel/kernel/Device.cpp | 1 + kernel/kernel/DeviceManager.cpp | 18 ++++++++++++++ kernel/kernel/FS/VirtualFileSystem.cpp | 1 + kernel/kernel/Input/PS2Controller.cpp | 8 +------ kernel/kernel/Input/PS2Keyboard.cpp | 10 ++++++-- kernel/kernel/Shell.cpp | 4 ++-- kernel/kernel/Storage/StorageDevice.cpp | 17 +++++-------- 15 files changed, 100 insertions(+), 78 deletions(-) diff --git a/base/boot/grub/grub.cfg b/base/boot/grub/grub.cfg index 6aa3ea9cb3..e92e2eba28 100644 --- a/base/boot/grub/grub.cfg +++ b/base/boot/grub/grub.cfg @@ -1,15 +1,15 @@ menuentry "banan-os" { - multiboot /boot/banan-os.kernel root=/dev/hda1 + multiboot /boot/banan-os.kernel root=/dev/hda2 } menuentry "banan-os (no serial)" { - multiboot /boot/banan-os.kernel root=/dev/hda1 noserial + multiboot /boot/banan-os.kernel root=/dev/hda2 noserial } menuentry "banan-os (no apic)" { - multiboot /boot/banan-os.kernel root=/dev/hda1 noapic + multiboot /boot/banan-os.kernel root=/dev/hda2 noapic } menuentry "banan-os (no apic, no serial)" { - multiboot /boot/banan-os.kernel root=/dev/hda1 noapic noserial + multiboot /boot/banan-os.kernel root=/dev/hda2 noapic noserial } diff --git a/kernel/include/kernel/Device.h b/kernel/include/kernel/Device.h index ff6a37659c..670cbcc450 100644 --- a/kernel/include/kernel/Device.h +++ b/kernel/include/kernel/Device.h @@ -1,6 +1,6 @@ #pragma once -#include +#include namespace Kernel { @@ -23,12 +23,28 @@ namespace Kernel virtual InodeType inode_type() const override { return InodeType::Device; } + virtual ino_t ino() const override { return m_ino_t; } + virtual nlink_t nlink() const override { return 1; } + virtual off_t size() const override { return 0; } virtual timespec atime() const override { return m_create_time; } virtual timespec mtime() const override { return m_create_time; } virtual timespec ctime() const override { return m_create_time; } + virtual blksize_t blksize() const override { return DeviceManager::get().blksize(); } + virtual blkcnt_t blocks() const override { return DeviceManager::get().blocks(); } + virtual dev_t dev() const override { return DeviceManager::get().dev(); } + + /* + a device has to overload + virtual Mode mode() const; + virtual uid_t uid() const; + virtual gid_t gid() const; + virutal dev_t rdev() const; + virtual BAN::StringView name() const; + */ private: - timespec m_create_time; + const timespec m_create_time; + const ino_t m_ino_t; }; class BlockDevice : public Device diff --git a/kernel/include/kernel/DeviceManager.h b/kernel/include/kernel/DeviceManager.h index fb6ca2a2ff..f029202591 100644 --- a/kernel/include/kernel/DeviceManager.h +++ b/kernel/include/kernel/DeviceManager.h @@ -2,12 +2,15 @@ #include #include -#include #include +#include + namespace Kernel { + class Device; + class DeviceManager final : public FileSystem, public Inode { BAN_NON_COPYABLE(DeviceManager); @@ -17,6 +20,10 @@ namespace Kernel static void initialize(); static DeviceManager& get(); + ino_t get_next_ino() const; + dev_t get_next_rdev() const; + uint8_t get_next_input_dev() const; + void update(); void add_device(Device*); @@ -48,13 +55,18 @@ namespace Kernel virtual timespec atime() const override { return { 0, 0 }; } virtual timespec mtime() const override { return { 0, 0 }; } virtual timespec ctime() const override { return { 0, 0 }; } - virtual blksize_t blksize() const override { return 0; } + virtual blksize_t blksize() const override { ASSERT(m_blksize); return m_blksize; } virtual blkcnt_t blocks() const override { return 0; } - virtual dev_t dev() const override { return 0x4900; } - virtual dev_t rdev() const override { return 0x7854; } + virtual dev_t dev() const override { return makedev(0, 5); } + virtual dev_t rdev() const override { return 0; } - virtual BAN::ErrorOr read(size_t, void*, size_t) { ASSERT_NOT_REACHED(); } - virtual BAN::ErrorOr create_file(BAN::StringView, mode_t) { ASSERT_NOT_REACHED(); } + virtual BAN::ErrorOr read(size_t, void*, size_t) { return BAN::Error::from_errno(EISDIR); } + virtual BAN::ErrorOr create_file(BAN::StringView, mode_t) { return BAN::Error::from_errno(ENOTSUP); } + + void set_blksize(blksize_t blksize) { m_blksize = blksize; } + + private: + blksize_t m_blksize = 0; }; } \ No newline at end of file diff --git a/kernel/include/kernel/Input/PS2Controller.h b/kernel/include/kernel/Input/PS2Controller.h index cb3849f6c6..ae2036ff9f 100644 --- a/kernel/include/kernel/Input/PS2Controller.h +++ b/kernel/include/kernel/Input/PS2Controller.h @@ -8,23 +8,13 @@ namespace Kernel::Input class PS2Device : public CharacterDevice { public: - PS2Device(dev_t); virtual ~PS2Device() {} virtual void on_byte(uint8_t) = 0; public: - virtual ino_t ino() const override { return m_ino; } virtual Mode mode() const override { return { Mode::IFCHR | Mode::IRUSR | Mode::IRGRP }; } - virtual nlink_t nlink() const override { return 1; } virtual uid_t uid() const override { return 0; } virtual gid_t gid() const override { return 0; } - virtual off_t size() const override { return 0; } - virtual blkcnt_t blocks() const override { return 0; } - virtual dev_t dev() const override { return m_dev; } - - private: - ino_t m_ino; - dev_t m_dev; }; class PS2Controller diff --git a/kernel/include/kernel/Input/PS2Keyboard.h b/kernel/include/kernel/Input/PS2Keyboard.h index f28226e4d8..cf64ad7d98 100644 --- a/kernel/include/kernel/Input/PS2Keyboard.h +++ b/kernel/include/kernel/Input/PS2Keyboard.h @@ -27,17 +27,13 @@ namespace Kernel::Input }; public: - static BAN::ErrorOr create(PS2Controller&, dev_t); + static BAN::ErrorOr create(PS2Controller&); virtual void on_byte(uint8_t) override; virtual void update() override; private: - PS2Keyboard(PS2Controller& controller, dev_t device) - : PS2Device(device) - , m_controller(controller) - , m_name(BAN::String::formatted("input{}", device)) - {} + PS2Keyboard(PS2Controller& controller); BAN::ErrorOr initialize(); void append_command_queue(uint8_t); @@ -63,13 +59,15 @@ namespace Kernel::Input Semaphore m_semaphore; - BAN::String m_name; - public: virtual BAN::StringView name() const override { return m_name; } - virtual blksize_t blksize() const override { return sizeof(KeyEvent); } - virtual dev_t rdev() const override { return 0x8594; } + virtual dev_t rdev() const override { return m_rdev; } + virtual BAN::ErrorOr read(size_t, void*, size_t) override; + + private: + const BAN::String m_name; + const dev_t m_rdev; }; } \ No newline at end of file diff --git a/kernel/include/kernel/Storage/ATAController.h b/kernel/include/kernel/Storage/ATAController.h index 76e7f16442..649ae42d16 100644 --- a/kernel/include/kernel/Storage/ATAController.h +++ b/kernel/include/kernel/Storage/ATAController.h @@ -20,7 +20,9 @@ namespace Kernel uint8_t next_device_index() const; private: - ATAController() = default; + ATAController() + : m_rdev(makedev(DeviceManager::get().get_next_rdev(), 0)) + { } BAN::ErrorOr initialize(const PCIDevice& device); private: @@ -28,20 +30,17 @@ namespace Kernel friend class ATABus; public: - virtual ino_t ino() const override { return 0; } virtual Mode mode() const override { return { Mode::IFCHR }; } - virtual nlink_t nlink() const override { return 1; } virtual uid_t uid() const override { return 0; } virtual gid_t gid() const override { return 0; } - virtual off_t size() const override { return 0; } - virtual blksize_t blksize() const override { return 0; } - virtual blkcnt_t blocks() const override { return 0; } - virtual dev_t dev() const override { return DeviceManager::get().dev(); } - virtual dev_t rdev() const override { return 0x8594; } + virtual dev_t rdev() const override { return m_rdev; } virtual BAN::StringView name() const override { return "hd"sv; } virtual BAN::ErrorOr read(size_t, void*, size_t) { return BAN::Error::from_errno(ENOTSUP); } + + private: + const dev_t m_rdev; }; } \ No newline at end of file diff --git a/kernel/include/kernel/Storage/ATADevice.h b/kernel/include/kernel/Storage/ATADevice.h index bf22820f19..584d1576be 100644 --- a/kernel/include/kernel/Storage/ATADevice.h +++ b/kernel/include/kernel/Storage/ATADevice.h @@ -9,7 +9,10 @@ namespace Kernel class ATADevice final : public StorageDevice { public: - ATADevice(ATABus* bus) : m_bus(bus) { } + ATADevice(ATABus* bus) + : m_bus(bus) + , m_rdev(makedev(DeviceManager::get().get_next_rdev(), 0)) + { } BAN::ErrorOr initialize(ATABus::DeviceType, const uint16_t*); virtual BAN::ErrorOr read_sectors(uint64_t, uint8_t, uint8_t*) override; @@ -34,22 +37,17 @@ namespace Kernel friend class ATABus; public: - virtual ino_t ino() const override { return m_index; } - virtual Mode mode() const override { return { Mode::IFBLK }; } - virtual nlink_t nlink() const override { return 1; } + virtual Mode mode() const override { return { Mode::IFBLK | Mode::IRUSR | Mode::IRGRP }; } virtual uid_t uid() const override { return 0; } virtual gid_t gid() const override { return 0; } - virtual off_t size() const override { return 0; } - virtual blksize_t blksize() const override { return sector_size(); } - virtual blkcnt_t blocks() const override { return 0; } - virtual dev_t dev() const override { return m_bus->controller()->dev(); } - virtual dev_t rdev() const override { return 0x5429; } + virtual dev_t rdev() const override { return m_rdev; } virtual BAN::StringView name() const override { return BAN::StringView(m_device_name, sizeof(m_device_name) - 1); } virtual BAN::ErrorOr read(size_t, void*, size_t) override; public: + const dev_t m_rdev; char m_device_name[4] {}; }; diff --git a/kernel/include/kernel/Storage/StorageDevice.h b/kernel/include/kernel/Storage/StorageDevice.h index 70fa31d160..8fb93e6dd5 100644 --- a/kernel/include/kernel/Storage/StorageDevice.h +++ b/kernel/include/kernel/Storage/StorageDevice.h @@ -44,16 +44,10 @@ namespace Kernel public: virtual DeviceType device_type() const override { return DeviceType::Partition; } - virtual ino_t ino() const override { return m_index; } virtual Mode mode() const override { return { Mode::IFBLK | Mode::IRUSR | Mode::IRGRP }; } - virtual nlink_t nlink() const override { return 1; } virtual uid_t uid() const override { return 0; } virtual gid_t gid() const override { return 0; } - virtual off_t size() const override { return 0; } - virtual blksize_t blksize() const; - virtual blkcnt_t blocks() const override { return 0; } - virtual dev_t dev() const override; - virtual dev_t rdev() const { return 0x7459; } + virtual dev_t rdev() const override; virtual BAN::StringView name() const override { return m_device_name; } diff --git a/kernel/kernel/Device.cpp b/kernel/kernel/Device.cpp index f82f65e180..38fea26523 100644 --- a/kernel/kernel/Device.cpp +++ b/kernel/kernel/Device.cpp @@ -7,6 +7,7 @@ namespace Kernel Device::Device() : m_create_time({ BAN::to_unix_time(RTC::get_current_time()), 0 }) + , m_ino_t(DeviceManager::get().get_next_ino()) { } } \ No newline at end of file diff --git a/kernel/kernel/DeviceManager.cpp b/kernel/kernel/DeviceManager.cpp index 422f78e4c3..00a67dbccc 100644 --- a/kernel/kernel/DeviceManager.cpp +++ b/kernel/kernel/DeviceManager.cpp @@ -76,6 +76,24 @@ namespace Kernel return *s_instance; } + ino_t DeviceManager::get_next_ino() const + { + static ino_t next_ino = 1; + return next_ino++; + } + + dev_t DeviceManager::get_next_rdev() const + { + static dev_t next_dev = 1; + return next_dev++; + } + + uint8_t DeviceManager::get_next_input_dev() const + { + static uint8_t next_dev = 0; + return next_dev++; + } + void DeviceManager::update() { LockGuard _(m_lock); diff --git a/kernel/kernel/FS/VirtualFileSystem.cpp b/kernel/kernel/FS/VirtualFileSystem.cpp index 72f5fc680d..fb62776fbc 100644 --- a/kernel/kernel/FS/VirtualFileSystem.cpp +++ b/kernel/kernel/FS/VirtualFileSystem.cpp @@ -25,6 +25,7 @@ namespace Kernel auto partition_inode = TRY(DeviceManager::get().read_directory_inode(root)); s_instance->m_root_fs = TRY(Ext2FS::create(*(Partition*)partition_inode.ptr())); + DeviceManager::get().set_blksize(s_instance->m_root_fs->root_inode()->blksize()); TRY(s_instance->mount(&DeviceManager::get(), "/dev")); guard.disable(); diff --git a/kernel/kernel/Input/PS2Controller.cpp b/kernel/kernel/Input/PS2Controller.cpp index d63ae643e2..8d65bc0893 100644 --- a/kernel/kernel/Input/PS2Controller.cpp +++ b/kernel/kernel/Input/PS2Controller.cpp @@ -302,7 +302,7 @@ namespace Kernel::Input // MF2 Keyboard if (index == 2 && (bytes[0] == 0xAB && bytes[1] == 0x83)) - m_devices[device] = TRY(PS2Keyboard::create(*this, device)); + m_devices[device] = TRY(PS2Keyboard::create(*this)); if (m_devices[device]) return {}; @@ -332,10 +332,4 @@ namespace Kernel::Input return {}; } - PS2Device::PS2Device(dev_t dev) - { - m_dev = dev; - m_ino = dev & 1; - } - } \ No newline at end of file diff --git a/kernel/kernel/Input/PS2Keyboard.cpp b/kernel/kernel/Input/PS2Keyboard.cpp index ec775629d9..9b5c7378f6 100644 --- a/kernel/kernel/Input/PS2Keyboard.cpp +++ b/kernel/kernel/Input/PS2Keyboard.cpp @@ -37,9 +37,9 @@ namespace Kernel::Input } - BAN::ErrorOr PS2Keyboard::create(PS2Controller& controller, dev_t device) + BAN::ErrorOr PS2Keyboard::create(PS2Controller& controller) { - PS2Keyboard* keyboard = new PS2Keyboard(controller, device); + PS2Keyboard* keyboard = new PS2Keyboard(controller); if (keyboard == nullptr) return BAN::Error::from_errno(ENOMEM); BAN::ScopeGuard guard([keyboard] { delete keyboard; }); @@ -48,6 +48,12 @@ namespace Kernel::Input return keyboard; } + PS2Keyboard::PS2Keyboard(PS2Controller& controller) + : m_controller(controller) + , m_name(BAN::String::formatted("input{}", DeviceManager::get().get_next_input_dev())) + , m_rdev(makedev(DeviceManager::get().get_next_rdev(), 0)) + { } + void PS2Keyboard::on_byte(uint8_t byte) { // NOTE: This implementation does not allow using commands diff --git a/kernel/kernel/Shell.cpp b/kernel/kernel/Shell.cpp index 4716fd8cef..0995e11cdb 100644 --- a/kernel/kernel/Shell.cpp +++ b/kernel/kernel/Shell.cpp @@ -455,9 +455,9 @@ argument_done: TTY_PRINTLN(" File: {}", arguments[1]); TTY_PRINTLN(" Size: {}\tBlocks: {}\tIO Block: {}\t {}", st.st_size, st.st_blocks, st.st_blksize, type); - TTY_PRINT("Device: {},{}\tInode: {}\tLinks: {}", st.st_dev >> 8, st.st_dev & 0xFF, st.st_ino, st.st_nlink); + TTY_PRINT("Device: {},{}\tInode: {}\tLinks: {}", major(st.st_dev), minor(st.st_dev), st.st_ino, st.st_nlink); if (st.st_rdev) - TTY_PRINT("\tDevice type: {},{}", st.st_rdev >> 8, st.st_rdev & 0xFF); + TTY_PRINT("\tDevice type: {},{}", major(st.st_rdev), minor(st.st_rdev)); TTY_PRINTLN(""); TTY_PRINTLN("Access: ({4O}/{})\tUid: {}\tGid: {}", mode.mode & 0777, mode_string(mode), st.st_uid, st.st_gid); TTY_PRINTLN("Access: {}", BAN::from_unix_time(st.st_atime)); diff --git a/kernel/kernel/Storage/StorageDevice.cpp b/kernel/kernel/Storage/StorageDevice.cpp index e7dde8cd79..500845e7d6 100644 --- a/kernel/kernel/Storage/StorageDevice.cpp +++ b/kernel/kernel/Storage/StorageDevice.cpp @@ -189,7 +189,7 @@ namespace Kernel entry.ending_lba, entry.attributes, utf8_name, - i + i + 1 ); ASSERT(partition != nullptr); MUST(m_partitions.push_back(partition)); @@ -211,6 +211,11 @@ namespace Kernel memcpy(m_label, label, sizeof(m_label)); } + dev_t Partition::rdev() const + { + return makedev(major(m_device.rdev()), m_index); + } + BAN::ErrorOr Partition::read_sectors(uint64_t lba, uint8_t sector_count, uint8_t* buffer) { const uint32_t sectors_in_partition = m_lba_end - m_lba_start; @@ -229,16 +234,6 @@ namespace Kernel return {}; } - blksize_t Partition::blksize() const - { - return m_device.blksize(); - } - - dev_t Partition::dev() const - { - return m_device.dev(); - } - BAN::ErrorOr Partition::read(size_t offset, void* buffer, size_t bytes) { if (offset % m_device.sector_size() || bytes % m_device.sector_size())