summaryrefslogblamecommitdiffstats
path: root/private/ntos/arcinst/low.c
blob: e24324f44cdc5e3b31b7e465e9afe48cbfeab915 (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
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186

































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































                                                                                                                      
#include "precomp.h"
#pragma hdrstop
#include <bootmbr.h>

ARC_STATUS
LowOpenDisk(
    IN  PCHAR   DevicePath,
    OUT PULONG  DiskId
    )
/*++

Routine Description:

    This routine opens the supplied device for DASD access.

Arguments:

    DevicePath  - Supplies the device path to be opened.
    DiskId      - Returns the disk id.

Return Value:

    ArcOpen

--*/
{
   char buffer[256];

   sprintf(buffer,"%spartition(0)",DevicePath);

   return ArcOpen(buffer, ArcOpenReadWrite, DiskId);
}


ARC_STATUS
LowCloseDisk(
    IN  ULONG    DiskId
    )
/*++

Routine Description:

    This routine closes the supplied device.

Arguments:

    DiskId      - Supplies the disk id.

Return Value:

    ArcClose

--*/
{
    return ArcClose(DiskId);
}

ARC_STATUS
LowGetDriveGeometry(
    IN  PCHAR   DevicePath,
    OUT PULONG  TotalSectorCount,
    OUT PULONG  SectorSize,
    OUT PULONG  SectorsPerTrack,
    OUT PULONG  Heads
    )
{
    char Buffer[256];

    sprintf(Buffer,"%spartition(0)",DevicePath);
    return(LowGetPartitionGeometry(Buffer,TotalSectorCount,SectorSize,SectorsPerTrack,Heads));
}



ARC_STATUS
LowGetPartitionGeometry(
    IN  PCHAR   PartitionPath,
    OUT PULONG  TotalSectorCount,
    OUT PULONG  SectorSize,
    OUT PULONG  SectorsPerTrack,
    OUT PULONG  Heads
    )
/*++

Routine Description:

    This routine computes the drive geometry for the given partition or
    physical disk.

Arguments:

    PartitionPath   - Supplies a path to the partition or physical disk.
    NumberOfSectors - Returns the number of sectors.
    SectorSize      - Returns the sector size.
    SectorsPerTrack - Returns the number of sectors per track.
    Heads           - Returns the number of heads.

Return Value:

    ArcOpen, ArcGetFileInformation, ArcClose, E2BIG, ESUCCESS

--*/
{
    FILE_INFORMATION    file_info;
    ARC_STATUS          r;
    ULONG               fileid;
    LARGE_INTEGER       l;
    CM_DISK_GEOMETRY_DEVICE_DATA    *DiskGeometry;
    CONFIGURATION_COMPONENT         *DiskComponent;
    CM_PARTIAL_RESOURCE_LIST        *DiskConfiguration;
    CHAR                            DataBuffer[sizeof(CM_PARTIAL_RESOURCE_LIST) +
                                               sizeof(CM_DISK_GEOMETRY_DEVICE_DATA)];
    CM_PARTIAL_RESOURCE_DESCRIPTOR  *DiskData;

    // Always assume 512 bytes per sector.

    *SectorSize      = 512;

    // Assume the SCSI default values for number of heads and sectors per track

    *SectorsPerTrack = 32;
    *Heads           = 64;

    // See if there is device specific data describing the geometry of
    // the drive.  If there is none, then just use the default SCSI
    // values.

    DiskComponent = ArcGetComponent(PartitionPath);

    if (DiskComponent == NULL) {
        return EINVAL;
    }

    //
    // See if the ConfigurationDataLength is correct
    // It should contain one DeviceSpecific resource descriptor
    //

    if (DiskComponent->ConfigurationDataLength == sizeof(CM_PARTIAL_RESOURCE_LIST) +
                                                  sizeof(CM_DISK_GEOMETRY_DEVICE_DATA)  ) {

        DiskConfiguration = (CM_PARTIAL_RESOURCE_LIST *)DataBuffer;

        r = ArcGetConfigurationData(DiskConfiguration,DiskComponent);

        if (r == ESUCCESS) {

            //
            // See if the Configuration Data has ARC version 1.3 or greater
            //

            if  ( (DiskConfiguration->Version == 1 && DiskConfiguration->Revision >=3 ) ||
                  (DiskConfiguration->Version >  1)                                        ) {

                DiskData = &(DiskConfiguration->PartialDescriptors[DiskConfiguration->Count-1]);

                if (DiskData->Type == CmResourceTypeDeviceSpecific) {

                    if (DiskData->u.DeviceSpecificData.DataSize == sizeof(CM_DISK_GEOMETRY_DEVICE_DATA)) {
                        DiskGeometry     = (CM_DISK_GEOMETRY_DEVICE_DATA *)
                                           &(DiskConfiguration->PartialDescriptors[DiskConfiguration->Count]);
                        *SectorsPerTrack = DiskGeometry->SectorsPerTrack;
                        *Heads           = DiskGeometry->NumberOfHeads;
                        *SectorSize      = DiskGeometry->BytesPerSector;
                    }
                }
            }
        }
    }

//    PrintError("SectorSize = %08x",*SectorSize);
//    PrintError("SectorsPerTrack = %08x",*SectorsPerTrack);
//    PrintError("Heads = %08x",*Heads);

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

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

    r = ArcGetFileInformation(fileid, &file_info);

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

    r = ArcClose(fileid);

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

    l.QuadPart = file_info.EndingAddress.QuadPart -
                 file_info.StartingAddress.QuadPart;

    l.QuadPart = ((ULONGLONG)l.QuadPart) / ((ULONGLONG)(*SectorSize));

    if (l.HighPart) {
        return E2BIG;
    }

    *TotalSectorCount = l.LowPart;

    return ESUCCESS;
}


#define MAX_TRANSFER    65536


ARC_STATUS
LowReadSectors(
    IN  ULONG   VolumeId,
    IN  ULONG   SectorSize,
    IN  ULONG   StartingSector,
    IN  ULONG   NumberOfSectors,
    OUT PVOID   Buffer
    )
/*++

Routine Description:

    This routine reads 'NumberOfSectors' sectors starting at sector
    'StartingSector' on the volume with ID 'VolumeId'.

Arguments:

    VolumeId        - Supplies the ID for the volume.
    SectorSize      - Supplies the number of bytes per sector.
    StartingSector  - Supplies the starting sector for the read.
    NumberOfSectors - Supplies the number of sectors to read.
    Buffer          - Returns the read in sectors.

Return Value:

    ArcSeek, ArcRead, EIO, ESUCCESS

--*/
{
    ARC_STATUS    r;
    ULONG         c;
    LARGE_INTEGER l;
    ULONG         i;
    ULONG         transfer;
    PCHAR         buf;
    ULONG         total;


    l.QuadPart = UInt32x32To64(StartingSector,SectorSize);

    buf = (PCHAR) Buffer;

    r = ArcSeek(VolumeId, &l, SeekAbsolute);

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

    total = SectorSize*NumberOfSectors;

    for (i = 0; i < total; i += MAX_TRANSFER) {

        transfer = min(MAX_TRANSFER, total - i);

        r = ArcRead(VolumeId, &buf[i], transfer, &c);

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

        if (c != transfer) {
            return EIO;
        }
    }

    return ESUCCESS;
}


ARC_STATUS
LowWriteSectors(
    IN  ULONG   VolumeId,
    IN  ULONG   SectorSize,
    IN  ULONG   StartingSector,
    IN  ULONG   NumberOfSectors,
    IN  PVOID   Buffer
    )
/*++

Routine Description:

    This routine write 'NumberOfSectors' sectors starting at sector
    'StartingSector' on the volume with ID 'VolumeId'.

Arguments:

    VolumeId        - Supplies the ID for the volume.
    SectorSize      - Supplies the number of bytes per sector.
    StartingSector  - Supplies the starting sector for the write.
    NumberOfSectors - Supplies the number of sectors to write.
    Buffer          - Supplies the sectors to write.

Return Value:

    ArcSeek, ArcWrite, EIO, ESUCCESS

--*/
{
    ARC_STATUS    r;
    ULONG         c;
    LARGE_INTEGER l;
    ULONG         i;
    ULONG         transfer;
    PCHAR         buf;
    ULONG         total;

    l.QuadPart = UInt32x32To64(StartingSector,SectorSize);

    buf = (PCHAR) Buffer;

    r = ArcSeek(VolumeId, &l, SeekAbsolute);

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

    total = SectorSize*NumberOfSectors;

    for (i = 0; i < total; i += MAX_TRANSFER) {

        transfer = min(MAX_TRANSFER, total - i);

        r = ArcWrite(VolumeId, &buf[i], transfer, &c);

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

        if (c != transfer) {
            return EIO;
        }
    }

    return ESUCCESS;
}


/******************** DAVIDRO CODE *************************************/
/*++

Copyright (c) 1991  Microsoft Corporation

Module Name:

    alpath.c

Abstract:

    This module provides ARC pathname functions.

Author:

    David M. Robinson (davidro) 13-November-1991

Revision History:

--*/

//
// Define the ARC pathname mnemonics.
//

PCHAR MnemonicTable[] = {
    "arc",
    "cpu",
    "fpu",
    "pic",
    "pdc",
    "sic",
    "sdc",
    "sc",
    "eisa",
    "tc",
    "scsi",
    "dti",
    "multi",
    "disk",
    "tape",
    "cdrom",
    "worm",
    "serial",
    "net",
    "video",
    "par",
    "point",
    "key",
    "audio",
    "other",
    "rdisk",
    "fdisk",
    "tape",
    "modem",
    "monitor",
    "print",
    "pointer",
    "keyboard",
    "term",
    "other",
    "line",
    "netper",
    "memory"
    };

//
// Static storage for the pathname return value.
//

CHAR Pathname[256];


PCHAR
AlGetPathnameFromComponent (
    IN PCONFIGURATION_COMPONENT Component
    )

/*++

Routine Description:

    This function builds an ARC pathname for the specified component.

Arguments:

    Component - Supplies a pointer to a configuration component.

Return Value:

    Returns a pointer to a string which contains the ARC pathname for the
    component.  NOTE: The string is stored in static storage, and must be
    copied by the user before another call to this routine.

--*/
{
    PCONFIGURATION_COMPONENT ParentComponent;
    CHAR NewSegment[16];
    CHAR Tempname[256];

    Pathname[0] = 0;

    //
    // Loop while not at the root component.
    //

    while ((ParentComponent = ArcGetParent(Component)) != NULL) {

        //
        // Build new pathname segment from the Component type and key.
        //

        sprintf(NewSegment,
                "%s(%d)",
                MnemonicTable[Component->Type],
                Component->Key);

        //
        // Add the new segment as a prefix of the current pathname.
        //

        strcpy(Tempname, Pathname);
        strcpy(Pathname, NewSegment);
        strcat(Pathname, Tempname);

        //
        // Move to the parent component.
        //

        Component = ParentComponent;
    }

    return Pathname;
}
/******************* END OF DAVIDRO CODE *********************************/


ARC_STATUS
LowQueryPathFromComponent(
    IN  PCONFIGURATION_COMPONENT    Component,
    OUT PCHAR*                      Path
    )
/*++

Routine Description:

    This routine computes a path from a component.  The resulting
    path is allocated on the heap.

Arguments:

    Component   - Supplies a component.
    Path        - Returns the path corresponding to that component.

Return Value:

    ENOMEM, ESUCCESS

--*/
{
    PCHAR   p;
    PCHAR   path;

    p = AlGetPathnameFromComponent(Component);

    path = AllocateMemory(strlen(p) + 1);

    if (!path) {
        return ENOMEM;
    }

    strcpy(path, p);

    *Path = path;

    return ESUCCESS;
}


ARC_STATUS
LowTraverseChildren(
    IN      PCONFIGURATION_COMPONENT    Parent,
    IN      CONFIGURATION_CLASS*        ConfigClass OPTIONAL,
    IN      CONFIGURATION_TYPE*         ConfigType OPTIONAL,
    IN OUT  PCONFIGURATION_COMPONENT*   MatchingArray OPTIONAL,
    IN OUT  PULONG                      CurrentLength
    )
/*++

Routine Description:

    This routine traverses the trees whose parent is 'Parent'.
    If the Matching array is provided then it will be filled
    with pointers to all of the components whose type and class
    match 'ConfigType' and 'ConfigClass'.  Also, the 'CurrentLength'
    is incremented by the number of nodes that match.

Arguments:

    Parent          - Supplies the root of the tree.
    ConfigClass     - Supplies the class to search for.
    ConfigType      - Supplies the type to search for.
    CurrentLength   - Supplies the current count.

Return Value:

    ESUCCESS

--*/
{
    PCONFIGURATION_COMPONENT    pc;
    ARC_STATUS                  r;

    if (!(pc = ArcGetChild(Parent))) {
        return ESUCCESS;
    }

    for (;;) {

        if ((!ConfigClass || pc->Class == *ConfigClass) &&
            (!ConfigType  || pc->Type  == *ConfigType)) {

            if (MatchingArray) {

                MatchingArray[*CurrentLength] = pc;
            }

            (*CurrentLength)++;
        }

        r = LowTraverseChildren(pc, ConfigClass, ConfigType,
                                MatchingArray, CurrentLength);

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

        if (!(pc = ArcGetPeer(pc))) {
            break;
        }
    }

    return ESUCCESS;
}


ARC_STATUS
LowQueryComponentList(
    IN  CONFIGURATION_CLASS*        ConfigClass OPTIONAL,
    IN  CONFIGURATION_TYPE*             ConfigType OPTIONAL,
    OUT PCONFIGURATION_COMPONENT**  ComponentList,
    OUT PULONG                      ListLength
    )
/*++

Routine Description:

    This routine returns an array of components whose class and
    type match the ones given.  (Since each parameter is optional,
    you can do type-only and class-only searches.)

    The array is allocated on the heap and contains pointers to
    the actual components (NOT copies).

Arguments:

    ConfigClass     - Supplies the configuation class to search for.
    ConfigType      - Supplies the configuration type to search for.
    ComponentList   - Returns a list of pointers to components whose
                        class and type match 'ConfigClass' and
                        'ConfigType'.
    ListLength      - Returns the number of components in the list.

Return Value:

    LowTraverseChildren, ENOMEM

--*/
{
    ARC_STATUS  r;

    *ListLength = 0;

    r = LowTraverseChildren(NULL, ConfigClass, ConfigType, NULL, ListLength);

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

    if (!(*ComponentList = (PCONFIGURATION_COMPONENT*) AllocateMemory(
            (*ListLength)*sizeof(PCONFIGURATION_COMPONENT)))) {

        return ENOMEM;
    }

    *ListLength = 0;

    return LowTraverseChildren(NULL, ConfigClass, ConfigType,
                               *ComponentList, ListLength);
}


ARC_STATUS
LowQueryPathList(
    IN  CONFIGURATION_CLASS*        ConfigClass OPTIONAL,
    IN  CONFIGURATION_TYPE*             ConfigType OPTIONAL,
    OUT PCHAR**                     PathList,
    OUT PULONG                      ListLength
    )
/*++

Routine Description:

    This routine returns a list of paths to the components that are
    of class ConfigClass and of type ConfigType.

Arguments:

    ConfigClass     - Supplies the configuation class to search for.
    ConfigType      - Supplies the configuration type to search for.
    PathList        - Returns a list of paths to the components.
    ListLength      - Returns the number of components in the list.

Return Value:

    LowQueryComponentList, LowQueryPathFromComponent

--*/
{
    PCONFIGURATION_COMPONENT*   component_list;
    ULONG                       list_length;
    ARC_STATUS                  r;
    ULONG                       i;
    PCHAR*                      path_list;

    r = LowQueryComponentList(ConfigClass, ConfigType,
                              &component_list, &list_length);

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

    if (!(path_list = (PCHAR*) AllocateMemory(list_length*sizeof(PCHAR)))) {
        FreeMemory(component_list);
        return ENOMEM;
    }


    for (i = 0; i < list_length; i++) {
        path_list[i] = NULL;
    }

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

        r = LowQueryPathFromComponent(component_list[i], &path_list[i]);

        if (r != ESUCCESS) {
            FreeMemory(component_list);
            LowFreePathList(path_list, list_length);
            return r;
        }
    }

    FreeMemory(component_list);

    *PathList = path_list;
    *ListLength = list_length;

    return ESUCCESS;
}


ARC_STATUS
LowFreePathList(
    IN  PCHAR*  PathList,
    IN  ULONG   ListLength
    )
/*++

Routine Description:

    This routine frees up the space taken by the path lists.

Arguments:

    PathList    - Supplies the paths.
    ListLength  - Supplies the number of paths.

Return Value:

    ESUCCESS

--*/
{
    ULONG i;

    for (i = 0; i < ListLength; i++) {
        if (PathList[i]) {
            FreeMemory(PathList[i]);
        }
    }
    FreeMemory(PathList);

    return ESUCCESS;
}


ARC_STATUS
LowQueryFdiskPathList(
    OUT PCHAR** PathList,
    OUT PULONG  ListLength
    )
/*++

Routine Description:

    This routine returns a list of paths to all the devices of interest
    to FDISK.

Arguments:

    PathList    - Returns a list of paths.
    ListLength  - Returns the length of the list.

Return Value:

    LowQueryComponentList, LowQueryPathFromComponent, ESUCCESS

--*/
{
    CONFIGURATION_TYPE  config_type;

    config_type = DiskPeripheral;
    return LowQueryPathList(NULL, &config_type, PathList, ListLength);
}


ARC_STATUS
LowFreeFdiskPathList(
    IN OUT  PCHAR*  PathList,
    IN      ULONG   ListLength
    )
/*++

Routine Description:

    This routine frees up the space taken by the path lists.

Arguments:

    PathList    - Supplies the paths.
    ListLength  - Supplies the number of paths.

Return Value:

    ESUCCESS

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


ARC_STATUS
LowGetDiskLayout(
    IN  PCHAR                      Path,
    OUT PDRIVE_LAYOUT_INFORMATION* DriveLayout
    )
{
    ARC_STATUS                status;
    ULONG                     Handle;
    ULONG                     i,ExtendedStart,BootSector,Entry;
    ULONG                     dummy,bps;
    BOOLEAN                   Link,mbr;
    PDRIVE_LAYOUT_INFORMATION DriveInfo;
    PPARTITION_INFORMATION    p;
    PCHAR                     SectorBuffer;
    PPARTITION_DESCRIPTOR     ptable;


#define PSTART(p)  (                                \
        (ULONG) ((p)->StartingSectorLsb0) +           \
        (ULONG) ((p)->StartingSectorLsb1 << 8) +      \
        (ULONG) ((p)->StartingSectorMsb0 << 16) +     \
        (ULONG) ((p)->StartingSectorMsb1 << 24) )

#define PLENGTH(p)   (                              \
        (ULONG) ((p)->PartitionLengthLsb0) +          \
        (ULONG) ((p)->PartitionLengthLsb1 << 8) +     \
        (ULONG) ((p)->PartitionLengthMsb0 << 16) +    \
        (ULONG) ((p)->PartitionLengthMsb1 << 24) )



    if((DriveInfo = AlAllocateHeap(sizeof(DRIVE_LAYOUT_INFORMATION) + (500*sizeof(PARTITION_INFORMATION)))) == NULL) {
        return(ENOMEM);
    }
    p = &DriveInfo->PartitionEntry[0];

    if((status = LowGetDriveGeometry(Path,&dummy,&bps,&dummy,&dummy)) != ESUCCESS) {
        AlDeallocateHeap(DriveInfo);
        return(status);
    }

    if((SectorBuffer = AlAllocateHeap(bps)) == NULL) {
        AlDeallocateHeap(DriveInfo);
        return(ENOMEM);
    }

    ptable = (PPARTITION_DESCRIPTOR)(SectorBuffer + (2*PARTITION_TABLE_OFFSET));

    if((status = LowOpenDisk(Path,&Handle)) != ESUCCESS) {
        AlDeallocateHeap(SectorBuffer);
        AlDeallocateHeap(DriveInfo);
        return(status);
    }

    mbr = TRUE;
    Entry = 0;
    BootSector = 0;
    ExtendedStart = 0;
    status = ESUCCESS;

    do {

        if((status = LowReadSectors(Handle,bps,BootSector,1,SectorBuffer)) != ESUCCESS) {
            break;
        }

        // This is to catch the case where there is no MBR yet.

        if(((PUSHORT)SectorBuffer)[BOOT_SIGNATURE_OFFSET] != BOOT_RECORD_SIGNATURE) {
            break;
        }

        Link = FALSE;

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

            if(ptable[i].PartitionType == SYSID_UNUSED) {

                // set as unused.

                p[Entry].PartitionType    = SYSID_UNUSED;
                p[Entry].BootIndicator    = FALSE;
                p[Entry].RewritePartition = FALSE;
                p[Entry].PartitionLength.QuadPart  = 0;
                p[Entry].HiddenSectors    = 0;
                p[Entry].StartingOffset.QuadPart   = 0;
                p[Entry].RecognizedPartition = FALSE;

            } else {

                LARGE_INTEGER   Result1, Result2;

                p[Entry].PartitionType    = ptable[i].PartitionType;
                p[Entry].BootIndicator    = ptable[i].ActiveFlag;
                p[Entry].RewritePartition = FALSE;
                p[Entry].PartitionLength.QuadPart  = UInt32x32To64(PLENGTH(ptable + i),bps);

                // BUGBUG (tedm) the following are not correct for link
                //               entries in the extended partition.
                //               But fdisk does not use these values in
                //               this case so blow it off.  As of
                //               11/13/91, IoReadPartitionTable has the
                //               same bug.

                p[Entry].HiddenSectors    = PSTART(ptable + i);
                Result1.QuadPart = UInt32x32To64(PSTART(ptable + i),bps);
                Result2.QuadPart = UInt32x32To64(BootSector,bps);
                p[Entry].StartingOffset.QuadPart = Result1.QuadPart + Result2.QuadPart;

                p[Entry].RecognizedPartition = TRUE;     // BUGBUG this is broken

                if(p[Entry].PartitionType == SYSID_EXTENDED) {

                    Link = TRUE;

                    if(mbr) {
                        mbr = FALSE;
                        BootSector = PSTART(ptable + i);
                        ExtendedStart = BootSector;
                    } else {
                        BootSector = ExtendedStart + PSTART(ptable + i);
                    }
                }
            }
            Entry++;
        }
    } while(Link);

    LowCloseDisk(Handle);

    AlDeallocateHeap(SectorBuffer);

    if(status != ESUCCESS) {

        AlDeallocateHeap(DriveInfo);
        return(status);
    }

    // reallocate DriveInfo, set PartitionCount field.
    // DriveInfo is shrinking.

    DriveInfo = AlReallocateHeap(DriveInfo,
                                   sizeof(DRIVE_LAYOUT_INFORMATION)
                                 + ((Entry - 1) * sizeof(PARTITION_INFORMATION))
                                );

    DriveInfo->PartitionCount = Entry;

    *DriveLayout = DriveInfo;

    return(ESUCCESS);
}


//
// cylinder/head/sector stuff placed in partition table
//

typedef struct _tagCHS {
    USHORT StartCylinder;
    UCHAR  StartHead;
    UCHAR  StartSector;
    USHORT EndCylinder;
    UCHAR  EndHead;
    UCHAR  EndSector;
} CHS, *PCHS;


VOID
CalculateCHSVals(
    IN  ULONG Start,
    IN  ULONG Size,
    IN  ULONG spt,
    IN  ULONG h,
    OUT PCHS  chs
    )
{
    ULONG spc = spt * h;        // sectors per cylinder
    ULONG r;
    ULONG End = Start+Size-1;

    chs->StartCylinder = (USHORT)(Start/spc);
    r = Start % spc;
    chs->StartHead = (UCHAR)(r / spt);
    chs->StartSector = (UCHAR)((r % spt) + 1);      // sector is 1-based

    chs->EndCylinder = (USHORT)(End/spc);
    r = End % spc;
    chs->EndHead = (UCHAR)(r / spt);
    chs->EndSector = (UCHAR)((r % spt) + 1);        // sector is 1-based
}


VOID
SetPartitionTableEntry(
    IN OUT PPARTITION_DESCRIPTOR p,
    IN     UCHAR                 Active,
    IN     UCHAR                 SysID,
    IN     ULONG                 RelativeSector,
    IN     ULONG                 SectorCount,
    IN     PCHS                  chs
    )
{
    // first get the easy ones out of the way.

    p->ActiveFlag    = Active;
    p->PartitionType = SysID;

    if(chs) {
        p->StartingTrack = chs->StartHead;
        p->EndingTrack   = chs->EndHead;
    } else {
        p->StartingTrack = 0;
        p->EndingTrack   = 0;
    }

    if(chs) {

        // pack sector/cyl values

        p->StartingCylinderLsb = (chs->StartSector & 0x3f) | ((chs->StartCylinder >> 2) & 0xc0);
        p->StartingCylinderMsb = (UCHAR)chs->StartCylinder;

        p->EndingCylinderLsb   = (chs->EndSector   & 0x3f) | ((chs->EndCylinder   >> 2) & 0xc0);
        p->EndingCylinderMsb   = (UCHAR)chs->EndCylinder;

    } else {

        p->StartingCylinderLsb = 0;
        p->StartingCylinderMsb = 0;

        p->EndingCylinderLsb   = 0;
        p->EndingCylinderMsb   = 0;
    }

    // now handle the relative and total sector counts

    p->StartingSectorLsb0  = (UCHAR)(RelativeSector >> 0 );
    p->StartingSectorLsb1  = (UCHAR)(RelativeSector >> 8 );
    p->StartingSectorMsb0  = (UCHAR)(RelativeSector >> 16);
    p->StartingSectorMsb1  = (UCHAR)(RelativeSector >> 24);

    p->PartitionLengthLsb0 = (UCHAR)(SectorCount    >> 0 );
    p->PartitionLengthLsb1 = (UCHAR)(SectorCount    >> 8 );
    p->PartitionLengthMsb0 = (UCHAR)(SectorCount    >> 16);
    p->PartitionLengthMsb1 = (UCHAR)(SectorCount    >> 24);
}


#define ZeroPartitionTableEntry(p) SetPartitionTableEntry(p,0,SYSID_UNUSED,0,0,NULL);

VOID
ZeroPartitionTable(
    PPARTITION_DESCRIPTOR PartitionTable
    )
{
    ULONG i;

    for(i=0; i<ENTRIES_PER_BOOTSECTOR; i++) {
        ZeroPartitionTableEntry(PartitionTable+i);
    }
}


ARC_STATUS
LowSetDiskLayout(
    IN PCHAR                     Path,
    IN PDRIVE_LAYOUT_INFORMATION DriveLayout
    )
{
    ARC_STATUS             status;
    ULONG                   dummy,bps,spt,h;
    PCHAR                   SectorBuffer;
    ULONG                   Handle;
    PPARTITION_DESCRIPTOR   PartitionTable;
    PPARTITION_INFORMATION  p;
    BOOLEAN                 mbr = TRUE,Update;
    ULONG                   BootSector = 0,ExtendedPartitionStart = 0,
                            NextBootSector;
    ULONG                   i,j,UsedCount;
    CHS                     chs;


#define SECCNT(l) ((ULONG)(((ULONGLONG)l.QuadPart)/((ULONGLONG)bps)))


    ASRT(DriveLayout->PartitionCount);

    if((status = LowGetDriveGeometry(Path,&dummy,&bps,&spt,&h)) != ESUCCESS) {
        return(status);
    }

    // allocate a buffer for sector I/O

    if((SectorBuffer = AlAllocateHeap(bps)) == NULL) {
        return(ENOMEM);
    }

    //
    // Use x86 bootcode as a template so the disk will boot an x86
    // if it is moved to disk0 on an x86 machine.
    //
    RtlMoveMemory(SectorBuffer,x86BootCode,min(X86BOOTCODE_SIZE,bps));

    ((PUSHORT)SectorBuffer)[BOOT_SIGNATURE_OFFSET] = BOOT_RECORD_SIGNATURE;
    PartitionTable = (PPARTITION_DESCRIPTOR)(&(((PUSHORT)SectorBuffer)[PARTITION_TABLE_OFFSET]));

    if((status = LowOpenDisk(Path,&Handle)) != ESUCCESS) {
        AlDeallocateHeap(SectorBuffer);
        return(status);
    }

    ASRT(!(DriveLayout->PartitionCount % ENTRIES_PER_BOOTSECTOR));
    for(i=0; i<DriveLayout->PartitionCount; i+=ENTRIES_PER_BOOTSECTOR) {

        Update = FALSE;
        UsedCount = 0;

        ZeroPartitionTable(PartitionTable);

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

            p = &DriveLayout->PartitionEntry[i+j];

            switch(p->PartitionType) {
            case SYSID_UNUSED:
                ZeroPartitionTableEntry(PartitionTable+j);
                break;

            case SYSID_EXTENDED:
                NextBootSector = SECCNT(p->StartingOffset);
                CalculateCHSVals(NextBootSector,SECCNT(p->PartitionLength),spt,h,&chs);
                SetPartitionTableEntry(PartitionTable+j,
                                       p->BootIndicator,
                                       SYSID_EXTENDED,
                                       SECCNT(p->StartingOffset) - ExtendedPartitionStart,
                                       SECCNT(p->PartitionLength),
                                       &chs
                                      );
                if(mbr) {
                    mbr = FALSE;
                    ExtendedPartitionStart = NextBootSector;
                }
                break;

            default:
                CalculateCHSVals(SECCNT(p->StartingOffset),SECCNT(p->PartitionLength),spt,h,&chs);
                SetPartitionTableEntry(PartitionTable+j,
                                       p->BootIndicator,
                                       p->PartitionType,
                                       SECCNT(p->StartingOffset) - BootSector,
                                       SECCNT(p->PartitionLength),
                                       &chs
                                      );
                break;
            }
            Update = Update || p->RewritePartition;

            if(p->PartitionType != SYSID_UNUSED) {
                UsedCount++;
            }
        }
        if(Update || !UsedCount) {
            if((status = LowWriteSectors(Handle,bps,BootSector,1,SectorBuffer)) != ESUCCESS) {
                LowCloseDisk(Handle);
                AlDeallocateHeap(SectorBuffer);
                return(status);
            }
        }
        BootSector = NextBootSector;
    }

    LowCloseDisk(Handle);
    AlDeallocateHeap(SectorBuffer);
    return(ESUCCESS);
}