2018-11-28 23:18:09 +01:00
|
|
|
|
using ChocolArm64.Memory;
|
|
|
|
|
using Ryujinx.Common;
|
|
|
|
|
using Ryujinx.HLE.HOS.Ipc;
|
|
|
|
|
using Ryujinx.HLE.HOS.Kernel;
|
2018-10-10 01:01:49 +02:00
|
|
|
|
using Ryujinx.HLE.Loaders.Executables;
|
|
|
|
|
using Ryujinx.HLE.Utilities;
|
2018-12-05 01:52:39 +01:00
|
|
|
|
using System;
|
2018-10-10 01:01:49 +02:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Runtime.InteropServices;
|
|
|
|
|
using System.Security.Cryptography;
|
|
|
|
|
|
|
|
|
|
using static Ryujinx.HLE.HOS.ErrorCode;
|
|
|
|
|
|
|
|
|
|
namespace Ryujinx.HLE.HOS.Services.Ldr
|
|
|
|
|
{
|
|
|
|
|
[StructLayout(LayoutKind.Explicit, Size = 0x350)]
|
|
|
|
|
unsafe struct NrrHeader
|
|
|
|
|
{
|
|
|
|
|
[FieldOffset(0)]
|
|
|
|
|
public uint Magic;
|
|
|
|
|
|
|
|
|
|
[FieldOffset(0x10)]
|
|
|
|
|
public ulong TitleIdMask;
|
|
|
|
|
|
|
|
|
|
[FieldOffset(0x18)]
|
|
|
|
|
public ulong TitleIdPattern;
|
|
|
|
|
|
|
|
|
|
[FieldOffset(0x30)]
|
|
|
|
|
public fixed byte Modulus[0x100];
|
|
|
|
|
|
|
|
|
|
[FieldOffset(0x130)]
|
|
|
|
|
public fixed byte FixedKeySignature[0x100];
|
|
|
|
|
|
|
|
|
|
[FieldOffset(0x230)]
|
|
|
|
|
public fixed byte NrrSignature[0x100];
|
|
|
|
|
|
|
|
|
|
[FieldOffset(0x330)]
|
|
|
|
|
public ulong TitleIdMin;
|
|
|
|
|
|
|
|
|
|
[FieldOffset(0x338)]
|
|
|
|
|
public uint NrrSize;
|
|
|
|
|
|
|
|
|
|
[FieldOffset(0x340)]
|
|
|
|
|
public uint HashOffset;
|
|
|
|
|
|
|
|
|
|
[FieldOffset(0x344)]
|
|
|
|
|
public uint HashCount;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class NrrInfo
|
|
|
|
|
{
|
2018-12-05 01:52:39 +01:00
|
|
|
|
public NrrHeader Header { get; private set; }
|
|
|
|
|
public List<byte[]> Hashes { get; private set; }
|
|
|
|
|
public long NrrAddress { get; private set; }
|
2018-10-10 01:01:49 +02:00
|
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
|
public NrrInfo(long NrrAddress, NrrHeader Header, List<byte[]> Hashes)
|
2018-10-10 01:01:49 +02:00
|
|
|
|
{
|
2018-12-05 01:52:39 +01:00
|
|
|
|
this.NrrAddress = NrrAddress;
|
|
|
|
|
this.Header = Header;
|
|
|
|
|
this.Hashes = Hashes;
|
2018-10-10 01:01:49 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class NroInfo
|
|
|
|
|
{
|
2018-12-05 01:52:39 +01:00
|
|
|
|
public NxRelocatableObject Executable { get; private set; }
|
|
|
|
|
|
|
|
|
|
public byte[] Hash { get; private set; }
|
|
|
|
|
public ulong NroAddress { get; private set; }
|
|
|
|
|
public ulong NroSize { get; private set; }
|
|
|
|
|
public ulong BssAddress { get; private set; }
|
|
|
|
|
public ulong BssSize { get; private set; }
|
|
|
|
|
public ulong TotalSize { get; private set; }
|
2018-11-28 23:18:09 +01:00
|
|
|
|
public ulong NroMappedAddress { get; set; }
|
|
|
|
|
|
|
|
|
|
public NroInfo(
|
2018-12-05 01:52:39 +01:00
|
|
|
|
NxRelocatableObject Executable,
|
|
|
|
|
byte[] Hash,
|
|
|
|
|
ulong NroAddress,
|
|
|
|
|
ulong NroSize,
|
|
|
|
|
ulong BssAddress,
|
|
|
|
|
ulong BssSize,
|
|
|
|
|
ulong TotalSize)
|
2018-10-10 01:01:49 +02:00
|
|
|
|
{
|
2018-12-05 01:52:39 +01:00
|
|
|
|
this.Executable = Executable;
|
|
|
|
|
this.Hash = Hash;
|
|
|
|
|
this.NroAddress = NroAddress;
|
|
|
|
|
this.NroSize = NroSize;
|
|
|
|
|
this.BssAddress = BssAddress;
|
|
|
|
|
this.BssSize = BssSize;
|
|
|
|
|
this.TotalSize = TotalSize;
|
2018-10-10 01:01:49 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class IRoInterface : IpcService
|
|
|
|
|
{
|
2018-12-05 01:52:39 +01:00
|
|
|
|
private Dictionary<int, ServiceProcessRequest> m_Commands;
|
2018-10-10 01:01:49 +02:00
|
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
|
public override IReadOnlyDictionary<int, ServiceProcessRequest> Commands => m_Commands;
|
2018-10-10 01:01:49 +02:00
|
|
|
|
|
|
|
|
|
private const int MaxNrr = 0x40;
|
|
|
|
|
private const int MaxNro = 0x40;
|
|
|
|
|
|
|
|
|
|
private const uint NrrMagic = 0x3052524E;
|
|
|
|
|
private const uint NroMagic = 0x304F524E;
|
|
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
|
private List<NrrInfo> NrrInfos;
|
|
|
|
|
private List<NroInfo> NroInfos;
|
2018-10-10 01:01:49 +02:00
|
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
|
private bool IsInitialized;
|
2018-10-10 01:01:49 +02:00
|
|
|
|
|
|
|
|
|
public IRoInterface()
|
|
|
|
|
{
|
2018-12-05 01:52:39 +01:00
|
|
|
|
m_Commands = new Dictionary<int, ServiceProcessRequest>()
|
2018-10-10 01:01:49 +02:00
|
|
|
|
{
|
|
|
|
|
{ 0, LoadNro },
|
|
|
|
|
{ 1, UnloadNro },
|
|
|
|
|
{ 2, LoadNrr },
|
|
|
|
|
{ 3, UnloadNrr },
|
2018-12-05 01:52:39 +01:00
|
|
|
|
{ 4, Initialize },
|
2018-10-10 01:01:49 +02:00
|
|
|
|
};
|
|
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
|
NrrInfos = new List<NrrInfo>(MaxNrr);
|
|
|
|
|
NroInfos = new List<NroInfo>(MaxNro);
|
2018-10-10 01:01:49 +02:00
|
|
|
|
}
|
|
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
|
private long ParseNrr(out NrrInfo NrrInfo, ServiceCtx Context, long NrrAddress, long NrrSize)
|
2018-10-10 01:01:49 +02:00
|
|
|
|
{
|
2018-12-05 01:52:39 +01:00
|
|
|
|
NrrInfo = null;
|
2018-10-10 01:01:49 +02:00
|
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
|
if (NrrSize == 0 || NrrAddress + NrrSize <= NrrAddress || (NrrSize & 0xFFF) != 0)
|
2018-10-10 01:01:49 +02:00
|
|
|
|
{
|
|
|
|
|
return MakeError(ErrorModule.Loader, LoaderErr.BadSize);
|
|
|
|
|
}
|
2018-12-05 01:52:39 +01:00
|
|
|
|
else if ((NrrAddress & 0xFFF) != 0)
|
2018-10-10 01:01:49 +02:00
|
|
|
|
{
|
|
|
|
|
return MakeError(ErrorModule.Loader, LoaderErr.UnalignedAddress);
|
|
|
|
|
}
|
|
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
|
StructReader Reader = new StructReader(Context.Memory, NrrAddress);
|
|
|
|
|
NrrHeader Header = Reader.Read<NrrHeader>();
|
2018-10-10 01:01:49 +02:00
|
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
|
if (Header.Magic != NrrMagic)
|
2018-10-10 01:01:49 +02:00
|
|
|
|
{
|
|
|
|
|
return MakeError(ErrorModule.Loader, LoaderErr.InvalidNrr);
|
|
|
|
|
}
|
2018-12-05 01:52:39 +01:00
|
|
|
|
else if (Header.NrrSize != NrrSize)
|
2018-10-10 01:01:49 +02:00
|
|
|
|
{
|
|
|
|
|
return MakeError(ErrorModule.Loader, LoaderErr.BadSize);
|
|
|
|
|
}
|
|
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
|
List<byte[]> Hashes = new List<byte[]>();
|
2018-10-10 01:01:49 +02:00
|
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
|
for (int i = 0; i < Header.HashCount; i++)
|
2018-10-10 01:01:49 +02:00
|
|
|
|
{
|
2018-12-05 01:52:39 +01:00
|
|
|
|
Hashes.Add(Context.Memory.ReadBytes(NrrAddress + Header.HashOffset + (i * 0x20), 0x20));
|
2018-10-10 01:01:49 +02:00
|
|
|
|
}
|
|
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
|
NrrInfo = new NrrInfo(NrrAddress, Header, Hashes);
|
2018-10-10 01:01:49 +02:00
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
|
public bool IsNroHashPresent(byte[] NroHash)
|
2018-10-10 01:01:49 +02:00
|
|
|
|
{
|
2018-12-05 01:52:39 +01:00
|
|
|
|
foreach (NrrInfo Info in NrrInfos)
|
2018-10-10 01:01:49 +02:00
|
|
|
|
{
|
2018-12-05 01:52:39 +01:00
|
|
|
|
foreach (byte[] Hash in Info.Hashes)
|
2018-10-10 01:01:49 +02:00
|
|
|
|
{
|
2018-12-05 01:52:39 +01:00
|
|
|
|
if (Hash.SequenceEqual(NroHash))
|
2018-10-10 01:01:49 +02:00
|
|
|
|
{
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
|
public bool IsNroLoaded(byte[] NroHash)
|
2018-10-10 01:01:49 +02:00
|
|
|
|
{
|
2018-12-05 01:52:39 +01:00
|
|
|
|
foreach (NroInfo Info in NroInfos)
|
2018-10-10 01:01:49 +02:00
|
|
|
|
{
|
2018-12-05 01:52:39 +01:00
|
|
|
|
if (Info.Hash.SequenceEqual(NroHash))
|
2018-10-10 01:01:49 +02:00
|
|
|
|
{
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
|
public long ParseNro(out NroInfo Res, ServiceCtx Context, ulong NroAddress, ulong NroSize, ulong BssAddress, ulong BssSize)
|
2018-10-10 01:01:49 +02:00
|
|
|
|
{
|
2018-12-05 01:52:39 +01:00
|
|
|
|
Res = null;
|
2018-10-10 01:01:49 +02:00
|
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
|
if (NroInfos.Count >= MaxNro)
|
2018-10-10 01:01:49 +02:00
|
|
|
|
{
|
|
|
|
|
return MakeError(ErrorModule.Loader, LoaderErr.MaxNro);
|
|
|
|
|
}
|
2018-12-05 01:52:39 +01:00
|
|
|
|
else if (NroSize == 0 || NroAddress + NroSize <= NroAddress || (NroSize & 0xFFF) != 0)
|
2018-10-10 01:01:49 +02:00
|
|
|
|
{
|
|
|
|
|
return MakeError(ErrorModule.Loader, LoaderErr.BadSize);
|
|
|
|
|
}
|
2018-12-05 01:52:39 +01:00
|
|
|
|
else if (BssSize != 0 && BssAddress + BssSize <= BssAddress)
|
2018-10-10 01:01:49 +02:00
|
|
|
|
{
|
|
|
|
|
return MakeError(ErrorModule.Loader, LoaderErr.BadSize);
|
|
|
|
|
}
|
2018-12-05 01:52:39 +01:00
|
|
|
|
else if ((NroAddress & 0xFFF) != 0)
|
2018-10-10 01:01:49 +02:00
|
|
|
|
{
|
|
|
|
|
return MakeError(ErrorModule.Loader, LoaderErr.UnalignedAddress);
|
|
|
|
|
}
|
|
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
|
uint Magic = Context.Memory.ReadUInt32((long)NroAddress + 0x10);
|
|
|
|
|
uint NroFileSize = Context.Memory.ReadUInt32((long)NroAddress + 0x18);
|
2018-10-10 01:01:49 +02:00
|
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
|
if (Magic != NroMagic || NroSize != NroFileSize)
|
2018-10-10 01:01:49 +02:00
|
|
|
|
{
|
|
|
|
|
return MakeError(ErrorModule.Loader, LoaderErr.InvalidNro);
|
|
|
|
|
}
|
|
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
|
byte[] NroData = Context.Memory.ReadBytes((long)NroAddress, (long)NroSize);
|
|
|
|
|
byte[] NroHash = null;
|
2018-10-10 01:01:49 +02:00
|
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
|
MemoryStream Stream = new MemoryStream(NroData);
|
2018-10-10 01:01:49 +02:00
|
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
|
using (SHA256 Hasher = SHA256.Create())
|
2018-10-10 01:01:49 +02:00
|
|
|
|
{
|
2018-12-05 01:52:39 +01:00
|
|
|
|
NroHash = Hasher.ComputeHash(Stream);
|
2018-10-10 01:01:49 +02:00
|
|
|
|
}
|
|
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
|
if (!IsNroHashPresent(NroHash))
|
2018-10-10 01:01:49 +02:00
|
|
|
|
{
|
|
|
|
|
return MakeError(ErrorModule.Loader, LoaderErr.NroHashNotPresent);
|
|
|
|
|
}
|
|
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
|
if (IsNroLoaded(NroHash))
|
2018-10-10 01:01:49 +02:00
|
|
|
|
{
|
|
|
|
|
return MakeError(ErrorModule.Loader, LoaderErr.NroAlreadyLoaded);
|
|
|
|
|
}
|
|
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
|
Stream.Position = 0;
|
2018-10-10 01:01:49 +02:00
|
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
|
NxRelocatableObject Executable = new NxRelocatableObject(Stream, NroAddress, BssAddress);
|
2018-10-10 01:01:49 +02:00
|
|
|
|
|
|
|
|
|
// check if everything is page align.
|
2018-12-05 01:52:39 +01:00
|
|
|
|
if ((Executable.Text.Length & 0xFFF) != 0 || (Executable.RO.Length & 0xFFF) != 0 ||
|
|
|
|
|
(Executable.Data.Length & 0xFFF) != 0 || (Executable.BssSize & 0xFFF) != 0)
|
2018-10-10 01:01:49 +02:00
|
|
|
|
{
|
|
|
|
|
return MakeError(ErrorModule.Loader, LoaderErr.InvalidNro);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// check if everything is contiguous.
|
2018-12-05 01:52:39 +01:00
|
|
|
|
if (Executable.ROOffset != Executable.TextOffset + Executable.Text.Length ||
|
|
|
|
|
Executable.DataOffset != Executable.ROOffset + Executable.RO.Length ||
|
|
|
|
|
NroFileSize != Executable.DataOffset + Executable.Data.Length)
|
2018-10-10 01:01:49 +02:00
|
|
|
|
{
|
|
|
|
|
return MakeError(ErrorModule.Loader, LoaderErr.InvalidNro);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// finally check the bss size match.
|
2018-12-05 01:52:39 +01:00
|
|
|
|
if ((ulong)Executable.BssSize != BssSize)
|
2018-10-10 01:01:49 +02:00
|
|
|
|
{
|
|
|
|
|
return MakeError(ErrorModule.Loader, LoaderErr.InvalidNro);
|
|
|
|
|
}
|
|
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
|
int TotalSize = Executable.Text.Length + Executable.RO.Length + Executable.Data.Length + Executable.BssSize;
|
2018-11-28 23:18:09 +01:00
|
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
|
Res = new NroInfo(
|
|
|
|
|
Executable,
|
|
|
|
|
NroHash,
|
|
|
|
|
NroAddress,
|
|
|
|
|
NroSize,
|
|
|
|
|
BssAddress,
|
|
|
|
|
BssSize,
|
|
|
|
|
(ulong)TotalSize);
|
2018-10-10 01:01:49 +02:00
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
|
private long MapNro(ServiceCtx Context, NroInfo Info, out ulong NroMappedAddress)
|
2018-10-10 01:01:49 +02:00
|
|
|
|
{
|
2018-12-05 01:52:39 +01:00
|
|
|
|
NroMappedAddress = 0;
|
2018-10-10 01:01:49 +02:00
|
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
|
KMemoryManager MemMgr = Context.Process.MemoryManager;
|
2018-10-10 01:01:49 +02:00
|
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
|
ulong TargetAddress = MemMgr.GetAddrSpaceBaseAddr();
|
2018-10-10 01:01:49 +02:00
|
|
|
|
|
|
|
|
|
while (true)
|
|
|
|
|
{
|
2018-12-05 01:52:39 +01:00
|
|
|
|
if (TargetAddress + Info.TotalSize >= MemMgr.AddrSpaceEnd)
|
2018-10-10 01:01:49 +02:00
|
|
|
|
{
|
|
|
|
|
return MakeError(ErrorModule.Loader, LoaderErr.InvalidMemoryState);
|
|
|
|
|
}
|
|
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
|
KMemoryInfo MemInfo = MemMgr.QueryMemory(TargetAddress);
|
2018-10-10 01:01:49 +02:00
|
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
|
if (MemInfo.State == MemoryState.Unmapped && MemInfo.Size >= Info.TotalSize)
|
2018-10-10 01:01:49 +02:00
|
|
|
|
{
|
2018-12-05 01:52:39 +01:00
|
|
|
|
if (!MemMgr.InsideHeapRegion (TargetAddress, Info.TotalSize) &&
|
|
|
|
|
!MemMgr.InsideAliasRegion(TargetAddress, Info.TotalSize))
|
2018-11-28 23:18:09 +01:00
|
|
|
|
{
|
|
|
|
|
break;
|
|
|
|
|
}
|
2018-10-10 01:01:49 +02:00
|
|
|
|
}
|
|
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
|
TargetAddress += MemInfo.Size;
|
2018-11-28 23:18:09 +01:00
|
|
|
|
}
|
|
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
|
KernelResult Result = MemMgr.MapProcessCodeMemory(TargetAddress, Info.NroAddress, Info.NroSize);
|
2018-11-28 23:18:09 +01:00
|
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
|
if (Result != KernelResult.Success)
|
2018-11-28 23:18:09 +01:00
|
|
|
|
{
|
|
|
|
|
return MakeError(ErrorModule.Loader, LoaderErr.InvalidMemoryState);
|
|
|
|
|
}
|
|
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
|
ulong BssTargetAddress = TargetAddress + Info.NroSize;
|
2018-11-28 23:18:09 +01:00
|
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
|
if (Info.BssSize != 0)
|
2018-11-28 23:18:09 +01:00
|
|
|
|
{
|
2018-12-05 01:52:39 +01:00
|
|
|
|
Result = MemMgr.MapProcessCodeMemory(BssTargetAddress, Info.BssAddress, Info.BssSize);
|
2018-11-28 23:18:09 +01:00
|
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
|
if (Result != KernelResult.Success)
|
2018-11-28 23:18:09 +01:00
|
|
|
|
{
|
2018-12-05 01:52:39 +01:00
|
|
|
|
MemMgr.UnmapProcessCodeMemory(TargetAddress, Info.NroAddress, Info.NroSize);
|
2018-11-28 23:18:09 +01:00
|
|
|
|
|
|
|
|
|
return MakeError(ErrorModule.Loader, LoaderErr.InvalidMemoryState);
|
|
|
|
|
}
|
2018-10-10 01:01:49 +02:00
|
|
|
|
}
|
|
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
|
Result = LoadNroIntoMemory(Context.Process, Info.Executable, TargetAddress);
|
2018-11-28 23:18:09 +01:00
|
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
|
if (Result != KernelResult.Success)
|
2018-11-28 23:18:09 +01:00
|
|
|
|
{
|
2018-12-05 01:52:39 +01:00
|
|
|
|
MemMgr.UnmapProcessCodeMemory(TargetAddress, Info.NroAddress, Info.NroSize);
|
2018-11-28 23:18:09 +01:00
|
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
|
if (Info.BssSize != 0)
|
2018-11-28 23:18:09 +01:00
|
|
|
|
{
|
2018-12-05 01:52:39 +01:00
|
|
|
|
MemMgr.UnmapProcessCodeMemory(BssTargetAddress, Info.BssAddress, Info.BssSize);
|
2018-11-28 23:18:09 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
2018-10-10 01:01:49 +02:00
|
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
|
Info.NroMappedAddress = TargetAddress;
|
|
|
|
|
NroMappedAddress = TargetAddress;
|
2018-10-10 01:01:49 +02:00
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
|
private KernelResult LoadNroIntoMemory(KProcess Process, IExecutable RelocatableObject, ulong BaseAddress)
|
2018-11-28 23:18:09 +01:00
|
|
|
|
{
|
2018-12-05 01:52:39 +01:00
|
|
|
|
ulong TextStart = BaseAddress + (ulong)RelocatableObject.TextOffset;
|
|
|
|
|
ulong ROStart = BaseAddress + (ulong)RelocatableObject.ROOffset;
|
|
|
|
|
ulong DataStart = BaseAddress + (ulong)RelocatableObject.DataOffset;
|
2018-11-28 23:18:09 +01:00
|
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
|
ulong BssStart = DataStart + (ulong)RelocatableObject.Data.Length;
|
2018-11-28 23:18:09 +01:00
|
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
|
ulong BssEnd = BitUtils.AlignUp(BssStart + (ulong)RelocatableObject.BssSize, KMemoryManager.PageSize);
|
2018-11-28 23:18:09 +01:00
|
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
|
Process.CpuMemory.WriteBytes((long)TextStart, RelocatableObject.Text);
|
|
|
|
|
Process.CpuMemory.WriteBytes((long)ROStart, RelocatableObject.RO);
|
|
|
|
|
Process.CpuMemory.WriteBytes((long)DataStart, RelocatableObject.Data);
|
2018-11-28 23:18:09 +01:00
|
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
|
MemoryHelper.FillWithZeros(Process.CpuMemory, (long)BssStart, (int)(BssEnd - BssStart));
|
2018-11-28 23:18:09 +01:00
|
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
|
KernelResult Result;
|
2018-11-28 23:18:09 +01:00
|
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
|
Result = Process.MemoryManager.SetProcessMemoryPermission(TextStart, ROStart - TextStart, MemoryPermission.ReadAndExecute);
|
2018-11-28 23:18:09 +01:00
|
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
|
if (Result != KernelResult.Success)
|
2018-11-28 23:18:09 +01:00
|
|
|
|
{
|
2018-12-05 01:52:39 +01:00
|
|
|
|
return Result;
|
2018-11-28 23:18:09 +01:00
|
|
|
|
}
|
|
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
|
Result = Process.MemoryManager.SetProcessMemoryPermission(ROStart, DataStart - ROStart, MemoryPermission.Read);
|
2018-11-28 23:18:09 +01:00
|
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
|
if (Result != KernelResult.Success)
|
2018-11-28 23:18:09 +01:00
|
|
|
|
{
|
2018-12-05 01:52:39 +01:00
|
|
|
|
return Result;
|
2018-11-28 23:18:09 +01:00
|
|
|
|
}
|
|
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
|
return Process.MemoryManager.SetProcessMemoryPermission(DataStart, BssEnd - DataStart, MemoryPermission.ReadAndWrite);
|
2018-11-28 23:18:09 +01:00
|
|
|
|
}
|
|
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
|
private long RemoveNrrInfo(long NrrAddress)
|
2018-10-10 01:01:49 +02:00
|
|
|
|
{
|
2018-12-05 01:52:39 +01:00
|
|
|
|
foreach (NrrInfo Info in NrrInfos)
|
2018-10-10 01:01:49 +02:00
|
|
|
|
{
|
2018-12-05 01:52:39 +01:00
|
|
|
|
if (Info.NrrAddress == NrrAddress)
|
2018-10-10 01:01:49 +02:00
|
|
|
|
{
|
2018-12-05 01:52:39 +01:00
|
|
|
|
NrrInfos.Remove(Info);
|
2018-10-10 01:01:49 +02:00
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return MakeError(ErrorModule.Loader, LoaderErr.BadNrrAddress);
|
|
|
|
|
}
|
|
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
|
private long RemoveNroInfo(ServiceCtx Context, ulong NroMappedAddress)
|
2018-10-10 01:01:49 +02:00
|
|
|
|
{
|
2018-12-05 01:52:39 +01:00
|
|
|
|
foreach (NroInfo Info in NroInfos)
|
2018-10-10 01:01:49 +02:00
|
|
|
|
{
|
2018-12-05 01:52:39 +01:00
|
|
|
|
if (Info.NroMappedAddress == NroMappedAddress)
|
2018-10-10 01:01:49 +02:00
|
|
|
|
{
|
2018-12-05 01:52:39 +01:00
|
|
|
|
NroInfos.Remove(Info);
|
2018-10-10 01:01:49 +02:00
|
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
|
ulong TextSize = (ulong)Info.Executable.Text.Length;
|
|
|
|
|
ulong ROSize = (ulong)Info.Executable.RO.Length;
|
|
|
|
|
ulong DataSize = (ulong)Info.Executable.Data.Length;
|
|
|
|
|
ulong BssSize = (ulong)Info.Executable.BssSize;
|
2018-10-10 01:01:49 +02:00
|
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
|
KernelResult Result = KernelResult.Success;
|
2018-10-10 01:01:49 +02:00
|
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
|
if (Info.Executable.BssSize != 0)
|
2018-10-10 01:01:49 +02:00
|
|
|
|
{
|
2018-12-05 01:52:39 +01:00
|
|
|
|
Result = Context.Process.MemoryManager.UnmapProcessCodeMemory(
|
|
|
|
|
Info.NroMappedAddress + TextSize + ROSize + DataSize,
|
|
|
|
|
Info.Executable.BssAddress,
|
|
|
|
|
BssSize);
|
2018-10-10 01:01:49 +02:00
|
|
|
|
}
|
|
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
|
if (Result == KernelResult.Success)
|
2018-11-28 23:18:09 +01:00
|
|
|
|
{
|
2018-12-05 01:52:39 +01:00
|
|
|
|
Result = Context.Process.MemoryManager.UnmapProcessCodeMemory(
|
|
|
|
|
Info.NroMappedAddress + TextSize + ROSize,
|
|
|
|
|
Info.Executable.SourceAddress + TextSize + ROSize,
|
|
|
|
|
DataSize);
|
2018-11-28 23:18:09 +01:00
|
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
|
if (Result == KernelResult.Success)
|
2018-11-28 23:18:09 +01:00
|
|
|
|
{
|
2018-12-05 01:52:39 +01:00
|
|
|
|
Result = Context.Process.MemoryManager.UnmapProcessCodeMemory(
|
|
|
|
|
Info.NroMappedAddress,
|
|
|
|
|
Info.Executable.SourceAddress,
|
|
|
|
|
TextSize + ROSize);
|
2018-11-28 23:18:09 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
|
return (long)Result;
|
2018-10-10 01:01:49 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return MakeError(ErrorModule.Loader, LoaderErr.BadNroAddress);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// LoadNro(u64, u64, u64, u64, u64, pid) -> u64
|
2018-12-05 01:52:39 +01:00
|
|
|
|
public long LoadNro(ServiceCtx Context)
|
2018-10-10 01:01:49 +02:00
|
|
|
|
{
|
2018-12-05 01:52:39 +01:00
|
|
|
|
long Result = MakeError(ErrorModule.Loader, LoaderErr.BadInitialization);
|
2018-10-10 01:01:49 +02:00
|
|
|
|
|
|
|
|
|
// Zero
|
2018-12-05 01:52:39 +01:00
|
|
|
|
Context.RequestData.ReadUInt64();
|
2018-10-10 01:01:49 +02:00
|
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
|
ulong NroHeapAddress = Context.RequestData.ReadUInt64();
|
|
|
|
|
ulong NroSize = Context.RequestData.ReadUInt64();
|
|
|
|
|
ulong BssHeapAddress = Context.RequestData.ReadUInt64();
|
|
|
|
|
ulong BssSize = Context.RequestData.ReadUInt64();
|
2018-10-10 01:01:49 +02:00
|
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
|
ulong NroMappedAddress = 0;
|
2018-10-10 01:01:49 +02:00
|
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
|
if (IsInitialized)
|
2018-10-10 01:01:49 +02:00
|
|
|
|
{
|
2018-12-05 01:52:39 +01:00
|
|
|
|
NroInfo Info;
|
2018-10-10 01:01:49 +02:00
|
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
|
Result = ParseNro(out Info, Context, NroHeapAddress, NroSize, BssHeapAddress, BssSize);
|
2018-10-10 01:01:49 +02:00
|
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
|
if (Result == 0)
|
2018-10-10 01:01:49 +02:00
|
|
|
|
{
|
2018-12-05 01:52:39 +01:00
|
|
|
|
Result = MapNro(Context, Info, out NroMappedAddress);
|
2018-10-10 01:01:49 +02:00
|
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
|
if (Result == 0)
|
2018-10-10 01:01:49 +02:00
|
|
|
|
{
|
2018-12-05 01:52:39 +01:00
|
|
|
|
NroInfos.Add(Info);
|
2018-10-10 01:01:49 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
|
Context.ResponseData.Write(NroMappedAddress);
|
2018-10-10 01:01:49 +02:00
|
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
|
return Result;
|
2018-10-10 01:01:49 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// UnloadNro(u64, u64, pid)
|
2018-12-05 01:52:39 +01:00
|
|
|
|
public long UnloadNro(ServiceCtx Context)
|
2018-10-10 01:01:49 +02:00
|
|
|
|
{
|
2018-12-05 01:52:39 +01:00
|
|
|
|
long Result = MakeError(ErrorModule.Loader, LoaderErr.BadInitialization);
|
2018-10-10 01:01:49 +02:00
|
|
|
|
|
2018-11-28 23:18:09 +01:00
|
|
|
|
// Zero
|
2018-12-05 01:52:39 +01:00
|
|
|
|
Context.RequestData.ReadUInt64();
|
2018-11-28 23:18:09 +01:00
|
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
|
ulong NroMappedAddress = Context.RequestData.ReadUInt64();
|
2018-10-10 01:01:49 +02:00
|
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
|
if (IsInitialized)
|
2018-10-10 01:01:49 +02:00
|
|
|
|
{
|
2018-12-05 01:52:39 +01:00
|
|
|
|
if ((NroMappedAddress & 0xFFF) != 0)
|
2018-10-10 01:01:49 +02:00
|
|
|
|
{
|
|
|
|
|
return MakeError(ErrorModule.Loader, LoaderErr.UnalignedAddress);
|
|
|
|
|
}
|
|
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
|
Result = RemoveNroInfo(Context, NroMappedAddress);
|
2018-10-10 01:01:49 +02:00
|
|
|
|
}
|
|
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
|
return Result;
|
2018-10-10 01:01:49 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// LoadNrr(u64, u64, u64, pid)
|
2018-12-05 01:52:39 +01:00
|
|
|
|
public long LoadNrr(ServiceCtx Context)
|
2018-10-10 01:01:49 +02:00
|
|
|
|
{
|
2018-12-05 01:52:39 +01:00
|
|
|
|
long Result = MakeError(ErrorModule.Loader, LoaderErr.BadInitialization);
|
2018-10-10 01:01:49 +02:00
|
|
|
|
|
|
|
|
|
// Zero
|
2018-12-05 01:52:39 +01:00
|
|
|
|
Context.RequestData.ReadUInt64();
|
2018-10-10 01:01:49 +02:00
|
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
|
long NrrAddress = Context.RequestData.ReadInt64();
|
|
|
|
|
long NrrSize = Context.RequestData.ReadInt64();
|
2018-10-10 01:01:49 +02:00
|
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
|
if (IsInitialized)
|
2018-10-10 01:01:49 +02:00
|
|
|
|
{
|
2018-12-05 01:52:39 +01:00
|
|
|
|
NrrInfo Info;
|
|
|
|
|
Result = ParseNrr(out Info, Context, NrrAddress, NrrSize);
|
2018-10-10 01:01:49 +02:00
|
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
|
if(Result == 0)
|
2018-10-10 01:01:49 +02:00
|
|
|
|
{
|
2018-12-05 01:52:39 +01:00
|
|
|
|
if (NrrInfos.Count >= MaxNrr)
|
2018-10-10 01:01:49 +02:00
|
|
|
|
{
|
2018-12-05 01:52:39 +01:00
|
|
|
|
Result = MakeError(ErrorModule.Loader, LoaderErr.MaxNrr);
|
2018-10-10 01:01:49 +02:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2018-12-05 01:52:39 +01:00
|
|
|
|
NrrInfos.Add(Info);
|
2018-10-10 01:01:49 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
|
return Result;
|
2018-10-10 01:01:49 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// UnloadNrr(u64, u64, pid)
|
2018-12-05 01:52:39 +01:00
|
|
|
|
public long UnloadNrr(ServiceCtx Context)
|
2018-10-10 01:01:49 +02:00
|
|
|
|
{
|
2018-12-05 01:52:39 +01:00
|
|
|
|
long Result = MakeError(ErrorModule.Loader, LoaderErr.BadInitialization);
|
2018-10-10 01:01:49 +02:00
|
|
|
|
|
|
|
|
|
// Zero
|
2018-12-05 01:52:39 +01:00
|
|
|
|
Context.RequestData.ReadUInt64();
|
2018-10-10 01:01:49 +02:00
|
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
|
long NrrHeapAddress = Context.RequestData.ReadInt64();
|
2018-10-10 01:01:49 +02:00
|
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
|
if (IsInitialized)
|
2018-10-10 01:01:49 +02:00
|
|
|
|
{
|
2018-12-05 01:52:39 +01:00
|
|
|
|
if ((NrrHeapAddress & 0xFFF) != 0)
|
2018-10-10 01:01:49 +02:00
|
|
|
|
{
|
|
|
|
|
return MakeError(ErrorModule.Loader, LoaderErr.UnalignedAddress);
|
|
|
|
|
}
|
|
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
|
Result = RemoveNrrInfo(NrrHeapAddress);
|
2018-10-10 01:01:49 +02:00
|
|
|
|
}
|
|
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
|
return Result;
|
2018-10-10 01:01:49 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Initialize(u64, pid, KObject)
|
2018-12-05 01:52:39 +01:00
|
|
|
|
public long Initialize(ServiceCtx Context)
|
2018-10-10 01:01:49 +02:00
|
|
|
|
{
|
|
|
|
|
// TODO: we actually ignore the pid and process handle receive, we will need to use them when we will have multi process support.
|
2018-12-05 01:52:39 +01:00
|
|
|
|
IsInitialized = true;
|
2018-10-10 01:01:49 +02:00
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|