-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfat32.c
1327 lines (1145 loc) · 41.7 KB
/
fat32.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/********************************************************************
* fat32.c
*
* FAT32 file system driver.
* Minimal implementation, FAT32 file and directory read and write.
* Long file names are supported, but *not* long directory names.
* The goal is functionality not performance performance implementing only
* the functionality needed for future projects.
*
* April 2024
*
*******************************************************************/
#include <string.h>
#include <stdio.h>
#include <assert.h>
#include "sd.h"
#include "fat32.h"
/* -----------------------------------------
Module definition
----------------------------------------- */
#define FAT32_SEC_SIZE 512 // Bytes
#define FAT32_MAX_SEC_PER_CLUS 16 // *** 1, 2, 4, 8, 16, 32, 64, 128
#define FAT32_CLUS_PER_SECTOR (FAT32_SEC_SIZE/sizeof(uint32_t))
#define FAT32_FAT_MASK 0x0fffffff
#define FAT32_FREE_CLUSTER 0x00000000
#define FAT32_FREE_RES1 0x00000001
#define FAT32_VALID_CLUST_LOW 0x00000002
#define FAT32_VALID_CLUST_HIGH 0x0fffffef
#define FAT32_FREE_RES2_LOW 0x0ffffff0
#define FAT32_FREE_RES2_HIGH 0x0ffffff6
#define FAT32_BAD_SEC_CLUSTER 0x0ffffff7
#define FAT32_END_OF_CHAIN 0x0ffffff8
#define FILE_ATTR_READ_ONLY 0b00000001
#define FILE_ATTR_HIDDEN 0b00000010
#define FILE_ATTR_SYSTEM 0b00000100
#define FILE_ATTR_VOL_LABEL 0b00001000
#define FILE_ATTR_DIRECTORY 0b00010000
#define FILE_ATTR_ARCHIVE 0b00100000
#define FILE_ATTR_LONG_NAME 0b00001111
#define FILE_LFN_END 0x40
#define MIN(a, b) (( a < b ) ? a : b)
/* -----------------------------------------
Module types
----------------------------------------- */
/* Partition table structure
*/
typedef struct
{
uint8_t status;
uint8_t first_head;
uint8_t first_sector;
uint8_t first_cylinder;
uint8_t type;
uint8_t last_head;
uint8_t last_sector;
uint8_t last_cylinder;
uint32_t first_lba;
uint32_t num_sectors;
} __attribute__ ((packed)) partition_t;
/* boot sector and BPB
* https://en.wikipedia.org/wiki/Design_of_the_FAT_file_system#BPB
*/
typedef struct
{
uint16_t bytes_per_sector; // DOS 2.0 BPB
uint8_t sectors_per_cluster;
uint16_t reserved_sectors;
uint8_t fat_count;
uint16_t root_directory_entries;
uint16_t total_sectors;
uint8_t media_descriptor;
uint16_t sectors_per_fat;
uint16_t sectors_per_track; // DOS 3.31 BPB
uint16_t heads;
uint32_t hidden_sectors;
uint32_t total_logical_sectors;
uint32_t logical_sectors_per_fat; // FAT32 extensions start here
uint16_t drive_desc;
uint16_t version;
uint32_t cluster_number_root_dir;
// ... there is more.
} __attribute__ ((packed)) bpb_t;
typedef struct
{
char short_dos_name[8];
char short_dos_ext[3];
uint8_t attribute;
uint8_t user_attribute;
uint8_t delete_attribute;
uint16_t create_time;
uint16_t create_date;
uint16_t last_access_date;
uint16_t fat32_high_cluster;
uint16_t last_mod_time;
uint16_t last_mod_date;
uint16_t fat32_low_cluster;
uint32_t file_size_bytes;
} __attribute__ ((packed)) dir_record_t;
/* -----------------------------------------
Module functions
----------------------------------------- */
static error_t fat32_read_sector(uint32_t lba, uint8_t *buffer, uint16_t buffer_len);
static error_t fat32_write_sector(uint32_t lba, uint8_t *buffer, uint16_t buffer_len);
static error_t fat32_get_next_cluster_num(uint32_t cluster_num, uint32_t *next_cluster_num);
static error_t fat32_get_new_cluster(uint32_t *new_cluster_num);
static error_t fat32_update_cluster_chain(uint32_t cluster_num, uint32_t new_cluster_num);
static error_t fat32_update_file_size(file_param_t *file, uint32_t file_size);
static uint32_t fat32_get_cluster_base_lba(uint32_t);
static int dir_get_sfn(dir_record_t *dir_record, char *name, uint16_t name_length);
static int dir_get_lfn(dir_record_t *dir_record, char *name, uint16_t name_length);
/* -----------------------------------------
Module globals
----------------------------------------- */
static int fat32_initialized = 0;
static uint8_t temp_sector_buffer[(2 * FAT32_SEC_SIZE)];
static uint8_t *low_sector_buffer;
static uint8_t *high_sector_buffer;
static uint32_t absolute_lba_cached = 0;
static struct fat_param_t
{
uint32_t first_lba;
uint32_t fat_begin_lba;
uint32_t cluster_begin_lba;
uint8_t sectors_per_cluster;
uint32_t sectors_per_fat;
uint32_t root_dir_first_cluster;
} fat32_parameters;
/* -------------------------------------------------------------
* fat32_init()
*
* Initialize FAT32 module
*
* Param: SD card initialize '=1' or skip '=0'
* Return: Driver error
*/
error_t fat32_init(void)
{
partition_t partitions[4];
bpb_t bpb;
error_t result;
if ((result = sd_init()) != NO_ERROR)
{
return result;
}
/* Initialize two buffer pointers
*/
low_sector_buffer = &temp_sector_buffer[0];
high_sector_buffer = &temp_sector_buffer[FAT32_SEC_SIZE];
/* Read data block
*/
if ( sd_read_block(0, low_sector_buffer, FAT32_SEC_SIZE) != NO_ERROR )
{
return FAT_READ_FAIL;
}
/* Analyze boot sector and partition table
*/
memcpy(partitions, &low_sector_buffer[446], sizeof(partitions));
if ( low_sector_buffer[510] != 0x55 || low_sector_buffer[511] != 0xaa )
{
return FAT_BAD_SECTOR_SIG;
}
/* Read file system
*/
if ( partitions[0].type != 0x0c )
{
return FAT_BAD_PARTITION_TYPE;
}
fat32_parameters.first_lba = partitions[0].first_lba;
if ( sd_read_block(fat32_parameters.first_lba, low_sector_buffer, FAT32_SEC_SIZE) != NO_ERROR )
{
return FAT_READ_FAIL;
}
/* Analyze BPB of first partition
*/
memcpy(&bpb, &low_sector_buffer[11], sizeof(bpb_t));
if ( low_sector_buffer[510] != 0x55 || low_sector_buffer[511] != 0xaa )
{
return FAT_BAD_SECTOR_SIG;
}
if ( bpb.sectors_per_cluster > FAT32_MAX_SEC_PER_CLUS )
{
return FAT_BAD_SECTOR_PER_CLUS;
}
/* Required FAT32 parsing parameters
*/
fat32_parameters.fat_begin_lba = fat32_parameters.first_lba + bpb.reserved_sectors;
fat32_parameters.cluster_begin_lba = fat32_parameters.fat_begin_lba + (bpb.fat_count * bpb.logical_sectors_per_fat);
fat32_parameters.sectors_per_cluster = bpb.sectors_per_cluster;
fat32_parameters.root_dir_first_cluster = bpb.cluster_number_root_dir;
fat32_parameters.sectors_per_fat = bpb.logical_sectors_per_fat;
fat32_initialized = 1;
return NO_ERROR;
}
/* -------------------------------------------------------------
* fat32_close()
*
* Close FAT32 module
*
* NOTE: All open file structures will be invalidated!
* Be sure to fat32_fclose() all of them before calling fat32_close()
*
* Param: None
* Return: None
*/
void fat32_close(void)
{
if ( !fat32_initialized )
return;
sd_close();
fat32_initialized = 0;
}
/* -------------------------------------------------------------
* fat32_is_initialized()
*
* Check if FAT32 (and SD, SPI) are ready for file IO
*
* Param: None
* Return: "1"=FAT32 initialized, "0"=not initialized
*/
int fat32_is_initialized(void)
{
return fat32_initialized;
}
/* -------------------------------------------------------------
* fat32_get_rootdir_cluster()
*
* Get the FAT32 cluster number of the root directory
*
* Param: None
* Return: FAT32 cluster number of the root directory, 0 if error
*/
uint32_t fat32_get_rootdir_cluster(void)
{
if ( !fat32_initialized )
return 0;
return fat32_parameters.root_dir_first_cluster;
}
/* -------------------------------------------------------------
* fat32_parse_dir()
*
* Parse directory listing from input cluster into directory list array
* supplied by the caller.
* If count of parsed items is less than 'dir_list_length', then the
* function reached the end of the listing.
* If 'start_cluster' is '-1' the function will continue to read entries following
* the one last read, unless no more entries exist.
*
* Param: Directory's start cluster number, pointer to directory list array, and its length
* Return: Count of parsed items or error code
*/
int fat32_parse_dir(uint32_t start_cluster, dir_entry_t *directory_list, uint16_t dir_list_length)
{
static uint32_t dir_cluster_num; // Cluster number
static uint32_t dir_base_cluster_lba; // LBA of first sector in the cluster
static int dir_sector_num; // Sector within cluster
static int dir_record_num; // Directory record within sector
static int done = 0;
int i; // Directory list index
int long_filename_flag;
dir_record_t *dir_record;
error_t result;
if ( !fat32_initialized )
return FAT_READ_FAIL;
/* Prevent iteration if the listing was completed before.
* Reset by invoking with a valid cluster number.
*/
if ( (start_cluster == -1) && done )
return 0;
/* Calculate LBA of the start of the cluster
* and so an initial read.
*/
if ( start_cluster != -1 )
{
done = 0;
dir_cluster_num = start_cluster;
dir_base_cluster_lba = fat32_get_cluster_base_lba(dir_cluster_num);
dir_sector_num = 0;
dir_record_num = 0;
result = fat32_read_sector(dir_base_cluster_lba, high_sector_buffer, FAT32_SEC_SIZE);
if ( result != NO_ERROR )
{
return result;
}
}
long_filename_flag = 0;
/* Read the cluster one sector at a time and parse
*/
i = 0;
while ( i < dir_list_length )
{
dir_record = (dir_record_t*) (high_sector_buffer + (dir_record_num * sizeof(dir_record_t)));
directory_list[i].sfn[0] = 0;
directory_list[i].lfn[0] = 0;
/* Done
*/
if ( dir_record->short_dos_name[0] == 0 )
{
done = 1;
break;
}
/* Flag long file name records
*/
if ( (dir_record->attribute & FILE_ATTR_LONG_NAME) == FILE_ATTR_LONG_NAME )
{
long_filename_flag = 1;
}
/* Process directory record while skipping volume labels and deleted files
*/
if ( dir_record->short_dos_name[0] != 0xe5 &&
dir_record->attribute != FILE_ATTR_VOL_LABEL &&
(dir_record->attribute & FILE_ATTR_LONG_NAME) != FILE_ATTR_LONG_NAME )
{
directory_list[i].is_directory = ((dir_record->attribute & FILE_ATTR_DIRECTORY) ? 1 : 0);
dir_get_sfn(dir_record, directory_list[i].sfn, FAT32_DOS_FILE_NAME);
if ( dir_record->short_dos_name[0] == '.' )
{
directory_list[i].lfn[0] = '.';
directory_list[i].lfn[1] = 0;
if ( dir_record->short_dos_name[1] == '.' )
{
directory_list[i].lfn[1] = '.';
directory_list[i].lfn[2] = 0;
}
}
if ( long_filename_flag )
{
dir_get_lfn(dir_record, directory_list[i].lfn, FAT32_LONG_FILE_NAME);
long_filename_flag = 0;
}
else
{
strncpy(directory_list[i].lfn, directory_list[i].sfn, FAT32_DOS_FILE_NAME);
}
directory_list[i].cluster_chain_head = ((uint32_t)dir_record->fat32_high_cluster << 16) + dir_record->fat32_low_cluster;
/* This adjustment is necessary because the first
* sub-directory level has a '..' file with a
* '0' cluster number.
*/
if ( directory_list[i].cluster_chain_head == 0 )
directory_list[i].cluster_chain_head = 2;
directory_list[i].file_size = dir_record->file_size_bytes;
/* Save references to the directory record location
* to allow access for modification.
*/
directory_list[i].dir_record_index = dir_record_num;
directory_list[i].dir_record_lba = dir_base_cluster_lba + dir_sector_num;
i++;
}
dir_record_num++;
/* If all records in current sector were parsed
* and there is still room left in the directory list then try next sector.
*/
if ( dir_record_num == (FAT32_SEC_SIZE / sizeof(dir_record_t)) )
{
dir_record_num = 0;
dir_sector_num++;
/* Reached last sector of the cluster then
* try next cluster until last cluster is reached.
*/
if ( dir_sector_num == fat32_parameters.sectors_per_cluster )
{
dir_sector_num = 0;
fat32_get_next_cluster_num(dir_cluster_num, &dir_cluster_num);
/* TODO: check and handle other possible values
*/
if ( dir_cluster_num >= FAT32_END_OF_CHAIN )
break;
dir_base_cluster_lba = fat32_get_cluster_base_lba(dir_cluster_num);
}
/* Read next sector of directory information.
* Use a rolling two-buffer schema to allow parsing
* entries that span sector boundary.
*/
memcpy(low_sector_buffer, high_sector_buffer, FAT32_SEC_SIZE);
result = fat32_read_sector((dir_base_cluster_lba + dir_sector_num), high_sector_buffer, FAT32_SEC_SIZE);
if ( result != NO_ERROR )
{
return result;
}
}
} /* while ( i < dir_list_length ) */
return i;
}
/* -------------------------------------------------------------
* fat32_fcreate()
*
* TODO: Stub for file or directory creation.
* Create a file or directory passed by name, in the parent
* directory pointed to by 'directory'.
*
* Param: File or directory name
* Return: Error code
*/
error_t fat32_fcreate(char *file_name, dir_entry_t *directory)
{
return FAT_FILE_NOT_FOUND;
}
/* -------------------------------------------------------------
* fat32_fdelete()
*
* TODO: Stub for file or directory deletion.
* Delete the file or directory pointed to by 'file_dir_info'.
* If the input parameter points to a directory, the directory must be empty.
*
* Param: Pointed to file or directory entry structure
* Return: Error code
*/
error_t fat32_fdelete(dir_entry_t *file_dir_info)
{
return FAT_FILE_NOT_FOUND;
}
/* -------------------------------------------------------------
* fat32_fopen()
*
* Open a file for reading. File to open is designated
* via a directory entry obtained by calling fat32_parse_dir()
* and not its name/location.
* When a file is open it can be read from by fat32_fread(),
* or written to by fat32_fwrite(), starting at the file position pointer.
* The file position pointer can be changed with fat32_fseek().
*
* Example: call fat32_parse_dir(), find the
* file you want to access, and pass that entry to fat32_fopen().
*
* Param: Pointed to file's directory entry structure, pointer to empty file structure.
* Return: Error code
*/
error_t fat32_fopen(dir_entry_t *directory_entry, file_param_t *file_parameters)
{
if ( directory_entry->is_directory || file_parameters->file_is_open )
return FAT_FILE_OPEN_ERR;
file_parameters->file_is_open = 1;
file_parameters->file_start_cluster = directory_entry->cluster_chain_head;
file_parameters->file_size = directory_entry->file_size;
file_parameters->current_position = 0;
file_parameters->current_cluster = file_parameters->file_start_cluster;
file_parameters->is_end_of_chain = 0; // Can't know at this point, need to use seek.
file_parameters->current_base_lba = fat32_get_cluster_base_lba(file_parameters->file_start_cluster);;
file_parameters->current_lba_index = 0;
file_parameters->current_byte_index = 0;
file_parameters->eof_flag = 0;
file_parameters->sector_cached = 0;
file_parameters->dir_record_index = directory_entry->dir_record_index;
file_parameters->dir_record_lba = directory_entry->dir_record_lba;
if ( file_parameters->file_size == 0 )
{
file_parameters->eof_flag = 1;
}
else
{
file_parameters->eof_flag = 0;
}
return NO_ERROR;
}
/* -------------------------------------------------------------
* fat32_fclose()
*
* Close a file by resetting its parameter structure.
*
* Param: Pointer to an open file structure.
* Return: None
*/
void fat32_fclose(file_param_t *file_parameters)
{
file_parameters->file_is_open = 0;
file_parameters->file_start_cluster = 0;
file_parameters->file_size = 0;
file_parameters->current_position = 0;
file_parameters->current_cluster = 0;
file_parameters->is_end_of_chain = 0;
file_parameters->current_base_lba = 0;
file_parameters->current_lba_index = 0;
file_parameters->current_byte_index = 0;
file_parameters->eof_flag = 0;
file_parameters->sector_cached = 0;
file_parameters->dir_record_index = 0;
file_parameters->dir_record_lba = 0;
}
/* -------------------------------------------------------------
* fat32_fseek()
*
* Set file read position for the next read command.
* The function reads the sector containing the next byte to read.
*
* Param: Pointer to an open file structure, 0-based index file byte position.
* Return: Error code
*/
error_t fat32_fseek(file_param_t *file_parameters, uint32_t byte_position)
{
int i;
int cluster_index;
uint32_t current_cluster_num;
if ( !file_parameters->file_is_open )
return FAT_FILE_NOT_OPEN;
/* Handle possible out of range condition if
* seeking past end-of-file.
*/
if ( byte_position > file_parameters->file_size )
{
return FAT_FILE_SEEK_RANGE;
}
/* Traves cluster linked list in FAT to find the cluster and the of the
* as LBA's index within that cluster of the requested 'byte_position'.
*/
cluster_index = byte_position / (fat32_parameters.sectors_per_cluster * FAT32_SEC_SIZE);
current_cluster_num = file_parameters->file_start_cluster;
file_parameters->current_cluster = current_cluster_num;
file_parameters->is_end_of_chain = 0;
for ( i = 0; i < cluster_index; i++ )
{
fat32_get_next_cluster_num(current_cluster_num, ¤t_cluster_num);
/* We hit end-of-chain so we stop here.
*/
if ( current_cluster_num >= FAT32_END_OF_CHAIN )
{
file_parameters->is_end_of_chain = 1;
break;
}
/* Keep walking cluster number chain.
*/
else if ( current_cluster_num >= FAT32_VALID_CLUST_LOW &&
current_cluster_num <= FAT32_VALID_CLUST_HIGH )
{
file_parameters->current_cluster = current_cluster_num;
continue;
}
/* Found something other than a valid cluster number or an end-of-chain.
*/
else
{
return FAT_FILE_SEEK_ERR;
}
}
if ( file_parameters->is_end_of_chain )
{
file_parameters->current_base_lba = 0;
file_parameters->current_lba_index = 0;
}
else
{
file_parameters->current_base_lba = fat32_get_cluster_base_lba(current_cluster_num);
file_parameters->current_lba_index = (byte_position / FAT32_SEC_SIZE) % fat32_parameters.sectors_per_cluster;
}
file_parameters->current_byte_index = byte_position % FAT32_SEC_SIZE;
file_parameters->sector_cached = 0;
file_parameters->current_position = byte_position;
if ( byte_position == file_parameters->file_size )
{
file_parameters->eof_flag = 1;
}
else
{
file_parameters->eof_flag = 0;
}
return NO_ERROR;
}
/* -------------------------------------------------------------
* fat32_fread()
*
* Read file data from current position towards end-of-file.
* Read stops at end-of-file or when buffer is full.
* If buffer is full, then another read will continue from
* the byte after the last position that was read.
* A fat32_fopen(), and an optional fat32_fseek(), must be called before fat32_fread().
*
* Param: Pointer to an open file structure, buffer for file data, and the buffer length
* Return: Byte count read, 0=no more data (reached EOF), or error code
*/
int fat32_fread(file_param_t *file_parameters, uint8_t *buffer, uint16_t buffer_length)
{
uint16_t i, j;
uint32_t k, r;
uint16_t byte_count;
uint32_t file_position; // Byte position in file
uint32_t file_cluster; // Cluster number
uint32_t base_lba; // Cluster base LBA
uint8_t lba_index; // LBA index within cluster
uint16_t byte_offset; // Byte index within current sector
error_t result;
if ( !file_parameters->file_is_open )
return FAT_FILE_NOT_OPEN;
if ( file_parameters->eof_flag )
return FAT_EOF;
file_position = file_parameters->current_position;
file_cluster = file_parameters->current_cluster;
base_lba = file_parameters->current_base_lba;
lba_index = file_parameters->current_lba_index;
byte_offset = file_parameters->current_byte_index;
/* This will address a case where multiple files are open
* and are accessed alternatly. It will detect the case and force
* sector cache refresh if the new request assumes a sector that is
* not already cached.
*/
if ( absolute_lba_cached != (base_lba + lba_index) )
{
file_parameters->sector_cached = 0;
}
/* An initial read to cache the first sector of the read sequence.
* A call to fat32_fopen() or fat32_fseek() invalidates the 'cached' flag
* so this initial read is needed.
*/
if ( !file_parameters->sector_cached )
{
result = fat32_read_sector((base_lba + lba_index), high_sector_buffer, FAT32_SEC_SIZE);
if ( result != NO_ERROR )
{
return result;
}
absolute_lba_cached = base_lba + lba_index;
file_parameters->sector_cached = 1;
}
/* Read sectors and move data into the client read buffer until the buffer
* if full or we reached end of file. Update file position for next call.
*
*/
byte_count = 0;
while ( byte_count < buffer_length )
{
/* Transfer data to client buffer
*/
i = buffer_length - byte_count; // Byte space available in client buffer
j = FAT32_SEC_SIZE - byte_offset; // Bytes left to read in cached buffer
k = file_parameters->file_size - file_position; // Bytes left to read in file
r = MIN(i, j);
r = MIN(r, k);
memcpy(&buffer[byte_count], &high_sector_buffer[byte_offset], r);
byte_offset += r;
byte_count += r;
file_position += r;
/* Read completed conditions
*/
if ( file_position == file_parameters->file_size )
{
file_parameters->eof_flag = 1;
break;
}
/* Prepare for getting the next sector if we have more bytes to read
* and we finished reading the current sector.
*/
/* End of sector
*/
if ( byte_offset == FAT32_SEC_SIZE )
{
byte_offset = 0;
lba_index++;
/* End of cluster
*/
if ( lba_index == fat32_parameters.sectors_per_cluster )
{
lba_index = 0;
file_parameters->is_end_of_chain = 0;
fat32_get_next_cluster_num(file_cluster, &file_cluster);
/* Just a guard, should not happen.
*/
/* TODO: check and handle other possible values
*/
if ( file_cluster >= FAT32_END_OF_CHAIN )
{
file_parameters->is_end_of_chain = 1;
file_parameters->eof_flag = 1;
break;
}
base_lba = fat32_get_cluster_base_lba(file_cluster);
}
}
/* Read next sector into cache
*/
result = fat32_read_sector((base_lba + lba_index), high_sector_buffer, FAT32_SEC_SIZE);
if ( result != NO_ERROR )
{
return result;
}
absolute_lba_cached = base_lba + lba_index;
file_parameters->sector_cached = 1;
}
/* Update file descriptor structure
*/
file_parameters->current_position = file_position;
file_parameters->current_cluster = file_cluster;
file_parameters->current_base_lba = base_lba;
file_parameters->current_lba_index = lba_index;
file_parameters->current_byte_index = byte_offset;
return byte_count;
}
/* -------------------------------------------------------------
* fat32_fwrite()
*
* Write data to an open file starting at current position towards end-of-file.
* Write stops if FAT is full or all bytes in buffer have been written.
* Function will allocate clusters as required and update FAT.
* A fat32_fcreate() [not implemented] or fat32_fopen(), and an optional fat32_fseek(), must be called before fat32_fwrite().
*
* Note: Internal write errors abort write operation without attempting cleanup.
*
* Param: Pointer to an open file structure, buffer with data to write, and the buffer length
* Return: Byte count written, 0=no more data, or error code
*/
int fat32_fwrite(file_param_t *file_parameters, uint8_t *buffer, uint16_t buffer_length)
{
uint16_t i, j, r;
uint16_t byte_count;
uint32_t file_position; // Byte position in file
uint32_t file_cluster; // Cluster number
uint32_t temp_file_cluster; // Cluster number
uint32_t base_lba; // Cluster base LBA
uint32_t new_file_size;
uint8_t lba_index; // LBA index within cluster
uint16_t byte_offset; // Byte index within current sector
error_t result;
if ( !file_parameters->file_is_open )
return FAT_FILE_NOT_OPEN;
/* File with zero-bytes size requires special handling
* and currently not supported.
*/
if ( file_parameters->file_size == 0 )
{
return FAT_WRITE_FAIL;
}
file_position = file_parameters->current_position;
new_file_size = file_parameters->file_size;
file_cluster = file_parameters->current_cluster;
base_lba = file_parameters->current_base_lba;
lba_index = file_parameters->current_lba_index;
byte_offset = file_parameters->current_byte_index;
/* Allocate a new cluster is the 'currnet_cluster is the last one in the chain.
* Leave 'is_end_of_chain' true becasue the new cluster is still the last one in the chain.
*/
if ( file_parameters->is_end_of_chain )
{
temp_file_cluster = file_cluster;
result = fat32_get_new_cluster(&file_cluster);
if ( result != NO_ERROR )
{
return result;
}
result = fat32_update_cluster_chain(temp_file_cluster, file_cluster);
if ( result != NO_ERROR )
{
return result;
}
base_lba = fat32_get_cluster_base_lba(file_cluster);
}
/* This will address a case where multiple files are open
* and are accessed alternatly. It will detect the case and force
* sector cache refresh if the new request assumes a sector that is
* not already cached.
*/
if ( absolute_lba_cached != (base_lba + lba_index) )
{
file_parameters->sector_cached = 0;
}
/* An initial read to cache the first sector of the write sequence.
* A call to fat32_fopen() or fat32_fseek() invalidates the 'cached' flag
* so this initial read is needed.
*/
if ( !file_parameters->sector_cached )
{
result = fat32_read_sector((base_lba + lba_index), high_sector_buffer, FAT32_SEC_SIZE);
if ( result != NO_ERROR )
{
return result;
}
absolute_lba_cached = base_lba + lba_index;
file_parameters->sector_cached = 1;
}
/* A read-modify-write sequecence of sectors with data from client buffer until the buffer
* if empty or we reached end of file. Update file position for next call.
*
*/
byte_count = 0;
while ( byte_count < buffer_length )
{
/* Transfer data to client buffer, persist to media
* and adjust indexes.
*/
i = buffer_length - byte_count; // Byte remaining in client buffer
j = FAT32_SEC_SIZE - byte_offset; // Bytes to end of cached buffer
r = MIN(i, j);
memcpy(&high_sector_buffer[byte_offset], &buffer[byte_count], r);
result = fat32_write_sector((base_lba + lba_index), high_sector_buffer, FAT32_SEC_SIZE);
if ( result != NO_ERROR )
{
return result;
}
byte_offset += r;
byte_count += r;
file_position += r;
if ( file_position > new_file_size )
new_file_size = file_position;
/* Write completed conditions
*/
if ( byte_count == buffer_length )
{
break;
}
/* Prepare for getting the next sector or allocating a cluster(s)
* if we have more bytes to write.
*/
/* End of sector
*/
if ( byte_offset == FAT32_SEC_SIZE )
{
byte_offset = 0;
lba_index++;
/* End of cluster then get the next in the chain or
* allocate a new cluster.
*/
if ( lba_index == fat32_parameters.sectors_per_cluster )
{
lba_index = 0;
temp_file_cluster = file_cluster;
file_parameters->is_end_of_chain = 0;
result = fat32_get_next_cluster_num(file_cluster, &file_cluster);
if ( result != NO_ERROR )
{
return result;
}
if ( file_cluster >= FAT32_END_OF_CHAIN )
{
file_parameters->is_end_of_chain = 1;
result = fat32_get_new_cluster(&file_cluster);
if ( result != NO_ERROR )
{
return result;
}
result = fat32_update_cluster_chain(temp_file_cluster, file_cluster);
if ( result != NO_ERROR )
{
return result;
}
}
base_lba = fat32_get_cluster_base_lba(file_cluster);
}
}
/* Read next sector into cache
*/
result = fat32_read_sector((base_lba + lba_index), high_sector_buffer, FAT32_SEC_SIZE);
if ( result != NO_ERROR )
{
return result;
}
absolute_lba_cached = base_lba + lba_index;
file_parameters->sector_cached = 1;
}
/* Update directory with file size
*/
if ( new_file_size > file_parameters->file_size )
{
result = fat32_update_file_size(file_parameters, new_file_size);
if ( result != NO_ERROR )
{
return result;
}
}
/* Update file descriptor structure
*/
file_parameters->file_size = new_file_size;
file_parameters->current_position = file_position;
file_parameters->current_cluster = file_cluster;
file_parameters->current_base_lba = base_lba;
file_parameters->current_lba_index = lba_index;
file_parameters->current_byte_index = byte_offset;
if ( file_position == file_parameters->file_size )
{
file_parameters->eof_flag = 1;
}
else
{
file_parameters->eof_flag = 0;
}
return byte_count;