2018-07-23 23:25:43 +01:00
|
|
|
|
// /***************************************************************************
|
2020-02-27 12:31:25 +00:00
|
|
|
|
// Aaru Data Preservation Suite
|
2016-08-29 03:38:18 +01:00
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
|
//
|
2018-07-23 23:25:43 +01:00
|
|
|
|
// Filename : Read.cs
|
2016-08-29 03:38:18 +01:00
|
|
|
|
// Author(s) : Natalia Portillo <claunia@claunia.com>
|
|
|
|
|
|
//
|
2017-12-19 03:50:57 +00:00
|
|
|
|
// Component : Disk image plugins.
|
2016-08-29 03:38:18 +01:00
|
|
|
|
//
|
|
|
|
|
|
// --[ Description ] ----------------------------------------------------------
|
|
|
|
|
|
//
|
2018-07-23 23:25:43 +01:00
|
|
|
|
// Reads Microsoft Hyper-V disk images.
|
2016-08-29 03:38:18 +01:00
|
|
|
|
//
|
|
|
|
|
|
// --[ License ] --------------------------------------------------------------
|
|
|
|
|
|
//
|
|
|
|
|
|
// This library is free software; you can redistribute it and/or modify
|
|
|
|
|
|
// it under the terms of the GNU Lesser General Public License as
|
|
|
|
|
|
// published by the Free Software Foundation; either version 2.1 of the
|
|
|
|
|
|
// License, or (at your option) any later version.
|
|
|
|
|
|
//
|
|
|
|
|
|
// This library is distributed in the hope that it will be useful, but
|
|
|
|
|
|
// WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
|
|
// Lesser General Public License for more details.
|
|
|
|
|
|
//
|
|
|
|
|
|
// You should have received a copy of the GNU Lesser General Public
|
|
|
|
|
|
// License along with this library; if not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
//
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
2020-01-03 17:51:30 +00:00
|
|
|
|
// Copyright © 2011-2020 Natalia Portillo
|
2016-08-29 03:38:18 +01:00
|
|
|
|
// ****************************************************************************/
|
|
|
|
|
|
|
|
|
|
|
|
using System;
|
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
using System.IO;
|
|
|
|
|
|
using System.Text;
|
2020-02-27 00:33:26 +00:00
|
|
|
|
using Aaru.CommonTypes;
|
|
|
|
|
|
using Aaru.CommonTypes.Enums;
|
|
|
|
|
|
using Aaru.CommonTypes.Exceptions;
|
|
|
|
|
|
using Aaru.CommonTypes.Interfaces;
|
|
|
|
|
|
using Aaru.Console;
|
|
|
|
|
|
using Aaru.Helpers;
|
2016-08-29 03:38:18 +01:00
|
|
|
|
|
2020-02-27 00:33:26 +00:00
|
|
|
|
namespace Aaru.DiscImages
|
2016-08-29 03:38:18 +01:00
|
|
|
|
{
|
2018-07-23 23:25:43 +01:00
|
|
|
|
public partial class Vhdx
|
2016-08-29 03:38:18 +01:00
|
|
|
|
{
|
2017-12-28 19:56:36 +00:00
|
|
|
|
public bool Open(IFilter imageFilter)
|
2016-08-29 03:38:18 +01:00
|
|
|
|
{
|
2016-09-05 17:37:31 +01:00
|
|
|
|
Stream stream = imageFilter.GetDataForkStream();
|
2016-08-29 03:38:18 +01:00
|
|
|
|
stream.Seek(0, SeekOrigin.Begin);
|
|
|
|
|
|
|
2020-02-29 18:03:35 +00:00
|
|
|
|
if(stream.Length < 512)
|
|
|
|
|
|
return false;
|
2016-08-29 03:38:18 +01:00
|
|
|
|
|
2019-03-01 07:35:22 +00:00
|
|
|
|
byte[] vhdxIdB = new byte[Marshal.SizeOf<VhdxIdentifier>()];
|
|
|
|
|
|
stream.Read(vhdxIdB, 0, Marshal.SizeOf<VhdxIdentifier>());
|
|
|
|
|
|
vhdxId = Marshal.ByteArrayToStructureLittleEndian<VhdxIdentifier>(vhdxIdB);
|
2016-08-29 03:38:18 +01:00
|
|
|
|
|
2020-02-29 18:03:35 +00:00
|
|
|
|
if(vhdxId.signature != VHDX_SIGNATURE)
|
|
|
|
|
|
return false;
|
2016-08-29 03:38:18 +01:00
|
|
|
|
|
2017-12-26 06:05:12 +00:00
|
|
|
|
imageInfo.Application = Encoding.Unicode.GetString(vhdxId.creator);
|
2016-08-29 03:38:18 +01:00
|
|
|
|
|
|
|
|
|
|
stream.Seek(64 * 1024, SeekOrigin.Begin);
|
2019-03-01 07:35:22 +00:00
|
|
|
|
byte[] vHdrB = new byte[Marshal.SizeOf<VhdxHeader>()];
|
|
|
|
|
|
stream.Read(vHdrB, 0, Marshal.SizeOf<VhdxHeader>());
|
|
|
|
|
|
vHdr = Marshal.ByteArrayToStructureLittleEndian<VhdxHeader>(vHdrB);
|
2016-08-29 03:38:18 +01:00
|
|
|
|
|
2017-12-20 17:15:26 +00:00
|
|
|
|
if(vHdr.Signature != VHDX_HEADER_SIG)
|
2016-08-29 03:38:18 +01:00
|
|
|
|
{
|
|
|
|
|
|
stream.Seek(128 * 1024, SeekOrigin.Begin);
|
2019-03-01 07:35:22 +00:00
|
|
|
|
vHdrB = new byte[Marshal.SizeOf<VhdxHeader>()];
|
|
|
|
|
|
stream.Read(vHdrB, 0, Marshal.SizeOf<VhdxHeader>());
|
|
|
|
|
|
vHdr = Marshal.ByteArrayToStructureLittleEndian<VhdxHeader>(vHdrB);
|
2016-08-29 03:38:18 +01:00
|
|
|
|
|
2020-02-29 18:03:35 +00:00
|
|
|
|
if(vHdr.Signature != VHDX_HEADER_SIG)
|
|
|
|
|
|
throw new ImageNotSupportedException("VHDX header not found");
|
2016-08-29 03:38:18 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
stream.Seek(192 * 1024, SeekOrigin.Begin);
|
2019-03-01 07:35:22 +00:00
|
|
|
|
byte[] vRegTableB = new byte[Marshal.SizeOf<VhdxRegionTableHeader>()];
|
|
|
|
|
|
stream.Read(vRegTableB, 0, Marshal.SizeOf<VhdxRegionTableHeader>());
|
|
|
|
|
|
vRegHdr = Marshal.ByteArrayToStructureLittleEndian<VhdxRegionTableHeader>(vRegTableB);
|
2016-08-29 03:38:18 +01:00
|
|
|
|
|
2017-12-20 17:15:26 +00:00
|
|
|
|
if(vRegHdr.signature != VHDX_REGION_SIG)
|
2016-08-29 03:38:18 +01:00
|
|
|
|
{
|
|
|
|
|
|
stream.Seek(256 * 1024, SeekOrigin.Begin);
|
2019-03-01 07:35:22 +00:00
|
|
|
|
vRegTableB = new byte[Marshal.SizeOf<VhdxRegionTableHeader>()];
|
|
|
|
|
|
stream.Read(vRegTableB, 0, Marshal.SizeOf<VhdxRegionTableHeader>());
|
|
|
|
|
|
vRegHdr = Marshal.ByteArrayToStructureLittleEndian<VhdxRegionTableHeader>(vRegTableB);
|
2016-08-29 03:38:18 +01:00
|
|
|
|
|
2017-12-20 17:15:26 +00:00
|
|
|
|
if(vRegHdr.signature != VHDX_REGION_SIG)
|
2016-08-29 03:38:18 +01:00
|
|
|
|
throw new ImageNotSupportedException("VHDX region table not found");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2017-12-20 17:15:26 +00:00
|
|
|
|
vRegs = new VhdxRegionTableEntry[vRegHdr.entries];
|
2020-02-29 18:03:35 +00:00
|
|
|
|
|
2016-08-29 03:38:18 +01:00
|
|
|
|
for(int i = 0; i < vRegs.Length; i++)
|
|
|
|
|
|
{
|
2019-03-01 07:35:22 +00:00
|
|
|
|
byte[] vRegB = new byte[System.Runtime.InteropServices.Marshal.SizeOf(vRegs[i])];
|
|
|
|
|
|
stream.Read(vRegB, 0, System.Runtime.InteropServices.Marshal.SizeOf(vRegs[i]));
|
|
|
|
|
|
vRegs[i] = Marshal.ByteArrayToStructureLittleEndian<VhdxRegionTableEntry>(vRegB);
|
2016-08-29 03:38:18 +01:00
|
|
|
|
|
2018-06-22 08:08:38 +01:00
|
|
|
|
if(vRegs[i].guid == batGuid)
|
|
|
|
|
|
batOffset = (long)vRegs[i].offset;
|
2018-01-28 20:29:46 +00:00
|
|
|
|
else if(vRegs[i].guid == metadataGuid)
|
|
|
|
|
|
metadataOffset = (long)vRegs[i].offset;
|
2017-12-20 17:15:26 +00:00
|
|
|
|
else if((vRegs[i].flags & REGION_FLAGS_REQUIRED) == REGION_FLAGS_REQUIRED)
|
2017-12-19 20:33:03 +00:00
|
|
|
|
throw new
|
2017-12-21 17:58:51 +00:00
|
|
|
|
ImageNotSupportedException($"Found unsupported and required region Guid {vRegs[i].guid}, not proceeding with image.");
|
2016-08-29 03:38:18 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2020-02-29 18:03:35 +00:00
|
|
|
|
if(batOffset == 0)
|
|
|
|
|
|
throw new Exception("BAT not found, cannot continue.");
|
2016-08-29 03:38:18 +01:00
|
|
|
|
|
2020-02-29 18:03:35 +00:00
|
|
|
|
if(metadataOffset == 0)
|
|
|
|
|
|
throw new Exception("Metadata not found, cannot continue.");
|
2016-08-29 03:38:18 +01:00
|
|
|
|
|
|
|
|
|
|
uint fileParamsOff = 0, vdSizeOff = 0, p83Off = 0, logOff = 0, physOff = 0, parentOff = 0;
|
|
|
|
|
|
|
|
|
|
|
|
stream.Seek(metadataOffset, SeekOrigin.Begin);
|
2019-03-01 07:35:22 +00:00
|
|
|
|
byte[] metTableB = new byte[Marshal.SizeOf<VhdxMetadataTableHeader>()];
|
|
|
|
|
|
stream.Read(metTableB, 0, Marshal.SizeOf<VhdxMetadataTableHeader>());
|
|
|
|
|
|
vMetHdr = Marshal.ByteArrayToStructureLittleEndian<VhdxMetadataTableHeader>(metTableB);
|
2016-08-29 03:38:18 +01:00
|
|
|
|
|
2017-12-20 17:15:26 +00:00
|
|
|
|
vMets = new VhdxMetadataTableEntry[vMetHdr.entries];
|
2020-02-29 18:03:35 +00:00
|
|
|
|
|
2016-08-29 03:38:18 +01:00
|
|
|
|
for(int i = 0; i < vMets.Length; i++)
|
|
|
|
|
|
{
|
2019-03-01 07:35:22 +00:00
|
|
|
|
byte[] vMetB = new byte[System.Runtime.InteropServices.Marshal.SizeOf(vMets[i])];
|
|
|
|
|
|
stream.Read(vMetB, 0, System.Runtime.InteropServices.Marshal.SizeOf(vMets[i]));
|
|
|
|
|
|
vMets[i] = Marshal.ByteArrayToStructureLittleEndian<VhdxMetadataTableEntry>(vMetB);
|
2016-08-29 03:38:18 +01:00
|
|
|
|
|
2018-06-22 08:08:38 +01:00
|
|
|
|
if(vMets[i].itemId == fileParametersGuid)
|
|
|
|
|
|
fileParamsOff = vMets[i].offset;
|
2018-01-28 20:29:46 +00:00
|
|
|
|
else if(vMets[i].itemId == virtualDiskSizeGuid)
|
|
|
|
|
|
vdSizeOff = vMets[i].offset;
|
|
|
|
|
|
else if(vMets[i].itemId == page83DataGuid)
|
|
|
|
|
|
p83Off = vMets[i].offset;
|
|
|
|
|
|
else if(vMets[i].itemId == logicalSectorSizeGuid)
|
|
|
|
|
|
logOff = vMets[i].offset;
|
|
|
|
|
|
else if(vMets[i].itemId == physicalSectorSizeGuid)
|
|
|
|
|
|
physOff = vMets[i].offset;
|
|
|
|
|
|
else if(vMets[i].itemId == parentLocatorGuid)
|
|
|
|
|
|
parentOff = vMets[i].offset;
|
2017-12-20 17:15:26 +00:00
|
|
|
|
else if((vMets[i].flags & METADATA_FLAGS_REQUIRED) == METADATA_FLAGS_REQUIRED)
|
2017-12-19 20:33:03 +00:00
|
|
|
|
throw new
|
2017-12-21 17:58:51 +00:00
|
|
|
|
ImageNotSupportedException($"Found unsupported and required metadata Guid {vMets[i].itemId}, not proceeding with image.");
|
2016-08-29 03:38:18 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
byte[] tmp;
|
|
|
|
|
|
|
|
|
|
|
|
if(fileParamsOff != 0)
|
|
|
|
|
|
{
|
|
|
|
|
|
stream.Seek(fileParamsOff + metadataOffset, SeekOrigin.Begin);
|
|
|
|
|
|
tmp = new byte[8];
|
|
|
|
|
|
stream.Read(tmp, 0, 8);
|
2020-02-29 18:03:35 +00:00
|
|
|
|
|
2017-12-22 06:55:04 +00:00
|
|
|
|
vFileParms = new VhdxFileParameters
|
|
|
|
|
|
{
|
2018-12-31 13:17:27 +00:00
|
|
|
|
blockSize = BitConverter.ToUInt32(tmp, 0), flags = BitConverter.ToUInt32(tmp, 4)
|
2017-12-22 06:55:04 +00:00
|
|
|
|
};
|
2016-08-29 03:38:18 +01:00
|
|
|
|
}
|
2020-02-29 18:03:35 +00:00
|
|
|
|
else
|
|
|
|
|
|
throw new Exception("File parameters not found.");
|
2016-08-29 03:38:18 +01:00
|
|
|
|
|
|
|
|
|
|
if(vdSizeOff != 0)
|
|
|
|
|
|
{
|
|
|
|
|
|
stream.Seek(vdSizeOff + metadataOffset, SeekOrigin.Begin);
|
|
|
|
|
|
tmp = new byte[8];
|
|
|
|
|
|
stream.Read(tmp, 0, 8);
|
2017-12-20 17:15:26 +00:00
|
|
|
|
virtualDiskSize = BitConverter.ToUInt64(tmp, 0);
|
2016-08-29 03:38:18 +01:00
|
|
|
|
}
|
2020-02-29 18:03:35 +00:00
|
|
|
|
else
|
|
|
|
|
|
throw new Exception("Virtual disk size not found.");
|
2016-08-29 03:38:18 +01:00
|
|
|
|
|
|
|
|
|
|
if(p83Off != 0)
|
|
|
|
|
|
{
|
|
|
|
|
|
stream.Seek(p83Off + metadataOffset, SeekOrigin.Begin);
|
|
|
|
|
|
tmp = new byte[16];
|
|
|
|
|
|
stream.Read(tmp, 0, 16);
|
2017-12-20 17:15:26 +00:00
|
|
|
|
page83Data = new Guid(tmp);
|
2016-08-29 03:38:18 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if(logOff != 0)
|
|
|
|
|
|
{
|
|
|
|
|
|
stream.Seek(logOff + metadataOffset, SeekOrigin.Begin);
|
|
|
|
|
|
tmp = new byte[4];
|
|
|
|
|
|
stream.Read(tmp, 0, 4);
|
2017-12-20 17:15:26 +00:00
|
|
|
|
logicalSectorSize = BitConverter.ToUInt32(tmp, 0);
|
2016-08-29 03:38:18 +01:00
|
|
|
|
}
|
2020-02-29 18:03:35 +00:00
|
|
|
|
else
|
|
|
|
|
|
throw new Exception("Logical sector size not found.");
|
2017-12-19 20:33:03 +00:00
|
|
|
|
|
2016-08-29 03:38:18 +01:00
|
|
|
|
if(physOff != 0)
|
|
|
|
|
|
{
|
|
|
|
|
|
stream.Seek(physOff + metadataOffset, SeekOrigin.Begin);
|
|
|
|
|
|
tmp = new byte[4];
|
|
|
|
|
|
stream.Read(tmp, 0, 4);
|
2017-12-20 17:15:26 +00:00
|
|
|
|
physicalSectorSize = BitConverter.ToUInt32(tmp, 0);
|
2016-08-29 03:38:18 +01:00
|
|
|
|
}
|
2020-02-29 18:03:35 +00:00
|
|
|
|
else
|
|
|
|
|
|
throw new Exception("Physical sector size not found.");
|
2016-08-29 03:38:18 +01:00
|
|
|
|
|
2020-02-29 18:03:35 +00:00
|
|
|
|
if(parentOff != 0 &&
|
|
|
|
|
|
(vFileParms.flags & FILE_FLAGS_HAS_PARENT) == FILE_FLAGS_HAS_PARENT)
|
2016-08-29 03:38:18 +01:00
|
|
|
|
{
|
|
|
|
|
|
stream.Seek(parentOff + metadataOffset, SeekOrigin.Begin);
|
2019-03-01 07:35:22 +00:00
|
|
|
|
byte[] vParHdrB = new byte[Marshal.SizeOf<VhdxParentLocatorHeader>()];
|
|
|
|
|
|
stream.Read(vParHdrB, 0, Marshal.SizeOf<VhdxParentLocatorHeader>());
|
|
|
|
|
|
vParHdr = Marshal.ByteArrayToStructureLittleEndian<VhdxParentLocatorHeader>(vParHdrB);
|
2016-08-29 03:38:18 +01:00
|
|
|
|
|
2017-12-20 17:15:26 +00:00
|
|
|
|
if(vParHdr.locatorType != parentTypeVhdxGuid)
|
2017-12-19 20:33:03 +00:00
|
|
|
|
throw new
|
2017-12-21 17:58:51 +00:00
|
|
|
|
ImageNotSupportedException($"Found unsupported and required parent locator type {vParHdr.locatorType}, not proceeding with image.");
|
2016-08-29 03:38:18 +01:00
|
|
|
|
|
2017-12-20 17:15:26 +00:00
|
|
|
|
vPars = new VhdxParentLocatorEntry[vParHdr.keyValueCount];
|
2020-02-29 18:03:35 +00:00
|
|
|
|
|
2016-08-29 03:38:18 +01:00
|
|
|
|
for(int i = 0; i < vPars.Length; i++)
|
|
|
|
|
|
{
|
2019-03-01 07:35:22 +00:00
|
|
|
|
byte[] vParB = new byte[System.Runtime.InteropServices.Marshal.SizeOf(vPars[i])];
|
|
|
|
|
|
stream.Read(vParB, 0, System.Runtime.InteropServices.Marshal.SizeOf(vPars[i]));
|
|
|
|
|
|
vPars[i] = Marshal.ByteArrayToStructureLittleEndian<VhdxParentLocatorEntry>(vParB);
|
2016-08-29 03:38:18 +01:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2017-12-20 17:15:26 +00:00
|
|
|
|
else if((vFileParms.flags & FILE_FLAGS_HAS_PARENT) == FILE_FLAGS_HAS_PARENT)
|
2016-08-29 03:38:18 +01:00
|
|
|
|
throw new Exception("Parent locator not found.");
|
|
|
|
|
|
|
2017-12-20 17:15:26 +00:00
|
|
|
|
if((vFileParms.flags & FILE_FLAGS_HAS_PARENT) == FILE_FLAGS_HAS_PARENT &&
|
2018-01-28 20:29:46 +00:00
|
|
|
|
vParHdr.locatorType == parentTypeVhdxGuid)
|
2016-08-29 03:38:18 +01:00
|
|
|
|
{
|
2018-06-22 08:08:38 +01:00
|
|
|
|
parentImage = new Vhdx();
|
2016-08-29 03:38:18 +01:00
|
|
|
|
bool parentWorks = false;
|
|
|
|
|
|
|
2017-12-20 17:15:26 +00:00
|
|
|
|
foreach(VhdxParentLocatorEntry parentEntry in vPars)
|
2016-08-29 03:38:18 +01:00
|
|
|
|
{
|
|
|
|
|
|
stream.Seek(parentEntry.keyOffset + metadataOffset, SeekOrigin.Begin);
|
|
|
|
|
|
byte[] tmpKey = new byte[parentEntry.keyLength];
|
|
|
|
|
|
stream.Read(tmpKey, 0, tmpKey.Length);
|
|
|
|
|
|
string entryType = Encoding.Unicode.GetString(tmpKey);
|
|
|
|
|
|
|
2017-12-26 06:05:12 +00:00
|
|
|
|
IFilter parentFilter;
|
2020-02-29 18:03:35 +00:00
|
|
|
|
|
2017-12-20 17:15:26 +00:00
|
|
|
|
if(string.Compare(entryType, RELATIVE_PATH_KEY, StringComparison.OrdinalIgnoreCase) == 0)
|
2016-08-29 03:38:18 +01:00
|
|
|
|
{
|
|
|
|
|
|
stream.Seek(parentEntry.valueOffset + metadataOffset, SeekOrigin.Begin);
|
|
|
|
|
|
byte[] tmpVal = new byte[parentEntry.valueLength];
|
|
|
|
|
|
stream.Read(tmpVal, 0, tmpVal.Length);
|
|
|
|
|
|
string entryValue = Encoding.Unicode.GetString(tmpVal);
|
|
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
2017-12-19 20:33:03 +00:00
|
|
|
|
parentFilter =
|
|
|
|
|
|
new FiltersList().GetFilter(Path.Combine(imageFilter.GetParentFolder(), entryValue));
|
2020-02-29 18:03:35 +00:00
|
|
|
|
|
|
|
|
|
|
if(parentFilter != null &&
|
|
|
|
|
|
parentImage.Open(parentFilter))
|
2016-08-29 03:38:18 +01:00
|
|
|
|
{
|
|
|
|
|
|
parentWorks = true;
|
2020-02-29 18:03:35 +00:00
|
|
|
|
|
2016-08-29 03:38:18 +01:00
|
|
|
|
break;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2020-02-29 18:03:35 +00:00
|
|
|
|
catch
|
|
|
|
|
|
{
|
|
|
|
|
|
parentWorks = false;
|
|
|
|
|
|
}
|
2016-08-29 03:38:18 +01:00
|
|
|
|
|
2016-09-05 17:37:31 +01:00
|
|
|
|
string relEntry = Path.Combine(Path.GetDirectoryName(imageFilter.GetPath()), entryValue);
|
2016-08-29 03:38:18 +01:00
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
2017-12-19 20:33:03 +00:00
|
|
|
|
parentFilter =
|
|
|
|
|
|
new FiltersList().GetFilter(Path.Combine(imageFilter.GetParentFolder(), relEntry));
|
2020-02-29 18:03:35 +00:00
|
|
|
|
|
|
|
|
|
|
if(parentFilter == null ||
|
|
|
|
|
|
!parentImage.Open(parentFilter))
|
|
|
|
|
|
continue;
|
2017-12-21 06:06:19 +00:00
|
|
|
|
|
|
|
|
|
|
parentWorks = true;
|
2020-02-29 18:03:35 +00:00
|
|
|
|
|
2017-12-21 06:06:19 +00:00
|
|
|
|
break;
|
2016-08-29 03:38:18 +01:00
|
|
|
|
}
|
2017-12-24 00:12:31 +00:00
|
|
|
|
catch
|
|
|
|
|
|
{
|
|
|
|
|
|
// ignored
|
|
|
|
|
|
}
|
2016-08-29 03:38:18 +01:00
|
|
|
|
}
|
2018-01-28 20:29:46 +00:00
|
|
|
|
else if(string.Compare(entryType, VOLUME_PATH_KEY, StringComparison.OrdinalIgnoreCase) ==
|
|
|
|
|
|
0 ||
|
2017-12-20 17:15:26 +00:00
|
|
|
|
string.Compare(entryType, ABSOLUTE_WIN32_PATH_KEY, StringComparison.OrdinalIgnoreCase) == 0)
|
2016-08-29 03:38:18 +01:00
|
|
|
|
{
|
|
|
|
|
|
stream.Seek(parentEntry.valueOffset + metadataOffset, SeekOrigin.Begin);
|
|
|
|
|
|
byte[] tmpVal = new byte[parentEntry.valueLength];
|
|
|
|
|
|
stream.Read(tmpVal, 0, tmpVal.Length);
|
|
|
|
|
|
string entryValue = Encoding.Unicode.GetString(tmpVal);
|
|
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
2017-12-19 20:33:03 +00:00
|
|
|
|
parentFilter =
|
|
|
|
|
|
new FiltersList().GetFilter(Path.Combine(imageFilter.GetParentFolder(), entryValue));
|
2020-02-29 18:03:35 +00:00
|
|
|
|
|
|
|
|
|
|
if(parentFilter == null ||
|
|
|
|
|
|
!parentImage.Open(parentFilter))
|
|
|
|
|
|
continue;
|
2017-12-21 06:06:19 +00:00
|
|
|
|
|
|
|
|
|
|
parentWorks = true;
|
2020-02-29 18:03:35 +00:00
|
|
|
|
|
2017-12-21 06:06:19 +00:00
|
|
|
|
break;
|
2016-08-29 03:38:18 +01:00
|
|
|
|
}
|
2017-12-24 00:12:31 +00:00
|
|
|
|
catch
|
|
|
|
|
|
{
|
|
|
|
|
|
// ignored
|
|
|
|
|
|
}
|
2016-08-29 03:38:18 +01:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-02-29 18:03:35 +00:00
|
|
|
|
if(!parentWorks)
|
|
|
|
|
|
throw new Exception("Image is differential but parent cannot be opened.");
|
2016-08-29 03:38:18 +01:00
|
|
|
|
|
|
|
|
|
|
hasParent = true;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-02-29 18:03:35 +00:00
|
|
|
|
chunkRatio = (long)((Math.Pow(2, 23) * logicalSectorSize) / vFileParms.blockSize);
|
2018-06-22 08:08:38 +01:00
|
|
|
|
dataBlocks = virtualDiskSize / vFileParms.blockSize;
|
2020-02-29 18:03:35 +00:00
|
|
|
|
|
|
|
|
|
|
if(virtualDiskSize % vFileParms.blockSize > 0)
|
|
|
|
|
|
dataBlocks++;
|
2016-08-29 03:38:18 +01:00
|
|
|
|
|
|
|
|
|
|
long batEntries;
|
2020-02-29 18:03:35 +00:00
|
|
|
|
|
2016-08-29 03:38:18 +01:00
|
|
|
|
if(hasParent)
|
|
|
|
|
|
{
|
|
|
|
|
|
long sectorBitmapBlocks = (long)dataBlocks / chunkRatio;
|
2020-02-29 18:03:35 +00:00
|
|
|
|
|
|
|
|
|
|
if(dataBlocks % (ulong)chunkRatio > 0)
|
|
|
|
|
|
sectorBitmapBlocks++;
|
|
|
|
|
|
|
2016-08-29 03:38:18 +01:00
|
|
|
|
sectorBitmapPointers = new ulong[sectorBitmapBlocks];
|
|
|
|
|
|
|
|
|
|
|
|
batEntries = sectorBitmapBlocks * (chunkRatio - 1);
|
|
|
|
|
|
}
|
2020-02-29 18:03:35 +00:00
|
|
|
|
else
|
|
|
|
|
|
batEntries = (long)(dataBlocks + ((dataBlocks - 1) / (ulong)chunkRatio));
|
2016-08-29 03:38:18 +01:00
|
|
|
|
|
2020-02-27 23:48:41 +00:00
|
|
|
|
AaruConsole.DebugWriteLine("VHDX plugin", "Reading BAT");
|
2016-08-29 03:38:18 +01:00
|
|
|
|
|
2018-06-22 08:08:38 +01:00
|
|
|
|
long readChunks = 0;
|
2016-08-29 03:38:18 +01:00
|
|
|
|
blockAllocationTable = new ulong[dataBlocks];
|
2018-06-22 08:08:38 +01:00
|
|
|
|
byte[] batB = new byte[batEntries * 8];
|
2016-08-29 03:38:18 +01:00
|
|
|
|
stream.Seek(batOffset, SeekOrigin.Begin);
|
2017-12-20 17:15:26 +00:00
|
|
|
|
stream.Read(batB, 0, batB.Length);
|
2016-08-29 03:38:18 +01:00
|
|
|
|
|
|
|
|
|
|
ulong skipSize = 0;
|
2020-02-29 18:03:35 +00:00
|
|
|
|
|
2016-08-29 03:38:18 +01:00
|
|
|
|
for(ulong i = 0; i < dataBlocks; i++)
|
2018-06-22 08:08:38 +01:00
|
|
|
|
if(readChunks == chunkRatio)
|
2016-08-29 03:38:18 +01:00
|
|
|
|
{
|
|
|
|
|
|
if(hasParent)
|
2020-02-29 18:03:35 +00:00
|
|
|
|
sectorBitmapPointers[skipSize / 8] = BitConverter.ToUInt64(batB, (int)((i * 8) + skipSize));
|
2016-08-29 03:38:18 +01:00
|
|
|
|
|
2018-01-28 20:29:46 +00:00
|
|
|
|
readChunks = 0;
|
|
|
|
|
|
skipSize += 8;
|
2016-08-29 03:38:18 +01:00
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
2020-02-29 18:03:35 +00:00
|
|
|
|
blockAllocationTable[i] = BitConverter.ToUInt64(batB, (int)((i * 8) + skipSize));
|
2016-08-29 03:38:18 +01:00
|
|
|
|
readChunks++;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if(hasParent)
|
|
|
|
|
|
{
|
2020-02-27 23:48:41 +00:00
|
|
|
|
AaruConsole.DebugWriteLine("VHDX plugin", "Reading Sector Bitmap");
|
2016-08-29 03:38:18 +01:00
|
|
|
|
|
2020-02-29 18:03:35 +00:00
|
|
|
|
var sectorBmpMs = new MemoryStream();
|
|
|
|
|
|
|
2016-08-29 03:38:18 +01:00
|
|
|
|
foreach(ulong pt in sectorBitmapPointers)
|
2017-12-24 00:12:31 +00:00
|
|
|
|
switch(pt & BAT_FLAGS_MASK)
|
|
|
|
|
|
{
|
|
|
|
|
|
case SECTOR_BITMAP_NOT_PRESENT:
|
|
|
|
|
|
sectorBmpMs.Write(new byte[1048576], 0, 1048576);
|
2020-02-29 18:03:35 +00:00
|
|
|
|
|
2017-12-21 04:43:29 +00:00
|
|
|
|
break;
|
|
|
|
|
|
case SECTOR_BITMAP_PRESENT:
|
|
|
|
|
|
stream.Seek((long)((pt & BAT_FILE_OFFSET_MASK) * 1048576), SeekOrigin.Begin);
|
|
|
|
|
|
byte[] bmp = new byte[1048576];
|
|
|
|
|
|
stream.Read(bmp, 0, bmp.Length);
|
|
|
|
|
|
sectorBmpMs.Write(bmp, 0, bmp.Length);
|
2020-02-29 18:03:35 +00:00
|
|
|
|
|
2017-12-21 04:43:29 +00:00
|
|
|
|
break;
|
|
|
|
|
|
default:
|
|
|
|
|
|
if((pt & BAT_FLAGS_MASK) != 0)
|
|
|
|
|
|
throw new
|
2017-12-21 17:58:51 +00:00
|
|
|
|
ImageNotSupportedException($"Unsupported sector bitmap block flags (0x{pt & BAT_FLAGS_MASK:X16}) found, not proceeding.");
|
2017-12-21 04:43:29 +00:00
|
|
|
|
|
|
|
|
|
|
break;
|
2016-08-29 03:38:18 +01:00
|
|
|
|
}
|
2017-12-19 20:33:03 +00:00
|
|
|
|
|
2016-08-29 03:38:18 +01:00
|
|
|
|
sectorBitmap = sectorBmpMs.ToArray();
|
|
|
|
|
|
sectorBmpMs.Close();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2018-01-28 20:29:46 +00:00
|
|
|
|
maxBlockCache = (int)(MAX_CACHE_SIZE / vFileParms.blockSize);
|
2017-12-20 17:15:26 +00:00
|
|
|
|
maxSectorCache = (int)(MAX_CACHE_SIZE / logicalSectorSize);
|
2016-08-29 03:38:18 +01:00
|
|
|
|
|
|
|
|
|
|
imageStream = stream;
|
|
|
|
|
|
|
|
|
|
|
|
sectorCache = new Dictionary<ulong, byte[]>();
|
2018-01-28 20:29:46 +00:00
|
|
|
|
blockCache = new Dictionary<ulong, byte[]>();
|
2016-08-29 03:38:18 +01:00
|
|
|
|
|
2018-01-28 20:29:46 +00:00
|
|
|
|
imageInfo.CreationTime = imageFilter.GetCreationTime();
|
2017-12-26 06:05:12 +00:00
|
|
|
|
imageInfo.LastModificationTime = imageFilter.GetLastWriteTime();
|
2018-01-28 20:29:46 +00:00
|
|
|
|
imageInfo.MediaTitle = Path.GetFileNameWithoutExtension(imageFilter.GetFilename());
|
|
|
|
|
|
imageInfo.SectorSize = logicalSectorSize;
|
|
|
|
|
|
imageInfo.XmlMediaType = XmlMediaType.BlockMedia;
|
|
|
|
|
|
imageInfo.MediaType = MediaType.GENERIC_HDD;
|
|
|
|
|
|
imageInfo.ImageSize = virtualDiskSize;
|
|
|
|
|
|
imageInfo.Sectors = imageInfo.ImageSize / imageInfo.SectorSize;
|
|
|
|
|
|
imageInfo.DriveSerialNumber = page83Data.ToString();
|
2016-08-29 03:38:18 +01:00
|
|
|
|
|
2017-12-19 20:33:03 +00:00
|
|
|
|
// TODO: Separate image application from version, need several samples.
|
2016-08-29 03:38:18 +01:00
|
|
|
|
|
2018-01-28 20:29:46 +00:00
|
|
|
|
imageInfo.Cylinders = (uint)(imageInfo.Sectors / 16 / 63);
|
|
|
|
|
|
imageInfo.Heads = 16;
|
2017-12-26 06:05:12 +00:00
|
|
|
|
imageInfo.SectorsPerTrack = 63;
|
2017-08-02 23:01:11 +01:00
|
|
|
|
|
2017-12-19 20:33:03 +00:00
|
|
|
|
return true;
|
2016-08-29 03:38:18 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2017-12-26 07:28:40 +00:00
|
|
|
|
public byte[] ReadSector(ulong sectorAddress)
|
2016-08-29 03:38:18 +01:00
|
|
|
|
{
|
2017-12-26 06:05:12 +00:00
|
|
|
|
if(sectorAddress > imageInfo.Sectors - 1)
|
2017-12-19 20:33:03 +00:00
|
|
|
|
throw new ArgumentOutOfRangeException(nameof(sectorAddress),
|
2017-12-21 17:58:51 +00:00
|
|
|
|
$"Sector address {sectorAddress} not found");
|
2016-08-29 03:38:18 +01:00
|
|
|
|
|
2020-02-29 18:03:35 +00:00
|
|
|
|
if(sectorCache.TryGetValue(sectorAddress, out byte[] sector))
|
|
|
|
|
|
return sector;
|
2016-08-29 03:38:18 +01:00
|
|
|
|
|
2020-02-29 18:03:35 +00:00
|
|
|
|
ulong index = (sectorAddress * logicalSectorSize) / vFileParms.blockSize;
|
|
|
|
|
|
ulong secOff = (sectorAddress * logicalSectorSize) % vFileParms.blockSize;
|
2016-08-29 03:38:18 +01:00
|
|
|
|
|
2018-01-28 20:29:46 +00:00
|
|
|
|
ulong blkPtr = blockAllocationTable[index];
|
2017-12-20 17:15:26 +00:00
|
|
|
|
ulong blkFlags = blkPtr & BAT_FLAGS_MASK;
|
2016-08-29 03:38:18 +01:00
|
|
|
|
|
2017-12-20 17:15:26 +00:00
|
|
|
|
if((blkPtr & BAT_RESERVED_MASK) != 0)
|
2017-12-24 00:12:31 +00:00
|
|
|
|
throw new
|
|
|
|
|
|
ImageNotSupportedException($"Unknown flags (0x{blkPtr & BAT_RESERVED_MASK:X16}) set in block pointer");
|
2016-08-29 03:38:18 +01:00
|
|
|
|
|
2017-12-24 00:12:31 +00:00
|
|
|
|
switch(blkFlags & BAT_FLAGS_MASK)
|
|
|
|
|
|
{
|
|
|
|
|
|
case PAYLOAD_BLOCK_NOT_PRESENT:
|
|
|
|
|
|
return hasParent ? parentImage.ReadSector(sectorAddress) : new byte[logicalSectorSize];
|
2017-12-21 04:43:29 +00:00
|
|
|
|
case PAYLOAD_BLOCK_UNDEFINED:
|
|
|
|
|
|
case PAYLOAD_BLOCK_ZERO:
|
|
|
|
|
|
case PAYLOAD_BLOCK_UNMAPPER: return new byte[logicalSectorSize];
|
|
|
|
|
|
}
|
2016-08-29 03:38:18 +01:00
|
|
|
|
|
|
|
|
|
|
bool partialBlock;
|
2017-12-20 17:15:26 +00:00
|
|
|
|
partialBlock = (blkFlags & BAT_FLAGS_MASK) == PAYLOAD_BLOCK_PARTIALLY_PRESENT;
|
2016-08-29 03:38:18 +01:00
|
|
|
|
|
2020-02-29 18:03:35 +00:00
|
|
|
|
if(partialBlock &&
|
|
|
|
|
|
hasParent &&
|
|
|
|
|
|
!CheckBitmap(sectorAddress))
|
|
|
|
|
|
return parentImage.ReadSector(sectorAddress);
|
2016-08-29 03:38:18 +01:00
|
|
|
|
|
2017-12-22 06:55:04 +00:00
|
|
|
|
if(!blockCache.TryGetValue(blkPtr & BAT_FILE_OFFSET_MASK, out byte[] block))
|
2016-08-29 03:38:18 +01:00
|
|
|
|
{
|
|
|
|
|
|
block = new byte[vFileParms.blockSize];
|
2017-12-20 17:26:28 +00:00
|
|
|
|
imageStream.Seek((long)(blkPtr & BAT_FILE_OFFSET_MASK), SeekOrigin.Begin);
|
2016-08-29 03:38:18 +01:00
|
|
|
|
imageStream.Read(block, 0, block.Length);
|
|
|
|
|
|
|
2020-02-29 18:03:35 +00:00
|
|
|
|
if(blockCache.Count >= maxBlockCache)
|
|
|
|
|
|
blockCache.Clear();
|
2016-08-29 03:38:18 +01:00
|
|
|
|
|
2017-12-20 17:15:26 +00:00
|
|
|
|
blockCache.Add(blkPtr & BAT_FILE_OFFSET_MASK, block);
|
2016-08-29 03:38:18 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2017-12-20 17:15:26 +00:00
|
|
|
|
sector = new byte[logicalSectorSize];
|
2016-08-29 03:38:18 +01:00
|
|
|
|
Array.Copy(block, (int)secOff, sector, 0, sector.Length);
|
|
|
|
|
|
|
2020-02-29 18:03:35 +00:00
|
|
|
|
if(sectorCache.Count >= maxSectorCache)
|
|
|
|
|
|
sectorCache.Clear();
|
2016-08-29 03:38:18 +01:00
|
|
|
|
|
|
|
|
|
|
sectorCache.Add(sectorAddress, sector);
|
|
|
|
|
|
|
|
|
|
|
|
return sector;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2017-12-26 07:28:40 +00:00
|
|
|
|
public byte[] ReadSectors(ulong sectorAddress, uint length)
|
2016-08-29 03:38:18 +01:00
|
|
|
|
{
|
2017-12-26 06:05:12 +00:00
|
|
|
|
if(sectorAddress > imageInfo.Sectors - 1)
|
2017-12-19 20:33:03 +00:00
|
|
|
|
throw new ArgumentOutOfRangeException(nameof(sectorAddress),
|
2017-12-21 17:58:51 +00:00
|
|
|
|
$"Sector address {sectorAddress} not found");
|
2016-08-29 03:38:18 +01:00
|
|
|
|
|
2017-12-26 06:05:12 +00:00
|
|
|
|
if(sectorAddress + length > imageInfo.Sectors)
|
2017-12-19 20:33:03 +00:00
|
|
|
|
throw new ArgumentOutOfRangeException(nameof(length),
|
2017-12-26 06:05:12 +00:00
|
|
|
|
$"Requested more sectors ({sectorAddress + length}) than available ({imageInfo.Sectors})");
|
2016-08-29 03:38:18 +01:00
|
|
|
|
|
2020-02-29 18:03:35 +00:00
|
|
|
|
var ms = new MemoryStream();
|
2016-08-29 03:38:18 +01:00
|
|
|
|
|
|
|
|
|
|
for(uint i = 0; i < length; i++)
|
|
|
|
|
|
{
|
|
|
|
|
|
byte[] sector = ReadSector(sectorAddress + i);
|
|
|
|
|
|
ms.Write(sector, 0, sector.Length);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return ms.ToArray();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2017-12-19 20:33:03 +00:00
|
|
|
|
}
|