mountmgr: Symlink WoW64 Wine ports key to regular Wine ports key.
[wine.git] / dlls / mountmgr.sys / device.c
blob20c96d002b06776475b94fcf5352705a145993a3
1 /*
2 * Dynamic devices support
4 * Copyright 2006 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 "config.h"
22 #include "wine/port.h"
24 #include <assert.h>
25 #include <errno.h>
26 #include <stdarg.h>
27 #include <stdio.h>
28 #include <sys/time.h>
30 #define NONAMELESSUNION
32 #include "mountmgr.h"
33 #include "winreg.h"
34 #include "winuser.h"
35 #include "dbt.h"
37 #include "wine/library.h"
38 #include "wine/list.h"
39 #include "wine/unicode.h"
40 #include "wine/debug.h"
42 WINE_DEFAULT_DEBUG_CHANNEL(mountmgr);
44 #define MAX_DOS_DRIVES 26
45 #define MAX_PORTS 256
47 static const WCHAR drive_types[][8] =
49 { 0 }, /* DEVICE_UNKNOWN */
50 { 0 }, /* DEVICE_HARDDISK */
51 {'h','d',0}, /* DEVICE_HARDDISK_VOL */
52 {'f','l','o','p','p','y',0}, /* DEVICE_FLOPPY */
53 {'c','d','r','o','m',0}, /* DEVICE_CDROM */
54 {'c','d','r','o','m',0}, /* DEVICE_DVD */
55 {'n','e','t','w','o','r','k',0}, /* DEVICE_NETWORK */
56 {'r','a','m','d','i','s','k',0} /* DEVICE_RAMDISK */
59 static const WCHAR drives_keyW[] = {'S','o','f','t','w','a','r','e','\\',
60 'W','i','n','e','\\','D','r','i','v','e','s',0};
61 static const WCHAR ports_keyW[] = {'S','o','f','t','w','a','r','e','\\',
62 'W','i','n','e','\\','P','o','r','t','s',0};
64 struct disk_device
66 enum device_type type; /* drive type */
67 DEVICE_OBJECT *dev_obj; /* disk device allocated for this volume */
68 UNICODE_STRING name; /* device name */
69 UNICODE_STRING symlink; /* device symlink if any */
70 STORAGE_DEVICE_NUMBER devnum; /* device number info */
71 char *unix_device; /* unix device path */
72 char *unix_mount; /* unix mount point path */
75 struct volume
77 struct list entry; /* entry in volumes list */
78 struct disk_device *device; /* disk device */
79 char *udi; /* unique identifier for dynamic volumes */
80 unsigned int ref; /* ref count */
81 GUID guid; /* volume uuid */
82 struct mount_point *mount; /* Volume{xxx} mount point */
85 struct dos_drive
87 struct list entry; /* entry in drives list */
88 struct volume *volume; /* volume for this drive */
89 int drive; /* drive letter (0 = A: etc.) */
90 struct mount_point *mount; /* DosDevices mount point */
93 static struct list drives_list = LIST_INIT(drives_list);
94 static struct list volumes_list = LIST_INIT(volumes_list);
96 static DRIVER_OBJECT *harddisk_driver;
97 static DRIVER_OBJECT *serial_driver;
98 static DRIVER_OBJECT *parallel_driver;
100 static CRITICAL_SECTION device_section;
101 static CRITICAL_SECTION_DEBUG critsect_debug =
103 0, 0, &device_section,
104 { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
105 0, 0, { (DWORD_PTR)(__FILE__ ": device_section") }
107 static CRITICAL_SECTION device_section = { &critsect_debug, -1, 0, 0, 0, 0 };
109 static char *get_dosdevices_path( char **device )
111 const char *config_dir = wine_get_config_dir();
112 size_t len = strlen(config_dir) + sizeof("/dosdevices/com256");
113 char *path = HeapAlloc( GetProcessHeap(), 0, len );
114 if (path)
116 strcpy( path, config_dir );
117 strcat( path, "/dosdevices/a::" );
118 *device = path + len - sizeof("com256");
120 return path;
123 static char *strdupA( const char *str )
125 char *ret;
127 if (!str) return NULL;
128 if ((ret = RtlAllocateHeap( GetProcessHeap(), 0, strlen(str) + 1 ))) strcpy( ret, str );
129 return ret;
132 static const GUID *get_default_uuid( int letter )
134 static GUID guid;
136 guid.Data4[7] = 'A' + letter;
137 return &guid;
140 /* read a Unix symlink; returned buffer must be freed by caller */
141 static char *read_symlink( const char *path )
143 char *buffer;
144 int ret, size = 128;
146 for (;;)
148 if (!(buffer = RtlAllocateHeap( GetProcessHeap(), 0, size )))
150 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
151 return 0;
153 ret = readlink( path, buffer, size );
154 if (ret == -1)
156 RtlFreeHeap( GetProcessHeap(), 0, buffer );
157 return 0;
159 if (ret != size)
161 buffer[ret] = 0;
162 return buffer;
164 RtlFreeHeap( GetProcessHeap(), 0, buffer );
165 size *= 2;
169 /* update a symlink if it changed; return TRUE if updated */
170 static void update_symlink( const char *path, const char *dest, const char *orig_dest )
172 if (dest && dest[0])
174 if (!orig_dest || strcmp( orig_dest, dest ))
176 unlink( path );
177 symlink( dest, path );
180 else unlink( path );
183 /* send notification about a change to a given drive */
184 static void send_notify( int drive, int code )
186 DEV_BROADCAST_VOLUME info;
188 info.dbcv_size = sizeof(info);
189 info.dbcv_devicetype = DBT_DEVTYP_VOLUME;
190 info.dbcv_reserved = 0;
191 info.dbcv_unitmask = 1 << drive;
192 info.dbcv_flags = DBTF_MEDIA;
193 BroadcastSystemMessageW( BSF_FORCEIFHUNG|BSF_QUERY, NULL,
194 WM_DEVICECHANGE, code, (LPARAM)&info );
197 /* create the disk device for a given volume */
198 static NTSTATUS create_disk_device( enum device_type type, struct disk_device **device_ret )
200 static const WCHAR harddiskvolW[] = {'\\','D','e','v','i','c','e',
201 '\\','H','a','r','d','d','i','s','k','V','o','l','u','m','e','%','u',0};
202 static const WCHAR harddiskW[] = {'\\','D','e','v','i','c','e','\\','H','a','r','d','d','i','s','k','%','u',0};
203 static const WCHAR cdromW[] = {'\\','D','e','v','i','c','e','\\','C','d','R','o','m','%','u',0};
204 static const WCHAR floppyW[] = {'\\','D','e','v','i','c','e','\\','F','l','o','p','p','y','%','u',0};
205 static const WCHAR ramdiskW[] = {'\\','D','e','v','i','c','e','\\','R','a','m','d','i','s','k','%','u',0};
206 static const WCHAR cdromlinkW[] = {'\\','?','?','\\','C','d','R','o','m','%','u',0};
207 static const WCHAR physdriveW[] = {'\\','?','?','\\','P','h','y','s','i','c','a','l','D','r','i','v','e','%','u',0};
209 UINT i, first = 0;
210 NTSTATUS status = 0;
211 const WCHAR *format = NULL;
212 const WCHAR *link_format = NULL;
213 UNICODE_STRING name;
214 DEVICE_OBJECT *dev_obj;
215 struct disk_device *device;
217 switch(type)
219 case DEVICE_UNKNOWN:
220 case DEVICE_HARDDISK:
221 case DEVICE_NETWORK: /* FIXME */
222 format = harddiskW;
223 link_format = physdriveW;
224 break;
225 case DEVICE_HARDDISK_VOL:
226 format = harddiskvolW;
227 first = 1; /* harddisk volumes start counting from 1 */
228 break;
229 case DEVICE_FLOPPY:
230 format = floppyW;
231 break;
232 case DEVICE_CDROM:
233 case DEVICE_DVD:
234 format = cdromW;
235 link_format = cdromlinkW;
236 break;
237 case DEVICE_RAMDISK:
238 format = ramdiskW;
239 break;
242 name.MaximumLength = (strlenW(format) + 10) * sizeof(WCHAR);
243 name.Buffer = RtlAllocateHeap( GetProcessHeap(), 0, name.MaximumLength );
244 for (i = first; i < 32; i++)
246 sprintfW( name.Buffer, format, i );
247 name.Length = strlenW(name.Buffer) * sizeof(WCHAR);
248 status = IoCreateDevice( harddisk_driver, sizeof(*device), &name, 0, 0, FALSE, &dev_obj );
249 if (status != STATUS_OBJECT_NAME_COLLISION) break;
251 if (!status)
253 device = dev_obj->DeviceExtension;
254 device->dev_obj = dev_obj;
255 device->name = name;
256 device->type = type;
257 device->unix_device = NULL;
258 device->unix_mount = NULL;
259 device->symlink.Buffer = NULL;
261 if (link_format)
263 UNICODE_STRING symlink;
265 symlink.MaximumLength = (strlenW(link_format) + 10) * sizeof(WCHAR);
266 if ((symlink.Buffer = RtlAllocateHeap( GetProcessHeap(), 0, symlink.MaximumLength)))
268 sprintfW( symlink.Buffer, link_format, i );
269 symlink.Length = strlenW(symlink.Buffer) * sizeof(WCHAR);
270 if (!IoCreateSymbolicLink( &symlink, &name )) device->symlink = symlink;
274 switch (type)
276 case DEVICE_FLOPPY:
277 case DEVICE_RAMDISK:
278 device->devnum.DeviceType = FILE_DEVICE_DISK;
279 device->devnum.DeviceNumber = i;
280 device->devnum.PartitionNumber = ~0u;
281 break;
282 case DEVICE_CDROM:
283 device->devnum.DeviceType = FILE_DEVICE_CD_ROM;
284 device->devnum.DeviceNumber = i;
285 device->devnum.PartitionNumber = ~0u;
286 break;
287 case DEVICE_DVD:
288 device->devnum.DeviceType = FILE_DEVICE_DVD;
289 device->devnum.DeviceNumber = i;
290 device->devnum.PartitionNumber = ~0u;
291 break;
292 case DEVICE_UNKNOWN:
293 case DEVICE_HARDDISK:
294 case DEVICE_NETWORK: /* FIXME */
295 device->devnum.DeviceType = FILE_DEVICE_DISK;
296 device->devnum.DeviceNumber = i;
297 device->devnum.PartitionNumber = 0;
298 break;
299 case DEVICE_HARDDISK_VOL:
300 device->devnum.DeviceType = FILE_DEVICE_DISK;
301 device->devnum.DeviceNumber = 0;
302 device->devnum.PartitionNumber = i;
303 break;
305 *device_ret = device;
306 TRACE( "created device %s\n", debugstr_w(name.Buffer) );
308 else
310 FIXME( "IoCreateDevice %s got %x\n", debugstr_w(name.Buffer), status );
311 RtlFreeUnicodeString( &name );
313 return status;
316 /* delete the disk device for a given drive */
317 static void delete_disk_device( struct disk_device *device )
319 TRACE( "deleting device %s\n", debugstr_w(device->name.Buffer) );
320 if (device->symlink.Buffer)
322 IoDeleteSymbolicLink( &device->symlink );
323 RtlFreeUnicodeString( &device->symlink );
325 RtlFreeHeap( GetProcessHeap(), 0, device->unix_device );
326 RtlFreeHeap( GetProcessHeap(), 0, device->unix_mount );
327 RtlFreeUnicodeString( &device->name );
328 IoDeleteDevice( device->dev_obj );
331 /* grab another reference to a volume */
332 static struct volume *grab_volume( struct volume *volume )
334 volume->ref++;
335 return volume;
338 /* release a volume and delete the corresponding disk device when refcount is 0 */
339 static unsigned int release_volume( struct volume *volume )
341 unsigned int ret = --volume->ref;
343 if (!ret)
345 TRACE( "%s udi %s\n", debugstr_guid(&volume->guid), debugstr_a(volume->udi) );
346 assert( !volume->udi );
347 list_remove( &volume->entry );
348 if (volume->mount) delete_mount_point( volume->mount );
349 delete_disk_device( volume->device );
350 RtlFreeHeap( GetProcessHeap(), 0, volume );
352 return ret;
355 /* set the volume udi */
356 static void set_volume_udi( struct volume *volume, const char *udi )
358 if (udi)
360 assert( !volume->udi );
361 /* having a udi means the HAL side holds an extra reference */
362 if ((volume->udi = strdupA( udi ))) grab_volume( volume );
364 else if (volume->udi)
366 RtlFreeHeap( GetProcessHeap(), 0, volume->udi );
367 volume->udi = NULL;
368 release_volume( volume );
372 /* create a disk volume */
373 static NTSTATUS create_volume( const char *udi, enum device_type type, struct volume **volume_ret )
375 struct volume *volume;
376 NTSTATUS status;
378 if (!(volume = RtlAllocateHeap( GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*volume) )))
379 return STATUS_NO_MEMORY;
381 if (!(status = create_disk_device( type, &volume->device )))
383 if (udi) set_volume_udi( volume, udi );
384 list_add_tail( &volumes_list, &volume->entry );
385 *volume_ret = grab_volume( volume );
387 else RtlFreeHeap( GetProcessHeap(), 0, volume );
389 return status;
392 /* create the disk device for a given volume */
393 static NTSTATUS create_dos_device( struct volume *volume, const char *udi, int letter,
394 enum device_type type, struct dos_drive **drive_ret )
396 struct dos_drive *drive;
397 NTSTATUS status;
399 if (!(drive = RtlAllocateHeap( GetProcessHeap(), 0, sizeof(*drive) ))) return STATUS_NO_MEMORY;
400 drive->drive = letter;
401 drive->mount = NULL;
403 if (volume)
405 if (udi) set_volume_udi( volume, udi );
406 drive->volume = grab_volume( volume );
407 status = STATUS_SUCCESS;
409 else status = create_volume( udi, type, &drive->volume );
411 if (status == STATUS_SUCCESS)
413 list_add_tail( &drives_list, &drive->entry );
414 *drive_ret = drive;
416 else RtlFreeHeap( GetProcessHeap(), 0, drive );
418 return status;
421 /* delete the disk device for a given drive */
422 static void delete_dos_device( struct dos_drive *drive )
424 list_remove( &drive->entry );
425 if (drive->mount) delete_mount_point( drive->mount );
426 release_volume( drive->volume );
427 RtlFreeHeap( GetProcessHeap(), 0, drive );
430 /* find a volume that matches the parameters */
431 static struct volume *find_matching_volume( const char *udi, const char *device,
432 const char *mount_point, enum device_type type )
434 struct volume *volume;
435 struct disk_device *disk_device;
437 LIST_FOR_EACH_ENTRY( volume, &volumes_list, struct volume, entry )
439 int match = 0;
441 /* when we have a udi we only match drives added manually */
442 if (udi && volume->udi) continue;
443 /* and when we don't have a udi we only match dynamic drives */
444 if (!udi && !volume->udi) continue;
446 disk_device = volume->device;
447 if (disk_device->type != type) continue;
448 if (device && disk_device->unix_device)
450 if (strcmp( device, disk_device->unix_device )) continue;
451 match++;
453 if (mount_point && disk_device->unix_mount)
455 if (strcmp( mount_point, disk_device->unix_mount )) continue;
456 match++;
458 if (!match) continue;
459 TRACE( "found matching volume %s for device %s mount %s type %u\n",
460 debugstr_guid(&volume->guid), debugstr_a(device), debugstr_a(mount_point), type );
461 return grab_volume( volume );
463 return NULL;
466 /* change the information for an existing volume */
467 static NTSTATUS set_volume_info( struct volume *volume, struct dos_drive *drive, const char *device,
468 const char *mount_point, enum device_type type, const GUID *guid )
470 void *id = NULL;
471 unsigned int id_len = 0;
472 struct disk_device *disk_device = volume->device;
473 NTSTATUS status;
475 if (type != disk_device->type)
477 if ((status = create_disk_device( type, &disk_device ))) return status;
478 if (volume->mount)
480 delete_mount_point( volume->mount );
481 volume->mount = NULL;
483 if (drive && drive->mount)
485 delete_mount_point( drive->mount );
486 drive->mount = NULL;
488 delete_disk_device( volume->device );
489 volume->device = disk_device;
491 else
493 RtlFreeHeap( GetProcessHeap(), 0, disk_device->unix_device );
494 RtlFreeHeap( GetProcessHeap(), 0, disk_device->unix_mount );
496 disk_device->unix_device = strdupA( device );
497 disk_device->unix_mount = strdupA( mount_point );
499 if (guid && memcmp( &volume->guid, guid, sizeof(volume->guid) ))
501 volume->guid = *guid;
502 if (volume->mount)
504 delete_mount_point( volume->mount );
505 volume->mount = NULL;
509 if (!volume->mount)
510 volume->mount = add_volume_mount_point( disk_device->dev_obj, &disk_device->name, &volume->guid );
511 if (drive && !drive->mount)
512 drive->mount = add_dosdev_mount_point( disk_device->dev_obj, &disk_device->name, drive->drive );
514 if (disk_device->unix_mount)
516 id = disk_device->unix_mount;
517 id_len = strlen( disk_device->unix_mount ) + 1;
519 if (volume->mount) set_mount_point_id( volume->mount, id, id_len );
520 if (drive && drive->mount) set_mount_point_id( drive->mount, id, id_len );
522 return STATUS_SUCCESS;
525 /* change the drive letter or volume for an existing drive */
526 static void set_drive_info( struct dos_drive *drive, int letter, struct volume *volume )
528 if (drive->drive != letter)
530 if (drive->mount) delete_mount_point( drive->mount );
531 drive->mount = NULL;
532 drive->drive = letter;
534 if (drive->volume != volume)
536 if (drive->mount) delete_mount_point( drive->mount );
537 drive->mount = NULL;
538 grab_volume( volume );
539 release_volume( drive->volume );
540 drive->volume = volume;
544 static inline BOOL is_valid_device( struct stat *st )
546 #if defined(linux) || defined(__sun__)
547 return S_ISBLK( st->st_mode );
548 #else
549 /* disks are char devices on *BSD */
550 return S_ISCHR( st->st_mode );
551 #endif
554 /* find or create a DOS drive for the corresponding device */
555 static int add_drive( const char *device, enum device_type type )
557 char *path, *p;
558 char in_use[26];
559 struct stat dev_st, drive_st;
560 int drive, first, last, avail = 0;
562 if (stat( device, &dev_st ) == -1 || !is_valid_device( &dev_st )) return -1;
564 if (!(path = get_dosdevices_path( &p ))) return -1;
566 memset( in_use, 0, sizeof(in_use) );
568 switch (type)
570 case DEVICE_FLOPPY:
571 first = 0;
572 last = 2;
573 break;
574 case DEVICE_CDROM:
575 case DEVICE_DVD:
576 first = 3;
577 last = 26;
578 break;
579 default:
580 first = 2;
581 last = 26;
582 break;
585 while (avail != -1)
587 avail = -1;
588 for (drive = first; drive < last; drive++)
590 if (in_use[drive]) continue; /* already checked */
591 *p = 'a' + drive;
592 if (stat( path, &drive_st ) == -1)
594 if (lstat( path, &drive_st ) == -1 && errno == ENOENT) /* this is a candidate */
596 if (avail == -1)
598 p[2] = 0;
599 /* if mount point symlink doesn't exist either, it's available */
600 if (lstat( path, &drive_st ) == -1 && errno == ENOENT) avail = drive;
601 p[2] = ':';
604 else in_use[drive] = 1;
606 else
608 in_use[drive] = 1;
609 if (!is_valid_device( &drive_st )) continue;
610 if (dev_st.st_rdev == drive_st.st_rdev) goto done;
613 if (avail != -1)
615 /* try to use the one we found */
616 drive = avail;
617 *p = 'a' + drive;
618 if (symlink( device, path ) != -1) goto done;
619 /* failed, retry the search */
622 drive = -1;
624 done:
625 HeapFree( GetProcessHeap(), 0, path );
626 return drive;
629 /* create devices for mapped drives */
630 static void create_drive_devices(void)
632 char *path, *p, *link, *device;
633 struct dos_drive *drive;
634 struct volume *volume;
635 unsigned int i;
636 HKEY drives_key;
637 enum device_type drive_type;
638 WCHAR driveW[] = {'a',':',0};
640 if (!(path = get_dosdevices_path( &p ))) return;
641 if (RegOpenKeyW( HKEY_LOCAL_MACHINE, drives_keyW, &drives_key )) drives_key = 0;
643 for (i = 0; i < MAX_DOS_DRIVES; i++)
645 p[0] = 'a' + i;
646 p[2] = 0;
647 if (!(link = read_symlink( path ))) continue;
648 p[2] = ':';
649 device = read_symlink( path );
651 drive_type = i < 2 ? DEVICE_FLOPPY : DEVICE_HARDDISK_VOL;
652 if (drives_key)
654 WCHAR buffer[32];
655 DWORD j, type, size = sizeof(buffer);
657 driveW[0] = 'a' + i;
658 if (!RegQueryValueExW( drives_key, driveW, NULL, &type, (BYTE *)buffer, &size ) &&
659 type == REG_SZ)
661 for (j = 0; j < sizeof(drive_types)/sizeof(drive_types[0]); j++)
662 if (drive_types[j][0] && !strcmpiW( buffer, drive_types[j] ))
664 drive_type = j;
665 break;
667 if (drive_type == DEVICE_FLOPPY && i >= 2) drive_type = DEVICE_HARDDISK;
671 volume = find_matching_volume( NULL, device, link, drive_type );
672 if (!create_dos_device( volume, NULL, i, drive_type, &drive ))
674 /* don't reset uuid if we used an existing volume */
675 const GUID *guid = volume ? NULL : get_default_uuid(i);
676 set_volume_info( drive->volume, drive, device, link, drive_type, guid );
678 else
680 RtlFreeHeap( GetProcessHeap(), 0, link );
681 RtlFreeHeap( GetProcessHeap(), 0, device );
683 if (volume) release_volume( volume );
685 RegCloseKey( drives_key );
686 RtlFreeHeap( GetProcessHeap(), 0, path );
689 /* create a new disk volume */
690 NTSTATUS add_volume( const char *udi, const char *device, const char *mount_point,
691 enum device_type type, const GUID *guid )
693 struct volume *volume;
694 NTSTATUS status = STATUS_SUCCESS;
696 TRACE( "adding %s device %s mount %s type %u uuid %s\n", debugstr_a(udi),
697 debugstr_a(device), debugstr_a(mount_point), type, debugstr_guid(guid) );
699 EnterCriticalSection( &device_section );
700 LIST_FOR_EACH_ENTRY( volume, &volumes_list, struct volume, entry )
701 if (volume->udi && !strcmp( udi, volume->udi ))
703 grab_volume( volume );
704 goto found;
707 /* udi not found, search for a non-dynamic volume */
708 if ((volume = find_matching_volume( udi, device, mount_point, type ))) set_volume_udi( volume, udi );
709 else status = create_volume( udi, type, &volume );
711 found:
712 if (!status) status = set_volume_info( volume, NULL, device, mount_point, type, guid );
713 if (volume) release_volume( volume );
714 LeaveCriticalSection( &device_section );
715 return status;
718 /* create a new disk volume */
719 NTSTATUS remove_volume( const char *udi )
721 NTSTATUS status = STATUS_NO_SUCH_DEVICE;
722 struct volume *volume;
724 EnterCriticalSection( &device_section );
725 LIST_FOR_EACH_ENTRY( volume, &volumes_list, struct volume, entry )
727 if (!volume->udi || strcmp( udi, volume->udi )) continue;
728 set_volume_udi( volume, NULL );
729 status = STATUS_SUCCESS;
730 break;
732 LeaveCriticalSection( &device_section );
733 return status;
737 /* create a new dos drive */
738 NTSTATUS add_dos_device( int letter, const char *udi, const char *device,
739 const char *mount_point, enum device_type type, const GUID *guid )
741 char *path, *p;
742 HKEY hkey;
743 NTSTATUS status = STATUS_SUCCESS;
744 struct dos_drive *drive, *next;
745 struct volume *volume;
746 int notify = -1;
748 if (!(path = get_dosdevices_path( &p ))) return STATUS_NO_MEMORY;
750 EnterCriticalSection( &device_section );
751 volume = find_matching_volume( udi, device, mount_point, type );
753 if (letter == -1) /* auto-assign a letter */
755 letter = add_drive( device, type );
756 if (letter == -1)
758 status = STATUS_OBJECT_NAME_COLLISION;
759 goto done;
762 LIST_FOR_EACH_ENTRY_SAFE( drive, next, &drives_list, struct dos_drive, entry )
764 if (drive->volume->udi && !strcmp( udi, drive->volume->udi )) goto found;
765 if (drive->drive == letter) delete_dos_device( drive );
768 else /* simply reset the device symlink */
770 LIST_FOR_EACH_ENTRY( drive, &drives_list, struct dos_drive, entry )
771 if (drive->drive == letter) break;
773 *p = 'a' + letter;
774 if (&drive->entry == &drives_list) update_symlink( path, device, NULL );
775 else
777 update_symlink( path, device, drive->volume->device->unix_device );
778 delete_dos_device( drive );
782 if ((status = create_dos_device( volume, udi, letter, type, &drive ))) goto done;
784 found:
785 if (!guid && !volume) guid = get_default_uuid( letter );
786 if (!volume) volume = grab_volume( drive->volume );
787 set_drive_info( drive, letter, volume );
788 p[0] = 'a' + drive->drive;
789 p[2] = 0;
790 update_symlink( path, mount_point, volume->device->unix_mount );
791 set_volume_info( volume, drive, device, mount_point, type, guid );
793 TRACE( "added device %c: udi %s for %s on %s type %u\n",
794 'a' + drive->drive, wine_dbgstr_a(udi), wine_dbgstr_a(device),
795 wine_dbgstr_a(mount_point), type );
797 /* hack: force the drive type in the registry */
798 if (!RegCreateKeyW( HKEY_LOCAL_MACHINE, drives_keyW, &hkey ))
800 const WCHAR *type_name = drive_types[type];
801 WCHAR name[] = {'a',':',0};
803 name[0] += drive->drive;
804 if (!type_name[0] && type == DEVICE_HARDDISK) type_name = drive_types[DEVICE_FLOPPY];
805 if (type_name[0])
806 RegSetValueExW( hkey, name, 0, REG_SZ, (const BYTE *)type_name,
807 (strlenW(type_name) + 1) * sizeof(WCHAR) );
808 else
809 RegDeleteValueW( hkey, name );
810 RegCloseKey( hkey );
813 if (udi) notify = drive->drive;
815 done:
816 if (volume) release_volume( volume );
817 LeaveCriticalSection( &device_section );
818 RtlFreeHeap( GetProcessHeap(), 0, path );
819 if (notify != -1) send_notify( notify, DBT_DEVICEARRIVAL );
820 return status;
823 /* remove an existing dos drive, by letter or udi */
824 NTSTATUS remove_dos_device( int letter, const char *udi )
826 NTSTATUS status = STATUS_NO_SUCH_DEVICE;
827 HKEY hkey;
828 struct dos_drive *drive;
829 char *path, *p;
830 int notify = -1;
832 EnterCriticalSection( &device_section );
833 LIST_FOR_EACH_ENTRY( drive, &drives_list, struct dos_drive, entry )
835 if (udi)
837 if (!drive->volume->udi) continue;
838 if (strcmp( udi, drive->volume->udi )) continue;
839 set_volume_udi( drive->volume, NULL );
841 else if (drive->drive != letter) continue;
843 if ((path = get_dosdevices_path( &p )))
845 p[0] = 'a' + drive->drive;
846 p[2] = 0;
847 unlink( path );
848 RtlFreeHeap( GetProcessHeap(), 0, path );
851 /* clear the registry key too */
852 if (!RegOpenKeyW( HKEY_LOCAL_MACHINE, drives_keyW, &hkey ))
854 WCHAR name[] = {'a',':',0};
855 name[0] += drive->drive;
856 RegDeleteValueW( hkey, name );
857 RegCloseKey( hkey );
860 if (udi && drive->volume->device->unix_mount) notify = drive->drive;
862 delete_dos_device( drive );
863 status = STATUS_SUCCESS;
864 break;
866 LeaveCriticalSection( &device_section );
867 if (notify != -1) send_notify( notify, DBT_DEVICEREMOVECOMPLETE );
868 return status;
871 /* query information about an existing dos drive, by letter or udi */
872 NTSTATUS query_dos_device( int letter, enum device_type *type, char **device, char **mount_point )
874 NTSTATUS status = STATUS_NO_SUCH_DEVICE;
875 struct dos_drive *drive;
876 struct disk_device *disk_device;
878 EnterCriticalSection( &device_section );
879 LIST_FOR_EACH_ENTRY( drive, &drives_list, struct dos_drive, entry )
881 if (drive->drive != letter) continue;
882 disk_device = drive->volume->device;
883 if (type) *type = disk_device->type;
884 if (device) *device = strdupA( disk_device->unix_device );
885 if (mount_point) *mount_point = strdupA( disk_device->unix_mount );
886 status = STATUS_SUCCESS;
887 break;
889 LeaveCriticalSection( &device_section );
890 return status;
893 /* handler for ioctls on the harddisk device */
894 static NTSTATUS WINAPI harddisk_ioctl( DEVICE_OBJECT *device, IRP *irp )
896 IO_STACK_LOCATION *irpsp = IoGetCurrentIrpStackLocation( irp );
897 struct disk_device *dev = device->DeviceExtension;
899 TRACE( "ioctl %x insize %u outsize %u\n",
900 irpsp->Parameters.DeviceIoControl.IoControlCode,
901 irpsp->Parameters.DeviceIoControl.InputBufferLength,
902 irpsp->Parameters.DeviceIoControl.OutputBufferLength );
904 EnterCriticalSection( &device_section );
906 switch(irpsp->Parameters.DeviceIoControl.IoControlCode)
908 case IOCTL_DISK_GET_DRIVE_GEOMETRY:
910 DISK_GEOMETRY info;
911 DWORD len = min( sizeof(info), irpsp->Parameters.DeviceIoControl.OutputBufferLength );
913 info.Cylinders.QuadPart = 10000;
914 info.MediaType = (dev->devnum.DeviceType == FILE_DEVICE_DISK) ? FixedMedia : RemovableMedia;
915 info.TracksPerCylinder = 255;
916 info.SectorsPerTrack = 63;
917 info.BytesPerSector = 512;
918 memcpy( irp->AssociatedIrp.SystemBuffer, &info, len );
919 irp->IoStatus.Information = len;
920 irp->IoStatus.u.Status = STATUS_SUCCESS;
921 break;
923 case IOCTL_DISK_GET_DRIVE_GEOMETRY_EX:
925 DISK_GEOMETRY_EX info;
926 DWORD len = min( sizeof(info), irpsp->Parameters.DeviceIoControl.OutputBufferLength );
928 FIXME("The DISK_PARTITION_INFO and DISK_DETECTION_INFO structures will not be filled\n");
930 info.Geometry.Cylinders.QuadPart = 10000;
931 info.Geometry.MediaType = (dev->devnum.DeviceType == FILE_DEVICE_DISK) ? FixedMedia : RemovableMedia;
932 info.Geometry.TracksPerCylinder = 255;
933 info.Geometry.SectorsPerTrack = 63;
934 info.Geometry.BytesPerSector = 512;
935 info.DiskSize.QuadPart = info.Geometry.Cylinders.QuadPart * info.Geometry.TracksPerCylinder *
936 info.Geometry.SectorsPerTrack * info.Geometry.BytesPerSector;
937 info.Data[0] = 0;
938 memcpy( irp->AssociatedIrp.SystemBuffer, &info, len );
939 irp->IoStatus.Information = len;
940 irp->IoStatus.u.Status = STATUS_SUCCESS;
941 break;
943 case IOCTL_STORAGE_GET_DEVICE_NUMBER:
945 DWORD len = min( sizeof(dev->devnum), irpsp->Parameters.DeviceIoControl.OutputBufferLength );
947 memcpy( irp->AssociatedIrp.SystemBuffer, &dev->devnum, len );
948 irp->IoStatus.Information = len;
949 irp->IoStatus.u.Status = STATUS_SUCCESS;
950 break;
952 case IOCTL_CDROM_READ_TOC:
953 irp->IoStatus.u.Status = STATUS_INVALID_DEVICE_REQUEST;
954 break;
955 case IOCTL_VOLUME_GET_VOLUME_DISK_EXTENTS:
957 DWORD len = min( 32, irpsp->Parameters.DeviceIoControl.OutputBufferLength );
959 FIXME( "returning zero-filled buffer for IOCTL_VOLUME_GET_VOLUME_DISK_EXTENTS\n" );
960 memset( irp->AssociatedIrp.SystemBuffer, 0, len );
961 irp->IoStatus.Information = len;
962 irp->IoStatus.u.Status = STATUS_SUCCESS;
963 break;
965 default:
967 ULONG code = irpsp->Parameters.DeviceIoControl.IoControlCode;
968 FIXME("Unsupported ioctl %x (device=%x access=%x func=%x method=%x)\n",
969 code, code >> 16, (code >> 14) & 3, (code >> 2) & 0xfff, code & 3);
970 irp->IoStatus.u.Status = STATUS_NOT_SUPPORTED;
971 break;
975 LeaveCriticalSection( &device_section );
976 IoCompleteRequest( irp, IO_NO_INCREMENT );
977 return STATUS_SUCCESS;
980 /* driver entry point for the harddisk driver */
981 NTSTATUS WINAPI harddisk_driver_entry( DRIVER_OBJECT *driver, UNICODE_STRING *path )
983 struct disk_device *device;
985 harddisk_driver = driver;
986 driver->MajorFunction[IRP_MJ_DEVICE_CONTROL] = harddisk_ioctl;
988 /* create a harddisk0 device that isn't assigned to any drive */
989 create_disk_device( DEVICE_HARDDISK, &device );
991 create_drive_devices();
993 return STATUS_SUCCESS;
997 /* create a serial or parallel port */
998 static BOOL create_port_device( DRIVER_OBJECT *driver, int n, const char *unix_path, const char *dosdevices_path, char *p,
999 HKEY wine_ports_key, HKEY windows_ports_key )
1001 static const WCHAR comW[] = {'C','O','M','%','u',0};
1002 static const WCHAR lptW[] = {'L','P','T','%','u',0};
1003 static const WCHAR device_serialW[] = {'\\','D','e','v','i','c','e','\\','S','e','r','i','a','l','%','u',0};
1004 static const WCHAR device_parallelW[] = {'\\','D','e','v','i','c','e','\\','P','a','r','a','l','l','e','l','%','u',0};
1005 static const WCHAR dosdevices_comW[] = {'\\','D','o','s','D','e','v','i','c','e','s','\\','C','O','M','%','u',0};
1006 static const WCHAR dosdevices_auxW[] = {'\\','D','o','s','D','e','v','i','c','e','s','\\','A','U','X',0};
1007 static const WCHAR dosdevices_lptW[] = {'\\','D','o','s','D','e','v','i','c','e','s','\\','L','P','T','%','u',0};
1008 static const WCHAR dosdevices_prnW[] = {'\\','D','o','s','D','e','v','i','c','e','s','\\','P','R','N',0};
1009 const WCHAR *dos_name_format, *nt_name_format, *reg_value_format, *symlink_format, *default_device;
1010 WCHAR dos_name[7], reg_value[256], nt_buffer[32], symlink_buffer[32];
1011 DWORD type, size;
1012 char override_path[256];
1013 UNICODE_STRING nt_name, symlink_name, default_name;
1014 DEVICE_OBJECT *dev_obj;
1015 NTSTATUS status;
1017 if (driver == serial_driver)
1019 dos_name_format = comW;
1020 nt_name_format = device_serialW;
1021 reg_value_format = comW;
1022 symlink_format = dosdevices_comW;
1023 default_device = dosdevices_auxW;
1025 else
1027 dos_name_format = lptW;
1028 nt_name_format = device_parallelW;
1029 reg_value_format = dosdevices_lptW;
1030 symlink_format = dosdevices_lptW;
1031 default_device = dosdevices_prnW;
1034 sprintfW( dos_name, dos_name_format, n );
1036 /* check for override */
1037 size = sizeof(reg_value);
1038 if (RegQueryValueExW( wine_ports_key, dos_name, NULL, &type, (BYTE *)reg_value, &size ) == 0 && type == REG_SZ)
1040 if (!reg_value[0] || !WideCharToMultiByte( CP_UNIXCP, WC_ERR_INVALID_CHARS, reg_value, size/sizeof(WCHAR),
1041 override_path, sizeof(override_path), NULL, NULL))
1042 return FALSE;
1043 unix_path = override_path;
1045 if (!unix_path)
1046 return FALSE;
1048 /* create DOS device */
1049 sprintf( p, "%u", n );
1050 if (symlink( unix_path, dosdevices_path ) != 0)
1051 return FALSE;
1053 /* create NT device */
1054 sprintfW( nt_buffer, nt_name_format, n - 1 );
1055 RtlInitUnicodeString( &nt_name, nt_buffer );
1056 status = IoCreateDevice( driver, 0, &nt_name, 0, 0, FALSE, &dev_obj );
1057 if (status != STATUS_SUCCESS)
1059 FIXME( "IoCreateDevice %s got %x\n", debugstr_w(nt_name.Buffer), status );
1060 return FALSE;
1062 sprintfW( symlink_buffer, symlink_format, n );
1063 RtlInitUnicodeString( &symlink_name, symlink_buffer );
1064 IoCreateSymbolicLink( &symlink_name, &nt_name );
1065 if (n == 1)
1067 RtlInitUnicodeString( &default_name, default_device );
1068 IoCreateSymbolicLink( &default_name, &symlink_name );
1071 /* TODO: store information about the Unix device in the NT device */
1073 /* create registry entry */
1074 sprintfW( reg_value, reg_value_format, n );
1075 RegSetValueExW( windows_ports_key, nt_name.Buffer, 0, REG_SZ,
1076 (BYTE *)reg_value, (strlenW( reg_value ) + 1) * sizeof(WCHAR) );
1078 return TRUE;
1081 /* find and create serial or parallel ports */
1082 static void create_port_devices( DRIVER_OBJECT *driver )
1084 static const char *serial_search_paths[] = {
1085 #ifdef linux
1086 "/dev/ttyS%u",
1087 "/dev/ttyUSB%u",
1088 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
1089 "/dev/cuau%u",
1090 #elif defined(__DragonFly__)
1091 "/dev/cuaa%u",
1092 #else
1094 #endif
1096 static const char *parallel_search_paths[] = {
1097 #ifdef linux
1098 "/dev/lp%u",
1099 #else
1101 #endif
1103 static const WCHAR serialcomm_keyW[] = {'H','A','R','D','W','A','R','E','\\',
1104 'D','E','V','I','C','E','M','A','P','\\',
1105 'S','E','R','I','A','L','C','O','M','M',0};
1106 static const WCHAR parallel_ports_keyW[] = {'H','A','R','D','W','A','R','E','\\',
1107 'D','E','V','I','C','E','M','A','P','\\',
1108 'P','A','R','A','L','L','E','L',' ','P','O','R','T','S',0};
1109 const char **search_paths;
1110 const WCHAR *windows_ports_key_name;
1111 char *dosdevices_path, *p;
1112 HKEY wine_ports_key = NULL, windows_ports_key = NULL;
1113 char unix_path[256];
1114 int num_search_paths, i, j, n;
1116 if (!(dosdevices_path = get_dosdevices_path( &p )))
1117 return;
1119 if (driver == serial_driver)
1121 p[0] = 'c';
1122 p[1] = 'o';
1123 p[2] = 'm';
1124 search_paths = serial_search_paths;
1125 num_search_paths = sizeof(serial_search_paths)/sizeof(serial_search_paths[0]);
1126 windows_ports_key_name = serialcomm_keyW;
1128 else
1130 p[0] = 'l';
1131 p[1] = 'p';
1132 p[2] = 't';
1133 search_paths = parallel_search_paths;
1134 num_search_paths = sizeof(parallel_search_paths)/sizeof(parallel_search_paths[0]);
1135 windows_ports_key_name = parallel_ports_keyW;
1137 p += 3;
1139 RegCreateKeyExW( HKEY_LOCAL_MACHINE, ports_keyW, 0, NULL, 0,
1140 KEY_QUERY_VALUE, NULL, &wine_ports_key, NULL );
1141 RegCreateKeyExW( HKEY_LOCAL_MACHINE, windows_ports_key_name, 0, NULL, REG_OPTION_VOLATILE,
1142 KEY_ALL_ACCESS, NULL, &windows_ports_key, NULL );
1144 /* remove old symlinks */
1145 for (n = 1; n <= MAX_PORTS; n++)
1147 sprintf( p, "%u", n );
1148 if (unlink( dosdevices_path ) != 0 && errno == ENOENT)
1149 break;
1152 /* look for ports in the usual places */
1153 n = 1;
1154 for (i = 0; i < num_search_paths; i++)
1156 for (j = 0; n <= MAX_PORTS; j++)
1158 sprintf( unix_path, search_paths[i], j );
1159 if (access( unix_path, F_OK ) != 0)
1160 break;
1162 create_port_device( driver, n, unix_path, dosdevices_path, p, wine_ports_key, windows_ports_key );
1163 n++;
1167 /* add any extra user-defined serial ports */
1168 while (n <= MAX_PORTS)
1170 if (!create_port_device( driver, n, NULL, dosdevices_path, p, wine_ports_key, windows_ports_key ))
1171 break;
1172 n++;
1175 RegCloseKey( wine_ports_key );
1176 RegCloseKey( windows_ports_key );
1177 HeapFree( GetProcessHeap(), 0, dosdevices_path );
1180 /* driver entry point for the serial port driver */
1181 NTSTATUS WINAPI serial_driver_entry( DRIVER_OBJECT *driver, UNICODE_STRING *path )
1183 serial_driver = driver;
1184 /* TODO: fill in driver->MajorFunction */
1186 create_port_devices( driver );
1188 return STATUS_SUCCESS;
1191 /* driver entry point for the parallel port driver */
1192 NTSTATUS WINAPI parallel_driver_entry( DRIVER_OBJECT *driver, UNICODE_STRING *path )
1194 parallel_driver = driver;
1195 /* TODO: fill in driver->MajorFunction */
1197 create_port_devices( driver );
1199 return STATUS_SUCCESS;