CommandLine.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include <Kernel/CommandLine.h>
  27. #include <Kernel/Panic.h>
  28. #include <Kernel/StdLib.h>
  29. namespace Kernel {
  30. static char s_cmd_line[1024];
  31. static CommandLine* s_the;
  32. UNMAP_AFTER_INIT void CommandLine::early_initialize(const char* cmd_line)
  33. {
  34. if (!cmd_line)
  35. return;
  36. size_t length = strlen(cmd_line);
  37. if (length >= sizeof(s_cmd_line))
  38. length = sizeof(s_cmd_line) - 1;
  39. memcpy(s_cmd_line, cmd_line, length);
  40. s_cmd_line[length] = '\0';
  41. }
  42. const CommandLine& kernel_command_line()
  43. {
  44. VERIFY(s_the);
  45. return *s_the;
  46. }
  47. UNMAP_AFTER_INIT void CommandLine::initialize()
  48. {
  49. VERIFY(!s_the);
  50. s_the = new CommandLine(s_cmd_line);
  51. }
  52. UNMAP_AFTER_INIT CommandLine::CommandLine(const String& string)
  53. : m_string(string)
  54. {
  55. s_the = this;
  56. const auto& args = m_string.split(' ');
  57. m_params.ensure_capacity(args.size());
  58. for (auto&& str : args) {
  59. if (str == "") {
  60. continue;
  61. }
  62. auto pair = str.split_limit('=', 2);
  63. if (pair.size() == 1) {
  64. m_params.set(move(pair[0]), "");
  65. } else {
  66. m_params.set(move(pair[0]), move(pair[1]));
  67. }
  68. }
  69. }
  70. Optional<String> CommandLine::lookup(const String& key) const
  71. {
  72. return m_params.get(key);
  73. }
  74. bool CommandLine::contains(const String& key) const
  75. {
  76. return m_params.contains(key);
  77. }
  78. UNMAP_AFTER_INIT bool CommandLine::is_boot_profiling_enabled() const
  79. {
  80. return contains("boot_prof");
  81. }
  82. UNMAP_AFTER_INIT bool CommandLine::is_ide_enabled() const
  83. {
  84. return !contains("disable_ide");
  85. }
  86. UNMAP_AFTER_INIT bool CommandLine::is_smp_enabled() const
  87. {
  88. return lookup("smp").value_or("off") == "on";
  89. }
  90. UNMAP_AFTER_INIT bool CommandLine::is_vmmouse_enabled() const
  91. {
  92. return lookup("vmmouse").value_or("on") == "on";
  93. }
  94. UNMAP_AFTER_INIT bool CommandLine::is_pci_ecam_enabled() const
  95. {
  96. auto value = lookup("pci_ecam").value_or("on");
  97. if (value == "on")
  98. return true;
  99. if (value == "off")
  100. return false;
  101. PANIC("Unknown PCI ECAM setting: {}", value);
  102. }
  103. UNMAP_AFTER_INIT bool CommandLine::is_legacy_time_enabled() const
  104. {
  105. return lookup("time").value_or("modern") == "legacy";
  106. }
  107. UNMAP_AFTER_INIT bool CommandLine::is_force_pio() const
  108. {
  109. return contains("force_pio");
  110. }
  111. UNMAP_AFTER_INIT String CommandLine::root_device() const
  112. {
  113. return lookup("root").value_or("/dev/hda");
  114. }
  115. UNMAP_AFTER_INIT AcpiFeatureLevel CommandLine::acpi_feature_level() const
  116. {
  117. auto value = kernel_command_line().lookup("acpi").value_or("on");
  118. if (value == "limited")
  119. return AcpiFeatureLevel::Limited;
  120. if (value == "off")
  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").value_or("periodic");
  127. if (hpet_mode == "periodic")
  128. return HPETMode::Periodic;
  129. if (hpet_mode == "nonperiodic")
  130. return HPETMode::NonPeriodic;
  131. PANIC("Unknown HPETMode: {}", hpet_mode);
  132. }
  133. UNMAP_AFTER_INIT AHCIResetMode CommandLine::ahci_reset_mode() const
  134. {
  135. const auto ahci_reset_mode = lookup("ahci_reset_mode").value_or("controller");
  136. if (ahci_reset_mode == "controller") {
  137. return AHCIResetMode::ControllerOnly;
  138. } else if (ahci_reset_mode == "none") {
  139. return AHCIResetMode::None;
  140. } else if (ahci_reset_mode == "complete") {
  141. return AHCIResetMode::Complete;
  142. }
  143. PANIC("Unknown AHCIResetMode: {}", ahci_reset_mode);
  144. }
  145. UNMAP_AFTER_INIT BootMode CommandLine::boot_mode() const
  146. {
  147. const auto boot_mode = lookup("boot_mode").value_or("graphical");
  148. if (boot_mode == "text") {
  149. return BootMode::Text;
  150. } else if (boot_mode == "self-test") {
  151. return BootMode::SelfTest;
  152. } else if (boot_mode == "graphical") {
  153. return BootMode::Graphical;
  154. }
  155. PANIC("Unknown BootMode: {}", boot_mode);
  156. }
  157. UNMAP_AFTER_INIT bool CommandLine::is_text_mode() const
  158. {
  159. const auto mode = boot_mode();
  160. return mode == BootMode::Text || mode == BootMode::SelfTest;
  161. }
  162. String CommandLine::userspace_init() const
  163. {
  164. return lookup("init").value_or("/bin/SystemServer");
  165. }
  166. Vector<String> CommandLine::userspace_init_args() const
  167. {
  168. auto init_args = lookup("init_args").value_or("").split(',');
  169. if (!init_args.is_empty())
  170. init_args.prepend(userspace_init());
  171. return init_args;
  172. }
  173. }