1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 15:37:46 +00:00

Kernel: Move all boot-related code to the new Boot subdirectory

This commit is contained in:
Liav A 2023-02-24 20:21:53 +02:00 committed by Jelle Raaijmakers
parent c9a34cae66
commit 8f21420a1d
31 changed files with 34 additions and 34 deletions

43
Kernel/Boot/BootInfo.h Normal file
View file

@ -0,0 +1,43 @@
/*
* Copyright (c) 2021, Gunnar Beutner <gbeutner@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <AK/StringView.h>
#include <Kernel/Boot/Multiboot.h>
#include <Kernel/Memory/PhysicalAddress.h>
#include <Kernel/Memory/VirtualAddress.h>
namespace Kernel::Memory {
class PageTableEntry;
}
extern "C" PhysicalAddress start_of_prekernel_image;
extern "C" PhysicalAddress end_of_prekernel_image;
extern "C" size_t physical_to_virtual_offset;
extern "C" FlatPtr kernel_mapping_base;
extern "C" FlatPtr kernel_load_base;
#if ARCH(X86_64)
extern "C" u32 gdt64ptr;
extern "C" u16 code64_sel;
#endif
extern "C" PhysicalAddress boot_pml4t;
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" 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;
extern "C" multiboot_module_entry_t* multiboot_modules;
extern "C" size_t multiboot_modules_count;
extern "C" PhysicalAddress multiboot_framebuffer_addr;
extern "C" u32 multiboot_framebuffer_pitch;
extern "C" u32 multiboot_framebuffer_width;
extern "C" u32 multiboot_framebuffer_height;
extern "C" u8 multiboot_framebuffer_bpp;
extern "C" u8 multiboot_framebuffer_type;

320
Kernel/Boot/CommandLine.cpp Normal file
View file

@ -0,0 +1,320 @@
/*
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <AK/StringBuilder.h>
#include <Kernel/Boot/CommandLine.h>
#include <Kernel/Library/Panic.h>
#include <Kernel/Library/StdLib.h>
#include <Kernel/Sections.h>
namespace Kernel {
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(StringView cmd_line)
{
(void)cmd_line.copy_characters_to_buffer(s_cmd_line, sizeof(s_cmd_line));
}
bool CommandLine::was_initialized()
{
return s_the != nullptr;
}
CommandLine const& kernel_command_line()
{
VERIFY(s_the);
return *s_the;
}
UNMAP_AFTER_INIT void CommandLine::initialize()
{
VERIFY(!s_the);
s_the = new CommandLine({ s_cmd_line, strlen(s_cmd_line) });
dmesgln("Kernel Commandline: {}", kernel_command_line().string());
// Validate the modes the user passed in.
(void)s_the->panic_mode(Validate::Yes);
}
UNMAP_AFTER_INIT NonnullOwnPtr<KString> CommandLine::build_commandline(StringView cmdline_from_bootloader)
{
StringBuilder builder;
builder.append(cmdline_from_bootloader);
if constexpr (!s_embedded_cmd_line.is_empty()) {
builder.append(' ');
builder.append(s_embedded_cmd_line);
}
return KString::must_create(builder.string_view());
}
UNMAP_AFTER_INIT void CommandLine::add_arguments(Vector<StringView> const& args)
{
for (auto&& str : args) {
if (str == ""sv) {
continue;
}
// Some boot loaders may include complex key-value pairs where the value is a composite entry,
// we handle this by only checking for the first equals sign in each command line parameter.
auto key = str.find_first_split_view('=');
if (key.length() == str.length())
m_params.set(key, ""sv);
else
m_params.set(key, str.substring_view(key.length() + 1));
}
}
UNMAP_AFTER_INIT CommandLine::CommandLine(StringView cmdline_from_bootloader)
: m_string(build_commandline(cmdline_from_bootloader))
{
s_the = this;
auto const& args = m_string->view().split_view(' ');
MUST(m_params.try_ensure_capacity(args.size()));
add_arguments(args);
}
Optional<StringView> CommandLine::lookup(StringView key) const
{
return m_params.get(key);
}
bool CommandLine::contains(StringView key) const
{
return m_params.contains(key);
}
UNMAP_AFTER_INIT bool CommandLine::is_boot_profiling_enabled() const
{
return contains("boot_prof"sv);
}
UNMAP_AFTER_INIT bool CommandLine::is_ide_enabled() const
{
return !contains("disable_ide"sv);
}
UNMAP_AFTER_INIT bool CommandLine::is_smp_enabled() const
{
// Note: We can't enable SMP mode without enabling the IOAPIC.
if (!is_ioapic_enabled())
return false;
return lookup("smp"sv).value_or("off"sv) == "on"sv;
}
UNMAP_AFTER_INIT bool CommandLine::is_smp_enabled_without_ioapic_enabled() const
{
auto smp_enabled = lookup("smp"sv).value_or("off"sv) == "on"sv;
return smp_enabled && !is_ioapic_enabled();
}
UNMAP_AFTER_INIT bool CommandLine::is_ioapic_enabled() const
{
auto value = lookup("enable_ioapic"sv).value_or("on"sv);
if (value == "on"sv)
return true;
if (value == "off"sv)
return false;
PANIC("Unknown enable_ioapic setting: {}", value);
}
UNMAP_AFTER_INIT bool CommandLine::is_early_boot_console_disabled() const
{
auto value = lookup("early_boot_console"sv).value_or("on"sv);
if (value == "on"sv)
return false;
if (value == "off"sv)
return true;
PANIC("Unknown early_boot_console setting: {}", value);
}
UNMAP_AFTER_INIT I8042PresenceMode CommandLine::i8042_presence_mode() const
{
auto value = lookup("i8042_presence_mode"sv).value_or("auto"sv);
if (value == "auto"sv)
return I8042PresenceMode::Automatic;
if (value == "none"sv)
return I8042PresenceMode::None;
if (value == "force"sv)
return I8042PresenceMode::Force;
if (value == "aggressive-test"sv)
return I8042PresenceMode::AggressiveTest;
PANIC("Unknown i8042_presence_mode setting: {}", value);
}
UNMAP_AFTER_INIT bool CommandLine::is_vmmouse_enabled() const
{
return lookup("vmmouse"sv).value_or("on"sv) == "on"sv;
}
UNMAP_AFTER_INIT PCIAccessLevel CommandLine::pci_access_level() const
{
auto value = lookup("pci"sv).value_or("ecam"sv);
if (value == "ecam"sv)
return PCIAccessLevel::MemoryAddressing;
#if ARCH(X86_64)
if (value == "io"sv)
return PCIAccessLevel::IOAddressing;
#endif
if (value == "none"sv)
return PCIAccessLevel::None;
PANIC("Unknown PCI ECAM setting: {}", value);
}
UNMAP_AFTER_INIT bool CommandLine::is_pci_disabled() const
{
return lookup("pci"sv).value_or("ecam"sv) == "none"sv;
}
UNMAP_AFTER_INIT bool CommandLine::is_legacy_time_enabled() const
{
return lookup("time"sv).value_or("modern"sv) == "legacy"sv;
}
bool CommandLine::is_pc_speaker_enabled() const
{
auto value = lookup("pcspeaker"sv).value_or("off"sv);
if (value == "on"sv)
return true;
if (value == "off"sv)
return false;
PANIC("Unknown pcspeaker setting: {}", value);
}
UNMAP_AFTER_INIT bool CommandLine::is_force_pio() const
{
return contains("force_pio"sv);
}
UNMAP_AFTER_INIT StringView CommandLine::root_device() const
{
return lookup("root"sv).value_or("lun0:0:0"sv);
}
bool CommandLine::is_nvme_polling_enabled() const
{
return contains("nvme_poll"sv);
}
UNMAP_AFTER_INIT AcpiFeatureLevel CommandLine::acpi_feature_level() const
{
auto value = kernel_command_line().lookup("acpi"sv).value_or("limited"sv);
if (value == "limited"sv)
return AcpiFeatureLevel::Limited;
if (value == "off"sv)
return AcpiFeatureLevel::Disabled;
if (value == "on"sv)
return AcpiFeatureLevel::Enabled;
PANIC("Unknown ACPI feature level: {}", value);
}
UNMAP_AFTER_INIT HPETMode CommandLine::hpet_mode() const
{
auto hpet_mode = lookup("hpet"sv).value_or("periodic"sv);
if (hpet_mode == "periodic"sv)
return HPETMode::Periodic;
if (hpet_mode == "nonperiodic"sv)
return HPETMode::NonPeriodic;
PANIC("Unknown HPETMode: {}", hpet_mode);
}
UNMAP_AFTER_INIT bool CommandLine::is_physical_networking_disabled() const
{
return contains("disable_physical_networking"sv);
}
UNMAP_AFTER_INIT bool CommandLine::disable_physical_storage() const
{
return contains("disable_physical_storage"sv);
}
UNMAP_AFTER_INIT bool CommandLine::disable_uhci_controller() const
{
return contains("disable_uhci_controller"sv);
}
UNMAP_AFTER_INIT bool CommandLine::disable_usb() const
{
return contains("disable_usb"sv);
}
UNMAP_AFTER_INIT bool CommandLine::disable_virtio() const
{
return contains("disable_virtio"sv);
}
UNMAP_AFTER_INIT AHCIResetMode CommandLine::ahci_reset_mode() const
{
auto const ahci_reset_mode = lookup("ahci_reset_mode"sv).value_or("controllers"sv);
if (ahci_reset_mode == "controllers"sv) {
return AHCIResetMode::ControllerOnly;
}
if (ahci_reset_mode == "aggressive"sv) {
return AHCIResetMode::Aggressive;
}
PANIC("Unknown AHCIResetMode: {}", ahci_reset_mode);
}
StringView CommandLine::system_mode() const
{
return lookup("system_mode"sv).value_or("graphical"sv);
}
PanicMode CommandLine::panic_mode(Validate should_validate) const
{
auto const panic_mode = lookup("panic"sv).value_or("halt"sv);
if (panic_mode == "halt"sv) {
return PanicMode::Halt;
}
if (panic_mode == "shutdown"sv) {
return PanicMode::Shutdown;
}
if (should_validate == Validate::Yes)
PANIC("Unknown PanicMode: {}", panic_mode);
return PanicMode::Halt;
}
UNMAP_AFTER_INIT CommandLine::GraphicsSubsystemMode CommandLine::graphics_subsystem_mode() const
{
auto const graphics_subsystem_mode_value = lookup("graphics_subsystem_mode"sv).value_or("on"sv);
if (graphics_subsystem_mode_value == "on"sv)
return GraphicsSubsystemMode::Enabled;
if (graphics_subsystem_mode_value == "limited"sv)
return GraphicsSubsystemMode::Limited;
if (graphics_subsystem_mode_value == "off"sv)
return GraphicsSubsystemMode::Disabled;
PANIC("Invalid graphics_subsystem_mode value: {}", graphics_subsystem_mode_value);
}
StringView CommandLine::userspace_init() const
{
return lookup("init"sv).value_or("/bin/SystemServer"sv);
}
Vector<NonnullOwnPtr<KString>> CommandLine::userspace_init_args() const
{
Vector<NonnullOwnPtr<KString>> args;
auto init_args = lookup("init_args"sv).value_or(""sv).split_view(';');
if (!init_args.is_empty())
MUST(args.try_prepend(MUST(KString::try_create(userspace_init()))));
for (auto& init_arg : init_args)
args.append(MUST(KString::try_create(init_arg)));
return args;
}
UNMAP_AFTER_INIT size_t CommandLine::switch_to_tty() const
{
auto const default_tty = lookup("switch_to_tty"sv).value_or("1"sv);
auto switch_tty_number = default_tty.to_uint();
if (switch_tty_number.has_value() && switch_tty_number.value() >= 1) {
return switch_tty_number.value() - 1;
}
PANIC("Invalid default tty value: {}", default_tty);
}
}

116
Kernel/Boot/CommandLine.h Normal file
View file

@ -0,0 +1,116 @@
/*
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <AK/HashMap.h>
#include <AK/Optional.h>
#include <AK/Vector.h>
#include <Kernel/Library/KString.h>
namespace Kernel {
enum class PanicMode {
Halt,
Shutdown,
};
enum class HPETMode {
Periodic,
NonPeriodic
};
enum class I8042PresenceMode {
Automatic,
AggressiveTest,
Force,
None,
};
enum class AcpiFeatureLevel {
Enabled,
Limited,
Disabled,
};
enum class PCIAccessLevel {
None,
#if ARCH(X86_64)
IOAddressing,
#endif
MemoryAddressing,
};
enum class AHCIResetMode {
ControllerOnly,
Aggressive,
};
class CommandLine {
public:
static void early_initialize(StringView cmd_line);
static void initialize();
static bool was_initialized();
enum class Validate {
Yes,
No,
};
enum class GraphicsSubsystemMode {
Enabled,
Limited,
Disabled
};
[[nodiscard]] StringView string() const { return m_string->view(); }
Optional<StringView> lookup(StringView key) const;
[[nodiscard]] bool contains(StringView key) const;
[[nodiscard]] bool is_boot_profiling_enabled() const;
[[nodiscard]] bool is_ide_enabled() const;
[[nodiscard]] bool is_ioapic_enabled() const;
[[nodiscard]] bool is_smp_enabled_without_ioapic_enabled() const;
[[nodiscard]] bool is_smp_enabled() const;
[[nodiscard]] bool is_physical_networking_disabled() const;
[[nodiscard]] bool is_vmmouse_enabled() const;
[[nodiscard]] PCIAccessLevel pci_access_level() const;
[[nodiscard]] bool is_pci_disabled() const;
[[nodiscard]] bool is_legacy_time_enabled() const;
[[nodiscard]] bool is_pc_speaker_enabled() const;
[[nodiscard]] GraphicsSubsystemMode graphics_subsystem_mode() const;
[[nodiscard]] I8042PresenceMode i8042_presence_mode() const;
[[nodiscard]] bool is_force_pio() const;
[[nodiscard]] AcpiFeatureLevel acpi_feature_level() const;
[[nodiscard]] StringView system_mode() const;
[[nodiscard]] PanicMode panic_mode(Validate should_validate = Validate::No) const;
[[nodiscard]] HPETMode hpet_mode() const;
[[nodiscard]] bool disable_physical_storage() const;
[[nodiscard]] bool disable_uhci_controller() const;
[[nodiscard]] bool disable_usb() const;
[[nodiscard]] bool disable_virtio() const;
[[nodiscard]] bool is_early_boot_console_disabled() const;
[[nodiscard]] AHCIResetMode ahci_reset_mode() const;
[[nodiscard]] StringView userspace_init() const;
[[nodiscard]] Vector<NonnullOwnPtr<KString>> userspace_init_args() const;
[[nodiscard]] StringView root_device() const;
[[nodiscard]] bool is_nvme_polling_enabled() const;
[[nodiscard]] size_t switch_to_tty() const;
private:
CommandLine(StringView);
void add_arguments(Vector<StringView> const& args);
static NonnullOwnPtr<KString> build_commandline(StringView cmdline_from_bootloader);
NonnullOwnPtr<KString> m_string;
HashMap<StringView, StringView> m_params;
};
CommandLine const& kernel_command_line();
}

130
Kernel/Boot/Multiboot.h Normal file
View file

@ -0,0 +1,130 @@
/*
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <AK/Types.h>
struct multiboot_module_entry {
u32 start;
u32 end;
u32 string_addr;
u32 reserved;
};
typedef struct multiboot_module_entry multiboot_module_entry_t;
struct multiboot_aout_symbol_table {
u32 tabsize;
u32 strsize;
u32 addr;
u32 reserved;
};
typedef struct multiboot_aout_symbol_table multiboot_aout_symbol_table_t;
struct multiboot_elf_section_header_table {
u32 num;
u32 size;
u32 addr;
u32 shndx;
};
typedef struct multiboot_elf_section_header_table multiboot_elf_section_header_table_t;
#define MULTIBOOT_MEMORY_AVAILABLE 1
#define MULTIBOOT_MEMORY_RESERVED 2
#define MULTIBOOT_MEMORY_ACPI_RECLAIMABLE 3
#define MULTIBOOT_MEMORY_NVS 4
#define MULTIBOOT_MEMORY_BADRAM 5
struct multiboot_mmap_entry {
u32 size;
u64 addr;
u64 len;
u32 type;
#if ARCH(AARCH64)
// __attribute__((packed)) causes alignment issues on aarch64
};
#else
} __attribute__((packed));
#endif
typedef struct multiboot_mmap_entry multiboot_memory_map_t;
#define MULTIBOOT_INFO_FRAMEBUFFER_INFO (1 << 12)
struct multiboot_info {
// Multiboot info version number.
u32 flags;
// Available memory from BIOS.
u32 mem_lower;
u32 mem_upper;
// "root" partition.
u32 boot_device;
// Kernel command line.
u32 cmdline;
// Boot-Module list.
u32 mods_count;
u32 mods_addr;
union {
multiboot_aout_symbol_table_t aout_sym;
multiboot_elf_section_header_table_t elf_sec;
} u;
// Memory Mapping buffer.
u32 mmap_length;
u32 mmap_addr;
// Drive Info buffer.
u32 drives_length;
u32 drives_addr;
// ROM configuration table.
u32 config_table;
// Boot Loader Name.
u32 boot_loader_name;
// APM table.
u32 apm_table;
// Video.
u32 vbe_control_info;
u32 vbe_mode_info;
u16 vbe_mode;
u16 vbe_interface_seg;
u16 vbe_interface_off;
u16 vbe_interface_len;
u64 framebuffer_addr;
u32 framebuffer_pitch;
u32 framebuffer_width;
u32 framebuffer_height;
u8 framebuffer_bpp;
#define MULTIBOOT_FRAMEBUFFER_TYPE_INDEXED 0
#define MULTIBOOT_FRAMEBUFFER_TYPE_RGB 1
#define MULTIBOOT_FRAMEBUFFER_TYPE_EGA_TEXT 2
u8 framebuffer_type;
union {
struct
{
u32 framebuffer_palette_addr;
u16 framebuffer_palette_num_colors;
};
struct
{
u8 framebuffer_red_field_position;
u8 framebuffer_red_mask_size;
u8 framebuffer_green_field_position;
u8 framebuffer_green_mask_size;
u8 framebuffer_blue_field_position;
u8 framebuffer_blue_mask_size;
};
};
};
typedef struct multiboot_info multiboot_info_t;