summaryrefslogblamecommitdiffstats
path: root/private/ntos/arcinst/fmtexp.c
blob: 6164f94bdb52cd6aa1ff6ce895415329f9c2feac (plain) (tree)
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
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
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
537
538
539
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
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
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
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942













































































































































































































































































































































































































































































































































































































































































































































































































































































































































































                                                                                      
#include "precomp.h"
#pragma hdrstop

#define READ_SIZE   65536

typedef struct {
    UCHAR   IntelNearJumpCommand[1];    // Intel Jump command
    UCHAR   BootStrapJumpOffset[2];     // offset of boot strap code
    UCHAR   OemData[8];                     // OEM data
    UCHAR   BytesPerSector[2];          // BPB
    UCHAR   SectorsPerCluster[1];       //
    UCHAR   ReservedSectors[2];         //
    UCHAR   Fats[1];                    //
    UCHAR   RootEntries[2];             //
    UCHAR   Sectors[2];                 //
    UCHAR   Media[1];                   //
    UCHAR   SectorsPerFat[2];           //
    UCHAR   SectorsPerTrack[2];         //
    UCHAR   Heads[2];                   //
    UCHAR   HiddenSectors[4];           //
    UCHAR   LargeSectors[4];            //
        UCHAR   PhysicalDrive[1];                   // 0 = removable, 80h = fixed
        UCHAR   CurrentHead[1];                     // not used by fs utils
        UCHAR   Signature[1];                       // boot signature
        UCHAR   SerialNumber[4];            // serial number
        UCHAR   Label[11];                          // volume label, aligned padded
        UCHAR   SystemIdText[8];            // system ID, FAT for example
} UNALIGNED_SECTOR_ZERO, *PUNALIGNED_SECTOR_ZERO;


#define CSEC_FAT32MEG   65536
#define CSEC_FAT16BIT   32680
#define SYSID_FAT12BIT  1
#define SYSID_FAT16BIT  4
#define SYSID_FAT32MEG  6
#define MIN_CLUS_BIG    4085    // Minimum clusters for a big FAT.
#define MAX_CLUS_BIG    65525   // Maximum + 1 clusters for big FAT.



ARC_STATUS
FmtIsFatPartition(
    IN  ULONG       PartitionId,
    IN  ULONG       SectorSize,
    OUT PBOOLEAN    IsFatPartition
    )
/*++

Routine Description:

    This routine computes whether or not the given partition is a FAT
    partition.

Arguments:

    VolumeId        - Supplies the volume to check.
    SectorSize      - Supplies the number of bytes per sector.
    IsFatPartition  - Returns whether or not the partition is FAT.

Return Value:

    LowReadSectors, ENOMEM, ESUCCESS

--*/
{
    PUCHAR      Buffer;
    ARC_STATUS  r;

    if (!(Buffer = AllocateMemory(SectorSize*2))) {
        return ENOMEM;
    }

    r = LowReadSectors(PartitionId, SectorSize, 0, 2, Buffer);

    if (r != ESUCCESS) {
        FreeMemory(Buffer);
        return r;
    }

    *IsFatPartition = Buffer[510] == 0x55 &&
                      Buffer[511] == 0xAA &&
                      Buffer[0x10] != 0 &&
                      Buffer[0x15] == Buffer[SectorSize] &&
                      Buffer[SectorSize + 1] == 0xFF &&
                      Buffer[SectorSize + 2] == 0xFF;

    FreeMemory(Buffer);
    return ESUCCESS;
}


ARC_STATUS
FmtIsFat(
    IN  PCHAR       PartitionPath,
    OUT PBOOLEAN    IsFatPartition
    )
/*++

Routine Description:

    This routine computes whether or not the given partition is a FAT
    partition.

Arguments:

    PartitionPath   - Supplies a path to the partition.
    IsFatPartition  - Returns whether or not the partition is FAT.

Return Value:

    ArcOpen, ArcClose, LowGetPartitionGeometry, FmtIsFatPartition

--*/
{
    ULONG       partition_id;
    ARC_STATUS  r;
    ULONG       total_sectors, sector_size, sec_per_track, heads;

    r = LowGetPartitionGeometry(PartitionPath, &total_sectors, &sector_size,
                                &sec_per_track, &heads);

    if (r != ESUCCESS) {
        return r;
    }

    r = ArcOpen(PartitionPath, ArcOpenReadOnly, &partition_id);

    if (r != ESUCCESS) {
        return r;
    }

    r = FmtIsFatPartition(partition_id, sector_size, IsFatPartition);

    if (r != ESUCCESS) {
        ArcClose(partition_id);
        return r;
    }

    return ArcClose(partition_id);
}




USHORT
ComputeSecPerCluster(
    IN  ULONG   NumSectors,
    IN  BOOLEAN SmallFat
    )
/*++

Routine Description:

    This routine computes the number of sectors per cluster.

Arguments:

    NumSectors  - Supplies the number of sectors on the disk.
    SmallFat    - Supplies whether or not the FAT should be small.

Return Value:

    The number of sectors per cluster necessary.

--*/
{
    ULONG   threshold;
    USHORT  sec_per_clus;
    USHORT  min_sec_per_clus;

    threshold = SmallFat ? MIN_CLUS_BIG : MAX_CLUS_BIG;
    sec_per_clus = 1;

    while (NumSectors >= threshold) {
        sec_per_clus *= 2;
        threshold *= 2;
    }

    if (SmallFat) {
        min_sec_per_clus = 8;
    } else {
        min_sec_per_clus = 4;
    }

    return max(sec_per_clus, min_sec_per_clus);
}


ULONG
ComputeNewSerialNumber(
    IN  ULONG   Seed
    )
/*++

Routine Description:

    This routine computes a new serial number for a volume.

Arguments:

    Seed    - Supplies a seed for the serial number.

Return Value:

    A new volume serial number.

--*/
{
    PUCHAR  p;
    ULONG   i;

    p = (PUCHAR) &Seed;

    for (i = 0; i < sizeof(ULONG); i++) {

        Seed += p[i];
        Seed = (Seed >> 2) + (Seed << 30);
    }

    return Seed;
}


VOID
EditFat(
    IN      USHORT  ClusterNumber,
    IN      USHORT  ClusterEntry,
    IN OUT  PUCHAR  Fat,
    IN      BOOLEAN SmallFat
    )
/*++

Routine Description:

    This routine edits the FAT entry 'ClusterNumber' with 'ClusterEntry'.

Arguments:

    ClusterNumber   - Supplies the number of the cluster to edit.
    ClusterEntry    - Supplies the new value for that cluster number.
    Fat             - Supplies the FAT to edit.
    SmallFat        - Supplies whether or not the FAT is small.

Return Value:

    None.

--*/
{
    ULONG   n;

    if (SmallFat) {

        n = ClusterNumber*3;
        if (n%2) {
                Fat[n/2] = (UCHAR) ((Fat[n/2]&0x0F) | ((ClusterEntry&0x000F)<<4));
            Fat[n/2 + 1] = (UCHAR) ((ClusterEntry&0x0FF0)>>4);
        } else {
            Fat[n/2] = (UCHAR) (ClusterEntry&0x00FF);
                Fat[n/2 + 1] = (UCHAR) ((Fat[n/2 + 1]&0xF0) |
                                    ((ClusterEntry&0x0F00)>>8));
        }

    } else {

        ((PUSHORT) Fat)[ClusterNumber] = ClusterEntry;

    }
}


ARC_STATUS
FmtFillFormatBuffer(
    IN  ULONG   NumberOfSectors,
    IN  ULONG   SectorSize,
    IN  ULONG   SectorsPerTrack,
    IN  ULONG   NumberOfHeads,
    IN  ULONG   NumberOfHiddenSectors,
    OUT PVOID   FormatBuffer,
    IN  ULONG   FormatBufferSize,
    OUT PULONG  SuperAreaSize,
    IN  ULONG   TimeSeed,
    IN  PULONG  BadSectorsList,
    IN  ULONG   NumberOfBadSectors
    )
/*++

Routine Description:

    This routine computes a FAT super area based on the disk size,
    disk geometry, and bad sectors of the volume.

Arguments:

    NumberOfSectors         - Supplies the number of sectors on the volume.
    SectorSize              - Supplies the number of bytes per sector.
    SectorsPerTrack         - Supplies the number of sectors per track.
    NumberOfHeads           - Supplies the number of heads.
    NumberOfHiddenSectors   - Supplies the number of hidden sectors.
    FormatBuffer            - Returns the super area for the volume.
    FormatBufferSize        - Supplies the number of bytes in the supplied
                                buffer.
    SuperAreaSize           - Returns the number of bytes in the super area.
    TimeSeed                - Supplies a time seed for serial number.
    BadSectorsList          - Supplies the list of bad sectors on the volume.
    NumberOfBadSectors      - Supplies the number of bad sectors in the list.

Return Value:

    ENOMEM  - The buffer wasn't big enough.
    E2BIG   - The disk is too large to be formatted.
    EIO     - There is a bad sector in the super area.
    EINVAL  - There is a bad sector off the end of the disk.
    ESUCCESS

--*/
{
    PUNALIGNED_SECTOR_ZERO  psecz;
    PUCHAR                  puchar;
    USHORT                  tmp_ushort;
    ULONG                   tmp_ulong;
    BOOLEAN                 small_fat;
    ULONG                   num_sectors;
    ULONG                   partition_id;
    ULONG                   sec_per_fat;
    ULONG                   sec_per_root;
    ULONG                   sec_per_clus;
    ULONG                   i;
    ULONG                   sec_per_sa;


    // First memset the buffer to all zeros;
    memset(FormatBuffer, 0, (unsigned int) FormatBufferSize);


    // Make sure that there's enough room for the BPB.

    if (!FormatBuffer || FormatBufferSize < SectorSize) {
        return ENOMEM;
    }

    // Compute the number of sectors on disk.
    num_sectors = NumberOfSectors;

    // Compute the partition identifier.
    partition_id = num_sectors < CSEC_FAT16BIT ? SYSID_FAT12BIT :
                   num_sectors < CSEC_FAT32MEG ? SYSID_FAT16BIT :
                                                 SYSID_FAT32MEG;

    // Compute whether or not to have a big or small FAT.
    small_fat = (BOOLEAN) (partition_id == SYSID_FAT12BIT);


    psecz = (PUNALIGNED_SECTOR_ZERO) FormatBuffer;
    puchar = (PUCHAR) FormatBuffer;

    // Set up the jump instruction.
    psecz->IntelNearJumpCommand[0] = 0xEB;
    tmp_ushort = 0x903C;
    memcpy(psecz->BootStrapJumpOffset, &tmp_ushort, sizeof(USHORT));

    // Set up the OEM data.
    memcpy(psecz->OemData, "WINNT1.0", 8); // BUGBUG norbertk

    // Set up the bytes per sector.
    tmp_ushort = (USHORT) SectorSize;
    memcpy(psecz->BytesPerSector, &tmp_ushort, sizeof(USHORT));

    // Set up the number of sectors per cluster.
    sec_per_clus = ComputeSecPerCluster(num_sectors, small_fat);
    if (sec_per_clus > 128) {

        // The disk is too large to be formatted.
        return E2BIG;
    }
    psecz->SectorsPerCluster[0] = (UCHAR) sec_per_clus;

    // Set up the number of reserved sectors.
    tmp_ushort = 1;
    memcpy(psecz->ReservedSectors, &tmp_ushort, sizeof(USHORT));

    // Set up the number of FATs.
    psecz->Fats[0] = 2;

    // Set up the number of root entries and number of sectors for the root.
    tmp_ushort = 512;
    memcpy(psecz->RootEntries, &tmp_ushort, sizeof(USHORT));
    sec_per_root = (512*32 - 1)/SectorSize + 1;

    // Set up the number of sectors.
    if (num_sectors >= 1<<16) {
        tmp_ushort = 0;
        tmp_ulong = num_sectors;
    } else {
        tmp_ushort = (USHORT) num_sectors;
        tmp_ulong = 0;
    }
    memcpy(psecz->Sectors, &tmp_ushort, sizeof(USHORT));
    memcpy(psecz->LargeSectors, &tmp_ulong, sizeof(ULONG));

    // Set up the media byte.
    psecz->Media[0] = 0xF8;

    // Set up the number of sectors per FAT.
    if (small_fat) {
        sec_per_fat = num_sectors/(2 + SectorSize*sec_per_clus*2/3);
    } else {
        sec_per_fat = num_sectors/(2 + SectorSize*sec_per_clus/2);
    }
    sec_per_fat++;
    tmp_ushort = (USHORT) sec_per_fat;
    memcpy(psecz->SectorsPerFat, &tmp_ushort, sizeof(USHORT));

    // Set up the number of sectors per track.
    tmp_ushort = (USHORT) SectorsPerTrack;
    memcpy(psecz->SectorsPerTrack, &tmp_ushort, sizeof(USHORT));

    // Set up the number of heads.
    tmp_ushort = (USHORT) NumberOfHeads;
    memcpy(psecz->Heads, &tmp_ushort, sizeof(USHORT));

    // Set up the number of hidden sectors.
    memcpy(psecz->HiddenSectors, &NumberOfHiddenSectors, sizeof(ULONG));

    // Set up the physical drive number.
    psecz->PhysicalDrive[0] = 0x80;

    // Set up the BPB signature.
    psecz->Signature[0] = 0x29;

    // Set up the serial number.
    tmp_ulong = ComputeNewSerialNumber(TimeSeed);
    memcpy(psecz->SerialNumber, &tmp_ulong, sizeof(ULONG));

    // Set up the system id.
    memcpy(psecz->SystemIdText, "FAT     ", 8);

    // Set up the boot signature.
    puchar[510] = 0x55;
    puchar[511] = 0xAA;


    // Now make sure that the buffer has enough room for both of the
    // FATs and the root directory.

    sec_per_sa = 1 + 2*sec_per_fat + sec_per_root;
    *SuperAreaSize = SectorSize*sec_per_sa;
    if (*SuperAreaSize > FormatBufferSize) {
        return ENOMEM;
    }


    // Set up the first FAT.

    puchar[SectorSize] = 0xF8;
    puchar[SectorSize + 1] = 0xFF;
    puchar[SectorSize + 2] = 0xFF;

    if (!small_fat) {
        puchar[SectorSize + 3] = 0xFF;
    }


    for (i = 0; i < NumberOfBadSectors; i++) {

        if (BadSectorsList[i] < sec_per_sa) {
            // There's a bad sector in the super area.
            return EIO;
        }

        if (BadSectorsList[i] >= num_sectors) {
            // Bad sector out of range.
            return EINVAL;
        }

        // Compute the bad cluster number;
        tmp_ushort = (USHORT)
                     ((BadSectorsList[i] - sec_per_sa)/sec_per_clus + 2);

        EditFat(tmp_ushort, (USHORT) 0xFFF7, &puchar[SectorSize], small_fat);
    }


    // Copy the first FAT onto the second.

    memcpy(&puchar[SectorSize*(1 + sec_per_fat)],
           &puchar[SectorSize],
           (unsigned int) SectorSize*sec_per_fat);

    return ESUCCESS;
}


ARC_STATUS
FmtVerifySectors(
    IN  ULONG       PartitionId,
    IN  ULONG       NumberOfSectors,
    IN  ULONG       SectorSize,
    OUT PULONG*     BadSectorsList,
    OUT PULONG      NumberOfBadSectors
    )
/*++

Routine Description:

    This routine verifies all of the sectors on the volume.
    It returns a pointer to a list of bad sectors.  The pointer
    will be NULL if there was an error detected.

Arguments:

    PartitionId         - Supplies a handle to the partition for reading.
    NumberOfSectors     - Supplies the number of partition sectors.
    SectorSize          - Supplies the number of bytes per sector.
    BadSectorsList      - Returns the list of bad sectors.
    NumberOfBadSectors  - Returns the number of bad sectors in the list.

Return Value:

    ENOMEM, ESUCCESS

--*/
{
    ULONG           num_read_sec;
    PVOID           read_buffer;
    ULONG           i, j;
    PULONG          bad_sec_buf;
    ULONG           max_num_bad;
    ARC_STATUS      r;
    ULONG           percent;

    if (!(read_buffer = AllocateMemory(READ_SIZE))) {
        return ENOMEM;
    }

    max_num_bad = 100;
    if (!(bad_sec_buf = (PULONG) AllocateMemory(max_num_bad*sizeof(ULONG)))) {
        FreeMemory(read_buffer);
        return ENOMEM;
    }

    *NumberOfBadSectors = 0;

    num_read_sec = READ_SIZE/SectorSize;


    percent = 1;
    for (i = 0; i < NumberOfSectors; i += num_read_sec) {

        if (percent != i*100/NumberOfSectors) {
            percent = i*100/NumberOfSectors;
            AlPrint("%s%d percent formatted.\r", MSGMARGIN, percent);
        }

        if (i + num_read_sec > NumberOfSectors) {
            num_read_sec = NumberOfSectors - i;
        }

        r = LowReadSectors(PartitionId, SectorSize, i,
                           num_read_sec, read_buffer);

        if (r != ESUCCESS) {

            for (j = 0; j < num_read_sec; j++) {

                r = LowReadSectors(PartitionId, SectorSize, i+j, 1,
                                   read_buffer);

                if (r != ESUCCESS) {

                    if (*NumberOfBadSectors == max_num_bad) {

                        max_num_bad += 100;
                        if (!(bad_sec_buf = (PULONG)
                              ReallocateMemory(bad_sec_buf,
                                               max_num_bad*sizeof(ULONG)))) {

                            FreeMemory(read_buffer);
                            FreeMemory(bad_sec_buf);
                            return ENOMEM;
                        }
                    }

                    bad_sec_buf[(*NumberOfBadSectors)++] = i + j;
                }
            }
        }
    }

    AlPrint("%s100 percent formatted.\r\n",MSGMARGIN);

    *BadSectorsList = bad_sec_buf;

    FreeMemory(read_buffer);

    return ESUCCESS;
}


ARC_STATUS
FmtFatFormat(
    IN  PCHAR   PartitionPath,
    IN  ULONG   HiddenSectorCount
    )
/*++

Routine Description:

    This routine does a FAT format on the given partition.

Arguments:

    PartitionPath   - Supplies a path to the partition to format.

Return Value:

    LowGetPartitionGeometry, ArcOpen,
    FmtVerifySectors, FmtFillFormatBuffer, LowWriteSectors,
    ArcClose, ENOMEM

--*/
{
    ULONG           num_sectors;
    ULONG           sector_size;
    ULONG           sec_per_track;
    ULONG           heads;
    ULONG           hidden_sectors;
    PULONG          bad_sectors;
    ULONG           num_bad_sectors;
    PVOID           format_buffer;
    ULONG           max_sec_per_sa;
    ULONG           super_area_size;
    ARC_STATUS      r;
    ULONG           partition_id;
    ULONG           time_seed;
    PTIME_FIELDS    ptime_fields;


    r = LowGetPartitionGeometry(PartitionPath, &num_sectors,
                                &sector_size, &sec_per_track, &heads);

    if (r != ESUCCESS) {
        return r;
    }

    hidden_sectors = HiddenSectorCount;

    r = ArcOpen(PartitionPath, ArcOpenReadWrite, &partition_id);

    if (r != ESUCCESS) {
        return r;
    }

    bad_sectors = NULL;

    r = FmtVerifySectors(partition_id, num_sectors, sector_size,
                         &bad_sectors, &num_bad_sectors);

    if (r != ESUCCESS) {
        ArcClose(partition_id);
        return r;
    }

    max_sec_per_sa = 1 +
                     2*((2*65536 - 1)/sector_size + 1) +
                     ((512*32 - 1)/sector_size + 1);

    ptime_fields = ArcGetTime();

    time_seed = (ptime_fields->Year - 1970)*366*24*60*60 +
                (ptime_fields->Month)*31*24*60*60 +
                (ptime_fields->Day)*24*60*60 +
                (ptime_fields->Hour)*60*60 +
                (ptime_fields->Minute)*60 +
                (ptime_fields->Second);

    if (!(format_buffer = AllocateMemory(max_sec_per_sa*sector_size))) {

        if (bad_sectors) {
            FreeMemory(bad_sectors);
        }
        ArcClose(partition_id);
        return ENOMEM;
    }

    r = FmtFillFormatBuffer(num_sectors,
                            sector_size,
                            sec_per_track,
                            heads,
                            hidden_sectors,
                            format_buffer,
                            max_sec_per_sa*sector_size,
                            &super_area_size,
                            time_seed,
                            bad_sectors,
                            num_bad_sectors);


    if (bad_sectors) {
        FreeMemory(bad_sectors);
    }

    if (r != ESUCCESS) {
        ArcClose(partition_id);
        FreeMemory(format_buffer);
        return r;
    }

    r = LowWriteSectors(partition_id, sector_size, 0,
                        super_area_size/sector_size, format_buffer);

    FreeMemory(format_buffer);

    if (r != ESUCCESS) {
        ArcClose(partition_id);
        return r;
    }

    return ArcClose(partition_id);
}


ARC_STATUS
FmtQueryFatPartitionList(
    OUT PCHAR** FatPartitionList,
    OUT PULONG  ListLength
    )
/*++

Routine Description:

    This routine browses the component tree for all disk peripherals
    and the attempts to open partitions on all of them.  It add all
    FAT partitions to the list and returns it.

Arguments:

    FatPartitionList    - Returns a list of FAT partitions.
    ListLength          - Returns the length of the list.

Return Value:

    LowQueryPathList, ArcOpen, ArcClose, FmtIsFat, EIO, ENOMEM, ESUCCESS

--*/
{
    CONFIGURATION_TYPE          config_type;
    ARC_STATUS                  r;
    PCHAR*                      peripheral_list;
    ULONG                       peripheral_list_length;
    ULONG                       i, j;
    ULONG                       num_partitions;
    CHAR                        partition_buf[21];
    ULONG                       partition_id;
    PCHAR*                      fat_partition_list;
    ULONG                       fat_partition_list_length;
    BOOLEAN                     is_fat;
    PCHAR                       partition_name;


    // First get a list of the all of the disk peripheral paths.

    config_type = DiskPeripheral;
    r = LowQueryPathList(NULL, &config_type, &peripheral_list,
                         &peripheral_list_length);

    if (r != ESUCCESS) {
        return r;
    }

    // Now we have a list of disk peripheral paths.



    // Next, count how many partitions there are.

    num_partitions = 0;
    for (i = 0; i < peripheral_list_length; i++) {

        partition_name = AllocateMemory(strlen(peripheral_list[i]) + 21);

        if (!partition_name) {
            LowFreePathList(peripheral_list, peripheral_list_length);
            return ENOMEM;
        }

        for (j = 1; ; j++) {

            strcpy(partition_name, peripheral_list[i]);
            sprintf(partition_buf, "partition(%d)", j);
            strcat(partition_name, partition_buf);

            r = ArcOpen(partition_name, ArcOpenReadOnly, &partition_id);

            if (r != ESUCCESS) {
                break;
            }

            num_partitions++;

            r = ArcClose(partition_id);

            if (r != ESUCCESS) {
                LowFreePathList(peripheral_list, peripheral_list_length);
                FreeMemory(partition_name);
                return r;
            }
        }

        FreeMemory(partition_name);
    }

    // 'num_partitions' indicates the number of partitions on the disk.


    // Allocate a buffer for the FAT partitions list.  There can be
    // no more FAT partitions then there are partitions.

    fat_partition_list = (PCHAR*) AllocateMemory(num_partitions*sizeof(PCHAR));

    if (!fat_partition_list) {
        LowFreePathList(peripheral_list, peripheral_list_length);
        return ENOMEM;
    }

    for (i = 0; i < num_partitions; i++) {
        fat_partition_list[i] = NULL;
    }

    fat_partition_list_length = 0;

    // 'fat_partition_list_length' indicates the number of FAT partitions.



    // Now go through all of the peripherals trying all possible
    // partitions on each.  Test these to see if they are FAT and
    // put the FAT ones in 'fat_partitions_list'.

    for (i = 0; i < peripheral_list_length; i++) {

        partition_name = AllocateMemory(strlen(peripheral_list[i]) + 21);

        if (!partition_name) {
            LowFreePathList(peripheral_list, peripheral_list_length);
            LowFreePathList(fat_partition_list, fat_partition_list_length);
            return ENOMEM;
        }

        for (j = 1; ; j++) {

            strcpy(partition_name, peripheral_list[i]);
            sprintf(partition_buf, "partition(%d)", j);
            strcat(partition_name, partition_buf);

            r = ArcOpen(partition_name, ArcOpenReadOnly, &partition_id);

            if (r != ESUCCESS) {
                break;
            }

            r = ArcClose(partition_id);

            if (r != ESUCCESS) {
                LowFreePathList(peripheral_list, peripheral_list_length);
                LowFreePathList(fat_partition_list, fat_partition_list_length);
                FreeMemory(partition_name);
                return r;
            }

            r = FmtIsFat(partition_name, &is_fat);

            if (r != ESUCCESS) {
                LowFreePathList(peripheral_list, peripheral_list_length);
                LowFreePathList(fat_partition_list, fat_partition_list_length);
                FreeMemory(partition_name);
                return r;
            }

            if (is_fat) {

                if (fat_partition_list_length == num_partitions) {
                    // This can't happen.
                    LowFreePathList(peripheral_list, peripheral_list_length);
                    LowFreePathList(fat_partition_list, fat_partition_list_length);
                    FreeMemory(partition_name);
                    return EIO;
                }

                fat_partition_list[fat_partition_list_length] =
                        AllocateMemory(strlen(partition_name) + 1);

                if (!fat_partition_list[fat_partition_list_length]) {
                    LowFreePathList(peripheral_list, peripheral_list_length);
                    LowFreePathList(fat_partition_list, fat_partition_list_length);
                    FreeMemory(partition_name);
                    return ENOMEM;
                }

                strcpy(fat_partition_list[fat_partition_list_length], partition_name);

                fat_partition_list_length++;
            }
        }

        FreeMemory(partition_name);
    }

    LowFreePathList(peripheral_list, peripheral_list_length);

    *FatPartitionList = fat_partition_list;
    *ListLength = fat_partition_list_length;

    return ESUCCESS;
}


ARC_STATUS
FmtFreeFatPartitionList(
    IN OUT  PCHAR*  FatPartitionList,
    IN      ULONG   ListLength
    )
/*++

Routine Description:

    This routine frees up the heap space taken by the FAT partition
    list.

Arguments:

    FatPartitionList    - Supplies the buffer to free.
    ListLength          - Supplies the buffer length.

Return Value:

    LowFreePathList

--*/
{
    return LowFreePathList(FatPartitionList, ListLength);
}