2 * QEMU System Emulator block driver
4 * Copyright (c) 2003 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"
28 #include <sys/types.h>
30 #include <sys/ioctl.h>
31 #include <sys/queue.h>
37 #include <sys/param.h>
38 #include <IOKit/IOKitLib.h>
39 #include <IOKit/IOBSD.h>
40 #include <IOKit/storage/IOMediaBSDClient.h>
41 #include <IOKit/storage/IOMedia.h>
42 #include <IOKit/storage/IOCDMedia.h>
43 //#include <IOKit/storage/IOCDTypes.h>
44 #include <CoreFoundation/CoreFoundation.h>
47 static BlockDriverState
*bdrv_first
;
48 static BlockDriver
*first_drv
;
51 static kern_return_t
FindEjectableCDMedia( io_iterator_t
*mediaIterator
);
52 static kern_return_t
GetBSDPath( io_iterator_t mediaIterator
, char *bsdPath
, CFIndex maxPathSize
);
54 kern_return_t
FindEjectableCDMedia( io_iterator_t
*mediaIterator
)
56 kern_return_t kernResult
;
57 mach_port_t masterPort
;
58 CFMutableDictionaryRef classesToMatch
;
60 kernResult
= IOMasterPort( MACH_PORT_NULL
, &masterPort
);
61 if ( KERN_SUCCESS
!= kernResult
) {
62 printf( "IOMasterPort returned %d\n", kernResult
);
65 classesToMatch
= IOServiceMatching( kIOCDMediaClass
);
66 if ( classesToMatch
== NULL
) {
67 printf( "IOServiceMatching returned a NULL dictionary.\n" );
69 CFDictionarySetValue( classesToMatch
, CFSTR( kIOMediaEjectableKey
), kCFBooleanTrue
);
71 kernResult
= IOServiceGetMatchingServices( masterPort
, classesToMatch
, mediaIterator
);
72 if ( KERN_SUCCESS
!= kernResult
)
74 printf( "IOServiceGetMatchingServices returned %d\n", kernResult
);
80 kern_return_t
GetBSDPath( io_iterator_t mediaIterator
, char *bsdPath
, CFIndex maxPathSize
)
82 io_object_t nextMedia
;
83 kern_return_t kernResult
= KERN_FAILURE
;
85 nextMedia
= IOIteratorNext( mediaIterator
);
88 CFTypeRef bsdPathAsCFString
;
89 bsdPathAsCFString
= IORegistryEntryCreateCFProperty( nextMedia
, CFSTR( kIOBSDNameKey
), kCFAllocatorDefault
, 0 );
90 if ( bsdPathAsCFString
) {
92 strcpy( bsdPath
, _PATH_DEV
);
93 strcat( bsdPath
, "r" );
94 devPathLength
= strlen( bsdPath
);
95 if ( CFStringGetCString( bsdPathAsCFString
, bsdPath
+ devPathLength
, maxPathSize
- devPathLength
, kCFStringEncodingASCII
) ) {
96 kernResult
= KERN_SUCCESS
;
98 CFRelease( bsdPathAsCFString
);
100 IOObjectRelease( nextMedia
);
108 void bdrv_register(BlockDriver
*bdrv
)
110 bdrv
->next
= first_drv
;
114 /* create a new block device (by default it is empty) */
115 BlockDriverState
*bdrv_new(const char *device_name
)
117 BlockDriverState
**pbs
, *bs
;
119 bs
= qemu_mallocz(sizeof(BlockDriverState
));
122 pstrcpy(bs
->device_name
, sizeof(bs
->device_name
), device_name
);
123 if (device_name
[0] != '\0') {
124 /* insert at the end */
133 BlockDriver
*bdrv_find_format(const char *format_name
)
136 for(drv1
= first_drv
; drv1
!= NULL
; drv1
= drv1
->next
) {
137 if (!strcmp(drv1
->format_name
, format_name
))
143 int bdrv_create(BlockDriver
*drv
,
144 const char *filename
, int64_t size_in_sectors
,
145 const char *backing_file
, int flags
)
147 if (!drv
->bdrv_create
)
149 return drv
->bdrv_create(filename
, size_in_sectors
, backing_file
, flags
);
153 void get_tmp_filename(char *filename
, int size
)
155 char* p
= strrchr(filename
, '/');
160 /* XXX: find a better function */
165 void get_tmp_filename(char *filename
, int size
)
168 /* XXX: race condition possible */
169 pstrcpy(filename
, size
, "/tmp/vl.XXXXXX");
170 fd
= mkstemp(filename
);
175 /* XXX: force raw format if block or character device ? It would
176 simplify the BSD case */
177 static BlockDriver
*find_image_format(const char *filename
)
179 int fd
, ret
, score
, score_max
;
180 BlockDriver
*drv1
, *drv
;
182 size_t bufsize
= 1024;
184 fd
= open(filename
, O_RDONLY
| O_BINARY
| O_LARGEFILE
);
189 #ifdef DIOCGSECTORSIZE
191 unsigned int sectorsize
= 512;
192 if (!ioctl(fd
, DIOCGSECTORSIZE
, §orsize
) &&
193 sectorsize
> bufsize
)
194 bufsize
= sectorsize
;
198 u_int32_t blockSize
= 512;
199 if ( !ioctl( fd
, DKIOCGETBLOCKSIZE
, &blockSize
) && blockSize
> bufsize
) {
203 buf
= qemu_malloc(bufsize
);
206 ret
= read(fd
, buf
, bufsize
);
217 for(drv1
= first_drv
; drv1
!= NULL
; drv1
= drv1
->next
) {
218 score
= drv1
->bdrv_probe(buf
, ret
, filename
);
219 if (score
> score_max
) {
228 int bdrv_open(BlockDriverState
*bs
, const char *filename
, int snapshot
)
231 if ( strncmp( filename
, "/dev/cdrom", 10 ) == 0 ) {
232 kern_return_t kernResult
;
233 io_iterator_t mediaIterator
;
234 char bsdPath
[ MAXPATHLEN
];
237 kernResult
= FindEjectableCDMedia( &mediaIterator
);
238 kernResult
= GetBSDPath( mediaIterator
, bsdPath
, sizeof( bsdPath
) );
240 if ( bsdPath
[ 0 ] != '\0' ) {
241 strcat(bsdPath
,"s0");
242 /* some CDs don't have a partition 0 */
243 fd
= open(bsdPath
, O_RDONLY
| O_BINARY
| O_LARGEFILE
);
245 bsdPath
[strlen(bsdPath
)-1] = '1';
253 IOObjectRelease( mediaIterator
);
256 return bdrv_open2(bs
, filename
, snapshot
, NULL
);
259 int bdrv_open2(BlockDriverState
*bs
, const char *filename
, int snapshot
,
263 char tmp_filename
[1024];
266 bs
->is_temporary
= 0;
270 BlockDriverState
*bs1
;
273 /* if snapshot, we create a temporary backing file and open it
274 instead of opening 'filename' directly */
276 /* if there is a backing file, use it */
281 if (bdrv_open(bs1
, filename
, 0) < 0) {
285 total_size
= bs1
->total_sectors
;
288 get_tmp_filename(tmp_filename
, sizeof(tmp_filename
));
289 /* XXX: use cow for linux as it is more efficient ? */
290 if (bdrv_create(&bdrv_qcow
, tmp_filename
,
291 total_size
, filename
, 0) < 0) {
294 filename
= tmp_filename
;
295 bs
->is_temporary
= 1;
298 pstrcpy(bs
->filename
, sizeof(bs
->filename
), filename
);
300 drv
= find_image_format(filename
);
305 bs
->opaque
= qemu_mallocz(drv
->instance_size
);
306 if (bs
->opaque
== NULL
&& drv
->instance_size
> 0)
309 ret
= drv
->bdrv_open(bs
, filename
);
311 qemu_free(bs
->opaque
);
315 if (bs
->is_temporary
) {
319 if (bs
->backing_file
[0] != '\0' && drv
->bdrv_is_allocated
) {
320 /* if there is a backing file, use it */
321 bs
->backing_hd
= bdrv_new("");
322 if (!bs
->backing_hd
) {
327 if (bdrv_open(bs
->backing_hd
, bs
->backing_file
, 0) < 0)
333 /* call the change callback */
335 bs
->change_cb(bs
->change_opaque
);
340 void bdrv_close(BlockDriverState
*bs
)
344 bdrv_delete(bs
->backing_hd
);
345 bs
->drv
->bdrv_close(bs
);
346 qemu_free(bs
->opaque
);
348 if (bs
->is_temporary
) {
349 unlink(bs
->filename
);
356 /* call the change callback */
358 bs
->change_cb(bs
->change_opaque
);
362 void bdrv_delete(BlockDriverState
*bs
)
364 /* XXX: remove the driver list */
369 /* commit COW file into the raw image */
370 int bdrv_commit(BlockDriverState
*bs
)
374 unsigned char sector
[512];
383 if (!bs
->backing_hd
) {
387 for (i
= 0; i
< bs
->total_sectors
;) {
388 if (bs
->drv
->bdrv_is_allocated(bs
, i
, 65536, &n
)) {
389 for(j
= 0; j
< n
; j
++) {
390 if (bdrv_read(bs
, i
, sector
, 1) != 0) {
394 if (bdrv_write(bs
->backing_hd
, i
, sector
, 1) != 0) {
404 if (bs
->drv
->bdrv_make_empty
)
405 return bs
->drv
->bdrv_make_empty(bs
);
410 /* return -1 if error */
411 int bdrv_read(BlockDriverState
*bs
, int64_t sector_num
,
412 uint8_t *buf
, int nb_sectors
)
415 BlockDriver
*drv
= bs
->drv
;
420 while (nb_sectors
> 0) {
421 if (sector_num
== 0 && bs
->boot_sector_enabled
) {
422 memcpy(buf
, bs
->boot_sector_data
, 512);
424 } else if (bs
->backing_hd
) {
425 if (drv
->bdrv_is_allocated(bs
, sector_num
, nb_sectors
, &n
)) {
426 ret
= drv
->bdrv_read(bs
, sector_num
, buf
, n
);
430 /* read from the base image */
431 ret
= bdrv_read(bs
->backing_hd
, sector_num
, buf
, n
);
436 ret
= drv
->bdrv_read(bs
, sector_num
, buf
, nb_sectors
);
439 /* no need to loop */
449 /* return -1 if error */
450 int bdrv_write(BlockDriverState
*bs
, int64_t sector_num
,
451 const uint8_t *buf
, int nb_sectors
)
457 if (sector_num
== 0 && bs
->boot_sector_enabled
&& nb_sectors
> 0) {
458 memcpy(bs
->boot_sector_data
, buf
, 512);
460 return bs
->drv
->bdrv_write(bs
, sector_num
, buf
, nb_sectors
);
463 void bdrv_get_geometry(BlockDriverState
*bs
, int64_t *nb_sectors_ptr
)
465 *nb_sectors_ptr
= bs
->total_sectors
;
468 /* force a given boot sector. */
469 void bdrv_set_boot_sector(BlockDriverState
*bs
, const uint8_t *data
, int size
)
471 bs
->boot_sector_enabled
= 1;
474 memcpy(bs
->boot_sector_data
, data
, size
);
475 memset(bs
->boot_sector_data
+ size
, 0, 512 - size
);
478 void bdrv_set_geometry_hint(BlockDriverState
*bs
,
479 int cyls
, int heads
, int secs
)
486 void bdrv_set_type_hint(BlockDriverState
*bs
, int type
)
489 bs
->removable
= ((type
== BDRV_TYPE_CDROM
||
490 type
== BDRV_TYPE_FLOPPY
));
493 void bdrv_set_translation_hint(BlockDriverState
*bs
, int translation
)
495 bs
->translation
= translation
;
498 void bdrv_get_geometry_hint(BlockDriverState
*bs
,
499 int *pcyls
, int *pheads
, int *psecs
)
506 int bdrv_get_type_hint(BlockDriverState
*bs
)
511 int bdrv_get_translation_hint(BlockDriverState
*bs
)
513 return bs
->translation
;
516 int bdrv_is_removable(BlockDriverState
*bs
)
518 return bs
->removable
;
521 int bdrv_is_read_only(BlockDriverState
*bs
)
523 return bs
->read_only
;
526 int bdrv_is_inserted(BlockDriverState
*bs
)
531 int bdrv_is_locked(BlockDriverState
*bs
)
536 void bdrv_set_locked(BlockDriverState
*bs
, int locked
)
541 void bdrv_set_change_cb(BlockDriverState
*bs
,
542 void (*change_cb
)(void *opaque
), void *opaque
)
544 bs
->change_cb
= change_cb
;
545 bs
->change_opaque
= opaque
;
548 int bdrv_is_encrypted(BlockDriverState
*bs
)
550 if (bs
->backing_hd
&& bs
->backing_hd
->encrypted
)
552 return bs
->encrypted
;
555 int bdrv_set_key(BlockDriverState
*bs
, const char *key
)
558 if (bs
->backing_hd
&& bs
->backing_hd
->encrypted
) {
559 ret
= bdrv_set_key(bs
->backing_hd
, key
);
565 if (!bs
->encrypted
|| !bs
->drv
|| !bs
->drv
->bdrv_set_key
)
567 return bs
->drv
->bdrv_set_key(bs
, key
);
570 void bdrv_get_format(BlockDriverState
*bs
, char *buf
, int buf_size
)
572 if (!bs
->inserted
|| !bs
->drv
) {
575 pstrcpy(buf
, buf_size
, bs
->drv
->format_name
);
579 void bdrv_iterate_format(void (*it
)(void *opaque
, const char *name
),
584 for (drv
= first_drv
; drv
!= NULL
; drv
= drv
->next
) {
585 it(opaque
, drv
->format_name
);
589 BlockDriverState
*bdrv_find(const char *name
)
591 BlockDriverState
*bs
;
593 for (bs
= bdrv_first
; bs
!= NULL
; bs
= bs
->next
) {
594 if (!strcmp(name
, bs
->device_name
))
600 void bdrv_iterate(void (*it
)(void *opaque
, const char *name
), void *opaque
)
602 BlockDriverState
*bs
;
604 for (bs
= bdrv_first
; bs
!= NULL
; bs
= bs
->next
) {
605 it(opaque
, bs
->device_name
);
609 const char *bdrv_get_device_name(BlockDriverState
*bs
)
611 return bs
->device_name
;
616 BlockDriverState
*bs
;
618 for (bs
= bdrv_first
; bs
!= NULL
; bs
= bs
->next
) {
619 term_printf("%s:", bs
->device_name
);
620 term_printf(" type=");
625 case BDRV_TYPE_CDROM
:
626 term_printf("cdrom");
628 case BDRV_TYPE_FLOPPY
:
629 term_printf("floppy");
632 term_printf(" removable=%d", bs
->removable
);
634 term_printf(" locked=%d", bs
->locked
);
637 term_printf(" file=%s", bs
->filename
);
638 if (bs
->backing_file
[0] != '\0')
639 term_printf(" backing_file=%s", bs
->backing_file
);
640 term_printf(" ro=%d", bs
->read_only
);
641 term_printf(" drv=%s", bs
->drv
->format_name
);
643 term_printf(" encrypted");
645 term_printf(" [not inserted]");
652 /**************************************************************/
653 /* RAW block driver */
655 typedef struct BDRVRawState
{
659 static int raw_probe(const uint8_t *buf
, int buf_size
, const char *filename
)
661 return 1; /* maybe */
664 static int raw_open(BlockDriverState
*bs
, const char *filename
)
666 BDRVRawState
*s
= bs
->opaque
;
673 fd
= open(filename
, O_RDWR
| O_BINARY
| O_LARGEFILE
);
675 fd
= open(filename
, O_RDONLY
| O_BINARY
| O_LARGEFILE
);
681 if (!fstat(fd
, &sb
) && (S_IFCHR
& sb
.st_mode
)) {
682 #ifdef DIOCGMEDIASIZE
683 if (ioctl(fd
, DIOCGMEDIASIZE
, (off_t
*)&size
))
686 size
= LONG_LONG_MAX
;
688 size
= lseek(fd
, 0LL, SEEK_END
);
693 size
= lseek(fd
, 0, SEEK_END
);
696 /* On Windows hosts it can happen that we're unable to get file size
697 for CD-ROM raw device (it's inherent limitation of the CDFS driver). */
699 size
= LONG_LONG_MAX
;
701 bs
->total_sectors
= size
/ 512;
706 static int raw_read(BlockDriverState
*bs
, int64_t sector_num
,
707 uint8_t *buf
, int nb_sectors
)
709 BDRVRawState
*s
= bs
->opaque
;
712 lseek(s
->fd
, sector_num
* 512, SEEK_SET
);
713 ret
= read(s
->fd
, buf
, nb_sectors
* 512);
714 if (ret
!= nb_sectors
* 512)
719 static int raw_write(BlockDriverState
*bs
, int64_t sector_num
,
720 const uint8_t *buf
, int nb_sectors
)
722 BDRVRawState
*s
= bs
->opaque
;
725 lseek(s
->fd
, sector_num
* 512, SEEK_SET
);
726 ret
= write(s
->fd
, buf
, nb_sectors
* 512);
727 if (ret
!= nb_sectors
* 512)
732 static void raw_close(BlockDriverState
*bs
)
734 BDRVRawState
*s
= bs
->opaque
;
738 static int raw_create(const char *filename
, int64_t total_size
,
739 const char *backing_file
, int flags
)
743 if (flags
|| backing_file
)
746 fd
= open(filename
, O_WRONLY
| O_CREAT
| O_TRUNC
| O_BINARY
| O_LARGEFILE
,
750 ftruncate(fd
, total_size
* 512);
755 BlockDriver bdrv_raw
= {
757 sizeof(BDRVRawState
),
768 bdrv_register(&bdrv_raw
);
770 bdrv_register(&bdrv_cow
);
772 bdrv_register(&bdrv_qcow
);
773 bdrv_register(&bdrv_vmdk
);
774 bdrv_register(&bdrv_cloop
);
775 bdrv_register(&bdrv_dmg
);
776 bdrv_register(&bdrv_bochs
);
777 bdrv_register(&bdrv_vpc
);
778 bdrv_register(&bdrv_vvfat
);