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>
49 #include "wine/library.h"
50 #include "wine/unicode.h"
51 #include "wine/debug.h"
52 #include "kernel_private.h"
54 WINE_DEFAULT_DEBUG_CHANNEL(reg
);
56 static NTSTATUS
create_key( HANDLE root
, const char *name
, HANDLE
*key
, DWORD
*disp
)
58 OBJECT_ATTRIBUTES attr
;
62 attr
.Length
= sizeof(attr
);
63 attr
.RootDirectory
= root
;
64 attr
.ObjectName
= &nameW
;
66 attr
.SecurityDescriptor
= NULL
;
67 attr
.SecurityQualityOfService
= NULL
;
69 if (!RtlCreateUnicodeStringFromAsciiz( &nameW
, name
)) return STATUS_NO_MEMORY
;
70 status
= NtCreateKey( key
, KEY_ALL_ACCESS
, &attr
, 0, NULL
, REG_OPTION_VOLATILE
, disp
);
71 if (status
) ERR("Cannot create %s registry key\n", name
);
72 RtlFreeUnicodeString( &nameW
);
76 /******************************************************************
79 * Initializes registry to contain scsi info about the cdrom in NT.
80 * All devices (even not real scsi ones) have this info in NT.
81 * NOTE: programs usually read these registry entries after sending the
82 * IOCTL_SCSI_GET_ADDRESS ioctl to the cdrom
84 static void create_scsi_entry( PSCSI_ADDRESS scsi_addr
, LPCSTR lpDriver
, UINT uDriveType
,
85 LPSTR lpDriveName
, LPSTR lpUnixDeviceName
)
87 static UCHAR uCdromNumber
= 0;
88 static UCHAR uTapeNumber
= 0;
103 if (create_key( 0, "\\Registry\\Machine\\HARDWARE\\DEVICEMAP", &scsiKey
, &disp
)) return;
106 /* Ensure there is Scsi key */
107 if (create_key( 0, "\\Registry\\Machine\\HARDWARE\\DEVICEMAP\\Scsi", &scsiKey
, &disp
)) return;
109 snprintf(buffer
,sizeof(buffer
),"Scsi Port %d",scsi_addr
->PortNumber
);
110 if (create_key( scsiKey
, buffer
, &portKey
, &disp
)) return;
112 RtlCreateUnicodeStringFromAsciiz( &nameW
, "Driver" );
113 RtlMultiByteToUnicodeN( dataW
, sizeof(dataW
), &sizeW
, lpDriver
, strlen(lpDriver
)+1);
114 NtSetValueKey( portKey
, &nameW
, 0, REG_SZ
, dataW
, sizeW
);
115 RtlFreeUnicodeString( &nameW
);
117 RtlCreateUnicodeStringFromAsciiz( &nameW
, "FirstBusTimeScanInMs" );
118 NtSetValueKey( portKey
,&nameW
, 0, REG_DWORD
, &value
, sizeof(DWORD
) );
119 RtlFreeUnicodeString( &nameW
);
122 if (strcmp(lpDriver
, "atapi") == 0)
127 fd
= open(lpUnixDeviceName
, O_RDONLY
|O_NONBLOCK
);
130 if (ioctl(fd
, HDIO_GET_DMA
, &dma
) != -1) value
= dma
;
134 RtlCreateUnicodeStringFromAsciiz( &nameW
, "DMAEnabled" );
135 NtSetValueKey( portKey
,&nameW
, 0, REG_DWORD
, &value
, sizeof(DWORD
) );
136 RtlFreeUnicodeString( &nameW
);
139 snprintf(buffer
, sizeof(buffer
),"Scsi Bus %d", scsi_addr
->PathId
);
140 if (create_key( portKey
, buffer
, &busKey
, &disp
)) return;
142 /* FIXME: get real controller Id for SCSI */
143 if (create_key( busKey
, buffer
, &targetKey
, &disp
)) return;
144 NtClose( targetKey
);
146 snprintf(buffer
, sizeof(buffer
),"Target Id %d", scsi_addr
->TargetId
);
147 if (create_key( busKey
, buffer
, &targetKey
, &disp
)) return;
149 snprintf(buffer
, sizeof(buffer
),"Logical Unit Id %d", scsi_addr
->Lun
);
150 if (create_key( targetKey
, buffer
, &lunKey
, &disp
)) return;
154 case DRIVE_NO_ROOT_DIR
:
156 data
= "OtherPeripheral"; break;
158 data
= "DiskPeripheral"; break;
159 case DRIVE_REMOVABLE
:
160 data
= "TapePeripheral";
161 snprintf(buffer
, sizeof(buffer
), "Tape%d", uTapeNumber
++);
164 data
= "CdRomPeripheral";
165 snprintf(buffer
, sizeof(buffer
), "Cdrom%d", uCdromNumber
++);
168 RtlCreateUnicodeStringFromAsciiz( &nameW
, "Type" );
169 RtlMultiByteToUnicodeN( dataW
, sizeof(dataW
), &sizeW
, data
, strlen(data
)+1);
170 NtSetValueKey( lunKey
, &nameW
, 0, REG_SZ
, dataW
, sizeW
);
171 RtlFreeUnicodeString( &nameW
);
173 RtlCreateUnicodeStringFromAsciiz( &nameW
, "Identifier" );
174 RtlMultiByteToUnicodeN( dataW
, sizeof(dataW
), &sizeW
, lpDriveName
, strlen(lpDriveName
)+1);
175 NtSetValueKey( lunKey
, &nameW
, 0, REG_SZ
, dataW
, sizeW
);
176 RtlFreeUnicodeString( &nameW
);
178 if (uDriveType
== DRIVE_CDROM
|| uDriveType
== DRIVE_REMOVABLE
)
180 RtlCreateUnicodeStringFromAsciiz( &nameW
, "DeviceName" );
181 RtlMultiByteToUnicodeN( dataW
, sizeof(dataW
), &sizeW
, buffer
, strlen(buffer
)+1);
182 NtSetValueKey( lunKey
, &nameW
, 0, REG_SZ
, dataW
, sizeW
);
183 RtlFreeUnicodeString( &nameW
);
186 RtlCreateUnicodeStringFromAsciiz( &nameW
, "UnixDeviceName" );
187 RtlMultiByteToUnicodeN( dataW
, sizeof(dataW
), &sizeW
, lpUnixDeviceName
, strlen(lpUnixDeviceName
)+1);
188 NtSetValueKey( lunKey
, &nameW
, 0, REG_SZ
, dataW
, sizeW
);
189 RtlFreeUnicodeString( &nameW
);
192 NtClose( targetKey
);
198 struct LinuxProcScsiDevice
212 * we need to declare white spaces explicitly via %*1[ ],
213 * as there are very dainbread CD-ROM devices out there
214 * which have their manufacturer name blanked out via spaces,
215 * which confuses fscanf's parsing (skips all blank spaces)
217 static int SCSI_getprocentry( FILE * procfile
, struct LinuxProcScsiDevice
* dev
)
221 result
= fscanf( procfile
,
222 "Host:%*1[ ]scsi%d%*1[ ]Channel:%*1[ ]%d%*1[ ]Id:%*1[ ]%d%*1[ ]Lun:%*1[ ]%d\n",
229 /* "end of entries" return, so no TRACE() here */
234 ERR("bus id line scan count error (fscanf returns %d, expected 4)\n", result
);
237 result
= fscanf( procfile
,
238 " Vendor:%*1[ ]%8c%*1[ ]Model:%*1[ ]%16c%*1[ ]Rev:%*1[ ]%4c\n",
244 ERR("model line scan count error (fscanf returns %d, expected 3)\n", result
);
248 result
= fscanf( procfile
,
249 " Type:%*3[ ]%32c%*1[ ]ANSI SCSI%*1[ ]revision:%*1[ ]%x\n",
254 ERR("SCSI type line scan count error (fscanf returns %d, expected 2)\n", result
);
257 /* Since we fscanf with %XXc instead of %s.. put a NULL at end */
267 /* create the hardware registry branch */
268 static void create_hardware_branch(void)
270 /* The following mostly will work on Linux, but should not cause
271 * problems on other systems. */
272 static const char procname_ide_media
[] = "/proc/ide/%s/media";
273 static const char procname_ide_model
[] = "/proc/ide/%s/model";
274 static const char procname_scsi
[] = "/proc/scsi/scsi";
276 struct dirent
*dent
= NULL
;
277 FILE *procfile
= NULL
;
278 char cStr
[40], cDevModel
[40], cUnixDeviceName
[40], read1
[10] = "\0", read2
[10] = "\0";
279 SCSI_ADDRESS scsi_addr
;
281 struct LinuxProcScsiDevice dev
;
282 int result
= 0, nSgNumber
= 0;
283 UCHAR uFirstSCSIPort
= 0;
285 /* Enumerate all ide devices first */
286 idedir
= opendir("/proc/ide");
289 while ((dent
= readdir(idedir
)))
291 if (strncmp(dent
->d_name
, "hd", 2) == 0)
293 sprintf(cStr
, procname_ide_media
, dent
->d_name
);
294 procfile
= fopen(cStr
, "r");
297 ERR("Could not open %s\n", cStr
);
300 fgets(cStr
, sizeof(cStr
), procfile
);
302 nType
= DRIVE_UNKNOWN
;
303 if (strncasecmp(cStr
, "disk", 4) == 0) nType
= DRIVE_FIXED
;
304 if (strncasecmp(cStr
, "cdrom", 5) == 0) nType
= DRIVE_CDROM
;
306 if (nType
== DRIVE_UNKNOWN
) continue;
309 sprintf(cStr
, procname_ide_model
, dent
->d_name
);
310 procfile
= fopen(cStr
, "r");
313 ERR("Could not open %s\n", cStr
);
316 case DRIVE_FIXED
: strcpy(cDevModel
, "Wine harddisk"); break;
317 case DRIVE_CDROM
: strcpy(cDevModel
, "Wine CDROM"); break;
320 fgets(cDevModel
, sizeof(cDevModel
), procfile
);
322 cDevModel
[strlen(cDevModel
) - 1] = 0;
325 sprintf(cUnixDeviceName
, "/dev/%s", dent
->d_name
);
326 scsi_addr
.PortNumber
= (dent
->d_name
[2] - 'a') / 2;
327 scsi_addr
.PathId
= 0;
328 scsi_addr
.TargetId
= (dent
->d_name
[2] - 'a') % 2;
330 if (scsi_addr
.PortNumber
+ 1 > uFirstSCSIPort
)
331 uFirstSCSIPort
= scsi_addr
.PortNumber
+ 1;
333 create_scsi_entry(&scsi_addr
, "atapi", nType
, cDevModel
, cUnixDeviceName
);
340 procfile
= fopen(procname_scsi
, "r");
343 TRACE("Could not open %s\n", procname_scsi
);
346 fgets(cStr
, 40, procfile
);
347 sscanf(cStr
, "Attached %9s %9s", read1
, read2
);
349 if (strcmp(read1
, "devices:") != 0)
351 WARN("Incorrect %s format\n", procname_scsi
);
355 if (strcmp(read2
, "none") == 0)
357 TRACE("No SCSI devices found in %s.\n", procname_scsi
);
362 /* Read info for one device */
363 while ((result
= SCSI_getprocentry(procfile
, &dev
)) > 0)
365 scsi_addr
.PortNumber
= dev
.host
;
366 scsi_addr
.PathId
= dev
.channel
;
367 scsi_addr
.TargetId
= dev
.target
;
368 scsi_addr
.Lun
= dev
.lun
;
370 scsi_addr
.PortNumber
+= uFirstSCSIPort
;
371 if (strncmp(dev
.type
, "Direct-Access", 13) == 0) nType
= DRIVE_FIXED
;
372 else if (strncmp(dev
.type
, "Sequential-Access", 17) == 0) nType
= DRIVE_REMOVABLE
;
373 else if (strncmp(dev
.type
, "CD-ROM", 6) == 0) nType
= DRIVE_CDROM
;
374 else if (strncmp(dev
.type
, "Processor", 9) == 0) nType
= DRIVE_NO_ROOT_DIR
;
375 else if (strncmp(dev
.type
, "Scanner", 7) == 0) nType
= DRIVE_NO_ROOT_DIR
;
376 else if (strncmp(dev
.type
, "Printer", 7) == 0) nType
= DRIVE_NO_ROOT_DIR
;
379 strcpy(cDevModel
, dev
.vendor
);
380 strcat(cDevModel
, dev
.model
);
381 strcat(cDevModel
, dev
.rev
);
382 sprintf(cUnixDeviceName
, "/dev/sg%d", nSgNumber
++);
383 /* FIXME: get real driver name */
384 create_scsi_entry(&scsi_addr
, "WINE SCSI", nType
, cDevModel
, cUnixDeviceName
);
387 WARN("Incorrect %s format\n", procname_scsi
);
392 /***********************************************************************
395 void convert_old_config(void)
400 /* create some hardware keys (FIXME: should not be done here) */
401 if (create_key( 0, "\\Registry\\Machine\\HARDWARE", &key
, &disp
)) return;
403 if (disp
!= REG_OPENED_EXISTING_KEY
) create_hardware_branch();