3 * Copyright (C) 1998, 1999, 2000, 2001 ReactOS Team
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.
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.
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.
19 /* $Id: main.c,v 1.169 2003/08/24 12:08:16 dwelch Exp $
21 * PROJECT: ReactOS kernel
22 * FILE: ntoskrnl/ke/main.c
23 * PURPOSE: Initalizes the kernel
24 * PROGRAMMER: David Welch (welch@cwcom.net)
29 /* INCLUDES *****************************************************************/
31 #define NTOS_MODE_KERNEL
33 #include <internal/ntoskrnl.h>
34 #include <reactos/resource.h>
35 #include <internal/mm.h>
36 #include <internal/ifs.h>
37 #include <internal/module.h>
38 #include <internal/ldr.h>
39 #include <internal/ex.h>
40 #include <internal/ps.h>
41 #include <internal/ke.h>
42 #include <internal/io.h>
43 #include <internal/po.h>
44 #include <internal/cc.h>
45 #include <internal/se.h>
46 #include <internal/v86m.h>
47 #include <internal/kd.h>
48 #include <internal/trap.h>
49 #include "../dbg/kdb.h"
50 #include <internal/registry.h>
51 #include <internal/nls.h>
52 #include <reactos/bugcodes.h>
53 #include <ntos/bootvid.h>
56 #include <internal/ntosdbg.h>
62 #include <internal/debug.h>
64 /* GLOBALS *******************************************************************/
66 ULONG EXPORTED NtBuildNumber
= KERNEL_VERSION_BUILD
;
67 ULONG EXPORTED NtGlobalFlag
= 0;
68 CHAR EXPORTED KeNumberProcessors
;
69 LOADER_PARAMETER_BLOCK EXPORTED KeLoaderBlock
;
70 ULONG EXPORTED KeDcacheFlushCount
= 0;
71 ULONG EXPORTED KeIcacheFlushCount
= 0;
72 static LOADER_MODULE KeLoaderModules
[64];
73 static UCHAR KeLoaderModuleStrings
[64][256];
74 static UCHAR KeLoaderCommandLine
[256];
75 static ADDRESS_RANGE KeMemoryMap
[64];
76 static ULONG KeMemoryMapRangeCount
;
77 static ULONG FirstKrnlPhysAddr
;
78 static ULONG LastKrnlPhysAddr
;
79 static ULONG LastKernelAddress
;
80 volatile BOOLEAN Initialized
= FALSE
;
82 extern PVOID Ki386InitialStackArray
[MAXIMUM_PROCESSORS
];
85 /* FUNCTIONS ****************************************************************/
88 RtlpCheckFileNameExtension(PCHAR FileName
,
93 Ext
= strrchr(FileName
, '.');
96 if ((Extension
== NULL
) || (*Extension
== 0))
102 if (*Extension
!= '.')
105 if (_stricmp(Ext
, Extension
) == 0)
113 InitSystemSharedUserPage (PCSZ ParameterLine
)
115 UNICODE_STRING ArcDeviceName
;
116 UNICODE_STRING ArcName
;
117 UNICODE_STRING BootPath
;
118 UNICODE_STRING DriveDeviceName
;
119 UNICODE_STRING DriveName
;
120 WCHAR DriveNameBuffer
[20];
122 PWCHAR ArcNameBuffer
;
126 OBJECT_ATTRIBUTES ObjectAttributes
;
129 BOOLEAN BootDriveFound
;
133 * The shared user page has been zeroed-out right after creation.
134 * There is NO need to do this again.
137 SharedUserData
->NtProductType
= NtProductWinNt
;
139 BootDriveFound
= FALSE
;
142 * Retrieve the current dos system path
143 * (e.g.: C:\reactos) from the given arc path
144 * (e.g.: multi(0)disk(0)rdisk(0)partititon(1)\reactos)
145 * Format: "<arc_name>\<path> [options...]"
148 /* create local parameter line copy */
149 ParamBuffer
= ExAllocatePool (PagedPool
, 256);
150 strcpy (ParamBuffer
, (char *)ParameterLine
);
151 DPRINT("%s\n", ParamBuffer
);
153 /* cut options off */
154 p
= strchr (ParamBuffer
, ' ');
159 DPRINT("%s\n", ParamBuffer
);
162 p
= strchr (ParamBuffer
, '\\');
165 DPRINT("Boot path: %s\n", p
);
166 RtlCreateUnicodeStringFromAsciiz (&BootPath
, p
);
171 DPRINT("Boot path: %s\n", "\\");
172 RtlCreateUnicodeStringFromAsciiz (&BootPath
, "\\");
174 DPRINT("Arc name: %s\n", ParamBuffer
);
176 /* Only arc name left - build full arc name */
177 ArcNameBuffer
= ExAllocatePool (PagedPool
, 256 * sizeof(WCHAR
));
178 swprintf (ArcNameBuffer
, L
"\\ArcName\\%S", ParamBuffer
);
179 RtlInitUnicodeString (&ArcName
, ArcNameBuffer
);
180 DPRINT("Arc name: %wZ\n", &ArcName
);
182 /* free ParamBuffer */
183 ExFreePool (ParamBuffer
);
185 /* allocate arc device name string */
186 ArcDeviceName
.Length
= 0;
187 ArcDeviceName
.MaximumLength
= 256 * sizeof(WCHAR
);
188 ArcDeviceName
.Buffer
= ExAllocatePool (PagedPool
, 256 * sizeof(WCHAR
));
190 InitializeObjectAttributes (&ObjectAttributes
,
196 Status
= NtOpenSymbolicLinkObject (&Handle
,
197 SYMBOLIC_LINK_ALL_ACCESS
,
199 RtlFreeUnicodeString (&ArcName
);
200 if (!NT_SUCCESS(Status
))
202 RtlFreeUnicodeString (&BootPath
);
203 RtlFreeUnicodeString (&ArcDeviceName
);
204 CPRINT("NtOpenSymbolicLinkObject() failed (Status %x)\n",
210 Status
= NtQuerySymbolicLinkObject (Handle
,
214 if (!NT_SUCCESS(Status
))
216 RtlFreeUnicodeString (&BootPath
);
217 RtlFreeUnicodeString (&ArcDeviceName
);
218 CPRINT("NtQuerySymbolicObject() failed (Status %x)\n",
223 DPRINT("Length: %lu ArcDeviceName: %wZ\n", Length
, &ArcDeviceName
);
226 /* allocate device name string */
227 DriveDeviceName
.Length
= 0;
228 DriveDeviceName
.MaximumLength
= 256 * sizeof(WCHAR
);
229 DriveDeviceName
.Buffer
= ExAllocatePool (PagedPool
, 256 * sizeof(WCHAR
));
231 for (i
= 0; i
< 26; i
++)
233 swprintf (DriveNameBuffer
, L
"\\??\\%C:", 'A' + i
);
234 RtlInitUnicodeString (&DriveName
,
237 InitializeObjectAttributes (&ObjectAttributes
,
243 Status
= NtOpenSymbolicLinkObject (&Handle
,
244 SYMBOLIC_LINK_ALL_ACCESS
,
246 if (!NT_SUCCESS(Status
))
248 DPRINT("Failed to open link %wZ\n",
253 Status
= NtQuerySymbolicLinkObject (Handle
,
256 if (!NT_SUCCESS(Status
))
258 DPRINT("Failed query open link %wZ\n",
262 DPRINT("Opened link: %wZ ==> %wZ\n",
263 &DriveName
, &DriveDeviceName
);
265 if (!RtlCompareUnicodeString (&ArcDeviceName
, &DriveDeviceName
, FALSE
))
267 DPRINT("DOS Boot path: %c:%wZ\n", 'A' + i
, &BootPath
);
268 swprintf(SharedUserData
->NtSystemRoot
,
269 L
"%C:%wZ", 'A' + i
, &BootPath
);
271 BootDriveFound
= TRUE
;
277 RtlFreeUnicodeString (&BootPath
);
278 RtlFreeUnicodeString (&DriveDeviceName
);
279 RtlFreeUnicodeString (&ArcDeviceName
);
281 DPRINT("DosDeviceMap: 0x%x\n", SharedUserData
->DosDeviceMap
);
283 if (BootDriveFound
== FALSE
)
285 DbgPrint("No system drive found!\n");
291 MiFreeBootDriverMemory(PVOID StartAddress
, ULONG Length
)
293 PHYSICAL_ADDRESS Page
;
296 for (i
= 0; i
< PAGE_ROUND_UP(Length
)/PAGE_SIZE
; i
++)
298 Page
= MmGetPhysicalAddressForProcess(NULL
, StartAddress
+ i
* PAGE_SIZE
);
299 MmDeleteVirtualMapping(NULL
, StartAddress
+ i
* PAGE_SIZE
, FALSE
, NULL
, NULL
);
300 MmDereferencePage(Page
);
305 ExpInitializeExecutive(VOID
)
307 LARGE_INTEGER Timeout
;
308 HANDLE ProcessHandle
;
310 ULONG BootDriverCount
;
320 BOOLEAN NoBootScreen
= FALSE
;
322 HANDLE InitDoneEventHandle
;
323 OBJECT_ATTRIBUTES ObjectAttributes
;
326 * Fail at runtime if someone has changed various structures without
327 * updating the offsets used for the assembler code.
329 assert(FIELD_OFFSET(KTHREAD
, InitialStack
) == KTHREAD_INITIAL_STACK
);
330 assert(FIELD_OFFSET(KTHREAD
, Teb
) == KTHREAD_TEB
);
331 assert(FIELD_OFFSET(KTHREAD
, KernelStack
) == KTHREAD_KERNEL_STACK
);
332 assert(FIELD_OFFSET(KTHREAD
, ServiceTable
) == KTHREAD_SERVICE_TABLE
);
333 assert(FIELD_OFFSET(KTHREAD
, PreviousMode
) == KTHREAD_PREVIOUS_MODE
);
334 assert(FIELD_OFFSET(KTHREAD
, TrapFrame
) == KTHREAD_TRAP_FRAME
);
335 assert(FIELD_OFFSET(KTHREAD
, CallbackStack
) == KTHREAD_CALLBACK_STACK
);
336 assert(FIELD_OFFSET(ETHREAD
, ThreadsProcess
) == ETHREAD_THREADS_PROCESS
);
337 assert(FIELD_OFFSET(KPROCESS
, DirectoryTableBase
) ==
338 KPROCESS_DIRECTORY_TABLE_BASE
);
339 assert(FIELD_OFFSET(KPROCESS
, IopmOffset
) == KPROCESS_IOPM_OFFSET
);
340 assert(FIELD_OFFSET(KPROCESS
, LdtDescriptor
) == KPROCESS_LDT_DESCRIPTOR0
);
341 assert(FIELD_OFFSET(KTRAP_FRAME
, Reserved9
) == KTRAP_FRAME_RESERVED9
);
342 assert(FIELD_OFFSET(KV86M_TRAP_FRAME
, regs
) == TF_REGS
);
343 assert(FIELD_OFFSET(KV86M_TRAP_FRAME
, orig_ebp
) == TF_ORIG_EBP
);
345 assert(FIELD_OFFSET(KPCR
, Tib
.ExceptionList
) == KPCR_EXCEPTION_LIST
);
346 assert(FIELD_OFFSET(KPCR
, Self
) == KPCR_SELF
);
347 assert(FIELD_OFFSET(IKPCR
, Tib
.ExceptionList
) == KPCR_EXCEPTION_LIST
);
348 assert(FIELD_OFFSET(IKPCR
, Self
) == KPCR_SELF
);
349 assert(FIELD_OFFSET(IKPCR
, CurrentThread
) == KPCR_CURRENT_THREAD
);
353 KeLowerIrql(DISPATCH_LEVEL
);
357 p1
= (PCHAR
)KeLoaderBlock
.CommandLine
;
360 while(*p1
&& (p2
= strchr(p1
, '/')))
363 if (!_strnicmp(p2
, "MAXMEM", 6))
366 while (isspace(*p2
)) p2
++;
370 while(isspace(*p2
)) p2
++;
375 MaxMem
= MaxMem
* 10 + *p2
- '0';
382 else if (!_strnicmp(p2
, "NOBOOTSCREEN", 12))
390 MmInit1(FirstKrnlPhysAddr
,
393 (PADDRESS_RANGE
)&KeMemoryMap
,
394 KeMemoryMapRangeCount
,
395 MaxMem
> 8 ? MaxMem
: 4096);
397 /* Import ANSI code page table */
398 for (i
= 1; i
< KeLoaderBlock
.ModsCount
; i
++)
400 start
= KeLoaderModules
[i
].ModStart
;
401 length
= KeLoaderModules
[i
].ModEnd
- start
;
403 name
= strrchr((PCHAR
)KeLoaderModules
[i
].String
, '\\');
406 name
= (PCHAR
)KeLoaderModules
[i
].String
;
413 if (!_stricmp (name
, "ansi.nls"))
415 RtlpImportAnsiCodePage((PUSHORT
)start
, length
);
419 /* Import OEM code page table */
420 for (i
= 1; i
< KeLoaderBlock
.ModsCount
; i
++)
422 start
= KeLoaderModules
[i
].ModStart
;
423 length
= KeLoaderModules
[i
].ModEnd
- start
;
425 name
= strrchr((PCHAR
)KeLoaderModules
[i
].String
, '\\');
428 name
= (PCHAR
)KeLoaderModules
[i
].String
;
435 if (!_stricmp (name
, "oem.nls"))
437 RtlpImportOemCodePage((PUSHORT
)start
, length
);
441 /* Import Unicode casemap table */
442 for (i
= 1; i
< KeLoaderBlock
.ModsCount
; i
++)
444 start
= KeLoaderModules
[i
].ModStart
;
445 length
= KeLoaderModules
[i
].ModEnd
- start
;
447 name
= strrchr((PCHAR
)KeLoaderModules
[i
].String
, '\\');
450 name
= (PCHAR
)KeLoaderModules
[i
].String
;
457 if (!_stricmp (name
, "casemap.nls"))
459 RtlpImportUnicodeCasemap((PUSHORT
)start
, length
);
463 /* Create initial NLS tables */
464 RtlpCreateInitialNlsTables();
467 * Initialize the kernel debugger
469 KdInitSystem (0, (PLOADER_PARAMETER_BLOCK
)&KeLoaderBlock
);
474 KeLowerIrql(PASSIVE_LEVEL
);
477 KEBUGCHECK(SECURITY_INITIALIZATION_FAILED
);
482 KEBUGCHECK(SECURITY1_INITIALIZATION_FAILED
);
484 PiInitProcessManager();
488 if (KdPollBreakIn ())
490 DbgBreakPointWithStatus (DBG_STATUS_CONTROL_C
);
493 /* Initialize all processors */
494 KeNumberProcessors
= 0;
496 while (!HalAllProcessorsStarted())
498 PVOID ProcessorStack
;
500 if (KeNumberProcessors
!= 0)
502 KePrepareForApplicationProcessorInit(KeNumberProcessors
);
503 PsPrepareForApplicationProcessorInit(KeNumberProcessors
);
505 /* Allocate a stack for use when booting the processor */
506 /* FIXME: The nonpaged memory for the stack is not released after use */
508 ExAllocatePool(NonPagedPool
, MM_STACK_SIZE
) + MM_STACK_SIZE
;
509 Ki386InitialStackArray
[((int)KeNumberProcessors
)] =
510 (PVOID
)(ProcessorStack
- MM_STACK_SIZE
);
511 HalInitializeProcessor(KeNumberProcessors
, ProcessorStack
);
512 KeNumberProcessors
++;
516 * Initialize various critical subsystems
518 HalInitSystem(1, (PLOADER_PARAMETER_BLOCK
)&KeLoaderBlock
);
523 LdrInitModuleManagement();
524 CmInitializeRegistry();
529 FsRtlpInitFileLockingImplementation();
531 /* Report all resources used by hal */
532 HalReportResourceUsage();
535 * Clear the screen to blue
537 HalInitSystem(2, (PLOADER_PARAMETER_BLOCK
)&KeLoaderBlock
);
540 * Display version number and copyright/warranty message
542 HalDisplayString("Starting ReactOS "KERNEL_VERSION_STR
" (Build "
543 KERNEL_VERSION_BUILD_STR
")\n");
544 HalDisplayString(RES_STR_LEGAL_COPYRIGHT
);
545 HalDisplayString("\n\nReactOS is free software, covered by the GNU General "
546 "Public License, and you\n");
547 HalDisplayString("are welcome to change it and/or distribute copies of it "
549 HalDisplayString("conditions. There is absolutely no warranty for "
552 if (KeNumberProcessors
> 1)
555 "Found %d system processors. [%lu MB Memory]\n",
557 (KeLoaderBlock
.MemHigher
+ 1088)/ 1024);
562 "Found 1 system processor. [%lu MB Memory]\n",
563 (KeLoaderBlock
.MemHigher
+ 1088)/ 1024);
565 HalDisplayString(str
);
570 /* Create the NLS section */
571 RtlpCreateNlsSection();
574 * Initalize services loaded at boot time
576 DPRINT("%d files loaded\n",KeLoaderBlock
.ModsCount
);
577 for (i
=0; i
< KeLoaderBlock
.ModsCount
; i
++)
579 CPRINT("Module: '%s' at %08lx, length 0x%08lx\n",
580 KeLoaderModules
[i
].String
,
581 KeLoaderModules
[i
].ModStart
,
582 KeLoaderModules
[i
].ModEnd
- KeLoaderModules
[i
].ModStart
);
585 /* Pass 1: import system hive registry chunk */
587 for (i
= 1; i
< KeLoaderBlock
.ModsCount
; i
++)
589 start
= KeLoaderModules
[i
].ModStart
;
590 length
= KeLoaderModules
[i
].ModEnd
- start
;
592 DPRINT("Module: '%s'\n", (PCHAR
)KeLoaderModules
[i
].String
);
593 name
= strrchr((PCHAR
)KeLoaderModules
[i
].String
, '\\');
596 name
= (PCHAR
)KeLoaderModules
[i
].String
;
603 if (!_stricmp (name
, "system") ||
604 !_stricmp (name
, "system.hiv"))
606 CPRINT("Process system hive registry chunk at %08lx\n", start
);
608 CmImportSystemHive((PCHAR
)start
, length
);
612 /* Pass 2: import hardware hive registry chunk */
613 for (i
= 1; i
< KeLoaderBlock
.ModsCount
; i
++)
615 start
= KeLoaderModules
[i
].ModStart
;
616 length
= KeLoaderModules
[i
].ModEnd
- start
;
617 name
= (PCHAR
)KeLoaderModules
[i
].String
;
618 if (!_stricmp (name
, "hardware") ||
619 !_stricmp (name
, "hardware.hiv"))
621 CPRINT("Process hardware hive registry chunk at %08lx\n", start
);
622 CmImportHardwareHive((PCHAR
)start
, length
);
626 /* Create dummy keys if no hardware hive was found */
627 CmImportHardwareHive (NULL
, 0);
629 /* Initialize volatile registry settings */
630 if (SetupBoot
== FALSE
)
632 CmInit2((PCHAR
)KeLoaderBlock
.CommandLine
);
636 * Enter the kernel debugger before starting up the boot drivers
642 IoCreateDriverList();
646 /* Pass 3: process boot loaded drivers */
648 for (i
=1; i
< KeLoaderBlock
.ModsCount
; i
++)
650 start
= KeLoaderModules
[i
].ModStart
;
651 length
= KeLoaderModules
[i
].ModEnd
- start
;
652 name
= (PCHAR
)KeLoaderModules
[i
].String
;
653 if (RtlpCheckFileNameExtension(name
, ".sys") ||
654 RtlpCheckFileNameExtension(name
, ".sym"))
656 CPRINT("Initializing driver '%s' at %08lx, length 0x%08lx\n",
657 name
, start
, length
);
658 LdrInitializeBootStartDriver((PVOID
)start
, name
, length
);
660 if (RtlpCheckFileNameExtension(name
, ".sys"))
664 /* Pass 4: free memory for all boot files, except ntoskrnl.exe and hal.dll */
665 for (i
= 2; i
< KeLoaderBlock
.ModsCount
; i
++)
668 /* Do not free the memory from symbol files, if the kernel debugger is activ */
669 if (!RtlpCheckFileNameExtension(name
, ".sym"))
672 MiFreeBootDriverMemory((PVOID
)KeLoaderModules
[i
].ModStart
,
673 KeLoaderModules
[i
].ModEnd
- KeLoaderModules
[i
].ModStart
);
677 if (BootDriverCount
== 0)
679 DbgPrint("No boot drivers available.\n");
683 /* Display the boot screen image if not disabled */
686 InbvEnableBootDriver(TRUE
);
689 /* Create ARC names for boot devices */
692 /* Create the SystemRoot symbolic link */
693 CPRINT("CommandLine: %s\n", (PUCHAR
)KeLoaderBlock
.CommandLine
);
694 Status
= IoCreateSystemRootLink((PUCHAR
)KeLoaderBlock
.CommandLine
);
695 if (!NT_SUCCESS(Status
))
696 KEBUGCHECK(INACCESSIBLE_BOOT_DEVICE
);
698 #ifdef DBGPRINT_FILE_LOG
699 /* On the assumption that we can now access disks start up the debug
702 #endif /* DBGPRINT_FILE_LOG */
708 PiInitDefaultLocale();
712 * Load boot start drivers
714 IopLoadBootStartDrivers();
717 * Load Auto configured drivers
719 LdrLoadAutoConfigDrivers();
722 IoDestroyDriverList();
725 * Assign drive letters
727 IoAssignDriveLetters ((PLOADER_PARAMETER_BLOCK
)&KeLoaderBlock
,
733 * Initialize shared user page:
734 * - set dos system path, dos device map, etc.
736 InitSystemSharedUserPage ((PUCHAR
)KeLoaderBlock
.CommandLine
);
738 /* Create 'ReactOSInitDone' event */
739 RtlInitUnicodeString(&Name
, L
"\\ReactOSInitDone");
740 InitializeObjectAttributes(&ObjectAttributes
,
745 Status
= NtCreateEvent(&InitDoneEventHandle
,
748 FALSE
, /* Synchronization event */
749 FALSE
); /* Not signalled */
750 if (!NT_SUCCESS(Status
))
752 DPRINT1("Failed to create 'ReactOSInitDone' event (Status 0x%x)\n", Status
);
753 InitDoneEventHandle
= INVALID_HANDLE_VALUE
;
757 * Launch initial process
759 Status
= LdrLoadInitialProcess(&ProcessHandle
,
761 if (!NT_SUCCESS(Status
))
763 KEBUGCHECKEX(SESSION4_INITIALIZATION_FAILED
, Status
, 0, 0, 0);
766 if (InitDoneEventHandle
!= INVALID_HANDLE_VALUE
)
768 HANDLE Handles
[2]; /* Init event, Initial process */
770 Handles
[0] = InitDoneEventHandle
;
771 Handles
[1] = ProcessHandle
;
773 /* Wait for the system to be initialized */
774 Timeout
.QuadPart
= -1200000000LL; /* 120 second timeout */
775 Status
= NtWaitForMultipleObjects(((LONG
) sizeof(Handles
) / sizeof(HANDLE
)),
778 FALSE
, /* Non-alertable */
780 if (!NT_SUCCESS(Status
))
782 DPRINT1("NtWaitForMultipleObjects failed with status 0x%x!\n", Status
);
784 else if (Status
== STATUS_TIMEOUT
)
786 DPRINT1("WARNING: System not initialized after 120 seconds.\n");
788 else if (Status
== STATUS_WAIT_0
+ 1)
791 * Crash the system if the initial process was terminated.
793 KEBUGCHECKEX(SESSION5_INITIALIZATION_FAILED
, Status
, 0, 0, 0);
798 InbvEnableBootDriver(FALSE
);
801 NtSetEvent(InitDoneEventHandle
, NULL
);
803 NtClose(InitDoneEventHandle
);
807 /* On failure to create 'ReactOSInitDone' event, go to text mode ASAP */
810 InbvEnableBootDriver(FALSE
);
814 * Crash the system if the initial process terminates within 5 seconds.
816 Timeout
.QuadPart
= -50000000LL;
817 Status
= NtWaitForSingleObject(ProcessHandle
,
820 if (Status
!= STATUS_TIMEOUT
)
822 KEBUGCHECKEX(SESSION5_INITIALIZATION_FAILED
, Status
, 0, 0, 0);
826 NtClose(ThreadHandle
);
827 NtClose(ProcessHandle
);
829 PsTerminateSystemThread(STATUS_SUCCESS
);
834 KiSystemStartup(BOOLEAN BootProcessor
)
836 HalInitSystem (0, (PLOADER_PARAMETER_BLOCK
)&KeLoaderBlock
);
841 ExpInitializeExecutive();
844 /* Do application processor initialization */
845 KeApplicationProcessorInit();
846 PsApplicationProcessorInit();
847 KeLowerIrql(PASSIVE_LEVEL
);
848 PsIdleThreadMain(NULL
);
854 _main (ULONG MultiBootMagic
, PLOADER_PARAMETER_BLOCK _LoaderBlock
)
856 * FUNCTION: Called by the boot loader to start the kernel
858 * LoaderBlock = Pointer to boot parameters initialized by the boot
860 * NOTE: The boot parameters are stored in low memory which will become
861 * invalid after the memory managment is initialized so we make a local copy.
866 ULONG last_kernel_address
;
867 extern ULONG _bss_end__
;
872 /* Low level architecture specific initialization */
876 * Copy the parameters to a local buffer because lowmem will go away
878 memcpy(&KeLoaderBlock
, _LoaderBlock
, sizeof(LOADER_PARAMETER_BLOCK
));
879 memcpy(&KeLoaderModules
[1], (PVOID
)KeLoaderBlock
.ModsAddr
,
880 sizeof(LOADER_MODULE
) * KeLoaderBlock
.ModsCount
);
881 KeLoaderBlock
.ModsCount
++;
882 KeLoaderBlock
.ModsAddr
= (ULONG
)&KeLoaderModules
;
885 * Convert a path specification in the grub format to one understood by the
886 * rest of the kernel.
888 if (((PUCHAR
)_LoaderBlock
->CommandLine
)[0] == '(')
890 ULONG DiskNumber
= 0, PartNumber
= 0;
896 if (((PUCHAR
)_LoaderBlock
->CommandLine
)[1] == 'h' &&
897 ((PUCHAR
)_LoaderBlock
->CommandLine
)[2] == 'd')
899 DiskNumber
= ((PUCHAR
)_LoaderBlock
->CommandLine
)[3] - '0';
900 PartNumber
= ((PUCHAR
)_LoaderBlock
->CommandLine
)[5] - '0';
902 strcpy(Temp
, &((PUCHAR
)_LoaderBlock
->CommandLine
)[7]);
903 if ((options
= strchr(Temp
, ' ')) != NULL
)
912 if ((s1
= strrchr(Temp
, '/')) != NULL
)
915 if ((s1
= strrchr(Temp
, '/')) != NULL
)
920 sprintf(KeLoaderCommandLine
,
921 "multi(0)disk(0)rdisk(%lu)partition(%lu)%s %s",
922 DiskNumber
, PartNumber
+ 1, Temp
, options
);
924 p
= KeLoaderCommandLine
;
925 while (*p
!= 0 && *p
!= ' ')
933 DPRINT1("Command Line: %s\n", KeLoaderCommandLine
);
937 strcpy(KeLoaderCommandLine
, (PUCHAR
)_LoaderBlock
->CommandLine
);
939 KeLoaderBlock
.CommandLine
= (ULONG
)KeLoaderCommandLine
;
941 strcpy(KeLoaderModuleStrings
[0], "ntoskrnl.exe");
942 KeLoaderModules
[0].String
= (ULONG
)KeLoaderModuleStrings
[0];
943 KeLoaderModules
[0].ModStart
= 0xC0000000;
944 KeLoaderModules
[0].ModEnd
= PAGE_ROUND_UP((ULONG
)&_bss_end__
);
945 for (i
= 1; i
< KeLoaderBlock
.ModsCount
; i
++)
948 if ((s
= strrchr((PUCHAR
)KeLoaderModules
[i
].String
, '/')) != 0)
950 strcpy(KeLoaderModuleStrings
[i
], s
+ 1);
954 strcpy(KeLoaderModuleStrings
[i
], (PUCHAR
)KeLoaderModules
[i
].String
);
956 KeLoaderModules
[i
].ModStart
-= 0x200000;
957 KeLoaderModules
[i
].ModStart
+= 0xc0000000;
958 KeLoaderModules
[i
].ModEnd
-= 0x200000;
959 KeLoaderModules
[i
].ModEnd
+= 0xc0000000;
960 KeLoaderModules
[i
].String
= (ULONG
)KeLoaderModuleStrings
[i
];
964 HalnInitializeDisplay((PLOADER_PARAMETER_BLOCK
)&KeLoaderBlock
);
967 HalBase
= KeLoaderModules
[1].ModStart
;
969 PAGE_ROUND_UP(KeLoaderModules
[KeLoaderBlock
.ModsCount
- 1].ModEnd
);
974 LdrSafePEProcessModule((PVOID
)HalBase
, (PVOID
)DriverBase
, (PVOID
)0xC0000000, &DriverSize
);
976 LdrHalBase
= (ULONG_PTR
)DriverBase
;
977 last_kernel_address
= DriverBase
+ DriverSize
;
980 * Process ntoskrnl.exe
982 LdrSafePEProcessModule((PVOID
)0xC0000000, (PVOID
)0xC0000000, (PVOID
)DriverBase
, &DriverSize
);
984 FirstKrnlPhysAddr
= KeLoaderModules
[0].ModStart
- 0xc0000000 + 0x200000;
985 LastKrnlPhysAddr
= last_kernel_address
- 0xc0000000 + 0x200000;
986 LastKernelAddress
= last_kernel_address
;
989 /* FIXME: VMware does not like it when ReactOS is using the BIOS memory map */
990 KeLoaderBlock
.Flags
&= ~MB_FLAGS_MMAP_INFO
;
993 KeMemoryMapRangeCount
= 0;
994 if (KeLoaderBlock
.Flags
& MB_FLAGS_MMAP_INFO
)
996 /* We have a memory map from the nice BIOS */
997 size
= *((PULONG
)(KeLoaderBlock
.MmapAddr
- sizeof(ULONG
)));
999 while (i
< KeLoaderBlock
.MmapLength
)
1001 memcpy (&KeMemoryMap
[KeMemoryMapRangeCount
],
1002 (PVOID
)(KeLoaderBlock
.MmapAddr
+ i
),
1003 sizeof(ADDRESS_RANGE
));
1004 KeMemoryMapRangeCount
++;