summaryrefslogtreecommitdiff
path: root/src/kernel/fs.c
diff options
context:
space:
mode:
authorBenji Dial <benji3.141@gmail.com>2020-05-24 11:02:43 -0400
committerBenji Dial <benji3.141@gmail.com>2020-05-24 11:02:43 -0400
commit02f14113cbf14c6f842fb43ecbc68d0c851ef3b0 (patch)
tree75d2d943d9822b3f945fd947e9b91eec1d68e900 /src/kernel/fs.c
parent31d8ae388a7ded576abd3e3d99c3d9193ea6d704 (diff)
downloadportland-os-02f14113cbf14c6f842fb43ecbc68d0c851ef3b0.tar.gz
very basic vga, ata, serial drivers. start of fat and fs drivers
Diffstat (limited to 'src/kernel/fs.c')
-rw-r--r--src/kernel/fs.c82
1 files changed, 82 insertions, 0 deletions
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 <stdint.h>
+#include "fs.h"
+#include "ata.h"
+#include "fat.h"
+#include <stdbool.h>
+#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