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 /* 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 ))
98 ERR("Cannot create DEVICEMAP\\Scsi registry key\n" );
101 RtlFreeUnicodeString( &nameW );
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 ))
109 ERR("Cannot create DEVICEMAP\\Scsi Port registry key\n" );
112 RtlFreeUnicodeString( &nameW );
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 );
119 RtlCreateUnicodeStringFromAsciiz( &nameW, "FirstBusTimeScanInMs" );
120 NtSetValueKey( portKey,&nameW, 0, REG_DWORD, &value, sizeof(DWORD) );
121 RtlFreeUnicodeString( &nameW );
124 if (strcmp(lpDriver, "atapi") == 0)
129 fd = open(lpUnixDeviceName, O_RDONLY|O_NONBLOCK);
132 if (ioctl(fd, HDIO_GET_DMA, &dma) != -1) value = dma;
136 RtlCreateUnicodeStringFromAsciiz( &nameW, "DMAEnabled" );
137 NtSetValueKey( portKey,&nameW, 0, REG_DWORD, &value, sizeof(DWORD) );
138 RtlFreeUnicodeString( &nameW );
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 ))
147 ERR("Cannot create DEVICEMAP\\Scsi Port\\Scsi Bus registry key\n" );
150 RtlFreeUnicodeString( &nameW );
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 ))
158 ERR("Cannot create DEVICEMAP\\Scsi Port\\Scsi Bus\\Initiator Id 255 registry key\n" );
161 RtlFreeUnicodeString( &nameW );
162 NtClose( targetKey );
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 ))
170 ERR("Cannot create DEVICEMAP\\Scsi Port\\Scsi Bus 0\\Target Id registry key\n" );
173 RtlFreeUnicodeString( &nameW );
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 ))
181 ERR("Cannot create DEVICEMAP\\Scsi Port\\Scsi Bus 0\\Target Id registry key\\Logical Unit Id\n" );
184 RtlFreeUnicodeString( &nameW );
188 case DRIVE_NO_ROOT_DIR:
190 data = "OtherPeripheral"; break;
192 data = "DiskPeripheral"; break;
193 case DRIVE_REMOVABLE:
194 data = "TapePeripheral";
195 snprintf(buffer, sizeof(buffer), "Tape%d", uTapeNumber++);
198 data = "CdRomPeripheral";
199 snprintf(buffer, sizeof(buffer), "Cdrom%d", uCdromNumber++);
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 );
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 );
212 if (uDriveType == DRIVE_CDROM || uDriveType == DRIVE_REMOVABLE)
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 );
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 );
226 NtClose( targetKey );
232 struct LinuxProcScsiDevice
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)
251 static int SCSI_getprocentry( FILE * procfile, struct LinuxProcScsiDevice * dev )
255 result = fscanf( procfile,
256 "Host:%*1[ ]scsi%d%*1[ ]Channel:%*1[ ]%d%*1[ ]Id:%*1[ ]%d%*1[ ]Lun:%*1[ ]%d\n",
263 /* "end of entries" return, so no TRACE() here */
268 ERR("bus id line scan count error (fscanf returns %d, expected 4)\n", result);
271 result = fscanf( procfile,
272 " Vendor:%*1[ ]%8c%*1[ ]Model:%*1[ ]%16c%*1[ ]Rev:%*1[ ]%4c\n",
278 ERR("model line scan count error (fscanf returns %d, expected 3)\n", result);
282 result = fscanf( procfile,
283 " Type:%*3[ ]%32c%*1[ ]ANSI SCSI%*1[ ]revision:%*1[ ]%x\n",
288 ERR("SCSI type line scan count error (fscanf returns %d, expected 2)\n", result);
291 /* Since we fscanf with %XXc instead of %s.. put a NULL at end */
301 /* create the hardware registry branch */
302 static void create_hardware_branch(void)
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";
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;
315 struct LinuxProcScsiDevice dev;
316 int result = 0, nSgNumber = 0;
317 UCHAR uFirstSCSIPort = 0;
319 /* Enumerate all ide devices first */
320 idedir = opendir("/proc/ide");
323 while ((dent = readdir(idedir)))
325 if (strncmp(dent->d_name, "hd", 2) == 0)
327 sprintf(cStr, procname_ide_media, dent->d_name);
328 procfile = fopen(cStr, "r");
331 ERR("Could not open %s\n", cStr);
334 fgets(cStr, sizeof(cStr), 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;
340 if (nType == DRIVE_UNKNOWN) continue;
343 sprintf(cStr, procname_ide_model, dent->d_name);
344 procfile = fopen(cStr, "r");
347 ERR("Could not open %s\n", cStr);
350 case DRIVE_FIXED: strcpy(cDevModel, "Wine harddisk"); break;
351 case DRIVE_CDROM: strcpy(cDevModel, "Wine CDROM"); break;
354 fgets(cDevModel, sizeof(cDevModel), procfile);
356 cDevModel[strlen(cDevModel) - 1] = 0;
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;
364 if (scsi_addr.PortNumber + 1 > uFirstSCSIPort)
365 uFirstSCSIPort = scsi_addr.PortNumber + 1;
367 create_scsi_entry(&scsi_addr, "atapi", nType, cDevModel, cUnixDeviceName);
374 procfile = fopen(procname_scsi, "r");
377 TRACE("Could not open %s\n", procname_scsi);
380 fgets(cStr, 40, procfile);
381 sscanf(cStr, "Attached %9s %9s", read1, read2);
383 if (strcmp(read1, "devices:") != 0)
385 WARN("Incorrect %s format\n", procname_scsi);
389 if (strcmp(read2, "none") == 0)
391 TRACE("No SCSI devices found in %s.\n", procname_scsi);
396 /* Read info for one device */
397 while ((result = SCSI_getprocentry(procfile, &dev)) > 0)
399 scsi_addr.PortNumber = dev.host;
400 scsi_addr.PathId = dev.channel;
401 scsi_addr.TargetId = dev.target;
402 scsi_addr.Lun = dev.lun;
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;
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);
419 WARN("Incorrect %s format\n", procname_scsi);
424 /***********************************************************************
427 void convert_old_config(void)
429 /* create some hardware keys (FIXME: should not be done here) */
430 create_hardware_branch();