Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Detects possible FAT geometry in fixed VHDs too #4961

Merged
merged 1 commit into from
Apr 14, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion src/ints/bios_vhd.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -127,13 +127,20 @@ imageDiskVHD::ErrorCodes imageDiskVHD::Open(const char* fileName, const bool rea
uint8_t buf[512];
if(fseeko64(file, 0, SEEK_SET)) { fclose(file); return INVALID_DATA; }
if(fread(&buf, sizeof(uint8_t), 512, file) != 512) { fclose(file); return INVALID_DATA; }
//detect real DOS geometry (MBR, BPB or default X-255-63)
imageDiskVHD::scanMBR(buf, sizes, footer.currentSize);
vhd->cylinders = sizes[3];
vhd->heads = sizes[2];
vhd->sectors = sizes[1];
vhd->fixedDisk = new imageDisk(file, fileName, vhd->cylinders, vhd->heads, vhd->sectors, 512, true);
//now possible FAT geometry can be detected (in Windows 11, those two can differ!)
vhd->DetectGeometry(sizes);
vhd->cylinders = sizes[3];
vhd->heads = sizes[2];
vhd->sectors = sizes[1];
vhd->fixedDisk = new imageDisk(file, fileName, vhd->cylinders, vhd->heads, vhd->sectors, 512, true);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does calling new imageDisk() twice safely deletes the previous one?
I think better to check sizes[] and DetectGeometry before new imageDisk

Copy link
Contributor Author

@maxpat78 maxpat78 Apr 13, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Obviously, it does not (deleting caused an obscure exception). [*]

DetectGeometry actually requires a valid disk image internally to properly read VHD sectors, that's the reason why I avoided that call in previous commits (having tought at least Windows 1x set MBR CHS == BPB CHS).

Perhaps I'll find another way, if you haven't suggestions.

EDIT:
[*] The exception cause: delete vhd->fixedDisk would call fclose(dskimg), where dskimg is an alias for file object, required by following new imageDisk call.

*disk = vhd;
return !readOnly && roflag ? UNSUPPORTED_WRITE : OPEN_SUCCESS;
return !readOnly && roflag ? UNSUPPORTED_WRITE : OPEN_SUCCESS;
}

//if not dynamic or differencing, fail
Expand Down