- Add PROFILE_ACPI_DOCKING_STATE.
[reactos.git] / reactos / lib / cmlib / hiveinit.c
1 /*
2 * PROJECT: registry manipulation library
3 * LICENSE: GPL - See COPYING in the top level directory
4 * COPYRIGHT: Copyright 2005 Filip Navara <navaraf@reactos.org>
5 * Copyright 2001 - 2005 Eric Kohl
6 */
7
8 #include "cmlib.h"
9 #define NDEBUG
10 #include <debug.h>
11
12 /**
13 * @name HvpVerifyHiveHeader
14 *
15 * Internal function to verify that a hive header has valid format.
16 */
17
18 BOOLEAN CMAPI
19 HvpVerifyHiveHeader(
20 PHBASE_BLOCK HiveHeader)
21 {
22 if (HiveHeader->Signature != HV_SIGNATURE ||
23 HiveHeader->Major != HV_MAJOR_VER ||
24 HiveHeader->Minor < HV_MINOR_VER ||
25 HiveHeader->Type != HV_TYPE_PRIMARY ||
26 HiveHeader->Format != HV_FORMAT_MEMORY ||
27 HiveHeader->Cluster != 1 ||
28 HiveHeader->Sequence1 != HiveHeader->Sequence2 ||
29 HvpHiveHeaderChecksum(HiveHeader) != HiveHeader->Checksum)
30 {
31 DPRINT1("Verify Hive Header failed: \n");
32 DPRINT1(" Signature: 0x%x and not 0x%x, Major: 0x%x and not 0x%x\n",
33 HiveHeader->Signature, HV_SIGNATURE, HiveHeader->Major, HV_MAJOR_VER);
34 DPRINT1(" Minor: 0x%x is not >= 0x%x, Type: 0x%x and not 0x%x\n",
35 HiveHeader->Minor, HV_MINOR_VER, HiveHeader->Type, HV_TYPE_PRIMARY);
36 DPRINT1(" Format: 0x%x and not 0x%x, Cluster: 0x%x and not 1\n",
37 HiveHeader->Format, HV_FORMAT_MEMORY, HiveHeader->Cluster);
38 DPRINT1(" Sequence: 0x%x and not 0x%x, Checksum: 0x%x and not 0x%x\n",
39 HiveHeader->Sequence1, HiveHeader->Sequence2,
40 HvpHiveHeaderChecksum(HiveHeader), HiveHeader->Checksum);
41 return FALSE;
42 }
43
44 return TRUE;
45 }
46
47 /**
48 * @name HvpFreeHiveBins
49 *
50 * Internal function to free all bin storage associated with hive
51 * descriptor.
52 */
53
54 VOID CMAPI
55 HvpFreeHiveBins(
56 PHHIVE Hive)
57 {
58 ULONG i;
59 PHBIN Bin;
60 ULONG Storage;
61
62 for (Storage = HvStable; Storage < HvMaxStorageType; Storage++)
63 {
64 Bin = NULL;
65 for (i = 0; i < Hive->Storage[Storage].Length; i++)
66 {
67 if (Hive->Storage[Storage].BlockList[i].Bin == (ULONG_PTR)NULL)
68 continue;
69 if (Hive->Storage[Storage].BlockList[i].Bin != (ULONG_PTR)Bin)
70 {
71 Bin = (PHBIN)Hive->Storage[Storage].BlockList[i].Bin;
72 Hive->Free((PHBIN)Hive->Storage[Storage].BlockList[i].Bin);
73 }
74 Hive->Storage[Storage].BlockList[i].Bin = (ULONG_PTR)NULL;
75 Hive->Storage[Storage].BlockList[i].Block = (ULONG_PTR)NULL;
76 }
77
78 if (Hive->Storage[Storage].Length)
79 Hive->Free(Hive->Storage[Storage].BlockList);
80 }
81 }
82
83 /**
84 * @name HvpCreateHive
85 *
86 * Internal helper function to initalize hive descriptor structure for
87 * newly created hive.
88 *
89 * @see HvInitialize
90 */
91
92 NTSTATUS CMAPI
93 HvpCreateHive(
94 PHHIVE RegistryHive)
95 {
96 PHBASE_BLOCK HiveHeader;
97 ULONG Index;
98
99 HiveHeader = RegistryHive->Allocate(sizeof(HBASE_BLOCK), FALSE);
100 if (HiveHeader == NULL)
101 return STATUS_NO_MEMORY;
102 RtlZeroMemory(HiveHeader, sizeof(HBASE_BLOCK));
103 HiveHeader->Signature = HV_SIGNATURE;
104 HiveHeader->Major = HV_MAJOR_VER;
105 HiveHeader->Minor = HV_MINOR_VER;
106 HiveHeader->Type = HV_TYPE_PRIMARY;
107 HiveHeader->Format = HV_FORMAT_MEMORY;
108 HiveHeader->Cluster = 1;
109 HiveHeader->RootCell = HCELL_NULL;
110 HiveHeader->Length = HV_BLOCK_SIZE;
111 HiveHeader->Sequence1 = 1;
112 HiveHeader->Sequence2 = 1;
113 /* FIXME: Fill in the file name */
114 HiveHeader->Checksum = HvpHiveHeaderChecksum(HiveHeader);
115
116 RegistryHive->HiveHeader = HiveHeader;
117 for (Index = 0; Index < 24; Index++)
118 {
119 RegistryHive->Storage[HvStable].FreeDisplay[Index] = HCELL_NULL;
120 RegistryHive->Storage[HvVolatile].FreeDisplay[Index] = HCELL_NULL;
121 }
122 RtlInitializeBitMap(&RegistryHive->DirtyVector, NULL, 0);
123
124 return STATUS_SUCCESS;
125 }
126
127 /**
128 * @name HvpInitializeMemoryHive
129 *
130 * Internal helper function to initalize hive descriptor structure for
131 * a hive stored in memory. The data of the hive are copied and it is
132 * prepared for read/write access.
133 *
134 * @see HvInitialize
135 */
136
137 NTSTATUS CMAPI
138 HvpInitializeMemoryHive(
139 PHHIVE Hive,
140 ULONG_PTR ChunkBase)
141 {
142 SIZE_T BlockIndex;
143 PHBIN Bin, NewBin;
144 ULONG i;
145 ULONG BitmapSize;
146 PULONG BitmapBuffer;
147 SIZE_T ChunkSize;
148
149 //
150 // This hack is similar in magnitude to the US's National Debt
151 //
152 ChunkSize = ((PHBASE_BLOCK)ChunkBase)->Length;
153 ((PHBASE_BLOCK)ChunkBase)->Length = HV_BLOCK_SIZE;
154 DPRINT("ChunkSize: %lx\n", ChunkSize);
155
156 if (ChunkSize < sizeof(HBASE_BLOCK) ||
157 !HvpVerifyHiveHeader((PHBASE_BLOCK)ChunkBase))
158 {
159 DPRINT1("Registry is corrupt: ChunkSize %d < sizeof(HBASE_BLOCK) %d, "
160 "or HvpVerifyHiveHeader() failed\n", ChunkSize, sizeof(HBASE_BLOCK));
161 return STATUS_REGISTRY_CORRUPT;
162 }
163
164 Hive->HiveHeader = Hive->Allocate(sizeof(HBASE_BLOCK), FALSE);
165 if (Hive->HiveHeader == NULL)
166 {
167 return STATUS_NO_MEMORY;
168 }
169 RtlCopyMemory(Hive->HiveHeader, (PVOID)ChunkBase, sizeof(HBASE_BLOCK));
170
171 /*
172 * Build a block list from the in-memory chunk and copy the data as
173 * we go.
174 */
175
176 Hive->Storage[HvStable].Length = (ULONG)(ChunkSize / HV_BLOCK_SIZE) - 1;
177 Hive->Storage[HvStable].BlockList =
178 Hive->Allocate(Hive->Storage[HvStable].Length *
179 sizeof(HMAP_ENTRY), FALSE);
180 if (Hive->Storage[HvStable].BlockList == NULL)
181 {
182 DPRINT1("Allocating block list failed\n");
183 Hive->Free(Hive->HiveHeader);
184 return STATUS_NO_MEMORY;
185 }
186
187 for (BlockIndex = 0; BlockIndex < Hive->Storage[HvStable].Length; )
188 {
189 Bin = (PHBIN)((ULONG_PTR)ChunkBase + (BlockIndex + 1) * HV_BLOCK_SIZE);
190 if (Bin->Signature != HV_BIN_SIGNATURE ||
191 (Bin->Size % HV_BLOCK_SIZE) != 0)
192 {
193 Hive->Free(Hive->HiveHeader);
194 Hive->Free(Hive->Storage[HvStable].BlockList);
195 return STATUS_REGISTRY_CORRUPT;
196 }
197
198 NewBin = Hive->Allocate(Bin->Size, TRUE);
199 if (NewBin == NULL)
200 {
201 Hive->Free(Hive->HiveHeader);
202 Hive->Free(Hive->Storage[HvStable].BlockList);
203 return STATUS_NO_MEMORY;
204 }
205
206 Hive->Storage[HvStable].BlockList[BlockIndex].Bin = (ULONG_PTR)NewBin;
207 Hive->Storage[HvStable].BlockList[BlockIndex].Block = (ULONG_PTR)NewBin;
208
209 RtlCopyMemory(NewBin, Bin, Bin->Size);
210
211 if (Bin->Size > HV_BLOCK_SIZE)
212 {
213 for (i = 1; i < Bin->Size / HV_BLOCK_SIZE; i++)
214 {
215 Hive->Storage[HvStable].BlockList[BlockIndex + i].Bin = (ULONG_PTR)NewBin;
216 Hive->Storage[HvStable].BlockList[BlockIndex + i].Block =
217 ((ULONG_PTR)NewBin + (i * HV_BLOCK_SIZE));
218 }
219 }
220
221 BlockIndex += Bin->Size / HV_BLOCK_SIZE;
222 }
223
224 if (HvpCreateHiveFreeCellList(Hive))
225 {
226 HvpFreeHiveBins(Hive);
227 Hive->Free(Hive->HiveHeader);
228 return STATUS_NO_MEMORY;
229 }
230
231 BitmapSize = ROUND_UP(Hive->Storage[HvStable].Length,
232 sizeof(ULONG) * 8) / 8;
233 BitmapBuffer = (PULONG)Hive->Allocate(BitmapSize, TRUE);
234 if (BitmapBuffer == NULL)
235 {
236 HvpFreeHiveBins(Hive);
237 Hive->Free(Hive->HiveHeader);
238 return STATUS_NO_MEMORY;
239 }
240
241 RtlInitializeBitMap(&Hive->DirtyVector, BitmapBuffer, BitmapSize * 8);
242 RtlClearAllBits(&Hive->DirtyVector);
243
244 return STATUS_SUCCESS;
245 }
246
247 /**
248 * @name HvpInitializeMemoryInplaceHive
249 *
250 * Internal helper function to initalize hive descriptor structure for
251 * a hive stored in memory. The in-memory data of the hive are directly
252 * used and it is read-only accessible.
253 *
254 * @see HvInitialize
255 */
256
257 NTSTATUS CMAPI
258 HvpInitializeMemoryInplaceHive(
259 PHHIVE Hive,
260 ULONG_PTR ChunkBase)
261 {
262 if (!HvpVerifyHiveHeader((PHBASE_BLOCK)ChunkBase))
263 {
264 return STATUS_REGISTRY_CORRUPT;
265 }
266
267 Hive->HiveHeader = (PHBASE_BLOCK)ChunkBase;
268 Hive->ReadOnly = TRUE;
269 Hive->Flat = TRUE;
270
271 return STATUS_SUCCESS;
272 }
273
274 /**
275 * @name HvInitialize
276 *
277 * Allocate a new hive descriptor structure and intialize it.
278 *
279 * @param RegistryHive
280 * Output variable to store pointer to the hive descriptor.
281 * @param Operation
282 * - HV_OPERATION_CREATE_HIVE
283 * Create a new hive for read/write access.
284 * - HV_OPERATION_MEMORY
285 * Load and copy in-memory hive for read/write access. The
286 * pointer to data passed to this routine can be freed after
287 * the function is executed.
288 * - HV_OPERATION_MEMORY_INPLACE
289 * Load an in-memory hive for read-only access. The pointer
290 * to data passed to this routine MUSTN'T be freed until
291 * HvFree is called.
292 * @param ChunkBase
293 * Pointer to hive data.
294 * @param ChunkSize
295 * Size of passed hive data.
296 *
297 * @return
298 * STATUS_NO_MEMORY - A memory allocation failed.
299 * STATUS_REGISTRY_CORRUPT - Registry corruption was detected.
300 * STATUS_SUCCESS
301 *
302 * @see HvFree
303 */
304
305 NTSTATUS CMAPI
306 HvInitialize(
307 PHHIVE RegistryHive,
308 ULONG Operation,
309 ULONG HiveType,
310 ULONG HiveFlags,
311 ULONG_PTR HiveData OPTIONAL,
312 ULONG Cluster OPTIONAL,
313 PALLOCATE_ROUTINE Allocate,
314 PFREE_ROUTINE Free,
315 PFILE_READ_ROUTINE FileRead,
316 PFILE_WRITE_ROUTINE FileWrite,
317 PFILE_SET_SIZE_ROUTINE FileSetSize,
318 PFILE_FLUSH_ROUTINE FileFlush,
319 IN PUNICODE_STRING FileName)
320 {
321 NTSTATUS Status;
322 PHHIVE Hive = RegistryHive;
323
324 /*
325 * Create a new hive structure that will hold all the maintenance data.
326 */
327
328 RtlZeroMemory(Hive, sizeof(HHIVE));
329
330 Hive->Allocate = Allocate;
331 Hive->Free = Free;
332 Hive->FileRead = FileRead;
333 Hive->FileWrite = FileWrite;
334 Hive->FileSetSize = FileSetSize;
335 Hive->FileFlush = FileFlush;
336
337 switch (Operation)
338 {
339 case HV_OPERATION_CREATE_HIVE:
340 Status = HvpCreateHive(Hive);
341 break;
342
343 case HV_OPERATION_MEMORY:
344 Status = HvpInitializeMemoryHive(Hive, HiveData);
345 break;
346
347 case HV_OPERATION_MEMORY_INPLACE:
348 Status = HvpInitializeMemoryInplaceHive(Hive, HiveData);
349 break;
350
351 default:
352 /* FIXME: A better return status value is needed */
353 Status = STATUS_NOT_IMPLEMENTED;
354 ASSERT(FALSE);
355 }
356
357 if (!NT_SUCCESS(Status))
358 {
359 Hive->Free(Hive);
360 return Status;
361 }
362
363 return Status;
364 }
365
366 /**
367 * @name HvFree
368 *
369 * Free all stroage and handles associated with hive descriptor.
370 */
371
372 VOID CMAPI
373 HvFree(
374 PHHIVE RegistryHive)
375 {
376 if (!RegistryHive->ReadOnly)
377 {
378 /* Release hive bitmap */
379 if (RegistryHive->DirtyVector.Buffer)
380 {
381 RegistryHive->Free(RegistryHive->DirtyVector.Buffer);
382 }
383
384 HvpFreeHiveBins(RegistryHive);
385 }
386
387 RegistryHive->Free(RegistryHive);
388 }
389
390 /* EOF */