2020-01-18 08:38:21 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
|
|
|
*
|
2021-04-22 08:24:48 +00:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 08:38:21 +00:00
|
|
|
*/
|
|
|
|
|
2021-05-21 17:42:33 +00:00
|
|
|
#include <AK/StringBuilder.h>
|
2020-04-08 11:54:44 +00:00
|
|
|
#include <Kernel/CommandLine.h>
|
2021-03-03 08:51:55 +00:00
|
|
|
#include <Kernel/Panic.h>
|
2021-06-22 15:40:16 +00:00
|
|
|
#include <Kernel/Sections.h>
|
2020-08-25 01:35:27 +00:00
|
|
|
#include <Kernel/StdLib.h>
|
2019-06-04 10:54:27 +00:00
|
|
|
|
2020-04-08 11:54:44 +00:00
|
|
|
namespace Kernel {
|
2019-06-04 10:54:27 +00:00
|
|
|
|
2020-08-25 01:35:27 +00:00
|
|
|
static char s_cmd_line[1024];
|
2021-05-21 17:42:33 +00:00
|
|
|
static constexpr StringView s_embedded_cmd_line = "";
|
2020-04-08 11:54:44 +00:00
|
|
|
static CommandLine* s_the;
|
|
|
|
|
2021-02-19 20:29:46 +00:00
|
|
|
UNMAP_AFTER_INIT void CommandLine::early_initialize(const char* cmd_line)
|
2020-08-25 01:35:27 +00:00
|
|
|
{
|
|
|
|
if (!cmd_line)
|
|
|
|
return;
|
|
|
|
size_t length = strlen(cmd_line);
|
|
|
|
if (length >= sizeof(s_cmd_line))
|
2020-09-18 07:49:51 +00:00
|
|
|
length = sizeof(s_cmd_line) - 1;
|
2020-08-25 01:35:27 +00:00
|
|
|
memcpy(s_cmd_line, cmd_line, length);
|
|
|
|
s_cmd_line[length] = '\0';
|
|
|
|
}
|
|
|
|
|
2020-04-08 11:54:44 +00:00
|
|
|
const CommandLine& kernel_command_line()
|
2019-06-04 10:54:27 +00:00
|
|
|
{
|
2021-02-23 19:42:32 +00:00
|
|
|
VERIFY(s_the);
|
2019-06-04 10:54:27 +00:00
|
|
|
return *s_the;
|
|
|
|
}
|
|
|
|
|
2021-02-19 20:29:46 +00:00
|
|
|
UNMAP_AFTER_INIT void CommandLine::initialize()
|
2020-04-08 11:54:44 +00:00
|
|
|
{
|
2021-02-23 19:42:32 +00:00
|
|
|
VERIFY(!s_the);
|
2020-08-25 01:35:27 +00:00
|
|
|
s_the = new CommandLine(s_cmd_line);
|
2021-05-21 17:12:29 +00:00
|
|
|
dmesgln("Kernel Commandline: {}", kernel_command_line().string());
|
2020-04-08 11:54:44 +00:00
|
|
|
}
|
|
|
|
|
2021-05-21 17:42:33 +00:00
|
|
|
UNMAP_AFTER_INIT void CommandLine::build_commandline(const String& cmdline_from_bootloader)
|
2019-06-04 10:54:27 +00:00
|
|
|
{
|
2021-05-21 17:42:33 +00:00
|
|
|
StringBuilder builder;
|
|
|
|
builder.append(cmdline_from_bootloader);
|
|
|
|
if (!s_embedded_cmd_line.is_empty()) {
|
|
|
|
builder.append(" ");
|
|
|
|
builder.append(s_embedded_cmd_line);
|
|
|
|
}
|
|
|
|
m_string = builder.to_string();
|
|
|
|
}
|
2020-08-22 14:34:14 +00:00
|
|
|
|
2021-05-31 08:59:02 +00:00
|
|
|
UNMAP_AFTER_INIT void CommandLine::add_arguments(const Vector<StringView>& args)
|
2021-05-21 17:42:33 +00:00
|
|
|
{
|
2020-08-23 13:04:49 +00:00
|
|
|
for (auto&& str : args) {
|
2021-05-31 08:59:02 +00:00
|
|
|
if (str == ""sv) {
|
2019-06-04 11:55:52 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2021-05-31 08:59:02 +00:00
|
|
|
auto pair = str.split_view('=', false);
|
|
|
|
VERIFY(pair.size() == 2 || pair.size() == 1);
|
2019-06-04 10:54:27 +00:00
|
|
|
|
|
|
|
if (pair.size() == 1) {
|
2021-05-31 08:59:02 +00:00
|
|
|
m_params.set(move(pair[0]), ""sv);
|
2019-06-04 10:54:27 +00:00
|
|
|
} else {
|
2020-08-23 13:04:49 +00:00
|
|
|
m_params.set(move(pair[0]), move(pair[1]));
|
2019-06-04 10:54:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-21 17:42:33 +00:00
|
|
|
UNMAP_AFTER_INIT CommandLine::CommandLine(const String& cmdline_from_bootloader)
|
|
|
|
{
|
|
|
|
s_the = this;
|
|
|
|
build_commandline(cmdline_from_bootloader);
|
2021-05-31 08:59:02 +00:00
|
|
|
const auto& args = m_string.split_view(' ');
|
2021-05-21 17:42:33 +00:00
|
|
|
m_params.ensure_capacity(args.size());
|
|
|
|
add_arguments(args);
|
|
|
|
}
|
|
|
|
|
2021-05-31 08:59:02 +00:00
|
|
|
Optional<String> CommandLine::lookup(const StringView& key) const
|
2020-04-08 14:35:00 +00:00
|
|
|
{
|
|
|
|
return m_params.get(key);
|
|
|
|
}
|
|
|
|
|
2021-05-31 08:59:02 +00:00
|
|
|
bool CommandLine::contains(const StringView& key) const
|
2019-06-04 10:54:27 +00:00
|
|
|
{
|
|
|
|
return m_params.contains(key);
|
|
|
|
}
|
2020-04-08 11:54:44 +00:00
|
|
|
|
2021-03-03 09:00:41 +00:00
|
|
|
UNMAP_AFTER_INIT bool CommandLine::is_boot_profiling_enabled() const
|
|
|
|
{
|
2021-05-31 08:59:02 +00:00
|
|
|
return contains("boot_prof"sv);
|
2021-03-03 09:00:41 +00:00
|
|
|
}
|
|
|
|
|
2021-03-03 08:51:55 +00:00
|
|
|
UNMAP_AFTER_INIT bool CommandLine::is_ide_enabled() const
|
|
|
|
{
|
2021-05-31 08:59:02 +00:00
|
|
|
return !contains("disable_ide"sv);
|
2021-03-03 08:51:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
UNMAP_AFTER_INIT bool CommandLine::is_smp_enabled() const
|
|
|
|
{
|
2021-05-31 08:59:02 +00:00
|
|
|
return lookup("smp"sv).value_or("off"sv) == "on"sv;
|
2021-03-03 08:51:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
UNMAP_AFTER_INIT bool CommandLine::is_vmmouse_enabled() const
|
|
|
|
{
|
2021-05-31 08:59:02 +00:00
|
|
|
return lookup("vmmouse"sv).value_or("on") == "on"sv;
|
2021-03-03 08:51:55 +00:00
|
|
|
}
|
|
|
|
|
2021-04-03 13:46:04 +00:00
|
|
|
UNMAP_AFTER_INIT PCIAccessLevel CommandLine::pci_access_level() const
|
2021-03-03 08:51:55 +00:00
|
|
|
{
|
2021-05-31 08:59:02 +00:00
|
|
|
auto value = lookup("pci_ecam"sv).value_or("off"sv);
|
|
|
|
if (value == "on"sv)
|
2021-04-03 13:46:04 +00:00
|
|
|
return PCIAccessLevel::MappingPerBus;
|
2021-05-31 08:59:02 +00:00
|
|
|
if (value == "per-device"sv)
|
2021-04-03 13:46:04 +00:00
|
|
|
return PCIAccessLevel::MappingPerDevice;
|
2021-05-31 08:59:02 +00:00
|
|
|
if (value == "off"sv)
|
2021-04-03 13:46:04 +00:00
|
|
|
return PCIAccessLevel::IOAddressing;
|
2021-04-03 10:29:07 +00:00
|
|
|
PANIC("Unknown PCI ECAM setting: {}", value);
|
2021-03-03 08:51:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
UNMAP_AFTER_INIT bool CommandLine::is_legacy_time_enabled() const
|
|
|
|
{
|
2021-05-31 08:59:02 +00:00
|
|
|
return lookup("time"sv).value_or("modern"sv) == "legacy"sv;
|
2021-03-03 08:51:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
UNMAP_AFTER_INIT bool CommandLine::is_force_pio() const
|
|
|
|
{
|
2021-05-31 08:59:02 +00:00
|
|
|
return contains("force_pio"sv);
|
2021-03-03 08:51:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
UNMAP_AFTER_INIT String CommandLine::root_device() const
|
|
|
|
{
|
2021-05-31 08:59:02 +00:00
|
|
|
return lookup("root"sv).value_or("/dev/hda"sv);
|
2021-03-03 08:51:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
UNMAP_AFTER_INIT AcpiFeatureLevel CommandLine::acpi_feature_level() const
|
|
|
|
{
|
2021-05-31 08:59:02 +00:00
|
|
|
auto value = kernel_command_line().lookup("acpi"sv).value_or("on"sv);
|
|
|
|
if (value == "limited"sv)
|
2021-03-03 08:51:55 +00:00
|
|
|
return AcpiFeatureLevel::Limited;
|
2021-05-31 08:59:02 +00:00
|
|
|
if (value == "off"sv)
|
2021-03-03 08:51:55 +00:00
|
|
|
return AcpiFeatureLevel::Disabled;
|
|
|
|
return AcpiFeatureLevel::Enabled;
|
|
|
|
}
|
|
|
|
|
|
|
|
UNMAP_AFTER_INIT HPETMode CommandLine::hpet_mode() const
|
|
|
|
{
|
2021-05-31 08:59:02 +00:00
|
|
|
auto hpet_mode = lookup("hpet"sv).value_or("periodic"sv);
|
|
|
|
if (hpet_mode == "periodic"sv)
|
2021-03-03 08:51:55 +00:00
|
|
|
return HPETMode::Periodic;
|
2021-05-31 08:59:02 +00:00
|
|
|
if (hpet_mode == "nonperiodic"sv)
|
2021-03-03 08:51:55 +00:00
|
|
|
return HPETMode::NonPeriodic;
|
|
|
|
PANIC("Unknown HPETMode: {}", hpet_mode);
|
|
|
|
}
|
|
|
|
|
2021-06-04 05:02:14 +00:00
|
|
|
UNMAP_AFTER_INIT bool CommandLine::is_physical_networking_disabled() const
|
|
|
|
{
|
|
|
|
return contains("disable_physical_networking"sv);
|
|
|
|
}
|
|
|
|
|
2021-04-08 18:18:48 +00:00
|
|
|
UNMAP_AFTER_INIT bool CommandLine::disable_ps2_controller() const
|
|
|
|
{
|
2021-05-31 08:59:02 +00:00
|
|
|
return contains("disable_ps2_controller"sv);
|
2021-04-08 18:18:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
UNMAP_AFTER_INIT bool CommandLine::disable_physical_storage() const
|
|
|
|
{
|
2021-05-31 08:59:02 +00:00
|
|
|
return contains("disable_physical_storage"sv);
|
2021-04-08 18:18:48 +00:00
|
|
|
}
|
|
|
|
|
2021-04-17 19:36:53 +00:00
|
|
|
UNMAP_AFTER_INIT bool CommandLine::disable_uhci_controller() const
|
|
|
|
{
|
2021-05-31 08:59:02 +00:00
|
|
|
return contains("disable_uhci_controller"sv);
|
2021-04-17 19:36:53 +00:00
|
|
|
}
|
|
|
|
|
2021-04-18 13:06:35 +00:00
|
|
|
UNMAP_AFTER_INIT bool CommandLine::disable_virtio() const
|
|
|
|
{
|
2021-05-31 08:59:02 +00:00
|
|
|
return contains("disable_virtio"sv);
|
2021-04-18 13:06:35 +00:00
|
|
|
}
|
|
|
|
|
2021-03-12 16:35:16 +00:00
|
|
|
UNMAP_AFTER_INIT AHCIResetMode CommandLine::ahci_reset_mode() const
|
|
|
|
{
|
2021-05-31 08:59:02 +00:00
|
|
|
const auto ahci_reset_mode = lookup("ahci_reset_mode"sv).value_or("controllers"sv);
|
|
|
|
if (ahci_reset_mode == "controllers"sv) {
|
2021-03-12 16:35:16 +00:00
|
|
|
return AHCIResetMode::ControllerOnly;
|
2021-05-31 08:59:02 +00:00
|
|
|
} else if (ahci_reset_mode == "aggressive"sv) {
|
2021-05-28 22:18:45 +00:00
|
|
|
return AHCIResetMode::Aggressive;
|
2021-03-12 16:35:16 +00:00
|
|
|
}
|
|
|
|
PANIC("Unknown AHCIResetMode: {}", ahci_reset_mode);
|
|
|
|
}
|
|
|
|
|
2021-03-03 08:51:55 +00:00
|
|
|
UNMAP_AFTER_INIT BootMode CommandLine::boot_mode() const
|
|
|
|
{
|
2021-05-31 08:59:02 +00:00
|
|
|
const auto boot_mode = lookup("boot_mode"sv).value_or("graphical"sv);
|
2021-06-03 15:02:56 +00:00
|
|
|
if (boot_mode == "no-fbdev"sv) {
|
2021-04-16 19:58:51 +00:00
|
|
|
return BootMode::NoFramebufferDevices;
|
2021-05-31 08:59:02 +00:00
|
|
|
} else if (boot_mode == "self-test"sv) {
|
2021-03-03 08:51:55 +00:00
|
|
|
return BootMode::SelfTest;
|
2021-05-31 08:59:02 +00:00
|
|
|
} else if (boot_mode == "graphical"sv) {
|
2021-03-03 08:51:55 +00:00
|
|
|
return BootMode::Graphical;
|
|
|
|
}
|
|
|
|
PANIC("Unknown BootMode: {}", boot_mode);
|
|
|
|
}
|
|
|
|
|
2021-04-16 19:58:51 +00:00
|
|
|
UNMAP_AFTER_INIT bool CommandLine::is_no_framebuffer_devices_mode() const
|
2021-03-03 08:51:55 +00:00
|
|
|
{
|
|
|
|
const auto mode = boot_mode();
|
2021-04-16 19:58:51 +00:00
|
|
|
return mode == BootMode::NoFramebufferDevices || mode == BootMode::SelfTest;
|
2021-03-03 08:51:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
String CommandLine::userspace_init() const
|
|
|
|
{
|
2021-05-31 08:59:02 +00:00
|
|
|
return lookup("init"sv).value_or("/bin/SystemServer"sv);
|
2021-03-03 08:51:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Vector<String> CommandLine::userspace_init_args() const
|
|
|
|
{
|
2021-05-31 08:59:02 +00:00
|
|
|
auto init_args = lookup("init_args"sv).value_or(""sv).split(',');
|
2021-03-03 08:51:55 +00:00
|
|
|
if (!init_args.is_empty())
|
|
|
|
init_args.prepend(userspace_init());
|
|
|
|
|
|
|
|
return init_args;
|
|
|
|
}
|
|
|
|
|
2021-05-13 21:22:02 +00:00
|
|
|
UNMAP_AFTER_INIT size_t CommandLine::switch_to_tty() const
|
|
|
|
{
|
2021-05-31 08:59:02 +00:00
|
|
|
const auto default_tty = lookup("switch_to_tty"sv).value_or("1"sv);
|
2021-05-13 21:22:02 +00:00
|
|
|
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);
|
|
|
|
}
|
2020-04-08 11:54:44 +00:00
|
|
|
}
|