2017-05-19 20:28:49 +01:00
|
|
|
// /***************************************************************************
|
2016-08-26 20:07:34 +01:00
|
|
|
// The Disc Image Chef
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
//
|
|
|
|
|
// Filename : QED.cs
|
|
|
|
|
// Author(s) : Natalia Portillo <claunia@claunia.com>
|
|
|
|
|
//
|
2017-12-19 03:50:57 +00:00
|
|
|
// Component : Disk image plugins.
|
2016-08-26 20:07:34 +01:00
|
|
|
//
|
|
|
|
|
// --[ Description ] ----------------------------------------------------------
|
|
|
|
|
//
|
|
|
|
|
// Manages QEMU Enhanced 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/>.
|
|
|
|
|
//
|
|
|
|
|
// ----------------------------------------------------------------------------
|
2017-12-19 03:50:57 +00:00
|
|
|
// Copyright © 2011-2018 Natalia Portillo
|
2016-08-26 20:07:34 +01:00
|
|
|
// ****************************************************************************/
|
|
|
|
|
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Runtime.InteropServices;
|
|
|
|
|
using DiscImageChef.CommonTypes;
|
|
|
|
|
using DiscImageChef.Console;
|
2016-09-05 17:37:31 +01:00
|
|
|
using DiscImageChef.Filters;
|
2016-08-26 20:07:34 +01:00
|
|
|
|
2017-12-20 17:15:26 +00:00
|
|
|
namespace DiscImageChef.DiscImages
|
2016-08-26 20:07:34 +01:00
|
|
|
{
|
2017-12-20 17:15:26 +00:00
|
|
|
public class Qed : ImagePlugin
|
2016-08-26 20:07:34 +01:00
|
|
|
{
|
|
|
|
|
/// <summary>
|
2017-12-24 00:12:31 +00:00
|
|
|
/// Magic number: 'Q', 'E', 'D', 0x00
|
2016-08-26 20:07:34 +01:00
|
|
|
/// </summary>
|
2017-12-20 17:15:26 +00:00
|
|
|
const uint QED_MAGIC = 0x00444551;
|
2016-08-26 20:07:34 +01:00
|
|
|
|
|
|
|
|
/// <summary>
|
2017-12-24 00:12:31 +00:00
|
|
|
/// Mask of unsupported incompatible features
|
2016-08-26 20:07:34 +01:00
|
|
|
/// </summary>
|
2017-12-20 17:15:26 +00:00
|
|
|
const ulong QED_FEATURE_MASK = 0xFFFFFFFFFFFFFFF8;
|
2016-08-26 20:07:34 +01:00
|
|
|
|
|
|
|
|
/// <summary>
|
2017-12-24 00:12:31 +00:00
|
|
|
/// File is differential (has a backing file)
|
2016-08-26 20:07:34 +01:00
|
|
|
/// </summary>
|
2017-12-20 17:15:26 +00:00
|
|
|
const ulong QED_FEATURE_BACKING_FILE = 0x01;
|
2016-08-26 20:07:34 +01:00
|
|
|
/// <summary>
|
2017-12-24 00:12:31 +00:00
|
|
|
/// Image needs a consistency check before writing
|
2016-08-26 20:07:34 +01:00
|
|
|
/// </summary>
|
2017-12-20 17:15:26 +00:00
|
|
|
const ulong QED_FEATURE_NEEDS_CHECK = 0x02;
|
2017-12-24 00:12:31 +00:00
|
|
|
/// <summary>
|
|
|
|
|
/// s
|
|
|
|
|
/// Backing file is a raw disk image
|
2016-08-26 20:07:34 +01:00
|
|
|
/// </summary>
|
2017-12-20 17:15:26 +00:00
|
|
|
const ulong QED_FEATURE_RAW_BACKING = 0x04;
|
2016-08-26 20:07:34 +01:00
|
|
|
|
2017-12-20 17:15:26 +00:00
|
|
|
const int MAX_CACHE_SIZE = 16777216;
|
2016-08-26 20:07:34 +01:00
|
|
|
|
2017-12-24 00:12:31 +00:00
|
|
|
const uint MAX_CACHED_SECTORS = MAX_CACHE_SIZE / 512;
|
|
|
|
|
int clusterBits;
|
|
|
|
|
Dictionary<ulong, byte[]> clusterCache;
|
2016-08-26 20:07:34 +01:00
|
|
|
uint clusterSectors;
|
2017-12-24 00:12:31 +00:00
|
|
|
|
|
|
|
|
Stream imageStream;
|
2016-08-26 20:07:34 +01:00
|
|
|
|
|
|
|
|
ulong l1Mask;
|
|
|
|
|
int l1Shift;
|
2017-12-24 00:12:31 +00:00
|
|
|
ulong[] l1Table;
|
2016-08-26 20:07:34 +01:00
|
|
|
ulong l2Mask;
|
|
|
|
|
Dictionary<ulong, ulong[]> l2TableCache;
|
|
|
|
|
uint maxClusterCache;
|
2017-12-24 00:12:31 +00:00
|
|
|
uint maxL2TableCache;
|
2016-08-26 20:07:34 +01:00
|
|
|
|
2017-12-24 00:12:31 +00:00
|
|
|
QedHeader qHdr;
|
|
|
|
|
|
|
|
|
|
Dictionary<ulong, byte[]> sectorCache;
|
|
|
|
|
ulong sectorMask;
|
|
|
|
|
uint tableSize;
|
2016-08-26 20:07:34 +01:00
|
|
|
|
2017-12-20 17:15:26 +00:00
|
|
|
public Qed()
|
2016-08-26 20:07:34 +01:00
|
|
|
{
|
|
|
|
|
Name = "QEMU Enhanced Disk image";
|
2017-12-20 17:15:26 +00:00
|
|
|
PluginUuid = new Guid("B9DBB155-A69A-4C10-BF91-96BF431B9BB6");
|
2016-08-26 20:07:34 +01:00
|
|
|
ImageInfo = new ImageInfo();
|
2017-12-20 17:15:26 +00:00
|
|
|
ImageInfo.ReadableSectorTags = new List<SectorTagType>();
|
|
|
|
|
ImageInfo.ReadableMediaTags = new List<MediaTagType>();
|
2017-12-26 02:51:10 +00:00
|
|
|
ImageInfo.HasPartitions = false;
|
|
|
|
|
ImageInfo.HasSessions = false;
|
|
|
|
|
ImageInfo.Version = "1";
|
|
|
|
|
ImageInfo.Application = "QEMU";
|
|
|
|
|
ImageInfo.ApplicationVersion = null;
|
|
|
|
|
ImageInfo.Creator = null;
|
|
|
|
|
ImageInfo.Comments = null;
|
2017-12-20 17:15:26 +00:00
|
|
|
ImageInfo.MediaManufacturer = null;
|
|
|
|
|
ImageInfo.MediaModel = null;
|
|
|
|
|
ImageInfo.MediaSerialNumber = null;
|
|
|
|
|
ImageInfo.MediaBarcode = null;
|
|
|
|
|
ImageInfo.MediaPartNumber = null;
|
|
|
|
|
ImageInfo.MediaSequence = 0;
|
|
|
|
|
ImageInfo.LastMediaSequence = 0;
|
|
|
|
|
ImageInfo.DriveManufacturer = null;
|
|
|
|
|
ImageInfo.DriveModel = null;
|
|
|
|
|
ImageInfo.DriveSerialNumber = null;
|
|
|
|
|
ImageInfo.DriveFirmwareRevision = null;
|
2016-08-26 20:07:34 +01:00
|
|
|
}
|
|
|
|
|
|
2017-12-26 02:51:10 +00:00
|
|
|
public override string ImageFormat => "QEMU Enhanced Disk";
|
|
|
|
|
|
|
|
|
|
public override List<Partition> Partitions =>
|
|
|
|
|
throw new FeatureUnsupportedImageException("Feature not supported by image format");
|
|
|
|
|
|
|
|
|
|
public override List<Track> Tracks =>
|
|
|
|
|
throw new FeatureUnsupportedImageException("Feature not supported by image format");
|
|
|
|
|
|
|
|
|
|
public override List<Session> Sessions =>
|
|
|
|
|
throw new FeatureUnsupportedImageException("Feature not supported by image format");
|
|
|
|
|
|
2016-09-05 17:37:31 +01:00
|
|
|
public override bool IdentifyImage(Filter imageFilter)
|
2016-08-26 20:07:34 +01:00
|
|
|
{
|
2016-09-05 17:37:31 +01:00
|
|
|
Stream stream = imageFilter.GetDataForkStream();
|
2016-08-26 20:07:34 +01:00
|
|
|
stream.Seek(0, SeekOrigin.Begin);
|
|
|
|
|
|
2017-12-19 20:33:03 +00:00
|
|
|
if(stream.Length < 512) return false;
|
2016-08-27 01:49:52 +01:00
|
|
|
|
2017-12-20 17:15:26 +00:00
|
|
|
byte[] qHdrB = new byte[64];
|
|
|
|
|
stream.Read(qHdrB, 0, 64);
|
2016-08-26 20:07:34 +01:00
|
|
|
qHdr = new QedHeader();
|
|
|
|
|
IntPtr headerPtr = Marshal.AllocHGlobal(64);
|
2017-12-20 17:15:26 +00:00
|
|
|
Marshal.Copy(qHdrB, 0, headerPtr, 64);
|
2016-08-26 20:07:34 +01:00
|
|
|
qHdr = (QedHeader)Marshal.PtrToStructure(headerPtr, typeof(QedHeader));
|
|
|
|
|
Marshal.FreeHGlobal(headerPtr);
|
|
|
|
|
|
2017-12-20 17:15:26 +00:00
|
|
|
return qHdr.magic == QED_MAGIC;
|
2016-08-26 20:07:34 +01:00
|
|
|
}
|
|
|
|
|
|
2016-09-05 17:37:31 +01:00
|
|
|
public override bool OpenImage(Filter imageFilter)
|
2016-08-26 20:07:34 +01:00
|
|
|
{
|
2016-09-05 17:37:31 +01:00
|
|
|
Stream stream = imageFilter.GetDataForkStream();
|
2016-08-26 20:07:34 +01:00
|
|
|
stream.Seek(0, SeekOrigin.Begin);
|
|
|
|
|
|
2017-12-19 20:33:03 +00:00
|
|
|
if(stream.Length < 512) return false;
|
2016-08-26 20:07:34 +01:00
|
|
|
|
2017-12-20 17:15:26 +00:00
|
|
|
byte[] qHdrB = new byte[64];
|
|
|
|
|
stream.Read(qHdrB, 0, 64);
|
2016-08-26 20:07:34 +01:00
|
|
|
qHdr = new QedHeader();
|
|
|
|
|
IntPtr headerPtr = Marshal.AllocHGlobal(64);
|
2017-12-20 17:15:26 +00:00
|
|
|
Marshal.Copy(qHdrB, 0, headerPtr, 64);
|
2016-08-26 20:07:34 +01:00
|
|
|
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.backing_file_offset = {0}", qHdr.backing_file_offset);
|
|
|
|
|
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");
|
|
|
|
|
|
|
|
|
|
if(!IsPowerOfTwo(qHdr.cluster_size))
|
|
|
|
|
throw new ArgumentOutOfRangeException(nameof(qHdr.cluster_size), "Cluster size must be a power of 2");
|
|
|
|
|
|
|
|
|
|
if(qHdr.cluster_size < 4096 || qHdr.cluster_size > 67108864)
|
2017-12-19 20:33:03 +00:00
|
|
|
throw new ArgumentOutOfRangeException(nameof(qHdr.cluster_size),
|
|
|
|
|
"Cluster size must be between 4 Kbytes and 64 Mbytes");
|
2016-08-27 01:49:52 +01:00
|
|
|
|
2016-08-26 20:07:34 +01:00
|
|
|
if(!IsPowerOfTwo(qHdr.table_size))
|
|
|
|
|
throw new ArgumentOutOfRangeException(nameof(qHdr.table_size), "Table size must be a power of 2");
|
|
|
|
|
|
|
|
|
|
if(qHdr.table_size < 1 || qHdr.table_size > 16)
|
2017-12-19 20:33:03 +00:00
|
|
|
throw new ArgumentOutOfRangeException(nameof(qHdr.table_size),
|
|
|
|
|
"Table size must be between 1 and 16 clusters");
|
2016-08-26 20:07:34 +01:00
|
|
|
|
2017-12-20 17:15:26 +00:00
|
|
|
if((qHdr.features & QED_FEATURE_MASK) > 0)
|
2017-12-19 20:33:03 +00:00
|
|
|
throw new ArgumentOutOfRangeException(nameof(qHdr.features),
|
2017-12-21 17:58:51 +00:00
|
|
|
$"Image uses unknown incompatible features {qHdr.features & QED_FEATURE_MASK:X}");
|
2016-08-26 20:07:34 +01:00
|
|
|
|
2017-12-20 17:15:26 +00:00
|
|
|
if((qHdr.features & QED_FEATURE_BACKING_FILE) == QED_FEATURE_BACKING_FILE)
|
2016-08-26 20:07:34 +01:00
|
|
|
throw new NotImplementedException("Differencing images not yet supported");
|
|
|
|
|
|
|
|
|
|
clusterSectors = qHdr.cluster_size / 512;
|
2017-12-20 17:26:28 +00:00
|
|
|
tableSize = qHdr.cluster_size * qHdr.table_size / 8;
|
2016-08-26 20:07:34 +01:00
|
|
|
|
|
|
|
|
DicConsole.DebugWriteLine("QED plugin", "qHdr.clusterSectors = {0}", clusterSectors);
|
|
|
|
|
DicConsole.DebugWriteLine("QED plugin", "qHdr.tableSize = {0}", tableSize);
|
|
|
|
|
|
2017-12-20 17:15:26 +00:00
|
|
|
byte[] l1TableB = new byte[tableSize * 8];
|
2016-08-26 20:07:34 +01:00
|
|
|
stream.Seek((long)qHdr.l1_table_offset, SeekOrigin.Begin);
|
2017-12-20 17:15:26 +00:00
|
|
|
stream.Read(l1TableB, 0, (int)tableSize * 8);
|
2016-09-13 20:47:07 +01:00
|
|
|
l1Table = new ulong[tableSize];
|
2016-08-26 20:07:34 +01:00
|
|
|
DicConsole.DebugWriteLine("QED plugin", "Reading L1 table");
|
2017-12-20 17:15:26 +00:00
|
|
|
for(long i = 0; i < l1Table.LongLength; i++) l1Table[i] = BitConverter.ToUInt64(l1TableB, (int)(i * 8));
|
2016-08-26 20:07:34 +01:00
|
|
|
|
|
|
|
|
l1Mask = 0;
|
|
|
|
|
int c = 0;
|
2017-12-20 17:15:26 +00:00
|
|
|
clusterBits = Ctz32(qHdr.cluster_size);
|
2017-12-19 20:33:03 +00:00
|
|
|
l2Mask = (tableSize - 1) << clusterBits;
|
2017-12-20 17:15:26 +00:00
|
|
|
l1Shift = clusterBits + Ctz32(tableSize);
|
2016-08-26 20:07:34 +01:00
|
|
|
|
|
|
|
|
for(int i = 0; i < 64; i++)
|
|
|
|
|
{
|
|
|
|
|
l1Mask <<= 1;
|
|
|
|
|
|
2017-12-21 06:06:19 +00:00
|
|
|
if(c >= 64 - l1Shift) continue;
|
|
|
|
|
|
|
|
|
|
l1Mask += 1;
|
|
|
|
|
c++;
|
2016-08-26 20:07:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
sectorMask = 0;
|
2017-12-19 20:33:03 +00:00
|
|
|
for(int i = 0; i < clusterBits; i++) sectorMask = (sectorMask << 1) + 1;
|
2016-08-26 20:07:34 +01:00
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
|
2017-12-20 17:26:28 +00:00
|
|
|
maxL2TableCache = MAX_CACHE_SIZE / tableSize;
|
2017-12-20 17:15:26 +00:00
|
|
|
maxClusterCache = MAX_CACHE_SIZE / qHdr.cluster_size;
|
2016-08-26 20:07:34 +01:00
|
|
|
|
|
|
|
|
imageStream = stream;
|
|
|
|
|
|
|
|
|
|
sectorCache = new Dictionary<ulong, byte[]>();
|
|
|
|
|
l2TableCache = new Dictionary<ulong, ulong[]>();
|
|
|
|
|
clusterCache = new Dictionary<ulong, byte[]>();
|
|
|
|
|
|
2017-12-26 02:51:10 +00:00
|
|
|
ImageInfo.CreationTime = imageFilter.GetCreationTime();
|
|
|
|
|
ImageInfo.LastModificationTime = imageFilter.GetLastWriteTime();
|
|
|
|
|
ImageInfo.MediaTitle = Path.GetFileNameWithoutExtension(imageFilter.GetFilename());
|
2017-12-20 17:15:26 +00:00
|
|
|
ImageInfo.Sectors = qHdr.image_size / 512;
|
|
|
|
|
ImageInfo.SectorSize = 512;
|
|
|
|
|
ImageInfo.XmlMediaType = XmlMediaType.BlockMedia;
|
|
|
|
|
ImageInfo.MediaType = MediaType.GENERIC_HDD;
|
|
|
|
|
ImageInfo.ImageSize = qHdr.image_size;
|
2016-08-26 20:07:34 +01:00
|
|
|
|
2017-12-20 17:26:28 +00:00
|
|
|
ImageInfo.Cylinders = (uint)(ImageInfo.Sectors / 16 / 63);
|
2017-12-20 17:15:26 +00:00
|
|
|
ImageInfo.Heads = 16;
|
|
|
|
|
ImageInfo.SectorsPerTrack = 63;
|
2017-08-02 23:01:11 +01:00
|
|
|
|
2017-12-19 20:33:03 +00:00
|
|
|
return true;
|
2016-08-26 20:07:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override byte[] ReadSector(ulong sectorAddress)
|
|
|
|
|
{
|
2017-12-20 17:15:26 +00:00
|
|
|
if(sectorAddress > ImageInfo.Sectors - 1)
|
2017-12-19 20:33:03 +00:00
|
|
|
throw new ArgumentOutOfRangeException(nameof(sectorAddress),
|
2017-12-21 17:58:51 +00:00
|
|
|
$"Sector address {sectorAddress} not found");
|
2016-08-26 20:07:34 +01:00
|
|
|
|
|
|
|
|
// Check cache
|
2017-12-22 06:55:04 +00:00
|
|
|
if(sectorCache.TryGetValue(sectorAddress, out byte[] sector)) return sector;
|
2016-08-26 20:07:34 +01:00
|
|
|
|
|
|
|
|
ulong byteAddress = sectorAddress * 512;
|
|
|
|
|
|
|
|
|
|
ulong l1Off = (byteAddress & l1Mask) >> l1Shift;
|
|
|
|
|
|
|
|
|
|
if((long)l1Off >= l1Table.LongLength)
|
2017-12-19 20:33:03 +00:00
|
|
|
throw new ArgumentOutOfRangeException(nameof(l1Off),
|
2017-12-21 17:58:51 +00:00
|
|
|
$"Trying to read past L1 table, position {l1Off} of a max {l1Table.LongLength}");
|
2016-08-26 20:07:34 +01:00
|
|
|
|
|
|
|
|
// TODO: Implement differential images
|
2017-12-19 20:33:03 +00:00
|
|
|
if(l1Table[l1Off] == 0) return new byte[512];
|
2016-08-26 20:07:34 +01:00
|
|
|
|
2017-12-22 06:55:04 +00:00
|
|
|
if(!l2TableCache.TryGetValue(l1Off, out ulong[] l2Table))
|
2016-08-26 20:07:34 +01:00
|
|
|
{
|
2016-09-13 20:47:07 +01:00
|
|
|
l2Table = new ulong[tableSize];
|
2016-08-26 20:07:34 +01:00
|
|
|
imageStream.Seek((long)l1Table[l1Off], SeekOrigin.Begin);
|
2017-12-20 17:15:26 +00:00
|
|
|
byte[] l2TableB = new byte[tableSize * 8];
|
|
|
|
|
imageStream.Read(l2TableB, 0, (int)tableSize * 8);
|
2016-08-26 20:07:34 +01:00
|
|
|
DicConsole.DebugWriteLine("QED plugin", "Reading L2 table #{0}", l1Off);
|
2017-12-24 00:12:31 +00:00
|
|
|
for(long i = 0; i < l2Table.LongLength; i++) l2Table[i] = BitConverter.ToUInt64(l2TableB, (int)(i * 8));
|
2016-08-26 20:07:34 +01:00
|
|
|
|
2017-12-19 20:33:03 +00:00
|
|
|
if(l2TableCache.Count >= maxL2TableCache) l2TableCache.Clear();
|
2016-08-26 20:07:34 +01:00
|
|
|
|
|
|
|
|
l2TableCache.Add(l1Off, l2Table);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ulong l2Off = (byteAddress & l2Mask) >> clusterBits;
|
|
|
|
|
|
2017-12-19 20:33:03 +00:00
|
|
|
ulong offset = l2Table[l2Off];
|
2016-08-26 20:07:34 +01:00
|
|
|
|
|
|
|
|
sector = new byte[512];
|
|
|
|
|
|
|
|
|
|
if(offset != 0 && offset != 1)
|
|
|
|
|
{
|
2017-12-22 06:55:04 +00:00
|
|
|
if(!clusterCache.TryGetValue(offset, out byte[] cluster))
|
2016-08-26 20:07:34 +01:00
|
|
|
{
|
|
|
|
|
cluster = new byte[qHdr.cluster_size];
|
|
|
|
|
imageStream.Seek((long)offset, SeekOrigin.Begin);
|
|
|
|
|
imageStream.Read(cluster, 0, (int)qHdr.cluster_size);
|
|
|
|
|
|
2017-12-19 20:33:03 +00:00
|
|
|
if(clusterCache.Count >= maxClusterCache) clusterCache.Clear();
|
2016-08-26 20:07:34 +01:00
|
|
|
|
|
|
|
|
clusterCache.Add(offset, cluster);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Array.Copy(cluster, (int)(byteAddress & sectorMask), sector, 0, 512);
|
|
|
|
|
}
|
|
|
|
|
|
2017-12-22 06:55:04 +00:00
|
|
|
if(sectorCache.Count >= MAX_CACHED_SECTORS) sectorCache.Clear();
|
2016-08-26 20:07:34 +01:00
|
|
|
|
|
|
|
|
sectorCache.Add(sectorAddress, sector);
|
|
|
|
|
|
|
|
|
|
return sector;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override byte[] ReadSectors(ulong sectorAddress, uint length)
|
|
|
|
|
{
|
2017-12-20 17:15:26 +00:00
|
|
|
if(sectorAddress > ImageInfo.Sectors - 1)
|
2017-12-19 20:33:03 +00:00
|
|
|
throw new ArgumentOutOfRangeException(nameof(sectorAddress),
|
2017-12-21 17:58:51 +00:00
|
|
|
$"Sector address {sectorAddress} not found");
|
2016-08-26 20:07:34 +01:00
|
|
|
|
2017-12-20 17:15:26 +00:00
|
|
|
if(sectorAddress + length > ImageInfo.Sectors)
|
2016-08-26 20:07:34 +01:00
|
|
|
throw new ArgumentOutOfRangeException(nameof(length), "Requested more sectors than available");
|
|
|
|
|
|
|
|
|
|
MemoryStream ms = new MemoryStream();
|
|
|
|
|
|
|
|
|
|
for(uint i = 0; i < length; i++)
|
|
|
|
|
{
|
|
|
|
|
byte[] sector = ReadSector(sectorAddress + i);
|
|
|
|
|
ms.Write(sector, 0, sector.Length);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return ms.ToArray();
|
|
|
|
|
}
|
|
|
|
|
|
2017-12-24 00:12:31 +00:00
|
|
|
static bool IsPowerOfTwo(uint x)
|
2016-08-26 20:07:34 +01:00
|
|
|
{
|
2017-12-20 17:26:28 +00:00
|
|
|
while((x & 1) == 0 && x > 1) x >>= 1;
|
2017-12-19 20:33:03 +00:00
|
|
|
|
2017-12-20 17:26:28 +00:00
|
|
|
return x == 1;
|
2016-08-26 20:07:34 +01:00
|
|
|
}
|
|
|
|
|
|
2017-12-20 17:15:26 +00:00
|
|
|
static int Ctz32(uint val)
|
2016-08-26 20:07:34 +01:00
|
|
|
{
|
|
|
|
|
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;
|
|
|
|
|
}
|
2017-12-20 23:07:46 +00:00
|
|
|
if((val & 0x1) == 0) cnt++;
|
2016-08-26 20:07:34 +01:00
|
|
|
|
|
|
|
|
return cnt;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override byte[] ReadSectorTag(ulong sectorAddress, SectorTagType tag)
|
|
|
|
|
{
|
|
|
|
|
throw new FeatureUnsupportedImageException("Feature not supported by image format");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override byte[] ReadSectorsTag(ulong sectorAddress, uint length, SectorTagType tag)
|
|
|
|
|
{
|
|
|
|
|
throw new FeatureUnsupportedImageException("Feature not supported by image format");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override byte[] ReadDiskTag(MediaTagType tag)
|
|
|
|
|
{
|
|
|
|
|
throw new FeatureUnsupportedImageException("Feature not supported by image format");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override byte[] ReadSector(ulong sectorAddress, uint track)
|
|
|
|
|
{
|
|
|
|
|
throw new FeatureUnsupportedImageException("Feature not supported by image format");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override byte[] ReadSectorTag(ulong sectorAddress, uint track, SectorTagType tag)
|
|
|
|
|
{
|
|
|
|
|
throw new FeatureUnsupportedImageException("Feature not supported by image format");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override byte[] ReadSectors(ulong sectorAddress, uint length, uint track)
|
|
|
|
|
{
|
|
|
|
|
throw new FeatureUnsupportedImageException("Feature not supported by image format");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override byte[] ReadSectorsTag(ulong sectorAddress, uint length, uint track, SectorTagType tag)
|
|
|
|
|
{
|
|
|
|
|
throw new FeatureUnsupportedImageException("Feature not supported by image format");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override byte[] ReadSectorLong(ulong sectorAddress)
|
|
|
|
|
{
|
|
|
|
|
throw new FeatureUnsupportedImageException("Feature not supported by image format");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override byte[] ReadSectorLong(ulong sectorAddress, uint track)
|
|
|
|
|
{
|
|
|
|
|
throw new FeatureUnsupportedImageException("Feature not supported by image format");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override byte[] ReadSectorsLong(ulong sectorAddress, uint length)
|
|
|
|
|
{
|
|
|
|
|
throw new FeatureUnsupportedImageException("Feature not supported by image format");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override byte[] ReadSectorsLong(ulong sectorAddress, uint length, uint track)
|
|
|
|
|
{
|
|
|
|
|
throw new FeatureUnsupportedImageException("Feature not supported by image format");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override List<Track> GetSessionTracks(Session session)
|
|
|
|
|
{
|
|
|
|
|
throw new FeatureUnsupportedImageException("Feature not supported by image format");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override List<Track> GetSessionTracks(ushort session)
|
|
|
|
|
{
|
|
|
|
|
throw new FeatureUnsupportedImageException("Feature not supported by image format");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override bool? VerifySector(ulong sectorAddress)
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override bool? VerifySector(ulong sectorAddress, uint track)
|
|
|
|
|
{
|
|
|
|
|
throw new FeatureUnsupportedImageException("Feature not supported by image format");
|
|
|
|
|
}
|
|
|
|
|
|
2017-12-20 17:15:26 +00:00
|
|
|
public override bool? VerifySectors(ulong sectorAddress, uint length, out List<ulong> failingLbas,
|
|
|
|
|
out List<ulong> unknownLbas)
|
2016-08-26 20:07:34 +01:00
|
|
|
{
|
2017-12-20 17:15:26 +00:00
|
|
|
failingLbas = new List<ulong>();
|
|
|
|
|
unknownLbas = new List<ulong>();
|
|
|
|
|
for(ulong i = 0; i < ImageInfo.Sectors; i++) unknownLbas.Add(i);
|
2017-12-19 20:33:03 +00:00
|
|
|
|
2016-08-26 20:07:34 +01:00
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
2017-12-20 17:15:26 +00:00
|
|
|
public override bool? VerifySectors(ulong sectorAddress, uint length, uint track, out List<ulong> failingLbas,
|
|
|
|
|
out List<ulong> unknownLbas)
|
2016-08-26 20:07:34 +01:00
|
|
|
{
|
|
|
|
|
throw new FeatureUnsupportedImageException("Feature not supported by image format");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override bool? VerifyMediaImage()
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
2017-12-24 00:12:31 +00:00
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// QED header, big-endian
|
|
|
|
|
/// </summary>
|
|
|
|
|
[StructLayout(LayoutKind.Sequential, Pack = 1)]
|
|
|
|
|
struct QedHeader
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// <see cref="Qed.QED_MAGIC" />
|
|
|
|
|
/// </summary>
|
|
|
|
|
public uint magic;
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Cluster size in bytes
|
|
|
|
|
/// </summary>
|
|
|
|
|
public uint cluster_size;
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// L1 and L2 table size in cluster
|
|
|
|
|
/// </summary>
|
|
|
|
|
public uint table_size;
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Header size in clusters
|
|
|
|
|
/// </summary>
|
|
|
|
|
public uint header_size;
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Incompatible features
|
|
|
|
|
/// </summary>
|
|
|
|
|
public ulong features;
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Compatible features
|
|
|
|
|
/// </summary>
|
|
|
|
|
public ulong compat_features;
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Self-resetting features
|
|
|
|
|
/// </summary>
|
|
|
|
|
public ulong autoclear_features;
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Offset to L1 table
|
|
|
|
|
/// </summary>
|
|
|
|
|
public ulong l1_table_offset;
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Image size
|
|
|
|
|
/// </summary>
|
|
|
|
|
public ulong image_size;
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Offset inside file to string containing backing file
|
|
|
|
|
/// </summary>
|
|
|
|
|
public ulong backing_file_offset;
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Size of <see cref="backing_file_offset" />
|
|
|
|
|
/// </summary>
|
|
|
|
|
public uint backing_file_size;
|
|
|
|
|
}
|
2016-08-26 20:07:34 +01:00
|
|
|
}
|
2017-12-19 20:33:03 +00:00
|
|
|
}
|