-
Notifications
You must be signed in to change notification settings - Fork 23
/
fat.c
454 lines (338 loc) · 8.55 KB
/
fat.c
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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
// Copyright 2009 Segher Boessenkool <[email protected]>
// This code is licensed to you under the terms of the GNU GPL, version 2;
// see file COPYING or http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt
// hacked to fit into xell by Felix Domke <[email protected]>
#include <types.h>
#include <string.h>
#include "diskio.h"
static inline u16 le16(const u8 *p)
{
return p[0] | (p[1] << 8);
}
static inline u32 le32(const u8 *p)
{
return p[0] | (p[1] << 8) | (p[2] << 16) | (p[3] << 24);
}
//#include <stdio.h>
#define RAW_BUF 0x200
#define MAX_SECTS 8
static u8 raw_buf[RAW_BUF] __attribute__((aligned(32)));
static struct bdev *dev;
static int raw_read(u32 sector)
{
static u32 current = -1;
if (current == sector)
return 0;
current = sector;
return dev->ops->read(dev, raw_buf, sector, 1) < 0 ? : 0;
}
static u64 partition_start_offset;
static int read(u8 *data, u64 offset, u32 len)
{
offset += partition_start_offset;
while (len) {
u32 buf_off = offset % RAW_BUF;
u32 n;
int err;
if (!buf_off && !((data - raw_buf)&31) && len >= RAW_BUF)
{
n = len / RAW_BUF;
if (n > MAX_SECTS)
n = MAX_SECTS;
/* aligned */
err = dev->ops->read(dev, data, offset / RAW_BUF, n);
if (err < n)
return err;
n *= RAW_BUF;
} else
{
/* non-aligned */
n = RAW_BUF - buf_off;
if (n > len)
n = len;
err = raw_read(offset / RAW_BUF);
if (err)
return err;
memcpy(data, raw_buf + buf_off, n);
}
data += n;
offset += n;
len -= n;
}
return 0;
}
static u32 bytes_per_cluster;
static u32 root_entries;
static u32 clusters;
static u32 fat_type; // 12, 16, or 32
static u64 fat_offset;
static u64 root_offset;
static u64 data_offset;
static u32 get_fat(u32 cluster)
{
u8 fat[4];
u32 offset_bits = cluster*fat_type;
int err = read(fat, fat_offset + offset_bits/8, 4);
if (err)
return 0;
u32 res = le32(fat) >> (offset_bits % 8);
res &= (1 << fat_type) - 1;
res &= 0x0fffffff; // for FAT32
return res;
}
static u64 extent_offset;
static u32 extent_len;
static u32 extent_next_cluster;
static void get_extent(u32 cluster)
{
extent_len = 0;
extent_next_cluster = 0;
if (cluster == 0) { // Root directory.
if (fat_type != 32) {
extent_offset = root_offset;
extent_len = 0x20*root_entries;
return;
}
cluster = root_offset;
}
if (cluster - 2 >= clusters)
return;
extent_offset = data_offset + (u64)bytes_per_cluster*(cluster - 2);
for (;;) {
extent_len += bytes_per_cluster;
u32 next_cluster = get_fat(cluster);
if (next_cluster - 2 >= clusters)
break;
if (next_cluster != cluster + 1) {
extent_next_cluster = next_cluster;
break;
}
cluster = next_cluster;
}
}
static int read_extent(u8 *data, u32 len)
{
while (len) {
if (extent_len == 0)
return -1;
u32 this = len;
if (this > extent_len)
this = extent_len;
int err = read(data, extent_offset, this);
if (err)
return err;
extent_offset += this;
extent_len -= this;
data += this;
len -= this;
if (extent_len == 0 && extent_next_cluster)
get_extent(extent_next_cluster);
}
return 0;
}
int fat_read(void *data, u32 len)
{
return read_extent(data, len);
}
static u8 fat_name[11];
static u8 ucase(char c)
{
if (c >= 'a' && c <= 'z')
return c - 'a' + 'A';
return c;
}
static const char *parse_component(const char *path)
{
u32 i = 0;
while (*path == '/')
path++;
while (*path && *path != '/' && *path != '.') {
if (i < 8)
fat_name[i++] = ucase(*path);
path++;
}
while (i < 8)
fat_name[i++] = ' ';
if (*path == '.')
path++;
while (*path && *path != '/') {
if (i < 11)
fat_name[i++] = ucase(*path);
path++;
}
while (i < 11)
fat_name[i++] = ' ';
if (fat_name[0] == 0xe5)
fat_name[0] = 0x05;
return path;
}
u32 fat_file_size;
int fat_open(const char *name)
{
u32 cluster = 0;
while (*name) {
get_extent(cluster);
name = parse_component(name);
while (extent_len) {
u8 dir[0x20];
int err = read_extent(dir, 0x20);
if (err)
return err;
if (dir[0] == 0)
return -1;
if (dir[0x0b] & 0x08) // volume label or LFN
continue;
if (dir[0x00] == 0xe5) // deleted file
continue;
if (!!*name != !!(dir[0x0b] & 0x10)) // dir vs. file
continue;
if (memcmp(fat_name, dir, 11) == 0) {
cluster = le16(dir + 0x1a);
if (fat_type == 32)
cluster |= le16(dir + 0x14) << 16;
if (*name == 0) {
fat_file_size = le32(dir + 0x1c);
get_extent(cluster);
return 0;
}
break;
}
}
}
return -1;
}
#ifdef FAT_TEST
static void print_dir_entry(u8 *dir)
{
int i, n;
if (dir[0x0b] & 0x08) // volume label or LFN
return;
if (dir[0x00] == 0xe5) // deleted file
return;
if (fat_type == 32) {
fprintf(stderr, "#%04x", le16(dir + 0x14));
fprintf(stderr, "%04x ", le16(dir + 0x1a));
} else
fprintf(stderr, "#%04x ", le16(dir + 0x1a)); // start cluster
u16 date = le16(dir + 0x18);
fprintf(stderr, "%04d-%02d-%02d ", 1980 + (date >> 9), (date >> 5) & 0x0f, date & 0x1f);
u16 time = le16(dir + 0x16);
fprintf(stderr, "%02d:%02d:%02d ", time >> 11, (time >> 5) & 0x3f, 2*(time & 0x1f));
fprintf(stderr, "%10d ", le32(dir + 0x1c)); // file size
u8 attr = dir[0x0b];
for (i = 0; i < 6; i++)
fprintf(stderr, "%c", (attr & (1 << i)) ? "RHSLDA"[i] : ' ');
fprintf(stderr, " ");
for (n = 8; n && dir[n - 1] == ' '; n--)
;
for (i = 0; i < n; i++)
fprintf(stderr, "%c", dir[i]);
for (n = 3; n && dir[8 + n - 1] == ' '; n--)
;
if (n) {
fprintf(stderr, ".");
for (i = 0; i < n; i++)
fprintf(stderr, "%c", dir[8 + i]);
}
fprintf(stderr, "\n");
}
int print_dir(u32 cluster)
{
u8 dir[0x20];
get_extent(cluster);
while (extent_len) {
int err = read_extent(dir, 0x20);
if (err)
return err;
if (dir[0] == 0)
break;
print_dir_entry(dir);
}
return 0;
}
#endif
static int fat_init_fs(const u8 *sb)
{
u32 bytes_per_sector = le16(sb + 0x0b);
u32 sectors_per_cluster = sb[0x0d];
bytes_per_cluster = bytes_per_sector * sectors_per_cluster;
u32 reserved_sectors = le16(sb + 0x0e);
u32 fats = sb[0x10];
root_entries = le16(sb + 0x11);
u32 total_sectors = le16(sb + 0x13);
u32 sectors_per_fat = le16(sb + 0x16);
// For FAT16 and FAT32:
if (total_sectors == 0)
total_sectors = le32(sb + 0x20);
// For FAT32:
if (sectors_per_fat == 0)
sectors_per_fat = le32(sb + 0x24);
// XXX: For FAT32, we might want to look at offsets 28, 2a
// XXX: We _do_ need to look at 2c
u32 fat_sectors = sectors_per_fat * fats;
u32 root_sectors = (0x20*root_entries + bytes_per_sector - 1)
/ bytes_per_sector;
u32 fat_start_sector = reserved_sectors;
u32 root_start_sector = fat_start_sector + fat_sectors;
u32 data_start_sector = root_start_sector + root_sectors;
clusters = (total_sectors - data_start_sector) / sectors_per_cluster;
if (clusters < 0x0ff5)
fat_type = 12;
else if (clusters < 0xfff5)
fat_type = 16;
else
fat_type = 32;
fat_offset = (u64)bytes_per_sector*fat_start_sector;
root_offset = (u64)bytes_per_sector*root_start_sector;
data_offset = (u64)bytes_per_sector*data_start_sector;
if (fat_type == 32)
root_offset = le32(sb + 0x2c);
#ifdef FAT_TEST
fprintf(stderr, "bytes_per_sector = %08x\n", bytes_per_sector);
fprintf(stderr, "sectors_per_cluster = %08x\n", sectors_per_cluster);
fprintf(stderr, "bytes_per_cluster = %08x\n", bytes_per_cluster);
fprintf(stderr, "root_entries = %08x\n", root_entries);
fprintf(stderr, "clusters = %08x\n", clusters);
fprintf(stderr, "fat_type = %08x\n", fat_type);
fprintf(stderr, "fat_offset = %012llx\n", fat_offset);
fprintf(stderr, "root_offset = %012llx\n", root_offset);
fprintf(stderr, "data_offset = %012llx\n", data_offset);
#endif
return 0;
}
static int is_fat_fs(const u8 *sb)
{
// Bytes per sector should be 512, 1024, 2048, or 4096
u32 bps = le16(sb + 0x0b);
if (bps < 0x0200 || bps > 0x1000 || bps & (bps - 1))
return 0;
// Media type should be f0 or f8,...,ff
if (sb[0x15] < 0xf8 && sb[0x15] != 0xf0)
return 0;
// If those checks didn't fail, it's FAT. We hope.
return 1;
}
int fat_init(struct bdev *_dev)
{
u8 buf[0x200];
int err;
dev = _dev;
partition_start_offset = 0;
err = read(buf, 0, 0x200);
if (err)
return err;
if (le16(buf + 0x01fe) != 0xaa55) // Not a DOS disk.
return -1;
if (is_fat_fs(buf))
return fat_init_fs(buf);
// Maybe there's a partition table? Let's try the first partition.
if (buf[0x01c2] == 0)
return -1;
partition_start_offset = 0x200ULL*le32(buf + 0x01c6);
err = read(buf, 0, 0x200);
if (err)
return err;
if (is_fat_fs(buf))
return fat_init_fs(buf);
return -1;
}