Ryujinx/Ryujinx.HLE/HOS/Ipc/IpcMessage.cs

216 lines
6.4 KiB
C#
Raw Normal View History

2018-02-04 15:08:20 -08:00
using System.Collections.Generic;
using System.IO;
namespace Ryujinx.HLE.HOS.Ipc
2018-02-04 15:08:20 -08:00
{
class IpcMessage
{
public IpcMessageType Type { get; set; }
public IpcHandleDesc HandleDesc { get; set; }
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-04 15:08:20 -08:00
public List<int> ObjectIds { get; private set; }
2018-02-04 15:08:20 -08: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>();
ObjectIds = new List<int>();
2018-02-04 15:08:20 -08:00
}
public IpcMessage(byte[] Data, long CmdPtr) : this()
2018-02-04 15:08:20 -08:00
{
using (MemoryStream MS = new MemoryStream(Data))
2018-02-04 15:08:20 -08:00
{
BinaryReader Reader = new BinaryReader(MS);
2018-02-04 15:08:20 -08:00
Initialize(Reader, CmdPtr);
2018-02-04 15:08:20 -08:00
}
}
private void Initialize(BinaryReader Reader, long CmdPtr)
2018-02-04 15:08:20 -08:00
{
int Word0 = Reader.ReadInt32();
int Word1 = Reader.ReadInt32();
2018-02-04 15:08:20 -08:00
Type = (IpcMessageType)(Word0 & 0xffff);
2018-02-04 15:08:20 -08:00
int PtrBuffCount = (Word0 >> 16) & 0xf;
int SendBuffCount = (Word0 >> 20) & 0xf;
int RecvBuffCount = (Word0 >> 24) & 0xf;
int XchgBuffCount = (Word0 >> 28) & 0xf;
2018-02-04 15:08:20 -08:00
int RawDataSize = (Word1 >> 0) & 0x3ff;
int RecvListFlags = (Word1 >> 10) & 0xf;
bool HndDescEnable = ((Word1 >> 31) & 0x1) != 0;
2018-02-04 15:08:20 -08:00
if (HndDescEnable)
2018-02-04 15:08:20 -08:00
{
HandleDesc = new IpcHandleDesc(Reader);
2018-02-04 15:08:20 -08:00
}
for (int Index = 0; Index < PtrBuffCount; Index++)
2018-02-04 15:08:20 -08:00
{
PtrBuff.Add(new IpcPtrBuffDesc(Reader));
2018-02-04 15:08:20 -08:00
}
void ReadBuff(List<IpcBuffDesc> Buff, int Count)
2018-02-04 15:08:20 -08:00
{
for (int Index = 0; Index < Count; Index++)
2018-02-04 15:08:20 -08:00
{
Buff.Add(new IpcBuffDesc(Reader));
2018-02-04 15:08:20 -08:00
}
}
ReadBuff(SendBuff, SendBuffCount);
ReadBuff(ReceiveBuff, RecvBuffCount);
ReadBuff(ExchangeBuff, XchgBuffCount);
2018-02-04 15:08:20 -08:00
RawDataSize *= 4;
2018-02-04 15:08:20 -08:00
long RecvListPos = Reader.BaseStream.Position + RawDataSize;
2018-02-04 15:08:20 -08:00
long Pad0 = GetPadSize16(Reader.BaseStream.Position + CmdPtr);
2018-02-04 15:08:20 -08:00
Reader.BaseStream.Seek(Pad0, SeekOrigin.Current);
2018-02-04 15:08:20 -08:00
int RecvListCount = RecvListFlags - 2;
2018-02-04 15:08:20 -08:00
if (RecvListCount == 0)
2018-02-04 15:08:20 -08:00
{
RecvListCount = 1;
2018-02-04 15:08:20 -08:00
}
else if (RecvListCount < 0)
2018-02-04 15:08:20 -08:00
{
RecvListCount = 0;
2018-02-04 15:08:20 -08:00
}
RawData = Reader.ReadBytes(RawDataSize);
2018-02-04 15:08:20 -08:00
Reader.BaseStream.Seek(RecvListPos, SeekOrigin.Begin);
2018-02-04 15:08:20 -08:00
for (int Index = 0; Index < RecvListCount; Index++)
2018-02-04 15:08:20 -08:00
{
RecvListBuff.Add(new IpcRecvListBuffDesc(Reader));
2018-02-04 15:08:20 -08:00
}
}
public byte[] GetBytes(long CmdPtr)
2018-02-04 15:08:20 -08:00
{
using (MemoryStream MS = new MemoryStream())
2018-02-04 15:08:20 -08:00
{
BinaryWriter Writer = new BinaryWriter(MS);
2018-02-04 15:08:20 -08:00
int Word0;
int Word1;
2018-02-04 15:08:20 -08: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-04 15:08:20 -08:00
byte[] HandleData = new byte[0];
2018-02-04 15:08:20 -08:00
if (HandleDesc != null)
{
HandleData = HandleDesc.GetBytes();
2018-02-04 15:08:20 -08:00
}
int DataLength = RawData?.Length ?? 0;
2018-02-04 15:08:20 -08:00
int Pad0 = (int)GetPadSize16(CmdPtr + 8 + HandleData.Length);
2018-02-04 15:08:20 -08: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...
int Pad1 = 0x10 - Pad0;
2018-02-04 15:08:20 -08:00
DataLength = (DataLength + Pad0 + Pad1) / 4;
2018-02-04 15:08:20 -08:00
Word1 = DataLength & 0x3ff;
2018-02-04 15:08:20 -08:00
if (HandleDesc != null)
{
Word1 |= 1 << 31;
2018-02-04 15:08:20 -08:00
}
Writer.Write(Word0);
Writer.Write(Word1);
Writer.Write(HandleData);
2018-02-04 15:08:20 -08:00
MS.Seek(Pad0, SeekOrigin.Current);
2018-02-04 15:08:20 -08:00
if (RawData != null)
{
Writer.Write(RawData);
2018-02-04 15:08:20 -08:00
}
Writer.Write(new byte[Pad1]);
2018-02-04 15:08:20 -08:00
return MS.ToArray();
2018-02-04 15:08:20 -08:00
}
}
private long GetPadSize16(long Position)
2018-02-04 15:08:20 -08:00
{
if ((Position & 0xf) != 0)
2018-02-04 15:08:20 -08:00
{
return 0x10 - (Position & 0xf);
2018-02-04 15:08:20 -08:00
}
return 0;
}
public (long Position, long Size) GetBufferType0x21(int Index = 0)
{
if (PtrBuff.Count > Index &&
PtrBuff[Index].Position != 0 &&
PtrBuff[Index].Size != 0)
{
return (PtrBuff[Index].Position, PtrBuff[Index].Size);
}
if (SendBuff.Count > Index &&
SendBuff[Index].Position != 0 &&
SendBuff[Index].Size != 0)
{
return (SendBuff[Index].Position, SendBuff[Index].Size);
}
return (0, 0);
}
public (long Position, long Size) GetBufferType0x22(int Index = 0)
{
if (RecvListBuff.Count > Index &&
RecvListBuff[Index].Position != 0 &&
RecvListBuff[Index].Size != 0)
{
return (RecvListBuff[Index].Position, RecvListBuff[Index].Size);
}
if (ReceiveBuff.Count > Index &&
ReceiveBuff[Index].Position != 0 &&
ReceiveBuff[Index].Size != 0)
{
return (ReceiveBuff[Index].Position, ReceiveBuff[Index].Size);
}
return (0, 0);
}
2018-02-04 15:08:20 -08:00
}
}