From dcde2ae6b457eabe4be0974e8736d461abed05fc Mon Sep 17 00:00:00 2001 From: Bananymous Date: Sat, 1 Apr 2023 00:55:07 +0300 Subject: [PATCH] Kernel: Reads return 0 bytes read at the end of device We used to not have any idea if we where already at the end of device. Also fixed couple of copy-paste errors from read->write --- kernel/kernel/Storage/ATAController.cpp | 4 +++- kernel/kernel/Storage/StorageDevice.cpp | 16 +++++++++++++--- 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/kernel/kernel/Storage/ATAController.cpp b/kernel/kernel/Storage/ATAController.cpp index 75ebec77..240df780 100644 --- a/kernel/kernel/Storage/ATAController.cpp +++ b/kernel/kernel/Storage/ATAController.cpp @@ -241,7 +241,7 @@ namespace Kernel BAN::ErrorOr ATAController::write(ATADevice* device, uint64_t lba, uint8_t sector_count, const uint8_t* buffer) { if (lba + sector_count > device->m_lba_count) - return BAN::Error::from_c_string("Attempted to read outside of the device boundaries"); + return BAN::Error::from_c_string("Attempted to write outside of the device boundaries"); LockGuard _(m_lock); @@ -377,6 +377,8 @@ namespace Kernel { if (offset % sector_size() || bytes % sector_size()) return BAN::Error::from_errno(EINVAL); + if (offset == total_size()) + return 0; TRY(read_sectors(offset / sector_size(), bytes / sector_size(), (uint8_t*)buffer)); return bytes; } diff --git a/kernel/kernel/Storage/StorageDevice.cpp b/kernel/kernel/Storage/StorageDevice.cpp index fd73a129..ef86120c 100644 --- a/kernel/kernel/Storage/StorageDevice.cpp +++ b/kernel/kernel/Storage/StorageDevice.cpp @@ -218,7 +218,7 @@ namespace Kernel { const uint32_t sectors_in_partition = m_lba_end - m_lba_start; if (lba + sector_count > sectors_in_partition) - return BAN::Error::from_c_string("Attempted to read outside of the partition boundaries"); + return BAN::Error::from_c_string("Attempted to write outside of the partition boundaries"); TRY(m_device.write_sectors(m_lba_start + lba, sector_count, buffer)); return {}; } @@ -237,8 +237,18 @@ namespace Kernel { if (offset % m_device.sector_size() || bytes % m_device.sector_size()) return BAN::Error::from_errno(ENOTSUP); - TRY(read_sectors(offset / m_device.sector_size(), bytes / m_device.sector_size(), (uint8_t*)buffer)); - return bytes; + + const uint32_t sectors_in_partition = m_lba_end - m_lba_start; + uint32_t lba = offset / m_device.sector_size(); + uint32_t sector_count = bytes / m_device.sector_size(); + + if (lba == sectors_in_partition) + return 0; + if (lba + sector_count > sectors_in_partition) + sector_count = sectors_in_partition - lba; + + TRY(read_sectors(lba, sector_count, (uint8_t*)buffer)); + return sector_count * m_device.sector_size(); } } \ No newline at end of file