xref: /openbmc/qemu/block/bochs.c (revision da34e65cb4025728566d6504a99916f6e7e1dd6a)
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"
26*da34e65cSMarkus Armbruster #include "qapi/error.h"
27019d6b8fSAnthony Liguori #include "qemu-common.h"
28737e150eSPaolo Bonzini #include "block/block_int.h"
291de7afc9SPaolo Bonzini #include "qemu/module.h"
30019d6b8fSAnthony Liguori 
31019d6b8fSAnthony Liguori /**************************************************************/
32019d6b8fSAnthony Liguori 
33019d6b8fSAnthony Liguori #define HEADER_MAGIC "Bochs Virtual HD Image"
34019d6b8fSAnthony Liguori #define HEADER_VERSION 0x00020000
35019d6b8fSAnthony Liguori #define HEADER_V1 0x00010000
36019d6b8fSAnthony Liguori #define HEADER_SIZE 512
37019d6b8fSAnthony Liguori 
38019d6b8fSAnthony Liguori #define REDOLOG_TYPE "Redolog"
39019d6b8fSAnthony Liguori #define GROWING_TYPE "Growing"
40019d6b8fSAnthony Liguori 
41019d6b8fSAnthony Liguori // not allocated: 0xffffffff
42019d6b8fSAnthony Liguori 
43019d6b8fSAnthony Liguori // always little-endian
44019d6b8fSAnthony Liguori struct bochs_header {
453dd8a676SKevin Wolf     char magic[32];     /* "Bochs Virtual HD Image" */
463dd8a676SKevin Wolf     char type[16];      /* "Redolog" */
473dd8a676SKevin Wolf     char subtype[16];   /* "Undoable" / "Volatile" / "Growing" */
48019d6b8fSAnthony Liguori     uint32_t version;
493dd8a676SKevin Wolf     uint32_t header;    /* size of header */
503dd8a676SKevin Wolf 
513dd8a676SKevin Wolf     uint32_t catalog;   /* num of entries */
523dd8a676SKevin Wolf     uint32_t bitmap;    /* bitmap size */
533dd8a676SKevin Wolf     uint32_t extent;    /* extent size */
54019d6b8fSAnthony Liguori 
55019d6b8fSAnthony Liguori     union {
56019d6b8fSAnthony Liguori         struct {
573dd8a676SKevin Wolf             uint32_t reserved;  /* for ??? */
583dd8a676SKevin Wolf             uint64_t disk;      /* disk size */
593dd8a676SKevin Wolf             char padding[HEADER_SIZE - 64 - 20 - 12];
603dd8a676SKevin Wolf         } QEMU_PACKED redolog;
613dd8a676SKevin Wolf         struct {
623dd8a676SKevin Wolf             uint64_t disk;      /* disk size */
633dd8a676SKevin Wolf             char padding[HEADER_SIZE - 64 - 20 - 8];
643dd8a676SKevin Wolf         } QEMU_PACKED redolog_v1;
653dd8a676SKevin Wolf         char padding[HEADER_SIZE - 64 - 20];
66019d6b8fSAnthony Liguori     } extra;
673dd8a676SKevin Wolf } QEMU_PACKED;
68019d6b8fSAnthony Liguori 
69019d6b8fSAnthony Liguori typedef struct BDRVBochsState {
70848c66e8SPaolo Bonzini     CoMutex lock;
71019d6b8fSAnthony Liguori     uint32_t *catalog_bitmap;
72246f6583SKevin Wolf     uint32_t catalog_size;
73019d6b8fSAnthony Liguori 
74246f6583SKevin Wolf     uint32_t data_offset;
75019d6b8fSAnthony Liguori 
76246f6583SKevin Wolf     uint32_t bitmap_blocks;
77246f6583SKevin Wolf     uint32_t extent_blocks;
78246f6583SKevin Wolf     uint32_t extent_size;
79019d6b8fSAnthony Liguori } BDRVBochsState;
80019d6b8fSAnthony Liguori 
81019d6b8fSAnthony Liguori static int bochs_probe(const uint8_t *buf, int buf_size, const char *filename)
82019d6b8fSAnthony Liguori {
83019d6b8fSAnthony Liguori     const struct bochs_header *bochs = (const void *)buf;
84019d6b8fSAnthony Liguori 
85019d6b8fSAnthony Liguori     if (buf_size < HEADER_SIZE)
86019d6b8fSAnthony Liguori 	return 0;
87019d6b8fSAnthony Liguori 
88019d6b8fSAnthony Liguori     if (!strcmp(bochs->magic, HEADER_MAGIC) &&
89019d6b8fSAnthony Liguori 	!strcmp(bochs->type, REDOLOG_TYPE) &&
90019d6b8fSAnthony Liguori 	!strcmp(bochs->subtype, GROWING_TYPE) &&
91019d6b8fSAnthony Liguori 	((le32_to_cpu(bochs->version) == HEADER_VERSION) ||
92019d6b8fSAnthony Liguori 	(le32_to_cpu(bochs->version) == HEADER_V1)))
93019d6b8fSAnthony Liguori 	return 100;
94019d6b8fSAnthony Liguori 
95019d6b8fSAnthony Liguori     return 0;
96019d6b8fSAnthony Liguori }
97019d6b8fSAnthony Liguori 
98015a1036SMax Reitz static int bochs_open(BlockDriverState *bs, QDict *options, int flags,
99015a1036SMax Reitz                       Error **errp)
100019d6b8fSAnthony Liguori {
101019d6b8fSAnthony Liguori     BDRVBochsState *s = bs->opaque;
102246f6583SKevin Wolf     uint32_t i;
103019d6b8fSAnthony Liguori     struct bochs_header bochs;
1045b7d7dfdSKevin Wolf     int ret;
105019d6b8fSAnthony Liguori 
106019d6b8fSAnthony Liguori     bs->read_only = 1; // no write support yet
107019d6b8fSAnthony Liguori 
1089a4f4c31SKevin Wolf     ret = bdrv_pread(bs->file->bs, 0, &bochs, sizeof(bochs));
1095b7d7dfdSKevin Wolf     if (ret < 0) {
1105b7d7dfdSKevin Wolf         return ret;
111019d6b8fSAnthony Liguori     }
112019d6b8fSAnthony Liguori 
113019d6b8fSAnthony Liguori     if (strcmp(bochs.magic, HEADER_MAGIC) ||
114019d6b8fSAnthony Liguori         strcmp(bochs.type, REDOLOG_TYPE) ||
115019d6b8fSAnthony Liguori         strcmp(bochs.subtype, GROWING_TYPE) ||
116019d6b8fSAnthony Liguori 	((le32_to_cpu(bochs.version) != HEADER_VERSION) &&
117019d6b8fSAnthony Liguori 	(le32_to_cpu(bochs.version) != HEADER_V1))) {
11876abe407SPaolo Bonzini         error_setg(errp, "Image not in Bochs format");
11976abe407SPaolo Bonzini         return -EINVAL;
120019d6b8fSAnthony Liguori     }
121019d6b8fSAnthony Liguori 
122019d6b8fSAnthony Liguori     if (le32_to_cpu(bochs.version) == HEADER_V1) {
1233dd8a676SKevin Wolf         bs->total_sectors = le64_to_cpu(bochs.extra.redolog_v1.disk) / 512;
124019d6b8fSAnthony Liguori     } else {
125019d6b8fSAnthony Liguori         bs->total_sectors = le64_to_cpu(bochs.extra.redolog.disk) / 512;
126019d6b8fSAnthony Liguori     }
127019d6b8fSAnthony Liguori 
128e3737b82SKevin Wolf     /* Limit to 1M entries to avoid unbounded allocation. This is what is
129e3737b82SKevin Wolf      * needed for the largest image that bximage can create (~8 TB). */
1303dd8a676SKevin Wolf     s->catalog_size = le32_to_cpu(bochs.catalog);
131e3737b82SKevin Wolf     if (s->catalog_size > 0x100000) {
132e3737b82SKevin Wolf         error_setg(errp, "Catalog size is too large");
133e3737b82SKevin Wolf         return -EFBIG;
134e3737b82SKevin Wolf     }
135e3737b82SKevin Wolf 
13602c4f26bSMarkus Armbruster     s->catalog_bitmap = g_try_new(uint32_t, s->catalog_size);
1377bf665eeSKevin Wolf     if (s->catalog_size && s->catalog_bitmap == NULL) {
1387bf665eeSKevin Wolf         error_setg(errp, "Could not allocate memory for catalog");
1397bf665eeSKevin Wolf         return -ENOMEM;
1407bf665eeSKevin Wolf     }
1415b7d7dfdSKevin Wolf 
1429a4f4c31SKevin Wolf     ret = bdrv_pread(bs->file->bs, le32_to_cpu(bochs.header), s->catalog_bitmap,
1435b7d7dfdSKevin Wolf                      s->catalog_size * 4);
1445b7d7dfdSKevin Wolf     if (ret < 0) {
145019d6b8fSAnthony Liguori         goto fail;
1465b7d7dfdSKevin Wolf     }
1475b7d7dfdSKevin Wolf 
148019d6b8fSAnthony Liguori     for (i = 0; i < s->catalog_size; i++)
149019d6b8fSAnthony Liguori 	le32_to_cpus(&s->catalog_bitmap[i]);
150019d6b8fSAnthony Liguori 
151019d6b8fSAnthony Liguori     s->data_offset = le32_to_cpu(bochs.header) + (s->catalog_size * 4);
152019d6b8fSAnthony Liguori 
1533dd8a676SKevin Wolf     s->bitmap_blocks = 1 + (le32_to_cpu(bochs.bitmap) - 1) / 512;
1543dd8a676SKevin Wolf     s->extent_blocks = 1 + (le32_to_cpu(bochs.extent) - 1) / 512;
155019d6b8fSAnthony Liguori 
1563dd8a676SKevin Wolf     s->extent_size = le32_to_cpu(bochs.extent);
157715c3f60SKevin Wolf     if (s->extent_size < BDRV_SECTOR_SIZE) {
158715c3f60SKevin Wolf         /* bximage actually never creates extents smaller than 4k */
159715c3f60SKevin Wolf         error_setg(errp, "Extent size must be at least 512");
160715c3f60SKevin Wolf         ret = -EINVAL;
161715c3f60SKevin Wolf         goto fail;
162715c3f60SKevin Wolf     } else if (!is_power_of_2(s->extent_size)) {
163715c3f60SKevin Wolf         error_setg(errp, "Extent size %" PRIu32 " is not a power of two",
164715c3f60SKevin Wolf                    s->extent_size);
16528ec11bcSKevin Wolf         ret = -EINVAL;
16628ec11bcSKevin Wolf         goto fail;
1678e53abbcSKevin Wolf     } else if (s->extent_size > 0x800000) {
1688e53abbcSKevin Wolf         error_setg(errp, "Extent size %" PRIu32 " is too large",
1698e53abbcSKevin Wolf                    s->extent_size);
17028ec11bcSKevin Wolf         ret = -EINVAL;
17128ec11bcSKevin Wolf         goto fail;
1728e53abbcSKevin Wolf     }
173019d6b8fSAnthony Liguori 
174715c3f60SKevin Wolf     if (s->catalog_size < DIV_ROUND_UP(bs->total_sectors,
175715c3f60SKevin Wolf                                        s->extent_size / BDRV_SECTOR_SIZE))
176715c3f60SKevin Wolf     {
177e3737b82SKevin Wolf         error_setg(errp, "Catalog size is too small for this disk size");
178e3737b82SKevin Wolf         ret = -EINVAL;
179e3737b82SKevin Wolf         goto fail;
180e3737b82SKevin Wolf     }
181e3737b82SKevin Wolf 
182848c66e8SPaolo Bonzini     qemu_co_mutex_init(&s->lock);
183019d6b8fSAnthony Liguori     return 0;
1845b7d7dfdSKevin Wolf 
185019d6b8fSAnthony Liguori fail:
1865b7d7dfdSKevin Wolf     g_free(s->catalog_bitmap);
1875b7d7dfdSKevin Wolf     return ret;
188019d6b8fSAnthony Liguori }
189019d6b8fSAnthony Liguori 
190efbca10fSChristoph Hellwig static int64_t seek_to_sector(BlockDriverState *bs, int64_t sector_num)
191019d6b8fSAnthony Liguori {
192019d6b8fSAnthony Liguori     BDRVBochsState *s = bs->opaque;
193246f6583SKevin Wolf     uint64_t offset = sector_num * 512;
194246f6583SKevin Wolf     uint64_t extent_index, extent_offset, bitmap_offset;
195019d6b8fSAnthony Liguori     char bitmap_entry;
196e1b42f45SMax Reitz     int ret;
197019d6b8fSAnthony Liguori 
198019d6b8fSAnthony Liguori     // seek to sector
199019d6b8fSAnthony Liguori     extent_index = offset / s->extent_size;
200019d6b8fSAnthony Liguori     extent_offset = (offset % s->extent_size) / 512;
201019d6b8fSAnthony Liguori 
202efbca10fSChristoph Hellwig     if (s->catalog_bitmap[extent_index] == 0xffffffff) {
203e1b42f45SMax Reitz 	return 0; /* not allocated */
204019d6b8fSAnthony Liguori     }
205019d6b8fSAnthony Liguori 
206a9ba36a4SKevin Wolf     bitmap_offset = s->data_offset +
207a9ba36a4SKevin Wolf         (512 * (uint64_t) s->catalog_bitmap[extent_index] *
208019d6b8fSAnthony Liguori         (s->extent_blocks + s->bitmap_blocks));
209019d6b8fSAnthony Liguori 
210efbca10fSChristoph Hellwig     /* read in bitmap for current extent */
2119a4f4c31SKevin Wolf     ret = bdrv_pread(bs->file->bs, bitmap_offset + (extent_offset / 8),
212e1b42f45SMax Reitz                      &bitmap_entry, 1);
213e1b42f45SMax Reitz     if (ret < 0) {
214e1b42f45SMax Reitz         return ret;
215ecbe1576SBlue Swirl     }
216019d6b8fSAnthony Liguori 
217efbca10fSChristoph Hellwig     if (!((bitmap_entry >> (extent_offset % 8)) & 1)) {
218e1b42f45SMax Reitz 	return 0; /* not allocated */
219019d6b8fSAnthony Liguori     }
220019d6b8fSAnthony Liguori 
221efbca10fSChristoph Hellwig     return bitmap_offset + (512 * (s->bitmap_blocks + extent_offset));
222019d6b8fSAnthony Liguori }
223019d6b8fSAnthony Liguori 
224019d6b8fSAnthony Liguori static int bochs_read(BlockDriverState *bs, int64_t sector_num,
225019d6b8fSAnthony Liguori                     uint8_t *buf, int nb_sectors)
226019d6b8fSAnthony Liguori {
227019d6b8fSAnthony Liguori     int ret;
228019d6b8fSAnthony Liguori 
229019d6b8fSAnthony Liguori     while (nb_sectors > 0) {
230efbca10fSChristoph Hellwig         int64_t block_offset = seek_to_sector(bs, sector_num);
231e1b42f45SMax Reitz         if (block_offset < 0) {
232e1b42f45SMax Reitz             return block_offset;
233e1b42f45SMax Reitz         } else if (block_offset > 0) {
2349a4f4c31SKevin Wolf             ret = bdrv_pread(bs->file->bs, block_offset, buf, 512);
235e1b42f45SMax Reitz             if (ret < 0) {
236e1b42f45SMax Reitz                 return ret;
237019d6b8fSAnthony Liguori             }
238e1b42f45SMax Reitz         } else {
239019d6b8fSAnthony Liguori             memset(buf, 0, 512);
240e1b42f45SMax Reitz         }
241019d6b8fSAnthony Liguori         nb_sectors--;
242019d6b8fSAnthony Liguori         sector_num++;
243019d6b8fSAnthony Liguori         buf += 512;
244019d6b8fSAnthony Liguori     }
245019d6b8fSAnthony Liguori     return 0;
246019d6b8fSAnthony Liguori }
247019d6b8fSAnthony Liguori 
2482914caa0SPaolo Bonzini static coroutine_fn int bochs_co_read(BlockDriverState *bs, int64_t sector_num,
2492914caa0SPaolo Bonzini                                       uint8_t *buf, int nb_sectors)
2502914caa0SPaolo Bonzini {
2512914caa0SPaolo Bonzini     int ret;
2522914caa0SPaolo Bonzini     BDRVBochsState *s = bs->opaque;
2532914caa0SPaolo Bonzini     qemu_co_mutex_lock(&s->lock);
2542914caa0SPaolo Bonzini     ret = bochs_read(bs, sector_num, buf, nb_sectors);
2552914caa0SPaolo Bonzini     qemu_co_mutex_unlock(&s->lock);
2562914caa0SPaolo Bonzini     return ret;
2572914caa0SPaolo Bonzini }
2582914caa0SPaolo Bonzini 
259019d6b8fSAnthony Liguori static void bochs_close(BlockDriverState *bs)
260019d6b8fSAnthony Liguori {
261019d6b8fSAnthony Liguori     BDRVBochsState *s = bs->opaque;
2627267c094SAnthony Liguori     g_free(s->catalog_bitmap);
263019d6b8fSAnthony Liguori }
264019d6b8fSAnthony Liguori 
265019d6b8fSAnthony Liguori static BlockDriver bdrv_bochs = {
266019d6b8fSAnthony Liguori     .format_name	= "bochs",
267019d6b8fSAnthony Liguori     .instance_size	= sizeof(BDRVBochsState),
268019d6b8fSAnthony Liguori     .bdrv_probe		= bochs_probe,
2697a6f3913SChristoph Hellwig     .bdrv_open		= bochs_open,
2702914caa0SPaolo Bonzini     .bdrv_read          = bochs_co_read,
271019d6b8fSAnthony Liguori     .bdrv_close		= bochs_close,
272019d6b8fSAnthony Liguori };
273019d6b8fSAnthony Liguori 
274019d6b8fSAnthony Liguori static void bdrv_bochs_init(void)
275019d6b8fSAnthony Liguori {
276019d6b8fSAnthony Liguori     bdrv_register(&bdrv_bochs);
277019d6b8fSAnthony Liguori }
278019d6b8fSAnthony Liguori 
279019d6b8fSAnthony Liguori block_init(bdrv_bochs_init);
280