CommandLine.cpp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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 boot mode the user passed in.
  36. (void)s_the->boot_mode(Validate::Yes);
  37. }
  38. UNMAP_AFTER_INIT void CommandLine::build_commandline(const String& cmdline_from_bootloader)
  39. {
  40. StringBuilder builder;
  41. builder.append(cmdline_from_bootloader);
  42. if constexpr (!s_embedded_cmd_line.is_empty()) {
  43. builder.append(" ");
  44. builder.append(s_embedded_cmd_line);
  45. }
  46. m_string = builder.to_string();
  47. }
  48. UNMAP_AFTER_INIT void CommandLine::add_arguments(const Vector<StringView>& args)
  49. {
  50. for (auto&& str : args) {
  51. if (str == ""sv) {
  52. continue;
  53. }
  54. auto pair = str.split_view('=', false);
  55. VERIFY(pair.size() == 2 || pair.size() == 1);
  56. if (pair.size() == 1) {
  57. m_params.set(move(pair[0]), ""sv);
  58. } else {
  59. m_params.set(move(pair[0]), move(pair[1]));
  60. }
  61. }
  62. }
  63. UNMAP_AFTER_INIT CommandLine::CommandLine(const String& cmdline_from_bootloader)
  64. {
  65. s_the = this;
  66. build_commandline(cmdline_from_bootloader);
  67. const auto& args = m_string.split_view(' ');
  68. m_params.ensure_capacity(args.size());
  69. add_arguments(args);
  70. }
  71. Optional<StringView> CommandLine::lookup(const StringView& key) const
  72. {
  73. return m_params.get(key);
  74. }
  75. bool CommandLine::contains(const StringView& key) const
  76. {
  77. return m_params.contains(key);
  78. }
  79. UNMAP_AFTER_INIT bool CommandLine::is_boot_profiling_enabled() const
  80. {
  81. return contains("boot_prof"sv);
  82. }
  83. UNMAP_AFTER_INIT bool CommandLine::is_ide_enabled() const
  84. {
  85. return !contains("disable_ide"sv);
  86. }
  87. UNMAP_AFTER_INIT bool CommandLine::is_smp_enabled() const
  88. {
  89. return lookup("smp"sv).value_or("off"sv) == "on"sv;
  90. }
  91. UNMAP_AFTER_INIT bool CommandLine::is_vmmouse_enabled() const
  92. {
  93. return lookup("vmmouse"sv).value_or("on") == "on"sv;
  94. }
  95. UNMAP_AFTER_INIT PCIAccessLevel CommandLine::pci_access_level() const
  96. {
  97. auto value = lookup("pci_ecam"sv).value_or("on"sv);
  98. if (value == "on"sv)
  99. return PCIAccessLevel::MemoryAddressing;
  100. if (value == "off"sv)
  101. return PCIAccessLevel::IOAddressing;
  102. PANIC("Unknown PCI ECAM setting: {}", value);
  103. }
  104. UNMAP_AFTER_INIT bool CommandLine::is_legacy_time_enabled() const
  105. {
  106. return lookup("time"sv).value_or("modern"sv) == "legacy"sv;
  107. }
  108. UNMAP_AFTER_INIT bool CommandLine::is_force_pio() const
  109. {
  110. return contains("force_pio"sv);
  111. }
  112. UNMAP_AFTER_INIT StringView CommandLine::root_device() const
  113. {
  114. return lookup("root"sv).value_or("/dev/hda"sv);
  115. }
  116. UNMAP_AFTER_INIT AcpiFeatureLevel CommandLine::acpi_feature_level() const
  117. {
  118. auto value = kernel_command_line().lookup("acpi"sv).value_or("limited"sv);
  119. if (value == "limited"sv)
  120. return AcpiFeatureLevel::Limited;
  121. if (value == "off"sv)
  122. return AcpiFeatureLevel::Disabled;
  123. if (value == "on"sv)
  124. return AcpiFeatureLevel::Enabled;
  125. PANIC("Unknown ACPI feature level: {}", value);
  126. }
  127. UNMAP_AFTER_INIT HPETMode CommandLine::hpet_mode() const
  128. {
  129. auto hpet_mode = lookup("hpet"sv).value_or("periodic"sv);
  130. if (hpet_mode == "periodic"sv)
  131. return HPETMode::Periodic;
  132. if (hpet_mode == "nonperiodic"sv)
  133. return HPETMode::NonPeriodic;
  134. PANIC("Unknown HPETMode: {}", hpet_mode);
  135. }
  136. UNMAP_AFTER_INIT bool CommandLine::is_physical_networking_disabled() const
  137. {
  138. return contains("disable_physical_networking"sv);
  139. }
  140. UNMAP_AFTER_INIT bool CommandLine::disable_ps2_controller() const
  141. {
  142. return contains("disable_ps2_controller"sv);
  143. }
  144. UNMAP_AFTER_INIT bool CommandLine::disable_physical_storage() const
  145. {
  146. return contains("disable_physical_storage"sv);
  147. }
  148. UNMAP_AFTER_INIT bool CommandLine::disable_uhci_controller() const
  149. {
  150. return contains("disable_uhci_controller"sv);
  151. }
  152. UNMAP_AFTER_INIT bool CommandLine::disable_usb() const
  153. {
  154. return contains("disable_usb"sv);
  155. }
  156. UNMAP_AFTER_INIT bool CommandLine::disable_virtio() const
  157. {
  158. return contains("disable_virtio"sv);
  159. }
  160. UNMAP_AFTER_INIT AHCIResetMode CommandLine::ahci_reset_mode() const
  161. {
  162. const auto ahci_reset_mode = lookup("ahci_reset_mode"sv).value_or("controllers"sv);
  163. if (ahci_reset_mode == "controllers"sv) {
  164. return AHCIResetMode::ControllerOnly;
  165. } else if (ahci_reset_mode == "aggressive"sv) {
  166. return AHCIResetMode::Aggressive;
  167. }
  168. PANIC("Unknown AHCIResetMode: {}", ahci_reset_mode);
  169. }
  170. BootMode CommandLine::boot_mode(Validate should_validate) const
  171. {
  172. const auto boot_mode = lookup("boot_mode"sv).value_or("graphical"sv);
  173. if (boot_mode == "no-fbdev"sv) {
  174. return BootMode::NoFramebufferDevices;
  175. } else if (boot_mode == "self-test"sv) {
  176. return BootMode::SelfTest;
  177. } else if (boot_mode == "graphical"sv) {
  178. return BootMode::Graphical;
  179. }
  180. if (should_validate == Validate::Yes)
  181. PANIC("Unknown BootMode: {}", boot_mode);
  182. return BootMode::Unknown;
  183. }
  184. UNMAP_AFTER_INIT bool CommandLine::are_framebuffer_devices_enabled() const
  185. {
  186. return lookup("fbdev"sv).value_or("on"sv) == "on"sv;
  187. }
  188. StringView CommandLine::userspace_init() const
  189. {
  190. return lookup("init"sv).value_or("/bin/SystemServer"sv);
  191. }
  192. NonnullOwnPtrVector<KString> CommandLine::userspace_init_args() const
  193. {
  194. NonnullOwnPtrVector<KString> args;
  195. auto init_args = lookup("init_args"sv).value_or(""sv).split_view(';');
  196. if (!init_args.is_empty())
  197. args.prepend(KString::must_create(userspace_init()));
  198. for (auto& init_arg : init_args)
  199. args.append(KString::must_create(init_arg));
  200. return args;
  201. }
  202. UNMAP_AFTER_INIT size_t CommandLine::switch_to_tty() const
  203. {
  204. const auto default_tty = lookup("switch_to_tty"sv).value_or("1"sv);
  205. auto switch_tty_number = default_tty.to_uint();
  206. if (switch_tty_number.has_value() && switch_tty_number.value() >= 1) {
  207. return switch_tty_number.value() - 1;
  208. }
  209. PANIC("Invalid default tty value: {}", default_tty);
  210. }
  211. }