IRoInterface.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457
  1. using Ryujinx.HLE.HOS.Ipc;
  2. using Ryujinx.HLE.Loaders.Executables;
  3. using Ryujinx.HLE.Utilities;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.IO;
  7. using System.Linq;
  8. using System.Runtime.InteropServices;
  9. using System.Security.Cryptography;
  10. using static Ryujinx.HLE.HOS.ErrorCode;
  11. namespace Ryujinx.HLE.HOS.Services.Ldr
  12. {
  13. [StructLayout(LayoutKind.Explicit, Size = 0x350)]
  14. unsafe struct NrrHeader
  15. {
  16. [FieldOffset(0)]
  17. public uint Magic;
  18. [FieldOffset(0x10)]
  19. public ulong TitleIdMask;
  20. [FieldOffset(0x18)]
  21. public ulong TitleIdPattern;
  22. [FieldOffset(0x30)]
  23. public fixed byte Modulus[0x100];
  24. [FieldOffset(0x130)]
  25. public fixed byte FixedKeySignature[0x100];
  26. [FieldOffset(0x230)]
  27. public fixed byte NrrSignature[0x100];
  28. [FieldOffset(0x330)]
  29. public ulong TitleIdMin;
  30. [FieldOffset(0x338)]
  31. public uint NrrSize;
  32. [FieldOffset(0x340)]
  33. public uint HashOffset;
  34. [FieldOffset(0x344)]
  35. public uint HashCount;
  36. }
  37. class NrrInfo
  38. {
  39. public NrrHeader Header { get; private set; }
  40. public List<byte[]> Hashes { get; private set; }
  41. public long NrrAddress { get; private set; }
  42. public NrrInfo(long NrrAddress, NrrHeader Header, List<byte[]> Hashes)
  43. {
  44. this.NrrAddress = NrrAddress;
  45. this.Header = Header;
  46. this.Hashes = Hashes;
  47. }
  48. }
  49. class NroInfo
  50. {
  51. public Nro Executable { get; private set; }
  52. public byte[] Hash { get; private set; }
  53. public long NroAddress { get; private set; }
  54. public long TotalSize { get; private set; }
  55. public long NroMappedAddress { get; set; }
  56. public NroInfo(Nro Executable, byte[] Hash, long TotalSize)
  57. {
  58. this.Executable = Executable;
  59. this.Hash = Hash;
  60. this.TotalSize = TotalSize;
  61. }
  62. }
  63. class IRoInterface : IpcService
  64. {
  65. private Dictionary<int, ServiceProcessRequest> m_Commands;
  66. public override IReadOnlyDictionary<int, ServiceProcessRequest> Commands => m_Commands;
  67. private const int MaxNrr = 0x40;
  68. private const int MaxNro = 0x40;
  69. private const uint NrrMagic = 0x3052524E;
  70. private const uint NroMagic = 0x304F524E;
  71. private List<NrrInfo> NrrInfos;
  72. private List<NroInfo> NroInfos;
  73. private bool IsInitialized;
  74. public IRoInterface()
  75. {
  76. m_Commands = new Dictionary<int, ServiceProcessRequest>()
  77. {
  78. { 0, LoadNro },
  79. { 1, UnloadNro },
  80. { 2, LoadNrr },
  81. { 3, UnloadNrr },
  82. { 4, Initialize },
  83. };
  84. NrrInfos = new List<NrrInfo>(MaxNrr);
  85. NroInfos = new List<NroInfo>(MaxNro);
  86. }
  87. private long ParseNrr(out NrrInfo NrrInfo, ServiceCtx Context, long NrrAddress, long NrrSize)
  88. {
  89. NrrInfo = null;
  90. if (NrrSize == 0 || NrrAddress + NrrSize <= NrrAddress || (NrrSize & 0xFFF) != 0)
  91. {
  92. return MakeError(ErrorModule.Loader, LoaderErr.BadSize);
  93. }
  94. else if ((NrrAddress & 0xFFF) != 0)
  95. {
  96. return MakeError(ErrorModule.Loader, LoaderErr.UnalignedAddress);
  97. }
  98. StructReader Reader = new StructReader(Context.Memory, NrrAddress);
  99. NrrHeader Header = Reader.Read<NrrHeader>();
  100. if (Header.Magic != NrrMagic)
  101. {
  102. return MakeError(ErrorModule.Loader, LoaderErr.InvalidNrr);
  103. }
  104. else if (Header.NrrSize != NrrSize)
  105. {
  106. return MakeError(ErrorModule.Loader, LoaderErr.BadSize);
  107. }
  108. List<byte[]> Hashes = new List<byte[]>();
  109. for (int i = 0; i < Header.HashCount; i++)
  110. {
  111. Hashes.Add(Context.Memory.ReadBytes(NrrAddress + Header.HashOffset + (i * 0x20), 0x20));
  112. }
  113. NrrInfo = new NrrInfo(NrrAddress, Header, Hashes);
  114. return 0;
  115. }
  116. public bool IsNroHashPresent(byte[] NroHash)
  117. {
  118. foreach (NrrInfo Info in NrrInfos)
  119. {
  120. foreach (byte[] Hash in Info.Hashes)
  121. {
  122. if (Hash.SequenceEqual(NroHash))
  123. {
  124. return true;
  125. }
  126. }
  127. }
  128. return false;
  129. }
  130. public bool IsNroLoaded(byte[] NroHash)
  131. {
  132. foreach (NroInfo Info in NroInfos)
  133. {
  134. if (Info.Hash.SequenceEqual(NroHash))
  135. {
  136. return true;
  137. }
  138. }
  139. return false;
  140. }
  141. public long ParseNro(out NroInfo Res, ServiceCtx Context, long NroHeapAddress, long NroSize, long BssHeapAddress, long BssSize)
  142. {
  143. Res = null;
  144. if (NroInfos.Count >= MaxNro)
  145. {
  146. return MakeError(ErrorModule.Loader, LoaderErr.MaxNro);
  147. }
  148. else if (NroSize == 0 || NroHeapAddress + NroSize <= NroHeapAddress || (NroSize & 0xFFF) != 0)
  149. {
  150. return MakeError(ErrorModule.Loader, LoaderErr.BadSize);
  151. }
  152. else if (BssSize != 0 && (BssHeapAddress + BssSize) <= BssHeapAddress)
  153. {
  154. return MakeError(ErrorModule.Loader, LoaderErr.BadSize);
  155. }
  156. else if ((NroHeapAddress & 0xFFF) != 0)
  157. {
  158. return MakeError(ErrorModule.Loader, LoaderErr.UnalignedAddress);
  159. }
  160. uint Magic = Context.Memory.ReadUInt32(NroHeapAddress + 0x10);
  161. uint NroFileSize = Context.Memory.ReadUInt32(NroHeapAddress + 0x18);
  162. if (Magic != NroMagic || NroSize != NroFileSize)
  163. {
  164. return MakeError(ErrorModule.Loader, LoaderErr.InvalidNro);
  165. }
  166. byte[] NroData = Context.Memory.ReadBytes(NroHeapAddress, NroSize);
  167. byte[] NroHash = null;
  168. MemoryStream Stream = new MemoryStream(NroData);
  169. using (SHA256 Hasher = SHA256.Create())
  170. {
  171. NroHash = Hasher.ComputeHash(Stream);
  172. }
  173. if (!IsNroHashPresent(NroHash))
  174. {
  175. return MakeError(ErrorModule.Loader, LoaderErr.NroHashNotPresent);
  176. }
  177. if (IsNroLoaded(NroHash))
  178. {
  179. return MakeError(ErrorModule.Loader, LoaderErr.NroAlreadyLoaded);
  180. }
  181. Stream.Position = 0;
  182. Nro Executable = new Nro(Stream, "memory", NroHeapAddress, BssHeapAddress);
  183. // check if everything is page align.
  184. if ((Executable.Text.Length & 0xFFF) != 0 || (Executable.RO.Length & 0xFFF) != 0
  185. || (Executable.Data.Length & 0xFFF) != 0 || (Executable.BssSize & 0xFFF) != 0)
  186. {
  187. return MakeError(ErrorModule.Loader, LoaderErr.InvalidNro);
  188. }
  189. // check if everything is contiguous.
  190. if (Executable.ROOffset != Executable.TextOffset + Executable.Text.Length
  191. || Executable.DataOffset != Executable.ROOffset + Executable.RO.Length
  192. || NroFileSize != Executable.DataOffset + Executable.Data.Length)
  193. {
  194. return MakeError(ErrorModule.Loader, LoaderErr.InvalidNro);
  195. }
  196. // finally check the bss size match.
  197. if (Executable.BssSize != BssSize)
  198. {
  199. return MakeError(ErrorModule.Loader, LoaderErr.InvalidNro);
  200. }
  201. Res = new NroInfo(Executable, NroHash, Executable.Text.Length + Executable.RO.Length + Executable.Data.Length + Executable.BssSize);
  202. return 0;
  203. }
  204. private long MapNro(ServiceCtx Context, NroInfo Info, out long NroMappedAddress)
  205. {
  206. NroMappedAddress = 0;
  207. long TargetAddress = Context.Process.MemoryManager.AddrSpaceStart;
  208. long HeapRegionStart = Context.Process.MemoryManager.HeapRegionStart;
  209. long HeapRegionEnd = Context.Process.MemoryManager.HeapRegionEnd;
  210. long MapRegionStart = Context.Process.MemoryManager.MapRegionStart;
  211. long MapRegionEnd = Context.Process.MemoryManager.MapRegionEnd;
  212. while (true)
  213. {
  214. if (TargetAddress + Info.TotalSize >= Context.Process.MemoryManager.AddrSpaceEnd)
  215. {
  216. return MakeError(ErrorModule.Loader, LoaderErr.InvalidMemoryState);
  217. }
  218. bool IsValidAddress = !(HeapRegionStart > 0 && HeapRegionStart <= TargetAddress + Info.TotalSize - 1
  219. && TargetAddress <= HeapRegionEnd - 1)
  220. && !(MapRegionStart > 0
  221. && MapRegionStart <= TargetAddress + Info.TotalSize - 1
  222. && TargetAddress <= MapRegionEnd - 1);
  223. if (IsValidAddress && Context.Process.MemoryManager.HleIsUnmapped(TargetAddress, Info.TotalSize))
  224. {
  225. break;
  226. }
  227. TargetAddress += 0x1000;
  228. }
  229. Context.Process.LoadProgram(Info.Executable, TargetAddress);
  230. Info.NroMappedAddress = TargetAddress;
  231. NroMappedAddress = TargetAddress;
  232. return 0;
  233. }
  234. private long RemoveNrrInfo(long NrrAddress)
  235. {
  236. foreach (NrrInfo Info in NrrInfos)
  237. {
  238. if (Info.NrrAddress == NrrAddress)
  239. {
  240. NrrInfos.Remove(Info);
  241. return 0;
  242. }
  243. }
  244. return MakeError(ErrorModule.Loader, LoaderErr.BadNrrAddress);
  245. }
  246. private long RemoveNroInfo(ServiceCtx Context, long NroMappedAddress, long NroHeapAddress)
  247. {
  248. foreach (NroInfo Info in NroInfos)
  249. {
  250. if (Info.NroMappedAddress == NroMappedAddress && Info.Executable.SourceAddress == NroHeapAddress)
  251. {
  252. NroInfos.Remove(Info);
  253. Context.Process.RemoveProgram(Info.NroMappedAddress);
  254. long Result = Context.Process.MemoryManager.UnmapProcessCodeMemory(Info.NroMappedAddress, Info.Executable.SourceAddress, Info.TotalSize - Info.Executable.BssSize);
  255. if (Result == 0 && Info.Executable.BssSize != 0)
  256. {
  257. Result = Context.Process.MemoryManager.UnmapProcessCodeMemory(Info.NroMappedAddress + Info.TotalSize - Info.Executable.BssSize, Info.Executable.BssAddress, Info.Executable.BssSize);
  258. }
  259. return Result;
  260. }
  261. }
  262. return MakeError(ErrorModule.Loader, LoaderErr.BadNroAddress);
  263. }
  264. // LoadNro(u64, u64, u64, u64, u64, pid) -> u64
  265. public long LoadNro(ServiceCtx Context)
  266. {
  267. long Result = MakeError(ErrorModule.Loader, LoaderErr.BadInitialization);
  268. // Zero
  269. Context.RequestData.ReadUInt64();
  270. long NroHeapAddress = Context.RequestData.ReadInt64();
  271. long NroSize = Context.RequestData.ReadInt64();
  272. long BssHeapAddress = Context.RequestData.ReadInt64();
  273. long BssSize = Context.RequestData.ReadInt64();
  274. long NroMappedAddress = 0;
  275. if (IsInitialized)
  276. {
  277. NroInfo Info;
  278. Result = ParseNro(out Info, Context, NroHeapAddress, NroSize, BssHeapAddress, BssSize);
  279. if (Result == 0)
  280. {
  281. Result = MapNro(Context, Info, out NroMappedAddress);
  282. if (Result == 0)
  283. {
  284. NroInfos.Add(Info);
  285. }
  286. }
  287. }
  288. Context.ResponseData.Write(NroMappedAddress);
  289. return Result;
  290. }
  291. // UnloadNro(u64, u64, pid)
  292. public long UnloadNro(ServiceCtx Context)
  293. {
  294. long Result = MakeError(ErrorModule.Loader, LoaderErr.BadInitialization);
  295. long NroMappedAddress = Context.RequestData.ReadInt64();
  296. long NroHeapAddress = Context.RequestData.ReadInt64();
  297. if (IsInitialized)
  298. {
  299. if ((NroMappedAddress & 0xFFF) != 0 || (NroHeapAddress & 0xFFF) != 0)
  300. {
  301. return MakeError(ErrorModule.Loader, LoaderErr.UnalignedAddress);
  302. }
  303. Result = RemoveNroInfo(Context, NroMappedAddress, NroHeapAddress);
  304. }
  305. return Result;
  306. }
  307. // LoadNrr(u64, u64, u64, pid)
  308. public long LoadNrr(ServiceCtx Context)
  309. {
  310. long Result = MakeError(ErrorModule.Loader, LoaderErr.BadInitialization);
  311. // Zero
  312. Context.RequestData.ReadUInt64();
  313. long NrrAddress = Context.RequestData.ReadInt64();
  314. long NrrSize = Context.RequestData.ReadInt64();
  315. if (IsInitialized)
  316. {
  317. NrrInfo Info;
  318. Result = ParseNrr(out Info, Context, NrrAddress, NrrSize);
  319. if(Result == 0)
  320. {
  321. if (NrrInfos.Count >= MaxNrr)
  322. {
  323. Result = MakeError(ErrorModule.Loader, LoaderErr.MaxNrr);
  324. }
  325. else
  326. {
  327. NrrInfos.Add(Info);
  328. }
  329. }
  330. }
  331. return Result;
  332. }
  333. // UnloadNrr(u64, u64, pid)
  334. public long UnloadNrr(ServiceCtx Context)
  335. {
  336. long Result = MakeError(ErrorModule.Loader, LoaderErr.BadInitialization);
  337. // Zero
  338. Context.RequestData.ReadUInt64();
  339. long NrrHeapAddress = Context.RequestData.ReadInt64();
  340. if (IsInitialized)
  341. {
  342. if ((NrrHeapAddress & 0xFFF) != 0)
  343. {
  344. return MakeError(ErrorModule.Loader, LoaderErr.UnalignedAddress);
  345. }
  346. Result = RemoveNrrInfo(NrrHeapAddress);
  347. }
  348. return Result;
  349. }
  350. // Initialize(u64, pid, KObject)
  351. public long Initialize(ServiceCtx Context)
  352. {
  353. // TODO: we actually ignore the pid and process handle receive, we will need to use them when we will have multi process support.
  354. IsInitialized = true;
  355. return 0;
  356. }
  357. }
  358. }