Add preliminary ATA PIO driver
diff --git a/src/kernel/Jmk b/src/kernel/Jmk
index e1dc457..4133349 100644
--- a/src/kernel/Jmk
+++ b/src/kernel/Jmk
@@ -11,6 +11,9 @@
archetype(asm)
depends(initrd, $(ROOT)/boot/initrd, initrd.img)
+depends(ata_pio, dri/ata_pio, ata_pio.a)
+
+CFLAGS += -I $(ROOT)/include/kernel
LDFLAGS += -Tlink.ld -melf_i386
ASMFLAGS += -felf -Fdwarf
@@ -37,7 +40,8 @@
vfs_initrd.o \
syscall.o \
task.o \
- task_api.o
+ task_api.o \
+ lib(ata_pio)
type(custom_link)
diff --git a/src/kernel/dri/ata_pio/Jmk b/src/kernel/dri/ata_pio/Jmk
new file mode 100644
index 0000000..b2fa318
--- /dev/null
+++ b/src/kernel/dri/ata_pio/Jmk
@@ -0,0 +1,17 @@
+init(ata_pio, ata_pio.a)
+
+preset(freestanding)
+preset(optimize)
+preset(debug)
+preset(32)
+preset(warn)
+
+CFLAGS += -I $(ROOT)/include/kernel
+
+archetype(c)
+
+OBJECTS = ata_pio.o
+
+type(static_lib)
+
+finish
diff --git a/src/kernel/dri/ata_pio/ata_pio.c b/src/kernel/dri/ata_pio/ata_pio.c
new file mode 100644
index 0000000..a8ec300
--- /dev/null
+++ b/src/kernel/dri/ata_pio/ata_pio.c
@@ -0,0 +1,70 @@
+#include <dri/ata_pio/ata_pio.h>
+
+#include <io.h>
+#include <log.h>
+
+static uchar test_buffer[256 * 4];
+
+void ata_pio_wait_bsy()
+{
+ while (inb(ATA_PORT_CMD) & ATA_BSY)
+ {}
+}
+
+void ata_pio_wait_drq()
+{
+ while (!(inb(ATA_PORT_CMD) & ATA_RDY))
+ {}
+}
+
+static void ata_pio_send_init(uint lba, uchar num_sectors)
+{
+ outb(ATA_PORT_DRIVE_SEL, 0xe0 | (lba >> 24));
+ outb(ATA_PORT_SECTOR_COUNT, num_sectors);
+ outb(ATA_PORT_LBA_LOW, lba & 0xff);
+ outb(ATA_PORT_LBA_MID, (lba >> 8) & 0xff);
+ outb(ATA_PORT_LBA_HIGH, (lba >> 16) & 0xff);
+}
+
+void ata_pio_read_sectors(uchar *buffer, uint lba, uchar num_sectors)
+{
+ ata_pio_wait_bsy();
+
+ ata_pio_send_init(lba, num_sectors);
+ outb(ATA_PORT_CMD, ATA_CMD_READ);
+
+ for (int i = 0; i < num_sectors; i++)
+ {
+ ata_pio_wait_bsy();
+ ata_pio_wait_drq();
+
+ for (int j = 0; j < 256; j++)
+ buffer[i * 256 + j] = inb(ATA_PORT_DATA);
+ }
+}
+
+void ata_pio_write_sectors(uint lba, uchar num_sectors, uchar *buffer)
+{
+ ata_pio_wait_bsy();
+
+ ata_pio_send_init(lba, num_sectors);
+ outb(ATA_PORT_CMD, ATA_CMD_WRITE);
+
+ for (int i = 0; i < num_sectors; i++)
+ {
+ ata_pio_wait_bsy();
+ ata_pio_wait_drq();
+
+ for (int j = 0; j < 256; j++)
+ outb(ATA_PORT_DATA, buffer[i * 256 + j]);
+ }
+}
+
+void test_ata_pio()
+{
+ kprintf("Going to ata_pio_read_sectors\n");
+ ata_pio_read_sectors(test_buffer, 0, 4);
+
+ for (int i = 0; i < 256 * 4; i += 64)
+ kprintf("Byte %d = %d\n", test_buffer[i]);
+}
diff --git a/src/kernel/io.h b/src/kernel/io.h
deleted file mode 100644
index 85f2e8c..0000000
--- a/src/kernel/io.h
+++ /dev/null
@@ -1,31 +0,0 @@
-#pragma once
-
-#include "kint.h"
-#include "registers.h"
-
-#define KBD_CMD_PORT 0x64
-#define KBD_DATA_PORT 0x60
-
-struct kbd_scan_code_info
-{
- bool pressed;
- bool escape;
- char key;
-};
-
-extern struct kbd_scan_code_info scan_code_table[0xff];
-
-void outb(ushort port, uchar val);
-uchar inb(ushort port);
-ushort inw(ushort port);
-
-/* Random string.h stuff, TODO: move to own header */
-void *memset(void *s, int c, size_t n);
-void *memcpy(void *dest, const void *src, size_t n);
-void strcpy(char *dest, char *src);
-int strcmp(char *a, char *b);
-uint strlen(char *a);
-
-uchar kbd_scan_code();
-void kbd_handle_input(struct registers *registers);
-void init_kbd();
diff --git a/src/kernel/kint.h b/src/kernel/kint.h
deleted file mode 100644
index f70b22f..0000000
--- a/src/kernel/kint.h
+++ /dev/null
@@ -1,23 +0,0 @@
-#pragma once
-
-typedef unsigned char uchar;
-typedef unsigned short ushort;
-typedef unsigned int uint;
-typedef unsigned long ulong;
-
-typedef unsigned long size_t;
-
-typedef _Bool bool;
-
-enum
-{
- false = 0,
- true,
-};
-
-#define NULL 0
-#define MIN(a, b) ((a)>(b)?(b):(a))
-#define MAX(a, b) ((a)>(b)?(a):(b))
-
-// Coerce into 1 or 0
-#define BOOL(a) (!(!(a)))
diff --git a/src/kernel/log.h b/src/kernel/log.h
deleted file mode 100644
index fe5dcd9..0000000
--- a/src/kernel/log.h
+++ /dev/null
@@ -1,12 +0,0 @@
-#pragma once
-
-#include "kint.h"
-
-void kprintf(const char *format, ...);
-void kassert_int(bool condition, const char *message, const char *file,
- const int line);
-
-#define kassert(cond, msg) kassert_int((cond), (msg), __FILE__, __LINE__)
-#define kpanic(msg) \
- kassert(false, msg); \
- __builtin_unreachable()
diff --git a/src/kernel/main.c b/src/kernel/main.c
index 355ce9f..2ee8192 100644
--- a/src/kernel/main.c
+++ b/src/kernel/main.c
@@ -10,6 +10,7 @@
#include "vfs.h"
#include "vfs_initrd.h"
#include "vga.h"
+#include <dri/ata_pio/ata_pio.h>
void greet()
{
@@ -76,15 +77,13 @@
init_tasks();
kprintf("\ndone initializing tasks\n");
+#ifdef TEST_THREADS
spawn_thread(other_thread, NULL);
greet();
+#endif
- asm volatile("cli");
-
- switch_task();
-
- kprintf("Switched task, this should have done nothing.\n");
+ test_ata_pio();
while (true)
asm volatile("hlt");
diff --git a/src/kernel/registers.h b/src/kernel/registers.h
deleted file mode 100644
index 687bd0e..0000000
--- a/src/kernel/registers.h
+++ /dev/null
@@ -1,11 +0,0 @@
-#pragma once
-
-#include "kint.h"
-
-struct registers
-{
- uint ds;
- uint edi, esi, ebp, esp, ebx, edx, ecx, eax;
- uint interrupt_number, error_code;
- uint eip, cs, eflags, useresp, ss;
-};