vpc.c (cc2040f8c22b933df6f3be11a1bf7a6b55327031) | vpc.c (66f82ceed6781261c09e65fb440ca76842fd0500) |
---|---|
1/* 2 * Block driver for Connectix / Microsoft Virtual PC images 3 * 4 * Copyright (c) 2005 Alex Beregszaszi 5 * Copyright (c) 2009 Kevin Wolf <kwolf@suse.de> 6 * 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 --- 136 unchanged lines hidden (view full) --- 145 146static int vpc_probe(const uint8_t *buf, int buf_size, const char *filename) 147{ 148 if (buf_size >= 8 && !strncmp((char *)buf, "conectix", 8)) 149 return 100; 150 return 0; 151} 152 | 1/* 2 * Block driver for Connectix / Microsoft Virtual PC images 3 * 4 * Copyright (c) 2005 Alex Beregszaszi 5 * Copyright (c) 2009 Kevin Wolf <kwolf@suse.de> 6 * 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 --- 136 unchanged lines hidden (view full) --- 145 146static int vpc_probe(const uint8_t *buf, int buf_size, const char *filename) 147{ 148 if (buf_size >= 8 && !strncmp((char *)buf, "conectix", 8)) 149 return 100; 150 return 0; 151} 152 |
153static int vpc_open(BlockDriverState *bs, const char *filename, int flags) | 153static int vpc_open(BlockDriverState *bs, int flags) |
154{ 155 BDRVVPCState *s = bs->opaque; | 154{ 155 BDRVVPCState *s = bs->opaque; |
156 int ret, i; | 156 int i; |
157 struct vhd_footer* footer; 158 struct vhd_dyndisk_header* dyndisk_header; 159 uint8_t buf[HEADER_SIZE]; 160 uint32_t checksum; 161 | 157 struct vhd_footer* footer; 158 struct vhd_dyndisk_header* dyndisk_header; 159 uint8_t buf[HEADER_SIZE]; 160 uint32_t checksum; 161 |
162 ret = bdrv_file_open(&s->hd, filename, flags); 163 if (ret < 0) 164 return ret; 165 166 if (bdrv_pread(s->hd, 0, s->footer_buf, HEADER_SIZE) != HEADER_SIZE) | 162 if (bdrv_pread(bs->file, 0, s->footer_buf, HEADER_SIZE) != HEADER_SIZE) |
167 goto fail; 168 169 footer = (struct vhd_footer*) s->footer_buf; 170 if (strncmp(footer->creator, "conectix", 8)) 171 goto fail; 172 173 checksum = be32_to_cpu(footer->checksum); 174 footer->checksum = 0; 175 if (vpc_checksum(s->footer_buf, HEADER_SIZE) != checksum) 176 fprintf(stderr, "block-vpc: The header checksum of '%s' is " | 163 goto fail; 164 165 footer = (struct vhd_footer*) s->footer_buf; 166 if (strncmp(footer->creator, "conectix", 8)) 167 goto fail; 168 169 checksum = be32_to_cpu(footer->checksum); 170 footer->checksum = 0; 171 if (vpc_checksum(s->footer_buf, HEADER_SIZE) != checksum) 172 fprintf(stderr, "block-vpc: The header checksum of '%s' is " |
177 "incorrect.\n", filename); | 173 "incorrect.\n", bs->filename); |
178 179 // The visible size of a image in Virtual PC depends on the geometry 180 // rather than on the size stored in the footer (the size in the footer 181 // is too large usually) 182 bs->total_sectors = (int64_t) 183 be16_to_cpu(footer->cyls) * footer->heads * footer->secs_per_cyl; 184 | 174 175 // The visible size of a image in Virtual PC depends on the geometry 176 // rather than on the size stored in the footer (the size in the footer 177 // is too large usually) 178 bs->total_sectors = (int64_t) 179 be16_to_cpu(footer->cyls) * footer->heads * footer->secs_per_cyl; 180 |
185 if (bdrv_pread(s->hd, be64_to_cpu(footer->data_offset), buf, HEADER_SIZE) | 181 if (bdrv_pread(bs->file, be64_to_cpu(footer->data_offset), buf, HEADER_SIZE) |
186 != HEADER_SIZE) 187 goto fail; 188 189 dyndisk_header = (struct vhd_dyndisk_header*) buf; 190 191 if (strncmp(dyndisk_header->magic, "cxsparse", 8)) 192 goto fail; 193 194 195 s->block_size = be32_to_cpu(dyndisk_header->block_size); 196 s->bitmap_size = ((s->block_size / (8 * 512)) + 511) & ~511; 197 198 s->max_table_entries = be32_to_cpu(dyndisk_header->max_table_entries); 199 s->pagetable = qemu_malloc(s->max_table_entries * 4); 200 201 s->bat_offset = be64_to_cpu(dyndisk_header->table_offset); | 182 != HEADER_SIZE) 183 goto fail; 184 185 dyndisk_header = (struct vhd_dyndisk_header*) buf; 186 187 if (strncmp(dyndisk_header->magic, "cxsparse", 8)) 188 goto fail; 189 190 191 s->block_size = be32_to_cpu(dyndisk_header->block_size); 192 s->bitmap_size = ((s->block_size / (8 * 512)) + 511) & ~511; 193 194 s->max_table_entries = be32_to_cpu(dyndisk_header->max_table_entries); 195 s->pagetable = qemu_malloc(s->max_table_entries * 4); 196 197 s->bat_offset = be64_to_cpu(dyndisk_header->table_offset); |
202 if (bdrv_pread(s->hd, s->bat_offset, s->pagetable, | 198 if (bdrv_pread(bs->file, s->bat_offset, s->pagetable, |
203 s->max_table_entries * 4) != s->max_table_entries * 4) 204 goto fail; 205 206 s->free_data_block_offset = 207 (s->bat_offset + (s->max_table_entries * 4) + 511) & ~511; 208 209 for (i = 0; i < s->max_table_entries; i++) { 210 be32_to_cpus(&s->pagetable[i]); --- 12 unchanged lines hidden (view full) --- 223 s->pageentry_u8 = qemu_malloc(512); 224 s->pageentry_u32 = s->pageentry_u8; 225 s->pageentry_u16 = s->pageentry_u8; 226 s->last_pagetable = -1; 227#endif 228 229 return 0; 230 fail: | 199 s->max_table_entries * 4) != s->max_table_entries * 4) 200 goto fail; 201 202 s->free_data_block_offset = 203 (s->bat_offset + (s->max_table_entries * 4) + 511) & ~511; 204 205 for (i = 0; i < s->max_table_entries; i++) { 206 be32_to_cpus(&s->pagetable[i]); --- 12 unchanged lines hidden (view full) --- 219 s->pageentry_u8 = qemu_malloc(512); 220 s->pageentry_u32 = s->pageentry_u8; 221 s->pageentry_u16 = s->pageentry_u8; 222 s->last_pagetable = -1; 223#endif 224 225 return 0; 226 fail: |
231 bdrv_delete(s->hd); | |
232 return -1; 233} 234 235/* 236 * Returns the absolute byte offset of the given sector in the image file. 237 * If the sector is not allocated, -1 is returned instead. 238 * 239 * The parameter write must be 1 if the offset will be used for a write --- 21 unchanged lines hidden (view full) --- 261 // bitmap each time we write to a new block. This might cause Virtual PC to 262 // miss sparse read optimization, but it's not a problem in terms of 263 // correctness. 264 if (write && (s->last_bitmap_offset != bitmap_offset)) { 265 uint8_t bitmap[s->bitmap_size]; 266 267 s->last_bitmap_offset = bitmap_offset; 268 memset(bitmap, 0xff, s->bitmap_size); | 227 return -1; 228} 229 230/* 231 * Returns the absolute byte offset of the given sector in the image file. 232 * If the sector is not allocated, -1 is returned instead. 233 * 234 * The parameter write must be 1 if the offset will be used for a write --- 21 unchanged lines hidden (view full) --- 256 // bitmap each time we write to a new block. This might cause Virtual PC to 257 // miss sparse read optimization, but it's not a problem in terms of 258 // correctness. 259 if (write && (s->last_bitmap_offset != bitmap_offset)) { 260 uint8_t bitmap[s->bitmap_size]; 261 262 s->last_bitmap_offset = bitmap_offset; 263 memset(bitmap, 0xff, s->bitmap_size); |
269 bdrv_pwrite(s->hd, bitmap_offset, bitmap, s->bitmap_size); | 264 bdrv_pwrite(bs->file, bitmap_offset, bitmap, s->bitmap_size); |
270 } 271 272// printf("sector: %" PRIx64 ", index: %x, offset: %x, bioff: %" PRIx64 ", bloff: %" PRIx64 "\n", 273// sector_num, pagetable_index, pageentry_index, 274// bitmap_offset, block_offset); 275 276// disabled by reason 277#if 0 --- 33 unchanged lines hidden (view full) --- 311 * Returns 0 on success and < 0 on error 312 */ 313static int rewrite_footer(BlockDriverState* bs) 314{ 315 int ret; 316 BDRVVPCState *s = bs->opaque; 317 int64_t offset = s->free_data_block_offset; 318 | 265 } 266 267// printf("sector: %" PRIx64 ", index: %x, offset: %x, bioff: %" PRIx64 ", bloff: %" PRIx64 "\n", 268// sector_num, pagetable_index, pageentry_index, 269// bitmap_offset, block_offset); 270 271// disabled by reason 272#if 0 --- 33 unchanged lines hidden (view full) --- 306 * Returns 0 on success and < 0 on error 307 */ 308static int rewrite_footer(BlockDriverState* bs) 309{ 310 int ret; 311 BDRVVPCState *s = bs->opaque; 312 int64_t offset = s->free_data_block_offset; 313 |
319 ret = bdrv_pwrite(s->hd, offset, s->footer_buf, HEADER_SIZE); | 314 ret = bdrv_pwrite(bs->file, offset, s->footer_buf, HEADER_SIZE); |
320 if (ret < 0) 321 return ret; 322 323 return 0; 324} 325 326/* 327 * Allocates a new block. This involves writing a new footer and updating --- 18 unchanged lines hidden (view full) --- 346 index = (sector_num * 512) / s->block_size; 347 if (s->pagetable[index] != 0xFFFFFFFF) 348 return -1; 349 350 s->pagetable[index] = s->free_data_block_offset / 512; 351 352 // Initialize the block's bitmap 353 memset(bitmap, 0xff, s->bitmap_size); | 315 if (ret < 0) 316 return ret; 317 318 return 0; 319} 320 321/* 322 * Allocates a new block. This involves writing a new footer and updating --- 18 unchanged lines hidden (view full) --- 341 index = (sector_num * 512) / s->block_size; 342 if (s->pagetable[index] != 0xFFFFFFFF) 343 return -1; 344 345 s->pagetable[index] = s->free_data_block_offset / 512; 346 347 // Initialize the block's bitmap 348 memset(bitmap, 0xff, s->bitmap_size); |
354 bdrv_pwrite(s->hd, s->free_data_block_offset, bitmap, s->bitmap_size); | 349 bdrv_pwrite(bs->file, s->free_data_block_offset, bitmap, s->bitmap_size); |
355 356 // Write new footer (the old one will be overwritten) 357 s->free_data_block_offset += s->block_size + s->bitmap_size; 358 ret = rewrite_footer(bs); 359 if (ret < 0) 360 goto fail; 361 362 // Write BAT entry to disk 363 bat_offset = s->bat_offset + (4 * index); 364 bat_value = be32_to_cpu(s->pagetable[index]); | 350 351 // Write new footer (the old one will be overwritten) 352 s->free_data_block_offset += s->block_size + s->bitmap_size; 353 ret = rewrite_footer(bs); 354 if (ret < 0) 355 goto fail; 356 357 // Write BAT entry to disk 358 bat_offset = s->bat_offset + (4 * index); 359 bat_value = be32_to_cpu(s->pagetable[index]); |
365 ret = bdrv_pwrite(s->hd, bat_offset, &bat_value, 4); | 360 ret = bdrv_pwrite(bs->file, bat_offset, &bat_value, 4); |
366 if (ret < 0) 367 goto fail; 368 369 return get_sector_offset(bs, sector_num, 0); 370 371fail: 372 s->free_data_block_offset -= (s->block_size + s->bitmap_size); 373 return -1; 374} 375 376static int vpc_read(BlockDriverState *bs, int64_t sector_num, 377 uint8_t *buf, int nb_sectors) 378{ | 361 if (ret < 0) 362 goto fail; 363 364 return get_sector_offset(bs, sector_num, 0); 365 366fail: 367 s->free_data_block_offset -= (s->block_size + s->bitmap_size); 368 return -1; 369} 370 371static int vpc_read(BlockDriverState *bs, int64_t sector_num, 372 uint8_t *buf, int nb_sectors) 373{ |
379 BDRVVPCState *s = bs->opaque; | |
380 int ret; 381 int64_t offset; 382 383 while (nb_sectors > 0) { 384 offset = get_sector_offset(bs, sector_num, 0); 385 386 if (offset == -1) { 387 memset(buf, 0, 512); 388 } else { | 374 int ret; 375 int64_t offset; 376 377 while (nb_sectors > 0) { 378 offset = get_sector_offset(bs, sector_num, 0); 379 380 if (offset == -1) { 381 memset(buf, 0, 512); 382 } else { |
389 ret = bdrv_pread(s->hd, offset, buf, 512); | 383 ret = bdrv_pread(bs->file, offset, buf, 512); |
390 if (ret != 512) 391 return -1; 392 } 393 394 nb_sectors--; 395 sector_num++; 396 buf += 512; 397 } 398 return 0; 399} 400 401static int vpc_write(BlockDriverState *bs, int64_t sector_num, 402 const uint8_t *buf, int nb_sectors) 403{ | 384 if (ret != 512) 385 return -1; 386 } 387 388 nb_sectors--; 389 sector_num++; 390 buf += 512; 391 } 392 return 0; 393} 394 395static int vpc_write(BlockDriverState *bs, int64_t sector_num, 396 const uint8_t *buf, int nb_sectors) 397{ |
404 BDRVVPCState *s = bs->opaque; | |
405 int64_t offset; 406 int ret; 407 408 while (nb_sectors > 0) { 409 offset = get_sector_offset(bs, sector_num, 1); 410 411 if (offset == -1) { 412 offset = alloc_block(bs, sector_num); 413 if (offset < 0) 414 return -1; 415 } 416 | 398 int64_t offset; 399 int ret; 400 401 while (nb_sectors > 0) { 402 offset = get_sector_offset(bs, sector_num, 1); 403 404 if (offset == -1) { 405 offset = alloc_block(bs, sector_num); 406 if (offset < 0) 407 return -1; 408 } 409 |
417 ret = bdrv_pwrite(s->hd, offset, buf, 512); | 410 ret = bdrv_pwrite(bs->file, offset, buf, 512); |
418 if (ret != 512) 419 return -1; 420 421 nb_sectors--; 422 sector_num++; 423 buf += 512; 424 } 425 --- 159 unchanged lines hidden (view full) --- 585 586static void vpc_close(BlockDriverState *bs) 587{ 588 BDRVVPCState *s = bs->opaque; 589 qemu_free(s->pagetable); 590#ifdef CACHE 591 qemu_free(s->pageentry_u8); 592#endif | 411 if (ret != 512) 412 return -1; 413 414 nb_sectors--; 415 sector_num++; 416 buf += 512; 417 } 418 --- 159 unchanged lines hidden (view full) --- 578 579static void vpc_close(BlockDriverState *bs) 580{ 581 BDRVVPCState *s = bs->opaque; 582 qemu_free(s->pagetable); 583#ifdef CACHE 584 qemu_free(s->pageentry_u8); 585#endif |
593 bdrv_delete(s->hd); | |
594} 595 596static QEMUOptionParameter vpc_create_options[] = { 597 { 598 .name = BLOCK_OPT_SIZE, 599 .type = OPT_SIZE, 600 .help = "Virtual disk size" 601 }, --- 22 unchanged lines hidden --- | 586} 587 588static QEMUOptionParameter vpc_create_options[] = { 589 { 590 .name = BLOCK_OPT_SIZE, 591 .type = OPT_SIZE, 592 .help = "Virtual disk size" 593 }, --- 22 unchanged lines hidden --- |