2021-12-23 17:55:50 +01:00
|
|
|
using LibHac.Common;
|
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; }
|
2022-02-22 14:32:10 +01: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-08 00:41:02 +02:00
|
|
|
|
2020-12-13 08:30:27 +01:00
|
|
|
public uint TextOffset { get; }
|
2021-09-15 01:24:49 +02:00
|
|
|
public uint RoOffset { get; }
|
2020-12-13 08:30:27 +01:00
|
|
|
public uint DataOffset { get; }
|
2021-09-15 01:24:49 +02:00
|
|
|
public uint BssOffset { get; }
|
2020-07-09 06:31:15 +02:00
|
|
|
|
2020-12-13 08:30:27 +01:00
|
|
|
public uint TextSize { get; }
|
2021-09-15 01:24:49 +02:00
|
|
|
public uint RoSize { get; }
|
2020-12-13 08:30:27 +01:00
|
|
|
public uint DataSize { get; }
|
2021-09-15 01:24:49 +02:00
|
|
|
public uint BssSize { get; }
|
2020-04-08 00:41:02 +02:00
|
|
|
|
2021-09-15 01:24:49 +02:00
|
|
|
public int[] Capabilities { get; }
|
|
|
|
public bool UsesSecureMemory { get; }
|
2020-07-04 01:58:01 +02:00
|
|
|
public bool Is64BitAddressSpace { get; }
|
2021-09-15 01:24:49 +02: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 22:08:59 +02:00
|
|
|
|
2021-12-23 17:55:50 +01:00
|
|
|
public KipExecutable(in SharedRef<IStorage> inStorage)
|
2020-04-08 00:41:02 +02:00
|
|
|
{
|
2020-07-04 01:58:01 +02:00
|
|
|
KipReader reader = new KipReader();
|
|
|
|
|
2021-12-23 17:55:50 +01:00
|
|
|
reader.Initialize(in inStorage).ThrowIfFailure();
|
2020-07-04 01:58:01 +02:00
|
|
|
|
2020-12-13 08:30:27 +01:00
|
|
|
TextOffset = (uint)reader.Segments[0].MemoryOffset;
|
2021-09-15 01:24:49 +02:00
|
|
|
RoOffset = (uint)reader.Segments[1].MemoryOffset;
|
2020-12-13 08:30:27 +01:00
|
|
|
DataOffset = (uint)reader.Segments[2].MemoryOffset;
|
2021-09-15 01:24:49 +02:00
|
|
|
BssOffset = (uint)reader.Segments[3].MemoryOffset;
|
|
|
|
BssSize = (uint)reader.Segments[3].Size;
|
2020-07-04 01:58:01 +02:00
|
|
|
|
|
|
|
StackSize = reader.StackSize;
|
|
|
|
|
2021-09-15 01:24:49 +02:00
|
|
|
UsesSecureMemory = reader.UsesSecureMemory;
|
2020-07-04 01:58:01 +02:00
|
|
|
Is64BitAddressSpace = reader.Is64BitAddressSpace;
|
2021-09-15 01:24:49 +02:00
|
|
|
Is64Bit = reader.Is64Bit;
|
2020-07-04 01:58:01 +02:00
|
|
|
|
2021-09-15 01:24:49 +02:00
|
|
|
ProgramId = reader.ProgramId;
|
|
|
|
Priority = reader.Priority;
|
2020-07-04 01:58:01 +02:00
|
|
|
IdealCoreId = reader.IdealCoreId;
|
2021-09-15 01:24:49 +02:00
|
|
|
Version = reader.Version;
|
|
|
|
Name = reader.Name.ToString();
|
2020-07-04 01:58:01 +02:00
|
|
|
|
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();
|
|
|
|
|
2022-02-22 14:32:10 +01:00
|
|
|
var span = program.AsSpan((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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|