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 static NTSTATUS create_key( HANDLE root, const char *name, HANDLE *key, DWORD *disp )
60 OBJECT_ATTRIBUTES attr;
64 attr.Length = sizeof(attr);
65 attr.RootDirectory = root;
66 attr.ObjectName = &nameW;
68 attr.SecurityDescriptor = NULL;
69 attr.SecurityQualityOfService = NULL;
71 if (!RtlCreateUnicodeStringFromAsciiz( &nameW, name )) return STATUS_NO_MEMORY;
72 status = NtCreateKey( key, KEY_ALL_ACCESS, &attr, 0, NULL, REG_OPTION_VOLATILE, disp );
73 if (status) ERR("Cannot create %s registry key\n", name );
74 RtlFreeUnicodeString( &nameW );
78 /******************************************************************
81 * Initializes registry to contain scsi info about the cdrom in NT.
82 * All devices (even not real scsi ones) have this info in NT.
83 * NOTE: programs usually read these registry entries after sending the
84 * IOCTL_SCSI_GET_ADDRESS ioctl to the cdrom
86 static void create_scsi_entry( PSCSI_ADDRESS scsi_addr, LPCSTR lpDriver, UINT uDriveType,
87 LPSTR lpDriveName, LPSTR lpUnixDeviceName )
89 static UCHAR uCdromNumber = 0;
90 static UCHAR uTapeNumber = 0;
105 if (create_key( 0, "Machine\\HARDWARE\\DEVICEMAP", &scsiKey, &disp )) return;
108 /* Ensure there is Scsi key */
109 if (create_key( 0, "Machine\\HARDWARE\\DEVICEMAP\\Scsi", &scsiKey, &disp )) return;
111 snprintf(buffer,sizeof(buffer),"Scsi Port %d",scsi_addr->PortNumber);
112 if (create_key( scsiKey, buffer, &portKey, &disp )) return;
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 if (create_key( portKey, buffer, &busKey, &disp )) return;
144 /* FIXME: get real controller Id for SCSI */
145 if (create_key( busKey, buffer, &targetKey, &disp )) return;
146 NtClose( targetKey );
148 snprintf(buffer, sizeof(buffer),"Target Id %d", scsi_addr->TargetId);
149 if (create_key( busKey, buffer, &targetKey, &disp )) return;
151 snprintf(buffer, sizeof(buffer),"Logical Unit Id %d", scsi_addr->Lun);
152 if (create_key( targetKey, buffer, &lunKey, &disp )) return;
156 case DRIVE_NO_ROOT_DIR:
158 data = "OtherPeripheral"; break;
160 data = "DiskPeripheral"; break;
161 case DRIVE_REMOVABLE:
162 data = "TapePeripheral";
163 snprintf(buffer, sizeof(buffer), "Tape%d", uTapeNumber++);
166 data = "CdRomPeripheral";
167 snprintf(buffer, sizeof(buffer), "Cdrom%d", uCdromNumber++);
170 RtlCreateUnicodeStringFromAsciiz( &nameW, "Type" );
171 RtlMultiByteToUnicodeN( dataW, sizeof(dataW), &sizeW, data, strlen(data)+1);
172 NtSetValueKey( lunKey, &nameW, 0, REG_SZ, dataW, sizeW );
173 RtlFreeUnicodeString( &nameW );
175 RtlCreateUnicodeStringFromAsciiz( &nameW, "Identifier" );
176 RtlMultiByteToUnicodeN( dataW, sizeof(dataW), &sizeW, lpDriveName, strlen(lpDriveName)+1);
177 NtSetValueKey( lunKey, &nameW, 0, REG_SZ, dataW, sizeW );
178 RtlFreeUnicodeString( &nameW );
180 if (uDriveType == DRIVE_CDROM || uDriveType == DRIVE_REMOVABLE)
182 RtlCreateUnicodeStringFromAsciiz( &nameW, "DeviceName" );
183 RtlMultiByteToUnicodeN( dataW, sizeof(dataW), &sizeW, buffer, strlen(buffer)+1);
184 NtSetValueKey( lunKey, &nameW, 0, REG_SZ, dataW, sizeW );
185 RtlFreeUnicodeString( &nameW );
188 RtlCreateUnicodeStringFromAsciiz( &nameW, "UnixDeviceName" );
189 RtlMultiByteToUnicodeN( dataW, sizeof(dataW), &sizeW, lpUnixDeviceName, strlen(lpUnixDeviceName)+1);
190 NtSetValueKey( lunKey, &nameW, 0, REG_SZ, dataW, sizeW );
191 RtlFreeUnicodeString( &nameW );
194 NtClose( targetKey );
200 struct LinuxProcScsiDevice
214 * we need to declare white spaces explicitly via %*1[ ],
215 * as there are very dainbread CD-ROM devices out there
216 * which have their manufacturer name blanked out via spaces,
217 * which confuses fscanf's parsing (skips all blank spaces)
219 static int SCSI_getprocentry( FILE * procfile, struct LinuxProcScsiDevice * dev )
223 result = fscanf( procfile,
224 "Host:%*1[ ]scsi%d%*1[ ]Channel:%*1[ ]%d%*1[ ]Id:%*1[ ]%d%*1[ ]Lun:%*1[ ]%d\n",
231 /* "end of entries" return, so no TRACE() here */
236 ERR("bus id line scan count error (fscanf returns %d, expected 4)\n", result);
239 result = fscanf( procfile,
240 " Vendor:%*1[ ]%8c%*1[ ]Model:%*1[ ]%16c%*1[ ]Rev:%*1[ ]%4c\n",
246 ERR("model line scan count error (fscanf returns %d, expected 3)\n", result);
250 result = fscanf( procfile,
251 " Type:%*3[ ]%32c%*1[ ]ANSI SCSI%*1[ ]revision:%*1[ ]%x\n",
256 ERR("SCSI type line scan count error (fscanf returns %d, expected 2)\n", result);
259 /* Since we fscanf with %XXc instead of %s.. put a NULL at end */
269 /* create the hardware registry branch */
270 static void create_hardware_branch(void)
272 /* The following mostly will work on Linux, but should not cause
273 * problems on other systems. */
274 static const char procname_ide_media[] = "/proc/ide/%s/media";
275 static const char procname_ide_model[] = "/proc/ide/%s/model";
276 static const char procname_scsi[] = "/proc/scsi/scsi";
278 struct dirent *dent = NULL;
279 FILE *procfile = NULL;
280 char cStr[40], cDevModel[40], cUnixDeviceName[40], read1[10] = "\0", read2[10] = "\0";
281 SCSI_ADDRESS scsi_addr;
283 struct LinuxProcScsiDevice dev;
284 int result = 0, nSgNumber = 0;
285 UCHAR uFirstSCSIPort = 0;
287 /* Enumerate all ide devices first */
288 idedir = opendir("/proc/ide");
291 while ((dent = readdir(idedir)))
293 if (strncmp(dent->d_name, "hd", 2) == 0)
295 sprintf(cStr, procname_ide_media, dent->d_name);
296 procfile = fopen(cStr, "r");
299 ERR("Could not open %s\n", cStr);
302 fgets(cStr, sizeof(cStr), procfile);
304 nType = DRIVE_UNKNOWN;
305 if (strncasecmp(cStr, "disk", 4) == 0) nType = DRIVE_FIXED;
306 if (strncasecmp(cStr, "cdrom", 5) == 0) nType = DRIVE_CDROM;
308 if (nType == DRIVE_UNKNOWN) continue;
311 sprintf(cStr, procname_ide_model, dent->d_name);
312 procfile = fopen(cStr, "r");
315 ERR("Could not open %s\n", cStr);
318 case DRIVE_FIXED: strcpy(cDevModel, "Wine harddisk"); break;
319 case DRIVE_CDROM: strcpy(cDevModel, "Wine CDROM"); break;
322 fgets(cDevModel, sizeof(cDevModel), procfile);
324 cDevModel[strlen(cDevModel) - 1] = 0;
327 sprintf(cUnixDeviceName, "/dev/%s", dent->d_name);
328 scsi_addr.PortNumber = (dent->d_name[2] - 'a') / 2;
329 scsi_addr.PathId = 0;
330 scsi_addr.TargetId = (dent->d_name[2] - 'a') % 2;
332 if (scsi_addr.PortNumber + 1 > uFirstSCSIPort)
333 uFirstSCSIPort = scsi_addr.PortNumber + 1;
335 create_scsi_entry(&scsi_addr, "atapi", nType, cDevModel, cUnixDeviceName);
342 procfile = fopen(procname_scsi, "r");
345 TRACE("Could not open %s\n", procname_scsi);
348 fgets(cStr, 40, procfile);
349 sscanf(cStr, "Attached %9s %9s", read1, read2);
351 if (strcmp(read1, "devices:") != 0)
353 WARN("Incorrect %s format\n", procname_scsi);
357 if (strcmp(read2, "none") == 0)
359 TRACE("No SCSI devices found in %s.\n", procname_scsi);
364 /* Read info for one device */
365 while ((result = SCSI_getprocentry(procfile, &dev)) > 0)
367 scsi_addr.PortNumber = dev.host;
368 scsi_addr.PathId = dev.channel;
369 scsi_addr.TargetId = dev.target;
370 scsi_addr.Lun = dev.lun;
372 scsi_addr.PortNumber += uFirstSCSIPort;
373 if (strncmp(dev.type, "Direct-Access", 13) == 0) nType = DRIVE_FIXED;
374 else if (strncmp(dev.type, "Sequential-Access", 17) == 0) nType = DRIVE_REMOVABLE;
375 else if (strncmp(dev.type, "CD-ROM", 6) == 0) nType = DRIVE_CDROM;
376 else if (strncmp(dev.type, "Processor", 9) == 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, "Machine\\HARDWARE", &key, &disp )) return;
403 if (disp != REG_OPENED_EXISTING_KEY) create_hardware_branch();