kernel32: Moved creation of the DynData registry keys to wineboot.
[wine] / dlls / kernel32 / 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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, 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 <sys/types.h>
29 #include <stdlib.h>
30 #include <stdio.h>
31 #ifdef HAVE_SYS_STAT_H
32 # include <sys/stat.h>
33 #endif
34 #include <fcntl.h>
35 #ifdef HAVE_DIRENT_H
36 # include <dirent.h>
37 #endif
38 #ifdef HAVE_SYS_IOCTL_H
39 #include <sys/ioctl.h>
40 #endif
41 #ifdef HAVE_LINUX_HDREG_H
42 # include <linux/hdreg.h>
43 #endif
44
45 #define NONAMELESSUNION
46 #define NONAMELESSSTRUCT
47 #include "windef.h"
48 #include "winbase.h"
49 #include "winternl.h"
50 #include "ntddscsi.h"
51 #include "wine/library.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_scsi_entry
60  *
61  * Initializes registry to contain scsi info about the cdrom in NT.
62  * All devices (even not real scsi ones) have this info in NT.
63  * NOTE: programs usually read these registry entries after sending the
64  *       IOCTL_SCSI_GET_ADDRESS ioctl to the cdrom
65  */
66 static void create_scsi_entry( PSCSI_ADDRESS scsi_addr, LPCSTR lpDriver, UINT uDriveType,
67     LPSTR lpDriveName, LPSTR lpUnixDeviceName )
68 {
69     static UCHAR uCdromNumber = 0;
70     static UCHAR uTapeNumber = 0;
71
72     OBJECT_ATTRIBUTES attr;
73     UNICODE_STRING nameW;
74     WCHAR dataW[50];
75     DWORD sizeW;
76     char buffer[40];
77     DWORD value;
78     const char *data;
79     HANDLE scsiKey;
80     HANDLE portKey;
81     HANDLE busKey;
82     HANDLE targetKey;
83     HANDLE lunKey;
84     DWORD disp;
85
86     attr.Length = sizeof(attr);
87     attr.RootDirectory = 0;
88     attr.ObjectName = &nameW;
89     attr.Attributes = 0;
90     attr.SecurityDescriptor = NULL;
91     attr.SecurityQualityOfService = NULL;
92
93     /* Ensure there is Scsi key */
94     if (!RtlCreateUnicodeStringFromAsciiz( &nameW, "Machine\\HARDWARE\\DEVICEMAP\\Scsi" ) ||
95         NtCreateKey( &scsiKey, KEY_ALL_ACCESS, &attr, 0,
96                      NULL, REG_OPTION_VOLATILE, &disp ))
97     {
98         ERR("Cannot create DEVICEMAP\\Scsi registry key\n" );
99         return;
100     }
101     RtlFreeUnicodeString( &nameW );
102
103     snprintf(buffer,sizeof(buffer),"Scsi Port %d",scsi_addr->PortNumber);
104     attr.RootDirectory = scsiKey;
105     if (!RtlCreateUnicodeStringFromAsciiz( &nameW, buffer ) ||
106         NtCreateKey( &portKey, KEY_ALL_ACCESS, &attr, 0,
107                      NULL, REG_OPTION_VOLATILE, &disp ))
108     {
109         ERR("Cannot create DEVICEMAP\\Scsi Port registry key\n" );
110         return;
111     }
112     RtlFreeUnicodeString( &nameW );
113
114     RtlCreateUnicodeStringFromAsciiz( &nameW, "Driver" );
115     RtlMultiByteToUnicodeN( dataW, sizeof(dataW), &sizeW, lpDriver, strlen(lpDriver)+1);
116     NtSetValueKey( portKey, &nameW, 0, REG_SZ, dataW, sizeW );
117     RtlFreeUnicodeString( &nameW );
118     value = 10;
119     RtlCreateUnicodeStringFromAsciiz( &nameW, "FirstBusTimeScanInMs" );
120     NtSetValueKey( portKey,&nameW, 0, REG_DWORD, &value, sizeof(DWORD) );
121     RtlFreeUnicodeString( &nameW );
122
123     value = 0;
124     if (strcmp(lpDriver, "atapi") == 0)
125     {
126 #ifdef HDIO_GET_DMA
127         int fd, dma;
128         
129         fd = open(lpUnixDeviceName, O_RDONLY|O_NONBLOCK);
130         if (fd != -1)
131         {
132             if (ioctl(fd, HDIO_GET_DMA, &dma) != -1) value = dma;
133             close(fd);
134         }
135 #endif
136         RtlCreateUnicodeStringFromAsciiz( &nameW, "DMAEnabled" );
137         NtSetValueKey( portKey,&nameW, 0, REG_DWORD, &value, sizeof(DWORD) );
138         RtlFreeUnicodeString( &nameW );
139     }
140
141     snprintf(buffer, sizeof(buffer),"Scsi Bus %d", scsi_addr->PathId);
142     attr.RootDirectory = portKey;
143     if (!RtlCreateUnicodeStringFromAsciiz( &nameW, buffer ) ||
144         NtCreateKey( &busKey, KEY_ALL_ACCESS, &attr, 0,
145                      NULL, REG_OPTION_VOLATILE, &disp ))
146     {
147         ERR("Cannot create DEVICEMAP\\Scsi Port\\Scsi Bus registry key\n" );
148         return;
149     }
150     RtlFreeUnicodeString( &nameW );
151
152     attr.RootDirectory = busKey;
153     /* FIXME: get real controller Id for SCSI */
154     if (!RtlCreateUnicodeStringFromAsciiz( &nameW, "Initiator Id 255" ) ||
155         NtCreateKey( &targetKey, KEY_ALL_ACCESS, &attr, 0,
156                      NULL, REG_OPTION_VOLATILE, &disp ))
157     {
158         ERR("Cannot create DEVICEMAP\\Scsi Port\\Scsi Bus\\Initiator Id 255 registry key\n" );
159         return;
160     }
161     RtlFreeUnicodeString( &nameW );
162     NtClose( targetKey );
163
164     snprintf(buffer, sizeof(buffer),"Target Id %d", scsi_addr->TargetId);
165     attr.RootDirectory = busKey;
166     if (!RtlCreateUnicodeStringFromAsciiz( &nameW, buffer ) ||
167         NtCreateKey( &targetKey, KEY_ALL_ACCESS, &attr, 0,
168                      NULL, REG_OPTION_VOLATILE, &disp ))
169     {
170         ERR("Cannot create DEVICEMAP\\Scsi Port\\Scsi Bus 0\\Target Id registry key\n" );
171         return;
172     }
173     RtlFreeUnicodeString( &nameW );
174
175     snprintf(buffer, sizeof(buffer),"Logical Unit Id %d", scsi_addr->Lun);
176     attr.RootDirectory = targetKey;
177     if (!RtlCreateUnicodeStringFromAsciiz( &nameW, buffer ) ||
178         NtCreateKey( &lunKey, KEY_ALL_ACCESS, &attr, 0,
179                      NULL, REG_OPTION_VOLATILE, &disp ))
180     {
181         ERR("Cannot create DEVICEMAP\\Scsi Port\\Scsi Bus 0\\Target Id registry key\\Logical Unit Id\n" );
182         return;
183     }
184     RtlFreeUnicodeString( &nameW );
185
186     switch (uDriveType)
187     {
188         case DRIVE_NO_ROOT_DIR:
189         default:
190             data = "OtherPeripheral"; break;
191         case DRIVE_FIXED:
192             data = "DiskPeripheral"; break;
193         case DRIVE_REMOVABLE:
194             data = "TapePeripheral";
195             snprintf(buffer, sizeof(buffer), "Tape%d", uTapeNumber++);
196             break;
197         case DRIVE_CDROM:
198             data = "CdRomPeripheral";
199             snprintf(buffer, sizeof(buffer), "Cdrom%d", uCdromNumber++);
200             break;
201     }
202     RtlCreateUnicodeStringFromAsciiz( &nameW, "Type" );
203     RtlMultiByteToUnicodeN( dataW, sizeof(dataW), &sizeW, data, strlen(data)+1);
204     NtSetValueKey( lunKey, &nameW, 0, REG_SZ, dataW, sizeW );
205     RtlFreeUnicodeString( &nameW );
206
207     RtlCreateUnicodeStringFromAsciiz( &nameW, "Identifier" );
208     RtlMultiByteToUnicodeN( dataW, sizeof(dataW), &sizeW, lpDriveName, strlen(lpDriveName)+1);
209     NtSetValueKey( lunKey, &nameW, 0, REG_SZ, dataW, sizeW );
210     RtlFreeUnicodeString( &nameW );
211
212     if (uDriveType == DRIVE_CDROM || uDriveType == DRIVE_REMOVABLE)
213     {
214         RtlCreateUnicodeStringFromAsciiz( &nameW, "DeviceName" );
215         RtlMultiByteToUnicodeN( dataW, sizeof(dataW), &sizeW, buffer, strlen(buffer)+1);
216         NtSetValueKey( lunKey, &nameW, 0, REG_SZ, dataW, sizeW );
217         RtlFreeUnicodeString( &nameW );
218     }
219
220     RtlCreateUnicodeStringFromAsciiz( &nameW, "UnixDeviceName" );
221     RtlMultiByteToUnicodeN( dataW, sizeof(dataW), &sizeW, lpUnixDeviceName, strlen(lpUnixDeviceName)+1);
222     NtSetValueKey( lunKey, &nameW, 0, REG_SZ, dataW, sizeW );
223     RtlFreeUnicodeString( &nameW );
224
225     NtClose( lunKey );
226     NtClose( targetKey );
227     NtClose( busKey );
228     NtClose( portKey );
229     NtClose( scsiKey );
230 }
231
232 struct LinuxProcScsiDevice
233 {
234     int host;
235     int channel;
236     int target;
237     int lun;
238     char vendor[9];
239     char model[17];
240     char rev[5];
241     char type[33];
242     int ansirev;
243 };
244
245 /*
246  * we need to declare white spaces explicitly via %*1[ ],
247  * as there are very dainbread CD-ROM devices out there
248  * which have their manufacturer name blanked out via spaces,
249  * which confuses fscanf's parsing (skips all blank spaces)
250  */
251 static int SCSI_getprocentry( FILE * procfile, struct LinuxProcScsiDevice * dev )
252 {
253     int result;
254
255     result = fscanf( procfile,
256         "Host:%*1[ ]scsi%d%*1[ ]Channel:%*1[ ]%d%*1[ ]Id:%*1[ ]%d%*1[ ]Lun:%*1[ ]%d\n",
257         &dev->host,
258         &dev->channel,
259         &dev->target,
260         &dev->lun );
261     if( result == EOF )
262     {
263         /* "end of entries" return, so no TRACE() here */
264         return EOF;
265     }
266     if( result != 4 )
267     {
268         ERR("bus id line scan count error (fscanf returns %d, expected 4)\n", result);
269         return 0;
270     }
271     result = fscanf( procfile,
272         "  Vendor:%*1[ ]%8c%*1[ ]Model:%*1[ ]%16c%*1[ ]Rev:%*1[ ]%4c\n",
273         dev->vendor,
274         dev->model,
275         dev->rev );
276     if( result != 3 )
277     {
278         ERR("model line scan count error (fscanf returns %d, expected 3)\n", result);
279         return 0;
280     }
281
282     result = fscanf( procfile,
283         "  Type:%*3[ ]%32c%*1[ ]ANSI SCSI%*1[ ]revision:%*1[ ]%x\n",
284         dev->type,
285         &dev->ansirev );
286     if( result != 2 )
287     {
288         ERR("SCSI type line scan count error (fscanf returns %d, expected 2)\n", result);
289         return 0;
290     }
291     /* Since we fscanf with %XXc instead of %s.. put a NULL at end */
292     dev->vendor[8] = 0;
293     dev->model[16] = 0;
294     dev->rev[4] = 0;
295     dev->type[32] = 0;
296
297     return 1;
298 }
299
300
301 /* create the hardware registry branch */
302 static void create_hardware_branch(void)
303 {
304     /* The following mostly will work on Linux, but should not cause
305      * problems on other systems. */
306     static const char procname_ide_media[] = "/proc/ide/%s/media";
307     static const char procname_ide_model[] = "/proc/ide/%s/model";
308     static const char procname_scsi[] = "/proc/scsi/scsi";
309     DIR *idedir;
310     struct dirent *dent = NULL;
311     FILE *procfile = NULL;
312     char cStr[40], cDevModel[40], cUnixDeviceName[40], read1[10] = "\0", read2[10] = "\0";
313     SCSI_ADDRESS scsi_addr;
314     UINT nType;
315     struct LinuxProcScsiDevice dev;
316     int result = 0, nSgNumber = 0;
317     UCHAR uFirstSCSIPort = 0;
318
319     /* Enumerate all ide devices first */
320     idedir = opendir("/proc/ide");
321     if (idedir)
322     {
323         while ((dent = readdir(idedir)))
324         {
325             if (strncmp(dent->d_name, "hd", 2) == 0)
326             {
327                 sprintf(cStr, procname_ide_media, dent->d_name);
328                 procfile = fopen(cStr, "r");
329                 if (!procfile)
330                 {
331                     ERR("Could not open %s\n", cStr);
332                     continue;
333                 } else {
334                     fgets(cStr, sizeof(cStr), procfile);
335                     fclose(procfile);
336                     nType = DRIVE_UNKNOWN;
337                     if (strncasecmp(cStr, "disk", 4)  == 0) nType = DRIVE_FIXED;
338                     if (strncasecmp(cStr, "cdrom", 5) == 0) nType = DRIVE_CDROM;
339
340                     if (nType == DRIVE_UNKNOWN) continue;
341                 }
342
343                 sprintf(cStr, procname_ide_model, dent->d_name);
344                 procfile = fopen(cStr, "r");
345                 if (!procfile)
346                 {
347                     ERR("Could not open %s\n", cStr);
348                     switch (nType)
349                     {
350                     case DRIVE_FIXED: strcpy(cDevModel, "Wine harddisk"); break;
351                     case DRIVE_CDROM: strcpy(cDevModel, "Wine CDROM"); break;
352                     }
353                 } else {
354                     fgets(cDevModel, sizeof(cDevModel), procfile);
355                     fclose(procfile);
356                     cDevModel[strlen(cDevModel) - 1] = 0;
357                 }
358
359                 sprintf(cUnixDeviceName, "/dev/%s", dent->d_name);
360                 scsi_addr.PortNumber = (dent->d_name[2] - 'a') / 2;
361                 scsi_addr.PathId = 0;
362                 scsi_addr.TargetId = (dent->d_name[2] - 'a') % 2;
363                 scsi_addr.Lun = 0;
364                 if (scsi_addr.PortNumber + 1 > uFirstSCSIPort)
365                     uFirstSCSIPort = scsi_addr.PortNumber + 1;
366
367                 create_scsi_entry(&scsi_addr, "atapi", nType, cDevModel, cUnixDeviceName);
368             }
369         }
370         closedir(idedir);
371     }
372
373     /* Now goes SCSI */
374     procfile = fopen(procname_scsi, "r");
375     if (!procfile)
376     {
377         TRACE("Could not open %s\n", procname_scsi);
378         return;
379     }
380     fgets(cStr, 40, procfile);
381     sscanf(cStr, "Attached %9s %9s", read1, read2);
382
383     if (strcmp(read1, "devices:") != 0)
384     {
385         WARN("Incorrect %s format\n", procname_scsi);
386         fclose( procfile );
387         return;
388     }
389     if (strcmp(read2, "none") == 0)
390     {
391         TRACE("No SCSI devices found in %s.\n", procname_scsi);
392         fclose( procfile );
393         return;
394     }
395
396     /* Read info for one device */
397     while ((result = SCSI_getprocentry(procfile, &dev)) > 0)
398     {
399         scsi_addr.PortNumber = dev.host;
400         scsi_addr.PathId = dev.channel;
401         scsi_addr.TargetId = dev.target;
402         scsi_addr.Lun = dev.lun;
403
404         scsi_addr.PortNumber += uFirstSCSIPort;
405         if (strncmp(dev.type, "Direct-Access", 13) == 0) nType = DRIVE_FIXED;
406         else if (strncmp(dev.type, "Sequential-Access", 17) == 0) nType = DRIVE_REMOVABLE;
407         else if (strncmp(dev.type, "CD-ROM", 6) == 0) nType = DRIVE_CDROM;
408         else if (strncmp(dev.type, "Processor", 9) == 0) nType = DRIVE_NO_ROOT_DIR;
409         else continue;
410
411         strcpy(cDevModel, dev.vendor);
412         strcat(cDevModel, dev.model);
413         strcat(cDevModel, dev.rev);
414         sprintf(cUnixDeviceName, "/dev/sg%d", nSgNumber++);
415         /* FIXME: get real driver name */
416         create_scsi_entry(&scsi_addr, "WINE SCSI", nType, cDevModel, cUnixDeviceName);
417     }
418     if( result != EOF )
419         WARN("Incorrect %s format\n", procname_scsi);
420     fclose( procfile );
421 }
422
423
424 /***********************************************************************
425  *              convert_old_config
426  */
427 void convert_old_config(void)
428 {
429     /* create some hardware keys (FIXME: should not be done here) */
430     create_hardware_branch();
431 }