IpcMessage.cs 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.IO;
  5. namespace Ryujinx.HLE.HOS.Ipc
  6. {
  7. class IpcMessage
  8. {
  9. public IpcMessageType Type { get; set; }
  10. public IpcHandleDesc HandleDesc { get; set; }
  11. public List<IpcPtrBuffDesc> PtrBuff { get; private set; }
  12. public List<IpcBuffDesc> SendBuff { get; private set; }
  13. public List<IpcBuffDesc> ReceiveBuff { get; private set; }
  14. public List<IpcBuffDesc> ExchangeBuff { get; private set; }
  15. public List<IpcRecvListBuffDesc> RecvListBuff { get; private set; }
  16. public List<int> ObjectIds { get; private set; }
  17. public byte[] RawData { get; set; }
  18. public IpcMessage()
  19. {
  20. PtrBuff = new List<IpcPtrBuffDesc>();
  21. SendBuff = new List<IpcBuffDesc>();
  22. ReceiveBuff = new List<IpcBuffDesc>();
  23. ExchangeBuff = new List<IpcBuffDesc>();
  24. RecvListBuff = new List<IpcRecvListBuffDesc>();
  25. ObjectIds = new List<int>();
  26. }
  27. public IpcMessage(byte[] data, long cmdPtr) : this()
  28. {
  29. using (MemoryStream ms = new MemoryStream(data))
  30. {
  31. BinaryReader reader = new BinaryReader(ms);
  32. Initialize(reader, cmdPtr);
  33. }
  34. }
  35. private void Initialize(BinaryReader reader, long cmdPtr)
  36. {
  37. int word0 = reader.ReadInt32();
  38. int word1 = reader.ReadInt32();
  39. Type = (IpcMessageType)(word0 & 0xffff);
  40. int ptrBuffCount = (word0 >> 16) & 0xf;
  41. int sendBuffCount = (word0 >> 20) & 0xf;
  42. int recvBuffCount = (word0 >> 24) & 0xf;
  43. int xchgBuffCount = (word0 >> 28) & 0xf;
  44. int rawDataSize = (word1 >> 0) & 0x3ff;
  45. int recvListFlags = (word1 >> 10) & 0xf;
  46. bool hndDescEnable = ((word1 >> 31) & 0x1) != 0;
  47. if (hndDescEnable)
  48. {
  49. HandleDesc = new IpcHandleDesc(reader);
  50. }
  51. for (int index = 0; index < ptrBuffCount; index++)
  52. {
  53. PtrBuff.Add(new IpcPtrBuffDesc(reader));
  54. }
  55. void ReadBuff(List<IpcBuffDesc> buff, int count)
  56. {
  57. for (int index = 0; index < count; index++)
  58. {
  59. buff.Add(new IpcBuffDesc(reader));
  60. }
  61. }
  62. ReadBuff(SendBuff, sendBuffCount);
  63. ReadBuff(ReceiveBuff, recvBuffCount);
  64. ReadBuff(ExchangeBuff, xchgBuffCount);
  65. rawDataSize *= 4;
  66. long recvListPos = reader.BaseStream.Position + rawDataSize;
  67. // only HIPC have the padding requirements.
  68. if (Type < IpcMessageType.TipcCloseSession)
  69. {
  70. long pad0 = GetPadSize16(reader.BaseStream.Position + cmdPtr);
  71. if (rawDataSize != 0)
  72. {
  73. rawDataSize -= (int)pad0;
  74. }
  75. reader.BaseStream.Seek(pad0, SeekOrigin.Current);
  76. }
  77. int recvListCount = recvListFlags - 2;
  78. if (recvListCount == 0)
  79. {
  80. recvListCount = 1;
  81. }
  82. else if (recvListCount < 0)
  83. {
  84. recvListCount = 0;
  85. }
  86. RawData = reader.ReadBytes(rawDataSize);
  87. reader.BaseStream.Seek(recvListPos, SeekOrigin.Begin);
  88. for (int index = 0; index < recvListCount; index++)
  89. {
  90. RecvListBuff.Add(new IpcRecvListBuffDesc(reader));
  91. }
  92. }
  93. public byte[] GetBytes(long cmdPtr, ulong recvListAddr)
  94. {
  95. using (MemoryStream ms = new MemoryStream())
  96. {
  97. BinaryWriter writer = new BinaryWriter(ms);
  98. int word0;
  99. int word1;
  100. word0 = (int)Type;
  101. word0 |= (PtrBuff.Count & 0xf) << 16;
  102. word0 |= (SendBuff.Count & 0xf) << 20;
  103. word0 |= (ReceiveBuff.Count & 0xf) << 24;
  104. word0 |= (ExchangeBuff.Count & 0xf) << 28;
  105. byte[] handleData = Array.Empty<byte>();
  106. if (HandleDesc != null)
  107. {
  108. handleData = HandleDesc.GetBytes();
  109. }
  110. int dataLength = RawData?.Length ?? 0;
  111. dataLength = (dataLength + 3) & ~3;
  112. int rawLength = dataLength;
  113. int pad0 = (int)GetPadSize16(cmdPtr + 8 + handleData.Length + PtrBuff.Count * 8);
  114. // Apparently, padding after Raw Data is 16 bytes, however when there is
  115. // padding before Raw Data too, we need to subtract the size of this padding.
  116. // This is the weirdest padding I've seen so far...
  117. int pad1 = 0x10 - pad0;
  118. dataLength = (dataLength + pad0 + pad1) / 4;
  119. word1 = (dataLength & 0x3ff) | (2 << 10);
  120. if (HandleDesc != null)
  121. {
  122. word1 |= 1 << 31;
  123. }
  124. writer.Write(word0);
  125. writer.Write(word1);
  126. writer.Write(handleData);
  127. for (int index = 0; index < PtrBuff.Count; index++)
  128. {
  129. writer.Write(PtrBuff[index].GetWord0());
  130. writer.Write(PtrBuff[index].GetWord1());
  131. }
  132. ms.Seek(pad0, SeekOrigin.Current);
  133. if (RawData != null)
  134. {
  135. writer.Write(RawData);
  136. ms.Seek(rawLength - RawData.Length, SeekOrigin.Current);
  137. }
  138. writer.Write(new byte[pad1]);
  139. writer.Write(recvListAddr);
  140. return ms.ToArray();
  141. }
  142. }
  143. public byte[] GetBytesTipc()
  144. {
  145. Debug.Assert(PtrBuff.Count == 0);
  146. using (MemoryStream ms = new MemoryStream())
  147. {
  148. BinaryWriter writer = new BinaryWriter(ms);
  149. int word0;
  150. int word1;
  151. word0 = (int)Type;
  152. word0 |= (SendBuff.Count & 0xf) << 20;
  153. word0 |= (ReceiveBuff.Count & 0xf) << 24;
  154. word0 |= (ExchangeBuff.Count & 0xf) << 28;
  155. byte[] handleData = Array.Empty<byte>();
  156. if (HandleDesc != null)
  157. {
  158. handleData = HandleDesc.GetBytes();
  159. }
  160. int dataLength = RawData?.Length ?? 0;
  161. dataLength = ((dataLength + 3) & ~3) / 4;
  162. word1 = (dataLength & 0x3ff);
  163. if (HandleDesc != null)
  164. {
  165. word1 |= 1 << 31;
  166. }
  167. writer.Write(word0);
  168. writer.Write(word1);
  169. writer.Write(handleData);
  170. if (RawData != null)
  171. {
  172. writer.Write(RawData);
  173. }
  174. return ms.ToArray();
  175. }
  176. }
  177. private long GetPadSize16(long position)
  178. {
  179. if ((position & 0xf) != 0)
  180. {
  181. return 0x10 - (position & 0xf);
  182. }
  183. return 0;
  184. }
  185. // ReSharper disable once InconsistentNaming
  186. public (ulong Position, ulong Size) GetBufferType0x21(int index = 0)
  187. {
  188. if (PtrBuff.Count > index && PtrBuff[index].Position != 0)
  189. {
  190. return (PtrBuff[index].Position, PtrBuff[index].Size);
  191. }
  192. if (SendBuff.Count > index)
  193. {
  194. return (SendBuff[index].Position, SendBuff[index].Size);
  195. }
  196. return (0, 0);
  197. }
  198. // ReSharper disable once InconsistentNaming
  199. public (ulong Position, ulong Size) GetBufferType0x22(int index = 0)
  200. {
  201. if (RecvListBuff.Count > index && RecvListBuff[index].Position != 0)
  202. {
  203. return (RecvListBuff[index].Position, RecvListBuff[index].Size);
  204. }
  205. if (ReceiveBuff.Count > index)
  206. {
  207. return (ReceiveBuff[index].Position, ReceiveBuff[index].Size);
  208. }
  209. return (0, 0);
  210. }
  211. }
  212. }