2017-05-19 20:28:49 +01:00
|
|
|
// /***************************************************************************
|
2016-08-30 05:25:27 +01:00
|
|
|
// The Disc Image Chef
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
//
|
|
|
|
|
// Filename : VMware.cs
|
|
|
|
|
// Author(s) : Natalia Portillo <claunia@claunia.com>
|
|
|
|
|
//
|
2017-12-19 03:50:57 +00:00
|
|
|
// Component : Disk image plugins.
|
2016-08-30 05:25:27 +01:00
|
|
|
//
|
|
|
|
|
// --[ Description ] ----------------------------------------------------------
|
|
|
|
|
//
|
|
|
|
|
// Manages VMware disk images.
|
|
|
|
|
//
|
|
|
|
|
// --[ 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/>.
|
|
|
|
|
//
|
|
|
|
|
// ----------------------------------------------------------------------------
|
2017-12-19 03:50:57 +00:00
|
|
|
// Copyright © 2011-2018 Natalia Portillo
|
2016-08-30 05:25:27 +01:00
|
|
|
// ****************************************************************************/
|
|
|
|
|
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Runtime.InteropServices;
|
2017-12-30 23:58:39 +00:00
|
|
|
using System.Text;
|
2016-08-30 05:25:27 +01:00
|
|
|
using System.Text.RegularExpressions;
|
|
|
|
|
using DiscImageChef.CommonTypes;
|
2018-06-25 19:08:16 +01:00
|
|
|
using DiscImageChef.CommonTypes.Enums;
|
|
|
|
|
using DiscImageChef.CommonTypes.Exceptions;
|
|
|
|
|
using DiscImageChef.CommonTypes.Interfaces;
|
|
|
|
|
using DiscImageChef.CommonTypes.Structs;
|
2016-08-30 05:25:27 +01:00
|
|
|
using DiscImageChef.Console;
|
2016-09-05 17:37:31 +01:00
|
|
|
using DiscImageChef.Filters;
|
2018-01-28 20:29:46 +00:00
|
|
|
using Schemas;
|
2016-08-30 05:25:27 +01:00
|
|
|
|
|
|
|
|
namespace DiscImageChef.DiscImages
|
|
|
|
|
{
|
2017-12-30 23:58:39 +00:00
|
|
|
public class VMware : IWritableImage
|
2016-08-30 05:25:27 +01:00
|
|
|
{
|
2017-12-20 17:15:26 +00:00
|
|
|
const uint VMWARE_EXTENT_MAGIC = 0x564D444B;
|
2017-12-30 23:58:39 +00:00
|
|
|
const uint VMWARE_COW_MAGIC = 0x44574F43;
|
2017-12-20 17:15:26 +00:00
|
|
|
|
2017-12-30 23:58:39 +00:00
|
|
|
const string VM_TYPE_CUSTOM = "custom";
|
|
|
|
|
const string VM_TYPE_MONO_SPARSE = "monolithicSparse";
|
|
|
|
|
const string VM_TYPE_MONO_FLAT = "monolithicFlat";
|
2017-12-20 17:15:26 +00:00
|
|
|
const string VM_TYPE_SPLIT_SPARSE = "twoGbMaxExtentSparse";
|
2017-12-30 23:58:39 +00:00
|
|
|
const string VM_TYPE_SPLIT_FLAT = "twoGbMaxExtentFlat";
|
|
|
|
|
const string VM_TYPE_FULL_DEVICE = "fullDevice";
|
|
|
|
|
const string VM_TYPE_PART_DEVICE = "partitionedDevice";
|
|
|
|
|
const string VMFS_TYPE_FLAT = "vmfsPreallocated";
|
|
|
|
|
const string VMFS_TYPE_ZERO = "vmfsEagerZeroedThick";
|
|
|
|
|
const string VMFS_TYPE_THIN = "vmfsThin";
|
|
|
|
|
const string VMFS_TYPE_SPARSE = "vmfsSparse";
|
|
|
|
|
const string VMFS_TYPE_RDM = "vmfsRDM";
|
|
|
|
|
const string VMFS_TYPE_RDM_OLD = "vmfsRawDeviceMap";
|
|
|
|
|
const string VMFS_TYPE_RDMP = "vmfsRDMP";
|
|
|
|
|
const string VMFS_TYPE_RDMP_OLD = "vmfsPassthroughRawDeviceMap";
|
|
|
|
|
const string VMFS_TYPE_RAW = "vmfsRaw";
|
|
|
|
|
const string VMFS_TYPE = "vmfs";
|
|
|
|
|
const string VM_TYPE_STREAM = "streamOptimized";
|
2017-12-20 17:15:26 +00:00
|
|
|
|
|
|
|
|
const string DDF_MAGIC = "# Disk DescriptorFile";
|
2016-08-30 05:25:27 +01:00
|
|
|
|
2017-12-30 23:58:39 +00:00
|
|
|
const string REGEX_VERSION = @"^\s*version\s*=\s*(?<version>\d+)$";
|
|
|
|
|
const string REGEX_CID = @"^\s*CID\s*=\s*(?<cid>[0123456789abcdef]{8})$";
|
2017-12-24 20:26:23 +00:00
|
|
|
const string REGEX_CID_PARENT = @"^\s*parentCID\s*=\s*(?<cid>[0123456789abcdef]{8})$";
|
2018-06-22 08:08:38 +01:00
|
|
|
const string REGEX_TYPE =
|
2017-12-30 23:58:39 +00:00
|
|
|
@"^\s*createType\s*=\s*\""(?<type>custom|monolithicSparse|monolithicFlat|twoGbMaxExtentSparse|twoGbMaxExtentFlat|fullDevice|partitionedDevice|vmfs|vmfsPreallocated|vmfsEagerZeroedThick|vmfsThin|vmfsSparse|vmfsRDM|vmfsRawDeviceMap|vmfsRDMP|vmfsPassthroughRawDeviceMap|vmfsRaw|streamOptimized)\""$";
|
2017-12-24 00:12:31 +00:00
|
|
|
const string REGEX_EXTENT =
|
2017-12-30 23:58:39 +00:00
|
|
|
@"^\s*(?<access>(RW|RDONLY|NOACCESS))\s+(?<sectors>\d+)\s+(?<type>(FLAT|SPARSE|ZERO|VMFS|VMFSSPARSE|VMFSRDM|VMFSRAW))\s+\""(?<filename>.+)\""(\s*(?<offset>\d+))?$";
|
|
|
|
|
const string REGEX_DDB_TYPE =
|
|
|
|
|
@"^\s*ddb\.adapterType\s*=\s*\""(?<type>ide|buslogic|lsilogic|legacyESX)\""$";
|
|
|
|
|
const string REGEX_DDB_SECTORS = @"^\s*ddb\.geometry\.sectors\s*=\s*\""(?<sectors>\d+)\""$";
|
|
|
|
|
const string REGEX_DDB_HEADS = @"^\s*ddb\.geometry\.heads\s*=\s*\""(?<heads>\d+)\""$";
|
2017-12-24 20:26:23 +00:00
|
|
|
const string REGEX_DDB_CYLINDERS = @"^\s*ddb\.geometry\.cylinders\s*=\s*\""(?<cylinders>\d+)\""$";
|
2017-12-30 23:58:39 +00:00
|
|
|
const string PARENT_REGEX = @"^\s*parentFileNameHint\s*=\s*\""(?<filename>.+)\""$";
|
2017-12-20 17:15:26 +00:00
|
|
|
|
2017-12-30 23:58:39 +00:00
|
|
|
const uint FLAGS_VALID_NEW_LINE = 0x01;
|
2017-12-20 17:15:26 +00:00
|
|
|
const uint FLAGS_USE_REDUNDANT_TABLE = 0x02;
|
2017-12-30 23:58:39 +00:00
|
|
|
const uint FLAGS_ZERO_GRAIN_GTE = 0x04;
|
|
|
|
|
const uint FLAGS_COMPRESSION = 0x10000;
|
|
|
|
|
const uint FLAGS_MARKERS = 0x20000;
|
2017-12-20 17:15:26 +00:00
|
|
|
|
2017-12-30 23:58:39 +00:00
|
|
|
const ushort COMPRESSION_NONE = 0;
|
2017-12-20 17:15:26 +00:00
|
|
|
const ushort COMPRESSION_DEFLATE = 1;
|
2016-08-30 05:25:27 +01:00
|
|
|
|
2018-06-22 08:08:38 +01:00
|
|
|
const uint MAX_CACHE_SIZE = 16777216;
|
|
|
|
|
const uint SECTOR_SIZE = 512;
|
|
|
|
|
const uint MAX_CACHED_SECTORS = MAX_CACHE_SIZE / SECTOR_SIZE;
|
|
|
|
|
readonly byte[] ddfMagicBytes =
|
2016-08-30 05:25:27 +01:00
|
|
|
{
|
2017-12-24 00:12:31 +00:00
|
|
|
0x23, 0x20, 0x44, 0x69, 0x73, 0x6B, 0x20, 0x44, 0x65, 0x73, 0x63, 0x72, 0x69, 0x70, 0x74, 0x6F, 0x72, 0x46,
|
|
|
|
|
0x69, 0x6C, 0x65
|
|
|
|
|
};
|
2017-12-30 23:58:39 +00:00
|
|
|
string adapter_type;
|
2016-08-30 05:25:27 +01:00
|
|
|
|
2017-12-30 23:58:39 +00:00
|
|
|
uint cid;
|
|
|
|
|
StreamWriter descriptorStream;
|
2016-08-30 05:25:27 +01:00
|
|
|
Dictionary<ulong, VMwareExtent> extents;
|
2017-12-30 23:58:39 +00:00
|
|
|
IFilter gdFilter;
|
|
|
|
|
Dictionary<ulong, byte[]> grainCache;
|
2016-08-30 05:25:27 +01:00
|
|
|
|
2017-12-30 23:58:39 +00:00
|
|
|
ulong grainSize;
|
|
|
|
|
uint[] gTable;
|
|
|
|
|
bool hasParent;
|
2018-01-17 19:22:00 +00:00
|
|
|
uint hwversion;
|
2017-12-26 06:05:12 +00:00
|
|
|
ImageInfo imageInfo;
|
2017-12-30 23:58:39 +00:00
|
|
|
string imageType;
|
|
|
|
|
uint maxCachedGrains;
|
|
|
|
|
uint parentCid;
|
2016-08-30 05:25:27 +01:00
|
|
|
|
2017-12-26 06:05:12 +00:00
|
|
|
IMediaImage parentImage;
|
2017-12-30 23:58:39 +00:00
|
|
|
string parentName;
|
2016-08-30 05:25:27 +01:00
|
|
|
|
2017-12-24 00:12:31 +00:00
|
|
|
Dictionary<ulong, byte[]> sectorCache;
|
2017-12-30 23:58:39 +00:00
|
|
|
uint version;
|
|
|
|
|
VMwareCowHeader vmCHdr;
|
2017-12-24 00:12:31 +00:00
|
|
|
|
|
|
|
|
VMwareExtentHeader vmEHdr;
|
2017-12-30 23:58:39 +00:00
|
|
|
string writingBaseName;
|
|
|
|
|
FileStream writingStream;
|
2016-08-30 05:25:27 +01:00
|
|
|
|
|
|
|
|
public VMware()
|
|
|
|
|
{
|
2017-12-26 06:05:12 +00:00
|
|
|
imageInfo = new ImageInfo
|
2017-12-22 06:55:04 +00:00
|
|
|
{
|
2017-12-30 23:58:39 +00:00
|
|
|
ReadableSectorTags = new List<SectorTagType>(),
|
|
|
|
|
ReadableMediaTags = new List<MediaTagType>(),
|
|
|
|
|
HasPartitions = false,
|
|
|
|
|
HasSessions = false,
|
|
|
|
|
Version = null,
|
|
|
|
|
Application = "VMware",
|
|
|
|
|
ApplicationVersion = null,
|
|
|
|
|
Creator = null,
|
|
|
|
|
Comments = null,
|
|
|
|
|
MediaManufacturer = null,
|
|
|
|
|
MediaModel = null,
|
|
|
|
|
MediaSerialNumber = null,
|
|
|
|
|
MediaBarcode = null,
|
|
|
|
|
MediaPartNumber = null,
|
|
|
|
|
MediaSequence = 0,
|
|
|
|
|
LastMediaSequence = 0,
|
|
|
|
|
DriveManufacturer = null,
|
|
|
|
|
DriveModel = null,
|
|
|
|
|
DriveSerialNumber = null,
|
2017-12-22 06:55:04 +00:00
|
|
|
DriveFirmwareRevision = null
|
|
|
|
|
};
|
2016-08-30 05:25:27 +01:00
|
|
|
}
|
|
|
|
|
|
2017-12-26 07:28:40 +00:00
|
|
|
public ImageInfo Info => imageInfo;
|
2017-12-26 02:51:10 +00:00
|
|
|
|
2017-12-26 07:28:40 +00:00
|
|
|
public string Name => "VMware disk image";
|
2017-12-30 23:58:39 +00:00
|
|
|
public Guid Id => new Guid("E314DE35-C103-48A3-AD36-990F68523C46");
|
2017-12-26 06:05:12 +00:00
|
|
|
|
2017-12-28 19:56:36 +00:00
|
|
|
public string Format => "VMware";
|
2017-12-26 06:05:12 +00:00
|
|
|
|
2017-12-26 07:28:40 +00:00
|
|
|
public List<Partition> Partitions =>
|
2017-12-26 02:51:10 +00:00
|
|
|
throw new FeatureUnsupportedImageException("Feature not supported by image format");
|
|
|
|
|
|
2017-12-26 07:28:40 +00:00
|
|
|
public List<Track> Tracks =>
|
2017-12-26 02:51:10 +00:00
|
|
|
throw new FeatureUnsupportedImageException("Feature not supported by image format");
|
|
|
|
|
|
2017-12-26 07:28:40 +00:00
|
|
|
public List<Session> Sessions =>
|
2017-12-26 02:51:10 +00:00
|
|
|
throw new FeatureUnsupportedImageException("Feature not supported by image format");
|
|
|
|
|
|
2017-12-28 19:56:36 +00:00
|
|
|
public bool Identify(IFilter imageFilter)
|
2016-08-30 05:25:27 +01:00
|
|
|
{
|
2016-09-05 17:37:31 +01:00
|
|
|
Stream stream = imageFilter.GetDataForkStream();
|
2016-08-30 05:25:27 +01:00
|
|
|
|
|
|
|
|
byte[] ddfMagic = new byte[0x15];
|
|
|
|
|
|
|
|
|
|
if(stream.Length > Marshal.SizeOf(vmEHdr))
|
|
|
|
|
{
|
|
|
|
|
stream.Seek(0, SeekOrigin.Begin);
|
2017-12-20 17:15:26 +00:00
|
|
|
byte[] vmEHdrB = new byte[Marshal.SizeOf(vmEHdr)];
|
|
|
|
|
stream.Read(vmEHdrB, 0, Marshal.SizeOf(vmEHdr));
|
2018-06-22 08:08:38 +01:00
|
|
|
vmEHdr = new VMwareExtentHeader();
|
2016-08-30 05:25:27 +01:00
|
|
|
IntPtr headerPtr = Marshal.AllocHGlobal(Marshal.SizeOf(vmEHdr));
|
2017-12-20 17:15:26 +00:00
|
|
|
Marshal.Copy(vmEHdrB, 0, headerPtr, Marshal.SizeOf(vmEHdr));
|
2016-08-30 05:25:27 +01:00
|
|
|
vmEHdr = (VMwareExtentHeader)Marshal.PtrToStructure(headerPtr, typeof(VMwareExtentHeader));
|
|
|
|
|
Marshal.FreeHGlobal(headerPtr);
|
|
|
|
|
|
|
|
|
|
stream.Seek(0, SeekOrigin.Begin);
|
|
|
|
|
stream.Read(ddfMagic, 0, 0x15);
|
|
|
|
|
|
|
|
|
|
vmCHdr = new VMwareCowHeader();
|
2018-06-22 08:08:38 +01:00
|
|
|
if(stream.Length <= Marshal.SizeOf(vmCHdr))
|
2017-12-21 06:06:19 +00:00
|
|
|
return ddfMagicBytes.SequenceEqual(ddfMagic) || vmEHdr.magic == VMWARE_EXTENT_MAGIC ||
|
2017-12-30 23:58:39 +00:00
|
|
|
vmCHdr.magic == VMWARE_COW_MAGIC;
|
2017-12-21 06:06:19 +00:00
|
|
|
|
|
|
|
|
stream.Seek(0, SeekOrigin.Begin);
|
|
|
|
|
byte[] vmCHdrB = new byte[Marshal.SizeOf(vmCHdr)];
|
|
|
|
|
stream.Read(vmCHdrB, 0, Marshal.SizeOf(vmCHdr));
|
|
|
|
|
headerPtr = Marshal.AllocHGlobal(Marshal.SizeOf(vmCHdr));
|
|
|
|
|
Marshal.Copy(vmCHdrB, 0, headerPtr, Marshal.SizeOf(vmCHdr));
|
|
|
|
|
vmCHdr = (VMwareCowHeader)Marshal.PtrToStructure(headerPtr, typeof(VMwareCowHeader));
|
|
|
|
|
Marshal.FreeHGlobal(headerPtr);
|
2016-08-30 05:25:27 +01:00
|
|
|
|
2017-12-20 17:15:26 +00:00
|
|
|
return ddfMagicBytes.SequenceEqual(ddfMagic) || vmEHdr.magic == VMWARE_EXTENT_MAGIC ||
|
2017-12-30 23:58:39 +00:00
|
|
|
vmCHdr.magic == VMWARE_COW_MAGIC;
|
2016-08-30 05:25:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
stream.Seek(0, SeekOrigin.Begin);
|
|
|
|
|
stream.Read(ddfMagic, 0, 0x15);
|
|
|
|
|
|
2017-12-20 17:15:26 +00:00
|
|
|
return ddfMagicBytes.SequenceEqual(ddfMagic);
|
2016-08-30 05:25:27 +01:00
|
|
|
}
|
|
|
|
|
|
2017-12-28 19:56:36 +00:00
|
|
|
public bool Open(IFilter imageFilter)
|
2016-08-30 05:25:27 +01:00
|
|
|
{
|
2016-09-05 17:37:31 +01:00
|
|
|
Stream stream = imageFilter.GetDataForkStream();
|
2016-08-30 05:25:27 +01:00
|
|
|
|
2018-06-22 08:08:38 +01:00
|
|
|
vmEHdr = new VMwareExtentHeader();
|
|
|
|
|
vmCHdr = new VMwareCowHeader();
|
2016-08-30 05:25:27 +01:00
|
|
|
bool embedded = false;
|
|
|
|
|
|
|
|
|
|
if(stream.Length > Marshal.SizeOf(vmEHdr))
|
|
|
|
|
{
|
|
|
|
|
stream.Seek(0, SeekOrigin.Begin);
|
2017-12-20 17:15:26 +00:00
|
|
|
byte[] vmEHdrB = new byte[Marshal.SizeOf(vmEHdr)];
|
|
|
|
|
stream.Read(vmEHdrB, 0, Marshal.SizeOf(vmEHdr));
|
2016-08-30 05:25:27 +01:00
|
|
|
IntPtr headerPtr = Marshal.AllocHGlobal(Marshal.SizeOf(vmEHdr));
|
2017-12-20 17:15:26 +00:00
|
|
|
Marshal.Copy(vmEHdrB, 0, headerPtr, Marshal.SizeOf(vmEHdr));
|
2016-08-30 05:25:27 +01:00
|
|
|
vmEHdr = (VMwareExtentHeader)Marshal.PtrToStructure(headerPtr, typeof(VMwareExtentHeader));
|
|
|
|
|
Marshal.FreeHGlobal(headerPtr);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(stream.Length > Marshal.SizeOf(vmCHdr))
|
|
|
|
|
{
|
|
|
|
|
stream.Seek(0, SeekOrigin.Begin);
|
2017-12-20 17:15:26 +00:00
|
|
|
byte[] vmCHdrB = new byte[Marshal.SizeOf(vmCHdr)];
|
|
|
|
|
stream.Read(vmCHdrB, 0, Marshal.SizeOf(vmCHdr));
|
2016-08-30 05:25:27 +01:00
|
|
|
IntPtr cowPtr = Marshal.AllocHGlobal(Marshal.SizeOf(vmCHdr));
|
2017-12-20 17:15:26 +00:00
|
|
|
Marshal.Copy(vmCHdrB, 0, cowPtr, Marshal.SizeOf(vmCHdr));
|
2016-08-30 05:25:27 +01:00
|
|
|
vmCHdr = (VMwareCowHeader)Marshal.PtrToStructure(cowPtr, typeof(VMwareCowHeader));
|
|
|
|
|
Marshal.FreeHGlobal(cowPtr);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
MemoryStream ddfStream = new MemoryStream();
|
2017-12-30 23:58:39 +00:00
|
|
|
bool vmEHdrSet = false;
|
|
|
|
|
bool cowD = false;
|
2016-08-30 05:25:27 +01:00
|
|
|
|
2017-12-20 17:15:26 +00:00
|
|
|
if(vmEHdr.magic == VMWARE_EXTENT_MAGIC)
|
2016-08-30 05:25:27 +01:00
|
|
|
{
|
|
|
|
|
vmEHdrSet = true;
|
2017-12-30 23:58:39 +00:00
|
|
|
gdFilter = imageFilter;
|
2016-08-30 05:25:27 +01:00
|
|
|
|
|
|
|
|
if(vmEHdr.descriptorOffset == 0 || vmEHdr.descriptorSize == 0)
|
|
|
|
|
throw new Exception("Please open VMDK descriptor.");
|
|
|
|
|
|
2017-12-20 17:15:26 +00:00
|
|
|
byte[] ddfEmbed = new byte[vmEHdr.descriptorSize * SECTOR_SIZE];
|
2016-08-30 05:25:27 +01:00
|
|
|
|
2017-12-20 17:15:26 +00:00
|
|
|
stream.Seek((long)(vmEHdr.descriptorOffset * SECTOR_SIZE), SeekOrigin.Begin);
|
2016-08-30 05:25:27 +01:00
|
|
|
stream.Read(ddfEmbed, 0, ddfEmbed.Length);
|
|
|
|
|
ddfStream.Write(ddfEmbed, 0, ddfEmbed.Length);
|
|
|
|
|
|
|
|
|
|
embedded = true;
|
|
|
|
|
}
|
2017-12-20 17:15:26 +00:00
|
|
|
else if(vmCHdr.magic == VMWARE_COW_MAGIC)
|
2016-08-30 05:25:27 +01:00
|
|
|
{
|
2016-09-05 17:37:31 +01:00
|
|
|
gdFilter = imageFilter;
|
2017-12-30 23:58:39 +00:00
|
|
|
cowD = true;
|
2016-08-30 05:25:27 +01:00
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
byte[] ddfMagic = new byte[0x15];
|
|
|
|
|
stream.Seek(0, SeekOrigin.Begin);
|
|
|
|
|
stream.Read(ddfMagic, 0, 0x15);
|
|
|
|
|
|
2017-12-20 17:15:26 +00:00
|
|
|
if(!ddfMagicBytes.SequenceEqual(ddfMagic)) throw new Exception("Not a descriptor.");
|
2016-08-30 05:25:27 +01:00
|
|
|
|
|
|
|
|
stream.Seek(0, SeekOrigin.Begin);
|
2016-09-05 17:37:31 +01:00
|
|
|
byte[] ddfExternal = new byte[imageFilter.GetDataForkLength()];
|
2016-08-30 05:25:27 +01:00
|
|
|
stream.Read(ddfExternal, 0, ddfExternal.Length);
|
|
|
|
|
ddfStream.Write(ddfExternal, 0, ddfExternal.Length);
|
|
|
|
|
}
|
|
|
|
|
|
2018-06-22 08:08:38 +01:00
|
|
|
extents = new Dictionary<ulong, VMwareExtent>();
|
2016-08-30 05:25:27 +01:00
|
|
|
ulong currentSector = 0;
|
|
|
|
|
|
2017-12-19 20:33:03 +00:00
|
|
|
bool matchedCyls = false, matchedHds = false, matchedSpt = false;
|
2017-08-02 23:01:11 +01:00
|
|
|
|
2017-12-19 20:33:03 +00:00
|
|
|
if(cowD)
|
2016-08-30 05:25:27 +01:00
|
|
|
{
|
2017-12-30 23:58:39 +00:00
|
|
|
int cowCount = 1;
|
2016-09-05 17:37:31 +01:00
|
|
|
string basePath = Path.GetFileNameWithoutExtension(imageFilter.GetBasePath());
|
2016-08-30 05:25:27 +01:00
|
|
|
|
|
|
|
|
while(true)
|
|
|
|
|
{
|
|
|
|
|
string curPath;
|
2017-12-19 20:33:03 +00:00
|
|
|
if(cowCount == 1) curPath = basePath + ".vmdk";
|
2017-12-30 23:58:39 +00:00
|
|
|
else curPath = $"{basePath}-{cowCount:D2}.vmdk";
|
2016-08-30 05:25:27 +01:00
|
|
|
|
2017-12-19 20:33:03 +00:00
|
|
|
if(!File.Exists(curPath)) break;
|
2016-08-30 05:25:27 +01:00
|
|
|
|
2017-12-26 06:05:12 +00:00
|
|
|
IFilter extentFilter = new FiltersList().GetFilter(curPath);
|
2017-12-30 23:58:39 +00:00
|
|
|
Stream extentStream = extentFilter.GetDataForkStream();
|
2016-08-30 05:25:27 +01:00
|
|
|
|
|
|
|
|
if(stream.Length > Marshal.SizeOf(vmCHdr))
|
|
|
|
|
{
|
|
|
|
|
VMwareCowHeader extHdrCow = new VMwareCowHeader();
|
|
|
|
|
extentStream.Seek(0, SeekOrigin.Begin);
|
2017-12-20 17:15:26 +00:00
|
|
|
byte[] vmCHdrB = new byte[Marshal.SizeOf(extHdrCow)];
|
|
|
|
|
extentStream.Read(vmCHdrB, 0, Marshal.SizeOf(extHdrCow));
|
2016-08-30 05:25:27 +01:00
|
|
|
IntPtr cowPtr = Marshal.AllocHGlobal(Marshal.SizeOf(extHdrCow));
|
2017-12-20 17:15:26 +00:00
|
|
|
Marshal.Copy(vmCHdrB, 0, cowPtr, Marshal.SizeOf(extHdrCow));
|
2016-08-30 05:25:27 +01:00
|
|
|
extHdrCow = (VMwareCowHeader)Marshal.PtrToStructure(cowPtr, typeof(VMwareCowHeader));
|
|
|
|
|
Marshal.FreeHGlobal(cowPtr);
|
|
|
|
|
|
2017-12-20 17:15:26 +00:00
|
|
|
if(extHdrCow.magic != VMWARE_COW_MAGIC) break;
|
2016-08-30 05:25:27 +01:00
|
|
|
|
2017-12-22 06:55:04 +00:00
|
|
|
VMwareExtent newExtent = new VMwareExtent
|
|
|
|
|
{
|
2017-12-30 23:58:39 +00:00
|
|
|
Access = "RW",
|
|
|
|
|
Filter = extentFilter,
|
2017-12-22 06:55:04 +00:00
|
|
|
Filename = extentFilter.GetFilename(),
|
2017-12-30 23:58:39 +00:00
|
|
|
Offset = 0,
|
|
|
|
|
Sectors = extHdrCow.sectors,
|
|
|
|
|
Type = "SPARSE"
|
2017-12-22 06:55:04 +00:00
|
|
|
};
|
2016-08-30 05:25:27 +01:00
|
|
|
|
2017-12-20 17:15:26 +00:00
|
|
|
DicConsole.DebugWriteLine("VMware plugin", "{0} {1} {2} \"{3}\" {4}", newExtent.Access,
|
|
|
|
|
newExtent.Sectors, newExtent.Type, newExtent.Filename,
|
|
|
|
|
newExtent.Offset);
|
2016-08-30 05:25:27 +01:00
|
|
|
|
|
|
|
|
extents.Add(currentSector, newExtent);
|
2017-12-20 17:15:26 +00:00
|
|
|
currentSector += newExtent.Sectors;
|
2017-12-19 20:33:03 +00:00
|
|
|
}
|
|
|
|
|
else break;
|
2016-08-30 05:25:27 +01:00
|
|
|
|
|
|
|
|
cowCount++;
|
|
|
|
|
}
|
|
|
|
|
|
2017-12-20 17:15:26 +00:00
|
|
|
imageType = VM_TYPE_SPLIT_SPARSE;
|
2016-08-30 05:25:27 +01:00
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
ddfStream.Seek(0, SeekOrigin.Begin);
|
|
|
|
|
|
2017-12-30 23:58:39 +00:00
|
|
|
Regex regexVersion = new Regex(REGEX_VERSION);
|
|
|
|
|
Regex regexCid = new Regex(REGEX_CID);
|
2017-12-24 00:12:31 +00:00
|
|
|
Regex regexParentCid = new Regex(REGEX_CID_PARENT);
|
2017-12-30 23:58:39 +00:00
|
|
|
Regex regexType = new Regex(REGEX_TYPE);
|
|
|
|
|
Regex regexExtent = new Regex(REGEX_EXTENT);
|
|
|
|
|
Regex regexParent = new Regex(PARENT_REGEX);
|
2017-12-24 00:12:31 +00:00
|
|
|
Regex regexCylinders = new Regex(REGEX_DDB_CYLINDERS);
|
2017-12-30 23:58:39 +00:00
|
|
|
Regex regexHeads = new Regex(REGEX_DDB_HEADS);
|
|
|
|
|
Regex regexSectors = new Regex(REGEX_DDB_SECTORS);
|
2017-12-20 17:15:26 +00:00
|
|
|
|
2017-12-19 20:33:03 +00:00
|
|
|
StreamReader ddfStreamRdr = new StreamReader(ddfStream);
|
2016-08-30 05:25:27 +01:00
|
|
|
|
|
|
|
|
while(ddfStreamRdr.Peek() >= 0)
|
|
|
|
|
{
|
2017-12-20 17:15:26 +00:00
|
|
|
string line = ddfStreamRdr.ReadLine();
|
|
|
|
|
|
2017-12-30 23:58:39 +00:00
|
|
|
Match matchVersion = regexVersion.Match(line);
|
|
|
|
|
Match matchCid = regexCid.Match(line);
|
2017-12-22 06:55:04 +00:00
|
|
|
Match matchParentCid = regexParentCid.Match(line);
|
2017-12-30 23:58:39 +00:00
|
|
|
Match matchType = regexType.Match(line);
|
|
|
|
|
Match matchExtent = regexExtent.Match(line);
|
|
|
|
|
Match matchParent = regexParent.Match(line);
|
2017-12-22 06:55:04 +00:00
|
|
|
Match matchCylinders = regexCylinders.Match(line);
|
2017-12-30 23:58:39 +00:00
|
|
|
Match matchHeads = regexHeads.Match(line);
|
|
|
|
|
Match matchSectors = regexSectors.Match(line);
|
2017-12-20 17:15:26 +00:00
|
|
|
|
|
|
|
|
if(matchVersion.Success)
|
2017-12-19 20:33:03 +00:00
|
|
|
{
|
2017-12-20 17:15:26 +00:00
|
|
|
uint.TryParse(matchVersion.Groups["version"].Value, out version);
|
2017-12-19 20:33:03 +00:00
|
|
|
DicConsole.DebugWriteLine("VMware plugin", "version = {0}", version);
|
|
|
|
|
}
|
2017-12-20 17:15:26 +00:00
|
|
|
else if(matchCid.Success)
|
2017-12-19 20:33:03 +00:00
|
|
|
{
|
2017-12-20 17:15:26 +00:00
|
|
|
cid = Convert.ToUInt32(matchCid.Groups["cid"].Value, 16);
|
2017-12-19 20:33:03 +00:00
|
|
|
DicConsole.DebugWriteLine("VMware plugin", "cid = {0:x8}", cid);
|
|
|
|
|
}
|
2017-12-20 17:15:26 +00:00
|
|
|
else if(matchParentCid.Success)
|
2017-12-19 20:33:03 +00:00
|
|
|
{
|
2017-12-20 17:15:26 +00:00
|
|
|
parentCid = Convert.ToUInt32(matchParentCid.Groups["cid"].Value, 16);
|
2017-12-19 20:33:03 +00:00
|
|
|
DicConsole.DebugWriteLine("VMware plugin", "parentCID = {0:x8}", parentCid);
|
|
|
|
|
}
|
2017-12-20 17:15:26 +00:00
|
|
|
else if(matchType.Success)
|
2017-12-19 20:33:03 +00:00
|
|
|
{
|
2017-12-20 17:15:26 +00:00
|
|
|
imageType = matchType.Groups["type"].Value;
|
2017-12-19 20:33:03 +00:00
|
|
|
DicConsole.DebugWriteLine("VMware plugin", "createType = \"{0}\"", imageType);
|
|
|
|
|
}
|
2017-12-20 17:15:26 +00:00
|
|
|
else if(matchExtent.Success)
|
2017-12-19 20:33:03 +00:00
|
|
|
{
|
2017-12-22 06:55:04 +00:00
|
|
|
VMwareExtent newExtent = new VMwareExtent {Access = matchExtent.Groups["access"].Value};
|
2017-12-19 20:33:03 +00:00
|
|
|
if(!embedded)
|
2017-12-20 17:15:26 +00:00
|
|
|
newExtent.Filter =
|
2017-12-19 20:33:03 +00:00
|
|
|
new FiltersList()
|
2017-12-30 23:58:39 +00:00
|
|
|
.GetFilter(Path.Combine(Path.GetDirectoryName(imageFilter.GetBasePath()),
|
|
|
|
|
matchExtent.Groups["filename"].Value));
|
2017-12-20 17:15:26 +00:00
|
|
|
else newExtent.Filter = imageFilter;
|
2017-12-30 23:58:39 +00:00
|
|
|
uint.TryParse(matchExtent.Groups["offset"].Value, out newExtent.Offset);
|
2017-12-20 17:15:26 +00:00
|
|
|
uint.TryParse(matchExtent.Groups["sectors"].Value, out newExtent.Sectors);
|
|
|
|
|
newExtent.Type = matchExtent.Groups["type"].Value;
|
|
|
|
|
DicConsole.DebugWriteLine("VMware plugin", "{0} {1} {2} \"{3}\" {4}", newExtent.Access,
|
|
|
|
|
newExtent.Sectors, newExtent.Type, newExtent.Filename,
|
|
|
|
|
newExtent.Offset);
|
2017-12-19 20:33:03 +00:00
|
|
|
|
|
|
|
|
extents.Add(currentSector, newExtent);
|
2017-12-20 17:15:26 +00:00
|
|
|
currentSector += newExtent.Sectors;
|
2017-12-19 20:33:03 +00:00
|
|
|
}
|
2017-12-20 17:15:26 +00:00
|
|
|
else if(matchParent.Success)
|
2017-12-19 20:33:03 +00:00
|
|
|
{
|
2017-12-20 17:15:26 +00:00
|
|
|
parentName = matchParent.Groups["filename"].Value;
|
2017-12-19 20:33:03 +00:00
|
|
|
DicConsole.DebugWriteLine("VMware plugin", "parentFileNameHint = \"{0}\"", parentName);
|
|
|
|
|
hasParent = true;
|
|
|
|
|
}
|
2017-12-20 17:15:26 +00:00
|
|
|
else if(matchCylinders.Success)
|
2017-12-19 20:33:03 +00:00
|
|
|
{
|
2017-12-26 06:05:12 +00:00
|
|
|
uint.TryParse(matchCylinders.Groups["cylinders"].Value, out imageInfo.Cylinders);
|
2017-12-19 20:33:03 +00:00
|
|
|
matchedCyls = true;
|
|
|
|
|
}
|
2017-12-20 17:15:26 +00:00
|
|
|
else if(matchHeads.Success)
|
2017-12-19 20:33:03 +00:00
|
|
|
{
|
2017-12-26 06:05:12 +00:00
|
|
|
uint.TryParse(matchHeads.Groups["heads"].Value, out imageInfo.Heads);
|
2017-12-19 20:33:03 +00:00
|
|
|
matchedHds = true;
|
|
|
|
|
}
|
2017-12-20 17:15:26 +00:00
|
|
|
else if(matchSectors.Success)
|
2017-12-19 20:33:03 +00:00
|
|
|
{
|
2017-12-26 06:05:12 +00:00
|
|
|
uint.TryParse(matchSectors.Groups["sectors"].Value, out imageInfo.SectorsPerTrack);
|
2017-12-19 20:33:03 +00:00
|
|
|
matchedSpt = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-08-30 05:25:27 +01:00
|
|
|
}
|
|
|
|
|
|
2017-12-19 20:33:03 +00:00
|
|
|
if(extents.Count == 0) throw new Exception("Did not find any extent");
|
2016-08-30 05:25:27 +01:00
|
|
|
|
|
|
|
|
switch(imageType)
|
|
|
|
|
{
|
2017-12-30 23:58:39 +00:00
|
|
|
case VM_TYPE_MONO_SPARSE: //"monolithicSparse";
|
|
|
|
|
case VM_TYPE_MONO_FLAT: //"monolithicFlat";
|
2017-12-20 17:15:26 +00:00
|
|
|
case VM_TYPE_SPLIT_SPARSE: //"twoGbMaxExtentSparse";
|
2017-12-30 23:58:39 +00:00
|
|
|
case VM_TYPE_SPLIT_FLAT: //"twoGbMaxExtentFlat";
|
|
|
|
|
case VMFS_TYPE_FLAT: //"vmfsPreallocated";
|
|
|
|
|
case VMFS_TYPE_ZERO: //"vmfsEagerZeroedThick";
|
|
|
|
|
case VMFS_TYPE_THIN: //"vmfsThin";
|
|
|
|
|
case VMFS_TYPE_SPARSE: //"vmfsSparse";
|
|
|
|
|
case VMFS_TYPE: //"vmfs";
|
|
|
|
|
case VM_TYPE_STREAM: //"streamOptimized";
|
2016-08-30 05:25:27 +01:00
|
|
|
break;
|
2017-12-20 17:15:26 +00:00
|
|
|
case VM_TYPE_FULL_DEVICE: //"fullDevice";
|
|
|
|
|
case VM_TYPE_PART_DEVICE: //"partitionedDevice";
|
2017-12-30 23:58:39 +00:00
|
|
|
case VMFS_TYPE_RDM: //"vmfsRDM";
|
|
|
|
|
case VMFS_TYPE_RDM_OLD: //"vmfsRawDeviceMap";
|
|
|
|
|
case VMFS_TYPE_RDMP: //"vmfsRDMP";
|
|
|
|
|
case VMFS_TYPE_RDMP_OLD: //"vmfsPassthroughRawDeviceMap";
|
|
|
|
|
case VMFS_TYPE_RAW: //"vmfsRaw";
|
2017-12-19 20:33:03 +00:00
|
|
|
throw new
|
|
|
|
|
ImageNotSupportedException("Raw device image files are not supported, try accessing the device directly.");
|
2017-12-24 00:12:31 +00:00
|
|
|
default: throw new ImageNotSupportedException($"Dunno how to handle \"{imageType}\" extents.");
|
2016-08-30 05:25:27 +01:00
|
|
|
}
|
|
|
|
|
|
2017-12-22 06:55:04 +00:00
|
|
|
bool oneNoFlat = cowD;
|
2016-08-30 05:25:27 +01:00
|
|
|
|
|
|
|
|
foreach(VMwareExtent extent in extents.Values)
|
|
|
|
|
{
|
2017-12-24 00:12:31 +00:00
|
|
|
if(extent.Filter == null) throw new Exception($"Extent file {extent.Filename} not found.");
|
2016-08-30 05:25:27 +01:00
|
|
|
|
2017-12-20 17:15:26 +00:00
|
|
|
if(extent.Access == "NOACCESS") throw new Exception("Cannot access NOACCESS extents ;).");
|
2016-08-30 05:25:27 +01:00
|
|
|
|
2017-12-21 06:06:19 +00:00
|
|
|
if(extent.Type == "FLAT" || extent.Type == "ZERO" || extent.Type == "VMFS" || cowD) continue;
|
2016-08-30 05:25:27 +01:00
|
|
|
|
2017-12-21 06:06:19 +00:00
|
|
|
Stream extentStream = extent.Filter.GetDataForkStream();
|
|
|
|
|
extentStream.Seek(0, SeekOrigin.Begin);
|
2016-08-30 05:25:27 +01:00
|
|
|
|
2017-12-24 00:12:31 +00:00
|
|
|
if(extentStream.Length < SECTOR_SIZE) throw new Exception($"Extent {extent.Filename} is too small.");
|
2016-08-30 05:25:27 +01:00
|
|
|
|
2017-12-30 23:58:39 +00:00
|
|
|
VMwareExtentHeader extentHdr = new VMwareExtentHeader();
|
|
|
|
|
byte[] extentHdrB = new byte[Marshal.SizeOf(extentHdr)];
|
2017-12-21 06:06:19 +00:00
|
|
|
extentStream.Read(extentHdrB, 0, Marshal.SizeOf(extentHdr));
|
|
|
|
|
IntPtr extentHdrPtr = Marshal.AllocHGlobal(Marshal.SizeOf(extentHdr));
|
|
|
|
|
Marshal.Copy(extentHdrB, 0, extentHdrPtr, Marshal.SizeOf(extentHdr));
|
|
|
|
|
extentHdr = (VMwareExtentHeader)Marshal.PtrToStructure(extentHdrPtr, typeof(VMwareExtentHeader));
|
|
|
|
|
Marshal.FreeHGlobal(extentHdrPtr);
|
2016-08-30 05:25:27 +01:00
|
|
|
|
2017-12-21 06:06:19 +00:00
|
|
|
if(extentHdr.magic != VMWARE_EXTENT_MAGIC)
|
2017-12-21 17:58:51 +00:00
|
|
|
throw new Exception($"{extent.Filter} is not an VMware extent.");
|
2016-08-30 05:25:27 +01:00
|
|
|
|
2017-12-21 06:06:19 +00:00
|
|
|
if(extentHdr.capacity < extent.Sectors)
|
|
|
|
|
throw new
|
2017-12-21 17:58:51 +00:00
|
|
|
Exception($"Extent contains incorrect number of sectors, {extentHdr.capacity}. {extent.Sectors} were expected");
|
2016-08-30 05:25:27 +01:00
|
|
|
|
2017-12-21 06:06:19 +00:00
|
|
|
// TODO: Support compressed extents
|
|
|
|
|
if(extentHdr.compression != COMPRESSION_NONE)
|
|
|
|
|
throw new ImageNotSupportedException("Compressed extents are not yet supported.");
|
2016-08-30 05:25:27 +01:00
|
|
|
|
2017-12-21 06:06:19 +00:00
|
|
|
if(!vmEHdrSet)
|
|
|
|
|
{
|
2017-12-30 23:58:39 +00:00
|
|
|
vmEHdr = extentHdr;
|
|
|
|
|
gdFilter = extent.Filter;
|
2017-12-21 06:06:19 +00:00
|
|
|
vmEHdrSet = true;
|
2016-08-30 05:25:27 +01:00
|
|
|
}
|
2017-12-21 06:06:19 +00:00
|
|
|
|
|
|
|
|
oneNoFlat = true;
|
2016-08-30 05:25:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(oneNoFlat && !vmEHdrSet && !cowD)
|
2017-12-19 20:33:03 +00:00
|
|
|
throw new
|
|
|
|
|
Exception("There are sparse extents but there is no header to find the grain tables, cannot proceed.");
|
2016-08-30 05:25:27 +01:00
|
|
|
|
2017-12-26 06:05:12 +00:00
|
|
|
imageInfo.Sectors = currentSector;
|
2016-08-30 05:25:27 +01:00
|
|
|
|
2017-12-30 23:58:39 +00:00
|
|
|
uint grains = 0;
|
2016-08-30 05:25:27 +01:00
|
|
|
uint gdEntries = 0;
|
2017-12-30 23:58:39 +00:00
|
|
|
long gdOffset = 0;
|
2017-12-20 17:15:26 +00:00
|
|
|
uint gtEsPerGt = 0;
|
2016-08-30 05:25:27 +01:00
|
|
|
|
|
|
|
|
if(oneNoFlat && !cowD)
|
|
|
|
|
{
|
2017-12-30 23:58:39 +00:00
|
|
|
DicConsole.DebugWriteLine("VMware plugin", "vmEHdr.magic = 0x{0:X8}", vmEHdr.magic);
|
|
|
|
|
DicConsole.DebugWriteLine("VMware plugin", "vmEHdr.version = {0}", vmEHdr.version);
|
|
|
|
|
DicConsole.DebugWriteLine("VMware plugin", "vmEHdr.flags = 0x{0:X8}", vmEHdr.flags);
|
|
|
|
|
DicConsole.DebugWriteLine("VMware plugin", "vmEHdr.capacity = {0}", vmEHdr.capacity);
|
|
|
|
|
DicConsole.DebugWriteLine("VMware plugin", "vmEHdr.grainSize = {0}", vmEHdr.grainSize);
|
2016-08-30 05:25:27 +01:00
|
|
|
DicConsole.DebugWriteLine("VMware plugin", "vmEHdr.descriptorOffset = {0}", vmEHdr.descriptorOffset);
|
2017-12-30 23:58:39 +00:00
|
|
|
DicConsole.DebugWriteLine("VMware plugin", "vmEHdr.descriptorSize = {0}", vmEHdr.descriptorSize);
|
|
|
|
|
DicConsole.DebugWriteLine("VMware plugin", "vmEHdr.GTEsPerGT = {0}", vmEHdr.GTEsPerGT);
|
|
|
|
|
DicConsole.DebugWriteLine("VMware plugin", "vmEHdr.rgdOffset = {0}", vmEHdr.rgdOffset);
|
|
|
|
|
DicConsole.DebugWriteLine("VMware plugin", "vmEHdr.gdOffset = {0}", vmEHdr.gdOffset);
|
|
|
|
|
DicConsole.DebugWriteLine("VMware plugin", "vmEHdr.overhead = {0}", vmEHdr.overhead);
|
|
|
|
|
DicConsole.DebugWriteLine("VMware plugin", "vmEHdr.uncleanShutdown = {0}", vmEHdr.uncleanShutdown);
|
2017-12-19 20:33:03 +00:00
|
|
|
DicConsole.DebugWriteLine("VMware plugin", "vmEHdr.singleEndLineChar = 0x{0:X2}",
|
|
|
|
|
vmEHdr.singleEndLineChar);
|
2016-08-30 05:25:27 +01:00
|
|
|
DicConsole.DebugWriteLine("VMware plugin", "vmEHdr.nonEndLineChar = 0x{0:X2}", vmEHdr.nonEndLineChar);
|
2017-12-19 20:33:03 +00:00
|
|
|
DicConsole.DebugWriteLine("VMware plugin", "vmEHdr.doubleEndLineChar1 = 0x{0:X2}",
|
|
|
|
|
vmEHdr.doubleEndLineChar1);
|
|
|
|
|
DicConsole.DebugWriteLine("VMware plugin", "vmEHdr.doubleEndLineChar2 = 0x{0:X2}",
|
|
|
|
|
vmEHdr.doubleEndLineChar2);
|
2016-08-30 05:25:27 +01:00
|
|
|
DicConsole.DebugWriteLine("VMware plugin", "vmEHdr.compression = 0x{0:X4}", vmEHdr.compression);
|
|
|
|
|
|
|
|
|
|
grainSize = vmEHdr.grainSize;
|
2017-12-30 23:58:39 +00:00
|
|
|
grains = (uint)(imageInfo.Sectors / vmEHdr.grainSize) + 1;
|
2018-06-22 08:08:38 +01:00
|
|
|
gdEntries = grains / vmEHdr.GTEsPerGT;
|
2017-12-20 17:15:26 +00:00
|
|
|
gtEsPerGt = vmEHdr.GTEsPerGT;
|
2016-08-30 05:25:27 +01:00
|
|
|
|
2017-12-24 00:12:31 +00:00
|
|
|
if((vmEHdr.flags & FLAGS_USE_REDUNDANT_TABLE) == FLAGS_USE_REDUNDANT_TABLE)
|
2017-12-30 23:58:39 +00:00
|
|
|
gdOffset = (long)vmEHdr.rgdOffset;
|
2017-12-19 20:33:03 +00:00
|
|
|
else gdOffset = (long)vmEHdr.gdOffset;
|
2016-08-30 05:25:27 +01:00
|
|
|
}
|
|
|
|
|
else if(oneNoFlat && cowD)
|
|
|
|
|
{
|
2017-12-30 23:58:39 +00:00
|
|
|
DicConsole.DebugWriteLine("VMware plugin", "vmCHdr.magic = 0x{0:X8}", vmCHdr.magic);
|
|
|
|
|
DicConsole.DebugWriteLine("VMware plugin", "vmCHdr.version = {0}", vmCHdr.version);
|
|
|
|
|
DicConsole.DebugWriteLine("VMware plugin", "vmCHdr.flags = 0x{0:X8}", vmCHdr.flags);
|
|
|
|
|
DicConsole.DebugWriteLine("VMware plugin", "vmCHdr.sectors = {0}", vmCHdr.sectors);
|
|
|
|
|
DicConsole.DebugWriteLine("VMware plugin", "vmCHdr.grainSize = {0}", vmCHdr.grainSize);
|
|
|
|
|
DicConsole.DebugWriteLine("VMware plugin", "vmCHdr.gdOffset = {0}", vmCHdr.gdOffset);
|
2016-08-30 05:25:27 +01:00
|
|
|
DicConsole.DebugWriteLine("VMware plugin", "vmCHdr.numGDEntries = {0}", vmCHdr.numGDEntries);
|
2017-12-30 23:58:39 +00:00
|
|
|
DicConsole.DebugWriteLine("VMware plugin", "vmCHdr.freeSector = {0}", vmCHdr.freeSector);
|
|
|
|
|
DicConsole.DebugWriteLine("VMware plugin", "vmCHdr.cylinders = {0}", vmCHdr.cylinders);
|
|
|
|
|
DicConsole.DebugWriteLine("VMware plugin", "vmCHdr.heads = {0}", vmCHdr.heads);
|
|
|
|
|
DicConsole.DebugWriteLine("VMware plugin", "vmCHdr.spt = {0}", vmCHdr.spt);
|
|
|
|
|
DicConsole.DebugWriteLine("VMware plugin", "vmCHdr.generation = {0}", vmCHdr.generation);
|
|
|
|
|
DicConsole.DebugWriteLine("VMware plugin", "vmCHdr.name = {0}",
|
|
|
|
|
StringHandlers.CToString(vmCHdr.name));
|
2017-12-19 20:33:03 +00:00
|
|
|
DicConsole.DebugWriteLine("VMware plugin", "vmCHdr.description = {0}",
|
|
|
|
|
StringHandlers.CToString(vmCHdr.description));
|
2016-08-30 05:25:27 +01:00
|
|
|
DicConsole.DebugWriteLine("VMware plugin", "vmCHdr.savedGeneration = {0}", vmCHdr.savedGeneration);
|
|
|
|
|
DicConsole.DebugWriteLine("VMware plugin", "vmCHdr.uncleanShutdown = {0}", vmCHdr.uncleanShutdown);
|
|
|
|
|
|
2017-12-30 23:58:39 +00:00
|
|
|
grainSize = vmCHdr.grainSize;
|
|
|
|
|
grains = (uint)(imageInfo.Sectors / vmCHdr.grainSize) + 1;
|
|
|
|
|
gdEntries = vmCHdr.numGDEntries;
|
|
|
|
|
gdOffset = vmCHdr.gdOffset;
|
|
|
|
|
gtEsPerGt = grains / gdEntries;
|
2017-12-26 06:05:12 +00:00
|
|
|
imageInfo.MediaTitle = StringHandlers.CToString(vmCHdr.name);
|
2017-12-30 23:58:39 +00:00
|
|
|
imageInfo.Comments = StringHandlers.CToString(vmCHdr.description);
|
|
|
|
|
version = vmCHdr.version;
|
2016-08-30 05:25:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(oneNoFlat)
|
|
|
|
|
{
|
2017-12-19 20:33:03 +00:00
|
|
|
if(grains == 0 || gdEntries == 0) throw new Exception("Some error ocurred setting GD sizes");
|
2016-08-30 05:25:27 +01:00
|
|
|
|
2017-12-26 06:05:12 +00:00
|
|
|
DicConsole.DebugWriteLine("VMware plugin", "{0} sectors in {1} grains in {2} tables", imageInfo.Sectors,
|
2017-12-19 20:33:03 +00:00
|
|
|
grains, gdEntries);
|
2016-08-30 05:25:27 +01:00
|
|
|
|
2016-09-05 17:37:31 +01:00
|
|
|
Stream gdStream = gdFilter.GetDataForkStream();
|
2016-08-30 05:25:27 +01:00
|
|
|
|
2017-12-20 17:15:26 +00:00
|
|
|
gdStream.Seek(gdOffset * SECTOR_SIZE, SeekOrigin.Begin);
|
2016-08-30 05:25:27 +01:00
|
|
|
|
|
|
|
|
DicConsole.DebugWriteLine("VMware plugin", "Reading grain directory");
|
2017-12-30 23:58:39 +00:00
|
|
|
uint[] gd = new uint[gdEntries];
|
2016-08-30 05:25:27 +01:00
|
|
|
byte[] gdBytes = new byte[gdEntries * 4];
|
|
|
|
|
gdStream.Read(gdBytes, 0, gdBytes.Length);
|
2017-12-19 20:33:03 +00:00
|
|
|
for(int i = 0; i < gdEntries; i++) gd[i] = BitConverter.ToUInt32(gdBytes, i * 4);
|
2016-08-30 05:25:27 +01:00
|
|
|
|
|
|
|
|
DicConsole.DebugWriteLine("VMware plugin", "Reading grain tables");
|
|
|
|
|
uint currentGrain = 0;
|
2018-06-22 08:08:38 +01:00
|
|
|
gTable = new uint[grains];
|
2016-08-30 05:25:27 +01:00
|
|
|
foreach(uint gtOff in gd)
|
|
|
|
|
{
|
2017-12-20 17:15:26 +00:00
|
|
|
byte[] gtBytes = new byte[gtEsPerGt * 4];
|
2018-06-22 08:08:38 +01:00
|
|
|
gdStream.Seek(gtOff * SECTOR_SIZE, SeekOrigin.Begin);
|
2016-08-30 05:25:27 +01:00
|
|
|
gdStream.Read(gtBytes, 0, gtBytes.Length);
|
2017-12-20 17:15:26 +00:00
|
|
|
for(int i = 0; i < gtEsPerGt; i++)
|
2016-08-30 05:25:27 +01:00
|
|
|
{
|
|
|
|
|
gTable[currentGrain] = BitConverter.ToUInt32(gtBytes, i * 4);
|
|
|
|
|
currentGrain++;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-12-20 17:15:26 +00:00
|
|
|
maxCachedGrains = (uint)(MAX_CACHE_SIZE / (grainSize * SECTOR_SIZE));
|
2016-08-30 05:25:27 +01:00
|
|
|
|
|
|
|
|
grainCache = new Dictionary<ulong, byte[]>();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(hasParent)
|
|
|
|
|
{
|
2017-12-26 06:05:12 +00:00
|
|
|
IFilter parentFilter =
|
2017-12-19 20:33:03 +00:00
|
|
|
new FiltersList().GetFilter(Path.Combine(imageFilter.GetParentFolder(), parentName));
|
2017-12-21 17:58:51 +00:00
|
|
|
if(parentFilter == null) throw new Exception($"Cannot find parent \"{parentName}\".");
|
2016-08-30 05:25:27 +01:00
|
|
|
|
|
|
|
|
parentImage = new VMware();
|
|
|
|
|
|
2017-12-28 19:56:36 +00:00
|
|
|
if(!parentImage.Open(parentFilter)) throw new Exception($"Cannot open parent \"{parentName}\".");
|
2016-08-30 05:25:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
sectorCache = new Dictionary<ulong, byte[]>();
|
|
|
|
|
|
2017-12-30 23:58:39 +00:00
|
|
|
imageInfo.CreationTime = imageFilter.GetCreationTime();
|
2017-12-26 06:05:12 +00:00
|
|
|
imageInfo.LastModificationTime = imageFilter.GetLastWriteTime();
|
2017-12-30 23:58:39 +00:00
|
|
|
imageInfo.MediaTitle = Path.GetFileNameWithoutExtension(imageFilter.GetFilename());
|
|
|
|
|
imageInfo.SectorSize = SECTOR_SIZE;
|
|
|
|
|
imageInfo.XmlMediaType = XmlMediaType.BlockMedia;
|
|
|
|
|
imageInfo.MediaType = MediaType.GENERIC_HDD;
|
|
|
|
|
imageInfo.ImageSize = imageInfo.Sectors * SECTOR_SIZE;
|
2016-08-30 05:25:27 +01:00
|
|
|
// VMDK version 1 started on VMware 4, so there is a previous version, "COWD"
|
2017-12-26 06:05:12 +00:00
|
|
|
imageInfo.Version = cowD ? $"{version}" : $"{version + 3}";
|
2017-12-19 20:33:03 +00:00
|
|
|
|
2016-08-30 05:25:27 +01:00
|
|
|
if(cowD)
|
2017-12-19 20:33:03 +00:00
|
|
|
{
|
2017-12-30 23:58:39 +00:00
|
|
|
imageInfo.Cylinders = vmCHdr.cylinders;
|
|
|
|
|
imageInfo.Heads = vmCHdr.heads;
|
2017-12-26 06:05:12 +00:00
|
|
|
imageInfo.SectorsPerTrack = vmCHdr.spt;
|
2017-12-19 20:33:03 +00:00
|
|
|
}
|
|
|
|
|
else if(!matchedCyls || !matchedHds || !matchedSpt)
|
|
|
|
|
{
|
2017-12-30 23:58:39 +00:00
|
|
|
imageInfo.Cylinders = (uint)(imageInfo.Sectors / 16 / 63);
|
|
|
|
|
imageInfo.Heads = 16;
|
2017-12-26 06:05:12 +00:00
|
|
|
imageInfo.SectorsPerTrack = 63;
|
2017-12-19 20:33:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
2016-08-30 05:25:27 +01:00
|
|
|
}
|
|
|
|
|
|
2017-12-26 07:28:40 +00:00
|
|
|
public byte[] ReadSector(ulong sectorAddress)
|
2016-08-30 05:25:27 +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-30 05:25:27 +01:00
|
|
|
|
2017-12-22 06:55:04 +00:00
|
|
|
if(sectorCache.TryGetValue(sectorAddress, out byte[] sector)) return sector;
|
2016-08-30 05:25:27 +01:00
|
|
|
|
2017-12-30 23:58:39 +00:00
|
|
|
VMwareExtent currentExtent = new VMwareExtent();
|
|
|
|
|
bool extentFound = false;
|
|
|
|
|
ulong extentStartSector = 0;
|
2016-08-30 05:25:27 +01:00
|
|
|
|
2017-12-24 00:12:31 +00:00
|
|
|
foreach(KeyValuePair<ulong, VMwareExtent> kvp in extents.Where(kvp => sectorAddress >= kvp.Key))
|
|
|
|
|
{
|
2017-12-30 23:58:39 +00:00
|
|
|
currentExtent = kvp.Value;
|
|
|
|
|
extentFound = true;
|
2017-12-21 07:08:26 +00:00
|
|
|
extentStartSector = kvp.Key;
|
|
|
|
|
}
|
2016-08-30 05:25:27 +01:00
|
|
|
|
|
|
|
|
if(!extentFound)
|
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-30 05:25:27 +01:00
|
|
|
|
2016-09-05 17:37:31 +01:00
|
|
|
Stream dataStream;
|
2016-08-30 05:25:27 +01:00
|
|
|
|
2017-12-24 00:12:31 +00:00
|
|
|
switch(currentExtent.Type)
|
|
|
|
|
{
|
2017-12-21 04:43:29 +00:00
|
|
|
case "ZERO":
|
|
|
|
|
sector = new byte[SECTOR_SIZE];
|
|
|
|
|
|
2017-12-22 06:55:04 +00:00
|
|
|
if(sectorCache.Count >= MAX_CACHED_SECTORS) sectorCache.Clear();
|
2017-12-21 04:43:29 +00:00
|
|
|
|
|
|
|
|
sectorCache.Add(sectorAddress, sector);
|
|
|
|
|
return sector;
|
|
|
|
|
case "FLAT":
|
|
|
|
|
case "VMFS":
|
|
|
|
|
dataStream = currentExtent.Filter.GetDataForkStream();
|
|
|
|
|
dataStream.Seek((long)((currentExtent.Offset + (sectorAddress - extentStartSector)) * SECTOR_SIZE),
|
|
|
|
|
SeekOrigin.Begin);
|
|
|
|
|
sector = new byte[SECTOR_SIZE];
|
|
|
|
|
dataStream.Read(sector, 0, sector.Length);
|
|
|
|
|
|
2017-12-22 06:55:04 +00:00
|
|
|
if(sectorCache.Count >= MAX_CACHED_SECTORS) sectorCache.Clear();
|
2017-12-21 04:43:29 +00:00
|
|
|
|
|
|
|
|
sectorCache.Add(sectorAddress, sector);
|
|
|
|
|
return sector;
|
2016-08-30 05:25:27 +01:00
|
|
|
}
|
|
|
|
|
|
2018-06-22 08:08:38 +01:00
|
|
|
ulong index = sectorAddress / grainSize;
|
2017-12-20 17:26:28 +00:00
|
|
|
ulong secOff = sectorAddress % grainSize * SECTOR_SIZE;
|
2016-08-30 05:25:27 +01:00
|
|
|
|
|
|
|
|
uint grainOff = gTable[index];
|
|
|
|
|
|
2017-12-19 20:33:03 +00:00
|
|
|
if(grainOff == 0 && hasParent) return parentImage.ReadSector(sectorAddress);
|
2016-08-30 05:25:27 +01:00
|
|
|
|
|
|
|
|
if(grainOff == 0 || grainOff == 1)
|
|
|
|
|
{
|
2017-12-20 17:15:26 +00:00
|
|
|
sector = new byte[SECTOR_SIZE];
|
2016-08-30 05:25:27 +01:00
|
|
|
|
2017-12-22 06:55:04 +00:00
|
|
|
if(sectorCache.Count >= MAX_CACHED_SECTORS) sectorCache.Clear();
|
2016-08-30 05:25:27 +01:00
|
|
|
|
|
|
|
|
sectorCache.Add(sectorAddress, sector);
|
|
|
|
|
return sector;
|
|
|
|
|
}
|
|
|
|
|
|
2017-12-22 06:55:04 +00:00
|
|
|
if(!grainCache.TryGetValue(grainOff, out byte[] grain))
|
2016-08-30 05:25:27 +01:00
|
|
|
{
|
2017-12-30 23:58:39 +00:00
|
|
|
grain = new byte[SECTOR_SIZE * grainSize];
|
2017-12-20 17:15:26 +00:00
|
|
|
dataStream = currentExtent.Filter.GetDataForkStream();
|
2017-12-20 17:26:28 +00:00
|
|
|
dataStream.Seek((long)((grainOff - extentStartSector) * SECTOR_SIZE), SeekOrigin.Begin);
|
2016-08-30 05:25:27 +01:00
|
|
|
dataStream.Read(grain, 0, grain.Length);
|
|
|
|
|
|
2017-12-19 20:33:03 +00:00
|
|
|
if(grainCache.Count >= maxCachedGrains) grainCache.Clear();
|
2016-08-30 05:25:27 +01:00
|
|
|
|
|
|
|
|
grainCache.Add(grainOff, grain);
|
|
|
|
|
}
|
|
|
|
|
|
2017-12-20 17:15:26 +00:00
|
|
|
sector = new byte[SECTOR_SIZE];
|
|
|
|
|
Array.Copy(grain, (int)secOff, sector, 0, SECTOR_SIZE);
|
2016-08-30 05:25:27 +01:00
|
|
|
|
2017-12-22 06:55:04 +00:00
|
|
|
if(sectorCache.Count > MAX_CACHED_SECTORS) sectorCache.Clear();
|
2016-08-30 05:25:27 +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-30 05:25:27 +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-30 05:25:27 +01:00
|
|
|
|
2017-12-26 06:05:12 +00:00
|
|
|
if(sectorAddress + length > imageInfo.Sectors)
|
2016-08-30 05:25:27 +01:00
|
|
|
throw new ArgumentOutOfRangeException(nameof(length), "Requested more sectors than available");
|
|
|
|
|
|
|
|
|
|
MemoryStream ms = new MemoryStream();
|
|
|
|
|
|
|
|
|
|
for(uint i = 0; i < length; i++)
|
|
|
|
|
{
|
|
|
|
|
byte[] sector = ReadSector(sectorAddress + i);
|
|
|
|
|
ms.Write(sector, 0, sector.Length);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return ms.ToArray();
|
|
|
|
|
}
|
|
|
|
|
|
2017-12-26 07:28:40 +00:00
|
|
|
public byte[] ReadSectorTag(ulong sectorAddress, SectorTagType tag)
|
2016-08-30 05:25:27 +01:00
|
|
|
{
|
|
|
|
|
throw new FeatureUnsupportedImageException("Feature not supported by image format");
|
|
|
|
|
}
|
|
|
|
|
|
2017-12-26 07:28:40 +00:00
|
|
|
public byte[] ReadSectorsTag(ulong sectorAddress, uint length, SectorTagType tag)
|
2016-08-30 05:25:27 +01:00
|
|
|
{
|
|
|
|
|
throw new FeatureUnsupportedImageException("Feature not supported by image format");
|
|
|
|
|
}
|
|
|
|
|
|
2017-12-26 07:28:40 +00:00
|
|
|
public byte[] ReadDiskTag(MediaTagType tag)
|
2016-08-30 05:25:27 +01:00
|
|
|
{
|
|
|
|
|
throw new FeatureUnsupportedImageException("Feature not supported by image format");
|
|
|
|
|
}
|
|
|
|
|
|
2017-12-26 07:28:40 +00:00
|
|
|
public byte[] ReadSector(ulong sectorAddress, uint track)
|
2016-08-30 05:25:27 +01:00
|
|
|
{
|
|
|
|
|
throw new FeatureUnsupportedImageException("Feature not supported by image format");
|
|
|
|
|
}
|
|
|
|
|
|
2017-12-26 07:28:40 +00:00
|
|
|
public byte[] ReadSectorTag(ulong sectorAddress, uint track, SectorTagType tag)
|
2016-08-30 05:25:27 +01:00
|
|
|
{
|
|
|
|
|
throw new FeatureUnsupportedImageException("Feature not supported by image format");
|
|
|
|
|
}
|
|
|
|
|
|
2017-12-26 07:28:40 +00:00
|
|
|
public byte[] ReadSectors(ulong sectorAddress, uint length, uint track)
|
2016-08-30 05:25:27 +01:00
|
|
|
{
|
|
|
|
|
throw new FeatureUnsupportedImageException("Feature not supported by image format");
|
|
|
|
|
}
|
|
|
|
|
|
2017-12-26 07:28:40 +00:00
|
|
|
public byte[] ReadSectorsTag(ulong sectorAddress, uint length, uint track, SectorTagType tag)
|
2016-08-30 05:25:27 +01:00
|
|
|
{
|
|
|
|
|
throw new FeatureUnsupportedImageException("Feature not supported by image format");
|
|
|
|
|
}
|
|
|
|
|
|
2017-12-26 07:28:40 +00:00
|
|
|
public byte[] ReadSectorLong(ulong sectorAddress)
|
2016-08-30 05:25:27 +01:00
|
|
|
{
|
|
|
|
|
throw new FeatureUnsupportedImageException("Feature not supported by image format");
|
|
|
|
|
}
|
|
|
|
|
|
2017-12-26 07:28:40 +00:00
|
|
|
public byte[] ReadSectorLong(ulong sectorAddress, uint track)
|
2016-08-30 05:25:27 +01:00
|
|
|
{
|
|
|
|
|
throw new FeatureUnsupportedImageException("Feature not supported by image format");
|
|
|
|
|
}
|
|
|
|
|
|
2017-12-26 07:28:40 +00:00
|
|
|
public byte[] ReadSectorsLong(ulong sectorAddress, uint length)
|
2016-08-30 05:25:27 +01:00
|
|
|
{
|
|
|
|
|
throw new FeatureUnsupportedImageException("Feature not supported by image format");
|
|
|
|
|
}
|
|
|
|
|
|
2017-12-26 07:28:40 +00:00
|
|
|
public byte[] ReadSectorsLong(ulong sectorAddress, uint length, uint track)
|
2016-08-30 05:25:27 +01:00
|
|
|
{
|
|
|
|
|
throw new FeatureUnsupportedImageException("Feature not supported by image format");
|
|
|
|
|
}
|
|
|
|
|
|
2017-12-26 07:28:40 +00:00
|
|
|
public List<Track> GetSessionTracks(Session session)
|
2016-08-30 05:25:27 +01:00
|
|
|
{
|
|
|
|
|
throw new FeatureUnsupportedImageException("Feature not supported by image format");
|
|
|
|
|
}
|
|
|
|
|
|
2017-12-26 07:28:40 +00:00
|
|
|
public List<Track> GetSessionTracks(ushort session)
|
2016-08-30 05:25:27 +01:00
|
|
|
{
|
|
|
|
|
throw new FeatureUnsupportedImageException("Feature not supported by image format");
|
|
|
|
|
}
|
|
|
|
|
|
2017-12-26 07:28:40 +00:00
|
|
|
public bool? VerifySector(ulong sectorAddress)
|
2016-08-30 05:25:27 +01:00
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
2017-12-26 07:28:40 +00:00
|
|
|
public bool? VerifySector(ulong sectorAddress, uint track)
|
2016-08-30 05:25:27 +01:00
|
|
|
{
|
|
|
|
|
throw new FeatureUnsupportedImageException("Feature not supported by image format");
|
|
|
|
|
}
|
|
|
|
|
|
2018-06-22 08:08:38 +01:00
|
|
|
public bool? VerifySectors(ulong sectorAddress, uint length, out List<ulong> failingLbas,
|
|
|
|
|
out List<ulong> unknownLbas)
|
2016-08-30 05:25:27 +01:00
|
|
|
{
|
2017-12-20 17:15:26 +00:00
|
|
|
failingLbas = new List<ulong>();
|
|
|
|
|
unknownLbas = new List<ulong>();
|
2017-12-26 06:05:12 +00:00
|
|
|
for(ulong i = 0; i < imageInfo.Sectors; i++) unknownLbas.Add(i);
|
2017-12-19 20:33:03 +00:00
|
|
|
|
2016-08-30 05:25:27 +01:00
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
2018-06-22 08:08:38 +01:00
|
|
|
public bool? VerifySectors(ulong sectorAddress, uint length, uint track, out List<ulong> failingLbas,
|
|
|
|
|
out List<ulong> unknownLbas)
|
2016-08-30 05:25:27 +01:00
|
|
|
{
|
|
|
|
|
throw new FeatureUnsupportedImageException("Feature not supported by image format");
|
|
|
|
|
}
|
|
|
|
|
|
2017-12-26 07:28:40 +00:00
|
|
|
public bool? VerifyMediaImage()
|
2016-08-30 05:25:27 +01:00
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
2017-12-24 00:12:31 +00:00
|
|
|
|
2018-01-28 20:29:46 +00:00
|
|
|
public List<DumpHardwareType> DumpHardware => null;
|
|
|
|
|
public CICMMetadataType CicmMetadata => null;
|
|
|
|
|
|
2017-12-30 23:58:39 +00:00
|
|
|
public IEnumerable<MediaTagType> SupportedMediaTags => new MediaTagType[] { };
|
|
|
|
|
public IEnumerable<SectorTagType> SupportedSectorTags => new SectorTagType[] { };
|
2018-06-22 08:08:38 +01:00
|
|
|
public IEnumerable<MediaType> SupportedMediaTypes =>
|
2018-01-18 23:18:56 +00:00
|
|
|
new[]
|
|
|
|
|
{
|
|
|
|
|
MediaType.GENERIC_HDD, MediaType.Unknown, MediaType.FlashDrive, MediaType.CompactFlash,
|
|
|
|
|
MediaType.CompactFlashType2, MediaType.PCCardTypeI, MediaType.PCCardTypeII, MediaType.PCCardTypeIII,
|
|
|
|
|
MediaType.PCCardTypeIV
|
|
|
|
|
};
|
2017-12-30 23:58:39 +00:00
|
|
|
public IEnumerable<(string name, Type type, string description)> SupportedOptions =>
|
|
|
|
|
new[]
|
|
|
|
|
{
|
|
|
|
|
("adapter_type", typeof(string),
|
2018-06-22 08:08:38 +01:00
|
|
|
"Type of adapter type. Possible values: ide, lsilogic, buslogic, legacyESX."),
|
2018-01-17 19:22:00 +00:00
|
|
|
("hwversion", typeof(uint), "VDMK hardware version."), ("sparse", typeof(bool), "Use sparse extents."),
|
2017-12-30 23:58:39 +00:00
|
|
|
("split", typeof(bool), "Split data file at 2GiB.")
|
|
|
|
|
};
|
|
|
|
|
public IEnumerable<string> KnownExtensions => new[] {".vmdk"};
|
|
|
|
|
public bool IsWriting { get; private set; }
|
|
|
|
|
public string ErrorMessage { get; private set; }
|
|
|
|
|
|
|
|
|
|
public bool Create(string path, MediaType mediaType, Dictionary<string, string> options, ulong sectors,
|
|
|
|
|
uint sectorSize)
|
|
|
|
|
{
|
|
|
|
|
if(options != null)
|
|
|
|
|
{
|
|
|
|
|
if(options.TryGetValue("adapter", out adapter_type))
|
|
|
|
|
switch(adapter_type.ToLowerInvariant())
|
|
|
|
|
{
|
|
|
|
|
case "ide":
|
|
|
|
|
case "lsilogic":
|
|
|
|
|
case "buslogic":
|
|
|
|
|
adapter_type = adapter_type.ToLowerInvariant();
|
|
|
|
|
break;
|
|
|
|
|
case "legacyesx":
|
|
|
|
|
adapter_type = "legacyESX";
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
ErrorMessage = $"Invalid adapter type {adapter_type}";
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
else adapter_type = "ide";
|
|
|
|
|
|
|
|
|
|
if(options.TryGetValue("hwversion", out string tmpValue))
|
|
|
|
|
{
|
2018-01-17 19:22:00 +00:00
|
|
|
if(!uint.TryParse(tmpValue, out hwversion))
|
2017-12-30 23:58:39 +00:00
|
|
|
{
|
|
|
|
|
ErrorMessage = "Invalid value for hwversion option";
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else hwversion = 4;
|
|
|
|
|
|
|
|
|
|
if(options.TryGetValue("split", out tmpValue))
|
|
|
|
|
{
|
|
|
|
|
if(!bool.TryParse(tmpValue, out bool tmpBool))
|
|
|
|
|
{
|
|
|
|
|
ErrorMessage = "Invalid value for split option";
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(tmpBool)
|
|
|
|
|
{
|
|
|
|
|
ErrorMessage = "Splitted images not yet implemented";
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(options.TryGetValue("sparse", out tmpValue))
|
|
|
|
|
{
|
|
|
|
|
if(!bool.TryParse(tmpValue, out bool tmpBool))
|
|
|
|
|
{
|
|
|
|
|
ErrorMessage = "Invalid value for sparse option";
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(tmpBool)
|
|
|
|
|
{
|
|
|
|
|
ErrorMessage = "Sparse images not yet implemented";
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
adapter_type = "ide";
|
|
|
|
|
hwversion = 4;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(sectorSize != 512)
|
|
|
|
|
{
|
|
|
|
|
ErrorMessage = "Unsupported sector size";
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(!SupportedMediaTypes.Contains(mediaType))
|
|
|
|
|
{
|
|
|
|
|
ErrorMessage = $"Unsupport media format {mediaType}";
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
imageInfo = new ImageInfo {MediaType = mediaType, SectorSize = sectorSize, Sectors = sectors};
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
writingBaseName = Path.Combine(Path.GetDirectoryName(path), Path.GetFileNameWithoutExtension(path));
|
|
|
|
|
descriptorStream = new StreamWriter(path, false, Encoding.ASCII);
|
|
|
|
|
// TODO: Support split
|
2018-01-18 23:18:56 +00:00
|
|
|
writingStream = new FileStream(writingBaseName + "-flat.vmdk", FileMode.OpenOrCreate,
|
|
|
|
|
FileAccess.ReadWrite, FileShare.None);
|
2017-12-30 23:58:39 +00:00
|
|
|
}
|
|
|
|
|
catch(IOException e)
|
|
|
|
|
{
|
|
|
|
|
ErrorMessage = $"Could not create new image file, exception {e.Message}";
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
IsWriting = true;
|
|
|
|
|
ErrorMessage = null;
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool WriteMediaTag(byte[] data, MediaTagType tag)
|
|
|
|
|
{
|
|
|
|
|
ErrorMessage = "Writing media tags is not supported.";
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool WriteSector(byte[] data, ulong sectorAddress)
|
|
|
|
|
{
|
|
|
|
|
if(!IsWriting)
|
|
|
|
|
{
|
|
|
|
|
ErrorMessage = "Tried to write on a non-writable image";
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(data.Length != 512)
|
|
|
|
|
{
|
|
|
|
|
ErrorMessage = "Incorrect data size";
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(sectorAddress >= imageInfo.Sectors)
|
|
|
|
|
{
|
|
|
|
|
ErrorMessage = "Tried to write past image size";
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
writingStream.Seek((long)(sectorAddress * 512), SeekOrigin.Begin);
|
|
|
|
|
writingStream.Write(data, 0, data.Length);
|
|
|
|
|
|
|
|
|
|
ErrorMessage = "";
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// TODO: Implement sparse and split
|
|
|
|
|
public bool WriteSectors(byte[] data, ulong sectorAddress, uint length)
|
|
|
|
|
{
|
|
|
|
|
if(!IsWriting)
|
|
|
|
|
{
|
|
|
|
|
ErrorMessage = "Tried to write on a non-writable image";
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(data.Length % 512 != 0)
|
|
|
|
|
{
|
|
|
|
|
ErrorMessage = "Incorrect data size";
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(sectorAddress + length > imageInfo.Sectors)
|
|
|
|
|
{
|
|
|
|
|
ErrorMessage = "Tried to write past image size";
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
writingStream.Seek((long)(sectorAddress * 512), SeekOrigin.Begin);
|
|
|
|
|
writingStream.Write(data, 0, data.Length);
|
|
|
|
|
|
|
|
|
|
ErrorMessage = "";
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool WriteSectorLong(byte[] data, ulong sectorAddress)
|
|
|
|
|
{
|
|
|
|
|
ErrorMessage = "Writing sectors with tags is not supported.";
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool WriteSectorsLong(byte[] data, ulong sectorAddress, uint length)
|
|
|
|
|
{
|
|
|
|
|
ErrorMessage = "Writing sectors with tags is not supported.";
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool SetTracks(List<Track> tracks)
|
|
|
|
|
{
|
|
|
|
|
ErrorMessage = "Unsupported feature";
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// TODO: Implement sparse and split
|
|
|
|
|
public bool Close()
|
|
|
|
|
{
|
|
|
|
|
if(!IsWriting)
|
|
|
|
|
{
|
|
|
|
|
ErrorMessage = "Image is not opened for writing";
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
writingStream.Flush();
|
|
|
|
|
writingStream.Close();
|
|
|
|
|
|
2018-01-01 20:56:33 +00:00
|
|
|
if(imageInfo.Cylinders == 0)
|
2017-12-30 23:58:39 +00:00
|
|
|
{
|
2018-01-01 20:56:33 +00:00
|
|
|
imageInfo.Cylinders = (uint)(imageInfo.Sectors / 16 / 63);
|
|
|
|
|
imageInfo.Heads = 16;
|
|
|
|
|
imageInfo.SectorsPerTrack = 63;
|
2017-12-30 23:58:39 +00:00
|
|
|
|
2018-01-01 20:56:33 +00:00
|
|
|
while(imageInfo.Cylinders == 0)
|
2017-12-30 23:58:39 +00:00
|
|
|
{
|
2018-01-01 20:56:33 +00:00
|
|
|
imageInfo.Heads--;
|
2017-12-30 23:58:39 +00:00
|
|
|
|
2018-01-01 20:56:33 +00:00
|
|
|
if(imageInfo.Heads == 0)
|
|
|
|
|
{
|
|
|
|
|
imageInfo.SectorsPerTrack--;
|
|
|
|
|
imageInfo.Heads = 16;
|
|
|
|
|
}
|
2017-12-30 23:58:39 +00:00
|
|
|
|
2018-01-01 20:56:33 +00:00
|
|
|
imageInfo.Cylinders = (uint)(imageInfo.Sectors / imageInfo.Heads / imageInfo.SectorsPerTrack);
|
|
|
|
|
|
|
|
|
|
if(imageInfo.Cylinders == 0 && imageInfo.Heads == 0 && imageInfo.SectorsPerTrack == 0) break;
|
|
|
|
|
}
|
2017-12-30 23:58:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
descriptorStream.WriteLine("# Disk DescriptorFile");
|
|
|
|
|
descriptorStream.WriteLine("version=1");
|
|
|
|
|
descriptorStream.WriteLine($"CID={new Random().Next(int.MinValue, int.MaxValue):x8}");
|
|
|
|
|
descriptorStream.WriteLine("parentCID=ffffffff");
|
|
|
|
|
descriptorStream.WriteLine("createType=\"monolithicFlat\"");
|
|
|
|
|
descriptorStream.WriteLine();
|
|
|
|
|
descriptorStream.WriteLine("# Extent description");
|
|
|
|
|
descriptorStream.WriteLine($"RW {imageInfo.Sectors} FLAT \"{writingBaseName + "-flat.vmdk"}\" 0");
|
|
|
|
|
descriptorStream.WriteLine();
|
|
|
|
|
descriptorStream.WriteLine("# The Disk Data Base");
|
|
|
|
|
descriptorStream.WriteLine("#DDB");
|
|
|
|
|
descriptorStream.WriteLine();
|
|
|
|
|
descriptorStream.WriteLine($"ddb.virtualHWVersion = \"{hwversion}\"");
|
|
|
|
|
descriptorStream.WriteLine($"ddb.geometry.cylinders = \"{imageInfo.Cylinders}\"");
|
|
|
|
|
descriptorStream.WriteLine($"ddb.geometry.heads = \"{imageInfo.Heads}\"");
|
|
|
|
|
descriptorStream.WriteLine($"ddb.geometry.sectors = \"{imageInfo.SectorsPerTrack}\"");
|
|
|
|
|
descriptorStream.WriteLine($"ddb.adapterType = \"{adapter_type}\"");
|
|
|
|
|
|
|
|
|
|
descriptorStream.Flush();
|
|
|
|
|
descriptorStream.Close();
|
|
|
|
|
|
2018-01-18 23:18:56 +00:00
|
|
|
IsWriting = false;
|
|
|
|
|
ErrorMessage = "";
|
2017-12-30 23:58:39 +00:00
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool SetMetadata(ImageInfo metadata)
|
|
|
|
|
{
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2018-01-01 20:56:33 +00:00
|
|
|
public bool SetGeometry(uint cylinders, uint heads, uint sectorsPerTrack)
|
|
|
|
|
{
|
|
|
|
|
if(cylinders > ushort.MaxValue)
|
|
|
|
|
{
|
|
|
|
|
ErrorMessage = "Too many cylinders.";
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(heads > byte.MaxValue)
|
|
|
|
|
{
|
|
|
|
|
ErrorMessage = "Too many heads.";
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(sectorsPerTrack > byte.MaxValue)
|
|
|
|
|
{
|
|
|
|
|
ErrorMessage = "Too many sectors per track.";
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
imageInfo.SectorsPerTrack = sectorsPerTrack;
|
|
|
|
|
imageInfo.Heads = heads;
|
|
|
|
|
imageInfo.Cylinders = cylinders;
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2018-01-01 22:19:31 +00:00
|
|
|
public bool WriteSectorTag(byte[] data, ulong sectorAddress, SectorTagType tag)
|
|
|
|
|
{
|
|
|
|
|
ErrorMessage = "Unsupported feature";
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool WriteSectorsTag(byte[] data, ulong sectorAddress, uint length, SectorTagType tag)
|
|
|
|
|
{
|
|
|
|
|
ErrorMessage = "Unsupported feature";
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2018-01-28 20:29:46 +00:00
|
|
|
public bool SetDumpHardware(List<DumpHardwareType> dumpHardware)
|
|
|
|
|
{
|
|
|
|
|
// Not supported
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool SetCicmMetadata(CICMMetadataType metadata)
|
|
|
|
|
{
|
|
|
|
|
// Not supported
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2017-12-24 00:12:31 +00:00
|
|
|
[StructLayout(LayoutKind.Sequential, Pack = 1)]
|
|
|
|
|
struct VMwareExtentHeader
|
|
|
|
|
{
|
2017-12-30 23:58:39 +00:00
|
|
|
public uint magic;
|
|
|
|
|
public uint version;
|
|
|
|
|
public uint flags;
|
|
|
|
|
public ulong capacity;
|
|
|
|
|
public ulong grainSize;
|
|
|
|
|
public ulong descriptorOffset;
|
|
|
|
|
public ulong descriptorSize;
|
|
|
|
|
public uint GTEsPerGT;
|
|
|
|
|
public ulong rgdOffset;
|
|
|
|
|
public ulong gdOffset;
|
|
|
|
|
public ulong overhead;
|
|
|
|
|
[MarshalAs(UnmanagedType.U1)] public bool uncleanShutdown;
|
|
|
|
|
public byte singleEndLineChar;
|
|
|
|
|
public byte nonEndLineChar;
|
|
|
|
|
public byte doubleEndLineChar1;
|
|
|
|
|
public byte doubleEndLineChar2;
|
|
|
|
|
public ushort compression;
|
|
|
|
|
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 433)]
|
|
|
|
|
public byte[] padding;
|
2017-12-24 00:12:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[StructLayout(LayoutKind.Sequential, Pack = 1)]
|
|
|
|
|
struct VMwareCowHeader
|
|
|
|
|
{
|
|
|
|
|
public uint magic;
|
|
|
|
|
public uint version;
|
|
|
|
|
public uint flags;
|
|
|
|
|
public uint sectors;
|
|
|
|
|
public uint grainSize;
|
|
|
|
|
public uint gdOffset;
|
|
|
|
|
public uint numGDEntries;
|
|
|
|
|
public uint freeSector;
|
|
|
|
|
public uint cylinders;
|
|
|
|
|
public uint heads;
|
|
|
|
|
public uint spt;
|
|
|
|
|
// It stats on cylinders, above, but, don't care
|
2017-12-30 23:58:39 +00:00
|
|
|
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 1024 - 12)]
|
|
|
|
|
public byte[] parentFileName;
|
2018-06-22 08:08:38 +01:00
|
|
|
public uint parentGeneration;
|
|
|
|
|
public uint generation;
|
2017-12-30 23:58:39 +00:00
|
|
|
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 60)]
|
|
|
|
|
public byte[] name;
|
|
|
|
|
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 512)]
|
|
|
|
|
public byte[] description;
|
2018-06-22 08:08:38 +01:00
|
|
|
public uint savedGeneration;
|
2017-12-30 23:58:39 +00:00
|
|
|
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 8)]
|
2018-06-22 08:08:38 +01:00
|
|
|
public byte[] reserved;
|
|
|
|
|
[MarshalAs(UnmanagedType.U1)] public bool uncleanShutdown;
|
2017-12-30 23:58:39 +00:00
|
|
|
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 396)]
|
|
|
|
|
public byte[] padding;
|
2017-12-24 00:12:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
struct VMwareExtent
|
|
|
|
|
{
|
2017-12-30 23:58:39 +00:00
|
|
|
public string Access;
|
|
|
|
|
public uint Sectors;
|
|
|
|
|
public string Type;
|
2017-12-26 06:05:12 +00:00
|
|
|
public IFilter Filter;
|
2017-12-30 23:58:39 +00:00
|
|
|
public string Filename;
|
|
|
|
|
public uint Offset;
|
2017-12-24 00:12:31 +00:00
|
|
|
}
|
2016-08-30 05:25:27 +01:00
|
|
|
}
|
2017-12-19 20:33:03 +00:00
|
|
|
}
|