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
52 #include "wine/library.h"
53 #include "wine/server.h"
54 #include "wine/unicode.h"
55 #include "wine/debug.h"
56 #include "kernel_private.h"
58 WINE_DEFAULT_DEBUG_CHANNEL(reg
);
61 /* registry initialisation, allocates some default keys. */
62 static ULONG
allocate_default_keys(void)
64 static const WCHAR StatDataW
[] = {'D','y','n','D','a','t','a','\\',
65 'P','e','r','f','S','t','a','t','s','\\',
66 'S','t','a','t','D','a','t','a',0};
67 static const WCHAR ConfigManagerW
[] = {'D','y','n','D','a','t','a','\\',
68 'C','o','n','f','i','g',' ','M','a','n','a','g','e','r','\\',
72 OBJECT_ATTRIBUTES attr
;
75 attr
.Length
= sizeof(attr
);
76 attr
.RootDirectory
= 0;
77 attr
.ObjectName
= &nameW
;
79 attr
.SecurityDescriptor
= NULL
;
80 attr
.SecurityQualityOfService
= NULL
;
82 RtlInitUnicodeString( &nameW
, StatDataW
);
83 if (!NtCreateKey( &hkey
, KEY_ALL_ACCESS
, &attr
, 0, NULL
, 0, &dispos
)) NtClose( hkey
);
84 if (dispos
== REG_OPENED_EXISTING_KEY
)
85 return dispos
; /* someone else already loaded the registry */
87 RtlInitUnicodeString( &nameW
, ConfigManagerW
);
88 if (!NtCreateKey( &hkey
, KEY_ALL_ACCESS
, &attr
, 0, NULL
, 0, NULL
)) NtClose( hkey
);
93 /******************************************************************
96 * Initializes registry to contain scsi info about the cdrom in NT.
97 * All devices (even not real scsi ones) have this info in NT.
98 * NOTE: programs usually read these registry entries after sending the
99 * IOCTL_SCSI_GET_ADDRESS ioctl to the cdrom
101 static void create_scsi_entry( PSCSI_ADDRESS scsi_addr
, LPSTR lpDriver
, UINT uDriveType
,
102 LPSTR lpDriveName
, LPSTR lpUnixDeviceName
)
104 static UCHAR uCdromNumber
= 0;
105 static UCHAR uTapeNumber
= 0;
107 OBJECT_ATTRIBUTES attr
;
108 UNICODE_STRING nameW
;
121 attr
.Length
= sizeof(attr
);
122 attr
.RootDirectory
= 0;
123 attr
.ObjectName
= &nameW
;
125 attr
.SecurityDescriptor
= NULL
;
126 attr
.SecurityQualityOfService
= NULL
;
128 /* Ensure there is Scsi key */
129 if (!RtlCreateUnicodeStringFromAsciiz( &nameW
, "Machine\\HARDWARE\\DEVICEMAP\\Scsi" ) ||
130 NtCreateKey( &scsiKey
, KEY_ALL_ACCESS
, &attr
, 0,
131 NULL
, REG_OPTION_VOLATILE
, &disp
))
133 ERR("Cannot create DEVICEMAP\\Scsi registry key\n" );
136 RtlFreeUnicodeString( &nameW
);
138 snprintf(buffer
,sizeof(buffer
),"Scsi Port %d",scsi_addr
->PortNumber
);
139 attr
.RootDirectory
= scsiKey
;
140 if (!RtlCreateUnicodeStringFromAsciiz( &nameW
, buffer
) ||
141 NtCreateKey( &portKey
, KEY_ALL_ACCESS
, &attr
, 0,
142 NULL
, REG_OPTION_VOLATILE
, &disp
))
144 ERR("Cannot create DEVICEMAP\\Scsi Port registry key\n" );
147 RtlFreeUnicodeString( &nameW
);
149 RtlCreateUnicodeStringFromAsciiz( &nameW
, "Driver" );
150 RtlMultiByteToUnicodeN( dataW
, sizeof(dataW
), &lenW
, lpDriver
, strlen(lpDriver
));
151 NtSetValueKey( portKey
, &nameW
, 0, REG_SZ
, (BYTE
*)dataW
, lenW
);
152 RtlFreeUnicodeString( &nameW
);
154 RtlCreateUnicodeStringFromAsciiz( &nameW
, "FirstBusTimeScanInMs" );
155 NtSetValueKey( portKey
,&nameW
, 0, REG_DWORD
, (BYTE
*)&value
, sizeof(DWORD
));
156 RtlFreeUnicodeString( &nameW
);
159 if (strcmp(lpDriver
, "atapi") == 0)
164 fd
= open(lpUnixDeviceName
, O_RDONLY
|O_NONBLOCK
);
167 if (ioctl(fd
, HDIO_GET_DMA
, &dma
) != -1) value
= dma
;
170 ERR("Can't open %s", buffer
);
172 RtlCreateUnicodeStringFromAsciiz( &nameW
, "DMAEnabled" );
173 NtSetValueKey( portKey
,&nameW
, 0, REG_DWORD
, (BYTE
*)&value
, sizeof(DWORD
));
174 RtlFreeUnicodeString( &nameW
);
177 snprintf(buffer
, sizeof(buffer
),"Scsi Bus %d", scsi_addr
->PathId
);
178 attr
.RootDirectory
= portKey
;
179 if (!RtlCreateUnicodeStringFromAsciiz( &nameW
, buffer
) ||
180 NtCreateKey( &busKey
, KEY_ALL_ACCESS
, &attr
, 0,
181 NULL
, REG_OPTION_VOLATILE
, &disp
))
183 ERR("Cannot create DEVICEMAP\\Scsi Port\\Scsi Bus registry key\n" );
186 RtlFreeUnicodeString( &nameW
);
188 attr
.RootDirectory
= busKey
;
189 /* FIXME: get real controller Id for SCSI */
190 if (!RtlCreateUnicodeStringFromAsciiz( &nameW
, "Initiator Id 255" ) ||
191 NtCreateKey( &targetKey
, KEY_ALL_ACCESS
, &attr
, 0,
192 NULL
, REG_OPTION_VOLATILE
, &disp
))
194 ERR("Cannot create DEVICEMAP\\Scsi Port\\Scsi Bus\\Initiator Id 255 registry key\n" );
197 RtlFreeUnicodeString( &nameW
);
198 NtClose( targetKey
);
200 snprintf(buffer
, sizeof(buffer
),"Target Id %d", scsi_addr
->TargetId
);
201 attr
.RootDirectory
= busKey
;
202 if (!RtlCreateUnicodeStringFromAsciiz( &nameW
, buffer
) ||
203 NtCreateKey( &targetKey
, KEY_ALL_ACCESS
, &attr
, 0,
204 NULL
, REG_OPTION_VOLATILE
, &disp
))
206 ERR("Cannot create DEVICEMAP\\Scsi Port\\Scsi Bus 0\\Target Id registry key\n" );
209 RtlFreeUnicodeString( &nameW
);
211 snprintf(buffer
, sizeof(buffer
),"Logical Unit Id %d", scsi_addr
->Lun
);
212 attr
.RootDirectory
= targetKey
;
213 if (!RtlCreateUnicodeStringFromAsciiz( &nameW
, buffer
) ||
214 NtCreateKey( &lunKey
, KEY_ALL_ACCESS
, &attr
, 0,
215 NULL
, REG_OPTION_VOLATILE
, &disp
))
217 ERR("Cannot create DEVICEMAP\\Scsi Port\\Scsi Bus 0\\Target Id registry key\\Logical Unit Id\n" );
220 RtlFreeUnicodeString( &nameW
);
224 case DRIVE_NO_ROOT_DIR
:
226 data
= "OtherPeripheral"; break;
228 data
= "DiskPeripheral"; break;
229 case DRIVE_REMOVABLE
:
230 data
= "TapePeripheral";
231 snprintf(buffer
, sizeof(buffer
), "Tape%d", uTapeNumber
++);
234 data
= "CdRomPeripheral";
235 snprintf(buffer
, sizeof(buffer
), "Cdrom%d", uCdromNumber
++);
238 RtlCreateUnicodeStringFromAsciiz( &nameW
, "Type" );
239 RtlMultiByteToUnicodeN( dataW
, sizeof(dataW
), &lenW
, data
, strlen(data
));
240 NtSetValueKey( lunKey
, &nameW
, 0, REG_SZ
, (BYTE
*)dataW
, lenW
);
241 RtlFreeUnicodeString( &nameW
);
243 RtlCreateUnicodeStringFromAsciiz( &nameW
, "Identifier" );
244 RtlMultiByteToUnicodeN( dataW
, sizeof(dataW
), &lenW
, lpDriveName
, strlen(lpDriveName
));
245 NtSetValueKey( lunKey
, &nameW
, 0, REG_SZ
, (BYTE
*)dataW
, lenW
);
246 RtlFreeUnicodeString( &nameW
);
248 if (uDriveType
== DRIVE_CDROM
|| uDriveType
== DRIVE_REMOVABLE
)
250 RtlCreateUnicodeStringFromAsciiz( &nameW
, "DeviceName" );
251 RtlMultiByteToUnicodeN( dataW
, sizeof(dataW
), &lenW
, buffer
, strlen(buffer
));
252 NtSetValueKey( lunKey
, &nameW
, 0, REG_SZ
, (BYTE
*)dataW
, lenW
);
253 RtlFreeUnicodeString( &nameW
);
256 RtlCreateUnicodeStringFromAsciiz( &nameW
, "UnixDeviceName" );
257 RtlMultiByteToUnicodeN( dataW
, sizeof(dataW
), &lenW
, lpUnixDeviceName
, strlen(lpUnixDeviceName
));
258 NtSetValueKey( lunKey
, &nameW
, 0, REG_SZ
, (BYTE
*)dataW
, lenW
);
259 RtlFreeUnicodeString( &nameW
);
262 NtClose( targetKey
);
268 struct LinuxProcScsiDevice
282 * we need to declare white spaces explicitly via %*1[ ],
283 * as there are very dainbread CD-ROM devices out there
284 * which have their manufacturer name blanked out via spaces,
285 * which confuses fscanf's parsing (skips all blank spaces)
287 static int SCSI_getprocentry( FILE * procfile
, struct LinuxProcScsiDevice
* dev
)
291 result
= fscanf( procfile
,
292 "Host:%*1[ ]scsi%d%*1[ ]Channel:%*1[ ]%d%*1[ ]Id:%*1[ ]%d%*1[ ]Lun:%*1[ ]%d\n",
299 /* "end of entries" return, so no TRACE() here */
304 ERR("bus id line scan count error\n");
307 result
= fscanf( procfile
,
308 " Vendor:%*1[ ]%8c%*1[ ]Model:%*1[ ]%16c%*1[ ]Rev:%*1[ ]%4c\n",
314 ERR("model line scan count error\n");
318 result
= fscanf( procfile
,
319 " Type:%*3[ ]%32c%*1[ ]ANSI%*1[ ]SCSI%*1[ ]revision:%*1[ ]%d\n",
324 ERR("SCSI type line scan count error\n");
327 /* Since we fscanf with %XXc instead of %s.. put a NULL at end */
337 /* create the hardware registry branch */
338 static void create_hardware_branch(void)
340 /* The following mostly will work on Linux, but should not cause
341 * problems on other systems. */
342 static const char procname_ide_media
[] = "/proc/ide/%s/media";
343 static const char procname_ide_model
[] = "/proc/ide/%s/model";
344 static const char procname_scsi
[] = "/proc/scsi/scsi";
346 struct dirent
*dent
= NULL
;
347 FILE *procfile
= NULL
;
348 char cStr
[40], cDevModel
[40], cUnixDeviceName
[40], read1
[10] = "\0", read2
[10] = "\0";
349 SCSI_ADDRESS scsi_addr
;
351 struct LinuxProcScsiDevice dev
;
352 int result
= 0, nSgNumber
= 0;
353 UCHAR uFirstSCSIPort
= 0;
355 /* Enumerate all ide devices first */
356 idedir
= opendir("/proc/ide");
359 while ((dent
= readdir(idedir
)))
361 if (strncmp(dent
->d_name
, "hd", 2) == 0)
363 sprintf(cStr
, procname_ide_media
, dent
->d_name
);
364 procfile
= fopen(cStr
, "r");
367 ERR("Could not open %s\n", cStr
);
370 fgets(cStr
, sizeof(cStr
), procfile
);
372 nType
= DRIVE_UNKNOWN
;
373 if (strncasecmp(cStr
, "disk", 4) == 0) nType
= DRIVE_FIXED
;
374 if (strncasecmp(cStr
, "cdrom", 5) == 0) nType
= DRIVE_CDROM
;
376 if (nType
== DRIVE_UNKNOWN
) continue;
379 sprintf(cStr
, procname_ide_model
, dent
->d_name
);
380 procfile
= fopen(cStr
, "r");
383 ERR("Could not open %s\n", cStr
);
386 case DRIVE_FIXED
: strcpy(cDevModel
, "Wine harddisk"); break;
387 case DRIVE_CDROM
: strcpy(cDevModel
, "Wine CDROM"); break;
390 fgets(cDevModel
, sizeof(cDevModel
), procfile
);
392 cDevModel
[strlen(cDevModel
) - 1] = 0;
395 sprintf(cUnixDeviceName
, "/dev/%s", dent
->d_name
);
396 scsi_addr
.PortNumber
= (dent
->d_name
[2] - 'a') / 2;
397 scsi_addr
.PathId
= 0;
398 scsi_addr
.TargetId
= (dent
->d_name
[2] - 'a') % 2;
400 if (scsi_addr
.PortNumber
+ 1 > uFirstSCSIPort
)
401 uFirstSCSIPort
= scsi_addr
.PortNumber
+ 1;
403 create_scsi_entry(&scsi_addr
, "atapi", nType
, cDevModel
, cUnixDeviceName
);
410 procfile
= fopen(procname_scsi
, "r");
413 TRACE("Could not open %s\n", procname_scsi
);
416 fgets(cStr
, 40, procfile
);
417 sscanf(cStr
, "Attached %9s %9s", read1
, read2
);
419 if (strcmp(read1
, "devices:") != 0)
421 WARN("Incorrect %s format\n", procname_scsi
);
425 if (strcmp(read2
, "none") == 0)
427 TRACE("No SCSI devices found in %s.\n", procname_scsi
);
432 /* Read info for one device */
433 while ((result
= SCSI_getprocentry(procfile
, &dev
)) > 0)
435 scsi_addr
.PortNumber
= dev
.host
;
436 scsi_addr
.PathId
= dev
.channel
;
437 scsi_addr
.TargetId
= dev
.target
;
438 scsi_addr
.Lun
= dev
.lun
;
440 scsi_addr
.PortNumber
+= uFirstSCSIPort
;
441 if (strncmp(dev
.type
, "Direct-Access", 13) == 0) nType
= DRIVE_FIXED
;
442 else if (strncmp(dev
.type
, "Sequential-Access", 17) == 0) nType
= DRIVE_REMOVABLE
;
443 else if (strncmp(dev
.type
, "CD-ROM", 6) == 0) nType
= DRIVE_CDROM
;
444 else if (strncmp(dev
.type
, "Processor", 9) == 0) nType
= DRIVE_NO_ROOT_DIR
;
447 strcpy(cDevModel
, dev
.vendor
);
448 strcat(cDevModel
, dev
.model
);
449 strcat(cDevModel
, dev
.rev
);
450 sprintf(cUnixDeviceName
, "/dev/sg%d", nSgNumber
++);
451 /* FIXME: get real driver name */
452 create_scsi_entry(&scsi_addr
, "WINE SCSI", nType
, cDevModel
, cUnixDeviceName
);
455 WARN("Incorrect %s format\n", procname_scsi
);
460 /***********************************************************************
463 void convert_old_config(void)
465 if (allocate_default_keys() == REG_OPENED_EXISTING_KEY
)
466 return; /* someone else already loaded the registry */
468 /* create some hardware keys (FIXME: should not be done here) */
469 create_hardware_branch();