2017-07-19 16:31:08 +01:00
|
|
|
|
// /***************************************************************************
|
2016-06-22 05:01:13 +01:00
|
|
|
|
// The Disc Image Chef
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
|
//
|
|
|
|
|
|
// Filename : APFS.cs
|
2016-07-28 18:13:49 +01:00
|
|
|
|
// Author(s) : Natalia Portillo <claunia@claunia.com>
|
2016-06-22 05:01:13 +01:00
|
|
|
|
//
|
2016-07-28 18:13:49 +01:00
|
|
|
|
// Component : Apple filesystem plugin.
|
2016-06-22 05:01:13 +01:00
|
|
|
|
//
|
|
|
|
|
|
// --[ Description ] ----------------------------------------------------------
|
|
|
|
|
|
//
|
2016-07-28 18:13:49 +01:00
|
|
|
|
// Identifies the Apple filesystem and shows information.
|
2016-06-22 05:01:13 +01:00
|
|
|
|
//
|
|
|
|
|
|
// --[ License ] --------------------------------------------------------------
|
|
|
|
|
|
//
|
2016-07-28 18:13:49 +01:00
|
|
|
|
// 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
|
2016-06-22 05:01:13 +01:00
|
|
|
|
// License, or (at your option) any later version.
|
|
|
|
|
|
//
|
2016-07-28 18:13:49 +01:00
|
|
|
|
// 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.
|
2016-06-22 05:01:13 +01:00
|
|
|
|
//
|
2016-07-28 18:13:49 +01:00
|
|
|
|
// 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/>.
|
2016-06-22 05:01:13 +01:00
|
|
|
|
//
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
2018-12-29 17:34:38 +00:00
|
|
|
|
// Copyright © 2011-2019 Natalia Portillo
|
2016-06-22 05:01:13 +01:00
|
|
|
|
// ****************************************************************************/
|
2016-07-28 18:13:49 +01:00
|
|
|
|
|
2016-06-22 05:01:13 +01:00
|
|
|
|
using System;
|
|
|
|
|
|
using System.Runtime.InteropServices;
|
|
|
|
|
|
using System.Text;
|
2017-07-19 16:31:08 +01:00
|
|
|
|
using DiscImageChef.CommonTypes;
|
2018-06-25 19:08:16 +01:00
|
|
|
|
using DiscImageChef.CommonTypes.Interfaces;
|
2017-12-21 14:30:38 +00:00
|
|
|
|
using Schemas;
|
2016-06-22 05:01:13 +01:00
|
|
|
|
|
2016-07-21 16:15:39 +01:00
|
|
|
|
namespace DiscImageChef.Filesystems
|
2016-06-22 05:01:13 +01:00
|
|
|
|
{
|
2017-12-26 06:05:12 +00:00
|
|
|
|
public class APFS : IFilesystem
|
2016-06-22 05:01:13 +01:00
|
|
|
|
{
|
2017-12-22 08:43:22 +00:00
|
|
|
|
const uint APFS_CONTAINER_MAGIC = 0x4253584E; // "NXSB"
|
2018-06-22 08:08:38 +01:00
|
|
|
|
const uint APFS_VOLUME_MAGIC = 0x42535041; // "APSB"
|
2016-06-22 05:01:13 +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 => "Apple File System";
|
|
|
|
|
|
public Guid Id => new Guid("A4060F9D-2909-42E2-9D95-DB31FA7EA797");
|
2018-08-29 22:15:43 +01:00
|
|
|
|
public string Author => "Natalia Portillo";
|
2016-07-27 13:32:45 +01:00
|
|
|
|
|
2017-12-26 07:28:40 +00:00
|
|
|
|
public bool Identify(IMediaImage imagePlugin, Partition partition)
|
2016-06-22 05:01:13 +01:00
|
|
|
|
{
|
2017-12-19 20:33:03 +00:00
|
|
|
|
if(partition.Start >= partition.End) return false;
|
2016-06-22 05:01:13 +01:00
|
|
|
|
|
2018-06-22 08:08:38 +01:00
|
|
|
|
byte[] sector = imagePlugin.ReadSector(partition.Start);
|
2016-06-22 05:01:13 +01:00
|
|
|
|
ApfsContainerSuperBlock nxSb;
|
|
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
|
|
|
|
|
GCHandle handle = GCHandle.Alloc(sector, GCHandleType.Pinned);
|
2017-12-19 20:33:03 +00:00
|
|
|
|
nxSb = (ApfsContainerSuperBlock)Marshal.PtrToStructure(handle.AddrOfPinnedObject(),
|
|
|
|
|
|
typeof(ApfsContainerSuperBlock));
|
2016-06-22 05:01:13 +01:00
|
|
|
|
handle.Free();
|
|
|
|
|
|
}
|
2017-12-19 20:33:03 +00:00
|
|
|
|
catch { return false; }
|
2016-06-22 05:01:13 +01:00
|
|
|
|
|
2017-12-22 08:43:22 +00:00
|
|
|
|
return nxSb.magic == APFS_CONTAINER_MAGIC;
|
2016-06-22 05:01:13 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2017-12-26 07:28:40 +00:00
|
|
|
|
public void GetInformation(IMediaImage imagePlugin, Partition partition, out string information,
|
2018-06-22 08:08:38 +01:00
|
|
|
|
Encoding encoding)
|
2016-06-22 05:01:13 +01:00
|
|
|
|
{
|
2017-12-26 08:01:40 +00:00
|
|
|
|
Encoding = Encoding.UTF8;
|
2016-06-22 05:01:13 +01:00
|
|
|
|
StringBuilder sbInformation = new StringBuilder();
|
2018-06-22 08:08:38 +01:00
|
|
|
|
XmlFsType = new FileSystemType();
|
2016-06-22 05:01:13 +01:00
|
|
|
|
information = "";
|
|
|
|
|
|
|
2017-12-19 20:33:03 +00:00
|
|
|
|
if(partition.Start >= partition.End) return;
|
2016-06-22 05:01:13 +01:00
|
|
|
|
|
2018-06-22 08:08:38 +01:00
|
|
|
|
byte[] sector = imagePlugin.ReadSector(partition.Start);
|
2016-06-22 05:01:13 +01:00
|
|
|
|
ApfsContainerSuperBlock nxSb;
|
|
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
|
|
|
|
|
GCHandle handle = GCHandle.Alloc(sector, GCHandleType.Pinned);
|
2017-12-19 20:33:03 +00:00
|
|
|
|
nxSb = (ApfsContainerSuperBlock)Marshal.PtrToStructure(handle.AddrOfPinnedObject(),
|
|
|
|
|
|
typeof(ApfsContainerSuperBlock));
|
2016-06-22 05:01:13 +01:00
|
|
|
|
handle.Free();
|
|
|
|
|
|
}
|
2017-12-19 20:33:03 +00:00
|
|
|
|
catch { return; }
|
2016-06-22 05:01:13 +01:00
|
|
|
|
|
2017-12-22 08:43:22 +00:00
|
|
|
|
if(nxSb.magic != APFS_CONTAINER_MAGIC) return;
|
2016-06-22 05:01:13 +01:00
|
|
|
|
|
|
|
|
|
|
sbInformation.AppendLine("Apple File System");
|
|
|
|
|
|
sbInformation.AppendLine();
|
|
|
|
|
|
sbInformation.AppendFormat("{0} bytes per block", nxSb.blockSize).AppendLine();
|
2017-12-19 20:33:03 +00:00
|
|
|
|
sbInformation.AppendFormat("Container has {0} bytes in {1} blocks", nxSb.containerBlocks * nxSb.blockSize,
|
|
|
|
|
|
nxSb.containerBlocks).AppendLine();
|
2016-06-22 05:01:13 +01:00
|
|
|
|
|
|
|
|
|
|
information = sbInformation.ToString();
|
|
|
|
|
|
|
2017-12-26 08:01:40 +00:00
|
|
|
|
XmlFsType = new FileSystemType
|
2017-12-22 08:43:22 +00:00
|
|
|
|
{
|
2018-06-22 08:08:38 +01:00
|
|
|
|
Bootable = false,
|
|
|
|
|
|
Clusters = (long)nxSb.containerBlocks,
|
2017-12-22 08:43:22 +00:00
|
|
|
|
ClusterSize = (int)nxSb.blockSize,
|
2018-06-22 08:08:38 +01:00
|
|
|
|
Type = "Apple File System"
|
2017-12-22 08:43:22 +00:00
|
|
|
|
};
|
2016-06-22 05:01:13 +01:00
|
|
|
|
}
|
2016-07-21 17:16:08 +01:00
|
|
|
|
|
2017-12-24 02:37:41 +00:00
|
|
|
|
[StructLayout(LayoutKind.Sequential, Pack = 1)]
|
|
|
|
|
|
struct ApfsContainerSuperBlock
|
|
|
|
|
|
{
|
|
|
|
|
|
public ulong unknown1; // Varies between copies of the superblock
|
|
|
|
|
|
public ulong unknown2;
|
|
|
|
|
|
public ulong unknown3; // Varies by 1 between copies of the superblock
|
|
|
|
|
|
public ulong unknown4;
|
2018-06-22 08:08:38 +01:00
|
|
|
|
public uint magic;
|
|
|
|
|
|
public uint blockSize;
|
2017-12-24 02:37:41 +00:00
|
|
|
|
public ulong containerBlocks;
|
|
|
|
|
|
}
|
2016-06-22 05:01:13 +01:00
|
|
|
|
}
|
2017-12-19 20:33:03 +00:00
|
|
|
|
}
|