2020-04-08 00:41:02 +02:00
|
|
|
using LibHac.Fs;
|
2020-09-01 22:08:59 +02:00
|
|
|
using LibHac.Kernel;
|
2020-07-09 06:31:15 +02:00
|
|
|
using System;
|
2020-04-08 00:41:02 +02:00
|
|
|
|
|
|
|
namespace Ryujinx.HLE.Loaders.Executables
|
|
|
|
{
|
2020-07-04 01:58:01 +02:00
|
|
|
class KipExecutable : IExecutable
|
2020-04-08 00:41:02 +02:00
|
|
|
{
|
2020-07-09 06:31:15 +02:00
|
|
|
public byte[] Program { get; }
|
2020-12-13 08:30:27 +01:00
|
|
|
public Span<byte> Text => Program.AsSpan().Slice((int)TextOffset, (int)TextSize);
|
|
|
|
public Span<byte> Ro => Program.AsSpan().Slice((int)RoOffset, (int)RoSize);
|
|
|
|
public Span<byte> Data => Program.AsSpan().Slice((int)DataOffset, (int)DataSize);
|
2020-04-08 00:41:02 +02:00
|
|
|
|
2020-12-13 08:30:27 +01:00
|
|
|
public uint TextOffset { get; }
|
|
|
|
public uint RoOffset { get; }
|
|
|
|
public uint DataOffset { get; }
|
|
|
|
public uint BssOffset { get; }
|
2020-07-09 06:31:15 +02:00
|
|
|
|
2020-12-13 08:30:27 +01:00
|
|
|
public uint TextSize { get; }
|
|
|
|
public uint RoSize { get; }
|
|
|
|
public uint DataSize { get; }
|
|
|
|
public uint BssSize { get; }
|
2020-04-08 00:41:02 +02:00
|
|
|
|
|
|
|
public int[] Capabilities { get; }
|
2020-07-04 01:58:01 +02: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; }
|
2020-09-01 22:08:59 +02:00
|
|
|
|
2020-07-04 01:58:01 +02:00
|
|
|
public KipExecutable(IStorage inStorage)
|
2020-04-08 00:41:02 +02:00
|
|
|
{
|
2020-07-04 01:58:01 +02:00
|
|
|
KipReader reader = new KipReader();
|
|
|
|
|
|
|
|
reader.Initialize(inStorage).ThrowIfFailure();
|
|
|
|
|
2020-12-13 08:30:27 +01:00
|
|
|
TextOffset = (uint)reader.Segments[0].MemoryOffset;
|
|
|
|
RoOffset = (uint)reader.Segments[1].MemoryOffset;
|
|
|
|
DataOffset = (uint)reader.Segments[2].MemoryOffset;
|
|
|
|
BssOffset = (uint)reader.Segments[3].MemoryOffset;
|
|
|
|
BssSize = (uint)reader.Segments[3].Size;
|
2020-07-04 01:58:01 +02:00
|
|
|
|
|
|
|
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-08 00:41:02 +02:00
|
|
|
Capabilities = new int[32];
|
|
|
|
|
|
|
|
for (int index = 0; index < Capabilities.Length; index++)
|
|
|
|
{
|
2020-07-04 01:58:01 +02:00
|
|
|
Capabilities[index] = (int)reader.Capabilities[index];
|
2020-04-08 00:41:02 +02:00
|
|
|
}
|
|
|
|
|
2020-07-09 06:31:15 +02: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-04 01:58:01 +02:00
|
|
|
}
|
|
|
|
|
2020-12-13 08:30:27 +01:00
|
|
|
private static uint DecompressSection(KipReader reader, KipReader.SegmentType segmentType, uint offset, byte[] program)
|
2020-07-04 01:58:01 +02:00
|
|
|
{
|
|
|
|
reader.GetSegmentSize(segmentType, out int uncompressedSize).ThrowIfFailure();
|
|
|
|
|
2020-12-13 08:30:27 +01:00
|
|
|
var span = program.AsSpan().Slice((int)offset, uncompressedSize);
|
2020-07-04 01:58:01 +02:00
|
|
|
|
2020-07-09 06:31:15 +02:00
|
|
|
reader.ReadSegment(segmentType, span).ThrowIfFailure();
|
2020-07-04 01:58:01 +02:00
|
|
|
|
2020-12-13 08:30:27 +01:00
|
|
|
return (uint)uncompressedSize;
|
2020-04-08 00:41:02 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|