xref: /qemu/block/bochs.c (revision a4b740db)
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 
bochs_probe(const uint8_t * buf,int buf_size,const char * filename)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 
bochs_open(BlockDriverState * bs,QDict * options,int flags,Error ** errp)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 
108a4b740dbSKevin Wolf     GLOBAL_STATE_CODE();
109a4b740dbSKevin Wolf 
110eaa2410fSKevin Wolf     /* No write support yet */
111018f9deaSKevin Wolf     bdrv_graph_rdlock_main_loop();
112eaa2410fSKevin Wolf     ret = bdrv_apply_auto_read_only(bs, NULL, errp);
113018f9deaSKevin Wolf     bdrv_graph_rdunlock_main_loop();
114eaa2410fSKevin Wolf     if (ret < 0) {
115eaa2410fSKevin Wolf         return ret;
116eaa2410fSKevin Wolf     }
117eaa2410fSKevin Wolf 
11883930780SVladimir Sementsov-Ogievskiy     ret = bdrv_open_file_child(NULL, options, "file", bs, errp);
11983930780SVladimir Sementsov-Ogievskiy     if (ret < 0) {
12083930780SVladimir Sementsov-Ogievskiy         return ret;
1214e4bf5c4SKevin Wolf     }
1224e4bf5c4SKevin Wolf 
123a4b740dbSKevin Wolf     GRAPH_RDLOCK_GUARD_MAINLOOP();
124a4b740dbSKevin Wolf 
12532cc71deSAlberto Faria     ret = bdrv_pread(bs->file, 0, sizeof(bochs), &bochs, 0);
1265b7d7dfdSKevin Wolf     if (ret < 0) {
1275b7d7dfdSKevin Wolf         return ret;
128019d6b8fSAnthony Liguori     }
129019d6b8fSAnthony Liguori 
130019d6b8fSAnthony Liguori     if (strcmp(bochs.magic, HEADER_MAGIC) ||
131019d6b8fSAnthony Liguori         strcmp(bochs.type, REDOLOG_TYPE) ||
132019d6b8fSAnthony Liguori         strcmp(bochs.subtype, GROWING_TYPE) ||
133019d6b8fSAnthony Liguori         ((le32_to_cpu(bochs.version) != HEADER_VERSION) &&
134019d6b8fSAnthony Liguori         (le32_to_cpu(bochs.version) != HEADER_V1))) {
13576abe407SPaolo Bonzini         error_setg(errp, "Image not in Bochs format");
13676abe407SPaolo Bonzini         return -EINVAL;
137019d6b8fSAnthony Liguori     }
138019d6b8fSAnthony Liguori 
139019d6b8fSAnthony Liguori     if (le32_to_cpu(bochs.version) == HEADER_V1) {
1403dd8a676SKevin Wolf         bs->total_sectors = le64_to_cpu(bochs.extra.redolog_v1.disk) / 512;
141019d6b8fSAnthony Liguori     } else {
142019d6b8fSAnthony Liguori         bs->total_sectors = le64_to_cpu(bochs.extra.redolog.disk) / 512;
143019d6b8fSAnthony Liguori     }
144019d6b8fSAnthony Liguori 
145e3737b82SKevin Wolf     /* Limit to 1M entries to avoid unbounded allocation. This is what is
146e3737b82SKevin Wolf      * needed for the largest image that bximage can create (~8 TB). */
1473dd8a676SKevin Wolf     s->catalog_size = le32_to_cpu(bochs.catalog);
148e3737b82SKevin Wolf     if (s->catalog_size > 0x100000) {
149e3737b82SKevin Wolf         error_setg(errp, "Catalog size is too large");
150e3737b82SKevin Wolf         return -EFBIG;
151e3737b82SKevin Wolf     }
152e3737b82SKevin Wolf 
15302c4f26bSMarkus Armbruster     s->catalog_bitmap = g_try_new(uint32_t, s->catalog_size);
1547bf665eeSKevin Wolf     if (s->catalog_size && s->catalog_bitmap == NULL) {
1557bf665eeSKevin Wolf         error_setg(errp, "Could not allocate memory for catalog");
1567bf665eeSKevin Wolf         return -ENOMEM;
1577bf665eeSKevin Wolf     }
1585b7d7dfdSKevin Wolf 
15932cc71deSAlberto Faria     ret = bdrv_pread(bs->file, le32_to_cpu(bochs.header), s->catalog_size * 4,
16032cc71deSAlberto Faria                      s->catalog_bitmap, 0);
1615b7d7dfdSKevin Wolf     if (ret < 0) {
162019d6b8fSAnthony Liguori         goto fail;
1635b7d7dfdSKevin Wolf     }
1645b7d7dfdSKevin Wolf 
165019d6b8fSAnthony Liguori     for (i = 0; i < s->catalog_size; i++)
166019d6b8fSAnthony Liguori         le32_to_cpus(&s->catalog_bitmap[i]);
167019d6b8fSAnthony Liguori 
168019d6b8fSAnthony Liguori     s->data_offset = le32_to_cpu(bochs.header) + (s->catalog_size * 4);
169019d6b8fSAnthony Liguori 
1703dd8a676SKevin Wolf     s->bitmap_blocks = 1 + (le32_to_cpu(bochs.bitmap) - 1) / 512;
1713dd8a676SKevin Wolf     s->extent_blocks = 1 + (le32_to_cpu(bochs.extent) - 1) / 512;
172019d6b8fSAnthony Liguori 
1733dd8a676SKevin Wolf     s->extent_size = le32_to_cpu(bochs.extent);
174715c3f60SKevin Wolf     if (s->extent_size < BDRV_SECTOR_SIZE) {
175715c3f60SKevin Wolf         /* bximage actually never creates extents smaller than 4k */
176715c3f60SKevin Wolf         error_setg(errp, "Extent size must be at least 512");
177715c3f60SKevin Wolf         ret = -EINVAL;
178715c3f60SKevin Wolf         goto fail;
179715c3f60SKevin Wolf     } else if (!is_power_of_2(s->extent_size)) {
180715c3f60SKevin Wolf         error_setg(errp, "Extent size %" PRIu32 " is not a power of two",
181715c3f60SKevin Wolf                    s->extent_size);
18228ec11bcSKevin Wolf         ret = -EINVAL;
18328ec11bcSKevin Wolf         goto fail;
1848e53abbcSKevin Wolf     } else if (s->extent_size > 0x800000) {
1858e53abbcSKevin Wolf         error_setg(errp, "Extent size %" PRIu32 " is too large",
1868e53abbcSKevin Wolf                    s->extent_size);
18728ec11bcSKevin Wolf         ret = -EINVAL;
18828ec11bcSKevin Wolf         goto fail;
1898e53abbcSKevin Wolf     }
190019d6b8fSAnthony Liguori 
191715c3f60SKevin Wolf     if (s->catalog_size < DIV_ROUND_UP(bs->total_sectors,
192715c3f60SKevin Wolf                                        s->extent_size / BDRV_SECTOR_SIZE))
193715c3f60SKevin Wolf     {
194e3737b82SKevin Wolf         error_setg(errp, "Catalog size is too small for this disk size");
195e3737b82SKevin Wolf         ret = -EINVAL;
196e3737b82SKevin Wolf         goto fail;
197e3737b82SKevin Wolf     }
198e3737b82SKevin Wolf 
199848c66e8SPaolo Bonzini     qemu_co_mutex_init(&s->lock);
200019d6b8fSAnthony Liguori     return 0;
2015b7d7dfdSKevin Wolf 
202019d6b8fSAnthony Liguori fail:
2035b7d7dfdSKevin Wolf     g_free(s->catalog_bitmap);
2045b7d7dfdSKevin Wolf     return ret;
205019d6b8fSAnthony Liguori }
206019d6b8fSAnthony Liguori 
bochs_refresh_limits(BlockDriverState * bs,Error ** errp)207a6506481SEric Blake static void bochs_refresh_limits(BlockDriverState *bs, Error **errp)
208a6506481SEric Blake {
209a5b8dd2cSEric Blake     bs->bl.request_alignment = BDRV_SECTOR_SIZE; /* No sub-sector I/O */
210a6506481SEric Blake }
211a6506481SEric Blake 
212e7918e96SPaolo Bonzini static int64_t coroutine_fn GRAPH_RDLOCK
seek_to_sector(BlockDriverState * bs,int64_t sector_num)213e7918e96SPaolo Bonzini seek_to_sector(BlockDriverState *bs, int64_t sector_num)
214019d6b8fSAnthony Liguori {
215019d6b8fSAnthony Liguori     BDRVBochsState *s = bs->opaque;
216246f6583SKevin Wolf     uint64_t offset = sector_num * 512;
217246f6583SKevin Wolf     uint64_t extent_index, extent_offset, bitmap_offset;
218019d6b8fSAnthony Liguori     char bitmap_entry;
219e1b42f45SMax Reitz     int ret;
220019d6b8fSAnthony Liguori 
221019d6b8fSAnthony Liguori     // seek to sector
222019d6b8fSAnthony Liguori     extent_index = offset / s->extent_size;
223019d6b8fSAnthony Liguori     extent_offset = (offset % s->extent_size) / 512;
224019d6b8fSAnthony Liguori 
225efbca10fSChristoph Hellwig     if (s->catalog_bitmap[extent_index] == 0xffffffff) {
226e1b42f45SMax Reitz         return 0; /* not allocated */
227019d6b8fSAnthony Liguori     }
228019d6b8fSAnthony Liguori 
229a9ba36a4SKevin Wolf     bitmap_offset = s->data_offset +
230a9ba36a4SKevin Wolf         (512 * (uint64_t) s->catalog_bitmap[extent_index] *
231019d6b8fSAnthony Liguori         (s->extent_blocks + s->bitmap_blocks));
232019d6b8fSAnthony Liguori 
233efbca10fSChristoph Hellwig     /* read in bitmap for current extent */
234e7918e96SPaolo Bonzini     ret = bdrv_co_pread(bs->file, bitmap_offset + (extent_offset / 8), 1,
23532cc71deSAlberto Faria                         &bitmap_entry, 0);
236e1b42f45SMax Reitz     if (ret < 0) {
237e1b42f45SMax Reitz         return ret;
238ecbe1576SBlue Swirl     }
239019d6b8fSAnthony Liguori 
240efbca10fSChristoph Hellwig     if (!((bitmap_entry >> (extent_offset % 8)) & 1)) {
241e1b42f45SMax Reitz         return 0; /* not allocated */
242019d6b8fSAnthony Liguori     }
243019d6b8fSAnthony Liguori 
244efbca10fSChristoph Hellwig     return bitmap_offset + (512 * (s->bitmap_blocks + extent_offset));
245019d6b8fSAnthony Liguori }
246019d6b8fSAnthony Liguori 
247b9b10c35SKevin Wolf static int coroutine_fn GRAPH_RDLOCK
bochs_co_preadv(BlockDriverState * bs,int64_t offset,int64_t bytes,QEMUIOVector * qiov,BdrvRequestFlags flags)248f7ef38ddSVladimir Sementsov-Ogievskiy bochs_co_preadv(BlockDriverState *bs, int64_t offset, int64_t bytes,
249f7ef38ddSVladimir Sementsov-Ogievskiy                 QEMUIOVector *qiov, BdrvRequestFlags flags)
250019d6b8fSAnthony Liguori {
2513b8fd330SKevin Wolf     BDRVBochsState *s = bs->opaque;
2523b8fd330SKevin Wolf     uint64_t sector_num = offset >> BDRV_SECTOR_BITS;
2533b8fd330SKevin Wolf     int nb_sectors = bytes >> BDRV_SECTOR_BITS;
2543b8fd330SKevin Wolf     uint64_t bytes_done = 0;
2553b8fd330SKevin Wolf     QEMUIOVector local_qiov;
256019d6b8fSAnthony Liguori     int ret;
257019d6b8fSAnthony Liguori 
2581bbbf32dSNir Soffer     assert(QEMU_IS_ALIGNED(offset, BDRV_SECTOR_SIZE));
2591bbbf32dSNir Soffer     assert(QEMU_IS_ALIGNED(bytes, BDRV_SECTOR_SIZE));
2603b8fd330SKevin Wolf 
2613b8fd330SKevin Wolf     qemu_iovec_init(&local_qiov, qiov->niov);
2623b8fd330SKevin Wolf     qemu_co_mutex_lock(&s->lock);
2633b8fd330SKevin Wolf 
264019d6b8fSAnthony Liguori     while (nb_sectors > 0) {
265efbca10fSChristoph Hellwig         int64_t block_offset = seek_to_sector(bs, sector_num);
266e1b42f45SMax Reitz         if (block_offset < 0) {
2673b8fd330SKevin Wolf             ret = block_offset;
2683b8fd330SKevin Wolf             goto fail;
2693b8fd330SKevin Wolf         }
2703b8fd330SKevin Wolf 
2713b8fd330SKevin Wolf         qemu_iovec_reset(&local_qiov);
2723b8fd330SKevin Wolf         qemu_iovec_concat(&local_qiov, qiov, bytes_done, 512);
2733b8fd330SKevin Wolf 
2743b8fd330SKevin Wolf         if (block_offset > 0) {
275a03ef88fSKevin Wolf             ret = bdrv_co_preadv(bs->file, block_offset, 512,
2763b8fd330SKevin Wolf                                  &local_qiov, 0);
277e1b42f45SMax Reitz             if (ret < 0) {
2783b8fd330SKevin Wolf                 goto fail;
279019d6b8fSAnthony Liguori             }
280e1b42f45SMax Reitz         } else {
2813b8fd330SKevin Wolf             qemu_iovec_memset(&local_qiov, 0, 0, 512);
282e1b42f45SMax Reitz         }
283019d6b8fSAnthony Liguori         nb_sectors--;
284019d6b8fSAnthony Liguori         sector_num++;
2853b8fd330SKevin Wolf         bytes_done += 512;
286019d6b8fSAnthony Liguori     }
287019d6b8fSAnthony Liguori 
2883b8fd330SKevin Wolf     ret = 0;
2893b8fd330SKevin Wolf fail:
2902914caa0SPaolo Bonzini     qemu_co_mutex_unlock(&s->lock);
2913b8fd330SKevin Wolf     qemu_iovec_destroy(&local_qiov);
2923b8fd330SKevin Wolf 
2932914caa0SPaolo Bonzini     return ret;
2942914caa0SPaolo Bonzini }
2952914caa0SPaolo Bonzini 
bochs_close(BlockDriverState * bs)296019d6b8fSAnthony Liguori static void bochs_close(BlockDriverState *bs)
297019d6b8fSAnthony Liguori {
298019d6b8fSAnthony Liguori     BDRVBochsState *s = bs->opaque;
2997267c094SAnthony Liguori     g_free(s->catalog_bitmap);
300019d6b8fSAnthony Liguori }
301019d6b8fSAnthony Liguori 
302019d6b8fSAnthony Liguori static BlockDriver bdrv_bochs = {
303019d6b8fSAnthony Liguori     .format_name	= "bochs",
304019d6b8fSAnthony Liguori     .instance_size	= sizeof(BDRVBochsState),
305019d6b8fSAnthony Liguori     .bdrv_probe		= bochs_probe,
3067a6f3913SChristoph Hellwig     .bdrv_open		= bochs_open,
30769dca43dSMax Reitz     .bdrv_child_perm     = bdrv_default_perms,
308a6506481SEric Blake     .bdrv_refresh_limits = bochs_refresh_limits,
3093b8fd330SKevin Wolf     .bdrv_co_preadv = bochs_co_preadv,
310019d6b8fSAnthony Liguori     .bdrv_close		= bochs_close,
311d67066d8SMax Reitz     .is_format          = true,
312019d6b8fSAnthony Liguori };
313019d6b8fSAnthony Liguori 
bdrv_bochs_init(void)314019d6b8fSAnthony Liguori static void bdrv_bochs_init(void)
315019d6b8fSAnthony Liguori {
316019d6b8fSAnthony Liguori     bdrv_register(&bdrv_bochs);
317019d6b8fSAnthony Liguori }
318019d6b8fSAnthony Liguori 
319019d6b8fSAnthony Liguori block_init(bdrv_bochs_init);
320