StorageManagement.cpp 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. /*
  2. * Copyright (c) 2020, Liav A. <liavalb@hotmail.co.il>
  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 <AK/UUID.h>
  27. #include <Kernel/CommandLine.h>
  28. #include <Kernel/Devices/BlockDevice.h>
  29. #include <Kernel/FileSystem/Ext2FileSystem.h>
  30. #include <Kernel/PCI/Access.h>
  31. #include <Kernel/Panic.h>
  32. #include <Kernel/Storage/IDEController.h>
  33. #include <Kernel/Storage/Partition/EBRPartitionTable.h>
  34. #include <Kernel/Storage/Partition/GUIDPartitionTable.h>
  35. #include <Kernel/Storage/Partition/MBRPartitionTable.h>
  36. #include <Kernel/Storage/RamdiskController.h>
  37. #include <Kernel/Storage/StorageManagement.h>
  38. namespace Kernel {
  39. static StorageManagement* s_the;
  40. StorageManagement::StorageManagement(String boot_argument, bool force_pio)
  41. : m_boot_argument(boot_argument)
  42. , m_controllers(enumerate_controllers(force_pio))
  43. , m_storage_devices(enumerate_storage_devices())
  44. , m_disk_partitions(enumerate_disk_partitions())
  45. {
  46. if (!boot_argument_contains_partition_uuid()) {
  47. determine_boot_device();
  48. return;
  49. }
  50. determine_boot_device_with_partition_uuid();
  51. }
  52. bool StorageManagement::boot_argument_contains_partition_uuid()
  53. {
  54. return m_boot_argument.starts_with("PARTUUID=");
  55. }
  56. NonnullRefPtrVector<StorageController> StorageManagement::enumerate_controllers(bool force_pio) const
  57. {
  58. NonnullRefPtrVector<StorageController> controllers;
  59. if (!kernel_command_line().contains("disable_ide")) {
  60. PCI::enumerate([&](const PCI::Address& address, PCI::ID) {
  61. if (PCI::get_class(address) == 0x1 && PCI::get_subclass(address) == 0x1) {
  62. controllers.append(IDEController::initialize(address, force_pio));
  63. }
  64. });
  65. }
  66. controllers.append(RamdiskController::initialize());
  67. return controllers;
  68. }
  69. NonnullRefPtrVector<StorageDevice> StorageManagement::enumerate_storage_devices() const
  70. {
  71. ASSERT(!m_controllers.is_empty());
  72. NonnullRefPtrVector<StorageDevice> devices;
  73. for (auto& controller : m_controllers) {
  74. for (size_t device_index = 0; device_index < controller.devices_count(); device_index++) {
  75. auto device = controller.device(device_index);
  76. if (device.is_null())
  77. continue;
  78. devices.append(device.release_nonnull());
  79. }
  80. }
  81. return devices;
  82. }
  83. OwnPtr<PartitionTable> StorageManagement::try_to_initialize_partition_table(const StorageDevice& device) const
  84. {
  85. auto mbr_table_or_result = MBRPartitionTable::try_to_initialize(device);
  86. if (!mbr_table_or_result.is_error())
  87. return move(mbr_table_or_result.value());
  88. if (mbr_table_or_result.error() == PartitionTable::Error::MBRProtective) {
  89. auto gpt_table_or_result = GUIDPartitionTable::try_to_initialize(device);
  90. if (gpt_table_or_result.is_error())
  91. return {};
  92. return move(gpt_table_or_result.value());
  93. }
  94. if (mbr_table_or_result.error() == PartitionTable::Error::ConatinsEBR) {
  95. auto ebr_table_or_result = EBRPartitionTable::try_to_initialize(device);
  96. if (ebr_table_or_result.is_error())
  97. return {};
  98. return move(ebr_table_or_result.value());
  99. }
  100. return {};
  101. }
  102. NonnullRefPtrVector<DiskPartition> StorageManagement::enumerate_disk_partitions() const
  103. {
  104. ASSERT(!m_storage_devices.is_empty());
  105. NonnullRefPtrVector<DiskPartition> partitions;
  106. size_t device_index = 0;
  107. for (auto& device : m_storage_devices) {
  108. auto partition_table = try_to_initialize_partition_table(device);
  109. if (!partition_table)
  110. continue;
  111. for (size_t partition_index = 0; partition_index < partition_table->partitions_count(); partition_index++) {
  112. auto partition_metadata = partition_table->partition(partition_index);
  113. if (!partition_metadata.has_value())
  114. continue;
  115. // FIXME: Try to not hardcode a maximum of 16 partitions per drive!
  116. auto disk_partition = DiskPartition::create(const_cast<StorageDevice&>(device), (partition_index + (16 * device_index)), partition_metadata.value());
  117. partitions.append(disk_partition);
  118. const_cast<StorageDevice&>(device).m_partitions.append(disk_partition);
  119. }
  120. device_index++;
  121. }
  122. return partitions;
  123. }
  124. void StorageManagement::determine_boot_device()
  125. {
  126. ASSERT(!m_controllers.is_empty());
  127. if (m_boot_argument.starts_with("/dev/")) {
  128. StringView device_name = m_boot_argument.substring_view(5);
  129. Device::for_each([&](Device& device) {
  130. if (device.is_block_device()) {
  131. auto& block_device = static_cast<BlockDevice&>(device);
  132. if (device.device_name() == device_name) {
  133. m_boot_block_device = block_device;
  134. }
  135. }
  136. });
  137. }
  138. if (m_boot_block_device.is_null()) {
  139. PANIC("StorageManagement: boot device {} not found", m_boot_argument);
  140. }
  141. }
  142. void StorageManagement::determine_boot_device_with_partition_uuid()
  143. {
  144. ASSERT(!m_disk_partitions.is_empty());
  145. ASSERT(m_boot_argument.starts_with("PARTUUID="));
  146. auto partition_uuid = UUID(m_boot_argument.substring_view(strlen("PARTUUID=")));
  147. if (partition_uuid.to_string().length() != 36) {
  148. PANIC("StorageManagement: Specified partition UUID is not valid");
  149. }
  150. for (auto& partition : m_disk_partitions) {
  151. if (partition.metadata().unique_guid().is_zero())
  152. continue;
  153. if (partition.metadata().unique_guid() == partition_uuid) {
  154. m_boot_block_device = partition;
  155. break;
  156. }
  157. }
  158. }
  159. RefPtr<BlockDevice> StorageManagement::boot_block_device() const
  160. {
  161. return m_boot_block_device;
  162. }
  163. NonnullRefPtr<FS> StorageManagement::root_filesystem() const
  164. {
  165. auto boot_device_description = boot_block_device();
  166. if (!boot_device_description) {
  167. PANIC("StorageManagement: Couldn't find a suitable device to boot from");
  168. }
  169. auto e2fs = Ext2FS::create(FileDescription::create(boot_device_description.release_nonnull()).value());
  170. if (!e2fs->initialize()) {
  171. PANIC("StorageManagement: Couldn't open root filesystem");
  172. }
  173. return e2fs;
  174. }
  175. bool StorageManagement::initialized()
  176. {
  177. return (s_the != nullptr);
  178. }
  179. void StorageManagement::initialize(String root_device, bool force_pio)
  180. {
  181. ASSERT(!StorageManagement::initialized());
  182. s_the = new StorageManagement(root_device, force_pio);
  183. }
  184. StorageManagement& StorageManagement::the()
  185. {
  186. return *s_the;
  187. }
  188. NonnullRefPtrVector<StorageController> StorageManagement::ide_controllers() const
  189. {
  190. NonnullRefPtrVector<StorageController> ide_controllers;
  191. for (auto& controller : m_controllers) {
  192. if (controller.type() == StorageController::Type::IDE)
  193. ide_controllers.append(controller);
  194. }
  195. return ide_controllers;
  196. }
  197. }