CommandLine.cpp 9.0 KB

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