CommandLine.cpp 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  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(char const* 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. bool CommandLine::was_initialized()
  26. {
  27. return s_the != nullptr;
  28. }
  29. CommandLine const& kernel_command_line()
  30. {
  31. VERIFY(s_the);
  32. return *s_the;
  33. }
  34. UNMAP_AFTER_INIT void CommandLine::initialize()
  35. {
  36. VERIFY(!s_the);
  37. s_the = new CommandLine({ s_cmd_line, strlen(s_cmd_line) });
  38. dmesgln("Kernel Commandline: {}", kernel_command_line().string());
  39. // Validate the modes the user passed in.
  40. (void)s_the->panic_mode(Validate::Yes);
  41. if (s_the->contains("boot_mode"sv)) {
  42. // I know, we don't do legacy, but even though I eliminated 'boot_mode' from the codebase, there
  43. // is a good chance that someone's still using it. Let's be nice and tell them where to look.
  44. // TODO: Remove this in 2022.
  45. PANIC("'boot_mode' is now split into panic=[halt|shutdown], fbdev=[on|off], and system_mode=[graphical|text|selftest].");
  46. }
  47. }
  48. UNMAP_AFTER_INIT NonnullOwnPtr<KString> CommandLine::build_commandline(StringView cmdline_from_bootloader)
  49. {
  50. StringBuilder builder;
  51. builder.append(cmdline_from_bootloader);
  52. if constexpr (!s_embedded_cmd_line.is_empty()) {
  53. builder.append(" ");
  54. builder.append(s_embedded_cmd_line);
  55. }
  56. return KString::must_create(builder.string_view());
  57. }
  58. UNMAP_AFTER_INIT void CommandLine::add_arguments(Vector<StringView> const& args)
  59. {
  60. for (auto&& str : args) {
  61. if (str == ""sv) {
  62. continue;
  63. }
  64. auto pair = str.split_view('=', false);
  65. VERIFY(pair.size() == 2 || pair.size() == 1);
  66. if (pair.size() == 1) {
  67. m_params.set(pair[0], ""sv);
  68. } else {
  69. m_params.set(pair[0], pair[1]);
  70. }
  71. }
  72. }
  73. UNMAP_AFTER_INIT CommandLine::CommandLine(StringView cmdline_from_bootloader)
  74. : m_string(build_commandline(cmdline_from_bootloader))
  75. {
  76. s_the = this;
  77. auto const& args = m_string->view().split_view(' ');
  78. m_params.ensure_capacity(args.size());
  79. add_arguments(args);
  80. }
  81. Optional<StringView> CommandLine::lookup(StringView key) const
  82. {
  83. return m_params.get(key);
  84. }
  85. bool CommandLine::contains(StringView key) const
  86. {
  87. return m_params.contains(key);
  88. }
  89. UNMAP_AFTER_INIT bool CommandLine::is_boot_profiling_enabled() const
  90. {
  91. return contains("boot_prof"sv);
  92. }
  93. UNMAP_AFTER_INIT bool CommandLine::is_ide_enabled() const
  94. {
  95. return !contains("disable_ide"sv);
  96. }
  97. UNMAP_AFTER_INIT bool CommandLine::is_smp_enabled() const
  98. {
  99. // Note: We can't enable SMP mode without enabling the IOAPIC.
  100. if (!is_ioapic_enabled())
  101. return false;
  102. return lookup("smp"sv).value_or("off"sv) == "on"sv;
  103. }
  104. UNMAP_AFTER_INIT bool CommandLine::is_smp_enabled_without_ioapic_enabled() const
  105. {
  106. auto smp_enabled = lookup("smp"sv).value_or("off"sv) == "on"sv;
  107. return smp_enabled && !is_ioapic_enabled();
  108. }
  109. UNMAP_AFTER_INIT bool CommandLine::is_ioapic_enabled() const
  110. {
  111. auto value = lookup("enable_ioapic"sv).value_or("on"sv);
  112. if (value == "on"sv)
  113. return true;
  114. if (value == "off"sv)
  115. return false;
  116. PANIC("Unknown enable_ioapic setting: {}", value);
  117. }
  118. UNMAP_AFTER_INIT bool CommandLine::is_early_boot_console_disabled() const
  119. {
  120. auto value = lookup("early_boot_console"sv).value_or("on"sv);
  121. if (value == "on"sv)
  122. return false;
  123. if (value == "off"sv)
  124. return true;
  125. PANIC("Unknown early_boot_console setting: {}", value);
  126. }
  127. UNMAP_AFTER_INIT bool CommandLine::is_vmmouse_enabled() const
  128. {
  129. return lookup("vmmouse"sv).value_or("on"sv) == "on"sv;
  130. }
  131. UNMAP_AFTER_INIT PCIAccessLevel CommandLine::pci_access_level() const
  132. {
  133. auto value = lookup("pci"sv).value_or("ecam"sv);
  134. if (value == "ecam"sv)
  135. return PCIAccessLevel::MemoryAddressing;
  136. if (value == "io"sv)
  137. return PCIAccessLevel::IOAddressing;
  138. if (value == "none"sv)
  139. return PCIAccessLevel::None;
  140. PANIC("Unknown PCI ECAM setting: {}", value);
  141. }
  142. UNMAP_AFTER_INIT bool CommandLine::is_pci_disabled() const
  143. {
  144. return lookup("pci"sv).value_or("ecam"sv) == "none"sv;
  145. }
  146. UNMAP_AFTER_INIT bool CommandLine::is_legacy_time_enabled() const
  147. {
  148. return lookup("time"sv).value_or("modern"sv) == "legacy"sv;
  149. }
  150. bool CommandLine::is_pc_speaker_enabled() const
  151. {
  152. auto value = lookup("pcspeaker"sv).value_or("off"sv);
  153. if (value == "on"sv)
  154. return true;
  155. if (value == "off"sv)
  156. return false;
  157. PANIC("Unknown pcspeaker setting: {}", value);
  158. }
  159. UNMAP_AFTER_INIT bool CommandLine::is_force_pio() const
  160. {
  161. return contains("force_pio"sv);
  162. }
  163. UNMAP_AFTER_INIT StringView CommandLine::root_device() const
  164. {
  165. return lookup("root"sv).value_or("/dev/hda"sv);
  166. }
  167. bool CommandLine::is_nvme_polling_enabled() const
  168. {
  169. return contains("nvme_poll"sv);
  170. }
  171. UNMAP_AFTER_INIT AcpiFeatureLevel CommandLine::acpi_feature_level() const
  172. {
  173. auto value = kernel_command_line().lookup("acpi"sv).value_or("limited"sv);
  174. if (value == "limited"sv)
  175. return AcpiFeatureLevel::Limited;
  176. if (value == "off"sv)
  177. return AcpiFeatureLevel::Disabled;
  178. if (value == "on"sv)
  179. return AcpiFeatureLevel::Enabled;
  180. PANIC("Unknown ACPI feature level: {}", value);
  181. }
  182. UNMAP_AFTER_INIT HPETMode CommandLine::hpet_mode() const
  183. {
  184. auto hpet_mode = lookup("hpet"sv).value_or("periodic"sv);
  185. if (hpet_mode == "periodic"sv)
  186. return HPETMode::Periodic;
  187. if (hpet_mode == "nonperiodic"sv)
  188. return HPETMode::NonPeriodic;
  189. PANIC("Unknown HPETMode: {}", hpet_mode);
  190. }
  191. UNMAP_AFTER_INIT bool CommandLine::is_physical_networking_disabled() const
  192. {
  193. return contains("disable_physical_networking"sv);
  194. }
  195. UNMAP_AFTER_INIT bool CommandLine::disable_ps2_controller() const
  196. {
  197. return contains("disable_ps2_controller"sv);
  198. }
  199. UNMAP_AFTER_INIT bool CommandLine::disable_physical_storage() const
  200. {
  201. return contains("disable_physical_storage"sv);
  202. }
  203. UNMAP_AFTER_INIT bool CommandLine::disable_uhci_controller() const
  204. {
  205. return contains("disable_uhci_controller"sv);
  206. }
  207. UNMAP_AFTER_INIT bool CommandLine::disable_usb() const
  208. {
  209. return contains("disable_usb"sv);
  210. }
  211. UNMAP_AFTER_INIT bool CommandLine::disable_virtio() const
  212. {
  213. return contains("disable_virtio"sv);
  214. }
  215. UNMAP_AFTER_INIT AHCIResetMode CommandLine::ahci_reset_mode() const
  216. {
  217. auto const ahci_reset_mode = lookup("ahci_reset_mode"sv).value_or("controllers"sv);
  218. if (ahci_reset_mode == "controllers"sv) {
  219. return AHCIResetMode::ControllerOnly;
  220. }
  221. if (ahci_reset_mode == "aggressive"sv) {
  222. return AHCIResetMode::Aggressive;
  223. }
  224. PANIC("Unknown AHCIResetMode: {}", ahci_reset_mode);
  225. }
  226. StringView CommandLine::system_mode() const
  227. {
  228. return lookup("system_mode"sv).value_or("graphical"sv);
  229. }
  230. PanicMode CommandLine::panic_mode(Validate should_validate) const
  231. {
  232. auto const panic_mode = lookup("panic"sv).value_or("halt"sv);
  233. if (panic_mode == "halt"sv) {
  234. return PanicMode::Halt;
  235. }
  236. if (panic_mode == "shutdown"sv) {
  237. return PanicMode::Shutdown;
  238. }
  239. if (should_validate == Validate::Yes)
  240. PANIC("Unknown PanicMode: {}", panic_mode);
  241. return PanicMode::Halt;
  242. }
  243. UNMAP_AFTER_INIT CommandLine::GraphicsSubsystemMode CommandLine::graphics_subsystem_mode() const
  244. {
  245. auto const graphics_subsystem_mode_value = lookup("graphics_subsystem_mode"sv).value_or("on"sv);
  246. if (graphics_subsystem_mode_value == "on"sv)
  247. return GraphicsSubsystemMode::Enabled;
  248. if (graphics_subsystem_mode_value == "limited"sv)
  249. return GraphicsSubsystemMode::Limited;
  250. if (graphics_subsystem_mode_value == "off"sv)
  251. return GraphicsSubsystemMode::Disabled;
  252. PANIC("Invalid graphics_subsystem_mode value: {}", graphics_subsystem_mode_value);
  253. }
  254. StringView CommandLine::userspace_init() const
  255. {
  256. return lookup("init"sv).value_or("/bin/SystemServer"sv);
  257. }
  258. NonnullOwnPtrVector<KString> CommandLine::userspace_init_args() const
  259. {
  260. NonnullOwnPtrVector<KString> args;
  261. auto init_args = lookup("init_args"sv).value_or(""sv).split_view(';');
  262. if (!init_args.is_empty())
  263. MUST(args.try_prepend(KString::must_create(userspace_init())));
  264. for (auto& init_arg : init_args)
  265. args.append(KString::must_create(init_arg));
  266. return args;
  267. }
  268. UNMAP_AFTER_INIT size_t CommandLine::switch_to_tty() const
  269. {
  270. auto const default_tty = lookup("switch_to_tty"sv).value_or("1"sv);
  271. auto switch_tty_number = default_tty.to_uint();
  272. if (switch_tty_number.has_value() && switch_tty_number.value() >= 1) {
  273. return switch_tty_number.value() - 1;
  274. }
  275. PANIC("Invalid default tty value: {}", default_tty);
  276. }
  277. }