Files
Aaru/Aaru.Filesystems/MicroDOS.cs

188 lines
6.9 KiB
C#
Raw Normal View History

// /***************************************************************************
2020-02-27 12:31:25 +00:00
// Aaru Data Preservation Suite
// ----------------------------------------------------------------------------
//
// Filename : MicroDOS.cs
// Author(s) : Natalia Portillo <claunia@claunia.com>
//
// Component : MicroDOS filesystem plugin
//
// --[ Description ] ----------------------------------------------------------
//
// Identifies the MicroDOS filesystem 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
// ReSharper disable UnusedMember.Local
namespace Aaru.Filesystems;
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;
2017-12-21 14:30:38 +00:00
using Schemas;
2020-02-27 00:33:26 +00:00
using Marshal = Aaru.Helpers.Marshal;
2022-03-06 13:29:38 +00:00
/// <inheritdoc />
/// <summary>
/// Implements detection for the MicroDOS filesystem. Information from http://www.owg.ru/mkt/BK/MKDOS.TXT Thanks
/// to tarlabnor for translating it
/// </summary>
public sealed class MicroDOS : IFilesystem
{
2022-03-06 13:29:38 +00:00
const ushort MAGIC = 0xA72E;
const ushort MAGIC2 = 0x530C;
/// <inheritdoc />
public FileSystemType XmlFsType { get; private set; }
/// <inheritdoc />
public Encoding Encoding { get; private set; }
/// <inheritdoc />
public string Name => "MicroDOS file system";
/// <inheritdoc />
public Guid Id => new("9F9A364A-1A27-48A3-B730-7A7122000324");
/// <inheritdoc />
public string Author => "Natalia Portillo";
/// <inheritdoc />
2022-03-06 13:29:38 +00:00
public bool Identify(IMediaImage imagePlugin, Partition partition)
{
2022-03-06 13:29:38 +00:00
if(1 + partition.Start >= partition.End)
return false;
2022-03-06 13:29:38 +00:00
if(imagePlugin.Info.SectorSize < 512)
return false;
2022-03-06 13:29:38 +00:00
ErrorNumber errno = imagePlugin.ReadSector(0 + partition.Start, out byte[] bk0);
2022-03-06 13:29:38 +00:00
if(errno != ErrorNumber.NoError)
return false;
2022-03-06 13:29:38 +00:00
Block0 block0 = Marshal.ByteArrayToStructureLittleEndian<Block0>(bk0);
2022-03-06 13:29:38 +00:00
return block0.label == MAGIC && block0.mklabel == MAGIC2;
}
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 = encoding ?? Encoding.GetEncoding("koi8-r");
information = "";
2022-03-06 13:29:38 +00:00
var sb = new StringBuilder();
2022-03-06 13:29:38 +00:00
ErrorNumber errno = imagePlugin.ReadSector(0 + partition.Start, out byte[] bk0);
2022-03-06 13:29:38 +00:00
if(errno != ErrorNumber.NoError)
return;
2022-03-06 13:29:38 +00:00
Block0 block0 = Marshal.ByteArrayToStructureLittleEndian<Block0>(bk0);
2022-03-06 13:29:38 +00:00
sb.AppendLine("MicroDOS filesystem");
sb.AppendFormat("Volume has {0} blocks ({1} bytes)", block0.blocks, block0.blocks * 512).AppendLine();
2020-02-29 18:03:35 +00:00
2022-03-06 13:29:38 +00:00
sb.AppendFormat("Volume has {0} blocks used ({1} bytes)", block0.usedBlocks, block0.usedBlocks * 512).
AppendLine();
2020-02-29 18:03:35 +00:00
2022-03-06 13:29:38 +00:00
sb.AppendFormat("Volume contains {0} files", block0.files).AppendLine();
sb.AppendFormat("First used block is {0}", block0.firstUsedBlock).AppendLine();
2022-03-06 13:29:38 +00:00
XmlFsType = new FileSystemType
{
Type = "MicroDOS",
ClusterSize = 512,
Clusters = block0.blocks,
Files = block0.files,
FilesSpecified = true,
FreeClusters = (ulong)(block0.blocks - block0.usedBlocks),
FreeClustersSpecified = true
};
information = sb.ToString();
}
2022-03-06 13:29:38 +00:00
// Followed by directory entries
[StructLayout(LayoutKind.Sequential, Pack = 1)]
readonly struct Block0
{
/// <summary>BK starts booting here</summary>
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 24)]
public readonly byte[] bootCode;
/// <summary>Number of files in directory</summary>
public readonly ushort files;
/// <summary>Total number of blocks in files of the directory</summary>
public readonly ushort usedBlocks;
/// <summary>Unknown</summary>
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 228)]
public readonly byte[] unknown;
/// <summary>Ownership label (label that shows it belongs to Micro DOS format)</summary>
public readonly ushort label;
/// <summary>MK-DOS directory format label</summary>
public readonly ushort mklabel;
/// <summary>Unknown</summary>
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 50)]
public readonly byte[] unknown2;
/// <summary>
/// Disk size in blocks (absolute value for the system unlike NORD, NORTON etc.) that doesn't use two fixed values
/// 40 or 80 tracks, but i.e. if you drive works with 76 tracks this field will contain an appropriate number of blocks
/// </summary>
public readonly ushort blocks;
/// <summary> Number of the first file's block. Value is changable</summary>
public readonly ushort firstUsedBlock;
/// <summary>Unknown</summary>
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 6)]
public readonly byte[] unknown3;
}
2022-03-06 13:29:38 +00:00
[StructLayout(LayoutKind.Sequential, Pack = 1)]
readonly struct DirectoryEntry
{
/// <summary>File status</summary>
public readonly byte status;
/// <summary>Directory number (0 - root)</summary>
public readonly byte directory;
/// <summary>File name 14. symbols in ASCII KOI8</summary>
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 12)]
public readonly byte[] filename;
/// <summary>Block number</summary>
public readonly ushort blockNo;
/// <summary>Length in blocks</summary>
public readonly ushort blocks;
/// <summary>Address</summary>
public readonly ushort address;
/// <summary>Length</summary>
public readonly ushort length;
}
enum FileStatus : byte
{
2022-03-07 07:36:44 +00:00
CommonFile = 0,
Protected = 1,
LogicalDisk = 2,
BadFile = 0x80,
Deleted = 0xFF
}
}