From 6becbff5724d79cb3a958db297b5c3310200daea Mon Sep 17 00:00:00 2001 From: Marvin Borner Date: Thu, 23 Jul 2020 21:06:44 +0200 Subject: Added *very* simple ide and ext2 support --- src/drivers/cpu.c | 8 ++++++++ src/drivers/ide.c | 41 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 49 insertions(+) create mode 100644 src/drivers/ide.c (limited to 'src/drivers') diff --git a/src/drivers/cpu.c b/src/drivers/cpu.c index 22691fb..d6a1760 100644 --- a/src/drivers/cpu.c +++ b/src/drivers/cpu.c @@ -24,6 +24,14 @@ u32 inl(u16 port) return value; } +void insl(u16 port, void *addr, int n) +{ + __asm__("cld; rep insl" + : "=D"(addr), "=c"(n) + : "d"(port), "0"(addr), "1"(n) + : "memory", "cc"); +} + void outb(u16 port, u8 data) { __asm__("outb %0, %1" ::"a"(data), "Nd"(port)); diff --git a/src/drivers/ide.c b/src/drivers/ide.c new file mode 100644 index 0000000..fe3955c --- /dev/null +++ b/src/drivers/ide.c @@ -0,0 +1,41 @@ +// MIT License, Copyright (c) 2020 Marvin Borner + +#include +#include +#include + +int ide_wait(int check) +{ + char r; + + // Wait while drive is busy. Once just ready is set, exit the loop + while (((r = (char)inb(IDE_IO | IDE_CMD)) & (IDE_BUSY | IDE_READY)) != IDE_READY) + ; + + // Check for errors + if (check && (r & (IDE_DRIVE_FAULT | IDE_ERROR)) != 0) + return 0xF; + return 0; +} + +void *ide_read(void *b, u32 block) +{ + int sector_per_block = BLOCK_SIZE / SECTOR_SIZE; // 2 + int sector = block * sector_per_block; + + ide_wait(0); + outb(IDE_IO | IDE_SECTOR_COUNT, sector_per_block); // Number of sectors + outb(IDE_IO | IDE_LOW, LBA_LOW(sector)); + outb(IDE_IO | IDE_MID, LBA_MID(sector)); + outb(IDE_IO | IDE_HIGH, LBA_HIGH(sector)); + + // Slave/Master << 4 and last 4 bits + outb(IDE_IO | IDE_HEAD, 0xE0 | (1 << 4) | LBA_LAST(sector)); + outb(IDE_IO | IDE_CMD, IDE_CMD_READ); + ide_wait(0); + + // Read-only + insl(IDE_IO, b, BLOCK_SIZE / 4); + + return b; +} -- cgit v1.2.3