IpcMessage.cs 7.1 KB

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