Create the AHCI branch for Aman's work
[reactos.git] / sdk / tools / mkhive / reginf.c
1 /*
2 * ReactOS kernel
3 * Copyright (C) 2003, 2006 ReactOS Team
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19 /*
20 * COPYRIGHT: See COPYING in the top level directory
21 * PROJECT: ReactOS hive maker
22 * FILE: tools/mkhive/reginf.c
23 * PURPOSE: Inf file import code
24 * PROGRAMMER: Eric Kohl
25 * Hervé Poussineau
26 */
27
28 /* INCLUDES *****************************************************************/
29
30 #include <string.h>
31 #include <stdlib.h>
32 #include <stdio.h>
33
34 #define NDEBUG
35 #include "mkhive.h"
36
37 #define FLG_ADDREG_BINVALUETYPE 0x00000001
38 #define FLG_ADDREG_NOCLOBBER 0x00000002
39 #define FLG_ADDREG_DELVAL 0x00000004
40 #define FLG_ADDREG_APPEND 0x00000008
41 #define FLG_ADDREG_KEYONLY 0x00000010
42 #define FLG_ADDREG_OVERWRITEONLY 0x00000020
43 #define FLG_ADDREG_TYPE_SZ 0x00000000
44 #define FLG_ADDREG_TYPE_MULTI_SZ 0x00010000
45 #define FLG_ADDREG_TYPE_EXPAND_SZ 0x00020000
46 #define FLG_ADDREG_TYPE_BINARY (0x00000000 | FLG_ADDREG_BINVALUETYPE)
47 #define FLG_ADDREG_TYPE_DWORD (0x00010000 | FLG_ADDREG_BINVALUETYPE)
48 #define FLG_ADDREG_TYPE_NONE (0x00020000 | FLG_ADDREG_BINVALUETYPE)
49 #define FLG_ADDREG_TYPE_MASK (0xFFFF0000 | FLG_ADDREG_BINVALUETYPE)
50
51
52 static const WCHAR HKCR[] = {'H','K','C','R',0};
53 static const WCHAR HKCU[] = {'H','K','C','U',0};
54 static const WCHAR HKLM[] = {'H','K','L','M',0};
55 static const WCHAR HKU[] = {'H','K','U',0};
56 static const WCHAR HKR[] = {'H','K','R',0};
57 static const WCHAR BCD[] = {'B','C','D',0};
58
59 static const WCHAR HKCRPath[] = {'\\','R','e','g','i','s','t','r','y','\\','M','a','c','h','i','n','e','\\','S','O','F','T','W','A','R','E','\\','C','l','a','s','s','e','s','\\',0};
60 static const WCHAR HKCUPath[] = {'\\','R','e','g','i','s','t','r','y','\\','U','s','e','r','\\','.','D','E','F','A','U','L','T','\\',0};
61 static const WCHAR HKLMPath[] = {'\\','R','e','g','i','s','t','r','y','\\','M','a','c','h','i','n','e','\\',0};
62 static const WCHAR HKUPath[] = {'\\','R','e','g','i','s','t','r','y','\\','U','s','e','r','\\',0};
63 static const WCHAR BCDPath[] = {'\\','R','e','g','i','s','t','r','y','\\','M','a','c','h','i','n','e','\\',0};
64
65 static const WCHAR AddReg[] = {'A','d','d','R','e','g',0};
66 static const WCHAR DelReg[] = {'D','e','l','R','e','g',0};
67
68 /* FUNCTIONS ****************************************************************/
69
70 static BOOL
71 GetRootKey (PWCHAR Name)
72 {
73 if (!strcmpiW (Name, HKCR))
74 {
75 strcpyW (Name, HKCRPath);
76 return TRUE;
77 }
78
79 if (!strcmpiW (Name, HKCU))
80 {
81 strcpyW (Name, HKCUPath);
82 return TRUE;
83 }
84
85 if (!strcmpiW (Name, HKLM))
86 {
87 strcpyW (Name, HKLMPath);
88 return TRUE;
89 }
90
91 if (!strcmpiW (Name, HKU))
92 {
93 strcpyW (Name, HKUPath);
94 return TRUE;
95 }
96
97 if (!strcmpiW (Name, BCD))
98 {
99 strcpyW (Name, BCDPath);
100 return TRUE;
101 }
102
103 #if 0
104 if (!strcmpiW (Name, HKR))
105 return FALSE;
106 #endif
107
108 return FALSE;
109 }
110
111
112 /***********************************************************************
113 * AppendMultiSzValue
114 *
115 * Append a multisz string to a multisz registry value.
116 */
117 static VOID
118 AppendMultiSzValue (
119 IN HKEY KeyHandle,
120 IN PWCHAR ValueName,
121 IN PWCHAR Strings,
122 IN ULONG StringSize)
123 {
124 ULONG Size;
125 ULONG Type;
126 ULONG Total;
127 PWCHAR Buffer;
128 PWCHAR p;
129 size_t len;
130 LONG Error;
131
132 Error = RegQueryValueExW (
133 KeyHandle,
134 ValueName,
135 NULL,
136 &Type,
137 NULL,
138 &Size);
139 if ((Error != ERROR_SUCCESS) ||
140 (Type != REG_MULTI_SZ))
141 return;
142
143 Buffer = malloc ((Size + StringSize) * sizeof(WCHAR));
144 if (Buffer == NULL)
145 return;
146
147 Error = RegQueryValueExW (
148 KeyHandle,
149 ValueName,
150 NULL,
151 NULL,
152 (PUCHAR)Buffer,
153 &Size);
154 if (Error != ERROR_SUCCESS)
155 goto done;
156
157 /* compare each string against all the existing ones */
158 Total = Size;
159 while (*Strings != 0)
160 {
161 len = strlenW(Strings) + 1;
162
163 for (p = Buffer; *p != 0; p += strlenW(p) + 1)
164 if (!strcmpiW(p, Strings))
165 break;
166
167 if (*p == 0) /* not found, need to append it */
168 {
169 memcpy (p, Strings, len);
170 p[len] = 0;
171 Total += len;
172 }
173 Strings += len;
174 }
175
176 if (Total != Size)
177 {
178 DPRINT ("setting value %S to %S\n", ValueName, Buffer);
179 RegSetValueExW (
180 KeyHandle,
181 ValueName,
182 0,
183 REG_MULTI_SZ,
184 (PUCHAR)Buffer,
185 Total * sizeof(WCHAR));
186 }
187
188 done:
189 free (Buffer);
190 }
191
192
193 /***********************************************************************
194 * do_reg_operation
195 *
196 * Perform an add/delete registry operation depending on the flags.
197 */
198 static BOOL
199 do_reg_operation(
200 IN HKEY KeyHandle,
201 IN PWCHAR ValueName,
202 IN PINFCONTEXT Context,
203 IN ULONG Flags)
204 {
205 WCHAR EmptyStr = (CHAR)0;
206 ULONG Type;
207 ULONG Size;
208 LONG Error;
209
210 if (Flags & FLG_ADDREG_DELVAL) /* deletion */
211 {
212 if (ValueName)
213 {
214 RegDeleteValueW (KeyHandle, ValueName);
215 }
216 else
217 {
218 RegDeleteKeyW (KeyHandle, NULL);
219 }
220
221 return TRUE;
222 }
223
224 if (Flags & FLG_ADDREG_KEYONLY)
225 return TRUE;
226
227 if (Flags & (FLG_ADDREG_NOCLOBBER | FLG_ADDREG_OVERWRITEONLY))
228 {
229 Error = RegQueryValueExW (
230 KeyHandle,
231 ValueName,
232 NULL,
233 NULL,
234 NULL,
235 NULL);
236 if ((Error == ERROR_SUCCESS) &&
237 (Flags & FLG_ADDREG_NOCLOBBER))
238 return TRUE;
239
240 if ((Error != ERROR_SUCCESS) &&
241 (Flags & FLG_ADDREG_OVERWRITEONLY))
242 return TRUE;
243 }
244
245 switch (Flags & FLG_ADDREG_TYPE_MASK)
246 {
247 case FLG_ADDREG_TYPE_SZ:
248 Type = REG_SZ;
249 break;
250
251 case FLG_ADDREG_TYPE_MULTI_SZ:
252 Type = REG_MULTI_SZ;
253 break;
254
255 case FLG_ADDREG_TYPE_EXPAND_SZ:
256 Type = REG_EXPAND_SZ;
257 break;
258
259 case FLG_ADDREG_TYPE_BINARY:
260 Type = REG_BINARY;
261 break;
262
263 case FLG_ADDREG_TYPE_DWORD:
264 Type = REG_DWORD;
265 break;
266
267 case FLG_ADDREG_TYPE_NONE:
268 Type = REG_NONE;
269 break;
270
271 default:
272 Type = Flags >> 16;
273 break;
274 }
275
276 if (!(Flags & FLG_ADDREG_BINVALUETYPE) ||
277 (Type == REG_DWORD && InfHostGetFieldCount (Context) == 5))
278 {
279 PWCHAR Str = NULL;
280
281 if (Type == REG_MULTI_SZ)
282 {
283 if (InfHostGetMultiSzField (Context, 5, NULL, 0, &Size) != 0)
284 Size = 0;
285
286 if (Size)
287 {
288 Str = malloc (Size * sizeof(WCHAR));
289 if (Str == NULL)
290 return FALSE;
291
292 InfHostGetMultiSzField (Context, 5, Str, Size, NULL);
293 }
294
295 if (Flags & FLG_ADDREG_APPEND)
296 {
297 if (Str == NULL)
298 return TRUE;
299
300 AppendMultiSzValue (
301 KeyHandle,
302 ValueName,
303 Str,
304 Size);
305
306 free (Str);
307 return TRUE;
308 }
309 /* else fall through to normal string handling */
310 }
311 else
312 {
313 if (InfHostGetStringField (Context, 5, NULL, 0, &Size) != 0)
314 Size = 0;
315
316 if (Size)
317 {
318 Str = malloc (Size * sizeof(WCHAR));
319 if (Str == NULL)
320 return FALSE;
321
322 InfHostGetStringField (Context, 5, Str, Size, NULL);
323 }
324 }
325
326 if (Type == REG_DWORD)
327 {
328 ULONG dw = Str ? strtoulW (Str, NULL, 0) : 0;
329
330 DPRINT("setting dword %S to %x\n", ValueName, dw);
331
332 RegSetValueExW (
333 KeyHandle,
334 ValueName,
335 0,
336 Type,
337 (const PUCHAR)&dw,
338 sizeof(ULONG));
339 }
340 else
341 {
342 DPRINT("setting value %S to %S\n", ValueName, Str);
343
344 if (Str)
345 {
346 RegSetValueExW (
347 KeyHandle,
348 ValueName,
349 0,
350 Type,
351 (PVOID)Str,
352 Size * sizeof(WCHAR));
353 }
354 else
355 {
356 RegSetValueExW (
357 KeyHandle,
358 ValueName,
359 0,
360 Type,
361 (PVOID)&EmptyStr,
362 sizeof(WCHAR));
363 }
364 }
365 free (Str);
366 }
367 else /* get the binary data */
368 {
369 PUCHAR Data = NULL;
370
371 if (InfHostGetBinaryField (Context, 5, NULL, 0, &Size) != 0)
372 Size = 0;
373
374 if (Size)
375 {
376 Data = malloc (Size);
377 if (Data == NULL)
378 return FALSE;
379
380 DPRINT("setting binary data %S len %d\n", ValueName, Size);
381 InfHostGetBinaryField (Context, 5, Data, Size, NULL);
382 }
383
384 RegSetValueExW (
385 KeyHandle,
386 ValueName,
387 0,
388 Type,
389 (PVOID)Data,
390 Size);
391
392 free (Data);
393 }
394
395 return TRUE;
396 }
397
398 /***********************************************************************
399 * registry_callback
400 *
401 * Called once for each AddReg and DelReg entry in a given section.
402 */
403 static BOOL
404 registry_callback (HINF hInf, PWCHAR Section, BOOL Delete)
405 {
406 WCHAR Buffer[MAX_INF_STRING_LENGTH];
407 PWCHAR ValuePtr;
408 ULONG Flags;
409 size_t Length;
410
411 PINFCONTEXT Context = NULL;
412 HKEY KeyHandle;
413 BOOL Ok;
414
415
416 Ok = InfHostFindFirstLine (hInf, Section, NULL, &Context) == 0;
417 if (!Ok)
418 return TRUE; /* Don't fail if the section isn't present */
419
420 for (;Ok; Ok = (InfHostFindNextLine (Context, Context) == 0))
421 {
422 /* get root */
423 if (InfHostGetStringField (Context, 1, Buffer, MAX_INF_STRING_LENGTH, NULL) != 0)
424 continue;
425 if (!GetRootKey (Buffer))
426 continue;
427
428 /* get key */
429 Length = strlenW (Buffer);
430 if (InfHostGetStringField (Context, 2, Buffer + Length, MAX_INF_STRING_LENGTH - (ULONG)Length, NULL) != 0)
431 *Buffer = 0;
432
433 DPRINT("KeyName: <%S>\n", Buffer);
434
435 if (Delete)
436 {
437 Flags = FLG_ADDREG_DELVAL;
438 }
439 else
440 {
441 /* get flags */
442 if (InfHostGetIntField (Context, 4, (INT *)&Flags) != 0)
443 Flags = 0;
444 }
445
446 DPRINT("Flags: 0x%x\n", Flags);
447
448 if (Delete || (Flags & FLG_ADDREG_OVERWRITEONLY))
449 {
450 if (RegOpenKeyW (NULL, Buffer, &KeyHandle) != ERROR_SUCCESS)
451 {
452 DPRINT("RegOpenKey(%S) failed\n", Buffer);
453 continue; /* ignore if it doesn't exist */
454 }
455 }
456 else
457 {
458 if (RegCreateKeyW (NULL, Buffer, &KeyHandle) != ERROR_SUCCESS)
459 {
460 DPRINT("RegCreateKey(%S) failed\n", Buffer);
461 continue;
462 }
463 }
464
465 /* get value name */
466 if (InfHostGetStringField (Context, 3, Buffer, MAX_INF_STRING_LENGTH, NULL) == 0)
467 {
468 ValuePtr = Buffer;
469 }
470 else
471 {
472 ValuePtr = NULL;
473 }
474
475 /* and now do it */
476 if (!do_reg_operation (KeyHandle, ValuePtr, Context, Flags))
477 {
478 return FALSE;
479 }
480 }
481
482 InfHostFreeContext(Context);
483
484 return TRUE;
485 }
486
487
488 BOOL
489 ImportRegistryFile(PCHAR FileName)
490 {
491 HINF hInf;
492 ULONG ErrorLine;
493
494 /* Load inf file from install media. */
495 if (InfHostOpenFile(&hInf, FileName, 0, &ErrorLine) != 0)
496 {
497 DPRINT1 ("InfHostOpenFile(%s) failed\n", FileName);
498 return FALSE;
499 }
500
501 if (!registry_callback (hInf, (PWCHAR)DelReg, TRUE))
502 {
503 DPRINT1 ("registry_callback() for DelReg failed\n");
504 }
505
506 if (!registry_callback (hInf, (PWCHAR)AddReg, FALSE))
507 {
508 DPRINT1 ("registry_callback() for AddReg failed\n");
509 }
510
511 InfHostCloseFile (hInf);
512
513 return TRUE;
514 }
515
516 /* EOF */