3 * PROJECT: ReactOS Kernel
4 * COPYRIGHT: GPL - See COPYING in the top level directory
5 * FILE: ntoskrnl/cm/import.c
6 * PURPOSE: Registry-Hive import functions
8 * PROGRAMMERS: Eric Kohl
11 /* INCLUDES *****************************************************************/
15 #include <internal/debug.h>
19 #if defined (ALLOC_PRAGMA)
20 #pragma alloc_text(INIT, CmImportHardwareHive)
23 /* GLOBALS ******************************************************************/
25 static BOOLEAN CmiHardwareHiveImported
= FALSE
;
27 /* FUNCTIONS ****************************************************************/
30 CmImportBinaryHive (PCHAR ChunkBase
,
33 PEREGISTRY_HIVE
*RegistryHive
)
40 /* Create a new hive */
41 Hive
= ExAllocatePool (NonPagedPool
,
42 sizeof(EREGISTRY_HIVE
));
48 sizeof(EREGISTRY_HIVE
));
53 /* Allocate hive header */
54 ((PHBASE_BLOCK
)ChunkBase
)->Length
= ChunkSize
;
55 Status
= HvInitialize(&Hive
->Hive
, HV_OPERATION_MEMORY
, 0, 0,
56 (ULONG_PTR
)ChunkBase
, 0,
58 CmpFileRead
, CmpFileWrite
, CmpFileSetSize
,
60 if (!NT_SUCCESS(Status
))
62 DPRINT1 ("Opening hive failed (%x)\n", Status
);
67 CmPrepareHive(&Hive
->Hive
);
69 /* Acquire hive list lock exclusively */
70 KeEnterCriticalRegion();
71 ExAcquireResourceExclusiveLite(&CmiRegistryLock
, TRUE
);
73 DPRINT("Adding new hive\n");
75 /* Add the new hive to the hive list */
76 InsertTailList(&CmiHiveListHead
, &Hive
->HiveList
);
78 /* Release hive list lock */
79 ExReleaseResourceLite(&CmiRegistryLock
);
80 KeLeaveCriticalRegion();
89 CmImportSystemHive(PCHAR ChunkBase
,
92 OBJECT_ATTRIBUTES ObjectAttributes
;
93 PEREGISTRY_HIVE RegistryHive
;
94 UNICODE_STRING KeyName
;
97 DPRINT ("CmImportSystemHive() called\n");
99 if (strncmp (ChunkBase
, "regf", 4))
101 DPRINT1 ("Found invalid '%.*s' magic\n", 4, ChunkBase
);
105 DPRINT ("Found '%.*s' magic\n", 4, ChunkBase
);
107 /* Import the binary system hive (non-volatile, offset-based, permanent) */
108 if (!CmImportBinaryHive (ChunkBase
, ChunkSize
, 0, &RegistryHive
))
110 DPRINT1 ("CmiImportBinaryHive() failed\n");
114 /* Attach it to the machine key */
115 RtlInitUnicodeString (&KeyName
,
116 REG_SYSTEM_KEY_NAME
);
117 InitializeObjectAttributes (&ObjectAttributes
,
119 OBJ_CASE_INSENSITIVE
,
122 Status
= CmiConnectHive (&ObjectAttributes
,
124 if (!NT_SUCCESS(Status
))
126 DPRINT1 ("CmiConnectHive(%wZ) failed (Status %lx)\n", &KeyName
, Status
);
130 /* Set the hive filename */
131 RtlCreateUnicodeString (&RegistryHive
->HiveFileName
,
134 /* Set the log filename */
135 RtlCreateUnicodeString (&RegistryHive
->LogFileName
,
142 BOOLEAN INIT_FUNCTION
143 CmImportHardwareHive(PCHAR ChunkBase
,
146 OBJECT_ATTRIBUTES ObjectAttributes
;
147 PEREGISTRY_HIVE RegistryHive
;
148 UNICODE_STRING KeyName
;
153 DPRINT ("CmImportHardwareHive() called\n");
155 if (CmiHardwareHiveImported
== TRUE
)
158 if (ChunkBase
== NULL
&&
161 /* Create '\Registry\Machine\HARDWARE' key. */
162 RtlInitUnicodeString (&KeyName
,
163 REG_HARDWARE_KEY_NAME
);
164 InitializeObjectAttributes (&ObjectAttributes
,
166 OBJ_CASE_INSENSITIVE
,
169 Status
= ZwCreateKey (&HardwareKey
,
176 if (!NT_SUCCESS(Status
))
178 DPRINT1("NtCreateKey() failed, status: 0x%x\n", Status
);
181 ZwClose (HardwareKey
);
183 /* Create '\Registry\Machine\HARDWARE\DESCRIPTION' key. */
184 RtlInitUnicodeString(&KeyName
,
185 REG_DESCRIPTION_KEY_NAME
);
186 InitializeObjectAttributes (&ObjectAttributes
,
188 OBJ_CASE_INSENSITIVE
,
191 Status
= ZwCreateKey (&HardwareKey
,
198 if (!NT_SUCCESS(Status
))
200 DPRINT1("NtCreateKey() failed, status: 0x%x\n", Status
);
203 ZwClose (HardwareKey
);
205 /* Create '\Registry\Machine\HARDWARE\DEVICEMAP' key. */
206 RtlInitUnicodeString (&KeyName
,
207 REG_DEVICEMAP_KEY_NAME
);
208 InitializeObjectAttributes (&ObjectAttributes
,
210 OBJ_CASE_INSENSITIVE
,
213 Status
= ZwCreateKey (&HardwareKey
,
220 if (!NT_SUCCESS(Status
))
222 DPRINT1("NtCreateKey() failed, status: 0x%x\n", Status
);
225 ZwClose (HardwareKey
);
227 /* Create '\Registry\Machine\HARDWARE\RESOURCEMAP' key. */
228 RtlInitUnicodeString(&KeyName
,
229 REG_RESOURCEMAP_KEY_NAME
);
230 InitializeObjectAttributes (&ObjectAttributes
,
232 OBJ_CASE_INSENSITIVE
,
235 Status
= ZwCreateKey (&HardwareKey
,
242 if (!NT_SUCCESS(Status
))
244 DPRINT1("NtCreateKey() failed, status: 0x%x\n", Status
);
247 ZwClose (HardwareKey
);
252 /* Check the hive magic */
253 if (strncmp (ChunkBase
, "regf", 4))
255 DPRINT1 ("Found invalid '%.*s' magic\n", 4, ChunkBase
);
259 DPRINT ("Found '%.*s' magic\n", 4, ChunkBase
);
260 DPRINT ("ChunkBase %lx ChunkSize %lu\n", ChunkBase
, ChunkSize
);
262 /* Import the binary system hive (volatile, offset-based, permanent) */
263 if (!CmImportBinaryHive (ChunkBase
, ChunkSize
, HIVE_NO_FILE
, &RegistryHive
))
265 DPRINT1 ("CmiImportBinaryHive() failed\n");
269 /* Attach it to the machine key */
270 RtlInitUnicodeString (&KeyName
,
271 REG_HARDWARE_KEY_NAME
);
272 InitializeObjectAttributes (&ObjectAttributes
,
274 OBJ_CASE_INSENSITIVE
,
277 Status
= CmiConnectHive (&ObjectAttributes
,
279 if (!NT_SUCCESS(Status
))
281 DPRINT1 ("CmiConnectHive(%wZ) failed (Status %lx)\n", &KeyName
, Status
);
282 // CmiRemoveRegistryHive(RegistryHive);
286 /* Set the hive filename */
287 RtlInitUnicodeString (&RegistryHive
->HiveFileName
,
290 /* Set the log filename */
291 RtlInitUnicodeString (&RegistryHive
->LogFileName
,
294 CmiHardwareHiveImported
= TRUE
;