2018-11-28 22:18:09 +00:00
|
|
|
|
using ChocolArm64.Memory;
|
|
|
|
|
using Ryujinx.Common;
|
|
|
|
|
using Ryujinx.HLE.HOS.Ipc;
|
|
|
|
|
using Ryujinx.HLE.HOS.Kernel;
|
2018-10-09 23:01:49 +00:00
|
|
|
|
using Ryujinx.HLE.Loaders.Executables;
|
|
|
|
|
using Ryujinx.HLE.Utilities;
|
|
|
|
|
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-04 20:23:37 +00:00
|
|
|
|
public NrrHeader Header { get; }
|
|
|
|
|
public List<byte[]> Hashes { get; }
|
|
|
|
|
public long NrrAddress { get; }
|
2018-10-09 23:01:49 +00:00
|
|
|
|
|
2018-12-04 20:23:37 +00:00
|
|
|
|
public NrrInfo(long nrrAddress, NrrHeader header, List<byte[]> hashes)
|
2018-10-09 23:01:49 +00:00
|
|
|
|
{
|
2018-12-04 20:23:37 +00:00
|
|
|
|
NrrAddress = nrrAddress;
|
|
|
|
|
Header = header;
|
|
|
|
|
Hashes = hashes;
|
2018-10-09 23:01:49 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class NroInfo
|
|
|
|
|
{
|
2018-12-04 20:23:37 +00:00
|
|
|
|
public NxRelocatableObject Executable { get; }
|
|
|
|
|
|
|
|
|
|
public byte[] Hash { get; }
|
|
|
|
|
public ulong NroAddress { get; }
|
|
|
|
|
public ulong NroSize { get; }
|
|
|
|
|
public ulong BssAddress { get; }
|
|
|
|
|
public ulong BssSize { get; }
|
|
|
|
|
public ulong TotalSize { get; }
|
2018-11-28 22:18:09 +00:00
|
|
|
|
public ulong NroMappedAddress { get; set; }
|
|
|
|
|
|
|
|
|
|
public NroInfo(
|
2018-12-04 20:23:37 +00:00
|
|
|
|
NxRelocatableObject executable,
|
|
|
|
|
byte[] hash,
|
|
|
|
|
ulong nroAddress,
|
|
|
|
|
ulong nroSize,
|
|
|
|
|
ulong bssAddress,
|
|
|
|
|
ulong bssSize,
|
|
|
|
|
ulong totalSize)
|
2018-10-09 23:01:49 +00:00
|
|
|
|
{
|
2018-12-04 20:23:37 +00:00
|
|
|
|
Executable = executable;
|
|
|
|
|
Hash = hash;
|
|
|
|
|
NroAddress = nroAddress;
|
|
|
|
|
NroSize = nroSize;
|
|
|
|
|
BssAddress = bssAddress;
|
|
|
|
|
BssSize = bssSize;
|
|
|
|
|
TotalSize = totalSize;
|
2018-10-09 23:01:49 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class IRoInterface : IpcService
|
|
|
|
|
{
|
2018-12-04 20:23:37 +00:00
|
|
|
|
private Dictionary<int, ServiceProcessRequest> _commands;
|
2018-10-09 23:01:49 +00:00
|
|
|
|
|
2018-12-04 20:23:37 +00:00
|
|
|
|
public override IReadOnlyDictionary<int, ServiceProcessRequest> Commands => _commands;
|
2018-10-09 23:01:49 +00:00
|
|
|
|
|
|
|
|
|
private const int MaxNrr = 0x40;
|
|
|
|
|
private const int MaxNro = 0x40;
|
|
|
|
|
|
|
|
|
|
private const uint NrrMagic = 0x3052524E;
|
|
|
|
|
private const uint NroMagic = 0x304F524E;
|
|
|
|
|
|
2018-12-04 20:23:37 +00:00
|
|
|
|
private List<NrrInfo> _nrrInfos;
|
|
|
|
|
private List<NroInfo> _nroInfos;
|
2018-10-09 23:01:49 +00:00
|
|
|
|
|
2018-12-04 20:23:37 +00:00
|
|
|
|
private bool _isInitialized;
|
2018-10-09 23:01:49 +00:00
|
|
|
|
|
|
|
|
|
public IRoInterface()
|
|
|
|
|
{
|
2018-12-04 20:23:37 +00:00
|
|
|
|
_commands = new Dictionary<int, ServiceProcessRequest>
|
2018-10-09 23:01:49 +00:00
|
|
|
|
{
|
|
|
|
|
{ 0, LoadNro },
|
|
|
|
|
{ 1, UnloadNro },
|
|
|
|
|
{ 2, LoadNrr },
|
|
|
|
|
{ 3, UnloadNrr },
|
2018-12-04 20:23:37 +00:00
|
|
|
|
{ 4, Initialize }
|
2018-10-09 23:01:49 +00:00
|
|
|
|
};
|
|
|
|
|
|
2018-12-04 20:23:37 +00:00
|
|
|
|
_nrrInfos = new List<NrrInfo>(MaxNrr);
|
|
|
|
|
_nroInfos = new List<NroInfo>(MaxNro);
|
2018-10-09 23:01:49 +00:00
|
|
|
|
}
|
|
|
|
|
|
2018-12-04 20:23:37 +00:00
|
|
|
|
private long ParseNrr(out NrrInfo nrrInfo, ServiceCtx context, long nrrAddress, long nrrSize)
|
2018-10-09 23:01:49 +00:00
|
|
|
|
{
|
2018-12-04 20:23:37 +00:00
|
|
|
|
nrrInfo = null;
|
2018-10-09 23:01:49 +00:00
|
|
|
|
|
2018-12-04 20:23:37 +00:00
|
|
|
|
if (nrrSize == 0 || nrrAddress + nrrSize <= nrrAddress || (nrrSize & 0xFFF) != 0)
|
2018-10-09 23:01:49 +00:00
|
|
|
|
{
|
|
|
|
|
return MakeError(ErrorModule.Loader, LoaderErr.BadSize);
|
|
|
|
|
}
|
2018-12-04 20:23:37 +00:00
|
|
|
|
else if ((nrrAddress & 0xFFF) != 0)
|
2018-10-09 23:01:49 +00:00
|
|
|
|
{
|
|
|
|
|
return MakeError(ErrorModule.Loader, LoaderErr.UnalignedAddress);
|
|
|
|
|
}
|
|
|
|
|
|
2018-12-04 20:23:37 +00:00
|
|
|
|
StructReader reader = new StructReader(context.Memory, nrrAddress);
|
|
|
|
|
NrrHeader header = reader.Read<NrrHeader>();
|
2018-10-09 23:01:49 +00:00
|
|
|
|
|
2018-12-04 20:23:37 +00:00
|
|
|
|
if (header.Magic != NrrMagic)
|
2018-10-09 23:01:49 +00:00
|
|
|
|
{
|
|
|
|
|
return MakeError(ErrorModule.Loader, LoaderErr.InvalidNrr);
|
|
|
|
|
}
|
2018-12-04 20:23:37 +00:00
|
|
|
|
else if (header.NrrSize != nrrSize)
|
2018-10-09 23:01:49 +00:00
|
|
|
|
{
|
|
|
|
|
return MakeError(ErrorModule.Loader, LoaderErr.BadSize);
|
|
|
|
|
}
|
|
|
|
|
|
2018-12-04 20:23:37 +00:00
|
|
|
|
List<byte[]> hashes = new List<byte[]>();
|
2018-10-09 23:01:49 +00:00
|
|
|
|
|
2018-12-04 20:23:37 +00:00
|
|
|
|
for (int i = 0; i < header.HashCount; i++)
|
2018-10-09 23:01:49 +00:00
|
|
|
|
{
|
2018-12-04 20:23:37 +00:00
|
|
|
|
hashes.Add(context.Memory.ReadBytes(nrrAddress + header.HashOffset + (i * 0x20), 0x20));
|
2018-10-09 23:01:49 +00:00
|
|
|
|
}
|
|
|
|
|
|
2018-12-04 20:23:37 +00:00
|
|
|
|
nrrInfo = new NrrInfo(nrrAddress, header, hashes);
|
2018-10-09 23:01:49 +00:00
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2018-12-04 20:23:37 +00:00
|
|
|
|
public bool IsNroHashPresent(byte[] nroHash)
|
2018-10-09 23:01:49 +00:00
|
|
|
|
{
|
2018-12-04 20:23:37 +00:00
|
|
|
|
foreach (NrrInfo info in _nrrInfos)
|
2018-10-09 23:01:49 +00:00
|
|
|
|
{
|
2018-12-04 20:23:37 +00:00
|
|
|
|
foreach (byte[] hash in info.Hashes)
|
2018-10-09 23:01:49 +00:00
|
|
|
|
{
|
2018-12-04 20:23:37 +00:00
|
|
|
|
if (hash.SequenceEqual(nroHash))
|
2018-10-09 23:01:49 +00:00
|
|
|
|
{
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2018-12-04 20:23:37 +00:00
|
|
|
|
public bool IsNroLoaded(byte[] nroHash)
|
2018-10-09 23:01:49 +00:00
|
|
|
|
{
|
2018-12-04 20:23:37 +00:00
|
|
|
|
foreach (NroInfo info in _nroInfos)
|
2018-10-09 23:01:49 +00:00
|
|
|
|
{
|
2018-12-04 20:23:37 +00:00
|
|
|
|
if (info.Hash.SequenceEqual(nroHash))
|
2018-10-09 23:01:49 +00:00
|
|
|
|
{
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2018-12-04 20:23:37 +00:00
|
|
|
|
public long ParseNro(out NroInfo res, ServiceCtx context, ulong nroAddress, ulong nroSize, ulong bssAddress, ulong bssSize)
|
2018-10-09 23:01:49 +00:00
|
|
|
|
{
|
2018-12-04 20:23:37 +00:00
|
|
|
|
res = null;
|
2018-10-09 23:01:49 +00:00
|
|
|
|
|
2018-12-04 20:23:37 +00:00
|
|
|
|
if (_nroInfos.Count >= MaxNro)
|
2018-10-09 23:01:49 +00:00
|
|
|
|
{
|
|
|
|
|
return MakeError(ErrorModule.Loader, LoaderErr.MaxNro);
|
|
|
|
|
}
|
2018-12-04 20:23:37 +00:00
|
|
|
|
else if (nroSize == 0 || nroAddress + nroSize <= nroAddress || (nroSize & 0xFFF) != 0)
|
2018-10-09 23:01:49 +00:00
|
|
|
|
{
|
|
|
|
|
return MakeError(ErrorModule.Loader, LoaderErr.BadSize);
|
|
|
|
|
}
|
2018-12-04 20:23:37 +00:00
|
|
|
|
else if (bssSize != 0 && bssAddress + bssSize <= bssAddress)
|
2018-10-09 23:01:49 +00:00
|
|
|
|
{
|
|
|
|
|
return MakeError(ErrorModule.Loader, LoaderErr.BadSize);
|
|
|
|
|
}
|
2018-12-04 20:23:37 +00:00
|
|
|
|
else if ((nroAddress & 0xFFF) != 0)
|
2018-10-09 23:01:49 +00:00
|
|
|
|
{
|
|
|
|
|
return MakeError(ErrorModule.Loader, LoaderErr.UnalignedAddress);
|
|
|
|
|
}
|
|
|
|
|
|
2018-12-04 20:23:37 +00:00
|
|
|
|
uint magic = context.Memory.ReadUInt32((long)nroAddress + 0x10);
|
|
|
|
|
uint nroFileSize = context.Memory.ReadUInt32((long)nroAddress + 0x18);
|
2018-10-09 23:01:49 +00:00
|
|
|
|
|
2018-12-04 20:23:37 +00:00
|
|
|
|
if (magic != NroMagic || nroSize != nroFileSize)
|
2018-10-09 23:01:49 +00:00
|
|
|
|
{
|
|
|
|
|
return MakeError(ErrorModule.Loader, LoaderErr.InvalidNro);
|
|
|
|
|
}
|
|
|
|
|
|
2018-12-04 20:23:37 +00:00
|
|
|
|
byte[] nroData = context.Memory.ReadBytes((long)nroAddress, (long)nroSize);
|
|
|
|
|
byte[] nroHash = null;
|
2018-10-09 23:01:49 +00:00
|
|
|
|
|
2018-12-04 20:23:37 +00:00
|
|
|
|
MemoryStream stream = new MemoryStream(nroData);
|
2018-10-09 23:01:49 +00:00
|
|
|
|
|
2018-12-04 20:23:37 +00:00
|
|
|
|
using (SHA256 hasher = SHA256.Create())
|
2018-10-09 23:01:49 +00:00
|
|
|
|
{
|
2018-12-04 20:23:37 +00:00
|
|
|
|
nroHash = hasher.ComputeHash(stream);
|
2018-10-09 23:01:49 +00:00
|
|
|
|
}
|
|
|
|
|
|
2018-12-04 20:23:37 +00:00
|
|
|
|
if (!IsNroHashPresent(nroHash))
|
2018-10-09 23:01:49 +00:00
|
|
|
|
{
|
|
|
|
|
return MakeError(ErrorModule.Loader, LoaderErr.NroHashNotPresent);
|
|
|
|
|
}
|
|
|
|
|
|
2018-12-04 20:23:37 +00:00
|
|
|
|
if (IsNroLoaded(nroHash))
|
2018-10-09 23:01:49 +00:00
|
|
|
|
{
|
|
|
|
|
return MakeError(ErrorModule.Loader, LoaderErr.NroAlreadyLoaded);
|
|
|
|
|
}
|
|
|
|
|
|
2018-12-04 20:23:37 +00:00
|
|
|
|
stream.Position = 0;
|
2018-10-09 23:01:49 +00:00
|
|
|
|
|
2018-12-04 20:23:37 +00:00
|
|
|
|
NxRelocatableObject executable = new NxRelocatableObject(stream, nroAddress, bssAddress);
|
2018-10-09 23:01:49 +00:00
|
|
|
|
|
|
|
|
|
// check if everything is page align.
|
2018-12-04 20:23:37 +00:00
|
|
|
|
if ((executable.Text.Length & 0xFFF) != 0 || (executable.Ro.Length & 0xFFF) != 0 ||
|
|
|
|
|
(executable.Data.Length & 0xFFF) != 0 || (executable.BssSize & 0xFFF) != 0)
|
2018-10-09 23:01:49 +00:00
|
|
|
|
{
|
|
|
|
|
return MakeError(ErrorModule.Loader, LoaderErr.InvalidNro);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// check if everything is contiguous.
|
2018-12-04 20:23:37 +00:00
|
|
|
|
if (executable.RoOffset != executable.TextOffset + executable.Text.Length ||
|
|
|
|
|
executable.DataOffset != executable.RoOffset + executable.Ro.Length ||
|
|
|
|
|
nroFileSize != executable.DataOffset + executable.Data.Length)
|
2018-10-09 23:01:49 +00:00
|
|
|
|
{
|
|
|
|
|
return MakeError(ErrorModule.Loader, LoaderErr.InvalidNro);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// finally check the bss size match.
|
2018-12-04 20:23:37 +00:00
|
|
|
|
if ((ulong)executable.BssSize != bssSize)
|
2018-10-09 23:01:49 +00:00
|
|
|
|
{
|
|
|
|
|
return MakeError(ErrorModule.Loader, LoaderErr.InvalidNro);
|
|
|
|
|
}
|
|
|
|
|
|
2018-12-04 20:23:37 +00:00
|
|
|
|
int totalSize = executable.Text.Length + executable.Ro.Length + executable.Data.Length + executable.BssSize;
|
2018-11-28 22:18:09 +00:00
|
|
|
|
|
2018-12-04 20:23:37 +00:00
|
|
|
|
res = new NroInfo(
|
|
|
|
|
executable,
|
|
|
|
|
nroHash,
|
|
|
|
|
nroAddress,
|
|
|
|
|
nroSize,
|
|
|
|
|
bssAddress,
|
|
|
|
|
bssSize,
|
|
|
|
|
(ulong)totalSize);
|
2018-10-09 23:01:49 +00:00
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2018-12-04 20:23:37 +00:00
|
|
|
|
private long MapNro(ServiceCtx context, NroInfo info, out ulong nroMappedAddress)
|
2018-10-09 23:01:49 +00:00
|
|
|
|
{
|
2018-12-04 20:23:37 +00:00
|
|
|
|
nroMappedAddress = 0;
|
2018-10-09 23:01:49 +00:00
|
|
|
|
|
2018-12-04 20:23:37 +00:00
|
|
|
|
KMemoryManager memMgr = context.Process.MemoryManager;
|
2018-10-09 23:01:49 +00:00
|
|
|
|
|
2018-12-04 20:23:37 +00:00
|
|
|
|
ulong targetAddress = memMgr.GetAddrSpaceBaseAddr();
|
2018-10-09 23:01:49 +00:00
|
|
|
|
|
|
|
|
|
while (true)
|
|
|
|
|
{
|
2018-12-04 20:23:37 +00:00
|
|
|
|
if (targetAddress + info.TotalSize >= memMgr.AddrSpaceEnd)
|
2018-10-09 23:01:49 +00:00
|
|
|
|
{
|
|
|
|
|
return MakeError(ErrorModule.Loader, LoaderErr.InvalidMemoryState);
|
|
|
|
|
}
|
|
|
|
|
|
2018-12-04 20:23:37 +00:00
|
|
|
|
KMemoryInfo memInfo = memMgr.QueryMemory(targetAddress);
|
2018-10-09 23:01:49 +00:00
|
|
|
|
|
2018-12-04 20:23:37 +00:00
|
|
|
|
if (memInfo.State == MemoryState.Unmapped && memInfo.Size >= info.TotalSize)
|
2018-10-09 23:01:49 +00:00
|
|
|
|
{
|
2018-12-04 20:23:37 +00:00
|
|
|
|
if (!memMgr.InsideHeapRegion (targetAddress, info.TotalSize) &&
|
|
|
|
|
!memMgr.InsideAliasRegion(targetAddress, info.TotalSize))
|
2018-11-28 22:18:09 +00:00
|
|
|
|
{
|
|
|
|
|
break;
|
|
|
|
|
}
|
2018-10-09 23:01:49 +00:00
|
|
|
|
}
|
|
|
|
|
|
2018-12-04 20:23:37 +00:00
|
|
|
|
targetAddress += memInfo.Size;
|
2018-11-28 22:18:09 +00:00
|
|
|
|
}
|
|
|
|
|
|
2018-12-04 20:23:37 +00:00
|
|
|
|
KernelResult result = memMgr.MapProcessCodeMemory(targetAddress, info.NroAddress, info.NroSize);
|
2018-11-28 22:18:09 +00:00
|
|
|
|
|
2018-12-04 20:23:37 +00:00
|
|
|
|
if (result != KernelResult.Success)
|
2018-11-28 22:18:09 +00:00
|
|
|
|
{
|
|
|
|
|
return MakeError(ErrorModule.Loader, LoaderErr.InvalidMemoryState);
|
|
|
|
|
}
|
|
|
|
|
|
2018-12-04 20:23:37 +00:00
|
|
|
|
ulong bssTargetAddress = targetAddress + info.NroSize;
|
2018-11-28 22:18:09 +00:00
|
|
|
|
|
2018-12-04 20:23:37 +00:00
|
|
|
|
if (info.BssSize != 0)
|
2018-11-28 22:18:09 +00:00
|
|
|
|
{
|
2018-12-04 20:23:37 +00:00
|
|
|
|
result = memMgr.MapProcessCodeMemory(bssTargetAddress, info.BssAddress, info.BssSize);
|
2018-11-28 22:18:09 +00:00
|
|
|
|
|
2018-12-04 20:23:37 +00:00
|
|
|
|
if (result != KernelResult.Success)
|
2018-11-28 22:18:09 +00:00
|
|
|
|
{
|
2018-12-04 20:23:37 +00:00
|
|
|
|
memMgr.UnmapProcessCodeMemory(targetAddress, info.NroAddress, info.NroSize);
|
2018-11-28 22:18:09 +00:00
|
|
|
|
|
|
|
|
|
return MakeError(ErrorModule.Loader, LoaderErr.InvalidMemoryState);
|
|
|
|
|
}
|
2018-10-09 23:01:49 +00:00
|
|
|
|
}
|
|
|
|
|
|
2018-12-04 20:23:37 +00:00
|
|
|
|
result = LoadNroIntoMemory(context.Process, info.Executable, targetAddress);
|
2018-11-28 22:18:09 +00:00
|
|
|
|
|
2018-12-04 20:23:37 +00:00
|
|
|
|
if (result != KernelResult.Success)
|
2018-11-28 22:18:09 +00:00
|
|
|
|
{
|
2018-12-04 20:23:37 +00:00
|
|
|
|
memMgr.UnmapProcessCodeMemory(targetAddress, info.NroAddress, info.NroSize);
|
2018-11-28 22:18:09 +00:00
|
|
|
|
|
2018-12-04 20:23:37 +00:00
|
|
|
|
if (info.BssSize != 0)
|
2018-11-28 22:18:09 +00:00
|
|
|
|
{
|
2018-12-04 20:23:37 +00:00
|
|
|
|
memMgr.UnmapProcessCodeMemory(bssTargetAddress, info.BssAddress, info.BssSize);
|
2018-11-28 22:18:09 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
2018-10-09 23:01:49 +00:00
|
|
|
|
|
2018-12-04 20:23:37 +00:00
|
|
|
|
info.NroMappedAddress = targetAddress;
|
|
|
|
|
nroMappedAddress = targetAddress;
|
2018-10-09 23:01:49 +00:00
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2018-12-04 20:23:37 +00:00
|
|
|
|
private KernelResult LoadNroIntoMemory(KProcess process, IExecutable relocatableObject, ulong baseAddress)
|
2018-11-28 22:18:09 +00:00
|
|
|
|
{
|
2018-12-04 20:23:37 +00:00
|
|
|
|
ulong textStart = baseAddress + (ulong)relocatableObject.TextOffset;
|
|
|
|
|
ulong roStart = baseAddress + (ulong)relocatableObject.RoOffset;
|
|
|
|
|
ulong dataStart = baseAddress + (ulong)relocatableObject.DataOffset;
|
2018-11-28 22:18:09 +00:00
|
|
|
|
|
2018-12-04 20:23:37 +00:00
|
|
|
|
ulong bssStart = dataStart + (ulong)relocatableObject.Data.Length;
|
2018-11-28 22:18:09 +00:00
|
|
|
|
|
2018-12-04 20:23:37 +00:00
|
|
|
|
ulong bssEnd = BitUtils.AlignUp(bssStart + (ulong)relocatableObject.BssSize, KMemoryManager.PageSize);
|
2018-11-28 22:18:09 +00:00
|
|
|
|
|
2018-12-04 20:23:37 +00: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 22:18:09 +00:00
|
|
|
|
|
2018-12-04 20:23:37 +00:00
|
|
|
|
MemoryHelper.FillWithZeros(process.CpuMemory, (long)bssStart, (int)(bssEnd - bssStart));
|
2018-11-28 22:18:09 +00:00
|
|
|
|
|
2018-12-04 20:23:37 +00:00
|
|
|
|
KernelResult result;
|
2018-11-28 22:18:09 +00:00
|
|
|
|
|
2018-12-04 20:23:37 +00:00
|
|
|
|
result = process.MemoryManager.SetProcessMemoryPermission(textStart, roStart - textStart, MemoryPermission.ReadAndExecute);
|
2018-11-28 22:18:09 +00:00
|
|
|
|
|
2018-12-04 20:23:37 +00:00
|
|
|
|
if (result != KernelResult.Success)
|
2018-11-28 22:18:09 +00:00
|
|
|
|
{
|
2018-12-04 20:23:37 +00:00
|
|
|
|
return result;
|
2018-11-28 22:18:09 +00:00
|
|
|
|
}
|
|
|
|
|
|
2018-12-04 20:23:37 +00:00
|
|
|
|
result = process.MemoryManager.SetProcessMemoryPermission(roStart, dataStart - roStart, MemoryPermission.Read);
|
2018-11-28 22:18:09 +00:00
|
|
|
|
|
2018-12-04 20:23:37 +00:00
|
|
|
|
if (result != KernelResult.Success)
|
2018-11-28 22:18:09 +00:00
|
|
|
|
{
|
2018-12-04 20:23:37 +00:00
|
|
|
|
return result;
|
2018-11-28 22:18:09 +00:00
|
|
|
|
}
|
|
|
|
|
|
2018-12-04 20:23:37 +00:00
|
|
|
|
return process.MemoryManager.SetProcessMemoryPermission(dataStart, bssEnd - dataStart, MemoryPermission.ReadAndWrite);
|
2018-11-28 22:18:09 +00:00
|
|
|
|
}
|
|
|
|
|
|
2018-12-04 20:23:37 +00:00
|
|
|
|
private long RemoveNrrInfo(long nrrAddress)
|
2018-10-09 23:01:49 +00:00
|
|
|
|
{
|
2018-12-04 20:23:37 +00:00
|
|
|
|
foreach (NrrInfo info in _nrrInfos)
|
2018-10-09 23:01:49 +00:00
|
|
|
|
{
|
2018-12-04 20:23:37 +00:00
|
|
|
|
if (info.NrrAddress == nrrAddress)
|
2018-10-09 23:01:49 +00:00
|
|
|
|
{
|
2018-12-04 20:23:37 +00:00
|
|
|
|
_nrrInfos.Remove(info);
|
2018-10-09 23:01:49 +00:00
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return MakeError(ErrorModule.Loader, LoaderErr.BadNrrAddress);
|
|
|
|
|
}
|
|
|
|
|
|
2018-12-04 20:23:37 +00:00
|
|
|
|
private long RemoveNroInfo(ServiceCtx context, ulong nroMappedAddress)
|
2018-10-09 23:01:49 +00:00
|
|
|
|
{
|
2018-12-04 20:23:37 +00:00
|
|
|
|
foreach (NroInfo info in _nroInfos)
|
2018-10-09 23:01:49 +00:00
|
|
|
|
{
|
2018-12-04 20:23:37 +00:00
|
|
|
|
if (info.NroMappedAddress == nroMappedAddress)
|
2018-10-09 23:01:49 +00:00
|
|
|
|
{
|
2018-12-04 20:23:37 +00:00
|
|
|
|
_nroInfos.Remove(info);
|
2018-10-09 23:01:49 +00:00
|
|
|
|
|
2018-12-04 20:23:37 +00: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-09 23:01:49 +00:00
|
|
|
|
|
2018-12-04 20:23:37 +00:00
|
|
|
|
KernelResult result = KernelResult.Success;
|
2018-10-09 23:01:49 +00:00
|
|
|
|
|
2018-12-04 20:23:37 +00:00
|
|
|
|
if (info.Executable.BssSize != 0)
|
2018-10-09 23:01:49 +00:00
|
|
|
|
{
|
2018-12-04 20:23:37 +00:00
|
|
|
|
result = context.Process.MemoryManager.UnmapProcessCodeMemory(
|
|
|
|
|
info.NroMappedAddress + textSize + roSize + dataSize,
|
|
|
|
|
info.Executable.BssAddress,
|
|
|
|
|
bssSize);
|
2018-10-09 23:01:49 +00:00
|
|
|
|
}
|
|
|
|
|
|
2018-12-04 20:23:37 +00:00
|
|
|
|
if (result == KernelResult.Success)
|
2018-11-28 22:18:09 +00:00
|
|
|
|
{
|
2018-12-04 20:23:37 +00:00
|
|
|
|
result = context.Process.MemoryManager.UnmapProcessCodeMemory(
|
|
|
|
|
info.NroMappedAddress + textSize + roSize,
|
|
|
|
|
info.Executable.SourceAddress + textSize + roSize,
|
|
|
|
|
dataSize);
|
2018-11-28 22:18:09 +00:00
|
|
|
|
|
2018-12-04 20:23:37 +00:00
|
|
|
|
if (result == KernelResult.Success)
|
2018-11-28 22:18:09 +00:00
|
|
|
|
{
|
2018-12-04 20:23:37 +00:00
|
|
|
|
result = context.Process.MemoryManager.UnmapProcessCodeMemory(
|
|
|
|
|
info.NroMappedAddress,
|
|
|
|
|
info.Executable.SourceAddress,
|
|
|
|
|
textSize + roSize);
|
2018-11-28 22:18:09 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-12-04 20:23:37 +00:00
|
|
|
|
return (long)result;
|
2018-10-09 23:01:49 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return MakeError(ErrorModule.Loader, LoaderErr.BadNroAddress);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// LoadNro(u64, u64, u64, u64, u64, pid) -> u64
|
2018-12-04 20:23:37 +00:00
|
|
|
|
public long LoadNro(ServiceCtx context)
|
2018-10-09 23:01:49 +00:00
|
|
|
|
{
|
2018-12-04 20:23:37 +00:00
|
|
|
|
long result = MakeError(ErrorModule.Loader, LoaderErr.BadInitialization);
|
2018-10-09 23:01:49 +00:00
|
|
|
|
|
|
|
|
|
// Zero
|
2018-12-04 20:23:37 +00:00
|
|
|
|
context.RequestData.ReadUInt64();
|
2018-10-09 23:01:49 +00:00
|
|
|
|
|
2018-12-04 20:23:37 +00:00
|
|
|
|
ulong nroHeapAddress = context.RequestData.ReadUInt64();
|
|
|
|
|
ulong nroSize = context.RequestData.ReadUInt64();
|
|
|
|
|
ulong bssHeapAddress = context.RequestData.ReadUInt64();
|
|
|
|
|
ulong bssSize = context.RequestData.ReadUInt64();
|
2018-10-09 23:01:49 +00:00
|
|
|
|
|
2018-12-04 20:23:37 +00:00
|
|
|
|
ulong nroMappedAddress = 0;
|
2018-10-09 23:01:49 +00:00
|
|
|
|
|
2018-12-04 20:23:37 +00:00
|
|
|
|
if (_isInitialized)
|
2018-10-09 23:01:49 +00:00
|
|
|
|
{
|
2018-12-04 20:23:37 +00:00
|
|
|
|
NroInfo info;
|
2018-10-09 23:01:49 +00:00
|
|
|
|
|
2018-12-04 20:23:37 +00:00
|
|
|
|
result = ParseNro(out info, context, nroHeapAddress, nroSize, bssHeapAddress, bssSize);
|
2018-10-09 23:01:49 +00:00
|
|
|
|
|
2018-12-04 20:23:37 +00:00
|
|
|
|
if (result == 0)
|
2018-10-09 23:01:49 +00:00
|
|
|
|
{
|
2018-12-04 20:23:37 +00:00
|
|
|
|
result = MapNro(context, info, out nroMappedAddress);
|
2018-10-09 23:01:49 +00:00
|
|
|
|
|
2018-12-04 20:23:37 +00:00
|
|
|
|
if (result == 0)
|
2018-10-09 23:01:49 +00:00
|
|
|
|
{
|
2018-12-04 20:23:37 +00:00
|
|
|
|
_nroInfos.Add(info);
|
2018-10-09 23:01:49 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-12-04 20:23:37 +00:00
|
|
|
|
context.ResponseData.Write(nroMappedAddress);
|
2018-10-09 23:01:49 +00:00
|
|
|
|
|
2018-12-04 20:23:37 +00:00
|
|
|
|
return result;
|
2018-10-09 23:01:49 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// UnloadNro(u64, u64, pid)
|
2018-12-04 20:23:37 +00:00
|
|
|
|
public long UnloadNro(ServiceCtx context)
|
2018-10-09 23:01:49 +00:00
|
|
|
|
{
|
2018-12-04 20:23:37 +00:00
|
|
|
|
long result = MakeError(ErrorModule.Loader, LoaderErr.BadInitialization);
|
2018-10-09 23:01:49 +00:00
|
|
|
|
|
2018-11-28 22:18:09 +00:00
|
|
|
|
// Zero
|
2018-12-04 20:23:37 +00:00
|
|
|
|
context.RequestData.ReadUInt64();
|
2018-11-28 22:18:09 +00:00
|
|
|
|
|
2018-12-04 20:23:37 +00:00
|
|
|
|
ulong nroMappedAddress = context.RequestData.ReadUInt64();
|
2018-10-09 23:01:49 +00:00
|
|
|
|
|
2018-12-04 20:23:37 +00:00
|
|
|
|
if (_isInitialized)
|
2018-10-09 23:01:49 +00:00
|
|
|
|
{
|
2018-12-04 20:23:37 +00:00
|
|
|
|
if ((nroMappedAddress & 0xFFF) != 0)
|
2018-10-09 23:01:49 +00:00
|
|
|
|
{
|
|
|
|
|
return MakeError(ErrorModule.Loader, LoaderErr.UnalignedAddress);
|
|
|
|
|
}
|
|
|
|
|
|
2018-12-04 20:23:37 +00:00
|
|
|
|
result = RemoveNroInfo(context, nroMappedAddress);
|
2018-10-09 23:01:49 +00:00
|
|
|
|
}
|
|
|
|
|
|
2018-12-04 20:23:37 +00:00
|
|
|
|
return result;
|
2018-10-09 23:01:49 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// LoadNrr(u64, u64, u64, pid)
|
2018-12-04 20:23:37 +00:00
|
|
|
|
public long LoadNrr(ServiceCtx context)
|
2018-10-09 23:01:49 +00:00
|
|
|
|
{
|
2018-12-04 20:23:37 +00:00
|
|
|
|
long result = MakeError(ErrorModule.Loader, LoaderErr.BadInitialization);
|
2018-10-09 23:01:49 +00:00
|
|
|
|
|
|
|
|
|
// Zero
|
2018-12-04 20:23:37 +00:00
|
|
|
|
context.RequestData.ReadUInt64();
|
2018-10-09 23:01:49 +00:00
|
|
|
|
|
2018-12-04 20:23:37 +00:00
|
|
|
|
long nrrAddress = context.RequestData.ReadInt64();
|
|
|
|
|
long nrrSize = context.RequestData.ReadInt64();
|
2018-10-09 23:01:49 +00:00
|
|
|
|
|
2018-12-04 20:23:37 +00:00
|
|
|
|
if (_isInitialized)
|
2018-10-09 23:01:49 +00:00
|
|
|
|
{
|
2018-12-04 20:23:37 +00:00
|
|
|
|
NrrInfo info;
|
|
|
|
|
result = ParseNrr(out info, context, nrrAddress, nrrSize);
|
2018-10-09 23:01:49 +00:00
|
|
|
|
|
2018-12-04 20:23:37 +00:00
|
|
|
|
if(result == 0)
|
2018-10-09 23:01:49 +00:00
|
|
|
|
{
|
2018-12-04 20:23:37 +00:00
|
|
|
|
if (_nrrInfos.Count >= MaxNrr)
|
2018-10-09 23:01:49 +00:00
|
|
|
|
{
|
2018-12-04 20:23:37 +00:00
|
|
|
|
result = MakeError(ErrorModule.Loader, LoaderErr.MaxNrr);
|
2018-10-09 23:01:49 +00:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2018-12-04 20:23:37 +00:00
|
|
|
|
_nrrInfos.Add(info);
|
2018-10-09 23:01:49 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-12-04 20:23:37 +00:00
|
|
|
|
return result;
|
2018-10-09 23:01:49 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// UnloadNrr(u64, u64, pid)
|
2018-12-04 20:23:37 +00:00
|
|
|
|
public long UnloadNrr(ServiceCtx context)
|
2018-10-09 23:01:49 +00:00
|
|
|
|
{
|
2018-12-04 20:23:37 +00:00
|
|
|
|
long result = MakeError(ErrorModule.Loader, LoaderErr.BadInitialization);
|
2018-10-09 23:01:49 +00:00
|
|
|
|
|
|
|
|
|
// Zero
|
2018-12-04 20:23:37 +00:00
|
|
|
|
context.RequestData.ReadUInt64();
|
2018-10-09 23:01:49 +00:00
|
|
|
|
|
2018-12-04 20:23:37 +00:00
|
|
|
|
long nrrHeapAddress = context.RequestData.ReadInt64();
|
2018-10-09 23:01:49 +00:00
|
|
|
|
|
2018-12-04 20:23:37 +00:00
|
|
|
|
if (_isInitialized)
|
2018-10-09 23:01:49 +00:00
|
|
|
|
{
|
2018-12-04 20:23:37 +00:00
|
|
|
|
if ((nrrHeapAddress & 0xFFF) != 0)
|
2018-10-09 23:01:49 +00:00
|
|
|
|
{
|
|
|
|
|
return MakeError(ErrorModule.Loader, LoaderErr.UnalignedAddress);
|
|
|
|
|
}
|
|
|
|
|
|
2018-12-04 20:23:37 +00:00
|
|
|
|
result = RemoveNrrInfo(nrrHeapAddress);
|
2018-10-09 23:01:49 +00:00
|
|
|
|
}
|
|
|
|
|
|
2018-12-04 20:23:37 +00:00
|
|
|
|
return result;
|
2018-10-09 23:01:49 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Initialize(u64, pid, KObject)
|
2018-12-04 20:23:37 +00:00
|
|
|
|
public long Initialize(ServiceCtx context)
|
2018-10-09 23:01:49 +00: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-04 20:23:37 +00:00
|
|
|
|
_isInitialized = true;
|
2018-10-09 23:01:49 +00:00
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|