2023-01-04 17:15:45 -05:00
|
|
|
|
using Ryujinx.Horizon.Common;
|
|
|
|
|
using Ryujinx.Horizon.Sdk.Sf.Hipc;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Runtime.CompilerServices;
|
|
|
|
|
using System.Runtime.InteropServices;
|
|
|
|
|
|
|
|
|
|
namespace Ryujinx.Horizon.Sdk.Sf.Cmif
|
|
|
|
|
{
|
|
|
|
|
static class CmifMessage
|
|
|
|
|
{
|
2023-01-08 07:13:39 -05:00
|
|
|
|
public const uint CmifInHeaderMagic = 0x49434653; // SFCI
|
2023-01-04 17:15:45 -05:00
|
|
|
|
public const uint CmifOutHeaderMagic = 0x4f434653; // SFCO
|
|
|
|
|
|
|
|
|
|
public static CmifRequest CreateRequest(Span<byte> output, CmifRequestFormat format)
|
|
|
|
|
{
|
|
|
|
|
int totalSize = 16;
|
|
|
|
|
|
|
|
|
|
if (format.ObjectId != 0)
|
|
|
|
|
{
|
|
|
|
|
totalSize += Unsafe.SizeOf<CmifDomainInHeader>() + format.ObjectsCount * sizeof(int);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
totalSize += Unsafe.SizeOf<CmifInHeader>() + format.DataSize;
|
2023-01-08 07:13:39 -05:00
|
|
|
|
totalSize = (totalSize + 1) & ~1;
|
|
|
|
|
|
2023-01-04 17:15:45 -05:00
|
|
|
|
int outPointerSizeTableOffset = totalSize;
|
2023-01-08 07:13:39 -05:00
|
|
|
|
int outPointerSizeTableSize = format.OutAutoBuffersCount + format.OutPointersCount;
|
|
|
|
|
|
2023-01-04 17:15:45 -05:00
|
|
|
|
totalSize += sizeof(ushort) * outPointerSizeTableSize;
|
|
|
|
|
|
2023-01-08 07:13:39 -05:00
|
|
|
|
int rawDataSizeInWords = (totalSize + sizeof(uint) - 1) / sizeof(uint);
|
2023-01-04 17:15:45 -05:00
|
|
|
|
|
2023-01-08 07:13:39 -05:00
|
|
|
|
CmifRequest request = new()
|
2023-01-04 17:15:45 -05:00
|
|
|
|
{
|
2023-01-08 07:13:39 -05:00
|
|
|
|
Hipc = HipcMessage.WriteMessage(output, new HipcMetadata()
|
|
|
|
|
{
|
|
|
|
|
Type = format.Context != 0 ? (int)CommandType.RequestWithContext : (int)CommandType.Request,
|
|
|
|
|
SendStaticsCount = format.InAutoBuffersCount + format.InPointersCount,
|
|
|
|
|
SendBuffersCount = format.InAutoBuffersCount + format.InBuffersCount,
|
|
|
|
|
ReceiveBuffersCount = format.OutAutoBuffersCount + format.OutBuffersCount,
|
|
|
|
|
ExchangeBuffersCount = format.InOutBuffersCount,
|
|
|
|
|
DataWordsCount = rawDataSizeInWords,
|
|
|
|
|
ReceiveStaticsCount = outPointerSizeTableSize + format.OutFixedPointersCount,
|
|
|
|
|
SendPid = format.SendPid,
|
|
|
|
|
CopyHandlesCount = format.HandlesCount,
|
|
|
|
|
MoveHandlesCount = 0
|
|
|
|
|
})
|
|
|
|
|
};
|
2023-01-04 17:15:45 -05:00
|
|
|
|
|
|
|
|
|
Span<uint> data = request.Hipc.DataWords;
|
|
|
|
|
|
|
|
|
|
if (format.ObjectId != 0)
|
|
|
|
|
{
|
|
|
|
|
ref CmifDomainInHeader domainHeader = ref MemoryMarshal.Cast<uint, CmifDomainInHeader>(data)[0];
|
|
|
|
|
|
|
|
|
|
int payloadSize = Unsafe.SizeOf<CmifInHeader>() + format.DataSize;
|
|
|
|
|
|
|
|
|
|
domainHeader = new CmifDomainInHeader()
|
|
|
|
|
{
|
2023-01-08 07:13:39 -05:00
|
|
|
|
Type = CmifDomainRequestType.SendMessage,
|
2023-01-04 17:15:45 -05:00
|
|
|
|
ObjectsCount = (byte)format.ObjectsCount,
|
2023-01-08 07:13:39 -05:00
|
|
|
|
DataSize = (ushort)payloadSize,
|
|
|
|
|
ObjectId = format.ObjectId,
|
|
|
|
|
Padding = 0,
|
|
|
|
|
Token = format.Context
|
2023-01-04 17:15:45 -05:00
|
|
|
|
};
|
|
|
|
|
|
2023-01-08 07:13:39 -05:00
|
|
|
|
data = data[(Unsafe.SizeOf<CmifDomainInHeader>() / sizeof(uint))..];
|
2023-01-04 17:15:45 -05:00
|
|
|
|
|
2023-01-08 07:13:39 -05:00
|
|
|
|
request.Objects = data[((payloadSize + sizeof(uint) - 1) / sizeof(uint))..];
|
2023-01-04 17:15:45 -05:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ref CmifInHeader header = ref MemoryMarshal.Cast<uint, CmifInHeader>(data)[0];
|
|
|
|
|
|
|
|
|
|
header = new CmifInHeader()
|
|
|
|
|
{
|
2023-01-08 07:13:39 -05:00
|
|
|
|
Magic = CmifInHeaderMagic,
|
|
|
|
|
Version = format.Context != 0 ? 1u : 0u,
|
2023-01-04 17:15:45 -05:00
|
|
|
|
CommandId = format.RequestId,
|
2023-01-08 07:13:39 -05:00
|
|
|
|
Token = format.ObjectId != 0 ? 0u : format.Context
|
2023-01-04 17:15:45 -05:00
|
|
|
|
};
|
|
|
|
|
|
2023-01-08 07:13:39 -05:00
|
|
|
|
request.Data = MemoryMarshal.Cast<uint, byte>(data)[Unsafe.SizeOf<CmifInHeader>()..];
|
2023-01-04 17:15:45 -05:00
|
|
|
|
|
|
|
|
|
int paddingSizeBefore = (rawDataSizeInWords - request.Hipc.DataWords.Length) * sizeof(uint);
|
|
|
|
|
|
2023-01-08 07:13:39 -05:00
|
|
|
|
Span<byte> outPointerTable = MemoryMarshal.Cast<uint, byte>(request.Hipc.DataWords)[(outPointerSizeTableOffset - paddingSizeBefore)..];
|
|
|
|
|
|
|
|
|
|
request.OutPointerSizes = MemoryMarshal.Cast<byte, ushort>(outPointerTable);
|
2023-01-04 17:15:45 -05:00
|
|
|
|
request.ServerPointerSize = format.ServerPointerSize;
|
|
|
|
|
|
|
|
|
|
return request;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static Result ParseResponse(out CmifResponse response, Span<byte> input, bool isDomain, int size)
|
|
|
|
|
{
|
2023-01-08 07:13:39 -05:00
|
|
|
|
HipcMessage responseMessage = new(input);
|
2023-01-04 17:15:45 -05:00
|
|
|
|
|
2023-01-08 07:13:39 -05:00
|
|
|
|
Span<byte> data = MemoryMarshal.Cast<uint, byte>(responseMessage.Data.DataWords);
|
2023-01-04 17:15:45 -05:00
|
|
|
|
Span<uint> objects = Span<uint>.Empty;
|
|
|
|
|
|
|
|
|
|
if (isDomain)
|
|
|
|
|
{
|
2023-01-08 07:13:39 -05:00
|
|
|
|
data = data[Unsafe.SizeOf<CmifDomainOutHeader>()..];
|
|
|
|
|
objects = MemoryMarshal.Cast<byte, uint>(data[(Unsafe.SizeOf<CmifOutHeader>() + size)..]);
|
2023-01-04 17:15:45 -05:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
CmifOutHeader header = MemoryMarshal.Cast<byte, CmifOutHeader>(data)[0];
|
|
|
|
|
|
|
|
|
|
if (header.Magic != CmifOutHeaderMagic)
|
|
|
|
|
{
|
|
|
|
|
response = default;
|
2023-01-08 07:13:39 -05:00
|
|
|
|
|
2023-01-04 17:15:45 -05:00
|
|
|
|
return SfResult.InvalidOutHeader;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (header.Result.IsFailure)
|
|
|
|
|
{
|
|
|
|
|
response = default;
|
2023-01-08 07:13:39 -05:00
|
|
|
|
|
2023-01-04 17:15:45 -05:00
|
|
|
|
return header.Result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
response = new CmifResponse()
|
|
|
|
|
{
|
2023-01-08 07:13:39 -05:00
|
|
|
|
Data = data[Unsafe.SizeOf<CmifOutHeader>()..],
|
|
|
|
|
Objects = objects,
|
2023-01-04 17:15:45 -05:00
|
|
|
|
CopyHandles = responseMessage.Data.CopyHandles,
|
|
|
|
|
MoveHandles = responseMessage.Data.MoveHandles
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return Result.Success;
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-01-08 07:13:39 -05:00
|
|
|
|
}
|