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
98
99
100
101
102
108
109
110
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
136
137
138
139
140
141
142
143
144
145
147
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
191
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
218
219
221
222
223
224
225
226
227
228
229
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
251
255
256
257
261
265
266
267
268
269
270
274
275
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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
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
359
360
361
362
363
364
373
374
377
378
379
380
381
382
383
384
385
386
390
391
392
393
394
396
397
398
399
400
401
402
403
404
405
406
407
408
412
416
417
418
419
420
421
423
424
425
426
427
428
429
430
437
438
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
...
...
...
#define FX_SOURCE_CODE
#include "fx_api.h"
#include "fx_system.h"
#include "fx_file.h"
#include "fx_utility.h"
...
...
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
if (file_ptr -> fx_file_id != FX_FILE_ID)
{
return(FX_NOT_OPEN);
}if (file_ptr -> fx_file_id != FX_FILE_ID) { ... }
#ifndef FX_MEDIA_STATISTICS_DISABLE
media_ptr = file_ptr -> fx_file_media_ptr;
media_ptr -> fx_media_file_reads++;/* ... */
#endif
media_ptr = file_ptr -> fx_file_media_ptr;
FX_TRACE_IN_LINE_INSERT(FX_TRACE_FILE_READ, file_ptr, buffer_ptr, request_size, 0, FX_TRACE_FILE_EVENTS, &trace_event, &trace_timestamp)
FX_PROTECT
if (file_ptr -> fx_file_current_file_offset >=
file_ptr -> fx_file_current_file_size)
{
FX_UNPROTECT
/* ... */
*actual_size = 0;
return(FX_END_OF_FILE);
}if (file_ptr -> fx_file_current_file_offset >= file_ptr -> fx_file_current_file_size) { ... }
destination_ptr = (UCHAR *)buffer_ptr;
/* ... */
if ((ULONG64)request_size >
(file_ptr -> fx_file_current_file_size - file_ptr -> fx_file_current_file_offset))
{
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)) { ... }
bytes_remaining = request_size;
while (bytes_remaining)
{
if ((file_ptr -> fx_file_current_logical_offset) ||
(bytes_remaining < media_ptr -> fx_media_bytes_per_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);
if (status != FX_SUCCESS)
{
FX_UNPROTECT
return(status);
}if (status != FX_SUCCESS) { ... }
copy_bytes = media_ptr -> fx_media_bytes_per_sector -
file_ptr -> fx_file_current_logical_offset;
/* ... */
if (copy_bytes > bytes_remaining)
{
copy_bytes = bytes_remaining;
}if (copy_bytes > bytes_remaining) { ... }
_fx_utility_memory_copy(((UCHAR *)media_ptr -> fx_media_memory_buffer) +
file_ptr -> fx_file_current_logical_offset,
destination_ptr, copy_bytes);
file_ptr -> fx_file_current_logical_offset =
file_ptr -> fx_file_current_logical_offset + copy_bytes;
bytes_remaining = bytes_remaining - copy_bytes;
destination_ptr = destination_ptr + copy_bytes;
}if ((file_ptr -> fx_file_current_logical_offset) || (bytes_remaining < media_ptr -> fx_media_bytes_per_sector)) { ... }
else
{
/* ... */
/* ... */
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
status = _fx_utility_FAT_entry_read(media_ptr, cluster, &next_cluster);
if ((status != FX_SUCCESS) || (next_cluster < FX_FAT_ENTRY_START) ||
(next_cluster > media_ptr -> fx_media_fat_reserved))
{
FX_UNPROTECT
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
}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) { ... }
/* ... */
if (sectors == 1)
{
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);
if (status != FX_SUCCESS)
{
FX_UNPROTECT
return(status);
}if (status != FX_SUCCESS) { ... }
_fx_utility_memory_copy((UCHAR *)media_ptr -> fx_media_memory_buffer, destination_ptr, media_ptr -> fx_media_bytes_per_sector);
}if (sectors == 1) { ... }
else
{
/* ... */
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;
if (status != FX_SUCCESS)
{
FX_UNPROTECT
return(status);
}if (status != FX_SUCCESS) { ... }
}else { ... }
/* ... */
file_ptr -> fx_file_current_logical_sector =
file_ptr -> fx_file_current_logical_sector +
(sectors - 1);
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;
file_ptr -> fx_file_current_logical_offset =
media_ptr -> fx_media_bytes_per_sector;
file_ptr -> fx_file_current_physical_cluster = cluster;
bytes_remaining = bytes_remaining -
(((ULONG)media_ptr -> fx_media_bytes_per_sector) * sectors);
destination_ptr = destination_ptr +
(((ULONG)media_ptr -> fx_media_bytes_per_sector) * sectors);
}else { ... }
/* ... */
if (file_ptr -> fx_file_current_logical_offset >=
media_ptr -> fx_media_bytes_per_sector)
{
if ((bytes_remaining == 0) && ((file_ptr -> fx_file_current_file_offset + (ULONG64)request_size) >=
file_ptr -> fx_file_current_available_size))
{
/* ... */
break;
}if ((bytes_remaining == 0) && ((file_ptr -> fx_file_current_file_offset + (ULONG64)request_size) >= file_ptr -> fx_file_current_available_size)) { ... }
/* ... */
file_ptr -> fx_file_current_relative_sector++;
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
/* ... */
status = _fx_utility_FAT_entry_read(media_ptr,
file_ptr -> fx_file_current_physical_cluster, &next_cluster);
if ((status != FX_SUCCESS) || (next_cluster < FX_FAT_ENTRY_START) ||
(next_cluster > media_ptr -> fx_media_fat_reserved))
{
FX_UNPROTECT
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
/* ... */
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
{
/* ... */
file_ptr -> fx_file_current_logical_sector++;
}else { ... }
/* ... */
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) { ... }
file_ptr -> fx_file_current_file_offset =
file_ptr -> fx_file_current_file_offset + (ULONG64)request_size;
*actual_size = request_size;
FX_TRACE_EVENT_UPDATE(trace_event, trace_timestamp, FX_TRACE_FILE_READ, 0, 0, 0, request_size)
file_ptr -> fx_file_dir_entry.fx_dir_entry_last_accessed_date = _fx_system_date;
FX_UNPROTECT
return(FX_SUCCESS);
}{ ... }