Remove non-portable code from configure.
[qemu/mini2440.git] / block.c
blob6924cee67d65f9c9006871664c232d869d16dadd
1 /*
2 * QEMU System Emulator block driver
3 *
4 * Copyright (c) 2003 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"
27 #ifdef _BSD
28 #include <sys/types.h>
29 #include <sys/stat.h>
30 #include <sys/ioctl.h>
31 #include <sys/queue.h>
32 #include <sys/disk.h>
33 #endif
35 #ifdef CONFIG_COCOA
36 #include <paths.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>
45 #endif
47 static BlockDriverState *bdrv_first;
48 static BlockDriver *first_drv;
50 #ifdef CONFIG_COCOA
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" );
68 } else {
69 CFDictionarySetValue( classesToMatch, CFSTR( kIOMediaEjectableKey ), kCFBooleanTrue );
71 kernResult = IOServiceGetMatchingServices( masterPort, classesToMatch, mediaIterator );
72 if ( KERN_SUCCESS != kernResult )
74 printf( "IOServiceGetMatchingServices returned %d\n", kernResult );
77 return 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;
84 *bsdPath = '\0';
85 nextMedia = IOIteratorNext( mediaIterator );
86 if ( nextMedia )
88 CFTypeRef bsdPathAsCFString;
89 bsdPathAsCFString = IORegistryEntryCreateCFProperty( nextMedia, CFSTR( kIOBSDNameKey ), kCFAllocatorDefault, 0 );
90 if ( bsdPathAsCFString ) {
91 size_t devPathLength;
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 );
103 return kernResult;
106 #endif
108 void bdrv_register(BlockDriver *bdrv)
110 bdrv->next = first_drv;
111 first_drv = bdrv;
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));
120 if(!bs)
121 return NULL;
122 pstrcpy(bs->device_name, sizeof(bs->device_name), device_name);
123 if (device_name[0] != '\0') {
124 /* insert at the end */
125 pbs = &bdrv_first;
126 while (*pbs != NULL)
127 pbs = &(*pbs)->next;
128 *pbs = bs;
130 return bs;
133 BlockDriver *bdrv_find_format(const char *format_name)
135 BlockDriver *drv1;
136 for(drv1 = first_drv; drv1 != NULL; drv1 = drv1->next) {
137 if (!strcmp(drv1->format_name, format_name))
138 return drv1;
140 return NULL;
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)
148 return -ENOTSUP;
149 return drv->bdrv_create(filename, size_in_sectors, backing_file, flags);
152 #ifdef _WIN32
153 void get_tmp_filename(char *filename, int size)
155 char* p = strrchr(filename, '/');
157 if (p == NULL)
158 return;
160 /* XXX: find a better function */
161 tmpnam(p);
162 *p = '/';
164 #else
165 void get_tmp_filename(char *filename, int size)
167 int fd;
168 /* XXX: race condition possible */
169 pstrcpy(filename, size, "/tmp/vl.XXXXXX");
170 fd = mkstemp(filename);
171 close(fd);
173 #endif
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;
181 uint8_t *buf;
182 size_t bufsize = 1024;
184 fd = open(filename, O_RDONLY | O_BINARY | O_LARGEFILE);
185 if (fd < 0) {
186 buf = NULL;
187 ret = 0;
188 } else {
189 #ifdef DIOCGSECTORSIZE
191 unsigned int sectorsize = 512;
192 if (!ioctl(fd, DIOCGSECTORSIZE, &sectorsize) &&
193 sectorsize > bufsize)
194 bufsize = sectorsize;
196 #endif
197 #ifdef CONFIG_COCOA
198 u_int32_t blockSize = 512;
199 if ( !ioctl( fd, DKIOCGETBLOCKSIZE, &blockSize ) && blockSize > bufsize) {
200 bufsize = blockSize;
202 #endif
203 buf = qemu_malloc(bufsize);
204 if (!buf)
205 return NULL;
206 ret = read(fd, buf, bufsize);
207 if (ret < 0) {
208 close(fd);
209 qemu_free(buf);
210 return NULL;
212 close(fd);
215 drv = NULL;
216 score_max = 0;
217 for(drv1 = first_drv; drv1 != NULL; drv1 = drv1->next) {
218 score = drv1->bdrv_probe(buf, ret, filename);
219 if (score > score_max) {
220 score_max = score;
221 drv = drv1;
224 qemu_free(buf);
225 return drv;
228 int bdrv_open(BlockDriverState *bs, const char *filename, int snapshot)
230 #ifdef CONFIG_COCOA
231 if ( strncmp( filename, "/dev/cdrom", 10 ) == 0 ) {
232 kern_return_t kernResult;
233 io_iterator_t mediaIterator;
234 char bsdPath[ MAXPATHLEN ];
235 int fd;
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);
244 if (fd < 0) {
245 bsdPath[strlen(bsdPath)-1] = '1';
246 } else {
247 close(fd);
249 filename = bsdPath;
252 if ( mediaIterator )
253 IOObjectRelease( mediaIterator );
255 #endif
256 return bdrv_open2(bs, filename, snapshot, NULL);
259 int bdrv_open2(BlockDriverState *bs, const char *filename, int snapshot,
260 BlockDriver *drv)
262 int ret;
263 char tmp_filename[1024];
265 bs->read_only = 0;
266 bs->is_temporary = 0;
267 bs->encrypted = 0;
269 if (snapshot) {
270 BlockDriverState *bs1;
271 int64_t total_size;
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 */
277 bs1 = bdrv_new("");
278 if (!bs1) {
279 return -1;
281 if (bdrv_open(bs1, filename, 0) < 0) {
282 bdrv_delete(bs1);
283 return -1;
285 total_size = bs1->total_sectors;
286 bdrv_delete(bs1);
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) {
292 return -1;
294 filename = tmp_filename;
295 bs->is_temporary = 1;
298 pstrcpy(bs->filename, sizeof(bs->filename), filename);
299 if (!drv) {
300 drv = find_image_format(filename);
301 if (!drv)
302 return -1;
304 bs->drv = drv;
305 bs->opaque = qemu_mallocz(drv->instance_size);
306 if (bs->opaque == NULL && drv->instance_size > 0)
307 return -1;
309 ret = drv->bdrv_open(bs, filename);
310 if (ret < 0) {
311 qemu_free(bs->opaque);
312 return -1;
314 #ifndef _WIN32
315 if (bs->is_temporary) {
316 unlink(filename);
318 #endif
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) {
323 fail:
324 bdrv_close(bs);
325 return -1;
327 if (bdrv_open(bs->backing_hd, bs->backing_file, 0) < 0)
328 goto fail;
331 bs->inserted = 1;
333 /* call the change callback */
334 if (bs->change_cb)
335 bs->change_cb(bs->change_opaque);
337 return 0;
340 void bdrv_close(BlockDriverState *bs)
342 if (bs->inserted) {
343 if (bs->backing_hd)
344 bdrv_delete(bs->backing_hd);
345 bs->drv->bdrv_close(bs);
346 qemu_free(bs->opaque);
347 #ifdef _WIN32
348 if (bs->is_temporary) {
349 unlink(bs->filename);
351 #endif
352 bs->opaque = NULL;
353 bs->drv = NULL;
354 bs->inserted = 0;
356 /* call the change callback */
357 if (bs->change_cb)
358 bs->change_cb(bs->change_opaque);
362 void bdrv_delete(BlockDriverState *bs)
364 /* XXX: remove the driver list */
365 bdrv_close(bs);
366 qemu_free(bs);
369 /* commit COW file into the raw image */
370 int bdrv_commit(BlockDriverState *bs)
372 int64_t i;
373 int n, j;
374 unsigned char sector[512];
376 if (!bs->inserted)
377 return -ENOENT;
379 if (bs->read_only) {
380 return -EACCES;
383 if (!bs->backing_hd) {
384 return -ENOTSUP;
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) {
391 return -EIO;
394 if (bdrv_write(bs->backing_hd, i, sector, 1) != 0) {
395 return -EIO;
397 i++;
399 } else {
400 i += n;
404 if (bs->drv->bdrv_make_empty)
405 return bs->drv->bdrv_make_empty(bs);
407 return 0;
410 /* return -1 if error */
411 int bdrv_read(BlockDriverState *bs, int64_t sector_num,
412 uint8_t *buf, int nb_sectors)
414 int ret, n;
415 BlockDriver *drv = bs->drv;
417 if (!bs->inserted)
418 return -1;
420 while (nb_sectors > 0) {
421 if (sector_num == 0 && bs->boot_sector_enabled) {
422 memcpy(buf, bs->boot_sector_data, 512);
423 n = 1;
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);
427 if (ret < 0)
428 return -1;
429 } else {
430 /* read from the base image */
431 ret = bdrv_read(bs->backing_hd, sector_num, buf, n);
432 if (ret < 0)
433 return -1;
435 } else {
436 ret = drv->bdrv_read(bs, sector_num, buf, nb_sectors);
437 if (ret < 0)
438 return -1;
439 /* no need to loop */
440 break;
442 nb_sectors -= n;
443 sector_num += n;
444 buf += n * 512;
446 return 0;
449 /* return -1 if error */
450 int bdrv_write(BlockDriverState *bs, int64_t sector_num,
451 const uint8_t *buf, int nb_sectors)
453 if (!bs->inserted)
454 return -1;
455 if (bs->read_only)
456 return -1;
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;
472 if (size > 512)
473 size = 512;
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)
481 bs->cyls = cyls;
482 bs->heads = heads;
483 bs->secs = secs;
486 void bdrv_set_type_hint(BlockDriverState *bs, int type)
488 bs->type = 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)
501 *pcyls = bs->cyls;
502 *pheads = bs->heads;
503 *psecs = bs->secs;
506 int bdrv_get_type_hint(BlockDriverState *bs)
508 return bs->type;
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)
528 return bs->inserted;
531 int bdrv_is_locked(BlockDriverState *bs)
533 return bs->locked;
536 void bdrv_set_locked(BlockDriverState *bs, int locked)
538 bs->locked = 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)
551 return 1;
552 return bs->encrypted;
555 int bdrv_set_key(BlockDriverState *bs, const char *key)
557 int ret;
558 if (bs->backing_hd && bs->backing_hd->encrypted) {
559 ret = bdrv_set_key(bs->backing_hd, key);
560 if (ret < 0)
561 return ret;
562 if (!bs->encrypted)
563 return 0;
565 if (!bs->encrypted || !bs->drv || !bs->drv->bdrv_set_key)
566 return -1;
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) {
573 buf[0] = '\0';
574 } else {
575 pstrcpy(buf, buf_size, bs->drv->format_name);
579 void bdrv_iterate_format(void (*it)(void *opaque, const char *name),
580 void *opaque)
582 BlockDriver *drv;
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))
595 return bs;
597 return NULL;
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;
614 void bdrv_info(void)
616 BlockDriverState *bs;
618 for (bs = bdrv_first; bs != NULL; bs = bs->next) {
619 term_printf("%s:", bs->device_name);
620 term_printf(" type=");
621 switch(bs->type) {
622 case BDRV_TYPE_HD:
623 term_printf("hd");
624 break;
625 case BDRV_TYPE_CDROM:
626 term_printf("cdrom");
627 break;
628 case BDRV_TYPE_FLOPPY:
629 term_printf("floppy");
630 break;
632 term_printf(" removable=%d", bs->removable);
633 if (bs->removable) {
634 term_printf(" locked=%d", bs->locked);
636 if (bs->inserted) {
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);
642 if (bs->encrypted)
643 term_printf(" encrypted");
644 } else {
645 term_printf(" [not inserted]");
647 term_printf("\n");
652 /**************************************************************/
653 /* RAW block driver */
655 typedef struct BDRVRawState {
656 int fd;
657 } 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;
667 int fd;
668 int64_t size;
669 #ifdef _BSD
670 struct stat sb;
671 #endif
673 fd = open(filename, O_RDWR | O_BINARY | O_LARGEFILE);
674 if (fd < 0) {
675 fd = open(filename, O_RDONLY | O_BINARY | O_LARGEFILE);
676 if (fd < 0)
677 return -1;
678 bs->read_only = 1;
680 #ifdef _BSD
681 if (!fstat(fd, &sb) && (S_IFCHR & sb.st_mode)) {
682 #ifdef DIOCGMEDIASIZE
683 if (ioctl(fd, DIOCGMEDIASIZE, (off_t *)&size))
684 #endif
685 #ifdef CONFIG_COCOA
686 size = LONG_LONG_MAX;
687 #else
688 size = lseek(fd, 0LL, SEEK_END);
689 #endif
690 } else
691 #endif
693 size = lseek(fd, 0, SEEK_END);
695 #ifdef _WIN32
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). */
698 if (size == -1)
699 size = LONG_LONG_MAX;
700 #endif
701 bs->total_sectors = size / 512;
702 s->fd = fd;
703 return 0;
706 static int raw_read(BlockDriverState *bs, int64_t sector_num,
707 uint8_t *buf, int nb_sectors)
709 BDRVRawState *s = bs->opaque;
710 int ret;
712 lseek(s->fd, sector_num * 512, SEEK_SET);
713 ret = read(s->fd, buf, nb_sectors * 512);
714 if (ret != nb_sectors * 512)
715 return -1;
716 return 0;
719 static int raw_write(BlockDriverState *bs, int64_t sector_num,
720 const uint8_t *buf, int nb_sectors)
722 BDRVRawState *s = bs->opaque;
723 int ret;
725 lseek(s->fd, sector_num * 512, SEEK_SET);
726 ret = write(s->fd, buf, nb_sectors * 512);
727 if (ret != nb_sectors * 512)
728 return -1;
729 return 0;
732 static void raw_close(BlockDriverState *bs)
734 BDRVRawState *s = bs->opaque;
735 close(s->fd);
738 static int raw_create(const char *filename, int64_t total_size,
739 const char *backing_file, int flags)
741 int fd;
743 if (flags || backing_file)
744 return -ENOTSUP;
746 fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY | O_LARGEFILE,
747 0644);
748 if (fd < 0)
749 return -EIO;
750 ftruncate(fd, total_size * 512);
751 close(fd);
752 return 0;
755 BlockDriver bdrv_raw = {
756 "raw",
757 sizeof(BDRVRawState),
758 raw_probe,
759 raw_open,
760 raw_read,
761 raw_write,
762 raw_close,
763 raw_create,
766 void bdrv_init(void)
768 bdrv_register(&bdrv_raw);
769 #ifndef _WIN32
770 bdrv_register(&bdrv_cow);
771 #endif
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);