// /*************************************************************************** // The Disc Image Chef // ---------------------------------------------------------------------------- // // Filename : extFS.cs // Author(s) : Natalia Portillo // // Component : Linux extended filesystem plugin. // // --[ Description ] ---------------------------------------------------------- // // Identifies the Linux extended 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-2018 Natalia Portillo // ****************************************************************************/ using System; using System.Collections.Generic; using System.Diagnostics.CodeAnalysis; using System.Text; using DiscImageChef.CommonTypes; using DiscImageChef.DiscImages; using Schemas; namespace DiscImageChef.Filesystems { // Information from the Linux kernel public class extFS : IFilesystem { const int SB_POS = 0x400; /// /// ext superblock magic /// const ushort EXT_MAGIC = 0x137D; Encoding currentEncoding; FileSystemType xmlFsType; public virtual FileSystemType XmlFsType => xmlFsType; public virtual string Name => "Linux extended Filesystem"; public virtual Guid Id => new Guid("076CB3A2-08C2-4D69-BC8A-FCAA2E502BE2"); public virtual Encoding Encoding => currentEncoding; public virtual bool Identify(IMediaImage imagePlugin, Partition partition) { if(imagePlugin.Info.SectorSize < 512) return false; ulong sbSectorOff = SB_POS / imagePlugin.Info.SectorSize; uint sbOff = SB_POS % imagePlugin.Info.SectorSize; if(sbSectorOff + partition.Start >= partition.End) return false; byte[] sbSector = imagePlugin.ReadSector(sbSectorOff + partition.Start); byte[] sb = new byte[512]; Array.Copy(sbSector, sbOff, sb, 0, 512); ushort magic = BitConverter.ToUInt16(sb, 0x038); return magic == EXT_MAGIC; } public virtual void GetInformation(IMediaImage imagePlugin, Partition partition, out string information, Encoding encoding) { currentEncoding = encoding ?? Encoding.GetEncoding("iso-8859-15"); information = ""; StringBuilder sb = new StringBuilder(); if(imagePlugin.Info.SectorSize < 512) return; ulong sbSectorOff = SB_POS / imagePlugin.Info.SectorSize; uint sbOff = SB_POS % imagePlugin.Info.SectorSize; if(sbSectorOff + partition.Start >= partition.End) return; byte[] sblock = imagePlugin.ReadSector(sbSectorOff + partition.Start); byte[] sbSector = new byte[512]; Array.Copy(sblock, sbOff, sbSector, 0, 512); extFSSuperBlock extSb = new extFSSuperBlock { inodes = BitConverter.ToUInt32(sbSector, 0x000), zones = BitConverter.ToUInt32(sbSector, 0x004), firstfreeblk = BitConverter.ToUInt32(sbSector, 0x008), freecountblk = BitConverter.ToUInt32(sbSector, 0x00C), firstfreeind = BitConverter.ToUInt32(sbSector, 0x010), freecountind = BitConverter.ToUInt32(sbSector, 0x014), firstdatazone = BitConverter.ToUInt32(sbSector, 0x018), logzonesize = BitConverter.ToUInt32(sbSector, 0x01C), maxsize = BitConverter.ToUInt32(sbSector, 0x020) }; sb.AppendLine("ext filesystem"); sb.AppendFormat("{0} zones on volume", extSb.zones); sb.AppendFormat("{0} free blocks ({1} bytes)", extSb.freecountblk, extSb.freecountblk * 1024); sb.AppendFormat("{0} inodes on volume, {1} free ({2}%)", extSb.inodes, extSb.freecountind, extSb.freecountind * 100 / extSb.inodes); sb.AppendFormat("First free inode is {0}", extSb.firstfreeind); sb.AppendFormat("First free block is {0}", extSb.firstfreeblk); sb.AppendFormat("First data zone is {0}", extSb.firstdatazone); sb.AppendFormat("Log zone size: {0}", extSb.logzonesize); sb.AppendFormat("Max zone size: {0}", extSb.maxsize); xmlFsType = new FileSystemType { Type = "ext", FreeClusters = extSb.freecountblk, FreeClustersSpecified = true, ClusterSize = 1024, Clusters = (long)((partition.End - partition.Start + 1) * imagePlugin.Info.SectorSize / 1024) }; information = sb.ToString(); } /// /// ext superblock /// [SuppressMessage("ReSharper", "InconsistentNaming")] struct extFSSuperBlock { /// 0x000, inodes on volume public uint inodes; /// 0x004, zones on volume public uint zones; /// 0x008, first free block public uint firstfreeblk; /// 0x00C, free blocks count public uint freecountblk; /// 0x010, first free inode public uint firstfreeind; /// 0x014, free inodes count public uint freecountind; /// 0x018, first data zone public uint firstdatazone; /// 0x01C, log zone size public uint logzonesize; /// 0x020, max zone size public uint maxsize; /// 0x024, reserved public uint reserved1; /// 0x028, reserved public uint reserved2; /// 0x02C, reserved public uint reserved3; /// 0x030, reserved public uint reserved4; /// 0x034, reserved public uint reserved5; /// 0x038, 0x137D (little endian) public ushort magic; } } }