we can reaad CDROMS
This commit is contained in:
239
kernel/drivers/ide/ide.c
Normal file
239
kernel/drivers/ide/ide.c
Normal file
@@ -0,0 +1,239 @@
|
||||
#include "ide.h"
|
||||
|
||||
#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 ide_info_buffer[512];
|
||||
|
||||
ide_device_t ide_devices[4];
|
||||
|
||||
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];
|
||||
buf[i] = buf[i + 1];
|
||||
buf[i + 1] = tmp;
|
||||
}
|
||||
buf[sz-1] = '\0';
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
static void ata_delay(uint16_t bus) {
|
||||
uint16_t ctrl = fetch_ctrl_reg(bus);
|
||||
for (int i = 0; i < 5; i++) {
|
||||
inb(ctrl);
|
||||
}
|
||||
}
|
||||
|
||||
static int wait_not_bsy_drq(uint16_t bus) {
|
||||
ata_delay(bus);
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
if (timeout == 0 || error) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static 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;
|
||||
}
|
||||
|
||||
static 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);
|
||||
}
|
||||
}
|
||||
|
||||
static int atapi_read_sector(ide_device_t* dev, uint32_t lba, uint32_t sectors, void* buf) {
|
||||
ide_device_t drive = *dev;
|
||||
|
||||
// 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 0;
|
||||
}
|
||||
|
||||
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) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
outsw(drive.bus + IO_DATA_REG, pkt, 6);
|
||||
|
||||
uint32_t ptr = (uint32_t)buf;
|
||||
for (int i = 0; i < sectors; i++) {
|
||||
err = wait_not_bsy_drq(drive.bus);
|
||||
if (err) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
insw(drive.bus + IO_DATA_REG, (void*)ptr, 1024);
|
||||
ptr += 2048;
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
void scan_drives() {
|
||||
ident_device(IDE_SECONDARY_IO_BASE, IDE_DRIVE_MASTER, 0);
|
||||
}
|
||||
|
||||
ide_device_t* get_device(uint8_t slot) {
|
||||
if (slot > 3) return NULL;
|
||||
return &ide_devices[slot];
|
||||
}
|
||||
|
||||
int ide_read_blks(ide_device_t* dev, uint32_t lba, uint32_t sectors, void* buf) {
|
||||
if (dev->type == ATAPI) {
|
||||
return atapi_read_sector(dev, lba, sectors, buf);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
123
kernel/drivers/ide/ide.h
Normal file
123
kernel/drivers/ide/ide.h
Normal file
@@ -0,0 +1,123 @@
|
||||
#ifndef KATA_H
|
||||
#define KATA_H
|
||||
|
||||
#ifndef __ASSEMBLER__
|
||||
#include <stdint.h>
|
||||
#include <stddef.h>
|
||||
#endif
|
||||
|
||||
#define IDE_PRIMARY_IO_BASE 0x01F0
|
||||
#define IDE_PRIMARY_CRTL_BASE 0x03F6
|
||||
|
||||
#define IDE_SECONDARY_IO_BASE 0x0170
|
||||
#define IDE_SECONDARY_CRTL_BASE 0x0376
|
||||
|
||||
#define IDE_DRIVE_MASTER 0xA0
|
||||
#define IDE_DRIVE_SLAVE 0xB0
|
||||
|
||||
#define IO_DATA_REG 0x0000
|
||||
#define IO_ERROR_REG 0x0001
|
||||
#define IO_FEATURE_REG 0x0001
|
||||
#define IO_SECTOR_COUNT_REG 0x0002
|
||||
#define IO_SECTOR_NUM_REG 0x0003
|
||||
#define IO_LBA_LO_REG 0x0003
|
||||
#define IO_CYL_LO_REG 0x0004
|
||||
#define IO_LBA_MID_REG 0x0004
|
||||
#define IO_LBA_HI_REG 0x0005
|
||||
#define IO_CYL_HI_REG 0x0005
|
||||
#define IO_DRIVE_SELECT_REG 0x0006
|
||||
#define IO_STATUS_REG 0x0007
|
||||
#define IO_CMD_REG 0x0007
|
||||
|
||||
#define CTRL_ALT_STATUS_REG 0x00
|
||||
#define CTRL_DEV_CTRL_REG 0x00
|
||||
#define CTRL_DRIVE_ADDR_REG 0x01
|
||||
|
||||
#define ERROR_REG_AMNF 0b00000001 // Address mark not found
|
||||
#define ERROR_REG_TKZNF 0b00000010 // Track zero not found
|
||||
#define ERROR_REG_ABRT 0b00000100 // Aborted command
|
||||
#define ERROR_REG_MCR 0b00001000 // Media change request
|
||||
#define ERROR_REG_IDNF 0b00010000 // ID not found
|
||||
#define ERROR_REG_MC 0b00100000 // Media changed
|
||||
#define ERROR_REG_UNC 0b01000000 // Uncorrectable data error
|
||||
#define ERROR_REG_BBK 0b10000000 // Bad Block detected
|
||||
|
||||
#define DRIVE_SELECT_REG_BN 0b00000111
|
||||
#define DRIVE_SELECT_REG_DRV 0b00010000
|
||||
#define DRIVE_SELECT_REG_LBA 0b01000000
|
||||
|
||||
#define STATUS_REG_ERR 0b00000001
|
||||
#define STATUS_REG_IDX 0b00000010
|
||||
#define STATUS_REG_CORR 0b00000100
|
||||
#define STATUS_REG_DRQ 0b00001000
|
||||
#define STATUS_REG_SRV 0b00010000
|
||||
#define STATUS_REG_DF 0b00100000
|
||||
#define STATUS_REG_RDY 0b01000000
|
||||
#define STATUS_REG_BSY 0b10000000
|
||||
|
||||
#define DEV_CTRL_REG_nIEN 0b00000010
|
||||
#define DEV_CTRL_REG_SRST 0b00000100
|
||||
#define DEV_CTRL_REG_HOB 0b10000000
|
||||
|
||||
#define DRIVE_ADDR_REG_DS0 0b00000001
|
||||
#define DRIVE_ADDR_REG_DS1 0b00000010
|
||||
#define DRIVE_ADDR_REG_HEAD 0b00111100
|
||||
#define DRIVE_ADDR_REG_WTG 0b01000000
|
||||
|
||||
#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__
|
||||
typedef enum {
|
||||
NO_DEVICE = 0,
|
||||
ATA,
|
||||
ATAPI,
|
||||
} ide_dev_type;
|
||||
|
||||
|
||||
typedef struct __attribute__((packed)) {
|
||||
uint16_t bus;
|
||||
uint8_t device;
|
||||
ide_dev_type type;
|
||||
uint16_t sig;
|
||||
uint16_t capabilities;
|
||||
uint32_t command_set;
|
||||
char* serial;
|
||||
char* model;
|
||||
uint32_t size;
|
||||
} ide_device_t;
|
||||
|
||||
void scan_drives();
|
||||
ide_device_t* get_device(uint8_t slot);
|
||||
int ide_read_blks(ide_device_t* dev, uint32_t lba, uint32_t sectors, void* buf);
|
||||
|
||||
#endif
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user