#include "devices/storage/StorageManagement.hpp" #include "kapi/boot_modules.hpp" #include "kapi/system.hpp" #include "devices/BlockDevice.hpp" #include "devices/storage/RAMDisk/RAMDiskController.hpp" #include "devices/storage/StorageController.hpp" #include #include #include namespace devices::storage { namespace { constexpr size_t static MINORS_PER_DEVICE = 16; constinit size_t static next_free_major = 1; constinit auto static active_storage_management = std::optional{}; constinit auto static active_ram_disk_controller = std::optional{}; } // namespace auto storage_management::init() -> void { if (active_storage_management) { kapi::system::panic("[DEVICES] Storage management has already been initialized."); } active_storage_management.emplace(storage_management{}); active_ram_disk_controller.emplace(&kapi::boot_modules::get_boot_module_registry()); active_storage_management->add_controller(&active_ram_disk_controller.value()); std::ranges::for_each(active_storage_management->m_controllers, [](auto controller) { controller->probe(); }); } auto storage_management::get() -> storage_management & { if (!active_storage_management) { kapi::system::panic("[DEVICES] Storage management has not been initialized."); } return *active_storage_management; } auto storage_management::add_controller(storage_controller * controller) -> void { controller->set_ids(next_free_major++, MINORS_PER_DEVICE); m_controllers.at(0) = controller; // TODO BA-FS26 use push_back from kstd:vector } auto storage_management::device_by_major_minor(size_t major, size_t minor) -> block_device * { block_device * found = nullptr; std::ranges::find_if(m_controllers, [&](auto const controller) { if (controller != nullptr && controller->major() == major) { found = controller->device_by_major_minor(major, minor); return found != nullptr; } return false; }); return found; } } // namespace devices::storage