Npdm.cs 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. using Ryujinx.HLE.OsHle.Utilities;
  2. using System.IO;
  3. using System.Text;
  4. namespace Ryujinx.HLE.Loaders.Npdm
  5. {
  6. //https://github.com/SciresM/hactool/blob/master/npdm.c
  7. //https://github.com/SciresM/hactool/blob/master/npdm.h
  8. //http://switchbrew.org/index.php?title=NPDM
  9. class Npdm
  10. {
  11. public bool Is64Bits;
  12. public int AddressSpaceWidth;
  13. public byte MainThreadPriority;
  14. public byte DefaultCpuId;
  15. public int SystemResourceSize;
  16. public int ProcessCategory;
  17. public int MainEntrypointStackSize;
  18. public string TitleName;
  19. public byte[] ProductCode;
  20. public ulong FSPerms;
  21. private int ACI0Offset;
  22. private int ACI0Size;
  23. private int ACIDOffset;
  24. private int ACIDSize;
  25. public ACI0 ACI0;
  26. public ACID ACID;
  27. public const long NpdmMagic = 'M' << 0 | 'E' << 8 | 'T' << 16 | 'A' << 24;
  28. public Npdm(Stream NPDMStream)
  29. {
  30. BinaryReader Reader = new BinaryReader(NPDMStream);
  31. if (Reader.ReadInt32() != NpdmMagic)
  32. {
  33. throw new InvalidNpdmException("NPDM Stream doesn't contain NPDM file!");
  34. }
  35. Reader.ReadInt64(); // Padding / Unused
  36. // MmuFlags, bit0: 64-bit instructions, bits1-3: address space width (1=64-bit, 2=32-bit). Needs to be <= 0xF
  37. byte MmuFlags = Reader.ReadByte();
  38. Is64Bits = (MmuFlags & 1) != 0;
  39. AddressSpaceWidth = (MmuFlags >> 1) & 7;
  40. Reader.ReadByte(); // Padding / Unused
  41. MainThreadPriority = Reader.ReadByte(); // (0-63)
  42. DefaultCpuId = Reader.ReadByte();
  43. Reader.ReadInt32(); // Padding / Unused
  44. // System resource size (max size as of 5.x: 534773760). Unknown usage.
  45. SystemResourceSize = EndianSwap.Swap32(Reader.ReadInt32());
  46. // ProcessCategory (0: regular title, 1: kernel built-in). Should be 0 here.
  47. ProcessCategory = EndianSwap.Swap32(Reader.ReadInt32());
  48. // Main entrypoint stack size
  49. // (Should(?) be page-aligned. In non-nspwn scenarios, values of 0 can also rarely break in Horizon.
  50. // This might be something auto-adapting or a security feature of some sort ?)
  51. MainEntrypointStackSize = Reader.ReadInt32();
  52. byte[] TempTitleName = Reader.ReadBytes(0x10);
  53. TitleName = Encoding.UTF8.GetString(TempTitleName, 0, TempTitleName.Length).Trim('\0');
  54. ProductCode = Reader.ReadBytes(0x10); // Unknown value
  55. NPDMStream.Seek(0x30, SeekOrigin.Current); // Skip reserved bytes
  56. ACI0Offset = Reader.ReadInt32();
  57. ACI0Size = Reader.ReadInt32();
  58. ACIDOffset = Reader.ReadInt32();
  59. ACIDSize = Reader.ReadInt32();
  60. ACI0 = new ACI0(NPDMStream, ACI0Offset);
  61. ACID = new ACID(NPDMStream, ACIDOffset);
  62. FSPerms = ACI0.FSAccessHeader.PermissionsBitmask & ACID.FSAccessControl.PermissionsBitmask;
  63. }
  64. }
  65. }