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