comctl32/tests: Basic test for PGN_CALCSIZE.
[wine.git] / dlls / kernel32 / oldconfig.c
blob6c80dc614f0c9bcfe9da3adf3011abf80dc76099
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 #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);
56 static NTSTATUS create_key( HANDLE root, const char *name, HANDLE *key, DWORD *disp )
58 OBJECT_ATTRIBUTES attr;
59 UNICODE_STRING nameW;
60 NTSTATUS status;
62 attr.Length = sizeof(attr);
63 attr.RootDirectory = root;
64 attr.ObjectName = &nameW;
65 attr.Attributes = 0;
66 attr.SecurityDescriptor = NULL;
67 attr.SecurityQualityOfService = NULL;
69 if (!RtlCreateUnicodeStringFromAsciiz( &nameW, name )) return STATUS_NO_MEMORY;
70 status = NtCreateKey( key, KEY_ALL_ACCESS, &attr, 0, NULL, REG_OPTION_VOLATILE, disp );
71 if (status) ERR("Cannot create %s registry key\n", name );
72 RtlFreeUnicodeString( &nameW );
73 return status;
76 /******************************************************************
77 * create_scsi_entry
79 * Initializes registry to contain scsi info about the cdrom in NT.
80 * All devices (even not real scsi ones) have this info in NT.
81 * NOTE: programs usually read these registry entries after sending the
82 * IOCTL_SCSI_GET_ADDRESS ioctl to the cdrom
84 static void create_scsi_entry( PSCSI_ADDRESS scsi_addr, LPCSTR lpDriver, UINT uDriveType,
85 LPSTR lpDriveName, LPSTR lpUnixDeviceName )
87 static UCHAR uCdromNumber = 0;
88 static UCHAR uTapeNumber = 0;
90 UNICODE_STRING nameW;
91 WCHAR dataW[50];
92 DWORD sizeW;
93 char buffer[40];
94 DWORD value;
95 const char *data;
96 HANDLE scsiKey;
97 HANDLE portKey;
98 HANDLE busKey;
99 HANDLE targetKey;
100 HANDLE lunKey;
101 DWORD disp;
103 if (create_key( 0, "\\Registry\\Machine\\HARDWARE\\DEVICEMAP", &scsiKey, &disp )) return;
104 NtClose( scsiKey );
106 /* Ensure there is Scsi key */
107 if (create_key( 0, "\\Registry\\Machine\\HARDWARE\\DEVICEMAP\\Scsi", &scsiKey, &disp )) return;
109 snprintf(buffer,sizeof(buffer),"Scsi Port %d",scsi_addr->PortNumber);
110 if (create_key( scsiKey, buffer, &portKey, &disp )) return;
112 RtlCreateUnicodeStringFromAsciiz( &nameW, "Driver" );
113 RtlMultiByteToUnicodeN( dataW, sizeof(dataW), &sizeW, lpDriver, strlen(lpDriver)+1);
114 NtSetValueKey( portKey, &nameW, 0, REG_SZ, dataW, sizeW );
115 RtlFreeUnicodeString( &nameW );
116 value = 10;
117 RtlCreateUnicodeStringFromAsciiz( &nameW, "FirstBusTimeScanInMs" );
118 NtSetValueKey( portKey,&nameW, 0, REG_DWORD, &value, sizeof(DWORD) );
119 RtlFreeUnicodeString( &nameW );
121 value = 0;
122 if (strcmp(lpDriver, "atapi") == 0)
124 #ifdef HDIO_GET_DMA
125 int fd, dma;
127 fd = open(lpUnixDeviceName, O_RDONLY|O_NONBLOCK);
128 if (fd != -1)
130 if (ioctl(fd, HDIO_GET_DMA, &dma) != -1) value = dma;
131 close(fd);
133 #endif
134 RtlCreateUnicodeStringFromAsciiz( &nameW, "DMAEnabled" );
135 NtSetValueKey( portKey,&nameW, 0, REG_DWORD, &value, sizeof(DWORD) );
136 RtlFreeUnicodeString( &nameW );
139 snprintf(buffer, sizeof(buffer),"Scsi Bus %d", scsi_addr->PathId);
140 if (create_key( portKey, buffer, &busKey, &disp )) return;
142 /* FIXME: get real controller Id for SCSI */
143 if (create_key( busKey, buffer, &targetKey, &disp )) return;
144 NtClose( targetKey );
146 snprintf(buffer, sizeof(buffer),"Target Id %d", scsi_addr->TargetId);
147 if (create_key( busKey, buffer, &targetKey, &disp )) return;
149 snprintf(buffer, sizeof(buffer),"Logical Unit Id %d", scsi_addr->Lun);
150 if (create_key( targetKey, buffer, &lunKey, &disp )) return;
152 switch (uDriveType)
154 case DRIVE_NO_ROOT_DIR:
155 default:
156 data = "OtherPeripheral"; break;
157 case DRIVE_FIXED:
158 data = "DiskPeripheral"; break;
159 case DRIVE_REMOVABLE:
160 data = "TapePeripheral";
161 snprintf(buffer, sizeof(buffer), "Tape%d", uTapeNumber++);
162 break;
163 case DRIVE_CDROM:
164 data = "CdRomPeripheral";
165 snprintf(buffer, sizeof(buffer), "Cdrom%d", uCdromNumber++);
166 break;
168 RtlCreateUnicodeStringFromAsciiz( &nameW, "Type" );
169 RtlMultiByteToUnicodeN( dataW, sizeof(dataW), &sizeW, data, strlen(data)+1);
170 NtSetValueKey( lunKey, &nameW, 0, REG_SZ, dataW, sizeW );
171 RtlFreeUnicodeString( &nameW );
173 RtlCreateUnicodeStringFromAsciiz( &nameW, "Identifier" );
174 RtlMultiByteToUnicodeN( dataW, sizeof(dataW), &sizeW, lpDriveName, strlen(lpDriveName)+1);
175 NtSetValueKey( lunKey, &nameW, 0, REG_SZ, dataW, sizeW );
176 RtlFreeUnicodeString( &nameW );
178 if (uDriveType == DRIVE_CDROM || uDriveType == DRIVE_REMOVABLE)
180 RtlCreateUnicodeStringFromAsciiz( &nameW, "DeviceName" );
181 RtlMultiByteToUnicodeN( dataW, sizeof(dataW), &sizeW, buffer, strlen(buffer)+1);
182 NtSetValueKey( lunKey, &nameW, 0, REG_SZ, dataW, sizeW );
183 RtlFreeUnicodeString( &nameW );
186 RtlCreateUnicodeStringFromAsciiz( &nameW, "UnixDeviceName" );
187 RtlMultiByteToUnicodeN( dataW, sizeof(dataW), &sizeW, lpUnixDeviceName, strlen(lpUnixDeviceName)+1);
188 NtSetValueKey( lunKey, &nameW, 0, REG_SZ, dataW, sizeW );
189 RtlFreeUnicodeString( &nameW );
191 NtClose( lunKey );
192 NtClose( targetKey );
193 NtClose( busKey );
194 NtClose( portKey );
195 NtClose( scsiKey );
198 struct LinuxProcScsiDevice
200 int host;
201 int channel;
202 int target;
203 int lun;
204 char vendor[9];
205 char model[17];
206 char rev[5];
207 char type[33];
208 int ansirev;
212 * we need to declare white spaces explicitly via %*1[ ],
213 * as there are very dainbread CD-ROM devices out there
214 * which have their manufacturer name blanked out via spaces,
215 * which confuses fscanf's parsing (skips all blank spaces)
217 static int SCSI_getprocentry( FILE * procfile, struct LinuxProcScsiDevice * dev )
219 int result;
221 result = fscanf( procfile,
222 "Host:%*1[ ]scsi%d%*1[ ]Channel:%*1[ ]%d%*1[ ]Id:%*1[ ]%d%*1[ ]Lun:%*1[ ]%d\n",
223 &dev->host,
224 &dev->channel,
225 &dev->target,
226 &dev->lun );
227 if( result == EOF )
229 /* "end of entries" return, so no TRACE() here */
230 return EOF;
232 if( result != 4 )
234 ERR("bus id line scan count error (fscanf returns %d, expected 4)\n", result);
235 return 0;
237 result = fscanf( procfile,
238 " Vendor:%*1[ ]%8c%*1[ ]Model:%*1[ ]%16c%*1[ ]Rev:%*1[ ]%4c\n",
239 dev->vendor,
240 dev->model,
241 dev->rev );
242 if( result != 3 )
244 ERR("model line scan count error (fscanf returns %d, expected 3)\n", result);
245 return 0;
248 result = fscanf( procfile,
249 " Type:%*3[ ]%32c%*1[ ]ANSI SCSI%*1[ ]revision:%*1[ ]%x\n",
250 dev->type,
251 &dev->ansirev );
252 if( result != 2 )
254 ERR("SCSI type line scan count error (fscanf returns %d, expected 2)\n", result);
255 return 0;
257 /* Since we fscanf with %XXc instead of %s.. put a NULL at end */
258 dev->vendor[8] = 0;
259 dev->model[16] = 0;
260 dev->rev[4] = 0;
261 dev->type[32] = 0;
263 return 1;
267 /* create the hardware registry branch */
268 static void create_hardware_branch(void)
270 /* The following mostly will work on Linux, but should not cause
271 * problems on other systems. */
272 static const char procname_ide_media[] = "/proc/ide/%s/media";
273 static const char procname_ide_model[] = "/proc/ide/%s/model";
274 static const char procname_scsi[] = "/proc/scsi/scsi";
275 DIR *idedir;
276 struct dirent *dent = NULL;
277 FILE *procfile = NULL;
278 char cStr[40], cDevModel[40], cUnixDeviceName[40], read1[10] = "\0", read2[10] = "\0";
279 SCSI_ADDRESS scsi_addr;
280 UINT nType;
281 struct LinuxProcScsiDevice dev;
282 int result = 0, nSgNumber = 0;
283 UCHAR uFirstSCSIPort = 0;
285 /* Enumerate all ide devices first */
286 idedir = opendir("/proc/ide");
287 if (idedir)
289 while ((dent = readdir(idedir)))
291 if (strncmp(dent->d_name, "hd", 2) == 0)
293 sprintf(cStr, procname_ide_media, dent->d_name);
294 procfile = fopen(cStr, "r");
295 if (!procfile)
297 ERR("Could not open %s\n", cStr);
298 continue;
299 } else {
300 fgets(cStr, sizeof(cStr), procfile);
301 fclose(procfile);
302 nType = DRIVE_UNKNOWN;
303 if (strncasecmp(cStr, "disk", 4) == 0) nType = DRIVE_FIXED;
304 if (strncasecmp(cStr, "cdrom", 5) == 0) nType = DRIVE_CDROM;
306 if (nType == DRIVE_UNKNOWN) continue;
309 sprintf(cStr, procname_ide_model, dent->d_name);
310 procfile = fopen(cStr, "r");
311 if (!procfile)
313 ERR("Could not open %s\n", cStr);
314 switch (nType)
316 case DRIVE_FIXED: strcpy(cDevModel, "Wine harddisk"); break;
317 case DRIVE_CDROM: strcpy(cDevModel, "Wine CDROM"); break;
319 } else {
320 fgets(cDevModel, sizeof(cDevModel), procfile);
321 fclose(procfile);
322 cDevModel[strlen(cDevModel) - 1] = 0;
325 sprintf(cUnixDeviceName, "/dev/%s", dent->d_name);
326 scsi_addr.PortNumber = (dent->d_name[2] - 'a') / 2;
327 scsi_addr.PathId = 0;
328 scsi_addr.TargetId = (dent->d_name[2] - 'a') % 2;
329 scsi_addr.Lun = 0;
330 if (scsi_addr.PortNumber + 1 > uFirstSCSIPort)
331 uFirstSCSIPort = scsi_addr.PortNumber + 1;
333 create_scsi_entry(&scsi_addr, "atapi", nType, cDevModel, cUnixDeviceName);
336 closedir(idedir);
339 /* Now goes SCSI */
340 procfile = fopen(procname_scsi, "r");
341 if (!procfile)
343 TRACE("Could not open %s\n", procname_scsi);
344 return;
346 fgets(cStr, 40, procfile);
347 sscanf(cStr, "Attached %9s %9s", read1, read2);
349 if (strcmp(read1, "devices:") != 0)
351 WARN("Incorrect %s format\n", procname_scsi);
352 fclose( procfile );
353 return;
355 if (strcmp(read2, "none") == 0)
357 TRACE("No SCSI devices found in %s.\n", procname_scsi);
358 fclose( procfile );
359 return;
362 /* Read info for one device */
363 while ((result = SCSI_getprocentry(procfile, &dev)) > 0)
365 scsi_addr.PortNumber = dev.host;
366 scsi_addr.PathId = dev.channel;
367 scsi_addr.TargetId = dev.target;
368 scsi_addr.Lun = dev.lun;
370 scsi_addr.PortNumber += uFirstSCSIPort;
371 if (strncmp(dev.type, "Direct-Access", 13) == 0) nType = DRIVE_FIXED;
372 else if (strncmp(dev.type, "Sequential-Access", 17) == 0) nType = DRIVE_REMOVABLE;
373 else if (strncmp(dev.type, "CD-ROM", 6) == 0) nType = DRIVE_CDROM;
374 else if (strncmp(dev.type, "Processor", 9) == 0) nType = DRIVE_NO_ROOT_DIR;
375 else if (strncmp(dev.type, "Scanner", 7) == 0) nType = DRIVE_NO_ROOT_DIR;
376 else if (strncmp(dev.type, "Printer", 7) == 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, "\\Registry\\Machine\\HARDWARE", &key, &disp )) return;
402 NtClose( key );
403 if (disp != REG_OPENED_EXISTING_KEY) create_hardware_branch();