StorageManagement.cpp 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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/ATA/AHCIController.h>
  16. #include <Kernel/Storage/ATA/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. break;
  120. }
  121. auto start_storage_name = storage_name.substring_view(0, min(storage_device.early_storage_name().length(), storage_name.length()));
  122. if (storage_device.early_storage_name().starts_with(start_storage_name)) {
  123. StringView partition_sign = storage_name.substring_view(start_storage_name.length());
  124. auto possible_partition_number = partition_sign.to_uint<size_t>();
  125. if (!possible_partition_number.has_value())
  126. break;
  127. if (possible_partition_number.value() == 0)
  128. break;
  129. if (storage_device.partitions().size() < possible_partition_number.value())
  130. break;
  131. m_boot_block_device = storage_device.partitions()[possible_partition_number.value() - 1];
  132. break;
  133. }
  134. }
  135. }
  136. if (m_boot_block_device.is_null()) {
  137. PANIC("StorageManagement: boot device {} not found", m_boot_argument);
  138. }
  139. }
  140. UNMAP_AFTER_INIT void StorageManagement::determine_boot_device_with_partition_uuid()
  141. {
  142. VERIFY(!m_storage_devices.is_empty());
  143. VERIFY(m_boot_argument.starts_with(partition_uuid_prefix));
  144. auto partition_uuid = UUID(m_boot_argument.substring_view(partition_uuid_prefix.length()));
  145. if (partition_uuid.to_string().length() != 36) {
  146. PANIC("StorageManagement: Specified partition UUID is not valid");
  147. }
  148. for (auto& storage_device : m_storage_devices) {
  149. for (auto& partition : storage_device.partitions()) {
  150. if (partition.metadata().unique_guid().is_zero())
  151. continue;
  152. if (partition.metadata().unique_guid() == partition_uuid) {
  153. m_boot_block_device = partition;
  154. break;
  155. }
  156. }
  157. }
  158. }
  159. RefPtr<BlockDevice> StorageManagement::boot_block_device() const
  160. {
  161. return m_boot_block_device.strong_ref();
  162. }
  163. int StorageManagement::major_number()
  164. {
  165. return 3;
  166. }
  167. int StorageManagement::minor_number()
  168. {
  169. auto minor_number = s_device_minor_number.load();
  170. s_device_minor_number++;
  171. return minor_number;
  172. }
  173. NonnullRefPtr<FileSystem> StorageManagement::root_filesystem() const
  174. {
  175. auto boot_device_description = boot_block_device();
  176. if (!boot_device_description) {
  177. PANIC("StorageManagement: Couldn't find a suitable device to boot from");
  178. }
  179. auto description_or_error = OpenFileDescription::try_create(boot_device_description.release_nonnull());
  180. VERIFY(!description_or_error.is_error());
  181. auto file_system = Ext2FS::try_create(description_or_error.release_value()).release_value();
  182. if (auto result = file_system->initialize(); result.is_error()) {
  183. PANIC("StorageManagement: Couldn't open root filesystem: {}", result.error());
  184. }
  185. return file_system;
  186. }
  187. UNMAP_AFTER_INIT void StorageManagement::initialize(StringView root_device, bool force_pio)
  188. {
  189. VERIFY(s_device_minor_number == 0);
  190. m_boot_argument = root_device;
  191. enumerate_controllers(force_pio);
  192. enumerate_storage_devices();
  193. enumerate_disk_partitions();
  194. if (!boot_argument_contains_partition_uuid()) {
  195. determine_boot_device();
  196. return;
  197. }
  198. determine_boot_device_with_partition_uuid();
  199. }
  200. StorageManagement& StorageManagement::the()
  201. {
  202. return *s_the;
  203. }
  204. }