2017-09-16 21:21:17 +01:00
|
|
|
|
// /***************************************************************************
|
2020-02-27 12:31:25 +00:00
|
|
|
|
// Aaru Data Preservation Suite
|
2017-09-16 21:21:17 +01:00
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
|
//
|
2018-07-23 23:25:43 +01:00
|
|
|
|
// Filename : Read.cs
|
2017-09-16 21:21:17 +01:00
|
|
|
|
// Author(s) : Natalia Portillo <claunia@claunia.com>
|
|
|
|
|
|
//
|
2017-12-19 03:50:57 +00:00
|
|
|
|
// Component : Disk image plugins.
|
2017-09-16 21:21:17 +01:00
|
|
|
|
//
|
|
|
|
|
|
// --[ Description ] ----------------------------------------------------------
|
|
|
|
|
|
//
|
2018-07-23 23:25:43 +01:00
|
|
|
|
// Reads Spectrum FDI disk images.
|
2017-09-16 21:21:17 +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
|
2017-09-16 21:21:17 +01:00
|
|
|
|
// ****************************************************************************/
|
2017-12-19 19:33:46 +00:00
|
|
|
|
|
2017-09-16 21:21:17 +01:00
|
|
|
|
using System;
|
|
|
|
|
|
using System.IO;
|
2020-02-27 00:33:26 +00:00
|
|
|
|
using Aaru.CommonTypes;
|
|
|
|
|
|
using Aaru.CommonTypes.Enums;
|
|
|
|
|
|
using Aaru.CommonTypes.Interfaces;
|
|
|
|
|
|
using Aaru.Console;
|
|
|
|
|
|
using Aaru.Helpers;
|
2017-09-16 21:21:17 +01:00
|
|
|
|
|
2020-02-27 00:33:26 +00:00
|
|
|
|
namespace Aaru.DiscImages
|
2017-09-16 21:21:17 +01:00
|
|
|
|
{
|
2020-07-22 13:20:25 +01:00
|
|
|
|
public sealed partial class UkvFdi
|
2017-12-19 20:33:03 +00:00
|
|
|
|
{
|
2017-12-28 19:56:36 +00:00
|
|
|
|
public bool Open(IFilter imageFilter)
|
2017-12-19 20:33:03 +00:00
|
|
|
|
{
|
|
|
|
|
|
Stream stream = imageFilter.GetDataForkStream();
|
|
|
|
|
|
stream.Seek(0, SeekOrigin.Begin);
|
|
|
|
|
|
|
2020-11-11 04:19:18 +00:00
|
|
|
|
if(stream.Length < Marshal.SizeOf<Header>())
|
2020-02-29 18:03:35 +00:00
|
|
|
|
return false;
|
2017-12-19 20:33:03 +00:00
|
|
|
|
|
2020-11-11 04:19:18 +00:00
|
|
|
|
byte[] hdrB = new byte[Marshal.SizeOf<Header>()];
|
2017-12-22 06:55:04 +00:00
|
|
|
|
stream.Read(hdrB, 0, hdrB.Length);
|
2017-12-19 20:33:03 +00:00
|
|
|
|
|
2020-11-11 04:19:18 +00:00
|
|
|
|
Header hdr = Marshal.ByteArrayToStructureLittleEndian<Header>(hdrB);
|
2017-12-19 20:33:03 +00:00
|
|
|
|
|
2020-02-27 23:48:41 +00:00
|
|
|
|
AaruConsole.DebugWriteLine("UkvFdi plugin", "hdr.addInfoLen = {0}", hdr.addInfoLen);
|
2020-02-29 18:03:35 +00:00
|
|
|
|
AaruConsole.DebugWriteLine("UkvFdi plugin", "hdr.cylinders = {0}", hdr.cylinders);
|
|
|
|
|
|
AaruConsole.DebugWriteLine("UkvFdi plugin", "hdr.dataOff = {0}", hdr.dataOff);
|
|
|
|
|
|
AaruConsole.DebugWriteLine("UkvFdi plugin", "hdr.descOff = {0}", hdr.descOff);
|
|
|
|
|
|
AaruConsole.DebugWriteLine("UkvFdi plugin", "hdr.flags = {0}", hdr.flags);
|
|
|
|
|
|
AaruConsole.DebugWriteLine("UkvFdi plugin", "hdr.heads = {0}", hdr.heads);
|
2017-12-19 20:33:03 +00:00
|
|
|
|
|
|
|
|
|
|
stream.Seek(hdr.descOff, SeekOrigin.Begin);
|
|
|
|
|
|
byte[] description = new byte[hdr.dataOff - hdr.descOff];
|
|
|
|
|
|
stream.Read(description, 0, description.Length);
|
2020-07-20 21:11:32 +01:00
|
|
|
|
_imageInfo.Comments = StringHandlers.CToString(description);
|
2017-12-19 20:33:03 +00:00
|
|
|
|
|
2020-07-20 21:11:32 +01:00
|
|
|
|
AaruConsole.DebugWriteLine("UkvFdi plugin", "hdr.description = \"{0}\"", _imageInfo.Comments);
|
2017-12-19 20:33:03 +00:00
|
|
|
|
|
|
|
|
|
|
stream.Seek(0xE + hdr.addInfoLen, SeekOrigin.Begin);
|
|
|
|
|
|
|
2018-01-28 20:29:46 +00:00
|
|
|
|
long spt = long.MaxValue;
|
2017-12-19 20:33:03 +00:00
|
|
|
|
uint[][][] sectorsOff = new uint[hdr.cylinders][][];
|
2020-07-20 21:11:32 +01:00
|
|
|
|
_sectorsData = new byte[hdr.cylinders][][][];
|
2017-12-19 20:33:03 +00:00
|
|
|
|
|
2020-07-20 21:11:32 +01:00
|
|
|
|
_imageInfo.Cylinders = hdr.cylinders;
|
|
|
|
|
|
_imageInfo.Heads = hdr.heads;
|
2017-12-19 20:33:03 +00:00
|
|
|
|
|
|
|
|
|
|
// Read track descriptors
|
|
|
|
|
|
for(ushort cyl = 0; cyl < hdr.cylinders; cyl++)
|
|
|
|
|
|
{
|
2020-07-20 21:11:32 +01:00
|
|
|
|
sectorsOff[cyl] = new uint[hdr.heads][];
|
|
|
|
|
|
_sectorsData[cyl] = new byte[hdr.heads][][];
|
2017-12-19 20:33:03 +00:00
|
|
|
|
|
|
|
|
|
|
for(ushort head = 0; head < hdr.heads; head++)
|
|
|
|
|
|
{
|
2017-12-22 06:55:04 +00:00
|
|
|
|
byte[] sctB = new byte[4];
|
|
|
|
|
|
stream.Read(sctB, 0, 4);
|
2017-12-19 20:33:03 +00:00
|
|
|
|
stream.Seek(2, SeekOrigin.Current);
|
|
|
|
|
|
byte sectors = (byte)stream.ReadByte();
|
2018-01-28 20:29:46 +00:00
|
|
|
|
uint trkOff = BitConverter.ToUInt32(sctB, 0);
|
2017-12-19 20:33:03 +00:00
|
|
|
|
|
2020-02-29 18:03:35 +00:00
|
|
|
|
AaruConsole.DebugWriteLine("UkvFdi plugin", "trkhdr.c = {0}", cyl);
|
|
|
|
|
|
AaruConsole.DebugWriteLine("UkvFdi plugin", "trkhdr.h = {0}", head);
|
2020-02-27 23:48:41 +00:00
|
|
|
|
AaruConsole.DebugWriteLine("UkvFdi plugin", "trkhdr.sectors = {0}", sectors);
|
2020-02-29 18:03:35 +00:00
|
|
|
|
AaruConsole.DebugWriteLine("UkvFdi plugin", "trkhdr.off = {0}", trkOff);
|
2017-12-19 20:33:03 +00:00
|
|
|
|
|
2020-07-20 21:11:32 +01:00
|
|
|
|
sectorsOff[cyl][head] = new uint[sectors];
|
|
|
|
|
|
_sectorsData[cyl][head] = new byte[sectors][];
|
2017-12-19 20:33:03 +00:00
|
|
|
|
|
2020-02-29 18:03:35 +00:00
|
|
|
|
if(sectors < spt &&
|
|
|
|
|
|
sectors > 0)
|
|
|
|
|
|
spt = sectors;
|
2017-12-19 20:33:03 +00:00
|
|
|
|
|
|
|
|
|
|
for(ushort sec = 0; sec < sectors; sec++)
|
|
|
|
|
|
{
|
2020-02-29 18:03:35 +00:00
|
|
|
|
byte c = (byte)stream.ReadByte();
|
|
|
|
|
|
byte h = (byte)stream.ReadByte();
|
|
|
|
|
|
byte r = (byte)stream.ReadByte();
|
|
|
|
|
|
byte n = (byte)stream.ReadByte();
|
|
|
|
|
|
var f = (SectorFlags)stream.ReadByte();
|
|
|
|
|
|
byte[] offB = new byte[2];
|
2017-12-22 06:55:04 +00:00
|
|
|
|
stream.Read(offB, 0, 2);
|
|
|
|
|
|
ushort secOff = BitConverter.ToUInt16(offB, 0);
|
2017-12-19 20:33:03 +00:00
|
|
|
|
|
2020-02-29 18:03:35 +00:00
|
|
|
|
AaruConsole.DebugWriteLine("UkvFdi plugin", "sechdr.c = {0}", c);
|
|
|
|
|
|
AaruConsole.DebugWriteLine("UkvFdi plugin", "sechdr.h = {0}", h);
|
|
|
|
|
|
AaruConsole.DebugWriteLine("UkvFdi plugin", "sechdr.r = {0}", r);
|
2020-02-27 23:48:41 +00:00
|
|
|
|
AaruConsole.DebugWriteLine("UkvFdi plugin", "sechdr.n = {0} ({1})", n, 128 << n);
|
2020-02-29 18:03:35 +00:00
|
|
|
|
AaruConsole.DebugWriteLine("UkvFdi plugin", "sechdr.f = {0}", f);
|
|
|
|
|
|
|
2020-02-27 23:48:41 +00:00
|
|
|
|
AaruConsole.DebugWriteLine("UkvFdi plugin", "sechdr.off = {0} ({1})", secOff,
|
2020-02-29 18:03:35 +00:00
|
|
|
|
secOff + trkOff + hdr.dataOff);
|
2017-12-19 20:33:03 +00:00
|
|
|
|
|
|
|
|
|
|
// TODO: This assumes sequential sectors.
|
2020-07-20 21:11:32 +01:00
|
|
|
|
sectorsOff[cyl][head][sec] = secOff + trkOff + hdr.dataOff;
|
|
|
|
|
|
_sectorsData[cyl][head][sec] = new byte[128 << n];
|
2017-12-19 20:33:03 +00:00
|
|
|
|
|
2020-07-20 21:11:32 +01:00
|
|
|
|
if(128 << n > _imageInfo.SectorSize)
|
|
|
|
|
|
_imageInfo.SectorSize = (uint)(128 << n);
|
2017-12-19 20:33:03 +00:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Read sectors
|
|
|
|
|
|
for(ushort cyl = 0; cyl < hdr.cylinders; cyl++)
|
|
|
|
|
|
{
|
|
|
|
|
|
bool emptyCyl = false;
|
|
|
|
|
|
|
|
|
|
|
|
for(ushort head = 0; head < hdr.heads; head++)
|
|
|
|
|
|
{
|
|
|
|
|
|
for(ushort sec = 0; sec < sectorsOff[cyl][head].Length; sec++)
|
|
|
|
|
|
{
|
|
|
|
|
|
stream.Seek(sectorsOff[cyl][head][sec], SeekOrigin.Begin);
|
2020-07-20 21:11:32 +01:00
|
|
|
|
stream.Read(_sectorsData[cyl][head][sec], 0, _sectorsData[cyl][head][sec].Length);
|
2017-12-19 20:33:03 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// For empty cylinders
|
2020-02-29 18:03:35 +00:00
|
|
|
|
if(sectorsOff[cyl][head].Length != 0)
|
|
|
|
|
|
continue;
|
2017-12-21 06:06:19 +00:00
|
|
|
|
|
|
|
|
|
|
if(cyl + 1 == hdr.cylinders ||
|
2020-02-29 18:03:35 +00:00
|
|
|
|
|
2017-12-21 06:06:19 +00:00
|
|
|
|
// Next cylinder is also empty
|
2020-02-29 18:03:35 +00:00
|
|
|
|
sectorsOff[cyl + 1][head].Length == 0)
|
|
|
|
|
|
emptyCyl = true;
|
|
|
|
|
|
|
2017-12-21 06:06:19 +00:00
|
|
|
|
// Create empty sectors
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
2020-07-20 21:11:32 +01:00
|
|
|
|
_sectorsData[cyl][head] = new byte[spt][];
|
2020-02-29 18:03:35 +00:00
|
|
|
|
|
|
|
|
|
|
for(int i = 0; i < spt; i++)
|
2020-07-20 21:11:32 +01:00
|
|
|
|
_sectorsData[cyl][head][i] = new byte[_imageInfo.SectorSize];
|
2017-12-21 06:06:19 +00:00
|
|
|
|
}
|
2017-12-19 20:33:03 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
2020-02-29 18:03:35 +00:00
|
|
|
|
if(emptyCyl)
|
2020-07-20 21:11:32 +01:00
|
|
|
|
_imageInfo.Cylinders--;
|
2017-12-19 20:33:03 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// TODO: What about double sided, half track pitch compact floppies?
|
2020-07-20 21:11:32 +01:00
|
|
|
|
_imageInfo.MediaType = MediaType.CompactFloppy;
|
|
|
|
|
|
_imageInfo.ImageSize = (ulong)stream.Length - hdr.dataOff;
|
|
|
|
|
|
_imageInfo.CreationTime = imageFilter.GetCreationTime();
|
|
|
|
|
|
_imageInfo.LastModificationTime = imageFilter.GetLastWriteTime();
|
|
|
|
|
|
_imageInfo.MediaTitle = Path.GetFileNameWithoutExtension(imageFilter.GetFilename());
|
|
|
|
|
|
_imageInfo.SectorsPerTrack = (uint)spt;
|
|
|
|
|
|
_imageInfo.Sectors = _imageInfo.Cylinders * _imageInfo.Heads * _imageInfo.SectorsPerTrack;
|
|
|
|
|
|
_imageInfo.XmlMediaType = XmlMediaType.BlockMedia;
|
2017-12-19 20:33:03 +00:00
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2017-12-26 07:28:40 +00:00
|
|
|
|
public byte[] ReadSector(ulong sectorAddress)
|
2017-12-19 20:33:03 +00:00
|
|
|
|
{
|
|
|
|
|
|
(ushort cylinder, byte head, byte sector) = LbaToChs(sectorAddress);
|
|
|
|
|
|
|
2020-07-20 21:11:32 +01:00
|
|
|
|
if(cylinder >= _sectorsData.Length)
|
2017-12-19 20:33:03 +00:00
|
|
|
|
throw new ArgumentOutOfRangeException(nameof(sectorAddress), "Sector address not found");
|
|
|
|
|
|
|
2020-07-20 21:11:32 +01:00
|
|
|
|
if(head >= _sectorsData[cylinder].Length)
|
2017-12-19 20:33:03 +00:00
|
|
|
|
throw new ArgumentOutOfRangeException(nameof(sectorAddress), "Sector address not found");
|
|
|
|
|
|
|
2020-07-20 21:11:32 +01:00
|
|
|
|
if(sector > _sectorsData[cylinder][head].Length)
|
2017-12-19 20:33:03 +00:00
|
|
|
|
throw new ArgumentOutOfRangeException(nameof(sectorAddress), "Sector address not found");
|
|
|
|
|
|
|
2020-07-20 21:11:32 +01:00
|
|
|
|
return _sectorsData[cylinder][head][sector - 1];
|
2017-12-19 20:33:03 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
2017-12-26 07:28:40 +00:00
|
|
|
|
public byte[] ReadSectors(ulong sectorAddress, uint length)
|
2017-12-19 20:33:03 +00:00
|
|
|
|
{
|
2020-07-20 21:11:32 +01:00
|
|
|
|
if(sectorAddress > _imageInfo.Sectors - 1)
|
2017-12-19 20:33:03 +00:00
|
|
|
|
throw new ArgumentOutOfRangeException(nameof(sectorAddress), "Sector address not found");
|
|
|
|
|
|
|
2020-07-20 21:11:32 +01:00
|
|
|
|
if(sectorAddress + length > _imageInfo.Sectors)
|
2017-12-19 20:33:03 +00:00
|
|
|
|
throw new ArgumentOutOfRangeException(nameof(length), "Requested more sectors than available");
|
|
|
|
|
|
|
2020-02-29 18:03:35 +00:00
|
|
|
|
var buffer = new MemoryStream();
|
|
|
|
|
|
|
2017-12-19 20:33:03 +00:00
|
|
|
|
for(uint i = 0; i < length; i++)
|
|
|
|
|
|
{
|
|
|
|
|
|
byte[] sector = ReadSector(sectorAddress + i);
|
|
|
|
|
|
buffer.Write(sector, 0, sector.Length);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return buffer.ToArray();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2017-09-16 21:21:17 +01:00
|
|
|
|
}
|