StorageManagement.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446
  1. /*
  2. * Copyright (c) 2020-2022, 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/Platform.h>
  8. #include <AK/Singleton.h>
  9. #include <AK/StringView.h>
  10. #include <AK/UUID.h>
  11. #if ARCH(X86_64)
  12. # include <Kernel/Arch/x86_64/ISABus/IDEController.h>
  13. # include <Kernel/Arch/x86_64/PCI/IDELegacyModeController.h>
  14. #endif
  15. #include <Kernel/Bus/PCI/API.h>
  16. #include <Kernel/Bus/PCI/Access.h>
  17. #include <Kernel/Bus/PCI/Controller/VolumeManagementDevice.h>
  18. #include <Kernel/CommandLine.h>
  19. #include <Kernel/Devices/BlockDevice.h>
  20. #include <Kernel/Devices/DeviceManagement.h>
  21. #include <Kernel/FileSystem/Ext2FS/FileSystem.h>
  22. #include <Kernel/FileSystem/VirtualFileSystem.h>
  23. #include <Kernel/Panic.h>
  24. #include <Kernel/Storage/ATA/AHCI/Controller.h>
  25. #include <Kernel/Storage/ATA/GenericIDE/Controller.h>
  26. #include <Kernel/Storage/NVMe/NVMeController.h>
  27. #include <Kernel/Storage/StorageManagement.h>
  28. #include <LibPartition/EBRPartitionTable.h>
  29. #include <LibPartition/GUIDPartitionTable.h>
  30. #include <LibPartition/MBRPartitionTable.h>
  31. namespace Kernel {
  32. static Singleton<StorageManagement> s_the;
  33. static Atomic<u32> s_storage_device_minor_number;
  34. static Atomic<u32> s_partition_device_minor_number;
  35. static Atomic<u32> s_controller_id;
  36. static Atomic<u32> s_relative_ata_controller_id;
  37. static Atomic<u32> s_relative_nvme_controller_id;
  38. static constexpr StringView partition_uuid_prefix = "PARTUUID:"sv;
  39. static constexpr StringView partition_number_prefix = "part"sv;
  40. static constexpr StringView block_device_prefix = "block"sv;
  41. static constexpr StringView ata_device_prefix = "ata"sv;
  42. static constexpr StringView nvme_device_prefix = "nvme"sv;
  43. static constexpr StringView logical_unit_number_device_prefix = "lun"sv;
  44. UNMAP_AFTER_INIT StorageManagement::StorageManagement()
  45. {
  46. }
  47. u32 StorageManagement::generate_relative_nvme_controller_id(Badge<NVMeController>)
  48. {
  49. auto controller_id = s_relative_nvme_controller_id.load();
  50. s_relative_nvme_controller_id++;
  51. return controller_id;
  52. }
  53. u32 StorageManagement::generate_relative_ata_controller_id(Badge<ATAController>)
  54. {
  55. auto controller_id = s_relative_ata_controller_id.load();
  56. s_relative_ata_controller_id++;
  57. return controller_id;
  58. }
  59. void StorageManagement::remove_device(StorageDevice& device)
  60. {
  61. m_storage_devices.remove(device);
  62. }
  63. UNMAP_AFTER_INIT void StorageManagement::enumerate_pci_controllers(bool force_pio, bool nvme_poll)
  64. {
  65. VERIFY(m_controllers.is_empty());
  66. using SubclassID = PCI::MassStorage::SubclassID;
  67. if (!kernel_command_line().disable_physical_storage()) {
  68. // NOTE: Search for VMD devices before actually searching for storage controllers
  69. // because the VMD device is only a bridge to such (NVMe) controllers.
  70. MUST(PCI::enumerate([&](PCI::DeviceIdentifier const& device_identifier) -> void {
  71. constexpr PCI::HardwareID vmd_device = { 0x8086, 0x9a0b };
  72. if (device_identifier.hardware_id() == vmd_device) {
  73. auto controller = PCI::VolumeManagementDevice::must_create(device_identifier);
  74. MUST(PCI::Access::the().add_host_controller_and_scan_for_devices(move(controller)));
  75. }
  76. }));
  77. MUST(PCI::enumerate([&](PCI::DeviceIdentifier const& device_identifier) -> void {
  78. if (device_identifier.class_code().value() != to_underlying(PCI::ClassID::MassStorage)) {
  79. return;
  80. }
  81. auto subclass_code = static_cast<SubclassID>(device_identifier.subclass_code().value());
  82. #if ARCH(X86_64)
  83. if (subclass_code == SubclassID::IDEController && kernel_command_line().is_ide_enabled()) {
  84. if (auto ide_controller_or_error = PCIIDELegacyModeController::initialize(device_identifier, force_pio); !ide_controller_or_error.is_error())
  85. m_controllers.append(ide_controller_or_error.release_value());
  86. else
  87. dmesgln("Unable to initialize IDE controller: {}", ide_controller_or_error.error());
  88. }
  89. #elif ARCH(AARCH64)
  90. (void)force_pio;
  91. TODO_AARCH64();
  92. #else
  93. # error Unknown architecture
  94. #endif
  95. if (subclass_code == SubclassID::SATAController
  96. && device_identifier.prog_if().value() == to_underlying(PCI::MassStorage::SATAProgIF::AHCI)) {
  97. m_controllers.append(AHCIController::initialize(device_identifier));
  98. }
  99. if (subclass_code == SubclassID::NVMeController) {
  100. auto controller = NVMeController::try_initialize(device_identifier, nvme_poll);
  101. if (controller.is_error()) {
  102. dmesgln("Unable to initialize NVMe controller: {}", controller.error());
  103. } else {
  104. m_controllers.append(controller.release_value());
  105. }
  106. }
  107. }));
  108. }
  109. }
  110. UNMAP_AFTER_INIT void StorageManagement::enumerate_storage_devices()
  111. {
  112. VERIFY(!m_controllers.is_empty());
  113. for (auto& controller : m_controllers) {
  114. for (size_t device_index = 0; device_index < controller.devices_count(); device_index++) {
  115. auto device = controller.device(device_index);
  116. if (device.is_null())
  117. continue;
  118. m_storage_devices.append(device.release_nonnull());
  119. }
  120. }
  121. }
  122. UNMAP_AFTER_INIT void StorageManagement::dump_storage_devices_and_partitions() const
  123. {
  124. dbgln("StorageManagement: Detected {} storage devices", m_storage_devices.size_slow());
  125. for (auto const& storage_device : m_storage_devices) {
  126. auto const& partitions = storage_device.partitions();
  127. if (partitions.is_empty()) {
  128. dbgln(" Device: block{}:{} (no partitions)", storage_device.major(), storage_device.minor());
  129. } else {
  130. dbgln(" Device: block{}:{} ({} partitions)", storage_device.major(), storage_device.minor(), partitions.size());
  131. unsigned partition_number = 1;
  132. for (auto const& partition : partitions) {
  133. dbgln(" Partition: {}, block{}:{} (UUID {})", partition_number, partition.major(), partition.minor(), partition.metadata().unique_guid().to_string());
  134. partition_number++;
  135. }
  136. }
  137. }
  138. }
  139. UNMAP_AFTER_INIT ErrorOr<NonnullOwnPtr<Partition::PartitionTable>> StorageManagement::try_to_initialize_partition_table(StorageDevice const& device) const
  140. {
  141. auto mbr_table_or_error = Partition::MBRPartitionTable::try_to_initialize(device);
  142. if (!mbr_table_or_error.is_error())
  143. return mbr_table_or_error.release_value();
  144. auto ebr_table_or_error = Partition::EBRPartitionTable::try_to_initialize(device);
  145. if (!ebr_table_or_error.is_error()) {
  146. return ebr_table_or_error.release_value();
  147. }
  148. return TRY(Partition::GUIDPartitionTable::try_to_initialize(device));
  149. }
  150. UNMAP_AFTER_INIT void StorageManagement::enumerate_disk_partitions()
  151. {
  152. VERIFY(!m_storage_devices.is_empty());
  153. for (auto& device : m_storage_devices) {
  154. auto partition_table_or_error = try_to_initialize_partition_table(device);
  155. if (partition_table_or_error.is_error())
  156. continue;
  157. auto partition_table = partition_table_or_error.release_value();
  158. for (size_t partition_index = 0; partition_index < partition_table->partitions_count(); partition_index++) {
  159. auto partition_metadata = partition_table->partition(partition_index);
  160. if (!partition_metadata.has_value())
  161. continue;
  162. auto disk_partition = DiskPartition::create(device, generate_partition_minor_number(), partition_metadata.value());
  163. device.add_partition(disk_partition);
  164. }
  165. }
  166. }
  167. UNMAP_AFTER_INIT Optional<unsigned> StorageManagement::extract_boot_device_partition_number_parameter(StringView device_prefix)
  168. {
  169. VERIFY(m_boot_argument.starts_with(device_prefix));
  170. VERIFY(!m_boot_argument.starts_with(partition_uuid_prefix));
  171. auto storage_device_relative_address_view = m_boot_argument.substring_view(device_prefix.length());
  172. auto parameter_view = storage_device_relative_address_view.find_last_split_view(';');
  173. if (parameter_view == storage_device_relative_address_view)
  174. return {};
  175. if (!parameter_view.starts_with(partition_number_prefix)) {
  176. PANIC("StorageManagement: Invalid root boot parameter.");
  177. }
  178. auto parameter_number = parameter_view.substring_view(partition_number_prefix.length()).to_uint<unsigned>();
  179. if (!parameter_number.has_value()) {
  180. PANIC("StorageManagement: Invalid root boot parameter.");
  181. }
  182. return parameter_number.value();
  183. }
  184. UNMAP_AFTER_INIT Array<unsigned, 3> StorageManagement::extract_boot_device_address_parameters(StringView device_prefix)
  185. {
  186. VERIFY(!m_boot_argument.starts_with(partition_uuid_prefix));
  187. Array<unsigned, 3> address_parameters;
  188. auto parameters_view = m_boot_argument.substring_view(device_prefix.length()).find_first_split_view(';');
  189. size_t parts_count = 0;
  190. bool parse_failure = false;
  191. parameters_view.for_each_split_view(':', SplitBehavior::Nothing, [&](StringView parameter_view) {
  192. if (parse_failure)
  193. return;
  194. if (parts_count > 2)
  195. return;
  196. auto parameter_number = parameter_view.to_uint<unsigned>();
  197. if (!parameter_number.has_value()) {
  198. parse_failure = true;
  199. return;
  200. }
  201. address_parameters[parts_count] = parameter_number.value();
  202. parts_count++;
  203. });
  204. if (parts_count > 3) {
  205. dbgln("StorageManagement: Detected {} parts in boot device parameter.", parts_count);
  206. PANIC("StorageManagement: Invalid root boot parameter.");
  207. }
  208. if (parse_failure) {
  209. PANIC("StorageManagement: Invalid root boot parameter.");
  210. }
  211. return address_parameters;
  212. }
  213. UNMAP_AFTER_INIT void StorageManagement::resolve_partition_from_boot_device_parameter(StorageDevice const& chosen_storage_device, StringView boot_device_prefix)
  214. {
  215. auto possible_partition_number = extract_boot_device_partition_number_parameter(boot_device_prefix);
  216. if (!possible_partition_number.has_value())
  217. return;
  218. auto partition_number = possible_partition_number.value();
  219. if (chosen_storage_device.partitions().size() <= partition_number)
  220. PANIC("StorageManagement: Invalid partition number parameter.");
  221. m_boot_block_device = chosen_storage_device.partitions()[partition_number];
  222. }
  223. UNMAP_AFTER_INIT void StorageManagement::determine_hardware_relative_boot_device(StringView relative_hardware_prefix, Function<bool(StorageDevice const&)> filter_device_callback)
  224. {
  225. VERIFY(m_boot_argument.starts_with(relative_hardware_prefix));
  226. auto address_parameters = extract_boot_device_address_parameters(relative_hardware_prefix);
  227. RefPtr<StorageDevice> chosen_storage_device;
  228. for (auto& storage_device : m_storage_devices) {
  229. if (!filter_device_callback(storage_device))
  230. continue;
  231. auto storage_device_lun = storage_device.logical_unit_number_address();
  232. if (storage_device.parent_controller_hardware_relative_id() == address_parameters[0]
  233. && storage_device_lun.target_id == address_parameters[1]
  234. && storage_device_lun.disk_id == address_parameters[2]) {
  235. m_boot_block_device = storage_device;
  236. chosen_storage_device = storage_device;
  237. break;
  238. }
  239. }
  240. if (chosen_storage_device)
  241. resolve_partition_from_boot_device_parameter(*chosen_storage_device, relative_hardware_prefix);
  242. }
  243. UNMAP_AFTER_INIT void StorageManagement::determine_ata_boot_device()
  244. {
  245. determine_hardware_relative_boot_device(ata_device_prefix, [](StorageDevice const& device) -> bool {
  246. return device.command_set() == StorageDevice::CommandSet::ATA;
  247. });
  248. }
  249. UNMAP_AFTER_INIT void StorageManagement::determine_nvme_boot_device()
  250. {
  251. determine_hardware_relative_boot_device(nvme_device_prefix, [](StorageDevice const& device) -> bool {
  252. return device.command_set() == StorageDevice::CommandSet::NVMe;
  253. });
  254. }
  255. UNMAP_AFTER_INIT void StorageManagement::determine_block_boot_device()
  256. {
  257. VERIFY(m_boot_argument.starts_with(block_device_prefix));
  258. auto parameters_view = extract_boot_device_address_parameters(block_device_prefix);
  259. // Note: We simply fetch the corresponding BlockDevice with the major and minor parameters.
  260. // We don't try to accept and resolve a partition number as it will make this code much more
  261. // complicated. This rule is also explained in the boot_device_addressing(7) manual page.
  262. LockRefPtr<Device> device = DeviceManagement::the().get_device(parameters_view[0], parameters_view[1]);
  263. if (device && device->is_block_device())
  264. m_boot_block_device = static_ptr_cast<BlockDevice>(device);
  265. }
  266. UNMAP_AFTER_INIT void StorageManagement::determine_boot_device_with_logical_unit_number()
  267. {
  268. VERIFY(m_boot_argument.starts_with(logical_unit_number_device_prefix));
  269. auto address_parameters = extract_boot_device_address_parameters(logical_unit_number_device_prefix);
  270. RefPtr<StorageDevice> chosen_storage_device;
  271. for (auto& storage_device : m_storage_devices) {
  272. auto storage_device_lun = storage_device.logical_unit_number_address();
  273. if (storage_device_lun.controller_id == address_parameters[0]
  274. && storage_device_lun.target_id == address_parameters[1]
  275. && storage_device_lun.disk_id == address_parameters[2]) {
  276. m_boot_block_device = storage_device;
  277. chosen_storage_device = storage_device;
  278. break;
  279. }
  280. }
  281. if (chosen_storage_device)
  282. resolve_partition_from_boot_device_parameter(*chosen_storage_device, logical_unit_number_device_prefix);
  283. }
  284. UNMAP_AFTER_INIT void StorageManagement::determine_boot_device()
  285. {
  286. VERIFY(!m_controllers.is_empty());
  287. if (m_boot_argument.starts_with(block_device_prefix)) {
  288. determine_block_boot_device();
  289. return;
  290. }
  291. if (m_boot_argument.starts_with(partition_uuid_prefix)) {
  292. determine_boot_device_with_partition_uuid();
  293. return;
  294. }
  295. if (m_boot_argument.starts_with(logical_unit_number_device_prefix)) {
  296. determine_boot_device_with_logical_unit_number();
  297. return;
  298. }
  299. if (m_boot_argument.starts_with(ata_device_prefix)) {
  300. determine_ata_boot_device();
  301. return;
  302. }
  303. if (m_boot_argument.starts_with(nvme_device_prefix)) {
  304. determine_nvme_boot_device();
  305. return;
  306. }
  307. PANIC("StorageManagement: Invalid root boot parameter.");
  308. }
  309. UNMAP_AFTER_INIT void StorageManagement::determine_boot_device_with_partition_uuid()
  310. {
  311. VERIFY(!m_storage_devices.is_empty());
  312. VERIFY(m_boot_argument.starts_with(partition_uuid_prefix));
  313. auto partition_uuid = UUID(m_boot_argument.substring_view(partition_uuid_prefix.length()), UUID::Endianness::Mixed);
  314. for (auto& storage_device : m_storage_devices) {
  315. for (auto& partition : storage_device.partitions()) {
  316. if (partition.metadata().unique_guid().is_zero())
  317. continue;
  318. if (partition.metadata().unique_guid() == partition_uuid) {
  319. m_boot_block_device = partition;
  320. break;
  321. }
  322. }
  323. }
  324. }
  325. LockRefPtr<BlockDevice> StorageManagement::boot_block_device() const
  326. {
  327. return m_boot_block_device.strong_ref();
  328. }
  329. MajorNumber StorageManagement::storage_type_major_number()
  330. {
  331. return 3;
  332. }
  333. MinorNumber StorageManagement::generate_storage_minor_number()
  334. {
  335. return s_storage_device_minor_number.fetch_add(1);
  336. }
  337. MinorNumber StorageManagement::generate_partition_minor_number()
  338. {
  339. return s_partition_device_minor_number.fetch_add(1);
  340. }
  341. u32 StorageManagement::generate_controller_id()
  342. {
  343. return s_controller_id.fetch_add(1);
  344. }
  345. NonnullLockRefPtr<FileSystem> StorageManagement::root_filesystem() const
  346. {
  347. auto boot_device_description = boot_block_device();
  348. if (!boot_device_description) {
  349. dump_storage_devices_and_partitions();
  350. PANIC("StorageManagement: Couldn't find a suitable device to boot from");
  351. }
  352. auto description_or_error = OpenFileDescription::try_create(boot_device_description.release_nonnull());
  353. VERIFY(!description_or_error.is_error());
  354. auto file_system = Ext2FS::try_create(description_or_error.release_value()).release_value();
  355. if (auto result = file_system->initialize(); result.is_error()) {
  356. dump_storage_devices_and_partitions();
  357. PANIC("StorageManagement: Couldn't open root filesystem: {}", result.error());
  358. }
  359. return file_system;
  360. }
  361. UNMAP_AFTER_INIT void StorageManagement::initialize(StringView root_device, bool force_pio, bool poll)
  362. {
  363. VERIFY(s_storage_device_minor_number == 0);
  364. m_boot_argument = root_device;
  365. if (PCI::Access::is_disabled()) {
  366. #if ARCH(X86_64)
  367. // Note: If PCI is disabled, we assume that at least we have an ISA IDE controller
  368. // to probe and use
  369. auto isa_ide_controller = MUST(ISAIDEController::initialize());
  370. m_controllers.append(isa_ide_controller);
  371. #endif
  372. } else {
  373. enumerate_pci_controllers(force_pio, poll);
  374. }
  375. enumerate_storage_devices();
  376. enumerate_disk_partitions();
  377. determine_boot_device();
  378. if (m_boot_block_device.is_null()) {
  379. dump_storage_devices_and_partitions();
  380. PANIC("StorageManagement: boot device {} not found", m_boot_argument);
  381. }
  382. }
  383. StorageManagement& StorageManagement::the()
  384. {
  385. return *s_the;
  386. }
  387. }