desparate to get ata reads to work
This commit is contained in:
@@ -26,11 +26,11 @@
|
|||||||
"-Wextra",
|
"-Wextra",
|
||||||
"-Ikernel",
|
"-Ikernel",
|
||||||
"-o",
|
"-o",
|
||||||
"obj/kernel/drivers/atapi/ata_stubs.o",
|
"obj/kernel/kmain.o",
|
||||||
"kernel/drivers/atapi/ata_stubs.S"
|
"kernel/kmain.c"
|
||||||
],
|
],
|
||||||
"directory": "/home/slinky/source/RockOS",
|
"directory": "/home/slinky/source/RockOS",
|
||||||
"file": "/home/slinky/source/RockOS/kernel/drivers/atapi/ata_stubs.S",
|
"file": "/home/slinky/source/RockOS/kernel/kmain.c",
|
||||||
"output": "/home/slinky/source/RockOS/obj/kernel/drivers/atapi/ata_stubs.o"
|
"output": "/home/slinky/source/RockOS/obj/kernel/kmain.o"
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
|
|||||||
@@ -66,3 +66,53 @@ inb:
|
|||||||
|
|
||||||
leave
|
leave
|
||||||
ret
|
ret
|
||||||
|
|
||||||
|
.global insw
|
||||||
|
insw:
|
||||||
|
push %ebp
|
||||||
|
mov %esp, %ebp
|
||||||
|
|
||||||
|
push %edi
|
||||||
|
push %ecx
|
||||||
|
push %edx
|
||||||
|
|
||||||
|
# 8(%ebp) addr
|
||||||
|
# 12(%ebp) buf
|
||||||
|
# 16(%ebp) sz
|
||||||
|
mov 8(%ebp), %edx
|
||||||
|
mov 12(%ebp), %edi
|
||||||
|
mov 16(%ebp), %ecx
|
||||||
|
|
||||||
|
cld
|
||||||
|
rep insw
|
||||||
|
|
||||||
|
pop %edx
|
||||||
|
pop %ecx
|
||||||
|
pop %edi
|
||||||
|
leave
|
||||||
|
ret
|
||||||
|
|
||||||
|
.global outsw
|
||||||
|
outsw:
|
||||||
|
push %ebp
|
||||||
|
mov %esp, %ebp
|
||||||
|
|
||||||
|
push %esi
|
||||||
|
push %ecx
|
||||||
|
push %edx
|
||||||
|
|
||||||
|
# 8(%ebp) address
|
||||||
|
# 12(%ebp) buf
|
||||||
|
# 16(%ebp) sz
|
||||||
|
mov 8(%ebp), %edx
|
||||||
|
mov 12(%ebp), %esi
|
||||||
|
mov 16(%ebp), %ecx
|
||||||
|
|
||||||
|
cld
|
||||||
|
rep outsw
|
||||||
|
|
||||||
|
pop %edx
|
||||||
|
pop %ecx
|
||||||
|
pop %esi
|
||||||
|
leave
|
||||||
|
ret
|
||||||
@@ -3,18 +3,22 @@
|
|||||||
#include "lib/print.h"
|
#include "lib/print.h"
|
||||||
|
|
||||||
#include <stddef.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_primary = 0;
|
||||||
uint8_t selected_drive_secondary = 0;
|
uint8_t selected_drive_secondary = 0;
|
||||||
|
|
||||||
uint8_t primary_master_info_buffer[512];
|
uint8_t ide_info_buffer[512];
|
||||||
uint8_t primary_slave_info_buffer[512];
|
|
||||||
uint8_t secondary_master_info_buffer[512];
|
|
||||||
uint8_t secondary_slave_info_buffer[512];
|
|
||||||
|
|
||||||
ide_device_t ide_devices[4];
|
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;
|
if ((sz % 2) != 0) return;
|
||||||
for (size_t i = 0; i < sz; i += 2) {
|
for (size_t i = 0; i < sz; i += 2) {
|
||||||
char tmp = buf[i];
|
char tmp = buf[i];
|
||||||
@@ -24,103 +28,238 @@ void fix_ata_string(char* buf, size_t sz) {
|
|||||||
buf[sz-1] = '\0';
|
buf[sz-1] = '\0';
|
||||||
}
|
}
|
||||||
|
|
||||||
void scan_drives() {
|
static uint16_t fetch_ctrl_reg(uint16_t bus) {
|
||||||
uint8_t type = ident_device(IDE_SECONDARY_IO_BASE, IDE_DRIVE_MASTER, secondary_master_info_buffer);
|
if (bus == IDE_PRIMARY_IO_BASE) return IDE_PRIMARY_CRTL_BASE;
|
||||||
if (type == 2) {
|
if (bus == IDE_SECONDARY_IO_BASE) return IDE_SECONDARY_CRTL_BASE;
|
||||||
ide_devices[0].bus = IDE_SECONDARY_IO_BASE;
|
return 0;
|
||||||
ide_devices[0].device = IDE_DRIVE_MASTER;
|
}
|
||||||
ide_devices->type = type;
|
|
||||||
|
|
||||||
atapi_identify_t* ident_result = (atapi_identify_t*)secondary_master_info_buffer;
|
static void ata_delay(uint16_t bus) {
|
||||||
ide_devices->ident_info_struct = (void*)ident_result;
|
uint16_t ctrl = fetch_ctrl_reg(bus);
|
||||||
|
for (int i = 0; i < 5; i++) {
|
||||||
kprintf("Assigned ATAPI master device on secondary bus to drive %d\n", 0);
|
inb(ctrl);
|
||||||
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");
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
extern void outb(uint8_t port, uint8_t data);
|
static int wait_not_bsy_drq(uint16_t bus) {
|
||||||
extern uint8_t inb(uint8_t port);
|
ata_delay(bus);
|
||||||
extern void atapi_wait(uint16_t port);
|
|
||||||
|
|
||||||
int read_atapi(uint16_t bus, uint8_t drive, uint32_t lba, uint32_t sectors, uint16_t* buf) {
|
int timeout = 10000;
|
||||||
volatile uint8_t read_cmd[12] = {0xA8, 0,
|
int error = 0;
|
||||||
(lba >> 0x18) & 0xFF, (lba >> 0x10) & 0xFF, (lba >> 0x08) & 0xFF,
|
while(timeout--) {
|
||||||
(lba >> 0x00) & 0xFF,
|
uint16_t ctrl = fetch_ctrl_reg(bus);
|
||||||
(sectors >> 0x18) & 0xFF, (sectors >> 0x10) & 0xFF, (sectors >> 0x08) & 0xFF,
|
uint8_t stat = inb(ctrl);
|
||||||
(sectors >> 0x00) & 0xFF,
|
if (stat & STATUS_REG_ERR) error = 1; break;
|
||||||
0, 0};
|
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)
|
if (timeout == 0 || error) {
|
||||||
return -1;
|
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);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return 0;
|
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);
|
||||||
|
}
|
||||||
@@ -44,7 +44,7 @@
|
|||||||
|
|
||||||
#define DRIVE_SELECT_REG_BN 0b00000111
|
#define DRIVE_SELECT_REG_BN 0b00000111
|
||||||
#define DRIVE_SELECT_REG_DRV 0b00010000
|
#define DRIVE_SELECT_REG_DRV 0b00010000
|
||||||
#define DRIVE_SELECT_REG_LBAA 0b01000000
|
#define DRIVE_SELECT_REG_LBA 0b01000000
|
||||||
|
|
||||||
#define STATUS_REG_ERR 0b00000001
|
#define STATUS_REG_ERR 0b00000001
|
||||||
#define STATUS_REG_IDX 0b00000010
|
#define STATUS_REG_IDX 0b00000010
|
||||||
@@ -55,8 +55,8 @@
|
|||||||
#define STATUS_REG_RDY 0b01000000
|
#define STATUS_REG_RDY 0b01000000
|
||||||
#define STATUS_REG_BSY 0b10000000
|
#define STATUS_REG_BSY 0b10000000
|
||||||
|
|
||||||
#define DEV_CTRL_REG_nIEN 0b00000001
|
#define DEV_CTRL_REG_nIEN 0b00000010
|
||||||
#define DEV_CTRL_REG_SRST 0b00000010
|
#define DEV_CTRL_REG_SRST 0b00000100
|
||||||
#define DEV_CTRL_REG_HOB 0b10000000
|
#define DEV_CTRL_REG_HOB 0b10000000
|
||||||
|
|
||||||
#define DRIVE_ADDR_REG_DS0 0b00000001
|
#define DRIVE_ADDR_REG_DS0 0b00000001
|
||||||
@@ -64,148 +64,61 @@
|
|||||||
#define DRIVE_ADDR_REG_HEAD 0b00111100
|
#define DRIVE_ADDR_REG_HEAD 0b00111100
|
||||||
#define DRIVE_ADDR_REG_WTG 0b01000000
|
#define DRIVE_ADDR_REG_WTG 0b01000000
|
||||||
|
|
||||||
#define ATAPI_CMD_IDENTIFY_PACKET 0xA1
|
#define ATA_CMD_READ_PIO 0x20
|
||||||
|
#define ATA_CMD_READ_PIO_EXT 0x24
|
||||||
|
#define ATA_CMD_READ_DMA 0xC8
|
||||||
|
#define ATA_CMD_READ_DMA_EXT 0x25
|
||||||
|
#define ATA_CMD_WRITE_PIO 0x30
|
||||||
|
#define ATA_CMD_WRITE_PIO_EXT 0x34
|
||||||
|
#define ATA_CMD_WRITE_DMA 0xCA
|
||||||
|
#define ATA_CMD_WRITE_DMA_EXT 0x35
|
||||||
|
#define ATA_CMD_CACHE_FLUSH 0xE7
|
||||||
|
#define ATA_CMD_CACHE_FLUSH_EXT 0xEA
|
||||||
|
#define ATA_CMD_PACKET 0xA0
|
||||||
|
#define ATA_CMD_IDENTIFY_PACKET 0xA1
|
||||||
|
#define ATA_CMD_IDENTIFY 0xEC
|
||||||
|
|
||||||
|
#define ATAPI_CMD_READ 0xA8
|
||||||
|
#define ATAPI_CMD_EJECT 0x1B
|
||||||
|
|
||||||
|
#define ATA_IDENT_DEVICETYPE 0
|
||||||
|
#define ATA_IDENT_CYLINDERS 2
|
||||||
|
#define ATA_IDENT_HEADS 6
|
||||||
|
#define ATA_IDENT_SECTORS 12
|
||||||
|
#define ATA_IDENT_SERIAL 20
|
||||||
|
#define ATA_IDENT_MODEL 54
|
||||||
|
#define ATA_IDENT_MODEL_SZ 40
|
||||||
|
#define ATA_IDENT_CAPABILITIES 98
|
||||||
|
#define ATA_IDENT_FIELDVALID 106
|
||||||
|
#define ATA_IDENT_MAX_LBA 120
|
||||||
|
#define ATA_IDENT_COMMANDSETS 164
|
||||||
|
#define ATA_IDENT_MAX_LBA_EXT 200
|
||||||
|
|
||||||
#ifndef __ASSEMBLER__
|
#ifndef __ASSEMBLER__
|
||||||
uint8_t ident_device(uint16_t bus, uint8_t drive_select, uint8_t* buffer);
|
typedef enum {
|
||||||
int read_atapi(uint16_t bus, uint8_t drive, uint32_t lba, uint32_t sectors, uint16_t* buf);
|
NO_DEVICE = 0,
|
||||||
void send_scsi_packet(uint16_t bus, void* cmd, size_t sz);
|
ATA,
|
||||||
void read_data_words(uint16_t bus, void* buf, size_t sz);
|
ATAPI,
|
||||||
|
} ide_dev_type;
|
||||||
|
|
||||||
|
|
||||||
typedef struct __attribute__((packed)) {
|
typedef struct __attribute__((packed)) {
|
||||||
uint16_t bus;
|
uint16_t bus;
|
||||||
uint8_t device;
|
uint8_t device;
|
||||||
uint8_t type;
|
ide_dev_type type;
|
||||||
void* ident_info_struct;
|
uint16_t sig;
|
||||||
|
uint16_t capabilities;
|
||||||
|
uint32_t command_set;
|
||||||
|
char* serial;
|
||||||
|
char* model;
|
||||||
|
uint32_t size;
|
||||||
} ide_device_t;
|
} ide_device_t;
|
||||||
|
|
||||||
typedef struct {
|
int select_drive(uint16_t bus, uint8_t drive);
|
||||||
// Word 0: General configuration bit-map
|
void ident_device(uint16_t bus, uint8_t drive, uint32_t slot);
|
||||||
struct {
|
void scan_drives();
|
||||||
uint16_t packet_size : 2; // 00=12 bytes, 01=16 bytes
|
|
||||||
uint16_t reserved1 : 3;
|
|
||||||
uint16_t drq_response : 2; // DRQ response microsecond timing
|
|
||||||
uint16_t device_type : 5; // Device type (e.g., 0x05 for CD/DVD-ROM)
|
|
||||||
uint16_t reserved2 : 1;
|
|
||||||
uint16_t removable_media : 1; // 1 = Removable media
|
|
||||||
uint16_t command_packet_type : 2; // 10 = ATAPI device
|
|
||||||
} general_config;
|
|
||||||
|
|
||||||
uint16_t logical_cylinders;
|
void ide_invoke_irq();
|
||||||
uint16_t specific_configuration; // Word 2: Specific configuration
|
|
||||||
uint16_t logical_heads; // Word 3: Obsolete / Reserved
|
|
||||||
uint16_t retired1[2]; // Words 4-5: Retired
|
|
||||||
|
|
||||||
uint16_t logical_sectors_per_track; // Word 6: Obsolete / Reserved
|
|
||||||
uint16_t reserved_vendor[3]; // Words 7-9: Vendor specific
|
|
||||||
|
|
||||||
char serial_number[20]; // Words 10-19: Serial number (ASCII, byte-swapped)
|
|
||||||
uint16_t retired2[2]; // Words 20-21: Retired
|
|
||||||
uint16_t obsolete1; // Word 22: Obsolete
|
|
||||||
|
|
||||||
char firmware_revision[8]; // Words 23-26: Firmware revision (ASCII, byte-swapped)
|
|
||||||
char model_number[40]; // Words 27-46: Model number (ASCII, byte-swapped)
|
|
||||||
|
|
||||||
uint16_t sectors_per_interrupt; // Word 47: Bits 7:0 = Max sectors per interrupt
|
|
||||||
uint16_t reserved3; // Word 48: Reserved
|
|
||||||
|
|
||||||
// Word 49: Capabilities
|
|
||||||
struct {
|
|
||||||
uint16_t reserved1 : 8;
|
|
||||||
uint16_t dma_supported : 1; // 1 = DMA supported
|
|
||||||
uint16_t lba_supported : 1; // 1 = LBA supported
|
|
||||||
uint16_t iordy_disable : 1; // 1 = IORDY can be disabled
|
|
||||||
uint16_t iordy_supported : 1; // 1 = IORDY supported
|
|
||||||
uint16_t reserved2 : 1;
|
|
||||||
uint16_t overlap_supported : 1; // 1 = Overlap operation supported
|
|
||||||
uint16_t reserved3 : 2;
|
|
||||||
} capabilities1;
|
|
||||||
|
|
||||||
uint16_t capabilities2; // Word 50: Capabilities bit 0 = Min MDMA cycle time info
|
|
||||||
uint16_t pio_data_transfer_timing;// Word 51: Obsolete
|
|
||||||
uint16_t dma_data_transfer_timing;// Word 52: Obsolete
|
|
||||||
|
|
||||||
// Word 53: Field validity
|
|
||||||
struct {
|
|
||||||
uint16_t fields_54_58_valid : 1; // 1 = Words 54-58 are valid
|
|
||||||
uint16_t fields_64_70_valid : 1; // 1 = Words 64-70 are valid
|
|
||||||
uint16_t field_88_valid : 1; // 1 = Word 88 is valid (Ultra DMA)
|
|
||||||
uint16_t reserved : 13;
|
|
||||||
} field_validity;
|
|
||||||
|
|
||||||
uint16_t obsolete2[5]; // Words 54-58: Obsolete
|
|
||||||
uint16_t multi_sector_setting; // Word 59: Multi-sector setting
|
|
||||||
uint32_t total_addressable_sectors; // Words 60-61: Total number of user addressable sectors (LBA28)
|
|
||||||
uint16_t single_word_dma_modes;
|
|
||||||
uint16_t multi_word_dma_modes;
|
|
||||||
|
|
||||||
struct {
|
|
||||||
uint16_t pio_mode_3 : 1;
|
|
||||||
uint16_t pio_mode_4 : 1;
|
|
||||||
uint16_t reserved : 14;
|
|
||||||
} advanced_pio_modes;
|
|
||||||
|
|
||||||
uint16_t min_mwdma_cycle_time;
|
|
||||||
uint16_t rec_mwdma_cycle_time;
|
|
||||||
uint16_t min_pio_cycle_time_no_flow;
|
|
||||||
uint16_t min_pio_cycle_time_iordy;
|
|
||||||
uint16_t reserved4[2];
|
|
||||||
|
|
||||||
uint16_t packet_release_time;
|
|
||||||
uint16_t service_clear_time;
|
|
||||||
uint16_t major_revision_number;
|
|
||||||
uint16_t minor_revision_number;
|
|
||||||
|
|
||||||
uint16_t queue_depth;
|
|
||||||
uint16_t reserved5[4];
|
|
||||||
|
|
||||||
uint16_t major_version;
|
|
||||||
uint16_t minor_version;
|
|
||||||
|
|
||||||
uint16_t command_sets_supported1;
|
|
||||||
uint16_t command_sets_supported2;
|
|
||||||
uint16_t command_sets_supported3;
|
|
||||||
|
|
||||||
uint16_t command_sets_enabled1;
|
|
||||||
uint16_t command_sets_enabled2;
|
|
||||||
uint16_t command_sets_enabled3;
|
|
||||||
|
|
||||||
uint16_t ultra_dma_modes;
|
|
||||||
uint16_t reserved_erase_time[2];
|
|
||||||
uint16_t current_apm_level;
|
|
||||||
uint16_t master_password_revision;
|
|
||||||
uint16_t hardware_reset_result;
|
|
||||||
uint16_t obsolete3;
|
|
||||||
uint16_t stream_minimum_request;
|
|
||||||
uint16_t streaming_transfer_delay;
|
|
||||||
uint16_t streaming_access_latency;
|
|
||||||
uint32_t streaming_perf_granularity;
|
|
||||||
|
|
||||||
uint64_t total_user_sectors_lba48;
|
|
||||||
uint16_t streaming_transfer_delay_block;
|
|
||||||
uint16_t reserved6;
|
|
||||||
|
|
||||||
struct {
|
|
||||||
uint16_t logical_sectors_per_physical : 4;
|
|
||||||
uint16_t reserved1 : 8;
|
|
||||||
uint16_t logical_sector_longer_than_256: 1;
|
|
||||||
uint16_t physical_sector_longer_than_logical: 1;
|
|
||||||
uint16_t validation_bits : 2;
|
|
||||||
} physical_sector_size;
|
|
||||||
|
|
||||||
uint16_t inter_seek_delay;
|
|
||||||
uint16_t world_wide_name[4];
|
|
||||||
uint16_t reserved7[8];
|
|
||||||
uint16_t command_submission_delay;
|
|
||||||
uint16_t reserved8[7];
|
|
||||||
uint16_t security_status;
|
|
||||||
uint16_t vendor_specific[31];
|
|
||||||
uint16_t cfa_power_mode;
|
|
||||||
uint16_t reserved9[15];
|
|
||||||
char media_serial_number[60];
|
|
||||||
uint16_t sct_command_transport;
|
|
||||||
uint16_t reserved10[49];
|
|
||||||
} __attribute__((packed)) atapi_identify_t;
|
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|||||||
@@ -1,227 +0,0 @@
|
|||||||
#include "drivers/atapi/ata.h"
|
|
||||||
|
|
||||||
.code32
|
|
||||||
|
|
||||||
.global ident_device
|
|
||||||
ident_device:
|
|
||||||
push %ebp
|
|
||||||
mov %esp, %ebp
|
|
||||||
|
|
||||||
push %ebx
|
|
||||||
push %esi
|
|
||||||
push %edi
|
|
||||||
push $0
|
|
||||||
|
|
||||||
mov 8(%ebp), %ebx # ebx = bus base address
|
|
||||||
mov 12(%ebp), %ecx # ecx = drive select mask
|
|
||||||
|
|
||||||
# drive select
|
|
||||||
mov %ebx, %edx
|
|
||||||
add $IO_DRIVE_SELECT_REG, %edx
|
|
||||||
mov %cl, %al
|
|
||||||
outb %al, (%dx)
|
|
||||||
|
|
||||||
mov %ebx, %edx
|
|
||||||
add $IO_STATUS_REG, %edx
|
|
||||||
.rept 15
|
|
||||||
inb (%dx), %al
|
|
||||||
.endr
|
|
||||||
|
|
||||||
xor %eax, %eax
|
|
||||||
# clear sector count and LBA
|
|
||||||
mov %ebx, %edx
|
|
||||||
add $IO_SECTOR_COUNT_REG, %edx
|
|
||||||
outb %al, (%dx)
|
|
||||||
|
|
||||||
mov %ebx, %edx
|
|
||||||
add $IO_LBA_LO_REG, %edx
|
|
||||||
outb %al, (%dx)
|
|
||||||
|
|
||||||
mov %ebx, %edx
|
|
||||||
add $IO_LBA_MID_REG, %edx
|
|
||||||
outb %al, (%dx)
|
|
||||||
|
|
||||||
mov %ebx, %edx
|
|
||||||
add $IO_LBA_HI_REG, %edx
|
|
||||||
outb %al, (%dx)
|
|
||||||
|
|
||||||
# send ident
|
|
||||||
mov %ebx, %edx
|
|
||||||
add $IO_CMD_REG, %edx
|
|
||||||
mov $0xEC, %al
|
|
||||||
outb %al, (%dx)
|
|
||||||
|
|
||||||
mov %ebx, %edx
|
|
||||||
add $IO_STATUS_REG, %edx
|
|
||||||
inb (%dx), %al
|
|
||||||
inb (%dx), %al
|
|
||||||
inb (%dx), %al
|
|
||||||
inb (%dx), %al
|
|
||||||
|
|
||||||
cmp $0xFF, %al
|
|
||||||
je not_found
|
|
||||||
test %al, %al
|
|
||||||
jz not_found
|
|
||||||
|
|
||||||
poll_status:
|
|
||||||
inb (%dx), %al
|
|
||||||
test $STATUS_REG_BSY, %al
|
|
||||||
jnz poll_status
|
|
||||||
|
|
||||||
# first check if this is an ATA device
|
|
||||||
check_ata:
|
|
||||||
mov %ebx, %edx
|
|
||||||
add $IO_LBA_MID_REG, %edx
|
|
||||||
inb (%dx), %al
|
|
||||||
test %al, %al
|
|
||||||
jnz check_atapi
|
|
||||||
|
|
||||||
mov %ebx, %edx
|
|
||||||
add $IO_LBA_HI_REG, %edx
|
|
||||||
inb (%dx), %al
|
|
||||||
test %al, %al
|
|
||||||
jnz check_atapi
|
|
||||||
is_ata:
|
|
||||||
mov $0x01, (%esp)
|
|
||||||
jmp poll_drq
|
|
||||||
|
|
||||||
check_atapi:
|
|
||||||
# check if atapi device
|
|
||||||
mov %ebx, %edx
|
|
||||||
add $IO_LBA_MID_REG, %edx
|
|
||||||
inb (%dx), %al
|
|
||||||
cmp $0x14, %al
|
|
||||||
jne not_found
|
|
||||||
|
|
||||||
mov %ebx, %edx
|
|
||||||
add $IO_LBA_HI_REG, %edx
|
|
||||||
inb (%dx), %al
|
|
||||||
cmp $0xEB, %al
|
|
||||||
jne not_found
|
|
||||||
is_atapi:
|
|
||||||
mov $0x02, (%esp)
|
|
||||||
mov %ebx, %edx
|
|
||||||
add $IO_CMD_REG, %edx
|
|
||||||
mov $(ATAPI_CMD_IDENTIFY_PACKET), %al
|
|
||||||
outb %al, (%dx)
|
|
||||||
|
|
||||||
mov %ebx, %edx
|
|
||||||
add $IO_STATUS_REG, %edx
|
|
||||||
inb (%dx), %al
|
|
||||||
inb (%dx), %al
|
|
||||||
inb (%dx), %al
|
|
||||||
inb (%dx), %al
|
|
||||||
1:
|
|
||||||
inb (%dx), %al
|
|
||||||
test $STATUS_REG_BSY, %al
|
|
||||||
jnz 1b
|
|
||||||
|
|
||||||
|
|
||||||
poll_drq:
|
|
||||||
mov %ebx, %edx
|
|
||||||
add $IO_STATUS_REG, %edx
|
|
||||||
inb (%dx), %al
|
|
||||||
|
|
||||||
test $STATUS_REG_BSY, %al
|
|
||||||
jnz poll_drq
|
|
||||||
|
|
||||||
test $STATUS_REG_ERR, %al
|
|
||||||
jnz error
|
|
||||||
|
|
||||||
test $STATUS_REG_DRQ, %al
|
|
||||||
jz poll_drq
|
|
||||||
|
|
||||||
drq_set:
|
|
||||||
mov %ebx, %edx
|
|
||||||
add $IO_DATA_REG, %edx
|
|
||||||
|
|
||||||
mov 16(%ebp), %edi
|
|
||||||
mov $256, %ecx
|
|
||||||
cld
|
|
||||||
read_data:
|
|
||||||
inw (%dx), %ax
|
|
||||||
mov %ax, (%edi)
|
|
||||||
add $2, %edi
|
|
||||||
loop read_data
|
|
||||||
jmp done
|
|
||||||
|
|
||||||
not_found:
|
|
||||||
error:
|
|
||||||
mov $0x0, (%esp)
|
|
||||||
done:
|
|
||||||
pop %eax
|
|
||||||
pop %edi
|
|
||||||
pop %esi
|
|
||||||
pop %ebx
|
|
||||||
leave
|
|
||||||
ret
|
|
||||||
|
|
||||||
.global atapi_wait
|
|
||||||
atapi_wait:
|
|
||||||
push %ebp
|
|
||||||
mov %esp, %ebp
|
|
||||||
|
|
||||||
# 8(%ebp) bus
|
|
||||||
mov 8(%ebp), %edx
|
|
||||||
|
|
||||||
add $IO_STATUS_REG, %edx
|
|
||||||
inb (%dx), %al
|
|
||||||
inb (%dx), %al
|
|
||||||
inb (%dx), %al
|
|
||||||
inb (%dx), %al
|
|
||||||
|
|
||||||
pop %edx
|
|
||||||
leave
|
|
||||||
ret
|
|
||||||
|
|
||||||
.global send_scsi_packet
|
|
||||||
send_scsi_packet:
|
|
||||||
push %ebp
|
|
||||||
mov %esp, %ebp
|
|
||||||
|
|
||||||
push %esi
|
|
||||||
push %ecx
|
|
||||||
push %edx
|
|
||||||
|
|
||||||
# 8(%ebp) bus
|
|
||||||
# 12(%ebp) cmd
|
|
||||||
# 16(%ebp) sz
|
|
||||||
mov 8(%ebp), %edx
|
|
||||||
add $IO_DATA_REG, %edx
|
|
||||||
mov 12(%ebp), %esi
|
|
||||||
mov 16(%ebp), %ecx
|
|
||||||
|
|
||||||
cld
|
|
||||||
rep outsw
|
|
||||||
|
|
||||||
pop %edx
|
|
||||||
pop %ecx
|
|
||||||
pop %esi
|
|
||||||
leave
|
|
||||||
ret
|
|
||||||
|
|
||||||
.global read_data_words
|
|
||||||
read_data_words:
|
|
||||||
push %ebp
|
|
||||||
mov %esp, %ebp
|
|
||||||
|
|
||||||
push %edi
|
|
||||||
push %ecx
|
|
||||||
push %edx
|
|
||||||
|
|
||||||
# 8(%ebp) bus
|
|
||||||
# 12(%ebp) buf
|
|
||||||
# 16(%ebp) sz
|
|
||||||
mov 8(%ebp), %edx
|
|
||||||
add $IO_DATA_REG, %edx
|
|
||||||
mov 12(%ebp), %edi
|
|
||||||
mov 16(%ebp), %ecx
|
|
||||||
|
|
||||||
cld
|
|
||||||
rep insw
|
|
||||||
|
|
||||||
pop %edx
|
|
||||||
pop %ecx
|
|
||||||
pop %edi
|
|
||||||
leave
|
|
||||||
ret
|
|
||||||
@@ -28,9 +28,9 @@ init_pic:
|
|||||||
outb %al, $PIC2_DATA
|
outb %al, $PIC2_DATA
|
||||||
|
|
||||||
# mask
|
# mask
|
||||||
mov $0xFC, %al
|
mov $0xF8, %al
|
||||||
outb %al, $PIC1_DATA
|
outb %al, $PIC1_DATA
|
||||||
mov $0xFF, %al
|
mov $0x3F, %al
|
||||||
outb %al, $PIC2_DATA
|
outb %al, $PIC2_DATA
|
||||||
|
|
||||||
ret
|
ret
|
||||||
|
|||||||
@@ -63,6 +63,13 @@ void vga_putchar(char c)
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (c == '\t') {
|
||||||
|
for (int i = 0; i < 4; i++) {
|
||||||
|
vga_putchar(' ');
|
||||||
|
}
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
const size_t index = terminal_row * VGA_WIDTH + terminal_column;
|
const size_t index = terminal_row * VGA_WIDTH + terminal_column;
|
||||||
terminal_buffer[index] = vga_entry(c, terminal_color);
|
terminal_buffer[index] = vga_entry(c, terminal_color);
|
||||||
if (++terminal_column == VGA_WIDTH) {
|
if (++terminal_column == VGA_WIDTH) {
|
||||||
|
|||||||
@@ -46,3 +46,6 @@ ISR_ERRCODE 14 # Page Fault (Has Error Code!)
|
|||||||
|
|
||||||
ISR_NOERRCODE 32 # IRQ0 - Timer
|
ISR_NOERRCODE 32 # IRQ0 - Timer
|
||||||
ISR_NOERRCODE 33 # IRQ1 - Keyboard
|
ISR_NOERRCODE 33 # IRQ1 - Keyboard
|
||||||
|
|
||||||
|
ISR_NOERRCODE 46 # IRQ14
|
||||||
|
ISR_NOERRCODE 47 # IRQ15
|
||||||
@@ -1,6 +1,8 @@
|
|||||||
#include "lib/print.h"
|
#include "lib/print.h"
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
|
|
||||||
|
#include "drivers/atapi/ata.h"
|
||||||
|
|
||||||
extern void send_eoi_master(void);
|
extern void send_eoi_master(void);
|
||||||
extern void send_eoi_slave(void);
|
extern void send_eoi_slave(void);
|
||||||
extern uint8_t read_isr_master(void);
|
extern uint8_t read_isr_master(void);
|
||||||
@@ -68,6 +70,18 @@ void common_interrupt_handler(struct registers *args) {
|
|||||||
send_eoi_master();
|
send_eoi_master();
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
case 46: { // IDE PRIMARY
|
||||||
|
kprintf("got interrupt\n");
|
||||||
|
ide_invoke_irq();
|
||||||
|
send_eoi_slave();
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case 47: { // IDE SLAVE
|
||||||
|
kprintf("got interrupt\n");
|
||||||
|
ide_invoke_irq();
|
||||||
|
send_eoi_slave();
|
||||||
|
break;
|
||||||
|
}
|
||||||
default:
|
default:
|
||||||
kprintf("Received unhandled interrupt: %d\n", args->int_no);
|
kprintf("Received unhandled interrupt: %d\n", args->int_no);
|
||||||
return;
|
return;
|
||||||
|
|||||||
Reference in New Issue
Block a user