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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
|
/*
gba_nds_fat.c
By chishm (Michael Chisholm)
Routines for reading a compact flash card
using the GBA Movie Player or M3.
Some FAT routines are based on those in fat.c, which
is part of avrlib by Pascal Stang.
This software is completely free. No warranty is provided.
If you use it, please give me credit and email me about your
project at chishm@hotmail.com
See gba_nds_fat.txt for help and license details.
*/
//---------------------------------------------------------------
// Includes
#include "gba_nds_fat.h"
#include "disc_io.h"
#include <string.h>
#ifdef NDS
// #include <nds/ipc.h> // Time on the NDS
#include <NDS/scummvm_ipc.h>
#endif
//----------------------------------------------------------------
// Data types
#ifndef NULL
#define NULL 0
#endif
//----------------------------------------------------------------
// NDS memory access control register
#ifdef NDS
#ifndef WAIT_CR
#define WAIT_CR (*(vu16*)0x04000204)
#endif
#endif
//---------------------------------------------------------------
// Appropriate placement of CF functions and data
#ifdef NDS
#define _VARS_IN_RAM
#else
#define _VARS_IN_RAM __attribute__ ((section (".sbss")))
#endif
//-----------------------------------------------------------------
// FAT constants
#define CLUSTER_EOF_16 0xFFFF
#define CLUSTER_EOF 0x0FFFFFFF
#define CLUSTER_FREE 0x0000
#define CLUSTER_FIRST 0x0002
#define FILE_LAST 0x00
#define FILE_FREE 0xE5
#define FAT16_ROOT_DIR_CLUSTER 0x00
//-----------------------------------------------------------------
// long file name constants
#define LFN_END 0x40
#define LFN_DEL 0x80
//-----------------------------------------------------------------
// Data Structures
// Take care of packing for GCC - it doesn't obey pragma pack()
// properly for ARM targets.
#ifdef __GNUC__
#define __PACKED __attribute__ ((__packed__))
#else
#define __PACKED
#pragma pack(1)
#endif
// Boot Sector - must be packed
typedef struct
{
u8 jmpBoot[3];
u8 OEMName[8];
// BIOS Parameter Block
u16 bytesPerSector;
u8 sectorsPerCluster;
u16 reservedSectors;
u8 numFATs;
u16 rootEntries;
u16 numSectorsSmall;
u8 mediaDesc;
u16 sectorsPerFAT;
u16 sectorsPerTrk;
u16 numHeads;
u32 numHiddenSectors;
u32 numSectors;
union // Different types of extended BIOS Parameter Block for FAT16 and FAT32
{
struct
{
// Ext BIOS Parameter Block for FAT16
u8 driveNumber;
u8 reserved1;
u8 extBootSig;
u32 volumeID;
u8 volumeLabel[11];
u8 fileSysType[8];
// Bootcode
u8 bootCode[448];
} __PACKED fat16;
struct
{
// FAT32 extended block
u32 sectorsPerFAT32;
u16 extFlags;
u16 fsVer;
u32 rootClus;
u16 fsInfo;
u16 bkBootSec;
u8 reserved[12];
// Ext BIOS Parameter Block for FAT16
u8 driveNumber;
u8 reserved1;
u8 extBootSig;
u32 volumeID;
u8 volumeLabel[11];
u8 fileSysType[8];
// Bootcode
u8 bootCode[420];
} __PACKED fat32;
} __PACKED extBlock;
u16 bootSig;
} __PACKED BOOT_SEC;
// Directory entry - must be packed
typedef struct
{
u8 name[8];
u8 ext[3];
u8 attrib;
u8 reserved;
u8 cTime_ms;
u16 cTime;
u16 cDate;
u16 aDate;
u16 startClusterHigh;
u16 mTime;
u16 mDate;
u16 startCluster;
u32 fileSize;
} __PACKED DIR_ENT;
// Long file name directory entry - must be packed
typedef struct
{
u8 ordinal; // Position within LFN
u16 char0;
u16 char1;
u16 char2;
u16 char3;
u16 char4;
u8 flag; // Should be equal to ATTRIB_LFN
u8 reserved1; // Always 0x00
u8 checkSum; // Checksum of short file name (alias)
u16 char5;
u16 char6;
u16 char7;
u16 char8;
u16 char9;
u16 char10;
u16 reserved2; // Always 0x0000
u16 char11;
u16 char12;
} __PACKED DIR_ENT_LFN;
const char lfn_offset_table[13]={0x01,0x03,0x05,0x07,0x09,0x0E,0x10,0x12,0x14,0x16,0x18,0x1C,0x1E};
// End of packed structs
#ifdef __PACKED
#undef __PACKED
#endif
#ifndef __GNUC__
#pragma pack()
#endif
//-----------------------------------------------------------------
// Global Variables
// _VARS_IN_RAM variables are stored in the largest section of WRAM
// available: IWRAM on NDS ARM7, EWRAM on NDS ARM9 and GBA
// Files
_VARS_IN_RAM FAT_FILE openFiles[MAX_FILES_OPEN];
// Long File names
_VARS_IN_RAM char lfnName[MAX_FILENAME_LENGTH];
bool lfnExists;
// Locations on card
int filesysRootDir;
int filesysRootDirClus;
int filesysFAT;
int filesysSecPerFAT;
int filesysNumSec;
int filesysData;
int filesysBytePerSec;
int filesysSecPerClus;
int filesysBytePerClus;
FS_TYPE filesysType = FS_UNKNOWN;
u32 filesysTotalSize;
// Info about FAT
u32 fatLastCluster;
u32 fatFirstFree;
// fatBuffer used to reduce wear on the CF card from multiple writes
_VARS_IN_RAM char fatBuffer[BYTE_PER_READ];
u32 fatBufferCurSector;
// Current working directory
u32 curWorkDirCluster;
// Position of the directory entry last retreived with FAT_GetDirEntry
u32 wrkDirCluster;
int wrkDirSector;
int wrkDirOffset;
// Global sector buffer to save on stack space
_VARS_IN_RAM unsigned char globalBuffer[BYTE_PER_READ];
//-----------------------------------------------------------------
// Functions contained in this file - predeclarations
char ucase (char character);
u16 getRTCtoFileTime (void);
u16 getRTCtoFileDate (void);
bool FAT_AddDirEntry (const char* path, DIR_ENT newDirEntry);
bool FAT_ClearLinks (u32 cluster);
DIR_ENT FAT_DirEntFromPath (const char* path);
u32 FAT_FirstFreeCluster(void);
DIR_ENT FAT_GetDirEntry ( u32 dirCluster, int entry, int origin);
u32 FAT_LinkFreeCluster(u32 cluster);
u32 FAT_NextCluster(u32 cluster);
bool FAT_WriteFatEntry (u32 cluster, u32 value);
bool FAT_GetFilename (DIR_ENT dirEntry, char* alias);
bool FAT_InitFiles (void);
bool FAT_FreeFiles (void);
int FAT_remove (const char* path);
bool FAT_chdir (const char* path);
FILE_TYPE FAT_FindFirstFile (char* filename);
FILE_TYPE FAT_FindNextFile (char* filename);
FILE_TYPE FAT_FileExists (const char* filename);
bool FAT_GetAlias (char* alias);
bool FAT_GetLongFilename (char* filename);
u32 FAT_GetFileSize (void);
u32 FAT_GetFileCluster (void);
FAT_FILE* FAT_fopen(const char* path, const char* mode);
bool FAT_fclose (FAT_FILE* file);
bool FAT_feof(FAT_FILE* file);
int FAT_fseek(FAT_FILE* file, s32 offset, int origin);
u32 FAT_ftell (FAT_FILE* file);
u32 FAT_fread (void* buffer, u32 size, u32 count, FAT_FILE* file);
u32 FAT_fwrite (const void* buffer, u32 size, u32 count, FAT_FILE* file);
char FAT_fgetc (FAT_FILE* file);
char FAT_fputc (char c, FAT_FILE* file);
/*-----------------------------------------------------------------
ucase
Returns the uppercase version of the given char
char IN: a character
char return OUT: uppercase version of character
-----------------------------------------------------------------*/
char ucase (char character)
{
if ((character > 0x60) && (character < 0x7B))
character = character - 0x20;
return (character);
}
/*-----------------------------------------------------------------
getRTCtoFileTime and getRTCtoFileDate
Returns the time / date in Dir Entry styled format
u16 return OUT: time / date in Dir Entry styled format
-----------------------------------------------------------------*/
u16 getRTCtoFileTime (void)
{
#ifdef NDS
return (
( ( (IPC->rtc_hours > 11 ? IPC->rtc_hours - 40 : IPC->rtc_hours) & 0x1F) << 11) |
( (IPC->rtc_minutes & 0x3F) << 5) |
( (IPC->rtc_seconds >> 1) & 0x1F) );
#else
return 0;
#endif
}
u16 getRTCtoFileDate (void)
{
#ifdef NDS
return (
( ((IPC->rtc_year + 20) & 0x7F) <<9) |
( (IPC->rtc_month & 0xF) << 5) |
(IPC->rtc_day & 0x1F) );
#else
return 0;
#endif
}
/*-----------------------------------------------------------------
Disc level FAT routines
-----------------------------------------------------------------*/
#define FAT_ClustToSect(m) \
(((m-2) * filesysSecPerClus) + filesysData)
/*-----------------------------------------------------------------
FAT_NextCluster
Internal function - gets the cluster linked from input cluster
-----------------------------------------------------------------*/
u32 FAT_NextCluster(u32 cluster)
{
u32 nextCluster = CLUSTER_FREE;
u32 sector;
int offset;
switch (filesysType)
{
case FS_UNKNOWN:
nextCluster = CLUSTER_FREE;
break;
case FS_FAT12:
sector = filesysFAT + (((cluster * 3) / 2) / BYTE_PER_READ);
offset = ((cluster * 3) / 2) % BYTE_PER_READ;
// If FAT buffer contains wrong sector
if (sector != fatBufferCurSector)
{
// Load correct sector to buffer
fatBufferCurSector = sector;
disc_ReadSector(fatBufferCurSector, fatBuffer);
}
nextCluster = ((u8*)fatBuffer)[offset];
offset++;
if (offset >= BYTE_PER_READ) {
offset = 0;
fatBufferCurSector++;
disc_ReadSector(fatBufferCurSector, fatBuffer);
}
nextCluster |= (((u8*)fatBuffer)[offset]) << 8;
if (cluster & 0x01) {
nextCluster = nextCluster >> 4;
} else {
nextCluster &= 0x0FFF;
}
if (nextCluster >= 0x0FF7)
{
nextCluster = CLUSTER_EOF;
}
break;
case FS_FAT16:
sector = filesysFAT + ((cluster << 1) / BYTE_PER_READ);
offset = cluster % (BYTE_PER_READ >> 1);
// If FAT buffer contains wrong sector
if (sector != fatBufferCurSector)
{
// Load correct sector to buffer
fatBufferCurSector = sector;
disc_ReadSector(fatBufferCurSector, fatBuffer);
}
// read the nextCluster value
nextCluster = ((u16*)fatBuffer)[offset];
if (nextCluster >= 0xFFF7)
{
nextCluster = CLUSTER_EOF;
}
break;
case FS_FAT32:
sector = filesysFAT + ((cluster << 2) / BYTE_PER_READ);
offset = cluster % (BYTE_PER_READ >> 2);
// If FAT buffer contains wrong sector
if (sector != fatBufferCurSector)
{
// Load correct sector to buffer
fatBufferCurSector = sector;
disc_ReadSector(fatBufferCurSector, fatBuffer);
}
// read the nextCluster value
nextCluster = (((u32*)fatBuffer)[offset]) & 0x0FFFFFFF;
if (nextCluster >= 0x0FFFFFF7)
{
nextCluster = CLUSTER_EOF;
}
break;
default:
nextCluster = CLUSTER_FREE;
break;
}
return nextCluster;
}
#ifdef CAN_WRITE_TO_DISC
/*-----------------------------------------------------------------
FAT_WriteFatEntry
Internal function - writes FAT information about a cluster
-----------------------------------------------------------------*/
bool FAT_WriteFatEntry (u32 cluster, u32 value)
{
u32 sector;
int offset;
if ((cluster < 0x0002) || (cluster > fatLastCluster))
{
return false;
}
switch (filesysType)
{
case FS_UNKNOWN:
return false;
break;
case FS_FAT12:
sector = filesysFAT + (((cluster * 3) / 2) / BYTE_PER_READ);
offset = ((cluster * 3) / 2) % BYTE_PER_READ;
// If FAT buffer contains wrong sector
if (sector != fatBufferCurSector)
{
// Load correct sector to buffer
fatBufferCurSector = sector;
disc_ReadSector(fatBufferCurSector, fatBuffer);
}
if (cluster & 0x01) {
((u8*)fatBuffer)[offset] = (((u8*)fatBuffer)[offset] & 0x0F) | ((value & 0x0F) << 4);
offset++;
if (offset >= BYTE_PER_READ) {
offset = 0;
// write the buffer back to disc
disc_WriteSector(fatBufferCurSector, fatBuffer);
// read the next sector
fatBufferCurSector++;
disc_ReadSector(fatBufferCurSector, fatBuffer);
}
((u8*)fatBuffer)[offset] = (value & 0x0FF0) >> 4;
} else {
((u8*)fatBuffer)[offset] = value & 0xFF;
offset++;
if (offset >= BYTE_PER_READ) {
offset = 0;
// write the buffer back to disc
disc_WriteSector(fatBufferCurSector, fatBuffer);
// read the next sector
fatBufferCurSector++;
disc_ReadSector(fatBufferCurSector, fatBuffer);
}
((u8*)fatBuffer)[offset] = (((u8*)fatBuffer)[offset] & 0xF0) | ((value >> 8) & 0x0F);
}
break;
case FS_FAT16:
sector = filesysFAT + ((cluster << 1) / BYTE_PER_READ);
offset = cluster % (BYTE_PER_READ >> 1);
// If FAT buffer contains wrong sector
if (sector != fatBufferCurSector)
{
// Load correct sector to buffer
fatBufferCurSector = sector;
disc_ReadSector(fatBufferCurSector, fatBuffer);
}
// write the value to the FAT buffer
((u16*)fatBuffer)[offset] = (value & 0xFFFF);
break;
case FS_FAT32:
sector = filesysFAT + ((cluster << 2) / BYTE_PER_READ);
offset = cluster % (BYTE_PER_READ >> 2);
// If FAT buffer contains wrong sector
if (sector != fatBufferCurSector)
{
// Load correct sector to buffer
fatBufferCurSector = sector;
disc_ReadSector(fatBufferCurSector, fatBuffer);
}
// write the value to the FAT buffer
(((u32*)fatBuffer)[offset]) = value;
break;
default:
return false;
break;
}
// write the buffer back to disc
disc_WriteSector(fatBufferCurSector, fatBuffer);
return true;
}
#endif
#ifdef CAN_WRITE_TO_DISC
/*-----------------------------------------------------------------
FAT_ReadWriteFatEntryBuffered
Internal function - writes FAT information about a cluster to a
buffer that should then be flushed to disc using
FAT_WriteFatEntryFlushBuffer()
Call FAT_WriteFatEntry first so as not to ruin the disc.
Also returns the entry being replaced
-----------------------------------------------------------------*/
u32 FAT_ReadWriteFatEntryBuffered (u32 cluster, u32 value)
{
u32 sector;
int offset;
u32 oldValue;
if ((cluster < 0x0002) || (cluster > fatLastCluster))
return CLUSTER_FREE;
switch (filesysType)
{
case FS_UNKNOWN:
oldValue = CLUSTER_FREE;
break;
case FS_FAT12:
sector = filesysFAT + (((cluster * 3) / 2) / BYTE_PER_READ);
offset = ((cluster * 3) / 2) % BYTE_PER_READ;
// If FAT buffer contains wrong sector
if (sector != fatBufferCurSector)
{
// write the old buffer to disc
if ((fatBufferCurSector >= filesysFAT) && (fatBufferCurSector < (filesysFAT + filesysSecPerFAT)))
disc_WriteSector(fatBufferCurSector, fatBuffer);
// Load correct sector to buffer
fatBufferCurSector = sector;
disc_ReadSector(fatBufferCurSector, fatBuffer);
}
if (cluster & 0x01) {
oldValue = (((u8*)fatBuffer)[offset] & 0xF0) >> 4;
((u8*)fatBuffer)[offset] = (((u8*)fatBuffer)[offset] & 0x0F) | ((value & 0x0F) << 4);
offset++;
if (offset >= BYTE_PER_READ) {
offset = 0;
// write the buffer back to disc
disc_WriteSector(fatBufferCurSector, fatBuffer);
// read the next sector
fatBufferCurSector++;
disc_ReadSector(fatBufferCurSector, fatBuffer);
}
oldValue |= ((((u8*)fatBuffer)[offset]) << 4) & 0x0FF0;
((u8*)fatBuffer)[offset] = (value & 0x0FF0) >> 4;
} else {
oldValue = ((u8*)fatBuffer)[offset] & 0xFF;
((u8*)fatBuffer)[offset] = value & 0xFF;
offset++;
if (offset >= BYTE_PER_READ) {
offset = 0;
// write the buffer back to disc
disc_WriteSector(fatBufferCurSector, fatBuffer);
// read the next sector
fatBufferCurSector++;
disc_ReadSector(fatBufferCurSector, fatBuffer);
}
oldValue |= (((u8*)fatBuffer)[offset] & 0x0F) << 8;
((u8*)fatBuffer)[offset] = (((u8*)fatBuffer)[offset] & 0xF0) | ((value >> 8) & 0x0F);
}
if (oldValue >= 0x0FF7)
{
oldValue = CLUSTER_EOF;
}
break;
case FS_FAT16:
sector = filesysFAT + ((cluster << 1) / BYTE_PER_READ);
offset = cluster % (BYTE_PER_READ >> 1);
// If FAT buffer contains wrong sector
if (sector != fatBufferCurSector)
{
// write the old buffer to disc
if ((fatBufferCurSector >= filesysFAT) && (fatBufferCurSector < (filesysFAT + filesysSecPerFAT)))
disc_WriteSector(fatBufferCurSector, fatBuffer);
// Load correct sector to buffer
fatBufferCurSector = sector;
disc_ReadSector(fatBufferCurSector, fatBuffer);
}
// write the value to the FAT buffer
oldValue = ((u16*)fatBuffer)[offset];
((u16*)fatBuffer)[offset] = value;
if (oldValue >= 0xFFF7)
{
oldValue = CLUSTER_EOF;
}
break;
case FS_FAT32:
sector = filesysFAT + ((cluster << 2) / BYTE_PER_READ);
offset = cluster % (BYTE_PER_READ >> 2);
// If FAT buffer contains wrong sector
if (sector != fatBufferCurSector)
{
// write the old buffer to disc
if ((fatBufferCurSector >= filesysFAT) && (fatBufferCurSector < (filesysFAT + filesysSecPerFAT)))
disc_WriteSector(fatBufferCurSector, fatBuffer);
// Load correct sector to buffer
fatBufferCurSector = sector;
disc_ReadSector(fatBufferCurSector, fatBuffer);
}
// write the value to the FAT buffer
oldValue = ((u32*)fatBuffer)[offset];
((u32*)fatBuffer)[offset] = value;
if (oldValue >= 0x0FFFFFF7)
{
oldValue = CLUSTER_EOF;
}
break;
default:
oldValue = CLUSTER_FREE;
break;
}
return oldValue;
}
#endif
#ifdef CAN_WRITE_TO_DISC
/*-----------------------------------------------------------------
FAT_WriteFatEntryFlushBuffer
Flush the FAT buffer back to the disc
-----------------------------------------------------------------*/
bool FAT_WriteFatEntryFlushBuffer (void)
{
// write the buffer disc
if ((fatBufferCurSector >= filesysFAT) && (fatBufferCurSector < (filesysFAT + filesysSecPerFAT)))
{
disc_WriteSector(fatBufferCurSector, fatBuffer);
return true;
} else {
return false;
}
}
#endif
#ifdef CAN_WRITE_TO_DISC
/*-----------------------------------------------------------------
FAT_FirstFreeCluster
Internal function - gets the first available free cluster
-----------------------------------------------------------------*/
u32 FAT_FirstFreeCluster(void)
{
// Start at first valid cluster
if (fatFirstFree < CLUSTER_FIRST)
fatFirstFree = CLUSTER_FIRST;
while ((FAT_NextCluster(fatFirstFree) != CLUSTER_FREE) && (fatFirstFree <= fatLastCluster))
{
fatFirstFree++;
}
if (fatFirstFree > fatLastCluster)
{
return CLUSTER_EOF;
}
return fatFirstFree;
}
#endif
#ifdef CAN_WRITE_TO_DISC
/*-----------------------------------------------------------------
FAT_LinkFreeCluster
Internal function - gets the first available free cluster, sets it
to end of file, links the input cluster to it then returns the
cluster number
-----------------------------------------------------------------*/
u32 FAT_LinkFreeCluster(u32 cluster)
{
u32 firstFree;
u32 curLink;
if (cluster > fatLastCluster)
{
return CLUSTER_FREE;
}
// Check if the cluster already has a link, and return it if so
curLink = FAT_NextCluster (cluster);
if ((curLink >= CLUSTER_FIRST) && (curLink < fatLastCluster))
{
return curLink; // Return the current link - don't allocate a new one
}
// Get a free cluster
firstFree = FAT_FirstFreeCluster();
// If couldn't get a free cluster then return
if (firstFree == CLUSTER_EOF)
{
return CLUSTER_FREE;
}
if ((cluster >= CLUSTER_FIRST) && (cluster < fatLastCluster))
{
// Update the linked from FAT entry
FAT_WriteFatEntry (cluster, firstFree);
}
// Create the linked to FAT entry
FAT_WriteFatEntry (firstFree, CLUSTER_EOF);
return firstFree;
}
#endif
#ifdef CAN_WRITE_TO_DISC
/*-----------------------------------------------------------------
FAT_ClearLinks
Internal function - frees any cluster used by a file
-----------------------------------------------------------------*/
bool FAT_ClearLinks (u32 cluster)
{
u32 nextCluster;
if ((cluster < 0x0002) || (cluster > fatLastCluster))
return false;
// Store next cluster before erasing the link
nextCluster = FAT_NextCluster (cluster);
// Erase the link
FAT_WriteFatEntry (cluster, CLUSTER_FREE);
// Move onto next cluster
cluster = nextCluster;
while ((cluster != CLUSTER_EOF) && (cluster != CLUSTER_FREE))
{
cluster = FAT_ReadWriteFatEntryBuffered (cluster, CLUSTER_FREE);
}
// Flush fat write buffer
FAT_WriteFatEntryFlushBuffer ();
return true;
}
#endif
/*-----------------------------------------------------------------
FAT_InitFiles
Reads the FAT information from the CF card.
You need to call this before reading any files.
bool return OUT: true if successful.
-----------------------------------------------------------------*/
bool FAT_InitFiles (void)
{
int i;
int bootSector;
BOOT_SEC* bootSec;
if (!disc_Init())
{
return (false);
}
// Read first sector of CF card
if ( !disc_ReadSector(0, globalBuffer)) {
return false;
}
// Make sure it is a valid MBR or boot sector
/* if ( (globalBuffer[0x1FE] != 0x55) || (globalBuffer[0x1FF] != 0xAA)) {
return false;
}*/
// Check if there is a FAT string, which indicates this is a boot sector
if ((globalBuffer[0x36] == 'F') && (globalBuffer[0x37] == 'A') && (globalBuffer[0x38] == 'T'))
{
bootSector = 0;
}
// Check for FAT32
else if ((globalBuffer[0x52] == 'F') && (globalBuffer[0x53] == 'A') && (globalBuffer[0x54] == 'T'))
{
bootSector = 0;
}
else // This is an MBR
{
// Find first valid partition from MBR
// First check for an active partition
for (i=0x1BE; (i < 0x1FE) && (globalBuffer[i] != 0x80); i+= 0x10);
// If it didn't find an active partition, search for any valid partition
if (i == 0x1FE)
for (i=0x1BE; (i < 0x1FE) && (globalBuffer[i+0x04] == 0x00); i+= 0x10);
// Go to first valid partition
if ( i != 0x1FE) // Make sure it found a partition
{
bootSector = globalBuffer[0x8 + i] + (globalBuffer[0x9 + i] << 8) + (globalBuffer[0xA + i] << 16) + ((globalBuffer[0xB + i] << 24) & 0x0F);
} else {
bootSector = 0; // No partition found, assume this is a MBR free disk
}
}
// Read in boot sector
bootSec = (BOOT_SEC*) globalBuffer;
if (!disc_ReadSector (bootSector, bootSec)) {
return false;
}
// Store required information about the file system
if (bootSec->sectorsPerFAT != 0)
{
filesysSecPerFAT = bootSec->sectorsPerFAT;
}
else
{
filesysSecPerFAT = bootSec->extBlock.fat32.sectorsPerFAT32;
}
if (bootSec->numSectorsSmall != 0)
{
filesysNumSec = bootSec->numSectorsSmall;
}
else
{
filesysNumSec = bootSec->numSectors;
}
filesysBytePerSec = BYTE_PER_READ; // Sector size is redefined to be 512 bytes
filesysSecPerClus = bootSec->sectorsPerCluster * bootSec->bytesPerSector / BYTE_PER_READ;
filesysBytePerClus = filesysBytePerSec * filesysSecPerClus;
filesysFAT = bootSector + bootSec->reservedSectors;
filesysRootDir = filesysFAT + (bootSec->numFATs * filesysSecPerFAT);
filesysData = filesysRootDir + ((bootSec->rootEntries * sizeof(DIR_ENT)) / filesysBytePerSec);
filesysTotalSize = (filesysNumSec - filesysData) * filesysBytePerSec;
// Store info about FAT
fatLastCluster = (filesysNumSec - filesysData) / bootSec->sectorsPerCluster;
fatFirstFree = CLUSTER_FIRST;
fatBufferCurSector = 0;
disc_ReadSector(fatBufferCurSector, fatBuffer);
if (fatLastCluster < 4085)
{
filesysType = FS_FAT12; // FAT12 volume - unsupported
}
else if (fatLastCluster < 65525)
{
filesysType = FS_FAT16; // FAT16 volume
}
else
{
filesysType = FS_FAT32; // FAT32 volume
}
if (filesysType != FS_FAT32)
{
filesysRootDirClus = FAT16_ROOT_DIR_CLUSTER;
}
else // Set up for the FAT32 way
{
filesysRootDirClus = bootSec->extBlock.fat32.rootClus;
// Check if FAT mirroring is enabled
if (!(bootSec->extBlock.fat32.extFlags & 0x80))
{
// Use the active FAT
filesysFAT = filesysFAT + ( filesysSecPerFAT * (bootSec->extBlock.fat32.extFlags & 0x0F));
}
}
// Set current directory to the root
curWorkDirCluster = filesysRootDirClus;
wrkDirCluster = filesysRootDirClus;
wrkDirSector = 0;
wrkDirOffset = 0;
// Set all files to free
for (i=0; i < MAX_FILES_OPEN; i++)
{
openFiles[i].inUse = false;
}
// No long filenames so far
lfnExists = false;
for (i = 0; i < MAX_FILENAME_LENGTH; i++)
{
lfnName[i] = '\0';
}
return (true);
}
/*-----------------------------------------------------------------
FAT_FreeFiles
Closes all open files then resets the CF card.
Call this before exiting back to the GBAMP
bool return OUT: true if successful.
-----------------------------------------------------------------*/
bool FAT_FreeFiles (void)
{
int i;
// Close all open files
for (i=0; i < MAX_FILES_OPEN; i++)
{
if (openFiles[i].inUse == true)
{
FAT_fclose(&openFiles[i]);
}
}
// Flush any sectors in disc cache
disc_CacheFlush();
// Clear card status
disc_ClearStatus();
// Return status of card
return disc_IsInserted();
}
/*-----------------------------------------------------------------
FAT_GetDirEntry
Return the file info structure of the next valid file entry
u32 dirCluster: IN cluster of subdirectory table
int entry: IN the desired file entry
int origin IN: relative position of the entry
DIR_ENT return OUT: desired dirEntry. First char will be FILE_FREE if
the entry does not exist.
-----------------------------------------------------------------*/
DIR_ENT FAT_GetDirEntry ( u32 dirCluster, int entry, int origin)
{
DIR_ENT dir;
DIR_ENT_LFN lfn;
int firstSector = 0;
bool notFound = false;
bool found = false;
int maxSectors;
int lfnPos, aliasPos;
u8 lfnChkSum, chkSum;
int i;
dir.name[0] = FILE_FREE; // default to no file found
dir.attrib = 0x00;
// Check if fat has been initialised
if (filesysBytePerSec == 0)
{
return (dir);
}
switch (origin)
{
case SEEK_SET:
wrkDirCluster = dirCluster;
wrkDirSector = 0;
wrkDirOffset = -1;
break;
case SEEK_CUR: // Don't change anything
break;
case SEEK_END: // Find entry signifying end of directory
// Subtraction will never reach 0, so it keeps going
// until reaches end of directory
wrkDirCluster = dirCluster;
wrkDirSector = 0;
wrkDirOffset = -1;
entry = -1;
break;
default:
return dir;
}
lfnChkSum = 0;
maxSectors = (wrkDirCluster == FAT16_ROOT_DIR_CLUSTER ? (filesysData - filesysRootDir) : filesysSecPerClus);
// Scan Dir for correct entry
firstSector = (wrkDirCluster == FAT16_ROOT_DIR_CLUSTER ? filesysRootDir : FAT_ClustToSect(wrkDirCluster));
disc_ReadSector (firstSector + wrkDirSector, globalBuffer);
found = false;
notFound = false;
do {
wrkDirOffset++;
if (wrkDirOffset == BYTE_PER_READ / sizeof (DIR_ENT))
{
wrkDirOffset = 0;
wrkDirSector++;
if ((wrkDirSector == filesysSecPerClus) && (wrkDirCluster != FAT16_ROOT_DIR_CLUSTER))
{
wrkDirSector = 0;
wrkDirCluster = FAT_NextCluster(wrkDirCluster);
if (wrkDirCluster == CLUSTER_EOF)
{
notFound = true;
}
firstSector = FAT_ClustToSect(wrkDirCluster);
}
else if ((wrkDirCluster == FAT16_ROOT_DIR_CLUSTER) && (wrkDirSector == (filesysData - filesysRootDir)))
{
notFound = true; // Got to end of root dir
}
disc_ReadSector (firstSector + wrkDirSector, globalBuffer);
}
dir = ((DIR_ENT*) globalBuffer)[wrkDirOffset];
if ((dir.name[0] != FILE_FREE) && (dir.name[0] > 0x20) && ((dir.attrib & ATTRIB_VOL) != ATTRIB_VOL))
{
entry--;
if (lfnExists)
{
// Calculate file checksum
chkSum = 0;
for (aliasPos=0; aliasPos < 11; aliasPos++)
{
// NOTE: The operation is an unsigned char rotate right
chkSum = ((chkSum & 1) ? 0x80 : 0) + (chkSum >> 1) + (aliasPos < 8 ? dir.name[aliasPos] : dir.ext[aliasPos - 8]);
}
if (chkSum != lfnChkSum)
{
lfnExists = false;
lfnName[0] = '\0';
}
}
if (entry == 0)
{
if (!lfnExists)
{
FAT_GetFilename (dir, lfnName);
}
found = true;
}
}
else if (dir.name[0] == FILE_LAST)
{
if (origin == SEEK_END)
{
found = true;
}
else
{
notFound = true;
}
}
else if (dir.attrib == ATTRIB_LFN)
{
lfn = ((DIR_ENT_LFN*) globalBuffer)[wrkDirOffset];
if (lfn.ordinal & LFN_DEL)
{
lfnExists = false;
}
else if (lfn.ordinal & LFN_END) // Last part of LFN, make sure it isn't deleted (Thanks MoonLight)
{
lfnExists = true;
lfnName[(lfn.ordinal & ~LFN_END) * 13] = '\0'; // Set end of lfn to null character
lfnChkSum = lfn.checkSum;
}
if (lfnChkSum != lfn.checkSum)
{
lfnExists = false;
}
if (lfnExists)
{
lfnPos = ((lfn.ordinal & ~LFN_END) - 1) * 13;
for (i = 0; i < 13; i++) {
lfnName[lfnPos + i] = ((u8*)&lfn)[(int)(lfn_offset_table[i])] /* | ((u8*)&lfn)[(int)(lfn_offset_table[i]) + 1] include this for unicode support*/;
}
}
}
} while (!found && !notFound);
// If no file is found, return FILE_FREE
if (notFound)
{
dir.name[0] = FILE_FREE;
}
return (dir);
}
/*-----------------------------------------------------------------
FAT_GetLongFilename
Get the long name of the last file or directory retrived with
GetDirEntry. Also works for FindFirstFile and FindNextFile.
If a long name doesn't exist, it returns the short name
instead.
char* filename: OUT will be filled with the filename, should be at
least 256 bytes long
bool return OUT: return true if successful
-----------------------------------------------------------------*/
bool FAT_GetLongFilename (char* filename)
{
if (filename == NULL)
return false;
strncpy (filename, lfnName, MAX_FILENAME_LENGTH - 1);
filename[MAX_FILENAME_LENGTH - 1] = '\0';
return true;
}
/*-----------------------------------------------------------------
FAT_GetFilename
Get the alias (short name) of the file or directory stored in
dirEntry
DIR_ENT dirEntry: IN a valid directory table entry
char* alias OUT: will be filled with the alias (short filename),
should be at least 13 bytes long
bool return OUT: return true if successful
-----------------------------------------------------------------*/
bool FAT_GetFilename (DIR_ENT dirEntry, char* alias)
{
int i=0;
int j=0;
alias[0] = '\0';
if (dirEntry.name[0] != FILE_FREE)
{
if (dirEntry.name[0] == '.')
{
alias[0] = '.';
if (dirEntry.name[1] == '.')
{
alias[1] = '.';
alias[2] = '\0';
}
else
{
alias[1] = '\0';
}
}
else
{
// Copy the filename from the dirEntry to the string
for (i = 0; (i < 8) && (dirEntry.name[i] != ' '); i++)
{
alias[i] = dirEntry.name[i];
}
// Copy the extension from the dirEntry to the string
if (dirEntry.ext[0] != ' ')
{
alias[i++] = '.';
for ( j = 0; (j < 3) && (dirEntry.ext[j] != ' '); j++)
{
alias[i++] = dirEntry.ext[j];
}
}
alias[i] = '\0';
}
}
return (alias[0] != '\0');
}
/*-----------------------------------------------------------------
FAT_GetAlias
Get the alias (short name) of the last file or directory entry read
using GetDirEntry. Works for FindFirstFile and FindNextFile
char* alias OUT: will be filled with the alias (short filename),
should be at least 13 bytes long
bool return OUT: return true if successful
-----------------------------------------------------------------*/
bool FAT_GetAlias (char* alias)
{
if (alias == NULL)
{
return false;
}
// Read in the last accessed directory entry
disc_ReadSector ((wrkDirCluster == FAT16_ROOT_DIR_CLUSTER ? filesysRootDir : FAT_ClustToSect(wrkDirCluster)) + wrkDirSector, globalBuffer);
return FAT_GetFilename (((DIR_ENT*)globalBuffer)[wrkDirOffset], alias);
}
/*-----------------------------------------------------------------
FAT_GetFileSize
Get the file size of the last file found or openned.
This idea is based on a modification by MoonLight
u32 return OUT: the file size
-----------------------------------------------------------------*/
u32 FAT_GetFileSize (void)
{
// Read in the last accessed directory entry
disc_ReadSector ((wrkDirCluster == FAT16_ROOT_DIR_CLUSTER ? filesysRootDir : FAT_ClustToSect(wrkDirCluster)) + wrkDirSector, globalBuffer);
return ((DIR_ENT*)globalBuffer)[wrkDirOffset].fileSize;
}
/*-----------------------------------------------------------------
FAT_GetFileCluster
Get the first cluster of the last file found or openned.
u32 return OUT: the file start cluster
-----------------------------------------------------------------*/
u32 FAT_GetFileCluster (void)
{
// Read in the last accessed directory entry
disc_ReadSector ((wrkDirCluster == FAT16_ROOT_DIR_CLUSTER ? filesysRootDir : FAT_ClustToSect(wrkDirCluster)) + wrkDirSector, globalBuffer);
return (((DIR_ENT*)globalBuffer)[wrkDirOffset].startCluster) | (((DIR_ENT*)globalBuffer)[wrkDirOffset].startClusterHigh << 16);
}
/*-----------------------------------------------------------------
FAT_GetFileAttributes
Get the attributes of the last file found or openned.
u8 return OUT: the file's attributes
-----------------------------------------------------------------*/
u8 FAT_GetFileAttributes (void)
{
// Read in the last accessed directory entry
disc_ReadSector ((wrkDirCluster == FAT16_ROOT_DIR_CLUSTER ? filesysRootDir : FAT_ClustToSect(wrkDirCluster)) + wrkDirSector, globalBuffer);
return ((DIR_ENT*)globalBuffer)[wrkDirOffset].attrib;
}
#ifdef CAN_WRITE_TO_DISC
/*-----------------------------------------------------------------
FAT_SetFileAttributes
Set the attributes of a file.
const char* filename IN: The name and path of the file to modify
u8 attributes IN: The attribute values to assign
u8 mask IN: Detemines which attributes are changed
u8 return OUT: the file's new attributes
-----------------------------------------------------------------*/
u8 FAT_SetFileAttributes (const char* filename, u8 attributes, u8 mask)
{
// Get the file
if (!FAT_FileExists(filename)) {
return 0xff;
}
// Read in the last accessed directory entry
disc_ReadSector ((wrkDirCluster == FAT16_ROOT_DIR_CLUSTER ? filesysRootDir : FAT_ClustToSect(wrkDirCluster)) + wrkDirSector, globalBuffer);
((DIR_ENT*)globalBuffer)[wrkDirOffset].attrib = (((DIR_ENT*)globalBuffer)[wrkDirOffset].attrib & ~(mask & 0x27)) | (attributes & 0x27); // 0x27 is he settable attributes
disc_WriteSector ((wrkDirCluster == FAT16_ROOT_DIR_CLUSTER ? filesysRootDir : FAT_ClustToSect(wrkDirCluster)) + wrkDirSector, globalBuffer);
return ((DIR_ENT*)globalBuffer)[wrkDirOffset].attrib;
}
#endif
#ifdef FILE_TIME_SUPPORT
time_t FAT_FileTimeToCTime (u16 fileTime, u16 fileDate)
{
struct tm timeInfo;
timeInfo.tm_year = (fileDate >> 9) + 80; // years since midnight January 1970
timeInfo.tm_mon = ((fileDate >> 5) & 0xf) - 1; // Months since january
timeInfo.tm_mday = fileDate & 0x1f; // Day of the month
timeInfo.tm_hour = fileTime >> 11; // hours past midnight
timeInfo.tm_min = (fileTime >> 5) & 0x3f; // minutes past the hour
timeInfo.tm_sec = (fileTime & 0x1f) * 2; // seconds past the minute
return mktime(&timeInfo);
}
/*-----------------------------------------------------------------
FAT_GetFileCreationTime
Get the creation time of the last file found or openned.
time_t return OUT: the file's creation time
-----------------------------------------------------------------*/
time_t FAT_GetFileCreationTime (void)
{
// Read in the last accessed directory entry
disc_ReadSector ((wrkDirCluster == FAT16_ROOT_DIR_CLUSTER ? filesysRootDir : FAT_ClustToSect(wrkDirCluster)) + wrkDirSector, globalBuffer);
return FAT_FileTimeToCTime(((DIR_ENT*)globalBuffer)[wrkDirOffset].cTime, ((DIR_ENT*)globalBuffer)[wrkDirOffset].cDate);
}
/*-----------------------------------------------------------------
FAT_GetFileLastWriteTime
Get the creation time of the last file found or openned.
time_t return OUT: the file's creation time
-----------------------------------------------------------------*/
time_t FAT_GetFileLastWriteTime (void)
{
// Read in the last accessed directory entry
disc_ReadSector ((wrkDirCluster == FAT16_ROOT_DIR_CLUSTER ? filesysRootDir : FAT_ClustToSect(wrkDirCluster)) + wrkDirSector, globalBuffer);
return FAT_FileTimeToCTime(((DIR_ENT*)globalBuffer)[wrkDirOffset].mTime, ((DIR_ENT*)globalBuffer)[wrkDirOffset].mDate);
}
#endif
/*-----------------------------------------------------------------
FAT_DirEntFromPath
Finds the directory entry for a file or directory from a path
Path separator is a forward slash /
const char* path: IN null terminated string of path.
DIR_ENT return OUT: dirEntry of found file. First char will be FILE_FREE
if the file was not found
-----------------------------------------------------------------*/
DIR_ENT FAT_DirEntFromPath (const char* path)
{
int pathPos;
char name[MAX_FILENAME_LENGTH];
char alias[13];
int namePos;
bool found, notFound;
DIR_ENT dirEntry;
u32 dirCluster;
bool flagLFN, dotSeen;
// Start at beginning of path
pathPos = 0;
#ifdef DS_BUILD_F
// Problems with Kyrandia doing a load of path lookups are reduced by this hack.
if (strstr(path, ".voc") || strstr(path, ".voc"))
{
dirEntry.name[0] = FILE_FREE;
dirEntry.attrib = 0x00;
return dirEntry;
}
#endif
if (path[pathPos] == '/')
{
dirCluster = filesysRootDirClus; // Start at root directory
}
else
{
dirCluster = curWorkDirCluster; // Start at current working dir
}
// Eat any slash /
while ((path[pathPos] == '/') && (path[pathPos] != '\0'))
{
pathPos++;
}
// Search until can't continue
found = false;
notFound = false;
while (!notFound && !found)
{
flagLFN = false;
// Copy name from path
namePos = 0;
if ((path[pathPos] == '.') && ((path[pathPos + 1] == '\0') || (path[pathPos + 1] == '/'))) {
// Dot entry
name[namePos++] = '.';
pathPos++;
} else if ((path[pathPos] == '.') && (path[pathPos + 1] == '.') && ((path[pathPos + 2] == '\0') || (path[pathPos + 2] == '/'))){
// Double dot entry
name[namePos++] = '.';
pathPos++;
name[namePos++] = '.';
pathPos++;
} else {
// Copy name from path
if (path[pathPos] == '.') {
flagLFN = true;
}
dotSeen = false;
while ((namePos < MAX_FILENAME_LENGTH - 1) && (path[pathPos] != '\0') && (path[pathPos] != '/'))
{
name[namePos] = ucase(path[pathPos]);
if ((name[namePos] <= ' ') || ((name[namePos] >= ':') && (name[namePos] <= '?'))) // Invalid character
{
flagLFN = true;
}
if (name[namePos] == '.') {
if (!dotSeen) {
dotSeen = true;
} else {
flagLFN = true;
}
}
namePos++;
pathPos++;
}
// Check if a long filename was specified
if (namePos > 12)
{
flagLFN = true;
}
}
// Add end of string char
name[namePos] = '\0';
// Move through path to correct place
while ((path[pathPos] != '/') && (path[pathPos] != '\0'))
pathPos++;
// Eat any slash /
while ((path[pathPos] == '/') && (path[pathPos] != '\0'))
{
pathPos++;
}
// Search current Dir for correct entry
dirEntry = FAT_GetDirEntry (dirCluster, 1, SEEK_SET);
while ( !found && !notFound)
{
// Match filename
found = true;
for (namePos = 0; (namePos < MAX_FILENAME_LENGTH) && found && (name[namePos] != '\0') && (lfnName[namePos] != '\0'); namePos++)
{
if (name[namePos] != ucase(lfnName[namePos]))
{
found = false;
}
}
if ((name[namePos] == '\0') != (lfnName[namePos] == '\0'))
{
found = false;
}
// Check against alias as well.
if (!found)
{
FAT_GetFilename(dirEntry, alias);
found = true;
for (namePos = 0; (namePos < 13) && found && (name[namePos] != '\0') && (alias[namePos] != '\0'); namePos++)
{
if (name[namePos] != ucase(alias[namePos]))
{
found = false;
}
}
if ((name[namePos] == '\0') != (alias[namePos] == '\0'))
{
found = false;
}
}
if (dirEntry.name[0] == FILE_FREE)
// Couldn't find specified file
{
found = false;
notFound = true;
}
if (!found && !notFound)
{
dirEntry = FAT_GetDirEntry (dirCluster, 1, SEEK_CUR);
}
}
if (found && ((dirEntry.attrib & ATTRIB_DIR) == ATTRIB_DIR) && (path[pathPos] != '\0'))
// It has found a directory from within the path that needs to be followed
{
found = false;
dirCluster = dirEntry.startCluster | (dirEntry.startClusterHigh << 16);
}
}
if (notFound)
{
dirEntry.name[0] = FILE_FREE;
dirEntry.attrib = 0x00;
}
return (dirEntry);
}
#ifdef CAN_WRITE_TO_DISC
/*-----------------------------------------------------------------
FAT_AddDirEntry
Creates a new dir entry for a file
Path separator is a forward slash /
const char* path: IN null terminated string of path to file.
DIR_ENT newDirEntry IN: The directory entry to use.
int file IN: The file being added (optional, use -1 if not used)
bool return OUT: true if successful
-----------------------------------------------------------------*/
bool FAT_AddDirEntry (const char* path, DIR_ENT newDirEntry)
{
char filename[MAX_FILENAME_LENGTH];
int filePos, pathPos, aliasPos;
char tempChar;
bool flagLFN, dotSeen;
char fileAlias[13] = {0};
int tailNum;
unsigned char chkSum = 0;
u32 oldWorkDirCluster;
DIR_ENT* dirEntries = (DIR_ENT*)globalBuffer;
u32 dirCluster;
int secOffset;
int entryOffset;
int maxSectors;
u32 firstSector;
DIR_ENT_LFN lfnEntry;
int lfnPos = 0;
int dirEntryLength = 0;
int dirEntryRemain = 0;
u32 tempDirCluster;
int tempSecOffset;
int tempEntryOffset;
bool dirEndFlag = false;
int i;
// Store current working directory
oldWorkDirCluster = curWorkDirCluster;
// Find filename within path and change to correct directory
if (path[0] == '/')
{
curWorkDirCluster = filesysRootDirClus;
}
pathPos = 0;
filePos = 0;
flagLFN = false;
while (path[pathPos + filePos] != '\0')
{
if (path[pathPos + filePos] == '/')
{
filename[filePos] = '\0';
if (FAT_chdir(filename) == false)
{
curWorkDirCluster = oldWorkDirCluster;
return false; // Couldn't change directory
}
pathPos += filePos + 1;
filePos = 0;
}
filename[filePos] = path[pathPos + filePos];
filePos++;
}
// Skip over last slashes
while (path[pathPos] == '/')
pathPos++;
// Check if the filename has a leading "."
// If so, it is an LFN
if (path[pathPos] == '.') {
flagLFN = true;
}
// Copy name from path
filePos = 0;
dotSeen = false;
while ((filePos < MAX_FILENAME_LENGTH - 1) && (path[pathPos] != '\0'))
{
filename[filePos] = path[pathPos];
if ((filename[filePos] <= ' ') || ((filename[filePos] >= ':') && (filename[filePos] <= '?'))) // Invalid character
{
flagLFN = true;
}
if (filename[filePos] == '.') {
if (!dotSeen) {
dotSeen = true;
} else {
flagLFN = true;
}
}
filePos++;
pathPos++;
if ((filePos > 8) && !dotSeen) {
flagLFN = true;
}
}
if (filePos == 0) // No filename
{
return false;
}
// Check if a long filename was specified
if (filePos > 12)
{
flagLFN = true;
}
// Check if extension is > 3 characters long
if (!flagLFN && (strrchr (filename, '.') != NULL) && (strlen(strrchr(filename, '.')) > 4)) {
flagLFN = true;
}
lfnPos = (filePos - 1) / 13;
// Add end of string char
filename[filePos++] = '\0';
// Clear remaining chars
while (filePos < MAX_FILENAME_LENGTH)
filename[filePos++] = 0x01; // Set for LFN compatibility
if (flagLFN)
{
// Generate short filename - always a 2 digit number for tail
// Get first 5 chars of alias from LFN
aliasPos = 0;
filePos = 0;
if (filename[filePos] == '.') {
filePos++;
}
for ( ; (aliasPos < 5) && (filename[filePos] != '\0') && (filename[filePos] != '.') ; filePos++)
{
tempChar = ucase(filename[filePos]);
if (((tempChar > ' ' && tempChar < ':') || tempChar > '?') && tempChar != '.')
fileAlias[aliasPos++] = tempChar;
}
// Pad Alias with underscores
while (aliasPos < 5)
fileAlias[aliasPos++] = '_';
fileAlias[5] = '~';
fileAlias[8] = '.';
fileAlias[9] = ' ';
fileAlias[10] = ' ';
fileAlias[11] = ' ';
if (strchr (filename, '.') != NULL) {
while(filename[filePos] != '\0')
{
filePos++;
if (filename[filePos] == '.')
{
pathPos = filePos;
}
}
filePos = pathPos + 1; //pathPos is used as a temporary variable
// Copy first 3 characters of extension
for (aliasPos = 9; (aliasPos < 12) && (filename[filePos] != '\0'); filePos++)
{
tempChar = ucase(filename[filePos]);
if ((tempChar > ' ' && tempChar < ':') || tempChar > '?')
fileAlias[aliasPos++] = tempChar;
}
} else {
aliasPos = 9;
}
// Pad Alias extension with spaces
while (aliasPos < 12)
fileAlias[aliasPos++] = ' ';
fileAlias[12] = '\0';
// Get a valid tail number
tailNum = 0;
do {
tailNum++;
fileAlias[6] = 0x30 + ((tailNum / 10) % 10); // 10's digit
fileAlias[7] = 0x30 + (tailNum % 10); // 1's digit
} while ((FAT_DirEntFromPath(fileAlias).name[0] != FILE_FREE) && (tailNum < 100));
if (tailNum < 100) // Found an alias not being used
{
// Calculate file checksum
chkSum = 0;
for (aliasPos=0; aliasPos < 12; aliasPos++)
{
// Skip '.'
if (fileAlias[aliasPos] == '.')
aliasPos++;
// NOTE: The operation is an unsigned char rotate right
chkSum = ((chkSum & 1) ? 0x80 : 0) + (chkSum >> 1) + fileAlias[aliasPos];
}
}
else // Couldn't find a valid alias
{
return false;
}
dirEntryLength = lfnPos + 2;
}
else // Its not a long file name
{
// Just copy alias straight from filename
for (aliasPos = 0; aliasPos < 13; aliasPos++)
{
tempChar = ucase(filename[aliasPos]);
if ((tempChar > ' ' && tempChar < ':') || tempChar > '?')
fileAlias[aliasPos] = tempChar;
}
fileAlias[12] = '\0';
lfnPos = -1;
dirEntryLength = 1;
}
// Change dirEntry name to match alias
for (aliasPos = 0; ((fileAlias[aliasPos] != '.') && (fileAlias[aliasPos] != '\0') && (aliasPos < 8)); aliasPos++)
{
newDirEntry.name[aliasPos] = fileAlias[aliasPos];
}
while (aliasPos < 8)
{
newDirEntry.name[aliasPos++] = ' ';
}
aliasPos = 0;
while ((fileAlias[aliasPos] != '.') && (fileAlias[aliasPos] != '\0'))
aliasPos++;
filePos = 0;
while (( filePos < 3 ) && (fileAlias[aliasPos] != '\0'))
{
tempChar = fileAlias[aliasPos++];
if ((tempChar > ' ' && tempChar < ':' && tempChar!='.') || tempChar > '?')
newDirEntry.ext[filePos++] = tempChar;
}
while (filePos < 3)
{
newDirEntry.ext[filePos++] = ' ';
}
// Scan Dir for free entry
dirCluster = curWorkDirCluster;
secOffset = 0;
entryOffset = 0;
maxSectors = (dirCluster == FAT16_ROOT_DIR_CLUSTER ? (filesysData - filesysRootDir) : filesysSecPerClus);
firstSector = (dirCluster == FAT16_ROOT_DIR_CLUSTER ? filesysRootDir : FAT_ClustToSect(dirCluster));
disc_ReadSector (firstSector + secOffset, dirEntries);
dirEntryRemain = dirEntryLength;
tempDirCluster = dirCluster;
tempSecOffset = secOffset;
tempEntryOffset = entryOffset;
// Search for a large enough space to fit in new directory entry
while ((dirEntries[entryOffset].name[0] != FILE_LAST) && (dirEntryRemain > 0))
{
entryOffset++;
if (entryOffset == BYTE_PER_READ / sizeof (DIR_ENT))
{
entryOffset = 0;
secOffset++;
if ((secOffset == filesysSecPerClus) && (dirCluster != FAT16_ROOT_DIR_CLUSTER))
{
secOffset = 0;
if (FAT_NextCluster(dirCluster) == CLUSTER_EOF)
{
dirCluster = FAT_LinkFreeCluster(dirCluster);
dirEntries[0].name[0] = FILE_LAST;
}
else
{
dirCluster = FAT_NextCluster(dirCluster);
}
firstSector = FAT_ClustToSect(dirCluster);
}
else if ((dirCluster == FAT16_ROOT_DIR_CLUSTER) && (secOffset == (filesysData - filesysRootDir)))
{
return false; // Got to end of root dir - can't fit in more files
}
disc_ReadSector (firstSector + secOffset, dirEntries);
}
if ((dirEntries[entryOffset].name[0] == FILE_FREE) || (dirEntries[entryOffset].name[0] == FILE_LAST) )
{
dirEntryRemain--;
} else {
dirEntryRemain = dirEntryLength;
tempDirCluster = dirCluster;
tempSecOffset = secOffset;
tempEntryOffset = entryOffset;
}
}
// Modifying the last directory is a special case - have to erase following entries
if (dirEntries[entryOffset].name[0] == FILE_LAST)
{
dirEndFlag = true;
}
// Recall last used entry
dirCluster = tempDirCluster;
secOffset = tempSecOffset;
entryOffset = tempEntryOffset;
dirEntryRemain = dirEntryLength;
// Re-read in first sector that will be written to
if (dirEndFlag && (entryOffset == 0)) {
memset (dirEntries, FILE_LAST, BYTE_PER_READ);
} else {
disc_ReadSector (firstSector + secOffset, dirEntries);
}
// Add new directory entry
while (dirEntryRemain > 0)
{
// Move to next entry, first pass advances from last used entry
entryOffset++;
if (entryOffset == BYTE_PER_READ / sizeof (DIR_ENT))
{
// Write out the current sector if we need to
entryOffset = 0;
if (dirEntryRemain < dirEntryLength) // Don't write out sector on first pass
{
disc_WriteSector (firstSector + secOffset, dirEntries);
}
secOffset++;
if ((secOffset == filesysSecPerClus) && (dirCluster != FAT16_ROOT_DIR_CLUSTER))
{
secOffset = 0;
if (FAT_NextCluster(dirCluster) == CLUSTER_EOF)
{
dirCluster = FAT_LinkFreeCluster(dirCluster);
dirEntries[0].name[0] = FILE_LAST;
}
else
{
dirCluster = FAT_NextCluster(dirCluster);
}
firstSector = FAT_ClustToSect(dirCluster);
}
else if ((dirCluster == FAT16_ROOT_DIR_CLUSTER) && (secOffset == (filesysData - filesysRootDir)))
{
return false; // Got to end of root dir - can't fit in more files
}
if (dirEndFlag)
{
memset (dirEntries, FILE_LAST, BYTE_PER_READ);
} else {
disc_ReadSector (firstSector + secOffset, dirEntries);
}
}
// Generate LFN entries
if (lfnPos >= 0)
{
lfnEntry.ordinal = (lfnPos + 1) | (dirEntryRemain == dirEntryLength ? LFN_END : 0);
for (i = 0; i < 13; i++) {
if (filename [lfnPos * 13 + i] == 0x01) {
((u8*)&lfnEntry)[(int)lfn_offset_table[i]] = 0xff;
((u8*)&lfnEntry)[(int)(lfn_offset_table[i]) + 1] = 0xff;
} else {
((u8*)&lfnEntry)[(int)lfn_offset_table[i]] = filename [lfnPos * 13 + i];
((u8*)&lfnEntry)[(int)(lfn_offset_table[i]) + 1] = 0x00;
}
}
lfnEntry.checkSum = chkSum;
lfnEntry.flag = ATTRIB_LFN;
lfnEntry.reserved1 = 0;
lfnEntry.reserved2 = 0;
*((DIR_ENT_LFN*)&dirEntries[entryOffset]) = lfnEntry;
lfnPos --;
lfnEntry.ordinal = 0;
} // end writing long filename entries
else
{
dirEntries[entryOffset] = newDirEntry;
if (dirEndFlag && (entryOffset < (BYTE_PER_READ / sizeof (DIR_ENT))) )
dirEntries[entryOffset+1].name[0] = FILE_LAST;
}
dirEntryRemain--;
}
// Write directory back to disk
disc_WriteSector (firstSector + secOffset, dirEntries);
// Change back to Working DIR
curWorkDirCluster = oldWorkDirCluster;
return true;
}
#endif
/*-----------------------------------------------------------------
FAT_FindNextFile
Gets the name of the next directory entry
(can be a file or subdirectory)
char* filename: OUT filename, must be at least 13 chars long
FILE_TYPE return: OUT returns FT_NONE if failed,
FT_FILE if it found a file and FT_DIR if it found a directory
-----------------------------------------------------------------*/
FILE_TYPE FAT_FindNextFile(char* filename)
{
// Get the next directory entry
DIR_ENT file;
file = FAT_GetDirEntry (curWorkDirCluster, 1, SEEK_CUR);
if (file.name[0] == FILE_FREE)
{
return FT_NONE; // Did not find a file
}
// Get the filename
if (filename != NULL)
FAT_GetFilename (file, filename);
if ((file.attrib & ATTRIB_DIR) != 0)
{
return FT_DIR; // Found a directory
}
else
{
return FT_FILE; // Found a file
}
}
/*-----------------------------------------------------------------
FAT_FindFirstFile
Gets the name of the first directory entry and resets the count
(can be a file or subdirectory)
char* filename: OUT filename, must be at least 13 chars long
FILE_TYPE return: OUT returns FT_NONE if failed,
FT_FILE if it found a file and FT_DIR if it found a directory
-----------------------------------------------------------------*/
FILE_TYPE FAT_FindFirstFile(char* filename)
{
// Get the first directory entry
DIR_ENT file;
file = FAT_GetDirEntry (curWorkDirCluster, 1, SEEK_SET);
if (file.name[0] == FILE_FREE)
{
return FT_NONE; // Did not find a file
}
// Get the filename
if (filename != NULL)
FAT_GetFilename (file, filename);
if ((file.attrib & ATTRIB_DIR) != 0)
{
return FT_DIR; // Found a directory
}
else
{
return FT_FILE; // Found a file
}
}
/*-----------------------------------------------------------------
FAT_FindFirstFileLFN
Gets the long file name of the first directory entry and resets
the count (can be a file or subdirectory)
char* lfn: OUT long file name, must be at least 256 chars long
FILE_TYPE return: OUT returns FT_NONE if failed,
FT_FILE if it found a file and FT_DIR if it found a directory
-----------------------------------------------------------------*/
FILE_TYPE FAT_FindFirstFileLFN(char* lfn)
{
FILE_TYPE type;
type = FAT_FindFirstFile(NULL);
FAT_GetLongFilename (lfn);
return type;
}
/*-----------------------------------------------------------------
FAT_FindNextFileLFN
Gets the long file name of the next directory entry
(can be a file or subdirectory)
char* lfn: OUT long file name, must be at least 256 chars long
FILE_TYPE return: OUT returns FT_NONE if failed,
FT_FILE if it found a file and FT_DIR if it found a directory
-----------------------------------------------------------------*/
FILE_TYPE FAT_FindNextFileLFN(char* lfn)
{
FILE_TYPE type;
type = FAT_FindNextFile(NULL);
FAT_GetLongFilename (lfn);
return type;
}
/*-----------------------------------------------------------------
FAT_FileExists
Returns the type of file
char* filename: IN filename of the file to look for
FILE_TYPE return: OUT returns FT_NONE if there is now file with
that name, FT_FILE if it is a file and FT_DIR if it is a directory
-----------------------------------------------------------------*/
FILE_TYPE FAT_FileExists(const char* filename)
{
DIR_ENT dirEntry;
// Get the dirEntry for the path specified
dirEntry = FAT_DirEntFromPath (filename);
if (dirEntry.name[0] == FILE_FREE)
{
return FT_NONE;
}
else if (dirEntry.attrib & ATTRIB_DIR)
{
return FT_DIR;
}
else
{
return FT_FILE;
}
}
/*-----------------------------------------------------------------
FAT_GetFileSystemType
FS_TYPE return: OUT returns the current file system type
-----------------------------------------------------------------*/
FS_TYPE FAT_GetFileSystemType (void)
{
return filesysType;
}
/*-----------------------------------------------------------------
FAT_GetFileSystemTotalSize
u32 return: OUT returns the total disk space (used + free)
-----------------------------------------------------------------*/
u32 FAT_GetFileSystemTotalSize (void)
{
return filesysTotalSize;
}
/*-----------------------------------------------------------------
FAT_chdir
Changes the current working directory
const char* path: IN null terminated string of directory separated by
forward slashes, / is root
bool return: OUT returns true if successful
-----------------------------------------------------------------*/
bool FAT_chdir (const char* path)
{
DIR_ENT dir;
if (path[0] == '/' && path[1] == '\0')
{
curWorkDirCluster = filesysRootDirClus;
return true;
}
if (path[0] == '\0') // Return true if changing relative to nothing
{
return true;
}
dir = FAT_DirEntFromPath (path);
if (((dir.attrib & ATTRIB_DIR) == ATTRIB_DIR) && (dir.name[0] != FILE_FREE))
{
// Change directory
curWorkDirCluster = dir.startCluster | (dir.startClusterHigh << 16);
// Move to correct cluster for root directory
if (curWorkDirCluster == FAT16_ROOT_DIR_CLUSTER)
{
curWorkDirCluster = filesysRootDirClus;
}
// Reset file position in directory
wrkDirCluster = curWorkDirCluster;
wrkDirSector = 0;
wrkDirOffset = -1;
return true;
}
else
{
// Couldn't change directory - wrong path specified
return false;
}
}
/*-----------------------------------------------------------------
FAT_fopen(filename, mode)
Opens a file
const char* path: IN null terminated string of filename and path
separated by forward slashes, / is root
const char* mode: IN mode to open file in
Supported modes: "r", "r+", "w", "w+", "a", "a+", don't use
"b" or "t" in any mode, as all files are openned in binary mode
FAT_FILE* return: OUT handle to open file, returns NULL if the file
couldn't be openned
-----------------------------------------------------------------*/
FAT_FILE* FAT_fopen(const char* path, const char* mode)
{
int fileNum;
FAT_FILE* file;
DIR_ENT dirEntry;
#ifdef CAN_WRITE_TO_DISC
u32 startCluster;
int clusCount;
#endif
char* pchTemp;
// Check that a valid mode was specified
pchTemp = strpbrk ( mode, "rRwWaA" );
if (pchTemp == NULL)
{
return NULL;
}
if (strpbrk ( pchTemp+1, "rRwWaA" ) != NULL)
{
return NULL;
}
// Get the dirEntry for the path specified
dirEntry = FAT_DirEntFromPath (path);
// Check that it is not a directory
if (dirEntry.attrib & ATTRIB_DIR)
{
return NULL;
}
#ifdef CAN_WRITE_TO_DISC
// Check that it is not a read only file being openned in a writing mode
if ( (strpbrk(mode, "wWaA+") != NULL) && (dirEntry.attrib & ATTRIB_RO))
{
return NULL;
}
#else
if ( (strpbrk(mode, "wWaA+") != NULL))
{
return NULL;
}
#endif
// Find a free file buffer
for (fileNum = 0; (fileNum < MAX_FILES_OPEN) && (openFiles[fileNum].inUse == true); fileNum++);
if (fileNum == MAX_FILES_OPEN) // No free files
{
return NULL;
}
file = &openFiles[fileNum];
// Remember where directory entry was
file->dirEntSector = (wrkDirCluster == FAT16_ROOT_DIR_CLUSTER ? filesysRootDir : FAT_ClustToSect(wrkDirCluster)) + wrkDirSector;
file->dirEntOffset = wrkDirOffset;
if ( strpbrk(mode, "rR") != NULL ) //(ucase(mode[0]) == 'R')
{
if (dirEntry.name[0] == FILE_FREE) // File must exist
{
return NULL;
}
file->read = true;
#ifdef CAN_WRITE_TO_DISC
file->write = ( strchr(mode, '+') != NULL ); //(mode[1] == '+');
#else
file->write = false;
#endif
file->append = false;
// Store information about position within the file, for use
// by FAT_fread, FAT_fseek, etc.
file->firstCluster = dirEntry.startCluster | (dirEntry.startClusterHigh << 16);
#ifdef CAN_WRITE_TO_DISC
// Check if file is openned for random. If it is, and currently has no cluster, one must be
// assigned to it.
if (file->write && file->firstCluster == CLUSTER_FREE)
{
file->firstCluster = FAT_LinkFreeCluster (CLUSTER_FREE);
if (file->firstCluster == CLUSTER_FREE) // Couldn't get a free cluster
{
return NULL;
}
// Store cluster position into the directory entry
dirEntry.startCluster = (file->firstCluster & 0xFFFF);
dirEntry.startClusterHigh = ((file->firstCluster >> 16) & 0xFFFF);
disc_ReadSector (file->dirEntSector, globalBuffer);
((DIR_ENT*) globalBuffer)[file->dirEntOffset] = dirEntry;
disc_WriteSector (file->dirEntSector, globalBuffer);
}
#endif
file->length = dirEntry.fileSize;
file->curPos = 0;
file->curClus = dirEntry.startCluster | (dirEntry.startClusterHigh << 16);
file->curSect = 0;
file->curByte = 0;
// Not appending
file->appByte = 0;
file->appClus = 0;
file->appSect = 0;
disc_ReadSector( FAT_ClustToSect( file->curClus), file->readBuffer);
file->inUse = true; // We're using this file now
return file;
} // mode "r"
#ifdef CAN_WRITE_TO_DISC
if ( strpbrk(mode, "wW") != NULL ) // (ucase(mode[0]) == 'W')
{
if (dirEntry.name[0] == FILE_FREE) // Create file if it doesn't exist
{
dirEntry.attrib = ATTRIB_ARCH;
dirEntry.reserved = 0;
// Time and date set to system time and date
dirEntry.cTime_ms = 0;
dirEntry.cTime = getRTCtoFileTime();
dirEntry.cDate = getRTCtoFileDate();
dirEntry.aDate = getRTCtoFileDate();
dirEntry.mTime = getRTCtoFileTime();
dirEntry.mDate = getRTCtoFileDate();
}
else // Already a file entry
{
// Free any clusters used
FAT_ClearLinks (dirEntry.startCluster | (dirEntry.startClusterHigh << 16));
}
// Get a cluster to use
startCluster = FAT_LinkFreeCluster (CLUSTER_FREE);
if (startCluster == CLUSTER_FREE) // Couldn't get a free cluster
{
return NULL;
}
// Store cluster position into the directory entry
dirEntry.startCluster = (startCluster & 0xFFFF);
dirEntry.startClusterHigh = ((startCluster >> 16) & 0xFFFF);
// The file has no data in it - its over written so should be empty
dirEntry.fileSize = 0;
if (dirEntry.name[0] == FILE_FREE) // No file
{
// Have to create a new entry
if(!FAT_AddDirEntry (path, dirEntry))
{
return NULL;
}
// Get the newly created dirEntry
dirEntry = FAT_DirEntFromPath (path);
// Remember where directory entry was
file->dirEntSector = (wrkDirCluster == FAT16_ROOT_DIR_CLUSTER ? filesysRootDir : FAT_ClustToSect(wrkDirCluster)) + wrkDirSector;
file->dirEntOffset = wrkDirOffset;
}
else // Already a file
{
// Just modify the old entry
disc_ReadSector (file->dirEntSector, globalBuffer);
((DIR_ENT*) globalBuffer)[file->dirEntOffset] = dirEntry;
disc_WriteSector (file->dirEntSector, globalBuffer);
}
// Now that file is created, open it
file->read = ( strchr(mode, '+') != NULL ); //(mode[1] == '+');
file->write = true;
file->append = false;
// Store information about position within the file, for use
// by FAT_fread, FAT_fseek, etc.
file->firstCluster = startCluster;
file->length = 0; // Should always have 0 bytes if openning in "w" mode
file->curPos = 0;
file->curClus = startCluster;
file->curSect = 0;
file->curByte = 0;
// Not appending
file->appByte = 0;
file->appClus = 0;
file->appSect = 0;
// Empty file, so empty read buffer
memset (file->readBuffer, 0, BYTE_PER_READ);
file->inUse = true; // We're using this file now
return file;
}
if ( strpbrk(mode, "aA") != NULL ) // (ucase(mode[0]) == 'A')
{
if (dirEntry.name[0] == FILE_FREE) // Create file if it doesn't exist
{
dirEntry.attrib = ATTRIB_ARCH;
dirEntry.reserved = 0;
// Time and date set to system time and date
dirEntry.cTime_ms = 0;
dirEntry.cTime = getRTCtoFileTime();
dirEntry.cDate = getRTCtoFileDate();
dirEntry.aDate = getRTCtoFileDate();
dirEntry.mTime = getRTCtoFileTime();
dirEntry.mDate = getRTCtoFileDate();
// The file has no data in it
dirEntry.fileSize = 0;
// Get a cluster to use
startCluster = FAT_LinkFreeCluster (CLUSTER_FREE);
if (startCluster == CLUSTER_FREE) // Couldn't get a free cluster
{
return NULL;
}
dirEntry.startCluster = (startCluster & 0xFFFF);
dirEntry.startClusterHigh = ((startCluster >> 16) & 0xFFFF);
if(!FAT_AddDirEntry (path, dirEntry))
return NULL;
// Get the newly created dirEntry
dirEntry = FAT_DirEntFromPath (path);
// Store append cluster
file->appClus = startCluster;
// Remember where directory entry was
file->dirEntSector = (wrkDirCluster == FAT16_ROOT_DIR_CLUSTER ? filesysRootDir : FAT_ClustToSect(wrkDirCluster)) + wrkDirSector;
file->dirEntOffset = wrkDirOffset;
}
else // File already exists - reuse the old directory entry
{
startCluster = dirEntry.startCluster | (dirEntry.startClusterHigh << 16);
// If it currently has no cluster, one must be assigned to it.
if (startCluster == CLUSTER_FREE)
{
file->firstCluster = FAT_LinkFreeCluster (CLUSTER_FREE);
if (file->firstCluster == CLUSTER_FREE) // Couldn't get a free cluster
{
return NULL;
}
// Store cluster position into the directory entry
dirEntry.startCluster = (file->firstCluster & 0xFFFF);
dirEntry.startClusterHigh = ((file->firstCluster >> 16) & 0xFFFF);
disc_ReadSector (file->dirEntSector, globalBuffer);
((DIR_ENT*) globalBuffer)[file->dirEntOffset] = dirEntry;
disc_WriteSector (file->dirEntSector, globalBuffer);
// Store append cluster
file->appClus = startCluster;
} else {
// Follow cluster list until last one is found
clusCount = dirEntry.fileSize / filesysBytePerClus;
file->appClus = startCluster;
while ((clusCount--) && (FAT_NextCluster (file->appClus) != CLUSTER_FREE) && (FAT_NextCluster (file->appClus) != CLUSTER_EOF))
{
file->appClus = FAT_NextCluster (file->appClus);
}
if (clusCount >= 0) // Check if ran out of clusters
{
// Set flag to allocate new cluster when needed
file->appSect = filesysSecPerClus;
file->appByte = 0;
}
}
}
// Now that file is created, open it
file->read = ( strchr(mode, '+') != NULL );
file->write = false;
file->append = true;
// Calculate the sector and byte of the current position,
// and store them
file->appSect = (dirEntry.fileSize % filesysBytePerClus) / BYTE_PER_READ;
file->appByte = dirEntry.fileSize % BYTE_PER_READ;
// Store information about position within the file, for use
// by FAT_fread, FAT_fseek, etc.
file->firstCluster = startCluster;
file->length = dirEntry.fileSize;
file->curPos = dirEntry.fileSize;
file->curClus = file->appClus;
file->curSect = file->appSect;
file->curByte = file->appByte;
// Read into buffer
disc_ReadSector( FAT_ClustToSect(file->curClus) + file->curSect, file->readBuffer);
file->inUse = true; // We're using this file now
return file;
}
#endif
// Can only reach here if a bad mode was specified
return NULL;
}
/*-----------------------------------------------------------------
FAT_fclose(file)
Closes a file
FAT_FILE* file: IN handle of the file to close
bool return OUT: true if successful, false if not
-----------------------------------------------------------------*/
bool FAT_fclose (FAT_FILE* file)
{
// Clear memory used by file information
if ((file != NULL) && (file->inUse == true))
{
#ifdef CAN_WRITE_TO_DISC
if (file->write || file->append)
{
// Write new length, time and date back to directory entry
disc_ReadSector (file->dirEntSector, globalBuffer);
((DIR_ENT*)globalBuffer)[file->dirEntOffset].fileSize = file->length;
((DIR_ENT*)globalBuffer)[file->dirEntOffset].mTime = getRTCtoFileTime();
((DIR_ENT*)globalBuffer)[file->dirEntOffset].mDate = getRTCtoFileDate();
((DIR_ENT*)globalBuffer)[file->dirEntOffset].aDate = getRTCtoFileDate();
disc_WriteSector (file->dirEntSector, globalBuffer);
// Flush any sectors in disc cache
disc_CacheFlush();
}
#endif
file->inUse = false;
return true;
}
else
{
return false;
}
}
/*-----------------------------------------------------------------
FAT_ftell(file)
Returns the current position in a file
FAT_FILE* file: IN handle of an open file
u32 OUT: Current position
-----------------------------------------------------------------*/
u32 FAT_ftell (FAT_FILE* file)
{
// Return the position as specified in the FAT_FILE structure
if ((file != NULL) && (file->inUse == true))
{
return file->curPos;
}
else
{
// Return -1 if no file was given
return -1;
}
}
/*-----------------------------------------------------------------
FAT_fseek(file, offset, origin)
Seeks to specified byte position in file
FAT_FILE* file: IN handle of an open file
s32 offset IN: position to seek to, relative to origin
int origin IN: origin to seek from
int OUT: Returns 0 if successful, -1 if not
-----------------------------------------------------------------*/
int FAT_fseek(FAT_FILE* file, s32 offset, int origin)
{
u32 cluster, nextCluster;
int clusCount;
u32 position;
u32 curPos;
if ((file == NULL) || (file->inUse == false)) // invalid file
{
return -1;
}
// Can't seek in append only mode
if (!file->read && !file->write)
{
return -1;
}
curPos = file->curPos;
switch (origin)
{
case SEEK_SET:
if (offset >= 0)
{
position = offset;
} else {
// Tried to seek before start of file
position = 0;
}
break;
case SEEK_CUR:
if (offset >= 0)
{
position = curPos + offset;
}
else if ( (u32)(offset * -1) >= curPos )
{
// Tried to seek before start of file
position = 0;
}
else
{
// Using u32 to maintain 32 bits of accuracy
position = curPos - (u32)(offset * -1);
}
break;
case SEEK_END:
if (offset >= 0)
{
// Seeking to end of file
position = file->length; // Fixed thanks to MoonLight
}
else if ( (u32)(offset * -1) >= file->length )
{
// Tried to seek before start of file
position = 0;
}
else
{
// Using u32 to maintain 32 bits of accuracy
position = file->length - (u32)(offset * -1);
}
break;
default:
return -1;
}
if (position > file->length)
{
// Tried to go past end of file
position = file->length;
}
// Save position
file->curPos = position;
// Calculate where the correct cluster is
if (position > curPos)
{
clusCount = (position - curPos + (file->curSect * filesysBytePerSec) + file->curByte) / filesysBytePerClus; // Fixed thanks to AgentQ
cluster = file->curClus;
} else {
clusCount = position / filesysBytePerClus;
cluster = file->firstCluster;
}
// Calculate the sector and byte of the current position,
// and store them
file->curSect = (position % filesysBytePerClus) / BYTE_PER_READ;
file->curByte = position % BYTE_PER_READ;
// Follow cluster list until desired one is found
if (clusCount > 0) // Only look at next cluster if need to
{
nextCluster = FAT_NextCluster (cluster);
} else {
nextCluster = cluster;
}
while ((clusCount--) && (nextCluster != CLUSTER_FREE) && (nextCluster != CLUSTER_EOF))
{
cluster = nextCluster;
nextCluster = FAT_NextCluster (cluster);
}
// Check if ran out of clusters, and the file is being written to
if ((clusCount >= 0) && (file->write || file->append))
{
// Set flag to allocate a new cluster
file->curSect = filesysSecPerClus;
file->curByte = 0;
}
file->curClus = cluster;
// Reload sector buffer for new position in file, if it is a different sector
if ((curPos ^ position) >= BYTE_PER_READ)
{
disc_ReadSector( file->curSect + FAT_ClustToSect(file->curClus), file->readBuffer);
}
return 0;
}
/*-----------------------------------------------------------------
FAT_fread(buffer, size, count, file)
Reads in size * count bytes into buffer from file, starting
from current position. It then sets the current position to the
byte after the last byte read. If it reaches the end of file
before filling the buffer then it stops reading.
void* buffer OUT: Pointer to buffer to fill. Should be at least as
big as the number of bytes required
u32 size IN: size of each item to read
u32 count IN: number of items to read
FAT_FILE* file IN: Handle of an open file
u32 OUT: returns the actual number of bytes read
-----------------------------------------------------------------*/
u32 FAT_fread (void* buffer, u32 size, u32 count, FAT_FILE* file)
{
int curByte;
int curSect;
u32 curClus;
u32 tempNextCluster;
int tempVar;
char* data = (char*)buffer;
u32 length = size * count;
u32 remain;
bool flagNoError = true;
// Can't read non-existant files
if ((file == NULL) || (file->inUse == false) || size == 0 || count == 0 || buffer == NULL)
return 0;
// Can only read files openned for reading
if (!file->read)
return 0;
// Don't read past end of file
if (length + file->curPos > file->length)
length = file->length - file->curPos;
remain = length;
curByte = file->curByte;
curSect = file->curSect;
curClus = file->curClus;
// Align to sector
tempVar = BYTE_PER_READ - curByte;
if (tempVar > remain)
tempVar = remain;
if ((tempVar < BYTE_PER_READ) && flagNoError)
{
memcpy(data, &(file->readBuffer[curByte]), tempVar);
remain -= tempVar;
data += tempVar;
curByte += tempVar;
if (curByte >= BYTE_PER_READ)
{
curByte = 0;
curSect++;
}
}
// align to cluster
// tempVar is number of sectors to read
if (remain > (filesysSecPerClus - curSect) * BYTE_PER_READ)
{
tempVar = filesysSecPerClus - curSect;
} else {
tempVar = remain / BYTE_PER_READ;
}
if ((tempVar > 0) && flagNoError)
{
disc_ReadSectors ( curSect + FAT_ClustToSect(curClus), tempVar, data);
data += tempVar * BYTE_PER_READ;
remain -= tempVar * BYTE_PER_READ;
curSect += tempVar;
}
// Move onto next cluster
// It should get to here without reading anything if a cluster is due to be allocated
if (curSect >= filesysSecPerClus)
{
tempNextCluster = FAT_NextCluster(curClus);
if ((remain == 0) && (tempNextCluster == CLUSTER_EOF))
{
curSect = filesysSecPerClus;
} else {
curSect = 0;
curClus = tempNextCluster;
if (curClus == CLUSTER_FREE)
{
flagNoError = false;
}
}
}
// Read in whole clusters
while ((remain >= filesysBytePerClus) && flagNoError)
{
disc_ReadSectors (FAT_ClustToSect(curClus), filesysSecPerClus, data);
data += filesysBytePerClus;
remain -= filesysBytePerClus;
// Advance to next cluster
tempNextCluster = FAT_NextCluster(curClus);
if ((remain == 0) && (tempNextCluster == CLUSTER_EOF))
{
curSect = filesysSecPerClus;
} else {
curSect = 0;
curClus = tempNextCluster;
if (curClus == CLUSTER_FREE)
{
flagNoError = false;
}
}
}
// Read remaining sectors
tempVar = remain / BYTE_PER_READ; // Number of sectors left
if ((tempVar > 0) && flagNoError)
{
disc_ReadSectors (FAT_ClustToSect(curClus), tempVar, data);
data += tempVar * BYTE_PER_READ;
remain -= tempVar * BYTE_PER_READ;
curSect += tempVar;
}
// Last remaining sector
// Check if sector wanted is different to the one started with
if ( ((file->curByte + length) >= BYTE_PER_READ) && flagNoError)
{
disc_ReadSector( curSect + FAT_ClustToSect( curClus), file->readBuffer);
if (remain > 0)
{
memcpy(data, file->readBuffer, remain);
curByte += remain;
remain = 0;
}
}
// Length read is the wanted length minus the stuff not read
length = length - remain;
// Update file information
file->curByte = curByte;
file->curSect = curSect;
file->curClus = curClus;
file->curPos = file->curPos + length;
return length;
}
#ifdef CAN_WRITE_TO_DISC
/*-----------------------------------------------------------------
FAT_fwrite(buffer, size, count, file)
Writes size * count bytes into file from buffer, starting
from current position. It then sets the current position to the
byte after the last byte written. If the file was openned in
append mode it always writes to the end of the file.
const void* buffer IN: Pointer to buffer containing data. Should be
at least as big as the number of bytes to be written.
u32 size IN: size of each item to write
u32 count IN: number of items to write
FAT_FILE* file IN: Handle of an open file
u32 OUT: returns the actual number of bytes written
-----------------------------------------------------------------*/
u32 FAT_fwrite (const void* buffer, u32 size, u32 count, FAT_FILE* file)
{
int curByte;
int curSect;
u32 curClus;
u32 tempNextCluster;
int tempVar;
u32 length = size * count;
u32 remain = length;
char* data = (char*)buffer;
char* writeBuffer;
bool flagNoError = true;
bool flagAppending = false;
if ((file == NULL) || (file->inUse == false) || length == 0 || buffer == NULL)
return 0;
if (file->write)
{
// Write at current read pointer
curByte = file->curByte;
curSect = file->curSect;
curClus = file->curClus;
// Use read buffer as write buffer
writeBuffer = file->readBuffer;
// If it is writing past the current end of file, set appending flag
if (length + file->curPos > file->length)
{
flagAppending = true;
}
}
else if (file->append)
{
// Write at end of file
curByte = file->appByte;
curSect = file->appSect;
curClus = file->appClus;
flagAppending = true;
// Use global buffer as write buffer, don't touch read buffer
writeBuffer = (char*)globalBuffer;
disc_ReadSector(curSect + FAT_ClustToSect(curClus), writeBuffer);
}
else
{
return 0;
}
// Move onto next cluster if needed
if (curSect >= filesysSecPerClus)
{
curSect = 0;
tempNextCluster = FAT_NextCluster(curClus);
if ((tempNextCluster == CLUSTER_EOF) || (tempNextCluster == CLUSTER_FREE))
{
// Ran out of clusters so get a new one
curClus = FAT_LinkFreeCluster(curClus);
if (curClus == CLUSTER_FREE) // Couldn't get a cluster, so abort
{
flagNoError = false;
}
memset(writeBuffer, 0, BYTE_PER_READ);
} else {
curClus = tempNextCluster;
disc_ReadSector( FAT_ClustToSect( curClus), writeBuffer);
}
}
// Align to sector
tempVar = BYTE_PER_READ - curByte;
if (tempVar > remain)
tempVar = remain;
if ((tempVar < BYTE_PER_READ) && flagNoError)
{
memcpy(&(writeBuffer[curByte]), data, tempVar);
remain -= tempVar;
data += tempVar;
curByte += tempVar;
// Write buffer back to disk
disc_WriteSector (curSect + FAT_ClustToSect(curClus), writeBuffer);
// Move onto next sector
if (curByte >= BYTE_PER_READ)
{
curByte = 0;
curSect++;
}
}
// Align to cluster
// tempVar is number of sectors to write
if (remain > (filesysSecPerClus - curSect) * BYTE_PER_READ)
{
tempVar = filesysSecPerClus - curSect;
} else {
tempVar = remain / BYTE_PER_READ;
}
if ((tempVar > 0) && flagNoError)
{
disc_WriteSectors ( curSect + FAT_ClustToSect(curClus), tempVar, data);
data += tempVar * BYTE_PER_READ;
remain -= tempVar * BYTE_PER_READ;
curSect += tempVar;
}
if (((curSect >= filesysSecPerClus) && flagNoError) && (remain > 0))
{
curSect = 0;
tempNextCluster = FAT_NextCluster(curClus);
if ((tempNextCluster == CLUSTER_EOF) || (tempNextCluster == CLUSTER_FREE))
{
// Ran out of clusters so get a new one
curClus = FAT_LinkFreeCluster(curClus);
if (curClus == CLUSTER_FREE) // Couldn't get a cluster, so abort
{
flagNoError = false;
}
} else {
curClus = tempNextCluster;
}
}
// Write whole clusters
while ((remain >= filesysBytePerClus) && flagNoError)
{
disc_WriteSectors (FAT_ClustToSect(curClus), filesysSecPerClus, data);
data += filesysBytePerClus;
remain -= filesysBytePerClus;
if (remain > 0)
{
tempNextCluster = FAT_NextCluster(curClus);
if ((tempNextCluster == CLUSTER_EOF) || (tempNextCluster == CLUSTER_FREE))
{
// Ran out of clusters so get a new one
curClus = FAT_LinkFreeCluster(curClus);
if (curClus == CLUSTER_FREE) // Couldn't get a cluster, so abort
{
flagNoError = false;
break;
}
} else {
curClus = tempNextCluster;
}
} else {
// Allocate a new cluster when next writing the file
curSect = filesysSecPerClus;
}
}
// Write remaining sectors
tempVar = remain / BYTE_PER_READ; // Number of sectors left
if ((tempVar > 0) && flagNoError)
{
disc_WriteSectors (FAT_ClustToSect(curClus), tempVar, data);
data += tempVar * BYTE_PER_READ;
remain -= tempVar * BYTE_PER_READ;
curSect += tempVar;
}
// Last remaining sector
// Check if sector wanted is different to the one started with
if ( (( (file->append ? file->appByte : file->curByte) + length) >= BYTE_PER_READ) && flagNoError)
{
if (flagAppending)
{
// Zero sector before using it
memset (writeBuffer, 0, BYTE_PER_READ);
} else {
// Modify existing sector
disc_ReadSector( curSect + FAT_ClustToSect( curClus), writeBuffer);
}
if (remain > 0) {
memcpy(writeBuffer, data, remain);
curByte += remain;
remain = 0;
disc_WriteSector( curSect + FAT_ClustToSect( curClus), writeBuffer);
}
}
// Amount read is the originally requested amount minus stuff remaining
length = length - remain;
// Update file information
if (file->write) // Writing also shifts the read pointer
{
file->curByte = curByte;
file->curSect = curSect;
file->curClus = curClus;
file->curPos = file->curPos + length;
if (file->length < file->curPos)
{
file->length = file->curPos;
}
}
else if (file->append) // Appending doesn't affect the read pointer
{
file->appByte = curByte;
file->appSect = curSect;
file->appClus = curClus;
file->length = file->length + length;
}
return length;
}
#endif
/*-----------------------------------------------------------------
FAT_feof(file)
Returns true if the end of file has been reached
FAT_FILE* file IN: Handle of an open file
bool return OUT: true if EOF, false if not
-----------------------------------------------------------------*/
bool FAT_feof(FAT_FILE* file)
{
if ((file == NULL) || (file->inUse == false))
return true; // Return eof on invalid files
return (file->length == file->curPos);
}
#ifdef CAN_WRITE_TO_DISC
/*-----------------------------------------------------------------
FAT_remove (path)
Deletes the file or empty directory sepecified in path
const char* path IN: Path of item to delete
int return OUT: zero if successful, non-zero if not
-----------------------------------------------------------------*/
int FAT_remove (const char* path)
{
DIR_ENT dirEntry;
u32 oldWorkDirCluster;
char checkFilename[13];
FILE_TYPE checkFiletype;
dirEntry = FAT_DirEntFromPath (path);
if (dirEntry.name[0] == FILE_FREE)
{
return -1;
}
// Only delete directories if the directory is entry
if (dirEntry.attrib & ATTRIB_DIR)
{
// Change to the directory temporarily
oldWorkDirCluster = curWorkDirCluster;
FAT_chdir(path);
// Search for files or directories, excluding the . and .. entries
checkFiletype = FAT_FindFirstFile (checkFilename);
while ((checkFilename[0] == '.') && (checkFiletype != FT_NONE))
{
checkFiletype = FAT_FindNextFile (checkFilename);
}
// Change back to working directory
curWorkDirCluster = oldWorkDirCluster;
// Check that the directory is empty
if (checkFiletype != FT_NONE)
{
// Directory isn't empty
return -1;
}
}
// Refresh directory information
dirEntry = FAT_DirEntFromPath (path);
// Free any clusters used
FAT_ClearLinks (dirEntry.startCluster | (dirEntry.startClusterHigh << 16));
// Remove Directory entry
disc_ReadSector ( (wrkDirCluster == FAT16_ROOT_DIR_CLUSTER ? filesysRootDir : FAT_ClustToSect(wrkDirCluster)) + wrkDirSector , globalBuffer);
((DIR_ENT*)globalBuffer)[wrkDirOffset].name[0] = FILE_FREE;
disc_WriteSector ( (wrkDirCluster == FAT16_ROOT_DIR_CLUSTER ? filesysRootDir : FAT_ClustToSect(wrkDirCluster)) + wrkDirSector , globalBuffer);
// Flush any sectors in disc cache
disc_CacheFlush();
return 0;
}
#endif
#ifdef CAN_WRITE_TO_DISC
/*-----------------------------------------------------------------
FAT_mkdir (path)
Makes a new directory, so long as no other directory or file has
the same name.
const char* path IN: Path and filename of directory to make
int return OUT: zero if successful, non-zero if not
-----------------------------------------------------------------*/
int FAT_mkdir (const char* path)
{
u32 newDirCluster;
u32 parentDirCluster;
DIR_ENT dirEntry;
DIR_ENT* entries = (DIR_ENT*)globalBuffer;
int i;
int pathPos, filePos;
char pathname[MAX_FILENAME_LENGTH];
u32 oldDirCluster;
if (FAT_FileExists(path) != FT_NONE)
{
return -1; // File or directory exists with that name
}
// Find filename within path and change to that directory
oldDirCluster = curWorkDirCluster;
if (path[0] == '/')
{
curWorkDirCluster = filesysRootDirClus;
}
pathPos = 0;
filePos = 0;
while (path[pathPos + filePos] != '\0')
{
if (path[pathPos + filePos] == '/')
{
pathname[filePos] = '\0';
if (FAT_chdir(pathname) == false)
{
curWorkDirCluster = oldDirCluster;
return -1; // Couldn't change directory
}
pathPos += filePos + 1;
filePos = 0;
}
pathname[filePos] = path[pathPos + filePos];
filePos++;
}
// Now grab the parent directory's cluster
parentDirCluster = curWorkDirCluster;
curWorkDirCluster = oldDirCluster;
// Get a new cluster for the file
newDirCluster = FAT_LinkFreeCluster(CLUSTER_FREE);
if (newDirCluster == CLUSTER_FREE)
{
return -1; // Couldn't get a new cluster for the directory
}
// Fill in directory entry's information
dirEntry.attrib = ATTRIB_DIR;
dirEntry.reserved = 0;
// Time and date set to system time and date
dirEntry.cTime_ms = 0;
dirEntry.cTime = getRTCtoFileTime();
dirEntry.cDate = getRTCtoFileDate();
dirEntry.aDate = getRTCtoFileDate();
dirEntry.mTime = getRTCtoFileTime();
dirEntry.mDate = getRTCtoFileDate();
// Store cluster position into the directory entry
dirEntry.startCluster = (newDirCluster & 0xFFFF);
dirEntry.startClusterHigh = ((newDirCluster >> 16) & 0xFFFF);
// The file has no data in it - its over written so should be empty
dirEntry.fileSize = 0;
if (FAT_AddDirEntry (path, dirEntry) == false)
{
return -1; // Couldn't add the directory entry
}
// Create the new directory itself
memset(entries, FILE_LAST, BYTE_PER_READ);
// Create . directory entry
dirEntry.name[0] = '.';
// Fill name and extension with spaces
for (i = 1; i < 11; i++)
{
dirEntry.name[i] = ' ';
}
memcpy(entries, &dirEntry, sizeof(dirEntry));
// Create .. directory entry
dirEntry.name[1] = '.';
dirEntry.startCluster = (parentDirCluster & 0xFFFF);
dirEntry.startClusterHigh = ((parentDirCluster >> 16) & 0xFFFF);
memcpy(&entries[1], &dirEntry, sizeof(dirEntry));
// Write entry to disc
disc_WriteSector(FAT_ClustToSect(newDirCluster), entries);
// Flush any sectors in disc cache
disc_CacheFlush();
return 0;
}
#endif
/*-----------------------------------------------------------------
FAT_fgetc (handle)
Gets the next character in the file
FAT_FILE* file IN: Handle of open file
bool return OUT: character if successful, EOF if not
-----------------------------------------------------------------*/
char FAT_fgetc (FAT_FILE* file)
{
char c;
return (FAT_fread(&c, 1, 1, file) == 1) ? c : EOF;
}
#ifdef CAN_WRITE_TO_DISC
/*-----------------------------------------------------------------
FAT_fputc (character, handle)
Writes the given character into the file
char c IN: Character to be written
FAT_FILE* file IN: Handle of open file
bool return OUT: character if successful, EOF if not
-----------------------------------------------------------------*/
char FAT_fputc (char c, FAT_FILE* file)
{
return (FAT_fwrite(&c, 1, 1, file) == 1) ? c : EOF;
}
#endif
/*-----------------------------------------------------------------
FAT_fgets (char *tgtBuffer, int num, FAT_FILE* file)
Gets a up to num bytes from file, stopping at the first
newline.
CAUTION: does not do strictly streaming. I.e. it's
reading more then needed bytes and seeking back.
shouldn't matter for random access
char *tgtBuffer OUT: buffer to write to
int num IN: size of target buffer
FAT_FILE* file IN: Handle of open file
bool return OUT: character if successful, EOF if not
Written by MightyMax
Modified by Chishm - 2005-11-17
* Added check for unix style text files
* Removed seek when no newline is found, since it isn't necessary
-------------------------------------------------------------------*/
char *FAT_fgets(char *tgtBuffer, int num, FAT_FILE* file)
{
u32 curPos;
u32 readLength;
char *returnChar;
// invalid filehandle
if (file == NULL)
{
return NULL ;
}
// end of file
if (FAT_feof(file)==true)
{
return NULL ;
}
// save current position
curPos = FAT_ftell(file);
// read the full buffer (max string chars is num-1 and one end of string \0
readLength = FAT_fread(tgtBuffer,1,num-1,file) ;
// mark least possible end of string
tgtBuffer[readLength] = '\0' ;
if (readLength==0) {
// return error
return NULL ;
}
// get position of first return '\r'
returnChar = strchr(tgtBuffer,'\r');
// if no return is found, search for a newline
if (returnChar == NULL)
{
returnChar = strchr(tgtBuffer,'\n');
}
// Mark the return, if existant, as end of line/string
if (returnChar!=NULL) {
*returnChar++ = 0 ;
if (*returnChar=='\n') { // catch newline too when jumping over the end
// return to location after \r\n (strlen+2)
FAT_fseek(file,curPos+strlen(tgtBuffer)+2,SEEK_SET) ;
return tgtBuffer ;
} else {
// return to location after \r (strlen+1)
FAT_fseek(file,curPos+strlen(tgtBuffer)+1,SEEK_SET) ;
return tgtBuffer ;
}
}
return tgtBuffer ;
}
#ifdef CAN_WRITE_TO_DISC
/*-----------------------------------------------------------------
FAT_fputs (const char *string, FAT_FILE* file)
Writes string to file, excluding end of string character
const char *string IN: string to write
FAT_FILE* file IN: Handle of open file
bool return OUT: number of characters written if successful,
EOF if not
Written by MightyMax
Modified by Chishm - 2005-11-17
* Uses FAT_FILE instead of int
* writtenBytes is now u32 instead of int
-------------------------------------------------------------------*/
int FAT_fputs (const char *string, FAT_FILE* file)
{
u32 writtenBytes;
// save string except end of string '\0'
writtenBytes = FAT_fwrite((void *)string, 1, strlen(string), file);
// check if we had an error
if (writtenBytes != strlen(string))
{
// return EOF error
return EOF;
}
// return the charcount written
return writtenBytes ;
}
#endif
|