Files
Aaru/Aaru.Images/SuperCardPro/Read.cs

303 lines
14 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 SuperCardPro flux 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/>.
//
// ----------------------------------------------------------------------------
2020-12-31 23:08:23 +00:00
// Copyright © 2011-2021 Natalia Portillo
// ****************************************************************************/
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
2020-02-27 00:33:26 +00:00
using Aaru.CommonTypes.Enums;
using Aaru.CommonTypes.Interfaces;
using Aaru.Console;
using Aaru.Helpers;
2020-02-27 00:33:26 +00:00
namespace Aaru.DiscImages
{
2020-07-22 13:20:25 +01:00
public sealed partial class SuperCardPro
{
2021-08-17 13:56:05 +01:00
/// <inheritdoc />
public ErrorNumber Open(IFilter imageFilter)
{
2020-07-20 21:11:32 +01:00
Header = new ScpHeader();
_scpStream = imageFilter.GetDataForkStream();
_scpStream.Seek(0, SeekOrigin.Begin);
2020-02-29 18:03:35 +00:00
2020-07-20 21:11:32 +01:00
if(_scpStream.Length < Marshal.SizeOf<ScpHeader>())
return ErrorNumber.InvalidArgument;
byte[] hdr = new byte[Marshal.SizeOf<ScpHeader>()];
2020-07-20 21:11:32 +01:00
_scpStream.Read(hdr, 0, Marshal.SizeOf<ScpHeader>());
Header = Marshal.ByteArrayToStructureLittleEndian<ScpHeader>(hdr);
2020-02-27 23:48:41 +00:00
AaruConsole.DebugWriteLine("SuperCardPro plugin", "header.signature = \"{0}\"",
2020-02-29 18:03:35 +00:00
StringHandlers.CToString(Header.signature));
2020-02-27 23:48:41 +00:00
AaruConsole.DebugWriteLine("SuperCardPro plugin", "header.version = {0}.{1}", (Header.version & 0xF0) >> 4,
2020-02-29 18:03:35 +00:00
Header.version & 0xF);
AaruConsole.DebugWriteLine("SuperCardPro plugin", "header.type = {0}", Header.type);
AaruConsole.DebugWriteLine("SuperCardPro plugin", "header.revolutions = {0}", Header.revolutions);
AaruConsole.DebugWriteLine("SuperCardPro plugin", "header.start = {0}", Header.start);
AaruConsole.DebugWriteLine("SuperCardPro plugin", "header.end = {0}", Header.end);
AaruConsole.DebugWriteLine("SuperCardPro plugin", "header.flags = {0}", Header.flags);
2020-02-27 23:48:41 +00:00
AaruConsole.DebugWriteLine("SuperCardPro plugin", "header.bitCellEncoding = {0}", Header.bitCellEncoding);
2020-02-29 18:03:35 +00:00
AaruConsole.DebugWriteLine("SuperCardPro plugin", "header.heads = {0}", Header.heads);
AaruConsole.DebugWriteLine("SuperCardPro plugin", "header.reserved = {0}", Header.reserved);
AaruConsole.DebugWriteLine("SuperCardPro plugin", "header.checksum = 0x{0:X8}", Header.checksum);
2020-07-20 21:11:32 +01:00
if(!_scpSignature.SequenceEqual(Header.signature))
return ErrorNumber.InvalidArgument;
ScpTracks = new Dictionary<byte, TrackHeader>();
for(byte t = Header.start; t <= Header.end; t++)
{
2020-02-29 18:03:35 +00:00
if(t >= Header.offsets.Length)
break;
2020-07-20 21:11:32 +01:00
_scpStream.Position = Header.offsets[t];
2020-02-29 18:03:35 +00:00
var trk = new TrackHeader
{
2020-07-20 04:34:16 +01:00
Signature = new byte[3],
Entries = new TrackEntry[Header.revolutions]
2020-02-29 18:03:35 +00:00
};
2020-07-20 21:11:32 +01:00
_scpStream.Read(trk.Signature, 0, trk.Signature.Length);
trk.TrackNumber = (byte)_scpStream.ReadByte();
2020-07-20 21:11:32 +01:00
if(!trk.Signature.SequenceEqual(_trkSignature))
{
2020-02-27 23:48:41 +00:00
AaruConsole.DebugWriteLine("SuperCardPro plugin",
2020-02-29 18:03:35 +00:00
"Track header at {0} contains incorrect signature.", Header.offsets[t]);
continue;
}
if(trk.TrackNumber != t)
{
2020-02-27 23:48:41 +00:00
AaruConsole.DebugWriteLine("SuperCardPro plugin", "Track number at {0} should be {1} but is {2}.",
2020-02-29 18:03:35 +00:00
Header.offsets[t], t, trk.TrackNumber);
continue;
}
2020-02-27 23:48:41 +00:00
AaruConsole.DebugWriteLine("SuperCardPro plugin", "Found track {0} at {1}.", t, Header.offsets[t]);
for(byte r = 0; r < Header.revolutions; r++)
{
byte[] rev = new byte[Marshal.SizeOf<TrackEntry>()];
2020-07-20 21:11:32 +01:00
_scpStream.Read(rev, 0, Marshal.SizeOf<TrackEntry>());
trk.Entries[r] = Marshal.ByteArrayToStructureLittleEndian<TrackEntry>(rev);
2020-02-29 18:03:35 +00:00
// De-relative offsets
trk.Entries[r].dataOffset += Header.offsets[t];
}
ScpTracks.Add(t, trk);
}
if(Header.flags.HasFlag(ScpFlags.HasFooter))
{
2020-07-20 21:11:32 +01:00
long position = _scpStream.Position;
_scpStream.Seek(-4, SeekOrigin.End);
2017-12-19 20:33:03 +00:00
2020-07-20 21:11:32 +01:00
while(_scpStream.Position >= position)
{
byte[] footerSig = new byte[4];
2020-07-20 21:11:32 +01:00
_scpStream.Read(footerSig, 0, 4);
uint footerMagic = BitConverter.ToUInt32(footerSig, 0);
if(footerMagic == FOOTER_SIGNATURE)
{
_scpStream.Seek(-Marshal.SizeOf<Footer>(), SeekOrigin.Current);
2017-12-19 20:33:03 +00:00
2020-07-20 21:11:32 +01:00
AaruConsole.DebugWriteLine("SuperCardPro plugin", "Found footer at {0}", _scpStream.Position);
2017-12-19 20:33:03 +00:00
byte[] ftr = new byte[Marshal.SizeOf<Footer>()];
_scpStream.Read(ftr, 0, Marshal.SizeOf<Footer>());
Footer footer = Marshal.ByteArrayToStructureLittleEndian<Footer>(ftr);
2020-02-27 23:48:41 +00:00
AaruConsole.DebugWriteLine("SuperCardPro plugin", "footer.manufacturerOffset = 0x{0:X8}",
2020-02-29 18:03:35 +00:00
footer.manufacturerOffset);
2020-02-27 23:48:41 +00:00
AaruConsole.DebugWriteLine("SuperCardPro plugin", "footer.modelOffset = 0x{0:X8}",
2020-02-29 18:03:35 +00:00
footer.modelOffset);
2020-02-27 23:48:41 +00:00
AaruConsole.DebugWriteLine("SuperCardPro plugin", "footer.serialOffset = 0x{0:X8}",
2020-02-29 18:03:35 +00:00
footer.serialOffset);
2020-02-27 23:48:41 +00:00
AaruConsole.DebugWriteLine("SuperCardPro plugin", "footer.creatorOffset = 0x{0:X8}",
2020-02-29 18:03:35 +00:00
footer.creatorOffset);
2020-02-27 23:48:41 +00:00
AaruConsole.DebugWriteLine("SuperCardPro plugin", "footer.applicationOffset = 0x{0:X8}",
2020-02-29 18:03:35 +00:00
footer.applicationOffset);
2020-02-27 23:48:41 +00:00
AaruConsole.DebugWriteLine("SuperCardPro plugin", "footer.commentsOffset = 0x{0:X8}",
2020-02-29 18:03:35 +00:00
footer.commentsOffset);
2020-02-27 23:48:41 +00:00
AaruConsole.DebugWriteLine("SuperCardPro plugin", "footer.creationTime = {0}",
2020-02-29 18:03:35 +00:00
footer.creationTime);
2020-02-27 23:48:41 +00:00
AaruConsole.DebugWriteLine("SuperCardPro plugin", "footer.modificationTime = {0}",
2020-02-29 18:03:35 +00:00
footer.modificationTime);
2020-02-27 23:48:41 +00:00
AaruConsole.DebugWriteLine("SuperCardPro plugin", "footer.applicationVersion = {0}.{1}",
2020-02-29 18:03:35 +00:00
(footer.applicationVersion & 0xF0) >> 4,
footer.applicationVersion & 0xF);
2020-02-27 23:48:41 +00:00
AaruConsole.DebugWriteLine("SuperCardPro plugin", "footer.hardwareVersion = {0}.{1}",
2020-02-29 18:03:35 +00:00
(footer.hardwareVersion & 0xF0) >> 4, footer.hardwareVersion & 0xF);
2020-02-27 23:48:41 +00:00
AaruConsole.DebugWriteLine("SuperCardPro plugin", "footer.firmwareVersion = {0}.{1}",
2020-02-29 18:03:35 +00:00
(footer.firmwareVersion & 0xF0) >> 4, footer.firmwareVersion & 0xF);
2020-02-27 23:48:41 +00:00
AaruConsole.DebugWriteLine("SuperCardPro plugin", "footer.imageVersion = {0}.{1}",
2020-02-29 18:03:35 +00:00
(footer.imageVersion & 0xF0) >> 4, footer.imageVersion & 0xF);
2020-02-27 23:48:41 +00:00
AaruConsole.DebugWriteLine("SuperCardPro plugin", "footer.signature = \"{0}\"",
2020-02-29 18:03:35 +00:00
StringHandlers.CToString(BitConverter.GetBytes(footer.signature)));
2020-07-20 21:11:32 +01:00
_imageInfo.DriveManufacturer = ReadPStringUtf8(_scpStream, footer.manufacturerOffset);
_imageInfo.DriveModel = ReadPStringUtf8(_scpStream, footer.modelOffset);
_imageInfo.DriveSerialNumber = ReadPStringUtf8(_scpStream, footer.serialOffset);
_imageInfo.Creator = ReadPStringUtf8(_scpStream, footer.creatorOffset);
_imageInfo.Application = ReadPStringUtf8(_scpStream, footer.applicationOffset);
_imageInfo.Comments = ReadPStringUtf8(_scpStream, footer.commentsOffset);
2020-02-27 23:48:41 +00:00
AaruConsole.DebugWriteLine("SuperCardPro plugin", "ImageInfo.driveManufacturer = \"{0}\"",
2020-07-20 21:11:32 +01:00
_imageInfo.DriveManufacturer);
2020-02-29 18:03:35 +00:00
2020-02-27 23:48:41 +00:00
AaruConsole.DebugWriteLine("SuperCardPro plugin", "ImageInfo.driveModel = \"{0}\"",
2020-07-20 21:11:32 +01:00
_imageInfo.DriveModel);
2020-02-29 18:03:35 +00:00
2020-02-27 23:48:41 +00:00
AaruConsole.DebugWriteLine("SuperCardPro plugin", "ImageInfo.driveSerialNumber = \"{0}\"",
2020-07-20 21:11:32 +01:00
_imageInfo.DriveSerialNumber);
2020-02-29 18:03:35 +00:00
2020-02-27 23:48:41 +00:00
AaruConsole.DebugWriteLine("SuperCardPro plugin", "ImageInfo.imageCreator = \"{0}\"",
2020-07-20 21:11:32 +01:00
_imageInfo.Creator);
2020-02-29 18:03:35 +00:00
2020-02-27 23:48:41 +00:00
AaruConsole.DebugWriteLine("SuperCardPro plugin", "ImageInfo.imageApplication = \"{0}\"",
2020-07-20 21:11:32 +01:00
_imageInfo.Application);
2020-02-29 18:03:35 +00:00
2020-02-27 23:48:41 +00:00
AaruConsole.DebugWriteLine("SuperCardPro plugin", "ImageInfo.imageComments = \"{0}\"",
2020-07-20 21:11:32 +01:00
_imageInfo.Comments);
2020-07-20 21:11:32 +01:00
_imageInfo.CreationTime = footer.creationTime != 0
? DateHandlers.UnixToDateTime(footer.creationTime)
: imageFilter.CreationTime;
2020-07-20 21:11:32 +01:00
_imageInfo.LastModificationTime = footer.modificationTime != 0
? DateHandlers.UnixToDateTime(footer.modificationTime)
: imageFilter.LastWriteTime;
2020-02-27 23:48:41 +00:00
AaruConsole.DebugWriteLine("SuperCardPro plugin", "ImageInfo.imageCreationTime = {0}",
2020-07-20 21:11:32 +01:00
_imageInfo.CreationTime);
2020-02-29 18:03:35 +00:00
2020-02-27 23:48:41 +00:00
AaruConsole.DebugWriteLine("SuperCardPro plugin", "ImageInfo.imageLastModificationTime = {0}",
2020-07-20 21:11:32 +01:00
_imageInfo.LastModificationTime);
2020-07-20 21:11:32 +01:00
_imageInfo.ApplicationVersion =
$"{(footer.applicationVersion & 0xF0) >> 4}.{footer.applicationVersion & 0xF}";
2020-02-29 18:03:35 +00:00
2020-07-20 21:11:32 +01:00
_imageInfo.DriveFirmwareRevision =
2018-06-22 08:08:38 +01:00
$"{(footer.firmwareVersion & 0xF0) >> 4}.{footer.firmwareVersion & 0xF}";
2020-02-29 18:03:35 +00:00
2020-07-20 21:11:32 +01:00
_imageInfo.Version = $"{(footer.imageVersion & 0xF0) >> 4}.{footer.imageVersion & 0xF}";
2017-12-19 20:33:03 +00:00
break;
}
2017-12-19 20:33:03 +00:00
2020-07-20 21:11:32 +01:00
_scpStream.Seek(-8, SeekOrigin.Current);
}
}
else
{
2020-07-20 21:11:32 +01:00
_imageInfo.Application = "SuperCardPro";
_imageInfo.ApplicationVersion = $"{(Header.version & 0xF0) >> 4}.{Header.version & 0xF}";
_imageInfo.CreationTime = imageFilter.CreationTime;
_imageInfo.LastModificationTime = imageFilter.LastWriteTime;
2020-07-20 21:11:32 +01:00
_imageInfo.Version = "1.5";
}
AaruConsole.ErrorWriteLine("Flux decoding is not yet implemented.");
return ErrorNumber.NotImplemented;
}
2021-08-17 13:56:05 +01:00
/// <inheritdoc />
public ErrorNumber ReadMediaTag(MediaTagType tag, out byte[] buffer)
{
buffer = null;
return ErrorNumber.NotImplemented;
}
2021-08-17 13:56:05 +01:00
/// <inheritdoc />
public ErrorNumber ReadSector(ulong sectorAddress, out byte[] buffer) =>
ReadSectors(sectorAddress, 1, out buffer);
2021-08-17 13:56:05 +01:00
/// <inheritdoc />
public ErrorNumber ReadSectorTag(ulong sectorAddress, SectorTagType tag, out byte[] buffer) =>
ReadSectorsTag(sectorAddress, 1, tag, out buffer);
2021-08-17 13:56:05 +01:00
/// <inheritdoc />
public ErrorNumber ReadSectors(ulong sectorAddress, uint length, out byte[] buffer)
{
buffer = null;
return ErrorNumber.NotImplemented;
}
2021-08-17 13:56:05 +01:00
/// <inheritdoc />
public ErrorNumber ReadSectorsTag(ulong sectorAddress, uint length, SectorTagType tag, out byte[] buffer)
{
buffer = null;
return ErrorNumber.NotImplemented;
}
2021-08-17 13:56:05 +01:00
/// <inheritdoc />
public ErrorNumber ReadSectorLong(ulong sectorAddress, out byte[] buffer) =>
ReadSectorsLong(sectorAddress, 1, out buffer);
2021-08-17 13:56:05 +01:00
/// <inheritdoc />
public ErrorNumber ReadSectorsLong(ulong sectorAddress, uint length, out byte[] buffer)
{
buffer = null;
return ErrorNumber.NotSupported;
}
}
}