[NTOSKRNL]: Fix what should be at least 40 KmTest:ob failures, by correctly using...
[reactos.git] / reactos / ntoskrnl / config / cmsysini.c
1 /*
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)
8 */
9
10 /* INCLUDES *******************************************************************/
11
12 #include "ntoskrnl.h"
13 #define NDEBUG
14 #include "debug.h"
15
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;
29 BOOLEAN CmpNoWrite;
30 BOOLEAN CmpWasSetupBoot;
31 BOOLEAN CmpProfileLoaded;
32 ULONG CmpTraceLevel = 0;
33
34 extern LONG CmpFlushStarveWriters;
35 extern BOOLEAN CmFirstTime;
36
37 /* FUNCTIONS ******************************************************************/
38
39 VOID
40 NTAPI
41 CmpDeleteKeyObject(PVOID DeletedObject)
42 {
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;
47 NTSTATUS Status;
48 PAGED_CODE();
49
50 /* First off, prepare the handle close information callback */
51 PostOperationInfo.Object = KeyBody;
52 KeyHandleCloseInfo.Object = KeyBody;
53 Status = CmiCallRegisteredCallbacks(RegNtPreKeyHandleClose,
54 &KeyHandleCloseInfo);
55 if (!NT_SUCCESS(Status))
56 {
57 /* If we failed, notify the post routine */
58 PostOperationInfo.Status = Status;
59 CmiCallRegisteredCallbacks(RegNtPostKeyHandleClose, &PostOperationInfo);
60 return;
61 }
62
63 /* Acquire hive lock */
64 CmpLockRegistry();
65
66 /* Make sure this is a valid key body */
67 if (KeyBody->Type == '20yk')
68 {
69 /* Get the KCB */
70 Kcb = KeyBody->KeyControlBlock;
71 if (Kcb)
72 {
73 /* Delist the key */
74 DelistKeyBodyFromKCB(KeyBody, FALSE);
75
76 /* Dereference the KCB */
77 CmpDelayDerefKeyControlBlock(Kcb);
78 }
79 }
80
81 /* Release the registry lock */
82 CmpUnlockRegistry();
83
84 /* Do the post callback */
85 PostOperationInfo.Status = STATUS_SUCCESS;
86 CmiCallRegisteredCallbacks(RegNtPostKeyHandleClose, &PostOperationInfo);
87 }
88
89 VOID
90 NTAPI
91 CmpCloseKeyObject(IN PEPROCESS Process OPTIONAL,
92 IN PVOID Object,
93 IN ACCESS_MASK GrantedAccess,
94 IN ULONG ProcessHandleCount,
95 IN ULONG SystemHandleCount)
96 {
97 PCM_KEY_BODY KeyBody = (PCM_KEY_BODY)Object;
98 PAGED_CODE();
99
100 /* Don't do anything if we're not the last handle */
101 if (SystemHandleCount > 1) return;
102
103 /* Make sure we're a valid key body */
104 if (KeyBody->Type == '20yk')
105 {
106 /* Don't do anything if we don't have a notify block */
107 if (!KeyBody->NotifyBlock) return;
108
109 /* This shouldn't happen yet */
110 ASSERT(FALSE);
111 }
112 }
113
114 NTSTATUS
115 NTAPI
116 CmpQueryKeyName(IN PVOID ObjectBody,
117 IN BOOLEAN HasName,
118 IN OUT POBJECT_NAME_INFORMATION ObjectNameInfo,
119 IN ULONG Length,
120 OUT PULONG ReturnLength,
121 IN KPROCESSOR_MODE PreviousMode)
122 {
123 PUNICODE_STRING KeyName;
124 ULONG BytesToCopy;
125 NTSTATUS Status = STATUS_SUCCESS;
126 PCM_KEY_BODY KeyBody = (PCM_KEY_BODY)ObjectBody;
127 PCM_KEY_CONTROL_BLOCK Kcb = KeyBody->KeyControlBlock;
128
129 /* Acquire hive lock */
130 CmpLockRegistry();
131
132 /* Lock KCB shared */
133 CmpAcquireKcbLockShared(Kcb);
134
135 /* Check if it's a deleted block */
136 if (Kcb->Delete)
137 {
138 /* Release the locks */
139 CmpReleaseKcbLock(Kcb);
140 CmpUnlockRegistry();
141
142 /* Let the caller know it's deleted */
143 return STATUS_KEY_DELETED;
144 }
145
146 /* Get the name */
147 KeyName = CmpConstructName(Kcb);
148
149 /* Release the locks */
150 CmpReleaseKcbLock(Kcb);
151 CmpUnlockRegistry();
152
153 /* Check if we got the name */
154 if (!KeyName) return STATUS_INSUFFICIENT_RESOURCES;
155
156 /* Set the returned length */
157 *ReturnLength = KeyName->Length + sizeof(OBJECT_NAME_INFORMATION) + sizeof(WCHAR);
158
159 /* Calculate amount of bytes to copy into the buffer */
160 BytesToCopy = KeyName->Length + sizeof(WCHAR);
161
162 /* Check if the provided buffer is too small to fit even anything */
163 if ((Length <= sizeof(OBJECT_NAME_INFORMATION)) ||
164 ((Length < (*ReturnLength)) && (BytesToCopy < sizeof(WCHAR))))
165 {
166 /* Free the buffer allocated by CmpConstructName */
167 ExFreePool(KeyName);
168
169 /* Return buffer length failure without writing anything there because nothing fits */
170 return STATUS_INFO_LENGTH_MISMATCH;
171 }
172
173 /* Check if the provided buffer can be partially written */
174 if (Length < (*ReturnLength))
175 {
176 /* Yes, indicate so in the return status */
177 Status = STATUS_INFO_LENGTH_MISMATCH;
178
179 /* Calculate amount of bytes which the provided buffer could handle */
180 BytesToCopy = Length - sizeof(OBJECT_NAME_INFORMATION);
181 }
182
183 /* Remove the null termination character from the size */
184 BytesToCopy -= sizeof(WCHAR);
185
186 /* Fill in the result */
187 _SEH2_TRY
188 {
189 /* Return data to user */
190 ObjectNameInfo->Name.Buffer = (PWCHAR)(ObjectNameInfo + 1);
191 ObjectNameInfo->Name.MaximumLength = KeyName->Length;
192 ObjectNameInfo->Name.Length = KeyName->Length;
193
194 /* Copy string content*/
195 RtlCopyMemory(ObjectNameInfo->Name.Buffer,
196 KeyName->Buffer,
197 BytesToCopy);
198
199 /* Null terminate it */
200 ObjectNameInfo->Name.Buffer[BytesToCopy / sizeof(WCHAR)] = 0;
201 }
202 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER)
203 {
204 /* Get the status */
205 Status = _SEH2_GetExceptionCode();
206 }
207 _SEH2_END;
208
209 /* Free the buffer allocated by CmpConstructName */
210 ExFreePool(KeyName);
211
212 /* Return status */
213 return Status;
214 }
215
216 NTSTATUS
217 NTAPI
218 CmpInitHiveFromFile(IN PCUNICODE_STRING HiveName,
219 IN ULONG HiveFlags,
220 OUT PCMHIVE *Hive,
221 IN OUT PBOOLEAN New,
222 IN ULONG CheckFlags)
223 {
224 ULONG HiveDisposition, LogDisposition;
225 HANDLE FileHandle = NULL, LogHandle = NULL;
226 NTSTATUS Status;
227 ULONG Operation, FileType;
228 PCMHIVE NewHive;
229 PAGED_CODE();
230
231 /* Assume failure */
232 *Hive = NULL;
233
234 /* Open or create the hive files */
235 Status = CmpOpenHiveFiles(HiveName,
236 L".LOG",
237 &FileHandle,
238 &LogHandle,
239 &HiveDisposition,
240 &LogDisposition,
241 *New,
242 FALSE,
243 TRUE,
244 NULL);
245 if (!NT_SUCCESS(Status)) return Status;
246
247 /* Check if we have a log handle */
248 FileType = (LogHandle) ? HFILE_TYPE_LOG : HFILE_TYPE_PRIMARY;
249
250 /* Check if we created or opened the hive */
251 if (HiveDisposition == FILE_CREATED)
252 {
253 /* Do a create operation */
254 Operation = HINIT_CREATE;
255 *New = TRUE;
256 }
257 else
258 {
259 /* Open it as a file */
260 Operation = HINIT_FILE;
261 *New = FALSE;
262 }
263
264 /* Check if we're sharing hives */
265 if (CmpShareSystemHives)
266 {
267 /* Then force using the primary hive */
268 FileType = HFILE_TYPE_PRIMARY;
269 if (LogHandle)
270 {
271 /* Get rid of the log handle */
272 ZwClose(LogHandle);
273 LogHandle = NULL;
274 }
275 }
276
277 /* Check if we're too late */
278 if (HvShutdownComplete)
279 {
280 /* Fail */
281 ZwClose(FileHandle);
282 if (LogHandle) ZwClose(LogHandle);
283 return STATUS_TOO_LATE;
284 }
285
286 /* Initialize the hive */
287 Status = CmpInitializeHive((PCMHIVE*)&NewHive,
288 Operation,
289 HiveFlags,
290 FileType,
291 NULL,
292 FileHandle,
293 LogHandle,
294 NULL,
295 HiveName,
296 0);
297 if (!NT_SUCCESS(Status))
298 {
299 /* Fail */
300 ZwClose(FileHandle);
301 if (LogHandle) ZwClose(LogHandle);
302 return Status;
303 }
304
305 /* Success, return hive */
306 *Hive = NewHive;
307
308 /* ROS: Init root key cell and prepare the hive */
309 if (Operation == HINIT_CREATE) CmCreateRootNode(&NewHive->Hive, L"");
310
311 /* Duplicate the hive name */
312 NewHive->FileFullPath.Buffer = ExAllocatePoolWithTag(PagedPool,
313 HiveName->Length,
314 TAG_CM);
315 if (NewHive->FileFullPath.Buffer)
316 {
317 /* Copy the string */
318 RtlCopyMemory(NewHive->FileFullPath.Buffer,
319 HiveName->Buffer,
320 HiveName->Length);
321 NewHive->FileFullPath.Length = HiveName->Length;
322 NewHive->FileFullPath.MaximumLength = HiveName->MaximumLength;
323 }
324
325 /* Return success */
326 return STATUS_SUCCESS;
327 }
328
329 NTSTATUS
330 NTAPI
331 INIT_FUNCTION
332 CmpSetSystemValues(IN PLOADER_PARAMETER_BLOCK LoaderBlock)
333 {
334 OBJECT_ATTRIBUTES ObjectAttributes;
335 UNICODE_STRING KeyName, ValueName = { 0, 0, NULL };
336 HANDLE KeyHandle = NULL;
337 NTSTATUS Status;
338 ASSERT(LoaderBlock != NULL);
339
340 /* Setup attributes for loader options */
341 RtlInitUnicodeString(&KeyName,
342 L"\\REGISTRY\\MACHINE\\SYSTEM\\CurrentControlSet\\"
343 L"Control");
344 InitializeObjectAttributes(&ObjectAttributes,
345 &KeyName,
346 OBJ_CASE_INSENSITIVE,
347 NULL,
348 NULL);
349 Status = NtOpenKey(&KeyHandle, KEY_WRITE, &ObjectAttributes);
350 if (!NT_SUCCESS(Status)) goto Quickie;
351
352 /* Key opened, now write to the key */
353 RtlInitUnicodeString(&KeyName, L"SystemStartOptions");
354 Status = NtSetValueKey(KeyHandle,
355 &KeyName,
356 0,
357 REG_SZ,
358 CmpLoadOptions.Buffer,
359 CmpLoadOptions.Length);
360 if (!NT_SUCCESS(Status)) goto Quickie;
361
362 /* Setup value name for system boot device */
363 RtlInitUnicodeString(&KeyName, L"SystemBootDevice");
364 RtlCreateUnicodeStringFromAsciiz(&ValueName, LoaderBlock->NtBootPathName);
365 Status = NtSetValueKey(KeyHandle,
366 &KeyName,
367 0,
368 REG_SZ,
369 ValueName.Buffer,
370 ValueName.Length);
371
372 Quickie:
373 /* Free the buffers */
374 RtlFreeUnicodeString(&ValueName);
375
376 /* Close the key and return */
377 if (KeyHandle) NtClose(KeyHandle);
378
379 /* Return the status */
380 return (ExpInTextModeSetup ? STATUS_SUCCESS : Status);
381 }
382
383 NTSTATUS
384 NTAPI
385 INIT_FUNCTION
386 CmpCreateControlSet(IN PLOADER_PARAMETER_BLOCK LoaderBlock)
387 {
388 UNICODE_STRING ConfigName = RTL_CONSTANT_STRING(L"Control\\IDConfigDB");
389 UNICODE_STRING SelectName =
390 RTL_CONSTANT_STRING(L"\\Registry\\Machine\\System\\Select");
391 UNICODE_STRING KeyName;
392 OBJECT_ATTRIBUTES ObjectAttributes;
393 CHAR ValueInfoBuffer[128];
394 PKEY_VALUE_FULL_INFORMATION ValueInfo;
395 CHAR Buffer[128];
396 WCHAR UnicodeBuffer[128];
397 HANDLE SelectHandle, KeyHandle, ConfigHandle = NULL, ProfileHandle = NULL;
398 HANDLE ParentHandle = NULL;
399 ULONG ControlSet, HwProfile;
400 ANSI_STRING TempString;
401 NTSTATUS Status;
402 ULONG ResultLength, Disposition;
403 PLOADER_PARAMETER_EXTENSION LoaderExtension;
404 PAGED_CODE();
405
406 /* Open the select key */
407 InitializeObjectAttributes(&ObjectAttributes,
408 &SelectName,
409 OBJ_CASE_INSENSITIVE,
410 NULL,
411 NULL);
412 Status = NtOpenKey(&SelectHandle, KEY_READ, &ObjectAttributes);
413 if (!NT_SUCCESS(Status))
414 {
415 /* ReactOS Hack: Hard-code current to 001 for SetupLdr */
416 if (!LoaderBlock->RegistryBase)
417 {
418 /* Build the ControlSet001 key */
419 RtlInitUnicodeString(&KeyName,
420 L"\\Registry\\Machine\\System\\ControlSet001");
421 InitializeObjectAttributes(&ObjectAttributes,
422 &KeyName,
423 OBJ_CASE_INSENSITIVE,
424 NULL,
425 NULL);
426 Status = NtCreateKey(&KeyHandle,
427 KEY_ALL_ACCESS,
428 &ObjectAttributes,
429 0,
430 NULL,
431 0,
432 &Disposition);
433 if (!NT_SUCCESS(Status)) return Status;
434
435 /* Don't need the handle */
436 ZwClose(KeyHandle);
437
438 /* Use hard-coded setting */
439 ControlSet = 1;
440 goto UseSet;
441 }
442
443 /* Fail for real boots */
444 return Status;
445 }
446
447 /* Open the current value */
448 RtlInitUnicodeString(&KeyName, L"Current");
449 Status = NtQueryValueKey(SelectHandle,
450 &KeyName,
451 KeyValueFullInformation,
452 ValueInfoBuffer,
453 sizeof(ValueInfoBuffer),
454 &ResultLength);
455 NtClose(SelectHandle);
456 if (!NT_SUCCESS(Status)) return Status;
457
458 /* Get the actual value pointer, and get the control set ID */
459 ValueInfo = (PKEY_VALUE_FULL_INFORMATION)ValueInfoBuffer;
460 ControlSet = *(PULONG)((PUCHAR)ValueInfo + ValueInfo->DataOffset);
461
462 /* Create the current control set key */
463 UseSet:
464 RtlInitUnicodeString(&KeyName,
465 L"\\Registry\\Machine\\System\\CurrentControlSet");
466 InitializeObjectAttributes(&ObjectAttributes,
467 &KeyName,
468 OBJ_CASE_INSENSITIVE,
469 NULL,
470 NULL);
471 Status = NtCreateKey(&KeyHandle,
472 KEY_CREATE_LINK,
473 &ObjectAttributes,
474 0,
475 NULL,
476 REG_OPTION_VOLATILE | REG_OPTION_CREATE_LINK,
477 &Disposition);
478 if (!NT_SUCCESS(Status)) return Status;
479
480 /* Sanity check */
481 ASSERT(Disposition == REG_CREATED_NEW_KEY);
482
483 /* Initialize the symbolic link name */
484 sprintf(Buffer,
485 "\\Registry\\Machine\\System\\ControlSet%03ld",
486 ControlSet);
487 RtlInitAnsiString(&TempString, Buffer);
488
489 /* Create a Unicode string out of it */
490 KeyName.MaximumLength = sizeof(UnicodeBuffer);
491 KeyName.Buffer = UnicodeBuffer;
492 Status = RtlAnsiStringToUnicodeString(&KeyName, &TempString, FALSE);
493
494 /* Set the value */
495 Status = NtSetValueKey(KeyHandle,
496 &CmSymbolicLinkValueName,
497 0,
498 REG_LINK,
499 KeyName.Buffer,
500 KeyName.Length);
501 if (!NT_SUCCESS(Status)) return Status;
502
503 /* Get the configuration database key */
504 InitializeObjectAttributes(&ObjectAttributes,
505 &ConfigName,
506 OBJ_CASE_INSENSITIVE,
507 KeyHandle,
508 NULL);
509 Status = NtOpenKey(&ConfigHandle, KEY_READ, &ObjectAttributes);
510 NtClose(KeyHandle);
511
512 /* Check if we don't have one */
513 if (!NT_SUCCESS(Status))
514 {
515 /* Cleanup and exit */
516 ConfigHandle = 0;
517 goto Cleanup;
518 }
519
520 /* Now get the current config */
521 RtlInitUnicodeString(&KeyName, L"CurrentConfig");
522 Status = NtQueryValueKey(ConfigHandle,
523 &KeyName,
524 KeyValueFullInformation,
525 ValueInfoBuffer,
526 sizeof(ValueInfoBuffer),
527 &ResultLength);
528
529 /* Set pointer to buffer */
530 ValueInfo = (PKEY_VALUE_FULL_INFORMATION)ValueInfoBuffer;
531
532 /* Check if we failed or got a non DWORD-value */
533 if (!(NT_SUCCESS(Status)) || (ValueInfo->Type != REG_DWORD)) goto Cleanup;
534
535 /* Get the hadware profile */
536 HwProfile = *(PULONG)((PUCHAR)ValueInfo + ValueInfo->DataOffset);
537
538 /* Open the hardware profile key */
539 RtlInitUnicodeString(&KeyName,
540 L"\\Registry\\Machine\\System\\CurrentControlSet"
541 L"\\Hardware Profiles");
542 InitializeObjectAttributes(&ObjectAttributes,
543 &KeyName,
544 OBJ_CASE_INSENSITIVE,
545 NULL,
546 NULL);
547 Status = NtOpenKey(&ParentHandle, KEY_READ, &ObjectAttributes);
548 if (!NT_SUCCESS(Status))
549 {
550 /* Exit and clean up */
551 ParentHandle = 0;
552 goto Cleanup;
553 }
554
555 /* Build the profile name */
556 sprintf(Buffer, "%04ld", HwProfile);
557 RtlInitAnsiString(&TempString, Buffer);
558
559 /* Convert it to Unicode */
560 KeyName.MaximumLength = sizeof(UnicodeBuffer);
561 KeyName.Buffer = UnicodeBuffer;
562 Status = RtlAnsiStringToUnicodeString(&KeyName,
563 &TempString,
564 FALSE);
565 ASSERT(Status == STATUS_SUCCESS);
566
567 /* Open the associated key */
568 InitializeObjectAttributes(&ObjectAttributes,
569 &KeyName,
570 OBJ_CASE_INSENSITIVE,
571 ParentHandle,
572 NULL);
573 Status = NtOpenKey(&ProfileHandle,
574 KEY_READ | KEY_WRITE,
575 &ObjectAttributes);
576 if (!NT_SUCCESS (Status))
577 {
578 /* Cleanup and exit */
579 ProfileHandle = 0;
580 goto Cleanup;
581 }
582
583 /* Check if we have a loader block extension */
584 LoaderExtension = LoaderBlock->Extension;
585 if (LoaderExtension)
586 {
587 ASSERTMSG("ReactOS doesn't support NTLDR Profiles yet!\n", FALSE);
588 }
589
590 /* Create the current hardware profile key */
591 RtlInitUnicodeString(&KeyName,
592 L"\\Registry\\Machine\\System\\CurrentControlSet\\"
593 L"Hardware Profiles\\Current");
594 InitializeObjectAttributes(&ObjectAttributes,
595 &KeyName,
596 OBJ_CASE_INSENSITIVE,
597 NULL,
598 NULL);
599 Status = NtCreateKey(&KeyHandle,
600 KEY_CREATE_LINK,
601 &ObjectAttributes,
602 0,
603 NULL,
604 REG_OPTION_VOLATILE | REG_OPTION_CREATE_LINK,
605 &Disposition);
606 if (NT_SUCCESS(Status))
607 {
608 /* Sanity check */
609 ASSERT(Disposition == REG_CREATED_NEW_KEY);
610
611 /* Create the profile name */
612 sprintf(Buffer,
613 "\\Registry\\Machine\\System\\CurrentControlSet\\"
614 "Hardware Profiles\\%04ld",
615 HwProfile);
616 RtlInitAnsiString(&TempString, Buffer);
617
618 /* Convert it to Unicode */
619 KeyName.MaximumLength = sizeof(UnicodeBuffer);
620 KeyName.Buffer = UnicodeBuffer;
621 Status = RtlAnsiStringToUnicodeString(&KeyName,
622 &TempString,
623 FALSE);
624 ASSERT(STATUS_SUCCESS == Status);
625
626 /* Set it */
627 Status = NtSetValueKey(KeyHandle,
628 &CmSymbolicLinkValueName,
629 0,
630 REG_LINK,
631 KeyName.Buffer,
632 KeyName.Length);
633 NtClose(KeyHandle);
634 }
635
636 /* Close every opened handle */
637 Cleanup:
638 if (ConfigHandle) NtClose(ConfigHandle);
639 if (ProfileHandle) NtClose(ProfileHandle);
640 if (ParentHandle) NtClose(ParentHandle);
641
642 /* Return success */
643 return STATUS_SUCCESS;
644 }
645
646 NTSTATUS
647 NTAPI
648 CmpLinkHiveToMaster(IN PUNICODE_STRING LinkName,
649 IN HANDLE RootDirectory,
650 IN PCMHIVE RegistryHive,
651 IN BOOLEAN Allocate,
652 IN PSECURITY_DESCRIPTOR SecurityDescriptor)
653 {
654 OBJECT_ATTRIBUTES ObjectAttributes;
655 NTSTATUS Status;
656 CM_PARSE_CONTEXT ParseContext = {0};
657 HANDLE KeyHandle;
658 PCM_KEY_BODY KeyBody;
659 PAGED_CODE();
660
661 /* Setup the object attributes */
662 InitializeObjectAttributes(&ObjectAttributes,
663 LinkName,
664 OBJ_CASE_INSENSITIVE | OBJ_KERNEL_HANDLE,
665 RootDirectory,
666 SecurityDescriptor);
667
668 /* Setup the parse context */
669 ParseContext.CreateLink = TRUE;
670 ParseContext.CreateOperation = TRUE;
671 ParseContext.ChildHive.KeyHive = &RegistryHive->Hive;
672
673 /* Check if we have a root keycell or if we need to create it */
674 if (Allocate)
675 {
676 /* Create it */
677 ParseContext.ChildHive.KeyCell = HCELL_NIL;
678 }
679 else
680 {
681 /* We have one */
682 ParseContext.ChildHive.KeyCell = RegistryHive->Hive.BaseBlock->RootCell;
683 }
684
685 /* Create the link node */
686 Status = ObOpenObjectByName(&ObjectAttributes,
687 CmpKeyObjectType,
688 KernelMode,
689 NULL,
690 KEY_READ | KEY_WRITE,
691 (PVOID)&ParseContext,
692 &KeyHandle);
693 if (!NT_SUCCESS(Status)) return Status;
694
695 /* Mark the hive as clean */
696 RegistryHive->Hive.DirtyFlag = FALSE;
697
698 /* ReactOS Hack: Keep alive */
699 Status = ObReferenceObjectByHandle(KeyHandle,
700 0,
701 CmpKeyObjectType,
702 KernelMode,
703 (PVOID*)&KeyBody,
704 NULL);
705 ASSERT(NT_SUCCESS(Status));
706
707 /* Close the extra handle */
708 ZwClose(KeyHandle);
709 return STATUS_SUCCESS;
710 }
711
712 BOOLEAN
713 NTAPI
714 INIT_FUNCTION
715 CmpInitializeSystemHive(IN PLOADER_PARAMETER_BLOCK LoaderBlock)
716 {
717 PVOID HiveBase;
718 ANSI_STRING LoadString;
719 PVOID Buffer;
720 ULONG Length;
721 NTSTATUS Status;
722 BOOLEAN Allocate;
723 UNICODE_STRING KeyName;
724 PCMHIVE SystemHive = NULL;
725 UNICODE_STRING HiveName = RTL_CONSTANT_STRING(L"SYSTEM");
726 PSECURITY_DESCRIPTOR SecurityDescriptor;
727 PAGED_CODE();
728
729 /* Setup the ansi string */
730 RtlInitAnsiString(&LoadString, LoaderBlock->LoadOptions);
731
732 /* Allocate the unicode buffer */
733 Length = LoadString.Length * sizeof(WCHAR) + sizeof(UNICODE_NULL);
734 Buffer = ExAllocatePoolWithTag(PagedPool, Length, TAG_CM);
735 if (!Buffer)
736 {
737 /* Fail */
738 KeBugCheckEx(BAD_SYSTEM_CONFIG_INFO, 3, 1, (ULONG_PTR)LoaderBlock, 0);
739 }
740
741 /* Setup the unicode string */
742 RtlInitEmptyUnicodeString(&CmpLoadOptions, Buffer, (USHORT)Length);
743
744 /* Add the load options and null-terminate */
745 RtlAnsiStringToUnicodeString(&CmpLoadOptions, &LoadString, FALSE);
746 CmpLoadOptions.Buffer[LoadString.Length] = UNICODE_NULL;
747 CmpLoadOptions.Length += sizeof(WCHAR);
748
749 /* Get the System Hive base address */
750 HiveBase = LoaderBlock->RegistryBase;
751 if (HiveBase)
752 {
753 /* Import it */
754 ((PHBASE_BLOCK)HiveBase)->Length = LoaderBlock->RegistryLength;
755 Status = CmpInitializeHive((PCMHIVE*)&SystemHive,
756 HINIT_MEMORY,
757 HIVE_NOLAZYFLUSH,
758 HFILE_TYPE_LOG,
759 HiveBase,
760 NULL,
761 NULL,
762 NULL,
763 &HiveName,
764 2);
765 if (!NT_SUCCESS(Status)) return FALSE;
766
767 /* Set the hive filename */
768 RtlCreateUnicodeString(&SystemHive->FileFullPath,
769 L"\\SystemRoot\\System32\\Config\\SYSTEM");
770
771 /* We imported, no need to create a new hive */
772 Allocate = FALSE;
773
774 /* Manually set the hive as volatile, if in Live CD mode */
775 if (CmpShareSystemHives) SystemHive->Hive.HiveFlags = HIVE_VOLATILE;
776 }
777 else
778 {
779 /* Create it */
780 Status = CmpInitializeHive(&SystemHive,
781 HINIT_CREATE,
782 HIVE_NOLAZYFLUSH,
783 HFILE_TYPE_LOG,
784 NULL,
785 NULL,
786 NULL,
787 NULL,
788 &HiveName,
789 0);
790 if (!NT_SUCCESS(Status)) return FALSE;
791
792 /* Set the hive filename */
793 RtlCreateUnicodeString(&SystemHive->FileFullPath,
794 L"\\SystemRoot\\System32\\Config\\SYSTEM");
795
796 /* Tell CmpLinkHiveToMaster to allocate a hive */
797 Allocate = TRUE;
798 }
799
800 /* Save the boot type */
801 CmpBootType = SystemHive->Hive.BaseBlock->BootType;
802
803 /* Are we in self-healing mode? */
804 if (!CmSelfHeal)
805 {
806 /* Disable self-healing internally and check if boot type wanted it */
807 CmpSelfHeal = FALSE;
808 if (CmpBootType & 4)
809 {
810 /* We're disabled, so bugcheck */
811 KeBugCheckEx(BAD_SYSTEM_CONFIG_INFO,
812 3,
813 3,
814 (ULONG_PTR)SystemHive,
815 0);
816 }
817 }
818
819 /* Create the default security descriptor */
820 SecurityDescriptor = CmpHiveRootSecurityDescriptor();
821
822 /* Attach it to the system key */
823 RtlInitUnicodeString(&KeyName, L"\\Registry\\Machine\\SYSTEM");
824 Status = CmpLinkHiveToMaster(&KeyName,
825 NULL,
826 (PCMHIVE)SystemHive,
827 Allocate,
828 SecurityDescriptor);
829
830 /* Free the security descriptor */
831 ExFreePoolWithTag(SecurityDescriptor, TAG_CM);
832 if (!NT_SUCCESS(Status)) return FALSE;
833
834 /* Add the hive to the hive list */
835 CmpMachineHiveList[3].CmHive = (PCMHIVE)SystemHive;
836
837 /* Success! */
838 return TRUE;
839 }
840
841 NTSTATUS
842 NTAPI
843 INIT_FUNCTION
844 CmpCreateObjectTypes(VOID)
845 {
846 OBJECT_TYPE_INITIALIZER ObjectTypeInitializer;
847 UNICODE_STRING Name;
848 GENERIC_MAPPING CmpKeyMapping = {KEY_READ,
849 KEY_WRITE,
850 KEY_EXECUTE,
851 KEY_ALL_ACCESS};
852 PAGED_CODE();
853
854 /* Initialize the Key object type */
855 RtlZeroMemory(&ObjectTypeInitializer, sizeof(ObjectTypeInitializer));
856 RtlInitUnicodeString(&Name, L"Key");
857 ObjectTypeInitializer.Length = sizeof(ObjectTypeInitializer);
858 ObjectTypeInitializer.DefaultPagedPoolCharge = sizeof(CM_KEY_BODY);
859 ObjectTypeInitializer.GenericMapping = CmpKeyMapping;
860 ObjectTypeInitializer.PoolType = PagedPool;
861 ObjectTypeInitializer.ValidAccessMask = KEY_ALL_ACCESS;
862 ObjectTypeInitializer.UseDefaultObject = TRUE;
863 ObjectTypeInitializer.DeleteProcedure = CmpDeleteKeyObject;
864 ObjectTypeInitializer.ParseProcedure = CmpParseKey;
865 ObjectTypeInitializer.SecurityProcedure = CmpSecurityMethod;
866 ObjectTypeInitializer.QueryNameProcedure = CmpQueryKeyName;
867 ObjectTypeInitializer.CloseProcedure = CmpCloseKeyObject;
868 ObjectTypeInitializer.SecurityRequired = TRUE;
869 ObjectTypeInitializer.InvalidAttributes = OBJ_EXCLUSIVE | OBJ_PERMANENT;
870
871 /* Create it */
872 return ObCreateObjectType(&Name, &ObjectTypeInitializer, NULL, &CmpKeyObjectType);
873 }
874
875 BOOLEAN
876 NTAPI
877 INIT_FUNCTION
878 CmpCreateRootNode(IN PHHIVE Hive,
879 IN PCWSTR Name,
880 OUT PHCELL_INDEX Index)
881 {
882 UNICODE_STRING KeyName;
883 PCM_KEY_NODE KeyCell;
884 LARGE_INTEGER SystemTime;
885 PAGED_CODE();
886
887 /* Initialize the node name and allocate it */
888 RtlInitUnicodeString(&KeyName, Name);
889 *Index = HvAllocateCell(Hive,
890 FIELD_OFFSET(CM_KEY_NODE, Name) +
891 CmpNameSize(Hive, &KeyName),
892 Stable,
893 HCELL_NIL);
894 if (*Index == HCELL_NIL) return FALSE;
895
896 /* Set the cell index and get the data */
897 Hive->BaseBlock->RootCell = *Index;
898 KeyCell = (PCM_KEY_NODE)HvGetCell(Hive, *Index);
899 if (!KeyCell) return FALSE;
900
901 /* Setup the cell */
902 KeyCell->Signature = (USHORT)CM_KEY_NODE_SIGNATURE;
903 KeyCell->Flags = KEY_HIVE_ENTRY | KEY_NO_DELETE;
904 KeQuerySystemTime(&SystemTime);
905 KeyCell->LastWriteTime = SystemTime;
906 KeyCell->Parent = HCELL_NIL;
907 KeyCell->SubKeyCounts[Stable] = 0;
908 KeyCell->SubKeyCounts[Volatile] = 0;
909 KeyCell->SubKeyLists[Stable] = HCELL_NIL;
910 KeyCell->SubKeyLists[Volatile] = HCELL_NIL;
911 KeyCell->ValueList.Count = 0;
912 KeyCell->ValueList.List = HCELL_NIL;
913 KeyCell->Security = HCELL_NIL;
914 KeyCell->Class = HCELL_NIL;
915 KeyCell->ClassLength = 0;
916 KeyCell->MaxNameLen = 0;
917 KeyCell->MaxClassLen = 0;
918 KeyCell->MaxValueNameLen = 0;
919 KeyCell->MaxValueDataLen = 0;
920
921 /* Copy the name (this will also set the length) */
922 KeyCell->NameLength = CmpCopyName(Hive, (PWCHAR)KeyCell->Name, &KeyName);
923
924 /* Check if the name was compressed */
925 if (KeyCell->NameLength < KeyName.Length)
926 {
927 /* Set the flag */
928 KeyCell->Flags |= KEY_COMP_NAME;
929 }
930
931 /* Return success */
932 HvReleaseCell(Hive, *Index);
933 return TRUE;
934 }
935
936 BOOLEAN
937 NTAPI
938 INIT_FUNCTION
939 CmpCreateRegistryRoot(VOID)
940 {
941 UNICODE_STRING KeyName;
942 OBJECT_ATTRIBUTES ObjectAttributes;
943 PCM_KEY_BODY RootKey;
944 HCELL_INDEX RootIndex;
945 NTSTATUS Status;
946 PCM_KEY_NODE KeyCell;
947 PSECURITY_DESCRIPTOR SecurityDescriptor;
948 PCM_KEY_CONTROL_BLOCK Kcb;
949 PAGED_CODE();
950
951 /* Setup the root node */
952 if (!CmpCreateRootNode(&CmiVolatileHive->Hive, L"REGISTRY", &RootIndex))
953 {
954 /* We failed */
955 return FALSE;
956 }
957
958 /* Create '\Registry' key. */
959 RtlInitUnicodeString(&KeyName, L"\\REGISTRY");
960 SecurityDescriptor = CmpHiveRootSecurityDescriptor();
961 InitializeObjectAttributes(&ObjectAttributes,
962 &KeyName,
963 OBJ_CASE_INSENSITIVE,
964 NULL,
965 NULL);
966 Status = ObCreateObject(KernelMode,
967 CmpKeyObjectType,
968 &ObjectAttributes,
969 KernelMode,
970 NULL,
971 sizeof(CM_KEY_BODY),
972 0,
973 0,
974 (PVOID*)&RootKey);
975 ExFreePoolWithTag(SecurityDescriptor, TAG_CM);
976 if (!NT_SUCCESS(Status)) return FALSE;
977
978 /* Sanity check, and get the key cell */
979 ASSERT((&CmiVolatileHive->Hive)->ReleaseCellRoutine == NULL);
980 KeyCell = (PCM_KEY_NODE)HvGetCell(&CmiVolatileHive->Hive, RootIndex);
981 if (!KeyCell) return FALSE;
982
983 /* Create the KCB */
984 RtlInitUnicodeString(&KeyName, L"\\REGISTRY");
985 Kcb = CmpCreateKeyControlBlock(&CmiVolatileHive->Hive,
986 RootIndex,
987 KeyCell,
988 NULL,
989 0,
990 &KeyName);
991 if (!Kcb) return FALSE;
992
993 /* Initialize the object */
994 RootKey->KeyControlBlock = Kcb;
995 RootKey->Type = '20yk';
996 RootKey->NotifyBlock = NULL;
997 RootKey->ProcessID = PsGetCurrentProcessId();
998
999 /* Link with KCB */
1000 EnlistKeyBodyWithKCB(RootKey, 0);
1001
1002 /* Insert the key into the namespace */
1003 Status = ObInsertObject(RootKey,
1004 NULL,
1005 KEY_ALL_ACCESS,
1006 0,
1007 NULL,
1008 &CmpRegistryRootHandle);
1009 if (!NT_SUCCESS(Status)) return FALSE;
1010
1011 /* Reference the key again so that we never lose it */
1012 Status = ObReferenceObjectByHandle(CmpRegistryRootHandle,
1013 KEY_READ,
1014 NULL,
1015 KernelMode,
1016 (PVOID*)&RootKey,
1017 NULL);
1018 if (!NT_SUCCESS(Status)) return FALSE;
1019
1020 /* Completely sucessful */
1021 return TRUE;
1022 }
1023
1024 NTSTATUS
1025 NTAPI
1026 CmpGetRegistryPath(IN PWCHAR ConfigPath)
1027 {
1028 OBJECT_ATTRIBUTES ObjectAttributes;
1029 NTSTATUS Status;
1030 HANDLE KeyHandle;
1031 PKEY_VALUE_PARTIAL_INFORMATION ValueInfo;
1032 UNICODE_STRING KeyName = RTL_CONSTANT_STRING(L"\\Registry\\Machine\\HARDWARE");
1033 UNICODE_STRING ValueName = RTL_CONSTANT_STRING(L"InstallPath");
1034 ULONG BufferSize, ResultSize;
1035
1036 /* Check if we are booted in setup */
1037 if (ExpInTextModeSetup)
1038 {
1039 /* Setup the object attributes */
1040 InitializeObjectAttributes(&ObjectAttributes,
1041 &KeyName,
1042 OBJ_CASE_INSENSITIVE,
1043 NULL,
1044 NULL);
1045 /* Open the key */
1046 Status = ZwOpenKey(&KeyHandle,
1047 KEY_ALL_ACCESS,
1048 &ObjectAttributes);
1049 if (!NT_SUCCESS(Status)) return Status;
1050
1051 /* Allocate the buffer */
1052 BufferSize = sizeof(KEY_VALUE_PARTIAL_INFORMATION) + 4096;
1053 ValueInfo = ExAllocatePoolWithTag(PagedPool, BufferSize, TAG_CM);
1054 if (!ValueInfo)
1055 {
1056 /* Fail */
1057 ZwClose(KeyHandle);
1058 return STATUS_INSUFFICIENT_RESOURCES;
1059 }
1060
1061 /* Query the value */
1062 Status = ZwQueryValueKey(KeyHandle,
1063 &ValueName,
1064 KeyValuePartialInformation,
1065 ValueInfo,
1066 BufferSize,
1067 &ResultSize);
1068 ZwClose(KeyHandle);
1069 if (!NT_SUCCESS(Status))
1070 {
1071 /* Fail */
1072 ExFreePoolWithTag(ValueInfo, TAG_CM);
1073 return Status;
1074 }
1075
1076 /* Copy the config path and null-terminate it */
1077 RtlCopyMemory(ConfigPath,
1078 ValueInfo->Data,
1079 ValueInfo->DataLength);
1080 ConfigPath[ValueInfo->DataLength / sizeof(WCHAR)] = UNICODE_NULL;
1081 ExFreePoolWithTag(ValueInfo, TAG_CM);
1082 }
1083 else
1084 {
1085 /* Just use default path */
1086 wcscpy(ConfigPath, L"\\SystemRoot");
1087 }
1088
1089 /* Add registry path */
1090 wcscat(ConfigPath, L"\\System32\\Config\\");
1091
1092 /* Done */
1093 return STATUS_SUCCESS;
1094 }
1095
1096 VOID
1097 NTAPI
1098 CmpLoadHiveThread(IN PVOID StartContext)
1099 {
1100 WCHAR FileBuffer[MAX_PATH], RegBuffer[MAX_PATH], ConfigPath[MAX_PATH];
1101 UNICODE_STRING TempName, FileName, RegName;
1102 ULONG i, ErrorResponse, WorkerCount, Length;
1103 USHORT FileStart;
1104 //ULONG RegStart;
1105 ULONG PrimaryDisposition, SecondaryDisposition, ClusterSize;
1106 PCMHIVE CmHive;
1107 HANDLE PrimaryHandle, LogHandle;
1108 NTSTATUS Status = STATUS_SUCCESS;
1109 PVOID ErrorParameters;
1110 PAGED_CODE();
1111
1112 /* Get the hive index, make sure it makes sense */
1113 i = PtrToUlong(StartContext);
1114 ASSERT(CmpMachineHiveList[i].Name != NULL);
1115
1116 /* We were started */
1117 CmpMachineHiveList[i].ThreadStarted = TRUE;
1118
1119 /* Build the file name and registry name strings */
1120 RtlInitEmptyUnicodeString(&FileName, FileBuffer, MAX_PATH);
1121 RtlInitEmptyUnicodeString(&RegName, RegBuffer, MAX_PATH);
1122
1123 /* Now build the system root path */
1124 CmpGetRegistryPath(ConfigPath);
1125 RtlInitUnicodeString(&TempName, ConfigPath);
1126 RtlAppendStringToString((PSTRING)&FileName, (PSTRING)&TempName);
1127 FileStart = FileName.Length;
1128
1129 /* And build the registry root path */
1130 RtlInitUnicodeString(&TempName, L"\\REGISTRY\\");
1131 RtlAppendStringToString((PSTRING)&RegName, (PSTRING)&TempName);
1132 //RegStart = RegName.Length;
1133
1134 /* Build the base name */
1135 RtlInitUnicodeString(&TempName, CmpMachineHiveList[i].BaseName);
1136 RtlAppendStringToString((PSTRING)&RegName, (PSTRING)&TempName);
1137
1138 /* Check if this is a child of the root */
1139 if (RegName.Buffer[RegName.Length / sizeof(WCHAR) - 1] == '\\')
1140 {
1141 /* Then setup the whole name */
1142 RtlInitUnicodeString(&TempName, CmpMachineHiveList[i].Name);
1143 RtlAppendStringToString((PSTRING)&RegName, (PSTRING)&TempName);
1144 }
1145
1146 /* Now add the rest of the file name */
1147 RtlInitUnicodeString(&TempName, CmpMachineHiveList[i].Name);
1148 FileName.Length = FileStart;
1149 RtlAppendStringToString((PSTRING)&FileName, (PSTRING)&TempName);
1150 if (!CmpMachineHiveList[i].CmHive)
1151 {
1152 /* We need to allocate a new hive structure */
1153 CmpMachineHiveList[i].Allocate = TRUE;
1154
1155 /* Load the hive file */
1156 Status = CmpInitHiveFromFile(&FileName,
1157 CmpMachineHiveList[i].HHiveFlags,
1158 &CmHive,
1159 &CmpMachineHiveList[i].Allocate,
1160 0);
1161 if (!(NT_SUCCESS(Status)) ||
1162 (!(CmHive->FileHandles[HFILE_TYPE_LOG]) && !(CmpMiniNTBoot))) // HACK
1163 {
1164 /* We failed or couldn't get a log file, raise a hard error */
1165 ErrorParameters = &FileName;
1166 NtRaiseHardError(STATUS_CANNOT_LOAD_REGISTRY_FILE,
1167 1,
1168 1,
1169 (PULONG_PTR)&ErrorParameters,
1170 OptionOk,
1171 &ErrorResponse);
1172 }
1173
1174 /* Set the hive flags and newly allocated hive pointer */
1175 CmHive->Flags = CmpMachineHiveList[i].CmHiveFlags;
1176 CmpMachineHiveList[i].CmHive2 = CmHive;
1177 }
1178 else
1179 {
1180 /* We already have a hive, is it volatile? */
1181 CmHive = CmpMachineHiveList[i].CmHive;
1182 if (!(CmHive->Hive.HiveFlags & HIVE_VOLATILE))
1183 {
1184 /* It's now, open the hive file and log */
1185 Status = CmpOpenHiveFiles(&FileName,
1186 L".LOG",
1187 &PrimaryHandle,
1188 &LogHandle,
1189 &PrimaryDisposition,
1190 &SecondaryDisposition,
1191 TRUE,
1192 TRUE,
1193 FALSE,
1194 &ClusterSize);
1195 if (!(NT_SUCCESS(Status)) || !(LogHandle))
1196 {
1197 /* Couldn't open the hive or its log file, raise a hard error */
1198 ErrorParameters = &FileName;
1199 NtRaiseHardError(STATUS_CANNOT_LOAD_REGISTRY_FILE,
1200 1,
1201 1,
1202 (PULONG_PTR)&ErrorParameters,
1203 OptionOk,
1204 &ErrorResponse);
1205
1206 /* And bugcheck for posterity's sake */
1207 KeBugCheckEx(BAD_SYSTEM_CONFIG_INFO, 9, 0, i, Status);
1208 }
1209
1210 /* Save the file handles. This should remove our sync hacks */
1211 CmHive->FileHandles[HFILE_TYPE_LOG] = LogHandle;
1212 CmHive->FileHandles[HFILE_TYPE_PRIMARY] = PrimaryHandle;
1213
1214 /* Allow lazy flushing since the handles are there -- remove sync hacks */
1215 //ASSERT(CmHive->Hive.HiveFlags & HIVE_NOLAZYFLUSH);
1216 CmHive->Hive.HiveFlags &= ~HIVE_NOLAZYFLUSH;
1217
1218 /* Get the real size of the hive */
1219 Length = CmHive->Hive.Storage[Stable].Length + HBLOCK_SIZE;
1220
1221 /* Check if the cluster size doesn't match */
1222 if (CmHive->Hive.Cluster != ClusterSize) ASSERT(FALSE);
1223
1224 /* Set the file size */
1225 DPRINT("FIXME: Should set file size: %lx\n", Length);
1226 //if (!CmpFileSetSize((PHHIVE)CmHive, HFILE_TYPE_PRIMARY, Length, Length))
1227 {
1228 /* This shouldn't fail */
1229 //ASSERT(FALSE);
1230 }
1231
1232 /* Another thing we don't support is NTLDR-recovery */
1233 if (CmHive->Hive.BaseBlock->BootRecover) ASSERT(FALSE);
1234
1235 /* Finally, set our allocated hive to the same hive we've had */
1236 CmpMachineHiveList[i].CmHive2 = CmHive;
1237 ASSERT(CmpMachineHiveList[i].CmHive == CmpMachineHiveList[i].CmHive2);
1238 }
1239 }
1240
1241 /* We're done */
1242 CmpMachineHiveList[i].ThreadFinished = TRUE;
1243
1244 /* Check if we're the last worker */
1245 WorkerCount = InterlockedIncrement(&CmpLoadWorkerIncrement);
1246 if (WorkerCount == CM_NUMBER_OF_MACHINE_HIVES)
1247 {
1248 /* Signal the event */
1249 KeSetEvent(&CmpLoadWorkerEvent, 0, FALSE);
1250 }
1251
1252 /* Kill the thread */
1253 PsTerminateSystemThread(Status);
1254 }
1255
1256 VOID
1257 NTAPI
1258 CmpInitializeHiveList(IN USHORT Flag)
1259 {
1260 WCHAR FileBuffer[MAX_PATH], RegBuffer[MAX_PATH], ConfigPath[MAX_PATH];
1261 UNICODE_STRING TempName, FileName, RegName;
1262 HANDLE Thread;
1263 NTSTATUS Status;
1264 ULONG i;
1265 USHORT RegStart;
1266 PSECURITY_DESCRIPTOR SecurityDescriptor;
1267 PAGED_CODE();
1268
1269 /* Allow writing for now */
1270 CmpNoWrite = FALSE;
1271
1272 /* Build the file name and registry name strings */
1273 RtlInitEmptyUnicodeString(&FileName, FileBuffer, MAX_PATH);
1274 RtlInitEmptyUnicodeString(&RegName, RegBuffer, MAX_PATH);
1275
1276 /* Now build the system root path */
1277 CmpGetRegistryPath(ConfigPath);
1278 RtlInitUnicodeString(&TempName, ConfigPath);
1279 RtlAppendStringToString((PSTRING)&FileName, (PSTRING)&TempName);
1280
1281 /* And build the registry root path */
1282 RtlInitUnicodeString(&TempName, L"\\REGISTRY\\");
1283 RtlAppendStringToString((PSTRING)&RegName, (PSTRING)&TempName);
1284 RegStart = RegName.Length;
1285
1286 /* Setup the event to synchronize workers */
1287 KeInitializeEvent(&CmpLoadWorkerEvent, SynchronizationEvent, FALSE);
1288
1289 /* Enter special boot condition */
1290 CmpSpecialBootCondition = TRUE;
1291
1292 /* Create the SD for the root hives */
1293 SecurityDescriptor = CmpHiveRootSecurityDescriptor();
1294
1295 /* Loop every hive we care about */
1296 for (i = 0; i < CM_NUMBER_OF_MACHINE_HIVES; i++)
1297 {
1298 /* Make sure the list is setup */
1299 ASSERT(CmpMachineHiveList[i].Name != NULL);
1300
1301 /* Create a thread to handle this hive */
1302 Status = PsCreateSystemThread(&Thread,
1303 THREAD_ALL_ACCESS,
1304 NULL,
1305 0,
1306 NULL,
1307 CmpLoadHiveThread,
1308 UlongToPtr(i));
1309 if (NT_SUCCESS(Status))
1310 {
1311 /* We don't care about the handle -- the thread self-terminates */
1312 ZwClose(Thread);
1313 }
1314 else
1315 {
1316 /* Can't imagine this happening */
1317 KeBugCheckEx(BAD_SYSTEM_CONFIG_INFO, 9, 3, i, Status);
1318 }
1319 }
1320
1321 /* Make sure we've reached the end of the list */
1322 ASSERT(CmpMachineHiveList[i].Name == NULL);
1323
1324 /* Wait for hive loading to finish */
1325 KeWaitForSingleObject(&CmpLoadWorkerEvent,
1326 Executive,
1327 KernelMode,
1328 FALSE,
1329 NULL);
1330
1331 /* Exit the special boot condition and make sure all workers completed */
1332 CmpSpecialBootCondition = FALSE;
1333 ASSERT(CmpLoadWorkerIncrement == CM_NUMBER_OF_MACHINE_HIVES);
1334
1335 /* Loop hives again */
1336 for (i = 0; i < CM_NUMBER_OF_MACHINE_HIVES; i++)
1337 {
1338 /* Make sure the thread ran and finished */
1339 ASSERT(CmpMachineHiveList[i].ThreadFinished == TRUE);
1340 ASSERT(CmpMachineHiveList[i].ThreadStarted == TRUE);
1341
1342 /* Check if this was a new hive */
1343 if (!CmpMachineHiveList[i].CmHive)
1344 {
1345 /* Make sure we allocated something */
1346 ASSERT(CmpMachineHiveList[i].CmHive2 != NULL);
1347
1348 /* Build the base name */
1349 RegName.Length = RegStart;
1350 RtlInitUnicodeString(&TempName, CmpMachineHiveList[i].BaseName);
1351 RtlAppendStringToString((PSTRING)&RegName, (PSTRING)&TempName);
1352
1353 /* Check if this is a child of the root */
1354 if (RegName.Buffer[RegName.Length / sizeof(WCHAR) - 1] == '\\')
1355 {
1356 /* Then setup the whole name */
1357 RtlInitUnicodeString(&TempName, CmpMachineHiveList[i].Name);
1358 RtlAppendStringToString((PSTRING)&RegName, (PSTRING)&TempName);
1359 }
1360
1361 /* Now link the hive to its master */
1362 Status = CmpLinkHiveToMaster(&RegName,
1363 NULL,
1364 CmpMachineHiveList[i].CmHive2,
1365 CmpMachineHiveList[i].Allocate,
1366 SecurityDescriptor);
1367 if (Status != STATUS_SUCCESS)
1368 {
1369 /* Linking needs to work */
1370 KeBugCheckEx(CONFIG_LIST_FAILED, 11, Status, i, (ULONG_PTR)&RegName);
1371 }
1372
1373 /* Check if we had to allocate a new hive */
1374 if (CmpMachineHiveList[i].Allocate)
1375 {
1376 /* Sync the new hive */
1377 //HvSyncHive((PHHIVE)(CmpMachineHiveList[i].CmHive2));
1378 }
1379 }
1380
1381 /* Check if we created a new hive */
1382 if (CmpMachineHiveList[i].CmHive2)
1383 {
1384 /* TODO: Add to HiveList key */
1385 }
1386 }
1387
1388 /* Get rid of the SD */
1389 ExFreePoolWithTag(SecurityDescriptor, TAG_CM);
1390
1391 /* FIXME: Link SECURITY to SAM */
1392
1393 /* FIXME: Link S-1-5-18 to .Default */
1394 }
1395
1396 BOOLEAN
1397 NTAPI
1398 INIT_FUNCTION
1399 CmInitSystem1(VOID)
1400 {
1401 OBJECT_ATTRIBUTES ObjectAttributes;
1402 UNICODE_STRING KeyName;
1403 HANDLE KeyHandle;
1404 NTSTATUS Status;
1405 PCMHIVE HardwareHive;
1406 PSECURITY_DESCRIPTOR SecurityDescriptor;
1407 PAGED_CODE();
1408
1409 /* Check if this is PE-boot */
1410 if (InitIsWinPEMode)
1411 {
1412 /* Set registry to PE mode */
1413 CmpMiniNTBoot = TRUE;
1414 CmpShareSystemHives = TRUE;
1415 }
1416
1417 /* Initialize the hive list and lock */
1418 InitializeListHead(&CmpHiveListHead);
1419 ExInitializePushLock(&CmpHiveListHeadLock);
1420 ExInitializePushLock(&CmpLoadHiveLock);
1421
1422 /* Initialize registry lock */
1423 ExInitializeResourceLite(&CmpRegistryLock);
1424
1425 /* Initialize the cache */
1426 CmpInitializeCache();
1427
1428 /* Initialize allocation and delayed dereferencing */
1429 CmpInitCmPrivateAlloc();
1430 CmpInitCmPrivateDelayAlloc();
1431 CmpInitDelayDerefKCBEngine();
1432
1433 /* Initialize callbacks */
1434 CmpInitCallback();
1435
1436 /* Initialize self healing */
1437 KeInitializeGuardedMutex(&CmpSelfHealQueueLock);
1438 InitializeListHead(&CmpSelfHealQueueListHead);
1439
1440 /* Save the current process and lock the registry */
1441 CmpSystemProcess = PsGetCurrentProcess();
1442
1443 /* Create the key object types */
1444 Status = CmpCreateObjectTypes();
1445 if (!NT_SUCCESS(Status))
1446 {
1447 /* Bugcheck */
1448 KeBugCheckEx(CONFIG_INITIALIZATION_FAILED, 1, 1, Status, 0);
1449 }
1450
1451 /* Build the master hive */
1452 Status = CmpInitializeHive((PCMHIVE*)&CmiVolatileHive,
1453 HINIT_CREATE,
1454 HIVE_VOLATILE,
1455 HFILE_TYPE_PRIMARY,
1456 NULL,
1457 NULL,
1458 NULL,
1459 NULL,
1460 NULL,
1461 0);
1462 if (!NT_SUCCESS(Status))
1463 {
1464 /* Bugcheck */
1465 KeBugCheckEx(CONFIG_INITIALIZATION_FAILED, 1, 2, Status, 0);
1466 }
1467
1468 /* Create the \REGISTRY key node */
1469 if (!CmpCreateRegistryRoot())
1470 {
1471 /* Bugcheck */
1472 KeBugCheckEx(CONFIG_INITIALIZATION_FAILED, 1, 3, 0, 0);
1473 }
1474
1475 /* Create the default security descriptor */
1476 SecurityDescriptor = CmpHiveRootSecurityDescriptor();
1477
1478 /* Create '\Registry\Machine' key. */
1479 RtlInitUnicodeString(&KeyName, L"\\REGISTRY\\MACHINE");
1480 InitializeObjectAttributes(&ObjectAttributes,
1481 &KeyName,
1482 OBJ_CASE_INSENSITIVE,
1483 NULL,
1484 SecurityDescriptor);
1485 Status = NtCreateKey(&KeyHandle,
1486 KEY_READ | KEY_WRITE,
1487 &ObjectAttributes,
1488 0,
1489 NULL,
1490 0,
1491 NULL);
1492 if (!NT_SUCCESS(Status))
1493 {
1494 /* Bugcheck */
1495 KeBugCheckEx(CONFIG_INITIALIZATION_FAILED, 1, 5, Status, 0);
1496 }
1497
1498 /* Close the handle */
1499 NtClose(KeyHandle);
1500
1501 /* Create '\Registry\User' key. */
1502 RtlInitUnicodeString(&KeyName, L"\\REGISTRY\\USER");
1503 InitializeObjectAttributes(&ObjectAttributes,
1504 &KeyName,
1505 OBJ_CASE_INSENSITIVE,
1506 NULL,
1507 SecurityDescriptor);
1508 Status = NtCreateKey(&KeyHandle,
1509 KEY_READ | KEY_WRITE,
1510 &ObjectAttributes,
1511 0,
1512 NULL,
1513 0,
1514 NULL);
1515 if (!NT_SUCCESS(Status))
1516 {
1517 /* Bugcheck */
1518 KeBugCheckEx(CONFIG_INITIALIZATION_FAILED, 1, 6, Status, 0);
1519 }
1520
1521 /* Close the handle */
1522 NtClose(KeyHandle);
1523
1524 /* Initialize the system hive */
1525 if (!CmpInitializeSystemHive(KeLoaderBlock))
1526 {
1527 /* Bugcheck */
1528 KeBugCheckEx(CONFIG_INITIALIZATION_FAILED, 1, 7, 0, 0);
1529 }
1530
1531 /* Create the 'CurrentControlSet' link. */
1532 Status = CmpCreateControlSet(KeLoaderBlock);
1533 if (!NT_SUCCESS(Status))
1534 {
1535 /* Bugcheck */
1536 KeBugCheckEx(CONFIG_INITIALIZATION_FAILED, 1, 8, Status, 0);
1537 }
1538
1539 /* Create the hardware hive */
1540 Status = CmpInitializeHive((PCMHIVE*)&HardwareHive,
1541 HINIT_CREATE,
1542 HIVE_VOLATILE,
1543 HFILE_TYPE_PRIMARY,
1544 NULL,
1545 NULL,
1546 NULL,
1547 NULL,
1548 NULL,
1549 0);
1550 if (!NT_SUCCESS(Status))
1551 {
1552 /* Bugcheck */
1553 KeBugCheckEx(CONFIG_INITIALIZATION_FAILED, 1, 11, Status, 0);
1554 }
1555
1556 /* Add the hive to the hive list */
1557 CmpMachineHiveList[0].CmHive = (PCMHIVE)HardwareHive;
1558
1559 /* Attach it to the machine key */
1560 RtlInitUnicodeString(&KeyName, L"\\Registry\\Machine\\HARDWARE");
1561 Status = CmpLinkHiveToMaster(&KeyName,
1562 NULL,
1563 (PCMHIVE)HardwareHive,
1564 TRUE,
1565 SecurityDescriptor);
1566 if (!NT_SUCCESS(Status))
1567 {
1568 /* Bugcheck */
1569 KeBugCheckEx(CONFIG_INITIALIZATION_FAILED, 1, 12, Status, 0);
1570 }
1571
1572 /* FIXME: Add to HiveList key */
1573
1574 /* Free the security descriptor */
1575 ExFreePoolWithTag(SecurityDescriptor, TAG_CM);
1576
1577 /* Fill out the Hardware key with the ARC Data from the Loader */
1578 Status = CmpInitializeHardwareConfiguration(KeLoaderBlock);
1579 if (!NT_SUCCESS(Status))
1580 {
1581 /* Bugcheck */
1582 KeBugCheckEx(CONFIG_INITIALIZATION_FAILED, 1, 13, Status, 0);
1583 }
1584
1585 /* Initialize machine-dependent information into the registry */
1586 Status = CmpInitializeMachineDependentConfiguration(KeLoaderBlock);
1587 if (!NT_SUCCESS(Status))
1588 {
1589 /* Bugcheck */
1590 KeBugCheckEx(CONFIG_INITIALIZATION_FAILED, 1, 14, Status, 0);
1591 }
1592
1593 /* Initialize volatile registry settings */
1594 Status = CmpSetSystemValues(KeLoaderBlock);
1595 if (!NT_SUCCESS(Status))
1596 {
1597 /* Bugcheck */
1598 KeBugCheckEx(CONFIG_INITIALIZATION_FAILED, 1, 15, Status, 0);
1599 }
1600
1601 /* Free the load options */
1602 ExFreePoolWithTag(CmpLoadOptions.Buffer, TAG_CM);
1603
1604 /* If we got here, all went well */
1605 return TRUE;
1606 }
1607
1608 VOID
1609 NTAPI
1610 INIT_FUNCTION
1611 CmpFreeDriverList(IN PHHIVE Hive,
1612 IN PLIST_ENTRY DriverList)
1613 {
1614 PLIST_ENTRY NextEntry, OldEntry;
1615 PBOOT_DRIVER_NODE DriverNode;
1616 PAGED_CODE();
1617
1618 /* Parse the current list */
1619 NextEntry = DriverList->Flink;
1620 while (NextEntry != DriverList)
1621 {
1622 /* Get the driver node */
1623 DriverNode = CONTAINING_RECORD(NextEntry, BOOT_DRIVER_NODE, ListEntry.Link);
1624
1625 /* Get the next entry now, since we're going to free it later */
1626 OldEntry = NextEntry;
1627 NextEntry = NextEntry->Flink;
1628
1629 /* Was there a name? */
1630 if (DriverNode->Name.Buffer)
1631 {
1632 /* Free it */
1633 CmpFree(DriverNode->Name.Buffer, DriverNode->Name.Length);
1634 }
1635
1636 /* Was there a registry path? */
1637 if (DriverNode->ListEntry.RegistryPath.Buffer)
1638 {
1639 /* Free it */
1640 CmpFree(DriverNode->ListEntry.RegistryPath.Buffer,
1641 DriverNode->ListEntry.RegistryPath.MaximumLength);
1642 }
1643
1644 /* Was there a file path? */
1645 if (DriverNode->ListEntry.FilePath.Buffer)
1646 {
1647 /* Free it */
1648 CmpFree(DriverNode->ListEntry.FilePath.Buffer,
1649 DriverNode->ListEntry.FilePath.MaximumLength);
1650 }
1651
1652 /* Now free the node, and move on */
1653 CmpFree(OldEntry, sizeof(BOOT_DRIVER_NODE));
1654 }
1655 }
1656
1657 PUNICODE_STRING*
1658 NTAPI
1659 INIT_FUNCTION
1660 CmGetSystemDriverList(VOID)
1661 {
1662 LIST_ENTRY DriverList;
1663 OBJECT_ATTRIBUTES ObjectAttributes;
1664 NTSTATUS Status;
1665 PCM_KEY_BODY KeyBody;
1666 PHHIVE Hive;
1667 HCELL_INDEX RootCell, ControlCell;
1668 HANDLE KeyHandle;
1669 UNICODE_STRING KeyName;
1670 PLIST_ENTRY NextEntry;
1671 ULONG i;
1672 PUNICODE_STRING* ServicePath = NULL;
1673 BOOLEAN Success, AutoSelect;
1674 PBOOT_DRIVER_LIST_ENTRY DriverEntry;
1675 PAGED_CODE();
1676
1677 /* Initialize the driver list */
1678 InitializeListHead(&DriverList);
1679
1680 /* Open the system hive key */
1681 RtlInitUnicodeString(&KeyName, L"\\Registry\\Machine\\System");
1682 InitializeObjectAttributes(&ObjectAttributes,
1683 &KeyName,
1684 OBJ_CASE_INSENSITIVE,
1685 NULL,
1686 NULL);
1687 Status = NtOpenKey(&KeyHandle, KEY_READ, &ObjectAttributes);
1688 if (!NT_SUCCESS(Status)) return NULL;
1689
1690 /* Reference the key object to get the root hive/cell to access directly */
1691 Status = ObReferenceObjectByHandle(KeyHandle,
1692 KEY_QUERY_VALUE,
1693 CmpKeyObjectType,
1694 KernelMode,
1695 (PVOID*)&KeyBody,
1696 NULL);
1697 if (!NT_SUCCESS(Status))
1698 {
1699 /* Fail */
1700 NtClose(KeyHandle);
1701 return NULL;
1702 }
1703
1704 /* Do all this under the registry lock */
1705 CmpLockRegistryExclusive();
1706
1707 /* Get the hive and key cell */
1708 Hive = KeyBody->KeyControlBlock->KeyHive;
1709 RootCell = KeyBody->KeyControlBlock->KeyCell;
1710
1711 /* Open the current control set key */
1712 RtlInitUnicodeString(&KeyName, L"Current");
1713 ControlCell = CmpFindControlSet(Hive, RootCell, &KeyName, &AutoSelect);
1714 if (ControlCell == HCELL_NIL) goto EndPath;
1715
1716 /* Find all system drivers */
1717 Success = CmpFindDrivers(Hive, ControlCell, SystemLoad, NULL, &DriverList);
1718 if (!Success) goto EndPath;
1719
1720 /* Sort by group/tag */
1721 if (!CmpSortDriverList(Hive, ControlCell, &DriverList)) goto EndPath;
1722
1723 /* Remove circular dependencies (cycles) and sort */
1724 if (!CmpResolveDriverDependencies(&DriverList)) goto EndPath;
1725
1726 /* Loop the list to count drivers */
1727 for (i = 0, NextEntry = DriverList.Flink;
1728 NextEntry != &DriverList;
1729 i++, NextEntry = NextEntry->Flink);
1730
1731 /* Allocate the array */
1732 ServicePath = ExAllocatePool(NonPagedPool, (i + 1) * sizeof(PUNICODE_STRING));
1733 if (!ServicePath) KeBugCheckEx(CONFIG_INITIALIZATION_FAILED, 2, 1, 0, 0);
1734
1735 /* Loop the driver list */
1736 for (i = 0, NextEntry = DriverList.Flink;
1737 NextEntry != &DriverList;
1738 i++, NextEntry = NextEntry->Flink)
1739 {
1740 /* Get the entry */
1741 DriverEntry = CONTAINING_RECORD(NextEntry, BOOT_DRIVER_LIST_ENTRY, Link);
1742
1743 /* Allocate the path for the caller and duplicate the registry path */
1744 ServicePath[i] = ExAllocatePool(NonPagedPool, sizeof(UNICODE_STRING));
1745 RtlDuplicateUnicodeString(RTL_DUPLICATE_UNICODE_STRING_NULL_TERMINATE,
1746 &DriverEntry->RegistryPath,
1747 ServicePath[i]);
1748 }
1749
1750 /* Terminate the list */
1751 ServicePath[i] = NULL;
1752
1753 EndPath:
1754 /* Free the driver list if we had one */
1755 if (!IsListEmpty(&DriverList)) CmpFreeDriverList(Hive, &DriverList);
1756
1757 /* Unlock the registry */
1758 CmpUnlockRegistry();
1759
1760 /* Close the key handle and dereference the object, then return the path */
1761 ObDereferenceObject(KeyBody);
1762 NtClose(KeyHandle);
1763 return ServicePath;
1764 }
1765
1766 VOID
1767 NTAPI
1768 CmpLockRegistryExclusive(VOID)
1769 {
1770 /* Enter a critical region and lock the registry */
1771 KeEnterCriticalRegion();
1772 ExAcquireResourceExclusiveLite(&CmpRegistryLock, TRUE);
1773
1774 /* Sanity check */
1775 ASSERT(CmpFlushStarveWriters == 0);
1776 RtlGetCallersAddress(&CmpRegistryLockCaller, &CmpRegistryLockCallerCaller);
1777 }
1778
1779 VOID
1780 NTAPI
1781 CmpLockRegistry(VOID)
1782 {
1783 /* Enter a critical region */
1784 KeEnterCriticalRegion();
1785
1786 /* Check if we have to starve writers */
1787 if (CmpFlushStarveWriters)
1788 {
1789 /* Starve exlusive waiters */
1790 ExAcquireSharedStarveExclusive(&CmpRegistryLock, TRUE);
1791 }
1792 else
1793 {
1794 /* Just grab the lock */
1795 ExAcquireResourceSharedLite(&CmpRegistryLock, TRUE);
1796 }
1797 }
1798
1799 BOOLEAN
1800 NTAPI
1801 CmpTestRegistryLock(VOID)
1802 {
1803 /* Test the lock */
1804 return !ExIsResourceAcquiredSharedLite(&CmpRegistryLock) ? FALSE : TRUE;
1805 }
1806
1807 BOOLEAN
1808 NTAPI
1809 CmpTestRegistryLockExclusive(VOID)
1810 {
1811 /* Test the lock */
1812 return !ExIsResourceAcquiredExclusiveLite(&CmpRegistryLock) ? FALSE : TRUE;
1813 }
1814
1815 VOID
1816 NTAPI
1817 CmpLockHiveFlusherExclusive(IN PCMHIVE Hive)
1818 {
1819 /* Lock the flusher. We should already be in a critical section */
1820 CMP_ASSERT_REGISTRY_LOCK_OR_LOADING(Hive);
1821 ASSERT((ExIsResourceAcquiredShared(Hive->FlusherLock) == 0) &&
1822 (ExIsResourceAcquiredExclusiveLite(Hive->FlusherLock) == 0));
1823 ExAcquireResourceExclusiveLite(Hive->FlusherLock, TRUE);
1824 }
1825
1826 VOID
1827 NTAPI
1828 CmpLockHiveFlusherShared(IN PCMHIVE Hive)
1829 {
1830 /* Lock the flusher. We should already be in a critical section */
1831 CMP_ASSERT_REGISTRY_LOCK_OR_LOADING(Hive);
1832 ASSERT((ExIsResourceAcquiredShared(Hive->FlusherLock) == 0) &&
1833 (ExIsResourceAcquiredExclusiveLite(Hive->FlusherLock) == 0));
1834 ExAcquireResourceSharedLite(Hive->FlusherLock, TRUE);
1835 }
1836
1837 VOID
1838 NTAPI
1839 CmpUnlockHiveFlusher(IN PCMHIVE Hive)
1840 {
1841 /* Sanity check */
1842 CMP_ASSERT_REGISTRY_LOCK_OR_LOADING(Hive);
1843 CMP_ASSERT_FLUSH_LOCK(Hive);
1844
1845 /* Release the lock */
1846 ExReleaseResourceLite(Hive->FlusherLock);
1847 }
1848
1849 BOOLEAN
1850 NTAPI
1851 CmpTestHiveFlusherLockShared(IN PCMHIVE Hive)
1852 {
1853 /* Test the lock */
1854 return !ExIsResourceAcquiredSharedLite(Hive->FlusherLock) ? FALSE : TRUE;
1855 }
1856
1857 BOOLEAN
1858 NTAPI
1859 CmpTestHiveFlusherLockExclusive(IN PCMHIVE Hive)
1860 {
1861 /* Test the lock */
1862 return !ExIsResourceAcquiredExclusiveLite(Hive->FlusherLock) ? FALSE : TRUE;
1863 }
1864
1865 VOID
1866 NTAPI
1867 CmpUnlockRegistry(VOID)
1868 {
1869 /* Sanity check */
1870 CMP_ASSERT_REGISTRY_LOCK();
1871
1872 /* Check if we should flush the registry */
1873 if (CmpFlushOnLockRelease)
1874 {
1875 /* The registry should be exclusively locked for this */
1876 CMP_ASSERT_EXCLUSIVE_REGISTRY_LOCK();
1877
1878 /* Flush the registry */
1879 CmpDoFlushAll(TRUE);
1880 CmpFlushOnLockRelease = FALSE;
1881 }
1882
1883 /* Release the lock and leave the critical region */
1884 ExReleaseResourceLite(&CmpRegistryLock);
1885 KeLeaveCriticalRegion();
1886 }
1887
1888 VOID
1889 NTAPI
1890 CmpAcquireTwoKcbLocksExclusiveByKey(IN ULONG ConvKey1,
1891 IN ULONG ConvKey2)
1892 {
1893 ULONG Index1, Index2;
1894
1895 /* Sanity check */
1896 CMP_ASSERT_REGISTRY_LOCK();
1897
1898 /* Get hash indexes */
1899 Index1 = GET_HASH_INDEX(ConvKey1);
1900 Index2 = GET_HASH_INDEX(ConvKey2);
1901
1902 /* See which one is highest */
1903 if (Index1 < Index2)
1904 {
1905 /* Grab them in the proper order */
1906 CmpAcquireKcbLockExclusiveByKey(ConvKey1);
1907 CmpAcquireKcbLockExclusiveByKey(ConvKey2);
1908 }
1909 else
1910 {
1911 /* Grab the second one first, then the first */
1912 CmpAcquireKcbLockExclusiveByKey(ConvKey2);
1913 if (Index1 != Index2) CmpAcquireKcbLockExclusiveByKey(ConvKey1);
1914 }
1915 }
1916
1917 VOID
1918 NTAPI
1919 CmpReleaseTwoKcbLockByKey(IN ULONG ConvKey1,
1920 IN ULONG ConvKey2)
1921 {
1922 ULONG Index1, Index2;
1923
1924 /* Sanity check */
1925 CMP_ASSERT_REGISTRY_LOCK();
1926
1927 /* Get hash indexes */
1928 Index1 = GET_HASH_INDEX(ConvKey1);
1929 Index2 = GET_HASH_INDEX(ConvKey2);
1930 ASSERT((GET_HASH_ENTRY(CmpCacheTable, ConvKey2).Owner == KeGetCurrentThread()) ||
1931 (CmpTestRegistryLockExclusive()));
1932
1933 /* See which one is highest */
1934 if (Index1 < Index2)
1935 {
1936 /* Grab them in the proper order */
1937 ASSERT((GET_HASH_ENTRY(CmpCacheTable, ConvKey1).Owner == KeGetCurrentThread()) ||
1938 (CmpTestRegistryLockExclusive()));
1939 CmpReleaseKcbLockByKey(ConvKey2);
1940 CmpReleaseKcbLockByKey(ConvKey1);
1941 }
1942 else
1943 {
1944 /* Release the first one first, then the second */
1945 if (Index1 != Index2)
1946 {
1947 ASSERT((GET_HASH_ENTRY(CmpCacheTable, ConvKey1).Owner == KeGetCurrentThread()) ||
1948 (CmpTestRegistryLockExclusive()));
1949 CmpReleaseKcbLockByKey(ConvKey1);
1950 }
1951 CmpReleaseKcbLockByKey(ConvKey2);
1952 }
1953 }
1954
1955 VOID
1956 NTAPI
1957 CmShutdownSystem(VOID)
1958 {
1959 /* Kill the workers */
1960 if (!CmFirstTime) CmpShutdownWorkers();
1961
1962 /* Flush all hives */
1963 CmpLockRegistryExclusive();
1964 CmpDoFlushAll(TRUE);
1965 CmpUnlockRegistry();
1966 }
1967
1968 VOID
1969 NTAPI
1970 CmpSetVersionData(VOID)
1971 {
1972 OBJECT_ATTRIBUTES ObjectAttributes;
1973 UNICODE_STRING KeyName;
1974 UNICODE_STRING ValueName;
1975 UNICODE_STRING ValueData;
1976 HANDLE SoftwareKeyHandle = NULL;
1977 HANDLE MicrosoftKeyHandle = NULL;
1978 HANDLE WindowsNtKeyHandle = NULL;
1979 HANDLE CurrentVersionKeyHandle = NULL;
1980 WCHAR Buffer[128];
1981 NTSTATUS Status;
1982
1983 /* Open the 'CurrentVersion' key */
1984 RtlInitUnicodeString(&KeyName,
1985 L"\\REGISTRY\\MACHINE\\SOFTWARE");
1986
1987 InitializeObjectAttributes(&ObjectAttributes,
1988 &KeyName,
1989 OBJ_CASE_INSENSITIVE,
1990 NULL,
1991 NULL);
1992
1993 Status = NtCreateKey(&SoftwareKeyHandle,
1994 KEY_CREATE_SUB_KEY,
1995 &ObjectAttributes,
1996 0,
1997 NULL,
1998 0,
1999 NULL);
2000 if (!NT_SUCCESS(Status))
2001 {
2002 DPRINT1("Failed to create key %wZ (Status: %08lx)\n", &KeyName, Status);
2003 return;
2004 }
2005
2006 /* Open the 'CurrentVersion' key */
2007 RtlInitUnicodeString(&KeyName,
2008 L"Microsoft");
2009
2010 InitializeObjectAttributes(&ObjectAttributes,
2011 &KeyName,
2012 OBJ_CASE_INSENSITIVE,
2013 SoftwareKeyHandle,
2014 NULL);
2015
2016 Status = NtCreateKey(&MicrosoftKeyHandle,
2017 KEY_CREATE_SUB_KEY,
2018 &ObjectAttributes,
2019 0,
2020 NULL,
2021 0,
2022 NULL);
2023 if (!NT_SUCCESS(Status))
2024 {
2025 DPRINT1("Failed to create key %wZ (Status: %08lx)\n", &KeyName, Status);
2026 goto done;
2027 }
2028
2029 /* Open the 'CurrentVersion' key */
2030 RtlInitUnicodeString(&KeyName,
2031 L"Windows NT");
2032
2033 InitializeObjectAttributes(&ObjectAttributes,
2034 &KeyName,
2035 OBJ_CASE_INSENSITIVE,
2036 MicrosoftKeyHandle,
2037 NULL);
2038
2039 Status = NtCreateKey(&WindowsNtKeyHandle,
2040 KEY_CREATE_SUB_KEY,
2041 &ObjectAttributes,
2042 0,
2043 NULL,
2044 0,
2045 NULL);
2046 if (!NT_SUCCESS(Status))
2047 {
2048 DPRINT1("Failed to create key %wZ (Status: %08lx)\n", &KeyName, Status);
2049 goto done;
2050 }
2051
2052 /* Open the 'CurrentVersion' key */
2053 RtlInitUnicodeString(&KeyName,
2054 L"CurrentVersion");
2055
2056 InitializeObjectAttributes(&ObjectAttributes,
2057 &KeyName,
2058 OBJ_CASE_INSENSITIVE,
2059 WindowsNtKeyHandle,
2060 NULL);
2061
2062 Status = NtCreateKey(&CurrentVersionKeyHandle,
2063 KEY_CREATE_SUB_KEY | KEY_SET_VALUE,
2064 &ObjectAttributes,
2065 0,
2066 NULL,
2067 0,
2068 NULL);
2069 if (!NT_SUCCESS(Status))
2070 {
2071 DPRINT1("Failed to create key %wZ (Status: %08lx)\n", &KeyName, Status);
2072 goto done;
2073 }
2074
2075 /* Set the 'CurrentType' value */
2076 RtlInitUnicodeString(&ValueName,
2077 L"CurrentType");
2078
2079 #ifdef CONFIG_SMP
2080 wcscpy(Buffer, L"Multiprocessor");
2081 #else
2082 wcscpy(Buffer, L"Uniprocessor");
2083 #endif
2084
2085 wcscat(Buffer, L" ");
2086
2087 #if (DBG == 1)
2088 wcscat(Buffer, L"Checked");
2089 #else
2090 wcscat(Buffer, L"Free");
2091 #endif
2092
2093 RtlInitUnicodeString(&ValueData,
2094 Buffer);
2095
2096 NtSetValueKey(CurrentVersionKeyHandle,
2097 &ValueName,
2098 0,
2099 REG_SZ,
2100 ValueData.Buffer,
2101 ValueData.Length + sizeof(WCHAR));
2102
2103 done:;
2104 /* Close the keys */
2105 if (CurrentVersionKeyHandle != NULL)
2106 NtClose(CurrentVersionKeyHandle);
2107
2108 if (WindowsNtKeyHandle != NULL)
2109 NtClose(WindowsNtKeyHandle);
2110
2111 if (MicrosoftKeyHandle != NULL)
2112 NtClose(MicrosoftKeyHandle);
2113
2114 if (SoftwareKeyHandle != NULL)
2115 NtClose(SoftwareKeyHandle);
2116 }
2117
2118 /* EOF */