4 * Copyright (C) 1998-2002 Brian Palmer <brianp@sginet.com>
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.
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.
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.
28 #include <multiboot.h>
40 LoadKernel(PCHAR szFileName
, int nPos
)
46 szShortName
= strrchr(szFileName
, '\\');
47 if (szShortName
== NULL
)
48 szShortName
= szFileName
;
50 szShortName
= szShortName
+ 1;
52 FilePointer
= OpenFile(szFileName
);
53 if (FilePointer
== NULL
)
55 strcpy(szBuffer
, szShortName
);
56 strcat(szBuffer
, " not found.");
57 UiMessageBox(szBuffer
);
62 * Update the status bar with the current file
64 strcpy(szBuffer
, "Reading ");
65 strcat(szBuffer
, szShortName
);
66 UiDrawStatusText(szBuffer
);
71 MultiBootLoadKernel(FilePointer
);
73 UiDrawProgressBarCenter(nPos
, 100);
79 LoadSymbolFile(PCHAR szSystemRoot
,
83 CHAR SymbolFileName
[1024];
91 /* Get the path to the symbol store */
92 strcpy(SymbolFileName
, szSystemRoot
);
93 strcat(SymbolFileName
, "symbols\\");
95 /* Get the symbol filename from the module name */
96 Start
= strrchr(ModuleName
, '\\');
102 Ext
= strrchr(ModuleName
, '.');
104 Length
= Ext
- Start
;
106 Length
= strlen(Start
);
108 strncat(SymbolFileName
, Start
, Length
);
109 strcat(SymbolFileName
, ".sym");
111 FilePointer
= OpenFile((PCHAR
)&SymbolFileName
[0]);
112 if (FilePointer
== NULL
)
114 DbgPrint((DPRINT_REACTOS
, "Symbol file %s not loaded.\n", SymbolFileName
));
115 /* This is not critical */
119 DbgPrint((DPRINT_REACTOS
, "Symbol file %s is loaded.\n", SymbolFileName
));
122 * Update the status bar with the current file
124 strcpy(value
, "Reading ");
125 p
= strrchr(SymbolFileName
, '\\');
127 strcat(value
, SymbolFileName
);
129 strcat(value
, p
+ 1);
130 UiDrawStatusText(value
);
133 * Load the symbol file
135 MultiBootLoadModule(FilePointer
, SymbolFileName
, NULL
);
137 UiDrawProgressBarCenter(nPos
, 100);
143 LoadDriver(PCHAR szFileName
, int nPos
)
149 FilePointer
= OpenFile(szFileName
);
150 if (FilePointer
== NULL
)
152 strcpy(value
, szFileName
);
153 strcat(value
, " not found.");
159 * Update the status bar with the current file
161 strcpy(value
, "Reading ");
162 p
= strrchr(szFileName
, '\\');
164 strcat(value
, szFileName
);
166 strcat(value
, p
+ 1);
167 UiDrawStatusText(value
);
172 MultiBootLoadModule(FilePointer
, szFileName
, NULL
);
174 UiDrawProgressBarCenter(nPos
, 100);
182 LoadNlsFile(PCHAR szFileName
, PCHAR szModuleName
)
188 FilePointer
= OpenFile(szFileName
);
189 if (FilePointer
== NULL
)
191 strcpy(value
, szFileName
);
192 strcat(value
, " not found.");
198 * Update the status bar with the current file
200 strcpy(value
, "Reading ");
201 p
= strrchr(szFileName
, '\\');
203 strcat(value
, szFileName
);
205 strcat(value
, p
+ 1);
206 UiDrawStatusText(value
);
211 MultiBootLoadModule(FilePointer
, szModuleName
, NULL
);
219 LoadBootDrivers(PCHAR szSystemRoot
, int nPos
)
222 HKEY hGroupKey
, hServiceKey
, hDriverKey
;
223 char ValueBuffer
[512];
224 char ServiceName
[256];
232 UCHAR DriverGroup
[256];
235 UCHAR ImagePath
[256];
237 /* get 'service group order' key */
238 rc
= RegOpenKey(NULL
,
239 "\\Registry\\Machine\\SYSTEM\\CurrentControlSet\\Control\\ServiceGroupOrder",
241 if (rc
!= ERROR_SUCCESS
)
243 DbgPrint((DPRINT_REACTOS
, "Failed to open the 'ServiceGroupOrder key (rc %d)\n", (int)rc
));
247 /* enumerate drivers */
248 rc
= RegOpenKey(NULL
,
249 "\\Registry\\Machine\\SYSTEM\\CurrentControlSet\\Services",
251 if (rc
!= ERROR_SUCCESS
)
253 DbgPrint((DPRINT_REACTOS
, "Failed to open the 'Services' key (rc %d)\n", (int)rc
));
257 BufferSize
= sizeof(ValueBuffer
);
258 rc
= RegQueryValue(hGroupKey
, "List", NULL
, (PUCHAR
)ValueBuffer
, &BufferSize
);
259 DbgPrint((DPRINT_REACTOS
, "RegQueryValue(): rc %d\n", (int)rc
));
260 if (rc
!= ERROR_SUCCESS
)
263 DbgPrint((DPRINT_REACTOS
, "BufferSize: %d \n", (int)BufferSize
));
265 DbgPrint((DPRINT_REACTOS
, "ValueBuffer: '%s' \n", ValueBuffer
));
267 GroupName
= ValueBuffer
;
270 DbgPrint((DPRINT_REACTOS
, "Driver group: '%s'\n", GroupName
));
272 /* enumerate all drivers */
276 ValueSize
= sizeof(ValueBuffer
);
277 rc
= RegEnumKey(hServiceKey
, Index
, ServiceName
, &ValueSize
);
278 DbgPrint((DPRINT_REACTOS
, "RegEnumKey(): rc %d\n", (int)rc
));
279 if (rc
== ERROR_NO_MORE_ITEMS
)
281 if (rc
!= ERROR_SUCCESS
)
283 DbgPrint((DPRINT_REACTOS
, "Service %d: '%s'\n", (int)Index
, ServiceName
));
285 /* open driver Key */
286 rc
= RegOpenKey(hServiceKey
, ServiceName
, &hDriverKey
);
288 ValueSize
= sizeof(U32
);
289 rc
= RegQueryValue(hDriverKey
, "Start", &ValueType
, (PUCHAR
)&StartValue
, &ValueSize
);
290 DbgPrint((DPRINT_REACTOS
, " Start: %x \n", (int)StartValue
));
292 DriverGroupSize
= 256;
293 rc
= RegQueryValue(hDriverKey
, "Group", NULL
, (PUCHAR
)DriverGroup
, &DriverGroupSize
);
294 DbgPrint((DPRINT_REACTOS
, " Group: '%s' \n", DriverGroup
));
296 if ((StartValue
== 0) && (stricmp(DriverGroup
, GroupName
) == 0))
299 rc
= RegQueryValue(hDriverKey
,
304 if (rc
!= ERROR_SUCCESS
)
306 DbgPrint((DPRINT_REACTOS
, " ImagePath: not found\n"));
307 strcpy(ImagePath
, szSystemRoot
);
308 strcat(ImagePath
, "system32\\drivers\\");
309 strcat(ImagePath
, ServiceName
);
310 strcat(ImagePath
, ".sys");
314 DbgPrint((DPRINT_REACTOS
, " ImagePath: '%s'\n", ImagePath
));
316 DbgPrint((DPRINT_REACTOS
, " Loading driver: '%s'\n", ImagePath
));
321 LoadDriver(ImagePath
, nPos
);
322 LoadSymbolFile(szSystemRoot
, ImagePath
, nPos
);
326 DbgPrint((DPRINT_REACTOS
, " Skipping driver '%s' with Start %d and Group '%s' (Current group '%s')\n",
327 ImagePath
, StartValue
, DriverGroup
, GroupName
));
332 GroupName
= GroupName
+ strlen(GroupName
) + 1;
338 LoadNlsFiles(PCHAR szSystemRoot
)
340 S32 rc
= ERROR_SUCCESS
;
343 char szNameBuffer
[80];
344 char szFileName
[256];
347 /* open the codepage key */
348 rc
= RegOpenKey(NULL
,
349 "\\Registry\\Machine\\SYSTEM\\CurrentControlSet\\Control\\NLS\\CodePage",
351 if (rc
!= ERROR_SUCCESS
)
355 /* get ANSI codepage */
357 rc
= RegQueryValue(hKey
, "ACP", NULL
, (PUCHAR
)szIdBuffer
, &BufferSize
);
358 if (rc
!= ERROR_SUCCESS
)
362 rc
= RegQueryValue(hKey
, szIdBuffer
, NULL
, (PUCHAR
)szNameBuffer
, &BufferSize
);
363 if (rc
!= ERROR_SUCCESS
)
367 /* load ANSI codepage table */
368 strcpy(szFileName
, szSystemRoot
);
369 strcat(szFileName
, "system32\\");
370 strcat(szFileName
, szNameBuffer
);
371 if (!LoadNlsFile(szFileName
, "ANSI.NLS"))
375 /* get OEM codepage */
377 rc
= RegQueryValue(hKey
, "OEMCP", NULL
, (PUCHAR
)szIdBuffer
, &BufferSize
);
378 if (rc
!= ERROR_SUCCESS
)
382 rc
= RegQueryValue(hKey
, szIdBuffer
, NULL
, (PUCHAR
)szNameBuffer
, &BufferSize
);
383 if (rc
!= ERROR_SUCCESS
)
386 /* load OEM codepage table */
387 strcpy(szFileName
, szSystemRoot
);
388 strcat(szFileName
, "system32\\");
389 strcat(szFileName
, szNameBuffer
);
390 if (!LoadNlsFile(szFileName
, "OEM.NLS"))
394 /* open the language key */
395 rc
= RegOpenKey(NULL
,
396 "\\Registry\\Machine\\SYSTEM\\CurrentControlSet\\Control\\NLS\\Language",
398 if (rc
!= ERROR_SUCCESS
)
402 /* get the Unicode case table */
404 rc
= RegQueryValue(hKey
, "Default", NULL
, (PUCHAR
)szIdBuffer
, &BufferSize
);
405 if (rc
!= ERROR_SUCCESS
)
409 rc
= RegQueryValue(hKey
, szIdBuffer
, NULL
, (PUCHAR
)szNameBuffer
, &BufferSize
);
410 if (rc
!= ERROR_SUCCESS
)
413 /* load Unicode case table */
414 strcpy(szFileName
, szSystemRoot
);
415 strcat(szFileName
, "system32\\");
416 strcat(szFileName
, szNameBuffer
);
417 if (!LoadNlsFile(szFileName
, "UNICASE.NLS"))
426 LoadAndBootReactOS(PUCHAR OperatingSystemName
)
431 char szKernelName
[1024];
432 char szHalName
[1024];
433 char szFileName
[1024];
434 char szBootPath
[256];
436 // int nNumDriverFiles=0;
437 // int nNumFilesLoaded=0;
446 // Open the operating system section
447 // specified in the .ini file
449 if (!IniOpenSection(OperatingSystemName
, &SectionId
))
451 sprintf(MsgBuffer
,"Operating System section '%s' not found in freeldr.ini", OperatingSystemName
);
452 UiMessageBox(MsgBuffer
);
457 * Setup multiboot information structure
459 mb_info
.flags
= MB_INFO_FLAG_MEM_SIZE
| MB_INFO_FLAG_BOOT_DEVICE
| MB_INFO_FLAG_COMMAND_LINE
| MB_INFO_FLAG_MODULES
;
460 mb_info
.mem_lower
= GetConventionalMemorySize();
461 mb_info
.mem_upper
= GetExtendedMemorySize();
462 mb_info
.boot_device
= 0xffffffff;
463 mb_info
.cmdline
= (unsigned long)multiboot_kernel_cmdline
;
464 mb_info
.mods_count
= 0;
465 mb_info
.mods_addr
= (unsigned long)multiboot_modules
;
466 mb_info
.mmap_length
= (unsigned long)GetBiosMemoryMap((PBIOS_MEMORY_MAP_ARRAY
)&multiboot_memory_map
);
467 if (mb_info
.mmap_length
)
469 mb_info
.mmap_addr
= (unsigned long)&multiboot_memory_map
;
470 mb_info
.flags
|= MB_INFO_FLAG_MEMORY_MAP
;
471 //printf("memory map length: %d\n", mb_info.mmap_length);
472 //printf("dumping memory map:\n");
473 //for (i=0; i<(mb_info.mmap_length / 4); i++)
475 // printf("0x%x\n", ((unsigned long *)&multiboot_memory_map)[i]);
479 //printf("low_mem = %d\n", mb_info.mem_lower);
480 //printf("high_mem = %d\n", mb_info.mem_upper);
484 * Make sure the system path is set in the .ini file
486 if (!IniReadSettingByName(SectionId
, "SystemPath", value
, 1024))
488 UiMessageBox("System path not specified for selected operating system.");
495 if (!DissectArcPath(value
, szBootPath
, &BootDrive
, &BootPartition
))
497 sprintf(MsgBuffer
,"Invalid system path: '%s'", value
);
498 UiMessageBox(MsgBuffer
);
502 /* set boot drive and partition */
503 ((char *)(&mb_info
.boot_device
))[0] = (char)BootDrive
;
504 ((char *)(&mb_info
.boot_device
))[1] = (char)BootPartition
;
506 /* copy ARC path into kernel command line */
507 strcpy(multiboot_kernel_cmdline
, value
);
510 * Read the optional kernel parameters (if any)
512 if (IniReadSettingByName(SectionId
, "Options", value
, 1024))
514 strcat(multiboot_kernel_cmdline
, " ");
515 strcat(multiboot_kernel_cmdline
, value
);
518 /* append a backslash */
519 if ((strlen(szBootPath
)==0) ||
520 szBootPath
[strlen(szBootPath
)] != '\\')
521 strcat(szBootPath
, "\\");
523 DbgPrint((DPRINT_REACTOS
,"SystemRoot: '%s'\n", szBootPath
));
526 UiDrawStatusText("Loading...");
527 UiDrawProgressBarCenter(0, 100);
530 * Try to open boot drive
532 if (!OpenDiskDrive(BootDrive
, BootPartition
))
534 UiMessageBox("Failed to open boot drive.");
539 * Find the kernel image name
540 * and try to load the kernel off the disk
542 if(IniReadSettingByName(SectionId
, "Kernel", value
, 1024))
547 if (value
[0] == '\\')
549 strcpy(szKernelName
, value
);
553 strcpy(szKernelName
, szBootPath
);
554 strcat(szKernelName
, "SYSTEM32\\");
555 strcat(szKernelName
, value
);
560 strcpy(value
, "NTOSKRNL.EXE");
561 strcpy(szKernelName
, szBootPath
);
562 strcat(szKernelName
, "SYSTEM32\\");
563 strcat(szKernelName
, value
);
566 if (!LoadKernel(szKernelName
, 5))
570 * Find the HAL image name
571 * and try to load the kernel off the disk
573 if(IniReadSettingByName(SectionId
, "Hal", value
, 1024))
578 if (value
[0] == '\\')
580 strcpy(szHalName
, value
);
584 strcpy(szHalName
, szBootPath
);
585 strcat(szHalName
, "SYSTEM32\\");
586 strcat(szHalName
, value
);
591 strcpy(value
, "HAL.DLL");
592 strcpy(szHalName
, szBootPath
);
593 strcat(szHalName
, "SYSTEM32\\");
594 strcat(szHalName
, value
);
597 if (!LoadDriver(szHalName
, 10))
601 * Find the System hive image name
602 * and try to load it off the disk
604 if(IniReadSettingByName(SectionId
, "SystemHive", value
, 1024))
609 if (value
[0] == '\\')
611 strcpy(szFileName
, value
);
615 strcpy(szFileName
, szBootPath
);
616 strcat(szFileName
, "SYSTEM32\\CONFIG\\");
617 strcat(szFileName
, value
);
622 strcpy(value
, "SYSTEM.HIV");
623 strcpy(szFileName
, szBootPath
);
624 strcat(szFileName
, "SYSTEM32\\CONFIG\\");
625 strcat(szFileName
, value
);
628 DbgPrint((DPRINT_REACTOS
, "SystemHive: '%s'", szFileName
));
630 FilePointer
= OpenFile(szFileName
);
631 if (FilePointer
== NULL
)
633 strcat(value
, " not found.");
639 * Update the status bar with the current file
641 strcpy(name
, "Reading ");
643 while (strlen(name
) < 80)
645 UiDrawStatusText(name
);
648 * Load the system hive
650 Base
= MultiBootLoadModule(FilePointer
, szFileName
, &Size
);
651 RegInitializeRegistry();
652 RegImportHive(Base
, Size
);
654 UiDrawProgressBarCenter(15, 100);
655 DbgPrint((DPRINT_REACTOS
, "SystemHive loaded at 0x%x size %u", (unsigned)Base
, (unsigned)Size
));
658 * Retrieve hardware information and create the hardware hive
662 Base
= MultiBootCreateModule(HARDWARE
.HIV
);
663 RegExportHive("\\Registry\\Machine\\HARDWARE", Base
, &Size
);
664 MultiBootCloseModule(Base
, Size
);
666 UiDrawProgressBarCenter(20, 100);
669 * Initialize the 'currentControlSet' link
671 RegInitCurrentControlSet(FALSE
);
677 if (!LoadNlsFiles(szBootPath
))
679 MessageBox("Failed to load NLS files\n");
684 LoadSymbolFile(szBootPath
, szKernelName
, 25);
685 LoadSymbolFile(szBootPath
, szHalName
, 25);
687 UiDrawProgressBarCenter(25, 100);
692 LoadBootDrivers(szBootPath
, 25);
696 * Clear the screen and redraw the backdrop and status bar
699 UiDrawStatusText("Press any key to boot");
704 strcpy(name
, "Kernel and Drivers loaded.\nPress any key to boot ");
705 strcat(name
, OperatingSystemName
);
710 * Now boot the kernel