Statistics
| Branch: | Revision:

root / block / bochs.c @ 4491e0f3

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