CommandLine.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/StringBuilder.h>
  7. #include <Kernel/CommandLine.h>
  8. #include <Kernel/Panic.h>
  9. #include <Kernel/Sections.h>
  10. #include <Kernel/StdLib.h>
  11. namespace Kernel {
  12. static char s_cmd_line[1024];
  13. static constexpr StringView s_embedded_cmd_line = "";
  14. static CommandLine* s_the;
  15. UNMAP_AFTER_INIT void CommandLine::early_initialize(const char* cmd_line)
  16. {
  17. if (!cmd_line)
  18. return;
  19. size_t length = strlen(cmd_line);
  20. if (length >= sizeof(s_cmd_line))
  21. length = sizeof(s_cmd_line) - 1;
  22. memcpy(s_cmd_line, cmd_line, length);
  23. s_cmd_line[length] = '\0';
  24. }
  25. const CommandLine& kernel_command_line()
  26. {
  27. VERIFY(s_the);
  28. return *s_the;
  29. }
  30. UNMAP_AFTER_INIT void CommandLine::initialize()
  31. {
  32. VERIFY(!s_the);
  33. s_the = new CommandLine(s_cmd_line);
  34. dmesgln("Kernel Commandline: {}", kernel_command_line().string());
  35. // Validate the boot mode the user passed in.
  36. (void)s_the->boot_mode(Validate::Yes);
  37. }
  38. UNMAP_AFTER_INIT void CommandLine::build_commandline(const String& cmdline_from_bootloader)
  39. {
  40. StringBuilder builder;
  41. builder.append(cmdline_from_bootloader);
  42. if (!s_embedded_cmd_line.is_empty()) {
  43. builder.append(" ");
  44. builder.append(s_embedded_cmd_line);
  45. }
  46. m_string = builder.to_string();
  47. }
  48. UNMAP_AFTER_INIT void CommandLine::add_arguments(const Vector<StringView>& args)
  49. {
  50. for (auto&& str : args) {
  51. if (str == ""sv) {
  52. continue;
  53. }
  54. auto pair = str.split_view('=', false);
  55. VERIFY(pair.size() == 2 || pair.size() == 1);
  56. if (pair.size() == 1) {
  57. m_params.set(move(pair[0]), ""sv);
  58. } else {
  59. m_params.set(move(pair[0]), move(pair[1]));
  60. }
  61. }
  62. }
  63. UNMAP_AFTER_INIT CommandLine::CommandLine(const String& cmdline_from_bootloader)
  64. {
  65. s_the = this;
  66. build_commandline(cmdline_from_bootloader);
  67. const auto& args = m_string.split_view(' ');
  68. m_params.ensure_capacity(args.size());
  69. add_arguments(args);
  70. }
  71. Optional<StringView> CommandLine::lookup(const StringView& key) const
  72. {
  73. return m_params.get(key);
  74. }
  75. bool CommandLine::contains(const StringView& key) const
  76. {
  77. return m_params.contains(key);
  78. }
  79. UNMAP_AFTER_INIT bool CommandLine::is_boot_profiling_enabled() const
  80. {
  81. return contains("boot_prof"sv);
  82. }
  83. UNMAP_AFTER_INIT bool CommandLine::is_ide_enabled() const
  84. {
  85. return !contains("disable_ide"sv);
  86. }
  87. UNMAP_AFTER_INIT bool CommandLine::is_smp_enabled() const
  88. {
  89. return lookup("smp"sv).value_or("off"sv) == "on"sv;
  90. }
  91. UNMAP_AFTER_INIT bool CommandLine::is_vmmouse_enabled() const
  92. {
  93. return lookup("vmmouse"sv).value_or("on") == "on"sv;
  94. }
  95. UNMAP_AFTER_INIT PCIAccessLevel CommandLine::pci_access_level() const
  96. {
  97. auto value = lookup("pci_ecam"sv).value_or("off"sv);
  98. if (value == "on"sv)
  99. return PCIAccessLevel::MemoryAddressing;
  100. if (value == "off"sv)
  101. return PCIAccessLevel::IOAddressing;
  102. PANIC("Unknown PCI ECAM setting: {}", value);
  103. }
  104. UNMAP_AFTER_INIT bool CommandLine::is_legacy_time_enabled() const
  105. {
  106. return lookup("time"sv).value_or("modern"sv) == "legacy"sv;
  107. }
  108. UNMAP_AFTER_INIT bool CommandLine::is_force_pio() const
  109. {
  110. return contains("force_pio"sv);
  111. }
  112. UNMAP_AFTER_INIT StringView CommandLine::root_device() const
  113. {
  114. return lookup("root"sv).value_or("/dev/hda"sv);
  115. }
  116. UNMAP_AFTER_INIT AcpiFeatureLevel CommandLine::acpi_feature_level() const
  117. {
  118. auto value = kernel_command_line().lookup("acpi"sv).value_or("on"sv);
  119. if (value == "limited"sv)
  120. return AcpiFeatureLevel::Limited;
  121. if (value == "off"sv)
  122. return AcpiFeatureLevel::Disabled;
  123. return AcpiFeatureLevel::Enabled;
  124. }
  125. UNMAP_AFTER_INIT HPETMode CommandLine::hpet_mode() const
  126. {
  127. auto hpet_mode = lookup("hpet"sv).value_or("periodic"sv);
  128. if (hpet_mode == "periodic"sv)
  129. return HPETMode::Periodic;
  130. if (hpet_mode == "nonperiodic"sv)
  131. return HPETMode::NonPeriodic;
  132. PANIC("Unknown HPETMode: {}", hpet_mode);
  133. }
  134. UNMAP_AFTER_INIT bool CommandLine::is_physical_networking_disabled() const
  135. {
  136. return contains("disable_physical_networking"sv);
  137. }
  138. UNMAP_AFTER_INIT bool CommandLine::disable_ps2_controller() const
  139. {
  140. return contains("disable_ps2_controller"sv);
  141. }
  142. UNMAP_AFTER_INIT bool CommandLine::disable_physical_storage() const
  143. {
  144. return contains("disable_physical_storage"sv);
  145. }
  146. UNMAP_AFTER_INIT bool CommandLine::disable_uhci_controller() const
  147. {
  148. return contains("disable_uhci_controller"sv);
  149. }
  150. UNMAP_AFTER_INIT bool CommandLine::disable_usb() const
  151. {
  152. return contains("disable_usb"sv);
  153. }
  154. UNMAP_AFTER_INIT bool CommandLine::disable_virtio() const
  155. {
  156. return contains("disable_virtio"sv);
  157. }
  158. UNMAP_AFTER_INIT AHCIResetMode CommandLine::ahci_reset_mode() const
  159. {
  160. const auto ahci_reset_mode = lookup("ahci_reset_mode"sv).value_or("controllers"sv);
  161. if (ahci_reset_mode == "controllers"sv) {
  162. return AHCIResetMode::ControllerOnly;
  163. } else if (ahci_reset_mode == "aggressive"sv) {
  164. return AHCIResetMode::Aggressive;
  165. }
  166. PANIC("Unknown AHCIResetMode: {}", ahci_reset_mode);
  167. }
  168. BootMode CommandLine::boot_mode(Validate should_validate) const
  169. {
  170. const auto boot_mode = lookup("boot_mode"sv).value_or("graphical"sv);
  171. if (boot_mode == "no-fbdev"sv) {
  172. return BootMode::NoFramebufferDevices;
  173. } else if (boot_mode == "self-test"sv) {
  174. return BootMode::SelfTest;
  175. } else if (boot_mode == "graphical"sv) {
  176. return BootMode::Graphical;
  177. }
  178. if (should_validate == Validate::Yes)
  179. PANIC("Unknown BootMode: {}", boot_mode);
  180. return BootMode::Unknown;
  181. }
  182. UNMAP_AFTER_INIT bool CommandLine::is_no_framebuffer_devices_mode() const
  183. {
  184. const auto mode = boot_mode();
  185. return mode == BootMode::NoFramebufferDevices || mode == BootMode::SelfTest;
  186. }
  187. StringView CommandLine::userspace_init() const
  188. {
  189. return lookup("init"sv).value_or("/bin/SystemServer"sv);
  190. }
  191. Vector<String> CommandLine::userspace_init_args() const
  192. {
  193. auto init_args = lookup("init_args"sv).value_or(""sv).to_string().split(';');
  194. if (!init_args.is_empty())
  195. init_args.prepend(userspace_init());
  196. return init_args;
  197. }
  198. UNMAP_AFTER_INIT size_t CommandLine::switch_to_tty() const
  199. {
  200. const auto default_tty = lookup("switch_to_tty"sv).value_or("1"sv);
  201. auto switch_tty_number = default_tty.to_uint();
  202. if (switch_tty_number.has_value() && switch_tty_number.value() >= 1) {
  203. return switch_tty_number.value() - 1;
  204. }
  205. PANIC("Invalid default tty value: {}", default_tty);
  206. }
  207. }