2 * PROJECT: ReactOS Kernel
3 * LICENSE: GPL - See COPYING in the top level directory
4 * FILE: ntoskrnl/ex/init.c
5 * PURPOSE: Executive Initialization Code
6 * PROGRAMMERS: Alex Ionescu (alex.ionescu@reactos.org)
10 /* INCLUDES ******************************************************************/
13 #include <reactos/buildno.h>
17 /* This is the size that we can expect from the win 2003 loader */
18 #define LOADER_PARAMETER_EXTENSION_MIN_SIZE \
19 RTL_SIZEOF_THROUGH_FIELD(LOADER_PARAMETER_EXTENSION, AcpiTableSize)
26 IN PLOADER_PARAMETER_BLOCK LoaderBlock
29 typedef struct _INIT_BUFFER
31 WCHAR DebugBuffer
[256];
32 CHAR VersionBuffer
[256];
33 CHAR BootlogHeader
[256];
34 CHAR VersionNumber
[24];
35 RTL_USER_PROCESS_INFORMATION ProcessInfo
;
36 WCHAR RegistryBuffer
[256];
37 } INIT_BUFFER
, *PINIT_BUFFER
;
39 /* DATA **********************************************************************/
42 ULONG NtMajorVersion
= VER_PRODUCTMAJORVERSION
;
43 ULONG NtMinorVersion
= VER_PRODUCTMINORVERSION
;
44 #if DBG /* Checked Build */
45 ULONG NtBuildNumber
= VER_PRODUCTBUILD
| 0xC0000000;
46 #else /* Free Build */
47 ULONG NtBuildNumber
= VER_PRODUCTBUILD
;
51 ULONG NtGlobalFlag
= 0;
55 ULONG CmNtSpBuildNumber
;
57 ULONG CmNtCSDReleaseType
;
58 UNICODE_STRING CmVersionString
;
59 UNICODE_STRING CmCSDVersionString
;
61 CHAR NtBuildLab
[] = KERNEL_VERSION_BUILD_STR
"."
62 REACTOS_COMPILER_NAME
"_" REACTOS_COMPILER_VERSION
;
64 /* Init flags and settings */
65 ULONG ExpInitializationPhase
;
66 BOOLEAN ExpInTextModeSetup
;
67 BOOLEAN IoRemoteBootClient
;
68 ULONG InitSafeBootMode
;
69 BOOLEAN InitIsWinPEMode
, InitWinPEModeType
;
72 UNICODE_STRING NtSystemRoot
;
74 /* NT Initial User Application */
75 WCHAR NtInitialUserProcessBuffer
[128] = L
"\\SystemRoot\\System32\\smss.exe";
76 ULONG NtInitialUserProcessBufferLength
= sizeof(NtInitialUserProcessBuffer
) -
78 ULONG NtInitialUserProcessBufferType
= REG_SZ
;
80 /* Boot NLS information */
81 PVOID ExpNlsTableBase
;
82 ULONG ExpAnsiCodePageDataOffset
, ExpOemCodePageDataOffset
;
83 ULONG ExpUnicodeCaseTableDataOffset
;
84 NLSTABLEINFO ExpNlsTableInfo
;
85 SIZE_T ExpNlsTableSize
;
86 PVOID ExpNlsSectionPointer
;
88 /* CMOS Timer Sanity */
89 BOOLEAN ExCmosClockIsSane
= TRUE
;
90 BOOLEAN ExpRealTimeIsUniversal
;
92 /* FUNCTIONS ****************************************************************/
97 ExpCreateSystemRootLink(IN PLOADER_PARAMETER_BLOCK LoaderBlock
)
99 UNICODE_STRING LinkName
;
100 OBJECT_ATTRIBUTES ObjectAttributes
;
103 ANSI_STRING AnsiName
;
105 ANSI_STRING TargetString
;
106 UNICODE_STRING TargetName
;
108 /* Initialize the ArcName tree */
109 RtlInitUnicodeString(&LinkName
, L
"\\ArcName");
110 InitializeObjectAttributes(&ObjectAttributes
,
112 OBJ_CASE_INSENSITIVE
| OBJ_PERMANENT
,
114 SePublicDefaultUnrestrictedSd
);
117 Status
= NtCreateDirectoryObject(&LinkHandle
,
118 DIRECTORY_ALL_ACCESS
,
120 if (!NT_SUCCESS(Status
))
123 KeBugCheckEx(SYMBOLIC_INITIALIZATION_FAILED
, Status
, 1, 0, 0);
126 /* Close the LinkHandle */
129 /* Initialize the Device tree */
130 RtlInitUnicodeString(&LinkName
, L
"\\Device");
131 InitializeObjectAttributes(&ObjectAttributes
,
133 OBJ_CASE_INSENSITIVE
| OBJ_PERMANENT
,
135 SePublicDefaultUnrestrictedSd
);
138 Status
= NtCreateDirectoryObject(&LinkHandle
,
139 DIRECTORY_ALL_ACCESS
,
141 if (!NT_SUCCESS(Status
))
144 KeBugCheckEx(SYMBOLIC_INITIALIZATION_FAILED
, Status
, 2, 0, 0);
147 /* Close the LinkHandle */
148 ObCloseHandle(LinkHandle
, KernelMode
);
150 /* Create the system root symlink name */
151 RtlInitAnsiString(&AnsiName
, "\\SystemRoot");
152 Status
= RtlAnsiStringToUnicodeString(&LinkName
, &AnsiName
, TRUE
);
153 if (!NT_SUCCESS(Status
))
156 KeBugCheckEx(SYMBOLIC_INITIALIZATION_FAILED
, Status
, 3, 0, 0);
159 /* Initialize the attributes for the link */
160 InitializeObjectAttributes(&ObjectAttributes
,
162 OBJ_CASE_INSENSITIVE
| OBJ_PERMANENT
,
164 SePublicDefaultUnrestrictedSd
);
166 /* Build the ARC name */
169 LoaderBlock
->ArcBootDeviceName
,
170 LoaderBlock
->NtBootPathName
);
171 Buffer
[strlen(Buffer
) - 1] = ANSI_NULL
;
173 /* Convert it to Unicode */
174 RtlInitString(&TargetString
, Buffer
);
175 Status
= RtlAnsiStringToUnicodeString(&TargetName
,
178 if (!NT_SUCCESS(Status
))
180 /* We failed, bugcheck */
181 KeBugCheckEx(SYMBOLIC_INITIALIZATION_FAILED
, Status
, 4, 0, 0);
185 Status
= NtCreateSymbolicLinkObject(&LinkHandle
,
186 SYMBOLIC_LINK_ALL_ACCESS
,
190 /* Free the strings */
191 RtlFreeUnicodeString(&LinkName
);
192 RtlFreeUnicodeString(&TargetName
);
194 /* Check if creating the link failed */
195 if (!NT_SUCCESS(Status
))
198 KeBugCheckEx(SYMBOLIC_INITIALIZATION_FAILED
, Status
, 5, 0, 0);
201 /* Close the handle and return success */
202 ObCloseHandle(LinkHandle
, KernelMode
);
203 return STATUS_SUCCESS
;
209 ExpInitNls(IN PLOADER_PARAMETER_BLOCK LoaderBlock
)
211 LARGE_INTEGER SectionSize
;
214 PVOID SectionBase
= NULL
;
216 LARGE_INTEGER SectionOffset
= {{0, 0}};
217 PLIST_ENTRY ListHead
, NextEntry
;
218 PMEMORY_ALLOCATION_DESCRIPTOR MdBlock
;
219 ULONG NlsTablesEncountered
= 0;
220 SIZE_T NlsTableSizes
[3] = {0, 0, 0}; /* 3 NLS tables */
222 /* Check if this is boot-time phase 0 initialization */
223 if (!ExpInitializationPhase
)
225 /* Loop the memory descriptors */
226 ListHead
= &LoaderBlock
->MemoryDescriptorListHead
;
227 NextEntry
= ListHead
->Flink
;
228 while (NextEntry
!= ListHead
)
230 /* Get the current block */
231 MdBlock
= CONTAINING_RECORD(NextEntry
,
232 MEMORY_ALLOCATION_DESCRIPTOR
,
235 /* Check if this is an NLS block */
236 if (MdBlock
->MemoryType
== LoaderNlsData
)
238 /* Increase the table size */
239 ExpNlsTableSize
+= MdBlock
->PageCount
* PAGE_SIZE
;
241 /* FreeLdr-specific */
242 NlsTableSizes
[NlsTablesEncountered
] = MdBlock
->PageCount
* PAGE_SIZE
;
243 NlsTablesEncountered
++;
244 ASSERT(NlsTablesEncountered
< 4);
247 /* Go to the next block */
248 NextEntry
= MdBlock
->ListEntry
.Flink
;
251 /* Allocate the a new buffer since loader memory will be freed */
252 ExpNlsTableBase
= ExAllocatePoolWithTag(NonPagedPool
,
255 if (!ExpNlsTableBase
) KeBugCheck(PHASE0_INITIALIZATION_FAILED
);
257 /* Copy the codepage data in its new location. */
258 if (NlsTablesEncountered
== 1)
260 /* Ntldr-way boot process */
261 RtlCopyMemory(ExpNlsTableBase
,
262 LoaderBlock
->NlsData
->AnsiCodePageData
,
268 * In NT, the memory blocks are contiguous, but in ReactOS they aren't,
269 * so unless someone fixes FreeLdr, we'll have to use this icky hack.
271 RtlCopyMemory(ExpNlsTableBase
,
272 LoaderBlock
->NlsData
->AnsiCodePageData
,
275 RtlCopyMemory((PVOID
)((ULONG_PTR
)ExpNlsTableBase
+ NlsTableSizes
[0]),
276 LoaderBlock
->NlsData
->OemCodePageData
,
279 RtlCopyMemory((PVOID
)((ULONG_PTR
)ExpNlsTableBase
+ NlsTableSizes
[0] +
281 LoaderBlock
->NlsData
->UnicodeCodePageData
,
286 /* Initialize and reset the NLS TAbles */
287 RtlInitNlsTables((PVOID
)((ULONG_PTR
)ExpNlsTableBase
+
288 ExpAnsiCodePageDataOffset
),
289 (PVOID
)((ULONG_PTR
)ExpNlsTableBase
+
290 ExpOemCodePageDataOffset
),
291 (PVOID
)((ULONG_PTR
)ExpNlsTableBase
+
292 ExpUnicodeCaseTableDataOffset
),
294 RtlResetRtlTranslations(&ExpNlsTableInfo
);
298 /* Set the section size */
299 SectionSize
.QuadPart
= ExpNlsTableSize
;
301 /* Create the NLS Section */
302 Status
= ZwCreateSection(&NlsSection
,
309 if (!NT_SUCCESS(Status
))
312 KeBugCheckEx(PHASE1_INITIALIZATION_FAILED
, Status
, 1, 0, 0);
315 /* Get a pointer to the section */
316 Status
= ObReferenceObjectByHandle(NlsSection
,
320 &ExpNlsSectionPointer
,
322 ObCloseHandle(NlsSection
, KernelMode
);
323 if (!NT_SUCCESS(Status
))
326 KeBugCheckEx(PHASE1_INITIALIZATION_FAILED
, Status
, 2, 0, 0);
329 /* Map the NLS Section in system space */
330 Status
= MmMapViewInSystemSpace(ExpNlsSectionPointer
,
333 if (!NT_SUCCESS(Status
))
336 KeBugCheckEx(PHASE1_INITIALIZATION_FAILED
, Status
, 3, 0, 0);
339 /* Copy the codepage data in its new location. */
340 ASSERT(SectionBase
>= MmSystemRangeStart
);
341 RtlCopyMemory(SectionBase
, ExpNlsTableBase
, ExpNlsTableSize
);
343 /* Free the previously allocated buffer and set the new location */
344 ExFreePoolWithTag(ExpNlsTableBase
, TAG_RTLI
);
345 ExpNlsTableBase
= SectionBase
;
347 /* Initialize the NLS Tables */
348 RtlInitNlsTables((PVOID
)((ULONG_PTR
)ExpNlsTableBase
+
349 ExpAnsiCodePageDataOffset
),
350 (PVOID
)((ULONG_PTR
)ExpNlsTableBase
+
351 ExpOemCodePageDataOffset
),
352 (PVOID
)((ULONG_PTR
)ExpNlsTableBase
+
353 ExpUnicodeCaseTableDataOffset
),
355 RtlResetRtlTranslations(&ExpNlsTableInfo
);
357 /* Reset the base to 0 */
360 /* Map the section in the system process */
361 Status
= MmMapViewOfSection(ExpNlsSectionPointer
,
362 PsGetCurrentProcess(),
371 if (!NT_SUCCESS(Status
))
374 KeBugCheckEx(PHASE1_INITIALIZATION_FAILED
, Status
, 5, 0, 0);
377 /* Copy the table into the system process and set this as the base */
378 RtlCopyMemory(SectionBase
, ExpNlsTableBase
, ExpNlsTableSize
);
379 ExpNlsTableBase
= SectionBase
;
385 ExpLoadInitialProcess(IN PINIT_BUFFER InitBuffer
,
386 OUT PRTL_USER_PROCESS_PARAMETERS
*ProcessParameters
,
387 OUT PCHAR
*ProcessEnvironment
)
392 UNICODE_STRING NullString
= RTL_CONSTANT_STRING(L
"");
393 UNICODE_STRING SmssName
, Environment
, SystemDriveString
, DebugString
;
394 PVOID EnvironmentPtr
= NULL
;
395 PRTL_USER_PROCESS_INFORMATION ProcessInformation
;
396 PRTL_USER_PROCESS_PARAMETERS ProcessParams
= NULL
;
398 NullString
.Length
= sizeof(WCHAR
);
400 /* Use the initial buffer, after the strings */
401 ProcessInformation
= &InitBuffer
->ProcessInfo
;
403 /* Allocate memory for the process parameters */
404 Size
= sizeof(*ProcessParams
) + ((MAX_PATH
* 6) * sizeof(WCHAR
));
405 Status
= ZwAllocateVirtualMemory(NtCurrentProcess(),
406 (PVOID
*)&ProcessParams
,
409 MEM_RESERVE
| MEM_COMMIT
,
411 if (!NT_SUCCESS(Status
))
413 /* Failed, display error */
414 _snwprintf(InitBuffer
->DebugBuffer
,
415 sizeof(InitBuffer
->DebugBuffer
)/sizeof(WCHAR
),
416 L
"INIT: Unable to allocate Process Parameters. 0x%lx",
418 RtlInitUnicodeString(&DebugString
, InitBuffer
->DebugBuffer
);
419 ZwDisplayString(&DebugString
);
421 /* Bugcheck the system */
422 KeBugCheckEx(SESSION1_INITIALIZATION_FAILED
, Status
, 0, 0, 0);
425 /* Setup the basic header, and give the process the low 1MB to itself */
426 ProcessParams
->Length
= (ULONG
)Size
;
427 ProcessParams
->MaximumLength
= (ULONG
)Size
;
428 ProcessParams
->Flags
= RTL_USER_PROCESS_PARAMETERS_NORMALIZED
|
429 RTL_USER_PROCESS_PARAMETERS_RESERVE_1MB
;
431 /* Allocate a page for the environment */
433 Status
= ZwAllocateVirtualMemory(NtCurrentProcess(),
437 MEM_RESERVE
| MEM_COMMIT
,
439 if (!NT_SUCCESS(Status
))
441 /* Failed, display error */
442 _snwprintf(InitBuffer
->DebugBuffer
,
443 sizeof(InitBuffer
->DebugBuffer
)/sizeof(WCHAR
),
444 L
"INIT: Unable to allocate Process Environment. 0x%lx",
446 RtlInitUnicodeString(&DebugString
, InitBuffer
->DebugBuffer
);
447 ZwDisplayString(&DebugString
);
449 /* Bugcheck the system */
450 KeBugCheckEx(SESSION2_INITIALIZATION_FAILED
, Status
, 0, 0, 0);
453 /* Write the pointer */
454 ProcessParams
->Environment
= EnvironmentPtr
;
456 /* Make a buffer for the DOS path */
457 p
= (PWSTR
)(ProcessParams
+ 1);
458 ProcessParams
->CurrentDirectory
.DosPath
.Buffer
= p
;
459 ProcessParams
->CurrentDirectory
.DosPath
.MaximumLength
= MAX_PATH
*
462 /* Copy the DOS path */
463 RtlCopyUnicodeString(&ProcessParams
->CurrentDirectory
.DosPath
,
466 /* Make a buffer for the DLL Path */
467 p
= (PWSTR
)((PCHAR
)ProcessParams
->CurrentDirectory
.DosPath
.Buffer
+
468 ProcessParams
->CurrentDirectory
.DosPath
.MaximumLength
);
469 ProcessParams
->DllPath
.Buffer
= p
;
470 ProcessParams
->DllPath
.MaximumLength
= MAX_PATH
* sizeof(WCHAR
);
472 /* Copy the DLL path and append the system32 directory */
473 RtlCopyUnicodeString(&ProcessParams
->DllPath
,
474 &ProcessParams
->CurrentDirectory
.DosPath
);
475 RtlAppendUnicodeToString(&ProcessParams
->DllPath
, L
"\\System32");
477 /* Make a buffer for the image name */
478 p
= (PWSTR
)((PCHAR
)ProcessParams
->DllPath
.Buffer
+
479 ProcessParams
->DllPath
.MaximumLength
);
480 ProcessParams
->ImagePathName
.Buffer
= p
;
481 ProcessParams
->ImagePathName
.MaximumLength
= MAX_PATH
* sizeof(WCHAR
);
483 /* Make sure the buffer is a valid string which within the given length */
484 if ((NtInitialUserProcessBufferType
!= REG_SZ
) ||
485 ((NtInitialUserProcessBufferLength
!= MAXULONG
) &&
486 ((NtInitialUserProcessBufferLength
< sizeof(WCHAR
)) ||
487 (NtInitialUserProcessBufferLength
>
488 sizeof(NtInitialUserProcessBuffer
) - sizeof(WCHAR
)))))
490 /* Invalid initial process string, bugcheck */
491 KeBugCheckEx(SESSION2_INITIALIZATION_FAILED
,
492 STATUS_INVALID_PARAMETER
,
493 NtInitialUserProcessBufferType
,
494 NtInitialUserProcessBufferLength
,
495 sizeof(NtInitialUserProcessBuffer
));
498 /* Cut out anything after a space */
499 p
= NtInitialUserProcessBuffer
;
500 while ((*p
) && (*p
!= L
' ')) p
++;
502 /* Set the image path length */
503 ProcessParams
->ImagePathName
.Length
=
504 (USHORT
)((PCHAR
)p
- (PCHAR
)NtInitialUserProcessBuffer
);
506 /* Copy the actual buffer */
507 RtlCopyMemory(ProcessParams
->ImagePathName
.Buffer
,
508 NtInitialUserProcessBuffer
,
509 ProcessParams
->ImagePathName
.Length
);
511 /* Null-terminate it */
512 ProcessParams
->ImagePathName
.Buffer
[ProcessParams
->ImagePathName
.Length
/
513 sizeof(WCHAR
)] = UNICODE_NULL
;
515 /* Make a buffer for the command line */
516 p
= (PWSTR
)((PCHAR
)ProcessParams
->ImagePathName
.Buffer
+
517 ProcessParams
->ImagePathName
.MaximumLength
);
518 ProcessParams
->CommandLine
.Buffer
= p
;
519 ProcessParams
->CommandLine
.MaximumLength
= MAX_PATH
* sizeof(WCHAR
);
521 /* Add the image name to the command line */
522 RtlAppendUnicodeToString(&ProcessParams
->CommandLine
,
523 NtInitialUserProcessBuffer
);
525 /* Create the environment string */
526 RtlInitEmptyUnicodeString(&Environment
,
527 ProcessParams
->Environment
,
530 /* Append the DLL path to it */
531 RtlAppendUnicodeToString(&Environment
, L
"Path=");
532 RtlAppendUnicodeStringToString(&Environment
, &ProcessParams
->DllPath
);
533 RtlAppendUnicodeStringToString(&Environment
, &NullString
);
535 /* Create the system drive string */
536 SystemDriveString
= NtSystemRoot
;
537 SystemDriveString
.Length
= 2 * sizeof(WCHAR
);
539 /* Append it to the environment */
540 RtlAppendUnicodeToString(&Environment
, L
"SystemDrive=");
541 RtlAppendUnicodeStringToString(&Environment
, &SystemDriveString
);
542 RtlAppendUnicodeStringToString(&Environment
, &NullString
);
544 /* Append the system root to the environment */
545 RtlAppendUnicodeToString(&Environment
, L
"SystemRoot=");
546 RtlAppendUnicodeStringToString(&Environment
, &NtSystemRoot
);
547 RtlAppendUnicodeStringToString(&Environment
, &NullString
);
549 /* Prepare the prefetcher */
550 //CcPfBeginBootPhase(150);
552 /* Create SMSS process */
553 SmssName
= ProcessParams
->ImagePathName
;
554 Status
= RtlCreateUserProcess(&SmssName
,
555 OBJ_CASE_INSENSITIVE
,
556 RtlDeNormalizeProcessParams(ProcessParams
),
564 if (!NT_SUCCESS(Status
))
566 /* Failed, display error */
567 _snwprintf(InitBuffer
->DebugBuffer
,
568 sizeof(InitBuffer
->DebugBuffer
)/sizeof(WCHAR
),
569 L
"INIT: Unable to create Session Manager. 0x%lx",
571 RtlInitUnicodeString(&DebugString
, InitBuffer
->DebugBuffer
);
572 ZwDisplayString(&DebugString
);
574 /* Bugcheck the system */
575 KeBugCheckEx(SESSION3_INITIALIZATION_FAILED
, Status
, 0, 0, 0);
578 /* Resume the thread */
579 Status
= ZwResumeThread(ProcessInformation
->ThreadHandle
, NULL
);
580 if (!NT_SUCCESS(Status
))
582 /* Failed, display error */
583 _snwprintf(InitBuffer
->DebugBuffer
,
584 sizeof(InitBuffer
->DebugBuffer
)/sizeof(WCHAR
),
585 L
"INIT: Unable to resume Session Manager. 0x%lx",
587 RtlInitUnicodeString(&DebugString
, InitBuffer
->DebugBuffer
);
588 ZwDisplayString(&DebugString
);
590 /* Bugcheck the system */
591 KeBugCheckEx(SESSION4_INITIALIZATION_FAILED
, Status
, 0, 0, 0);
595 *ProcessParameters
= ProcessParams
;
596 *ProcessEnvironment
= EnvironmentPtr
;
602 ExComputeTickCountMultiplier(IN ULONG ClockIncrement
)
604 ULONG MsRemainder
= 0, MsIncrement
;
605 ULONG IncrementRemainder
;
608 /* Count the number of milliseconds for each clock interrupt */
609 MsIncrement
= ClockIncrement
/ (10 * 1000);
611 /* Count the remainder from the division above, with 24-bit precision */
612 IncrementRemainder
= ClockIncrement
- (MsIncrement
* (10 * 1000));
613 for (i
= 0; i
< 24; i
++)
615 /* Shift the remainders */
617 IncrementRemainder
<<= 1;
619 /* Check if we've went past 1 ms */
620 if (IncrementRemainder
>= (10 * 1000))
622 /* Increase the remainder by one, and substract from increment */
623 IncrementRemainder
-= (10 * 1000);
628 /* Return the increment */
629 return (MsIncrement
<< 24) | MsRemainder
;
635 ExpInitSystemPhase0(VOID
)
637 /* Initialize EXRESOURCE Support */
638 ExpResourceInitialization();
640 /* Initialize the environment lock */
641 ExInitializeFastMutex(&ExpEnvironmentLock
);
643 /* Initialize the lookaside lists and locks */
644 ExpInitLookasideLists();
646 /* Initialize the Firmware Table resource and listhead */
647 InitializeListHead(&ExpFirmwareTableProviderListHead
);
648 ExInitializeResourceLite(&ExpFirmwareTableResource
);
650 /* Set the suite mask to maximum and return */
651 ExSuiteMask
= 0xFFFFFFFF;
658 ExpInitSystemPhase1(VOID
)
660 /* Initialize worker threads */
661 ExpInitializeWorkerThreads();
663 /* Initialize pushlocks */
664 ExpInitializePushLocks();
666 /* Initialize events and event pairs */
667 if (ExpInitializeEventImplementation() == FALSE
)
669 DPRINT1("Executive: Event initialization failed\n");
672 if (ExpInitializeEventPairImplementation() == FALSE
)
674 DPRINT1("Executive: Event Pair initialization failed\n");
678 /* Initialize mutants */
679 if (ExpInitializeMutantImplementation() == FALSE
)
681 DPRINT1("Executive: Mutant initialization failed\n");
685 /* Initialize callbacks */
686 if (ExpInitializeCallbacks() == FALSE
)
688 DPRINT1("Executive: Callback initialization failed\n");
692 /* Initialize semaphores */
693 if (ExpInitializeSemaphoreImplementation() == FALSE
)
695 DPRINT1("Executive: Semaphore initialization failed\n");
699 /* Initialize timers */
700 if (ExpInitializeTimerImplementation() == FALSE
)
702 DPRINT1("Executive: Timer initialization failed\n");
706 /* Initialize profiling */
707 if (ExpInitializeProfileImplementation() == FALSE
)
709 DPRINT1("Executive: Profile initialization failed\n");
713 /* Initialize UUIDs */
716 /* Initialize keyed events */
717 if (ExpInitializeKeyedEventImplementation() == FALSE
)
719 DPRINT1("Executive: Keyed event initialization failed\n");
723 /* Initialize Win32K */
724 if (ExpWin32kInit() == FALSE
)
726 DPRINT1("Executive: Win32 initialization failed\n");
737 /* Check the initialization phase */
738 switch (ExpInitializationPhase
)
743 return ExpInitSystemPhase0();
748 return ExpInitSystemPhase1();
752 /* Don't know any other phase! Bugcheck! */
753 KeBugCheck(UNEXPECTED_INITIALIZATION_CALL
);
761 ExpIsLoaderValid(IN PLOADER_PARAMETER_BLOCK LoaderBlock
)
763 PLOADER_PARAMETER_EXTENSION Extension
;
765 /* Get the loader extension */
766 Extension
= LoaderBlock
->Extension
;
768 /* Validate the size (Windows 2003 loader doesn't provide more) */
769 if (Extension
->Size
< LOADER_PARAMETER_EXTENSION_MIN_SIZE
) return FALSE
;
771 /* Don't validate upper versions */
772 if (Extension
->MajorVersion
> VER_PRODUCTMAJORVERSION
) return TRUE
;
774 /* Fail if this is NT 4 */
775 if (Extension
->MajorVersion
< VER_PRODUCTMAJORVERSION
) return FALSE
;
777 /* Fail if this is XP */
778 if (Extension
->MinorVersion
< VER_PRODUCTMINORVERSION
) return FALSE
;
780 /* This is 2003 or newer, approve it */
787 ExpLoadBootSymbols(IN PLOADER_PARAMETER_BLOCK LoaderBlock
)
790 PLIST_ENTRY NextEntry
;
793 PLDR_DATA_TABLE_ENTRY LdrEntry
;
794 CHAR NameBuffer
[256];
798 /* Loop the driver list */
799 NextEntry
= LoaderBlock
->LoadOrderListHead
.Flink
;
800 while (NextEntry
!= &LoaderBlock
->LoadOrderListHead
)
802 /* Skip the first two images */
806 LdrEntry
= CONTAINING_RECORD(NextEntry
,
807 LDR_DATA_TABLE_ENTRY
,
809 if (LdrEntry
->FullDllName
.Buffer
[0] == L
'\\')
811 /* We have a name, read its data */
812 Name
= LdrEntry
->FullDllName
.Buffer
;
813 Length
= LdrEntry
->FullDllName
.Length
/ sizeof(WCHAR
);
815 /* Check if our buffer can hold it */
816 if (sizeof(NameBuffer
) < Length
+ sizeof(ANSI_NULL
))
819 Status
= STATUS_BUFFER_OVERFLOW
;
827 /* Copy the character */
828 NameBuffer
[Count
++] = (CHAR
)*Name
++;
829 } while (Count
< Length
);
832 NameBuffer
[Count
] = ANSI_NULL
;
833 Status
= STATUS_SUCCESS
;
838 /* Safely print the string into our buffer */
839 Status
= RtlStringCbPrintfA(NameBuffer
,
841 "%S\\System32\\Drivers\\%wZ",
842 &SharedUserData
->NtSystemRoot
[2],
843 &LdrEntry
->BaseDllName
);
846 /* Check if the buffer was ok */
847 if (NT_SUCCESS(Status
))
849 /* Initialize the STRING for the debugger */
850 RtlInitString(&SymbolString
, NameBuffer
);
852 /* Load the symbols */
853 DbgLoadImageSymbols(&SymbolString
,
855 (ULONG_PTR
)PsGetCurrentProcessId());
859 /* Go to the next entry */
861 NextEntry
= NextEntry
->Flink
;
868 ExBurnMemory(IN PLOADER_PARAMETER_BLOCK LoaderBlock
,
869 IN ULONG_PTR PagesToDestroy
,
870 IN TYPE_OF_MEMORY MemoryType
)
872 PLIST_ENTRY ListEntry
;
873 PMEMORY_ALLOCATION_DESCRIPTOR MemDescriptor
;
875 DPRINT1("Burn RAM amount: %lu pages\n", PagesToDestroy
);
877 /* Loop the memory descriptors, beginning at the end */
878 for (ListEntry
= LoaderBlock
->MemoryDescriptorListHead
.Blink
;
879 ListEntry
!= &LoaderBlock
->MemoryDescriptorListHead
;
880 ListEntry
= ListEntry
->Blink
)
882 /* Get the memory descriptor structure */
883 MemDescriptor
= CONTAINING_RECORD(ListEntry
,
884 MEMORY_ALLOCATION_DESCRIPTOR
,
887 /* Is memory free there or is it temporary? */
888 if (MemDescriptor
->MemoryType
== LoaderFree
||
889 MemDescriptor
->MemoryType
== LoaderFirmwareTemporary
)
891 /* Check if the descriptor has more pages than we want */
892 if (MemDescriptor
->PageCount
> PagesToDestroy
)
894 /* Change block's page count, ntoskrnl doesn't care much */
895 MemDescriptor
->PageCount
-= PagesToDestroy
;
900 /* Change block type */
901 MemDescriptor
->MemoryType
= MemoryType
;
902 PagesToDestroy
-= MemDescriptor
->PageCount
;
904 /* Check if we are done */
905 if (PagesToDestroy
== 0) break;
914 ExpInitializeExecutive(IN ULONG Cpu
,
915 IN PLOADER_PARAMETER_BLOCK LoaderBlock
)
917 PNLS_DATA_BLOCK NlsData
;
919 ANSI_STRING AnsiPath
;
921 PCHAR CommandLine
, PerfMem
;
923 PLDR_DATA_TABLE_ENTRY NtosEntry
;
924 PMESSAGE_RESOURCE_ENTRY MsgEntry
;
925 ANSI_STRING CSDString
;
926 size_t Remaining
= 0;
928 CHAR VersionBuffer
[65];
930 /* Validate Loader */
931 if (!ExpIsLoaderValid(LoaderBlock
))
933 /* Invalid loader version */
934 KeBugCheckEx(MISMATCHED_HAL
,
936 LoaderBlock
->Extension
->Size
,
937 LoaderBlock
->Extension
->MajorVersion
,
938 LoaderBlock
->Extension
->MinorVersion
);
941 /* Initialize PRCB pool lookaside pointers */
942 ExInitPoolLookasidePointers();
944 /* Check if this is an application CPU */
947 /* Then simply initialize it with HAL */
948 if (!HalInitSystem(ExpInitializationPhase
, LoaderBlock
))
950 /* Initialization failed */
951 KeBugCheck(HAL_INITIALIZATION_FAILED
);
958 /* Assume no text-mode or remote boot */
959 ExpInTextModeSetup
= FALSE
;
960 IoRemoteBootClient
= FALSE
;
962 /* Check if we have a setup loader block */
963 if (LoaderBlock
->SetupLdrBlock
)
965 /* Check if this is text-mode setup */
966 if (LoaderBlock
->SetupLdrBlock
->Flags
& SETUPLDR_TEXT_MODE
)
967 ExpInTextModeSetup
= TRUE
;
969 /* Check if this is network boot */
970 if (LoaderBlock
->SetupLdrBlock
->Flags
& SETUPLDR_REMOTE_BOOT
)
973 IoRemoteBootClient
= TRUE
;
975 /* Make sure we're actually booting off the network */
976 ASSERT(!_memicmp(LoaderBlock
->ArcBootDeviceName
, "net(0)", 6));
981 ExpInitializationPhase
= 0;
983 /* Get boot command line */
984 CommandLine
= LoaderBlock
->LoadOptions
;
987 /* Upcase it for comparison and check if we're in performance mode */
988 _strupr(CommandLine
);
989 PerfMem
= strstr(CommandLine
, "PERFMEM");
992 /* Check if the user gave a number of bytes to use */
993 PerfMem
= strstr(PerfMem
, "=");
996 /* Read the number of pages we'll use */
997 PerfMemUsed
= atol(PerfMem
+ 1) * (1024 * 1024 / PAGE_SIZE
);
1001 DPRINT1("BBT performance mode not yet supported."
1002 "/PERFMEM option ignored.\n");
1007 /* Check if we're burning memory */
1008 PerfMem
= strstr(CommandLine
, "BURNMEMORY");
1011 /* Check if the user gave a number of bytes to use */
1012 PerfMem
= strstr(PerfMem
, "=");
1015 /* Read the number of pages we'll use */
1016 PerfMemUsed
= atol(PerfMem
+ 1) * (1024 * 1024 / PAGE_SIZE
);
1017 if (PerfMemUsed
) ExBurnMemory(LoaderBlock
, PerfMemUsed
, LoaderBad
);
1022 /* Setup NLS Base and offsets */
1023 NlsData
= LoaderBlock
->NlsData
;
1024 ExpNlsTableBase
= NlsData
->AnsiCodePageData
;
1025 ExpAnsiCodePageDataOffset
= 0;
1026 ExpOemCodePageDataOffset
= (ULONG
)((ULONG_PTR
)NlsData
->OemCodePageData
-
1027 (ULONG_PTR
)NlsData
->AnsiCodePageData
);
1028 ExpUnicodeCaseTableDataOffset
= (ULONG
)((ULONG_PTR
)NlsData
->UnicodeCodePageData
-
1029 (ULONG_PTR
)NlsData
->AnsiCodePageData
);
1031 /* Initialize the NLS Tables */
1032 RtlInitNlsTables((PVOID
)((ULONG_PTR
)ExpNlsTableBase
+
1033 ExpAnsiCodePageDataOffset
),
1034 (PVOID
)((ULONG_PTR
)ExpNlsTableBase
+
1035 ExpOemCodePageDataOffset
),
1036 (PVOID
)((ULONG_PTR
)ExpNlsTableBase
+
1037 ExpUnicodeCaseTableDataOffset
),
1039 RtlResetRtlTranslations(&ExpNlsTableInfo
);
1041 /* Now initialize the HAL */
1042 if (!HalInitSystem(ExpInitializationPhase
, LoaderBlock
))
1044 /* HAL failed to initialize, bugcheck */
1045 KeBugCheck(HAL_INITIALIZATION_FAILED
);
1048 /* Make sure interrupts are active now */
1051 /* Clear the crypto exponent */
1052 SharedUserData
->CryptoExponent
= 0;
1054 /* Set global flags for the checked build */
1056 NtGlobalFlag
|= FLG_ENABLE_CLOSE_EXCEPTIONS
|
1057 FLG_ENABLE_KDEBUG_SYMBOL_LOAD
;
1060 /* Setup NT System Root Path */
1061 sprintf(Buffer
, "C:%s", LoaderBlock
->NtBootPathName
);
1063 /* Convert to ANSI_STRING and null-terminate it */
1064 RtlInitString(&AnsiPath
, Buffer
);
1065 Buffer
[--AnsiPath
.Length
] = ANSI_NULL
;
1067 /* Get the string from KUSER_SHARED_DATA's buffer */
1068 RtlInitEmptyUnicodeString(&NtSystemRoot
,
1069 SharedUserData
->NtSystemRoot
,
1070 sizeof(SharedUserData
->NtSystemRoot
));
1072 /* Now fill it in */
1073 Status
= RtlAnsiStringToUnicodeString(&NtSystemRoot
, &AnsiPath
, FALSE
);
1074 if (!NT_SUCCESS(Status
)) KeBugCheck(SESSION3_INITIALIZATION_FAILED
);
1076 /* Setup bugcheck messages */
1077 KiInitializeBugCheck();
1079 /* Setup initial system settings */
1080 CmGetSystemControlValues(LoaderBlock
->RegistryBase
, CmControlVector
);
1082 /* Set the Service Pack Number and add it to the CSD Version number if needed */
1083 CmNtSpBuildNumber
= VER_PRODUCTBUILD_QFE
;
1084 if (((CmNtCSDVersion
& 0xFFFF0000) == 0) && (CmNtCSDReleaseType
== 1))
1086 CmNtCSDVersion
|= (VER_PRODUCTBUILD_QFE
<< 16);
1089 /* Add loaded CmNtGlobalFlag value */
1090 NtGlobalFlag
|= CmNtGlobalFlag
;
1092 /* Initialize the executive at phase 0 */
1093 if (!ExInitSystem()) KeBugCheck(PHASE0_INITIALIZATION_FAILED
);
1095 /* Initialize the memory manager at phase 0 */
1096 if (!MmArmInitSystem(0, LoaderBlock
)) KeBugCheck(PHASE0_INITIALIZATION_FAILED
);
1098 /* Load boot symbols */
1099 ExpLoadBootSymbols(LoaderBlock
);
1101 /* Check if we should break after symbol load */
1102 if (KdBreakAfterSymbolLoad
) DbgBreakPointWithStatus(DBG_STATUS_CONTROL_C
);
1104 /* Check if this loader is compatible with NT 5.2 */
1105 if (LoaderBlock
->Extension
->Size
>= sizeof(LOADER_PARAMETER_EXTENSION
))
1107 /* Setup headless terminal settings */
1108 HeadlessInit(LoaderBlock
);
1111 /* Set system ranges */
1113 SharedUserData
->Reserved1
= MM_HIGHEST_USER_ADDRESS_WOW64
;
1114 SharedUserData
->Reserved3
= MM_SYSTEM_RANGE_START_WOW64
;
1116 SharedUserData
->Reserved1
= (ULONG_PTR
)MmHighestUserAddress
;
1117 SharedUserData
->Reserved3
= (ULONG_PTR
)MmSystemRangeStart
;
1120 /* Make a copy of the NLS Tables */
1121 ExpInitNls(LoaderBlock
);
1123 /* Get the kernel's load entry */
1124 NtosEntry
= CONTAINING_RECORD(LoaderBlock
->LoadOrderListHead
.Flink
,
1125 LDR_DATA_TABLE_ENTRY
,
1128 /* Check if this is a service pack */
1129 if (CmNtCSDVersion
& 0xFFFF)
1131 /* Get the service pack string */
1132 Status
= RtlFindMessage(NtosEntry
->DllBase
,
1135 WINDOWS_NT_CSD_STRING
,
1137 if (NT_SUCCESS(Status
))
1139 /* Setup the string */
1140 RtlInitAnsiString(&CSDString
, (PCHAR
)MsgEntry
->Text
);
1142 /* Remove trailing newline */
1143 while ((CSDString
.Length
> 0) &&
1144 ((CSDString
.Buffer
[CSDString
.Length
- 1] == '\r') ||
1145 (CSDString
.Buffer
[CSDString
.Length
- 1] == '\n')))
1147 /* Skip the trailing character */
1151 /* Fill the buffer with version information */
1152 Status
= RtlStringCbPrintfA(Buffer
,
1156 (CmNtCSDVersion
& 0xFF00) >> 8,
1157 (CmNtCSDVersion
& 0xFF) ?
1158 'A' + (CmNtCSDVersion
& 0xFF) - 1 :
1163 /* Build default string */
1164 Status
= RtlStringCbPrintfA(Buffer
,
1170 /* Check for success */
1171 if (!NT_SUCCESS(Status
))
1174 KeBugCheckEx(PHASE0_INITIALIZATION_FAILED
, Status
, 0, 0, 0);
1179 /* Then this is a beta */
1180 Status
= RtlStringCbCopyExA(Buffer
,
1182 VER_PRODUCTBETA_STR
,
1186 if (!NT_SUCCESS(Status
))
1189 KeBugCheckEx(PHASE0_INITIALIZATION_FAILED
, Status
, 0, 0, 0);
1193 CmCSDVersionString
.MaximumLength
= sizeof(Buffer
) - (USHORT
)Remaining
;
1196 /* Check if we have an RC number */
1197 if ((CmNtCSDVersion
& 0xFFFF0000) && (CmNtCSDReleaseType
== 1))
1199 /* Check if we have no version data yet */
1203 Remaining
= sizeof(Buffer
);
1208 /* Add comma and space */
1209 Status
= RtlStringCbCatExA(Buffer
,
1215 if (!NT_SUCCESS(Status
))
1218 KeBugCheckEx(PHASE0_INITIALIZATION_FAILED
, Status
, 0, 0, 0);
1222 /* Add the version format string */
1223 Status
= RtlStringCbPrintfA(RcEnd
,
1226 (CmNtCSDVersion
& 0xFFFF0000) >> 16);
1227 if (!NT_SUCCESS(Status
))
1230 KeBugCheckEx(PHASE0_INITIALIZATION_FAILED
, Status
, 0, 0, 0);
1234 /* Now setup the final string */
1235 RtlInitAnsiString(&CSDString
, Buffer
);
1236 Status
= RtlAnsiStringToUnicodeString(&CmCSDVersionString
,
1239 if (!NT_SUCCESS(Status
))
1242 KeBugCheckEx(PHASE0_INITIALIZATION_FAILED
, Status
, 0, 0, 0);
1245 /* Add our version */
1246 Status
= RtlStringCbPrintfA(VersionBuffer
,
1247 sizeof(VersionBuffer
),
1249 VER_PRODUCTMAJORVERSION
,
1250 VER_PRODUCTMINORVERSION
);
1251 if (!NT_SUCCESS(Status
))
1254 KeBugCheckEx(PHASE0_INITIALIZATION_FAILED
, Status
, 0, 0, 0);
1257 /* Build the final version string */
1258 RtlCreateUnicodeStringFromAsciiz(&CmVersionString
, VersionBuffer
);
1260 /* Check if the user wants a kernel stack trace database */
1261 if (NtGlobalFlag
& FLG_KERNEL_STACK_TRACE_DB
)
1264 DPRINT1("Kernel-mode stack trace support not yet present."
1265 "FLG_KERNEL_STACK_TRACE_DB flag ignored.\n");
1268 /* Check if he wanted exception logging */
1269 if (NtGlobalFlag
& FLG_ENABLE_EXCEPTION_LOGGING
)
1272 DPRINT1("Kernel-mode exception logging support not yet present."
1273 "FLG_ENABLE_EXCEPTION_LOGGING flag ignored.\n");
1276 /* Initialize the Handle Table */
1277 ExpInitializeHandleTables();
1280 /* On checked builds, allocate the system call count table */
1281 KeServiceDescriptorTable
[0].Count
=
1282 ExAllocatePoolWithTag(NonPagedPool
,
1283 KiServiceLimit
* sizeof(ULONG
),
1286 /* Use it for the shadow table too */
1287 KeServiceDescriptorTableShadow
[0].Count
= KeServiceDescriptorTable
[0].Count
;
1289 /* Make sure allocation succeeded */
1290 if (KeServiceDescriptorTable
[0].Count
)
1292 /* Zero the call counts to 0 */
1293 RtlZeroMemory(KeServiceDescriptorTable
[0].Count
,
1294 KiServiceLimit
* sizeof(ULONG
));
1298 /* Create the Basic Object Manager Types to allow new Object Types */
1299 if (!ObInitSystem()) KeBugCheck(OBJECT_INITIALIZATION_FAILED
);
1301 /* Load basic Security for other Managers */
1302 if (!SeInitSystem()) KeBugCheck(SECURITY_INITIALIZATION_FAILED
);
1304 /* Initialize the Process Manager */
1305 if (!PsInitSystem(LoaderBlock
)) KeBugCheck(PROCESS_INITIALIZATION_FAILED
);
1307 /* Initialize the PnP Manager */
1308 if (!PpInitSystem()) KeBugCheck(PP0_INITIALIZATION_FAILED
);
1310 /* Initialize the User-Mode Debugging Subsystem */
1313 /* Calculate the tick count multiplier */
1314 ExpTickCountMultiplier
= ExComputeTickCountMultiplier(KeMaximumIncrement
);
1315 SharedUserData
->TickCountMultiplier
= ExpTickCountMultiplier
;
1317 /* Set the OS Version */
1318 SharedUserData
->NtMajorVersion
= NtMajorVersion
;
1319 SharedUserData
->NtMinorVersion
= NtMinorVersion
;
1321 /* Set the machine type */
1322 SharedUserData
->ImageNumberLow
= IMAGE_FILE_MACHINE_NATIVE
;
1323 SharedUserData
->ImageNumberHigh
= IMAGE_FILE_MACHINE_NATIVE
;
1328 MmFreeLoaderBlock(IN PLOADER_PARAMETER_BLOCK LoaderBlock
);
1333 Phase1InitializationDiscard(IN PVOID Context
)
1335 PLOADER_PARAMETER_BLOCK LoaderBlock
= Context
;
1336 NTSTATUS Status
, MsgStatus
;
1337 TIME_FIELDS TimeFields
;
1338 LARGE_INTEGER SystemBootTime
, UniversalBootTime
, OldTime
, Timeout
;
1339 BOOLEAN SosEnabled
, NoGuiBoot
, ResetBias
= FALSE
, AlternateShell
= FALSE
;
1340 PLDR_DATA_TABLE_ENTRY NtosEntry
;
1341 PMESSAGE_RESOURCE_ENTRY MsgEntry
;
1342 PCHAR CommandLine
, Y2KHackRequired
, SafeBoot
, Environment
;
1343 PCHAR StringBuffer
, EndBuffer
, BeginBuffer
, MpString
= "";
1344 PINIT_BUFFER InitBuffer
;
1345 ANSI_STRING TempString
;
1346 ULONG LastTzBias
, Length
, YearHack
= 0, Disposition
, MessageCode
= 0;
1349 PRTL_USER_PROCESS_INFORMATION ProcessInfo
;
1350 KEY_VALUE_PARTIAL_INFORMATION KeyPartialInfo
;
1351 UNICODE_STRING KeyName
;
1352 OBJECT_ATTRIBUTES ObjectAttributes
;
1353 HANDLE KeyHandle
, OptionHandle
;
1354 PRTL_USER_PROCESS_PARAMETERS ProcessParameters
= NULL
;
1356 /* Allocate the initialization buffer */
1357 InitBuffer
= ExAllocatePoolWithTag(NonPagedPool
,
1358 sizeof(INIT_BUFFER
),
1363 KeBugCheckEx(PHASE1_INITIALIZATION_FAILED
, STATUS_NO_MEMORY
, 8, 0, 0);
1366 /* Set to phase 1 */
1367 ExpInitializationPhase
= 1;
1369 /* Set us at maximum priority */
1370 KeSetPriorityThread(KeGetCurrentThread(), HIGH_PRIORITY
);
1372 /* Do Phase 1 HAL Initialization */
1373 if (!HalInitSystem(1, LoaderBlock
)) KeBugCheck(HAL1_INITIALIZATION_FAILED
);
1375 /* Get the command line and upcase it */
1376 CommandLine
= (LoaderBlock
->LoadOptions
? _strupr(LoaderBlock
->LoadOptions
) : NULL
);
1378 /* Check if GUI Boot is enabled */
1379 NoGuiBoot
= (CommandLine
&& strstr(CommandLine
, "NOGUIBOOT") != NULL
);
1381 /* Get the SOS setting */
1382 SosEnabled
= (CommandLine
&& strstr(CommandLine
, "SOS") != NULL
);
1384 /* Setup the boot driver */
1385 InbvEnableBootDriver(!NoGuiBoot
);
1386 InbvDriverInitialize(LoaderBlock
, IDB_MAX_RESOURCE
);
1388 /* Check if GUI boot is enabled */
1391 /* It is, display the boot logo and enable printing strings */
1392 InbvEnableDisplayString(SosEnabled
);
1393 DisplayBootBitmap(SosEnabled
);
1397 /* Release display ownership if not using GUI boot */
1398 InbvNotifyDisplayOwnershipLost(NULL
);
1400 /* Don't allow boot-time strings */
1401 InbvEnableDisplayString(FALSE
);
1404 /* Check if this is LiveCD (WinPE) mode */
1405 if (CommandLine
&& strstr(CommandLine
, "MININT") != NULL
)
1407 /* Setup WinPE Settings */
1408 InitIsWinPEMode
= TRUE
;
1409 InitWinPEModeType
|= (strstr(CommandLine
, "INRAM") != NULL
) ? 0x80000000 : 0x00000001;
1412 /* Get the kernel's load entry */
1413 NtosEntry
= CONTAINING_RECORD(LoaderBlock
->LoadOrderListHead
.Flink
,
1414 LDR_DATA_TABLE_ENTRY
,
1417 /* Find the banner message */
1418 MsgStatus
= RtlFindMessage(NtosEntry
->DllBase
,
1424 /* Setup defaults and check if we have a version string */
1425 StringBuffer
= InitBuffer
->VersionBuffer
;
1426 BeginBuffer
= StringBuffer
;
1427 EndBuffer
= StringBuffer
;
1428 Remaining
= sizeof(InitBuffer
->VersionBuffer
);
1429 if (CmCSDVersionString
.Length
)
1431 /* Print the version string */
1432 Status
= RtlStringCbPrintfExA(StringBuffer
,
1438 &CmCSDVersionString
);
1439 if (!NT_SUCCESS(Status
))
1442 KeBugCheckEx(PHASE1_INITIALIZATION_FAILED
, Status
, 7, 0, 0);
1448 *EndBuffer
= ANSI_NULL
; /* Null-terminate the string */
1451 /* Skip over the null-terminator to start a new string */
1455 /* Build the version number */
1456 StringBuffer
= InitBuffer
->VersionNumber
;
1457 Status
= RtlStringCbPrintfA(StringBuffer
,
1458 sizeof(InitBuffer
->VersionNumber
),
1460 VER_PRODUCTMAJORVERSION
,
1461 VER_PRODUCTMINORVERSION
);
1462 if (!NT_SUCCESS(Status
))
1465 KeBugCheckEx(PHASE1_INITIALIZATION_FAILED
, Status
, 7, 0, 0);
1468 /* Check if we had found a banner message */
1469 if (NT_SUCCESS(MsgStatus
))
1471 /* Create the banner message */
1472 /* ReactOS specific: Report ReactOS version, NtBuildLab information and reported NT kernel version */
1473 Status
= RtlStringCbPrintfA(EndBuffer
,
1475 (PCHAR
)MsgEntry
->Text
,
1479 NtBuildNumber
& 0xFFFF,
1481 if (!NT_SUCCESS(Status
))
1484 KeBugCheckEx(PHASE1_INITIALIZATION_FAILED
, Status
, 7, 0, 0);
1489 /* Use hard-coded banner message */
1490 Status
= RtlStringCbCopyA(EndBuffer
, Remaining
, "REACTOS (R)\r\n");
1491 if (!NT_SUCCESS(Status
))
1494 KeBugCheckEx(PHASE1_INITIALIZATION_FAILED
, Status
, 7, 0, 0);
1498 /* Display the version string on-screen */
1499 InbvDisplayString(EndBuffer
);
1501 /* Initialize Power Subsystem in Phase 0 */
1502 if (!PoInitSystem(0)) KeBugCheck(INTERNAL_POWER_ERROR
);
1504 /* Check for Y2K hack */
1505 Y2KHackRequired
= CommandLine
? strstr(CommandLine
, "YEAR") : NULL
;
1506 if (Y2KHackRequired
) Y2KHackRequired
= strstr(Y2KHackRequired
, "=");
1507 if (Y2KHackRequired
) YearHack
= atol(Y2KHackRequired
+ 1);
1509 /* Query the clock */
1510 if ((ExCmosClockIsSane
) && (HalQueryRealTimeClock(&TimeFields
)))
1512 /* Check if we're using the Y2K hack */
1513 if (Y2KHackRequired
) TimeFields
.Year
= (CSHORT
)YearHack
;
1515 /* Convert to time fields */
1516 RtlTimeFieldsToTime(&TimeFields
, &SystemBootTime
);
1517 UniversalBootTime
= SystemBootTime
;
1519 /* Check if real time is GMT */
1520 if (!ExpRealTimeIsUniversal
)
1522 /* Check if we don't have a valid bias */
1523 if (ExpLastTimeZoneBias
== MAXULONG
)
1527 ExpLastTimeZoneBias
= ExpAltTimeZoneBias
;
1530 /* Calculate the bias in seconds */
1531 ExpTimeZoneBias
.QuadPart
= Int32x32To64(ExpLastTimeZoneBias
* 60,
1534 /* Set the boot time-zone bias */
1535 SharedUserData
->TimeZoneBias
.High2Time
= ExpTimeZoneBias
.HighPart
;
1536 SharedUserData
->TimeZoneBias
.LowPart
= ExpTimeZoneBias
.LowPart
;
1537 SharedUserData
->TimeZoneBias
.High1Time
= ExpTimeZoneBias
.HighPart
;
1539 /* Convert the boot time to local time, and set it */
1540 UniversalBootTime
.QuadPart
= SystemBootTime
.QuadPart
+
1541 ExpTimeZoneBias
.QuadPart
;
1544 /* Update the system time */
1545 KeSetSystemTime(&UniversalBootTime
, &OldTime
, FALSE
, NULL
);
1547 /* Do system callback */
1548 PoNotifySystemTimeSet();
1550 /* Remember this as the boot time */
1551 KeBootTime
= UniversalBootTime
;
1555 /* Initialize all processors */
1556 if (!HalAllProcessorsStarted()) KeBugCheck(HAL1_INITIALIZATION_FAILED
);
1559 /* HACK: We should use RtlFindMessage and not only fallback to this */
1560 MpString
= "MultiProcessor Kernel\r\n";
1563 /* Setup the "MP" String */
1564 RtlInitAnsiString(&TempString
, MpString
);
1566 /* Make sure to remove the \r\n if we actually have a string */
1567 while ((TempString
.Length
> 0) &&
1568 ((TempString
.Buffer
[TempString
.Length
- 1] == '\r') ||
1569 (TempString
.Buffer
[TempString
.Length
- 1] == '\n')))
1571 /* Skip the trailing character */
1572 TempString
.Length
--;
1575 /* Get the information string from our resource file */
1576 MsgStatus
= RtlFindMessage(NtosEntry
->DllBase
,
1579 KeNumberProcessors
> 1 ?
1580 WINDOWS_NT_INFO_STRING_PLURAL
:
1581 WINDOWS_NT_INFO_STRING
,
1584 /* Get total RAM size, in MiB */
1585 /* Round size up. Assumed to better match actual physical RAM size */
1586 Size
= ALIGN_UP_BY(MmNumberOfPhysicalPages
* PAGE_SIZE
, 1024 * 1024) / (1024 * 1024);
1588 /* Create the string */
1589 StringBuffer
= InitBuffer
->VersionBuffer
;
1590 Status
= RtlStringCbPrintfA(StringBuffer
,
1591 sizeof(InitBuffer
->VersionBuffer
),
1592 NT_SUCCESS(MsgStatus
) ?
1593 (PCHAR
)MsgEntry
->Text
:
1594 "%u System Processor [%Iu MB Memory] %Z\r\n",
1598 if (!NT_SUCCESS(Status
))
1601 KeBugCheckEx(PHASE1_INITIALIZATION_FAILED
, Status
, 4, 0, 0);
1604 /* Display RAM and CPU count */
1605 InbvDisplayString(StringBuffer
);
1607 /* Update the progress bar */
1608 InbvUpdateProgressBar(5);
1610 /* Call OB initialization again */
1611 if (!ObInitSystem()) KeBugCheck(OBJECT1_INITIALIZATION_FAILED
);
1613 /* Initialize Basic System Objects and Worker Threads */
1614 if (!ExInitSystem()) KeBugCheckEx(PHASE1_INITIALIZATION_FAILED
, 0, 0, 1, 0);
1616 /* Initialize the later stages of the kernel */
1617 if (!KeInitSystem()) KeBugCheckEx(PHASE1_INITIALIZATION_FAILED
, 0, 0, 2, 0);
1619 /* Call KD Providers at Phase 1 */
1620 if (!KdInitSystem(ExpInitializationPhase
, KeLoaderBlock
))
1622 /* Failed, bugcheck */
1623 KeBugCheckEx(PHASE1_INITIALIZATION_FAILED
, 0, 0, 3, 0);
1626 /* Initialize the SRM in Phase 1 */
1627 if (!SeInitSystem()) KeBugCheck(SECURITY1_INITIALIZATION_FAILED
);
1629 /* Update the progress bar */
1630 InbvUpdateProgressBar(10);
1632 /* Create SystemRoot Link */
1633 Status
= ExpCreateSystemRootLink(LoaderBlock
);
1634 if (!NT_SUCCESS(Status
))
1636 /* Failed to create the system root link */
1637 KeBugCheckEx(SYMBOLIC_INITIALIZATION_FAILED
, Status
, 0, 0, 0);
1640 /* Set up Region Maps, Sections and the Paging File */
1641 if (!MmInitSystem(1, LoaderBlock
)) KeBugCheck(MEMORY1_INITIALIZATION_FAILED
);
1643 /* Create NLS section */
1644 ExpInitNls(LoaderBlock
);
1646 /* Initialize Cache Views */
1647 if (!CcInitializeCacheManager()) KeBugCheck(CACHE_INITIALIZATION_FAILED
);
1649 /* Initialize the Registry */
1650 if (!CmInitSystem1()) KeBugCheck(CONFIG_INITIALIZATION_FAILED
);
1652 /* Initialize Prefetcher */
1653 CcPfInitializePrefetcher();
1655 /* Update progress bar */
1656 InbvUpdateProgressBar(15);
1658 /* Update timezone information */
1659 LastTzBias
= ExpLastTimeZoneBias
;
1660 ExRefreshTimeZoneInformation(&SystemBootTime
);
1662 /* Check if we're resetting timezone data */
1665 /* Convert the local time to system time */
1666 ExLocalTimeToSystemTime(&SystemBootTime
, &UniversalBootTime
);
1667 KeBootTime
= UniversalBootTime
;
1670 /* Set the new time */
1671 KeSetSystemTime(&UniversalBootTime
, &OldTime
, FALSE
, NULL
);
1675 /* Check if the timezone switched and update the time */
1676 if (LastTzBias
!= ExpLastTimeZoneBias
) ZwSetSystemTime(NULL
, NULL
);
1679 /* Initialize the File System Runtime Library */
1680 if (!FsRtlInitSystem()) KeBugCheck(FILE_INITIALIZATION_FAILED
);
1682 /* Initialize range lists */
1683 RtlInitializeRangeListPackage();
1685 /* Report all resources used by HAL */
1686 HalReportResourceUsage();
1688 /* Call the debugger DLL */
1689 KdDebuggerInitialize1(LoaderBlock
);
1691 /* Setup PnP Manager in phase 1 */
1692 if (!PpInitSystem()) KeBugCheck(PP1_INITIALIZATION_FAILED
);
1694 /* Update progress bar */
1695 InbvUpdateProgressBar(20);
1697 /* Initialize LPC */
1698 if (!LpcInitSystem()) KeBugCheck(LPC_INITIALIZATION_FAILED
);
1700 /* Make sure we have a command line */
1703 /* Check if this is a safe mode boot */
1704 SafeBoot
= strstr(CommandLine
, "SAFEBOOT:");
1707 /* Check what kind of boot this is */
1709 if (!strncmp(SafeBoot
, "MINIMAL", 7))
1712 InitSafeBootMode
= 1;
1714 MessageCode
= BOOTING_IN_SAFEMODE_MINIMAL
;
1716 else if (!strncmp(SafeBoot
, "NETWORK", 7))
1718 /* With Networking */
1719 InitSafeBootMode
= 2;
1721 MessageCode
= BOOTING_IN_SAFEMODE_NETWORK
;
1723 else if (!strncmp(SafeBoot
, "DSREPAIR", 8))
1725 /* Domain Server Repair */
1726 InitSafeBootMode
= 3;
1728 MessageCode
= BOOTING_IN_SAFEMODE_DSREPAIR
;
1734 InitSafeBootMode
= 0;
1737 /* Check if there's any settings left */
1740 /* Check if an alternate shell was requested */
1741 if (!strncmp(SafeBoot
, "(ALTERNATESHELL)", 16))
1743 /* Remember this for later */
1744 AlternateShell
= TRUE
;
1748 /* Find the message to print out */
1749 Status
= RtlFindMessage(NtosEntry
->DllBase
,
1754 if (NT_SUCCESS(Status
))
1757 InbvDisplayString((PCHAR
)MsgEntry
->Text
);
1762 /* Make sure we have a command line */
1765 /* Check if bootlogging is enabled */
1766 if (strstr(CommandLine
, "BOOTLOG"))
1768 /* Find the message to print out */
1769 Status
= RtlFindMessage(NtosEntry
->DllBase
,
1774 if (NT_SUCCESS(Status
))
1777 InbvDisplayString((PCHAR
)MsgEntry
->Text
);
1780 /* Setup boot logging */
1781 //IopInitializeBootLogging(LoaderBlock, InitBuffer->BootlogHeader);
1785 /* Setup the Executive in Phase 2 */
1786 //ExInitSystemPhase2();
1788 /* Update progress bar */
1789 InbvUpdateProgressBar(25);
1792 /* No KD Time Slip is pending */
1793 KdpTimeSlipPending
= 0;
1796 /* Initialize in-place execution support */
1797 XIPInit(LoaderBlock
);
1799 /* Set maximum update to 75% */
1800 InbvSetProgressBarSubset(25, 75);
1802 /* Initialize the I/O Subsystem */
1803 if (!IoInitSystem(LoaderBlock
)) KeBugCheck(IO1_INITIALIZATION_FAILED
);
1805 /* Set maximum update to 100% */
1806 InbvSetProgressBarSubset(0, 100);
1808 /* Are we in safe mode? */
1809 if (InitSafeBootMode
)
1811 /* Open the safe boot key */
1812 RtlInitUnicodeString(&KeyName
,
1813 L
"\\REGISTRY\\MACHINE\\SYSTEM\\CURRENTCONTROLSET"
1814 L
"\\CONTROL\\SAFEBOOT");
1815 InitializeObjectAttributes(&ObjectAttributes
,
1817 OBJ_CASE_INSENSITIVE
,
1820 Status
= ZwOpenKey(&KeyHandle
, KEY_ALL_ACCESS
, &ObjectAttributes
);
1821 if (NT_SUCCESS(Status
))
1823 /* First check if we have an alternate shell */
1826 /* Make sure that the registry has one setup */
1827 RtlInitUnicodeString(&KeyName
, L
"AlternateShell");
1828 Status
= NtQueryValueKey(KeyHandle
,
1830 KeyValuePartialInformation
,
1832 sizeof(KeyPartialInfo
),
1834 if (!(NT_SUCCESS(Status
) || Status
== STATUS_BUFFER_OVERFLOW
))
1836 AlternateShell
= FALSE
;
1840 /* Create the option key */
1841 RtlInitUnicodeString(&KeyName
, L
"Option");
1842 InitializeObjectAttributes(&ObjectAttributes
,
1844 OBJ_CASE_INSENSITIVE
,
1847 Status
= ZwCreateKey(&OptionHandle
,
1852 REG_OPTION_VOLATILE
,
1856 /* Check if the key create worked */
1857 if (NT_SUCCESS(Status
))
1859 /* Write the safe boot type */
1860 RtlInitUnicodeString(&KeyName
, L
"OptionValue");
1861 NtSetValueKey(OptionHandle
,
1866 sizeof(InitSafeBootMode
));
1868 /* Check if we have to use an alternate shell */
1871 /* Remember this for later */
1873 RtlInitUnicodeString(&KeyName
, L
"UseAlternateShell");
1874 NtSetValueKey(OptionHandle
,
1879 sizeof(Disposition
));
1882 /* Close the options key handle */
1883 NtClose(OptionHandle
);
1888 /* Are we in Win PE mode? */
1889 if (InitIsWinPEMode
)
1891 /* Open the safe control key */
1892 RtlInitUnicodeString(&KeyName
,
1893 L
"\\REGISTRY\\MACHINE\\SYSTEM\\CURRENTCONTROLSET"
1895 InitializeObjectAttributes(&ObjectAttributes
,
1897 OBJ_CASE_INSENSITIVE
,
1900 Status
= ZwOpenKey(&KeyHandle
, KEY_ALL_ACCESS
, &ObjectAttributes
);
1901 if (!NT_SUCCESS(Status
))
1904 KeBugCheckEx(PHASE1_INITIALIZATION_FAILED
, Status
, 6, 0, 0);
1907 /* Create the MiniNT key */
1908 RtlInitUnicodeString(&KeyName
, L
"MiniNT");
1909 InitializeObjectAttributes(&ObjectAttributes
,
1911 OBJ_CASE_INSENSITIVE
,
1914 Status
= ZwCreateKey(&OptionHandle
,
1919 REG_OPTION_VOLATILE
,
1921 if (!NT_SUCCESS(Status
))
1924 KeBugCheckEx(PHASE1_INITIALIZATION_FAILED
, Status
, 6, 0, 0);
1927 /* Close the handles */
1929 NtClose(OptionHandle
);
1932 /* FIXME: This doesn't do anything for now */
1933 MmArmInitSystem(2, LoaderBlock
);
1935 /* Update progress bar */
1936 InbvUpdateProgressBar(80);
1938 /* Initialize VDM support */
1939 #if defined(_M_IX86)
1940 KeI386VdmInitialize();
1943 /* Initialize Power Subsystem in Phase 1*/
1944 if (!PoInitSystem(1)) KeBugCheck(INTERNAL_POWER_ERROR
);
1946 /* Update progress bar */
1947 InbvUpdateProgressBar(90);
1949 /* Initialize the Process Manager at Phase 1 */
1950 if (!PsInitSystem(LoaderBlock
)) KeBugCheck(PROCESS1_INITIALIZATION_FAILED
);
1952 /* Make sure nobody touches the loader block again */
1953 if (LoaderBlock
== KeLoaderBlock
) KeLoaderBlock
= NULL
;
1954 MmFreeLoaderBlock(LoaderBlock
);
1955 LoaderBlock
= Context
= NULL
;
1957 /* Initialize the SRM in phase 1 */
1958 if (!SeRmInitPhase1()) KeBugCheck(PROCESS1_INITIALIZATION_FAILED
);
1960 /* Update progress bar */
1961 InbvUpdateProgressBar(100);
1963 /* Clear the screen */
1964 if (InbvBootDriverInstalled
) FinalizeBootLogo();
1966 /* Allow strings to be displayed */
1967 InbvEnableDisplayString(TRUE
);
1969 /* Launch initial process */
1970 DPRINT("Free non-cache pages: %lx\n", MmAvailablePages
+ MiMemoryConsumers
[MC_CACHE
].PagesUsed
);
1971 ProcessInfo
= &InitBuffer
->ProcessInfo
;
1972 ExpLoadInitialProcess(InitBuffer
, &ProcessParameters
, &Environment
);
1974 /* Wait 5 seconds for initial process to initialize */
1975 Timeout
.QuadPart
= Int32x32To64(5, -10000000);
1976 Status
= ZwWaitForSingleObject(ProcessInfo
->ProcessHandle
, FALSE
, &Timeout
);
1977 if (Status
== STATUS_SUCCESS
)
1979 /* Failed, display error */
1980 DPRINT1("INIT: Session Manager terminated.\n");
1982 /* Bugcheck the system if SMSS couldn't initialize */
1983 KeBugCheck(SESSION5_INITIALIZATION_FAILED
);
1986 /* Close process handles */
1987 ZwClose(ProcessInfo
->ThreadHandle
);
1988 ZwClose(ProcessInfo
->ProcessHandle
);
1990 /* Free the initial process environment */
1992 ZwFreeVirtualMemory(NtCurrentProcess(),
1993 (PVOID
*)&Environment
,
1997 /* Free the initial process parameters */
1999 ZwFreeVirtualMemory(NtCurrentProcess(),
2000 (PVOID
*)&ProcessParameters
,
2004 /* Increase init phase */
2005 ExpInitializationPhase
++;
2007 /* Free the boot buffer */
2008 ExFreePoolWithTag(InitBuffer
, TAG_INIT
);
2009 DPRINT("Free non-cache pages: %lx\n", MmAvailablePages
+ MiMemoryConsumers
[MC_CACHE
].PagesUsed
);
2014 Phase1Initialization(IN PVOID Context
)
2016 /* Do the .INIT part of Phase 1 which we can free later */
2017 Phase1InitializationDiscard(Context
);
2019 /* Jump into zero page thread */