StorageManagement.cpp 12 KB

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