StorageManagement.cpp 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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/NVMe/NVMeController.h>
  18. #include <Kernel/Storage/Partition/EBRPartitionTable.h>
  19. #include <Kernel/Storage/Partition/GUIDPartitionTable.h>
  20. #include <Kernel/Storage/Partition/MBRPartitionTable.h>
  21. #include <Kernel/Storage/RamdiskController.h>
  22. #include <Kernel/Storage/StorageManagement.h>
  23. namespace Kernel {
  24. static Singleton<StorageManagement> s_the;
  25. static Atomic<u32> s_device_minor_number;
  26. static constexpr StringView partition_uuid_prefix = "PARTUUID="sv;
  27. UNMAP_AFTER_INIT StorageManagement::StorageManagement()
  28. {
  29. }
  30. void StorageManagement::remove_device(StorageDevice& device)
  31. {
  32. m_storage_devices.remove(device);
  33. }
  34. bool StorageManagement::boot_argument_contains_partition_uuid()
  35. {
  36. return m_boot_argument.starts_with(partition_uuid_prefix);
  37. }
  38. UNMAP_AFTER_INIT void StorageManagement::enumerate_controllers(bool force_pio)
  39. {
  40. VERIFY(m_controllers.is_empty());
  41. using SubclassID = PCI::MassStorage::SubclassID;
  42. if (!kernel_command_line().disable_physical_storage()) {
  43. PCI::enumerate([&](PCI::DeviceIdentifier const& device_identifier) {
  44. if (device_identifier.class_code().value() != to_underlying(PCI::ClassID::MassStorage)) {
  45. return;
  46. }
  47. auto subclass_code = static_cast<SubclassID>(device_identifier.subclass_code().value());
  48. if (subclass_code == SubclassID::IDEController && kernel_command_line().is_ide_enabled()) {
  49. m_controllers.append(IDEController::initialize(device_identifier, force_pio));
  50. }
  51. if (subclass_code == SubclassID::SATAController
  52. && device_identifier.prog_if().value() == to_underlying(PCI::MassStorage::SATAProgIF::AHCI)) {
  53. m_controllers.append(AHCIController::initialize(device_identifier));
  54. }
  55. if (subclass_code == SubclassID::NVMeController) {
  56. auto controller = NVMeController::try_initialize(device_identifier);
  57. if (controller.is_error()) {
  58. dmesgln("Unable to initialize NVMe controller: {}", controller.error());
  59. } else {
  60. m_controllers.append(controller.release_value());
  61. }
  62. }
  63. });
  64. }
  65. m_controllers.append(RamdiskController::initialize());
  66. }
  67. UNMAP_AFTER_INIT void StorageManagement::enumerate_storage_devices()
  68. {
  69. VERIFY(!m_controllers.is_empty());
  70. for (auto& controller : m_controllers) {
  71. for (size_t device_index = 0; device_index < controller.devices_count(); device_index++) {
  72. auto device = controller.device(device_index);
  73. if (device.is_null())
  74. continue;
  75. m_storage_devices.append(device.release_nonnull());
  76. }
  77. }
  78. }
  79. UNMAP_AFTER_INIT OwnPtr<PartitionTable> StorageManagement::try_to_initialize_partition_table(const StorageDevice& device) const
  80. {
  81. auto mbr_table_or_result = MBRPartitionTable::try_to_initialize(device);
  82. if (!mbr_table_or_result.is_error())
  83. return move(mbr_table_or_result.value());
  84. if (mbr_table_or_result.error() == PartitionTable::Error::MBRProtective) {
  85. auto gpt_table_or_result = GUIDPartitionTable::try_to_initialize(device);
  86. if (gpt_table_or_result.is_error())
  87. return {};
  88. return move(gpt_table_or_result.value());
  89. }
  90. if (mbr_table_or_result.error() == PartitionTable::Error::ConatinsEBR) {
  91. auto ebr_table_or_result = EBRPartitionTable::try_to_initialize(device);
  92. if (ebr_table_or_result.is_error())
  93. return {};
  94. return move(ebr_table_or_result.value());
  95. }
  96. return {};
  97. }
  98. UNMAP_AFTER_INIT void StorageManagement::enumerate_disk_partitions() const
  99. {
  100. VERIFY(!m_storage_devices.is_empty());
  101. NonnullRefPtrVector<DiskPartition> partitions;
  102. size_t device_index = 0;
  103. for (auto& device : m_storage_devices) {
  104. auto partition_table = try_to_initialize_partition_table(device);
  105. if (!partition_table)
  106. continue;
  107. for (size_t partition_index = 0; partition_index < partition_table->partitions_count(); partition_index++) {
  108. auto partition_metadata = partition_table->partition(partition_index);
  109. if (!partition_metadata.has_value())
  110. continue;
  111. // FIXME: Try to not hardcode a maximum of 16 partitions per drive!
  112. auto disk_partition = DiskPartition::create(const_cast<StorageDevice&>(device), (partition_index + (16 * device_index)), partition_metadata.value());
  113. partitions.append(disk_partition);
  114. const_cast<StorageDevice&>(device).m_partitions.append(disk_partition);
  115. }
  116. device_index++;
  117. }
  118. }
  119. UNMAP_AFTER_INIT void StorageManagement::determine_boot_device()
  120. {
  121. VERIFY(!m_controllers.is_empty());
  122. if (m_boot_argument.starts_with("/dev/"sv)) {
  123. StringView storage_name = m_boot_argument.substring_view(5);
  124. for (auto& storage_device : m_storage_devices) {
  125. if (storage_device.early_storage_name() == storage_name) {
  126. m_boot_block_device = storage_device;
  127. break;
  128. }
  129. // If the early storage name's last character is a digit (e.g. in the case of NVMe where the last
  130. // number in the device name indicates the node, e.g. /dev/nvme0n1 we need to append a "p" character
  131. // so that we can properly distinguish the partition index from the device itself
  132. char storage_name_last_char = *(storage_device.early_storage_name().end() - 1);
  133. String early_storage_name;
  134. if (storage_name_last_char >= '0' && storage_name_last_char <= '9')
  135. early_storage_name = String::formatted("{}p", storage_device.early_storage_name());
  136. else
  137. early_storage_name = storage_device.early_storage_name();
  138. auto start_storage_name = storage_name.substring_view(0, min(early_storage_name.length(), storage_name.length()));
  139. if (early_storage_name.starts_with(start_storage_name)) {
  140. StringView partition_sign = storage_name.substring_view(start_storage_name.length());
  141. auto possible_partition_number = partition_sign.to_uint<size_t>();
  142. if (!possible_partition_number.has_value())
  143. break;
  144. if (possible_partition_number.value() == 0)
  145. break;
  146. if (storage_device.partitions().size() < possible_partition_number.value())
  147. break;
  148. m_boot_block_device = storage_device.partitions()[possible_partition_number.value() - 1];
  149. break;
  150. }
  151. }
  152. }
  153. if (m_boot_block_device.is_null()) {
  154. PANIC("StorageManagement: boot device {} not found", m_boot_argument);
  155. }
  156. }
  157. UNMAP_AFTER_INIT void StorageManagement::determine_boot_device_with_partition_uuid()
  158. {
  159. VERIFY(!m_storage_devices.is_empty());
  160. VERIFY(m_boot_argument.starts_with(partition_uuid_prefix));
  161. auto partition_uuid = UUID(m_boot_argument.substring_view(partition_uuid_prefix.length()));
  162. if (partition_uuid.to_string().length() != 36) {
  163. PANIC("StorageManagement: Specified partition UUID is not valid");
  164. }
  165. for (auto& storage_device : m_storage_devices) {
  166. for (auto& partition : storage_device.partitions()) {
  167. if (partition.metadata().unique_guid().is_zero())
  168. continue;
  169. if (partition.metadata().unique_guid() == partition_uuid) {
  170. m_boot_block_device = partition;
  171. break;
  172. }
  173. }
  174. }
  175. }
  176. RefPtr<BlockDevice> StorageManagement::boot_block_device() const
  177. {
  178. return m_boot_block_device.strong_ref();
  179. }
  180. MajorNumber StorageManagement::storage_type_major_number()
  181. {
  182. return 3;
  183. }
  184. MinorNumber StorageManagement::generate_storage_minor_number()
  185. {
  186. auto minor_number = s_device_minor_number.load();
  187. s_device_minor_number++;
  188. return minor_number;
  189. }
  190. NonnullRefPtr<FileSystem> StorageManagement::root_filesystem() const
  191. {
  192. auto boot_device_description = boot_block_device();
  193. if (!boot_device_description) {
  194. PANIC("StorageManagement: Couldn't find a suitable device to boot from");
  195. }
  196. auto description_or_error = OpenFileDescription::try_create(boot_device_description.release_nonnull());
  197. VERIFY(!description_or_error.is_error());
  198. auto file_system = Ext2FS::try_create(description_or_error.release_value()).release_value();
  199. if (auto result = file_system->initialize(); result.is_error()) {
  200. PANIC("StorageManagement: Couldn't open root filesystem: {}", result.error());
  201. }
  202. return file_system;
  203. }
  204. UNMAP_AFTER_INIT void StorageManagement::initialize(StringView root_device, bool force_pio)
  205. {
  206. VERIFY(s_device_minor_number == 0);
  207. m_boot_argument = root_device;
  208. enumerate_controllers(force_pio);
  209. enumerate_storage_devices();
  210. enumerate_disk_partitions();
  211. if (!boot_argument_contains_partition_uuid()) {
  212. determine_boot_device();
  213. return;
  214. }
  215. determine_boot_device_with_partition_uuid();
  216. }
  217. StorageManagement& StorageManagement::the()
  218. {
  219. return *s_the;
  220. }
  221. }