Npdm.cs 3.2 KB

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