ntoskrnl/ke/main.c
[reactos.git] / reactos / ntoskrnl / ke / main.c
1 /*
2 * ReactOS kernel
3 * Copyright (C) 1998, 1999, 2000, 2001 ReactOS Team
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 */
19 /* $Id: main.c,v 1.126 2002/06/17 22:16:33 joeg Exp $
20 *
21 * PROJECT: ReactOS kernel
22 * FILE: ntoskrnl/ke/main.c
23 * PURPOSE: Initalizes the kernel
24 * PROGRAMMER: David Welch (welch@cwcom.net)
25 * UPDATE HISTORY:
26 * 28/05/98: Created
27 */
28
29 /* INCLUDES *****************************************************************/
30
31 #include <ddk/ntddk.h>
32 #include <internal/ntoskrnl.h>
33 #include <reactos/resource.h>
34 #include <internal/mm.h>
35 #include <internal/module.h>
36 #include <internal/ldr.h>
37 #include <internal/ex.h>
38 #include <internal/ps.h>
39 #include <internal/ke.h>
40 #include <internal/io.h>
41 #include <internal/po.h>
42 #include <internal/cc.h>
43 #include <internal/se.h>
44 #include <internal/v86m.h>
45 #include <internal/kd.h>
46 #include <internal/trap.h>
47 #include "../dbg/kdb.h"
48 #include <internal/registry.h>
49 #include <reactos/bugcodes.h>
50
51 #ifdef HALDBG
52 #include <internal/ntosdbg.h>
53 #else
54 #define ps(args...)
55 #endif
56
57 #define NDEBUG
58 #include <internal/debug.h>
59
60 /* GLOBALS *******************************************************************/
61
62 ULONG EXPORTED NtBuildNumber = KERNEL_VERSION_BUILD;
63 ULONG EXPORTED NtGlobalFlag = 0;
64 CHAR EXPORTED KeNumberProcessors;
65 LOADER_PARAMETER_BLOCK EXPORTED KeLoaderBlock;
66 static LOADER_MODULE KeLoaderModules[64];
67 static UCHAR KeLoaderModuleStrings[64][256];
68 static UCHAR KeLoaderCommandLine[256];
69 static ADDRESS_RANGE KeMemoryMap[64];
70 static ULONG KeMemoryMapRangeCount;
71 static ULONG FirstKrnlPhysAddr;
72 static ULONG LastKrnlPhysAddr;
73 static ULONG LastKernelAddress;
74 volatile BOOLEAN Initialized = FALSE;
75
76 extern PVOID Ki386InitialStackArray[MAXIMUM_PROCESSORS];
77
78
79 /* FUNCTIONS ****************************************************************/
80
81 static BOOLEAN
82 RtlpCheckFileNameExtension(PCHAR FileName,
83 PCHAR Extension)
84 {
85 PCHAR Ext;
86
87 Ext = strrchr(FileName, '.');
88 if ((Extension == NULL) || (*Extension == 0))
89 {
90 if (Ext == NULL)
91 return TRUE;
92 else
93 return FALSE;
94 }
95 if (*Extension != '.')
96 Ext++;
97
98 if (_stricmp(Ext, Extension) == 0)
99 return TRUE;
100 else
101 return FALSE;
102 }
103
104
105 static VOID
106 InitSystemSharedUserPage (PCSZ ParameterLine)
107 {
108 UNICODE_STRING ArcDeviceName;
109 UNICODE_STRING ArcName;
110 UNICODE_STRING BootPath;
111 UNICODE_STRING DriveDeviceName;
112 UNICODE_STRING DriveName;
113 WCHAR DriveNameBuffer[20];
114 PCHAR ParamBuffer;
115 PWCHAR ArcNameBuffer;
116 PCHAR p;
117 NTSTATUS Status;
118 ULONG Length;
119 OBJECT_ATTRIBUTES ObjectAttributes;
120 HANDLE Handle;
121 ULONG i;
122 BOOLEAN BootDriveFound;
123
124 /*
125 * NOTE:
126 * The shared user page has been zeroed-out right after creation.
127 * There is NO need to do this again.
128 */
129
130 SharedUserData->NtProductType = NtProductWinNt;
131
132 BootDriveFound = FALSE;
133
134 /*
135 * Retrieve the current dos system path
136 * (e.g.: C:\reactos) from the given arc path
137 * (e.g.: multi(0)disk(0)rdisk(0)partititon(1)\reactos)
138 * Format: "<arc_name>\<path> [options...]"
139 */
140
141 /* create local parameter line copy */
142 ParamBuffer = ExAllocatePool (PagedPool, 256);
143 strcpy (ParamBuffer, (char *)ParameterLine);
144 DPRINT("%s\n", ParamBuffer);
145
146 /* cut options off */
147 p = strchr (ParamBuffer, ' ');
148 if (p)
149 {
150 *p = 0;
151 }
152 DPRINT("%s\n", ParamBuffer);
153
154 /* extract path */
155 p = strchr (ParamBuffer, '\\');
156 if (p)
157 {
158 DPRINT("Boot path: %s\n", p);
159 RtlCreateUnicodeStringFromAsciiz (&BootPath, p);
160 *p = 0;
161 }
162 else
163 {
164 DPRINT("Boot path: %s\n", "\\");
165 RtlCreateUnicodeStringFromAsciiz (&BootPath, "\\");
166 }
167 DPRINT("Arc name: %s\n", ParamBuffer);
168
169 /* Only arc name left - build full arc name */
170 ArcNameBuffer = ExAllocatePool (PagedPool, 256 * sizeof(WCHAR));
171 swprintf (ArcNameBuffer, L"\\ArcName\\%S", ParamBuffer);
172 RtlInitUnicodeString (&ArcName, ArcNameBuffer);
173 DPRINT("Arc name: %wZ\n", &ArcName);
174
175 /* free ParamBuffer */
176 ExFreePool (ParamBuffer);
177
178 /* allocate arc device name string */
179 ArcDeviceName.Length = 0;
180 ArcDeviceName.MaximumLength = 256 * sizeof(WCHAR);
181 ArcDeviceName.Buffer = ExAllocatePool (PagedPool, 256 * sizeof(WCHAR));
182
183 InitializeObjectAttributes (&ObjectAttributes,
184 &ArcName,
185 0,
186 NULL,
187 NULL);
188
189 Status = NtOpenSymbolicLinkObject (&Handle,
190 SYMBOLIC_LINK_ALL_ACCESS,
191 &ObjectAttributes);
192 RtlFreeUnicodeString (&ArcName);
193 if (!NT_SUCCESS(Status))
194 {
195 RtlFreeUnicodeString (&BootPath);
196 RtlFreeUnicodeString (&ArcDeviceName);
197 CPRINT("NtOpenSymbolicLinkObject() failed (Status %x)\n",
198 Status);
199
200 KeBugCheck (0x0);
201 }
202
203 Status = NtQuerySymbolicLinkObject (Handle,
204 &ArcDeviceName,
205 &Length);
206 NtClose (Handle);
207 if (!NT_SUCCESS(Status))
208 {
209 RtlFreeUnicodeString (&BootPath);
210 RtlFreeUnicodeString (&ArcDeviceName);
211 CPRINT("NtQuerySymbolicObject() failed (Status %x)\n",
212 Status);
213
214 KeBugCheck (0x0);
215 }
216 DPRINT("Length: %lu ArcDeviceName: %wZ\n", Length, &ArcDeviceName);
217
218
219 /* allocate device name string */
220 DriveDeviceName.Length = 0;
221 DriveDeviceName.MaximumLength = 256 * sizeof(WCHAR);
222 DriveDeviceName.Buffer = ExAllocatePool (PagedPool, 256 * sizeof(WCHAR));
223
224 for (i = 0; i < 26; i++)
225 {
226 swprintf (DriveNameBuffer, L"\\??\\%C:", 'A' + i);
227 RtlInitUnicodeString (&DriveName,
228 DriveNameBuffer);
229
230 InitializeObjectAttributes (&ObjectAttributes,
231 &DriveName,
232 0,
233 NULL,
234 NULL);
235
236 Status = NtOpenSymbolicLinkObject (&Handle,
237 SYMBOLIC_LINK_ALL_ACCESS,
238 &ObjectAttributes);
239 if (!NT_SUCCESS(Status))
240 {
241 DPRINT("Failed to open link %wZ\n",
242 &DriveName);
243 continue;
244 }
245
246 Status = NtQuerySymbolicLinkObject (Handle,
247 &DriveDeviceName,
248 &Length);
249 if (!NT_SUCCESS(Status))
250 {
251 DPRINT("Failed query open link %wZ\n",
252 &DriveName);
253 continue;
254 }
255 DPRINT("Opened link: %wZ ==> %wZ\n",
256 &DriveName, &DriveDeviceName);
257
258 if (!RtlCompareUnicodeString (&ArcDeviceName, &DriveDeviceName, FALSE))
259 {
260 DPRINT("DOS Boot path: %c:%wZ\n", 'A' + i, &BootPath);
261 swprintf(SharedUserData->NtSystemRoot,
262 L"%C:%wZ", 'A' + i, &BootPath);
263
264 BootDriveFound = TRUE;
265 }
266
267 NtClose (Handle);
268 }
269
270 RtlFreeUnicodeString (&BootPath);
271 RtlFreeUnicodeString (&DriveDeviceName);
272 RtlFreeUnicodeString (&ArcDeviceName);
273
274 DPRINT("DosDeviceMap: 0x%x\n", SharedUserData->DosDeviceMap);
275
276 if (BootDriveFound == FALSE)
277 {
278 DbgPrint("No system drive found!\n");
279 KeBugCheck (0x0);
280 }
281 }
282
283 #ifndef NDEBUG
284
285 VOID DumpBIOSMemoryMap(VOID)
286 {
287 ULONG i;
288
289 DbgPrint("Dumping BIOS memory map:\n");
290 DbgPrint("Memory map base: %d\n", KeLoaderBlock.MmapAddr);
291 DbgPrint("Memory map size: %d\n", KeLoaderBlock.MmapLength);
292 DbgPrint("Address range count: %d\n", KeMemoryMapRangeCount);
293 for (i = 0; i < KeMemoryMapRangeCount; i++)
294 {
295 DbgPrint("Range: Base (%08X) Length (%08X) Type (%02X)\n",
296 KeMemoryMap[i].BaseAddrLow,
297 KeMemoryMap[i].LengthLow,
298 KeMemoryMap[i].Type);
299 }
300 for (;;);
301 }
302
303 #endif /* !NDEBUG */
304
305 #if 1
306 // SEH Test
307
308 static ULONG Scratch;
309
310 EXCEPTION_DISPOSITION
311 ExpUnhandledException1(
312 PEXCEPTION_RECORD ExceptionRecord,
313 PEXCEPTION_REGISTRATION ExceptionRegistration,
314 PCONTEXT Context,
315 PVOID DispatcherContext)
316 {
317 DbgPrint("ExpUnhandledException1() called\n");
318 DbgPrint("ExceptionRecord 0x%X\n", ExceptionRecord);
319 DbgPrint(" Flags 0x%X\n", ExceptionRecord->ExceptionFlags);
320 DbgPrint("ExceptionRegistration 0x%X\n", ExceptionRegistration);
321 DbgPrint("Context 0x%X\n", Context);
322 DbgPrint("DispatcherContext 0x%X\n", DispatcherContext);
323
324 Context->Eax = (ULONG)&Scratch;
325
326 return ExceptionContinueExecution;
327 }
328
329
330 EXCEPTION_DISPOSITION
331 ExpUnhandledException2(
332 PEXCEPTION_RECORD ExceptionRecord,
333 PEXCEPTION_REGISTRATION ExceptionRegistration,
334 PCONTEXT Context,
335 PVOID DispatcherContext)
336 {
337 DbgPrint("ExpUnhandledException2() called\n");
338 DbgPrint("ExceptionRecord 0x%X\n", ExceptionRecord);
339 DbgPrint(" Flags 0x%X\n", ExceptionRecord->ExceptionFlags);
340 DbgPrint("ExceptionRegistration 0x%X\n", ExceptionRegistration);
341 DbgPrint("Context 0x%X\n", Context);
342 DbgPrint("DispatcherContext 0x%X\n", DispatcherContext);
343
344 #if 1
345 Context->Eax = (ULONG)&Scratch;
346
347 return ExceptionContinueExecution;
348
349 #else
350
351 return ExceptionContinueSearch;
352
353 #endif
354 }
355
356
357 #if 1
358 // Put in mingw headers
359 extern VOID
360 CDECL
361 _local_unwind2(
362 PEXCEPTION_REGISTRATION RegistrationFrame,
363 DWORD TryLevel);
364
365 extern VOID
366 CDECL
367 _global_unwind2(
368 PVOID RegistrationFrame);
369
370 extern EXCEPTION_DISPOSITION
371 CDECL
372 _except_handler2(
373 PEXCEPTION_RECORD ExceptionRecord,
374 PEXCEPTION_REGISTRATION RegistrationFrame,
375 PCONTEXT Context,
376 PVOID DispatcherContext);
377
378 extern EXCEPTION_DISPOSITION
379 CDECL
380 _except_handler3(
381 PEXCEPTION_RECORD ExceptionRecord,
382 PEXCEPTION_REGISTRATION RegistrationFrame,
383 PCONTEXT Context,
384 PVOID DispatcherContext);
385
386 #endif
387
388 PRTL_EXCEPTION_REGISTRATION
389 CurrentRER(VOID)
390 {
391 ULONG Value;
392
393 __asm__("movl %%ebp, %0\n\t" : "=a" (Value));
394
395 return((PRTL_EXCEPTION_REGISTRATION)Value) - 1;
396 }
397
398 PULONG x;
399 PRTL_EXCEPTION_REGISTRATION TestER;
400 SCOPETABLE_ENTRY ScopeTable;
401 PEXCEPTION_REGISTRATION OSPtr;
402
403
404 DWORD CDECL SEHFilterRoutine(VOID)
405 {
406 DbgPrint("Within filter routine.\n");
407 return EXCEPTION_EXECUTE_HANDLER;
408 //return EXCEPTION_CONTINUE_EXECUTION;
409 }
410
411 VOID CDECL SEHHandlerRoutine(VOID)
412 {
413 DbgPrint("Within exception handler.\n");
414 DbgPrint("System halted.\n");
415 for (;;);
416 }
417
418
419 VOID SEHTest()
420 {
421 RTL_EXCEPTION_REGISTRATION ER;
422 LPEXCEPTION_POINTERS ExceptionPointers;
423 PVOID StandardESPInFrame;
424
425 __asm__ ("movl %%esp,%%eax;" : "=a" (StandardESPInFrame));
426 DbgPrint("StandardESPInFrame: 0x%X\n", StandardESPInFrame);
427
428 ExceptionPointers = NULL;
429
430 ER.OS.handler = _except_handler3;
431 __asm__ ("movl %%fs:0,%%eax;" : "=a" (ER.OS.prev));
432 DbgPrint("ER.OS.prev: 0x%X\n", ER.OS.prev);
433
434 ER.ScopeTable = &ScopeTable;
435 DbgPrint("ER.ScopeTable: 0x%X\n", ER.ScopeTable);
436 ER.TryLevel = -1;
437 __asm__ ("movl %%ebp,%%eax;" : "=a" (ER.Ebp));
438 DbgPrint("ER.Ebp: 0x%X\n", ER.Ebp);
439
440 ScopeTable.PreviousTryLevel = -1;
441 ScopeTable.FilterRoutine = SEHFilterRoutine;
442 DbgPrint("ScopeTable.FilterRoutine: 0x%X\n", ScopeTable.FilterRoutine);
443 ScopeTable.HandlerRoutine = SEHHandlerRoutine;
444 DbgPrint("ScopeTable.HandlerRoutine: 0x%X\n", ScopeTable.HandlerRoutine);
445
446
447 OSPtr = &ER.OS;
448 DbgPrint("OSPtr: 0x%X\n", OSPtr);
449
450 __asm__ ("movl %0,%%eax;movl %%eax,%%fs:0;" : : "m" (OSPtr));
451
452 /*__try1(__except_handler3)*/ if(1) {
453 ER.TryLevel = 0; // Entered first try... block
454
455 DbgPrint("Within guarded section.\n");
456 x = (PULONG)0xf2000000; *x = 0;
457 DbgPrint("After exception.\n");
458 } /* __except1 */ if(0) {
459 }
460
461 DbgPrint("After exception2.\n");
462
463 __asm__ ("movl %0,%%eax;movl %%eax,%%fs:0;" : : "m" (ER.OS.prev));
464 //KeGetCurrentKPCR()->ExceptionList = ER.OS.prev;
465
466 DbgPrint("Exiting.\n");
467 }
468
469 #endif
470
471 VOID
472 ExpInitializeExecutive(VOID)
473 {
474 ULONG BootDriverCount;
475 ULONG i;
476 ULONG start;
477 ULONG length;
478 PCHAR name;
479 CHAR str[50];
480
481 /*
482 * Fail at runtime if someone has changed various structures without
483 * updating the offsets used for the assembler code.
484 */
485 assert(FIELD_OFFSET(KTHREAD, InitialStack) == KTHREAD_INITIAL_STACK);
486 assert(FIELD_OFFSET(KTHREAD, Teb) == KTHREAD_TEB);
487 assert(FIELD_OFFSET(KTHREAD, KernelStack) == KTHREAD_KERNEL_STACK);
488 assert(FIELD_OFFSET(KTHREAD, PreviousMode) == KTHREAD_PREVIOUS_MODE);
489 assert(FIELD_OFFSET(KTHREAD, TrapFrame) == KTHREAD_TRAP_FRAME);
490 assert(FIELD_OFFSET(KTHREAD, CallbackStack) == KTHREAD_CALLBACK_STACK);
491 assert(FIELD_OFFSET(ETHREAD, ThreadsProcess) == ETHREAD_THREADS_PROCESS);
492 assert(FIELD_OFFSET(KPROCESS, DirectoryTableBase) ==
493 KPROCESS_DIRECTORY_TABLE_BASE);
494 assert(FIELD_OFFSET(KTRAP_FRAME, Reserved9) == KTRAP_FRAME_RESERVED9);
495 assert(FIELD_OFFSET(KV86M_TRAP_FRAME, regs) == TF_REGS);
496 assert(FIELD_OFFSET(KV86M_TRAP_FRAME, orig_ebp) == TF_ORIG_EBP);
497
498 assert(FIELD_OFFSET(KPCR, ExceptionList) == KPCR_EXCEPTION_LIST);
499 assert(FIELD_OFFSET(KPCR, Self) == KPCR_SELF);
500 assert(FIELD_OFFSET(KPCR, CurrentThread) == KPCR_CURRENT_THREAD);
501
502 LdrInit1();
503
504 KeLowerIrql(DISPATCH_LEVEL);
505
506 NtEarlyInitVdm();
507
508 MmInit1(FirstKrnlPhysAddr,
509 LastKrnlPhysAddr,
510 LastKernelAddress,
511 (PADDRESS_RANGE)&KeMemoryMap,
512 KeMemoryMapRangeCount);
513
514 /* create default nls tables */
515 RtlpInitNlsTables();
516
517 /*
518 * Initialize the kernel debugger
519 */
520 KdInitSystem (0, (PLOADER_PARAMETER_BLOCK)&KeLoaderBlock);
521
522 MmInit2();
523 KeInit2();
524
525 KeLowerIrql(PASSIVE_LEVEL);
526
527 if (!SeInit1())
528 KeBugCheck(SECURITY_INITIALIZATION_FAILED);
529
530 ObInit();
531
532 if (!SeInit2())
533 KeBugCheck(SECURITY1_INITIALIZATION_FAILED);
534
535 PiInitProcessManager();
536
537 KdInit1();
538
539 if (KdPollBreakIn ())
540 {
541 DbgBreakPointWithStatus (DBG_STATUS_CONTROL_C);
542 }
543
544 /*
545 * Display version number and copyright/warranty message
546 */
547 HalDisplayString("Starting ReactOS "KERNEL_VERSION_STR" (Build "
548 KERNEL_VERSION_BUILD_STR")\n");
549 HalDisplayString(RES_STR_LEGAL_COPYRIGHT);
550 HalDisplayString("\n\nReactOS is free software, covered by the GNU General "
551 "Public License, and you\n");
552 HalDisplayString("are welcome to change it and/or distribute copies of it "
553 "under certain\n");
554 HalDisplayString("conditions. There is absolutely no warranty for "
555 "ReactOS.\n\n");
556
557 /* Initialize all processors */
558 KeNumberProcessors = 0;
559
560 while (!HalAllProcessorsStarted())
561 {
562 PVOID ProcessorStack;
563
564 if (KeNumberProcessors != 0)
565 {
566 KePrepareForApplicationProcessorInit(KeNumberProcessors);
567 PsPrepareForApplicationProcessorInit(KeNumberProcessors);
568 }
569 /* Allocate a stack for use when booting the processor */
570 /* FIXME: The nonpaged memory for the stack is not released after use */
571 ProcessorStack =
572 ExAllocatePool(NonPagedPool, MM_STACK_SIZE) + MM_STACK_SIZE;
573 Ki386InitialStackArray[((int)KeNumberProcessors)] =
574 (PVOID)(ProcessorStack - MM_STACK_SIZE);
575 HalInitializeProcessor(KeNumberProcessors, ProcessorStack);
576 KeNumberProcessors++;
577 }
578
579 if (KeNumberProcessors > 1)
580 {
581 sprintf(str,
582 "Found %d system processors. [%lu MB Memory]\n",
583 KeNumberProcessors,
584 (KeLoaderBlock.MemHigher + 1088)/ 1024);
585 }
586 else
587 {
588 sprintf(str,
589 "Found 1 system processor. [%lu MB Memory]\n",
590 (KeLoaderBlock.MemHigher + 1088)/ 1024);
591 }
592 HalDisplayString(str);
593
594 /*
595 * Initialize various critical subsystems
596 */
597 HalInitSystem(1, (PLOADER_PARAMETER_BLOCK)&KeLoaderBlock);
598
599 ExInit();
600 IoInit();
601 PoInit();
602 LdrInitModuleManagement();
603 CmInitializeRegistry();
604 NtInit();
605 MmInit3();
606 CcInit();
607 KdInit2();
608
609 /* Report all resources used by hal */
610 HalReportResourceUsage();
611
612 /*
613 * Initalize services loaded at boot time
614 */
615 DPRINT("%d files loaded\n",KeLoaderBlock.ModsCount);
616 for (i=0; i < KeLoaderBlock.ModsCount; i++)
617 {
618 CPRINT("Module: '%s' at %08lx, length 0x%08lx\n",
619 KeLoaderModules[i].String,
620 KeLoaderModules[i].ModStart,
621 KeLoaderModules[i].ModEnd - KeLoaderModules[i].ModStart);
622 }
623
624 /* Pass 1: load nls files */
625 for (i = 1; i < KeLoaderBlock.ModsCount; i++)
626 {
627 name = (PCHAR)KeLoaderModules[i].String;
628 if (RtlpCheckFileNameExtension(name, ".nls"))
629 {
630 ULONG Mod2Start = 0;
631 ULONG Mod2End = 0;
632 ULONG Mod3Start = 0;
633 ULONG Mod3End = 0;
634
635 name = (PCHAR)KeLoaderModules[i+1].String;
636 if (RtlpCheckFileNameExtension(name, ".nls"))
637 {
638 Mod2Start = (ULONG)KeLoaderModules[i+1].ModStart;
639 Mod2End = (ULONG)KeLoaderModules[i+1].ModEnd;
640
641 name = (PCHAR)KeLoaderModules[i+2].String;
642 if (RtlpCheckFileNameExtension(name, ".nls"))
643 {
644 Mod3Start = (ULONG)KeLoaderModules[i+2].ModStart;
645 Mod3End = (ULONG)KeLoaderModules[i+2].ModEnd;
646 }
647 }
648
649 /* Initialize nls sections */
650 RtlpInitNlsSections((ULONG)KeLoaderModules[i].ModStart,
651 (ULONG)KeLoaderModules[i].ModEnd,
652 Mod2Start,
653 Mod2End,
654 Mod3Start,
655 Mod3End);
656 break;
657 }
658 }
659
660 /* Pass 2: load registry chunks passed in */
661 for (i = 1; i < KeLoaderBlock.ModsCount; i++)
662 {
663 start = KeLoaderModules[i].ModStart;
664 length = KeLoaderModules[i].ModEnd - start;
665 name = (PCHAR)KeLoaderModules[i].String;
666 if (RtlpCheckFileNameExtension(name, "") ||
667 RtlpCheckFileNameExtension(name, ".hiv"))
668 {
669 CPRINT("Process registry chunk at %08lx\n", start);
670 CmImportHive((PCHAR)start, length);
671 }
672 }
673
674 /* Initialize volatile registry settings */
675 CmInit2((PCHAR)KeLoaderBlock.CommandLine);
676
677 /*
678 * Enter the kernel debugger before starting up the boot drivers
679 */
680 #ifdef KDBG
681 KdbEnter();
682 #endif /* KDBG */
683
684 IoCreateDriverList();
685
686 /* Pass 3: process boot loaded drivers */
687 BootDriverCount = 0;
688 for (i=1; i < KeLoaderBlock.ModsCount; i++)
689 {
690 start = KeLoaderModules[i].ModStart;
691 length = KeLoaderModules[i].ModEnd - start;
692 name = (PCHAR)KeLoaderModules[i].String;
693 if (RtlpCheckFileNameExtension(name, ".sys") ||
694 RtlpCheckFileNameExtension(name, ".sym"))
695 {
696 CPRINT("Initializing driver '%s' at %08lx, length 0x%08lx\n",
697 name, start, length);
698 LdrInitializeBootStartDriver((PVOID)start, name, length);
699 }
700 if (RtlpCheckFileNameExtension(name, ".sys"))
701 BootDriverCount++;
702 }
703
704 if (BootDriverCount == 0)
705 {
706 DbgPrint("No boot drivers available.\n");
707 KeBugCheck(0);
708 }
709
710 /* Create ARC names for boot devices */
711 IoCreateArcNames();
712
713 /* Create the SystemRoot symbolic link */
714 CPRINT("CommandLine: %s\n", (PUCHAR)KeLoaderBlock.CommandLine);
715 IoCreateSystemRootLink((PUCHAR)KeLoaderBlock.CommandLine);
716
717 #ifdef DBGPRINT_FILE_LOG
718 /* On the assumption that we can now access disks start up the debug
719 logger thread */
720 DebugLogInit2();
721 #endif /* DBGPRINT_FILE_LOG */
722
723
724 PiInitDefaultLocale();
725
726 /*
727 * Start the motherboard enumerator (the HAL)
728 */
729 HalInitSystem(2, (PLOADER_PARAMETER_BLOCK)&KeLoaderBlock);
730 #if 0
731 /*
732 * Load boot start drivers
733 */
734 IopLoadBootStartDrivers();
735 #else
736 /*
737 * Load Auto configured drivers
738 */
739 LdrLoadAutoConfigDrivers();
740 #endif
741
742 IoDestroyDriverList();
743
744 /*
745 * Assign drive letters
746 */
747 IoAssignDriveLetters ((PLOADER_PARAMETER_BLOCK)&KeLoaderBlock,
748 NULL,
749 NULL,
750 NULL);
751
752 /*
753 * Initialize shared user page:
754 * - set dos system path, dos device map, etc.
755 */
756 InitSystemSharedUserPage ((PUCHAR)KeLoaderBlock.CommandLine);
757
758 /*
759 * Launch initial process
760 */
761 LdrLoadInitialProcess();
762
763 PsTerminateSystemThread(STATUS_SUCCESS);
764 }
765
766
767 VOID
768 KiSystemStartup(BOOLEAN BootProcessor)
769 {
770 HalInitSystem (0, (PLOADER_PARAMETER_BLOCK)&KeLoaderBlock);
771
772 if (BootProcessor)
773 {
774 /* Never returns */
775 ExpInitializeExecutive();
776 KeBugCheck(0);
777 }
778 /* Do application processor initialization */
779 KeApplicationProcessorInit();
780 PsApplicationProcessorInit();
781 KeLowerIrql(PASSIVE_LEVEL);
782 PsIdleThreadMain(NULL);
783 KeBugCheck(0);
784 for(;;);
785 }
786
787 VOID
788 _main (ULONG MultiBootMagic, PLOADER_PARAMETER_BLOCK _LoaderBlock)
789 /*
790 * FUNCTION: Called by the boot loader to start the kernel
791 * ARGUMENTS:
792 * LoaderBlock = Pointer to boot parameters initialized by the boot
793 * loader
794 * NOTE: The boot parameters are stored in low memory which will become
795 * invalid after the memory managment is initialized so we make a local copy.
796 */
797 {
798 ULONG i;
799 ULONG size;
800 ULONG last_kernel_address;
801 extern ULONG _bss_end__;
802 ULONG HalBase;
803 ULONG DriverBase;
804 ULONG DriverSize;
805
806 /* Low level architecture specific initialization */
807 KeInit1();
808
809 /*
810 * Copy the parameters to a local buffer because lowmem will go away
811 */
812 memcpy(&KeLoaderBlock, _LoaderBlock, sizeof(LOADER_PARAMETER_BLOCK));
813 memcpy(&KeLoaderModules[1], (PVOID)KeLoaderBlock.ModsAddr,
814 sizeof(LOADER_MODULE) * KeLoaderBlock.ModsCount);
815 KeLoaderBlock.ModsCount++;
816 KeLoaderBlock.ModsAddr = (ULONG)&KeLoaderModules;
817
818 if (((PUCHAR)_LoaderBlock->CommandLine)[0] == '(')
819 {
820 ULONG DiskNumber, PartNumber;
821 PCH p;
822 CHAR Temp[256];
823 PCH options;
824 PCH s1;
825
826 if (((PUCHAR)_LoaderBlock->CommandLine)[1] == 'h' &&
827 ((PUCHAR)_LoaderBlock->CommandLine)[2] == 'd')
828 {
829 DiskNumber = ((PUCHAR)_LoaderBlock->CommandLine)[3] - '0';
830 PartNumber = ((PUCHAR)_LoaderBlock->CommandLine)[5] - '0';
831 }
832 strcpy(Temp, &((PUCHAR)_LoaderBlock->CommandLine)[7]);
833 if ((options = strchr(Temp, ' ')) != NULL)
834 {
835 *options = 0;
836 options++;
837 }
838 else
839 {
840 options = "";
841 }
842 if ((s1 = strrchr(Temp, '/')) != NULL)
843 {
844 *s1 = 0;
845 if ((s1 = strrchr(Temp, '/')) != NULL)
846 {
847 *s1 = 0;
848 }
849 }
850 sprintf(KeLoaderCommandLine,
851 "multi(0)disk(0)rdisk(%ld)partition(%ld)%s %s",
852 DiskNumber, PartNumber + 1, Temp, options);
853
854 p = KeLoaderCommandLine;
855 while (*p != 0 && *p != ' ')
856 {
857 if ((*p) == '/')
858 {
859 (*p) = '\\';
860 }
861 p++;
862 }
863 DPRINT1("Command Line: %s\n", KeLoaderCommandLine);
864 }
865 else
866 {
867 strcpy(KeLoaderCommandLine, (PUCHAR)_LoaderBlock->CommandLine);
868 }
869 KeLoaderBlock.CommandLine = (ULONG)KeLoaderCommandLine;
870
871 strcpy(KeLoaderModuleStrings[0], "ntoskrnl.exe");
872 KeLoaderModules[0].String = (ULONG)KeLoaderModuleStrings[0];
873 KeLoaderModules[0].ModStart = 0xC0000000;
874 KeLoaderModules[0].ModEnd = PAGE_ROUND_UP((ULONG)&_bss_end__);
875 for (i = 1; i < KeLoaderBlock.ModsCount; i++)
876 {
877 strcpy(KeLoaderModuleStrings[i], (PUCHAR)KeLoaderModules[i].String);
878 KeLoaderModules[i].ModStart -= 0x200000;
879 KeLoaderModules[i].ModStart += 0xc0000000;
880 KeLoaderModules[i].ModEnd -= 0x200000;
881 KeLoaderModules[i].ModEnd += 0xc0000000;
882 KeLoaderModules[i].String = (ULONG)KeLoaderModuleStrings[i];
883 }
884
885 #ifdef HAL_DBG
886 HalnInitializeDisplay((PLOADER_PARAMETER_BLOCK)&KeLoaderBlock);
887 #endif
888
889 HalBase = KeLoaderModules[1].ModStart;
890 DriverBase =
891 PAGE_ROUND_UP(KeLoaderModules[KeLoaderBlock.ModsCount - 1].ModEnd);
892
893 /*
894 * Process hal.dll
895 */
896 LdrSafePEProcessModule((PVOID)HalBase, (PVOID)DriverBase, (PVOID)0xC0000000, &DriverSize);
897
898 LdrHalBase = (ULONG_PTR)DriverBase;
899 last_kernel_address = DriverBase + DriverSize;
900
901 /*
902 * Process ntoskrnl.exe
903 */
904 LdrSafePEProcessModule((PVOID)0xC0000000, (PVOID)0xC0000000, (PVOID)DriverBase, &DriverSize);
905
906 FirstKrnlPhysAddr = KeLoaderModules[0].ModStart - 0xc0000000 + 0x200000;
907 LastKrnlPhysAddr = last_kernel_address - 0xc0000000 + 0x200000;
908 LastKernelAddress = last_kernel_address;
909
910 #ifndef ACPI
911 /* FIXME: VMware does not like it when ReactOS is using the BIOS memory map */
912 KeLoaderBlock.Flags &= ~MB_FLAGS_MMAP_INFO;
913 #endif
914
915 KeMemoryMapRangeCount = 0;
916 if (KeLoaderBlock.Flags & MB_FLAGS_MMAP_INFO)
917 {
918 /* We have a memory map from the nice BIOS */
919 size = *((PULONG)(KeLoaderBlock.MmapAddr - sizeof(ULONG)));
920 i = 0;
921 while (i < KeLoaderBlock.MmapLength)
922 {
923 memcpy (&KeMemoryMap[KeMemoryMapRangeCount],
924 (PVOID)(KeLoaderBlock.MmapAddr + i),
925 sizeof(ADDRESS_RANGE));
926 KeMemoryMapRangeCount++;
927 i += size;
928 }
929 }
930
931 KiSystemStartup(1);
932 }
933
934 /* EOF */
935