From 2995a369427b5ac7090f7e3f072f7c65b4e735f2 Mon Sep 17 00:00:00 2001 From: Bananymous Date: Thu, 30 Mar 2023 16:37:53 +0300 Subject: [PATCH] Kernel: root partition is now passed from the commandline --- disk.sh | 19 +++---- kernel/include/kernel/FS/VirtualFileSystem.h | 2 +- kernel/kernel/FS/VirtualFileSystem.cpp | 11 +++- kernel/kernel/kernel.cpp | 59 +++++++++++--------- 4 files changed, 49 insertions(+), 42 deletions(-) diff --git a/disk.sh b/disk.sh index a0113ec8e..f599ac7ba 100755 --- a/disk.sh +++ b/disk.sh @@ -30,14 +30,9 @@ sed -e 's/\s*\([-\+[:alnum:]]*\).*/\1/' << EOF | fdisk $DISK_NAME t # set type 2 # ... of partition 2 20 # Linux filesystem - x # expert menu - n # partition name - 3 # ... of partition 3 - mount-test - n # partition name - 2 # ... of partition 2 - banan-root - r # back to main menu + t # set type + 3 # ... of partition 3 + 20 # Linux filesystem w # write changes EOF @@ -63,16 +58,16 @@ sudo grub-install --no-floppy --target=i386-pc --modules="normal ext2 multiboot" echo -e ' menuentry "banan-os" { - multiboot /boot/banan-os.kernel + multiboot /boot/banan-os.kernel root=/dev/hda1 } menuentry "banan-os (no serial)" { - multiboot /boot/banan-os.kernel noserial + multiboot /boot/banan-os.kernel root=/dev/hda1 noserial } menuentry "banan-os (no apic)" { - multiboot /boot/banan-os.kernel noapic + multiboot /boot/banan-os.kernel root=/dev/hda1 noapic } menuentry "banan-os (no apic, no serial)" { - multiboot /boot/banan-os.kernel noapic noserial + multiboot /boot/banan-os.kernel root=/dev/hda1 noapic noserial } ' | sudo tee ${MOUNT_DIR}/boot/grub/grub.cfg diff --git a/kernel/include/kernel/FS/VirtualFileSystem.h b/kernel/include/kernel/FS/VirtualFileSystem.h index c701fe558..18463c03e 100644 --- a/kernel/include/kernel/FS/VirtualFileSystem.h +++ b/kernel/include/kernel/FS/VirtualFileSystem.h @@ -12,7 +12,7 @@ namespace Kernel class VirtualFileSystem : public FileSystem { public: - static BAN::ErrorOr initialize(); + static BAN::ErrorOr initialize(BAN::StringView); static VirtualFileSystem& get(); virtual ~VirtualFileSystem() {}; diff --git a/kernel/kernel/FS/VirtualFileSystem.cpp b/kernel/kernel/FS/VirtualFileSystem.cpp index 6cc69e2ab..85bfe4a31 100644 --- a/kernel/kernel/FS/VirtualFileSystem.cpp +++ b/kernel/kernel/FS/VirtualFileSystem.cpp @@ -12,7 +12,7 @@ namespace Kernel static VirtualFileSystem* s_instance = nullptr; - BAN::ErrorOr VirtualFileSystem::initialize() + BAN::ErrorOr VirtualFileSystem::initialize(BAN::StringView root) { ASSERT(s_instance == nullptr); s_instance = new VirtualFileSystem(); @@ -20,8 +20,13 @@ namespace Kernel return BAN::Error::from_errno(ENOMEM); BAN::ScopeGuard guard([] { delete s_instance; s_instance = nullptr; } ); - auto partition_inode = TRY(DeviceManager::get().read_directory_inode("hda1")); + if (root.size() < 5 || root.substring(0, 5) != "/dev/") + return BAN::Error::from_c_string("root must be in /dev/"); + root = root.substring(5); + + auto partition_inode = TRY(DeviceManager::get().read_directory_inode(root)); s_instance->m_root_fs = TRY(Ext2FS::create(*(Partition*)partition_inode.ptr())); + TRY(s_instance->mount(&DeviceManager::get(), "/dev")); guard.disable(); @@ -76,7 +81,7 @@ namespace Kernel const auto path_parts = TRY(path.split('/')); - for (const auto& path_part : path_parts) + for (auto path_part : path_parts) { if (path_part.empty() || path_part == "."sv) { diff --git a/kernel/kernel/kernel.cpp b/kernel/kernel/kernel.cpp index 1343b5942..c8044deee 100644 --- a/kernel/kernel/kernel.cpp +++ b/kernel/kernel/kernel.cpp @@ -27,14 +27,13 @@ struct ParsedCommandLine { bool force_pic = false; bool disable_serial = false; + BAN::StringView root; }; -ParsedCommandLine ParseCommandLine() +static bool should_disable_serial() { - ParsedCommandLine result; - if (!(g_multiboot_info->flags & 0x02)) - return result; + return false; const char* start = g_kernel_cmdline; const char* current = g_kernel_cmdline; @@ -42,12 +41,8 @@ ParsedCommandLine ParseCommandLine() { if (!*current || *current == ' ' || *current == '\t') { - if (current - start == 6 && memcmp(start, "noapic", 6) == 0) - result.force_pic = true; - if (current - start == 8 && memcmp(start, "noserial", 8) == 0) - result.disable_serial = true; - + return true; if (!*current) break; start = current + 1; @@ -55,7 +50,28 @@ ParsedCommandLine ParseCommandLine() current++; } - return result; + return false; +} + +static ParsedCommandLine cmdline; + +static void parse_command_line() +{ + if (!(g_multiboot_info->flags & 0x02)) + return; + + BAN::StringView full_command_line(g_kernel_cmdline); + auto arguments = MUST(full_command_line.split(' ')); + + for (auto argument : arguments) + { + if (argument == "noapic") + cmdline.force_pic = true; + else if (argument == "noserial") + cmdline.disable_serial = true; + else if (argument.size() > 5 && argument.substring(0, 5) == "root=") + cmdline.root = argument.substring(5); + } } struct Test @@ -87,8 +103,7 @@ extern "C" uintptr_t g_userspace_end; extern void userspace_entry(); -void init2(void*); -void device_updater(void*); +static void init2(void*); extern "C" void kernel_main() { @@ -96,9 +111,7 @@ extern "C" void kernel_main() DISABLE_INTERRUPTS(); - auto cmdline = ParseCommandLine(); - - if (!cmdline.disable_serial) + if (!should_disable_serial()) Serial::initialize(); if (g_multiboot_magic != 0x2BADB002) { @@ -119,6 +132,9 @@ extern "C" void kernel_main() MMU::intialize(); dprintln("MMU initialized"); + parse_command_line(); + dprintln("command line parsed, root='{}'", cmdline.root); + PCI::initialize(); dprintln("PCI initialized"); @@ -189,16 +205,7 @@ extern "C" void kernel_main() ASSERT(false); } -void device_updater(void*) -{ - while (true) - { - Kernel::DeviceManager::get().update(); - PIT::sleep(1); - } -} - -void init2(void* tty1_ptr) +static void init2(void* tty1_ptr) { using namespace Kernel; using namespace Kernel::Input; @@ -207,7 +214,7 @@ void init2(void* tty1_ptr) DeviceManager::initialize(); - MUST(VirtualFileSystem::initialize()); + MUST(VirtualFileSystem::initialize(cmdline.root)); if (auto res = PS2Controller::initialize(); res.is_error()) dprintln("{}", res.error());