Add writing support to Parallels disk image.

This commit is contained in:
2018-01-04 10:14:07 +00:00
parent b213a2d734
commit 1dae7bdcd3

View File

@@ -41,7 +41,7 @@ using DiscImageChef.Filters;
namespace DiscImageChef.DiscImages namespace DiscImageChef.DiscImages
{ {
public class Parallels : IMediaImage public class Parallels : IWritableImage
{ {
const uint PARALLELS_VERSION = 2; const uint PARALLELS_VERSION = 2;
@@ -52,12 +52,14 @@ namespace DiscImageChef.DiscImages
const uint MAX_CACHE_SIZE = 16777216; const uint MAX_CACHE_SIZE = 16777216;
const uint MAX_CACHED_SECTORS = MAX_CACHE_SIZE / 512; const uint MAX_CACHED_SECTORS = MAX_CACHE_SIZE / 512;
const uint DEFAULT_CLUSTER_SIZE = 1048576;
readonly byte[] parallelsExtMagic = readonly byte[] parallelsExtMagic =
{0x57, 0x69, 0x74, 0x68, 0x6F, 0x75, 0x46, 0x72, 0x65, 0x53, 0x70, 0x61, 0x63, 0x45, 0x78, 0x74}; {0x57, 0x69, 0x74, 0x68, 0x6F, 0x75, 0x46, 0x72, 0x65, 0x53, 0x70, 0x61, 0x63, 0x45, 0x78, 0x74};
readonly byte[] parallelsMagic = readonly byte[] parallelsMagic =
{0x57, 0x69, 0x74, 0x68, 0x6F, 0x75, 0x74, 0x46, 0x72, 0x65, 0x65, 0x53, 0x70, 0x61, 0x63, 0x65}; {0x57, 0x69, 0x74, 0x68, 0x6F, 0x75, 0x74, 0x46, 0x72, 0x65, 0x65, 0x53, 0x70, 0x61, 0x63, 0x65};
uint[] bat; uint[] bat;
uint clusterBytes; uint clusterBytes;
long currentWritingPosition;
long dataOffset; long dataOffset;
bool empty; bool empty;
@@ -67,6 +69,7 @@ namespace DiscImageChef.DiscImages
ParallelsHeader pHdr; ParallelsHeader pHdr;
Dictionary<ulong, byte[]> sectorCache; Dictionary<ulong, byte[]> sectorCache;
FileStream writingStream;
public Parallels() public Parallels()
{ {
@@ -143,7 +146,8 @@ namespace DiscImageChef.DiscImages
pHdr = (ParallelsHeader)Marshal.PtrToStructure(headerPtr, typeof(ParallelsHeader)); pHdr = (ParallelsHeader)Marshal.PtrToStructure(headerPtr, typeof(ParallelsHeader));
Marshal.FreeHGlobal(headerPtr); Marshal.FreeHGlobal(headerPtr);
DicConsole.DebugWriteLine("Parallels plugin", "pHdr.magic = {0}", StringHandlers.CToString(pHdr.magic)); 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.version = {0}", pHdr.version);
DicConsole.DebugWriteLine("Parallels plugin", "pHdr.heads = {0}", pHdr.heads); DicConsole.DebugWriteLine("Parallels plugin", "pHdr.heads = {0}", pHdr.heads);
DicConsole.DebugWriteLine("Parallels plugin", "pHdr.cylinders = {0}", pHdr.cylinders); DicConsole.DebugWriteLine("Parallels plugin", "pHdr.cylinders = {0}", pHdr.cylinders);
@@ -166,7 +170,9 @@ namespace DiscImageChef.DiscImages
clusterBytes = pHdr.cluster_size * 512; clusterBytes = pHdr.cluster_size * 512;
if(pHdr.data_off > 0) dataOffset = pHdr.data_off * 512; if(pHdr.data_off > 0) dataOffset = pHdr.data_off * 512;
else dataOffset = (stream.Position / clusterBytes + stream.Position % clusterBytes) * clusterBytes; else
dataOffset =
(stream.Position / clusterBytes + stream.Position % clusterBytes) * clusterBytes;
sectorCache = new Dictionary<ulong, byte[]>(); sectorCache = new Dictionary<ulong, byte[]>();
@@ -340,6 +346,225 @@ namespace DiscImageChef.DiscImages
return null; return null;
} }
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;
}
if(sectorAddress == 28672) System.Console.WriteLine("Empty {0}", ArrayHelpers.ArrayIsNullOrEmpty(data));
// 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;
}
/// <summary> /// <summary>
/// Parallels disk image header, little-endian /// Parallels disk image header, little-endian
/// </summary> /// </summary>
@@ -349,7 +574,8 @@ namespace DiscImageChef.DiscImages
/// <summary> /// <summary>
/// Magic, <see cref="Parallels.parallelsMagic" /> or <see cref="Parallels.parallelsExtMagic" /> /// Magic, <see cref="Parallels.parallelsMagic" /> or <see cref="Parallels.parallelsExtMagic" />
/// </summary> /// </summary>
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 16)] public byte[] magic; [MarshalAs(UnmanagedType.ByValArray, SizeConst = 16)]
public byte[] magic;
/// <summary> /// <summary>
/// Version /// Version
/// </summary> /// </summary>