StorageManagement.cpp 7.6 KB

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