// /*************************************************************************** // Aaru Data Preservation Suite // ---------------------------------------------------------------------------- // // Filename : Atheos.cs // Author(s) : Natalia Portillo // // Component : Atheos filesystem plugin. // // --[ Description ] ---------------------------------------------------------- // // Identifies the Atheos 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-2023 Natalia Portillo // ****************************************************************************/ using System; using System.Diagnostics.CodeAnalysis; using System.Runtime.InteropServices; using System.Text; using Aaru.CommonTypes; using Aaru.CommonTypes.Enums; using Aaru.CommonTypes.Interfaces; using Aaru.Helpers; using Schemas; using Marshal = Aaru.Helpers.Marshal; namespace Aaru.Filesystems; /// /// Implements detection for the AtheOS filesystem [SuppressMessage("ReSharper", "UnusedMember.Local")] public sealed class AtheOS : IFilesystem { // Little endian constants (that is, as read by .NET :p) const uint AFS_MAGIC1 = 0x41465331; const uint AFS_MAGIC2 = 0xDD121031; const uint AFS_MAGIC3 = 0x15B6830E; // Common constants const uint AFS_SUPERBLOCK_SIZE = 1024; const uint AFS_BOOTBLOCK_SIZE = AFS_SUPERBLOCK_SIZE; /// public FileSystemType XmlFsType { get; private set; } /// public Encoding Encoding { get; private set; } /// public string Name => Localization.AtheOS_Name; /// public Guid Id => new("AAB2C4F1-DC07-49EE-A948-576CC51B58C5"); /// public string Author => Authors.NataliaPortillo; /// public bool Identify(IMediaImage imagePlugin, Partition partition) { ulong sector = AFS_BOOTBLOCK_SIZE / imagePlugin.Info.SectorSize; uint offset = AFS_BOOTBLOCK_SIZE % imagePlugin.Info.SectorSize; uint run = 1; if(imagePlugin.Info.SectorSize < AFS_SUPERBLOCK_SIZE) run = AFS_SUPERBLOCK_SIZE / imagePlugin.Info.SectorSize; if(sector + partition.Start >= partition.End) return false; ErrorNumber errno = imagePlugin.ReadSectors(sector + partition.Start, run, out byte[] tmp); if(errno != ErrorNumber.NoError) return false; byte[] sbSector = new byte[AFS_SUPERBLOCK_SIZE]; if(offset + AFS_SUPERBLOCK_SIZE > tmp.Length) return false; Array.Copy(tmp, offset, sbSector, 0, AFS_SUPERBLOCK_SIZE); uint magic = BitConverter.ToUInt32(sbSector, 0x20); return magic == AFS_MAGIC1; } /// public void GetInformation(IMediaImage imagePlugin, Partition partition, out string information, Encoding encoding) { Encoding = encoding ?? Encoding.GetEncoding("iso-8859-15"); information = ""; var sb = new StringBuilder(); ulong sector = AFS_BOOTBLOCK_SIZE / imagePlugin.Info.SectorSize; uint offset = AFS_BOOTBLOCK_SIZE % imagePlugin.Info.SectorSize; uint run = 1; if(imagePlugin.Info.SectorSize < AFS_SUPERBLOCK_SIZE) run = AFS_SUPERBLOCK_SIZE / imagePlugin.Info.SectorSize; ErrorNumber errno = imagePlugin.ReadSectors(sector + partition.Start, run, out byte[] tmp); if(errno != ErrorNumber.NoError) return; byte[] sbSector = new byte[AFS_SUPERBLOCK_SIZE]; Array.Copy(tmp, offset, sbSector, 0, AFS_SUPERBLOCK_SIZE); SuperBlock afsSb = Marshal.ByteArrayToStructureLittleEndian(sbSector); sb.AppendLine(Localization.Atheos_filesystem); if(afsSb.flags == 1) sb.AppendLine(Localization.Filesystem_is_read_only); sb.AppendFormat(Localization.Volume_name_0, StringHandlers.CToString(afsSb.name, Encoding)).AppendLine(); sb.AppendFormat(Localization._0_bytes_per_block, afsSb.block_size).AppendLine(); sb.AppendFormat(Localization._0_blocks_in_volume_1_bytes, afsSb.num_blocks, afsSb.num_blocks * afsSb.block_size).AppendLine(); sb.AppendFormat(Localization._0_used_blocks_1_bytes, afsSb.used_blocks, afsSb.used_blocks * afsSb.block_size). AppendLine(); sb.AppendFormat(Localization._0_bytes_per_i_node, afsSb.inode_size).AppendLine(); sb.AppendFormat(Localization._0_blocks_per_allocation_group_1_bytes, afsSb.blocks_per_ag, afsSb.blocks_per_ag * afsSb.block_size).AppendLine(); sb.AppendFormat(Localization._0_allocation_groups_in_volume, afsSb.num_ags).AppendLine(); sb.AppendFormat(Localization.Journal_resides_in_block_0_of_allocation_group_1_and_runs_for_2_blocks_3_bytes, afsSb.log_blocks_start, afsSb.log_blocks_ag, afsSb.log_blocks_len, afsSb.log_blocks_len * afsSb.block_size).AppendLine(); sb.AppendFormat(Localization.Journal_starts_in_byte_0_and_has_1_bytes_in_2_blocks, afsSb.log_start, afsSb.log_size, afsSb.log_valid_blocks).AppendLine(); sb. AppendFormat(Localization.Root_folder_s_i_node_resides_in_block_0_of_allocation_group_1_and_runs_for_2_blocks_3_bytes, afsSb.root_dir_start, afsSb.root_dir_ag, afsSb.root_dir_len, afsSb.root_dir_len * afsSb.block_size).AppendLine(); sb. AppendFormat(Localization.Directory_containing_files_scheduled_for_deletion_i_node_resides_in_block_0_of_allocation_group_1_and_runs_for_2_blocks_3_bytes, afsSb.deleted_start, afsSb.deleted_ag, afsSb.deleted_len, afsSb.deleted_len * afsSb.block_size).AppendLine(); sb. AppendFormat(Localization.Indices_i_node_resides_in_block_0_of_allocation_group_1_and_runs_for_2_blocks_3_bytes, afsSb.indices_start, afsSb.indices_ag, afsSb.indices_len, afsSb.indices_len * afsSb.block_size).AppendLine(); sb.AppendFormat(Localization._0_blocks_for_bootloader_1_bytes, afsSb.boot_size, afsSb.boot_size * afsSb.block_size).AppendLine(); information = sb.ToString(); XmlFsType = new FileSystemType { Clusters = (ulong)afsSb.num_blocks, ClusterSize = afsSb.block_size, Dirty = false, FreeClusters = (ulong)(afsSb.num_blocks - afsSb.used_blocks), FreeClustersSpecified = true, Type = FS_TYPE, VolumeName = StringHandlers.CToString(afsSb.name, Encoding) }; } const string FS_TYPE = "atheos"; /// Be superblock [StructLayout(LayoutKind.Sequential, Pack = 1)] readonly struct SuperBlock { /// 0x000, Volume name, 32 bytes [MarshalAs(UnmanagedType.ByValArray, SizeConst = 32)] public readonly byte[] name; /// 0x020, "AFS1", 0x41465331 public readonly uint magic1; /// 0x024, "BIGE", 0x42494745 public readonly uint fs_byte_order; /// 0x028, Bytes per block public readonly uint block_size; /// 0x02C, 1 << block_shift == block_size public readonly uint block_shift; /// 0x030, Blocks in volume public readonly long num_blocks; /// 0x038, Used blocks in volume public readonly long used_blocks; /// 0x040, Bytes per inode public readonly int inode_size; /// 0x044, 0xDD121031 public readonly uint magic2; /// 0x048, Blocks per allocation group public readonly int blocks_per_ag; /// 0x04C, 1 << ag_shift == blocks_per_ag public readonly int ag_shift; /// 0x050, Allocation groups in volume public readonly int num_ags; /// 0x054, 0x434c454e if clean, 0x44495254 if dirty public readonly uint flags; /// 0x058, Allocation group of journal public readonly int log_blocks_ag; /// 0x05C, Start block of journal, inside ag public readonly ushort log_blocks_start; /// 0x05E, Length in blocks of journal, inside ag public readonly ushort log_blocks_len; /// 0x060, Start of journal public readonly long log_start; /// 0x068, Valid block logs public readonly int log_valid_blocks; /// 0x06C, Log size public readonly int log_size; /// 0x070, 0x15B6830E public readonly uint magic3; /// 0x074, Allocation group where root folder's i-node resides public readonly int root_dir_ag; /// 0x078, Start in ag of root folder's i-node public readonly ushort root_dir_start; /// 0x07A, As this is part of inode_addr, this is 1 public readonly ushort root_dir_len; /// 0x07C, Allocation group where pending-delete-files' i-node resides public readonly int deleted_ag; /// 0x080, Start in ag of pending-delete-files' i-node public readonly ushort deleted_start; /// 0x082, As this is part of inode_addr, this is 1 public readonly ushort deleted_len; /// 0x084, Allocation group where indices' i-node resides public readonly int indices_ag; /// 0x088, Start in ag of indices' i-node public readonly ushort indices_start; /// 0x08A, As this is part of inode_addr, this is 1 public readonly ushort indices_len; /// 0x08C, Size of bootloader public readonly int boot_size; } }