push 7f8c39dca3a5819e8ef115eebf7abed537de3a22
[wine/hacks.git] / dlls / kernel32 / oldconfig.c
blob09e407b59d5cf2d796c1e895c65ea857bcae20b4
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 #include <dirent.h>
36 #ifdef HAVE_SYS_IOCTL_H
37 #include <sys/ioctl.h>
38 #endif
39 #ifdef HAVE_LINUX_HDREG_H
40 # include <linux/hdreg.h>
41 #endif
43 #define NONAMELESSUNION
44 #define NONAMELESSSTRUCT
45 #include "windef.h"
46 #include "winbase.h"
47 #include "winternl.h"
48 #include "ntddscsi.h"
49 #include "wine/library.h"
50 #include "wine/unicode.h"
51 #include "wine/debug.h"
52 #include "kernel_private.h"
54 WINE_DEFAULT_DEBUG_CHANNEL(reg);
57 /* registry initialisation, allocates some default keys. */
58 static ULONG allocate_default_keys(void)
60 static const WCHAR StatDataW[] = {'D','y','n','D','a','t','a','\\',
61 'P','e','r','f','S','t','a','t','s','\\',
62 'S','t','a','t','D','a','t','a',0};
63 static const WCHAR ConfigManagerW[] = {'D','y','n','D','a','t','a','\\',
64 'C','o','n','f','i','g',' ','M','a','n','a','g','e','r','\\',
65 'E','n','u','m',0};
66 HANDLE hkey;
67 ULONG dispos;
68 OBJECT_ATTRIBUTES attr;
69 UNICODE_STRING nameW;
71 attr.Length = sizeof(attr);
72 attr.RootDirectory = 0;
73 attr.ObjectName = &nameW;
74 attr.Attributes = 0;
75 attr.SecurityDescriptor = NULL;
76 attr.SecurityQualityOfService = NULL;
78 RtlInitUnicodeString( &nameW, StatDataW );
79 if (!NtCreateKey( &hkey, KEY_ALL_ACCESS, &attr, 0, NULL, 0, &dispos )) NtClose( hkey );
80 if (dispos == REG_OPENED_EXISTING_KEY)
81 return dispos; /* someone else already loaded the registry */
83 RtlInitUnicodeString( &nameW, ConfigManagerW );
84 if (!NtCreateKey( &hkey, KEY_ALL_ACCESS, &attr, 0, NULL, 0, NULL )) NtClose( hkey );
86 return dispos;
89 /******************************************************************
90 * create_scsi_entry
92 * Initializes registry to contain scsi info about the cdrom in NT.
93 * All devices (even not real scsi ones) have this info in NT.
94 * NOTE: programs usually read these registry entries after sending the
95 * IOCTL_SCSI_GET_ADDRESS ioctl to the cdrom
97 static void create_scsi_entry( PSCSI_ADDRESS scsi_addr, LPCSTR lpDriver, UINT uDriveType,
98 LPSTR lpDriveName, LPSTR lpUnixDeviceName )
100 static UCHAR uCdromNumber = 0;
101 static UCHAR uTapeNumber = 0;
103 OBJECT_ATTRIBUTES attr;
104 UNICODE_STRING nameW;
105 WCHAR dataW[50];
106 DWORD lenW;
107 char buffer[40];
108 DWORD value;
109 const char *data;
110 HANDLE scsiKey;
111 HANDLE portKey;
112 HANDLE busKey;
113 HANDLE targetKey;
114 HANDLE lunKey;
115 DWORD disp;
117 attr.Length = sizeof(attr);
118 attr.RootDirectory = 0;
119 attr.ObjectName = &nameW;
120 attr.Attributes = 0;
121 attr.SecurityDescriptor = NULL;
122 attr.SecurityQualityOfService = NULL;
124 /* Ensure there is Scsi key */
125 if (!RtlCreateUnicodeStringFromAsciiz( &nameW, "Machine\\HARDWARE\\DEVICEMAP\\Scsi" ) ||
126 NtCreateKey( &scsiKey, KEY_ALL_ACCESS, &attr, 0,
127 NULL, REG_OPTION_VOLATILE, &disp ))
129 ERR("Cannot create DEVICEMAP\\Scsi registry key\n" );
130 return;
132 RtlFreeUnicodeString( &nameW );
134 snprintf(buffer,sizeof(buffer),"Scsi Port %d",scsi_addr->PortNumber);
135 attr.RootDirectory = scsiKey;
136 if (!RtlCreateUnicodeStringFromAsciiz( &nameW, buffer ) ||
137 NtCreateKey( &portKey, KEY_ALL_ACCESS, &attr, 0,
138 NULL, REG_OPTION_VOLATILE, &disp ))
140 ERR("Cannot create DEVICEMAP\\Scsi Port registry key\n" );
141 return;
143 RtlFreeUnicodeString( &nameW );
145 RtlCreateUnicodeStringFromAsciiz( &nameW, "Driver" );
146 RtlMultiByteToUnicodeN( dataW, sizeof(dataW), &lenW, lpDriver, strlen(lpDriver));
147 NtSetValueKey( portKey, &nameW, 0, REG_SZ, (BYTE*)dataW, lenW );
148 RtlFreeUnicodeString( &nameW );
149 value = 10;
150 RtlCreateUnicodeStringFromAsciiz( &nameW, "FirstBusTimeScanInMs" );
151 NtSetValueKey( portKey,&nameW, 0, REG_DWORD, (BYTE *)&value, sizeof(DWORD));
152 RtlFreeUnicodeString( &nameW );
154 value = 0;
155 if (strcmp(lpDriver, "atapi") == 0)
157 #ifdef HDIO_GET_DMA
158 int fd, dma;
160 fd = open(lpUnixDeviceName, O_RDONLY|O_NONBLOCK);
161 if (fd != -1)
163 if (ioctl(fd, HDIO_GET_DMA, &dma) != -1) value = dma;
164 close(fd);
166 #endif
167 RtlCreateUnicodeStringFromAsciiz( &nameW, "DMAEnabled" );
168 NtSetValueKey( portKey,&nameW, 0, REG_DWORD, (BYTE *)&value, sizeof(DWORD));
169 RtlFreeUnicodeString( &nameW );
172 snprintf(buffer, sizeof(buffer),"Scsi Bus %d", scsi_addr->PathId);
173 attr.RootDirectory = portKey;
174 if (!RtlCreateUnicodeStringFromAsciiz( &nameW, buffer ) ||
175 NtCreateKey( &busKey, KEY_ALL_ACCESS, &attr, 0,
176 NULL, REG_OPTION_VOLATILE, &disp ))
178 ERR("Cannot create DEVICEMAP\\Scsi Port\\Scsi Bus registry key\n" );
179 return;
181 RtlFreeUnicodeString( &nameW );
183 attr.RootDirectory = busKey;
184 /* FIXME: get real controller Id for SCSI */
185 if (!RtlCreateUnicodeStringFromAsciiz( &nameW, "Initiator Id 255" ) ||
186 NtCreateKey( &targetKey, KEY_ALL_ACCESS, &attr, 0,
187 NULL, REG_OPTION_VOLATILE, &disp ))
189 ERR("Cannot create DEVICEMAP\\Scsi Port\\Scsi Bus\\Initiator Id 255 registry key\n" );
190 return;
192 RtlFreeUnicodeString( &nameW );
193 NtClose( targetKey );
195 snprintf(buffer, sizeof(buffer),"Target Id %d", scsi_addr->TargetId);
196 attr.RootDirectory = busKey;
197 if (!RtlCreateUnicodeStringFromAsciiz( &nameW, buffer ) ||
198 NtCreateKey( &targetKey, KEY_ALL_ACCESS, &attr, 0,
199 NULL, REG_OPTION_VOLATILE, &disp ))
201 ERR("Cannot create DEVICEMAP\\Scsi Port\\Scsi Bus 0\\Target Id registry key\n" );
202 return;
204 RtlFreeUnicodeString( &nameW );
206 snprintf(buffer, sizeof(buffer),"Logical Unit Id %d", scsi_addr->Lun);
207 attr.RootDirectory = targetKey;
208 if (!RtlCreateUnicodeStringFromAsciiz( &nameW, buffer ) ||
209 NtCreateKey( &lunKey, KEY_ALL_ACCESS, &attr, 0,
210 NULL, REG_OPTION_VOLATILE, &disp ))
212 ERR("Cannot create DEVICEMAP\\Scsi Port\\Scsi Bus 0\\Target Id registry key\\Logical Unit Id\n" );
213 return;
215 RtlFreeUnicodeString( &nameW );
217 switch (uDriveType)
219 case DRIVE_NO_ROOT_DIR:
220 default:
221 data = "OtherPeripheral"; break;
222 case DRIVE_FIXED:
223 data = "DiskPeripheral"; break;
224 case DRIVE_REMOVABLE:
225 data = "TapePeripheral";
226 snprintf(buffer, sizeof(buffer), "Tape%d", uTapeNumber++);
227 break;
228 case DRIVE_CDROM:
229 data = "CdRomPeripheral";
230 snprintf(buffer, sizeof(buffer), "Cdrom%d", uCdromNumber++);
231 break;
233 RtlCreateUnicodeStringFromAsciiz( &nameW, "Type" );
234 RtlMultiByteToUnicodeN( dataW, sizeof(dataW), &lenW, data, strlen(data));
235 NtSetValueKey( lunKey, &nameW, 0, REG_SZ, (BYTE*)dataW, lenW );
236 RtlFreeUnicodeString( &nameW );
238 RtlCreateUnicodeStringFromAsciiz( &nameW, "Identifier" );
239 RtlMultiByteToUnicodeN( dataW, sizeof(dataW), &lenW, lpDriveName, strlen(lpDriveName));
240 NtSetValueKey( lunKey, &nameW, 0, REG_SZ, (BYTE*)dataW, lenW );
241 RtlFreeUnicodeString( &nameW );
243 if (uDriveType == DRIVE_CDROM || uDriveType == DRIVE_REMOVABLE)
245 RtlCreateUnicodeStringFromAsciiz( &nameW, "DeviceName" );
246 RtlMultiByteToUnicodeN( dataW, sizeof(dataW), &lenW, buffer, strlen(buffer));
247 NtSetValueKey( lunKey, &nameW, 0, REG_SZ, (BYTE*)dataW, lenW );
248 RtlFreeUnicodeString( &nameW );
251 RtlCreateUnicodeStringFromAsciiz( &nameW, "UnixDeviceName" );
252 RtlMultiByteToUnicodeN( dataW, sizeof(dataW), &lenW, lpUnixDeviceName, strlen(lpUnixDeviceName));
253 NtSetValueKey( lunKey, &nameW, 0, REG_SZ, (BYTE*)dataW, lenW );
254 RtlFreeUnicodeString( &nameW );
256 NtClose( lunKey );
257 NtClose( targetKey );
258 NtClose( busKey );
259 NtClose( portKey );
260 NtClose( scsiKey );
263 struct LinuxProcScsiDevice
265 int host;
266 int channel;
267 int target;
268 int lun;
269 char vendor[9];
270 char model[17];
271 char rev[5];
272 char type[33];
273 int ansirev;
277 * we need to declare white spaces explicitly via %*1[ ],
278 * as there are very dainbread CD-ROM devices out there
279 * which have their manufacturer name blanked out via spaces,
280 * which confuses fscanf's parsing (skips all blank spaces)
282 static int SCSI_getprocentry( FILE * procfile, struct LinuxProcScsiDevice * dev )
284 int result;
286 result = fscanf( procfile,
287 "Host:%*1[ ]scsi%d%*1[ ]Channel:%*1[ ]%d%*1[ ]Id:%*1[ ]%d%*1[ ]Lun:%*1[ ]%d\n",
288 &dev->host,
289 &dev->channel,
290 &dev->target,
291 &dev->lun );
292 if( result == EOF )
294 /* "end of entries" return, so no TRACE() here */
295 return EOF;
297 if( result != 4 )
299 ERR("bus id line scan count error (fscanf returns %d, expected 4)\n", result);
300 return 0;
302 result = fscanf( procfile,
303 " Vendor:%*1[ ]%8c%*1[ ]Model:%*1[ ]%16c%*1[ ]Rev:%*1[ ]%4c\n",
304 dev->vendor,
305 dev->model,
306 dev->rev );
307 if( result != 3 )
309 ERR("model line scan count error (fscanf returns %d, expected 3)\n", result);
310 return 0;
313 result = fscanf( procfile,
314 " Type:%*3[ ]%32c%*1[ ]ANSI SCSI%*1[ ]revision:%*1[ ]%x\n",
315 dev->type,
316 &dev->ansirev );
317 if( result != 2 )
319 ERR("SCSI type line scan count error (fscanf returns %d, expected 2)\n", result);
320 return 0;
322 /* Since we fscanf with %XXc instead of %s.. put a NULL at end */
323 dev->vendor[8] = 0;
324 dev->model[16] = 0;
325 dev->rev[4] = 0;
326 dev->type[32] = 0;
328 return 1;
332 /* create the hardware registry branch */
333 static void create_hardware_branch(void)
335 /* The following mostly will work on Linux, but should not cause
336 * problems on other systems. */
337 static const char procname_ide_media[] = "/proc/ide/%s/media";
338 static const char procname_ide_model[] = "/proc/ide/%s/model";
339 static const char procname_scsi[] = "/proc/scsi/scsi";
340 DIR *idedir;
341 struct dirent *dent = NULL;
342 FILE *procfile = NULL;
343 char cStr[40], cDevModel[40], cUnixDeviceName[40], read1[10] = "\0", read2[10] = "\0";
344 SCSI_ADDRESS scsi_addr;
345 UINT nType;
346 struct LinuxProcScsiDevice dev;
347 int result = 0, nSgNumber = 0;
348 UCHAR uFirstSCSIPort = 0;
350 /* Enumerate all ide devices first */
351 idedir = opendir("/proc/ide");
352 if (idedir)
354 while ((dent = readdir(idedir)))
356 if (strncmp(dent->d_name, "hd", 2) == 0)
358 sprintf(cStr, procname_ide_media, dent->d_name);
359 procfile = fopen(cStr, "r");
360 if (!procfile)
362 ERR("Could not open %s\n", cStr);
363 continue;
364 } else {
365 fgets(cStr, sizeof(cStr), procfile);
366 fclose(procfile);
367 nType = DRIVE_UNKNOWN;
368 if (strncasecmp(cStr, "disk", 4) == 0) nType = DRIVE_FIXED;
369 if (strncasecmp(cStr, "cdrom", 5) == 0) nType = DRIVE_CDROM;
371 if (nType == DRIVE_UNKNOWN) continue;
374 sprintf(cStr, procname_ide_model, dent->d_name);
375 procfile = fopen(cStr, "r");
376 if (!procfile)
378 ERR("Could not open %s\n", cStr);
379 switch (nType)
381 case DRIVE_FIXED: strcpy(cDevModel, "Wine harddisk"); break;
382 case DRIVE_CDROM: strcpy(cDevModel, "Wine CDROM"); break;
384 } else {
385 fgets(cDevModel, sizeof(cDevModel), procfile);
386 fclose(procfile);
387 cDevModel[strlen(cDevModel) - 1] = 0;
390 sprintf(cUnixDeviceName, "/dev/%s", dent->d_name);
391 scsi_addr.PortNumber = (dent->d_name[2] - 'a') / 2;
392 scsi_addr.PathId = 0;
393 scsi_addr.TargetId = (dent->d_name[2] - 'a') % 2;
394 scsi_addr.Lun = 0;
395 if (scsi_addr.PortNumber + 1 > uFirstSCSIPort)
396 uFirstSCSIPort = scsi_addr.PortNumber + 1;
398 create_scsi_entry(&scsi_addr, "atapi", nType, cDevModel, cUnixDeviceName);
401 closedir(idedir);
404 /* Now goes SCSI */
405 procfile = fopen(procname_scsi, "r");
406 if (!procfile)
408 TRACE("Could not open %s\n", procname_scsi);
409 return;
411 fgets(cStr, 40, procfile);
412 sscanf(cStr, "Attached %9s %9s", read1, read2);
414 if (strcmp(read1, "devices:") != 0)
416 WARN("Incorrect %s format\n", procname_scsi);
417 fclose( procfile );
418 return;
420 if (strcmp(read2, "none") == 0)
422 TRACE("No SCSI devices found in %s.\n", procname_scsi);
423 fclose( procfile );
424 return;
427 /* Read info for one device */
428 while ((result = SCSI_getprocentry(procfile, &dev)) > 0)
430 scsi_addr.PortNumber = dev.host;
431 scsi_addr.PathId = dev.channel;
432 scsi_addr.TargetId = dev.target;
433 scsi_addr.Lun = dev.lun;
435 scsi_addr.PortNumber += uFirstSCSIPort;
436 if (strncmp(dev.type, "Direct-Access", 13) == 0) nType = DRIVE_FIXED;
437 else if (strncmp(dev.type, "Sequential-Access", 17) == 0) nType = DRIVE_REMOVABLE;
438 else if (strncmp(dev.type, "CD-ROM", 6) == 0) nType = DRIVE_CDROM;
439 else if (strncmp(dev.type, "Processor", 9) == 0) nType = DRIVE_NO_ROOT_DIR;
440 else continue;
442 strcpy(cDevModel, dev.vendor);
443 strcat(cDevModel, dev.model);
444 strcat(cDevModel, dev.rev);
445 sprintf(cUnixDeviceName, "/dev/sg%d", nSgNumber++);
446 /* FIXME: get real driver name */
447 create_scsi_entry(&scsi_addr, "WINE SCSI", nType, cDevModel, cUnixDeviceName);
449 if( result != EOF )
450 WARN("Incorrect %s format\n", procname_scsi);
451 fclose( procfile );
455 /***********************************************************************
456 * convert_old_config
458 void convert_old_config(void)
460 if (allocate_default_keys() == REG_OPENED_EXISTING_KEY)
461 return; /* someone else already loaded the registry */
463 /* create some hardware keys (FIXME: should not be done here) */
464 create_hardware_branch();