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_file.h"
#include "fx_utility.h"
...
...
_fx_file_read(FX_FILE *, void *, ULONG, ULONG *)
Files
loading...
CodeScopeSTM32 Libraries and Samplesfilexcommon/src/fx_file_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
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
/**************************************************************************/ /* */ /* 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 */ /** */ /** File */ /** */... /**************************************************************************/ /**************************************************************************/ #define FX_SOURCE_CODE /* Include necessary system files. */ #include "fx_api.h" #include "fx_system.h" #include "fx_file.h" #include "fx_utility.h" ... /**************************************************************************/ /* */ /* FUNCTION RELEASE */ /* */ /* _fx_file_read PORTABLE C */ /* 6.1 */ /* AUTHOR */ /* */ /* William E. Lamie, Microsoft Corporation */ /* */ /* DESCRIPTION */ /* */ /* This function reads the specified number of bytes (or as many as */ /* possible into the buffer supplied by the caller. The actual number */ /* of bytes and the status of the read operation is returned to the */ /* caller. In addition, various internal file pointers in the file */ /* control block are also updated. */ /* */ /* INPUT */ /* */ /* file_ptr File control block pointer */ /* buffer_ptr Buffer pointer */ /* request_size Number of bytes requested */ /* actual_size Pointer to variable for the */ /* number of bytes read */ /* */ /* OUTPUT */ /* */ /* return status */ /* */ /* CALLS */ /* */ /* _fx_utility_FAT_entry_read Read a FAT entry */ /* _fx_utility_logical_sector_read Read a logical sector */ /* _fx_utility_memory_copy Fast memory copy routine */ /* */ /* 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), verified */ /* memcpy usage, */ /* resulting in version 6.1 */ /* */... /**************************************************************************/ UINT _fx_file_read(FX_FILE *file_ptr, VOID *buffer_ptr, ULONG request_size, ULONG *actual_size) { UINT status; ULONG bytes_remaining, i; ULONG copy_bytes; UCHAR *destination_ptr; ULONG cluster, next_cluster; UINT sectors; FX_MEDIA *media_ptr; #ifdef TX_ENABLE_EVENT_TRACE TX_TRACE_BUFFER_ENTRY *trace_event; ULONG trace_timestamp;/* ... */ #endif /* First, determine if the file is still open. */ if (file_ptr -> fx_file_id != FX_FILE_ID) { /* Return the file not open error status. */ return(FX_NOT_OPEN); }if (file_ptr -> fx_file_id != FX_FILE_ID) { ... } #ifndef FX_MEDIA_STATISTICS_DISABLE /* Setup pointer to media structure. */ media_ptr = file_ptr -> fx_file_media_ptr; /* Increment the number of times this service has been called. */ media_ptr -> fx_media_file_reads++;/* ... */ #endif /* Setup pointer to associated media control block. */ media_ptr = file_ptr -> fx_file_media_ptr; /* If trace is enabled, insert this event into the trace buffer. */ FX_TRACE_IN_LINE_INSERT(FX_TRACE_FILE_READ, file_ptr, buffer_ptr, request_size, 0, FX_TRACE_FILE_EVENTS, &trace_event, &trace_timestamp) /* Protect against other threads accessing the media. */ FX_PROTECT /* Next, determine if there is any more bytes to read in the file. */ if (file_ptr -> fx_file_current_file_offset >= file_ptr -> fx_file_current_file_size) { /* Release media protection. */ FX_UNPROTECT /* The file is at the end, return the proper status and set the actual size to 0. *//* ... */ *actual_size = 0; return(FX_END_OF_FILE); }if (file_ptr -> fx_file_current_file_offset >= file_ptr -> fx_file_current_file_size) { ... } /* At this point there is something to read. */ /* Setup local buffer pointer. */ destination_ptr = (UCHAR *)buffer_ptr; /* Determine if there are less bytes left in the file than that specified by the request. If so, adjust the requested size. *//* ... */ if ((ULONG64)request_size > (file_ptr -> fx_file_current_file_size - file_ptr -> fx_file_current_file_offset)) { /* Adjust the bytes remaining to what's available. */ request_size = (ULONG)(file_ptr -> fx_file_current_file_size - file_ptr -> fx_file_current_file_offset); }if ((ULONG64)request_size > (file_ptr -> fx_file_current_file_size - file_ptr -> fx_file_current_file_offset)) { ... } /* Setup the remaining number of bytes to read. */ bytes_remaining = request_size; /* Loop to read all of the bytes. */ while (bytes_remaining) { /* Determine if a beginning or ending partial read is required. */ if ((file_ptr -> fx_file_current_logical_offset) || (bytes_remaining < media_ptr -> fx_media_bytes_per_sector)) { /* A partial sector read is required. */ /* Read the current logical sector. */ status = _fx_utility_logical_sector_read(media_ptr, file_ptr -> fx_file_current_logical_sector, media_ptr -> fx_media_memory_buffer, ((ULONG) 1), FX_DATA_SECTOR); /* Check for good completion status. */ if (status != FX_SUCCESS) { /* Release media protection. */ FX_UNPROTECT /* Return the error status. */ return(status); }if (status != FX_SUCCESS) { ... } /* Copy the appropriate number of bytes into the destination buffer. */ copy_bytes = media_ptr -> fx_media_bytes_per_sector - file_ptr -> fx_file_current_logical_offset; /* Check to see if only a portion of the read sector needs to be copied. *//* ... */ if (copy_bytes > bytes_remaining) { /* Adjust the number of bytes to copy. */ copy_bytes = bytes_remaining; }if (copy_bytes > bytes_remaining) { ... } /* Actually perform the memory copy. */ _fx_utility_memory_copy(((UCHAR *)media_ptr -> fx_media_memory_buffer) + /* Use case of memcpy is verified. */ file_ptr -> fx_file_current_logical_offset, destination_ptr, copy_bytes); /* Increment the logical sector byte offset. */ file_ptr -> fx_file_current_logical_offset = file_ptr -> fx_file_current_logical_offset + copy_bytes; /* Adjust the remaining bytes to read. */ bytes_remaining = bytes_remaining - copy_bytes; /* Adjust the pointer to the destination buffer. */ destination_ptr = destination_ptr + copy_bytes; }if ((file_ptr -> fx_file_current_logical_offset) || (bytes_remaining < media_ptr -> fx_media_bytes_per_sector)) { ... } else { /* Attempt to read multiple sectors directly into the destination buffer. *//* ... */ /* Calculate the number of whole sectors to read directly into the destination buffer. *//* ... */ sectors = (UINT)(bytes_remaining / media_ptr -> fx_media_bytes_per_sector); next_cluster = cluster = file_ptr -> fx_file_current_physical_cluster; for (i = (media_ptr -> fx_media_sectors_per_cluster - file_ptr -> fx_file_current_relative_sector); i < sectors; i += media_ptr -> fx_media_sectors_per_cluster) { #ifdef FX_ENABLE_EXFAT if (file_ptr -> fx_file_dir_entry.fx_dir_entry_dont_use_fat & 1) { cluster++; }if (file_ptr -> fx_file_dir_entry.fx_dir_entry_dont_use_fat & 1) { ... } else { #endif /* FX_ENABLE_EXFAT */ status = _fx_utility_FAT_entry_read(media_ptr, cluster, &next_cluster); /* Determine if an error is present. */ if ((status != FX_SUCCESS) || (next_cluster < FX_FAT_ENTRY_START) || (next_cluster > media_ptr -> fx_media_fat_reserved)) { /* Release media protection. */ FX_UNPROTECT /* Send error message back to caller. */ if (status != FX_SUCCESS) { return(status); }if (status != FX_SUCCESS) { ... } else { return(FX_FILE_CORRUPT); }else { ... } }if ((status != FX_SUCCESS) || (next_cluster < FX_FAT_ENTRY_START) || (next_cluster > media_ptr -> fx_media_fat_reserved)) { ... } if (next_cluster != cluster + 1) { break; }if (next_cluster != cluster + 1) { ... } else { cluster = next_cluster; }else { ... } #ifdef FX_ENABLE_EXFAT }else { ... } #endif /* FX_ENABLE_EXFAT */ }for (i = (media_ptr -> fx_media_sectors_per_cluster - file_ptr -> fx_file_current_relative_sector); i < sectors; i += media_ptr -> fx_media_sectors_per_cluster) { ... } if (i < sectors) { sectors = i; }if (i < sectors) { ... } /* Determine if this is a single sector read request. If so, read the sector so it will come from the internal cache. *//* ... */ if (sectors == 1) { /* Read the current logical sector. */ status = _fx_utility_logical_sector_read(media_ptr, file_ptr -> fx_file_current_logical_sector, media_ptr -> fx_media_memory_buffer, ((ULONG) 1), FX_DATA_SECTOR); /* Check for good completion status. */ if (status != FX_SUCCESS) { /* Release media protection. */ FX_UNPROTECT /* Return the error status. */ return(status); }if (status != FX_SUCCESS) { ... } /* Actually perform the memory copy. */ _fx_utility_memory_copy((UCHAR *)media_ptr -> fx_media_memory_buffer, destination_ptr, media_ptr -> fx_media_bytes_per_sector); /* Use case of memcpy is verified. */ }if (sectors == 1) { ... } else { /* Multiple sector read request. Read all the sectors at once. */ /* Perform the data read directly into the user's buffer of the appropriate number of sectors. *//* ... */ media_ptr -> fx_media_disable_burst_cache = file_ptr -> fx_file_disable_burst_cache; status = _fx_utility_logical_sector_read(media_ptr, file_ptr -> fx_file_current_logical_sector, destination_ptr, (ULONG) sectors, FX_DATA_SECTOR); media_ptr -> fx_media_disable_burst_cache = FX_FALSE; /* Check for good completion status. */ if (status != FX_SUCCESS) { /* Release media protection. */ FX_UNPROTECT /* Return the error status. */ return(status); }if (status != FX_SUCCESS) { ... } }else { ... } /* Now adjust the various file pointers. */ /* Increment the current logical sector. Subtract one from the sector count because we are going to use the logical offset to do additional sector/cluster arithmetic below. *//* ... */ file_ptr -> fx_file_current_logical_sector = file_ptr -> fx_file_current_logical_sector + (sectors - 1); /* Move the relative sector and cluster as well. */ file_ptr -> fx_file_current_relative_cluster = file_ptr -> fx_file_current_relative_cluster + (file_ptr -> fx_file_current_relative_sector + (sectors - 1)) / media_ptr -> fx_media_sectors_per_cluster; file_ptr -> fx_file_current_relative_sector = (file_ptr -> fx_file_current_relative_sector + (sectors - 1)) % media_ptr -> fx_media_sectors_per_cluster; /* Increment the logical sector byte offset. */ file_ptr -> fx_file_current_logical_offset = media_ptr -> fx_media_bytes_per_sector; file_ptr -> fx_file_current_physical_cluster = cluster; /* Adjust the remaining bytes. */ bytes_remaining = bytes_remaining - (((ULONG)media_ptr -> fx_media_bytes_per_sector) * sectors); /* Adjust the pointer to the destination buffer. */ destination_ptr = destination_ptr + (((ULONG)media_ptr -> fx_media_bytes_per_sector) * sectors); }else { ... } /* At this point, we have either read a partial sector or have successfully read one or more whole sectors. Determine if we are at the end of the current logical sector. *//* ... */ if (file_ptr -> fx_file_current_logical_offset >= media_ptr -> fx_media_bytes_per_sector) { /* Determine if we are at the exact physical end of the file at the end of reading. */ if ((bytes_remaining == 0) && ((file_ptr -> fx_file_current_file_offset + (ULONG64)request_size) >= file_ptr -> fx_file_current_available_size)) { /* Skip the following file parameter adjustments. The next write will detect the logical offset out of the range of the sector and reset all of the pertinent information. *//* ... */ break; }if ((bytes_remaining == 0) && ((file_ptr -> fx_file_current_file_offset + (ULONG64)request_size) >= file_ptr -> fx_file_current_available_size)) { ... } /* We need to move to the next logical sector, but first determine if the next logical sector is within the same cluster. *//* ... */ /* Increment the current relative sector in the cluster. */ file_ptr -> fx_file_current_relative_sector++; /* Determine if this is in a new cluster. */ if (file_ptr -> fx_file_current_relative_sector >= media_ptr -> fx_media_sectors_per_cluster) { #ifdef FX_ENABLE_EXFAT if (file_ptr -> fx_file_dir_entry.fx_dir_entry_dont_use_fat & 1) { next_cluster = file_ptr -> fx_file_current_physical_cluster + 1; }if (file_ptr -> fx_file_dir_entry.fx_dir_entry_dont_use_fat & 1) { ... } else { #endif /* FX_ENABLE_EXFAT */ /* Read the FAT entry of the current cluster to find the next cluster. *//* ... */ status = _fx_utility_FAT_entry_read(media_ptr, file_ptr -> fx_file_current_physical_cluster, &next_cluster); /* Determine if an error is present. */ if ((status != FX_SUCCESS) || (next_cluster < FX_FAT_ENTRY_START) || (next_cluster > media_ptr -> fx_media_fat_reserved)) { /* Release media protection. */ FX_UNPROTECT /* Send error message back to caller. */ if (status != FX_SUCCESS) { return(status); }if (status != FX_SUCCESS) { ... } else { return(FX_FILE_CORRUPT); }else { ... } }if ((status != FX_SUCCESS) || (next_cluster < FX_FAT_ENTRY_START) || (next_cluster > media_ptr -> fx_media_fat_reserved)) { ... } #ifdef FX_ENABLE_EXFAT }else { ... } #endif /* FX_ENABLE_EXFAT */ /* Otherwise, we have a new cluster. Save it in the file control block and calculate a new logical sector value. *//* ... */ file_ptr -> fx_file_current_physical_cluster = next_cluster; file_ptr -> fx_file_current_relative_cluster++; file_ptr -> fx_file_current_logical_sector = ((ULONG)media_ptr -> fx_media_data_sector_start) + ((((ULONG64)next_cluster) - FX_FAT_ENTRY_START) * ((ULONG)media_ptr -> fx_media_sectors_per_cluster)); file_ptr -> fx_file_current_relative_sector = 0; }if (file_ptr -> fx_file_current_relative_sector >= media_ptr -> fx_media_sectors_per_cluster) { ... } else { /* Still within the same cluster so just increment the logical sector. *//* ... */ file_ptr -> fx_file_current_logical_sector++; }else { ... } /* In either case, we are now positioned at a new sector so clear the logical sector offset. *//* ... */ file_ptr -> fx_file_current_logical_offset = 0; }if (file_ptr -> fx_file_current_logical_offset >= media_ptr -> fx_media_bytes_per_sector) { ... } }while (bytes_remaining) { ... } /* Adjust the current file offset accordingly. */ file_ptr -> fx_file_current_file_offset = file_ptr -> fx_file_current_file_offset + (ULONG64)request_size; /* Store the number of bytes actually read. */ *actual_size = request_size; /* Update the trace event with the bytes read. */ FX_TRACE_EVENT_UPDATE(trace_event, trace_timestamp, FX_TRACE_FILE_READ, 0, 0, 0, request_size) /* Update the last accessed date. */ file_ptr -> fx_file_dir_entry.fx_dir_entry_last_accessed_date = _fx_system_date; /* Release media protection. */ FX_UNPROTECT /* Return a successful status to the caller. */ return(FX_SUCCESS); }{ ... }