mirror of
https://github.com/aaru-dps/Aaru.Server.git
synced 2025-12-16 19:24:27 +00:00
Add writing support to QEMU Copy-On-Write v2 disk image.
This commit is contained in:
@@ -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;
|
||||
@@ -42,80 +43,82 @@ using SharpCompress.Compressors.Deflate;
|
||||
|
||||
namespace DiscImageChef.DiscImages
|
||||
{
|
||||
public class Qcow2 : IMediaImage
|
||||
public class Qcow2 : IWritableImage
|
||||
{
|
||||
/// <summary>
|
||||
/// Magic number: 'Q', 'F', 'I', 0xFB
|
||||
/// </summary>
|
||||
const uint QCOW_MAGIC = 0x514649FB;
|
||||
const uint QCOW_VERSION2 = 2;
|
||||
const uint QCOW_VERSION3 = 3;
|
||||
const uint QCOW_MAGIC = 0x514649FB;
|
||||
const uint QCOW_VERSION2 = 2;
|
||||
const uint QCOW_VERSION3 = 3;
|
||||
const uint QCOW_ENCRYPTION_NONE = 0;
|
||||
const uint QCOW_ENCRYPTION_AES = 1;
|
||||
const uint QCOW_ENCRYPTION_AES = 1;
|
||||
|
||||
const ulong QCOW_FEATURE_DIRTY = 0x01;
|
||||
const ulong QCOW_FEATURE_DIRTY = 0x01;
|
||||
const ulong QCOW_FEATURE_CORRUPT = 0x02;
|
||||
const ulong QCOW_FEATURE_MASK = 0xFFFFFFFFFFFFFFFC;
|
||||
const ulong QCOW_FEATURE_MASK = 0xFFFFFFFFFFFFFFFC;
|
||||
|
||||
const ulong QCOW_COMPAT_FEATURE_LAZY_REFCOUNTS = 0x01;
|
||||
const ulong QCOW_AUTO_CLEAR_FEATURE_BITMAP = 0x01;
|
||||
const ulong QCOW_AUTO_CLEAR_FEATURE_BITMAP = 0x01;
|
||||
|
||||
const ulong QCOW_FLAGS_MASK = 0x3FFFFFFFFFFFFFFF;
|
||||
const ulong QCOW_COPIED = 0x8000000000000000;
|
||||
const ulong QCOW_COPIED = 0x8000000000000000;
|
||||
const ulong QCOW_COMPRESSED = 0x4000000000000000;
|
||||
|
||||
const ulong QCOW_HEADER_EXTENSION_BACKING_FILE = 0xE2792ACA;
|
||||
const ulong QCOW_HEADER_EXTENSION_BACKING_FILE = 0xE2792ACA;
|
||||
const ulong QCOW_HEADER_EXTENSION_FEATURE_TABLE = 0x6803F857;
|
||||
const ulong QCOW_HEADER_EXTENSION_BITMAPS = 0x23852875;
|
||||
const ulong QCOW_HEADER_EXTENSION_BITMAPS = 0x23852875;
|
||||
|
||||
const int MAX_CACHE_SIZE = 16777216;
|
||||
|
||||
const int MAX_CACHED_SECTORS = MAX_CACHE_SIZE / 512;
|
||||
const int MAX_CACHED_SECTORS = MAX_CACHE_SIZE / 512;
|
||||
Dictionary<ulong, byte[]> clusterCache;
|
||||
int clusterSectors;
|
||||
int clusterSize;
|
||||
ImageInfo imageInfo;
|
||||
int clusterSectors;
|
||||
int clusterSize;
|
||||
ImageInfo imageInfo;
|
||||
|
||||
Stream imageStream;
|
||||
|
||||
ulong l1Mask;
|
||||
int l1Shift;
|
||||
ulong[] l1Table;
|
||||
int l2Bits;
|
||||
ulong l2Mask;
|
||||
int l2Size;
|
||||
ulong l1Mask;
|
||||
int l1Shift;
|
||||
ulong[] l1Table;
|
||||
int l2Bits;
|
||||
ulong l2Mask;
|
||||
int l2Size;
|
||||
Dictionary<ulong, ulong[]> l2TableCache;
|
||||
int maxClusterCache;
|
||||
int maxL2TableCache;
|
||||
int maxClusterCache;
|
||||
int maxL2TableCache;
|
||||
|
||||
QCow2Header qHdr;
|
||||
ulong[] refCountTable;
|
||||
|
||||
Dictionary<ulong, byte[]> sectorCache;
|
||||
ulong sectorMask;
|
||||
ulong sectorMask;
|
||||
FileStream writingStream;
|
||||
|
||||
public Qcow2()
|
||||
{
|
||||
imageInfo = new ImageInfo
|
||||
{
|
||||
ReadableSectorTags = new List<SectorTagType>(),
|
||||
ReadableMediaTags = new List<MediaTagType>(),
|
||||
HasPartitions = false,
|
||||
HasSessions = false,
|
||||
Version = null,
|
||||
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 = null,
|
||||
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
|
||||
};
|
||||
}
|
||||
@@ -123,7 +126,7 @@ namespace DiscImageChef.DiscImages
|
||||
public ImageInfo Info => imageInfo;
|
||||
|
||||
public string Name => "QEMU Copy-On-Write disk image v2";
|
||||
public Guid Id => new Guid("F20107CB-95B3-4398-894B-975261F1E8C5");
|
||||
public Guid Id => new Guid("F20107CB-95B3-4398-894B-975261F1E8C5");
|
||||
|
||||
public string Format => "QEMU Copy-On-Write";
|
||||
|
||||
@@ -148,7 +151,7 @@ namespace DiscImageChef.DiscImages
|
||||
qHdr = BigEndianMarshal.ByteArrayToStructureBigEndian<QCow2Header>(qHdrB);
|
||||
|
||||
DicConsole.DebugWriteLine("QCOW plugin", "qHdr.magic = 0x{0:X8}", qHdr.magic);
|
||||
DicConsole.DebugWriteLine("QCOW plugin", "qHdr.version = {0}", qHdr.version);
|
||||
DicConsole.DebugWriteLine("QCOW plugin", "qHdr.version = {0}", qHdr.version);
|
||||
|
||||
return qHdr.magic == QCOW_MAGIC && (qHdr.version == QCOW_VERSION2 || qHdr.version == QCOW_VERSION3);
|
||||
}
|
||||
@@ -164,28 +167,28 @@ namespace DiscImageChef.DiscImages
|
||||
stream.Read(qHdrB, 0, Marshal.SizeOf(qHdr));
|
||||
qHdr = BigEndianMarshal.ByteArrayToStructureBigEndian<QCow2Header>(qHdrB);
|
||||
|
||||
DicConsole.DebugWriteLine("QCOW plugin", "qHdr.magic = 0x{0:X8}", qHdr.magic);
|
||||
DicConsole.DebugWriteLine("QCOW plugin", "qHdr.version = {0}", qHdr.version);
|
||||
DicConsole.DebugWriteLine("QCOW plugin", "qHdr.backing_file_offset = {0}", qHdr.backing_file_offset);
|
||||
DicConsole.DebugWriteLine("QCOW plugin", "qHdr.backing_file_size = {0}", qHdr.backing_file_size);
|
||||
DicConsole.DebugWriteLine("QCOW plugin", "qHdr.cluster_bits = {0}", qHdr.cluster_bits);
|
||||
DicConsole.DebugWriteLine("QCOW plugin", "qHdr.size = {0}", qHdr.size);
|
||||
DicConsole.DebugWriteLine("QCOW plugin", "qHdr.crypt_method = {0}", qHdr.crypt_method);
|
||||
DicConsole.DebugWriteLine("QCOW plugin", "qHdr.l1_size = {0}", qHdr.l1_size);
|
||||
DicConsole.DebugWriteLine("QCOW plugin", "qHdr.l1_table_offset = {0}", qHdr.l1_table_offset);
|
||||
DicConsole.DebugWriteLine("QCOW plugin", "qHdr.magic = 0x{0:X8}", qHdr.magic);
|
||||
DicConsole.DebugWriteLine("QCOW plugin", "qHdr.version = {0}", qHdr.version);
|
||||
DicConsole.DebugWriteLine("QCOW plugin", "qHdr.backing_file_offset = {0}", qHdr.backing_file_offset);
|
||||
DicConsole.DebugWriteLine("QCOW plugin", "qHdr.backing_file_size = {0}", qHdr.backing_file_size);
|
||||
DicConsole.DebugWriteLine("QCOW plugin", "qHdr.cluster_bits = {0}", qHdr.cluster_bits);
|
||||
DicConsole.DebugWriteLine("QCOW plugin", "qHdr.size = {0}", qHdr.size);
|
||||
DicConsole.DebugWriteLine("QCOW plugin", "qHdr.crypt_method = {0}", qHdr.crypt_method);
|
||||
DicConsole.DebugWriteLine("QCOW plugin", "qHdr.l1_size = {0}", qHdr.l1_size);
|
||||
DicConsole.DebugWriteLine("QCOW plugin", "qHdr.l1_table_offset = {0}", qHdr.l1_table_offset);
|
||||
DicConsole.DebugWriteLine("QCOW plugin", "qHdr.refcount_table_offset = {0}", qHdr.refcount_table_offset);
|
||||
DicConsole.DebugWriteLine("QCOW plugin", "qHdr.refcount_table_clusters = {0}",
|
||||
qHdr.refcount_table_clusters);
|
||||
DicConsole.DebugWriteLine("QCOW plugin", "qHdr.nb_snapshots = {0}", qHdr.nb_snapshots);
|
||||
DicConsole.DebugWriteLine("QCOW plugin", "qHdr.nb_snapshots = {0}", qHdr.nb_snapshots);
|
||||
DicConsole.DebugWriteLine("QCOW plugin", "qHdr.snapshots_offset = {0}", qHdr.snapshots_offset);
|
||||
|
||||
if(qHdr.version >= QCOW_VERSION3)
|
||||
{
|
||||
DicConsole.DebugWriteLine("QCOW plugin", "qHdr.features = {0:X}", qHdr.features);
|
||||
DicConsole.DebugWriteLine("QCOW plugin", "qHdr.compat_features = {0:X}", qHdr.compat_features);
|
||||
DicConsole.DebugWriteLine("QCOW plugin", "qHdr.features = {0:X}", qHdr.features);
|
||||
DicConsole.DebugWriteLine("QCOW plugin", "qHdr.compat_features = {0:X}", qHdr.compat_features);
|
||||
DicConsole.DebugWriteLine("QCOW plugin", "qHdr.autoclear_features = {0:X}", qHdr.autoclear_features);
|
||||
DicConsole.DebugWriteLine("QCOW plugin", "qHdr.refcount_order = {0}", qHdr.refcount_order);
|
||||
DicConsole.DebugWriteLine("QCOW plugin", "qHdr.header_length = {0}", qHdr.header_length);
|
||||
DicConsole.DebugWriteLine("QCOW plugin", "qHdr.refcount_order = {0}", qHdr.refcount_order);
|
||||
DicConsole.DebugWriteLine("QCOW plugin", "qHdr.header_length = {0}", qHdr.header_length);
|
||||
|
||||
if((qHdr.features & QCOW_FEATURE_MASK) != 0)
|
||||
throw new
|
||||
@@ -207,21 +210,16 @@ namespace DiscImageChef.DiscImages
|
||||
if(qHdr.backing_file_offset != 0)
|
||||
throw new NotImplementedException("Differencing images not yet supported");
|
||||
|
||||
int shift = (int)(qHdr.cluster_bits + l2Bits);
|
||||
|
||||
if(qHdr.size > ulong.MaxValue - (ulong)(1 << shift))
|
||||
throw new ArgumentOutOfRangeException(nameof(qHdr.size), "Image is too large");
|
||||
|
||||
clusterSize = 1 << (int)qHdr.cluster_bits;
|
||||
clusterSize = 1 << (int)qHdr.cluster_bits;
|
||||
clusterSectors = 1 << ((int)qHdr.cluster_bits - 9);
|
||||
l2Bits = (int)(qHdr.cluster_bits - 3);
|
||||
l2Size = 1 << l2Bits;
|
||||
l2Bits = (int)(qHdr.cluster_bits - 3);
|
||||
l2Size = 1 << l2Bits;
|
||||
|
||||
DicConsole.DebugWriteLine("QCOW plugin", "qHdr.clusterSize = {0}", clusterSize);
|
||||
DicConsole.DebugWriteLine("QCOW plugin", "qHdr.clusterSize = {0}", clusterSize);
|
||||
DicConsole.DebugWriteLine("QCOW plugin", "qHdr.clusterSectors = {0}", clusterSectors);
|
||||
DicConsole.DebugWriteLine("QCOW plugin", "qHdr.qHdr.l1_size = {0}", qHdr.l1_size);
|
||||
DicConsole.DebugWriteLine("QCOW plugin", "qHdr.l2Size = {0}", l2Size);
|
||||
DicConsole.DebugWriteLine("QCOW plugin", "qHdr.sectors = {0}", imageInfo.Sectors);
|
||||
DicConsole.DebugWriteLine("QCOW plugin", "qHdr.qHdr.l1_size = {0}", qHdr.l1_size);
|
||||
DicConsole.DebugWriteLine("QCOW plugin", "qHdr.l2Size = {0}", l2Size);
|
||||
DicConsole.DebugWriteLine("QCOW plugin", "qHdr.sectors = {0}", imageInfo.Sectors);
|
||||
|
||||
BigEndianBitConverter.IsLittleEndian = BitConverter.IsLittleEndian;
|
||||
|
||||
@@ -234,8 +232,8 @@ namespace DiscImageChef.DiscImages
|
||||
for(long i = 0; i < l1Table.LongLength; i++)
|
||||
l1Table[i] = BigEndianBitConverter.ToUInt64(l1TableB, (int)(i * 8));
|
||||
|
||||
l1Mask = 0;
|
||||
int c = 0;
|
||||
l1Mask = 0;
|
||||
int c = 0;
|
||||
l1Shift = (int)(l2Bits + qHdr.cluster_bits);
|
||||
|
||||
for(int i = 0; i < 64; i++)
|
||||
@@ -248,17 +246,17 @@ namespace DiscImageChef.DiscImages
|
||||
c++;
|
||||
}
|
||||
|
||||
l2Mask = 0;
|
||||
l2Mask = 0;
|
||||
for(int i = 0; i < l2Bits; i++) l2Mask = (l2Mask << 1) + 1;
|
||||
|
||||
l2Mask <<= (int)qHdr.cluster_bits;
|
||||
|
||||
sectorMask = 0;
|
||||
sectorMask = 0;
|
||||
for(int i = 0; i < qHdr.cluster_bits; i++) sectorMask = (sectorMask << 1) + 1;
|
||||
|
||||
DicConsole.DebugWriteLine("QCOW plugin", "qHdr.l1Mask = {0:X}", l1Mask);
|
||||
DicConsole.DebugWriteLine("QCOW plugin", "qHdr.l1Shift = {0}", l1Shift);
|
||||
DicConsole.DebugWriteLine("QCOW plugin", "qHdr.l2Mask = {0:X}", l2Mask);
|
||||
DicConsole.DebugWriteLine("QCOW plugin", "qHdr.l1Mask = {0:X}", l1Mask);
|
||||
DicConsole.DebugWriteLine("QCOW plugin", "qHdr.l1Shift = {0}", l1Shift);
|
||||
DicConsole.DebugWriteLine("QCOW plugin", "qHdr.l2Mask = {0:X}", l2Mask);
|
||||
DicConsole.DebugWriteLine("QCOW plugin", "qHdr.sectorMask = {0:X}", sectorMask);
|
||||
|
||||
maxL2TableCache = MAX_CACHE_SIZE / (l2Size * 8);
|
||||
@@ -266,22 +264,22 @@ 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.size / 512;
|
||||
imageInfo.SectorSize = 512;
|
||||
imageInfo.XmlMediaType = XmlMediaType.BlockMedia;
|
||||
imageInfo.MediaType = MediaType.GENERIC_HDD;
|
||||
imageInfo.ImageSize = qHdr.size;
|
||||
imageInfo.Version = $"{qHdr.version}";
|
||||
imageInfo.MediaTitle = Path.GetFileNameWithoutExtension(imageFilter.GetFilename());
|
||||
imageInfo.Sectors = qHdr.size / 512;
|
||||
imageInfo.SectorSize = 512;
|
||||
imageInfo.XmlMediaType = XmlMediaType.BlockMedia;
|
||||
imageInfo.MediaType = MediaType.GENERIC_HDD;
|
||||
imageInfo.ImageSize = qHdr.size;
|
||||
imageInfo.Version = $"{qHdr.version}";
|
||||
|
||||
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;
|
||||
@@ -311,7 +309,7 @@ namespace DiscImageChef.DiscImages
|
||||
{
|
||||
l2Table = new ulong[l2Size];
|
||||
imageStream.Seek((long)(l1Table[l1Off] & QCOW_FLAGS_MASK), SeekOrigin.Begin);
|
||||
byte[] l2TableB = new byte[l2Size * 8];
|
||||
byte[] l2TableB = new byte[l2Size * 8];
|
||||
imageStream.Read(l2TableB, 0, l2Size * 8);
|
||||
DicConsole.DebugWriteLine("QCOW plugin", "Reading L2 table #{0}", l1Off);
|
||||
for(long i = 0; i < l2Table.LongLength; i++)
|
||||
@@ -337,12 +335,12 @@ namespace DiscImageChef.DiscImages
|
||||
ulong compSizeMask;
|
||||
ulong offMask;
|
||||
|
||||
compSizeMask = (ulong)(1 << (int)(qHdr.cluster_bits - 8)) - 1;
|
||||
byte countbits = (byte)(qHdr.cluster_bits - 8);
|
||||
compSizeMask <<= 62 - countbits;
|
||||
offMask = ~compSizeMask & QCOW_FLAGS_MASK;
|
||||
compSizeMask = (ulong)(1 << (int)(qHdr.cluster_bits - 8)) - 1;
|
||||
byte countbits = (byte)(qHdr.cluster_bits - 8);
|
||||
compSizeMask <<= 62 - countbits;
|
||||
offMask = ~compSizeMask & QCOW_FLAGS_MASK;
|
||||
|
||||
ulong realOff = offset & offMask;
|
||||
ulong realOff = offset & offMask;
|
||||
ulong compSize = (((offset & compSizeMask) >> (62 - countbits)) + 1) * 512;
|
||||
|
||||
byte[] zCluster = new byte[compSize];
|
||||
@@ -351,7 +349,7 @@ namespace DiscImageChef.DiscImages
|
||||
|
||||
DeflateStream zStream =
|
||||
new DeflateStream(new MemoryStream(zCluster), CompressionMode.Decompress);
|
||||
cluster = new byte[clusterSize];
|
||||
cluster = new byte[clusterSize];
|
||||
int read = zStream.Read(cluster, 0, clusterSize);
|
||||
|
||||
if(read != clusterSize)
|
||||
@@ -476,7 +474,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>();
|
||||
@@ -486,7 +484,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");
|
||||
}
|
||||
@@ -496,6 +494,326 @@ 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};
|
||||
public IEnumerable<(string name, Type type, string description)> SupportedOptions =>
|
||||
new (string name, Type type, string description)[] { };
|
||||
public IEnumerable<string> KnownExtensions => new[] {".qcow2", ".qc2", ".qcow3", ".qc3"};
|
||||
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 / 65536 > 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;
|
||||
}
|
||||
|
||||
string extension = Path.GetExtension(path);
|
||||
bool version3 = extension == ".qcow3" || extension == ".qc3";
|
||||
|
||||
qHdr = new QCow2Header
|
||||
{
|
||||
magic = QCOW_MAGIC,
|
||||
version = version3 ? QCOW_VERSION3 : QCOW_VERSION2,
|
||||
size = sectors * sectorSize,
|
||||
cluster_bits = 16,
|
||||
header_length = (uint)Marshal.SizeOf(typeof(QCow2Header))
|
||||
};
|
||||
|
||||
clusterSize = 1 << (int)qHdr.cluster_bits;
|
||||
clusterSectors = 1 << ((int)qHdr.cluster_bits - 9);
|
||||
l2Bits = (int)(qHdr.cluster_bits - 3);
|
||||
l2Size = 1 << l2Bits;
|
||||
|
||||
l1Mask = 0;
|
||||
int c = 0;
|
||||
l1Shift = (int)(l2Bits + qHdr.cluster_bits);
|
||||
|
||||
for(int i = 0; i < 64; i++)
|
||||
{
|
||||
l1Mask <<= 1;
|
||||
|
||||
if(c >= 64 - l1Shift) continue;
|
||||
|
||||
l1Mask += 1;
|
||||
c++;
|
||||
}
|
||||
|
||||
l2Mask = 0;
|
||||
for(int i = 0; i < l2Bits; i++) l2Mask = (l2Mask << 1) + 1;
|
||||
|
||||
l2Mask <<= (int)qHdr.cluster_bits;
|
||||
|
||||
sectorMask = 0;
|
||||
for(int i = 0; i < qHdr.cluster_bits; i++) sectorMask = (sectorMask << 1) + 1;
|
||||
|
||||
qHdr.l1_size = (uint)(qHdr.size >> l1Shift);
|
||||
if(qHdr.l1_size == 0) qHdr.l1_size = 1;
|
||||
l1Table = new ulong[qHdr.l1_size];
|
||||
|
||||
ulong clusters = qHdr.size / (ulong)clusterSize;
|
||||
ulong refCountBlocks = clusters * 2 / (ulong)clusterSize;
|
||||
if(refCountBlocks == 0) refCountBlocks = 1;
|
||||
|
||||
qHdr.refcount_table_offset = (ulong)clusterSize;
|
||||
qHdr.refcount_table_clusters =
|
||||
(uint)(refCountBlocks * 8 / (ulong)clusterSize);
|
||||
if(qHdr.refcount_table_clusters == 0) qHdr.refcount_table_clusters = 1;
|
||||
refCountTable = new ulong[refCountBlocks];
|
||||
qHdr.l1_table_offset =
|
||||
qHdr.refcount_table_offset + (ulong)(qHdr.refcount_table_clusters * clusterSize);
|
||||
ulong l1TableClusters =
|
||||
qHdr.l1_size * 8 / (ulong)clusterSize;
|
||||
if(l1TableClusters == 0) l1TableClusters = 1;
|
||||
|
||||
byte[] empty = new byte[qHdr.l1_table_offset + l1TableClusters * (ulong)clusterSize];
|
||||
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[l2Size * 8];
|
||||
writingStream.Seek(0, SeekOrigin.End);
|
||||
writingStream.Write(l2TableB, 0, l2TableB.Length);
|
||||
}
|
||||
|
||||
writingStream.Position = (long)l1Table[l1Off];
|
||||
|
||||
ulong l2Off = (byteAddress & l2Mask) >> (int)qHdr.cluster_bits;
|
||||
|
||||
writingStream.Seek((long)(l1Table[l1Off] + l2Off * 8), SeekOrigin.Begin);
|
||||
|
||||
BigEndianBitConverter.IsLittleEndian = BitConverter.IsLittleEndian;
|
||||
|
||||
byte[] entry = new byte[8];
|
||||
writingStream.Read(entry, 0, 8);
|
||||
ulong offset = BigEndianBitConverter.ToUInt64(entry, 0);
|
||||
|
||||
if(offset == 0)
|
||||
{
|
||||
offset = (ulong)writingStream.Length;
|
||||
byte[] cluster = new byte[clusterSize];
|
||||
entry = BigEndianBitConverter.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);
|
||||
|
||||
int refCountBlockEntries = clusterSize * 8 / 16;
|
||||
ulong refCountBlockIndex = offset / (ulong)clusterSize % (ulong)refCountBlockEntries;
|
||||
ulong refCountTableIndex = offset / (ulong)clusterSize / (ulong)refCountBlockEntries;
|
||||
|
||||
ulong refBlockOffset = refCountTable[refCountTableIndex];
|
||||
|
||||
if(refBlockOffset == 0)
|
||||
{
|
||||
refBlockOffset = (ulong)writingStream.Length;
|
||||
refCountTable[refCountTableIndex] = refBlockOffset;
|
||||
byte[] cluster = new byte[clusterSize];
|
||||
writingStream.Seek(0, SeekOrigin.End);
|
||||
writingStream.Write(cluster, 0, cluster.Length);
|
||||
}
|
||||
|
||||
writingStream.Seek((long)(refBlockOffset + refCountBlockIndex), SeekOrigin.Begin);
|
||||
writingStream.Write(new byte[] {0, 1}, 0, 2);
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
BigEndianBitConverter.IsLittleEndian = BitConverter.IsLittleEndian;
|
||||
writingStream.Seek(0, SeekOrigin.Begin);
|
||||
writingStream.Write(BigEndianBitConverter.GetBytes(qHdr.magic), 0, 4);
|
||||
writingStream.Write(BigEndianBitConverter.GetBytes(qHdr.version), 0, 4);
|
||||
writingStream.Write(BigEndianBitConverter.GetBytes(qHdr.backing_file_offset), 0, 8);
|
||||
writingStream.Write(BigEndianBitConverter.GetBytes(qHdr.backing_file_size), 0, 4);
|
||||
writingStream.Write(BigEndianBitConverter.GetBytes(qHdr.cluster_bits), 0, 4);
|
||||
writingStream.Write(BigEndianBitConverter.GetBytes(qHdr.size), 0, 8);
|
||||
writingStream.Write(BigEndianBitConverter.GetBytes(qHdr.crypt_method), 0, 4);
|
||||
writingStream.Write(BigEndianBitConverter.GetBytes(qHdr.l1_size), 0, 4);
|
||||
writingStream.Write(BigEndianBitConverter.GetBytes(qHdr.l1_table_offset), 0, 8);
|
||||
writingStream.Write(BigEndianBitConverter.GetBytes(qHdr.refcount_table_offset), 0, 8);
|
||||
writingStream.Write(BigEndianBitConverter.GetBytes(qHdr.refcount_table_clusters), 0, 4);
|
||||
writingStream.Write(BigEndianBitConverter.GetBytes(qHdr.nb_snapshots), 0, 4);
|
||||
writingStream.Write(BigEndianBitConverter.GetBytes(qHdr.snapshots_offset), 0, 8);
|
||||
if(qHdr.version == QCOW_VERSION3)
|
||||
{
|
||||
writingStream.Write(BigEndianBitConverter.GetBytes(qHdr.features), 0, 8);
|
||||
writingStream.Write(BigEndianBitConverter.GetBytes(qHdr.compat_features), 0, 8);
|
||||
writingStream.Write(BigEndianBitConverter.GetBytes(qHdr.autoclear_features), 0, 8);
|
||||
writingStream.Write(BigEndianBitConverter.GetBytes(qHdr.refcount_order), 0, 4);
|
||||
writingStream.Write(BigEndianBitConverter.GetBytes(qHdr.header_length), 0, 4);
|
||||
}
|
||||
|
||||
writingStream.Seek((long)qHdr.refcount_table_offset, SeekOrigin.Begin);
|
||||
for(long i = 0; i < refCountTable.LongLength; i++)
|
||||
writingStream.Write(BigEndianBitConverter.GetBytes(refCountTable[i]), 0, 8);
|
||||
|
||||
writingStream.Seek((long)qHdr.l1_table_offset, SeekOrigin.Begin);
|
||||
for(long i = 0; i < l1Table.LongLength; i++)
|
||||
writingStream.Write(BigEndianBitConverter.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;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// QCOW header, big-endian
|
||||
/// </summary>
|
||||
@@ -559,8 +877,8 @@ namespace DiscImageChef.DiscImages
|
||||
public ulong features;
|
||||
public ulong compat_features;
|
||||
public ulong autoclear_features;
|
||||
public uint refcount_order;
|
||||
public uint header_length;
|
||||
public uint refcount_order;
|
||||
public uint header_length;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user