diff options
Diffstat (limited to 'Ryujinx.HLE/Loaders')
| -rw-r--r-- | Ryujinx.HLE/Loaders/Executables/IExecutable.cs | 9 | ||||
| -rw-r--r-- | Ryujinx.HLE/Loaders/Executables/KipExecutable.cs | 30 | ||||
| -rw-r--r-- | Ryujinx.HLE/Loaders/Executables/NroExecutable.cs | 71 | ||||
| -rw-r--r-- | Ryujinx.HLE/Loaders/Executables/NsoExecutable.cs | 37 | ||||
| -rw-r--r-- | Ryujinx.HLE/Loaders/Mods/IPSPatcher.cs | 117 | ||||
| -rw-r--r-- | Ryujinx.HLE/Loaders/Mods/IPSwitchPatcher.cs | 262 | ||||
| -rw-r--r-- | Ryujinx.HLE/Loaders/Mods/MemPatch.cs | 98 | ||||
| -rw-r--r-- | Ryujinx.HLE/Loaders/Npdm/Npdm.cs | 4 |
8 files changed, 551 insertions, 77 deletions
diff --git a/Ryujinx.HLE/Loaders/Executables/IExecutable.cs b/Ryujinx.HLE/Loaders/Executables/IExecutable.cs index 440e8f5f..76a550df 100644 --- a/Ryujinx.HLE/Loaders/Executables/IExecutable.cs +++ b/Ryujinx.HLE/Loaders/Executables/IExecutable.cs @@ -1,10 +1,13 @@ +using System; + namespace Ryujinx.HLE.Loaders.Executables { interface IExecutable { - byte[] Text { get; } - byte[] Ro { get; } - byte[] Data { get; } + byte[] Program { get; } + Span<byte> Text { get; } + Span<byte> Ro { get; } + Span<byte> Data { get; } int TextOffset { get; } int RoOffset { get; } diff --git a/Ryujinx.HLE/Loaders/Executables/KipExecutable.cs b/Ryujinx.HLE/Loaders/Executables/KipExecutable.cs index 0f1309c0..a44b7c48 100644 --- a/Ryujinx.HLE/Loaders/Executables/KipExecutable.cs +++ b/Ryujinx.HLE/Loaders/Executables/KipExecutable.cs @@ -1,18 +1,24 @@ using LibHac.Fs; using LibHac.Loader; +using System; namespace Ryujinx.HLE.Loaders.Executables { class KipExecutable : IExecutable { - public byte[] Text { get; } - public byte[] Ro { get; } - public byte[] Data { get; } + public byte[] Program { get; } + public Span<byte> Text => Program.AsSpan().Slice(TextOffset, TextSize); + public Span<byte> Ro => Program.AsSpan().Slice(RoOffset, RoSize); + public Span<byte> Data => Program.AsSpan().Slice(DataOffset, DataSize); public int TextOffset { get; } public int RoOffset { get; } public int DataOffset { get; } public int BssOffset { get; } + + public int TextSize { get; } + public int RoSize { get; } + public int DataSize { get; } public int BssSize { get; } public int[] Capabilities { get; } @@ -25,7 +31,6 @@ namespace Ryujinx.HLE.Loaders.Executables public byte IdealCoreId { get; } public int Version { get; } public string Name { get; } - public KipExecutable(IStorage inStorage) { KipReader reader = new KipReader(); @@ -57,20 +62,23 @@ namespace Ryujinx.HLE.Loaders.Executables Capabilities[index] = (int)reader.Capabilities[index]; } - Text = DecompressSection(reader, KipReader.SegmentType.Text); - Ro = DecompressSection(reader, KipReader.SegmentType.Ro); - Data = DecompressSection(reader, KipReader.SegmentType.Data); + 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); } - private static byte[] DecompressSection(KipReader reader, KipReader.SegmentType segmentType) + private static int DecompressSection(KipReader reader, KipReader.SegmentType segmentType, int offset, byte[] Program) { reader.GetSegmentSize(segmentType, out int uncompressedSize).ThrowIfFailure(); - byte[] result = new byte[uncompressedSize]; + var span = Program.AsSpan().Slice(offset, uncompressedSize); - reader.ReadSegment(segmentType, result).ThrowIfFailure(); + reader.ReadSegment(segmentType, span).ThrowIfFailure(); - return result; + return uncompressedSize; } } }
\ No newline at end of file diff --git a/Ryujinx.HLE/Loaders/Executables/NroExecutable.cs b/Ryujinx.HLE/Loaders/Executables/NroExecutable.cs index 4a7f2116..b7a887b7 100644 --- a/Ryujinx.HLE/Loaders/Executables/NroExecutable.cs +++ b/Ryujinx.HLE/Loaders/Executables/NroExecutable.cs @@ -1,67 +1,38 @@ -using System.IO; +using LibHac; +using LibHac.Fs; +using System; namespace Ryujinx.HLE.Loaders.Executables { - class NroExecutable : IExecutable + class NroExecutable : Nro, IExecutable { - public byte[] Text { get; private set; } - public byte[] Ro { get; private set; } - public byte[] Data { get; private set; } + public byte[] Program { get; } + public Span<byte> Text => Program.AsSpan().Slice(TextOffset, (int)Header.NroSegments[0].Size); + public Span<byte> Ro => Program.AsSpan().Slice(RoOffset, (int)Header.NroSegments[1].Size); + public Span<byte> Data => Program.AsSpan().Slice(DataOffset, (int)Header.NroSegments[2].Size); - public int Mod0Offset { get; private set; } - public int TextOffset { get; private set; } - public int RoOffset { get; private set; } - public int DataOffset { get; private set; } - public int BssSize { get; private set; } - public int FileSize { get; private set; } + public int TextOffset => (int)Header.NroSegments[0].FileOffset; + public int RoOffset => (int)Header.NroSegments[1].FileOffset; + public int DataOffset => (int)Header.NroSegments[2].FileOffset; + public int BssOffset => DataOffset + Data.Length; + public int BssSize => (int)Header.BssSize; - public int BssOffset => DataOffset + Data.Length; + public int Mod0Offset => Start.Mod0Offset; + public int FileSize => (int)Header.Size; public ulong SourceAddress { get; private set; } public ulong BssAddress { get; private set; } - public NroExecutable(Stream input, ulong sourceAddress = 0, ulong bssAddress = 0) + public NroExecutable(IStorage inStorage, ulong sourceAddress = 0, ulong bssAddress = 0) : base(inStorage) { + Program = new byte[FileSize]; + SourceAddress = sourceAddress; BssAddress = bssAddress; - BinaryReader reader = new BinaryReader(input); - - input.Seek(4, SeekOrigin.Begin); - - int mod0Offset = reader.ReadInt32(); - int padding8 = reader.ReadInt32(); - int paddingC = reader.ReadInt32(); - int nroMagic = reader.ReadInt32(); - int unknown14 = reader.ReadInt32(); - int fileSize = reader.ReadInt32(); - int unknown1C = reader.ReadInt32(); - int textOffset = reader.ReadInt32(); - int textSize = reader.ReadInt32(); - int roOffset = reader.ReadInt32(); - int roSize = reader.ReadInt32(); - int dataOffset = reader.ReadInt32(); - int dataSize = reader.ReadInt32(); - int bssSize = reader.ReadInt32(); - - Mod0Offset = mod0Offset; - TextOffset = textOffset; - RoOffset = roOffset; - DataOffset = dataOffset; - BssSize = bssSize; - - byte[] Read(long position, int size) - { - input.Seek(position, SeekOrigin.Begin); - - return reader.ReadBytes(size); - } - - Text = Read(textOffset, textSize); - Ro = Read(roOffset, roSize); - Data = Read(dataOffset, dataSize); - - FileSize = fileSize; + OpenNroSegment(NroSegmentType.Text, false).Read(0, Text); + OpenNroSegment(NroSegmentType.Ro , false).Read(0, Ro); + OpenNroSegment(NroSegmentType.Data, false).Read(0, Data); } } }
\ No newline at end of file diff --git a/Ryujinx.HLE/Loaders/Executables/NsoExecutable.cs b/Ryujinx.HLE/Loaders/Executables/NsoExecutable.cs index bbe2c87f..96f1df4e 100644 --- a/Ryujinx.HLE/Loaders/Executables/NsoExecutable.cs +++ b/Ryujinx.HLE/Loaders/Executables/NsoExecutable.cs @@ -1,23 +1,32 @@ +using LibHac.Common; using LibHac.Fs; using LibHac.FsSystem; using LibHac.Loader; +using System; namespace Ryujinx.HLE.Loaders.Executables { class NsoExecutable : IExecutable { - public byte[] Text { get; } - public byte[] Ro { get; } - public byte[] Data { get; } + public byte[] Program { get; } + public Span<byte> Text => Program.AsSpan().Slice(TextOffset, TextSize); + public Span<byte> Ro => Program.AsSpan().Slice(RoOffset, RoSize); + public Span<byte> Data => Program.AsSpan().Slice(DataOffset, DataSize); public int TextOffset { get; } public int RoOffset { get; } public int DataOffset { get; } public int BssOffset => DataOffset + Data.Length; + public int TextSize { get; } + public int RoSize { get; } + public int DataSize { get; } public int BssSize { get; } - public NsoExecutable(IStorage inStorage) + public string Name; + public Buffer32 BuildId; + + public NsoExecutable(IStorage inStorage, string name = null) { NsoReader reader = new NsoReader(); @@ -28,20 +37,26 @@ namespace Ryujinx.HLE.Loaders.Executables DataOffset = (int)reader.Header.Segments[2].MemoryOffset; BssSize = (int)reader.Header.BssSize; - Text = DecompressSection(reader, NsoReader.SegmentType.Text); - Ro = DecompressSection(reader, NsoReader.SegmentType.Ro); - Data = DecompressSection(reader, NsoReader.SegmentType.Data); + reader.GetSegmentSize(NsoReader.SegmentType.Data, out uint uncompressedSize).ThrowIfFailure(); + Program = new byte[DataOffset + uncompressedSize]; + + TextSize = DecompressSection(reader, NsoReader.SegmentType.Text, TextOffset, Program); + RoSize = DecompressSection(reader, NsoReader.SegmentType.Ro, RoOffset, Program); + DataSize = DecompressSection(reader, NsoReader.SegmentType.Data, DataOffset, Program); + + Name = name; + BuildId = reader.Header.ModuleId; } - private static byte[] DecompressSection(NsoReader reader, NsoReader.SegmentType segmentType) + private static int DecompressSection(NsoReader reader, NsoReader.SegmentType segmentType, int offset, byte[] Program) { reader.GetSegmentSize(segmentType, out uint uncompressedSize).ThrowIfFailure(); - byte[] result = new byte[uncompressedSize]; + var span = Program.AsSpan().Slice(offset, (int)uncompressedSize); - reader.ReadSegment(segmentType, result).ThrowIfFailure(); + reader.ReadSegment(segmentType, span).ThrowIfFailure(); - return result; + return (int)uncompressedSize; } } }
\ No newline at end of file diff --git a/Ryujinx.HLE/Loaders/Mods/IPSPatcher.cs b/Ryujinx.HLE/Loaders/Mods/IPSPatcher.cs new file mode 100644 index 00000000..d104d25a --- /dev/null +++ b/Ryujinx.HLE/Loaders/Mods/IPSPatcher.cs @@ -0,0 +1,117 @@ +using Ryujinx.Common.Logging; +using System; +using System.IO; +using System.Text; + +namespace Ryujinx.HLE.Loaders.Mods +{ + class IpsPatcher + { + MemPatch _patches; + + public IpsPatcher(BinaryReader reader) + { + _patches = ParseIps(reader); + if (_patches != null) + { + Logger.PrintInfo(LogClass.ModLoader, "IPS patch loaded successfully"); + } + } + + private static MemPatch ParseIps(BinaryReader reader) + { + Span<byte> IpsHeaderMagic = Encoding.ASCII.GetBytes("PATCH").AsSpan(); + Span<byte> IpsTailMagic = Encoding.ASCII.GetBytes("EOF").AsSpan(); + Span<byte> Ips32HeaderMagic = Encoding.ASCII.GetBytes("IPS32").AsSpan(); + Span<byte> Ips32TailMagic = Encoding.ASCII.GetBytes("EEOF").AsSpan(); + + MemPatch patches = new MemPatch(); + var header = reader.ReadBytes(IpsHeaderMagic.Length).AsSpan(); + + if (header.Length != IpsHeaderMagic.Length) + { + return null; + } + + bool is32; + Span<byte> tailSpan; + + if (header.SequenceEqual(IpsHeaderMagic)) + { + is32 = false; + tailSpan = IpsTailMagic; + } + else if (header.SequenceEqual(Ips32HeaderMagic)) + { + is32 = true; + tailSpan = Ips32TailMagic; + } + else + { + return null; + } + + byte[] buf = new byte[tailSpan.Length]; + + bool ReadNext(int size) => reader.Read(buf, 0, size) != size; + + while (true) + { + if (ReadNext(buf.Length)) + { + return null; + } + + if (buf.AsSpan().SequenceEqual(tailSpan)) + { + break; + } + + int patchOffset = is32 ? buf[0] << 24 | buf[1] << 16 | buf[2] << 8 | buf[3] + : buf[0] << 16 | buf[1] << 8 | buf[2]; + + if (ReadNext(2)) + { + return null; + } + + int patchSize = buf[0] << 8 | buf[1]; + + if (patchSize == 0) // RLE/Fill mode + { + if (ReadNext(2)) + { + return null; + } + + int fillLength = buf[0] << 8 | buf[1]; + + if (ReadNext(1)) + { + return null; + } + + patches.AddFill((uint)patchOffset, fillLength, buf[0]); + } + else // Copy mode + { + var patch = reader.ReadBytes(patchSize); + + if (patch.Length != patchSize) + { + return null; + } + + patches.Add((uint)patchOffset, patch); + } + } + + return patches; + } + + public void AddPatches(MemPatch patches) + { + patches.AddFrom(_patches); + } + } +}
\ No newline at end of file diff --git a/Ryujinx.HLE/Loaders/Mods/IPSwitchPatcher.cs b/Ryujinx.HLE/Loaders/Mods/IPSwitchPatcher.cs new file mode 100644 index 00000000..d5b0b1be --- /dev/null +++ b/Ryujinx.HLE/Loaders/Mods/IPSwitchPatcher.cs @@ -0,0 +1,262 @@ +using Ryujinx.Common.Logging; +using System; +using System.IO; +using System.Text; + +namespace Ryujinx.HLE.Loaders.Mods +{ + class IPSwitchPatcher + { + readonly StreamReader _reader; + public string BuildId { get; } + + const string BidHeader = "@nsobid-"; + + public IPSwitchPatcher(StreamReader reader) + { + string header = reader.ReadLine(); + if (header == null || !header.StartsWith(BidHeader)) + { + Logger.PrintError(LogClass.ModLoader, "IPSwitch: Malformed PCHTXT file. Skipping..."); + return; + } + + _reader = reader; + BuildId = header.Substring(BidHeader.Length).TrimEnd().TrimEnd('0'); + } + + private enum Token + { + Normal, + String, + EscapeChar, + Comment + } + + // Uncomments line and unescapes C style strings within + private static string PreprocessLine(string line) + { + StringBuilder str = new StringBuilder(); + Token state = Token.Normal; + + for (int i = 0; i < line.Length; ++i) + { + char c = line[i]; + char la = i + 1 != line.Length ? line[i + 1] : '\0'; + + switch (state) + { + case Token.Normal: + state = c == '"' ? Token.String : + c == '/' && la == '/' ? Token.Comment : + c == '/' && la != '/' ? Token.Comment : // Ignore error and stop parsing + Token.Normal; + break; + case Token.String: + state = c switch + { + '"' => Token.Normal, + '\\' => Token.EscapeChar, + _ => Token.String + }; + break; + case Token.EscapeChar: + state = Token.String; + c = c switch + { + 'a' => '\a', + 'b' => '\b', + 'f' => '\f', + 'n' => '\n', + 'r' => '\r', + 't' => '\t', + 'v' => '\v', + '\\' => '\\', + _ => '?' + }; + break; + } + + if (state == Token.Comment) break; + + if (state < Token.EscapeChar) + { + str.Append(c); + } + } + + return str.ToString().Trim(); + } + + static int ParseHexByte(byte c) + { + if (c >= '0' && c <= '9') + { + return c - '0'; + } + else if (c >= 'A' && c <= 'F') + { + return c - 'A' + 10; + } + else if (c >= 'a' && c <= 'f') + { + return c - 'a' + 10; + } + else + { + return 0; + } + } + + // Big Endian + static byte[] Hex2ByteArrayBE(string hexstr) + { + if ((hexstr.Length & 1) == 1) return null; + + byte[] bytes = new byte[hexstr.Length >> 1]; + + for (int i = 0; i < hexstr.Length; i += 2) + { + int high = ParseHexByte((byte)hexstr[i]); + int low = ParseHexByte((byte)hexstr[i + 1]); + bytes[i >> 1] = (byte)((high << 4) | low); + } + + return bytes; + } + + // Auto base discovery + private static bool ParseInt(string str, out int value) + { + if (str[0] == '0' && (str[1] == 'x' || str[1] == 'X')) + { + return Int32.TryParse(str.Substring(2), System.Globalization.NumberStyles.HexNumber, null, out value); + } + else + { + return Int32.TryParse(str, System.Globalization.NumberStyles.Integer, null, out value); + } + } + + private MemPatch Parse() + { + if (_reader == null) + { + return null; + } + + MemPatch patches = new MemPatch(); + + bool enabled = true; + bool printValues = false; + int offset_shift = 0; + + string line; + int lineNum = 0; + + static void Print(string s) => Logger.PrintInfo(LogClass.ModLoader, $"IPSwitch: {s}"); + + void ParseWarn() => Logger.PrintWarning(LogClass.ModLoader, $"IPSwitch: Parse error at line {lineNum} for bid={BuildId}"); + + while ((line = _reader.ReadLine()) != null) + { + line = PreprocessLine(line); + lineNum += 1; + + if (line.Length == 0) + { + continue; + } + else if (line.StartsWith('#')) + { + Print(line); + } + else if (line.StartsWith("@stop")) + { + break; + } + else if (line.StartsWith("@enabled")) + { + enabled = true; + } + else if (line.StartsWith("@disabled")) + { + enabled = false; + } + else if (line.StartsWith("@flag")) + { + var tokens = line.Split(' ', 3, StringSplitOptions.RemoveEmptyEntries); + + if (tokens.Length < 2) + { + ParseWarn(); + continue; + } + + if (tokens[1] == "offset_shift") + { + if (tokens.Length != 3 || !ParseInt(tokens[2], out offset_shift)) + { + ParseWarn(); + continue; + } + } + else if (tokens[1] == "print_values") + { + printValues = true; + } + } + else if (line.StartsWith('@')) + { + // Ignore + } + else + { + if (!enabled) + { + continue; + } + + var tokens = line.Split(' ', 2, StringSplitOptions.RemoveEmptyEntries); + + if (tokens.Length < 2) + { + ParseWarn(); + continue; + } + + if (!Int32.TryParse(tokens[0], System.Globalization.NumberStyles.HexNumber, null, out int offset)) + { + ParseWarn(); + continue; + } + + offset += offset_shift; + + if (printValues) + { + Print($"print_values 0x{offset:x} <= {tokens[1]}"); + } + + if (tokens[1][0] == '"') + { + var patch = Encoding.ASCII.GetBytes(tokens[1].Trim('"')); + patches.Add((uint)offset, patch); + } + else + { + var patch = Hex2ByteArrayBE(tokens[1]); + patches.Add((uint)offset, patch); + } + } + } + + return patches; + } + + public void AddPatches(MemPatch patches) + { + patches.AddFrom(Parse()); + } + } +}
\ No newline at end of file diff --git a/Ryujinx.HLE/Loaders/Mods/MemPatch.cs b/Ryujinx.HLE/Loaders/Mods/MemPatch.cs new file mode 100644 index 00000000..0fc19159 --- /dev/null +++ b/Ryujinx.HLE/Loaders/Mods/MemPatch.cs @@ -0,0 +1,98 @@ +using Ryujinx.Cpu; +using System; +using System.Collections.Generic; +using System.Linq; + +using Ryujinx.Common.Logging; + +namespace Ryujinx.HLE.Loaders.Mods +{ + public class MemPatch + { + readonly Dictionary<uint, byte[]> _patches = new Dictionary<uint, byte[]>(); + + /// <summary> + /// Adds a patch to specified offset. Overwrites if already present. + /// </summary> + /// <param name="offset">Memory offset</param> + /// <param name="patch">The patch to add</param> + public void Add(uint offset, byte[] patch) + { + _patches[offset] = patch; + } + + /// <summary> + /// Adds a patch in the form of an RLE (Fill mode). + /// </summary> + /// <param name="offset">Memory offset</param> + /// <param name="length"The fill length</param> + /// <param name="filler">The byte to fill</param> + public void AddFill(uint offset, int length, byte filler) + { + // TODO: Can be made space efficient by changing `_patches` + // Should suffice for now + byte[] patch = new byte[length]; + patch.AsSpan().Fill(filler); + + _patches[offset] = patch; + } + + /// <summary> + /// Adds all patches from an existing MemPatch + /// </summary> + /// <param name="patches">The patches to add</param> + public void AddFrom(MemPatch patches) + { + if (patches == null) + { + return; + } + + foreach (var (patchOffset, patch) in patches._patches) + { + _patches[patchOffset] = patch; + } + } + + /// <summary> + /// Applies all the patches added to this instance. + /// </summary> + /// <remarks> + /// Patches are applied in ascending order of offsets to guarantee + /// overlapping patches always apply the same way. + /// </remarks> + /// <param name="memory">The span of bytes to patch</param> + /// <param name="maxSize">The maximum size of the slice of patchable memory</param> + /// <param name="protectedOffset">A secondary offset used in special cases (NSO header)</param> + /// <returns>Successful patches count</returns> + public int Patch(Span<byte> memory, int protectedOffset = 0) + { + int count = 0; + foreach (var (offset, patch) in _patches.OrderBy(item => item.Key)) + { + int patchOffset = (int)offset; + int patchSize = patch.Length; + + if (patchOffset < protectedOffset || patchOffset > memory.Length) + { + continue; // Add warning? + } + + patchOffset -= protectedOffset; + + if (patchOffset + patchSize > memory.Length) + { + patchSize = memory.Length - (int)patchOffset; // Add warning? + } + + Logger.PrintInfo(LogClass.ModLoader, $"Patching address offset {patchOffset:x} <= {BitConverter.ToString(patch).Replace('-', ' ')} len={patchSize}"); + + patch.AsSpan().Slice(0, patchSize).CopyTo(memory.Slice(patchOffset, patchSize)); + + count++; + } + + return count; + } + } +}
\ No newline at end of file diff --git a/Ryujinx.HLE/Loaders/Npdm/Npdm.cs b/Ryujinx.HLE/Loaders/Npdm/Npdm.cs index 345721c7..7f1e2935 100644 --- a/Ryujinx.HLE/Loaders/Npdm/Npdm.cs +++ b/Ryujinx.HLE/Loaders/Npdm/Npdm.cs @@ -16,7 +16,7 @@ namespace Ryujinx.HLE.Loaders.Npdm public byte MainThreadPriority { get; private set; } public byte DefaultCpuId { get; private set; } public int PersonalMmHeapSize { get; private set; } - public int ProcessCategory { get; private set; } + public int Version { get; private set; } public int MainThreadStackSize { get; private set; } public string TitleName { get; set; } public byte[] ProductCode { get; private set; } @@ -48,7 +48,7 @@ namespace Ryujinx.HLE.Loaders.Npdm PersonalMmHeapSize = reader.ReadInt32(); - ProcessCategory = reader.ReadInt32(); + Version = reader.ReadInt32(); MainThreadStackSize = reader.ReadInt32(); |
