From e83ca6c0b7868d1711c52a934978a7bd49e53a0b Mon Sep 17 00:00:00 2001 From: Kevin Lange Date: Tue, 25 Oct 2011 21:39:55 -0500 Subject: [PATCH] [hack] IDE disk reads to HDB Run read-disk from the kernel shell to read the first sector of -hdb, run write-disk to write the sequence "DCDCDCDCDC..." to the first sector. --- kernel/core/shell.c | 49 ++++++++++++++++++++ kernel/include/ata.h | 107 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 156 insertions(+) create mode 100644 kernel/include/ata.h diff --git a/kernel/core/shell.c b/kernel/core/shell.c index 54a93bc0..c45e79d4 100644 --- a/kernel/core/shell.c +++ b/kernel/core/shell.c @@ -21,6 +21,7 @@ #include #include #include +#include void start_shell() { @@ -208,6 +209,54 @@ start_shell() { free(allocs); free(sizes); kprintf("Testing complete.\n"); + } else if (!strcmp(cmd, "read-disk")) { + __asm__ __volatile__ ("cli"); + char buf[512]; + uint32_t lba = 0x0; + uint16_t bus = 0x1F0; + outportb(bus + 0x01, 0x00); + outportb(bus + 0x03, lba & 0xff); + outportb(bus + 0x04, (lba >> 8) & 0xff); + outportb(bus + 0x05, (lba >> 16)& 0xff); + outportb(bus + 0x06, 0xf0 | (lba >> 24 & 0xf)); + outportb(bus + 0x07, ATA_CMD_READ_PIO); + outportb(bus + 0x02, 1); + uint8_t status = 0; + while ((status = inportb(bus + 0x07)) & 0x80) { + } + for (volatile int i = 0; i < 512; i += 2) + { + uint16_t s = inports(0x1f0); + buf[i] = s & 0xFF; + buf[i+1] = (s & 0xFF00) >> 8; + } + outportb(0x177, 0xe7); + for (uint16_t i = 0; i < 512; ++i) { + kprintf("%c", (int)(buf[i] & 0xFF)); + } + __asm__ __volatile__ ("sti"); + } else if (!strcmp(cmd, "write-disk")) { + __asm__ __volatile__ ("cli"); + uint32_t lba = 0x0; + uint16_t bus = 0x1F0; + outportb(bus + 0x01, 0x00); + outportb(bus + 0x03, lba & 0xff); + outportb(bus + 0x04, (lba >> 8) & 0xff); + outportb(bus + 0x05, (lba >> 16)& 0xff); + outportb(bus + 0x06, 0xf0 | (lba >> 24 & 0xf)); + outportb(bus + 0x07, ATA_CMD_WRITE_PIO); + uint8_t status = 0; + while ((status = inportb(bus + 0x07)) & 0x80) { + } + for (volatile int i = 0; i < 512; i+=2) + { + outportb(bus + 0x02, 0); + outports(bus, 0x4344); + } + outportb(bus + 0x07, ATA_CMD_CACHE_FLUSH); + outportb(bus + 0x02, 0); + outportb(0x177, 0xe7); + __asm__ __volatile__ ("sti"); } else { /* Alright, here we go */ char * filename = malloc(sizeof(char) * 1024); diff --git a/kernel/include/ata.h b/kernel/include/ata.h new file mode 100644 index 00000000..ef112f1e --- /dev/null +++ b/kernel/include/ata.h @@ -0,0 +1,107 @@ +/* vim: noexpandtab tabstop=4 shiftwidth=4 + * + * Values for ATA / PATA devices + */ + +#ifndef ATA_H +#define ATA_H + +#define ATA_SR_BSY 0x80 +#define ATA_SR_DRDY 0x40 +#define ATA_SR_DF 0x20 +#define ATA_SR_DSC 0x10 +#define ATA_SR_DRQ 0x08 +#define ATA_SR_CORR 0x04 +#define ATA_SR_IDX 0x02 +#define ATA_SR_ERR 0x01 + +#define ATA_ER_BBK 0x80 +#define ATA_ER_UNC 0x40 +#define ATA_ER_MC 0x20 +#define ATA_ER_IDNF 0x10 +#define ATA_ER_MCR 0x08 +#define ATA_ER_ABRT 0x04 +#define ATA_ER_TK0NF 0x02 +#define ATA_ER_AMNF 0x01 + +#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_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 + +#define IDE_ATA 0x00 +#define IDE_ATAPI 0x01 + +#define ATA_MASTER 0x00 +#define ATA_SLAVE 0x01 + +#define ATA_REG_DATA 0x00 +#define ATA_REG_ERROR 0x01 +#define ATA_REG_FEATURES 0x01 +#define ATA_REG_SECCOUNT0 0x02 +#define ATA_REG_LBA0 0x03 +#define ATA_REG_LBA1 0x04 +#define ATA_REG_LBA2 0x05 +#define ATA_REG_HDDEVSEL 0x06 +#define ATA_REG_COMMAND 0x07 +#define ATA_REG_STATUS 0x07 +#define ATA_REG_SECCOUNT1 0x08 +#define ATA_REG_LBA3 0x09 +#define ATA_REG_LBA4 0x0A +#define ATA_REG_LBA5 0x0B +#define ATA_REG_CONTROL 0x0C +#define ATA_REG_ALTSTATUS 0x0C +#define ATA_REG_DEVADDRESS 0x0D + +// Channels: +#define ATA_PRIMARY 0x00 +#define ATA_SECONDARY 0x01 + +// Directions: +#define ATA_READ 0x00 +#define ATA_WRITE 0x01 + +typedef struct { + uint16_t base; + uint16_t ctrl; + uint16_t bmide; + uint16_t nien; +} ide_channel_regs_t; + +typedef struct { + uint8_t reserved; + uint8_t channel; + uint8_t drive; + uint16_t type; + uint16_t signature; + uint16_t capabilities; + uint32_t command_sets; + uint32_t size; + uint8_t model[41]; +} ide_device_t; + +#endif