- Apply 28852 changes to setupldr.c (fixes bootcd).
[reactos.git] / reactos / boot / freeldr / freeldr / reactos / setupldr.c
1 /*
2 * FreeLoader
3 *
4 * Copyright (C) 1998-2003 Brian Palmer <brianp@sginet.com>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 */
20
21 #define _NTSYSTEM_
22 #include <freeldr.h>
23 #include <debug.h>
24
25 extern ULONG PageDirectoryStart;
26 extern ULONG PageDirectoryEnd;
27
28 ROS_LOADER_PARAMETER_BLOCK LoaderBlock;
29 char reactos_kernel_cmdline[255]; // Command line passed to kernel
30 LOADER_MODULE reactos_modules[64]; // Array to hold boot module info loaded for the kernel
31 char reactos_module_strings[64][256]; // Array to hold module names
32 unsigned long reactos_memory_map_descriptor_size;
33 memory_map_t reactos_memory_map[32]; // Memory map
34 char szBootPath[256];
35 char szHalName[256];
36 CHAR SystemRoot[255];
37 extern ULONG_PTR KernelBase, KernelEntryPoint;
38
39 extern BOOLEAN FrLdrLoadDriver(PCHAR szFileName, INT nPos);
40
41 #define USE_UI
42
43 BOOLEAN
44 NTAPI
45 static FrLdrLoadKernel(IN PCHAR szFileName,
46 IN INT nPos)
47 {
48 PFILE FilePointer;
49 PCHAR szShortName;
50 CHAR szBuffer[256];
51 PVOID LoadBase;
52 PIMAGE_NT_HEADERS NtHeader;
53
54 /* Extract Kernel filename without path */
55 szShortName = strrchr(szFileName, '\\');
56 if (!szShortName)
57 {
58 /* No path, leave it alone */
59 szShortName = szFileName;
60 }
61 else
62 {
63 /* Skip the path */
64 szShortName = szShortName + 1;
65 }
66
67 /* Open the Kernel */
68 FilePointer = FsOpenFile(szFileName);
69 if (!FilePointer)
70 {
71 /* Return failure on the short name */
72 strcpy(szBuffer, szShortName);
73 strcat(szBuffer, " not found.");
74 UiMessageBox(szBuffer);
75 return FALSE;
76 }
77
78 /* Update the status bar with the current file */
79 strcpy(szBuffer, "Reading ");
80 strcat(szBuffer, szShortName);
81 UiDrawStatusText(szBuffer);
82
83 /* Do the actual loading */
84 LoadBase = FrLdrMapImage(FilePointer, szShortName, 1);
85
86 /* Get the NT header, kernel base and kernel entry */
87 NtHeader = RtlImageNtHeader(LoadBase);
88 KernelBase = NtHeader->OptionalHeader.ImageBase;
89 KernelEntryPoint = KernelBase + NtHeader->OptionalHeader.AddressOfEntryPoint;
90 LoaderBlock.KernelBase = KernelBase;
91
92 /* Update Processbar and return success */
93 return TRUE;
94 }
95
96 static BOOLEAN
97 LoadDriver(PCSTR szSourcePath, PCSTR szFileName)
98 {
99 return FrLdrLoadDriver((PCHAR)szFileName, 0);
100 }
101
102
103 static BOOLEAN
104 LoadNlsFile(PCSTR szSourcePath, PCSTR szFileName, PCSTR szModuleName)
105 {
106 CHAR szFullName[256];
107 #ifdef USE_UI
108 CHAR szBuffer[80];
109 #endif
110 PFILE FilePointer;
111 PCSTR szShortName;
112
113 if (szSourcePath[0] != '\\')
114 {
115 strcpy(szFullName, "\\");
116 strcat(szFullName, szSourcePath);
117 }
118 else
119 {
120 strcpy(szFullName, szSourcePath);
121 }
122
123 if (szFullName[strlen(szFullName)] != '\\')
124 {
125 strcat(szFullName, "\\");
126 }
127
128 if (szFileName[0] != '\\')
129 {
130 strcat(szFullName, szFileName);
131 }
132 else
133 {
134 strcat(szFullName, szFileName + 1);
135 }
136
137 szShortName = strrchr(szFileName, '\\');
138 if (szShortName == NULL)
139 szShortName = szFileName;
140 else
141 szShortName = szShortName + 1;
142
143
144 FilePointer = FsOpenFile(szFullName);
145 if (FilePointer == NULL)
146 {
147 printf("Could not find %s\n", szFileName);
148 return(FALSE);
149 }
150
151 /*
152 * Update the status bar with the current file
153 */
154 #ifdef USE_UI
155 sprintf(szBuffer, "Setup is loading files (%s)", szShortName);
156 UiDrawStatusText(szBuffer);
157 #else
158 printf("Reading %s\n", szShortName);
159 #endif
160
161 /* Load the driver */
162 FrLdrLoadModule(FilePointer, szModuleName, NULL);
163
164 return(TRUE);
165 }
166
167 VOID RunLoader(VOID)
168 {
169 ULONG_PTR Base;
170 ULONG Size;
171 const char *SourcePath;
172 const char *LoadOptions = "", *DbgLoadOptions = "";
173 char szKernelName[256];
174
175 HINF InfHandle;
176 ULONG ErrorLine;
177 INFCONTEXT InfContext;
178
179 /* Setup multiboot information structure */
180 LoaderBlock.CommandLine = reactos_kernel_cmdline;
181 LoaderBlock.PageDirectoryStart = (ULONG)&PageDirectoryStart;
182 LoaderBlock.PageDirectoryEnd = (ULONG)&PageDirectoryEnd;
183 LoaderBlock.ModsCount = 0;
184 LoaderBlock.ModsAddr = reactos_modules;
185 LoaderBlock.MmapLength = (unsigned long)MachGetMemoryMap((PBIOS_MEMORY_MAP)reactos_memory_map, 32) * sizeof(memory_map_t);
186 if (LoaderBlock.MmapLength)
187 {
188 ULONG i;
189
190 LoaderBlock.MmapAddr = (unsigned long)&reactos_memory_map;
191 reactos_memory_map_descriptor_size = sizeof(memory_map_t); // GetBiosMemoryMap uses a fixed value of 24
192 for (i=0; i<(LoaderBlock.MmapLength/sizeof(memory_map_t)); i++)
193 {
194 if (BiosMemoryUsable == reactos_memory_map[i].type &&
195 0 == reactos_memory_map[i].base_addr_low)
196 {
197 LoaderBlock.MemLower = (reactos_memory_map[i].base_addr_low + reactos_memory_map[i].length_low) / 1024;
198 if (640 < LoaderBlock.MemLower)
199 {
200 LoaderBlock.MemLower = 640;
201 }
202 }
203 if (BiosMemoryUsable == reactos_memory_map[i].type &&
204 reactos_memory_map[i].base_addr_low <= 1024 * 1024 &&
205 1024 * 1024 <= reactos_memory_map[i].base_addr_low + reactos_memory_map[i].length_low)
206 {
207 LoaderBlock.MemHigher = (reactos_memory_map[i].base_addr_low + reactos_memory_map[i].length_low) / 1024 - 1024;
208 }
209 }
210 }
211
212 #ifdef USE_UI
213 SetupUiInitialize();
214 UiDrawStatusText("");
215 #endif
216
217 extern BOOLEAN FrLdrBootType;
218 FrLdrBootType = TRUE;
219
220 /* Initialize registry */
221 RegInitializeRegistry();
222
223 /* Detect hardware */
224 #ifdef USE_UI
225 UiDrawStatusText("Detecting hardware...");
226 #else
227 printf("Detecting hardware...\n\n");
228 #endif
229 MachHwDetect();
230 #ifdef USE_UI
231 UiDrawStatusText("");
232 #endif
233
234 /* set boot device */
235 MachDiskGetBootDevice(&LoaderBlock.BootDevice);
236
237 /* Open boot drive */
238 if (!FsOpenBootVolume())
239 {
240 #ifdef USE_UI
241 UiMessageBox("Failed to open boot drive.");
242 #else
243 printf("Failed to open boot drive.");
244 #endif
245 return;
246 }
247
248 /* Open 'txtsetup.sif' */
249 if (!InfOpenFile (&InfHandle,
250 MachDiskBootingFromFloppy() ? "\\txtsetup.sif" : "\\reactos\\txtsetup.sif",
251 &ErrorLine))
252 {
253 printf("Failed to open 'txtsetup.sif'\n");
254 return;
255 }
256
257 #ifdef DBG
258 /* Get load options */
259 if (InfFindFirstLine (InfHandle,
260 "SetupData",
261 "DbgOsLoadOptions",
262 &InfContext))
263 {
264 if (!InfGetDataField (&InfContext, 1, &DbgLoadOptions))
265 DbgLoadOptions = "";
266 }
267 #endif
268 if (!strlen(DbgLoadOptions) && !InfFindFirstLine (InfHandle,
269 "SetupData",
270 "OsLoadOptions",
271 &InfContext))
272 {
273 printf("Failed to find 'SetupData/OsLoadOptions'\n");
274 return;
275 }
276
277 if (!InfGetDataField (&InfContext,
278 1,
279 &LoadOptions))
280 {
281 printf("Failed to get load options\n");
282 return;
283 }
284 #if 0
285 printf("LoadOptions: '%s'\n", LoadOptions);
286 #endif
287
288 if (MachDiskBootingFromFloppy())
289 {
290 /* Boot from floppy disk */
291 SourcePath = "\\";
292 }
293 else
294 {
295 /* Boot from cdrom */
296 SourcePath = "\\reactos";
297 }
298
299 /* Set kernel command line */
300 MachDiskGetBootPath(reactos_kernel_cmdline, sizeof(reactos_kernel_cmdline));
301 strcat(strcat(strcat(strcat(reactos_kernel_cmdline, SourcePath), " "),
302 LoadOptions), DbgLoadOptions);
303
304 strcpy(SystemRoot, SourcePath);
305 strcat(SystemRoot, "\\");
306
307 /* Setup the boot path and kernel path */
308 strcpy(szBootPath, SourcePath);
309 strcpy(szKernelName, szBootPath);
310 strcat(szKernelName, "\\ntoskrnl.exe");
311
312 /* Setup the HAL path */
313 strcpy(szHalName, szBootPath);
314 strcat(szHalName, "\\hal.dll");
315
316 /* Load the kernel */
317 if (!FrLdrLoadKernel(szKernelName, 5)) return;
318
319 /* Export the hardware hive */
320 Base = FrLdrCreateModule ("HARDWARE");
321 RegExportBinaryHive (L"\\Registry\\Machine\\HARDWARE", (PVOID)Base, &Size);
322 FrLdrCloseModule (Base, Size);
323
324 #if 0
325 printf("Base: %x\n", Base);
326 printf("Size: %u\n", Size);
327 printf("*** System stopped ***\n");
328 for(;;);
329 #endif
330
331 /* Insert boot disk 2 */
332 if (MachDiskBootingFromFloppy())
333 {
334 #ifdef USE_UI
335 UiMessageBox("Please insert \"ReactOS Boot Disk 2\" and press ENTER");
336 #else
337 printf("\n\n Please insert \"ReactOS Boot Disk 2\" and press ENTER\n");
338 MachConsGetCh();
339 #endif
340
341 /* Open boot drive */
342 if (!FsOpenBootVolume())
343 {
344 #ifdef USE_UI
345 UiMessageBox("Failed to open boot drive.");
346 #else
347 printf("Failed to open boot drive.");
348 #endif
349 return;
350 }
351
352 /* FIXME: check volume label or disk marker file */
353 }
354
355
356 /* Get ANSI codepage file */
357 if (!InfFindFirstLine (InfHandle,
358 "NLS",
359 "AnsiCodepage",
360 &InfContext))
361 {
362 printf("Failed to find 'NLS/AnsiCodepage'\n");
363 return;
364 }
365
366 if (!InfGetDataField (&InfContext,
367 1,
368 &LoadOptions))
369 {
370 printf("Failed to get load options\n");
371 return;
372 }
373
374 /* Load ANSI codepage file */
375 if (!LoadNlsFile(SourcePath, LoadOptions, "ansi.nls"))
376 {
377 #ifdef USE_UI
378 UiMessageBox("Failed to load the ANSI codepage file.");
379 #else
380 printf("Failed to load the ANSI codepage file.");
381 #endif
382 return;
383 }
384
385 /* Get OEM codepage file */
386 if (!InfFindFirstLine (InfHandle,
387 "NLS",
388 "OemCodepage",
389 &InfContext))
390 {
391 printf("Failed to find 'NLS/AnsiCodepage'\n");
392 return;
393 }
394
395 if (!InfGetDataField (&InfContext,
396 1,
397 &LoadOptions))
398 {
399 printf("Failed to get load options\n");
400 return;
401 }
402
403 /* Load OEM codepage file */
404 if (!LoadNlsFile(SourcePath, LoadOptions, "oem.nls"))
405 {
406 #ifdef USE_UI
407 UiMessageBox("Failed to load the OEM codepage file.");
408 #else
409 printf("Failed to load the OEM codepage file.");
410 #endif
411 return;
412 }
413
414 /* Get Unicode Casemap file */
415 if (!InfFindFirstLine (InfHandle,
416 "NLS",
417 "UnicodeCasetable",
418 &InfContext))
419 {
420 printf("Failed to find 'NLS/AnsiCodepage'\n");
421 return;
422 }
423
424 if (!InfGetDataField (&InfContext,
425 1,
426 &LoadOptions))
427 {
428 printf("Failed to get load options\n");
429 return;
430 }
431
432 /* Load Unicode casemap file */
433 if (!LoadNlsFile(SourcePath, LoadOptions, "casemap.nls"))
434 {
435 #ifdef USE_UI
436 UiMessageBox("Failed to load the Unicode casemap file.");
437 #else
438 printf("Failed to load the Unicode casemap file.");
439 #endif
440 return;
441 }
442
443 #if 0
444 /* Load acpi.sys */
445 if (!LoadDriver(SourcePath, "acpi.sys"))
446 return;
447 #endif
448
449 #if 0
450 /* Load isapnp.sys */
451 if (!LoadDriver(SourcePath, "isapnp.sys"))
452 return;
453 #endif
454
455 #if 0
456 /* Load pci.sys */
457 if (!LoadDriver(SourcePath, "pci.sys"))
458 return;
459 #endif
460
461 /* Load scsiport.sys */
462 if (!LoadDriver(SourcePath, "scsiport.sys"))
463 return;
464
465 /* Load atapi.sys (depends on hardware detection) */
466 if (!LoadDriver(SourcePath, "atapi.sys"))
467 return;
468
469 /* Load buslogic.sys (depends on hardware detection) */
470 if (!LoadDriver(SourcePath, "buslogic.sys"))
471 return;
472
473 /* Load class2.sys */
474 if (!LoadDriver(SourcePath, "class2.sys"))
475 return;
476
477 /* Load cdrom.sys */
478 if (!LoadDriver(SourcePath, "cdrom.sys"))
479 return;
480
481 /* Load cdfs.sys */
482 if (!LoadDriver(SourcePath, "cdfs.sys"))
483 return;
484
485 /* Load disk.sys */
486 if (!LoadDriver(SourcePath, "disk.sys"))
487 return;
488
489 /* Load floppy.sys */
490 if (!LoadDriver(SourcePath, "floppy.sys"))
491 return;
492
493 /* Load vfatfs.sys (could be loaded by the setup prog!) */
494 if (!LoadDriver(SourcePath, "vfatfs.sys"))
495 return;
496
497
498 /* Load keyboard driver */
499 if (!LoadDriver(SourcePath, "i8042prt.sys"))
500 return;
501 if (!LoadDriver(SourcePath, "kbdclass.sys"))
502 return;
503
504 /* Load screen driver */
505 if (!LoadDriver(SourcePath, "blue.sys"))
506 return;
507
508 #ifdef USE_UI
509 UiUnInitialize("Booting ReactOS...");
510 #endif
511
512 /* Now boot the kernel */
513 DiskStopFloppyMotor();
514 MachVideoPrepareForReactOS(TRUE);
515 FrLdrStartup(0x2badb002);
516 }
517
518 /* EOF */