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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 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>
36 #ifdef HAVE_SYS_IOCTL_H
37 #include <sys/ioctl.h>
39 #ifdef HAVE_LINUX_HDREG_H
40 # include <linux/hdreg.h>
43 #define NONAMELESSUNION
44 #define NONAMELESSSTRUCT
51 #include "wine/library.h"
52 #include "wine/server.h"
53 #include "wine/unicode.h"
54 #include "wine/debug.h"
55 #include "kernel_private.h"
57 WINE_DEFAULT_DEBUG_CHANNEL(reg);
60 /* registry initialisation, allocates some default keys. */
61 static ULONG allocate_default_keys(void)
63 static const WCHAR StatDataW[] = {'D','y','n','D','a','t','a','\\',
64 'P','e','r','f','S','t','a','t','s','\\',
65 'S','t','a','t','D','a','t','a',0};
66 static const WCHAR ConfigManagerW[] = {'D','y','n','D','a','t','a','\\',
67 'C','o','n','f','i','g',' ','M','a','n','a','g','e','r','\\',
71 OBJECT_ATTRIBUTES attr;
74 attr.Length = sizeof(attr);
75 attr.RootDirectory = 0;
76 attr.ObjectName = &nameW;
78 attr.SecurityDescriptor = NULL;
79 attr.SecurityQualityOfService = NULL;
81 RtlInitUnicodeString( &nameW, StatDataW );
82 if (!NtCreateKey( &hkey, KEY_ALL_ACCESS, &attr, 0, NULL, 0, &dispos )) NtClose( hkey );
83 if (dispos == REG_OPENED_EXISTING_KEY)
84 return dispos; /* someone else already loaded the registry */
86 RtlInitUnicodeString( &nameW, ConfigManagerW );
87 if (!NtCreateKey( &hkey, KEY_ALL_ACCESS, &attr, 0, NULL, 0, NULL )) NtClose( hkey );
92 /******************************************************************
95 * Initializes registry to contain scsi info about the cdrom in NT.
96 * All devices (even not real scsi ones) have this info in NT.
97 * NOTE: programs usually read these registry entries after sending the
98 * IOCTL_SCSI_GET_ADDRESS ioctl to the cdrom
100 static void create_scsi_entry( PSCSI_ADDRESS scsi_addr, LPSTR lpDriver, UINT uDriveType,
101 LPSTR lpDriveName, LPSTR lpUnixDeviceName )
103 static UCHAR uCdromNumber = 0;
104 static UCHAR uTapeNumber = 0;
106 OBJECT_ATTRIBUTES attr;
107 UNICODE_STRING nameW;
120 attr.Length = sizeof(attr);
121 attr.RootDirectory = 0;
122 attr.ObjectName = &nameW;
124 attr.SecurityDescriptor = NULL;
125 attr.SecurityQualityOfService = NULL;
127 /* Ensure there is Scsi key */
128 if (!RtlCreateUnicodeStringFromAsciiz( &nameW, "Machine\\HARDWARE\\DEVICEMAP\\Scsi" ) ||
129 NtCreateKey( &scsiKey, KEY_ALL_ACCESS, &attr, 0,
130 NULL, REG_OPTION_VOLATILE, &disp ))
132 ERR("Cannot create DEVICEMAP\\Scsi registry key\n" );
135 RtlFreeUnicodeString( &nameW );
137 snprintf(buffer,sizeof(buffer),"Scsi Port %d",scsi_addr->PortNumber);
138 attr.RootDirectory = scsiKey;
139 if (!RtlCreateUnicodeStringFromAsciiz( &nameW, buffer ) ||
140 NtCreateKey( &portKey, KEY_ALL_ACCESS, &attr, 0,
141 NULL, REG_OPTION_VOLATILE, &disp ))
143 ERR("Cannot create DEVICEMAP\\Scsi Port registry key\n" );
146 RtlFreeUnicodeString( &nameW );
148 RtlCreateUnicodeStringFromAsciiz( &nameW, "Driver" );
149 RtlMultiByteToUnicodeN( dataW, sizeof(dataW), &lenW, lpDriver, strlen(lpDriver));
150 NtSetValueKey( portKey, &nameW, 0, REG_SZ, (BYTE*)dataW, lenW );
151 RtlFreeUnicodeString( &nameW );
153 RtlCreateUnicodeStringFromAsciiz( &nameW, "FirstBusTimeScanInMs" );
154 NtSetValueKey( portKey,&nameW, 0, REG_DWORD, (BYTE *)&value, sizeof(DWORD));
155 RtlFreeUnicodeString( &nameW );
158 if (strcmp(lpDriver, "atapi") == 0)
163 fd = open(lpUnixDeviceName, O_RDONLY|O_NONBLOCK);
166 if (ioctl(fd, HDIO_GET_DMA, &dma) != -1) value = dma;
169 ERR("Can't open %s", buffer);
171 RtlCreateUnicodeStringFromAsciiz( &nameW, "DMAEnabled" );
172 NtSetValueKey( portKey,&nameW, 0, REG_DWORD, (BYTE *)&value, sizeof(DWORD));
173 RtlFreeUnicodeString( &nameW );
176 snprintf(buffer, sizeof(buffer),"Scsi Bus %d", scsi_addr->PathId);
177 attr.RootDirectory = portKey;
178 if (!RtlCreateUnicodeStringFromAsciiz( &nameW, buffer ) ||
179 NtCreateKey( &busKey, KEY_ALL_ACCESS, &attr, 0,
180 NULL, REG_OPTION_VOLATILE, &disp ))
182 ERR("Cannot create DEVICEMAP\\Scsi Port\\Scsi Bus registry key\n" );
185 RtlFreeUnicodeString( &nameW );
187 attr.RootDirectory = busKey;
188 /* FIXME: get real controller Id for SCSI */
189 if (!RtlCreateUnicodeStringFromAsciiz( &nameW, "Initiator Id 255" ) ||
190 NtCreateKey( &targetKey, KEY_ALL_ACCESS, &attr, 0,
191 NULL, REG_OPTION_VOLATILE, &disp ))
193 ERR("Cannot create DEVICEMAP\\Scsi Port\\Scsi Bus\\Initiator Id 255 registry key\n" );
196 RtlFreeUnicodeString( &nameW );
197 NtClose( targetKey );
199 snprintf(buffer, sizeof(buffer),"Target Id %d", scsi_addr->TargetId);
200 attr.RootDirectory = busKey;
201 if (!RtlCreateUnicodeStringFromAsciiz( &nameW, buffer ) ||
202 NtCreateKey( &targetKey, KEY_ALL_ACCESS, &attr, 0,
203 NULL, REG_OPTION_VOLATILE, &disp ))
205 ERR("Cannot create DEVICEMAP\\Scsi Port\\Scsi Bus 0\\Target Id registry key\n" );
208 RtlFreeUnicodeString( &nameW );
210 snprintf(buffer, sizeof(buffer),"Logical Unit Id %d", scsi_addr->Lun);
211 attr.RootDirectory = targetKey;
212 if (!RtlCreateUnicodeStringFromAsciiz( &nameW, buffer ) ||
213 NtCreateKey( &lunKey, KEY_ALL_ACCESS, &attr, 0,
214 NULL, REG_OPTION_VOLATILE, &disp ))
216 ERR("Cannot create DEVICEMAP\\Scsi Port\\Scsi Bus 0\\Target Id registry key\\Logical Unit Id\n" );
219 RtlFreeUnicodeString( &nameW );
223 case DRIVE_NO_ROOT_DIR:
225 data = "OtherPeripheral"; break;
227 data = "DiskPeripheral"; break;
228 case DRIVE_REMOVABLE:
229 data = "TapePeripheral";
230 snprintf(buffer, sizeof(buffer), "Tape%d", uTapeNumber++);
233 data = "CdRomPeripheral";
234 snprintf(buffer, sizeof(buffer), "Cdrom%d", uCdromNumber++);
237 RtlCreateUnicodeStringFromAsciiz( &nameW, "Type" );
238 RtlMultiByteToUnicodeN( dataW, sizeof(dataW), &lenW, data, strlen(data));
239 NtSetValueKey( lunKey, &nameW, 0, REG_SZ, (BYTE*)dataW, lenW );
240 RtlFreeUnicodeString( &nameW );
242 RtlCreateUnicodeStringFromAsciiz( &nameW, "Identifier" );
243 RtlMultiByteToUnicodeN( dataW, sizeof(dataW), &lenW, lpDriveName, strlen(lpDriveName));
244 NtSetValueKey( lunKey, &nameW, 0, REG_SZ, (BYTE*)dataW, lenW );
245 RtlFreeUnicodeString( &nameW );
247 if (uDriveType == DRIVE_CDROM || uDriveType == DRIVE_REMOVABLE)
249 RtlCreateUnicodeStringFromAsciiz( &nameW, "DeviceName" );
250 RtlMultiByteToUnicodeN( dataW, sizeof(dataW), &lenW, buffer, strlen(buffer));
251 NtSetValueKey( lunKey, &nameW, 0, REG_SZ, (BYTE*)dataW, lenW );
252 RtlFreeUnicodeString( &nameW );
255 RtlCreateUnicodeStringFromAsciiz( &nameW, "UnixDeviceName" );
256 RtlMultiByteToUnicodeN( dataW, sizeof(dataW), &lenW, lpUnixDeviceName, strlen(lpUnixDeviceName));
257 NtSetValueKey( lunKey, &nameW, 0, REG_SZ, (BYTE*)dataW, lenW );
258 RtlFreeUnicodeString( &nameW );
261 NtClose( targetKey );
267 struct LinuxProcScsiDevice
281 * we need to declare white spaces explicitly via %*1[ ],
282 * as there are very dainbread CD-ROM devices out there
283 * which have their manufacturer name blanked out via spaces,
284 * which confuses fscanf's parsing (skips all blank spaces)
286 static int SCSI_getprocentry( FILE * procfile, struct LinuxProcScsiDevice * dev )
290 result = fscanf( procfile,
291 "Host:%*1[ ]scsi%d%*1[ ]Channel:%*1[ ]%d%*1[ ]Id:%*1[ ]%d%*1[ ]Lun:%*1[ ]%d\n",
298 /* "end of entries" return, so no TRACE() here */
303 ERR("bus id line scan count error (fscanf returns %d, expected 4)\n", result);
306 result = fscanf( procfile,
307 " Vendor:%*1[ ]%8c%*1[ ]Model:%*1[ ]%16c%*1[ ]Rev:%*1[ ]%4c\n",
313 ERR("model line scan count error (fscanf returns %d, expected 3)\n", result);
317 result = fscanf( procfile,
318 " Type:%*3[ ]%32c%*1[ ]ANSI%*1[ ]SCSI%*1[ ]revision:%*1[ ]%x\n",
323 ERR("SCSI type line scan count error (fscanf returns %d, expected 2)\n", result);
326 /* Since we fscanf with %XXc instead of %s.. put a NULL at end */
336 /* create the hardware registry branch */
337 static void create_hardware_branch(void)
339 /* The following mostly will work on Linux, but should not cause
340 * problems on other systems. */
341 static const char procname_ide_media[] = "/proc/ide/%s/media";
342 static const char procname_ide_model[] = "/proc/ide/%s/model";
343 static const char procname_scsi[] = "/proc/scsi/scsi";
345 struct dirent *dent = NULL;
346 FILE *procfile = NULL;
347 char cStr[40], cDevModel[40], cUnixDeviceName[40], read1[10] = "\0", read2[10] = "\0";
348 SCSI_ADDRESS scsi_addr;
350 struct LinuxProcScsiDevice dev;
351 int result = 0, nSgNumber = 0;
352 UCHAR uFirstSCSIPort = 0;
354 /* Enumerate all ide devices first */
355 idedir = opendir("/proc/ide");
358 while ((dent = readdir(idedir)))
360 if (strncmp(dent->d_name, "hd", 2) == 0)
362 sprintf(cStr, procname_ide_media, dent->d_name);
363 procfile = fopen(cStr, "r");
366 ERR("Could not open %s\n", cStr);
369 fgets(cStr, sizeof(cStr), procfile);
371 nType = DRIVE_UNKNOWN;
372 if (strncasecmp(cStr, "disk", 4) == 0) nType = DRIVE_FIXED;
373 if (strncasecmp(cStr, "cdrom", 5) == 0) nType = DRIVE_CDROM;
375 if (nType == DRIVE_UNKNOWN) continue;
378 sprintf(cStr, procname_ide_model, dent->d_name);
379 procfile = fopen(cStr, "r");
382 ERR("Could not open %s\n", cStr);
385 case DRIVE_FIXED: strcpy(cDevModel, "Wine harddisk"); break;
386 case DRIVE_CDROM: strcpy(cDevModel, "Wine CDROM"); break;
389 fgets(cDevModel, sizeof(cDevModel), procfile);
391 cDevModel[strlen(cDevModel) - 1] = 0;
394 sprintf(cUnixDeviceName, "/dev/%s", dent->d_name);
395 scsi_addr.PortNumber = (dent->d_name[2] - 'a') / 2;
396 scsi_addr.PathId = 0;
397 scsi_addr.TargetId = (dent->d_name[2] - 'a') % 2;
399 if (scsi_addr.PortNumber + 1 > uFirstSCSIPort)
400 uFirstSCSIPort = scsi_addr.PortNumber + 1;
402 create_scsi_entry(&scsi_addr, "atapi", nType, cDevModel, cUnixDeviceName);
409 procfile = fopen(procname_scsi, "r");
412 TRACE("Could not open %s\n", procname_scsi);
415 fgets(cStr, 40, procfile);
416 sscanf(cStr, "Attached %9s %9s", read1, read2);
418 if (strcmp(read1, "devices:") != 0)
420 WARN("Incorrect %s format\n", procname_scsi);
424 if (strcmp(read2, "none") == 0)
426 TRACE("No SCSI devices found in %s.\n", procname_scsi);
431 /* Read info for one device */
432 while ((result = SCSI_getprocentry(procfile, &dev)) > 0)
434 scsi_addr.PortNumber = dev.host;
435 scsi_addr.PathId = dev.channel;
436 scsi_addr.TargetId = dev.target;
437 scsi_addr.Lun = dev.lun;
439 scsi_addr.PortNumber += uFirstSCSIPort;
440 if (strncmp(dev.type, "Direct-Access", 13) == 0) nType = DRIVE_FIXED;
441 else if (strncmp(dev.type, "Sequential-Access", 17) == 0) nType = DRIVE_REMOVABLE;
442 else if (strncmp(dev.type, "CD-ROM", 6) == 0) nType = DRIVE_CDROM;
443 else if (strncmp(dev.type, "Processor", 9) == 0) nType = DRIVE_NO_ROOT_DIR;
446 strcpy(cDevModel, dev.vendor);
447 strcat(cDevModel, dev.model);
448 strcat(cDevModel, dev.rev);
449 sprintf(cUnixDeviceName, "/dev/sg%d", nSgNumber++);
450 /* FIXME: get real driver name */
451 create_scsi_entry(&scsi_addr, "WINE SCSI", nType, cDevModel, cUnixDeviceName);
454 WARN("Incorrect %s format\n", procname_scsi);
459 /***********************************************************************
462 void convert_old_config(void)
464 if (allocate_default_keys() == REG_OPENED_EXISTING_KEY)
465 return; /* someone else already loaded the registry */
467 /* create some hardware keys (FIXME: should not be done here) */
468 create_hardware_branch();