2017-08-17 02:16:34 +01:00
|
|
|
|
// /***************************************************************************
|
|
|
|
|
|
// The Disc Image Chef
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
|
//
|
2018-07-23 23:25:43 +01:00
|
|
|
|
// Filename : Read.cs
|
2017-08-17 02:16:34 +01:00
|
|
|
|
// Author(s) : Natalia Portillo <claunia@claunia.com>
|
|
|
|
|
|
//
|
2017-12-19 03:50:57 +00:00
|
|
|
|
// Component : Disk image plugins.
|
2017-08-17 02:16:34 +01:00
|
|
|
|
//
|
|
|
|
|
|
// --[ Description ] ----------------------------------------------------------
|
|
|
|
|
|
//
|
2018-07-23 23:25:43 +01:00
|
|
|
|
// 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/>.
|
|
|
|
|
|
//
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
2018-12-29 17:34:38 +00:00
|
|
|
|
// Copyright © 2011-2019 Natalia Portillo
|
2017-08-17 02:16:34 +01:00
|
|
|
|
// ****************************************************************************/
|
2017-08-17 02:17:08 +01:00
|
|
|
|
|
2017-08-17 02:16:34 +01:00
|
|
|
|
using System;
|
2017-08-17 02:17:08 +01:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
using System.IO;
|
|
|
|
|
|
using DiscImageChef.CommonTypes;
|
2018-06-25 19:08:16 +01:00
|
|
|
|
using DiscImageChef.CommonTypes.Enums;
|
|
|
|
|
|
using DiscImageChef.CommonTypes.Exceptions;
|
|
|
|
|
|
using DiscImageChef.CommonTypes.Interfaces;
|
2017-08-17 02:17:08 +01:00
|
|
|
|
using DiscImageChef.Console;
|
|
|
|
|
|
|
2017-12-20 17:15:26 +00:00
|
|
|
|
namespace DiscImageChef.DiscImages
|
2017-08-17 02:16:34 +01:00
|
|
|
|
{
|
2018-07-23 23:25:43 +01:00
|
|
|
|
public partial class Imd
|
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);
|
|
|
|
|
|
|
|
|
|
|
|
MemoryStream cmt = new MemoryStream();
|
|
|
|
|
|
stream.Seek(0x1F, SeekOrigin.Begin);
|
|
|
|
|
|
for(uint i = 0; i < stream.Length; i++)
|
|
|
|
|
|
{
|
|
|
|
|
|
byte b = (byte)stream.ReadByte();
|
|
|
|
|
|
if(b == 0x1A) break;
|
|
|
|
|
|
|
|
|
|
|
|
cmt.WriteByte(b);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2017-12-26 06:05:12 +00:00
|
|
|
|
imageInfo.Comments = StringHandlers.CToString(cmt.ToArray());
|
2017-12-28 04:57:26 +00:00
|
|
|
|
sectorsData = new List<byte[]>();
|
2017-12-19 20:33:03 +00:00
|
|
|
|
|
|
|
|
|
|
byte currentCylinder = 0;
|
2018-06-22 08:08:38 +01:00
|
|
|
|
imageInfo.Cylinders = 1;
|
|
|
|
|
|
imageInfo.Heads = 1;
|
|
|
|
|
|
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();
|
2017-12-28 04:57:26 +00:00
|
|
|
|
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;
|
2017-12-26 06:05:12 +00:00
|
|
|
|
imageInfo.Cylinders++;
|
2017-12-19 20:33:03 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
2017-12-26 06:05:12 +00:00
|
|
|
|
if((head & 1) == 1) 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);
|
2017-12-20 17:15:26 +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)
|
2017-12-28 04:57:26 +00:00
|
|
|
|
stream.Read(headmap, 0, headmap.Length);
|
2017-12-19 20:33:03 +00:00
|
|
|
|
if(n == 0xFF)
|
|
|
|
|
|
{
|
|
|
|
|
|
byte[] bpsbytes = new byte[spt * 2];
|
|
|
|
|
|
stream.Read(bpsbytes, 0, bpsbytes.Length);
|
|
|
|
|
|
for(int i = 0; i < spt; i++) bps[i] = BitConverter.ToUInt16(bpsbytes, i * 2);
|
|
|
|
|
|
}
|
2017-12-28 04:57:26 +00:00
|
|
|
|
else
|
|
|
|
|
|
for(int i = 0; i < spt; i++)
|
|
|
|
|
|
bps[i] = (ushort)(128 << n);
|
2017-12-19 20:33:03 +00:00
|
|
|
|
|
2017-12-26 06:05:12 +00:00
|
|
|
|
if(spt > imageInfo.SectorsPerTrack) imageInfo.SectorsPerTrack = spt;
|
2017-12-19 20:33:03 +00:00
|
|
|
|
|
|
|
|
|
|
SortedDictionary<byte, byte[]> track = new SortedDictionary<byte, byte[]>();
|
|
|
|
|
|
|
|
|
|
|
|
for(int i = 0; i < spt; i++)
|
|
|
|
|
|
{
|
|
|
|
|
|
SectorType type = (SectorType)stream.ReadByte();
|
2017-12-28 04:57:26 +00:00
|
|
|
|
byte[] data = new byte[bps[i]];
|
2017-12-19 20:33:03 +00:00
|
|
|
|
|
|
|
|
|
|
// TODO; Handle disks with different bps in track 0
|
2017-12-26 06:05:12 +00:00
|
|
|
|
if(bps[i] > imageInfo.SectorSize) imageInfo.SectorSize = bps[i];
|
2017-12-19 20:33:03 +00:00
|
|
|
|
|
|
|
|
|
|
switch(type)
|
|
|
|
|
|
{
|
|
|
|
|
|
case SectorType.Unavailable:
|
|
|
|
|
|
if(!track.ContainsKey(idmap[i])) track.Add(idmap[i], data);
|
|
|
|
|
|
break;
|
|
|
|
|
|
case SectorType.Normal:
|
|
|
|
|
|
case SectorType.Deleted:
|
|
|
|
|
|
case SectorType.Error:
|
|
|
|
|
|
case SectorType.DeletedError:
|
|
|
|
|
|
stream.Read(data, 0, data.Length);
|
|
|
|
|
|
if(!track.ContainsKey(idmap[i])) track.Add(idmap[i], data);
|
2017-12-26 06:05:12 +00:00
|
|
|
|
imageInfo.ImageSize += (ulong)data.Length;
|
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);
|
|
|
|
|
|
if(!track.ContainsKey(idmap[i])) track.Add(idmap[i], data);
|
|
|
|
|
|
break;
|
2017-12-24 00:12:31 +00:00
|
|
|
|
default: throw new ImageNotSupportedException($"Invalid sector type {(byte)type}");
|
2017-12-19 20:33:03 +00:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
foreach(KeyValuePair<byte, byte[]> kvp in track)
|
|
|
|
|
|
{
|
|
|
|
|
|
sectorsData.Add(kvp.Value);
|
|
|
|
|
|
currentLba++;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2017-12-26 06:05:12 +00:00
|
|
|
|
imageInfo.Application = "IMD";
|
2017-12-19 20:33:03 +00:00
|
|
|
|
// TODO: The header is the date of dump or the date of the application compilation?
|
2017-12-28 04:57:26 +00:00
|
|
|
|
imageInfo.CreationTime = imageFilter.GetCreationTime();
|
2017-12-26 06:05:12 +00:00
|
|
|
|
imageInfo.LastModificationTime = imageFilter.GetLastWriteTime();
|
2017-12-28 04:57:26 +00:00
|
|
|
|
imageInfo.MediaTitle = Path.GetFileNameWithoutExtension(imageFilter.GetFilename());
|
|
|
|
|
|
imageInfo.Comments = StringHandlers.CToString(cmt.ToArray());
|
|
|
|
|
|
imageInfo.Sectors = currentLba;
|
|
|
|
|
|
imageInfo.MediaType = MediaType.Unknown;
|
|
|
|
|
|
|
|
|
|
|
|
MediaEncoding mediaEncoding = MediaEncoding.MFM;
|
|
|
|
|
|
if(mode == TransferRate.TwoHundred || mode == TransferRate.ThreeHundred || mode == TransferRate.FiveHundred)
|
|
|
|
|
|
mediaEncoding = MediaEncoding.FM;
|
|
|
|
|
|
|
2018-06-22 08:08:38 +01:00
|
|
|
|
imageInfo.MediaType = Geometry.GetMediaType(((ushort)imageInfo.Cylinders, (byte)imageInfo.Heads,
|
2019-03-01 07:35:22 +00:00
|
|
|
|
(ushort)imageInfo.SectorsPerTrack, imageInfo.SectorSize,
|
|
|
|
|
|
mediaEncoding, false));
|
2017-12-19 20:33:03 +00:00
|
|
|
|
|
2017-12-28 04:57:26 +00:00
|
|
|
|
switch(imageInfo.MediaType)
|
2017-12-19 20:33:03 +00:00
|
|
|
|
{
|
2017-12-28 04:57:26 +00:00
|
|
|
|
case MediaType.NEC_525_HD when mode == TransferRate.FiveHundredMfm:
|
|
|
|
|
|
imageInfo.MediaType = MediaType.NEC_35_HD_8;
|
2017-12-19 20:33:03 +00:00
|
|
|
|
break;
|
2017-12-28 04:57:26 +00:00
|
|
|
|
case MediaType.DOS_525_HD when mode == TransferRate.FiveHundredMfm:
|
|
|
|
|
|
imageInfo.MediaType = MediaType.NEC_35_HD_15;
|
2017-12-19 20:33:03 +00:00
|
|
|
|
break;
|
2017-12-28 04:57:26 +00:00
|
|
|
|
case MediaType.RX50 when mode == TransferRate.FiveHundredMfm:
|
|
|
|
|
|
imageInfo.MediaType = MediaType.ATARI_35_SS_DD;
|
2017-12-19 20:33:03 +00:00
|
|
|
|
break;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2017-12-26 06:05:12 +00:00
|
|
|
|
imageInfo.XmlMediaType = XmlMediaType.BlockMedia;
|
2017-12-19 20:33:03 +00:00
|
|
|
|
|
2017-12-26 06:05:12 +00:00
|
|
|
|
DicConsole.VerboseWriteLine("IMD image contains a disk of type {0}", imageInfo.MediaType);
|
|
|
|
|
|
if(!string.IsNullOrEmpty(imageInfo.Comments))
|
|
|
|
|
|
DicConsole.VerboseWriteLine("IMD comments: {0}", imageInfo.Comments);
|
2017-12-19 20:33:03 +00:00
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2018-12-31 13:17:27 +00:00
|
|
|
|
public byte[] ReadSector(ulong sectorAddress) => ReadSectors(sectorAddress, 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
|
|
|
|
{
|
2017-12-26 06:05:12 +00:00
|
|
|
|
if(sectorAddress > imageInfo.Sectors - 1)
|
2017-12-19 20:33:03 +00:00
|
|
|
|
throw new ArgumentOutOfRangeException(nameof(sectorAddress), "Sector address not found");
|
|
|
|
|
|
|
2017-12-26 06:05:12 +00:00
|
|
|
|
if(sectorAddress + length > imageInfo.Sectors)
|
2017-12-19 20:33:03 +00:00
|
|
|
|
throw new ArgumentOutOfRangeException(nameof(length), "Requested more sectors than available");
|
|
|
|
|
|
|
|
|
|
|
|
MemoryStream buffer = new MemoryStream();
|
|
|
|
|
|
for(int i = 0; i < length; i++)
|
|
|
|
|
|
buffer.Write(sectorsData[(int)sectorAddress + i], 0, sectorsData[(int)sectorAddress + i].Length);
|
|
|
|
|
|
|
|
|
|
|
|
return buffer.ToArray();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|