Files
Aaru/Aaru.Images/CPCDSK/Read.cs

358 lines
15 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 CPCEMU 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.Linq;
using System.Text;
2020-02-27 00:33:26 +00:00
using Aaru.Checksums;
using Aaru.CommonTypes;
using Aaru.CommonTypes.Enums;
using Aaru.CommonTypes.Interfaces;
using Aaru.Console;
using Aaru.Decoders.Floppy;
using Aaru.Helpers;
2022-03-06 13:29:38 +00:00
public sealed partial class Cpcdsk
{
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);
2022-03-06 13:29:38 +00:00
if(stream.Length < 512)
return ErrorNumber.InvalidArgument;
2022-03-07 07:36:44 +00:00
var headerB = new byte[256];
2022-03-06 13:29:38 +00:00
stream.Read(headerB, 0, 256);
2022-03-06 13:29:38 +00:00
int pos;
2022-03-06 13:29:38 +00:00
for(pos = 0; pos < 254; pos++)
if(headerB[pos] == 0x0D &&
headerB[pos + 1] == 0x0A)
break;
2022-03-06 13:29:38 +00:00
if(pos >= 254)
return ErrorNumber.InvalidArgument;
2022-03-06 13:29:38 +00:00
string magic = Encoding.ASCII.GetString(headerB, 0, pos);
2022-03-06 13:29:38 +00:00
stream.Position = pos + 2;
stream.Read(headerB, 0, 256);
2022-03-06 13:29:38 +00:00
DiskInfo header = Marshal.ByteArrayToStructureLittleEndian<DiskInfo>(headerB);
2022-03-06 13:29:38 +00:00
if(string.Compare(CPCDSK_ID, magic, StringComparison.InvariantCultureIgnoreCase) != 0 &&
string.Compare(EDSK_ID, magic, StringComparison.InvariantCultureIgnoreCase) != 0 &&
string.Compare(DU54_ID, magic, StringComparison.InvariantCultureIgnoreCase) != 0)
return ErrorNumber.InvalidArgument;
2022-03-06 13:29:38 +00:00
_extended = string.Compare(EDSK_ID, magic, StringComparison.InvariantCultureIgnoreCase) == 0;
AaruConsole.DebugWriteLine("CPCDSK plugin", "Extended = {0}", _extended);
2022-03-06 13:29:38 +00:00
AaruConsole.DebugWriteLine("CPCDSK plugin", "magic = \"{0}\"", magic);
2022-03-07 07:36:44 +00:00
AaruConsole.DebugWriteLine("CPCDSK plugin", "header.magic = \"{0}\"", StringHandlers.CToString(header.magic));
2022-03-06 13:29:38 +00:00
AaruConsole.DebugWriteLine("CPCDSK plugin", "header.creator = \"{0}\"",
StringHandlers.CToString(header.creator));
2022-03-06 13:29:38 +00:00
AaruConsole.DebugWriteLine("CPCDSK plugin", "header.tracks = {0}", header.tracks);
AaruConsole.DebugWriteLine("CPCDSK plugin", "header.sides = {0}", header.sides);
2022-03-06 13:29:38 +00:00
if(!_extended)
AaruConsole.DebugWriteLine("CPCDSK plugin", "header.tracksize = {0}", header.tracksize);
else
2022-03-07 07:36:44 +00:00
for(var i = 0; i < header.tracks; i++)
{
2022-03-07 07:36:44 +00:00
for(var j = 0; j < header.sides; j++)
2022-03-06 13:29:38 +00:00
AaruConsole.DebugWriteLine("CPCDSK plugin", "Track {0} Side {1} size = {2}", i, j,
2022-03-07 07:36:44 +00:00
header.tracksizeTable[i * header.sides + j] * 256);
2022-03-06 13:29:38 +00:00
}
2022-03-06 13:29:38 +00:00
ulong currentSector = 0;
_sectors = new Dictionary<ulong, byte[]>();
_addressMarks = new Dictionary<ulong, byte[]>();
ulong readtracks = 0;
2022-03-07 07:36:44 +00:00
var allTracksSameSize = true;
2022-03-06 13:29:38 +00:00
ulong sectorsPerTrack = 0;
2022-03-06 13:29:38 +00:00
// Seek to first track descriptor
stream.Seek(256, SeekOrigin.Begin);
2022-03-07 07:36:44 +00:00
for(var i = 0; i < header.tracks; i++)
2022-03-06 13:29:38 +00:00
{
2022-03-07 07:36:44 +00:00
for(var j = 0; j < header.sides; j++)
2022-03-06 13:29:38 +00:00
{
// Track not stored in image
2022-03-07 07:36:44 +00:00
if(_extended && header.tracksizeTable[i * header.sides + j] == 0)
2022-03-06 13:29:38 +00:00
continue;
2022-03-06 13:29:38 +00:00
long trackPos = stream.Position;
2022-03-07 07:36:44 +00:00
var trackB = new byte[256];
2022-03-06 13:29:38 +00:00
stream.Read(trackB, 0, 256);
TrackInfo trackInfo = Marshal.ByteArrayToStructureLittleEndian<TrackInfo>(trackB);
2022-03-06 13:29:38 +00:00
if(string.Compare(TRACK_ID, Encoding.ASCII.GetString(trackInfo.magic),
StringComparison.InvariantCultureIgnoreCase) != 0)
{
AaruConsole.ErrorWriteLine("Not the expected track info.");
2022-03-06 13:29:38 +00:00
return ErrorNumber.InvalidArgument;
}
2022-03-06 13:29:38 +00:00
AaruConsole.DebugWriteLine("CPCDSK plugin", "trackInfo[{1}:{2}].magic = \"{0}\"",
StringHandlers.CToString(trackInfo.magic), i, j);
2022-03-06 13:29:38 +00:00
AaruConsole.DebugWriteLine("CPCDSK plugin", "trackInfo[{1}:{2}].bps = {0}",
SizeCodeToBytes(trackInfo.bps), i, j);
2022-03-07 07:36:44 +00:00
AaruConsole.DebugWriteLine("CPCDSK plugin", "trackInfo[{1}:{2}].dataRate = {0}", trackInfo.dataRate, i,
j);
2020-02-29 18:03:35 +00:00
2022-03-07 07:36:44 +00:00
AaruConsole.DebugWriteLine("CPCDSK plugin", "trackInfo[{1}:{2}].filler = 0x{0:X2}", trackInfo.filler, i,
2022-03-06 13:29:38 +00:00
j);
2022-03-07 07:36:44 +00:00
AaruConsole.DebugWriteLine("CPCDSK plugin", "trackInfo[{1}:{2}].gap3 = 0x{0:X2}", trackInfo.gap3, i, j);
AaruConsole.DebugWriteLine("CPCDSK plugin", "trackInfo[{1}:{2}].padding = {0}", trackInfo.padding, i,
j);
2022-03-06 13:29:38 +00:00
AaruConsole.DebugWriteLine("CPCDSK plugin", "trackInfo[{1}:{2}].recordingMode = {0}",
trackInfo.recordingMode, i, j);
2022-03-07 07:36:44 +00:00
AaruConsole.DebugWriteLine("CPCDSK plugin", "trackInfo[{1}:{2}].sectors = {0}", trackInfo.sectors, i,
j);
2022-03-06 13:29:38 +00:00
AaruConsole.DebugWriteLine("CPCDSK plugin", "trackInfo[{1}:{2}].side = {0}", trackInfo.side, i, j);
2022-03-07 07:36:44 +00:00
AaruConsole.DebugWriteLine("CPCDSK plugin", "trackInfo[{1}:{2}].track = {0}", trackInfo.track, i, j);
2022-03-06 13:29:38 +00:00
if(trackInfo.sectors != sectorsPerTrack)
if(sectorsPerTrack == 0)
sectorsPerTrack = trackInfo.sectors;
else
allTracksSameSize = false;
2022-03-06 13:29:38 +00:00
Dictionary<int, byte[]> thisTrackSectors = new();
Dictionary<int, byte[]> thisTrackAddressMarks = new();
2022-03-07 07:36:44 +00:00
for(var k = 1; k <= trackInfo.sectors; k++)
2022-03-06 13:29:38 +00:00
{
AaruConsole.DebugWriteLine("CPCDSK plugin", "trackInfo[{1}:{2}].sector[{3}].id = 0x{0:X2}",
trackInfo.sectorsInfo[k - 1].id, i, j, k);
2022-03-06 13:29:38 +00:00
AaruConsole.DebugWriteLine("CPCDSK plugin", "trackInfo[{1}:{2}].sector[{3}].len = {0}",
trackInfo.sectorsInfo[k - 1].len, i, j, k);
2022-03-06 13:29:38 +00:00
AaruConsole.DebugWriteLine("CPCDSK plugin", "trackInfo[{1}:{2}].sector[{3}].side = {0}",
trackInfo.sectorsInfo[k - 1].side, i, j, k);
2022-03-06 13:29:38 +00:00
AaruConsole.DebugWriteLine("CPCDSK plugin", "trackInfo[{1}:{2}].sector[{3}].size = {0}",
SizeCodeToBytes(trackInfo.sectorsInfo[k - 1].size), i, j, k);
2022-03-06 13:29:38 +00:00
AaruConsole.DebugWriteLine("CPCDSK plugin", "trackInfo[{1}:{2}].sector[{3}].st1 = 0x{0:X2}",
trackInfo.sectorsInfo[k - 1].st1, i, j, k);
2022-03-06 13:29:38 +00:00
AaruConsole.DebugWriteLine("CPCDSK plugin", "trackInfo[{1}:{2}].sector[{3}].st2 = 0x{0:X2}",
trackInfo.sectorsInfo[k - 1].st2, i, j, k);
2022-03-06 13:29:38 +00:00
AaruConsole.DebugWriteLine("CPCDSK plugin", "trackInfo[{1}:{2}].sector[{3}].track = {0}",
trackInfo.sectorsInfo[k - 1].track, i, j, k);
2022-03-06 13:29:38 +00:00
int sectLen = _extended ? trackInfo.sectorsInfo[k - 1].len
: SizeCodeToBytes(trackInfo.sectorsInfo[k - 1].size);
2022-03-07 07:36:44 +00:00
var sector = new byte[sectLen];
2022-03-06 13:29:38 +00:00
stream.Read(sector, 0, sectLen);
2022-03-06 13:29:38 +00:00
if(sectLen < SizeCodeToBytes(trackInfo.sectorsInfo[k - 1].size))
{
2022-03-07 07:36:44 +00:00
var temp = new byte[SizeCodeToBytes(trackInfo.sectorsInfo[k - 1].size)];
2022-03-06 13:29:38 +00:00
Array.Copy(sector, 0, temp, 0, sector.Length);
sector = temp;
}
2022-03-06 13:29:38 +00:00
else if(sectLen > SizeCodeToBytes(trackInfo.sectorsInfo[k - 1].size))
{
2022-03-07 07:36:44 +00:00
var temp = new byte[SizeCodeToBytes(trackInfo.sectorsInfo[k - 1].size)];
2022-03-06 13:29:38 +00:00
Array.Copy(sector, 0, temp, 0, temp.Length);
sector = temp;
}
2022-03-06 13:29:38 +00:00
thisTrackSectors[(trackInfo.sectorsInfo[k - 1].id & 0x3F) - 1] = sector;
2022-03-07 07:36:44 +00:00
var amForCrc = new byte[8];
2022-03-06 13:29:38 +00:00
amForCrc[0] = 0xA1;
amForCrc[1] = 0xA1;
amForCrc[2] = 0xA1;
amForCrc[3] = (byte)IBMIdType.AddressMark;
amForCrc[4] = trackInfo.sectorsInfo[k - 1].track;
amForCrc[5] = trackInfo.sectorsInfo[k - 1].side;
amForCrc[6] = trackInfo.sectorsInfo[k - 1].id;
amForCrc[7] = (byte)trackInfo.sectorsInfo[k - 1].size;
2022-03-06 13:29:38 +00:00
CRC16IBMContext.Data(amForCrc, 8, out byte[] amCrc);
2022-03-07 07:36:44 +00:00
var addressMark = new byte[22];
2022-03-06 13:29:38 +00:00
Array.Copy(amForCrc, 0, addressMark, 12, 8);
Array.Copy(amCrc, 0, addressMark, 20, 2);
thisTrackAddressMarks[(trackInfo.sectorsInfo[k - 1].id & 0x3F) - 1] = addressMark;
}
2022-03-06 13:29:38 +00:00
foreach(KeyValuePair<int, byte[]> s in thisTrackSectors.OrderBy(k => k.Key))
{
_sectors.Add(currentSector, s.Value);
_addressMarks.Add(currentSector, s.Value);
currentSector++;
if(s.Value.Length > _imageInfo.SectorSize)
_imageInfo.SectorSize = (uint)s.Value.Length;
}
2022-03-06 13:29:38 +00:00
stream.Seek(trackPos, SeekOrigin.Begin);
2022-03-06 13:29:38 +00:00
if(_extended)
{
2022-03-07 07:36:44 +00:00
stream.Seek(header.tracksizeTable[i * header.sides + j] * 256, SeekOrigin.Current);
_imageInfo.ImageSize += (ulong)(header.tracksizeTable[i * header.sides + j] * 256) - 256;
2022-03-06 13:29:38 +00:00
}
else
{
stream.Seek(header.tracksize, SeekOrigin.Current);
_imageInfo.ImageSize += (ulong)header.tracksize - 256;
}
2017-08-02 23:01:11 +01:00
2022-03-06 13:29:38 +00:00
readtracks++;
}
}
2022-03-06 13:29:38 +00:00
AaruConsole.DebugWriteLine("CPCDSK plugin", "Read {0} sectors", _sectors.Count);
AaruConsole.DebugWriteLine("CPCDSK plugin", "Read {0} tracks", readtracks);
AaruConsole.DebugWriteLine("CPCDSK plugin", "All tracks are same size? {0}", allTracksSameSize);
2022-03-06 13:29:38 +00:00
_imageInfo.Application = StringHandlers.CToString(header.creator);
_imageInfo.CreationTime = imageFilter.CreationTime;
_imageInfo.LastModificationTime = imageFilter.LastWriteTime;
_imageInfo.MediaTitle = Path.GetFileNameWithoutExtension(imageFilter.Filename);
_imageInfo.Sectors = (ulong)_sectors.Count;
_imageInfo.XmlMediaType = XmlMediaType.BlockMedia;
_imageInfo.MediaType = MediaType.CompactFloppy;
_imageInfo.ReadableSectorTags.Add(SectorTagType.FloppyAddressMark);
2022-03-06 13:29:38 +00:00
_imageInfo.Cylinders = header.tracks;
_imageInfo.Heads = header.sides;
_imageInfo.SectorsPerTrack = (uint)(_imageInfo.Sectors / (_imageInfo.Cylinders * _imageInfo.Heads));
2022-03-06 13:29:38 +00:00
return ErrorNumber.NoError;
}
2022-03-06 13:29:38 +00:00
/// <inheritdoc />
public ErrorNumber ReadSector(ulong sectorAddress, out byte[] buffer) =>
_sectors.TryGetValue(sectorAddress, out buffer) ? ErrorNumber.NoError : ErrorNumber.SectorNotFound;
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++)
{
2022-03-06 13:29:38 +00:00
ErrorNumber errno = ReadSector(sectorAddress + i, out byte[] sector);
2022-03-06 13:29:38 +00:00
if(errno != ErrorNumber.NoError)
return errno;
2022-03-06 13:29:38 +00:00
ms.Write(sector, 0, sector.Length);
}
2022-03-06 13:29:38 +00:00
buffer = ms.ToArray();
2022-03-06 13:29:38 +00:00
return ErrorNumber.NoError;
}
2022-03-06 13:29:38 +00:00
/// <inheritdoc />
public ErrorNumber ReadSectorTag(ulong sectorAddress, SectorTagType tag, out byte[] buffer)
{
buffer = null;
2022-03-06 13:29:38 +00:00
if(tag != SectorTagType.FloppyAddressMark)
return ErrorNumber.NotSupported;
2022-03-07 07:36:44 +00:00
return _addressMarks.TryGetValue(sectorAddress, out buffer) ? ErrorNumber.NoError : ErrorNumber.SectorNotFound;
2022-03-06 13:29:38 +00:00
}
2022-03-06 13:29:38 +00:00
/// <inheritdoc />
public ErrorNumber ReadSectorsTag(ulong sectorAddress, uint length, SectorTagType tag, out byte[] buffer)
{
buffer = null;
2022-03-06 13:29:38 +00:00
if(tag != SectorTagType.FloppyAddressMark)
return ErrorNumber.NotSupported;
2022-03-06 13:29:38 +00:00
if(sectorAddress > _imageInfo.Sectors - 1)
return ErrorNumber.OutOfRange;
if(sectorAddress + length > _imageInfo.Sectors)
return ErrorNumber.OutOfRange;
2022-03-06 13:29:38 +00:00
var ms = new MemoryStream();
for(uint i = 0; i < length; i++)
{
ErrorNumber errno = ReadSector(sectorAddress + i, out byte[] addressMark);
2022-03-06 13:29:38 +00:00
if(errno != ErrorNumber.NoError)
return errno;
ms.Write(addressMark, 0, addressMark.Length);
}
2022-03-06 13:29:38 +00:00
buffer = ms.ToArray();
return ErrorNumber.NoError;
}
2017-12-19 20:33:03 +00:00
}