2017-05-19 20:28:49 +01:00
|
|
|
// /***************************************************************************
|
2016-08-27 03:24:20 +01:00
|
|
|
// The Disc Image Chef
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
//
|
|
|
|
|
// Filename : Parallels.cs
|
|
|
|
|
// Author(s) : Natalia Portillo <claunia@claunia.com>
|
|
|
|
|
//
|
2017-12-19 03:50:57 +00:00
|
|
|
// Component : Disk image plugins.
|
2016-08-27 03:24:20 +01:00
|
|
|
//
|
|
|
|
|
// --[ Description ] ----------------------------------------------------------
|
|
|
|
|
//
|
|
|
|
|
// Manages Parallels 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-27 03:24:20 +01:00
|
|
|
// ****************************************************************************/
|
|
|
|
|
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Linq;
|
2017-12-19 19:33:46 +00:00
|
|
|
using System.Runtime.InteropServices;
|
2016-08-27 03:24:20 +01:00
|
|
|
using DiscImageChef.CommonTypes;
|
|
|
|
|
using DiscImageChef.Console;
|
2016-09-05 17:37:31 +01:00
|
|
|
using DiscImageChef.Filters;
|
2016-08-27 03:24:20 +01:00
|
|
|
|
|
|
|
|
namespace DiscImageChef.DiscImages
|
|
|
|
|
{
|
2018-01-04 10:14:07 +00:00
|
|
|
public class Parallels : IWritableImage
|
2016-08-27 03:24:20 +01:00
|
|
|
{
|
2017-12-20 17:15:26 +00:00
|
|
|
const uint PARALLELS_VERSION = 2;
|
2016-08-27 03:24:20 +01:00
|
|
|
|
2018-01-04 10:14:07 +00:00
|
|
|
const uint PARALLELS_INUSE = 0x746F6E59;
|
2017-12-20 17:15:26 +00:00
|
|
|
const uint PARALLELS_CLOSED = 0x312E3276;
|
2016-08-27 03:24:20 +01:00
|
|
|
|
2017-12-20 17:15:26 +00:00
|
|
|
const uint PARALLELS_EMPTY = 0x00000001;
|
2016-08-27 03:24:20 +01:00
|
|
|
|
2018-01-04 10:14:07 +00:00
|
|
|
const uint MAX_CACHE_SIZE = 16777216;
|
|
|
|
|
const uint MAX_CACHED_SECTORS = MAX_CACHE_SIZE / 512;
|
|
|
|
|
const uint DEFAULT_CLUSTER_SIZE = 1048576;
|
|
|
|
|
readonly byte[] parallelsExtMagic =
|
2017-12-24 00:12:31 +00:00
|
|
|
{0x57, 0x69, 0x74, 0x68, 0x6F, 0x75, 0x46, 0x72, 0x65, 0x53, 0x70, 0x61, 0x63, 0x45, 0x78, 0x74};
|
|
|
|
|
readonly byte[] parallelsMagic =
|
|
|
|
|
{0x57, 0x69, 0x74, 0x68, 0x6F, 0x75, 0x74, 0x46, 0x72, 0x65, 0x65, 0x53, 0x70, 0x61, 0x63, 0x65};
|
2017-12-20 17:15:26 +00:00
|
|
|
uint[] bat;
|
2018-01-04 10:14:07 +00:00
|
|
|
uint clusterBytes;
|
|
|
|
|
long currentWritingPosition;
|
|
|
|
|
long dataOffset;
|
|
|
|
|
bool empty;
|
|
|
|
|
|
|
|
|
|
bool extended;
|
|
|
|
|
ImageInfo imageInfo;
|
|
|
|
|
Stream imageStream;
|
2017-12-24 00:12:31 +00:00
|
|
|
ParallelsHeader pHdr;
|
2016-08-27 03:24:20 +01:00
|
|
|
|
|
|
|
|
Dictionary<ulong, byte[]> sectorCache;
|
2018-01-04 10:14:07 +00:00
|
|
|
FileStream writingStream;
|
2016-08-27 03:24:20 +01:00
|
|
|
|
|
|
|
|
public Parallels()
|
|
|
|
|
{
|
2017-12-26 06:05:12 +00:00
|
|
|
imageInfo = new ImageInfo
|
2017-12-22 06:55:04 +00:00
|
|
|
{
|
2018-01-04 10:14:07 +00:00
|
|
|
ReadableSectorTags = new List<SectorTagType>(),
|
|
|
|
|
ReadableMediaTags = new List<MediaTagType>(),
|
|
|
|
|
HasPartitions = false,
|
|
|
|
|
HasSessions = false,
|
|
|
|
|
Version = "2",
|
|
|
|
|
Application = "Parallels",
|
|
|
|
|
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-27 03:24:20 +01:00
|
|
|
}
|
|
|
|
|
|
2018-01-04 10:14:07 +00:00
|
|
|
public string Name => "Parallels disk image";
|
|
|
|
|
public Guid Id => new Guid("E314DE35-C103-48A3-AD36-990F68523C46");
|
2017-12-26 07:28:40 +00:00
|
|
|
public ImageInfo Info => imageInfo;
|
2017-12-26 02:51:10 +00:00
|
|
|
|
2017-12-28 19:56:36 +00:00
|
|
|
public string Format => "Parallels";
|
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-27 03:24:20 +01:00
|
|
|
{
|
2016-09-05 17:37:31 +01:00
|
|
|
Stream stream = imageFilter.GetDataForkStream();
|
2016-08-27 03:24:20 +01:00
|
|
|
stream.Seek(0, SeekOrigin.Begin);
|
|
|
|
|
|
2017-12-19 20:33:03 +00:00
|
|
|
if(stream.Length < 512) return false;
|
2016-08-27 03:24:20 +01:00
|
|
|
|
2017-12-20 17:15:26 +00:00
|
|
|
byte[] pHdrB = new byte[Marshal.SizeOf(pHdr)];
|
|
|
|
|
stream.Read(pHdrB, 0, Marshal.SizeOf(pHdr));
|
2018-01-04 10:14:07 +00:00
|
|
|
pHdr = new ParallelsHeader();
|
2016-08-27 03:24:20 +01:00
|
|
|
IntPtr headerPtr = Marshal.AllocHGlobal(Marshal.SizeOf(pHdr));
|
2017-12-20 17:15:26 +00:00
|
|
|
Marshal.Copy(pHdrB, 0, headerPtr, Marshal.SizeOf(pHdr));
|
2016-08-27 03:24:20 +01:00
|
|
|
pHdr = (ParallelsHeader)Marshal.PtrToStructure(headerPtr, typeof(ParallelsHeader));
|
|
|
|
|
Marshal.FreeHGlobal(headerPtr);
|
|
|
|
|
|
2017-12-20 17:15:26 +00:00
|
|
|
return parallelsMagic.SequenceEqual(pHdr.magic) || parallelsExtMagic.SequenceEqual(pHdr.magic);
|
2016-08-27 03:24:20 +01:00
|
|
|
}
|
|
|
|
|
|
2017-12-28 19:56:36 +00:00
|
|
|
public bool Open(IFilter imageFilter)
|
2016-08-27 03:24:20 +01:00
|
|
|
{
|
2016-09-05 17:37:31 +01:00
|
|
|
Stream stream = imageFilter.GetDataForkStream();
|
2016-08-27 03:24:20 +01:00
|
|
|
stream.Seek(0, SeekOrigin.Begin);
|
|
|
|
|
|
2017-12-19 20:33:03 +00:00
|
|
|
if(stream.Length < 512) return false;
|
2016-08-27 03:24:20 +01:00
|
|
|
|
2017-12-20 17:15:26 +00:00
|
|
|
byte[] pHdrB = new byte[Marshal.SizeOf(pHdr)];
|
|
|
|
|
stream.Read(pHdrB, 0, Marshal.SizeOf(pHdr));
|
2018-01-04 10:14:07 +00:00
|
|
|
pHdr = new ParallelsHeader();
|
2016-08-27 03:24:20 +01:00
|
|
|
IntPtr headerPtr = Marshal.AllocHGlobal(Marshal.SizeOf(pHdr));
|
2017-12-20 17:15:26 +00:00
|
|
|
Marshal.Copy(pHdrB, 0, headerPtr, Marshal.SizeOf(pHdr));
|
2016-08-27 03:24:20 +01:00
|
|
|
pHdr = (ParallelsHeader)Marshal.PtrToStructure(headerPtr, typeof(ParallelsHeader));
|
|
|
|
|
Marshal.FreeHGlobal(headerPtr);
|
|
|
|
|
|
2018-01-04 10:14:07 +00:00
|
|
|
DicConsole.DebugWriteLine("Parallels plugin", "pHdr.magic = {0}",
|
|
|
|
|
StringHandlers.CToString(pHdr.magic));
|
|
|
|
|
DicConsole.DebugWriteLine("Parallels plugin", "pHdr.version = {0}", pHdr.version);
|
|
|
|
|
DicConsole.DebugWriteLine("Parallels plugin", "pHdr.heads = {0}", pHdr.heads);
|
|
|
|
|
DicConsole.DebugWriteLine("Parallels plugin", "pHdr.cylinders = {0}", pHdr.cylinders);
|
2016-08-27 04:44:36 +01:00
|
|
|
DicConsole.DebugWriteLine("Parallels plugin", "pHdr.cluster_size = {0}", pHdr.cluster_size);
|
2018-01-04 10:14:07 +00:00
|
|
|
DicConsole.DebugWriteLine("Parallels plugin", "pHdr.bat_entries = {0}", pHdr.bat_entries);
|
|
|
|
|
DicConsole.DebugWriteLine("Parallels plugin", "pHdr.sectors = {0}", pHdr.sectors);
|
|
|
|
|
DicConsole.DebugWriteLine("Parallels plugin", "pHdr.in_use = 0x{0:X8}", pHdr.in_use);
|
|
|
|
|
DicConsole.DebugWriteLine("Parallels plugin", "pHdr.data_off = {0}", pHdr.data_off);
|
|
|
|
|
DicConsole.DebugWriteLine("Parallels plugin", "pHdr.flags = {0}", pHdr.flags);
|
|
|
|
|
DicConsole.DebugWriteLine("Parallels plugin", "pHdr.ext_off = {0}", pHdr.ext_off);
|
2016-08-27 03:24:20 +01:00
|
|
|
|
2017-12-20 17:15:26 +00:00
|
|
|
extended = parallelsExtMagic.SequenceEqual(pHdr.magic);
|
2016-08-27 04:44:36 +01:00
|
|
|
DicConsole.DebugWriteLine("Parallels plugin", "pHdr.extended = {0}", extended);
|
2016-08-27 03:24:20 +01:00
|
|
|
|
2016-08-27 04:44:36 +01:00
|
|
|
DicConsole.DebugWriteLine("Parallels plugin", "Reading BAT");
|
2018-01-04 10:14:07 +00:00
|
|
|
bat = new uint[pHdr.bat_entries];
|
2017-12-20 17:15:26 +00:00
|
|
|
byte[] batB = new byte[pHdr.bat_entries * 4];
|
|
|
|
|
stream.Read(batB, 0, batB.Length);
|
|
|
|
|
for(int i = 0; i < bat.Length; i++) bat[i] = BitConverter.ToUInt32(batB, i * 4);
|
2016-08-27 03:24:20 +01:00
|
|
|
|
2018-01-04 10:14:07 +00:00
|
|
|
clusterBytes = pHdr.cluster_size * 512;
|
|
|
|
|
if(pHdr.data_off > 0) dataOffset = pHdr.data_off * 512;
|
|
|
|
|
else
|
|
|
|
|
dataOffset =
|
|
|
|
|
(stream.Position / clusterBytes + stream.Position % clusterBytes) * clusterBytes;
|
2016-08-27 03:24:20 +01:00
|
|
|
|
|
|
|
|
sectorCache = new Dictionary<ulong, byte[]>();
|
|
|
|
|
|
2017-12-20 17:15:26 +00:00
|
|
|
empty = (pHdr.flags & PARALLELS_EMPTY) == PARALLELS_EMPTY;
|
|
|
|
|
|
2018-01-04 10:14:07 +00:00
|
|
|
imageInfo.CreationTime = imageFilter.GetCreationTime();
|
2017-12-26 06:05:12 +00:00
|
|
|
imageInfo.LastModificationTime = imageFilter.GetLastWriteTime();
|
2018-01-04 10:14:07 +00:00
|
|
|
imageInfo.MediaTitle = Path.GetFileNameWithoutExtension(imageFilter.GetFilename());
|
|
|
|
|
imageInfo.Sectors = pHdr.sectors;
|
|
|
|
|
imageInfo.SectorSize = 512;
|
|
|
|
|
imageInfo.XmlMediaType = XmlMediaType.BlockMedia;
|
|
|
|
|
imageInfo.MediaType = MediaType.GENERIC_HDD;
|
|
|
|
|
imageInfo.ImageSize = pHdr.sectors * 512;
|
|
|
|
|
imageInfo.Cylinders = pHdr.cylinders;
|
|
|
|
|
imageInfo.Heads = pHdr.heads;
|
|
|
|
|
imageInfo.SectorsPerTrack = (uint)(imageInfo.Sectors / imageInfo.Cylinders / imageInfo.Heads);
|
|
|
|
|
imageStream = stream;
|
2016-08-27 03:24:20 +01:00
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2017-12-26 07:28:40 +00:00
|
|
|
public byte[] ReadSector(ulong sectorAddress)
|
2016-08-27 03:24:20 +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-27 03:24:20 +01:00
|
|
|
|
2017-12-19 20:33:03 +00:00
|
|
|
if(empty) return new byte[512];
|
2016-08-27 03:24:20 +01:00
|
|
|
|
2017-12-22 06:55:04 +00:00
|
|
|
if(sectorCache.TryGetValue(sectorAddress, out byte[] sector)) return sector;
|
2016-08-27 03:24:20 +01:00
|
|
|
|
2018-01-04 10:14:07 +00:00
|
|
|
ulong index = sectorAddress / pHdr.cluster_size;
|
2016-08-27 03:24:20 +01:00
|
|
|
ulong secOff = sectorAddress % pHdr.cluster_size;
|
|
|
|
|
|
2018-01-04 10:14:07 +00:00
|
|
|
uint batOff = bat[index];
|
2016-08-27 03:24:20 +01:00
|
|
|
ulong imageOff;
|
|
|
|
|
|
2017-12-19 20:33:03 +00:00
|
|
|
if(batOff == 0) return new byte[512];
|
2016-08-27 03:24:20 +01:00
|
|
|
|
2017-12-19 20:33:03 +00:00
|
|
|
if(extended) imageOff = batOff * clusterBytes;
|
2018-01-04 10:14:07 +00:00
|
|
|
else imageOff = batOff * 512;
|
2016-08-27 03:24:20 +01:00
|
|
|
|
|
|
|
|
byte[] cluster = new byte[clusterBytes];
|
|
|
|
|
imageStream.Seek((long)imageOff, SeekOrigin.Begin);
|
|
|
|
|
imageStream.Read(cluster, 0, (int)clusterBytes);
|
|
|
|
|
sector = new byte[512];
|
|
|
|
|
Array.Copy(cluster, (int)(secOff * 512), sector, 0, 512);
|
|
|
|
|
|
2017-12-22 06:55:04 +00:00
|
|
|
if(sectorCache.Count > MAX_CACHED_SECTORS) sectorCache.Clear();
|
2016-08-27 03:24:20 +01:00
|
|
|
|
|
|
|
|
sectorCache.Add(sectorAddress, sector);
|
|
|
|
|
|
2017-12-19 20:33:03 +00:00
|
|
|
return sector;
|
2016-08-27 03:24:20 +01:00
|
|
|
}
|
|
|
|
|
|
2017-12-26 07:28:40 +00:00
|
|
|
public byte[] ReadSectors(ulong sectorAddress, uint length)
|
2016-08-27 03:24:20 +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-27 03:24:20 +01:00
|
|
|
|
2017-12-26 06:05:12 +00:00
|
|
|
if(sectorAddress + length > imageInfo.Sectors)
|
2016-08-27 03:24:20 +01:00
|
|
|
throw new ArgumentOutOfRangeException(nameof(length), "Requested more sectors than available");
|
|
|
|
|
|
2017-12-19 20:33:03 +00:00
|
|
|
if(empty) return new byte[512 * length];
|
2016-08-27 03:24:20 +01:00
|
|
|
|
|
|
|
|
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-27 03:24:20 +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-27 03:24:20 +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-27 03:24:20 +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-27 03:24:20 +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-27 03:24:20 +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-27 03:24:20 +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-27 03:24:20 +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-27 03:24:20 +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-27 03:24:20 +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-27 03:24:20 +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-27 03:24:20 +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-27 03:24:20 +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-27 03:24:20 +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-27 03:24:20 +01:00
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
2017-12-26 07:28:40 +00:00
|
|
|
public bool? VerifySector(ulong sectorAddress, uint track)
|
2016-08-27 03:24:20 +01:00
|
|
|
{
|
|
|
|
|
throw new FeatureUnsupportedImageException("Feature not supported by image format");
|
|
|
|
|
}
|
|
|
|
|
|
2017-12-26 07:28:40 +00:00
|
|
|
public bool? VerifySectors(ulong sectorAddress, uint length, out List<ulong> failingLbas,
|
2018-01-04 10:14:07 +00:00
|
|
|
out List<ulong> unknownLbas)
|
2016-08-27 03:24:20 +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-27 03:24:20 +01:00
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
2017-12-26 07:28:40 +00:00
|
|
|
public bool? VerifySectors(ulong sectorAddress, uint length, uint track, out List<ulong> failingLbas,
|
2018-01-04 10:14:07 +00:00
|
|
|
out List<ulong> unknownLbas)
|
2016-08-27 03:24:20 +01:00
|
|
|
{
|
|
|
|
|
throw new FeatureUnsupportedImageException("Feature not supported by image format");
|
|
|
|
|
}
|
|
|
|
|
|
2017-12-26 07:28:40 +00:00
|
|
|
public bool? VerifyMediaImage()
|
2016-08-27 03:24:20 +01:00
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
2017-12-24 00:12:31 +00:00
|
|
|
|
2018-01-04 10:14:07 +00:00
|
|
|
public IEnumerable<MediaTagType> SupportedMediaTags => new MediaTagType[] { };
|
|
|
|
|
public IEnumerable<SectorTagType> SupportedSectorTags => new SectorTagType[] { };
|
|
|
|
|
public IEnumerable<MediaType> SupportedMediaTypes => new[] {MediaType.Unknown, MediaType.GENERIC_HDD};
|
|
|
|
|
// TODO: Add cluster size option
|
|
|
|
|
public IEnumerable<(string name, Type type, string description)> SupportedOptions =>
|
|
|
|
|
new (string name, Type type, string description)[] { };
|
|
|
|
|
public IEnumerable<string> KnownExtensions => new[] {".hdd"};
|
|
|
|
|
public bool IsWriting { get; private set; }
|
|
|
|
|
public string ErrorMessage { get; private set; }
|
|
|
|
|
|
|
|
|
|
// TODO: Support extended
|
|
|
|
|
public bool Create(string path, MediaType mediaType, Dictionary<string, string> options, ulong sectors,
|
|
|
|
|
uint sectorSize)
|
|
|
|
|
{
|
|
|
|
|
if(sectorSize != 512)
|
|
|
|
|
{
|
|
|
|
|
ErrorMessage = "Unsupported sector size";
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(!SupportedMediaTypes.Contains(mediaType))
|
|
|
|
|
{
|
|
|
|
|
ErrorMessage = $"Unsupport media format {mediaType}";
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(sectors * sectorSize / DEFAULT_CLUSTER_SIZE > uint.MaxValue)
|
|
|
|
|
{
|
|
|
|
|
ErrorMessage = "Too many sectors for selected cluster size";
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
imageInfo = new ImageInfo {MediaType = mediaType, SectorSize = sectorSize, Sectors = sectors};
|
|
|
|
|
|
|
|
|
|
try { writingStream = new FileStream(path, FileMode.CreateNew, FileAccess.ReadWrite, FileShare.None); }
|
|
|
|
|
catch(IOException e)
|
|
|
|
|
{
|
|
|
|
|
ErrorMessage = $"Could not create new image file, exception {e.Message}";
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
uint batEntries = (uint)(sectors * sectorSize /
|
|
|
|
|
DEFAULT_CLUSTER_SIZE);
|
|
|
|
|
if(sectors * sectorSize %
|
|
|
|
|
DEFAULT_CLUSTER_SIZE > 0) batEntries++;
|
|
|
|
|
uint headerSectors = (uint)Marshal.SizeOf(typeof(ParallelsHeader)) + batEntries * 4;
|
|
|
|
|
if((uint)Marshal.SizeOf(typeof(ParallelsHeader)) + batEntries % 4 > 0) headerSectors++;
|
|
|
|
|
|
|
|
|
|
pHdr = new ParallelsHeader
|
|
|
|
|
{
|
|
|
|
|
magic = parallelsMagic,
|
|
|
|
|
version = PARALLELS_VERSION,
|
|
|
|
|
sectors = sectors,
|
|
|
|
|
in_use = PARALLELS_CLOSED,
|
|
|
|
|
bat_entries = batEntries,
|
|
|
|
|
data_off = headerSectors,
|
|
|
|
|
cluster_size = DEFAULT_CLUSTER_SIZE / 512
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
bat = new uint[batEntries];
|
|
|
|
|
currentWritingPosition = headerSectors * 512;
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Ignore empty sectors
|
|
|
|
|
if(ArrayHelpers.ArrayIsNullOrEmpty(data)) return true;
|
|
|
|
|
|
|
|
|
|
ulong index = sectorAddress / pHdr.cluster_size;
|
|
|
|
|
ulong secOff = sectorAddress % pHdr.cluster_size;
|
|
|
|
|
|
|
|
|
|
uint batOff = bat[index];
|
|
|
|
|
|
|
|
|
|
if(batOff == 0)
|
|
|
|
|
{
|
|
|
|
|
batOff = (uint)(currentWritingPosition / 512);
|
|
|
|
|
bat[index] = batOff;
|
|
|
|
|
currentWritingPosition += pHdr.cluster_size * 512;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ulong imageOff = batOff * 512;
|
|
|
|
|
|
|
|
|
|
writingStream.Seek((long)imageOff, SeekOrigin.Begin);
|
|
|
|
|
writingStream.Seek((long)secOff * 512, SeekOrigin.Current);
|
|
|
|
|
writingStream.Write(data, 0, data.Length);
|
|
|
|
|
|
|
|
|
|
ErrorMessage = "";
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// TODO: This can be optimized
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Ignore empty sectors
|
|
|
|
|
if(ArrayHelpers.ArrayIsNullOrEmpty(data)) return true;
|
|
|
|
|
|
|
|
|
|
for(uint i = 0; i < length; i++)
|
|
|
|
|
{
|
|
|
|
|
byte[] tmp = new byte[512];
|
|
|
|
|
Array.Copy(data, i * 512, tmp, 0, 512);
|
|
|
|
|
if(!WriteSector(tmp, sectorAddress + i)) return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool Close()
|
|
|
|
|
{
|
|
|
|
|
if(!IsWriting)
|
|
|
|
|
{
|
|
|
|
|
ErrorMessage = "Image is not opened for writing";
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
byte[] hdr = new byte[Marshal.SizeOf(typeof(ParallelsHeader))];
|
|
|
|
|
IntPtr hdrPtr = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(ParallelsHeader)));
|
|
|
|
|
Marshal.StructureToPtr(pHdr, hdrPtr, true);
|
|
|
|
|
Marshal.Copy(hdrPtr, hdr, 0, hdr.Length);
|
|
|
|
|
Marshal.FreeHGlobal(hdrPtr);
|
|
|
|
|
|
|
|
|
|
writingStream.Seek(0, SeekOrigin.Begin);
|
|
|
|
|
writingStream.Write(hdr, 0, hdr.Length);
|
|
|
|
|
|
|
|
|
|
for(long i = 0; i < bat.LongLength; i++) writingStream.Write(BitConverter.GetBytes(bat[i]), 0, 4);
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool SetMetadata(ImageInfo metadata)
|
|
|
|
|
{
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool SetGeometry(uint cylinders, uint heads, uint sectorsPerTrack)
|
|
|
|
|
{
|
|
|
|
|
pHdr.cylinders = cylinders;
|
|
|
|
|
pHdr.heads = heads;
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool WriteSectorTag(byte[] data, ulong sectorAddress, SectorTagType tag)
|
|
|
|
|
{
|
|
|
|
|
ErrorMessage = "Writing sectors with tags is not supported.";
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool WriteSectorsTag(byte[] data, ulong sectorAddress, uint length, SectorTagType tag)
|
|
|
|
|
{
|
|
|
|
|
ErrorMessage = "Writing sectors with tags is not supported.";
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2017-12-24 00:12:31 +00:00
|
|
|
/// <summary>
|
|
|
|
|
/// Parallels disk image header, little-endian
|
|
|
|
|
/// </summary>
|
|
|
|
|
[StructLayout(LayoutKind.Sequential, Pack = 1)]
|
|
|
|
|
struct ParallelsHeader
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Magic, <see cref="Parallels.parallelsMagic" /> or <see cref="Parallels.parallelsExtMagic" />
|
|
|
|
|
/// </summary>
|
2018-01-04 10:14:07 +00:00
|
|
|
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 16)]
|
|
|
|
|
public byte[] magic;
|
2017-12-24 00:12:31 +00:00
|
|
|
/// <summary>
|
|
|
|
|
/// Version
|
|
|
|
|
/// </summary>
|
|
|
|
|
public uint version;
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Disk geometry parameter
|
|
|
|
|
/// </summary>
|
|
|
|
|
public uint heads;
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Disk geometry parameter
|
|
|
|
|
/// </summary>
|
|
|
|
|
public uint cylinders;
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Cluser size in sectors
|
|
|
|
|
/// </summary>
|
|
|
|
|
public uint cluster_size;
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Entries in BAT (clusters in image)
|
|
|
|
|
/// </summary>
|
|
|
|
|
public uint bat_entries;
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Disk size in sectors
|
|
|
|
|
/// </summary>
|
|
|
|
|
public ulong sectors;
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Set to <see cref="Parallels.PARALLELS_INUSE" /> if image is opened by any software,
|
|
|
|
|
/// <see cref="Parallels.PARALLELS_CLOSED" /> if not, and 0 if old version
|
|
|
|
|
/// </summary>
|
|
|
|
|
public uint in_use;
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Offset in sectors to start of data
|
|
|
|
|
/// </summary>
|
|
|
|
|
public uint data_off;
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Flags
|
|
|
|
|
/// </summary>
|
|
|
|
|
public uint flags;
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Offset in sectors to format extension
|
|
|
|
|
/// </summary>
|
|
|
|
|
public ulong ext_off;
|
|
|
|
|
}
|
2016-08-27 03:24:20 +01:00
|
|
|
}
|
2017-12-19 20:33:03 +00:00
|
|
|
}
|