2018-11-28 22:18:09 +00:00
|
|
|
using Ryujinx.HLE.Loaders.Compression;
|
|
|
|
using System.IO;
|
|
|
|
|
|
|
|
namespace Ryujinx.HLE.Loaders.Executables
|
|
|
|
{
|
|
|
|
class KernelInitialProcess : IExecutable
|
|
|
|
{
|
2018-12-05 00:52:39 +00:00
|
|
|
public string Name { get; private set; }
|
2018-11-28 22:18:09 +00:00
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
public long TitleId { get; private set; }
|
2018-11-28 22:18:09 +00:00
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
public int ProcessCategory { get; private set; }
|
2018-11-28 22:18:09 +00:00
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
public byte MainThreadPriority { get; private set; }
|
|
|
|
public byte DefaultProcessorId { get; private set; }
|
2018-11-28 22:18:09 +00:00
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
public bool Is64Bits { get; private set; }
|
|
|
|
public bool Addr39Bits { get; private set; }
|
|
|
|
public bool IsService { get; private set; }
|
2018-11-28 22:18:09 +00:00
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
public byte[] Text { get; private set; }
|
|
|
|
public byte[] RO { get; private set; }
|
|
|
|
public byte[] Data { get; private set; }
|
2018-11-28 22:18:09 +00:00
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
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; }
|
2018-11-28 22:18:09 +00:00
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
public int MainThreadStackSize { get; private set; }
|
2018-11-28 22:18:09 +00:00
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
public int[] Capabilities { get; private set; }
|
2018-11-28 22:18:09 +00:00
|
|
|
|
|
|
|
private struct SegmentHeader
|
|
|
|
{
|
2018-12-05 00:52:39 +00:00
|
|
|
public int Offset { get; private set; }
|
|
|
|
public int DecompressedSize { get; private set; }
|
|
|
|
public int CompressedSize { get; private set; }
|
|
|
|
public int Attribute { get; private set; }
|
2018-11-28 22:18:09 +00:00
|
|
|
|
|
|
|
public SegmentHeader(
|
2018-12-05 00:52:39 +00:00
|
|
|
int Offset,
|
|
|
|
int DecompressedSize,
|
|
|
|
int CompressedSize,
|
|
|
|
int Attribute)
|
2018-11-28 22:18:09 +00:00
|
|
|
{
|
2018-12-05 00:52:39 +00:00
|
|
|
this.Offset = Offset;
|
|
|
|
this.DecompressedSize = DecompressedSize;
|
|
|
|
this.CompressedSize = CompressedSize;
|
|
|
|
this.Attribute = Attribute;
|
2018-11-28 22:18:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
public KernelInitialProcess(Stream Input)
|
2018-11-28 22:18:09 +00:00
|
|
|
{
|
2018-12-05 00:52:39 +00:00
|
|
|
BinaryReader Reader = new BinaryReader(Input);
|
2018-11-28 22:18:09 +00:00
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
string Magic = ReadString(Reader, 4);
|
2018-11-28 22:18:09 +00:00
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
if (Magic != "KIP1")
|
2018-11-28 22:18:09 +00:00
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
Name = ReadString(Reader, 12);
|
2018-11-28 22:18:09 +00:00
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
TitleId = Reader.ReadInt64();
|
2018-11-28 22:18:09 +00:00
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
ProcessCategory = Reader.ReadInt32();
|
2018-11-28 22:18:09 +00:00
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
MainThreadPriority = Reader.ReadByte();
|
|
|
|
DefaultProcessorId = Reader.ReadByte();
|
2018-11-28 22:18:09 +00:00
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
byte Reserved = Reader.ReadByte();
|
|
|
|
byte Flags = Reader.ReadByte();
|
2018-11-28 22:18:09 +00:00
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
Is64Bits = (Flags & 0x08) != 0;
|
|
|
|
Addr39Bits = (Flags & 0x10) != 0;
|
|
|
|
IsService = (Flags & 0x20) != 0;
|
2018-11-28 22:18:09 +00:00
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
SegmentHeader[] Segments = new SegmentHeader[6];
|
2018-11-28 22:18:09 +00:00
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
for (int Index = 0; Index < Segments.Length; Index++)
|
2018-11-28 22:18:09 +00:00
|
|
|
{
|
2018-12-05 00:52:39 +00:00
|
|
|
Segments[Index] = new SegmentHeader(
|
|
|
|
Reader.ReadInt32(),
|
|
|
|
Reader.ReadInt32(),
|
|
|
|
Reader.ReadInt32(),
|
|
|
|
Reader.ReadInt32());
|
2018-11-28 22:18:09 +00:00
|
|
|
}
|
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
TextOffset = Segments[0].Offset;
|
|
|
|
ROOffset = Segments[1].Offset;
|
|
|
|
DataOffset = Segments[2].Offset;
|
|
|
|
BssOffset = Segments[3].Offset;
|
|
|
|
BssSize = Segments[3].DecompressedSize;
|
2018-11-28 22:18:09 +00:00
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
MainThreadStackSize = Segments[1].Attribute;
|
2018-11-28 22:18:09 +00:00
|
|
|
|
|
|
|
Capabilities = new int[8];
|
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
for (int Index = 0; Index < Capabilities.Length; Index++)
|
2018-11-28 22:18:09 +00:00
|
|
|
{
|
2018-12-05 00:52:39 +00:00
|
|
|
Capabilities[Index] = Reader.ReadInt32();
|
2018-11-28 22:18:09 +00:00
|
|
|
}
|
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
Input.Seek(0x100, SeekOrigin.Begin);
|
2018-11-28 22:18:09 +00:00
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
Text = ReadSegment(Segments[0], Input);
|
|
|
|
RO = ReadSegment(Segments[1], Input);
|
|
|
|
Data = ReadSegment(Segments[2], Input);
|
2018-11-28 22:18:09 +00:00
|
|
|
}
|
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
private byte[] ReadSegment(SegmentHeader Header, Stream Input)
|
2018-11-28 22:18:09 +00:00
|
|
|
{
|
2018-12-05 00:52:39 +00:00
|
|
|
long End = Input.Position + Header.CompressedSize;
|
2018-11-28 22:18:09 +00:00
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
Input.Seek(End, SeekOrigin.Begin);
|
2018-11-28 22:18:09 +00:00
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
byte[] Data = BackwardsLz.Decompress(Input, Header.DecompressedSize);
|
2018-11-28 22:18:09 +00:00
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
Input.Seek(End, SeekOrigin.Begin);
|
2018-11-28 22:18:09 +00:00
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
return Data;
|
2018-11-28 22:18:09 +00:00
|
|
|
}
|
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
private static string ReadString(BinaryReader Reader, int MaxSize)
|
2018-11-28 22:18:09 +00:00
|
|
|
{
|
2018-12-05 00:52:39 +00:00
|
|
|
string Value = string.Empty;
|
2018-11-28 22:18:09 +00:00
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
for (int Index = 0; Index < MaxSize; Index++)
|
2018-11-28 22:18:09 +00:00
|
|
|
{
|
2018-12-05 00:52:39 +00:00
|
|
|
char Chr = (char)Reader.ReadByte();
|
2018-11-28 22:18:09 +00:00
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
if (Chr == '\0')
|
2018-11-28 22:18:09 +00:00
|
|
|
{
|
2018-12-05 00:52:39 +00:00
|
|
|
Reader.BaseStream.Seek(MaxSize - Index - 1, SeekOrigin.Current);
|
2018-11-28 22:18:09 +00:00
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
Value += Chr;
|
2018-11-28 22:18:09 +00:00
|
|
|
}
|
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
return Value;
|
2018-11-28 22:18:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|