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 static void get_tmp_filename(char *filename
, int size
)
155 /* XXX: find a better function */
159 static void get_tmp_filename(char *filename
, int size
)
162 /* XXX: race condition possible */
163 pstrcpy(filename
, size
, "/tmp/vl.XXXXXX");
164 fd
= mkstemp(filename
);
169 /* XXX: force raw format if block or character device ? It would
170 simplify the BSD case */
171 static BlockDriver
*find_image_format(const char *filename
)
173 int fd
, ret
, score
, score_max
;
174 BlockDriver
*drv1
, *drv
;
176 size_t bufsize
= 1024;
178 fd
= open(filename
, O_RDONLY
| O_BINARY
| O_LARGEFILE
);
183 #ifdef DIOCGSECTORSIZE
185 unsigned int sectorsize
= 512;
186 if (!ioctl(fd
, DIOCGSECTORSIZE
, §orsize
) &&
187 sectorsize
> bufsize
)
188 bufsize
= sectorsize
;
192 u_int32_t blockSize
= 512;
193 if ( !ioctl( fd
, DKIOCGETBLOCKSIZE
, &blockSize
) && blockSize
> bufsize
) {
197 buf
= qemu_malloc(bufsize
);
200 ret
= read(fd
, buf
, bufsize
);
211 for(drv1
= first_drv
; drv1
!= NULL
; drv1
= drv1
->next
) {
212 score
= drv1
->bdrv_probe(buf
, ret
, filename
);
213 if (score
> score_max
) {
222 int bdrv_open(BlockDriverState
*bs
, const char *filename
, int snapshot
)
225 if ( strncmp( filename
, "/dev/cdrom", 10 ) == 0 ) {
226 kern_return_t kernResult
;
227 io_iterator_t mediaIterator
;
228 char bsdPath
[ MAXPATHLEN
];
231 kernResult
= FindEjectableCDMedia( &mediaIterator
);
232 kernResult
= GetBSDPath( mediaIterator
, bsdPath
, sizeof( bsdPath
) );
234 if ( bsdPath
[ 0 ] != '\0' ) {
235 strcat(bsdPath
,"s0");
236 /* some CDs don't have a partition 0 */
237 fd
= open(bsdPath
, O_RDONLY
| O_BINARY
| O_LARGEFILE
);
239 bsdPath
[strlen(bsdPath
)-1] = '1';
247 IOObjectRelease( mediaIterator
);
250 return bdrv_open2(bs
, filename
, snapshot
, NULL
);
253 int bdrv_open2(BlockDriverState
*bs
, const char *filename
, int snapshot
,
257 char tmp_filename
[1024];
260 bs
->is_temporary
= 0;
264 BlockDriverState
*bs1
;
267 /* if snapshot, we create a temporary backing file and open it
268 instead of opening 'filename' directly */
270 /* if there is a backing file, use it */
275 if (bdrv_open(bs1
, filename
, 0) < 0) {
279 total_size
= bs1
->total_sectors
;
282 get_tmp_filename(tmp_filename
, sizeof(tmp_filename
));
283 /* XXX: use cow for linux as it is more efficient ? */
284 if (bdrv_create(&bdrv_qcow
, tmp_filename
,
285 total_size
, filename
, 0) < 0) {
288 filename
= tmp_filename
;
289 bs
->is_temporary
= 1;
292 pstrcpy(bs
->filename
, sizeof(bs
->filename
), filename
);
294 drv
= find_image_format(filename
);
299 bs
->opaque
= qemu_mallocz(drv
->instance_size
);
300 if (bs
->opaque
== NULL
&& drv
->instance_size
> 0)
303 ret
= drv
->bdrv_open(bs
, filename
);
305 qemu_free(bs
->opaque
);
309 if (bs
->is_temporary
) {
313 if (bs
->backing_file
[0] != '\0' && drv
->bdrv_is_allocated
) {
314 /* if there is a backing file, use it */
315 bs
->backing_hd
= bdrv_new("");
316 if (!bs
->backing_hd
) {
321 if (bdrv_open(bs
->backing_hd
, bs
->backing_file
, 0) < 0)
327 /* call the change callback */
329 bs
->change_cb(bs
->change_opaque
);
334 void bdrv_close(BlockDriverState
*bs
)
338 bdrv_delete(bs
->backing_hd
);
339 bs
->drv
->bdrv_close(bs
);
340 qemu_free(bs
->opaque
);
342 if (bs
->is_temporary
) {
343 unlink(bs
->filename
);
350 /* call the change callback */
352 bs
->change_cb(bs
->change_opaque
);
356 void bdrv_delete(BlockDriverState
*bs
)
358 /* XXX: remove the driver list */
363 /* commit COW file into the raw image */
364 int bdrv_commit(BlockDriverState
*bs
)
368 unsigned char sector
[512];
377 if (!bs
->backing_hd
) {
381 for (i
= 0; i
< bs
->total_sectors
;) {
382 if (bs
->drv
->bdrv_is_allocated(bs
, i
, 65536, &n
)) {
383 for(j
= 0; j
< n
; j
++) {
384 if (bdrv_read(bs
, i
, sector
, 1) != 0) {
388 if (bdrv_write(bs
->backing_hd
, i
, sector
, 1) != 0) {
400 /* return -1 if error */
401 int bdrv_read(BlockDriverState
*bs
, int64_t sector_num
,
402 uint8_t *buf
, int nb_sectors
)
405 BlockDriver
*drv
= bs
->drv
;
410 while (nb_sectors
> 0) {
411 if (sector_num
== 0 && bs
->boot_sector_enabled
) {
412 memcpy(buf
, bs
->boot_sector_data
, 512);
414 } else if (bs
->backing_hd
) {
415 if (drv
->bdrv_is_allocated(bs
, sector_num
, nb_sectors
, &n
)) {
416 ret
= drv
->bdrv_read(bs
, sector_num
, buf
, n
);
420 /* read from the base image */
421 ret
= bdrv_read(bs
->backing_hd
, sector_num
, buf
, n
);
426 ret
= drv
->bdrv_read(bs
, sector_num
, buf
, nb_sectors
);
429 /* no need to loop */
439 /* return -1 if error */
440 int bdrv_write(BlockDriverState
*bs
, int64_t sector_num
,
441 const uint8_t *buf
, int nb_sectors
)
447 if (sector_num
== 0 && bs
->boot_sector_enabled
&& nb_sectors
> 0) {
448 memcpy(bs
->boot_sector_data
, buf
, 512);
450 return bs
->drv
->bdrv_write(bs
, sector_num
, buf
, nb_sectors
);
453 void bdrv_get_geometry(BlockDriverState
*bs
, int64_t *nb_sectors_ptr
)
455 *nb_sectors_ptr
= bs
->total_sectors
;
458 /* force a given boot sector. */
459 void bdrv_set_boot_sector(BlockDriverState
*bs
, const uint8_t *data
, int size
)
461 bs
->boot_sector_enabled
= 1;
464 memcpy(bs
->boot_sector_data
, data
, size
);
465 memset(bs
->boot_sector_data
+ size
, 0, 512 - size
);
468 void bdrv_set_geometry_hint(BlockDriverState
*bs
,
469 int cyls
, int heads
, int secs
)
476 void bdrv_set_type_hint(BlockDriverState
*bs
, int type
)
479 bs
->removable
= ((type
== BDRV_TYPE_CDROM
||
480 type
== BDRV_TYPE_FLOPPY
));
483 void bdrv_set_translation_hint(BlockDriverState
*bs
, int translation
)
485 bs
->translation
= translation
;
488 void bdrv_get_geometry_hint(BlockDriverState
*bs
,
489 int *pcyls
, int *pheads
, int *psecs
)
496 int bdrv_get_type_hint(BlockDriverState
*bs
)
501 int bdrv_get_translation_hint(BlockDriverState
*bs
)
503 return bs
->translation
;
506 int bdrv_is_removable(BlockDriverState
*bs
)
508 return bs
->removable
;
511 int bdrv_is_read_only(BlockDriverState
*bs
)
513 return bs
->read_only
;
516 int bdrv_is_inserted(BlockDriverState
*bs
)
521 int bdrv_is_locked(BlockDriverState
*bs
)
526 void bdrv_set_locked(BlockDriverState
*bs
, int locked
)
531 void bdrv_set_change_cb(BlockDriverState
*bs
,
532 void (*change_cb
)(void *opaque
), void *opaque
)
534 bs
->change_cb
= change_cb
;
535 bs
->change_opaque
= opaque
;
538 int bdrv_is_encrypted(BlockDriverState
*bs
)
540 if (bs
->backing_hd
&& bs
->backing_hd
->encrypted
)
542 return bs
->encrypted
;
545 int bdrv_set_key(BlockDriverState
*bs
, const char *key
)
548 if (bs
->backing_hd
&& bs
->backing_hd
->encrypted
) {
549 ret
= bdrv_set_key(bs
->backing_hd
, key
);
555 if (!bs
->encrypted
|| !bs
->drv
|| !bs
->drv
->bdrv_set_key
)
557 return bs
->drv
->bdrv_set_key(bs
, key
);
560 void bdrv_get_format(BlockDriverState
*bs
, char *buf
, int buf_size
)
562 if (!bs
->inserted
|| !bs
->drv
) {
565 pstrcpy(buf
, buf_size
, bs
->drv
->format_name
);
569 void bdrv_iterate_format(void (*it
)(void *opaque
, const char *name
),
574 for (drv
= first_drv
; drv
!= NULL
; drv
= drv
->next
) {
575 it(opaque
, drv
->format_name
);
579 BlockDriverState
*bdrv_find(const char *name
)
581 BlockDriverState
*bs
;
583 for (bs
= bdrv_first
; bs
!= NULL
; bs
= bs
->next
) {
584 if (!strcmp(name
, bs
->device_name
))
590 void bdrv_iterate(void (*it
)(void *opaque
, const char *name
), void *opaque
)
592 BlockDriverState
*bs
;
594 for (bs
= bdrv_first
; bs
!= NULL
; bs
= bs
->next
) {
595 it(opaque
, bs
->device_name
);
599 const char *bdrv_get_device_name(BlockDriverState
*bs
)
601 return bs
->device_name
;
606 BlockDriverState
*bs
;
608 for (bs
= bdrv_first
; bs
!= NULL
; bs
= bs
->next
) {
609 term_printf("%s:", bs
->device_name
);
610 term_printf(" type=");
615 case BDRV_TYPE_CDROM
:
616 term_printf("cdrom");
618 case BDRV_TYPE_FLOPPY
:
619 term_printf("floppy");
622 term_printf(" removable=%d", bs
->removable
);
624 term_printf(" locked=%d", bs
->locked
);
627 term_printf(" file=%s", bs
->filename
);
628 if (bs
->backing_file
[0] != '\0')
629 term_printf(" backing_file=%s", bs
->backing_file
);
630 term_printf(" ro=%d", bs
->read_only
);
631 term_printf(" drv=%s", bs
->drv
->format_name
);
633 term_printf(" encrypted");
635 term_printf(" [not inserted]");
642 /**************************************************************/
643 /* RAW block driver */
645 typedef struct BDRVRawState
{
649 static int raw_probe(const uint8_t *buf
, int buf_size
, const char *filename
)
651 return 1; /* maybe */
654 static int raw_open(BlockDriverState
*bs
, const char *filename
)
656 BDRVRawState
*s
= bs
->opaque
;
663 fd
= open(filename
, O_RDWR
| O_BINARY
| O_LARGEFILE
);
665 fd
= open(filename
, O_RDONLY
| O_BINARY
| O_LARGEFILE
);
671 if (!fstat(fd
, &sb
) && (S_IFCHR
& sb
.st_mode
)) {
672 #ifdef DIOCGMEDIASIZE
673 if (ioctl(fd
, DIOCGMEDIASIZE
, (off_t
*)&size
))
676 size
= LONG_LONG_MAX
;
678 size
= lseek(fd
, 0LL, SEEK_END
);
683 size
= lseek(fd
, 0, SEEK_END
);
686 /* On Windows hosts it can happen that we're unable to get file size
687 for CD-ROM raw device (it's inherent limitation of the CDFS driver). */
689 size
= LONG_LONG_MAX
;
691 bs
->total_sectors
= size
/ 512;
696 static int raw_read(BlockDriverState
*bs
, int64_t sector_num
,
697 uint8_t *buf
, int nb_sectors
)
699 BDRVRawState
*s
= bs
->opaque
;
702 lseek(s
->fd
, sector_num
* 512, SEEK_SET
);
703 ret
= read(s
->fd
, buf
, nb_sectors
* 512);
704 if (ret
!= nb_sectors
* 512)
709 static int raw_write(BlockDriverState
*bs
, int64_t sector_num
,
710 const uint8_t *buf
, int nb_sectors
)
712 BDRVRawState
*s
= bs
->opaque
;
715 lseek(s
->fd
, sector_num
* 512, SEEK_SET
);
716 ret
= write(s
->fd
, buf
, nb_sectors
* 512);
717 if (ret
!= nb_sectors
* 512)
722 static void raw_close(BlockDriverState
*bs
)
724 BDRVRawState
*s
= bs
->opaque
;
728 static int raw_create(const char *filename
, int64_t total_size
,
729 const char *backing_file
, int flags
)
733 if (flags
|| backing_file
)
736 fd
= open(filename
, O_WRONLY
| O_CREAT
| O_TRUNC
| O_BINARY
| O_LARGEFILE
,
740 ftruncate(fd
, total_size
* 512);
745 BlockDriver bdrv_raw
= {
747 sizeof(BDRVRawState
),
758 bdrv_register(&bdrv_raw
);
760 bdrv_register(&bdrv_cow
);
762 bdrv_register(&bdrv_qcow
);
763 bdrv_register(&bdrv_vmdk
);
764 bdrv_register(&bdrv_cloop
);
765 bdrv_register(&bdrv_dmg
);
766 bdrv_register(&bdrv_bochs
);
767 bdrv_register(&bdrv_vpc
);
768 bdrv_register(&bdrv_vvfat
);