- Merge the remaining portion of the wlan-bringup branch
[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;
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 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
870 /* Create it */
871 return ObCreateObjectType(&Name, &ObjectTypeInitializer, NULL, &CmpKeyObjectType);
872 }
873
874 BOOLEAN
875 NTAPI
876 INIT_FUNCTION
877 CmpCreateRootNode(IN PHHIVE Hive,
878 IN PCWSTR Name,
879 OUT PHCELL_INDEX Index)
880 {
881 UNICODE_STRING KeyName;
882 PCM_KEY_NODE KeyCell;
883 LARGE_INTEGER SystemTime;
884 PAGED_CODE();
885
886 /* Initialize the node name and allocate it */
887 RtlInitUnicodeString(&KeyName, Name);
888 *Index = HvAllocateCell(Hive,
889 FIELD_OFFSET(CM_KEY_NODE, Name) +
890 CmpNameSize(Hive, &KeyName),
891 Stable,
892 HCELL_NIL);
893 if (*Index == HCELL_NIL) return FALSE;
894
895 /* Set the cell index and get the data */
896 Hive->BaseBlock->RootCell = *Index;
897 KeyCell = (PCM_KEY_NODE)HvGetCell(Hive, *Index);
898 if (!KeyCell) return FALSE;
899
900 /* Setup the cell */
901 KeyCell->Signature = (USHORT)CM_KEY_NODE_SIGNATURE;
902 KeyCell->Flags = KEY_HIVE_ENTRY | KEY_NO_DELETE;
903 KeQuerySystemTime(&SystemTime);
904 KeyCell->LastWriteTime = SystemTime;
905 KeyCell->Parent = HCELL_NIL;
906 KeyCell->SubKeyCounts[Stable] = 0;
907 KeyCell->SubKeyCounts[Volatile] = 0;
908 KeyCell->SubKeyLists[Stable] = HCELL_NIL;
909 KeyCell->SubKeyLists[Volatile] = HCELL_NIL;
910 KeyCell->ValueList.Count = 0;
911 KeyCell->ValueList.List = HCELL_NIL;
912 KeyCell->Security = HCELL_NIL;
913 KeyCell->Class = HCELL_NIL;
914 KeyCell->ClassLength = 0;
915 KeyCell->MaxNameLen = 0;
916 KeyCell->MaxClassLen = 0;
917 KeyCell->MaxValueNameLen = 0;
918 KeyCell->MaxValueDataLen = 0;
919
920 /* Copy the name (this will also set the length) */
921 KeyCell->NameLength = CmpCopyName(Hive, (PWCHAR)KeyCell->Name, &KeyName);
922
923 /* Check if the name was compressed */
924 if (KeyCell->NameLength < KeyName.Length)
925 {
926 /* Set the flag */
927 KeyCell->Flags |= KEY_COMP_NAME;
928 }
929
930 /* Return success */
931 HvReleaseCell(Hive, *Index);
932 return TRUE;
933 }
934
935 BOOLEAN
936 NTAPI
937 INIT_FUNCTION
938 CmpCreateRegistryRoot(VOID)
939 {
940 UNICODE_STRING KeyName;
941 OBJECT_ATTRIBUTES ObjectAttributes;
942 PCM_KEY_BODY RootKey;
943 HCELL_INDEX RootIndex;
944 NTSTATUS Status;
945 PCM_KEY_NODE KeyCell;
946 PSECURITY_DESCRIPTOR SecurityDescriptor;
947 PCM_KEY_CONTROL_BLOCK Kcb;
948 PAGED_CODE();
949
950 /* Setup the root node */
951 if (!CmpCreateRootNode(&CmiVolatileHive->Hive, L"REGISTRY", &RootIndex))
952 {
953 /* We failed */
954 return FALSE;
955 }
956
957 /* Create '\Registry' key. */
958 RtlInitUnicodeString(&KeyName, L"\\REGISTRY");
959 SecurityDescriptor = CmpHiveRootSecurityDescriptor();
960 InitializeObjectAttributes(&ObjectAttributes,
961 &KeyName,
962 OBJ_CASE_INSENSITIVE,
963 NULL,
964 NULL);
965 Status = ObCreateObject(KernelMode,
966 CmpKeyObjectType,
967 &ObjectAttributes,
968 KernelMode,
969 NULL,
970 sizeof(CM_KEY_BODY),
971 0,
972 0,
973 (PVOID*)&RootKey);
974 ExFreePoolWithTag(SecurityDescriptor, TAG_CM);
975 if (!NT_SUCCESS(Status)) return FALSE;
976
977 /* Sanity check, and get the key cell */
978 ASSERT((&CmiVolatileHive->Hive)->ReleaseCellRoutine == NULL);
979 KeyCell = (PCM_KEY_NODE)HvGetCell(&CmiVolatileHive->Hive, RootIndex);
980 if (!KeyCell) return FALSE;
981
982 /* Create the KCB */
983 RtlInitUnicodeString(&KeyName, L"\\REGISTRY");
984 Kcb = CmpCreateKeyControlBlock(&CmiVolatileHive->Hive,
985 RootIndex,
986 KeyCell,
987 NULL,
988 0,
989 &KeyName);
990 if (!Kcb) return FALSE;
991
992 /* Initialize the object */
993 RootKey->KeyControlBlock = Kcb;
994 RootKey->Type = '20yk';
995 RootKey->NotifyBlock = NULL;
996 RootKey->ProcessID = PsGetCurrentProcessId();
997
998 /* Link with KCB */
999 EnlistKeyBodyWithKCB(RootKey, 0);
1000
1001 /* Insert the key into the namespace */
1002 Status = ObInsertObject(RootKey,
1003 NULL,
1004 KEY_ALL_ACCESS,
1005 0,
1006 NULL,
1007 &CmpRegistryRootHandle);
1008 if (!NT_SUCCESS(Status)) return FALSE;
1009
1010 /* Reference the key again so that we never lose it */
1011 Status = ObReferenceObjectByHandle(CmpRegistryRootHandle,
1012 KEY_READ,
1013 NULL,
1014 KernelMode,
1015 (PVOID*)&RootKey,
1016 NULL);
1017 if (!NT_SUCCESS(Status)) return FALSE;
1018
1019 /* Completely sucessful */
1020 return TRUE;
1021 }
1022
1023 NTSTATUS
1024 NTAPI
1025 CmpGetRegistryPath(IN PWCHAR ConfigPath)
1026 {
1027 OBJECT_ATTRIBUTES ObjectAttributes;
1028 NTSTATUS Status;
1029 HANDLE KeyHandle;
1030 PKEY_VALUE_PARTIAL_INFORMATION ValueInfo;
1031 UNICODE_STRING KeyName = RTL_CONSTANT_STRING(L"\\Registry\\Machine\\HARDWARE");
1032 UNICODE_STRING ValueName = RTL_CONSTANT_STRING(L"InstallPath");
1033 ULONG BufferSize, ResultSize;
1034
1035 /* Check if we are booted in setup */
1036 if (ExpInTextModeSetup)
1037 {
1038 /* Setup the object attributes */
1039 InitializeObjectAttributes(&ObjectAttributes,
1040 &KeyName,
1041 OBJ_CASE_INSENSITIVE,
1042 NULL,
1043 NULL);
1044 /* Open the key */
1045 Status = ZwOpenKey(&KeyHandle,
1046 KEY_ALL_ACCESS,
1047 &ObjectAttributes);
1048 if (!NT_SUCCESS(Status)) return Status;
1049
1050 /* Allocate the buffer */
1051 BufferSize = sizeof(KEY_VALUE_PARTIAL_INFORMATION) + 4096;
1052 ValueInfo = ExAllocatePoolWithTag(PagedPool, BufferSize, TAG_CM);
1053 if (!ValueInfo)
1054 {
1055 /* Fail */
1056 ZwClose(KeyHandle);
1057 return STATUS_INSUFFICIENT_RESOURCES;
1058 }
1059
1060 /* Query the value */
1061 Status = ZwQueryValueKey(KeyHandle,
1062 &ValueName,
1063 KeyValuePartialInformation,
1064 ValueInfo,
1065 BufferSize,
1066 &ResultSize);
1067 ZwClose(KeyHandle);
1068 if (!NT_SUCCESS(Status))
1069 {
1070 /* Fail */
1071 ExFreePoolWithTag(ValueInfo, TAG_CM);
1072 return Status;
1073 }
1074
1075 /* Copy the config path and null-terminate it */
1076 RtlCopyMemory(ConfigPath,
1077 ValueInfo->Data,
1078 ValueInfo->DataLength);
1079 ConfigPath[ValueInfo->DataLength / sizeof(WCHAR)] = UNICODE_NULL;
1080 ExFreePoolWithTag(ValueInfo, TAG_CM);
1081 }
1082 else
1083 {
1084 /* Just use default path */
1085 wcscpy(ConfigPath, L"\\SystemRoot");
1086 }
1087
1088 /* Add registry path */
1089 wcscat(ConfigPath, L"\\System32\\Config\\");
1090
1091 /* Done */
1092 return STATUS_SUCCESS;
1093 }
1094
1095 VOID
1096 NTAPI
1097 CmpLoadHiveThread(IN PVOID StartContext)
1098 {
1099 WCHAR FileBuffer[MAX_PATH], RegBuffer[MAX_PATH], ConfigPath[MAX_PATH];
1100 UNICODE_STRING TempName, FileName, RegName;
1101 ULONG FileStart, i, ErrorResponse, WorkerCount, Length;
1102 //ULONG RegStart;
1103 ULONG PrimaryDisposition, SecondaryDisposition, ClusterSize;
1104 PCMHIVE CmHive;
1105 HANDLE PrimaryHandle, LogHandle;
1106 NTSTATUS Status = STATUS_SUCCESS;
1107 PVOID ErrorParameters;
1108 PAGED_CODE();
1109
1110 /* Get the hive index, make sure it makes sense */
1111 i = PtrToUlong(StartContext);
1112 ASSERT(CmpMachineHiveList[i].Name != NULL);
1113
1114 /* We were started */
1115 CmpMachineHiveList[i].ThreadStarted = TRUE;
1116
1117 /* Build the file name and registry name strings */
1118 RtlInitEmptyUnicodeString(&FileName, FileBuffer, MAX_PATH);
1119 RtlInitEmptyUnicodeString(&RegName, RegBuffer, MAX_PATH);
1120
1121 /* Now build the system root path */
1122 CmpGetRegistryPath(ConfigPath);
1123 RtlInitUnicodeString(&TempName, ConfigPath);
1124 RtlAppendStringToString((PSTRING)&FileName, (PSTRING)&TempName);
1125 FileStart = FileName.Length;
1126
1127 /* And build the registry root path */
1128 RtlInitUnicodeString(&TempName, L"\\REGISTRY\\");
1129 RtlAppendStringToString((PSTRING)&RegName, (PSTRING)&TempName);
1130 //RegStart = RegName.Length;
1131
1132 /* Build the base name */
1133 RtlInitUnicodeString(&TempName, CmpMachineHiveList[i].BaseName);
1134 RtlAppendStringToString((PSTRING)&RegName, (PSTRING)&TempName);
1135
1136 /* Check if this is a child of the root */
1137 if (RegName.Buffer[RegName.Length / sizeof(WCHAR) - 1] == '\\')
1138 {
1139 /* Then setup the whole name */
1140 RtlInitUnicodeString(&TempName, CmpMachineHiveList[i].Name);
1141 RtlAppendStringToString((PSTRING)&RegName, (PSTRING)&TempName);
1142 }
1143
1144 /* Now add the rest of the file name */
1145 RtlInitUnicodeString(&TempName, CmpMachineHiveList[i].Name);
1146 FileName.Length = FileStart;
1147 RtlAppendStringToString((PSTRING)&FileName, (PSTRING)&TempName);
1148 if (!CmpMachineHiveList[i].CmHive)
1149 {
1150 /* We need to allocate a new hive structure */
1151 CmpMachineHiveList[i].Allocate = TRUE;
1152
1153 /* Load the hive file */
1154 Status = CmpInitHiveFromFile(&FileName,
1155 CmpMachineHiveList[i].HHiveFlags,
1156 &CmHive,
1157 &CmpMachineHiveList[i].Allocate,
1158 0);
1159 if (!(NT_SUCCESS(Status)) ||
1160 (!(CmHive->FileHandles[HFILE_TYPE_LOG]) && !(CmpMiniNTBoot))) // HACK
1161 {
1162 /* We failed or couldn't get a log file, raise a hard error */
1163 ErrorParameters = &FileName;
1164 NtRaiseHardError(STATUS_CANNOT_LOAD_REGISTRY_FILE,
1165 1,
1166 1,
1167 (PULONG_PTR)&ErrorParameters,
1168 OptionOk,
1169 &ErrorResponse);
1170 }
1171
1172 /* Set the hive flags and newly allocated hive pointer */
1173 CmHive->Flags = CmpMachineHiveList[i].CmHiveFlags;
1174 CmpMachineHiveList[i].CmHive2 = CmHive;
1175 }
1176 else
1177 {
1178 /* We already have a hive, is it volatile? */
1179 CmHive = CmpMachineHiveList[i].CmHive;
1180 if (!(CmHive->Hive.HiveFlags & HIVE_VOLATILE))
1181 {
1182 /* It's now, open the hive file and log */
1183 Status = CmpOpenHiveFiles(&FileName,
1184 L".LOG",
1185 &PrimaryHandle,
1186 &LogHandle,
1187 &PrimaryDisposition,
1188 &SecondaryDisposition,
1189 TRUE,
1190 TRUE,
1191 FALSE,
1192 &ClusterSize);
1193 if (!(NT_SUCCESS(Status)) || !(LogHandle))
1194 {
1195 /* Couldn't open the hive or its log file, raise a hard error */
1196 ErrorParameters = &FileName;
1197 NtRaiseHardError(STATUS_CANNOT_LOAD_REGISTRY_FILE,
1198 1,
1199 1,
1200 (PULONG_PTR)&ErrorParameters,
1201 OptionOk,
1202 &ErrorResponse);
1203
1204 /* And bugcheck for posterity's sake */
1205 KeBugCheckEx(BAD_SYSTEM_CONFIG_INFO, 9, 0, i, Status);
1206 }
1207
1208 /* Save the file handles. This should remove our sync hacks */
1209 CmHive->FileHandles[HFILE_TYPE_LOG] = LogHandle;
1210 CmHive->FileHandles[HFILE_TYPE_PRIMARY] = PrimaryHandle;
1211
1212 /* Allow lazy flushing since the handles are there -- remove sync hacks */
1213 //ASSERT(CmHive->Hive.HiveFlags & HIVE_NOLAZYFLUSH);
1214 CmHive->Hive.HiveFlags &= ~HIVE_NOLAZYFLUSH;
1215
1216 /* Get the real size of the hive */
1217 Length = CmHive->Hive.Storage[Stable].Length + HBLOCK_SIZE;
1218
1219 /* Check if the cluster size doesn't match */
1220 if (CmHive->Hive.Cluster != ClusterSize) ASSERT(FALSE);
1221
1222 /* Set the file size */
1223 DPRINT("FIXME: Should set file size: %lx\n", Length);
1224 //if (!CmpFileSetSize((PHHIVE)CmHive, HFILE_TYPE_PRIMARY, Length, Length))
1225 {
1226 /* This shouldn't fail */
1227 //ASSERT(FALSE);
1228 }
1229
1230 /* Another thing we don't support is NTLDR-recovery */
1231 if (CmHive->Hive.BaseBlock->BootRecover) ASSERT(FALSE);
1232
1233 /* Finally, set our allocated hive to the same hive we've had */
1234 CmpMachineHiveList[i].CmHive2 = CmHive;
1235 ASSERT(CmpMachineHiveList[i].CmHive == CmpMachineHiveList[i].CmHive2);
1236 }
1237 }
1238
1239 /* We're done */
1240 CmpMachineHiveList[i].ThreadFinished = TRUE;
1241
1242 /* Check if we're the last worker */
1243 WorkerCount = InterlockedIncrement(&CmpLoadWorkerIncrement);
1244 if (WorkerCount == CM_NUMBER_OF_MACHINE_HIVES)
1245 {
1246 /* Signal the event */
1247 KeSetEvent(&CmpLoadWorkerEvent, 0, FALSE);
1248 }
1249
1250 /* Kill the thread */
1251 PsTerminateSystemThread(Status);
1252 }
1253
1254 VOID
1255 NTAPI
1256 CmpInitializeHiveList(IN USHORT Flag)
1257 {
1258 WCHAR FileBuffer[MAX_PATH], RegBuffer[MAX_PATH], ConfigPath[MAX_PATH];
1259 UNICODE_STRING TempName, FileName, RegName;
1260 HANDLE Thread;
1261 NTSTATUS Status;
1262 ULONG RegStart, i;
1263 PSECURITY_DESCRIPTOR SecurityDescriptor;
1264 PAGED_CODE();
1265
1266 /* Allow writing for now */
1267 CmpNoWrite = FALSE;
1268
1269 /* Build the file name and registry name strings */
1270 RtlInitEmptyUnicodeString(&FileName, FileBuffer, MAX_PATH);
1271 RtlInitEmptyUnicodeString(&RegName, RegBuffer, MAX_PATH);
1272
1273 /* Now build the system root path */
1274 CmpGetRegistryPath(ConfigPath);
1275 RtlInitUnicodeString(&TempName, ConfigPath);
1276 RtlAppendStringToString((PSTRING)&FileName, (PSTRING)&TempName);
1277
1278 /* And build the registry root path */
1279 RtlInitUnicodeString(&TempName, L"\\REGISTRY\\");
1280 RtlAppendStringToString((PSTRING)&RegName, (PSTRING)&TempName);
1281 RegStart = RegName.Length;
1282
1283 /* Setup the event to synchronize workers */
1284 KeInitializeEvent(&CmpLoadWorkerEvent, SynchronizationEvent, FALSE);
1285
1286 /* Enter special boot condition */
1287 CmpSpecialBootCondition = TRUE;
1288
1289 /* Create the SD for the root hives */
1290 SecurityDescriptor = CmpHiveRootSecurityDescriptor();
1291
1292 /* Loop every hive we care about */
1293 for (i = 0; i < CM_NUMBER_OF_MACHINE_HIVES; i++)
1294 {
1295 /* Make sure the list is setup */
1296 ASSERT(CmpMachineHiveList[i].Name != NULL);
1297
1298 /* Create a thread to handle this hive */
1299 Status = PsCreateSystemThread(&Thread,
1300 THREAD_ALL_ACCESS,
1301 NULL,
1302 0,
1303 NULL,
1304 CmpLoadHiveThread,
1305 UlongToPtr(i));
1306 if (NT_SUCCESS(Status))
1307 {
1308 /* We don't care about the handle -- the thread self-terminates */
1309 ZwClose(Thread);
1310 }
1311 else
1312 {
1313 /* Can't imagine this happening */
1314 KeBugCheckEx(BAD_SYSTEM_CONFIG_INFO, 9, 3, i, Status);
1315 }
1316 }
1317
1318 /* Make sure we've reached the end of the list */
1319 ASSERT(CmpMachineHiveList[i].Name == NULL);
1320
1321 /* Wait for hive loading to finish */
1322 KeWaitForSingleObject(&CmpLoadWorkerEvent,
1323 Executive,
1324 KernelMode,
1325 FALSE,
1326 NULL);
1327
1328 /* Exit the special boot condition and make sure all workers completed */
1329 CmpSpecialBootCondition = FALSE;
1330 ASSERT(CmpLoadWorkerIncrement == CM_NUMBER_OF_MACHINE_HIVES);
1331
1332 /* Loop hives again */
1333 for (i = 0; i < CM_NUMBER_OF_MACHINE_HIVES; i++)
1334 {
1335 /* Make sure the thread ran and finished */
1336 ASSERT(CmpMachineHiveList[i].ThreadFinished == TRUE);
1337 ASSERT(CmpMachineHiveList[i].ThreadStarted == TRUE);
1338
1339 /* Check if this was a new hive */
1340 if (!CmpMachineHiveList[i].CmHive)
1341 {
1342 /* Make sure we allocated something */
1343 ASSERT(CmpMachineHiveList[i].CmHive2 != NULL);
1344
1345 /* Build the base name */
1346 RegName.Length = RegStart;
1347 RtlInitUnicodeString(&TempName, CmpMachineHiveList[i].BaseName);
1348 RtlAppendStringToString((PSTRING)&RegName, (PSTRING)&TempName);
1349
1350 /* Check if this is a child of the root */
1351 if (RegName.Buffer[RegName.Length / sizeof(WCHAR) - 1] == '\\')
1352 {
1353 /* Then setup the whole name */
1354 RtlInitUnicodeString(&TempName, CmpMachineHiveList[i].Name);
1355 RtlAppendStringToString((PSTRING)&RegName, (PSTRING)&TempName);
1356 }
1357
1358 /* Now link the hive to its master */
1359 Status = CmpLinkHiveToMaster(&RegName,
1360 NULL,
1361 CmpMachineHiveList[i].CmHive2,
1362 CmpMachineHiveList[i].Allocate,
1363 SecurityDescriptor);
1364 if (Status != STATUS_SUCCESS)
1365 {
1366 /* Linking needs to work */
1367 KeBugCheckEx(CONFIG_LIST_FAILED, 11, Status, i, (ULONG_PTR)&RegName);
1368 }
1369
1370 /* Check if we had to allocate a new hive */
1371 if (CmpMachineHiveList[i].Allocate)
1372 {
1373 /* Sync the new hive */
1374 //HvSyncHive((PHHIVE)(CmpMachineHiveList[i].CmHive2));
1375 }
1376 }
1377
1378 /* Check if we created a new hive */
1379 if (CmpMachineHiveList[i].CmHive2)
1380 {
1381 /* TODO: Add to HiveList key */
1382 }
1383 }
1384
1385 /* Get rid of the SD */
1386 ExFreePoolWithTag(SecurityDescriptor, TAG_CM);
1387
1388 /* FIXME: Link SECURITY to SAM */
1389
1390 /* FIXME: Link S-1-5-18 to .Default */
1391 }
1392
1393 BOOLEAN
1394 NTAPI
1395 INIT_FUNCTION
1396 CmInitSystem1(VOID)
1397 {
1398 OBJECT_ATTRIBUTES ObjectAttributes;
1399 UNICODE_STRING KeyName;
1400 HANDLE KeyHandle;
1401 NTSTATUS Status;
1402 PCMHIVE HardwareHive;
1403 PSECURITY_DESCRIPTOR SecurityDescriptor;
1404 PAGED_CODE();
1405
1406 /* Check if this is PE-boot */
1407 if (InitIsWinPEMode)
1408 {
1409 /* Set registry to PE mode */
1410 CmpMiniNTBoot = TRUE;
1411 CmpShareSystemHives = TRUE;
1412 }
1413
1414 /* Initialize the hive list and lock */
1415 InitializeListHead(&CmpHiveListHead);
1416 ExInitializePushLock(&CmpHiveListHeadLock);
1417 ExInitializePushLock(&CmpLoadHiveLock);
1418
1419 /* Initialize registry lock */
1420 ExInitializeResourceLite(&CmpRegistryLock);
1421
1422 /* Initialize the cache */
1423 CmpInitializeCache();
1424
1425 /* Initialize allocation and delayed dereferencing */
1426 CmpInitCmPrivateAlloc();
1427 CmpInitCmPrivateDelayAlloc();
1428 CmpInitDelayDerefKCBEngine();
1429
1430 /* Initialize callbacks */
1431 CmpInitCallback();
1432
1433 /* Initialize self healing */
1434 KeInitializeGuardedMutex(&CmpSelfHealQueueLock);
1435 InitializeListHead(&CmpSelfHealQueueListHead);
1436
1437 /* Save the current process and lock the registry */
1438 CmpSystemProcess = PsGetCurrentProcess();
1439
1440 /* Create the key object types */
1441 Status = CmpCreateObjectTypes();
1442 if (!NT_SUCCESS(Status))
1443 {
1444 /* Bugcheck */
1445 KeBugCheckEx(CONFIG_INITIALIZATION_FAILED, 1, 1, Status, 0);
1446 }
1447
1448 /* Build the master hive */
1449 Status = CmpInitializeHive((PCMHIVE*)&CmiVolatileHive,
1450 HINIT_CREATE,
1451 HIVE_VOLATILE,
1452 HFILE_TYPE_PRIMARY,
1453 NULL,
1454 NULL,
1455 NULL,
1456 NULL,
1457 NULL,
1458 0);
1459 if (!NT_SUCCESS(Status))
1460 {
1461 /* Bugcheck */
1462 KeBugCheckEx(CONFIG_INITIALIZATION_FAILED, 1, 2, Status, 0);
1463 }
1464
1465 /* Create the \REGISTRY key node */
1466 if (!CmpCreateRegistryRoot())
1467 {
1468 /* Bugcheck */
1469 KeBugCheckEx(CONFIG_INITIALIZATION_FAILED, 1, 3, 0, 0);
1470 }
1471
1472 /* Create the default security descriptor */
1473 SecurityDescriptor = CmpHiveRootSecurityDescriptor();
1474
1475 /* Create '\Registry\Machine' key. */
1476 RtlInitUnicodeString(&KeyName, L"\\REGISTRY\\MACHINE");
1477 InitializeObjectAttributes(&ObjectAttributes,
1478 &KeyName,
1479 OBJ_CASE_INSENSITIVE,
1480 NULL,
1481 SecurityDescriptor);
1482 Status = NtCreateKey(&KeyHandle,
1483 KEY_READ | KEY_WRITE,
1484 &ObjectAttributes,
1485 0,
1486 NULL,
1487 0,
1488 NULL);
1489 if (!NT_SUCCESS(Status))
1490 {
1491 /* Bugcheck */
1492 KeBugCheckEx(CONFIG_INITIALIZATION_FAILED, 1, 5, Status, 0);
1493 }
1494
1495 /* Close the handle */
1496 NtClose(KeyHandle);
1497
1498 /* Create '\Registry\User' key. */
1499 RtlInitUnicodeString(&KeyName, L"\\REGISTRY\\USER");
1500 InitializeObjectAttributes(&ObjectAttributes,
1501 &KeyName,
1502 OBJ_CASE_INSENSITIVE,
1503 NULL,
1504 SecurityDescriptor);
1505 Status = NtCreateKey(&KeyHandle,
1506 KEY_READ | KEY_WRITE,
1507 &ObjectAttributes,
1508 0,
1509 NULL,
1510 0,
1511 NULL);
1512 if (!NT_SUCCESS(Status))
1513 {
1514 /* Bugcheck */
1515 KeBugCheckEx(CONFIG_INITIALIZATION_FAILED, 1, 6, Status, 0);
1516 }
1517
1518 /* Close the handle */
1519 NtClose(KeyHandle);
1520
1521 /* Initialize the system hive */
1522 if (!CmpInitializeSystemHive(KeLoaderBlock))
1523 {
1524 /* Bugcheck */
1525 KeBugCheckEx(CONFIG_INITIALIZATION_FAILED, 1, 7, 0, 0);
1526 }
1527
1528 /* Create the 'CurrentControlSet' link. */
1529 Status = CmpCreateControlSet(KeLoaderBlock);
1530 if (!NT_SUCCESS(Status))
1531 {
1532 /* Bugcheck */
1533 KeBugCheckEx(CONFIG_INITIALIZATION_FAILED, 1, 8, Status, 0);
1534 }
1535
1536 /* Create the hardware hive */
1537 Status = CmpInitializeHive((PCMHIVE*)&HardwareHive,
1538 HINIT_CREATE,
1539 HIVE_VOLATILE,
1540 HFILE_TYPE_PRIMARY,
1541 NULL,
1542 NULL,
1543 NULL,
1544 NULL,
1545 NULL,
1546 0);
1547 if (!NT_SUCCESS(Status))
1548 {
1549 /* Bugcheck */
1550 KeBugCheckEx(CONFIG_INITIALIZATION_FAILED, 1, 11, Status, 0);
1551 }
1552
1553 /* Add the hive to the hive list */
1554 CmpMachineHiveList[0].CmHive = (PCMHIVE)HardwareHive;
1555
1556 /* Attach it to the machine key */
1557 RtlInitUnicodeString(&KeyName, L"\\Registry\\Machine\\HARDWARE");
1558 Status = CmpLinkHiveToMaster(&KeyName,
1559 NULL,
1560 (PCMHIVE)HardwareHive,
1561 TRUE,
1562 SecurityDescriptor);
1563 if (!NT_SUCCESS(Status))
1564 {
1565 /* Bugcheck */
1566 KeBugCheckEx(CONFIG_INITIALIZATION_FAILED, 1, 12, Status, 0);
1567 }
1568
1569 /* FIXME: Add to HiveList key */
1570
1571 /* Free the security descriptor */
1572 ExFreePoolWithTag(SecurityDescriptor, TAG_CM);
1573
1574 /* Fill out the Hardware key with the ARC Data from the Loader */
1575 Status = CmpInitializeHardwareConfiguration(KeLoaderBlock);
1576 if (!NT_SUCCESS(Status))
1577 {
1578 /* Bugcheck */
1579 KeBugCheckEx(CONFIG_INITIALIZATION_FAILED, 1, 13, Status, 0);
1580 }
1581
1582 /* Initialize machine-dependent information into the registry */
1583 Status = CmpInitializeMachineDependentConfiguration(KeLoaderBlock);
1584 if (!NT_SUCCESS(Status))
1585 {
1586 /* Bugcheck */
1587 KeBugCheckEx(CONFIG_INITIALIZATION_FAILED, 1, 14, Status, 0);
1588 }
1589
1590 /* Initialize volatile registry settings */
1591 Status = CmpSetSystemValues(KeLoaderBlock);
1592 if (!NT_SUCCESS(Status))
1593 {
1594 /* Bugcheck */
1595 KeBugCheckEx(CONFIG_INITIALIZATION_FAILED, 1, 15, Status, 0);
1596 }
1597
1598 /* Free the load options */
1599 ExFreePoolWithTag(CmpLoadOptions.Buffer, TAG_CM);
1600
1601 /* If we got here, all went well */
1602 return TRUE;
1603 }
1604
1605 VOID
1606 NTAPI
1607 INIT_FUNCTION
1608 CmpFreeDriverList(IN PHHIVE Hive,
1609 IN PLIST_ENTRY DriverList)
1610 {
1611 PLIST_ENTRY NextEntry, OldEntry;
1612 PBOOT_DRIVER_NODE DriverNode;
1613 PAGED_CODE();
1614
1615 /* Parse the current list */
1616 NextEntry = DriverList->Flink;
1617 while (NextEntry != DriverList)
1618 {
1619 /* Get the driver node */
1620 DriverNode = CONTAINING_RECORD(NextEntry, BOOT_DRIVER_NODE, ListEntry.Link);
1621
1622 /* Get the next entry now, since we're going to free it later */
1623 OldEntry = NextEntry;
1624 NextEntry = NextEntry->Flink;
1625
1626 /* Was there a name? */
1627 if (DriverNode->Name.Buffer)
1628 {
1629 /* Free it */
1630 CmpFree(DriverNode->Name.Buffer, DriverNode->Name.Length);
1631 }
1632
1633 /* Was there a registry path? */
1634 if (DriverNode->ListEntry.RegistryPath.Buffer)
1635 {
1636 /* Free it */
1637 CmpFree(DriverNode->ListEntry.RegistryPath.Buffer,
1638 DriverNode->ListEntry.RegistryPath.MaximumLength);
1639 }
1640
1641 /* Was there a file path? */
1642 if (DriverNode->ListEntry.FilePath.Buffer)
1643 {
1644 /* Free it */
1645 CmpFree(DriverNode->ListEntry.FilePath.Buffer,
1646 DriverNode->ListEntry.FilePath.MaximumLength);
1647 }
1648
1649 /* Now free the node, and move on */
1650 CmpFree(OldEntry, sizeof(BOOT_DRIVER_NODE));
1651 }
1652 }
1653
1654 PUNICODE_STRING*
1655 NTAPI
1656 INIT_FUNCTION
1657 CmGetSystemDriverList(VOID)
1658 {
1659 LIST_ENTRY DriverList;
1660 OBJECT_ATTRIBUTES ObjectAttributes;
1661 NTSTATUS Status;
1662 PCM_KEY_BODY KeyBody;
1663 PHHIVE Hive;
1664 HCELL_INDEX RootCell, ControlCell;
1665 HANDLE KeyHandle;
1666 UNICODE_STRING KeyName;
1667 PLIST_ENTRY NextEntry;
1668 ULONG i;
1669 PUNICODE_STRING* ServicePath = NULL;
1670 BOOLEAN Success, AutoSelect;
1671 PBOOT_DRIVER_LIST_ENTRY DriverEntry;
1672 PAGED_CODE();
1673
1674 /* Initialize the driver list */
1675 InitializeListHead(&DriverList);
1676
1677 /* Open the system hive key */
1678 RtlInitUnicodeString(&KeyName, L"\\Registry\\Machine\\System");
1679 InitializeObjectAttributes(&ObjectAttributes,
1680 &KeyName,
1681 OBJ_CASE_INSENSITIVE,
1682 NULL,
1683 NULL);
1684 Status = NtOpenKey(&KeyHandle, KEY_READ, &ObjectAttributes);
1685 if (!NT_SUCCESS(Status)) return NULL;
1686
1687 /* Reference the key object to get the root hive/cell to access directly */
1688 Status = ObReferenceObjectByHandle(KeyHandle,
1689 KEY_QUERY_VALUE,
1690 CmpKeyObjectType,
1691 KernelMode,
1692 (PVOID*)&KeyBody,
1693 NULL);
1694 if (!NT_SUCCESS(Status))
1695 {
1696 /* Fail */
1697 NtClose(KeyHandle);
1698 return NULL;
1699 }
1700
1701 /* Do all this under the registry lock */
1702 CmpLockRegistryExclusive();
1703
1704 /* Get the hive and key cell */
1705 Hive = KeyBody->KeyControlBlock->KeyHive;
1706 RootCell = KeyBody->KeyControlBlock->KeyCell;
1707
1708 /* Open the current control set key */
1709 RtlInitUnicodeString(&KeyName, L"Current");
1710 ControlCell = CmpFindControlSet(Hive, RootCell, &KeyName, &AutoSelect);
1711 if (ControlCell == HCELL_NIL) goto EndPath;
1712
1713 /* Find all system drivers */
1714 Success = CmpFindDrivers(Hive, ControlCell, SystemLoad, NULL, &DriverList);
1715 if (!Success) goto EndPath;
1716
1717 /* Sort by group/tag */
1718 if (!CmpSortDriverList(Hive, ControlCell, &DriverList)) goto EndPath;
1719
1720 /* Remove circular dependencies (cycles) and sort */
1721 if (!CmpResolveDriverDependencies(&DriverList)) goto EndPath;
1722
1723 /* Loop the list to count drivers */
1724 for (i = 0, NextEntry = DriverList.Flink;
1725 NextEntry != &DriverList;
1726 i++, NextEntry = NextEntry->Flink);
1727
1728 /* Allocate the array */
1729 ServicePath = ExAllocatePool(NonPagedPool, (i + 1) * sizeof(PUNICODE_STRING));
1730 if (!ServicePath) KeBugCheckEx(CONFIG_INITIALIZATION_FAILED, 2, 1, 0, 0);
1731
1732 /* Loop the driver list */
1733 for (i = 0, NextEntry = DriverList.Flink;
1734 NextEntry != &DriverList;
1735 i++, NextEntry = NextEntry->Flink)
1736 {
1737 /* Get the entry */
1738 DriverEntry = CONTAINING_RECORD(NextEntry, BOOT_DRIVER_LIST_ENTRY, Link);
1739
1740 /* Allocate the path for the caller and duplicate the registry path */
1741 ServicePath[i] = ExAllocatePool(NonPagedPool, sizeof(UNICODE_STRING));
1742 RtlDuplicateUnicodeString(RTL_DUPLICATE_UNICODE_STRING_NULL_TERMINATE,
1743 &DriverEntry->RegistryPath,
1744 ServicePath[i]);
1745 }
1746
1747 /* Terminate the list */
1748 ServicePath[i] = NULL;
1749
1750 EndPath:
1751 /* Free the driver list if we had one */
1752 if (!IsListEmpty(&DriverList)) CmpFreeDriverList(Hive, &DriverList);
1753
1754 /* Unlock the registry */
1755 CmpUnlockRegistry();
1756
1757 /* Close the key handle and dereference the object, then return the path */
1758 ObDereferenceObject(KeyBody);
1759 NtClose(KeyHandle);
1760 return ServicePath;
1761 }
1762
1763 VOID
1764 NTAPI
1765 CmpLockRegistryExclusive(VOID)
1766 {
1767 /* Enter a critical region and lock the registry */
1768 KeEnterCriticalRegion();
1769 ExAcquireResourceExclusiveLite(&CmpRegistryLock, TRUE);
1770
1771 /* Sanity check */
1772 ASSERT(CmpFlushStarveWriters == 0);
1773 RtlGetCallersAddress(&CmpRegistryLockCaller, &CmpRegistryLockCallerCaller);
1774 }
1775
1776 VOID
1777 NTAPI
1778 CmpLockRegistry(VOID)
1779 {
1780 /* Enter a critical region */
1781 KeEnterCriticalRegion();
1782
1783 /* Check if we have to starve writers */
1784 if (CmpFlushStarveWriters)
1785 {
1786 /* Starve exlusive waiters */
1787 ExAcquireSharedStarveExclusive(&CmpRegistryLock, TRUE);
1788 }
1789 else
1790 {
1791 /* Just grab the lock */
1792 ExAcquireResourceSharedLite(&CmpRegistryLock, TRUE);
1793 }
1794 }
1795
1796 BOOLEAN
1797 NTAPI
1798 CmpTestRegistryLock(VOID)
1799 {
1800 /* Test the lock */
1801 return !ExIsResourceAcquiredSharedLite(&CmpRegistryLock) ? FALSE : TRUE;
1802 }
1803
1804 BOOLEAN
1805 NTAPI
1806 CmpTestRegistryLockExclusive(VOID)
1807 {
1808 /* Test the lock */
1809 return !ExIsResourceAcquiredExclusiveLite(&CmpRegistryLock) ? FALSE : TRUE;
1810 }
1811
1812 VOID
1813 NTAPI
1814 CmpLockHiveFlusherExclusive(IN PCMHIVE Hive)
1815 {
1816 /* Lock the flusher. We should already be in a critical section */
1817 CMP_ASSERT_REGISTRY_LOCK_OR_LOADING(Hive);
1818 ASSERT((ExIsResourceAcquiredShared(Hive->FlusherLock) == 0) &&
1819 (ExIsResourceAcquiredExclusiveLite(Hive->FlusherLock) == 0));
1820 ExAcquireResourceExclusiveLite(Hive->FlusherLock, TRUE);
1821 }
1822
1823 VOID
1824 NTAPI
1825 CmpLockHiveFlusherShared(IN PCMHIVE Hive)
1826 {
1827 /* Lock the flusher. We should already be in a critical section */
1828 CMP_ASSERT_REGISTRY_LOCK_OR_LOADING(Hive);
1829 ASSERT((ExIsResourceAcquiredShared(Hive->FlusherLock) == 0) &&
1830 (ExIsResourceAcquiredExclusiveLite(Hive->FlusherLock) == 0));
1831 ExAcquireResourceSharedLite(Hive->FlusherLock, TRUE);
1832 }
1833
1834 VOID
1835 NTAPI
1836 CmpUnlockHiveFlusher(IN PCMHIVE Hive)
1837 {
1838 /* Sanity check */
1839 CMP_ASSERT_REGISTRY_LOCK_OR_LOADING(Hive);
1840 CMP_ASSERT_FLUSH_LOCK(Hive);
1841
1842 /* Release the lock */
1843 ExReleaseResourceLite(Hive->FlusherLock);
1844 }
1845
1846 BOOLEAN
1847 NTAPI
1848 CmpTestHiveFlusherLockShared(IN PCMHIVE Hive)
1849 {
1850 /* Test the lock */
1851 return !ExIsResourceAcquiredSharedLite(Hive->FlusherLock) ? FALSE : TRUE;
1852 }
1853
1854 BOOLEAN
1855 NTAPI
1856 CmpTestHiveFlusherLockExclusive(IN PCMHIVE Hive)
1857 {
1858 /* Test the lock */
1859 return !ExIsResourceAcquiredExclusiveLite(Hive->FlusherLock) ? FALSE : TRUE;
1860 }
1861
1862 VOID
1863 NTAPI
1864 CmpUnlockRegistry(VOID)
1865 {
1866 /* Sanity check */
1867 CMP_ASSERT_REGISTRY_LOCK();
1868
1869 /* Check if we should flush the registry */
1870 if (CmpFlushOnLockRelease)
1871 {
1872 /* The registry should be exclusively locked for this */
1873 CMP_ASSERT_EXCLUSIVE_REGISTRY_LOCK();
1874
1875 /* Flush the registry */
1876 CmpDoFlushAll(TRUE);
1877 CmpFlushOnLockRelease = FALSE;
1878 }
1879
1880 /* Release the lock and leave the critical region */
1881 ExReleaseResourceLite(&CmpRegistryLock);
1882 KeLeaveCriticalRegion();
1883 }
1884
1885 VOID
1886 NTAPI
1887 CmpAcquireTwoKcbLocksExclusiveByKey(IN ULONG ConvKey1,
1888 IN ULONG ConvKey2)
1889 {
1890 ULONG Index1, Index2;
1891
1892 /* Sanity check */
1893 CMP_ASSERT_REGISTRY_LOCK();
1894
1895 /* Get hash indexes */
1896 Index1 = GET_HASH_INDEX(ConvKey1);
1897 Index2 = GET_HASH_INDEX(ConvKey2);
1898
1899 /* See which one is highest */
1900 if (Index1 < Index2)
1901 {
1902 /* Grab them in the proper order */
1903 CmpAcquireKcbLockExclusiveByKey(ConvKey1);
1904 CmpAcquireKcbLockExclusiveByKey(ConvKey2);
1905 }
1906 else
1907 {
1908 /* Grab the second one first, then the first */
1909 CmpAcquireKcbLockExclusiveByKey(ConvKey2);
1910 if (Index1 != Index2) CmpAcquireKcbLockExclusiveByKey(ConvKey1);
1911 }
1912 }
1913
1914 VOID
1915 NTAPI
1916 CmpReleaseTwoKcbLockByKey(IN ULONG ConvKey1,
1917 IN ULONG ConvKey2)
1918 {
1919 ULONG Index1, Index2;
1920
1921 /* Sanity check */
1922 CMP_ASSERT_REGISTRY_LOCK();
1923
1924 /* Get hash indexes */
1925 Index1 = GET_HASH_INDEX(ConvKey1);
1926 Index2 = GET_HASH_INDEX(ConvKey2);
1927 ASSERT((GET_HASH_ENTRY(CmpCacheTable, ConvKey2).Owner == KeGetCurrentThread()) ||
1928 (CmpTestRegistryLockExclusive()));
1929
1930 /* See which one is highest */
1931 if (Index1 < Index2)
1932 {
1933 /* Grab them in the proper order */
1934 ASSERT((GET_HASH_ENTRY(CmpCacheTable, ConvKey1).Owner == KeGetCurrentThread()) ||
1935 (CmpTestRegistryLockExclusive()));
1936 CmpReleaseKcbLockByKey(ConvKey2);
1937 CmpReleaseKcbLockByKey(ConvKey1);
1938 }
1939 else
1940 {
1941 /* Release the first one first, then the second */
1942 if (Index1 != Index2)
1943 {
1944 ASSERT((GET_HASH_ENTRY(CmpCacheTable, ConvKey1).Owner == KeGetCurrentThread()) ||
1945 (CmpTestRegistryLockExclusive()));
1946 CmpReleaseKcbLockByKey(ConvKey1);
1947 }
1948 CmpReleaseKcbLockByKey(ConvKey2);
1949 }
1950 }
1951
1952 VOID
1953 NTAPI
1954 CmShutdownSystem(VOID)
1955 {
1956 /* Kill the workers */
1957 if (!CmFirstTime) CmpShutdownWorkers();
1958
1959 /* Flush all hives */
1960 CmpLockRegistryExclusive();
1961 CmpDoFlushAll(TRUE);
1962 CmpUnlockRegistry();
1963 }
1964
1965 VOID
1966 NTAPI
1967 CmpSetVersionData(VOID)
1968 {
1969 OBJECT_ATTRIBUTES ObjectAttributes;
1970 UNICODE_STRING KeyName;
1971 UNICODE_STRING ValueName;
1972 UNICODE_STRING ValueData;
1973 HANDLE SoftwareKeyHandle = NULL;
1974 HANDLE MicrosoftKeyHandle = NULL;
1975 HANDLE WindowsNtKeyHandle = NULL;
1976 HANDLE CurrentVersionKeyHandle = NULL;
1977 WCHAR Buffer[128];
1978 NTSTATUS Status;
1979
1980 /* Open the 'CurrentVersion' key */
1981 RtlInitUnicodeString(&KeyName,
1982 L"\\REGISTRY\\MACHINE\\SOFTWARE");
1983
1984 InitializeObjectAttributes(&ObjectAttributes,
1985 &KeyName,
1986 OBJ_CASE_INSENSITIVE,
1987 NULL,
1988 NULL);
1989
1990 Status = NtCreateKey(&SoftwareKeyHandle,
1991 KEY_CREATE_SUB_KEY,
1992 &ObjectAttributes,
1993 0,
1994 NULL,
1995 0,
1996 NULL);
1997 if (!NT_SUCCESS(Status))
1998 {
1999 DPRINT1("Failed to create key %wZ (Status: %08lx)\n", &KeyName, Status);
2000 return;
2001 }
2002
2003 /* Open the 'CurrentVersion' key */
2004 RtlInitUnicodeString(&KeyName,
2005 L"Microsoft");
2006
2007 InitializeObjectAttributes(&ObjectAttributes,
2008 &KeyName,
2009 OBJ_CASE_INSENSITIVE,
2010 SoftwareKeyHandle,
2011 NULL);
2012
2013 Status = NtCreateKey(&MicrosoftKeyHandle,
2014 KEY_CREATE_SUB_KEY,
2015 &ObjectAttributes,
2016 0,
2017 NULL,
2018 0,
2019 NULL);
2020 if (!NT_SUCCESS(Status))
2021 {
2022 DPRINT1("Failed to create key %wZ (Status: %08lx)\n", &KeyName, Status);
2023 goto done;
2024 }
2025
2026 /* Open the 'CurrentVersion' key */
2027 RtlInitUnicodeString(&KeyName,
2028 L"Windows NT");
2029
2030 InitializeObjectAttributes(&ObjectAttributes,
2031 &KeyName,
2032 OBJ_CASE_INSENSITIVE,
2033 MicrosoftKeyHandle,
2034 NULL);
2035
2036 Status = NtCreateKey(&WindowsNtKeyHandle,
2037 KEY_CREATE_SUB_KEY,
2038 &ObjectAttributes,
2039 0,
2040 NULL,
2041 0,
2042 NULL);
2043 if (!NT_SUCCESS(Status))
2044 {
2045 DPRINT1("Failed to create key %wZ (Status: %08lx)\n", &KeyName, Status);
2046 goto done;
2047 }
2048
2049 /* Open the 'CurrentVersion' key */
2050 RtlInitUnicodeString(&KeyName,
2051 L"CurrentVersion");
2052
2053 InitializeObjectAttributes(&ObjectAttributes,
2054 &KeyName,
2055 OBJ_CASE_INSENSITIVE,
2056 WindowsNtKeyHandle,
2057 NULL);
2058
2059 Status = NtCreateKey(&CurrentVersionKeyHandle,
2060 KEY_CREATE_SUB_KEY | KEY_SET_VALUE,
2061 &ObjectAttributes,
2062 0,
2063 NULL,
2064 0,
2065 NULL);
2066 if (!NT_SUCCESS(Status))
2067 {
2068 DPRINT1("Failed to create key %wZ (Status: %08lx)\n", &KeyName, Status);
2069 goto done;
2070 }
2071
2072 /* Set the 'CurrentType' value */
2073 RtlInitUnicodeString(&ValueName,
2074 L"CurrentType");
2075
2076 #ifdef CONFIG_SMP
2077 wcscpy(Buffer, L"Multiprocessor");
2078 #else
2079 wcscpy(Buffer, L"Uniprocessor");
2080 #endif
2081
2082 wcscat(Buffer, L" ");
2083
2084 #if (DBG == 1)
2085 wcscat(Buffer, L"Checked");
2086 #else
2087 wcscat(Buffer, L"Free");
2088 #endif
2089
2090 RtlInitUnicodeString(&ValueData,
2091 Buffer);
2092
2093 NtSetValueKey(CurrentVersionKeyHandle,
2094 &ValueName,
2095 0,
2096 REG_SZ,
2097 ValueData.Buffer,
2098 ValueData.Length + sizeof(WCHAR));
2099
2100 done:;
2101 /* Close the keys */
2102 if (CurrentVersionKeyHandle != NULL)
2103 NtClose(CurrentVersionKeyHandle);
2104
2105 if (WindowsNtKeyHandle != NULL)
2106 NtClose(WindowsNtKeyHandle);
2107
2108 if (MicrosoftKeyHandle != NULL)
2109 NtClose(MicrosoftKeyHandle);
2110
2111 if (SoftwareKeyHandle != NULL)
2112 NtClose(SoftwareKeyHandle);
2113 }
2114
2115 /* EOF */