From cf0d081ab25441d00c46f7756d6716721b617e91 Mon Sep 17 00:00:00 2001 From: maxpat78 Date: Fri, 20 Oct 2023 15:13:07 +0200 Subject: [PATCH] Fixes "zero optimization" bug The full VHD chain is now checked for absence of a block to keep virtual --- include/bios_disk.h | 1 + src/ints/bios_vhd.cpp | 13 +++++++++++-- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/include/bios_disk.h b/include/bios_disk.h index d94cc806f57..383260584b6 100644 --- a/include/bios_disk.h +++ b/include/bios_disk.h @@ -385,6 +385,7 @@ class imageDiskVHD : public imageDisk { virtual bool loadBlock(const uint32_t blockNumber); static bool convert_UTF16_for_fopen(std::string &string, const void* data, const uint32_t dataLength); bool is_zeroed_sector(const void* data); + bool is_block_allocated(uint32_t blockNumber); imageDisk* parentDisk = NULL; imageDisk* fixedDisk = NULL; diff --git a/src/ints/bios_vhd.cpp b/src/ints/bios_vhd.cpp index be3ac2377a4..4fe8b2bd1ba 100644 --- a/src/ints/bios_vhd.cpp +++ b/src/ints/bios_vhd.cpp @@ -337,14 +337,23 @@ bool imageDiskVHD::is_zeroed_sector(const void* data) { return true; } +bool imageDiskVHD::is_block_allocated(uint32_t blockNumber) { + if(currentBlockAllocated) return true; + if(parentDisk && ((imageDiskVHD*) parentDisk)->is_block_allocated(blockNumber)) return true; + return false; +} + uint8_t imageDiskVHD::Write_AbsoluteSector(uint32_t sectnum, const void * data) { if(vhdType == VHD_TYPE_FIXED) return fixedDisk->Write_AbsoluteSector(sectnum, data); uint32_t blockNumber = sectnum / sectorsPerBlock; uint32_t sectorOffset = sectnum % sectorsPerBlock; if (!loadBlock(blockNumber)) return 0x05; //can't load block if (!currentBlockAllocated) { - //an unallocated block is kept virtualized until zeroed - if(is_zeroed_sector(data)) return 0; + //an unallocated block is kept virtual until zeroed + if(is_zeroed_sector(data)) { + if(vhdType != VHD_TYPE_DIFFERENCING) return 0; + if(!is_block_allocated(blockNumber)) return 0; + } if (!copiedFooter) { //write backup of footer at start of file (should already exist, but we never checked to be sure it is readable or matches the footer we used)