Files
Aaru/Aaru.Filesystems/CBM.cs

264 lines
9.9 KiB
C#
Raw Normal View History

// /***************************************************************************
2020-02-27 12:31:25 +00:00
// Aaru Data Preservation Suite
// ----------------------------------------------------------------------------
//
// 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/>.
//
// ----------------------------------------------------------------------------
2022-12-03 16:07:10 +00:00
// Copyright © 2011-2023 Natalia Portillo
// ****************************************************************************/
using System;
using System.Runtime.InteropServices;
using System.Text;
2020-02-27 00:33:26 +00:00
using Aaru.CommonTypes;
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;
namespace Aaru.Filesystems;
2022-03-06 13:29:38 +00:00
/// <inheritdoc />
/// <summary>Implements detection of the filesystem used in 8-bit Commodore microcomputers</summary>
public sealed class CBM : IFilesystem
{
const string FS_TYPE = "cbmfs";
/// <inheritdoc />
2022-03-06 13:29:38 +00:00
public FileSystemType XmlFsType { get; private set; }
/// <inheritdoc />
public string Name => Localization.CBM_Name;
2022-03-06 13:29:38 +00:00
/// <inheritdoc />
public Guid Id => new("D104744E-A376-450C-BAC0-1347C93F983B");
/// <inheritdoc />
public Encoding Encoding { get; private set; }
/// <inheritdoc />
public string Author => Authors.NataliaPortillo;
2022-03-06 13:29:38 +00:00
/// <inheritdoc />
public bool Identify(IMediaImage imagePlugin, Partition partition)
{
if(partition.Start > 0)
return false;
2022-03-06 13:29:38 +00:00
if(imagePlugin.Info.SectorSize != 256)
return false;
2022-03-06 13:29:38 +00:00
if(imagePlugin.Info.Sectors != 683 &&
imagePlugin.Info.Sectors != 768 &&
imagePlugin.Info.Sectors != 1366 &&
imagePlugin.Info.Sectors != 3200)
return false;
2022-03-06 13:29:38 +00:00
byte[] sector;
2022-03-06 13:29:38 +00:00
if(imagePlugin.Info.Sectors == 3200)
{
ErrorNumber errno = imagePlugin.ReadSector(1560, out sector);
2022-03-06 13:29:38 +00:00
if(errno != ErrorNumber.NoError)
return false;
2022-03-06 13:29:38 +00:00
Header cbmHdr = Marshal.ByteArrayToStructureLittleEndian<Header>(sector);
2022-03-06 13:29:38 +00:00
if(cbmHdr.diskDosVersion == 0x44 &&
cbmHdr is { dosVersion: 0x33, diskVersion: 0x44 })
2022-03-06 13:29:38 +00:00
return true;
}
else
{
ErrorNumber errno = imagePlugin.ReadSector(357, out sector);
2022-03-06 13:29:38 +00:00
if(errno != ErrorNumber.NoError)
return false;
2022-03-06 13:29:38 +00:00
BAM cbmBam = Marshal.ByteArrayToStructureLittleEndian<BAM>(sector);
if(cbmBam is { dosVersion: 0x41, doubleSided: 0x00 or 0x80 } and { unused1: 0x00, directoryTrack: 0x12 })
2022-03-06 13:29:38 +00:00
return true;
}
2022-03-06 13:29:38 +00:00
return false;
}
2022-03-06 13:29:38 +00:00
/// <inheritdoc />
2022-03-07 07:36:44 +00:00
public void GetInformation(IMediaImage imagePlugin, Partition partition, out string information, Encoding encoding)
2022-03-06 13:29:38 +00:00
{
Encoding = new PETSCII();
information = "";
byte[] sector;
2022-03-06 13:29:38 +00:00
var sbInformation = new StringBuilder();
sbInformation.AppendLine(Localization.Commodore_file_system);
2022-03-06 13:29:38 +00:00
XmlFsType = new FileSystemType
{
Type = FS_TYPE,
2022-03-06 13:29:38 +00:00
Clusters = imagePlugin.Info.Sectors,
ClusterSize = 256
};
2022-03-06 13:29:38 +00:00
if(imagePlugin.Info.Sectors == 3200)
{
ErrorNumber errno = imagePlugin.ReadSector(1560, out sector);
2022-03-06 13:29:38 +00:00
if(errno != ErrorNumber.NoError)
return;
2022-03-06 13:29:38 +00:00
Header cbmHdr = Marshal.ByteArrayToStructureLittleEndian<Header>(sector);
2020-02-29 18:03:35 +00:00
sbInformation.AppendFormat(Localization.Directory_starts_at_track_0_sector_1, cbmHdr.directoryTrack,
2022-03-06 13:29:38 +00:00
cbmHdr.directorySector).AppendLine();
2020-02-29 18:03:35 +00:00
sbInformation.AppendFormat(Localization.Disk_DOS_Version_0, Encoding.ASCII.GetString(new[]
2022-03-06 13:29:38 +00:00
{
cbmHdr.diskDosVersion
})).AppendLine();
2020-02-29 18:03:35 +00:00
sbInformation.AppendFormat(Localization.DOS_Version_0, Encoding.ASCII.GetString(new[]
2022-03-06 13:29:38 +00:00
{
cbmHdr.dosVersion
})).AppendLine();
2020-02-29 18:03:35 +00:00
sbInformation.AppendFormat(Localization.Disk_Version_0, Encoding.ASCII.GetString(new[]
2022-03-06 13:29:38 +00:00
{
cbmHdr.diskVersion
})).AppendLine();
2020-02-29 18:03:35 +00:00
sbInformation.AppendFormat(Localization.Disk_ID_0, cbmHdr.diskId).AppendLine();
sbInformation.AppendFormat(Localization.Disk_name_0, StringHandlers.CToString(cbmHdr.name, Encoding)).
AppendLine();
2022-03-06 13:29:38 +00:00
XmlFsType.VolumeName = StringHandlers.CToString(cbmHdr.name, Encoding);
XmlFsType.VolumeSerial = $"{cbmHdr.diskId}";
}
else
{
ErrorNumber errno = imagePlugin.ReadSector(357, out sector);
2022-03-06 13:29:38 +00:00
if(errno != ErrorNumber.NoError)
return;
2022-03-06 13:29:38 +00:00
BAM cbmBam = Marshal.ByteArrayToStructureLittleEndian<BAM>(sector);
2020-02-29 18:03:35 +00:00
sbInformation.AppendFormat(Localization.Directory_starts_at_track_0_sector_1, cbmBam.directoryTrack,
2022-03-06 13:29:38 +00:00
cbmBam.directorySector).AppendLine();
2020-02-29 18:03:35 +00:00
sbInformation.AppendFormat(Localization.Disk_DOS_type_0,
2022-03-07 07:36:44 +00:00
Encoding.ASCII.GetString(BitConverter.GetBytes(cbmBam.dosType))).AppendLine();
2020-02-29 18:03:35 +00:00
sbInformation.AppendFormat(Localization.DOS_Version_0, Encoding.ASCII.GetString(new[]
2022-03-06 13:29:38 +00:00
{
cbmBam.dosVersion
})).AppendLine();
2020-02-29 18:03:35 +00:00
sbInformation.AppendFormat(Localization.Disk_ID_0, cbmBam.diskId).AppendLine();
sbInformation.AppendFormat(Localization.Disk_name_0, StringHandlers.CToString(cbmBam.name, Encoding)).
AppendLine();
2022-03-06 13:29:38 +00:00
XmlFsType.VolumeName = StringHandlers.CToString(cbmBam.name, Encoding);
XmlFsType.VolumeSerial = $"{cbmBam.diskId}";
}
2022-03-06 13:29:38 +00:00
information = sbInformation.ToString();
}
2022-03-06 13:29:38 +00:00
[StructLayout(LayoutKind.Sequential, Pack = 1)]
readonly struct BAM
{
/// <summary>Track where directory starts</summary>
public readonly byte directoryTrack;
/// <summary>Sector where directory starts</summary>
public readonly byte directorySector;
/// <summary>Disk DOS version, 0x41</summary>
public readonly byte dosVersion;
/// <summary>Set to 0x80 if 1571, 0x00 if not</summary>
public readonly byte doubleSided;
/// <summary>Block allocation map</summary>
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 140)]
public readonly byte[] bam;
/// <summary>Disk name</summary>
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 16)]
public readonly byte[] name;
/// <summary>Filled with 0xA0</summary>
public readonly ushort fill1;
/// <summary>Disk ID</summary>
public readonly ushort diskId;
/// <summary>Filled with 0xA0</summary>
public readonly byte fill2;
/// <summary>DOS type</summary>
public readonly ushort dosType;
/// <summary>Filled with 0xA0</summary>
public readonly uint fill3;
/// <summary>Unused</summary>
public readonly byte unused1;
/// <summary>Block allocation map for Dolphin DOS extended tracks</summary>
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 20)]
public readonly byte[] dolphinBam;
/// <summary>Block allocation map for Speed DOS extended tracks</summary>
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 20)]
public readonly byte[] speedBam;
/// <summary>Unused</summary>
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 9)]
public readonly byte[] unused2;
/// <summary>Free sector count for second side in 1571</summary>
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 9)]
public readonly byte[] freeCount;
}
[StructLayout(LayoutKind.Sequential, Pack = 1)]
readonly struct Header
{
/// <summary>Track where directory starts</summary>
public readonly byte directoryTrack;
/// <summary>Sector where directory starts</summary>
public readonly byte directorySector;
/// <summary>Disk DOS version, 0x44</summary>
public readonly byte diskDosVersion;
/// <summary>Unusued</summary>
public readonly byte unused1;
/// <summary>Disk name</summary>
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 16)]
public readonly byte[] name;
/// <summary>Filled with 0xA0</summary>
public readonly ushort fill1;
/// <summary>Disk ID</summary>
public readonly ushort diskId;
/// <summary>Filled with 0xA0</summary>
public readonly byte fill2;
/// <summary>DOS version ('3')</summary>
public readonly byte dosVersion;
/// <summary>Disk version ('D')</summary>
public readonly byte diskVersion;
/// <summary>Filled with 0xA0</summary>
public readonly short fill3;
}
}