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
This commit is contained in:
Bananymous 2023-04-01 00:55:07 +03:00
parent 1ca0d3a3c1
commit d91a5bccf4
2 changed files with 16 additions and 4 deletions

View File

@ -241,7 +241,7 @@ namespace Kernel
BAN::ErrorOr<void> 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;
}

View File

@ -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();
}
}