diff options
| -rw-r--r-- | kernel/devices/src/storage/RAMDisk/RAMDiskDevice.cpp | 33 |
1 files changed, 23 insertions, 10 deletions
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<std::byte *>(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<std::byte const *>(m_boot_module.start_address) + offset; + kstd::libc::memcpy(buffer, source, to_copy); + } + + if (to_copy < block_size) + { + kstd::libc::memset(static_cast<std::byte *>(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<std::byte *>(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<std::byte *>(m_boot_module.start_address) + offset; + kstd::libc::memcpy(destination, buffer, to_write); } } // namespace devices::storage::ram_disk
\ No newline at end of file |
