StorageManagement.cpp 12 KB

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