2 * PROJECT: ReactOS Kernel
3 * LICENSE: BSD - See COPYING.ARM in the top level directory
4 * FILE: ntoskrnl/config/cmsysini.c
5 * PURPOSE: Configuration Manager - System Initialization Code
6 * PROGRAMMERS: ReactOS Portable Systems Group
7 * Alex Ionescu (alex.ionescu@reactos.org)
10 /* INCLUDES *******************************************************************/
16 POBJECT_TYPE CmpKeyObjectType
;
17 PCMHIVE CmiVolatileHive
;
18 LIST_ENTRY CmpHiveListHead
;
19 ERESOURCE CmpRegistryLock
;
20 KGUARDED_MUTEX CmpSelfHealQueueLock
;
21 LIST_ENTRY CmpSelfHealQueueListHead
;
22 KEVENT CmpLoadWorkerEvent
;
23 LONG CmpLoadWorkerIncrement
;
24 PEPROCESS CmpSystemProcess
;
25 BOOLEAN HvShutdownComplete
;
26 PVOID CmpRegistryLockCallerCaller
, CmpRegistryLockCaller
;
27 BOOLEAN CmpFlushOnLockRelease
;
28 BOOLEAN CmpSpecialBootCondition
;
30 BOOLEAN CmpWasSetupBoot
;
31 BOOLEAN CmpProfileLoaded
;
32 BOOLEAN CmpNoVolatileCreates
;
33 ULONG CmpTraceLevel
= 0;
35 extern LONG CmpFlushStarveWriters
;
36 extern BOOLEAN CmFirstTime
;
38 /* FUNCTIONS ******************************************************************/
43 _In_z_ PCWSTR LinkKeyName
,
44 _In_z_ PCWSTR TargetKeyName
)
47 OBJECT_ATTRIBUTES ObjectAttributes
;
48 UNICODE_STRING KeyName
;
54 /* Initialize the object attributes */
55 RtlInitUnicodeString(&KeyName
, LinkKeyName
);
56 InitializeObjectAttributes(&ObjectAttributes
,
58 OBJ_CASE_INSENSITIVE
| OBJ_KERNEL_HANDLE
,
62 /* Create the link key */
63 Status
= ZwCreateKey(&LinkKeyHandle
,
68 REG_OPTION_VOLATILE
| REG_OPTION_CREATE_LINK
,
70 if (!NT_SUCCESS(Status
))
72 DPRINT1("CM: CmpLinkKeyToHive: couldn't create %S, Status = 0x%lx\n",
77 /* Check if the new key was actually created */
78 if (Disposition
!= REG_CREATED_NEW_KEY
)
80 DPRINT1("CM: CmpLinkKeyToHive: %S already exists!\n", LinkKeyName
);
81 ZwClose(LinkKeyHandle
);
85 /* Set the target key name as link target */
86 RtlInitUnicodeString(&KeyName
, TargetKeyName
);
87 Status
= ZwSetValueKey(LinkKeyHandle
,
88 &CmSymbolicLinkValueName
,
94 /* Close the link key handle */
95 ObCloseHandle(LinkKeyHandle
, KernelMode
);
97 if (!NT_SUCCESS(Status
))
99 DPRINT1("CM: CmpLinkKeyToHive: couldn't create symbolic link for %S, Status = 0x%lx\n",
100 TargetKeyName
, Status
);
109 CmpDeleteKeyObject(PVOID DeletedObject
)
111 PCM_KEY_BODY KeyBody
= (PCM_KEY_BODY
)DeletedObject
;
112 PCM_KEY_CONTROL_BLOCK Kcb
;
113 REG_KEY_HANDLE_CLOSE_INFORMATION KeyHandleCloseInfo
;
114 REG_POST_OPERATION_INFORMATION PostOperationInfo
;
118 /* First off, prepare the handle close information callback */
119 PostOperationInfo
.Object
= KeyBody
;
120 KeyHandleCloseInfo
.Object
= KeyBody
;
121 Status
= CmiCallRegisteredCallbacks(RegNtPreKeyHandleClose
,
122 &KeyHandleCloseInfo
);
123 if (!NT_SUCCESS(Status
))
125 /* If we failed, notify the post routine */
126 PostOperationInfo
.Status
= Status
;
127 CmiCallRegisteredCallbacks(RegNtPostKeyHandleClose
, &PostOperationInfo
);
131 /* Acquire hive lock */
134 /* Make sure this is a valid key body */
135 if (KeyBody
->Type
== CM_KEY_BODY_TYPE
)
138 Kcb
= KeyBody
->KeyControlBlock
;
142 DelistKeyBodyFromKCB(KeyBody
, FALSE
);
144 /* Dereference the KCB */
145 CmpDelayDerefKeyControlBlock(Kcb
);
149 /* Release the registry lock */
152 /* Do the post callback */
153 PostOperationInfo
.Status
= STATUS_SUCCESS
;
154 CmiCallRegisteredCallbacks(RegNtPostKeyHandleClose
, &PostOperationInfo
);
159 CmpCloseKeyObject(IN PEPROCESS Process OPTIONAL
,
161 IN ACCESS_MASK GrantedAccess
,
162 IN ULONG ProcessHandleCount
,
163 IN ULONG SystemHandleCount
)
165 PCM_KEY_BODY KeyBody
= (PCM_KEY_BODY
)Object
;
168 /* Don't do anything if we're not the last handle */
169 if (SystemHandleCount
> 1) return;
171 /* Make sure we're a valid key body */
172 if (KeyBody
->Type
== CM_KEY_BODY_TYPE
)
174 /* Don't do anything if we don't have a notify block */
175 if (!KeyBody
->NotifyBlock
) return;
177 /* This shouldn't happen yet */
184 CmpQueryKeyName(IN PVOID ObjectBody
,
186 IN OUT POBJECT_NAME_INFORMATION ObjectNameInfo
,
188 OUT PULONG ReturnLength
,
189 IN KPROCESSOR_MODE PreviousMode
)
191 PUNICODE_STRING KeyName
;
193 NTSTATUS Status
= STATUS_SUCCESS
;
194 PCM_KEY_BODY KeyBody
= (PCM_KEY_BODY
)ObjectBody
;
195 PCM_KEY_CONTROL_BLOCK Kcb
= KeyBody
->KeyControlBlock
;
197 /* Acquire hive lock */
200 /* Lock KCB shared */
201 CmpAcquireKcbLockShared(Kcb
);
203 /* Check if it's a deleted block */
206 /* Release the locks */
207 CmpReleaseKcbLock(Kcb
);
210 /* Let the caller know it's deleted */
211 return STATUS_KEY_DELETED
;
215 KeyName
= CmpConstructName(Kcb
);
217 /* Release the locks */
218 CmpReleaseKcbLock(Kcb
);
221 /* Check if we got the name */
222 if (!KeyName
) return STATUS_INSUFFICIENT_RESOURCES
;
224 /* Set the returned length */
225 *ReturnLength
= KeyName
->Length
+ sizeof(OBJECT_NAME_INFORMATION
) + sizeof(WCHAR
);
227 /* Calculate amount of bytes to copy into the buffer */
228 BytesToCopy
= KeyName
->Length
+ sizeof(WCHAR
);
230 /* Check if the provided buffer is too small to fit even anything */
231 if ((Length
<= sizeof(OBJECT_NAME_INFORMATION
)) ||
232 ((Length
< (*ReturnLength
)) && (BytesToCopy
< sizeof(WCHAR
))))
234 /* Free the buffer allocated by CmpConstructName */
235 ExFreePoolWithTag(KeyName
, TAG_CM
);
237 /* Return buffer length failure without writing anything there because nothing fits */
238 return STATUS_INFO_LENGTH_MISMATCH
;
241 /* Check if the provided buffer can be partially written */
242 if (Length
< (*ReturnLength
))
244 /* Yes, indicate so in the return status */
245 Status
= STATUS_INFO_LENGTH_MISMATCH
;
247 /* Calculate amount of bytes which the provided buffer could handle */
248 BytesToCopy
= Length
- sizeof(OBJECT_NAME_INFORMATION
);
251 /* Remove the null termination character from the size */
252 BytesToCopy
-= sizeof(WCHAR
);
254 /* Fill in the result */
257 /* Return data to user */
258 ObjectNameInfo
->Name
.Buffer
= (PWCHAR
)(ObjectNameInfo
+ 1);
259 ObjectNameInfo
->Name
.MaximumLength
= KeyName
->Length
;
260 ObjectNameInfo
->Name
.Length
= KeyName
->Length
;
262 /* Copy string content*/
263 RtlCopyMemory(ObjectNameInfo
->Name
.Buffer
,
267 /* Null terminate it */
268 ObjectNameInfo
->Name
.Buffer
[BytesToCopy
/ sizeof(WCHAR
)] = UNICODE_NULL
;
270 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
273 Status
= _SEH2_GetExceptionCode();
277 /* Free the buffer allocated by CmpConstructName */
278 ExFreePoolWithTag(KeyName
, TAG_CM
);
286 CmpInitHiveFromFile(IN PCUNICODE_STRING HiveName
,
292 ULONG HiveDisposition
, LogDisposition
;
293 HANDLE FileHandle
= NULL
, LogHandle
= NULL
;
295 ULONG Operation
, FileType
;
302 /* Open or create the hive files */
303 Status
= CmpOpenHiveFiles(HiveName
,
313 if (!NT_SUCCESS(Status
)) return Status
;
315 /* Check if we have a log handle */
316 FileType
= (LogHandle
) ? HFILE_TYPE_LOG
: HFILE_TYPE_PRIMARY
;
318 /* Check if we created or opened the hive */
319 if (HiveDisposition
== FILE_CREATED
)
321 /* Do a create operation */
322 Operation
= HINIT_CREATE
;
327 /* Open it as a file */
328 Operation
= HINIT_FILE
;
332 /* Check if we're sharing hives */
333 if (CmpShareSystemHives
)
335 /* Then force using the primary hive */
336 FileType
= HFILE_TYPE_PRIMARY
;
339 /* Get rid of the log handle */
345 /* Check if we're too late */
346 if (HvShutdownComplete
)
350 if (LogHandle
) ZwClose(LogHandle
);
351 return STATUS_TOO_LATE
;
354 /* Initialize the hive */
355 Status
= CmpInitializeHive(&NewHive
,
365 if (!NT_SUCCESS(Status
))
369 if (LogHandle
) ZwClose(LogHandle
);
373 /* Success, return hive */
376 /* Duplicate the hive name */
377 NewHive
->FileFullPath
.Buffer
= ExAllocatePoolWithTag(PagedPool
,
380 if (NewHive
->FileFullPath
.Buffer
)
382 /* Copy the string */
383 RtlCopyMemory(NewHive
->FileFullPath
.Buffer
,
386 NewHive
->FileFullPath
.Length
= HiveName
->Length
;
387 NewHive
->FileFullPath
.MaximumLength
= HiveName
->Length
;
391 return STATUS_SUCCESS
;
397 CmpSetSystemValues(IN PLOADER_PARAMETER_BLOCK LoaderBlock
)
400 OBJECT_ATTRIBUTES ObjectAttributes
;
402 UNICODE_STRING KeyName
, ValueName
= { 0, 0, NULL
};
404 ASSERT(LoaderBlock
!= NULL
);
406 /* Setup attributes for loader options */
407 RtlInitUnicodeString(&KeyName
,
408 L
"\\REGISTRY\\MACHINE\\SYSTEM\\CurrentControlSet\\"
410 InitializeObjectAttributes(&ObjectAttributes
,
412 OBJ_CASE_INSENSITIVE
,
415 Status
= NtOpenKey(&KeyHandle
, KEY_WRITE
, &ObjectAttributes
);
416 if (!NT_SUCCESS(Status
))
419 /* Setup the value for the system start options */
420 RtlInitUnicodeString(&KeyName
, L
"SystemStartOptions");
421 Status
= NtSetValueKey(KeyHandle
,
425 CmpLoadOptions
.Buffer
,
426 CmpLoadOptions
.Length
);
427 if (!NT_SUCCESS(Status
))
430 /* Setup the value for the system boot device in ARC format */
431 RtlInitUnicodeString(&KeyName
, L
"SystemBootDevice");
432 RtlCreateUnicodeStringFromAsciiz(&ValueName
, LoaderBlock
->ArcBootDeviceName
);
433 Status
= NtSetValueKey(KeyHandle
,
440 /* Free the temporary string */
441 RtlFreeUnicodeString(&ValueName
);
444 /* Close the key and return */
452 CmpCreateHardwareProfile(HANDLE ControlSetHandle
)
454 OBJECT_ATTRIBUTES ObjectAttributes
;
455 UNICODE_STRING KeyName
;
456 HANDLE ProfilesHandle
= NULL
;
457 HANDLE ProfileHandle
= NULL
;
461 DPRINT("CmpCreateHardwareProfile()\n");
463 /* Create the Hardware Profiles key */
464 RtlInitUnicodeString(&KeyName
, L
"Hardware Profiles");
465 InitializeObjectAttributes(&ObjectAttributes
,
467 OBJ_CASE_INSENSITIVE
,
470 Status
= NtCreateKey(&ProfilesHandle
,
477 if (!NT_SUCCESS(Status
))
479 DPRINT1("Creating the Hardware Profile key failed\n");
484 ASSERT(Disposition
== REG_CREATED_NEW_KEY
);
486 /* Create the 0000 key */
487 RtlInitUnicodeString(&KeyName
, L
"0000");
488 InitializeObjectAttributes(&ObjectAttributes
,
490 OBJ_CASE_INSENSITIVE
,
493 Status
= NtCreateKey(&ProfileHandle
,
500 if (!NT_SUCCESS(Status
))
502 DPRINT1("Creating the Hardware Profile\\0000 key failed\n");
507 ASSERT(Disposition
== REG_CREATED_NEW_KEY
);
511 NtClose(ProfilesHandle
);
514 NtClose(ProfileHandle
);
516 DPRINT("CmpCreateHardwareProfile() done\n");
524 CmpCreateControlSet(IN PLOADER_PARAMETER_BLOCK LoaderBlock
)
526 UNICODE_STRING ConfigName
= RTL_CONSTANT_STRING(L
"Control\\IDConfigDB");
527 UNICODE_STRING SelectName
=
528 RTL_CONSTANT_STRING(L
"\\Registry\\Machine\\System\\Select");
529 UNICODE_STRING KeyName
;
530 OBJECT_ATTRIBUTES ObjectAttributes
;
531 CHAR ValueInfoBuffer
[128];
532 PKEY_VALUE_FULL_INFORMATION ValueInfo
;
533 WCHAR UnicodeBuffer
[128];
534 HANDLE SelectHandle
= NULL
;
535 HANDLE KeyHandle
= NULL
;
536 HANDLE ConfigHandle
= NULL
;
537 HANDLE ProfileHandle
= NULL
;
538 HANDLE ParentHandle
= NULL
;
539 ULONG ControlSet
, HwProfile
;
541 ULONG ResultLength
, Disposition
;
542 PLOADER_PARAMETER_EXTENSION LoaderExtension
;
545 /* ReactOS Hack: Hard-code current to 001 for SetupLdr */
546 if (LoaderBlock
->RegistryBase
== NULL
)
548 /* Build the ControlSet001 key */
549 RtlInitUnicodeString(&KeyName
,
550 L
"\\Registry\\Machine\\System\\ControlSet001");
551 InitializeObjectAttributes(&ObjectAttributes
,
553 OBJ_CASE_INSENSITIVE
,
556 Status
= NtCreateKey(&KeyHandle
,
563 if (!NT_SUCCESS(Status
))
565 DPRINT1("Failed to create ControlSet001 key: 0x%lx\n", Status
);
569 /* Create the Hardware Profile keys */
570 Status
= CmpCreateHardwareProfile(KeyHandle
);
571 if (!NT_SUCCESS(Status
))
573 DPRINT1("Failed to create Hardware profile keys: 0x%lx\n", Status
);
577 /* Use hard-coded setting */
582 /* Open the select key */
583 InitializeObjectAttributes(&ObjectAttributes
,
585 OBJ_CASE_INSENSITIVE
,
588 Status
= NtOpenKey(&SelectHandle
, KEY_READ
, &ObjectAttributes
);
589 if (!NT_SUCCESS(Status
))
591 DPRINT1("Failed to open select key: 0x%lx\n", Status
);
595 /* Open the current value */
596 RtlInitUnicodeString(&KeyName
, L
"Current");
597 Status
= NtQueryValueKey(SelectHandle
,
599 KeyValueFullInformation
,
601 sizeof(ValueInfoBuffer
),
603 if (!NT_SUCCESS(Status
))
605 DPRINT1("Failed to open the Current value: 0x%lx\n", Status
);
609 /* Get the actual value pointer, and get the control set ID */
610 ValueInfo
= (PKEY_VALUE_FULL_INFORMATION
)ValueInfoBuffer
;
611 ControlSet
= *(PULONG
)((PUCHAR
)ValueInfo
+ ValueInfo
->DataOffset
);
614 /* Create the current control set key */
615 RtlInitUnicodeString(&KeyName
,
616 L
"\\Registry\\Machine\\System\\CurrentControlSet");
617 InitializeObjectAttributes(&ObjectAttributes
,
619 OBJ_CASE_INSENSITIVE
,
622 Status
= NtCreateKey(&KeyHandle
,
627 REG_OPTION_VOLATILE
| REG_OPTION_CREATE_LINK
,
629 if (!NT_SUCCESS(Status
))
633 ASSERT(Disposition
== REG_CREATED_NEW_KEY
);
635 /* Initialize the target link name */
636 Status
= RtlStringCbPrintfW(UnicodeBuffer
, sizeof(UnicodeBuffer
),
637 L
"\\Registry\\Machine\\System\\ControlSet%03ld",
639 if (!NT_SUCCESS(Status
))
642 RtlInitUnicodeString(&KeyName
, UnicodeBuffer
);
645 Status
= NtSetValueKey(KeyHandle
,
646 &CmSymbolicLinkValueName
,
651 if (!NT_SUCCESS(Status
))
654 /* Get the configuration database key */
655 InitializeObjectAttributes(&ObjectAttributes
,
657 OBJ_CASE_INSENSITIVE
,
660 Status
= NtOpenKey(&ConfigHandle
, KEY_READ
, &ObjectAttributes
);
662 /* Check if we don't have one */
663 if (!NT_SUCCESS(Status
))
665 /* Cleanup and exit */
666 Status
= STATUS_SUCCESS
;
670 /* ReactOS Hack: Hard-code current to 001 for SetupLdr */
671 if (LoaderBlock
->RegistryBase
== NULL
)
677 /* Now get the current config */
678 RtlInitUnicodeString(&KeyName
, L
"CurrentConfig");
679 Status
= NtQueryValueKey(ConfigHandle
,
681 KeyValueFullInformation
,
683 sizeof(ValueInfoBuffer
),
686 /* Set pointer to buffer */
687 ValueInfo
= (PKEY_VALUE_FULL_INFORMATION
)ValueInfoBuffer
;
689 /* Check if we failed or got a non DWORD-value */
690 if (!(NT_SUCCESS(Status
)) || (ValueInfo
->Type
!= REG_DWORD
))
692 Status
= STATUS_SUCCESS
;
696 /* Get the hadware profile */
697 HwProfile
= *(PULONG
)((PUCHAR
)ValueInfo
+ ValueInfo
->DataOffset
);
700 /* Open the hardware profile key */
701 RtlInitUnicodeString(&KeyName
,
702 L
"\\Registry\\Machine\\System\\CurrentControlSet"
703 L
"\\Hardware Profiles");
704 InitializeObjectAttributes(&ObjectAttributes
,
706 OBJ_CASE_INSENSITIVE
,
709 Status
= NtOpenKey(&ParentHandle
, KEY_READ
, &ObjectAttributes
);
710 if (!NT_SUCCESS(Status
))
712 /* Exit and clean up */
713 Status
= STATUS_SUCCESS
;
717 /* Build the profile name */
718 RtlStringCbPrintfW(UnicodeBuffer
, sizeof(UnicodeBuffer
),
719 L
"%04ld", HwProfile
);
720 RtlInitUnicodeString(&KeyName
, UnicodeBuffer
);
722 /* Open the associated key */
723 InitializeObjectAttributes(&ObjectAttributes
,
725 OBJ_CASE_INSENSITIVE
,
728 Status
= NtOpenKey(&ProfileHandle
,
729 KEY_READ
| KEY_WRITE
,
731 if (!NT_SUCCESS (Status
))
733 /* Cleanup and exit */
734 Status
= STATUS_SUCCESS
;
738 /* Check if we have a loader block extension */
739 LoaderExtension
= LoaderBlock
->Extension
;
742 DPRINT("ReactOS doesn't support NTLDR Profiles yet!\n");
745 /* Create the current hardware profile key */
746 RtlInitUnicodeString(&KeyName
,
747 L
"\\Registry\\Machine\\System\\CurrentControlSet\\"
748 L
"Hardware Profiles\\Current");
749 InitializeObjectAttributes(&ObjectAttributes
,
751 OBJ_CASE_INSENSITIVE
,
754 Status
= NtCreateKey(&KeyHandle
,
759 REG_OPTION_VOLATILE
| REG_OPTION_CREATE_LINK
,
761 if (NT_SUCCESS(Status
))
764 ASSERT(Disposition
== REG_CREATED_NEW_KEY
);
766 /* Create the profile name */
767 RtlStringCbPrintfW(UnicodeBuffer
, sizeof(UnicodeBuffer
),
768 L
"\\Registry\\Machine\\System\\CurrentControlSet\\"
769 L
"Hardware Profiles\\%04ld",
771 RtlInitUnicodeString(&KeyName
, UnicodeBuffer
);
774 Status
= NtSetValueKey(KeyHandle
,
775 &CmSymbolicLinkValueName
,
782 Status
= STATUS_SUCCESS
;
785 /* Close every opened handle */
786 if (SelectHandle
) NtClose(SelectHandle
);
787 if (KeyHandle
) NtClose(KeyHandle
);
788 if (ConfigHandle
) NtClose(ConfigHandle
);
789 if (ProfileHandle
) NtClose(ProfileHandle
);
790 if (ParentHandle
) NtClose(ParentHandle
);
792 DPRINT("CmpCreateControlSet() done\n");
798 CmpLinkHiveToMaster(IN PUNICODE_STRING LinkName
,
799 IN HANDLE RootDirectory
,
800 IN PCMHIVE RegistryHive
,
802 IN PSECURITY_DESCRIPTOR SecurityDescriptor
)
804 OBJECT_ATTRIBUTES ObjectAttributes
;
806 CM_PARSE_CONTEXT ParseContext
= {0};
808 PCM_KEY_BODY KeyBody
;
811 /* Setup the object attributes */
812 InitializeObjectAttributes(&ObjectAttributes
,
814 OBJ_CASE_INSENSITIVE
| OBJ_KERNEL_HANDLE
,
818 /* Setup the parse context */
819 ParseContext
.CreateLink
= TRUE
;
820 ParseContext
.CreateOperation
= TRUE
;
821 ParseContext
.ChildHive
.KeyHive
= &RegistryHive
->Hive
;
823 /* Check if we have a root keycell or if we need to create it */
827 ParseContext
.ChildHive
.KeyCell
= HCELL_NIL
;
832 ParseContext
.ChildHive
.KeyCell
= RegistryHive
->Hive
.BaseBlock
->RootCell
;
835 /* Create the link node */
836 Status
= ObOpenObjectByName(&ObjectAttributes
,
840 KEY_READ
| KEY_WRITE
,
841 (PVOID
)&ParseContext
,
843 if (!NT_SUCCESS(Status
)) return Status
;
845 /* Mark the hive as clean */
846 RegistryHive
->Hive
.DirtyFlag
= FALSE
;
848 /* ReactOS Hack: Keep alive */
849 Status
= ObReferenceObjectByHandle(KeyHandle
,
855 ASSERT(NT_SUCCESS(Status
));
857 /* Close the extra handle */
859 return STATUS_SUCCESS
;
865 CmpInitializeSystemHive(IN PLOADER_PARAMETER_BLOCK LoaderBlock
)
867 static const UNICODE_STRING HiveName
= RTL_CONSTANT_STRING(L
"SYSTEM");
869 ANSI_STRING LoadString
;
873 UNICODE_STRING KeyName
;
874 PCMHIVE SystemHive
= NULL
;
875 PSECURITY_DESCRIPTOR SecurityDescriptor
;
880 /* Setup the ansi string */
881 RtlInitAnsiString(&LoadString
, LoaderBlock
->LoadOptions
);
883 /* Allocate the unicode buffer */
884 Length
= LoadString
.Length
* sizeof(WCHAR
) + sizeof(UNICODE_NULL
);
885 Buffer
= ExAllocatePoolWithTag(PagedPool
, Length
, TAG_CM
);
889 KeBugCheckEx(BAD_SYSTEM_CONFIG_INFO
, 3, 1, (ULONG_PTR
)LoaderBlock
, 0);
892 /* Setup the unicode string */
893 RtlInitEmptyUnicodeString(&CmpLoadOptions
, Buffer
, (USHORT
)Length
);
895 /* Add the load options and null-terminate */
896 Status
= RtlAnsiStringToUnicodeString(&CmpLoadOptions
, &LoadString
, FALSE
);
897 if (!NT_SUCCESS(Status
))
902 CmpLoadOptions
.Buffer
[LoadString
.Length
] = UNICODE_NULL
;
903 CmpLoadOptions
.Length
+= sizeof(WCHAR
);
905 /* Get the System Hive base address */
906 HiveBase
= LoaderBlock
->RegistryBase
;
908 Status
= CmpInitializeHive(&SystemHive
,
909 HiveBase
? HINIT_MEMORY
: HINIT_CREATE
,
918 if (!NT_SUCCESS(Status
))
923 /* Set the hive filename */
924 Success
= RtlCreateUnicodeString(&SystemHive
->FileFullPath
,
925 L
"\\SystemRoot\\System32\\Config\\SYSTEM");
931 /* Manually set the hive as volatile, if in Live CD mode */
932 if (HiveBase
&& CmpShareSystemHives
)
934 SystemHive
->Hive
.HiveFlags
= HIVE_VOLATILE
;
937 /* Save the boot type */
938 CmpBootType
= SystemHive
->Hive
.BaseBlock
->BootType
;
940 /* Are we in self-healing mode? */
943 /* Disable self-healing internally and check if boot type wanted it */
947 /* We're disabled, so bugcheck */
948 KeBugCheckEx(BAD_SYSTEM_CONFIG_INFO
,
951 (ULONG_PTR
)SystemHive
,
956 /* Create the default security descriptor */
957 SecurityDescriptor
= CmpHiveRootSecurityDescriptor();
959 /* Attach it to the system key */
960 /* Let CmpLinkHiveToMaster allocate a new hive if we got none from the LoaderBlock. */
961 RtlInitUnicodeString(&KeyName
, L
"\\Registry\\Machine\\SYSTEM");
962 Status
= CmpLinkHiveToMaster(&KeyName
,
968 /* Free the security descriptor */
969 ExFreePoolWithTag(SecurityDescriptor
, TAG_CMSD
);
970 if (!NT_SUCCESS(Status
)) return FALSE
;
972 /* Add the hive to the hive list */
973 CmpMachineHiveList
[3].CmHive
= SystemHive
;
982 CmpCreateObjectTypes(VOID
)
984 OBJECT_TYPE_INITIALIZER ObjectTypeInitializer
;
986 GENERIC_MAPPING CmpKeyMapping
= {KEY_READ
,
992 /* Initialize the Key object type */
993 RtlZeroMemory(&ObjectTypeInitializer
, sizeof(ObjectTypeInitializer
));
994 RtlInitUnicodeString(&Name
, L
"Key");
995 ObjectTypeInitializer
.Length
= sizeof(ObjectTypeInitializer
);
996 ObjectTypeInitializer
.DefaultPagedPoolCharge
= sizeof(CM_KEY_BODY
);
997 ObjectTypeInitializer
.GenericMapping
= CmpKeyMapping
;
998 ObjectTypeInitializer
.PoolType
= PagedPool
;
999 ObjectTypeInitializer
.ValidAccessMask
= KEY_ALL_ACCESS
;
1000 ObjectTypeInitializer
.UseDefaultObject
= TRUE
;
1001 ObjectTypeInitializer
.DeleteProcedure
= CmpDeleteKeyObject
;
1002 ObjectTypeInitializer
.ParseProcedure
= CmpParseKey
;
1003 ObjectTypeInitializer
.SecurityProcedure
= CmpSecurityMethod
;
1004 ObjectTypeInitializer
.QueryNameProcedure
= CmpQueryKeyName
;
1005 ObjectTypeInitializer
.CloseProcedure
= CmpCloseKeyObject
;
1006 ObjectTypeInitializer
.SecurityRequired
= TRUE
;
1007 ObjectTypeInitializer
.InvalidAttributes
= OBJ_EXCLUSIVE
| OBJ_PERMANENT
;
1010 return ObCreateObjectType(&Name
, &ObjectTypeInitializer
, NULL
, &CmpKeyObjectType
);
1016 CmpCreateRootNode(IN PHHIVE Hive
,
1018 OUT PHCELL_INDEX Index
)
1020 UNICODE_STRING KeyName
;
1021 PCM_KEY_NODE KeyCell
;
1024 /* Initialize the node name and allocate it */
1025 RtlInitUnicodeString(&KeyName
, Name
);
1026 *Index
= HvAllocateCell(Hive
,
1027 FIELD_OFFSET(CM_KEY_NODE
, Name
) +
1028 CmpNameSize(Hive
, &KeyName
),
1031 if (*Index
== HCELL_NIL
) return FALSE
;
1033 /* Set the cell index and get the data */
1034 Hive
->BaseBlock
->RootCell
= *Index
;
1035 KeyCell
= (PCM_KEY_NODE
)HvGetCell(Hive
, *Index
);
1036 if (!KeyCell
) return FALSE
;
1038 /* Setup the cell */
1039 KeyCell
->Signature
= CM_KEY_NODE_SIGNATURE
;
1040 KeyCell
->Flags
= KEY_HIVE_ENTRY
| KEY_NO_DELETE
;
1041 KeQuerySystemTime(&KeyCell
->LastWriteTime
);
1042 KeyCell
->Parent
= HCELL_NIL
;
1043 KeyCell
->SubKeyCounts
[Stable
] = 0;
1044 KeyCell
->SubKeyCounts
[Volatile
] = 0;
1045 KeyCell
->SubKeyLists
[Stable
] = HCELL_NIL
;
1046 KeyCell
->SubKeyLists
[Volatile
] = HCELL_NIL
;
1047 KeyCell
->ValueList
.Count
= 0;
1048 KeyCell
->ValueList
.List
= HCELL_NIL
;
1049 KeyCell
->Security
= HCELL_NIL
;
1050 KeyCell
->Class
= HCELL_NIL
;
1051 KeyCell
->ClassLength
= 0;
1052 KeyCell
->MaxNameLen
= 0;
1053 KeyCell
->MaxClassLen
= 0;
1054 KeyCell
->MaxValueNameLen
= 0;
1055 KeyCell
->MaxValueDataLen
= 0;
1057 /* Copy the name (this will also set the length) */
1058 KeyCell
->NameLength
= CmpCopyName(Hive
, KeyCell
->Name
, &KeyName
);
1060 /* Check if the name was compressed and set the flag if so */
1061 if (KeyCell
->NameLength
< KeyName
.Length
)
1062 KeyCell
->Flags
|= KEY_COMP_NAME
;
1064 /* Return success */
1065 HvReleaseCell(Hive
, *Index
);
1072 CmpCreateRegistryRoot(VOID
)
1074 UNICODE_STRING KeyName
;
1075 OBJECT_ATTRIBUTES ObjectAttributes
;
1076 PCM_KEY_BODY RootKey
;
1077 HCELL_INDEX RootIndex
;
1079 PCM_KEY_NODE KeyCell
;
1080 PSECURITY_DESCRIPTOR SecurityDescriptor
;
1081 PCM_KEY_CONTROL_BLOCK Kcb
;
1084 /* Setup the root node */
1085 if (!CmpCreateRootNode(&CmiVolatileHive
->Hive
, L
"REGISTRY", &RootIndex
))
1091 /* Create '\Registry' key. */
1092 RtlInitUnicodeString(&KeyName
, L
"\\REGISTRY");
1093 SecurityDescriptor
= CmpHiveRootSecurityDescriptor();
1094 InitializeObjectAttributes(&ObjectAttributes
,
1096 OBJ_CASE_INSENSITIVE
,
1098 SecurityDescriptor
);
1099 Status
= ObCreateObject(KernelMode
,
1104 sizeof(CM_KEY_BODY
),
1108 ExFreePoolWithTag(SecurityDescriptor
, TAG_CMSD
);
1109 if (!NT_SUCCESS(Status
)) return FALSE
;
1111 /* Sanity check, and get the key cell */
1112 ASSERT((&CmiVolatileHive
->Hive
)->ReleaseCellRoutine
== NULL
);
1113 KeyCell
= (PCM_KEY_NODE
)HvGetCell(&CmiVolatileHive
->Hive
, RootIndex
);
1114 if (!KeyCell
) return FALSE
;
1116 /* Create the KCB */
1117 RtlInitUnicodeString(&KeyName
, L
"\\REGISTRY");
1118 Kcb
= CmpCreateKeyControlBlock(&CmiVolatileHive
->Hive
,
1126 ObDereferenceObject(RootKey
);
1130 /* Initialize the object */
1131 RootKey
->KeyControlBlock
= Kcb
;
1132 RootKey
->Type
= CM_KEY_BODY_TYPE
;
1133 RootKey
->NotifyBlock
= NULL
;
1134 RootKey
->ProcessID
= PsGetCurrentProcessId();
1137 EnlistKeyBodyWithKCB(RootKey
, 0);
1139 /* Insert the key into the namespace */
1140 Status
= ObInsertObject(RootKey
,
1145 &CmpRegistryRootHandle
);
1146 if (!NT_SUCCESS(Status
))
1148 ObDereferenceObject(RootKey
);
1152 /* Reference the key again so that we never lose it */
1153 Status
= ObReferenceObjectByHandle(CmpRegistryRootHandle
,
1159 if (!NT_SUCCESS(Status
))
1161 ObDereferenceObject(RootKey
);
1165 /* Completely sucessful */
1170 CmpGetRegistryPath(OUT PWCHAR ConfigPath
)
1172 /* Just use default path */
1173 wcscpy(ConfigPath
, L
"\\SystemRoot");
1175 /* Check if we are booted in setup */
1176 if (!ExpInTextModeSetup
)
1178 /* Add registry path */
1180 ResultSize
= wcslen(ConfigPath
);
1181 if (ResultSize
&& ConfigPath
[ResultSize
- 1] == L
'\\')
1182 ConfigPath
[ResultSize
- 1] = UNICODE_NULL
;
1184 wcscat(ConfigPath
, L
"\\System32\\Config\\");
1188 wcscat(ConfigPath
, L
"\\");
1191 DPRINT1("CmpGetRegistryPath: ConfigPath = '%S'\n", ConfigPath
);
1194 return STATUS_SUCCESS
;
1197 _Function_class_(KSTART_ROUTINE
)
1200 CmpLoadHiveThread(IN PVOID StartContext
)
1202 WCHAR FileBuffer
[MAX_PATH
], RegBuffer
[MAX_PATH
], ConfigPath
[MAX_PATH
];
1203 UNICODE_STRING TempName
, FileName
, RegName
;
1204 ULONG i
, ErrorResponse
, WorkerCount
, Length
;
1206 ULONG PrimaryDisposition
, SecondaryDisposition
, ClusterSize
;
1208 HANDLE PrimaryHandle
= NULL
, LogHandle
= NULL
;
1209 NTSTATUS Status
= STATUS_SUCCESS
;
1210 PVOID ErrorParameters
;
1213 /* Get the hive index, make sure it makes sense */
1214 i
= PtrToUlong(StartContext
);
1215 ASSERT(CmpMachineHiveList
[i
].Name
!= NULL
);
1217 /* We were started */
1218 CmpMachineHiveList
[i
].ThreadStarted
= TRUE
;
1220 /* Build the file name and registry name strings */
1221 RtlInitEmptyUnicodeString(&FileName
, FileBuffer
, sizeof(FileBuffer
));
1222 RtlInitEmptyUnicodeString(&RegName
, RegBuffer
, sizeof(RegBuffer
));
1224 /* Now build the system root path */
1225 CmpGetRegistryPath(ConfigPath
);
1226 RtlInitUnicodeString(&TempName
, ConfigPath
);
1227 RtlAppendUnicodeStringToString(&FileName
, &TempName
);
1228 FileStart
= FileName
.Length
;
1230 /* And build the registry root path */
1231 RtlInitUnicodeString(&TempName
, L
"\\REGISTRY\\");
1232 RtlAppendUnicodeStringToString(&RegName
, &TempName
);
1234 /* Build the base name */
1235 RtlInitUnicodeString(&TempName
, CmpMachineHiveList
[i
].BaseName
);
1236 RtlAppendUnicodeStringToString(&RegName
, &TempName
);
1238 /* Check if this is a child of the root */
1239 if (RegName
.Buffer
[RegName
.Length
/ sizeof(WCHAR
) - 1] == OBJ_NAME_PATH_SEPARATOR
)
1241 /* Then setup the whole name */
1242 RtlInitUnicodeString(&TempName
, CmpMachineHiveList
[i
].Name
);
1243 RtlAppendUnicodeStringToString(&RegName
, &TempName
);
1246 /* Now add the rest of the file name */
1247 RtlInitUnicodeString(&TempName
, CmpMachineHiveList
[i
].Name
);
1248 FileName
.Length
= FileStart
;
1249 RtlAppendUnicodeStringToString(&FileName
, &TempName
);
1250 if (!CmpMachineHiveList
[i
].CmHive
)
1252 /* We need to allocate a new hive structure */
1253 CmpMachineHiveList
[i
].Allocate
= TRUE
;
1255 /* Load the hive file */
1256 Status
= CmpInitHiveFromFile(&FileName
,
1257 CmpMachineHiveList
[i
].HHiveFlags
,
1259 &CmpMachineHiveList
[i
].Allocate
,
1261 if (!(NT_SUCCESS(Status
)) ||
1262 (!(CmpShareSystemHives
) && !(CmHive
->FileHandles
[HFILE_TYPE_LOG
])))
1265 * We failed, or could not get a log file (unless
1266 * the hive is shared), raise a hard error.
1268 ErrorParameters
= &FileName
;
1269 NtRaiseHardError(STATUS_CANNOT_LOAD_REGISTRY_FILE
,
1272 (PULONG_PTR
)&ErrorParameters
,
1277 /* Set the hive flags and newly allocated hive pointer */
1278 CmHive
->Flags
= CmpMachineHiveList
[i
].CmHiveFlags
;
1279 CmpMachineHiveList
[i
].CmHive2
= CmHive
;
1283 /* We already have a hive, is it volatile? */
1284 CmHive
= CmpMachineHiveList
[i
].CmHive
;
1285 if (!(CmHive
->Hive
.HiveFlags
& HIVE_VOLATILE
))
1287 /* It's now, open the hive file and log */
1288 Status
= CmpOpenHiveFiles(&FileName
,
1292 &PrimaryDisposition
,
1293 &SecondaryDisposition
,
1298 if (!(NT_SUCCESS(Status
)) || !(LogHandle
))
1300 /* Couldn't open the hive or its log file, raise a hard error */
1301 ErrorParameters
= &FileName
;
1302 NtRaiseHardError(STATUS_CANNOT_LOAD_REGISTRY_FILE
,
1305 (PULONG_PTR
)&ErrorParameters
,
1309 /* And bugcheck for posterity's sake */
1310 KeBugCheckEx(BAD_SYSTEM_CONFIG_INFO
, 9, 0, i
, Status
);
1313 /* Save the file handles. This should remove our sync hacks */
1314 CmHive
->FileHandles
[HFILE_TYPE_LOG
] = LogHandle
;
1315 CmHive
->FileHandles
[HFILE_TYPE_PRIMARY
] = PrimaryHandle
;
1317 /* Allow lazy flushing since the handles are there -- remove sync hacks */
1318 //ASSERT(CmHive->Hive.HiveFlags & HIVE_NOLAZYFLUSH);
1319 CmHive
->Hive
.HiveFlags
&= ~HIVE_NOLAZYFLUSH
;
1321 /* Get the real size of the hive */
1322 Length
= CmHive
->Hive
.Storage
[Stable
].Length
+ HBLOCK_SIZE
;
1324 /* Check if the cluster size doesn't match */
1325 if (CmHive
->Hive
.Cluster
!= ClusterSize
)
1327 DPRINT1("FIXME: Support for CmHive->Hive.Cluster (%lu) != ClusterSize (%lu) is unimplemented!\n",
1328 CmHive
->Hive
.Cluster
, ClusterSize
);
1331 /* Set the file size */
1332 DPRINT("FIXME: Should set file size: %lu\n", Length
);
1333 //if (!CmpFileSetSize((PHHIVE)CmHive, HFILE_TYPE_PRIMARY, Length, Length))
1335 /* This shouldn't fail */
1339 /* Another thing we don't support is NTLDR-recovery */
1340 if (CmHive
->Hive
.BaseBlock
->BootRecover
) ASSERT(FALSE
);
1342 /* Finally, set our allocated hive to the same hive we've had */
1343 CmpMachineHiveList
[i
].CmHive2
= CmHive
;
1344 ASSERT(CmpMachineHiveList
[i
].CmHive
== CmpMachineHiveList
[i
].CmHive2
);
1349 CmpMachineHiveList
[i
].ThreadFinished
= TRUE
;
1351 /* Check if we're the last worker */
1352 WorkerCount
= InterlockedIncrement(&CmpLoadWorkerIncrement
);
1353 if (WorkerCount
== CM_NUMBER_OF_MACHINE_HIVES
)
1355 /* Signal the event */
1356 KeSetEvent(&CmpLoadWorkerEvent
, 0, FALSE
);
1359 /* Kill the thread */
1360 PsTerminateSystemThread(Status
);
1365 CmpInitializeHiveList(VOID
)
1367 WCHAR FileBuffer
[MAX_PATH
], RegBuffer
[MAX_PATH
], ConfigPath
[MAX_PATH
];
1368 UNICODE_STRING TempName
, FileName
, RegName
;
1373 PSECURITY_DESCRIPTOR SecurityDescriptor
;
1376 /* Allow writing for now */
1379 /* Build the file name and registry name strings */
1380 RtlInitEmptyUnicodeString(&FileName
, FileBuffer
, sizeof(FileBuffer
));
1381 RtlInitEmptyUnicodeString(&RegName
, RegBuffer
, sizeof(RegBuffer
));
1383 /* Now build the system root path */
1384 CmpGetRegistryPath(ConfigPath
);
1385 RtlInitUnicodeString(&TempName
, ConfigPath
);
1386 RtlAppendUnicodeStringToString(&FileName
, &TempName
);
1388 /* And build the registry root path */
1389 RtlInitUnicodeString(&TempName
, L
"\\REGISTRY\\");
1390 RtlAppendUnicodeStringToString(&RegName
, &TempName
);
1391 RegStart
= RegName
.Length
;
1393 /* Setup the event to synchronize workers */
1394 KeInitializeEvent(&CmpLoadWorkerEvent
, SynchronizationEvent
, FALSE
);
1396 /* Enter special boot condition */
1397 CmpSpecialBootCondition
= TRUE
;
1399 /* Create the SD for the root hives */
1400 SecurityDescriptor
= CmpHiveRootSecurityDescriptor();
1402 /* Loop every hive we care about */
1403 for (i
= 0; i
< CM_NUMBER_OF_MACHINE_HIVES
; i
++)
1405 /* Make sure the list is set up */
1406 ASSERT(CmpMachineHiveList
[i
].Name
!= NULL
);
1408 /* Load the hive as volatile, if in LiveCD mode */
1409 if (CmpShareSystemHives
)
1410 CmpMachineHiveList
[i
].HHiveFlags
|= HIVE_VOLATILE
;
1412 /* Create a thread to handle this hive */
1413 Status
= PsCreateSystemThread(&Thread
,
1420 if (NT_SUCCESS(Status
))
1422 /* We don't care about the handle -- the thread self-terminates */
1427 /* Can't imagine this happening */
1428 KeBugCheckEx(BAD_SYSTEM_CONFIG_INFO
, 9, 3, i
, Status
);
1432 /* Make sure we've reached the end of the list */
1433 ASSERT(CmpMachineHiveList
[i
].Name
== NULL
);
1435 /* Wait for hive loading to finish */
1436 KeWaitForSingleObject(&CmpLoadWorkerEvent
,
1442 /* Exit the special boot condition and make sure all workers completed */
1443 CmpSpecialBootCondition
= FALSE
;
1444 ASSERT(CmpLoadWorkerIncrement
== CM_NUMBER_OF_MACHINE_HIVES
);
1446 /* Loop hives again */
1447 for (i
= 0; i
< CM_NUMBER_OF_MACHINE_HIVES
; i
++)
1449 /* Make sure the thread ran and finished */
1450 ASSERT(CmpMachineHiveList
[i
].ThreadFinished
== TRUE
);
1451 ASSERT(CmpMachineHiveList
[i
].ThreadStarted
== TRUE
);
1453 /* Check if this was a new hive */
1454 if (!CmpMachineHiveList
[i
].CmHive
)
1456 /* Make sure we allocated something */
1457 ASSERT(CmpMachineHiveList
[i
].CmHive2
!= NULL
);
1459 /* Build the base name */
1460 RegName
.Length
= RegStart
;
1461 RtlInitUnicodeString(&TempName
, CmpMachineHiveList
[i
].BaseName
);
1462 RtlAppendUnicodeStringToString(&RegName
, &TempName
);
1464 /* Check if this is a child of the root */
1465 if (RegName
.Buffer
[RegName
.Length
/ sizeof(WCHAR
) - 1] == OBJ_NAME_PATH_SEPARATOR
)
1467 /* Then setup the whole name */
1468 RtlInitUnicodeString(&TempName
, CmpMachineHiveList
[i
].Name
);
1469 RtlAppendUnicodeStringToString(&RegName
, &TempName
);
1472 /* Now link the hive to its master */
1473 Status
= CmpLinkHiveToMaster(&RegName
,
1475 CmpMachineHiveList
[i
].CmHive2
,
1476 CmpMachineHiveList
[i
].Allocate
,
1477 SecurityDescriptor
);
1478 if (Status
!= STATUS_SUCCESS
)
1480 /* Linking needs to work */
1481 KeBugCheckEx(CONFIG_LIST_FAILED
, 11, Status
, i
, (ULONG_PTR
)&RegName
);
1484 /* Check if we had to allocate a new hive */
1485 if (CmpMachineHiveList
[i
].Allocate
)
1487 /* Sync the new hive */
1488 //HvSyncHive((PHHIVE)(CmpMachineHiveList[i].CmHive2));
1492 /* Check if we created a new hive */
1493 if (CmpMachineHiveList
[i
].CmHive2
)
1495 /* Add to HiveList key */
1496 CmpAddToHiveFileList(CmpMachineHiveList
[i
].CmHive2
);
1500 /* Get rid of the SD */
1501 ExFreePoolWithTag(SecurityDescriptor
, TAG_CMSD
);
1503 /* Link SECURITY to SAM */
1504 CmpLinkKeyToHive(L
"\\Registry\\Machine\\Security\\SAM",
1505 L
"\\Registry\\Machine\\SAM\\SAM");
1507 /* Link S-1-5-18 to .Default */
1508 CmpNoVolatileCreates
= FALSE
;
1509 CmpLinkKeyToHive(L
"\\Registry\\User\\S-1-5-18",
1510 L
"\\Registry\\User\\.Default");
1511 CmpNoVolatileCreates
= TRUE
;
1519 OBJECT_ATTRIBUTES ObjectAttributes
;
1520 UNICODE_STRING KeyName
;
1523 PCMHIVE HardwareHive
;
1524 PSECURITY_DESCRIPTOR SecurityDescriptor
;
1527 /* Check if this is PE-boot */
1528 if (InitIsWinPEMode
)
1530 /* Set registry to PE mode */
1531 CmpMiniNTBoot
= TRUE
;
1532 CmpShareSystemHives
= TRUE
;
1535 /* Initialize the hive list and lock */
1536 InitializeListHead(&CmpHiveListHead
);
1537 ExInitializePushLock(&CmpHiveListHeadLock
);
1538 ExInitializePushLock(&CmpLoadHiveLock
);
1540 /* Initialize registry lock */
1541 ExInitializeResourceLite(&CmpRegistryLock
);
1543 /* Initialize the cache */
1544 CmpInitializeCache();
1546 /* Initialize allocation and delayed dereferencing */
1547 CmpInitCmPrivateAlloc();
1548 CmpInitCmPrivateDelayAlloc();
1549 CmpInitDelayDerefKCBEngine();
1551 /* Initialize callbacks */
1554 /* Initialize self healing */
1555 KeInitializeGuardedMutex(&CmpSelfHealQueueLock
);
1556 InitializeListHead(&CmpSelfHealQueueListHead
);
1558 /* Save the current process and lock the registry */
1559 CmpSystemProcess
= PsGetCurrentProcess();
1561 /* Create the key object types */
1562 Status
= CmpCreateObjectTypes();
1563 if (!NT_SUCCESS(Status
))
1566 KeBugCheckEx(CONFIG_INITIALIZATION_FAILED
, 1, 1, Status
, 0);
1569 /* Build the master hive */
1570 Status
= CmpInitializeHive(&CmiVolatileHive
,
1580 if (!NT_SUCCESS(Status
))
1583 KeBugCheckEx(CONFIG_INITIALIZATION_FAILED
, 1, 2, Status
, 0);
1586 /* Create the \REGISTRY key node */
1587 if (!CmpCreateRegistryRoot())
1590 KeBugCheckEx(CONFIG_INITIALIZATION_FAILED
, 1, 3, 0, 0);
1593 /* Create the default security descriptor */
1594 SecurityDescriptor
= CmpHiveRootSecurityDescriptor();
1596 /* Create '\Registry\Machine' key */
1597 RtlInitUnicodeString(&KeyName
, L
"\\REGISTRY\\MACHINE");
1598 InitializeObjectAttributes(&ObjectAttributes
,
1600 OBJ_CASE_INSENSITIVE
,
1602 SecurityDescriptor
);
1603 Status
= NtCreateKey(&KeyHandle
,
1604 KEY_READ
| KEY_WRITE
,
1610 if (!NT_SUCCESS(Status
))
1613 KeBugCheckEx(CONFIG_INITIALIZATION_FAILED
, 1, 5, Status
, 0);
1616 /* Close the handle */
1619 /* Create '\Registry\User' key */
1620 RtlInitUnicodeString(&KeyName
, L
"\\REGISTRY\\USER");
1621 InitializeObjectAttributes(&ObjectAttributes
,
1623 OBJ_CASE_INSENSITIVE
,
1625 SecurityDescriptor
);
1626 Status
= NtCreateKey(&KeyHandle
,
1627 KEY_READ
| KEY_WRITE
,
1633 if (!NT_SUCCESS(Status
))
1636 KeBugCheckEx(CONFIG_INITIALIZATION_FAILED
, 1, 6, Status
, 0);
1639 /* Close the handle */
1642 /* After this point, do not allow creating keys in the master hive */
1643 CmpNoVolatileCreates
= TRUE
;
1645 /* Initialize the system hive */
1646 if (!CmpInitializeSystemHive(KeLoaderBlock
))
1649 KeBugCheckEx(CONFIG_INITIALIZATION_FAILED
, 1, 7, 0, 0);
1652 /* Create the 'CurrentControlSet' link */
1653 Status
= CmpCreateControlSet(KeLoaderBlock
);
1654 if (!NT_SUCCESS(Status
))
1657 KeBugCheckEx(CONFIG_INITIALIZATION_FAILED
, 1, 8, Status
, 0);
1660 /* Create the hardware hive */
1661 Status
= CmpInitializeHive(&HardwareHive
,
1671 if (!NT_SUCCESS(Status
))
1674 KeBugCheckEx(CONFIG_INITIALIZATION_FAILED
, 1, 11, Status
, 0);
1677 /* Add the hive to the hive list */
1678 CmpMachineHiveList
[0].CmHive
= HardwareHive
;
1680 /* Attach it to the machine key */
1681 RtlInitUnicodeString(&KeyName
, L
"\\Registry\\Machine\\HARDWARE");
1682 Status
= CmpLinkHiveToMaster(&KeyName
,
1686 SecurityDescriptor
);
1687 if (!NT_SUCCESS(Status
))
1690 KeBugCheckEx(CONFIG_INITIALIZATION_FAILED
, 1, 12, Status
, 0);
1693 /* Add to HiveList key */
1694 CmpAddToHiveFileList(HardwareHive
);
1696 /* Free the security descriptor */
1697 ExFreePoolWithTag(SecurityDescriptor
, TAG_CMSD
);
1699 /* Fill out the Hardware key with the ARC Data from the Loader */
1700 Status
= CmpInitializeHardwareConfiguration(KeLoaderBlock
);
1701 if (!NT_SUCCESS(Status
))
1704 KeBugCheckEx(CONFIG_INITIALIZATION_FAILED
, 1, 13, Status
, 0);
1707 /* Initialize machine-dependent information into the registry */
1708 Status
= CmpInitializeMachineDependentConfiguration(KeLoaderBlock
);
1709 if (!NT_SUCCESS(Status
))
1712 KeBugCheckEx(CONFIG_INITIALIZATION_FAILED
, 1, 14, Status
, 0);
1715 /* Initialize volatile registry settings */
1716 Status
= CmpSetSystemValues(KeLoaderBlock
);
1717 if (!NT_SUCCESS(Status
))
1720 KeBugCheckEx(CONFIG_INITIALIZATION_FAILED
, 1, 15, Status
, 0);
1723 /* Free the load options */
1724 ExFreePoolWithTag(CmpLoadOptions
.Buffer
, TAG_CM
);
1726 /* If we got here, all went well */
1733 CmpFreeDriverList(IN PHHIVE Hive
,
1734 IN PLIST_ENTRY DriverList
)
1736 PLIST_ENTRY NextEntry
, OldEntry
;
1737 PBOOT_DRIVER_NODE DriverNode
;
1740 /* Parse the current list */
1741 NextEntry
= DriverList
->Flink
;
1742 while (NextEntry
!= DriverList
)
1744 /* Get the driver node */
1745 DriverNode
= CONTAINING_RECORD(NextEntry
, BOOT_DRIVER_NODE
, ListEntry
.Link
);
1747 /* Get the next entry now, since we're going to free it later */
1748 OldEntry
= NextEntry
;
1749 NextEntry
= NextEntry
->Flink
;
1751 /* Was there a name? */
1752 if (DriverNode
->Name
.Buffer
)
1755 CmpFree(DriverNode
->Name
.Buffer
, DriverNode
->Name
.Length
);
1758 /* Was there a registry path? */
1759 if (DriverNode
->ListEntry
.RegistryPath
.Buffer
)
1762 CmpFree(DriverNode
->ListEntry
.RegistryPath
.Buffer
,
1763 DriverNode
->ListEntry
.RegistryPath
.MaximumLength
);
1766 /* Was there a file path? */
1767 if (DriverNode
->ListEntry
.FilePath
.Buffer
)
1770 CmpFree(DriverNode
->ListEntry
.FilePath
.Buffer
,
1771 DriverNode
->ListEntry
.FilePath
.MaximumLength
);
1774 /* Now free the node, and move on */
1775 CmpFree(OldEntry
, sizeof(BOOT_DRIVER_NODE
));
1782 CmGetSystemDriverList(VOID
)
1784 LIST_ENTRY DriverList
;
1785 OBJECT_ATTRIBUTES ObjectAttributes
;
1787 PCM_KEY_BODY KeyBody
;
1789 HCELL_INDEX RootCell
, ControlCell
;
1791 UNICODE_STRING KeyName
;
1792 PLIST_ENTRY NextEntry
;
1794 PUNICODE_STRING
* ServicePath
= NULL
;
1795 BOOLEAN Success
, AutoSelect
;
1796 PBOOT_DRIVER_LIST_ENTRY DriverEntry
;
1799 /* Initialize the driver list */
1800 InitializeListHead(&DriverList
);
1802 /* Open the system hive key */
1803 RtlInitUnicodeString(&KeyName
, L
"\\Registry\\Machine\\System");
1804 InitializeObjectAttributes(&ObjectAttributes
,
1806 OBJ_CASE_INSENSITIVE
,
1809 Status
= NtOpenKey(&KeyHandle
, KEY_READ
, &ObjectAttributes
);
1810 if (!NT_SUCCESS(Status
)) return NULL
;
1812 /* Reference the key object to get the root hive/cell to access directly */
1813 Status
= ObReferenceObjectByHandle(KeyHandle
,
1819 if (!NT_SUCCESS(Status
))
1826 /* Do all this under the registry lock */
1827 CmpLockRegistryExclusive();
1829 /* Get the hive and key cell */
1830 Hive
= KeyBody
->KeyControlBlock
->KeyHive
;
1831 RootCell
= KeyBody
->KeyControlBlock
->KeyCell
;
1833 /* Open the current control set key */
1834 RtlInitUnicodeString(&KeyName
, L
"Current");
1835 ControlCell
= CmpFindControlSet(Hive
, RootCell
, &KeyName
, &AutoSelect
);
1836 if (ControlCell
== HCELL_NIL
) goto EndPath
;
1838 /* Find all system drivers */
1839 Success
= CmpFindDrivers(Hive
, ControlCell
, SystemLoad
, NULL
, &DriverList
);
1840 if (!Success
) goto EndPath
;
1842 /* Sort by group/tag */
1843 if (!CmpSortDriverList(Hive
, ControlCell
, &DriverList
)) goto EndPath
;
1845 /* Remove circular dependencies (cycles) and sort */
1846 if (!CmpResolveDriverDependencies(&DriverList
)) goto EndPath
;
1848 /* Loop the list to count drivers */
1849 for (i
= 0, NextEntry
= DriverList
.Flink
;
1850 NextEntry
!= &DriverList
;
1851 i
++, NextEntry
= NextEntry
->Flink
);
1853 /* Allocate the array */
1854 ServicePath
= ExAllocatePool(NonPagedPool
, (i
+ 1) * sizeof(PUNICODE_STRING
));
1855 if (!ServicePath
) KeBugCheckEx(CONFIG_INITIALIZATION_FAILED
, 2, 1, 0, 0);
1857 /* Loop the driver list */
1858 for (i
= 0, NextEntry
= DriverList
.Flink
;
1859 NextEntry
!= &DriverList
;
1860 i
++, NextEntry
= NextEntry
->Flink
)
1863 DriverEntry
= CONTAINING_RECORD(NextEntry
, BOOT_DRIVER_LIST_ENTRY
, Link
);
1865 /* Allocate the path for the caller */
1866 ServicePath
[i
] = ExAllocatePool(NonPagedPool
, sizeof(UNICODE_STRING
));
1867 if (!ServicePath
[i
])
1869 KeBugCheckEx(CONFIG_INITIALIZATION_FAILED
, 2, 1, 0, 0);
1872 /* Duplicate the registry path */
1873 Status
= RtlDuplicateUnicodeString(RTL_DUPLICATE_UNICODE_STRING_NULL_TERMINATE
,
1874 &DriverEntry
->RegistryPath
,
1876 if (!NT_SUCCESS(Status
))
1878 KeBugCheckEx(CONFIG_INITIALIZATION_FAILED
, 2, 1, 0, 0);
1882 /* Terminate the list */
1883 ServicePath
[i
] = NULL
;
1886 /* Free the driver list if we had one */
1887 if (!IsListEmpty(&DriverList
)) CmpFreeDriverList(Hive
, &DriverList
);
1889 /* Unlock the registry */
1890 CmpUnlockRegistry();
1892 /* Close the key handle and dereference the object, then return the path */
1893 ObDereferenceObject(KeyBody
);
1900 CmpLockRegistryExclusive(VOID
)
1902 /* Enter a critical region and lock the registry */
1903 KeEnterCriticalRegion();
1904 ExAcquireResourceExclusiveLite(&CmpRegistryLock
, TRUE
);
1907 ASSERT(CmpFlushStarveWriters
== 0);
1908 RtlGetCallersAddress(&CmpRegistryLockCaller
, &CmpRegistryLockCallerCaller
);
1913 CmpLockRegistry(VOID
)
1915 /* Enter a critical region */
1916 KeEnterCriticalRegion();
1918 /* Check if we have to starve writers */
1919 if (CmpFlushStarveWriters
)
1921 /* Starve exlusive waiters */
1922 ExAcquireSharedStarveExclusive(&CmpRegistryLock
, TRUE
);
1926 /* Just grab the lock */
1927 ExAcquireResourceSharedLite(&CmpRegistryLock
, TRUE
);
1933 CmpTestRegistryLock(VOID
)
1936 return !ExIsResourceAcquiredSharedLite(&CmpRegistryLock
) ? FALSE
: TRUE
;
1941 CmpTestRegistryLockExclusive(VOID
)
1944 return !ExIsResourceAcquiredExclusiveLite(&CmpRegistryLock
) ? FALSE
: TRUE
;
1949 CmpLockHiveFlusherExclusive(IN PCMHIVE Hive
)
1951 /* Lock the flusher. We should already be in a critical section */
1952 CMP_ASSERT_REGISTRY_LOCK_OR_LOADING(Hive
);
1953 ASSERT((ExIsResourceAcquiredShared(Hive
->FlusherLock
) == 0) &&
1954 (ExIsResourceAcquiredExclusiveLite(Hive
->FlusherLock
) == 0));
1955 ExAcquireResourceExclusiveLite(Hive
->FlusherLock
, TRUE
);
1960 CmpLockHiveFlusherShared(IN PCMHIVE Hive
)
1962 /* Lock the flusher. We should already be in a critical section */
1963 CMP_ASSERT_REGISTRY_LOCK_OR_LOADING(Hive
);
1964 ASSERT((ExIsResourceAcquiredShared(Hive
->FlusherLock
) == 0) &&
1965 (ExIsResourceAcquiredExclusiveLite(Hive
->FlusherLock
) == 0));
1966 ExAcquireResourceSharedLite(Hive
->FlusherLock
, TRUE
);
1971 CmpUnlockHiveFlusher(IN PCMHIVE Hive
)
1974 CMP_ASSERT_REGISTRY_LOCK_OR_LOADING(Hive
);
1975 CMP_ASSERT_FLUSH_LOCK(Hive
);
1977 /* Release the lock */
1978 ExReleaseResourceLite(Hive
->FlusherLock
);
1983 CmpTestHiveFlusherLockShared(IN PCMHIVE Hive
)
1986 return !ExIsResourceAcquiredSharedLite(Hive
->FlusherLock
) ? FALSE
: TRUE
;
1991 CmpTestHiveFlusherLockExclusive(IN PCMHIVE Hive
)
1994 return !ExIsResourceAcquiredExclusiveLite(Hive
->FlusherLock
) ? FALSE
: TRUE
;
1999 CmpUnlockRegistry(VOID
)
2002 CMP_ASSERT_REGISTRY_LOCK();
2004 /* Check if we should flush the registry */
2005 if (CmpFlushOnLockRelease
)
2007 /* The registry should be exclusively locked for this */
2008 CMP_ASSERT_EXCLUSIVE_REGISTRY_LOCK();
2010 /* Flush the registry */
2011 CmpDoFlushAll(TRUE
);
2012 CmpFlushOnLockRelease
= FALSE
;
2016 /* Lazy flush the registry */
2020 /* Release the lock and leave the critical region */
2021 ExReleaseResourceLite(&CmpRegistryLock
);
2022 KeLeaveCriticalRegion();
2027 CmpAcquireTwoKcbLocksExclusiveByKey(IN ULONG ConvKey1
,
2030 ULONG Index1
, Index2
;
2033 CMP_ASSERT_REGISTRY_LOCK();
2035 /* Get hash indexes */
2036 Index1
= GET_HASH_INDEX(ConvKey1
);
2037 Index2
= GET_HASH_INDEX(ConvKey2
);
2039 /* See which one is highest */
2040 if (Index1
< Index2
)
2042 /* Grab them in the proper order */
2043 CmpAcquireKcbLockExclusiveByKey(ConvKey1
);
2044 CmpAcquireKcbLockExclusiveByKey(ConvKey2
);
2048 /* Grab the second one first, then the first */
2049 CmpAcquireKcbLockExclusiveByKey(ConvKey2
);
2050 if (Index1
!= Index2
) CmpAcquireKcbLockExclusiveByKey(ConvKey1
);
2056 CmpReleaseTwoKcbLockByKey(IN ULONG ConvKey1
,
2059 ULONG Index1
, Index2
;
2062 CMP_ASSERT_REGISTRY_LOCK();
2064 /* Get hash indexes */
2065 Index1
= GET_HASH_INDEX(ConvKey1
);
2066 Index2
= GET_HASH_INDEX(ConvKey2
);
2067 ASSERT((GET_HASH_ENTRY(CmpCacheTable
, ConvKey2
)->Owner
== KeGetCurrentThread()) ||
2068 (CmpTestRegistryLockExclusive()));
2070 /* See which one is highest */
2071 if (Index1
< Index2
)
2073 /* Grab them in the proper order */
2074 ASSERT((GET_HASH_ENTRY(CmpCacheTable
, ConvKey1
)->Owner
== KeGetCurrentThread()) ||
2075 (CmpTestRegistryLockExclusive()));
2076 CmpReleaseKcbLockByKey(ConvKey2
);
2077 CmpReleaseKcbLockByKey(ConvKey1
);
2081 /* Release the first one first, then the second */
2082 if (Index1
!= Index2
)
2084 ASSERT((GET_HASH_ENTRY(CmpCacheTable
, ConvKey1
)->Owner
== KeGetCurrentThread()) ||
2085 (CmpTestRegistryLockExclusive()));
2086 CmpReleaseKcbLockByKey(ConvKey1
);
2088 CmpReleaseKcbLockByKey(ConvKey2
);
2094 CmShutdownSystem(VOID
)
2096 PLIST_ENTRY ListEntry
;
2099 /* Kill the workers */
2100 if (!CmFirstTime
) CmpShutdownWorkers();
2102 /* Flush all hives */
2103 CmpLockRegistryExclusive();
2104 CmpDoFlushAll(TRUE
);
2106 /* Close all hive files */
2107 ListEntry
= CmpHiveListHead
.Flink
;
2108 while (ListEntry
!= &CmpHiveListHead
)
2110 Hive
= CONTAINING_RECORD(ListEntry
, CMHIVE
, HiveList
);
2112 CmpCloseHiveFiles(Hive
);
2114 ListEntry
= ListEntry
->Flink
;
2117 CmpUnlockRegistry();
2122 CmpSetVersionData(VOID
)
2125 OBJECT_ATTRIBUTES ObjectAttributes
;
2126 UNICODE_STRING KeyName
;
2127 UNICODE_STRING ValueName
;
2128 UNICODE_STRING ValueData
;
2129 ANSI_STRING TempString
;
2130 HANDLE SoftwareKeyHandle
= NULL
;
2131 HANDLE MicrosoftKeyHandle
= NULL
;
2132 HANDLE WindowsNtKeyHandle
= NULL
;
2133 HANDLE CurrentVersionKeyHandle
= NULL
;
2134 WCHAR Buffer
[128]; // Buffer large enough to contain a full ULONG in decimal
2135 // representation, and the full 'CurrentType' string.
2138 * Open the 'HKLM\Software\Microsoft\Windows NT\CurrentVersion' key
2139 * (create the intermediate subkeys if needed).
2142 RtlInitUnicodeString(&KeyName
, L
"\\REGISTRY\\MACHINE\\SOFTWARE");
2143 InitializeObjectAttributes(&ObjectAttributes
,
2145 OBJ_CASE_INSENSITIVE
| OBJ_KERNEL_HANDLE
,
2148 Status
= NtCreateKey(&SoftwareKeyHandle
,
2155 if (!NT_SUCCESS(Status
))
2157 DPRINT1("Failed to create key %wZ (Status: %08lx)\n", &KeyName
, Status
);
2161 RtlInitUnicodeString(&KeyName
, L
"Microsoft");
2162 InitializeObjectAttributes(&ObjectAttributes
,
2164 OBJ_CASE_INSENSITIVE
| OBJ_KERNEL_HANDLE
,
2167 Status
= NtCreateKey(&MicrosoftKeyHandle
,
2174 if (!NT_SUCCESS(Status
))
2176 DPRINT1("Failed to create key %wZ (Status: %08lx)\n", &KeyName
, Status
);
2180 RtlInitUnicodeString(&KeyName
, L
"Windows NT");
2181 InitializeObjectAttributes(&ObjectAttributes
,
2183 OBJ_CASE_INSENSITIVE
| OBJ_KERNEL_HANDLE
,
2186 Status
= NtCreateKey(&WindowsNtKeyHandle
,
2193 if (!NT_SUCCESS(Status
))
2195 DPRINT1("Failed to create key %wZ (Status: %08lx)\n", &KeyName
, Status
);
2199 RtlInitUnicodeString(&KeyName
, L
"CurrentVersion");
2200 InitializeObjectAttributes(&ObjectAttributes
,
2202 OBJ_CASE_INSENSITIVE
| OBJ_KERNEL_HANDLE
,
2205 Status
= NtCreateKey(&CurrentVersionKeyHandle
,
2206 KEY_CREATE_SUB_KEY
| KEY_SET_VALUE
,
2212 if (!NT_SUCCESS(Status
))
2214 DPRINT1("Failed to create key %wZ (Status: %08lx)\n", &KeyName
, Status
);
2218 /* Set the 'CurrentVersion' value */
2219 RtlInitUnicodeString(&ValueName
, L
"CurrentVersion");
2220 NtSetValueKey(CurrentVersionKeyHandle
,
2224 CmVersionString
.Buffer
,
2225 CmVersionString
.Length
+ sizeof(WCHAR
));
2227 /* Set the 'CurrentBuildNumber' value */
2228 RtlInitUnicodeString(&ValueName
, L
"CurrentBuildNumber");
2229 RtlInitEmptyUnicodeString(&ValueData
, Buffer
, sizeof(Buffer
));
2230 RtlIntegerToUnicodeString(NtBuildNumber
& 0xFFFF, 10, &ValueData
);
2231 NtSetValueKey(CurrentVersionKeyHandle
,
2236 ValueData
.Length
+ sizeof(WCHAR
));
2238 /* Set the 'BuildLab' value */
2239 RtlInitUnicodeString(&ValueName
, L
"BuildLab");
2240 RtlInitAnsiString(&TempString
, NtBuildLab
);
2241 Status
= RtlAnsiStringToUnicodeString(&ValueData
, &TempString
, FALSE
);
2242 if (NT_SUCCESS(Status
))
2244 NtSetValueKey(CurrentVersionKeyHandle
,
2249 ValueData
.Length
+ sizeof(WCHAR
));
2252 /* Set the 'CurrentType' value */
2253 RtlInitUnicodeString(&ValueName
, L
"CurrentType");
2254 RtlStringCbPrintfW(Buffer
, sizeof(Buffer
),
2268 RtlInitUnicodeString(&ValueData
, Buffer
);
2269 NtSetValueKey(CurrentVersionKeyHandle
,
2274 ValueData
.Length
+ sizeof(WCHAR
));
2276 /* Set the 'CSDVersion' value */
2277 RtlInitUnicodeString(&ValueName
, L
"CSDVersion");
2278 if (CmCSDVersionString
.Length
!= 0)
2280 NtSetValueKey(CurrentVersionKeyHandle
,
2284 CmCSDVersionString
.Buffer
,
2285 CmCSDVersionString
.Length
+ sizeof(WCHAR
));
2289 NtDeleteValueKey(CurrentVersionKeyHandle
, &ValueName
);
2292 /* Set the 'CSDBuildNumber' value */
2293 RtlInitUnicodeString(&ValueName
, L
"CSDBuildNumber");
2294 if (CmNtSpBuildNumber
!= 0)
2296 RtlInitEmptyUnicodeString(&ValueData
, Buffer
, sizeof(Buffer
));
2297 RtlIntegerToUnicodeString(CmNtSpBuildNumber
, 10, &ValueData
);
2298 NtSetValueKey(CurrentVersionKeyHandle
,
2303 ValueData
.Length
+ sizeof(WCHAR
));
2307 NtDeleteValueKey(CurrentVersionKeyHandle
, &ValueName
);
2310 /* Set the 'SystemRoot' value */
2311 RtlInitUnicodeString(&ValueName
, L
"SystemRoot");
2312 NtSetValueKey(CurrentVersionKeyHandle
,
2316 NtSystemRoot
.Buffer
,
2317 NtSystemRoot
.Length
+ sizeof(WCHAR
));
2320 /* Close the keys */
2321 if (CurrentVersionKeyHandle
!= NULL
)
2322 NtClose(CurrentVersionKeyHandle
);
2324 if (WindowsNtKeyHandle
!= NULL
)
2325 NtClose(WindowsNtKeyHandle
);
2327 if (MicrosoftKeyHandle
!= NULL
)
2328 NtClose(MicrosoftKeyHandle
);
2330 if (SoftwareKeyHandle
!= NULL
)
2331 NtClose(SoftwareKeyHandle
);