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