summaryrefslogtreecommitdiff
path: root/src/kernel/fs.c
blob: 129e6312a2b7aecae7f5760a337838213340fc64 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
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;
}