IpcMessage.cs 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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. reader.BaseStream.Seek(pad0, SeekOrigin.Current);
  67. int recvListCount = recvListFlags - 2;
  68. if (recvListCount == 0)
  69. {
  70. recvListCount = 1;
  71. }
  72. else if (recvListCount < 0)
  73. {
  74. recvListCount = 0;
  75. }
  76. RawData = reader.ReadBytes(rawDataSize);
  77. reader.BaseStream.Seek(recvListPos, SeekOrigin.Begin);
  78. for (int index = 0; index < recvListCount; index++)
  79. {
  80. RecvListBuff.Add(new IpcRecvListBuffDesc(reader));
  81. }
  82. }
  83. public byte[] GetBytes(long cmdPtr)
  84. {
  85. using (MemoryStream ms = new MemoryStream())
  86. {
  87. BinaryWriter writer = new BinaryWriter(ms);
  88. int word0;
  89. int word1;
  90. word0 = (int)Type;
  91. word0 |= (PtrBuff.Count & 0xf) << 16;
  92. word0 |= (SendBuff.Count & 0xf) << 20;
  93. word0 |= (ReceiveBuff.Count & 0xf) << 24;
  94. word0 |= (ExchangeBuff.Count & 0xf) << 28;
  95. byte[] handleData = new byte[0];
  96. if (HandleDesc != null)
  97. {
  98. handleData = HandleDesc.GetBytes();
  99. }
  100. int dataLength = RawData?.Length ?? 0;
  101. int pad0 = (int)GetPadSize16(cmdPtr + 8 + handleData.Length);
  102. // Apparently, padding after Raw Data is 16 bytes, however when there is
  103. // padding before Raw Data too, we need to subtract the size of this padding.
  104. // This is the weirdest padding I've seen so far...
  105. int pad1 = 0x10 - pad0;
  106. dataLength = (dataLength + pad0 + pad1) / 4;
  107. word1 = dataLength & 0x3ff;
  108. if (HandleDesc != null)
  109. {
  110. word1 |= 1 << 31;
  111. }
  112. writer.Write(word0);
  113. writer.Write(word1);
  114. writer.Write(handleData);
  115. ms.Seek(pad0, SeekOrigin.Current);
  116. if (RawData != null)
  117. {
  118. writer.Write(RawData);
  119. }
  120. writer.Write(new byte[pad1]);
  121. return ms.ToArray();
  122. }
  123. }
  124. private long GetPadSize16(long position)
  125. {
  126. if ((position & 0xf) != 0)
  127. {
  128. return 0x10 - (position & 0xf);
  129. }
  130. return 0;
  131. }
  132. // ReSharper disable once InconsistentNaming
  133. public (long Position, long Size) GetBufferType0x21(int index = 0)
  134. {
  135. if (PtrBuff.Count > index &&
  136. PtrBuff[index].Position != 0 &&
  137. PtrBuff[index].Size != 0)
  138. {
  139. return (PtrBuff[index].Position, PtrBuff[index].Size);
  140. }
  141. if (SendBuff.Count > index &&
  142. SendBuff[index].Position != 0 &&
  143. SendBuff[index].Size != 0)
  144. {
  145. return (SendBuff[index].Position, SendBuff[index].Size);
  146. }
  147. return (0, 0);
  148. }
  149. // ReSharper disable once InconsistentNaming
  150. public (long Position, long Size) GetBufferType0x22(int index = 0)
  151. {
  152. if (RecvListBuff.Count > index &&
  153. RecvListBuff[index].Position != 0 &&
  154. RecvListBuff[index].Size != 0)
  155. {
  156. return (RecvListBuff[index].Position, RecvListBuff[index].Size);
  157. }
  158. if (ReceiveBuff.Count > index &&
  159. ReceiveBuff[index].Position != 0 &&
  160. ReceiveBuff[index].Size != 0)
  161. {
  162. return (ReceiveBuff[index].Position, ReceiveBuff[index].Size);
  163. }
  164. return (0, 0);
  165. }
  166. }
  167. }