Statistics
| Branch: | Revision:

root / block / bochs.c @ a74cdab4

History | View | Annotate | Download (6.6 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 a8753c34 bellard
#include "block_int.h"
27 5efa9d5a Anthony Liguori
#include "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 a8753c34 bellard
    uint32_t *catalog_bitmap;
84 a8753c34 bellard
    int catalog_size;
85 3b46e624 ths
86 a8753c34 bellard
    int data_offset;
87 3b46e624 ths
88 a8753c34 bellard
    int bitmap_blocks;
89 a8753c34 bellard
    int extent_blocks;
90 a8753c34 bellard
    int extent_size;
91 a8753c34 bellard
} BDRVBochsState;
92 a8753c34 bellard
93 a8753c34 bellard
static int bochs_probe(const uint8_t *buf, int buf_size, const char *filename)
94 a8753c34 bellard
{
95 a8753c34 bellard
    const struct bochs_header *bochs = (const void *)buf;
96 3b46e624 ths
97 a8753c34 bellard
    if (buf_size < HEADER_SIZE)
98 a8753c34 bellard
        return 0;
99 a8753c34 bellard
100 a8753c34 bellard
    if (!strcmp(bochs->magic, HEADER_MAGIC) &&
101 a8753c34 bellard
        !strcmp(bochs->type, REDOLOG_TYPE) &&
102 a8753c34 bellard
        !strcmp(bochs->subtype, GROWING_TYPE) &&
103 6850dd94 ths
        ((le32_to_cpu(bochs->version) == HEADER_VERSION) ||
104 6850dd94 ths
        (le32_to_cpu(bochs->version) == HEADER_V1)))
105 a8753c34 bellard
        return 100;
106 a8753c34 bellard
107 a8753c34 bellard
    return 0;
108 a8753c34 bellard
}
109 a8753c34 bellard
110 7a6f3913 Christoph Hellwig
static int bochs_open(BlockDriverState *bs, int flags)
111 a8753c34 bellard
{
112 a8753c34 bellard
    BDRVBochsState *s = bs->opaque;
113 7a6f3913 Christoph Hellwig
    int i;
114 a8753c34 bellard
    struct bochs_header bochs;
115 6850dd94 ths
    struct bochs_header_v1 header_v1;
116 a8753c34 bellard
117 a8753c34 bellard
    bs->read_only = 1; // no write support yet
118 3b46e624 ths
119 7a6f3913 Christoph Hellwig
    if (bdrv_pread(bs->file, 0, &bochs, sizeof(bochs)) != sizeof(bochs)) {
120 a8753c34 bellard
        goto fail;
121 a8753c34 bellard
    }
122 a8753c34 bellard
123 a8753c34 bellard
    if (strcmp(bochs.magic, HEADER_MAGIC) ||
124 a8753c34 bellard
        strcmp(bochs.type, REDOLOG_TYPE) ||
125 a8753c34 bellard
        strcmp(bochs.subtype, GROWING_TYPE) ||
126 6850dd94 ths
        ((le32_to_cpu(bochs.version) != HEADER_VERSION) &&
127 6850dd94 ths
        (le32_to_cpu(bochs.version) != HEADER_V1))) {
128 a8753c34 bellard
        goto fail;
129 a8753c34 bellard
    }
130 a8753c34 bellard
131 6850dd94 ths
    if (le32_to_cpu(bochs.version) == HEADER_V1) {
132 6850dd94 ths
      memcpy(&header_v1, &bochs, sizeof(bochs));
133 6850dd94 ths
      bs->total_sectors = le64_to_cpu(header_v1.extra.redolog.disk) / 512;
134 6850dd94 ths
    } else {
135 6850dd94 ths
      bs->total_sectors = le64_to_cpu(bochs.extra.redolog.disk) / 512;
136 6850dd94 ths
    }
137 a8753c34 bellard
138 a8753c34 bellard
    s->catalog_size = le32_to_cpu(bochs.extra.redolog.catalog);
139 a8753c34 bellard
    s->catalog_bitmap = qemu_malloc(s->catalog_size * 4);
140 7a6f3913 Christoph Hellwig
    if (bdrv_pread(bs->file, le32_to_cpu(bochs.header), s->catalog_bitmap,
141 7a6f3913 Christoph Hellwig
                   s->catalog_size * 4) != s->catalog_size * 4)
142 a8753c34 bellard
        goto fail;
143 a8753c34 bellard
    for (i = 0; i < s->catalog_size; i++)
144 a8753c34 bellard
        le32_to_cpus(&s->catalog_bitmap[i]);
145 a8753c34 bellard
146 a8753c34 bellard
    s->data_offset = le32_to_cpu(bochs.header) + (s->catalog_size * 4);
147 a8753c34 bellard
148 a8753c34 bellard
    s->bitmap_blocks = 1 + (le32_to_cpu(bochs.extra.redolog.bitmap) - 1) / 512;
149 a8753c34 bellard
    s->extent_blocks = 1 + (le32_to_cpu(bochs.extra.redolog.extent) - 1) / 512;
150 3b46e624 ths
151 a8753c34 bellard
    s->extent_size = le32_to_cpu(bochs.extra.redolog.extent);
152 a8753c34 bellard
153 a8753c34 bellard
    return 0;
154 a8753c34 bellard
 fail:
155 a8753c34 bellard
    return -1;
156 a8753c34 bellard
}
157 a8753c34 bellard
158 efbca10f Christoph Hellwig
static int64_t seek_to_sector(BlockDriverState *bs, int64_t sector_num)
159 a8753c34 bellard
{
160 a8753c34 bellard
    BDRVBochsState *s = bs->opaque;
161 a8753c34 bellard
    int64_t offset = sector_num * 512;
162 efbca10f Christoph Hellwig
    int64_t extent_index, extent_offset, bitmap_offset;
163 a8753c34 bellard
    char bitmap_entry;
164 a8753c34 bellard
165 a8753c34 bellard
    // seek to sector
166 a8753c34 bellard
    extent_index = offset / s->extent_size;
167 a8753c34 bellard
    extent_offset = (offset % s->extent_size) / 512;
168 3b46e624 ths
169 efbca10f Christoph Hellwig
    if (s->catalog_bitmap[extent_index] == 0xffffffff) {
170 efbca10f Christoph Hellwig
        return -1; /* not allocated */
171 a8753c34 bellard
    }
172 a8753c34 bellard
173 a8753c34 bellard
    bitmap_offset = s->data_offset + (512 * s->catalog_bitmap[extent_index] *
174 a8753c34 bellard
        (s->extent_blocks + s->bitmap_blocks));
175 3b46e624 ths
176 efbca10f Christoph Hellwig
    /* read in bitmap for current extent */
177 7a6f3913 Christoph Hellwig
    if (bdrv_pread(bs->file, bitmap_offset + (extent_offset / 8),
178 7a6f3913 Christoph Hellwig
                   &bitmap_entry, 1) != 1) {
179 00ccf932 Kirill A. Shutemov
        return -1;
180 a8753c34 bellard
    }
181 a8753c34 bellard
182 efbca10f Christoph Hellwig
    if (!((bitmap_entry >> (extent_offset % 8)) & 1)) {
183 efbca10f Christoph Hellwig
        return -1; /* not allocated */
184 ecbe1576 Blue Swirl
    }
185 3b46e624 ths
186 efbca10f Christoph Hellwig
    return bitmap_offset + (512 * (s->bitmap_blocks + extent_offset));
187 a8753c34 bellard
}
188 a8753c34 bellard
189 5fafdf24 ths
static int bochs_read(BlockDriverState *bs, int64_t sector_num,
190 a8753c34 bellard
                    uint8_t *buf, int nb_sectors)
191 a8753c34 bellard
{
192 a8753c34 bellard
    int ret;
193 a8753c34 bellard
194 a8753c34 bellard
    while (nb_sectors > 0) {
195 efbca10f Christoph Hellwig
        int64_t block_offset = seek_to_sector(bs, sector_num);
196 efbca10f Christoph Hellwig
        if (block_offset >= 0) {
197 7a6f3913 Christoph Hellwig
            ret = bdrv_pread(bs->file, block_offset, buf, 512);
198 efbca10f Christoph Hellwig
            if (ret != 512) {
199 efbca10f Christoph Hellwig
                return -1;
200 efbca10f Christoph Hellwig
            }
201 efbca10f Christoph Hellwig
        } else
202 a8753c34 bellard
            memset(buf, 0, 512);
203 a8753c34 bellard
        nb_sectors--;
204 a8753c34 bellard
        sector_num++;
205 a8753c34 bellard
        buf += 512;
206 a8753c34 bellard
    }
207 a8753c34 bellard
    return 0;
208 a8753c34 bellard
}
209 a8753c34 bellard
210 a8753c34 bellard
static void bochs_close(BlockDriverState *bs)
211 a8753c34 bellard
{
212 a8753c34 bellard
    BDRVBochsState *s = bs->opaque;
213 a8753c34 bellard
    qemu_free(s->catalog_bitmap);
214 a8753c34 bellard
}
215 a8753c34 bellard
216 5efa9d5a Anthony Liguori
static BlockDriver bdrv_bochs = {
217 e60f469c aurel32
    .format_name        = "bochs",
218 e60f469c aurel32
    .instance_size        = sizeof(BDRVBochsState),
219 e60f469c aurel32
    .bdrv_probe                = bochs_probe,
220 7a6f3913 Christoph Hellwig
    .bdrv_open                = bochs_open,
221 e60f469c aurel32
    .bdrv_read                = bochs_read,
222 e60f469c aurel32
    .bdrv_close                = bochs_close,
223 a8753c34 bellard
};
224 5efa9d5a Anthony Liguori
225 5efa9d5a Anthony Liguori
static void bdrv_bochs_init(void)
226 5efa9d5a Anthony Liguori
{
227 5efa9d5a Anthony Liguori
    bdrv_register(&bdrv_bochs);
228 5efa9d5a Anthony Liguori
}
229 5efa9d5a Anthony Liguori
230 5efa9d5a Anthony Liguori
block_init(bdrv_bochs_init);