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
;
311 CmpSetSystemValues(IN PLOADER_PARAMETER_BLOCK LoaderBlock
)
313 OBJECT_ATTRIBUTES ObjectAttributes
;
314 UNICODE_STRING KeyName
, ValueName
= { 0, 0, NULL
};
317 ASSERT(LoaderBlock
!= NULL
);
319 /* Setup attributes for loader options */
320 RtlInitUnicodeString(&KeyName
,
321 L
"\\REGISTRY\\MACHINE\\SYSTEM\\CurrentControlSet\\"
323 InitializeObjectAttributes(&ObjectAttributes
,
325 OBJ_CASE_INSENSITIVE
,
328 Status
= NtOpenKey(&KeyHandle
, KEY_WRITE
, &ObjectAttributes
);
329 if (!NT_SUCCESS(Status
)) goto Quickie
;
331 /* Key opened, now write to the key */
332 RtlInitUnicodeString(&KeyName
, L
"SystemStartOptions");
333 Status
= NtSetValueKey(KeyHandle
,
337 CmpLoadOptions
.Buffer
,
338 CmpLoadOptions
.Length
);
339 if (!NT_SUCCESS(Status
)) goto Quickie
;
341 /* Setup value name for system boot device */
342 RtlInitUnicodeString(&KeyName
, L
"SystemBootDevice");
343 RtlCreateUnicodeStringFromAsciiz(&ValueName
, LoaderBlock
->NtBootPathName
);
344 Status
= NtSetValueKey(KeyHandle
,
352 /* Free the buffers */
353 RtlFreeUnicodeString(&ValueName
);
355 /* Close the key and return */
358 /* Return the status */
359 return (ExpInTextModeSetup
? STATUS_SUCCESS
: Status
);
364 CmpCreateControlSet(IN PLOADER_PARAMETER_BLOCK LoaderBlock
)
366 UNICODE_STRING ConfigName
= RTL_CONSTANT_STRING(L
"Control\\IDConfigDB");
367 UNICODE_STRING SelectName
=
368 RTL_CONSTANT_STRING(L
"\\Registry\\Machine\\System\\Select");
369 UNICODE_STRING KeyName
;
370 OBJECT_ATTRIBUTES ObjectAttributes
;
371 CHAR ValueInfoBuffer
[128];
372 PKEY_VALUE_FULL_INFORMATION ValueInfo
;
374 WCHAR UnicodeBuffer
[128];
375 HANDLE SelectHandle
, KeyHandle
, ConfigHandle
= NULL
, ProfileHandle
= NULL
;
376 HANDLE ParentHandle
= NULL
;
377 ULONG ControlSet
, HwProfile
;
378 ANSI_STRING TempString
;
380 ULONG ResultLength
, Disposition
;
381 PLOADER_PARAMETER_EXTENSION LoaderExtension
;
384 /* Open the select key */
385 InitializeObjectAttributes(&ObjectAttributes
,
387 OBJ_CASE_INSENSITIVE
,
390 Status
= NtOpenKey(&SelectHandle
, KEY_READ
, &ObjectAttributes
);
391 if (!NT_SUCCESS(Status
))
393 /* ReactOS Hack: Hard-code current to 001 for SetupLdr */
394 if (!LoaderBlock
->RegistryBase
)
396 /* Build the ControlSet001 key */
397 RtlInitUnicodeString(&KeyName
,
398 L
"\\Registry\\Machine\\System\\ControlSet001");
399 InitializeObjectAttributes(&ObjectAttributes
,
401 OBJ_CASE_INSENSITIVE
,
404 Status
= NtCreateKey(&KeyHandle
,
411 if (!NT_SUCCESS(Status
)) return Status
;
413 /* Don't need the handle */
416 /* Use hard-coded setting */
421 /* Fail for real boots */
425 /* Open the current value */
426 RtlInitUnicodeString(&KeyName
, L
"Current");
427 Status
= NtQueryValueKey(SelectHandle
,
429 KeyValueFullInformation
,
431 sizeof(ValueInfoBuffer
),
433 NtClose(SelectHandle
);
434 if (!NT_SUCCESS(Status
)) return Status
;
436 /* Get the actual value pointer, and get the control set ID */
437 ValueInfo
= (PKEY_VALUE_FULL_INFORMATION
)ValueInfoBuffer
;
438 ControlSet
= *(PULONG
)((PUCHAR
)ValueInfo
+ ValueInfo
->DataOffset
);
440 /* Create the current control set key */
442 RtlInitUnicodeString(&KeyName
,
443 L
"\\Registry\\Machine\\System\\CurrentControlSet");
444 InitializeObjectAttributes(&ObjectAttributes
,
446 OBJ_CASE_INSENSITIVE
,
449 Status
= NtCreateKey(&KeyHandle
,
454 REG_OPTION_VOLATILE
| REG_OPTION_CREATE_LINK
,
456 if (!NT_SUCCESS(Status
)) return Status
;
459 ASSERT(Disposition
== REG_CREATED_NEW_KEY
);
461 /* Initialize the symbolic link name */
463 "\\Registry\\Machine\\System\\ControlSet%03ld",
465 RtlInitAnsiString(&TempString
, Buffer
);
467 /* Create a Unicode string out of it */
468 KeyName
.MaximumLength
= sizeof(UnicodeBuffer
);
469 KeyName
.Buffer
= UnicodeBuffer
;
470 Status
= RtlAnsiStringToUnicodeString(&KeyName
, &TempString
, FALSE
);
473 Status
= NtSetValueKey(KeyHandle
,
474 &CmSymbolicLinkValueName
,
479 if (!NT_SUCCESS(Status
)) return Status
;
481 /* Get the configuration database key */
482 InitializeObjectAttributes(&ObjectAttributes
,
484 OBJ_CASE_INSENSITIVE
,
487 Status
= NtOpenKey(&ConfigHandle
, KEY_READ
, &ObjectAttributes
);
490 /* Check if we don't have one */
491 if (!NT_SUCCESS(Status
))
493 /* Cleanup and exit */
498 /* Now get the current config */
499 RtlInitUnicodeString(&KeyName
, L
"CurrentConfig");
500 Status
= NtQueryValueKey(ConfigHandle
,
502 KeyValueFullInformation
,
504 sizeof(ValueInfoBuffer
),
507 /* Set pointer to buffer */
508 ValueInfo
= (PKEY_VALUE_FULL_INFORMATION
)ValueInfoBuffer
;
510 /* Check if we failed or got a non DWORD-value */
511 if (!(NT_SUCCESS(Status
)) || (ValueInfo
->Type
!= REG_DWORD
)) goto Cleanup
;
513 /* Get the hadware profile */
514 HwProfile
= *(PULONG
)((PUCHAR
)ValueInfo
+ ValueInfo
->DataOffset
);
516 /* Open the hardware profile key */
517 RtlInitUnicodeString(&KeyName
,
518 L
"\\Registry\\Machine\\System\\CurrentControlSet"
519 L
"\\Hardware Profiles");
520 InitializeObjectAttributes(&ObjectAttributes
,
522 OBJ_CASE_INSENSITIVE
,
525 Status
= NtOpenKey(&ParentHandle
, KEY_READ
, &ObjectAttributes
);
526 if (!NT_SUCCESS(Status
))
528 /* Exit and clean up */
533 /* Build the profile name */
534 sprintf(Buffer
, "%04ld", HwProfile
);
535 RtlInitAnsiString(&TempString
, Buffer
);
537 /* Convert it to Unicode */
538 KeyName
.MaximumLength
= sizeof(UnicodeBuffer
);
539 KeyName
.Buffer
= UnicodeBuffer
;
540 Status
= RtlAnsiStringToUnicodeString(&KeyName
,
543 ASSERT(Status
== STATUS_SUCCESS
);
545 /* Open the associated key */
546 InitializeObjectAttributes(&ObjectAttributes
,
548 OBJ_CASE_INSENSITIVE
,
551 Status
= NtOpenKey(&ProfileHandle
,
552 KEY_READ
| KEY_WRITE
,
554 if (!NT_SUCCESS (Status
))
556 /* Cleanup and exit */
561 /* Check if we have a loader block extension */
562 LoaderExtension
= LoaderBlock
->Extension
;
565 ASSERTMSG("ReactOS doesn't support NTLDR Profiles yet!\n", FALSE
);
568 /* Create the current hardware profile key */
569 RtlInitUnicodeString(&KeyName
,
570 L
"\\Registry\\Machine\\System\\CurrentControlSet\\"
571 L
"Hardware Profiles\\Current");
572 InitializeObjectAttributes(&ObjectAttributes
,
574 OBJ_CASE_INSENSITIVE
,
577 Status
= NtCreateKey(&KeyHandle
,
582 REG_OPTION_VOLATILE
| REG_OPTION_CREATE_LINK
,
584 if (NT_SUCCESS(Status
))
587 ASSERT(Disposition
== REG_CREATED_NEW_KEY
);
589 /* Create the profile name */
591 "\\Registry\\Machine\\System\\CurrentControlSet\\"
592 "Hardware Profiles\\%04ld",
594 RtlInitAnsiString(&TempString
, Buffer
);
596 /* Convert it to Unicode */
597 KeyName
.MaximumLength
= sizeof(UnicodeBuffer
);
598 KeyName
.Buffer
= UnicodeBuffer
;
599 Status
= RtlAnsiStringToUnicodeString(&KeyName
,
602 ASSERT(STATUS_SUCCESS
== Status
);
605 Status
= NtSetValueKey(KeyHandle
,
606 &CmSymbolicLinkValueName
,
614 /* Close every opened handle */
616 if (ConfigHandle
) NtClose(ConfigHandle
);
617 if (ProfileHandle
) NtClose(ProfileHandle
);
618 if (ParentHandle
) NtClose(ParentHandle
);
621 return STATUS_SUCCESS
;
626 CmpLinkHiveToMaster(IN PUNICODE_STRING LinkName
,
627 IN HANDLE RootDirectory
,
628 IN PCMHIVE RegistryHive
,
630 IN PSECURITY_DESCRIPTOR SecurityDescriptor
)
632 OBJECT_ATTRIBUTES ObjectAttributes
;
634 CM_PARSE_CONTEXT ParseContext
= {0};
636 PCM_KEY_BODY KeyBody
;
639 /* Setup the object attributes */
640 InitializeObjectAttributes(&ObjectAttributes
,
642 OBJ_CASE_INSENSITIVE
| OBJ_KERNEL_HANDLE
,
646 /* Setup the parse context */
647 ParseContext
.CreateLink
= TRUE
;
648 ParseContext
.CreateOperation
= TRUE
;
649 ParseContext
.ChildHive
.KeyHive
= &RegistryHive
->Hive
;
651 /* Check if we have a root keycell or if we need to create it */
655 ParseContext
.ChildHive
.KeyCell
= HCELL_NIL
;
660 ParseContext
.ChildHive
.KeyCell
= RegistryHive
->Hive
.BaseBlock
->RootCell
;
663 /* Create the link node */
664 Status
= ObOpenObjectByName(&ObjectAttributes
,
668 KEY_READ
| KEY_WRITE
,
669 (PVOID
)&ParseContext
,
671 if (!NT_SUCCESS(Status
)) return Status
;
673 /* Mark the hive as clean */
674 RegistryHive
->Hive
.DirtyFlag
= FALSE
;
676 /* ReactOS Hack: Keep alive */
677 Status
= ObReferenceObjectByHandle(KeyHandle
,
683 ASSERT(NT_SUCCESS(Status
));
685 /* Close the extra handle */
687 return STATUS_SUCCESS
;
692 CmpInitializeSystemHive(IN PLOADER_PARAMETER_BLOCK LoaderBlock
)
695 ANSI_STRING LoadString
;
700 UNICODE_STRING KeyName
;
701 PCMHIVE SystemHive
= NULL
;
702 UNICODE_STRING HiveName
= RTL_CONSTANT_STRING(L
"SYSTEM");
703 PSECURITY_DESCRIPTOR SecurityDescriptor
;
706 /* Setup the ansi string */
707 RtlInitAnsiString(&LoadString
, LoaderBlock
->LoadOptions
);
709 /* Allocate the unicode buffer */
710 Length
= LoadString
.Length
* sizeof(WCHAR
) + sizeof(UNICODE_NULL
);
711 Buffer
= ExAllocatePoolWithTag(PagedPool
, Length
, TAG_CM
);
715 KeBugCheckEx(BAD_SYSTEM_CONFIG_INFO
, 3, 1, (ULONG_PTR
)LoaderBlock
, 0);
718 /* Setup the unicode string */
719 RtlInitEmptyUnicodeString(&CmpLoadOptions
, Buffer
, (USHORT
)Length
);
721 /* Add the load options and null-terminate */
722 RtlAnsiStringToUnicodeString(&CmpLoadOptions
, &LoadString
, FALSE
);
723 CmpLoadOptions
.Buffer
[LoadString
.Length
] = UNICODE_NULL
;
724 CmpLoadOptions
.Length
+= sizeof(WCHAR
);
726 /* Get the System Hive base address */
727 HiveBase
= LoaderBlock
->RegistryBase
;
731 ((PHBASE_BLOCK
)HiveBase
)->Length
= LoaderBlock
->RegistryLength
;
732 Status
= CmpInitializeHive((PCMHIVE
*)&SystemHive
,
742 if (!NT_SUCCESS(Status
)) return FALSE
;
744 /* Set the hive filename */
745 RtlCreateUnicodeString(&SystemHive
->FileFullPath
,
746 L
"\\SystemRoot\\System32\\Config\\SYSTEM");
748 /* We imported, no need to create a new hive */
751 /* Manually set the hive as volatile, if in Live CD mode */
752 if (CmpShareSystemHives
) SystemHive
->Hive
.HiveFlags
= HIVE_VOLATILE
;
757 Status
= CmpInitializeHive(&SystemHive
,
767 if (!NT_SUCCESS(Status
)) return FALSE
;
769 /* Set the hive filename */
770 RtlCreateUnicodeString(&SystemHive
->FileFullPath
,
771 L
"\\SystemRoot\\System32\\Config\\SYSTEM");
773 /* Tell CmpLinkHiveToMaster to allocate a hive */
777 /* Save the boot type */
778 CmpBootType
= SystemHive
->Hive
.BaseBlock
->BootType
;
780 /* Are we in self-healing mode? */
783 /* Disable self-healing internally and check if boot type wanted it */
787 /* We're disabled, so bugcheck */
788 KeBugCheckEx(BAD_SYSTEM_CONFIG_INFO
,
791 (ULONG_PTR
)SystemHive
,
796 /* Create the default security descriptor */
797 SecurityDescriptor
= CmpHiveRootSecurityDescriptor();
799 /* Attach it to the system key */
800 RtlInitUnicodeString(&KeyName
, L
"\\Registry\\Machine\\SYSTEM");
801 Status
= CmpLinkHiveToMaster(&KeyName
,
807 /* Free the security descriptor */
808 ExFreePoolWithTag(SecurityDescriptor
, TAG_CM
);
809 if (!NT_SUCCESS(Status
)) return FALSE
;
811 /* Add the hive to the hive list */
812 CmpMachineHiveList
[3].CmHive
= (PCMHIVE
)SystemHive
;
820 CmpCreateObjectTypes(VOID
)
822 OBJECT_TYPE_INITIALIZER ObjectTypeInitializer
;
824 GENERIC_MAPPING CmpKeyMapping
= {KEY_READ
,
830 /* Initialize the Key object type */
831 RtlZeroMemory(&ObjectTypeInitializer
, sizeof(ObjectTypeInitializer
));
832 RtlInitUnicodeString(&Name
, L
"Key");
833 ObjectTypeInitializer
.Length
= sizeof(ObjectTypeInitializer
);
834 ObjectTypeInitializer
.DefaultPagedPoolCharge
= sizeof(CM_KEY_BODY
);
835 ObjectTypeInitializer
.GenericMapping
= CmpKeyMapping
;
836 ObjectTypeInitializer
.PoolType
= PagedPool
;
837 ObjectTypeInitializer
.ValidAccessMask
= KEY_ALL_ACCESS
;
838 ObjectTypeInitializer
.UseDefaultObject
= TRUE
;
839 ObjectTypeInitializer
.DeleteProcedure
= CmpDeleteKeyObject
;
840 ObjectTypeInitializer
.ParseProcedure
= CmpParseKey
;
841 ObjectTypeInitializer
.SecurityProcedure
= CmpSecurityMethod
;
842 ObjectTypeInitializer
.QueryNameProcedure
= CmpQueryKeyName
;
843 ObjectTypeInitializer
.CloseProcedure
= CmpCloseKeyObject
;
844 ObjectTypeInitializer
.SecurityRequired
= TRUE
;
847 return ObCreateObjectType(&Name
, &ObjectTypeInitializer
, NULL
, &CmpKeyObjectType
);
852 CmpCreateRootNode(IN PHHIVE Hive
,
854 OUT PHCELL_INDEX Index
)
856 UNICODE_STRING KeyName
;
857 PCM_KEY_NODE KeyCell
;
858 LARGE_INTEGER SystemTime
;
861 /* Initialize the node name and allocate it */
862 RtlInitUnicodeString(&KeyName
, Name
);
863 *Index
= HvAllocateCell(Hive
,
864 FIELD_OFFSET(CM_KEY_NODE
, Name
) +
865 CmpNameSize(Hive
, &KeyName
),
868 if (*Index
== HCELL_NIL
) return FALSE
;
870 /* Set the cell index and get the data */
871 Hive
->BaseBlock
->RootCell
= *Index
;
872 KeyCell
= (PCM_KEY_NODE
)HvGetCell(Hive
, *Index
);
873 if (!KeyCell
) return FALSE
;
876 KeyCell
->Signature
= (USHORT
)CM_KEY_NODE_SIGNATURE
;
877 KeyCell
->Flags
= KEY_HIVE_ENTRY
| KEY_NO_DELETE
;
878 KeQuerySystemTime(&SystemTime
);
879 KeyCell
->LastWriteTime
= SystemTime
;
880 KeyCell
->Parent
= HCELL_NIL
;
881 KeyCell
->SubKeyCounts
[Stable
] = 0;
882 KeyCell
->SubKeyCounts
[Volatile
] = 0;
883 KeyCell
->SubKeyLists
[Stable
] = HCELL_NIL
;
884 KeyCell
->SubKeyLists
[Volatile
] = HCELL_NIL
;
885 KeyCell
->ValueList
.Count
= 0;
886 KeyCell
->ValueList
.List
= HCELL_NIL
;
887 KeyCell
->Security
= HCELL_NIL
;
888 KeyCell
->Class
= HCELL_NIL
;
889 KeyCell
->ClassLength
= 0;
890 KeyCell
->MaxNameLen
= 0;
891 KeyCell
->MaxClassLen
= 0;
892 KeyCell
->MaxValueNameLen
= 0;
893 KeyCell
->MaxValueDataLen
= 0;
895 /* Copy the name (this will also set the length) */
896 KeyCell
->NameLength
= CmpCopyName(Hive
, (PWCHAR
)KeyCell
->Name
, &KeyName
);
898 /* Check if the name was compressed */
899 if (KeyCell
->NameLength
< KeyName
.Length
)
902 KeyCell
->Flags
|= KEY_COMP_NAME
;
906 HvReleaseCell(Hive
, *Index
);
912 CmpCreateRegistryRoot(VOID
)
914 UNICODE_STRING KeyName
;
915 OBJECT_ATTRIBUTES ObjectAttributes
;
916 PCM_KEY_BODY RootKey
;
917 HCELL_INDEX RootIndex
;
919 PCM_KEY_NODE KeyCell
;
920 PSECURITY_DESCRIPTOR SecurityDescriptor
;
921 PCM_KEY_CONTROL_BLOCK Kcb
;
924 /* Setup the root node */
925 if (!CmpCreateRootNode(&CmiVolatileHive
->Hive
, L
"REGISTRY", &RootIndex
))
931 /* Create '\Registry' key. */
932 RtlInitUnicodeString(&KeyName
, L
"\\REGISTRY");
933 SecurityDescriptor
= CmpHiveRootSecurityDescriptor();
934 InitializeObjectAttributes(&ObjectAttributes
,
936 OBJ_CASE_INSENSITIVE
,
939 Status
= ObCreateObject(KernelMode
,
948 ExFreePoolWithTag(SecurityDescriptor
, TAG_CM
);
949 if (!NT_SUCCESS(Status
)) return FALSE
;
951 /* Sanity check, and get the key cell */
952 ASSERT((&CmiVolatileHive
->Hive
)->ReleaseCellRoutine
== NULL
);
953 KeyCell
= (PCM_KEY_NODE
)HvGetCell(&CmiVolatileHive
->Hive
, RootIndex
);
954 if (!KeyCell
) return FALSE
;
957 RtlInitUnicodeString(&KeyName
, L
"\\REGISTRY");
958 Kcb
= CmpCreateKeyControlBlock(&CmiVolatileHive
->Hive
,
964 if (!Kcb
) return FALSE
;
966 /* Initialize the object */
967 RootKey
->KeyControlBlock
= Kcb
;
968 RootKey
->Type
= '20yk';
969 RootKey
->NotifyBlock
= NULL
;
970 RootKey
->ProcessID
= PsGetCurrentProcessId();
973 EnlistKeyBodyWithKCB(RootKey
, 0);
975 /* Insert the key into the namespace */
976 Status
= ObInsertObject(RootKey
,
981 &CmpRegistryRootHandle
);
982 if (!NT_SUCCESS(Status
)) return FALSE
;
984 /* Reference the key again so that we never lose it */
985 Status
= ObReferenceObjectByHandle(CmpRegistryRootHandle
,
991 if (!NT_SUCCESS(Status
)) return FALSE
;
993 /* Completely sucessful */
999 CmpGetRegistryPath(IN PWCHAR ConfigPath
)
1001 OBJECT_ATTRIBUTES ObjectAttributes
;
1004 PKEY_VALUE_PARTIAL_INFORMATION ValueInfo
;
1005 UNICODE_STRING KeyName
= RTL_CONSTANT_STRING(L
"\\Registry\\Machine\\HARDWARE");
1006 UNICODE_STRING ValueName
= RTL_CONSTANT_STRING(L
"InstallPath");
1007 ULONG BufferSize
, ResultSize
;
1009 /* Check if we are booted in setup */
1010 if (ExpInTextModeSetup
)
1012 /* Setup the object attributes */
1013 InitializeObjectAttributes(&ObjectAttributes
,
1015 OBJ_CASE_INSENSITIVE
,
1019 Status
= ZwOpenKey(&KeyHandle
,
1022 if (!NT_SUCCESS(Status
)) return Status
;
1024 /* Allocate the buffer */
1025 BufferSize
= sizeof(KEY_VALUE_PARTIAL_INFORMATION
) + 4096;
1026 ValueInfo
= ExAllocatePoolWithTag(PagedPool
, BufferSize
, TAG_CM
);
1031 return STATUS_INSUFFICIENT_RESOURCES
;
1034 /* Query the value */
1035 Status
= ZwQueryValueKey(KeyHandle
,
1037 KeyValuePartialInformation
,
1042 if (!NT_SUCCESS(Status
))
1045 ExFreePoolWithTag(ValueInfo
, TAG_CM
);
1049 /* Copy the config path and null-terminate it */
1050 RtlCopyMemory(ConfigPath
,
1052 ValueInfo
->DataLength
);
1053 ConfigPath
[ValueInfo
->DataLength
/ sizeof(WCHAR
)] = UNICODE_NULL
;
1054 ExFreePoolWithTag(ValueInfo
, TAG_CM
);
1058 /* Just use default path */
1059 wcscpy(ConfigPath
, L
"\\SystemRoot");
1062 /* Add registry path */
1063 wcscat(ConfigPath
, L
"\\System32\\Config\\");
1066 return STATUS_SUCCESS
;
1071 CmpLoadHiveThread(IN PVOID StartContext
)
1073 WCHAR FileBuffer
[MAX_PATH
], RegBuffer
[MAX_PATH
], ConfigPath
[MAX_PATH
];
1074 UNICODE_STRING TempName
, FileName
, RegName
;
1075 ULONG FileStart
, RegStart
, i
, ErrorResponse
, WorkerCount
, Length
;
1076 ULONG PrimaryDisposition
, SecondaryDisposition
, ClusterSize
;
1078 HANDLE PrimaryHandle
, LogHandle
;
1079 NTSTATUS Status
= STATUS_SUCCESS
;
1080 PVOID ErrorParameters
;
1083 /* Get the hive index, make sure it makes sense */
1084 i
= PtrToUlong(StartContext
);
1085 ASSERT(CmpMachineHiveList
[i
].Name
!= NULL
);
1087 /* We were started */
1088 CmpMachineHiveList
[i
].ThreadStarted
= TRUE
;
1090 /* Build the file name and registry name strings */
1091 RtlInitEmptyUnicodeString(&FileName
, FileBuffer
, MAX_PATH
);
1092 RtlInitEmptyUnicodeString(&RegName
, RegBuffer
, MAX_PATH
);
1094 /* Now build the system root path */
1095 CmpGetRegistryPath(ConfigPath
);
1096 RtlInitUnicodeString(&TempName
, ConfigPath
);
1097 RtlAppendStringToString((PSTRING
)&FileName
, (PSTRING
)&TempName
);
1098 FileStart
= FileName
.Length
;
1100 /* And build the registry root path */
1101 RtlInitUnicodeString(&TempName
, L
"\\REGISTRY\\");
1102 RtlAppendStringToString((PSTRING
)&RegName
, (PSTRING
)&TempName
);
1103 RegStart
= RegName
.Length
;
1105 /* Build the base name */
1106 RegName
.Length
= RegStart
;
1107 RtlInitUnicodeString(&TempName
, CmpMachineHiveList
[i
].BaseName
);
1108 RtlAppendStringToString((PSTRING
)&RegName
, (PSTRING
)&TempName
);
1110 /* Check if this is a child of the root */
1111 if (RegName
.Buffer
[RegName
.Length
/ sizeof(WCHAR
) - 1] == '\\')
1113 /* Then setup the whole name */
1114 RtlInitUnicodeString(&TempName
, CmpMachineHiveList
[i
].Name
);
1115 RtlAppendStringToString((PSTRING
)&RegName
, (PSTRING
)&TempName
);
1118 /* Now add the rest of the file name */
1119 RtlInitUnicodeString(&TempName
, CmpMachineHiveList
[i
].Name
);
1120 FileName
.Length
= FileStart
;
1121 RtlAppendStringToString((PSTRING
)&FileName
, (PSTRING
)&TempName
);
1122 if (!CmpMachineHiveList
[i
].CmHive
)
1124 /* We need to allocate a new hive structure */
1125 CmpMachineHiveList
[i
].Allocate
= TRUE
;
1127 /* Load the hive file */
1128 Status
= CmpInitHiveFromFile(&FileName
,
1129 CmpMachineHiveList
[i
].HHiveFlags
,
1131 &CmpMachineHiveList
[i
].Allocate
,
1133 if (!(NT_SUCCESS(Status
)) ||
1134 (!(CmHive
->FileHandles
[HFILE_TYPE_LOG
]) && !(CmpMiniNTBoot
))) // HACK
1136 /* We failed or couldn't get a log file, raise a hard error */
1137 ErrorParameters
= &FileName
;
1138 NtRaiseHardError(STATUS_CANNOT_LOAD_REGISTRY_FILE
,
1141 (PULONG_PTR
)&ErrorParameters
,
1146 /* Set the hive flags and newly allocated hive pointer */
1147 CmHive
->Flags
= CmpMachineHiveList
[i
].CmHiveFlags
;
1148 CmpMachineHiveList
[i
].CmHive2
= CmHive
;
1152 /* We already have a hive, is it volatile? */
1153 CmHive
= CmpMachineHiveList
[i
].CmHive
;
1154 if (!(CmHive
->Hive
.HiveFlags
& HIVE_VOLATILE
))
1156 /* It's now, open the hive file and log */
1157 Status
= CmpOpenHiveFiles(&FileName
,
1161 &PrimaryDisposition
,
1162 &SecondaryDisposition
,
1167 if (!(NT_SUCCESS(Status
)) || !(LogHandle
))
1169 /* Couldn't open the hive or its log file, raise a hard error */
1170 ErrorParameters
= &FileName
;
1171 NtRaiseHardError(STATUS_CANNOT_LOAD_REGISTRY_FILE
,
1174 (PULONG_PTR
)&ErrorParameters
,
1178 /* And bugcheck for posterity's sake */
1179 KeBugCheckEx(BAD_SYSTEM_CONFIG_INFO
, 9, 0, i
, Status
);
1182 /* Save the file handles. This should remove our sync hacks */
1183 CmHive
->FileHandles
[HFILE_TYPE_LOG
] = LogHandle
;
1184 CmHive
->FileHandles
[HFILE_TYPE_PRIMARY
] = PrimaryHandle
;
1186 /* Allow lazy flushing since the handles are there -- remove sync hacks */
1187 //ASSERT(CmHive->Hive.HiveFlags & HIVE_NOLAZYFLUSH);
1188 CmHive
->Hive
.HiveFlags
&= ~HIVE_NOLAZYFLUSH
;
1190 /* Get the real size of the hive */
1191 Length
= CmHive
->Hive
.Storage
[Stable
].Length
+ HBLOCK_SIZE
;
1193 /* Check if the cluster size doesn't match */
1194 if (CmHive
->Hive
.Cluster
!= ClusterSize
) ASSERT(FALSE
);
1196 /* Set the file size */
1197 DPRINT("FIXME: Should set file size: %lx\n", Length
);
1198 //if (!CmpFileSetSize((PHHIVE)CmHive, HFILE_TYPE_PRIMARY, Length, Length))
1200 /* This shouldn't fail */
1204 /* Another thing we don't support is NTLDR-recovery */
1205 if (CmHive
->Hive
.BaseBlock
->BootRecover
) ASSERT(FALSE
);
1207 /* Finally, set our allocated hive to the same hive we've had */
1208 CmpMachineHiveList
[i
].CmHive2
= CmHive
;
1209 ASSERT(CmpMachineHiveList
[i
].CmHive
== CmpMachineHiveList
[i
].CmHive2
);
1214 CmpMachineHiveList
[i
].ThreadFinished
= TRUE
;
1216 /* Check if we're the last worker */
1217 WorkerCount
= InterlockedIncrement(&CmpLoadWorkerIncrement
);
1218 if (WorkerCount
== CM_NUMBER_OF_MACHINE_HIVES
)
1220 /* Signal the event */
1221 KeSetEvent(&CmpLoadWorkerEvent
, 0, FALSE
);
1224 /* Kill the thread */
1225 PsTerminateSystemThread(Status
);
1230 CmpInitializeHiveList(IN USHORT Flag
)
1232 WCHAR FileBuffer
[MAX_PATH
], RegBuffer
[MAX_PATH
], ConfigPath
[MAX_PATH
];
1233 UNICODE_STRING TempName
, FileName
, RegName
;
1237 PSECURITY_DESCRIPTOR SecurityDescriptor
;
1240 /* Allow writing for now */
1243 /* Build the file name and registry name strings */
1244 RtlInitEmptyUnicodeString(&FileName
, FileBuffer
, MAX_PATH
);
1245 RtlInitEmptyUnicodeString(&RegName
, RegBuffer
, MAX_PATH
);
1247 /* Now build the system root path */
1248 CmpGetRegistryPath(ConfigPath
);
1249 RtlInitUnicodeString(&TempName
, ConfigPath
);
1250 RtlAppendStringToString((PSTRING
)&FileName
, (PSTRING
)&TempName
);
1252 /* And build the registry root path */
1253 RtlInitUnicodeString(&TempName
, L
"\\REGISTRY\\");
1254 RtlAppendStringToString((PSTRING
)&RegName
, (PSTRING
)&TempName
);
1255 RegStart
= RegName
.Length
;
1257 /* Setup the event to synchronize workers */
1258 KeInitializeEvent(&CmpLoadWorkerEvent
, SynchronizationEvent
, FALSE
);
1260 /* Enter special boot condition */
1261 CmpSpecialBootCondition
= TRUE
;
1263 /* Create the SD for the root hives */
1264 SecurityDescriptor
= CmpHiveRootSecurityDescriptor();
1266 /* Loop every hive we care about */
1267 for (i
= 0; i
< CM_NUMBER_OF_MACHINE_HIVES
; i
++)
1269 /* Make sure the list is setup */
1270 ASSERT(CmpMachineHiveList
[i
].Name
!= NULL
);
1272 /* Create a thread to handle this hive */
1273 Status
= PsCreateSystemThread(&Thread
,
1280 if (NT_SUCCESS(Status
))
1282 /* We don't care about the handle -- the thread self-terminates */
1287 /* Can't imagine this happening */
1288 KeBugCheckEx(BAD_SYSTEM_CONFIG_INFO
, 9, 3, i
, Status
);
1292 /* Make sure we've reached the end of the list */
1293 ASSERT(CmpMachineHiveList
[i
].Name
== NULL
);
1295 /* Wait for hive loading to finish */
1296 KeWaitForSingleObject(&CmpLoadWorkerEvent
,
1302 /* Exit the special boot condition and make sure all workers completed */
1303 CmpSpecialBootCondition
= FALSE
;
1304 ASSERT(CmpLoadWorkerIncrement
== CM_NUMBER_OF_MACHINE_HIVES
);
1306 /* Loop hives again */
1307 for (i
= 0; i
< CM_NUMBER_OF_MACHINE_HIVES
; i
++)
1309 /* Make sure the thread ran and finished */
1310 ASSERT(CmpMachineHiveList
[i
].ThreadFinished
== TRUE
);
1311 ASSERT(CmpMachineHiveList
[i
].ThreadStarted
== TRUE
);
1313 /* Check if this was a new hive */
1314 if (!CmpMachineHiveList
[i
].CmHive
)
1316 /* Make sure we allocated something */
1317 ASSERT(CmpMachineHiveList
[i
].CmHive2
!= NULL
);
1319 /* Build the base name */
1320 RegName
.Length
= RegStart
;
1321 RtlInitUnicodeString(&TempName
, CmpMachineHiveList
[i
].BaseName
);
1322 RtlAppendStringToString((PSTRING
)&RegName
, (PSTRING
)&TempName
);
1324 /* Check if this is a child of the root */
1325 if (RegName
.Buffer
[RegName
.Length
/ sizeof(WCHAR
) - 1] == '\\')
1327 /* Then setup the whole name */
1328 RtlInitUnicodeString(&TempName
, CmpMachineHiveList
[i
].Name
);
1329 RtlAppendStringToString((PSTRING
)&RegName
, (PSTRING
)&TempName
);
1332 /* Now link the hive to its master */
1333 Status
= CmpLinkHiveToMaster(&RegName
,
1335 CmpMachineHiveList
[i
].CmHive2
,
1336 CmpMachineHiveList
[i
].Allocate
,
1337 SecurityDescriptor
);
1338 if (Status
!= STATUS_SUCCESS
)
1340 /* Linking needs to work */
1341 KeBugCheckEx(CONFIG_LIST_FAILED
, 11, Status
, i
, (ULONG_PTR
)&RegName
);
1344 /* Check if we had to allocate a new hive */
1345 if (CmpMachineHiveList
[i
].Allocate
)
1347 /* Sync the new hive */
1348 //HvSyncHive((PHHIVE)(CmpMachineHiveList[i].CmHive2));
1352 /* Check if we created a new hive */
1353 if (CmpMachineHiveList
[i
].CmHive2
)
1355 /* TODO: Add to HiveList key */
1359 /* Get rid of the SD */
1360 ExFreePoolWithTag(SecurityDescriptor
, TAG_CM
);
1362 /* FIXME: Link SECURITY to SAM */
1364 /* FIXME: Link S-1-5-18 to .Default */
1371 OBJECT_ATTRIBUTES ObjectAttributes
;
1372 UNICODE_STRING KeyName
;
1375 PCMHIVE HardwareHive
;
1376 PSECURITY_DESCRIPTOR SecurityDescriptor
;
1379 /* Check if this is PE-boot */
1380 if (InitIsWinPEMode
)
1382 /* Set registry to PE mode */
1383 CmpMiniNTBoot
= TRUE
;
1384 CmpShareSystemHives
= TRUE
;
1387 /* Initialize the hive list and lock */
1388 InitializeListHead(&CmpHiveListHead
);
1389 ExInitializePushLock((PVOID
)&CmpHiveListHeadLock
);
1390 ExInitializePushLock((PVOID
)&CmpLoadHiveLock
);
1392 /* Initialize registry lock */
1393 ExInitializeResourceLite(&CmpRegistryLock
);
1395 /* Initialize the cache */
1396 CmpInitializeCache();
1398 /* Initialize allocation and delayed dereferencing */
1399 CmpInitCmPrivateAlloc();
1400 CmpInitCmPrivateDelayAlloc();
1401 CmpInitDelayDerefKCBEngine();
1403 /* Initialize callbacks */
1406 /* Initialize self healing */
1407 KeInitializeGuardedMutex(&CmpSelfHealQueueLock
);
1408 InitializeListHead(&CmpSelfHealQueueListHead
);
1410 /* Save the current process and lock the registry */
1411 CmpSystemProcess
= PsGetCurrentProcess();
1413 /* Create the key object types */
1414 Status
= CmpCreateObjectTypes();
1415 if (!NT_SUCCESS(Status
))
1418 KeBugCheckEx(CONFIG_INITIALIZATION_FAILED
, 1, 1, Status
, 0);
1421 /* Build the master hive */
1422 Status
= CmpInitializeHive((PCMHIVE
*)&CmiVolatileHive
,
1432 if (!NT_SUCCESS(Status
))
1435 KeBugCheckEx(CONFIG_INITIALIZATION_FAILED
, 1, 2, Status
, 0);
1438 /* Create the \REGISTRY key node */
1439 if (!CmpCreateRegistryRoot())
1442 KeBugCheckEx(CONFIG_INITIALIZATION_FAILED
, 1, 3, 0, 0);
1445 /* Create the default security descriptor */
1446 SecurityDescriptor
= CmpHiveRootSecurityDescriptor();
1448 /* Create '\Registry\Machine' key. */
1449 RtlInitUnicodeString(&KeyName
, L
"\\REGISTRY\\MACHINE");
1450 InitializeObjectAttributes(&ObjectAttributes
,
1452 OBJ_CASE_INSENSITIVE
,
1454 SecurityDescriptor
);
1455 Status
= NtCreateKey(&KeyHandle
,
1456 KEY_READ
| KEY_WRITE
,
1462 if (!NT_SUCCESS(Status
))
1465 KeBugCheckEx(CONFIG_INITIALIZATION_FAILED
, 1, 5, Status
, 0);
1468 /* Close the handle */
1471 /* Create '\Registry\User' key. */
1472 RtlInitUnicodeString(&KeyName
, L
"\\REGISTRY\\USER");
1473 InitializeObjectAttributes(&ObjectAttributes
,
1475 OBJ_CASE_INSENSITIVE
,
1477 SecurityDescriptor
);
1478 Status
= NtCreateKey(&KeyHandle
,
1479 KEY_READ
| KEY_WRITE
,
1485 if (!NT_SUCCESS(Status
))
1488 KeBugCheckEx(CONFIG_INITIALIZATION_FAILED
, 1, 6, Status
, 0);
1491 /* Close the handle */
1494 /* Initialize the system hive */
1495 if (!CmpInitializeSystemHive(KeLoaderBlock
))
1498 KeBugCheckEx(CONFIG_INITIALIZATION_FAILED
, 1, 7, 0, 0);
1501 /* Create the 'CurrentControlSet' link. */
1502 Status
= CmpCreateControlSet(KeLoaderBlock
);
1503 if (!NT_SUCCESS(Status
))
1506 KeBugCheckEx(CONFIG_INITIALIZATION_FAILED
, 1, 8, Status
, 0);
1509 /* Create the hardware hive */
1510 Status
= CmpInitializeHive((PCMHIVE
*)&HardwareHive
,
1520 if (!NT_SUCCESS(Status
))
1523 KeBugCheckEx(CONFIG_INITIALIZATION_FAILED
, 1, 11, Status
, 0);
1526 /* Add the hive to the hive list */
1527 CmpMachineHiveList
[0].CmHive
= (PCMHIVE
)HardwareHive
;
1529 /* Attach it to the machine key */
1530 RtlInitUnicodeString(&KeyName
, L
"\\Registry\\Machine\\HARDWARE");
1531 Status
= CmpLinkHiveToMaster(&KeyName
,
1533 (PCMHIVE
)HardwareHive
,
1535 SecurityDescriptor
);
1536 if (!NT_SUCCESS(Status
))
1539 KeBugCheckEx(CONFIG_INITIALIZATION_FAILED
, 1, 12, Status
, 0);
1542 /* FIXME: Add to HiveList key */
1544 /* Free the security descriptor */
1545 ExFreePoolWithTag(SecurityDescriptor
, TAG_CM
);
1547 /* Fill out the Hardware key with the ARC Data from the Loader */
1548 Status
= CmpInitializeHardwareConfiguration(KeLoaderBlock
);
1549 if (!NT_SUCCESS(Status
))
1552 KeBugCheckEx(CONFIG_INITIALIZATION_FAILED
, 1, 13, Status
, 0);
1555 /* Initialize machine-dependent information into the registry */
1556 Status
= CmpInitializeMachineDependentConfiguration(KeLoaderBlock
);
1557 if (!NT_SUCCESS(Status
))
1560 KeBugCheckEx(CONFIG_INITIALIZATION_FAILED
, 1, 14, Status
, 0);
1563 /* Initialize volatile registry settings */
1564 Status
= CmpSetSystemValues(KeLoaderBlock
);
1565 if (!NT_SUCCESS(Status
))
1568 KeBugCheckEx(CONFIG_INITIALIZATION_FAILED
, 1, 15, Status
, 0);
1571 /* Free the load options */
1572 ExFreePoolWithTag(CmpLoadOptions
.Buffer
, TAG_CM
);
1574 /* If we got here, all went well */
1580 CmpFreeDriverList(IN PHHIVE Hive
,
1581 IN PLIST_ENTRY DriverList
)
1583 PLIST_ENTRY NextEntry
, OldEntry
;
1584 PBOOT_DRIVER_NODE DriverNode
;
1587 /* Parse the current list */
1588 NextEntry
= DriverList
->Flink
;
1589 while (NextEntry
!= DriverList
)
1591 /* Get the driver node */
1592 DriverNode
= CONTAINING_RECORD(NextEntry
, BOOT_DRIVER_NODE
, ListEntry
.Link
);
1594 /* Get the next entry now, since we're going to free it later */
1595 OldEntry
= NextEntry
;
1596 NextEntry
= NextEntry
->Flink
;
1598 /* Was there a name? */
1599 if (DriverNode
->Name
.Buffer
)
1602 CmpFree(DriverNode
->Name
.Buffer
, DriverNode
->Name
.Length
);
1605 /* Was there a registry path? */
1606 if (DriverNode
->ListEntry
.RegistryPath
.Buffer
)
1609 CmpFree(DriverNode
->ListEntry
.RegistryPath
.Buffer
,
1610 DriverNode
->ListEntry
.RegistryPath
.MaximumLength
);
1613 /* Was there a file path? */
1614 if (DriverNode
->ListEntry
.FilePath
.Buffer
)
1617 CmpFree(DriverNode
->ListEntry
.FilePath
.Buffer
,
1618 DriverNode
->ListEntry
.FilePath
.MaximumLength
);
1621 /* Now free the node, and move on */
1622 CmpFree(OldEntry
, sizeof(BOOT_DRIVER_NODE
));
1628 CmGetSystemDriverList(VOID
)
1630 LIST_ENTRY DriverList
;
1631 OBJECT_ATTRIBUTES ObjectAttributes
;
1633 PCM_KEY_BODY KeyBody
;
1635 HCELL_INDEX RootCell
, ControlCell
;
1637 UNICODE_STRING KeyName
;
1638 PLIST_ENTRY NextEntry
;
1640 PUNICODE_STRING
* ServicePath
= NULL
;
1641 BOOLEAN Success
, AutoSelect
;
1642 PBOOT_DRIVER_LIST_ENTRY DriverEntry
;
1645 /* Initialize the driver list */
1646 InitializeListHead(&DriverList
);
1648 /* Open the system hive key */
1649 RtlInitUnicodeString(&KeyName
, L
"\\Registry\\Machine\\System");
1650 InitializeObjectAttributes(&ObjectAttributes
,
1652 OBJ_CASE_INSENSITIVE
,
1655 Status
= NtOpenKey(&KeyHandle
, KEY_READ
, &ObjectAttributes
);
1656 if (!NT_SUCCESS(Status
)) return NULL
;
1658 /* Reference the key object to get the root hive/cell to access directly */
1659 Status
= ObReferenceObjectByHandle(KeyHandle
,
1665 if (!NT_SUCCESS(Status
))
1672 /* Do all this under the registry lock */
1673 CmpLockRegistryExclusive();
1675 /* Get the hive and key cell */
1676 Hive
= KeyBody
->KeyControlBlock
->KeyHive
;
1677 RootCell
= KeyBody
->KeyControlBlock
->KeyCell
;
1679 /* Open the current control set key */
1680 RtlInitUnicodeString(&KeyName
, L
"Current");
1681 ControlCell
= CmpFindControlSet(Hive
, RootCell
, &KeyName
, &AutoSelect
);
1682 if (ControlCell
== HCELL_NIL
) goto EndPath
;
1684 /* Find all system drivers */
1685 Success
= CmpFindDrivers(Hive
, ControlCell
, SystemLoad
, NULL
, &DriverList
);
1686 if (!Success
) goto EndPath
;
1688 /* Sort by group/tag */
1689 if (!CmpSortDriverList(Hive
, ControlCell
, &DriverList
)) goto EndPath
;
1691 /* Remove circular dependencies (cycles) and sort */
1692 if (!CmpResolveDriverDependencies(&DriverList
)) goto EndPath
;
1694 /* Loop the list to count drivers */
1695 for (i
= 0, NextEntry
= DriverList
.Flink
;
1696 NextEntry
!= &DriverList
;
1697 i
++, NextEntry
= NextEntry
->Flink
);
1699 /* Allocate the array */
1700 ServicePath
= ExAllocatePool(NonPagedPool
, (i
+ 1) * sizeof(PUNICODE_STRING
));
1701 if (!ServicePath
) KeBugCheckEx(CONFIG_INITIALIZATION_FAILED
, 2, 1, 0, 0);
1703 /* Loop the driver list */
1704 for (i
= 0, NextEntry
= DriverList
.Flink
;
1705 NextEntry
!= &DriverList
;
1706 i
++, NextEntry
= NextEntry
->Flink
)
1709 DriverEntry
= CONTAINING_RECORD(NextEntry
, BOOT_DRIVER_LIST_ENTRY
, Link
);
1711 /* Allocate the path for the caller and duplicate the registry path */
1712 ServicePath
[i
] = ExAllocatePool(NonPagedPool
, sizeof(UNICODE_STRING
));
1713 RtlDuplicateUnicodeString(RTL_DUPLICATE_UNICODE_STRING_NULL_TERMINATE
,
1714 &DriverEntry
->RegistryPath
,
1718 /* Terminate the list */
1719 ServicePath
[i
] = NULL
;
1722 /* Free the driver list if we had one */
1723 if (!IsListEmpty(&DriverList
)) CmpFreeDriverList(Hive
, &DriverList
);
1725 /* Unlock the registry */
1726 CmpUnlockRegistry();
1728 /* Close the key handle and dereference the object, then return the path */
1729 ObDereferenceObject(KeyBody
);
1736 CmpLockRegistryExclusive(VOID
)
1738 /* Enter a critical region and lock the registry */
1739 KeEnterCriticalRegion();
1740 ExAcquireResourceExclusiveLite(&CmpRegistryLock
, TRUE
);
1743 ASSERT(CmpFlushStarveWriters
== 0);
1744 RtlGetCallersAddress(&CmpRegistryLockCaller
, &CmpRegistryLockCallerCaller
);
1749 CmpLockRegistry(VOID
)
1751 /* Enter a critical region */
1752 KeEnterCriticalRegion();
1754 /* Check if we have to starve writers */
1755 if (CmpFlushStarveWriters
)
1757 /* Starve exlusive waiters */
1758 ExAcquireSharedStarveExclusive(&CmpRegistryLock
, TRUE
);
1762 /* Just grab the lock */
1763 ExAcquireResourceSharedLite(&CmpRegistryLock
, TRUE
);
1769 CmpTestRegistryLock(VOID
)
1772 return !ExIsResourceAcquiredSharedLite(&CmpRegistryLock
) ? FALSE
: TRUE
;
1777 CmpTestRegistryLockExclusive(VOID
)
1780 return !ExIsResourceAcquiredExclusiveLite(&CmpRegistryLock
) ? FALSE
: TRUE
;
1785 CmpLockHiveFlusherExclusive(IN PCMHIVE Hive
)
1787 /* Lock the flusher. We should already be in a critical section */
1788 CMP_ASSERT_REGISTRY_LOCK_OR_LOADING(Hive
);
1789 ASSERT((ExIsResourceAcquiredShared(Hive
->FlusherLock
) == 0) &&
1790 (ExIsResourceAcquiredExclusiveLite(Hive
->FlusherLock
) == 0));
1791 ExAcquireResourceExclusiveLite(Hive
->FlusherLock
, TRUE
);
1796 CmpLockHiveFlusherShared(IN PCMHIVE Hive
)
1798 /* Lock the flusher. We should already be in a critical section */
1799 CMP_ASSERT_REGISTRY_LOCK_OR_LOADING(Hive
);
1800 ASSERT((ExIsResourceAcquiredShared(Hive
->FlusherLock
) == 0) &&
1801 (ExIsResourceAcquiredExclusiveLite(Hive
->FlusherLock
) == 0));
1802 ExAcquireResourceSharedLite(Hive
->FlusherLock
, TRUE
);
1807 CmpUnlockHiveFlusher(IN PCMHIVE Hive
)
1810 CMP_ASSERT_REGISTRY_LOCK_OR_LOADING(Hive
);
1811 CMP_ASSERT_FLUSH_LOCK(Hive
);
1813 /* Release the lock */
1814 ExReleaseResourceLite(Hive
->FlusherLock
);
1819 CmpTestHiveFlusherLockShared(IN PCMHIVE Hive
)
1822 return !ExIsResourceAcquiredSharedLite(Hive
->FlusherLock
) ? FALSE
: TRUE
;
1827 CmpTestHiveFlusherLockExclusive(IN PCMHIVE Hive
)
1830 return !ExIsResourceAcquiredExclusiveLite(Hive
->FlusherLock
) ? FALSE
: TRUE
;
1835 CmpUnlockRegistry(VOID
)
1838 CMP_ASSERT_REGISTRY_LOCK();
1840 /* Check if we should flush the registry */
1841 if (CmpFlushOnLockRelease
)
1843 /* The registry should be exclusively locked for this */
1844 CMP_ASSERT_EXCLUSIVE_REGISTRY_LOCK();
1846 /* Flush the registry */
1847 CmpDoFlushAll(TRUE
);
1848 CmpFlushOnLockRelease
= FALSE
;
1851 /* Release the lock and leave the critical region */
1852 ExReleaseResourceLite(&CmpRegistryLock
);
1853 KeLeaveCriticalRegion();
1858 CmpAcquireTwoKcbLocksExclusiveByKey(IN ULONG ConvKey1
,
1861 ULONG Index1
, Index2
;
1864 CMP_ASSERT_REGISTRY_LOCK();
1866 /* Get hash indexes */
1867 Index1
= GET_HASH_INDEX(ConvKey1
);
1868 Index2
= GET_HASH_INDEX(ConvKey2
);
1870 /* See which one is highest */
1871 if (Index1
< Index2
)
1873 /* Grab them in the proper order */
1874 CmpAcquireKcbLockExclusiveByKey(ConvKey1
);
1875 CmpAcquireKcbLockExclusiveByKey(ConvKey2
);
1879 /* Grab the second one first, then the first */
1880 CmpAcquireKcbLockExclusiveByKey(ConvKey2
);
1881 if (Index1
!= Index2
) CmpAcquireKcbLockExclusiveByKey(ConvKey1
);
1887 CmpReleaseTwoKcbLockByKey(IN ULONG ConvKey1
,
1890 ULONG Index1
, Index2
;
1893 CMP_ASSERT_REGISTRY_LOCK();
1895 /* Get hash indexes */
1896 Index1
= GET_HASH_INDEX(ConvKey1
);
1897 Index2
= GET_HASH_INDEX(ConvKey2
);
1898 ASSERT((GET_HASH_ENTRY(CmpCacheTable
, ConvKey2
).Owner
== KeGetCurrentThread()) ||
1899 (CmpTestRegistryLockExclusive()));
1901 /* See which one is highest */
1902 if (Index1
< Index2
)
1904 /* Grab them in the proper order */
1905 ASSERT((GET_HASH_ENTRY(CmpCacheTable
, ConvKey1
).Owner
== KeGetCurrentThread()) ||
1906 (CmpTestRegistryLockExclusive()));
1907 CmpReleaseKcbLockByKey(ConvKey2
);
1908 CmpReleaseKcbLockByKey(ConvKey1
);
1912 /* Release the first one first, then the second */
1913 if (Index1
!= Index2
)
1915 ASSERT((GET_HASH_ENTRY(CmpCacheTable
, ConvKey1
).Owner
== KeGetCurrentThread()) ||
1916 (CmpTestRegistryLockExclusive()));
1917 CmpReleaseKcbLockByKey(ConvKey1
);
1919 CmpReleaseKcbLockByKey(ConvKey2
);
1925 CmShutdownSystem(VOID
)
1927 /* Kill the workers and flush all hives */
1928 if (!CmFirstTime
) CmpShutdownWorkers();
1929 CmpDoFlushAll(TRUE
);