root / block / vpc.c @ 0aa217e4
History | View | Annotate | Download (17.2 kB)
1 | 6a0f9e82 | bellard | /*
|
---|---|---|---|
2 | 6a0f9e82 | bellard | * Block driver for Conectix/Microsoft Virtual PC images
|
3 | 5fafdf24 | ths | *
|
4 | 6a0f9e82 | bellard | * Copyright (c) 2005 Alex Beregszaszi
|
5 | 15d35bc5 | aliguori | * Copyright (c) 2009 Kevin Wolf <kwolf@suse.de>
|
6 | 5fafdf24 | ths | *
|
7 | 6a0f9e82 | bellard | * Permission is hereby granted, free of charge, to any person obtaining a copy
|
8 | 6a0f9e82 | bellard | * of this software and associated documentation files (the "Software"), to deal
|
9 | 6a0f9e82 | bellard | * in the Software without restriction, including without limitation the rights
|
10 | 6a0f9e82 | bellard | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
11 | 6a0f9e82 | bellard | * copies of the Software, and to permit persons to whom the Software is
|
12 | 6a0f9e82 | bellard | * furnished to do so, subject to the following conditions:
|
13 | 6a0f9e82 | bellard | *
|
14 | 6a0f9e82 | bellard | * The above copyright notice and this permission notice shall be included in
|
15 | 6a0f9e82 | bellard | * all copies or substantial portions of the Software.
|
16 | 6a0f9e82 | bellard | *
|
17 | 6a0f9e82 | bellard | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
18 | 6a0f9e82 | bellard | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
19 | 6a0f9e82 | bellard | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
20 | 6a0f9e82 | bellard | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
21 | 6a0f9e82 | bellard | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
22 | 6a0f9e82 | bellard | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
23 | 6a0f9e82 | bellard | * THE SOFTWARE.
|
24 | 6a0f9e82 | bellard | */
|
25 | faf07963 | pbrook | #include "qemu-common.h" |
26 | 6a0f9e82 | bellard | #include "block_int.h" |
27 | 5efa9d5a | Anthony Liguori | #include "module.h" |
28 | 6a0f9e82 | bellard | |
29 | 6a0f9e82 | bellard | /**************************************************************/
|
30 | 6a0f9e82 | bellard | |
31 | 6a0f9e82 | bellard | #define HEADER_SIZE 512 |
32 | 6a0f9e82 | bellard | |
33 | 6a0f9e82 | bellard | //#define CACHE
|
34 | 6a0f9e82 | bellard | |
35 | 2cfacb62 | aliguori | enum vhd_type {
|
36 | 2cfacb62 | aliguori | VHD_FIXED = 2,
|
37 | 2cfacb62 | aliguori | VHD_DYNAMIC = 3,
|
38 | 2cfacb62 | aliguori | VHD_DIFFERENCING = 4,
|
39 | 2cfacb62 | aliguori | }; |
40 | 2cfacb62 | aliguori | |
41 | 57c7d9e5 | aliguori | // Seconds since Jan 1, 2000 0:00:00 (UTC)
|
42 | 57c7d9e5 | aliguori | #define VHD_TIMESTAMP_BASE 946684800 |
43 | 57c7d9e5 | aliguori | |
44 | 6a0f9e82 | bellard | // always big-endian
|
45 | b9fa33a6 | aliguori | struct vhd_footer {
|
46 | 2cfacb62 | aliguori | char creator[8]; // "conectix" |
47 | 2cfacb62 | aliguori | uint32_t features; |
48 | 2cfacb62 | aliguori | uint32_t version; |
49 | 2cfacb62 | aliguori | |
50 | 2cfacb62 | aliguori | // Offset of next header structure, 0xFFFFFFFF if none
|
51 | 2cfacb62 | aliguori | uint64_t data_offset; |
52 | 2cfacb62 | aliguori | |
53 | 2cfacb62 | aliguori | // Seconds since Jan 1, 2000 0:00:00 (UTC)
|
54 | 2cfacb62 | aliguori | uint32_t timestamp; |
55 | 2cfacb62 | aliguori | |
56 | 2cfacb62 | aliguori | char creator_app[4]; // "vpc " |
57 | 2cfacb62 | aliguori | uint16_t major; |
58 | 2cfacb62 | aliguori | uint16_t minor; |
59 | 2cfacb62 | aliguori | char creator_os[4]; // "Wi2k" |
60 | 2cfacb62 | aliguori | |
61 | 2cfacb62 | aliguori | uint64_t orig_size; |
62 | 2cfacb62 | aliguori | uint64_t size; |
63 | 2cfacb62 | aliguori | |
64 | 2cfacb62 | aliguori | uint16_t cyls; |
65 | 2cfacb62 | aliguori | uint8_t heads; |
66 | 2cfacb62 | aliguori | uint8_t secs_per_cyl; |
67 | 2cfacb62 | aliguori | |
68 | 2cfacb62 | aliguori | uint32_t type; |
69 | 2cfacb62 | aliguori | |
70 | 2cfacb62 | aliguori | // Checksum of the Hard Disk Footer ("one's complement of the sum of all
|
71 | 2cfacb62 | aliguori | // the bytes in the footer without the checksum field")
|
72 | 2cfacb62 | aliguori | uint32_t checksum; |
73 | 2cfacb62 | aliguori | |
74 | 2cfacb62 | aliguori | // UUID used to identify a parent hard disk (backing file)
|
75 | 2cfacb62 | aliguori | uint8_t uuid[16];
|
76 | 2cfacb62 | aliguori | |
77 | 2cfacb62 | aliguori | uint8_t in_saved_state; |
78 | b9fa33a6 | aliguori | }; |
79 | b9fa33a6 | aliguori | |
80 | b9fa33a6 | aliguori | struct vhd_dyndisk_header {
|
81 | 2cfacb62 | aliguori | char magic[8]; // "cxsparse" |
82 | 2cfacb62 | aliguori | |
83 | 2cfacb62 | aliguori | // Offset of next header structure, 0xFFFFFFFF if none
|
84 | 2cfacb62 | aliguori | uint64_t data_offset; |
85 | 2cfacb62 | aliguori | |
86 | 2cfacb62 | aliguori | // Offset of the Block Allocation Table (BAT)
|
87 | 2cfacb62 | aliguori | uint64_t table_offset; |
88 | 2cfacb62 | aliguori | |
89 | 2cfacb62 | aliguori | uint32_t version; |
90 | 2cfacb62 | aliguori | uint32_t max_table_entries; // 32bit/entry
|
91 | 2cfacb62 | aliguori | |
92 | 2cfacb62 | aliguori | // 2 MB by default, must be a power of two
|
93 | 2cfacb62 | aliguori | uint32_t block_size; |
94 | 2cfacb62 | aliguori | |
95 | 2cfacb62 | aliguori | uint32_t checksum; |
96 | 2cfacb62 | aliguori | uint8_t parent_uuid[16];
|
97 | 2cfacb62 | aliguori | uint32_t parent_timestamp; |
98 | 2cfacb62 | aliguori | uint32_t reserved; |
99 | 2cfacb62 | aliguori | |
100 | 2cfacb62 | aliguori | // Backing file name (in UTF-16)
|
101 | 2cfacb62 | aliguori | uint8_t parent_name[512];
|
102 | 2cfacb62 | aliguori | |
103 | 2cfacb62 | aliguori | struct {
|
104 | 2cfacb62 | aliguori | uint32_t platform; |
105 | 2cfacb62 | aliguori | uint32_t data_space; |
106 | 2cfacb62 | aliguori | uint32_t data_length; |
107 | 2cfacb62 | aliguori | uint32_t reserved; |
108 | 2cfacb62 | aliguori | uint64_t data_offset; |
109 | 2cfacb62 | aliguori | } parent_locator[8];
|
110 | 6a0f9e82 | bellard | }; |
111 | 6a0f9e82 | bellard | |
112 | 6a0f9e82 | bellard | typedef struct BDRVVPCState { |
113 | b71d1c2e | aliguori | BlockDriverState *hd; |
114 | 3b46e624 | ths | |
115 | 15d35bc5 | aliguori | uint8_t footer_buf[HEADER_SIZE]; |
116 | 15d35bc5 | aliguori | uint64_t free_data_block_offset; |
117 | 2cfacb62 | aliguori | int max_table_entries;
|
118 | 6a0f9e82 | bellard | uint32_t *pagetable; |
119 | 15d35bc5 | aliguori | uint64_t bat_offset; |
120 | 15d35bc5 | aliguori | uint64_t last_bitmap_offset; |
121 | 6a0f9e82 | bellard | |
122 | 2cfacb62 | aliguori | uint32_t block_size; |
123 | 15d35bc5 | aliguori | uint32_t bitmap_size; |
124 | 15d35bc5 | aliguori | |
125 | 6a0f9e82 | bellard | #ifdef CACHE
|
126 | 6a0f9e82 | bellard | uint8_t *pageentry_u8; |
127 | 6a0f9e82 | bellard | uint32_t *pageentry_u32; |
128 | 6a0f9e82 | bellard | uint16_t *pageentry_u16; |
129 | 3b46e624 | ths | |
130 | 6a0f9e82 | bellard | uint64_t last_bitmap; |
131 | 6a0f9e82 | bellard | #endif
|
132 | 6a0f9e82 | bellard | } BDRVVPCState; |
133 | 6a0f9e82 | bellard | |
134 | 57c7d9e5 | aliguori | static uint32_t vpc_checksum(uint8_t* buf, size_t size)
|
135 | 57c7d9e5 | aliguori | { |
136 | 57c7d9e5 | aliguori | uint32_t res = 0;
|
137 | 57c7d9e5 | aliguori | int i;
|
138 | 57c7d9e5 | aliguori | |
139 | 57c7d9e5 | aliguori | for (i = 0; i < size; i++) |
140 | 57c7d9e5 | aliguori | res += buf[i]; |
141 | 57c7d9e5 | aliguori | |
142 | 57c7d9e5 | aliguori | return ~res;
|
143 | 57c7d9e5 | aliguori | } |
144 | 57c7d9e5 | aliguori | |
145 | 57c7d9e5 | aliguori | |
146 | 6a0f9e82 | bellard | static int vpc_probe(const uint8_t *buf, int buf_size, const char *filename) |
147 | 6a0f9e82 | bellard | { |
148 | ffe8ab83 | ths | if (buf_size >= 8 && !strncmp((char *)buf, "conectix", 8)) |
149 | 6a0f9e82 | bellard | return 100; |
150 | 6a0f9e82 | bellard | return 0; |
151 | 6a0f9e82 | bellard | } |
152 | 6a0f9e82 | bellard | |
153 | 83f64091 | bellard | static int vpc_open(BlockDriverState *bs, const char *filename, int flags) |
154 | 6a0f9e82 | bellard | { |
155 | 6a0f9e82 | bellard | BDRVVPCState *s = bs->opaque; |
156 | b71d1c2e | aliguori | int ret, i;
|
157 | b9fa33a6 | aliguori | struct vhd_footer* footer;
|
158 | b9fa33a6 | aliguori | struct vhd_dyndisk_header* dyndisk_header;
|
159 | b9fa33a6 | aliguori | uint8_t buf[HEADER_SIZE]; |
160 | 57c7d9e5 | aliguori | uint32_t checksum; |
161 | 6a0f9e82 | bellard | |
162 | b71d1c2e | aliguori | ret = bdrv_file_open(&s->hd, filename, flags); |
163 | b71d1c2e | aliguori | if (ret < 0) |
164 | b71d1c2e | aliguori | return ret;
|
165 | 6a0f9e82 | bellard | |
166 | 15d35bc5 | aliguori | if (bdrv_pread(s->hd, 0, s->footer_buf, HEADER_SIZE) != HEADER_SIZE) |
167 | 6a0f9e82 | bellard | goto fail;
|
168 | 6a0f9e82 | bellard | |
169 | 15d35bc5 | aliguori | footer = (struct vhd_footer*) s->footer_buf;
|
170 | b9fa33a6 | aliguori | if (strncmp(footer->creator, "conectix", 8)) |
171 | 6a0f9e82 | bellard | goto fail;
|
172 | 6a0f9e82 | bellard | |
173 | 57c7d9e5 | aliguori | checksum = be32_to_cpu(footer->checksum); |
174 | 57c7d9e5 | aliguori | footer->checksum = 0;
|
175 | 57c7d9e5 | aliguori | if (vpc_checksum(s->footer_buf, HEADER_SIZE) != checksum)
|
176 | 57c7d9e5 | aliguori | fprintf(stderr, "block-vpc: The header checksum of '%s' is "
|
177 | 57c7d9e5 | aliguori | "incorrect.\n", filename);
|
178 | 57c7d9e5 | aliguori | |
179 | 1fa79228 | aliguori | // The visible size of a image in Virtual PC depends on the geometry
|
180 | 1fa79228 | aliguori | // rather than on the size stored in the footer (the size in the footer
|
181 | 1fa79228 | aliguori | // is too large usually)
|
182 | 1fa79228 | aliguori | bs->total_sectors = (int64_t) |
183 | 1fa79228 | aliguori | be16_to_cpu(footer->cyls) * footer->heads * footer->secs_per_cyl; |
184 | 1fa79228 | aliguori | |
185 | b71d1c2e | aliguori | if (bdrv_pread(s->hd, be64_to_cpu(footer->data_offset), buf, HEADER_SIZE)
|
186 | b71d1c2e | aliguori | != HEADER_SIZE) |
187 | 6a0f9e82 | bellard | goto fail;
|
188 | 6a0f9e82 | bellard | |
189 | b9fa33a6 | aliguori | dyndisk_header = (struct vhd_dyndisk_header*) buf;
|
190 | b9fa33a6 | aliguori | |
191 | b9fa33a6 | aliguori | if (strncmp(dyndisk_header->magic, "cxsparse", 8)) |
192 | b9fa33a6 | aliguori | goto fail;
|
193 | 6a0f9e82 | bellard | |
194 | 6a0f9e82 | bellard | |
195 | 15d35bc5 | aliguori | s->block_size = be32_to_cpu(dyndisk_header->block_size); |
196 | 15d35bc5 | aliguori | s->bitmap_size = ((s->block_size / (8 * 512)) + 511) & ~511; |
197 | 15d35bc5 | aliguori | |
198 | 2cfacb62 | aliguori | s->max_table_entries = be32_to_cpu(dyndisk_header->max_table_entries); |
199 | 2cfacb62 | aliguori | s->pagetable = qemu_malloc(s->max_table_entries * 4);
|
200 | b71d1c2e | aliguori | |
201 | 15d35bc5 | aliguori | s->bat_offset = be64_to_cpu(dyndisk_header->table_offset); |
202 | 15d35bc5 | aliguori | if (bdrv_pread(s->hd, s->bat_offset, s->pagetable,
|
203 | 15d35bc5 | aliguori | s->max_table_entries * 4) != s->max_table_entries * 4) |
204 | b71d1c2e | aliguori | goto fail;
|
205 | b71d1c2e | aliguori | |
206 | 15d35bc5 | aliguori | s->free_data_block_offset = |
207 | 15d35bc5 | aliguori | (s->bat_offset + (s->max_table_entries * 4) + 511) & ~511; |
208 | 15d35bc5 | aliguori | |
209 | 15d35bc5 | aliguori | for (i = 0; i < s->max_table_entries; i++) { |
210 | 15d35bc5 | aliguori | be32_to_cpus(&s->pagetable[i]); |
211 | 15d35bc5 | aliguori | if (s->pagetable[i] != 0xFFFFFFFF) { |
212 | 15d35bc5 | aliguori | int64_t next = (512 * (int64_t) s->pagetable[i]) +
|
213 | 15d35bc5 | aliguori | s->bitmap_size + s->block_size; |
214 | 15d35bc5 | aliguori | |
215 | 15d35bc5 | aliguori | if (next> s->free_data_block_offset)
|
216 | 15d35bc5 | aliguori | s->free_data_block_offset = next; |
217 | 15d35bc5 | aliguori | } |
218 | 15d35bc5 | aliguori | } |
219 | 15d35bc5 | aliguori | |
220 | 15d35bc5 | aliguori | s->last_bitmap_offset = (int64_t) -1;
|
221 | 6a0f9e82 | bellard | |
222 | 6a0f9e82 | bellard | #ifdef CACHE
|
223 | 6a0f9e82 | bellard | s->pageentry_u8 = qemu_malloc(512);
|
224 | 6a0f9e82 | bellard | s->pageentry_u32 = s->pageentry_u8; |
225 | 6a0f9e82 | bellard | s->pageentry_u16 = s->pageentry_u8; |
226 | 6a0f9e82 | bellard | s->last_pagetable = -1;
|
227 | 6a0f9e82 | bellard | #endif
|
228 | 6a0f9e82 | bellard | |
229 | 6a0f9e82 | bellard | return 0; |
230 | 6a0f9e82 | bellard | fail:
|
231 | b71d1c2e | aliguori | bdrv_delete(s->hd); |
232 | 6a0f9e82 | bellard | return -1; |
233 | 6a0f9e82 | bellard | } |
234 | 6a0f9e82 | bellard | |
235 | b71d1c2e | aliguori | /*
|
236 | b71d1c2e | aliguori | * Returns the absolute byte offset of the given sector in the image file.
|
237 | b71d1c2e | aliguori | * If the sector is not allocated, -1 is returned instead.
|
238 | 15d35bc5 | aliguori | *
|
239 | 15d35bc5 | aliguori | * The parameter write must be 1 if the offset will be used for a write
|
240 | 15d35bc5 | aliguori | * operation (the block bitmaps is updated then), 0 otherwise.
|
241 | b71d1c2e | aliguori | */
|
242 | 15d35bc5 | aliguori | static inline int64_t get_sector_offset(BlockDriverState *bs, |
243 | 15d35bc5 | aliguori | int64_t sector_num, int write)
|
244 | 6a0f9e82 | bellard | { |
245 | 6a0f9e82 | bellard | BDRVVPCState *s = bs->opaque; |
246 | 6a0f9e82 | bellard | uint64_t offset = sector_num * 512;
|
247 | 6a0f9e82 | bellard | uint64_t bitmap_offset, block_offset; |
248 | 6a0f9e82 | bellard | uint32_t pagetable_index, pageentry_index; |
249 | 6a0f9e82 | bellard | |
250 | 2cfacb62 | aliguori | pagetable_index = offset / s->block_size; |
251 | 2cfacb62 | aliguori | pageentry_index = (offset % s->block_size) / 512;
|
252 | 3b46e624 | ths | |
253 | 15d35bc5 | aliguori | if (pagetable_index >= s->max_table_entries || s->pagetable[pagetable_index] == 0xffffffff) |
254 | 15d35bc5 | aliguori | return -1; // not allocated |
255 | 6a0f9e82 | bellard | |
256 | 378e2aea | aliguori | bitmap_offset = 512 * (uint64_t) s->pagetable[pagetable_index];
|
257 | 15d35bc5 | aliguori | block_offset = bitmap_offset + s->bitmap_size + (512 * pageentry_index);
|
258 | 15d35bc5 | aliguori | |
259 | 15d35bc5 | aliguori | // We must ensure that we don't write to any sectors which are marked as
|
260 | 15d35bc5 | aliguori | // unused in the bitmap. We get away with setting all bits in the block
|
261 | 15d35bc5 | aliguori | // bitmap each time we write to a new block. This might cause Virtual PC to
|
262 | 15d35bc5 | aliguori | // miss sparse read optimization, but it's not a problem in terms of
|
263 | 15d35bc5 | aliguori | // correctness.
|
264 | 15d35bc5 | aliguori | if (write && (s->last_bitmap_offset != bitmap_offset)) {
|
265 | 15d35bc5 | aliguori | uint8_t bitmap[s->bitmap_size]; |
266 | 15d35bc5 | aliguori | |
267 | 15d35bc5 | aliguori | s->last_bitmap_offset = bitmap_offset; |
268 | 15d35bc5 | aliguori | memset(bitmap, 0xff, s->bitmap_size);
|
269 | 15d35bc5 | aliguori | bdrv_pwrite(s->hd, bitmap_offset, bitmap, s->bitmap_size); |
270 | 15d35bc5 | aliguori | } |
271 | 3b46e624 | ths | |
272 | 26a76461 | bellard | // printf("sector: %" PRIx64 ", index: %x, offset: %x, bioff: %" PRIx64 ", bloff: %" PRIx64 "\n",
|
273 | 6a0f9e82 | bellard | // sector_num, pagetable_index, pageentry_index,
|
274 | 6a0f9e82 | bellard | // bitmap_offset, block_offset);
|
275 | 6a0f9e82 | bellard | |
276 | 6a0f9e82 | bellard | // disabled by reason
|
277 | 6a0f9e82 | bellard | #if 0
|
278 | 6a0f9e82 | bellard | #ifdef CACHE
|
279 | 6a0f9e82 | bellard | if (bitmap_offset != s->last_bitmap)
|
280 | 6a0f9e82 | bellard | {
|
281 | 6a0f9e82 | bellard | lseek(s->fd, bitmap_offset, SEEK_SET);
|
282 | 6a0f9e82 | bellard | |
283 | 6a0f9e82 | bellard | s->last_bitmap = bitmap_offset;
|
284 | 5fafdf24 | ths | |
285 | 6a0f9e82 | bellard | // Scary! Bitmap is stored as big endian 32bit entries,
|
286 | 6a0f9e82 | bellard | // while we used to look it up byte by byte
|
287 | 6a0f9e82 | bellard | read(s->fd, s->pageentry_u8, 512);
|
288 | 6a0f9e82 | bellard | for (i = 0; i < 128; i++)
|
289 | 6a0f9e82 | bellard | be32_to_cpus(&s->pageentry_u32[i]);
|
290 | 6a0f9e82 | bellard | }
|
291 | 6a0f9e82 | bellard | |
292 | 6a0f9e82 | bellard | if ((s->pageentry_u8[pageentry_index / 8] >> (pageentry_index % 8)) & 1)
|
293 | 6a0f9e82 | bellard | return -1;
|
294 | 6a0f9e82 | bellard | #else
|
295 | 6a0f9e82 | bellard | lseek(s->fd, bitmap_offset + (pageentry_index / 8), SEEK_SET);
|
296 | 5fafdf24 | ths | |
297 | 6a0f9e82 | bellard | read(s->fd, &bitmap_entry, 1);
|
298 | 6a0f9e82 | bellard | |
299 | 6a0f9e82 | bellard | if ((bitmap_entry >> (pageentry_index % 8)) & 1) |
300 | 6a0f9e82 | bellard | return -1; // not allocated |
301 | 6a0f9e82 | bellard | #endif
|
302 | 6a0f9e82 | bellard | #endif
|
303 | 6a0f9e82 | bellard | |
304 | b71d1c2e | aliguori | return block_offset;
|
305 | 6a0f9e82 | bellard | } |
306 | 6a0f9e82 | bellard | |
307 | 15d35bc5 | aliguori | /*
|
308 | 15d35bc5 | aliguori | * Writes the footer to the end of the image file. This is needed when the
|
309 | 15d35bc5 | aliguori | * file grows as it overwrites the old footer
|
310 | 15d35bc5 | aliguori | *
|
311 | 15d35bc5 | aliguori | * Returns 0 on success and < 0 on error
|
312 | 15d35bc5 | aliguori | */
|
313 | 15d35bc5 | aliguori | static int rewrite_footer(BlockDriverState* bs) |
314 | 15d35bc5 | aliguori | { |
315 | 15d35bc5 | aliguori | int ret;
|
316 | 15d35bc5 | aliguori | BDRVVPCState *s = bs->opaque; |
317 | 15d35bc5 | aliguori | int64_t offset = s->free_data_block_offset; |
318 | 15d35bc5 | aliguori | |
319 | 15d35bc5 | aliguori | ret = bdrv_pwrite(s->hd, offset, s->footer_buf, HEADER_SIZE); |
320 | 15d35bc5 | aliguori | if (ret < 0) |
321 | 15d35bc5 | aliguori | return ret;
|
322 | 15d35bc5 | aliguori | |
323 | 15d35bc5 | aliguori | return 0; |
324 | 15d35bc5 | aliguori | } |
325 | 15d35bc5 | aliguori | |
326 | 15d35bc5 | aliguori | /*
|
327 | 15d35bc5 | aliguori | * Allocates a new block. This involves writing a new footer and updating
|
328 | 15d35bc5 | aliguori | * the Block Allocation Table to use the space at the old end of the image
|
329 | 15d35bc5 | aliguori | * file (overwriting the old footer)
|
330 | 15d35bc5 | aliguori | *
|
331 | 15d35bc5 | aliguori | * Returns the sectors' offset in the image file on success and < 0 on error
|
332 | 15d35bc5 | aliguori | */
|
333 | 15d35bc5 | aliguori | static int64_t alloc_block(BlockDriverState* bs, int64_t sector_num)
|
334 | 15d35bc5 | aliguori | { |
335 | 15d35bc5 | aliguori | BDRVVPCState *s = bs->opaque; |
336 | 15d35bc5 | aliguori | int64_t bat_offset; |
337 | 15d35bc5 | aliguori | uint32_t index, bat_value; |
338 | 15d35bc5 | aliguori | int ret;
|
339 | 15d35bc5 | aliguori | uint8_t bitmap[s->bitmap_size]; |
340 | 15d35bc5 | aliguori | |
341 | 15d35bc5 | aliguori | // Check if sector_num is valid
|
342 | 15d35bc5 | aliguori | if ((sector_num < 0) || (sector_num > bs->total_sectors)) |
343 | 15d35bc5 | aliguori | return -1; |
344 | 15d35bc5 | aliguori | |
345 | 15d35bc5 | aliguori | // Write entry into in-memory BAT
|
346 | 15d35bc5 | aliguori | index = (sector_num * 512) / s->block_size;
|
347 | 15d35bc5 | aliguori | if (s->pagetable[index] != 0xFFFFFFFF) |
348 | 15d35bc5 | aliguori | return -1; |
349 | 15d35bc5 | aliguori | |
350 | 15d35bc5 | aliguori | s->pagetable[index] = s->free_data_block_offset / 512;
|
351 | 15d35bc5 | aliguori | |
352 | 15d35bc5 | aliguori | // Initialize the block's bitmap
|
353 | 15d35bc5 | aliguori | memset(bitmap, 0xff, s->bitmap_size);
|
354 | 15d35bc5 | aliguori | bdrv_pwrite(s->hd, s->free_data_block_offset, bitmap, s->bitmap_size); |
355 | 15d35bc5 | aliguori | |
356 | 15d35bc5 | aliguori | // Write new footer (the old one will be overwritten)
|
357 | 15d35bc5 | aliguori | s->free_data_block_offset += s->block_size + s->bitmap_size; |
358 | 15d35bc5 | aliguori | ret = rewrite_footer(bs); |
359 | 15d35bc5 | aliguori | if (ret < 0) |
360 | 15d35bc5 | aliguori | goto fail;
|
361 | 15d35bc5 | aliguori | |
362 | 15d35bc5 | aliguori | // Write BAT entry to disk
|
363 | 15d35bc5 | aliguori | bat_offset = s->bat_offset + (4 * index);
|
364 | 15d35bc5 | aliguori | bat_value = be32_to_cpu(s->pagetable[index]); |
365 | 15d35bc5 | aliguori | ret = bdrv_pwrite(s->hd, bat_offset, &bat_value, 4);
|
366 | 15d35bc5 | aliguori | if (ret < 0) |
367 | 15d35bc5 | aliguori | goto fail;
|
368 | 15d35bc5 | aliguori | |
369 | 15d35bc5 | aliguori | return get_sector_offset(bs, sector_num, 0); |
370 | 15d35bc5 | aliguori | |
371 | 15d35bc5 | aliguori | fail:
|
372 | 15d35bc5 | aliguori | s->free_data_block_offset -= (s->block_size + s->bitmap_size); |
373 | 15d35bc5 | aliguori | return -1; |
374 | 15d35bc5 | aliguori | } |
375 | 15d35bc5 | aliguori | |
376 | 5fafdf24 | ths | static int vpc_read(BlockDriverState *bs, int64_t sector_num, |
377 | 6a0f9e82 | bellard | uint8_t *buf, int nb_sectors)
|
378 | 6a0f9e82 | bellard | { |
379 | 6a0f9e82 | bellard | BDRVVPCState *s = bs->opaque; |
380 | 6a0f9e82 | bellard | int ret;
|
381 | b71d1c2e | aliguori | int64_t offset; |
382 | 6a0f9e82 | bellard | |
383 | 6a0f9e82 | bellard | while (nb_sectors > 0) { |
384 | 15d35bc5 | aliguori | offset = get_sector_offset(bs, sector_num, 0);
|
385 | b71d1c2e | aliguori | |
386 | b71d1c2e | aliguori | if (offset == -1) { |
387 | 6a0f9e82 | bellard | memset(buf, 0, 512); |
388 | b71d1c2e | aliguori | } else {
|
389 | b71d1c2e | aliguori | ret = bdrv_pread(s->hd, offset, buf, 512);
|
390 | b71d1c2e | aliguori | if (ret != 512) |
391 | b71d1c2e | aliguori | return -1; |
392 | b71d1c2e | aliguori | } |
393 | b71d1c2e | aliguori | |
394 | 6a0f9e82 | bellard | nb_sectors--; |
395 | 6a0f9e82 | bellard | sector_num++; |
396 | 6a0f9e82 | bellard | buf += 512;
|
397 | 6a0f9e82 | bellard | } |
398 | 6a0f9e82 | bellard | return 0; |
399 | 6a0f9e82 | bellard | } |
400 | 6a0f9e82 | bellard | |
401 | 15d35bc5 | aliguori | static int vpc_write(BlockDriverState *bs, int64_t sector_num, |
402 | 15d35bc5 | aliguori | const uint8_t *buf, int nb_sectors) |
403 | 15d35bc5 | aliguori | { |
404 | 15d35bc5 | aliguori | BDRVVPCState *s = bs->opaque; |
405 | 15d35bc5 | aliguori | int64_t offset; |
406 | 15d35bc5 | aliguori | int ret;
|
407 | 15d35bc5 | aliguori | |
408 | 15d35bc5 | aliguori | while (nb_sectors > 0) { |
409 | 15d35bc5 | aliguori | offset = get_sector_offset(bs, sector_num, 1);
|
410 | 15d35bc5 | aliguori | |
411 | 15d35bc5 | aliguori | if (offset == -1) { |
412 | 15d35bc5 | aliguori | offset = alloc_block(bs, sector_num); |
413 | 15d35bc5 | aliguori | if (offset < 0) |
414 | 15d35bc5 | aliguori | return -1; |
415 | 15d35bc5 | aliguori | } |
416 | 15d35bc5 | aliguori | |
417 | 15d35bc5 | aliguori | ret = bdrv_pwrite(s->hd, offset, buf, 512);
|
418 | 15d35bc5 | aliguori | if (ret != 512) |
419 | 15d35bc5 | aliguori | return -1; |
420 | 15d35bc5 | aliguori | |
421 | 15d35bc5 | aliguori | nb_sectors--; |
422 | 15d35bc5 | aliguori | sector_num++; |
423 | 15d35bc5 | aliguori | buf += 512;
|
424 | 15d35bc5 | aliguori | } |
425 | 15d35bc5 | aliguori | |
426 | 15d35bc5 | aliguori | return 0; |
427 | 15d35bc5 | aliguori | } |
428 | 15d35bc5 | aliguori | |
429 | 57c7d9e5 | aliguori | |
430 | 57c7d9e5 | aliguori | /*
|
431 | 57c7d9e5 | aliguori | * Calculates the number of cylinders, heads and sectors per cylinder
|
432 | 57c7d9e5 | aliguori | * based on a given number of sectors. This is the algorithm described
|
433 | 57c7d9e5 | aliguori | * in the VHD specification.
|
434 | 57c7d9e5 | aliguori | *
|
435 | 57c7d9e5 | aliguori | * Note that the geometry doesn't always exactly match total_sectors but
|
436 | 57c7d9e5 | aliguori | * may round it down.
|
437 | 6e9ea0c0 | aurel32 | *
|
438 | 6e9ea0c0 | aurel32 | * Returns 0 on success, -EFBIG if the size is larger than 127 GB
|
439 | 57c7d9e5 | aliguori | */
|
440 | 6e9ea0c0 | aurel32 | static int calculate_geometry(int64_t total_sectors, uint16_t* cyls, |
441 | 57c7d9e5 | aliguori | uint8_t* heads, uint8_t* secs_per_cyl) |
442 | 57c7d9e5 | aliguori | { |
443 | 57c7d9e5 | aliguori | uint32_t cyls_times_heads; |
444 | 57c7d9e5 | aliguori | |
445 | 57c7d9e5 | aliguori | if (total_sectors > 65535 * 16 * 255) |
446 | 6e9ea0c0 | aurel32 | return -EFBIG;
|
447 | 57c7d9e5 | aliguori | |
448 | 57c7d9e5 | aliguori | if (total_sectors > 65535 * 16 * 63) { |
449 | 57c7d9e5 | aliguori | *secs_per_cyl = 255;
|
450 | 57c7d9e5 | aliguori | *heads = 16;
|
451 | 57c7d9e5 | aliguori | cyls_times_heads = total_sectors / *secs_per_cyl; |
452 | 57c7d9e5 | aliguori | } else {
|
453 | 57c7d9e5 | aliguori | *secs_per_cyl = 17;
|
454 | 57c7d9e5 | aliguori | cyls_times_heads = total_sectors / *secs_per_cyl; |
455 | 57c7d9e5 | aliguori | *heads = (cyls_times_heads + 1023) / 1024; |
456 | 57c7d9e5 | aliguori | |
457 | 57c7d9e5 | aliguori | if (*heads < 4) |
458 | 57c7d9e5 | aliguori | *heads = 4;
|
459 | 57c7d9e5 | aliguori | |
460 | 57c7d9e5 | aliguori | if (cyls_times_heads >= (*heads * 1024) || *heads > 16) { |
461 | 57c7d9e5 | aliguori | *secs_per_cyl = 31;
|
462 | 57c7d9e5 | aliguori | *heads = 16;
|
463 | 57c7d9e5 | aliguori | cyls_times_heads = total_sectors / *secs_per_cyl; |
464 | 57c7d9e5 | aliguori | } |
465 | 57c7d9e5 | aliguori | |
466 | 57c7d9e5 | aliguori | if (cyls_times_heads >= (*heads * 1024)) { |
467 | 57c7d9e5 | aliguori | *secs_per_cyl = 63;
|
468 | 57c7d9e5 | aliguori | *heads = 16;
|
469 | 57c7d9e5 | aliguori | cyls_times_heads = total_sectors / *secs_per_cyl; |
470 | 57c7d9e5 | aliguori | } |
471 | 57c7d9e5 | aliguori | } |
472 | 57c7d9e5 | aliguori | |
473 | 57c7d9e5 | aliguori | // Note: Rounding up deviates from the Virtual PC behaviour
|
474 | 57c7d9e5 | aliguori | // However, we need this to avoid truncating images in qemu-img convert
|
475 | 57c7d9e5 | aliguori | *cyls = (cyls_times_heads + *heads - 1) / *heads;
|
476 | 6e9ea0c0 | aurel32 | |
477 | 6e9ea0c0 | aurel32 | return 0; |
478 | 57c7d9e5 | aliguori | } |
479 | 57c7d9e5 | aliguori | |
480 | 0e7e1989 | Kevin Wolf | static int vpc_create(const char *filename, QEMUOptionParameter *options) |
481 | 57c7d9e5 | aliguori | { |
482 | 57c7d9e5 | aliguori | uint8_t buf[1024];
|
483 | 57c7d9e5 | aliguori | struct vhd_footer* footer = (struct vhd_footer*) buf; |
484 | 57c7d9e5 | aliguori | struct vhd_dyndisk_header* dyndisk_header =
|
485 | 57c7d9e5 | aliguori | (struct vhd_dyndisk_header*) buf;
|
486 | 57c7d9e5 | aliguori | int fd, i;
|
487 | 57c7d9e5 | aliguori | uint16_t cyls; |
488 | 57c7d9e5 | aliguori | uint8_t heads; |
489 | 57c7d9e5 | aliguori | uint8_t secs_per_cyl; |
490 | 57c7d9e5 | aliguori | size_t block_size, num_bat_entries; |
491 | 0e7e1989 | Kevin Wolf | int64_t total_sectors = 0;
|
492 | 57c7d9e5 | aliguori | |
493 | 0e7e1989 | Kevin Wolf | // Read out options
|
494 | 0e7e1989 | Kevin Wolf | while (options && options->name) {
|
495 | 0e7e1989 | Kevin Wolf | if (!strcmp(options->name, "size")) { |
496 | 0e7e1989 | Kevin Wolf | total_sectors = options->value.n / 512;
|
497 | 0e7e1989 | Kevin Wolf | } |
498 | 0e7e1989 | Kevin Wolf | options++; |
499 | 0e7e1989 | Kevin Wolf | } |
500 | 57c7d9e5 | aliguori | |
501 | 0e7e1989 | Kevin Wolf | // Create the file
|
502 | 57c7d9e5 | aliguori | fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, 0644);
|
503 | 57c7d9e5 | aliguori | if (fd < 0) |
504 | 57c7d9e5 | aliguori | return -EIO;
|
505 | 57c7d9e5 | aliguori | |
506 | 57c7d9e5 | aliguori | // Calculate matching total_size and geometry
|
507 | 6e9ea0c0 | aurel32 | if (calculate_geometry(total_sectors, &cyls, &heads, &secs_per_cyl))
|
508 | 6e9ea0c0 | aurel32 | return -EFBIG;
|
509 | 57c7d9e5 | aliguori | total_sectors = (int64_t) cyls * heads * secs_per_cyl; |
510 | 57c7d9e5 | aliguori | |
511 | 57c7d9e5 | aliguori | // Prepare the Hard Disk Footer
|
512 | 57c7d9e5 | aliguori | memset(buf, 0, 1024); |
513 | 57c7d9e5 | aliguori | |
514 | 57c7d9e5 | aliguori | strncpy(footer->creator, "conectix", 8); |
515 | 57c7d9e5 | aliguori | // TODO Check if "qemu" creator_app is ok for VPC
|
516 | 57c7d9e5 | aliguori | strncpy(footer->creator_app, "qemu", 4); |
517 | 57c7d9e5 | aliguori | strncpy(footer->creator_os, "Wi2k", 4); |
518 | 57c7d9e5 | aliguori | |
519 | 57c7d9e5 | aliguori | footer->features = be32_to_cpu(0x02);
|
520 | 57c7d9e5 | aliguori | footer->version = be32_to_cpu(0x00010000);
|
521 | 57c7d9e5 | aliguori | footer->data_offset = be64_to_cpu(HEADER_SIZE); |
522 | 57c7d9e5 | aliguori | footer->timestamp = be32_to_cpu(time(NULL) - VHD_TIMESTAMP_BASE);
|
523 | 57c7d9e5 | aliguori | |
524 | 57c7d9e5 | aliguori | // Version of Virtual PC 2007
|
525 | 57c7d9e5 | aliguori | footer->major = be16_to_cpu(0x0005);
|
526 | 57c7d9e5 | aliguori | footer->minor =be16_to_cpu(0x0003);
|
527 | 57c7d9e5 | aliguori | |
528 | 57c7d9e5 | aliguori | footer->orig_size = be64_to_cpu(total_sectors * 512);
|
529 | 57c7d9e5 | aliguori | footer->size = be64_to_cpu(total_sectors * 512);
|
530 | 57c7d9e5 | aliguori | |
531 | 57c7d9e5 | aliguori | footer->cyls = be16_to_cpu(cyls); |
532 | 57c7d9e5 | aliguori | footer->heads = heads; |
533 | 57c7d9e5 | aliguori | footer->secs_per_cyl = secs_per_cyl; |
534 | 57c7d9e5 | aliguori | |
535 | 57c7d9e5 | aliguori | footer->type = be32_to_cpu(VHD_DYNAMIC); |
536 | 57c7d9e5 | aliguori | |
537 | 57c7d9e5 | aliguori | // TODO uuid is missing
|
538 | 57c7d9e5 | aliguori | |
539 | 57c7d9e5 | aliguori | footer->checksum = be32_to_cpu(vpc_checksum(buf, HEADER_SIZE)); |
540 | 57c7d9e5 | aliguori | |
541 | 57c7d9e5 | aliguori | // Write the footer (twice: at the beginning and at the end)
|
542 | 57c7d9e5 | aliguori | block_size = 0x200000;
|
543 | 57c7d9e5 | aliguori | num_bat_entries = (total_sectors + block_size / 512) / (block_size / 512); |
544 | 57c7d9e5 | aliguori | |
545 | 57c7d9e5 | aliguori | if (write(fd, buf, HEADER_SIZE) != HEADER_SIZE)
|
546 | 57c7d9e5 | aliguori | return -EIO;
|
547 | 57c7d9e5 | aliguori | |
548 | 57c7d9e5 | aliguori | if (lseek(fd, 1536 + ((num_bat_entries * 4 + 511) & ~511), SEEK_SET) < 0) |
549 | 57c7d9e5 | aliguori | return -EIO;
|
550 | 57c7d9e5 | aliguori | if (write(fd, buf, HEADER_SIZE) != HEADER_SIZE)
|
551 | 57c7d9e5 | aliguori | return -EIO;
|
552 | 57c7d9e5 | aliguori | |
553 | 57c7d9e5 | aliguori | // Write the initial BAT
|
554 | 57c7d9e5 | aliguori | if (lseek(fd, 3 * 512, SEEK_SET) < 0) |
555 | 57c7d9e5 | aliguori | return -EIO;
|
556 | 57c7d9e5 | aliguori | |
557 | 57c7d9e5 | aliguori | memset(buf, 0xFF, 512); |
558 | 57c7d9e5 | aliguori | for (i = 0; i < (num_bat_entries * 4 + 511) / 512; i++) |
559 | 57c7d9e5 | aliguori | if (write(fd, buf, 512) != 512) |
560 | 57c7d9e5 | aliguori | return -EIO;
|
561 | 57c7d9e5 | aliguori | |
562 | 57c7d9e5 | aliguori | |
563 | 57c7d9e5 | aliguori | // Prepare the Dynamic Disk Header
|
564 | 57c7d9e5 | aliguori | memset(buf, 0, 1024); |
565 | 57c7d9e5 | aliguori | |
566 | 57c7d9e5 | aliguori | strncpy(dyndisk_header->magic, "cxsparse", 8); |
567 | 57c7d9e5 | aliguori | |
568 | 57c7d9e5 | aliguori | dyndisk_header->data_offset = be64_to_cpu(0xFFFFFFFF);
|
569 | 57c7d9e5 | aliguori | dyndisk_header->table_offset = be64_to_cpu(3 * 512); |
570 | 57c7d9e5 | aliguori | dyndisk_header->version = be32_to_cpu(0x00010000);
|
571 | 57c7d9e5 | aliguori | dyndisk_header->block_size = be32_to_cpu(block_size); |
572 | 57c7d9e5 | aliguori | dyndisk_header->max_table_entries = be32_to_cpu(num_bat_entries); |
573 | 57c7d9e5 | aliguori | |
574 | 57c7d9e5 | aliguori | dyndisk_header->checksum = be32_to_cpu(vpc_checksum(buf, 1024));
|
575 | 57c7d9e5 | aliguori | |
576 | 57c7d9e5 | aliguori | // Write the header
|
577 | 57c7d9e5 | aliguori | if (lseek(fd, 512, SEEK_SET) < 0) |
578 | 57c7d9e5 | aliguori | return -EIO;
|
579 | 57c7d9e5 | aliguori | if (write(fd, buf, 1024) != 1024) |
580 | 57c7d9e5 | aliguori | return -EIO;
|
581 | 57c7d9e5 | aliguori | |
582 | 57c7d9e5 | aliguori | close(fd); |
583 | 57c7d9e5 | aliguori | return 0; |
584 | 57c7d9e5 | aliguori | } |
585 | 57c7d9e5 | aliguori | |
586 | 6a0f9e82 | bellard | static void vpc_close(BlockDriverState *bs) |
587 | 6a0f9e82 | bellard | { |
588 | 6a0f9e82 | bellard | BDRVVPCState *s = bs->opaque; |
589 | 6a0f9e82 | bellard | qemu_free(s->pagetable); |
590 | 6a0f9e82 | bellard | #ifdef CACHE
|
591 | 6a0f9e82 | bellard | qemu_free(s->pageentry_u8); |
592 | 6a0f9e82 | bellard | #endif
|
593 | b71d1c2e | aliguori | bdrv_delete(s->hd); |
594 | 6a0f9e82 | bellard | } |
595 | 6a0f9e82 | bellard | |
596 | 0e7e1989 | Kevin Wolf | static QEMUOptionParameter vpc_create_options[] = {
|
597 | db08adf5 | Kevin Wolf | { |
598 | db08adf5 | Kevin Wolf | .name = BLOCK_OPT_SIZE, |
599 | db08adf5 | Kevin Wolf | .type = OPT_SIZE, |
600 | db08adf5 | Kevin Wolf | .help = "Virtual disk size"
|
601 | db08adf5 | Kevin Wolf | }, |
602 | 0e7e1989 | Kevin Wolf | { NULL }
|
603 | 0e7e1989 | Kevin Wolf | }; |
604 | 0e7e1989 | Kevin Wolf | |
605 | 5efa9d5a | Anthony Liguori | static BlockDriver bdrv_vpc = {
|
606 | e60f469c | aurel32 | .format_name = "vpc",
|
607 | e60f469c | aurel32 | .instance_size = sizeof(BDRVVPCState),
|
608 | e60f469c | aurel32 | .bdrv_probe = vpc_probe, |
609 | e60f469c | aurel32 | .bdrv_open = vpc_open, |
610 | e60f469c | aurel32 | .bdrv_read = vpc_read, |
611 | e60f469c | aurel32 | .bdrv_write = vpc_write, |
612 | e60f469c | aurel32 | .bdrv_close = vpc_close, |
613 | e60f469c | aurel32 | .bdrv_create = vpc_create, |
614 | 0e7e1989 | Kevin Wolf | |
615 | 0e7e1989 | Kevin Wolf | .create_options = vpc_create_options, |
616 | 6a0f9e82 | bellard | }; |
617 | 5efa9d5a | Anthony Liguori | |
618 | 5efa9d5a | Anthony Liguori | static void bdrv_vpc_init(void) |
619 | 5efa9d5a | Anthony Liguori | { |
620 | 5efa9d5a | Anthony Liguori | bdrv_register(&bdrv_vpc); |
621 | 5efa9d5a | Anthony Liguori | } |
622 | 5efa9d5a | Anthony Liguori | |
623 | 5efa9d5a | Anthony Liguori | block_init(bdrv_vpc_init); |