Move Npdm, LayeredFileSystem

This commit is contained in:
Alex Barney 2021-12-19 01:42:39 -07:00
parent a289059ecf
commit 14fcdc9d67
18 changed files with 201 additions and 197 deletions

View file

@ -1,172 +0,0 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
namespace LibHac.Npdm;
public class KernelAccessControl
{
public List<KernelAccessControlItem> Items { get; }
public KernelAccessControl(Stream stream, int offset, int size)
{
stream.Seek(offset, SeekOrigin.Begin);
var reader = new BinaryReader(stream);
var 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;
}
}
}
Items = items.ToList();
}
}

View file

@ -5,7 +5,7 @@ using LibHac.Fs;
using LibHac.Fs.Fsa;
using LibHac.Util;
namespace LibHac.FsSystem;
namespace LibHac.Tools.FsSystem;
public class LayeredFileSystem : IFileSystem
{

View file

@ -1,9 +1,10 @@
// ReSharper disable UnusedVariable
using System;
using System.IO;
using LibHac.Common;
namespace LibHac.Npdm;
namespace LibHac.Tools.Npdm;
public class Aci0
{

View file

@ -1,11 +1,12 @@
// ReSharper disable UnusedVariable
using System;
using System.IO;
using LibHac.Common;
using LibHac.Common.Keys;
using LibHac.Tools.Crypto;
namespace LibHac.Npdm;
namespace LibHac.Tools.Npdm;
public class Acid
{

View file

@ -1,8 +1,8 @@
namespace LibHac.Npdm;
namespace LibHac.Tools.Npdm;
internal enum ApplicationType
{
SystemModule,
Application,
Applet
}
}

View file

@ -1,6 +1,6 @@
using System.IO;
namespace LibHac.Npdm;
namespace LibHac.Tools.Npdm;
public class FsAccessControl
{
@ -24,4 +24,4 @@ public class FsAccessControl
Unknown3 = reader.ReadInt32();
Unknown4 = reader.ReadInt32();
}
}
}

View file

@ -1,8 +1,9 @@
// ReSharper disable UnusedVariable
using System;
using System.IO;
namespace LibHac.Npdm;
namespace LibHac.Tools.Npdm;
public class FsAccessHeader
{
@ -33,4 +34,4 @@ public class FsAccessHeader
throw new NotImplementedException("ContentOwnerId section is not implemented!");
}
}
}
}

View file

@ -1,5 +1,5 @@
// ReSharper disable InconsistentNaming
namespace LibHac.Npdm;
namespace LibHac.Tools.Npdm;
public enum FsPermissionBool : ulong
{
@ -30,4 +30,4 @@ public enum FsPermissionBool : ulong
Unknown0x18 = 0x8000000010000000,
Unknown0x19 = 0x8000000000000800,
Unknown0x1A = 0x8000000000004020
}
}

View file

@ -1,5 +1,5 @@
// ReSharper disable InconsistentNaming
namespace LibHac.Npdm;
namespace LibHac.Tools.Npdm;
public enum FsPermissionRw : ulong
{
@ -42,4 +42,4 @@ public enum FsPermissionRw : ulong
GameCard_System = 0x8000000000000100,
MountContent_System = 0x8000000000100008,
HostAccess = 0xC000000000400000
}
}

View file

@ -0,0 +1,172 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
namespace LibHac.Tools.Npdm;
public class KernelAccessControl
{
public List<KernelAccessControlItem> Items { get; }
public KernelAccessControl(Stream stream, int offset, int size)
{
stream.Seek(offset, SeekOrigin.Begin);
var reader = new BinaryReader(stream);
var 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;
}
}
}
Items = items.ToList();
}
}

View file

@ -1,4 +1,4 @@
namespace LibHac.Npdm;
namespace LibHac.Tools.Npdm;
public struct KernelAccessControlIrq
{
@ -10,4 +10,4 @@ public struct KernelAccessControlIrq
Irq0 = irq0;
Irq1 = irq1;
}
}
}

View file

@ -1,4 +1,4 @@
namespace LibHac.Npdm;
namespace LibHac.Tools.Npdm;
public struct KernelAccessControlMmio
{
@ -18,4 +18,4 @@ public struct KernelAccessControlMmio
IsRo = isro;
IsNormal = isnormal;
}
}
}

View file

@ -1,6 +1,6 @@
using System.Collections.Generic;
namespace LibHac.Npdm;
namespace LibHac.Tools.Npdm;
public class KernelAccessControlItem
{
@ -36,4 +36,4 @@ public class KernelAccessControlItem
PageMmio = new List<KernelAccessControlMmio>();
Irq = new List<KernelAccessControlIrq>();
}
}
}

View file

@ -1,11 +1,12 @@
// ReSharper disable UnusedVariable
using System;
using System.Buffers.Binary;
using System.IO;
using LibHac.Common;
using LibHac.Common.Keys;
namespace LibHac.Npdm;
namespace LibHac.Tools.Npdm;
//https://github.com/Ryujinx/Ryujinx/blob/master/Ryujinx.HLE/Loaders/Npdm/Npdm.cs
//https://github.com/SciresM/hactool/blob/master/npdm.c

View file

@ -3,7 +3,7 @@ using System.Collections.Generic;
using System.IO;
using LibHac.Common;
namespace LibHac.Npdm;
namespace LibHac.Tools.Npdm;
public class ServiceAccessControl
{

View file

@ -1,4 +1,4 @@
namespace LibHac.Npdm;
namespace LibHac.Tools.Npdm;
public enum SvcName
{
@ -130,4 +130,4 @@ public enum SvcName
CreateResourceLimit,
SetResourceLimitLimitValue,
CallSecureMonitor
}
}

View file

@ -6,10 +6,10 @@ using LibHac.Fs;
using LibHac.Fs.Fsa;
using LibHac.Fs.Impl;
using LibHac.FsSystem;
using LibHac.Npdm;
using LibHac.Tools.Fs;
using LibHac.Tools.FsSystem;
using LibHac.Tools.FsSystem.NcaUtils;
using LibHac.Tools.Npdm;
using static hactoolnet.Print;
namespace hactoolnet;

View file

@ -2,8 +2,8 @@
using LibHac.Common;
using LibHac.Fs;
using LibHac.Fs.Fsa;
using LibHac.FsSystem;
using LibHac.Tools.Fs;
using LibHac.Tools.FsSystem;
using LibHac.Util;
using Xunit;