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_utility.h"
#include "fx_fault_tolerant.h"
...
...
_fx_utility_FAT_entry_read(FX_MEDIA *, ULONG, ULONG *)
Files
loading...
CodeScopeSTM32 Libraries and Samplesfilexcommon/src/fx_utility_FAT_entry_read.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
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
/**************************************************************************/ /* */ /* 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 */ /** */ /** Utility */ /** */... /**************************************************************************/ /**************************************************************************/ #define FX_SOURCE_CODE /* Include necessary system files. */ #include "fx_api.h" #include "fx_system.h" #include "fx_utility.h" #ifdef FX_ENABLE_FAULT_TOLERANT #include "fx_fault_tolerant.h" #endif /* FX_ENABLE_FAULT_TOLERANT */ ... /**************************************************************************/ /* */ /* FUNCTION RELEASE */ /* */ /* _fx_utility_FAT_entry_read PORTABLE C */ /* 6.1 */ /* AUTHOR */ /* */ /* William E. Lamie, Microsoft Corporation */ /* */ /* DESCRIPTION */ /* */ /* This function reads the supplied FAT entry from the first FAT of */ /* the media. 12-bit, 16-bit, and 32-bit FAT reading is supported. */ /* */ /* INPUT */ /* */ /* media_ptr Media control block pointer */ /* cluster Cluster entry number */ /* entry_ptr Pointer to destination for */ /* the FAT entry */ /* */ /* OUTPUT */ /* */ /* return status */ /* */ /* CALLS */ /* */ /* _fx_utility_16_unsigned_read Read a UINT from FAT buffer */ /* _fx_utility_32_unsigned_read Read a ULONG form FAT buffer */ /* _fx_utility_FAT_flush Flush FAT entry cache */ /* _fx_utility_logical_sector_read Read FAT sector into memory */ /* _fx_fault_tolerant_read_FAT Read FAT entry from log file */ /* */ /* CALLED BY */ /* */ /* FileX System Functions */ /* */ /* RELEASE HISTORY */ /* */ /* DATE NAME DESCRIPTION */ /* */ /* 05-19-2020 William E. Lamie Initial Version 6.0 */ /* 09-30-2020 William E. Lamie Modified comment(s), and */ /* added conditional to */ /* disable fat entry refresh, */ /* resulting in version 6.1 */ /* */... /**************************************************************************/ 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)) { /* Redirect this request to log file. */ status = _fx_fault_tolerant_read_FAT(media_ptr, cluster, entry_ptr, FX_FAULT_TOLERANT_FAT_LOG_TYPE); /* Return on success. */ 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 /* FX_ENABLE_FAULT_TOLERANT */ #ifndef FX_MEDIA_STATISTICS_DISABLE /* Increment the number of FAT entry reads and cache hits. */ media_ptr -> fx_media_fat_entry_reads++; media_ptr -> fx_media_fat_entry_cache_read_hits++;/* ... */ #endif /* Extended port-specific processing macro, which is by default defined to white space. */ FX_UTILITY_FAT_ENTRY_READ_EXTENSION /* Calculate the area of the cache for this FAT entry. */ index = (cluster & FX_FAT_CACHE_HASH_MASK) * FX_FAT_CACHE_DEPTH; /* Build a pointer to the cache entry. */ cache_entry_ptr = &media_ptr -> fx_media_fat_cache[index]; #ifndef FX_DISABLE_FAT_ENTRY_REFRESH /* Determine if the FAT entry is in the cache - assuming the depth of the FAT cache is 4 entries. *//* ... */ if ((cache_entry_ptr -> fx_fat_cache_entry_cluster) == cluster) { /* Yes, return the cached value. */ *entry_ptr = cache_entry_ptr -> fx_fat_cache_entry_value; /* Don't move anything since we found the entry. */ /* Return a successful status. */ 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) { /* Yes, return the cached value. */ *entry_ptr = (cache_entry_ptr + 1) -> fx_fat_cache_entry_value; /* Just swap the first and second entry. */ temp_cache_entry = *(cache_entry_ptr); *(cache_entry_ptr) = *(cache_entry_ptr + 1); *(cache_entry_ptr + 1) = temp_cache_entry; /* Return a successful status. */ 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) { /* Yes, return the cached value. */ *entry_ptr = (cache_entry_ptr + 2) -> fx_fat_cache_entry_value; /* Move the third entry to the top and the first two entries down. */ 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 a successful status. */ 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) { /* Yes, return the cached value. */ *entry_ptr = (cache_entry_ptr + 3) -> fx_fat_cache_entry_value; /* Move the last entry to the top and the first three entries down. */ 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 a successful status. */ 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 a successful status. */ return(FX_SUCCESS); }if (((cache_entry_ptr + i) -> fx_fat_cache_entry_cluster) == cluster) { ... } }for (UINT i = 0; i < 4; i++) { ... } /* ... */#endif /* FX_DISABLE_FAT_ENTRY_REFRESH */ /* Determine if the oldest entry was modified, i.e. whether or not it is dirty. *//* ... */ if (media_ptr -> fx_media_fat_cache[index + 3].fx_fat_cache_entry_dirty) { /* Yes, the entry is dirty and needs to be flushed out. */ status = _fx_utility_FAT_flush(media_ptr); /* Check for completion status. */ if (status != FX_SUCCESS) { /* Return error status. */ return(status); }if (status != FX_SUCCESS) { ... } }if (media_ptr -> fx_media_fat_cache[index + 3].fx_fat_cache_entry_dirty) { ... } /* If we get here, the entry was not found in the FAT entry cache. We need to actually read the FAT entry. *//* ... */ #ifndef FX_MEDIA_STATISTICS_DISABLE /* Decrement the number of cache hits. */ media_ptr -> fx_media_fat_entry_cache_read_hits--; /* Increment the number of cache misses. */ media_ptr -> fx_media_fat_entry_cache_read_misses++;/* ... */ #endif /* Determine which type of FAT is present. */ if (media_ptr -> fx_media_12_bit_FAT) { /* Calculate the byte offset to the cluster entry. */ byte_offset = (((ULONG)cluster << 1) + cluster) >> 1; /* Calculate the FAT sector the requested FAT entry resides in. */ FAT_sector = (byte_offset / media_ptr -> fx_media_bytes_per_sector) + (ULONG)media_ptr -> fx_media_reserved_sectors; /* Read the sector in. */ status = _fx_utility_logical_sector_read(media_ptr, (ULONG64) FAT_sector, media_ptr -> fx_media_memory_buffer, ((ULONG) 1), FX_FAT_SECTOR); /* Determine if an error occurred. */ if (status != FX_SUCCESS) { /* Return the error status. */ return(status); }if (status != FX_SUCCESS) { ... } /* Now calculate the byte offset into this FAT sector. */ byte_offset = byte_offset - ((FAT_sector - (ULONG)media_ptr -> fx_media_reserved_sectors) * media_ptr -> fx_media_bytes_per_sector); /* Setup a pointer into the buffer. */ FAT_ptr = (UCHAR *)media_ptr -> fx_media_memory_buffer + (UINT)byte_offset; /* Determine if the cluster entry is odd or even. */ if (cluster & 1) { /* Odd cluster number. */ /* Pickup the lower nibble of the FAT entry. */ entry = (((UINT)*FAT_ptr) & 0xF0) >> 4; /* Move to the next byte of the FAT entry. */ FAT_ptr++; /* Determine if we are now past the end of the FAT buffer in memory. */ if (byte_offset == (ULONG)(media_ptr -> fx_media_bytes_per_sector - 1)) { /* Yes, we need to read the next sector. */ FAT_sector++; status = _fx_utility_logical_sector_read(media_ptr, (ULONG64) FAT_sector, media_ptr -> fx_media_memory_buffer, ((ULONG) 1), FX_FAT_SECTOR); /* Determine if an error occurred. */ if (status != FX_SUCCESS) { /* Return the error status. */ return(status); }if (status != FX_SUCCESS) { ... } /* Setup a pointer into the buffer. */ FAT_ptr = (UCHAR *)media_ptr -> fx_media_memory_buffer; }if (byte_offset == (ULONG)(media_ptr -> fx_media_bytes_per_sector - 1)) { ... } /* Pickup the upper 8 bits of the FAT entry. */ entry = entry | (((UINT)*FAT_ptr) << 4); }if (cluster & 1) { ... } else { /* Even cluster number. */ /* Pickup the lower byte of the FAT entry. */ entry = (UINT)(((UINT)*FAT_ptr) & 0xFF); /* Move to the next nibble of the FAT entry. */ FAT_ptr++; /* Determine if we are now past the end of the FAT buffer in memory. */ if (byte_offset == (ULONG)(media_ptr -> fx_media_bytes_per_sector - 1)) { /* Yes, we need to read the next sector. */ FAT_sector++; status = _fx_utility_logical_sector_read(media_ptr, (ULONG64) FAT_sector, media_ptr -> fx_media_memory_buffer, ((ULONG) 1), FX_FAT_SECTOR); /* Determine if an error occurred. */ if (status != FX_SUCCESS) { return(status); }if (status != FX_SUCCESS) { ... } /* Setup a pointer into the buffer. */ FAT_ptr = (UCHAR *)media_ptr -> fx_media_memory_buffer; }if (byte_offset == (ULONG)(media_ptr -> fx_media_bytes_per_sector - 1)) { ... } /* Pickup the upper 4 bits of the FAT entry. */ entry = entry | ((((UINT)*FAT_ptr) & 0x0F) << 8); }else { ... } /* Determine if we need to do sign extension on the 12-bit eof value. */ if (entry >= FX_MAX_12BIT_CLUST) { /* Yes, we need to sign extend. */ entry = entry | FX_SIGN_EXTEND; }if (entry >= FX_MAX_12BIT_CLUST) { ... } *entry_ptr = entry; }if (media_ptr -> fx_media_12_bit_FAT) { ... } /* Check for a 16-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 /* FX_ENABLE_EXFAT */ { /* 16-bit FAT is present. */ /* Calculate the byte offset to the cluster entry. */ byte_offset = (((ULONG)cluster) * 2); /* Calculate the FAT sector the requested FAT entry resides in. */ FAT_sector = (byte_offset / media_ptr -> fx_media_bytes_per_sector) + (ULONG)media_ptr -> fx_media_reserved_sectors; /* Read the FAT sector. */ status = _fx_utility_logical_sector_read(media_ptr, (ULONG64) FAT_sector, media_ptr -> fx_media_memory_buffer, ((ULONG) 1), FX_FAT_SECTOR); /* Determine if an error occurred. */ if (status != FX_SUCCESS) { /* Return the error code. */ return(status); }if (status != FX_SUCCESS) { ... } /* Now calculate the byte offset into this FAT sector. */ byte_offset = byte_offset - ((FAT_sector - (ULONG)media_ptr -> fx_media_reserved_sectors) * media_ptr -> fx_media_bytes_per_sector); /* Setup a pointer into the buffer. */ FAT_ptr = (UCHAR *)media_ptr -> fx_media_memory_buffer + (UINT)byte_offset; /* Pickup the FAT entry. */ 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 /* FX_ENABLE_EXFAT */ { /* Otherwise, a 32 bit FAT present. */ byte_offset = (((ULONG)cluster) * 4); /* Calculate the FAT sector the requested FAT entry resides in. */ FAT_sector = (byte_offset / media_ptr -> fx_media_bytes_per_sector) + (ULONG)media_ptr -> fx_media_reserved_sectors; /* Calculate the byte offset to the FAT entry. */ byte_offset = (byte_offset % media_ptr -> fx_media_bytes_per_sector); /* Read the appropriate FAT sector. */ status = _fx_utility_logical_sector_read(media_ptr, (ULONG64) FAT_sector, media_ptr -> fx_media_memory_buffer, ((ULONG) 1), FX_FAT_SECTOR); /* Determine if an error occurred. */ if (status != FX_SUCCESS) { /* Return the error code. */ return(status); }if (status != FX_SUCCESS) { ... } /* Setup a pointer into the buffer. */ FAT_ptr = (UCHAR *)media_ptr -> fx_media_memory_buffer + (ULONG)byte_offset; /* Pickup the FAT entry. */ entry32 = _fx_utility_32_unsigned_read(FAT_ptr); #ifdef FX_ENABLE_EXFAT /* FAT32 uses 28 bit cluster addressing but exFAT uses 32 bit. */ if (media_ptr -> fx_media_FAT_type == FX_FAT32) { #endif /* FX_ENABLE_EXFAT */ /* Clear upper nibble. */ entry32 = entry32 & 0x0FFFFFFF; #ifdef FX_ENABLE_EXFAT }if (media_ptr -> fx_media_FAT_type == FX_FAT32) { ... } #endif /* FX_ENABLE_EXFAT */ *entry_ptr = entry32; }else { ... } /* Move all the cache entries down so the oldest is at the bottom. */ *(cache_entry_ptr + 3) = *(cache_entry_ptr + 2); *(cache_entry_ptr + 2) = *(cache_entry_ptr + 1); *(cache_entry_ptr + 1) = *(cache_entry_ptr); /* Setup the new FAT entry in the cache. */ cache_entry_ptr -> fx_fat_cache_entry_cluster = cluster; cache_entry_ptr -> fx_fat_cache_entry_value = *entry_ptr; /* Return success to the caller. */ return(FX_SUCCESS); }{ ... }