Add cached read function + some slight updates

This commit is contained in:
iiSaLMaN 2019-06-02 12:17:49 +03:00 committed by GitHub
parent 3768a8cf2e
commit e34646ba6c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -7,6 +7,7 @@
#define SECTOR_SIZE 512
static uint64_t last_sector = -1;
static uint8_t sector_buf[512];
static struct {
@ -17,11 +18,11 @@ static struct {
uint64_t lba;
} dap = { 16, 1, 0, 0, 0 };
int read_sector(int drive, int lba, int count, void *buffer) {
int read_sector(int drive, void *buffer, uint64_t lba, size_t count) {
dap.offset = (uint16_t)(size_t)sector_buf;
while (count--) {
dap.lba = lba++;
dap.lba = lba;
struct rm_regs r = {0};
r.eax = 0x4200;
r.edx = drive;
@ -35,9 +36,38 @@ int read_sector(int drive, int lba, int count, void *buffer) {
}
memcpy(buffer, sector_buf, SECTOR_SIZE);
last_sector = lba++;
buffer += SECTOR_SIZE;
}
return 0;
}
int read(int drive, void *buffer, int offset, size_t count) {
int res;
if (last_sector == (uint64_t)-1)
if ((res = read_sector(drive, sector_buf, 0, 1)))
return res;
uint64_t cur_sector = last_sector + (offset / SECTOR_SIZE);
offset %= SECTOR_SIZE;
size_t sectors_count = 1 + ((offset + count) / SECTOR_SIZE);
while (sectors_count--) {
if (cur_sector != last_sector)
if ((res = read_sector(drive, sector_buf, cur_sector, 1)))
return res;
size_t limited_count = ((offset + count) > SECTOR_SIZE) ? (size_t)(SECTOR_SIZE - offset) : count;
memcpy(buffer, &sector_buf[offset], limited_count);
offset = (offset + limited_count) % SECTOR_SIZE;
buffer += limited_count;
cur_sector++;
}
return 0;
}