ahci: fix bug with block read and write

This commit is contained in:
Michael Shaw 2023-05-10 00:40:14 -04:00
commit 0b81c6cccc
3 changed files with 18 additions and 7 deletions

View file

@ -138,6 +138,7 @@ static int cmdslot(hbaport_t *port)
bool ahci_access_sectors(struct sata_device *dev, paddr_t lba, uint16_t count, paddr_t buf, bool write) bool ahci_access_sectors(struct sata_device *dev, paddr_t lba, uint16_t count, paddr_t buf, bool write)
{ {
hbaport_t *port = &dev->abar->ports[dev->port]; hbaport_t *port = &dev->abar->ports[dev->port];
uint16_t ocount = count;
/* clear pending interrupt */ /* clear pending interrupt */
port->is = (uint32_t)-1; port->is = (uint32_t)-1;
@ -212,7 +213,7 @@ bool ahci_access_sectors(struct sata_device *dev, paddr_t lba, uint16_t count, p
/* disk read success */ /* disk read success */
#ifdef KDEBUG #ifdef KDEBUG
printf(LOG_DEBUG "Read %d sectors from disk\n", count); printf(LOG_DEBUG "Read %d sectors from disk\n", ocount);
#endif #endif
return true; return true;
} }
@ -223,11 +224,14 @@ static int ahci_block_read(struct block_device *dev, void *buf, size_t offset, s
/* repeat every 64K, due to the current limitations of this driver */ /* repeat every 64K, due to the current limitations of this driver */
while (sector_count > 0) { while (sector_count > 0) {
if (!ahci_access_sectors(sdev, offset, MIN(sector_count, 128u), (paddr_t)buf, false)) size_t n = MIN(sector_count, 128u);
if (!ahci_access_sectors(sdev, offset, n, (paddr_t)buf & ~hhdm_start, false))
return -1; return -1;
sector_count -= MIN(sector_count, 128u); if(sector_count <= 128)
break;
sector_count -= 128ull;
offset += 128; offset += 128;
buf += 128 << 9; buf += (128 << 9);
} }
return 0; return 0;
@ -239,11 +243,14 @@ static int ahci_block_write(struct block_device *dev, void *buf, size_t offset,
/* repeat every 64K, same as above */ /* repeat every 64K, same as above */
while (sector_count > 0) { while (sector_count > 0) {
if (!ahci_access_sectors(sdev, offset, MIN(sector_count, 128u), (paddr_t)buf, true)) size_t n = MIN(sector_count, 128u);
if (!ahci_access_sectors(sdev, offset, n, (paddr_t)buf & ~hhdm_start, true))
return -1; return -1;
sector_count -= MIN(sector_count, 128u); if(sector_count <= 128)
break;
sector_count -= 128ull;
offset += 128; offset += 128;
buf += 128 << 9; buf += (128 << 9);
} }
return 0; return 0;

View file

@ -22,4 +22,7 @@ struct block_device *block_register(char *name, struct block_device_ops *ops, vo
int block_read(struct block_device *bdev, void *buf, size_t offset, size_t size); int block_read(struct block_device *bdev, void *buf, size_t offset, size_t size);
int block_write(struct block_device *bdev, void *buf, size_t offset, size_t size); int block_write(struct block_device *bdev, void *buf, size_t offset, size_t size);
extern struct block_device *block_devices[];
extern size_t block_devices_count;
#endif /* _BLOCK_H_ */ #endif /* _BLOCK_H_ */

View file

@ -6,6 +6,7 @@
#include <kernel/mem.h> #include <kernel/mem.h>
#include <kernel/slab.h> #include <kernel/slab.h>
#include <kernel/pio.h> #include <kernel/pio.h>
#include <kernel/block.h>
#include <dev/pic.h> #include <dev/pic.h>
#include <dev/console.h> #include <dev/console.h>