aboutsummaryrefslogtreecommitdiff
path: root/kernel/devices/src/storage/RAMDisk/RAMDiskDevice.cpp
diff options
context:
space:
mode:
authorLukas Oesch <lukasoesch20@gmail.com>2026-03-03 13:54:48 +0100
committerLukas Oesch <lukasoesch20@gmail.com>2026-03-17 16:42:39 +0100
commit1883c8bbcbd2aeff57ce8f2dac1289f68418bbc7 (patch)
treeb36e0c1883ef36ffd1e76930e429004a7abf9746 /kernel/devices/src/storage/RAMDisk/RAMDiskDevice.cpp
parentf25b3c3cb5fd1e00521b91d575da0177dbb44ddc (diff)
downloadteachos-1883c8bbcbd2aeff57ce8f2dac1289f68418bbc7.tar.xz
teachos-1883c8bbcbd2aeff57ce8f2dac1289f68418bbc7.zip
handle read / write behind module boundaries
Diffstat (limited to 'kernel/devices/src/storage/RAMDisk/RAMDiskDevice.cpp')
-rw-r--r--kernel/devices/src/storage/RAMDisk/RAMDiskDevice.cpp33
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