2018-07-23 23:25:43 +01:00
|
|
|
|
// /***************************************************************************
|
2020-02-27 12:31:25 +00:00
|
|
|
|
// Aaru Data Preservation Suite
|
2016-10-07 00:41:59 +01:00
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
|
//
|
2018-07-23 23:25:43 +01:00
|
|
|
|
// Filename : Read.cs
|
2016-10-07 00:41:59 +01:00
|
|
|
|
// Author(s) : Natalia Portillo <claunia@claunia.com>
|
|
|
|
|
|
//
|
2017-12-19 03:50:57 +00:00
|
|
|
|
// Component : Disk image plugins.
|
2016-10-07 00:41:59 +01:00
|
|
|
|
//
|
|
|
|
|
|
// --[ Description ] ----------------------------------------------------------
|
|
|
|
|
|
//
|
2018-07-23 23:25:43 +01:00
|
|
|
|
// Reads Apple nibbelized disk images.
|
2016-10-07 00:41:59 +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/>.
|
|
|
|
|
|
//
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
2022-02-18 10:02:53 +00:00
|
|
|
|
// Copyright © 2011-2022 Natalia Portillo
|
2016-10-07 00:41:59 +01:00
|
|
|
|
// ****************************************************************************/
|
2016-10-07 00:43:12 +01:00
|
|
|
|
|
2022-03-07 07:36:44 +00:00
|
|
|
|
namespace Aaru.DiscImages;
|
|
|
|
|
|
|
2016-10-07 00:43:12 +01:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
using System.IO;
|
|
|
|
|
|
using System.Linq;
|
2020-02-27 00:33:26 +00:00
|
|
|
|
using Aaru.CommonTypes;
|
|
|
|
|
|
using Aaru.CommonTypes.Enums;
|
|
|
|
|
|
using Aaru.CommonTypes.Interfaces;
|
|
|
|
|
|
using Aaru.Console;
|
|
|
|
|
|
using Aaru.Decoders.Floppy;
|
2022-11-14 09:43:16 +00:00
|
|
|
|
using Aaru.Helpers;
|
2016-10-07 00:43:12 +01:00
|
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
|
public sealed partial class AppleNib
|
2016-10-07 00:41:59 +01:00
|
|
|
|
{
|
2022-03-06 13:29:38 +00:00
|
|
|
|
/// <inheritdoc />
|
|
|
|
|
|
public ErrorNumber Open(IFilter imageFilter)
|
2017-12-19 20:33:03 +00:00
|
|
|
|
{
|
2022-03-06 13:29:38 +00:00
|
|
|
|
Stream stream = imageFilter.GetDataForkStream();
|
|
|
|
|
|
stream.Seek(0, SeekOrigin.Begin);
|
2017-12-19 20:33:03 +00:00
|
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
|
if(stream.Length < 512)
|
|
|
|
|
|
return ErrorNumber.InvalidArgument;
|
2017-12-19 20:33:03 +00:00
|
|
|
|
|
2022-03-07 07:36:44 +00:00
|
|
|
|
var buffer = new byte[stream.Length];
|
2022-11-14 09:43:16 +00:00
|
|
|
|
stream.EnsureRead(buffer, 0, buffer.Length);
|
2017-12-19 20:33:03 +00:00
|
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
|
AaruConsole.DebugWriteLine("Apple NIB Plugin", "Decoding whole image");
|
|
|
|
|
|
List<Apple2.RawTrack> tracks = Apple2.MarshalDisk(buffer);
|
|
|
|
|
|
AaruConsole.DebugWriteLine("Apple NIB Plugin", "Got {0} tracks", tracks.Count);
|
2017-12-19 20:33:03 +00:00
|
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
|
Dictionary<ulong, Apple2.RawSector> rawSectors = new();
|
2017-12-19 20:33:03 +00:00
|
|
|
|
|
2022-03-07 07:36:44 +00:00
|
|
|
|
var spt = 0;
|
|
|
|
|
|
var allTracksEqual = true;
|
2020-02-29 18:03:35 +00:00
|
|
|
|
|
2022-03-07 07:36:44 +00:00
|
|
|
|
for(var i = 1; i < tracks.Count; i++)
|
2022-03-06 13:29:38 +00:00
|
|
|
|
allTracksEqual &= tracks[i - 1].sectors.Length == tracks[i].sectors.Length;
|
2017-12-19 20:33:03 +00:00
|
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
|
if(allTracksEqual)
|
|
|
|
|
|
spt = tracks[0].sectors.Length;
|
2017-12-19 20:33:03 +00:00
|
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
|
bool skewed = spt == 16;
|
|
|
|
|
|
ulong[] skewing = _proDosSkewing;
|
2018-01-28 20:29:46 +00:00
|
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
|
// Detect ProDOS skewed disks
|
|
|
|
|
|
if(skewed)
|
|
|
|
|
|
foreach(bool isDos in from sector in tracks[17].sectors
|
|
|
|
|
|
where sector.addressField.sector.SequenceEqual(new byte[]
|
|
|
|
|
|
{
|
|
|
|
|
|
170, 170
|
|
|
|
|
|
}) select Apple2.DecodeSector(sector) into sector0 where sector0 != null
|
|
|
|
|
|
select sector0[0x01] == 17 && sector0[0x02] < 16 && sector0[0x27] <= 122 &&
|
|
|
|
|
|
sector0[0x34] == 35 && sector0[0x35] == 16 && sector0[0x36] == 0 &&
|
|
|
|
|
|
sector0[0x37] == 1)
|
|
|
|
|
|
{
|
|
|
|
|
|
if(isDos)
|
|
|
|
|
|
skewing = _dosSkewing;
|
2017-12-19 20:33:03 +00:00
|
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
|
AaruConsole.DebugWriteLine("Apple NIB Plugin", "Using {0}DOS skewing",
|
|
|
|
|
|
skewing.SequenceEqual(_dosSkewing) ? "" : "Pro");
|
|
|
|
|
|
}
|
2020-02-29 18:03:35 +00:00
|
|
|
|
|
2022-03-07 07:36:44 +00:00
|
|
|
|
for(var i = 0; i < tracks.Count; i++)
|
2022-03-06 13:29:38 +00:00
|
|
|
|
foreach(Apple2.RawSector sector in tracks[i].sectors)
|
|
|
|
|
|
if(skewed && spt != 0)
|
|
|
|
|
|
{
|
2022-03-07 07:36:44 +00:00
|
|
|
|
var sectorNo = (ulong)((((sector.addressField.sector[0] & 0x55) << 1) |
|
|
|
|
|
|
(sector.addressField.sector[1] & 0x55)) & 0xFF);
|
2020-02-29 18:03:35 +00:00
|
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
|
AaruConsole.DebugWriteLine("Apple NIB Plugin",
|
2022-03-07 07:36:44 +00:00
|
|
|
|
"Hardware sector {0} of track {1} goes to logical sector {2}", sectorNo,
|
|
|
|
|
|
i, skewing[sectorNo] + (ulong)(i * spt));
|
2017-12-19 20:33:03 +00:00
|
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
|
rawSectors.Add(skewing[sectorNo] + (ulong)(i * spt), sector);
|
|
|
|
|
|
_imageInfo.Sectors++;
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
rawSectors.Add(_imageInfo.Sectors, sector);
|
|
|
|
|
|
_imageInfo.Sectors++;
|
|
|
|
|
|
}
|
2017-12-19 20:33:03 +00:00
|
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
|
AaruConsole.DebugWriteLine("Apple NIB Plugin", "Got {0} sectors", _imageInfo.Sectors);
|
2017-12-19 20:33:03 +00:00
|
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
|
AaruConsole.DebugWriteLine("Apple NIB Plugin", "Cooking sectors");
|
2017-12-19 20:33:03 +00:00
|
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
|
_longSectors = new Dictionary<ulong, byte[]>();
|
|
|
|
|
|
_cookedSectors = new Dictionary<ulong, byte[]>();
|
|
|
|
|
|
_addressFields = new Dictionary<ulong, byte[]>();
|
2017-12-19 20:33:03 +00:00
|
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
|
foreach(KeyValuePair<ulong, Apple2.RawSector> kvp in rawSectors)
|
|
|
|
|
|
{
|
|
|
|
|
|
byte[] cooked = Apple2.DecodeSector(kvp.Value);
|
|
|
|
|
|
byte[] raw = Apple2.MarshalSector(kvp.Value);
|
|
|
|
|
|
byte[] addr = Apple2.MarshalAddressField(kvp.Value.addressField);
|
|
|
|
|
|
_longSectors.Add(kvp.Key, raw);
|
|
|
|
|
|
_cookedSectors.Add(kvp.Key, cooked);
|
|
|
|
|
|
_addressFields.Add(kvp.Key, addr);
|
|
|
|
|
|
}
|
2020-02-29 18:03:35 +00:00
|
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
|
_imageInfo.ImageSize = (ulong)imageFilter.DataForkLength;
|
|
|
|
|
|
_imageInfo.CreationTime = imageFilter.CreationTime;
|
|
|
|
|
|
_imageInfo.LastModificationTime = imageFilter.LastWriteTime;
|
|
|
|
|
|
_imageInfo.MediaTitle = Path.GetFileNameWithoutExtension(imageFilter.Filename);
|
2020-02-29 18:03:35 +00:00
|
|
|
|
|
2022-11-13 19:59:24 +00:00
|
|
|
|
_imageInfo.MediaType = _imageInfo.Sectors switch
|
|
|
|
|
|
{
|
|
|
|
|
|
455 => MediaType.Apple32SS,
|
|
|
|
|
|
560 => MediaType.Apple33SS,
|
|
|
|
|
|
_ => MediaType.Unknown
|
|
|
|
|
|
};
|
2020-02-29 18:03:35 +00:00
|
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
|
_imageInfo.SectorSize = 256;
|
|
|
|
|
|
_imageInfo.XmlMediaType = XmlMediaType.BlockMedia;
|
|
|
|
|
|
_imageInfo.ReadableSectorTags.Add(SectorTagType.FloppyAddressMark);
|
2017-12-19 20:33:03 +00:00
|
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
|
switch(_imageInfo.MediaType)
|
|
|
|
|
|
{
|
|
|
|
|
|
case MediaType.Apple32SS:
|
|
|
|
|
|
_imageInfo.Cylinders = 35;
|
|
|
|
|
|
_imageInfo.Heads = 1;
|
|
|
|
|
|
_imageInfo.SectorsPerTrack = 13;
|
|
|
|
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
case MediaType.Apple33SS:
|
|
|
|
|
|
_imageInfo.Cylinders = 35;
|
|
|
|
|
|
_imageInfo.Heads = 1;
|
|
|
|
|
|
_imageInfo.SectorsPerTrack = 16;
|
|
|
|
|
|
|
|
|
|
|
|
break;
|
2017-12-19 20:33:03 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
|
return ErrorNumber.NoError;
|
|
|
|
|
|
}
|
2017-12-19 20:33:03 +00:00
|
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
|
/// <inheritdoc />
|
|
|
|
|
|
public ErrorNumber ReadSector(ulong sectorAddress, out byte[] buffer)
|
|
|
|
|
|
{
|
|
|
|
|
|
buffer = null;
|
2020-02-29 18:03:35 +00:00
|
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
|
if(sectorAddress > _imageInfo.Sectors - 1)
|
|
|
|
|
|
return ErrorNumber.OutOfRange;
|
2017-12-19 20:33:03 +00:00
|
|
|
|
|
2022-03-07 07:36:44 +00:00
|
|
|
|
return _cookedSectors.TryGetValue(sectorAddress, out buffer) ? ErrorNumber.NoError : ErrorNumber.SectorNotFound;
|
2022-03-06 13:29:38 +00:00
|
|
|
|
}
|
2021-09-19 21:16:47 +01:00
|
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
|
/// <inheritdoc />
|
|
|
|
|
|
public ErrorNumber ReadSectors(ulong sectorAddress, uint length, out byte[] buffer)
|
|
|
|
|
|
{
|
|
|
|
|
|
buffer = null;
|
2017-12-19 20:33:03 +00:00
|
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
|
if(sectorAddress > _imageInfo.Sectors - 1)
|
|
|
|
|
|
return ErrorNumber.OutOfRange;
|
2017-12-19 20:33:03 +00:00
|
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
|
if(sectorAddress + length > _imageInfo.Sectors)
|
|
|
|
|
|
return ErrorNumber.OutOfRange;
|
2017-12-19 20:33:03 +00:00
|
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
|
var ms = new MemoryStream();
|
2021-09-19 21:16:47 +01:00
|
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
|
for(uint i = 0; i < length; i++)
|
|
|
|
|
|
{
|
|
|
|
|
|
ErrorNumber errno = ReadSector(sectorAddress + i, out byte[] sector);
|
2017-12-19 20:33:03 +00:00
|
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
|
if(errno != ErrorNumber.NoError)
|
|
|
|
|
|
return errno;
|
2021-09-19 21:16:47 +01:00
|
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
|
ms.Write(sector, 0, sector.Length);
|
2017-12-19 20:33:03 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
|
buffer = ms.ToArray();
|
2021-09-20 20:52:18 +01:00
|
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
|
return ErrorNumber.NoError;
|
|
|
|
|
|
}
|
2017-12-19 20:33:03 +00:00
|
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
|
/// <inheritdoc />
|
|
|
|
|
|
public ErrorNumber ReadSectorTag(ulong sectorAddress, SectorTagType tag, out byte[] buffer)
|
|
|
|
|
|
{
|
|
|
|
|
|
buffer = null;
|
2020-02-29 18:03:35 +00:00
|
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
|
if(sectorAddress > _imageInfo.Sectors - 1)
|
|
|
|
|
|
return ErrorNumber.OutOfRange;
|
2017-12-19 20:33:03 +00:00
|
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
|
if(tag != SectorTagType.FloppyAddressMark)
|
|
|
|
|
|
return ErrorNumber.NotSupported;
|
2021-09-20 20:52:18 +01:00
|
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
|
return _addressFields.TryGetValue(sectorAddress, out buffer) ? ErrorNumber.NoError : ErrorNumber.NoData;
|
|
|
|
|
|
}
|
2017-12-19 20:33:03 +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;
|
2017-12-19 20:33:03 +00:00
|
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
|
if(sectorAddress > _imageInfo.Sectors - 1)
|
|
|
|
|
|
return ErrorNumber.OutOfRange;
|
2017-12-19 20:33:03 +00:00
|
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
|
if(sectorAddress + length > _imageInfo.Sectors)
|
|
|
|
|
|
return ErrorNumber.OutOfRange;
|
2017-12-19 20:33:03 +00:00
|
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
|
if(tag != SectorTagType.FloppyAddressMark)
|
|
|
|
|
|
return ErrorNumber.NotSupported;
|
2021-09-20 20:52:18 +01:00
|
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
|
var ms = new MemoryStream();
|
2021-09-20 20:52:18 +01:00
|
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
|
for(uint i = 0; i < length; i++)
|
|
|
|
|
|
{
|
|
|
|
|
|
ErrorNumber errno = ReadSectorTag(sectorAddress + i, tag, out byte[] sector);
|
2017-12-19 20:33:03 +00:00
|
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
|
if(errno != ErrorNumber.NoError)
|
|
|
|
|
|
return errno;
|
2021-09-20 20:52:18 +01:00
|
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
|
ms.Write(sector, 0, sector.Length);
|
2017-12-19 20:33:03 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
|
buffer = ms.ToArray();
|
2017-12-19 20:33:03 +00:00
|
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
|
return ErrorNumber.NoError;
|
|
|
|
|
|
}
|
2020-02-29 18:03:35 +00:00
|
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
|
/// <inheritdoc />
|
|
|
|
|
|
public ErrorNumber ReadSectorLong(ulong sectorAddress, out byte[] buffer)
|
|
|
|
|
|
{
|
|
|
|
|
|
buffer = null;
|
2017-12-19 20:33:03 +00:00
|
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
|
if(sectorAddress > _imageInfo.Sectors - 1)
|
|
|
|
|
|
return ErrorNumber.OutOfRange;
|
2021-09-20 14:22:22 +01:00
|
|
|
|
|
2022-03-07 07:36:44 +00:00
|
|
|
|
return _longSectors.TryGetValue(sectorAddress, out buffer) ? ErrorNumber.NoError : ErrorNumber.SectorNotFound;
|
2022-03-06 13:29:38 +00:00
|
|
|
|
}
|
2017-12-19 20:33:03 +00:00
|
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
|
/// <inheritdoc />
|
|
|
|
|
|
public ErrorNumber ReadSectorsLong(ulong sectorAddress, uint length, out byte[] buffer)
|
|
|
|
|
|
{
|
|
|
|
|
|
buffer = null;
|
2017-12-19 20:33:03 +00:00
|
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
|
if(sectorAddress > _imageInfo.Sectors - 1)
|
|
|
|
|
|
return ErrorNumber.OutOfRange;
|
2017-12-19 20:33:03 +00:00
|
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
|
if(sectorAddress + length > _imageInfo.Sectors)
|
|
|
|
|
|
return ErrorNumber.OutOfRange;
|
2021-09-20 14:22:22 +01:00
|
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
|
var ms = new MemoryStream();
|
2021-09-20 14:22:22 +01:00
|
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
|
for(uint i = 0; i < length; i++)
|
|
|
|
|
|
{
|
|
|
|
|
|
ErrorNumber errno = ReadSectorLong(sectorAddress + i, out byte[] sector);
|
2017-12-19 20:33:03 +00:00
|
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
|
if(errno != ErrorNumber.NoError)
|
|
|
|
|
|
return errno;
|
2021-09-20 14:22:22 +01:00
|
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
|
ms.Write(sector, 0, sector.Length);
|
2017-12-19 20:33:03 +00:00
|
|
|
|
}
|
2022-03-06 13:29:38 +00:00
|
|
|
|
|
|
|
|
|
|
buffer = ms.ToArray();
|
|
|
|
|
|
|
|
|
|
|
|
return ErrorNumber.NoError;
|
2017-12-19 20:33:03 +00:00
|
|
|
|
}
|
2016-10-07 00:43:12 +01:00
|
|
|
|
}
|