aboutsummaryrefslogtreecommitdiff
path: root/src/Ryujinx.Horizon/Sdk/Sf/Cmif/CmifMessage.cs
blob: f0b6f0c36aa789096adc7552bc28a3f162bc1a81 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
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
    {
        public const uint CmifInHeaderMagic = 0x49434653; // SFCI
        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;
            totalSize = (totalSize + 1) & ~1;

            int outPointerSizeTableOffset = totalSize;
            int outPointerSizeTableSize = format.OutAutoBuffersCount + format.OutPointersCount;

            totalSize += sizeof(ushort) * outPointerSizeTableSize;

            int rawDataSizeInWords = (totalSize + sizeof(uint) - 1) / sizeof(uint);

            CmifRequest request = new()
            {
                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,
                }),
            };

            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
                {
                    Type = CmifDomainRequestType.SendMessage,
                    ObjectsCount = (byte)format.ObjectsCount,
                    DataSize = (ushort)payloadSize,
                    ObjectId = format.ObjectId,
                    Padding = 0,
                    Token = format.Context,
                };

                data = data[(Unsafe.SizeOf<CmifDomainInHeader>() / sizeof(uint))..];

                request.Objects = data[((payloadSize + sizeof(uint) - 1) / sizeof(uint))..];
            }

            ref CmifInHeader header = ref MemoryMarshal.Cast<uint, CmifInHeader>(data)[0];

            header = new CmifInHeader
            {
                Magic = CmifInHeaderMagic,
                Version = format.Context != 0 ? 1u : 0u,
                CommandId = format.RequestId,
                Token = format.ObjectId != 0 ? 0u : format.Context,
            };

            request.Data = MemoryMarshal.Cast<uint, byte>(data)[Unsafe.SizeOf<CmifInHeader>()..];

            int paddingSizeBefore = (rawDataSizeInWords - request.Hipc.DataWords.Length) * sizeof(uint);

            Span<byte> outPointerTable = MemoryMarshal.Cast<uint, byte>(request.Hipc.DataWords)[(outPointerSizeTableOffset - paddingSizeBefore)..];

            request.OutPointerSizes = MemoryMarshal.Cast<byte, ushort>(outPointerTable);
            request.ServerPointerSize = format.ServerPointerSize;

            return request;
        }

        public static Result ParseResponse(out CmifResponse response, Span<byte> input, bool isDomain, int size)
        {
            HipcMessage responseMessage = new(input);

            Span<byte> data = MemoryMarshal.Cast<uint, byte>(responseMessage.Data.DataWords);
            Span<uint> objects = Span<uint>.Empty;

            if (isDomain)
            {
                data = data[Unsafe.SizeOf<CmifDomainOutHeader>()..];
                objects = MemoryMarshal.Cast<byte, uint>(data[(Unsafe.SizeOf<CmifOutHeader>() + size)..]);
            }

            CmifOutHeader header = MemoryMarshal.Cast<byte, CmifOutHeader>(data)[0];

            if (header.Magic != CmifOutHeaderMagic)
            {
                response = default;

                return SfResult.InvalidOutHeader;
            }

            if (header.Result.IsFailure)
            {
                response = default;

                return header.Result;
            }

            response = new CmifResponse
            {
                Data = data[Unsafe.SizeOf<CmifOutHeader>()..],
                Objects = objects,
                CopyHandles = responseMessage.Data.CopyHandles,
                MoveHandles = responseMessage.Data.MoveHandles,
            };

            return Result.Success;
        }
    }
}