2020-07-09 06:31:15 +02:00
|
|
|
using LibHac.Common;
|
2020-04-08 00:41:02 +02:00
|
|
|
using LibHac.Fs;
|
2020-07-04 01:58:01 +02:00
|
|
|
using LibHac.FsSystem;
|
|
|
|
using LibHac.Loader;
|
2020-11-06 19:58:57 +01:00
|
|
|
using Ryujinx.Common.Logging;
|
2020-07-09 06:31:15 +02:00
|
|
|
using System;
|
2020-11-06 19:58:57 +01:00
|
|
|
using System.Text;
|
|
|
|
using System.Text.RegularExpressions;
|
2020-04-08 00:41:02 +02:00
|
|
|
|
|
|
|
namespace Ryujinx.HLE.Loaders.Executables
|
|
|
|
{
|
2020-07-04 01:58:01 +02:00
|
|
|
class NsoExecutable : 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 => DataOffset + (uint)Data.Length;
|
2020-04-08 00:41:02 +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
|
|
|
|
2020-11-06 19:58:57 +01:00
|
|
|
public string Name;
|
2020-07-09 06:31:15 +02:00
|
|
|
public Buffer32 BuildId;
|
|
|
|
|
|
|
|
public NsoExecutable(IStorage inStorage, string name = null)
|
2020-04-08 00:41:02 +02:00
|
|
|
{
|
2020-07-04 01:58:01 +02:00
|
|
|
NsoReader reader = new NsoReader();
|
|
|
|
|
|
|
|
reader.Initialize(inStorage.AsFile(OpenMode.Read)).ThrowIfFailure();
|
|
|
|
|
2020-12-13 08:30:27 +01:00
|
|
|
TextOffset = reader.Header.Segments[0].MemoryOffset;
|
|
|
|
RoOffset = reader.Header.Segments[1].MemoryOffset;
|
|
|
|
DataOffset = reader.Header.Segments[2].MemoryOffset;
|
|
|
|
BssSize = reader.Header.BssSize;
|
2020-07-04 01:58:01 +02:00
|
|
|
|
2020-07-09 06:31:15 +02:00
|
|
|
reader.GetSegmentSize(NsoReader.SegmentType.Data, out uint uncompressedSize).ThrowIfFailure();
|
2020-11-06 19:58:57 +01:00
|
|
|
|
2020-07-09 06:31:15 +02:00
|
|
|
Program = new byte[DataOffset + uncompressedSize];
|
|
|
|
|
2020-11-06 19:58:57 +01:00
|
|
|
TextSize = DecompressSection(reader, NsoReader.SegmentType.Text, TextOffset);
|
|
|
|
RoSize = DecompressSection(reader, NsoReader.SegmentType.Ro, RoOffset);
|
|
|
|
DataSize = DecompressSection(reader, NsoReader.SegmentType.Data, DataOffset);
|
2020-07-09 06:31:15 +02:00
|
|
|
|
2020-11-06 19:58:57 +01:00
|
|
|
Name = name;
|
2020-07-09 06:31:15 +02:00
|
|
|
BuildId = reader.Header.ModuleId;
|
2020-11-06 19:58:57 +01:00
|
|
|
|
|
|
|
PrintRoSectionInfo();
|
2020-07-04 01:58:01 +02:00
|
|
|
}
|
|
|
|
|
2020-12-13 08:30:27 +01:00
|
|
|
private uint DecompressSection(NsoReader reader, NsoReader.SegmentType segmentType, uint offset)
|
2020-07-04 01:58:01 +02:00
|
|
|
{
|
|
|
|
reader.GetSegmentSize(segmentType, out uint uncompressedSize).ThrowIfFailure();
|
|
|
|
|
2020-12-13 08:30:27 +01:00
|
|
|
var span = Program.AsSpan().Slice((int)offset, (int)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 uncompressedSize;
|
2020-04-08 00:41:02 +02:00
|
|
|
}
|
2020-11-06 19:58:57 +01:00
|
|
|
|
|
|
|
private void PrintRoSectionInfo()
|
|
|
|
{
|
2021-03-29 18:31:16 +02:00
|
|
|
string rawTextBuffer = Encoding.ASCII.GetString(Ro);
|
2020-11-06 19:58:57 +01:00
|
|
|
StringBuilder stringBuilder = new StringBuilder();
|
|
|
|
|
2021-03-29 18:31:16 +02:00
|
|
|
string modulePath = null;
|
2020-11-18 19:04:42 +01:00
|
|
|
|
2021-03-29 18:31:16 +02:00
|
|
|
if (BitConverter.ToInt32(Ro.Slice(0, 4)) == 0)
|
2020-11-06 19:58:57 +01:00
|
|
|
{
|
2021-03-29 18:31:16 +02:00
|
|
|
int length = BitConverter.ToInt32(Ro.Slice(4, 4));
|
|
|
|
if (length > 0)
|
2020-11-18 19:04:42 +01:00
|
|
|
{
|
2021-03-29 18:31:16 +02:00
|
|
|
modulePath = Encoding.UTF8.GetString(Ro.Slice(8, length));
|
2020-11-18 19:04:42 +01:00
|
|
|
}
|
2021-03-29 18:31:16 +02:00
|
|
|
}
|
2020-11-06 19:58:57 +01:00
|
|
|
|
2021-03-29 18:31:16 +02:00
|
|
|
if (string.IsNullOrEmpty(modulePath))
|
|
|
|
{
|
|
|
|
Match moduleMatch = Regex.Match(rawTextBuffer, @"[a-z]:[\\/][ -~]{5,}\.nss", RegexOptions.IgnoreCase | RegexOptions.CultureInvariant | RegexOptions.Compiled);
|
|
|
|
if (moduleMatch.Success)
|
|
|
|
{
|
|
|
|
modulePath = moduleMatch.Value;
|
|
|
|
}
|
2020-11-18 19:04:42 +01:00
|
|
|
}
|
2020-11-06 19:58:57 +01:00
|
|
|
|
2021-03-29 18:31:16 +02:00
|
|
|
stringBuilder.AppendLine($" Module: {modulePath}");
|
|
|
|
|
|
|
|
Match fsSdkMatch = Regex.Match(rawTextBuffer, @"sdk_version: ([0-9.]*)", RegexOptions.Compiled);
|
|
|
|
if (fsSdkMatch.Success)
|
2020-11-06 19:58:57 +01:00
|
|
|
{
|
2021-03-29 18:31:16 +02:00
|
|
|
stringBuilder.AppendLine($" FS SDK Version: {fsSdkMatch.Value.Replace("sdk_version: ", "")}");
|
2020-11-06 19:58:57 +01:00
|
|
|
}
|
|
|
|
|
2021-03-29 18:31:16 +02:00
|
|
|
MatchCollection sdkMwMatches = Regex.Matches(rawTextBuffer, @"SDK MW[ -~]*", RegexOptions.Compiled);
|
2020-11-06 19:58:57 +01:00
|
|
|
if (sdkMwMatches.Count != 0)
|
|
|
|
{
|
|
|
|
string libHeader = " SDK Libraries: ";
|
|
|
|
string libContent = string.Join($"\n{new string(' ', libHeader.Length)}", sdkMwMatches);
|
|
|
|
|
|
|
|
stringBuilder.AppendLine($"{libHeader}{libContent}");
|
|
|
|
}
|
|
|
|
|
2020-11-18 19:04:42 +01:00
|
|
|
if (stringBuilder.Length > 0)
|
|
|
|
{
|
|
|
|
Logger.Info?.Print(LogClass.Loader, $"{Name}:\n{stringBuilder.ToString().TrimEnd('\r', '\n')}");
|
|
|
|
}
|
2020-11-06 19:58:57 +01:00
|
|
|
}
|
2020-04-08 00:41:02 +02:00
|
|
|
}
|
|
|
|
}
|