CommandLine.cpp 7.9 KB

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