push b8b0d21e31a2078e72f3e29b2ec7fde29873cc3e
[wine/hacks.git] / dlls / kernel32 / oldconfig.c
blob02dac06f7c4b2deb2a4b3d43c394448a7b9d150a
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 static NTSTATUS create_key( HANDLE root, const char *name, HANDLE *key, DWORD *disp )
60 OBJECT_ATTRIBUTES attr;
61 UNICODE_STRING nameW;
62 NTSTATUS status;
64 attr.Length = sizeof(attr);
65 attr.RootDirectory = root;
66 attr.ObjectName = &nameW;
67 attr.Attributes = 0;
68 attr.SecurityDescriptor = NULL;
69 attr.SecurityQualityOfService = NULL;
71 if (!RtlCreateUnicodeStringFromAsciiz( &nameW, name )) return STATUS_NO_MEMORY;
72 status = NtCreateKey( key, KEY_ALL_ACCESS, &attr, 0, NULL, REG_OPTION_VOLATILE, disp );
73 if (status) ERR("Cannot create %s registry key\n", name );
74 RtlFreeUnicodeString( &nameW );
75 return status;
78 /******************************************************************
79 * create_scsi_entry
81 * Initializes registry to contain scsi info about the cdrom in NT.
82 * All devices (even not real scsi ones) have this info in NT.
83 * NOTE: programs usually read these registry entries after sending the
84 * IOCTL_SCSI_GET_ADDRESS ioctl to the cdrom
86 static void create_scsi_entry( PSCSI_ADDRESS scsi_addr, LPCSTR lpDriver, UINT uDriveType,
87 LPSTR lpDriveName, LPSTR lpUnixDeviceName )
89 static UCHAR uCdromNumber = 0;
90 static UCHAR uTapeNumber = 0;
92 UNICODE_STRING nameW;
93 WCHAR dataW[50];
94 DWORD sizeW;
95 char buffer[40];
96 DWORD value;
97 const char *data;
98 HANDLE scsiKey;
99 HANDLE portKey;
100 HANDLE busKey;
101 HANDLE targetKey;
102 HANDLE lunKey;
103 DWORD disp;
105 if (create_key( 0, "Machine\\HARDWARE\\DEVICEMAP", &scsiKey, &disp )) return;
106 NtClose( scsiKey );
108 /* Ensure there is Scsi key */
109 if (create_key( 0, "Machine\\HARDWARE\\DEVICEMAP\\Scsi", &scsiKey, &disp )) return;
111 snprintf(buffer,sizeof(buffer),"Scsi Port %d",scsi_addr->PortNumber);
112 if (create_key( scsiKey, buffer, &portKey, &disp )) return;
114 RtlCreateUnicodeStringFromAsciiz( &nameW, "Driver" );
115 RtlMultiByteToUnicodeN( dataW, sizeof(dataW), &sizeW, lpDriver, strlen(lpDriver)+1);
116 NtSetValueKey( portKey, &nameW, 0, REG_SZ, dataW, sizeW );
117 RtlFreeUnicodeString( &nameW );
118 value = 10;
119 RtlCreateUnicodeStringFromAsciiz( &nameW, "FirstBusTimeScanInMs" );
120 NtSetValueKey( portKey,&nameW, 0, REG_DWORD, &value, sizeof(DWORD) );
121 RtlFreeUnicodeString( &nameW );
123 value = 0;
124 if (strcmp(lpDriver, "atapi") == 0)
126 #ifdef HDIO_GET_DMA
127 int fd, dma;
129 fd = open(lpUnixDeviceName, O_RDONLY|O_NONBLOCK);
130 if (fd != -1)
132 if (ioctl(fd, HDIO_GET_DMA, &dma) != -1) value = dma;
133 close(fd);
135 #endif
136 RtlCreateUnicodeStringFromAsciiz( &nameW, "DMAEnabled" );
137 NtSetValueKey( portKey,&nameW, 0, REG_DWORD, &value, sizeof(DWORD) );
138 RtlFreeUnicodeString( &nameW );
141 snprintf(buffer, sizeof(buffer),"Scsi Bus %d", scsi_addr->PathId);
142 if (create_key( portKey, buffer, &busKey, &disp )) return;
144 /* FIXME: get real controller Id for SCSI */
145 if (create_key( busKey, buffer, &targetKey, &disp )) return;
146 NtClose( targetKey );
148 snprintf(buffer, sizeof(buffer),"Target Id %d", scsi_addr->TargetId);
149 if (create_key( busKey, buffer, &targetKey, &disp )) return;
151 snprintf(buffer, sizeof(buffer),"Logical Unit Id %d", scsi_addr->Lun);
152 if (create_key( targetKey, buffer, &lunKey, &disp )) return;
154 switch (uDriveType)
156 case DRIVE_NO_ROOT_DIR:
157 default:
158 data = "OtherPeripheral"; break;
159 case DRIVE_FIXED:
160 data = "DiskPeripheral"; break;
161 case DRIVE_REMOVABLE:
162 data = "TapePeripheral";
163 snprintf(buffer, sizeof(buffer), "Tape%d", uTapeNumber++);
164 break;
165 case DRIVE_CDROM:
166 data = "CdRomPeripheral";
167 snprintf(buffer, sizeof(buffer), "Cdrom%d", uCdromNumber++);
168 break;
170 RtlCreateUnicodeStringFromAsciiz( &nameW, "Type" );
171 RtlMultiByteToUnicodeN( dataW, sizeof(dataW), &sizeW, data, strlen(data)+1);
172 NtSetValueKey( lunKey, &nameW, 0, REG_SZ, dataW, sizeW );
173 RtlFreeUnicodeString( &nameW );
175 RtlCreateUnicodeStringFromAsciiz( &nameW, "Identifier" );
176 RtlMultiByteToUnicodeN( dataW, sizeof(dataW), &sizeW, lpDriveName, strlen(lpDriveName)+1);
177 NtSetValueKey( lunKey, &nameW, 0, REG_SZ, dataW, sizeW );
178 RtlFreeUnicodeString( &nameW );
180 if (uDriveType == DRIVE_CDROM || uDriveType == DRIVE_REMOVABLE)
182 RtlCreateUnicodeStringFromAsciiz( &nameW, "DeviceName" );
183 RtlMultiByteToUnicodeN( dataW, sizeof(dataW), &sizeW, buffer, strlen(buffer)+1);
184 NtSetValueKey( lunKey, &nameW, 0, REG_SZ, dataW, sizeW );
185 RtlFreeUnicodeString( &nameW );
188 RtlCreateUnicodeStringFromAsciiz( &nameW, "UnixDeviceName" );
189 RtlMultiByteToUnicodeN( dataW, sizeof(dataW), &sizeW, lpUnixDeviceName, strlen(lpUnixDeviceName)+1);
190 NtSetValueKey( lunKey, &nameW, 0, REG_SZ, dataW, sizeW );
191 RtlFreeUnicodeString( &nameW );
193 NtClose( lunKey );
194 NtClose( targetKey );
195 NtClose( busKey );
196 NtClose( portKey );
197 NtClose( scsiKey );
200 struct LinuxProcScsiDevice
202 int host;
203 int channel;
204 int target;
205 int lun;
206 char vendor[9];
207 char model[17];
208 char rev[5];
209 char type[33];
210 int ansirev;
214 * we need to declare white spaces explicitly via %*1[ ],
215 * as there are very dainbread CD-ROM devices out there
216 * which have their manufacturer name blanked out via spaces,
217 * which confuses fscanf's parsing (skips all blank spaces)
219 static int SCSI_getprocentry( FILE * procfile, struct LinuxProcScsiDevice * dev )
221 int result;
223 result = fscanf( procfile,
224 "Host:%*1[ ]scsi%d%*1[ ]Channel:%*1[ ]%d%*1[ ]Id:%*1[ ]%d%*1[ ]Lun:%*1[ ]%d\n",
225 &dev->host,
226 &dev->channel,
227 &dev->target,
228 &dev->lun );
229 if( result == EOF )
231 /* "end of entries" return, so no TRACE() here */
232 return EOF;
234 if( result != 4 )
236 ERR("bus id line scan count error (fscanf returns %d, expected 4)\n", result);
237 return 0;
239 result = fscanf( procfile,
240 " Vendor:%*1[ ]%8c%*1[ ]Model:%*1[ ]%16c%*1[ ]Rev:%*1[ ]%4c\n",
241 dev->vendor,
242 dev->model,
243 dev->rev );
244 if( result != 3 )
246 ERR("model line scan count error (fscanf returns %d, expected 3)\n", result);
247 return 0;
250 result = fscanf( procfile,
251 " Type:%*3[ ]%32c%*1[ ]ANSI SCSI%*1[ ]revision:%*1[ ]%x\n",
252 dev->type,
253 &dev->ansirev );
254 if( result != 2 )
256 ERR("SCSI type line scan count error (fscanf returns %d, expected 2)\n", result);
257 return 0;
259 /* Since we fscanf with %XXc instead of %s.. put a NULL at end */
260 dev->vendor[8] = 0;
261 dev->model[16] = 0;
262 dev->rev[4] = 0;
263 dev->type[32] = 0;
265 return 1;
269 /* create the hardware registry branch */
270 static void create_hardware_branch(void)
272 /* The following mostly will work on Linux, but should not cause
273 * problems on other systems. */
274 static const char procname_ide_media[] = "/proc/ide/%s/media";
275 static const char procname_ide_model[] = "/proc/ide/%s/model";
276 static const char procname_scsi[] = "/proc/scsi/scsi";
277 DIR *idedir;
278 struct dirent *dent = NULL;
279 FILE *procfile = NULL;
280 char cStr[40], cDevModel[40], cUnixDeviceName[40], read1[10] = "\0", read2[10] = "\0";
281 SCSI_ADDRESS scsi_addr;
282 UINT nType;
283 struct LinuxProcScsiDevice dev;
284 int result = 0, nSgNumber = 0;
285 UCHAR uFirstSCSIPort = 0;
287 /* Enumerate all ide devices first */
288 idedir = opendir("/proc/ide");
289 if (idedir)
291 while ((dent = readdir(idedir)))
293 if (strncmp(dent->d_name, "hd", 2) == 0)
295 sprintf(cStr, procname_ide_media, dent->d_name);
296 procfile = fopen(cStr, "r");
297 if (!procfile)
299 ERR("Could not open %s\n", cStr);
300 continue;
301 } else {
302 fgets(cStr, sizeof(cStr), procfile);
303 fclose(procfile);
304 nType = DRIVE_UNKNOWN;
305 if (strncasecmp(cStr, "disk", 4) == 0) nType = DRIVE_FIXED;
306 if (strncasecmp(cStr, "cdrom", 5) == 0) nType = DRIVE_CDROM;
308 if (nType == DRIVE_UNKNOWN) continue;
311 sprintf(cStr, procname_ide_model, dent->d_name);
312 procfile = fopen(cStr, "r");
313 if (!procfile)
315 ERR("Could not open %s\n", cStr);
316 switch (nType)
318 case DRIVE_FIXED: strcpy(cDevModel, "Wine harddisk"); break;
319 case DRIVE_CDROM: strcpy(cDevModel, "Wine CDROM"); break;
321 } else {
322 fgets(cDevModel, sizeof(cDevModel), procfile);
323 fclose(procfile);
324 cDevModel[strlen(cDevModel) - 1] = 0;
327 sprintf(cUnixDeviceName, "/dev/%s", dent->d_name);
328 scsi_addr.PortNumber = (dent->d_name[2] - 'a') / 2;
329 scsi_addr.PathId = 0;
330 scsi_addr.TargetId = (dent->d_name[2] - 'a') % 2;
331 scsi_addr.Lun = 0;
332 if (scsi_addr.PortNumber + 1 > uFirstSCSIPort)
333 uFirstSCSIPort = scsi_addr.PortNumber + 1;
335 create_scsi_entry(&scsi_addr, "atapi", nType, cDevModel, cUnixDeviceName);
338 closedir(idedir);
341 /* Now goes SCSI */
342 procfile = fopen(procname_scsi, "r");
343 if (!procfile)
345 TRACE("Could not open %s\n", procname_scsi);
346 return;
348 fgets(cStr, 40, procfile);
349 sscanf(cStr, "Attached %9s %9s", read1, read2);
351 if (strcmp(read1, "devices:") != 0)
353 WARN("Incorrect %s format\n", procname_scsi);
354 fclose( procfile );
355 return;
357 if (strcmp(read2, "none") == 0)
359 TRACE("No SCSI devices found in %s.\n", procname_scsi);
360 fclose( procfile );
361 return;
364 /* Read info for one device */
365 while ((result = SCSI_getprocentry(procfile, &dev)) > 0)
367 scsi_addr.PortNumber = dev.host;
368 scsi_addr.PathId = dev.channel;
369 scsi_addr.TargetId = dev.target;
370 scsi_addr.Lun = dev.lun;
372 scsi_addr.PortNumber += uFirstSCSIPort;
373 if (strncmp(dev.type, "Direct-Access", 13) == 0) nType = DRIVE_FIXED;
374 else if (strncmp(dev.type, "Sequential-Access", 17) == 0) nType = DRIVE_REMOVABLE;
375 else if (strncmp(dev.type, "CD-ROM", 6) == 0) nType = DRIVE_CDROM;
376 else if (strncmp(dev.type, "Processor", 9) == 0) nType = DRIVE_NO_ROOT_DIR;
377 else continue;
379 strcpy(cDevModel, dev.vendor);
380 strcat(cDevModel, dev.model);
381 strcat(cDevModel, dev.rev);
382 sprintf(cUnixDeviceName, "/dev/sg%d", nSgNumber++);
383 /* FIXME: get real driver name */
384 create_scsi_entry(&scsi_addr, "WINE SCSI", nType, cDevModel, cUnixDeviceName);
386 if( result != EOF )
387 WARN("Incorrect %s format\n", procname_scsi);
388 fclose( procfile );
392 /***********************************************************************
393 * convert_old_config
395 void convert_old_config(void)
397 HANDLE key;
398 DWORD disp;
400 /* create some hardware keys (FIXME: should not be done here) */
401 if (create_key( 0, "Machine\\HARDWARE", &key, &disp )) return;
402 NtClose( key );
403 if (disp != REG_OPENED_EXISTING_KEY) create_hardware_branch();