-
Notifications
You must be signed in to change notification settings - Fork 39
/
fat.c
1455 lines (1259 loc) · 61.9 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
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
/*
Copyright 2005, 2006, 2007 Dennis van Weeren
Copyright 2008, 2009 Jakub Bednarski
This file is part of Minimig
Minimig is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
Minimig is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
This is a simple FAT16 handler. It works on a sector basis to allow fastest acces on disk
images.
11-12-2005 - first version, ported from FAT1618.C
JB:
2008-10-11 - added SeekFile() and cluster_mask
- limited file create and write support added
2009-05-01 - modified LoadDirectory() and GetDirEntry() to support sub-directories (with limitation of 511 files/subdirs per directory)
- added GetFATLink() function
- code cleanup
2009-05-03 - modified sorting algorithm in LoadDirectory() to display sub-directories above files
2009-08-23 - modified ScanDirectory() to support page scrolling and parent dir selection
2009-11-22 - modified FileSeek()
- added FileReadEx()
2009-12-15 - all entries are now sorted by name with extension
- directory short names are displayed with extensions
*/
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include "mmc.h"
#include "fat.h"
#include "swap.h"
#include "usb.h"
#include "fpga.h"
//
typedef unsigned char (*rw_func_t)(unsigned long, unsigned char *);
typedef unsigned char (*rw_multiple_func_t)(unsigned long, unsigned char *, unsigned long);
unsigned short directory_cluster; // first cluster of directory (0 if root)
unsigned short entries_per_cluster; // number of directory entries per cluster
// internal global variables
unsigned char fattype; // volume format
unsigned char fat32; // volume format is FAT32
uint32_t boot_sector; // partition boot sector
uint32_t fat_start; // start LBA of first FAT table
uint32_t data_start; // start LBA of data field
uint32_t root_directory_cluster; // root directory cluster (used in FAT32)
uint32_t root_directory_start; // start LBA of directory table
uint32_t root_directory_size; // size of directory region in sectors
unsigned char fat_number; // number of FAT tables
unsigned char cluster_size; // size of a cluster in sectors
uint32_t cluster_mask; // binary mask of cluster number
unsigned short dir_entries; // number of entry's in directory table
uint32_t fat_size; // size of fat
unsigned short info_sector; // fat32 info sector
struct PartitionEntry partitions[4]; // lbastart and sectors will be byteswapped as necessary
int partitioncount;
unsigned char sector_buffer[512*SECTOR_BUFFER_SIZE]; // sector buffer
FATBUFFER fat_buffer; // buffer for caching fat entries
uint32_t buffered_fat_index; // index of buffered FAT sector
char DirEntryLFN[MAXDIRENTRIES][261];
DIRENTRY DirEntry[MAXDIRENTRIES];
unsigned char sort_table[MAXDIRENTRIES];
unsigned char nDirEntries = 0; // entries in DirEntry table
unsigned char iSelectedEntry = 0; // selected entry index
uint32_t iCurrentDirectory = 0; // cluster number of current directory, 0 for root
uint32_t iPreviousDirectory = 0; // cluster number of previous directory
// temporary storage buffers
char t_DirEntryLFN[MAXDIRENTRIES][261];
DIRENTRY t_DirEntry[MAXDIRENTRIES];
unsigned char t_sort_table[MAXDIRENTRIES];
// external functions
extern unsigned long GetTimer(unsigned long);
extern void ErrorMessage(const char *message, unsigned char code);
// current read/write functions
rw_func_t lread = MMC_Read;
rw_multiple_func_t lread_multiple = MMC_ReadMultiple;
rw_func_t lwrite = MMC_Write;
int8_t fat_uses_mmc(void) {
return(lread == MMC_Read);
}
int8_t fat_medium_present() {
if(lread == MMC_Read)
return MMC_CheckCard();
#ifdef USB_STORAGE
return(storage_devices > 0);
#else
return(0);
#endif
}
#ifdef USB_STORAGE
void fat_switch_to_usb(void) {
lread = usb_storage_read;
// TODO: lread_multiple
lwrite = usb_storage_write;
}
#endif
// return fat sector index of a given cluster entry
#define CLUSTER2SECTOR(c) ((c) >> (fat32?7:8))
// return byte offset withing sector of a given cluster entry
#define CLUSTER2OFFSET(c) ((c) & (fat32?0x7f:0xff))
#pragma section_code_init
// read sector of FAT if not already in the buffer
RAMFUNC static char FatLoad(uint32_t index) {
// load fat sector
if(buffered_fat_index != index) {
if (!lread(fat_start + index, (unsigned char*)&fat_buffer)) {
iprintf("FAT read failed!\n");
return(0);
}
// remember current buffer index
buffered_fat_index = index;
}
return 1;
}
RAMFUNC uint32_t NextCluster(uint32_t cluster) {
uint32_t fat_index = CLUSTER2SECTOR(cluster);
unsigned short buffer_index = CLUSTER2OFFSET(cluster);
if(!FatLoad(fat_index)) return(0);
if (fat32)
return SwapBBBB(fat_buffer.fat32[buffer_index]) & 0x0FFFFFFF; // get FAT32 link
return SwapBB(fat_buffer.fat16[buffer_index]); // get FAT16 link
}
#pragma section_no_code_init
// FindDrive() checks if a card is present and contains FAT formatted primary partition
unsigned char FindDrive(void) {
buffered_fat_index = -1;
fat32 = 0;
if (!lread(0, sector_buffer)) // read MBR
return(0);
boot_sector=0;
partitioncount=1;
// If we can identify a filesystem on block 0 we don't look for partitions
if (strncmp((const char*)§or_buffer[0x36], "FAT16 ", 8)==0) // check for FAT16
partitioncount=0;
if (strncmp((const char*)§or_buffer[0x52], "FAT32 ", 8)==0) // check for FAT32
partitioncount=0;
iprintf("partitioncount = %d\n", partitioncount);
if(partitioncount) {
// We have at least one partition, parse the MBR.
struct MasterBootRecord *mbr=(struct MasterBootRecord *)sector_buffer;
memcpy(&partitions[0],&mbr->Partition[0],sizeof(struct PartitionEntry));
memcpy(&partitions[1],&mbr->Partition[1],sizeof(struct PartitionEntry));
memcpy(&partitions[2],&mbr->Partition[2],sizeof(struct PartitionEntry));
memcpy(&partitions[3],&mbr->Partition[3],sizeof(struct PartitionEntry));
if(mbr->Signature == 0xaa55) {
// get start of first partition
boot_sector = partitions[0].startlba;
iprintf("Start: %ld\n",partitions[0].startlba);
for(partitioncount=4;(partitions[partitioncount-1].sectors==0) && (partitioncount>1); --partitioncount);
iprintf("PartitionCount: %d\n",partitioncount);
int i;
for(i=0;i<partitioncount;++i) {
iprintf("Partition: %d",i);
iprintf(" Start: %ld",partitions[i].startlba);
iprintf(" Size: %ld\n",partitions[i].sectors);
}
if (!lread(boot_sector, sector_buffer)) // read discriptor
return(0);
BootPrint("Read boot sector from first partition\n");
} else
BootPrint("No partition signature found\n");
}
if (strncmp((const char*)§or_buffer[0x36], "FAT16 ", 8)==0) // check for FAT16
fattype = 16;
if (strncmp((const char*)§or_buffer[0x52], "FAT32 ", 8)==0) // check for FAT32
fattype = 32;
iprintf("partition type: 0x%02X (", sector_buffer[450]);
switch (fattype) {
case 0:
iprintf("NONE");
break;
case 12:
iprintf("FAT12");
break;
case 16:
iprintf("FAT16");
break;
case 32:
iprintf("FAT32");
fat32 = 1;
break;
default:
iprintf("UNKNOWN");
break;
}
iprintf(")\n");
if (fattype != 32 && fattype != 16) { // first partition filesystem type: FAT16 or FAT32
iprintf("Unsupported partition type!\n");
return(0);
}
if (sector_buffer[510] != 0x55 || sector_buffer[511] != 0xaa) // check signature
return(0);
// check for near-jump or short-jump opcode
if (sector_buffer[0] != 0xe9 && sector_buffer[0] != 0xeb)
return(0);
// check if blocksize is really 512 bytes
if (sector_buffer[11] != 0x00 || sector_buffer[12] != 0x02)
return(0);
// check medium descriptor byte, must be 0xf8 for hard drive
if (sector_buffer[21] != 0xf8)
return(0);
if (fat32) {
if (strncmp((const char*)§or_buffer[0x52], "FAT32 ", 8) != 0) // check file system type
return(0);
cluster_size = sector_buffer[0x0D]; // get cluster_size in sectors
cluster_mask = ~(cluster_size - 1); // calculate cluster mask
dir_entries = cluster_size << 4; // total number of dir entries (16 entries per sector)
root_directory_size = cluster_size; // root directory size in sectors
fat_start = boot_sector + sector_buffer[0x0E] + (sector_buffer[0x0F] << 8); // reserved sector count before FAT table (usually 32 for FAT32)
fat_number = sector_buffer[0x10];
fat_size = sector_buffer[0x24] + (sector_buffer[0x25] << 8) + (sector_buffer[0x26] << 16) + (sector_buffer[0x27] << 24);
data_start = fat_start + (fat_number * fat_size);
root_directory_cluster = sector_buffer[0x2C] + (sector_buffer[0x2D] << 8) + (sector_buffer[0x2E] << 16) + ((sector_buffer[0x2F] & 0x0F) << 24);
root_directory_start = (root_directory_cluster - 2) * cluster_size + data_start;
info_sector = boot_sector + (sector_buffer[0x30] + (sector_buffer[0x31] << 8));
} else {
// calculate drive's parameters from bootsector, first up is size of directory
dir_entries = sector_buffer[17] + (sector_buffer[18] << 8);
root_directory_size = ((dir_entries << 5) + 511) >> 9;
// calculate start of FAT,size of FAT and number of FAT's
fat_start = boot_sector + sector_buffer[14] + (sector_buffer[15] << 8);
fat_size = sector_buffer[22] + (sector_buffer[23] << 8);
fat_number = sector_buffer[16];
// calculate start of directory
root_directory_start = fat_start + (fat_number * fat_size);
root_directory_cluster = 0; // unused
// get cluster_size
cluster_size = sector_buffer[13];
// calculate cluster mask
cluster_mask = ~(cluster_size - 1);
// calculate start of data
data_start = root_directory_start + root_directory_size;
// fat16 does not have a info sector
info_sector = 0;
}
// some debug output
iprintf("fat_size: %lu\n", fat_size);
iprintf("fat_number: %u\n", fat_number);
iprintf("fat_start: %lu\n", fat_start);
iprintf("root_directory_start: %lu\n", root_directory_start);
iprintf("dir_entries: %u\n", dir_entries);
iprintf("data_start: %lu\n", data_start);
iprintf("cluster_size: %u\n", cluster_size);
iprintf("cluster_mask: %08lX\n", cluster_mask);
return(1);
}
unsigned char FileOpenDir(fileTYPE *file, const char *name, unsigned long iDirectory) {
DIRENTRY *pEntry = NULL; // pointer to current entry in sector buffer
unsigned long iDirectorySector; // current sector of directory entries table
unsigned long iDirectoryCluster; // start cluster of subdirectory or FAT32 root directory
unsigned long iEntry; // entry index in directory cluster or FAT16 root directory
unsigned long nEntries; // number of entries per cluster or FAT16 root directory size
if (iDirectory) { // subdirectory
iDirectoryCluster = iDirectory;
iDirectorySector = data_start + cluster_size * (iDirectoryCluster - 2);
nEntries = cluster_size << 4; // 16 entries per sector
} else { // root directory
iDirectoryCluster = root_directory_cluster;
iDirectorySector = root_directory_start;
nEntries = fat32 ? cluster_size << 4 : root_directory_size << 4; // 16 entries per sector
}
while (1) {
for (iEntry = 0; iEntry < nEntries; iEntry++) {
if ((iEntry & 0x0F) == 0) { // first entry in sector, load the sector
lread(iDirectorySector++, sector_buffer); // root directory is linear
pEntry = (DIRENTRY*)sector_buffer;
} else
pEntry++;
if (pEntry->Name[0] != SLOT_EMPTY && pEntry->Name[0] != SLOT_DELETED) // valid entry??
{
if (!(pEntry->Attributes & (ATTR_VOLUME | ATTR_DIRECTORY))) // not a volume nor directory
{
// iprintf("check %.11s %.11s\n", pEntry->Name, name);
if (strncmp((const char*)pEntry->Name, name, sizeof(file->name)) == 0)
{
strncpy(file->name, (const char*)pEntry->Name, sizeof(file->name));
file->attributes = pEntry->Attributes;
// file->size = pEntry->FileSize;
file->size = SwapBBBB(pEntry->FileSize); // for 68000
// file->size = ((file->size>>24)&0xFF)|((file->size>>8)&0xFF00)|((file->size<<8)&0xFF0000)|((file->size<<24)&0xFF000000); // for 68000
// file->start_cluster = pEntry->StartCluster + (fat32 ? (pEntry->HighCluster & 0x0FFF) << 16 : 0); // it only works when using little endian long representation
file->start_cluster = SwapBB(pEntry->StartCluster) + (fat32 ? (SwapBB(pEntry->HighCluster) & 0x0FFF) << 16 : 0); // it only works when using little endian long representation for 68000
// file->start_cluster = (((pEntry->StartCluster>>8)&0xFF)|((pEntry->StartCluster<<8)&0xFF00)) | (fat32 ? ((pEntry->HighCluster & 0x0F) << 24)|((pEntry->HighCluster & 0xFF00) << 8) : 0); // for 68000
file->cluster = file->start_cluster;
file->sector = 0;
file->cluster_change = 0;
file->entry.sector = iDirectorySector - 1;
file->entry.index = iEntry & 0x0F;
// iprintf("file \"%s\" found\n", name);
return(1);
}
}
}
}
if (iDirectory || fat32) // subdirectory is a linked cluster chain
{
iDirectoryCluster = NextCluster(iDirectoryCluster); // get next cluster in chain
if (fat32 ? (iDirectoryCluster & 0x0FFFFFF8) == 0x0FFFFFF8 : (iDirectoryCluster & 0xFFF8) == 0xFFF8) // check if end of cluster chain
break; // no more clusters in chain
iDirectorySector = data_start + cluster_size * (iDirectoryCluster - 2); // calculate first sector address of the new cluster
}
else
break;
}
iprintf("file \"%s\" not found\n", name);
memset(file, 0, sizeof(fileTYPE));
return(0);
}
unsigned char FileOpen(fileTYPE *file, const char *name) {
return FileOpenDir(file, name, 0);
}
unsigned char lfn_checksum(unsigned char *pName)
{
unsigned char i = 11;
unsigned char checksum = 0;
while (i--)
checksum = ((checksum & 1) << 7) + (checksum >> 1) + *pName++;
return checksum;
}
int _strnicmp(const char *s1, const char *s2, size_t n)
{
char c1, c2;
int v;
do
{
c1 = *s1++;
c2 = *s2++;
v = (unsigned int)tolower(c1) - (unsigned int)tolower(c2);
}
while (v == 0 && c1 != '\0' && --n > 0);
return v;
}
int CompareDirEntries(DIRENTRY *pDirEntry1, char *pLFN1, DIRENTRY *pDirEntry2, char *pLFN2)
{
const char *pStr1, *pStr2;
int len;
int rc;
if ((pDirEntry2->Attributes & ATTR_DIRECTORY)
&& !(pDirEntry1->Attributes & ATTR_DIRECTORY) // directories first
|| (pDirEntry2->Name[0] == '.' && pDirEntry2->Name[1] == '.')) // parent directory entry at top
return 1;
if ((pDirEntry1->Attributes & ATTR_DIRECTORY)
&& !(pDirEntry2->Attributes & ATTR_DIRECTORY) // directories first
|| (pDirEntry1->Name[0] == '.' && pDirEntry1->Name[1] == '.')) // parent directory entry at top
return -1;
len = 260;
if (*pLFN1)
pStr1 = pLFN1;
else
{
pStr1 = (const char*)pDirEntry1->Name;
len = 11;
}
if (*pLFN2)
pStr2 = pLFN2;
else
{
pStr2 = (const char*)pDirEntry2->Name;
len = 11;
}
rc = _strnicmp(pStr1, pStr2, len);
if (rc == 0) // it might happen that both strings are equal when one is a long name and other not
{
if (*pLFN1)
{
if (!*pLFN2) // first string long, second short
rc = 1;
}
else
{
if (*pLFN2) // first string short, second long
rc = -1;
}
}
return(rc);
}
int compareExt(char *fileExt, char *extension)
{
int found = 0;
while(!found && *extension)
{
found = 1;
for (int i = 0; i < 3; i++)
{
if (extension[i] == '?') continue;
if (tolower(extension[i]) != tolower(fileExt[i])) found = 0;
}
if (strlen(extension) < 3) break;
extension += 3;
}
return found;
}
char ScanDirectory(unsigned long mode, char *extension, unsigned char options) {
DIRENTRY *pEntry = NULL; // pointer to current entry in sector buffer
unsigned long iDirectorySector; // current sector of directory entries table
unsigned long iDirectoryCluster;
unsigned long iEntry; // entry index in directory
unsigned long nEntries;
char i;
unsigned char x;
unsigned char nNewEntries = 0; // indicates if a new entry has been found (used in scroll mode)
char rc = 0; //return code
char find_file = 0;
char find_dir = 0;
char is_file = 0;
unsigned char sequence_number = 0;
unsigned char name_checksum = 0;
unsigned char prev_sequence_number = 0;
unsigned char prev_name_checksum = 0;
char *ptr;
static char lfn[261];
unsigned char lfn_error = 1;
/*
unsigned long time;
time = GetTimer(0);
*/
lfn[0] = 0;
if (mode == SCAN_INIT)
{
nDirEntries = 0;
iSelectedEntry = 0;
for (i = 0; i < MAXDIRENTRIES; i++)
sort_table[i] = i;
}
else
{
if (nDirEntries == 0) // directory is empty so there is no point in searching for any entry
return 0;
if (mode == SCAN_NEXT)
{
if (iSelectedEntry + 1 < nDirEntries) // scroll within visible items
{
iSelectedEntry++;
return 0;
}
if (nDirEntries < MAXDIRENTRIES)
return 0;
}
else if (mode == SCAN_PREV)
{
if (iSelectedEntry > 0) // scroll within visible items
{
iSelectedEntry--;
return 0;
}
}
else if (mode ==SCAN_NEXT_PAGE)
{
if (iSelectedEntry + 1 < nDirEntries)
{
iSelectedEntry = nDirEntries - 1;
return 0;
}
if (nDirEntries < MAXDIRENTRIES)
return 0;
}
else if (mode == SCAN_PREV_PAGE)
{
if (iSelectedEntry)
{
iSelectedEntry = 0;
return 0;
}
}
find_file = options & FIND_FILE;
find_dir = options & FIND_DIR;
}
if (iCurrentDirectory) // subdirectory
{
iDirectoryCluster = iCurrentDirectory;
iDirectorySector = data_start + cluster_size * (iDirectoryCluster - 2);
nEntries = cluster_size << 4; // 16 entries per sector
}
else // root directory
{
iDirectoryCluster = root_directory_cluster;
iDirectorySector = root_directory_start;
nEntries = fat32 ? cluster_size << 4 : root_directory_size << 4; // 16 entries per sector
}
while (1)
{
for (iEntry = 0; iEntry < nEntries; iEntry++)
{
if ((iEntry & (SECTOR_BUFFER_SIZE * 16 - 1)) == 0) // first entry in sector buffer, load the sector
{
lread_multiple(iDirectorySector, sector_buffer, SECTOR_BUFFER_SIZE);
iDirectorySector += SECTOR_BUFFER_SIZE;
pEntry = (DIRENTRY*)sector_buffer;
for (i = 0; i < (SECTOR_BUFFER_SIZE * 16); i++)
{
if (pEntry->Attributes != ATTR_LFN)
{
pEntry->StartCluster = SwapBB(pEntry->StartCluster);
pEntry->HighCluster = SwapBB(pEntry->HighCluster);
pEntry->FileSize = SwapBBBB(pEntry->FileSize);
}
pEntry++;
}
pEntry = (DIRENTRY*)sector_buffer;
}
else
pEntry++;
if (pEntry->Name[0] != SLOT_EMPTY && pEntry->Name[0] != SLOT_DELETED) // valid entry??
{
if (pEntry->Attributes == ATTR_LFN) // long file name entry
{
if (options & SCAN_LFN)
{
sequence_number = ((unsigned char*)pEntry)[0];
name_checksum = ((unsigned char*)pEntry)[13];
ptr = &lfn[((sequence_number & 0x1F) - 1) * 13];
if (sequence_number & 0x40)
lfn_error = 0;
else
if ((sequence_number & 0x1F) != (prev_sequence_number & 0x1F) - 1 || name_checksum != prev_name_checksum || (sequence_number & 0x1F) > sizeof(lfn) / 13 - 1)
lfn_error = 1;
prev_sequence_number = sequence_number;
prev_name_checksum = name_checksum;
if (!lfn_error)
{
*ptr++ = ((unsigned char*)pEntry)[1];
*ptr++ = ((unsigned char*)pEntry)[3];
*ptr++ = ((unsigned char*)pEntry)[5];
*ptr++ = ((unsigned char*)pEntry)[7];
*ptr++ = ((unsigned char*)pEntry)[9]; // first 5 characters
*ptr++ = ((unsigned char*)pEntry)[14];
*ptr++ = ((unsigned char*)pEntry)[16];
*ptr++ = ((unsigned char*)pEntry)[18];
*ptr++ = ((unsigned char*)pEntry)[20];
*ptr++ = ((unsigned char*)pEntry)[22];
*ptr++ = ((unsigned char*)pEntry)[24]; // next 6 characters
*ptr++ = ((unsigned char*)pEntry)[28];
*ptr++ = ((unsigned char*)pEntry)[30]; // last 2 characters
if (sequence_number & 0x40) // last lfn part
*ptr++ = 0;
}
else
iprintf("LFN error!\n");
}
}
else // if not an LFN entry
{
is_file = ~pEntry->Attributes & ATTR_DIRECTORY;
if (!(pEntry->Attributes & (ATTR_VOLUME | ATTR_HIDDEN)) && (pEntry->Name[0] != '.' || pEntry->Name[1] != ' ')) // if not VOLUME label (also filter current directory entry)
{
if ((extension[0] == '*')
|| compareExt(&pEntry->Name[8], extension)
|| (options & SCAN_DIR && pEntry->Attributes & ATTR_DIRECTORY)
|| (options & SCAN_SYSDIR && pEntry->Attributes & ATTR_DIRECTORY && ((pEntry->Name[0] == '.' && pEntry->Name[1] == '.') || pEntry->Attributes & ATTR_SYSTEM)))
{
if (mode == SCAN_INIT)
{ // scan the directory table and return first MAXDIRENTRIES alphabetically sorted entries
if (nDirEntries < MAXDIRENTRIES) // initial directory scan (first 8 entries)
{
DirEntry[nDirEntries] = *pEntry; // add new entry at first empty slot in storage buffer
DirEntryLFN[nDirEntries][0] = 0;
if (lfn[0])
if (lfn_checksum(pEntry->Name) == name_checksum)
strncpy(DirEntryLFN[nDirEntries], lfn, sizeof(lfn));
nDirEntries++;
}
else
{
if (CompareDirEntries(pEntry, lfn, &DirEntry[sort_table[MAXDIRENTRIES-1]], DirEntryLFN[sort_table[MAXDIRENTRIES-1]]) < 0) // compare new entry with the last already found
{
DirEntry[sort_table[MAXDIRENTRIES-1]] = *pEntry; // replace the last entry with the new one if appropriate
DirEntryLFN[sort_table[MAXDIRENTRIES-1]][0] = 0;
if (lfn[0])
if (lfn_checksum(pEntry->Name) == name_checksum)
strncpy(DirEntryLFN[sort_table[MAXDIRENTRIES-1]], lfn, sizeof(lfn));
}
}
for (i = nDirEntries - 1; i > 0; i--) // one pass bubble-sorting (table is already sorted, only the new item must be placed in order)
{
if (CompareDirEntries(&DirEntry[sort_table[i]], DirEntryLFN[sort_table[i]], &DirEntry[sort_table[i-1]], DirEntryLFN[sort_table[i-1]])<0) // compare items and swap if necessary
{
x = sort_table[i];
sort_table[i] = sort_table[i-1];
sort_table[i-1] = x;
}
else
break; // don't check further entries as they are already sorted
}
}
else if (mode == SCAN_INIT_FIRST)
{ // find a dir entry with given cluster number and store it in the buffer
if (pEntry->StartCluster + (fat32 ? (pEntry->HighCluster & 0x0FFF) << 16 : 0) == iPreviousDirectory)
{ // directory entry found
for (i = 0; i< MAXDIRENTRIES; i++)
sort_table[i] = i; // init sorting table
nDirEntries = 1;
iSelectedEntry = 0;
DirEntry[0] = *pEntry; // add the entry at the top of the buffer
DirEntryLFN[0][0] = 0;
if (lfn[0])
if (lfn_checksum(pEntry->Name) == name_checksum)
strncpy(DirEntryLFN[0], lfn, sizeof(lfn));
rc = 1; // indicate to the caller that the directory entry has been found
}
}
else if (mode == SCAN_INIT_NEXT)
{ // scan the directory table and return next MAXDIRENTRIES-1 alphabetically sorted entries (first entry is in the buffer)
if (CompareDirEntries(pEntry, lfn, &DirEntry[sort_table[0]], DirEntryLFN[sort_table[0]]) > 0) // compare new entry with the first one
{
if (nDirEntries < MAXDIRENTRIES) // initial directory scan (first 8 entries)
{
DirEntry[nDirEntries] = *pEntry; // add new entry at first empty slot in storage buffer
DirEntryLFN[nDirEntries][0] = 0;
if (lfn[0])
if (lfn_checksum(pEntry->Name) == name_checksum)
strncpy(DirEntryLFN[nDirEntries], lfn, sizeof(lfn));
nDirEntries++;
}
else
{
if (CompareDirEntries(pEntry, lfn, &DirEntry[sort_table[MAXDIRENTRIES-1]], DirEntryLFN[sort_table[MAXDIRENTRIES-1]]) < 0) // compare new entry with the last already found
{
DirEntry[sort_table[MAXDIRENTRIES-1]] = *pEntry; // replace the last entry with the new one if appropriate
DirEntryLFN[sort_table[MAXDIRENTRIES-1]][0] = 0;
if (lfn[0])
if (lfn_checksum(pEntry->Name) == name_checksum)
strncpy(DirEntryLFN[sort_table[MAXDIRENTRIES-1]], lfn, sizeof(lfn));
}
}
for (i = nDirEntries - 1; i > 0; i--) // one pass bubble-sorting (table is already sorted, only the new item must be placed in order)
{
if (CompareDirEntries(&DirEntry[sort_table[i]], DirEntryLFN[sort_table[i]], &DirEntry[sort_table[i-1]], DirEntryLFN[sort_table[i-1]])<0) // compare items and swap if necessary
{
x = sort_table[i];
sort_table[i] = sort_table[i-1];
sort_table[i-1] = x;
}
else
break; // don't check further entries as they are already sorted
}
}
}
else if (mode == SCAN_NEXT) // replace the last dir entry with the new (higher) one
{
if (nNewEntries == 0) // no entry higher than the last one has been found yet
{
if (CompareDirEntries(pEntry, lfn, &DirEntry[sort_table[MAXDIRENTRIES-1]], DirEntryLFN[sort_table[MAXDIRENTRIES-1]]) > 0) // found entry higher than the last one
{
nNewEntries++;
DirEntry[sort_table[0]] = *pEntry; // replace the first entry with the found one
DirEntryLFN[sort_table[0]][0] = 0;
if (lfn[0])
if (lfn_checksum(pEntry->Name) == name_checksum)
strncpy(DirEntryLFN[sort_table[0]], lfn, sizeof(lfn));
// scroll entries' indices
x = sort_table[0];
for (i = 0; i < MAXDIRENTRIES-1; i++)
sort_table[i] = sort_table[i+1];
sort_table[MAXDIRENTRIES-1] = x; // last entry is the found one
}
}
else // higher entry already found but we need to check the remaining ones if any of them is lower then the already found one
{
// check if the found entry is lower than the last one and higher than the last but one, if so then replace the last one with it
if (CompareDirEntries(pEntry, lfn, &DirEntry[sort_table[MAXDIRENTRIES-1]], DirEntryLFN[sort_table[MAXDIRENTRIES-1]]) < 0)
if (CompareDirEntries(pEntry, lfn, &DirEntry[sort_table[MAXDIRENTRIES-2]], DirEntryLFN[sort_table[MAXDIRENTRIES-2]]) > 0)
{
DirEntry[sort_table[MAXDIRENTRIES-1]] = *pEntry;
DirEntryLFN[sort_table[MAXDIRENTRIES-1]][0] = 0;
if (lfn[0])
if (lfn_checksum(pEntry->Name) == name_checksum)
strncpy(DirEntryLFN[sort_table[MAXDIRENTRIES-1]], lfn, sizeof(lfn));
}
}
}
else if (mode == SCAN_PREV) // replace the first dir entry with the new (lower) one
{
if (nNewEntries == 0) // no entry lower than the first one has been found yet
{
if (CompareDirEntries(pEntry, lfn, &DirEntry[sort_table[0]], DirEntryLFN[sort_table[0]]) < 0) // found entry lower than the first one
{
nNewEntries++;
if (nDirEntries < MAXDIRENTRIES)
{
//sort_table[nDirEntries] = nDirEntries; // init sorting table
nDirEntries++;
}
DirEntry[sort_table[MAXDIRENTRIES-1]] = *pEntry; // replace the last entry with the found one
DirEntryLFN[sort_table[MAXDIRENTRIES-1]][0] = 0;
if (lfn[0])
if (lfn_checksum(pEntry->Name) == name_checksum)
strncpy(DirEntryLFN[sort_table[MAXDIRENTRIES-1]], lfn, sizeof(lfn));
// scroll entries' indices
x = sort_table[MAXDIRENTRIES-1];
for (i = MAXDIRENTRIES - 1; i > 0; i--)
sort_table[i] = sort_table[i-1];
sort_table[0] = x; // the first entry is the found one
}
}
else // lower entry already found but we need to check the remaining ones if any of them is higher then the already found one
{
// check if the found entry is higher than the first one and lower than the second one, if so then replace the first one with it
if (CompareDirEntries(pEntry, lfn, &DirEntry[sort_table[0]], DirEntryLFN[sort_table[0]]) > 0)
if (CompareDirEntries(pEntry, lfn, &DirEntry[sort_table[1]], DirEntryLFN[sort_table[1]]) < 0)
{
DirEntry[sort_table[0]] = *pEntry;
DirEntryLFN[sort_table[0]][0] = 0;
if (lfn[0])
if (lfn_checksum(pEntry->Name) == name_checksum)
strncpy(DirEntryLFN[sort_table[0]], lfn, sizeof(lfn));
}
}
}
else if (mode == SCAN_NEXT_PAGE) // find next 8 entries
{
if (CompareDirEntries(pEntry, lfn, &DirEntry[sort_table[MAXDIRENTRIES-1]], DirEntryLFN[sort_table[MAXDIRENTRIES-1]]) > 0) // compare with the last visible entry
{
if (nNewEntries < MAXDIRENTRIES) // initial directory scan (first 8 entries)
{
t_DirEntry[nNewEntries] = *pEntry; // add new entry at first empty slot in storage buffer
t_DirEntryLFN[nNewEntries][0] = 0;
if (lfn[0])
if (lfn_checksum(pEntry->Name) == name_checksum)
strncpy(t_DirEntryLFN[nNewEntries], lfn, sizeof(lfn));
t_sort_table[nNewEntries] = nNewEntries; // init sorting table
nNewEntries++;
}
else
{
if (CompareDirEntries(pEntry, lfn, &t_DirEntry[t_sort_table[MAXDIRENTRIES-1]], t_DirEntryLFN[t_sort_table[MAXDIRENTRIES-1]]) < 0) // compare new entry with the last already found
{
t_DirEntry[t_sort_table[MAXDIRENTRIES-1]] = *pEntry; // replace the last entry with the new one if appropriate
t_DirEntryLFN[t_sort_table[MAXDIRENTRIES-1]][0] = 0;
if (lfn[0])
if (lfn_checksum(pEntry->Name) == name_checksum)
strncpy(t_DirEntryLFN[t_sort_table[MAXDIRENTRIES-1]], lfn, sizeof(lfn));
}
}
for (i = nNewEntries - 1; i > 0; i--) // one pass bubble-sorting (table is already sorted, only the new item must be placed in order)
{
if (CompareDirEntries(&t_DirEntry[t_sort_table[i]], t_DirEntryLFN[t_sort_table[i]], &t_DirEntry[t_sort_table[i-1]], t_DirEntryLFN[t_sort_table[i-1]]) < 0) // compare items and swap if necessary
{
x = t_sort_table[i];
t_sort_table[i] = t_sort_table[i-1];
t_sort_table[i-1] = x;
}
else
break; // don't check further entries as they are already sorted
}
}
}
else if (mode == SCAN_PREV_PAGE) // find next 8 entries
{
if (CompareDirEntries(pEntry, lfn, &DirEntry[sort_table[0]], DirEntryLFN[sort_table[0]]) < 0) // compare with the last visible entry
{
if (nNewEntries < MAXDIRENTRIES) // initial directory scan (first 8 entries)
{
t_DirEntry[nNewEntries] = *pEntry; // add new entry at first empty slot in storage buffer
t_DirEntryLFN[nNewEntries][0] = 0;
if (lfn[0])
if (lfn_checksum(pEntry->Name) == name_checksum)
strncpy(t_DirEntryLFN[nNewEntries], lfn, sizeof(lfn));
t_sort_table[nNewEntries] = nNewEntries; // init sorting table
nNewEntries++;
}
else
{
if (CompareDirEntries(pEntry, lfn, &t_DirEntry[t_sort_table[MAXDIRENTRIES-1]], t_DirEntryLFN[t_sort_table[MAXDIRENTRIES-1]]) > 0) // compare new entry with the last already found
{
t_DirEntry[t_sort_table[MAXDIRENTRIES-1]] = *pEntry; // replace the last entry with the new one if appropriate
t_DirEntryLFN[t_sort_table[MAXDIRENTRIES-1]][0] = 0;
if (lfn[0])
if (lfn_checksum(pEntry->Name) == name_checksum)
strncpy(t_DirEntryLFN[t_sort_table[MAXDIRENTRIES-1]], lfn, sizeof(lfn));
}
}
for (i = nNewEntries - 1; i > 0; i--) // one pass bubble-sorting (table is already sorted, only the new item must be placed in order)
{
if (CompareDirEntries(&t_DirEntry[t_sort_table[i]], t_DirEntryLFN[t_sort_table[i]], &t_DirEntry[t_sort_table[i-1]], t_DirEntryLFN[t_sort_table[i-1]]) > 0) // compare items and swap if necessary
{
x = t_sort_table[i];
t_sort_table[i] = t_sort_table[i-1];
t_sort_table[i-1] = x;
}
else
break; // don't check further entries as they are already sorted
}
}
}
else if ((mode >= '0' && mode <= '9') || (mode >= 'A' && mode <= 'Z')) // find first entry beginning with given character
{
if (find_file)
x = tolower(pEntry->Name[0]) >= tolower(mode) && is_file;
else if (find_dir)
x = tolower(pEntry->Name[0]) >= tolower(mode) || is_file;
else
x = (CompareDirEntries(pEntry, lfn, &DirEntry[sort_table[iSelectedEntry]], DirEntryLFN[sort_table[iSelectedEntry]]) > 0); // compare with the last visible entry
if (x)
{
if (nNewEntries < MAXDIRENTRIES) // initial directory scan (first 8 entries)
{
t_DirEntry[nNewEntries] = *pEntry; // add new entry at first empty slot in storage buffer
t_DirEntryLFN[nNewEntries][0] = 0;
if (lfn[0])
if (lfn_checksum(pEntry->Name) == name_checksum)
strncpy(t_DirEntryLFN[nNewEntries], lfn, sizeof(lfn));
t_sort_table[nNewEntries] = nNewEntries; // init sorting table
nNewEntries++;
}
else
{
if (CompareDirEntries(pEntry, lfn, &t_DirEntry[t_sort_table[MAXDIRENTRIES-1]], t_DirEntryLFN[t_sort_table[MAXDIRENTRIES-1]]) < 0) // compare new entry with the last already found
{
t_DirEntry[t_sort_table[MAXDIRENTRIES-1]] = *pEntry; // replace the last entry with the new one if appropriate
t_DirEntryLFN[t_sort_table[MAXDIRENTRIES-1]][0] = 0;
if (lfn[0])
if (lfn_checksum(pEntry->Name) == name_checksum)
strncpy(t_DirEntryLFN[t_sort_table[MAXDIRENTRIES-1]], lfn, sizeof(lfn));
}
}
for (i = nNewEntries - 1; i > 0; i--) // one pass bubble-sorting (table is already sorted, only the new item must be placed in order)
{
if (CompareDirEntries(&t_DirEntry[t_sort_table[i]], t_DirEntryLFN[t_sort_table[i]], &t_DirEntry[t_sort_table[i-1]], t_DirEntryLFN[t_sort_table[i-1]]) < 0) // compare items and swap if necessary
{
x = t_sort_table[i];
t_sort_table[i] = t_sort_table[i-1];
t_sort_table[i-1] = x;
}
else
break; // don't check further entries as they are already sorted
}
}
}
}
}
lfn[0] = 0;
}
}
}
if (iCurrentDirectory || fat32) // subdirectory is a linked cluster chain
{
iDirectoryCluster = NextCluster(iDirectoryCluster); // get next cluster in chain
if (fat32 ? (iDirectoryCluster & 0x0FFFFFF8) == 0x0FFFFFF8 : (iDirectoryCluster & 0xFFF8) == 0xFFF8) // check if end of chain
break; // no more clusters in chain
iDirectorySector = data_start + cluster_size * (iDirectoryCluster - 2); // calculate first sector address of the new cluster
}
else
break;
}
if (nNewEntries)
{
if (mode == SCAN_NEXT_PAGE)
{
unsigned char j = 8 - nNewEntries; // number of remaining old entries to scroll
for (i = 0; i < j; i++)
{
x = sort_table[i];
sort_table[i] = sort_table[i + nNewEntries];
sort_table[i + nNewEntries] = x;
}
// copy temporary buffer to display
for (i = 0; i < nNewEntries; i++)
{
DirEntry[sort_table[i + j]] = t_DirEntry[t_sort_table[i]];
strcpy(DirEntryLFN[sort_table[i + j]], t_DirEntryLFN[t_sort_table[i]]);
}
}
else if (mode == SCAN_PREV_PAGE)
{ // note: temporary buffer entries are in reverse order
unsigned char j = nNewEntries - 1;
for (i = 7; i > j; i--)
{
x = sort_table[i];
sort_table[i] = sort_table[i - nNewEntries];
sort_table[i - nNewEntries] = x;
}
// copy temporary buffer to display
for (i = 0; i < nNewEntries; i++)
{
DirEntry[sort_table[j - i]] = t_DirEntry[t_sort_table[i]];
strcpy(DirEntryLFN[sort_table[j - i]], t_DirEntryLFN[t_sort_table[i]]);
}
nDirEntries += nNewEntries;
if (nDirEntries > MAXDIRENTRIES)