From 6aa392f6e413be166879c3fef3864905d3474aa4 Mon Sep 17 00:00:00 2001 From: Daniel Bertalan Date: Mon, 24 Apr 2023 20:14:44 +0200 Subject: [PATCH] Kernel: Store the kernel command line in a `StringView` The Raspberry Pi's mailbox interface does not guarantee that the returned command line is null-terminated. This commit removes that assumption from the current code, allowing the next commit to add support for reading it on the Pi. This also lets us eliminate a few manual `strlen()` calls :^) --- Kernel/Arch/init.cpp | 9 +++++---- Kernel/BootInfo.h | 3 ++- Kernel/CommandLine.cpp | 10 ++-------- Kernel/CommandLine.h | 2 +- 4 files changed, 10 insertions(+), 14 deletions(-) diff --git a/Kernel/Arch/init.cpp b/Kernel/Arch/init.cpp index 8f7cf5381b..883e450d7d 100644 --- a/Kernel/Arch/init.cpp +++ b/Kernel/Arch/init.cpp @@ -133,7 +133,7 @@ READONLY_AFTER_INIT PhysicalAddress boot_pdpt; READONLY_AFTER_INIT PhysicalAddress boot_pd0; READONLY_AFTER_INIT PhysicalAddress boot_pd_kernel; READONLY_AFTER_INIT Memory::PageTableEntry* boot_pd_kernel_pt1023; -READONLY_AFTER_INIT char const* kernel_cmdline; +READONLY_AFTER_INIT StringView kernel_cmdline; READONLY_AFTER_INIT u32 multiboot_flags; READONLY_AFTER_INIT multiboot_memory_map_t* multiboot_memory_map; READONLY_AFTER_INIT size_t multiboot_memory_map_count; @@ -166,7 +166,8 @@ extern "C" [[noreturn]] UNMAP_AFTER_INIT void init([[maybe_unused]] BootInfo con boot_pd0 = PhysicalAddress { boot_info.boot_pd0 }; boot_pd_kernel = PhysicalAddress { boot_info.boot_pd_kernel }; boot_pd_kernel_pt1023 = (Memory::PageTableEntry*)boot_info.boot_pd_kernel_pt1023; - kernel_cmdline = (char const*)boot_info.kernel_cmdline; + char const* cmdline = (char const*)boot_info.kernel_cmdline; + kernel_cmdline = StringView { cmdline, strlen(cmdline) }; multiboot_flags = boot_info.multiboot_flags; multiboot_memory_map = (multiboot_memory_map_t*)boot_info.multiboot_memory_map; multiboot_memory_map_count = boot_info.multiboot_memory_map_count; @@ -192,7 +193,7 @@ extern "C" [[noreturn]] UNMAP_AFTER_INIT void init([[maybe_unused]] BootInfo con multiboot_module_entry_t modules[] = {}; multiboot_modules = modules; multiboot_modules_count = 0; - kernel_cmdline = ""; + kernel_cmdline = ""sv; #endif setup_serial_debug(); @@ -442,7 +443,7 @@ UNMAP_AFTER_INIT void setup_serial_debug() // serial_debug will output all the dbgln() data to COM1 at // 8-N-1 57600 baud. this is particularly useful for debugging the boot // process on live hardware. - if (StringView { kernel_cmdline, strlen(kernel_cmdline) }.contains("serial_debug"sv)) { + if (kernel_cmdline.contains("serial_debug"sv)) { set_serial_debug_enabled(true); } } diff --git a/Kernel/BootInfo.h b/Kernel/BootInfo.h index 4724f0b12f..af3300f27a 100644 --- a/Kernel/BootInfo.h +++ b/Kernel/BootInfo.h @@ -6,6 +6,7 @@ #pragma once +#include #include #include #include @@ -28,7 +29,7 @@ extern "C" PhysicalAddress boot_pdpt; extern "C" PhysicalAddress boot_pd0; extern "C" PhysicalAddress boot_pd_kernel; extern "C" Kernel::Memory::PageTableEntry* boot_pd_kernel_pt1023; -extern "C" char const* kernel_cmdline; +extern "C" StringView kernel_cmdline; extern "C" u32 multiboot_flags; extern "C" multiboot_memory_map_t* multiboot_memory_map; extern "C" size_t multiboot_memory_map_count; diff --git a/Kernel/CommandLine.cpp b/Kernel/CommandLine.cpp index 23245e044f..d52f4ce0e0 100644 --- a/Kernel/CommandLine.cpp +++ b/Kernel/CommandLine.cpp @@ -16,15 +16,9 @@ static char s_cmd_line[1024]; static constexpr StringView s_embedded_cmd_line = ""sv; static CommandLine* s_the; -UNMAP_AFTER_INIT void CommandLine::early_initialize(char const* cmd_line) +UNMAP_AFTER_INIT void CommandLine::early_initialize(StringView cmd_line) { - if (!cmd_line) - return; - size_t length = strlen(cmd_line); - if (length >= sizeof(s_cmd_line)) - length = sizeof(s_cmd_line) - 1; - memcpy(s_cmd_line, cmd_line, length); - s_cmd_line[length] = '\0'; + (void)cmd_line.copy_characters_to_buffer(s_cmd_line, sizeof(s_cmd_line)); } bool CommandLine::was_initialized() diff --git a/Kernel/CommandLine.h b/Kernel/CommandLine.h index af67aea472..decaf2aae2 100644 --- a/Kernel/CommandLine.h +++ b/Kernel/CommandLine.h @@ -52,7 +52,7 @@ enum class AHCIResetMode { class CommandLine { public: - static void early_initialize(char const* cmd_line); + static void early_initialize(StringView cmd_line); static void initialize(); static bool was_initialized();