- Add information on new executive locks being worked on.
[reactos.git] / reactos / boot / freeldr / freeldr / reactos / reactos.c
1 /*
2 * FreeLoader
3 *
4 * Copyright (C) 1998-2003 Brian Palmer <brianp@sginet.com>
5 * Copyright (C) 2005 Alex Ionescu <alex@relsoft.net>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 */
21
22 #include <freeldr.h>
23
24 #define NDEBUG
25 #include <debug.h>
26
27 LOADER_PARAMETER_BLOCK LoaderBlock;
28 char reactos_kernel_cmdline[255]; // Command line passed to kernel
29 LOADER_MODULE reactos_modules[64]; // Array to hold boot module info loaded for the kernel
30 char reactos_module_strings[64][256]; // Array to hold module names
31 unsigned long reactos_memory_map_descriptor_size;
32 memory_map_t reactos_memory_map[32]; // Memory map
33
34 static CHAR szLoadingMsg[] = "Loading ReactOS...";
35
36 static BOOL
37 STDCALL
38 FrLdrLoadKernel(PCHAR szFileName,
39 INT nPos)
40 {
41 PFILE FilePointer;
42 PCHAR szShortName;
43 CHAR szBuffer[256];
44
45 /* Extract Kernel filename without path */
46 szShortName = strrchr(szFileName, '\\');
47 if (szShortName == NULL) {
48
49 /* No path, leave it alone */
50 szShortName = szFileName;
51
52 } else {
53
54 /* Skip the path */
55 szShortName = szShortName + 1;
56 }
57
58 /* Open the Kernel */
59 FilePointer = FsOpenFile(szFileName);
60
61 /* Make sure it worked */
62 if (FilePointer == NULL) {
63
64 /* Return failure on the short name */
65 strcpy(szBuffer, szShortName);
66 strcat(szBuffer, " not found.");
67 UiMessageBox(szBuffer);
68 return(FALSE);
69 }
70
71 /* Update the status bar with the current file */
72 strcpy(szBuffer, "Reading ");
73 strcat(szBuffer, szShortName);
74 UiDrawStatusText(szBuffer);
75
76 /* Do the actual loading */
77 FrLdrMapKernel(FilePointer);
78
79 /* Update Processbar and return success */
80 UiDrawProgressBarCenter(nPos, 100, szLoadingMsg);
81 return(TRUE);
82 }
83
84 static VOID
85 FreeldrFreeMem(PVOID Area)
86 {
87 MmFreeMemory(Area);
88 }
89
90 static PVOID
91 FreeldrAllocMem(ULONG_PTR Size)
92 {
93 return MmAllocateMemory((ULONG) Size);
94 }
95
96 static BOOLEAN
97 FreeldrReadFile(PVOID FileContext, PVOID Buffer, ULONG Size)
98 {
99 ULONG BytesRead;
100
101 return FsReadFile((PFILE) FileContext, (ULONG) Size, &BytesRead, Buffer)
102 && Size == BytesRead;
103 }
104
105 static BOOLEAN
106 FreeldrSeekFile(PVOID FileContext, ULONG_PTR Position)
107 {
108 FsSetFilePointer((PFILE) FileContext, (ULONG) Position);
109 return TRUE;
110 }
111
112 static BOOL
113 LoadKernelSymbols(PCHAR szKernelName, int nPos)
114 {
115 static ROSSYM_CALLBACKS FreeldrCallbacks =
116 {
117 FreeldrAllocMem,
118 FreeldrFreeMem,
119 FreeldrReadFile,
120 FreeldrSeekFile
121 };
122 PFILE FilePointer;
123 PROSSYM_INFO RosSymInfo;
124 ULONG Size;
125 ULONG_PTR Base;
126
127 RosSymInit(&FreeldrCallbacks);
128
129 FilePointer = FsOpenFile(szKernelName);
130 if (FilePointer == NULL)
131 {
132 return FALSE;
133 }
134 if (! RosSymCreateFromFile(FilePointer, &RosSymInfo))
135 {
136 return FALSE;
137 }
138 Base = FrLdrCreateModule("NTOSKRNL.SYM");
139 Size = RosSymGetRawDataLength(RosSymInfo);
140 RosSymGetRawData(RosSymInfo, (PVOID)Base);
141 FrLdrCloseModule(Base, Size);
142 RosSymDelete(RosSymInfo);
143 return TRUE;
144 }
145
146 static BOOL
147 FrLdrLoadNlsFile(PCSTR szFileName,
148 PCSTR szModuleName)
149 {
150 PFILE FilePointer;
151 CHAR value[256];
152 LPSTR p;
153
154 /* Open the Driver */
155 FilePointer = FsOpenFile(szFileName);
156
157 /* Make sure we did */
158 if (FilePointer == NULL) {
159
160 /* Fail if file wasn't opened */
161 strcpy(value, szFileName);
162 strcat(value, " not found.");
163 UiMessageBox(value);
164 return(FALSE);
165 }
166
167 /* Update the status bar with the current file */
168 strcpy(value, "Reading ");
169 p = strrchr(szFileName, '\\');
170 if (p == NULL) {
171
172 strcat(value, szFileName);
173
174 } else {
175
176 strcat(value, p + 1);
177 }
178 UiDrawStatusText(value);
179
180 /* Load the driver */
181 FrLdrLoadModule(FilePointer, szModuleName, NULL);
182 return(TRUE);
183 }
184
185 static BOOL
186 FrLdrLoadNlsFiles(PCHAR szSystemRoot,
187 PCHAR szErrorOut)
188 {
189 LONG rc = ERROR_SUCCESS;
190 FRLDRHKEY hKey;
191 WCHAR szIdBuffer[80];
192 WCHAR szNameBuffer[80];
193 CHAR szFileName[256];
194 ULONG BufferSize;
195
196 /* open the codepage key */
197 rc = RegOpenKey(NULL,
198 L"\\Registry\\Machine\\SYSTEM\\CurrentControlSet\\Control\\NLS\\CodePage",
199 &hKey);
200 if (rc != ERROR_SUCCESS) {
201
202 strcpy(szErrorOut, "Couldn't open CodePage registry key");
203 return(FALSE);
204 }
205
206 /* get ANSI codepage */
207 BufferSize = sizeof(szIdBuffer);
208 rc = RegQueryValue(hKey, L"ACP", NULL, (PUCHAR)szIdBuffer, &BufferSize);
209 if (rc != ERROR_SUCCESS) {
210
211 strcpy(szErrorOut, "Couldn't get ACP NLS setting");
212 return(FALSE);
213 }
214
215 BufferSize = sizeof(szNameBuffer);
216 rc = RegQueryValue(hKey, szIdBuffer, NULL, (PUCHAR)szNameBuffer, &BufferSize);
217 if (rc != ERROR_SUCCESS) {
218
219 strcpy(szErrorOut, "ACP NLS Setting exists, but isn't readable");
220 return(FALSE);
221 }
222
223 /* load ANSI codepage table */
224 sprintf(szFileName,"%ssystem32\\%S", szSystemRoot, szNameBuffer);
225 DbgPrint((DPRINT_REACTOS, "ANSI file: %s\n", szFileName));
226 if (!FrLdrLoadNlsFile(szFileName, "ansi.nls")) {
227
228 strcpy(szErrorOut, "Couldn't load ansi.nls");
229 return(FALSE);
230 }
231
232 /* get OEM codepage */
233 BufferSize = sizeof(szIdBuffer);
234 rc = RegQueryValue(hKey, L"OEMCP", NULL, (PUCHAR)szIdBuffer, &BufferSize);
235 if (rc != ERROR_SUCCESS) {
236
237 strcpy(szErrorOut, "Couldn't get OEMCP NLS setting");
238 return(FALSE);
239 }
240
241 BufferSize = sizeof(szNameBuffer);
242 rc = RegQueryValue(hKey, szIdBuffer, NULL, (PUCHAR)szNameBuffer, &BufferSize);
243 if (rc != ERROR_SUCCESS) {
244
245 strcpy(szErrorOut, "OEMCP NLS setting exists, but isn't readable");
246 return(FALSE);
247 }
248
249 /* load OEM codepage table */
250 sprintf(szFileName, "%ssystem32\\%S", szSystemRoot, szNameBuffer);
251 DbgPrint((DPRINT_REACTOS, "Oem file: %s\n", szFileName));
252 if (!FrLdrLoadNlsFile(szFileName, "oem.nls")) {
253
254 strcpy(szErrorOut, "Couldn't load oem.nls");
255 return(FALSE);
256 }
257
258 /* open the language key */
259 rc = RegOpenKey(NULL,
260 L"\\Registry\\Machine\\SYSTEM\\CurrentControlSet\\Control\\NLS\\Language",
261 &hKey);
262 if (rc != ERROR_SUCCESS) {
263
264 strcpy(szErrorOut, "Couldn't open Language registry key");
265 return(FALSE);
266 }
267
268 /* get the Unicode case table */
269 BufferSize = sizeof(szIdBuffer);
270 rc = RegQueryValue(hKey, L"Default", NULL, (PUCHAR)szIdBuffer, &BufferSize);
271 if (rc != ERROR_SUCCESS) {
272
273 strcpy(szErrorOut, "Couldn't get Language Default setting");
274 return(FALSE);
275 }
276
277 BufferSize = sizeof(szNameBuffer);
278 rc = RegQueryValue(hKey, szIdBuffer, NULL, (PUCHAR)szNameBuffer, &BufferSize);
279 if (rc != ERROR_SUCCESS) {
280
281 strcpy(szErrorOut, "Language Default setting exists, but isn't readable");
282 return(FALSE);
283 }
284
285 /* load Unicode case table */
286 sprintf(szFileName, "%ssystem32\\%S", szSystemRoot, szNameBuffer);
287 DbgPrint((DPRINT_REACTOS, "Casemap file: %s\n", szFileName));
288 if (!FrLdrLoadNlsFile(szFileName, "casemap.nls")) {
289
290 strcpy(szErrorOut, "casemap.nls");
291 return(FALSE);
292 }
293
294 return(TRUE);
295 }
296
297 static BOOL
298 FrLdrLoadDriver(PCHAR szFileName,
299 INT nPos)
300 {
301 PFILE FilePointer;
302 CHAR value[256];
303 LPSTR p;
304
305 /* Open the Driver */
306 FilePointer = FsOpenFile(szFileName);
307
308 /* Make sure we did */
309 if (FilePointer == NULL) {
310
311 /* Fail if file wasn't opened */
312 strcpy(value, szFileName);
313 strcat(value, " not found.");
314 UiMessageBox(value);
315 return(FALSE);
316 }
317
318 /* Update the status bar with the current file */
319 strcpy(value, "Reading ");
320 p = strrchr(szFileName, '\\');
321 if (p == NULL) {
322
323 strcat(value, szFileName);
324
325 } else {
326
327 strcat(value, p + 1);
328
329 }
330 UiDrawStatusText(value);
331
332 /* Load the driver */
333 FrLdrLoadModule(FilePointer, szFileName, NULL);
334
335 /* Update status and return */
336 UiDrawProgressBarCenter(nPos, 100, szLoadingMsg);
337 return(TRUE);
338 }
339
340 static VOID
341 FrLdrLoadBootDrivers(PCHAR szSystemRoot,
342 INT nPos)
343 {
344 LONG rc = 0;
345 FRLDRHKEY hGroupKey, hOrderKey, hServiceKey, hDriverKey;
346 WCHAR GroupNameBuffer[512];
347 WCHAR ServiceName[256];
348 ULONG OrderList[128];
349 ULONG BufferSize;
350 ULONG Index;
351 ULONG TagIndex;
352 LPWSTR GroupName;
353
354 ULONG ValueSize;
355 ULONG ValueType;
356 ULONG StartValue;
357 ULONG TagValue;
358 WCHAR DriverGroup[256];
359 ULONG DriverGroupSize;
360
361 CHAR ImagePath[256];
362 WCHAR TempImagePath[256];
363
364 /* get 'service group order' key */
365 rc = RegOpenKey(NULL,
366 L"\\Registry\\Machine\\SYSTEM\\CurrentControlSet\\Control\\ServiceGroupOrder",
367 &hGroupKey);
368 if (rc != ERROR_SUCCESS) {
369
370 DbgPrint((DPRINT_REACTOS, "Failed to open the 'ServiceGroupOrder' key (rc %d)\n", (int)rc));
371 return;
372 }
373
374 /* get 'group order list' key */
375 rc = RegOpenKey(NULL,
376 L"\\Registry\\Machine\\SYSTEM\\CurrentControlSet\\Control\\GroupOrderList",
377 &hOrderKey);
378 if (rc != ERROR_SUCCESS) {
379
380 DbgPrint((DPRINT_REACTOS, "Failed to open the 'GroupOrderList' key (rc %d)\n", (int)rc));
381 return;
382 }
383
384 /* enumerate drivers */
385 rc = RegOpenKey(NULL,
386 L"\\Registry\\Machine\\SYSTEM\\CurrentControlSet\\Services",
387 &hServiceKey);
388 if (rc != ERROR_SUCCESS) {
389
390 DbgPrint((DPRINT_REACTOS, "Failed to open the 'Services' key (rc %d)\n", (int)rc));
391 return;
392 }
393
394 /* Get the Name Group */
395 BufferSize = sizeof(GroupNameBuffer);
396 rc = RegQueryValue(hGroupKey, L"List", NULL, (PUCHAR)GroupNameBuffer, &BufferSize);
397 DbgPrint((DPRINT_REACTOS, "RegQueryValue(): rc %d\n", (int)rc));
398 if (rc != ERROR_SUCCESS) return;
399 DbgPrint((DPRINT_REACTOS, "BufferSize: %d \n", (int)BufferSize));
400 DbgPrint((DPRINT_REACTOS, "GroupNameBuffer: '%S' \n", GroupNameBuffer));
401
402 /* Loop through each group */
403 GroupName = GroupNameBuffer;
404 while (*GroupName) {
405 DbgPrint((DPRINT_REACTOS, "Driver group: '%S'\n", GroupName));
406
407 /* Query the Order */
408 BufferSize = sizeof(OrderList);
409 rc = RegQueryValue(hOrderKey, GroupName, NULL, (PUCHAR)OrderList, &BufferSize);
410 if (rc != ERROR_SUCCESS) OrderList[0] = 0;
411
412 /* enumerate all drivers */
413 for (TagIndex = 1; TagIndex <= OrderList[0]; TagIndex++) {
414
415 Index = 0;
416
417 while (TRUE) {
418
419 /* Get the Driver's Name */
420 ValueSize = sizeof(ServiceName);
421 rc = RegEnumKey(hServiceKey, Index, ServiceName, &ValueSize);
422 DbgPrint((DPRINT_REACTOS, "RegEnumKey(): rc %d\n", (int)rc));
423
424 /* Makre sure it's valid, and check if we're done */
425 if (rc == ERROR_NO_MORE_ITEMS) break;
426 if (rc != ERROR_SUCCESS) return;
427 DbgPrint((DPRINT_REACTOS, "Service %d: '%S'\n", (int)Index, ServiceName));
428
429 /* open driver Key */
430 rc = RegOpenKey(hServiceKey, ServiceName, &hDriverKey);
431
432 /* Read the Start Value */
433 ValueSize = sizeof(ULONG);
434 rc = RegQueryValue(hDriverKey, L"Start", &ValueType, (PUCHAR)&StartValue, &ValueSize);
435 if (rc != ERROR_SUCCESS) StartValue = (ULONG)-1;
436 DbgPrint((DPRINT_REACTOS, " Start: %x \n", (int)StartValue));
437
438 /* Read the Tag */
439 ValueSize = sizeof(ULONG);
440 rc = RegQueryValue(hDriverKey, L"Tag", &ValueType, (PUCHAR)&TagValue, &ValueSize);
441 if (rc != ERROR_SUCCESS) TagValue = (ULONG)-1;
442 DbgPrint((DPRINT_REACTOS, " Tag: %x \n", (int)TagValue));
443
444 /* Read the driver's group */
445 DriverGroupSize = sizeof(DriverGroup);
446 rc = RegQueryValue(hDriverKey, L"Group", NULL, (PUCHAR)DriverGroup, &DriverGroupSize);
447 DbgPrint((DPRINT_REACTOS, " Group: '%S' \n", DriverGroup));
448
449 /* Make sure it should be started */
450 if ((StartValue == 0) &&
451 (TagValue == OrderList[TagIndex]) &&
452 (_wcsicmp(DriverGroup, GroupName) == 0)) {
453
454 /* Get the Driver's Location */
455 ValueSize = sizeof(TempImagePath);
456 rc = RegQueryValue(hDriverKey, L"ImagePath", NULL, (PUCHAR)TempImagePath, &ValueSize);
457
458 /* Write the whole path if it suceeded, else prepare to fail */
459 if (rc != ERROR_SUCCESS) {
460 DbgPrint((DPRINT_REACTOS, " ImagePath: not found\n"));
461 sprintf(ImagePath, "%s\\system32\\drivers\\%S.sys", szSystemRoot, ServiceName);
462 } else if (TempImagePath[0] != L'\\') {
463 sprintf(ImagePath, "%s%S", szSystemRoot, TempImagePath);
464 } else {
465 sprintf(ImagePath, "%S", TempImagePath);
466 DbgPrint((DPRINT_REACTOS, " ImagePath: '%s'\n", ImagePath));
467 }
468
469 DbgPrint((DPRINT_REACTOS, " Loading driver: '%s'\n", ImagePath));
470
471 /* Update the position if needed */
472 if (nPos < 100) nPos += 5;
473
474 FrLdrLoadDriver(ImagePath, nPos);
475
476 } else {
477
478 DbgPrint((DPRINT_REACTOS, " Skipping driver '%S' with Start %d, Tag %d and Group '%S' (Current Tag %d, current group '%S')\n",
479 ServiceName, StartValue, TagValue, DriverGroup, OrderList[TagIndex], GroupName));
480 }
481
482 Index++;
483 }
484 }
485
486 Index = 0;
487 while (TRUE) {
488
489 /* Get the Driver's Name */
490 ValueSize = sizeof(ServiceName);
491 rc = RegEnumKey(hServiceKey, Index, ServiceName, &ValueSize);
492
493 DbgPrint((DPRINT_REACTOS, "RegEnumKey(): rc %d\n", (int)rc));
494 if (rc == ERROR_NO_MORE_ITEMS) break;
495 if (rc != ERROR_SUCCESS) return;
496 DbgPrint((DPRINT_REACTOS, "Service %d: '%S'\n", (int)Index, ServiceName));
497
498 /* open driver Key */
499 rc = RegOpenKey(hServiceKey, ServiceName, &hDriverKey);
500
501 /* Read the Start Value */
502 ValueSize = sizeof(ULONG);
503 rc = RegQueryValue(hDriverKey, L"Start", &ValueType, (PUCHAR)&StartValue, &ValueSize);
504 if (rc != ERROR_SUCCESS) StartValue = (ULONG)-1;
505 DbgPrint((DPRINT_REACTOS, " Start: %x \n", (int)StartValue));
506
507 /* Read the Tag */
508 ValueSize = sizeof(ULONG);
509 rc = RegQueryValue(hDriverKey, L"Tag", &ValueType, (PUCHAR)&TagValue, &ValueSize);
510 if (rc != ERROR_SUCCESS) TagValue = (ULONG)-1;
511 DbgPrint((DPRINT_REACTOS, " Tag: %x \n", (int)TagValue));
512
513 /* Read the driver's group */
514 DriverGroupSize = sizeof(DriverGroup);
515 rc = RegQueryValue(hDriverKey, L"Group", NULL, (PUCHAR)DriverGroup, &DriverGroupSize);
516 DbgPrint((DPRINT_REACTOS, " Group: '%S' \n", DriverGroup));
517
518 for (TagIndex = 1; TagIndex <= OrderList[0]; TagIndex++) {
519 if (TagValue == OrderList[TagIndex]) break;
520 }
521
522 if ((StartValue == 0) &&
523 (TagIndex > OrderList[0]) &&
524 (_wcsicmp(DriverGroup, GroupName) == 0)) {
525
526 ValueSize = sizeof(TempImagePath);
527 rc = RegQueryValue(hDriverKey, L"ImagePath", NULL, (PUCHAR)TempImagePath, &ValueSize);
528 if (rc != ERROR_SUCCESS) {
529 DbgPrint((DPRINT_REACTOS, " ImagePath: not found\n"));
530 sprintf(ImagePath, "%ssystem32\\drivers\\%S.sys", szSystemRoot, ServiceName);
531 } else if (TempImagePath[0] != L'\\') {
532 sprintf(ImagePath, "%s%S", szSystemRoot, TempImagePath);
533 } else {
534 sprintf(ImagePath, "%S", TempImagePath);
535 DbgPrint((DPRINT_REACTOS, " ImagePath: '%s'\n", ImagePath));
536 }
537 DbgPrint((DPRINT_REACTOS, " Loading driver: '%s'\n", ImagePath));
538
539 if (nPos < 100) nPos += 5;
540
541 FrLdrLoadDriver(ImagePath, nPos);
542
543 } else {
544
545 DbgPrint((DPRINT_REACTOS, " Skipping driver '%S' with Start %d, Tag %d and Group '%S' (Current group '%S')\n",
546 ServiceName, StartValue, TagValue, DriverGroup, GroupName));
547 }
548
549 Index++;
550 }
551
552 /* Move to the next group name */
553 GroupName = GroupName + wcslen(GroupName) + 1;
554 }
555 }
556
557 VOID
558 LoadAndBootReactOS(PCSTR OperatingSystemName)
559 {
560 PFILE FilePointer;
561 CHAR name[1024];
562 CHAR value[1024];
563 CHAR SystemPath[1024];
564 CHAR szKernelName[1024];
565 CHAR szHalName[1024];
566 CHAR szFileName[1024];
567 CHAR szBootPath[256];
568 UINT i;
569 CHAR MsgBuffer[256];
570 ULONG SectionId;
571
572 ULONG_PTR Base;
573 ULONG Size;
574
575 extern ULONG PageDirectoryStart;
576 extern ULONG PageDirectoryEnd;
577 extern BOOLEAN AcpiPresent;
578
579 //
580 // Open the operating system section
581 // specified in the .ini file
582 //
583 if (!IniOpenSection(OperatingSystemName, &SectionId))
584 {
585 sprintf(MsgBuffer,"Operating System section '%s' not found in freeldr.ini", OperatingSystemName);
586 UiMessageBox(MsgBuffer);
587 return;
588 }
589
590 /*
591 * Setup multiboot information structure
592 */
593 LoaderBlock.Flags = MB_FLAGS_BOOT_DEVICE | MB_FLAGS_COMMAND_LINE | MB_FLAGS_MODULE_INFO;
594 LoaderBlock.PageDirectoryStart = (ULONG)&PageDirectoryStart;
595 LoaderBlock.PageDirectoryEnd = (ULONG)&PageDirectoryEnd;
596 LoaderBlock.BootDevice = 0xffffffff;
597 LoaderBlock.CommandLine = (unsigned long)reactos_kernel_cmdline;
598 LoaderBlock.ModsCount = 0;
599 LoaderBlock.ModsAddr = (unsigned long)reactos_modules;
600 LoaderBlock.MmapLength = (unsigned long)MachGetMemoryMap((PBIOS_MEMORY_MAP)(PVOID)&reactos_memory_map, 32) * sizeof(memory_map_t);
601 if (LoaderBlock.MmapLength)
602 {
603 LoaderBlock.MmapAddr = (unsigned long)&reactos_memory_map;
604 LoaderBlock.Flags |= MB_FLAGS_MEM_INFO | MB_FLAGS_MMAP_INFO;
605 reactos_memory_map_descriptor_size = sizeof(memory_map_t); // GetBiosMemoryMap uses a fixed value of 24
606 DbgPrint((DPRINT_REACTOS, "memory map length: %d\n", LoaderBlock.MmapLength));
607 DbgPrint((DPRINT_REACTOS, "dumping memory map:\n"));
608 for (i=0; i<(LoaderBlock.MmapLength/sizeof(memory_map_t)); i++)
609 {
610 if (MEMTYPE_USABLE == reactos_memory_map[i].type &&
611 0 == reactos_memory_map[i].base_addr_low)
612 {
613 LoaderBlock.MemLower = (reactos_memory_map[i].base_addr_low + reactos_memory_map[i].length_low) / 1024;
614 if (640 < LoaderBlock.MemLower)
615 {
616 LoaderBlock.MemLower = 640;
617 }
618 }
619 if (MEMTYPE_USABLE == reactos_memory_map[i].type &&
620 reactos_memory_map[i].base_addr_low <= 1024 * 1024 &&
621 1024 * 1024 <= reactos_memory_map[i].base_addr_low + reactos_memory_map[i].length_low)
622 {
623 LoaderBlock.MemHigher = (reactos_memory_map[i].base_addr_low + reactos_memory_map[i].length_low) / 1024 - 1024;
624 }
625 DbgPrint((DPRINT_REACTOS, "start: %x\t size: %x\t type %d\n",
626 reactos_memory_map[i].base_addr_low,
627 reactos_memory_map[i].length_low,
628 reactos_memory_map[i].type));
629 }
630 }
631 DbgPrint((DPRINT_REACTOS, "low_mem = %d\n", LoaderBlock.MemLower));
632 DbgPrint((DPRINT_REACTOS, "high_mem = %d\n", LoaderBlock.MemHigher));
633
634 /*
635 * Initialize the registry
636 */
637 RegInitializeRegistry();
638
639 /*
640 * Make sure the system path is set in the .ini file
641 */
642 if (!IniReadSettingByName(SectionId, "SystemPath", SystemPath, sizeof(SystemPath)))
643 {
644 UiMessageBox("System path not specified for selected operating system.");
645 return;
646 }
647
648 /*
649 * Special case for Live CD.
650 */
651 if (!_stricmp(SystemPath, "LiveCD"))
652 {
653 /* Normalize */
654 MachDiskGetBootPath(SystemPath, sizeof(SystemPath));
655 strcat(SystemPath, "\\reactos");
656 strcat(strcpy(reactos_kernel_cmdline, SystemPath),
657 " /MININT");
658 }
659 else
660 {
661 if (! MachDiskNormalizeSystemPath(SystemPath,
662 sizeof(SystemPath)))
663 {
664 UiMessageBox("Invalid system path");
665 return;
666 }
667 /* copy system path into kernel command line */
668 strcpy(reactos_kernel_cmdline, SystemPath);
669 }
670
671 /*
672 * Read the optional kernel parameters (if any)
673 */
674 if (IniReadSettingByName(SectionId, "Options", value, 1024))
675 {
676 strcat(reactos_kernel_cmdline, " ");
677 strcat(reactos_kernel_cmdline, value);
678 }
679
680
681 UiDrawBackdrop();
682 UiDrawStatusText("Detecting Hardware...");
683
684 /*
685 * Detect hardware
686 */
687 MachHwDetect();
688
689 if (AcpiPresent) LoaderBlock.Flags |= MB_FLAGS_ACPI_TABLE;
690
691 UiDrawStatusText("Loading...");
692 UiDrawProgressBarCenter(0, 100, szLoadingMsg);
693
694 /*
695 * Try to open system drive
696 */
697 if (!FsOpenSystemVolume(SystemPath, szBootPath, &LoaderBlock.BootDevice))
698 {
699 UiMessageBox("Failed to open boot drive.");
700 return;
701 }
702
703 /* append a backslash */
704 if ((strlen(szBootPath)==0) ||
705 szBootPath[strlen(szBootPath)] != '\\')
706 strcat(szBootPath, "\\");
707
708 DbgPrint((DPRINT_REACTOS,"SystemRoot: '%s'\n", szBootPath));
709
710 /*
711 * Find the kernel image name
712 * and try to load the kernel off the disk
713 */
714 if(IniReadSettingByName(SectionId, "Kernel", value, 1024))
715 {
716 /*
717 * Set the name and
718 */
719 if (value[0] == '\\')
720 {
721 strcpy(szKernelName, value);
722 }
723 else
724 {
725 strcpy(szKernelName, szBootPath);
726 strcat(szKernelName, "SYSTEM32\\");
727 strcat(szKernelName, value);
728 }
729 }
730 else
731 {
732 strcpy(value, "NTOSKRNL.EXE");
733 strcpy(szKernelName, szBootPath);
734 strcat(szKernelName, "SYSTEM32\\");
735 strcat(szKernelName, value);
736 }
737
738 if (!FrLdrLoadKernel(szKernelName, 5)) return;
739
740 /*
741 * Find the HAL image name
742 * and try to load the kernel off the disk
743 */
744 if(IniReadSettingByName(SectionId, "Hal", value, 1024))
745 {
746 /*
747 * Set the name and
748 */
749 if (value[0] == '\\')
750 {
751 strcpy(szHalName, value);
752 }
753 else
754 {
755 strcpy(szHalName, szBootPath);
756 strcat(szHalName, "SYSTEM32\\");
757 strcat(szHalName, value);
758 }
759 }
760 else
761 {
762 strcpy(value, "HAL.DLL");
763 strcpy(szHalName, szBootPath);
764 strcat(szHalName, "SYSTEM32\\");
765 strcat(szHalName, value);
766 }
767
768 if (!FrLdrLoadDriver(szHalName, 10))
769 return;
770
771 #if 0
772 /* Load bootvid */
773 strcpy(value, "INBV.DLL");
774 strcpy(szHalName, szBootPath);
775 strcat(szHalName, "SYSTEM32\\");
776 strcat(szHalName, value);
777
778 if (!FrLdrLoadDriver(szHalName, 10))
779 return;
780 #endif
781 /*
782 * Load the System hive from disk
783 */
784 strcpy(szFileName, szBootPath);
785 strcat(szFileName, "SYSTEM32\\CONFIG\\SYSTEM");
786
787 DbgPrint((DPRINT_REACTOS, "SystemHive: '%s'", szFileName));
788
789 FilePointer = FsOpenFile(szFileName);
790 if (FilePointer == NULL)
791 {
792 UiMessageBox("Could not find the System hive!");
793 return;
794 }
795
796 /*
797 * Update the status bar with the current file
798 */
799 strcpy(name, "Reading ");
800 strcat(name, value);
801 while (strlen(name) < 80)
802 strcat(name, " ");
803 UiDrawStatusText(name);
804
805 /*
806 * Load the System hive
807 */
808 Base = FrLdrLoadModule(FilePointer, szFileName, &Size);
809 if (Base == 0 || Size == 0)
810 {
811 UiMessageBox("Could not load the System hive!\n");
812 return;
813 }
814 DbgPrint((DPRINT_REACTOS, "SystemHive loaded at 0x%x size %u", (unsigned)Base, (unsigned)Size));
815
816 /*
817 * Import the loaded system hive
818 */
819 RegImportBinaryHive((PCHAR)Base, Size);
820
821 /*
822 * Initialize the 'CurrentControlSet' link
823 */
824 RegInitCurrentControlSet(FALSE);
825
826 UiDrawProgressBarCenter(15, 100, szLoadingMsg);
827
828 /*
829 * Export the hardware hive
830 */
831 Base = FrLdrCreateModule ("HARDWARE");
832 RegExportBinaryHive (L"\\Registry\\Machine\\HARDWARE", (PCHAR)Base, &Size);
833 FrLdrCloseModule (Base, Size);
834
835 UiDrawProgressBarCenter(20, 100, szLoadingMsg);
836
837 /*
838 * Load NLS files
839 */
840 if (!FrLdrLoadNlsFiles(szBootPath, MsgBuffer))
841 {
842 UiMessageBox(MsgBuffer);
843 return;
844 }
845 UiDrawProgressBarCenter(30, 100, szLoadingMsg);
846
847 /*
848 * Load kernel symbols
849 */
850 LoadKernelSymbols(szKernelName, 30);
851 UiDrawProgressBarCenter(40, 100, szLoadingMsg);
852
853 /*
854 * Load boot drivers
855 */
856 FrLdrLoadBootDrivers(szBootPath, 40);
857 UiUnInitialize("Booting ReactOS...");
858
859 /*
860 * Now boot the kernel
861 */
862 DiskStopFloppyMotor();
863 MachVideoPrepareForReactOS();
864 FrLdrStartup(0x2badb002);
865 }
866
867 #undef DbgPrint
868 ULONG
869 DbgPrint(char *Fmt, ...)
870 {
871 UiMessageBox(Fmt);
872 return 0;
873 }
874
875 /* EOF */