summaryrefslogtreecommitdiff
path: root/src/kernel/fat.c
blob: 807af00f969fd999fd119e5685f123a45c76c643 (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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
#include "drive.h"
#include "panic.h"
#include "util.h"
#include "ata.h"
#include "fat.h"
#include "mem.h"

#define MAX_FAT_DRIVES 16
#define MAX_OPEN_FILES_PER_DRIVE 32

enum {
  FA_READ_ONLY = 0x01,
  FA_HIDDEN    = 0x02,
  FA_SYSTEM    = 0x04,
  FA_LABEL     = 0x08,
  FA_DIRECTORY = 0x10,
  FA_ARCHIVE   = 0x20,

  FA_LFN       = 0x0f
};

struct directory_entry {
  uint8_t name[11];
  uint8_t attrib;
  uint8_t name_case;
  uint8_t created_decimal;
  uint16_t created_time;
  uint16_t created_date;
  uint16_t accessed_date;
  uint16_t ignore;
  uint16_t modified_time;
  uint16_t modified_date;
  uint16_t first_cluster;
  uint32_t length;
} __attribute__ ((packed));

struct fat_info {
  //3 bytes jump
  uint8_t  oem[8];
  uint16_t bytes_per_sector;//Assumed to be 512
  uint8_t  sectors_per_cluster;//Assumed to be 1
  uint16_t reserved_sectors;
  uint8_t  fats;//Only first is used
  uint16_t root_entries;
  uint16_t sectors;//Assumed not to be 0
  uint8_t  media_type;
  uint16_t sectors_per_fat;
  uint16_t sectors_per_track;
  uint16_t heads;
  uint32_t hidden_sectors;
  uint32_t sectors_long;
  uint8_t  drive_number;
  uint8_t  reserved;
  uint8_t  ext_boot_marker;
  uint32_t volume_id;
  uint8_t  label[11];
  uint8_t  fs_type[8];
} __attribute__ ((packed));

#define CTOS(c, fdi) ((fdi)->data_start + (c) - 2)

struct fat_drive_info {
  const struct fat_info *fi;
  uint16_t *fat;
  uint16_t root_start;
  uint16_t data_start;
  struct directory_entry open_files[MAX_OPEN_FILES_PER_DRIVE];
};

struct fat_drive_info infos[MAX_FAT_DRIVES];
uint8_t next_id = 0;

uint8_t fat_driver_buffer[512];
struct fat_info *next_fi;

void alloc_next_fi() {
  if (!((uint32_t)(next_fi = (struct fat_info *)((uint32_t)next_fi + 64)) & 0xfff))
    if (!(next_fi = allocate_pages(1)))
      panic("Out of memory in FAT driver.");
}

const struct drive *cur_drive;
fs_id_t cur_id;
const struct fat_drive_info *cur_fdi;
struct directory_entry *cur_dir;

//loads cluster `c`
void load_cluster(uint16_t c, void *to) {
  if (c == 0) {
    *(uint8_t *)to = 0;
    return;
  }

  uint32_t s = CTOS(c, cur_fdi);
  cur_drive->read_sectors(cur_drive, s, 1, to);
}

uint16_t next_cluster(uint16_t c) {
  panic("TODO: compute next sector (or 0 for none)");
}

static inline bool check_fat_names(const uint8_t *a, const uint8_t *b) {
  return (((uint32_t *)a)[0] == ((uint32_t *)b)[0]) &&
         (((uint32_t *)a)[1] == ((uint32_t *)b)[1]) &&
         (((uint16_t *)a)[8] == ((uint16_t *)b)[8]) &&
         (((uint8_t *)a)[10] == ((uint8_t *)b)[10]);
}

//after: cur_dir -> specified entry in root
bool try_locate_root_entry(const uint8_t *fat_name) {
  uint32_t cur_dir_sect = cur_fdi->root_start - 1;
  cur_dir = (struct directory_entry *)(fat_driver_buffer + 512);
  while (true) {
    if (cur_dir == (struct directory_entry *)(fat_driver_buffer + 512)) {
      cur_dir = (struct directory_entry *)fat_driver_buffer;
      ++cur_dir_sect;
      cur_drive->read_sectors(cur_drive, cur_dir_sect, 1, cur_dir);
    }
    if (!*(uint8_t *)cur_dir)
      return false;
    if (check_fat_names(cur_dir->name, fat_name))
      return true;
    else
      ++cur_dir;
  }
}

//before: cur_dir -> entry of dir to search
//after: cur_dir -> specified entry in dir
bool try_locate_entry(const uint8_t *fat_name) {
  uint16_t cur_dir_cluster = cur_dir->first_cluster;
  load_cluster(cur_dir_cluster, fat_driver_buffer);
  cur_dir = (struct directory_entry *)fat_driver_buffer;
  while (true) {
    if (cur_dir == (struct directory_entry *)(fat_driver_buffer + 512)) {
      cur_dir = (struct directory_entry *)fat_driver_buffer;
      ++cur_dir_cluster;
      load_cluster(cur_dir_cluster = next_cluster(cur_dir_cluster), fat_driver_buffer);
    }
    if (!*(uint8_t *)cur_dir)
      return false;
    if (check_fat_names(cur_dir -> name, fat_name))
      return true;
    else
      ++cur_dir;
  }
}

drive_file_id_t fat_get_file(const struct drive *d, const char *path) {
  cur_drive = d;
  cur_id = d->drive_id;
  cur_fdi = &infos[cur_id];
  const struct directory_entry *open_files = cur_fdi->open_files - 1;
  for (drive_file_id_t n = 1; n != MAX_OPEN_FILES_PER_DRIVE + 1; ++n)
    if (!*(uint8_t *)(&open_files[n])) {
      panic("TODO: open path into open_files[n]");
      return n;
    }
  panic("Maximum number of files open reached for FAT drive.");
}

void fat_free_file(const struct drive *d, drive_file_id_t fid) {
  *(uint8_t *)(&infos[d->drive_id].open_files[fid - 1]) = 0;
}

void fat_load_sector(const struct drive *d, drive_file_id_t fid, uint32_t sector, void *at) {
  cur_drive = d;
  cur_id = d->drive_id;
  cur_fdi = &infos[cur_id];
  uint16_t c = cur_fdi->open_files[fid - 1].first_cluster;
  for (uint32_t i = 0; i < sector; ++i)
    c = next_cluster(c);
  load_cluster(c, at);
}

__attribute__ ((pure)) uint32_t fat_get_free_sectors(const struct drive *d) {
  uint16_t *start = infos[d->fs_id].fat + 2;
  uint16_t *end = start + d->n_sectors - infos[d->fs_id].data_start;
  uint32_t count = 0;
  for (uint16_t *i = start; i < end; ++i)
    if (!*i)
      ++count;
  return count;
}

void init_fat() {
  next_fi = allocate_pages(1);
}

bool try_fat_init_drive(struct drive *d) {
  if (next_id >= MAX_FAT_DRIVES)
    panic("Maximum number of FAT drives reached.");

  if (!d->read_sectors(d, 0, 1, fat_driver_buffer))
    return false;
  memcpy(next_fi, fat_driver_buffer + 3, sizeof(struct fat_info));
  uint32_t *fs_type_32 = (uint32_t *)next_fi->fs_type;
  if ((fs_type_32[0] != ('F' + 'A' * 256 + 'T' * 65536 + '1' * 16777216)) ||
      (fs_type_32[1] != ('6' + ' ' * 256 + ' ' * 65536 + ' ' * 16777216)))
    return false;

  d->fs_type = "FAT16";
  d->get_file = &fat_get_file;
  d->free_file = &fat_free_file;
  d->load_sector = &fat_load_sector;
  d->get_free_sectors = &fat_get_free_sectors;

  d->fs_id = next_id;
  infos[next_id].fi = next_fi;
  infos[next_id].fat = allocate_pages(((next_fi->sectors_per_fat - 1) >> 3) + 1);
  infos[next_id].root_start = next_fi->reserved_sectors + 
                              next_fi->sectors_per_fat * next_fi->fats;
  infos[next_id].data_start = infos[next_id].root_start +
                              ((next_fi->root_entries - 1) >> 4) + 1;

  d->read_sectors(d, next_fi->reserved_sectors, next_fi->sectors_per_fat, infos[next_id].fat);
  struct directory_entry *open_files = infos[next_id].open_files - 1;
  for (drive_file_id_t i = 0; i < MAX_OPEN_FILES_PER_DRIVE; ++i)
    *(uint8_t *)&open_files[i] = 0;

  alloc_next_fi();
  ++next_id;
  return true;
}