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>
55 //#define DEBUG_FLOPPY
61 /* if the FD is not accessed during that time (in ms), we try to
62 reopen it to see if the disk has been changed */
63 #define FD_OPEN_TIMEOUT 1000
65 typedef struct BDRVRawState {
68 #if defined(__linux__)
69 /* linux floppy specific */
72 int64_t fd_error_time;
78 static int fd_open(BlockDriverState *bs);
80 static int raw_open(BlockDriverState *bs, const char *filename, int flags)
82 BDRVRawState *s = bs->opaque;
83 int fd, open_flags, ret;
85 open_flags = O_BINARY;
86 if ((flags & BDRV_O_ACCESS) == O_RDWR) {
89 open_flags |= O_RDONLY;
92 if (flags & BDRV_O_CREAT)
93 open_flags |= O_CREAT | O_TRUNC;
97 fd = open(filename, open_flags, 0644);
108 /* XXX: use host sector size if necessary with:
109 #ifdef DIOCGSECTORSIZE
111 unsigned int sectorsize = 512;
112 if (!ioctl(fd, DIOCGSECTORSIZE, §orsize) &&
113 sectorsize > bufsize)
114 bufsize = sectorsize;
118 u_int32_t blockSize = 512;
119 if ( !ioctl( fd, DKIOCGETBLOCKSIZE, &blockSize ) && blockSize > bufsize) {
125 static int raw_pread(BlockDriverState *bs, int64_t offset,
126 uint8_t *buf, int count)
128 BDRVRawState *s = bs->opaque;
135 lseek(s->fd, offset, SEEK_SET);
136 ret = read(s->fd, buf, count);
140 static int raw_pwrite(BlockDriverState *bs, int64_t offset,
141 const uint8_t *buf, int count)
143 BDRVRawState *s = bs->opaque;
150 lseek(s->fd, offset, SEEK_SET);
151 ret = write(s->fd, buf, count);
155 /***********************************************************/
156 /* Unix AIO using POSIX AIO */
158 typedef struct RawAIOCB {
159 BlockDriverAIOCB common;
161 struct RawAIOCB *next;
164 static int aio_sig_num = SIGUSR2;
165 static RawAIOCB *first_aio; /* AIO issued */
166 static int aio_initialized = 0;
168 static void aio_signal_handler(int signum)
171 CPUState *env = cpu_single_env;
173 /* stop the currently executing cpu because a timer occured */
174 cpu_interrupt(env, CPU_INTERRUPT_EXIT);
176 if (env->kqemu_enabled) {
177 kqemu_cpu_interrupt(env);
184 void qemu_aio_init(void)
186 struct sigaction act;
190 sigfillset(&act.sa_mask);
191 act.sa_flags = 0; /* do not restart syscalls to interrupt select() */
192 act.sa_handler = aio_signal_handler;
193 sigaction(aio_sig_num, &act, NULL);
195 #if defined(__GLIBC__) && defined(__linux__)
197 /* XXX: aio thread exit seems to hang on RedHat 9 and this init
198 seems to fix the problem. */
200 memset(&ai, 0, sizeof(ai));
203 ai.aio_idle_time = 365 * 100000;
209 void qemu_aio_poll(void)
211 RawAIOCB *acb, **pacb;
220 ret = aio_error(&acb->aiocb);
221 if (ret == ECANCELED) {
222 /* remove the request */
224 qemu_aio_release(acb);
225 } else if (ret != EINPROGRESS) {
228 ret = aio_return(&acb->aiocb);
229 if (ret == acb->aiocb.aio_nbytes)
236 /* remove the request */
238 /* call the callback */
239 acb->common.cb(acb->common.opaque, ret);
240 qemu_aio_release(acb);
250 /* wait until at least one AIO was handled */
251 static sigset_t wait_oset;
253 void qemu_aio_wait_start(void)
257 if (!aio_initialized)
260 sigaddset(&set, aio_sig_num);
261 sigprocmask(SIG_BLOCK, &set, &wait_oset);
264 void qemu_aio_wait(void)
274 sigaddset(&set, aio_sig_num);
275 sigwait(&set, &nb_sigs);
279 void qemu_aio_wait_end(void)
281 sigprocmask(SIG_SETMASK, &wait_oset, NULL);
284 static RawAIOCB *raw_aio_setup(BlockDriverState *bs,
285 int64_t sector_num, uint8_t *buf, int nb_sectors,
286 BlockDriverCompletionFunc *cb, void *opaque)
288 BDRVRawState *s = bs->opaque;
294 acb = qemu_aio_get(bs, cb, opaque);
297 acb->aiocb.aio_fildes = s->fd;
298 acb->aiocb.aio_sigevent.sigev_signo = aio_sig_num;
299 acb->aiocb.aio_sigevent.sigev_notify = SIGEV_SIGNAL;
300 acb->aiocb.aio_buf = buf;
301 acb->aiocb.aio_nbytes = nb_sectors * 512;
302 acb->aiocb.aio_offset = sector_num * 512;
303 acb->next = first_aio;
308 static BlockDriverAIOCB *raw_aio_read(BlockDriverState *bs,
309 int64_t sector_num, uint8_t *buf, int nb_sectors,
310 BlockDriverCompletionFunc *cb, void *opaque)
314 acb = raw_aio_setup(bs, sector_num, buf, nb_sectors, cb, opaque);
317 if (aio_read(&acb->aiocb) < 0) {
318 qemu_aio_release(acb);
324 static BlockDriverAIOCB *raw_aio_write(BlockDriverState *bs,
325 int64_t sector_num, const uint8_t *buf, int nb_sectors,
326 BlockDriverCompletionFunc *cb, void *opaque)
330 acb = raw_aio_setup(bs, sector_num, (uint8_t*)buf, nb_sectors, cb, opaque);
333 if (aio_write(&acb->aiocb) < 0) {
334 qemu_aio_release(acb);
340 static void raw_aio_cancel(BlockDriverAIOCB *blockacb)
343 RawAIOCB *acb = (RawAIOCB *)blockacb;
346 ret = aio_cancel(acb->aiocb.aio_fildes, &acb->aiocb);
347 if (ret == AIO_NOTCANCELED) {
348 /* fail safe: if the aio could not be canceled, we wait for
350 while (aio_error(&acb->aiocb) == EINPROGRESS);
353 /* remove the callback from the queue */
358 } else if (*pacb == acb) {
360 qemu_aio_release(acb);
367 static void raw_close(BlockDriverState *bs)
369 BDRVRawState *s = bs->opaque;
376 static int raw_truncate(BlockDriverState *bs, int64_t offset)
378 BDRVRawState *s = bs->opaque;
379 if (s->type != FTYPE_FILE)
381 if (ftruncate(s->fd, offset) < 0)
386 static int64_t raw_getlength(BlockDriverState *bs)
388 BDRVRawState *s = bs->opaque;
395 struct dk_minfo minfo;
405 if (!fstat(fd, &sb) && (S_IFCHR & sb.st_mode)) {
406 #ifdef DIOCGMEDIASIZE
407 if (ioctl(fd, DIOCGMEDIASIZE, (off_t *)&size))
410 size = LONG_LONG_MAX;
412 size = lseek(fd, 0LL, SEEK_END);
418 * use the DKIOCGMEDIAINFO ioctl to read the size.
420 rv = ioctl ( fd, DKIOCGMEDIAINFO, &minfo );
422 size = minfo.dki_lbsize * minfo.dki_capacity;
423 } else /* there are reports that lseek on some devices
424 fails, but irc discussion said that contingency
425 on contingency was overkill */
428 size = lseek(fd, 0, SEEK_END);
433 static int raw_create(const char *filename, int64_t total_size,
434 const char *backing_file, int flags)
438 if (flags || backing_file)
441 fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY,
445 ftruncate(fd, total_size * 512);
450 static void raw_flush(BlockDriverState *bs)
452 BDRVRawState *s = bs->opaque;
456 BlockDriver bdrv_raw = {
458 sizeof(BDRVRawState),
459 NULL, /* no probe for protocols */
467 .bdrv_aio_read = raw_aio_read,
468 .bdrv_aio_write = raw_aio_write,
469 .bdrv_aio_cancel = raw_aio_cancel,
470 .aiocb_size = sizeof(RawAIOCB),
471 .protocol_name = "file",
472 .bdrv_pread = raw_pread,
473 .bdrv_pwrite = raw_pwrite,
474 .bdrv_truncate = raw_truncate,
475 .bdrv_getlength = raw_getlength,
478 /***********************************************/
482 static kern_return_t FindEjectableCDMedia( io_iterator_t *mediaIterator );
483 static kern_return_t GetBSDPath( io_iterator_t mediaIterator, char *bsdPath, CFIndex maxPathSize );
485 kern_return_t FindEjectableCDMedia( io_iterator_t *mediaIterator )
487 kern_return_t kernResult;
488 mach_port_t masterPort;
489 CFMutableDictionaryRef classesToMatch;
491 kernResult = IOMasterPort( MACH_PORT_NULL, &masterPort );
492 if ( KERN_SUCCESS != kernResult ) {
493 printf( "IOMasterPort returned %d\n", kernResult );
496 classesToMatch = IOServiceMatching( kIOCDMediaClass );
497 if ( classesToMatch == NULL ) {
498 printf( "IOServiceMatching returned a NULL dictionary.\n" );
500 CFDictionarySetValue( classesToMatch, CFSTR( kIOMediaEjectableKey ), kCFBooleanTrue );
502 kernResult = IOServiceGetMatchingServices( masterPort, classesToMatch, mediaIterator );
503 if ( KERN_SUCCESS != kernResult )
505 printf( "IOServiceGetMatchingServices returned %d\n", kernResult );
511 kern_return_t GetBSDPath( io_iterator_t mediaIterator, char *bsdPath, CFIndex maxPathSize )
513 io_object_t nextMedia;
514 kern_return_t kernResult = KERN_FAILURE;
516 nextMedia = IOIteratorNext( mediaIterator );
519 CFTypeRef bsdPathAsCFString;
520 bsdPathAsCFString = IORegistryEntryCreateCFProperty( nextMedia, CFSTR( kIOBSDNameKey ), kCFAllocatorDefault, 0 );
521 if ( bsdPathAsCFString ) {
522 size_t devPathLength;
523 strcpy( bsdPath, _PATH_DEV );
524 strcat( bsdPath, "r" );
525 devPathLength = strlen( bsdPath );
526 if ( CFStringGetCString( bsdPathAsCFString, bsdPath + devPathLength, maxPathSize - devPathLength, kCFStringEncodingASCII ) ) {
527 kernResult = KERN_SUCCESS;
529 CFRelease( bsdPathAsCFString );
531 IOObjectRelease( nextMedia );
539 static int hdev_open(BlockDriverState *bs, const char *filename, int flags)
541 BDRVRawState *s = bs->opaque;
542 int fd, open_flags, ret;
545 if (strstart(filename, "/dev/cdrom", NULL)) {
546 kern_return_t kernResult;
547 io_iterator_t mediaIterator;
548 char bsdPath[ MAXPATHLEN ];
551 kernResult = FindEjectableCDMedia( &mediaIterator );
552 kernResult = GetBSDPath( mediaIterator, bsdPath, sizeof( bsdPath ) );
554 if ( bsdPath[ 0 ] != '\0' ) {
555 strcat(bsdPath,"s0");
556 /* some CDs don't have a partition 0 */
557 fd = open(bsdPath, O_RDONLY | O_BINARY | O_LARGEFILE);
559 bsdPath[strlen(bsdPath)-1] = '1';
567 IOObjectRelease( mediaIterator );
570 open_flags = O_BINARY;
571 if ((flags & BDRV_O_ACCESS) == O_RDWR) {
572 open_flags |= O_RDWR;
574 open_flags |= O_RDONLY;
578 s->type = FTYPE_FILE;
579 #if defined(__linux__)
580 if (strstart(filename, "/dev/cd", NULL)) {
581 /* open will not fail even if no CD is inserted */
582 open_flags |= O_NONBLOCK;
584 } else if (strstart(filename, "/dev/fd", NULL)) {
586 s->fd_open_flags = open_flags;
587 /* open will not fail even if no floppy is inserted */
588 open_flags |= O_NONBLOCK;
591 fd = open(filename, open_flags, 0644);
599 #if defined(__linux__)
600 /* close fd so that we can reopen it as needed */
601 if (s->type == FTYPE_FD) {
604 s->fd_media_changed = 1;
610 #if defined(__linux__) && !defined(QEMU_TOOL)
612 /* Note: we do not have a reliable method to detect if the floppy is
613 present. The current method is to try to open the floppy at every
614 I/O and to keep it opened during a few hundreds of ms. */
615 static int fd_open(BlockDriverState *bs)
617 BDRVRawState *s = bs->opaque;
618 int last_media_present;
620 if (s->type != FTYPE_FD)
622 last_media_present = (s->fd >= 0);
624 (qemu_get_clock(rt_clock) - s->fd_open_time) >= FD_OPEN_TIMEOUT) {
628 printf("Floppy closed\n");
632 if (s->fd_got_error &&
633 (qemu_get_clock(rt_clock) - s->fd_error_time) < FD_OPEN_TIMEOUT) {
635 printf("No floppy (open delayed)\n");
639 s->fd = open(bs->filename, s->fd_open_flags);
641 s->fd_error_time = qemu_get_clock(rt_clock);
643 if (last_media_present)
644 s->fd_media_changed = 1;
646 printf("No floppy\n");
651 printf("Floppy opened\n");
654 if (!last_media_present)
655 s->fd_media_changed = 1;
656 s->fd_open_time = qemu_get_clock(rt_clock);
661 static int fd_open(BlockDriverState *bs)
667 #if defined(__linux__)
669 static int raw_is_inserted(BlockDriverState *bs)
671 BDRVRawState *s = bs->opaque;
676 ret = ioctl(s->fd, CDROM_DRIVE_STATUS, CDSL_CURRENT);
677 if (ret == CDS_DISC_OK)
690 /* currently only used by fdc.c, but a CD version would be good too */
691 static int raw_media_changed(BlockDriverState *bs)
693 BDRVRawState *s = bs->opaque;
699 /* XXX: we do not have a true media changed indication. It
700 does not work if the floppy is changed without trying
703 ret = s->fd_media_changed;
704 s->fd_media_changed = 0;
706 printf("Floppy changed=%d\n", ret);
715 static int raw_eject(BlockDriverState *bs, int eject_flag)
717 BDRVRawState *s = bs->opaque;
722 if (ioctl (s->fd, CDROMEJECT, NULL) < 0)
723 perror("CDROMEJECT");
725 if (ioctl (s->fd, CDROMCLOSETRAY, NULL) < 0)
726 perror("CDROMEJECT");
736 fd = open(bs->filename, s->fd_open_flags | O_NONBLOCK);
738 if (ioctl(fd, FDEJECT, 0) < 0)
750 static int raw_set_locked(BlockDriverState *bs, int locked)
752 BDRVRawState *s = bs->opaque;
756 if (ioctl (s->fd, CDROM_LOCKDOOR, locked) < 0) {
757 /* Note: an error can happen if the distribution automatically
759 // perror("CDROM_LOCKDOOR");
770 static int raw_is_inserted(BlockDriverState *bs)
775 static int raw_media_changed(BlockDriverState *bs)
780 static int raw_eject(BlockDriverState *bs, int eject_flag)
785 static int raw_set_locked(BlockDriverState *bs, int locked)
792 BlockDriver bdrv_host_device = {
794 sizeof(BDRVRawState),
795 NULL, /* no probe for protocols */
803 .bdrv_aio_read = raw_aio_read,
804 .bdrv_aio_write = raw_aio_write,
805 .bdrv_aio_cancel = raw_aio_cancel,
806 .aiocb_size = sizeof(RawAIOCB),
807 .bdrv_pread = raw_pread,
808 .bdrv_pwrite = raw_pwrite,
809 .bdrv_getlength = raw_getlength,
811 /* removable device support */
812 .bdrv_is_inserted = raw_is_inserted,
813 .bdrv_media_changed = raw_media_changed,
814 .bdrv_eject = raw_eject,
815 .bdrv_set_locked = raw_set_locked,
820 /* XXX: use another file ? */
821 #include <winioctl.h>
826 typedef struct BDRVRawState {
829 char drive_letter[2];
832 typedef struct RawAIOCB {
833 BlockDriverAIOCB common;
839 int qemu_ftruncate64(int fd, int64_t length)
846 if ((GetVersion() & 0x80000000UL) && (length >> 32) != 0)
849 h = (HANDLE)_get_osfhandle(fd);
851 /* get current position, ftruncate do not change position */
853 li.LowPart = SetFilePointer (h, 0, &li.HighPart, FILE_CURRENT);
854 if (li.LowPart == 0xffffffffUL && GetLastError() != NO_ERROR)
858 if (!SetFilePointer(h, (DWORD) length, &high, FILE_BEGIN))
860 res = SetEndOfFile(h);
862 /* back to old position */
863 SetFilePointer(h, li.LowPart, &li.HighPart, FILE_BEGIN);
867 static int set_sparse(int fd)
870 return (int) DeviceIoControl((HANDLE)_get_osfhandle(fd), FSCTL_SET_SPARSE,
871 NULL, 0, NULL, 0, &returned, NULL);
874 static int raw_open(BlockDriverState *bs, const char *filename, int flags)
876 BDRVRawState *s = bs->opaque;
877 int access_flags, create_flags;
879 char device_name[64];
882 if (strstart(filename, "/dev/cdrom", NULL)) {
883 if (find_cdrom(device_name, sizeof(device_name)) < 0)
885 filename = device_name;
887 /* transform drive letters into device name */
888 if (((filename[0] >= 'a' && filename[0] <= 'z') ||
889 (filename[0] >= 'A' && filename[0] <= 'Z')) &&
890 filename[1] == ':' && filename[2] == '\0') {
891 snprintf(device_name, sizeof(device_name), "\\\\.\\%c:", filename[0]);
892 filename = device_name;
895 s->type = find_device_type(filename);
897 if ((flags & BDRV_O_ACCESS) == O_RDWR) {
898 access_flags = GENERIC_READ | GENERIC_WRITE;
900 access_flags = GENERIC_READ;
902 if (flags & BDRV_O_CREAT) {
903 create_flags = CREATE_ALWAYS;
905 create_flags = OPEN_EXISTING;
910 overlapped = FILE_FLAG_OVERLAPPED;
912 s->hfile = CreateFile(filename, access_flags,
913 FILE_SHARE_READ, NULL,
914 create_flags, overlapped, 0);
915 if (s->hfile == INVALID_HANDLE_VALUE)
920 static int raw_pread(BlockDriverState *bs, int64_t offset,
921 uint8_t *buf, int count)
923 BDRVRawState *s = bs->opaque;
928 memset(&ov, 0, sizeof(ov));
930 ov.OffsetHigh = offset >> 32;
931 ret = ReadFile(s->hfile, buf, count, &ret_count, &ov);
933 ret = GetOverlappedResult(s->hfile, &ov, &ret_count, TRUE);
942 static int raw_pwrite(BlockDriverState *bs, int64_t offset,
943 const uint8_t *buf, int count)
945 BDRVRawState *s = bs->opaque;
950 memset(&ov, 0, sizeof(ov));
952 ov.OffsetHigh = offset >> 32;
953 ret = WriteFile(s->hfile, buf, count, &ret_count, &ov);
955 ret = GetOverlappedResult(s->hfile, &ov, &ret_count, TRUE);
965 static void raw_aio_cb(void *opaque)
967 RawAIOCB *acb = opaque;
968 BlockDriverState *bs = acb->common.bs;
969 BDRVRawState *s = bs->opaque;
973 ret = GetOverlappedResult(s->hfile, &acb->ov, &ret_count, TRUE);
974 if (!ret || ret_count != acb->count) {
975 acb->common.cb(acb->common.opaque, -EIO);
977 acb->common.cb(acb->common.opaque, 0);
982 static RawAIOCB *raw_aio_setup(BlockDriverState *bs,
983 int64_t sector_num, uint8_t *buf, int nb_sectors,
984 BlockDriverCompletionFunc *cb, void *opaque)
989 acb = qemu_aio_get(bs, cb, opaque);
991 acb->hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
993 qemu_aio_release(acb);
997 memset(&acb->ov, 0, sizeof(acb->ov));
998 offset = sector_num * 512;
999 acb->ov.Offset = offset;
1000 acb->ov.OffsetHigh = offset >> 32;
1001 acb->ov.hEvent = acb->hEvent;
1002 acb->count = nb_sectors * 512;
1004 qemu_add_wait_object(acb->ov.hEvent, raw_aio_cb, acb);
1009 static BlockDriverAIOCB *raw_aio_read(BlockDriverState *bs,
1010 int64_t sector_num, uint8_t *buf, int nb_sectors,
1011 BlockDriverCompletionFunc *cb, void *opaque)
1013 BDRVRawState *s = bs->opaque;
1017 acb = raw_aio_setup(bs, sector_num, buf, nb_sectors, cb, opaque);
1020 ret = ReadFile(s->hfile, buf, acb->count, NULL, &acb->ov);
1022 qemu_aio_release(acb);
1026 qemu_aio_release(acb);
1028 return (BlockDriverAIOCB *)acb;
1031 static BlockDriverAIOCB *raw_aio_write(BlockDriverState *bs,
1032 int64_t sector_num, uint8_t *buf, int nb_sectors,
1033 BlockDriverCompletionFunc *cb, void *opaque)
1035 BDRVRawState *s = bs->opaque;
1039 acb = raw_aio_setup(bs, sector_num, buf, nb_sectors, cb, opaque);
1042 ret = WriteFile(s->hfile, buf, acb->count, NULL, &acb->ov);
1044 qemu_aio_release(acb);
1048 qemu_aio_release(acb);
1050 return (BlockDriverAIOCB *)acb;
1053 static void raw_aio_cancel(BlockDriverAIOCB *blockacb)
1056 RawAIOCB *acb = (RawAIOCB *)blockacb;
1057 BlockDriverState *bs = acb->common.bs;
1058 BDRVRawState *s = bs->opaque;
1060 qemu_del_wait_object(acb->ov.hEvent, raw_aio_cb, acb);
1061 /* XXX: if more than one async I/O it is not correct */
1063 qemu_aio_release(acb);
1067 static void raw_flush(BlockDriverState *bs)
1072 static void raw_close(BlockDriverState *bs)
1074 BDRVRawState *s = bs->opaque;
1075 CloseHandle(s->hfile);
1078 static int raw_truncate(BlockDriverState *bs, int64_t offset)
1080 BDRVRawState *s = bs->opaque;
1084 high = offset >> 32;
1085 if (!SetFilePointer(s->hfile, low, &high, FILE_BEGIN))
1087 if (!SetEndOfFile(s->hfile))
1092 static int64_t raw_getlength(BlockDriverState *bs)
1094 BDRVRawState *s = bs->opaque;
1096 ULARGE_INTEGER available, total, total_free;
1100 l.LowPart = GetFileSize(s->hfile, &l.HighPart);
1101 if (l.LowPart == 0xffffffffUL && GetLastError() != NO_ERROR)
1105 if (!GetDiskFreeSpaceEx(s->drive_letter, &available, &total, &total_free))
1115 static int raw_create(const char *filename, int64_t total_size,
1116 const char *backing_file, int flags)
1120 if (flags || backing_file)
1123 fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY,
1128 ftruncate(fd, total_size * 512);
1133 void qemu_aio_init(void)
1137 void qemu_aio_poll(void)
1141 void qemu_aio_wait_start(void)
1145 void qemu_aio_wait(void)
1149 void qemu_aio_wait_end(void)
1153 BlockDriver bdrv_raw = {
1155 sizeof(BDRVRawState),
1156 NULL, /* no probe for protocols */
1165 .bdrv_aio_read = raw_aio_read,
1166 .bdrv_aio_write = raw_aio_write,
1167 .bdrv_aio_cancel = raw_aio_cancel,
1168 .aiocb_size = sizeof(RawAIOCB);
1170 .protocol_name = "file",
1171 .bdrv_pread = raw_pread,
1172 .bdrv_pwrite = raw_pwrite,
1173 .bdrv_truncate = raw_truncate,
1174 .bdrv_getlength = raw_getlength,
1177 /***********************************************/
1180 static int find_cdrom(char *cdrom_name, int cdrom_name_size)
1182 char drives[256], *pdrv = drives;
1185 memset(drives, 0, sizeof(drivers));
1186 GetLogicalDriveStrings(sizeof(drives), drives);
1187 while(pdrv[0] != '\0') {
1188 type = GetDriveType(pdrv);
1191 snprintf(cdrom_name, cdrom_name_size, "\\\\.\\%c:", pdrv[0]);
1195 pdrv += lstrlen(pdrv) + 1;
1200 static int find_device_type(const char *filename)
1205 if (strstart(filename, "\\\\.\\", &p) ||
1206 strstart(filename, "//./", &p)) {
1207 s->drive_letter[0] = p[0];
1208 s->drive_letter[1] = '\0';
1209 type = GetDriveType(s->drive_letter);
1210 if (type == DRIVE_CDROM)
1219 static int hdev_open(BlockDriverState *bs, const char *filename, int flags)
1221 BDRVRawState *s = bs->opaque;
1222 int access_flags, create_flags;
1224 char device_name[64];
1227 if (strstart(filename, "/dev/cdrom", NULL)) {
1228 if (find_cdrom(device_name, sizeof(device_name)) < 0)
1230 filename = device_name;
1232 /* transform drive letters into device name */
1233 if (((filename[0] >= 'a' && filename[0] <= 'z') ||
1234 (filename[0] >= 'A' && filename[0] <= 'Z')) &&
1235 filename[1] == ':' && filename[2] == '\0') {
1236 snprintf(device_name, sizeof(device_name), "\\\\.\\%c:", filename[0]);
1237 filename = device_name;
1240 s->type = find_device_type(filename);
1242 if ((flags & BDRV_O_ACCESS) == O_RDWR) {
1243 access_flags = GENERIC_READ | GENERIC_WRITE;
1245 access_flags = GENERIC_READ;
1247 create_flags = OPEN_EXISTING;
1252 overlapped = FILE_FLAG_OVERLAPPED;
1254 s->hfile = CreateFile(filename, access_flags,
1255 FILE_SHARE_READ, NULL,
1256 create_flags, overlapped, 0);
1257 if (s->hfile == INVALID_HANDLE_VALUE)
1263 /***********************************************/
1264 /* removable device additionnal commands */
1266 static int raw_is_inserted(BlockDriverState *bs)
1271 static int raw_media_changed(BlockDriverState *bs)
1276 static int raw_eject(BlockDriverState *bs, int eject_flag)
1280 if (s->type == FTYPE_FILE)
1283 DeviceIoControl(s->hfile, IOCTL_STORAGE_EJECT_MEDIA,
1284 NULL, 0, NULL, 0, &lpBytesReturned, NULL);
1286 DeviceIoControl(s->hfile, IOCTL_STORAGE_LOAD_MEDIA,
1287 NULL, 0, NULL, 0, &lpBytesReturned, NULL);
1291 static int raw_set_locked(BlockDriverState *bs, int locked)
1297 BlockDriver bdrv_host_device = {
1299 sizeof(BDRVRawState),
1300 NULL, /* no probe for protocols */
1309 .bdrv_aio_read = raw_aio_read,
1310 .bdrv_aio_write = raw_aio_write,
1311 .bdrv_aio_cancel = raw_aio_cancel,
1312 .aiocb_size = sizeof(RawAIOCB);
1314 .bdrv_pread = raw_pread,
1315 .bdrv_pwrite = raw_pwrite,
1316 .bdrv_getlength = raw_getlength,