IpcMessage.cs 8.2 KB

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