-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathfat.c
2551 lines (2240 loc) · 75.2 KB
/
fat.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (c) 2006-2012 by Roland Riegel <[email protected]>
*
* This file is free software; you can redistribute it and/or modify
* it under the terms of either the GNU General Public License version 2
* or the GNU Lesser General Public License version 2.1, both as
* published by the Free Software Foundation.
*/
#include "byteordering.h"
#include "partition.h"
#include "fat.h"
#include "fat_config.h"
#include "sd-reader_config.h"
#include <string.h>
#if USE_DYNAMIC_MEMORY
#include <stdlib.h>
#endif
/**
* \addtogroup fat FAT support
*
* This module implements FAT16/FAT32 read and write access.
*
* The following features are supported:
* - File names up to 31 characters long.
* - Unlimited depth of subdirectories.
* - Short 8.3 and long filenames.
* - Creating and deleting files.
* - Reading and writing from and to files.
* - File resizing.
* - File sizes of up to 4 gigabytes.
*
* @{
*/
/**
* \file
* FAT implementation (license: GPLv2 or LGPLv2.1)
*
* \author Roland Riegel
*/
/**
* \addtogroup fat_config FAT configuration
* Preprocessor defines to configure the FAT implementation.
*/
/**
* \addtogroup fat_fs FAT access
* Basic functions for handling a FAT filesystem.
*/
/**
* \addtogroup fat_file FAT file functions
* Functions for managing files.
*/
/**
* \addtogroup fat_dir FAT directory functions
* Functions for managing directories.
*/
/**
* @}
*/
#define FAT16_CLUSTER_FREE 0x0000
#define FAT16_CLUSTER_RESERVED_MIN 0xfff0
#define FAT16_CLUSTER_RESERVED_MAX 0xfff6
#define FAT16_CLUSTER_BAD 0xfff7
#define FAT16_CLUSTER_LAST_MIN 0xfff8
#define FAT16_CLUSTER_LAST_MAX 0xffff
#define FAT32_CLUSTER_FREE 0x00000000
#define FAT32_CLUSTER_RESERVED_MIN 0x0ffffff0
#define FAT32_CLUSTER_RESERVED_MAX 0x0ffffff6
#define FAT32_CLUSTER_BAD 0x0ffffff7
#define FAT32_CLUSTER_LAST_MIN 0x0ffffff8
#define FAT32_CLUSTER_LAST_MAX 0x0fffffff
#define FAT_DIRENTRY_DELETED 0xe5
#define FAT_DIRENTRY_LFNLAST (1 << 6)
#define FAT_DIRENTRY_LFNSEQMASK ((1 << 6) - 1)
/* Each entry within the directory table has a size of 32 bytes
* and either contains a 8.3 DOS-style file name or a part of a
* long file name, which may consist of several directory table
* entries at once.
*
* multi-byte integer values are stored little-endian!
*
* 8.3 file name entry:
* ====================
* offset length description
* 0 8 name (space padded)
* 8 3 extension (space padded)
* 11 1 attributes (FAT_ATTRIB_*)
*
* long file name (lfn) entry ordering for a single file name:
* ===========================================================
* LFN entry n
* ...
* LFN entry 2
* LFN entry 1
* 8.3 entry (see above)
*
* lfn entry:
* ==========
* offset length description
* 0 1 ordinal field
* 1 2 unicode character 1
* 3 3 unicode character 2
* 5 3 unicode character 3
* 7 3 unicode character 4
* 9 3 unicode character 5
* 11 1 attribute (always 0x0f)
* 12 1 type (reserved, always 0)
* 13 1 checksum
* 14 2 unicode character 6
* 16 2 unicode character 7
* 18 2 unicode character 8
* 20 2 unicode character 9
* 22 2 unicode character 10
* 24 2 unicode character 11
* 26 2 cluster (unused, always 0)
* 28 2 unicode character 12
* 30 2 unicode character 13
*
* The ordinal field contains a descending number, from n to 1.
* For the n'th lfn entry the ordinal field is or'ed with 0x40.
* For deleted lfn entries, the ordinal field is set to 0xe5.
*/
struct fat_header_struct
{
offset_t size;
offset_t fat_offset;
uint32_t fat_size;
uint16_t sector_size;
uint16_t cluster_size;
offset_t cluster_zero_offset;
offset_t root_dir_offset;
#if FAT_FAT32_SUPPORT
cluster_t root_dir_cluster;
#endif
};
struct fat_fs_struct
{
struct partition_struct* partition;
struct fat_header_struct header;
cluster_t cluster_free;
};
struct fat_file_struct
{
struct fat_fs_struct* fs;
struct fat_dir_entry_struct dir_entry;
offset_t pos;
cluster_t pos_cluster;
};
struct fat_dir_struct
{
struct fat_fs_struct* fs;
struct fat_dir_entry_struct dir_entry;
cluster_t entry_cluster;
uint16_t entry_offset;
};
struct fat_read_dir_callback_arg
{
struct fat_dir_entry_struct* dir_entry;
uintptr_t bytes_read;
#if FAT_LFN_SUPPORT
uint8_t checksum;
#endif
uint8_t finished;
};
struct fat_usage_count_callback_arg
{
cluster_t cluster_count;
uintptr_t buffer_size;
};
#if !USE_DYNAMIC_MEMORY
static struct fat_fs_struct fat_fs_handles[FAT_FS_COUNT];
static struct fat_file_struct fat_file_handles[FAT_FILE_COUNT];
static struct fat_dir_struct fat_dir_handles[FAT_DIR_COUNT];
#endif
static uint8_t fat_read_header(struct fat_fs_struct* fs);
static cluster_t fat_get_next_cluster(const struct fat_fs_struct* fs, cluster_t cluster_num);
static offset_t fat_cluster_offset(const struct fat_fs_struct* fs, cluster_t cluster_num);
static uint8_t fat_dir_entry_read_callback(uint8_t* buffer, offset_t offset, void* p);
#if FAT_LFN_SUPPORT
static uint8_t fat_calc_83_checksum(const uint8_t* file_name_83);
#endif
static uint8_t fat_get_fs_free_16_callback(uint8_t* buffer, offset_t offset, void* p);
#if FAT_FAT32_SUPPORT
static uint8_t fat_get_fs_free_32_callback(uint8_t* buffer, offset_t offset, void* p);
#endif
#if FAT_WRITE_SUPPORT
static cluster_t fat_append_clusters(struct fat_fs_struct* fs, cluster_t cluster_num, cluster_t count);
static uint8_t fat_free_clusters(struct fat_fs_struct* fs, cluster_t cluster_num);
static uint8_t fat_terminate_clusters(struct fat_fs_struct* fs, cluster_t cluster_num);
static uint8_t fat_clear_cluster(const struct fat_fs_struct* fs, cluster_t cluster_num);
static uintptr_t fat_clear_cluster_callback(uint8_t* buffer, offset_t offset, void* p);
static offset_t fat_find_offset_for_dir_entry(struct fat_fs_struct* fs, const struct fat_dir_struct* parent, const struct fat_dir_entry_struct* dir_entry);
static uint8_t fat_write_dir_entry(const struct fat_fs_struct* fs, struct fat_dir_entry_struct* dir_entry);
#if FAT_DATETIME_SUPPORT
static void fat_set_file_modification_date(struct fat_dir_entry_struct* dir_entry, uint16_t year, uint8_t month, uint8_t day);
static void fat_set_file_modification_time(struct fat_dir_entry_struct* dir_entry, uint8_t hour, uint8_t min, uint8_t sec);
#endif
#endif
/**
* \ingroup fat_fs
* Opens a FAT filesystem.
*
* \param[in] partition Discriptor of partition on which the filesystem resides.
* \returns 0 on error, a FAT filesystem descriptor on success.
* \see fat_close
*/
struct fat_fs_struct* fat_open(struct partition_struct* partition)
{
if(!partition ||
#if FAT_WRITE_SUPPORT
!partition->device_write ||
!partition->device_write_interval
#else
0
#endif
)
return 0;
#if USE_DYNAMIC_MEMORY
struct fat_fs_struct* fs = malloc(sizeof(*fs));
if(!fs)
return 0;
#else
struct fat_fs_struct* fs = fat_fs_handles;
uint8_t i;
for(i = 0; i < FAT_FS_COUNT; ++i)
{
if(!fs->partition)
break;
++fs;
}
if(i >= FAT_FS_COUNT)
return 0;
#endif
memset(fs, 0, sizeof(*fs));
fs->partition = partition;
if(!fat_read_header(fs))
{
#if USE_DYNAMIC_MEMORY
free(fs);
#else
fs->partition = 0;
#endif
return 0;
}
return fs;
}
/**
* \ingroup fat_fs
* Closes a FAT filesystem.
*
* When this function returns, the given filesystem descriptor
* will be invalid.
*
* \param[in] fs The filesystem to close.
* \see fat_open
*/
void fat_close(struct fat_fs_struct* fs)
{
if(!fs)
return;
#if USE_DYNAMIC_MEMORY
free(fs);
#else
fs->partition = 0;
#endif
}
/**
* \ingroup fat_fs
* Reads and parses the header of a FAT filesystem.
*
* \param[in,out] fs The filesystem for which to parse the header.
* \returns 0 on failure, 1 on success.
*/
uint8_t fat_read_header(struct fat_fs_struct* fs)
{
if(!fs)
return 0;
struct partition_struct* partition = fs->partition;
if(!partition)
return 0;
/* read fat parameters */
#if FAT_FAT32_SUPPORT
uint8_t buffer[37];
#else
uint8_t buffer[25];
#endif
offset_t partition_offset = (offset_t) partition->offset * 512;
if(!partition->device_read(partition_offset + 0x0b, buffer, sizeof(buffer)))
return 0;
uint16_t bytes_per_sector = read16(&buffer[0x00]);
uint16_t reserved_sectors = read16(&buffer[0x03]);
uint8_t sectors_per_cluster = buffer[0x02];
uint8_t fat_copies = buffer[0x05];
uint16_t max_root_entries = read16(&buffer[0x06]);
uint16_t sector_count_16 = read16(&buffer[0x08]);
uint16_t sectors_per_fat = read16(&buffer[0x0b]);
uint32_t sector_count = read32(&buffer[0x15]);
#if FAT_FAT32_SUPPORT
uint32_t sectors_per_fat32 = read32(&buffer[0x19]);
uint32_t cluster_root_dir = read32(&buffer[0x21]);
#endif
if(sector_count == 0)
{
if(sector_count_16 == 0)
/* illegal volume size */
return 0;
else
sector_count = sector_count_16;
}
#if FAT_FAT32_SUPPORT
if(sectors_per_fat != 0)
sectors_per_fat32 = sectors_per_fat;
else if(sectors_per_fat32 == 0)
/* this is neither FAT16 nor FAT32 */
return 0;
#else
if(sectors_per_fat == 0)
/* this is not a FAT16 */
return 0;
#endif
/* determine the type of FAT we have here */
uint32_t data_sector_count = sector_count
- reserved_sectors
#if FAT_FAT32_SUPPORT
- sectors_per_fat32 * fat_copies
#else
- (uint32_t) sectors_per_fat * fat_copies
#endif
- ((max_root_entries * 32 + bytes_per_sector - 1) / bytes_per_sector);
uint32_t data_cluster_count = data_sector_count / sectors_per_cluster;
if(data_cluster_count < 4085)
/* this is a FAT12, not supported */
return 0;
else if(data_cluster_count < 65525)
/* this is a FAT16 */
partition->type = PARTITION_TYPE_FAT16;
else
/* this is a FAT32 */
partition->type = PARTITION_TYPE_FAT32;
/* fill header information */
struct fat_header_struct* header = &fs->header;
memset(header, 0, sizeof(*header));
header->size = (offset_t) sector_count * bytes_per_sector;
header->fat_offset = /* jump to partition */
partition_offset +
/* jump to fat */
(offset_t) reserved_sectors * bytes_per_sector;
header->fat_size = (data_cluster_count + 2) * (partition->type == PARTITION_TYPE_FAT16 ? 2 : 4);
header->sector_size = bytes_per_sector;
header->cluster_size = (uint16_t) bytes_per_sector * sectors_per_cluster;
#if FAT_FAT32_SUPPORT
if(partition->type == PARTITION_TYPE_FAT16)
#endif
{
header->root_dir_offset = /* jump to fats */
header->fat_offset +
/* jump to root directory entries */
(offset_t) fat_copies * sectors_per_fat * bytes_per_sector;
header->cluster_zero_offset = /* jump to root directory entries */
header->root_dir_offset +
/* skip root directory entries */
(offset_t) max_root_entries * 32;
}
#if FAT_FAT32_SUPPORT
else
{
header->cluster_zero_offset = /* jump to fats */
header->fat_offset +
/* skip fats */
(offset_t) fat_copies * sectors_per_fat32 * bytes_per_sector;
header->root_dir_cluster = cluster_root_dir;
}
#endif
return 1;
}
/**
* \ingroup fat_fs
* Retrieves the next following cluster of a given cluster.
*
* Using the filesystem file allocation table, this function returns
* the number of the cluster containing the data directly following
* the data within the cluster with the given number.
*
* \param[in] fs The filesystem for which to determine the next cluster.
* \param[in] cluster_num The number of the cluster for which to determine its successor.
* \returns The wanted cluster number, or 0 on error.
*/
cluster_t fat_get_next_cluster(const struct fat_fs_struct* fs, cluster_t cluster_num)
{
if(!fs || cluster_num < 2)
return 0;
#if FAT_FAT32_SUPPORT
if(fs->partition->type == PARTITION_TYPE_FAT32)
{
/* read appropriate fat entry */
uint32_t fat_entry;
if(!fs->partition->device_read(fs->header.fat_offset + (offset_t) cluster_num * sizeof(fat_entry), (uint8_t*) &fat_entry, sizeof(fat_entry)))
return 0;
/* determine next cluster from fat */
cluster_num = ltoh32(fat_entry);
if(cluster_num == FAT32_CLUSTER_FREE ||
cluster_num == FAT32_CLUSTER_BAD ||
(cluster_num >= FAT32_CLUSTER_RESERVED_MIN && cluster_num <= FAT32_CLUSTER_RESERVED_MAX) ||
(cluster_num >= FAT32_CLUSTER_LAST_MIN && cluster_num <= FAT32_CLUSTER_LAST_MAX))
return 0;
}
else
#endif
{
/* read appropriate fat entry */
uint16_t fat_entry;
if(!fs->partition->device_read(fs->header.fat_offset + (offset_t) cluster_num * sizeof(fat_entry), (uint8_t*) &fat_entry, sizeof(fat_entry)))
return 0;
/* determine next cluster from fat */
cluster_num = ltoh16(fat_entry);
if(cluster_num == FAT16_CLUSTER_FREE ||
cluster_num == FAT16_CLUSTER_BAD ||
(cluster_num >= FAT16_CLUSTER_RESERVED_MIN && cluster_num <= FAT16_CLUSTER_RESERVED_MAX) ||
(cluster_num >= FAT16_CLUSTER_LAST_MIN && cluster_num <= FAT16_CLUSTER_LAST_MAX))
return 0;
}
return cluster_num;
}
#if DOXYGEN || FAT_WRITE_SUPPORT
/**
* \ingroup fat_fs
* Appends a new cluster chain to an existing one.
*
* Set cluster_num to zero to create a completely new one.
*
* \param[in] fs The file system on which to operate.
* \param[in] cluster_num The cluster to which to append the new chain.
* \param[in] count The number of clusters to allocate.
* \returns 0 on failure, the number of the first new cluster on success.
*/
cluster_t fat_append_clusters(struct fat_fs_struct* fs, cluster_t cluster_num, cluster_t count)
{
if(!fs)
return 0;
device_read_t device_read = fs->partition->device_read;
device_write_t device_write = fs->partition->device_write;
offset_t fat_offset = fs->header.fat_offset;
cluster_t count_left = count;
cluster_t cluster_current = fs->cluster_free;
cluster_t cluster_next = 0;
cluster_t cluster_count;
uint16_t fat_entry16;
#if FAT_FAT32_SUPPORT
uint32_t fat_entry32;
uint8_t is_fat32 = (fs->partition->type == PARTITION_TYPE_FAT32);
if(is_fat32)
cluster_count = fs->header.fat_size / sizeof(fat_entry32);
else
#endif
cluster_count = fs->header.fat_size / sizeof(fat_entry16);
fs->cluster_free = 0;
for(cluster_t cluster_left = cluster_count; cluster_left > 0; --cluster_left, ++cluster_current)
{
if(cluster_current < 2 || cluster_current >= cluster_count)
cluster_current = 2;
#if FAT_FAT32_SUPPORT
if(is_fat32)
{
if(!device_read(fat_offset + (offset_t) cluster_current * sizeof(fat_entry32), (uint8_t*) &fat_entry32, sizeof(fat_entry32)))
return 0;
}
else
#endif
{
if(!device_read(fat_offset + (offset_t) cluster_current * sizeof(fat_entry16), (uint8_t*) &fat_entry16, sizeof(fat_entry16)))
return 0;
}
#if FAT_FAT32_SUPPORT
if(is_fat32)
{
/* check if this is a free cluster */
if(fat_entry32 != HTOL32(FAT32_CLUSTER_FREE))
continue;
/* If we don't need this free cluster for the
* current allocation, we keep it in mind for
* the next time.
*/
if(count_left == 0)
{
fs->cluster_free = cluster_current;
break;
}
/* allocate cluster */
if(cluster_next == 0)
fat_entry32 = HTOL32(FAT32_CLUSTER_LAST_MAX);
else
fat_entry32 = htol32(cluster_next);
if(!device_write(fat_offset + (offset_t) cluster_current * sizeof(fat_entry32), (uint8_t*) &fat_entry32, sizeof(fat_entry32)))
break;
}
else
#endif
{
/* check if this is a free cluster */
if(fat_entry16 != HTOL16(FAT16_CLUSTER_FREE))
continue;
/* If we don't need this free cluster for the
* current allocation, we keep it in mind for
* the next time.
*/
if(count_left == 0)
{
fs->cluster_free = cluster_current;
break;
}
/* allocate cluster */
if(cluster_next == 0)
fat_entry16 = HTOL16(FAT16_CLUSTER_LAST_MAX);
else
fat_entry16 = htol16((uint16_t) cluster_next);
if(!device_write(fat_offset + (offset_t) cluster_current * sizeof(fat_entry16), (uint8_t*) &fat_entry16, sizeof(fat_entry16)))
break;
}
cluster_next = cluster_current;
--count_left;
}
do
{
if(count_left > 0)
break;
/* We allocated a new cluster chain. Now join
* it with the existing one (if any).
*/
if(cluster_num >= 2)
{
#if FAT_FAT32_SUPPORT
if(is_fat32)
{
fat_entry32 = htol32(cluster_next);
if(!device_write(fat_offset + (offset_t) cluster_num * sizeof(fat_entry32), (uint8_t*) &fat_entry32, sizeof(fat_entry32)))
break;
}
else
#endif
{
fat_entry16 = htol16((uint16_t) cluster_next);
if(!device_write(fat_offset + (offset_t) cluster_num * sizeof(fat_entry16), (uint8_t*) &fat_entry16, sizeof(fat_entry16)))
break;
}
}
return cluster_next;
} while(0);
/* No space left on device or writing error.
* Free up all clusters already allocated.
*/
fat_free_clusters(fs, cluster_next);
return 0;
}
#endif
#if DOXYGEN || FAT_WRITE_SUPPORT
/**
* \ingroup fat_fs
* Frees a cluster chain, or a part thereof.
*
* Marks the specified cluster and all clusters which are sequentially
* referenced by it as free. They may then be used again for future
* file allocations.
*
* \note If this function is used for freeing just a part of a cluster
* chain, the new end of the chain is not correctly terminated
* within the FAT. Use fat_terminate_clusters() instead.
*
* \param[in] fs The filesystem on which to operate.
* \param[in] cluster_num The starting cluster of the chain which to free.
* \returns 0 on failure, 1 on success.
* \see fat_terminate_clusters
*/
uint8_t fat_free_clusters(struct fat_fs_struct* fs, cluster_t cluster_num)
{
if(!fs || cluster_num < 2)
return 0;
offset_t fat_offset = fs->header.fat_offset;
#if FAT_FAT32_SUPPORT
if(fs->partition->type == PARTITION_TYPE_FAT32)
{
uint32_t fat_entry;
while(cluster_num)
{
if(!fs->partition->device_read(fat_offset + (offset_t) cluster_num * sizeof(fat_entry), (uint8_t*) &fat_entry, sizeof(fat_entry)))
return 0;
/* get next cluster of current cluster before freeing current cluster */
uint32_t cluster_num_next = ltoh32(fat_entry);
if(cluster_num_next == FAT32_CLUSTER_FREE)
return 1;
if(cluster_num_next == FAT32_CLUSTER_BAD ||
(cluster_num_next >= FAT32_CLUSTER_RESERVED_MIN &&
cluster_num_next <= FAT32_CLUSTER_RESERVED_MAX
)
)
return 0;
if(cluster_num_next >= FAT32_CLUSTER_LAST_MIN && cluster_num_next <= FAT32_CLUSTER_LAST_MAX)
cluster_num_next = 0;
/* We know we will free the cluster, so remember it as
* free for the next allocation.
*/
if(!fs->cluster_free)
fs->cluster_free = cluster_num;
/* free cluster */
fat_entry = HTOL32(FAT32_CLUSTER_FREE);
fs->partition->device_write(fat_offset + (offset_t) cluster_num * sizeof(fat_entry), (uint8_t*) &fat_entry, sizeof(fat_entry));
/* We continue in any case here, even if freeing the cluster failed.
* The cluster is lost, but maybe we can still free up some later ones.
*/
cluster_num = cluster_num_next;
}
}
else
#endif
{
uint16_t fat_entry;
while(cluster_num)
{
if(!fs->partition->device_read(fat_offset + (offset_t) cluster_num * sizeof(fat_entry), (uint8_t*) &fat_entry, sizeof(fat_entry)))
return 0;
/* get next cluster of current cluster before freeing current cluster */
uint16_t cluster_num_next = ltoh16(fat_entry);
if(cluster_num_next == FAT16_CLUSTER_FREE)
return 1;
if(cluster_num_next == FAT16_CLUSTER_BAD ||
(cluster_num_next >= FAT16_CLUSTER_RESERVED_MIN &&
cluster_num_next <= FAT16_CLUSTER_RESERVED_MAX
)
)
return 0;
if(cluster_num_next >= FAT16_CLUSTER_LAST_MIN && cluster_num_next <= FAT16_CLUSTER_LAST_MAX)
cluster_num_next = 0;
/* free cluster */
fat_entry = HTOL16(FAT16_CLUSTER_FREE);
fs->partition->device_write(fat_offset + (offset_t) cluster_num * sizeof(fat_entry), (uint8_t*) &fat_entry, sizeof(fat_entry));
/* We continue in any case here, even if freeing the cluster failed.
* The cluster is lost, but maybe we can still free up some later ones.
*/
cluster_num = cluster_num_next;
}
}
return 1;
}
#endif
#if DOXYGEN || FAT_WRITE_SUPPORT
/**
* \ingroup fat_fs
* Frees a part of a cluster chain and correctly terminates the rest.
*
* Marks the specified cluster as the new end of a cluster chain and
* frees all following clusters.
*
* \param[in] fs The filesystem on which to operate.
* \param[in] cluster_num The new end of the cluster chain.
* \returns 0 on failure, 1 on success.
* \see fat_free_clusters
*/
uint8_t fat_terminate_clusters(struct fat_fs_struct* fs, cluster_t cluster_num)
{
if(!fs || cluster_num < 2)
return 0;
/* fetch next cluster before overwriting the cluster entry */
cluster_t cluster_num_next = fat_get_next_cluster(fs, cluster_num);
/* mark cluster as the last one */
#if FAT_FAT32_SUPPORT
if(fs->partition->type == PARTITION_TYPE_FAT32)
{
uint32_t fat_entry = HTOL32(FAT32_CLUSTER_LAST_MAX);
if(!fs->partition->device_write(fs->header.fat_offset + (offset_t) cluster_num * sizeof(fat_entry), (uint8_t*) &fat_entry, sizeof(fat_entry)))
return 0;
}
else
#endif
{
uint16_t fat_entry = HTOL16(FAT16_CLUSTER_LAST_MAX);
if(!fs->partition->device_write(fs->header.fat_offset + (offset_t) cluster_num * sizeof(fat_entry), (uint8_t*) &fat_entry, sizeof(fat_entry)))
return 0;
}
/* free remaining clusters */
if(cluster_num_next)
return fat_free_clusters(fs, cluster_num_next);
else
return 1;
}
#endif
#if DOXYGEN || FAT_WRITE_SUPPORT
/**
* \ingroup fat_fs
* Clears a single cluster.
*
* The complete cluster is filled with zeros.
*
* \param[in] fs The filesystem on which to operate.
* \param[in] cluster_num The cluster to clear.
* \returns 0 on failure, 1 on success.
*/
uint8_t fat_clear_cluster(const struct fat_fs_struct* fs, cluster_t cluster_num)
{
if(cluster_num < 2)
return 0;
offset_t cluster_offset = fat_cluster_offset(fs, cluster_num);
uint8_t zero[16];
memset(zero, 0, sizeof(zero));
return fs->partition->device_write_interval(cluster_offset,
zero,
fs->header.cluster_size,
fat_clear_cluster_callback,
0
);
}
#endif
#if DOXYGEN || FAT_WRITE_SUPPORT
/**
* \ingroup fat_fs
* Callback function for clearing a cluster.
*/
uintptr_t fat_clear_cluster_callback(uint8_t* buffer, offset_t offset, void* p)
{
return 16;
}
#endif
/**
* \ingroup fat_fs
* Calculates the offset of the specified cluster.
*
* \param[in] fs The filesystem on which to operate.
* \param[in] cluster_num The cluster whose offset to calculate.
* \returns The cluster offset.
*/
offset_t fat_cluster_offset(const struct fat_fs_struct* fs, cluster_t cluster_num)
{
if(!fs || cluster_num < 2)
return 0;
return fs->header.cluster_zero_offset + (offset_t) (cluster_num - 2) * fs->header.cluster_size;
}
/**
* \ingroup fat_file
* Retrieves the directory entry of a path.
*
* The given path may both describe a file or a directory.
*
* \param[in] fs The FAT filesystem on which to search.
* \param[in] path The path of which to read the directory entry.
* \param[out] dir_entry The directory entry to fill.
* \returns 0 on failure, 1 on success.
* \see fat_read_dir
*/
uint8_t fat_get_dir_entry_of_path(struct fat_fs_struct* fs, const char* path, struct fat_dir_entry_struct* dir_entry)
{
if(!fs || !path || path[0] == '\0' || !dir_entry)
return 0;
if(path[0] == '/')
++path;
/* begin with the root directory */
memset(dir_entry, 0, sizeof(*dir_entry));
dir_entry->attributes = FAT_ATTRIB_DIR;
while(1)
{
if(path[0] == '\0')
return 1;
struct fat_dir_struct* dd = fat_open_dir(fs, dir_entry);
if(!dd)
break;
/* extract the next hierarchy we will search for */
const char* sub_path = strchr(path, '/');
uint8_t length_to_sep;
if(sub_path)
{
length_to_sep = sub_path - path;
++sub_path;
}
else
{
length_to_sep = strlen(path);
sub_path = path + length_to_sep;
}
/* read directory entries */
while(fat_read_dir(dd, dir_entry))
{
/* check if we have found the next hierarchy */
if((strlen(dir_entry->long_name) != length_to_sep ||
strncmp(path, dir_entry->long_name, length_to_sep) != 0))
continue;
fat_close_dir(dd);
dd = 0;
if(path[length_to_sep] == '\0')
/* we iterated through the whole path and have found the file */
return 1;
if(dir_entry->attributes & FAT_ATTRIB_DIR)
{
/* we found a parent directory of the file we are searching for */
path = sub_path;
break;
}
/* a parent of the file exists, but not the file itself */
return 0;
}
fat_close_dir(dd);
}
return 0;
}
/**
* \ingroup fat_file
* Opens a file on a FAT filesystem.
*
* \param[in] fs The filesystem on which the file to open lies.
* \param[in] dir_entry The directory entry of the file to open.
* \returns The file handle, or 0 on failure.
* \see fat_close_file
*/
struct fat_file_struct* fat_open_file(struct fat_fs_struct* fs, const struct fat_dir_entry_struct* dir_entry)
{
if(!fs || !dir_entry || (dir_entry->attributes & FAT_ATTRIB_DIR))
return 0;
#if USE_DYNAMIC_MEMORY
struct fat_file_struct* fd = malloc(sizeof(*fd));
if(!fd)
return 0;
#else
struct fat_file_struct* fd = fat_file_handles;
uint8_t i;
for(i = 0; i < FAT_FILE_COUNT; ++i)
{
if(!fd->fs)
break;
++fd;
}
if(i >= FAT_FILE_COUNT)
return 0;
#endif
memcpy(&fd->dir_entry, dir_entry, sizeof(*dir_entry));
fd->fs = fs;
fd->pos = 0;
fd->pos_cluster = dir_entry->cluster;
return fd;
}
/**
* \ingroup fat_file
* Closes a file.
*
* \param[in] fd The file handle of the file to close.
* \see fat_open_file
*/
void fat_close_file(struct fat_file_struct* fd)
{
if(fd)
{
#if FAT_DELAY_DIRENTRY_UPDATE
/* write directory entry */
fat_write_dir_entry(fd->fs, &fd->dir_entry);
#endif
#if USE_DYNAMIC_MEMORY
free(fd);
#else
fd->fs = 0;
#endif
}
}
/**
* \ingroup fat_file
* Reads data from a file.
*
* The data requested is read from the current file location.
*
* \param[in] fd The file handle of the file from which to read.
* \param[out] buffer The buffer into which to write.
* \param[in] buffer_len The amount of data to read.
* \returns The number of bytes read, 0 on end of file, or -1 on failure.
* \see fat_write_file
*/
intptr_t fat_read_file(struct fat_file_struct* fd, uint8_t* buffer, uintptr_t buffer_len)
{
/* check arguments */
if(!fd || !buffer || buffer_len < 1)
return -1;
/* determine number of bytes to read */
if(fd->pos + buffer_len > fd->dir_entry.file_size)
buffer_len = fd->dir_entry.file_size - fd->pos;