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