-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathfat.c
1716 lines (1372 loc) · 38.8 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
// SPDX-License-Identifier: BSD-3-Clause
// Copyright (c) 2025, Bjørn Brodtkorb. All rights reserved.
#include "fat.h"
#include <string.h>
//------------------------------------------------------------------------------
#define LIMIT(a, b) ((a) < (b) ? (a) : (b))
#define FSINFO_HEAD_SIG 0x41615252
#define FSINFO_STRUCT_SIG 0x61417272
#define FSINFO_TAIL_SIG 0xaa550000
#define MBR_PART_OFF 446
#define EXT_FLAG_MIRROR (1 << 7)
#define EXT_FLAG_ACT 0x000f
#define EXT_FLAG_SECOND 0x0001
#define LFN_HEAD_MSK 0x40
#define LFN_SEQ_MSK 0x1f
#define SFN_FREE 0xe5
#define SFN_LAST 0x00
#define SFN_PAD 0x20
//------------------------------------------------------------------------------
enum
{
FAT_BUF_DIRTY = 0x01,
FAT_INFO_DIRTY = 0x02,
};
enum
{
CLUST_FREE = 0x01,
CLUST_USED = 0x02,
CLUST_LAST = 0x04,
CLUST_BAD = 0x08,
};
//------------------------------------------------------------------------------
typedef struct __attribute__((packed))
{
uint8_t boot[446];
struct
{
uint8_t status;
uint8_t reserved_0[3];
uint8_t type;
uint8_t reserved_1[3];
uint32_t lba;
uint32_t size;
} part[4];
uint16_t sig;
} Mbr;
typedef struct __attribute__((packed))
{
uint8_t jump[3];
char name[8];
uint16_t bytes_per_sect;
uint8_t sect_per_clust;
uint16_t res_sect_cnt;
uint8_t fat_cnt;
uint16_t root_ent_cnt;
uint16_t sect_cnt_16;
uint8_t media;
uint16_t sect_per_fat_16;
uint16_t sect_per_track;
uint16_t head_cnt;
uint32_t hidden_sect_cnt;
uint32_t sect_cnt_32;
uint32_t sect_per_fat_32;
uint16_t ext_flags;
uint8_t minor;
uint8_t major;
uint32_t root_cluster;
uint16_t info_sect;
uint16_t copy_bpb_sector;
uint8_t reserved_0[12];
uint8_t drive_num;
uint8_t reserved_1;
uint8_t boot_sig;
uint32_t volume_id;
char volume_label[11];
char fs_type[8];
uint8_t reserved_2[420];
uint8_t sign[2];
} Bpb;
typedef struct __attribute__((packed))
{
uint32_t head_sig;
uint8_t reserved_0[480];
uint32_t struct_sig;
uint32_t free_cnt;
uint32_t next_free;
uint8_t reserved_1[12];
uint32_t tail_sig;
} FsInfo;
typedef struct __attribute__((packed))
{
uint8_t name[11];
uint8_t attr;
uint8_t reserved;
uint8_t tenth;
uint16_t cre_time;
uint16_t cre_date;
uint16_t acc_date;
uint16_t clust_hi;
uint16_t mod_time;
uint16_t mod_date;
uint16_t clust_lo;
uint32_t size;
} Sfn;
typedef union __attribute__((packed))
{
uint8_t raw[32];
struct
{
uint8_t seq;
uint8_t name0[10];
uint8_t attr;
uint8_t type;
uint8_t crc;
uint8_t name1[12];
uint16_t clust;
uint8_t name2[4];
};
} Lfn;
typedef struct
{
uint32_t sect;
uint16_t idx;
} Loc;
//------------------------------------------------------------------------------
static Fat* g_fat_list;
static uint8_t g_lfn_indices[13] = {1, 3, 5, 7, 9, 14, 16, 18, 20, 22, 24, 28, 30};
static uint8_t g_buf[512];
static uint16_t g_len;
static uint8_t g_crc;
//------------------------------------------------------------------------------
static char to_upper(char c)
{
return (c >= 'a' && c <= 'z') ? c & ~0x20 : c;
}
//------------------------------------------------------------------------------
static int subpath_len(const char* path)
{
int i;
for (i = 0; path[i] && path[i] != '/'; i++);
return i;
}
//------------------------------------------------------------------------------
static int last_subpath_len(const char* path)
{
int len = subpath_len(path);
if (len == 0)
return 0;
path += len;
// Verify last subpath
while (*path == '/')
path++;
return *path ? 0 : len;
}
//------------------------------------------------------------------------------
static uint8_t get_crc(const uint8_t* name)
{
uint8_t sum = 0;
for (int i = 0; i < 11; i++)
sum = ((sum & 1) << 7) + (sum >> 1) + name[i];
return sum;
}
//------------------------------------------------------------------------------
static void decode_timestamp(uint16_t date, uint16_t time, Timestamp* ts)
{
ts->day = date & 0x1f;
ts->month = (date >> 5) & 0xf;
ts->year = ((date >> 9) & 0x3f) + 1980;
ts->hour = (time >> 11) & 0x1f;
ts->min = (time >> 5) & 0x3f;
ts->sec = 2 * (time & 0x1f);
}
//------------------------------------------------------------------------------
static void encode_timestamp(uint16_t* date, uint16_t* time)
{
Timestamp ts;
fat_get_timestamp(&ts);
*date = ((ts.year - 1980) & 0x3f) << 9 | (ts.month & 0xf) << 5 | (ts.day & 0x1f);
*time = ((ts.sec / 2) & 0x1f) | (ts.min & 0x3f) << 5 | (ts.hour & 0x1f) << 11;
}
//------------------------------------------------------------------------------
static Fat* find_fat_volume(const char* name, int len)
{
for (Fat* it = g_fat_list; it; it = it->next)
{
if (len == it->name_len && !memcmp(name, it->name, len))
return it;
}
return NULL;
}
//------------------------------------------------------------------------------
static uint32_t sect_to_clust(Fat* fat, uint32_t sect)
{
return ((sect - fat->data_sect) >> fat->clust_shift) + 2;
}
//------------------------------------------------------------------------------
static uint32_t clust_to_sect(Fat* fat, uint32_t clust)
{
return ((clust - 2) << fat->clust_shift) + fat->data_sect;
}
//------------------------------------------------------------------------------
static int sync_buf(Fat* fat)
{
if (fat->flags & FAT_BUF_DIRTY)
{
if (!fat->ops.write(fat->buf, fat->sect))
return FAT_ERR_IO;
fat->flags &= ~FAT_BUF_DIRTY;
}
return FAT_ERR_NONE;
}
//------------------------------------------------------------------------------
static int update_buf(Fat* fat, uint32_t sect)
{
if (fat->sect != sect)
{
int err = sync_buf(fat);
if (err)
return err;
if (!fat->ops.read(fat->buf, sect))
return FAT_ERR_IO;
fat->sect = sect;
}
return FAT_ERR_NONE;
}
//------------------------------------------------------------------------------
static int sync_fs(Fat* fat)
{
int err = sync_buf(fat);
if (err)
return err;
if (fat->flags & FAT_INFO_DIRTY)
{
err = update_buf(fat, fat->info_sect);
if (err)
return err;
FsInfo* info = (FsInfo*)fat->buf;
fat->flags |= FAT_BUF_DIRTY;
info->next_free = fat->last_used;
info->free_cnt = fat->free_cnt;
err = sync_buf(fat);
if (err)
return err;
fat->flags &= ~FAT_INFO_DIRTY;
}
return FAT_ERR_NONE;
}
//------------------------------------------------------------------------------
static int get_fat(Fat* fat, uint32_t clust, uint32_t* out_val, uint8_t* out_flags)
{
uint32_t* items = (uint32_t*)fat->buf;
uint32_t sect = fat->fat_sect[0] + clust / 128; // Active FAT
uint32_t idx = clust % 128;
int err = update_buf(fat, sect);
if (err)
return err;
// Upper nibble is ignored
uint32_t val = items[idx] & 0x0fffffff;
uint8_t flags;
if (val == 0)
flags = CLUST_FREE;
else if (val == 0x0ffffff7)
flags = CLUST_BAD;
else if (val >= 0x0ffffff8)
flags = CLUST_USED | CLUST_LAST;
else if (val >= 2 && val < fat->clust_cnt)
flags = CLUST_USED;
else
return FAT_ERR_BROKEN;
*out_val = val;
*out_flags = flags;
return FAT_ERR_NONE;
}
//------------------------------------------------------------------------------
static int put_fat2(Fat* fat, uint32_t fat_sect, uint32_t clust, uint32_t val)
{
uint32_t* items = (uint32_t*)fat->buf;
uint32_t sect = fat_sect + clust / 128;
uint16_t idx = clust % 128;
int err = update_buf(fat, sect);
if (err)
return err;
// Upper nibble must be preserved
items[idx] = (items[idx] & 0xf0000000) | (val & 0x0fffffff);
fat->flags |= FAT_BUF_DIRTY;
return FAT_ERR_NONE;
}
//------------------------------------------------------------------------------
static int put_fat(Fat* fat, uint32_t clust, uint32_t val)
{
if (fat->fat_sect[1]) // Mirroring enabled
{
int err = put_fat2(fat, fat->fat_sect[1], clust, val);
if (err)
return err;
}
return put_fat2(fat, fat->fat_sect[0], clust, val);
}
//------------------------------------------------------------------------------
static int remove_chain(Fat* fat, uint32_t clust)
{
uint8_t flags;
uint32_t next;
fat->flags |= FAT_INFO_DIRTY;
for (;;)
{
int err = get_fat(fat, clust, &next, &flags);
if (err)
return err;
if (flags & (CLUST_BAD | CLUST_FREE))
return FAT_ERR_BROKEN;
err = put_fat(fat, clust, 0);
if (err)
return err;
fat->free_cnt++;
clust = next;
if (flags & CLUST_LAST)
break;
}
return sync_fs(fat);
}
//------------------------------------------------------------------------------
static int stretch_chain(Fat* fat, uint32_t clust, uint32_t* out_clust)
{
int err;
uint8_t flags;
uint32_t next;
uint32_t prev = clust;
bool scan = true;
fat->flags |= FAT_INFO_DIRTY;
if (prev)
{
// Stretching. Check next cluster.
if (++clust >= fat->clust_cnt)
clust = 2;
err = get_fat(fat, clust, &next, &flags);
if (err)
return err;
if (flags & CLUST_FREE)
scan = false;
}
if (scan)
{
clust = fat->last_used;
for (;;)
{
if (++clust >= fat->clust_cnt)
clust = 2;
if (clust == fat->last_used)
return FAT_ERR_FULL;
err = get_fat(fat, clust, &next, &flags);
if (err)
return flags;
if (flags & CLUST_FREE)
break;
}
}
err = put_fat(fat, clust, 0x0fffffff); // EOC
if (err)
return err;
if (prev)
{
// Stretching. Add link.
err = put_fat(fat, prev, clust);
if (err)
return err;
}
fat->last_used = clust;
fat->free_cnt--;
*out_clust = clust;
return sync_fs(fat);
}
//------------------------------------------------------------------------------
static int create_chain(Fat* fat, uint32_t* out_clust)
{
return stretch_chain(fat, 0, out_clust);
}
//------------------------------------------------------------------------------
static int clust_clear(Fat* fat, uint32_t clust)
{
int err = sync_buf(fat);
if (err)
return err;
uint32_t sect = clust_to_sect(fat, clust);
memset(fat->buf, 0, 512);
for (int i = 0; i < (1 << fat->clust_shift); i++)
{
fat->flags |= FAT_BUF_DIRTY;
fat->sect = sect++;
err = sync_buf(fat);
if (err)
return err;
}
return FAT_ERR_NONE;
}
//------------------------------------------------------------------------------
static void dir_at_clust(Dir* dir, uint32_t clust)
{
dir->clust = clust;
dir->sect = clust_to_sect(dir->fat, clust);
dir->idx = 0;
}
//------------------------------------------------------------------------------
static void dir_enter(Dir* dir, uint32_t clust)
{
// Cluster is zero for .. entries pointing to root
if (clust == 0)
clust = dir->fat->root_clust;
dir->sclust = clust;
dir_at_clust(dir, clust);
}
//------------------------------------------------------------------------------
static int dir_next(Dir* dir)
{
dir->idx += sizeof(Sfn);
if (dir->idx < 512)
return FAT_ERR_NONE;
dir->idx = 0;
dir->sect++;
if (dir->sect & dir->fat->clust_msk) // Still in same cluster
return FAT_ERR_NONE;
uint8_t flags;
uint32_t next;
int err = get_fat(dir->fat, dir->clust, &next, &flags);
if (err)
return err;
if (flags & (CLUST_BAD | CLUST_FREE))
return FAT_ERR_BROKEN;
if (flags & CLUST_LAST) // EOC
return FAT_ERR_EOF;
dir_at_clust(dir, next);
return FAT_ERR_NONE;
}
//------------------------------------------------------------------------------
static int dir_advance(Dir* dir, int cnt)
{
for (int i = 0; i < cnt; i++)
{
int err = dir_next(dir);
if (err)
return err;
}
return FAT_ERR_NONE;
}
//------------------------------------------------------------------------------
static int dir_next_stretch(Dir* dir)
{
int err = dir_next(dir);
if (err != FAT_ERR_EOF)
return err;
uint32_t next;
err = stretch_chain(dir->fat, dir->clust, &next);
if (err)
return err;
dir_at_clust(dir, next);
return clust_clear(dir->fat, dir->clust);
}
//------------------------------------------------------------------------------
static void* dir_ptr(Dir* dir)
{
return dir->fat->buf + dir->idx;
}
//------------------------------------------------------------------------------
static bool sfn_is_last(Sfn* sfn)
{
return sfn->name[0] == SFN_LAST;
}
//------------------------------------------------------------------------------
static bool sfn_is_free(Sfn* sfn)
{
return sfn->name[0] == SFN_LAST || sfn->name[0] == SFN_FREE;
}
//------------------------------------------------------------------------------
static bool sfn_is_lfn(Sfn* sfn)
{
return sfn->attr == FAT_ATTR_LFN;
}
//------------------------------------------------------------------------------
static uint32_t sfn_cluster(Sfn* sfn)
{
return sfn->clust_hi << 16 | sfn->clust_lo;
}
//------------------------------------------------------------------------------
// Only certain characters are allowed in an SFN file name. Invalid characters
// are converted to underscore. It does not follow Windows' algorithm, using
// ~N for duplicate names, since it relies on LFN names only.
static char sfn_char(char c)
{
const char* str = "!#$%&'()-@^_`{}~ "; // Allowed special characters
c = to_upper(c);
if (c >= 'A' && c <= 'Z')
return c;
for (int i = 0; str[i]; i++)
{
if (c == str[i])
return c;
}
return '_';
}
//------------------------------------------------------------------------------
static void put_sfn_name(uint8_t* sfn_name, const char* name, int len)
{
int i, j;
for (i = 0; i < LIMIT(len, 8) && name[i] != '.'; i++)
sfn_name[i] = sfn_char(name[i]);
for (j = i; j < 8; j++)
sfn_name[j] = SFN_PAD;
while (i < len && name[i++] != '.');
for (j = 0; j < 3 && i < len; j++, i++)
sfn_name[8 + j] = sfn_char(name[i]);
for (; j < 3; j++)
sfn_name[8 + j] = SFN_PAD;
}
//------------------------------------------------------------------------------
static void parse_sfn_name(uint8_t* sfn_name)
{
uint8_t* ptr = g_buf;
for (int i = 0; i < 8 && sfn_name[i] != SFN_PAD; i++)
*ptr++ = sfn_name[i];
if (sfn_name[8] != SFN_PAD)
*ptr++ = '.';
for (int i = 8; i < 11 && sfn_name[i] != SFN_PAD; i++)
*ptr++ = sfn_name[i];
g_len = ptr - g_buf;
}
//------------------------------------------------------------------------------
static void put_lfn_name_frag(Lfn* lfn, const char* name, int len)
{
int i;
for (i = 0; i < len; i++)
{
lfn->raw[g_lfn_indices[i] + 0] = name[i];
lfn->raw[g_lfn_indices[i] + 1] = 0x00;
}
if (i < 13)
{
lfn->raw[g_lfn_indices[i] + 0] = 0x00;
lfn->raw[g_lfn_indices[i] + 1] = 0x00;
while (++i < 13)
{
lfn->raw[g_lfn_indices[i] + 0] = 0xff;
lfn->raw[g_lfn_indices[i] + 1] = 0xff;
}
}
}
//------------------------------------------------------------------------------
static int parse_lfn_name(Dir* dir)
{
int err = update_buf(dir->fat, dir->sect);
if (err) return err;
Lfn* lfn = dir_ptr(dir);
g_crc = lfn->crc;
g_len = 0;
if (0 == (lfn->seq & LFN_HEAD_MSK))
return FAT_ERR_BROKEN;
int cnt = lfn->seq & LFN_SEQ_MSK;
if (cnt > 20)
return FAT_ERR_BROKEN;
while (cnt--)
{
if (lfn->attr != FAT_ATTR_LFN || lfn->crc != g_crc)
return FAT_ERR_BROKEN;
for (int i = 0; i < 13; i++)
{
char c = lfn->raw[g_lfn_indices[i]];
if (c == 0xff)
return FAT_ERR_BROKEN; // 0x00 must be first
if (c == 0x00)
break;
g_buf[13 * cnt + i] = c;
g_len++;
}
err = dir_next(dir);
if (err)
return err;
err = update_buf(dir->fat, dir->sect);
if (err)
return err;
lfn = dir_ptr(dir);
}
return g_len <= 255 ? FAT_ERR_NONE : FAT_ERR_BROKEN;
}
//------------------------------------------------------------------------------
static int dir_search(Dir* dir, const char* name, int len, Loc* loc)
{
// Should it support matching SFN?
uint8_t sfn_name[11];
put_sfn_name(sfn_name, name, len);
dir_at_clust(dir, dir->sclust);
for (int err = 0;; err = dir_next(dir))
{
if (err)
return err;
err = update_buf(dir->fat, dir->sect);
if (err)
return err;
Sfn* sfn = dir_ptr(dir);
if (sfn_is_last(sfn))
return FAT_ERR_EOF;
if (sfn_is_free(sfn))
continue;
if (loc)
{
// Update the start location (SFN or first LFN). Used when removing entries.
loc->sect = dir->sect;
loc->idx = dir->idx;
}
if (sfn_is_lfn(sfn))
{
err = parse_lfn_name(dir);
if (err)
return err;
sfn = dir_ptr(dir);
if (sfn_is_free(sfn) || sfn_is_lfn(sfn) || g_crc != get_crc(sfn->name))
return FAT_ERR_BROKEN;
if (g_len == len && !memcmp(g_buf, name, len))
return FAT_ERR_NONE;
}
else
{
if (!memcmp(sfn_name, sfn->name, sizeof(sfn_name)))
return FAT_ERR_NONE;
}
}
}
//------------------------------------------------------------------------------
static bool dir_at_root(Dir* dir)
{
return dir->clust == dir->fat->root_clust &&
dir->sect == clust_to_sect(dir->fat, dir->fat->root_clust) &&
dir->idx == 0;
}
//------------------------------------------------------------------------------
static int follow_path(Dir* dir, const char** path, Loc* loc)
{
int err, len;
uint32_t dir_clust;
bool dir_enterable;
const char* str = *path;
if (*str++ != '/')
return FAT_ERR_PATH;
len = subpath_len(str);
if (len == 0)
return FAT_ERR_PATH;
dir->fat = find_fat_volume(str, len);
if (!dir->fat)
return FAT_ERR_PATH;
// Enter root by default (no entry points to it)
dir_enter(dir, dir->fat->root_clust);
dir_clust = dir->clust;
dir_enterable = true;
str += len;
*path = str;
for (;;)
{
while (*str == '/')
str++;
*path = str;
len = subpath_len(str);
if (len == 0)
return FAT_ERR_NONE; // Do not enter directory. Dir points to the SFN of path.
if (!dir_enterable)
return FAT_ERR_PATH;
dir_enter(dir, dir_clust);
err = dir_search(dir, str, len, loc);
if (err)
return err;
str += len;
*path = str;
Sfn* sfn = dir_ptr(dir);
dir_clust = sfn_cluster(sfn);
dir_enterable = (sfn->attr & FAT_ATTR_DIR) != 0;
}
}
//------------------------------------------------------------------------------
static int remove_entries(Dir* dir, Loc* loc)
{
// Save dir location (last entry to delete)
uint32_t sect = dir->sect;
uint16_t idx = dir->idx;
// Rewind dir to loc (first entry to delete)
dir->clust = sect_to_clust(dir->fat, loc->sect);
dir->sect = loc->sect;
dir->idx = loc->idx;
for (;;)
{
int err = update_buf(dir->fat, dir->sect);
if (err)
return err;
Sfn* sfn = dir_ptr(dir);
sfn->name[0] = SFN_FREE;
dir->fat->flags |= FAT_BUF_DIRTY;
if (dir->sect == sect && dir->idx == idx)
return FAT_ERR_NONE;
err = dir_next(dir);
if (err)
return err;
}
}
//------------------------------------------------------------------------------
static int dir_add(Dir* dir, const char* name, int len, uint8_t attr, uint32_t clust)
{
if (len <= 0 || len > 255)
return FAT_ERR_PARAM;
int err;
bool eod = false;
uint16_t idx = 0;
uint32_t sect = 0;
int lfns = (len + 12) / 13;
dir_enter(dir, dir->sclust);
// Try to find lfn_cnt + 1 consecutive free entries. Stretch cluster chain
// if necessary. Store location of first entry in the sequence.
for (int cnt = 0; cnt < lfns + 1;)
{
err = update_buf(dir->fat, dir->sect);
if (err)
return err;
Sfn* sfn = dir_ptr(dir);
if (eod || sfn_is_free(sfn))
{
if (cnt++ == 0)
{
sect = dir->sect;
idx = dir->idx;
}
}
else
cnt = 0;
if (sfn_is_last(sfn))
eod = true;
err = dir_next_stretch(dir);
if (err)
return err;
}
if (eod)
{
// We are currently at the entry after the SFN we will create.
// Since it hit EOD the entry is free. Create new EOD.
err = update_buf(dir->fat, dir->sect);
if (err)
return err;
Sfn* sfn = dir_ptr(dir);
sfn->name[0] = 0x00;
dir->fat->flags |= FAT_BUF_DIRTY;
}
// Rewind to the first free entry
dir->clust = sect_to_clust(dir->fat, sect);
dir->sect = sect;
dir->idx = idx;
uint8_t sfn_name[11];
put_sfn_name(sfn_name, name, len);
uint8_t crc = get_crc(sfn_name);
uint8_t mask = LFN_HEAD_MSK;
// Create LFN entries
for (int i = lfns; i > 0; i--, mask = 0)
{
err = update_buf(dir->fat, dir->sect);
if (err)
return err;
Lfn* lfn = dir_ptr(dir);
dir->fat->flags |= FAT_BUF_DIRTY;
int pos = 13 * (i - 1);
put_lfn_name_frag(lfn, name + pos, LIMIT(len - pos, 13));
lfn->attr = FAT_ATTR_LFN;
lfn->seq = mask | i;
lfn->crc = crc;
lfn->type = 0;
lfn->clust = 0;
err = dir_next(dir);
if (err)
return err;
}
uint16_t time, date;
encode_timestamp(&date, &time);
err = update_buf(dir->fat, dir->sect);
if (err)
return err;
Sfn* sfn = dir_ptr(dir);
dir->fat->flags |= FAT_BUF_DIRTY;
memcpy(sfn->name, sfn_name, sizeof(sfn->name));
sfn->clust_hi = clust >> 16;
sfn->clust_lo = clust & 0xffff;
sfn->attr = attr;
sfn->reserved = 0;
sfn->tenth = 0;
sfn->cre_time = time;
sfn->mod_time = time;
sfn->cre_date = date;
sfn->mod_date = date;
sfn->acc_date = date;
sfn->size = 0;
return FAT_ERR_NONE;
}
//------------------------------------------------------------------------------
static bool get_part_lba(uint8_t* buf, int partition, uint32_t* lba)
{
Mbr* mbr = (Mbr*)buf;
if (mbr->sig != 0xaa55)
return false;
if (mbr->part[partition].type != 0x0c) // Must be FAT32
return false;
*lba = mbr->part[partition].lba;
return true;
}
//------------------------------------------------------------------------------
static bool check_fat(uint8_t* buf)