4 * Copyright 1996 Marcus Meissner
5 * Copyright 1998 Matthew Becker
6 * Copyright 1999 Sylvain St-Germain
8 * December 21, 1997 - Kevin Cozens
9 * Fixed bugs in the _w95_loadreg() function. Added extra information
10 * regarding the format of the Windows '95 registry files.
12 * This library is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU Lesser General Public
14 * License as published by the Free Software Foundation; either
15 * version 2.1 of the License, or (at your option) any later version.
17 * This library is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * Lesser General Public License for more details.
22 * You should have received a copy of the GNU Lesser General Public
23 * License along with this library; if not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
29 * Time for RegEnumKey*, RegQueryInfoKey*
33 #include "wine/port.h"
43 #include <sys/types.h>
46 #ifdef HAVE_SYS_IOCTL_H
47 #include <sys/ioctl.h>
49 #ifdef HAVE_LINUX_HDREG_H
50 # include <linux/hdreg.h>
53 #define NONAMELESSUNION
54 #define NONAMELESSSTRUCT
62 #include "wine/library.h"
63 #include "wine/server.h"
64 #include "wine/unicode.h"
66 #include "wine/debug.h"
68 WINE_DEFAULT_DEBUG_CHANNEL(reg
);
71 /******************************************************************************
72 * allocate_default_keys [Internal]
73 * Registry initialisation, allocates some default keys.
75 static ULONG
allocate_default_keys(void)
77 static const WCHAR StatDataW
[] = {'D','y','n','D','a','t','a','\\',
78 'P','e','r','f','S','t','a','t','s','\\',
79 'S','t','a','t','D','a','t','a',0};
80 static const WCHAR ConfigManagerW
[] = {'D','y','n','D','a','t','a','\\',
81 'C','o','n','f','i','g',' ','M','a','n','a','g','e','r','\\',
83 static const WCHAR Clone
[] = {'M','a','c','h','i','n','e','\\',
84 'S','y','s','t','e','m','\\',
85 'C','l','o','n','e',0};
88 OBJECT_ATTRIBUTES attr
;
91 attr
.Length
= sizeof(attr
);
92 attr
.RootDirectory
= 0;
93 attr
.ObjectName
= &nameW
;
95 attr
.SecurityDescriptor
= NULL
;
96 attr
.SecurityQualityOfService
= NULL
;
98 RtlInitUnicodeString( &nameW
, StatDataW
);
99 if (!NtCreateKey( &hkey
, KEY_ALL_ACCESS
, &attr
, 0, NULL
, 0, &dispos
)) NtClose( hkey
);
100 if (dispos
== REG_OPENED_EXISTING_KEY
)
101 return dispos
; /* someone else already loaded the registry */
103 RtlInitUnicodeString( &nameW
, ConfigManagerW
);
104 if (!NtCreateKey( &hkey
, KEY_ALL_ACCESS
, &attr
, 0, NULL
, 0, NULL
)) NtClose( hkey
);
106 /* this key is generated when the nt-core booted successfully */
107 RtlInitUnicodeString( &nameW
, Clone
);
108 if (!NtCreateKey( &hkey
, KEY_ALL_ACCESS
, &attr
, 0, NULL
, 0, NULL
)) NtClose( hkey
);
114 /******************************************************************
115 * init_cdrom_registry
117 * Initializes registry to contain scsi info about the cdrom in NT.
118 * All devices (even not real scsi ones) have this info in NT.
119 * TODO: for now it only works for non scsi devices
120 * NOTE: programs usually read these registry entries after sending the
121 * IOCTL_SCSI_GET_ADDRESS ioctl to the cdrom
123 static void init_cdrom_registry( HANDLE handle
)
125 OBJECT_ATTRIBUTES attr
;
126 UNICODE_STRING nameW
;
138 SCSI_ADDRESS scsi_addr
;
140 if (NtDeviceIoControlFile( handle
, 0, NULL
, NULL
, &io
, IOCTL_SCSI_GET_ADDRESS
,
141 NULL
, 0, &scsi_addr
, sizeof(scsi_addr
) ))
144 attr
.Length
= sizeof(attr
);
145 attr
.RootDirectory
= 0;
146 attr
.ObjectName
= &nameW
;
148 attr
.SecurityDescriptor
= NULL
;
149 attr
.SecurityQualityOfService
= NULL
;
151 /* Ensure there is Scsi key */
152 if (!RtlCreateUnicodeStringFromAsciiz( &nameW
, "Machine\\HARDWARE\\DEVICEMAP\\Scsi" ) ||
153 NtCreateKey( &scsiKey
, KEY_ALL_ACCESS
, &attr
, 0,
154 NULL
, REG_OPTION_VOLATILE
, &disp
))
156 ERR("Cannot create DEVICEMAP\\Scsi registry key\n" );
159 RtlFreeUnicodeString( &nameW
);
161 snprintf(buffer
,sizeof(buffer
),"Scsi Port %d",scsi_addr
.PortNumber
);
162 attr
.RootDirectory
= scsiKey
;
163 if (!RtlCreateUnicodeStringFromAsciiz( &nameW
, buffer
) ||
164 NtCreateKey( &portKey
, KEY_ALL_ACCESS
, &attr
, 0,
165 NULL
, REG_OPTION_VOLATILE
, &disp
))
167 ERR("Cannot create DEVICEMAP\\Scsi Port registry key\n" );
170 RtlFreeUnicodeString( &nameW
);
172 RtlCreateUnicodeStringFromAsciiz( &nameW
, "Driver" );
174 RtlMultiByteToUnicodeN( dataW
, 50, &lenW
, data
, strlen(data
));
175 NtSetValueKey( portKey
, &nameW
, 0, REG_SZ
, (BYTE
*)dataW
, lenW
);
176 RtlFreeUnicodeString( &nameW
);
178 RtlCreateUnicodeStringFromAsciiz( &nameW
, "FirstBusTimeScanInMs" );
179 NtSetValueKey( portKey
,&nameW
, 0, REG_DWORD
, (BYTE
*)&value
, sizeof(DWORD
));
180 RtlFreeUnicodeString( &nameW
);
185 if (!wine_server_handle_to_fd( handle
, 0, &fd
, NULL
))
187 if (ioctl(fd
,HDIO_GET_DMA
, &dma
) != -1) value
= dma
;
188 wine_server_release_fd( handle
, fd
);
192 RtlCreateUnicodeStringFromAsciiz( &nameW
, "DMAEnabled" );
193 NtSetValueKey( portKey
,&nameW
, 0, REG_DWORD
, (BYTE
*)&value
, sizeof(DWORD
));
194 RtlFreeUnicodeString( &nameW
);
196 snprintf(buffer
,40,"Scsi Bus %d", scsi_addr
.PathId
);
197 attr
.RootDirectory
= portKey
;
198 if (!RtlCreateUnicodeStringFromAsciiz( &nameW
, buffer
) ||
199 NtCreateKey( &busKey
, KEY_ALL_ACCESS
, &attr
, 0,
200 NULL
, REG_OPTION_VOLATILE
, &disp
))
202 ERR("Cannot create DEVICEMAP\\Scsi Port\\Scsi Bus registry key\n" );
205 RtlFreeUnicodeString( &nameW
);
207 attr
.RootDirectory
= busKey
;
208 if (!RtlCreateUnicodeStringFromAsciiz( &nameW
, "Initiator Id 255" ) ||
209 NtCreateKey( &targetKey
, KEY_ALL_ACCESS
, &attr
, 0,
210 NULL
, REG_OPTION_VOLATILE
, &disp
))
212 ERR("Cannot create DEVICEMAP\\Scsi Port\\Scsi Bus\\Initiator Id 255 registry key\n" );
215 RtlFreeUnicodeString( &nameW
);
216 NtClose( targetKey
);
218 snprintf(buffer
,40,"Target Id %d", scsi_addr
.TargetId
);
219 attr
.RootDirectory
= busKey
;
220 if (!RtlCreateUnicodeStringFromAsciiz( &nameW
, buffer
) ||
221 NtCreateKey( &targetKey
, KEY_ALL_ACCESS
, &attr
, 0,
222 NULL
, REG_OPTION_VOLATILE
, &disp
))
224 ERR("Cannot create DEVICEMAP\\Scsi Port\\Scsi Bus 0\\Target Id registry key\n" );
227 RtlFreeUnicodeString( &nameW
);
229 RtlCreateUnicodeStringFromAsciiz( &nameW
, "Type" );
230 data
= "CdRomPeripheral";
231 RtlMultiByteToUnicodeN( dataW
, 50, &lenW
, data
, strlen(data
));
232 NtSetValueKey( targetKey
, &nameW
, 0, REG_SZ
, (BYTE
*)dataW
, lenW
);
233 RtlFreeUnicodeString( &nameW
);
234 /* FIXME - maybe read the real identifier?? */
235 RtlCreateUnicodeStringFromAsciiz( &nameW
, "Identifier" );
237 RtlMultiByteToUnicodeN( dataW
, 50, &lenW
, data
, strlen(data
));
238 NtSetValueKey( targetKey
, &nameW
, 0, REG_SZ
, (BYTE
*)dataW
, lenW
);
239 RtlFreeUnicodeString( &nameW
);
240 /* FIXME - we always use Cdrom0 - do not know about the nt behaviour */
241 RtlCreateUnicodeStringFromAsciiz( &nameW
, "DeviceName" );
243 RtlMultiByteToUnicodeN( dataW
, 50, &lenW
, data
, strlen(data
));
244 NtSetValueKey( targetKey
, &nameW
, 0, REG_SZ
, (BYTE
*)dataW
, lenW
);
245 RtlFreeUnicodeString( &nameW
);
247 NtClose( targetKey
);
254 /* create the hardware registry branch */
255 static void create_hardware_branch(void)
259 char drive
[] = "\\\\.\\A:";
261 /* create entries for cdroms */
262 for (i
= 0; i
< 26; i
++)
265 handle
= CreateFileA( drive
, 0, 0, NULL
, OPEN_EXISTING
, 0, 0 );
266 if (handle
== INVALID_HANDLE_VALUE
) continue;
267 init_cdrom_registry( handle
);
268 CloseHandle( handle
);
273 /* convert the drive type entries from the old format to the new one */
274 static void convert_drive_types(void)
276 static const WCHAR TypeW
[] = {'T','y','p','e',0};
277 static const WCHAR drive_types_keyW
[] = {'M','a','c','h','i','n','e','\\',
278 'S','o','f','t','w','a','r','e','\\',
279 'W','i','n','e','\\',
280 'D','r','i','v','e','s',0 };
281 WCHAR driveW
[] = {'M','a','c','h','i','n','e','\\','S','o','f','t','w','a','r','e','\\',
282 'W','i','n','e','\\','W','i','n','e','\\',
283 'C','o','n','f','i','g','\\','D','r','i','v','e',' ','A',0};
284 char tmp
[32*sizeof(WCHAR
) + sizeof(KEY_VALUE_PARTIAL_INFORMATION
)];
285 OBJECT_ATTRIBUTES attr
;
286 UNICODE_STRING nameW
;
289 HKEY hkey_old
, hkey_new
;
292 attr
.Length
= sizeof(attr
);
293 attr
.RootDirectory
= 0;
294 attr
.ObjectName
= &nameW
;
296 attr
.SecurityDescriptor
= NULL
;
297 attr
.SecurityQualityOfService
= NULL
;
298 RtlInitUnicodeString( &nameW
, drive_types_keyW
);
300 if (NtCreateKey( &hkey_new
, KEY_ALL_ACCESS
, &attr
, 0, NULL
, 0, &disp
)) return;
301 if (disp
!= REG_CREATED_NEW_KEY
)
307 for (i
= 0; i
< 26; i
++)
309 RtlInitUnicodeString( &nameW
, driveW
);
310 nameW
.Buffer
[(nameW
.Length
/ sizeof(WCHAR
)) - 1] = 'A' + i
;
311 if (NtOpenKey( &hkey_old
, KEY_ALL_ACCESS
, &attr
) != STATUS_SUCCESS
) continue;
312 RtlInitUnicodeString( &nameW
, TypeW
);
313 if (!NtQueryValueKey( hkey_old
, &nameW
, KeyValuePartialInformation
, tmp
, sizeof(tmp
), &dummy
))
315 WCHAR valueW
[] = {'A',':',0};
316 WCHAR
*type
= (WCHAR
*)((KEY_VALUE_PARTIAL_INFORMATION
*)tmp
)->Data
;
319 RtlInitUnicodeString( &nameW
, valueW
);
320 NtSetValueKey( hkey_new
, &nameW
, 0, REG_SZ
, type
, (strlenW(type
) + 1) * sizeof(WCHAR
) );
321 MESSAGE( "Converted drive type to new entry HKLM\\Software\\Wine\\Drives \"%c:\" = %s\n",
322 'A' + i
, debugstr_w(type
) );
330 /* convert the environment variable entries from the old format to the new one */
331 static void convert_environment( HKEY hkey_current_user
)
333 static const WCHAR wineW
[] = {'M','a','c','h','i','n','e','\\',
334 'S','o','f','t','w','a','r','e','\\',
335 'W','i','n','e','\\','W','i','n','e','\\',
336 'C','o','n','f','i','g','\\','W','i','n','e',0};
337 static const WCHAR windowsW
[] = {'w','i','n','d','o','w','s',0};
338 static const WCHAR systemW
[] = {'s','y','s','t','e','m',0};
339 static const WCHAR windirW
[] = {'w','i','n','d','i','r',0};
340 static const WCHAR systemrootW
[] = {'S','y','s','t','e','m','r','o','o','t',0};
341 static const WCHAR winsysdirW
[] = {'w','i','n','s','y','s','d','i','r',0};
342 static const WCHAR envW
[] = {'E','n','v','i','r','o','n','m','e','n','t',0};
343 static const WCHAR tempW
[] = {'T','E','M','P',0};
344 static const WCHAR tmpW
[] = {'T','M','P',0};
345 static const WCHAR pathW
[] = {'P','A','T','H',0};
346 static const WCHAR profileW
[] = {'p','r','o','f','i','l','e',0};
347 static const WCHAR userprofileW
[] = {'U','S','E','R','P','R','O','F','I','L','E',0};
349 char buffer
[1024*sizeof(WCHAR
) + sizeof(KEY_VALUE_PARTIAL_INFORMATION
)];
350 KEY_VALUE_PARTIAL_INFORMATION
*info
= (KEY_VALUE_PARTIAL_INFORMATION
*)buffer
;
351 OBJECT_ATTRIBUTES attr
;
352 UNICODE_STRING nameW
;
355 HKEY hkey_old
, hkey_env
;
357 attr
.Length
= sizeof(attr
);
358 attr
.RootDirectory
= 0;
359 attr
.ObjectName
= &nameW
;
361 attr
.SecurityDescriptor
= NULL
;
362 attr
.SecurityQualityOfService
= NULL
;
363 RtlInitUnicodeString( &nameW
, wineW
);
365 if (NtOpenKey( &hkey_old
, KEY_ALL_ACCESS
, &attr
) != STATUS_SUCCESS
) return;
367 attr
.RootDirectory
= hkey_current_user
;
368 RtlInitUnicodeString( &nameW
, envW
);
369 if (NtCreateKey( &hkey_env
, KEY_ALL_ACCESS
, &attr
, 0, NULL
, 0, &disp
))
375 /* Test for existence of TEMP */
376 RtlInitUnicodeString( &nameW
, tempW
);
377 if (NtQueryValueKey(hkey_env
, &nameW
, KeyValuePartialInformation
, buffer
, sizeof(buffer
), &dummy
))
380 RtlInitUnicodeString( &nameW
, tempW
);
381 if (!NtQueryValueKey( hkey_old
, &nameW
, KeyValuePartialInformation
, buffer
, sizeof(buffer
), &dummy
))
383 NtSetValueKey( hkey_env
, &nameW
, 0, info
->Type
, info
->Data
, info
->DataLength
);
384 RtlInitUnicodeString( &nameW
, tmpW
);
385 NtSetValueKey( hkey_env
, &nameW
, 0, info
->Type
, info
->Data
, info
->DataLength
);
386 MESSAGE( "Converted temp dir to new entry HKCU\\Environment \"TEMP\" = %s\n",
387 debugstr_w( (WCHAR
*)info
->Data
) );
391 /* Test for existence of PATH */
392 RtlInitUnicodeString( &nameW
, pathW
);
393 if (NtQueryValueKey(hkey_env
, &nameW
, KeyValuePartialInformation
, buffer
, sizeof(buffer
), &dummy
))
396 RtlInitUnicodeString( &nameW
, pathW
);
397 if (!NtQueryValueKey( hkey_old
, &nameW
, KeyValuePartialInformation
, buffer
, sizeof(buffer
), &dummy
))
399 NtSetValueKey( hkey_env
, &nameW
, 0, info
->Type
, info
->Data
, info
->DataLength
);
400 MESSAGE( "Converted path dir to new entry HKCU\\Environment \"PATH\" = %s\n",
401 debugstr_w( (WCHAR
*)info
->Data
) );
405 /* Test for existence of USERPROFILE */
406 RtlInitUnicodeString( &nameW
, userprofileW
);
407 if (NtQueryValueKey(hkey_env
, &nameW
, KeyValuePartialInformation
, buffer
, sizeof(buffer
), &dummy
))
409 /* convert USERPROFILE */
410 RtlInitUnicodeString( &nameW
, profileW
);
411 if (!NtQueryValueKey( hkey_old
, &nameW
, KeyValuePartialInformation
, buffer
, sizeof(buffer
), &dummy
))
413 RtlInitUnicodeString( &nameW
, userprofileW
);
414 NtSetValueKey( hkey_env
, &nameW
, 0, info
->Type
, info
->Data
, info
->DataLength
);
415 MESSAGE( "Converted profile dir to new entry HKCU\\Environment \"USERPROFILE\" = %s\n",
416 debugstr_w( (WCHAR
*)info
->Data
) );
420 /* Test for existence of windir */
421 RtlInitUnicodeString( &nameW
, windirW
);
422 if (NtQueryValueKey(hkey_env
, &nameW
, KeyValuePartialInformation
, buffer
, sizeof(buffer
), &dummy
))
425 RtlInitUnicodeString( &nameW
, windowsW
);
426 if (!NtQueryValueKey( hkey_old
, &nameW
, KeyValuePartialInformation
, buffer
, sizeof(buffer
), &dummy
))
428 RtlInitUnicodeString( &nameW
, windirW
);
429 NtSetValueKey( hkey_env
, &nameW
, 0, info
->Type
, info
->Data
, info
->DataLength
);
430 RtlInitUnicodeString( &nameW
, systemrootW
);
431 NtSetValueKey( hkey_env
, &nameW
, 0, info
->Type
, info
->Data
, info
->DataLength
);
432 MESSAGE( "Converted windows dir to new entry HKCU\\Environment \"windir\" = %s\n",
433 debugstr_w( (WCHAR
*)info
->Data
) );
437 /* Test for existence of winsysdir */
438 RtlInitUnicodeString( &nameW
, winsysdirW
);
439 if (NtQueryValueKey(hkey_env
, &nameW
, KeyValuePartialInformation
, buffer
, sizeof(buffer
), &dummy
))
441 /* convert winsysdir */
442 RtlInitUnicodeString( &nameW
, systemW
);
443 if (!NtQueryValueKey( hkey_old
, &nameW
, KeyValuePartialInformation
, buffer
, sizeof(buffer
), &dummy
))
445 RtlInitUnicodeString( &nameW
, winsysdirW
);
446 NtSetValueKey( hkey_env
, &nameW
, 0, info
->Type
, info
->Data
, info
->DataLength
);
447 MESSAGE( "Converted system dir to new entry HKCU\\Environment \"winsysdir\" = %s\n",
448 debugstr_w( (WCHAR
*)info
->Data
) );
456 /* load all registry (native and global and home) */
457 void SHELL_LoadRegistry( void )
459 HKEY hkey_current_user
;
460 OBJECT_ATTRIBUTES attr
;
461 UNICODE_STRING nameW
;
466 attr
.Length
= sizeof(attr
);
467 attr
.RootDirectory
= 0;
468 attr
.ObjectName
= &nameW
;
470 attr
.SecurityDescriptor
= NULL
;
471 attr
.SecurityQualityOfService
= NULL
;
473 dispos
= allocate_default_keys();
474 if (dispos
== REG_OPENED_EXISTING_KEY
)
475 return; /* someone else already loaded the registry */
477 RtlOpenCurrentUser( KEY_ALL_ACCESS
, &hkey_current_user
);
479 /* load home registries */
481 SERVER_START_REQ( load_user_registries
)
483 req
->hkey
= hkey_current_user
;
484 wine_server_call( req
);
488 /* create hardware registry branch */
490 create_hardware_branch();
492 /* convert keys from config file to new registry format */
494 convert_drive_types();
495 convert_environment( hkey_current_user
);
497 NtClose(hkey_current_user
);