CommandLine.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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. }
  32. UNMAP_AFTER_INIT CommandLine::CommandLine(const String& string)
  33. : m_string(string)
  34. {
  35. s_the = this;
  36. const auto& args = m_string.split(' ');
  37. m_params.ensure_capacity(args.size());
  38. for (auto&& str : args) {
  39. if (str == "") {
  40. continue;
  41. }
  42. auto pair = str.split_limit('=', 2);
  43. if (pair.size() == 1) {
  44. m_params.set(move(pair[0]), "");
  45. } else {
  46. m_params.set(move(pair[0]), move(pair[1]));
  47. }
  48. }
  49. }
  50. Optional<String> CommandLine::lookup(const String& key) const
  51. {
  52. return m_params.get(key);
  53. }
  54. bool CommandLine::contains(const String& key) const
  55. {
  56. return m_params.contains(key);
  57. }
  58. UNMAP_AFTER_INIT bool CommandLine::is_boot_profiling_enabled() const
  59. {
  60. return contains("boot_prof");
  61. }
  62. UNMAP_AFTER_INIT bool CommandLine::is_ide_enabled() const
  63. {
  64. return !contains("disable_ide");
  65. }
  66. UNMAP_AFTER_INIT bool CommandLine::is_smp_enabled() const
  67. {
  68. return lookup("smp").value_or("off") == "on";
  69. }
  70. UNMAP_AFTER_INIT bool CommandLine::is_vmmouse_enabled() const
  71. {
  72. return lookup("vmmouse").value_or("on") == "on";
  73. }
  74. UNMAP_AFTER_INIT PCIAccessLevel CommandLine::pci_access_level() const
  75. {
  76. auto value = lookup("pci_ecam").value_or("off");
  77. if (value == "on")
  78. return PCIAccessLevel::MappingPerBus;
  79. if (value == "per-device")
  80. return PCIAccessLevel::MappingPerDevice;
  81. if (value == "off")
  82. return PCIAccessLevel::IOAddressing;
  83. PANIC("Unknown PCI ECAM setting: {}", value);
  84. }
  85. UNMAP_AFTER_INIT bool CommandLine::is_legacy_time_enabled() const
  86. {
  87. return lookup("time").value_or("modern") == "legacy";
  88. }
  89. UNMAP_AFTER_INIT bool CommandLine::is_force_pio() const
  90. {
  91. return contains("force_pio");
  92. }
  93. UNMAP_AFTER_INIT String CommandLine::root_device() const
  94. {
  95. return lookup("root").value_or("/dev/hda");
  96. }
  97. UNMAP_AFTER_INIT AcpiFeatureLevel CommandLine::acpi_feature_level() const
  98. {
  99. auto value = kernel_command_line().lookup("acpi").value_or("on");
  100. if (value == "limited")
  101. return AcpiFeatureLevel::Limited;
  102. if (value == "off")
  103. return AcpiFeatureLevel::Disabled;
  104. return AcpiFeatureLevel::Enabled;
  105. }
  106. UNMAP_AFTER_INIT HPETMode CommandLine::hpet_mode() const
  107. {
  108. auto hpet_mode = lookup("hpet").value_or("periodic");
  109. if (hpet_mode == "periodic")
  110. return HPETMode::Periodic;
  111. if (hpet_mode == "nonperiodic")
  112. return HPETMode::NonPeriodic;
  113. PANIC("Unknown HPETMode: {}", hpet_mode);
  114. }
  115. UNMAP_AFTER_INIT bool CommandLine::disable_ps2_controller() const
  116. {
  117. return contains("disable_ps2_controller");
  118. }
  119. UNMAP_AFTER_INIT bool CommandLine::disable_physical_storage() const
  120. {
  121. return contains("disable_physical_storage");
  122. }
  123. UNMAP_AFTER_INIT bool CommandLine::disable_uhci_controller() const
  124. {
  125. return contains("disable_uhci_controller");
  126. }
  127. UNMAP_AFTER_INIT bool CommandLine::disable_virtio() const
  128. {
  129. return contains("disable_virtio");
  130. }
  131. UNMAP_AFTER_INIT AHCIResetMode CommandLine::ahci_reset_mode() const
  132. {
  133. const auto ahci_reset_mode = lookup("ahci_reset_mode").value_or("controller");
  134. if (ahci_reset_mode == "controller") {
  135. return AHCIResetMode::ControllerOnly;
  136. } else if (ahci_reset_mode == "none") {
  137. return AHCIResetMode::None;
  138. } else if (ahci_reset_mode == "complete") {
  139. return AHCIResetMode::Complete;
  140. }
  141. PANIC("Unknown AHCIResetMode: {}", ahci_reset_mode);
  142. }
  143. UNMAP_AFTER_INIT BootMode CommandLine::boot_mode() const
  144. {
  145. const auto boot_mode = lookup("boot_mode").value_or("graphical");
  146. if (boot_mode == "text") {
  147. return BootMode::Text;
  148. } else if (boot_mode == "self-test") {
  149. return BootMode::SelfTest;
  150. } else if (boot_mode == "graphical") {
  151. return BootMode::Graphical;
  152. }
  153. PANIC("Unknown BootMode: {}", boot_mode);
  154. }
  155. UNMAP_AFTER_INIT bool CommandLine::is_text_mode() const
  156. {
  157. const auto mode = boot_mode();
  158. return mode == BootMode::Text || mode == BootMode::SelfTest;
  159. }
  160. String CommandLine::userspace_init() const
  161. {
  162. return lookup("init").value_or("/bin/SystemServer");
  163. }
  164. Vector<String> CommandLine::userspace_init_args() const
  165. {
  166. auto init_args = lookup("init_args").value_or("").split(',');
  167. if (!init_args.is_empty())
  168. init_args.prepend(userspace_init());
  169. return init_args;
  170. }
  171. }