Files
Aaru/Aaru.Filesystems/F2FS.cs

219 lines
9.8 KiB
C#
Raw Normal View History

// /***************************************************************************
2020-02-27 12:31:25 +00:00
// Aaru Data Preservation Suite
2016-09-02 18:10:54 +01:00
// ----------------------------------------------------------------------------
//
// Filename : F2FS.cs
// Author(s) : Natalia Portillo <claunia@claunia.com>
//
// Component : F2FS filesystem plugin.
2016-09-02 18:10:54 +01:00
//
// --[ Description ] ----------------------------------------------------------
//
// Identifies the F2FS 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/>.
//
// ----------------------------------------------------------------------------
2020-01-03 17:51:30 +00:00
// Copyright © 2011-2020 Natalia Portillo
2016-09-02 18:10:54 +01:00
// ****************************************************************************/
using System;
using System.Diagnostics.CodeAnalysis;
2016-09-02 18:10:54 +01:00
using System.Runtime.InteropServices;
using System.Text;
2020-02-27 00:33:26 +00:00
using Aaru.CommonTypes;
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;
2016-09-02 18:10:54 +01:00
2020-02-27 00:33:26 +00:00
namespace Aaru.Filesystems
2016-09-02 18:10:54 +01:00
{
public class F2FS : IFilesystem
2016-09-02 18:10:54 +01:00
{
2018-06-22 08:08:38 +01:00
const uint F2FS_MAGIC = 0xF2F52010;
const uint F2FS_SUPER_OFFSET = 1024;
2018-06-22 08:08:38 +01:00
const uint F2FS_MIN_SECTOR = 512;
const uint F2FS_MAX_SECTOR = 4096;
const uint F2FS_BLOCK_SIZE = 4096;
2016-09-02 18:10:54 +01:00
2017-12-26 08:01:40 +00:00
public FileSystemType XmlFsType { get; private set; }
2018-06-22 08:08:38 +01:00
public Encoding Encoding { get; private set; }
public string Name => "F2FS Plugin";
public Guid Id => new Guid("82B0920F-5F0D-4063-9F57-ADE0AE02ECE5");
2018-08-29 22:15:43 +01:00
public string Author => "Natalia Portillo";
public bool Identify(IMediaImage imagePlugin, Partition partition)
2016-09-02 18:10:54 +01:00
{
2020-02-29 18:03:35 +00:00
if(imagePlugin.Info.SectorSize < F2FS_MIN_SECTOR ||
imagePlugin.Info.SectorSize > F2FS_MAX_SECTOR)
return false;
2016-09-02 18:10:54 +01:00
2020-02-29 18:03:35 +00:00
uint sbAddr = F2FS_SUPER_OFFSET / imagePlugin.Info.SectorSize;
if(sbAddr == 0)
sbAddr = 1;
2016-09-02 18:10:54 +01:00
uint sbSize = (uint)(Marshal.SizeOf<F2FS_Superblock>() / imagePlugin.Info.SectorSize);
2016-09-02 18:10:54 +01:00
2020-02-29 18:03:35 +00:00
if(Marshal.SizeOf<F2FS_Superblock>() % imagePlugin.Info.SectorSize != 0)
sbSize++;
if(partition.Start + sbAddr >= partition.End)
return false;
2017-07-23 19:58:11 +01:00
2017-07-19 16:37:11 +01:00
byte[] sector = imagePlugin.ReadSectors(partition.Start + sbAddr, sbSize);
2020-02-29 18:03:35 +00:00
if(sector.Length < Marshal.SizeOf<F2FS_Superblock>())
return false;
2016-09-02 18:10:54 +01:00
F2FS_Superblock f2fsSb = Marshal.ByteArrayToStructureLittleEndian<F2FS_Superblock>(sector);
2016-09-02 18:10:54 +01:00
return f2fsSb.magic == F2FS_MAGIC;
2016-09-02 18:10:54 +01:00
}
public void GetInformation(IMediaImage imagePlugin, Partition partition, out string information,
2020-02-29 18:03:35 +00:00
Encoding encoding)
2016-09-02 18:10:54 +01:00
{
2018-06-22 08:08:38 +01:00
Encoding = Encoding.Unicode;
2016-09-02 18:10:54 +01:00
information = "";
2020-02-29 18:03:35 +00:00
if(imagePlugin.Info.SectorSize < F2FS_MIN_SECTOR ||
imagePlugin.Info.SectorSize > F2FS_MAX_SECTOR)
return;
uint sbAddr = F2FS_SUPER_OFFSET / imagePlugin.Info.SectorSize;
if(sbAddr == 0)
sbAddr = 1;
2016-09-02 18:10:54 +01:00
uint sbSize = (uint)(Marshal.SizeOf<F2FS_Superblock>() / imagePlugin.Info.SectorSize);
2020-02-29 18:03:35 +00:00
if(Marshal.SizeOf<F2FS_Superblock>() % imagePlugin.Info.SectorSize != 0)
sbSize++;
2016-09-02 18:10:54 +01:00
2017-07-19 16:37:11 +01:00
byte[] sector = imagePlugin.ReadSectors(partition.Start + sbAddr, sbSize);
2020-02-29 18:03:35 +00:00
if(sector.Length < Marshal.SizeOf<F2FS_Superblock>())
return;
2016-09-02 18:10:54 +01:00
F2FS_Superblock f2fsSb = Marshal.ByteArrayToStructureLittleEndian<F2FS_Superblock>(sector);
2016-09-02 18:10:54 +01:00
2020-02-29 18:03:35 +00:00
if(f2fsSb.magic != F2FS_MAGIC)
return;
2016-09-02 18:10:54 +01:00
2020-02-29 18:03:35 +00:00
var sb = new StringBuilder();
2016-09-02 18:10:54 +01:00
sb.AppendLine("F2FS filesystem");
sb.AppendFormat("Version {0}.{1}", f2fsSb.major_ver, f2fsSb.minor_ver).AppendLine();
sb.AppendFormat("{0} bytes per sector", 1 << (int)f2fsSb.log_sectorsize).AppendLine();
2020-02-29 18:03:35 +00:00
2017-12-19 20:33:03 +00:00
sb.AppendFormat("{0} sectors ({1} bytes) per block", 1 << (int)f2fsSb.log_sectors_per_block,
2018-06-22 08:08:38 +01:00
1 << (int)f2fsSb.log_blocksize).AppendLine();
2020-02-29 18:03:35 +00:00
2016-09-02 18:10:54 +01:00
sb.AppendFormat("{0} blocks per segment", f2fsSb.log_blocks_per_seg).AppendLine();
sb.AppendFormat("{0} blocks in volume", f2fsSb.block_count).AppendLine();
sb.AppendFormat("{0} segments per section", f2fsSb.segs_per_sec).AppendLine();
sb.AppendFormat("{0} sections per zone", f2fsSb.secs_per_zone).AppendLine();
sb.AppendFormat("{0} sections", f2fsSb.section_count).AppendLine();
sb.AppendFormat("{0} segments", f2fsSb.segment_count).AppendLine();
sb.AppendFormat("Root directory resides on inode {0}", f2fsSb.root_ino).AppendLine();
sb.AppendFormat("Volume UUID: {0}", f2fsSb.uuid).AppendLine();
2020-02-29 18:03:35 +00:00
sb.AppendFormat("Volume name: {0}", StringHandlers.CToString(f2fsSb.volume_name, Encoding.Unicode, true)).
AppendLine();
sb.AppendFormat("Volume last mounted on kernel version: {0}", StringHandlers.CToString(f2fsSb.version)).
AppendLine();
sb.AppendFormat("Volume created on kernel version: {0}", StringHandlers.CToString(f2fsSb.init_version)).
AppendLine();
2016-09-02 18:10:54 +01:00
information = sb.ToString();
2017-12-26 08:01:40 +00:00
XmlFsType = new FileSystemType
{
2020-02-29 18:03:35 +00:00
Type = "F2FS filesystem", SystemIdentifier = Encoding.ASCII.GetString(f2fsSb.version),
Clusters = f2fsSb.block_count, ClusterSize = (uint)(1 << (int)f2fsSb.log_blocksize),
DataPreparerIdentifier = Encoding.ASCII.GetString(f2fsSb.init_version),
2018-06-22 08:08:38 +01:00
VolumeName = StringHandlers.CToString(f2fsSb.volume_name, Encoding.Unicode, true),
VolumeSerial = f2fsSb.uuid.ToString()
};
2016-09-02 18:10:54 +01:00
}
2020-02-29 18:03:35 +00:00
[StructLayout(LayoutKind.Sequential, Pack = 1), SuppressMessage("ReSharper", "InconsistentNaming")]
struct F2FS_Superblock
{
2019-04-23 01:38:33 +01:00
public readonly uint magic;
public readonly ushort major_ver;
public readonly ushort minor_ver;
public readonly uint log_sectorsize;
public readonly uint log_sectors_per_block;
public readonly uint log_blocksize;
public readonly uint log_blocks_per_seg;
public readonly uint segs_per_sec;
public readonly uint secs_per_zone;
public readonly uint checksum_offset;
public readonly ulong block_count;
public readonly uint section_count;
public readonly uint segment_count;
public readonly uint segment_count_ckpt;
public readonly uint segment_count_sit;
public readonly uint segment_count_nat;
public readonly uint segment_count_ssa;
public readonly uint segment_count_main;
public readonly uint segment0_blkaddr;
public readonly uint cp_blkaddr;
public readonly uint sit_blkaddr;
public readonly uint nat_blkaddr;
public readonly uint ssa_blkaddr;
public readonly uint main_blkaddr;
public readonly uint root_ino;
public readonly uint node_ino;
public readonly uint meta_ino;
public readonly Guid uuid;
2018-06-22 08:08:38 +01:00
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 1024)]
2019-04-23 01:38:33 +01:00
public readonly byte[] volume_name;
public readonly uint extension_count;
2018-06-22 08:08:38 +01:00
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 64)]
2019-04-23 01:38:33 +01:00
public readonly byte[] extension_list1;
2018-06-22 08:08:38 +01:00
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 64)]
2019-04-23 01:38:33 +01:00
public readonly byte[] extension_list2;
2018-06-22 08:08:38 +01:00
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 64)]
2019-04-23 01:38:33 +01:00
public readonly byte[] extension_list3;
2018-06-22 08:08:38 +01:00
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 64)]
2019-04-23 01:38:33 +01:00
public readonly byte[] extension_list4;
2018-06-22 08:08:38 +01:00
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 64)]
2019-04-23 01:38:33 +01:00
public readonly byte[] extension_list5;
2018-06-22 08:08:38 +01:00
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 64)]
2019-04-23 01:38:33 +01:00
public readonly byte[] extension_list6;
2018-06-22 08:08:38 +01:00
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 64)]
2019-04-23 01:38:33 +01:00
public readonly byte[] extension_list7;
2018-06-22 08:08:38 +01:00
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 64)]
2019-04-23 01:38:33 +01:00
public readonly byte[] extension_list8;
public readonly uint cp_payload;
2018-06-22 08:08:38 +01:00
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 256)]
2019-04-23 01:38:33 +01:00
public readonly byte[] version;
2018-06-22 08:08:38 +01:00
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 256)]
2019-04-23 01:38:33 +01:00
public readonly byte[] init_version;
public readonly uint feature;
public readonly byte encryption_level;
2018-06-22 08:08:38 +01:00
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 16)]
2019-04-23 01:38:33 +01:00
public readonly byte[] encrypt_pw_salt;
2018-06-22 08:08:38 +01:00
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 871)]
2019-04-23 01:38:33 +01:00
public readonly byte[] reserved;
}
2016-09-02 18:10:54 +01:00
}
}