2018-02-05 00:08:20 +01:00
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.IO;
|
|
|
|
|
2018-08-17 01:47:36 +02:00
|
|
|
namespace Ryujinx.HLE.HOS.Ipc
|
2018-02-05 00:08:20 +01:00
|
|
|
{
|
|
|
|
class IpcMessage
|
|
|
|
{
|
|
|
|
public IpcMessageType Type { get; set; }
|
|
|
|
|
|
|
|
public IpcHandleDesc HandleDesc { get; set; }
|
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
public List<IpcPtrBuffDesc> PtrBuff { get; private set; }
|
|
|
|
public List<IpcBuffDesc> SendBuff { get; private set; }
|
|
|
|
public List<IpcBuffDesc> ReceiveBuff { get; private set; }
|
|
|
|
public List<IpcBuffDesc> ExchangeBuff { get; private set; }
|
|
|
|
public List<IpcRecvListBuffDesc> RecvListBuff { get; private set; }
|
2018-02-05 00:08:20 +01:00
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
public List<int> ObjectIds { get; private set; }
|
2018-02-05 00:08:20 +01:00
|
|
|
|
|
|
|
public byte[] RawData { get; set; }
|
|
|
|
|
|
|
|
public IpcMessage()
|
|
|
|
{
|
|
|
|
PtrBuff = new List<IpcPtrBuffDesc>();
|
|
|
|
SendBuff = new List<IpcBuffDesc>();
|
|
|
|
ReceiveBuff = new List<IpcBuffDesc>();
|
|
|
|
ExchangeBuff = new List<IpcBuffDesc>();
|
|
|
|
RecvListBuff = new List<IpcRecvListBuffDesc>();
|
|
|
|
|
2018-07-29 06:36:29 +02:00
|
|
|
ObjectIds = new List<int>();
|
2018-02-05 00:08:20 +01:00
|
|
|
}
|
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
public IpcMessage(byte[] data, long cmdPtr) : this()
|
2018-02-05 00:08:20 +01:00
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
using (MemoryStream ms = new MemoryStream(data))
|
2018-02-05 00:08:20 +01:00
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
BinaryReader reader = new BinaryReader(ms);
|
2018-02-05 00:08:20 +01:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
Initialize(reader, cmdPtr);
|
2018-02-05 00:08:20 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
private void Initialize(BinaryReader reader, long cmdPtr)
|
2018-02-05 00:08:20 +01:00
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
int word0 = reader.ReadInt32();
|
|
|
|
int word1 = reader.ReadInt32();
|
2018-02-05 00:08:20 +01:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
Type = (IpcMessageType)(word0 & 0xffff);
|
2018-02-05 00:08:20 +01:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
int ptrBuffCount = (word0 >> 16) & 0xf;
|
|
|
|
int sendBuffCount = (word0 >> 20) & 0xf;
|
|
|
|
int recvBuffCount = (word0 >> 24) & 0xf;
|
|
|
|
int xchgBuffCount = (word0 >> 28) & 0xf;
|
2018-02-05 00:08:20 +01:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
int rawDataSize = (word1 >> 0) & 0x3ff;
|
|
|
|
int recvListFlags = (word1 >> 10) & 0xf;
|
|
|
|
bool hndDescEnable = ((word1 >> 31) & 0x1) != 0;
|
2018-02-05 00:08:20 +01:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
if (hndDescEnable)
|
2018-02-05 00:08:20 +01:00
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
HandleDesc = new IpcHandleDesc(reader);
|
2018-02-05 00:08:20 +01:00
|
|
|
}
|
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
for (int index = 0; index < ptrBuffCount; index++)
|
2018-02-05 00:08:20 +01:00
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
PtrBuff.Add(new IpcPtrBuffDesc(reader));
|
2018-02-05 00:08:20 +01:00
|
|
|
}
|
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
void ReadBuff(List<IpcBuffDesc> buff, int count)
|
2018-02-05 00:08:20 +01:00
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
for (int index = 0; index < count; index++)
|
2018-02-05 00:08:20 +01:00
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
buff.Add(new IpcBuffDesc(reader));
|
2018-02-05 00:08:20 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
ReadBuff(SendBuff, sendBuffCount);
|
|
|
|
ReadBuff(ReceiveBuff, recvBuffCount);
|
|
|
|
ReadBuff(ExchangeBuff, xchgBuffCount);
|
2018-02-05 00:08:20 +01:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
rawDataSize *= 4;
|
2018-02-05 00:08:20 +01:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
long recvListPos = reader.BaseStream.Position + rawDataSize;
|
2018-02-05 00:08:20 +01:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
long pad0 = GetPadSize16(reader.BaseStream.Position + cmdPtr);
|
2018-02-05 00:08:20 +01:00
|
|
|
|
2020-12-02 00:23:43 +01:00
|
|
|
if (rawDataSize != 0)
|
|
|
|
{
|
|
|
|
rawDataSize -= (int)pad0;
|
|
|
|
}
|
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
reader.BaseStream.Seek(pad0, SeekOrigin.Current);
|
2018-02-05 00:08:20 +01:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
int recvListCount = recvListFlags - 2;
|
2018-02-05 00:08:20 +01:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
if (recvListCount == 0)
|
2018-02-05 00:08:20 +01:00
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
recvListCount = 1;
|
2018-02-05 00:08:20 +01:00
|
|
|
}
|
2018-12-06 12:16:24 +01:00
|
|
|
else if (recvListCount < 0)
|
2018-02-05 00:08:20 +01:00
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
recvListCount = 0;
|
2018-02-05 00:08:20 +01:00
|
|
|
}
|
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
RawData = reader.ReadBytes(rawDataSize);
|
2018-02-05 00:08:20 +01:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
reader.BaseStream.Seek(recvListPos, SeekOrigin.Begin);
|
2018-02-05 00:08:20 +01:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
for (int index = 0; index < recvListCount; index++)
|
2018-02-05 00:08:20 +01:00
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
RecvListBuff.Add(new IpcRecvListBuffDesc(reader));
|
2018-02-05 00:08:20 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-02 00:23:43 +01:00
|
|
|
public byte[] GetBytes(long cmdPtr, ulong recvListAddr)
|
2018-02-05 00:08:20 +01:00
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
using (MemoryStream ms = new MemoryStream())
|
2018-02-05 00:08:20 +01:00
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
BinaryWriter writer = new BinaryWriter(ms);
|
2018-02-05 00:08:20 +01:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
int word0;
|
|
|
|
int word1;
|
2018-02-05 00:08:20 +01:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
word0 = (int)Type;
|
|
|
|
word0 |= (PtrBuff.Count & 0xf) << 16;
|
|
|
|
word0 |= (SendBuff.Count & 0xf) << 20;
|
|
|
|
word0 |= (ReceiveBuff.Count & 0xf) << 24;
|
|
|
|
word0 |= (ExchangeBuff.Count & 0xf) << 28;
|
2018-02-05 00:08:20 +01:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
byte[] handleData = new byte[0];
|
2018-02-05 00:08:20 +01:00
|
|
|
|
|
|
|
if (HandleDesc != null)
|
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
handleData = HandleDesc.GetBytes();
|
2018-02-05 00:08:20 +01:00
|
|
|
}
|
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
int dataLength = RawData?.Length ?? 0;
|
2018-02-05 00:08:20 +01:00
|
|
|
|
2020-12-02 00:23:43 +01:00
|
|
|
dataLength = (dataLength + 3) & ~3;
|
|
|
|
|
|
|
|
int rawLength = dataLength;
|
|
|
|
|
|
|
|
int pad0 = (int)GetPadSize16(cmdPtr + 8 + handleData.Length + PtrBuff.Count * 8);
|
2018-02-05 00:08:20 +01:00
|
|
|
|
2019-07-02 04:39:22 +02:00
|
|
|
// Apparently, padding after Raw Data is 16 bytes, however when there is
|
|
|
|
// padding before Raw Data too, we need to subtract the size of this padding.
|
|
|
|
// This is the weirdest padding I've seen so far...
|
2018-12-06 12:16:24 +01:00
|
|
|
int pad1 = 0x10 - pad0;
|
2018-02-05 00:08:20 +01:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
dataLength = (dataLength + pad0 + pad1) / 4;
|
2018-02-05 00:08:20 +01:00
|
|
|
|
2020-12-02 00:23:43 +01:00
|
|
|
word1 = (dataLength & 0x3ff) | (2 << 10);
|
2018-02-05 00:08:20 +01:00
|
|
|
|
|
|
|
if (HandleDesc != null)
|
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
word1 |= 1 << 31;
|
2018-02-05 00:08:20 +01:00
|
|
|
}
|
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
writer.Write(word0);
|
|
|
|
writer.Write(word1);
|
|
|
|
writer.Write(handleData);
|
2018-02-05 00:08:20 +01:00
|
|
|
|
2020-12-02 00:23:43 +01:00
|
|
|
for (int index = 0; index < PtrBuff.Count; index++)
|
|
|
|
{
|
|
|
|
writer.Write(PtrBuff[index].GetWord0());
|
|
|
|
writer.Write(PtrBuff[index].GetWord1());
|
|
|
|
}
|
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
ms.Seek(pad0, SeekOrigin.Current);
|
2018-02-05 00:08:20 +01:00
|
|
|
|
|
|
|
if (RawData != null)
|
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
writer.Write(RawData);
|
2020-12-02 00:23:43 +01:00
|
|
|
ms.Seek(rawLength - RawData.Length, SeekOrigin.Current);
|
2018-02-05 00:08:20 +01:00
|
|
|
}
|
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
writer.Write(new byte[pad1]);
|
2020-12-02 00:23:43 +01:00
|
|
|
writer.Write(recvListAddr);
|
2018-02-05 00:08:20 +01:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
return ms.ToArray();
|
2018-02-05 00:08:20 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
private long GetPadSize16(long position)
|
2018-02-05 00:08:20 +01:00
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
if ((position & 0xf) != 0)
|
2018-02-05 00:08:20 +01:00
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
return 0x10 - (position & 0xf);
|
2018-02-05 00:08:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
// ReSharper disable once InconsistentNaming
|
|
|
|
public (long Position, long Size) GetBufferType0x21(int index = 0)
|
2018-04-26 04:11:26 +02:00
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
if (PtrBuff.Count > index &&
|
|
|
|
PtrBuff[index].Position != 0 &&
|
|
|
|
PtrBuff[index].Size != 0)
|
2018-04-26 04:11:26 +02:00
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
return (PtrBuff[index].Position, PtrBuff[index].Size);
|
2018-04-26 04:11:26 +02:00
|
|
|
}
|
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
if (SendBuff.Count > index &&
|
|
|
|
SendBuff[index].Position != 0 &&
|
|
|
|
SendBuff[index].Size != 0)
|
2018-04-26 04:11:26 +02:00
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
return (SendBuff[index].Position, SendBuff[index].Size);
|
2018-04-26 04:11:26 +02:00
|
|
|
}
|
|
|
|
|
2018-06-03 00:46:09 +02:00
|
|
|
return (0, 0);
|
2018-04-26 04:11:26 +02:00
|
|
|
}
|
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
// ReSharper disable once InconsistentNaming
|
|
|
|
public (long Position, long Size) GetBufferType0x22(int index = 0)
|
2018-04-26 04:11:26 +02:00
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
if (RecvListBuff.Count > index &&
|
|
|
|
RecvListBuff[index].Position != 0 &&
|
|
|
|
RecvListBuff[index].Size != 0)
|
2018-04-26 04:11:26 +02:00
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
return (RecvListBuff[index].Position, RecvListBuff[index].Size);
|
2018-04-26 04:11:26 +02:00
|
|
|
}
|
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
if (ReceiveBuff.Count > index &&
|
|
|
|
ReceiveBuff[index].Position != 0 &&
|
|
|
|
ReceiveBuff[index].Size != 0)
|
2018-04-26 04:11:26 +02:00
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
return (ReceiveBuff[index].Position, ReceiveBuff[index].Size);
|
2018-04-26 04:11:26 +02:00
|
|
|
}
|
|
|
|
|
2018-06-03 00:46:09 +02:00
|
|
|
return (0, 0);
|
2018-04-26 04:11:26 +02:00
|
|
|
}
|
2018-02-05 00:08:20 +01:00
|
|
|
}
|
2018-02-25 00:08:54 +01:00
|
|
|
}
|