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

298 lines
11 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/>.
//
// ----------------------------------------------------------------------------
2022-02-18 10:02:53 +00:00
// Copyright © 2011-2022 Natalia Portillo
2019-04-06 11:03:43 +01:00
// ****************************************************************************/
2022-03-07 07:36:44 +00:00
namespace Aaru.Filesystems;
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;
2021-09-16 04:42:14 +01:00
using Aaru.CommonTypes.Enums;
2020-02-27 00:33:26 +00:00
using Aaru.CommonTypes.Interfaces;
using Aaru.CommonTypes.Structs;
using Aaru.Console;
2020-07-20 15:43:52 +01:00
using Aaru.Helpers;
using Schemas;
2020-02-27 00:33:26 +00:00
using Marshal = Aaru.Helpers.Marshal;
2019-04-06 11:03:43 +01:00
2022-03-06 13:29:38 +00:00
public sealed partial class XboxFatPlugin
2019-04-06 11:03:43 +01:00
{
2022-03-06 13:29:38 +00:00
/// <inheritdoc />
public ErrorNumber Mount(IMediaImage imagePlugin, Partition partition, Encoding encoding,
Dictionary<string, string> options, string @namespace)
2019-04-06 11:03:43 +01:00
{
2022-03-06 13:29:38 +00:00
Encoding = Encoding.GetEncoding("iso-8859-15");
_littleEndian = true;
2022-03-06 13:29:38 +00:00
options ??= GetDefaultOptions();
2020-02-29 18:03:35 +00:00
2022-03-06 13:29:38 +00:00
if(options.TryGetValue("debug", out string debugString))
bool.TryParse(debugString, out _debug);
2020-02-29 18:03:35 +00:00
2022-03-06 13:29:38 +00:00
if(imagePlugin.Info.SectorSize < 512)
return ErrorNumber.InvalidArgument;
2022-03-06 13:29:38 +00:00
AaruConsole.DebugWriteLine("Xbox FAT plugin", "Reading superblock");
2022-03-06 13:29:38 +00:00
ErrorNumber errno = imagePlugin.ReadSector(partition.Start, out byte[] sector);
2022-03-06 13:29:38 +00:00
if(errno != ErrorNumber.NoError)
return errno;
2022-03-06 13:29:38 +00:00
_superblock = Marshal.ByteArrayToStructureLittleEndian<Superblock>(sector);
2022-03-06 13:29:38 +00:00
if(_superblock.magic == FATX_CIGAM)
{
_superblock = Marshal.ByteArrayToStructureBigEndian<Superblock>(sector);
_littleEndian = false;
}
2022-03-06 13:29:38 +00:00
if(_superblock.magic != FATX_MAGIC)
return ErrorNumber.InvalidArgument;
2022-03-06 13:29:38 +00:00
AaruConsole.DebugWriteLine("Xbox FAT plugin",
_littleEndian ? "Filesystem is little endian" : "Filesystem is big endian");
2022-03-06 13:29:38 +00:00
int logicalSectorsPerPhysicalSectors = partition.Offset == 0 && _littleEndian ? 8 : 1;
2020-02-29 18:03:35 +00:00
2022-03-06 13:29:38 +00:00
AaruConsole.DebugWriteLine("Xbox FAT plugin", "logicalSectorsPerPhysicalSectors = {0}",
logicalSectorsPerPhysicalSectors);
2022-03-06 13:29:38 +00:00
string volumeLabel = StringHandlers.CToString(_superblock.volumeLabel,
!_littleEndian ? Encoding.BigEndianUnicode : Encoding.Unicode,
true);
2022-03-06 13:29:38 +00:00
XmlFsType = new FileSystemType
{
Type = "FATX filesystem",
ClusterSize = (uint)(_superblock.sectorsPerCluster * logicalSectorsPerPhysicalSectors *
imagePlugin.Info.SectorSize),
VolumeName = volumeLabel,
VolumeSerial = $"{_superblock.id:X8}"
};
2020-02-29 18:03:35 +00:00
2022-03-06 13:29:38 +00:00
XmlFsType.Clusters = (partition.End - partition.Start + 1) * imagePlugin.Info.SectorSize /
XmlFsType.ClusterSize;
2022-03-06 13:29:38 +00:00
_statfs = new FileSystemInfo
{
Blocks = XmlFsType.Clusters,
FilenameLength = MAX_FILENAME,
Files = 0, // Requires traversing all directories
FreeFiles = 0,
Id =
{
2022-03-06 13:29:38 +00:00
IsInt = true,
Serial32 = _superblock.magic
},
PluginId = Id,
Type = _littleEndian ? "Xbox FAT" : "Xbox 360 FAT",
FreeBlocks = 0 // Requires traversing the FAT
};
AaruConsole.DebugWriteLine("Xbox FAT plugin", "XmlFsType.ClusterSize: {0}", XmlFsType.ClusterSize);
AaruConsole.DebugWriteLine("Xbox FAT plugin", "XmlFsType.VolumeName: {0}", XmlFsType.VolumeName);
AaruConsole.DebugWriteLine("Xbox FAT plugin", "XmlFsType.VolumeSerial: {0}", XmlFsType.VolumeSerial);
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;
2022-03-07 07:36:44 +00:00
_fatStartSector = FAT_START / imagePlugin.Info.SectorSize + partition.Start;
2022-03-06 13:29:38 +00:00
uint fatSize;
AaruConsole.DebugWriteLine("Xbox FAT plugin", "fatStartSector: {0}", _fatStartSector);
if(_statfs.Blocks > MAX_XFAT16_CLUSTERS)
{
AaruConsole.DebugWriteLine("Xbox FAT plugin", "Reading FAT32");
2022-03-06 13:29:38 +00:00
fatSize = (uint)((_statfs.Blocks + 1) * sizeof(uint) / imagePlugin.Info.SectorSize);
2020-02-29 18:03:35 +00:00
2022-03-06 13:29:38 +00:00
if((uint)((_statfs.Blocks + 1) * sizeof(uint) % imagePlugin.Info.SectorSize) > 0)
fatSize++;
2022-03-06 13:29:38 +00:00
long fatClusters = fatSize * imagePlugin.Info.SectorSize / 4096;
2020-02-29 18:03:35 +00:00
2022-03-06 13:29:38 +00:00
if(fatSize * imagePlugin.Info.SectorSize % 4096 > 0)
fatClusters++;
2020-02-29 18:03:35 +00:00
2022-03-06 13:29:38 +00:00
fatSize = (uint)(fatClusters * 4096 / imagePlugin.Info.SectorSize);
2022-03-06 13:29:38 +00:00
AaruConsole.DebugWriteLine("Xbox FAT plugin", "FAT is {0} sectors", fatSize);
2022-03-06 13:29:38 +00:00
errno = imagePlugin.ReadSectors(_fatStartSector, fatSize, out buffer);
2022-03-06 13:29:38 +00:00
if(errno != ErrorNumber.NoError)
return errno;
2022-03-06 13:29:38 +00:00
AaruConsole.DebugWriteLine("Xbox FAT plugin", "Casting FAT");
_fat32 = MemoryMarshal.Cast<byte, uint>(buffer).ToArray();
2020-02-29 18:03:35 +00:00
2022-03-06 13:29:38 +00:00
if(!_littleEndian)
2022-03-07 07:36:44 +00:00
for(var i = 0; i < _fat32.Length; i++)
2022-03-06 13:29:38 +00:00
_fat32[i] = Swapping.Swap(_fat32[i]);
2022-03-06 13:29:38 +00:00
AaruConsole.DebugWriteLine("Xbox FAT plugin", "fat32[0] == FATX32_ID = {0}", _fat32[0] == FATX32_ID);
2020-02-29 18:03:35 +00:00
2022-03-06 13:29:38 +00:00
if(_fat32[0] != FATX32_ID)
return ErrorNumber.InvalidArgument;
}
else
{
AaruConsole.DebugWriteLine("Xbox FAT plugin", "Reading FAT16");
2022-03-06 13:29:38 +00:00
fatSize = (uint)((_statfs.Blocks + 1) * sizeof(ushort) / imagePlugin.Info.SectorSize);
2022-03-06 13:29:38 +00:00
if((uint)((_statfs.Blocks + 1) * sizeof(ushort) % imagePlugin.Info.SectorSize) > 0)
fatSize++;
2020-02-29 18:03:35 +00:00
2022-03-06 13:29:38 +00:00
long fatClusters = fatSize * imagePlugin.Info.SectorSize / 4096;
2020-02-29 18:03:35 +00:00
2022-03-06 13:29:38 +00:00
if(fatSize * imagePlugin.Info.SectorSize % 4096 > 0)
fatClusters++;
2020-02-29 18:03:35 +00:00
2022-03-06 13:29:38 +00:00
fatSize = (uint)(fatClusters * 4096 / imagePlugin.Info.SectorSize);
2022-03-06 13:29:38 +00:00
AaruConsole.DebugWriteLine("Xbox FAT plugin", "FAT is {0} sectors", fatSize);
2022-03-06 13:29:38 +00:00
errno = imagePlugin.ReadSectors(_fatStartSector, fatSize, out buffer);
2022-03-06 13:29:38 +00:00
if(errno != ErrorNumber.NoError)
return errno;
2022-03-06 13:29:38 +00:00
AaruConsole.DebugWriteLine("Xbox FAT plugin", "Casting FAT");
_fat16 = MemoryMarshal.Cast<byte, ushort>(buffer).ToArray();
2020-02-29 18:03:35 +00:00
2022-03-06 13:29:38 +00:00
if(!_littleEndian)
2022-03-07 07:36:44 +00:00
for(var i = 0; i < _fat16.Length; i++)
2022-03-06 13:29:38 +00:00
_fat16[i] = Swapping.Swap(_fat16[i]);
2022-03-06 13:29:38 +00:00
AaruConsole.DebugWriteLine("Xbox FAT plugin", "fat16[0] == FATX16_ID = {0}", _fat16[0] == FATX16_ID);
2020-02-29 18:03:35 +00:00
2022-03-06 13:29:38 +00:00
if(_fat16[0] != FATX16_ID)
return ErrorNumber.InvalidArgument;
}
2022-03-06 13:29:38 +00:00
_sectorsPerCluster = (uint)(_superblock.sectorsPerCluster * logicalSectorsPerPhysicalSectors);
_imagePlugin = imagePlugin;
_firstClusterSector = _fatStartSector + fatSize;
_bytesPerCluster = _sectorsPerCluster * imagePlugin.Info.SectorSize;
2022-03-06 13:29:38 +00:00
AaruConsole.DebugWriteLine("Xbox FAT plugin", "sectorsPerCluster = {0}", _sectorsPerCluster);
AaruConsole.DebugWriteLine("Xbox FAT plugin", "bytesPerCluster = {0}", _bytesPerCluster);
AaruConsole.DebugWriteLine("Xbox FAT plugin", "firstClusterSector = {0}", _firstClusterSector);
2022-03-06 13:29:38 +00:00
uint[] rootDirectoryClusters = GetClusters(_superblock.rootDirectoryCluster);
2019-04-07 13:31:27 +01:00
2022-03-06 13:29:38 +00:00
if(rootDirectoryClusters is null)
return ErrorNumber.InvalidArgument;
2019-04-07 13:31:27 +01:00
2022-03-07 07:36:44 +00:00
var rootDirectoryBuffer = new byte[_bytesPerCluster * rootDirectoryClusters.Length];
2019-04-07 13:31:27 +01:00
2022-03-06 13:29:38 +00:00
AaruConsole.DebugWriteLine("Xbox FAT plugin", "Reading root directory");
2020-02-29 18:03:35 +00:00
2022-03-07 07:36:44 +00:00
for(var i = 0; i < rootDirectoryClusters.Length; i++)
2022-03-06 13:29:38 +00:00
{
2022-03-07 07:36:44 +00:00
errno = imagePlugin.ReadSectors(_firstClusterSector + (rootDirectoryClusters[i] - 1) * _sectorsPerCluster,
_sectorsPerCluster, out buffer);
2022-03-06 13:29:38 +00:00
if(errno != ErrorNumber.NoError)
return errno;
2020-02-29 18:03:35 +00:00
2022-03-06 13:29:38 +00:00
Array.Copy(buffer, 0, rootDirectoryBuffer, i * _bytesPerCluster, _bytesPerCluster);
}
2019-04-07 13:31:27 +01:00
2022-03-06 13:29:38 +00:00
_rootDirectory = new Dictionary<string, DirectoryEntry>();
2019-04-07 13:31:27 +01:00
2022-03-07 07:36:44 +00:00
var pos = 0;
2020-02-29 18:03:35 +00:00
2022-03-06 13:29:38 +00:00
while(pos < rootDirectoryBuffer.Length)
{
DirectoryEntry entry = _littleEndian
2022-03-07 07:36:44 +00:00
? Marshal.ByteArrayToStructureLittleEndian<DirectoryEntry>(rootDirectoryBuffer,
pos, Marshal.SizeOf<DirectoryEntry>())
: Marshal.ByteArrayToStructureBigEndian<DirectoryEntry>(rootDirectoryBuffer, pos,
Marshal.SizeOf<DirectoryEntry>());
2019-04-07 13:31:27 +01:00
2022-03-06 13:29:38 +00:00
pos += Marshal.SizeOf<DirectoryEntry>();
2019-04-07 13:31:27 +01:00
2022-03-06 13:29:38 +00:00
if(entry.filenameSize == UNUSED_DIRENTRY ||
entry.filenameSize == FINISHED_DIRENTRY)
break;
2019-04-07 13:31:27 +01:00
2022-03-06 13:29:38 +00:00
if(entry.filenameSize == DELETED_DIRENTRY ||
entry.filenameSize > MAX_FILENAME)
continue;
2019-04-07 13:31:27 +01:00
2022-03-06 13:29:38 +00:00
string filename = Encoding.GetString(entry.filename, 0, entry.filenameSize);
2019-04-07 13:31:27 +01:00
2022-03-06 13:29:38 +00:00
_rootDirectory.Add(filename, entry);
}
2019-04-07 13:31:27 +01:00
2022-03-06 13:29:38 +00:00
_cultureInfo = new CultureInfo("en-US", false);
_directoryCache = new Dictionary<string, Dictionary<string, DirectoryEntry>>();
_mounted = true;
2022-03-06 13:29:38 +00:00
return ErrorNumber.NoError;
}
2022-03-06 13:29:38 +00:00
/// <inheritdoc />
public ErrorNumber Unmount()
{
if(!_mounted)
return ErrorNumber.AccessDenied;
2022-03-06 13:29:38 +00:00
_fat16 = null;
_fat32 = null;
_imagePlugin = null;
_mounted = false;
2022-03-06 13:29:38 +00:00
return ErrorNumber.NoError;
}
2019-04-06 11:03:43 +01:00
2022-03-06 13:29:38 +00:00
/// <inheritdoc />
public ErrorNumber StatFs(out FileSystemInfo stat)
{
stat = null;
2020-02-29 18:03:35 +00:00
2022-03-06 13:29:38 +00:00
if(!_mounted)
return ErrorNumber.AccessDenied;
2022-03-06 13:29:38 +00:00
stat = _statfs.ShallowCopy();
2019-04-06 11:03:43 +01:00
2022-03-06 13:29:38 +00:00
return ErrorNumber.NoError;
2019-04-06 11:03:43 +01:00
}
}