mountmgr.sys: Add devices to the MountedDevices registry key.
[wine/winequartzdrv.git] / dlls / mountmgr.sys / mountmgr.c
blob875bd7901b3938d2b6fa5318e41d87cf8918345c
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
25 #define NONAMELESSSTRUCT
27 #include "ntstatus.h"
28 #define WIN32_NO_STATUS
29 #include "windef.h"
30 #include "winbase.h"
31 #include "winternl.h"
32 #include "winioctl.h"
33 #include "winreg.h"
34 #include "ddk/wdm.h"
35 #include "ddk/mountmgr.h"
36 #include "wine/library.h"
37 #include "wine/unicode.h"
38 #include "wine/debug.h"
40 WINE_DEFAULT_DEBUG_CHANNEL(mountmgr);
42 #define MAX_DOS_DRIVES 26
43 #define MAX_MOUNT_POINTS (2 * MAX_DOS_DRIVES)
45 /* extra info for disk devices, stored in DeviceExtension */
46 struct disk_device_info
48 UNICODE_STRING name; /* device name */
51 struct mount_point
53 DEVICE_OBJECT *device;
54 UNICODE_STRING link; /* DOS device symlink */
55 void *id; /* device unique id */
56 unsigned int id_len;
59 static struct mount_point mount_points[MAX_MOUNT_POINTS];
60 static HKEY mount_key;
62 static inline UNICODE_STRING *get_device_name( DEVICE_OBJECT *dev )
64 return &((struct disk_device_info *)dev->DeviceExtension)->name;
67 /* read a Unix symlink; returned buffer must be freed by caller */
68 static char *read_symlink( const char *path )
70 char *buffer;
71 int ret, size = 128;
73 for (;;)
75 if (!(buffer = RtlAllocateHeap( GetProcessHeap(), 0, size )))
77 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
78 return 0;
80 ret = readlink( path, buffer, size );
81 if (ret == -1)
83 RtlFreeHeap( GetProcessHeap(), 0, buffer );
84 return 0;
86 if (ret != size)
88 buffer[ret] = 0;
89 return buffer;
91 RtlFreeHeap( GetProcessHeap(), 0, buffer );
92 size *= 2;
96 static NTSTATUS create_disk_device( DRIVER_OBJECT *driver, DWORD type, DEVICE_OBJECT **dev_obj )
98 static const WCHAR harddiskW[] = {'\\','D','e','v','i','c','e',
99 '\\','H','a','r','d','d','i','s','k','V','o','l','u','m','e','%','u',0};
100 static const WCHAR cdromW[] = {'\\','D','e','v','i','c','e','\\','C','d','R','o','m','%','u',0};
101 static const WCHAR floppyW[] = {'\\','D','e','v','i','c','e','\\','F','l','o','p','p','y','%','u',0};
103 UINT i, first = 0;
104 NTSTATUS status = 0;
105 const WCHAR *format;
106 UNICODE_STRING name;
107 struct disk_device_info *info;
109 switch(type)
111 case DRIVE_REMOVABLE:
112 format = floppyW;
113 break;
114 case DRIVE_CDROM:
115 format = cdromW;
116 break;
117 case DRIVE_FIXED:
118 default: /* FIXME */
119 format = harddiskW;
120 first = 1; /* harddisk volumes start counting from 1 */
121 break;
124 name.MaximumLength = (strlenW(format) + 10) * sizeof(WCHAR);
125 name.Buffer = RtlAllocateHeap( GetProcessHeap(), 0, name.MaximumLength );
126 for (i = first; i < 32; i++)
128 sprintfW( name.Buffer, format, i );
129 name.Length = strlenW(name.Buffer) * sizeof(WCHAR);
130 status = IoCreateDevice( driver, sizeof(*info), &name, 0, 0, FALSE, dev_obj );
131 if (status != STATUS_OBJECT_NAME_COLLISION) break;
133 if (!status)
135 info = (*dev_obj)->DeviceExtension;
136 info->name = name;
138 else
140 FIXME( "IoCreateDevice %s got %x\n", debugstr_w(name.Buffer), status );
141 RtlFreeUnicodeString( &name );
143 return status;
147 static NTSTATUS add_mount_point( DRIVER_OBJECT *driver, DWORD type, int drive,
148 const void *id, unsigned int id_len )
150 static const WCHAR driveW[] = {'\\','D','o','s','D','e','v','i','c','e','s','\\','%','c',':',0};
151 static const WCHAR volumeW[] = {'\\','?','?','\\','V','o','l','u','m','e','{',
152 '%','0','8','x','-','%','0','4','x','-','%','0','4','x','-',
153 '%','0','2','x','%','0','2','x','-','%','0','2','x','%','0','2','x',
154 '%','0','2','x','%','0','2','x','%','0','2','x','%','0','2','x','}',0};
155 WCHAR *drive_link, *volume_link;
156 NTSTATUS status;
157 GUID guid;
158 UINT i;
159 struct mount_point *mount_drive = NULL, *mount_volume = NULL;
161 /* find two free mount points */
163 for (i = 0; i < MAX_MOUNT_POINTS; i++)
165 if (mount_points[i].device) continue;
166 if (!mount_drive)
168 mount_drive = &mount_points[i];
169 continue;
171 mount_volume = &mount_points[i];
172 break;
174 if (!mount_volume) return STATUS_NO_MEMORY;
176 /* create the volume */
178 memset( &guid, 0, sizeof(guid) ); /* FIXME */
179 guid.Data4[7] = 'A' + drive;
181 drive_link = RtlAllocateHeap( GetProcessHeap(), 0, sizeof(driveW) );
182 volume_link = RtlAllocateHeap( GetProcessHeap(), 0, sizeof(volumeW) );
183 sprintfW( drive_link, driveW, 'A' + drive );
184 sprintfW( volume_link, volumeW, guid.Data1, guid.Data2, guid.Data3,
185 guid.Data4[0], guid.Data4[1], guid.Data4[2], guid.Data4[3],
186 guid.Data4[4], guid.Data4[5], guid.Data4[6], guid.Data4[7]);
188 RtlInitUnicodeString( &mount_drive->link, drive_link );
189 RtlInitUnicodeString( &mount_volume->link, volume_link );
190 status = create_disk_device( driver, type, &mount_drive->device );
191 if (status)
193 RtlFreeUnicodeString( &mount_drive->link );
194 RtlFreeUnicodeString( &mount_volume->link );
195 return status;
198 mount_volume->device = mount_drive->device; /* FIXME: incr ref count */
199 mount_drive->id = RtlAllocateHeap( GetProcessHeap(), 0, id_len );
200 mount_drive->id_len = id_len;
201 memcpy( mount_drive->id, id, id_len );
202 mount_volume->id = RtlAllocateHeap( GetProcessHeap(), 0, id_len );
203 mount_volume->id_len = id_len;
204 memcpy( mount_volume->id, id, id_len );
206 IoCreateSymbolicLink( &mount_drive->link, get_device_name(mount_drive->device) );
207 IoCreateSymbolicLink( &mount_volume->link, get_device_name(mount_volume->device) );
209 TRACE( "created device %s symlinks %s %s\n", debugstr_w(get_device_name(mount_drive->device)->Buffer),
210 debugstr_w(mount_drive->link.Buffer), debugstr_w(mount_volume->link.Buffer) );
212 RegSetValueExW( mount_key, mount_drive->link.Buffer, 0, REG_BINARY,
213 mount_drive->id, mount_drive->id_len );
214 RegSetValueExW( mount_key, mount_volume->link.Buffer, 0, REG_BINARY,
215 mount_volume->id, mount_volume->id_len );
217 return STATUS_SUCCESS;
220 /* handler for ioctls on the mount manager device */
221 static NTSTATUS WINAPI mountmgr_ioctl( DEVICE_OBJECT *device, IRP *irp )
223 IO_STACK_LOCATION *irpsp = irp->Tail.Overlay.s.u.CurrentStackLocation;
225 TRACE( "ioctl %x insize %u outsize %u\n",
226 irpsp->Parameters.DeviceIoControl.IoControlCode,
227 irpsp->Parameters.DeviceIoControl.InputBufferLength,
228 irpsp->Parameters.DeviceIoControl.OutputBufferLength );
230 switch(irpsp->Parameters.DeviceIoControl.IoControlCode)
232 default:
233 FIXME( "ioctl %x not supported\n", irpsp->Parameters.DeviceIoControl.IoControlCode );
234 irp->IoStatus.u.Status = STATUS_NOT_SUPPORTED;
235 break;
237 return irp->IoStatus.u.Status;
240 /* handler for ioctls on the harddisk device */
241 static NTSTATUS WINAPI harddisk_ioctl( DEVICE_OBJECT *device, IRP *irp )
243 IO_STACK_LOCATION *irpsp = irp->Tail.Overlay.s.u.CurrentStackLocation;
245 TRACE( "ioctl %x insize %u outsize %u\n",
246 irpsp->Parameters.DeviceIoControl.IoControlCode,
247 irpsp->Parameters.DeviceIoControl.InputBufferLength,
248 irpsp->Parameters.DeviceIoControl.OutputBufferLength );
250 switch(irpsp->Parameters.DeviceIoControl.IoControlCode)
252 default:
253 FIXME( "unsupported ioctl %x\n", irpsp->Parameters.DeviceIoControl.IoControlCode );
254 irp->IoStatus.u.Status = STATUS_NOT_SUPPORTED;
255 break;
257 return irp->IoStatus.u.Status;
260 /* create mount points for mapped drives */
261 static void create_drive_mount_points( DRIVER_OBJECT *driver )
263 const char *config_dir = wine_get_config_dir();
264 char *buffer, *p, *link;
265 unsigned int i;
267 if ((buffer = RtlAllocateHeap( GetProcessHeap(), 0,
268 strlen(config_dir) + sizeof("/dosdevices/a:") )))
270 strcpy( buffer, config_dir );
271 strcat( buffer, "/dosdevices/a:" );
272 p = buffer + strlen(buffer) - 2;
274 for (i = 0; i < MAX_DOS_DRIVES; i++)
276 *p = 'a' + i;
277 if (!(link = read_symlink( buffer ))) continue;
278 add_mount_point( driver, DRIVE_FIXED, i, link, strlen(link) );
279 RtlFreeHeap( GetProcessHeap(), 0, link );
281 RtlFreeHeap( GetProcessHeap(), 0, buffer );
285 /* driver entry point for the harddisk driver */
286 static NTSTATUS WINAPI harddisk_driver_entry( DRIVER_OBJECT *driver, UNICODE_STRING *path )
288 static const WCHAR mounted_devicesW[] = {'S','y','s','t','e','m','\\',
289 'M','o','u','n','t','e','d','D','e','v','i','c','e','s',0};
290 static const WCHAR harddisk0W[] = {'\\','D','e','v','i','c','e',
291 '\\','H','a','r','d','d','i','s','k','0',0};
292 static const WCHAR physdrive0W[] = {'\\','?','?','\\','P','h','y','s','i','c','a','l','D','r','i','v','e','0',0};
294 UNICODE_STRING nameW, linkW;
295 DEVICE_OBJECT *device;
296 NTSTATUS status;
298 driver->MajorFunction[IRP_MJ_DEVICE_CONTROL] = harddisk_ioctl;
300 RegCreateKeyExW( HKEY_LOCAL_MACHINE, mounted_devicesW, 0, NULL,
301 REG_OPTION_VOLATILE, KEY_ALL_ACCESS, NULL, &mount_key, NULL );
303 RtlInitUnicodeString( &nameW, harddisk0W );
304 RtlInitUnicodeString( &linkW, physdrive0W );
305 if (!(status = IoCreateDevice( driver, 0, &nameW, 0, 0, FALSE, &device )))
306 status = IoCreateSymbolicLink( &linkW, &nameW );
307 if (status)
309 FIXME( "failed to create device error %x\n", status );
310 return status;
313 create_drive_mount_points( driver );
315 return status;
318 /* main entry point for the mount point manager driver */
319 NTSTATUS WINAPI DriverEntry( DRIVER_OBJECT *driver, UNICODE_STRING *path )
321 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};
322 static const WCHAR link_mountmgrW[] = {'\\','?','?','\\','M','o','u','n','t','P','o','i','n','t','M','a','n','a','g','e','r',0};
323 static const WCHAR harddiskW[] = {'\\','D','r','i','v','e','r','\\','H','a','r','d','d','i','s','k',0};
325 UNICODE_STRING nameW, linkW;
326 DEVICE_OBJECT *device;
327 NTSTATUS status;
329 TRACE( "%s\n", debugstr_w(path->Buffer) );
331 driver->MajorFunction[IRP_MJ_DEVICE_CONTROL] = mountmgr_ioctl;
333 RtlInitUnicodeString( &nameW, device_mountmgrW );
334 RtlInitUnicodeString( &linkW, link_mountmgrW );
335 if (!(status = IoCreateDevice( driver, 0, &nameW, 0, 0, FALSE, &device )))
336 status = IoCreateSymbolicLink( &linkW, &nameW );
337 if (status)
339 FIXME( "failed to create device error %x\n", status );
340 return status;
343 RtlInitUnicodeString( &nameW, harddiskW );
344 status = IoCreateDriver( &nameW, harddisk_driver_entry );
346 return status;