xref: /openbmc/qemu/block/bochs.c (revision a5b8dd2ce83208cd7d6eb4562339ecf5aae13574)
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"
27019d6b8fSAnthony Liguori #include "qemu-common.h"
28737e150eSPaolo Bonzini #include "block/block_int.h"
291de7afc9SPaolo Bonzini #include "qemu/module.h"
3058369e22SPaolo Bonzini #include "qemu/bswap.h"
31019d6b8fSAnthony Liguori 
32019d6b8fSAnthony Liguori /**************************************************************/
33019d6b8fSAnthony Liguori 
34019d6b8fSAnthony Liguori #define HEADER_MAGIC "Bochs Virtual HD Image"
35019d6b8fSAnthony Liguori #define HEADER_VERSION 0x00020000
36019d6b8fSAnthony Liguori #define HEADER_V1 0x00010000
37019d6b8fSAnthony Liguori #define HEADER_SIZE 512
38019d6b8fSAnthony Liguori 
39019d6b8fSAnthony Liguori #define REDOLOG_TYPE "Redolog"
40019d6b8fSAnthony Liguori #define GROWING_TYPE "Growing"
41019d6b8fSAnthony Liguori 
42019d6b8fSAnthony Liguori // not allocated: 0xffffffff
43019d6b8fSAnthony Liguori 
44019d6b8fSAnthony Liguori // always little-endian
45019d6b8fSAnthony Liguori struct bochs_header {
463dd8a676SKevin Wolf     char magic[32];     /* "Bochs Virtual HD Image" */
473dd8a676SKevin Wolf     char type[16];      /* "Redolog" */
483dd8a676SKevin Wolf     char subtype[16];   /* "Undoable" / "Volatile" / "Growing" */
49019d6b8fSAnthony Liguori     uint32_t version;
503dd8a676SKevin Wolf     uint32_t header;    /* size of header */
513dd8a676SKevin Wolf 
523dd8a676SKevin Wolf     uint32_t catalog;   /* num of entries */
533dd8a676SKevin Wolf     uint32_t bitmap;    /* bitmap size */
543dd8a676SKevin Wolf     uint32_t extent;    /* extent size */
55019d6b8fSAnthony Liguori 
56019d6b8fSAnthony Liguori     union {
57019d6b8fSAnthony Liguori         struct {
583dd8a676SKevin Wolf             uint32_t reserved;  /* for ??? */
593dd8a676SKevin Wolf             uint64_t disk;      /* disk size */
603dd8a676SKevin Wolf             char padding[HEADER_SIZE - 64 - 20 - 12];
613dd8a676SKevin Wolf         } QEMU_PACKED redolog;
623dd8a676SKevin Wolf         struct {
633dd8a676SKevin Wolf             uint64_t disk;      /* disk size */
643dd8a676SKevin Wolf             char padding[HEADER_SIZE - 64 - 20 - 8];
653dd8a676SKevin Wolf         } QEMU_PACKED redolog_v1;
663dd8a676SKevin Wolf         char padding[HEADER_SIZE - 64 - 20];
67019d6b8fSAnthony Liguori     } extra;
683dd8a676SKevin Wolf } QEMU_PACKED;
69019d6b8fSAnthony Liguori 
70019d6b8fSAnthony Liguori typedef struct BDRVBochsState {
71848c66e8SPaolo Bonzini     CoMutex lock;
72019d6b8fSAnthony Liguori     uint32_t *catalog_bitmap;
73246f6583SKevin Wolf     uint32_t catalog_size;
74019d6b8fSAnthony Liguori 
75246f6583SKevin Wolf     uint32_t data_offset;
76019d6b8fSAnthony Liguori 
77246f6583SKevin Wolf     uint32_t bitmap_blocks;
78246f6583SKevin Wolf     uint32_t extent_blocks;
79246f6583SKevin Wolf     uint32_t extent_size;
80019d6b8fSAnthony Liguori } BDRVBochsState;
81019d6b8fSAnthony Liguori 
82019d6b8fSAnthony Liguori static int bochs_probe(const uint8_t *buf, int buf_size, const char *filename)
83019d6b8fSAnthony Liguori {
84019d6b8fSAnthony Liguori     const struct bochs_header *bochs = (const void *)buf;
85019d6b8fSAnthony Liguori 
86019d6b8fSAnthony Liguori     if (buf_size < HEADER_SIZE)
87019d6b8fSAnthony Liguori 	return 0;
88019d6b8fSAnthony Liguori 
89019d6b8fSAnthony Liguori     if (!strcmp(bochs->magic, HEADER_MAGIC) &&
90019d6b8fSAnthony Liguori 	!strcmp(bochs->type, REDOLOG_TYPE) &&
91019d6b8fSAnthony Liguori 	!strcmp(bochs->subtype, GROWING_TYPE) &&
92019d6b8fSAnthony Liguori 	((le32_to_cpu(bochs->version) == HEADER_VERSION) ||
93019d6b8fSAnthony Liguori 	(le32_to_cpu(bochs->version) == HEADER_V1)))
94019d6b8fSAnthony Liguori 	return 100;
95019d6b8fSAnthony Liguori 
96019d6b8fSAnthony Liguori     return 0;
97019d6b8fSAnthony Liguori }
98019d6b8fSAnthony Liguori 
99015a1036SMax Reitz static int bochs_open(BlockDriverState *bs, QDict *options, int flags,
100015a1036SMax Reitz                       Error **errp)
101019d6b8fSAnthony Liguori {
102019d6b8fSAnthony Liguori     BDRVBochsState *s = bs->opaque;
103246f6583SKevin Wolf     uint32_t i;
104019d6b8fSAnthony Liguori     struct bochs_header bochs;
1055b7d7dfdSKevin Wolf     int ret;
106019d6b8fSAnthony Liguori 
107019d6b8fSAnthony Liguori     bs->read_only = 1; // no write support yet
108019d6b8fSAnthony Liguori 
1099a4f4c31SKevin Wolf     ret = bdrv_pread(bs->file->bs, 0, &bochs, sizeof(bochs));
1105b7d7dfdSKevin Wolf     if (ret < 0) {
1115b7d7dfdSKevin Wolf         return ret;
112019d6b8fSAnthony Liguori     }
113019d6b8fSAnthony Liguori 
114019d6b8fSAnthony Liguori     if (strcmp(bochs.magic, HEADER_MAGIC) ||
115019d6b8fSAnthony Liguori         strcmp(bochs.type, REDOLOG_TYPE) ||
116019d6b8fSAnthony Liguori         strcmp(bochs.subtype, GROWING_TYPE) ||
117019d6b8fSAnthony Liguori 	((le32_to_cpu(bochs.version) != HEADER_VERSION) &&
118019d6b8fSAnthony Liguori 	(le32_to_cpu(bochs.version) != HEADER_V1))) {
11976abe407SPaolo Bonzini         error_setg(errp, "Image not in Bochs format");
12076abe407SPaolo Bonzini         return -EINVAL;
121019d6b8fSAnthony Liguori     }
122019d6b8fSAnthony Liguori 
123019d6b8fSAnthony Liguori     if (le32_to_cpu(bochs.version) == HEADER_V1) {
1243dd8a676SKevin Wolf         bs->total_sectors = le64_to_cpu(bochs.extra.redolog_v1.disk) / 512;
125019d6b8fSAnthony Liguori     } else {
126019d6b8fSAnthony Liguori         bs->total_sectors = le64_to_cpu(bochs.extra.redolog.disk) / 512;
127019d6b8fSAnthony Liguori     }
128019d6b8fSAnthony Liguori 
129e3737b82SKevin Wolf     /* Limit to 1M entries to avoid unbounded allocation. This is what is
130e3737b82SKevin Wolf      * needed for the largest image that bximage can create (~8 TB). */
1313dd8a676SKevin Wolf     s->catalog_size = le32_to_cpu(bochs.catalog);
132e3737b82SKevin Wolf     if (s->catalog_size > 0x100000) {
133e3737b82SKevin Wolf         error_setg(errp, "Catalog size is too large");
134e3737b82SKevin Wolf         return -EFBIG;
135e3737b82SKevin Wolf     }
136e3737b82SKevin Wolf 
13702c4f26bSMarkus Armbruster     s->catalog_bitmap = g_try_new(uint32_t, s->catalog_size);
1387bf665eeSKevin Wolf     if (s->catalog_size && s->catalog_bitmap == NULL) {
1397bf665eeSKevin Wolf         error_setg(errp, "Could not allocate memory for catalog");
1407bf665eeSKevin Wolf         return -ENOMEM;
1417bf665eeSKevin Wolf     }
1425b7d7dfdSKevin Wolf 
1439a4f4c31SKevin Wolf     ret = bdrv_pread(bs->file->bs, le32_to_cpu(bochs.header), s->catalog_bitmap,
1445b7d7dfdSKevin Wolf                      s->catalog_size * 4);
1455b7d7dfdSKevin Wolf     if (ret < 0) {
146019d6b8fSAnthony Liguori         goto fail;
1475b7d7dfdSKevin Wolf     }
1485b7d7dfdSKevin Wolf 
149019d6b8fSAnthony Liguori     for (i = 0; i < s->catalog_size; i++)
150019d6b8fSAnthony Liguori 	le32_to_cpus(&s->catalog_bitmap[i]);
151019d6b8fSAnthony Liguori 
152019d6b8fSAnthony Liguori     s->data_offset = le32_to_cpu(bochs.header) + (s->catalog_size * 4);
153019d6b8fSAnthony Liguori 
1543dd8a676SKevin Wolf     s->bitmap_blocks = 1 + (le32_to_cpu(bochs.bitmap) - 1) / 512;
1553dd8a676SKevin Wolf     s->extent_blocks = 1 + (le32_to_cpu(bochs.extent) - 1) / 512;
156019d6b8fSAnthony Liguori 
1573dd8a676SKevin Wolf     s->extent_size = le32_to_cpu(bochs.extent);
158715c3f60SKevin Wolf     if (s->extent_size < BDRV_SECTOR_SIZE) {
159715c3f60SKevin Wolf         /* bximage actually never creates extents smaller than 4k */
160715c3f60SKevin Wolf         error_setg(errp, "Extent size must be at least 512");
161715c3f60SKevin Wolf         ret = -EINVAL;
162715c3f60SKevin Wolf         goto fail;
163715c3f60SKevin Wolf     } else if (!is_power_of_2(s->extent_size)) {
164715c3f60SKevin Wolf         error_setg(errp, "Extent size %" PRIu32 " is not a power of two",
165715c3f60SKevin Wolf                    s->extent_size);
16628ec11bcSKevin Wolf         ret = -EINVAL;
16728ec11bcSKevin Wolf         goto fail;
1688e53abbcSKevin Wolf     } else if (s->extent_size > 0x800000) {
1698e53abbcSKevin Wolf         error_setg(errp, "Extent size %" PRIu32 " is too large",
1708e53abbcSKevin Wolf                    s->extent_size);
17128ec11bcSKevin Wolf         ret = -EINVAL;
17228ec11bcSKevin Wolf         goto fail;
1738e53abbcSKevin Wolf     }
174019d6b8fSAnthony Liguori 
175715c3f60SKevin Wolf     if (s->catalog_size < DIV_ROUND_UP(bs->total_sectors,
176715c3f60SKevin Wolf                                        s->extent_size / BDRV_SECTOR_SIZE))
177715c3f60SKevin Wolf     {
178e3737b82SKevin Wolf         error_setg(errp, "Catalog size is too small for this disk size");
179e3737b82SKevin Wolf         ret = -EINVAL;
180e3737b82SKevin Wolf         goto fail;
181e3737b82SKevin Wolf     }
182e3737b82SKevin Wolf 
183848c66e8SPaolo Bonzini     qemu_co_mutex_init(&s->lock);
184019d6b8fSAnthony Liguori     return 0;
1855b7d7dfdSKevin Wolf 
186019d6b8fSAnthony Liguori fail:
1875b7d7dfdSKevin Wolf     g_free(s->catalog_bitmap);
1885b7d7dfdSKevin Wolf     return ret;
189019d6b8fSAnthony Liguori }
190019d6b8fSAnthony Liguori 
191a6506481SEric Blake static void bochs_refresh_limits(BlockDriverState *bs, Error **errp)
192a6506481SEric Blake {
193*a5b8dd2cSEric Blake     bs->bl.request_alignment = BDRV_SECTOR_SIZE; /* No sub-sector I/O */
194a6506481SEric Blake }
195a6506481SEric Blake 
196efbca10fSChristoph Hellwig static int64_t seek_to_sector(BlockDriverState *bs, int64_t sector_num)
197019d6b8fSAnthony Liguori {
198019d6b8fSAnthony Liguori     BDRVBochsState *s = bs->opaque;
199246f6583SKevin Wolf     uint64_t offset = sector_num * 512;
200246f6583SKevin Wolf     uint64_t extent_index, extent_offset, bitmap_offset;
201019d6b8fSAnthony Liguori     char bitmap_entry;
202e1b42f45SMax Reitz     int ret;
203019d6b8fSAnthony Liguori 
204019d6b8fSAnthony Liguori     // seek to sector
205019d6b8fSAnthony Liguori     extent_index = offset / s->extent_size;
206019d6b8fSAnthony Liguori     extent_offset = (offset % s->extent_size) / 512;
207019d6b8fSAnthony Liguori 
208efbca10fSChristoph Hellwig     if (s->catalog_bitmap[extent_index] == 0xffffffff) {
209e1b42f45SMax Reitz 	return 0; /* not allocated */
210019d6b8fSAnthony Liguori     }
211019d6b8fSAnthony Liguori 
212a9ba36a4SKevin Wolf     bitmap_offset = s->data_offset +
213a9ba36a4SKevin Wolf         (512 * (uint64_t) s->catalog_bitmap[extent_index] *
214019d6b8fSAnthony Liguori         (s->extent_blocks + s->bitmap_blocks));
215019d6b8fSAnthony Liguori 
216efbca10fSChristoph Hellwig     /* read in bitmap for current extent */
2179a4f4c31SKevin Wolf     ret = bdrv_pread(bs->file->bs, bitmap_offset + (extent_offset / 8),
218e1b42f45SMax Reitz                      &bitmap_entry, 1);
219e1b42f45SMax Reitz     if (ret < 0) {
220e1b42f45SMax Reitz         return ret;
221ecbe1576SBlue Swirl     }
222019d6b8fSAnthony Liguori 
223efbca10fSChristoph Hellwig     if (!((bitmap_entry >> (extent_offset % 8)) & 1)) {
224e1b42f45SMax Reitz 	return 0; /* not allocated */
225019d6b8fSAnthony Liguori     }
226019d6b8fSAnthony Liguori 
227efbca10fSChristoph Hellwig     return bitmap_offset + (512 * (s->bitmap_blocks + extent_offset));
228019d6b8fSAnthony Liguori }
229019d6b8fSAnthony Liguori 
2303b8fd330SKevin Wolf static int coroutine_fn
2313b8fd330SKevin Wolf bochs_co_preadv(BlockDriverState *bs, uint64_t offset, uint64_t bytes,
2323b8fd330SKevin Wolf                 QEMUIOVector *qiov, int flags)
233019d6b8fSAnthony Liguori {
2343b8fd330SKevin Wolf     BDRVBochsState *s = bs->opaque;
2353b8fd330SKevin Wolf     uint64_t sector_num = offset >> BDRV_SECTOR_BITS;
2363b8fd330SKevin Wolf     int nb_sectors = bytes >> BDRV_SECTOR_BITS;
2373b8fd330SKevin Wolf     uint64_t bytes_done = 0;
2383b8fd330SKevin Wolf     QEMUIOVector local_qiov;
239019d6b8fSAnthony Liguori     int ret;
240019d6b8fSAnthony Liguori 
2413b8fd330SKevin Wolf     assert((offset & (BDRV_SECTOR_SIZE - 1)) == 0);
2423b8fd330SKevin Wolf     assert((bytes & (BDRV_SECTOR_SIZE - 1)) == 0);
2433b8fd330SKevin Wolf 
2443b8fd330SKevin Wolf     qemu_iovec_init(&local_qiov, qiov->niov);
2453b8fd330SKevin Wolf     qemu_co_mutex_lock(&s->lock);
2463b8fd330SKevin Wolf 
247019d6b8fSAnthony Liguori     while (nb_sectors > 0) {
248efbca10fSChristoph Hellwig         int64_t block_offset = seek_to_sector(bs, sector_num);
249e1b42f45SMax Reitz         if (block_offset < 0) {
2503b8fd330SKevin Wolf             ret = block_offset;
2513b8fd330SKevin Wolf             goto fail;
2523b8fd330SKevin Wolf         }
2533b8fd330SKevin Wolf 
2543b8fd330SKevin Wolf         qemu_iovec_reset(&local_qiov);
2553b8fd330SKevin Wolf         qemu_iovec_concat(&local_qiov, qiov, bytes_done, 512);
2563b8fd330SKevin Wolf 
2573b8fd330SKevin Wolf         if (block_offset > 0) {
2583b8fd330SKevin Wolf             ret = bdrv_co_preadv(bs->file->bs, block_offset, 512,
2593b8fd330SKevin Wolf                                  &local_qiov, 0);
260e1b42f45SMax Reitz             if (ret < 0) {
2613b8fd330SKevin Wolf                 goto fail;
262019d6b8fSAnthony Liguori             }
263e1b42f45SMax Reitz         } else {
2643b8fd330SKevin Wolf             qemu_iovec_memset(&local_qiov, 0, 0, 512);
265e1b42f45SMax Reitz         }
266019d6b8fSAnthony Liguori         nb_sectors--;
267019d6b8fSAnthony Liguori         sector_num++;
2683b8fd330SKevin Wolf         bytes_done += 512;
269019d6b8fSAnthony Liguori     }
270019d6b8fSAnthony Liguori 
2713b8fd330SKevin Wolf     ret = 0;
2723b8fd330SKevin Wolf fail:
2732914caa0SPaolo Bonzini     qemu_co_mutex_unlock(&s->lock);
2743b8fd330SKevin Wolf     qemu_iovec_destroy(&local_qiov);
2753b8fd330SKevin Wolf 
2762914caa0SPaolo Bonzini     return ret;
2772914caa0SPaolo Bonzini }
2782914caa0SPaolo Bonzini 
279019d6b8fSAnthony Liguori static void bochs_close(BlockDriverState *bs)
280019d6b8fSAnthony Liguori {
281019d6b8fSAnthony Liguori     BDRVBochsState *s = bs->opaque;
2827267c094SAnthony Liguori     g_free(s->catalog_bitmap);
283019d6b8fSAnthony Liguori }
284019d6b8fSAnthony Liguori 
285019d6b8fSAnthony Liguori static BlockDriver bdrv_bochs = {
286019d6b8fSAnthony Liguori     .format_name	= "bochs",
287019d6b8fSAnthony Liguori     .instance_size	= sizeof(BDRVBochsState),
288019d6b8fSAnthony Liguori     .bdrv_probe		= bochs_probe,
2897a6f3913SChristoph Hellwig     .bdrv_open		= bochs_open,
290a6506481SEric Blake     .bdrv_refresh_limits = bochs_refresh_limits,
2913b8fd330SKevin Wolf     .bdrv_co_preadv = bochs_co_preadv,
292019d6b8fSAnthony Liguori     .bdrv_close		= bochs_close,
293019d6b8fSAnthony Liguori };
294019d6b8fSAnthony Liguori 
295019d6b8fSAnthony Liguori static void bdrv_bochs_init(void)
296019d6b8fSAnthony Liguori {
297019d6b8fSAnthony Liguori     bdrv_register(&bdrv_bochs);
298019d6b8fSAnthony Liguori }
299019d6b8fSAnthony Liguori 
300019d6b8fSAnthony Liguori block_init(bdrv_bochs_init);
301