2017-07-19 16:31:08 +01:00
|
|
|
|
// /***************************************************************************
|
2020-02-27 12:31:25 +00:00
|
|
|
|
// Aaru Data Preservation Suite
|
2016-08-18 01:13:52 +01:00
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
|
//
|
|
|
|
|
|
// Filename : exFAT.cs
|
|
|
|
|
|
// Author(s) : Natalia Portillo <claunia@claunia.com>
|
|
|
|
|
|
//
|
|
|
|
|
|
// Component : Microsoft exFAT filesystem plugin.
|
|
|
|
|
|
//
|
|
|
|
|
|
// --[ Description ] ----------------------------------------------------------
|
|
|
|
|
|
//
|
2017-12-19 03:50:57 +00:00
|
|
|
|
// Identifies the Microsoft exFAT filesystem and shows information.
|
2016-08-18 01:13:52 +01:00
|
|
|
|
//
|
|
|
|
|
|
// --[ 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-08-18 01:13:52 +01:00
|
|
|
|
// ****************************************************************************/
|
|
|
|
|
|
|
|
|
|
|
|
using System;
|
2020-07-20 07:47:12 +01:00
|
|
|
|
using System.Diagnostics.CodeAnalysis;
|
2016-08-18 01:13:52 +01:00
|
|
|
|
using System.Linq;
|
|
|
|
|
|
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-08-18 01:13:52 +01:00
|
|
|
|
|
2020-02-27 00:33:26 +00:00
|
|
|
|
namespace Aaru.Filesystems
|
2016-08-18 01:13:52 +01:00
|
|
|
|
{
|
|
|
|
|
|
// Information from https://www.sans.org/reading-room/whitepapers/forensics/reverse-engineering-microsoft-exfat-file-system-33274
|
2020-07-20 07:47:12 +01:00
|
|
|
|
[SuppressMessage("ReSharper", "UnusedMember.Local")]
|
2020-07-22 13:20:25 +01:00
|
|
|
|
public sealed class exFAT : IFilesystem
|
2016-08-18 01:13:52 +01:00
|
|
|
|
{
|
2020-07-20 21:11:32 +01:00
|
|
|
|
readonly Guid _oemFlashParameterGuid = new Guid("0A0C7E46-3399-4021-90C8-FA6D389C4BA2");
|
2017-12-24 02:37:41 +00:00
|
|
|
|
|
2020-07-20 21:11:32 +01:00
|
|
|
|
readonly byte[] _signature =
|
2020-02-29 18:03:35 +00:00
|
|
|
|
{
|
|
|
|
|
|
0x45, 0x58, 0x46, 0x41, 0x54, 0x20, 0x20, 0x20
|
|
|
|
|
|
};
|
2017-12-24 02:37:41 +00: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 => "Microsoft Extended File Allocation Table";
|
|
|
|
|
|
public Guid Id => new Guid("8271D088-1533-4CB3-AC28-D802B68BB95C");
|
2018-08-29 22:15:43 +01:00
|
|
|
|
public string Author => "Natalia Portillo";
|
2016-08-18 01:13:52 +01:00
|
|
|
|
|
2017-12-26 07:28:40 +00:00
|
|
|
|
public bool Identify(IMediaImage imagePlugin, Partition partition)
|
2016-08-18 01:13:52 +01:00
|
|
|
|
{
|
2020-02-29 18:03:35 +00:00
|
|
|
|
if(12 + partition.Start >= partition.End)
|
|
|
|
|
|
return false;
|
2016-08-18 01:13:52 +01:00
|
|
|
|
|
2017-07-19 16:37:11 +01:00
|
|
|
|
byte[] vbrSector = imagePlugin.ReadSector(0 + partition.Start);
|
2020-02-29 18:03:35 +00:00
|
|
|
|
|
|
|
|
|
|
if(vbrSector.Length < 512)
|
|
|
|
|
|
return false;
|
2016-08-18 01:13:52 +01:00
|
|
|
|
|
2019-03-01 07:35:22 +00:00
|
|
|
|
VolumeBootRecord vbr = Marshal.ByteArrayToStructureLittleEndian<VolumeBootRecord>(vbrSector);
|
2016-08-18 01:13:52 +01:00
|
|
|
|
|
2020-07-20 21:11:32 +01:00
|
|
|
|
return _signature.SequenceEqual(vbr.signature);
|
2016-08-18 01:13:52 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2017-12-26 07:28:40 +00:00
|
|
|
|
public void GetInformation(IMediaImage imagePlugin, Partition partition, out string information,
|
2020-02-29 18:03:35 +00:00
|
|
|
|
Encoding encoding)
|
2016-08-18 01:13:52 +01:00
|
|
|
|
{
|
2018-06-22 08:08:38 +01:00
|
|
|
|
Encoding = encoding ?? Encoding.GetEncoding("iso-8859-15");
|
2016-08-18 01:13:52 +01:00
|
|
|
|
information = "";
|
|
|
|
|
|
|
2020-02-29 18:03:35 +00:00
|
|
|
|
var sb = new StringBuilder();
|
2017-12-26 08:01:40 +00:00
|
|
|
|
XmlFsType = new FileSystemType();
|
2016-08-18 01:13:52 +01:00
|
|
|
|
|
2019-03-01 07:35:22 +00:00
|
|
|
|
byte[] vbrSector = imagePlugin.ReadSector(0 + partition.Start);
|
|
|
|
|
|
VolumeBootRecord vbr = Marshal.ByteArrayToStructureLittleEndian<VolumeBootRecord>(vbrSector);
|
2016-08-18 01:13:52 +01:00
|
|
|
|
|
2017-07-19 16:37:11 +01:00
|
|
|
|
byte[] parametersSector = imagePlugin.ReadSector(9 + partition.Start);
|
2020-02-29 18:03:35 +00:00
|
|
|
|
|
2017-12-24 02:37:41 +00:00
|
|
|
|
OemParameterTable parametersTable =
|
2019-03-01 07:35:22 +00:00
|
|
|
|
Marshal.ByteArrayToStructureLittleEndian<OemParameterTable>(parametersSector);
|
2016-08-18 01:13:52 +01:00
|
|
|
|
|
2019-03-01 07:35:22 +00:00
|
|
|
|
byte[] chkSector = imagePlugin.ReadSector(11 + partition.Start);
|
|
|
|
|
|
ChecksumSector chksector = Marshal.ByteArrayToStructureLittleEndian<ChecksumSector>(chkSector);
|
2016-08-18 01:13:52 +01:00
|
|
|
|
|
|
|
|
|
|
sb.AppendLine("Microsoft exFAT");
|
|
|
|
|
|
sb.AppendFormat("Partition offset: {0}", vbr.offset).AppendLine();
|
2020-02-29 18:03:35 +00:00
|
|
|
|
|
2017-12-19 20:33:03 +00:00
|
|
|
|
sb.AppendFormat("Volume has {0} sectors of {1} bytes each for a total of {2} bytes", vbr.sectors,
|
|
|
|
|
|
1 << vbr.sectorShift, vbr.sectors * (ulong)(1 << vbr.sectorShift)).AppendLine();
|
2020-02-29 18:03:35 +00:00
|
|
|
|
|
2017-12-19 20:33:03 +00:00
|
|
|
|
sb.AppendFormat("Volume uses clusters of {0} sectors ({1} bytes) each", 1 << vbr.clusterShift,
|
|
|
|
|
|
(1 << vbr.sectorShift) * (1 << vbr.clusterShift)).AppendLine();
|
2020-02-29 18:03:35 +00:00
|
|
|
|
|
|
|
|
|
|
sb.AppendFormat("First FAT starts at sector {0} and runs for {1} sectors", vbr.fatOffset, vbr.fatLength).
|
|
|
|
|
|
AppendLine();
|
|
|
|
|
|
|
2016-08-18 01:13:52 +01:00
|
|
|
|
sb.AppendFormat("Volume uses {0} FATs", vbr.fats).AppendLine();
|
2020-02-29 18:03:35 +00:00
|
|
|
|
|
2017-12-19 20:33:03 +00:00
|
|
|
|
sb.AppendFormat("Cluster heap starts at sector {0}, contains {1} clusters and is {2}% used",
|
|
|
|
|
|
vbr.clusterHeapOffset, vbr.clusterHeapLength, vbr.heapUsage).AppendLine();
|
2020-02-29 18:03:35 +00:00
|
|
|
|
|
2016-08-18 01:13:52 +01:00
|
|
|
|
sb.AppendFormat("Root directory starts at cluster {0}", vbr.rootDirectoryCluster).AppendLine();
|
2020-02-29 18:03:35 +00:00
|
|
|
|
|
|
|
|
|
|
sb.AppendFormat("Filesystem revision is {0}.{1:D2}", (vbr.revision & 0xFF00) >> 8, vbr.revision & 0xFF).
|
|
|
|
|
|
AppendLine();
|
|
|
|
|
|
|
2016-08-18 01:13:52 +01:00
|
|
|
|
sb.AppendFormat("Volume serial number: {0:X8}", vbr.volumeSerial).AppendLine();
|
|
|
|
|
|
sb.AppendFormat("BIOS drive is {0:X2}h", vbr.drive).AppendLine();
|
2020-02-29 18:03:35 +00:00
|
|
|
|
|
|
|
|
|
|
if(vbr.flags.HasFlag(VolumeFlags.SecondFatActive))
|
|
|
|
|
|
sb.AppendLine("2nd FAT is in use");
|
|
|
|
|
|
|
|
|
|
|
|
if(vbr.flags.HasFlag(VolumeFlags.VolumeDirty))
|
|
|
|
|
|
sb.AppendLine("Volume is dirty");
|
|
|
|
|
|
|
|
|
|
|
|
if(vbr.flags.HasFlag(VolumeFlags.MediaFailure))
|
|
|
|
|
|
sb.AppendLine("Underlying media presented errors");
|
2016-08-18 01:13:52 +01:00
|
|
|
|
|
|
|
|
|
|
int count = 1;
|
2020-02-29 18:03:35 +00:00
|
|
|
|
|
2016-08-18 01:13:52 +01:00
|
|
|
|
foreach(OemParameter parameter in parametersTable.parameters)
|
|
|
|
|
|
{
|
2020-07-20 21:11:32 +01:00
|
|
|
|
if(parameter.OemParameterType == _oemFlashParameterGuid)
|
2016-08-18 01:13:52 +01:00
|
|
|
|
{
|
|
|
|
|
|
sb.AppendFormat("OEM Parameters {0}:", count).AppendLine();
|
|
|
|
|
|
sb.AppendFormat("\t{0} bytes in erase block", parameter.eraseBlockSize).AppendLine();
|
|
|
|
|
|
sb.AppendFormat("\t{0} bytes per page", parameter.pageSize).AppendLine();
|
|
|
|
|
|
sb.AppendFormat("\t{0} spare blocks", parameter.spareBlocks).AppendLine();
|
|
|
|
|
|
sb.AppendFormat("\t{0} nanoseconds random access time", parameter.randomAccessTime).AppendLine();
|
|
|
|
|
|
sb.AppendFormat("\t{0} nanoseconds program time", parameter.programTime).AppendLine();
|
|
|
|
|
|
sb.AppendFormat("\t{0} nanoseconds read cycle time", parameter.readCycleTime).AppendLine();
|
|
|
|
|
|
sb.AppendFormat("\t{0} nanoseconds write cycle time", parameter.writeCycleTime).AppendLine();
|
|
|
|
|
|
}
|
|
|
|
|
|
else if(parameter.OemParameterType != Guid.Empty)
|
|
|
|
|
|
sb.AppendFormat("Found unknown parameter type {0}", parameter.OemParameterType).AppendLine();
|
2018-06-22 08:08:38 +01:00
|
|
|
|
|
2016-08-18 01:13:52 +01:00
|
|
|
|
count++;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
sb.AppendFormat("Checksum 0x{0:X8}", chksector.checksum[0]).AppendLine();
|
|
|
|
|
|
|
2019-04-23 01:38:33 +01:00
|
|
|
|
XmlFsType.ClusterSize = (uint)((1 << vbr.sectorShift) * (1 << vbr.clusterShift));
|
2018-06-22 08:08:38 +01:00
|
|
|
|
XmlFsType.Clusters = vbr.clusterHeapLength;
|
|
|
|
|
|
XmlFsType.Dirty = vbr.flags.HasFlag(VolumeFlags.VolumeDirty);
|
|
|
|
|
|
XmlFsType.Type = "exFAT";
|
2017-12-26 08:01:40 +00:00
|
|
|
|
XmlFsType.VolumeSerial = $"{vbr.volumeSerial:X8}";
|
2016-08-18 01:13:52 +01:00
|
|
|
|
|
|
|
|
|
|
information = sb.ToString();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
[Flags]
|
|
|
|
|
|
enum VolumeFlags : ushort
|
|
|
|
|
|
{
|
2020-02-29 18:03:35 +00:00
|
|
|
|
SecondFatActive = 1, VolumeDirty = 2, MediaFailure = 4,
|
2018-06-22 08:08:38 +01:00
|
|
|
|
ClearToZero = 8
|
2016-08-18 01:13:52 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
[StructLayout(LayoutKind.Sequential, Pack = 1)]
|
|
|
|
|
|
struct VolumeBootRecord
|
|
|
|
|
|
{
|
2018-06-22 08:08:38 +01:00
|
|
|
|
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 3)]
|
2019-04-23 01:38:33 +01:00
|
|
|
|
public readonly byte[] jump;
|
2018-06-22 08:08:38 +01:00
|
|
|
|
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 8)]
|
2019-04-23 01:38:33 +01:00
|
|
|
|
public readonly byte[] signature;
|
2018-06-22 08:08:38 +01:00
|
|
|
|
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 53)]
|
2019-04-23 01:38:33 +01:00
|
|
|
|
public readonly byte[] zero;
|
|
|
|
|
|
public readonly ulong offset;
|
|
|
|
|
|
public readonly ulong sectors;
|
|
|
|
|
|
public readonly uint fatOffset;
|
|
|
|
|
|
public readonly uint fatLength;
|
|
|
|
|
|
public readonly uint clusterHeapOffset;
|
|
|
|
|
|
public readonly uint clusterHeapLength;
|
|
|
|
|
|
public readonly uint rootDirectoryCluster;
|
|
|
|
|
|
public readonly uint volumeSerial;
|
|
|
|
|
|
public readonly ushort revision;
|
|
|
|
|
|
public readonly VolumeFlags flags;
|
|
|
|
|
|
public readonly byte sectorShift;
|
|
|
|
|
|
public readonly byte clusterShift;
|
|
|
|
|
|
public readonly byte fats;
|
|
|
|
|
|
public readonly byte drive;
|
|
|
|
|
|
public readonly byte heapUsage;
|
2018-06-22 08:08:38 +01:00
|
|
|
|
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 53)]
|
2019-04-23 01:38:33 +01:00
|
|
|
|
public readonly byte[] reserved;
|
2018-06-22 08:08:38 +01:00
|
|
|
|
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 53)]
|
2019-04-23 01:38:33 +01:00
|
|
|
|
public readonly byte[] bootCode;
|
|
|
|
|
|
public readonly ushort bootSignature;
|
2016-08-18 01:13:52 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
[StructLayout(LayoutKind.Sequential, Pack = 1)]
|
|
|
|
|
|
struct OemParameter
|
|
|
|
|
|
{
|
2019-04-23 01:38:33 +01:00
|
|
|
|
public readonly Guid OemParameterType;
|
|
|
|
|
|
public readonly uint eraseBlockSize;
|
|
|
|
|
|
public readonly uint pageSize;
|
|
|
|
|
|
public readonly uint spareBlocks;
|
|
|
|
|
|
public readonly uint randomAccessTime;
|
|
|
|
|
|
public readonly uint programTime;
|
|
|
|
|
|
public readonly uint readCycleTime;
|
|
|
|
|
|
public readonly uint writeCycleTime;
|
2018-06-22 08:08:38 +01:00
|
|
|
|
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 4)]
|
2019-04-23 01:38:33 +01:00
|
|
|
|
public readonly byte[] reserved;
|
2016-08-18 01:13:52 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
[StructLayout(LayoutKind.Sequential, Pack = 1)]
|
|
|
|
|
|
struct OemParameterTable
|
|
|
|
|
|
{
|
2018-06-22 08:08:38 +01:00
|
|
|
|
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 10)]
|
2019-04-23 01:38:33 +01:00
|
|
|
|
public readonly OemParameter[] parameters;
|
2018-06-22 08:08:38 +01:00
|
|
|
|
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 32)]
|
2019-04-23 01:38:33 +01:00
|
|
|
|
public readonly byte[] padding;
|
2016-08-18 01:13:52 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
[StructLayout(LayoutKind.Sequential, Pack = 1)]
|
|
|
|
|
|
struct ChecksumSector
|
|
|
|
|
|
{
|
2018-06-22 08:08:38 +01:00
|
|
|
|
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 128)]
|
2019-04-23 01:38:33 +01:00
|
|
|
|
public readonly uint[] checksum;
|
2016-08-18 01:13:52 +01:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2017-12-19 20:33:03 +00:00
|
|
|
|
}
|