-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfat32.c
1429 lines (1138 loc) · 37.1 KB
/
fat32.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
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//
// fat32.c
//
#include <ultra64.h>
#include <math.h>
#include "fat32.h"
#include "ci.h"
unsigned char buffer[512];
unsigned char buffer_2[512];
unsigned char fat_message[64];
unsigned char fat_buffer[512];
uint32_t fat_buffer_sector = -1;
int fat_buffer_dirty = 0;
uint32_t dir_buffer_sector = 0;
int dir_buffer_dirty = 0;
uint32_t fs_begin_sector;
typedef struct _fat_fs_t {
uint32_t begin_sector;
uint32_t sect_per_clus;
uint32_t num_fats;
uint32_t sect_per_fat;
uint32_t root_cluster;
uint32_t clus_begin_sector;
uint32_t total_clusters;
uint32_t free_clusters;
} fat_fs_t;
fat_fs_t fat_fs;
/// 2-byte number
uint16_t shortEndian(unsigned char *i)
{
int t = ((int)*(i+1)<<8) |
((int)*(i+0));
return t;
}
void writeShort(unsigned char *dest, uint16_t val) {
*(uint16_t *)dest = shortEndian((unsigned char *)&val);
}
// 4-byte number
uint32_t intEndian(unsigned char *i)
{
uint32_t t = ((int)*(i+3)<<24) |
((int)*(i+2)<<16) |
((int)*(i+1)<<8) |
((int)*(i+0));
return t;
}
void writeInt(unsigned char *dest, uint32_t val) {
*(uint32_t *)dest = intEndian((unsigned char *)&val);
}
/************************
* CONSTANTS AND MACROS *
************************/
// first sector of a cluster
#define CLUSTER_TO_SECTOR(X) ( fat_fs.clus_begin_sector + (X - 2) * fat_fs.sect_per_clus )
// dirents per sector
#define DE_PER_SECTOR (512 / 32)
uint32_t fat_cluster_to_sector(uint32_t clus)
{
return CLUSTER_TO_SECTOR(clus);
}
uint32_t fat_sect_per_clus()
{
return fat_fs.sect_per_clus;
}
int fat_init()
{
char fat_systemid[8];
uint32_t fat_num_resv_sect;
uint32_t total_sectors, data_offset;
// read first sector
ciReadSector(buffer, 0);
// check for MBR/VBR magic
if( !(buffer[0x1fe]==0x55 && buffer[0x1ff]==0xAA) ){
sprintf(fat_message, "No memory card / bad FS");
return 1;
}
/*
// now speed things up!
if(compat_mode)
{
cfSetCycleTime(30);
}else{
cfOptimizeCycleTime();
}
*/
// look for 'FAT'
if(strncmp((char *)&buffer[82], "FAT", 3) == 0)
{
// this first sector is a Volume Boot Record
fs_begin_sector = 0;
}else{
// this is a MBR. Read first entry from partition table
fs_begin_sector = intEndian(&buffer[0x1c6]);
}
ciReadSector(buffer, fs_begin_sector);
// copy the system ID string
memcpy(fat_systemid, &buffer[82], 8);
fat_systemid[7] = 0;
if(strncmp(fat_systemid, "FAT32", 5) != 0){
// not a fat32 volume
sprintf(fat_message, "FAT32 partition not found.");
return 1;
}
fat_fs.sect_per_clus = buffer[0x0d];
fat_num_resv_sect = shortEndian(&buffer[0x0e]);
fat_fs.num_fats = buffer[0x10];
fat_fs.sect_per_fat = intEndian(&buffer[0x24]);
fat_fs.root_cluster = intEndian(&buffer[0x2c]);
fat_fs.begin_sector = fs_begin_sector + fat_num_resv_sect;
data_offset = fat_num_resv_sect + (fat_fs.num_fats * fat_fs.sect_per_fat);
fat_fs.clus_begin_sector = fs_begin_sector + data_offset;
total_sectors = intEndian(&buffer[0x20]);
fat_fs.total_clusters = (total_sectors - data_offset) / fat_fs.sect_per_clus;
//
// Load free cluster count
//
ciReadSector(buffer, fs_begin_sector + 1);
fat_fs.free_clusters = intEndian(&buffer[0x1e8]);
return 0;
}
/**
* Get the relative sector # and offset into the sector for a given cluster.
*/
void fat_sector_offset(uint32_t cluster, uint32_t *fat_sector, uint32_t *fat_offset) {
uint32_t index = cluster * 4; // each cluster is 4 bytes long
uint32_t sector = index / 512; // 512 bytes per sector, rounds down
*fat_sector = sector;
*fat_offset = index - sector * 512;
}
/**
* Get the absolute number of a relative fat sector for a given fat.
*/
static uint32_t _fat_absolute_sector(uint32_t relative_sector, int fat_num) {
return fat_fs.begin_sector + (fat_num * fat_fs.sect_per_fat) + relative_sector;
}
// flush changes to the fat
static void _fat_flush_fat(void) {
uint32_t sector, i;
unsigned char fs_info[512];
uint32_t old_free;
if (fat_buffer_dirty) {
// write the dirty sector to each copy of the FAT
for (i = 0; i < fat_fs.num_fats; ++i) {
sector = _fat_absolute_sector(fat_buffer_sector, i);
ciWriteSector(fat_buffer, sector);
}
fat_buffer_dirty = 0;
}
//
// Write free cluster count
//
ciReadSector(fs_info, fs_begin_sector + 1);
old_free = intEndian(&fs_info[0x1e8]);
if (old_free != fat_fs.free_clusters) {
writeInt(&fs_info[0x1e8], fat_fs.free_clusters);
ciWriteSector(fs_info, fs_begin_sector + 1);
}
}
/**
* Load the sector for a FAT cluster, return offset into sector.
*/
uint32_t _fat_load_fat(uint32_t cluster) {
uint32_t relative_sector, offset;
// get the sector of the FAT and offset into the sector
fat_sector_offset(cluster, &relative_sector, &offset);
// only read sector if it has changed! saves time
if (relative_sector != fat_buffer_sector) {
// flush pending writes
_fat_flush_fat();
// read the sector
ciReadSector(fat_buffer, _fat_absolute_sector(relative_sector, 0));
fat_buffer_sector = relative_sector;
}
return offset;
}
/**
* Get the FAT entry for a given cluster.
*/
uint32_t fat_get_fat(uint32_t cluster) {
uint32_t offset = _fat_load_fat(cluster);
return intEndian(&fat_buffer[offset]);
}
/**
* Set the FAT entry for a given cluster.
*/
void fat_set_fat(uint32_t cluster, uint32_t value) {
uint32_t offset = _fat_load_fat(cluster);
writeInt(&fat_buffer[offset], value);
fat_buffer_dirty = 1;
}
/**
* Find the first unused entry in the FAT.
*
* Returns:
* FAT_SUCCESS on success, with new entry in new_entry
* FAT_NOSPACE if it can't find an unused cluster
*/
static int _fat_find_free_entry(uint32_t start, uint32_t *new_entry) {
uint32_t entry = 1;
uint32_t num_entries = fat_fs.total_clusters + 2; // 2 unused entries at the start of the FAT
if (start > 0)
entry = start;
while (entry < num_entries && fat_get_fat(entry) != 0)
++entry;
// if we reach the end, loop back to the beginning and try to find an unused entry
if (entry == num_entries) {
entry = 1;
while (entry < start && fat_get_fat(entry) != 0)
++entry;
if (entry == start)
return FAT_NOSPACE;
}
*new_entry = entry;
return FAT_SUCCESS;
}
/**
* Get the root directory entry.
*/
int fat_root_dirent(fat_dirent *dirent) {
dirent->short_name[0] = 0;
dirent->long_name[0] = 0;
dirent->directory = 0;
dirent->start_cluster = 0;
dirent->size = 0;
dirent->index = 0;
dirent->cluster = fat_fs.root_cluster;
dirent->sector = 0;
dirent->first_cluster = fat_fs.root_cluster;
return 0;
}
/*
* Returns a dirent for the directory starting at start_cluster.
*/
void fat_sub_dirent(uint32_t start_cluster, fat_dirent *de) {
fat_root_dirent(de);
de->cluster = de->first_cluster = start_cluster;
}
/**
* Flush pending changes to a directory.
*/
static void _fat_flush_dir(void) {
if (dir_buffer_dirty) {
ciWriteSector(buffer, dir_buffer_sector);
dir_buffer_dirty = 0;
}
}
/**
* Read a sector from a directory. Automatically handles buffering and flushing
* changes to dirty buffers.
*/
static void _dir_read_sector(uint32_t sector) {
if (sector != dir_buffer_sector) {
// flush pending writes
_fat_flush_dir();
ciReadSector(buffer, sector);
dir_buffer_sector = sector;
}
}
/**
* Load the current sector pointed to by the dirent. Automatically loads new
* sectors and clusters.
* Returns 0 if next cluster is 0x0ffffff8.
* Returns 1 on success.
*/
static int _fat_load_dir_sector(fat_dirent *dirent) {
uint32_t sector;
uint32_t fat_entry;
if (dirent->index == DE_PER_SECTOR) {
// load the next cluster once we reach the end of this
if (dirent->sector + 1 == fat_fs.sect_per_clus) {
// look up the cluster number in the FAT
fat_entry = fat_get_fat(dirent->cluster);
if (fat_entry >= 0x0ffffff8) // last cluster
return 0; // end of dir
dirent->cluster = fat_entry;
dirent->sector = 0;
dirent->index = 0;
}else {
++dirent->sector;
dirent->index = 0;
}
}
// sector may or may not have changed, but buffering makes this efficient
sector = CLUSTER_TO_SECTOR(dirent->cluster) + dirent->sector;
////////////////////////////////////////////////////////////////////////////////////
dirent->phy_sector = sector;
////////////////////////////////////////////////////////////////////////////////////
_dir_read_sector(sector);
return 1;
}
//////////////////////////////////////////////////////////
/**
* Delete a file, given its directory entry.
* returns:
* 1 success
* -1 error (usually INCONSISTENT)
*/
int fat_deletefile(fat_dirent *dirent) {
int ret, i;
lfn_entry_t *lfn;
// zero out the file's fat chain
ret = fat_set_size(dirent, 0);
if(ret != FAT_SUCCESS){
return -1;
}
// mark out all of its directory entries
for(i = 0; i < dirent->num_lfn_entries; i++){
if(i > MAX_LFN){
// unpossible!
return -1;
}
lfn = &dirent->lfn[i];
_dir_read_sector(lfn->sector);
buffer[lfn->offset] = 0xE5;
dir_buffer_dirty = 1;
}
_fat_flush_dir();
return 1;
}
//////////////////////////////////////////////////////////
/**
* Read a directory.
* returns:
* 1 success
* 0 end of dir
* -1 error
*/
int fat_readdir(fat_dirent *dirent) {
int found_file = 0;
int ret, i, j;
uint32_t offset;
uint32_t attributes;
uint32_t segment;
char *dest;
//////////////////////////////////////////////////////////
lfn_entry_t *lfn;
//////////////////////////////////////////////////////////
if (dirent->index > DE_PER_SECTOR) {
sprintf(fat_message, "Invalid directory");
return -1;
}
//////////////////////////////////////////////////////////
dirent->num_lfn_entries = 0;
//////////////////////////////////////////////////////////
if (dirent->long_name)
memset(dirent->long_name, 0, 256);
do {
ret = _fat_load_dir_sector(dirent);
if (ret == 0) // end of dir
return 0;
offset = dirent->index * 32;
// end of directory reached
if (buffer[offset] == 0)
return 0;
++dirent->index;
// deleted file, skip
if (buffer[offset] == 0xe5)
continue;
attributes = buffer[offset + 0x0b];
// long filename, copy the bytes and move along
if (attributes == 0x0f) {
segment = (buffer[offset] & 0x1F) - 1;
if (segment < 0 || segment > 19)
continue; // invalid segment
dest = (dirent->long_name + segment * 13);
for (i = 0; i < 5; ++i)
dest[i] = buffer[offset + 1 + i * 2];
for (j = 0; j < 3; ++j)
dest[i+j] = buffer[offset + 0xe + j * 2];
// last segment can only have 9 characters
if (segment == 19) {
dest[i+j] = 0;
continue;
}
for ( ; j < 6; ++j)
dest[i+j] = buffer[offset + 0xe + j * 2];
i += j;
for (j = 0; j < 2; ++j)
dest[i+j] = buffer[offset + 0x1c + j * 2];
//////////////////////////////////////////////////////////
// add a lfn entry and set up the values
lfn = &dirent->lfn[dirent->num_lfn_entries++];
lfn->offset = offset;
lfn->sector = dirent->phy_sector;
lfn->cluster = dirent->cluster;
//////////////////////////////////////////////////////////
continue;
}
dirent->directory = attributes & 0x10 ? 1 : 0;
dirent->volume_label = attributes & 0x08 ? 1 : 0;
dirent->hidden = attributes & 0x02 ? 1 : 0;
// you can thank FAT16 for this
dirent->start_cluster = shortEndian(buffer + offset + 0x14) << 16;
dirent->start_cluster |= shortEndian(buffer + offset + 0x1a);
dirent->size = intEndian(buffer + offset + 0x1c);
// copy the name
memcpy(dirent->short_name, buffer + offset, 8);
//////////////////////////////////////////////////////////
// add a lfn entry for the shortname
lfn = &dirent->lfn[dirent->num_lfn_entries++];
lfn->offset = offset;
lfn->sector = dirent->phy_sector;
lfn->cluster = dirent->cluster;
//////////////////////////////////////////////////////////
// kill trailing space
for (i = 8; i > 0 && dirent->short_name[i-1] == ' '; --i)
;
// get the extension
dirent->short_name[i++] = '.';
memcpy(dirent->short_name + i, buffer + offset + 8, 3);
// kill trailing space
for (j = 3; j > 0 && dirent->short_name[i+j-1] == ' '; --j)
;
// hack! kill the . if there's no extension
if (j == 0) --j;
dirent->short_name[i+j] = 0;
found_file = 1;
}
while (!found_file);
if (dirent->long_name[0] != '\0')
dirent->name = dirent->long_name;
else
dirent->name = dirent->short_name;
return 1;
}
void fat_rewind(fat_dirent *dirent) {
// reset the metadata to point to the beginning of the dir
dirent->index = 0;
dirent->cluster = dirent->first_cluster;
dirent->sector = 0;
}
/**
* Get an array of all the sectors a file owns. Returns all sectors of all
* clusters even if the file does not occupy all the sectors (i.e., a 512 byte
* file will still return 4 sectors in a 4 sector-per-cluster FS).
*
* Return 0 on succes, -1 on fail
* Only failure mode is if the size of the sectors array is too small.
*/
int fat_get_sectors(uint32_t start_cluster, uint32_t *sectors, int size) {
int i, num = 0;
uint32_t cluster, sector;
cluster = start_cluster;
while (cluster < 0x0ffffff8) {
sector = CLUSTER_TO_SECTOR(cluster);
for (i = 0; i < fat_fs.sect_per_clus; ++i) {
// make sure we have enough space for the sectors
if (num >= size)
return -1;
sectors[num++] = sector + i;
}
cluster = fat_get_fat(cluster);
}
return 0;
}
/**
* Return the sector and offset into the sector of a file given start cluster
* and offset.
*
* Returns:
* FAT_SUCCESS on success
* FAT_EOF when the end of file is reached
*/
int fat_get_sector(uint32_t start_cluster, uint32_t offset, uint32_t *sector, uint32_t *new_offset)
{
uint32_t bytes_per_clus = fat_fs.sect_per_clus * 512;
uint32_t cluster = start_cluster;
// skip to the right cluster
while (offset >= bytes_per_clus) {
cluster = fat_get_fat(cluster);
// hit the end of the file
if (cluster >= 0x0ffffff7)
return FAT_EOF;
offset -= bytes_per_clus;
}
*sector = CLUSTER_TO_SECTOR(cluster);
while (offset >= 512) {
++*sector;
offset -= 512;
}
*new_offset = offset;
return FAT_SUCCESS;
}
/**
* Allocate a new cluster after the last cluster. Sets the end of file marker
* in the FAT as a bonus.
*
* If last_cluster is 0, it assumes this is the first cluster in the file and
* WILL NOT set the FAT entry on cluster 0.
*
* Returns:
* FAT_SUCCESS on success, new cluster in new_cluster
* FAT_NOSPACE when the FS has no free clusters
* FAT_INCONSISTENT when the fs needs to be checked
*/
static int _fat_allocate_cluster(uint32_t last_cluster, uint32_t *new_cluster) {
int ret;
uint32_t new_last;
if (fat_fs.free_clusters == 0)
return FAT_NOSPACE;
ret = _fat_find_free_entry(last_cluster, &new_last);
// according to the free cluster count, we should be able to find a free
// cluster. since _fat_find_free_entry couldn't, this means the FS must be
// in an inconistent state
if (ret == FAT_NOSPACE)
return FAT_INCONSISTENT;
// see comment above for explanation
if (last_cluster != 0)
fat_set_fat(last_cluster, new_last);
fat_set_fat(new_last, 0x0ffffff8);
--fat_fs.free_clusters;
*new_cluster = new_last;
return FAT_SUCCESS;
}
/**
* Fills a cluster with 0's.
*/
static void _fat_clear_cluster(uint32_t cluster) {
unsigned char clear_buffer[512] = { 0, };
uint32_t i, sector = CLUSTER_TO_SECTOR(cluster);
for (i = 0; i < fat_fs.sect_per_clus; ++i)
ciWriteSector(clear_buffer, sector + i);
}
/**
* Fills a cluster with FF's.
*/
static void _fat_clear_ff_cluster(uint32_t cluster) {
unsigned char clear_buffer[512] = { 0xff, };
uint32_t i, sector = CLUSTER_TO_SECTOR(cluster);
for (i = 0; i < fat_fs.sect_per_clus; ++i)
ciWriteSector(clear_buffer, sector + i);
}
/**
* Calculate remaining dirents in a directory at EOD.
* If the dirent isn't EOD, this value is bogus.
*/
static int _fat_remaining_dirents(fat_dirent *dirent) {
int remaining;
uint32_t cluster = dirent->cluster;
// DE_PER_SECTOR for each unused sector in the cluster
remaining = DE_PER_SECTOR * (fat_fs.sect_per_clus - (dirent->sector + 1));
// remaining in the current sector
remaining += DE_PER_SECTOR - dirent->index;
// add the rest of the clusters in the directory
while ((cluster = fat_get_fat(cluster)) < 0x0ffffff7)
remaining += DE_PER_SECTOR * fat_fs.sect_per_clus;
return remaining;
}
/**
* Allocate a bunch of dirents at the end of a directory. If there aren't
* enough available, allocate a new cluster.
*
* Preconditions:
* dirent is at end of directory
* count >= 0
*
* Return:
* FAT_SUCCESS success
* FAT_NOSPACE file system full
*/
static int _fat_allocate_dirents(fat_dirent *dirent, int count) {
int remaining, ret;
uint32_t cluster;
remaining = _fat_remaining_dirents(dirent);
if (count > remaining) {
ret = _fat_allocate_cluster(dirent->cluster, &cluster);
if (ret == FAT_NOSPACE)
return FAT_NOSPACE;
_fat_flush_fat();
_fat_clear_cluster(cluster);
}
return FAT_SUCCESS;
}
/**
* Find a file by name in a directory. Create it if it doesn't exist.
* Initialize the first cluster of a directory. Creates the . and .. entries.
*/
static void _fat_init_dir(uint32_t cluster, uint32_t parent) {
unsigned char buf[512] = { 0, };
char name[11];
uint32_t i, sector = CLUSTER_TO_SECTOR(cluster);
uint16_t top16, bottom16;
memset(name, 0x20, sizeof(name));
// FAT32 hack: if parent is root, cluster should be 0
if (parent == fat_fs.root_cluster)
parent = 0;
//
// .
//
name[0] = '.';
memcpy(&buf[0], name, sizeof(name));
buf[11] = 0x10; // dir
// current dir start cluster
top16 = (cluster >> 16 & 0xffff);
bottom16 = (cluster & 0xffff);
writeShort(&buf[0x14], top16);
writeShort(&buf[0x1a], bottom16);
//
// ..
//
name[1] = '.';
memcpy(&buf[32], name, sizeof(name));
buf[32+11] = 0x10; // dir
// parent start cluster
top16 = (parent >> 16 & 0xffff);
bottom16 = (parent & 0xffff);
writeShort(&buf[32 + 0x14], top16);
writeShort(&buf[32 + 0x1a], bottom16);
//
// write first sector
//
ciWriteSector(buf, sector);
//
// zero the rest of the sectors
//
memset(buf, 0, 32 * 2);
for (i = 1; i < fat_fs.sect_per_clus; ++i)
ciWriteSector(buf, sector + i);
}
static const unsigned char charmap[] = {
'\000', '\001', '\002', '\003', '\004', '\005', '\006', '\007',
'\010', '\011', '\012', '\013', '\014', '\015', '\016', '\017',
'\020', '\021', '\022', '\023', '\024', '\025', '\026', '\027',
'\030', '\031', '\032', '\033', '\034', '\035', '\036', '\037',
'\040', '\041', '\042', '\043', '\044', '\045', '\046', '\047',
'\050', '\051', '\052', '\053', '\054', '\055', '\056', '\057',
'\060', '\061', '\062', '\063', '\064', '\065', '\066', '\067',
'\070', '\071', '\072', '\073', '\074', '\075', '\076', '\077',
'\100', '\141', '\142', '\143', '\144', '\145', '\146', '\147',
'\150', '\151', '\152', '\153', '\154', '\155', '\156', '\157',
'\160', '\161', '\162', '\163', '\164', '\165', '\166', '\167',
'\170', '\171', '\172', '\133', '\134', '\135', '\136', '\137',
'\140', '\141', '\142', '\143', '\144', '\145', '\146', '\147',
'\150', '\151', '\152', '\153', '\154', '\155', '\156', '\157',
'\160', '\161', '\162', '\163', '\164', '\165', '\166', '\167',
'\170', '\171', '\172', '\173', '\174', '\175', '\176', '\177',
'\200', '\201', '\202', '\203', '\204', '\205', '\206', '\207',
'\210', '\211', '\212', '\213', '\214', '\215', '\216', '\217',
'\220', '\221', '\222', '\223', '\224', '\225', '\226', '\227',
'\230', '\231', '\232', '\233', '\234', '\235', '\236', '\237',
'\240', '\241', '\242', '\243', '\244', '\245', '\246', '\247',
'\250', '\251', '\252', '\253', '\254', '\255', '\256', '\257',
'\260', '\261', '\262', '\263', '\264', '\265', '\266', '\267',
'\270', '\271', '\272', '\273', '\274', '\275', '\276', '\277',
'\300', '\301', '\302', '\303', '\304', '\305', '\306', '\307',
'\310', '\311', '\312', '\313', '\314', '\315', '\316', '\317',
'\320', '\321', '\322', '\323', '\324', '\325', '\326', '\327',
'\330', '\331', '\332', '\333', '\334', '\335', '\336', '\337',
'\340', '\341', '\342', '\343', '\344', '\345', '\346', '\347',
'\350', '\351', '\352', '\353', '\354', '\355', '\356', '\357',
'\360', '\361', '\362', '\363', '\364', '\365', '\366', '\367',
'\370', '\371', '\372', '\373', '\374', '\375', '\376', '\377',
};
/*
* strcasecmp --
* Do strcmp(3) in a case-insensitive manner.
*
* PUBLIC: #ifndef HAVE_STRCASECMP
* PUBLIC: int strcasecmp __P((const char *, const char *));
* PUBLIC: #endif
*/
int strcasecmp(s1, s2)
const char *s1, *s2;
{
register const unsigned char *cm = charmap,
*us1 = (const unsigned char *)s1,
*us2 = (const unsigned char *)s2;
while (cm[*us1] == cm[*us2++])
if (*us1++ == '\0')
return (0);
return (cm[*us1] - cm[*--us2]);
}
/**
* Find a file by name in a directory. Create it if it doesn't exist.
* Return:
* FAT_SUCCESS success
* FAT_NOSPACE file system full
*/
int fat_find_create(char *filename, fat_dirent *folder, fat_dirent *result_de, int dir, int create) {
int ret, segment, i, num_dirents, total_clusters;
int len;
char long_name[256];
char short_name[12];
char segment_chars[26];
unsigned char crc, *buf;
uint16_t date_field, time_field;
uint32_t start_cluster;
//
// Try to find the file in the dir, return it if found
//
while ((ret = fat_readdir(folder)) > 0)
if (strcasecmp(filename, folder->name) == 0) {
// found, return it
*result_de = *folder;
return FAT_SUCCESS;
}
if(!create){
return FAT_NOEXIST;
}
//
// File not found. Create a new file
//
// short name is a random alphabetic string
for (i = 0; i < 11; ++i)
short_name[i] = 'A' + (rand() % ('Z' - 'A' + 1));
short_name[11] = 0; // for display purposes
// calc the CRC
crc = 0;
for (i = 0; i < 11; ++i)
crc = ((crc<<7) | (crc>>1)) + short_name[i];
// trunc long name to 255 bytes
memset(long_name, 0, 255);
strncpy(long_name, filename, 255);
long_name[255] = '\0';
len = strlen(long_name);
num_dirents = 1; // short filename
num_dirents += (len - 1) / 13 + 1; // long filename
total_clusters = 0;
// add a cluster if we have to extend the directory for new dirents
if (num_dirents > _fat_remaining_dirents(folder))
++total_clusters;
// add a cluster if we're creating a dir
if (dir)
++total_clusters;
// make sure we've got enough room
if (fat_fs.free_clusters < total_clusters)
return FAT_NOSPACE;
ret = _fat_allocate_dirents(folder, num_dirents);
if (ret == FAT_NOSPACE)
return FAT_INCONSISTENT;
// since we may have allocated a new cluster we must reload the dir
_fat_load_dir_sector(folder);
*result_de = *folder;
// copy it 13 bytes at a time
for (segment = len / 13; segment >= 0; --segment) {
memset(segment_chars, 0, 26);
// copy up to (and including) the first ASCII NUL
for (i = 0; i < 13; ++i) {
segment_chars[2*i] = long_name[segment * 13 + i];
if (segment_chars[2*i] == '\0') {
++i; // make sure we FF after the \0
break;
}
}
// 0xFF the rest
for ( ; i < 13; ++i) {
segment_chars[2*i] = 0xff;
segment_chars[2*i+1] = 0xff;
}
buf = &buffer[folder->index * 32];
memset(buf, 0, 32);
memcpy(&buf[1], &segment_chars[0], 10);
memcpy(&buf[14], &segment_chars[10], 12);
memcpy(&buf[28], &segment_chars[22], 4);
buf[0] = (segment + 1) | ((segment == len / 13) << 6);
buf[11] = 0x0f;
buf[13] = crc;
dir_buffer_dirty = 1;
_fat_flush_fat();
// TODO check for inconsistency here
++folder->index;
_fat_load_dir_sector(folder);
}
//
// 8.3 dirent
//
buf = &buffer[folder->index * 32];
memset(buf, 0, 32);
// copy the short name
memcpy(buf, short_name, 11);
// directory: allocate the first cluster and init it
if (dir) {
uint16_t top16, bottom16;
// attribute: archive and dir flags
buf[11] = 0x30;
ret = _fat_allocate_cluster(0, &start_cluster);
if (ret == FAT_NOSPACE)
return FAT_INCONSISTENT;
_fat_init_dir(start_cluster, folder->first_cluster);
//////////////////////////////////////////////////////////////////
// MARSHALLH ADD DEC 31 2010
//////////////////////////////////////////////////////////////////
// Unless you flush the changes, the directory entry will be added
// but the cluster will not show as allocated in the FATs
//////////////////////////////////////////////////////////////////
_fat_flush_fat();
// start cluster
top16 = (start_cluster >> 16 & 0xffff);
writeShort(&buf[0x14], top16);
bottom16 = (start_cluster & 0xffff);
writeShort(&buf[0x1a], bottom16);
}
// regular file
else {
// attribute: archive
buf[11] = 0x20;
}