// /*************************************************************************** // The Disc Image Chef // ---------------------------------------------------------------------------- // // Filename : RBF.cs // Author(s) : Natalia Portillo // // Component : Random Block File filesystem plugin // // --[ Description ] ---------------------------------------------------------- // // Identifies the Random Block File 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 . // // ---------------------------------------------------------------------------- // Copyright © 2011-2020 Natalia Portillo // ****************************************************************************/ using System; using System.Runtime.InteropServices; using System.Text; using DiscImageChef.CommonTypes; using DiscImageChef.CommonTypes.Interfaces; using DiscImageChef.Console; using Schemas; using Marshal = DiscImageChef.Helpers.Marshal; namespace DiscImageChef.Filesystems { public class RBF : IFilesystem { /// Magic number for OS-9. Same for OS-9000? const uint RBF_SYNC = 0x4372757A; const uint RBF_CNYS = 0x7A757243; public FileSystemType XmlFsType { get; private set; } public Encoding Encoding { get; private set; } public string Name => "OS-9 Random Block File Plugin"; public Guid Id => new Guid("E864E45B-0B52-4D29-A858-7BDFA9199FB2"); public string Author => "Natalia Portillo"; public bool Identify(IMediaImage imagePlugin, Partition partition) { if(imagePlugin.Info.SectorSize < 256) return false; // Documentation says ID should be sector 0 // I've found that OS-9/X68000 has it on sector 4 // I've read OS-9/Apple2 has it on sector 15 foreach(int i in new[] {0, 4, 15}) { ulong location = (ulong)i; uint sbSize = (uint)(Marshal.SizeOf() / imagePlugin.Info.SectorSize); if(Marshal.SizeOf() % imagePlugin.Info.SectorSize != 0) sbSize++; if(partition.Start + location + sbSize >= imagePlugin.Info.Sectors) break; byte[] sector = imagePlugin.ReadSectors(partition.Start + location, sbSize); if(sector.Length < Marshal.SizeOf()) return false; RBF_IdSector rbfSb = Marshal.ByteArrayToStructureBigEndian(sector); RBF_NewIdSector rbf9000Sb = Marshal.ByteArrayToStructureBigEndian(sector); DicConsole.DebugWriteLine("RBF plugin", "magic at {0} = 0x{1:X8} or 0x{2:X8} (expected 0x{3:X8} or 0x{4:X8})", location, rbfSb.dd_sync, rbf9000Sb.rid_sync, RBF_SYNC, RBF_CNYS); if(rbfSb.dd_sync == RBF_SYNC || rbf9000Sb.rid_sync == RBF_SYNC || rbf9000Sb.rid_sync == RBF_CNYS) return true; } return false; } public void GetInformation(IMediaImage imagePlugin, Partition partition, out string information, Encoding encoding) { Encoding = encoding ?? Encoding.GetEncoding("iso-8859-15"); information = ""; if(imagePlugin.Info.SectorSize < 256) return; RBF_IdSector rbfSb = new RBF_IdSector(); RBF_NewIdSector rbf9000Sb = new RBF_NewIdSector(); foreach(int i in new[] {0, 4, 15}) { ulong location = (ulong)i; uint sbSize = (uint)(Marshal.SizeOf() / imagePlugin.Info.SectorSize); if(Marshal.SizeOf() % imagePlugin.Info.SectorSize != 0) sbSize++; byte[] sector = imagePlugin.ReadSectors(partition.Start + location, sbSize); if(sector.Length < Marshal.SizeOf()) return; rbfSb = Marshal.ByteArrayToStructureBigEndian(sector); rbf9000Sb = Marshal.ByteArrayToStructureBigEndian(sector); DicConsole.DebugWriteLine("RBF plugin", "magic at {0} = 0x{1:X8} or 0x{2:X8} (expected 0x{3:X8} or 0x{4:X8})", location, rbfSb.dd_sync, rbf9000Sb.rid_sync, RBF_SYNC, RBF_CNYS); if(rbfSb.dd_sync == RBF_SYNC || rbf9000Sb.rid_sync == RBF_SYNC || rbf9000Sb.rid_sync == RBF_CNYS) break; } if(rbfSb.dd_sync != RBF_SYNC && rbf9000Sb.rid_sync != RBF_SYNC && rbf9000Sb.rid_sync != RBF_CNYS) return; if(rbf9000Sb.rid_sync == RBF_CNYS) rbf9000Sb = (RBF_NewIdSector)Marshal.SwapStructureMembersEndian(rbf9000Sb); StringBuilder sb = new StringBuilder(); sb.AppendLine("OS-9 Random Block File"); if(rbf9000Sb.rid_sync == RBF_SYNC) { sb.AppendFormat("Volume ID: {0:X8}", rbf9000Sb.rid_diskid).AppendLine(); sb.AppendFormat("{0} blocks in volume", rbf9000Sb.rid_totblocks).AppendLine(); sb.AppendFormat("{0} cylinders", rbf9000Sb.rid_cylinders).AppendLine(); sb.AppendFormat("{0} blocks in cylinder 0", rbf9000Sb.rid_cyl0size).AppendLine(); sb.AppendFormat("{0} blocks per cylinder", rbf9000Sb.rid_cylsize).AppendLine(); sb.AppendFormat("{0} heads", rbf9000Sb.rid_heads).AppendLine(); sb.AppendFormat("{0} bytes per block", rbf9000Sb.rid_blocksize).AppendLine(); // TODO: Convert to flags? sb.AppendLine((rbf9000Sb.rid_format & 0x01) == 0x01 ? "Disk is double sided" : "Disk is single sided"); sb.AppendLine((rbf9000Sb.rid_format & 0x02) == 0x02 ? "Disk is double density" : "Disk is single density"); if((rbf9000Sb.rid_format & 0x10) == 0x10) sb.AppendLine("Disk is 384 TPI"); else if((rbf9000Sb.rid_format & 0x08) == 0x08) sb.AppendLine("Disk is 192 TPI"); else if((rbf9000Sb.rid_format & 0x04) == 0x04) sb.AppendLine("Disk is 96 TPI or 135 TPI"); else sb.AppendLine("Disk is 48 TPI"); sb.AppendFormat("Allocation bitmap descriptor starts at block {0}", rbf9000Sb.rid_bitmap == 0 ? 1 : rbf9000Sb.rid_bitmap).AppendLine(); if(rbf9000Sb.rid_firstboot > 0) sb.AppendFormat("Debugger descriptor starts at block {0}", rbf9000Sb.rid_firstboot).AppendLine(); if(rbf9000Sb.rid_bootfile > 0) sb.AppendFormat("Boot file descriptor starts at block {0}", rbf9000Sb.rid_bootfile).AppendLine(); sb.AppendFormat("Root directory descriptor starts at block {0}", rbf9000Sb.rid_rootdir).AppendLine(); sb.AppendFormat("Disk is owned by group {0} user {1}", rbf9000Sb.rid_group, rbf9000Sb.rid_owner) .AppendLine(); sb.AppendFormat("Volume was created on {0}", DateHandlers.UnixToDateTime(rbf9000Sb.rid_ctime)) .AppendLine(); sb.AppendFormat("Volume's identification block was last written on {0}", DateHandlers.UnixToDateTime(rbf9000Sb.rid_mtime)).AppendLine(); sb.AppendFormat("Volume name: {0}", StringHandlers.CToString(rbf9000Sb.rid_name, Encoding)) .AppendLine(); XmlFsType = new FileSystemType { Type = "OS-9 Random Block File", Bootable = rbf9000Sb.rid_bootfile > 0, ClusterSize = rbf9000Sb.rid_blocksize, Clusters = rbf9000Sb.rid_totblocks, CreationDate = DateHandlers.UnixToDateTime(rbf9000Sb.rid_ctime), CreationDateSpecified = true, ModificationDate = DateHandlers.UnixToDateTime(rbf9000Sb.rid_mtime), ModificationDateSpecified = true, VolumeName = StringHandlers.CToString(rbf9000Sb.rid_name, Encoding), VolumeSerial = $"{rbf9000Sb.rid_diskid:X8}" }; } else { sb.AppendFormat("Volume ID: {0:X4}", rbfSb.dd_dsk).AppendLine(); sb.AppendFormat("{0} blocks in volume", LSNToUInt32(rbfSb.dd_tot)).AppendLine(); sb.AppendFormat("{0} tracks", rbfSb.dd_tks).AppendLine(); sb.AppendFormat("{0} sectors per track", rbfSb.dd_spt).AppendLine(); sb.AppendFormat("{0} bytes per sector", 256 << rbfSb.dd_lsnsize).AppendLine(); sb.AppendFormat("{0} sectors per cluster ({1} bytes)", rbfSb.dd_bit, rbfSb.dd_bit * (256 << rbfSb.dd_lsnsize)).AppendLine(); // TODO: Convert to flags? sb.AppendLine((rbfSb.dd_fmt & 0x01) == 0x01 ? "Disk is double sided" : "Disk is single sided"); sb.AppendLine((rbfSb.dd_fmt & 0x02) == 0x02 ? "Disk is double density" : "Disk is single density"); if((rbfSb.dd_fmt & 0x10) == 0x10) sb.AppendLine("Disk is 384 TPI"); else if((rbfSb.dd_fmt & 0x08) == 0x08) sb.AppendLine("Disk is 192 TPI"); else if((rbfSb.dd_fmt & 0x04) == 0x04) sb.AppendLine("Disk is 96 TPI or 135 TPI"); else sb.AppendLine("Disk is 48 TPI"); sb.AppendFormat("Allocation bitmap descriptor starts at block {0}", rbfSb.dd_maplsn == 0 ? 1 : rbfSb.dd_maplsn).AppendLine(); sb.AppendFormat("{0} bytes in allocation bitmap", rbfSb.dd_map).AppendLine(); if(LSNToUInt32(rbfSb.dd_bt) > 0 && rbfSb.dd_bsz > 0) sb.AppendFormat("Boot file starts at block {0} and has {1} bytes", LSNToUInt32(rbfSb.dd_bt), rbfSb.dd_bsz).AppendLine(); sb.AppendFormat("Root directory descriptor starts at block {0}", LSNToUInt32(rbfSb.dd_dir)) .AppendLine(); sb.AppendFormat("Disk is owned by user {0}", rbfSb.dd_own).AppendLine(); sb.AppendFormat("Volume was created on {0}", DateHandlers.Os9ToDateTime(rbfSb.dd_dat)).AppendLine(); sb.AppendFormat("Volume attributes: {0:X2}", rbfSb.dd_att).AppendLine(); sb.AppendFormat("Volume name: {0}", StringHandlers.CToString(rbfSb.dd_nam, Encoding)).AppendLine(); sb.AppendFormat("Path descriptor options: {0}", StringHandlers.CToString(rbfSb.dd_opt, Encoding)) .AppendLine(); XmlFsType = new FileSystemType { Type = "OS-9 Random Block File", Bootable = LSNToUInt32(rbfSb.dd_bt) > 0 && rbfSb.dd_bsz > 0, ClusterSize = (uint)(rbfSb.dd_bit * (256 << rbfSb.dd_lsnsize)), Clusters = LSNToUInt32(rbfSb.dd_tot), CreationDate = DateHandlers.Os9ToDateTime(rbfSb.dd_dat), CreationDateSpecified = true, VolumeName = StringHandlers.CToString(rbfSb.dd_nam, Encoding), VolumeSerial = $"{rbfSb.dd_dsk:X4}" }; } information = sb.ToString(); } static uint LSNToUInt32(byte[] lsn) { if(lsn == null || lsn.Length != 3) return 0; return (uint)((lsn[0] << 16) + (lsn[1] << 8) + lsn[2]); } /// /// Identification sector. Wherever the sector this resides on, becomes LSN 0. /// [StructLayout(LayoutKind.Sequential, Pack = 1)] struct RBF_IdSector { /// Sectors on disk [MarshalAs(UnmanagedType.ByValArray, SizeConst = 3)] public readonly byte[] dd_tot; /// Tracks public readonly byte dd_tks; /// Bytes in allocation map public readonly ushort dd_map; /// Sectors per cluster public readonly ushort dd_bit; /// LSN of root directory [MarshalAs(UnmanagedType.ByValArray, SizeConst = 3)] public readonly byte[] dd_dir; /// Owner ID public readonly ushort dd_own; /// Attributes public readonly byte dd_att; /// Disk ID public readonly ushort dd_dsk; /// Format byte public readonly byte dd_fmt; /// Sectors per track public readonly ushort dd_spt; /// Reserved public readonly ushort dd_res; /// LSN of boot file [MarshalAs(UnmanagedType.ByValArray, SizeConst = 3)] public readonly byte[] dd_bt; /// Size of boot file public readonly ushort dd_bsz; /// Creation date [MarshalAs(UnmanagedType.ByValArray, SizeConst = 5)] public readonly byte[] dd_dat; /// Volume name [MarshalAs(UnmanagedType.ByValArray, SizeConst = 32)] public readonly byte[] dd_nam; /// Path options [MarshalAs(UnmanagedType.ByValArray, SizeConst = 32)] public readonly byte[] dd_opt; /// Reserved public readonly byte reserved; /// Magic number public readonly uint dd_sync; /// LSN of allocation map public readonly uint dd_maplsn; /// Size of an LSN public readonly ushort dd_lsnsize; /// Version ID public readonly ushort dd_versid; } /// /// Identification sector. Wherever the sector this resides on, becomes LSN 0. /// Introduced on OS-9000, this can be big or little endian. /// [StructLayout(LayoutKind.Sequential, Pack = 1)] struct RBF_NewIdSector { /// Magic number public readonly uint rid_sync; /// Disk ID public readonly uint rid_diskid; /// Sectors on disk public readonly uint rid_totblocks; /// Cylinders public readonly ushort rid_cylinders; /// Sectors in cylinder 0 public readonly ushort rid_cyl0size; /// Sectors per cylinder public readonly ushort rid_cylsize; /// Heads public readonly ushort rid_heads; /// Bytes per sector public readonly ushort rid_blocksize; /// Disk format public readonly ushort rid_format; /// Flags public readonly ushort rid_flags; /// Padding public readonly ushort rid_unused1; /// Sector of allocation bitmap public readonly uint rid_bitmap; /// Sector of debugger FD public readonly uint rid_firstboot; /// Sector of bootfile FD public readonly uint rid_bootfile; /// Sector of root directory FD public readonly uint rid_rootdir; /// Group owner of media public readonly ushort rid_group; /// Owner of media public readonly ushort rid_owner; /// Creation time public readonly uint rid_ctime; /// Last write time for this structure public readonly uint rid_mtime; /// Volume name [MarshalAs(UnmanagedType.ByValArray, SizeConst = 32)] public readonly byte[] rid_name; /// Endian flag public readonly byte rid_endflag; /// Padding [MarshalAs(UnmanagedType.ByValArray, SizeConst = 3)] public readonly byte[] rid_unused2; /// Parity public readonly uint rid_parity; } } }