Files
Aaru/Aaru.Filesystems/FATX/Super.cs

289 lines
12 KiB
C#
Raw Normal View History

2019-04-06 11:03:43 +01:00
// /***************************************************************************
2020-02-27 12:31:25 +00:00
// Aaru Data Preservation Suite
2019-04-06 11:03:43 +01:00
// ----------------------------------------------------------------------------
//
// Filename : Super.cs
// Author(s) : Natalia Portillo <claunia@claunia.com>
//
// Component : FATX filesystem plugin.
//
// --[ Description ] ----------------------------------------------------------
//
// Handles mounting and umounting the FATX filesystem.
//
// --[ 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
2019-04-06 11:03:43 +01:00
// ****************************************************************************/
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Runtime.InteropServices;
2019-04-06 11:03:43 +01:00
using System.Text;
2020-02-27 00:33:26 +00:00
using Aaru.CommonTypes;
using Aaru.CommonTypes.Interfaces;
using Aaru.CommonTypes.Structs;
using Aaru.Console;
using Schemas;
2020-02-27 00:33:26 +00:00
using Marshal = Aaru.Helpers.Marshal;
2019-04-06 11:03:43 +01:00
2020-02-27 00:33:26 +00:00
namespace Aaru.Filesystems.FATX
2019-04-06 11:03:43 +01:00
{
public partial class XboxFatPlugin
{
2020-02-29 18:03:35 +00:00
public Errno Mount(IMediaImage imagePlugin, Partition partition, Encoding encoding,
Dictionary<string, string> options, string @namespace)
{
Encoding = Encoding.GetEncoding("iso-8859-15");
littleEndian = true;
2020-02-29 18:03:35 +00:00
if(options == null)
options = GetDefaultOptions();
if(options.TryGetValue("debug", out string debugString))
bool.TryParse(debugString, out debug);
if(imagePlugin.Info.SectorSize < 512)
return Errno.InvalidArgument;
2020-02-27 23:48:41 +00:00
AaruConsole.DebugWriteLine("Xbox FAT plugin", "Reading superblock");
byte[] sector = imagePlugin.ReadSector(partition.Start);
superblock = Marshal.ByteArrayToStructureLittleEndian<Superblock>(sector);
if(superblock.magic == FATX_CIGAM)
{
superblock = Marshal.ByteArrayToStructureBigEndian<Superblock>(sector);
littleEndian = false;
}
2020-02-29 18:03:35 +00:00
if(superblock.magic != FATX_MAGIC)
return Errno.InvalidArgument;
2020-02-27 23:48:41 +00:00
AaruConsole.DebugWriteLine("Xbox FAT plugin",
2020-02-29 18:03:35 +00:00
littleEndian ? "Filesystem is little endian" : "Filesystem is big endian");
int logicalSectorsPerPhysicalSectors = partition.Offset == 0 && littleEndian ? 8 : 1;
2020-02-29 18:03:35 +00:00
2020-02-27 23:48:41 +00:00
AaruConsole.DebugWriteLine("Xbox FAT plugin", "logicalSectorsPerPhysicalSectors = {0}",
2020-02-29 18:03:35 +00:00
logicalSectorsPerPhysicalSectors);
string volumeLabel = StringHandlers.CToString(superblock.volumeLabel,
!littleEndian ? Encoding.BigEndianUnicode : Encoding.Unicode,
true);
XmlFsType = new FileSystemType
{
Type = "FATX filesystem",
2020-02-29 18:03:35 +00:00
ClusterSize = (uint)(superblock.sectorsPerCluster * logicalSectorsPerPhysicalSectors *
imagePlugin.Info.SectorSize),
2020-07-20 04:34:16 +01:00
VolumeName = volumeLabel,
VolumeSerial = $"{superblock.id:X8}"
};
2020-02-29 18:03:35 +00:00
XmlFsType.Clusters = (((partition.End - partition.Start) + 1) * imagePlugin.Info.SectorSize) /
2019-04-23 01:38:33 +01:00
XmlFsType.ClusterSize;
statfs = new FileSystemInfo
{
2020-07-20 04:34:16 +01:00
Blocks = XmlFsType.Clusters,
FilenameLength = MAX_FILENAME,
Files = 0, // Requires traversing all directories
FreeFiles = 0,
Id =
2020-02-29 18:03:35 +00:00
{
2020-07-20 04:34:16 +01:00
IsInt = true,
Serial32 = superblock.magic
2020-02-29 18:03:35 +00:00
},
2020-07-20 04:34:16 +01:00
PluginId = Id,
Type = littleEndian ? "Xbox FAT" : "Xbox 360 FAT",
2020-02-29 18:03:35 +00:00
FreeBlocks = 0 // Requires traversing the FAT
};
2020-02-29 18:03:35 +00:00
AaruConsole.DebugWriteLine("Xbox FAT plugin", "XmlFsType.ClusterSize: {0}", XmlFsType.ClusterSize);
AaruConsole.DebugWriteLine("Xbox FAT plugin", "XmlFsType.VolumeName: {0}", XmlFsType.VolumeName);
2020-02-27 23:48:41 +00:00
AaruConsole.DebugWriteLine("Xbox FAT plugin", "XmlFsType.VolumeSerial: {0}", XmlFsType.VolumeSerial);
2020-02-29 18:03:35 +00:00
AaruConsole.DebugWriteLine("Xbox FAT plugin", "stat.Blocks: {0}", statfs.Blocks);
AaruConsole.DebugWriteLine("Xbox FAT plugin", "stat.FilenameLength: {0}", statfs.FilenameLength);
AaruConsole.DebugWriteLine("Xbox FAT plugin", "stat.Id: {0}", statfs.Id.Serial32);
AaruConsole.DebugWriteLine("Xbox FAT plugin", "stat.Type: {0}", statfs.Type);
byte[] buffer;
2020-02-29 18:03:35 +00:00
fatStartSector = (FAT_START / imagePlugin.Info.SectorSize) + partition.Start;
uint fatSize;
2020-02-27 23:48:41 +00:00
AaruConsole.DebugWriteLine("Xbox FAT plugin", "fatStartSector: {0}", fatStartSector);
if(statfs.Blocks > MAX_XFAT16_CLUSTERS)
{
2020-02-27 23:48:41 +00:00
AaruConsole.DebugWriteLine("Xbox FAT plugin", "Reading FAT32");
2020-02-29 18:03:35 +00:00
fatSize = (uint)(((statfs.Blocks + 1) * sizeof(uint)) / imagePlugin.Info.SectorSize);
if((uint)(((statfs.Blocks + 1) * sizeof(uint)) % imagePlugin.Info.SectorSize) > 0)
fatSize++;
2020-02-29 18:03:35 +00:00
long fatClusters = (fatSize * imagePlugin.Info.SectorSize) / 4096;
if((fatSize * imagePlugin.Info.SectorSize) % 4096 > 0)
fatClusters++;
fatSize = (uint)((fatClusters * 4096) / imagePlugin.Info.SectorSize);
2020-02-27 23:48:41 +00:00
AaruConsole.DebugWriteLine("Xbox FAT plugin", "FAT is {0} sectors", fatSize);
buffer = imagePlugin.ReadSectors(fatStartSector, fatSize);
2020-02-27 23:48:41 +00:00
AaruConsole.DebugWriteLine("Xbox FAT plugin", "Casting FAT");
fat32 = MemoryMarshal.Cast<byte, uint>(buffer).ToArray();
2020-02-29 18:03:35 +00:00
if(!littleEndian)
for(int i = 0; i < fat32.Length; i++)
fat32[i] = Swapping.Swap(fat32[i]);
2020-02-27 23:48:41 +00:00
AaruConsole.DebugWriteLine("Xbox FAT plugin", "fat32[0] == FATX32_ID = {0}", fat32[0] == FATX32_ID);
2020-02-29 18:03:35 +00:00
if(fat32[0] != FATX32_ID)
return Errno.InvalidArgument;
}
else
{
2020-02-27 23:48:41 +00:00
AaruConsole.DebugWriteLine("Xbox FAT plugin", "Reading FAT16");
2020-02-29 18:03:35 +00:00
fatSize = (uint)(((statfs.Blocks + 1) * sizeof(ushort)) / imagePlugin.Info.SectorSize);
2020-02-29 18:03:35 +00:00
if((uint)(((statfs.Blocks + 1) * sizeof(ushort)) % imagePlugin.Info.SectorSize) > 0)
fatSize++;
long fatClusters = (fatSize * imagePlugin.Info.SectorSize) / 4096;
if((fatSize * imagePlugin.Info.SectorSize) % 4096 > 0)
fatClusters++;
fatSize = (uint)((fatClusters * 4096) / imagePlugin.Info.SectorSize);
2020-02-27 23:48:41 +00:00
AaruConsole.DebugWriteLine("Xbox FAT plugin", "FAT is {0} sectors", fatSize);
buffer = imagePlugin.ReadSectors(fatStartSector, fatSize);
2020-02-27 23:48:41 +00:00
AaruConsole.DebugWriteLine("Xbox FAT plugin", "Casting FAT");
fat16 = MemoryMarshal.Cast<byte, ushort>(buffer).ToArray();
2020-02-29 18:03:35 +00:00
if(!littleEndian)
for(int i = 0; i < fat16.Length; i++)
fat16[i] = Swapping.Swap(fat16[i]);
2020-02-27 23:48:41 +00:00
AaruConsole.DebugWriteLine("Xbox FAT plugin", "fat16[0] == FATX16_ID = {0}", fat16[0] == FATX16_ID);
2020-02-29 18:03:35 +00:00
if(fat16[0] != FATX16_ID)
return Errno.InvalidArgument;
}
sectorsPerCluster = (uint)(superblock.sectorsPerCluster * logicalSectorsPerPhysicalSectors);
this.imagePlugin = imagePlugin;
firstClusterSector = fatStartSector + fatSize;
2019-04-07 13:31:27 +01:00
bytesPerCluster = sectorsPerCluster * imagePlugin.Info.SectorSize;
2020-02-29 18:03:35 +00:00
AaruConsole.DebugWriteLine("Xbox FAT plugin", "sectorsPerCluster = {0}", sectorsPerCluster);
AaruConsole.DebugWriteLine("Xbox FAT plugin", "bytesPerCluster = {0}", bytesPerCluster);
2020-02-27 23:48:41 +00:00
AaruConsole.DebugWriteLine("Xbox FAT plugin", "firstClusterSector = {0}", firstClusterSector);
2019-04-07 13:31:27 +01:00
uint[] rootDirectoryClusters = GetClusters(superblock.rootDirectoryCluster);
2020-02-29 18:03:35 +00:00
if(rootDirectoryClusters is null)
return Errno.InvalidArgument;
2019-04-07 13:31:27 +01:00
byte[] rootDirectoryBuffer = new byte[bytesPerCluster * rootDirectoryClusters.Length];
2020-02-27 23:48:41 +00:00
AaruConsole.DebugWriteLine("Xbox FAT plugin", "Reading root directory");
2020-02-29 18:03:35 +00:00
2019-04-07 13:31:27 +01:00
for(int i = 0; i < rootDirectoryClusters.Length; i++)
{
buffer =
2020-02-29 18:03:35 +00:00
imagePlugin.ReadSectors(firstClusterSector + ((rootDirectoryClusters[i] - 1) * sectorsPerCluster),
2019-04-07 13:31:27 +01:00
sectorsPerCluster);
2020-02-29 18:03:35 +00:00
2019-04-07 13:31:27 +01:00
Array.Copy(buffer, 0, rootDirectoryBuffer, i * bytesPerCluster, bytesPerCluster);
}
rootDirectory = new Dictionary<string, DirectoryEntry>();
int pos = 0;
2020-02-29 18:03:35 +00:00
2019-04-07 13:31:27 +01:00
while(pos < rootDirectoryBuffer.Length)
{
DirectoryEntry entry = littleEndian
2020-02-29 18:03:35 +00:00
? Marshal.
ByteArrayToStructureLittleEndian<DirectoryEntry
2019-04-07 13:31:27 +01:00
>(rootDirectoryBuffer, pos, Marshal.SizeOf<DirectoryEntry>())
: Marshal.ByteArrayToStructureBigEndian<DirectoryEntry>(rootDirectoryBuffer,
pos,
2020-02-29 18:03:35 +00:00
Marshal.
SizeOf<
2019-04-07 13:31:27 +01:00
DirectoryEntry
>());
pos += Marshal.SizeOf<DirectoryEntry>();
2020-02-29 18:03:35 +00:00
if(entry.filenameSize == UNUSED_DIRENTRY ||
entry.filenameSize == FINISHED_DIRENTRY)
break;
2019-04-07 13:31:27 +01:00
2020-02-29 18:03:35 +00:00
if(entry.filenameSize == DELETED_DIRENTRY ||
entry.filenameSize > MAX_FILENAME)
continue;
2019-04-07 13:31:27 +01:00
string filename = Encoding.GetString(entry.filename, 0, entry.filenameSize);
rootDirectory.Add(filename, entry);
}
cultureInfo = new CultureInfo("en-US", false);
directoryCache = new Dictionary<string, Dictionary<string, DirectoryEntry>>();
2019-04-14 11:38:29 +01:00
mounted = true;
2019-04-07 13:31:27 +01:00
return Errno.NoError;
}
public Errno Unmount()
{
2020-02-29 18:03:35 +00:00
if(!mounted)
return Errno.AccessDenied;
fat16 = null;
fat32 = null;
imagePlugin = null;
mounted = false;
return Errno.NoError;
}
2019-04-06 11:03:43 +01:00
public Errno StatFs(out FileSystemInfo stat)
{
stat = null;
2020-02-29 18:03:35 +00:00
if(!mounted)
return Errno.AccessDenied;
stat = statfs.ShallowCopy();
2019-04-06 11:03:43 +01:00
return Errno.NoError;
}
2019-04-06 11:03:43 +01:00
}
}