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 ULONG CmpTraceLevel
= 0;
34 extern LONG CmpFlushStarveWriters
;
35 extern BOOLEAN CmFirstTime
;
37 /* FUNCTIONS ******************************************************************/
41 CmpDeleteKeyObject(PVOID DeletedObject
)
43 PCM_KEY_BODY KeyBody
= (PCM_KEY_BODY
)DeletedObject
;
44 PCM_KEY_CONTROL_BLOCK Kcb
;
45 REG_KEY_HANDLE_CLOSE_INFORMATION KeyHandleCloseInfo
;
46 REG_POST_OPERATION_INFORMATION PostOperationInfo
;
50 /* First off, prepare the handle close information callback */
51 PostOperationInfo
.Object
= KeyBody
;
52 KeyHandleCloseInfo
.Object
= KeyBody
;
53 Status
= CmiCallRegisteredCallbacks(RegNtPreKeyHandleClose
,
55 if (!NT_SUCCESS(Status
))
57 /* If we failed, notify the post routine */
58 PostOperationInfo
.Status
= Status
;
59 CmiCallRegisteredCallbacks(RegNtPostKeyHandleClose
, &PostOperationInfo
);
63 /* Acquire hive lock */
66 /* Make sure this is a valid key body */
67 if (KeyBody
->Type
== '20yk')
70 Kcb
= KeyBody
->KeyControlBlock
;
74 DelistKeyBodyFromKCB(KeyBody
, FALSE
);
76 /* Dereference the KCB */
77 CmpDelayDerefKeyControlBlock(Kcb
);
81 /* Release the registry lock */
84 /* Do the post callback */
85 PostOperationInfo
.Status
= STATUS_SUCCESS
;
86 CmiCallRegisteredCallbacks(RegNtPostKeyHandleClose
, &PostOperationInfo
);
91 CmpCloseKeyObject(IN PEPROCESS Process OPTIONAL
,
93 IN ACCESS_MASK GrantedAccess
,
94 IN ULONG ProcessHandleCount
,
95 IN ULONG SystemHandleCount
)
97 PCM_KEY_BODY KeyBody
= (PCM_KEY_BODY
)Object
;
100 /* Don't do anything if we're not the last handle */
101 if (SystemHandleCount
> 1) return;
103 /* Make sure we're a valid key body */
104 if (KeyBody
->Type
== '20yk')
106 /* Don't do anything if we don't have a notify block */
107 if (!KeyBody
->NotifyBlock
) return;
109 /* This shouldn't happen yet */
116 CmpQueryKeyName(IN PVOID ObjectBody
,
118 IN OUT POBJECT_NAME_INFORMATION ObjectNameInfo
,
120 OUT PULONG ReturnLength
,
121 IN KPROCESSOR_MODE PreviousMode
)
123 PUNICODE_STRING KeyName
;
124 NTSTATUS Status
= STATUS_SUCCESS
;
125 PCM_KEY_BODY KeyBody
= (PCM_KEY_BODY
)ObjectBody
;
126 PCM_KEY_CONTROL_BLOCK Kcb
= KeyBody
->KeyControlBlock
;
128 /* Acquire hive lock */
131 /* Lock KCB shared */
132 CmpAcquireKcbLockShared(Kcb
);
134 /* Check if it's a deleted block */
137 /* Release the locks */
138 CmpReleaseKcbLock(Kcb
);
141 /* Let the caller know it's deleted */
142 return STATUS_KEY_DELETED
;
146 KeyName
= CmpConstructName(Kcb
);
148 /* Release the locks */
149 CmpReleaseKcbLock(Kcb
);
152 /* Check if we got the name */
153 if (!KeyName
) return STATUS_INSUFFICIENT_RESOURCES
;
155 /* Set the returned length */
156 *ReturnLength
= KeyName
->Length
+ sizeof(OBJECT_NAME_INFORMATION
) + sizeof(WCHAR
);
158 /* Check if it fits into the provided buffer */
159 if ((Length
< sizeof(OBJECT_NAME_INFORMATION
)) ||
160 (Length
< (*ReturnLength
- sizeof(OBJECT_NAME_INFORMATION
))))
162 /* Free the buffer allocated by CmpConstructName */
165 /* Return buffer length failure */
166 return STATUS_INFO_LENGTH_MISMATCH
;
169 /* Fill in the result */
172 /* Return data to user */
173 ObjectNameInfo
->Name
.Buffer
= (PWCHAR
)(ObjectNameInfo
+ 1);
174 ObjectNameInfo
->Name
.MaximumLength
= KeyName
->Length
;
175 ObjectNameInfo
->Name
.Length
= KeyName
->Length
;
177 /* Copy string content*/
178 RtlCopyMemory(ObjectNameInfo
->Name
.Buffer
,
182 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
185 Status
= _SEH2_GetExceptionCode();
189 /* Free the buffer allocated by CmpConstructName */
198 CmpInitHiveFromFile(IN PCUNICODE_STRING HiveName
,
204 ULONG HiveDisposition
, LogDisposition
;
205 HANDLE FileHandle
= NULL
, LogHandle
= NULL
;
207 ULONG Operation
, FileType
;
214 /* Open or create the hive files */
215 Status
= CmpOpenHiveFiles(HiveName
,
225 if (!NT_SUCCESS(Status
)) return Status
;
227 /* Check if we have a log handle */
228 FileType
= (LogHandle
) ? HFILE_TYPE_LOG
: HFILE_TYPE_PRIMARY
;
230 /* Check if we created or opened the hive */
231 if (HiveDisposition
== FILE_CREATED
)
233 /* Do a create operation */
234 Operation
= HINIT_CREATE
;
239 /* Open it as a file */
240 Operation
= HINIT_FILE
;
244 /* Check if we're sharing hives */
245 if (CmpShareSystemHives
)
247 /* Then force using the primary hive */
248 FileType
= HFILE_TYPE_PRIMARY
;
251 /* Get rid of the log handle */
257 /* Check if we're too late */
258 if (HvShutdownComplete
)
262 if (LogHandle
) ZwClose(LogHandle
);
263 return STATUS_TOO_LATE
;
266 /* Initialize the hive */
267 Status
= CmpInitializeHive((PCMHIVE
*)&NewHive
,
277 if (!NT_SUCCESS(Status
))
281 if (LogHandle
) ZwClose(LogHandle
);
285 /* Success, return hive */
288 /* ROS: Init root key cell and prepare the hive */
289 if (Operation
== HINIT_CREATE
) CmCreateRootNode(&NewHive
->Hive
, L
"");
291 /* Duplicate the hive name */
292 NewHive
->FileFullPath
.Buffer
= ExAllocatePoolWithTag(PagedPool
,
295 if (NewHive
->FileFullPath
.Buffer
)
297 /* Copy the string */
298 RtlCopyMemory(NewHive
->FileFullPath
.Buffer
,
301 NewHive
->FileFullPath
.Length
= HiveName
->Length
;
302 NewHive
->FileFullPath
.MaximumLength
= HiveName
->MaximumLength
;
306 return STATUS_SUCCESS
;
312 CmpSetSystemValues(IN PLOADER_PARAMETER_BLOCK LoaderBlock
)
314 OBJECT_ATTRIBUTES ObjectAttributes
;
315 UNICODE_STRING KeyName
, ValueName
= { 0, 0, NULL
};
318 ASSERT(LoaderBlock
!= NULL
);
320 /* Setup attributes for loader options */
321 RtlInitUnicodeString(&KeyName
,
322 L
"\\REGISTRY\\MACHINE\\SYSTEM\\CurrentControlSet\\"
324 InitializeObjectAttributes(&ObjectAttributes
,
326 OBJ_CASE_INSENSITIVE
,
329 Status
= NtOpenKey(&KeyHandle
, KEY_WRITE
, &ObjectAttributes
);
330 if (!NT_SUCCESS(Status
)) goto Quickie
;
332 /* Key opened, now write to the key */
333 RtlInitUnicodeString(&KeyName
, L
"SystemStartOptions");
334 Status
= NtSetValueKey(KeyHandle
,
338 CmpLoadOptions
.Buffer
,
339 CmpLoadOptions
.Length
);
340 if (!NT_SUCCESS(Status
)) goto Quickie
;
342 /* Setup value name for system boot device */
343 RtlInitUnicodeString(&KeyName
, L
"SystemBootDevice");
344 RtlCreateUnicodeStringFromAsciiz(&ValueName
, LoaderBlock
->NtBootPathName
);
345 Status
= NtSetValueKey(KeyHandle
,
353 /* Free the buffers */
354 RtlFreeUnicodeString(&ValueName
);
356 /* Close the key and return */
359 /* Return the status */
360 return (ExpInTextModeSetup
? STATUS_SUCCESS
: Status
);
366 CmpCreateControlSet(IN PLOADER_PARAMETER_BLOCK LoaderBlock
)
368 UNICODE_STRING ConfigName
= RTL_CONSTANT_STRING(L
"Control\\IDConfigDB");
369 UNICODE_STRING SelectName
=
370 RTL_CONSTANT_STRING(L
"\\Registry\\Machine\\System\\Select");
371 UNICODE_STRING KeyName
;
372 OBJECT_ATTRIBUTES ObjectAttributes
;
373 CHAR ValueInfoBuffer
[128];
374 PKEY_VALUE_FULL_INFORMATION ValueInfo
;
376 WCHAR UnicodeBuffer
[128];
377 HANDLE SelectHandle
, KeyHandle
, ConfigHandle
= NULL
, ProfileHandle
= NULL
;
378 HANDLE ParentHandle
= NULL
;
379 ULONG ControlSet
, HwProfile
;
380 ANSI_STRING TempString
;
382 ULONG ResultLength
, Disposition
;
383 PLOADER_PARAMETER_EXTENSION LoaderExtension
;
386 /* Open the select key */
387 InitializeObjectAttributes(&ObjectAttributes
,
389 OBJ_CASE_INSENSITIVE
,
392 Status
= NtOpenKey(&SelectHandle
, KEY_READ
, &ObjectAttributes
);
393 if (!NT_SUCCESS(Status
))
395 /* ReactOS Hack: Hard-code current to 001 for SetupLdr */
396 if (!LoaderBlock
->RegistryBase
)
398 /* Build the ControlSet001 key */
399 RtlInitUnicodeString(&KeyName
,
400 L
"\\Registry\\Machine\\System\\ControlSet001");
401 InitializeObjectAttributes(&ObjectAttributes
,
403 OBJ_CASE_INSENSITIVE
,
406 Status
= NtCreateKey(&KeyHandle
,
413 if (!NT_SUCCESS(Status
)) return Status
;
415 /* Don't need the handle */
418 /* Use hard-coded setting */
423 /* Fail for real boots */
427 /* Open the current value */
428 RtlInitUnicodeString(&KeyName
, L
"Current");
429 Status
= NtQueryValueKey(SelectHandle
,
431 KeyValueFullInformation
,
433 sizeof(ValueInfoBuffer
),
435 NtClose(SelectHandle
);
436 if (!NT_SUCCESS(Status
)) return Status
;
438 /* Get the actual value pointer, and get the control set ID */
439 ValueInfo
= (PKEY_VALUE_FULL_INFORMATION
)ValueInfoBuffer
;
440 ControlSet
= *(PULONG
)((PUCHAR
)ValueInfo
+ ValueInfo
->DataOffset
);
442 /* Create the current control set key */
444 RtlInitUnicodeString(&KeyName
,
445 L
"\\Registry\\Machine\\System\\CurrentControlSet");
446 InitializeObjectAttributes(&ObjectAttributes
,
448 OBJ_CASE_INSENSITIVE
,
451 Status
= NtCreateKey(&KeyHandle
,
456 REG_OPTION_VOLATILE
| REG_OPTION_CREATE_LINK
,
458 if (!NT_SUCCESS(Status
)) return Status
;
461 ASSERT(Disposition
== REG_CREATED_NEW_KEY
);
463 /* Initialize the symbolic link name */
465 "\\Registry\\Machine\\System\\ControlSet%03ld",
467 RtlInitAnsiString(&TempString
, Buffer
);
469 /* Create a Unicode string out of it */
470 KeyName
.MaximumLength
= sizeof(UnicodeBuffer
);
471 KeyName
.Buffer
= UnicodeBuffer
;
472 Status
= RtlAnsiStringToUnicodeString(&KeyName
, &TempString
, FALSE
);
475 Status
= NtSetValueKey(KeyHandle
,
476 &CmSymbolicLinkValueName
,
481 if (!NT_SUCCESS(Status
)) return Status
;
483 /* Get the configuration database key */
484 InitializeObjectAttributes(&ObjectAttributes
,
486 OBJ_CASE_INSENSITIVE
,
489 Status
= NtOpenKey(&ConfigHandle
, KEY_READ
, &ObjectAttributes
);
492 /* Check if we don't have one */
493 if (!NT_SUCCESS(Status
))
495 /* Cleanup and exit */
500 /* Now get the current config */
501 RtlInitUnicodeString(&KeyName
, L
"CurrentConfig");
502 Status
= NtQueryValueKey(ConfigHandle
,
504 KeyValueFullInformation
,
506 sizeof(ValueInfoBuffer
),
509 /* Set pointer to buffer */
510 ValueInfo
= (PKEY_VALUE_FULL_INFORMATION
)ValueInfoBuffer
;
512 /* Check if we failed or got a non DWORD-value */
513 if (!(NT_SUCCESS(Status
)) || (ValueInfo
->Type
!= REG_DWORD
)) goto Cleanup
;
515 /* Get the hadware profile */
516 HwProfile
= *(PULONG
)((PUCHAR
)ValueInfo
+ ValueInfo
->DataOffset
);
518 /* Open the hardware profile key */
519 RtlInitUnicodeString(&KeyName
,
520 L
"\\Registry\\Machine\\System\\CurrentControlSet"
521 L
"\\Hardware Profiles");
522 InitializeObjectAttributes(&ObjectAttributes
,
524 OBJ_CASE_INSENSITIVE
,
527 Status
= NtOpenKey(&ParentHandle
, KEY_READ
, &ObjectAttributes
);
528 if (!NT_SUCCESS(Status
))
530 /* Exit and clean up */
535 /* Build the profile name */
536 sprintf(Buffer
, "%04ld", HwProfile
);
537 RtlInitAnsiString(&TempString
, Buffer
);
539 /* Convert it to Unicode */
540 KeyName
.MaximumLength
= sizeof(UnicodeBuffer
);
541 KeyName
.Buffer
= UnicodeBuffer
;
542 Status
= RtlAnsiStringToUnicodeString(&KeyName
,
545 ASSERT(Status
== STATUS_SUCCESS
);
547 /* Open the associated key */
548 InitializeObjectAttributes(&ObjectAttributes
,
550 OBJ_CASE_INSENSITIVE
,
553 Status
= NtOpenKey(&ProfileHandle
,
554 KEY_READ
| KEY_WRITE
,
556 if (!NT_SUCCESS (Status
))
558 /* Cleanup and exit */
563 /* Check if we have a loader block extension */
564 LoaderExtension
= LoaderBlock
->Extension
;
567 ASSERTMSG("ReactOS doesn't support NTLDR Profiles yet!\n", FALSE
);
570 /* Create the current hardware profile key */
571 RtlInitUnicodeString(&KeyName
,
572 L
"\\Registry\\Machine\\System\\CurrentControlSet\\"
573 L
"Hardware Profiles\\Current");
574 InitializeObjectAttributes(&ObjectAttributes
,
576 OBJ_CASE_INSENSITIVE
,
579 Status
= NtCreateKey(&KeyHandle
,
584 REG_OPTION_VOLATILE
| REG_OPTION_CREATE_LINK
,
586 if (NT_SUCCESS(Status
))
589 ASSERT(Disposition
== REG_CREATED_NEW_KEY
);
591 /* Create the profile name */
593 "\\Registry\\Machine\\System\\CurrentControlSet\\"
594 "Hardware Profiles\\%04ld",
596 RtlInitAnsiString(&TempString
, Buffer
);
598 /* Convert it to Unicode */
599 KeyName
.MaximumLength
= sizeof(UnicodeBuffer
);
600 KeyName
.Buffer
= UnicodeBuffer
;
601 Status
= RtlAnsiStringToUnicodeString(&KeyName
,
604 ASSERT(STATUS_SUCCESS
== Status
);
607 Status
= NtSetValueKey(KeyHandle
,
608 &CmSymbolicLinkValueName
,
616 /* Close every opened handle */
618 if (ConfigHandle
) NtClose(ConfigHandle
);
619 if (ProfileHandle
) NtClose(ProfileHandle
);
620 if (ParentHandle
) NtClose(ParentHandle
);
623 return STATUS_SUCCESS
;
628 CmpLinkHiveToMaster(IN PUNICODE_STRING LinkName
,
629 IN HANDLE RootDirectory
,
630 IN PCMHIVE RegistryHive
,
632 IN PSECURITY_DESCRIPTOR SecurityDescriptor
)
634 OBJECT_ATTRIBUTES ObjectAttributes
;
636 CM_PARSE_CONTEXT ParseContext
= {0};
638 PCM_KEY_BODY KeyBody
;
641 /* Setup the object attributes */
642 InitializeObjectAttributes(&ObjectAttributes
,
644 OBJ_CASE_INSENSITIVE
| OBJ_KERNEL_HANDLE
,
648 /* Setup the parse context */
649 ParseContext
.CreateLink
= TRUE
;
650 ParseContext
.CreateOperation
= TRUE
;
651 ParseContext
.ChildHive
.KeyHive
= &RegistryHive
->Hive
;
653 /* Check if we have a root keycell or if we need to create it */
657 ParseContext
.ChildHive
.KeyCell
= HCELL_NIL
;
662 ParseContext
.ChildHive
.KeyCell
= RegistryHive
->Hive
.BaseBlock
->RootCell
;
665 /* Create the link node */
666 Status
= ObOpenObjectByName(&ObjectAttributes
,
670 KEY_READ
| KEY_WRITE
,
671 (PVOID
)&ParseContext
,
673 if (!NT_SUCCESS(Status
)) return Status
;
675 /* Mark the hive as clean */
676 RegistryHive
->Hive
.DirtyFlag
= FALSE
;
678 /* ReactOS Hack: Keep alive */
679 Status
= ObReferenceObjectByHandle(KeyHandle
,
685 ASSERT(NT_SUCCESS(Status
));
687 /* Close the extra handle */
689 return STATUS_SUCCESS
;
695 CmpInitializeSystemHive(IN PLOADER_PARAMETER_BLOCK LoaderBlock
)
698 ANSI_STRING LoadString
;
703 UNICODE_STRING KeyName
;
704 PCMHIVE SystemHive
= NULL
;
705 UNICODE_STRING HiveName
= RTL_CONSTANT_STRING(L
"SYSTEM");
706 PSECURITY_DESCRIPTOR SecurityDescriptor
;
709 /* Setup the ansi string */
710 RtlInitAnsiString(&LoadString
, LoaderBlock
->LoadOptions
);
712 /* Allocate the unicode buffer */
713 Length
= LoadString
.Length
* sizeof(WCHAR
) + sizeof(UNICODE_NULL
);
714 Buffer
= ExAllocatePoolWithTag(PagedPool
, Length
, TAG_CM
);
718 KeBugCheckEx(BAD_SYSTEM_CONFIG_INFO
, 3, 1, (ULONG_PTR
)LoaderBlock
, 0);
721 /* Setup the unicode string */
722 RtlInitEmptyUnicodeString(&CmpLoadOptions
, Buffer
, (USHORT
)Length
);
724 /* Add the load options and null-terminate */
725 RtlAnsiStringToUnicodeString(&CmpLoadOptions
, &LoadString
, FALSE
);
726 CmpLoadOptions
.Buffer
[LoadString
.Length
] = UNICODE_NULL
;
727 CmpLoadOptions
.Length
+= sizeof(WCHAR
);
729 /* Get the System Hive base address */
730 HiveBase
= LoaderBlock
->RegistryBase
;
734 ((PHBASE_BLOCK
)HiveBase
)->Length
= LoaderBlock
->RegistryLength
;
735 Status
= CmpInitializeHive((PCMHIVE
*)&SystemHive
,
745 if (!NT_SUCCESS(Status
)) return FALSE
;
747 /* Set the hive filename */
748 RtlCreateUnicodeString(&SystemHive
->FileFullPath
,
749 L
"\\SystemRoot\\System32\\Config\\SYSTEM");
751 /* We imported, no need to create a new hive */
754 /* Manually set the hive as volatile, if in Live CD mode */
755 if (CmpShareSystemHives
) SystemHive
->Hive
.HiveFlags
= HIVE_VOLATILE
;
760 Status
= CmpInitializeHive(&SystemHive
,
770 if (!NT_SUCCESS(Status
)) return FALSE
;
772 /* Set the hive filename */
773 RtlCreateUnicodeString(&SystemHive
->FileFullPath
,
774 L
"\\SystemRoot\\System32\\Config\\SYSTEM");
776 /* Tell CmpLinkHiveToMaster to allocate a hive */
780 /* Save the boot type */
781 CmpBootType
= SystemHive
->Hive
.BaseBlock
->BootType
;
783 /* Are we in self-healing mode? */
786 /* Disable self-healing internally and check if boot type wanted it */
790 /* We're disabled, so bugcheck */
791 KeBugCheckEx(BAD_SYSTEM_CONFIG_INFO
,
794 (ULONG_PTR
)SystemHive
,
799 /* Create the default security descriptor */
800 SecurityDescriptor
= CmpHiveRootSecurityDescriptor();
802 /* Attach it to the system key */
803 RtlInitUnicodeString(&KeyName
, L
"\\Registry\\Machine\\SYSTEM");
804 Status
= CmpLinkHiveToMaster(&KeyName
,
810 /* Free the security descriptor */
811 ExFreePoolWithTag(SecurityDescriptor
, TAG_CM
);
812 if (!NT_SUCCESS(Status
)) return FALSE
;
814 /* Add the hive to the hive list */
815 CmpMachineHiveList
[3].CmHive
= (PCMHIVE
)SystemHive
;
824 CmpCreateObjectTypes(VOID
)
826 OBJECT_TYPE_INITIALIZER ObjectTypeInitializer
;
828 GENERIC_MAPPING CmpKeyMapping
= {KEY_READ
,
834 /* Initialize the Key object type */
835 RtlZeroMemory(&ObjectTypeInitializer
, sizeof(ObjectTypeInitializer
));
836 RtlInitUnicodeString(&Name
, L
"Key");
837 ObjectTypeInitializer
.Length
= sizeof(ObjectTypeInitializer
);
838 ObjectTypeInitializer
.DefaultPagedPoolCharge
= sizeof(CM_KEY_BODY
);
839 ObjectTypeInitializer
.GenericMapping
= CmpKeyMapping
;
840 ObjectTypeInitializer
.PoolType
= PagedPool
;
841 ObjectTypeInitializer
.ValidAccessMask
= KEY_ALL_ACCESS
;
842 ObjectTypeInitializer
.UseDefaultObject
= TRUE
;
843 ObjectTypeInitializer
.DeleteProcedure
= CmpDeleteKeyObject
;
844 ObjectTypeInitializer
.ParseProcedure
= CmpParseKey
;
845 ObjectTypeInitializer
.SecurityProcedure
= CmpSecurityMethod
;
846 ObjectTypeInitializer
.QueryNameProcedure
= CmpQueryKeyName
;
847 ObjectTypeInitializer
.CloseProcedure
= CmpCloseKeyObject
;
848 ObjectTypeInitializer
.SecurityRequired
= TRUE
;
851 return ObCreateObjectType(&Name
, &ObjectTypeInitializer
, NULL
, &CmpKeyObjectType
);
857 CmpCreateRootNode(IN PHHIVE Hive
,
859 OUT PHCELL_INDEX Index
)
861 UNICODE_STRING KeyName
;
862 PCM_KEY_NODE KeyCell
;
863 LARGE_INTEGER SystemTime
;
866 /* Initialize the node name and allocate it */
867 RtlInitUnicodeString(&KeyName
, Name
);
868 *Index
= HvAllocateCell(Hive
,
869 FIELD_OFFSET(CM_KEY_NODE
, Name
) +
870 CmpNameSize(Hive
, &KeyName
),
873 if (*Index
== HCELL_NIL
) return FALSE
;
875 /* Set the cell index and get the data */
876 Hive
->BaseBlock
->RootCell
= *Index
;
877 KeyCell
= (PCM_KEY_NODE
)HvGetCell(Hive
, *Index
);
878 if (!KeyCell
) return FALSE
;
881 KeyCell
->Signature
= (USHORT
)CM_KEY_NODE_SIGNATURE
;
882 KeyCell
->Flags
= KEY_HIVE_ENTRY
| KEY_NO_DELETE
;
883 KeQuerySystemTime(&SystemTime
);
884 KeyCell
->LastWriteTime
= SystemTime
;
885 KeyCell
->Parent
= HCELL_NIL
;
886 KeyCell
->SubKeyCounts
[Stable
] = 0;
887 KeyCell
->SubKeyCounts
[Volatile
] = 0;
888 KeyCell
->SubKeyLists
[Stable
] = HCELL_NIL
;
889 KeyCell
->SubKeyLists
[Volatile
] = HCELL_NIL
;
890 KeyCell
->ValueList
.Count
= 0;
891 KeyCell
->ValueList
.List
= HCELL_NIL
;
892 KeyCell
->Security
= HCELL_NIL
;
893 KeyCell
->Class
= HCELL_NIL
;
894 KeyCell
->ClassLength
= 0;
895 KeyCell
->MaxNameLen
= 0;
896 KeyCell
->MaxClassLen
= 0;
897 KeyCell
->MaxValueNameLen
= 0;
898 KeyCell
->MaxValueDataLen
= 0;
900 /* Copy the name (this will also set the length) */
901 KeyCell
->NameLength
= CmpCopyName(Hive
, (PWCHAR
)KeyCell
->Name
, &KeyName
);
903 /* Check if the name was compressed */
904 if (KeyCell
->NameLength
< KeyName
.Length
)
907 KeyCell
->Flags
|= KEY_COMP_NAME
;
911 HvReleaseCell(Hive
, *Index
);
918 CmpCreateRegistryRoot(VOID
)
920 UNICODE_STRING KeyName
;
921 OBJECT_ATTRIBUTES ObjectAttributes
;
922 PCM_KEY_BODY RootKey
;
923 HCELL_INDEX RootIndex
;
925 PCM_KEY_NODE KeyCell
;
926 PSECURITY_DESCRIPTOR SecurityDescriptor
;
927 PCM_KEY_CONTROL_BLOCK Kcb
;
930 /* Setup the root node */
931 if (!CmpCreateRootNode(&CmiVolatileHive
->Hive
, L
"REGISTRY", &RootIndex
))
937 /* Create '\Registry' key. */
938 RtlInitUnicodeString(&KeyName
, L
"\\REGISTRY");
939 SecurityDescriptor
= CmpHiveRootSecurityDescriptor();
940 InitializeObjectAttributes(&ObjectAttributes
,
942 OBJ_CASE_INSENSITIVE
,
945 Status
= ObCreateObject(KernelMode
,
954 ExFreePoolWithTag(SecurityDescriptor
, TAG_CM
);
955 if (!NT_SUCCESS(Status
)) return FALSE
;
957 /* Sanity check, and get the key cell */
958 ASSERT((&CmiVolatileHive
->Hive
)->ReleaseCellRoutine
== NULL
);
959 KeyCell
= (PCM_KEY_NODE
)HvGetCell(&CmiVolatileHive
->Hive
, RootIndex
);
960 if (!KeyCell
) return FALSE
;
963 RtlInitUnicodeString(&KeyName
, L
"\\REGISTRY");
964 Kcb
= CmpCreateKeyControlBlock(&CmiVolatileHive
->Hive
,
970 if (!Kcb
) return FALSE
;
972 /* Initialize the object */
973 RootKey
->KeyControlBlock
= Kcb
;
974 RootKey
->Type
= '20yk';
975 RootKey
->NotifyBlock
= NULL
;
976 RootKey
->ProcessID
= PsGetCurrentProcessId();
979 EnlistKeyBodyWithKCB(RootKey
, 0);
981 /* Insert the key into the namespace */
982 Status
= ObInsertObject(RootKey
,
987 &CmpRegistryRootHandle
);
988 if (!NT_SUCCESS(Status
)) return FALSE
;
990 /* Reference the key again so that we never lose it */
991 Status
= ObReferenceObjectByHandle(CmpRegistryRootHandle
,
997 if (!NT_SUCCESS(Status
)) return FALSE
;
999 /* Completely sucessful */
1005 CmpGetRegistryPath(IN PWCHAR ConfigPath
)
1007 OBJECT_ATTRIBUTES ObjectAttributes
;
1010 PKEY_VALUE_PARTIAL_INFORMATION ValueInfo
;
1011 UNICODE_STRING KeyName
= RTL_CONSTANT_STRING(L
"\\Registry\\Machine\\HARDWARE");
1012 UNICODE_STRING ValueName
= RTL_CONSTANT_STRING(L
"InstallPath");
1013 ULONG BufferSize
, ResultSize
;
1015 /* Check if we are booted in setup */
1016 if (ExpInTextModeSetup
)
1018 /* Setup the object attributes */
1019 InitializeObjectAttributes(&ObjectAttributes
,
1021 OBJ_CASE_INSENSITIVE
,
1025 Status
= ZwOpenKey(&KeyHandle
,
1028 if (!NT_SUCCESS(Status
)) return Status
;
1030 /* Allocate the buffer */
1031 BufferSize
= sizeof(KEY_VALUE_PARTIAL_INFORMATION
) + 4096;
1032 ValueInfo
= ExAllocatePoolWithTag(PagedPool
, BufferSize
, TAG_CM
);
1037 return STATUS_INSUFFICIENT_RESOURCES
;
1040 /* Query the value */
1041 Status
= ZwQueryValueKey(KeyHandle
,
1043 KeyValuePartialInformation
,
1048 if (!NT_SUCCESS(Status
))
1051 ExFreePoolWithTag(ValueInfo
, TAG_CM
);
1055 /* Copy the config path and null-terminate it */
1056 RtlCopyMemory(ConfigPath
,
1058 ValueInfo
->DataLength
);
1059 ConfigPath
[ValueInfo
->DataLength
/ sizeof(WCHAR
)] = UNICODE_NULL
;
1060 ExFreePoolWithTag(ValueInfo
, TAG_CM
);
1064 /* Just use default path */
1065 wcscpy(ConfigPath
, L
"\\SystemRoot");
1068 /* Add registry path */
1069 wcscat(ConfigPath
, L
"\\System32\\Config\\");
1072 return STATUS_SUCCESS
;
1077 CmpLoadHiveThread(IN PVOID StartContext
)
1079 WCHAR FileBuffer
[MAX_PATH
], RegBuffer
[MAX_PATH
], ConfigPath
[MAX_PATH
];
1080 UNICODE_STRING TempName
, FileName
, RegName
;
1081 ULONG FileStart
, RegStart
, i
, ErrorResponse
, WorkerCount
, Length
;
1082 ULONG PrimaryDisposition
, SecondaryDisposition
, ClusterSize
;
1084 HANDLE PrimaryHandle
, LogHandle
;
1085 NTSTATUS Status
= STATUS_SUCCESS
;
1086 PVOID ErrorParameters
;
1089 /* Get the hive index, make sure it makes sense */
1090 i
= PtrToUlong(StartContext
);
1091 ASSERT(CmpMachineHiveList
[i
].Name
!= NULL
);
1093 /* We were started */
1094 CmpMachineHiveList
[i
].ThreadStarted
= TRUE
;
1096 /* Build the file name and registry name strings */
1097 RtlInitEmptyUnicodeString(&FileName
, FileBuffer
, MAX_PATH
);
1098 RtlInitEmptyUnicodeString(&RegName
, RegBuffer
, MAX_PATH
);
1100 /* Now build the system root path */
1101 CmpGetRegistryPath(ConfigPath
);
1102 RtlInitUnicodeString(&TempName
, ConfigPath
);
1103 RtlAppendStringToString((PSTRING
)&FileName
, (PSTRING
)&TempName
);
1104 FileStart
= FileName
.Length
;
1106 /* And build the registry root path */
1107 RtlInitUnicodeString(&TempName
, L
"\\REGISTRY\\");
1108 RtlAppendStringToString((PSTRING
)&RegName
, (PSTRING
)&TempName
);
1109 RegStart
= RegName
.Length
;
1111 /* Build the base name */
1112 RtlInitUnicodeString(&TempName
, CmpMachineHiveList
[i
].BaseName
);
1113 RtlAppendStringToString((PSTRING
)&RegName
, (PSTRING
)&TempName
);
1115 /* Check if this is a child of the root */
1116 if (RegName
.Buffer
[RegName
.Length
/ sizeof(WCHAR
) - 1] == '\\')
1118 /* Then setup the whole name */
1119 RtlInitUnicodeString(&TempName
, CmpMachineHiveList
[i
].Name
);
1120 RtlAppendStringToString((PSTRING
)&RegName
, (PSTRING
)&TempName
);
1123 /* Now add the rest of the file name */
1124 RtlInitUnicodeString(&TempName
, CmpMachineHiveList
[i
].Name
);
1125 FileName
.Length
= FileStart
;
1126 RtlAppendStringToString((PSTRING
)&FileName
, (PSTRING
)&TempName
);
1127 if (!CmpMachineHiveList
[i
].CmHive
)
1129 /* We need to allocate a new hive structure */
1130 CmpMachineHiveList
[i
].Allocate
= TRUE
;
1132 /* Load the hive file */
1133 Status
= CmpInitHiveFromFile(&FileName
,
1134 CmpMachineHiveList
[i
].HHiveFlags
,
1136 &CmpMachineHiveList
[i
].Allocate
,
1138 if (!(NT_SUCCESS(Status
)) ||
1139 (!(CmHive
->FileHandles
[HFILE_TYPE_LOG
]) && !(CmpMiniNTBoot
))) // HACK
1141 /* We failed or couldn't get a log file, raise a hard error */
1142 ErrorParameters
= &FileName
;
1143 NtRaiseHardError(STATUS_CANNOT_LOAD_REGISTRY_FILE
,
1146 (PULONG_PTR
)&ErrorParameters
,
1151 /* Set the hive flags and newly allocated hive pointer */
1152 CmHive
->Flags
= CmpMachineHiveList
[i
].CmHiveFlags
;
1153 CmpMachineHiveList
[i
].CmHive2
= CmHive
;
1157 /* We already have a hive, is it volatile? */
1158 CmHive
= CmpMachineHiveList
[i
].CmHive
;
1159 if (!(CmHive
->Hive
.HiveFlags
& HIVE_VOLATILE
))
1161 /* It's now, open the hive file and log */
1162 Status
= CmpOpenHiveFiles(&FileName
,
1166 &PrimaryDisposition
,
1167 &SecondaryDisposition
,
1172 if (!(NT_SUCCESS(Status
)) || !(LogHandle
))
1174 /* Couldn't open the hive or its log file, raise a hard error */
1175 ErrorParameters
= &FileName
;
1176 NtRaiseHardError(STATUS_CANNOT_LOAD_REGISTRY_FILE
,
1179 (PULONG_PTR
)&ErrorParameters
,
1183 /* And bugcheck for posterity's sake */
1184 KeBugCheckEx(BAD_SYSTEM_CONFIG_INFO
, 9, 0, i
, Status
);
1187 /* Save the file handles. This should remove our sync hacks */
1188 CmHive
->FileHandles
[HFILE_TYPE_LOG
] = LogHandle
;
1189 CmHive
->FileHandles
[HFILE_TYPE_PRIMARY
] = PrimaryHandle
;
1191 /* Allow lazy flushing since the handles are there -- remove sync hacks */
1192 //ASSERT(CmHive->Hive.HiveFlags & HIVE_NOLAZYFLUSH);
1193 CmHive
->Hive
.HiveFlags
&= ~HIVE_NOLAZYFLUSH
;
1195 /* Get the real size of the hive */
1196 Length
= CmHive
->Hive
.Storage
[Stable
].Length
+ HBLOCK_SIZE
;
1198 /* Check if the cluster size doesn't match */
1199 if (CmHive
->Hive
.Cluster
!= ClusterSize
) ASSERT(FALSE
);
1201 /* Set the file size */
1202 DPRINT("FIXME: Should set file size: %lx\n", Length
);
1203 //if (!CmpFileSetSize((PHHIVE)CmHive, HFILE_TYPE_PRIMARY, Length, Length))
1205 /* This shouldn't fail */
1209 /* Another thing we don't support is NTLDR-recovery */
1210 if (CmHive
->Hive
.BaseBlock
->BootRecover
) ASSERT(FALSE
);
1212 /* Finally, set our allocated hive to the same hive we've had */
1213 CmpMachineHiveList
[i
].CmHive2
= CmHive
;
1214 ASSERT(CmpMachineHiveList
[i
].CmHive
== CmpMachineHiveList
[i
].CmHive2
);
1219 CmpMachineHiveList
[i
].ThreadFinished
= TRUE
;
1221 /* Check if we're the last worker */
1222 WorkerCount
= InterlockedIncrement(&CmpLoadWorkerIncrement
);
1223 if (WorkerCount
== CM_NUMBER_OF_MACHINE_HIVES
)
1225 /* Signal the event */
1226 KeSetEvent(&CmpLoadWorkerEvent
, 0, FALSE
);
1229 /* Kill the thread */
1230 PsTerminateSystemThread(Status
);
1235 CmpInitializeHiveList(IN USHORT Flag
)
1237 WCHAR FileBuffer
[MAX_PATH
], RegBuffer
[MAX_PATH
], ConfigPath
[MAX_PATH
];
1238 UNICODE_STRING TempName
, FileName
, RegName
;
1242 PSECURITY_DESCRIPTOR SecurityDescriptor
;
1245 /* Allow writing for now */
1248 /* Build the file name and registry name strings */
1249 RtlInitEmptyUnicodeString(&FileName
, FileBuffer
, MAX_PATH
);
1250 RtlInitEmptyUnicodeString(&RegName
, RegBuffer
, MAX_PATH
);
1252 /* Now build the system root path */
1253 CmpGetRegistryPath(ConfigPath
);
1254 RtlInitUnicodeString(&TempName
, ConfigPath
);
1255 RtlAppendStringToString((PSTRING
)&FileName
, (PSTRING
)&TempName
);
1257 /* And build the registry root path */
1258 RtlInitUnicodeString(&TempName
, L
"\\REGISTRY\\");
1259 RtlAppendStringToString((PSTRING
)&RegName
, (PSTRING
)&TempName
);
1260 RegStart
= RegName
.Length
;
1262 /* Setup the event to synchronize workers */
1263 KeInitializeEvent(&CmpLoadWorkerEvent
, SynchronizationEvent
, FALSE
);
1265 /* Enter special boot condition */
1266 CmpSpecialBootCondition
= TRUE
;
1268 /* Create the SD for the root hives */
1269 SecurityDescriptor
= CmpHiveRootSecurityDescriptor();
1271 /* Loop every hive we care about */
1272 for (i
= 0; i
< CM_NUMBER_OF_MACHINE_HIVES
; i
++)
1274 /* Make sure the list is setup */
1275 ASSERT(CmpMachineHiveList
[i
].Name
!= NULL
);
1277 /* Create a thread to handle this hive */
1278 Status
= PsCreateSystemThread(&Thread
,
1285 if (NT_SUCCESS(Status
))
1287 /* We don't care about the handle -- the thread self-terminates */
1292 /* Can't imagine this happening */
1293 KeBugCheckEx(BAD_SYSTEM_CONFIG_INFO
, 9, 3, i
, Status
);
1297 /* Make sure we've reached the end of the list */
1298 ASSERT(CmpMachineHiveList
[i
].Name
== NULL
);
1300 /* Wait for hive loading to finish */
1301 KeWaitForSingleObject(&CmpLoadWorkerEvent
,
1307 /* Exit the special boot condition and make sure all workers completed */
1308 CmpSpecialBootCondition
= FALSE
;
1309 ASSERT(CmpLoadWorkerIncrement
== CM_NUMBER_OF_MACHINE_HIVES
);
1311 /* Loop hives again */
1312 for (i
= 0; i
< CM_NUMBER_OF_MACHINE_HIVES
; i
++)
1314 /* Make sure the thread ran and finished */
1315 ASSERT(CmpMachineHiveList
[i
].ThreadFinished
== TRUE
);
1316 ASSERT(CmpMachineHiveList
[i
].ThreadStarted
== TRUE
);
1318 /* Check if this was a new hive */
1319 if (!CmpMachineHiveList
[i
].CmHive
)
1321 /* Make sure we allocated something */
1322 ASSERT(CmpMachineHiveList
[i
].CmHive2
!= NULL
);
1324 /* Build the base name */
1325 RegName
.Length
= RegStart
;
1326 RtlInitUnicodeString(&TempName
, CmpMachineHiveList
[i
].BaseName
);
1327 RtlAppendStringToString((PSTRING
)&RegName
, (PSTRING
)&TempName
);
1329 /* Check if this is a child of the root */
1330 if (RegName
.Buffer
[RegName
.Length
/ sizeof(WCHAR
) - 1] == '\\')
1332 /* Then setup the whole name */
1333 RtlInitUnicodeString(&TempName
, CmpMachineHiveList
[i
].Name
);
1334 RtlAppendStringToString((PSTRING
)&RegName
, (PSTRING
)&TempName
);
1337 /* Now link the hive to its master */
1338 Status
= CmpLinkHiveToMaster(&RegName
,
1340 CmpMachineHiveList
[i
].CmHive2
,
1341 CmpMachineHiveList
[i
].Allocate
,
1342 SecurityDescriptor
);
1343 if (Status
!= STATUS_SUCCESS
)
1345 /* Linking needs to work */
1346 KeBugCheckEx(CONFIG_LIST_FAILED
, 11, Status
, i
, (ULONG_PTR
)&RegName
);
1349 /* Check if we had to allocate a new hive */
1350 if (CmpMachineHiveList
[i
].Allocate
)
1352 /* Sync the new hive */
1353 //HvSyncHive((PHHIVE)(CmpMachineHiveList[i].CmHive2));
1357 /* Check if we created a new hive */
1358 if (CmpMachineHiveList
[i
].CmHive2
)
1360 /* TODO: Add to HiveList key */
1364 /* Get rid of the SD */
1365 ExFreePoolWithTag(SecurityDescriptor
, TAG_CM
);
1367 /* FIXME: Link SECURITY to SAM */
1369 /* FIXME: Link S-1-5-18 to .Default */
1377 OBJECT_ATTRIBUTES ObjectAttributes
;
1378 UNICODE_STRING KeyName
;
1381 PCMHIVE HardwareHive
;
1382 PSECURITY_DESCRIPTOR SecurityDescriptor
;
1385 /* Check if this is PE-boot */
1386 if (InitIsWinPEMode
)
1388 /* Set registry to PE mode */
1389 CmpMiniNTBoot
= TRUE
;
1390 CmpShareSystemHives
= TRUE
;
1393 /* Initialize the hive list and lock */
1394 InitializeListHead(&CmpHiveListHead
);
1395 ExInitializePushLock(&CmpHiveListHeadLock
);
1396 ExInitializePushLock(&CmpLoadHiveLock
);
1398 /* Initialize registry lock */
1399 ExInitializeResourceLite(&CmpRegistryLock
);
1401 /* Initialize the cache */
1402 CmpInitializeCache();
1404 /* Initialize allocation and delayed dereferencing */
1405 CmpInitCmPrivateAlloc();
1406 CmpInitCmPrivateDelayAlloc();
1407 CmpInitDelayDerefKCBEngine();
1409 /* Initialize callbacks */
1412 /* Initialize self healing */
1413 KeInitializeGuardedMutex(&CmpSelfHealQueueLock
);
1414 InitializeListHead(&CmpSelfHealQueueListHead
);
1416 /* Save the current process and lock the registry */
1417 CmpSystemProcess
= PsGetCurrentProcess();
1419 /* Create the key object types */
1420 Status
= CmpCreateObjectTypes();
1421 if (!NT_SUCCESS(Status
))
1424 KeBugCheckEx(CONFIG_INITIALIZATION_FAILED
, 1, 1, Status
, 0);
1427 /* Build the master hive */
1428 Status
= CmpInitializeHive((PCMHIVE
*)&CmiVolatileHive
,
1438 if (!NT_SUCCESS(Status
))
1441 KeBugCheckEx(CONFIG_INITIALIZATION_FAILED
, 1, 2, Status
, 0);
1444 /* Create the \REGISTRY key node */
1445 if (!CmpCreateRegistryRoot())
1448 KeBugCheckEx(CONFIG_INITIALIZATION_FAILED
, 1, 3, 0, 0);
1451 /* Create the default security descriptor */
1452 SecurityDescriptor
= CmpHiveRootSecurityDescriptor();
1454 /* Create '\Registry\Machine' key. */
1455 RtlInitUnicodeString(&KeyName
, L
"\\REGISTRY\\MACHINE");
1456 InitializeObjectAttributes(&ObjectAttributes
,
1458 OBJ_CASE_INSENSITIVE
,
1460 SecurityDescriptor
);
1461 Status
= NtCreateKey(&KeyHandle
,
1462 KEY_READ
| KEY_WRITE
,
1468 if (!NT_SUCCESS(Status
))
1471 KeBugCheckEx(CONFIG_INITIALIZATION_FAILED
, 1, 5, Status
, 0);
1474 /* Close the handle */
1477 /* Create '\Registry\User' key. */
1478 RtlInitUnicodeString(&KeyName
, L
"\\REGISTRY\\USER");
1479 InitializeObjectAttributes(&ObjectAttributes
,
1481 OBJ_CASE_INSENSITIVE
,
1483 SecurityDescriptor
);
1484 Status
= NtCreateKey(&KeyHandle
,
1485 KEY_READ
| KEY_WRITE
,
1491 if (!NT_SUCCESS(Status
))
1494 KeBugCheckEx(CONFIG_INITIALIZATION_FAILED
, 1, 6, Status
, 0);
1497 /* Close the handle */
1500 /* Initialize the system hive */
1501 if (!CmpInitializeSystemHive(KeLoaderBlock
))
1504 KeBugCheckEx(CONFIG_INITIALIZATION_FAILED
, 1, 7, 0, 0);
1507 /* Create the 'CurrentControlSet' link. */
1508 Status
= CmpCreateControlSet(KeLoaderBlock
);
1509 if (!NT_SUCCESS(Status
))
1512 KeBugCheckEx(CONFIG_INITIALIZATION_FAILED
, 1, 8, Status
, 0);
1515 /* Create the hardware hive */
1516 Status
= CmpInitializeHive((PCMHIVE
*)&HardwareHive
,
1526 if (!NT_SUCCESS(Status
))
1529 KeBugCheckEx(CONFIG_INITIALIZATION_FAILED
, 1, 11, Status
, 0);
1532 /* Add the hive to the hive list */
1533 CmpMachineHiveList
[0].CmHive
= (PCMHIVE
)HardwareHive
;
1535 /* Attach it to the machine key */
1536 RtlInitUnicodeString(&KeyName
, L
"\\Registry\\Machine\\HARDWARE");
1537 Status
= CmpLinkHiveToMaster(&KeyName
,
1539 (PCMHIVE
)HardwareHive
,
1541 SecurityDescriptor
);
1542 if (!NT_SUCCESS(Status
))
1545 KeBugCheckEx(CONFIG_INITIALIZATION_FAILED
, 1, 12, Status
, 0);
1548 /* FIXME: Add to HiveList key */
1550 /* Free the security descriptor */
1551 ExFreePoolWithTag(SecurityDescriptor
, TAG_CM
);
1553 /* Fill out the Hardware key with the ARC Data from the Loader */
1554 Status
= CmpInitializeHardwareConfiguration(KeLoaderBlock
);
1555 if (!NT_SUCCESS(Status
))
1558 KeBugCheckEx(CONFIG_INITIALIZATION_FAILED
, 1, 13, Status
, 0);
1561 /* Initialize machine-dependent information into the registry */
1562 Status
= CmpInitializeMachineDependentConfiguration(KeLoaderBlock
);
1563 if (!NT_SUCCESS(Status
))
1566 KeBugCheckEx(CONFIG_INITIALIZATION_FAILED
, 1, 14, Status
, 0);
1569 /* Initialize volatile registry settings */
1570 Status
= CmpSetSystemValues(KeLoaderBlock
);
1571 if (!NT_SUCCESS(Status
))
1574 KeBugCheckEx(CONFIG_INITIALIZATION_FAILED
, 1, 15, Status
, 0);
1577 /* Free the load options */
1578 ExFreePoolWithTag(CmpLoadOptions
.Buffer
, TAG_CM
);
1580 /* If we got here, all went well */
1587 CmpFreeDriverList(IN PHHIVE Hive
,
1588 IN PLIST_ENTRY DriverList
)
1590 PLIST_ENTRY NextEntry
, OldEntry
;
1591 PBOOT_DRIVER_NODE DriverNode
;
1594 /* Parse the current list */
1595 NextEntry
= DriverList
->Flink
;
1596 while (NextEntry
!= DriverList
)
1598 /* Get the driver node */
1599 DriverNode
= CONTAINING_RECORD(NextEntry
, BOOT_DRIVER_NODE
, ListEntry
.Link
);
1601 /* Get the next entry now, since we're going to free it later */
1602 OldEntry
= NextEntry
;
1603 NextEntry
= NextEntry
->Flink
;
1605 /* Was there a name? */
1606 if (DriverNode
->Name
.Buffer
)
1609 CmpFree(DriverNode
->Name
.Buffer
, DriverNode
->Name
.Length
);
1612 /* Was there a registry path? */
1613 if (DriverNode
->ListEntry
.RegistryPath
.Buffer
)
1616 CmpFree(DriverNode
->ListEntry
.RegistryPath
.Buffer
,
1617 DriverNode
->ListEntry
.RegistryPath
.MaximumLength
);
1620 /* Was there a file path? */
1621 if (DriverNode
->ListEntry
.FilePath
.Buffer
)
1624 CmpFree(DriverNode
->ListEntry
.FilePath
.Buffer
,
1625 DriverNode
->ListEntry
.FilePath
.MaximumLength
);
1628 /* Now free the node, and move on */
1629 CmpFree(OldEntry
, sizeof(BOOT_DRIVER_NODE
));
1636 CmGetSystemDriverList(VOID
)
1638 LIST_ENTRY DriverList
;
1639 OBJECT_ATTRIBUTES ObjectAttributes
;
1641 PCM_KEY_BODY KeyBody
;
1643 HCELL_INDEX RootCell
, ControlCell
;
1645 UNICODE_STRING KeyName
;
1646 PLIST_ENTRY NextEntry
;
1648 PUNICODE_STRING
* ServicePath
= NULL
;
1649 BOOLEAN Success
, AutoSelect
;
1650 PBOOT_DRIVER_LIST_ENTRY DriverEntry
;
1653 /* Initialize the driver list */
1654 InitializeListHead(&DriverList
);
1656 /* Open the system hive key */
1657 RtlInitUnicodeString(&KeyName
, L
"\\Registry\\Machine\\System");
1658 InitializeObjectAttributes(&ObjectAttributes
,
1660 OBJ_CASE_INSENSITIVE
,
1663 Status
= NtOpenKey(&KeyHandle
, KEY_READ
, &ObjectAttributes
);
1664 if (!NT_SUCCESS(Status
)) return NULL
;
1666 /* Reference the key object to get the root hive/cell to access directly */
1667 Status
= ObReferenceObjectByHandle(KeyHandle
,
1673 if (!NT_SUCCESS(Status
))
1680 /* Do all this under the registry lock */
1681 CmpLockRegistryExclusive();
1683 /* Get the hive and key cell */
1684 Hive
= KeyBody
->KeyControlBlock
->KeyHive
;
1685 RootCell
= KeyBody
->KeyControlBlock
->KeyCell
;
1687 /* Open the current control set key */
1688 RtlInitUnicodeString(&KeyName
, L
"Current");
1689 ControlCell
= CmpFindControlSet(Hive
, RootCell
, &KeyName
, &AutoSelect
);
1690 if (ControlCell
== HCELL_NIL
) goto EndPath
;
1692 /* Find all system drivers */
1693 Success
= CmpFindDrivers(Hive
, ControlCell
, SystemLoad
, NULL
, &DriverList
);
1694 if (!Success
) goto EndPath
;
1696 /* Sort by group/tag */
1697 if (!CmpSortDriverList(Hive
, ControlCell
, &DriverList
)) goto EndPath
;
1699 /* Remove circular dependencies (cycles) and sort */
1700 if (!CmpResolveDriverDependencies(&DriverList
)) goto EndPath
;
1702 /* Loop the list to count drivers */
1703 for (i
= 0, NextEntry
= DriverList
.Flink
;
1704 NextEntry
!= &DriverList
;
1705 i
++, NextEntry
= NextEntry
->Flink
);
1707 /* Allocate the array */
1708 ServicePath
= ExAllocatePool(NonPagedPool
, (i
+ 1) * sizeof(PUNICODE_STRING
));
1709 if (!ServicePath
) KeBugCheckEx(CONFIG_INITIALIZATION_FAILED
, 2, 1, 0, 0);
1711 /* Loop the driver list */
1712 for (i
= 0, NextEntry
= DriverList
.Flink
;
1713 NextEntry
!= &DriverList
;
1714 i
++, NextEntry
= NextEntry
->Flink
)
1717 DriverEntry
= CONTAINING_RECORD(NextEntry
, BOOT_DRIVER_LIST_ENTRY
, Link
);
1719 /* Allocate the path for the caller and duplicate the registry path */
1720 ServicePath
[i
] = ExAllocatePool(NonPagedPool
, sizeof(UNICODE_STRING
));
1721 RtlDuplicateUnicodeString(RTL_DUPLICATE_UNICODE_STRING_NULL_TERMINATE
,
1722 &DriverEntry
->RegistryPath
,
1726 /* Terminate the list */
1727 ServicePath
[i
] = NULL
;
1730 /* Free the driver list if we had one */
1731 if (!IsListEmpty(&DriverList
)) CmpFreeDriverList(Hive
, &DriverList
);
1733 /* Unlock the registry */
1734 CmpUnlockRegistry();
1736 /* Close the key handle and dereference the object, then return the path */
1737 ObDereferenceObject(KeyBody
);
1744 CmpLockRegistryExclusive(VOID
)
1746 /* Enter a critical region and lock the registry */
1747 KeEnterCriticalRegion();
1748 ExAcquireResourceExclusiveLite(&CmpRegistryLock
, TRUE
);
1751 ASSERT(CmpFlushStarveWriters
== 0);
1752 RtlGetCallersAddress(&CmpRegistryLockCaller
, &CmpRegistryLockCallerCaller
);
1757 CmpLockRegistry(VOID
)
1759 /* Enter a critical region */
1760 KeEnterCriticalRegion();
1762 /* Check if we have to starve writers */
1763 if (CmpFlushStarveWriters
)
1765 /* Starve exlusive waiters */
1766 ExAcquireSharedStarveExclusive(&CmpRegistryLock
, TRUE
);
1770 /* Just grab the lock */
1771 ExAcquireResourceSharedLite(&CmpRegistryLock
, TRUE
);
1777 CmpTestRegistryLock(VOID
)
1780 return !ExIsResourceAcquiredSharedLite(&CmpRegistryLock
) ? FALSE
: TRUE
;
1785 CmpTestRegistryLockExclusive(VOID
)
1788 return !ExIsResourceAcquiredExclusiveLite(&CmpRegistryLock
) ? FALSE
: TRUE
;
1793 CmpLockHiveFlusherExclusive(IN PCMHIVE Hive
)
1795 /* Lock the flusher. We should already be in a critical section */
1796 CMP_ASSERT_REGISTRY_LOCK_OR_LOADING(Hive
);
1797 ASSERT((ExIsResourceAcquiredShared(Hive
->FlusherLock
) == 0) &&
1798 (ExIsResourceAcquiredExclusiveLite(Hive
->FlusherLock
) == 0));
1799 ExAcquireResourceExclusiveLite(Hive
->FlusherLock
, TRUE
);
1804 CmpLockHiveFlusherShared(IN PCMHIVE Hive
)
1806 /* Lock the flusher. We should already be in a critical section */
1807 CMP_ASSERT_REGISTRY_LOCK_OR_LOADING(Hive
);
1808 ASSERT((ExIsResourceAcquiredShared(Hive
->FlusherLock
) == 0) &&
1809 (ExIsResourceAcquiredExclusiveLite(Hive
->FlusherLock
) == 0));
1810 ExAcquireResourceSharedLite(Hive
->FlusherLock
, TRUE
);
1815 CmpUnlockHiveFlusher(IN PCMHIVE Hive
)
1818 CMP_ASSERT_REGISTRY_LOCK_OR_LOADING(Hive
);
1819 CMP_ASSERT_FLUSH_LOCK(Hive
);
1821 /* Release the lock */
1822 ExReleaseResourceLite(Hive
->FlusherLock
);
1827 CmpTestHiveFlusherLockShared(IN PCMHIVE Hive
)
1830 return !ExIsResourceAcquiredSharedLite(Hive
->FlusherLock
) ? FALSE
: TRUE
;
1835 CmpTestHiveFlusherLockExclusive(IN PCMHIVE Hive
)
1838 return !ExIsResourceAcquiredExclusiveLite(Hive
->FlusherLock
) ? FALSE
: TRUE
;
1843 CmpUnlockRegistry(VOID
)
1846 CMP_ASSERT_REGISTRY_LOCK();
1848 /* Check if we should flush the registry */
1849 if (CmpFlushOnLockRelease
)
1851 /* The registry should be exclusively locked for this */
1852 CMP_ASSERT_EXCLUSIVE_REGISTRY_LOCK();
1854 /* Flush the registry */
1855 CmpDoFlushAll(TRUE
);
1856 CmpFlushOnLockRelease
= FALSE
;
1859 /* Release the lock and leave the critical region */
1860 ExReleaseResourceLite(&CmpRegistryLock
);
1861 KeLeaveCriticalRegion();
1866 CmpAcquireTwoKcbLocksExclusiveByKey(IN ULONG ConvKey1
,
1869 ULONG Index1
, Index2
;
1872 CMP_ASSERT_REGISTRY_LOCK();
1874 /* Get hash indexes */
1875 Index1
= GET_HASH_INDEX(ConvKey1
);
1876 Index2
= GET_HASH_INDEX(ConvKey2
);
1878 /* See which one is highest */
1879 if (Index1
< Index2
)
1881 /* Grab them in the proper order */
1882 CmpAcquireKcbLockExclusiveByKey(ConvKey1
);
1883 CmpAcquireKcbLockExclusiveByKey(ConvKey2
);
1887 /* Grab the second one first, then the first */
1888 CmpAcquireKcbLockExclusiveByKey(ConvKey2
);
1889 if (Index1
!= Index2
) CmpAcquireKcbLockExclusiveByKey(ConvKey1
);
1895 CmpReleaseTwoKcbLockByKey(IN ULONG ConvKey1
,
1898 ULONG Index1
, Index2
;
1901 CMP_ASSERT_REGISTRY_LOCK();
1903 /* Get hash indexes */
1904 Index1
= GET_HASH_INDEX(ConvKey1
);
1905 Index2
= GET_HASH_INDEX(ConvKey2
);
1906 ASSERT((GET_HASH_ENTRY(CmpCacheTable
, ConvKey2
).Owner
== KeGetCurrentThread()) ||
1907 (CmpTestRegistryLockExclusive()));
1909 /* See which one is highest */
1910 if (Index1
< Index2
)
1912 /* Grab them in the proper order */
1913 ASSERT((GET_HASH_ENTRY(CmpCacheTable
, ConvKey1
).Owner
== KeGetCurrentThread()) ||
1914 (CmpTestRegistryLockExclusive()));
1915 CmpReleaseKcbLockByKey(ConvKey2
);
1916 CmpReleaseKcbLockByKey(ConvKey1
);
1920 /* Release the first one first, then the second */
1921 if (Index1
!= Index2
)
1923 ASSERT((GET_HASH_ENTRY(CmpCacheTable
, ConvKey1
).Owner
== KeGetCurrentThread()) ||
1924 (CmpTestRegistryLockExclusive()));
1925 CmpReleaseKcbLockByKey(ConvKey1
);
1927 CmpReleaseKcbLockByKey(ConvKey2
);
1933 CmShutdownSystem(VOID
)
1935 /* Kill the workers and flush all hives */
1936 if (!CmFirstTime
) CmpShutdownWorkers();
1937 CmpDoFlushAll(TRUE
);
1942 CmpSetVersionData(VOID
)
1944 OBJECT_ATTRIBUTES ObjectAttributes
;
1945 UNICODE_STRING KeyName
;
1946 UNICODE_STRING ValueName
;
1947 UNICODE_STRING ValueData
;
1948 HANDLE SoftwareKeyHandle
= NULL
;
1949 HANDLE MicrosoftKeyHandle
= NULL
;
1950 HANDLE WindowsNtKeyHandle
= NULL
;
1951 HANDLE CurrentVersionKeyHandle
= NULL
;
1955 /* Open the 'CurrentVersion' key */
1956 RtlInitUnicodeString(&KeyName
,
1957 L
"\\REGISTRY\\MACHINE\\SOFTWARE");
1959 InitializeObjectAttributes(&ObjectAttributes
,
1961 OBJ_CASE_INSENSITIVE
,
1965 Status
= NtCreateKey(&SoftwareKeyHandle
,
1972 if (!NT_SUCCESS(Status
))
1974 DPRINT1("Failed to create key %wZ (Status: %08lx)\n", &KeyName
, Status
);
1978 /* Open the 'CurrentVersion' key */
1979 RtlInitUnicodeString(&KeyName
,
1982 InitializeObjectAttributes(&ObjectAttributes
,
1984 OBJ_CASE_INSENSITIVE
,
1988 Status
= NtCreateKey(&MicrosoftKeyHandle
,
1995 if (!NT_SUCCESS(Status
))
1997 DPRINT1("Failed to create key %wZ (Status: %08lx)\n", &KeyName
, Status
);
2001 /* Open the 'CurrentVersion' key */
2002 RtlInitUnicodeString(&KeyName
,
2005 InitializeObjectAttributes(&ObjectAttributes
,
2007 OBJ_CASE_INSENSITIVE
,
2011 Status
= NtCreateKey(&WindowsNtKeyHandle
,
2018 if (!NT_SUCCESS(Status
))
2020 DPRINT1("Failed to create key %wZ (Status: %08lx)\n", &KeyName
, Status
);
2024 /* Open the 'CurrentVersion' key */
2025 RtlInitUnicodeString(&KeyName
,
2028 InitializeObjectAttributes(&ObjectAttributes
,
2030 OBJ_CASE_INSENSITIVE
,
2034 Status
= NtCreateKey(&CurrentVersionKeyHandle
,
2035 KEY_CREATE_SUB_KEY
| KEY_SET_VALUE
,
2041 if (!NT_SUCCESS(Status
))
2043 DPRINT1("Failed to create key %wZ (Status: %08lx)\n", &KeyName
, Status
);
2047 /* Set the 'CurrentType' value */
2048 RtlInitUnicodeString(&ValueName
,
2052 wcscpy(Buffer
, L
"Multiprocessor");
2054 wcscpy(Buffer
, L
"Uniprocessor");
2057 wcscat(Buffer
, L
" ");
2060 wcscat(Buffer
, L
"Checked");
2062 wcscat(Buffer
, L
"Free");
2065 RtlInitUnicodeString(&ValueData
,
2068 NtSetValueKey(CurrentVersionKeyHandle
,
2073 ValueData
.Length
+ sizeof(WCHAR
));
2076 /* Close the keys */
2077 if (CurrentVersionKeyHandle
!= NULL
)
2078 NtClose(CurrentVersionKeyHandle
);
2080 if (WindowsNtKeyHandle
!= NULL
)
2081 NtClose(WindowsNtKeyHandle
);
2083 if (MicrosoftKeyHandle
!= NULL
)
2084 NtClose(MicrosoftKeyHandle
);
2086 if (SoftwareKeyHandle
!= NULL
)
2087 NtClose(SoftwareKeyHandle
);