mirror of
https://github.com/aaru-dps/Aaru.git
synced 2025-12-16 19:24:25 +00:00
Calculate size of Xbox FAT and read it to memory.
This commit is contained in:
@@ -41,6 +41,10 @@ namespace DiscImageChef.Filesystems.FATX
|
|||||||
const byte UNUSED_DIRENTRY = 0x00;
|
const byte UNUSED_DIRENTRY = 0x00;
|
||||||
const byte DELETED_DIRENTRY = 0xE5;
|
const byte DELETED_DIRENTRY = 0xE5;
|
||||||
const byte MAX_FILENAME = 42;
|
const byte MAX_FILENAME = 42;
|
||||||
|
const int MAX_XFAT16_CLUSTERS = 65525;
|
||||||
|
const int FAT_START = 4096;
|
||||||
|
const uint FATX32_ID = 0xFFFFFFF8;
|
||||||
|
const ushort FATX16_ID = 0xFFF8;
|
||||||
|
|
||||||
[Flags]
|
[Flags]
|
||||||
enum Attributes : byte
|
enum Attributes : byte
|
||||||
|
|||||||
@@ -38,6 +38,12 @@ namespace DiscImageChef.Filesystems.FATX
|
|||||||
{
|
{
|
||||||
public partial class XboxFatPlugin
|
public partial class XboxFatPlugin
|
||||||
{
|
{
|
||||||
public Errno ReadDir(string path, out List<string> contents) => throw new NotImplementedException();
|
public Errno ReadDir(string path, out List<string> contents)
|
||||||
|
{
|
||||||
|
contents = null;
|
||||||
|
if(!mounted) return Errno.AccessDenied;
|
||||||
|
|
||||||
|
throw new NotImplementedException();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -33,6 +33,7 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
|
using DiscImageChef.CommonTypes;
|
||||||
using DiscImageChef.CommonTypes.Interfaces;
|
using DiscImageChef.CommonTypes.Interfaces;
|
||||||
using DiscImageChef.CommonTypes.Structs;
|
using DiscImageChef.CommonTypes.Structs;
|
||||||
using Schemas;
|
using Schemas;
|
||||||
@@ -41,6 +42,18 @@ namespace DiscImageChef.Filesystems.FATX
|
|||||||
{
|
{
|
||||||
public partial class XboxFatPlugin : IReadOnlyFilesystem
|
public partial class XboxFatPlugin : IReadOnlyFilesystem
|
||||||
{
|
{
|
||||||
|
ushort[] fat16;
|
||||||
|
uint[] fat32;
|
||||||
|
ulong fatStartSector;
|
||||||
|
ulong firstClusterSector;
|
||||||
|
IMediaImage imagePlugin;
|
||||||
|
bool littleEndian;
|
||||||
|
bool mounted;
|
||||||
|
Partition partition;
|
||||||
|
uint sectorsPerCluster;
|
||||||
|
FileSystemInfo stat;
|
||||||
|
|
||||||
|
Superblock superblock;
|
||||||
public FileSystemType XmlFsType { get; private set; }
|
public FileSystemType XmlFsType { get; private set; }
|
||||||
public Encoding Encoding { get; private set; }
|
public Encoding Encoding { get; private set; }
|
||||||
public string Name => "FATX Filesystem Plugin";
|
public string Name => "FATX Filesystem Plugin";
|
||||||
|
|||||||
@@ -37,12 +37,35 @@ namespace DiscImageChef.Filesystems.FATX
|
|||||||
{
|
{
|
||||||
public partial class XboxFatPlugin
|
public partial class XboxFatPlugin
|
||||||
{
|
{
|
||||||
public Errno MapBlock(string path, long fileBlock, out long deviceBlock) => throw new NotImplementedException();
|
public Errno MapBlock(string path, long fileBlock, out long deviceBlock)
|
||||||
|
{
|
||||||
|
deviceBlock = 0;
|
||||||
|
if(!mounted) return Errno.AccessDenied;
|
||||||
|
|
||||||
public Errno GetAttributes(string path, out FileAttributes attributes) => throw new NotImplementedException();
|
throw new NotImplementedException();
|
||||||
|
}
|
||||||
|
|
||||||
public Errno Read(string path, long offset, long size, ref byte[] buf) => throw new NotImplementedException();
|
public Errno GetAttributes(string path, out FileAttributes attributes)
|
||||||
|
{
|
||||||
|
attributes = new FileAttributes();
|
||||||
|
if(!mounted) return Errno.AccessDenied;
|
||||||
|
|
||||||
public Errno Stat(string path, out FileEntryInfo stat) => throw new NotImplementedException();
|
throw new NotImplementedException();
|
||||||
|
}
|
||||||
|
|
||||||
|
public Errno Read(string path, long offset, long size, ref byte[] buf)
|
||||||
|
{
|
||||||
|
if(!mounted) return Errno.AccessDenied;
|
||||||
|
|
||||||
|
throw new NotImplementedException();
|
||||||
|
}
|
||||||
|
|
||||||
|
public Errno Stat(string path, out FileEntryInfo stat)
|
||||||
|
{
|
||||||
|
stat = null;
|
||||||
|
if(!mounted) return Errno.AccessDenied;
|
||||||
|
|
||||||
|
throw new NotImplementedException();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -32,21 +32,158 @@
|
|||||||
|
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
using System.Runtime.InteropServices;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
using DiscImageChef.CommonTypes;
|
using DiscImageChef.CommonTypes;
|
||||||
using DiscImageChef.CommonTypes.Interfaces;
|
using DiscImageChef.CommonTypes.Interfaces;
|
||||||
using DiscImageChef.CommonTypes.Structs;
|
using DiscImageChef.CommonTypes.Structs;
|
||||||
|
using DiscImageChef.Console;
|
||||||
|
using Schemas;
|
||||||
|
using Marshal = DiscImageChef.Helpers.Marshal;
|
||||||
|
|
||||||
namespace DiscImageChef.Filesystems.FATX
|
namespace DiscImageChef.Filesystems.FATX
|
||||||
{
|
{
|
||||||
public partial class XboxFatPlugin
|
public partial class XboxFatPlugin
|
||||||
{
|
{
|
||||||
public Errno Mount(IMediaImage imagePlugin, Partition partition, Encoding encoding,
|
public Errno Mount(IMediaImage imagePlugin, Partition partition, Encoding encoding,
|
||||||
Dictionary<string, string> options) =>
|
Dictionary<string, string> options)
|
||||||
|
{
|
||||||
|
Encoding = Encoding.GetEncoding("iso-8859-15");
|
||||||
|
littleEndian = true;
|
||||||
|
|
||||||
|
if(imagePlugin.Info.SectorSize < 512) return Errno.InvalidArgument;
|
||||||
|
|
||||||
|
DicConsole.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;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(superblock.magic != FATX_MAGIC) return Errno.InvalidArgument;
|
||||||
|
|
||||||
|
DicConsole.DebugWriteLine("Xbox FAT plugin",
|
||||||
|
littleEndian ? "Filesystem is little endian" : "Filesystem is big endian");
|
||||||
|
|
||||||
|
int logicalSectorsPerPhysicalSectors = partition.Offset == 0 ? 8 : 1;
|
||||||
|
DicConsole.DebugWriteLine("Xbox FAT plugin", "logicalSectorsPerPhysicalSectors = {0}",
|
||||||
|
logicalSectorsPerPhysicalSectors);
|
||||||
|
|
||||||
|
string volumeLabel = StringHandlers.CToString(superblock.volumeLabel,
|
||||||
|
!littleEndian ? Encoding.BigEndianUnicode : Encoding.Unicode,
|
||||||
|
true);
|
||||||
|
|
||||||
|
XmlFsType = new FileSystemType
|
||||||
|
{
|
||||||
|
Type = "FATX filesystem",
|
||||||
|
ClusterSize =
|
||||||
|
(int)(superblock.sectorsPerCluster * logicalSectorsPerPhysicalSectors *
|
||||||
|
imagePlugin.Info.SectorSize),
|
||||||
|
VolumeName = volumeLabel,
|
||||||
|
VolumeSerial = $"{superblock.id:X8}"
|
||||||
|
};
|
||||||
|
XmlFsType.Clusters = (long)((partition.End - partition.Start + 1) * imagePlugin.Info.SectorSize /
|
||||||
|
(ulong)XmlFsType.ClusterSize);
|
||||||
|
|
||||||
|
stat = new FileSystemInfo
|
||||||
|
{
|
||||||
|
Blocks = XmlFsType.Clusters,
|
||||||
|
FilenameLength = MAX_FILENAME,
|
||||||
|
Files = 0, // Requires traversing all directories
|
||||||
|
FreeFiles = 0,
|
||||||
|
Id = {IsInt = true, Serial32 = superblock.magic},
|
||||||
|
PluginId = Id,
|
||||||
|
Type = littleEndian ? "Xbox FAT" : "Xbox 360 FAT",
|
||||||
|
FreeBlocks = 0 // Requires traversing the FAT
|
||||||
|
};
|
||||||
|
|
||||||
|
DicConsole.DebugWriteLine("Xbox FAT plugin", "XmlFsType.ClusterSize", XmlFsType.ClusterSize);
|
||||||
|
DicConsole.DebugWriteLine("Xbox FAT plugin", "XmlFsType.VolumeName", XmlFsType.VolumeName);
|
||||||
|
DicConsole.DebugWriteLine("Xbox FAT plugin", "XmlFsType.VolumeSerial", XmlFsType.VolumeSerial);
|
||||||
|
DicConsole.DebugWriteLine("Xbox FAT plugin", "stat.Blocks", stat.Blocks);
|
||||||
|
DicConsole.DebugWriteLine("Xbox FAT plugin", "stat.FilenameLength", stat.FilenameLength);
|
||||||
|
DicConsole.DebugWriteLine("Xbox FAT plugin", "stat.Id", stat.Id);
|
||||||
|
DicConsole.DebugWriteLine("Xbox FAT plugin", "stat.Type", stat.Type);
|
||||||
|
|
||||||
|
byte[] buffer;
|
||||||
|
fatStartSector = FAT_START / imagePlugin.Info.SectorSize + partition.Start;
|
||||||
|
uint fatSize;
|
||||||
|
|
||||||
|
DicConsole.DebugWriteLine("Xbox FAT plugin", "fatStartSector", fatStartSector);
|
||||||
|
|
||||||
|
if(stat.Blocks > MAX_XFAT16_CLUSTERS)
|
||||||
|
{
|
||||||
|
DicConsole.DebugWriteLine("Xbox FAT plugin", "Reading FAT32");
|
||||||
|
|
||||||
|
fatSize = (uint)(stat.Blocks * sizeof(uint) / imagePlugin.Info.SectorSize);
|
||||||
|
if((uint)(stat.Blocks * sizeof(uint) % imagePlugin.Info.SectorSize) > 0) fatSize++;
|
||||||
|
DicConsole.DebugWriteLine("Xbox FAT plugin", "FAT is {0} sectors", fatSize);
|
||||||
|
|
||||||
|
buffer = imagePlugin.ReadSectors(fatStartSector, fatSize);
|
||||||
|
|
||||||
|
DicConsole.DebugWriteLine("Xbox FAT plugin", "Casting FAT");
|
||||||
|
fat32 = MemoryMarshal.Cast<byte, uint>(buffer).ToArray();
|
||||||
|
if(!littleEndian)
|
||||||
|
for(int i = 0; i < fat32.Length; i++)
|
||||||
|
fat32[i] = Swapping.Swap(fat32[i]);
|
||||||
|
|
||||||
|
DicConsole.DebugWriteLine("Xbox FAT plugin", "fat32[0] == FATX32_ID = {0}", fat32[0] == FATX32_ID);
|
||||||
|
if(fat32[0] != FATX32_ID) return Errno.InvalidArgument;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
DicConsole.DebugWriteLine("Xbox FAT plugin", "Reading FAT16");
|
||||||
|
|
||||||
|
fatSize = (uint)(stat.Blocks * sizeof(ushort) / imagePlugin.Info.SectorSize);
|
||||||
|
if((uint)(stat.Blocks * sizeof(ushort) % imagePlugin.Info.SectorSize) > 0) fatSize++;
|
||||||
|
DicConsole.DebugWriteLine("Xbox FAT plugin", "FAT is {0} sectors", fatSize);
|
||||||
|
|
||||||
|
buffer = imagePlugin.ReadSectors(fatStartSector, fatSize);
|
||||||
|
|
||||||
|
DicConsole.DebugWriteLine("Xbox FAT plugin", "Casting FAT");
|
||||||
|
fat16 = MemoryMarshal.Cast<byte, ushort>(buffer).ToArray();
|
||||||
|
if(!littleEndian)
|
||||||
|
for(int i = 0; i < fat16.Length; i++)
|
||||||
|
fat16[i] = Swapping.Swap(fat16[i]);
|
||||||
|
|
||||||
|
DicConsole.DebugWriteLine("Xbox FAT plugin", "fat16[0] == FATX16_ID = {0}", fat16[0] == FATX16_ID);
|
||||||
|
if(fat16[0] != FATX16_ID) return Errno.InvalidArgument;
|
||||||
|
}
|
||||||
|
|
||||||
|
sectorsPerCluster = (uint)(superblock.sectorsPerCluster * logicalSectorsPerPhysicalSectors);
|
||||||
|
this.partition = partition;
|
||||||
|
this.imagePlugin = imagePlugin;
|
||||||
|
firstClusterSector = fatStartSector + fatSize;
|
||||||
|
|
||||||
|
DicConsole.DebugWriteLine("Xbox FAT plugin", "sectorsPerCluster = {0}", sectorsPerCluster);
|
||||||
|
DicConsole.DebugWriteLine("Xbox FAT plugin", "firstClusterSector = {0}", firstClusterSector);
|
||||||
|
|
||||||
throw new NotImplementedException();
|
throw new NotImplementedException();
|
||||||
|
}
|
||||||
|
|
||||||
public Errno Unmount() => throw new NotImplementedException();
|
public Errno Unmount()
|
||||||
|
{
|
||||||
|
if(!mounted) return Errno.AccessDenied;
|
||||||
|
|
||||||
public Errno StatFs(out FileSystemInfo stat) => throw new NotImplementedException();
|
fat16 = null;
|
||||||
|
fat32 = null;
|
||||||
|
imagePlugin = null;
|
||||||
|
partition = new Partition();
|
||||||
|
mounted = false;
|
||||||
|
|
||||||
|
return Errno.NoError;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Errno StatFs(out FileSystemInfo stat)
|
||||||
|
{
|
||||||
|
stat = this.stat;
|
||||||
|
|
||||||
|
return !mounted ? Errno.AccessDenied : Errno.NoError;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user