root / block-bochs.c @ 1247c5f7
History | View | Annotate | Download (6.2 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 | a8753c34 | bellard | *
|
5 | a8753c34 | bellard | * Copyright (c) 2005 Alex Beregszaszi
|
6 | a8753c34 | bellard | *
|
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 | a8753c34 | bellard | #include "vl.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 | a8753c34 | bellard | #define HEADER_VERSION 0x00010000 |
32 | a8753c34 | bellard | #define HEADER_SIZE 512 |
33 | a8753c34 | bellard | |
34 | a8753c34 | bellard | #define REDOLOG_TYPE "Redolog" |
35 | a8753c34 | bellard | #define GROWING_TYPE "Growing" |
36 | a8753c34 | bellard | |
37 | a8753c34 | bellard | // not allocated: 0xffffffff
|
38 | a8753c34 | bellard | |
39 | a8753c34 | bellard | // always little-endian
|
40 | a8753c34 | bellard | struct bochs_header {
|
41 | a8753c34 | bellard | char magic[32]; // "Bochs Virtual HD Image" |
42 | a8753c34 | bellard | char type[16]; // "Redolog" |
43 | a8753c34 | bellard | char subtype[16]; // "Undoable" / "Volatile" / "Growing" |
44 | a8753c34 | bellard | uint32_t version; |
45 | a8753c34 | bellard | uint32_t header; // size of header
|
46 | a8753c34 | bellard | |
47 | a8753c34 | bellard | union {
|
48 | a8753c34 | bellard | struct {
|
49 | a8753c34 | bellard | uint32_t catalog; // num of entries
|
50 | a8753c34 | bellard | uint32_t bitmap; // bitmap size
|
51 | a8753c34 | bellard | uint32_t extent; // extent size
|
52 | a8753c34 | bellard | uint64_t disk; // disk size
|
53 | a8753c34 | bellard | char padding[HEADER_SIZE - 64 - 8 - 20]; |
54 | a8753c34 | bellard | } redolog; |
55 | a8753c34 | bellard | char padding[HEADER_SIZE - 64 - 8]; |
56 | a8753c34 | bellard | } extra; |
57 | a8753c34 | bellard | }; |
58 | a8753c34 | bellard | |
59 | a8753c34 | bellard | typedef struct BDRVBochsState { |
60 | a8753c34 | bellard | int fd;
|
61 | a8753c34 | bellard | |
62 | a8753c34 | bellard | uint32_t *catalog_bitmap; |
63 | a8753c34 | bellard | int catalog_size;
|
64 | a8753c34 | bellard | |
65 | a8753c34 | bellard | int data_offset;
|
66 | a8753c34 | bellard | |
67 | a8753c34 | bellard | int bitmap_blocks;
|
68 | a8753c34 | bellard | int extent_blocks;
|
69 | a8753c34 | bellard | int extent_size;
|
70 | a8753c34 | bellard | } BDRVBochsState; |
71 | a8753c34 | bellard | |
72 | a8753c34 | bellard | static int bochs_probe(const uint8_t *buf, int buf_size, const char *filename) |
73 | a8753c34 | bellard | { |
74 | a8753c34 | bellard | const struct bochs_header *bochs = (const void *)buf; |
75 | a8753c34 | bellard | |
76 | a8753c34 | bellard | if (buf_size < HEADER_SIZE)
|
77 | a8753c34 | bellard | return 0; |
78 | a8753c34 | bellard | |
79 | a8753c34 | bellard | if (!strcmp(bochs->magic, HEADER_MAGIC) &&
|
80 | a8753c34 | bellard | !strcmp(bochs->type, REDOLOG_TYPE) && |
81 | a8753c34 | bellard | !strcmp(bochs->subtype, GROWING_TYPE) && |
82 | a8753c34 | bellard | (le32_to_cpu(bochs->version) == HEADER_VERSION)) |
83 | a8753c34 | bellard | return 100; |
84 | a8753c34 | bellard | |
85 | a8753c34 | bellard | return 0; |
86 | a8753c34 | bellard | } |
87 | a8753c34 | bellard | |
88 | a8753c34 | bellard | static int bochs_open(BlockDriverState *bs, const char *filename) |
89 | a8753c34 | bellard | { |
90 | a8753c34 | bellard | BDRVBochsState *s = bs->opaque; |
91 | a8753c34 | bellard | int fd, i;
|
92 | a8753c34 | bellard | struct bochs_header bochs;
|
93 | a8753c34 | bellard | |
94 | a8753c34 | bellard | fd = open(filename, O_RDWR | O_BINARY | O_LARGEFILE); |
95 | a8753c34 | bellard | if (fd < 0) { |
96 | a8753c34 | bellard | fd = open(filename, O_RDONLY | O_BINARY | O_LARGEFILE); |
97 | a8753c34 | bellard | if (fd < 0) |
98 | a8753c34 | bellard | return -1; |
99 | a8753c34 | bellard | } |
100 | a8753c34 | bellard | |
101 | a8753c34 | bellard | bs->read_only = 1; // no write support yet |
102 | a8753c34 | bellard | |
103 | a8753c34 | bellard | s->fd = fd; |
104 | a8753c34 | bellard | |
105 | a8753c34 | bellard | if (read(fd, &bochs, sizeof(bochs)) != sizeof(bochs)) { |
106 | a8753c34 | bellard | goto fail;
|
107 | a8753c34 | bellard | } |
108 | a8753c34 | bellard | |
109 | a8753c34 | bellard | if (strcmp(bochs.magic, HEADER_MAGIC) ||
|
110 | a8753c34 | bellard | strcmp(bochs.type, REDOLOG_TYPE) || |
111 | a8753c34 | bellard | strcmp(bochs.subtype, GROWING_TYPE) || |
112 | a8753c34 | bellard | (le32_to_cpu(bochs.version) != HEADER_VERSION)) { |
113 | a8753c34 | bellard | goto fail;
|
114 | a8753c34 | bellard | } |
115 | a8753c34 | bellard | |
116 | a8753c34 | bellard | bs->total_sectors = le64_to_cpu(bochs.extra.redolog.disk) / 512;
|
117 | a8753c34 | bellard | |
118 | a8753c34 | bellard | lseek(s->fd, le32_to_cpu(bochs.header), SEEK_SET); |
119 | a8753c34 | bellard | |
120 | a8753c34 | bellard | s->catalog_size = le32_to_cpu(bochs.extra.redolog.catalog); |
121 | a8753c34 | bellard | s->catalog_bitmap = qemu_malloc(s->catalog_size * 4);
|
122 | a8753c34 | bellard | if (!s->catalog_bitmap)
|
123 | a8753c34 | bellard | goto fail;
|
124 | a8753c34 | bellard | if (read(s->fd, s->catalog_bitmap, s->catalog_size * 4) != |
125 | a8753c34 | bellard | s->catalog_size * 4)
|
126 | a8753c34 | bellard | goto fail;
|
127 | a8753c34 | bellard | for (i = 0; i < s->catalog_size; i++) |
128 | a8753c34 | bellard | le32_to_cpus(&s->catalog_bitmap[i]); |
129 | a8753c34 | bellard | |
130 | a8753c34 | bellard | s->data_offset = le32_to_cpu(bochs.header) + (s->catalog_size * 4);
|
131 | a8753c34 | bellard | |
132 | a8753c34 | bellard | s->bitmap_blocks = 1 + (le32_to_cpu(bochs.extra.redolog.bitmap) - 1) / 512; |
133 | a8753c34 | bellard | s->extent_blocks = 1 + (le32_to_cpu(bochs.extra.redolog.extent) - 1) / 512; |
134 | a8753c34 | bellard | |
135 | a8753c34 | bellard | s->extent_size = le32_to_cpu(bochs.extra.redolog.extent); |
136 | a8753c34 | bellard | |
137 | a8753c34 | bellard | return 0; |
138 | a8753c34 | bellard | fail:
|
139 | a8753c34 | bellard | close(fd); |
140 | a8753c34 | bellard | return -1; |
141 | a8753c34 | bellard | } |
142 | a8753c34 | bellard | |
143 | a8753c34 | bellard | static inline int seek_to_sector(BlockDriverState *bs, int64_t sector_num) |
144 | a8753c34 | bellard | { |
145 | a8753c34 | bellard | BDRVBochsState *s = bs->opaque; |
146 | a8753c34 | bellard | int64_t offset = sector_num * 512;
|
147 | a8753c34 | bellard | int64_t extent_index, extent_offset, bitmap_offset, block_offset; |
148 | a8753c34 | bellard | char bitmap_entry;
|
149 | a8753c34 | bellard | |
150 | a8753c34 | bellard | // seek to sector
|
151 | a8753c34 | bellard | extent_index = offset / s->extent_size; |
152 | a8753c34 | bellard | extent_offset = (offset % s->extent_size) / 512;
|
153 | a8753c34 | bellard | |
154 | a8753c34 | bellard | if (s->catalog_bitmap[extent_index] == 0xffffffff) |
155 | a8753c34 | bellard | { |
156 | a8753c34 | bellard | // fprintf(stderr, "page not allocated [%x - %x:%x]\n",
|
157 | a8753c34 | bellard | // sector_num, extent_index, extent_offset);
|
158 | a8753c34 | bellard | return -1; // not allocated |
159 | a8753c34 | bellard | } |
160 | a8753c34 | bellard | |
161 | a8753c34 | bellard | bitmap_offset = s->data_offset + (512 * s->catalog_bitmap[extent_index] *
|
162 | a8753c34 | bellard | (s->extent_blocks + s->bitmap_blocks)); |
163 | a8753c34 | bellard | block_offset = bitmap_offset + (512 * (s->bitmap_blocks + extent_offset));
|
164 | a8753c34 | bellard | |
165 | a8753c34 | bellard | // fprintf(stderr, "sect: %x [ext i: %x o: %x] -> %x bitmap: %x block: %x\n",
|
166 | a8753c34 | bellard | // sector_num, extent_index, extent_offset,
|
167 | a8753c34 | bellard | // le32_to_cpu(s->catalog_bitmap[extent_index]),
|
168 | a8753c34 | bellard | // bitmap_offset, block_offset);
|
169 | a8753c34 | bellard | |
170 | a8753c34 | bellard | // read in bitmap for current extent
|
171 | a8753c34 | bellard | lseek(s->fd, bitmap_offset + (extent_offset / 8), SEEK_SET);
|
172 | a8753c34 | bellard | |
173 | a8753c34 | bellard | read(s->fd, &bitmap_entry, 1);
|
174 | a8753c34 | bellard | |
175 | a8753c34 | bellard | if (!((bitmap_entry >> (extent_offset % 8)) & 1)) |
176 | a8753c34 | bellard | { |
177 | a8753c34 | bellard | // fprintf(stderr, "sector (%x) in bitmap not allocated\n",
|
178 | a8753c34 | bellard | // sector_num);
|
179 | a8753c34 | bellard | return -1; // not allocated |
180 | a8753c34 | bellard | } |
181 | a8753c34 | bellard | |
182 | a8753c34 | bellard | lseek(s->fd, block_offset, SEEK_SET); |
183 | a8753c34 | bellard | |
184 | a8753c34 | bellard | return 0; |
185 | a8753c34 | bellard | } |
186 | a8753c34 | bellard | |
187 | a8753c34 | bellard | static int bochs_read(BlockDriverState *bs, int64_t sector_num, |
188 | a8753c34 | bellard | uint8_t *buf, int nb_sectors)
|
189 | a8753c34 | bellard | { |
190 | a8753c34 | bellard | BDRVBochsState *s = bs->opaque; |
191 | a8753c34 | bellard | int ret;
|
192 | a8753c34 | bellard | |
193 | a8753c34 | bellard | while (nb_sectors > 0) { |
194 | a8753c34 | bellard | if (!seek_to_sector(bs, sector_num))
|
195 | a8753c34 | bellard | { |
196 | a8753c34 | bellard | ret = read(s->fd, buf, 512);
|
197 | a8753c34 | bellard | if (ret != 512) |
198 | a8753c34 | bellard | return -1; |
199 | a8753c34 | bellard | } |
200 | a8753c34 | bellard | else
|
201 | a8753c34 | bellard | memset(buf, 0, 512); |
202 | a8753c34 | bellard | nb_sectors--; |
203 | a8753c34 | bellard | sector_num++; |
204 | a8753c34 | bellard | buf += 512;
|
205 | a8753c34 | bellard | } |
206 | a8753c34 | bellard | return 0; |
207 | a8753c34 | bellard | } |
208 | a8753c34 | bellard | |
209 | a8753c34 | bellard | static void bochs_close(BlockDriverState *bs) |
210 | a8753c34 | bellard | { |
211 | a8753c34 | bellard | BDRVBochsState *s = bs->opaque; |
212 | a8753c34 | bellard | qemu_free(s->catalog_bitmap); |
213 | a8753c34 | bellard | close(s->fd); |
214 | a8753c34 | bellard | } |
215 | a8753c34 | bellard | |
216 | a8753c34 | bellard | BlockDriver bdrv_bochs = { |
217 | a8753c34 | bellard | "bochs",
|
218 | a8753c34 | bellard | sizeof(BDRVBochsState),
|
219 | a8753c34 | bellard | bochs_probe, |
220 | a8753c34 | bellard | bochs_open, |
221 | a8753c34 | bellard | bochs_read, |
222 | a8753c34 | bellard | NULL,
|
223 | a8753c34 | bellard | bochs_close, |
224 | a8753c34 | bellard | }; |