desparate to get ata reads to work

This commit is contained in:
2026-06-23 23:19:49 -05:00
parent a82a7aa1a4
commit ae4b6863bf
9 changed files with 364 additions and 465 deletions

View File

@@ -3,18 +3,22 @@
#include "lib/print.h"
#include <stddef.h>
#include <stdint.h>
extern uint8_t inb(uint16_t port);
extern void outb(uint16_t port, uint8_t data);
extern void insw(uint32_t address, void* buf, uint32_t sz);
extern void outsw(uint32_t address, void* buf, uint32_t sz);
uint8_t selected_drive_primary = 0;
uint8_t selected_drive_secondary = 0;
uint8_t primary_master_info_buffer[512];
uint8_t primary_slave_info_buffer[512];
uint8_t secondary_master_info_buffer[512];
uint8_t secondary_slave_info_buffer[512];
uint8_t ide_info_buffer[512];
ide_device_t ide_devices[4];
void fix_ata_string(char* buf, size_t sz) {
static void fix_ata_string(char* buf, size_t sz) {
if ((sz % 2) != 0) return;
for (size_t i = 0; i < sz; i += 2) {
char tmp = buf[i];
@@ -24,103 +28,238 @@ void fix_ata_string(char* buf, size_t sz) {
buf[sz-1] = '\0';
}
void scan_drives() {
uint8_t type = ident_device(IDE_SECONDARY_IO_BASE, IDE_DRIVE_MASTER, secondary_master_info_buffer);
if (type == 2) {
ide_devices[0].bus = IDE_SECONDARY_IO_BASE;
ide_devices[0].device = IDE_DRIVE_MASTER;
ide_devices->type = type;
static uint16_t fetch_ctrl_reg(uint16_t bus) {
if (bus == IDE_PRIMARY_IO_BASE) return IDE_PRIMARY_CRTL_BASE;
if (bus == IDE_SECONDARY_IO_BASE) return IDE_SECONDARY_CRTL_BASE;
return 0;
}
atapi_identify_t* ident_result = (atapi_identify_t*)secondary_master_info_buffer;
ide_devices->ident_info_struct = (void*)ident_result;
kprintf("Assigned ATAPI master device on secondary bus to drive %d\n", 0);
uint8_t buf[2048];
int rtn = read_atapi(IDE_SECONDARY_IO_BASE, IDE_DRIVE_MASTER, 0x10, 1, (uint16_t*)buf);
if (rtn < 0) {
if (rtn == -1) {
kprintf("Read error, failed to send command\n");
}
if (rtn == -2) {
kprintf("Read error, failed to read sector\n");
}
kprintf("Read error\n");
return;
}
kprintf("Read first sector of drive\n");
if (buf[0] != 0x01) {
kprintf("First byte not valid for ISO drive\n");
return;
}
if (buf[1] != 'C' || buf[2] != 'D') {
kprintf("Magic bytes are invalid for ISO drive\n");
return;
}
kprintf("ISO filesystem detected on drive\n");
static void ata_delay(uint16_t bus) {
uint16_t ctrl = fetch_ctrl_reg(bus);
for (int i = 0; i < 5; i++) {
inb(ctrl);
}
}
extern void outb(uint8_t port, uint8_t data);
extern uint8_t inb(uint8_t port);
extern void atapi_wait(uint16_t port);
static int wait_not_bsy_drq(uint16_t bus) {
ata_delay(bus);
int read_atapi(uint16_t bus, uint8_t drive, uint32_t lba, uint32_t sectors, uint16_t* buf) {
volatile uint8_t read_cmd[12] = {0xA8, 0,
(lba >> 0x18) & 0xFF, (lba >> 0x10) & 0xFF, (lba >> 0x08) & 0xFF,
(lba >> 0x00) & 0xFF,
(sectors >> 0x18) & 0xFF, (sectors >> 0x10) & 0xFF, (sectors >> 0x08) & 0xFF,
(sectors >> 0x00) & 0xFF,
0, 0};
int timeout = 10000;
int error = 0;
while(timeout--) {
uint16_t ctrl = fetch_ctrl_reg(bus);
uint8_t stat = inb(ctrl);
if (stat & STATUS_REG_ERR) error = 1; break;
if (!(stat & STATUS_REG_BSY)) break;
}
outb(bus + IO_DRIVE_SELECT_REG, drive); // select drive
atapi_wait(bus);
outb(bus + IO_FEATURE_REG, 0x00);
outb(bus + IO_LBA_MID_REG, 2048 & 0xFF);
outb(bus + IO_LBA_HI_REG, 2048 >> 8);
outb(bus + IO_CMD_REG, 0xA0);
atapi_wait(bus);
atapi_wait(bus);
while (1) {
kprintf("waiting bsy\n");
uint8_t status = inb(bus + IO_STATUS_REG);
if (status & STATUS_REG_BSY) {
atapi_wait(bus);
continue;
}
if (status & STATUS_REG_ERR)
return -1;
if (status & STATUS_REG_DRQ)
break;
atapi_wait(bus);
}
send_scsi_packet(bus, (void*)read_cmd, 6);
for (uint32_t i = 0; i < sectors; i++) {
while (1) {
uint8_t status = inb(bus + IO_STATUS_REG);
if (status & STATUS_REG_ERR)
return -2;
if (!(status & STATUS_REG_BSY) && (status & STATUS_REG_DRQ))
break;
atapi_wait(bus);
}
int bytes = inb(bus + IO_LBA_HI_REG) << 8 | inb(bus + IO_LBA_MID_REG);
int words = bytes / 2;
read_data_words(bus, ((uint8_t*)buf + i * 0x800), words);
if (timeout == 0 || error) {
return 1;
}
return 0;
}
static int ide_irq_invoked = 0;
static void ide_irq_wait() {
while (!ide_irq_invoked)
;
ide_irq_invoked = 0;
}
void ide_invoke_irq() {
ide_irq_invoked = 1;
}
int select_drive(uint16_t bus, uint8_t drive) {
if (bus == IDE_PRIMARY_IO_BASE && selected_drive_primary == drive) {
return 1;
}
if (bus == IDE_SECONDARY_IO_BASE && selected_drive_secondary == drive) {
return 1;
}
uint16_t ctrl_reg = fetch_ctrl_reg(bus);
if (ctrl_reg == 0) {
return 0;
}
// wait for bsy and drq clear
while (1) {
uint8_t status = inb(ctrl_reg + CTRL_ALT_STATUS_REG);
if (!(status & STATUS_REG_BSY) && !(status & STATUS_REG_DRQ)) {
break;
}
}
outb(bus + IO_DRIVE_SELECT_REG, drive);
ata_delay(bus);
uint8_t status = inb(ctrl_reg + CTRL_ALT_STATUS_REG);
if (status & STATUS_REG_ERR) {
return 0;
}
return 1;
}
void ident_device(uint16_t bus, uint8_t drive, uint32_t slot) {
ide_dev_type type = NO_DEVICE;
uint8_t stat = inb(bus + IO_STATUS_REG);
if (stat == 0xFF) {
kprintf("no device on bus 0x%x\n", bus);
return;
}
if (!select_drive(bus, drive)) {
kprintf("failed to select drive 0x%x on bus 0x%x\n", bus, drive);
return;
}
outb(bus + IO_SECTOR_COUNT_REG, 0x00);
outb(bus + IO_LBA_LO_REG, 0x00);
outb(bus + IO_LBA_MID_REG, 0x00);
outb(bus + IO_LBA_HI_REG, 0x00);
// ident ata
outb(bus + IO_CMD_REG, ATA_CMD_IDENTIFY);
ata_delay(bus);
stat = inb(bus + IO_STATUS_REG);
if (stat == 0x00) {
kprintf("no drive 0x%x on bus 0x%x\n", drive, bus);
return;
}
int not_ata = 0;
int timeout = 10000;
while(timeout--) {
stat = inb(bus + IO_STATUS_REG);
if (stat & STATUS_REG_ERR) not_ata = 1; break;
if (!(stat & STATUS_REG_BSY) && (stat & STATUS_REG_DRQ)) type = ATA; break;
}
if (timeout == 0) {
kprintf("drive 0x%x ident timed out on bus 0x%x\n", drive, bus);
return;
}
if (not_ata) {
uint8_t mid = inb(bus + IO_LBA_MID_REG);
uint8_t hi = inb(bus + IO_LBA_HI_REG);
if (mid != 0x14 || hi != 0xEB) {
return;
}
type = ATAPI;
// ident atapi
outb(bus + IO_CMD_REG, ATA_CMD_IDENTIFY_PACKET);
ata_delay(bus);
}
insw(bus + IO_DATA_REG, ide_info_buffer, 256);
ide_devices[slot].bus = bus;
ide_devices[slot].device = drive;
ide_devices[slot].type = type;
ide_devices[slot].capabilities = *(uint16_t*)(ide_info_buffer + ATA_IDENT_CAPABILITIES);
ide_devices[slot].sig = *(uint16_t*)(ide_info_buffer + ATA_IDENT_DEVICETYPE);
ide_devices[slot].command_set = *(uint32_t*)(ide_info_buffer + ATA_IDENT_COMMANDSETS);
if (ide_devices[slot].command_set & (1 << 26)) {
ide_devices[slot].size = *(uint32_t*)(ide_info_buffer + ATA_IDENT_MAX_LBA_EXT);
} else {
ide_devices[slot].size = *(uint32_t*)(ide_info_buffer + ATA_IDENT_MAX_LBA);
}
char* serial_buf = (char*)(ide_info_buffer + ATA_IDENT_SERIAL);
fix_ata_string(serial_buf, 8);
// ide_devices[slot].serial = serial_buf;
char* model_buf = (char*)(ide_info_buffer + ATA_IDENT_MODEL);
fix_ata_string(model_buf, ATA_IDENT_MODEL_SZ);
// ide_devices[slot].model = model_buf;
kprintf("Found device 0x%x on bus 0x%x:\n", drive, bus);
kprintf("\tModel Number: %s\n", model_buf);
kprintf("\tSerial Number: %s\n", serial_buf);
kprintf("\tSize %dB\n", *(uint32_t*)(ide_info_buffer + ATA_IDENT_MAX_LBA));
}
uint8_t temp_sector_buf[2048];
void atapi_read_sector(uint8_t drive_num, uint32_t lba, uint32_t sectors, void* buf) {
ide_device_t drive = ide_devices[drive_num];
// enable irqs
uint16_t ctrl_reg = fetch_ctrl_reg(drive.bus);
outb(ctrl_reg, 0x00);
uint8_t pkt[12];
pkt[ 0] = ATAPI_CMD_READ;
pkt[ 1] = 0x0;
pkt[ 2] = (lba >> 24) & 0xFF;
pkt[ 3] = (lba >> 16) & 0xFF;
pkt[ 4] = (lba >> 8) & 0xFF;
pkt[ 5] = (lba >> 0) & 0xFF;
pkt[ 6] = 0x0;
pkt[ 7] = 0x0;
pkt[ 8] = 0x0;
pkt[ 9] = sectors;
pkt[10] = 0x0;
pkt[11] = 0x0;
if (!select_drive(drive.bus, drive.device)) {
return;
}
outb(drive.bus + IO_FEATURE_REG, 0x00); // PIO mode
uint32_t words = 1024;
outb(drive.bus + IO_LBA_MID_REG, (words * 2) & 0xFF);
outb(drive.bus + IO_LBA_HI_REG, (words * 2) >> 8);
outb(drive.bus + IO_CMD_REG, ATA_CMD_PACKET);
// poll for drq
int err = wait_not_bsy_drq(drive.bus);
if (err) {
kprintf("failed sending ata packet command\n");
return;
}
outsw(drive.bus + IO_DATA_REG, pkt, 6);
uint32_t ptr = (uint32_t)buf;
for (int i = 0; i < sectors; i++) {
ide_irq_wait();
err = wait_not_bsy_drq(drive.bus);
if (err) {
kprintf("failed reading atapi sector\n");
return;
}
insw(drive.bus + IO_DATA_REG, (void*)ptr, 1024);
ptr += 2048;
}
ide_irq_wait();
while(inb(drive.bus + IO_STATUS_REG) & (STATUS_REG_BSY | STATUS_REG_DRQ));
kprintf("read %d sectors from drive %d\n", sectors, drive_num);
}
void scan_drives() {
ident_device(IDE_SECONDARY_IO_BASE, IDE_DRIVE_MASTER, 0);
ide_device_t boot_drive = ide_devices[0];
if (!(boot_drive.capabilities & 0x200)) {
kprintf("boot drive does not support LBA addressing");
return;
}
atapi_read_sector(0, 16, 1, temp_sector_buf);
}