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
110
111
112
113
114
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
199
200
202
203
204
205
206
207
208
209
215
216
217
219
220
221
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
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
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
320
321
322
323
324
325
326
327
328
329
330
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
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
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
...
...
...
#define FX_SOURCE_CODE
#include "fx_api.h"
#include "fx_system.h"
#include "fx_utility.h"
#ifdef FX_ENABLE_FAULT_TOLERANT
#include "fx_fault_tolerant.h"
#endif
...
...
UINT _fx_utility_FAT_entry_read(FX_MEDIA *media_ptr, ULONG cluster, ULONG *entry_ptr)
{
ULONG FAT_sector;
ULONG byte_offset, entry32;
UCHAR *FAT_ptr;
UINT entry, index;
UINT status;
FX_FAT_CACHE_ENTRY *cache_entry_ptr;
FX_FAT_CACHE_ENTRY temp_cache_entry;
#ifdef FX_ENABLE_FAULT_TOLERANT
if (media_ptr -> fx_media_fault_tolerant_enabled &&
(media_ptr -> fx_media_fault_tolerant_state & FX_FAULT_TOLERANT_STATE_STARTED))
{
status = _fx_fault_tolerant_read_FAT(media_ptr, cluster, entry_ptr, FX_FAULT_TOLERANT_FAT_LOG_TYPE);
if (status != FX_READ_CONTINUE)
{
return(status);
}if (status != FX_READ_CONTINUE) { ... }
}if (media_ptr -> fx_media_fault_tolerant_enabled && (media_ptr -> fx_media_fault_tolerant_state & FX_FAULT_TOLERANT_STATE_STARTED)) { ... }
/* ... */#endif
#ifndef FX_MEDIA_STATISTICS_DISABLE
media_ptr -> fx_media_fat_entry_reads++;
media_ptr -> fx_media_fat_entry_cache_read_hits++;/* ... */
#endif
FX_UTILITY_FAT_ENTRY_READ_EXTENSION
index = (cluster & FX_FAT_CACHE_HASH_MASK) * FX_FAT_CACHE_DEPTH;
cache_entry_ptr = &media_ptr -> fx_media_fat_cache[index];
#ifndef FX_DISABLE_FAT_ENTRY_REFRESH
/* ... */
if ((cache_entry_ptr -> fx_fat_cache_entry_cluster) == cluster)
{
*entry_ptr = cache_entry_ptr -> fx_fat_cache_entry_value;
return(FX_SUCCESS);
}if ((cache_entry_ptr -> fx_fat_cache_entry_cluster) == cluster) { ... }
else if (((cache_entry_ptr + 1) -> fx_fat_cache_entry_cluster) == cluster)
{
*entry_ptr = (cache_entry_ptr + 1) -> fx_fat_cache_entry_value;
temp_cache_entry = *(cache_entry_ptr);
*(cache_entry_ptr) = *(cache_entry_ptr + 1);
*(cache_entry_ptr + 1) = temp_cache_entry;
return(FX_SUCCESS);
}else if (((cache_entry_ptr + 1) -> fx_fat_cache_entry_cluster) == cluster) { ... }
else if (((cache_entry_ptr + 2) -> fx_fat_cache_entry_cluster) == cluster)
{
*entry_ptr = (cache_entry_ptr + 2) -> fx_fat_cache_entry_value;
temp_cache_entry = *(cache_entry_ptr);
*(cache_entry_ptr) = *(cache_entry_ptr + 2);
*(cache_entry_ptr + 2) = *(cache_entry_ptr + 1);
*(cache_entry_ptr + 1) = temp_cache_entry;
return(FX_SUCCESS);
}else if (((cache_entry_ptr + 2) -> fx_fat_cache_entry_cluster) == cluster) { ... }
else if (((cache_entry_ptr + 3) -> fx_fat_cache_entry_cluster) == cluster)
{
*entry_ptr = (cache_entry_ptr + 3) -> fx_fat_cache_entry_value;
temp_cache_entry = *(cache_entry_ptr);
*(cache_entry_ptr) = *(cache_entry_ptr + 3);
*(cache_entry_ptr + 3) = *(cache_entry_ptr + 2);
*(cache_entry_ptr + 2) = *(cache_entry_ptr + 1);
*(cache_entry_ptr + 1) = temp_cache_entry;
return(FX_SUCCESS);
}else if (((cache_entry_ptr + 3) -> fx_fat_cache_entry_cluster) == cluster) { ... }
/* ... */#else
for (UINT i = 0; i < 4; i++)
{
if (((cache_entry_ptr + i) -> fx_fat_cache_entry_cluster) == cluster)
{
*entry_ptr = (cache_entry_ptr + i) -> fx_fat_cache_entry_value;
return(FX_SUCCESS);
}if (((cache_entry_ptr + i) -> fx_fat_cache_entry_cluster) == cluster) { ... }
}for (UINT i = 0; i < 4; i++) { ... }
/* ... */#endif
/* ... */
if (media_ptr -> fx_media_fat_cache[index + 3].fx_fat_cache_entry_dirty)
{
status = _fx_utility_FAT_flush(media_ptr);
if (status != FX_SUCCESS)
{
return(status);
}if (status != FX_SUCCESS) { ... }
}if (media_ptr -> fx_media_fat_cache[index + 3].fx_fat_cache_entry_dirty) { ... }
/* ... */
#ifndef FX_MEDIA_STATISTICS_DISABLE
media_ptr -> fx_media_fat_entry_cache_read_hits--;
media_ptr -> fx_media_fat_entry_cache_read_misses++;/* ... */
#endif
if (media_ptr -> fx_media_12_bit_FAT)
{
byte_offset = (((ULONG)cluster << 1) + cluster) >> 1;
FAT_sector = (byte_offset / media_ptr -> fx_media_bytes_per_sector) +
(ULONG)media_ptr -> fx_media_reserved_sectors;
status = _fx_utility_logical_sector_read(media_ptr, (ULONG64) FAT_sector,
media_ptr -> fx_media_memory_buffer, ((ULONG) 1), FX_FAT_SECTOR);
if (status != FX_SUCCESS)
{
return(status);
}if (status != FX_SUCCESS) { ... }
byte_offset = byte_offset -
((FAT_sector - (ULONG)media_ptr -> fx_media_reserved_sectors) *
media_ptr -> fx_media_bytes_per_sector);
FAT_ptr = (UCHAR *)media_ptr -> fx_media_memory_buffer + (UINT)byte_offset;
if (cluster & 1)
{
entry = (((UINT)*FAT_ptr) & 0xF0) >> 4;
FAT_ptr++;
if (byte_offset == (ULONG)(media_ptr -> fx_media_bytes_per_sector - 1))
{
FAT_sector++;
status = _fx_utility_logical_sector_read(media_ptr, (ULONG64) FAT_sector,
media_ptr -> fx_media_memory_buffer, ((ULONG) 1), FX_FAT_SECTOR);
if (status != FX_SUCCESS)
{
return(status);
}if (status != FX_SUCCESS) { ... }
FAT_ptr = (UCHAR *)media_ptr -> fx_media_memory_buffer;
}if (byte_offset == (ULONG)(media_ptr -> fx_media_bytes_per_sector - 1)) { ... }
entry = entry | (((UINT)*FAT_ptr) << 4);
}if (cluster & 1) { ... }
else
{
entry = (UINT)(((UINT)*FAT_ptr) & 0xFF);
FAT_ptr++;
if (byte_offset == (ULONG)(media_ptr -> fx_media_bytes_per_sector - 1))
{
FAT_sector++;
status = _fx_utility_logical_sector_read(media_ptr, (ULONG64) FAT_sector,
media_ptr -> fx_media_memory_buffer, ((ULONG) 1), FX_FAT_SECTOR);
if (status != FX_SUCCESS)
{
return(status);
}if (status != FX_SUCCESS) { ... }
FAT_ptr = (UCHAR *)media_ptr -> fx_media_memory_buffer;
}if (byte_offset == (ULONG)(media_ptr -> fx_media_bytes_per_sector - 1)) { ... }
entry = entry | ((((UINT)*FAT_ptr) & 0x0F) << 8);
}else { ... }
if (entry >= FX_MAX_12BIT_CLUST)
{
entry = entry | FX_SIGN_EXTEND;
}if (entry >= FX_MAX_12BIT_CLUST) { ... }
*entry_ptr = entry;
}if (media_ptr -> fx_media_12_bit_FAT) { ... }
#ifdef FX_ENABLE_EXFAT
else if (FX_FAT16 == media_ptr -> fx_media_FAT_type)
#else
else if (!media_ptr -> fx_media_32_bit_FAT)
#endif
{
byte_offset = (((ULONG)cluster) * 2);
FAT_sector = (byte_offset / media_ptr -> fx_media_bytes_per_sector) +
(ULONG)media_ptr -> fx_media_reserved_sectors;
status = _fx_utility_logical_sector_read(media_ptr, (ULONG64) FAT_sector,
media_ptr -> fx_media_memory_buffer, ((ULONG) 1), FX_FAT_SECTOR);
if (status != FX_SUCCESS)
{
return(status);
}if (status != FX_SUCCESS) { ... }
byte_offset = byte_offset -
((FAT_sector - (ULONG)media_ptr -> fx_media_reserved_sectors) *
media_ptr -> fx_media_bytes_per_sector);
FAT_ptr = (UCHAR *)media_ptr -> fx_media_memory_buffer + (UINT)byte_offset;
entry = _fx_utility_16_unsigned_read(FAT_ptr);
*entry_ptr = entry;
...}
#ifdef FX_ENABLE_EXFAT
else if ((media_ptr -> fx_media_FAT_type == FX_FAT32) ||
(media_ptr -> fx_media_FAT_type == FX_exFAT))/* ... */
#else
else
#endif
{
byte_offset = (((ULONG)cluster) * 4);
FAT_sector = (byte_offset / media_ptr -> fx_media_bytes_per_sector) +
(ULONG)media_ptr -> fx_media_reserved_sectors;
byte_offset = (byte_offset % media_ptr -> fx_media_bytes_per_sector);
status = _fx_utility_logical_sector_read(media_ptr, (ULONG64) FAT_sector,
media_ptr -> fx_media_memory_buffer, ((ULONG) 1), FX_FAT_SECTOR);
if (status != FX_SUCCESS)
{
return(status);
}if (status != FX_SUCCESS) { ... }
FAT_ptr = (UCHAR *)media_ptr -> fx_media_memory_buffer + (ULONG)byte_offset;
entry32 = _fx_utility_32_unsigned_read(FAT_ptr);
#ifdef FX_ENABLE_EXFAT
if (media_ptr -> fx_media_FAT_type == FX_FAT32)
{
#endif
entry32 = entry32 & 0x0FFFFFFF;
#ifdef FX_ENABLE_EXFAT
}if (media_ptr -> fx_media_FAT_type == FX_FAT32) { ... }
#endif
*entry_ptr = entry32;
}else { ... }
*(cache_entry_ptr + 3) = *(cache_entry_ptr + 2);
*(cache_entry_ptr + 2) = *(cache_entry_ptr + 1);
*(cache_entry_ptr + 1) = *(cache_entry_ptr);
cache_entry_ptr -> fx_fat_cache_entry_cluster = cluster;
cache_entry_ptr -> fx_fat_cache_entry_value = *entry_ptr;
return(FX_SUCCESS);
}{ ... }