2017-07-19 16:31:08 +01:00
|
|
|
|
// /***************************************************************************
|
2020-02-27 12:31:25 +00:00
|
|
|
|
// Aaru Data Preservation Suite
|
2016-09-14 17:59:54 +01:00
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
|
//
|
|
|
|
|
|
// Filename : CBM.cs
|
|
|
|
|
|
// Author(s) : Natalia Portillo <claunia@claunia.com>
|
|
|
|
|
|
//
|
|
|
|
|
|
// Component : Commodore file system plugin.
|
|
|
|
|
|
//
|
|
|
|
|
|
// --[ Description ] ----------------------------------------------------------
|
|
|
|
|
|
//
|
|
|
|
|
|
// Identifies the Commodore file system and shows information.
|
|
|
|
|
|
//
|
|
|
|
|
|
// --[ 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
|
2016-09-14 17:59:54 +01:00
|
|
|
|
// ****************************************************************************/
|
|
|
|
|
|
|
|
|
|
|
|
using System;
|
|
|
|
|
|
using System.Runtime.InteropServices;
|
|
|
|
|
|
using System.Text;
|
2020-02-27 00:33:26 +00:00
|
|
|
|
using Aaru.CommonTypes;
|
2021-09-19 21:16:47 +01:00
|
|
|
|
using Aaru.CommonTypes.Enums;
|
2020-02-27 00:33:26 +00:00
|
|
|
|
using Aaru.CommonTypes.Interfaces;
|
2020-07-20 15:43:52 +01:00
|
|
|
|
using Aaru.Helpers;
|
2020-02-29 18:03:35 +00:00
|
|
|
|
using Claunia.Encoding;
|
2017-12-21 14:30:38 +00:00
|
|
|
|
using Schemas;
|
|
|
|
|
|
using Encoding = System.Text.Encoding;
|
2020-02-27 00:33:26 +00:00
|
|
|
|
using Marshal = Aaru.Helpers.Marshal;
|
2016-09-14 17:59:54 +01:00
|
|
|
|
|
2020-02-27 00:33:26 +00:00
|
|
|
|
namespace Aaru.Filesystems
|
2016-09-14 17:59:54 +01:00
|
|
|
|
{
|
2021-08-17 14:25:12 +01:00
|
|
|
|
/// <inheritdoc />
|
2021-08-17 21:23:10 +01:00
|
|
|
|
/// <summary>Implements detection of the filesystem used in 8-bit Commodore microcomputers</summary>
|
2020-07-22 13:20:25 +01:00
|
|
|
|
public sealed class CBM : IFilesystem
|
2016-09-14 17:59:54 +01:00
|
|
|
|
{
|
2021-08-17 13:56:05 +01:00
|
|
|
|
/// <inheritdoc />
|
2017-12-26 08:01:40 +00:00
|
|
|
|
public FileSystemType XmlFsType { get; private set; }
|
2021-08-17 13:56:05 +01:00
|
|
|
|
/// <inheritdoc />
|
2021-08-17 21:23:10 +01:00
|
|
|
|
public string Name => "Commodore file system";
|
2021-08-17 13:56:05 +01:00
|
|
|
|
/// <inheritdoc />
|
2021-09-19 21:16:47 +01:00
|
|
|
|
public Guid Id => new("D104744E-A376-450C-BAC0-1347C93F983B");
|
2021-08-17 13:56:05 +01:00
|
|
|
|
/// <inheritdoc />
|
2021-08-17 21:23:10 +01:00
|
|
|
|
public Encoding Encoding { get; private set; }
|
2021-08-17 13:56:05 +01:00
|
|
|
|
/// <inheritdoc />
|
2021-08-17 21:23:10 +01:00
|
|
|
|
public string Author => "Natalia Portillo";
|
2016-09-14 17:59:54 +01:00
|
|
|
|
|
2021-08-17 13:56:05 +01:00
|
|
|
|
/// <inheritdoc />
|
2017-12-26 07:28:40 +00:00
|
|
|
|
public bool Identify(IMediaImage imagePlugin, Partition partition)
|
2016-09-14 17:59:54 +01:00
|
|
|
|
{
|
2020-02-29 18:03:35 +00:00
|
|
|
|
if(partition.Start > 0)
|
|
|
|
|
|
return false;
|
2016-09-14 17:59:54 +01:00
|
|
|
|
|
2020-02-29 18:03:35 +00:00
|
|
|
|
if(imagePlugin.Info.SectorSize != 256)
|
|
|
|
|
|
return false;
|
2016-09-14 17:59:54 +01:00
|
|
|
|
|
2020-02-29 18:03:35 +00:00
|
|
|
|
if(imagePlugin.Info.Sectors != 683 &&
|
|
|
|
|
|
imagePlugin.Info.Sectors != 768 &&
|
|
|
|
|
|
imagePlugin.Info.Sectors != 1366 &&
|
|
|
|
|
|
imagePlugin.Info.Sectors != 3200)
|
|
|
|
|
|
return false;
|
2016-09-14 17:59:54 +01:00
|
|
|
|
|
|
|
|
|
|
byte[] sector;
|
|
|
|
|
|
|
2017-12-26 06:05:12 +00:00
|
|
|
|
if(imagePlugin.Info.Sectors == 3200)
|
2016-09-14 17:59:54 +01:00
|
|
|
|
{
|
2021-09-19 21:16:47 +01:00
|
|
|
|
ErrorNumber errno = imagePlugin.ReadSector(1560, out sector);
|
|
|
|
|
|
|
|
|
|
|
|
if(errno != ErrorNumber.NoError)
|
|
|
|
|
|
return false;
|
|
|
|
|
|
|
2020-07-20 21:11:32 +01:00
|
|
|
|
Header cbmHdr = Marshal.ByteArrayToStructureLittleEndian<Header>(sector);
|
2016-09-14 17:59:54 +01:00
|
|
|
|
|
2020-02-29 18:03:35 +00:00
|
|
|
|
if(cbmHdr.diskDosVersion == 0x44 &&
|
|
|
|
|
|
cbmHdr.dosVersion == 0x33 &&
|
|
|
|
|
|
cbmHdr.diskVersion == 0x44)
|
2016-09-14 17:59:54 +01:00
|
|
|
|
return true;
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
2021-09-19 21:16:47 +01:00
|
|
|
|
ErrorNumber errno = imagePlugin.ReadSector(357, out sector);
|
|
|
|
|
|
|
|
|
|
|
|
if(errno != ErrorNumber.NoError)
|
|
|
|
|
|
return false;
|
|
|
|
|
|
|
2020-07-20 21:11:32 +01:00
|
|
|
|
BAM cbmBam = Marshal.ByteArrayToStructureLittleEndian<BAM>(sector);
|
2016-09-14 17:59:54 +01:00
|
|
|
|
|
2020-02-29 18:03:35 +00:00
|
|
|
|
if(cbmBam.dosVersion == 0x41 &&
|
|
|
|
|
|
(cbmBam.doubleSided == 0x00 || cbmBam.doubleSided == 0x80) &&
|
|
|
|
|
|
cbmBam.unused1 == 0x00 &&
|
|
|
|
|
|
cbmBam.directoryTrack == 0x12)
|
|
|
|
|
|
return true;
|
2016-09-14 17:59:54 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2021-08-17 13:56:05 +01:00
|
|
|
|
/// <inheritdoc />
|
2017-12-26 08:01:40 +00:00
|
|
|
|
public void GetInformation(IMediaImage imagePlugin, Partition partition, out string information,
|
2020-02-29 18:03:35 +00:00
|
|
|
|
Encoding encoding)
|
2016-09-14 17:59:54 +01:00
|
|
|
|
{
|
2021-09-19 21:16:47 +01:00
|
|
|
|
Encoding = new PETSCII();
|
|
|
|
|
|
information = "";
|
2016-09-14 17:59:54 +01:00
|
|
|
|
byte[] sector;
|
|
|
|
|
|
|
2020-02-29 18:03:35 +00:00
|
|
|
|
var sbInformation = new StringBuilder();
|
2016-09-14 17:59:54 +01:00
|
|
|
|
|
|
|
|
|
|
sbInformation.AppendLine("Commodore file system");
|
|
|
|
|
|
|
2017-12-26 08:01:40 +00:00
|
|
|
|
XmlFsType = new FileSystemType
|
2017-12-22 08:43:22 +00:00
|
|
|
|
{
|
2020-07-20 04:34:16 +01:00
|
|
|
|
Type = "Commodore file system",
|
|
|
|
|
|
Clusters = imagePlugin.Info.Sectors,
|
|
|
|
|
|
ClusterSize = 256
|
2017-12-22 08:43:22 +00:00
|
|
|
|
};
|
2016-09-14 17:59:54 +01:00
|
|
|
|
|
2017-12-26 06:05:12 +00:00
|
|
|
|
if(imagePlugin.Info.Sectors == 3200)
|
2016-09-14 17:59:54 +01:00
|
|
|
|
{
|
2021-09-19 21:16:47 +01:00
|
|
|
|
ErrorNumber errno = imagePlugin.ReadSector(1560, out sector);
|
|
|
|
|
|
|
|
|
|
|
|
if(errno != ErrorNumber.NoError)
|
|
|
|
|
|
return;
|
|
|
|
|
|
|
2020-07-20 21:11:32 +01:00
|
|
|
|
Header cbmHdr = Marshal.ByteArrayToStructureLittleEndian<Header>(sector);
|
2016-09-14 17:59:54 +01:00
|
|
|
|
|
2017-12-19 20:33:03 +00:00
|
|
|
|
sbInformation.AppendFormat("Directory starts at track {0} sector {1}", cbmHdr.directoryTrack,
|
|
|
|
|
|
cbmHdr.directorySector).AppendLine();
|
2020-02-29 18:03:35 +00:00
|
|
|
|
|
|
|
|
|
|
sbInformation.AppendFormat("Disk DOS Version: {0}", Encoding.ASCII.GetString(new[]
|
|
|
|
|
|
{
|
|
|
|
|
|
cbmHdr.diskDosVersion
|
|
|
|
|
|
})).AppendLine();
|
|
|
|
|
|
|
|
|
|
|
|
sbInformation.AppendFormat("DOS Version: {0}", Encoding.ASCII.GetString(new[]
|
|
|
|
|
|
{
|
|
|
|
|
|
cbmHdr.dosVersion
|
|
|
|
|
|
})).AppendLine();
|
|
|
|
|
|
|
|
|
|
|
|
sbInformation.AppendFormat("Disk Version: {0}", Encoding.ASCII.GetString(new[]
|
|
|
|
|
|
{
|
|
|
|
|
|
cbmHdr.diskVersion
|
|
|
|
|
|
})).AppendLine();
|
|
|
|
|
|
|
2016-09-14 17:59:54 +01:00
|
|
|
|
sbInformation.AppendFormat("Disk ID: {0}", cbmHdr.diskId).AppendLine();
|
2020-02-29 18:03:35 +00:00
|
|
|
|
|
|
|
|
|
|
sbInformation.AppendFormat("Disk name: {0}", StringHandlers.CToString(cbmHdr.name, Encoding)).
|
|
|
|
|
|
AppendLine();
|
2016-09-14 17:59:54 +01:00
|
|
|
|
|
2018-06-22 08:08:38 +01:00
|
|
|
|
XmlFsType.VolumeName = StringHandlers.CToString(cbmHdr.name, Encoding);
|
2017-12-26 08:01:40 +00:00
|
|
|
|
XmlFsType.VolumeSerial = $"{cbmHdr.diskId}";
|
2016-09-14 17:59:54 +01:00
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
2021-09-19 21:16:47 +01:00
|
|
|
|
ErrorNumber errno = imagePlugin.ReadSector(357, out sector);
|
|
|
|
|
|
|
|
|
|
|
|
if(errno != ErrorNumber.NoError)
|
|
|
|
|
|
return;
|
|
|
|
|
|
|
2020-07-20 21:11:32 +01:00
|
|
|
|
BAM cbmBam = Marshal.ByteArrayToStructureLittleEndian<BAM>(sector);
|
2016-09-14 17:59:54 +01:00
|
|
|
|
|
2017-12-19 20:33:03 +00:00
|
|
|
|
sbInformation.AppendFormat("Directory starts at track {0} sector {1}", cbmBam.directoryTrack,
|
|
|
|
|
|
cbmBam.directorySector).AppendLine();
|
2020-02-29 18:03:35 +00:00
|
|
|
|
|
2017-12-19 20:33:03 +00:00
|
|
|
|
sbInformation.AppendFormat("Disk DOS type: {0}",
|
2020-02-29 18:03:35 +00:00
|
|
|
|
Encoding.ASCII.GetString(BitConverter.GetBytes(cbmBam.dosType))).
|
|
|
|
|
|
AppendLine();
|
|
|
|
|
|
|
|
|
|
|
|
sbInformation.AppendFormat("DOS Version: {0}", Encoding.ASCII.GetString(new[]
|
|
|
|
|
|
{
|
|
|
|
|
|
cbmBam.dosVersion
|
|
|
|
|
|
})).AppendLine();
|
|
|
|
|
|
|
2016-09-14 17:59:54 +01:00
|
|
|
|
sbInformation.AppendFormat("Disk ID: {0}", cbmBam.diskId).AppendLine();
|
2020-02-29 18:03:35 +00:00
|
|
|
|
|
|
|
|
|
|
sbInformation.AppendFormat("Disk name: {0}", StringHandlers.CToString(cbmBam.name, Encoding)).
|
|
|
|
|
|
AppendLine();
|
2016-09-14 17:59:54 +01:00
|
|
|
|
|
2018-06-22 08:08:38 +01:00
|
|
|
|
XmlFsType.VolumeName = StringHandlers.CToString(cbmBam.name, Encoding);
|
2017-12-26 08:01:40 +00:00
|
|
|
|
XmlFsType.VolumeSerial = $"{cbmBam.diskId}";
|
2016-09-14 17:59:54 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
information = sbInformation.ToString();
|
|
|
|
|
|
}
|
2017-12-24 02:37:41 +00:00
|
|
|
|
|
|
|
|
|
|
[StructLayout(LayoutKind.Sequential, Pack = 1)]
|
2020-11-11 04:19:18 +00:00
|
|
|
|
readonly struct BAM
|
2017-12-24 02:37:41 +00:00
|
|
|
|
{
|
2020-02-29 18:03:35 +00:00
|
|
|
|
/// <summary>Track where directory starts</summary>
|
2019-04-23 01:38:33 +01:00
|
|
|
|
public readonly byte directoryTrack;
|
2020-02-29 18:03:35 +00:00
|
|
|
|
/// <summary>Sector where directory starts</summary>
|
2019-04-23 01:38:33 +01:00
|
|
|
|
public readonly byte directorySector;
|
2020-02-29 18:03:35 +00:00
|
|
|
|
/// <summary>Disk DOS version, 0x41</summary>
|
2019-04-23 01:38:33 +01:00
|
|
|
|
public readonly byte dosVersion;
|
2020-02-29 18:03:35 +00:00
|
|
|
|
/// <summary>Set to 0x80 if 1571, 0x00 if not</summary>
|
2019-04-23 01:38:33 +01:00
|
|
|
|
public readonly byte doubleSided;
|
2020-02-29 18:03:35 +00:00
|
|
|
|
/// <summary>Block allocation map</summary>
|
2018-06-22 08:08:38 +01:00
|
|
|
|
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 140)]
|
2019-04-23 01:38:33 +01:00
|
|
|
|
public readonly byte[] bam;
|
2020-02-29 18:03:35 +00:00
|
|
|
|
/// <summary>Disk name</summary>
|
2018-06-22 08:08:38 +01:00
|
|
|
|
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 16)]
|
2019-04-23 01:38:33 +01:00
|
|
|
|
public readonly byte[] name;
|
2020-02-29 18:03:35 +00:00
|
|
|
|
/// <summary>Filled with 0xA0</summary>
|
2019-04-23 01:38:33 +01:00
|
|
|
|
public readonly ushort fill1;
|
2020-02-29 18:03:35 +00:00
|
|
|
|
/// <summary>Disk ID</summary>
|
2019-04-23 01:38:33 +01:00
|
|
|
|
public readonly ushort diskId;
|
2020-02-29 18:03:35 +00:00
|
|
|
|
/// <summary>Filled with 0xA0</summary>
|
2019-04-23 01:38:33 +01:00
|
|
|
|
public readonly byte fill2;
|
2020-02-29 18:03:35 +00:00
|
|
|
|
/// <summary>DOS type</summary>
|
2019-04-23 01:38:33 +01:00
|
|
|
|
public readonly ushort dosType;
|
2020-02-29 18:03:35 +00:00
|
|
|
|
/// <summary>Filled with 0xA0</summary>
|
2019-04-23 01:38:33 +01:00
|
|
|
|
public readonly uint fill3;
|
2020-02-29 18:03:35 +00:00
|
|
|
|
/// <summary>Unused</summary>
|
2019-04-23 01:38:33 +01:00
|
|
|
|
public readonly byte unused1;
|
2020-02-29 18:03:35 +00:00
|
|
|
|
/// <summary>Block allocation map for Dolphin DOS extended tracks</summary>
|
2018-06-22 08:08:38 +01:00
|
|
|
|
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 20)]
|
2019-04-23 01:38:33 +01:00
|
|
|
|
public readonly byte[] dolphinBam;
|
2020-02-29 18:03:35 +00:00
|
|
|
|
/// <summary>Block allocation map for Speed DOS extended tracks</summary>
|
2018-06-22 08:08:38 +01:00
|
|
|
|
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 20)]
|
2019-04-23 01:38:33 +01:00
|
|
|
|
public readonly byte[] speedBam;
|
2020-02-29 18:03:35 +00:00
|
|
|
|
/// <summary>Unused</summary>
|
2018-06-22 08:08:38 +01:00
|
|
|
|
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 9)]
|
2019-04-23 01:38:33 +01:00
|
|
|
|
public readonly byte[] unused2;
|
2020-02-29 18:03:35 +00:00
|
|
|
|
/// <summary>Free sector count for second side in 1571</summary>
|
2018-06-22 08:08:38 +01:00
|
|
|
|
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 9)]
|
2019-04-23 01:38:33 +01:00
|
|
|
|
public readonly byte[] freeCount;
|
2017-12-24 02:37:41 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
[StructLayout(LayoutKind.Sequential, Pack = 1)]
|
2020-11-11 04:19:18 +00:00
|
|
|
|
readonly struct Header
|
2017-12-24 02:37:41 +00:00
|
|
|
|
{
|
2020-02-29 18:03:35 +00:00
|
|
|
|
/// <summary>Track where directory starts</summary>
|
2019-04-23 01:38:33 +01:00
|
|
|
|
public readonly byte directoryTrack;
|
2020-02-29 18:03:35 +00:00
|
|
|
|
/// <summary>Sector where directory starts</summary>
|
2019-04-23 01:38:33 +01:00
|
|
|
|
public readonly byte directorySector;
|
2020-02-29 18:03:35 +00:00
|
|
|
|
/// <summary>Disk DOS version, 0x44</summary>
|
2019-04-23 01:38:33 +01:00
|
|
|
|
public readonly byte diskDosVersion;
|
2020-02-29 18:03:35 +00:00
|
|
|
|
/// <summary>Unusued</summary>
|
2019-04-23 01:38:33 +01:00
|
|
|
|
public readonly byte unused1;
|
2020-02-29 18:03:35 +00:00
|
|
|
|
/// <summary>Disk name</summary>
|
2018-06-22 08:08:38 +01:00
|
|
|
|
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 16)]
|
2019-04-23 01:38:33 +01:00
|
|
|
|
public readonly byte[] name;
|
2020-02-29 18:03:35 +00:00
|
|
|
|
/// <summary>Filled with 0xA0</summary>
|
2019-04-23 01:38:33 +01:00
|
|
|
|
public readonly ushort fill1;
|
2020-02-29 18:03:35 +00:00
|
|
|
|
/// <summary>Disk ID</summary>
|
2019-04-23 01:38:33 +01:00
|
|
|
|
public readonly ushort diskId;
|
2020-02-29 18:03:35 +00:00
|
|
|
|
/// <summary>Filled with 0xA0</summary>
|
2019-04-23 01:38:33 +01:00
|
|
|
|
public readonly byte fill2;
|
2020-02-29 18:03:35 +00:00
|
|
|
|
/// <summary>DOS version ('3')</summary>
|
2019-04-23 01:38:33 +01:00
|
|
|
|
public readonly byte dosVersion;
|
2020-02-29 18:03:35 +00:00
|
|
|
|
/// <summary>Disk version ('D')</summary>
|
2019-04-23 01:38:33 +01:00
|
|
|
|
public readonly byte diskVersion;
|
2020-02-29 18:03:35 +00:00
|
|
|
|
/// <summary>Filled with 0xA0</summary>
|
2019-04-23 01:38:33 +01:00
|
|
|
|
public readonly short fill3;
|
2017-12-24 02:37:41 +00:00
|
|
|
|
}
|
2016-09-14 17:59:54 +01:00
|
|
|
|
}
|
|
|
|
|
|
}
|