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

302 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-12-31 23:08:23 +00:00
// Copyright © 2011-2021 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;
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
2020-07-20 15:43:52 +01:00
namespace Aaru.Filesystems
2019-04-06 11:03:43 +01:00
{
2020-07-22 13:20:25 +01:00
public sealed partial class XboxFatPlugin
2019-04-06 11:03:43 +01:00
{
2021-08-17 13:56:05 +01:00
/// <inheritdoc />
2021-09-16 04:42:14 +01:00
public ErrorNumber Mount(IMediaImage imagePlugin, Partition partition, Encoding encoding,
Dictionary<string, string> options, string @namespace)
{
2020-07-20 21:11:32 +01:00
Encoding = Encoding.GetEncoding("iso-8859-15");
_littleEndian = true;
2020-07-22 13:20:25 +01:00
options ??= GetDefaultOptions();
2020-02-29 18:03:35 +00:00
if(options.TryGetValue("debug", out string debugString))
2020-07-20 21:11:32 +01:00
bool.TryParse(debugString, out _debug);
2020-02-29 18:03:35 +00:00
if(imagePlugin.Info.SectorSize < 512)
2021-09-16 04:42:14 +01:00
return ErrorNumber.InvalidArgument;
2020-02-27 23:48:41 +00:00
AaruConsole.DebugWriteLine("Xbox FAT plugin", "Reading superblock");
ErrorNumber errno = imagePlugin.ReadSector(partition.Start, out byte[] sector);
if(errno != ErrorNumber.NoError)
return errno;
2020-07-20 21:11:32 +01:00
_superblock = Marshal.ByteArrayToStructureLittleEndian<Superblock>(sector);
2020-07-20 21:11:32 +01:00
if(_superblock.magic == FATX_CIGAM)
{
2020-07-20 21:11:32 +01:00
_superblock = Marshal.ByteArrayToStructureBigEndian<Superblock>(sector);
_littleEndian = false;
}
2020-07-20 21:11:32 +01:00
if(_superblock.magic != FATX_MAGIC)
2021-09-16 04:42:14 +01:00
return ErrorNumber.InvalidArgument;
2020-02-27 23:48:41 +00:00
AaruConsole.DebugWriteLine("Xbox FAT plugin",
2020-07-20 21:11:32 +01:00
_littleEndian ? "Filesystem is little endian" : "Filesystem is big endian");
2020-07-20 21:11:32 +01:00
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);
2020-07-20 21:11:32 +01:00
string volumeLabel = StringHandlers.CToString(_superblock.volumeLabel,
!_littleEndian ? Encoding.BigEndianUnicode : Encoding.Unicode,
true);
XmlFsType = new FileSystemType
{
Type = "FATX filesystem",
2020-07-20 21:11:32 +01:00
ClusterSize = (uint)(_superblock.sectorsPerCluster * logicalSectorsPerPhysicalSectors *
2020-02-29 18:03:35 +00:00
imagePlugin.Info.SectorSize),
2020-07-20 04:34:16 +01:00
VolumeName = volumeLabel,
2020-07-20 21:11:32 +01:00
VolumeSerial = $"{_superblock.id:X8}"
};
2020-02-29 18:03:35 +00:00
2021-08-17 14:27:19 +01:00
XmlFsType.Clusters = (partition.End - partition.Start + 1) * imagePlugin.Info.SectorSize /
2019-04-23 01:38:33 +01:00
XmlFsType.ClusterSize;
2020-07-20 21:11:32 +01:00
_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,
2020-07-20 21:11:32 +01:00
Serial32 = _superblock.magic
2020-02-29 18:03:35 +00:00
},
2020-07-20 04:34:16 +01:00
PluginId = Id,
2020-07-20 21:11:32 +01:00
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-07-20 21:11:32 +01: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-07-20 21:11:32 +01:00
_fatStartSector = (FAT_START / imagePlugin.Info.SectorSize) + partition.Start;
uint fatSize;
2020-07-20 21:11:32 +01:00
AaruConsole.DebugWriteLine("Xbox FAT plugin", "fatStartSector: {0}", _fatStartSector);
2020-07-20 21:11:32 +01:00
if(_statfs.Blocks > MAX_XFAT16_CLUSTERS)
{
2020-02-27 23:48:41 +00:00
AaruConsole.DebugWriteLine("Xbox FAT plugin", "Reading FAT32");
2021-08-17 14:27:19 +01:00
fatSize = (uint)((_statfs.Blocks + 1) * sizeof(uint) / imagePlugin.Info.SectorSize);
2020-02-29 18:03:35 +00:00
2021-08-17 14:27:19 +01:00
if((uint)((_statfs.Blocks + 1) * sizeof(uint) % imagePlugin.Info.SectorSize) > 0)
2020-02-29 18:03:35 +00:00
fatSize++;
2021-08-17 14:27:19 +01:00
long fatClusters = fatSize * imagePlugin.Info.SectorSize / 4096;
2020-02-29 18:03:35 +00:00
2021-08-17 14:27:19 +01:00
if(fatSize * imagePlugin.Info.SectorSize % 4096 > 0)
2020-02-29 18:03:35 +00:00
fatClusters++;
2021-08-17 14:27:19 +01:00
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);
errno = imagePlugin.ReadSectors(_fatStartSector, fatSize, out buffer);
if(errno != ErrorNumber.NoError)
return errno;
2020-02-27 23:48:41 +00:00
AaruConsole.DebugWriteLine("Xbox FAT plugin", "Casting FAT");
2020-07-20 21:11:32 +01:00
_fat32 = MemoryMarshal.Cast<byte, uint>(buffer).ToArray();
2020-02-29 18:03:35 +00:00
2020-07-20 21:11:32 +01:00
if(!_littleEndian)
for(int i = 0; i < _fat32.Length; i++)
_fat32[i] = Swapping.Swap(_fat32[i]);
2020-07-20 21:11:32 +01:00
AaruConsole.DebugWriteLine("Xbox FAT plugin", "fat32[0] == FATX32_ID = {0}", _fat32[0] == FATX32_ID);
2020-02-29 18:03:35 +00:00
2020-07-20 21:11:32 +01:00
if(_fat32[0] != FATX32_ID)
2021-09-16 04:42:14 +01:00
return ErrorNumber.InvalidArgument;
}
else
{
2020-02-27 23:48:41 +00:00
AaruConsole.DebugWriteLine("Xbox FAT plugin", "Reading FAT16");
2021-08-17 14:27:19 +01:00
fatSize = (uint)((_statfs.Blocks + 1) * sizeof(ushort) / imagePlugin.Info.SectorSize);
2021-08-17 14:27:19 +01:00
if((uint)((_statfs.Blocks + 1) * sizeof(ushort) % imagePlugin.Info.SectorSize) > 0)
2020-02-29 18:03:35 +00:00
fatSize++;
2021-08-17 14:27:19 +01:00
long fatClusters = fatSize * imagePlugin.Info.SectorSize / 4096;
2020-02-29 18:03:35 +00:00
2021-08-17 14:27:19 +01:00
if(fatSize * imagePlugin.Info.SectorSize % 4096 > 0)
2020-02-29 18:03:35 +00:00
fatClusters++;
2021-08-17 14:27:19 +01:00
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);
errno = imagePlugin.ReadSectors(_fatStartSector, fatSize, out buffer);
if(errno != ErrorNumber.NoError)
return errno;
2020-02-27 23:48:41 +00:00
AaruConsole.DebugWriteLine("Xbox FAT plugin", "Casting FAT");
2020-07-20 21:11:32 +01:00
_fat16 = MemoryMarshal.Cast<byte, ushort>(buffer).ToArray();
2020-02-29 18:03:35 +00:00
2020-07-20 21:11:32 +01:00
if(!_littleEndian)
for(int i = 0; i < _fat16.Length; i++)
_fat16[i] = Swapping.Swap(_fat16[i]);
2020-07-20 21:11:32 +01:00
AaruConsole.DebugWriteLine("Xbox FAT plugin", "fat16[0] == FATX16_ID = {0}", _fat16[0] == FATX16_ID);
2020-02-29 18:03:35 +00:00
2020-07-20 21:11:32 +01:00
if(_fat16[0] != FATX16_ID)
2021-09-16 04:42:14 +01:00
return ErrorNumber.InvalidArgument;
}
2020-07-20 21:11:32 +01:00
_sectorsPerCluster = (uint)(_superblock.sectorsPerCluster * logicalSectorsPerPhysicalSectors);
_imagePlugin = imagePlugin;
_firstClusterSector = _fatStartSector + fatSize;
_bytesPerCluster = _sectorsPerCluster * imagePlugin.Info.SectorSize;
2020-07-20 21:11:32 +01: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);
2020-07-20 21:11:32 +01:00
uint[] rootDirectoryClusters = GetClusters(_superblock.rootDirectoryCluster);
2019-04-07 13:31:27 +01:00
2020-02-29 18:03:35 +00:00
if(rootDirectoryClusters is null)
2021-09-16 04:42:14 +01:00
return ErrorNumber.InvalidArgument;
2019-04-07 13:31:27 +01:00
2020-07-20 21:11:32 +01:00
byte[] rootDirectoryBuffer = new byte[_bytesPerCluster * rootDirectoryClusters.Length];
2019-04-07 13:31:27 +01:00
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++)
{
errno =
2020-07-20 21:11:32 +01:00
imagePlugin.ReadSectors(_firstClusterSector + ((rootDirectoryClusters[i] - 1) * _sectorsPerCluster),
_sectorsPerCluster, out buffer);
if(errno != ErrorNumber.NoError)
return errno;
2020-02-29 18:03:35 +00:00
2020-07-20 21:11:32 +01:00
Array.Copy(buffer, 0, rootDirectoryBuffer, i * _bytesPerCluster, _bytesPerCluster);
2019-04-07 13:31:27 +01:00
}
2020-07-20 21:11:32 +01:00
_rootDirectory = new Dictionary<string, DirectoryEntry>();
2019-04-07 13:31:27 +01:00
int pos = 0;
2020-02-29 18:03:35 +00:00
2019-04-07 13:31:27 +01:00
while(pos < rootDirectoryBuffer.Length)
{
2020-07-20 21:11:32 +01:00
DirectoryEntry entry = _littleEndian
2020-02-29 18:03:35 +00:00
? Marshal.
2021-08-17 21:23:10 +01:00
ByteArrayToStructureLittleEndian<
DirectoryEntry>(rootDirectoryBuffer, pos,
Marshal.SizeOf<DirectoryEntry>())
2019-04-07 13:31:27 +01:00
: Marshal.ByteArrayToStructureBigEndian<DirectoryEntry>(rootDirectoryBuffer,
pos, Marshal.SizeOf<DirectoryEntry>());
2019-04-07 13:31:27 +01:00
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);
2020-07-20 21:11:32 +01:00
_rootDirectory.Add(filename, entry);
2019-04-07 13:31:27 +01:00
}
2020-07-20 21:11:32 +01:00
_cultureInfo = new CultureInfo("en-US", false);
_directoryCache = new Dictionary<string, Dictionary<string, DirectoryEntry>>();
_mounted = true;
2021-09-16 04:42:14 +01:00
return ErrorNumber.NoError;
}
2021-08-17 13:56:05 +01:00
/// <inheritdoc />
2021-09-16 04:42:14 +01:00
public ErrorNumber Unmount()
{
2020-07-20 21:11:32 +01:00
if(!_mounted)
2021-09-16 04:42:14 +01:00
return ErrorNumber.AccessDenied;
2020-07-20 21:11:32 +01:00
_fat16 = null;
_fat32 = null;
_imagePlugin = null;
_mounted = false;
2021-09-16 04:42:14 +01:00
return ErrorNumber.NoError;
}
2019-04-06 11:03:43 +01:00
2021-08-17 13:56:05 +01:00
/// <inheritdoc />
2021-09-16 04:42:14 +01:00
public ErrorNumber StatFs(out FileSystemInfo stat)
{
stat = null;
2020-02-29 18:03:35 +00:00
2020-07-20 21:11:32 +01:00
if(!_mounted)
2021-09-16 04:42:14 +01:00
return ErrorNumber.AccessDenied;
2020-07-20 21:11:32 +01:00
stat = _statfs.ShallowCopy();
2019-04-06 11:03:43 +01:00
2021-09-16 04:42:14 +01:00
return ErrorNumber.NoError;
}
2019-04-06 11:03:43 +01:00
}
}