Files
Aaru/Aaru.Images/IMD/Read.cs

246 lines
9.2 KiB
C#
Raw Normal View History

2017-08-17 02:16:34 +01:00
// /***************************************************************************
2020-02-27 12:31:25 +00:00
// Aaru Data Preservation Suite
2017-08-17 02:16:34 +01:00
// ----------------------------------------------------------------------------
//
// Filename : Read.cs
2017-08-17 02:16:34 +01:00
// Author(s) : Natalia Portillo <claunia@claunia.com>
//
// Component : Disk image plugins.
2017-08-17 02:16:34 +01:00
//
// --[ Description ] ----------------------------------------------------------
//
// Reads Dunfield's IMD disk images.
2017-08-17 02:16:34 +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-08-17 02:16:34 +01:00
// ****************************************************************************/
2017-08-17 02:16:34 +01:00
using System;
using System.Collections.Generic;
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;
2020-07-20 15:43:52 +01:00
using Aaru.Helpers;
2020-02-27 00:33:26 +00:00
namespace Aaru.DiscImages
2017-08-17 02:16:34 +01:00
{
2020-07-22 13:20:25 +01:00
public sealed partial class Imd
2017-12-19 20:33:03 +00:00
{
2021-08-17 13:56:05 +01:00
/// <inheritdoc />
public ErrorNumber Open(IFilter imageFilter)
2017-12-19 20:33:03 +00:00
{
Stream stream = imageFilter.GetDataForkStream();
stream.Seek(0, SeekOrigin.Begin);
2020-02-29 18:03:35 +00:00
var cmt = new MemoryStream();
2017-12-19 20:33:03 +00:00
stream.Seek(0x1F, SeekOrigin.Begin);
2020-02-29 18:03:35 +00:00
2017-12-19 20:33:03 +00:00
for(uint i = 0; i < stream.Length; i++)
{
byte b = (byte)stream.ReadByte();
2020-02-29 18:03:35 +00:00
if(b == 0x1A)
break;
2017-12-19 20:33:03 +00:00
cmt.WriteByte(b);
}
2020-07-20 21:11:32 +01:00
_imageInfo.Comments = StringHandlers.CToString(cmt.ToArray());
_sectorsData = new List<byte[]>();
2017-12-19 20:33:03 +00:00
byte currentCylinder = 0;
2020-07-20 21:11:32 +01:00
_imageInfo.Cylinders = 1;
_imageInfo.Heads = 1;
2018-06-22 08:08:38 +01:00
ulong currentLba = 0;
2017-12-19 20:33:03 +00:00
TransferRate mode = TransferRate.TwoHundred;
while(stream.Position + 5 < stream.Length)
{
2018-06-22 08:08:38 +01:00
mode = (TransferRate)stream.ReadByte();
byte cylinder = (byte)stream.ReadByte();
byte head = (byte)stream.ReadByte();
byte spt = (byte)stream.ReadByte();
byte n = (byte)stream.ReadByte();
byte[] idmap = new byte[spt];
byte[] cylmap = new byte[spt];
byte[] headmap = new byte[spt];
ushort[] bps = new ushort[spt];
2017-12-19 20:33:03 +00:00
if(cylinder != currentCylinder)
{
currentCylinder = cylinder;
2020-07-20 21:11:32 +01:00
_imageInfo.Cylinders++;
2017-12-19 20:33:03 +00:00
}
2020-02-29 18:03:35 +00:00
if((head & 1) == 1)
2020-07-20 21:11:32 +01:00
_imageInfo.Heads = 2;
2017-12-19 20:33:03 +00:00
2018-06-22 08:08:38 +01:00
stream.Read(idmap, 0, idmap.Length);
2020-02-29 18:03:35 +00:00
if((head & SECTOR_CYLINDER_MAP_MASK) == SECTOR_CYLINDER_MAP_MASK)
stream.Read(cylmap, 0, cylmap.Length);
2018-06-22 08:08:38 +01:00
if((head & SECTOR_HEAD_MAP_MASK) == SECTOR_HEAD_MAP_MASK)
stream.Read(headmap, 0, headmap.Length);
2020-02-29 18:03:35 +00:00
2017-12-19 20:33:03 +00:00
if(n == 0xFF)
{
byte[] bpsbytes = new byte[spt * 2];
stream.Read(bpsbytes, 0, bpsbytes.Length);
2020-02-29 18:03:35 +00:00
for(int i = 0; i < spt; i++)
bps[i] = BitConverter.ToUInt16(bpsbytes, i * 2);
2017-12-19 20:33:03 +00:00
}
else
for(int i = 0; i < spt; i++)
bps[i] = (ushort)(128 << n);
2017-12-19 20:33:03 +00:00
2020-07-20 21:11:32 +01:00
if(spt > _imageInfo.SectorsPerTrack)
_imageInfo.SectorsPerTrack = spt;
2017-12-19 20:33:03 +00:00
SortedDictionary<byte, byte[]> track = new();
2017-12-19 20:33:03 +00:00
for(int i = 0; i < spt; i++)
{
2020-02-29 18:03:35 +00:00
var type = (SectorType)stream.ReadByte();
byte[] data = new byte[bps[i]];
2017-12-19 20:33:03 +00:00
// TODO; Handle disks with different bps in track 0
2020-07-20 21:11:32 +01:00
if(bps[i] > _imageInfo.SectorSize)
_imageInfo.SectorSize = bps[i];
2017-12-19 20:33:03 +00:00
switch(type)
{
case SectorType.Unavailable:
2020-02-29 18:03:35 +00:00
if(!track.ContainsKey(idmap[i]))
track.Add(idmap[i], data);
2017-12-19 20:33:03 +00:00
break;
case SectorType.Normal:
case SectorType.Deleted:
case SectorType.Error:
case SectorType.DeletedError:
stream.Read(data, 0, data.Length);
2020-02-29 18:03:35 +00:00
if(!track.ContainsKey(idmap[i]))
track.Add(idmap[i], data);
2020-07-20 21:11:32 +01:00
_imageInfo.ImageSize += (ulong)data.Length;
2020-02-29 18:03:35 +00:00
2017-12-19 20:33:03 +00:00
break;
case SectorType.Compressed:
case SectorType.CompressedDeleted:
case SectorType.CompressedError:
case SectorType.CompressedDeletedError:
byte filling = (byte)stream.ReadByte();
ArrayHelpers.ArrayFill(data, filling);
2020-02-29 18:03:35 +00:00
if(!track.ContainsKey(idmap[i]))
track.Add(idmap[i], data);
2017-12-19 20:33:03 +00:00
break;
2021-09-21 04:55:28 +01:00
default:
AaruConsole.ErrorWriteLine($"Invalid sector type {(byte)type}");
return ErrorNumber.InvalidArgument;
2017-12-19 20:33:03 +00:00
}
}
foreach(KeyValuePair<byte, byte[]> kvp in track)
{
2020-07-20 21:11:32 +01:00
_sectorsData.Add(kvp.Value);
2017-12-19 20:33:03 +00:00
currentLba++;
}
}
2020-07-20 21:11:32 +01:00
_imageInfo.Application = "IMD";
2020-02-29 18:03:35 +00:00
2017-12-19 20:33:03 +00:00
// TODO: The header is the date of dump or the date of the application compilation?
_imageInfo.CreationTime = imageFilter.CreationTime;
_imageInfo.LastModificationTime = imageFilter.LastWriteTime;
_imageInfo.MediaTitle = Path.GetFileNameWithoutExtension(imageFilter.Filename);
2020-07-20 21:11:32 +01:00
_imageInfo.Comments = StringHandlers.CToString(cmt.ToArray());
_imageInfo.Sectors = currentLba;
_imageInfo.MediaType = MediaType.Unknown;
MediaEncoding mediaEncoding = MediaEncoding.MFM;
2020-02-29 18:03:35 +00:00
if(mode == TransferRate.TwoHundred ||
mode == TransferRate.ThreeHundred ||
mode == TransferRate.FiveHundred)
mediaEncoding = MediaEncoding.FM;
2020-07-20 21:11:32 +01:00
_imageInfo.MediaType = Geometry.GetMediaType(((ushort)_imageInfo.Cylinders, (byte)_imageInfo.Heads,
(ushort)_imageInfo.SectorsPerTrack, _imageInfo.SectorSize,
mediaEncoding, false));
2017-12-19 20:33:03 +00:00
2020-07-20 21:11:32 +01:00
switch(_imageInfo.MediaType)
2017-12-19 20:33:03 +00:00
{
case MediaType.NEC_525_HD when mode == TransferRate.FiveHundredMfm:
2020-07-20 21:11:32 +01:00
_imageInfo.MediaType = MediaType.NEC_35_HD_8;
2020-02-29 18:03:35 +00:00
2017-12-19 20:33:03 +00:00
break;
case MediaType.DOS_525_HD when mode == TransferRate.FiveHundredMfm:
2020-07-20 21:11:32 +01:00
_imageInfo.MediaType = MediaType.NEC_35_HD_15;
2020-02-29 18:03:35 +00:00
2017-12-19 20:33:03 +00:00
break;
case MediaType.RX50 when mode == TransferRate.FiveHundredMfm:
2020-07-20 21:11:32 +01:00
_imageInfo.MediaType = MediaType.ATARI_35_SS_DD;
2020-02-29 18:03:35 +00:00
2017-12-19 20:33:03 +00:00
break;
}
2020-07-20 21:11:32 +01:00
_imageInfo.XmlMediaType = XmlMediaType.BlockMedia;
2017-12-19 20:33:03 +00:00
2020-07-20 21:11:32 +01:00
AaruConsole.VerboseWriteLine("IMD image contains a disk of type {0}", _imageInfo.MediaType);
2020-02-29 18:03:35 +00:00
2020-07-20 21:11:32 +01:00
if(!string.IsNullOrEmpty(_imageInfo.Comments))
AaruConsole.VerboseWriteLine("IMD comments: {0}", _imageInfo.Comments);
2017-12-19 20:33:03 +00:00
return ErrorNumber.NoError;
2017-12-19 20:33:03 +00:00
}
2021-08-17 13:56:05 +01:00
/// <inheritdoc />
2021-09-21 04:55:28 +01:00
public ErrorNumber ReadSector(ulong sectorAddress, out byte[] buffer) =>
ReadSectors(sectorAddress, 1, out buffer);
2017-12-19 20:33:03 +00:00
2021-08-17 13:56:05 +01:00
/// <inheritdoc />
public ErrorNumber ReadSectors(ulong sectorAddress, uint length, out byte[] buffer)
2017-12-19 20:33:03 +00:00
{
buffer = null;
2021-09-21 04:55:28 +01:00
2020-07-20 21:11:32 +01:00
if(sectorAddress > _imageInfo.Sectors - 1)
return ErrorNumber.OutOfRange;
2017-12-19 20:33:03 +00:00
2020-07-20 21:11:32 +01:00
if(sectorAddress + length > _imageInfo.Sectors)
return ErrorNumber.OutOfRange;
2017-12-19 20:33:03 +00:00
var ms = new MemoryStream();
2020-02-29 18:03:35 +00:00
2017-12-19 20:33:03 +00:00
for(int i = 0; i < length; i++)
ms.Write(_sectorsData[(int)sectorAddress + i], 0, _sectorsData[(int)sectorAddress + i].Length);
2017-12-19 20:33:03 +00:00
buffer = ms.ToArray();
return ErrorNumber.NoError;
2017-12-19 20:33:03 +00:00
}
}
}