2 * Block driver for Conectix/Microsoft Virtual PC images
4 * Copyright (c) 2005 Alex Beregszaszi
5 * Copyright (c) 2009 Kevin Wolf <kwolf@suse.de>
7 * Permission is hereby granted, free of charge, to any person obtaining a copy
8 * of this software and associated documentation files (the "Software"), to deal
9 * in the Software without restriction, including without limitation the rights
10 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 * copies of the Software, and to permit persons to whom the Software is
12 * furnished to do so, subject to the following conditions:
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25 #include "qemu-common.h"
26 #include "block_int.h"
28 /**************************************************************/
30 #define HEADER_SIZE 512
42 char creator
[8]; // "conectix"
46 // Offset of next header structure, 0xFFFFFFFF if none
49 // Seconds since Jan 1, 2000 0:00:00 (UTC)
52 char creator_app
[4]; // "vpc "
55 char creator_os
[4]; // "Wi2k"
66 // Checksum of the Hard Disk Footer ("one's complement of the sum of all
67 // the bytes in the footer without the checksum field")
70 // UUID used to identify a parent hard disk (backing file)
73 uint8_t in_saved_state
;
76 struct vhd_dyndisk_header
{
77 char magic
[8]; // "cxsparse"
79 // Offset of next header structure, 0xFFFFFFFF if none
82 // Offset of the Block Allocation Table (BAT)
83 uint64_t table_offset
;
86 uint32_t max_table_entries
; // 32bit/entry
88 // 2 MB by default, must be a power of two
92 uint8_t parent_uuid
[16];
93 uint32_t parent_timestamp
;
96 // Backing file name (in UTF-16)
97 uint8_t parent_name
[512];
102 uint32_t data_length
;
104 uint64_t data_offset
;
108 typedef struct BDRVVPCState
{
109 BlockDriverState
*hd
;
111 uint8_t footer_buf
[HEADER_SIZE
];
112 uint64_t free_data_block_offset
;
113 int max_table_entries
;
116 uint64_t last_bitmap_offset
;
119 uint32_t bitmap_size
;
122 uint8_t *pageentry_u8
;
123 uint32_t *pageentry_u32
;
124 uint16_t *pageentry_u16
;
126 uint64_t last_bitmap
;
130 static int vpc_probe(const uint8_t *buf
, int buf_size
, const char *filename
)
132 if (buf_size
>= 8 && !strncmp((char *)buf
, "conectix", 8))
137 static int vpc_open(BlockDriverState
*bs
, const char *filename
, int flags
)
139 BDRVVPCState
*s
= bs
->opaque
;
141 struct vhd_footer
* footer
;
142 struct vhd_dyndisk_header
* dyndisk_header
;
143 uint8_t buf
[HEADER_SIZE
];
145 ret
= bdrv_file_open(&s
->hd
, filename
, flags
);
149 if (bdrv_pread(s
->hd
, 0, s
->footer_buf
, HEADER_SIZE
) != HEADER_SIZE
)
152 footer
= (struct vhd_footer
*) s
->footer_buf
;
153 if (strncmp(footer
->creator
, "conectix", 8))
156 // The visible size of a image in Virtual PC depends on the geometry
157 // rather than on the size stored in the footer (the size in the footer
158 // is too large usually)
159 bs
->total_sectors
= (int64_t)
160 be16_to_cpu(footer
->cyls
) * footer
->heads
* footer
->secs_per_cyl
;
162 if (bdrv_pread(s
->hd
, be64_to_cpu(footer
->data_offset
), buf
, HEADER_SIZE
)
166 dyndisk_header
= (struct vhd_dyndisk_header
*) buf
;
168 if (strncmp(dyndisk_header
->magic
, "cxsparse", 8))
172 s
->block_size
= be32_to_cpu(dyndisk_header
->block_size
);
173 s
->bitmap_size
= ((s
->block_size
/ (8 * 512)) + 511) & ~511;
175 s
->max_table_entries
= be32_to_cpu(dyndisk_header
->max_table_entries
);
176 s
->pagetable
= qemu_malloc(s
->max_table_entries
* 4);
180 s
->bat_offset
= be64_to_cpu(dyndisk_header
->table_offset
);
181 if (bdrv_pread(s
->hd
, s
->bat_offset
, s
->pagetable
,
182 s
->max_table_entries
* 4) != s
->max_table_entries
* 4)
185 s
->free_data_block_offset
=
186 (s
->bat_offset
+ (s
->max_table_entries
* 4) + 511) & ~511;
188 for (i
= 0; i
< s
->max_table_entries
; i
++) {
189 be32_to_cpus(&s
->pagetable
[i
]);
190 if (s
->pagetable
[i
] != 0xFFFFFFFF) {
191 int64_t next
= (512 * (int64_t) s
->pagetable
[i
]) +
192 s
->bitmap_size
+ s
->block_size
;
194 if (next
> s
->free_data_block_offset
)
195 s
->free_data_block_offset
= next
;
199 s
->last_bitmap_offset
= (int64_t) -1;
202 s
->pageentry_u8
= qemu_malloc(512);
203 if (!s
->pageentry_u8
)
205 s
->pageentry_u32
= s
->pageentry_u8
;
206 s
->pageentry_u16
= s
->pageentry_u8
;
207 s
->last_pagetable
= -1;
217 * Returns the absolute byte offset of the given sector in the image file.
218 * If the sector is not allocated, -1 is returned instead.
220 * The parameter write must be 1 if the offset will be used for a write
221 * operation (the block bitmaps is updated then), 0 otherwise.
223 static inline int64_t get_sector_offset(BlockDriverState
*bs
,
224 int64_t sector_num
, int write
)
226 BDRVVPCState
*s
= bs
->opaque
;
227 uint64_t offset
= sector_num
* 512;
228 uint64_t bitmap_offset
, block_offset
;
229 uint32_t pagetable_index
, pageentry_index
;
231 pagetable_index
= offset
/ s
->block_size
;
232 pageentry_index
= (offset
% s
->block_size
) / 512;
234 if (pagetable_index
>= s
->max_table_entries
|| s
->pagetable
[pagetable_index
] == 0xffffffff)
235 return -1; // not allocated
237 bitmap_offset
= 512 * s
->pagetable
[pagetable_index
];
238 block_offset
= bitmap_offset
+ s
->bitmap_size
+ (512 * pageentry_index
);
240 // We must ensure that we don't write to any sectors which are marked as
241 // unused in the bitmap. We get away with setting all bits in the block
242 // bitmap each time we write to a new block. This might cause Virtual PC to
243 // miss sparse read optimization, but it's not a problem in terms of
245 if (write
&& (s
->last_bitmap_offset
!= bitmap_offset
)) {
246 uint8_t bitmap
[s
->bitmap_size
];
248 s
->last_bitmap_offset
= bitmap_offset
;
249 memset(bitmap
, 0xff, s
->bitmap_size
);
250 bdrv_pwrite(s
->hd
, bitmap_offset
, bitmap
, s
->bitmap_size
);
253 // printf("sector: %" PRIx64 ", index: %x, offset: %x, bioff: %" PRIx64 ", bloff: %" PRIx64 "\n",
254 // sector_num, pagetable_index, pageentry_index,
255 // bitmap_offset, block_offset);
257 // disabled by reason
260 if (bitmap_offset
!= s
->last_bitmap
)
262 lseek(s
->fd
, bitmap_offset
, SEEK_SET
);
264 s
->last_bitmap
= bitmap_offset
;
266 // Scary! Bitmap is stored as big endian 32bit entries,
267 // while we used to look it up byte by byte
268 read(s
->fd
, s
->pageentry_u8
, 512);
269 for (i
= 0; i
< 128; i
++)
270 be32_to_cpus(&s
->pageentry_u32
[i
]);
273 if ((s
->pageentry_u8
[pageentry_index
/ 8] >> (pageentry_index
% 8)) & 1)
276 lseek(s
->fd
, bitmap_offset
+ (pageentry_index
/ 8), SEEK_SET
);
278 read(s
->fd
, &bitmap_entry
, 1);
280 if ((bitmap_entry
>> (pageentry_index
% 8)) & 1)
281 return -1; // not allocated
289 * Writes the footer to the end of the image file. This is needed when the
290 * file grows as it overwrites the old footer
292 * Returns 0 on success and < 0 on error
294 static int rewrite_footer(BlockDriverState
* bs
)
297 BDRVVPCState
*s
= bs
->opaque
;
298 int64_t offset
= s
->free_data_block_offset
;
300 ret
= bdrv_pwrite(s
->hd
, offset
, s
->footer_buf
, HEADER_SIZE
);
308 * Allocates a new block. This involves writing a new footer and updating
309 * the Block Allocation Table to use the space at the old end of the image
310 * file (overwriting the old footer)
312 * Returns the sectors' offset in the image file on success and < 0 on error
314 static int64_t alloc_block(BlockDriverState
* bs
, int64_t sector_num
)
316 BDRVVPCState
*s
= bs
->opaque
;
318 uint32_t index
, bat_value
;
320 uint8_t bitmap
[s
->bitmap_size
];
322 // Check if sector_num is valid
323 if ((sector_num
< 0) || (sector_num
> bs
->total_sectors
))
326 // Write entry into in-memory BAT
327 index
= (sector_num
* 512) / s
->block_size
;
328 if (s
->pagetable
[index
] != 0xFFFFFFFF)
331 s
->pagetable
[index
] = s
->free_data_block_offset
/ 512;
333 // Initialize the block's bitmap
334 memset(bitmap
, 0xff, s
->bitmap_size
);
335 bdrv_pwrite(s
->hd
, s
->free_data_block_offset
, bitmap
, s
->bitmap_size
);
337 // Write new footer (the old one will be overwritten)
338 s
->free_data_block_offset
+= s
->block_size
+ s
->bitmap_size
;
339 ret
= rewrite_footer(bs
);
343 // Write BAT entry to disk
344 bat_offset
= s
->bat_offset
+ (4 * index
);
345 bat_value
= be32_to_cpu(s
->pagetable
[index
]);
346 ret
= bdrv_pwrite(s
->hd
, bat_offset
, &bat_value
, 4);
350 return get_sector_offset(bs
, sector_num
, 0);
353 s
->free_data_block_offset
-= (s
->block_size
+ s
->bitmap_size
);
357 static int vpc_read(BlockDriverState
*bs
, int64_t sector_num
,
358 uint8_t *buf
, int nb_sectors
)
360 BDRVVPCState
*s
= bs
->opaque
;
364 while (nb_sectors
> 0) {
365 offset
= get_sector_offset(bs
, sector_num
, 0);
370 ret
= bdrv_pread(s
->hd
, offset
, buf
, 512);
382 static int vpc_write(BlockDriverState
*bs
, int64_t sector_num
,
383 const uint8_t *buf
, int nb_sectors
)
385 BDRVVPCState
*s
= bs
->opaque
;
389 while (nb_sectors
> 0) {
390 offset
= get_sector_offset(bs
, sector_num
, 1);
393 offset
= alloc_block(bs
, sector_num
);
398 ret
= bdrv_pwrite(s
->hd
, offset
, buf
, 512);
410 static void vpc_close(BlockDriverState
*bs
)
412 BDRVVPCState
*s
= bs
->opaque
;
413 qemu_free(s
->pagetable
);
415 qemu_free(s
->pageentry_u8
);
420 BlockDriver bdrv_vpc
= {
422 sizeof(BDRVVPCState
),