Track memory allocations in the SQL parser.
[wine] / dlls / kernel / oldconfig.c
1 /*
2  * Support for converting from old configuration format
3  *
4  * Copyright 2005 Alexandre Julliard
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  *
20  * NOTES
21  *   This file should be removed after a suitable transition period.
22  */
23
24 #include "config.h"
25 #include "wine/port.h"
26
27 #include <stdarg.h>
28 #include <stdlib.h>
29 #include <stdio.h>
30 #ifdef HAVE_SYS_STAT_H
31 # include <sys/stat.h>
32 #endif
33 #include <fcntl.h>
34 #ifdef HAVE_SYS_IOCTL_H
35 #include <sys/ioctl.h>
36 #endif
37 #ifdef HAVE_LINUX_HDREG_H
38 # include <linux/hdreg.h>
39 #endif
40
41 #define NONAMELESSUNION
42 #define NONAMELESSSTRUCT
43 #include "windef.h"
44 #include "winbase.h"
45 #include "winreg.h"
46 #include "winnls.h"
47 #include "winternl.h"
48 #include "ntstatus.h"
49 #include "winioctl.h"
50 #include "ntddscsi.h"
51 #include "wine/library.h"
52 #include "wine/server.h"
53 #include "wine/unicode.h"
54 #include "wine/debug.h"
55
56 WINE_DEFAULT_DEBUG_CHANNEL(reg);
57
58
59 /* create symlinks for the DOS drives; helper for create_dos_devices */
60 static int create_drives( int devices_only )
61 {
62     static const WCHAR PathW[] = {'P','a','t','h',0};
63     static const WCHAR DeviceW[] = {'D','e','v','i','c','e',0};
64     WCHAR driveW[] = {'M','a','c','h','i','n','e','\\','S','o','f','t','w','a','r','e','\\',
65                       'W','i','n','e','\\','W','i','n','e','\\',
66                       'C','o','n','f','i','g','\\','D','r','i','v','e',' ','A',0};
67     const char *config_dir = wine_get_config_dir();
68     OBJECT_ATTRIBUTES attr;
69     UNICODE_STRING nameW;
70     char tmp[1024*sizeof(WCHAR) + sizeof(KEY_VALUE_PARTIAL_INFORMATION)];
71     char dest[1024];
72     char *buffer;
73     WCHAR *p, name[3];
74     HKEY hkey;
75     DWORD dummy;
76     int i, count = 0;
77
78     attr.Length = sizeof(attr);
79     attr.RootDirectory = 0;
80     attr.ObjectName = &nameW;
81     attr.Attributes = 0;
82     attr.SecurityDescriptor = NULL;
83     attr.SecurityQualityOfService = NULL;
84
85     /* create symlinks for the drive roots */
86
87     if (!devices_only) for (i = 0; i < 26; i++)
88     {
89         RtlInitUnicodeString( &nameW, driveW );
90         nameW.Buffer[(nameW.Length / sizeof(WCHAR)) - 1] = 'A' + i;
91         if (NtOpenKey( &hkey, KEY_ALL_ACCESS, &attr ) != STATUS_SUCCESS) continue;
92
93         RtlInitUnicodeString( &nameW, PathW );
94         if (!NtQueryValueKey( hkey, &nameW, KeyValuePartialInformation, tmp, sizeof(tmp), &dummy ))
95         {
96             WCHAR path[1024];
97             WCHAR *data = (WCHAR *)((KEY_VALUE_PARTIAL_INFORMATION *)tmp)->Data;
98             ExpandEnvironmentStringsW( data, path, sizeof(path)/sizeof(WCHAR) );
99
100             p = path + strlenW(path) - 1;
101             while ((p > path) && (*p == '/')) *p-- = '\0';
102
103             name[0] = 'a' + i;
104             name[1] = ':';
105             name[2] = 0;
106
107             if (path[0] != '/')
108             {
109                 /* relative paths are relative to config dir */
110                 memmove( path + 3, path, (strlenW(path) + 1) * sizeof(WCHAR) );
111                 path[0] = '.';
112                 path[1] = '.';
113                 path[2] = '/';
114             }
115             if (DefineDosDeviceW( DDD_RAW_TARGET_PATH, name, path ))
116             {
117                 WideCharToMultiByte(CP_UNIXCP, 0, path, -1, dest, sizeof(dest), NULL, NULL);
118                 MESSAGE( "Created symlink %s/dosdevices/%c: -> %s\n",
119                          wine_get_config_dir(), 'a' + i, dest );
120                 count++;
121             }
122         }
123         NtClose( hkey );
124     }
125
126     /* create symlinks for the drive devices */
127
128     buffer = HeapAlloc( GetProcessHeap(), 0,
129                         strlen(config_dir) + sizeof("/dosdevices/a::") );
130     strcpy( buffer, config_dir );
131     strcat( buffer, "/dosdevices/a::" );
132
133     for (i = 0; i < 26; i++)
134     {
135         RtlInitUnicodeString( &nameW, driveW );
136         nameW.Buffer[(nameW.Length / sizeof(WCHAR)) - 1] = 'A' + i;
137         if (NtOpenKey( &hkey, KEY_ALL_ACCESS, &attr ) != STATUS_SUCCESS) continue;
138
139         RtlInitUnicodeString( &nameW, DeviceW );
140         if (!NtQueryValueKey( hkey, &nameW, KeyValuePartialInformation, tmp, sizeof(tmp), &dummy ))
141         {
142             WCHAR *data = (WCHAR *)((KEY_VALUE_PARTIAL_INFORMATION *)tmp)->Data;
143             WideCharToMultiByte(CP_UNIXCP, 0, data, -1, dest, sizeof(dest), NULL, NULL);
144
145             buffer[strlen(buffer) - 3] = 'a' + i;
146             if (!symlink( dest, buffer ))
147             {
148                 MESSAGE( "Created symlink %s/dosdevices/%c:: -> %s\n",
149                          wine_get_config_dir(), 'a' + i, dest );
150                 count++;
151             }
152         }
153         NtClose( hkey );
154     }
155     HeapFree( GetProcessHeap(), 0, buffer );
156
157     return count;
158 }
159
160
161 /* create the device files for the new device naming scheme */
162 static void create_dos_devices(void)
163 {
164     const char *config_dir = wine_get_config_dir();
165     char *buffer;
166     int i, count = 0;
167
168     if (!(buffer = HeapAlloc( GetProcessHeap(), 0,
169                               strlen(config_dir) + sizeof("/dosdevices/a::") )))
170         return;
171
172     strcpy( buffer, config_dir );
173     strcat( buffer, "/dosdevices" );
174
175     if (!mkdir( buffer, 0777 ))  /* we created it, so now create the devices */
176     {
177         HKEY hkey;
178         DWORD dummy;
179         OBJECT_ATTRIBUTES attr;
180         UNICODE_STRING nameW;
181         WCHAR *p, *devnameW;
182         char tmp[128];
183         WCHAR com[5] = {'C','O','M','1',0};
184         WCHAR lpt[5] = {'L','P','T','1',0};
185
186         static const WCHAR serialportsW[] = {'M','a','c','h','i','n','e','\\',
187                                              'S','o','f','t','w','a','r','e','\\',
188                                              'W','i','n','e','\\','W','i','n','e','\\',
189                                              'C','o','n','f','i','g','\\',
190                                              'S','e','r','i','a','l','P','o','r','t','s',0};
191         static const WCHAR parallelportsW[] = {'M','a','c','h','i','n','e','\\',
192                                                'S','o','f','t','w','a','r','e','\\',
193                                                'W','i','n','e','\\','W','i','n','e','\\',
194                                                'C','o','n','f','i','g','\\',
195                                                'P','a','r','a','l','l','e','l','P','o','r','t','s',0};
196
197         attr.Length = sizeof(attr);
198         attr.RootDirectory = 0;
199         attr.ObjectName = &nameW;
200         attr.Attributes = 0;
201         attr.SecurityDescriptor = NULL;
202         attr.SecurityQualityOfService = NULL;
203         RtlInitUnicodeString( &nameW, serialportsW );
204
205         if (!NtOpenKey( &hkey, KEY_ALL_ACCESS, &attr ))
206         {
207             RtlInitUnicodeString( &nameW, com );
208             for (i = 1; i <= 9; i++)
209             {
210                 com[3] = '0' + i;
211                 if (!NtQueryValueKey( hkey, &nameW, KeyValuePartialInformation,
212                                       tmp, sizeof(tmp), &dummy ))
213                 {
214                     devnameW = (WCHAR *)((KEY_VALUE_PARTIAL_INFORMATION *)tmp)->Data;
215                     if ((p = strchrW( devnameW, ',' ))) *p = 0;
216                     if (DefineDosDeviceW( DDD_RAW_TARGET_PATH, com, devnameW ))
217                     {
218                         char devname[32];
219                         WideCharToMultiByte(CP_UNIXCP, 0, devnameW, -1,
220                                             devname, sizeof(devname), NULL, NULL);
221                         MESSAGE( "Created symlink %s/dosdevices/com%d -> %s\n", config_dir, i, devname );
222                         count++;
223                     }
224                 }
225             }
226             NtClose( hkey );
227         }
228
229         RtlInitUnicodeString( &nameW, parallelportsW );
230         if (!NtOpenKey( &hkey, KEY_ALL_ACCESS, &attr ))
231         {
232             RtlInitUnicodeString( &nameW, lpt );
233             for (i = 1; i <= 9; i++)
234             {
235                 lpt[3] = '0' + i;
236                 if (!NtQueryValueKey( hkey, &nameW, KeyValuePartialInformation,
237                                       tmp, sizeof(tmp), &dummy ))
238                 {
239                     devnameW = (WCHAR *)((KEY_VALUE_PARTIAL_INFORMATION *)tmp)->Data;
240                     if ((p = strchrW( devnameW, ',' ))) *p = 0;
241                     if (DefineDosDeviceW( DDD_RAW_TARGET_PATH, lpt, devnameW ))
242                     {
243                         char devname[32];
244                         WideCharToMultiByte(CP_UNIXCP, 0, devnameW, -1,
245                                             devname, sizeof(devname), NULL, NULL);
246                         MESSAGE( "Created symlink %s/dosdevices/lpt%d -> %s\n", config_dir, i, devname );
247                         count++;
248                     }
249                 }
250             }
251             NtClose( hkey );
252         }
253         count += create_drives( FALSE );
254     }
255     else
256     {
257         struct stat st;
258         int i;
259
260         /* it is possible that the serial/parallel devices have been created but */
261         /* not the drives; check for at least one drive symlink to catch that case */
262         strcat( buffer, "/a:" );
263         for (i = 0; i < 26; i++)
264         {
265             buffer[strlen(buffer)-2] = 'a' + i;
266             if (!lstat( buffer, &st )) break;
267         }
268         if (i == 26) count += create_drives( FALSE );
269         else
270         {
271             strcat( buffer, ":" );
272             for (i = 0; i < 26; i++)
273             {
274                 buffer[strlen(buffer)-3] = 'a' + i;
275                 if (!lstat( buffer, &st )) break;
276             }
277             if (i == 26) count += create_drives( TRUE );
278         }
279     }
280
281     if (count)
282         MESSAGE( "\nYou can now remove the [SerialPorts], [ParallelPorts], and [Drive] sections\n"
283                  "in your configuration file, they are replaced by the above symlinks.\n\n" );
284
285     HeapFree( GetProcessHeap(), 0, buffer );
286 }
287
288
289 /* convert the drive type entries from the old format to the new one */
290 static void convert_drive_types(void)
291 {
292     static const WCHAR TypeW[] = {'T','y','p','e',0};
293     static const WCHAR drive_types_keyW[] = {'M','a','c','h','i','n','e','\\',
294                                              'S','o','f','t','w','a','r','e','\\',
295                                              'W','i','n','e','\\',
296                                              'D','r','i','v','e','s',0 };
297     WCHAR driveW[] = {'M','a','c','h','i','n','e','\\','S','o','f','t','w','a','r','e','\\',
298                       'W','i','n','e','\\','W','i','n','e','\\',
299                       'C','o','n','f','i','g','\\','D','r','i','v','e',' ','A',0};
300     char tmp[32*sizeof(WCHAR) + sizeof(KEY_VALUE_PARTIAL_INFORMATION)];
301     OBJECT_ATTRIBUTES attr;
302     UNICODE_STRING nameW;
303     DWORD dummy;
304     ULONG disp;
305     HKEY hkey_old, hkey_new;
306     int i;
307
308     attr.Length = sizeof(attr);
309     attr.RootDirectory = 0;
310     attr.ObjectName = &nameW;
311     attr.Attributes = 0;
312     attr.SecurityDescriptor = NULL;
313     attr.SecurityQualityOfService = NULL;
314     RtlInitUnicodeString( &nameW, drive_types_keyW );
315
316     if (NtCreateKey( &hkey_new, KEY_ALL_ACCESS, &attr, 0, NULL, 0, &disp )) return;
317     if (disp != REG_CREATED_NEW_KEY)
318     {
319         NtClose( hkey_new );
320         return;
321     }
322
323     for (i = 0; i < 26; i++)
324     {
325         RtlInitUnicodeString( &nameW, driveW );
326         nameW.Buffer[(nameW.Length / sizeof(WCHAR)) - 1] = 'A' + i;
327         if (NtOpenKey( &hkey_old, KEY_ALL_ACCESS, &attr ) != STATUS_SUCCESS) continue;
328         RtlInitUnicodeString( &nameW, TypeW );
329         if (!NtQueryValueKey( hkey_old, &nameW, KeyValuePartialInformation, tmp, sizeof(tmp), &dummy ))
330         {
331             WCHAR valueW[] = {'A',':',0};
332             WCHAR *type = (WCHAR *)((KEY_VALUE_PARTIAL_INFORMATION *)tmp)->Data;
333
334             valueW[0] = 'A' + i;
335             RtlInitUnicodeString( &nameW, valueW );
336             NtSetValueKey( hkey_new, &nameW, 0, REG_SZ, type, (strlenW(type) + 1) * sizeof(WCHAR) );
337             MESSAGE( "Converted drive type to new entry HKLM\\Software\\Wine\\Drives \"%c:\" = %s\n",
338                      'A' + i, debugstr_w(type) );
339         }
340         NtClose( hkey_old );
341     }
342     NtClose( hkey_new );
343 }
344
345
346 /* convert the environment variable entries from the old format to the new one */
347 static void convert_environment( HKEY hkey_current_user )
348 {
349     static const WCHAR wineW[] = {'M','a','c','h','i','n','e','\\',
350                                   'S','o','f','t','w','a','r','e','\\',
351                                   'W','i','n','e','\\','W','i','n','e','\\',
352                                   'C','o','n','f','i','g','\\','W','i','n','e',0};
353     static const WCHAR windowsW[] = {'w','i','n','d','o','w','s',0};
354     static const WCHAR systemW[] = {'s','y','s','t','e','m',0};
355     static const WCHAR windirW[] = {'w','i','n','d','i','r',0};
356     static const WCHAR systemrootW[] = {'S','y','s','t','e','m','r','o','o','t',0};
357     static const WCHAR winsysdirW[] = {'w','i','n','s','y','s','d','i','r',0};
358     static const WCHAR envW[] = {'E','n','v','i','r','o','n','m','e','n','t',0};
359     static const WCHAR tempW[] = {'T','E','M','P',0};
360     static const WCHAR tmpW[] = {'T','M','P',0};
361     static const WCHAR pathW[] = {'P','A','T','H',0};
362     static const WCHAR profileW[] = {'p','r','o','f','i','l','e',0};
363     static const WCHAR userprofileW[] = {'U','S','E','R','P','R','O','F','I','L','E',0};
364
365     char buffer[1024*sizeof(WCHAR) + sizeof(KEY_VALUE_PARTIAL_INFORMATION)];
366     KEY_VALUE_PARTIAL_INFORMATION *info = (KEY_VALUE_PARTIAL_INFORMATION *)buffer;
367     OBJECT_ATTRIBUTES attr;
368     UNICODE_STRING nameW;
369     DWORD dummy;
370     ULONG disp;
371     HKEY hkey_old, hkey_env;
372
373     attr.Length = sizeof(attr);
374     attr.RootDirectory = 0;
375     attr.ObjectName = &nameW;
376     attr.Attributes = 0;
377     attr.SecurityDescriptor = NULL;
378     attr.SecurityQualityOfService = NULL;
379     RtlInitUnicodeString( &nameW, wineW );
380
381     if (NtOpenKey( &hkey_old, KEY_ALL_ACCESS, &attr ) != STATUS_SUCCESS) return;
382
383     attr.RootDirectory = hkey_current_user;
384     RtlInitUnicodeString( &nameW, envW );
385     if (NtCreateKey( &hkey_env, KEY_ALL_ACCESS, &attr, 0, NULL, 0, &disp ))
386     {
387         NtClose( hkey_old );
388         return;
389     }
390
391     /* Test for existence of TEMP */
392     RtlInitUnicodeString( &nameW, tempW );
393     if (NtQueryValueKey(hkey_env, &nameW, KeyValuePartialInformation, buffer, sizeof(buffer), &dummy ))
394     {
395         /* convert TEMP */
396         RtlInitUnicodeString( &nameW, tempW );
397         if (!NtQueryValueKey( hkey_old, &nameW, KeyValuePartialInformation, buffer, sizeof(buffer), &dummy ))
398         {
399             NtSetValueKey( hkey_env, &nameW, 0, info->Type, info->Data, info->DataLength );
400             RtlInitUnicodeString( &nameW, tmpW );
401             NtSetValueKey( hkey_env, &nameW, 0, info->Type, info->Data, info->DataLength );
402             MESSAGE( "Converted temp dir to new entry HKCU\\Environment \"TEMP\" = %s\n",
403                     debugstr_w( (WCHAR*)info->Data ) );
404         }
405     }
406
407     /* Test for existence of PATH */
408     RtlInitUnicodeString( &nameW, pathW );
409     if (NtQueryValueKey(hkey_env, &nameW, KeyValuePartialInformation, buffer, sizeof(buffer), &dummy ))
410     {
411         /* convert PATH */
412         RtlInitUnicodeString( &nameW, pathW );
413         if (!NtQueryValueKey( hkey_old, &nameW, KeyValuePartialInformation, buffer, sizeof(buffer), &dummy ))
414         {
415             NtSetValueKey( hkey_env, &nameW, 0, info->Type, info->Data, info->DataLength );
416             MESSAGE( "Converted path dir to new entry HKCU\\Environment \"PATH\" = %s\n",
417                     debugstr_w( (WCHAR*)info->Data ) );
418         }
419     }
420
421     /* Test for existence of USERPROFILE */
422     RtlInitUnicodeString( &nameW, userprofileW );
423     if (NtQueryValueKey(hkey_env, &nameW, KeyValuePartialInformation, buffer, sizeof(buffer), &dummy ))
424     {
425         /* convert USERPROFILE */
426         RtlInitUnicodeString( &nameW, profileW );
427         if (!NtQueryValueKey( hkey_old, &nameW, KeyValuePartialInformation, buffer, sizeof(buffer), &dummy ))
428         {
429             RtlInitUnicodeString( &nameW, userprofileW );
430             NtSetValueKey( hkey_env, &nameW, 0, info->Type, info->Data, info->DataLength );
431             MESSAGE( "Converted profile dir to new entry HKCU\\Environment \"USERPROFILE\" = %s\n",
432                     debugstr_w( (WCHAR*)info->Data ) );
433         }
434     }
435
436     /* Test for existence of windir */
437     RtlInitUnicodeString( &nameW, windirW );
438     if (NtQueryValueKey(hkey_env, &nameW, KeyValuePartialInformation, buffer, sizeof(buffer), &dummy ))
439     {
440         /* convert windir */
441         RtlInitUnicodeString( &nameW, windowsW );
442         if (!NtQueryValueKey( hkey_old, &nameW, KeyValuePartialInformation, buffer, sizeof(buffer), &dummy ))
443         {
444             RtlInitUnicodeString( &nameW, windirW );
445             NtSetValueKey( hkey_env, &nameW, 0, info->Type, info->Data, info->DataLength );
446             RtlInitUnicodeString( &nameW, systemrootW );
447             NtSetValueKey( hkey_env, &nameW, 0, info->Type, info->Data, info->DataLength );
448             MESSAGE( "Converted windows dir to new entry HKCU\\Environment \"windir\" = %s\n",
449                     debugstr_w( (WCHAR*)info->Data ) );
450         }
451     }
452
453     /* Test for existence of winsysdir */
454     RtlInitUnicodeString( &nameW, winsysdirW );
455     if (NtQueryValueKey(hkey_env, &nameW, KeyValuePartialInformation, buffer, sizeof(buffer), &dummy ))
456     {
457         /* convert winsysdir */
458         RtlInitUnicodeString( &nameW, systemW );
459         if (!NtQueryValueKey( hkey_old, &nameW, KeyValuePartialInformation, buffer, sizeof(buffer), &dummy ))
460         {
461             RtlInitUnicodeString( &nameW, winsysdirW );
462             NtSetValueKey( hkey_env, &nameW, 0, info->Type, info->Data, info->DataLength );
463             MESSAGE( "Converted system dir to new entry HKCU\\Environment \"winsysdir\" = %s\n",
464                     debugstr_w( (WCHAR*)info->Data ) );
465         }
466     }
467     NtClose( hkey_old );
468     NtClose( hkey_env );
469 }
470
471
472 /* registry initialisation, allocates some default keys. */
473 static ULONG allocate_default_keys(void)
474 {
475     static const WCHAR StatDataW[] = {'D','y','n','D','a','t','a','\\',
476                                       'P','e','r','f','S','t','a','t','s','\\',
477                                       'S','t','a','t','D','a','t','a',0};
478     static const WCHAR ConfigManagerW[] = {'D','y','n','D','a','t','a','\\',
479                                            'C','o','n','f','i','g',' ','M','a','n','a','g','e','r','\\',
480                                             'E','n','u','m',0};
481     HKEY hkey;
482     ULONG dispos;
483     OBJECT_ATTRIBUTES attr;
484     UNICODE_STRING nameW;
485
486     attr.Length = sizeof(attr);
487     attr.RootDirectory = 0;
488     attr.ObjectName = &nameW;
489     attr.Attributes = 0;
490     attr.SecurityDescriptor = NULL;
491     attr.SecurityQualityOfService = NULL;
492
493     RtlInitUnicodeString( &nameW, StatDataW );
494     if (!NtCreateKey( &hkey, KEY_ALL_ACCESS, &attr, 0, NULL, 0, &dispos )) NtClose( hkey );
495     if (dispos == REG_OPENED_EXISTING_KEY)
496         return dispos; /* someone else already loaded the registry */
497
498     RtlInitUnicodeString( &nameW, ConfigManagerW );
499     if (!NtCreateKey( &hkey, KEY_ALL_ACCESS, &attr, 0, NULL, 0, NULL )) NtClose( hkey );
500
501     return dispos;
502 }
503
504
505 /******************************************************************
506  *              init_cdrom_registry
507  *
508  * Initializes registry to contain scsi info about the cdrom in NT.
509  * All devices (even not real scsi ones) have this info in NT.
510  * TODO: for now it only works for non scsi devices
511  * NOTE: programs usually read these registry entries after sending the
512  *       IOCTL_SCSI_GET_ADDRESS ioctl to the cdrom
513  */
514 static void init_cdrom_registry( HANDLE handle )
515 {
516     OBJECT_ATTRIBUTES attr;
517     UNICODE_STRING nameW;
518     WCHAR dataW[50];
519     DWORD lenW;
520     char buffer[40];
521     DWORD value;
522     const char *data;
523     HKEY scsiKey;
524     HKEY portKey;
525     HKEY busKey;
526     HKEY targetKey;
527     DWORD disp;
528     IO_STATUS_BLOCK io;
529     SCSI_ADDRESS scsi_addr;
530
531     if (NtDeviceIoControlFile( handle, 0, NULL, NULL, &io, IOCTL_SCSI_GET_ADDRESS,
532                                NULL, 0, &scsi_addr, sizeof(scsi_addr) ))
533         return;
534
535     attr.Length = sizeof(attr);
536     attr.RootDirectory = 0;
537     attr.ObjectName = &nameW;
538     attr.Attributes = 0;
539     attr.SecurityDescriptor = NULL;
540     attr.SecurityQualityOfService = NULL;
541
542     /* Ensure there is Scsi key */
543     if (!RtlCreateUnicodeStringFromAsciiz( &nameW, "Machine\\HARDWARE\\DEVICEMAP\\Scsi" ) ||
544         NtCreateKey( &scsiKey, KEY_ALL_ACCESS, &attr, 0,
545                      NULL, REG_OPTION_VOLATILE, &disp ))
546     {
547         ERR("Cannot create DEVICEMAP\\Scsi registry key\n" );
548         return;
549     }
550     RtlFreeUnicodeString( &nameW );
551
552     snprintf(buffer,sizeof(buffer),"Scsi Port %d",scsi_addr.PortNumber);
553     attr.RootDirectory = scsiKey;
554     if (!RtlCreateUnicodeStringFromAsciiz( &nameW, buffer ) ||
555         NtCreateKey( &portKey, KEY_ALL_ACCESS, &attr, 0,
556                      NULL, REG_OPTION_VOLATILE, &disp ))
557     {
558         ERR("Cannot create DEVICEMAP\\Scsi Port registry key\n" );
559         return;
560     }
561     RtlFreeUnicodeString( &nameW );
562
563     RtlCreateUnicodeStringFromAsciiz( &nameW, "Driver" );
564     data = "atapi";
565     RtlMultiByteToUnicodeN( dataW, 50, &lenW, data, strlen(data));
566     NtSetValueKey( portKey, &nameW, 0, REG_SZ, (BYTE*)dataW, lenW );
567     RtlFreeUnicodeString( &nameW );
568     value = 10;
569     RtlCreateUnicodeStringFromAsciiz( &nameW, "FirstBusTimeScanInMs" );
570     NtSetValueKey( portKey,&nameW, 0, REG_DWORD, (BYTE *)&value, sizeof(DWORD));
571     RtlFreeUnicodeString( &nameW );
572     value = 0;
573 #ifdef HDIO_GET_DMA
574     {
575         int fd, dma;
576         if (!wine_server_handle_to_fd( handle, 0, &fd, NULL ))
577         {
578             if (ioctl(fd,HDIO_GET_DMA, &dma) != -1) value = dma;
579             wine_server_release_fd( handle, fd );
580         }
581     }
582 #endif
583     RtlCreateUnicodeStringFromAsciiz( &nameW, "DMAEnabled" );
584     NtSetValueKey( portKey,&nameW, 0, REG_DWORD, (BYTE *)&value, sizeof(DWORD));
585     RtlFreeUnicodeString( &nameW );
586
587     snprintf(buffer,40,"Scsi Bus %d", scsi_addr.PathId);
588     attr.RootDirectory = portKey;
589     if (!RtlCreateUnicodeStringFromAsciiz( &nameW, buffer ) ||
590         NtCreateKey( &busKey, KEY_ALL_ACCESS, &attr, 0,
591                      NULL, REG_OPTION_VOLATILE, &disp ))
592     {
593         ERR("Cannot create DEVICEMAP\\Scsi Port\\Scsi Bus registry key\n" );
594         return;
595     }
596     RtlFreeUnicodeString( &nameW );
597
598     attr.RootDirectory = busKey;
599     if (!RtlCreateUnicodeStringFromAsciiz( &nameW, "Initiator Id 255" ) ||
600         NtCreateKey( &targetKey, KEY_ALL_ACCESS, &attr, 0,
601                      NULL, REG_OPTION_VOLATILE, &disp ))
602     {
603         ERR("Cannot create DEVICEMAP\\Scsi Port\\Scsi Bus\\Initiator Id 255 registry key\n" );
604         return;
605     }
606     RtlFreeUnicodeString( &nameW );
607     NtClose( targetKey );
608
609     snprintf(buffer,40,"Target Id %d", scsi_addr.TargetId);
610     attr.RootDirectory = busKey;
611     if (!RtlCreateUnicodeStringFromAsciiz( &nameW, buffer ) ||
612         NtCreateKey( &targetKey, KEY_ALL_ACCESS, &attr, 0,
613                      NULL, REG_OPTION_VOLATILE, &disp ))
614     {
615         ERR("Cannot create DEVICEMAP\\Scsi Port\\Scsi Bus 0\\Target Id registry key\n" );
616         return;
617     }
618     RtlFreeUnicodeString( &nameW );
619
620     RtlCreateUnicodeStringFromAsciiz( &nameW, "Type" );
621     data = "CdRomPeripheral";
622     RtlMultiByteToUnicodeN( dataW, 50, &lenW, data, strlen(data));
623     NtSetValueKey( targetKey, &nameW, 0, REG_SZ, (BYTE*)dataW, lenW );
624     RtlFreeUnicodeString( &nameW );
625     /* FIXME - maybe read the real identifier?? */
626     RtlCreateUnicodeStringFromAsciiz( &nameW, "Identifier" );
627     data = "Wine CDROM";
628     RtlMultiByteToUnicodeN( dataW, 50, &lenW, data, strlen(data));
629     NtSetValueKey( targetKey, &nameW, 0, REG_SZ, (BYTE*)dataW, lenW );
630     RtlFreeUnicodeString( &nameW );
631     /* FIXME - we always use Cdrom0 - do not know about the nt behaviour */
632     RtlCreateUnicodeStringFromAsciiz( &nameW, "DeviceName" );
633     data = "Cdrom0";
634     RtlMultiByteToUnicodeN( dataW, 50, &lenW, data, strlen(data));
635     NtSetValueKey( targetKey, &nameW, 0, REG_SZ, (BYTE*)dataW, lenW );
636     RtlFreeUnicodeString( &nameW );
637
638     NtClose( targetKey );
639     NtClose( busKey );
640     NtClose( portKey );
641     NtClose( scsiKey );
642 }
643
644
645 /* create the hardware registry branch */
646 static void create_hardware_branch(void)
647 {
648     int i;
649     HANDLE handle;
650     char drive[] = "\\\\.\\A:";
651
652     /* create entries for cdroms (skipping A: and B:) */
653     for (i = 2; i < 26; i++)
654     {
655         drive[4] = 'A' + i;
656         handle = CreateFileA( drive, 0, 0, NULL, OPEN_EXISTING, 0, 0 );
657         if (handle == INVALID_HANDLE_VALUE) continue;
658         init_cdrom_registry( handle );
659         CloseHandle( handle );
660     }
661 }
662
663
664 /***********************************************************************
665  *              convert_old_config
666  */
667 void convert_old_config(void)
668 {
669     HKEY hkey_current_user;
670
671     if (allocate_default_keys() == REG_OPENED_EXISTING_KEY)
672         return; /* someone else already loaded the registry */
673
674     RtlOpenCurrentUser( KEY_ALL_ACCESS, &hkey_current_user );
675
676     /* load the user registry (FIXME: should be done at server init time) */
677     SERVER_START_REQ( load_user_registries )
678     {
679         req->hkey = hkey_current_user;
680         wine_server_call( req );
681     }
682     SERVER_END_REQ;
683
684     /* convert old configuration */
685     create_dos_devices();
686     convert_drive_types();
687     convert_environment( hkey_current_user );
688
689     /* create some hardware keys (FIXME: should not be done here) */
690     create_hardware_branch();
691
692     NtClose( hkey_current_user );
693 }