summaryrefslogtreecommitdiff
path: root/src/kernel
diff options
context:
space:
mode:
Diffstat (limited to 'src/kernel')
-rw-r--r--src/kernel/arch/i386/ata.c48
-rw-r--r--src/kernel/arch/i386/ata.h3
-rw-r--r--src/kernel/vfs/root.c52
3 files changed, 85 insertions, 18 deletions
diff --git a/src/kernel/arch/i386/ata.c b/src/kernel/arch/i386/ata.c
index 9224ed6..b546c07 100644
--- a/src/kernel/arch/i386/ata.c
+++ b/src/kernel/arch/i386/ata.c
@@ -15,6 +15,9 @@ static struct {
} ata_drives[4];
enum {
+ DATA = 0,
+ FEAT = 1,
+ SCNT = 2,
LBAlo = 3,
LBAmid = 4,
LBAhi = 5,
@@ -112,20 +115,39 @@ static bool ata_identify(int drive) {
}
void ata_init(void) {
- tty_const("\n");
for (int i = 0; i < 4; i++) {
- tty_const("probing drive ");
- _tty_var(i);
ata_detecttype(i);
- if (ata_drives[i].type != DEV_UNKNOWN) {
- if (ata_identify(i)) {
- tty_const(" - ");
- _tty_var(ata_drives[i].sectors);
- tty_const(" sectors (512b)");
- } else {
- tty_const(" identify failed");
- }
- }
- tty_const("\n");
+ if (ata_drives[i].type == DEV_PATA)
+ ata_identify(i);
}
}
+
+bool ata_available(int drive) {
+ return ata_drives[drive].type != DEV_UNKNOWN;
+}
+
+int ata_read(int drive, uint32_t lba, void *buf) {
+ assert(ata_drives[drive].type == DEV_PATA);
+ int iobase = ata_iobase(drive);
+
+ ata_driveselect(drive, lba);
+ port_out8(iobase + FEAT, 0); // supposedly pointless
+ port_out8(iobase + SCNT, 1); // sector count
+ port_out8(iobase + LBAlo, lba);
+ port_out8(iobase + LBAmid, lba >> 8);
+ port_out8(iobase + LBAhi, lba >> 16);
+ port_out8(iobase + CMD, 0x20); // READ SECTORS
+
+ for (;;) { // TODO separate polling function
+ uint8_t v = port_in8(iobase + STATUS);
+ if (v & 0x80) continue; // still BSY, continue
+ if (v & 0x40) break; // RDY, break
+ // TODO check for ERR
+ }
+
+ uint16_t *b = buf;
+ for (int i = 0; i < 256; i++)
+ b[i] = port_in16(iobase);
+
+ return 512;
+}
diff --git a/src/kernel/arch/i386/ata.h b/src/kernel/arch/i386/ata.h
index 9137cdb..82f4f81 100644
--- a/src/kernel/arch/i386/ata.h
+++ b/src/kernel/arch/i386/ata.h
@@ -1,4 +1,7 @@
#pragma once
+#include <stdbool.h>
#include <stdint.h>
void ata_init(void);
+bool ata_available(int drive);
+int ata_read(int drive, uint32_t lba, void *buf);
diff --git a/src/kernel/vfs/root.c b/src/kernel/vfs/root.c
index 9833330..e881ff2 100644
--- a/src/kernel/vfs/root.c
+++ b/src/kernel/vfs/root.c
@@ -1,3 +1,4 @@
+#include <kernel/arch/i386/ata.h>
#include <kernel/mem/virt.h>
#include <kernel/panic.h>
#include <kernel/proc.h>
@@ -5,10 +6,15 @@
#include <kernel/vfs/root.h>
#include <shared/mem.h>
+// TODO move to arch/
+
enum {
HANDLE_ROOT,
HANDLE_TTY,
HANDLE_VGA,
+ HANDLE_ATA_ROOT,
+ HANDLE_ATA,
+ _SKIP = HANDLE_ATA + 4,
};
static bool exacteq(struct vfs_request *req, const char *str) {
@@ -49,18 +55,29 @@ static void req_preprocess(struct vfs_request *req, int max_len) {
int vfs_root_handler(struct vfs_request *req) {
switch (req->type) {
case VFSOP_OPEN:
- if (exacteq(req, "/")) return HANDLE_ROOT;
- if (exacteq(req, "/tty")) return HANDLE_TTY;
- if (exacteq(req, "/vga")) return HANDLE_VGA;
+ if (exacteq(req, "/")) return HANDLE_ROOT;
+ if (exacteq(req, "/tty")) return HANDLE_TTY;
+ if (exacteq(req, "/vga")) return HANDLE_VGA;
+ if (exacteq(req, "/ata/")) return HANDLE_ATA_ROOT;
+
+ if (exacteq(req, "/ata/0"))
+ return ata_available(0) ? HANDLE_ATA+0 : -1;
+ if (exacteq(req, "/ata/1"))
+ return ata_available(1) ? HANDLE_ATA+1 : -1;
+ if (exacteq(req, "/ata/2"))
+ return ata_available(2) ? HANDLE_ATA+2 : -1;
+ if (exacteq(req, "/ata/3"))
+ return ata_available(3) ? HANDLE_ATA+3 : -1;
+
return -1;
case VFSOP_READ:
switch (req->id) {
case HANDLE_ROOT: {
// TODO document directory read format
- const char *src = "tty\0vga\0";
+ const char src[] = "tty\0vga\0ata/";
if (req->output.len < 0) return 0; // is this needed? TODO make that a size_t or something
- int len = min((size_t) req->output.len, strlen(src) + 1);
+ int len = min((size_t) req->output.len, sizeof(src));
virt_cpy_to(req->caller->pages, req->output.buf, src, len);
return len;
}
@@ -79,6 +96,30 @@ int vfs_root_handler(struct vfs_request *req) {
vga + req->offset, req->output.len);
return req->output.len;
}
+ case HANDLE_ATA_ROOT: {
+ // TODO offset
+ char list[8] = {};
+ size_t len = 0;
+ for (int i = 0; i < 4; i++) {
+ if (ata_available(i)) {
+ list[len] = '0' + i;
+ len += 2;
+ }
+ }
+ len = min((size_t) req->output.len, len);
+ virt_cpy_to(req->caller->pages, req->output.buf, list, len);
+ return len;
+ }
+ case HANDLE_ATA: case HANDLE_ATA+1:
+ case HANDLE_ATA+2: case HANDLE_ATA+3: {
+ if (req->offset < 0) return 0;
+ char buf[512];
+ uint32_t sector = req->offset / 512;
+ int len = min(req->output.len, 512 - (req->offset & 511));
+ ata_read(req->id - HANDLE_ATA, sector, buf);
+ virt_cpy_to(req->caller->pages, req->output.buf, buf, len);
+ return len;
+ }
default: panic_invalid_state();
}
@@ -100,6 +141,7 @@ int vfs_root_handler(struct vfs_request *req) {
req->input.buf, req->input.len);
return req->input.len;
}
+ case HANDLE_ATA_ROOT: return -1;
default: panic_invalid_state();
}