Select one of the symbols to view example projects that use it.
 
Outline
...
...
...
...
#define FX_SOURCE_CODE
#include "fx_api.h"
#include "fx_system.h"
#include "fx_directory.h"
#include "fx_media.h"
#include "fx_utility.h"
#include "fx_directory_exFAT.h"
#define FX_MAX_DIRECTORY_NESTING
CURRENT_DIRECTORY_ENTRY_STRUCT
...
...
_fx_media_check(FX_MEDIA *, UCHAR *, ULONG, ULONG, ULONG *)
Files
filex
common
drivers
inc
src
ports
threadx
levelx
SourceVuSTM32 Libraries and Samplesfilexcommon/src/fx_media_check.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
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
/**************************************************************************/ /* */ /* Copyright (c) Microsoft Corporation. All rights reserved. */ /* */ /* This software is licensed under the Microsoft Software License */ /* Terms for Microsoft Azure RTOS. Full text of the license can be */ /* found in the LICENSE file at https://aka.ms/AzureRTOS_EULA */ /* and in the root directory of this software. */ /* */... /**************************************************************************/ ... /**************************************************************************/ /**************************************************************************/ /** */ /** FileX Component */ /** */ /** Media */ /** */... /**************************************************************************/ /**************************************************************************/ #define FX_SOURCE_CODE /* Include necessary system files. */ #include "fx_api.h" #include "fx_system.h" #include "fx_directory.h" #include "fx_media.h" #include "fx_utility.h" 5 includes#ifdef FX_ENABLE_EXFAT #include "fx_directory_exFAT.h" #endif /* FX_ENABLE_EXFAT */ /* Define parameters for FileX media check utility. */ #ifndef FX_MAX_DIRECTORY_NESTING #define FX_MAX_DIRECTORY_NESTING 20 #endif /* Define data structures local to the FileX media check utility. */ typedef struct CURRENT_DIRECTORY_ENTRY_STRUCT { ULONG current_directory_entry; ULONG current_total_entries; ULONG current_start_cluster; ...} CURRENT_DIRECTORY_ENTRY; ... /**************************************************************************/ /* */ /* FUNCTION RELEASE */ /* */ /* _fx_media_check PORTABLE C */ /* 6.1 */ /* AUTHOR */ /* */ /* William E. Lamie, Microsoft Corporation */ /* */ /* DESCRIPTION */ /* */ /* This function checks the specified media for basic structural */ /* errors, including cross linking, invalid FAT chains, and lost */ /* clusters. The function also provides the capability to correct */ /* errors. */ /* */ /* The algorithm is to follow all sub-directories immediately and */ /* check their contents. The maximum depth of sub-directories is */ /* determined by the constant FX_MAX_DIRECTORY_NESTING. */ /* By default, this is set at 20. Basically, the FAT chain of every */ /* file and sub-directory is traversed. If valid, clusters are marked */ /* in a logical FAT bit map to signal they are already in-use. The */ /* algorithm should detect any broken or cross linked FAT condition. */ /* */ /* As for memory usage, the scratch memory supplied to the media check */ /* function is used to hold several directory entries, a data */ /* structure to "stack" the current directory entry position before */ /* diving into the sub-directory, and finally the logical FAT bit map. */ /* The basic data structures take from 512-1024 bytes and the logical */ /* FAT bit map requires as many bits as there are clusters in your */ /* device. For example, a device with 8000 clusters would require */ /* 1000 bytes to represent. */ /* */ /* On the performance end of things, the traversal is reasonably fast */ /* and is basically linear with the number of files and their sizes. */ /* The lost cluster checking at the end of media check is a bit more */ /* performance comprehensive. It basically checks that each unused */ /* cluster is actually marked as free. You might decide to bypass this */ /* step if no other errors are found... or there might be ways to */ /* optimize the search by reading whole FAT sectors. You probably just */ /* need to see how it behaves in your system. */ /* */ /* INPUT */ /* */ /* media_ptr Pointer to a previously */ /* opened media */ /* scratch_memory_ptr Pointer to memory area for */ /* media check to use (as */ /* mentioned above) */ /* scratch_memory_size Size of the scratch memory */ /* error_correction_option Specifies which - if any - */ /* errors are corrected by */ /* the media check function. */ /* Setting the following bit */ /* causes that error to be */ /* corrected: */ /* */ /* 0x01 -> Fix FAT Chain Errors*/ /* 0x02 -> Fix Directory Entry */ /* Errors */ /* 0x04 -> Fix Lost Clusters */ /* */ /* errors_detected Specifies the destination */ /* ULONG to place the error */ /* report from media check. */ /* This has a similar bit map */ /* as before: */ /* */ /* 0x01 -> FAT Chain Error(s) */ /* 0x02 -> Directory Entry */ /* Error(s) */ /* 0x04 -> Lost Cluster(s) */ /* */ /* OUTPUT */ /* */ /* FX_SUCCESS Media check performed its */ /* operation successfully. */ /* This does not mean that */ /* there were no errors. The */ /* errors_detected variable */ /* needs to be examined. */ /* FX_MEDIA_NOT_OPEN The media was not open. */ /* FX_NOT_ENOUGH_MEMORY The scratch memory was not */ /* large enough or the nesting */ /* depth was greater than the */ /* maximum specified. */ /* FX_IO_ERROR I/O Error reading/writing to */ /* the media. */ /* FX_ERROR_NOT_FIXED Fundamental problem with */ /* media that couldn't be fixed*/ /* */ /* CALLS */ /* */ /* _fx_media_cache_invalidate Invalidate the cache */ /* _fx_media_check_FAT_chain_check Walk the supplied FAT chain */ /* _fx_media_check_lost_cluster_check Check for lost clusters */ /* _fx_directory_entry_read Directory entry read */ /* _fx_directory_entry_write Directory entry write */ /* _fx_media_flush Flush changes to the media */ /* _fx_utility_FAT_entry_write Write value to FAT entry */ /* */ /* CALLED BY */ /* */ /* Application Code */ /* */ /* RELEASE HISTORY */ /* */ /* DATE NAME DESCRIPTION */ /* */ /* 05-19-2020 William E. Lamie Initial Version 6.0 */ /* 09-30-2020 William E. Lamie Modified comment(s), */ /* resulting in version 6.1 */ /* */... /**************************************************************************/ UINT _fx_media_check(FX_MEDIA *media_ptr, UCHAR *scratch_memory_ptr, ULONG scratch_memory_size, ULONG error_correction_option, ULONG *errors_detected) { CURRENT_DIRECTORY_ENTRY *current_directory; ULONG total_entries, last_cluster, valid_clusters; ULONG bytes_per_cluster, i, current_errors; UINT status, long_name_size; UINT current_directory_index; UCHAR *logical_fat, *working_ptr; ALIGN_TYPE address_mask; FX_DIR_ENTRY *temp_dir_ptr, *source_dir_ptr, *dir_entry_ptr; #ifdef TX_ENABLE_EVENT_TRACE TX_TRACE_BUFFER_ENTRY *trace_event; ULONG trace_timestamp;/* ... */ #endif #ifdef FX_ENABLE_EXFAT current_errors = 0; #endif /* Check the media to make sure it is open. */ if (media_ptr -> fx_media_id != FX_MEDIA_ID) { /* Return the media not opened error. */ return(FX_MEDIA_NOT_OPEN); }if (media_ptr -> fx_media_id != FX_MEDIA_ID) { ... } /* If trace is enabled, insert this event into the trace buffer. */ FX_TRACE_IN_LINE_INSERT(FX_TRACE_MEDIA_CHECK, media_ptr, scratch_memory_ptr, scratch_memory_size, 0, FX_TRACE_MEDIA_EVENTS, &trace_event, &trace_timestamp) /* Protect against other threads accessing the media. */ FX_PROTECT /* Determine if there are any opened files. */ if (media_ptr -> fx_media_opened_file_count) { /* There are opened files... this is an error! */ /* Release protection. */ FX_UNPROTECT /* Return an error. */ return(FX_ACCESS_ERROR); }if (media_ptr -> fx_media_opened_file_count) { ... } /* Invalidate the cache. */ _fx_media_cache_invalidate(media_ptr); /* Initialize the reported error flag. */ *errors_detected = 0; /* Calculate the long name size, rounded up to something that is evenly divisible by 4. */ long_name_size = (((FX_MAX_LONG_NAME_LEN + 3) >> 2) << 2); /* Calculate the number of bytes per cluster. */ bytes_per_cluster = media_ptr -> fx_media_sectors_per_cluster * media_ptr -> fx_media_bytes_per_sector; /* Setup address mask. */ address_mask = sizeof(ULONG) - 1; address_mask = ~address_mask; /* Setup working pointer. */ working_ptr = scratch_memory_ptr + (sizeof(ULONG) - 1); working_ptr = (UCHAR *)(((ALIGN_TYPE)working_ptr) & address_mask); /* Memory is set aside for two FX_DIR_ENTRY structures */ dir_entry_ptr = (FX_DIR_ENTRY *)working_ptr; /* Adjust the scratch memory pointer forward. */ working_ptr = &working_ptr[sizeof(FX_DIR_ENTRY)]; /* Setup the name buffer for the first directory entry. */ dir_entry_ptr -> fx_dir_entry_name = (CHAR *)working_ptr; /* Adjust the scratch memory pointer forward. */ working_ptr = working_ptr + long_name_size + (sizeof(ULONG) - 1); working_ptr = (UCHAR *)(((ALIGN_TYPE)working_ptr) & address_mask); /* Setup the source directory entry. */ source_dir_ptr = (FX_DIR_ENTRY *)working_ptr; /* Adjust the scratch memory pointer forward. */ working_ptr = &working_ptr[sizeof(FX_DIR_ENTRY)]; /* Setup the name buffer for the source directory entry. */ source_dir_ptr -> fx_dir_entry_name = (CHAR *)working_ptr; /* Adjust the scratch memory pointer forward. */ working_ptr = working_ptr + long_name_size + (sizeof(ULONG) - 1); working_ptr = (UCHAR *)(((ALIGN_TYPE)working_ptr) & address_mask); /* Setup the current directory stack memory. */ current_directory = (CURRENT_DIRECTORY_ENTRY *)working_ptr; /* Allocate space for the size of the directory entry stack. This basically defines the maximum level of sub-directories supported. *//* ... */ working_ptr = &working_ptr[(FX_MAX_DIRECTORY_NESTING * sizeof(CURRENT_DIRECTORY_ENTRY))]; /* Setup the initial current directory entry. */ current_directory_index = 0; /* Adjust the size to account for the header information. */ if (scratch_memory_size < (ULONG)((working_ptr - scratch_memory_ptr))) { /* Release media protection. */ FX_UNPROTECT /* Return the not enough memory error. */ return(FX_NOT_ENOUGH_MEMORY); }if (scratch_memory_size < (ULONG)((working_ptr - scratch_memory_ptr))) { ... } /* Adjust the scratch memory size. */ scratch_memory_size = scratch_memory_size - (ULONG)(working_ptr - scratch_memory_ptr); /* Memory is set aside for logical FAT - one bit per cluster */ logical_fat = (UCHAR *)working_ptr; /* Determine if there is enough memory. */ if (scratch_memory_size < ((media_ptr -> fx_media_total_clusters >> 3) + 1)) { /* Release media protection. */ FX_UNPROTECT /* Return the not enough memory error. */ return(FX_NOT_ENOUGH_MEMORY); }if (scratch_memory_size < ((media_ptr -> fx_media_total_clusters >> 3) + 1)) { ... } /* Initialize the logical FAT table. */ for (i = 0; i < ((media_ptr -> fx_media_total_clusters >> 3) + 1); i++) { /* Clear the logical FAT entry, which actually represents eight clusters. */ logical_fat[i] = 0; }for (i = 0; i < ((media_ptr -> fx_media_total_clusters >> 3) + 1); i++) { ... } #ifdef FX_ENABLE_EXFAT /* Mark the clusters occupied by Allocation Bitmap table, Up-cast table and the first cluster of root directory. */ if (media_ptr -> fx_media_FAT_type == FX_exFAT) { ULONG offset = 0; for (i = ((media_ptr -> fx_media_root_cluster_32 - FX_FAT_ENTRY_START) >> 3); i; i--) { logical_fat[offset++] = 0xff; }for (i = ((media_ptr -> fx_media_root_cluster_32 - FX_FAT_ENTRY_START) >> 3); i; i--) { ... } for (i = ((media_ptr -> fx_media_root_cluster_32 - FX_FAT_ENTRY_START) & 7); i; i--) { logical_fat[offset] = (UCHAR)((logical_fat[offset] << 1) | 0x1); }for (i = ((media_ptr -> fx_media_root_cluster_32 - FX_FAT_ENTRY_START) & 7); i; i--) { ... } }if (media_ptr -> fx_media_FAT_type == FX_exFAT) { ... } /* ... */#endif /* FX_ENABLE_EXFAT */ #ifdef FX_ENABLE_FAULT_TOLERANT if (media_ptr -> fx_media_fault_tolerant_enabled) { ULONG cluster, cluster_number; /* Mark the cluster used by fault tolerant as valid. */ for (cluster = media_ptr -> fx_media_fault_tolerant_start_cluster; cluster < media_ptr -> fx_media_fault_tolerant_start_cluster + media_ptr -> fx_media_fault_tolerant_clusters; cluster++) { cluster_number = cluster; #ifdef FX_ENABLE_EXFAT /* For the index of the first cluster in exFAT is 2, adjust the number of clusters to fit Allocation Bitmap Table. */ /* We will compare logical_fat with Aollcation Bitmap table later to find out lost clusters. */ if (media_ptr -> fx_media_FAT_type == FX_exFAT) { cluster_number = cluster - FX_FAT_ENTRY_START; }if (media_ptr -> fx_media_FAT_type == FX_exFAT) { ... } /* ... */#endif /* FX_ENABLE_EXFAT */ logical_fat[cluster_number >> 3] = (UCHAR)(logical_fat[cluster_number >> 3] | (1 << (cluster_number & 7))); }for (cluster = media_ptr -> fx_media_fault_tolerant_start_cluster; cluster < media_ptr -> fx_media_fault_tolerant_start_cluster + media_ptr -> fx_media_fault_tolerant_clusters; cluster++) { ... } }if (media_ptr -> fx_media_fault_tolerant_enabled) { ... } /* ... */#endif /* FX_ENABLE_FAULT_TOLERANT */ /* If FAT32 is present, determine if the root directory is coherent. */ #ifdef FX_ENABLE_EXFAT if ((media_ptr -> fx_media_FAT_type == FX_FAT32) || (media_ptr -> fx_media_FAT_type == FX_exFAT))/* ... */ #else if (media_ptr -> fx_media_32_bit_FAT) #endif /* FX_ENABLE_EXFAT */ { /* A 32-bit FAT is present. We need to walk the clusters of the root directory to determine if it is intact. *//* ... */ current_errors = _fx_media_check_FAT_chain_check(media_ptr, media_ptr -> fx_media_root_cluster_32, &last_cluster, &valid_clusters, logical_fat); /* Make them part of the errors reported to the caller. */ *errors_detected = *errors_detected | current_errors; /* Update the trace event with the errors detected. */ FX_TRACE_EVENT_UPDATE(trace_event, trace_timestamp, FX_TRACE_MEDIA_CHECK, 0, 0, 0, *errors_detected) /* Determine if the I/O error is set. */ if (current_errors & FX_IO_ERROR) { /* Release media protection. */ FX_UNPROTECT /* File I/O Error. */ return(FX_IO_ERROR); }if (current_errors & FX_IO_ERROR) { ... } /* Check the status. */ if (*errors_detected) { /* Determine if we can fix the FAT32 root directory error. */ if ((valid_clusters) && (error_correction_option & FX_FAT_CHAIN_ERROR)) { /* Make the chain end at the last cluster. */ status = _fx_utility_FAT_entry_write(media_ptr, last_cluster, media_ptr -> fx_media_fat_last); /* Determine if the write was successful. */ if (status) { /* Release media protection. */ FX_UNPROTECT /* Return the error code. */ return(status); }if (status) { ... } /* Adjust the total entries in the root directory. */ media_ptr -> fx_media_root_directory_entries = (valid_clusters * bytes_per_cluster) / FX_DIR_ENTRY_SIZE; }if ((valid_clusters) && (error_correction_option & FX_FAT_CHAIN_ERROR)) { ... } else { /* Release media protection. */ FX_UNPROTECT /* Return an error. */ return(FX_ERROR_NOT_FIXED); }else { ... } }if (*errors_detected) { ... } ...} /* Pickup total entries in the root directory. */ total_entries = media_ptr -> fx_media_root_directory_entries; /* Set temp directory pointer to NULL. */ temp_dir_ptr = FX_NULL; /* Put the root directory information in the entry stack */ current_directory[current_directory_index].current_total_entries = total_entries; current_directory[current_directory_index].current_start_cluster = media_ptr -> fx_media_fat_last; current_directory[current_directory_index].current_directory_entry = 0; /* Now we shall do the checking in depth first manner. */ do { /* Pickup the directory index. */ i = current_directory[current_directory_index].current_directory_entry; /* Loop to process remaining directory entries. */ while (i < current_directory[current_directory_index].current_total_entries) { /* Read a directory entry. */ #ifdef FX_ENABLE_EXFAT /* Hash value of the file name is not cared. */ status = _fx_directory_entry_read_ex(media_ptr, temp_dir_ptr, &i, dir_entry_ptr, 0);/* ... */ #else status = _fx_directory_entry_read(media_ptr, temp_dir_ptr, &i, dir_entry_ptr); #endif /* FX_ENABLE_EXFAT */ /* Determine if the read was successful. */ if (status) { /* Release media protection. */ FX_UNPROTECT /* Return the error code. */ return(status); }if (status) { ... } /* Check for the last entry. */ #ifdef FX_ENABLE_EXFAT if (dir_entry_ptr -> fx_dir_entry_type == FX_EXFAT_DIR_ENTRY_TYPE_END_MARKER) #else if (dir_entry_ptr -> fx_dir_entry_name[0] == (CHAR)FX_DIR_ENTRY_DONE) #endif /* FX_ENABLE_EXFAT */ { /* Last entry in this directory - no need to check further. */ break; ...} /* Is the entry free? */ #ifdef FX_ENABLE_EXFAT if (dir_entry_ptr -> fx_dir_entry_type != FX_EXFAT_DIR_ENTRY_TYPE_FILE_DIRECTORY) #else if ((dir_entry_ptr -> fx_dir_entry_name[0] == (CHAR)FX_DIR_ENTRY_FREE) && (dir_entry_ptr -> fx_dir_entry_short_name[0] == 0)) #endif /* FX_ENABLE_EXFAT */ { /* A deleted entry */ i++; continue; ...} #ifdef FX_ENABLE_EXFAT /* Determine whether FAT chain is used. */ if (dir_entry_ptr -> fx_dir_entry_dont_use_fat & 1) { ULONG64 remaining_size = dir_entry_ptr -> fx_dir_entry_file_size; ULONG cluster = dir_entry_ptr -> fx_dir_entry_cluster, cluster_number; valid_clusters = 0; while (remaining_size) { if (remaining_size >= bytes_per_cluster) { remaining_size -= bytes_per_cluster; }if (remaining_size >= bytes_per_cluster) { ... } else { remaining_size = 0; last_cluster = cluster; }else { ... } cluster_number = cluster - FX_FAT_ENTRY_START; /* Is the current cluster already marked? */ if ((logical_fat[cluster_number / 8] >> (cluster_number % 8)) & 0x01) { current_errors = FX_FILE_SIZE_ERROR; last_cluster = dir_entry_ptr -> fx_dir_entry_cluster + valid_clusters; break; }if ((logical_fat[cluster_number / 8] >> (cluster_number % 8)) & 0x01) { ... } /* Mark the current cluster. */ logical_fat[cluster_number >> 3] = (UCHAR)(logical_fat[cluster_number >> 3] | (1 << (cluster_number & 7))); valid_clusters++; cluster++; }while (remaining_size) { ... } }if (dir_entry_ptr -> fx_dir_entry_dont_use_fat & 1) { ... } else { #endif /* FX_ENABLE_EXFAT */ /* Look for any cross links or errors in the FAT chain of current directory entry. */ current_errors = _fx_media_check_FAT_chain_check(media_ptr, dir_entry_ptr -> fx_dir_entry_cluster, &last_cluster, &valid_clusters, logical_fat); #ifdef FX_ENABLE_EXFAT }else { ... } #endif /* FX_ENABLE_EXFAT */ /* Make them part of the errors reported to the caller. */ *errors_detected = *errors_detected | current_errors; /* Update the trace event with the errors detected. */ FX_TRACE_EVENT_UPDATE(trace_event, trace_timestamp, FX_TRACE_MEDIA_CHECK, 0, 0, 0, *errors_detected) /* Determine if the I/O error is set. */ if (current_errors & FX_IO_ERROR) { /* Release media protection. */ FX_UNPROTECT /* File I/O Error. */ return(FX_IO_ERROR); }if (current_errors & FX_IO_ERROR) { ... } /* Check for errors. */ if (*errors_detected) { /* Determine if we can fix the FAT chain. */ if (error_correction_option & FX_FAT_CHAIN_ERROR) { /* Determine if there is a valid cluster to write the EOF to. */ if (valid_clusters) { /* Write EOF in the last FAT entry. */ status = _fx_utility_FAT_entry_write(media_ptr, last_cluster, media_ptr -> fx_media_fat_last); /* Determine if the write was successful. */ if (status) { /* Release media protection. */ FX_UNPROTECT /* Return the error code. */ return(status); }if (status) { ... } }if (valid_clusters) { ... } }if (error_correction_option & FX_FAT_CHAIN_ERROR) { ... } }if (*errors_detected) { ... } /* Determine if we need to update the size of the directory entry. */ if (dir_entry_ptr -> fx_dir_entry_file_size > (valid_clusters * bytes_per_cluster)) { /* Yes, update the directory entry's size. */ dir_entry_ptr -> fx_dir_entry_file_size = valid_clusters * bytes_per_cluster; /* Determine if the new file size is zero. */ if (dir_entry_ptr -> fx_dir_entry_file_size == 0) { /* Consider this a directory error. */ *errors_detected = *errors_detected | FX_DIRECTORY_ERROR; /* Update the trace event with the errors detected. */ FX_TRACE_EVENT_UPDATE(trace_event, trace_timestamp, FX_TRACE_MEDIA_CHECK, 0, 0, 0, *errors_detected) /* Clear the starting cluster number of this directory entry. */ dir_entry_ptr -> fx_dir_entry_cluster = 0; /* If directory fixing is required, delete this directory entry as well. */ if (error_correction_option & FX_DIRECTORY_ERROR) { /* Mark the entry as deleted. */ dir_entry_ptr -> fx_dir_entry_name[0] = (CHAR)FX_DIR_ENTRY_FREE; dir_entry_ptr -> fx_dir_entry_short_name[0] = (CHAR)FX_DIR_ENTRY_FREE; }if (error_correction_option & FX_DIRECTORY_ERROR) { ... } }if (dir_entry_ptr -> fx_dir_entry_file_size == 0) { ... } /* Only update the directory if the FAT chain was actually updated. */ if (error_correction_option & FX_FAT_CHAIN_ERROR) { /* Update the directory entry. */ status = _fx_directory_entry_write(media_ptr, dir_entry_ptr); /* Determine if the write was successful. */ if (status) { /* Release media protection. */ FX_UNPROTECT /* Return the error code. */ return(status); }if (status) { ... } }if (error_correction_option & FX_FAT_CHAIN_ERROR) { ... } }if (dir_entry_ptr -> fx_dir_entry_file_size > (valid_clusters * bytes_per_cluster)) { ... } /* Determine if the entry is a sub-directory. */ if ((dir_entry_ptr -> fx_dir_entry_attributes & FX_DIRECTORY) && (valid_clusters == 0)) { /* Consider this a directory error. */ *errors_detected = *errors_detected | FX_DIRECTORY_ERROR; /* Update the trace event with the errors detected. */ FX_TRACE_EVENT_UPDATE(trace_event, trace_timestamp, FX_TRACE_MEDIA_CHECK, 0, 0, 0, *errors_detected) /* Determine if we can fix the error. */ if (error_correction_option & FX_DIRECTORY_ERROR) { /* Yes, make the directory entry free. */ dir_entry_ptr -> fx_dir_entry_name[0] = (CHAR)FX_DIR_ENTRY_FREE; dir_entry_ptr -> fx_dir_entry_short_name[0] = (CHAR)FX_DIR_ENTRY_FREE; /* Delete the sub-directory entry. */ status = _fx_directory_entry_write(media_ptr, dir_entry_ptr); /* Determine if the write was successful. */ if (status) { /* Release media protection. */ FX_UNPROTECT /* Return the error code. */ return(status); }if (status) { ... } /* Move to next entry. */ i++; continue; }if (error_correction_option & FX_DIRECTORY_ERROR) { ... } }if ((dir_entry_ptr -> fx_dir_entry_attributes & FX_DIRECTORY) && (valid_clusters == 0)) { ... } /* Determine if the entry is a directory. */ if (dir_entry_ptr -> fx_dir_entry_attributes & FX_DIRECTORY) { /* Current entry is a directory. The algorithm is designed to follow all sub-directories immediately, i.e., a depth first search. *//* ... */ /* First, save the next entry position. */ current_directory[current_directory_index].current_directory_entry = i + 1; /* Push the current directory entry on the stack. */ current_directory_index++; /* Check for current directory stack overflow. */ if (current_directory_index >= FX_MAX_DIRECTORY_NESTING) { /* Release media protection. */ FX_UNPROTECT /* Current directory stack overflow. Return error. */ return(FX_NOT_ENOUGH_MEMORY); }if (current_directory_index >= FX_MAX_DIRECTORY_NESTING) { ... } /* Otherwise, setup the new directory entry. */ current_directory[current_directory_index].current_total_entries = (valid_clusters * bytes_per_cluster) / FX_DIR_ENTRY_SIZE; current_directory[current_directory_index].current_start_cluster = dir_entry_ptr -> fx_dir_entry_cluster; current_directory[current_directory_index].current_directory_entry = 2; /* Setup new source directory. */ source_dir_ptr -> fx_dir_entry_cluster = dir_entry_ptr -> fx_dir_entry_cluster; source_dir_ptr -> fx_dir_entry_file_size = current_directory[current_directory_index].current_total_entries; source_dir_ptr -> fx_dir_entry_last_search_cluster = 0; temp_dir_ptr = source_dir_ptr; /* Skip the first two entries of sub-directories. */ i = 2; #ifdef FX_ENABLE_EXFAT /* For exFAT, there is no dir-entries for ".." and ".". */ if (media_ptr -> fx_media_FAT_type == FX_exFAT) { current_directory[current_directory_index].current_directory_entry = 0; i = 0; }if (media_ptr -> fx_media_FAT_type == FX_exFAT) { ... } /* ... */#endif /* FX_ENABLE_EXFAT */ }if (dir_entry_ptr -> fx_dir_entry_attributes & FX_DIRECTORY) { ... } else { /* Regular file entry. */ /* Check for an invalid file size. */ if (((valid_clusters * bytes_per_cluster) - dir_entry_ptr -> fx_dir_entry_file_size) > bytes_per_cluster) { /* There are more clusters allocated than needed for the file's size. Indicate that this error is present. *//* ... */ *errors_detected = *errors_detected | FX_FILE_SIZE_ERROR; /* Update the trace event with the errors detected. */ FX_TRACE_EVENT_UPDATE(trace_event, trace_timestamp, FX_TRACE_MEDIA_CHECK, 0, 0, 0, *errors_detected) /* For now, don't shorten the cluster chain. */ }if (((valid_clusters * bytes_per_cluster) - dir_entry_ptr -> fx_dir_entry_file_size) > bytes_per_cluster) { ... } /* Look into the next entry in the current directory. */ i++; }else { ... } }while (i < current_directory[current_directory_index].current_total_entries) { ... } /* Once we get here, we have exhausted the current directory and need to return to the previous directory. *//* ... */ /* Check for being at the root directory. */ if (current_directory_index == 0) { /* Yes, we have now exhausted searching the root directory so we are done! */ break; }if (current_directory_index == 0) { ... } /* Backup to the place we left off in the previous directory. */ current_directory_index--; /* Determine if we are now back at the root directory. */ if (current_directory[current_directory_index].current_start_cluster == media_ptr -> fx_media_fat_last) { /* The search directory should be NULL since it is the root directory. */ temp_dir_ptr = FX_NULL; }if (current_directory[current_directory_index].current_start_cluster == media_ptr -> fx_media_fat_last) { ... } else { /* Otherwise, we are returning to a sub-directory. Setup the search directory appropriately. *//* ... */ source_dir_ptr -> fx_dir_entry_cluster = current_directory[current_directory_index].current_start_cluster; source_dir_ptr -> fx_dir_entry_file_size = current_directory[current_directory_index].current_total_entries; source_dir_ptr -> fx_dir_entry_last_search_cluster = 0; temp_dir_ptr = source_dir_ptr; }else { ... } ...} while (1); #ifdef FX_ENABLE_EXFAT if (media_ptr -> fx_media_FAT_type == FX_exFAT) { /* If exFAT is in use, compare the logical_fat with Allocation Bitmap Table directly. */ current_errors = _fx_media_check_exFAT_lost_cluster_check(media_ptr, logical_fat, media_ptr -> fx_media_total_clusters, error_correction_option); }if (media_ptr -> fx_media_FAT_type == FX_exFAT) { ... } else { #endif /* FX_ENABLE_EXFAT */ /* At this point, all the files and sub-directories have been examined. We now need to check for lost clusters in the logical FAT. A lost cluster is basically anything that is not reported in the logical FAT that has a non-zero value in the real FAT. *//* ... */ current_errors = _fx_media_check_lost_cluster_check(media_ptr, logical_fat, media_ptr -> fx_media_total_clusters, error_correction_option); #ifdef FX_ENABLE_EXFAT }else { ... } #endif /* FX_ENABLE_EXFAT */ /* Incorporate the error returned by the lost FAT check. */ *errors_detected = *errors_detected | current_errors; /* Update the trace event with the errors detected. */ FX_TRACE_EVENT_UPDATE(trace_event, trace_timestamp, FX_TRACE_MEDIA_CHECK, 0, 0, 0, *errors_detected) /* Determine if the I/O error is set. */ if (current_errors & FX_IO_ERROR) { /* Release media protection. */ FX_UNPROTECT /* File I/O Error. */ return(FX_IO_ERROR); }if (current_errors & FX_IO_ERROR) { ... } /* Determine if there was any error and update was selected. */ if ((*errors_detected) && (error_correction_option)) { /* Flush any unwritten items to the media. */ _fx_media_flush(media_ptr); }if ((*errors_detected) && (error_correction_option)) { ... } /* Release media protection. */ FX_UNPROTECT /* At this point, we have completed the diagnostic of the media, return success! */ return(FX_SUCCESS); }{ ... }
Details
Show:
from
Types: Columns:
This file uses the notable symbols shown below. Click anywhere in the file to view more details.