ARM timer counts down, not up.
[qemu/mini2440.git] / block-raw.c
blob3a2843c5d66119dd1a9decfb0f8c1910a4601054
1 /*
2 * Block driver for RAW files
3 *
4 * Copyright (c) 2006 Fabrice Bellard
5 *
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
22 * THE SOFTWARE.
24 #include "vl.h"
25 #include "block_int.h"
26 #include <assert.h>
27 #ifndef _WIN32
28 #include <aio.h>
30 #ifndef QEMU_TOOL
31 #include "exec-all.h"
32 #endif
34 #ifdef CONFIG_COCOA
35 #include <paths.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>
44 #endif
46 #ifdef __sun__
47 #include <sys/dkio.h>
48 #endif
49 #ifdef __linux__
50 #include <sys/ioctl.h>
51 #include <linux/cdrom.h>
52 #include <linux/fd.h>
53 #endif
55 //#define DEBUG_FLOPPY
57 #define FTYPE_FILE 0
58 #define FTYPE_CD 1
59 #define FTYPE_FD 2
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 {
66 int fd;
67 int type;
68 #if defined(__linux__)
69 /* linux floppy specific */
70 int fd_open_flags;
71 int64_t fd_open_time;
72 int64_t fd_error_time;
73 int fd_got_error;
74 int fd_media_changed;
75 #endif
76 } BDRVRawState;
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) {
87 open_flags |= O_RDWR;
88 } else {
89 open_flags |= O_RDONLY;
90 bs->read_only = 1;
92 if (flags & BDRV_O_CREAT)
93 open_flags |= O_CREAT | O_TRUNC;
95 s->type = FTYPE_FILE;
97 fd = open(filename, open_flags, 0644);
98 if (fd < 0) {
99 ret = -errno;
100 if (ret == -EROFS)
101 ret = -EACCES;
102 return ret;
104 s->fd = fd;
105 return 0;
108 /* XXX: use host sector size if necessary with:
109 #ifdef DIOCGSECTORSIZE
111 unsigned int sectorsize = 512;
112 if (!ioctl(fd, DIOCGSECTORSIZE, &sectorsize) &&
113 sectorsize > bufsize)
114 bufsize = sectorsize;
116 #endif
117 #ifdef CONFIG_COCOA
118 u_int32_t blockSize = 512;
119 if ( !ioctl( fd, DKIOCGETBLOCKSIZE, &blockSize ) && blockSize > bufsize) {
120 bufsize = blockSize;
122 #endif
125 static int raw_pread(BlockDriverState *bs, int64_t offset,
126 uint8_t *buf, int count)
128 BDRVRawState *s = bs->opaque;
129 int ret;
131 ret = fd_open(bs);
132 if (ret < 0)
133 return ret;
135 lseek(s->fd, offset, SEEK_SET);
136 ret = read(s->fd, buf, count);
137 return ret;
140 static int raw_pwrite(BlockDriverState *bs, int64_t offset,
141 const uint8_t *buf, int count)
143 BDRVRawState *s = bs->opaque;
144 int ret;
146 ret = fd_open(bs);
147 if (ret < 0)
148 return ret;
150 lseek(s->fd, offset, SEEK_SET);
151 ret = write(s->fd, buf, count);
152 return ret;
155 /***********************************************************/
156 /* Unix AIO using POSIX AIO */
158 typedef struct RawAIOCB {
159 BlockDriverAIOCB common;
160 struct aiocb aiocb;
161 struct RawAIOCB *next;
162 } RawAIOCB;
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)
170 #ifndef QEMU_TOOL
171 CPUState *env = cpu_single_env;
172 if (env) {
173 /* stop the currently executing cpu because a timer occured */
174 cpu_interrupt(env, CPU_INTERRUPT_EXIT);
175 #ifdef USE_KQEMU
176 if (env->kqemu_enabled) {
177 kqemu_cpu_interrupt(env);
179 #endif
181 #endif
184 void qemu_aio_init(void)
186 struct sigaction act;
188 aio_initialized = 1;
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. */
199 struct aioinit ai;
200 memset(&ai, 0, sizeof(ai));
201 ai.aio_threads = 1;
202 ai.aio_num = 1;
203 ai.aio_idle_time = 365 * 100000;
204 aio_init(&ai);
206 #endif
209 void qemu_aio_poll(void)
211 RawAIOCB *acb, **pacb;
212 int ret;
214 for(;;) {
215 pacb = &first_aio;
216 for(;;) {
217 acb = *pacb;
218 if (!acb)
219 goto the_end;
220 ret = aio_error(&acb->aiocb);
221 if (ret == ECANCELED) {
222 /* remove the request */
223 *pacb = acb->next;
224 qemu_aio_release(acb);
225 } else if (ret != EINPROGRESS) {
226 /* end of aio */
227 if (ret == 0) {
228 ret = aio_return(&acb->aiocb);
229 if (ret == acb->aiocb.aio_nbytes)
230 ret = 0;
231 else
232 ret = -EINVAL;
233 } else {
234 ret = -ret;
236 /* remove the request */
237 *pacb = acb->next;
238 /* call the callback */
239 acb->common.cb(acb->common.opaque, ret);
240 qemu_aio_release(acb);
241 break;
242 } else {
243 pacb = &acb->next;
247 the_end: ;
250 /* Wait for all IO requests to complete. */
251 void qemu_aio_flush(void)
253 qemu_aio_wait_start();
254 qemu_aio_poll();
255 while (first_aio) {
256 qemu_aio_wait();
258 qemu_aio_wait_end();
261 /* wait until at least one AIO was handled */
262 static sigset_t wait_oset;
264 void qemu_aio_wait_start(void)
266 sigset_t set;
268 if (!aio_initialized)
269 qemu_aio_init();
270 sigemptyset(&set);
271 sigaddset(&set, aio_sig_num);
272 sigprocmask(SIG_BLOCK, &set, &wait_oset);
275 void qemu_aio_wait(void)
277 sigset_t set;
278 int nb_sigs;
280 #ifndef QEMU_TOOL
281 if (qemu_bh_poll())
282 return;
283 #endif
284 sigemptyset(&set);
285 sigaddset(&set, aio_sig_num);
286 sigwait(&set, &nb_sigs);
287 qemu_aio_poll();
290 void qemu_aio_wait_end(void)
292 sigprocmask(SIG_SETMASK, &wait_oset, NULL);
295 static RawAIOCB *raw_aio_setup(BlockDriverState *bs,
296 int64_t sector_num, uint8_t *buf, int nb_sectors,
297 BlockDriverCompletionFunc *cb, void *opaque)
299 BDRVRawState *s = bs->opaque;
300 RawAIOCB *acb;
302 if (fd_open(bs) < 0)
303 return NULL;
305 acb = qemu_aio_get(bs, cb, opaque);
306 if (!acb)
307 return NULL;
308 acb->aiocb.aio_fildes = s->fd;
309 acb->aiocb.aio_sigevent.sigev_signo = aio_sig_num;
310 acb->aiocb.aio_sigevent.sigev_notify = SIGEV_SIGNAL;
311 acb->aiocb.aio_buf = buf;
312 acb->aiocb.aio_nbytes = nb_sectors * 512;
313 acb->aiocb.aio_offset = sector_num * 512;
314 acb->next = first_aio;
315 first_aio = acb;
316 return acb;
319 static BlockDriverAIOCB *raw_aio_read(BlockDriverState *bs,
320 int64_t sector_num, uint8_t *buf, int nb_sectors,
321 BlockDriverCompletionFunc *cb, void *opaque)
323 RawAIOCB *acb;
325 acb = raw_aio_setup(bs, sector_num, buf, nb_sectors, cb, opaque);
326 if (!acb)
327 return NULL;
328 if (aio_read(&acb->aiocb) < 0) {
329 qemu_aio_release(acb);
330 return NULL;
332 return &acb->common;
335 static BlockDriverAIOCB *raw_aio_write(BlockDriverState *bs,
336 int64_t sector_num, const uint8_t *buf, int nb_sectors,
337 BlockDriverCompletionFunc *cb, void *opaque)
339 RawAIOCB *acb;
341 acb = raw_aio_setup(bs, sector_num, (uint8_t*)buf, nb_sectors, cb, opaque);
342 if (!acb)
343 return NULL;
344 if (aio_write(&acb->aiocb) < 0) {
345 qemu_aio_release(acb);
346 return NULL;
348 return &acb->common;
351 static void raw_aio_cancel(BlockDriverAIOCB *blockacb)
353 int ret;
354 RawAIOCB *acb = (RawAIOCB *)blockacb;
355 RawAIOCB **pacb;
357 ret = aio_cancel(acb->aiocb.aio_fildes, &acb->aiocb);
358 if (ret == AIO_NOTCANCELED) {
359 /* fail safe: if the aio could not be canceled, we wait for
360 it */
361 while (aio_error(&acb->aiocb) == EINPROGRESS);
364 /* remove the callback from the queue */
365 pacb = &first_aio;
366 for(;;) {
367 if (*pacb == NULL) {
368 break;
369 } else if (*pacb == acb) {
370 *pacb = acb->next;
371 qemu_aio_release(acb);
372 break;
374 pacb = &acb->next;
378 static void raw_close(BlockDriverState *bs)
380 BDRVRawState *s = bs->opaque;
381 if (s->fd >= 0) {
382 close(s->fd);
383 s->fd = -1;
387 static int raw_truncate(BlockDriverState *bs, int64_t offset)
389 BDRVRawState *s = bs->opaque;
390 if (s->type != FTYPE_FILE)
391 return -ENOTSUP;
392 if (ftruncate(s->fd, offset) < 0)
393 return -errno;
394 return 0;
397 static int64_t raw_getlength(BlockDriverState *bs)
399 BDRVRawState *s = bs->opaque;
400 int fd = s->fd;
401 int64_t size;
402 #ifdef _BSD
403 struct stat sb;
404 #endif
405 #ifdef __sun__
406 struct dk_minfo minfo;
407 int rv;
408 #endif
409 int ret;
411 ret = fd_open(bs);
412 if (ret < 0)
413 return ret;
415 #ifdef _BSD
416 if (!fstat(fd, &sb) && (S_IFCHR & sb.st_mode)) {
417 #ifdef DIOCGMEDIASIZE
418 if (ioctl(fd, DIOCGMEDIASIZE, (off_t *)&size))
419 #endif
420 #ifdef CONFIG_COCOA
421 size = LONG_LONG_MAX;
422 #else
423 size = lseek(fd, 0LL, SEEK_END);
424 #endif
425 } else
426 #endif
427 #ifdef __sun__
429 * use the DKIOCGMEDIAINFO ioctl to read the size.
431 rv = ioctl ( fd, DKIOCGMEDIAINFO, &minfo );
432 if ( rv != -1 ) {
433 size = minfo.dki_lbsize * minfo.dki_capacity;
434 } else /* there are reports that lseek on some devices
435 fails, but irc discussion said that contingency
436 on contingency was overkill */
437 #endif
439 size = lseek(fd, 0, SEEK_END);
441 return size;
444 static int raw_create(const char *filename, int64_t total_size,
445 const char *backing_file, int flags)
447 int fd;
449 if (flags || backing_file)
450 return -ENOTSUP;
452 fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY,
453 0644);
454 if (fd < 0)
455 return -EIO;
456 ftruncate(fd, total_size * 512);
457 close(fd);
458 return 0;
461 static void raw_flush(BlockDriverState *bs)
463 BDRVRawState *s = bs->opaque;
464 fsync(s->fd);
467 BlockDriver bdrv_raw = {
468 "raw",
469 sizeof(BDRVRawState),
470 NULL, /* no probe for protocols */
471 raw_open,
472 NULL,
473 NULL,
474 raw_close,
475 raw_create,
476 raw_flush,
478 .bdrv_aio_read = raw_aio_read,
479 .bdrv_aio_write = raw_aio_write,
480 .bdrv_aio_cancel = raw_aio_cancel,
481 .aiocb_size = sizeof(RawAIOCB),
482 .protocol_name = "file",
483 .bdrv_pread = raw_pread,
484 .bdrv_pwrite = raw_pwrite,
485 .bdrv_truncate = raw_truncate,
486 .bdrv_getlength = raw_getlength,
489 /***********************************************/
490 /* host device */
492 #ifdef CONFIG_COCOA
493 static kern_return_t FindEjectableCDMedia( io_iterator_t *mediaIterator );
494 static kern_return_t GetBSDPath( io_iterator_t mediaIterator, char *bsdPath, CFIndex maxPathSize );
496 kern_return_t FindEjectableCDMedia( io_iterator_t *mediaIterator )
498 kern_return_t kernResult;
499 mach_port_t masterPort;
500 CFMutableDictionaryRef classesToMatch;
502 kernResult = IOMasterPort( MACH_PORT_NULL, &masterPort );
503 if ( KERN_SUCCESS != kernResult ) {
504 printf( "IOMasterPort returned %d\n", kernResult );
507 classesToMatch = IOServiceMatching( kIOCDMediaClass );
508 if ( classesToMatch == NULL ) {
509 printf( "IOServiceMatching returned a NULL dictionary.\n" );
510 } else {
511 CFDictionarySetValue( classesToMatch, CFSTR( kIOMediaEjectableKey ), kCFBooleanTrue );
513 kernResult = IOServiceGetMatchingServices( masterPort, classesToMatch, mediaIterator );
514 if ( KERN_SUCCESS != kernResult )
516 printf( "IOServiceGetMatchingServices returned %d\n", kernResult );
519 return kernResult;
522 kern_return_t GetBSDPath( io_iterator_t mediaIterator, char *bsdPath, CFIndex maxPathSize )
524 io_object_t nextMedia;
525 kern_return_t kernResult = KERN_FAILURE;
526 *bsdPath = '\0';
527 nextMedia = IOIteratorNext( mediaIterator );
528 if ( nextMedia )
530 CFTypeRef bsdPathAsCFString;
531 bsdPathAsCFString = IORegistryEntryCreateCFProperty( nextMedia, CFSTR( kIOBSDNameKey ), kCFAllocatorDefault, 0 );
532 if ( bsdPathAsCFString ) {
533 size_t devPathLength;
534 strcpy( bsdPath, _PATH_DEV );
535 strcat( bsdPath, "r" );
536 devPathLength = strlen( bsdPath );
537 if ( CFStringGetCString( bsdPathAsCFString, bsdPath + devPathLength, maxPathSize - devPathLength, kCFStringEncodingASCII ) ) {
538 kernResult = KERN_SUCCESS;
540 CFRelease( bsdPathAsCFString );
542 IOObjectRelease( nextMedia );
545 return kernResult;
548 #endif
550 static int hdev_open(BlockDriverState *bs, const char *filename, int flags)
552 BDRVRawState *s = bs->opaque;
553 int fd, open_flags, ret;
555 #ifdef CONFIG_COCOA
556 if (strstart(filename, "/dev/cdrom", NULL)) {
557 kern_return_t kernResult;
558 io_iterator_t mediaIterator;
559 char bsdPath[ MAXPATHLEN ];
560 int fd;
562 kernResult = FindEjectableCDMedia( &mediaIterator );
563 kernResult = GetBSDPath( mediaIterator, bsdPath, sizeof( bsdPath ) );
565 if ( bsdPath[ 0 ] != '\0' ) {
566 strcat(bsdPath,"s0");
567 /* some CDs don't have a partition 0 */
568 fd = open(bsdPath, O_RDONLY | O_BINARY | O_LARGEFILE);
569 if (fd < 0) {
570 bsdPath[strlen(bsdPath)-1] = '1';
571 } else {
572 close(fd);
574 filename = bsdPath;
577 if ( mediaIterator )
578 IOObjectRelease( mediaIterator );
580 #endif
581 open_flags = O_BINARY;
582 if ((flags & BDRV_O_ACCESS) == O_RDWR) {
583 open_flags |= O_RDWR;
584 } else {
585 open_flags |= O_RDONLY;
586 bs->read_only = 1;
589 s->type = FTYPE_FILE;
590 #if defined(__linux__)
591 if (strstart(filename, "/dev/cd", NULL)) {
592 /* open will not fail even if no CD is inserted */
593 open_flags |= O_NONBLOCK;
594 s->type = FTYPE_CD;
595 } else if (strstart(filename, "/dev/fd", NULL)) {
596 s->type = FTYPE_FD;
597 s->fd_open_flags = open_flags;
598 /* open will not fail even if no floppy is inserted */
599 open_flags |= O_NONBLOCK;
601 #endif
602 fd = open(filename, open_flags, 0644);
603 if (fd < 0) {
604 ret = -errno;
605 if (ret == -EROFS)
606 ret = -EACCES;
607 return ret;
609 s->fd = fd;
610 #if defined(__linux__)
611 /* close fd so that we can reopen it as needed */
612 if (s->type == FTYPE_FD) {
613 close(s->fd);
614 s->fd = -1;
615 s->fd_media_changed = 1;
617 #endif
618 return 0;
621 #if defined(__linux__) && !defined(QEMU_TOOL)
623 /* Note: we do not have a reliable method to detect if the floppy is
624 present. The current method is to try to open the floppy at every
625 I/O and to keep it opened during a few hundreds of ms. */
626 static int fd_open(BlockDriverState *bs)
628 BDRVRawState *s = bs->opaque;
629 int last_media_present;
631 if (s->type != FTYPE_FD)
632 return 0;
633 last_media_present = (s->fd >= 0);
634 if (s->fd >= 0 &&
635 (qemu_get_clock(rt_clock) - s->fd_open_time) >= FD_OPEN_TIMEOUT) {
636 close(s->fd);
637 s->fd = -1;
638 #ifdef DEBUG_FLOPPY
639 printf("Floppy closed\n");
640 #endif
642 if (s->fd < 0) {
643 if (s->fd_got_error &&
644 (qemu_get_clock(rt_clock) - s->fd_error_time) < FD_OPEN_TIMEOUT) {
645 #ifdef DEBUG_FLOPPY
646 printf("No floppy (open delayed)\n");
647 #endif
648 return -EIO;
650 s->fd = open(bs->filename, s->fd_open_flags);
651 if (s->fd < 0) {
652 s->fd_error_time = qemu_get_clock(rt_clock);
653 s->fd_got_error = 1;
654 if (last_media_present)
655 s->fd_media_changed = 1;
656 #ifdef DEBUG_FLOPPY
657 printf("No floppy\n");
658 #endif
659 return -EIO;
661 #ifdef DEBUG_FLOPPY
662 printf("Floppy opened\n");
663 #endif
665 if (!last_media_present)
666 s->fd_media_changed = 1;
667 s->fd_open_time = qemu_get_clock(rt_clock);
668 s->fd_got_error = 0;
669 return 0;
671 #else
672 static int fd_open(BlockDriverState *bs)
674 return 0;
676 #endif
678 #if defined(__linux__)
680 static int raw_is_inserted(BlockDriverState *bs)
682 BDRVRawState *s = bs->opaque;
683 int ret;
685 switch(s->type) {
686 case FTYPE_CD:
687 ret = ioctl(s->fd, CDROM_DRIVE_STATUS, CDSL_CURRENT);
688 if (ret == CDS_DISC_OK)
689 return 1;
690 else
691 return 0;
692 break;
693 case FTYPE_FD:
694 ret = fd_open(bs);
695 return (ret >= 0);
696 default:
697 return 1;
701 /* currently only used by fdc.c, but a CD version would be good too */
702 static int raw_media_changed(BlockDriverState *bs)
704 BDRVRawState *s = bs->opaque;
706 switch(s->type) {
707 case FTYPE_FD:
709 int ret;
710 /* XXX: we do not have a true media changed indication. It
711 does not work if the floppy is changed without trying
712 to read it */
713 fd_open(bs);
714 ret = s->fd_media_changed;
715 s->fd_media_changed = 0;
716 #ifdef DEBUG_FLOPPY
717 printf("Floppy changed=%d\n", ret);
718 #endif
719 return ret;
721 default:
722 return -ENOTSUP;
726 static int raw_eject(BlockDriverState *bs, int eject_flag)
728 BDRVRawState *s = bs->opaque;
730 switch(s->type) {
731 case FTYPE_CD:
732 if (eject_flag) {
733 if (ioctl (s->fd, CDROMEJECT, NULL) < 0)
734 perror("CDROMEJECT");
735 } else {
736 if (ioctl (s->fd, CDROMCLOSETRAY, NULL) < 0)
737 perror("CDROMEJECT");
739 break;
740 case FTYPE_FD:
742 int fd;
743 if (s->fd >= 0) {
744 close(s->fd);
745 s->fd = -1;
747 fd = open(bs->filename, s->fd_open_flags | O_NONBLOCK);
748 if (fd >= 0) {
749 if (ioctl(fd, FDEJECT, 0) < 0)
750 perror("FDEJECT");
751 close(fd);
754 break;
755 default:
756 return -ENOTSUP;
758 return 0;
761 static int raw_set_locked(BlockDriverState *bs, int locked)
763 BDRVRawState *s = bs->opaque;
765 switch(s->type) {
766 case FTYPE_CD:
767 if (ioctl (s->fd, CDROM_LOCKDOOR, locked) < 0) {
768 /* Note: an error can happen if the distribution automatically
769 mounts the CD-ROM */
770 // perror("CDROM_LOCKDOOR");
772 break;
773 default:
774 return -ENOTSUP;
776 return 0;
779 #else
781 static int raw_is_inserted(BlockDriverState *bs)
783 return 1;
786 static int raw_media_changed(BlockDriverState *bs)
788 return -ENOTSUP;
791 static int raw_eject(BlockDriverState *bs, int eject_flag)
793 return -ENOTSUP;
796 static int raw_set_locked(BlockDriverState *bs, int locked)
798 return -ENOTSUP;
801 #endif /* !linux */
803 BlockDriver bdrv_host_device = {
804 "host_device",
805 sizeof(BDRVRawState),
806 NULL, /* no probe for protocols */
807 hdev_open,
808 NULL,
809 NULL,
810 raw_close,
811 NULL,
812 raw_flush,
814 .bdrv_aio_read = raw_aio_read,
815 .bdrv_aio_write = raw_aio_write,
816 .bdrv_aio_cancel = raw_aio_cancel,
817 .aiocb_size = sizeof(RawAIOCB),
818 .bdrv_pread = raw_pread,
819 .bdrv_pwrite = raw_pwrite,
820 .bdrv_getlength = raw_getlength,
822 /* removable device support */
823 .bdrv_is_inserted = raw_is_inserted,
824 .bdrv_media_changed = raw_media_changed,
825 .bdrv_eject = raw_eject,
826 .bdrv_set_locked = raw_set_locked,
829 #else /* _WIN32 */
831 /* XXX: use another file ? */
832 #include <winioctl.h>
834 #define FTYPE_FILE 0
835 #define FTYPE_CD 1
837 typedef struct BDRVRawState {
838 HANDLE hfile;
839 int type;
840 char drive_path[16]; /* format: "d:\" */
841 } BDRVRawState;
843 typedef struct RawAIOCB {
844 BlockDriverAIOCB common;
845 HANDLE hEvent;
846 OVERLAPPED ov;
847 int count;
848 } RawAIOCB;
850 int qemu_ftruncate64(int fd, int64_t length)
852 LARGE_INTEGER li;
853 LONG high;
854 HANDLE h;
855 BOOL res;
857 if ((GetVersion() & 0x80000000UL) && (length >> 32) != 0)
858 return -1;
860 h = (HANDLE)_get_osfhandle(fd);
862 /* get current position, ftruncate do not change position */
863 li.HighPart = 0;
864 li.LowPart = SetFilePointer (h, 0, &li.HighPart, FILE_CURRENT);
865 if (li.LowPart == 0xffffffffUL && GetLastError() != NO_ERROR)
866 return -1;
868 high = length >> 32;
869 if (!SetFilePointer(h, (DWORD) length, &high, FILE_BEGIN))
870 return -1;
871 res = SetEndOfFile(h);
873 /* back to old position */
874 SetFilePointer(h, li.LowPart, &li.HighPart, FILE_BEGIN);
875 return res ? 0 : -1;
878 static int set_sparse(int fd)
880 DWORD returned;
881 return (int) DeviceIoControl((HANDLE)_get_osfhandle(fd), FSCTL_SET_SPARSE,
882 NULL, 0, NULL, 0, &returned, NULL);
885 static int raw_open(BlockDriverState *bs, const char *filename, int flags)
887 BDRVRawState *s = bs->opaque;
888 int access_flags, create_flags;
889 DWORD overlapped;
891 s->type = FTYPE_FILE;
893 if ((flags & BDRV_O_ACCESS) == O_RDWR) {
894 access_flags = GENERIC_READ | GENERIC_WRITE;
895 } else {
896 access_flags = GENERIC_READ;
898 if (flags & BDRV_O_CREAT) {
899 create_flags = CREATE_ALWAYS;
900 } else {
901 create_flags = OPEN_EXISTING;
903 #ifdef QEMU_TOOL
904 overlapped = 0;
905 #else
906 overlapped = FILE_FLAG_OVERLAPPED;
907 #endif
908 s->hfile = CreateFile(filename, access_flags,
909 FILE_SHARE_READ, NULL,
910 create_flags, overlapped, 0);
911 if (s->hfile == INVALID_HANDLE_VALUE)
912 return -1;
913 return 0;
916 static int raw_pread(BlockDriverState *bs, int64_t offset,
917 uint8_t *buf, int count)
919 BDRVRawState *s = bs->opaque;
920 OVERLAPPED ov;
921 DWORD ret_count;
922 int ret;
924 memset(&ov, 0, sizeof(ov));
925 ov.Offset = offset;
926 ov.OffsetHigh = offset >> 32;
927 ret = ReadFile(s->hfile, buf, count, &ret_count, &ov);
928 if (!ret) {
929 ret = GetOverlappedResult(s->hfile, &ov, &ret_count, TRUE);
930 if (!ret)
931 return -EIO;
932 else
933 return ret_count;
935 return ret_count;
938 static int raw_pwrite(BlockDriverState *bs, int64_t offset,
939 const uint8_t *buf, int count)
941 BDRVRawState *s = bs->opaque;
942 OVERLAPPED ov;
943 DWORD ret_count;
944 int ret;
946 memset(&ov, 0, sizeof(ov));
947 ov.Offset = offset;
948 ov.OffsetHigh = offset >> 32;
949 ret = WriteFile(s->hfile, buf, count, &ret_count, &ov);
950 if (!ret) {
951 ret = GetOverlappedResult(s->hfile, &ov, &ret_count, TRUE);
952 if (!ret)
953 return -EIO;
954 else
955 return ret_count;
957 return ret_count;
960 #ifndef QEMU_TOOL
961 static void raw_aio_cb(void *opaque)
963 RawAIOCB *acb = opaque;
964 BlockDriverState *bs = acb->common.bs;
965 BDRVRawState *s = bs->opaque;
966 DWORD ret_count;
967 int ret;
969 ret = GetOverlappedResult(s->hfile, &acb->ov, &ret_count, TRUE);
970 if (!ret || ret_count != acb->count) {
971 acb->common.cb(acb->common.opaque, -EIO);
972 } else {
973 acb->common.cb(acb->common.opaque, 0);
976 #endif
978 static RawAIOCB *raw_aio_setup(BlockDriverState *bs,
979 int64_t sector_num, uint8_t *buf, int nb_sectors,
980 BlockDriverCompletionFunc *cb, void *opaque)
982 RawAIOCB *acb;
983 int64_t offset;
985 acb = qemu_aio_get(bs, cb, opaque);
986 if (acb->hEvent) {
987 acb->hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
988 if (!acb->hEvent) {
989 qemu_aio_release(acb);
990 return NULL;
993 memset(&acb->ov, 0, sizeof(acb->ov));
994 offset = sector_num * 512;
995 acb->ov.Offset = offset;
996 acb->ov.OffsetHigh = offset >> 32;
997 acb->ov.hEvent = acb->hEvent;
998 acb->count = nb_sectors * 512;
999 #ifndef QEMU_TOOL
1000 qemu_add_wait_object(acb->ov.hEvent, raw_aio_cb, acb);
1001 #endif
1002 return acb;
1005 static BlockDriverAIOCB *raw_aio_read(BlockDriverState *bs,
1006 int64_t sector_num, uint8_t *buf, int nb_sectors,
1007 BlockDriverCompletionFunc *cb, void *opaque)
1009 BDRVRawState *s = bs->opaque;
1010 RawAIOCB *acb;
1011 int ret;
1013 acb = raw_aio_setup(bs, sector_num, buf, nb_sectors, cb, opaque);
1014 if (!acb)
1015 return NULL;
1016 ret = ReadFile(s->hfile, buf, acb->count, NULL, &acb->ov);
1017 if (!ret) {
1018 qemu_aio_release(acb);
1019 return NULL;
1021 #ifdef QEMU_TOOL
1022 qemu_aio_release(acb);
1023 #endif
1024 return (BlockDriverAIOCB *)acb;
1027 static BlockDriverAIOCB *raw_aio_write(BlockDriverState *bs,
1028 int64_t sector_num, uint8_t *buf, int nb_sectors,
1029 BlockDriverCompletionFunc *cb, void *opaque)
1031 BDRVRawState *s = bs->opaque;
1032 RawAIOCB *acb;
1033 int ret;
1035 acb = raw_aio_setup(bs, sector_num, buf, nb_sectors, cb, opaque);
1036 if (!acb)
1037 return NULL;
1038 ret = WriteFile(s->hfile, buf, acb->count, NULL, &acb->ov);
1039 if (!ret) {
1040 qemu_aio_release(acb);
1041 return NULL;
1043 #ifdef QEMU_TOOL
1044 qemu_aio_release(acb);
1045 #endif
1046 return (BlockDriverAIOCB *)acb;
1049 static void raw_aio_cancel(BlockDriverAIOCB *blockacb)
1051 #ifndef QEMU_TOOL
1052 RawAIOCB *acb = (RawAIOCB *)blockacb;
1053 BlockDriverState *bs = acb->common.bs;
1054 BDRVRawState *s = bs->opaque;
1056 qemu_del_wait_object(acb->ov.hEvent, raw_aio_cb, acb);
1057 /* XXX: if more than one async I/O it is not correct */
1058 CancelIo(s->hfile);
1059 qemu_aio_release(acb);
1060 #endif
1063 static void raw_flush(BlockDriverState *bs)
1065 /* XXX: add it */
1068 static void raw_close(BlockDriverState *bs)
1070 BDRVRawState *s = bs->opaque;
1071 CloseHandle(s->hfile);
1074 static int raw_truncate(BlockDriverState *bs, int64_t offset)
1076 BDRVRawState *s = bs->opaque;
1077 DWORD low, high;
1079 low = offset;
1080 high = offset >> 32;
1081 if (!SetFilePointer(s->hfile, low, &high, FILE_BEGIN))
1082 return -EIO;
1083 if (!SetEndOfFile(s->hfile))
1084 return -EIO;
1085 return 0;
1088 static int64_t raw_getlength(BlockDriverState *bs)
1090 BDRVRawState *s = bs->opaque;
1091 LARGE_INTEGER l;
1092 ULARGE_INTEGER available, total, total_free;
1094 switch(s->type) {
1095 case FTYPE_FILE:
1096 l.LowPart = GetFileSize(s->hfile, &l.HighPart);
1097 if (l.LowPart == 0xffffffffUL && GetLastError() != NO_ERROR)
1098 return -EIO;
1099 break;
1100 case FTYPE_CD:
1101 if (!GetDiskFreeSpaceEx(s->drive_path, &available, &total, &total_free))
1102 return -EIO;
1103 l.QuadPart = total.QuadPart;
1104 break;
1105 default:
1106 return -EIO;
1108 return l.QuadPart;
1111 static int raw_create(const char *filename, int64_t total_size,
1112 const char *backing_file, int flags)
1114 int fd;
1116 if (flags || backing_file)
1117 return -ENOTSUP;
1119 fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY,
1120 0644);
1121 if (fd < 0)
1122 return -EIO;
1123 set_sparse(fd);
1124 ftruncate(fd, total_size * 512);
1125 close(fd);
1126 return 0;
1129 void qemu_aio_init(void)
1133 void qemu_aio_poll(void)
1137 void qemu_aio_wait_start(void)
1141 void qemu_aio_wait(void)
1143 #ifndef QEMU_TOOL
1144 qemu_bh_poll();
1145 #endif
1148 void qemu_aio_wait_end(void)
1152 BlockDriver bdrv_raw = {
1153 "raw",
1154 sizeof(BDRVRawState),
1155 NULL, /* no probe for protocols */
1156 raw_open,
1157 NULL,
1158 NULL,
1159 raw_close,
1160 raw_create,
1161 raw_flush,
1163 #if 0
1164 .bdrv_aio_read = raw_aio_read,
1165 .bdrv_aio_write = raw_aio_write,
1166 .bdrv_aio_cancel = raw_aio_cancel,
1167 .aiocb_size = sizeof(RawAIOCB);
1168 #endif
1169 .protocol_name = "file",
1170 .bdrv_pread = raw_pread,
1171 .bdrv_pwrite = raw_pwrite,
1172 .bdrv_truncate = raw_truncate,
1173 .bdrv_getlength = raw_getlength,
1176 /***********************************************/
1177 /* host device */
1179 static int find_cdrom(char *cdrom_name, int cdrom_name_size)
1181 char drives[256], *pdrv = drives;
1182 UINT type;
1184 memset(drives, 0, sizeof(drives));
1185 GetLogicalDriveStrings(sizeof(drives), drives);
1186 while(pdrv[0] != '\0') {
1187 type = GetDriveType(pdrv);
1188 switch(type) {
1189 case DRIVE_CDROM:
1190 snprintf(cdrom_name, cdrom_name_size, "\\\\.\\%c:", pdrv[0]);
1191 return 0;
1192 break;
1194 pdrv += lstrlen(pdrv) + 1;
1196 return -1;
1199 static int find_device_type(BlockDriverState *bs, const char *filename)
1201 BDRVRawState *s = bs->opaque;
1202 UINT type;
1203 const char *p;
1205 if (strstart(filename, "\\\\.\\", &p) ||
1206 strstart(filename, "//./", &p)) {
1207 snprintf(s->drive_path, sizeof(s->drive_path), "%c:\\", p[0]);
1208 type = GetDriveType(s->drive_path);
1209 if (type == DRIVE_CDROM)
1210 return FTYPE_CD;
1211 else
1212 return FTYPE_FILE;
1213 } else {
1214 return FTYPE_FILE;
1218 static int hdev_open(BlockDriverState *bs, const char *filename, int flags)
1220 BDRVRawState *s = bs->opaque;
1221 int access_flags, create_flags;
1222 DWORD overlapped;
1223 char device_name[64];
1225 if (strstart(filename, "/dev/cdrom", NULL)) {
1226 if (find_cdrom(device_name, sizeof(device_name)) < 0)
1227 return -ENOENT;
1228 filename = device_name;
1229 } else {
1230 /* transform drive letters into device name */
1231 if (((filename[0] >= 'a' && filename[0] <= 'z') ||
1232 (filename[0] >= 'A' && filename[0] <= 'Z')) &&
1233 filename[1] == ':' && filename[2] == '\0') {
1234 snprintf(device_name, sizeof(device_name), "\\\\.\\%c:", filename[0]);
1235 filename = device_name;
1238 s->type = find_device_type(bs, filename);
1240 if ((flags & BDRV_O_ACCESS) == O_RDWR) {
1241 access_flags = GENERIC_READ | GENERIC_WRITE;
1242 } else {
1243 access_flags = GENERIC_READ;
1245 create_flags = OPEN_EXISTING;
1247 #ifdef QEMU_TOOL
1248 overlapped = 0;
1249 #else
1250 overlapped = FILE_FLAG_OVERLAPPED;
1251 #endif
1252 s->hfile = CreateFile(filename, access_flags,
1253 FILE_SHARE_READ, NULL,
1254 create_flags, overlapped, 0);
1255 if (s->hfile == INVALID_HANDLE_VALUE)
1256 return -1;
1257 return 0;
1260 #if 0
1261 /***********************************************/
1262 /* removable device additionnal commands */
1264 static int raw_is_inserted(BlockDriverState *bs)
1266 return 1;
1269 static int raw_media_changed(BlockDriverState *bs)
1271 return -ENOTSUP;
1274 static int raw_eject(BlockDriverState *bs, int eject_flag)
1276 DWORD ret_count;
1278 if (s->type == FTYPE_FILE)
1279 return -ENOTSUP;
1280 if (eject_flag) {
1281 DeviceIoControl(s->hfile, IOCTL_STORAGE_EJECT_MEDIA,
1282 NULL, 0, NULL, 0, &lpBytesReturned, NULL);
1283 } else {
1284 DeviceIoControl(s->hfile, IOCTL_STORAGE_LOAD_MEDIA,
1285 NULL, 0, NULL, 0, &lpBytesReturned, NULL);
1289 static int raw_set_locked(BlockDriverState *bs, int locked)
1291 return -ENOTSUP;
1293 #endif
1295 BlockDriver bdrv_host_device = {
1296 "host_device",
1297 sizeof(BDRVRawState),
1298 NULL, /* no probe for protocols */
1299 hdev_open,
1300 NULL,
1301 NULL,
1302 raw_close,
1303 NULL,
1304 raw_flush,
1306 #if 0
1307 .bdrv_aio_read = raw_aio_read,
1308 .bdrv_aio_write = raw_aio_write,
1309 .bdrv_aio_cancel = raw_aio_cancel,
1310 .aiocb_size = sizeof(RawAIOCB);
1311 #endif
1312 .bdrv_pread = raw_pread,
1313 .bdrv_pwrite = raw_pwrite,
1314 .bdrv_getlength = raw_getlength,
1316 #endif /* _WIN32 */