CommandLine.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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/StdLib.h>
  10. namespace Kernel {
  11. static char s_cmd_line[1024];
  12. static constexpr StringView s_embedded_cmd_line = "";
  13. static CommandLine* s_the;
  14. UNMAP_AFTER_INIT void CommandLine::early_initialize(const char* cmd_line)
  15. {
  16. if (!cmd_line)
  17. return;
  18. size_t length = strlen(cmd_line);
  19. if (length >= sizeof(s_cmd_line))
  20. length = sizeof(s_cmd_line) - 1;
  21. memcpy(s_cmd_line, cmd_line, length);
  22. s_cmd_line[length] = '\0';
  23. }
  24. const CommandLine& kernel_command_line()
  25. {
  26. VERIFY(s_the);
  27. return *s_the;
  28. }
  29. UNMAP_AFTER_INIT void CommandLine::initialize()
  30. {
  31. VERIFY(!s_the);
  32. s_the = new CommandLine(s_cmd_line);
  33. dmesgln("Kernel Commandline: {}", kernel_command_line().string());
  34. }
  35. UNMAP_AFTER_INIT void CommandLine::build_commandline(const String& cmdline_from_bootloader)
  36. {
  37. StringBuilder builder;
  38. builder.append(cmdline_from_bootloader);
  39. if (!s_embedded_cmd_line.is_empty()) {
  40. builder.append(" ");
  41. builder.append(s_embedded_cmd_line);
  42. }
  43. m_string = builder.to_string();
  44. }
  45. UNMAP_AFTER_INIT void CommandLine::add_arguments(const Vector<StringView>& args)
  46. {
  47. for (auto&& str : args) {
  48. if (str == ""sv) {
  49. continue;
  50. }
  51. auto pair = str.split_view('=', false);
  52. VERIFY(pair.size() == 2 || pair.size() == 1);
  53. if (pair.size() == 1) {
  54. m_params.set(move(pair[0]), ""sv);
  55. } else {
  56. m_params.set(move(pair[0]), move(pair[1]));
  57. }
  58. }
  59. }
  60. UNMAP_AFTER_INIT CommandLine::CommandLine(const String& cmdline_from_bootloader)
  61. {
  62. s_the = this;
  63. build_commandline(cmdline_from_bootloader);
  64. const auto& args = m_string.split_view(' ');
  65. m_params.ensure_capacity(args.size());
  66. add_arguments(args);
  67. }
  68. Optional<String> CommandLine::lookup(const StringView& key) const
  69. {
  70. return m_params.get(key);
  71. }
  72. bool CommandLine::contains(const StringView& key) const
  73. {
  74. return m_params.contains(key);
  75. }
  76. UNMAP_AFTER_INIT bool CommandLine::is_boot_profiling_enabled() const
  77. {
  78. return contains("boot_prof"sv);
  79. }
  80. UNMAP_AFTER_INIT bool CommandLine::is_ide_enabled() const
  81. {
  82. return !contains("disable_ide"sv);
  83. }
  84. UNMAP_AFTER_INIT bool CommandLine::is_smp_enabled() const
  85. {
  86. return lookup("smp"sv).value_or("off"sv) == "on"sv;
  87. }
  88. UNMAP_AFTER_INIT bool CommandLine::is_vmmouse_enabled() const
  89. {
  90. return lookup("vmmouse"sv).value_or("on") == "on"sv;
  91. }
  92. UNMAP_AFTER_INIT PCIAccessLevel CommandLine::pci_access_level() const
  93. {
  94. auto value = lookup("pci_ecam"sv).value_or("off"sv);
  95. if (value == "on"sv)
  96. return PCIAccessLevel::MappingPerBus;
  97. if (value == "per-device"sv)
  98. return PCIAccessLevel::MappingPerDevice;
  99. if (value == "off"sv)
  100. return PCIAccessLevel::IOAddressing;
  101. PANIC("Unknown PCI ECAM setting: {}", value);
  102. }
  103. UNMAP_AFTER_INIT bool CommandLine::is_legacy_time_enabled() const
  104. {
  105. return lookup("time"sv).value_or("modern"sv) == "legacy"sv;
  106. }
  107. UNMAP_AFTER_INIT bool CommandLine::is_force_pio() const
  108. {
  109. return contains("force_pio"sv);
  110. }
  111. UNMAP_AFTER_INIT String CommandLine::root_device() const
  112. {
  113. return lookup("root"sv).value_or("/dev/hda"sv);
  114. }
  115. UNMAP_AFTER_INIT AcpiFeatureLevel CommandLine::acpi_feature_level() const
  116. {
  117. auto value = kernel_command_line().lookup("acpi"sv).value_or("on"sv);
  118. if (value == "limited"sv)
  119. return AcpiFeatureLevel::Limited;
  120. if (value == "off"sv)
  121. return AcpiFeatureLevel::Disabled;
  122. return AcpiFeatureLevel::Enabled;
  123. }
  124. UNMAP_AFTER_INIT HPETMode CommandLine::hpet_mode() const
  125. {
  126. auto hpet_mode = lookup("hpet"sv).value_or("periodic"sv);
  127. if (hpet_mode == "periodic"sv)
  128. return HPETMode::Periodic;
  129. if (hpet_mode == "nonperiodic"sv)
  130. return HPETMode::NonPeriodic;
  131. PANIC("Unknown HPETMode: {}", hpet_mode);
  132. }
  133. UNMAP_AFTER_INIT bool CommandLine::disable_ps2_controller() const
  134. {
  135. return contains("disable_ps2_controller"sv);
  136. }
  137. UNMAP_AFTER_INIT bool CommandLine::disable_physical_storage() const
  138. {
  139. return contains("disable_physical_storage"sv);
  140. }
  141. UNMAP_AFTER_INIT bool CommandLine::disable_uhci_controller() const
  142. {
  143. return contains("disable_uhci_controller"sv);
  144. }
  145. UNMAP_AFTER_INIT bool CommandLine::disable_virtio() const
  146. {
  147. return contains("disable_virtio"sv);
  148. }
  149. UNMAP_AFTER_INIT AHCIResetMode CommandLine::ahci_reset_mode() const
  150. {
  151. const auto ahci_reset_mode = lookup("ahci_reset_mode"sv).value_or("controllers"sv);
  152. if (ahci_reset_mode == "controllers"sv) {
  153. return AHCIResetMode::ControllerOnly;
  154. } else if (ahci_reset_mode == "aggressive"sv) {
  155. return AHCIResetMode::Aggressive;
  156. }
  157. PANIC("Unknown AHCIResetMode: {}", ahci_reset_mode);
  158. }
  159. UNMAP_AFTER_INIT BootMode CommandLine::boot_mode() const
  160. {
  161. const auto boot_mode = lookup("boot_mode"sv).value_or("graphical"sv);
  162. if (boot_mode == "no-fbdev"sv) {
  163. return BootMode::NoFramebufferDevices;
  164. } else if (boot_mode == "self-test"sv) {
  165. return BootMode::SelfTest;
  166. } else if (boot_mode == "graphical"sv) {
  167. return BootMode::Graphical;
  168. }
  169. PANIC("Unknown BootMode: {}", boot_mode);
  170. }
  171. UNMAP_AFTER_INIT bool CommandLine::is_no_framebuffer_devices_mode() const
  172. {
  173. const auto mode = boot_mode();
  174. return mode == BootMode::NoFramebufferDevices || mode == BootMode::SelfTest;
  175. }
  176. String CommandLine::userspace_init() const
  177. {
  178. return lookup("init"sv).value_or("/bin/SystemServer"sv);
  179. }
  180. Vector<String> CommandLine::userspace_init_args() const
  181. {
  182. auto init_args = lookup("init_args"sv).value_or(""sv).split(',');
  183. if (!init_args.is_empty())
  184. init_args.prepend(userspace_init());
  185. return init_args;
  186. }
  187. UNMAP_AFTER_INIT size_t CommandLine::switch_to_tty() const
  188. {
  189. const auto default_tty = lookup("switch_to_tty"sv).value_or("1"sv);
  190. auto switch_tty_number = default_tty.to_uint();
  191. if (switch_tty_number.has_value() && switch_tty_number.value() >= 1) {
  192. return switch_tty_number.value() - 1;
  193. }
  194. PANIC("Invalid default tty value: {}", default_tty);
  195. }
  196. }