Allow large VHD files (#79)

This PR allows larger VHD image files. The size in question doesn't
necessarily mean the size of the VHD file on disk. The size is the total
size of the image emulated. This total size is in question.

Without this patch, the total size allowed is 65535 * 16 * 255, or
roughly 32gig.
With this patch, the total size is calculated by who the creator of the
image is, what the CHS values are, and possibly a total size of up to a
limit of 2TB.

Since the original code was ported from QEMU, I ported an updated QEMU
code snippet.

https://gitlab.com/qemu-project/qemu/-/blob/master/block/vpc.c?ref_type=heads#L305

This PR now allows VHD image files with a total size emulated less than
or equal to 2TB.
This commit is contained in:
Benjamin David Lunt 2023-09-18 22:04:32 -07:00 committed by GitHub
parent b46fc681d2
commit 74ed550334
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 39 additions and 6 deletions

View File

@ -166,11 +166,44 @@ int vpc_image_t::open(const char* _pathname, int flags)
cylinders = be16_to_cpu(footer->cyls);
heads = footer->heads;
spt = footer->secs_per_cyl;
sector_count = (Bit64u)(cylinders * heads * spt);
sector_count = (Bit64u)((Bit64u)cylinders * heads * spt);
sect_size = 512;
hd_size = sector_count * sect_size;
if (sector_count >= 65535 * 16 * 255) {
/* Microsoft Virtual PC and Microsoft Hyper-V produce and read
* VHD image sizes differently. VPC will rely on CHS geometry,
* while Hyper-V and disk2vhd use the size specified in the footer.
*
* We use a couple of approaches to try and determine the correct method:
* look at the Creator App field, and look for images that have CHS
* geometry that is the maximum value.
*
* If the CHS geometry is the maximum CHS geometry, then we assume that
* the size is the footer->size to avoid truncation. Otherwise,
* we follow the table based on footer->creator_app:
*
* Known creator apps:
* 'vpc ' : CHS Virtual PC (uses disk geometry)
* 'qemu' : CHS QEMU (uses disk geometry)
* 'qem2' : current_size QEMU (uses current_size)
* 'win ' : current_size Hyper-V
* 'd2v ' : current_size Disk2vhd
* 'tap\0' : current_size XenServer
* 'CTXS' : current_size XenConverter
*/
bool use_chs = (!!strncmp(footer->creator_app, "win ", 4) &&
!!strncmp(footer->creator_app, "qem2", 4) &&
!!strncmp(footer->creator_app, "d2v ", 4) &&
!!strncmp(footer->creator_app, "CTXS", 4) &&
!!memcmp (footer->creator_app, "tap", 4));
if (!use_chs || sector_count == (65535 * 16 * 255)) {
sector_count = be64_to_cpu(footer->size) / sect_size;
}
hd_size = sector_count * sect_size;
/* Allow a maximum disk size of 2040 GiB */
if (sector_count > 0xff000000) {
BX_ERROR(("VHD Emulated Image too large. " FMT_LL "i > 4278190080", sector_count));
bx_close_image(fd, pathname);
return -EFBIG;
}

View File

@ -69,7 +69,7 @@ typedef
__declspec(align(1))
#endif
struct vhd_footer_t {
Bit8u creator[8]; // "conectix"
char creator[8]; // "conectix"
Bit32u features;
Bit32u version;
@ -79,10 +79,10 @@ struct vhd_footer_t {
// Seconds since Jan 1, 2000 0:00:00 (UTC)
Bit32u timestamp;
Bit8u creator_app[4]; // "vpc "
char creator_app[4]; // "vpc "
Bit16u major;
Bit16u minor;
Bit8u creator_os[4]; // "Wi2k"
char creator_os[4]; // "Wi2k"
Bit64u orig_size;
Bit64u size;