2017-05-19 20:28:49 +01:00
|
|
|
// /***************************************************************************
|
2016-08-26 01:43:15 +01:00
|
|
|
// The Disc Image Chef
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
//
|
|
|
|
|
// Filename : CPCDSK.cs
|
|
|
|
|
// Author(s) : Natalia Portillo <claunia@claunia.com>
|
|
|
|
|
//
|
2017-12-19 03:50:57 +00:00
|
|
|
// Component : Disk image plugins.
|
2016-08-26 01:43:15 +01:00
|
|
|
//
|
|
|
|
|
// --[ Description ] ----------------------------------------------------------
|
|
|
|
|
//
|
2017-12-19 03:50:57 +00:00
|
|
|
// Manages CPCEMU disk images.
|
2016-08-26 01:43:15 +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/>.
|
|
|
|
|
//
|
|
|
|
|
// ----------------------------------------------------------------------------
|
2017-12-19 03:50:57 +00:00
|
|
|
// Copyright © 2011-2018 Natalia Portillo
|
2016-08-26 01:43:15 +01:00
|
|
|
// ****************************************************************************/
|
2016-08-26 01:45:58 +01:00
|
|
|
|
2016-08-26 01:43:15 +01:00
|
|
|
using System;
|
2016-08-26 01:45:58 +01:00
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Runtime.InteropServices;
|
|
|
|
|
using DiscImageChef.Checksums;
|
|
|
|
|
using DiscImageChef.CommonTypes;
|
|
|
|
|
using DiscImageChef.Console;
|
|
|
|
|
using DiscImageChef.Decoders.Floppy;
|
2016-09-05 17:37:31 +01:00
|
|
|
using DiscImageChef.Filters;
|
2016-08-26 01:45:58 +01:00
|
|
|
|
2017-12-20 17:15:26 +00:00
|
|
|
namespace DiscImageChef.DiscImages
|
2016-08-26 01:43:15 +01:00
|
|
|
{
|
2017-12-26 06:05:12 +00:00
|
|
|
public class Cpcdsk : IMediaImage
|
2016-08-26 01:43:15 +01:00
|
|
|
{
|
2016-08-26 01:45:58 +01:00
|
|
|
/// <summary>
|
2017-12-24 00:12:31 +00:00
|
|
|
/// Identifier for CPCEMU disk images, "MV - CPCEMU Disk-File"
|
2016-08-26 01:45:58 +01:00
|
|
|
/// </summary>
|
2017-12-20 17:15:26 +00:00
|
|
|
readonly byte[] cpcdskId =
|
2017-12-19 20:33:03 +00:00
|
|
|
{
|
|
|
|
|
0x4D, 0x56, 0x20, 0x2D, 0x20, 0x43, 0x50, 0x43, 0x45, 0x4D, 0x55, 0x20, 0x44, 0x69, 0x73, 0x6B, 0x2D, 0x46,
|
|
|
|
|
0x69, 0x6C, 0x65
|
|
|
|
|
};
|
|
|
|
|
/// <summary>
|
2017-12-24 00:12:31 +00:00
|
|
|
/// Identifier for DU54 disk images, "MV - CPC format Disk Image (DU54)"
|
2017-12-19 20:33:03 +00:00
|
|
|
/// </summary>
|
2017-12-20 17:15:26 +00:00
|
|
|
readonly byte[] du54Id =
|
2017-12-19 20:33:03 +00:00
|
|
|
{
|
|
|
|
|
0x4D, 0x56, 0x20, 0x2D, 0x20, 0x43, 0x50, 0x43, 0x20, 0x66, 0x6F, 0x72, 0x6D, 0x61, 0x74, 0x20, 0x44, 0x69,
|
|
|
|
|
0x73, 0x6B, 0x20
|
|
|
|
|
};
|
|
|
|
|
/// <summary>
|
2017-12-24 00:12:31 +00:00
|
|
|
/// Identifier for Extended CPCEMU disk images, "EXTENDED CPC DSK File"
|
2017-12-19 20:33:03 +00:00
|
|
|
/// </summary>
|
2017-12-20 17:15:26 +00:00
|
|
|
readonly byte[] edskId =
|
2017-12-19 20:33:03 +00:00
|
|
|
{
|
|
|
|
|
0x45, 0x58, 0x54, 0x45, 0x4E, 0x44, 0x45, 0x44, 0x20, 0x43, 0x50, 0x43, 0x20, 0x44, 0x53, 0x4B, 0x20, 0x46,
|
|
|
|
|
0x69, 0x6C, 0x65
|
|
|
|
|
};
|
2016-08-26 01:45:58 +01:00
|
|
|
/// <summary>
|
2017-12-24 00:12:31 +00:00
|
|
|
/// Identifier for track information, "Track-Info\r\n"
|
2016-08-26 01:45:58 +01:00
|
|
|
/// </summary>
|
2017-12-20 17:15:26 +00:00
|
|
|
readonly byte[] trackId = {0x54, 0x72, 0x61, 0x63, 0x6B, 0x2D, 0x49, 0x6E, 0x66, 0x6F};
|
2017-12-24 00:12:31 +00:00
|
|
|
Dictionary<ulong, byte[]> addressMarks;
|
2016-08-26 01:45:58 +01:00
|
|
|
|
|
|
|
|
bool extended;
|
2017-12-26 06:05:12 +00:00
|
|
|
ImageInfo imageInfo;
|
2016-08-26 01:45:58 +01:00
|
|
|
Dictionary<ulong, byte[]> sectors;
|
|
|
|
|
|
2017-12-20 17:15:26 +00:00
|
|
|
public Cpcdsk()
|
2016-08-26 01:43:15 +01:00
|
|
|
{
|
2017-12-26 06:05:12 +00:00
|
|
|
imageInfo = new ImageInfo
|
2017-12-22 06:55:04 +00:00
|
|
|
{
|
|
|
|
|
ReadableSectorTags = new List<SectorTagType>(),
|
|
|
|
|
ReadableMediaTags = new List<MediaTagType>(),
|
2017-12-26 02:51:10 +00:00
|
|
|
HasPartitions = false,
|
|
|
|
|
HasSessions = false,
|
|
|
|
|
Version = null,
|
|
|
|
|
Application = null,
|
|
|
|
|
ApplicationVersion = null,
|
|
|
|
|
Creator = null,
|
|
|
|
|
Comments = null,
|
2017-12-22 06:55:04 +00:00
|
|
|
MediaManufacturer = null,
|
|
|
|
|
MediaModel = null,
|
|
|
|
|
MediaSerialNumber = null,
|
|
|
|
|
MediaBarcode = null,
|
|
|
|
|
MediaPartNumber = null,
|
|
|
|
|
MediaSequence = 0,
|
|
|
|
|
LastMediaSequence = 0,
|
|
|
|
|
DriveManufacturer = null,
|
|
|
|
|
DriveModel = null,
|
|
|
|
|
DriveSerialNumber = null,
|
|
|
|
|
DriveFirmwareRevision = null
|
|
|
|
|
};
|
2016-08-26 01:45:58 +01:00
|
|
|
}
|
|
|
|
|
|
2017-12-26 07:28:40 +00:00
|
|
|
public ImageInfo Info => imageInfo;
|
2017-12-26 02:51:10 +00:00
|
|
|
|
2017-12-26 07:28:40 +00:00
|
|
|
public string Name => "CPCEMU Disk-File and Extended CPC Disk-File";
|
|
|
|
|
public Guid Id => new Guid("724B16CC-ADB9-492E-BA07-CAEEC1012B16");
|
2017-12-26 06:05:12 +00:00
|
|
|
|
2017-12-26 07:28:40 +00:00
|
|
|
public string ImageFormat => extended ? "CPCEMU Extended disk image" : "CPCEMU disk image";
|
2017-12-26 06:05:12 +00:00
|
|
|
|
2017-12-26 07:28:40 +00:00
|
|
|
public List<Partition> Partitions =>
|
2017-12-26 02:51:10 +00:00
|
|
|
throw new FeatureUnsupportedImageException("Feature not supported by image format");
|
|
|
|
|
|
2017-12-26 07:28:40 +00:00
|
|
|
public List<Track> Tracks =>
|
2017-12-26 02:51:10 +00:00
|
|
|
throw new FeatureUnsupportedImageException("Feature not supported by image format");
|
|
|
|
|
|
2017-12-26 07:28:40 +00:00
|
|
|
public List<Session> Sessions =>
|
2017-12-26 02:51:10 +00:00
|
|
|
throw new FeatureUnsupportedImageException("Feature not supported by image format");
|
|
|
|
|
|
2017-12-26 07:28:40 +00:00
|
|
|
public bool IdentifyImage(IFilter imageFilter)
|
2016-08-26 01:45:58 +01:00
|
|
|
{
|
2016-09-05 17:37:31 +01:00
|
|
|
Stream stream = imageFilter.GetDataForkStream();
|
2016-08-26 01:45:58 +01:00
|
|
|
stream.Seek(0, SeekOrigin.Begin);
|
|
|
|
|
|
2017-12-19 20:33:03 +00:00
|
|
|
if(stream.Length < 512) return false;
|
2016-08-26 01:45:58 +01:00
|
|
|
|
2017-12-20 17:15:26 +00:00
|
|
|
byte[] headerB = new byte[256];
|
|
|
|
|
stream.Read(headerB, 0, 256);
|
2016-08-26 01:45:58 +01:00
|
|
|
IntPtr headerPtr = Marshal.AllocHGlobal(256);
|
2017-12-20 17:15:26 +00:00
|
|
|
Marshal.Copy(headerB, 0, headerPtr, 256);
|
2017-12-22 06:55:04 +00:00
|
|
|
CpcDiskInfo header = (CpcDiskInfo)Marshal.PtrToStructure(headerPtr, typeof(CpcDiskInfo));
|
2016-08-26 01:45:58 +01:00
|
|
|
Marshal.FreeHGlobal(headerPtr);
|
|
|
|
|
|
2017-12-19 20:33:03 +00:00
|
|
|
DicConsole.DebugWriteLine("CPCDSK plugin", "header.magic = \"{0}\"",
|
|
|
|
|
StringHandlers.CToString(header.magic));
|
2016-08-26 01:45:58 +01:00
|
|
|
|
2017-12-20 17:15:26 +00:00
|
|
|
return cpcdskId.SequenceEqual(header.magic) || edskId.SequenceEqual(header.magic) ||
|
|
|
|
|
du54Id.SequenceEqual(header.magic);
|
2016-08-26 01:45:58 +01:00
|
|
|
}
|
|
|
|
|
|
2017-12-26 07:28:40 +00:00
|
|
|
public bool OpenImage(IFilter imageFilter)
|
2016-08-26 01:45:58 +01:00
|
|
|
{
|
2016-09-05 17:37:31 +01:00
|
|
|
Stream stream = imageFilter.GetDataForkStream();
|
2016-08-26 01:45:58 +01:00
|
|
|
stream.Seek(0, SeekOrigin.Begin);
|
|
|
|
|
|
2017-12-19 20:33:03 +00:00
|
|
|
if(stream.Length < 512) return false;
|
2016-08-26 01:45:58 +01:00
|
|
|
|
2017-12-20 17:15:26 +00:00
|
|
|
byte[] headerB = new byte[256];
|
|
|
|
|
stream.Read(headerB, 0, 256);
|
2016-08-26 01:45:58 +01:00
|
|
|
IntPtr headerPtr = Marshal.AllocHGlobal(256);
|
2017-12-20 17:15:26 +00:00
|
|
|
Marshal.Copy(headerB, 0, headerPtr, 256);
|
2017-12-22 06:55:04 +00:00
|
|
|
CpcDiskInfo header = (CpcDiskInfo)Marshal.PtrToStructure(headerPtr, typeof(CpcDiskInfo));
|
2016-08-26 01:45:58 +01:00
|
|
|
Marshal.FreeHGlobal(headerPtr);
|
|
|
|
|
|
2017-12-20 17:15:26 +00:00
|
|
|
if(!cpcdskId.SequenceEqual(header.magic) && !edskId.SequenceEqual(header.magic) &&
|
|
|
|
|
!du54Id.SequenceEqual(header.magic)) return false;
|
2016-08-26 01:45:58 +01:00
|
|
|
|
2017-12-20 17:15:26 +00:00
|
|
|
extended = edskId.SequenceEqual(header.magic);
|
2016-08-26 01:45:58 +01:00
|
|
|
DicConsole.DebugWriteLine("CPCDSK plugin", "Extended = {0}", extended);
|
2017-12-19 20:33:03 +00:00
|
|
|
DicConsole.DebugWriteLine("CPCDSK plugin", "header.magic = \"{0}\"",
|
|
|
|
|
StringHandlers.CToString(header.magic));
|
|
|
|
|
DicConsole.DebugWriteLine("CPCDSK plugin", "header.magic2 = \"{0}\"",
|
|
|
|
|
StringHandlers.CToString(header.magic2));
|
|
|
|
|
DicConsole.DebugWriteLine("CPCDSK plugin", "header.creator = \"{0}\"",
|
|
|
|
|
StringHandlers.CToString(header.creator));
|
2016-08-26 01:45:58 +01:00
|
|
|
DicConsole.DebugWriteLine("CPCDSK plugin", "header.tracks = {0}", header.tracks);
|
|
|
|
|
DicConsole.DebugWriteLine("CPCDSK plugin", "header.sides = {0}", header.sides);
|
2017-12-19 20:33:03 +00:00
|
|
|
if(!extended) DicConsole.DebugWriteLine("CPCDSK plugin", "header.tracksize = {0}", header.tracksize);
|
2016-08-26 01:45:58 +01:00
|
|
|
else
|
|
|
|
|
for(int i = 0; i < header.tracks; i++)
|
|
|
|
|
{
|
|
|
|
|
for(int j = 0; j < header.sides; j++)
|
2017-12-19 20:33:03 +00:00
|
|
|
DicConsole.DebugWriteLine("CPCDSK plugin", "Track {0} Side {1} size = {2}", i, j,
|
|
|
|
|
header.tracksizeTable[i * header.sides + j] * 256);
|
2016-08-26 01:45:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ulong currentSector = 0;
|
|
|
|
|
sectors = new Dictionary<ulong, byte[]>();
|
|
|
|
|
addressMarks = new Dictionary<ulong, byte[]>();
|
|
|
|
|
ulong readtracks = 0;
|
|
|
|
|
bool allTracksSameSize = true;
|
|
|
|
|
ulong sectorsPerTrack = 0;
|
|
|
|
|
|
|
|
|
|
// Seek to first track descriptor
|
|
|
|
|
stream.Seek(256, SeekOrigin.Begin);
|
|
|
|
|
for(int i = 0; i < header.tracks; i++)
|
|
|
|
|
{
|
|
|
|
|
for(int j = 0; j < header.sides; j++)
|
|
|
|
|
{
|
|
|
|
|
// Track not stored in image
|
2017-12-19 20:33:03 +00:00
|
|
|
if(extended && header.tracksizeTable[i * header.sides + j] == 0) continue;
|
2016-08-26 01:45:58 +01:00
|
|
|
|
|
|
|
|
long trackPos = stream.Position;
|
|
|
|
|
|
2017-12-20 17:15:26 +00:00
|
|
|
byte[] trackB = new byte[256];
|
|
|
|
|
stream.Read(trackB, 0, 256);
|
2016-08-26 01:45:58 +01:00
|
|
|
IntPtr trackPtr = Marshal.AllocHGlobal(256);
|
2017-12-20 17:15:26 +00:00
|
|
|
Marshal.Copy(trackB, 0, trackPtr, 256);
|
2017-12-22 06:55:04 +00:00
|
|
|
CpcTrackInfo trackInfo = (CpcTrackInfo)Marshal.PtrToStructure(trackPtr, typeof(CpcTrackInfo));
|
2016-08-26 01:45:58 +01:00
|
|
|
Marshal.FreeHGlobal(trackPtr);
|
|
|
|
|
|
2017-12-20 17:15:26 +00:00
|
|
|
if(!trackId.SequenceEqual(trackInfo.magic))
|
2016-08-26 01:45:58 +01:00
|
|
|
{
|
|
|
|
|
DicConsole.ErrorWriteLine("Not the expected track info.");
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2017-12-19 20:33:03 +00:00
|
|
|
DicConsole.DebugWriteLine("CPCDSK plugin", "trackInfo[{1}:{2}].magic = \"{0}\"",
|
|
|
|
|
StringHandlers.CToString(trackInfo.magic), i, j);
|
|
|
|
|
DicConsole.DebugWriteLine("CPCDSK plugin", "trackInfo[{1}:{2}].bps = {0}",
|
|
|
|
|
SizeCodeToBytes(trackInfo.bps), i, j);
|
|
|
|
|
DicConsole.DebugWriteLine("CPCDSK plugin", "trackInfo[{1}:{2}].dataRate = {0}", trackInfo.dataRate,
|
|
|
|
|
i, j);
|
|
|
|
|
DicConsole.DebugWriteLine("CPCDSK plugin", "trackInfo[{1}:{2}].filler = 0x{0:X2}", trackInfo.filler,
|
|
|
|
|
i, j);
|
|
|
|
|
DicConsole.DebugWriteLine("CPCDSK plugin", "trackInfo[{1}:{2}].gap3 = 0x{0:X2}", trackInfo.gap3, i,
|
|
|
|
|
j);
|
|
|
|
|
DicConsole.DebugWriteLine("CPCDSK plugin", "trackInfo[{1}:{2}].padding = {0}", trackInfo.padding, i,
|
|
|
|
|
j);
|
|
|
|
|
DicConsole.DebugWriteLine("CPCDSK plugin", "trackInfo[{1}:{2}].recordingMode = {0}",
|
|
|
|
|
trackInfo.recordingMode, i, j);
|
|
|
|
|
DicConsole.DebugWriteLine("CPCDSK plugin", "trackInfo[{1}:{2}].sectors = {0}", trackInfo.sectors, i,
|
|
|
|
|
j);
|
2016-08-26 01:45:58 +01:00
|
|
|
DicConsole.DebugWriteLine("CPCDSK plugin", "trackInfo[{1}:{2}].side = {0}", trackInfo.side, i, j);
|
|
|
|
|
DicConsole.DebugWriteLine("CPCDSK plugin", "trackInfo[{1}:{2}].track = {0}", trackInfo.track, i, j);
|
|
|
|
|
|
|
|
|
|
if(trackInfo.sectors != sectorsPerTrack)
|
2017-12-19 20:33:03 +00:00
|
|
|
if(sectorsPerTrack == 0) sectorsPerTrack = trackInfo.sectors;
|
|
|
|
|
else allTracksSameSize = false;
|
2016-08-26 01:45:58 +01:00
|
|
|
|
|
|
|
|
byte[][] thisTrackSectors = new byte[trackInfo.sectors][];
|
|
|
|
|
byte[][] thisTrackAddressMarks = new byte[trackInfo.sectors][];
|
|
|
|
|
|
|
|
|
|
for(int k = 1; k <= trackInfo.sectors; k++)
|
|
|
|
|
{
|
2017-12-19 20:33:03 +00:00
|
|
|
DicConsole.DebugWriteLine("CPCDSK plugin", "trackInfo[{1}:{2}].sector[{3}].id = 0x{0:X2}",
|
|
|
|
|
trackInfo.sectorsInfo[k - 1].id, i, j, k);
|
|
|
|
|
DicConsole.DebugWriteLine("CPCDSK plugin", "trackInfo[{1}:{2}].sector[{3}].len = {0}",
|
|
|
|
|
trackInfo.sectorsInfo[k - 1].len, i, j, k);
|
|
|
|
|
DicConsole.DebugWriteLine("CPCDSK plugin", "trackInfo[{1}:{2}].sector[{3}].side = {0}",
|
|
|
|
|
trackInfo.sectorsInfo[k - 1].side, i, j, k);
|
|
|
|
|
DicConsole.DebugWriteLine("CPCDSK plugin", "trackInfo[{1}:{2}].sector[{3}].size = {0}",
|
|
|
|
|
SizeCodeToBytes(trackInfo.sectorsInfo[k - 1].size), i, j, k);
|
|
|
|
|
DicConsole.DebugWriteLine("CPCDSK plugin", "trackInfo[{1}:{2}].sector[{3}].st1 = 0x{0:X2}",
|
|
|
|
|
trackInfo.sectorsInfo[k - 1].st1, i, j, k);
|
|
|
|
|
DicConsole.DebugWriteLine("CPCDSK plugin", "trackInfo[{1}:{2}].sector[{3}].st2 = 0x{0:X2}",
|
|
|
|
|
trackInfo.sectorsInfo[k - 1].st2, i, j, k);
|
|
|
|
|
DicConsole.DebugWriteLine("CPCDSK plugin", "trackInfo[{1}:{2}].sector[{3}].track = {0}",
|
|
|
|
|
trackInfo.sectorsInfo[k - 1].track, i, j, k);
|
2016-08-26 01:45:58 +01:00
|
|
|
|
|
|
|
|
int sectLen;
|
2017-12-24 00:12:31 +00:00
|
|
|
sectLen = extended
|
|
|
|
|
? trackInfo.sectorsInfo[k - 1].len
|
|
|
|
|
: SizeCodeToBytes(trackInfo.sectorsInfo[k - 1].size);
|
2016-08-26 01:45:58 +01:00
|
|
|
|
|
|
|
|
byte[] sector = new byte[sectLen];
|
|
|
|
|
stream.Read(sector, 0, sectLen);
|
|
|
|
|
|
|
|
|
|
if(sectLen < SizeCodeToBytes(trackInfo.sectorsInfo[k - 1].size))
|
|
|
|
|
{
|
|
|
|
|
byte[] temp = new byte[SizeCodeToBytes(trackInfo.sectorsInfo[k - 1].size)];
|
|
|
|
|
Array.Copy(sector, 0, temp, 0, sector.Length);
|
|
|
|
|
sector = temp;
|
|
|
|
|
}
|
|
|
|
|
else if(sectLen > SizeCodeToBytes(trackInfo.sectorsInfo[k - 1].size))
|
|
|
|
|
{
|
|
|
|
|
byte[] temp = new byte[SizeCodeToBytes(trackInfo.sectorsInfo[k - 1].size)];
|
|
|
|
|
Array.Copy(sector, 0, temp, 0, temp.Length);
|
|
|
|
|
sector = temp;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
thisTrackSectors[(trackInfo.sectorsInfo[k - 1].id & 0x3F) - 1] = sector;
|
|
|
|
|
|
|
|
|
|
byte[] amForCrc = new byte[8];
|
|
|
|
|
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;
|
|
|
|
|
|
2017-12-22 06:55:04 +00:00
|
|
|
Crc16Context.Data(amForCrc, 8, out byte[] amCrc);
|
2016-08-26 01:45:58 +01:00
|
|
|
|
|
|
|
|
byte[] addressMark = new byte[22];
|
|
|
|
|
Array.Copy(amForCrc, 0, addressMark, 12, 8);
|
|
|
|
|
Array.Copy(amCrc, 0, addressMark, 20, 2);
|
|
|
|
|
|
|
|
|
|
thisTrackAddressMarks[(trackInfo.sectorsInfo[k - 1].id & 0x3F) - 1] = addressMark;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for(int s = 0; s < thisTrackSectors.Length; s++)
|
|
|
|
|
{
|
|
|
|
|
sectors.Add(currentSector, thisTrackSectors[s]);
|
|
|
|
|
addressMarks.Add(currentSector, thisTrackAddressMarks[s]);
|
|
|
|
|
currentSector++;
|
2017-12-26 06:05:12 +00:00
|
|
|
if(thisTrackSectors[s].Length > imageInfo.SectorSize)
|
|
|
|
|
imageInfo.SectorSize = (uint)thisTrackSectors[s].Length;
|
2016-08-26 01:45:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
stream.Seek(trackPos, SeekOrigin.Begin);
|
|
|
|
|
if(extended)
|
|
|
|
|
{
|
|
|
|
|
stream.Seek(header.tracksizeTable[i * header.sides + j] * 256, SeekOrigin.Current);
|
2017-12-26 06:05:12 +00:00
|
|
|
imageInfo.ImageSize += (ulong)(header.tracksizeTable[i * header.sides + j] * 256) - 256;
|
2016-08-26 01:45:58 +01:00
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
stream.Seek(header.tracksize, SeekOrigin.Current);
|
2017-12-26 06:05:12 +00:00
|
|
|
imageInfo.ImageSize += (ulong)header.tracksize - 256;
|
2016-08-26 01:45:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
readtracks++;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
DicConsole.DebugWriteLine("CPCDSK plugin", "Read {0} sectors", sectors.Count);
|
|
|
|
|
DicConsole.DebugWriteLine("CPCDSK plugin", "Read {0} tracks", readtracks);
|
|
|
|
|
DicConsole.DebugWriteLine("CPCDSK plugin", "All tracks are same size? {0}", allTracksSameSize);
|
|
|
|
|
|
2017-12-26 06:05:12 +00:00
|
|
|
imageInfo.Application = StringHandlers.CToString(header.creator);
|
|
|
|
|
imageInfo.CreationTime = imageFilter.GetCreationTime();
|
|
|
|
|
imageInfo.LastModificationTime = imageFilter.GetLastWriteTime();
|
|
|
|
|
imageInfo.MediaTitle = Path.GetFileNameWithoutExtension(imageFilter.GetFilename());
|
|
|
|
|
imageInfo.Sectors = (ulong)sectors.Count;
|
|
|
|
|
imageInfo.XmlMediaType = XmlMediaType.BlockMedia;
|
|
|
|
|
imageInfo.MediaType = MediaType.CompactFloppy;
|
|
|
|
|
imageInfo.ReadableSectorTags.Add(SectorTagType.FloppyAddressMark);
|
2016-08-26 01:45:58 +01:00
|
|
|
|
2017-12-19 20:33:03 +00:00
|
|
|
// Debug writing full disk as raw
|
|
|
|
|
/*
|
|
|
|
|
FileStream foo = new FileStream(Path.GetFileNameWithoutExtension(imageFilter.GetFilename()) + ".bin", FileMode.Create);
|
2016-08-26 01:45:58 +01:00
|
|
|
for(ulong i = 0; i < (ulong)sectors.Count; i++)
|
|
|
|
|
{
|
|
|
|
|
byte[] foob;
|
|
|
|
|
sectors.TryGetValue(i, out foob);
|
|
|
|
|
foo.Write(foob, 0, foob.Length);
|
|
|
|
|
}
|
|
|
|
|
foo.Close();
|
2016-09-05 17:37:31 +01:00
|
|
|
*/
|
2016-08-26 01:45:58 +01:00
|
|
|
|
2017-12-26 06:05:12 +00:00
|
|
|
imageInfo.Cylinders = header.tracks;
|
|
|
|
|
imageInfo.Heads = header.sides;
|
|
|
|
|
imageInfo.SectorsPerTrack = (uint)(imageInfo.Sectors / (imageInfo.Cylinders * imageInfo.Heads));
|
2017-08-02 23:01:11 +01:00
|
|
|
|
2017-12-19 20:33:03 +00:00
|
|
|
return true;
|
2016-08-26 01:45:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int SizeCodeToBytes(IBMSectorSizeCode code)
|
|
|
|
|
{
|
|
|
|
|
switch(code)
|
|
|
|
|
{
|
2017-12-19 20:33:03 +00:00
|
|
|
case IBMSectorSizeCode.EighthKilo: return 128;
|
|
|
|
|
case IBMSectorSizeCode.QuarterKilo: return 256;
|
|
|
|
|
case IBMSectorSizeCode.HalfKilo: return 512;
|
|
|
|
|
case IBMSectorSizeCode.Kilo: return 1024;
|
|
|
|
|
case IBMSectorSizeCode.TwiceKilo: return 2048;
|
|
|
|
|
case IBMSectorSizeCode.FriceKilo: return 4096;
|
|
|
|
|
case IBMSectorSizeCode.TwiceFriceKilo: return 8192;
|
|
|
|
|
case IBMSectorSizeCode.FricelyFriceKilo: return 16384;
|
|
|
|
|
default: return 0;
|
2016-08-26 01:45:58 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-12-26 07:28:40 +00:00
|
|
|
public byte[] ReadSector(ulong sectorAddress)
|
2016-08-26 01:45:58 +01:00
|
|
|
{
|
2017-12-22 06:55:04 +00:00
|
|
|
if(sectors.TryGetValue(sectorAddress, out byte[] sector)) return sector;
|
2017-12-19 20:33:03 +00:00
|
|
|
|
2017-12-21 17:58:51 +00:00
|
|
|
throw new ArgumentOutOfRangeException(nameof(sectorAddress), $"Sector address {sectorAddress} not found");
|
2016-08-26 01:45:58 +01:00
|
|
|
}
|
|
|
|
|
|
2017-12-26 07:28:40 +00:00
|
|
|
public byte[] ReadSectors(ulong sectorAddress, uint length)
|
2016-08-26 01:45:58 +01: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),
|
2017-12-21 17:58:51 +00:00
|
|
|
$"Sector address {sectorAddress} not found");
|
2016-08-26 01:45:58 +01:00
|
|
|
|
2017-12-26 06:05:12 +00:00
|
|
|
if(sectorAddress + length > imageInfo.Sectors)
|
2016-08-26 01:45:58 +01:00
|
|
|
throw new ArgumentOutOfRangeException(nameof(length), "Requested more sectors than available");
|
|
|
|
|
|
|
|
|
|
MemoryStream ms = new MemoryStream();
|
|
|
|
|
|
|
|
|
|
for(uint i = 0; i < length; i++)
|
|
|
|
|
{
|
|
|
|
|
byte[] sector = ReadSector(sectorAddress + i);
|
|
|
|
|
ms.Write(sector, 0, sector.Length);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return ms.ToArray();
|
|
|
|
|
}
|
|
|
|
|
|
2017-12-26 07:28:40 +00:00
|
|
|
public byte[] ReadSectorTag(ulong sectorAddress, SectorTagType tag)
|
2016-08-26 01:45:58 +01:00
|
|
|
{
|
|
|
|
|
if(tag != SectorTagType.FloppyAddressMark)
|
2017-12-21 17:58:51 +00:00
|
|
|
throw new FeatureUnsupportedImageException($"Tag {tag} not supported by image format");
|
2016-08-26 01:45:58 +01:00
|
|
|
|
2017-12-22 06:55:04 +00:00
|
|
|
if(addressMarks.TryGetValue(sectorAddress, out byte[] addressMark)) return addressMark;
|
2016-08-26 01:45:58 +01:00
|
|
|
|
|
|
|
|
throw new ArgumentOutOfRangeException(nameof(sectorAddress), "Sector address not found");
|
|
|
|
|
}
|
|
|
|
|
|
2017-12-26 07:28:40 +00:00
|
|
|
public byte[] ReadSectorsTag(ulong sectorAddress, uint length, SectorTagType tag)
|
2016-08-26 01:45:58 +01:00
|
|
|
{
|
|
|
|
|
if(tag != SectorTagType.FloppyAddressMark)
|
2017-12-21 17:58:51 +00:00
|
|
|
throw new FeatureUnsupportedImageException($"Tag {tag} not supported by image format");
|
2016-08-26 01:45:58 +01: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),
|
2017-12-21 17:58:51 +00:00
|
|
|
$"Sector address {sectorAddress} not found");
|
2016-08-26 01:45:58 +01:00
|
|
|
|
2017-12-26 06:05:12 +00:00
|
|
|
if(sectorAddress + length > imageInfo.Sectors)
|
2016-08-26 01:45:58 +01:00
|
|
|
throw new ArgumentOutOfRangeException(nameof(length), "Requested more sectors than available");
|
|
|
|
|
|
|
|
|
|
MemoryStream ms = new MemoryStream();
|
|
|
|
|
|
|
|
|
|
for(uint i = 0; i < length; i++)
|
|
|
|
|
{
|
|
|
|
|
byte[] adddressMark = ReadSector(sectorAddress + i);
|
|
|
|
|
ms.Write(adddressMark, 0, adddressMark.Length);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return ms.ToArray();
|
|
|
|
|
}
|
|
|
|
|
|
2017-12-26 07:28:40 +00:00
|
|
|
public byte[] ReadDiskTag(MediaTagType tag)
|
2016-08-26 01:45:58 +01:00
|
|
|
{
|
|
|
|
|
throw new FeatureUnsupportedImageException("Feature not supported by image format");
|
|
|
|
|
}
|
|
|
|
|
|
2017-12-26 07:28:40 +00:00
|
|
|
public byte[] ReadSector(ulong sectorAddress, uint track)
|
2016-08-26 01:45:58 +01:00
|
|
|
{
|
|
|
|
|
throw new FeatureUnsupportedImageException("Feature not supported by image format");
|
|
|
|
|
}
|
|
|
|
|
|
2017-12-26 07:28:40 +00:00
|
|
|
public byte[] ReadSectorTag(ulong sectorAddress, uint track, SectorTagType tag)
|
2016-08-26 01:45:58 +01:00
|
|
|
{
|
|
|
|
|
throw new FeatureUnsupportedImageException("Feature not supported by image format");
|
|
|
|
|
}
|
|
|
|
|
|
2017-12-26 07:28:40 +00:00
|
|
|
public byte[] ReadSectors(ulong sectorAddress, uint length, uint track)
|
2016-08-26 01:45:58 +01:00
|
|
|
{
|
|
|
|
|
throw new FeatureUnsupportedImageException("Feature not supported by image format");
|
|
|
|
|
}
|
|
|
|
|
|
2017-12-26 07:28:40 +00:00
|
|
|
public byte[] ReadSectorsTag(ulong sectorAddress, uint length, uint track, SectorTagType tag)
|
2016-08-26 01:45:58 +01:00
|
|
|
{
|
|
|
|
|
throw new FeatureUnsupportedImageException("Feature not supported by image format");
|
|
|
|
|
}
|
|
|
|
|
|
2017-12-26 07:28:40 +00:00
|
|
|
public byte[] ReadSectorLong(ulong sectorAddress)
|
2016-08-26 01:45:58 +01:00
|
|
|
{
|
|
|
|
|
throw new FeatureUnsupportedImageException("Feature not supported by image format");
|
|
|
|
|
}
|
|
|
|
|
|
2017-12-26 07:28:40 +00:00
|
|
|
public byte[] ReadSectorLong(ulong sectorAddress, uint track)
|
2016-08-26 01:45:58 +01:00
|
|
|
{
|
|
|
|
|
throw new FeatureUnsupportedImageException("Feature not supported by image format");
|
|
|
|
|
}
|
|
|
|
|
|
2017-12-26 07:28:40 +00:00
|
|
|
public byte[] ReadSectorsLong(ulong sectorAddress, uint length)
|
2016-08-26 01:45:58 +01:00
|
|
|
{
|
|
|
|
|
throw new FeatureUnsupportedImageException("Feature not supported by image format");
|
|
|
|
|
}
|
|
|
|
|
|
2017-12-26 07:28:40 +00:00
|
|
|
public byte[] ReadSectorsLong(ulong sectorAddress, uint length, uint track)
|
2016-08-26 01:45:58 +01:00
|
|
|
{
|
|
|
|
|
throw new FeatureUnsupportedImageException("Feature not supported by image format");
|
|
|
|
|
}
|
|
|
|
|
|
2017-12-26 07:28:40 +00:00
|
|
|
public List<Track> GetSessionTracks(Session session)
|
2016-08-26 01:45:58 +01:00
|
|
|
{
|
|
|
|
|
throw new FeatureUnsupportedImageException("Feature not supported by image format");
|
|
|
|
|
}
|
|
|
|
|
|
2017-12-26 07:28:40 +00:00
|
|
|
public List<Track> GetSessionTracks(ushort session)
|
2016-08-26 01:45:58 +01:00
|
|
|
{
|
|
|
|
|
throw new FeatureUnsupportedImageException("Feature not supported by image format");
|
|
|
|
|
}
|
|
|
|
|
|
2017-12-26 07:28:40 +00:00
|
|
|
public bool? VerifySector(ulong sectorAddress)
|
2016-08-26 01:45:58 +01:00
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
2017-12-26 07:28:40 +00:00
|
|
|
public bool? VerifySector(ulong sectorAddress, uint track)
|
2016-08-26 01:45:58 +01:00
|
|
|
{
|
|
|
|
|
throw new FeatureUnsupportedImageException("Feature not supported by image format");
|
|
|
|
|
}
|
|
|
|
|
|
2017-12-26 07:28:40 +00:00
|
|
|
public bool? VerifySectors(ulong sectorAddress, uint length, out List<ulong> failingLbas,
|
2017-12-20 17:15:26 +00:00
|
|
|
out List<ulong> unknownLbas)
|
2016-08-26 01:45:58 +01:00
|
|
|
{
|
2017-12-20 17:15:26 +00:00
|
|
|
failingLbas = new List<ulong>();
|
|
|
|
|
unknownLbas = new List<ulong>();
|
2017-12-26 06:05:12 +00:00
|
|
|
for(ulong i = 0; i < imageInfo.Sectors; i++) unknownLbas.Add(i);
|
2017-12-19 20:33:03 +00:00
|
|
|
|
2016-08-26 01:45:58 +01:00
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
2017-12-26 07:28:40 +00:00
|
|
|
public bool? VerifySectors(ulong sectorAddress, uint length, uint track, out List<ulong> failingLbas,
|
2017-12-20 17:15:26 +00:00
|
|
|
out List<ulong> unknownLbas)
|
2016-08-26 01:45:58 +01:00
|
|
|
{
|
|
|
|
|
throw new FeatureUnsupportedImageException("Feature not supported by image format");
|
|
|
|
|
}
|
|
|
|
|
|
2017-12-26 07:28:40 +00:00
|
|
|
public bool? VerifyMediaImage()
|
2016-08-26 01:45:58 +01:00
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
2017-12-24 00:12:31 +00:00
|
|
|
|
|
|
|
|
[StructLayout(LayoutKind.Sequential, Pack = 1)]
|
|
|
|
|
struct CpcDiskInfo
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Magic number, "MV - CPCEMU Disk-File" in old files, "EXTENDED CPC DSK File" in extended ones
|
|
|
|
|
/// </summary>
|
|
|
|
|
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 21)] public byte[] magic;
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Second part of magic, should be "\r\nDisk-Info\r\n" in all, but some emulators write spaces instead.
|
|
|
|
|
/// </summary>
|
|
|
|
|
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 13)] public byte[] magic2;
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Creator application (can be null)
|
|
|
|
|
/// </summary>
|
|
|
|
|
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 14)] public byte[] creator;
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Tracks
|
|
|
|
|
/// </summary>
|
|
|
|
|
public byte tracks;
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Sides
|
|
|
|
|
/// </summary>
|
|
|
|
|
public byte sides;
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Size of a track including the 256 bytes header. Unused by extended format, as this format includes a table in the
|
|
|
|
|
/// next field
|
|
|
|
|
/// </summary>
|
|
|
|
|
public ushort tracksize;
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Size of each track in the extended format. 0 indicates track is not formatted and not present in image.
|
|
|
|
|
/// </summary>
|
|
|
|
|
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 204)] public byte[] tracksizeTable;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[StructLayout(LayoutKind.Sequential, Pack = 1)]
|
|
|
|
|
struct CpcTrackInfo
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Magic number, "Track-Info\r\n"
|
|
|
|
|
/// </summary>
|
|
|
|
|
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 10)] public byte[] magic;
|
|
|
|
|
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 2)] public byte[] carriageReturn;
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Padding
|
|
|
|
|
/// </summary>
|
|
|
|
|
public uint padding;
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Track number
|
|
|
|
|
/// </summary>
|
|
|
|
|
public byte track;
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Side number
|
|
|
|
|
/// </summary>
|
|
|
|
|
public byte side;
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Controller data rate
|
|
|
|
|
/// </summary>
|
|
|
|
|
public byte dataRate;
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Recording mode
|
|
|
|
|
/// </summary>
|
|
|
|
|
public byte recordingMode;
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Bytes per sector
|
|
|
|
|
/// </summary>
|
|
|
|
|
public IBMSectorSizeCode bps;
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// How many sectors in this track
|
|
|
|
|
/// </summary>
|
|
|
|
|
public byte sectors;
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// GAP#3
|
|
|
|
|
/// </summary>
|
|
|
|
|
public byte gap3;
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Filler
|
|
|
|
|
/// </summary>
|
|
|
|
|
public byte filler;
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Informatino for up to 32 sectors
|
|
|
|
|
/// </summary>
|
|
|
|
|
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 32)] public CpcSectorInfo[] sectorsInfo;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Sector information
|
|
|
|
|
/// </summary>
|
|
|
|
|
[StructLayout(LayoutKind.Sequential, Pack = 1)]
|
|
|
|
|
struct CpcSectorInfo
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Track number from address mark
|
|
|
|
|
/// </summary>
|
|
|
|
|
public byte track;
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Side number from address mark
|
|
|
|
|
/// </summary>
|
|
|
|
|
public byte side;
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Sector ID from address mark
|
|
|
|
|
/// </summary>
|
|
|
|
|
public byte id;
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Sector size from address mark
|
|
|
|
|
/// </summary>
|
|
|
|
|
public IBMSectorSizeCode size;
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// ST1 register from controller
|
|
|
|
|
/// </summary>
|
|
|
|
|
public byte st1;
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// ST2 register from controller
|
|
|
|
|
/// </summary>
|
|
|
|
|
public byte st2;
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Length in bytes of this sector size. If it is bigger than expected sector size, it's a weak sector read several
|
|
|
|
|
/// times.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public ushort len;
|
|
|
|
|
}
|
2016-08-26 01:43:15 +01:00
|
|
|
}
|
2017-12-19 20:33:03 +00:00
|
|
|
}
|