aboutsummaryrefslogtreecommitdiff
path: root/Ryujinx.HLE/Loaders/Executables/KernelInitialProcess.cs
blob: af57cf2d06cc50a0cc8b97267ec2f0a721d1f4c5 (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
136
137
138
139
140
141
142
143
144
145
146
147
using Ryujinx.HLE.Loaders.Compression;
using System.IO;

namespace Ryujinx.HLE.Loaders.Executables
{
    class KernelInitialProcess : IExecutable
    {
        public string Name { get; private set; }

        public long TitleId { get; private set; }

        public int ProcessCategory { get; private set; }

        public byte MainThreadPriority { get; private set; }
        public byte DefaultProcessorId { get; private set; }

        public bool Is64Bits   { get; private set; }
        public bool Addr39Bits { get; private set; }
        public bool IsService  { get; private set; }

        public byte[] Text { get; private set; }
        public byte[] Ro   { get; private set; }
        public byte[] Data { get; private set; }

        public int TextOffset { get; private set; }
        public int RoOffset   { get; private set; }
        public int DataOffset { get; private set; }
        public int BssOffset  { get; private set; }
        public int BssSize    { get; private set; }

        public int MainThreadStackSize { get; private set; }

        public int[] Capabilities { get; private set; }

        private struct SegmentHeader
        {
            public int Offset           { get; private set; }
            public int DecompressedSize { get; private set; }
            public int CompressedSize   { get; private set; }
            public int Attribute        { get; private set; }

            public SegmentHeader(
                int offset,
                int decompressedSize,
                int compressedSize,
                int attribute)
            {
                Offset           = offset;
                DecompressedSize = decompressedSize;
                CompressedSize   = compressedSize;
                Attribute        = attribute;
            }
        }

        public KernelInitialProcess(Stream input)
        {
            BinaryReader reader = new BinaryReader(input);

            string magic = ReadString(reader, 4);

            if (magic != "KIP1")
            {

            }

            Name = ReadString(reader, 12);

            TitleId = reader.ReadInt64();

            ProcessCategory = reader.ReadInt32();

            MainThreadPriority = reader.ReadByte();
            DefaultProcessorId = reader.ReadByte();

            byte reserved = reader.ReadByte();
            byte flags    = reader.ReadByte();

            Is64Bits   = (flags & 0x08) != 0;
            Addr39Bits = (flags & 0x10) != 0;
            IsService  = (flags & 0x20) != 0;

            SegmentHeader[] segments = new SegmentHeader[6];

            for (int index = 0; index < segments.Length; index++)
            {
                segments[index] = new SegmentHeader(
                    reader.ReadInt32(),
                    reader.ReadInt32(),
                    reader.ReadInt32(),
                    reader.ReadInt32());
            }

            TextOffset = segments[0].Offset;
            RoOffset   = segments[1].Offset;
            DataOffset = segments[2].Offset;
            BssOffset  = segments[3].Offset;
            BssSize    = segments[3].DecompressedSize;

            MainThreadStackSize = segments[1].Attribute;

            Capabilities = new int[32];

            for (int index = 0; index < Capabilities.Length; index++)
            {
                Capabilities[index] = reader.ReadInt32();
            }

            input.Seek(0x100, SeekOrigin.Begin);

            Text = ReadSegment(segments[0], input);
            Ro   = ReadSegment(segments[1], input);
            Data = ReadSegment(segments[2], input);
        }

        private byte[] ReadSegment(SegmentHeader header, Stream input)
        {
            byte[] data = new byte[header.DecompressedSize];

            input.Read(data, 0, header.CompressedSize);

            BackwardsLz.DecompressInPlace(data, header.CompressedSize);

            return data;
        }

        private static string ReadString(BinaryReader reader, int maxSize)
        {
            string value = string.Empty;

            for (int index = 0; index < maxSize; index++)
            {
                char chr = (char)reader.ReadByte();

                if (chr == '\0')
                {
                    reader.BaseStream.Seek(maxSize - index - 1, SeekOrigin.Current);

                    break;
                }

                value += chr;
            }

            return value;
        }
    }
}