3 * Copyright (C) 2003 ReactOS Team
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.
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.
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.
20 * COPYRIGHT: See COPYING in the top level directory
21 * PROJECT: ReactOS text-mode setup
22 * FILE: subsys/system/usetup/registry.c
23 * PURPOSE: Registry creation functions
24 * PROGRAMMER: Eric Kohl
27 /* INCLUDES *****************************************************************/
35 #define FLG_ADDREG_BINVALUETYPE 0x00000001
36 #define FLG_ADDREG_NOCLOBBER 0x00000002
37 #define FLG_ADDREG_DELVAL 0x00000004
38 #define FLG_ADDREG_APPEND 0x00000008
39 #define FLG_ADDREG_KEYONLY 0x00000010
40 #define FLG_ADDREG_OVERWRITEONLY 0x00000020
41 #define FLG_ADDREG_TYPE_SZ 0x00000000
42 #define FLG_ADDREG_TYPE_MULTI_SZ 0x00010000
43 #define FLG_ADDREG_TYPE_EXPAND_SZ 0x00020000
44 #define FLG_ADDREG_TYPE_BINARY (0x00000000 | FLG_ADDREG_BINVALUETYPE)
45 #define FLG_ADDREG_TYPE_DWORD (0x00010000 | FLG_ADDREG_BINVALUETYPE)
46 #define FLG_ADDREG_TYPE_NONE (0x00020000 | FLG_ADDREG_BINVALUETYPE)
47 #define FLG_ADDREG_TYPE_MASK (0xFFFF0000 | FLG_ADDREG_BINVALUETYPE)
51 #define Architecture L"x86"
52 #elif defined(_M_AMD64)
53 #define Architecture L"amd64"
54 #elif defined(_M_IA64)
55 #define Architecture L"ia64"
57 #define Architecture L"arm"
59 #define Architecture L"ppc"
64 typedef struct _REG_DISK_MOUNT_INFO
67 LARGE_INTEGER StartingOffset
;
68 } REG_DISK_MOUNT_INFO
, *PREG_DISK_MOUNT_INFO
;
72 /* FUNCTIONS ****************************************************************/
75 GetRootKey (PWCHAR Name
)
77 if (!_wcsicmp (Name
, L
"HKCR"))
79 wcscpy (Name
, L
"\\Registry\\Machine\\SOFTWARE\\Classes\\");
83 if (!_wcsicmp (Name
, L
"HKCU"))
85 wcscpy (Name
, L
"\\Registry\\User\\.DEFAULT\\");
89 if (!_wcsicmp (Name
, L
"HKLM"))
91 wcscpy (Name
, L
"\\Registry\\Machine\\");
95 if (!_wcsicmp (Name
, L
"HKU"))
97 wcscpy (Name
, L
"\\Registry\\User\\");
102 if (!_wcsicmp (Name
, L
"HKR"))
110 /***********************************************************************
111 * append_multi_sz_value
113 * Append a multisz string to a multisz registry value.
117 append_multi_sz_value (HANDLE hkey
,
119 const WCHAR
*strings
,
122 DWORD size
, type
, total
;
125 if (RegQueryValueExW( hkey
, value
, NULL
, &type
, NULL
, &size
)) return;
126 if (type
!= REG_MULTI_SZ
) return;
128 if (!(buffer
= HeapAlloc( GetProcessHeap(), 0, (size
+ str_size
) * sizeof(WCHAR
) ))) return;
129 if (RegQueryValueExW( hkey
, value
, NULL
, NULL
, (BYTE
*)buffer
, &size
)) goto done
;
131 /* compare each string against all the existing ones */
135 int len
= strlenW(strings
) + 1;
137 for (p
= buffer
; *p
; p
+= strlenW(p
) + 1)
138 if (!strcmpiW( p
, strings
)) break;
140 if (!*p
) /* not found, need to append it */
142 memcpy( p
, strings
, len
* sizeof(WCHAR
) );
150 TRACE( "setting value %s to %s\n", debugstr_w(value
), debugstr_w(buffer
) );
151 RegSetValueExW( hkey
, value
, 0, REG_MULTI_SZ
, (BYTE
*)buffer
, total
);
154 HeapFree( GetProcessHeap(), 0, buffer
);
158 /***********************************************************************
159 * delete_multi_sz_value
161 * Remove a string from a multisz registry value.
164 static void delete_multi_sz_value( HKEY hkey
, const WCHAR
*value
, const WCHAR
*string
)
167 WCHAR
*buffer
, *src
, *dst
;
169 if (RegQueryValueExW( hkey
, value
, NULL
, &type
, NULL
, &size
)) return;
170 if (type
!= REG_MULTI_SZ
) return;
171 /* allocate double the size, one for value before and one for after */
172 if (!(buffer
= HeapAlloc( GetProcessHeap(), 0, size
* 2 * sizeof(WCHAR
) ))) return;
173 if (RegQueryValueExW( hkey
, value
, NULL
, NULL
, (BYTE
*)buffer
, &size
)) goto done
;
178 int len
= strlenW(src
) + 1;
179 if (strcmpiW( src
, string
))
181 memcpy( dst
, src
, len
* sizeof(WCHAR
) );
187 if (dst
!= buffer
+ 2*size
) /* did we remove something? */
189 TRACE( "setting value %s to %s\n", debugstr_w(value
), debugstr_w(buffer
+ size
) );
190 RegSetValueExW( hkey
, value
, 0, REG_MULTI_SZ
,
191 (BYTE
*)(buffer
+ size
), dst
- (buffer
+ size
) );
194 HeapFree( GetProcessHeap(), 0, buffer
);
198 /***********************************************************************
201 * Perform an add/delete registry operation depending on the flags.
204 do_reg_operation(HANDLE KeyHandle
,
205 PUNICODE_STRING ValueName
,
209 WCHAR EmptyStr
= (WCHAR
)0;
213 if (Flags
& FLG_ADDREG_DELVAL
) /* deletion */
218 RegDeleteValueW( KeyHandle
, ValueName
);
222 RegDeleteKeyW( KeyHandle
, NULL
);
228 if (Flags
& FLG_ADDREG_KEYONLY
)
232 if (Flags
& (FLG_ADDREG_NOCLOBBER
| FLG_ADDREG_OVERWRITEONLY
))
234 BOOL exists
= !RegQueryValueExW( hkey
, value
, NULL
, NULL
, NULL
, NULL
);
235 if (exists
&& (flags
& FLG_ADDREG_NOCLOBBER
))
237 if (!exists
& (flags
& FLG_ADDREG_OVERWRITEONLY
))
242 switch (Flags
& FLG_ADDREG_TYPE_MASK
)
244 case FLG_ADDREG_TYPE_SZ
:
248 case FLG_ADDREG_TYPE_MULTI_SZ
:
252 case FLG_ADDREG_TYPE_EXPAND_SZ
:
253 Type
= REG_EXPAND_SZ
;
256 case FLG_ADDREG_TYPE_BINARY
:
260 case FLG_ADDREG_TYPE_DWORD
:
264 case FLG_ADDREG_TYPE_NONE
:
273 if (!(Flags
& FLG_ADDREG_BINVALUETYPE
) ||
274 (Type
== REG_DWORD
&& SetupGetFieldCount (Context
) == 5))
278 if (Type
== REG_MULTI_SZ
)
280 if (!SetupGetMultiSzFieldW (Context
, 5, NULL
, 0, &Size
))
285 Str
= (WCHAR
*) RtlAllocateHeap (ProcessHeap
, 0, Size
* sizeof(WCHAR
));
289 SetupGetMultiSzFieldW (Context
, 5, Str
, Size
, NULL
);
292 if (Flags
& FLG_ADDREG_APPEND
)
297 // append_multi_sz_value( hkey, value, str, size );
299 RtlFreeHeap (ProcessHeap
, 0, Str
);
302 /* else fall through to normal string handling */
306 if (!SetupGetStringFieldW (Context
, 5, NULL
, 0, &Size
))
311 Str
= (WCHAR
*) RtlAllocateHeap (ProcessHeap
, 0, Size
* sizeof(WCHAR
));
315 SetupGetStringFieldW (Context
, 5, Str
, Size
, NULL
);
319 if (Type
== REG_DWORD
)
321 ULONG dw
= Str
? wcstoul (Str
, NULL
, 0) : 0;
323 DPRINT("setting dword %wZ to %lx\n", ValueName
, dw
);
326 NtSetValueKey (KeyHandle
,
333 RegSetValueExW(KeyHandle
, ValueName
, 0, Type
, (const UCHAR
*)&dw
, sizeof(ULONG
));
338 DPRINT("setting value %wZ to %S\n", ValueName
, Str
);
343 NtSetValueKey (KeyHandle
,
348 Size
* sizeof(WCHAR
));
350 RegSetValueExW(KeyHandle
, ValueName
, 0, Type
, (const UCHAR
*)Str
, Size
* sizeof(WCHAR
));
356 NtSetValueKey (KeyHandle
,
363 RegSetValueExW(KeyHandle
, ValueName
, 0, Type
, (const UCHAR
*)&EmptyStr
, sizeof(WCHAR
));
367 RtlFreeHeap (ProcessHeap
, 0, Str
);
369 else /* get the binary data */
373 if (!SetupGetBinaryField (Context
, 5, NULL
, 0, &Size
))
378 Data
= (unsigned char*) RtlAllocateHeap (ProcessHeap
, 0, Size
);
382 DPRINT("setting binary data %wZ len %lu\n", ValueName
, Size
);
383 SetupGetBinaryField (Context
, 5, Data
, Size
, NULL
);
387 NtSetValueKey (KeyHandle
,
394 RegSetValueExW(KeyHandle
, ValueName
, 0, Type
, (const UCHAR
*)Data
, Size
);
397 RtlFreeHeap (ProcessHeap
, 0, Data
);
405 CreateNestedKey (PHANDLE KeyHandle
,
406 ACCESS_MASK DesiredAccess
,
407 POBJECT_ATTRIBUTES ObjectAttributes
)
409 OBJECT_ATTRIBUTES LocalObjectAttributes
;
410 UNICODE_STRING LocalKeyName
;
413 USHORT FullNameLength
;
415 HANDLE LocalKeyHandle
;
417 Status
= NtCreateKey (KeyHandle
,
424 DPRINT("NtCreateKey(%wZ) called (Status %lx)\n", ObjectAttributes
->ObjectName
, Status
);
425 if (Status
!= STATUS_OBJECT_NAME_NOT_FOUND
)
428 /* Copy object attributes */
429 RtlCopyMemory (&LocalObjectAttributes
,
431 sizeof(OBJECT_ATTRIBUTES
));
432 RtlCreateUnicodeString (&LocalKeyName
,
433 ObjectAttributes
->ObjectName
->Buffer
);
434 LocalObjectAttributes
.ObjectName
= &LocalKeyName
;
435 FullNameLength
= LocalKeyName
.Length
;
437 /* Remove the last part of the key name and try to create the key again. */
438 while (Status
== STATUS_OBJECT_NAME_NOT_FOUND
)
440 Ptr
= wcsrchr (LocalKeyName
.Buffer
, '\\');
441 if (Ptr
== NULL
|| Ptr
== LocalKeyName
.Buffer
)
443 Status
= STATUS_UNSUCCESSFUL
;
447 LocalKeyName
.Length
= wcslen (LocalKeyName
.Buffer
) * sizeof(WCHAR
);
449 Status
= NtCreateKey (&LocalKeyHandle
,
451 &LocalObjectAttributes
,
456 DPRINT("NtCreateKey(%wZ) called (Status %lx)\n", &LocalKeyName
, Status
);
459 if (!NT_SUCCESS(Status
))
461 RtlFreeUnicodeString (&LocalKeyName
);
465 /* Add removed parts of the key name and create them too. */
468 if (LocalKeyName
.Length
== FullNameLength
)
470 Status
= STATUS_SUCCESS
;
471 *KeyHandle
= LocalKeyHandle
;
474 NtClose (LocalKeyHandle
);
476 LocalKeyName
.Buffer
[LocalKeyName
.Length
/ sizeof(WCHAR
)] = L
'\\';
477 LocalKeyName
.Length
= wcslen (LocalKeyName
.Buffer
) * sizeof(WCHAR
);
479 Status
= NtCreateKey (&LocalKeyHandle
,
481 &LocalObjectAttributes
,
486 DPRINT("NtCreateKey(%wZ) called (Status %lx)\n", &LocalKeyName
, Status
);
487 if (!NT_SUCCESS(Status
))
491 RtlFreeUnicodeString (&LocalKeyName
);
497 /***********************************************************************
500 * Called once for each AddReg and DelReg entry in a given section.
503 registry_callback (HINF hInf
, PCWSTR Section
, BOOLEAN Delete
)
505 OBJECT_ATTRIBUTES ObjectAttributes
;
506 WCHAR Buffer
[MAX_INF_STRING_LENGTH
];
508 UNICODE_STRING Value
;
509 PUNICODE_STRING ValuePtr
;
519 Ok
= SetupFindFirstLineW (hInf
, Section
, NULL
, &Context
);
523 for (;Ok
; Ok
= SetupFindNextLine (&Context
, &Context
))
526 if (!SetupGetStringFieldW (&Context
, 1, Buffer
, MAX_INF_STRING_LENGTH
, NULL
))
528 if (!GetRootKey (Buffer
))
532 Length
= wcslen (Buffer
);
533 if (!SetupGetStringFieldW (&Context
, 2, Buffer
+ Length
, MAX_INF_STRING_LENGTH
- Length
, NULL
))
536 DPRINT("KeyName: <%S>\n", Buffer
);
539 if (!SetupGetIntField (&Context
, 4, (PINT
)&Flags
))
542 DPRINT("Flags: %lx\n", Flags
);
545 RtlInitUnicodeString (&Name
,
548 InitializeObjectAttributes (&ObjectAttributes
,
550 OBJ_CASE_INSENSITIVE
,
554 if (Delete
|| (Flags
& FLG_ADDREG_OVERWRITEONLY
))
556 Status
= NtOpenKey (&KeyHandle
,
559 if (!NT_SUCCESS(Status
))
561 DPRINT("NtOpenKey(%wZ) failed (Status %lx)\n", &Name
, Status
);
562 continue; /* ignore if it doesn't exist */
567 Status
= CreateNestedKey (&KeyHandle
,
570 if (!NT_SUCCESS(Status
))
572 DPRINT("CreateNestedKey(%wZ) failed (Status %lx)\n", &Name
, Status
);
577 if (Delete
|| (Flags
& FLG_ADDREG_OVERWRITEONLY
))
579 LONG rc
= RegOpenKeyW(NULL
, Buffer
, &KeyHandle
);
580 if (rc
!= ERROR_SUCCESS
)
582 DPRINT("RegOpenKeyW(%S) failed (error %lu)\n", Buffer
, rc
);
583 continue; /* ignore if it doesn't exist */
588 LONG rc
= RegCreateKeyW(NULL
, Buffer
, &KeyHandle
);
589 if (rc
!= ERROR_SUCCESS
)
591 DPRINT("RegCreateKeyW(%S) failed (error %lu)\n", Buffer
, rc
);
598 if (SetupGetStringFieldW (&Context
, 3, Buffer
, MAX_INF_STRING_LENGTH
, NULL
))
600 RtlInitUnicodeString (&Value
,
610 if (!do_reg_operation (KeyHandle
, ValuePtr
, &Context
, Flags
))
627 ImportRegistryFile(PWSTR Filename
,
632 WCHAR FileNameBuffer
[MAX_PATH
];
636 /* Load inf file from install media. */
637 wcscpy(FileNameBuffer
, SourcePath
.Buffer
);
638 wcscat(FileNameBuffer
, L
"\\");
639 wcscat(FileNameBuffer
, Filename
);
641 hInf
= SetupOpenInfFileW(
647 if (hInf
== INVALID_HANDLE_VALUE
)
649 DPRINT1("SetupOpenInfFile() failed\n");
653 if (!registry_callback (hInf
, L
"AddReg", FALSE
))
655 DPRINT1("registry_callback() failed\n");
658 if (!registry_callback (hInf
, L
"AddReg.NT" Architecture
, FALSE
))
660 DPRINT1("registry_callback() failed\n");
670 SetInstallPathValue(PUNICODE_STRING InstallPath
)
672 OBJECT_ATTRIBUTES ObjectAttributes
;
673 UNICODE_STRING KeyName
= RTL_CONSTANT_STRING(L
"\\Registry\\Machine\\HARDWARE");
674 UNICODE_STRING ValueName
= RTL_CONSTANT_STRING(L
"InstallPath");
678 /* Create the 'secret' InstallPath key */
679 InitializeObjectAttributes (&ObjectAttributes
,
681 OBJ_CASE_INSENSITIVE
,
684 Status
= NtOpenKey (&KeyHandle
,
687 if (!NT_SUCCESS(Status
))
689 DPRINT1("NtOpenKey() failed (Status %lx)\n", Status
);
693 Status
= NtSetValueKey (KeyHandle
,
697 (PVOID
)InstallPath
->Buffer
,
698 InstallPath
->Length
+ sizeof(WCHAR
));
700 if (!NT_SUCCESS(Status
))
702 DPRINT1("NtSetValueKey() failed (Status %lx)\n", Status
);
710 SetMountedDeviceValue(CHAR Letter
, ULONG Signature
, LARGE_INTEGER StartingOffset
)
712 OBJECT_ATTRIBUTES ObjectAttributes
;
713 WCHAR ValueNameBuffer
[16];
714 UNICODE_STRING KeyName
= RTL_CONSTANT_STRING(L
"\\Registry\\Machine\\SYSTEM\\MountedDevices");
715 UNICODE_STRING ValueName
;
716 REG_DISK_MOUNT_INFO MountInfo
;
720 swprintf(ValueNameBuffer
, L
"\\DosDevices\\%C:", Letter
);
721 RtlInitUnicodeString(&ValueName
, ValueNameBuffer
);
723 InitializeObjectAttributes (&ObjectAttributes
,
725 OBJ_CASE_INSENSITIVE
,
728 Status
= NtOpenKey (&KeyHandle
,
731 if (!NT_SUCCESS(Status
))
733 Status
= NtCreateKey(&KeyHandle
,
738 REG_OPTION_NON_VOLATILE
,
742 if (!NT_SUCCESS(Status
))
744 DPRINT1("NtCreateKey() failed (Status %lx)\n", Status
);
748 MountInfo
.Signature
= Signature
;
749 MountInfo
.StartingOffset
= StartingOffset
;
750 Status
= NtSetValueKey (KeyHandle
,
757 if (!NT_SUCCESS(Status
))
759 DPRINT1("NtSetValueKey() failed (Status %lx)\n", Status
);