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
);
59 /* registry initialisation, allocates some default keys. */
60 static ULONG
allocate_default_keys(void)
62 static const WCHAR StatDataW
[] = {'D','y','n','D','a','t','a','\\',
63 'P','e','r','f','S','t','a','t','s','\\',
64 'S','t','a','t','D','a','t','a',0};
65 static const WCHAR ConfigManagerW
[] = {'D','y','n','D','a','t','a','\\',
66 'C','o','n','f','i','g',' ','M','a','n','a','g','e','r','\\',
70 OBJECT_ATTRIBUTES attr
;
73 attr
.Length
= sizeof(attr
);
74 attr
.RootDirectory
= 0;
75 attr
.ObjectName
= &nameW
;
77 attr
.SecurityDescriptor
= NULL
;
78 attr
.SecurityQualityOfService
= NULL
;
80 RtlInitUnicodeString( &nameW
, StatDataW
);
81 if (!NtCreateKey( &hkey
, KEY_ALL_ACCESS
, &attr
, 0, NULL
, 0, &dispos
)) NtClose( hkey
);
82 if (dispos
== REG_OPENED_EXISTING_KEY
)
83 return dispos
; /* someone else already loaded the registry */
85 RtlInitUnicodeString( &nameW
, ConfigManagerW
);
86 if (!NtCreateKey( &hkey
, KEY_ALL_ACCESS
, &attr
, 0, NULL
, 0, NULL
)) NtClose( hkey
);
91 /******************************************************************
94 * Initializes registry to contain scsi info about the cdrom in NT.
95 * All devices (even not real scsi ones) have this info in NT.
96 * NOTE: programs usually read these registry entries after sending the
97 * IOCTL_SCSI_GET_ADDRESS ioctl to the cdrom
99 static void create_scsi_entry( PSCSI_ADDRESS scsi_addr
, LPCSTR lpDriver
, UINT uDriveType
,
100 LPSTR lpDriveName
, LPSTR lpUnixDeviceName
)
102 static UCHAR uCdromNumber
= 0;
103 static UCHAR uTapeNumber
= 0;
105 OBJECT_ATTRIBUTES attr
;
106 UNICODE_STRING nameW
;
119 attr
.Length
= sizeof(attr
);
120 attr
.RootDirectory
= 0;
121 attr
.ObjectName
= &nameW
;
123 attr
.SecurityDescriptor
= NULL
;
124 attr
.SecurityQualityOfService
= NULL
;
126 /* Ensure there is Scsi key */
127 if (!RtlCreateUnicodeStringFromAsciiz( &nameW
, "Machine\\HARDWARE\\DEVICEMAP\\Scsi" ) ||
128 NtCreateKey( &scsiKey
, KEY_ALL_ACCESS
, &attr
, 0,
129 NULL
, REG_OPTION_VOLATILE
, &disp
))
131 ERR("Cannot create DEVICEMAP\\Scsi registry key\n" );
134 RtlFreeUnicodeString( &nameW
);
136 snprintf(buffer
,sizeof(buffer
),"Scsi Port %d",scsi_addr
->PortNumber
);
137 attr
.RootDirectory
= scsiKey
;
138 if (!RtlCreateUnicodeStringFromAsciiz( &nameW
, buffer
) ||
139 NtCreateKey( &portKey
, KEY_ALL_ACCESS
, &attr
, 0,
140 NULL
, REG_OPTION_VOLATILE
, &disp
))
142 ERR("Cannot create DEVICEMAP\\Scsi Port registry key\n" );
145 RtlFreeUnicodeString( &nameW
);
147 RtlCreateUnicodeStringFromAsciiz( &nameW
, "Driver" );
148 RtlMultiByteToUnicodeN( dataW
, sizeof(dataW
), &sizeW
, lpDriver
, strlen(lpDriver
)+1);
149 NtSetValueKey( portKey
, &nameW
, 0, REG_SZ
, (BYTE
*)dataW
, sizeW
);
150 RtlFreeUnicodeString( &nameW
);
152 RtlCreateUnicodeStringFromAsciiz( &nameW
, "FirstBusTimeScanInMs" );
153 NtSetValueKey( portKey
,&nameW
, 0, REG_DWORD
, (BYTE
*)&value
, sizeof(DWORD
));
154 RtlFreeUnicodeString( &nameW
);
157 if (strcmp(lpDriver
, "atapi") == 0)
162 fd
= open(lpUnixDeviceName
, O_RDONLY
|O_NONBLOCK
);
165 if (ioctl(fd
, HDIO_GET_DMA
, &dma
) != -1) value
= dma
;
169 RtlCreateUnicodeStringFromAsciiz( &nameW
, "DMAEnabled" );
170 NtSetValueKey( portKey
,&nameW
, 0, REG_DWORD
, (BYTE
*)&value
, sizeof(DWORD
));
171 RtlFreeUnicodeString( &nameW
);
174 snprintf(buffer
, sizeof(buffer
),"Scsi Bus %d", scsi_addr
->PathId
);
175 attr
.RootDirectory
= portKey
;
176 if (!RtlCreateUnicodeStringFromAsciiz( &nameW
, buffer
) ||
177 NtCreateKey( &busKey
, KEY_ALL_ACCESS
, &attr
, 0,
178 NULL
, REG_OPTION_VOLATILE
, &disp
))
180 ERR("Cannot create DEVICEMAP\\Scsi Port\\Scsi Bus registry key\n" );
183 RtlFreeUnicodeString( &nameW
);
185 attr
.RootDirectory
= busKey
;
186 /* FIXME: get real controller Id for SCSI */
187 if (!RtlCreateUnicodeStringFromAsciiz( &nameW
, "Initiator Id 255" ) ||
188 NtCreateKey( &targetKey
, KEY_ALL_ACCESS
, &attr
, 0,
189 NULL
, REG_OPTION_VOLATILE
, &disp
))
191 ERR("Cannot create DEVICEMAP\\Scsi Port\\Scsi Bus\\Initiator Id 255 registry key\n" );
194 RtlFreeUnicodeString( &nameW
);
195 NtClose( targetKey
);
197 snprintf(buffer
, sizeof(buffer
),"Target Id %d", scsi_addr
->TargetId
);
198 attr
.RootDirectory
= busKey
;
199 if (!RtlCreateUnicodeStringFromAsciiz( &nameW
, buffer
) ||
200 NtCreateKey( &targetKey
, KEY_ALL_ACCESS
, &attr
, 0,
201 NULL
, REG_OPTION_VOLATILE
, &disp
))
203 ERR("Cannot create DEVICEMAP\\Scsi Port\\Scsi Bus 0\\Target Id registry key\n" );
206 RtlFreeUnicodeString( &nameW
);
208 snprintf(buffer
, sizeof(buffer
),"Logical Unit Id %d", scsi_addr
->Lun
);
209 attr
.RootDirectory
= targetKey
;
210 if (!RtlCreateUnicodeStringFromAsciiz( &nameW
, buffer
) ||
211 NtCreateKey( &lunKey
, KEY_ALL_ACCESS
, &attr
, 0,
212 NULL
, REG_OPTION_VOLATILE
, &disp
))
214 ERR("Cannot create DEVICEMAP\\Scsi Port\\Scsi Bus 0\\Target Id registry key\\Logical Unit Id\n" );
217 RtlFreeUnicodeString( &nameW
);
221 case DRIVE_NO_ROOT_DIR
:
223 data
= "OtherPeripheral"; break;
225 data
= "DiskPeripheral"; break;
226 case DRIVE_REMOVABLE
:
227 data
= "TapePeripheral";
228 snprintf(buffer
, sizeof(buffer
), "Tape%d", uTapeNumber
++);
231 data
= "CdRomPeripheral";
232 snprintf(buffer
, sizeof(buffer
), "Cdrom%d", uCdromNumber
++);
235 RtlCreateUnicodeStringFromAsciiz( &nameW
, "Type" );
236 RtlMultiByteToUnicodeN( dataW
, sizeof(dataW
), &sizeW
, data
, strlen(data
)+1);
237 NtSetValueKey( lunKey
, &nameW
, 0, REG_SZ
, (BYTE
*)dataW
, sizeW
);
238 RtlFreeUnicodeString( &nameW
);
240 RtlCreateUnicodeStringFromAsciiz( &nameW
, "Identifier" );
241 RtlMultiByteToUnicodeN( dataW
, sizeof(dataW
), &sizeW
, lpDriveName
, strlen(lpDriveName
)+1);
242 NtSetValueKey( lunKey
, &nameW
, 0, REG_SZ
, (BYTE
*)dataW
, sizeW
);
243 RtlFreeUnicodeString( &nameW
);
245 if (uDriveType
== DRIVE_CDROM
|| uDriveType
== DRIVE_REMOVABLE
)
247 RtlCreateUnicodeStringFromAsciiz( &nameW
, "DeviceName" );
248 RtlMultiByteToUnicodeN( dataW
, sizeof(dataW
), &sizeW
, buffer
, strlen(buffer
)+1);
249 NtSetValueKey( lunKey
, &nameW
, 0, REG_SZ
, (BYTE
*)dataW
, sizeW
);
250 RtlFreeUnicodeString( &nameW
);
253 RtlCreateUnicodeStringFromAsciiz( &nameW
, "UnixDeviceName" );
254 RtlMultiByteToUnicodeN( dataW
, sizeof(dataW
), &sizeW
, lpUnixDeviceName
, strlen(lpUnixDeviceName
)+1);
255 NtSetValueKey( lunKey
, &nameW
, 0, REG_SZ
, (BYTE
*)dataW
, sizeW
);
256 RtlFreeUnicodeString( &nameW
);
259 NtClose( targetKey
);
265 struct LinuxProcScsiDevice
279 * we need to declare white spaces explicitly via %*1[ ],
280 * as there are very dainbread CD-ROM devices out there
281 * which have their manufacturer name blanked out via spaces,
282 * which confuses fscanf's parsing (skips all blank spaces)
284 static int SCSI_getprocentry( FILE * procfile
, struct LinuxProcScsiDevice
* dev
)
288 result
= fscanf( procfile
,
289 "Host:%*1[ ]scsi%d%*1[ ]Channel:%*1[ ]%d%*1[ ]Id:%*1[ ]%d%*1[ ]Lun:%*1[ ]%d\n",
296 /* "end of entries" return, so no TRACE() here */
301 ERR("bus id line scan count error (fscanf returns %d, expected 4)\n", result
);
304 result
= fscanf( procfile
,
305 " Vendor:%*1[ ]%8c%*1[ ]Model:%*1[ ]%16c%*1[ ]Rev:%*1[ ]%4c\n",
311 ERR("model line scan count error (fscanf returns %d, expected 3)\n", result
);
315 result
= fscanf( procfile
,
316 " Type:%*3[ ]%32c%*1[ ]ANSI SCSI%*1[ ]revision:%*1[ ]%x\n",
321 ERR("SCSI type line scan count error (fscanf returns %d, expected 2)\n", result
);
324 /* Since we fscanf with %XXc instead of %s.. put a NULL at end */
334 /* create the hardware registry branch */
335 static void create_hardware_branch(void)
337 /* The following mostly will work on Linux, but should not cause
338 * problems on other systems. */
339 static const char procname_ide_media
[] = "/proc/ide/%s/media";
340 static const char procname_ide_model
[] = "/proc/ide/%s/model";
341 static const char procname_scsi
[] = "/proc/scsi/scsi";
343 struct dirent
*dent
= NULL
;
344 FILE *procfile
= NULL
;
345 char cStr
[40], cDevModel
[40], cUnixDeviceName
[40], read1
[10] = "\0", read2
[10] = "\0";
346 SCSI_ADDRESS scsi_addr
;
348 struct LinuxProcScsiDevice dev
;
349 int result
= 0, nSgNumber
= 0;
350 UCHAR uFirstSCSIPort
= 0;
352 /* Enumerate all ide devices first */
353 idedir
= opendir("/proc/ide");
356 while ((dent
= readdir(idedir
)))
358 if (strncmp(dent
->d_name
, "hd", 2) == 0)
360 sprintf(cStr
, procname_ide_media
, dent
->d_name
);
361 procfile
= fopen(cStr
, "r");
364 ERR("Could not open %s\n", cStr
);
367 fgets(cStr
, sizeof(cStr
), procfile
);
369 nType
= DRIVE_UNKNOWN
;
370 if (strncasecmp(cStr
, "disk", 4) == 0) nType
= DRIVE_FIXED
;
371 if (strncasecmp(cStr
, "cdrom", 5) == 0) nType
= DRIVE_CDROM
;
373 if (nType
== DRIVE_UNKNOWN
) continue;
376 sprintf(cStr
, procname_ide_model
, dent
->d_name
);
377 procfile
= fopen(cStr
, "r");
380 ERR("Could not open %s\n", cStr
);
383 case DRIVE_FIXED
: strcpy(cDevModel
, "Wine harddisk"); break;
384 case DRIVE_CDROM
: strcpy(cDevModel
, "Wine CDROM"); break;
387 fgets(cDevModel
, sizeof(cDevModel
), procfile
);
389 cDevModel
[strlen(cDevModel
) - 1] = 0;
392 sprintf(cUnixDeviceName
, "/dev/%s", dent
->d_name
);
393 scsi_addr
.PortNumber
= (dent
->d_name
[2] - 'a') / 2;
394 scsi_addr
.PathId
= 0;
395 scsi_addr
.TargetId
= (dent
->d_name
[2] - 'a') % 2;
397 if (scsi_addr
.PortNumber
+ 1 > uFirstSCSIPort
)
398 uFirstSCSIPort
= scsi_addr
.PortNumber
+ 1;
400 create_scsi_entry(&scsi_addr
, "atapi", nType
, cDevModel
, cUnixDeviceName
);
407 procfile
= fopen(procname_scsi
, "r");
410 TRACE("Could not open %s\n", procname_scsi
);
413 fgets(cStr
, 40, procfile
);
414 sscanf(cStr
, "Attached %9s %9s", read1
, read2
);
416 if (strcmp(read1
, "devices:") != 0)
418 WARN("Incorrect %s format\n", procname_scsi
);
422 if (strcmp(read2
, "none") == 0)
424 TRACE("No SCSI devices found in %s.\n", procname_scsi
);
429 /* Read info for one device */
430 while ((result
= SCSI_getprocentry(procfile
, &dev
)) > 0)
432 scsi_addr
.PortNumber
= dev
.host
;
433 scsi_addr
.PathId
= dev
.channel
;
434 scsi_addr
.TargetId
= dev
.target
;
435 scsi_addr
.Lun
= dev
.lun
;
437 scsi_addr
.PortNumber
+= uFirstSCSIPort
;
438 if (strncmp(dev
.type
, "Direct-Access", 13) == 0) nType
= DRIVE_FIXED
;
439 else if (strncmp(dev
.type
, "Sequential-Access", 17) == 0) nType
= DRIVE_REMOVABLE
;
440 else if (strncmp(dev
.type
, "CD-ROM", 6) == 0) nType
= DRIVE_CDROM
;
441 else if (strncmp(dev
.type
, "Processor", 9) == 0) nType
= DRIVE_NO_ROOT_DIR
;
444 strcpy(cDevModel
, dev
.vendor
);
445 strcat(cDevModel
, dev
.model
);
446 strcat(cDevModel
, dev
.rev
);
447 sprintf(cUnixDeviceName
, "/dev/sg%d", nSgNumber
++);
448 /* FIXME: get real driver name */
449 create_scsi_entry(&scsi_addr
, "WINE SCSI", nType
, cDevModel
, cUnixDeviceName
);
452 WARN("Incorrect %s format\n", procname_scsi
);
457 /***********************************************************************
460 void convert_old_config(void)
462 if (allocate_default_keys() == REG_OPENED_EXISTING_KEY
)
463 return; /* someone else already loaded the registry */
465 /* create some hardware keys (FIXME: should not be done here) */
466 create_hardware_branch();