2021-12-23 08:55:50 -08:00
|
|
|
using LibHac.Common;
|
2020-04-07 15:41:02 -07:00
|
|
|
using LibHac.Fs;
|
2020-09-01 13:08:59 -07:00
|
|
|
using LibHac.Kernel;
|
2020-07-08 21:31:15 -07:00
|
|
|
using System;
|
2020-04-07 15:41:02 -07:00
|
|
|
|
|
|
|
namespace Ryujinx.HLE.Loaders.Executables
|
|
|
|
{
|
2020-07-03 16:58:01 -07:00
|
|
|
class KipExecutable : IExecutable
|
2020-04-07 15:41:02 -07:00
|
|
|
{
|
2020-07-08 21:31:15 -07:00
|
|
|
public byte[] Program { get; }
|
2022-02-22 05:32:10 -08:00
|
|
|
public Span<byte> Text => Program.AsSpan((int)TextOffset, (int)TextSize);
|
|
|
|
public Span<byte> Ro => Program.AsSpan((int)RoOffset, (int)RoSize);
|
|
|
|
public Span<byte> Data => Program.AsSpan((int)DataOffset, (int)DataSize);
|
2020-04-07 15:41:02 -07:00
|
|
|
|
2020-12-12 23:30:27 -08:00
|
|
|
public uint TextOffset { get; }
|
2021-09-14 16:24:49 -07:00
|
|
|
public uint RoOffset { get; }
|
2020-12-12 23:30:27 -08:00
|
|
|
public uint DataOffset { get; }
|
2021-09-14 16:24:49 -07:00
|
|
|
public uint BssOffset { get; }
|
2020-07-08 21:31:15 -07:00
|
|
|
|
2020-12-12 23:30:27 -08:00
|
|
|
public uint TextSize { get; }
|
2021-09-14 16:24:49 -07:00
|
|
|
public uint RoSize { get; }
|
2020-12-12 23:30:27 -08:00
|
|
|
public uint DataSize { get; }
|
2021-09-14 16:24:49 -07:00
|
|
|
public uint BssSize { get; }
|
2020-04-07 15:41:02 -07:00
|
|
|
|
2021-09-14 16:24:49 -07:00
|
|
|
public int[] Capabilities { get; }
|
|
|
|
public bool UsesSecureMemory { get; }
|
2020-07-03 16:58:01 -07:00
|
|
|
public bool Is64BitAddressSpace { get; }
|
2021-09-14 16:24:49 -07:00
|
|
|
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 13:08:59 -07:00
|
|
|
|
2021-12-23 08:55:50 -08:00
|
|
|
public KipExecutable(in SharedRef<IStorage> inStorage)
|
2020-04-07 15:41:02 -07:00
|
|
|
{
|
2020-07-03 16:58:01 -07:00
|
|
|
KipReader reader = new KipReader();
|
|
|
|
|
2021-12-23 08:55:50 -08:00
|
|
|
reader.Initialize(in inStorage).ThrowIfFailure();
|
2020-07-03 16:58:01 -07:00
|
|
|
|
2020-12-12 23:30:27 -08:00
|
|
|
TextOffset = (uint)reader.Segments[0].MemoryOffset;
|
2021-09-14 16:24:49 -07:00
|
|
|
RoOffset = (uint)reader.Segments[1].MemoryOffset;
|
2020-12-12 23:30:27 -08:00
|
|
|
DataOffset = (uint)reader.Segments[2].MemoryOffset;
|
2021-09-14 16:24:49 -07:00
|
|
|
BssOffset = (uint)reader.Segments[3].MemoryOffset;
|
|
|
|
BssSize = (uint)reader.Segments[3].Size;
|
2020-07-03 16:58:01 -07:00
|
|
|
|
|
|
|
StackSize = reader.StackSize;
|
|
|
|
|
2021-09-14 16:24:49 -07:00
|
|
|
UsesSecureMemory = reader.UsesSecureMemory;
|
2020-07-03 16:58:01 -07:00
|
|
|
Is64BitAddressSpace = reader.Is64BitAddressSpace;
|
2021-09-14 16:24:49 -07:00
|
|
|
Is64Bit = reader.Is64Bit;
|
2020-07-03 16:58:01 -07:00
|
|
|
|
2021-09-14 16:24:49 -07:00
|
|
|
ProgramId = reader.ProgramId;
|
|
|
|
Priority = reader.Priority;
|
2020-07-03 16:58:01 -07:00
|
|
|
IdealCoreId = reader.IdealCoreId;
|
2021-09-14 16:24:49 -07:00
|
|
|
Version = reader.Version;
|
|
|
|
Name = reader.Name.ToString();
|
2020-07-03 16:58:01 -07:00
|
|
|
|
2020-04-07 15:41:02 -07:00
|
|
|
Capabilities = new int[32];
|
|
|
|
|
|
|
|
for (int index = 0; index < Capabilities.Length; index++)
|
|
|
|
{
|
2020-07-03 16:58:01 -07:00
|
|
|
Capabilities[index] = (int)reader.Capabilities[index];
|
2020-04-07 15:41:02 -07:00
|
|
|
}
|
|
|
|
|
2020-07-08 21:31:15 -07: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 16:58:01 -07:00
|
|
|
}
|
|
|
|
|
2020-12-12 23:30:27 -08:00
|
|
|
private static uint DecompressSection(KipReader reader, KipReader.SegmentType segmentType, uint offset, byte[] program)
|
2020-07-03 16:58:01 -07:00
|
|
|
{
|
|
|
|
reader.GetSegmentSize(segmentType, out int uncompressedSize).ThrowIfFailure();
|
|
|
|
|
2022-02-22 05:32:10 -08:00
|
|
|
var span = program.AsSpan((int)offset, uncompressedSize);
|
2020-07-03 16:58:01 -07:00
|
|
|
|
2020-07-08 21:31:15 -07:00
|
|
|
reader.ReadSegment(segmentType, span).ThrowIfFailure();
|
2020-07-03 16:58:01 -07:00
|
|
|
|
2020-12-12 23:30:27 -08:00
|
|
|
return (uint)uncompressedSize;
|
2020-04-07 15:41:02 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|