summaryrefslogtreecommitdiffstats
path: root/private/ntos/boot/bldr/i386/initx86.c
blob: 699f53945840f34a915664701e61bd2a68375de8 (plain) (blame)
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
/*++

Copyright (c) 1992  Microsoft Corporation

Module Name:

    initx86.c

Abstract:

    Does any x86-specific initialization, then starts the common ARC osloader

Author:

    John Vert (jvert) 4-Nov-1993

Revision History:

--*/
#include "bldrx86.h"
#include "msg.h"

UCHAR BootPartitionName[80];
UCHAR KernelBootDevice[80];
UCHAR OsLoadFilename[100];
UCHAR OsLoaderFilename[100];
UCHAR SystemPartition[100];
UCHAR OsLoadPartition[100];
UCHAR OsLoadOptions[100];
UCHAR ConsoleInputName[50];
UCHAR MyBuffer[SECTOR_SIZE+32];
UCHAR ConsoleOutputName[50];
UCHAR X86SystemPartition[sizeof("x86systempartition=") + sizeof(BootPartitionName)];


VOID
BlStartup(
    IN PCHAR PartitionName
    )

/*++

Routine Description:

    Does x86-specific initialization, particularly presenting the boot.ini
    menu and running NTDETECT, then calls to the common osloader.

Arguments:

    PartitionName - Supplies the ARC name of the partition (or floppy) that
        setupldr was loaded from.

Return Value:

    Does not return

--*/

{
    PUCHAR Argv[10];
    ARC_STATUS Status;
    ULONG BootFileId;
    PCHAR BootFile;
    ULONG Read;
    PCHAR p;
    ULONG i;
    ULONG DriveId;
    ULONG FileSize;
    ULONG Count;
    LARGE_INTEGER SeekPosition;
    PCHAR LoadOptions = NULL;
    BOOLEAN UseTimeOut=TRUE;
    BOOLEAN AlreadyInitialized = FALSE;
    extern BOOLEAN FwDescriptorsValid;

    //
    // Open the boot partition so we can load boot drivers off it.
    //
    Status = ArcOpen(PartitionName, ArcOpenReadOnly, &DriveId);
    if (Status != ESUCCESS) {
        BlPrint("Couldn't open drive %s\n",PartitionName);
        BlPrint(BlFindMessage(BL_DRIVE_ERROR),PartitionName);
        goto BootFailed;
    }

    //
    // Initialize dbcs font and display support.
    //
    TextGrInitialize(DriveId);

    do {

        Status = BlOpen( DriveId,
                         "\\boot.ini",
                         ArcOpenReadOnly,
                         &BootFileId );

        BootFile = MyBuffer;

        RtlZeroMemory(MyBuffer, SECTOR_SIZE+32);

        if (Status != ESUCCESS) {
            BootFile[0]='\0';
        } else {
            //
            // Determine the length of the boot.ini file by reading to the end of
            // file.
            //

            FileSize = 0;
            do {
                Status = BlRead(BootFileId, BootFile, SECTOR_SIZE, &Count);
                if (Status != ESUCCESS) {
                    BlClose(BootFileId);
                    BlPrint(BlFindMessage(BL_READ_ERROR),Status);
                    BootFile[0] = '\0';
                    FileSize = 0;
                    Count = 0;
                    goto BootFailed;
                }

                FileSize += Count;
            } while (Count != 0);

            if (FileSize >= SECTOR_SIZE) {

                //
                // We need to allocate a bigger buffer, since the boot.ini file
                // is bigger than one sector.
                //

                BootFile=FwAllocateHeap(FileSize);
            }

            if (BootFile == NULL) {
                BlPrint(BlFindMessage(BL_READ_ERROR),ENOMEM);
                BootFile = MyBuffer;
                BootFile[0] = '\0';
                goto BootFailed;
            } else {

                SeekPosition.QuadPart = 0;
                Status = BlSeek(BootFileId,
                                &SeekPosition,
                                SeekAbsolute);
                if (Status != ESUCCESS) {
                    BlPrint(BlFindMessage(BL_READ_ERROR),Status);
                    BootFile = MyBuffer;
                    BootFile[0] = '\0';
                    goto BootFailed;
                } else {
                    Status = BlRead( BootFileId,
                                     BootFile,
                                     FileSize,
                                     &Read );

                    SeekPosition.QuadPart = 0;
                    Status = BlSeek(BootFileId,
                                    &SeekPosition,
                                    SeekAbsolute);
                    if (Status != ESUCCESS) {
                        BlPrint(BlFindMessage(BL_READ_ERROR),Status);
                        BootFile = MyBuffer;
                        BootFile[0] = '\0';
                        goto BootFailed;
                    } else {
                        BootFile[Read]='\0';
                    }
                }
            }

            //
            // Find Ctrl-Z, if it exists
            //

            p=BootFile;
            while ((*p!='\0') && (*p!=26)) {
                ++p;
            }
            if (*p != '\0') {
                *p='\0';
            }
            BlClose(BootFileId);
        }

        if (!AlreadyInitialized) {
            AbiosInitDataStructures();
        }

        MdShutoffFloppy();

        TextClearDisplay();

        p=BlSelectKernel(DriveId,BootFile, &LoadOptions, UseTimeOut);

        if (!AlreadyInitialized) {

            BlPrint(BlFindMessage(BL_NTDETECT_MSG));
            if (!BlDetectHardware(DriveId, LoadOptions)) {
                BlPrint(BlFindMessage(BL_NTDETECT_FAILURE));
                return;
            }

            TextClearDisplay();

            //
            // Initialize SCSI boot driver, if necessary.
            //
            if(!_strnicmp(p,"scsi(",5)) {
                AEInitializeIo(DriveId);
            }
            ArcClose(DriveId);
            //
            // Indicate that fw memory descriptors cannot be changed from
            // now on.
            //
            FwDescriptorsValid = FALSE;
        } else {
            TextClearDisplay();
        }

        //
        // Set AlreadyInitialized Flag to TRUE to indicate that ntdetect
        // and abios init routines have been run.
        //

        AlreadyInitialized = TRUE;

        //
        // Only time out the boot menu the first time through the boot.
        // For all subsequent reboots, the menu will stay up.
        //
        UseTimeOut=FALSE;

        i=0;
        while (*p !='\\') {
            KernelBootDevice[i] = *p;
            i++;
            p++;
        }
        KernelBootDevice[i] = '\0';

        strcpy(OsLoadFilename,"osloadfilename=");
        strcat(OsLoadFilename,p);

        //
        // We are fooling the OS Loader here.  It only uses the osloader= variable
        // to determine where to load HAL.DLL from.  Since x86 systems have no
        // "system partition" we want to load HAL.DLL from \nt\system\HAL.DLL.
        // So we pass that it as the osloader path.
        //

        strcpy(OsLoaderFilename,"osloader=");
        strcat(OsLoaderFilename,p);
        strcat(OsLoaderFilename,"\\System32\\NTLDR");

        strcpy(SystemPartition,"systempartition=");
        strcat(SystemPartition,KernelBootDevice);

        strcpy(OsLoadPartition,"osloadpartition=");
        strcat(OsLoadPartition,KernelBootDevice);

        strcpy(OsLoadOptions,"osloadoptions=");
        if (LoadOptions) {
            strcat(OsLoadOptions,LoadOptions);
        }


        strcpy(ConsoleInputName,"consolein=multi(0)key(0)keyboard(0)");

        strcpy(ConsoleOutputName,"consoleout=multi(0)video(0)monitor(0)");

        strcpy(X86SystemPartition,"x86systempartition=");
        strcat(X86SystemPartition,PartitionName);

        Argv[0]="load";

        Argv[1]=OsLoaderFilename;
        Argv[2]=SystemPartition;
        Argv[3]=OsLoadFilename;
        Argv[4]=OsLoadPartition;
        Argv[5]=OsLoadOptions;
        Argv[6]=ConsoleInputName;
        Argv[7]=ConsoleOutputName;
        Argv[8]=X86SystemPartition;

        Status = BlOsLoader(9,Argv,NULL);

    BootFailed:

        if (Status != ESUCCESS) {
            //
            // Boot failed, wait for reboot
            //
            while (TRUE) {
                GET_KEY();
            }
        } else {
            //
            // Need to reopen the drive
            //
            Status = ArcOpen(BootPartitionName, ArcOpenReadOnly, &DriveId);
            if (Status != ESUCCESS) {
                BlPrint(BlFindMessage(BL_DRIVE_ERROR),BootPartitionName);
                goto BootFailed;
            }

        }
    } while (TRUE);

}