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
108
109
110
111
112
113
114
115
116
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
150
151
152
153
154
157
158
159
160
163
164
167
168
169
171
172
173
174
175
177
178
179
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
203
204
205
206
207
208
209
210
211
212
213
214
221
222
224
225
226
227
228
229
230
232
233
234
238
239
240
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
289
290
291
292
293
294
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
348
349
350
351
352
353
354
358
359
360
361
362
363
364
365
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
397
398
399
400
401
402
403
404
405
406
407
408
409
415
416
417
418
419
420
421
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
445
446
447
448
449
450
451
452
453
454
455
456
462
463
464
465
467
468
469
470
471
472
474
475
476
477
478
479
480
481
482
483
486
493
494
495
496
497
498
499
501
502
503
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
530
531
532
533
534
535
536
537
538
539
540
541
548
549
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
570
571
572
573
574
575
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
...
...
...
#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_logical_sector_read(FX_MEDIA *media_ptr, ULONG64 logical_sector,
VOID *buffer_ptr, ULONG sectors, UCHAR sector_type)
{
#ifndef FX_DISABLE_CACHE
FX_CACHED_SECTOR *cache_entry;
FX_CACHED_SECTOR *previous_cache_entry;
ULONG64 end_sector;/* ... */
#endif
#ifdef FX_ENABLE_FAULT_TOLERANT
UINT status;
#endif
#ifndef FX_MEDIA_STATISTICS_DISABLE
if (sector_type == FX_FAT_SECTOR)
{
media_ptr -> fx_media_fat_sector_reads++;
}if (sector_type == FX_FAT_SECTOR) { ... }
media_ptr -> fx_media_logical_sector_reads++;/* ... */
#endif
FX_UTILITY_LOGICAL_SECTOR_READ_EXTENSION
#ifndef FX_DISABLE_CACHE
if ((((UCHAR *)buffer_ptr) >= media_ptr -> fx_media_memory_buffer) &&
(((UCHAR *)buffer_ptr) <= media_ptr -> fx_media_sector_cache_end))
{
cache_entry = _fx_utility_logical_sector_cache_entry_read(media_ptr, logical_sector, &previous_cache_entry);
if (cache_entry == FX_NULL)
{
return(FX_SUCCESS);
}if (cache_entry == FX_NULL) { ... }
#ifndef FX_MEDIA_STATISTICS_DISABLE
media_ptr -> fx_media_logical_sector_cache_read_misses++;/* ... */
#endif
#ifndef FX_MEDIA_STATISTICS_DISABLE
FX_TRACE_IN_LINE_INSERT(FX_TRACE_INTERNAL_LOG_SECTOR_CACHE_MISS, media_ptr, logical_sector, media_ptr -> fx_media_logical_sector_cache_read_misses, media_ptr -> fx_media_sector_cache_size, FX_TRACE_INTERNAL_EVENTS, 0, 0)/* ... */
#else
FX_TRACE_IN_LINE_INSERT(FX_TRACE_INTERNAL_LOG_SECTOR_CACHE_MISS, media_ptr, logical_sector, 0, media_ptr -> fx_media_sector_cache_size, FX_TRACE_INTERNAL_EVENTS, 0, 0)/* ... */
#endif
/* ... */
if ((cache_entry -> fx_cached_sector_valid) &&
(cache_entry -> fx_cached_sector_buffer_dirty))
{
/* ... */
#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 = cache_entry -> fx_cached_sector_memory_buffer;
#ifdef FX_DRIVER_USE_64BIT_LBA
media_ptr -> fx_media_driver_logical_sector = cache_entry -> fx_cached_sector;
#else
media_ptr -> fx_media_driver_logical_sector = (ULONG)cache_entry -> fx_cached_sector;
#endif
media_ptr -> fx_media_driver_sectors = 1;
media_ptr -> fx_media_driver_sector_type = cache_entry -> fx_cached_sector_type;
if (cache_entry -> fx_cached_sector_type != FX_DATA_SECTOR)
{
media_ptr -> fx_media_driver_system_write = FX_TRUE;
}if (cache_entry -> fx_cached_sector_type != FX_DATA_SECTOR) { ... }
FX_TRACE_IN_LINE_INSERT(FX_TRACE_INTERNAL_IO_DRIVER_WRITE, media_ptr, cache_entry -> fx_cached_sector, 1, cache_entry -> fx_cached_sector_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)
{
/* ... */
return(media_ptr -> fx_media_driver_status);
}if (media_ptr -> fx_media_driver_status) { ... }
/* ... */
cache_entry -> fx_cached_sector_buffer_dirty = FX_FALSE;
media_ptr -> fx_media_sector_cache_dirty_count--;
}if ((cache_entry -> fx_cached_sector_valid) && (cache_entry -> fx_cached_sector_buffer_dirty)) { ... }
/* ... */
if (logical_sector >= media_ptr -> fx_media_total_sectors)
{
return(FX_SECTOR_INVALID);
}if (logical_sector >= media_ptr -> fx_media_total_sectors) { ... }
#ifndef FX_MEDIA_STATISTICS_DISABLE
media_ptr -> fx_media_driver_read_requests++;/* ... */
#endif
media_ptr -> fx_media_driver_request = FX_DRIVER_READ;
media_ptr -> fx_media_driver_status = FX_IO_ERROR;
media_ptr -> fx_media_driver_buffer = cache_entry -> fx_cached_sector_memory_buffer;
#ifdef FX_DRIVER_USE_64BIT_LBA
media_ptr -> fx_media_driver_logical_sector = logical_sector;
#else
media_ptr -> fx_media_driver_logical_sector = (ULONG)logical_sector;
#endif
media_ptr -> fx_media_driver_sectors = 1;
media_ptr -> fx_media_driver_sector_type = sector_type;
if (sector_type == FX_DATA_SECTOR)
{
media_ptr -> fx_media_driver_data_sector_read = FX_TRUE;
}if (sector_type == FX_DATA_SECTOR) { ... }
FX_TRACE_IN_LINE_INSERT(FX_TRACE_INTERNAL_IO_DRIVER_READ, media_ptr, logical_sector, 1, cache_entry -> fx_cached_sector_memory_buffer, FX_TRACE_INTERNAL_EVENTS, 0, 0)
(media_ptr -> fx_media_driver_entry) (media_ptr);
media_ptr -> fx_media_driver_data_sector_read = FX_FALSE;
if (media_ptr -> fx_media_driver_status == FX_SUCCESS)
{
cache_entry -> fx_cached_sector = logical_sector;
cache_entry -> fx_cached_sector_valid = FX_TRUE;
cache_entry -> fx_cached_sector_type = sector_type;
/* ... */
if (previous_cache_entry)
{
/* ... */
previous_cache_entry -> fx_cached_sector_next_used =
cache_entry -> fx_cached_sector_next_used;
cache_entry -> fx_cached_sector_next_used =
media_ptr -> fx_media_sector_cache_list_ptr;
media_ptr -> fx_media_sector_cache_list_ptr = cache_entry;
}if (previous_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) &&
(sector_type == FX_DIRECTORY_SECTOR))
{
status = _fx_fault_tolerant_read_directory_sector(media_ptr, logical_sector, cache_entry -> fx_cached_sector_memory_buffer, 1);
if (status)
{
return(status);
}if (status) { ... }
}if (media_ptr -> fx_media_fault_tolerant_enabled && (media_ptr -> fx_media_fault_tolerant_state & FX_FAULT_TOLERANT_STATE_STARTED) && (sector_type == FX_DIRECTORY_SECTOR)) { ... }
/* ... */#endif
}if (media_ptr -> fx_media_driver_status == FX_SUCCESS) { ... }
else
{
cache_entry -> fx_cached_sector_valid = FX_FALSE;
cache_entry -> fx_cached_sector = (~(ULONG64)0);
}else { ... }
media_ptr -> fx_media_memory_buffer = cache_entry -> fx_cached_sector_memory_buffer;
return(media_ptr -> fx_media_driver_status);
}if ((((UCHAR *)buffer_ptr) >= media_ptr -> fx_media_memory_buffer) && (((UCHAR *)buffer_ptr) <= media_ptr -> fx_media_sector_cache_end)) { ... }
/* ... */#else
if ((logical_sector == media_ptr -> fx_media_memory_buffer_sector) && (sectors == 1) && (buffer_ptr == media_ptr -> fx_media_memory_buffer))
{
return(FX_SUCCESS);
}if ((logical_sector == media_ptr -> fx_media_memory_buffer_sector) && (sectors == 1) && (buffer_ptr == media_ptr -> fx_media_memory_buffer)) { ... }
/* ... */#endif
else
{
if ((logical_sector + sectors - 1) > (ULONG)media_ptr -> fx_media_total_sectors)
{
return(FX_SECTOR_INVALID);
}if ((logical_sector + sectors - 1) > (ULONG)media_ptr -> fx_media_total_sectors) { ... }
#ifndef FX_DISABLE_CACHE
while (sectors)
{
if (_fx_utility_logical_sector_cache_entry_read(media_ptr, logical_sector, &previous_cache_entry))
{
break;
}if (_fx_utility_logical_sector_cache_entry_read(media_ptr, logical_sector, &previous_cache_entry)) { ... }
_fx_utility_memory_copy(media_ptr -> fx_media_memory_buffer, buffer_ptr, media_ptr -> fx_media_bytes_per_sector);
buffer_ptr = ((UCHAR *)buffer_ptr) + media_ptr -> fx_media_bytes_per_sector;
logical_sector++;
sectors--;
}while (sectors) { ... }
end_sector = logical_sector + sectors - 1;
while (sectors)
{
if (_fx_utility_logical_sector_cache_entry_read(media_ptr, end_sector, &previous_cache_entry))
{
break;
}if (_fx_utility_logical_sector_cache_entry_read(media_ptr, end_sector, &previous_cache_entry)) { ... }
_fx_utility_memory_copy(media_ptr -> fx_media_memory_buffer,
((UCHAR *)buffer_ptr) + ((sectors - 1) * media_ptr -> fx_media_bytes_per_sector),
media_ptr -> fx_media_bytes_per_sector);
end_sector--;
sectors--;
}while (sectors) { ... }
if (sectors == 0)
{
return(FX_SUCCESS);
}if (sectors == 0) { ... }
_fx_utility_logical_sector_flush(media_ptr, logical_sector, (ULONG64) sectors, FX_TRUE);/* ... */
#endif
#ifndef FX_MEDIA_STATISTICS_DISABLE
media_ptr -> fx_media_driver_read_requests++;/* ... */
#endif
media_ptr -> fx_media_driver_request = FX_DRIVER_READ;
media_ptr -> fx_media_driver_status = FX_IO_ERROR;
media_ptr -> fx_media_driver_buffer = buffer_ptr;
#ifdef FX_DRIVER_USE_64BIT_LBA
media_ptr -> fx_media_driver_logical_sector = logical_sector;
#else
media_ptr -> fx_media_driver_logical_sector = (ULONG)logical_sector;
#endif
media_ptr -> fx_media_driver_sectors = sectors;
media_ptr -> fx_media_driver_sector_type = sector_type;
if (sector_type == FX_DATA_SECTOR)
{
media_ptr -> fx_media_driver_data_sector_read = FX_TRUE;
}if (sector_type == FX_DATA_SECTOR) { ... }
FX_TRACE_IN_LINE_INSERT(FX_TRACE_INTERNAL_IO_DRIVER_READ, media_ptr, logical_sector, sectors, buffer_ptr, FX_TRACE_INTERNAL_EVENTS, 0, 0)
(media_ptr -> fx_media_driver_entry) (media_ptr);
media_ptr -> fx_media_driver_data_sector_read = FX_FALSE;
#ifdef FX_DISABLE_CACHE
if ((media_ptr -> fx_media_driver_status == FX_SUCCESS) && (sectors == 1) && (buffer_ptr == media_ptr -> fx_media_memory_buffer))
{
media_ptr -> fx_media_memory_buffer_sector = logical_sector;
return(FX_SUCCESS);
}if ((media_ptr -> fx_media_driver_status == FX_SUCCESS) && (sectors == 1) && (buffer_ptr == media_ptr -> fx_media_memory_buffer)) { ... }
/* ... */#endif
#ifndef FX_DISABLE_DIRECT_DATA_READ_CACHE_FILL
/* ... */
if ((media_ptr -> fx_media_driver_status == FX_SUCCESS) && (sectors < (media_ptr -> fx_media_sector_cache_size / 4)))
{
/* ... */
while (sectors)
{
cache_entry = _fx_utility_logical_sector_cache_entry_read(media_ptr, logical_sector, &previous_cache_entry);
FX_UTILITY_LOGICAL_SECTOR_READ_EXTENSION_1
/* ... */
if (cache_entry == FX_NULL)
{
/* ... */
return(FX_SUCCESS);
}if (cache_entry == FX_NULL) { ... }
if ((cache_entry -> fx_cached_sector_valid) &&
(cache_entry -> fx_cached_sector_buffer_dirty))
{
/* ... */
#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 = cache_entry -> fx_cached_sector_memory_buffer;
#ifdef FX_DRIVER_USE_64BIT_LBA
media_ptr -> fx_media_driver_logical_sector = cache_entry -> fx_cached_sector;
#else
media_ptr -> fx_media_driver_logical_sector = (ULONG)cache_entry -> fx_cached_sector;
#endif
media_ptr -> fx_media_driver_sectors = 1;
media_ptr -> fx_media_driver_sector_type = cache_entry -> fx_cached_sector_type;
#ifndef FX_FAULT_TOLERANT
if (cache_entry -> fx_cached_sector_type != FX_DATA_SECTOR)
{
media_ptr -> fx_media_driver_system_write = FX_TRUE;
}if (cache_entry -> fx_cached_sector_type != FX_DATA_SECTOR) { ... }
/* ... */#endif
FX_TRACE_IN_LINE_INSERT(FX_TRACE_INTERNAL_IO_DRIVER_WRITE, media_ptr, cache_entry -> fx_cached_sector, 1, cache_entry -> fx_cached_sector_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)
{
/* ... */
return(media_ptr -> fx_media_driver_status);
}if (media_ptr -> fx_media_driver_status) { ... }
/* ... */
cache_entry -> fx_cached_sector_buffer_dirty = FX_FALSE;
media_ptr -> fx_media_sector_cache_dirty_count--;
}if ((cache_entry -> fx_cached_sector_valid) && (cache_entry -> fx_cached_sector_buffer_dirty)) { ... }
cache_entry -> fx_cached_sector = logical_sector;
cache_entry -> fx_cached_sector_valid = FX_TRUE;
cache_entry -> fx_cached_sector_type = sector_type;
/* ... */
if (previous_cache_entry)
{
/* ... */
previous_cache_entry -> fx_cached_sector_next_used =
cache_entry -> fx_cached_sector_next_used;
cache_entry -> fx_cached_sector_next_used =
media_ptr -> fx_media_sector_cache_list_ptr;
media_ptr -> fx_media_sector_cache_list_ptr = cache_entry;
}if (previous_cache_entry) { ... }
_fx_utility_memory_copy(buffer_ptr,
cache_entry -> fx_cached_sector_memory_buffer,
media_ptr -> fx_media_bytes_per_sector);
buffer_ptr = ((UCHAR *)buffer_ptr) + media_ptr -> fx_media_bytes_per_sector;
logical_sector++;
sectors--;
}while (sectors) { ... }
}if ((media_ptr -> fx_media_driver_status == FX_SUCCESS) && (sectors < (media_ptr -> fx_media_sector_cache_size / 4))) { ... }
/* ... */#endif
return(media_ptr -> fx_media_driver_status);
}else { ... }
}{ ... }