2 * Support for converting from old configuration format
4 * Copyright 2005 Alexandre Julliard
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.
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.
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
21 * This file should be removed after a suitable transition period.
25 #include "wine/port.h"
28 #include <sys/types.h>
31 #ifdef HAVE_SYS_STAT_H
32 # include <sys/stat.h>
38 #ifdef HAVE_SYS_IOCTL_H
39 #include <sys/ioctl.h>
41 #ifdef HAVE_LINUX_HDREG_H
42 # include <linux/hdreg.h>
45 #define NONAMELESSUNION
46 #define NONAMELESSSTRUCT
51 #include "wine/library.h"
52 #include "wine/unicode.h"
53 #include "wine/debug.h"
54 #include "kernel_private.h"
56 WINE_DEFAULT_DEBUG_CHANNEL(reg
);
58 /******************************************************************
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
66 static void create_scsi_entry( PSCSI_ADDRESS scsi_addr
, LPCSTR lpDriver
, UINT uDriveType
,
67 LPSTR lpDriveName
, LPSTR lpUnixDeviceName
)
69 static UCHAR uCdromNumber
= 0;
70 static UCHAR uTapeNumber
= 0;
72 OBJECT_ATTRIBUTES attr
;
86 attr
.Length
= sizeof(attr
);
87 attr
.RootDirectory
= 0;
88 attr
.ObjectName
= &nameW
;
90 attr
.SecurityDescriptor
= NULL
;
91 attr
.SecurityQualityOfService
= NULL
;
93 if (!RtlCreateUnicodeStringFromAsciiz( &nameW
, "Machine\\HARDWARE" ) ||
94 NtCreateKey( &scsiKey
, KEY_ALL_ACCESS
, &attr
, 0, NULL
, REG_OPTION_VOLATILE
, &disp
))
96 ERR("Cannot create HARDWARE registry key\n" );
100 RtlFreeUnicodeString( &nameW
);
101 if (disp
== REG_OPENED_EXISTING_KEY
) return;
103 if (!RtlCreateUnicodeStringFromAsciiz( &nameW
, "Machine\\HARDWARE\\DEVICEMAP" ) ||
104 NtCreateKey( &scsiKey
, KEY_ALL_ACCESS
, &attr
, 0, NULL
, REG_OPTION_VOLATILE
, &disp
))
106 ERR("Cannot create DEVICEMAP registry key\n" );
110 RtlFreeUnicodeString( &nameW
);
112 /* Ensure there is Scsi key */
113 if (!RtlCreateUnicodeStringFromAsciiz( &nameW
, "Machine\\HARDWARE\\DEVICEMAP\\Scsi" ) ||
114 NtCreateKey( &scsiKey
, KEY_ALL_ACCESS
, &attr
, 0,
115 NULL
, REG_OPTION_VOLATILE
, &disp
))
117 ERR("Cannot create DEVICEMAP\\Scsi registry key\n" );
120 RtlFreeUnicodeString( &nameW
);
122 snprintf(buffer
,sizeof(buffer
),"Scsi Port %d",scsi_addr
->PortNumber
);
123 attr
.RootDirectory
= scsiKey
;
124 if (!RtlCreateUnicodeStringFromAsciiz( &nameW
, buffer
) ||
125 NtCreateKey( &portKey
, KEY_ALL_ACCESS
, &attr
, 0,
126 NULL
, REG_OPTION_VOLATILE
, &disp
))
128 ERR("Cannot create DEVICEMAP\\Scsi Port registry key\n" );
131 RtlFreeUnicodeString( &nameW
);
133 RtlCreateUnicodeStringFromAsciiz( &nameW
, "Driver" );
134 RtlMultiByteToUnicodeN( dataW
, sizeof(dataW
), &sizeW
, lpDriver
, strlen(lpDriver
)+1);
135 NtSetValueKey( portKey
, &nameW
, 0, REG_SZ
, dataW
, sizeW
);
136 RtlFreeUnicodeString( &nameW
);
138 RtlCreateUnicodeStringFromAsciiz( &nameW
, "FirstBusTimeScanInMs" );
139 NtSetValueKey( portKey
,&nameW
, 0, REG_DWORD
, &value
, sizeof(DWORD
) );
140 RtlFreeUnicodeString( &nameW
);
143 if (strcmp(lpDriver
, "atapi") == 0)
148 fd
= open(lpUnixDeviceName
, O_RDONLY
|O_NONBLOCK
);
151 if (ioctl(fd
, HDIO_GET_DMA
, &dma
) != -1) value
= dma
;
155 RtlCreateUnicodeStringFromAsciiz( &nameW
, "DMAEnabled" );
156 NtSetValueKey( portKey
,&nameW
, 0, REG_DWORD
, &value
, sizeof(DWORD
) );
157 RtlFreeUnicodeString( &nameW
);
160 snprintf(buffer
, sizeof(buffer
),"Scsi Bus %d", scsi_addr
->PathId
);
161 attr
.RootDirectory
= portKey
;
162 if (!RtlCreateUnicodeStringFromAsciiz( &nameW
, buffer
) ||
163 NtCreateKey( &busKey
, KEY_ALL_ACCESS
, &attr
, 0,
164 NULL
, REG_OPTION_VOLATILE
, &disp
))
166 ERR("Cannot create DEVICEMAP\\Scsi Port\\Scsi Bus registry key\n" );
169 RtlFreeUnicodeString( &nameW
);
171 attr
.RootDirectory
= busKey
;
172 /* FIXME: get real controller Id for SCSI */
173 if (!RtlCreateUnicodeStringFromAsciiz( &nameW
, "Initiator Id 255" ) ||
174 NtCreateKey( &targetKey
, KEY_ALL_ACCESS
, &attr
, 0,
175 NULL
, REG_OPTION_VOLATILE
, &disp
))
177 ERR("Cannot create DEVICEMAP\\Scsi Port\\Scsi Bus\\Initiator Id 255 registry key\n" );
180 RtlFreeUnicodeString( &nameW
);
181 NtClose( targetKey
);
183 snprintf(buffer
, sizeof(buffer
),"Target Id %d", scsi_addr
->TargetId
);
184 attr
.RootDirectory
= busKey
;
185 if (!RtlCreateUnicodeStringFromAsciiz( &nameW
, buffer
) ||
186 NtCreateKey( &targetKey
, KEY_ALL_ACCESS
, &attr
, 0,
187 NULL
, REG_OPTION_VOLATILE
, &disp
))
189 ERR("Cannot create DEVICEMAP\\Scsi Port\\Scsi Bus 0\\Target Id registry key\n" );
192 RtlFreeUnicodeString( &nameW
);
194 snprintf(buffer
, sizeof(buffer
),"Logical Unit Id %d", scsi_addr
->Lun
);
195 attr
.RootDirectory
= targetKey
;
196 if (!RtlCreateUnicodeStringFromAsciiz( &nameW
, buffer
) ||
197 NtCreateKey( &lunKey
, KEY_ALL_ACCESS
, &attr
, 0,
198 NULL
, REG_OPTION_VOLATILE
, &disp
))
200 ERR("Cannot create DEVICEMAP\\Scsi Port\\Scsi Bus 0\\Target Id registry key\\Logical Unit Id\n" );
203 RtlFreeUnicodeString( &nameW
);
207 case DRIVE_NO_ROOT_DIR
:
209 data
= "OtherPeripheral"; break;
211 data
= "DiskPeripheral"; break;
212 case DRIVE_REMOVABLE
:
213 data
= "TapePeripheral";
214 snprintf(buffer
, sizeof(buffer
), "Tape%d", uTapeNumber
++);
217 data
= "CdRomPeripheral";
218 snprintf(buffer
, sizeof(buffer
), "Cdrom%d", uCdromNumber
++);
221 RtlCreateUnicodeStringFromAsciiz( &nameW
, "Type" );
222 RtlMultiByteToUnicodeN( dataW
, sizeof(dataW
), &sizeW
, data
, strlen(data
)+1);
223 NtSetValueKey( lunKey
, &nameW
, 0, REG_SZ
, dataW
, sizeW
);
224 RtlFreeUnicodeString( &nameW
);
226 RtlCreateUnicodeStringFromAsciiz( &nameW
, "Identifier" );
227 RtlMultiByteToUnicodeN( dataW
, sizeof(dataW
), &sizeW
, lpDriveName
, strlen(lpDriveName
)+1);
228 NtSetValueKey( lunKey
, &nameW
, 0, REG_SZ
, dataW
, sizeW
);
229 RtlFreeUnicodeString( &nameW
);
231 if (uDriveType
== DRIVE_CDROM
|| uDriveType
== DRIVE_REMOVABLE
)
233 RtlCreateUnicodeStringFromAsciiz( &nameW
, "DeviceName" );
234 RtlMultiByteToUnicodeN( dataW
, sizeof(dataW
), &sizeW
, buffer
, strlen(buffer
)+1);
235 NtSetValueKey( lunKey
, &nameW
, 0, REG_SZ
, dataW
, sizeW
);
236 RtlFreeUnicodeString( &nameW
);
239 RtlCreateUnicodeStringFromAsciiz( &nameW
, "UnixDeviceName" );
240 RtlMultiByteToUnicodeN( dataW
, sizeof(dataW
), &sizeW
, lpUnixDeviceName
, strlen(lpUnixDeviceName
)+1);
241 NtSetValueKey( lunKey
, &nameW
, 0, REG_SZ
, dataW
, sizeW
);
242 RtlFreeUnicodeString( &nameW
);
245 NtClose( targetKey
);
251 struct LinuxProcScsiDevice
265 * we need to declare white spaces explicitly via %*1[ ],
266 * as there are very dainbread CD-ROM devices out there
267 * which have their manufacturer name blanked out via spaces,
268 * which confuses fscanf's parsing (skips all blank spaces)
270 static int SCSI_getprocentry( FILE * procfile
, struct LinuxProcScsiDevice
* dev
)
274 result
= fscanf( procfile
,
275 "Host:%*1[ ]scsi%d%*1[ ]Channel:%*1[ ]%d%*1[ ]Id:%*1[ ]%d%*1[ ]Lun:%*1[ ]%d\n",
282 /* "end of entries" return, so no TRACE() here */
287 ERR("bus id line scan count error (fscanf returns %d, expected 4)\n", result
);
290 result
= fscanf( procfile
,
291 " Vendor:%*1[ ]%8c%*1[ ]Model:%*1[ ]%16c%*1[ ]Rev:%*1[ ]%4c\n",
297 ERR("model line scan count error (fscanf returns %d, expected 3)\n", result
);
301 result
= fscanf( procfile
,
302 " Type:%*3[ ]%32c%*1[ ]ANSI SCSI%*1[ ]revision:%*1[ ]%x\n",
307 ERR("SCSI type line scan count error (fscanf returns %d, expected 2)\n", result
);
310 /* Since we fscanf with %XXc instead of %s.. put a NULL at end */
320 /* create the hardware registry branch */
321 static void create_hardware_branch(void)
323 /* The following mostly will work on Linux, but should not cause
324 * problems on other systems. */
325 static const char procname_ide_media
[] = "/proc/ide/%s/media";
326 static const char procname_ide_model
[] = "/proc/ide/%s/model";
327 static const char procname_scsi
[] = "/proc/scsi/scsi";
329 struct dirent
*dent
= NULL
;
330 FILE *procfile
= NULL
;
331 char cStr
[40], cDevModel
[40], cUnixDeviceName
[40], read1
[10] = "\0", read2
[10] = "\0";
332 SCSI_ADDRESS scsi_addr
;
334 struct LinuxProcScsiDevice dev
;
335 int result
= 0, nSgNumber
= 0;
336 UCHAR uFirstSCSIPort
= 0;
338 /* Enumerate all ide devices first */
339 idedir
= opendir("/proc/ide");
342 while ((dent
= readdir(idedir
)))
344 if (strncmp(dent
->d_name
, "hd", 2) == 0)
346 sprintf(cStr
, procname_ide_media
, dent
->d_name
);
347 procfile
= fopen(cStr
, "r");
350 ERR("Could not open %s\n", cStr
);
353 fgets(cStr
, sizeof(cStr
), procfile
);
355 nType
= DRIVE_UNKNOWN
;
356 if (strncasecmp(cStr
, "disk", 4) == 0) nType
= DRIVE_FIXED
;
357 if (strncasecmp(cStr
, "cdrom", 5) == 0) nType
= DRIVE_CDROM
;
359 if (nType
== DRIVE_UNKNOWN
) continue;
362 sprintf(cStr
, procname_ide_model
, dent
->d_name
);
363 procfile
= fopen(cStr
, "r");
366 ERR("Could not open %s\n", cStr
);
369 case DRIVE_FIXED
: strcpy(cDevModel
, "Wine harddisk"); break;
370 case DRIVE_CDROM
: strcpy(cDevModel
, "Wine CDROM"); break;
373 fgets(cDevModel
, sizeof(cDevModel
), procfile
);
375 cDevModel
[strlen(cDevModel
) - 1] = 0;
378 sprintf(cUnixDeviceName
, "/dev/%s", dent
->d_name
);
379 scsi_addr
.PortNumber
= (dent
->d_name
[2] - 'a') / 2;
380 scsi_addr
.PathId
= 0;
381 scsi_addr
.TargetId
= (dent
->d_name
[2] - 'a') % 2;
383 if (scsi_addr
.PortNumber
+ 1 > uFirstSCSIPort
)
384 uFirstSCSIPort
= scsi_addr
.PortNumber
+ 1;
386 create_scsi_entry(&scsi_addr
, "atapi", nType
, cDevModel
, cUnixDeviceName
);
393 procfile
= fopen(procname_scsi
, "r");
396 TRACE("Could not open %s\n", procname_scsi
);
399 fgets(cStr
, 40, procfile
);
400 sscanf(cStr
, "Attached %9s %9s", read1
, read2
);
402 if (strcmp(read1
, "devices:") != 0)
404 WARN("Incorrect %s format\n", procname_scsi
);
408 if (strcmp(read2
, "none") == 0)
410 TRACE("No SCSI devices found in %s.\n", procname_scsi
);
415 /* Read info for one device */
416 while ((result
= SCSI_getprocentry(procfile
, &dev
)) > 0)
418 scsi_addr
.PortNumber
= dev
.host
;
419 scsi_addr
.PathId
= dev
.channel
;
420 scsi_addr
.TargetId
= dev
.target
;
421 scsi_addr
.Lun
= dev
.lun
;
423 scsi_addr
.PortNumber
+= uFirstSCSIPort
;
424 if (strncmp(dev
.type
, "Direct-Access", 13) == 0) nType
= DRIVE_FIXED
;
425 else if (strncmp(dev
.type
, "Sequential-Access", 17) == 0) nType
= DRIVE_REMOVABLE
;
426 else if (strncmp(dev
.type
, "CD-ROM", 6) == 0) nType
= DRIVE_CDROM
;
427 else if (strncmp(dev
.type
, "Processor", 9) == 0) nType
= DRIVE_NO_ROOT_DIR
;
430 strcpy(cDevModel
, dev
.vendor
);
431 strcat(cDevModel
, dev
.model
);
432 strcat(cDevModel
, dev
.rev
);
433 sprintf(cUnixDeviceName
, "/dev/sg%d", nSgNumber
++);
434 /* FIXME: get real driver name */
435 create_scsi_entry(&scsi_addr
, "WINE SCSI", nType
, cDevModel
, cUnixDeviceName
);
438 WARN("Incorrect %s format\n", procname_scsi
);
443 /***********************************************************************
446 void convert_old_config(void)
448 /* create some hardware keys (FIXME: should not be done here) */
449 create_hardware_branch();