1
10
13
14
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
116
117
118
121
122
123
124
125
126
127
128
129
130
140
141
142
143
144
145
146
150
151
155
156
160
161
165
166
167
168
169
170
171
172
173
174
175
181
182
187
188
189
193
194
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
233
234
235
236
247
248
249
250
251
252
253
254
255
256
264
265
281
282
285
286
287
288
289
290
292
293
294
295
296
297
298
299
300
302
303
304
305
306
307
308
309
310
311
312
314
315
316
317
318
319
320
322
323
324
325
326
327
328
329
330
334
335
336
337
338
339
340
341
342
343
344
346
347
348
349
350
351
352
353
354
355
359
360
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
405
409
410
411
412
413
414
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
466
467
468
469
470
471
477
478
479
480
481
482
483
484
485
486
487
488
489
495
496
497
498
499
500
501
502
503
504
505
506
512
513
514
515
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
565
566
568
569
570
571
573
574
575
576
577
578
584
585
586
587
588
589
590
591
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
623
624
625
626
627
628
629
630
636
637
638
639
640
641
643
644
645
646
652
653
654
655
656
657
658
659
660
661
662
663
664
665
671
672
673
678
679
680
681
685
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
709
710
711
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
734
740
741
742
743
744
746
747
748
754
755
756
757
758
759
765
766
767
770
771
772
773
774
775
776
777
778
...
...
...
#define FX_SOURCE_CODE
#include "fx_api.h"
#include "fx_system.h"
#include "fx_directory.h"
#include "fx_utility.h"
#ifdef FX_ENABLE_EXFAT
#include "fx_directory_exFAT.h"
#endif
...
...
UINT _fx_directory_free_search(FX_MEDIA *media_ptr, FX_DIR_ENTRY *directory_ptr, FX_DIR_ENTRY *entry_ptr)
{
ULONG i, j;
UCHAR *work_ptr;
UINT status, total_entries;
ULONG entry_sector, entry_offset;
ULONG FAT_index, FAT_value;
ULONG cluster, total_clusters, clusters_needed;
ULONG first_new_cluster, last_cluster, clusters;
ULONG directory_index;
ULONG directory_entries;
ULONG logical_sector;
FX_DIR_ENTRY *search_dir_ptr;
ULONG free_entry_start;
UINT sectors;
FX_INT_SAVE_AREA
#ifdef FX_ENABLE_EXFAT
if (media_ptr -> fx_media_FAT_type == FX_exFAT)
{
return(_fx_directory_exFAT_free_search(media_ptr, directory_ptr, entry_ptr));
}if (media_ptr -> fx_media_FAT_type == FX_exFAT) { ... }
/* ... */#endif
#ifndef FX_MEDIA_STATISTICS_DISABLE
media_ptr -> fx_media_directory_free_searches++;/* ... */
#endif
entry_sector = entry_offset = 0;
entry_ptr -> fx_dir_entry_long_name_present = 0;
if (entry_ptr -> fx_dir_entry_name[0] == '.')
{
if (entry_ptr -> fx_dir_entry_name[1] == '.')
{
return(FX_INVALID_NAME);
}if (entry_ptr -> fx_dir_entry_name[1] == '.') { ... }
}if (entry_ptr -> fx_dir_entry_name[0] == '.') { ... }
for (i = 0, j = 0; entry_ptr -> fx_dir_entry_name[i]; i++)
{
if ((entry_ptr -> fx_dir_entry_name[i] >= 'A') && (entry_ptr -> fx_dir_entry_name[i] <= 'Z'))
{
continue;
}if ((entry_ptr -> fx_dir_entry_name[i] >= 'A') && (entry_ptr -> fx_dir_entry_name[i] <= 'Z')) { ... }
else if ((entry_ptr -> fx_dir_entry_name[i] >= '0') && (entry_ptr -> fx_dir_entry_name[i] <= '9'))
{
continue;
}else if ((entry_ptr -> fx_dir_entry_name[i] >= '0') && (entry_ptr -> fx_dir_entry_name[i] <= '9')) { ... }
else if ((entry_ptr -> fx_dir_entry_name[i] >= 'a') && (entry_ptr -> fx_dir_entry_name[i] <= 'z'))
{
entry_ptr -> fx_dir_entry_long_name_present = 1;
}else if ((entry_ptr -> fx_dir_entry_name[i] >= 'a') && (entry_ptr -> fx_dir_entry_name[i] <= 'z')) { ... }
else if (entry_ptr -> fx_dir_entry_name[i] == ' ')
{
entry_ptr -> fx_dir_entry_long_name_present = 1;
}else if (entry_ptr -> fx_dir_entry_name[i] == ' ') { ... }
else if (entry_ptr -> fx_dir_entry_name[i] == '.')
{
if (j == 0)
{
j = i;
if (i == 0)
{
entry_ptr -> fx_dir_entry_long_name_present = 1;
}if (i == 0) { ... }
}if (j == 0) { ... }
else
{
entry_ptr -> fx_dir_entry_long_name_present = 1;
}else { ... }
}else if (entry_ptr -> fx_dir_entry_name[i] == '.') { ... }
else if ((UCHAR)entry_ptr -> fx_dir_entry_name[i] == (UCHAR)0xE5)
{
entry_ptr -> fx_dir_entry_long_name_present = 1;
}else if ((UCHAR)entry_ptr -> fx_dir_entry_name[i] == (UCHAR)0xE5) { ... }
else if ((UCHAR)entry_ptr -> fx_dir_entry_name[i] > (UCHAR)127)
{
continue;
}else if ((UCHAR)entry_ptr -> fx_dir_entry_name[i] > (UCHAR)127) { ... }
else if ((entry_ptr -> fx_dir_entry_name[i] == '~') ||
(entry_ptr -> fx_dir_entry_name[i] == '-') ||
(entry_ptr -> fx_dir_entry_name[i] == '_') ||
(entry_ptr -> fx_dir_entry_name[i] == '}') ||
(entry_ptr -> fx_dir_entry_name[i] == '{') ||
(entry_ptr -> fx_dir_entry_name[i] == '(') ||
(entry_ptr -> fx_dir_entry_name[i] == ')') ||
(entry_ptr -> fx_dir_entry_name[i] == '`') ||
(entry_ptr -> fx_dir_entry_name[i] == '\'') ||
(entry_ptr -> fx_dir_entry_name[i] == '!') ||
(entry_ptr -> fx_dir_entry_name[i] == '#') ||
(entry_ptr -> fx_dir_entry_name[i] == '$') ||
(entry_ptr -> fx_dir_entry_name[i] == '&') ||
(entry_ptr -> fx_dir_entry_name[i] == '@') ||
(entry_ptr -> fx_dir_entry_name[i] == '^') ||
(entry_ptr -> fx_dir_entry_name[i] == '%'))
{
continue;
}else if ((entry_ptr -> fx_dir_entry_name[i] == '~') || (entry_ptr -> fx_dir_entry_name[i] == '-') || (entry_ptr -> fx_dir_entry_name[i] == '_') || (entry_ptr -> fx_dir_entry_name[i] == '}') || (entry_ptr -> fx_dir_entry_name[i] == '{') || (entry_ptr -> fx_dir_entry_name[i] == '(') || (entry_ptr -> fx_dir_entry_name[i] == ')') || (entry_ptr -> fx_dir_entry_name[i] == '`') || (entry_ptr -> fx_dir_entry_name[i] == '\'') || (entry_ptr -> fx_dir_entry_name[i] == '!') || (entry_ptr -> fx_dir_entry_name[i] == '#') || (entry_ptr -> fx_dir_entry_name[i] == '$') || (entry_ptr -> fx_dir_entry_name[i] == '&') || (entry_ptr -> fx_dir_entry_name[i] == '@') || (entry_ptr -> fx_dir_entry_name[i] == '^') || (entry_ptr -> fx_dir_entry_name[i] == '%')) { ... }
else if ((entry_ptr -> fx_dir_entry_name[i] == '+') ||
(entry_ptr -> fx_dir_entry_name[i] == ',') ||
(entry_ptr -> fx_dir_entry_name[i] == ';') ||
(entry_ptr -> fx_dir_entry_name[i] == '=') ||
(entry_ptr -> fx_dir_entry_name[i] == '[') ||
(entry_ptr -> fx_dir_entry_name[i] == ']'))
{
entry_ptr -> fx_dir_entry_long_name_present = 1;
}else if ((entry_ptr -> fx_dir_entry_name[i] == '+') || (entry_ptr -> fx_dir_entry_name[i] == ',') || (entry_ptr -> fx_dir_entry_name[i] == ';') || (entry_ptr -> fx_dir_entry_name[i] == '=') || (entry_ptr -> fx_dir_entry_name[i] == '[') || (entry_ptr -> fx_dir_entry_name[i] == ']')) { ... }
else
{
return(FX_INVALID_NAME);
}else { ... }
}for (i = 0, j = 0; entry_ptr -> fx_dir_entry_name[i]; i++) { ... }
if (j != 0)
{
if ((i - j) > 4)
{
entry_ptr -> fx_dir_entry_long_name_present = 1;
}if ((i - j) > 4) { ... }
}if (j != 0) { ... }
if ((i <= 12) && (entry_ptr -> fx_dir_entry_long_name_present == 0))
{
total_entries = 1;
if ((j >= 9) || ((i - j) >= 9))
{
/* ... */
entry_ptr -> fx_dir_entry_long_name_present = 1;
total_entries = 2;
}if ((j >= 9) || ((i - j) >= 9)) { ... }
}if ((i <= 12) && (entry_ptr -> fx_dir_entry_long_name_present == 0)) { ... }
else
{
/* ... */
if (i % 13 == 0)
{
total_entries = i / 13 + 1;
}if (i % 13 == 0) { ... }
else
{
total_entries = i / 13 + 2;
}else { ... }
}else { ... }
/* ... */
if (directory_ptr -> fx_dir_entry_name[0])
{
/* ... */
directory_entries = (ULONG)directory_ptr -> fx_dir_entry_file_size;
search_dir_ptr = directory_ptr;
search_dir_ptr -> fx_dir_entry_last_search_cluster = 0;
/* ... */
directory_index = 2;
}if (directory_ptr -> fx_dir_entry_name[0]) { ... }
else
{
directory_entries = (ULONG)media_ptr -> fx_media_root_directory_entries;
/* ... */
search_dir_ptr = FX_NULL;
directory_index = 0;
}else { ... }
/* ... */
free_entry_start = directory_entries;
do
{
status = _fx_directory_entry_read(media_ptr, search_dir_ptr, &directory_index, entry_ptr);
if (status != FX_SUCCESS)
{
return(status);
}if (status != FX_SUCCESS) { ... }
if ((((UCHAR)entry_ptr -> fx_dir_entry_name[0] == (UCHAR)FX_DIR_ENTRY_FREE) && (entry_ptr -> fx_dir_entry_short_name[0] == 0)) ||
((UCHAR)entry_ptr -> fx_dir_entry_name[0] == (UCHAR)FX_DIR_ENTRY_DONE))
{
if (total_entries > 1)
{
/* ... */
if (entry_ptr -> fx_dir_entry_name[0] == FX_DIR_ENTRY_DONE)
{
entry_ptr -> fx_dir_entry_long_name_present = 0;
entry_ptr -> fx_dir_entry_name[0] = (CHAR)FX_DIR_ENTRY_FREE;
entry_ptr -> fx_dir_entry_name[1] = (CHAR)0;
status = _fx_directory_entry_write(media_ptr, entry_ptr);
if(status != FX_SUCCESS)
{
return(status);
}if (status != FX_SUCCESS) { ... }
/* ... */
}if (entry_ptr -> fx_dir_entry_name[0] == FX_DIR_ENTRY_DONE) { ... }
}if (total_entries > 1) { ... }
if (free_entry_start == directory_entries)
{
free_entry_start = directory_index;
entry_sector = (ULONG)entry_ptr -> fx_dir_entry_log_sector;
entry_offset = entry_ptr -> fx_dir_entry_byte_offset;
}if (free_entry_start == directory_entries) { ... }
if ((directory_index - free_entry_start + 1) >= total_entries)
{
/* ... */
entry_ptr -> fx_dir_entry_log_sector = entry_sector;
entry_ptr -> fx_dir_entry_byte_offset = entry_offset;
entry_ptr -> fx_dir_entry_reserved = 0;
entry_ptr -> fx_dir_entry_created_time_ms = 0;
FX_DISABLE_INTS
entry_ptr -> fx_dir_entry_created_time = _fx_system_time;
entry_ptr -> fx_dir_entry_created_date = _fx_system_date;
entry_ptr -> fx_dir_entry_last_accessed_date = _fx_system_date;
FX_RESTORE_INTS
if (total_entries == 1)
{
entry_ptr -> fx_dir_entry_long_name_present = 0;
}if (total_entries == 1) { ... }
else
{
entry_ptr -> fx_dir_entry_long_name_present = 1;
}else { ... }
return(FX_SUCCESS);
}if ((directory_index - free_entry_start + 1) >= total_entries) { ... }
}if ((((UCHAR)entry_ptr -> fx_dir_entry_name[0] == (UCHAR)FX_DIR_ENTRY_FREE) && (entry_ptr -> fx_dir_entry_short_name[0] == 0)) || ((UCHAR)entry_ptr -> fx_dir_entry_name[0] == (UCHAR)FX_DIR_ENTRY_DONE)) { ... }
else
{
free_entry_start = directory_entries;
}else { ... }
directory_index++;
if (directory_index >= directory_entries)
{
sectors = ((total_entries * FX_DIR_ENTRY_SIZE) + (media_ptr -> fx_media_bytes_per_sector - 1))/
media_ptr -> fx_media_bytes_per_sector;
clusters_needed = (sectors + (media_ptr -> fx_media_sectors_per_cluster - 1)) / media_ptr -> fx_media_sectors_per_cluster;
/* ... */
if (((search_dir_ptr) || (media_ptr -> fx_media_32_bit_FAT)) && (media_ptr -> fx_media_available_clusters > clusters_needed))
{
first_new_cluster = 0;
total_clusters = media_ptr -> fx_media_total_clusters;
last_cluster = 0;
FAT_index = media_ptr -> fx_media_cluster_search_start;
clusters = clusters_needed;
while (clusters)
{
clusters--;
do
{
if (!total_clusters)
{
/* ... */
return(FX_NO_MORE_SPACE);
}if (!total_clusters) { ... }
status = _fx_utility_FAT_entry_read(media_ptr, FAT_index, &FAT_value);
if (status != FX_SUCCESS)
{
return(status);
}if (status != FX_SUCCESS) { ... }
total_clusters--;
if (FAT_value == FX_FREE_CLUSTER)
{
media_ptr -> fx_media_cluster_search_start = FAT_index + 1;
if (media_ptr -> fx_media_cluster_search_start >= (media_ptr -> fx_media_total_clusters + FX_FAT_ENTRY_START))
{
media_ptr -> fx_media_cluster_search_start = FX_FAT_ENTRY_START;
}if (media_ptr -> fx_media_cluster_search_start >= (media_ptr -> fx_media_total_clusters + FX_FAT_ENTRY_START)) { ... }
break;
}if (FAT_value == FX_FREE_CLUSTER) { ... }
else
{
FAT_index++;
if (FAT_index >= (media_ptr -> fx_media_total_clusters + FX_FAT_ENTRY_START))
{
FAT_index = FX_FAT_ENTRY_START;
}if (FAT_index >= (media_ptr -> fx_media_total_clusters + FX_FAT_ENTRY_START)) { ... }
}else { ... }
...} while (FX_TRUE);
/* ... */
logical_sector = ((ULONG) media_ptr -> fx_media_data_sector_start) +
((((ULONG) FAT_index) - FX_FAT_ENTRY_START) *
((ULONG) media_ptr -> fx_media_sectors_per_cluster));
sectors = media_ptr -> fx_media_sectors_per_cluster;
status = _fx_utility_logical_sector_read(media_ptr, (ULONG64) logical_sector,
media_ptr -> fx_media_memory_buffer, ((ULONG) 1), FX_DIRECTORY_SECTOR);
if (status != FX_SUCCESS)
{
return(status);
}if (status != FX_SUCCESS) { ... }
work_ptr = (UCHAR *)media_ptr -> fx_media_memory_buffer;
i = 0;
while (i < media_ptr -> fx_media_bytes_per_sector)
{
*((ULONG *)work_ptr) = (ULONG)0;
work_ptr = work_ptr + sizeof(ULONG);
i = i + (ULONG)sizeof(ULONG);
}while (i < media_ptr -> fx_media_bytes_per_sector) { ... }
status = _fx_utility_logical_sector_write(media_ptr, (ULONG64) logical_sector,
media_ptr -> fx_media_memory_buffer, ((ULONG) 1), FX_DIRECTORY_SECTOR);
if (status != FX_SUCCESS)
{
return(status);
}if (status != FX_SUCCESS) { ... }
/* ... */
if (sectors > 1)
{
/* ... */
status = _fx_utility_logical_sector_flush(media_ptr, (ULONG64) (logical_sector + 1), (ULONG64) (sectors - 1), FX_TRUE);
if (status != FX_SUCCESS)
{
return(status);
}if (status != FX_SUCCESS) { ... }
sectors--;
while (sectors)
{
#ifndef FX_MEDIA_STATISTICS_DISABLE
media_ptr -> fx_media_driver_write_requests++;/* ... */
#endif
media_ptr -> fx_media_driver_request = FX_DRIVER_WRITE;
media_ptr -> fx_media_driver_status = FX_IO_ERROR;
media_ptr -> fx_media_driver_buffer = media_ptr -> fx_media_memory_buffer;
media_ptr -> fx_media_driver_logical_sector = (ULONG)logical_sector + ((ULONG)sectors);
media_ptr -> fx_media_driver_sectors = 1;
media_ptr -> fx_media_driver_sector_type = FX_DIRECTORY_SECTOR;
media_ptr -> fx_media_driver_system_write = FX_TRUE;
FX_TRACE_IN_LINE_INSERT(FX_TRACE_INTERNAL_IO_DRIVER_WRITE, media_ptr, ((ULONG)logical_sector) + ((ULONG)sectors), 1, media_ptr -> fx_media_memory_buffer, FX_TRACE_INTERNAL_EVENTS, 0, 0)
(media_ptr -> fx_media_driver_entry) (media_ptr);
media_ptr -> fx_media_driver_system_write = FX_FALSE;
if (media_ptr -> fx_media_driver_status != FX_SUCCESS)
{
return(media_ptr -> fx_media_driver_status);
}if (media_ptr -> fx_media_driver_status != FX_SUCCESS) { ... }
sectors--;
}while (sectors) { ... }
}if (sectors > 1) { ... }
if (first_new_cluster == 0)
{
first_new_cluster = FAT_index;
}if (first_new_cluster == 0) { ... }
if (last_cluster)
{
/* ... */
status = _fx_utility_FAT_entry_write(media_ptr, last_cluster, FAT_index);
if (status != FX_SUCCESS)
{
return(status);
}if (status != FX_SUCCESS) { ... }
}if (last_cluster) { ... }
last_cluster = FAT_index;
FAT_index = media_ptr -> fx_media_cluster_search_start;
}while (clusters) { ... }
status = _fx_utility_FAT_entry_write(media_ptr, last_cluster, media_ptr -> fx_media_fat_last);
if (status != FX_SUCCESS)
{
return(status);
}if (status != FX_SUCCESS) { ... }
#ifdef FX_FAULT_TOLERANT
_fx_utility_FAT_flush(media_ptr);/* ... */
#endif
if (search_dir_ptr)
{
cluster = search_dir_ptr -> fx_dir_entry_cluster;
}if (search_dir_ptr) { ... }
else
{
cluster = media_ptr -> fx_media_root_cluster_32;
}else { ... }
last_cluster = 0;
i = 0;
while (cluster < media_ptr -> fx_media_fat_reserved)
{
status = _fx_utility_FAT_entry_read(media_ptr, cluster, &FAT_value);
i++;
if (status != FX_SUCCESS)
{
return(status);
}if (status != FX_SUCCESS) { ... }
if ((cluster < FX_FAT_ENTRY_START) || (cluster == FAT_value) || (i > media_ptr -> fx_media_total_clusters))
{
return(FX_FAT_READ_ERROR);
}if ((cluster < FX_FAT_ENTRY_START) || (cluster == FAT_value) || (i > media_ptr -> fx_media_total_clusters)) { ... }
last_cluster = cluster;
cluster = FAT_value;
}while (cluster < media_ptr -> fx_media_fat_reserved) { ... }
media_ptr -> fx_media_available_clusters = media_ptr -> fx_media_available_clusters - clusters_needed;
directory_entries = directory_entries + ((clusters_needed * media_ptr -> fx_media_sectors_per_cluster) * media_ptr -> fx_media_bytes_per_sector) / FX_DIR_ENTRY_SIZE;
/* ... */
if (!(((UCHAR)entry_ptr -> fx_dir_entry_name[0] == (UCHAR) FX_DIR_ENTRY_FREE) && (entry_ptr -> fx_dir_entry_short_name[0] == 0)))
{
free_entry_start = directory_entries;
}if (!(((UCHAR)entry_ptr -> fx_dir_entry_name[0] == (UCHAR) FX_DIR_ENTRY_FREE) && (entry_ptr -> fx_dir_entry_short_name[0] == 0))) { ... }
directory_ptr -> fx_dir_entry_file_size = directory_entries;
/* ... */
if ((media_ptr -> fx_media_32_bit_FAT) && (search_dir_ptr == FX_NULL))
{
media_ptr -> fx_media_root_directory_entries = directory_entries;
}if ((media_ptr -> fx_media_32_bit_FAT) && (search_dir_ptr == FX_NULL)) { ... }
status = _fx_utility_FAT_entry_write(media_ptr, last_cluster, first_new_cluster);
if (status != FX_SUCCESS)
{
return(status);
}if (status != FX_SUCCESS) { ... }
#ifdef FX_FAULT_TOLERANT
_fx_utility_FAT_flush(media_ptr);/* ... */
#endif
}if (((search_dir_ptr) || (media_ptr -> fx_media_32_bit_FAT)) && (media_ptr -> fx_media_available_clusters > clusters_needed)) { ... }
}if (directory_index >= directory_entries) { ... }
...} while (directory_index < directory_entries);
return(FX_NO_MORE_SPACE);
}{ ... }