Files
Aaru/Aaru.Images/QCOW2/Read.cs

341 lines
13 KiB
C#
Raw Normal View History

// /***************************************************************************
2020-02-27 12:31:25 +00:00
// Aaru Data Preservation Suite
// ----------------------------------------------------------------------------
//
// Filename : Read.cs
// Author(s) : Natalia Portillo <claunia@claunia.com>
//
// Component : Disk image plugins.
//
// --[ Description ] ----------------------------------------------------------
//
// Reads QEMU Copy-On-Write v2 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/>.
//
// ----------------------------------------------------------------------------
2022-02-18 10:02:53 +00:00
// Copyright © 2011-2022 Natalia Portillo
// ****************************************************************************/
2022-03-07 07:36:44 +00:00
namespace Aaru.DiscImages;
using System;
using System.Collections.Generic;
using System.IO;
using System.Runtime.InteropServices;
2020-02-27 00:33:26 +00:00
using Aaru.CommonTypes;
using Aaru.CommonTypes.Enums;
using Aaru.CommonTypes.Interfaces;
using Aaru.Console;
2020-07-20 15:43:52 +01:00
using Aaru.Helpers;
using SharpCompress.Compressors;
using SharpCompress.Compressors.Deflate;
2020-02-27 00:33:26 +00:00
using Marshal = Aaru.Helpers.Marshal;
2022-03-06 13:29:38 +00:00
public sealed partial class Qcow2
{
2022-03-06 13:29:38 +00:00
/// <inheritdoc />
public ErrorNumber Open(IFilter imageFilter)
{
2022-03-06 13:29:38 +00:00
Stream stream = imageFilter.GetDataForkStream();
stream.Seek(0, SeekOrigin.Begin);
if(stream.Length < 512)
return ErrorNumber.InvalidArgument;
2022-03-07 07:36:44 +00:00
var qHdrB = new byte[Marshal.SizeOf<Header>()];
2022-03-06 13:29:38 +00:00
stream.Read(qHdrB, 0, Marshal.SizeOf<Header>());
_qHdr = Marshal.SpanToStructureBigEndian<Header>(qHdrB);
AaruConsole.DebugWriteLine("QCOW plugin", "qHdr.magic = 0x{0:X8}", _qHdr.magic);
AaruConsole.DebugWriteLine("QCOW plugin", "qHdr.version = {0}", _qHdr.version);
AaruConsole.DebugWriteLine("QCOW plugin", "qHdr.backing_file_offset = {0}", _qHdr.backing_file_offset);
AaruConsole.DebugWriteLine("QCOW plugin", "qHdr.backing_file_size = {0}", _qHdr.backing_file_size);
AaruConsole.DebugWriteLine("QCOW plugin", "qHdr.cluster_bits = {0}", _qHdr.cluster_bits);
AaruConsole.DebugWriteLine("QCOW plugin", "qHdr.size = {0}", _qHdr.size);
AaruConsole.DebugWriteLine("QCOW plugin", "qHdr.crypt_method = {0}", _qHdr.crypt_method);
AaruConsole.DebugWriteLine("QCOW plugin", "qHdr.l1_size = {0}", _qHdr.l1_size);
AaruConsole.DebugWriteLine("QCOW plugin", "qHdr.l1_table_offset = {0}", _qHdr.l1_table_offset);
AaruConsole.DebugWriteLine("QCOW plugin", "qHdr.refcount_table_offset = {0}", _qHdr.refcount_table_offset);
2022-03-07 07:36:44 +00:00
AaruConsole.DebugWriteLine("QCOW plugin", "qHdr.refcount_table_clusters = {0}", _qHdr.refcount_table_clusters);
2022-03-06 13:29:38 +00:00
AaruConsole.DebugWriteLine("QCOW plugin", "qHdr.nb_snapshots = {0}", _qHdr.nb_snapshots);
AaruConsole.DebugWriteLine("QCOW plugin", "qHdr.snapshots_offset = {0}", _qHdr.snapshots_offset);
if(_qHdr.version >= QCOW_VERSION3)
{
2022-03-06 13:29:38 +00:00
AaruConsole.DebugWriteLine("QCOW plugin", "qHdr.features = {0:X}", _qHdr.features);
AaruConsole.DebugWriteLine("QCOW plugin", "qHdr.compat_features = {0:X}", _qHdr.compat_features);
AaruConsole.DebugWriteLine("QCOW plugin", "qHdr.autoclear_features = {0:X}", _qHdr.autoclear_features);
AaruConsole.DebugWriteLine("QCOW plugin", "qHdr.refcount_order = {0}", _qHdr.refcount_order);
AaruConsole.DebugWriteLine("QCOW plugin", "qHdr.header_length = {0}", _qHdr.header_length);
if((_qHdr.features & QCOW_FEATURE_MASK) != 0)
{
AaruConsole.
ErrorWriteLine($"Unknown incompatible features {_qHdr.features & QCOW_FEATURE_MASK:X} enabled, not proceeding.");
return ErrorNumber.InvalidArgument;
2022-03-06 13:29:38 +00:00
}
}
2022-03-06 13:29:38 +00:00
if(_qHdr.size <= 1)
{
AaruConsole.ErrorWriteLine("Image size is too small");
2020-07-20 21:11:32 +01:00
2022-03-06 13:29:38 +00:00
return ErrorNumber.InvalidArgument;
}
2020-02-29 18:03:35 +00:00
2022-03-06 13:29:38 +00:00
if(_qHdr.cluster_bits < 9 ||
_qHdr.cluster_bits > 16)
{
AaruConsole.ErrorWriteLine("Cluster size must be between 512 bytes and 64 Kbytes");
2020-02-29 18:03:35 +00:00
2022-03-06 13:29:38 +00:00
return ErrorNumber.InvalidArgument;
}
2022-03-06 13:29:38 +00:00
if(_qHdr.crypt_method > QCOW_ENCRYPTION_AES)
{
AaruConsole.ErrorWriteLine("Invalid encryption method");
2022-03-06 13:29:38 +00:00
return ErrorNumber.InvalidArgument;
}
2021-09-21 04:55:28 +01:00
2022-03-06 13:29:38 +00:00
if(_qHdr.crypt_method > QCOW_ENCRYPTION_NONE)
{
AaruConsole.ErrorWriteLine("AES encrypted images not yet supported");
2022-03-06 13:29:38 +00:00
return ErrorNumber.NotImplemented;
}
2021-09-21 04:55:28 +01:00
2022-03-06 13:29:38 +00:00
if(_qHdr.backing_file_offset != 0)
{
AaruConsole.ErrorWriteLine("Differencing images not yet supported");
2022-03-06 13:29:38 +00:00
return ErrorNumber.NotImplemented;
}
2021-09-21 04:55:28 +01:00
2022-03-06 13:29:38 +00:00
_clusterSize = 1 << (int)_qHdr.cluster_bits;
_clusterSectors = 1 << ((int)_qHdr.cluster_bits - 9);
_l2Bits = (int)(_qHdr.cluster_bits - 3);
_l2Size = 1 << _l2Bits;
2022-03-06 13:29:38 +00:00
AaruConsole.DebugWriteLine("QCOW plugin", "qHdr.clusterSize = {0}", _clusterSize);
AaruConsole.DebugWriteLine("QCOW plugin", "qHdr.clusterSectors = {0}", _clusterSectors);
AaruConsole.DebugWriteLine("QCOW plugin", "qHdr.qHdr.l1_size = {0}", _qHdr.l1_size);
AaruConsole.DebugWriteLine("QCOW plugin", "qHdr.l2Size = {0}", _l2Size);
AaruConsole.DebugWriteLine("QCOW plugin", "qHdr.sectors = {0}", _imageInfo.Sectors);
2021-09-21 04:55:28 +01:00
2022-03-07 07:36:44 +00:00
var l1TableB = new byte[_qHdr.l1_size * 8];
2022-03-06 13:29:38 +00:00
stream.Seek((long)_qHdr.l1_table_offset, SeekOrigin.Begin);
stream.Read(l1TableB, 0, (int)_qHdr.l1_size * 8);
_l1Table = MemoryMarshal.Cast<byte, ulong>(l1TableB).ToArray();
AaruConsole.DebugWriteLine("QCOW plugin", "Reading L1 table");
2022-03-06 13:29:38 +00:00
for(long i = 0; i < _l1Table.LongLength; i++)
_l1Table[i] = Swapping.Swap(_l1Table[i]);
2021-09-21 04:55:28 +01:00
2022-03-06 13:29:38 +00:00
_l1Mask = 0;
2022-03-07 07:36:44 +00:00
var c = 0;
2022-03-06 13:29:38 +00:00
_l1Shift = (int)(_l2Bits + _qHdr.cluster_bits);
2022-03-07 07:36:44 +00:00
for(var i = 0; i < 64; i++)
2022-03-06 13:29:38 +00:00
{
_l1Mask <<= 1;
2021-09-21 04:55:28 +01:00
2022-03-06 13:29:38 +00:00
if(c >= 64 - _l1Shift)
continue;
2022-03-06 13:29:38 +00:00
_l1Mask += 1;
c++;
}
2020-07-20 21:11:32 +01:00
2022-03-06 13:29:38 +00:00
_l2Mask = 0;
2020-07-20 21:11:32 +01:00
2022-03-07 07:36:44 +00:00
for(var i = 0; i < _l2Bits; i++)
2022-03-06 13:29:38 +00:00
_l2Mask = (_l2Mask << 1) + 1;
2020-02-29 18:03:35 +00:00
2022-03-06 13:29:38 +00:00
_l2Mask <<= (int)_qHdr.cluster_bits;
2022-03-06 13:29:38 +00:00
_sectorMask = 0;
2022-03-07 07:36:44 +00:00
for(var i = 0; i < _qHdr.cluster_bits; i++)
2022-03-06 13:29:38 +00:00
_sectorMask = (_sectorMask << 1) + 1;
2022-03-06 13:29:38 +00:00
AaruConsole.DebugWriteLine("QCOW plugin", "qHdr.l1Mask = {0:X}", _l1Mask);
AaruConsole.DebugWriteLine("QCOW plugin", "qHdr.l1Shift = {0}", _l1Shift);
AaruConsole.DebugWriteLine("QCOW plugin", "qHdr.l2Mask = {0:X}", _l2Mask);
AaruConsole.DebugWriteLine("QCOW plugin", "qHdr.sectorMask = {0:X}", _sectorMask);
2022-03-06 13:29:38 +00:00
_maxL2TableCache = MAX_CACHE_SIZE / (_l2Size * 8);
_maxClusterCache = MAX_CACHE_SIZE / _clusterSize;
2022-03-06 13:29:38 +00:00
_imageStream = stream;
2020-02-29 18:03:35 +00:00
2022-03-06 13:29:38 +00:00
_sectorCache = new Dictionary<ulong, byte[]>();
_l2TableCache = new Dictionary<ulong, ulong[]>();
_clusterCache = new Dictionary<ulong, byte[]>();
2022-03-06 13:29:38 +00:00
_imageInfo.CreationTime = imageFilter.CreationTime;
_imageInfo.LastModificationTime = imageFilter.LastWriteTime;
_imageInfo.MediaTitle = Path.GetFileNameWithoutExtension(imageFilter.Filename);
_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}";
2022-03-06 13:29:38 +00:00
_imageInfo.Cylinders = (uint)(_imageInfo.Sectors / 16 / 63);
_imageInfo.Heads = 16;
_imageInfo.SectorsPerTrack = 63;
2022-03-06 13:29:38 +00:00
return ErrorNumber.NoError;
}
2020-02-29 18:03:35 +00:00
2022-03-06 13:29:38 +00:00
/// <inheritdoc />
public ErrorNumber ReadSector(ulong sectorAddress, out byte[] buffer)
{
buffer = null;
2022-03-06 13:29:38 +00:00
if(sectorAddress > _imageInfo.Sectors - 1)
return ErrorNumber.OutOfRange;
2022-03-06 13:29:38 +00:00
// Check cache
if(_sectorCache.TryGetValue(sectorAddress, out buffer))
return ErrorNumber.NoError;
2022-03-06 13:29:38 +00:00
ulong byteAddress = sectorAddress * 512;
2022-03-06 13:29:38 +00:00
ulong l1Off = (byteAddress & _l1Mask) >> _l1Shift;
2022-03-06 13:29:38 +00:00
if((long)l1Off >= _l1Table.LongLength)
{
AaruConsole.DebugWriteLine("QCOW2 plugin",
$"Trying to read past L1 table, position {l1Off} of a max {_l1Table.LongLength}");
2022-03-06 13:29:38 +00:00
return ErrorNumber.InvalidArgument;
}
2022-03-06 13:29:38 +00:00
// TODO: Implement differential images
if(_l1Table[l1Off] == 0)
{
2022-03-06 13:29:38 +00:00
buffer = new byte[512];
2022-03-06 13:29:38 +00:00
return ErrorNumber.NoError;
}
2022-03-06 13:29:38 +00:00
if(!_l2TableCache.TryGetValue(l1Off, out ulong[] l2Table))
{
_imageStream.Seek((long)(_l1Table[l1Off] & QCOW_FLAGS_MASK), SeekOrigin.Begin);
2022-03-07 07:36:44 +00:00
var l2TableB = new byte[_l2Size * 8];
2022-03-06 13:29:38 +00:00
_imageStream.Read(l2TableB, 0, _l2Size * 8);
AaruConsole.DebugWriteLine("QCOW plugin", "Reading L2 table #{0}", l1Off);
l2Table = MemoryMarshal.Cast<byte, ulong>(l2TableB).ToArray();
2022-03-06 13:29:38 +00:00
for(long i = 0; i < l2Table.LongLength; i++)
l2Table[i] = Swapping.Swap(l2Table[i]);
2022-03-06 13:29:38 +00:00
if(_l2TableCache.Count >= _maxL2TableCache)
_l2TableCache.Clear();
2022-03-06 13:29:38 +00:00
_l2TableCache.Add(l1Off, l2Table);
}
2022-03-06 13:29:38 +00:00
ulong l2Off = (byteAddress & _l2Mask) >> (int)_qHdr.cluster_bits;
2022-03-06 13:29:38 +00:00
ulong offset = l2Table[l2Off];
2022-03-06 13:29:38 +00:00
buffer = new byte[512];
2022-03-06 13:29:38 +00:00
if((offset & QCOW_FLAGS_MASK) != 0)
{
if(!_clusterCache.TryGetValue(offset, out byte[] cluster))
{
2022-03-06 13:29:38 +00:00
if((offset & QCOW_COMPRESSED) == QCOW_COMPRESSED)
{
ulong compSizeMask = (ulong)(1 << (int)(_qHdr.cluster_bits - 8)) - 1;
2022-03-07 07:36:44 +00:00
var countbits = (byte)(_qHdr.cluster_bits - 8);
2022-03-06 13:29:38 +00:00
compSizeMask <<= 62 - countbits;
ulong offMask = ~compSizeMask & QCOW_FLAGS_MASK;
2022-03-06 13:29:38 +00:00
ulong realOff = offset & offMask;
ulong compSize = (((offset & compSizeMask) >> (62 - countbits)) + 1) * 512;
2022-03-07 07:36:44 +00:00
var zCluster = new byte[compSize];
2022-03-06 13:29:38 +00:00
_imageStream.Seek((long)realOff, SeekOrigin.Begin);
_imageStream.Read(zCluster, 0, (int)compSize);
2022-03-06 13:29:38 +00:00
var zStream = new DeflateStream(new MemoryStream(zCluster), CompressionMode.Decompress);
cluster = new byte[_clusterSize];
int read = zStream.Read(cluster, 0, _clusterSize);
2022-03-06 13:29:38 +00:00
if(read != _clusterSize)
return ErrorNumber.InOutError;
}
else
{
2022-03-06 13:29:38 +00:00
cluster = new byte[_clusterSize];
_imageStream.Seek((long)(offset & QCOW_FLAGS_MASK), SeekOrigin.Begin);
_imageStream.Read(cluster, 0, _clusterSize);
}
2022-03-06 13:29:38 +00:00
if(_clusterCache.Count >= _maxClusterCache)
_clusterCache.Clear();
2022-03-06 13:29:38 +00:00
_clusterCache.Add(offset, cluster);
}
2022-03-06 13:29:38 +00:00
Array.Copy(cluster, (int)(byteAddress & _sectorMask), buffer, 0, 512);
}
2022-03-06 13:29:38 +00:00
if(_sectorCache.Count >= MAX_CACHED_SECTORS)
_sectorCache.Clear();
2022-03-06 13:29:38 +00:00
_sectorCache.Add(sectorAddress, buffer);
2022-03-06 13:29:38 +00:00
return ErrorNumber.NoError;
}
2022-03-06 13:29:38 +00:00
/// <inheritdoc />
public ErrorNumber ReadSectors(ulong sectorAddress, uint length, out byte[] buffer)
{
buffer = null;
2022-03-06 13:29:38 +00:00
if(sectorAddress > _imageInfo.Sectors - 1)
return ErrorNumber.OutOfRange;
2022-03-06 13:29:38 +00:00
if(sectorAddress + length > _imageInfo.Sectors)
return ErrorNumber.OutOfRange;
2022-03-06 13:29:38 +00:00
var ms = new MemoryStream();
2022-03-06 13:29:38 +00:00
for(uint i = 0; i < length; i++)
{
ErrorNumber errno = ReadSector(sectorAddress + i, out byte[] sector);
2022-03-06 13:29:38 +00:00
if(errno != ErrorNumber.NoError)
return errno;
ms.Write(sector, 0, sector.Length);
}
2022-03-06 13:29:38 +00:00
buffer = ms.ToArray();
return ErrorNumber.NoError;
}
}