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