Renamed project files and folders

This commit is contained in:
2020-02-26 19:10:46 +00:00
parent e133798583
commit f5b199e483
1417 changed files with 109 additions and 109 deletions

130
Aaru.Filesystems/CPM/CPM.cs Normal file
View File

@@ -0,0 +1,130 @@
// /***************************************************************************
// The Disc Image Chef
// ----------------------------------------------------------------------------
//
// Filename : CPM.cs
// Author(s) : Natalia Portillo <claunia@claunia.com>
//
// Component : CP/M filesystem plugin.
//
// --[ Description ] ----------------------------------------------------------
//
// Constructors and common variables for the CP/M filesystem plugin.
//
// --[ 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/>.
//
// ----------------------------------------------------------------------------
// Copyright © 2011-2020 Natalia Portillo
// ****************************************************************************/
using System;
using System.Collections.Generic;
using System.Text;
using DiscImageChef.CommonTypes;
using DiscImageChef.CommonTypes.Interfaces;
using DiscImageChef.CommonTypes.Structs;
using Schemas;
namespace DiscImageChef.Filesystems.CPM
{
partial class CPM : IReadOnlyFilesystem
{
/// <summary>
/// True if <see cref="Identify" /> thinks this is a CP/M filesystem
/// </summary>
bool cpmFound;
/// <summary>
/// Cached <see cref="FileSystemInfo" />
/// </summary>
FileSystemInfo cpmStat;
/// <summary>
/// Cached file passwords, decoded
/// </summary>
Dictionary<string, byte[]> decodedPasswordCache;
/// <summary>
/// Stores all known CP/M disk definitions
/// </summary>
CpmDefinitions definitions;
IMediaImage device;
/// <summary>
/// Cached directory listing
/// </summary>
List<string> dirList;
/// <summary>
/// CP/M disc parameter block (on-memory)
/// </summary>
DiscParameterBlock dpb;
/// <summary>
/// Cached file data
/// </summary>
Dictionary<string, byte[]> fileCache;
/// <summary>
/// The volume label, if the CP/M filesystem contains one
/// </summary>
string label;
/// <summary>
/// Timestamp in volume label for creation
/// </summary>
byte[] labelCreationDate;
/// <summary>
/// Timestamp in volume label for update
/// </summary>
byte[] labelUpdateDate;
bool mounted;
Partition partition;
/// <summary>
/// Cached file passwords
/// </summary>
Dictionary<string, byte[]> passwordCache;
/// <summary>
/// Sector deinterleaving mask
/// </summary>
int[] sectorMask;
/// <summary>
/// True if there are CP/M 3 timestamps
/// </summary>
bool standardTimestamps;
/// <summary>
/// Cached file <see cref="FileEntryInfo" />
/// </summary>
Dictionary<string, FileEntryInfo> statCache;
/// <summary>
/// True if there are timestamps in Z80DOS or DOS+ format
/// </summary>
bool thirdPartyTimestamps;
/// <summary>
/// If <see cref="Identify" /> thinks this is a CP/M filesystem, this is the definition for it
/// </summary>
CpmDefinition workingDefinition;
public FileSystemType XmlFsType { get; private set; }
public Encoding Encoding { get; private set; }
public string Name => "CP/M File System";
public Guid Id => new Guid("AA2B8585-41DF-4E3B-8A35-D1A935E2F8A1");
public string Author => "Natalia Portillo";
public IEnumerable<(string name, Type type, string description)> SupportedOptions =>
new (string name, Type type, string description)[] { };
public Dictionary<string, string> Namespaces => null;
static Dictionary<string, string> GetDefaultOptions() =>
new Dictionary<string, string> {{"debug", false.ToString()}};
}
}

View File

@@ -0,0 +1,84 @@
// /***************************************************************************
// The Disc Image Chef
// ----------------------------------------------------------------------------
//
// Filename : Consts.cs
// Author(s) : Natalia Portillo <claunia@claunia.com>
//
// Component : CP/M filesystem plugin.
//
// --[ Description ] ----------------------------------------------------------
//
// CP/M filesystem constants.
//
// --[ 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/>.
//
// ----------------------------------------------------------------------------
// Copyright © 2011-2020 Natalia Portillo
// ****************************************************************************/
namespace DiscImageChef.Filesystems.CPM
{
partial class CPM
{
/// <summary>
/// Enumerates the format identification byte used by CP/M-86
/// </summary>
enum FormatByte : byte
{
/// <summary>
/// 5.25" double-density single-side 8 sectors/track
/// </summary>
k160 = 0,
/// <summary>
/// 5.25" double-density double-side 8 sectors/track
/// </summary>
k320 = 1,
/// <summary>
/// 5.25" double-density double-side 9 sectors/track
/// </summary>
k360 = 0x10,
/// <summary>
/// 5.25" double-density double-side 9 sectors/track
/// </summary>
k360Alt = 0x40,
/// <summary>
/// 3.5" double-density double-side 9 sectors/track
/// </summary>
k720 = 0x11,
/// <summary>
/// 3.5" double-density double-side 9 sectors/track using FEAT144
/// </summary>
f720 = 0x48,
/// <summary>
/// 5.25" high-density double-side 15 sectors/track using FEAT144
/// </summary>
f1200 = 0x0C,
/// <summary>
/// 3.5" high-density double-side 18 sectors/track using FEAT144
/// </summary>
f1440 = 0x90,
/// <summary>
/// 5.25" double-density double-side 9 sectors/track
/// </summary>
k360Alt2 = 0x26,
/// <summary>
/// 3.5" double-density double-side 9 sectors/track
/// </summary>
k720Alt = 0x94
}
}
}

View File

@@ -0,0 +1,210 @@
// /***************************************************************************
// The Disc Image Chef
// ----------------------------------------------------------------------------
//
// Filename : Definitions.cs
// Author(s) : Natalia Portillo <claunia@claunia.com>
//
// Component : CP/M filesystem plugin.
//
// --[ Description ] ----------------------------------------------------------
//
// Handles definitions of known CP/M disks.
//
// --[ 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/>.
//
// ----------------------------------------------------------------------------
// Copyright © 2011-2020 Natalia Portillo
// ****************************************************************************/
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Xml;
using System.Xml.Serialization;
namespace DiscImageChef.Filesystems.CPM
{
partial class CPM
{
/// <summary>
/// Loads all the known CP/M disk definitions from an XML stored as an embedded resource.
/// </summary>
/// <returns>The definitions.</returns>
bool LoadDefinitions()
{
try
{
XmlReader defsReader =
XmlReader.Create(Assembly.GetExecutingAssembly()
.GetManifestResourceStream("DiscImageChef.Filesystems.CPM.cpmdefs.xml") ??
throw new InvalidOperationException());
XmlSerializer defsSerializer = new XmlSerializer(typeof(CpmDefinitions));
definitions = (CpmDefinitions)defsSerializer.Deserialize(defsReader);
// Patch definitions
foreach(CpmDefinition def in definitions.definitions)
{
if(def.side1 == null)
{
def.side1 = new Side {sideId = 0, sectorIds = new int[def.sectorsPerTrack]};
for(int i = 0; i < def.sectorsPerTrack; i++) def.side1.sectorIds[i] = i + 1;
}
if(def.sides != 2 || def.side2 != null) continue;
{
def.side2 = new Side {sideId = 1, sectorIds = new int[def.sectorsPerTrack]};
for(int i = 0; i < def.sectorsPerTrack; i++) def.side2.sectorIds[i] = i + 1;
}
}
return true;
}
catch { return false; }
}
}
/// <summary>
/// CP/M disk definitions
/// </summary>
public class CpmDefinitions
{
/// <summary>
/// Timestamp of creation of the CP/M disk definitions list
/// </summary>
public DateTime creation;
/// <summary>
/// List of all CP/M disk definitions
/// </summary>
public List<CpmDefinition> definitions;
}
/// <summary>
/// CP/M disk definition
/// </summary>
public class CpmDefinition
{
/// <summary>
/// Maps the first 16 allocation blocks for reservation, high byte
/// </summary>
public int al0;
/// <summary>
/// Maps the first 16 allocation blocks for reservation, low byte
/// </summary>
public int al1;
/// <summary>
/// Controller bitrate
/// </summary>
public string bitrate;
/// <summary>
/// Block mask for <see cref="bsh" />
/// </summary>
public int blm;
/// <summary>
/// Left shifts needed to translate allocation block number to lba
/// </summary>
public int bsh;
/// <summary>
/// Physical bytes per sector
/// </summary>
public int bytesPerSector;
/// <summary>
/// Comment and description
/// </summary>
public string comment;
/// <summary>
/// If true, all bytes written on disk are negated
/// </summary>
public bool complement;
/// <summary>
/// Total cylinders
/// </summary>
public int cylinders;
/// <summary>
/// Total number of available directory entries
/// </summary>
public int drm;
/// <summary>
/// Total number of 128 byte records on disk
/// </summary>
public int dsm;
/// <summary>
/// Encoding, "FM", "MFM", "GCR"
/// </summary>
public string encoding;
/// <summary>
/// Absolutely unknown?
/// </summary>
public bool evenOdd;
/// <summary>
/// Extent mask
/// </summary>
public int exm;
/// <summary>
/// Disk definition label
/// </summary>
public string label;
/// <summary>
/// Tracks at the beginning of disk reserved for BIOS/BDOS
/// </summary>
public int ofs;
/// <summary>
/// Cylinder/side ordering. SIDES = change side after each track, CYLINDERS = change side after whole side, EAGLE and
/// COLUMBIA unknown
/// </summary>
public string order;
/// <summary>
/// Physical sectors per side
/// </summary>
public int sectorsPerTrack;
/// <summary>
/// Description of controller's side 0 (usually, upper side)
/// </summary>
public Side side1;
/// <summary>
/// Description of controller's side 1 (usually, lower side)
/// </summary>
public Side side2;
/// <summary>
/// Total sides
/// </summary>
public int sides;
/// <summary>
/// Physical sector interleaving
/// </summary>
public int skew;
/// <summary>
/// Sectors at the beginning of disk reserved for BIOS/BDOS
/// </summary>
public int sofs;
}
/// <summary>
/// Side descriptions
/// </summary>
public class Side
{
/// <summary>
/// Software interleaving mask, [1,3,0,2] means CP/M LBA 0 is physical sector 1, LBA 1 = 3, so on
/// </summary>
public int[] sectorIds;
/// <summary>
/// Side ID as found in each sector address mark
/// </summary>
public int sideId;
}
}

116
Aaru.Filesystems/CPM/Dir.cs Normal file
View File

@@ -0,0 +1,116 @@
// /***************************************************************************
// The Disc Image Chef
// ----------------------------------------------------------------------------
//
// Filename : Dir.cs
// Author(s) : Natalia Portillo <claunia@claunia.com>
//
// Component : CP/M filesystem plugin.
//
// --[ Description ] ----------------------------------------------------------
//
// Methods to show the CP/M filesystem directory.
//
// --[ 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/>.
//
// ----------------------------------------------------------------------------
// Copyright © 2011-2020 Natalia Portillo
// ****************************************************************************/
using System;
using System.Collections.Generic;
using System.Text;
using DiscImageChef.CommonTypes.Structs;
using DiscImageChef.Helpers;
namespace DiscImageChef.Filesystems.CPM
{
partial class CPM
{
public Errno ReadDir(string path, out List<string> contents)
{
contents = null;
if(!mounted) return Errno.AccessDenied;
if(!string.IsNullOrEmpty(path) && string.Compare(path, "/", StringComparison.OrdinalIgnoreCase) != 0)
return Errno.NotSupported;
contents = new List<string>(dirList);
return Errno.NoError;
}
/// <summary>
/// Checks that the given directory blocks follow the CP/M filesystem directory specification
/// Corrupted directories will fail.
/// FAT firectories will false positive if all files start with 0x05, and do not use full extentions, for example:
/// "σAFILE.GZ" (using code page 437)
/// </summary>
/// <returns>False if the directory does not follow the directory specification</returns>
/// <param name="directory">Directory blocks.</param>
bool CheckDir(byte[] directory)
{
try
{
if(directory == null) return false;
int fileCount = 0;
for(int off = 0; off < directory.Length; off += 32)
{
DirectoryEntry entry = Marshal.ByteArrayToStructureLittleEndian<DirectoryEntry>(directory, off, 32);
if((entry.statusUser & 0x7F) < 0x20)
{
for(int f = 0; f < 8; f++)
if(entry.filename[f] < 0x20 && entry.filename[f] != 0x00)
return false;
for(int e = 0; e < 3; e++)
if(entry.extension[e] < 0x20 && entry.extension[e] != 0x00)
return false;
if(!ArrayHelpers.ArrayIsNullOrWhiteSpace(entry.filename)) fileCount++;
}
else if(entry.statusUser == 0x20)
{
for(int f = 0; f < 8; f++)
if(entry.filename[f] < 0x20 && entry.filename[f] != 0x00)
return false;
for(int e = 0; e < 3; e++)
if(entry.extension[e] < 0x20 && entry.extension[e] != 0x00)
return false;
label = Encoding.ASCII.GetString(directory, off + 1, 11).Trim();
labelCreationDate = new byte[4];
labelUpdateDate = new byte[4];
Array.Copy(directory, off + 24, labelCreationDate, 0, 4);
Array.Copy(directory, off + 28, labelUpdateDate, 0, 4);
}
else if(entry.statusUser == 0x21)
if(directory[off + 1] == 0x00)
thirdPartyTimestamps = true;
else
standardTimestamps |= directory[off + 21] == 0x00 && directory[off + 31] == 0x00;
}
return fileCount > 0;
}
catch { return false; }
}
}
}

View File

@@ -0,0 +1,121 @@
// /***************************************************************************
// The Disc Image Chef
// ----------------------------------------------------------------------------
//
// Filename : File.cs
// Author(s) : Natalia Portillo <claunia@claunia.com>
//
// Component : CP/M filesystem plugin.
//
// --[ Description ] ----------------------------------------------------------
//
// Methods to handle files.
//
// --[ 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/>.
//
// ----------------------------------------------------------------------------
// Copyright © 2011-2020 Natalia Portillo
// ****************************************************************************/
using System;
using DiscImageChef.CommonTypes.Structs;
namespace DiscImageChef.Filesystems.CPM
{
partial class CPM
{
public Errno GetAttributes(string path, out FileAttributes attributes)
{
attributes = new FileAttributes();
if(!mounted) return Errno.AccessDenied;
string[] pathElements = path.Split(new[] {'/'}, StringSplitOptions.RemoveEmptyEntries);
if(pathElements.Length != 1) return Errno.NotSupported;
if(string.IsNullOrEmpty(pathElements[0]) ||
string.Compare(pathElements[0], "/", StringComparison.OrdinalIgnoreCase) == 0)
{
attributes = new FileAttributes();
attributes = FileAttributes.Directory;
return Errno.NoError;
}
if(!statCache.TryGetValue(pathElements[0].ToUpperInvariant(), out FileEntryInfo fInfo))
return Errno.NoSuchFile;
attributes = fInfo.Attributes;
return Errno.NoError;
}
// TODO: Implementing this would require storing the interleaving
public Errno MapBlock(string path, long fileBlock, out long deviceBlock)
{
deviceBlock = 0;
return !mounted ? Errno.AccessDenied : Errno.NotImplemented;
}
public Errno Read(string path, long offset, long size, ref byte[] buf)
{
if(!mounted) return Errno.AccessDenied;
if(size == 0)
{
buf = new byte[0];
return Errno.NoError;
}
if(offset < 0) return Errno.InvalidArgument;
string[] pathElements = path.Split(new[] {'/'}, StringSplitOptions.RemoveEmptyEntries);
if(pathElements.Length != 1) return Errno.NotSupported;
if(!fileCache.TryGetValue(pathElements[0].ToUpperInvariant(), out byte[] file)) return Errno.NoSuchFile;
if(offset >= file.Length) return Errno.EINVAL;
if(size + offset >= file.Length) size = file.Length - offset;
buf = new byte[size];
Array.Copy(file, offset, buf, 0, size);
return Errno.NoError;
}
public Errno ReadLink(string path, out string dest)
{
dest = null;
return !mounted ? Errno.AccessDenied : Errno.NotSupported;
}
public Errno Stat(string path, out FileEntryInfo stat)
{
stat = null;
if(!mounted) return Errno.AccessDenied;
string[] pathElements = path.Split(new[] {'/'}, StringSplitOptions.RemoveEmptyEntries);
if(pathElements.Length != 1) return Errno.NotSupported;
if(!string.IsNullOrEmpty(path) && string.Compare(path, "/", StringComparison.OrdinalIgnoreCase) != 0)
return statCache.TryGetValue(pathElements[0].ToUpperInvariant(), out stat)
? Errno.NoError
: Errno.NoSuchFile;
stat = new FileEntryInfo {Attributes = FileAttributes.Directory, BlockSize = XmlFsType.ClusterSize};
if(labelCreationDate != null) stat.CreationTime = DateHandlers.CpmToDateTime(labelCreationDate);
if(labelUpdateDate != null) stat.StatusChangeTime = DateHandlers.CpmToDateTime(labelUpdateDate);
return Errno.NoError;
}
}
}

1069
Aaru.Filesystems/CPM/Info.cs Normal file

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,568 @@
// /***************************************************************************
// The Disc Image Chef
// ----------------------------------------------------------------------------
//
// Filename : Structs.cs
// Author(s) : Natalia Portillo <claunia@claunia.com>
//
// Component : CP/M filesystem plugin.
//
// --[ Description ] ----------------------------------------------------------
//
// CP/M filesystem structures.
//
// --[ 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/>.
//
// ----------------------------------------------------------------------------
// Copyright © 2011-2020 Natalia Portillo
// ****************************************************************************/
using System.Runtime.InteropServices;
namespace DiscImageChef.Filesystems.CPM
{
partial class CPM
{
/// <summary>
/// Most of the times this structure is hard wired or generated by CP/M, not stored on disk
/// </summary>
class DiscParameterBlock
{
/// <summary>
/// First byte of allocation bitmap
/// </summary>
public byte al0;
/// <summary>
/// Second byte of allocation bitmap
/// </summary>
public byte al1;
/// <summary>
/// Block mask
/// </summary>
public byte blm;
/// <summary>
/// Block shift
/// </summary>
public byte bsh;
/// <summary>
/// Checksum vector size
/// </summary>
public ushort cks;
/// <summary>
/// Directory entries - 1
/// </summary>
public ushort drm;
/// <summary>
/// Blocks on disk - 1
/// </summary>
public ushort dsm;
/// <summary>
/// Extent mask
/// </summary>
public byte exm;
/// <summary>
/// Reserved tracks
/// </summary>
public ushort off;
/// <summary>
/// Physical sector mask
/// </summary>
public byte phm;
/// <summary>
/// Physical sector shift
/// </summary>
public byte psh;
/// <summary>
/// Sectors per track
/// </summary>
public ushort spt;
}
/// <summary>
/// Amstrad superblock, for PCW
/// </summary>
[StructLayout(LayoutKind.Sequential, Pack = 1)]
struct AmstradSuperBlock
{
/// <summary>
/// Format ID. 0 single-side, 3 double-side. 1 and 2 are for CPC but they don't use the superblock
/// </summary>
public byte format;
/// <summary>
/// Gives information about side ordering
/// </summary>
public byte sidedness;
/// <summary>
/// Tracks per side, aka, cylinders
/// </summary>
public byte tps;
/// <summary>
/// Sectors per track
/// </summary>
public byte spt;
/// <summary>
/// Physical sector shift
/// </summary>
public byte psh;
/// <summary>
/// Reserved tracks
/// </summary>
public byte off;
/// <summary>
/// Block size shift
/// </summary>
public byte bsh;
/// <summary>
/// How many blocks does the directory take
/// </summary>
public byte dirBlocks;
/// <summary>
/// GAP#3 length (intersector)
/// </summary>
public byte gapLen;
/// <summary>
/// GAP#4 length (end-of-track)
/// </summary>
public byte formatGap;
/// <summary>
/// Must be 0
/// </summary>
public byte zero1;
/// <summary>
/// Must be 0
/// </summary>
public byte zero2;
/// <summary>
/// Must be 0
/// </summary>
public byte zero3;
/// <summary>
/// Must be 0
/// </summary>
public byte zero4;
/// <summary>
/// Must be 0
/// </summary>
public byte zero5;
/// <summary>
/// Indicates machine the boot code following the superblock is designed to boot
/// </summary>
public byte fiddle;
}
/// <summary>
/// Superblock found on CP/M-86 hard disk volumes
/// </summary>
[StructLayout(LayoutKind.Sequential, Pack = 1)]
struct HardDiskSuperBlock
{
/// <summary>
/// Value so the sum of all the superblock's sector bytes taken as 16-bit values gives 0
/// </summary>
public ushort checksum;
/// <summary>
/// Copyright string
/// </summary>
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 0x1F)]
public byte[] copyright;
/// <summary>
/// First cylinder of disk where this volume resides
/// </summary>
public ushort firstCylinder;
/// <summary>
/// How many cylinders does this volume span
/// </summary>
public ushort cylinders;
/// <summary>
/// Heads on hard disk
/// </summary>
public byte heads;
/// <summary>
/// Sectors per track
/// </summary>
public byte sectorsPerTrack;
/// <summary>
/// Flags, only use by CCP/M where bit 0 equals verify on write
/// </summary>
public byte flags;
/// <summary>
/// Sector size / 128
/// </summary>
public byte recordsPerSector;
/// <summary>
/// <see cref="DiscParameterBlock.spt" />
/// </summary>
public ushort spt;
/// <summary>
/// <see cref="DiscParameterBlock.bsh" />
/// </summary>
public byte bsh;
/// <summary>
/// <see cref="DiscParameterBlock.blm" />
/// </summary>
public byte blm;
/// <summary>
/// <see cref="DiscParameterBlock.exm" />
/// </summary>
public byte exm;
/// <summary>
/// <see cref="DiscParameterBlock.dsm" />
/// </summary>
public ushort dsm;
/// <summary>
/// <see cref="DiscParameterBlock.drm" />
/// </summary>
public ushort drm;
/// <summary>
/// <see cref="DiscParameterBlock.al0" />
/// </summary>
public ushort al0;
/// <summary>
/// <see cref="DiscParameterBlock.al1" />
/// </summary>
public ushort al1;
/// <summary>
/// <see cref="DiscParameterBlock.cks" />
/// </summary>
public ushort cks;
/// <summary>
/// <see cref="DiscParameterBlock.off" />
/// </summary>
public ushort off;
/// <summary>
/// Must be zero
/// </summary>
public ushort zero1;
/// <summary>
/// Must be zero
/// </summary>
public ushort zero2;
/// <summary>
/// Must be zero
/// </summary>
public ushort zero3;
/// <summary>
/// Must be zero
/// </summary>
public ushort zero4;
/// <summary>
/// How many 128 bytes are in a block
/// </summary>
public ushort recordsPerBlock;
/// <summary>
/// Maximum number of bad blocks in the bad block list
/// </summary>
public ushort badBlockWordsMax;
/// <summary>
/// Used number of bad blocks in the bad block list
/// </summary>
public ushort badBlockWords;
/// <summary>
/// First block after the blocks reserved for bad block substitution
/// </summary>
public ushort firstSub;
}
/// <summary>
/// Volume label entry
/// </summary>
[StructLayout(LayoutKind.Sequential, Pack = 1)]
struct LabelEntry
{
/// <summary>
/// Must be 0x20
/// </summary>
public byte signature;
/// <summary>
/// Label in ASCII
/// </summary>
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 11)]
public byte[] label;
/// <summary>
/// Label flags. Bit 0 = label exists, bit 4 = creation timestamp, bit 5 = modification timestamp, bit 6 = access
/// timestamp, bit 7 = password enabled
/// </summary>
public byte flags;
/// <summary>
/// Password decoder byte
/// </summary>
public byte passwordDecoder;
/// <summary>
/// Must be 0
/// </summary>
public ushort reserved;
/// <summary>
/// Password XOR'ed with <see cref="passwordDecoder" />
/// </summary>
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 8)]
public byte[] password;
/// <summary>
/// Label creation time
/// </summary>
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 4)]
public byte[] ctime;
/// <summary>
/// Label modification time
/// </summary>
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 4)]
public byte[] mtime;
}
/// <summary>
/// CP/M 3 timestamp entry
/// </summary>
[StructLayout(LayoutKind.Sequential, Pack = 1)]
struct DateEntry
{
/// <summary>
/// Must be 0x21
/// </summary>
public byte signature;
/// <summary>
/// File 1 create/access timestamp
/// </summary>
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 4)]
public byte[] date1;
/// <summary>
/// File 1 modification timestamp
/// </summary>
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 4)]
public byte[] date2;
/// <summary>
/// File 1 password mode
/// </summary>
public byte mode1;
public byte zero1;
/// <summary>
/// File 2 create/access timestamp
/// </summary>
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 4)]
public byte[] date3;
/// <summary>
/// File 2 modification timestamp
/// </summary>
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 4)]
public byte[] date4;
/// <summary>
/// File 2 password mode
/// </summary>
public byte mode2;
public byte zero2;
/// <summary>
/// File 3 create/access timestamp
/// </summary>
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 4)]
public byte[] date5;
/// <summary>
/// File 3 modification timestamp
/// </summary>
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 4)]
public byte[] date6;
/// <summary>
/// File 3 password mode
/// </summary>
public byte mode3;
public ushort zero3;
}
/// <summary>
/// Password entry
/// </summary>
[StructLayout(LayoutKind.Sequential, Pack = 1)]
struct PasswordEntry
{
/// <summary>
/// 16 + user number
/// </summary>
public byte userNumber;
/// <summary>
/// Filename
/// </summary>
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 8)]
public byte[] filename;
/// <summary>
/// Extension
/// </summary>
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 3)]
public byte[] extension;
/// <summary>
/// Password mode. Bit 7 = required for read, bit 6 = required for write, bit 5 = required for delete
/// </summary>
public byte mode;
/// <summary>
/// Password decoder byte
/// </summary>
public byte passwordDecoder;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 2)]
public byte[] reserved;
/// <summary>
/// Password XOR'ed with <see cref="passwordDecoder" />
/// </summary>
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 8)]
public byte[] password;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 8)]
public byte[] reserved2;
}
/// <summary>
/// Timestamp for Z80DOS or DOS+
/// </summary>
[StructLayout(LayoutKind.Sequential, Pack = 1)]
struct TrdPartyDateEntry
{
/// <summary>
/// Must be 0x21
/// </summary>
public byte signature;
public byte zero;
/// <summary>
/// Creation year for file 1
/// </summary>
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 2)]
public byte[] create1;
/// <summary>
/// Modification time for file 1
/// </summary>
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 4)]
public byte[] modify1;
/// <summary>
/// Access time for file 1
/// </summary>
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 4)]
public byte[] access1;
/// <summary>
/// Creation year for file 2
/// </summary>
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 2)]
public byte[] create2;
/// <summary>
/// Modification time for file 2
/// </summary>
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 4)]
public byte[] modify2;
/// <summary>
/// Access time for file 2
/// </summary>
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 4)]
public byte[] access2;
/// <summary>
/// Creation year for file 3
/// </summary>
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 2)]
public byte[] create3;
/// <summary>
/// Modification time for file 3
/// </summary>
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 4)]
public byte[] modify3;
/// <summary>
/// Access time for file 3
/// </summary>
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 4)]
public byte[] access3;
}
/// <summary>
/// Directory entry for &lt;256 allocation blocks
/// </summary>
[StructLayout(LayoutKind.Sequential, Pack = 1)]
struct DirectoryEntry
{
/// <summary>
/// User number. Bit 7 set in CP/M 1 means hidden
/// </summary>
public byte statusUser;
/// <summary>
/// Filename and bit 7 as flags
/// </summary>
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 8)]
public byte[] filename;
/// <summary>
/// Extension and bit 7 as flags
/// </summary>
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 3)]
public byte[] extension;
/// <summary>
/// Low byte of extent number
/// </summary>
public byte extentCounter;
/// <summary>
/// Last record bytes. In some implementations it means how many bytes are used in the last record, in others how many
/// bytes are free.
/// It always refer to 128 byte records even if blocks are way bigger, so it's mostly useless.
/// </summary>
public byte lastRecordBytes;
/// <summary>
/// High byte of extent number
/// </summary>
public byte extentCounterHigh;
/// <summary>
/// How many records are used in this entry. 0x80 if all are used.
/// </summary>
public byte records;
/// <summary>
/// Allocation blocks
/// </summary>
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 16)]
public byte[] allocations;
}
/// <summary>
/// Directory entry for &gt;256 allocation blocks
/// </summary>
[StructLayout(LayoutKind.Sequential, Pack = 1)]
struct DirectoryEntry16
{
/// <summary>
/// User number. Bit 7 set in CP/M 1 means hidden
/// </summary>
public byte statusUser;
/// <summary>
/// Filename and bit 7 as flags
/// </summary>
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 8)]
public byte[] filename;
/// <summary>
/// Extension and bit 7 as flags
/// </summary>
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 3)]
public byte[] extension;
/// <summary>
/// Low byte of extent number
/// </summary>
public byte extentCounter;
/// <summary>
/// Last record bytes. In some implementations it means how many bytes are used in the last record, in others how many
/// bytes are free.
/// It always refer to 128 byte records even if blocks are way bigger, so it's mostly useless.
/// </summary>
public byte lastRecordBytes;
/// <summary>
/// High byte of extent number
/// </summary>
public byte extentCounterHigh;
/// <summary>
/// How many records are used in this entry. 0x80 if all are used.
/// </summary>
public byte records;
/// <summary>
/// Allocation blocks
/// </summary>
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 8)]
public ushort[] allocations;
}
}
}

View File

@@ -0,0 +1,709 @@
// /***************************************************************************
// The Disc Image Chef
// ----------------------------------------------------------------------------
//
// Filename : Super.cs
// Author(s) : Natalia Portillo <claunia@claunia.com>
//
// Component : CP/M filesystem plugin.
//
// --[ Description ] ----------------------------------------------------------
//
// Handles mounting and umounting the CP/M filesystem.
// Caches the whole volume on mounting (shouldn't be a problem, maximum
// volume size for CP/M is 8 MiB).
//
// --[ 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/>.
//
// ----------------------------------------------------------------------------
// Copyright © 2011-2020 Natalia Portillo
// ****************************************************************************/
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using DiscImageChef.CommonTypes;
using DiscImageChef.CommonTypes.Interfaces;
using DiscImageChef.CommonTypes.Structs;
using DiscImageChef.Console;
using DiscImageChef.Helpers;
using Schemas;
using FileAttributes = DiscImageChef.CommonTypes.Structs.FileAttributes;
using FileSystemInfo = DiscImageChef.CommonTypes.Structs.FileSystemInfo;
namespace DiscImageChef.Filesystems.CPM
{
partial class CPM
{
public Errno Mount(IMediaImage imagePlugin, Partition partition, Encoding encoding,
Dictionary<string, string> options, string @namespace)
{
device = imagePlugin;
this.partition = partition;
Encoding = encoding ?? Encoding.GetEncoding("IBM437");
// As the identification is so complex, just call Identify() and relay on its findings
if(!Identify(device, partition) || !cpmFound || workingDefinition == null || dpb == null)
return Errno.InvalidArgument;
// Build the software interleaving sector mask
if(workingDefinition.sides == 1)
{
sectorMask = new int[workingDefinition.side1.sectorIds.Length];
for(int m = 0; m < sectorMask.Length; m++)
sectorMask[m] = workingDefinition.side1.sectorIds[m] - workingDefinition.side1.sectorIds[0];
}
else
{
// Head changes after every track
if(string.Compare(workingDefinition.order, "SIDES", StringComparison.InvariantCultureIgnoreCase) == 0)
{
sectorMask = new int[workingDefinition.side1.sectorIds.Length +
workingDefinition.side2.sectorIds.Length];
for(int m = 0; m < workingDefinition.side1.sectorIds.Length; m++)
sectorMask[m] = workingDefinition.side1.sectorIds[m] - workingDefinition.side1.sectorIds[0];
// Skip first track (first side)
for(int m = 0; m < workingDefinition.side2.sectorIds.Length; m++)
sectorMask[m + workingDefinition.side1.sectorIds.Length] =
workingDefinition.side2.sectorIds[m] - workingDefinition.side2.sectorIds[0] +
workingDefinition.side1.sectorIds.Length;
}
// Head changes after whole side
else if(string.Compare(workingDefinition.order, "CYLINDERS",
StringComparison.InvariantCultureIgnoreCase) == 0)
{
for(int m = 0; m < workingDefinition.side1.sectorIds.Length; m++)
sectorMask[m] = workingDefinition.side1.sectorIds[m] - workingDefinition.side1.sectorIds[0];
// Skip first track (first side) and first track (second side)
for(int m = 0; m < workingDefinition.side1.sectorIds.Length; m++)
sectorMask[m + workingDefinition.side1.sectorIds.Length] =
workingDefinition.side1.sectorIds[m] - workingDefinition.side1.sectorIds[0] +
workingDefinition.side1.sectorIds.Length +
workingDefinition.side2.sectorIds.Length;
// TODO: Implement CYLINDERS ordering
DicConsole.DebugWriteLine("CP/M Plugin", "CYLINDERS ordering not yet implemented.");
return Errno.NotImplemented;
}
// TODO: Implement COLUMBIA ordering
else if(string.Compare(workingDefinition.order, "COLUMBIA",
StringComparison.InvariantCultureIgnoreCase) == 0)
{
DicConsole.DebugWriteLine("CP/M Plugin",
"Don't know how to handle COLUMBIA ordering, not proceeding with this definition.");
return Errno.NotImplemented;
}
// TODO: Implement EAGLE ordering
else if(string.Compare(workingDefinition.order, "EAGLE", StringComparison.InvariantCultureIgnoreCase) ==
0)
{
DicConsole.DebugWriteLine("CP/M Plugin",
"Don't know how to handle EAGLE ordering, not proceeding with this definition.");
return Errno.NotImplemented;
}
else
{
DicConsole.DebugWriteLine("CP/M Plugin",
"Unknown order type \"{0}\", not proceeding with this definition.",
workingDefinition.order);
return Errno.NotSupported;
}
}
// Deinterleave whole volume
Dictionary<ulong, byte[]> deinterleavedSectors = new Dictionary<ulong, byte[]>();
if(workingDefinition.sides == 1 ||
string.Compare(workingDefinition.order, "SIDES", StringComparison.InvariantCultureIgnoreCase) == 0)
{
DicConsole.DebugWriteLine("CP/M Plugin", "Deinterleaving whole volume.");
for(int p = 0; p <= (int)(partition.End - partition.Start); p++)
{
byte[] readSector =
device.ReadSector((ulong)((int)partition.Start + p / sectorMask.Length * sectorMask.Length +
sectorMask[p % sectorMask.Length]));
if(workingDefinition.complement)
for(int b = 0; b < readSector.Length; b++)
readSector[b] = (byte)(~readSector[b] & 0xFF);
deinterleavedSectors.Add((ulong)p, readSector);
}
}
int blockSize = 128 << dpb.bsh;
MemoryStream blockMs = new MemoryStream();
ulong blockNo = 0;
int sectorsPerBlock = 0;
Dictionary<ulong, byte[]> allocationBlocks = new Dictionary<ulong, byte[]>();
DicConsole.DebugWriteLine("CP/M Plugin", "Creating allocation blocks.");
// For each volume sector
for(ulong a = 0; a < (ulong)deinterleavedSectors.Count; a++)
{
deinterleavedSectors.TryGetValue(a, out byte[] sector);
// May it happen? Just in case, CP/M blocks are smaller than physical sectors
if(sector.Length > blockSize)
for(int i = 0; i < sector.Length / blockSize; i++)
{
byte[] tmp = new byte[blockSize];
Array.Copy(sector, blockSize * i, tmp, 0, blockSize);
allocationBlocks.Add(blockNo++, tmp);
}
// CP/M blocks are larger than physical sectors
else if(sector.Length < blockSize)
{
blockMs.Write(sector, 0, sector.Length);
sectorsPerBlock++;
if(sectorsPerBlock != blockSize / sector.Length) continue;
allocationBlocks.Add(blockNo++, blockMs.ToArray());
sectorsPerBlock = 0;
blockMs = new MemoryStream();
}
// CP/M blocks are same size than physical sectors
else allocationBlocks.Add(blockNo++, sector);
}
DicConsole.DebugWriteLine("CP/M Plugin", "Reading directory.");
int dirOff;
int dirSectors = (dpb.drm + 1) * 32 / workingDefinition.bytesPerSector;
if(workingDefinition.sofs > 0) dirOff = workingDefinition.sofs;
else dirOff = workingDefinition.ofs * workingDefinition.sectorsPerTrack;
// Read the whole directory blocks
MemoryStream dirMs = new MemoryStream();
for(int d = 0; d < dirSectors; d++)
{
deinterleavedSectors.TryGetValue((ulong)(d + dirOff), out byte[] sector);
dirMs.Write(sector, 0, sector.Length);
}
byte[] directory = dirMs.ToArray();
if(directory == null) return Errno.InvalidArgument;
int dirCnt = 0;
string file1 = null;
string file2 = null;
string file3 = null;
Dictionary<string, Dictionary<int, List<ushort>>> fileExtents =
new Dictionary<string, Dictionary<int, List<ushort>>>();
statCache = new Dictionary<string, FileEntryInfo>();
cpmStat = new FileSystemInfo();
bool atime = false;
dirList = new List<string>();
labelCreationDate = null;
labelUpdateDate = null;
passwordCache = new Dictionary<string, byte[]>();
DicConsole.DebugWriteLine("CP/M Plugin", "Traversing directory.");
// For each directory entry
for(int dOff = 0; dOff < directory.Length; dOff += 32)
// Describes a file (does not support PDOS entries with user >= 16, because they're identical to password entries
if((directory[dOff] & 0x7F) < 0x10)
if(allocationBlocks.Count > 256)
{
DirectoryEntry16 entry =
Marshal.ByteArrayToStructureLittleEndian<DirectoryEntry16>(directory, dOff, 32);
bool hidden = (entry.statusUser & 0x80) == 0x80;
bool rdOnly = (entry.filename[0] & 0x80) == 0x80 || (entry.extension[0] & 0x80) == 0x80;
bool system = (entry.filename[1] & 0x80) == 0x80 || (entry.extension[2] & 0x80) == 0x80;
//bool backed = (entry.filename[3] & 0x80) == 0x80 || (entry.extension[3] & 0x80) == 0x80;
int user = entry.statusUser & 0x0F;
bool validEntry = true;
for(int i = 0; i < 8; i++)
{
entry.filename[i] &= 0x7F;
validEntry &= entry.filename[i] >= 0x20;
}
for(int i = 0; i < 3; i++)
{
entry.extension[i] &= 0x7F;
validEntry &= entry.extension[i] >= 0x20;
}
if(!validEntry) continue;
string filename = Encoding.ASCII.GetString(entry.filename).Trim();
string extension = Encoding.ASCII.GetString(entry.extension).Trim();
// If user is != 0, append user to name to have identical filenames
if(user > 0) filename = $"{user:X1}:{filename}";
if(!string.IsNullOrEmpty(extension)) filename = filename + "." + extension;
int entryNo = (32 * entry.extentCounter + entry.extentCounterHigh) / (dpb.exm + 1);
// Do we have a stat for the file already?
if(statCache.TryGetValue(filename, out FileEntryInfo fInfo)) statCache.Remove(filename);
else fInfo = new FileEntryInfo {Attributes = new FileAttributes()};
// And any extent?
if(fileExtents.TryGetValue(filename, out Dictionary<int, List<ushort>> extentBlocks))
fileExtents.Remove(filename);
else extentBlocks = new Dictionary<int, List<ushort>>();
// Do we already have this extent? Should never happen
if(extentBlocks.TryGetValue(entryNo, out List<ushort> blocks)) extentBlocks.Remove(entryNo);
else blocks = new List<ushort>();
// Attributes
if(hidden) fInfo.Attributes |= FileAttributes.Hidden;
if(rdOnly) fInfo.Attributes |= FileAttributes.ReadOnly;
if(system) fInfo.Attributes |= FileAttributes.System;
// Supposedly there is a value in the directory entry telling how many blocks are designated in
// this entry. However some implementations tend to do whatever they wish, but none will ever
// allocate block 0 for a file because that's where the directory resides.
// There is also a field telling how many bytes are used in the last block, but its meaning is
// non-standard so we must ignore it.
foreach(ushort blk in entry.allocations.Where(blk => !blocks.Contains(blk) && blk != 0))
blocks.Add(blk);
// Save the file
fInfo.UID = (ulong)user;
extentBlocks.Add(entryNo, blocks);
fileExtents.Add(filename, extentBlocks);
statCache.Add(filename, fInfo);
// Add the file to the directory listing
if(!dirList.Contains(filename)) dirList.Add(filename);
// Count entries 3 by 3 for timestamps
switch(dirCnt % 3)
{
case 0:
file1 = filename;
break;
case 1:
file2 = filename;
break;
case 2:
file3 = filename;
break;
}
dirCnt++;
}
else
{
DirectoryEntry entry =
Marshal.ByteArrayToStructureLittleEndian<DirectoryEntry>(directory, dOff, 32);
bool hidden = (entry.statusUser & 0x80) == 0x80;
bool rdOnly = (entry.filename[0] & 0x80) == 0x80 || (entry.extension[0] & 0x80) == 0x80;
bool system = (entry.filename[1] & 0x80) == 0x80 || (entry.extension[2] & 0x80) == 0x80;
//bool backed = (entry.filename[3] & 0x80) == 0x80 || (entry.extension[3] & 0x80) == 0x80;
int user = entry.statusUser & 0x0F;
bool validEntry = true;
for(int i = 0; i < 8; i++)
{
entry.filename[i] &= 0x7F;
validEntry &= entry.filename[i] >= 0x20;
}
for(int i = 0; i < 3; i++)
{
entry.extension[i] &= 0x7F;
validEntry &= entry.extension[i] >= 0x20;
}
if(!validEntry) continue;
string filename = Encoding.ASCII.GetString(entry.filename).Trim();
string extension = Encoding.ASCII.GetString(entry.extension).Trim();
// If user is != 0, append user to name to have identical filenames
if(user > 0) filename = $"{user:X1}:{filename}";
if(!string.IsNullOrEmpty(extension)) filename = filename + "." + extension;
int entryNo = (32 * entry.extentCounterHigh + entry.extentCounter) / (dpb.exm + 1);
// Do we have a stat for the file already?
if(statCache.TryGetValue(filename, out FileEntryInfo fInfo)) statCache.Remove(filename);
else fInfo = new FileEntryInfo {Attributes = new FileAttributes()};
// And any extent?
if(fileExtents.TryGetValue(filename, out Dictionary<int, List<ushort>> extentBlocks))
fileExtents.Remove(filename);
else extentBlocks = new Dictionary<int, List<ushort>>();
// Do we already have this extent? Should never happen
if(extentBlocks.TryGetValue(entryNo, out List<ushort> blocks)) extentBlocks.Remove(entryNo);
else blocks = new List<ushort>();
// Attributes
if(hidden) fInfo.Attributes |= FileAttributes.Hidden;
if(rdOnly) fInfo.Attributes |= FileAttributes.ReadOnly;
if(system) fInfo.Attributes |= FileAttributes.System;
// Supposedly there is a value in the directory entry telling how many blocks are designated in
// this entry. However some implementations tend to do whatever they wish, but none will ever
// allocate block 0 for a file because that's where the directory resides.
// There is also a field telling how many bytes are used in the last block, but its meaning is
// non-standard so we must ignore it.
foreach(ushort blk in entry.allocations
.Where(blk => !blocks.Contains(blk) && blk != 0)) blocks.Add(blk);
// Save the file
fInfo.UID = (ulong)user;
extentBlocks.Add(entryNo, blocks);
fileExtents.Add(filename, extentBlocks);
statCache.Add(filename, fInfo);
// Add the file to the directory listing
if(!dirList.Contains(filename)) dirList.Add(filename);
// Count entries 3 by 3 for timestamps
switch(dirCnt % 3)
{
case 0:
file1 = filename;
break;
case 1:
file2 = filename;
break;
case 2:
file3 = filename;
break;
}
dirCnt++;
}
// A password entry (or a file entry in PDOS, but this does not handle that case)
else if((directory[dOff] & 0x7F) >= 0x10 && (directory[dOff] & 0x7F) < 0x20)
{
PasswordEntry entry = Marshal.ByteArrayToStructureLittleEndian<PasswordEntry>(directory, dOff, 32);
int user = entry.userNumber & 0x0F;
for(int i = 0; i < 8; i++) entry.filename[i] &= 0x7F;
for(int i = 0; i < 3; i++) entry.extension[i] &= 0x7F;
string filename = Encoding.ASCII.GetString(entry.filename).Trim();
string extension = Encoding.ASCII.GetString(entry.extension).Trim();
// If user is != 0, append user to name to have identical filenames
if(user > 0) filename = $"{user:X1}:{filename}";
if(!string.IsNullOrEmpty(extension)) filename = filename + "." + extension;
// Do not repeat passwords
if(passwordCache.ContainsKey(filename)) passwordCache.Remove(filename);
// Copy whole password entry
byte[] tmp = new byte[32];
Array.Copy(directory, dOff, tmp, 0, 32);
passwordCache.Add(filename, tmp);
// Count entries 3 by 3 for timestamps
switch(dirCnt % 3)
{
case 0:
file1 = filename;
break;
case 1:
file2 = filename;
break;
case 2:
file3 = filename;
break;
}
dirCnt++;
}
// Volume label and password entry. Volume password is ignored.
else
switch(directory[dOff] & 0x7F)
{
case 0x20:
LabelEntry labelEntry =
Marshal.ByteArrayToStructureLittleEndian<LabelEntry>(directory, dOff, 32);
// The volume label defines if one of the fields in CP/M 3 timestamp is a creation or an
// access time
atime |= (labelEntry.flags & 0x40) == 0x40;
label = Encoding.ASCII.GetString(directory, dOff + 1, 11).Trim();
labelCreationDate = new byte[4];
labelUpdateDate = new byte[4];
Array.Copy(directory, dOff + 24, labelCreationDate, 0, 4);
Array.Copy(directory, dOff + 28, labelUpdateDate, 0, 4);
// Count entries 3 by 3 for timestamps
switch(dirCnt % 3)
{
case 0:
file1 = null;
break;
case 1:
file2 = null;
break;
case 2:
file3 = null;
break;
}
dirCnt++;
break;
case 0x21:
if(directory[dOff + 10] == 0x00 && directory[dOff + 20] == 0x00 &&
directory[dOff + 30] == 0x00 && directory[dOff + 31] == 0x00)
{
DateEntry dateEntry =
Marshal.ByteArrayToStructureLittleEndian<DateEntry>(directory, dOff, 32);
FileEntryInfo fInfo;
// Entry contains timestamps for last 3 entries, whatever the kind they are.
if(!string.IsNullOrEmpty(file1))
{
if(statCache.TryGetValue(file1, out fInfo)) statCache.Remove(file1);
else fInfo = new FileEntryInfo();
if(atime) fInfo.AccessTime = DateHandlers.CpmToDateTime(dateEntry.date1);
else fInfo.CreationTime = DateHandlers.CpmToDateTime(dateEntry.date1);
fInfo.LastWriteTime = DateHandlers.CpmToDateTime(dateEntry.date2);
statCache.Add(file1, fInfo);
}
if(!string.IsNullOrEmpty(file2))
{
if(statCache.TryGetValue(file2, out fInfo)) statCache.Remove(file2);
else fInfo = new FileEntryInfo();
if(atime) fInfo.AccessTime = DateHandlers.CpmToDateTime(dateEntry.date3);
else fInfo.CreationTime = DateHandlers.CpmToDateTime(dateEntry.date3);
fInfo.LastWriteTime = DateHandlers.CpmToDateTime(dateEntry.date4);
statCache.Add(file2, fInfo);
}
if(!string.IsNullOrEmpty(file3))
{
if(statCache.TryGetValue(file3, out fInfo)) statCache.Remove(file3);
else fInfo = new FileEntryInfo();
if(atime) fInfo.AccessTime = DateHandlers.CpmToDateTime(dateEntry.date5);
else fInfo.CreationTime = DateHandlers.CpmToDateTime(dateEntry.date5);
fInfo.LastWriteTime = DateHandlers.CpmToDateTime(dateEntry.date6);
statCache.Add(file3, fInfo);
}
file1 = null;
file2 = null;
file3 = null;
dirCnt = 0;
}
// However, if this byte is 0, timestamp is in Z80DOS or DOS+ format
else if(directory[dOff + 1] == 0x00)
{
TrdPartyDateEntry trdPartyDateEntry =
Marshal.ByteArrayToStructureLittleEndian<TrdPartyDateEntry>(directory, dOff, 32);
FileEntryInfo fInfo;
// Entry contains timestamps for last 3 entries, whatever the kind they are.
if(!string.IsNullOrEmpty(file1))
{
if(statCache.TryGetValue(file1, out fInfo)) statCache.Remove(file1);
else fInfo = new FileEntryInfo();
byte[] ctime = new byte[4];
ctime[0] = trdPartyDateEntry.create1[0];
ctime[1] = trdPartyDateEntry.create1[1];
fInfo.AccessTime = DateHandlers.CpmToDateTime(trdPartyDateEntry.access1);
fInfo.CreationTime = DateHandlers.CpmToDateTime(ctime);
fInfo.LastWriteTime = DateHandlers.CpmToDateTime(trdPartyDateEntry.modify1);
statCache.Add(file1, fInfo);
}
if(!string.IsNullOrEmpty(file2))
{
if(statCache.TryGetValue(file2, out fInfo)) statCache.Remove(file2);
else fInfo = new FileEntryInfo();
byte[] ctime = new byte[4];
ctime[0] = trdPartyDateEntry.create2[0];
ctime[1] = trdPartyDateEntry.create2[1];
fInfo.AccessTime = DateHandlers.CpmToDateTime(trdPartyDateEntry.access2);
fInfo.CreationTime = DateHandlers.CpmToDateTime(ctime);
fInfo.LastWriteTime = DateHandlers.CpmToDateTime(trdPartyDateEntry.modify2);
statCache.Add(file2, fInfo);
}
if(!string.IsNullOrEmpty(file3))
{
if(statCache.TryGetValue(file1, out fInfo)) statCache.Remove(file3);
else fInfo = new FileEntryInfo();
byte[] ctime = new byte[4];
ctime[0] = trdPartyDateEntry.create3[0];
ctime[1] = trdPartyDateEntry.create3[1];
fInfo.AccessTime = DateHandlers.CpmToDateTime(trdPartyDateEntry.access3);
fInfo.CreationTime = DateHandlers.CpmToDateTime(ctime);
fInfo.LastWriteTime = DateHandlers.CpmToDateTime(trdPartyDateEntry.modify3);
statCache.Add(file3, fInfo);
}
file1 = null;
file2 = null;
file3 = null;
dirCnt = 0;
}
break;
}
// Cache all files. As CP/M maximum volume size is 8 Mib
// this should not be a problem
DicConsole.DebugWriteLine("CP/M Plugin", "Reading files.");
long usedBlocks = 0;
fileCache = new Dictionary<string, byte[]>();
foreach(string filename in dirList)
{
MemoryStream fileMs = new MemoryStream();
if(statCache.TryGetValue(filename, out FileEntryInfo fInfo)) statCache.Remove(filename);
fInfo.Blocks = 0;
if(fileExtents.TryGetValue(filename, out Dictionary<int, List<ushort>> extents))
for(int ex = 0; ex < extents.Count; ex++)
{
if(!extents.TryGetValue(ex, out List<ushort> alBlks)) continue;
foreach(ushort alBlk in alBlks)
{
allocationBlocks.TryGetValue(alBlk, out byte[] blk);
fileMs.Write(blk, 0, blk.Length);
fInfo.Blocks++;
}
}
// If you insist to call CP/M "extent based"
fInfo.Attributes |= FileAttributes.Extents;
fInfo.BlockSize = blockSize;
fInfo.Length = fileMs.Length;
cpmStat.Files++;
usedBlocks += fInfo.Blocks;
statCache.Add(filename, fInfo);
fileCache.Add(filename, fileMs.ToArray());
}
decodedPasswordCache = new Dictionary<string, byte[]>();
// For each stored password, store a decoded version of it
if(passwordCache.Count > 0)
foreach(KeyValuePair<string, byte[]> kvp in passwordCache)
{
byte[] tmp = new byte[8];
Array.Copy(kvp.Value, 16, tmp, 0, 8);
for(int t = 0; t < 8; t++) tmp[t] ^= kvp.Value[13];
decodedPasswordCache.Add(kvp.Key, tmp);
}
// Generate statfs.
cpmStat.Blocks = (ulong)(dpb.dsm + 1);
cpmStat.FilenameLength = 11;
cpmStat.Files = (ulong)fileCache.Count;
cpmStat.FreeBlocks = cpmStat.Blocks - (ulong)usedBlocks;
cpmStat.PluginId = Id;
cpmStat.Type = "CP/M filesystem";
// Generate XML info
XmlFsType = new FileSystemType
{
Clusters = cpmStat.Blocks,
ClusterSize = (uint)blockSize,
Files = (ulong)fileCache.Count,
FilesSpecified = true,
FreeClusters = cpmStat.FreeBlocks,
FreeClustersSpecified = true,
Type = "CP/M filesystem"
};
if(labelCreationDate != null)
{
XmlFsType.CreationDate = DateHandlers.CpmToDateTime(labelCreationDate);
XmlFsType.CreationDateSpecified = true;
}
if(labelUpdateDate != null)
{
XmlFsType.ModificationDate = DateHandlers.CpmToDateTime(labelUpdateDate);
XmlFsType.ModificationDateSpecified = true;
}
if(!string.IsNullOrEmpty(label)) XmlFsType.VolumeName = label;
mounted = true;
return Errno.NoError;
}
/// <summary>
/// Gets information about the mounted volume.
/// </summary>
/// <param name="stat">Information about the mounted volume.</param>
public Errno StatFs(out FileSystemInfo stat)
{
stat = null;
if(!mounted) return Errno.AccessDenied;
stat = cpmStat;
return Errno.NoError;
}
public Errno Unmount()
{
mounted = false;
definitions = null;
cpmFound = false;
workingDefinition = null;
dpb = null;
sectorMask = null;
label = null;
thirdPartyTimestamps = false;
standardTimestamps = false;
labelCreationDate = null;
labelUpdateDate = null;
return Errno.NoError;
}
}
}

View File

@@ -0,0 +1,94 @@
// /***************************************************************************
// The Disc Image Chef
// ----------------------------------------------------------------------------
//
// Filename : Xattr.cs
// Author(s) : Natalia Portillo <claunia@claunia.com>
//
// Component : CP/M filesystem plugin.
//
// --[ Description ] ----------------------------------------------------------
//
// Methods to handle CP/M extended attributes (password).
//
// --[ 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/>.
//
// ----------------------------------------------------------------------------
// Copyright © 2011-2020 Natalia Portillo
// ****************************************************************************/
using System;
using System.Collections.Generic;
using DiscImageChef.CommonTypes.Structs;
namespace DiscImageChef.Filesystems.CPM
{
partial class CPM
{
/// <summary>
/// Reads an extended attribute, alternate data stream or fork from the given file.
/// </summary>
/// <returns>Error number.</returns>
/// <param name="path">File path.</param>
/// <param name="xattr">Extendad attribute, alternate data stream or fork name.</param>
/// <param name="buf">Buffer.</param>
public Errno GetXattr(string path, string xattr, ref byte[] buf)
{
if(!mounted) return Errno.AccessDenied;
string[] pathElements = path.Split(new[] {'/'}, StringSplitOptions.RemoveEmptyEntries);
if(pathElements.Length != 1) return Errno.NotSupported;
if(!fileCache.ContainsKey(pathElements[0].ToUpperInvariant())) return Errno.NoSuchFile;
if(string.Compare(xattr, "com.caldera.cpm.password", StringComparison.InvariantCulture) == 0)
if(!passwordCache.TryGetValue(pathElements[0].ToUpperInvariant(), out buf))
return Errno.NoError;
if(string.Compare(xattr, "com.caldera.cpm.password.text", StringComparison.InvariantCulture) != 0)
return Errno.NoSuchExtendedAttribute;
return !passwordCache.TryGetValue(pathElements[0].ToUpperInvariant(), out buf)
? Errno.NoError
: Errno.NoSuchExtendedAttribute;
}
/// <summary>
/// Lists all extended attributes, alternate data streams and forks of the given file.
/// </summary>
/// <returns>Error number.</returns>
/// <param name="path">Path.</param>
/// <param name="xattrs">List of extended attributes, alternate data streams and forks.</param>
public Errno ListXAttr(string path, out List<string> xattrs)
{
xattrs = null;
if(!mounted) return Errno.AccessDenied;
string[] pathElements = path.Split(new[] {'/'}, StringSplitOptions.RemoveEmptyEntries);
if(pathElements.Length != 1) return Errno.NotSupported;
if(!fileCache.ContainsKey(pathElements[0].ToUpperInvariant())) return Errno.NoSuchFile;
xattrs = new List<string>();
if(passwordCache.ContainsKey(pathElements[0].ToUpperInvariant())) xattrs.Add("com.caldera.cpm.password");
if(decodedPasswordCache.ContainsKey(pathElements[0].ToUpperInvariant()))
xattrs.Add("com.caldera.cpm.password.text");
return Errno.NoError;
}
}
}

File diff suppressed because it is too large Load Diff