Copy w32api from trunk
[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 #include <freeldr.h>
22 #include <debug.h>
23 #include <arch.h>
24 #include <disk.h>
25 #include <reactos.h>
26 #include <rtl.h>
27 #include <fs.h>
28 #include <multiboot.h>
29 #include <mm.h>
30 #include <machine.h>
31 #include <ui.h>
32 #include <inffile.h>
33
34 #include "registry.h"
35
36
37 //#define USE_UI
38
39
40 static BOOL
41 LoadKernel(PCHAR szSourcePath, PCHAR szFileName)
42 {
43 CHAR szFullName[256];
44 #ifdef USE_UI
45 CHAR szBuffer[80];
46 #endif
47 PFILE FilePointer;
48 PCHAR szShortName;
49
50 if (szSourcePath[0] != '\\')
51 {
52 strcpy(szFullName, "\\");
53 strcat(szFullName, szSourcePath);
54 }
55 else
56 {
57 strcpy(szFullName, szSourcePath);
58 }
59
60 if (szFullName[strlen(szFullName)] != '\\')
61 {
62 strcat(szFullName, "\\");
63 }
64
65 if (szFileName[0] != '\\')
66 {
67 strcat(szFullName, szFileName);
68 }
69 else
70 {
71 strcat(szFullName, szFileName + 1);
72 }
73
74 szShortName = strrchr(szFileName, '\\');
75 if (szShortName == NULL)
76 szShortName = szFileName;
77 else
78 szShortName = szShortName + 1;
79
80 FilePointer = FsOpenFile(szFullName);
81 if (FilePointer == NULL)
82 {
83 printf("Could not find %s\n", szShortName);
84 return(FALSE);
85 }
86
87 /*
88 * Update the status bar with the current file
89 */
90 #ifdef USE_UI
91 sprintf(szBuffer, "Reading %s", szShortName);
92 UiDrawStatusText(szBuffer);
93 #else
94 printf("Reading %s\n", szShortName);
95 #endif
96
97 /*
98 * Load the kernel
99 */
100 FrLdrMapKernel(FilePointer);
101
102 return(TRUE);
103 }
104
105
106 static BOOL
107 LoadDriver(PCHAR szSourcePath, PCHAR szFileName)
108 {
109 CHAR szFullName[256];
110 #ifdef USE_UI
111 CHAR szBuffer[80];
112 #endif
113 PFILE FilePointer;
114 PCHAR szShortName;
115
116 if (szSourcePath[0] != '\\')
117 {
118 strcpy(szFullName, "\\");
119 strcat(szFullName, szSourcePath);
120 }
121 else
122 {
123 strcpy(szFullName, szSourcePath);
124 }
125
126 if (szFullName[strlen(szFullName)] != '\\')
127 {
128 strcat(szFullName, "\\");
129 }
130
131 if (szFileName[0] != '\\')
132 {
133 strcat(szFullName, szFileName);
134 }
135 else
136 {
137 strcat(szFullName, szFileName + 1);
138 }
139
140 szShortName = strrchr(szFileName, '\\');
141 if (szShortName == NULL)
142 szShortName = szFileName;
143 else
144 szShortName = szShortName + 1;
145
146
147 FilePointer = FsOpenFile(szFullName);
148 if (FilePointer == NULL)
149 {
150 printf("Could not find %s\n", szFileName);
151 return(FALSE);
152 }
153
154 /*
155 * Update the status bar with the current file
156 */
157 #ifdef USE_UI
158 sprintf(szBuffer, "Reading %s", szShortName);
159 UiDrawStatusText(szBuffer);
160 #else
161 printf("Reading %s\n", szShortName);
162 #endif
163
164 /* Load the driver */
165 FrLdrLoadModule(FilePointer, szFileName, NULL);
166
167 return(TRUE);
168 }
169
170
171 static BOOL
172 LoadNlsFile(PCHAR szSourcePath, PCHAR szFileName, PCHAR szModuleName)
173 {
174 CHAR szFullName[256];
175 #ifdef USE_UI
176 CHAR szBuffer[80];
177 #endif
178 PFILE FilePointer;
179 PCHAR szShortName;
180
181 if (szSourcePath[0] != '\\')
182 {
183 strcpy(szFullName, "\\");
184 strcat(szFullName, szSourcePath);
185 }
186 else
187 {
188 strcpy(szFullName, szSourcePath);
189 }
190
191 if (szFullName[strlen(szFullName)] != '\\')
192 {
193 strcat(szFullName, "\\");
194 }
195
196 if (szFileName[0] != '\\')
197 {
198 strcat(szFullName, szFileName);
199 }
200 else
201 {
202 strcat(szFullName, szFileName + 1);
203 }
204
205 szShortName = strrchr(szFileName, '\\');
206 if (szShortName == NULL)
207 szShortName = szFileName;
208 else
209 szShortName = szShortName + 1;
210
211
212 FilePointer = FsOpenFile(szFullName);
213 if (FilePointer == NULL)
214 {
215 printf("Could not find %s\n", szFileName);
216 return(FALSE);
217 }
218
219 /*
220 * Update the status bar with the current file
221 */
222 #ifdef USE_UI
223 sprintf(szBuffer, "Reading %s", szShortName);
224 UiDrawStatusText(szBuffer);
225 #else
226 printf("Reading %s\n", szShortName);
227 #endif
228
229 /* Load the driver */
230 FrLdrLoadModule(FilePointer, szModuleName, NULL);
231
232 return(TRUE);
233 }
234
235
236 VOID RunLoader(VOID)
237 {
238 ULONG_PTR Base;
239 ULONG Size;
240 char *SourcePath;
241 char *LoadOptions;
242 int i;
243
244 HINF InfHandle;
245 ULONG ErrorLine;
246 INFCONTEXT InfContext;
247
248 extern ULONG PageDirectoryStart;
249 extern ULONG PageDirectoryEnd;
250
251 /* Setup multiboot information structure */
252 LoaderBlock.Flags = MB_INFO_FLAG_BOOT_DEVICE | MB_INFO_FLAG_COMMAND_LINE | MB_INFO_FLAG_MODULES;
253 LoaderBlock.PageDirectoryStart = (ULONG)&PageDirectoryStart;
254 LoaderBlock.PageDirectoryEnd = (ULONG)&PageDirectoryEnd;
255 LoaderBlock.BootDevice = 0xffffffff;
256 LoaderBlock.CommandLine = (unsigned long)multiboot_kernel_cmdline;
257 LoaderBlock.ModsCount = 0;
258 LoaderBlock.ModsAddr = (unsigned long)multiboot_modules;
259 LoaderBlock.MmapLength = (unsigned long)MachGetMemoryMap((PBIOS_MEMORY_MAP)(PVOID)&multiboot_memory_map, 32) * sizeof(memory_map_t);
260 if (LoaderBlock.MmapLength)
261 {
262 LoaderBlock.MmapAddr = (unsigned long)&multiboot_memory_map;
263 LoaderBlock.Flags |= MB_INFO_FLAG_MEM_SIZE | MB_INFO_FLAG_MEMORY_MAP;
264 multiboot_memory_map_descriptor_size = sizeof(memory_map_t); // GetBiosMemoryMap uses a fixed value of 24
265 for (i = 0; i < (LoaderBlock.MmapLength / sizeof(memory_map_t)); i++)
266 {
267 if (MEMTYPE_USABLE == multiboot_memory_map[i].type &&
268 0 == multiboot_memory_map[i].base_addr_low)
269 {
270 LoaderBlock.MemLower = (multiboot_memory_map[i].base_addr_low + multiboot_memory_map[i].length_low) / 1024;
271 if (640 < LoaderBlock.MemLower)
272 {
273 LoaderBlock.MemLower = 640;
274 }
275 }
276 if (MEMTYPE_USABLE == multiboot_memory_map[i].type &&
277 multiboot_memory_map[i].base_addr_low <= 1024 * 1024 &&
278 1024 * 1024 <= multiboot_memory_map[i].base_addr_low + multiboot_memory_map[i].length_low)
279 {
280 LoaderBlock.MemHigher = (multiboot_memory_map[i].base_addr_low + multiboot_memory_map[i].length_low) / 1024 - 1024;
281 }
282 #if 0
283 printf("start: %x\t size: %x\t type %d\n",
284 multiboot_memory_map[i].base_addr_low,
285 multiboot_memory_map[i].length_low,
286 multiboot_memory_map[i].type);
287 #endif
288 }
289 }
290 #if 0
291 printf("low_mem = %d\n", LoaderBlock.MemLower);
292 printf("high_mem = %d\n", LoaderBlock.MemHigher);
293 MachConsGetCh();
294 #endif
295
296 #ifdef USE_UI
297 UiInitialize();
298 UiDrawStatusText("");
299 #endif
300
301 /* Initialize registry */
302 RegInitializeRegistry();
303
304 /* Detect hardware */
305 #ifdef USE_UI
306 UiDrawStatusText("Detecting hardware...");
307 #else
308 printf("Detecting hardware...\n\n");
309 #endif
310 MachHwDetect();
311 #ifdef USE_UI
312 UiDrawStatusText("");
313 #endif
314
315 /* set boot device */
316 MachDiskGetBootDevice(&LoaderBlock.BootDevice);
317
318 /* Open boot drive */
319 if (!FsOpenBootVolume())
320 {
321 #ifdef USE_UI
322 UiMessageBox("Failed to open boot drive.");
323 #else
324 printf("Failed to open boot drive.");
325 #endif
326 return;
327 }
328
329 /* Open 'txtsetup.sif' */
330 if (!InfOpenFile (&InfHandle,
331 MachDiskBootingFromFloppy() ? "\\txtsetup.sif" : "\\reactos\\txtsetup.sif",
332 &ErrorLine))
333 {
334 printf("Failed to open 'txtsetup.sif'\n");
335 return;
336 }
337
338 /* Get load options */
339 if (!InfFindFirstLine (InfHandle,
340 "SetupData",
341 "OsLoadOptions",
342 &InfContext))
343 {
344 printf("Failed to find 'SetupData/OsLoadOptions'\n");
345 return;
346 }
347
348 if (!InfGetDataField (&InfContext,
349 1,
350 &LoadOptions))
351 {
352 printf("Failed to get load options\n");
353 return;
354 }
355 #if 0
356 printf("LoadOptions: '%s'\n", LoadOptions);
357 #endif
358
359 if (MachDiskBootingFromFloppy())
360 {
361 /* Boot from floppy disk */
362 SourcePath = "\\";
363 }
364 else
365 {
366 /* Boot from cdrom */
367 SourcePath = "\\reactos";
368 }
369
370 /* Set kernel command line */
371 MachDiskGetBootPath(multiboot_kernel_cmdline, sizeof(multiboot_kernel_cmdline));
372 strcat(strcat(strcat(multiboot_kernel_cmdline, SourcePath), " "),
373 LoadOptions);
374
375 /* Load ntoskrnl.exe */
376 if (!LoadKernel(SourcePath, "ntoskrnl.exe"))
377 return;
378
379
380 /* Load hal.dll */
381 if (!LoadDriver(SourcePath, "hal.dll"))
382 return;
383
384
385 /* Export the hardware hive */
386 Base = FrLdrCreateModule ("HARDWARE");
387 RegExportBinaryHive ("\\Registry\\Machine\\HARDWARE", (PVOID)Base, &Size);
388 FrLdrCloseModule (Base, Size);
389
390 #if 0
391 printf("Base: %x\n", Base);
392 printf("Size: %u\n", Size);
393 printf("*** System stopped ***\n");
394 for(;;);
395 #endif
396
397 /* Insert boot disk 2 */
398 if (MachDiskBootingFromFloppy())
399 {
400 #ifdef USE_UI
401 UiMessageBox("Please insert \"ReactOS Boot Disk 2\" and press ENTER");
402 #else
403 printf("\n\n Please insert \"ReactOS Boot Disk 2\" and press ENTER\n");
404 MachConsGetCh();
405 #endif
406
407 /* Open boot drive */
408 if (!FsOpenBootVolume())
409 {
410 #ifdef USE_UI
411 UiMessageBox("Failed to open boot drive.");
412 #else
413 printf("Failed to open boot drive.");
414 #endif
415 return;
416 }
417
418 /* FIXME: check volume label or disk marker file */
419 }
420
421
422 /* Get ANSI codepage file */
423 if (!InfFindFirstLine (InfHandle,
424 "NLS",
425 "AnsiCodepage",
426 &InfContext))
427 {
428 printf("Failed to find 'NLS/AnsiCodepage'\n");
429 return;
430 }
431
432 if (!InfGetDataField (&InfContext,
433 1,
434 &LoadOptions))
435 {
436 printf("Failed to get load options\n");
437 return;
438 }
439
440 /* Load ANSI codepage file */
441 if (!LoadNlsFile(SourcePath, LoadOptions, "ansi.nls"))
442 {
443 #ifdef USE_UI
444 UiMessageBox("Failed to load the ANSI codepage file.");
445 #else
446 printf("Failed to load the ANSI codepage file.");
447 #endif
448 return;
449 }
450
451 /* Get OEM codepage file */
452 if (!InfFindFirstLine (InfHandle,
453 "NLS",
454 "OemCodepage",
455 &InfContext))
456 {
457 printf("Failed to find 'NLS/AnsiCodepage'\n");
458 return;
459 }
460
461 if (!InfGetDataField (&InfContext,
462 1,
463 &LoadOptions))
464 {
465 printf("Failed to get load options\n");
466 return;
467 }
468
469 /* Load OEM codepage file */
470 if (!LoadNlsFile(SourcePath, LoadOptions, "oem.nls"))
471 {
472 #ifdef USE_UI
473 UiMessageBox("Failed to load the OEM codepage file.");
474 #else
475 printf("Failed to load the OEM codepage file.");
476 #endif
477 return;
478 }
479
480 /* Get Unicode Casemap file */
481 if (!InfFindFirstLine (InfHandle,
482 "NLS",
483 "UnicodeCasetable",
484 &InfContext))
485 {
486 printf("Failed to find 'NLS/AnsiCodepage'\n");
487 return;
488 }
489
490 if (!InfGetDataField (&InfContext,
491 1,
492 &LoadOptions))
493 {
494 printf("Failed to get load options\n");
495 return;
496 }
497
498 /* Load Unicode casemap file */
499 if (!LoadNlsFile(SourcePath, LoadOptions, "casemap.nls"))
500 {
501 #ifdef USE_UI
502 UiMessageBox("Failed to load the Unicode casemap file.");
503 #else
504 printf("Failed to load the Unicode casemap file.");
505 #endif
506 return;
507 }
508
509 #if 0
510 /* Load acpi.sys */
511 if (!LoadDriver(SourcePath, "acpi.sys"))
512 return;
513 #endif
514
515 #if 0
516 /* Load isapnp.sys */
517 if (!LoadDriver(SourcePath, "isapnp.sys"))
518 return;
519 #endif
520
521 #if 0
522 /* Load pci.sys */
523 if (!LoadDriver(SourcePath, "pci.sys"))
524 return;
525 #endif
526
527 /* Load scsiport.sys */
528 if (!LoadDriver(SourcePath, "scsiport.sys"))
529 return;
530
531 /* Load atapi.sys (depends on hardware detection) */
532 if (!LoadDriver(SourcePath, "atapi.sys"))
533 return;
534
535 /* Load class2.sys */
536 if (!LoadDriver(SourcePath, "class2.sys"))
537 return;
538
539 /* Load cdrom.sys */
540 if (!LoadDriver(SourcePath, "cdrom.sys"))
541 return;
542
543 /* Load cdfs.sys */
544 if (!LoadDriver(SourcePath, "cdfs.sys"))
545 return;
546
547 /* Load disk.sys */
548 if (!LoadDriver(SourcePath, "disk.sys"))
549 return;
550
551 /* Load floppy.sys */
552 if (!LoadDriver(SourcePath, "floppy.sys"))
553 return;
554
555 /* Load vfatfs.sys (could be loaded by the setup prog!) */
556 if (!LoadDriver(SourcePath, "vfatfs.sys"))
557 return;
558
559
560 /* Load keyboard driver */
561 #if 0
562 if (!LoadDriver(SourcePath, "keyboard.sys"))
563 return;
564 #endif
565 if (!LoadDriver(SourcePath, "i8042prt.sys"))
566 return;
567 if (!LoadDriver(SourcePath, "kbdclass.sys"))
568 return;
569
570 /* Load screen driver */
571 if (!LoadDriver(SourcePath, "blue.sys"))
572 return;
573
574 #ifdef USE_UI
575 UiUnInitialize("Booting ReactOS...");
576 #endif
577
578 /* Now boot the kernel */
579 DiskStopFloppyMotor();
580 MachVideoPrepareForReactOS();
581 FrLdrStartup(0x2badb002);
582 }
583
584 /* EOF */