2 * Block driver for RAW files
4 * Copyright (c) 2006 Fabrice Bellard
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25 #include "block_int.h"
36 #include <sys/param.h>
37 #include <IOKit/IOKitLib.h>
38 #include <IOKit/IOBSD.h>
39 #include <IOKit/storage/IOMediaBSDClient.h>
40 #include <IOKit/storage/IOMedia.h>
41 #include <IOKit/storage/IOCDMedia.h>
42 //#include <IOKit/storage/IOCDTypes.h>
43 #include <CoreFoundation/CoreFoundation.h>
50 #include <sys/ioctl.h>
51 #include <linux/cdrom.h>
58 //#define DEBUG_FLOPPY
64 /* if the FD is not accessed during that time (in ms), we try to
65 reopen it to see if the disk has been changed */
66 #define FD_OPEN_TIMEOUT 1000
68 typedef struct BDRVRawState {
71 #if defined(__linux__)
72 /* linux floppy specific */
75 int64_t fd_error_time;
81 static int fd_open(BlockDriverState *bs);
83 static int raw_open(BlockDriverState *bs, const char *filename, int flags)
85 BDRVRawState *s = bs->opaque;
86 int fd, open_flags, ret;
88 open_flags = O_BINARY;
89 if ((flags & BDRV_O_ACCESS) == O_RDWR) {
92 open_flags |= O_RDONLY;
95 if (flags & BDRV_O_CREAT)
96 open_flags |= O_CREAT | O_TRUNC;
100 fd = open(filename, open_flags, 0644);
111 /* XXX: use host sector size if necessary with:
112 #ifdef DIOCGSECTORSIZE
114 unsigned int sectorsize = 512;
115 if (!ioctl(fd, DIOCGSECTORSIZE, §orsize) &&
116 sectorsize > bufsize)
117 bufsize = sectorsize;
121 u_int32_t blockSize = 512;
122 if ( !ioctl( fd, DKIOCGETBLOCKSIZE, &blockSize ) && blockSize > bufsize) {
128 static int raw_pread(BlockDriverState *bs, int64_t offset,
129 uint8_t *buf, int count)
131 BDRVRawState *s = bs->opaque;
138 lseek(s->fd, offset, SEEK_SET);
139 ret = read(s->fd, buf, count);
143 static int raw_pwrite(BlockDriverState *bs, int64_t offset,
144 const uint8_t *buf, int count)
146 BDRVRawState *s = bs->opaque;
153 lseek(s->fd, offset, SEEK_SET);
154 ret = write(s->fd, buf, count);
158 /***********************************************************/
159 /* Unix AIO using POSIX AIO */
161 typedef struct RawAIOCB {
162 BlockDriverAIOCB common;
164 struct RawAIOCB *next;
167 static int aio_sig_num = SIGUSR2;
168 static RawAIOCB *first_aio; /* AIO issued */
169 static int aio_initialized = 0;
171 static void aio_signal_handler(int signum)
174 CPUState *env = cpu_single_env;
176 /* stop the currently executing cpu because a timer occured */
177 cpu_interrupt(env, CPU_INTERRUPT_EXIT);
179 if (env->kqemu_enabled) {
180 kqemu_cpu_interrupt(env);
187 void qemu_aio_init(void)
189 struct sigaction act;
193 sigfillset(&act.sa_mask);
194 act.sa_flags = 0; /* do not restart syscalls to interrupt select() */
195 act.sa_handler = aio_signal_handler;
196 sigaction(aio_sig_num, &act, NULL);
198 #if defined(__GLIBC__) && defined(__linux__)
200 /* XXX: aio thread exit seems to hang on RedHat 9 and this init
201 seems to fix the problem. */
203 memset(&ai, 0, sizeof(ai));
206 ai.aio_idle_time = 365 * 100000;
212 void qemu_aio_poll(void)
214 RawAIOCB *acb, **pacb;
223 ret = aio_error(&acb->aiocb);
224 if (ret == ECANCELED) {
225 /* remove the request */
227 qemu_aio_release(acb);
228 } else if (ret != EINPROGRESS) {
231 ret = aio_return(&acb->aiocb);
232 if (ret == acb->aiocb.aio_nbytes)
239 /* remove the request */
241 /* call the callback */
242 acb->common.cb(acb->common.opaque, ret);
243 qemu_aio_release(acb);
253 /* Wait for all IO requests to complete. */
254 void qemu_aio_flush(void)
256 qemu_aio_wait_start();
264 /* wait until at least one AIO was handled */
265 static sigset_t wait_oset;
267 void qemu_aio_wait_start(void)
271 if (!aio_initialized)
274 sigaddset(&set, aio_sig_num);
275 sigprocmask(SIG_BLOCK, &set, &wait_oset);
278 void qemu_aio_wait(void)
288 sigaddset(&set, aio_sig_num);
289 sigwait(&set, &nb_sigs);
293 void qemu_aio_wait_end(void)
295 sigprocmask(SIG_SETMASK, &wait_oset, NULL);
298 static RawAIOCB *raw_aio_setup(BlockDriverState *bs,
299 int64_t sector_num, uint8_t *buf, int nb_sectors,
300 BlockDriverCompletionFunc *cb, void *opaque)
302 BDRVRawState *s = bs->opaque;
308 acb = qemu_aio_get(bs, cb, opaque);
311 acb->aiocb.aio_fildes = s->fd;
312 acb->aiocb.aio_sigevent.sigev_signo = aio_sig_num;
313 acb->aiocb.aio_sigevent.sigev_notify = SIGEV_SIGNAL;
314 acb->aiocb.aio_buf = buf;
315 acb->aiocb.aio_nbytes = nb_sectors * 512;
316 acb->aiocb.aio_offset = sector_num * 512;
317 acb->next = first_aio;
322 static BlockDriverAIOCB *raw_aio_read(BlockDriverState *bs,
323 int64_t sector_num, uint8_t *buf, int nb_sectors,
324 BlockDriverCompletionFunc *cb, void *opaque)
328 acb = raw_aio_setup(bs, sector_num, buf, nb_sectors, cb, opaque);
331 if (aio_read(&acb->aiocb) < 0) {
332 qemu_aio_release(acb);
338 static BlockDriverAIOCB *raw_aio_write(BlockDriverState *bs,
339 int64_t sector_num, const uint8_t *buf, int nb_sectors,
340 BlockDriverCompletionFunc *cb, void *opaque)
344 acb = raw_aio_setup(bs, sector_num, (uint8_t*)buf, nb_sectors, cb, opaque);
347 if (aio_write(&acb->aiocb) < 0) {
348 qemu_aio_release(acb);
354 static void raw_aio_cancel(BlockDriverAIOCB *blockacb)
357 RawAIOCB *acb = (RawAIOCB *)blockacb;
360 ret = aio_cancel(acb->aiocb.aio_fildes, &acb->aiocb);
361 if (ret == AIO_NOTCANCELED) {
362 /* fail safe: if the aio could not be canceled, we wait for
364 while (aio_error(&acb->aiocb) == EINPROGRESS);
367 /* remove the callback from the queue */
372 } else if (*pacb == acb) {
374 qemu_aio_release(acb);
381 static void raw_close(BlockDriverState *bs)
383 BDRVRawState *s = bs->opaque;
390 static int raw_truncate(BlockDriverState *bs, int64_t offset)
392 BDRVRawState *s = bs->opaque;
393 if (s->type != FTYPE_FILE)
395 if (ftruncate(s->fd, offset) < 0)
400 static int64_t raw_getlength(BlockDriverState *bs)
402 BDRVRawState *s = bs->opaque;
409 struct dk_minfo minfo;
419 if (!fstat(fd, &sb) && (S_IFCHR & sb.st_mode)) {
420 #ifdef DIOCGMEDIASIZE
421 if (ioctl(fd, DIOCGMEDIASIZE, (off_t *)&size))
424 size = LONG_LONG_MAX;
426 size = lseek(fd, 0LL, SEEK_END);
432 * use the DKIOCGMEDIAINFO ioctl to read the size.
434 rv = ioctl ( fd, DKIOCGMEDIAINFO, &minfo );
436 size = minfo.dki_lbsize * minfo.dki_capacity;
437 } else /* there are reports that lseek on some devices
438 fails, but irc discussion said that contingency
439 on contingency was overkill */
442 size = lseek(fd, 0, SEEK_END);
447 static int raw_create(const char *filename, int64_t total_size,
448 const char *backing_file, int flags)
452 if (flags || backing_file)
455 fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY,
459 ftruncate(fd, total_size * 512);
464 static void raw_flush(BlockDriverState *bs)
466 BDRVRawState *s = bs->opaque;
470 BlockDriver bdrv_raw = {
472 sizeof(BDRVRawState),
473 NULL, /* no probe for protocols */
481 .bdrv_aio_read = raw_aio_read,
482 .bdrv_aio_write = raw_aio_write,
483 .bdrv_aio_cancel = raw_aio_cancel,
484 .aiocb_size = sizeof(RawAIOCB),
485 .protocol_name = "file",
486 .bdrv_pread = raw_pread,
487 .bdrv_pwrite = raw_pwrite,
488 .bdrv_truncate = raw_truncate,
489 .bdrv_getlength = raw_getlength,
492 /***********************************************/
496 static kern_return_t FindEjectableCDMedia( io_iterator_t *mediaIterator );
497 static kern_return_t GetBSDPath( io_iterator_t mediaIterator, char *bsdPath, CFIndex maxPathSize );
499 kern_return_t FindEjectableCDMedia( io_iterator_t *mediaIterator )
501 kern_return_t kernResult;
502 mach_port_t masterPort;
503 CFMutableDictionaryRef classesToMatch;
505 kernResult = IOMasterPort( MACH_PORT_NULL, &masterPort );
506 if ( KERN_SUCCESS != kernResult ) {
507 printf( "IOMasterPort returned %d\n", kernResult );
510 classesToMatch = IOServiceMatching( kIOCDMediaClass );
511 if ( classesToMatch == NULL ) {
512 printf( "IOServiceMatching returned a NULL dictionary.\n" );
514 CFDictionarySetValue( classesToMatch, CFSTR( kIOMediaEjectableKey ), kCFBooleanTrue );
516 kernResult = IOServiceGetMatchingServices( masterPort, classesToMatch, mediaIterator );
517 if ( KERN_SUCCESS != kernResult )
519 printf( "IOServiceGetMatchingServices returned %d\n", kernResult );
525 kern_return_t GetBSDPath( io_iterator_t mediaIterator, char *bsdPath, CFIndex maxPathSize )
527 io_object_t nextMedia;
528 kern_return_t kernResult = KERN_FAILURE;
530 nextMedia = IOIteratorNext( mediaIterator );
533 CFTypeRef bsdPathAsCFString;
534 bsdPathAsCFString = IORegistryEntryCreateCFProperty( nextMedia, CFSTR( kIOBSDNameKey ), kCFAllocatorDefault, 0 );
535 if ( bsdPathAsCFString ) {
536 size_t devPathLength;
537 strcpy( bsdPath, _PATH_DEV );
538 strcat( bsdPath, "r" );
539 devPathLength = strlen( bsdPath );
540 if ( CFStringGetCString( bsdPathAsCFString, bsdPath + devPathLength, maxPathSize - devPathLength, kCFStringEncodingASCII ) ) {
541 kernResult = KERN_SUCCESS;
543 CFRelease( bsdPathAsCFString );
545 IOObjectRelease( nextMedia );
553 static int hdev_open(BlockDriverState *bs, const char *filename, int flags)
555 BDRVRawState *s = bs->opaque;
556 int fd, open_flags, ret;
559 if (strstart(filename, "/dev/cdrom", NULL)) {
560 kern_return_t kernResult;
561 io_iterator_t mediaIterator;
562 char bsdPath[ MAXPATHLEN ];
565 kernResult = FindEjectableCDMedia( &mediaIterator );
566 kernResult = GetBSDPath( mediaIterator, bsdPath, sizeof( bsdPath ) );
568 if ( bsdPath[ 0 ] != '\0' ) {
569 strcat(bsdPath,"s0");
570 /* some CDs don't have a partition 0 */
571 fd = open(bsdPath, O_RDONLY | O_BINARY | O_LARGEFILE);
573 bsdPath[strlen(bsdPath)-1] = '1';
581 IOObjectRelease( mediaIterator );
584 open_flags = O_BINARY;
585 if ((flags & BDRV_O_ACCESS) == O_RDWR) {
586 open_flags |= O_RDWR;
588 open_flags |= O_RDONLY;
592 s->type = FTYPE_FILE;
593 #if defined(__linux__)
594 if (strstart(filename, "/dev/cd", NULL)) {
595 /* open will not fail even if no CD is inserted */
596 open_flags |= O_NONBLOCK;
598 } else if (strstart(filename, "/dev/fd", NULL)) {
600 s->fd_open_flags = open_flags;
601 /* open will not fail even if no floppy is inserted */
602 open_flags |= O_NONBLOCK;
605 fd = open(filename, open_flags, 0644);
613 #if defined(__linux__)
614 /* close fd so that we can reopen it as needed */
615 if (s->type == FTYPE_FD) {
618 s->fd_media_changed = 1;
624 #if defined(__linux__) && !defined(QEMU_TOOL)
626 /* Note: we do not have a reliable method to detect if the floppy is
627 present. The current method is to try to open the floppy at every
628 I/O and to keep it opened during a few hundreds of ms. */
629 static int fd_open(BlockDriverState *bs)
631 BDRVRawState *s = bs->opaque;
632 int last_media_present;
634 if (s->type != FTYPE_FD)
636 last_media_present = (s->fd >= 0);
638 (qemu_get_clock(rt_clock) - s->fd_open_time) >= FD_OPEN_TIMEOUT) {
642 printf("Floppy closed\n");
646 if (s->fd_got_error &&
647 (qemu_get_clock(rt_clock) - s->fd_error_time) < FD_OPEN_TIMEOUT) {
649 printf("No floppy (open delayed)\n");
653 s->fd = open(bs->filename, s->fd_open_flags);
655 s->fd_error_time = qemu_get_clock(rt_clock);
657 if (last_media_present)
658 s->fd_media_changed = 1;
660 printf("No floppy\n");
665 printf("Floppy opened\n");
668 if (!last_media_present)
669 s->fd_media_changed = 1;
670 s->fd_open_time = qemu_get_clock(rt_clock);
675 static int fd_open(BlockDriverState *bs)
681 #if defined(__linux__)
683 static int raw_is_inserted(BlockDriverState *bs)
685 BDRVRawState *s = bs->opaque;
690 ret = ioctl(s->fd, CDROM_DRIVE_STATUS, CDSL_CURRENT);
691 if (ret == CDS_DISC_OK)
704 /* currently only used by fdc.c, but a CD version would be good too */
705 static int raw_media_changed(BlockDriverState *bs)
707 BDRVRawState *s = bs->opaque;
713 /* XXX: we do not have a true media changed indication. It
714 does not work if the floppy is changed without trying
717 ret = s->fd_media_changed;
718 s->fd_media_changed = 0;
720 printf("Floppy changed=%d\n", ret);
729 static int raw_eject(BlockDriverState *bs, int eject_flag)
731 BDRVRawState *s = bs->opaque;
736 if (ioctl (s->fd, CDROMEJECT, NULL) < 0)
737 perror("CDROMEJECT");
739 if (ioctl (s->fd, CDROMCLOSETRAY, NULL) < 0)
740 perror("CDROMEJECT");
750 fd = open(bs->filename, s->fd_open_flags | O_NONBLOCK);
752 if (ioctl(fd, FDEJECT, 0) < 0)
764 static int raw_set_locked(BlockDriverState *bs, int locked)
766 BDRVRawState *s = bs->opaque;
770 if (ioctl (s->fd, CDROM_LOCKDOOR, locked) < 0) {
771 /* Note: an error can happen if the distribution automatically
773 // perror("CDROM_LOCKDOOR");
784 static int raw_is_inserted(BlockDriverState *bs)
789 static int raw_media_changed(BlockDriverState *bs)
794 static int raw_eject(BlockDriverState *bs, int eject_flag)
799 static int raw_set_locked(BlockDriverState *bs, int locked)
806 BlockDriver bdrv_host_device = {
808 sizeof(BDRVRawState),
809 NULL, /* no probe for protocols */
817 .bdrv_aio_read = raw_aio_read,
818 .bdrv_aio_write = raw_aio_write,
819 .bdrv_aio_cancel = raw_aio_cancel,
820 .aiocb_size = sizeof(RawAIOCB),
821 .bdrv_pread = raw_pread,
822 .bdrv_pwrite = raw_pwrite,
823 .bdrv_getlength = raw_getlength,
825 /* removable device support */
826 .bdrv_is_inserted = raw_is_inserted,
827 .bdrv_media_changed = raw_media_changed,
828 .bdrv_eject = raw_eject,
829 .bdrv_set_locked = raw_set_locked,
834 /* XXX: use another file ? */
835 #include <winioctl.h>
840 typedef struct BDRVRawState {
843 char drive_path[16]; /* format: "d:\" */
846 typedef struct RawAIOCB {
847 BlockDriverAIOCB common;
853 int qemu_ftruncate64(int fd, int64_t length)
860 if ((GetVersion() & 0x80000000UL) && (length >> 32) != 0)
863 h = (HANDLE)_get_osfhandle(fd);
865 /* get current position, ftruncate do not change position */
867 li.LowPart = SetFilePointer (h, 0, &li.HighPart, FILE_CURRENT);
868 if (li.LowPart == 0xffffffffUL && GetLastError() != NO_ERROR)
872 if (!SetFilePointer(h, (DWORD) length, &high, FILE_BEGIN))
874 res = SetEndOfFile(h);
876 /* back to old position */
877 SetFilePointer(h, li.LowPart, &li.HighPart, FILE_BEGIN);
881 static int set_sparse(int fd)
884 return (int) DeviceIoControl((HANDLE)_get_osfhandle(fd), FSCTL_SET_SPARSE,
885 NULL, 0, NULL, 0, &returned, NULL);
888 static int raw_open(BlockDriverState *bs, const char *filename, int flags)
890 BDRVRawState *s = bs->opaque;
891 int access_flags, create_flags;
894 s->type = FTYPE_FILE;
896 if ((flags & BDRV_O_ACCESS) == O_RDWR) {
897 access_flags = GENERIC_READ | GENERIC_WRITE;
899 access_flags = GENERIC_READ;
901 if (flags & BDRV_O_CREAT) {
902 create_flags = CREATE_ALWAYS;
904 create_flags = OPEN_EXISTING;
909 overlapped = FILE_FLAG_OVERLAPPED;
911 s->hfile = CreateFile(filename, access_flags,
912 FILE_SHARE_READ, NULL,
913 create_flags, overlapped, 0);
914 if (s->hfile == INVALID_HANDLE_VALUE)
919 static int raw_pread(BlockDriverState *bs, int64_t offset,
920 uint8_t *buf, int count)
922 BDRVRawState *s = bs->opaque;
927 memset(&ov, 0, sizeof(ov));
929 ov.OffsetHigh = offset >> 32;
930 ret = ReadFile(s->hfile, buf, count, &ret_count, &ov);
932 ret = GetOverlappedResult(s->hfile, &ov, &ret_count, TRUE);
941 static int raw_pwrite(BlockDriverState *bs, int64_t offset,
942 const uint8_t *buf, int count)
944 BDRVRawState *s = bs->opaque;
949 memset(&ov, 0, sizeof(ov));
951 ov.OffsetHigh = offset >> 32;
952 ret = WriteFile(s->hfile, buf, count, &ret_count, &ov);
954 ret = GetOverlappedResult(s->hfile, &ov, &ret_count, TRUE);
964 static void raw_aio_cb(void *opaque)
966 RawAIOCB *acb = opaque;
967 BlockDriverState *bs = acb->common.bs;
968 BDRVRawState *s = bs->opaque;
972 ret = GetOverlappedResult(s->hfile, &acb->ov, &ret_count, TRUE);
973 if (!ret || ret_count != acb->count) {
974 acb->common.cb(acb->common.opaque, -EIO);
976 acb->common.cb(acb->common.opaque, 0);
981 static RawAIOCB *raw_aio_setup(BlockDriverState *bs,
982 int64_t sector_num, uint8_t *buf, int nb_sectors,
983 BlockDriverCompletionFunc *cb, void *opaque)
988 acb = qemu_aio_get(bs, cb, opaque);
990 acb->hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
992 qemu_aio_release(acb);
996 memset(&acb->ov, 0, sizeof(acb->ov));
997 offset = sector_num * 512;
998 acb->ov.Offset = offset;
999 acb->ov.OffsetHigh = offset >> 32;
1000 acb->ov.hEvent = acb->hEvent;
1001 acb->count = nb_sectors * 512;
1003 qemu_add_wait_object(acb->ov.hEvent, raw_aio_cb, acb);
1008 static BlockDriverAIOCB *raw_aio_read(BlockDriverState *bs,
1009 int64_t sector_num, uint8_t *buf, int nb_sectors,
1010 BlockDriverCompletionFunc *cb, void *opaque)
1012 BDRVRawState *s = bs->opaque;
1016 acb = raw_aio_setup(bs, sector_num, buf, nb_sectors, cb, opaque);
1019 ret = ReadFile(s->hfile, buf, acb->count, NULL, &acb->ov);
1021 qemu_aio_release(acb);
1025 qemu_aio_release(acb);
1027 return (BlockDriverAIOCB *)acb;
1030 static BlockDriverAIOCB *raw_aio_write(BlockDriverState *bs,
1031 int64_t sector_num, uint8_t *buf, int nb_sectors,
1032 BlockDriverCompletionFunc *cb, void *opaque)
1034 BDRVRawState *s = bs->opaque;
1038 acb = raw_aio_setup(bs, sector_num, buf, nb_sectors, cb, opaque);
1041 ret = WriteFile(s->hfile, buf, acb->count, NULL, &acb->ov);
1043 qemu_aio_release(acb);
1047 qemu_aio_release(acb);
1049 return (BlockDriverAIOCB *)acb;
1052 static void raw_aio_cancel(BlockDriverAIOCB *blockacb)
1055 RawAIOCB *acb = (RawAIOCB *)blockacb;
1056 BlockDriverState *bs = acb->common.bs;
1057 BDRVRawState *s = bs->opaque;
1059 qemu_del_wait_object(acb->ov.hEvent, raw_aio_cb, acb);
1060 /* XXX: if more than one async I/O it is not correct */
1062 qemu_aio_release(acb);
1066 static void raw_flush(BlockDriverState *bs)
1071 static void raw_close(BlockDriverState *bs)
1073 BDRVRawState *s = bs->opaque;
1074 CloseHandle(s->hfile);
1077 static int raw_truncate(BlockDriverState *bs, int64_t offset)
1079 BDRVRawState *s = bs->opaque;
1083 high = offset >> 32;
1084 if (!SetFilePointer(s->hfile, low, &high, FILE_BEGIN))
1086 if (!SetEndOfFile(s->hfile))
1091 static int64_t raw_getlength(BlockDriverState *bs)
1093 BDRVRawState *s = bs->opaque;
1095 ULARGE_INTEGER available, total, total_free;
1099 l.LowPart = GetFileSize(s->hfile, &l.HighPart);
1100 if (l.LowPart == 0xffffffffUL && GetLastError() != NO_ERROR)
1104 if (!GetDiskFreeSpaceEx(s->drive_path, &available, &total, &total_free))
1106 l.QuadPart = total.QuadPart;
1114 static int raw_create(const char *filename, int64_t total_size,
1115 const char *backing_file, int flags)
1119 if (flags || backing_file)
1122 fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY,
1127 ftruncate(fd, total_size * 512);
1132 void qemu_aio_init(void)
1136 void qemu_aio_poll(void)
1140 void qemu_aio_flush(void)
1144 void qemu_aio_wait_start(void)
1148 void qemu_aio_wait(void)
1155 void qemu_aio_wait_end(void)
1159 BlockDriver bdrv_raw = {
1161 sizeof(BDRVRawState),
1162 NULL, /* no probe for protocols */
1171 .bdrv_aio_read = raw_aio_read,
1172 .bdrv_aio_write = raw_aio_write,
1173 .bdrv_aio_cancel = raw_aio_cancel,
1174 .aiocb_size = sizeof(RawAIOCB);
1176 .protocol_name = "file",
1177 .bdrv_pread = raw_pread,
1178 .bdrv_pwrite = raw_pwrite,
1179 .bdrv_truncate = raw_truncate,
1180 .bdrv_getlength = raw_getlength,
1183 /***********************************************/
1186 static int find_cdrom(char *cdrom_name, int cdrom_name_size)
1188 char drives[256], *pdrv = drives;
1191 memset(drives, 0, sizeof(drives));
1192 GetLogicalDriveStrings(sizeof(drives), drives);
1193 while(pdrv[0] != '\0') {
1194 type = GetDriveType(pdrv);
1197 snprintf(cdrom_name, cdrom_name_size, "\\\\.\\%c:", pdrv[0]);
1201 pdrv += lstrlen(pdrv) + 1;
1206 static int find_device_type(BlockDriverState *bs, const char *filename)
1208 BDRVRawState *s = bs->opaque;
1212 if (strstart(filename, "\\\\.\\", &p) ||
1213 strstart(filename, "//./", &p)) {
1214 snprintf(s->drive_path, sizeof(s->drive_path), "%c:\\", p[0]);
1215 type = GetDriveType(s->drive_path);
1216 if (type == DRIVE_CDROM)
1225 static int hdev_open(BlockDriverState *bs, const char *filename, int flags)
1227 BDRVRawState *s = bs->opaque;
1228 int access_flags, create_flags;
1230 char device_name[64];
1232 if (strstart(filename, "/dev/cdrom", NULL)) {
1233 if (find_cdrom(device_name, sizeof(device_name)) < 0)
1235 filename = device_name;
1237 /* transform drive letters into device name */
1238 if (((filename[0] >= 'a' && filename[0] <= 'z') ||
1239 (filename[0] >= 'A' && filename[0] <= 'Z')) &&
1240 filename[1] == ':' && filename[2] == '\0') {
1241 snprintf(device_name, sizeof(device_name), "\\\\.\\%c:", filename[0]);
1242 filename = device_name;
1245 s->type = find_device_type(bs, filename);
1247 if ((flags & BDRV_O_ACCESS) == O_RDWR) {
1248 access_flags = GENERIC_READ | GENERIC_WRITE;
1250 access_flags = GENERIC_READ;
1252 create_flags = OPEN_EXISTING;
1257 overlapped = FILE_FLAG_OVERLAPPED;
1259 s->hfile = CreateFile(filename, access_flags,
1260 FILE_SHARE_READ, NULL,
1261 create_flags, overlapped, 0);
1262 if (s->hfile == INVALID_HANDLE_VALUE)
1268 /***********************************************/
1269 /* removable device additionnal commands */
1271 static int raw_is_inserted(BlockDriverState *bs)
1276 static int raw_media_changed(BlockDriverState *bs)
1281 static int raw_eject(BlockDriverState *bs, int eject_flag)
1285 if (s->type == FTYPE_FILE)
1288 DeviceIoControl(s->hfile, IOCTL_STORAGE_EJECT_MEDIA,
1289 NULL, 0, NULL, 0, &lpBytesReturned, NULL);
1291 DeviceIoControl(s->hfile, IOCTL_STORAGE_LOAD_MEDIA,
1292 NULL, 0, NULL, 0, &lpBytesReturned, NULL);
1296 static int raw_set_locked(BlockDriverState *bs, int locked)
1302 BlockDriver bdrv_host_device = {
1304 sizeof(BDRVRawState),
1305 NULL, /* no probe for protocols */
1314 .bdrv_aio_read = raw_aio_read,
1315 .bdrv_aio_write = raw_aio_write,
1316 .bdrv_aio_cancel = raw_aio_cancel,
1317 .aiocb_size = sizeof(RawAIOCB);
1319 .bdrv_pread = raw_pread,
1320 .bdrv_pwrite = raw_pwrite,
1321 .bdrv_getlength = raw_getlength,