ladybird/Kernel/CommandLine.cpp

250 lines
6.7 KiB
C++
Raw Normal View History

/*
* 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 = "";
static CommandLine* s_the;
UNMAP_AFTER_INIT void CommandLine::early_initialize(const char* 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';
}
const CommandLine& 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);
dmesgln("Kernel Commandline: {}", kernel_command_line().string());
// Validate the boot mode the user passed in.
(void)s_the->boot_mode(Validate::Yes);
}
UNMAP_AFTER_INIT void CommandLine::build_commandline(const String& 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);
}
m_string = builder.to_string();
}
UNMAP_AFTER_INIT void CommandLine::add_arguments(const Vector<StringView>& 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(move(pair[0]), ""sv);
} else {
m_params.set(move(pair[0]), move(pair[1]));
}
}
}
UNMAP_AFTER_INIT CommandLine::CommandLine(const String& cmdline_from_bootloader)
{
s_the = this;
build_commandline(cmdline_from_bootloader);
const auto& args = m_string.split_view(' ');
m_params.ensure_capacity(args.size());
add_arguments(args);
}
Optional<StringView> CommandLine::lookup(const StringView& key) const
{
return m_params.get(key);
}
bool CommandLine::contains(const 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
{
return lookup("smp"sv).value_or("off"sv) == "on"sv;
}
UNMAP_AFTER_INIT bool CommandLine::is_vmmouse_enabled() const
{
return lookup("vmmouse"sv).value_or("on") == "on"sv;
}
UNMAP_AFTER_INIT PCIAccessLevel CommandLine::pci_access_level() const
{
auto value = lookup("pci_ecam"sv).value_or("on"sv);
if (value == "on"sv)
Kernel/PCI: Simplify the entire subsystem A couple of things were changed: 1. Semantic changes - PCI segments are now called PCI domains, to better match what they are really. It's also the name that Linux gave, and it seems that Wikipedia also uses this name. We also remove PCI::ChangeableAddress, because it was used in the past but now it's no longer being used. 2. There are no WindowedMMIOAccess or MMIOAccess classes anymore, as they made a bunch of unnecessary complexity. Instead, Windowed access is removed entirely (this was tested, but never was benchmarked), so we are left with IO access and memory access options. The memory access option is essentially mapping the PCI bus (from the chosen PCI domain), to virtual memory as-is. This means that unless needed, at any time, there is only one PCI bus being mapped, and this is changed if access to another PCI bus in the same PCI domain is needed. For now, we don't support mapping of different PCI buses from different PCI domains at the same time, because basically it's still a non-issue for most machines out there. 2. OOM-safety is increased, especially when constructing the Access object. It means that we pre-allocating any needed resources, and we try to find PCI domains (if requested to initialize memory access) after we attempt to construct the Access object, so it's possible to fail at this point "gracefully". 3. All PCI API functions are now separated into a different header file, which means only "clients" of the PCI subsystem API will need to include that header file. 4. Functional changes - we only allow now to enumerate the bus after a hardware scan. This means that the old method "enumerate_hardware" is removed, so, when initializing an Access object, the initializing function must call rescan on it to force it to find devices. This makes it possible to fail rescan, and also to defer it after construction from both OOM-safety terms and hotplug capabilities.
2021-09-07 12:08:38 +03:00
return PCIAccessLevel::MemoryAddressing;
if (value == "off"sv)
return PCIAccessLevel::IOAddressing;
PANIC("Unknown PCI ECAM setting: {}", value);
}
UNMAP_AFTER_INIT bool CommandLine::is_legacy_time_enabled() const
{
return lookup("time"sv).value_or("modern"sv) == "legacy"sv;
}
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("/dev/hda"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
{
const auto ahci_reset_mode = lookup("ahci_reset_mode"sv).value_or("controllers"sv);
if (ahci_reset_mode == "controllers"sv) {
return AHCIResetMode::ControllerOnly;
} else if (ahci_reset_mode == "aggressive"sv) {
return AHCIResetMode::Aggressive;
}
PANIC("Unknown AHCIResetMode: {}", ahci_reset_mode);
}
BootMode CommandLine::boot_mode(Validate should_validate) const
{
const auto boot_mode = lookup("boot_mode"sv).value_or("graphical"sv);
if (boot_mode == "no-fbdev"sv) {
return BootMode::NoFramebufferDevices;
} else if (boot_mode == "self-test"sv) {
return BootMode::SelfTest;
} else if (boot_mode == "graphical"sv) {
return BootMode::Graphical;
}
if (should_validate == Validate::Yes)
PANIC("Unknown BootMode: {}", boot_mode);
return BootMode::Unknown;
}
UNMAP_AFTER_INIT bool CommandLine::is_no_framebuffer_devices_mode() const
{
const auto mode = boot_mode();
return mode == BootMode::NoFramebufferDevices || mode == BootMode::SelfTest;
}
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())
args.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
{
const auto 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);
}
}