From 02f14113cbf14c6f842fb43ecbc68d0c851ef3b0 Mon Sep 17 00:00:00 2001 From: Benji Dial Date: Sun, 24 May 2020 11:02:43 -0400 Subject: very basic vga, ata, serial drivers. start of fat and fs drivers --- src/kernel/fs.c | 82 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 82 insertions(+) create mode 100644 src/kernel/fs.c (limited to 'src/kernel/fs.c') diff --git a/src/kernel/fs.c b/src/kernel/fs.c new file mode 100644 index 0000000..129e631 --- /dev/null +++ b/src/kernel/fs.c @@ -0,0 +1,82 @@ +#include +#include "fs.h" +#include "ata.h" +#include "fat.h" +#include +#include "panic.h" + +#define MAX_HANDLES 32 +#define FILE_BUFFER(h) ((void *)(0x0002be00 + (h << 9))) +#define HANDLE(h) ((handles - 1)[h]) +#define HEAD(h) (FILE_BUFFER(h) + HANDLE(h).seek % 512) + +struct handle_info { + uint16_t start_cluster; + uint16_t seek; + uint16_t loaded_cluster; + bool dirty; +}; + +struct handle_info handles[MAX_HANDLES]; + +void clear_fs_handles() { + struct handle_info *f = handles; + while (f < handles + MAX_HANDLES) + (f++)->start_cluster = 0; +} + +fs_handle next_handle() { + fs_handle r = 0; + while (HANDLE(++r).start_cluster) + if (r == MAX_HANDLES) + return 0; + return r; +} + +void write_buffer(fs_handle handle) { + HANDLE(handle).dirty = false; + panic("Not implemented (write_buffer)."); +} + +void read_buffer(fs_handle handle) { + if (HANDLE(handle).dirty) + write_buffer(handle); + HANDLE(handle).loaded_cluster = HANDLE(handle).seek >> 9; + panic("Not implemented (read_buffer)."); +} + +fs_handle fs_open(uint8_t *path) { + fs_handle next = next_handle(); + if (!next) + return 0; + uint16_t s = get_start_cluster(path); + if (!s) + return 0; + HANDLE(next).start_cluster = s; + HANDLE(next).seek = 0; + HANDLE(next).dirty = false; + read_buffer(next); + return next; +} + +int16_t fs_seek(fs_handle handle, int16_t by) { + uint16_t old = HANDLE(handle).seek; + uint16_t to = -by > old ? 0 : old + by; + HANDLE(handle).seek = to; + if ((to ^ old) & 0xfe00) + read_buffer(handle); +} + +uint16_t fs_read(fs_handle handle, uint16_t max, void *buffer) { + panic("Not implemented (fs_read)."); +} + +uint16_t fs_write(fs_handle handle, uint16_t max, void *buffer) { + panic("Not implemented (fs_write)."); +} + +void fs_close(fs_handle handle) { + if (HANDLE(handle).dirty) + write_buffer(handle); + HANDLE(handle).start_cluster = 0; +} \ No newline at end of file -- cgit v1.2.3