push 6e61d6ca5bcaf95ac09a664b4ba4f88238c927be
[wine/hacks.git] / dlls / kernel32 / oldconfig.c
blobc77e7b2ee22d69c13aff7340e3cb461ca2dd86bf
1 /*
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
20 * NOTES
21 * This file should be removed after a suitable transition period.
24 #include "config.h"
25 #include "wine/port.h"
27 #include <stdarg.h>
28 #include <sys/types.h>
29 #include <stdlib.h>
30 #include <stdio.h>
31 #ifdef HAVE_SYS_STAT_H
32 # include <sys/stat.h>
33 #endif
34 #include <fcntl.h>
35 #ifdef HAVE_DIRENT_H
36 # include <dirent.h>
37 #endif
38 #ifdef HAVE_SYS_IOCTL_H
39 #include <sys/ioctl.h>
40 #endif
41 #ifdef HAVE_LINUX_HDREG_H
42 # include <linux/hdreg.h>
43 #endif
45 #define NONAMELESSUNION
46 #define NONAMELESSSTRUCT
47 #include "windef.h"
48 #include "winbase.h"
49 #include "winternl.h"
50 #include "ntddscsi.h"
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 /******************************************************************
59 * create_scsi_entry
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;
73 UNICODE_STRING nameW;
74 WCHAR dataW[50];
75 DWORD sizeW;
76 char buffer[40];
77 DWORD value;
78 const char *data;
79 HANDLE scsiKey;
80 HANDLE portKey;
81 HANDLE busKey;
82 HANDLE targetKey;
83 HANDLE lunKey;
84 DWORD disp;
86 attr.Length = sizeof(attr);
87 attr.RootDirectory = 0;
88 attr.ObjectName = &nameW;
89 attr.Attributes = 0;
90 attr.SecurityDescriptor = NULL;
91 attr.SecurityQualityOfService = NULL;
93 if (!RtlCreateUnicodeStringFromAsciiz( &nameW, "Machine\\HARDWARE" ) ||
94 NtCreateKey( &scsiKey, KEY_ALL_ACCESS, &attr, 0, NULL, REG_OPTION_VOLATILE, &disp ))
96 ERR("Cannot create HARDWARE registry key\n" );
97 return;
99 NtClose( scsiKey );
100 RtlFreeUnicodeString( &nameW );
101 if (disp == REG_OPENED_EXISTING_KEY) return;
103 if (!RtlCreateUnicodeStringFromAsciiz( &nameW, "Machine\\HARDWARE\\DEVICEMAP" ) ||
104 NtCreateKey( &scsiKey, KEY_ALL_ACCESS, &attr, 0, NULL, REG_OPTION_VOLATILE, &disp ))
106 ERR("Cannot create DEVICEMAP registry key\n" );
107 return;
109 NtClose( scsiKey );
110 RtlFreeUnicodeString( &nameW );
112 /* Ensure there is Scsi key */
113 if (!RtlCreateUnicodeStringFromAsciiz( &nameW, "Machine\\HARDWARE\\DEVICEMAP\\Scsi" ) ||
114 NtCreateKey( &scsiKey, KEY_ALL_ACCESS, &attr, 0,
115 NULL, REG_OPTION_VOLATILE, &disp ))
117 ERR("Cannot create DEVICEMAP\\Scsi registry key\n" );
118 return;
120 RtlFreeUnicodeString( &nameW );
122 snprintf(buffer,sizeof(buffer),"Scsi Port %d",scsi_addr->PortNumber);
123 attr.RootDirectory = scsiKey;
124 if (!RtlCreateUnicodeStringFromAsciiz( &nameW, buffer ) ||
125 NtCreateKey( &portKey, KEY_ALL_ACCESS, &attr, 0,
126 NULL, REG_OPTION_VOLATILE, &disp ))
128 ERR("Cannot create DEVICEMAP\\Scsi Port registry key\n" );
129 return;
131 RtlFreeUnicodeString( &nameW );
133 RtlCreateUnicodeStringFromAsciiz( &nameW, "Driver" );
134 RtlMultiByteToUnicodeN( dataW, sizeof(dataW), &sizeW, lpDriver, strlen(lpDriver)+1);
135 NtSetValueKey( portKey, &nameW, 0, REG_SZ, dataW, sizeW );
136 RtlFreeUnicodeString( &nameW );
137 value = 10;
138 RtlCreateUnicodeStringFromAsciiz( &nameW, "FirstBusTimeScanInMs" );
139 NtSetValueKey( portKey,&nameW, 0, REG_DWORD, &value, sizeof(DWORD) );
140 RtlFreeUnicodeString( &nameW );
142 value = 0;
143 if (strcmp(lpDriver, "atapi") == 0)
145 #ifdef HDIO_GET_DMA
146 int fd, dma;
148 fd = open(lpUnixDeviceName, O_RDONLY|O_NONBLOCK);
149 if (fd != -1)
151 if (ioctl(fd, HDIO_GET_DMA, &dma) != -1) value = dma;
152 close(fd);
154 #endif
155 RtlCreateUnicodeStringFromAsciiz( &nameW, "DMAEnabled" );
156 NtSetValueKey( portKey,&nameW, 0, REG_DWORD, &value, sizeof(DWORD) );
157 RtlFreeUnicodeString( &nameW );
160 snprintf(buffer, sizeof(buffer),"Scsi Bus %d", scsi_addr->PathId);
161 attr.RootDirectory = portKey;
162 if (!RtlCreateUnicodeStringFromAsciiz( &nameW, buffer ) ||
163 NtCreateKey( &busKey, KEY_ALL_ACCESS, &attr, 0,
164 NULL, REG_OPTION_VOLATILE, &disp ))
166 ERR("Cannot create DEVICEMAP\\Scsi Port\\Scsi Bus registry key\n" );
167 return;
169 RtlFreeUnicodeString( &nameW );
171 attr.RootDirectory = busKey;
172 /* FIXME: get real controller Id for SCSI */
173 if (!RtlCreateUnicodeStringFromAsciiz( &nameW, "Initiator Id 255" ) ||
174 NtCreateKey( &targetKey, KEY_ALL_ACCESS, &attr, 0,
175 NULL, REG_OPTION_VOLATILE, &disp ))
177 ERR("Cannot create DEVICEMAP\\Scsi Port\\Scsi Bus\\Initiator Id 255 registry key\n" );
178 return;
180 RtlFreeUnicodeString( &nameW );
181 NtClose( targetKey );
183 snprintf(buffer, sizeof(buffer),"Target Id %d", scsi_addr->TargetId);
184 attr.RootDirectory = busKey;
185 if (!RtlCreateUnicodeStringFromAsciiz( &nameW, buffer ) ||
186 NtCreateKey( &targetKey, KEY_ALL_ACCESS, &attr, 0,
187 NULL, REG_OPTION_VOLATILE, &disp ))
189 ERR("Cannot create DEVICEMAP\\Scsi Port\\Scsi Bus 0\\Target Id registry key\n" );
190 return;
192 RtlFreeUnicodeString( &nameW );
194 snprintf(buffer, sizeof(buffer),"Logical Unit Id %d", scsi_addr->Lun);
195 attr.RootDirectory = targetKey;
196 if (!RtlCreateUnicodeStringFromAsciiz( &nameW, buffer ) ||
197 NtCreateKey( &lunKey, KEY_ALL_ACCESS, &attr, 0,
198 NULL, REG_OPTION_VOLATILE, &disp ))
200 ERR("Cannot create DEVICEMAP\\Scsi Port\\Scsi Bus 0\\Target Id registry key\\Logical Unit Id\n" );
201 return;
203 RtlFreeUnicodeString( &nameW );
205 switch (uDriveType)
207 case DRIVE_NO_ROOT_DIR:
208 default:
209 data = "OtherPeripheral"; break;
210 case DRIVE_FIXED:
211 data = "DiskPeripheral"; break;
212 case DRIVE_REMOVABLE:
213 data = "TapePeripheral";
214 snprintf(buffer, sizeof(buffer), "Tape%d", uTapeNumber++);
215 break;
216 case DRIVE_CDROM:
217 data = "CdRomPeripheral";
218 snprintf(buffer, sizeof(buffer), "Cdrom%d", uCdromNumber++);
219 break;
221 RtlCreateUnicodeStringFromAsciiz( &nameW, "Type" );
222 RtlMultiByteToUnicodeN( dataW, sizeof(dataW), &sizeW, data, strlen(data)+1);
223 NtSetValueKey( lunKey, &nameW, 0, REG_SZ, dataW, sizeW );
224 RtlFreeUnicodeString( &nameW );
226 RtlCreateUnicodeStringFromAsciiz( &nameW, "Identifier" );
227 RtlMultiByteToUnicodeN( dataW, sizeof(dataW), &sizeW, lpDriveName, strlen(lpDriveName)+1);
228 NtSetValueKey( lunKey, &nameW, 0, REG_SZ, dataW, sizeW );
229 RtlFreeUnicodeString( &nameW );
231 if (uDriveType == DRIVE_CDROM || uDriveType == DRIVE_REMOVABLE)
233 RtlCreateUnicodeStringFromAsciiz( &nameW, "DeviceName" );
234 RtlMultiByteToUnicodeN( dataW, sizeof(dataW), &sizeW, buffer, strlen(buffer)+1);
235 NtSetValueKey( lunKey, &nameW, 0, REG_SZ, dataW, sizeW );
236 RtlFreeUnicodeString( &nameW );
239 RtlCreateUnicodeStringFromAsciiz( &nameW, "UnixDeviceName" );
240 RtlMultiByteToUnicodeN( dataW, sizeof(dataW), &sizeW, lpUnixDeviceName, strlen(lpUnixDeviceName)+1);
241 NtSetValueKey( lunKey, &nameW, 0, REG_SZ, dataW, sizeW );
242 RtlFreeUnicodeString( &nameW );
244 NtClose( lunKey );
245 NtClose( targetKey );
246 NtClose( busKey );
247 NtClose( portKey );
248 NtClose( scsiKey );
251 struct LinuxProcScsiDevice
253 int host;
254 int channel;
255 int target;
256 int lun;
257 char vendor[9];
258 char model[17];
259 char rev[5];
260 char type[33];
261 int ansirev;
265 * we need to declare white spaces explicitly via %*1[ ],
266 * as there are very dainbread CD-ROM devices out there
267 * which have their manufacturer name blanked out via spaces,
268 * which confuses fscanf's parsing (skips all blank spaces)
270 static int SCSI_getprocentry( FILE * procfile, struct LinuxProcScsiDevice * dev )
272 int result;
274 result = fscanf( procfile,
275 "Host:%*1[ ]scsi%d%*1[ ]Channel:%*1[ ]%d%*1[ ]Id:%*1[ ]%d%*1[ ]Lun:%*1[ ]%d\n",
276 &dev->host,
277 &dev->channel,
278 &dev->target,
279 &dev->lun );
280 if( result == EOF )
282 /* "end of entries" return, so no TRACE() here */
283 return EOF;
285 if( result != 4 )
287 ERR("bus id line scan count error (fscanf returns %d, expected 4)\n", result);
288 return 0;
290 result = fscanf( procfile,
291 " Vendor:%*1[ ]%8c%*1[ ]Model:%*1[ ]%16c%*1[ ]Rev:%*1[ ]%4c\n",
292 dev->vendor,
293 dev->model,
294 dev->rev );
295 if( result != 3 )
297 ERR("model line scan count error (fscanf returns %d, expected 3)\n", result);
298 return 0;
301 result = fscanf( procfile,
302 " Type:%*3[ ]%32c%*1[ ]ANSI SCSI%*1[ ]revision:%*1[ ]%x\n",
303 dev->type,
304 &dev->ansirev );
305 if( result != 2 )
307 ERR("SCSI type line scan count error (fscanf returns %d, expected 2)\n", result);
308 return 0;
310 /* Since we fscanf with %XXc instead of %s.. put a NULL at end */
311 dev->vendor[8] = 0;
312 dev->model[16] = 0;
313 dev->rev[4] = 0;
314 dev->type[32] = 0;
316 return 1;
320 /* create the hardware registry branch */
321 static void create_hardware_branch(void)
323 /* The following mostly will work on Linux, but should not cause
324 * problems on other systems. */
325 static const char procname_ide_media[] = "/proc/ide/%s/media";
326 static const char procname_ide_model[] = "/proc/ide/%s/model";
327 static const char procname_scsi[] = "/proc/scsi/scsi";
328 DIR *idedir;
329 struct dirent *dent = NULL;
330 FILE *procfile = NULL;
331 char cStr[40], cDevModel[40], cUnixDeviceName[40], read1[10] = "\0", read2[10] = "\0";
332 SCSI_ADDRESS scsi_addr;
333 UINT nType;
334 struct LinuxProcScsiDevice dev;
335 int result = 0, nSgNumber = 0;
336 UCHAR uFirstSCSIPort = 0;
338 /* Enumerate all ide devices first */
339 idedir = opendir("/proc/ide");
340 if (idedir)
342 while ((dent = readdir(idedir)))
344 if (strncmp(dent->d_name, "hd", 2) == 0)
346 sprintf(cStr, procname_ide_media, dent->d_name);
347 procfile = fopen(cStr, "r");
348 if (!procfile)
350 ERR("Could not open %s\n", cStr);
351 continue;
352 } else {
353 fgets(cStr, sizeof(cStr), procfile);
354 fclose(procfile);
355 nType = DRIVE_UNKNOWN;
356 if (strncasecmp(cStr, "disk", 4) == 0) nType = DRIVE_FIXED;
357 if (strncasecmp(cStr, "cdrom", 5) == 0) nType = DRIVE_CDROM;
359 if (nType == DRIVE_UNKNOWN) continue;
362 sprintf(cStr, procname_ide_model, dent->d_name);
363 procfile = fopen(cStr, "r");
364 if (!procfile)
366 ERR("Could not open %s\n", cStr);
367 switch (nType)
369 case DRIVE_FIXED: strcpy(cDevModel, "Wine harddisk"); break;
370 case DRIVE_CDROM: strcpy(cDevModel, "Wine CDROM"); break;
372 } else {
373 fgets(cDevModel, sizeof(cDevModel), procfile);
374 fclose(procfile);
375 cDevModel[strlen(cDevModel) - 1] = 0;
378 sprintf(cUnixDeviceName, "/dev/%s", dent->d_name);
379 scsi_addr.PortNumber = (dent->d_name[2] - 'a') / 2;
380 scsi_addr.PathId = 0;
381 scsi_addr.TargetId = (dent->d_name[2] - 'a') % 2;
382 scsi_addr.Lun = 0;
383 if (scsi_addr.PortNumber + 1 > uFirstSCSIPort)
384 uFirstSCSIPort = scsi_addr.PortNumber + 1;
386 create_scsi_entry(&scsi_addr, "atapi", nType, cDevModel, cUnixDeviceName);
389 closedir(idedir);
392 /* Now goes SCSI */
393 procfile = fopen(procname_scsi, "r");
394 if (!procfile)
396 TRACE("Could not open %s\n", procname_scsi);
397 return;
399 fgets(cStr, 40, procfile);
400 sscanf(cStr, "Attached %9s %9s", read1, read2);
402 if (strcmp(read1, "devices:") != 0)
404 WARN("Incorrect %s format\n", procname_scsi);
405 fclose( procfile );
406 return;
408 if (strcmp(read2, "none") == 0)
410 TRACE("No SCSI devices found in %s.\n", procname_scsi);
411 fclose( procfile );
412 return;
415 /* Read info for one device */
416 while ((result = SCSI_getprocentry(procfile, &dev)) > 0)
418 scsi_addr.PortNumber = dev.host;
419 scsi_addr.PathId = dev.channel;
420 scsi_addr.TargetId = dev.target;
421 scsi_addr.Lun = dev.lun;
423 scsi_addr.PortNumber += uFirstSCSIPort;
424 if (strncmp(dev.type, "Direct-Access", 13) == 0) nType = DRIVE_FIXED;
425 else if (strncmp(dev.type, "Sequential-Access", 17) == 0) nType = DRIVE_REMOVABLE;
426 else if (strncmp(dev.type, "CD-ROM", 6) == 0) nType = DRIVE_CDROM;
427 else if (strncmp(dev.type, "Processor", 9) == 0) nType = DRIVE_NO_ROOT_DIR;
428 else continue;
430 strcpy(cDevModel, dev.vendor);
431 strcat(cDevModel, dev.model);
432 strcat(cDevModel, dev.rev);
433 sprintf(cUnixDeviceName, "/dev/sg%d", nSgNumber++);
434 /* FIXME: get real driver name */
435 create_scsi_entry(&scsi_addr, "WINE SCSI", nType, cDevModel, cUnixDeviceName);
437 if( result != EOF )
438 WARN("Incorrect %s format\n", procname_scsi);
439 fclose( procfile );
443 /***********************************************************************
444 * convert_old_config
446 void convert_old_config(void)
448 /* create some hardware keys (FIXME: should not be done here) */
449 create_hardware_branch();