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>
22 IN PLOADER_PARAMETER_BLOCK LoaderBlock
25 typedef struct _INIT_BUFFER
27 WCHAR DebugBuffer
[256];
28 CHAR VersionBuffer
[256];
29 CHAR BootlogHeader
[256];
30 CHAR VersionNumber
[24];
31 RTL_USER_PROCESS_INFORMATION ProcessInfo
;
32 WCHAR RegistryBuffer
[256];
33 } INIT_BUFFER
, *PINIT_BUFFER
;
35 /* DATA **********************************************************************/
38 ULONG NtMajorVersion
= VER_PRODUCTMAJORVERSION
;
39 ULONG NtMinorVersion
= VER_PRODUCTMINORVERSION
;
41 ULONG NtBuildNumber
= VER_PRODUCTBUILD
| 0xC0000000;
43 ULONG NtBuildNumber
= VER_PRODUCTBUILD
;
47 ULONG NtGlobalFlag
= 0;
51 ULONG CmNtSpBuildNumber
;
53 ULONG CmNtCSDReleaseType
;
54 UNICODE_STRING CmVersionString
;
55 UNICODE_STRING CmCSDVersionString
;
56 CHAR NtBuildLab
[] = KERNEL_VERSION_BUILD_STR
;
58 /* Init flags and settings */
59 ULONG ExpInitializationPhase
;
60 BOOLEAN ExpInTextModeSetup
;
61 BOOLEAN IoRemoteBootClient
;
62 ULONG InitSafeBootMode
;
63 BOOLEAN InitIsWinPEMode
, InitWinPEModeType
;
66 UNICODE_STRING NtSystemRoot
;
68 /* NT Initial User Application */
69 WCHAR NtInitialUserProcessBuffer
[128] = L
"\\SystemRoot\\System32\\smss.exe";
70 ULONG NtInitialUserProcessBufferLength
= sizeof(NtInitialUserProcessBuffer
) -
72 ULONG NtInitialUserProcessBufferType
= REG_SZ
;
74 /* Boot NLS information */
75 PVOID ExpNlsTableBase
;
76 ULONG ExpAnsiCodePageDataOffset
, ExpOemCodePageDataOffset
;
77 ULONG ExpUnicodeCaseTableDataOffset
;
78 NLSTABLEINFO ExpNlsTableInfo
;
79 SIZE_T ExpNlsTableSize
;
80 PVOID ExpNlsSectionPointer
;
82 /* CMOS Timer Sanity */
83 BOOLEAN ExCmosClockIsSane
= TRUE
;
84 BOOLEAN ExpRealTimeIsUniversal
;
86 /* FUNCTIONS ****************************************************************/
91 ExpCreateSystemRootLink(IN PLOADER_PARAMETER_BLOCK LoaderBlock
)
93 UNICODE_STRING LinkName
;
94 OBJECT_ATTRIBUTES ObjectAttributes
;
99 ANSI_STRING TargetString
;
100 UNICODE_STRING TargetName
;
102 /* Initialize the ArcName tree */
103 RtlInitUnicodeString(&LinkName
, L
"\\ArcName");
104 InitializeObjectAttributes(&ObjectAttributes
,
106 OBJ_CASE_INSENSITIVE
| OBJ_PERMANENT
,
108 SePublicDefaultUnrestrictedSd
);
111 Status
= NtCreateDirectoryObject(&LinkHandle
,
112 DIRECTORY_ALL_ACCESS
,
114 if (!NT_SUCCESS(Status
))
117 KeBugCheckEx(SYMBOLIC_INITIALIZATION_FAILED
, Status
, 1, 0, 0);
120 /* Close the LinkHandle */
123 /* Initialize the Device tree */
124 RtlInitUnicodeString(&LinkName
, L
"\\Device");
125 InitializeObjectAttributes(&ObjectAttributes
,
127 OBJ_CASE_INSENSITIVE
| OBJ_PERMANENT
,
129 SePublicDefaultUnrestrictedSd
);
132 Status
= NtCreateDirectoryObject(&LinkHandle
,
133 DIRECTORY_ALL_ACCESS
,
135 if (!NT_SUCCESS(Status
))
138 KeBugCheckEx(SYMBOLIC_INITIALIZATION_FAILED
, Status
, 2, 0, 0);
141 /* Close the LinkHandle */
142 ObCloseHandle(LinkHandle
, KernelMode
);
144 /* Create the system root symlink name */
145 RtlInitAnsiString(&AnsiName
, "\\SystemRoot");
146 Status
= RtlAnsiStringToUnicodeString(&LinkName
, &AnsiName
, TRUE
);
147 if (!NT_SUCCESS(Status
))
150 KeBugCheckEx(SYMBOLIC_INITIALIZATION_FAILED
, Status
, 3, 0, 0);
153 /* Initialize the attributes for the link */
154 InitializeObjectAttributes(&ObjectAttributes
,
156 OBJ_CASE_INSENSITIVE
| OBJ_PERMANENT
,
158 SePublicDefaultUnrestrictedSd
);
160 /* Build the ARC name */
163 LoaderBlock
->ArcBootDeviceName
,
164 LoaderBlock
->NtBootPathName
);
165 Buffer
[strlen(Buffer
) - 1] = ANSI_NULL
;
167 /* Convert it to Unicode */
168 RtlInitString(&TargetString
, Buffer
);
169 Status
= RtlAnsiStringToUnicodeString(&TargetName
,
172 if (!NT_SUCCESS(Status
))
174 /* We failed, bugcheck */
175 KeBugCheckEx(SYMBOLIC_INITIALIZATION_FAILED
, Status
, 4, 0, 0);
179 Status
= NtCreateSymbolicLinkObject(&LinkHandle
,
180 SYMBOLIC_LINK_ALL_ACCESS
,
184 /* Free the strings */
185 RtlFreeUnicodeString(&LinkName
);
186 RtlFreeUnicodeString(&TargetName
);
188 /* Check if creating the link failed */
189 if (!NT_SUCCESS(Status
))
192 KeBugCheckEx(SYMBOLIC_INITIALIZATION_FAILED
, Status
, 5, 0, 0);
195 /* Close the handle and return success */
196 ObCloseHandle(LinkHandle
, KernelMode
);
197 return STATUS_SUCCESS
;
203 ExpInitNls(IN PLOADER_PARAMETER_BLOCK LoaderBlock
)
205 LARGE_INTEGER SectionSize
;
208 PVOID SectionBase
= NULL
;
210 LARGE_INTEGER SectionOffset
= {{0, 0}};
211 PLIST_ENTRY ListHead
, NextEntry
;
212 PMEMORY_ALLOCATION_DESCRIPTOR MdBlock
;
213 ULONG NlsTablesEncountered
= 0;
214 ULONG NlsTableSizes
[3]; /* 3 NLS tables */
216 /* Check if this is boot-time phase 0 initialization */
217 if (!ExpInitializationPhase
)
219 /* Loop the memory descriptors */
220 ListHead
= &LoaderBlock
->MemoryDescriptorListHead
;
221 NextEntry
= ListHead
->Flink
;
222 while (NextEntry
!= ListHead
)
224 /* Get the current block */
225 MdBlock
= CONTAINING_RECORD(NextEntry
,
226 MEMORY_ALLOCATION_DESCRIPTOR
,
229 /* Check if this is an NLS block */
230 if (MdBlock
->MemoryType
== LoaderNlsData
)
232 /* Increase the table size */
233 ExpNlsTableSize
+= MdBlock
->PageCount
* PAGE_SIZE
;
235 /* FreeLdr-specific */
236 NlsTableSizes
[NlsTablesEncountered
] = MdBlock
->PageCount
* PAGE_SIZE
;
237 NlsTablesEncountered
++;
238 ASSERT(NlsTablesEncountered
< 4);
241 /* Go to the next block */
242 NextEntry
= MdBlock
->ListEntry
.Flink
;
245 /* Allocate the a new buffer since loader memory will be freed */
246 ExpNlsTableBase
= ExAllocatePoolWithTag(NonPagedPool
,
249 if (!ExpNlsTableBase
) KeBugCheck(PHASE0_INITIALIZATION_FAILED
);
251 /* Copy the codepage data in its new location. */
252 if (NlsTablesEncountered
== 1)
254 /* Ntldr-way boot process */
255 RtlCopyMemory(ExpNlsTableBase
,
256 LoaderBlock
->NlsData
->AnsiCodePageData
,
262 * In NT, the memory blocks are contiguous, but in ReactOS they aren't,
263 * so unless someone fixes FreeLdr, we'll have to use this icky hack.
265 RtlCopyMemory(ExpNlsTableBase
,
266 LoaderBlock
->NlsData
->AnsiCodePageData
,
269 RtlCopyMemory((PVOID
)((ULONG_PTR
)ExpNlsTableBase
+ NlsTableSizes
[0]),
270 LoaderBlock
->NlsData
->OemCodePageData
,
273 RtlCopyMemory((PVOID
)((ULONG_PTR
)ExpNlsTableBase
+ NlsTableSizes
[0] +
275 LoaderBlock
->NlsData
->UnicodeCodePageData
,
280 /* Initialize and reset the NLS TAbles */
281 RtlInitNlsTables((PVOID
)((ULONG_PTR
)ExpNlsTableBase
+
282 ExpAnsiCodePageDataOffset
),
283 (PVOID
)((ULONG_PTR
)ExpNlsTableBase
+
284 ExpOemCodePageDataOffset
),
285 (PVOID
)((ULONG_PTR
)ExpNlsTableBase
+
286 ExpUnicodeCaseTableDataOffset
),
288 RtlResetRtlTranslations(&ExpNlsTableInfo
);
292 /* Set the section size */
293 SectionSize
.QuadPart
= ExpNlsTableSize
;
295 /* Create the NLS Section */
296 Status
= ZwCreateSection(&NlsSection
,
303 if (!NT_SUCCESS(Status
))
306 KeBugCheckEx(PHASE1_INITIALIZATION_FAILED
, Status
, 1, 0, 0);
309 /* Get a pointer to the section */
310 Status
= ObReferenceObjectByHandle(NlsSection
,
314 &ExpNlsSectionPointer
,
316 ObCloseHandle(NlsSection
, KernelMode
);
317 if (!NT_SUCCESS(Status
))
320 KeBugCheckEx(PHASE1_INITIALIZATION_FAILED
, Status
, 2, 0, 0);
323 /* Map the NLS Section in system space */
324 Status
= MmMapViewInSystemSpace(ExpNlsSectionPointer
,
327 if (!NT_SUCCESS(Status
))
330 KeBugCheckEx(PHASE1_INITIALIZATION_FAILED
, Status
, 3, 0, 0);
333 /* Copy the codepage data in its new location. */
334 ASSERT(SectionBase
> MmSystemRangeStart
);
335 RtlCopyMemory(SectionBase
, ExpNlsTableBase
, ExpNlsTableSize
);
337 /* Free the previously allocated buffer and set the new location */
338 ExFreePoolWithTag(ExpNlsTableBase
, TAG_RTLI
);
339 ExpNlsTableBase
= SectionBase
;
341 /* Initialize the NLS Tables */
342 RtlInitNlsTables((PVOID
)((ULONG_PTR
)ExpNlsTableBase
+
343 ExpAnsiCodePageDataOffset
),
344 (PVOID
)((ULONG_PTR
)ExpNlsTableBase
+
345 ExpOemCodePageDataOffset
),
346 (PVOID
)((ULONG_PTR
)ExpNlsTableBase
+
347 ExpUnicodeCaseTableDataOffset
),
349 RtlResetRtlTranslations(&ExpNlsTableInfo
);
351 /* Reset the base to 0 */
354 /* Map the section in the system process */
355 Status
= MmMapViewOfSection(ExpNlsSectionPointer
,
356 PsGetCurrentProcess(),
365 if (!NT_SUCCESS(Status
))
368 KeBugCheckEx(PHASE1_INITIALIZATION_FAILED
, Status
, 5, 0, 0);
371 /* Copy the table into the system process and set this as the base */
372 RtlCopyMemory(SectionBase
, ExpNlsTableBase
, ExpNlsTableSize
);
373 ExpNlsTableBase
= SectionBase
;
379 ExpLoadInitialProcess(IN PINIT_BUFFER InitBuffer
,
380 OUT PRTL_USER_PROCESS_PARAMETERS
*ProcessParameters
,
381 OUT PCHAR
*ProcessEnvironment
)
386 UNICODE_STRING NullString
= RTL_CONSTANT_STRING(L
"");
387 UNICODE_STRING SmssName
, Environment
, SystemDriveString
, DebugString
;
388 PVOID EnvironmentPtr
= NULL
;
389 PRTL_USER_PROCESS_INFORMATION ProcessInformation
;
390 PRTL_USER_PROCESS_PARAMETERS ProcessParams
= NULL
;
392 NullString
.Length
= sizeof(WCHAR
);
394 /* Use the initial buffer, after the strings */
395 ProcessInformation
= &InitBuffer
->ProcessInfo
;
397 /* Allocate memory for the process parameters */
398 Size
= sizeof(*ProcessParams
) + ((MAX_PATH
* 6) * sizeof(WCHAR
));
399 Status
= ZwAllocateVirtualMemory(NtCurrentProcess(),
400 (PVOID
*)&ProcessParams
,
405 if (!NT_SUCCESS(Status
))
407 /* Failed, display error */
408 p
= InitBuffer
->DebugBuffer
;
411 L
"INIT: Unable to allocate Process Parameters. 0x%lx",
413 RtlInitUnicodeString(&DebugString
, p
);
414 ZwDisplayString(&DebugString
);
416 /* Bugcheck the system */
417 KeBugCheckEx(SESSION1_INITIALIZATION_FAILED
, Status
, 0, 0, 0);
420 /* Setup the basic header, and give the process the low 1MB to itself */
421 ProcessParams
->Length
= Size
;
422 ProcessParams
->MaximumLength
= Size
;
423 ProcessParams
->Flags
= RTL_USER_PROCESS_PARAMETERS_NORMALIZED
|
424 RTL_USER_PROCESS_PARAMETERS_RESERVE_1MB
;
426 /* Allocate a page for the environment */
428 Status
= ZwAllocateVirtualMemory(NtCurrentProcess(),
434 if (!NT_SUCCESS(Status
))
436 /* Failed, display error */
437 p
= InitBuffer
->DebugBuffer
;
440 L
"INIT: Unable to allocate Process Environment. 0x%lx",
442 RtlInitUnicodeString(&DebugString
, p
);
443 ZwDisplayString(&DebugString
);
445 /* Bugcheck the system */
446 KeBugCheckEx(SESSION2_INITIALIZATION_FAILED
, Status
, 0, 0, 0);
449 /* Write the pointer */
450 ProcessParams
->Environment
= EnvironmentPtr
;
452 /* Make a buffer for the DOS path */
453 p
= (PWSTR
)(ProcessParams
+ 1);
454 ProcessParams
->CurrentDirectory
.DosPath
.Buffer
= p
;
455 ProcessParams
->CurrentDirectory
.DosPath
.MaximumLength
= MAX_PATH
*
458 /* Copy the DOS path */
459 RtlCopyUnicodeString(&ProcessParams
->CurrentDirectory
.DosPath
,
462 /* Make a buffer for the DLL Path */
463 p
= (PWSTR
)((PCHAR
)ProcessParams
->CurrentDirectory
.DosPath
.Buffer
+
464 ProcessParams
->CurrentDirectory
.DosPath
.MaximumLength
);
465 ProcessParams
->DllPath
.Buffer
= p
;
466 ProcessParams
->DllPath
.MaximumLength
= MAX_PATH
* sizeof(WCHAR
);
468 /* Copy the DLL path and append the system32 directory */
469 RtlCopyUnicodeString(&ProcessParams
->DllPath
,
470 &ProcessParams
->CurrentDirectory
.DosPath
);
471 RtlAppendUnicodeToString(&ProcessParams
->DllPath
, L
"\\System32");
473 /* Make a buffer for the image name */
474 p
= (PWSTR
)((PCHAR
)ProcessParams
->DllPath
.Buffer
+
475 ProcessParams
->DllPath
.MaximumLength
);
476 ProcessParams
->ImagePathName
.Buffer
= p
;
477 ProcessParams
->ImagePathName
.MaximumLength
= MAX_PATH
* sizeof(WCHAR
);
479 /* Make sure the buffer is a valid string which within the given length */
480 if ((NtInitialUserProcessBufferType
!= REG_SZ
) ||
481 ((NtInitialUserProcessBufferLength
!= MAXULONG
) &&
482 ((NtInitialUserProcessBufferLength
< sizeof(WCHAR
)) ||
483 (NtInitialUserProcessBufferLength
>
484 sizeof(NtInitialUserProcessBuffer
) - sizeof(WCHAR
)))))
486 /* Invalid initial process string, bugcheck */
487 KeBugCheckEx(SESSION2_INITIALIZATION_FAILED
,
488 STATUS_INVALID_PARAMETER
,
489 NtInitialUserProcessBufferType
,
490 NtInitialUserProcessBufferLength
,
491 sizeof(NtInitialUserProcessBuffer
));
494 /* Cut out anything after a space */
495 p
= NtInitialUserProcessBuffer
;
496 while ((*p
) && (*p
!= L
' ')) p
++;
498 /* Set the image path length */
499 ProcessParams
->ImagePathName
.Length
=
500 (USHORT
)((PCHAR
)p
- (PCHAR
)NtInitialUserProcessBuffer
);
502 /* Copy the actual buffer */
503 RtlCopyMemory(ProcessParams
->ImagePathName
.Buffer
,
504 NtInitialUserProcessBuffer
,
505 ProcessParams
->ImagePathName
.Length
);
507 /* Null-terminate it */
508 ProcessParams
->ImagePathName
.Buffer
[ProcessParams
->ImagePathName
.Length
/
509 sizeof(WCHAR
)] = UNICODE_NULL
;
511 /* Make a buffer for the command line */
512 p
= (PWSTR
)((PCHAR
)ProcessParams
->ImagePathName
.Buffer
+
513 ProcessParams
->ImagePathName
.MaximumLength
);
514 ProcessParams
->CommandLine
.Buffer
= p
;
515 ProcessParams
->CommandLine
.MaximumLength
= MAX_PATH
* sizeof(WCHAR
);
517 /* Add the image name to the command line */
518 RtlAppendUnicodeToString(&ProcessParams
->CommandLine
,
519 NtInitialUserProcessBuffer
);
521 /* Create the environment string */
522 RtlInitEmptyUnicodeString(&Environment
,
523 ProcessParams
->Environment
,
526 /* Append the DLL path to it */
527 RtlAppendUnicodeToString(&Environment
, L
"Path=" );
528 RtlAppendUnicodeStringToString(&Environment
, &ProcessParams
->DllPath
);
529 RtlAppendUnicodeStringToString(&Environment
, &NullString
);
531 /* Create the system drive string */
532 SystemDriveString
= NtSystemRoot
;
533 SystemDriveString
.Length
= 2 * sizeof(WCHAR
);
535 /* Append it to the environment */
536 RtlAppendUnicodeToString(&Environment
, L
"SystemDrive=");
537 RtlAppendUnicodeStringToString(&Environment
, &SystemDriveString
);
538 RtlAppendUnicodeStringToString(&Environment
, &NullString
);
540 /* Append the system root to the environment */
541 RtlAppendUnicodeToString(&Environment
, L
"SystemRoot=");
542 RtlAppendUnicodeStringToString(&Environment
, &NtSystemRoot
);
543 RtlAppendUnicodeStringToString(&Environment
, &NullString
);
545 /* Prepare the prefetcher */
546 //CcPfBeginBootPhase(150);
548 /* Create SMSS process */
549 SmssName
= ProcessParams
->ImagePathName
;
550 Status
= RtlCreateUserProcess(&SmssName
,
551 OBJ_CASE_INSENSITIVE
,
552 RtlDeNormalizeProcessParams(ProcessParams
),
560 if (!NT_SUCCESS(Status
))
562 /* Failed, display error */
563 p
= InitBuffer
->DebugBuffer
;
566 L
"INIT: Unable to create Session Manager. 0x%lx",
568 RtlInitUnicodeString(&DebugString
, p
);
569 ZwDisplayString(&DebugString
);
571 /* Bugcheck the system */
572 KeBugCheckEx(SESSION3_INITIALIZATION_FAILED
, Status
, 0, 0, 0);
575 /* Resume the thread */
576 Status
= ZwResumeThread(ProcessInformation
->ThreadHandle
, NULL
);
577 if (!NT_SUCCESS(Status
))
579 /* Failed, display error */
580 p
= InitBuffer
->DebugBuffer
;
583 L
"INIT: Unable to resume Session Manager. 0x%lx",
585 RtlInitUnicodeString(&DebugString
, p
);
586 ZwDisplayString(&DebugString
);
588 /* Bugcheck the system */
589 KeBugCheckEx(SESSION4_INITIALIZATION_FAILED
, Status
, 0, 0, 0);
593 *ProcessParameters
= ProcessParams
;
594 *ProcessEnvironment
= EnvironmentPtr
;
600 ExComputeTickCountMultiplier(IN ULONG ClockIncrement
)
602 ULONG MsRemainder
= 0, MsIncrement
;
603 ULONG IncrementRemainder
;
606 /* Count the number of milliseconds for each clock interrupt */
607 MsIncrement
= ClockIncrement
/ (10 * 1000);
609 /* Count the remainder from the division above, with 24-bit precision */
610 IncrementRemainder
= ClockIncrement
- (MsIncrement
* (10 * 1000));
611 for (i
= 0; i
< 24; i
++)
613 /* Shift the remainders */
615 IncrementRemainder
<<= 1;
617 /* Check if we've went past 1 ms */
618 if (IncrementRemainder
>= (10 * 1000))
620 /* Increase the remainder by one, and substract from increment */
621 IncrementRemainder
-= (10 * 1000);
626 /* Return the increment */
627 return (MsIncrement
<< 24) | MsRemainder
;
633 ExpInitSystemPhase0(VOID
)
635 /* Initialize EXRESOURCE Support */
636 ExpResourceInitialization();
638 /* Initialize the environment lock */
639 ExInitializeFastMutex(&ExpEnvironmentLock
);
641 /* Initialize the lookaside lists and locks */
642 ExpInitLookasideLists();
644 /* Initialize the Firmware Table resource and listhead */
645 InitializeListHead(&ExpFirmwareTableProviderListHead
);
646 ExInitializeResourceLite(&ExpFirmwareTableResource
);
648 /* Set the suite mask to maximum and return */
649 ExSuiteMask
= 0xFFFFFFFF;
656 ExpInitSystemPhase1(VOID
)
658 /* Initialize worker threads */
659 ExpInitializeWorkerThreads();
661 /* Initialize pushlocks */
662 ExpInitializePushLocks();
664 /* Initialize events and event pairs */
665 ExpInitializeEventImplementation();
666 ExpInitializeEventPairImplementation();
668 /* Initialize callbacks */
669 ExpInitializeCallbacks();
671 /* Initialize mutants */
672 ExpInitializeMutantImplementation();
674 /* Initialize semaphores */
675 ExpInitializeSemaphoreImplementation();
677 /* Initialize timers */
678 ExpInitializeTimerImplementation();
680 /* Initialize profiling */
681 ExpInitializeProfileImplementation();
683 /* Initialize UUIDs */
686 /* Initialize Win32K */
696 /* Check the initialization phase */
697 switch (ExpInitializationPhase
)
702 return ExpInitSystemPhase0();
707 return ExpInitSystemPhase1();
711 /* Don't know any other phase! Bugcheck! */
712 KeBugCheck(UNEXPECTED_INITIALIZATION_CALL
);
720 ExpIsLoaderValid(IN PLOADER_PARAMETER_BLOCK LoaderBlock
)
722 PLOADER_PARAMETER_EXTENSION Extension
;
724 /* Get the loader extension */
725 Extension
= LoaderBlock
->Extension
;
727 /* Validate the size (larger structures are OK, we'll just ignore them) */
728 if (Extension
->Size
< sizeof(LOADER_PARAMETER_EXTENSION
)) return FALSE
;
730 /* Don't validate upper versions */
731 if (Extension
->MajorVersion
> VER_PRODUCTMAJORVERSION
) return TRUE
;
733 /* Fail if this is NT 4 */
734 if (Extension
->MajorVersion
< VER_PRODUCTMAJORVERSION
) return FALSE
;
736 /* Fail if this is XP */
737 if (Extension
->MinorVersion
< VER_PRODUCTMINORVERSION
) return FALSE
;
739 /* This is 2003 or newer, approve it */
746 ExpLoadBootSymbols(IN PLOADER_PARAMETER_BLOCK LoaderBlock
)
749 PLIST_ENTRY NextEntry
;
752 PLDR_DATA_TABLE_ENTRY LdrEntry
;
753 BOOLEAN OverFlow
= FALSE
;
754 CHAR NameBuffer
[256];
757 /* Loop the driver list */
758 NextEntry
= LoaderBlock
->LoadOrderListHead
.Flink
;
759 while (NextEntry
!= &LoaderBlock
->LoadOrderListHead
)
761 /* Skip the first two images */
765 LdrEntry
= CONTAINING_RECORD(NextEntry
,
766 LDR_DATA_TABLE_ENTRY
,
768 if (LdrEntry
->FullDllName
.Buffer
[0] == L
'\\')
770 /* We have a name, read its data */
771 Name
= LdrEntry
->FullDllName
.Buffer
;
772 Length
= LdrEntry
->FullDllName
.Length
/ sizeof(WCHAR
);
774 /* Check if our buffer can hold it */
775 if (sizeof(NameBuffer
) < Length
+ sizeof(ANSI_NULL
))
786 /* Copy the character */
787 NameBuffer
[Count
++] = (CHAR
)*Name
++;
788 } while (Count
< Length
);
791 NameBuffer
[Count
] = ANSI_NULL
;
796 /* This should be a driver, check if it fits */
797 if (sizeof(NameBuffer
) <
798 (sizeof("\\System32\\Drivers\\") +
799 NtSystemRoot
.Length
/ sizeof(WCHAR
) - sizeof(UNICODE_NULL
) +
800 LdrEntry
->BaseDllName
.Length
/ sizeof(WCHAR
) +
803 /* Buffer too small */
809 /* Otherwise build the name. HACKED for GCC :( */
811 "%S\\System32\\Drivers\\%S",
812 &SharedUserData
->NtSystemRoot
[2],
813 LdrEntry
->BaseDllName
.Buffer
);
817 /* Check if the buffer was ok */
820 /* Initialize the STRING for the debugger */
821 RtlInitString(&SymbolString
, NameBuffer
);
823 /* Load the symbols */
824 DbgLoadImageSymbols(&SymbolString
,
826 (ULONG_PTR
)ZwCurrentProcess());
830 /* Go to the next entry */
832 NextEntry
= NextEntry
->Flink
;
839 ExBurnMemory(IN PLOADER_PARAMETER_BLOCK LoaderBlock
,
840 IN ULONG PagesToDestroy
,
841 IN TYPE_OF_MEMORY MemoryType
)
843 PLIST_ENTRY ListEntry
;
844 PMEMORY_ALLOCATION_DESCRIPTOR MemDescriptor
;
846 DPRINT1("Burn RAM amount: %d pages\n", PagesToDestroy
);
848 /* Loop the memory descriptors, beginning at the end */
849 for (ListEntry
= LoaderBlock
->MemoryDescriptorListHead
.Blink
;
850 ListEntry
!= &LoaderBlock
->MemoryDescriptorListHead
;
851 ListEntry
= ListEntry
->Blink
)
853 /* Get the memory descriptor structure */
854 MemDescriptor
= CONTAINING_RECORD(ListEntry
,
855 MEMORY_ALLOCATION_DESCRIPTOR
,
858 /* Is memory free there or is it temporary? */
859 if (MemDescriptor
->MemoryType
== LoaderFree
||
860 MemDescriptor
->MemoryType
== LoaderFirmwareTemporary
)
862 /* Check if the descriptor has more pages than we want */
863 if (MemDescriptor
->PageCount
> PagesToDestroy
)
865 /* Change block's page count, ntoskrnl doesn't care much */
866 MemDescriptor
->PageCount
-= PagesToDestroy
;
871 /* Change block type */
872 MemDescriptor
->MemoryType
= MemoryType
;
873 PagesToDestroy
-= MemDescriptor
->PageCount
;
875 /* Check if we are done */
876 if (PagesToDestroy
== 0) break;
885 ExpInitializeExecutive(IN ULONG Cpu
,
886 IN PLOADER_PARAMETER_BLOCK LoaderBlock
)
888 PNLS_DATA_BLOCK NlsData
;
890 ANSI_STRING AnsiPath
;
892 PCHAR CommandLine
, PerfMem
;
894 PLDR_DATA_TABLE_ENTRY NtosEntry
;
895 PMESSAGE_RESOURCE_ENTRY MsgEntry
;
896 ANSI_STRING CsdString
;
897 size_t Remaining
= 0;
899 CHAR VersionBuffer
[65];
901 /* Validate Loader */
902 if (!ExpIsLoaderValid(LoaderBlock
))
904 /* Invalid loader version */
905 KeBugCheckEx(MISMATCHED_HAL
,
907 LoaderBlock
->Extension
->Size
,
908 LoaderBlock
->Extension
->MajorVersion
,
909 LoaderBlock
->Extension
->MinorVersion
);
912 /* Initialize PRCB pool lookaside pointers */
913 ExInitPoolLookasidePointers();
915 /* Check if this is an application CPU */
918 /* Then simply initialize it with HAL */
919 if (!HalInitSystem(ExpInitializationPhase
, LoaderBlock
))
921 /* Initialization failed */
922 KeBugCheck(HAL_INITIALIZATION_FAILED
);
929 /* Assume no text-mode or remote boot */
930 ExpInTextModeSetup
= FALSE
;
931 IoRemoteBootClient
= FALSE
;
933 /* Check if we have a setup loader block */
934 if (LoaderBlock
->SetupLdrBlock
)
936 /* Check if this is text-mode setup */
937 if (LoaderBlock
->SetupLdrBlock
->Flags
& SETUPLDR_TEXT_MODE
) ExpInTextModeSetup
= TRUE
;
939 /* Check if this is network boot */
940 if (LoaderBlock
->SetupLdrBlock
->Flags
& SETUPLDR_REMOTE_BOOT
)
943 IoRemoteBootClient
= TRUE
;
945 /* Make sure we're actually booting off the network */
946 ASSERT(!_memicmp(LoaderBlock
->ArcBootDeviceName
, "net(0)", 6));
951 ExpInitializationPhase
= 0;
953 /* Get boot command line */
954 CommandLine
= LoaderBlock
->LoadOptions
;
957 /* Upcase it for comparison and check if we're in performance mode */
958 _strupr(CommandLine
);
959 PerfMem
= strstr(CommandLine
, "PERFMEM");
962 /* Check if the user gave a number of bytes to use */
963 PerfMem
= strstr(PerfMem
, "=");
966 /* Read the number of pages we'll use */
967 PerfMemUsed
= atol(PerfMem
+ 1) * (1024 * 1024 / PAGE_SIZE
);
971 DPRINT1("BBT performance mode not yet supported."
972 "/PERFMEM option ignored.\n");
977 /* Check if we're burning memory */
978 PerfMem
= strstr(CommandLine
, "BURNMEMORY");
981 /* Check if the user gave a number of bytes to use */
982 PerfMem
= strstr(PerfMem
, "=");
985 /* Read the number of pages we'll use */
986 PerfMemUsed
= atol(PerfMem
+ 1) * (1024 * 1024 / PAGE_SIZE
);
987 if (PerfMemUsed
) ExBurnMemory(LoaderBlock
, PerfMemUsed
, LoaderBad
);
992 /* Setup NLS Base and offsets */
993 NlsData
= LoaderBlock
->NlsData
;
994 ExpNlsTableBase
= NlsData
->AnsiCodePageData
;
995 ExpAnsiCodePageDataOffset
= 0;
996 ExpOemCodePageDataOffset
= ((ULONG_PTR
)NlsData
->OemCodePageData
-
997 (ULONG_PTR
)NlsData
->AnsiCodePageData
);
998 ExpUnicodeCaseTableDataOffset
= ((ULONG_PTR
)NlsData
->UnicodeCodePageData
-
999 (ULONG_PTR
)NlsData
->AnsiCodePageData
);
1001 /* Initialize the NLS Tables */
1002 RtlInitNlsTables((PVOID
)((ULONG_PTR
)ExpNlsTableBase
+
1003 ExpAnsiCodePageDataOffset
),
1004 (PVOID
)((ULONG_PTR
)ExpNlsTableBase
+
1005 ExpOemCodePageDataOffset
),
1006 (PVOID
)((ULONG_PTR
)ExpNlsTableBase
+
1007 ExpUnicodeCaseTableDataOffset
),
1009 RtlResetRtlTranslations(&ExpNlsTableInfo
);
1011 /* Now initialize the HAL */
1012 if (!HalInitSystem(ExpInitializationPhase
, LoaderBlock
))
1014 /* HAL failed to initialize, bugcheck */
1015 KeBugCheck(HAL_INITIALIZATION_FAILED
);
1018 /* Make sure interrupts are active now */
1021 /* Clear the crypto exponent */
1022 SharedUserData
->CryptoExponent
= 0;
1024 /* Set global flags for the checked build */
1026 NtGlobalFlag
|= FLG_ENABLE_CLOSE_EXCEPTIONS
|
1027 FLG_ENABLE_KDEBUG_SYMBOL_LOAD
;
1030 /* Setup NT System Root Path */
1031 sprintf(Buffer
, "C:%s", LoaderBlock
->NtBootPathName
);
1033 /* Convert to ANSI_STRING and null-terminate it */
1034 RtlInitString(&AnsiPath
, Buffer
);
1035 Buffer
[--AnsiPath
.Length
] = ANSI_NULL
;
1037 /* Get the string from KUSER_SHARED_DATA's buffer */
1038 RtlInitEmptyUnicodeString(&NtSystemRoot
,
1039 SharedUserData
->NtSystemRoot
,
1040 sizeof(SharedUserData
->NtSystemRoot
));
1042 /* Now fill it in */
1043 Status
= RtlAnsiStringToUnicodeString(&NtSystemRoot
, &AnsiPath
, FALSE
);
1044 if (!NT_SUCCESS(Status
)) KeBugCheck(SESSION3_INITIALIZATION_FAILED
);
1046 /* Setup bugcheck messages */
1047 KiInitializeBugCheck();
1049 /* Setup initial system settings */
1050 CmGetSystemControlValues(LoaderBlock
->RegistryBase
, CmControlVector
);
1052 /* Load static defaults for Service Pack 1 and add our SVN revision */
1053 CmNtCSDVersion
= 0x100 | (KERNEL_VERSION_BUILD_HEX
<< 16);
1054 CmNtCSDReleaseType
= 0;
1056 /* Set Service Pack data for Service Pack 1 */
1057 CmNtSpBuildNumber
= 1830;
1058 if (!(CmNtCSDVersion
& 0xFFFF0000))
1060 /* Check the release type */
1061 if (CmNtCSDReleaseType
== 1) CmNtSpBuildNumber
|= 1830 << 16;
1064 /* Add loaded CmNtGlobalFlag value */
1065 NtGlobalFlag
|= CmNtGlobalFlag
;
1067 /* Initialize the executive at phase 0 */
1068 if (!ExInitSystem()) KeBugCheck(PHASE0_INITIALIZATION_FAILED
);
1070 /* Initialize the memory manager at phase 0 */
1071 if (!MmArmInitSystem(0, LoaderBlock
)) KeBugCheck(PHASE0_INITIALIZATION_FAILED
);
1073 /* Load boot symbols */
1074 ExpLoadBootSymbols(LoaderBlock
);
1076 /* Check if we should break after symbol load */
1077 if (KdBreakAfterSymbolLoad
) DbgBreakPointWithStatus(DBG_STATUS_CONTROL_C
);
1079 /* Check if this loader is compatible with NT 5.2 */
1080 if (LoaderBlock
->Extension
->Size
>= sizeof(LOADER_PARAMETER_EXTENSION
))
1082 /* Setup headless terminal settings */
1083 HeadlessInit(LoaderBlock
);
1086 /* Set system ranges */
1087 SharedUserData
->Reserved1
= (ULONG_PTR
)MmHighestUserAddress
;
1088 SharedUserData
->Reserved3
= (ULONG_PTR
)MmSystemRangeStart
;
1090 /* Make a copy of the NLS Tables */
1091 ExpInitNls(LoaderBlock
);
1093 /* Get the kernel's load entry */
1094 NtosEntry
= CONTAINING_RECORD(LoaderBlock
->LoadOrderListHead
.Flink
,
1095 LDR_DATA_TABLE_ENTRY
,
1098 /* Check if this is a service pack */
1099 if (CmNtCSDVersion
& 0xFFFF)
1101 /* Get the service pack string */
1102 Status
= RtlFindMessage(NtosEntry
->DllBase
,
1105 WINDOWS_NT_CSD_STRING
,
1107 if (NT_SUCCESS(Status
))
1109 /* Setup the string */
1110 RtlInitAnsiString(&CsdString
, (PCHAR
)MsgEntry
->Text
);
1112 /* Remove trailing newline */
1113 while ((CsdString
.Length
> 0) &&
1114 ((CsdString
.Buffer
[CsdString
.Length
- 1] == '\r') ||
1115 (CsdString
.Buffer
[CsdString
.Length
- 1] == '\n')))
1117 /* Skip the trailing character */
1121 /* Fill the buffer with version information */
1122 Status
= RtlStringCbPrintfA(Buffer
,
1126 (CmNtCSDVersion
& 0xFF00) >> 8,
1127 (CmNtCSDVersion
& 0xFF) ?
1128 'A' + (CmNtCSDVersion
& 0xFF) - 1 :
1133 /* Build default string */
1134 Status
= RtlStringCbPrintfA(Buffer
,
1140 /* Check for success */
1141 if (!NT_SUCCESS(Status
))
1144 KeBugCheckEx(PHASE0_INITIALIZATION_FAILED
, Status
, 0, 0, 0);
1149 /* Then this is a beta */
1150 Status
= RtlStringCbCopyExA(Buffer
,
1152 VER_PRODUCTBETA_STR
,
1156 if (!NT_SUCCESS(Status
))
1159 KeBugCheckEx(PHASE0_INITIALIZATION_FAILED
, Status
, 0, 0, 0);
1163 CmCSDVersionString
.MaximumLength
= sizeof(Buffer
) - (USHORT
)Remaining
;
1166 /* Check if we have an RC number */
1167 if (CmNtCSDVersion
& 0xFFFF0000)
1169 /* Check if we have no version data yet */
1173 Remaining
= sizeof(Buffer
);
1178 /* Add comma and space */
1179 Status
= RtlStringCbCatExA(Buffer
,
1185 if (!NT_SUCCESS(Status
))
1188 KeBugCheckEx(PHASE0_INITIALIZATION_FAILED
, Status
, 0, 0, 0);
1192 /* Add the version format string */
1193 Status
= RtlStringCbPrintfA(RcEnd
,
1196 (CmNtCSDVersion
& 0xFFFF0000) >> 16);
1197 if (!NT_SUCCESS(Status
))
1200 KeBugCheckEx(PHASE0_INITIALIZATION_FAILED
, Status
, 0, 0, 0);
1204 /* Now setup the final string */
1205 RtlInitAnsiString(&CsdString
, Buffer
);
1206 Status
= RtlAnsiStringToUnicodeString(&CmCSDVersionString
,
1209 if (!NT_SUCCESS(Status
))
1212 KeBugCheckEx(PHASE0_INITIALIZATION_FAILED
, Status
, 0, 0, 0);
1215 /* Add our version */
1216 Status
= RtlStringCbPrintfA(VersionBuffer
,
1217 sizeof(VersionBuffer
),
1219 VER_PRODUCTMAJORVERSION
,
1220 VER_PRODUCTMINORVERSION
);
1221 if (!NT_SUCCESS(Status
))
1224 KeBugCheckEx(PHASE0_INITIALIZATION_FAILED
, Status
, 0, 0, 0);
1227 /* Build the final version string */
1228 RtlCreateUnicodeStringFromAsciiz(&CmVersionString
, VersionBuffer
);
1230 /* Check if the user wants a kernel stack trace database */
1231 if (NtGlobalFlag
& FLG_KERNEL_STACK_TRACE_DB
)
1234 DPRINT1("Kernel-mode stack trace support not yet present."
1235 "FLG_KERNEL_STACK_TRACE_DB flag ignored.\n");
1238 /* Check if he wanted exception logging */
1239 if (NtGlobalFlag
& FLG_ENABLE_EXCEPTION_LOGGING
)
1242 DPRINT1("Kernel-mode exception logging support not yet present."
1243 "FLG_ENABLE_EXCEPTION_LOGGING flag ignored.\n");
1246 /* Initialize the Handle Table */
1247 ExpInitializeHandleTables();
1250 /* On checked builds, allocate the system call count table */
1251 KeServiceDescriptorTable
[0].Count
=
1252 ExAllocatePoolWithTag(NonPagedPool
,
1253 KiServiceLimit
* sizeof(ULONG
),
1256 /* Use it for the shadow table too */
1257 KeServiceDescriptorTableShadow
[0].Count
= KeServiceDescriptorTable
[0].Count
;
1259 /* Make sure allocation succeeded */
1260 if (KeServiceDescriptorTable
[0].Count
)
1262 /* Zero the call counts to 0 */
1263 RtlZeroMemory(KeServiceDescriptorTable
[0].Count
,
1264 KiServiceLimit
* sizeof(ULONG
));
1268 /* Create the Basic Object Manager Types to allow new Object Types */
1269 if (!ObInitSystem()) KeBugCheck(OBJECT_INITIALIZATION_FAILED
);
1271 /* Load basic Security for other Managers */
1272 if (!SeInitSystem()) KeBugCheck(SECURITY_INITIALIZATION_FAILED
);
1274 /* Initialize the Process Manager */
1275 if (!PsInitSystem(LoaderBlock
)) KeBugCheck(PROCESS_INITIALIZATION_FAILED
);
1277 /* Initialize the PnP Manager */
1278 if (!PpInitSystem()) KeBugCheck(PP0_INITIALIZATION_FAILED
);
1280 /* Initialize the User-Mode Debugging Subsystem */
1283 /* Calculate the tick count multiplier */
1284 ExpTickCountMultiplier
= ExComputeTickCountMultiplier(KeMaximumIncrement
);
1285 SharedUserData
->TickCountMultiplier
= ExpTickCountMultiplier
;
1287 /* Set the OS Version */
1288 SharedUserData
->NtMajorVersion
= NtMajorVersion
;
1289 SharedUserData
->NtMinorVersion
= NtMinorVersion
;
1291 /* Set the machine type */
1292 SharedUserData
->ImageNumberLow
= IMAGE_FILE_MACHINE_NATIVE
;
1293 SharedUserData
->ImageNumberHigh
= IMAGE_FILE_MACHINE_NATIVE
;
1299 Phase1InitializationDiscard(IN PVOID Context
)
1301 PLOADER_PARAMETER_BLOCK LoaderBlock
= Context
;
1302 NTSTATUS Status
, MsgStatus
;
1303 TIME_FIELDS TimeFields
;
1304 LARGE_INTEGER SystemBootTime
, UniversalBootTime
, OldTime
, Timeout
;
1305 BOOLEAN SosEnabled
, NoGuiBoot
, ResetBias
= FALSE
, AlternateShell
= FALSE
;
1306 PLDR_DATA_TABLE_ENTRY NtosEntry
;
1307 PMESSAGE_RESOURCE_ENTRY MsgEntry
;
1308 PCHAR CommandLine
, Y2KHackRequired
, SafeBoot
, Environment
;
1309 PCHAR StringBuffer
, EndBuffer
, BeginBuffer
, MpString
= "";
1310 PINIT_BUFFER InitBuffer
;
1311 ANSI_STRING TempString
;
1312 ULONG LastTzBias
, Length
, YearHack
= 0, Disposition
, MessageCode
= 0;
1315 PRTL_USER_PROCESS_INFORMATION ProcessInfo
;
1316 KEY_VALUE_PARTIAL_INFORMATION KeyPartialInfo
;
1317 UNICODE_STRING KeyName
;
1318 OBJECT_ATTRIBUTES ObjectAttributes
;
1319 HANDLE KeyHandle
, OptionHandle
;
1320 PRTL_USER_PROCESS_PARAMETERS ProcessParameters
= NULL
;
1322 /* Allocate the initialization buffer */
1323 InitBuffer
= ExAllocatePoolWithTag(NonPagedPool
,
1324 sizeof(INIT_BUFFER
),
1329 KeBugCheckEx(PHASE1_INITIALIZATION_FAILED
, STATUS_NO_MEMORY
, 8, 0, 0);
1332 /* Set to phase 1 */
1333 ExpInitializationPhase
= 1;
1335 /* Set us at maximum priority */
1336 KeSetPriorityThread(KeGetCurrentThread(), HIGH_PRIORITY
);
1338 /* Do Phase 1 HAL Initialization */
1339 if (!HalInitSystem(1, LoaderBlock
)) KeBugCheck(HAL1_INITIALIZATION_FAILED
);
1341 /* Get the command line and upcase it */
1342 CommandLine
= _strupr(LoaderBlock
->LoadOptions
);
1344 /* Check if GUI Boot is enabled */
1345 NoGuiBoot
= (strstr(CommandLine
, "NOGUIBOOT")) ? TRUE
: FALSE
;
1347 /* Get the SOS setting */
1348 SosEnabled
= strstr(CommandLine
, "SOS") ? TRUE
: FALSE
;
1350 /* Setup the boot driver */
1351 InbvEnableBootDriver(!NoGuiBoot
);
1352 InbvDriverInitialize(LoaderBlock
, 18);
1354 /* Check if GUI boot is enabled */
1357 /* It is, display the boot logo and enable printing strings */
1358 InbvEnableDisplayString(SosEnabled
);
1359 DisplayBootBitmap(SosEnabled
);
1363 /* Release display ownership if not using GUI boot */
1364 InbvNotifyDisplayOwnershipLost(NULL
);
1366 /* Don't allow boot-time strings */
1367 InbvEnableDisplayString(FALSE
);
1370 /* Check if this is LiveCD (WinPE) mode */
1371 if (strstr(CommandLine
, "MININT"))
1373 /* Setup WinPE Settings */
1374 InitIsWinPEMode
= TRUE
;
1375 InitWinPEModeType
|= (strstr(CommandLine
, "INRAM")) ? 0x80000000 : 1;
1378 /* Get the kernel's load entry */
1379 NtosEntry
= CONTAINING_RECORD(LoaderBlock
->LoadOrderListHead
.Flink
,
1380 LDR_DATA_TABLE_ENTRY
,
1383 /* Find the banner message */
1384 MsgStatus
= RtlFindMessage(NtosEntry
->DllBase
,
1390 /* Setup defaults and check if we have a version string */
1391 StringBuffer
= InitBuffer
->VersionBuffer
;
1392 BeginBuffer
= StringBuffer
;
1393 EndBuffer
= StringBuffer
;
1394 Remaining
= sizeof(InitBuffer
->VersionBuffer
);
1395 if (CmCSDVersionString
.Length
)
1397 /* Print the version string */
1398 Status
= RtlStringCbPrintfExA(StringBuffer
,
1404 &CmCSDVersionString
);
1405 if (!NT_SUCCESS(Status
))
1408 KeBugCheckEx(PHASE1_INITIALIZATION_FAILED
, Status
, 7, 0, 0);
1414 *EndBuffer
= ANSI_NULL
; /* Null-terminate the string */
1417 /* Skip over the null-terminator to start a new string */
1421 /* Build the version number */
1422 StringBuffer
= InitBuffer
->VersionNumber
;
1423 Status
= RtlStringCbPrintfA(StringBuffer
,
1424 sizeof(InitBuffer
->VersionNumber
),
1426 VER_PRODUCTMAJORVERSION
,
1427 VER_PRODUCTMINORVERSION
);
1428 if (!NT_SUCCESS(Status
))
1431 KeBugCheckEx(PHASE1_INITIALIZATION_FAILED
, Status
, 7, 0, 0);
1434 /* Check if we had found a banner message */
1435 if (NT_SUCCESS(MsgStatus
))
1437 /* Create the banner message */
1438 Status
= RtlStringCbPrintfA(EndBuffer
,
1440 (PCHAR
)MsgEntry
->Text
,
1442 NtBuildNumber
& 0xFFFF,
1444 if (!NT_SUCCESS(Status
))
1447 KeBugCheckEx(PHASE1_INITIALIZATION_FAILED
, Status
, 7, 0, 0);
1452 /* Use hard-coded banner message */
1453 Status
= RtlStringCbCopyA(EndBuffer
, Remaining
, "REACTOS (R)\n");
1454 if (!NT_SUCCESS(Status
))
1457 KeBugCheckEx(PHASE1_INITIALIZATION_FAILED
, Status
, 7, 0, 0);
1461 /* Display the version string on-screen */
1462 InbvDisplayString(EndBuffer
);
1464 /* Initialize Power Subsystem in Phase 0 */
1465 if (!PoInitSystem(0)) KeBugCheck(INTERNAL_POWER_ERROR
);
1467 /* Check for Y2K hack */
1468 Y2KHackRequired
= strstr(CommandLine
, "YEAR");
1469 if (Y2KHackRequired
) Y2KHackRequired
= strstr(Y2KHackRequired
, "=");
1470 if (Y2KHackRequired
) YearHack
= atol(Y2KHackRequired
+ 1);
1472 /* Query the clock */
1473 if ((ExCmosClockIsSane
) && (HalQueryRealTimeClock(&TimeFields
)))
1475 /* Check if we're using the Y2K hack */
1476 if (Y2KHackRequired
) TimeFields
.Year
= (CSHORT
)YearHack
;
1478 /* Convert to time fields */
1479 RtlTimeFieldsToTime(&TimeFields
, &SystemBootTime
);
1480 UniversalBootTime
= SystemBootTime
;
1482 /* Check if real time is GMT */
1483 if (!ExpRealTimeIsUniversal
)
1485 /* Check if we don't have a valid bias */
1486 if (ExpLastTimeZoneBias
== MAXULONG
)
1490 ExpLastTimeZoneBias
= ExpAltTimeZoneBias
;
1493 /* Calculate the bias in seconds */
1494 ExpTimeZoneBias
.QuadPart
= Int32x32To64(ExpLastTimeZoneBias
* 60,
1497 /* Set the boot time-zone bias */
1498 SharedUserData
->TimeZoneBias
.High2Time
= ExpTimeZoneBias
.HighPart
;
1499 SharedUserData
->TimeZoneBias
.LowPart
= ExpTimeZoneBias
.LowPart
;
1500 SharedUserData
->TimeZoneBias
.High1Time
= ExpTimeZoneBias
.HighPart
;
1502 /* Convert the boot time to local time, and set it */
1503 UniversalBootTime
.QuadPart
= SystemBootTime
.QuadPart
+
1504 ExpTimeZoneBias
.QuadPart
;
1507 /* Update the system time */
1508 KeSetSystemTime(&UniversalBootTime
, &OldTime
, FALSE
, NULL
);
1510 /* Do system callback */
1511 PoNotifySystemTimeSet();
1513 /* Remember this as the boot time */
1514 KeBootTime
= UniversalBootTime
;
1518 /* Initialize all processors */
1519 if (!HalAllProcessorsStarted()) KeBugCheck(HAL1_INITIALIZATION_FAILED
);
1522 /* HACK: We should use RtlFindMessage and not only fallback to this */
1523 MpString
= "MultiProcessor Kernel\r\n";
1526 /* Setup the "MP" String */
1527 RtlInitAnsiString(&TempString
, MpString
);
1529 /* Make sure to remove the \r\n if we actually have a string */
1530 while ((TempString
.Length
> 0) &&
1531 ((TempString
.Buffer
[TempString
.Length
- 1] == '\r') ||
1532 (TempString
.Buffer
[TempString
.Length
- 1] == '\n')))
1534 /* Skip the trailing character */
1535 TempString
.Length
--;
1538 /* Get the information string from our resource file */
1539 MsgStatus
= RtlFindMessage(NtosEntry
->DllBase
,
1542 KeNumberProcessors
> 1 ?
1543 WINDOWS_NT_INFO_STRING_PLURAL
:
1544 WINDOWS_NT_INFO_STRING
,
1547 /* Get total RAM size */
1548 Size
= MmNumberOfPhysicalPages
* PAGE_SIZE
/ 1024 / 1024;
1550 /* Create the string */
1551 StringBuffer
= InitBuffer
->VersionBuffer
;
1552 Status
= RtlStringCbPrintfA(StringBuffer
,
1553 sizeof(InitBuffer
->VersionBuffer
),
1554 NT_SUCCESS(MsgStatus
) ?
1555 (PCHAR
)MsgEntry
->Text
:
1556 "%u System Processor [%u MB Memory] %Z\n",
1560 if (!NT_SUCCESS(Status
))
1563 KeBugCheckEx(PHASE1_INITIALIZATION_FAILED
, Status
, 4, 0, 0);
1566 /* Display RAM and CPU count */
1567 InbvDisplayString(StringBuffer
);
1569 /* Update the progress bar */
1570 InbvUpdateProgressBar(5);
1572 /* Call OB initialization again */
1573 if (!ObInitSystem()) KeBugCheck(OBJECT1_INITIALIZATION_FAILED
);
1575 /* Initialize Basic System Objects and Worker Threads */
1576 if (!ExInitSystem()) KeBugCheckEx(PHASE1_INITIALIZATION_FAILED
, 0, 0, 1, 0);
1578 /* Initialize the later stages of the kernel */
1579 if (!KeInitSystem()) KeBugCheckEx(PHASE1_INITIALIZATION_FAILED
, 0, 0, 2, 0);
1581 /* Call KD Providers at Phase 1 */
1582 if (!KdInitSystem(ExpInitializationPhase
, KeLoaderBlock
))
1584 /* Failed, bugcheck */
1585 KeBugCheckEx(PHASE1_INITIALIZATION_FAILED
, 0, 0, 3, 0);
1588 /* Initialize the SRM in Phase 1 */
1589 if (!SeInitSystem()) KeBugCheck(SECURITY1_INITIALIZATION_FAILED
);
1591 /* Update the progress bar */
1592 InbvUpdateProgressBar(10);
1594 /* Create SystemRoot Link */
1595 Status
= ExpCreateSystemRootLink(LoaderBlock
);
1596 if (!NT_SUCCESS(Status
))
1598 /* Failed to create the system root link */
1599 KeBugCheckEx(SYMBOLIC_INITIALIZATION_FAILED
, Status
, 0, 0, 0);
1602 /* Set up Region Maps, Sections and the Paging File */
1603 if (!MmInitSystem(1, LoaderBlock
)) KeBugCheck(MEMORY1_INITIALIZATION_FAILED
);
1605 /* Create NLS section */
1606 ExpInitNls(LoaderBlock
);
1608 /* Initialize Cache Views */
1609 if (!CcInitializeCacheManager()) KeBugCheck(CACHE_INITIALIZATION_FAILED
);
1611 /* Initialize the Registry */
1612 if (!CmInitSystem1()) KeBugCheck(CONFIG_INITIALIZATION_FAILED
);
1614 /* Initialize Prefetcher */
1615 CcPfInitializePrefetcher();
1617 /* Update progress bar */
1618 InbvUpdateProgressBar(15);
1620 /* Update timezone information */
1621 LastTzBias
= ExpLastTimeZoneBias
;
1622 ExRefreshTimeZoneInformation(&SystemBootTime
);
1624 /* Check if we're resetting timezone data */
1627 /* Convert the local time to system time */
1628 ExLocalTimeToSystemTime(&SystemBootTime
, &UniversalBootTime
);
1629 KeBootTime
= UniversalBootTime
;
1632 /* Set the new time */
1633 KeSetSystemTime(&UniversalBootTime
, &OldTime
, FALSE
, NULL
);
1637 /* Check if the timezone switched and update the time */
1638 if (LastTzBias
!= ExpLastTimeZoneBias
) ZwSetSystemTime(NULL
, NULL
);
1641 /* Initialize the File System Runtime Library */
1642 if (!FsRtlInitSystem()) KeBugCheck(FILE_INITIALIZATION_FAILED
);
1644 /* Initialize range lists */
1645 RtlInitializeRangeListPackage();
1647 /* Report all resources used by HAL */
1648 HalReportResourceUsage();
1650 /* Call the debugger DLL */
1651 KdDebuggerInitialize1(LoaderBlock
);
1653 /* Setup PnP Manager in phase 1 */
1654 if (!PpInitSystem()) KeBugCheck(PP1_INITIALIZATION_FAILED
);
1656 /* Update progress bar */
1657 InbvUpdateProgressBar(20);
1659 /* Initialize LPC */
1660 if (!LpcInitSystem()) KeBugCheck(LPC_INITIALIZATION_FAILED
);
1662 /* Make sure we have a command line */
1665 /* Check if this is a safe mode boot */
1666 SafeBoot
= strstr(CommandLine
, "SAFEBOOT:");
1669 /* Check what kind of boot this is */
1671 if (!strncmp(SafeBoot
, "MINIMAL", 7))
1674 InitSafeBootMode
= 1;
1676 MessageCode
= BOOTING_IN_SAFEMODE_MINIMAL
;
1678 else if (!strncmp(SafeBoot
, "NETWORK", 7))
1680 /* With Networking */
1681 InitSafeBootMode
= 2;
1683 MessageCode
= BOOTING_IN_SAFEMODE_NETWORK
;
1685 else if (!strncmp(SafeBoot
, "DSREPAIR", 8))
1687 /* Domain Server Repair */
1688 InitSafeBootMode
= 3;
1690 MessageCode
= BOOTING_IN_SAFEMODE_DSREPAIR
;
1696 InitSafeBootMode
= 0;
1699 /* Check if there's any settings left */
1702 /* Check if an alternate shell was requested */
1703 if (!strncmp(SafeBoot
, "(ALTERNATESHELL)", 16))
1705 /* Remember this for later */
1706 AlternateShell
= TRUE
;
1710 /* Find the message to print out */
1711 Status
= RtlFindMessage(NtosEntry
->DllBase
,
1716 if (NT_SUCCESS(Status
))
1719 InbvDisplayString((PCHAR
)MsgEntry
->Text
);
1724 /* Make sure we have a command line */
1727 /* Check if bootlogging is enabled */
1728 if (strstr(CommandLine
, "BOOTLOG"))
1730 /* Find the message to print out */
1731 Status
= RtlFindMessage(NtosEntry
->DllBase
,
1736 if (NT_SUCCESS(Status
))
1739 InbvDisplayString((PCHAR
)MsgEntry
->Text
);
1742 /* Setup boot logging */
1743 //IopInitializeBootLogging(LoaderBlock, InitBuffer->BootlogHeader);
1747 /* Setup the Executive in Phase 2 */
1748 //ExInitSystemPhase2();
1750 /* Update progress bar */
1751 InbvUpdateProgressBar(25);
1754 /* No KD Time Slip is pending */
1755 KdpTimeSlipPending
= 0;
1758 /* Initialize in-place execution support */
1759 XIPInit(LoaderBlock
);
1761 /* Set maximum update to 75% */
1762 InbvSetProgressBarSubset(25, 75);
1764 /* Initialize the I/O Subsystem */
1765 if (!IoInitSystem(LoaderBlock
)) KeBugCheck(IO1_INITIALIZATION_FAILED
);
1767 /* Set maximum update to 100% */
1768 InbvSetProgressBarSubset(0, 100);
1770 /* Are we in safe mode? */
1771 if (InitSafeBootMode
)
1773 /* Open the safe boot key */
1774 RtlInitUnicodeString(&KeyName
,
1775 L
"\\REGISTRY\\MACHINE\\SYSTEM\\CURRENTCONTROLSET"
1776 L
"\\CONTROL\\SAFEBOOT");
1777 InitializeObjectAttributes(&ObjectAttributes
,
1779 OBJ_CASE_INSENSITIVE
,
1782 Status
= ZwOpenKey(&KeyHandle
, KEY_ALL_ACCESS
, &ObjectAttributes
);
1783 if (NT_SUCCESS(Status
))
1785 /* First check if we have an alternate shell */
1788 /* Make sure that the registry has one setup */
1789 RtlInitUnicodeString(&KeyName
, L
"AlternateShell");
1790 Status
= NtQueryValueKey(KeyHandle
,
1792 KeyValuePartialInformation
,
1794 sizeof(KeyPartialInfo
),
1796 if (!(NT_SUCCESS(Status
) || Status
== STATUS_BUFFER_OVERFLOW
))
1798 AlternateShell
= FALSE
;
1802 /* Create the option key */
1803 RtlInitUnicodeString(&KeyName
, L
"Option");
1804 InitializeObjectAttributes(&ObjectAttributes
,
1806 OBJ_CASE_INSENSITIVE
,
1809 Status
= ZwCreateKey(&OptionHandle
,
1814 REG_OPTION_VOLATILE
,
1818 /* Check if the key create worked */
1819 if (NT_SUCCESS(Status
))
1821 /* Write the safe boot type */
1822 RtlInitUnicodeString(&KeyName
, L
"OptionValue");
1823 NtSetValueKey(OptionHandle
,
1828 sizeof(InitSafeBootMode
));
1830 /* Check if we have to use an alternate shell */
1833 /* Remember this for later */
1835 RtlInitUnicodeString(&KeyName
, L
"UseAlternateShell");
1836 NtSetValueKey(OptionHandle
,
1841 sizeof(Disposition
));
1844 /* Close the options key handle */
1845 NtClose(OptionHandle
);
1850 /* Are we in Win PE mode? */
1851 if (InitIsWinPEMode
)
1853 /* Open the safe control key */
1854 RtlInitUnicodeString(&KeyName
,
1855 L
"\\REGISTRY\\MACHINE\\SYSTEM\\CURRENTCONTROLSET"
1857 InitializeObjectAttributes(&ObjectAttributes
,
1859 OBJ_CASE_INSENSITIVE
,
1862 Status
= ZwOpenKey(&KeyHandle
, KEY_ALL_ACCESS
, &ObjectAttributes
);
1863 if (!NT_SUCCESS(Status
))
1866 KeBugCheckEx(PHASE1_INITIALIZATION_FAILED
, Status
, 6, 0, 0);
1869 /* Create the MiniNT key */
1870 RtlInitUnicodeString(&KeyName
, L
"MiniNT");
1871 InitializeObjectAttributes(&ObjectAttributes
,
1873 OBJ_CASE_INSENSITIVE
,
1876 Status
= ZwCreateKey(&OptionHandle
,
1881 REG_OPTION_VOLATILE
,
1883 if (!NT_SUCCESS(Status
))
1886 KeBugCheckEx(PHASE1_INITIALIZATION_FAILED
, Status
, 6, 0, 0);
1889 /* Close the handles */
1891 NtClose(OptionHandle
);
1894 /* FIXME: This doesn't do anything for now */
1895 MmArmInitSystem(2, LoaderBlock
);
1897 /* Update progress bar */
1898 InbvUpdateProgressBar(80);
1900 /* Initialize VDM support */
1901 #if defined(_M_IX86)
1902 KeI386VdmInitialize();
1905 /* Initialize Power Subsystem in Phase 1*/
1906 if (!PoInitSystem(1)) KeBugCheck(INTERNAL_POWER_ERROR
);
1908 /* Update progress bar */
1909 InbvUpdateProgressBar(90);
1911 /* Initialize the Process Manager at Phase 1 */
1912 if (!PsInitSystem(LoaderBlock
)) KeBugCheck(PROCESS1_INITIALIZATION_FAILED
);
1914 /* Make sure nobody touches the loader block again */
1915 if (LoaderBlock
== KeLoaderBlock
) KeLoaderBlock
= NULL
;
1916 LoaderBlock
= Context
= NULL
;
1918 /* Update progress bar */
1919 InbvUpdateProgressBar(100);
1921 /* Clean the screen */
1922 if (InbvBootDriverInstalled
) FinalizeBootLogo();
1924 /* Allow strings to be displayed */
1925 InbvEnableDisplayString(TRUE
);
1927 /* Launch initial process */
1928 DPRINT1("Free non-cache pages: %lx\n", MmAvailablePages
+ MiMemoryConsumers
[MC_CACHE
].PagesUsed
);
1929 ProcessInfo
= &InitBuffer
->ProcessInfo
;
1930 ExpLoadInitialProcess(InitBuffer
, &ProcessParameters
, &Environment
);
1932 /* Wait 5 seconds for initial process to initialize */
1933 Timeout
.QuadPart
= Int32x32To64(5, -10000000);
1934 Status
= ZwWaitForSingleObject(ProcessInfo
->ProcessHandle
, FALSE
, &Timeout
);
1935 if (Status
== STATUS_SUCCESS
)
1937 /* Failed, display error */
1938 DPRINT1("INIT: Session Manager terminated.\n");
1940 /* Bugcheck the system if SMSS couldn't initialize */
1941 KeBugCheck(SESSION5_INITIALIZATION_FAILED
);
1944 /* Close process handles */
1945 ZwClose(ProcessInfo
->ThreadHandle
);
1946 ZwClose(ProcessInfo
->ProcessHandle
);
1948 /* Free the initial process environment */
1950 ZwFreeVirtualMemory(NtCurrentProcess(),
1951 (PVOID
*)&Environment
,
1955 /* Free the initial process parameters */
1957 ZwFreeVirtualMemory(NtCurrentProcess(),
1958 (PVOID
*)&ProcessParameters
,
1962 /* Increase init phase */
1963 ExpInitializationPhase
++;
1965 /* Free the boot buffer */
1966 ExFreePoolWithTag(InitBuffer
, TAG_INIT
);
1967 DPRINT1("Free non-cache pages: %lx\n", MmAvailablePages
+ MiMemoryConsumers
[MC_CACHE
].PagesUsed
);
1972 Phase1Initialization(IN PVOID Context
)
1974 /* Do the .INIT part of Phase 1 which we can free later */
1975 Phase1InitializationDiscard(Context
);
1977 /* Jump into zero page thread */