CommandLine.cpp 5.5 KB

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