CommandLine.cpp 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  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. m_params.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 bool CommandLine::is_vmmouse_enabled() const
  122. {
  123. return lookup("vmmouse"sv).value_or("on"sv) == "on"sv;
  124. }
  125. UNMAP_AFTER_INIT PCIAccessLevel CommandLine::pci_access_level() const
  126. {
  127. auto value = lookup("pci"sv).value_or("ecam"sv);
  128. if (value == "ecam"sv)
  129. return PCIAccessLevel::MemoryAddressing;
  130. #if ARCH(I386) || ARCH(X86_64)
  131. if (value == "io"sv)
  132. return PCIAccessLevel::IOAddressing;
  133. #endif
  134. if (value == "none"sv)
  135. return PCIAccessLevel::None;
  136. PANIC("Unknown PCI ECAM setting: {}", value);
  137. }
  138. UNMAP_AFTER_INIT bool CommandLine::is_pci_disabled() const
  139. {
  140. return lookup("pci"sv).value_or("ecam"sv) == "none"sv;
  141. }
  142. UNMAP_AFTER_INIT bool CommandLine::is_legacy_time_enabled() const
  143. {
  144. return lookup("time"sv).value_or("modern"sv) == "legacy"sv;
  145. }
  146. bool CommandLine::is_pc_speaker_enabled() const
  147. {
  148. auto value = lookup("pcspeaker"sv).value_or("off"sv);
  149. if (value == "on"sv)
  150. return true;
  151. if (value == "off"sv)
  152. return false;
  153. PANIC("Unknown pcspeaker setting: {}", value);
  154. }
  155. UNMAP_AFTER_INIT bool CommandLine::is_force_pio() const
  156. {
  157. return contains("force_pio"sv);
  158. }
  159. UNMAP_AFTER_INIT StringView CommandLine::root_device() const
  160. {
  161. return lookup("root"sv).value_or("lun0:0:0"sv);
  162. }
  163. bool CommandLine::is_nvme_polling_enabled() const
  164. {
  165. return contains("nvme_poll"sv);
  166. }
  167. UNMAP_AFTER_INIT AcpiFeatureLevel CommandLine::acpi_feature_level() const
  168. {
  169. auto value = kernel_command_line().lookup("acpi"sv).value_or("limited"sv);
  170. if (value == "limited"sv)
  171. return AcpiFeatureLevel::Limited;
  172. if (value == "off"sv)
  173. return AcpiFeatureLevel::Disabled;
  174. if (value == "on"sv)
  175. return AcpiFeatureLevel::Enabled;
  176. PANIC("Unknown ACPI feature level: {}", value);
  177. }
  178. UNMAP_AFTER_INIT HPETMode CommandLine::hpet_mode() const
  179. {
  180. auto hpet_mode = lookup("hpet"sv).value_or("periodic"sv);
  181. if (hpet_mode == "periodic"sv)
  182. return HPETMode::Periodic;
  183. if (hpet_mode == "nonperiodic"sv)
  184. return HPETMode::NonPeriodic;
  185. PANIC("Unknown HPETMode: {}", hpet_mode);
  186. }
  187. UNMAP_AFTER_INIT bool CommandLine::is_physical_networking_disabled() const
  188. {
  189. return contains("disable_physical_networking"sv);
  190. }
  191. UNMAP_AFTER_INIT bool CommandLine::disable_ps2_controller() const
  192. {
  193. return contains("disable_ps2_controller"sv);
  194. }
  195. UNMAP_AFTER_INIT bool CommandLine::disable_physical_storage() const
  196. {
  197. return contains("disable_physical_storage"sv);
  198. }
  199. UNMAP_AFTER_INIT bool CommandLine::disable_uhci_controller() const
  200. {
  201. return contains("disable_uhci_controller"sv);
  202. }
  203. UNMAP_AFTER_INIT bool CommandLine::disable_usb() const
  204. {
  205. return contains("disable_usb"sv);
  206. }
  207. UNMAP_AFTER_INIT bool CommandLine::disable_virtio() const
  208. {
  209. return contains("disable_virtio"sv);
  210. }
  211. UNMAP_AFTER_INIT AHCIResetMode CommandLine::ahci_reset_mode() const
  212. {
  213. auto const ahci_reset_mode = lookup("ahci_reset_mode"sv).value_or("controllers"sv);
  214. if (ahci_reset_mode == "controllers"sv) {
  215. return AHCIResetMode::ControllerOnly;
  216. }
  217. if (ahci_reset_mode == "aggressive"sv) {
  218. return AHCIResetMode::Aggressive;
  219. }
  220. PANIC("Unknown AHCIResetMode: {}", ahci_reset_mode);
  221. }
  222. StringView CommandLine::system_mode() const
  223. {
  224. return lookup("system_mode"sv).value_or("graphical"sv);
  225. }
  226. PanicMode CommandLine::panic_mode(Validate should_validate) const
  227. {
  228. auto const panic_mode = lookup("panic"sv).value_or("halt"sv);
  229. if (panic_mode == "halt"sv) {
  230. return PanicMode::Halt;
  231. }
  232. if (panic_mode == "shutdown"sv) {
  233. return PanicMode::Shutdown;
  234. }
  235. if (should_validate == Validate::Yes)
  236. PANIC("Unknown PanicMode: {}", panic_mode);
  237. return PanicMode::Halt;
  238. }
  239. UNMAP_AFTER_INIT CommandLine::GraphicsSubsystemMode CommandLine::graphics_subsystem_mode() const
  240. {
  241. auto const graphics_subsystem_mode_value = lookup("graphics_subsystem_mode"sv).value_or("on"sv);
  242. if (graphics_subsystem_mode_value == "on"sv)
  243. return GraphicsSubsystemMode::Enabled;
  244. if (graphics_subsystem_mode_value == "limited"sv)
  245. return GraphicsSubsystemMode::Limited;
  246. if (graphics_subsystem_mode_value == "off"sv)
  247. return GraphicsSubsystemMode::Disabled;
  248. PANIC("Invalid graphics_subsystem_mode value: {}", graphics_subsystem_mode_value);
  249. }
  250. StringView CommandLine::userspace_init() const
  251. {
  252. return lookup("init"sv).value_or("/bin/SystemServer"sv);
  253. }
  254. NonnullOwnPtrVector<KString> CommandLine::userspace_init_args() const
  255. {
  256. NonnullOwnPtrVector<KString> args;
  257. auto init_args = lookup("init_args"sv).value_or(""sv).split_view(';');
  258. if (!init_args.is_empty())
  259. MUST(args.try_prepend(MUST(KString::try_create(userspace_init()))));
  260. for (auto& init_arg : init_args)
  261. args.append(MUST(KString::try_create(init_arg)));
  262. return args;
  263. }
  264. UNMAP_AFTER_INIT size_t CommandLine::switch_to_tty() const
  265. {
  266. auto const default_tty = lookup("switch_to_tty"sv).value_or("1"sv);
  267. auto switch_tty_number = default_tty.to_uint();
  268. if (switch_tty_number.has_value() && switch_tty_number.value() >= 1) {
  269. return switch_tty_number.value() - 1;
  270. }
  271. PANIC("Invalid default tty value: {}", default_tty);
  272. }
  273. }