root / block-bochs.c @ f7350b47
History | View | Annotate | Download (7 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 | a8753c34 | bellard | |
28 | a8753c34 | bellard | /**************************************************************/
|
29 | a8753c34 | bellard | |
30 | a8753c34 | bellard | #define HEADER_MAGIC "Bochs Virtual HD Image" |
31 | 6850dd94 | ths | #define HEADER_VERSION 0x00020000 |
32 | 6850dd94 | ths | #define HEADER_V1 0x00010000 |
33 | a8753c34 | bellard | #define HEADER_SIZE 512 |
34 | a8753c34 | bellard | |
35 | a8753c34 | bellard | #define REDOLOG_TYPE "Redolog" |
36 | a8753c34 | bellard | #define GROWING_TYPE "Growing" |
37 | a8753c34 | bellard | |
38 | a8753c34 | bellard | // not allocated: 0xffffffff
|
39 | a8753c34 | bellard | |
40 | a8753c34 | bellard | // always little-endian
|
41 | 6850dd94 | ths | struct bochs_header_v1 {
|
42 | a8753c34 | bellard | char magic[32]; // "Bochs Virtual HD Image" |
43 | a8753c34 | bellard | char type[16]; // "Redolog" |
44 | a8753c34 | bellard | char subtype[16]; // "Undoable" / "Volatile" / "Growing" |
45 | a8753c34 | bellard | uint32_t version; |
46 | a8753c34 | bellard | uint32_t header; // size of header
|
47 | 3b46e624 | ths | |
48 | a8753c34 | bellard | union {
|
49 | a8753c34 | bellard | struct {
|
50 | a8753c34 | bellard | uint32_t catalog; // num of entries
|
51 | a8753c34 | bellard | uint32_t bitmap; // bitmap size
|
52 | a8753c34 | bellard | uint32_t extent; // extent size
|
53 | a8753c34 | bellard | uint64_t disk; // disk size
|
54 | a8753c34 | bellard | char padding[HEADER_SIZE - 64 - 8 - 20]; |
55 | a8753c34 | bellard | } redolog; |
56 | a8753c34 | bellard | char padding[HEADER_SIZE - 64 - 8]; |
57 | a8753c34 | bellard | } extra; |
58 | a8753c34 | bellard | }; |
59 | a8753c34 | bellard | |
60 | 6850dd94 | ths | // always little-endian
|
61 | 6850dd94 | ths | struct bochs_header {
|
62 | 6850dd94 | ths | char magic[32]; // "Bochs Virtual HD Image" |
63 | 6850dd94 | ths | char type[16]; // "Redolog" |
64 | 6850dd94 | ths | char subtype[16]; // "Undoable" / "Volatile" / "Growing" |
65 | 6850dd94 | ths | uint32_t version; |
66 | 6850dd94 | ths | uint32_t header; // size of header
|
67 | 3b46e624 | ths | |
68 | 6850dd94 | ths | union {
|
69 | 6850dd94 | ths | struct {
|
70 | 6850dd94 | ths | uint32_t catalog; // num of entries
|
71 | 6850dd94 | ths | uint32_t bitmap; // bitmap size
|
72 | 6850dd94 | ths | uint32_t extent; // extent size
|
73 | 6850dd94 | ths | uint32_t reserved; // for ???
|
74 | 6850dd94 | ths | uint64_t disk; // disk size
|
75 | 6850dd94 | ths | char padding[HEADER_SIZE - 64 - 8 - 24]; |
76 | 6850dd94 | ths | } redolog; |
77 | 6850dd94 | ths | char padding[HEADER_SIZE - 64 - 8]; |
78 | 6850dd94 | ths | } extra; |
79 | 6850dd94 | ths | }; |
80 | 6850dd94 | ths | |
81 | a8753c34 | bellard | typedef struct BDRVBochsState { |
82 | a8753c34 | bellard | int fd;
|
83 | a8753c34 | bellard | |
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 | 83f64091 | bellard | static int bochs_open(BlockDriverState *bs, const char *filename, int flags) |
112 | a8753c34 | bellard | { |
113 | a8753c34 | bellard | BDRVBochsState *s = bs->opaque; |
114 | a8753c34 | bellard | int fd, i;
|
115 | a8753c34 | bellard | struct bochs_header bochs;
|
116 | 6850dd94 | ths | struct bochs_header_v1 header_v1;
|
117 | a8753c34 | bellard | |
118 | 83f64091 | bellard | fd = open(filename, O_RDWR | O_BINARY); |
119 | a8753c34 | bellard | if (fd < 0) { |
120 | 83f64091 | bellard | fd = open(filename, O_RDONLY | O_BINARY); |
121 | a8753c34 | bellard | if (fd < 0) |
122 | a8753c34 | bellard | return -1; |
123 | a8753c34 | bellard | } |
124 | 3b46e624 | ths | |
125 | a8753c34 | bellard | bs->read_only = 1; // no write support yet |
126 | 3b46e624 | ths | |
127 | a8753c34 | bellard | s->fd = fd; |
128 | a8753c34 | bellard | |
129 | a8753c34 | bellard | if (read(fd, &bochs, sizeof(bochs)) != sizeof(bochs)) { |
130 | a8753c34 | bellard | goto fail;
|
131 | a8753c34 | bellard | } |
132 | a8753c34 | bellard | |
133 | a8753c34 | bellard | if (strcmp(bochs.magic, HEADER_MAGIC) ||
|
134 | a8753c34 | bellard | strcmp(bochs.type, REDOLOG_TYPE) || |
135 | a8753c34 | bellard | strcmp(bochs.subtype, GROWING_TYPE) || |
136 | 6850dd94 | ths | ((le32_to_cpu(bochs.version) != HEADER_VERSION) && |
137 | 6850dd94 | ths | (le32_to_cpu(bochs.version) != HEADER_V1))) { |
138 | a8753c34 | bellard | goto fail;
|
139 | a8753c34 | bellard | } |
140 | a8753c34 | bellard | |
141 | 6850dd94 | ths | if (le32_to_cpu(bochs.version) == HEADER_V1) {
|
142 | 6850dd94 | ths | memcpy(&header_v1, &bochs, sizeof(bochs));
|
143 | 6850dd94 | ths | bs->total_sectors = le64_to_cpu(header_v1.extra.redolog.disk) / 512;
|
144 | 6850dd94 | ths | } else {
|
145 | 6850dd94 | ths | bs->total_sectors = le64_to_cpu(bochs.extra.redolog.disk) / 512;
|
146 | 6850dd94 | ths | } |
147 | a8753c34 | bellard | |
148 | a8753c34 | bellard | lseek(s->fd, le32_to_cpu(bochs.header), SEEK_SET); |
149 | a8753c34 | bellard | |
150 | a8753c34 | bellard | s->catalog_size = le32_to_cpu(bochs.extra.redolog.catalog); |
151 | a8753c34 | bellard | s->catalog_bitmap = qemu_malloc(s->catalog_size * 4);
|
152 | a8753c34 | bellard | if (!s->catalog_bitmap)
|
153 | a8753c34 | bellard | goto fail;
|
154 | a8753c34 | bellard | if (read(s->fd, s->catalog_bitmap, s->catalog_size * 4) != |
155 | a8753c34 | bellard | s->catalog_size * 4)
|
156 | a8753c34 | bellard | goto fail;
|
157 | a8753c34 | bellard | for (i = 0; i < s->catalog_size; i++) |
158 | a8753c34 | bellard | le32_to_cpus(&s->catalog_bitmap[i]); |
159 | a8753c34 | bellard | |
160 | a8753c34 | bellard | s->data_offset = le32_to_cpu(bochs.header) + (s->catalog_size * 4);
|
161 | a8753c34 | bellard | |
162 | a8753c34 | bellard | s->bitmap_blocks = 1 + (le32_to_cpu(bochs.extra.redolog.bitmap) - 1) / 512; |
163 | a8753c34 | bellard | s->extent_blocks = 1 + (le32_to_cpu(bochs.extra.redolog.extent) - 1) / 512; |
164 | 3b46e624 | ths | |
165 | a8753c34 | bellard | s->extent_size = le32_to_cpu(bochs.extra.redolog.extent); |
166 | a8753c34 | bellard | |
167 | a8753c34 | bellard | return 0; |
168 | a8753c34 | bellard | fail:
|
169 | a8753c34 | bellard | close(fd); |
170 | a8753c34 | bellard | return -1; |
171 | a8753c34 | bellard | } |
172 | a8753c34 | bellard | |
173 | a8753c34 | bellard | static inline int seek_to_sector(BlockDriverState *bs, int64_t sector_num) |
174 | a8753c34 | bellard | { |
175 | a8753c34 | bellard | BDRVBochsState *s = bs->opaque; |
176 | a8753c34 | bellard | int64_t offset = sector_num * 512;
|
177 | a8753c34 | bellard | int64_t extent_index, extent_offset, bitmap_offset, block_offset; |
178 | a8753c34 | bellard | char bitmap_entry;
|
179 | a8753c34 | bellard | |
180 | a8753c34 | bellard | // seek to sector
|
181 | a8753c34 | bellard | extent_index = offset / s->extent_size; |
182 | a8753c34 | bellard | extent_offset = (offset % s->extent_size) / 512;
|
183 | 3b46e624 | ths | |
184 | a8753c34 | bellard | if (s->catalog_bitmap[extent_index] == 0xffffffff) |
185 | a8753c34 | bellard | { |
186 | a8753c34 | bellard | // fprintf(stderr, "page not allocated [%x - %x:%x]\n",
|
187 | a8753c34 | bellard | // sector_num, extent_index, extent_offset);
|
188 | a8753c34 | bellard | return -1; // not allocated |
189 | a8753c34 | bellard | } |
190 | a8753c34 | bellard | |
191 | a8753c34 | bellard | bitmap_offset = s->data_offset + (512 * s->catalog_bitmap[extent_index] *
|
192 | a8753c34 | bellard | (s->extent_blocks + s->bitmap_blocks)); |
193 | a8753c34 | bellard | block_offset = bitmap_offset + (512 * (s->bitmap_blocks + extent_offset));
|
194 | 3b46e624 | ths | |
195 | a8753c34 | bellard | // fprintf(stderr, "sect: %x [ext i: %x o: %x] -> %x bitmap: %x block: %x\n",
|
196 | a8753c34 | bellard | // sector_num, extent_index, extent_offset,
|
197 | a8753c34 | bellard | // le32_to_cpu(s->catalog_bitmap[extent_index]),
|
198 | a8753c34 | bellard | // bitmap_offset, block_offset);
|
199 | 3b46e624 | ths | |
200 | a8753c34 | bellard | // read in bitmap for current extent
|
201 | a8753c34 | bellard | lseek(s->fd, bitmap_offset + (extent_offset / 8), SEEK_SET);
|
202 | 3b46e624 | ths | |
203 | a8753c34 | bellard | read(s->fd, &bitmap_entry, 1);
|
204 | 3b46e624 | ths | |
205 | a8753c34 | bellard | if (!((bitmap_entry >> (extent_offset % 8)) & 1)) |
206 | a8753c34 | bellard | { |
207 | a8753c34 | bellard | // fprintf(stderr, "sector (%x) in bitmap not allocated\n",
|
208 | a8753c34 | bellard | // sector_num);
|
209 | a8753c34 | bellard | return -1; // not allocated |
210 | a8753c34 | bellard | } |
211 | a8753c34 | bellard | |
212 | a8753c34 | bellard | lseek(s->fd, block_offset, SEEK_SET); |
213 | 3b46e624 | ths | |
214 | a8753c34 | bellard | return 0; |
215 | a8753c34 | bellard | } |
216 | a8753c34 | bellard | |
217 | 5fafdf24 | ths | static int bochs_read(BlockDriverState *bs, int64_t sector_num, |
218 | a8753c34 | bellard | uint8_t *buf, int nb_sectors)
|
219 | a8753c34 | bellard | { |
220 | a8753c34 | bellard | BDRVBochsState *s = bs->opaque; |
221 | a8753c34 | bellard | int ret;
|
222 | a8753c34 | bellard | |
223 | a8753c34 | bellard | while (nb_sectors > 0) { |
224 | a8753c34 | bellard | if (!seek_to_sector(bs, sector_num))
|
225 | a8753c34 | bellard | { |
226 | a8753c34 | bellard | ret = read(s->fd, buf, 512);
|
227 | a8753c34 | bellard | if (ret != 512) |
228 | a8753c34 | bellard | return -1; |
229 | a8753c34 | bellard | } |
230 | a8753c34 | bellard | else
|
231 | a8753c34 | bellard | memset(buf, 0, 512); |
232 | a8753c34 | bellard | nb_sectors--; |
233 | a8753c34 | bellard | sector_num++; |
234 | a8753c34 | bellard | buf += 512;
|
235 | a8753c34 | bellard | } |
236 | a8753c34 | bellard | return 0; |
237 | a8753c34 | bellard | } |
238 | a8753c34 | bellard | |
239 | a8753c34 | bellard | static void bochs_close(BlockDriverState *bs) |
240 | a8753c34 | bellard | { |
241 | a8753c34 | bellard | BDRVBochsState *s = bs->opaque; |
242 | a8753c34 | bellard | qemu_free(s->catalog_bitmap); |
243 | a8753c34 | bellard | close(s->fd); |
244 | a8753c34 | bellard | } |
245 | a8753c34 | bellard | |
246 | a8753c34 | bellard | BlockDriver bdrv_bochs = { |
247 | a8753c34 | bellard | "bochs",
|
248 | a8753c34 | bellard | sizeof(BDRVBochsState),
|
249 | a8753c34 | bellard | bochs_probe, |
250 | a8753c34 | bellard | bochs_open, |
251 | a8753c34 | bellard | bochs_read, |
252 | a8753c34 | bellard | NULL,
|
253 | a8753c34 | bellard | bochs_close, |
254 | a8753c34 | bellard | }; |