Npdm Support

from Ryujinx with adjustments
This commit is contained in:
jonnysp 2018-10-15 19:52:25 +02:00
parent ffd1af4c8e
commit 81f43774d1
15 changed files with 768 additions and 0 deletions

55
LibHac/Npdm/ACI0.cs Normal file
View file

@ -0,0 +1,55 @@
using System;
using System.IO;
namespace LibHac
{
class ACI0
{
public string Magic;
public long TitleId { get; private set; }
public int FsVersion { get; private set; }
public ulong FsPermissionsBitmask { get; private set; }
public ServiceAccessControl ServiceAccessControl { get; private set; }
public KernelAccessControl KernelAccessControl { get; private set; }
public ACI0(Stream Stream, int Offset)
{
Stream.Seek(Offset, SeekOrigin.Begin);
BinaryReader Reader = new BinaryReader(Stream);
Magic = Reader.ReadAscii(0x4);
if (Magic != "ACI0")
{
throw new Exception("ACI0 Stream doesn't contain ACI0 section!");
}
Stream.Seek(0xc, SeekOrigin.Current);
TitleId = Reader.ReadInt64();
//Reserved.
Stream.Seek(8, SeekOrigin.Current);
int FsAccessHeaderOffset = Reader.ReadInt32();
int FsAccessHeaderSize = Reader.ReadInt32();
int ServiceAccessControlOffset = Reader.ReadInt32();
int ServiceAccessControlSize = Reader.ReadInt32();
int KernelAccessControlOffset = Reader.ReadInt32();
int KernelAccessControlSize = Reader.ReadInt32();
FsAccessHeader FsAccessHeader = new FsAccessHeader(Stream, Offset + FsAccessHeaderOffset, FsAccessHeaderSize);
FsVersion = FsAccessHeader.Version;
FsPermissionsBitmask = FsAccessHeader.PermissionsBitmask;
ServiceAccessControl = new ServiceAccessControl(Stream, Offset + ServiceAccessControlOffset, ServiceAccessControlSize);
KernelAccessControl = new KernelAccessControl(Stream, Offset + KernelAccessControlOffset, KernelAccessControlSize);
}
}
}

62
LibHac/Npdm/ACID.cs Normal file
View file

@ -0,0 +1,62 @@
using System;
using System.IO;
namespace LibHac
{
class ACID
{
public string Magic;
public byte[] RSA2048Signature { get; private set; }
public byte[] RSA2048Modulus { get; private set; }
public int Unknown1 { get; private set; }
public int Flags { get; private set; }
public long TitleIdRangeMin { get; private set; }
public long TitleIdRangeMax { get; private set; }
public FsAccessControl FsAccessControl { get; private set; }
public ServiceAccessControl ServiceAccessControl { get; private set; }
public KernelAccessControl KernelAccessControl { get; private set; }
public ACID(Stream Stream, int Offset)
{
Stream.Seek(Offset, SeekOrigin.Begin);
BinaryReader Reader = new BinaryReader(Stream);
RSA2048Signature = Reader.ReadBytes(0x100);
RSA2048Modulus = Reader.ReadBytes(0x100);
Magic = Reader.ReadAscii(0x4);
if (Magic != "ACID")
{
throw new Exception("ACID Stream doesn't contain ACID section!");
}
//Size field used with the above signature (?).
Unknown1 = Reader.ReadInt32();
Reader.ReadInt32();
//Bit0 must be 1 on retail, on devunit 0 is also allowed. Bit1 is unknown.
Flags = Reader.ReadInt32();
TitleIdRangeMin = Reader.ReadInt64();
TitleIdRangeMax = Reader.ReadInt64();
int FsAccessControlOffset = Reader.ReadInt32();
int FsAccessControlSize = Reader.ReadInt32();
int ServiceAccessControlOffset = Reader.ReadInt32();
int ServiceAccessControlSize = Reader.ReadInt32();
int KernelAccessControlOffset = Reader.ReadInt32();
int KernelAccessControlSize = Reader.ReadInt32();
FsAccessControl = new FsAccessControl(Stream, Offset + FsAccessControlOffset, FsAccessControlSize);
ServiceAccessControl = new ServiceAccessControl(Stream, Offset + ServiceAccessControlOffset, ServiceAccessControlSize);
KernelAccessControl = new KernelAccessControl(Stream, Offset + KernelAccessControlOffset, KernelAccessControlSize);
}
}
}

View file

@ -0,0 +1,9 @@
namespace LibHac
{
enum ApplicationType
{
SystemModule,
Application,
Applet
}
}

View file

@ -0,0 +1,28 @@
using System.IO;
namespace LibHac
{
class FsAccessControl
{
public int Version { get; private set; }
public ulong PermissionsBitmask { get; private set; }
public int Unknown1 { get; private set; }
public int Unknown2 { get; private set; }
public int Unknown3 { get; private set; }
public int Unknown4 { get; private set; }
public FsAccessControl(Stream Stream, int Offset, int Size)
{
Stream.Seek(Offset, SeekOrigin.Begin);
BinaryReader Reader = new BinaryReader(Stream);
Version = Reader.ReadInt32();
PermissionsBitmask = Reader.ReadUInt64();
Unknown1 = Reader.ReadInt32();
Unknown2 = Reader.ReadInt32();
Unknown3 = Reader.ReadInt32();
Unknown4 = Reader.ReadInt32();
}
}
}

View file

@ -0,0 +1,36 @@
using System;
using System.IO;
namespace LibHac
{
class FsAccessHeader
{
public int Version { get; private set; }
public ulong PermissionsBitmask { get; private set; }
public FsAccessHeader(Stream Stream, int Offset, int Size)
{
Stream.Seek(Offset, SeekOrigin.Begin);
BinaryReader Reader = new BinaryReader(Stream);
Version = Reader.ReadInt32();
PermissionsBitmask = Reader.ReadUInt64();
int DataSize = Reader.ReadInt32();
if (DataSize != 0x1c)
{
throw new Exception("FsAccessHeader is corrupted!");
}
int ContentOwnerIdSize = Reader.ReadInt32();
int DataAndContentOwnerIdSize = Reader.ReadInt32();
if (DataAndContentOwnerIdSize != 0x1c)
{
throw new NotImplementedException("ContentOwnerId section is not implemented!");
}
}
}
}

View file

@ -0,0 +1,33 @@
namespace LibHac
{
enum FsPermissionBool : ulong
{
BisCache = 0x8000000000000080,
EraseMmc = 0x8000000000000080,
GameCardCertificate = 0x8000000000000010,
GameCardIdSet = 0x8000000000000010,
GameCardDriver = 0x8000000000000200,
GameCardAsic = 0x8000000000000200,
SaveDataCreate = 0x8000000000002020,
SaveDataDelete0 = 0x8000000000000060,
SystemSaveDataCreate0 = 0x8000000000000028,
SystemSaveDataCreate1 = 0x8000000000000020,
SaveDataDelete1 = 0x8000000000004028,
SaveDataIterators0 = 0x8000000000000060,
SaveDataIterators1 = 0x8000000000004020,
SaveThumbnails = 0x8000000000020000,
PosixTime = 0x8000000000000400,
SaveDataExtraData = 0x8000000000004060,
GlobalMode = 0x8000000000080000,
SpeedEmulation = 0x8000000000080000,
NULL = 0,
PaddingFiles = 0xC000000000800000,
SaveData_Debug = 0xC000000001000000,
SaveData_SystemManagement = 0xC000000002000000,
Unknown0x16 = 0x8000000004000000,
Unknown0x17 = 0x8000000008000000,
Unknown0x18 = 0x8000000010000000,
Unknown0x19 = 0x8000000000000800,
Unknown0x1A = 0x8000000000004020
}
}

View file

@ -0,0 +1,45 @@
namespace LibHac
{
enum FsPermissionRw : ulong
{
MountContentType2 = 0x8000000000000801,
MountContentType5 = 0x8000000000000801,
MountContentType3 = 0x8000000000000801,
MountContentType4 = 0x8000000000000801,
MountContentType6 = 0x8000000000000801,
MountContentType7 = 0x8000000000000801,
Unknown0x6 = 0x8000000000000000,
ContentStorageAccess = 0x8000000000000800,
ImageDirectoryAccess = 0x8000000000001000,
MountBisType28 = 0x8000000000000084,
MountBisType29 = 0x8000000000000080,
MountBisType30 = 0x8000000000008080,
MountBisType31 = 0x8000000000008080,
Unknown0xD = 0x8000000000000080,
SdCardAccess = 0xC000000000200000,
GameCardUser = 0x8000000000000010,
SaveDataAccess0 = 0x8000000000040020,
SystemSaveDataAccess0 = 0x8000000000000028,
SaveDataAccess1 = 0x8000000000000020,
SystemSaveDataAccess1 = 0x8000000000000020,
BisPartition0 = 0x8000000000010082,
BisPartition10 = 0x8000000000010080,
BisPartition20 = 0x8000000000010080,
BisPartition21 = 0x8000000000010080,
BisPartition22 = 0x8000000000010080,
BisPartition23 = 0x8000000000010080,
BisPartition24 = 0x8000000000010080,
BisPartition25 = 0x8000000000010080,
BisPartition26 = 0x8000000000000080,
BisPartition27 = 0x8000000000000084,
BisPartition28 = 0x8000000000000084,
BisPartition29 = 0x8000000000000080,
BisPartition30 = 0x8000000000000080,
BisPartition31 = 0x8000000000000080,
BisPartition32 = 0x8000000000000080,
Unknown0x23 = 0xC000000000200000,
GameCard_System = 0x8000000000000100,
MountContent_System = 0x8000000000100008,
HostAccess = 0xC000000000400000
}
}

View file

@ -0,0 +1,173 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
namespace LibHac
{
class KernelAccessControl
{
public List<KernelAccessControlItem> Items;
public KernelAccessControl(Stream Stream, int Offset, int Size)
{
Stream.Seek(Offset, SeekOrigin.Begin);
BinaryReader Reader = new BinaryReader(Stream);
KernelAccessControlItem[] Items = new KernelAccessControlItem[Size / 4];
for (int Index = 0; Index < Size / 4; Index++)
{
uint Descriptor = Reader.ReadUInt32();
//Ignore the descriptor.
if (Descriptor == 0xffffffff)
{
continue;
}
Items[Index] = new KernelAccessControlItem();
int LowBits = 0;
while ((Descriptor & 1) != 0)
{
Descriptor >>= 1;
LowBits++;
}
Descriptor >>= 1;
switch (LowBits)
{
//Kernel flags.
case 3:
{
Items[Index].HasKernelFlags = true;
Items[Index].HighestThreadPriority = (Descriptor >> 0) & 0x3f;
Items[Index].LowestThreadPriority = (Descriptor >> 6) & 0x3f;
Items[Index].LowestCpuId = (Descriptor >> 12) & 0xff;
Items[Index].HighestCpuId = (Descriptor >> 20) & 0xff;
break;
}
//Syscall mask.
case 4:
{
Items[Index].HasSvcFlags = true;
Items[Index].AllowedSvcs = new bool[0x80];
int SysCallBase = (int)(Descriptor >> 24) * 0x18;
for (int SysCall = 0; SysCall < 0x18 && SysCallBase + SysCall < 0x80; SysCall++)
{
Items[Index].AllowedSvcs[SysCallBase + SysCall] = (Descriptor & 1) != 0;
Descriptor >>= 1;
}
break;
}
//Map IO/Normal.
case 6:
{
ulong Address = (Descriptor & 0xffffff) << 12;
bool IsRo = (Descriptor >> 24) != 0;
if (Index == Size / 4 - 1)
{
throw new Exception("Invalid Kernel Access Control Descriptors!");
}
Descriptor = Reader.ReadUInt32();
if ((Descriptor & 0x7f) != 0x3f)
{
throw new Exception("Invalid Kernel Access Control Descriptors!");
}
Descriptor >>= 7;
ulong MmioSize = (Descriptor & 0xffffff) << 12;
bool IsNormal = (Descriptor >> 24) != 0;
Items[Index].NormalMmio.Add(new KernelAccessControlMmio(Address, MmioSize, IsRo, IsNormal));
Index++;
break;
}
//Map Normal Page.
case 7:
{
ulong Address = Descriptor << 12;
Items[Index].PageMmio.Add(new KernelAccessControlMmio(Address, 0x1000, false, false));
break;
}
//IRQ Pair.
case 11:
{
Items[Index].Irq.Add(new KernelAccessControlIrq(
(Descriptor >> 0) & 0x3ff,
(Descriptor >> 10) & 0x3ff));
break;
}
//Application Type.
case 13:
{
Items[Index].HasApplicationType = true;
Items[Index].ApplicationType = (int)Descriptor & 7;
break;
}
//Kernel Release Version.
case 14:
{
Items[Index].HasKernelVersion = true;
Items[Index].KernelVersionRelease = (int)Descriptor;
break;
}
//Handle Table Size.
case 15:
{
Items[Index].HasHandleTableSize = true;
Items[Index].HandleTableSize = (int)Descriptor;
break;
}
//Debug Flags.
case 16:
{
Items[Index].HasDebugFlags = true;
Items[Index].AllowDebug = ((Descriptor >> 0) & 1) != 0;
Items[Index].ForceDebug = ((Descriptor >> 1) & 1) != 0;
break;
}
}
}
this.Items = Items.ToList();
}
}
}

View file

@ -0,0 +1,14 @@
namespace LibHac
{
struct KernelAccessControlIrq
{
public uint Irq0 { get; private set; }
public uint Irq1 { get; private set; }
public KernelAccessControlIrq(uint Irq0, uint Irq1)
{
this.Irq0 = Irq0;
this.Irq1 = Irq1;
}
}
}

View file

@ -0,0 +1,22 @@
namespace LibHac
{
struct KernelAccessControlMmio
{
public ulong Address { get; private set; }
public ulong Size { get; private set; }
public bool IsRo { get; private set; }
public bool IsNormal { get; private set; }
public KernelAccessControlMmio(
ulong Address,
ulong Size,
bool IsRo,
bool IsNormal)
{
this.Address = Address;
this.Size = Size;
this.IsRo = IsRo;
this.IsNormal = IsNormal;
}
}
}

View file

@ -0,0 +1,33 @@
using System.Collections.Generic;
namespace LibHac
{
struct KernelAccessControlItem
{
public bool HasKernelFlags { get; set; }
public uint LowestThreadPriority { get; set; }
public uint HighestThreadPriority { get; set; }
public uint LowestCpuId { get; set; }
public uint HighestCpuId { get; set; }
public bool HasSvcFlags { get; set; }
public bool[] AllowedSvcs { get; set; }
public List<KernelAccessControlMmio> NormalMmio { get; set; }
public List<KernelAccessControlMmio> PageMmio { get; set; }
public List<KernelAccessControlIrq> Irq { get; set; }
public bool HasApplicationType { get; set; }
public int ApplicationType { get; set; }
public bool HasKernelVersion { get; set; }
public int KernelVersionRelease { get; set; }
public bool HasHandleTableSize { get; set; }
public int HandleTableSize { get; set; }
public bool HasDebugFlags { get; set; }
public bool AllowDebug { get; set; }
public bool ForceDebug { get; set; }
}
}

77
LibHac/Npdm/Npdm.cs Normal file
View file

@ -0,0 +1,77 @@
using System;
using System.IO;
using System.Text;
namespace LibHac
{
//https://github.com/SciresM/hactool/blob/master/npdm.c
//https://github.com/SciresM/hactool/blob/master/npdm.h
//http://switchbrew.org/index.php?title=NPDM
class Npdm
{
public string Magic;
public bool Is64Bits { get; private set; }
public int AddressSpaceWidth { get; private set; }
public byte MainThreadPriority { get; private set; }
public byte DefaultCpuId { get; private set; }
public int SystemResourceSize { get; private set; }
public int ProcessCategory { get; private set; }
public int MainEntrypointStackSize { get; private set; }
public string TitleName { get; private set; }
public byte[] ProductCode { get; private set; }
public ACI0 ACI0 { get; private set; }
public ACID ACID { get; private set; }
public Npdm(Stream Stream)
{
BinaryReader Reader = new BinaryReader(Stream);
Magic = Reader.ReadAscii(0x4);
if (Magic != "META")
{
throw new Exception("NPDM Stream doesn't contain NPDM file!");
}
Reader.ReadInt64();
//MmuFlags, bit0: 64-bit instructions, bits1-3: address space width (1=64-bit, 2=32-bit). Needs to be <= 0xF.
byte MmuFlags = Reader.ReadByte();
Is64Bits = (MmuFlags & 1) != 0;
AddressSpaceWidth = (MmuFlags >> 1) & 7;
Reader.ReadByte();
MainThreadPriority = Reader.ReadByte(); //(0-63).
DefaultCpuId = Reader.ReadByte();
Reader.ReadInt32();
//System resource size (max size as of 5.x: 534773760).
SystemResourceSize = Util.Swap32(Reader.ReadInt32());
//ProcessCategory (0: regular title, 1: kernel built-in). Should be 0 here.
ProcessCategory = Util.Swap32(Reader.ReadInt32());
//Main entrypoint stack size.
MainEntrypointStackSize = Reader.ReadInt32();
TitleName = Reader.ReadUtf8(0x10).Trim('\0');
ProductCode = Reader.ReadBytes(0x10);
Stream.Seek(0x30, SeekOrigin.Current);
int ACI0Offset = Reader.ReadInt32();
int ACI0Size = Reader.ReadInt32();
int ACIDOffset = Reader.ReadInt32();
int ACIDSize = Reader.ReadInt32();
ACI0 = new ACI0(Stream, ACI0Offset);
ACID = new ACID(Stream, ACIDOffset);
}
}
}

View file

@ -0,0 +1,37 @@
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
namespace LibHac
{
class ServiceAccessControl
{
public Dictionary<string, bool> Services { get; private set; } = new Dictionary<string, bool>();
public ServiceAccessControl(Stream Stream, int Offset, int Size)
{
Stream.Seek(Offset, SeekOrigin.Begin);
BinaryReader Reader = new BinaryReader(Stream);
int ByteReaded = 0;
while (ByteReaded != Size)
{
byte ControlByte = Reader.ReadByte();
if (ControlByte == 0)
{
break;
}
int Length = ((ControlByte & 0x07)) + 1;
bool RegisterAllowed = ((ControlByte & 0x80) != 0);
Services.Add(Reader.ReadAscii(Length), RegisterAllowed);
ByteReaded += Length + 1;
}
}
}
}

134
LibHac/Npdm/SvcName.cs Normal file
View file

@ -0,0 +1,134 @@
namespace LibHac
{
enum SvcName
{
Reserved0,
SetHeapSize,
SetMemoryPermission,
SetMemoryAttribute,
MapMemory,
UnmapMemory,
QueryMemory,
ExitProcess,
CreateThread,
StartThread,
ExitThread,
SleepThread,
GetThreadPriority,
SetThreadPriority,
GetThreadCoreMask,
SetThreadCoreMask,
GetCurrentProcessorNumber,
SignalEvent,
ClearEvent,
MapSharedMemory,
UnmapSharedMemory,
CreateTransferMemory,
CloseHandle,
ResetSignal,
WaitSynchronization,
CancelSynchronization,
ArbitrateLock,
ArbitrateUnlock,
WaitProcessWideKeyAtomic,
SignalProcessWideKey,
GetSystemTick,
ConnectToNamedPort,
SendSyncRequestLight,
SendSyncRequest,
SendSyncRequestWithUserBuffer,
SendAsyncRequestWithUserBuffer,
GetProcessId,
GetThreadId,
Break,
OutputDebugString,
ReturnFromException,
GetInfo,
FlushEntireDataCache,
FlushDataCache,
MapPhysicalMemory,
UnmapPhysicalMemory,
GetFutureThreadInfo,
GetLastThreadInfo,
GetResourceLimitLimitValue,
GetResourceLimitCurrentValue,
SetThreadActivity,
GetThreadContext3,
WaitForAddress,
SignalToAddress,
Reserved1,
Reserved2,
Reserved3,
Reserved4,
Reserved5,
Reserved6,
DumpInfo,
DumpInfoNew,
Reserved7,
Reserved8,
CreateSession,
AcceptSession,
ReplyAndReceiveLight,
ReplyAndReceive,
ReplyAndReceiveWithUserBuffer,
CreateEvent,
Reserved9,
Reserved10,
MapPhysicalMemoryUnsafe,
UnmapPhysicalMemoryUnsafe,
SetUnsafeLimit,
CreateCodeMemory,
ControlCodeMemory,
SleepSystem,
ReadWriteRegister,
SetProcessActivity,
CreateSharedMemory,
MapTransferMemory,
UnmapTransferMemory,
CreateInterruptEvent,
QueryPhysicalAddress,
QueryIoMapping,
CreateDeviceAddressSpace,
AttachDeviceAddressSpace,
DetachDeviceAddressSpace,
MapDeviceAddressSpaceByForce,
MapDeviceAddressSpaceAligned,
MapDeviceAddressSpace,
UnmapDeviceAddressSpace,
InvalidateProcessDataCache,
StoreProcessDataCache,
FlushProcessDataCache,
DebugActiveProcess,
BreakDebugProcess,
TerminateDebugProcess,
GetDebugEvent,
ContinueDebugEvent,
GetProcessList,
GetThreadList,
GetDebugThreadContext,
SetDebugThreadContext,
QueryDebugProcessMemory,
ReadDebugProcessMemory,
WriteDebugProcessMemory,
SetHardwareBreakPoint,
GetDebugThreadParam,
Reserved11,
GetSystemInfo,
CreatePort,
ManageNamedPort,
ConnectToPort,
SetProcessMemoryPermission,
MapProcessMemory,
UnmapProcessMemory,
QueryProcessMemory,
MapProcessCodeMemory,
UnmapProcessCodeMemory,
CreateProcess,
StartProcess,
TerminateProcess,
GetProcessInfo,
CreateResourceLimit,
SetResourceLimitLimitValue,
CallSecureMonitor
}
}

View file

@ -409,6 +409,16 @@ namespace LibHac
bool isOutOfRange = startIndex < 0 || startIndex > length || subLength < 0 || startIndex > length - subLength;
return !isOutOfRange;
}
public static int Swap32(int Value)
{
uint UintVal = (uint)Value;
return (int)(((UintVal >> 24) & 0x000000ff) |
((UintVal >> 8) & 0x0000ff00) |
((UintVal << 8) & 0x00ff0000) |
((UintVal << 24) & 0xff000000));
}
}
public class ByteArray128BitComparer : EqualityComparer<byte[]>