Add writing support to QEMU Enhanced Disk image.

This commit is contained in:
2018-01-17 14:59:05 +00:00
parent 4bb8b41363
commit eb92cd0602
2 changed files with 374 additions and 111 deletions

View File

@@ -33,6 +33,7 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using DiscImageChef.CommonTypes;
using DiscImageChef.Console;
@@ -40,7 +41,7 @@ using DiscImageChef.Filters;
namespace DiscImageChef.DiscImages
{
public class Qed : IMediaImage
public class Qed : IWritableImage
{
/// <summary>
/// Magic number: 'Q', 'E', 'D', 0x00
@@ -61,58 +62,60 @@ namespace DiscImageChef.DiscImages
/// </summary>
const ulong QED_FEATURE_NEEDS_CHECK = 0x02;
/// <summary>
/// s
/// Backing file is a raw disk image
/// </summary>
const ulong QED_FEATURE_RAW_BACKING = 0x04;
const int MAX_CACHE_SIZE = 16777216;
const uint MAX_CACHED_SECTORS = MAX_CACHE_SIZE / 512;
int clusterBits;
const uint MAX_CACHED_SECTORS = MAX_CACHE_SIZE / 512;
const uint DEFAULT_CLUSTER_SIZE = 65536;
const uint DEFAULT_TABLE_SIZE = 4;
int clusterBits;
Dictionary<ulong, byte[]> clusterCache;
uint clusterSectors;
ImageInfo imageInfo;
uint clusterSectors;
ImageInfo imageInfo;
Stream imageStream;
ulong l1Mask;
int l1Shift;
ulong[] l1Table;
ulong l2Mask;
ulong l1Mask;
int l1Shift;
ulong[] l1Table;
ulong l2Mask;
Dictionary<ulong, ulong[]> l2TableCache;
uint maxClusterCache;
uint maxL2TableCache;
uint maxClusterCache;
uint maxL2TableCache;
QedHeader qHdr;
Dictionary<ulong, byte[]> sectorCache;
ulong sectorMask;
uint tableSize;
ulong sectorMask;
uint tableSize;
FileStream writingStream;
public Qed()
{
imageInfo = new ImageInfo
{
ReadableSectorTags = new List<SectorTagType>(),
ReadableMediaTags = new List<MediaTagType>(),
HasPartitions = false,
HasSessions = false,
Version = "1",
Application = "QEMU",
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,
ReadableSectorTags = new List<SectorTagType>(),
ReadableMediaTags = new List<MediaTagType>(),
HasPartitions = false,
HasSessions = false,
Version = "1",
Application = "QEMU",
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,
DriveFirmwareRevision = null
};
}
@@ -120,7 +123,7 @@ namespace DiscImageChef.DiscImages
public ImageInfo Info => imageInfo;
public string Name => "QEMU Enhanced Disk image";
public Guid Id => new Guid("B9DBB155-A69A-4C10-BF91-96BF431B9BB6");
public Guid Id => new Guid("B9DBB155-A69A-4C10-BF91-96BF431B9BB6");
public string Format => "QEMU Enhanced Disk";
@@ -142,7 +145,7 @@ namespace DiscImageChef.DiscImages
byte[] qHdrB = new byte[64];
stream.Read(qHdrB, 0, 64);
qHdr = new QedHeader();
qHdr = new QedHeader();
IntPtr headerPtr = Marshal.AllocHGlobal(64);
Marshal.Copy(qHdrB, 0, headerPtr, 64);
qHdr = (QedHeader)Marshal.PtrToStructure(headerPtr, typeof(QedHeader));
@@ -160,23 +163,23 @@ namespace DiscImageChef.DiscImages
byte[] qHdrB = new byte[64];
stream.Read(qHdrB, 0, 64);
qHdr = new QedHeader();
qHdr = new QedHeader();
IntPtr headerPtr = Marshal.AllocHGlobal(64);
Marshal.Copy(qHdrB, 0, headerPtr, 64);
qHdr = (QedHeader)Marshal.PtrToStructure(headerPtr, typeof(QedHeader));
Marshal.FreeHGlobal(headerPtr);
DicConsole.DebugWriteLine("QED plugin", "qHdr.magic = 0x{0:X8}", qHdr.magic);
DicConsole.DebugWriteLine("QED plugin", "qHdr.cluster_size = {0}", qHdr.cluster_size);
DicConsole.DebugWriteLine("QED plugin", "qHdr.table_size = {0}", qHdr.table_size);
DicConsole.DebugWriteLine("QED plugin", "qHdr.header_size = {0}", qHdr.header_size);
DicConsole.DebugWriteLine("QED plugin", "qHdr.features = {0}", qHdr.features);
DicConsole.DebugWriteLine("QED plugin", "qHdr.compat_features = {0}", qHdr.compat_features);
DicConsole.DebugWriteLine("QED plugin", "qHdr.autoclear_features = {0}", qHdr.autoclear_features);
DicConsole.DebugWriteLine("QED plugin", "qHdr.l1_table_offset = {0}", qHdr.l1_table_offset);
DicConsole.DebugWriteLine("QED plugin", "qHdr.image_size = {0}", qHdr.image_size);
DicConsole.DebugWriteLine("QED plugin", "qHdr.magic = 0x{0:X8}", qHdr.magic);
DicConsole.DebugWriteLine("QED plugin", "qHdr.cluster_size = {0}", qHdr.cluster_size);
DicConsole.DebugWriteLine("QED plugin", "qHdr.table_size = {0}", qHdr.table_size);
DicConsole.DebugWriteLine("QED plugin", "qHdr.header_size = {0}", qHdr.header_size);
DicConsole.DebugWriteLine("QED plugin", "qHdr.features = {0}", qHdr.features);
DicConsole.DebugWriteLine("QED plugin", "qHdr.compat_features = {0}", qHdr.compat_features);
DicConsole.DebugWriteLine("QED plugin", "qHdr.autoclear_features = {0}", qHdr.autoclear_features);
DicConsole.DebugWriteLine("QED plugin", "qHdr.l1_table_offset = {0}", qHdr.l1_table_offset);
DicConsole.DebugWriteLine("QED plugin", "qHdr.image_size = {0}", qHdr.image_size);
DicConsole.DebugWriteLine("QED plugin", "qHdr.backing_file_offset = {0}", qHdr.backing_file_offset);
DicConsole.DebugWriteLine("QED plugin", "qHdr.backing_file_size = {0}", qHdr.backing_file_size);
DicConsole.DebugWriteLine("QED plugin", "qHdr.backing_file_size = {0}", qHdr.backing_file_size);
if(qHdr.image_size <= 1)
throw new ArgumentOutOfRangeException(nameof(qHdr.image_size), "Image size is too small");
@@ -203,10 +206,10 @@ namespace DiscImageChef.DiscImages
throw new NotImplementedException("Differencing images not yet supported");
clusterSectors = qHdr.cluster_size / 512;
tableSize = qHdr.cluster_size * qHdr.table_size / 8;
tableSize = qHdr.cluster_size * qHdr.table_size / 8;
DicConsole.DebugWriteLine("QED plugin", "qHdr.clusterSectors = {0}", clusterSectors);
DicConsole.DebugWriteLine("QED plugin", "qHdr.tableSize = {0}", tableSize);
DicConsole.DebugWriteLine("QED plugin", "qHdr.tableSize = {0}", tableSize);
byte[] l1TableB = new byte[tableSize * 8];
stream.Seek((long)qHdr.l1_table_offset, SeekOrigin.Begin);
@@ -215,11 +218,11 @@ namespace DiscImageChef.DiscImages
DicConsole.DebugWriteLine("QED plugin", "Reading L1 table");
for(long i = 0; i < l1Table.LongLength; i++) l1Table[i] = BitConverter.ToUInt64(l1TableB, (int)(i * 8));
l1Mask = 0;
int c = 0;
l1Mask = 0;
int c = 0;
clusterBits = Ctz32(qHdr.cluster_size);
l2Mask = (tableSize - 1) << clusterBits;
l1Shift = clusterBits + Ctz32(tableSize);
l2Mask = (tableSize - 1) << clusterBits;
l1Shift = clusterBits + Ctz32(tableSize);
for(int i = 0; i < 64; i++)
{
@@ -231,13 +234,13 @@ namespace DiscImageChef.DiscImages
c++;
}
sectorMask = 0;
sectorMask = 0;
for(int i = 0; i < clusterBits; i++) sectorMask = (sectorMask << 1) + 1;
DicConsole.DebugWriteLine("QED plugin", "qHdr.clusterBits = {0}", clusterBits);
DicConsole.DebugWriteLine("QED plugin", "qHdr.l1Mask = {0:X}", l1Mask);
DicConsole.DebugWriteLine("QED plugin", "qHdr.l1Shift = {0}", l1Shift);
DicConsole.DebugWriteLine("QED plugin", "qHdr.l2Mask = {0:X}", l2Mask);
DicConsole.DebugWriteLine("QED plugin", "qHdr.clusterBits = {0}", clusterBits);
DicConsole.DebugWriteLine("QED plugin", "qHdr.l1Mask = {0:X}", l1Mask);
DicConsole.DebugWriteLine("QED plugin", "qHdr.l1Shift = {0}", l1Shift);
DicConsole.DebugWriteLine("QED plugin", "qHdr.l2Mask = {0:X}", l2Mask);
DicConsole.DebugWriteLine("QED plugin", "qHdr.sectorMask = {0:X}", sectorMask);
maxL2TableCache = MAX_CACHE_SIZE / tableSize;
@@ -245,21 +248,21 @@ namespace DiscImageChef.DiscImages
imageStream = stream;
sectorCache = new Dictionary<ulong, byte[]>();
sectorCache = new Dictionary<ulong, byte[]>();
l2TableCache = new Dictionary<ulong, ulong[]>();
clusterCache = new Dictionary<ulong, byte[]>();
imageInfo.CreationTime = imageFilter.GetCreationTime();
imageInfo.CreationTime = imageFilter.GetCreationTime();
imageInfo.LastModificationTime = imageFilter.GetLastWriteTime();
imageInfo.MediaTitle = Path.GetFileNameWithoutExtension(imageFilter.GetFilename());
imageInfo.Sectors = qHdr.image_size / 512;
imageInfo.SectorSize = 512;
imageInfo.XmlMediaType = XmlMediaType.BlockMedia;
imageInfo.MediaType = MediaType.GENERIC_HDD;
imageInfo.ImageSize = qHdr.image_size;
imageInfo.MediaTitle = Path.GetFileNameWithoutExtension(imageFilter.GetFilename());
imageInfo.Sectors = qHdr.image_size / 512;
imageInfo.SectorSize = 512;
imageInfo.XmlMediaType = XmlMediaType.BlockMedia;
imageInfo.MediaType = MediaType.GENERIC_HDD;
imageInfo.ImageSize = qHdr.image_size;
imageInfo.Cylinders = (uint)(imageInfo.Sectors / 16 / 63);
imageInfo.Heads = 16;
imageInfo.Cylinders = (uint)(imageInfo.Sectors / 16 / 63);
imageInfo.Heads = 16;
imageInfo.SectorsPerTrack = 63;
return true;
@@ -289,7 +292,7 @@ namespace DiscImageChef.DiscImages
{
l2Table = new ulong[tableSize];
imageStream.Seek((long)l1Table[l1Off], SeekOrigin.Begin);
byte[] l2TableB = new byte[tableSize * 8];
byte[] l2TableB = new byte[tableSize * 8];
imageStream.Read(l2TableB, 0, (int)tableSize * 8);
DicConsole.DebugWriteLine("QED plugin", "Reading L2 table #{0}", l1Off);
for(long i = 0; i < l2Table.LongLength; i++) l2Table[i] = BitConverter.ToUInt64(l2TableB, (int)(i * 8));
@@ -348,46 +351,6 @@ namespace DiscImageChef.DiscImages
return ms.ToArray();
}
static bool IsPowerOfTwo(uint x)
{
while((x & 1) == 0 && x > 1) x >>= 1;
return x == 1;
}
static int Ctz32(uint val)
{
int cnt = 0;
if((val & 0xFFFF) == 0)
{
cnt += 16;
val >>= 16;
}
if((val & 0xFF) == 0)
{
cnt += 8;
val >>= 8;
}
if((val & 0xF) == 0)
{
cnt += 4;
val >>= 4;
}
if((val & 0x3) == 0)
{
cnt += 2;
val >>= 2;
}
if((val & 0x1) == 0)
{
cnt++;
val >>= 1;
}
if((val & 0x1) == 0) cnt++;
return cnt;
}
public byte[] ReadSectorTag(ulong sectorAddress, SectorTagType tag)
{
throw new FeatureUnsupportedImageException("Feature not supported by image format");
@@ -464,7 +427,7 @@ namespace DiscImageChef.DiscImages
}
public bool? VerifySectors(ulong sectorAddress, uint length, out List<ulong> failingLbas,
out List<ulong> unknownLbas)
out List<ulong> unknownLbas)
{
failingLbas = new List<ulong>();
unknownLbas = new List<ulong>();
@@ -474,7 +437,7 @@ namespace DiscImageChef.DiscImages
}
public bool? VerifySectors(ulong sectorAddress, uint length, uint track, out List<ulong> failingLbas,
out List<ulong> unknownLbas)
out List<ulong> unknownLbas)
{
throw new FeatureUnsupportedImageException("Feature not supported by image format");
}
@@ -484,6 +447,306 @@ namespace DiscImageChef.DiscImages
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[] {".qed"};
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(sectorSize != 512)
{
ErrorMessage = "Unsupported sector size";
return false;
}
if(!SupportedMediaTypes.Contains(mediaType))
{
ErrorMessage = $"Unsupport media format {mediaType}";
return false;
}
// TODO: Correct this calculation
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;
}
qHdr = new QedHeader
{
magic = QED_MAGIC,
cluster_size = DEFAULT_CLUSTER_SIZE,
table_size = DEFAULT_TABLE_SIZE,
header_size = 1,
l1_table_offset = DEFAULT_CLUSTER_SIZE,
image_size = sectors * sectorSize
};
clusterSectors = qHdr.cluster_size / 512;
tableSize = qHdr.cluster_size * qHdr.table_size / 8;
l1Table = new ulong[tableSize];
l1Mask = 0;
int c = 0;
clusterBits = Ctz32(qHdr.cluster_size);
l2Mask = (tableSize - 1) << clusterBits;
l1Shift = clusterBits + Ctz32(tableSize);
for(int i = 0; i < 64; i++)
{
l1Mask <<= 1;
if(c >= 64 - l1Shift) continue;
l1Mask += 1;
c++;
}
sectorMask = 0;
for(int i = 0; i < clusterBits; i++) sectorMask = (sectorMask << 1) + 1;
byte[] empty = new byte[qHdr.l1_table_offset + tableSize * 8];
writingStream.Write(empty, 0, empty.Length);
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 != imageInfo.SectorSize)
{
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 byteAddress = sectorAddress * 512;
ulong l1Off = (byteAddress & l1Mask) >> l1Shift;
if((long)l1Off >= l1Table.LongLength)
throw new ArgumentOutOfRangeException(nameof(l1Off),
$"Trying to write past L1 table, position {l1Off} of a max {l1Table.LongLength}");
if(l1Table[l1Off] == 0)
{
writingStream.Seek(0, SeekOrigin.End);
l1Table[l1Off] = (ulong)writingStream.Position;
byte[] l2TableB = new byte[tableSize * 8];
writingStream.Seek(0, SeekOrigin.End);
writingStream.Write(l2TableB, 0, l2TableB.Length);
}
writingStream.Position = (long)l1Table[l1Off];
ulong l2Off = (byteAddress & l2Mask) >> clusterBits;
writingStream.Seek((long)(l1Table[l1Off] + l2Off * 8), SeekOrigin.Begin);
byte[] entry = new byte[8];
writingStream.Read(entry, 0, 8);
ulong offset = BitConverter.ToUInt64(entry, 0);
if(offset == 0)
{
offset = (ulong)writingStream.Length;
byte[] cluster = new byte[qHdr.cluster_size];
entry = BitConverter.GetBytes(offset);
writingStream.Seek((long)(l1Table[l1Off] + l2Off * 8), SeekOrigin.Begin);
writingStream.Write(entry, 0, 8);
writingStream.Seek(0, SeekOrigin.End);
writingStream.Write(cluster, 0, cluster.Length);
}
writingStream.Seek((long)(offset + (byteAddress & sectorMask)), SeekOrigin.Begin);
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 % imageInfo.SectorSize != 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[imageInfo.SectorSize];
Array.Copy(data, i * imageInfo.SectorSize, tmp, 0, imageInfo.SectorSize);
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(QedHeader))];
IntPtr hdrPtr = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(QedHeader)));
Marshal.StructureToPtr(qHdr, hdrPtr, true);
Marshal.Copy(hdrPtr, hdr, 0, hdr.Length);
Marshal.FreeHGlobal(hdrPtr);
writingStream.Seek(0, SeekOrigin.Begin);
writingStream.Write(hdr, 0, hdr.Length);
writingStream.Seek((long)qHdr.l1_table_offset, SeekOrigin.Begin);
for(long i = 0; i < l1Table.LongLength; i++) writingStream.Write(BitConverter.GetBytes(l1Table[i]), 0, 8);
return true;
}
public bool SetMetadata(ImageInfo metadata)
{
return true;
}
public bool SetGeometry(uint cylinders, uint heads, uint sectorsPerTrack)
{
// Not stored in image
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;
}
static bool IsPowerOfTwo(uint x)
{
while((x & 1) == 0 && x > 1) x >>= 1;
return x == 1;
}
static int Ctz32(uint val)
{
int cnt = 0;
if((val & 0xFFFF) == 0)
{
cnt += 16;
val >>= 16;
}
if((val & 0xFF) == 0)
{
cnt += 8;
val >>= 8;
}
if((val & 0xF) == 0)
{
cnt += 4;
val >>= 4;
}
if((val & 0x3) == 0)
{
cnt += 2;
val >>= 2;
}
if((val & 0x1) == 0)
{
cnt++;
val >>= 1;
}
if((val & 0x1) == 0) cnt++;
return cnt;
}
/// <summary>
/// QED header, big-endian
/// </summary>

View File

@@ -386,7 +386,7 @@ namespace DiscImageChef.DiscImages
public bool Create(string path, MediaType mediaType, Dictionary<string, string> options, ulong sectors,
uint sectorSize)
{
if(sectorSize == 0)
if(sectorSize != 512)
{
ErrorMessage = "Unsupported sector size";
return false;