StorageManagement.cpp 7.4 KB

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