#include "kernel/devices/storage/ram_disk/device.hpp" #include "kapi/boot_module/boot_module.hpp" #include "kapi/system.hpp" #include "kernel/devices/block_device.hpp" #include #include #include namespace kernel::devices::storage::ram_disk { namespace { constexpr size_t RAM_DISK_BLOCK_SIZE = 512uz; } // namespace device::device(kapi::boot_modules::boot_module const & module, size_t major, size_t minor) : block_device(major, minor, "ram" + kstd::to_string(minor), RAM_DISK_BLOCK_SIZE) , m_boot_module(module) {} auto device::init() -> bool { return m_boot_module.start_address.raw() != 0 && m_boot_module.size > 0; } auto device::read_block(size_t block_index, void * buffer) const -> void { if (buffer == nullptr) { kapi::system::panic("[RAM DISK DEVICE] read_block called with null buffer."); } auto const info = calculate_transfer(block_index); if (info.to_transfer > 0) { auto const src = static_cast(m_boot_module.start_address) + info.offset; kstd::libc::memcpy(buffer, src, info.to_transfer); } if (info.remainder > 0) { kstd::libc::memset(static_cast(buffer) + info.to_transfer, 0, info.remainder); } } auto device::write_block(size_t block_index, void const * buffer) -> void { if (buffer == nullptr) { kapi::system::panic("[RAM DISK DEVICE] write_block called with null buffer."); } auto const info = calculate_transfer(block_index); if (info.to_transfer > 0) { auto const dest = static_cast(m_boot_module.start_address) + info.offset; kstd::libc::memcpy(dest, buffer, info.to_transfer); } } auto device::size() const -> size_t { return m_boot_module.size; } } // namespace kernel::devices::storage::ram_disk