summaryrefslogblamecommitdiffstats
path: root/private/ntos/boot/lib/hpfsboot.c
blob: 84d8d30481795f3bb51be0a45e09914f381ef218 (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
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515










































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































                                                                                                        
/*++

Copyright (c) 1991  Microsoft Corporation

Module Name:

    HpfsBoot.c

Abstract:

    This module implements the Hpfs boot file system used by the operating
    system loader.

Author:

    Gary Kimura     [GaryKi]    19-Jul-1991

Revision History:

--*/

//
//  Stuff to get around the fact that we include both Fat, Hpfs, and Ntfs include
//  environments
//

#define VBO ULONG
#define LBO ULONG

#include "bootlib.h"

BOOTFS_INFO HpfsBootFsInfo={L"pinball"};


//
//  Local procedure prototypes.
//

ARC_STATUS
HpfsReadDisk(
    IN ULONG DeviceId,
    IN ULONG Lbo,
    IN ULONG ByteCount,
    IN OUT PVOID Buffer
    );

VOID
HpfsFirstComponent(
    IN OUT PSTRING String,
    OUT PSTRING FirstComponent
    );

typedef enum _COMPARISON_RESULTS {
    LessThan = -1,
    EqualTo = 0,
    GreaterThan = 1
} COMPARISON_RESULTS;

COMPARISON_RESULTS
HpfsCompareNames(
    IN PSTRING Name1,
    IN PSTRING Name2
    );

ARC_STATUS
HpfsSearchDirectory(
    IN LBN Fnode,
    IN PSTRING Name,
    OUT PBOOLEAN IsDirectory,
    OUT PLBN FoundLbn,
    OUT PULONG FileSize
    );

ARC_STATUS
HpfsLoadMcb(
    IN LBN Fnode,
    IN VBN StartingVbn
    );

ARC_STATUS
HpfsVbnToLbn(
    IN VBN Vbn,
    OUT PLBN Lbn,
    OUT PULONG ByteCount
    );

//
//  The following macro upcases a single ascii character
//

#define ToUpper(C) ((((C) >= 'a') && ((C) <= 'z')) ? (C) - 'a' + 'A' : (C))

//
//  The following macro indicate if the flag is on or off
//

#define FlagOn(Flags,SingleFlag) ((BOOLEAN)(       \
    (((Flags) & (SingleFlag)) != 0 ? TRUE : FALSE) \
    )                                              \
)


//
//  Define global data.
//
//  Context Pointer - This is a pointer to the context for the current file
//      operation that is active.
//

PHPFS_STRUCTURE_CONTEXT HpfsStructureContext;

//
//  File Descriptor - This is a pointer to the file descriptor for the current
//      file operation that is active.
//

PBL_FILE_TABLE HpfsFileTableEntry;

//
//  File entry table - This is a structure that provides entry to the Hpfs
//      file system procedures. It is exported when a Hpfs file structure
//      is recognized.
//

BL_DEVICE_ENTRY_TABLE HpfsDeviceEntryTable;


PBL_DEVICE_ENTRY_TABLE
IsHpfsFileStructure (
    IN ULONG DeviceId,
    IN PVOID StructureContext
    )

/*++

Routine Description:

    This routine determines if the partition on the specified channel
    contains a Hpfs file system volume.

Arguments:

    DeviceId - Supplies the file table index for the device on which
        read operations are to be performed.

    StructureContext - Supplies a pointer to a Hpfs file structure context.

Return Value:

    A pointer to the Hpfs entry table is returned if the partition is
    recognized as containing a Hpfs volume. Otherwise, NULL is returned.

--*/

{
    ARC_STATUS Status;

    UCHAR UnalignedSuperSector[SECTOR_SIZE+256];
    UCHAR UnalignedSpareSector[SECTOR_SIZE+256];

    PSUPER_SECTOR SuperSector;
    PSPARE_SECTOR SpareSector;

    //
    //  Capture in our global variable the Hpfs Structure context record
    //

    HpfsStructureContext = (PHPFS_STRUCTURE_CONTEXT)StructureContext;
    RtlZeroMemory((PVOID)HpfsStructureContext, sizeof(HPFS_STRUCTURE_CONTEXT));

    //
    //  Compute the properly aligned buffers for reading in our sectors
    //

    SuperSector = ALIGN_BUFFER(UnalignedSuperSector);
    SpareSector = ALIGN_BUFFER(UnalignedSpareSector);

    //
    //  Read in the super and sector
    //

    if ((HpfsReadDisk(DeviceId, SUPER_SECTOR_LBN * 512, 512, SuperSector) != ESUCCESS) ||
        (HpfsReadDisk(DeviceId, SPARE_SECTOR_LBN * 512, 512, SpareSector) != ESUCCESS)) {

        return NULL;
    }

    //
    //  Check the signature for both sectors.
    //

    if ((SuperSector->Signature1 != SUPER_SECTOR_SIGNATURE1) ||
        (SuperSector->Signature2 != SUPER_SECTOR_SIGNATURE2) ||
        (SpareSector->Signature1 != SPARE_SECTOR_SIGNATURE1) ||
        (SpareSector->Signature2 != SPARE_SECTOR_SIGNATURE2)) {

        return NULL;
    }

    //
    //  Initialize the file entry table.
    //

    HpfsDeviceEntryTable.Open  = HpfsOpen;
    HpfsDeviceEntryTable.Close = HpfsClose;
    HpfsDeviceEntryTable.Read  = HpfsRead;
    HpfsDeviceEntryTable.Seek  = HpfsSeek;
    HpfsDeviceEntryTable.Write = HpfsWrite;
    HpfsDeviceEntryTable.GetFileInformation = HpfsGetFileInformation;
    HpfsDeviceEntryTable.SetFileInformation = HpfsSetFileInformation;
    HpfsDeviceEntryTable.BootFsInfo = &HpfsBootFsInfo;

    //
    //  And return the address of the table to our caller.
    //

    return &HpfsDeviceEntryTable;
}


ARC_STATUS
HpfsClose (
    IN ULONG FileId
    )

/*++

Routine Description:

    This routine closes the file specified by the file id.

Arguments:

    FileId - Supplies the file table index.

Return Value:

    ESUCCESS if returned as the function value.

--*/

{
    //
    //  Indicate that the file isn't open any longer
    //

    BlFileTable[FileId].Flags.Open = 0;

    //
    //  And return to our caller
    //

    return ESUCCESS;
}


ARC_STATUS
HpfsGetFileInformation (
    IN ULONG FileId,
    OUT PFILE_INFORMATION Buffer
    )

/*++

Routine Description:

    This procedure returns to the user a buffer filled with file information

Arguments:

    FileId - Supplies the File id for the operation

    Buffer - Supplies the buffer to receive the file information.  Note that
        it must be large enough to hold the full file name

Return Value:

    ESUCCESS is returned if the open operation is successful.  Otherwise,
    an unsuccessful status is returned that describes the reason for failure.

--*/

{
    ULONG i;

    HpfsFileTableEntry = &BlFileTable[FileId];

    //
    //  Zero out the buffer, and fill in its non-zero values
    //

    RtlZeroMemory(Buffer, sizeof(FILE_INFORMATION));

    Buffer->EndingAddress.LowPart = HpfsFileTableEntry->u.HpfsFileContext.FileSize;

    Buffer->CurrentPosition.LowPart = HpfsFileTableEntry->Position.LowPart;

    Buffer->FileNameLength = HpfsFileTableEntry->FileNameLength;

    for (i = 0; i < HpfsFileTableEntry->FileNameLength; i += 1) {

        Buffer->FileName[i] = HpfsFileTableEntry->FileName[i];
    }

    return ESUCCESS;
}


ARC_STATUS
HpfsOpen (
    IN PCHAR FileName,
    IN OPEN_MODE OpenMode,
    IN PULONG FileId
    )

/*++

Routine Description:

    This routine searches the root directory for a file matching FileName.
    If a match is found the dirent for the file is saved and the file is
    opened.

Arguments:

    FileName - Supplies a pointer to a zero terminated file name.

    OpenMode - Supplies the mode of the open.

    FileId - Supplies a pointer to a variable that specifies the file
        table entry that is to be filled in if the open is successful.

Return Value:

    ESUCCESS is returned if the open operation is successful. Otherwise,
    an unsuccessful status is returned that describes the reason for failure.

--*/

{
    ARC_STATUS Status;

    ULONG DeviceId;

    UCHAR UnalignedSuperSector[SECTOR_SIZE+256];
    PSUPER_SECTOR SuperSector;

    LBN Fnode;

    STRING PathName;

    //
    //  Save the address of the file table entry, context area, and the device
    //  id in use.
    //

    HpfsFileTableEntry = &BlFileTable[*FileId];
    HpfsStructureContext = (PHPFS_STRUCTURE_CONTEXT)HpfsFileTableEntry->StructureContext;

    DeviceId = HpfsFileTableEntry->DeviceId;

    //
    //  Compute the properly aligned buffers for reading in our sectors
    //

    SuperSector = ALIGN_BUFFER(UnalignedSuperSector);

    //
    //  Read in the Super sector.
    //

    if ((Status = HpfsReadDisk(DeviceId, SUPER_SECTOR_LBN * 512, 512, SuperSector)) != ESUCCESS) {

        return Status;
    }

    //
    //  Double check that the super sector is real
    //

    if ((SuperSector->Signature1 != SUPER_SECTOR_SIGNATURE1) ||
        (SuperSector->Signature2 != SUPER_SECTOR_SIGNATURE2)) {

        return EIO;
    }

    //
    //  Get the root fnode lbn
    //

    Fnode = SuperSector->RootDirectoryFnode;

    //
    // Construct a file name descriptor from the input file name.
    //

    RtlInitString( &PathName, FileName );

    //
    //  While the path name has some characters in it we'll go through our
    //  loop which extracts the first part of the path name and searches
    //  the current fnode (which must be a directory) for an the entry.
    //  If what we find is a directory then we have a new directory fnode
    //  and simply continue back to the top of the loop.
    //

    while (PathName.Length > 0) {

        STRING Name;
        BOOLEAN IsDirectory;
        ULONG FileSize;

        //
        //  Extract the first component and search the directory for a match, but
        //  first copy the first part to the file name buffer in the file table entry
        //

        for (HpfsFileTableEntry->FileNameLength = 0;
             (((USHORT)HpfsFileTableEntry->FileNameLength < PathName.Length) &&
              (PathName.Buffer[HpfsFileTableEntry->FileNameLength] != '\\'));
             HpfsFileTableEntry->FileNameLength += 1) {

            HpfsFileTableEntry->FileName[HpfsFileTableEntry->FileNameLength] =
                                         PathName.Buffer[HpfsFileTableEntry->FileNameLength];
        }

        HpfsFirstComponent( &PathName, &Name );

        if ((Status = HpfsSearchDirectory(Fnode, &Name, &IsDirectory, &Fnode, &FileSize)) != ESUCCESS) {

            return Status;
        }

        //
        //  If we didn't get back a directory then we're about to stop either
        //  with an error or with success
        //

        if (!IsDirectory) {

            //
            //  if the path name still has some characters in it then the
            //  caller wanted to continue but we can't because we're not
            //  currently sitting on a directory
            //

            if (PathName.Length > 0) {

                return ENOENT;
            }

            //
            //  Load in the mcb for the file, set the fnode in structure
            //  context, file size, open flags, position, and return
            //  success to our caller
            //

            if ((Status = HpfsLoadMcb(Fnode, 0)) != ESUCCESS) {

                return Status;
            }

            HpfsStructureContext->Fnode = Fnode;

            HpfsFileTableEntry->u.HpfsFileContext.FileSize = FileSize;

            HpfsFileTableEntry->Flags.Open = 1;
            HpfsFileTableEntry->Flags.Read = 1;
            HpfsFileTableEntry->Position.LowPart = 0;
            HpfsFileTableEntry->Position.HighPart = 0;

            return ESUCCESS;
        }
    }

    //
    //  If we reach here then the path name is exhausted and we didn't
    //  reach a file so return an error to our caller
    //

    return ENOENT;
}


ARC_STATUS
HpfsRead (
    IN ULONG FileId,
    OUT PVOID Buffer,
    IN ULONG Length,
    OUT PULONG Transfer
    )

/*++

Routine Description:

    This routine reads data from the specified file.

Arguments:

    FileId - Supplies the file table index.

    Buffer - Supplies a pointer to the buffer that receives the data
        read.

    Length - Supplies the number of bytes that are to be read.

    Transfer - Supplies a pointer to a variable that receives the number
        of bytes actually transfered.

Return Value:

    ESUCCESS is returned if the read operation is successful. Otherwise,
    an unsuccessful status is returned that describes the reason for failure.

--*/

{
    ARC_STATUS Status;

    ULONG DeviceId;
    ULONG RemainingSectorCount;

    //
    //  Save the address of the file table entry, context area, and the device
    //  id in use.
    //

    HpfsFileTableEntry = &BlFileTable[FileId];
    HpfsStructureContext = (PHPFS_STRUCTURE_CONTEXT)HpfsFileTableEntry->StructureContext;

    DeviceId = HpfsFileTableEntry->DeviceId;

    //
    //  Clear the transfer count
    //

    *Transfer = 0;

    //
    //  Read in runs (i.e., sectors) until the byte count goes to zero
    //

    while (Length > 0) {

        VBN Vbn;
        LBN Lbn;

        ULONG CurrentRunByteCount;

        //
        //  Compute the Vbn from the current byte position and then
        //  lookup the corresponding Lbn and current run length
        //

        Vbn = HpfsFileTableEntry->Position.LowPart / 512;

        if ((Status = HpfsVbnToLbn( Vbn, &Lbn, &CurrentRunByteCount )) != ESUCCESS) {

            return Status;
        }

        //
        //  Now bias the run size by the offset we are into the
        //  sector
        //

        CurrentRunByteCount -= (HpfsFileTableEntry->Position.LowPart & 511);

        //
        //  while there are sectors to be read in from the current run
        //  length and we haven't exhausted the request we loop reading
        //  in sectors.  The biggest request we'll handle is only 64
        //  contiguous sectors per physical read.  So we might need to loop
        //  through the run.
        //

        while ((Length > 0) && (CurrentRunByteCount > 0)) {

            ULONG i;
            ULONG Lbo;

            //
            //  Compute the size of the next physical read
            //

            i = (Length < (64 * 512) ? Length : (64 * 512));
            i = (i < CurrentRunByteCount ? i : CurrentRunByteCount);

            //
            //  Don't read beyond the eof
            //

            if (i + HpfsFileTableEntry->Position.LowPart >=
                    HpfsFileTableEntry->u.HpfsFileContext.FileSize) {

                i = HpfsFileTableEntry->u.HpfsFileContext.FileSize -
                    HpfsFileTableEntry->Position.LowPart;

                if (i == 0) {

                    return ESUCCESS;
                }

                Length = i;
            }

            //
            //  Compute the lbo to read, and then issue the read.
            //

            Lbo = (Lbn * 512) | (HpfsFileTableEntry->Position.LowPart & 511);

            if ((Status = HpfsReadDisk( DeviceId, Lbo, i, Buffer)) != ESUCCESS) {

                return Status;
            }

            //
            //  Update the remaining length, Current run byte count
            //  and new Lbn offset
            //

            Length -= i;
            CurrentRunByteCount -= i;

            Lbn += i/512;

            //
            //  Update the current position and the number of bytes transfered
            //

            HpfsFileTableEntry->Position.LowPart += i;
            *Transfer += i;

            //
            //  Update buffer to point to the next byte location to fill in
            //

            Buffer = (PCHAR)Buffer + i;
        }
    }

    //
    //  If we get here then remaining sector count is zero so we can
    //  return success to our caller
    //

    return ESUCCESS;
}


ARC_STATUS
HpfsSeek (
    IN ULONG FileId,
    IN PLARGE_INTEGER Offset,
    IN SEEK_MODE SeekMode
    )

/*++

Routine Description:

    This routine seeks to the specified position for the file specified
    by the file id.

Arguments:

    FileId - Supplies the file table index.

    Offset - Supplies the offset in the file to position to.

    SeekMode - Supplies the mode of the seek operation.

Return Value:

    ESUCCESS if returned as the function value.

--*/

{
    ULONG NewPosition;

    //
    //  Compute the new position
    //

    if (SeekMode == SeekAbsolute) {

        NewPosition = Offset->LowPart;

    } else {

        NewPosition = BlFileTable[FileId].Position.LowPart + Offset->LowPart;
    }

    //
    //  If the new position is greater than the file size then return
    //  an error
    //

    if (NewPosition > BlFileTable[FileId].u.HpfsFileContext.FileSize) {

        return EINVAL;
    }

    //
    //  Otherwise set the new position and return to our caller
    //

    BlFileTable[FileId].Position.LowPart = NewPosition;

    return ESUCCESS;
}


ARC_STATUS
HpfsSetFileInformation (
    IN ULONG FileId,
    IN ULONG AttributeFlags,
    IN ULONG AttributeMask
    )

/*++

Routine Description:

    This routine sets the file attributes of the indicated file

Arguments:

    FileId - Supplies the File Id for the operation

    AttributeFlags - Supplies the value (on or off) for each attribute being modified

    AttributeMask - Supplies a mask of the attributes being altered.  All other
        file attributes are left alone.

Return Value:

    ESUCCESS is returned if the read operation is successful.  Otherwise,
    an unsuccessful status is returned that describes the reason for failure.

--*/

{
    return EROFS;
}


ARC_STATUS
HpfsWrite (
    IN ULONG FileId,
    IN PVOID Buffer,
    IN ULONG Length,
    OUT PULONG Transfer
    )

/*++

Routine Description:

    This routine writes data to the specified file.

Arguments:

    FileId - Supplies the file table index.

    Buffer - Supplies a pointer to the buffer that contains the data
        written.

    Length - Supplies the number of bytes that are to be written.

    Transfer - Supplies a pointer to a variable that receives the number
        of bytes actually transfered.

Return Value:

    ESUCCESS is returned if the write operation is successful. Otherwise,
    an unsuccessful status is returned that describes the reason for failure.

--*/

{
    return EROFS;
}


ARC_STATUS
HpfsInitialize (
    VOID
    )

/*++

Routine Description:

    This routine initializes the hpfs boot filesystem.
    Currently this is a no-op.

Arguments:

    None.

Return Value:

    ESUCCESS.

--*/

{
    return ESUCCESS;
}


//
//  Internal support routine
//

ARC_STATUS
HpfsReadDisk(
    IN ULONG DeviceId,
    IN ULONG Lbo,
    IN ULONG ByteCount,
    IN OUT PVOID Buffer
    )

/*++

Routine Description:

    This routine reads in zero or more sectors from the specified device.

Arguments:

    DeviceId - Supplies the device id to use in the arc calls.

    Lbo - Supplies the LBO to start reading from.

    ByteCount - Supplies the number of bytes to read.

    Buffer - Supplies a pointer to the buffer to read the bytes into.

Return Value:

    ESUCCESS is returned if the read operation is successful. Otherwise,
    an unsuccessful status is returned that describes the reason for failure.

--*/

{
    LARGE_INTEGER LargeLbo;
    ARC_STATUS Status;
    ULONG i;

    //
    //  Special case the zero byte read request
    //

    if (ByteCount == 0) {

        return ESUCCESS;
    }

    //
    //  Seek to the appropriate offset on the volume
    //

    LargeLbo.LowPart = Lbo;
    LargeLbo.HighPart = 0;

    if ((Status = ArcSeek( DeviceId, &LargeLbo, SeekAbsolute )) != ESUCCESS) {

        return Status;
    }

    //
    //  Issue the arc read request
    //

    if ((Status = ArcRead( DeviceId, Buffer, ByteCount, &i)) != ESUCCESS) {

        return Status;
    }

    //
    //  Make sure we got back the amount requested
    //

    if (ByteCount != i) {

        return EIO;
    }

    //
    //  Everything is fine so return success to our caller
    //

    return ESUCCESS;
}


//
//  Internal support routine
//

VOID
HpfsFirstComponent(
    IN OUT PSTRING String,
    OUT PSTRING FirstComponent
    )

/*++

Routine Description:

    This routine takes an input path name and separates it into its
    first file name component and the remaining part.

Arguments:

    String - Supplies the original string being dissected.  On return
        this string will now point to the remaining part.

    FirstComponent - Returns the string representing the first file name
        in the input string.

Return Value:

    None.

--*/

{
    ULONG Index;

    //
    //  Copy over the string variable into the first component variable
    //

    *FirstComponent = *String;

    //
    //  Now if the first character in the name is a backslash then
    //  simply skip over the backslash.
    //

    if (FirstComponent->Buffer[0] == '\\') {

        FirstComponent->Buffer += 1;
        FirstComponent->Length -= 1;
    }

    //
    //  Now search the name for a backslash
    //

    for (Index = 0; Index < FirstComponent->Length; Index += 1) {

        if (FirstComponent->Buffer[Index] == '\\') {

            break;
        }
    }

    //
    //  At this point Index denotes a backslash or is equal to the length
    //  of the string.  So update string to be the remaining part.
    //  Decrement the length of the first component by the approprate
    //  amount
    //

    String->Buffer = &FirstComponent->Buffer[Index];
    String->Length = (SHORT)(FirstComponent->Length - Index);

    FirstComponent->Length = (SHORT)Index;

    //
    //  And return to our caller.
    //

    return;
}


//
//  Internal support routine
//

COMPARISON_RESULTS
HpfsCompareNames(
    IN PSTRING Name1,
    IN PSTRING Name2
    )

/*++

Routine Description:

    This routine takes two names and compare them ignoring case.  This
    routine does not do implied dot or dbcs processing.

Arguments:

    Name1 - Supplies the first name to compare

    Name2 - Supplies the second name to compare

Return Value:

    LessThan    if Name1 is lexically less than Name2
    EqualTo     if Name1 is lexically equal to Name2
    GreaterThan if Name1 is lexically greater than Name2

--*/

{
    ULONG i;
    ULONG MinimumLength;

    //
    //  Compute the smallest of the two name lengths
    //

    MinimumLength = (Name1->Length < Name2->Length ? Name1->Length : Name2->Length);

    //
    //  Now compare each character in the names.
    //

    for (i = 0; i < MinimumLength; i += 1) {

        if ((UCHAR)(ToUpper(Name1->Buffer[i])) < (UCHAR)(ToUpper(Name2->Buffer[i]))) {

            return LessThan;
        }

        if ((UCHAR)(ToUpper(Name1->Buffer[i])) > (UCHAR)(ToUpper(Name2->Buffer[i]))) {

            return GreaterThan;
        }
    }

    //
    //  The names compared equal up to the smallest name length so
    //  now check the name lengths
    //

    if (Name1->Length < Name2->Length) {

        return LessThan;
    }

    if (Name1->Length > Name2->Length) {

        return GreaterThan;
    }

    return EqualTo;
}


//
//  Internal support routine
//

ARC_STATUS
HpfsSearchDirectory(
    IN LBN Fnode,
    IN PSTRING Name,
    OUT PBOOLEAN IsDirectory,
    OUT PLBN FoundLbn,
    OUT PULONG FileSize
    )

/*++

Routine Description:

    This routine searches the indicated directory for a matching name

Arguments:

    Fnode - Supplies the fnode of the directory to search

    Name - Supplies the name to search for

    Directory - Recieves an indication of the found file is a directory
        or a file.

    FoundLbn - Receives the lbn of the fnode for the file/directory if
        one is found.

    FileSize - Receives the size of the file if one is found.

Return Value:

    ESUCCESS is returned if the operation is successful. Otherwise,
    an unsuccessful status is returned that describes the reason for failure.

--*/

{
    ARC_STATUS Status;

    ULONG DeviceId;

    UCHAR UnalignedFnodeSector[SECTOR_SIZE+256];
    UCHAR UnalignedDirDiskBuffer[(SECTOR_SIZE*4)+256];

    PFNODE_SECTOR FnodeSector;
    PDIRECTORY_DISK_BUFFER DirDiskBuffer;

    LBN DirDiskBufferLbn;

    PPBDIRENT Dirent;

    //
    //  Compute the properly aligned buffers for reading in our sectors
    //

    FnodeSector   = ALIGN_BUFFER(UnalignedFnodeSector);
    DirDiskBuffer = ALIGN_BUFFER(UnalignedDirDiskBuffer);

    //
    //  Capture the device id from our global variable
    //

    DeviceId = HpfsFileTableEntry->DeviceId;

    //
    //  Read in the fnode for the directory, and check that it is real
    //

    if ((Status = HpfsReadDisk(DeviceId, Fnode * 512, 512, FnodeSector)) != ESUCCESS) {

        return Status;
    }

    if (FnodeSector->Signature != FNODE_SECTOR_SIGNATURE) {

        return EIO;
    }

    //
    //  Now setup the lbn for the first dir disk buffer
    //

    DirDiskBufferLbn = FnodeSector->Allocation.Leaf[0].Lbn;

    //
    //  the following loop is executed until we either find our entry
    //  or have gone past where it could possible be
    //

    while (TRUE) {

        BOOLEAN ReadNewDirDiskBuffer;

        //
        //  Read in the next dir disk buffer, and check that it is real
        //

        if ((Status = HpfsReadDisk( DeviceId,
                                    DirDiskBufferLbn * 512,
                                    512 * 4,
                                    DirDiskBuffer )) != ESUCCESS) {

            return Status;
        }

        if (DirDiskBuffer->Signature != DIRECTORY_DISK_BUFFER_SIGNATURE) {

            return EIO;
        }

        //
        //  Search each dirent in the dir disk buffer, we continue the
        //  loop until we either exit or read new dir disk buffer is set
        //  to true.
        //

        ReadNewDirDiskBuffer = FALSE;

        for ( Dirent = (PPBDIRENT)GetFirstDirent( DirDiskBuffer );
              !ReadNewDirDiskBuffer;
              Dirent = (PPBDIRENT)GetNextDirent( Dirent )) {

            STRING String;
            COMPARISON_RESULTS CompareResults;

            //
            //  Get a string for the file name in the dirent and then
            //  compare the names against each other
            //

            String.Length = Dirent->FileNameLength;
            String.Buffer = &Dirent->FileName[0];

            CompareResults = HpfsCompareNames( Name, &String );

            //
            //  If the names are equal then we've found our match and we
            //  need to figure out if this is a directory, store the
            //  found fnode and return success
            //

            if (CompareResults == EqualTo) {

                *IsDirectory = FlagOn(Dirent->FatFlags, FAT_DIRENT_ATTR_DIRECTORY);

                *FoundLbn = Dirent->Fnode;
                *FileSize = Dirent->FileSize;

                return ESUCCESS;

            //
            //  If the results is less than then we've gone too far in
            //  the current dir disk buffer.  If we have a down pointer then
            //  there are other buffers to search through otherwise the
            //  name doesn't exist in the directory
            //

            } else if (CompareResults == LessThan) {

                if (FlagOn(Dirent->Flags, DIRENT_BTREE_POINTER)) {

                    //
                    //  Compute the new dir disk buffer to search and
                    //  indicate to the for loop that we need to read in
                    //  another dir disk buffer
                    //

                    DirDiskBufferLbn = GetBtreePointerInDirent( Dirent );

                    ReadNewDirDiskBuffer = TRUE;

                } else {

                    //
                    //  We didn't find the name in the directory
                    //

                    return ENOENT;
                }

            //
            //  Otherwise the result is greater than which means we need to
            //  compare against the next dirent.
            //

            } else {

                NOTHING;
            }
        }
    }
}


//
//  Internal support routine
//

ARC_STATUS
HpfsLoadMcb(
    IN LBN Fnode,
    IN VBN StartingVbn
    )

/*++

Routine Description:

    This routine loads into our cache (i.e., structure context's boot mcb)
    the retrieval information for the starting vbn.

Arguments:

    Fnode - Supplies the lbn for the fnode for the file we're reading

    StartingVbn - Supplies the Vbn that we want to load

Return Value:

    ESUCCESS is returned if the operation is successful. Otherwise,
    an unsuccessful status is returned that describes the reason for failure.

--*/

{
    ARC_STATUS Status;

    PHPFS_BOOT_MCB Mcb = &HpfsStructureContext->BootMcb;
    ULONG DeviceId;

    UCHAR UnalignedFnodeSector[SECTOR_SIZE+256];
    UCHAR UnalignedAllocationSector[SECTOR_SIZE+256];

    PFNODE_SECTOR FnodeSector;
    PALLOCATION_SECTOR AllocationSector;

    PALLOCATION_HEADER AllocationHeader;
    PALLOCATION_LEAF Leafs;
    PALLOCATION_NODE Nodes;

    ULONG i;

    //
    //  Compute the properly aligned buffers for reading in our sectors
    //

    FnodeSector      = ALIGN_BUFFER(UnalignedFnodeSector);
    AllocationSector = ALIGN_BUFFER(UnalignedAllocationSector);

    //
    //  Capture the device id from our global variable
    //

    DeviceId = HpfsFileTableEntry->DeviceId;

    //
    //  Read in the fnode for the file, and check that it is real
    //

    if ((Status = HpfsReadDisk(DeviceId, Fnode * 512, 512, FnodeSector)) != ESUCCESS) {

        return Status;
    }

    if (FnodeSector->Signature != FNODE_SECTOR_SIGNATURE) {

        return EIO;
    }

    //
    //  Setup the allocation header, leafs and nodes
    //

    AllocationHeader = &FnodeSector->AllocationHeader;
    Leafs = &FnodeSector->Allocation.Leaf[0];
    Nodes = &FnodeSector->Allocation.Node[0];

    //
    //  While we have nodes and not leafs we need to search for the entry
    //  containing our starting vbn and then subsearch in that allocation
    //  sector
    //

    while (FlagOn(AllocationHeader->Flags, ALLOCATION_BLOCK_NODE)) {

        for (i = 0; i < AllocationHeader->OccupiedCount; i += 1) {

            if (StartingVbn < Nodes[i].Vbn) {

                //
                //  We found a node that contains our starting vbn so
                //  read in the next allocation sector and check that
                //  it is real.

                if ((Status = HpfsReadDisk( DeviceId,
                                            Nodes[i].Lbn * 512,
                                            512,
                                            AllocationSector )) != ESUCCESS) {

                    return Status;
                }

                if (AllocationSector->Signature != ALLOCATION_SECTOR_SIGNATURE) {

                    return EIO;
                }

                //
                //  Setup the allocation header, leafs and nodes, and then
                //  break out of the for loop and let our while loop check
                //  if we have  allocation leafs or nodes.
                //

                AllocationHeader = &AllocationSector->AllocationHeader;
                Leafs = &AllocationSector->Allocation.Leaf[0];
                Nodes = &AllocationSector->Allocation.Node[0];

                break;
            }
        }
    }

    //
    //  Now the allocation header indictes that we have leaf entries
    //  so we can simply load up the cached mcb.  We set the in use here
    //  The most entries we'll ever preload is the maximum available in
    //  an allocation sector (i.e., 40).
    //

    Mcb->InUse = AllocationHeader->OccupiedCount;

    for (i = 0; i < AllocationHeader->OccupiedCount; i += 1) {

        //
        //  For each entry we set the vbn and lbn value.  We also
        //  set the next vbn value to get the size of the run.
        //  This means that we'll really be double setting each vbn
        //  value (except the first and last entry) but that's okay
        //  because they better compute the same value
        //

        Mcb->Vbn[i] = Leafs[i].Vbn;
        Mcb->Lbn[i] = Leafs[i].Lbn;
        Mcb->Vbn[i+1] = Leafs[i].Vbn + Leafs[i].Length;
    }

    //
    //  We're all done so return success to our caller
    //

    return ESUCCESS;
}


//
//  Internal support routine
//

ARC_STATUS
HpfsVbnToLbn(
    IN VBN Vbn,
    OUT PLBN Lbn,
    OUT PULONG ByteCount
    )

/*++

Routine Description:

    This routine computes the run denoted by the input vbn to into its
    corresponding lbn and also returns the number of bytes remaining in
    the run.  For all cases this byte count will be a multiple of 512.

Arguments:

    Vbn - Supplies the Vbn to match

    Lbn - Recieves the corresponding Lbn

    ByteCount - Receives the number of bytes remaining in the run

Return Value:

    ESUCCESS is returned if the operation is successful. Otherwise,
    an unsuccessful status is returned that describes the reason for failure.

--*/

{
    ARC_STATUS Status;

    PHPFS_BOOT_MCB Mcb = &HpfsStructureContext->BootMcb;
    ULONG i;

    //
    //  Check if the boot mcb has the range we're asking for.  If it
    //  doesn't then call load mcb to load in the right range.
    //

    if ((Vbn < Mcb->Vbn[0]) || (Vbn >= Mcb->Vbn[Mcb->InUse])) {

        if ((Status = HpfsLoadMcb(HpfsStructureContext->Fnode, Vbn)) != ESUCCESS) {

            return Status;
        }
    }

    //
    //  Now search for the slot where the Vbn fits in the mcb.  Note that
    //  we could also do a binary search here but because the run count
    //  is probably small the extra overhead of a binary search doesn't
    //  buy us anything
    //

    for (i = 0; i < Mcb->InUse; i += 1) {

        //
        //  We found our slot if the vbn we're after is less then the
        //  next mcb's vbn
        //

        if (Vbn < Mcb->Vbn[i+1]) {

            //
            //  Compute the corresponding lbn which is the stored lbn plus
            //  the difference between the stored vbn and the vbn we're
            //  looking up.  Also compute the byte count which is the
            //  difference between the current vbn we're looking up and
            //  the vbn for the next run, all multiplied by 512.
            //

            *Lbn = Mcb->Lbn[i] + (Vbn - Mcb->Vbn[i]);

            *ByteCount = (Mcb->Vbn[i+1] - Vbn) * 512;

            //
            //  and return success to our caller
            //

            return ESUCCESS;
        }
    }

    //
    //  If we really reach here we have an error in the mcb
    //

    return EIO;
}