Files
Aaru/Aaru.Filesystems/PFS.cs

216 lines
8.3 KiB
C#
Raw Normal View History

// /***************************************************************************
2020-02-27 12:31:25 +00:00
// Aaru Data Preservation Suite
// ----------------------------------------------------------------------------
//
2016-09-14 00:13:29 +01:00
// Filename : PFS.cs
// Author(s) : Natalia Portillo <claunia@claunia.com>
//
2016-09-14 02:31:32 +01:00
// Component : Professional File System plugin.
//
// --[ Description ] ----------------------------------------------------------
//
// Identifies the Professional 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-02-18 10:02:53 +00:00
// Copyright © 2011-2022 Natalia Portillo
// ****************************************************************************/
2022-03-07 07:36:44 +00:00
// ReSharper disable UnusedType.Local
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;
2017-12-21 14:30:38 +00:00
using Schemas;
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 Professional File System</summary>
public sealed class PFS : IFilesystem
{
2022-03-06 13:29:38 +00:00
/// <summary>Identifier for AFS (PFS v1)</summary>
const uint AFS_DISK = 0x41465301;
/// <summary>Identifier for PFS v2</summary>
const uint PFS2_DISK = 0x50465302;
/// <summary>Identifier for PFS v3</summary>
const uint PFS_DISK = 0x50465301;
/// <summary>Identifier for multi-user AFS</summary>
const uint MUAF_DISK = 0x6D754146;
/// <summary>Identifier for multi-user PFS</summary>
const uint MUPFS_DISK = 0x6D755046;
const string FS_TYPE = "pfs";
/// <inheritdoc />
2022-03-06 13:29:38 +00:00
public FileSystemType XmlFsType { get; private set; }
/// <inheritdoc />
public Encoding Encoding { get; private set; }
/// <inheritdoc />
public string Name => Localization.PFS_Name;
2022-03-06 13:29:38 +00:00
/// <inheritdoc />
public Guid Id => new("68DE769E-D957-406A-8AE4-3781CA8CDA77");
/// <inheritdoc />
public string Author => Authors.NataliaPortillo;
2022-03-06 13:29:38 +00:00
/// <inheritdoc />
public bool Identify(IMediaImage imagePlugin, Partition partition)
{
if(partition.Length < 3)
return false;
2022-03-06 13:29:38 +00:00
ErrorNumber errno = imagePlugin.ReadSector(2 + partition.Start, out byte[] sector);
2022-03-06 13:29:38 +00:00
if(errno != ErrorNumber.NoError)
return false;
uint magic = BigEndianBitConverter.ToUInt32(sector, 0x00);
2022-03-16 11:47:00 +00:00
return magic is AFS_DISK or PFS2_DISK or PFS_DISK or MUAF_DISK or MUPFS_DISK;
2022-03-06 13:29:38 +00:00
}
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
{
information = "";
Encoding = encoding ?? Encoding.GetEncoding("iso-8859-1");
ErrorNumber errno = imagePlugin.ReadSector(2 + partition.Start, out byte[] rootBlockSector);
2022-03-06 13:29:38 +00:00
if(errno != ErrorNumber.NoError)
return;
2022-03-06 13:29:38 +00:00
RootBlock rootBlock = Marshal.ByteArrayToStructureBigEndian<RootBlock>(rootBlockSector);
2022-03-06 13:29:38 +00:00
var sbInformation = new StringBuilder();
XmlFsType = new FileSystemType();
2020-02-29 18:03:35 +00:00
2022-03-06 13:29:38 +00:00
switch(rootBlock.diskType)
{
case AFS_DISK:
case MUAF_DISK:
sbInformation.Append(Localization.Professional_File_System_v1);
XmlFsType.Type = FS_TYPE;
2022-03-06 13:29:38 +00:00
break;
case PFS2_DISK:
sbInformation.Append(Localization.Professional_File_System_v2);
XmlFsType.Type = FS_TYPE;
2022-03-06 13:29:38 +00:00
break;
case PFS_DISK:
case MUPFS_DISK:
sbInformation.Append(Localization.Professional_File_System_v3);
XmlFsType.Type = FS_TYPE;
2022-03-06 13:29:38 +00:00
break;
}
2020-02-29 18:03:35 +00:00
2022-03-16 11:47:00 +00:00
if(rootBlock.diskType is MUAF_DISK or MUPFS_DISK)
sbInformation.Append(Localization.with_multi_user_support);
2020-02-29 18:03:35 +00:00
2022-03-06 13:29:38 +00:00
sbInformation.AppendLine();
sbInformation.
AppendFormat(Localization.Volume_name_0, StringHandlers.PascalToString(rootBlock.diskname, Encoding)).
AppendLine();
sbInformation.
AppendFormat(Localization.Volume_has_0_free_sectors_of_1, rootBlock.blocksfree, rootBlock.diskSize).
AppendLine();
sbInformation.AppendFormat(Localization.Volume_created_on_0,
2022-03-06 13:29:38 +00:00
DateHandlers.AmigaToDateTime(rootBlock.creationday, rootBlock.creationminute,
rootBlock.creationtick)).AppendLine();
2020-02-29 18:03:35 +00:00
2022-03-06 13:29:38 +00:00
if(rootBlock.extension > 0)
sbInformation.AppendFormat(Localization.Root_block_extension_resides_at_block_0, rootBlock.extension).
AppendLine();
2020-02-29 18:03:35 +00:00
2022-03-06 13:29:38 +00:00
information = sbInformation.ToString();
2022-03-06 13:29:38 +00:00
XmlFsType.CreationDate =
DateHandlers.AmigaToDateTime(rootBlock.creationday, rootBlock.creationminute, rootBlock.creationtick);
2022-03-06 13:29:38 +00:00
XmlFsType.CreationDateSpecified = true;
XmlFsType.FreeClusters = rootBlock.blocksfree;
XmlFsType.FreeClustersSpecified = true;
XmlFsType.Clusters = rootBlock.diskSize;
XmlFsType.ClusterSize = imagePlugin.Info.SectorSize;
XmlFsType.VolumeName = StringHandlers.PascalToString(rootBlock.diskname, Encoding);
}
2022-03-06 13:29:38 +00:00
/// <summary>Boot block, first 2 sectors</summary>
[StructLayout(LayoutKind.Sequential, Pack = 1)]
readonly struct BootBlock
{
/// <summary>"PFS\1" disk type</summary>
public readonly uint diskType;
/// <summary>Boot code, til completion</summary>
public readonly byte[] bootCode;
}
2022-03-06 13:29:38 +00:00
[StructLayout(LayoutKind.Sequential, Pack = 1)]
readonly struct RootBlock
{
/// <summary>Disk type</summary>
public readonly uint diskType;
/// <summary>Options</summary>
public readonly uint options;
/// <summary>Current datestamp</summary>
public readonly uint datestamp;
/// <summary>Volume creation day</summary>
public readonly ushort creationday;
/// <summary>Volume creation minute</summary>
public readonly ushort creationminute;
/// <summary>Volume creation tick</summary>
public readonly ushort creationtick;
/// <summary>AmigaDOS protection bits</summary>
public readonly ushort protection;
/// <summary>Volume label (Pascal string)</summary>
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 32)]
public readonly byte[] diskname;
/// <summary>Last reserved block</summary>
public readonly uint lastreserved;
/// <summary>First reserved block</summary>
public readonly uint firstreserved;
/// <summary>Free reserved blocks</summary>
public readonly uint reservedfree;
/// <summary>Size of reserved blocks in bytes</summary>
public readonly ushort reservedblocksize;
/// <summary>Blocks in rootblock, including bitmap</summary>
public readonly ushort rootblockclusters;
/// <summary>Free blocks</summary>
public readonly uint blocksfree;
/// <summary>Blocks that must be always free</summary>
public readonly uint alwaysfree;
/// <summary>Current bitmapfield number for allocation</summary>
public readonly uint rovingPointer;
/// <summary>Pointer to deldir</summary>
public readonly uint delDirPtr;
/// <summary>Disk size in sectors</summary>
public readonly uint diskSize;
/// <summary>Rootblock extension</summary>
public readonly uint extension;
/// <summary>Unused</summary>
public readonly uint unused;
}
}