2018-11-28 23:18:09 +01:00
|
|
|
using Ryujinx.HLE.Loaders.Compression;
|
|
|
|
using System.IO;
|
|
|
|
|
|
|
|
namespace Ryujinx.HLE.Loaders.Executables
|
|
|
|
{
|
|
|
|
class KernelInitialProcess : IExecutable
|
|
|
|
{
|
2018-12-04 21:23:37 +01:00
|
|
|
public string Name { get; }
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-04 21:23:37 +01:00
|
|
|
public long TitleId { get; }
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-04 21:23:37 +01:00
|
|
|
public int ProcessCategory { get; }
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-04 21:23:37 +01:00
|
|
|
public byte MainThreadPriority { get; }
|
|
|
|
public byte DefaultProcessorId { get; }
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-04 21:23:37 +01:00
|
|
|
public bool Is64Bits { get; }
|
|
|
|
public bool Addr39Bits { get; }
|
|
|
|
public bool IsService { get; }
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-04 21:23:37 +01:00
|
|
|
public byte[] Text { get; }
|
|
|
|
public byte[] Ro { get; }
|
|
|
|
public byte[] Data { get; }
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-04 21:23:37 +01:00
|
|
|
public int TextOffset { get; }
|
|
|
|
public int RoOffset { get; }
|
|
|
|
public int DataOffset { get; }
|
|
|
|
public int BssOffset { get; }
|
|
|
|
public int BssSize { get; }
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-04 21:23:37 +01:00
|
|
|
public int MainThreadStackSize { get; }
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-04 21:23:37 +01:00
|
|
|
public int[] Capabilities { get; }
|
2018-11-28 23:18:09 +01:00
|
|
|
|
|
|
|
private struct SegmentHeader
|
|
|
|
{
|
2018-12-04 21:23:37 +01:00
|
|
|
public int Offset { get; }
|
|
|
|
public int DecompressedSize { get; }
|
|
|
|
public int CompressedSize { get; }
|
|
|
|
public int Attribute { get; }
|
2018-11-28 23:18:09 +01:00
|
|
|
|
|
|
|
public SegmentHeader(
|
2018-12-04 21:23:37 +01:00
|
|
|
int offset,
|
|
|
|
int decompressedSize,
|
|
|
|
int compressedSize,
|
|
|
|
int attribute)
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
2018-12-04 21:23:37 +01:00
|
|
|
Offset = offset;
|
|
|
|
DecompressedSize = decompressedSize;
|
|
|
|
CompressedSize = compressedSize;
|
|
|
|
Attribute = attribute;
|
2018-11-28 23:18:09 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-04 21:23:37 +01:00
|
|
|
public KernelInitialProcess(Stream input)
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
2018-12-04 21:23:37 +01:00
|
|
|
BinaryReader reader = new BinaryReader(input);
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-04 21:23:37 +01:00
|
|
|
string magic = ReadString(reader, 4);
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-04 21:23:37 +01:00
|
|
|
if (magic != "KIP1")
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-12-04 21:23:37 +01:00
|
|
|
Name = ReadString(reader, 12);
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-04 21:23:37 +01:00
|
|
|
TitleId = reader.ReadInt64();
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-04 21:23:37 +01:00
|
|
|
ProcessCategory = reader.ReadInt32();
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-04 21:23:37 +01:00
|
|
|
MainThreadPriority = reader.ReadByte();
|
|
|
|
DefaultProcessorId = reader.ReadByte();
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-04 21:23:37 +01:00
|
|
|
byte reserved = reader.ReadByte();
|
|
|
|
byte flags = reader.ReadByte();
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-04 21:23:37 +01:00
|
|
|
Is64Bits = (flags & 0x08) != 0;
|
|
|
|
Addr39Bits = (flags & 0x10) != 0;
|
|
|
|
IsService = (flags & 0x20) != 0;
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-04 21:23:37 +01:00
|
|
|
SegmentHeader[] segments = new SegmentHeader[6];
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-04 21:23:37 +01:00
|
|
|
for (int index = 0; index < segments.Length; index++)
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
2018-12-04 21:23:37 +01:00
|
|
|
segments[index] = new SegmentHeader(
|
|
|
|
reader.ReadInt32(),
|
|
|
|
reader.ReadInt32(),
|
|
|
|
reader.ReadInt32(),
|
|
|
|
reader.ReadInt32());
|
2018-11-28 23:18:09 +01:00
|
|
|
}
|
|
|
|
|
2018-12-04 21:23:37 +01:00
|
|
|
TextOffset = segments[0].Offset;
|
|
|
|
RoOffset = segments[1].Offset;
|
|
|
|
DataOffset = segments[2].Offset;
|
|
|
|
BssOffset = segments[3].Offset;
|
|
|
|
BssSize = segments[3].DecompressedSize;
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-04 21:23:37 +01:00
|
|
|
MainThreadStackSize = segments[1].Attribute;
|
2018-11-28 23:18:09 +01:00
|
|
|
|
|
|
|
Capabilities = new int[8];
|
|
|
|
|
2018-12-04 21:23:37 +01:00
|
|
|
for (int index = 0; index < Capabilities.Length; index++)
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
2018-12-04 21:23:37 +01:00
|
|
|
Capabilities[index] = reader.ReadInt32();
|
2018-11-28 23:18:09 +01:00
|
|
|
}
|
|
|
|
|
2018-12-04 21:23:37 +01:00
|
|
|
input.Seek(0x100, SeekOrigin.Begin);
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-04 21:23:37 +01:00
|
|
|
Text = ReadSegment(segments[0], input);
|
|
|
|
Ro = ReadSegment(segments[1], input);
|
|
|
|
Data = ReadSegment(segments[2], input);
|
2018-11-28 23:18:09 +01:00
|
|
|
}
|
|
|
|
|
2018-12-04 21:23:37 +01:00
|
|
|
private byte[] ReadSegment(SegmentHeader header, Stream input)
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
2018-12-04 21:23:37 +01:00
|
|
|
long end = input.Position + header.CompressedSize;
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-04 21:23:37 +01:00
|
|
|
input.Seek(end, SeekOrigin.Begin);
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-04 21:23:37 +01:00
|
|
|
byte[] data = BackwardsLz.Decompress(input, header.DecompressedSize);
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-04 21:23:37 +01:00
|
|
|
input.Seek(end, SeekOrigin.Begin);
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-04 21:23:37 +01:00
|
|
|
return data;
|
2018-11-28 23:18:09 +01:00
|
|
|
}
|
|
|
|
|
2018-12-04 21:23:37 +01:00
|
|
|
private static string ReadString(BinaryReader reader, int maxSize)
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
2018-12-04 21:23:37 +01:00
|
|
|
string value = string.Empty;
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-04 21:23:37 +01:00
|
|
|
for (int index = 0; index < maxSize; index++)
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
2018-12-04 21:23:37 +01:00
|
|
|
char chr = (char)reader.ReadByte();
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-04 21:23:37 +01:00
|
|
|
if (chr == '\0')
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
2018-12-04 21:23:37 +01:00
|
|
|
reader.BaseStream.Seek(maxSize - index - 1, SeekOrigin.Current);
|
2018-11-28 23:18:09 +01:00
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2018-12-04 21:23:37 +01:00
|
|
|
value += chr;
|
2018-11-28 23:18:09 +01:00
|
|
|
}
|
|
|
|
|
2018-12-04 21:23:37 +01:00
|
|
|
return value;
|
2018-11-28 23:18:09 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|