StorageManagement.cpp 11 KB

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