CommandLine.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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_mmio_enabled() const
  95. {
  96. return lookup("pci_mmio").value_or("off") == "on";
  97. }
  98. UNMAP_AFTER_INIT bool CommandLine::is_legacy_time_enabled() const
  99. {
  100. return lookup("time").value_or("modern") == "legacy";
  101. }
  102. UNMAP_AFTER_INIT bool CommandLine::is_force_pio() const
  103. {
  104. return contains("force_pio");
  105. }
  106. UNMAP_AFTER_INIT String CommandLine::root_device() const
  107. {
  108. return lookup("root").value_or("/dev/hda");
  109. }
  110. UNMAP_AFTER_INIT AcpiFeatureLevel CommandLine::acpi_feature_level() const
  111. {
  112. auto value = kernel_command_line().lookup("acpi").value_or("on");
  113. if (value == "limited")
  114. return AcpiFeatureLevel::Limited;
  115. if (value == "off")
  116. return AcpiFeatureLevel::Disabled;
  117. return AcpiFeatureLevel::Enabled;
  118. }
  119. UNMAP_AFTER_INIT HPETMode CommandLine::hpet_mode() const
  120. {
  121. auto hpet_mode = lookup("hpet").value_or("periodic");
  122. if (hpet_mode == "periodic")
  123. return HPETMode::Periodic;
  124. if (hpet_mode == "nonperiodic")
  125. return HPETMode::NonPeriodic;
  126. PANIC("Unknown HPETMode: {}", hpet_mode);
  127. }
  128. UNMAP_AFTER_INIT AHCIResetMode CommandLine::ahci_reset_mode() const
  129. {
  130. const auto ahci_reset_mode = lookup("ahci_reset_mode").value_or("controller");
  131. if (ahci_reset_mode == "controller") {
  132. return AHCIResetMode::ControllerOnly;
  133. } else if (ahci_reset_mode == "none") {
  134. return AHCIResetMode::None;
  135. } else if (ahci_reset_mode == "complete") {
  136. return AHCIResetMode::Complete;
  137. }
  138. PANIC("Unknown AHCIResetMode: {}", ahci_reset_mode);
  139. }
  140. UNMAP_AFTER_INIT BootMode CommandLine::boot_mode() const
  141. {
  142. const auto boot_mode = lookup("boot_mode").value_or("graphical");
  143. if (boot_mode == "text") {
  144. return BootMode::Text;
  145. } else if (boot_mode == "self-test") {
  146. return BootMode::SelfTest;
  147. } else if (boot_mode == "graphical") {
  148. return BootMode::Graphical;
  149. }
  150. PANIC("Unknown BootMode: {}", boot_mode);
  151. }
  152. UNMAP_AFTER_INIT bool CommandLine::is_text_mode() const
  153. {
  154. const auto mode = boot_mode();
  155. return mode == BootMode::Text || mode == BootMode::SelfTest;
  156. }
  157. String CommandLine::userspace_init() const
  158. {
  159. return lookup("init").value_or("/bin/SystemServer");
  160. }
  161. Vector<String> CommandLine::userspace_init_args() const
  162. {
  163. auto init_args = lookup("init_args").value_or("").split(',');
  164. if (!init_args.is_empty())
  165. init_args.prepend(userspace_init());
  166. return init_args;
  167. }
  168. }