Fix some Rtl Prototype inconsistencies, more are in ntifs/winddk but i have fixed...
[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 MultiBootLoadKernel(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 MultiBootLoadModule(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 MultiBootLoadModule(FilePointer, szModuleName, NULL);
231
232 return(TRUE);
233 }
234
235
236 VOID RunLoader(VOID)
237 {
238 PVOID Base;
239 U32 Size;
240 char *SourcePath;
241 char *LoadOptions;
242 int i;
243
244 HINF InfHandle;
245 U32 ErrorLine;
246 INFCONTEXT InfContext;
247
248 /* Setup multiboot information structure */
249 mb_info.flags = MB_INFO_FLAG_BOOT_DEVICE | MB_INFO_FLAG_COMMAND_LINE | MB_INFO_FLAG_MODULES;
250 mb_info.boot_device = 0xffffffff;
251 mb_info.cmdline = (unsigned long)multiboot_kernel_cmdline;
252 mb_info.mods_count = 0;
253 mb_info.mods_addr = (unsigned long)multiboot_modules;
254 mb_info.mmap_length = (unsigned long)MachGetMemoryMap((PBIOS_MEMORY_MAP)(PVOID)&multiboot_memory_map, 32) * sizeof(memory_map_t);
255 if (mb_info.mmap_length)
256 {
257 mb_info.mmap_addr = (unsigned long)&multiboot_memory_map;
258 mb_info.flags |= MB_INFO_FLAG_MEM_SIZE | MB_INFO_FLAG_MEMORY_MAP;
259 multiboot_memory_map_descriptor_size = sizeof(memory_map_t); // GetBiosMemoryMap uses a fixed value of 24
260 for (i = 0; i < (mb_info.mmap_length / sizeof(memory_map_t)); i++)
261 {
262 if (MEMTYPE_USABLE == multiboot_memory_map[i].type &&
263 0 == multiboot_memory_map[i].base_addr_low)
264 {
265 mb_info.mem_lower = (multiboot_memory_map[i].base_addr_low + multiboot_memory_map[i].length_low) / 1024;
266 if (640 < mb_info.mem_lower)
267 {
268 mb_info.mem_lower = 640;
269 }
270 }
271 if (MEMTYPE_USABLE == multiboot_memory_map[i].type &&
272 multiboot_memory_map[i].base_addr_low <= 1024 * 1024 &&
273 1024 * 1024 <= multiboot_memory_map[i].base_addr_low + multiboot_memory_map[i].length_low)
274 {
275 mb_info.mem_upper = (multiboot_memory_map[i].base_addr_low + multiboot_memory_map[i].length_low) / 1024 - 1024;
276 }
277 #if 0
278 printf("start: %x\t size: %x\t type %d\n",
279 multiboot_memory_map[i].base_addr_low,
280 multiboot_memory_map[i].length_low,
281 multiboot_memory_map[i].type);
282 #endif
283 }
284 }
285 #if 0
286 printf("low_mem = %d\n", mb_info.mem_lower);
287 printf("high_mem = %d\n", mb_info.mem_upper);
288 MachConsGetCh();
289 #endif
290
291 #ifdef USE_UI
292 UiInitialize();
293 UiDrawStatusText("");
294 #endif
295
296 /* Initialize registry */
297 RegInitializeRegistry();
298
299 /* Detect hardware */
300 #ifdef USE_UI
301 UiDrawStatusText("Detecting hardware...");
302 #else
303 printf("Detecting hardware...\n\n");
304 #endif
305 MachHwDetect();
306 #ifdef USE_UI
307 UiDrawStatusText("");
308 #endif
309
310 /* set boot drive and partition */
311 ((char *)(&mb_info.boot_device))[0] = (char)BootDrive;
312 ((char *)(&mb_info.boot_device))[1] = (char)BootPartition;
313
314
315 /* Open boot drive */
316 if (!FsOpenVolume(BootDrive, BootPartition))
317 {
318 #ifdef USE_UI
319 UiMessageBox("Failed to open boot drive.");
320 #else
321 printf("Failed to open boot drive.");
322 #endif
323 return;
324 }
325
326 /* Open 'txtsetup.sif' */
327 if (!InfOpenFile (&InfHandle,
328 (BootDrive < 0x80) ? "\\txtsetup.sif" : "\\reactos\\txtsetup.sif",
329 &ErrorLine))
330 {
331 printf("Failed to open 'txtsetup.sif'\n");
332 return;
333 }
334
335 /* Get load options */
336 if (!InfFindFirstLine (InfHandle,
337 "SetupData",
338 "OsLoadOptions",
339 &InfContext))
340 {
341 printf("Failed to find 'SetupData/OsLoadOptions'\n");
342 return;
343 }
344
345 if (!InfGetDataField (&InfContext,
346 1,
347 &LoadOptions))
348 {
349 printf("Failed to get load options\n");
350 return;
351 }
352 #if 0
353 printf("LoadOptions: '%s'\n", LoadOptions);
354 #endif
355
356 if (BootDrive < 0x80)
357 {
358 /* Boot from floppy disk */
359 SourcePath = "\\";
360 }
361 else
362 {
363 /* Boot from cdrom */
364 SourcePath = "\\reactos";
365 }
366
367 /* Set kernel command line */
368 sprintf(multiboot_kernel_cmdline,
369 "multi(0)disk(0)%s(%u)%s %s",
370 (BootDrive < 0x80) ? "fdisk" : "cdrom",
371 (unsigned int)BootDrive,
372 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 = MultiBootCreateModule ("HARDWARE");
387 RegExportBinaryHive ("\\Registry\\Machine\\HARDWARE", Base, &Size);
388 MultiBootCloseModule (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 (BootDrive < 0x80)
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 (!FsOpenVolume(BootDrive, BootPartition))
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 (!LoadDriver(SourcePath, "keyboard.sys"))
562 return;
563
564 /* Load screen driver */
565 if (!LoadDriver(SourcePath, "blue.sys"))
566 return;
567
568 #ifdef USE_UI
569 UiUnInitialize("Booting ReactOS...");
570 #endif
571
572 /* Now boot the kernel */
573 DiskStopFloppyMotor();
574 boot_reactos();
575 }
576
577 /* EOF */