1
10
13
14
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
38
39
40
41
42
45
46
47
48
49
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
182
183
184
185
186
187
188
189
190
198
199
200
201
202
203
204
205
206
207
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
239
242
243
244
245
247
248
249
250
254
258
259
263
264
265
266
267
268
269
270
271
272
273
275
276
277
278
279
283
284
285
286
287
288
289
290
291
292
293
295
296
297
298
299
300
301
302
303
304
310
311
312
313
314
315
316
317
318
319
320
321
322
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
397
398
399
405
411
412
413
414
415
416
417
423
424
425
426
427
428
429
430
431
432
433
440
441
442
443
444
445
449
450
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
476
477
478
479
480
482
483
484
485
486
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
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
606
607
608
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
638
639
640
641
642
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
...
...
...
#define FX_SOURCE_CODE
#include "fx_api.h"
#include "fx_media.h"
#include "fx_utility.h"
/* ... */
UCHAR _fx_media_format_oem_name[8] = "EL FILEX";
/* ... */
UCHAR _fx_media_format_media_type = 0xF8;
/* ... */
ULONG _fx_media_format_volume_id = 1;
...
...
UINT _fx_media_format(FX_MEDIA *media_ptr, VOID (*driver)(FX_MEDIA *media), VOID *driver_info_ptr, UCHAR *memory_ptr, UINT memory_size,
CHAR *volume_name, UINT number_of_fats, UINT directory_entries, UINT hidden_sectors,
ULONG total_sectors, UINT bytes_per_sector, UINT sectors_per_cluster,
UINT heads, UINT sectors_per_track)
{
UCHAR *byte_ptr;
UINT reserved_sectors, i, j, root_sectors, total_clusters, bytes_needed;
UINT sectors_per_fat, f, s;
FX_TRACE_IN_LINE_INSERT(FX_TRACE_MEDIA_FORMAT, media_ptr, directory_entries, total_sectors, sectors_per_cluster, FX_TRACE_MEDIA_EVENTS, 0, 0)
if((bytes_per_sector == 0) || (bytes_per_sector > 4096))
return(FX_SECTOR_INVALID);
if((sectors_per_cluster == 0) || (sectors_per_cluster > 128))
return(FX_SECTOR_INVALID);
media_ptr -> fx_media_driver_entry = driver;
media_ptr -> fx_media_memory_buffer = (UCHAR *)memory_ptr;
media_ptr -> fx_media_memory_size = memory_size;
media_ptr -> fx_media_bytes_per_sector = bytes_per_sector;
media_ptr -> fx_media_sectors_per_track = sectors_per_track;
media_ptr -> fx_media_heads = heads;
media_ptr -> fx_media_hidden_sectors = hidden_sectors;
/* ... */
media_ptr -> fx_media_driver_request = FX_DRIVER_INIT;
media_ptr -> fx_media_driver_status = FX_IO_ERROR;
media_ptr -> fx_media_driver_info = driver_info_ptr;
media_ptr -> fx_media_driver_write_protect = FX_FALSE;
media_ptr -> fx_media_driver_free_sector_update = FX_FALSE;
media_ptr -> fx_media_driver_data_sector_read = FX_FALSE;
FX_TRACE_IN_LINE_INSERT(FX_TRACE_INTERNAL_IO_DRIVER_INIT, media_ptr, 0, 0, 0, FX_TRACE_INTERNAL_EVENTS, 0, 0)
(media_ptr -> fx_media_driver_entry) (media_ptr);
if (media_ptr -> fx_media_driver_status != FX_SUCCESS)
{
return(FX_IO_ERROR);
}if (media_ptr -> fx_media_driver_status != FX_SUCCESS) { ... }
media_ptr -> fx_media_driver_buffer = memory_ptr;
byte_ptr = media_ptr -> fx_media_driver_buffer;
#ifndef FX_DISABLE_FORCE_MEMORY_OPERATION
for (i = 0; i < bytes_per_sector; i++)
{
byte_ptr[i] = (UCHAR)0;
}for (i = 0; i < bytes_per_sector; i++) { ... }
/* ... */#else
_fx_utility_memory_set(byte_ptr, 0, bytes_per_sector);
#endif
byte_ptr[0] = (UCHAR)0xEB;
byte_ptr[1] = (UCHAR)0x34;
byte_ptr[2] = (UCHAR)0x90;
for (i = 0; i < 8; i++)
{
byte_ptr[i + 3] = _fx_media_format_oem_name[i];
}for (i = 0; i < 8; i++) { ... }
byte_ptr[FX_MEDIA_TYPE] = _fx_media_format_media_type;
_fx_utility_16_unsigned_write(&byte_ptr[FX_BYTES_SECTOR], bytes_per_sector);
_fx_utility_16_unsigned_write(&byte_ptr[FX_SECTORS_PER_TRK], sectors_per_track);
_fx_utility_16_unsigned_write(&byte_ptr[FX_HEADS], heads);
#ifdef FX_FORCE_512_BYTE_BOOT_SECTOR
/* ... */
if (bytes_per_sector < 512)
{
reserved_sectors = 512 / bytes_per_sector;
}if (bytes_per_sector < 512) { ... }
else
{
reserved_sectors = 1;
}else { ... }
/* ... */#else
reserved_sectors = 1;/* ... */
#endif
/* ... */
total_clusters = (total_sectors - reserved_sectors - ((directory_entries * FX_DIR_ENTRY_SIZE) + (bytes_per_sector - 1)) / bytes_per_sector) / sectors_per_cluster;
if (total_clusters % 2)
{
bytes_needed = (total_clusters + total_clusters / 2) + 1;
}if (total_clusters % 2) { ... }
else
{
bytes_needed = (total_clusters + total_clusters / 2);
}else { ... }
sectors_per_fat = bytes_needed / bytes_per_sector;
if (bytes_needed % bytes_per_sector)
{
sectors_per_fat++;
}if (bytes_needed % bytes_per_sector) { ... }
total_clusters = total_clusters - ((sectors_per_fat * number_of_fats) + (sectors_per_cluster - 1)) / sectors_per_cluster;
if (total_clusters >= FX_12_BIT_FAT_SIZE)
{
/* ... */
total_clusters = (total_sectors - reserved_sectors - ((directory_entries * FX_DIR_ENTRY_SIZE) + (bytes_per_sector - 1)) / bytes_per_sector) / sectors_per_cluster;
sectors_per_fat = (total_clusters * 2) / bytes_per_sector;
if ((total_clusters * 2) % bytes_per_sector)
{
sectors_per_fat++;
}if ((total_clusters * 2) % bytes_per_sector) { ... }
total_clusters = total_clusters - ((sectors_per_fat * number_of_fats) + (sectors_per_cluster - 1)) / sectors_per_cluster;
if (total_clusters >= FX_16_BIT_FAT_SIZE)
{
/* ... */
if (bytes_per_sector == 512)
{
_fx_utility_16_unsigned_write(&byte_ptr[48], 1);
reserved_sectors++;
}if (bytes_per_sector == 512) { ... }
else
{
_fx_utility_16_unsigned_write(&byte_ptr[48], 0xFFFF);
}else { ... }
_fx_utility_32_unsigned_write(&byte_ptr[FX_ROOT_CLUSTER_32], FX_FAT_ENTRY_START);
directory_entries = (sectors_per_cluster * bytes_per_sector) / FX_DIR_ENTRY_SIZE;
total_clusters = (total_sectors - reserved_sectors) / sectors_per_cluster;
sectors_per_fat = (total_clusters * 4) / bytes_per_sector;
if ((total_clusters * 4) % bytes_per_sector)
{
sectors_per_fat++;
}if ((total_clusters * 4) % bytes_per_sector) { ... }
total_clusters = total_clusters - ((sectors_per_fat * number_of_fats) + (sectors_per_cluster - 1)) / sectors_per_cluster;
}if (total_clusters >= FX_16_BIT_FAT_SIZE) { ... }
}if (total_clusters >= FX_12_BIT_FAT_SIZE) { ... }
if (total_clusters < FX_16_BIT_FAT_SIZE)
{
_fx_utility_16_unsigned_write(&byte_ptr[FX_SECTORS_PER_FAT], sectors_per_fat);
byte_ptr[FX_BOOT_SIG] = 0x29;
_fx_utility_32_unsigned_write(&byte_ptr[FX_VOLUME_ID], _fx_media_format_volume_id);
}if (total_clusters < FX_16_BIT_FAT_SIZE) { ... }
else
{
_fx_utility_32_unsigned_write(&byte_ptr[FX_SECTORS_PER_FAT_32], sectors_per_fat);
byte_ptr[FX_BOOT_SIG_32] = 0x29;
_fx_utility_32_unsigned_write(&byte_ptr[FX_VOLUME_ID_32], _fx_media_format_volume_id);
}else { ... }
if (total_sectors < (ULONG)0xFFFF)
{
_fx_utility_16_unsigned_write(&byte_ptr[FX_SECTORS], (UINT)(total_sectors));
_fx_utility_32_unsigned_write(&byte_ptr[FX_HUGE_SECTORS], 0);
}if (total_sectors < (ULONG)0xFFFF) { ... }
else
{
_fx_utility_16_unsigned_write(&byte_ptr[FX_SECTORS], (UINT)0);
_fx_utility_32_unsigned_write(&byte_ptr[FX_HUGE_SECTORS], total_sectors);
}else { ... }
_fx_utility_16_unsigned_write(&byte_ptr[FX_RESERVED_SECTORS], reserved_sectors);
byte_ptr[FX_SECTORS_CLUSTER] = (UCHAR)sectors_per_cluster;
byte_ptr[FX_NUMBER_OF_FATS] = (UCHAR)number_of_fats;
_fx_utility_32_unsigned_write(&byte_ptr[FX_HIDDEN_SECTORS], hidden_sectors);
if (total_clusters < FX_16_BIT_FAT_SIZE)
{
_fx_utility_16_unsigned_write(&byte_ptr[FX_ROOT_DIR_ENTRIES], directory_entries);
}if (total_clusters < FX_16_BIT_FAT_SIZE) { ... }
if (total_clusters < FX_16_BIT_FAT_SIZE)
{
j = FX_VOLUME_LABEL;
}if (total_clusters < FX_16_BIT_FAT_SIZE) { ... }
else
{
j = FX_VOLUME_LABEL_32;
}else { ... }
i = 0;
while (i < 11)
{
if (volume_name[i] == 0)
{
break;
}if (volume_name[i] == 0) { ... }
byte_ptr[j + i] = (UCHAR)volume_name[i];
i++;
}while (i < 11) { ... }
#ifndef FX_DISABLE_FORCE_MEMORY_OPERATION
while (i < 11)
{
byte_ptr[j + i] = (UCHAR)' ';
i++;
}while (i < 11) { ... }
/* ... */#else
_fx_utility_memory_set(&byte_ptr[j + i], ' ', (11 - i));
#endif
#ifdef FX_FORCE_512_BYTE_BOOT_SECTOR
byte_ptr[510] = 0x55;
byte_ptr[511] = 0xAA;/* ... */
#else
byte_ptr[bytes_per_sector - 2] = 0x55;
byte_ptr[bytes_per_sector - 1] = 0xAA;/* ... */
#endif
media_ptr -> fx_media_driver_request = FX_DRIVER_BOOT_WRITE;
media_ptr -> fx_media_driver_system_write = FX_TRUE;
media_ptr -> fx_media_driver_sectors = 1;
media_ptr -> fx_media_driver_sector_type = FX_BOOT_SECTOR;
FX_TRACE_IN_LINE_INSERT(FX_TRACE_INTERNAL_IO_DRIVER_BOOT_WRITE, media_ptr, memory_ptr, 0, 0, FX_TRACE_INTERNAL_EVENTS, 0, 0)
(driver)(media_ptr);
media_ptr -> fx_media_driver_system_write = FX_FALSE;
if (media_ptr -> fx_media_driver_status != FX_SUCCESS)
{
return(FX_IO_ERROR);
}if (media_ptr -> fx_media_driver_status != FX_SUCCESS) { ... }
root_sectors = ((directory_entries * FX_DIR_ENTRY_SIZE) + bytes_per_sector - 1) / bytes_per_sector;
/* ... */
if ((total_clusters >= FX_16_BIT_FAT_SIZE) && (bytes_per_sector == 512))
{
#ifndef FX_DISABLE_FORCE_MEMORY_OPERATION
for (i = 0; i < bytes_per_sector; i++)
{
byte_ptr[i] = (CHAR)0;
}for (i = 0; i < bytes_per_sector; i++) { ... }
/* ... */#else
_fx_utility_memory_set(byte_ptr, 0, bytes_per_sector);
#endif
byte_ptr[0] = 0x52;
byte_ptr[1] = 0x52;
byte_ptr[2] = 0x61;
byte_ptr[3] = 0x41;
byte_ptr[484] = 0x72;
byte_ptr[485] = 0x72;
byte_ptr[486] = 0x41;
byte_ptr[487] = 0x61;
byte_ptr[508] = 0x55;
byte_ptr[509] = 0xAA;
_fx_utility_32_unsigned_write(&byte_ptr[488], (total_clusters - 1));
_fx_utility_32_unsigned_write(&byte_ptr[492], 3);
media_ptr -> fx_media_driver_logical_sector = 1;
media_ptr -> fx_media_driver_request = FX_DRIVER_WRITE;
media_ptr -> fx_media_driver_sectors = 1;
media_ptr -> fx_media_driver_system_write = FX_TRUE;
media_ptr -> fx_media_driver_sector_type = FX_BOOT_SECTOR;
FX_TRACE_IN_LINE_INSERT(FX_TRACE_INTERNAL_IO_DRIVER_WRITE, media_ptr, 1, 1, memory_ptr, FX_TRACE_INTERNAL_EVENTS, 0, 0)
(driver)(media_ptr);
media_ptr -> fx_media_driver_system_write = FX_FALSE;
if (media_ptr -> fx_media_driver_status != FX_SUCCESS)
{
return(FX_IO_ERROR);
}if (media_ptr -> fx_media_driver_status != FX_SUCCESS) { ... }
}if ((total_clusters >= FX_16_BIT_FAT_SIZE) && (bytes_per_sector == 512)) { ... }
for (f = 0; f < number_of_fats; f++)
{
for (s = 0; s < sectors_per_fat; s++)
{
if (s == 0)
{
if (total_clusters < FX_12_BIT_FAT_SIZE)
{
byte_ptr[0] = (UCHAR)0x0F;
byte_ptr[1] = (UCHAR)0x0F;
byte_ptr[2] = (UCHAR)0xFF;
i = 3;
}if (total_clusters < FX_12_BIT_FAT_SIZE) { ... }
else if (total_clusters < FX_16_BIT_FAT_SIZE)
{
byte_ptr[0] = (UCHAR)0xF0;
byte_ptr[1] = (UCHAR)0x00;
byte_ptr[2] = (UCHAR)0xFF;
byte_ptr[3] = (UCHAR)0xFF;
i = 4;
}else if (total_clusters < FX_16_BIT_FAT_SIZE) { ... }
else
{
byte_ptr[0] = (UCHAR)0xF0;
byte_ptr[1] = (UCHAR)0x00;
byte_ptr[2] = (UCHAR)0x00;
byte_ptr[3] = (UCHAR)0x00;
byte_ptr[4] = (UCHAR)0xFF;
byte_ptr[5] = (UCHAR)0xFF;
byte_ptr[6] = (UCHAR)0xFF;
byte_ptr[7] = (UCHAR)0xFF;
byte_ptr[8] = (UCHAR)0xFF;
byte_ptr[9] = (UCHAR)0xFF;
byte_ptr[10] = (UCHAR)0xFF;
byte_ptr[11] = (UCHAR)0x0F;
i = 12;
}else { ... }
}if (s == 0) { ... }
else
{
i = 0;
}else { ... }
#ifndef FX_DISABLE_FORCE_MEMORY_OPERATION
for (; i < bytes_per_sector; i++)
{
byte_ptr[i] = (CHAR)0;
}for (; i < bytes_per_sector; i++) { ... }
/* ... */#else
_fx_utility_memory_set(&byte_ptr[i], 0, (bytes_per_sector - i));
#endif
media_ptr -> fx_media_driver_logical_sector = reserved_sectors + (f * sectors_per_fat) + s;
media_ptr -> fx_media_driver_request = FX_DRIVER_WRITE;
media_ptr -> fx_media_driver_sectors = 1;
media_ptr -> fx_media_driver_system_write = FX_TRUE;
media_ptr -> fx_media_driver_sector_type = FX_FAT_SECTOR;
FX_TRACE_IN_LINE_INSERT(FX_TRACE_INTERNAL_IO_DRIVER_WRITE, media_ptr, media_ptr -> fx_media_driver_logical_sector, 1, memory_ptr, FX_TRACE_INTERNAL_EVENTS, 0, 0)
(driver)(media_ptr);
media_ptr -> fx_media_driver_system_write = FX_FALSE;
if (media_ptr -> fx_media_driver_status != FX_SUCCESS)
{
return(FX_IO_ERROR);
}if (media_ptr -> fx_media_driver_status != FX_SUCCESS) { ... }
}for (s = 0; s < sectors_per_fat; s++) { ... }
}for (f = 0; f < number_of_fats; f++) { ... }
#ifndef FX_DISABLE_FORCE_MEMORY_OPERATION
for (i = 0; i < bytes_per_sector; i++)
{
byte_ptr[i] = (CHAR)0;
}for (i = 0; i < bytes_per_sector; i++) { ... }
/* ... */#else
_fx_utility_memory_set(byte_ptr, 0, bytes_per_sector);
#endif
for (s = 0; s < root_sectors; s++)
{
media_ptr -> fx_media_driver_logical_sector = reserved_sectors + (number_of_fats * sectors_per_fat) + s;
media_ptr -> fx_media_driver_request = FX_DRIVER_WRITE;
media_ptr -> fx_media_driver_sectors = 1;
media_ptr -> fx_media_driver_system_write = FX_TRUE;
media_ptr -> fx_media_driver_sector_type = FX_DIRECTORY_SECTOR;
FX_TRACE_IN_LINE_INSERT(FX_TRACE_INTERNAL_IO_DRIVER_WRITE, media_ptr, media_ptr -> fx_media_driver_logical_sector, 1, memory_ptr, FX_TRACE_INTERNAL_EVENTS, 0, 0)
(driver)(media_ptr);
media_ptr -> fx_media_driver_system_write = FX_FALSE;
if (media_ptr -> fx_media_driver_status != FX_SUCCESS)
{
return(FX_IO_ERROR);
}if (media_ptr -> fx_media_driver_status != FX_SUCCESS) { ... }
}for (s = 0; s < root_sectors; s++) { ... }
media_ptr -> fx_media_driver_request = FX_DRIVER_UNINIT;
media_ptr -> fx_media_driver_status = FX_IO_ERROR;
FX_TRACE_IN_LINE_INSERT(FX_TRACE_INTERNAL_IO_DRIVER_UNINIT, media_ptr, 0, 0, 0, FX_TRACE_INTERNAL_EVENTS, 0, 0)
(media_ptr -> fx_media_driver_entry) (media_ptr);
return(media_ptr -> fx_media_driver_status);
}{ ... }