Npdm.cs 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. using Ryujinx.HLE.Exceptions;
  2. using Ryujinx.HLE.Utilities;
  3. using System.IO;
  4. using System.Text;
  5. namespace Ryujinx.HLE.Loaders.Npdm
  6. {
  7. //https://github.com/SciresM/hactool/blob/master/npdm.c
  8. //https://github.com/SciresM/hactool/blob/master/npdm.h
  9. //http://switchbrew.org/index.php?title=NPDM
  10. class Npdm
  11. {
  12. private const int MetaMagic = 'M' << 0 | 'E' << 8 | 'T' << 16 | 'A' << 24;
  13. public bool Is64Bits { get; private set; }
  14. public int AddressSpaceWidth { get; private set; }
  15. public byte MainThreadPriority { get; private set; }
  16. public byte DefaultCpuId { get; private set; }
  17. public int SystemResourceSize { get; private set; }
  18. public int ProcessCategory { get; private set; }
  19. public int MainEntrypointStackSize { get; private set; }
  20. public string TitleName { get; private set; }
  21. public byte[] ProductCode { get; private set; }
  22. public ACI0 ACI0 { get; private set; }
  23. public ACID ACID { get; private set; }
  24. public Npdm(Stream Stream)
  25. {
  26. BinaryReader Reader = new BinaryReader(Stream);
  27. if (Reader.ReadInt32() != MetaMagic)
  28. {
  29. throw new InvalidNpdmException("NPDM Stream doesn't contain NPDM file!");
  30. }
  31. Reader.ReadInt64();
  32. //MmuFlags, bit0: 64-bit instructions, bits1-3: address space width (1=64-bit, 2=32-bit). Needs to be <= 0xF.
  33. byte MmuFlags = Reader.ReadByte();
  34. Is64Bits = (MmuFlags & 1) != 0;
  35. AddressSpaceWidth = (MmuFlags >> 1) & 7;
  36. Reader.ReadByte();
  37. MainThreadPriority = Reader.ReadByte(); //(0-63).
  38. DefaultCpuId = Reader.ReadByte();
  39. Reader.ReadInt32();
  40. //System resource size (max size as of 5.x: 534773760).
  41. SystemResourceSize = EndianSwap.Swap32(Reader.ReadInt32());
  42. //ProcessCategory (0: regular title, 1: kernel built-in). Should be 0 here.
  43. ProcessCategory = EndianSwap.Swap32(Reader.ReadInt32());
  44. //Main entrypoint stack size.
  45. MainEntrypointStackSize = Reader.ReadInt32();
  46. byte[] TempTitleName = Reader.ReadBytes(0x10);
  47. TitleName = Encoding.UTF8.GetString(TempTitleName, 0, TempTitleName.Length).Trim('\0');
  48. ProductCode = Reader.ReadBytes(0x10);
  49. Stream.Seek(0x30, SeekOrigin.Current);
  50. int ACI0Offset = Reader.ReadInt32();
  51. int ACI0Size = Reader.ReadInt32();
  52. int ACIDOffset = Reader.ReadInt32();
  53. int ACIDSize = Reader.ReadInt32();
  54. ACI0 = new ACI0(Stream, ACI0Offset);
  55. ACID = new ACID(Stream, ACIDOffset);
  56. }
  57. }
  58. }