diff options
Diffstat (limited to 'src/kernel/arch')
-rw-r--r-- | src/kernel/arch/i386/ata.c | 65 | ||||
-rw-r--r-- | src/kernel/arch/i386/ata.h | 4 | ||||
-rw-r--r-- | src/kernel/arch/i386/boot.c | 7 |
3 files changed, 74 insertions, 2 deletions
diff --git a/src/kernel/arch/i386/ata.c b/src/kernel/arch/i386/ata.c new file mode 100644 index 0000000..996fa3a --- /dev/null +++ b/src/kernel/arch/i386/ata.c @@ -0,0 +1,65 @@ +#include <kernel/arch/i386/ata.h> +#include <kernel/arch/i386/port_io.h> +#include <stdbool.h> + +#include <kernel/arch/io.h> + +enum { + LBAlo = 3, + LBAmid = 4, + LBAhi = 5, + DRV = 6, + CMD = 7, + STATUS = 7, +}; // offsets + +// get I/O port base for drive +static uint16_t ata_iobase(int drive) { + bool secondary = drive&2; + return secondary ? 0x170 : 0x1F0; +} + +static void ata_driveselect(int drive, int block) { + uint8_t v = 0xE0; + if (drive&1) // slave? + v |= 0x10; // set drive number bit + // TODO account for block + port_outb(ata_iobase(drive) + DRV, v); +} + +static bool ata_identify(int drive) { + uint16_t iobase = ata_iobase(drive); + uint8_t v; + + ata_driveselect(drive, 0); + for (int i = 2; i < 6; i++) + port_outb(iobase + i, 0); + port_outb(iobase + CMD, 0xEC); // IDENTIFY + + v = port_inb(iobase + STATUS); + if (v == 0) return false; // nonexistent drive + while (port_inb(iobase + STATUS) & 0x80); + + /* check for uncomformant devices, quit early */ + if (port_inb(iobase + LBAmid) || port_inb(iobase + LBAhi)) { + // TODO atapi + return true; + } + /* pool until bit 3 (DRQ) or 0 (ERR) is set */ + while (!((v = port_inb(iobase + STATUS) & 0x9))); + if (v & 1) return false; /* ERR was set, bail */ + + // now I can read 512 bytes of data, TODO + return true; +} + +void ata_init(void) { + tty_const("\n"); + for (int i = 0; i < 4; i++) { + tty_const("probing drive "); + _tty_var(i); + if (ata_identify(i)) + tty_const(" - exists"); + tty_const("\n"); + } +} diff --git a/src/kernel/arch/i386/ata.h b/src/kernel/arch/i386/ata.h new file mode 100644 index 0000000..9137cdb --- /dev/null +++ b/src/kernel/arch/i386/ata.h @@ -0,0 +1,4 @@ +#pragma once +#include <stdint.h> + +void ata_init(void); diff --git a/src/kernel/arch/i386/boot.c b/src/kernel/arch/i386/boot.c index bd2a00b..94e9e2f 100644 --- a/src/kernel/arch/i386/boot.c +++ b/src/kernel/arch/i386/boot.c @@ -1,4 +1,5 @@ #include <kernel/arch/generic.h> +#include <kernel/arch/i386/ata.h> #include <kernel/arch/i386/boot.h> #include <kernel/arch/i386/gdt.h> #include <kernel/arch/i386/interrupts/idt.h> @@ -15,7 +16,9 @@ void kmain_early(struct multiboot_info *multiboot) { gdt_init(); tty_const("idt..."); idt_init(); - + tty_const("ata..."); + ata_init(); + { // find the init module struct multiboot_mod *module = &multiboot->mods[0]; if (multiboot->mods_count < 1) { @@ -25,6 +28,6 @@ void kmain_early(struct multiboot_info *multiboot) { info.init.at = module->start; info.init.size = module->end - module->start; } - + kmain(info); } |