From a0aa45ea6f4027dd4080cf1456a2fc5d9e94f87b Mon Sep 17 00:00:00 2001 From: Jake Mannens Date: Mon, 16 Jul 2018 17:36:59 +1000 Subject: Included the necessary stdint.h in kernel/hd.h. That header file may now be used freely throughout the kernel. --- kernel/fs/hd.c | 132 --------------------------------------------------------- kernel/hd.c | 132 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ kernel/kmain.c | 31 +------------- 3 files changed, 133 insertions(+), 162 deletions(-) delete mode 100644 kernel/fs/hd.c create mode 100644 kernel/hd.c (limited to 'kernel') diff --git a/kernel/fs/hd.c b/kernel/fs/hd.c deleted file mode 100644 index 706d503..0000000 --- a/kernel/fs/hd.c +++ /dev/null @@ -1,132 +0,0 @@ -#include -#include -#include -#include - -#define ST_ERR 1 -#define ST_DRQ 8 -#define ST_SRV 16 -#define ST_DF 32 -#define ST_RDY 64 -#define ST_BSY 128 - -/* sector buffer */ -static char sbuf[512]; - -void hd_init(void) { - int i; - uint8_t s; - uint16_t *buf = (uint16_t*) sbuf; - - /* select the master drive */ - outb(0x1F6, 0xA0); - /* send the IDENTIFY command */ - outb(0x1F7, 0xEC); - - while(1) { - s = inb(0x1F7); - if(!s) { - printk("[hd] Master drive not detected on primary bus!\n"); - panic(); - } - if(s & ST_ERR) { - if(!inb(0x1F4) && !inb(0x1F5)) - continue; - printk("[hd] IDENTIFY command error!\n"); - panic(); - } - if(s & ST_DRQ && !(s & ST_ERR)) - break; - } - - printk("[hd] Master drive detected on primary bus! (status: 0x%02x)\n", s); - - for(i = 0; i < 256; i++) - buf[i] = inw(0x1F0); -} - -int hd_read(void *buf, uint32_t lba, uint8_t c) { - int n; - uint8_t s; - uint16_t *p = buf; - - if(!c) - return 0; - - /* select the master disk on the primary bus */ - outb(0x1F6, 0xE0 | ((lba >> 24) & 0xF)); - /* set the number of sectors to be read */ - outb(0x1F2, c); - /* set the LBA address for the read operation */ - outb(0x1F3, lba); - outb(0x1F4, lba >> 8); - outb(0x1F5, lba >> 16); - /* send the READ command */ - outb(0x1F7, 0x20); - - while(c--) { - /* wait for the drive */ - while(1) { - /* poll the drive's status */ - s = inb(0x1F7); - if(s & ST_DF) { - printk("[hd] Drive failure!\n"); - panic(); - } - if(s & ST_ERR) - return -1; - if(!(s & ST_BSY) && s & ST_DRQ) - break; - } - - /* buffer is full, copy it's data */ - n = 256; - while(n--) - *(p++) = inw(0x1F0); - } - - return 0; -} - -int hd_write(void *buf, uint32_t lba, uint8_t c) { - int n; - uint8_t s; - uint16_t *p = buf; - - if(!c) - return 0; - - /* select the master disk on the primary bus */ - outb(0x1F6, 0xE0 | ((lba >> 24) & 0xF)); - /* set the number of sectors to be written */ - outb(0x1F2, c); - /* set the LBA address for the write operation */ - outb(0x1F3, lba); - outb(0x1F4, lba >> 8); - outb(0x1F5, lba >> 16); - /* send the WRITE command */ - outb(0x1F7, 0x30); - - while(c--) { - /* wait for the drive */ - while(1) { - /* poll the drive's status */ - s = inb(0x1F7); - if(s & ST_DF) { - printk("[hd] Drive failure!\n"); - panic(); - } - if(s & ST_ERR) - return -1; - if(!(s & ST_BSY) && s & ST_DRQ) - break; - } - - /* refill the drive's buffer */ - n = 256; - while(n--) - outw(0x1F0, *(p++)); - } - - return 0; -} diff --git a/kernel/hd.c b/kernel/hd.c new file mode 100644 index 0000000..706d503 --- /dev/null +++ b/kernel/hd.c @@ -0,0 +1,132 @@ +#include +#include +#include +#include + +#define ST_ERR 1 +#define ST_DRQ 8 +#define ST_SRV 16 +#define ST_DF 32 +#define ST_RDY 64 +#define ST_BSY 128 + +/* sector buffer */ +static char sbuf[512]; + +void hd_init(void) { + int i; + uint8_t s; + uint16_t *buf = (uint16_t*) sbuf; + + /* select the master drive */ + outb(0x1F6, 0xA0); + /* send the IDENTIFY command */ + outb(0x1F7, 0xEC); + + while(1) { + s = inb(0x1F7); + if(!s) { + printk("[hd] Master drive not detected on primary bus!\n"); + panic(); + } + if(s & ST_ERR) { + if(!inb(0x1F4) && !inb(0x1F5)) + continue; + printk("[hd] IDENTIFY command error!\n"); + panic(); + } + if(s & ST_DRQ && !(s & ST_ERR)) + break; + } + + printk("[hd] Master drive detected on primary bus! (status: 0x%02x)\n", s); + + for(i = 0; i < 256; i++) + buf[i] = inw(0x1F0); +} + +int hd_read(void *buf, uint32_t lba, uint8_t c) { + int n; + uint8_t s; + uint16_t *p = buf; + + if(!c) + return 0; + + /* select the master disk on the primary bus */ + outb(0x1F6, 0xE0 | ((lba >> 24) & 0xF)); + /* set the number of sectors to be read */ + outb(0x1F2, c); + /* set the LBA address for the read operation */ + outb(0x1F3, lba); + outb(0x1F4, lba >> 8); + outb(0x1F5, lba >> 16); + /* send the READ command */ + outb(0x1F7, 0x20); + + while(c--) { + /* wait for the drive */ + while(1) { + /* poll the drive's status */ + s = inb(0x1F7); + if(s & ST_DF) { + printk("[hd] Drive failure!\n"); + panic(); + } + if(s & ST_ERR) + return -1; + if(!(s & ST_BSY) && s & ST_DRQ) + break; + } + + /* buffer is full, copy it's data */ + n = 256; + while(n--) + *(p++) = inw(0x1F0); + } + + return 0; +} + +int hd_write(void *buf, uint32_t lba, uint8_t c) { + int n; + uint8_t s; + uint16_t *p = buf; + + if(!c) + return 0; + + /* select the master disk on the primary bus */ + outb(0x1F6, 0xE0 | ((lba >> 24) & 0xF)); + /* set the number of sectors to be written */ + outb(0x1F2, c); + /* set the LBA address for the write operation */ + outb(0x1F3, lba); + outb(0x1F4, lba >> 8); + outb(0x1F5, lba >> 16); + /* send the WRITE command */ + outb(0x1F7, 0x30); + + while(c--) { + /* wait for the drive */ + while(1) { + /* poll the drive's status */ + s = inb(0x1F7); + if(s & ST_DF) { + printk("[hd] Drive failure!\n"); + panic(); + } + if(s & ST_ERR) + return -1; + if(!(s & ST_BSY) && s & ST_DRQ) + break; + } + + /* refill the drive's buffer */ + n = 256; + while(n--) + outw(0x1F0, *(p++)); + } + + return 0; +} diff --git a/kernel/kmain.c b/kernel/kmain.c index 00455b7..7c8a72d 100644 --- a/kernel/kmain.c +++ b/kernel/kmain.c @@ -3,40 +3,11 @@ #include #include -extern int hd_read(void*, uint32_t, uint8_t); -extern int hd_write(void*, uint32_t, uint8_t); - -static char buf[0x1000]; - void kmain(void) { int i; int ret; printk("Kernel booted!\n\n"); - /* sched_init(); */ - - /* try to read some data */ - - ret = hd_read(buf, 0, 2); - if(ret < 0) { - printk("Failed to read data!\n"); - return; - } - printk("Successfully read first and second sectors!\n"); - printk("First dword (sector 0): 0x%08x\n", *((uint32_t*) buf)); - printk("First dword (sector 1): 0x%08x\n", *((uint32_t*) (buf + 512))); - - /* try to write some data */ - - for(i = 0; i < 256; i++) - ((uint16_t*) buf)[i] = i; - - ret = hd_write(buf, 2, 1); - if(ret < 0) { - printk("Failed to write data!\n"); - return; - } - - printk("Successfully wrote to sector 2\n"); + sched_init(); } -- cgit v1.3