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>
47 #define _POSIX_PTHREAD_SEMANTICS 1
52 #include <sys/ioctl.h>
53 #include <linux/cdrom.h>
60 //#define DEBUG_FLOPPY
66 /* if the FD is not accessed during that time (in ms), we try to
67 reopen it to see if the disk has been changed */
68 #define FD_OPEN_TIMEOUT 1000
70 typedef struct BDRVRawState {
73 #if defined(__linux__)
74 /* linux floppy specific */
77 int64_t fd_error_time;
83 static int fd_open(BlockDriverState *bs);
85 static int raw_open(BlockDriverState *bs, const char *filename, int flags)
87 BDRVRawState *s = bs->opaque;
88 int fd, open_flags, ret;
90 open_flags = O_BINARY;
91 if ((flags & BDRV_O_ACCESS) == O_RDWR) {
94 open_flags |= O_RDONLY;
97 if (flags & BDRV_O_CREAT)
98 open_flags |= O_CREAT | O_TRUNC;
100 s->type = FTYPE_FILE;
102 fd = open(filename, open_flags, 0644);
113 /* XXX: use host sector size if necessary with:
114 #ifdef DIOCGSECTORSIZE
116 unsigned int sectorsize = 512;
117 if (!ioctl(fd, DIOCGSECTORSIZE, §orsize) &&
118 sectorsize > bufsize)
119 bufsize = sectorsize;
123 u_int32_t blockSize = 512;
124 if ( !ioctl( fd, DKIOCGETBLOCKSIZE, &blockSize ) && blockSize > bufsize) {
130 static int raw_pread(BlockDriverState *bs, int64_t offset,
131 uint8_t *buf, int count)
133 BDRVRawState *s = bs->opaque;
140 lseek(s->fd, offset, SEEK_SET);
141 ret = read(s->fd, buf, count);
145 static int raw_pwrite(BlockDriverState *bs, int64_t offset,
146 const uint8_t *buf, int count)
148 BDRVRawState *s = bs->opaque;
155 lseek(s->fd, offset, SEEK_SET);
156 ret = write(s->fd, buf, count);
160 /***********************************************************/
161 /* Unix AIO using POSIX AIO */
163 typedef struct RawAIOCB {
164 BlockDriverAIOCB common;
166 struct RawAIOCB *next;
169 static int aio_sig_num = SIGUSR2;
170 static RawAIOCB *first_aio; /* AIO issued */
171 static int aio_initialized = 0;
173 static void aio_signal_handler(int signum)
176 CPUState *env = cpu_single_env;
178 /* stop the currently executing cpu because a timer occured */
179 cpu_interrupt(env, CPU_INTERRUPT_EXIT);
181 if (env->kqemu_enabled) {
182 kqemu_cpu_interrupt(env);
189 void qemu_aio_init(void)
191 struct sigaction act;
195 sigfillset(&act.sa_mask);
196 act.sa_flags = 0; /* do not restart syscalls to interrupt select() */
197 act.sa_handler = aio_signal_handler;
198 sigaction(aio_sig_num, &act, NULL);
200 #if defined(__GLIBC__) && defined(__linux__)
202 /* XXX: aio thread exit seems to hang on RedHat 9 and this init
203 seems to fix the problem. */
205 memset(&ai, 0, sizeof(ai));
208 ai.aio_idle_time = 365 * 100000;
214 void qemu_aio_poll(void)
216 RawAIOCB *acb, **pacb;
225 ret = aio_error(&acb->aiocb);
226 if (ret == ECANCELED) {
227 /* remove the request */
229 qemu_aio_release(acb);
230 } else if (ret != EINPROGRESS) {
233 ret = aio_return(&acb->aiocb);
234 if (ret == acb->aiocb.aio_nbytes)
241 /* remove the request */
243 /* call the callback */
244 acb->common.cb(acb->common.opaque, ret);
245 qemu_aio_release(acb);
255 /* Wait for all IO requests to complete. */
256 void qemu_aio_flush(void)
258 qemu_aio_wait_start();
266 /* wait until at least one AIO was handled */
267 static sigset_t wait_oset;
269 void qemu_aio_wait_start(void)
273 if (!aio_initialized)
276 sigaddset(&set, aio_sig_num);
277 sigprocmask(SIG_BLOCK, &set, &wait_oset);
280 void qemu_aio_wait(void)
290 sigaddset(&set, aio_sig_num);
291 sigwait(&set, &nb_sigs);
295 void qemu_aio_wait_end(void)
297 sigprocmask(SIG_SETMASK, &wait_oset, NULL);
300 static RawAIOCB *raw_aio_setup(BlockDriverState *bs,
301 int64_t sector_num, uint8_t *buf, int nb_sectors,
302 BlockDriverCompletionFunc *cb, void *opaque)
304 BDRVRawState *s = bs->opaque;
310 acb = qemu_aio_get(bs, cb, opaque);
313 acb->aiocb.aio_fildes = s->fd;
314 acb->aiocb.aio_sigevent.sigev_signo = aio_sig_num;
315 acb->aiocb.aio_sigevent.sigev_notify = SIGEV_SIGNAL;
316 acb->aiocb.aio_buf = buf;
317 acb->aiocb.aio_nbytes = nb_sectors * 512;
318 acb->aiocb.aio_offset = sector_num * 512;
319 acb->next = first_aio;
324 static BlockDriverAIOCB *raw_aio_read(BlockDriverState *bs,
325 int64_t sector_num, uint8_t *buf, int nb_sectors,
326 BlockDriverCompletionFunc *cb, void *opaque)
330 acb = raw_aio_setup(bs, sector_num, buf, nb_sectors, cb, opaque);
333 if (aio_read(&acb->aiocb) < 0) {
334 qemu_aio_release(acb);
340 static BlockDriverAIOCB *raw_aio_write(BlockDriverState *bs,
341 int64_t sector_num, const uint8_t *buf, int nb_sectors,
342 BlockDriverCompletionFunc *cb, void *opaque)
346 acb = raw_aio_setup(bs, sector_num, (uint8_t*)buf, nb_sectors, cb, opaque);
349 if (aio_write(&acb->aiocb) < 0) {
350 qemu_aio_release(acb);
356 static void raw_aio_cancel(BlockDriverAIOCB *blockacb)
359 RawAIOCB *acb = (RawAIOCB *)blockacb;
362 ret = aio_cancel(acb->aiocb.aio_fildes, &acb->aiocb);
363 if (ret == AIO_NOTCANCELED) {
364 /* fail safe: if the aio could not be canceled, we wait for
366 while (aio_error(&acb->aiocb) == EINPROGRESS);
369 /* remove the callback from the queue */
374 } else if (*pacb == acb) {
376 qemu_aio_release(acb);
383 static void raw_close(BlockDriverState *bs)
385 BDRVRawState *s = bs->opaque;
392 static int raw_truncate(BlockDriverState *bs, int64_t offset)
394 BDRVRawState *s = bs->opaque;
395 if (s->type != FTYPE_FILE)
397 if (ftruncate(s->fd, offset) < 0)
402 static int64_t raw_getlength(BlockDriverState *bs)
404 BDRVRawState *s = bs->opaque;
411 struct dk_minfo minfo;
421 if (!fstat(fd, &sb) && (S_IFCHR & sb.st_mode)) {
422 #ifdef DIOCGMEDIASIZE
423 if (ioctl(fd, DIOCGMEDIASIZE, (off_t *)&size))
426 size = LONG_LONG_MAX;
428 size = lseek(fd, 0LL, SEEK_END);
434 * use the DKIOCGMEDIAINFO ioctl to read the size.
436 rv = ioctl ( fd, DKIOCGMEDIAINFO, &minfo );
438 size = minfo.dki_lbsize * minfo.dki_capacity;
439 } else /* there are reports that lseek on some devices
440 fails, but irc discussion said that contingency
441 on contingency was overkill */
444 size = lseek(fd, 0, SEEK_END);
449 static int raw_create(const char *filename, int64_t total_size,
450 const char *backing_file, int flags)
454 if (flags || backing_file)
457 fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY,
461 ftruncate(fd, total_size * 512);
466 static void raw_flush(BlockDriverState *bs)
468 BDRVRawState *s = bs->opaque;
472 BlockDriver bdrv_raw = {
474 sizeof(BDRVRawState),
475 NULL, /* no probe for protocols */
483 .bdrv_aio_read = raw_aio_read,
484 .bdrv_aio_write = raw_aio_write,
485 .bdrv_aio_cancel = raw_aio_cancel,
486 .aiocb_size = sizeof(RawAIOCB),
487 .protocol_name = "file",
488 .bdrv_pread = raw_pread,
489 .bdrv_pwrite = raw_pwrite,
490 .bdrv_truncate = raw_truncate,
491 .bdrv_getlength = raw_getlength,
494 /***********************************************/
498 static kern_return_t FindEjectableCDMedia( io_iterator_t *mediaIterator );
499 static kern_return_t GetBSDPath( io_iterator_t mediaIterator, char *bsdPath, CFIndex maxPathSize );
501 kern_return_t FindEjectableCDMedia( io_iterator_t *mediaIterator )
503 kern_return_t kernResult;
504 mach_port_t masterPort;
505 CFMutableDictionaryRef classesToMatch;
507 kernResult = IOMasterPort( MACH_PORT_NULL, &masterPort );
508 if ( KERN_SUCCESS != kernResult ) {
509 printf( "IOMasterPort returned %d\n", kernResult );
512 classesToMatch = IOServiceMatching( kIOCDMediaClass );
513 if ( classesToMatch == NULL ) {
514 printf( "IOServiceMatching returned a NULL dictionary.\n" );
516 CFDictionarySetValue( classesToMatch, CFSTR( kIOMediaEjectableKey ), kCFBooleanTrue );
518 kernResult = IOServiceGetMatchingServices( masterPort, classesToMatch, mediaIterator );
519 if ( KERN_SUCCESS != kernResult )
521 printf( "IOServiceGetMatchingServices returned %d\n", kernResult );
527 kern_return_t GetBSDPath( io_iterator_t mediaIterator, char *bsdPath, CFIndex maxPathSize )
529 io_object_t nextMedia;
530 kern_return_t kernResult = KERN_FAILURE;
532 nextMedia = IOIteratorNext( mediaIterator );
535 CFTypeRef bsdPathAsCFString;
536 bsdPathAsCFString = IORegistryEntryCreateCFProperty( nextMedia, CFSTR( kIOBSDNameKey ), kCFAllocatorDefault, 0 );
537 if ( bsdPathAsCFString ) {
538 size_t devPathLength;
539 strcpy( bsdPath, _PATH_DEV );
540 strcat( bsdPath, "r" );
541 devPathLength = strlen( bsdPath );
542 if ( CFStringGetCString( bsdPathAsCFString, bsdPath + devPathLength, maxPathSize - devPathLength, kCFStringEncodingASCII ) ) {
543 kernResult = KERN_SUCCESS;
545 CFRelease( bsdPathAsCFString );
547 IOObjectRelease( nextMedia );
555 static int hdev_open(BlockDriverState *bs, const char *filename, int flags)
557 BDRVRawState *s = bs->opaque;
558 int fd, open_flags, ret;
561 if (strstart(filename, "/dev/cdrom", NULL)) {
562 kern_return_t kernResult;
563 io_iterator_t mediaIterator;
564 char bsdPath[ MAXPATHLEN ];
567 kernResult = FindEjectableCDMedia( &mediaIterator );
568 kernResult = GetBSDPath( mediaIterator, bsdPath, sizeof( bsdPath ) );
570 if ( bsdPath[ 0 ] != '\0' ) {
571 strcat(bsdPath,"s0");
572 /* some CDs don't have a partition 0 */
573 fd = open(bsdPath, O_RDONLY | O_BINARY | O_LARGEFILE);
575 bsdPath[strlen(bsdPath)-1] = '1';
583 IOObjectRelease( mediaIterator );
586 open_flags = O_BINARY;
587 if ((flags & BDRV_O_ACCESS) == O_RDWR) {
588 open_flags |= O_RDWR;
590 open_flags |= O_RDONLY;
594 s->type = FTYPE_FILE;
595 #if defined(__linux__)
596 if (strstart(filename, "/dev/cd", NULL)) {
597 /* open will not fail even if no CD is inserted */
598 open_flags |= O_NONBLOCK;
600 } else if (strstart(filename, "/dev/fd", NULL)) {
602 s->fd_open_flags = open_flags;
603 /* open will not fail even if no floppy is inserted */
604 open_flags |= O_NONBLOCK;
607 fd = open(filename, open_flags, 0644);
615 #if defined(__linux__)
616 /* close fd so that we can reopen it as needed */
617 if (s->type == FTYPE_FD) {
620 s->fd_media_changed = 1;
626 #if defined(__linux__) && !defined(QEMU_TOOL)
628 /* Note: we do not have a reliable method to detect if the floppy is
629 present. The current method is to try to open the floppy at every
630 I/O and to keep it opened during a few hundreds of ms. */
631 static int fd_open(BlockDriverState *bs)
633 BDRVRawState *s = bs->opaque;
634 int last_media_present;
636 if (s->type != FTYPE_FD)
638 last_media_present = (s->fd >= 0);
640 (qemu_get_clock(rt_clock) - s->fd_open_time) >= FD_OPEN_TIMEOUT) {
644 printf("Floppy closed\n");
648 if (s->fd_got_error &&
649 (qemu_get_clock(rt_clock) - s->fd_error_time) < FD_OPEN_TIMEOUT) {
651 printf("No floppy (open delayed)\n");
655 s->fd = open(bs->filename, s->fd_open_flags);
657 s->fd_error_time = qemu_get_clock(rt_clock);
659 if (last_media_present)
660 s->fd_media_changed = 1;
662 printf("No floppy\n");
667 printf("Floppy opened\n");
670 if (!last_media_present)
671 s->fd_media_changed = 1;
672 s->fd_open_time = qemu_get_clock(rt_clock);
677 static int fd_open(BlockDriverState *bs)
683 #if defined(__linux__)
685 static int raw_is_inserted(BlockDriverState *bs)
687 BDRVRawState *s = bs->opaque;
692 ret = ioctl(s->fd, CDROM_DRIVE_STATUS, CDSL_CURRENT);
693 if (ret == CDS_DISC_OK)
706 /* currently only used by fdc.c, but a CD version would be good too */
707 static int raw_media_changed(BlockDriverState *bs)
709 BDRVRawState *s = bs->opaque;
715 /* XXX: we do not have a true media changed indication. It
716 does not work if the floppy is changed without trying
719 ret = s->fd_media_changed;
720 s->fd_media_changed = 0;
722 printf("Floppy changed=%d\n", ret);
731 static int raw_eject(BlockDriverState *bs, int eject_flag)
733 BDRVRawState *s = bs->opaque;
738 if (ioctl (s->fd, CDROMEJECT, NULL) < 0)
739 perror("CDROMEJECT");
741 if (ioctl (s->fd, CDROMCLOSETRAY, NULL) < 0)
742 perror("CDROMEJECT");
752 fd = open(bs->filename, s->fd_open_flags | O_NONBLOCK);
754 if (ioctl(fd, FDEJECT, 0) < 0)
766 static int raw_set_locked(BlockDriverState *bs, int locked)
768 BDRVRawState *s = bs->opaque;
772 if (ioctl (s->fd, CDROM_LOCKDOOR, locked) < 0) {
773 /* Note: an error can happen if the distribution automatically
775 // perror("CDROM_LOCKDOOR");
786 static int raw_is_inserted(BlockDriverState *bs)
791 static int raw_media_changed(BlockDriverState *bs)
796 static int raw_eject(BlockDriverState *bs, int eject_flag)
801 static int raw_set_locked(BlockDriverState *bs, int locked)
808 BlockDriver bdrv_host_device = {
810 sizeof(BDRVRawState),
811 NULL, /* no probe for protocols */
819 .bdrv_aio_read = raw_aio_read,
820 .bdrv_aio_write = raw_aio_write,
821 .bdrv_aio_cancel = raw_aio_cancel,
822 .aiocb_size = sizeof(RawAIOCB),
823 .bdrv_pread = raw_pread,
824 .bdrv_pwrite = raw_pwrite,
825 .bdrv_getlength = raw_getlength,
827 /* removable device support */
828 .bdrv_is_inserted = raw_is_inserted,
829 .bdrv_media_changed = raw_media_changed,
830 .bdrv_eject = raw_eject,
831 .bdrv_set_locked = raw_set_locked,
836 /* XXX: use another file ? */
837 #include <winioctl.h>
841 #define FTYPE_HARDDISK 2
843 typedef struct BDRVRawState {
846 char drive_path[16]; /* format: "d:\" */
849 typedef struct RawAIOCB {
850 BlockDriverAIOCB common;
856 int qemu_ftruncate64(int fd, int64_t length)
863 if ((GetVersion() & 0x80000000UL) && (length >> 32) != 0)
866 h = (HANDLE)_get_osfhandle(fd);
868 /* get current position, ftruncate do not change position */
870 li.LowPart = SetFilePointer (h, 0, &li.HighPart, FILE_CURRENT);
871 if (li.LowPart == 0xffffffffUL && GetLastError() != NO_ERROR)
875 if (!SetFilePointer(h, (DWORD) length, &high, FILE_BEGIN))
877 res = SetEndOfFile(h);
879 /* back to old position */
880 SetFilePointer(h, li.LowPart, &li.HighPart, FILE_BEGIN);
884 static int set_sparse(int fd)
887 return (int) DeviceIoControl((HANDLE)_get_osfhandle(fd), FSCTL_SET_SPARSE,
888 NULL, 0, NULL, 0, &returned, NULL);
891 static int raw_open(BlockDriverState *bs, const char *filename, int flags)
893 BDRVRawState *s = bs->opaque;
894 int access_flags, create_flags;
897 s->type = FTYPE_FILE;
899 if ((flags & BDRV_O_ACCESS) == O_RDWR) {
900 access_flags = GENERIC_READ | GENERIC_WRITE;
902 access_flags = GENERIC_READ;
904 if (flags & BDRV_O_CREAT) {
905 create_flags = CREATE_ALWAYS;
907 create_flags = OPEN_EXISTING;
910 overlapped = FILE_ATTRIBUTE_NORMAL;
912 overlapped = FILE_FLAG_OVERLAPPED;
914 s->hfile = CreateFile(filename, access_flags,
915 FILE_SHARE_READ, NULL,
916 create_flags, overlapped, NULL);
917 if (s->hfile == INVALID_HANDLE_VALUE) {
918 int err = GetLastError();
920 if (err == ERROR_ACCESS_DENIED)
927 static int raw_pread(BlockDriverState *bs, int64_t offset,
928 uint8_t *buf, int count)
930 BDRVRawState *s = bs->opaque;
935 memset(&ov, 0, sizeof(ov));
937 ov.OffsetHigh = offset >> 32;
938 ret = ReadFile(s->hfile, buf, count, &ret_count, &ov);
940 ret = GetOverlappedResult(s->hfile, &ov, &ret_count, TRUE);
949 static int raw_pwrite(BlockDriverState *bs, int64_t offset,
950 const uint8_t *buf, int count)
952 BDRVRawState *s = bs->opaque;
957 memset(&ov, 0, sizeof(ov));
959 ov.OffsetHigh = offset >> 32;
960 ret = WriteFile(s->hfile, buf, count, &ret_count, &ov);
962 ret = GetOverlappedResult(s->hfile, &ov, &ret_count, TRUE);
973 static void raw_aio_cb(void *opaque)
975 RawAIOCB *acb = opaque;
976 BlockDriverState *bs = acb->common.bs;
977 BDRVRawState *s = bs->opaque;
981 ret = GetOverlappedResult(s->hfile, &acb->ov, &ret_count, TRUE);
982 if (!ret || ret_count != acb->count) {
983 acb->common.cb(acb->common.opaque, -EIO);
985 acb->common.cb(acb->common.opaque, 0);
990 static RawAIOCB *raw_aio_setup(BlockDriverState *bs,
991 int64_t sector_num, uint8_t *buf, int nb_sectors,
992 BlockDriverCompletionFunc *cb, void *opaque)
997 acb = qemu_aio_get(bs, cb, opaque);
999 acb->hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
1001 qemu_aio_release(acb);
1005 memset(&acb->ov, 0, sizeof(acb->ov));
1006 offset = sector_num * 512;
1007 acb->ov.Offset = offset;
1008 acb->ov.OffsetHigh = offset >> 32;
1009 acb->ov.hEvent = acb->hEvent;
1010 acb->count = nb_sectors * 512;
1012 qemu_add_wait_object(acb->ov.hEvent, raw_aio_cb, acb);
1017 static BlockDriverAIOCB *raw_aio_read(BlockDriverState *bs,
1018 int64_t sector_num, uint8_t *buf, int nb_sectors,
1019 BlockDriverCompletionFunc *cb, void *opaque)
1021 BDRVRawState *s = bs->opaque;
1025 acb = raw_aio_setup(bs, sector_num, buf, nb_sectors, cb, opaque);
1028 ret = ReadFile(s->hfile, buf, acb->count, NULL, &acb->ov);
1030 qemu_aio_release(acb);
1034 qemu_aio_release(acb);
1036 return (BlockDriverAIOCB *)acb;
1039 static BlockDriverAIOCB *raw_aio_write(BlockDriverState *bs,
1040 int64_t sector_num, uint8_t *buf, int nb_sectors,
1041 BlockDriverCompletionFunc *cb, void *opaque)
1043 BDRVRawState *s = bs->opaque;
1047 acb = raw_aio_setup(bs, sector_num, buf, nb_sectors, cb, opaque);
1050 ret = WriteFile(s->hfile, buf, acb->count, NULL, &acb->ov);
1052 qemu_aio_release(acb);
1056 qemu_aio_release(acb);
1058 return (BlockDriverAIOCB *)acb;
1061 static void raw_aio_cancel(BlockDriverAIOCB *blockacb)
1064 RawAIOCB *acb = (RawAIOCB *)blockacb;
1065 BlockDriverState *bs = acb->common.bs;
1066 BDRVRawState *s = bs->opaque;
1068 qemu_del_wait_object(acb->ov.hEvent, raw_aio_cb, acb);
1069 /* XXX: if more than one async I/O it is not correct */
1071 qemu_aio_release(acb);
1076 static void raw_flush(BlockDriverState *bs)
1078 BDRVRawState *s = bs->opaque;
1079 FlushFileBuffers(s->hfile);
1082 static void raw_close(BlockDriverState *bs)
1084 BDRVRawState *s = bs->opaque;
1085 CloseHandle(s->hfile);
1088 static int raw_truncate(BlockDriverState *bs, int64_t offset)
1090 BDRVRawState *s = bs->opaque;
1094 high = offset >> 32;
1095 if (!SetFilePointer(s->hfile, low, &high, FILE_BEGIN))
1097 if (!SetEndOfFile(s->hfile))
1102 static int64_t raw_getlength(BlockDriverState *bs)
1104 BDRVRawState *s = bs->opaque;
1106 ULARGE_INTEGER available, total, total_free;
1113 l.LowPart = GetFileSize(s->hfile, &l.HighPart);
1114 if (l.LowPart == 0xffffffffUL && GetLastError() != NO_ERROR)
1118 if (!GetDiskFreeSpaceEx(s->drive_path, &available, &total, &total_free))
1120 l.QuadPart = total.QuadPart;
1122 case FTYPE_HARDDISK:
1123 status = DeviceIoControl(s->hfile, IOCTL_DISK_GET_DRIVE_GEOMETRY,
1124 NULL, 0, &dg, sizeof(dg), &count, NULL);
1125 if (status != FALSE) {
1126 l.QuadPart = dg.Cylinders.QuadPart * dg.TracksPerCylinder
1127 * dg.SectorsPerTrack * dg.BytesPerSector;
1136 static int raw_create(const char *filename, int64_t total_size,
1137 const char *backing_file, int flags)
1141 if (flags || backing_file)
1144 fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY,
1149 ftruncate(fd, total_size * 512);
1154 void qemu_aio_init(void)
1158 void qemu_aio_poll(void)
1162 void qemu_aio_flush(void)
1166 void qemu_aio_wait_start(void)
1170 void qemu_aio_wait(void)
1177 void qemu_aio_wait_end(void)
1181 BlockDriver bdrv_raw = {
1183 sizeof(BDRVRawState),
1184 NULL, /* no probe for protocols */
1193 .bdrv_aio_read = raw_aio_read,
1194 .bdrv_aio_write = raw_aio_write,
1195 .bdrv_aio_cancel = raw_aio_cancel,
1196 .aiocb_size = sizeof(RawAIOCB);
1198 .protocol_name = "file",
1199 .bdrv_pread = raw_pread,
1200 .bdrv_pwrite = raw_pwrite,
1201 .bdrv_truncate = raw_truncate,
1202 .bdrv_getlength = raw_getlength,
1205 /***********************************************/
1208 static int find_cdrom(char *cdrom_name, int cdrom_name_size)
1210 char drives[256], *pdrv = drives;
1213 memset(drives, 0, sizeof(drives));
1214 GetLogicalDriveStrings(sizeof(drives), drives);
1215 while(pdrv[0] != '\0') {
1216 type = GetDriveType(pdrv);
1219 snprintf(cdrom_name, cdrom_name_size, "\\\\.\\%c:", pdrv[0]);
1223 pdrv += lstrlen(pdrv) + 1;
1228 static int find_device_type(BlockDriverState *bs, const char *filename)
1230 BDRVRawState *s = bs->opaque;
1234 if (strstart(filename, "\\\\.\\", &p) ||
1235 strstart(filename, "//./", &p)) {
1236 if (stristart(p, "PhysicalDrive", NULL))
1237 return FTYPE_HARDDISK;
1238 snprintf(s->drive_path, sizeof(s->drive_path), "%c:\\", p[0]);
1239 type = GetDriveType(s->drive_path);
1240 if (type == DRIVE_CDROM)
1249 static int hdev_open(BlockDriverState *bs, const char *filename, int flags)
1251 BDRVRawState *s = bs->opaque;
1252 int access_flags, create_flags;
1254 char device_name[64];
1256 if (strstart(filename, "/dev/cdrom", NULL)) {
1257 if (find_cdrom(device_name, sizeof(device_name)) < 0)
1259 filename = device_name;
1261 /* transform drive letters into device name */
1262 if (((filename[0] >= 'a' && filename[0] <= 'z') ||
1263 (filename[0] >= 'A' && filename[0] <= 'Z')) &&
1264 filename[1] == ':' && filename[2] == '\0') {
1265 snprintf(device_name, sizeof(device_name), "\\\\.\\%c:", filename[0]);
1266 filename = device_name;
1269 s->type = find_device_type(bs, filename);
1271 if ((flags & BDRV_O_ACCESS) == O_RDWR) {
1272 access_flags = GENERIC_READ | GENERIC_WRITE;
1274 access_flags = GENERIC_READ;
1276 create_flags = OPEN_EXISTING;
1279 overlapped = FILE_ATTRIBUTE_NORMAL;
1281 overlapped = FILE_FLAG_OVERLAPPED;
1283 s->hfile = CreateFile(filename, access_flags,
1284 FILE_SHARE_READ, NULL,
1285 create_flags, overlapped, NULL);
1286 if (s->hfile == INVALID_HANDLE_VALUE) {
1287 int err = GetLastError();
1289 if (err == ERROR_ACCESS_DENIED)
1297 /***********************************************/
1298 /* removable device additionnal commands */
1300 static int raw_is_inserted(BlockDriverState *bs)
1305 static int raw_media_changed(BlockDriverState *bs)
1310 static int raw_eject(BlockDriverState *bs, int eject_flag)
1314 if (s->type == FTYPE_FILE)
1317 DeviceIoControl(s->hfile, IOCTL_STORAGE_EJECT_MEDIA,
1318 NULL, 0, NULL, 0, &lpBytesReturned, NULL);
1320 DeviceIoControl(s->hfile, IOCTL_STORAGE_LOAD_MEDIA,
1321 NULL, 0, NULL, 0, &lpBytesReturned, NULL);
1325 static int raw_set_locked(BlockDriverState *bs, int locked)
1331 BlockDriver bdrv_host_device = {
1333 sizeof(BDRVRawState),
1334 NULL, /* no probe for protocols */
1343 .bdrv_aio_read = raw_aio_read,
1344 .bdrv_aio_write = raw_aio_write,
1345 .bdrv_aio_cancel = raw_aio_cancel,
1346 .aiocb_size = sizeof(RawAIOCB);
1348 .bdrv_pread = raw_pread,
1349 .bdrv_pwrite = raw_pwrite,
1350 .bdrv_getlength = raw_getlength,