StorageManagement.cpp 12 KB

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