From 14fcdc9d6774619ced76af2a47eb64cfa17276b7 Mon Sep 17 00:00:00 2001 From: Alex Barney Date: Sun, 19 Dec 2021 01:42:39 -0700 Subject: [PATCH] Move Npdm, LayeredFileSystem --- src/LibHac/Npdm/KernelAccessControl.cs | 172 ------------------ .../{ => Tools}/FsSystem/LayeredFileSystem.cs | 2 +- src/LibHac/{ => Tools}/Npdm/Aci0.cs | 3 +- src/LibHac/{ => Tools}/Npdm/Acid.cs | 3 +- .../{ => Tools}/Npdm/ApplicationType.cs | 4 +- .../Npdm/FsAccessControl.cs} | 4 +- .../Npdm/FsAccessHeader.cs} | 5 +- .../{ => Tools}/Npdm/FsPermissionBool.cs | 4 +- src/LibHac/{ => Tools}/Npdm/FsPermissionRw.cs | 4 +- src/LibHac/Tools/Npdm/KernelAccessControl.cs | 172 ++++++++++++++++++ .../Npdm/KernelAccessControlIrq.cs | 4 +- .../Npdm/KernelAccessControlMmio.cs | 4 +- .../{ => Tools}/Npdm/KernelAccessItem.cs | 4 +- src/LibHac/{ => Tools}/Npdm/NpdmBinary.cs | 3 +- .../{ => Tools}/Npdm/ServiceAccessControl.cs | 2 +- src/LibHac/{ => Tools}/Npdm/SvcName.cs | 4 +- src/hactoolnet/ProcessNca.cs | 2 +- .../LibHac.Tests/Fs/LayeredFileSystemTests.cs | 2 +- 18 files changed, 201 insertions(+), 197 deletions(-) delete mode 100644 src/LibHac/Npdm/KernelAccessControl.cs rename src/LibHac/{ => Tools}/FsSystem/LayeredFileSystem.cs (99%) rename src/LibHac/{ => Tools}/Npdm/Aci0.cs (98%) rename src/LibHac/{ => Tools}/Npdm/Acid.cs (98%) rename src/LibHac/{ => Tools}/Npdm/ApplicationType.cs (72%) rename src/LibHac/{Npdm/FSAccessControl.cs => Tools/Npdm/FsAccessControl.cs} (95%) rename src/LibHac/{Npdm/FSAccessHeader.cs => Tools/Npdm/FsAccessHeader.cs} (96%) rename src/LibHac/{ => Tools}/Npdm/FsPermissionBool.cs (97%) rename src/LibHac/{ => Tools}/Npdm/FsPermissionRw.cs (98%) create mode 100644 src/LibHac/Tools/Npdm/KernelAccessControl.cs rename src/LibHac/{ => Tools}/Npdm/KernelAccessControlIrq.cs (87%) rename src/LibHac/{ => Tools}/Npdm/KernelAccessControlMmio.cs (93%) rename src/LibHac/{ => Tools}/Npdm/KernelAccessItem.cs (97%) rename src/LibHac/{ => Tools}/Npdm/NpdmBinary.cs (98%) rename src/LibHac/{ => Tools}/Npdm/ServiceAccessControl.cs (96%) rename src/LibHac/{ => Tools}/Npdm/SvcName.cs (98%) diff --git a/src/LibHac/Npdm/KernelAccessControl.cs b/src/LibHac/Npdm/KernelAccessControl.cs deleted file mode 100644 index 7fb6f38c..00000000 --- a/src/LibHac/Npdm/KernelAccessControl.cs +++ /dev/null @@ -1,172 +0,0 @@ -using System; -using System.Collections.Generic; -using System.IO; -using System.Linq; - -namespace LibHac.Npdm; - -public class KernelAccessControl -{ - public List 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(); - } -} diff --git a/src/LibHac/FsSystem/LayeredFileSystem.cs b/src/LibHac/Tools/FsSystem/LayeredFileSystem.cs similarity index 99% rename from src/LibHac/FsSystem/LayeredFileSystem.cs rename to src/LibHac/Tools/FsSystem/LayeredFileSystem.cs index 9070d4fb..4598b443 100644 --- a/src/LibHac/FsSystem/LayeredFileSystem.cs +++ b/src/LibHac/Tools/FsSystem/LayeredFileSystem.cs @@ -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 { diff --git a/src/LibHac/Npdm/Aci0.cs b/src/LibHac/Tools/Npdm/Aci0.cs similarity index 98% rename from src/LibHac/Npdm/Aci0.cs rename to src/LibHac/Tools/Npdm/Aci0.cs index f46d150b..08ae9fec 100644 --- a/src/LibHac/Npdm/Aci0.cs +++ b/src/LibHac/Tools/Npdm/Aci0.cs @@ -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 { diff --git a/src/LibHac/Npdm/Acid.cs b/src/LibHac/Tools/Npdm/Acid.cs similarity index 98% rename from src/LibHac/Npdm/Acid.cs rename to src/LibHac/Tools/Npdm/Acid.cs index 20f27155..2595781d 100644 --- a/src/LibHac/Npdm/Acid.cs +++ b/src/LibHac/Tools/Npdm/Acid.cs @@ -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 { diff --git a/src/LibHac/Npdm/ApplicationType.cs b/src/LibHac/Tools/Npdm/ApplicationType.cs similarity index 72% rename from src/LibHac/Npdm/ApplicationType.cs rename to src/LibHac/Tools/Npdm/ApplicationType.cs index 03f9fa3f..d01d7fe1 100644 --- a/src/LibHac/Npdm/ApplicationType.cs +++ b/src/LibHac/Tools/Npdm/ApplicationType.cs @@ -1,8 +1,8 @@ -namespace LibHac.Npdm; +namespace LibHac.Tools.Npdm; internal enum ApplicationType { SystemModule, Application, Applet -} +} \ No newline at end of file diff --git a/src/LibHac/Npdm/FSAccessControl.cs b/src/LibHac/Tools/Npdm/FsAccessControl.cs similarity index 95% rename from src/LibHac/Npdm/FSAccessControl.cs rename to src/LibHac/Tools/Npdm/FsAccessControl.cs index 70cdb070..de53432d 100644 --- a/src/LibHac/Npdm/FSAccessControl.cs +++ b/src/LibHac/Tools/Npdm/FsAccessControl.cs @@ -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(); } -} +} \ No newline at end of file diff --git a/src/LibHac/Npdm/FSAccessHeader.cs b/src/LibHac/Tools/Npdm/FsAccessHeader.cs similarity index 96% rename from src/LibHac/Npdm/FSAccessHeader.cs rename to src/LibHac/Tools/Npdm/FsAccessHeader.cs index 101a7f95..d9e5faaf 100644 --- a/src/LibHac/Npdm/FSAccessHeader.cs +++ b/src/LibHac/Tools/Npdm/FsAccessHeader.cs @@ -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!"); } } -} +} \ No newline at end of file diff --git a/src/LibHac/Npdm/FsPermissionBool.cs b/src/LibHac/Tools/Npdm/FsPermissionBool.cs similarity index 97% rename from src/LibHac/Npdm/FsPermissionBool.cs rename to src/LibHac/Tools/Npdm/FsPermissionBool.cs index e67c2526..5de2adae 100644 --- a/src/LibHac/Npdm/FsPermissionBool.cs +++ b/src/LibHac/Tools/Npdm/FsPermissionBool.cs @@ -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 -} +} \ No newline at end of file diff --git a/src/LibHac/Npdm/FsPermissionRw.cs b/src/LibHac/Tools/Npdm/FsPermissionRw.cs similarity index 98% rename from src/LibHac/Npdm/FsPermissionRw.cs rename to src/LibHac/Tools/Npdm/FsPermissionRw.cs index d8122899..3369532c 100644 --- a/src/LibHac/Npdm/FsPermissionRw.cs +++ b/src/LibHac/Tools/Npdm/FsPermissionRw.cs @@ -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 -} +} \ No newline at end of file diff --git a/src/LibHac/Tools/Npdm/KernelAccessControl.cs b/src/LibHac/Tools/Npdm/KernelAccessControl.cs new file mode 100644 index 00000000..da9b5585 --- /dev/null +++ b/src/LibHac/Tools/Npdm/KernelAccessControl.cs @@ -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 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(); + } +} \ No newline at end of file diff --git a/src/LibHac/Npdm/KernelAccessControlIrq.cs b/src/LibHac/Tools/Npdm/KernelAccessControlIrq.cs similarity index 87% rename from src/LibHac/Npdm/KernelAccessControlIrq.cs rename to src/LibHac/Tools/Npdm/KernelAccessControlIrq.cs index 91c437f7..e5ba2d53 100644 --- a/src/LibHac/Npdm/KernelAccessControlIrq.cs +++ b/src/LibHac/Tools/Npdm/KernelAccessControlIrq.cs @@ -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; } -} +} \ No newline at end of file diff --git a/src/LibHac/Npdm/KernelAccessControlMmio.cs b/src/LibHac/Tools/Npdm/KernelAccessControlMmio.cs similarity index 93% rename from src/LibHac/Npdm/KernelAccessControlMmio.cs rename to src/LibHac/Tools/Npdm/KernelAccessControlMmio.cs index a0071e94..7541b14f 100644 --- a/src/LibHac/Npdm/KernelAccessControlMmio.cs +++ b/src/LibHac/Tools/Npdm/KernelAccessControlMmio.cs @@ -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; } -} +} \ No newline at end of file diff --git a/src/LibHac/Npdm/KernelAccessItem.cs b/src/LibHac/Tools/Npdm/KernelAccessItem.cs similarity index 97% rename from src/LibHac/Npdm/KernelAccessItem.cs rename to src/LibHac/Tools/Npdm/KernelAccessItem.cs index 1600349b..62f26650 100644 --- a/src/LibHac/Npdm/KernelAccessItem.cs +++ b/src/LibHac/Tools/Npdm/KernelAccessItem.cs @@ -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(); Irq = new List(); } -} +} \ No newline at end of file diff --git a/src/LibHac/Npdm/NpdmBinary.cs b/src/LibHac/Tools/Npdm/NpdmBinary.cs similarity index 98% rename from src/LibHac/Npdm/NpdmBinary.cs rename to src/LibHac/Tools/Npdm/NpdmBinary.cs index 9b3d6739..ac9f3c10 100644 --- a/src/LibHac/Npdm/NpdmBinary.cs +++ b/src/LibHac/Tools/Npdm/NpdmBinary.cs @@ -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 diff --git a/src/LibHac/Npdm/ServiceAccessControl.cs b/src/LibHac/Tools/Npdm/ServiceAccessControl.cs similarity index 96% rename from src/LibHac/Npdm/ServiceAccessControl.cs rename to src/LibHac/Tools/Npdm/ServiceAccessControl.cs index d7c65b13..e3c68902 100644 --- a/src/LibHac/Npdm/ServiceAccessControl.cs +++ b/src/LibHac/Tools/Npdm/ServiceAccessControl.cs @@ -3,7 +3,7 @@ using System.Collections.Generic; using System.IO; using LibHac.Common; -namespace LibHac.Npdm; +namespace LibHac.Tools.Npdm; public class ServiceAccessControl { diff --git a/src/LibHac/Npdm/SvcName.cs b/src/LibHac/Tools/Npdm/SvcName.cs similarity index 98% rename from src/LibHac/Npdm/SvcName.cs rename to src/LibHac/Tools/Npdm/SvcName.cs index bd363757..7368eb7c 100644 --- a/src/LibHac/Npdm/SvcName.cs +++ b/src/LibHac/Tools/Npdm/SvcName.cs @@ -1,4 +1,4 @@ -namespace LibHac.Npdm; +namespace LibHac.Tools.Npdm; public enum SvcName { @@ -130,4 +130,4 @@ public enum SvcName CreateResourceLimit, SetResourceLimitLimitValue, CallSecureMonitor -} +} \ No newline at end of file diff --git a/src/hactoolnet/ProcessNca.cs b/src/hactoolnet/ProcessNca.cs index 4e609a4d..bb834b95 100644 --- a/src/hactoolnet/ProcessNca.cs +++ b/src/hactoolnet/ProcessNca.cs @@ -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; diff --git a/tests/LibHac.Tests/Fs/LayeredFileSystemTests.cs b/tests/LibHac.Tests/Fs/LayeredFileSystemTests.cs index 0ae66103..04877f4b 100644 --- a/tests/LibHac.Tests/Fs/LayeredFileSystemTests.cs +++ b/tests/LibHac.Tests/Fs/LayeredFileSystemTests.cs @@ -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;