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