CommandLine.cpp 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  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/Boot/CommandLine.h>
  8. #include <Kernel/Library/Panic.h>
  9. #include <Kernel/Library/StdLib.h>
  10. #include <Kernel/Sections.h>
  11. namespace Kernel {
  12. static char s_cmd_line[1024];
  13. static constexpr StringView s_embedded_cmd_line = ""sv;
  14. static CommandLine* s_the;
  15. UNMAP_AFTER_INIT void CommandLine::early_initialize(StringView cmd_line)
  16. {
  17. (void)cmd_line.copy_characters_to_buffer(s_cmd_line, sizeof(s_cmd_line));
  18. }
  19. bool CommandLine::was_initialized()
  20. {
  21. return s_the != nullptr;
  22. }
  23. CommandLine const& kernel_command_line()
  24. {
  25. VERIFY(s_the);
  26. return *s_the;
  27. }
  28. UNMAP_AFTER_INIT void CommandLine::initialize()
  29. {
  30. VERIFY(!s_the);
  31. s_the = new CommandLine({ s_cmd_line, strlen(s_cmd_line) });
  32. dmesgln("Kernel Commandline: {}", kernel_command_line().string());
  33. // Validate the modes the user passed in.
  34. (void)s_the->panic_mode(Validate::Yes);
  35. }
  36. UNMAP_AFTER_INIT NonnullOwnPtr<KString> CommandLine::build_commandline(StringView cmdline_from_bootloader)
  37. {
  38. StringBuilder builder;
  39. builder.append(cmdline_from_bootloader);
  40. if constexpr (!s_embedded_cmd_line.is_empty()) {
  41. builder.append(' ');
  42. builder.append(s_embedded_cmd_line);
  43. }
  44. return KString::must_create(builder.string_view());
  45. }
  46. UNMAP_AFTER_INIT void CommandLine::add_arguments(Vector<StringView> const& args)
  47. {
  48. for (auto&& str : args) {
  49. if (str == ""sv) {
  50. continue;
  51. }
  52. // Some boot loaders may include complex key-value pairs where the value is a composite entry,
  53. // we handle this by only checking for the first equals sign in each command line parameter.
  54. auto key = str.find_first_split_view('=');
  55. if (key.length() == str.length())
  56. m_params.set(key, ""sv);
  57. else
  58. m_params.set(key, str.substring_view(key.length() + 1));
  59. }
  60. }
  61. UNMAP_AFTER_INIT CommandLine::CommandLine(StringView cmdline_from_bootloader)
  62. : m_string(build_commandline(cmdline_from_bootloader))
  63. {
  64. s_the = this;
  65. auto const& args = m_string->view().split_view(' ');
  66. MUST(m_params.try_ensure_capacity(args.size()));
  67. add_arguments(args);
  68. }
  69. Optional<StringView> CommandLine::lookup(StringView key) const
  70. {
  71. return m_params.get(key);
  72. }
  73. bool CommandLine::contains(StringView key) const
  74. {
  75. return m_params.contains(key);
  76. }
  77. UNMAP_AFTER_INIT bool CommandLine::is_boot_profiling_enabled() const
  78. {
  79. return contains("boot_prof"sv);
  80. }
  81. UNMAP_AFTER_INIT bool CommandLine::is_ide_enabled() const
  82. {
  83. return !contains("disable_ide"sv);
  84. }
  85. UNMAP_AFTER_INIT bool CommandLine::is_smp_enabled() const
  86. {
  87. // Note: We can't enable SMP mode without enabling the IOAPIC.
  88. if (!is_ioapic_enabled())
  89. return false;
  90. return lookup("smp"sv).value_or("off"sv) == "on"sv;
  91. }
  92. UNMAP_AFTER_INIT bool CommandLine::is_smp_enabled_without_ioapic_enabled() const
  93. {
  94. auto smp_enabled = lookup("smp"sv).value_or("off"sv) == "on"sv;
  95. return smp_enabled && !is_ioapic_enabled();
  96. }
  97. UNMAP_AFTER_INIT bool CommandLine::is_ioapic_enabled() const
  98. {
  99. auto value = lookup("enable_ioapic"sv).value_or("on"sv);
  100. if (value == "on"sv)
  101. return true;
  102. if (value == "off"sv)
  103. return false;
  104. PANIC("Unknown enable_ioapic setting: {}", value);
  105. }
  106. UNMAP_AFTER_INIT bool CommandLine::is_early_boot_console_disabled() const
  107. {
  108. auto value = lookup("early_boot_console"sv).value_or("on"sv);
  109. if (value == "on"sv)
  110. return false;
  111. if (value == "off"sv)
  112. return true;
  113. PANIC("Unknown early_boot_console setting: {}", value);
  114. }
  115. UNMAP_AFTER_INIT I8042PresenceMode CommandLine::i8042_presence_mode() const
  116. {
  117. auto value = lookup("i8042_presence_mode"sv).value_or("auto"sv);
  118. if (value == "auto"sv)
  119. return I8042PresenceMode::Automatic;
  120. if (value == "none"sv)
  121. return I8042PresenceMode::None;
  122. if (value == "force"sv)
  123. return I8042PresenceMode::Force;
  124. if (value == "aggressive-test"sv)
  125. return I8042PresenceMode::AggressiveTest;
  126. PANIC("Unknown i8042_presence_mode setting: {}", value);
  127. }
  128. UNMAP_AFTER_INIT bool CommandLine::is_vmmouse_enabled() const
  129. {
  130. return lookup("vmmouse"sv).value_or("on"sv) == "on"sv;
  131. }
  132. UNMAP_AFTER_INIT PCIAccessLevel CommandLine::pci_access_level() const
  133. {
  134. auto value = lookup("pci"sv).value_or("ecam"sv);
  135. if (value == "ecam"sv)
  136. return PCIAccessLevel::MemoryAddressing;
  137. #if ARCH(X86_64)
  138. if (value == "io"sv)
  139. return PCIAccessLevel::IOAddressing;
  140. #endif
  141. if (value == "none"sv)
  142. return PCIAccessLevel::None;
  143. PANIC("Unknown PCI ECAM setting: {}", value);
  144. }
  145. UNMAP_AFTER_INIT bool CommandLine::is_pci_disabled() const
  146. {
  147. return lookup("pci"sv).value_or("ecam"sv) == "none"sv;
  148. }
  149. UNMAP_AFTER_INIT bool CommandLine::is_legacy_time_enabled() const
  150. {
  151. return lookup("time"sv).value_or("modern"sv) == "legacy"sv;
  152. }
  153. bool CommandLine::is_pc_speaker_enabled() const
  154. {
  155. auto value = lookup("pcspeaker"sv).value_or("off"sv);
  156. if (value == "on"sv)
  157. return true;
  158. if (value == "off"sv)
  159. return false;
  160. PANIC("Unknown pcspeaker setting: {}", value);
  161. }
  162. UNMAP_AFTER_INIT bool CommandLine::is_force_pio() const
  163. {
  164. return contains("force_pio"sv);
  165. }
  166. UNMAP_AFTER_INIT StringView CommandLine::root_device() const
  167. {
  168. return lookup("root"sv).value_or("lun0:0:0"sv);
  169. }
  170. bool CommandLine::is_nvme_polling_enabled() const
  171. {
  172. return contains("nvme_poll"sv);
  173. }
  174. UNMAP_AFTER_INIT AcpiFeatureLevel CommandLine::acpi_feature_level() const
  175. {
  176. auto value = kernel_command_line().lookup("acpi"sv).value_or("limited"sv);
  177. if (value == "limited"sv)
  178. return AcpiFeatureLevel::Limited;
  179. if (value == "off"sv)
  180. return AcpiFeatureLevel::Disabled;
  181. if (value == "on"sv)
  182. return AcpiFeatureLevel::Enabled;
  183. PANIC("Unknown ACPI feature level: {}", value);
  184. }
  185. UNMAP_AFTER_INIT HPETMode CommandLine::hpet_mode() const
  186. {
  187. auto hpet_mode = lookup("hpet"sv).value_or("periodic"sv);
  188. if (hpet_mode == "periodic"sv)
  189. return HPETMode::Periodic;
  190. if (hpet_mode == "nonperiodic"sv)
  191. return HPETMode::NonPeriodic;
  192. PANIC("Unknown HPETMode: {}", hpet_mode);
  193. }
  194. UNMAP_AFTER_INIT bool CommandLine::is_physical_networking_disabled() const
  195. {
  196. return contains("disable_physical_networking"sv);
  197. }
  198. UNMAP_AFTER_INIT bool CommandLine::disable_ps2_mouse() const
  199. {
  200. return contains("disable_ps2_mouse"sv);
  201. }
  202. UNMAP_AFTER_INIT bool CommandLine::disable_physical_storage() const
  203. {
  204. return contains("disable_physical_storage"sv);
  205. }
  206. UNMAP_AFTER_INIT bool CommandLine::disable_uhci_controller() const
  207. {
  208. return contains("disable_uhci_controller"sv);
  209. }
  210. UNMAP_AFTER_INIT bool CommandLine::disable_usb() const
  211. {
  212. return contains("disable_usb"sv);
  213. }
  214. UNMAP_AFTER_INIT bool CommandLine::disable_virtio() const
  215. {
  216. return contains("disable_virtio"sv);
  217. }
  218. UNMAP_AFTER_INIT AHCIResetMode CommandLine::ahci_reset_mode() const
  219. {
  220. auto const ahci_reset_mode = lookup("ahci_reset_mode"sv).value_or("controllers"sv);
  221. if (ahci_reset_mode == "controllers"sv) {
  222. return AHCIResetMode::ControllerOnly;
  223. }
  224. if (ahci_reset_mode == "aggressive"sv) {
  225. return AHCIResetMode::Aggressive;
  226. }
  227. PANIC("Unknown AHCIResetMode: {}", ahci_reset_mode);
  228. }
  229. StringView CommandLine::system_mode() const
  230. {
  231. return lookup("system_mode"sv).value_or("graphical"sv);
  232. }
  233. PanicMode CommandLine::panic_mode(Validate should_validate) const
  234. {
  235. auto const panic_mode = lookup("panic"sv).value_or("halt"sv);
  236. if (panic_mode == "halt"sv) {
  237. return PanicMode::Halt;
  238. }
  239. if (panic_mode == "shutdown"sv) {
  240. return PanicMode::Shutdown;
  241. }
  242. if (should_validate == Validate::Yes)
  243. PANIC("Unknown PanicMode: {}", panic_mode);
  244. return PanicMode::Halt;
  245. }
  246. UNMAP_AFTER_INIT CommandLine::GraphicsSubsystemMode CommandLine::graphics_subsystem_mode() const
  247. {
  248. auto const graphics_subsystem_mode_value = lookup("graphics_subsystem_mode"sv).value_or("on"sv);
  249. if (graphics_subsystem_mode_value == "on"sv)
  250. return GraphicsSubsystemMode::Enabled;
  251. if (graphics_subsystem_mode_value == "limited"sv)
  252. return GraphicsSubsystemMode::Limited;
  253. if (graphics_subsystem_mode_value == "off"sv)
  254. return GraphicsSubsystemMode::Disabled;
  255. PANIC("Invalid graphics_subsystem_mode value: {}", graphics_subsystem_mode_value);
  256. }
  257. StringView CommandLine::userspace_init() const
  258. {
  259. return lookup("init"sv).value_or("/init"sv);
  260. }
  261. Vector<NonnullOwnPtr<KString>> CommandLine::userspace_init_args() const
  262. {
  263. Vector<NonnullOwnPtr<KString>> args;
  264. auto init_args = lookup("init_args"sv).value_or(""sv).split_view(';');
  265. if (!init_args.is_empty())
  266. MUST(args.try_prepend(MUST(KString::try_create(userspace_init()))));
  267. for (auto& init_arg : init_args)
  268. args.append(MUST(KString::try_create(init_arg)));
  269. return args;
  270. }
  271. UNMAP_AFTER_INIT size_t CommandLine::switch_to_tty() const
  272. {
  273. auto const default_tty = lookup("switch_to_tty"sv).value_or("1"sv);
  274. auto switch_tty_number = default_tty.to_number<unsigned>();
  275. if (switch_tty_number.has_value() && switch_tty_number.value() >= 1) {
  276. return switch_tty_number.value() - 1;
  277. }
  278. PANIC("Invalid default tty value: {}", default_tty);
  279. }
  280. }