Statistics
| Branch: | Revision:

root / block / bochs.c @ b4160af1

History | View | Annotate | Download (7.1 kB)

1 a8753c34 bellard
/*
2 a8753c34 bellard
 * Block driver for the various disk image formats used by Bochs
3 a8753c34 bellard
 * Currently only for "growing" type in read-only mode
4 5fafdf24 ths
 *
5 a8753c34 bellard
 * Copyright (c) 2005 Alex Beregszaszi
6 5fafdf24 ths
 *
7 a8753c34 bellard
 * Permission is hereby granted, free of charge, to any person obtaining a copy
8 a8753c34 bellard
 * of this software and associated documentation files (the "Software"), to deal
9 a8753c34 bellard
 * in the Software without restriction, including without limitation the rights
10 a8753c34 bellard
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 a8753c34 bellard
 * copies of the Software, and to permit persons to whom the Software is
12 a8753c34 bellard
 * furnished to do so, subject to the following conditions:
13 a8753c34 bellard
 *
14 a8753c34 bellard
 * The above copyright notice and this permission notice shall be included in
15 a8753c34 bellard
 * all copies or substantial portions of the Software.
16 a8753c34 bellard
 *
17 a8753c34 bellard
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 a8753c34 bellard
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 a8753c34 bellard
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 a8753c34 bellard
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 a8753c34 bellard
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 a8753c34 bellard
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23 a8753c34 bellard
 * THE SOFTWARE.
24 a8753c34 bellard
 */
25 faf07963 pbrook
#include "qemu-common.h"
26 737e150e Paolo Bonzini
#include "block/block_int.h"
27 1de7afc9 Paolo Bonzini
#include "qemu/module.h"
28 a8753c34 bellard
29 a8753c34 bellard
/**************************************************************/
30 a8753c34 bellard
31 a8753c34 bellard
#define HEADER_MAGIC "Bochs Virtual HD Image"
32 6850dd94 ths
#define HEADER_VERSION 0x00020000
33 6850dd94 ths
#define HEADER_V1 0x00010000
34 a8753c34 bellard
#define HEADER_SIZE 512
35 a8753c34 bellard
36 a8753c34 bellard
#define REDOLOG_TYPE "Redolog"
37 a8753c34 bellard
#define GROWING_TYPE "Growing"
38 a8753c34 bellard
39 a8753c34 bellard
// not allocated: 0xffffffff
40 a8753c34 bellard
41 a8753c34 bellard
// always little-endian
42 6850dd94 ths
struct bochs_header_v1 {
43 a8753c34 bellard
    char magic[32]; // "Bochs Virtual HD Image"
44 a8753c34 bellard
    char type[16]; // "Redolog"
45 a8753c34 bellard
    char subtype[16]; // "Undoable" / "Volatile" / "Growing"
46 a8753c34 bellard
    uint32_t version;
47 a8753c34 bellard
    uint32_t header; // size of header
48 3b46e624 ths
49 a8753c34 bellard
    union {
50 a8753c34 bellard
        struct {
51 a8753c34 bellard
            uint32_t catalog; // num of entries
52 a8753c34 bellard
            uint32_t bitmap; // bitmap size
53 a8753c34 bellard
            uint32_t extent; // extent size
54 a8753c34 bellard
            uint64_t disk; // disk size
55 a8753c34 bellard
            char padding[HEADER_SIZE - 64 - 8 - 20];
56 a8753c34 bellard
        } redolog;
57 a8753c34 bellard
        char padding[HEADER_SIZE - 64 - 8];
58 a8753c34 bellard
    } extra;
59 a8753c34 bellard
};
60 a8753c34 bellard
61 6850dd94 ths
// always little-endian
62 6850dd94 ths
struct bochs_header {
63 6850dd94 ths
    char magic[32]; // "Bochs Virtual HD Image"
64 6850dd94 ths
    char type[16]; // "Redolog"
65 6850dd94 ths
    char subtype[16]; // "Undoable" / "Volatile" / "Growing"
66 6850dd94 ths
    uint32_t version;
67 6850dd94 ths
    uint32_t header; // size of header
68 3b46e624 ths
69 6850dd94 ths
    union {
70 6850dd94 ths
        struct {
71 6850dd94 ths
            uint32_t catalog; // num of entries
72 6850dd94 ths
            uint32_t bitmap; // bitmap size
73 6850dd94 ths
            uint32_t extent; // extent size
74 6850dd94 ths
            uint32_t reserved; // for ???
75 6850dd94 ths
            uint64_t disk; // disk size
76 6850dd94 ths
            char padding[HEADER_SIZE - 64 - 8 - 24];
77 6850dd94 ths
        } redolog;
78 6850dd94 ths
        char padding[HEADER_SIZE - 64 - 8];
79 6850dd94 ths
    } extra;
80 6850dd94 ths
};
81 6850dd94 ths
82 a8753c34 bellard
typedef struct BDRVBochsState {
83 848c66e8 Paolo Bonzini
    CoMutex lock;
84 a8753c34 bellard
    uint32_t *catalog_bitmap;
85 a8753c34 bellard
    int catalog_size;
86 3b46e624 ths
87 a8753c34 bellard
    int data_offset;
88 3b46e624 ths
89 a8753c34 bellard
    int bitmap_blocks;
90 a8753c34 bellard
    int extent_blocks;
91 a8753c34 bellard
    int extent_size;
92 a8753c34 bellard
} BDRVBochsState;
93 a8753c34 bellard
94 a8753c34 bellard
static int bochs_probe(const uint8_t *buf, int buf_size, const char *filename)
95 a8753c34 bellard
{
96 a8753c34 bellard
    const struct bochs_header *bochs = (const void *)buf;
97 3b46e624 ths
98 a8753c34 bellard
    if (buf_size < HEADER_SIZE)
99 a8753c34 bellard
        return 0;
100 a8753c34 bellard
101 a8753c34 bellard
    if (!strcmp(bochs->magic, HEADER_MAGIC) &&
102 a8753c34 bellard
        !strcmp(bochs->type, REDOLOG_TYPE) &&
103 a8753c34 bellard
        !strcmp(bochs->subtype, GROWING_TYPE) &&
104 6850dd94 ths
        ((le32_to_cpu(bochs->version) == HEADER_VERSION) ||
105 6850dd94 ths
        (le32_to_cpu(bochs->version) == HEADER_V1)))
106 a8753c34 bellard
        return 100;
107 a8753c34 bellard
108 a8753c34 bellard
    return 0;
109 a8753c34 bellard
}
110 a8753c34 bellard
111 015a1036 Max Reitz
static int bochs_open(BlockDriverState *bs, QDict *options, int flags,
112 015a1036 Max Reitz
                      Error **errp)
113 a8753c34 bellard
{
114 a8753c34 bellard
    BDRVBochsState *s = bs->opaque;
115 7a6f3913 Christoph Hellwig
    int i;
116 a8753c34 bellard
    struct bochs_header bochs;
117 6850dd94 ths
    struct bochs_header_v1 header_v1;
118 5b7d7dfd Kevin Wolf
    int ret;
119 a8753c34 bellard
120 a8753c34 bellard
    bs->read_only = 1; // no write support yet
121 3b46e624 ths
122 5b7d7dfd Kevin Wolf
    ret = bdrv_pread(bs->file, 0, &bochs, sizeof(bochs));
123 5b7d7dfd Kevin Wolf
    if (ret < 0) {
124 5b7d7dfd Kevin Wolf
        return ret;
125 a8753c34 bellard
    }
126 a8753c34 bellard
127 a8753c34 bellard
    if (strcmp(bochs.magic, HEADER_MAGIC) ||
128 a8753c34 bellard
        strcmp(bochs.type, REDOLOG_TYPE) ||
129 a8753c34 bellard
        strcmp(bochs.subtype, GROWING_TYPE) ||
130 6850dd94 ths
        ((le32_to_cpu(bochs.version) != HEADER_VERSION) &&
131 6850dd94 ths
        (le32_to_cpu(bochs.version) != HEADER_V1))) {
132 15bac0d5 Stefan Weil
        return -EMEDIUMTYPE;
133 a8753c34 bellard
    }
134 a8753c34 bellard
135 6850dd94 ths
    if (le32_to_cpu(bochs.version) == HEADER_V1) {
136 6850dd94 ths
      memcpy(&header_v1, &bochs, sizeof(bochs));
137 6850dd94 ths
      bs->total_sectors = le64_to_cpu(header_v1.extra.redolog.disk) / 512;
138 6850dd94 ths
    } else {
139 6850dd94 ths
      bs->total_sectors = le64_to_cpu(bochs.extra.redolog.disk) / 512;
140 6850dd94 ths
    }
141 a8753c34 bellard
142 a8753c34 bellard
    s->catalog_size = le32_to_cpu(bochs.extra.redolog.catalog);
143 7267c094 Anthony Liguori
    s->catalog_bitmap = g_malloc(s->catalog_size * 4);
144 5b7d7dfd Kevin Wolf
145 5b7d7dfd Kevin Wolf
    ret = bdrv_pread(bs->file, le32_to_cpu(bochs.header), s->catalog_bitmap,
146 5b7d7dfd Kevin Wolf
                     s->catalog_size * 4);
147 5b7d7dfd Kevin Wolf
    if (ret < 0) {
148 5b7d7dfd Kevin Wolf
        goto fail;
149 5b7d7dfd Kevin Wolf
    }
150 5b7d7dfd Kevin Wolf
151 a8753c34 bellard
    for (i = 0; i < s->catalog_size; i++)
152 a8753c34 bellard
        le32_to_cpus(&s->catalog_bitmap[i]);
153 a8753c34 bellard
154 a8753c34 bellard
    s->data_offset = le32_to_cpu(bochs.header) + (s->catalog_size * 4);
155 a8753c34 bellard
156 a8753c34 bellard
    s->bitmap_blocks = 1 + (le32_to_cpu(bochs.extra.redolog.bitmap) - 1) / 512;
157 a8753c34 bellard
    s->extent_blocks = 1 + (le32_to_cpu(bochs.extra.redolog.extent) - 1) / 512;
158 3b46e624 ths
159 a8753c34 bellard
    s->extent_size = le32_to_cpu(bochs.extra.redolog.extent);
160 a8753c34 bellard
161 848c66e8 Paolo Bonzini
    qemu_co_mutex_init(&s->lock);
162 a8753c34 bellard
    return 0;
163 5b7d7dfd Kevin Wolf
164 5b7d7dfd Kevin Wolf
fail:
165 5b7d7dfd Kevin Wolf
    g_free(s->catalog_bitmap);
166 5b7d7dfd Kevin Wolf
    return ret;
167 a8753c34 bellard
}
168 a8753c34 bellard
169 efbca10f Christoph Hellwig
static int64_t seek_to_sector(BlockDriverState *bs, int64_t sector_num)
170 a8753c34 bellard
{
171 a8753c34 bellard
    BDRVBochsState *s = bs->opaque;
172 a8753c34 bellard
    int64_t offset = sector_num * 512;
173 efbca10f Christoph Hellwig
    int64_t extent_index, extent_offset, bitmap_offset;
174 a8753c34 bellard
    char bitmap_entry;
175 a8753c34 bellard
176 a8753c34 bellard
    // seek to sector
177 a8753c34 bellard
    extent_index = offset / s->extent_size;
178 a8753c34 bellard
    extent_offset = (offset % s->extent_size) / 512;
179 3b46e624 ths
180 efbca10f Christoph Hellwig
    if (s->catalog_bitmap[extent_index] == 0xffffffff) {
181 efbca10f Christoph Hellwig
        return -1; /* not allocated */
182 a8753c34 bellard
    }
183 a8753c34 bellard
184 a8753c34 bellard
    bitmap_offset = s->data_offset + (512 * s->catalog_bitmap[extent_index] *
185 a8753c34 bellard
        (s->extent_blocks + s->bitmap_blocks));
186 3b46e624 ths
187 efbca10f Christoph Hellwig
    /* read in bitmap for current extent */
188 7a6f3913 Christoph Hellwig
    if (bdrv_pread(bs->file, bitmap_offset + (extent_offset / 8),
189 7a6f3913 Christoph Hellwig
                   &bitmap_entry, 1) != 1) {
190 00ccf932 Kirill A. Shutemov
        return -1;
191 a8753c34 bellard
    }
192 a8753c34 bellard
193 efbca10f Christoph Hellwig
    if (!((bitmap_entry >> (extent_offset % 8)) & 1)) {
194 efbca10f Christoph Hellwig
        return -1; /* not allocated */
195 ecbe1576 Blue Swirl
    }
196 3b46e624 ths
197 efbca10f Christoph Hellwig
    return bitmap_offset + (512 * (s->bitmap_blocks + extent_offset));
198 a8753c34 bellard
}
199 a8753c34 bellard
200 5fafdf24 ths
static int bochs_read(BlockDriverState *bs, int64_t sector_num,
201 a8753c34 bellard
                    uint8_t *buf, int nb_sectors)
202 a8753c34 bellard
{
203 a8753c34 bellard
    int ret;
204 a8753c34 bellard
205 a8753c34 bellard
    while (nb_sectors > 0) {
206 efbca10f Christoph Hellwig
        int64_t block_offset = seek_to_sector(bs, sector_num);
207 efbca10f Christoph Hellwig
        if (block_offset >= 0) {
208 7a6f3913 Christoph Hellwig
            ret = bdrv_pread(bs->file, block_offset, buf, 512);
209 efbca10f Christoph Hellwig
            if (ret != 512) {
210 efbca10f Christoph Hellwig
                return -1;
211 efbca10f Christoph Hellwig
            }
212 efbca10f Christoph Hellwig
        } else
213 a8753c34 bellard
            memset(buf, 0, 512);
214 a8753c34 bellard
        nb_sectors--;
215 a8753c34 bellard
        sector_num++;
216 a8753c34 bellard
        buf += 512;
217 a8753c34 bellard
    }
218 a8753c34 bellard
    return 0;
219 a8753c34 bellard
}
220 a8753c34 bellard
221 2914caa0 Paolo Bonzini
static coroutine_fn int bochs_co_read(BlockDriverState *bs, int64_t sector_num,
222 2914caa0 Paolo Bonzini
                                      uint8_t *buf, int nb_sectors)
223 2914caa0 Paolo Bonzini
{
224 2914caa0 Paolo Bonzini
    int ret;
225 2914caa0 Paolo Bonzini
    BDRVBochsState *s = bs->opaque;
226 2914caa0 Paolo Bonzini
    qemu_co_mutex_lock(&s->lock);
227 2914caa0 Paolo Bonzini
    ret = bochs_read(bs, sector_num, buf, nb_sectors);
228 2914caa0 Paolo Bonzini
    qemu_co_mutex_unlock(&s->lock);
229 2914caa0 Paolo Bonzini
    return ret;
230 2914caa0 Paolo Bonzini
}
231 2914caa0 Paolo Bonzini
232 a8753c34 bellard
static void bochs_close(BlockDriverState *bs)
233 a8753c34 bellard
{
234 a8753c34 bellard
    BDRVBochsState *s = bs->opaque;
235 7267c094 Anthony Liguori
    g_free(s->catalog_bitmap);
236 a8753c34 bellard
}
237 a8753c34 bellard
238 5efa9d5a Anthony Liguori
static BlockDriver bdrv_bochs = {
239 e60f469c aurel32
    .format_name        = "bochs",
240 e60f469c aurel32
    .instance_size        = sizeof(BDRVBochsState),
241 e60f469c aurel32
    .bdrv_probe                = bochs_probe,
242 7a6f3913 Christoph Hellwig
    .bdrv_open                = bochs_open,
243 2914caa0 Paolo Bonzini
    .bdrv_read          = bochs_co_read,
244 e60f469c aurel32
    .bdrv_close                = bochs_close,
245 a8753c34 bellard
};
246 5efa9d5a Anthony Liguori
247 5efa9d5a Anthony Liguori
static void bdrv_bochs_init(void)
248 5efa9d5a Anthony Liguori
{
249 5efa9d5a Anthony Liguori
    bdrv_register(&bdrv_bochs);
250 5efa9d5a Anthony Liguori
}
251 5efa9d5a Anthony Liguori
252 5efa9d5a Anthony Liguori
block_init(bdrv_bochs_init);