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