ladybird/Kernel/CommandLine.cpp
Liav A 2c84466ad8 Kernel/Storage: Introduce new boot device addressing modes
Before of this patch, we supported two methods to address a boot device:
1. Specifying root=/dev/hdXY, where X is a-z letter which corresponds to
a boot device, and Y as number from 1 to 16, to indicate the partition
number, which can be omitted to instruct the kernel to use a raw device
rather than a partition on a raw device.
2. Specifying root=PARTUUID: with a GUID string of a GUID partition. In
case of existing storage device with GPT partitions, this is most likely
the safest option to ensure booting from persistent storage.

While option 2 is more advanced and reliable, the first option has 2
caveats:
1. The string prefix "/dev/hd" doesn't mean anything beside a convention
on Linux installations, that was taken into use in Serenity. In Serenity
we don't mount DevTmpFS before we mount the boot device on /, so the
kernel doesn't really access /dev anyway, so this convention is only a
big misleading relic that can easily make the user to assume we access
/dev early on boot.
2. This convention although resemble the simple linux convention, is
quite limited in specifying a correct boot device across hardware setup
changes, so option 2 was recommended to ensure the system is always
bootable.

With these caveats in mind, this commit tries to fix the problem with
adding more addressing options as well as to remove the first option
being mentioned above of addressing.
To sum it up, there are 4 addressing options:
1. Hardware relative address - Each instance of StorageController is
assigned with a index number relative to the type of hardware it handles
which makes it possible to address storage devices with a prefix of the
commandset ("ata" for ATA, "nvme" for NVMe, "ramdisk" for Plain memory),
and then the number for the parent controller relative hardware index,
another number LUN target_id, and a third number for LUN disk_id.
2. LUN address - Similar to the previous option, but instead we rely on
the parent controller absolute index for the first number.
3. Block device major and minor numbers - by specifying the major and
minor numbers, the kernel can simply try to get the corresponding block
device and use it as the boot device.
4. GUID string, in the same fashion like before, so the user use the
"PARTUUID:" string prefix and add the GUID of the GPT partition.

For the new address modes 1 and 2, the user can choose to also specify a
partition out of the selected boot device. To do that, the user needs to
append the semicolon character and then add the string "partX" where X
is to be changed for the partition number. We start counting from 0, and
therefore the first partition number is 0 and not 1 in the kernel boot
argument.
2022-08-30 00:50:15 +01:00

317 lines
8.6 KiB
C++

/*
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <AK/StringBuilder.h>
#include <Kernel/CommandLine.h>
#include <Kernel/Panic.h>
#include <Kernel/Sections.h>
#include <Kernel/StdLib.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(char const* 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';
}
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;
}
auto pair = str.split_view('=', false);
VERIFY(pair.size() == 2 || pair.size() == 1);
if (pair.size() == 1) {
m_params.set(pair[0], ""sv);
} else {
m_params.set(pair[0], pair[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(' ');
m_params.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 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 (value == "io"sv)
return PCIAccessLevel::IOAddressing;
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_ps2_controller() const
{
return contains("disable_ps2_controller"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);
}
NonnullOwnPtrVector<KString> CommandLine::userspace_init_args() const
{
NonnullOwnPtrVector<KString> args;
auto init_args = lookup("init_args"sv).value_or(""sv).split_view(';');
if (!init_args.is_empty())
MUST(args.try_prepend(KString::must_create(userspace_init())));
for (auto& init_arg : init_args)
args.append(KString::must_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);
}
}