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 */ 25019d6b8fSAnthony Liguori #include "qemu-common.h" 26019d6b8fSAnthony Liguori #include "block_int.h" 27019d6b8fSAnthony Liguori #include "module.h" 28019d6b8fSAnthony Liguori 29019d6b8fSAnthony Liguori /**************************************************************/ 30019d6b8fSAnthony Liguori 31019d6b8fSAnthony Liguori #define HEADER_MAGIC "Bochs Virtual HD Image" 32019d6b8fSAnthony Liguori #define HEADER_VERSION 0x00020000 33019d6b8fSAnthony Liguori #define HEADER_V1 0x00010000 34019d6b8fSAnthony Liguori #define HEADER_SIZE 512 35019d6b8fSAnthony Liguori 36019d6b8fSAnthony Liguori #define REDOLOG_TYPE "Redolog" 37019d6b8fSAnthony Liguori #define GROWING_TYPE "Growing" 38019d6b8fSAnthony Liguori 39019d6b8fSAnthony Liguori // not allocated: 0xffffffff 40019d6b8fSAnthony Liguori 41019d6b8fSAnthony Liguori // always little-endian 42019d6b8fSAnthony Liguori struct bochs_header_v1 { 43019d6b8fSAnthony Liguori char magic[32]; // "Bochs Virtual HD Image" 44019d6b8fSAnthony Liguori char type[16]; // "Redolog" 45019d6b8fSAnthony Liguori char subtype[16]; // "Undoable" / "Volatile" / "Growing" 46019d6b8fSAnthony Liguori uint32_t version; 47019d6b8fSAnthony Liguori uint32_t header; // size of header 48019d6b8fSAnthony Liguori 49019d6b8fSAnthony Liguori union { 50019d6b8fSAnthony Liguori struct { 51019d6b8fSAnthony Liguori uint32_t catalog; // num of entries 52019d6b8fSAnthony Liguori uint32_t bitmap; // bitmap size 53019d6b8fSAnthony Liguori uint32_t extent; // extent size 54019d6b8fSAnthony Liguori uint64_t disk; // disk size 55019d6b8fSAnthony Liguori char padding[HEADER_SIZE - 64 - 8 - 20]; 56019d6b8fSAnthony Liguori } redolog; 57019d6b8fSAnthony Liguori char padding[HEADER_SIZE - 64 - 8]; 58019d6b8fSAnthony Liguori } extra; 59019d6b8fSAnthony Liguori }; 60019d6b8fSAnthony Liguori 61019d6b8fSAnthony Liguori // always little-endian 62019d6b8fSAnthony Liguori struct bochs_header { 63019d6b8fSAnthony Liguori char magic[32]; // "Bochs Virtual HD Image" 64019d6b8fSAnthony Liguori char type[16]; // "Redolog" 65019d6b8fSAnthony Liguori char subtype[16]; // "Undoable" / "Volatile" / "Growing" 66019d6b8fSAnthony Liguori uint32_t version; 67019d6b8fSAnthony Liguori uint32_t header; // size of header 68019d6b8fSAnthony Liguori 69019d6b8fSAnthony Liguori union { 70019d6b8fSAnthony Liguori struct { 71019d6b8fSAnthony Liguori uint32_t catalog; // num of entries 72019d6b8fSAnthony Liguori uint32_t bitmap; // bitmap size 73019d6b8fSAnthony Liguori uint32_t extent; // extent size 74019d6b8fSAnthony Liguori uint32_t reserved; // for ??? 75019d6b8fSAnthony Liguori uint64_t disk; // disk size 76019d6b8fSAnthony Liguori char padding[HEADER_SIZE - 64 - 8 - 24]; 77019d6b8fSAnthony Liguori } redolog; 78019d6b8fSAnthony Liguori char padding[HEADER_SIZE - 64 - 8]; 79019d6b8fSAnthony Liguori } extra; 80019d6b8fSAnthony Liguori }; 81019d6b8fSAnthony Liguori 82019d6b8fSAnthony Liguori typedef struct BDRVBochsState { 83848c66e8SPaolo Bonzini CoMutex lock; 84019d6b8fSAnthony Liguori uint32_t *catalog_bitmap; 85019d6b8fSAnthony Liguori int catalog_size; 86019d6b8fSAnthony Liguori 87019d6b8fSAnthony Liguori int data_offset; 88019d6b8fSAnthony Liguori 89019d6b8fSAnthony Liguori int bitmap_blocks; 90019d6b8fSAnthony Liguori int extent_blocks; 91019d6b8fSAnthony Liguori int extent_size; 92019d6b8fSAnthony Liguori } BDRVBochsState; 93019d6b8fSAnthony Liguori 94019d6b8fSAnthony Liguori static int bochs_probe(const uint8_t *buf, int buf_size, const char *filename) 95019d6b8fSAnthony Liguori { 96019d6b8fSAnthony Liguori const struct bochs_header *bochs = (const void *)buf; 97019d6b8fSAnthony Liguori 98019d6b8fSAnthony Liguori if (buf_size < HEADER_SIZE) 99019d6b8fSAnthony Liguori return 0; 100019d6b8fSAnthony Liguori 101019d6b8fSAnthony Liguori if (!strcmp(bochs->magic, HEADER_MAGIC) && 102019d6b8fSAnthony Liguori !strcmp(bochs->type, REDOLOG_TYPE) && 103019d6b8fSAnthony Liguori !strcmp(bochs->subtype, GROWING_TYPE) && 104019d6b8fSAnthony Liguori ((le32_to_cpu(bochs->version) == HEADER_VERSION) || 105019d6b8fSAnthony Liguori (le32_to_cpu(bochs->version) == HEADER_V1))) 106019d6b8fSAnthony Liguori return 100; 107019d6b8fSAnthony Liguori 108019d6b8fSAnthony Liguori return 0; 109019d6b8fSAnthony Liguori } 110019d6b8fSAnthony Liguori 1117a6f3913SChristoph Hellwig static int bochs_open(BlockDriverState *bs, int flags) 112019d6b8fSAnthony Liguori { 113019d6b8fSAnthony Liguori BDRVBochsState *s = bs->opaque; 1147a6f3913SChristoph Hellwig int i; 115019d6b8fSAnthony Liguori struct bochs_header bochs; 116019d6b8fSAnthony Liguori struct bochs_header_v1 header_v1; 117019d6b8fSAnthony Liguori 118019d6b8fSAnthony Liguori bs->read_only = 1; // no write support yet 119019d6b8fSAnthony Liguori 1207a6f3913SChristoph Hellwig if (bdrv_pread(bs->file, 0, &bochs, sizeof(bochs)) != sizeof(bochs)) { 121019d6b8fSAnthony Liguori goto fail; 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))) { 129019d6b8fSAnthony Liguori goto fail; 130019d6b8fSAnthony Liguori } 131019d6b8fSAnthony Liguori 132019d6b8fSAnthony Liguori if (le32_to_cpu(bochs.version) == HEADER_V1) { 133019d6b8fSAnthony Liguori memcpy(&header_v1, &bochs, sizeof(bochs)); 134019d6b8fSAnthony Liguori bs->total_sectors = le64_to_cpu(header_v1.extra.redolog.disk) / 512; 135019d6b8fSAnthony Liguori } else { 136019d6b8fSAnthony Liguori bs->total_sectors = le64_to_cpu(bochs.extra.redolog.disk) / 512; 137019d6b8fSAnthony Liguori } 138019d6b8fSAnthony Liguori 139019d6b8fSAnthony Liguori s->catalog_size = le32_to_cpu(bochs.extra.redolog.catalog); 1407267c094SAnthony Liguori s->catalog_bitmap = g_malloc(s->catalog_size * 4); 1417a6f3913SChristoph Hellwig if (bdrv_pread(bs->file, le32_to_cpu(bochs.header), s->catalog_bitmap, 1427a6f3913SChristoph Hellwig s->catalog_size * 4) != s->catalog_size * 4) 143019d6b8fSAnthony Liguori goto fail; 144019d6b8fSAnthony Liguori for (i = 0; i < s->catalog_size; i++) 145019d6b8fSAnthony Liguori le32_to_cpus(&s->catalog_bitmap[i]); 146019d6b8fSAnthony Liguori 147019d6b8fSAnthony Liguori s->data_offset = le32_to_cpu(bochs.header) + (s->catalog_size * 4); 148019d6b8fSAnthony Liguori 149019d6b8fSAnthony Liguori s->bitmap_blocks = 1 + (le32_to_cpu(bochs.extra.redolog.bitmap) - 1) / 512; 150019d6b8fSAnthony Liguori s->extent_blocks = 1 + (le32_to_cpu(bochs.extra.redolog.extent) - 1) / 512; 151019d6b8fSAnthony Liguori 152019d6b8fSAnthony Liguori s->extent_size = le32_to_cpu(bochs.extra.redolog.extent); 153019d6b8fSAnthony Liguori 154848c66e8SPaolo Bonzini qemu_co_mutex_init(&s->lock); 155019d6b8fSAnthony Liguori return 0; 156019d6b8fSAnthony Liguori fail: 157019d6b8fSAnthony Liguori return -1; 158019d6b8fSAnthony Liguori } 159019d6b8fSAnthony Liguori 160efbca10fSChristoph Hellwig static int64_t seek_to_sector(BlockDriverState *bs, int64_t sector_num) 161019d6b8fSAnthony Liguori { 162019d6b8fSAnthony Liguori BDRVBochsState *s = bs->opaque; 163019d6b8fSAnthony Liguori int64_t offset = sector_num * 512; 164efbca10fSChristoph Hellwig int64_t extent_index, extent_offset, bitmap_offset; 165019d6b8fSAnthony Liguori char bitmap_entry; 166019d6b8fSAnthony Liguori 167019d6b8fSAnthony Liguori // seek to sector 168019d6b8fSAnthony Liguori extent_index = offset / s->extent_size; 169019d6b8fSAnthony Liguori extent_offset = (offset % s->extent_size) / 512; 170019d6b8fSAnthony Liguori 171efbca10fSChristoph Hellwig if (s->catalog_bitmap[extent_index] == 0xffffffff) { 172efbca10fSChristoph Hellwig return -1; /* not allocated */ 173019d6b8fSAnthony Liguori } 174019d6b8fSAnthony Liguori 175019d6b8fSAnthony Liguori bitmap_offset = s->data_offset + (512 * s->catalog_bitmap[extent_index] * 176019d6b8fSAnthony Liguori (s->extent_blocks + s->bitmap_blocks)); 177019d6b8fSAnthony Liguori 178efbca10fSChristoph Hellwig /* read in bitmap for current extent */ 1797a6f3913SChristoph Hellwig if (bdrv_pread(bs->file, bitmap_offset + (extent_offset / 8), 1807a6f3913SChristoph Hellwig &bitmap_entry, 1) != 1) { 181ecbe1576SBlue Swirl return -1; 182ecbe1576SBlue Swirl } 183019d6b8fSAnthony Liguori 184efbca10fSChristoph Hellwig if (!((bitmap_entry >> (extent_offset % 8)) & 1)) { 185efbca10fSChristoph Hellwig return -1; /* not allocated */ 186019d6b8fSAnthony Liguori } 187019d6b8fSAnthony Liguori 188efbca10fSChristoph Hellwig return bitmap_offset + (512 * (s->bitmap_blocks + extent_offset)); 189019d6b8fSAnthony Liguori } 190019d6b8fSAnthony Liguori 191019d6b8fSAnthony Liguori static int bochs_read(BlockDriverState *bs, int64_t sector_num, 192019d6b8fSAnthony Liguori uint8_t *buf, int nb_sectors) 193019d6b8fSAnthony Liguori { 194019d6b8fSAnthony Liguori int ret; 195019d6b8fSAnthony Liguori 196019d6b8fSAnthony Liguori while (nb_sectors > 0) { 197efbca10fSChristoph Hellwig int64_t block_offset = seek_to_sector(bs, sector_num); 198efbca10fSChristoph Hellwig if (block_offset >= 0) { 1997a6f3913SChristoph Hellwig ret = bdrv_pread(bs->file, block_offset, buf, 512); 200efbca10fSChristoph Hellwig if (ret != 512) { 201019d6b8fSAnthony Liguori return -1; 202019d6b8fSAnthony Liguori } 203efbca10fSChristoph Hellwig } else 204019d6b8fSAnthony Liguori memset(buf, 0, 512); 205019d6b8fSAnthony Liguori nb_sectors--; 206019d6b8fSAnthony Liguori sector_num++; 207019d6b8fSAnthony Liguori buf += 512; 208019d6b8fSAnthony Liguori } 209019d6b8fSAnthony Liguori return 0; 210019d6b8fSAnthony Liguori } 211019d6b8fSAnthony Liguori 212*2914caa0SPaolo Bonzini static coroutine_fn int bochs_co_read(BlockDriverState *bs, int64_t sector_num, 213*2914caa0SPaolo Bonzini uint8_t *buf, int nb_sectors) 214*2914caa0SPaolo Bonzini { 215*2914caa0SPaolo Bonzini int ret; 216*2914caa0SPaolo Bonzini BDRVBochsState *s = bs->opaque; 217*2914caa0SPaolo Bonzini qemu_co_mutex_lock(&s->lock); 218*2914caa0SPaolo Bonzini ret = bochs_read(bs, sector_num, buf, nb_sectors); 219*2914caa0SPaolo Bonzini qemu_co_mutex_unlock(&s->lock); 220*2914caa0SPaolo Bonzini return ret; 221*2914caa0SPaolo Bonzini } 222*2914caa0SPaolo Bonzini 223019d6b8fSAnthony Liguori static void bochs_close(BlockDriverState *bs) 224019d6b8fSAnthony Liguori { 225019d6b8fSAnthony Liguori BDRVBochsState *s = bs->opaque; 2267267c094SAnthony Liguori g_free(s->catalog_bitmap); 227019d6b8fSAnthony Liguori } 228019d6b8fSAnthony Liguori 229019d6b8fSAnthony Liguori static BlockDriver bdrv_bochs = { 230019d6b8fSAnthony Liguori .format_name = "bochs", 231019d6b8fSAnthony Liguori .instance_size = sizeof(BDRVBochsState), 232019d6b8fSAnthony Liguori .bdrv_probe = bochs_probe, 2337a6f3913SChristoph Hellwig .bdrv_open = bochs_open, 234*2914caa0SPaolo Bonzini .bdrv_read = bochs_co_read, 235019d6b8fSAnthony Liguori .bdrv_close = bochs_close, 236019d6b8fSAnthony Liguori }; 237019d6b8fSAnthony Liguori 238019d6b8fSAnthony Liguori static void bdrv_bochs_init(void) 239019d6b8fSAnthony Liguori { 240019d6b8fSAnthony Liguori bdrv_register(&bdrv_bochs); 241019d6b8fSAnthony Liguori } 242019d6b8fSAnthony Liguori 243019d6b8fSAnthony Liguori block_init(bdrv_bochs_init); 244