2020-04-07 18:41:02 -04:00
|
|
|
using LibHac.Fs;
|
2020-07-03 19:58:01 -04:00
|
|
|
using LibHac.Loader;
|
2020-07-09 00:31:15 -04:00
|
|
|
using System;
|
2020-04-07 18:41:02 -04:00
|
|
|
|
|
|
|
namespace Ryujinx.HLE.Loaders.Executables
|
|
|
|
{
|
2020-07-03 19:58:01 -04:00
|
|
|
class KipExecutable : IExecutable
|
2020-04-07 18:41:02 -04:00
|
|
|
{
|
2020-07-09 00:31:15 -04:00
|
|
|
public byte[] Program { get; }
|
|
|
|
public Span<byte> Text => Program.AsSpan().Slice(TextOffset, TextSize);
|
|
|
|
public Span<byte> Ro => Program.AsSpan().Slice(RoOffset, RoSize);
|
|
|
|
public Span<byte> Data => Program.AsSpan().Slice(DataOffset, DataSize);
|
2020-04-07 18:41:02 -04:00
|
|
|
|
2020-07-03 19:58:01 -04:00
|
|
|
public int TextOffset { get; }
|
|
|
|
public int RoOffset { get; }
|
|
|
|
public int DataOffset { get; }
|
|
|
|
public int BssOffset { get; }
|
2020-07-09 00:31:15 -04:00
|
|
|
|
|
|
|
public int TextSize { get; }
|
|
|
|
public int RoSize { get; }
|
|
|
|
public int DataSize { get; }
|
2020-07-03 19:58:01 -04:00
|
|
|
public int BssSize { get; }
|
2020-04-07 18:41:02 -04:00
|
|
|
|
|
|
|
public int[] Capabilities { get; }
|
2020-07-03 19:58:01 -04:00
|
|
|
public bool UsesSecureMemory { get; }
|
|
|
|
public bool Is64BitAddressSpace { get; }
|
|
|
|
public bool Is64Bit { get; }
|
|
|
|
public ulong ProgramId { get; }
|
|
|
|
public byte Priority { get; }
|
|
|
|
public int StackSize { get; }
|
|
|
|
public byte IdealCoreId { get; }
|
|
|
|
public int Version { get; }
|
|
|
|
public string Name { get; }
|
|
|
|
public KipExecutable(IStorage inStorage)
|
2020-04-07 18:41:02 -04:00
|
|
|
{
|
2020-07-03 19:58:01 -04:00
|
|
|
KipReader reader = new KipReader();
|
|
|
|
|
|
|
|
reader.Initialize(inStorage).ThrowIfFailure();
|
|
|
|
|
|
|
|
TextOffset = reader.Segments[0].MemoryOffset;
|
|
|
|
RoOffset = reader.Segments[1].MemoryOffset;
|
|
|
|
DataOffset = reader.Segments[2].MemoryOffset;
|
|
|
|
BssOffset = reader.Segments[3].MemoryOffset;
|
|
|
|
BssSize = reader.Segments[3].Size;
|
|
|
|
|
|
|
|
StackSize = reader.StackSize;
|
|
|
|
|
|
|
|
UsesSecureMemory = reader.UsesSecureMemory;
|
|
|
|
Is64BitAddressSpace = reader.Is64BitAddressSpace;
|
|
|
|
Is64Bit = reader.Is64Bit;
|
|
|
|
|
|
|
|
ProgramId = reader.ProgramId;
|
|
|
|
Priority = reader.Priority;
|
|
|
|
IdealCoreId = reader.IdealCoreId;
|
|
|
|
Version = reader.Version;
|
|
|
|
Name = reader.Name.ToString();
|
|
|
|
|
2020-04-07 18:41:02 -04:00
|
|
|
Capabilities = new int[32];
|
|
|
|
|
|
|
|
for (int index = 0; index < Capabilities.Length; index++)
|
|
|
|
{
|
2020-07-03 19:58:01 -04:00
|
|
|
Capabilities[index] = (int)reader.Capabilities[index];
|
2020-04-07 18:41:02 -04:00
|
|
|
}
|
|
|
|
|
2020-07-09 00:31:15 -04:00
|
|
|
reader.GetSegmentSize(KipReader.SegmentType.Data, out int uncompressedSize).ThrowIfFailure();
|
|
|
|
Program = new byte[DataOffset + uncompressedSize];
|
|
|
|
|
|
|
|
TextSize = DecompressSection(reader, KipReader.SegmentType.Text, TextOffset, Program);
|
|
|
|
RoSize = DecompressSection(reader, KipReader.SegmentType.Ro, RoOffset, Program);
|
|
|
|
DataSize = DecompressSection(reader, KipReader.SegmentType.Data, DataOffset, Program);
|
2020-07-03 19:58:01 -04:00
|
|
|
}
|
|
|
|
|
2020-07-09 00:31:15 -04:00
|
|
|
private static int DecompressSection(KipReader reader, KipReader.SegmentType segmentType, int offset, byte[] Program)
|
2020-07-03 19:58:01 -04:00
|
|
|
{
|
|
|
|
reader.GetSegmentSize(segmentType, out int uncompressedSize).ThrowIfFailure();
|
|
|
|
|
2020-07-09 00:31:15 -04:00
|
|
|
var span = Program.AsSpan().Slice(offset, uncompressedSize);
|
2020-07-03 19:58:01 -04:00
|
|
|
|
2020-07-09 00:31:15 -04:00
|
|
|
reader.ReadSegment(segmentType, span).ThrowIfFailure();
|
2020-07-03 19:58:01 -04:00
|
|
|
|
2020-07-09 00:31:15 -04:00
|
|
|
return uncompressedSize;
|
2020-04-07 18:41:02 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|