1019d6b8fSAnthony Liguori /* 2019d6b8fSAnthony Liguori * Block driver for the various disk image formats used by Bochs 3019d6b8fSAnthony Liguori * Currently only for "growing" type in read-only mode 4019d6b8fSAnthony Liguori * 5019d6b8fSAnthony Liguori * Copyright (c) 2005 Alex Beregszaszi 6019d6b8fSAnthony Liguori * 7019d6b8fSAnthony Liguori * Permission is hereby granted, free of charge, to any person obtaining a copy 8019d6b8fSAnthony Liguori * of this software and associated documentation files (the "Software"), to deal 9019d6b8fSAnthony Liguori * in the Software without restriction, including without limitation the rights 10019d6b8fSAnthony Liguori * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 11019d6b8fSAnthony Liguori * copies of the Software, and to permit persons to whom the Software is 12019d6b8fSAnthony Liguori * furnished to do so, subject to the following conditions: 13019d6b8fSAnthony Liguori * 14019d6b8fSAnthony Liguori * The above copyright notice and this permission notice shall be included in 15019d6b8fSAnthony Liguori * all copies or substantial portions of the Software. 16019d6b8fSAnthony Liguori * 17019d6b8fSAnthony Liguori * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18019d6b8fSAnthony Liguori * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19019d6b8fSAnthony Liguori * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 20019d6b8fSAnthony Liguori * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21019d6b8fSAnthony Liguori * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 22019d6b8fSAnthony Liguori * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 23019d6b8fSAnthony Liguori * THE SOFTWARE. 24019d6b8fSAnthony Liguori */ 2580c71a24SPeter Maydell #include "qemu/osdep.h" 26da34e65cSMarkus Armbruster #include "qapi/error.h" 27e2c1c34fSMarkus Armbruster #include "block/block-io.h" 28737e150eSPaolo Bonzini #include "block/block_int.h" 291de7afc9SPaolo Bonzini #include "qemu/module.h" 3058369e22SPaolo Bonzini #include "qemu/bswap.h" 31398e6ad0SKevin Wolf #include "qemu/error-report.h" 32019d6b8fSAnthony Liguori 33019d6b8fSAnthony Liguori /**************************************************************/ 34019d6b8fSAnthony Liguori 35019d6b8fSAnthony Liguori #define HEADER_MAGIC "Bochs Virtual HD Image" 36019d6b8fSAnthony Liguori #define HEADER_VERSION 0x00020000 37019d6b8fSAnthony Liguori #define HEADER_V1 0x00010000 38019d6b8fSAnthony Liguori #define HEADER_SIZE 512 39019d6b8fSAnthony Liguori 40019d6b8fSAnthony Liguori #define REDOLOG_TYPE "Redolog" 41019d6b8fSAnthony Liguori #define GROWING_TYPE "Growing" 42019d6b8fSAnthony Liguori 43019d6b8fSAnthony Liguori // not allocated: 0xffffffff 44019d6b8fSAnthony Liguori 45019d6b8fSAnthony Liguori // always little-endian 46019d6b8fSAnthony Liguori struct bochs_header { 473dd8a676SKevin Wolf char magic[32]; /* "Bochs Virtual HD Image" */ 483dd8a676SKevin Wolf char type[16]; /* "Redolog" */ 493dd8a676SKevin Wolf char subtype[16]; /* "Undoable" / "Volatile" / "Growing" */ 50019d6b8fSAnthony Liguori uint32_t version; 513dd8a676SKevin Wolf uint32_t header; /* size of header */ 523dd8a676SKevin Wolf 533dd8a676SKevin Wolf uint32_t catalog; /* num of entries */ 543dd8a676SKevin Wolf uint32_t bitmap; /* bitmap size */ 553dd8a676SKevin Wolf uint32_t extent; /* extent size */ 56019d6b8fSAnthony Liguori 57019d6b8fSAnthony Liguori union { 58019d6b8fSAnthony Liguori struct { 593dd8a676SKevin Wolf uint32_t reserved; /* for ??? */ 603dd8a676SKevin Wolf uint64_t disk; /* disk size */ 613dd8a676SKevin Wolf char padding[HEADER_SIZE - 64 - 20 - 12]; 623dd8a676SKevin Wolf } QEMU_PACKED redolog; 633dd8a676SKevin Wolf struct { 643dd8a676SKevin Wolf uint64_t disk; /* disk size */ 653dd8a676SKevin Wolf char padding[HEADER_SIZE - 64 - 20 - 8]; 663dd8a676SKevin Wolf } QEMU_PACKED redolog_v1; 673dd8a676SKevin Wolf char padding[HEADER_SIZE - 64 - 20]; 68019d6b8fSAnthony Liguori } extra; 693dd8a676SKevin Wolf } QEMU_PACKED; 70019d6b8fSAnthony Liguori 71019d6b8fSAnthony Liguori typedef struct BDRVBochsState { 72848c66e8SPaolo Bonzini CoMutex lock; 73019d6b8fSAnthony Liguori uint32_t *catalog_bitmap; 74246f6583SKevin Wolf uint32_t catalog_size; 75019d6b8fSAnthony Liguori 76246f6583SKevin Wolf uint32_t data_offset; 77019d6b8fSAnthony Liguori 78246f6583SKevin Wolf uint32_t bitmap_blocks; 79246f6583SKevin Wolf uint32_t extent_blocks; 80246f6583SKevin Wolf uint32_t extent_size; 81019d6b8fSAnthony Liguori } BDRVBochsState; 82019d6b8fSAnthony Liguori 83019d6b8fSAnthony Liguori static int bochs_probe(const uint8_t *buf, int buf_size, const char *filename) 84019d6b8fSAnthony Liguori { 85019d6b8fSAnthony Liguori const struct bochs_header *bochs = (const void *)buf; 86019d6b8fSAnthony Liguori 87019d6b8fSAnthony Liguori if (buf_size < HEADER_SIZE) 88019d6b8fSAnthony Liguori return 0; 89019d6b8fSAnthony Liguori 90019d6b8fSAnthony Liguori if (!strcmp(bochs->magic, HEADER_MAGIC) && 91019d6b8fSAnthony Liguori !strcmp(bochs->type, REDOLOG_TYPE) && 92019d6b8fSAnthony Liguori !strcmp(bochs->subtype, GROWING_TYPE) && 93019d6b8fSAnthony Liguori ((le32_to_cpu(bochs->version) == HEADER_VERSION) || 94019d6b8fSAnthony Liguori (le32_to_cpu(bochs->version) == HEADER_V1))) 95019d6b8fSAnthony Liguori return 100; 96019d6b8fSAnthony Liguori 97019d6b8fSAnthony Liguori return 0; 98019d6b8fSAnthony Liguori } 99019d6b8fSAnthony Liguori 100015a1036SMax Reitz static int bochs_open(BlockDriverState *bs, QDict *options, int flags, 101015a1036SMax Reitz Error **errp) 102019d6b8fSAnthony Liguori { 103019d6b8fSAnthony Liguori BDRVBochsState *s = bs->opaque; 104246f6583SKevin Wolf uint32_t i; 105019d6b8fSAnthony Liguori struct bochs_header bochs; 1065b7d7dfdSKevin Wolf int ret; 107019d6b8fSAnthony Liguori 108eaa2410fSKevin Wolf /* No write support yet */ 109eaa2410fSKevin Wolf ret = bdrv_apply_auto_read_only(bs, NULL, errp); 110eaa2410fSKevin Wolf if (ret < 0) { 111eaa2410fSKevin Wolf return ret; 112eaa2410fSKevin Wolf } 113eaa2410fSKevin Wolf 11483930780SVladimir Sementsov-Ogievskiy ret = bdrv_open_file_child(NULL, options, "file", bs, errp); 11583930780SVladimir Sementsov-Ogievskiy if (ret < 0) { 11683930780SVladimir Sementsov-Ogievskiy return ret; 1174e4bf5c4SKevin Wolf } 1184e4bf5c4SKevin Wolf 11932cc71deSAlberto Faria ret = bdrv_pread(bs->file, 0, sizeof(bochs), &bochs, 0); 1205b7d7dfdSKevin Wolf if (ret < 0) { 1215b7d7dfdSKevin Wolf return ret; 122019d6b8fSAnthony Liguori } 123019d6b8fSAnthony Liguori 124019d6b8fSAnthony Liguori if (strcmp(bochs.magic, HEADER_MAGIC) || 125019d6b8fSAnthony Liguori strcmp(bochs.type, REDOLOG_TYPE) || 126019d6b8fSAnthony Liguori strcmp(bochs.subtype, GROWING_TYPE) || 127019d6b8fSAnthony Liguori ((le32_to_cpu(bochs.version) != HEADER_VERSION) && 128019d6b8fSAnthony Liguori (le32_to_cpu(bochs.version) != HEADER_V1))) { 12976abe407SPaolo Bonzini error_setg(errp, "Image not in Bochs format"); 13076abe407SPaolo Bonzini return -EINVAL; 131019d6b8fSAnthony Liguori } 132019d6b8fSAnthony Liguori 133019d6b8fSAnthony Liguori if (le32_to_cpu(bochs.version) == HEADER_V1) { 1343dd8a676SKevin Wolf bs->total_sectors = le64_to_cpu(bochs.extra.redolog_v1.disk) / 512; 135019d6b8fSAnthony Liguori } else { 136019d6b8fSAnthony Liguori bs->total_sectors = le64_to_cpu(bochs.extra.redolog.disk) / 512; 137019d6b8fSAnthony Liguori } 138019d6b8fSAnthony Liguori 139e3737b82SKevin Wolf /* Limit to 1M entries to avoid unbounded allocation. This is what is 140e3737b82SKevin Wolf * needed for the largest image that bximage can create (~8 TB). */ 1413dd8a676SKevin Wolf s->catalog_size = le32_to_cpu(bochs.catalog); 142e3737b82SKevin Wolf if (s->catalog_size > 0x100000) { 143e3737b82SKevin Wolf error_setg(errp, "Catalog size is too large"); 144e3737b82SKevin Wolf return -EFBIG; 145e3737b82SKevin Wolf } 146e3737b82SKevin Wolf 14702c4f26bSMarkus Armbruster s->catalog_bitmap = g_try_new(uint32_t, s->catalog_size); 1487bf665eeSKevin Wolf if (s->catalog_size && s->catalog_bitmap == NULL) { 1497bf665eeSKevin Wolf error_setg(errp, "Could not allocate memory for catalog"); 1507bf665eeSKevin Wolf return -ENOMEM; 1517bf665eeSKevin Wolf } 1525b7d7dfdSKevin Wolf 15332cc71deSAlberto Faria ret = bdrv_pread(bs->file, le32_to_cpu(bochs.header), s->catalog_size * 4, 15432cc71deSAlberto Faria s->catalog_bitmap, 0); 1555b7d7dfdSKevin Wolf if (ret < 0) { 156019d6b8fSAnthony Liguori goto fail; 1575b7d7dfdSKevin Wolf } 1585b7d7dfdSKevin Wolf 159019d6b8fSAnthony Liguori for (i = 0; i < s->catalog_size; i++) 160019d6b8fSAnthony Liguori le32_to_cpus(&s->catalog_bitmap[i]); 161019d6b8fSAnthony Liguori 162019d6b8fSAnthony Liguori s->data_offset = le32_to_cpu(bochs.header) + (s->catalog_size * 4); 163019d6b8fSAnthony Liguori 1643dd8a676SKevin Wolf s->bitmap_blocks = 1 + (le32_to_cpu(bochs.bitmap) - 1) / 512; 1653dd8a676SKevin Wolf s->extent_blocks = 1 + (le32_to_cpu(bochs.extent) - 1) / 512; 166019d6b8fSAnthony Liguori 1673dd8a676SKevin Wolf s->extent_size = le32_to_cpu(bochs.extent); 168715c3f60SKevin Wolf if (s->extent_size < BDRV_SECTOR_SIZE) { 169715c3f60SKevin Wolf /* bximage actually never creates extents smaller than 4k */ 170715c3f60SKevin Wolf error_setg(errp, "Extent size must be at least 512"); 171715c3f60SKevin Wolf ret = -EINVAL; 172715c3f60SKevin Wolf goto fail; 173715c3f60SKevin Wolf } else if (!is_power_of_2(s->extent_size)) { 174715c3f60SKevin Wolf error_setg(errp, "Extent size %" PRIu32 " is not a power of two", 175715c3f60SKevin Wolf s->extent_size); 17628ec11bcSKevin Wolf ret = -EINVAL; 17728ec11bcSKevin Wolf goto fail; 1788e53abbcSKevin Wolf } else if (s->extent_size > 0x800000) { 1798e53abbcSKevin Wolf error_setg(errp, "Extent size %" PRIu32 " is too large", 1808e53abbcSKevin Wolf s->extent_size); 18128ec11bcSKevin Wolf ret = -EINVAL; 18228ec11bcSKevin Wolf goto fail; 1838e53abbcSKevin Wolf } 184019d6b8fSAnthony Liguori 185715c3f60SKevin Wolf if (s->catalog_size < DIV_ROUND_UP(bs->total_sectors, 186715c3f60SKevin Wolf s->extent_size / BDRV_SECTOR_SIZE)) 187715c3f60SKevin Wolf { 188e3737b82SKevin Wolf error_setg(errp, "Catalog size is too small for this disk size"); 189e3737b82SKevin Wolf ret = -EINVAL; 190e3737b82SKevin Wolf goto fail; 191e3737b82SKevin Wolf } 192e3737b82SKevin Wolf 193848c66e8SPaolo Bonzini qemu_co_mutex_init(&s->lock); 194019d6b8fSAnthony Liguori return 0; 1955b7d7dfdSKevin Wolf 196019d6b8fSAnthony Liguori fail: 1975b7d7dfdSKevin Wolf g_free(s->catalog_bitmap); 1985b7d7dfdSKevin Wolf return ret; 199019d6b8fSAnthony Liguori } 200019d6b8fSAnthony Liguori 201a6506481SEric Blake static void bochs_refresh_limits(BlockDriverState *bs, Error **errp) 202a6506481SEric Blake { 203a5b8dd2cSEric Blake bs->bl.request_alignment = BDRV_SECTOR_SIZE; /* No sub-sector I/O */ 204a6506481SEric Blake } 205a6506481SEric Blake 206*e7918e96SPaolo Bonzini static int64_t coroutine_fn GRAPH_RDLOCK 207*e7918e96SPaolo Bonzini seek_to_sector(BlockDriverState *bs, int64_t sector_num) 208019d6b8fSAnthony Liguori { 209019d6b8fSAnthony Liguori BDRVBochsState *s = bs->opaque; 210246f6583SKevin Wolf uint64_t offset = sector_num * 512; 211246f6583SKevin Wolf uint64_t extent_index, extent_offset, bitmap_offset; 212019d6b8fSAnthony Liguori char bitmap_entry; 213e1b42f45SMax Reitz int ret; 214019d6b8fSAnthony Liguori 215019d6b8fSAnthony Liguori // seek to sector 216019d6b8fSAnthony Liguori extent_index = offset / s->extent_size; 217019d6b8fSAnthony Liguori extent_offset = (offset % s->extent_size) / 512; 218019d6b8fSAnthony Liguori 219efbca10fSChristoph Hellwig if (s->catalog_bitmap[extent_index] == 0xffffffff) { 220e1b42f45SMax Reitz return 0; /* not allocated */ 221019d6b8fSAnthony Liguori } 222019d6b8fSAnthony Liguori 223a9ba36a4SKevin Wolf bitmap_offset = s->data_offset + 224a9ba36a4SKevin Wolf (512 * (uint64_t) s->catalog_bitmap[extent_index] * 225019d6b8fSAnthony Liguori (s->extent_blocks + s->bitmap_blocks)); 226019d6b8fSAnthony Liguori 227efbca10fSChristoph Hellwig /* read in bitmap for current extent */ 228*e7918e96SPaolo Bonzini ret = bdrv_co_pread(bs->file, bitmap_offset + (extent_offset / 8), 1, 22932cc71deSAlberto Faria &bitmap_entry, 0); 230e1b42f45SMax Reitz if (ret < 0) { 231e1b42f45SMax Reitz return ret; 232ecbe1576SBlue Swirl } 233019d6b8fSAnthony Liguori 234efbca10fSChristoph Hellwig if (!((bitmap_entry >> (extent_offset % 8)) & 1)) { 235e1b42f45SMax Reitz return 0; /* not allocated */ 236019d6b8fSAnthony Liguori } 237019d6b8fSAnthony Liguori 238efbca10fSChristoph Hellwig return bitmap_offset + (512 * (s->bitmap_blocks + extent_offset)); 239019d6b8fSAnthony Liguori } 240019d6b8fSAnthony Liguori 241b9b10c35SKevin Wolf static int coroutine_fn GRAPH_RDLOCK 242f7ef38ddSVladimir Sementsov-Ogievskiy bochs_co_preadv(BlockDriverState *bs, int64_t offset, int64_t bytes, 243f7ef38ddSVladimir Sementsov-Ogievskiy QEMUIOVector *qiov, BdrvRequestFlags flags) 244019d6b8fSAnthony Liguori { 2453b8fd330SKevin Wolf BDRVBochsState *s = bs->opaque; 2463b8fd330SKevin Wolf uint64_t sector_num = offset >> BDRV_SECTOR_BITS; 2473b8fd330SKevin Wolf int nb_sectors = bytes >> BDRV_SECTOR_BITS; 2483b8fd330SKevin Wolf uint64_t bytes_done = 0; 2493b8fd330SKevin Wolf QEMUIOVector local_qiov; 250019d6b8fSAnthony Liguori int ret; 251019d6b8fSAnthony Liguori 2521bbbf32dSNir Soffer assert(QEMU_IS_ALIGNED(offset, BDRV_SECTOR_SIZE)); 2531bbbf32dSNir Soffer assert(QEMU_IS_ALIGNED(bytes, BDRV_SECTOR_SIZE)); 2543b8fd330SKevin Wolf 2553b8fd330SKevin Wolf qemu_iovec_init(&local_qiov, qiov->niov); 2563b8fd330SKevin Wolf qemu_co_mutex_lock(&s->lock); 2573b8fd330SKevin Wolf 258019d6b8fSAnthony Liguori while (nb_sectors > 0) { 259efbca10fSChristoph Hellwig int64_t block_offset = seek_to_sector(bs, sector_num); 260e1b42f45SMax Reitz if (block_offset < 0) { 2613b8fd330SKevin Wolf ret = block_offset; 2623b8fd330SKevin Wolf goto fail; 2633b8fd330SKevin Wolf } 2643b8fd330SKevin Wolf 2653b8fd330SKevin Wolf qemu_iovec_reset(&local_qiov); 2663b8fd330SKevin Wolf qemu_iovec_concat(&local_qiov, qiov, bytes_done, 512); 2673b8fd330SKevin Wolf 2683b8fd330SKevin Wolf if (block_offset > 0) { 269a03ef88fSKevin Wolf ret = bdrv_co_preadv(bs->file, block_offset, 512, 2703b8fd330SKevin Wolf &local_qiov, 0); 271e1b42f45SMax Reitz if (ret < 0) { 2723b8fd330SKevin Wolf goto fail; 273019d6b8fSAnthony Liguori } 274e1b42f45SMax Reitz } else { 2753b8fd330SKevin Wolf qemu_iovec_memset(&local_qiov, 0, 0, 512); 276e1b42f45SMax Reitz } 277019d6b8fSAnthony Liguori nb_sectors--; 278019d6b8fSAnthony Liguori sector_num++; 2793b8fd330SKevin Wolf bytes_done += 512; 280019d6b8fSAnthony Liguori } 281019d6b8fSAnthony Liguori 2823b8fd330SKevin Wolf ret = 0; 2833b8fd330SKevin Wolf fail: 2842914caa0SPaolo Bonzini qemu_co_mutex_unlock(&s->lock); 2853b8fd330SKevin Wolf qemu_iovec_destroy(&local_qiov); 2863b8fd330SKevin Wolf 2872914caa0SPaolo Bonzini return ret; 2882914caa0SPaolo Bonzini } 2892914caa0SPaolo Bonzini 290019d6b8fSAnthony Liguori static void bochs_close(BlockDriverState *bs) 291019d6b8fSAnthony Liguori { 292019d6b8fSAnthony Liguori BDRVBochsState *s = bs->opaque; 2937267c094SAnthony Liguori g_free(s->catalog_bitmap); 294019d6b8fSAnthony Liguori } 295019d6b8fSAnthony Liguori 296019d6b8fSAnthony Liguori static BlockDriver bdrv_bochs = { 297019d6b8fSAnthony Liguori .format_name = "bochs", 298019d6b8fSAnthony Liguori .instance_size = sizeof(BDRVBochsState), 299019d6b8fSAnthony Liguori .bdrv_probe = bochs_probe, 3007a6f3913SChristoph Hellwig .bdrv_open = bochs_open, 30169dca43dSMax Reitz .bdrv_child_perm = bdrv_default_perms, 302a6506481SEric Blake .bdrv_refresh_limits = bochs_refresh_limits, 3033b8fd330SKevin Wolf .bdrv_co_preadv = bochs_co_preadv, 304019d6b8fSAnthony Liguori .bdrv_close = bochs_close, 305d67066d8SMax Reitz .is_format = true, 306019d6b8fSAnthony Liguori }; 307019d6b8fSAnthony Liguori 308019d6b8fSAnthony Liguori static void bdrv_bochs_init(void) 309019d6b8fSAnthony Liguori { 310019d6b8fSAnthony Liguori bdrv_register(&bdrv_bochs); 311019d6b8fSAnthony Liguori } 312019d6b8fSAnthony Liguori 313019d6b8fSAnthony Liguori block_init(bdrv_bochs_init); 314