From 1883c8bbcbd2aeff57ce8f2dac1289f68418bbc7 Mon Sep 17 00:00:00 2001 From: Lukas Oesch Date: Tue, 3 Mar 2026 13:54:48 +0100 Subject: handle read / write behind module boundaries --- .../devices/src/storage/RAMDisk/RAMDiskDevice.cpp | 33 +++++++++++++++------- 1 file changed, 23 insertions(+), 10 deletions(-) (limited to 'kernel/devices/src/storage/RAMDisk') diff --git a/kernel/devices/src/storage/RAMDisk/RAMDiskDevice.cpp b/kernel/devices/src/storage/RAMDisk/RAMDiskDevice.cpp index 1bc475d..627641c 100644 --- a/kernel/devices/src/storage/RAMDisk/RAMDiskDevice.cpp +++ b/kernel/devices/src/storage/RAMDisk/RAMDiskDevice.cpp @@ -27,13 +27,22 @@ namespace devices::storage::ram_disk kapi::system::panic("[RAM DISK DEVICE] read_block called with null buffer."); } - // TODO BA-FS26 add bounds checking based on module size? - // TODO BA-FS26 fill with 0 after the end of the module, if the block extends beyond it? + size_t const offset = block_index * block_size; + size_t const limit = m_boot_module.size; - auto const offset = block_index * block_size; - auto const source = static_cast(m_boot_module.start_address) + offset; + size_t const available = (offset < limit) ? (limit - offset) : 0; + size_t const to_copy = (available < block_size) ? available : block_size; - kstd::libc::memcpy(buffer, source, block_size); + if (to_copy > 0) + { + auto const source = static_cast(m_boot_module.start_address) + offset; + kstd::libc::memcpy(buffer, source, to_copy); + } + + if (to_copy < block_size) + { + kstd::libc::memset(static_cast(buffer) + to_copy, std::byte{0}, block_size - to_copy); + } } auto ram_disk_device::write_block(size_t block_index, void const * buffer) -> void @@ -43,12 +52,16 @@ namespace devices::storage::ram_disk kapi::system::panic("[RAM DISK DEVICE] write_block called with null buffer."); } - // TODO BA-FS26 add bounds checking based on module size? - // TODO BA-FS26 ignore writes beyond the end of the module? + size_t const offset = block_index * block_size; + size_t const limit = m_boot_module.size; - auto const offset = block_index * block_size; - auto const destination = static_cast(m_boot_module.start_address) + offset; + if (offset >= limit) + return; - kstd::libc::memcpy(destination, buffer, block_size); + size_t const available = limit - offset; + size_t const to_write = (available < block_size) ? available : block_size; + + auto const destination = static_cast(m_boot_module.start_address) + offset; + kstd::libc::memcpy(destination, buffer, to_write); } } // namespace devices::storage::ram_disk \ No newline at end of file -- cgit v1.2.3