2018-07-23 23:25:43 +01:00
|
|
|
|
// /***************************************************************************
|
2020-02-27 12:31:25 +00:00
|
|
|
|
// Aaru Data Preservation Suite
|
2018-07-23 23:25:43 +01:00
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
|
//
|
|
|
|
|
|
// Filename : Read.cs
|
|
|
|
|
|
// Author(s) : Natalia Portillo <claunia@claunia.com>
|
|
|
|
|
|
//
|
|
|
|
|
|
// Component : Disk image plugins.
|
|
|
|
|
|
//
|
|
|
|
|
|
// --[ Description ] ----------------------------------------------------------
|
|
|
|
|
|
//
|
2018-12-31 13:17:27 +00:00
|
|
|
|
// Reads QEMU Enhanced Disk images.
|
2018-07-23 23:25:43 +01:00
|
|
|
|
//
|
|
|
|
|
|
// --[ 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/>.
|
|
|
|
|
|
//
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
2020-12-31 23:08:23 +00:00
|
|
|
|
// Copyright © 2011-2021 Natalia Portillo
|
2018-07-23 23:25:43 +01:00
|
|
|
|
// ****************************************************************************/
|
|
|
|
|
|
|
|
|
|
|
|
using System;
|
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
using System.IO;
|
2019-03-15 23:00:12 +00:00
|
|
|
|
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;
|
|
|
|
|
|
using Marshal = Aaru.Helpers.Marshal;
|
2018-07-23 23:25:43 +01:00
|
|
|
|
|
2020-02-27 00:33:26 +00:00
|
|
|
|
namespace Aaru.DiscImages
|
2018-07-23 23:25:43 +01:00
|
|
|
|
{
|
2020-07-22 13:20:25 +01:00
|
|
|
|
public sealed partial class Qed
|
2018-07-23 23:25:43 +01:00
|
|
|
|
{
|
2021-08-17 13:56:05 +01:00
|
|
|
|
/// <inheritdoc />
|
2021-09-16 19:10:39 +01:00
|
|
|
|
public ErrorNumber Open(IFilter imageFilter)
|
2018-07-23 23:25:43 +01:00
|
|
|
|
{
|
|
|
|
|
|
Stream stream = imageFilter.GetDataForkStream();
|
|
|
|
|
|
stream.Seek(0, SeekOrigin.Begin);
|
|
|
|
|
|
|
2020-02-29 18:03:35 +00:00
|
|
|
|
if(stream.Length < 512)
|
2021-09-16 19:10:39 +01:00
|
|
|
|
return ErrorNumber.InvalidArgument;
|
2018-07-23 23:25:43 +01:00
|
|
|
|
|
2019-03-15 23:00:12 +00:00
|
|
|
|
byte[] qHdrB = new byte[68];
|
|
|
|
|
|
stream.Read(qHdrB, 0, 68);
|
2020-07-20 21:11:32 +01:00
|
|
|
|
_qHdr = Marshal.SpanToStructureLittleEndian<QedHeader>(qHdrB);
|
|
|
|
|
|
|
|
|
|
|
|
AaruConsole.DebugWriteLine("QED plugin", "qHdr.magic = 0x{0:X8}", _qHdr.magic);
|
|
|
|
|
|
AaruConsole.DebugWriteLine("QED plugin", "qHdr.cluster_size = {0}", _qHdr.cluster_size);
|
|
|
|
|
|
AaruConsole.DebugWriteLine("QED plugin", "qHdr.table_size = {0}", _qHdr.table_size);
|
|
|
|
|
|
AaruConsole.DebugWriteLine("QED plugin", "qHdr.header_size = {0}", _qHdr.header_size);
|
|
|
|
|
|
AaruConsole.DebugWriteLine("QED plugin", "qHdr.features = {0}", _qHdr.features);
|
|
|
|
|
|
AaruConsole.DebugWriteLine("QED plugin", "qHdr.compat_features = {0}", _qHdr.compat_features);
|
|
|
|
|
|
AaruConsole.DebugWriteLine("QED plugin", "qHdr.autoclear_features = {0}", _qHdr.autoclear_features);
|
|
|
|
|
|
AaruConsole.DebugWriteLine("QED plugin", "qHdr.l1_table_offset = {0}", _qHdr.l1_table_offset);
|
|
|
|
|
|
AaruConsole.DebugWriteLine("QED plugin", "qHdr.image_size = {0}", _qHdr.image_size);
|
|
|
|
|
|
AaruConsole.DebugWriteLine("QED plugin", "qHdr.backing_file_offset = {0}", _qHdr.backing_file_offset);
|
|
|
|
|
|
AaruConsole.DebugWriteLine("QED plugin", "qHdr.backing_file_size = {0}", _qHdr.backing_file_size);
|
|
|
|
|
|
|
|
|
|
|
|
if(_qHdr.image_size <= 1)
|
2021-09-21 04:55:28 +01:00
|
|
|
|
{
|
|
|
|
|
|
AaruConsole.ErrorWriteLine("Image size is too small");
|
|
|
|
|
|
return ErrorNumber.InvalidArgument;
|
|
|
|
|
|
}
|
2020-07-20 21:11:32 +01:00
|
|
|
|
if(!IsPowerOfTwo(_qHdr.cluster_size))
|
2021-09-21 04:55:28 +01:00
|
|
|
|
{
|
|
|
|
|
|
AaruConsole.ErrorWriteLine("Cluster size must be a power of 2");
|
|
|
|
|
|
return ErrorNumber.InvalidArgument;
|
|
|
|
|
|
}
|
2020-07-20 21:11:32 +01:00
|
|
|
|
if(_qHdr.cluster_size < 4096 ||
|
|
|
|
|
|
_qHdr.cluster_size > 67108864)
|
2021-09-21 04:55:28 +01:00
|
|
|
|
{
|
|
|
|
|
|
AaruConsole.ErrorWriteLine("Cluster size must be between 4 Kbytes and 64 Mbytes");
|
|
|
|
|
|
return ErrorNumber.InvalidArgument;
|
|
|
|
|
|
}
|
2020-07-20 21:11:32 +01:00
|
|
|
|
if(!IsPowerOfTwo(_qHdr.table_size))
|
2021-09-21 04:55:28 +01:00
|
|
|
|
{
|
|
|
|
|
|
AaruConsole.ErrorWriteLine("Table size must be a power of 2");
|
|
|
|
|
|
return ErrorNumber.InvalidArgument;
|
|
|
|
|
|
}
|
2020-07-20 21:11:32 +01:00
|
|
|
|
if(_qHdr.table_size < 1 ||
|
|
|
|
|
|
_qHdr.table_size > 16)
|
2021-09-21 04:55:28 +01:00
|
|
|
|
{
|
|
|
|
|
|
AaruConsole.ErrorWriteLine(
|
2018-07-23 23:25:43 +01:00
|
|
|
|
"Table size must be between 1 and 16 clusters");
|
2021-09-21 04:55:28 +01:00
|
|
|
|
return ErrorNumber.InvalidArgument;
|
|
|
|
|
|
}
|
2020-07-20 21:11:32 +01:00
|
|
|
|
if((_qHdr.features & QED_FEATURE_MASK) > 0)
|
2021-09-21 04:55:28 +01:00
|
|
|
|
{
|
|
|
|
|
|
AaruConsole.ErrorWriteLine(
|
2020-07-20 21:11:32 +01:00
|
|
|
|
$"Image uses unknown incompatible features {_qHdr.features & QED_FEATURE_MASK:X}");
|
2018-07-23 23:25:43 +01:00
|
|
|
|
|
2021-09-21 04:55:28 +01:00
|
|
|
|
return ErrorNumber.InvalidArgument;
|
|
|
|
|
|
}
|
2020-07-20 21:11:32 +01:00
|
|
|
|
if((_qHdr.features & QED_FEATURE_BACKING_FILE) == QED_FEATURE_BACKING_FILE)
|
2021-09-21 04:55:28 +01:00
|
|
|
|
{
|
|
|
|
|
|
AaruConsole.ErrorWriteLine("Differencing images not yet supported");
|
2018-07-23 23:25:43 +01:00
|
|
|
|
|
2021-09-21 04:55:28 +01:00
|
|
|
|
return ErrorNumber.NotImplemented;
|
|
|
|
|
|
}
|
2021-08-17 14:27:19 +01:00
|
|
|
|
_clusterSectors = _qHdr.cluster_size / 512;
|
|
|
|
|
|
_tableSize = _qHdr.cluster_size * _qHdr.table_size / 8;
|
2018-07-23 23:25:43 +01:00
|
|
|
|
|
2020-07-20 21:11:32 +01:00
|
|
|
|
AaruConsole.DebugWriteLine("QED plugin", "qHdr.clusterSectors = {0}", _clusterSectors);
|
|
|
|
|
|
AaruConsole.DebugWriteLine("QED plugin", "qHdr.tableSize = {0}", _tableSize);
|
2018-07-23 23:25:43 +01:00
|
|
|
|
|
2020-07-20 21:11:32 +01:00
|
|
|
|
byte[] l1TableB = new byte[_tableSize * 8];
|
|
|
|
|
|
stream.Seek((long)_qHdr.l1_table_offset, SeekOrigin.Begin);
|
|
|
|
|
|
stream.Read(l1TableB, 0, (int)_tableSize * 8);
|
2020-02-27 23:48:41 +00:00
|
|
|
|
AaruConsole.DebugWriteLine("QED plugin", "Reading L1 table");
|
2020-07-20 21:11:32 +01:00
|
|
|
|
_l1Table = MemoryMarshal.Cast<byte, ulong>(l1TableB).ToArray();
|
2018-07-23 23:25:43 +01:00
|
|
|
|
|
2020-07-20 21:11:32 +01:00
|
|
|
|
_l1Mask = 0;
|
2018-07-23 23:25:43 +01:00
|
|
|
|
int c = 0;
|
2020-07-20 21:11:32 +01:00
|
|
|
|
_clusterBits = Ctz32(_qHdr.cluster_size);
|
|
|
|
|
|
_l2Mask = (_tableSize - 1) << _clusterBits;
|
|
|
|
|
|
_l1Shift = _clusterBits + Ctz32(_tableSize);
|
2018-07-23 23:25:43 +01:00
|
|
|
|
|
|
|
|
|
|
for(int i = 0; i < 64; i++)
|
|
|
|
|
|
{
|
2020-07-20 21:11:32 +01:00
|
|
|
|
_l1Mask <<= 1;
|
2018-07-23 23:25:43 +01:00
|
|
|
|
|
2020-07-20 21:11:32 +01:00
|
|
|
|
if(c >= 64 - _l1Shift)
|
2020-02-29 18:03:35 +00:00
|
|
|
|
continue;
|
2018-07-23 23:25:43 +01:00
|
|
|
|
|
2020-07-20 21:11:32 +01:00
|
|
|
|
_l1Mask += 1;
|
2018-07-23 23:25:43 +01:00
|
|
|
|
c++;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-07-20 21:11:32 +01:00
|
|
|
|
_sectorMask = 0;
|
2018-07-23 23:25:43 +01:00
|
|
|
|
|
2020-07-20 21:11:32 +01:00
|
|
|
|
for(int i = 0; i < _clusterBits; i++)
|
|
|
|
|
|
_sectorMask = (_sectorMask << 1) + 1;
|
2020-02-29 18:03:35 +00:00
|
|
|
|
|
2020-07-20 21:11:32 +01:00
|
|
|
|
AaruConsole.DebugWriteLine("QED plugin", "qHdr.clusterBits = {0}", _clusterBits);
|
|
|
|
|
|
AaruConsole.DebugWriteLine("QED plugin", "qHdr.l1Mask = {0:X}", _l1Mask);
|
|
|
|
|
|
AaruConsole.DebugWriteLine("QED plugin", "qHdr.l1Shift = {0}", _l1Shift);
|
|
|
|
|
|
AaruConsole.DebugWriteLine("QED plugin", "qHdr.l2Mask = {0:X}", _l2Mask);
|
|
|
|
|
|
AaruConsole.DebugWriteLine("QED plugin", "qHdr.sectorMask = {0:X}", _sectorMask);
|
2018-07-23 23:25:43 +01:00
|
|
|
|
|
2020-07-20 21:11:32 +01:00
|
|
|
|
_maxL2TableCache = MAX_CACHE_SIZE / _tableSize;
|
|
|
|
|
|
_maxClusterCache = MAX_CACHE_SIZE / _qHdr.cluster_size;
|
2018-07-23 23:25:43 +01:00
|
|
|
|
|
2020-07-20 21:11:32 +01:00
|
|
|
|
_imageStream = stream;
|
2018-07-23 23:25:43 +01:00
|
|
|
|
|
2020-07-20 21:11:32 +01:00
|
|
|
|
_sectorCache = new Dictionary<ulong, byte[]>();
|
|
|
|
|
|
_l2TableCache = new Dictionary<ulong, ulong[]>();
|
|
|
|
|
|
_clusterCache = new Dictionary<ulong, byte[]>();
|
2018-07-23 23:25:43 +01:00
|
|
|
|
|
2021-09-15 11:25:26 +01:00
|
|
|
|
_imageInfo.CreationTime = imageFilter.CreationTime;
|
|
|
|
|
|
_imageInfo.LastModificationTime = imageFilter.LastWriteTime;
|
|
|
|
|
|
_imageInfo.MediaTitle = Path.GetFileNameWithoutExtension(imageFilter.Filename);
|
2020-07-20 21:11:32 +01:00
|
|
|
|
_imageInfo.Sectors = _qHdr.image_size / 512;
|
|
|
|
|
|
_imageInfo.SectorSize = 512;
|
|
|
|
|
|
_imageInfo.XmlMediaType = XmlMediaType.BlockMedia;
|
|
|
|
|
|
_imageInfo.MediaType = MediaType.GENERIC_HDD;
|
|
|
|
|
|
_imageInfo.ImageSize = _qHdr.image_size;
|
2018-07-23 23:25:43 +01:00
|
|
|
|
|
2020-07-20 21:11:32 +01:00
|
|
|
|
_imageInfo.Cylinders = (uint)(_imageInfo.Sectors / 16 / 63);
|
|
|
|
|
|
_imageInfo.Heads = 16;
|
|
|
|
|
|
_imageInfo.SectorsPerTrack = 63;
|
2018-07-23 23:25:43 +01:00
|
|
|
|
|
2021-09-16 19:10:39 +01:00
|
|
|
|
return ErrorNumber.NoError;
|
2018-07-23 23:25:43 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2021-08-17 13:56:05 +01:00
|
|
|
|
/// <inheritdoc />
|
2021-09-19 21:16:47 +01:00
|
|
|
|
public ErrorNumber ReadSector(ulong sectorAddress, out byte[] buffer)
|
2018-07-23 23:25:43 +01:00
|
|
|
|
{
|
2021-09-19 21:16:47 +01:00
|
|
|
|
buffer = null;
|
|
|
|
|
|
|
2020-07-20 21:11:32 +01:00
|
|
|
|
if(sectorAddress > _imageInfo.Sectors - 1)
|
2021-09-19 21:16:47 +01:00
|
|
|
|
return ErrorNumber.OutOfRange;
|
2018-07-23 23:25:43 +01:00
|
|
|
|
|
|
|
|
|
|
// Check cache
|
2021-09-19 21:16:47 +01:00
|
|
|
|
if(_sectorCache.TryGetValue(sectorAddress, out buffer))
|
|
|
|
|
|
return ErrorNumber.NoError;
|
2018-07-23 23:25:43 +01:00
|
|
|
|
|
|
|
|
|
|
ulong byteAddress = sectorAddress * 512;
|
|
|
|
|
|
|
2020-07-20 21:11:32 +01:00
|
|
|
|
ulong l1Off = (byteAddress & _l1Mask) >> _l1Shift;
|
2018-07-23 23:25:43 +01:00
|
|
|
|
|
2020-07-20 21:11:32 +01:00
|
|
|
|
if((long)l1Off >= _l1Table.LongLength)
|
2021-09-19 21:16:47 +01:00
|
|
|
|
{
|
|
|
|
|
|
AaruConsole.DebugWriteLine("QED plugin",
|
|
|
|
|
|
$"Trying to read past L1 table, position {l1Off} of a max {_l1Table.LongLength}");
|
|
|
|
|
|
|
|
|
|
|
|
return ErrorNumber.InvalidArgument;
|
|
|
|
|
|
}
|
2018-07-23 23:25:43 +01:00
|
|
|
|
|
|
|
|
|
|
// TODO: Implement differential images
|
2020-07-20 21:11:32 +01:00
|
|
|
|
if(_l1Table[l1Off] == 0)
|
2021-09-19 21:16:47 +01:00
|
|
|
|
{
|
|
|
|
|
|
buffer = new byte[512];
|
|
|
|
|
|
|
|
|
|
|
|
return ErrorNumber.NoError;
|
|
|
|
|
|
}
|
2018-07-23 23:25:43 +01:00
|
|
|
|
|
2020-07-20 21:11:32 +01:00
|
|
|
|
if(!_l2TableCache.TryGetValue(l1Off, out ulong[] l2Table))
|
2018-07-23 23:25:43 +01:00
|
|
|
|
{
|
2020-07-20 21:11:32 +01:00
|
|
|
|
_imageStream.Seek((long)_l1Table[l1Off], SeekOrigin.Begin);
|
|
|
|
|
|
byte[] l2TableB = new byte[_tableSize * 8];
|
|
|
|
|
|
_imageStream.Read(l2TableB, 0, (int)_tableSize * 8);
|
2020-02-27 23:48:41 +00:00
|
|
|
|
AaruConsole.DebugWriteLine("QED plugin", "Reading L2 table #{0}", l1Off);
|
2019-03-15 23:00:12 +00:00
|
|
|
|
l2Table = MemoryMarshal.Cast<byte, ulong>(l2TableB).ToArray();
|
2018-07-23 23:25:43 +01:00
|
|
|
|
|
2020-07-20 21:11:32 +01:00
|
|
|
|
if(_l2TableCache.Count >= _maxL2TableCache)
|
|
|
|
|
|
_l2TableCache.Clear();
|
2018-07-23 23:25:43 +01:00
|
|
|
|
|
2020-07-20 21:11:32 +01:00
|
|
|
|
_l2TableCache.Add(l1Off, l2Table);
|
2018-07-23 23:25:43 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2020-07-20 21:11:32 +01:00
|
|
|
|
ulong l2Off = (byteAddress & _l2Mask) >> _clusterBits;
|
2018-07-23 23:25:43 +01:00
|
|
|
|
|
|
|
|
|
|
ulong offset = l2Table[l2Off];
|
|
|
|
|
|
|
2021-09-19 21:16:47 +01:00
|
|
|
|
buffer = new byte[512];
|
2018-07-23 23:25:43 +01:00
|
|
|
|
|
2020-02-29 18:03:35 +00:00
|
|
|
|
if(offset != 0 &&
|
|
|
|
|
|
offset != 1)
|
2018-07-23 23:25:43 +01:00
|
|
|
|
{
|
2020-07-20 21:11:32 +01:00
|
|
|
|
if(!_clusterCache.TryGetValue(offset, out byte[] cluster))
|
2018-07-23 23:25:43 +01:00
|
|
|
|
{
|
2020-07-20 21:11:32 +01:00
|
|
|
|
cluster = new byte[_qHdr.cluster_size];
|
|
|
|
|
|
_imageStream.Seek((long)offset, SeekOrigin.Begin);
|
|
|
|
|
|
_imageStream.Read(cluster, 0, (int)_qHdr.cluster_size);
|
2018-07-23 23:25:43 +01:00
|
|
|
|
|
2020-07-20 21:11:32 +01:00
|
|
|
|
if(_clusterCache.Count >= _maxClusterCache)
|
|
|
|
|
|
_clusterCache.Clear();
|
2018-07-23 23:25:43 +01:00
|
|
|
|
|
2020-07-20 21:11:32 +01:00
|
|
|
|
_clusterCache.Add(offset, cluster);
|
2018-07-23 23:25:43 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2021-09-19 21:16:47 +01:00
|
|
|
|
Array.Copy(cluster, (int)(byteAddress & _sectorMask), buffer, 0, 512);
|
2018-07-23 23:25:43 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2020-07-20 21:11:32 +01:00
|
|
|
|
if(_sectorCache.Count >= MAX_CACHED_SECTORS)
|
|
|
|
|
|
_sectorCache.Clear();
|
2018-07-23 23:25:43 +01:00
|
|
|
|
|
2021-09-19 21:16:47 +01:00
|
|
|
|
_sectorCache.Add(sectorAddress, buffer);
|
2018-07-23 23:25:43 +01:00
|
|
|
|
|
2021-09-19 21:16:47 +01:00
|
|
|
|
return ErrorNumber.NoError;
|
2018-07-23 23:25:43 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2021-08-17 13:56:05 +01:00
|
|
|
|
/// <inheritdoc />
|
2021-09-19 21:16:47 +01:00
|
|
|
|
public ErrorNumber ReadSectors(ulong sectorAddress, uint length, out byte[] buffer)
|
2018-07-23 23:25:43 +01:00
|
|
|
|
{
|
2021-09-19 21:16:47 +01:00
|
|
|
|
buffer = null;
|
|
|
|
|
|
|
2020-07-20 21:11:32 +01:00
|
|
|
|
if(sectorAddress > _imageInfo.Sectors - 1)
|
2021-09-19 21:16:47 +01:00
|
|
|
|
return ErrorNumber.OutOfRange;
|
2018-07-23 23:25:43 +01:00
|
|
|
|
|
2020-07-20 21:11:32 +01:00
|
|
|
|
if(sectorAddress + length > _imageInfo.Sectors)
|
2021-09-19 21:16:47 +01:00
|
|
|
|
return ErrorNumber.OutOfRange;
|
2018-07-23 23:25:43 +01:00
|
|
|
|
|
2020-02-29 18:03:35 +00:00
|
|
|
|
var ms = new MemoryStream();
|
2018-07-23 23:25:43 +01:00
|
|
|
|
|
|
|
|
|
|
for(uint i = 0; i < length; i++)
|
|
|
|
|
|
{
|
2021-09-19 21:16:47 +01:00
|
|
|
|
ErrorNumber errno = ReadSector(sectorAddress + i, out byte[] sector);
|
|
|
|
|
|
|
|
|
|
|
|
if(errno != ErrorNumber.NoError)
|
|
|
|
|
|
return errno;
|
|
|
|
|
|
|
2018-07-23 23:25:43 +01:00
|
|
|
|
ms.Write(sector, 0, sector.Length);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2021-09-19 21:16:47 +01:00
|
|
|
|
buffer = ms.ToArray();
|
|
|
|
|
|
|
|
|
|
|
|
return ErrorNumber.NoError;
|
2018-07-23 23:25:43 +01:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|