-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfathelper.c
587 lines (546 loc) · 14 KB
/
fathelper.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
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
#include <assert.h>
#include <stdio.h>
#include <time.h>
#include <string.h>
#include <strings.h>
#include <ctype.h>
#include "utils.h"
#include "fatstruct.h"
#include "fathelper.h"
#include "disk.h"
extern uint8_t boot_sector[DISK_BLOCK_SIZE];
void struct_check()
{
//check structure sizes are correct
assert(sizeof(fat_bpb_t) == 25);
assert(sizeof(fat_ebpb_t) == 26);
assert(sizeof(fat_bs_t) == DISK_BLOCK_SIZE);
assert(sizeof(fat_file_t) == 32);
assert(sizeof(fat_attr_t) == 1);
assert(sizeof(fat_date_t) == 2);
assert(sizeof(fat_time_t) == 2);
//check that the machine is little endian
int i = 1;
assert((int)*((unsigned char *)&i)==1);
//make sure int range is reasonable
assert(sizeof(int) >= 4);
return;
}
void print_bs()
{
fat_bs_t bs_struct;
memcpy(&bs_struct, &boot_sector, sizeof(boot_sector));
debug_printf("jump opcodes: %02x %02x %02x\n",
bs_struct.jump[0], bs_struct.jump[1], bs_struct.jump[2]);
debug_printf("oem name: %.8s\n", bs_struct.oem);
debug_printf("bytes per sector: %u\n", bs_struct.bpb.bytes_sector);
debug_printf("sectors per cluster: %u\n", bs_struct.bpb.sectors_cluster);
debug_printf("reserved sectors: %u\n", bs_struct.bpb.reserved_sectors);
debug_printf("number of FATs: %u\n", bs_struct.bpb.fats);
debug_printf("root directory entries: %u\n", bs_struct.bpb.root_entries);
debug_printf("sectors per volume: %u\n", bs_struct.bpb.sectors_volume);
debug_printf("media descriptor: %02x\n", bs_struct.bpb.mdt);
debug_printf("sectors per FAT: %u\n", bs_struct.bpb.sectors_fat);
debug_printf("sectors per track: %u\n", bs_struct.bpb.sectors_track);
debug_printf("heads: %u\n", bs_struct.bpb.heads);
debug_printf("hidden sectors: %u\n", bs_struct.bpb.hidden_sectors);
debug_printf("huge sectors per volume: %u\n", bs_struct.bpb.huge_sectors_volume);
debug_printf("drive number: %u\n", bs_struct.ebpb.drive_num);
debug_printf("windows nt flags: %u\n", bs_struct.ebpb.nt_flags);
debug_printf("ebpb signature: %u\n", bs_struct.ebpb.signature);
debug_printf("volume serial number: %u\n", bs_struct.ebpb.volume_id);
debug_printf("volume label: %.11s\n", bs_struct.ebpb.volume_label);
debug_printf("FAT type: %.8s\n", bs_struct.ebpb.fat_type_label);
debug_printf("boot sector signature: %02x %02x\n",
bs_struct.signature[0], bs_struct.signature[1]);
return;
}
int number_of_fats()
{
fat_bs_t bs_struct;
memcpy(&bs_struct, &boot_sector, sizeof(boot_sector));
return bs_struct.bpb.fats;
}
int bytes_sector()
{
fat_bs_t bs_struct;
memcpy(&bs_struct, &boot_sector, sizeof(boot_sector));
int b_s = bs_struct.bpb.bytes_sector;
assert(b_s == DISK_BLOCK_SIZE);
return b_s;
}
int sectors_cluster()
{
fat_bs_t bs_struct;
memcpy(&bs_struct, &boot_sector, sizeof(boot_sector));
return bs_struct.bpb.sectors_cluster;
}
int sectors_fat()
{
fat_bs_t bs_struct;
memcpy(&bs_struct, &boot_sector, sizeof(boot_sector));
return bs_struct.bpb.sectors_fat;
}
int start_of_fat()
{
fat_bs_t bs_struct;
memcpy(&bs_struct, &boot_sector, sizeof(boot_sector));
return bs_struct.bpb.reserved_sectors;
}
int root_dir_entries()
{
fat_bs_t bs_struct;
memcpy(&bs_struct, &boot_sector, sizeof(boot_sector));
return bs_struct.bpb.root_entries;
}
int start_of_root_dir()
{
fat_bs_t bs_struct;
memcpy(&bs_struct, &boot_sector, sizeof(boot_sector));
return start_of_fat() + bs_struct.bpb.fats * bs_struct.bpb.sectors_fat;
}
int start_of_data()
{
return start_of_root_dir() + root_dir_sectors();
}
int dir_entries_sector()
{
// printf("~~~~~bytes_sector() = %d \n",bytes_sector());
// printf("~~~~~(int)sizeof(fat_file_t)() = %d \n",(int)sizeof(fat_file_t));
return bytes_sector() / (int)sizeof(fat_file_t);
}
int root_dir_sectors()
{
fat_bs_t bs_struct;
memcpy(&bs_struct, &boot_sector, sizeof(boot_sector));
return bs_struct.bpb.root_entries * (int)sizeof(fat_file_t) /
bs_struct.bpb.bytes_sector;
}
int data_cluster_to_sector(uint16_t cluster)
{
fat_bs_t bs_struct;
memcpy(&bs_struct, &boot_sector, sizeof(boot_sector));
// printf(" start_of_data() %d \n",start_of_data());
// printf(" cluster %d \n",cluster);
// printf(" FAT_CLUSTER_OFFSET %d \n",FAT_CLUSTER_OFFSET);
// printf(" bs_struct.bpb.sectors_cluster %d \n",bs_struct.bpb.sectors_cluster);
int sector = start_of_data() +
(((int)cluster - FAT_CLUSTER_OFFSET) * bs_struct.bpb.sectors_cluster);
return sector;
}
int sector_to_data_cluster(int sector)
{
int data_sector = sector - start_of_data();
int data_cluster = data_sector / sectors_cluster();
return data_cluster + FAT_CLUSTER_OFFSET;
}
int highest_cluster()
{
int disk_bytes = disk_size();
int disk_sectors = disk_bytes / bytes_sector();
int data_sectors = disk_sectors - start_of_data();
int data_clusters = data_sectors / sectors_cluster();
int total_clusters = FAT_CLUSTER_OFFSET + data_clusters;
return total_clusters;
}
bool is_lfn(fat_attr_t attr)
{
uint8_t attr_int;
memcpy(&attr_int, &attr, sizeof(uint8_t));
if(attr_int == 0x0f)
{
return true;
}
return false;
}
void print_attributes(fat_attr_t attr)
{
uint8_t attr_int;
memcpy(&attr_int, &attr, sizeof(uint8_t));
char letters[] = "rhsvdadu";
for(int i = 0; i < 8; ++i)
{
if(attr_int & (1<<i))
{
printf("%c", letters[i]);
}
else
{
printf("-");
}
}
return;
}
fat_date_t date_now()
{
fat_date_t d;
time_t now;
now = time(NULL);
struct tm *now_tm = localtime(&now);
d.day = now_tm->tm_mday;
d.month = now_tm->tm_mon;
d.year = now_tm->tm_year - 80;
return d;
}
uint8_t fine_time_now()
{
uint8_t t;
time_t now;
now = time(NULL);
struct tm *now_tm = localtime(&now);
t = (now_tm->tm_sec % 2) * 100;
return t;
}
fat_time_t time_now()
{
fat_time_t t;
time_t now;
now = time(NULL);
struct tm *now_tm = localtime(&now);
t.hour = now_tm->tm_hour;
t.min = now_tm->tm_min;
t.sec = now_tm->tm_sec / 2;
return t;
}
uint16_t next_cluster(uint16_t current)
{
int fat_start = start_of_fat();
//FAT lookup
int fat_offset = (int)current * (int)sizeof(uint16_t);
int fat_sector = fat_start + (fat_offset / bytes_sector());
int fat_entry_sector = current % (bytes_sector() / sizeof(uint16_t));
uint16_t fat_piece[bytes_sector() / (int)sizeof(uint16_t)];
read_block(fat_sector, &fat_piece);
uint16_t fat_entry = fat_piece[fat_entry_sector];
return fat_entry;
}
uint16_t next_cluster_for_sector(int current)
{
int cluster = sector_to_data_cluster(current);
uint16_t next_c = next_cluster((uint16_t)cluster);
return next_c;
}
int chain_length(uint16_t start_cluster)
{
uint16_t current = start_cluster;
int length = 1;
uint16_t next = next_cluster(current);
while(next <= max_cluster && next >= min_cluster)
{
current = next;
length++;
next = next_cluster(current);
}
return length;
}
int write_fat_entry(uint16_t entry, uint16_t value)
{
int fat_entries_sector = bytes_sector() / (int)sizeof(uint16_t);
int sector = entry / fat_entries_sector;
int entry_offset = (int)entry % fat_entries_sector;
uint8_t fat_bytes[bytes_sector()];
read_block(start_of_fat() + sector, &fat_bytes);
uint16_t fat_entries[fat_entries_sector];
memcpy(&fat_entries, &fat_bytes, (size_t)bytes_sector());
fat_entries[entry_offset] = value;
memcpy(&fat_bytes, &fat_entries, (size_t)bytes_sector());
//multiple FATs
for(int i = 0; i < number_of_fats(); ++i)
{
write_block(start_of_fat() + (i * sectors_fat()) + sector,
&fat_bytes);
}
return 0;
}
int read_file_entry(fat_file_t* file, int directory_sector, int entry_number)
{
uint8_t dir_bytes[bytes_sector()];
read_block(directory_sector, &dir_bytes);
fat_file_t dir_entries[dir_entries_sector()];
memcpy(&dir_entries, &dir_bytes, (size_t)bytes_sector());
*file = dir_entries[entry_number];
return 0;
}
int write_file_entry(fat_file_t file, int directory_sector, int entry_number)
{
uint8_t dir_bytes[bytes_sector()];
read_block(directory_sector, &dir_bytes);
fat_file_t dir_entries[dir_entries_sector()];
memcpy(&dir_entries, &dir_bytes, (size_t)bytes_sector());
dir_entries[entry_number] = file;
memcpy(&dir_bytes, &dir_entries, (size_t)bytes_sector());
write_block(directory_sector, &dir_bytes);
return 0;
}
int name_to_83(char* whole_name, unsigned char* name, unsigned char* ext)
{
//prefill with spaces
for(int i = 0; i < FAT_FILE_LEN; ++i)
{
name[i] = ' ';
}
for(int i = 0; i < FAT_EXT_LEN; ++i)
{
ext[i] = ' ';
}
bool name_part = true;
int name_ctr = 0;
int ext_ctr = 0;
char current_dir[] = ".";
if(strncmp(whole_name, current_dir, 11) == 0)
{
name[0] = '.';
return 0;
}
char parent_dir[] = "..";
if(strncmp(whole_name, parent_dir, 11) == 0)
{
name[0] = '.';
name[1] = '.';
return 0;
}
for(int i = 0; i < FAT_FILE_LEN + FAT_EXT_LEN + 1; ++i)
{
if(whole_name[i] == '\0')
{
break;
}
if(whole_name[i] == '.')
{
name_part = false;
}
else if(isupper(whole_name[i]) || isdigit(whole_name[i]))
{
if(name_part && name_ctr < FAT_FILE_LEN)
{
name[name_ctr] = (unsigned char)whole_name[i];
name_ctr++;
}
else if(!name_part && ext_ctr < FAT_EXT_LEN)
{
ext[ext_ctr] = (unsigned char)whole_name[i];
ext_ctr++;
}
}
}
if(name_ctr == 0)
{
return -1;
}
return 0;
}
int compare_filename(char* name, fat_file_t dir_entry)
{
unsigned char name_name[FAT_FILE_LEN+1];
unsigned char name_ext[FAT_EXT_LEN+1];
name_name[FAT_FILE_LEN] = '\0';
name_ext[FAT_EXT_LEN] = '\0';
name_to_83(name, name_name, name_ext);
for(int i = 0; i < FAT_FILE_LEN; ++i)
{
if(dir_entry.name[i] != name_name[i])
{
return 1;
}
}
for(int i = 0; i < FAT_EXT_LEN; ++i)
{
if(dir_entry.ext[i] != name_ext[i])
{
return 1;
}
}
return 0;
}
//returns sector number where directory is located, negative = error
int dir_lookup(char* path)
{
char dname[MAX_PATH_LEN+1];
dname[MAX_PATH_LEN] = '\0';
strncpy(dname, path, MAX_PATH_LEN);
char* dname_part = NULL;
dname_part = strtok(dname, "/");
int dlocation = start_of_root_dir();
//special case for root directory
char root_dir_dname[] = ".";
if(strcmp(path, root_dir_dname) == 0)
{
return start_of_root_dir();
}
bool found = false;
//traverse the directory structure
while(dname_part != NULL)
{
found = false;
debug_printf("looking for directory %s\n", dname_part);
// printf(" ====looking for directory %s\n", dname_part);
// printf(" dir_entries_sector() = %d \n",dir_entries_sector());
while(!found)
{
//read sector into a memory block
uint8_t dir_sector[bytes_sector()];
read_block(dlocation, &dir_sector);
fat_file_t dir_files[dir_entries_sector()];
memcpy(&dir_files, &dir_sector, (size_t)bytes_sector());
//search the memory block for matching name
for(int i = 0; i < dir_entries_sector(); ++i)
{
if(dir_files[i].name[0] == 0x00)
{
//not found, don't need to keep looking
// printf(" //not found, don't need to keep looking");
break;
}
else if(dir_files[i].name[0] == deleted_file)
{
//deleted
continue;
}
else if(is_lfn(dir_files[i].attr))
{
//LFN entry
continue;
}
else if(dir_files[i].attr.vol == 1 ||
dir_files[i].attr.dir == 0 ||
dir_files[i].attr.device == 1)
{
//volume label, not a directory, device file
continue;
}
if(compare_filename(dname_part, dir_files[i]) == 0)
{
//got a directory with the correct name
dlocation = data_cluster_to_sector(dir_files[i].first_cluster);
// printf(" dir_files[i].first_cluster: %d \n", dir_files[i].first_cluster);
found = true;
break;
}
}
if(found)
{
break;
}
//directory not found yet, need to pick the next sector to search
//(if available)
if(dlocation < start_of_data())
{
if(dlocation < start_of_root_dir() + root_dir_sectors())
{
//there are more root directory sectors to search
dlocation++;
}
else
{
//directory doesn't exist
return -1;
}
}
else
{
if((dlocation + 1 - start_of_data()) % sectors_cluster() != 0)
{
//more sectors in this cluster
dlocation++;
}
else if(next_cluster_for_sector(dlocation) <= max_cluster)
{
//continues into another cluster
uint16_t next_c = next_cluster_for_sector(dlocation);
dlocation = data_cluster_to_sector(next_c);
}
else
{
//directory doesn't exist
return -1;
}
}
}
// printf(" dlocation: %d \n", dlocation);
dname_part = strtok(NULL, "/");
}
return dlocation;
}
//returns file entry number in directory or -1 for not found
int file_lookup(char* name, int *directory_sector)
{
int dlocation = *directory_sector;
bool root_dir = true;
if(dlocation >= start_of_data())
{
root_dir = false;
}
debug_printf("looking for file %s\n", name);
while(true)
{
uint8_t dir_sector[bytes_sector()];
read_block(dlocation, &dir_sector);
fat_file_t dir_files[dir_entries_sector()];
memcpy(&dir_files, &dir_sector, (size_t)bytes_sector());
//search the memory block for matching name
for(int i = 0; i < dir_entries_sector(); ++i)
{
if(dir_files[i].name[0] == 0x00)
{
//not found, don't need to keep looking
break;
}
else if(dir_files[i].name[0] == deleted_file)
{
//deleted
continue;
}
else if(is_lfn(dir_files[i].attr))
{
//LFN entry
continue;
}
else if(dir_files[i].attr.vol == 1 ||
dir_files[i].attr.device == 1)
{
//volume label, device file
continue;
}
if(compare_filename(name, dir_files[i]) == 0)
{
//got a directory with the correct name
*directory_sector = dlocation;
return i;
}
}
//file not found yet, need to pick the next sector to search
//if there is actually a next sector
if(root_dir)
{
if(dlocation < start_of_root_dir() + root_dir_sectors())
{
//there are more root directory sectors to search
dlocation++;
}
else
{
//file doesn't exist
return -1;
}
}
else
{
if((dlocation + 1 - start_of_data()) % sectors_cluster() != 0)
{
//more sectors to search in this cluster
dlocation++;
}
else if(next_cluster_for_sector(dlocation) <= max_cluster)
{
//continues into another cluster
uint16_t next_c = next_cluster_for_sector(dlocation);
dlocation = data_cluster_to_sector(next_c);
}
else
{
return -1;
}
}
}
return -1;
}