mountmgr: Symlink WoW64 Wine ports key to regular Wine ports key.
[wine.git] / dlls / mountmgr.sys / mountmgr.c
blobb792be4bea0dc965e2a09f36548d7eb818b76748
1 /*
2 * Mount manager service implementation
4 * Copyright 2008 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 #include <stdarg.h>
22 #include <unistd.h>
24 #define NONAMELESSUNION
26 #include "mountmgr.h"
27 #include "winreg.h"
28 #include "wine/library.h"
29 #include "wine/list.h"
30 #include "wine/unicode.h"
31 #include "wine/debug.h"
33 WINE_DEFAULT_DEBUG_CHANNEL(mountmgr);
35 #define MIN_ID_LEN 4
37 struct mount_point
39 struct list entry; /* entry in mount points list */
40 DEVICE_OBJECT *device; /* disk device */
41 UNICODE_STRING name; /* device name */
42 UNICODE_STRING link; /* DOS device symlink */
43 void *id; /* device unique id */
44 unsigned int id_len;
47 static struct list mount_points_list = LIST_INIT(mount_points_list);
48 static HKEY mount_key;
50 void set_mount_point_id( struct mount_point *mount, const void *id, unsigned int id_len )
52 RtlFreeHeap( GetProcessHeap(), 0, mount->id );
53 mount->id_len = max( MIN_ID_LEN, id_len );
54 if ((mount->id = RtlAllocateHeap( GetProcessHeap(), HEAP_ZERO_MEMORY, mount->id_len )))
56 memcpy( mount->id, id, id_len );
57 RegSetValueExW( mount_key, mount->link.Buffer, 0, REG_BINARY, mount->id, mount->id_len );
59 else mount->id_len = 0;
62 static struct mount_point *add_mount_point( DEVICE_OBJECT *device, UNICODE_STRING *device_name,
63 const WCHAR *link )
65 struct mount_point *mount;
66 WCHAR *str;
67 UINT len = (strlenW(link) + 1) * sizeof(WCHAR) + device_name->Length + sizeof(WCHAR);
69 if (!(mount = RtlAllocateHeap( GetProcessHeap(), 0, sizeof(*mount) + len ))) return NULL;
71 str = (WCHAR *)(mount + 1);
72 strcpyW( str, link );
73 RtlInitUnicodeString( &mount->link, str );
74 str += strlenW(str) + 1;
75 memcpy( str, device_name->Buffer, device_name->Length );
76 str[device_name->Length / sizeof(WCHAR)] = 0;
77 mount->name.Buffer = str;
78 mount->name.Length = device_name->Length;
79 mount->name.MaximumLength = device_name->Length + sizeof(WCHAR);
80 mount->device = device;
81 mount->id = NULL;
82 list_add_tail( &mount_points_list, &mount->entry );
84 IoCreateSymbolicLink( &mount->link, device_name );
86 TRACE( "created %s id %s for %s\n", debugstr_w(mount->link.Buffer),
87 debugstr_a(mount->id), debugstr_w(mount->name.Buffer) );
88 return mount;
91 /* create the DosDevices mount point symlink for a new device */
92 struct mount_point *add_dosdev_mount_point( DEVICE_OBJECT *device, UNICODE_STRING *device_name, int drive )
94 static const WCHAR driveW[] = {'\\','D','o','s','D','e','v','i','c','e','s','\\','%','c',':',0};
95 WCHAR link[sizeof(driveW)];
97 sprintfW( link, driveW, 'A' + drive );
98 return add_mount_point( device, device_name, link );
101 /* create the Volume mount point symlink for a new device */
102 struct mount_point *add_volume_mount_point( DEVICE_OBJECT *device, UNICODE_STRING *device_name,
103 const GUID *guid )
105 static const WCHAR volumeW[] = {'\\','?','?','\\','V','o','l','u','m','e','{',
106 '%','0','8','x','-','%','0','4','x','-','%','0','4','x','-',
107 '%','0','2','x','%','0','2','x','-','%','0','2','x','%','0','2','x',
108 '%','0','2','x','%','0','2','x','%','0','2','x','%','0','2','x','}',0};
109 WCHAR link[sizeof(volumeW)];
111 sprintfW( link, volumeW, guid->Data1, guid->Data2, guid->Data3,
112 guid->Data4[0], guid->Data4[1], guid->Data4[2], guid->Data4[3],
113 guid->Data4[4], guid->Data4[5], guid->Data4[6], guid->Data4[7]);
114 return add_mount_point( device, device_name, link );
117 /* delete the mount point symlinks when a device goes away */
118 void delete_mount_point( struct mount_point *mount )
120 TRACE( "deleting %s\n", debugstr_w(mount->link.Buffer) );
121 list_remove( &mount->entry );
122 RegDeleteValueW( mount_key, mount->link.Buffer );
123 IoDeleteSymbolicLink( &mount->link );
124 RtlFreeHeap( GetProcessHeap(), 0, mount->id );
125 RtlFreeHeap( GetProcessHeap(), 0, mount );
128 /* check if a given mount point matches the requested specs */
129 static BOOL matching_mount_point( const struct mount_point *mount, const MOUNTMGR_MOUNT_POINT *spec )
131 if (spec->SymbolicLinkNameOffset)
133 const WCHAR *name = (const WCHAR *)((const char *)spec + spec->SymbolicLinkNameOffset);
134 if (spec->SymbolicLinkNameLength != mount->link.Length) return FALSE;
135 if (memicmpW( name, mount->link.Buffer, mount->link.Length/sizeof(WCHAR)))
136 return FALSE;
138 if (spec->DeviceNameOffset)
140 const WCHAR *name = (const WCHAR *)((const char *)spec + spec->DeviceNameOffset);
141 if (spec->DeviceNameLength != mount->name.Length) return FALSE;
142 if (memicmpW( name, mount->name.Buffer, mount->name.Length/sizeof(WCHAR)))
143 return FALSE;
145 if (spec->UniqueIdOffset)
147 const void *id = ((const char *)spec + spec->UniqueIdOffset);
148 if (spec->UniqueIdLength != mount->id_len) return FALSE;
149 if (memcmp( id, mount->id, mount->id_len )) return FALSE;
151 return TRUE;
154 /* implementation of IOCTL_MOUNTMGR_QUERY_POINTS */
155 static NTSTATUS query_mount_points( void *buff, SIZE_T insize,
156 SIZE_T outsize, IO_STATUS_BLOCK *iosb )
158 UINT count, pos, size;
159 MOUNTMGR_MOUNT_POINT *input = buff;
160 MOUNTMGR_MOUNT_POINTS *info;
161 struct mount_point *mount;
163 /* sanity checks */
164 if (input->SymbolicLinkNameOffset + input->SymbolicLinkNameLength > insize ||
165 input->UniqueIdOffset + input->UniqueIdLength > insize ||
166 input->DeviceNameOffset + input->DeviceNameLength > insize ||
167 input->SymbolicLinkNameOffset + input->SymbolicLinkNameLength < input->SymbolicLinkNameOffset ||
168 input->UniqueIdOffset + input->UniqueIdLength < input->UniqueIdOffset ||
169 input->DeviceNameOffset + input->DeviceNameLength < input->DeviceNameOffset)
170 return STATUS_INVALID_PARAMETER;
172 count = size = 0;
173 LIST_FOR_EACH_ENTRY( mount, &mount_points_list, struct mount_point, entry )
175 if (!matching_mount_point( mount, input )) continue;
176 size += mount->name.Length;
177 size += mount->link.Length;
178 size += mount->id_len;
179 size = (size + sizeof(WCHAR) - 1) & ~(sizeof(WCHAR) - 1);
180 count++;
182 pos = FIELD_OFFSET( MOUNTMGR_MOUNT_POINTS, MountPoints[count] );
183 size += pos;
185 if (size > outsize)
187 info = buff;
188 if (size >= sizeof(info->Size)) info->Size = size;
189 iosb->Information = sizeof(info->Size);
190 return STATUS_MORE_ENTRIES;
193 input = HeapAlloc( GetProcessHeap(), 0, insize );
194 if (!input)
195 return STATUS_NO_MEMORY;
196 memcpy( input, buff, insize );
197 info = buff;
199 info->NumberOfMountPoints = count;
200 count = 0;
201 LIST_FOR_EACH_ENTRY( mount, &mount_points_list, struct mount_point, entry )
203 if (!matching_mount_point( mount, input )) continue;
205 info->MountPoints[count].DeviceNameOffset = pos;
206 info->MountPoints[count].DeviceNameLength = mount->name.Length;
207 memcpy( (char *)buff + pos, mount->name.Buffer, mount->name.Length );
208 pos += mount->name.Length;
210 info->MountPoints[count].SymbolicLinkNameOffset = pos;
211 info->MountPoints[count].SymbolicLinkNameLength = mount->link.Length;
212 memcpy( (char *)buff + pos, mount->link.Buffer, mount->link.Length );
213 pos += mount->link.Length;
215 info->MountPoints[count].UniqueIdOffset = pos;
216 info->MountPoints[count].UniqueIdLength = mount->id_len;
217 memcpy( (char *)buff + pos, mount->id, mount->id_len );
218 pos += mount->id_len;
219 pos = (pos + sizeof(WCHAR) - 1) & ~(sizeof(WCHAR) - 1);
220 count++;
222 info->Size = pos;
223 iosb->Information = pos;
224 HeapFree( GetProcessHeap(), 0, input );
225 return STATUS_SUCCESS;
228 /* implementation of IOCTL_MOUNTMGR_DEFINE_UNIX_DRIVE */
229 static NTSTATUS define_unix_drive( const void *in_buff, SIZE_T insize )
231 const struct mountmgr_unix_drive *input = in_buff;
232 const char *mount_point = NULL, *device = NULL;
233 unsigned int i;
234 WCHAR letter = tolowerW( input->letter );
236 if (letter < 'a' || letter > 'z') return STATUS_INVALID_PARAMETER;
237 if (input->type > DRIVE_RAMDISK) return STATUS_INVALID_PARAMETER;
238 if (input->mount_point_offset > insize || input->device_offset > insize)
239 return STATUS_INVALID_PARAMETER;
241 /* make sure string are null-terminated */
242 if (input->mount_point_offset)
244 mount_point = (const char *)in_buff + input->mount_point_offset;
245 for (i = input->mount_point_offset; i < insize; i++)
246 if (!*((const char *)in_buff + i)) break;
247 if (i >= insize) return STATUS_INVALID_PARAMETER;
249 if (input->device_offset)
251 device = (const char *)in_buff + input->device_offset;
252 for (i = input->device_offset; i < insize; i++)
253 if (!*((const char *)in_buff + i)) break;
254 if (i >= insize) return STATUS_INVALID_PARAMETER;
257 if (input->type != DRIVE_NO_ROOT_DIR)
259 enum device_type type = DEVICE_UNKNOWN;
261 TRACE( "defining %c: dev %s mount %s type %u\n",
262 letter, debugstr_a(device), debugstr_a(mount_point), input->type );
263 switch (input->type)
265 case DRIVE_REMOVABLE: type = (letter >= 'c') ? DEVICE_HARDDISK : DEVICE_FLOPPY; break;
266 case DRIVE_REMOTE: type = DEVICE_NETWORK; break;
267 case DRIVE_CDROM: type = DEVICE_CDROM; break;
268 case DRIVE_RAMDISK: type = DEVICE_RAMDISK; break;
269 case DRIVE_FIXED: type = DEVICE_HARDDISK_VOL; break;
271 return add_dos_device( letter - 'a', NULL, device, mount_point, type, NULL );
273 else
275 TRACE( "removing %c:\n", letter );
276 return remove_dos_device( letter - 'a', NULL );
280 /* implementation of IOCTL_MOUNTMGR_QUERY_UNIX_DRIVE */
281 static NTSTATUS query_unix_drive( void *buff, SIZE_T insize,
282 SIZE_T outsize, IO_STATUS_BLOCK *iosb )
284 const struct mountmgr_unix_drive *input = buff;
285 struct mountmgr_unix_drive *output = NULL;
286 char *device, *mount_point;
287 int letter = tolowerW( input->letter );
288 NTSTATUS status;
289 DWORD size, type = DEVICE_UNKNOWN;
290 enum device_type device_type;
291 char *ptr;
293 if (letter < 'a' || letter > 'z') return STATUS_INVALID_PARAMETER;
295 if ((status = query_dos_device( letter - 'a', &device_type, &device, &mount_point ))) return status;
296 switch (device_type)
298 case DEVICE_UNKNOWN: type = DRIVE_UNKNOWN; break;
299 case DEVICE_HARDDISK: type = DRIVE_REMOVABLE; break;
300 case DEVICE_HARDDISK_VOL: type = DRIVE_FIXED; break;
301 case DEVICE_FLOPPY: type = DRIVE_REMOVABLE; break;
302 case DEVICE_CDROM: type = DRIVE_CDROM; break;
303 case DEVICE_DVD: type = DRIVE_CDROM; break;
304 case DEVICE_NETWORK: type = DRIVE_REMOTE; break;
305 case DEVICE_RAMDISK: type = DRIVE_RAMDISK; break;
308 size = sizeof(*output);
309 if (device) size += strlen(device) + 1;
310 if (mount_point) size += strlen(mount_point) + 1;
312 input = NULL;
313 output = buff;
315 if (size > outsize)
317 iosb->Information = 0;
318 if (size >= FIELD_OFFSET( struct mountmgr_unix_drive, size ) + sizeof(output->size))
320 output->size = size;
321 iosb->Information = FIELD_OFFSET( struct mountmgr_unix_drive, size ) + sizeof(output->size);
323 if (size >= FIELD_OFFSET( struct mountmgr_unix_drive, type ) + sizeof(output->type))
325 output->type = type;
326 iosb->Information = FIELD_OFFSET( struct mountmgr_unix_drive, type ) + sizeof(output->type);
328 status = STATUS_MORE_ENTRIES;
329 goto done;
331 output->size = size;
332 output->letter = letter;
333 output->type = type;
334 ptr = (char *)(output + 1);
336 if (mount_point)
338 output->mount_point_offset = ptr - (char *)output;
339 strcpy( ptr, mount_point );
340 ptr += strlen(ptr) + 1;
342 else output->mount_point_offset = 0;
344 if (device)
346 output->device_offset = ptr - (char *)output;
347 strcpy( ptr, device );
348 ptr += strlen(ptr) + 1;
350 else output->device_offset = 0;
352 TRACE( "returning %c: dev %s mount %s type %u\n",
353 letter, debugstr_a(device), debugstr_a(mount_point), type );
355 iosb->Information = ptr - (char *)output;
356 done:
357 RtlFreeHeap( GetProcessHeap(), 0, device );
358 RtlFreeHeap( GetProcessHeap(), 0, mount_point );
359 return status;
362 /* handler for ioctls on the mount manager device */
363 static NTSTATUS WINAPI mountmgr_ioctl( DEVICE_OBJECT *device, IRP *irp )
365 IO_STACK_LOCATION *irpsp = IoGetCurrentIrpStackLocation( irp );
367 TRACE( "ioctl %x insize %u outsize %u\n",
368 irpsp->Parameters.DeviceIoControl.IoControlCode,
369 irpsp->Parameters.DeviceIoControl.InputBufferLength,
370 irpsp->Parameters.DeviceIoControl.OutputBufferLength );
372 switch(irpsp->Parameters.DeviceIoControl.IoControlCode)
374 case IOCTL_MOUNTMGR_QUERY_POINTS:
375 if (irpsp->Parameters.DeviceIoControl.InputBufferLength < sizeof(MOUNTMGR_MOUNT_POINT))
377 irp->IoStatus.u.Status = STATUS_INVALID_PARAMETER;
378 break;
380 irp->IoStatus.u.Status = query_mount_points( irp->AssociatedIrp.SystemBuffer,
381 irpsp->Parameters.DeviceIoControl.InputBufferLength,
382 irpsp->Parameters.DeviceIoControl.OutputBufferLength,
383 &irp->IoStatus );
384 break;
385 case IOCTL_MOUNTMGR_DEFINE_UNIX_DRIVE:
386 if (irpsp->Parameters.DeviceIoControl.InputBufferLength < sizeof(struct mountmgr_unix_drive))
388 irp->IoStatus.u.Status = STATUS_INVALID_PARAMETER;
389 break;
391 irp->IoStatus.Information = 0;
392 irp->IoStatus.u.Status = define_unix_drive( irp->AssociatedIrp.SystemBuffer,
393 irpsp->Parameters.DeviceIoControl.InputBufferLength );
394 break;
395 case IOCTL_MOUNTMGR_QUERY_UNIX_DRIVE:
396 if (irpsp->Parameters.DeviceIoControl.InputBufferLength < sizeof(struct mountmgr_unix_drive))
398 irp->IoStatus.u.Status = STATUS_INVALID_PARAMETER;
399 break;
401 irp->IoStatus.u.Status = query_unix_drive( irp->AssociatedIrp.SystemBuffer,
402 irpsp->Parameters.DeviceIoControl.InputBufferLength,
403 irpsp->Parameters.DeviceIoControl.OutputBufferLength,
404 &irp->IoStatus );
405 break;
406 default:
407 FIXME( "ioctl %x not supported\n", irpsp->Parameters.DeviceIoControl.IoControlCode );
408 irp->IoStatus.u.Status = STATUS_NOT_SUPPORTED;
409 break;
411 IoCompleteRequest( irp, IO_NO_INCREMENT );
412 return STATUS_SUCCESS;
415 /* main entry point for the mount point manager driver */
416 NTSTATUS WINAPI DriverEntry( DRIVER_OBJECT *driver, UNICODE_STRING *path )
418 static const WCHAR mounted_devicesW[] = {'S','y','s','t','e','m','\\','M','o','u','n','t','e','d','D','e','v','i','c','e','s',0};
419 static const WCHAR device_mountmgrW[] = {'\\','D','e','v','i','c','e','\\','M','o','u','n','t','P','o','i','n','t','M','a','n','a','g','e','r',0};
420 static const WCHAR link_mountmgrW[] = {'\\','?','?','\\','M','o','u','n','t','P','o','i','n','t','M','a','n','a','g','e','r',0};
421 static const WCHAR harddiskW[] = {'\\','D','r','i','v','e','r','\\','H','a','r','d','d','i','s','k',0};
422 static const WCHAR driver_serialW[] = {'\\','D','r','i','v','e','r','\\','S','e','r','i','a','l',0};
423 static const WCHAR driver_parallelW[] = {'\\','D','r','i','v','e','r','\\','P','a','r','a','l','l','e','l',0};
425 #ifdef _WIN64
426 static const WCHAR qualified_ports_keyW[] = {'\\','R','E','G','I','S','T','R','Y','\\',
427 'M','A','C','H','I','N','E','\\','S','o','f','t','w','a','r','e','\\',
428 'W','i','n','e','\\','P','o','r','t','s'}; /* no null terminator */
429 static const WCHAR wow64_ports_keyW[] = {'S','o','f','t','w','a','r','e','\\',
430 'W','o','w','6','4','3','2','N','o','d','e','\\','W','i','n','e','\\',
431 'P','o','r','t','s',0};
432 static const WCHAR symbolic_link_valueW[] = {'S','y','m','b','o','l','i','c','L','i','n','k','V','a','l','u','e',0};
433 HKEY wow64_ports_key = NULL;
434 #endif
436 UNICODE_STRING nameW, linkW;
437 DEVICE_OBJECT *device;
438 NTSTATUS status;
440 TRACE( "%s\n", debugstr_w(path->Buffer) );
442 driver->MajorFunction[IRP_MJ_DEVICE_CONTROL] = mountmgr_ioctl;
444 RtlInitUnicodeString( &nameW, device_mountmgrW );
445 RtlInitUnicodeString( &linkW, link_mountmgrW );
446 if (!(status = IoCreateDevice( driver, 0, &nameW, 0, 0, FALSE, &device )))
447 status = IoCreateSymbolicLink( &linkW, &nameW );
448 if (status)
450 FIXME( "failed to create device error %x\n", status );
451 return status;
454 RegCreateKeyExW( HKEY_LOCAL_MACHINE, mounted_devicesW, 0, NULL,
455 REG_OPTION_VOLATILE, KEY_ALL_ACCESS, NULL, &mount_key, NULL );
457 RtlInitUnicodeString( &nameW, harddiskW );
458 status = IoCreateDriver( &nameW, harddisk_driver_entry );
460 initialize_dbus();
461 initialize_diskarbitration();
463 #ifdef _WIN64
464 /* create a symlink so that the Wine port overrides key can be edited with 32-bit reg or regedit */
465 RegCreateKeyExW( HKEY_LOCAL_MACHINE, wow64_ports_keyW, 0, NULL, REG_OPTION_CREATE_LINK,
466 KEY_SET_VALUE, NULL, &wow64_ports_key, NULL );
467 RegSetValueExW( wow64_ports_key, symbolic_link_valueW, 0, REG_LINK,
468 (BYTE *)qualified_ports_keyW, sizeof(qualified_ports_keyW) );
469 RegCloseKey( wow64_ports_key );
470 #endif
472 RtlInitUnicodeString( &nameW, driver_serialW );
473 IoCreateDriver( &nameW, serial_driver_entry );
475 RtlInitUnicodeString( &nameW, driver_parallelW );
476 IoCreateDriver( &nameW, parallel_driver_entry );
478 return status;