HLE: Refactoring of ApplicationLoader (#4480)

* HLE: Refactoring of ApplicationLoader

* Fix SDL2 Headless

* Addresses gdkchan feedback

* Fixes LoadUnpackedNca RomFS loading

* remove useless casting

* Cleanup and fixe empty application name

* Remove ProcessInfo

* Fixes typo

* ActiveProcess to ActiveApplication

* Update check

* Clean using.

* Use the correct filepath when loading Homebrew.npdm

* Fix NRE in ProcessResult if MetaLoader is null

* Add more checks for valid processId & return success

* Add missing logging statement for npdm error

* Return result for LoadKip()

* Move error logging out of PFS load extension method

This avoids logging "Could not find Main NCA"
followed by "Loading main..." when trying to start hbl.

* Fix GUIs not checking load results

* Fix style and formatting issues

* Fix formatting and wording

* gtk: Refactor LoadApplication()

---------

Co-authored-by: TSR Berry <20988865+TSRBerry@users.noreply.github.com>
This commit is contained in:
Ac_K 2023-03-31 21:16:46 +02:00 committed by GitHub
parent 8198b99935
commit 4c2d9ff3ff
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
41 changed files with 1567 additions and 1322 deletions

View file

@ -0,0 +1,133 @@
using LibHac.Common;
using LibHac.Fs;
using LibHac.Fs.Fsa;
using LibHac.Loader;
using LibHac.Ns;
using LibHac.Tools.FsSystem;
using Ryujinx.Common.Configuration;
using Ryujinx.Common.Logging;
using Ryujinx.HLE.Loaders.Executables;
using Ryujinx.Memory;
using System.Linq;
using static Ryujinx.HLE.HOS.ModLoader;
namespace Ryujinx.HLE.Loaders.Processes.Extensions
{
static class FileSystemExtensions
{
public static MetaLoader GetNpdm(this IFileSystem fileSystem)
{
MetaLoader metaLoader = new();
if (fileSystem == null || !fileSystem.FileExists(ProcessConst.MainNpdmPath))
{
Logger.Warning?.Print(LogClass.Loader, "NPDM file not found, using default values!");
metaLoader.LoadDefault();
}
else
{
metaLoader.LoadFromFile(fileSystem);
}
return metaLoader;
}
public static ProcessResult Load(this IFileSystem exeFs, Switch device, BlitStruct<ApplicationControlProperty> nacpData, MetaLoader metaLoader, bool isHomebrew = false)
{
ulong programId = metaLoader.GetProgramId();
// Replace the whole ExeFs partition by the modded one.
if (device.Configuration.VirtualFileSystem.ModLoader.ReplaceExefsPartition(programId, ref exeFs))
{
metaLoader = null;
}
// Reload the MetaLoader in case of ExeFs partition replacement.
metaLoader ??= exeFs.GetNpdm();
NsoExecutable[] nsoExecutables = new NsoExecutable[ProcessConst.ExeFsPrefixes.Length];
for (int i = 0; i < nsoExecutables.Length; i++)
{
string name = ProcessConst.ExeFsPrefixes[i];
if (!exeFs.FileExists($"/{name}"))
{
continue; // File doesn't exist, skip.
}
Logger.Info?.Print(LogClass.Loader, $"Loading {name}...");
using var nsoFile = new UniqueRef<IFile>();
exeFs.OpenFile(ref nsoFile.Ref, $"/{name}".ToU8Span(), OpenMode.Read).ThrowIfFailure();
nsoExecutables[i] = new NsoExecutable(nsoFile.Release().AsStorage(), name);
}
// ExeFs file replacements.
ModLoadResult modLoadResult = device.Configuration.VirtualFileSystem.ModLoader.ApplyExefsMods(programId, nsoExecutables);
// Take the Npdm from mods if present.
if (modLoadResult.Npdm != null)
{
metaLoader = modLoadResult.Npdm;
}
// Collect the Nsos, ignoring ones that aren't used.
nsoExecutables = nsoExecutables.Where(x => x != null).ToArray();
// Apply Nsos patches.
device.Configuration.VirtualFileSystem.ModLoader.ApplyNsoPatches(programId, nsoExecutables);
// Don't use PTC if ExeFS files have been replaced.
bool enablePtc = device.System.EnablePtc && !modLoadResult.Modified;
if (!enablePtc)
{
Logger.Warning?.Print(LogClass.Ptc, $"Detected unsupported ExeFs modifications. PTC disabled.");
}
// We allow it for nx-hbloader because it can be used to launch homebrew.
bool allowCodeMemoryForJit = programId == 0x010000000000100DUL || isHomebrew;
string programName = "";
if (!isHomebrew && programId > 0x010000000000FFFF)
{
programName = nacpData.Value.Title[(int)device.System.State.DesiredTitleLanguage].NameString.ToString();
if (string.IsNullOrWhiteSpace(programName))
{
programName = nacpData.Value.Title.ItemsRo.ToArray().FirstOrDefault(x => x.Name[0] != 0).NameString.ToString();
}
}
// Initialize GPU.
Graphics.Gpu.GraphicsConfig.TitleId = $"{programId:x16}";
device.Gpu.HostInitalized.Set();
if (!MemoryBlock.SupportsFlags(MemoryAllocationFlags.ViewCompatible))
{
device.Configuration.MemoryManagerMode = MemoryManagerMode.SoftwarePageTable;
}
ProcessResult processResult = ProcessLoaderHelper.LoadNsos(
device,
device.System.KernelContext,
metaLoader,
nacpData.Value,
enablePtc,
allowCodeMemoryForJit,
programName,
metaLoader.GetProgramId(),
null,
nsoExecutables);
// TODO: This should be stored using ProcessId instead.
device.System.LibHacHorizonManager.ArpIReader.ApplicationId = new LibHac.ApplicationId(metaLoader.GetProgramId());
return processResult;
}
}
}

View file

@ -0,0 +1,39 @@
using LibHac.Common;
using LibHac.FsSystem;
using LibHac.Loader;
using LibHac.Ns;
using Ryujinx.HLE.Loaders.Processes.Extensions;
using ApplicationId = LibHac.Ncm.ApplicationId;
namespace Ryujinx.HLE.Loaders.Processes
{
static class LocalFileSystemExtensions
{
public static ProcessResult Load(this LocalFileSystem exeFs, Switch device, string romFsPath = "")
{
MetaLoader metaLoader = exeFs.GetNpdm();
var nacpData = new BlitStruct<ApplicationControlProperty>(1);
ulong programId = metaLoader.GetProgramId();
device.Configuration.VirtualFileSystem.ModLoader.CollectMods(
new[] { programId },
device.Configuration.VirtualFileSystem.ModLoader.GetModsBasePath(),
device.Configuration.VirtualFileSystem.ModLoader.GetSdModsBasePath());
if (programId != 0)
{
ProcessLoaderHelper.EnsureSaveData(device, new ApplicationId(programId), nacpData);
}
ProcessResult processResult = exeFs.Load(device, nacpData, metaLoader);
// Load RomFS.
if (!string.IsNullOrEmpty(romFsPath))
{
device.Configuration.VirtualFileSystem.LoadRomFs(processResult.ProcessId, romFsPath);
}
return processResult;
}
}
}

View file

@ -0,0 +1,61 @@
using LibHac.Common;
using LibHac.Fs;
using LibHac.Fs.Fsa;
using LibHac.Loader;
using LibHac.Util;
using Ryujinx.Common;
using System;
namespace Ryujinx.HLE.Loaders.Processes.Extensions
{
public static class MetaLoaderExtensions
{
public static ulong GetProgramId(this MetaLoader metaLoader)
{
metaLoader.GetNpdm(out var npdm).ThrowIfFailure();
return npdm.Aci.ProgramId.Value;
}
public static string GetProgramName(this MetaLoader metaLoader)
{
metaLoader.GetNpdm(out var npdm).ThrowIfFailure();
return StringUtils.Utf8ZToString(npdm.Meta.ProgramName);
}
public static bool IsProgram64Bit(this MetaLoader metaLoader)
{
metaLoader.GetNpdm(out var npdm).ThrowIfFailure();
return (npdm.Meta.Flags & 1) != 0;
}
public static void LoadDefault(this MetaLoader metaLoader)
{
byte[] npdmBuffer = EmbeddedResources.Read("Ryujinx.HLE/Homebrew.npdm");
metaLoader.Load(npdmBuffer).ThrowIfFailure();
}
public static void LoadFromFile(this MetaLoader metaLoader, IFileSystem fileSystem, string path = "")
{
if (string.IsNullOrEmpty(path))
{
path = ProcessConst.MainNpdmPath;
}
using var npdmFile = new UniqueRef<IFile>();
fileSystem.OpenFile(ref npdmFile.Ref, path.ToU8Span(), OpenMode.Read).ThrowIfFailure();
npdmFile.Get.GetSize(out long fileSize).ThrowIfFailure();
Span<byte> npdmBuffer = new byte[fileSize];
npdmFile.Get.Read(out _, 0, npdmBuffer).ThrowIfFailure();
metaLoader.Load(npdmBuffer).ThrowIfFailure();
}
}
}

View file

@ -0,0 +1,175 @@
using LibHac;
using LibHac.Common;
using LibHac.Fs;
using LibHac.Fs.Fsa;
using LibHac.Loader;
using LibHac.Ncm;
using LibHac.Ns;
using LibHac.Tools.FsSystem;
using LibHac.Tools.FsSystem.NcaUtils;
using Ryujinx.Common.Logging;
using System.IO;
using System.Linq;
using ApplicationId = LibHac.Ncm.ApplicationId;
namespace Ryujinx.HLE.Loaders.Processes.Extensions
{
static class NcaExtensions
{
public static ProcessResult Load(this Nca nca, Switch device, Nca patchNca, Nca controlNca)
{
// Extract RomFs and ExeFs from NCA.
IStorage romFs = nca.GetRomFs(device, patchNca);
IFileSystem exeFs = nca.GetExeFs(device, patchNca);
if (exeFs == null)
{
Logger.Error?.Print(LogClass.Loader, "No ExeFS found in NCA");
return ProcessResult.Failed;
}
// Load Npdm file.
MetaLoader metaLoader = exeFs.GetNpdm();
// Collecting mods related to AocTitleIds and ProgramId.
device.Configuration.VirtualFileSystem.ModLoader.CollectMods(
device.Configuration.ContentManager.GetAocTitleIds().Prepend(metaLoader.GetProgramId()),
device.Configuration.VirtualFileSystem.ModLoader.GetModsBasePath(),
device.Configuration.VirtualFileSystem.ModLoader.GetSdModsBasePath());
// Load Nacp file.
var nacpData = new BlitStruct<ApplicationControlProperty>(1);
if (controlNca != null)
{
nacpData = controlNca.GetNacp(device);
}
/* TODO: Rework this since it's wrong and doesn't work as it takes the DisplayVersion from a "potential" inexistant update.
// Load program 0 control NCA as we are going to need it for display version.
(_, Nca updateProgram0ControlNca) = GetGameUpdateData(_device.Configuration.VirtualFileSystem, mainNca.Header.TitleId.ToString("x16"), 0, out _);
// NOTE: Nintendo doesn't guarantee that the display version will be updated on sub programs when updating a multi program application.
// As such, to avoid PTC cache confusion, we only trust the program 0 display version when launching a sub program.
if (updateProgram0ControlNca != null && _device.Configuration.UserChannelPersistence.Index != 0)
{
nacpData.Value.DisplayVersion = updateProgram0ControlNca.GetNacp(_device).Value.DisplayVersion;
}
*/
ProcessResult processResult = exeFs.Load(device, nacpData, metaLoader);
// Load RomFS.
if (romFs == null)
{
Logger.Warning?.Print(LogClass.Loader, "No RomFS found in NCA");
}
else
{
romFs = device.Configuration.VirtualFileSystem.ModLoader.ApplyRomFsMods(processResult.ProgramId, romFs);
device.Configuration.VirtualFileSystem.SetRomFs(processResult.ProcessId, romFs.AsStream(FileAccess.Read));
}
// Don't create save data for system programs.
if (processResult.ProgramId != 0 && (processResult.ProgramId < SystemProgramId.Start.Value || processResult.ProgramId > SystemAppletId.End.Value))
{
// Multi-program applications can technically use any program ID for the main program, but in practice they always use 0 in the low nibble.
// We'll know if this changes in the future because applications will get errors when trying to mount the correct save.
ProcessLoaderHelper.EnsureSaveData(device, new ApplicationId(processResult.ProgramId & ~0xFul), nacpData);
}
return processResult;
}
public static int GetProgramIndex(this Nca nca)
{
return (int)(nca.Header.TitleId & 0xF);
}
public static bool IsProgram(this Nca nca)
{
return nca.Header.ContentType == NcaContentType.Program;
}
public static bool IsPatch(this Nca nca)
{
int dataIndex = Nca.GetSectionIndexFromType(NcaSectionType.Data, NcaContentType.Program);
return nca.IsProgram() && nca.SectionExists(NcaSectionType.Data) && nca.Header.GetFsHeader(dataIndex).IsPatchSection();
}
public static bool IsControl(this Nca nca)
{
return nca.Header.ContentType == NcaContentType.Control;
}
public static IFileSystem GetExeFs(this Nca nca, Switch device, Nca patchNca = null)
{
IFileSystem exeFs = null;
if (patchNca == null)
{
if (nca.CanOpenSection(NcaSectionType.Code))
{
exeFs = nca.OpenFileSystem(NcaSectionType.Code, device.System.FsIntegrityCheckLevel);
}
}
else
{
if (patchNca.CanOpenSection(NcaSectionType.Code))
{
exeFs = nca.OpenFileSystemWithPatch(patchNca, NcaSectionType.Code, device.System.FsIntegrityCheckLevel);
}
}
return exeFs;
}
public static IStorage GetRomFs(this Nca nca, Switch device, Nca patchNca = null)
{
IStorage romFs = null;
if (patchNca == null)
{
if (nca.CanOpenSection(NcaSectionType.Data))
{
romFs = nca.OpenStorage(NcaSectionType.Data, device.System.FsIntegrityCheckLevel);
}
}
else
{
if (patchNca.CanOpenSection(NcaSectionType.Data))
{
romFs = nca.OpenStorageWithPatch(patchNca, NcaSectionType.Data, device.System.FsIntegrityCheckLevel);
}
}
return romFs;
}
public static BlitStruct<ApplicationControlProperty> GetNacp(this Nca controlNca, Switch device)
{
var nacpData = new BlitStruct<ApplicationControlProperty>(1);
using var controlFile = new UniqueRef<IFile>();
Result result = controlNca.OpenFileSystem(NcaSectionType.Data, device.System.FsIntegrityCheckLevel)
.OpenFile(ref controlFile.Ref, "/control.nacp".ToU8Span(), OpenMode.Read);
if (result.IsSuccess())
{
result = controlFile.Get.Read(out long bytesRead, 0, nacpData.ByteSpan, ReadOption.None);
}
else
{
nacpData.ByteSpan.Clear();
}
return nacpData;
}
}
}

View file

@ -0,0 +1,177 @@
using LibHac.Common;
using LibHac.Fs;
using LibHac.Fs.Fsa;
using LibHac.FsSystem;
using LibHac.Tools.Fs;
using LibHac.Tools.FsSystem;
using LibHac.Tools.FsSystem.NcaUtils;
using Ryujinx.Common.Configuration;
using Ryujinx.Common.Logging;
using Ryujinx.Common.Utilities;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
namespace Ryujinx.HLE.Loaders.Processes.Extensions
{
public static class PartitionFileSystemExtensions
{
internal static (bool, ProcessResult) TryLoad(this PartitionFileSystem partitionFileSystem, Switch device, string path, out string errorMessage)
{
errorMessage = null;
// Load required NCAs.
Nca mainNca = null;
Nca patchNca = null;
Nca controlNca = null;
try
{
device.Configuration.VirtualFileSystem.ImportTickets(partitionFileSystem);
// TODO: To support multi-games container, this should use CNMT NCA instead.
foreach (DirectoryEntryEx fileEntry in partitionFileSystem.EnumerateEntries("/", "*.nca"))
{
Nca nca = partitionFileSystem.GetNca(device, fileEntry.FullPath);
if (nca.GetProgramIndex() != device.Configuration.UserChannelPersistence.Index)
{
continue;
}
if (nca.IsPatch())
{
patchNca = nca;
}
else if (nca.IsProgram())
{
mainNca = nca;
}
else if (nca.IsControl())
{
controlNca = nca;
}
}
ProcessLoaderHelper.RegisterProgramMapInfo(device, partitionFileSystem).ThrowIfFailure();
}
catch (Exception ex)
{
errorMessage = $"Unable to load: {ex.Message}";
return (false, ProcessResult.Failed);
}
if (mainNca != null)
{
if (mainNca.Header.ContentType != NcaContentType.Program)
{
errorMessage = "Selected NCA file is not a \"Program\" NCA";
return (false, ProcessResult.Failed);
}
// Load Update NCAs.
Nca updatePatchNca = null;
Nca updateControlNca = null;
if (ulong.TryParse(mainNca.Header.TitleId.ToString("x16"), NumberStyles.HexNumber, CultureInfo.InvariantCulture, out ulong titleIdBase))
{
// Clear the program index part.
titleIdBase &= ~0xFUL;
// Load update information if exists.
string titleUpdateMetadataPath = System.IO.Path.Combine(AppDataManager.GamesDirPath, titleIdBase.ToString("x16"), "updates.json");
if (File.Exists(titleUpdateMetadataPath))
{
string updatePath = JsonHelper.DeserializeFromFile<TitleUpdateMetadata>(titleUpdateMetadataPath).Selected;
if (File.Exists(updatePath))
{
PartitionFileSystem updatePartitionFileSystem = new(new FileStream(updatePath, FileMode.Open, FileAccess.Read).AsStorage());
device.Configuration.VirtualFileSystem.ImportTickets(updatePartitionFileSystem);
// TODO: This should use CNMT NCA instead.
foreach (DirectoryEntryEx fileEntry in updatePartitionFileSystem.EnumerateEntries("/", "*.nca"))
{
Nca nca = updatePartitionFileSystem.GetNca(device, fileEntry.FullPath);
if (nca.GetProgramIndex() != device.Configuration.UserChannelPersistence.Index)
{
continue;
}
if ($"{nca.Header.TitleId.ToString("x16")[..^3]}000" != titleIdBase.ToString("x16"))
{
break;
}
if (nca.IsProgram())
{
updatePatchNca = nca;
}
else if (nca.IsControl())
{
updateControlNca = nca;
}
}
}
}
}
if (updatePatchNca != null)
{
patchNca = updatePatchNca;
}
if (updateControlNca != null)
{
controlNca = updateControlNca;
}
// Load contained DownloadableContents.
// TODO: If we want to support multi-processes in future, we shouldn't clear AddOnContent data here.
device.Configuration.ContentManager.ClearAocData();
device.Configuration.ContentManager.AddAocData(partitionFileSystem, path, mainNca.Header.TitleId, device.Configuration.FsIntegrityCheckLevel);
// Load DownloadableContents.
string addOnContentMetadataPath = System.IO.Path.Combine(AppDataManager.GamesDirPath, mainNca.Header.TitleId.ToString("x16"), "dlc.json");
if (File.Exists(addOnContentMetadataPath))
{
List<DownloadableContentContainer> dlcContainerList = JsonHelper.DeserializeFromFile<List<DownloadableContentContainer>>(addOnContentMetadataPath);
foreach (DownloadableContentContainer downloadableContentContainer in dlcContainerList)
{
foreach (DownloadableContentNca downloadableContentNca in downloadableContentContainer.DownloadableContentNcaList)
{
if (File.Exists(downloadableContentContainer.ContainerPath) && downloadableContentNca.Enabled)
{
device.Configuration.ContentManager.AddAocItem(downloadableContentNca.TitleId, downloadableContentContainer.ContainerPath, downloadableContentNca.FullPath);
}
else
{
Logger.Warning?.Print(LogClass.Application, $"Cannot find AddOnContent file {downloadableContentContainer.ContainerPath}. It may have been moved or renamed.");
}
}
}
}
return (true, mainNca.Load(device, patchNca, controlNca));
}
errorMessage = "Unable to load: Could not find Main NCA";
return (false, ProcessResult.Failed);
}
public static Nca GetNca(this IFileSystem fileSystem, Switch device, string path)
{
using var ncaFile = new UniqueRef<IFile>();
fileSystem.OpenFile(ref ncaFile.Ref, path.ToU8Span(), OpenMode.Read).ThrowIfFailure();
return new Nca(device.Configuration.VirtualFileSystem.KeySet, ncaFile.Release().AsStorage());
}
}
}

View file

@ -0,0 +1,33 @@
namespace Ryujinx.HLE.Loaders.Processes
{
static class ProcessConst
{
// Binaries from exefs are loaded into mem in this order. Do not change.
public static readonly string[] ExeFsPrefixes =
{
"rtld",
"main",
"subsdk0",
"subsdk1",
"subsdk2",
"subsdk3",
"subsdk4",
"subsdk5",
"subsdk6",
"subsdk7",
"subsdk8",
"subsdk9",
"sdk"
};
public static readonly string MainNpdmPath = "/main.npdm";
public const int NroAsetMagic = ('A' << 0) | ('S' << 8) | ('E' << 16) | ('T' << 24);
public const bool AslrEnabled = true;
public const int NsoArgsHeaderSize = 8;
public const int NsoArgsDataSize = 0x9000;
public const int NsoArgsTotalSize = NsoArgsHeaderSize + NsoArgsDataSize;
}
}

View file

@ -0,0 +1,244 @@
using LibHac.Common;
using LibHac.Fs;
using LibHac.Fs.Fsa;
using LibHac.FsSystem;
using LibHac.Ns;
using LibHac.Tools.Fs;
using LibHac.Tools.FsSystem;
using LibHac.Tools.FsSystem.NcaUtils;
using Ryujinx.Common.Logging;
using Ryujinx.HLE.Loaders.Executables;
using Ryujinx.HLE.Loaders.Processes.Extensions;
using System.Collections.Concurrent;
using System.IO;
using System.Linq;
using Path = System.IO.Path;
namespace Ryujinx.HLE.Loaders.Processes
{
public class ProcessLoader
{
private readonly Switch _device;
private readonly ConcurrentDictionary<ulong, ProcessResult> _processesByPid;
private ulong _latestPid;
public ProcessResult ActiveApplication => _processesByPid[_latestPid];
public ProcessLoader(Switch device)
{
_device = device;
_processesByPid = new ConcurrentDictionary<ulong, ProcessResult>();
}
public bool LoadXci(string path)
{
FileStream stream = new(path, FileMode.Open, FileAccess.Read);
Xci xci = new(_device.Configuration.VirtualFileSystem.KeySet, stream.AsStorage());
if (!xci.HasPartition(XciPartitionType.Secure))
{
Logger.Error?.Print(LogClass.Loader, "Unable to load XCI: Could not find XCI Secure partition");
return false;
}
(bool success, ProcessResult processResult) = xci.OpenPartition(XciPartitionType.Secure).TryLoad(_device, path, out string errorMessage);
if (!success)
{
Logger.Error?.Print(LogClass.Loader, errorMessage, nameof(PartitionFileSystemExtensions.TryLoad));
return false;
}
if (processResult.ProcessId != 0 && _processesByPid.TryAdd(processResult.ProcessId, processResult))
{
if (processResult.Start(_device))
{
_latestPid = processResult.ProcessId;
return true;
}
}
return false;
}
public bool LoadNsp(string path)
{
FileStream file = new(path, FileMode.Open, FileAccess.Read);
PartitionFileSystem partitionFileSystem = new(file.AsStorage());
(bool success, ProcessResult processResult) = partitionFileSystem.TryLoad(_device, path, out string errorMessage);
if (processResult.ProcessId == 0)
{
// This is not a normal NSP, it's actually a ExeFS as a NSP
processResult = partitionFileSystem.Load(_device, new BlitStruct<ApplicationControlProperty>(1), partitionFileSystem.GetNpdm(), true);
}
if (processResult.ProcessId != 0 && _processesByPid.TryAdd(processResult.ProcessId, processResult))
{
if (processResult.Start(_device))
{
_latestPid = processResult.ProcessId;
return true;
}
}
if (!success)
{
Logger.Error?.Print(LogClass.Loader, errorMessage, nameof(PartitionFileSystemExtensions.TryLoad));
}
return false;
}
public bool LoadNca(string path)
{
FileStream file = new(path, FileMode.Open, FileAccess.Read);
Nca nca = new(_device.Configuration.VirtualFileSystem.KeySet, file.AsStorage(false));
ProcessResult processResult = nca.Load(_device, null, null);
if (processResult.ProcessId != 0 && _processesByPid.TryAdd(processResult.ProcessId, processResult))
{
if (processResult.Start(_device))
{
// NOTE: Check if process is SystemApplicationId or ApplicationId
if (processResult.ProgramId > 0x01000000000007FF)
{
_latestPid = processResult.ProcessId;
}
return true;
}
}
return false;
}
public bool LoadUnpackedNca(string exeFsDirPath, string romFsPath = null)
{
ProcessResult processResult = new LocalFileSystem(exeFsDirPath).Load(_device, romFsPath);
if (processResult.ProcessId != 0 && _processesByPid.TryAdd(processResult.ProcessId, processResult))
{
if (processResult.Start(_device))
{
_latestPid = processResult.ProcessId;
return true;
}
}
return false;
}
public bool LoadNxo(string path)
{
var nacpData = new BlitStruct<ApplicationControlProperty>(1);
IFileSystem dummyExeFs = null;
Stream romfsStream = null;
string programName = "";
ulong programId = 0000000000000000;
// Load executable.
IExecutable executable;
if (Path.GetExtension(path).ToLower() == ".nro")
{
FileStream input = new(path, FileMode.Open);
NroExecutable nro = new(input.AsStorage());
executable = nro;
// Open RomFS if exists.
IStorage romFsStorage = nro.OpenNroAssetSection(LibHac.Tools.Ro.NroAssetType.RomFs, false);
romFsStorage.GetSize(out long romFsSize).ThrowIfFailure();
if (romFsSize != 0)
{
romfsStream = romFsStorage.AsStream();
}
// Load Nacp if exists.
IStorage nacpStorage = nro.OpenNroAssetSection(LibHac.Tools.Ro.NroAssetType.Nacp, false);
nacpStorage.GetSize(out long nacpSize).ThrowIfFailure();
if (nacpSize != 0)
{
nacpStorage.Read(0, nacpData.ByteSpan);
programName = nacpData.Value.Title[(int)_device.System.State.DesiredTitleLanguage].NameString.ToString();
if (string.IsNullOrWhiteSpace(programName))
{
programName = nacpData.Value.Title.ItemsRo.ToArray().FirstOrDefault(x => x.Name[0] != 0).NameString.ToString();
}
if (nacpData.Value.PresenceGroupId != 0)
{
programId = nacpData.Value.PresenceGroupId;
}
else if (nacpData.Value.SaveDataOwnerId != 0)
{
programId = nacpData.Value.SaveDataOwnerId;
}
else if (nacpData.Value.AddOnContentBaseId != 0)
{
programId = nacpData.Value.AddOnContentBaseId - 0x1000;
}
}
// TODO: Add icon maybe ?
}
else
{
programName = System.IO.Path.GetFileNameWithoutExtension(path);
executable = new NsoExecutable(new LocalStorage(path, FileAccess.Read), programName);
}
// Explicitly null TitleId to disable the shader cache.
Graphics.Gpu.GraphicsConfig.TitleId = null;
_device.Gpu.HostInitalized.Set();
ProcessResult processResult = ProcessLoaderHelper.LoadNsos(_device,
_device.System.KernelContext,
dummyExeFs.GetNpdm(),
nacpData.Value,
diskCacheEnabled: false,
allowCodeMemoryForJit: true,
programName,
programId,
null,
executable);
// Make sure the process id is valid.
if (processResult.ProcessId != 0)
{
// Load RomFS.
if (romfsStream != null)
{
_device.Configuration.VirtualFileSystem.SetRomFs(processResult.ProcessId, romfsStream);
}
// Start process.
if (_processesByPid.TryAdd(processResult.ProcessId, processResult))
{
if (processResult.Start(_device))
{
_latestPid = processResult.ProcessId;
return true;
}
}
}
return false;
}
}
}

View file

@ -0,0 +1,458 @@
using LibHac.Account;
using LibHac.Common;
using LibHac.Fs;
using LibHac.Fs.Shim;
using LibHac.FsSystem;
using LibHac.Loader;
using LibHac.Ncm;
using LibHac.Ns;
using LibHac.Tools.Fs;
using LibHac.Tools.FsSystem;
using LibHac.Tools.FsSystem.NcaUtils;
using Ryujinx.Common;
using Ryujinx.Common.Logging;
using Ryujinx.HLE.HOS;
using Ryujinx.HLE.HOS.Kernel;
using Ryujinx.HLE.HOS.Kernel.Common;
using Ryujinx.HLE.HOS.Kernel.Memory;
using Ryujinx.HLE.HOS.Kernel.Process;
using Ryujinx.HLE.Loaders.Executables;
using Ryujinx.HLE.Loaders.Processes.Extensions;
using Ryujinx.Horizon.Common;
using System;
using System.Linq;
using System.Runtime.InteropServices;
using ApplicationId = LibHac.Ncm.ApplicationId;
namespace Ryujinx.HLE.Loaders.Processes
{
static class ProcessLoaderHelper
{
public static LibHac.Result RegisterProgramMapInfo(Switch device, PartitionFileSystem partitionFileSystem)
{
ulong applicationId = 0;
int programCount = 0;
Span<bool> hasIndex = stackalloc bool[0x10];
foreach (DirectoryEntryEx fileEntry in partitionFileSystem.EnumerateEntries("/", "*.nca"))
{
Nca nca = partitionFileSystem.GetNca(device, fileEntry.FullPath);
if (!nca.IsProgram() && nca.IsPatch())
{
continue;
}
ulong currentProgramId = nca.Header.TitleId;
ulong currentMainProgramId = currentProgramId & ~0xFFFul;
if (applicationId == 0 && currentMainProgramId != 0)
{
applicationId = currentMainProgramId;
}
if (applicationId != currentMainProgramId)
{
// Currently there aren't any known multi-application game cards containing multi-program applications,
// so because multi-application game cards are the only way we could run into multiple applications
// we'll just return that there's a single program.
programCount = 1;
break;
}
hasIndex[(int)(currentProgramId & 0xF)] = true;
}
if (programCount == 0)
{
for (int i = 0; i < hasIndex.Length && hasIndex[i]; i++)
{
programCount++;
}
}
if (programCount <= 0)
{
return LibHac.Result.Success;
}
Span<ProgramIndexMapInfo> mapInfo = stackalloc ProgramIndexMapInfo[0x10];
for (int i = 0; i < programCount; i++)
{
mapInfo[i].ProgramId = new ProgramId(applicationId + (uint)i);
mapInfo[i].MainProgramId = new ApplicationId(applicationId);
mapInfo[i].ProgramIndex = (byte)i;
}
return device.System.LibHacHorizonManager.NsClient.Fs.RegisterProgramIndexMapInfo(mapInfo[..programCount]);
}
public static LibHac.Result EnsureSaveData(Switch device, ApplicationId applicationId, BlitStruct<ApplicationControlProperty> applicationControlProperty)
{
Logger.Info?.Print(LogClass.Application, "Ensuring required savedata exists.");
ref ApplicationControlProperty control = ref applicationControlProperty.Value;
if (LibHac.Common.Utilities.IsZeros(applicationControlProperty.ByteSpan))
{
// If the current application doesn't have a loaded control property, create a dummy one and set the savedata sizes so a user savedata will be created.
control = ref new BlitStruct<ApplicationControlProperty>(1).Value;
// The set sizes don't actually matter as long as they're non-zero because we use directory savedata.
control.UserAccountSaveDataSize = 0x4000;
control.UserAccountSaveDataJournalSize = 0x4000;
control.SaveDataOwnerId = applicationId.Value;
Logger.Warning?.Print(LogClass.Application, "No control file was found for this game. Using a dummy one instead. This may cause inaccuracies in some games.");
}
LibHac.Result resultCode = device.System.LibHacHorizonManager.RyujinxClient.Fs.EnsureApplicationCacheStorage(out _, out _, applicationId, in control);
if (resultCode.IsFailure())
{
Logger.Error?.Print(LogClass.Application, $"Error calling EnsureApplicationCacheStorage. Result code {resultCode.ToStringWithName()}");
return resultCode;
}
Uid userId = device.System.AccountManager.LastOpenedUser.UserId.ToLibHacUid();
resultCode = device.System.LibHacHorizonManager.RyujinxClient.Fs.EnsureApplicationSaveData(out _, applicationId, in control, in userId);
if (resultCode.IsFailure())
{
Logger.Error?.Print(LogClass.Application, $"Error calling EnsureApplicationSaveData. Result code {resultCode.ToStringWithName()}");
}
return resultCode;
}
public static bool LoadKip(KernelContext context, KipExecutable kip)
{
uint endOffset = kip.DataOffset + (uint)kip.Data.Length;
if (kip.BssSize != 0)
{
endOffset = kip.BssOffset + kip.BssSize;
}
uint codeSize = BitUtils.AlignUp<uint>(kip.TextOffset + endOffset, KPageTableBase.PageSize);
int codePagesCount = (int)(codeSize / KPageTableBase.PageSize);
ulong codeBaseAddress = kip.Is64BitAddressSpace ? 0x8000000UL : 0x200000UL;
ulong codeAddress = codeBaseAddress + kip.TextOffset;
ProcessCreationFlags flags = 0;
if (ProcessConst.AslrEnabled)
{
// TODO: Randomization.
flags |= ProcessCreationFlags.EnableAslr;
}
if (kip.Is64BitAddressSpace)
{
flags |= ProcessCreationFlags.AddressSpace64Bit;
}
if (kip.Is64Bit)
{
flags |= ProcessCreationFlags.Is64Bit;
}
ProcessCreationInfo creationInfo = new(kip.Name, kip.Version, kip.ProgramId, codeAddress, codePagesCount, flags, 0, 0);
MemoryRegion memoryRegion = kip.UsesSecureMemory ? MemoryRegion.Service : MemoryRegion.Application;
KMemoryRegionManager region = context.MemoryManager.MemoryRegions[(int)memoryRegion];
Result result = region.AllocatePages(out KPageList pageList, (ulong)codePagesCount);
if (result != Result.Success)
{
Logger.Error?.Print(LogClass.Loader, $"Process initialization returned error \"{result}\".");
return false;
}
KProcess process = new(context);
var processContextFactory = new ArmProcessContextFactory(
context.Device.System.TickSource,
context.Device.Gpu,
string.Empty,
string.Empty,
false,
codeAddress,
codeSize);
result = process.InitializeKip(creationInfo, kip.Capabilities, pageList, context.ResourceLimit, memoryRegion, processContextFactory);
if (result != Result.Success)
{
Logger.Error?.Print(LogClass.Loader, $"Process initialization returned error \"{result}\".");
return false;
}
result = LoadIntoMemory(process, kip, codeBaseAddress);
if (result != Result.Success)
{
Logger.Error?.Print(LogClass.Loader, $"Process initialization returned error \"{result}\".");
return false;
}
process.DefaultCpuCore = kip.IdealCoreId;
result = process.Start(kip.Priority, (ulong)kip.StackSize);
if (result != Result.Success)
{
Logger.Error?.Print(LogClass.Loader, $"Process start returned error \"{result}\".");
return false;
}
context.Processes.TryAdd(process.Pid, process);
return true;
}
public static ProcessResult LoadNsos(
Switch device,
KernelContext context,
MetaLoader metaLoader,
ApplicationControlProperty applicationControlProperties,
bool diskCacheEnabled,
bool allowCodeMemoryForJit,
string name,
ulong programId,
byte[] arguments = null,
params IExecutable[] executables)
{
context.Device.System.ServiceTable.WaitServicesReady();
LibHac.Result resultCode = metaLoader.GetNpdm(out var npdm);
if (resultCode.IsFailure())
{
Logger.Error?.Print(LogClass.Loader, $"Process initialization failed getting npdm. Result Code {resultCode.ToStringWithName()}");
return ProcessResult.Failed;
}
ref readonly var meta = ref npdm.Meta;
ulong argsStart = 0;
uint argsSize = 0;
ulong codeStart = (meta.Flags & 1) != 0 ? 0x8000000UL : 0x200000UL;
uint codeSize = 0;
var buildIds = executables.Select(e => (e switch
{
NsoExecutable nso => Convert.ToHexString(nso.BuildId.ItemsRo.ToArray()),
NroExecutable nro => Convert.ToHexString(nro.Header.BuildId),
_ => ""
}).ToUpper());
ulong[] nsoBase = new ulong[executables.Length];
for (int index = 0; index < executables.Length; index++)
{
IExecutable nso = executables[index];
uint textEnd = nso.TextOffset + (uint)nso.Text.Length;
uint roEnd = nso.RoOffset + (uint)nso.Ro.Length;
uint dataEnd = nso.DataOffset + (uint)nso.Data.Length + nso.BssSize;
uint nsoSize = textEnd;
if (nsoSize < roEnd)
{
nsoSize = roEnd;
}
if (nsoSize < dataEnd)
{
nsoSize = dataEnd;
}
nsoSize = BitUtils.AlignUp<uint>(nsoSize, KPageTableBase.PageSize);
nsoBase[index] = codeStart + codeSize;
codeSize += nsoSize;
if (arguments != null && argsSize == 0)
{
argsStart = codeSize;
argsSize = (uint)BitUtils.AlignDown(arguments.Length * 2 + ProcessConst.NsoArgsTotalSize - 1, KPageTableBase.PageSize);
codeSize += argsSize;
}
}
int codePagesCount = (int)(codeSize / KPageTableBase.PageSize);
int personalMmHeapPagesCount = (int)(meta.SystemResourceSize / KPageTableBase.PageSize);
ProcessCreationInfo creationInfo = new(
name,
(int)meta.Version,
programId,
codeStart,
codePagesCount,
(ProcessCreationFlags)meta.Flags | ProcessCreationFlags.IsApplication,
0,
personalMmHeapPagesCount);
context.Device.System.LibHacHorizonManager.InitializeApplicationClient(new ProgramId(programId), in npdm);
Result result;
KResourceLimit resourceLimit = new(context);
long applicationRgSize = (long)context.MemoryManager.MemoryRegions[(int)MemoryRegion.Application].Size;
result = resourceLimit.SetLimitValue(LimitableResource.Memory, applicationRgSize);
if (result.IsSuccess)
{
result = resourceLimit.SetLimitValue(LimitableResource.Thread, 608);
}
if (result.IsSuccess)
{
result = resourceLimit.SetLimitValue(LimitableResource.Event, 700);
}
if (result.IsSuccess)
{
result = resourceLimit.SetLimitValue(LimitableResource.TransferMemory, 128);
}
if (result.IsSuccess)
{
result = resourceLimit.SetLimitValue(LimitableResource.Session, 894);
}
if (result != Result.Success)
{
Logger.Error?.Print(LogClass.Loader, $"Process initialization failed setting resource limit values.");
return ProcessResult.Failed;
}
KProcess process = new(context, allowCodeMemoryForJit);
// NOTE: This field doesn't exists one firmware pre-5.0.0, a workaround have to be found.
MemoryRegion memoryRegion = (MemoryRegion)(npdm.Acid.Flags >> 2 & 0xf);
if (memoryRegion > MemoryRegion.NvServices)
{
Logger.Error?.Print(LogClass.Loader, $"Process initialization failed due to invalid ACID flags.");
return ProcessResult.Failed;
}
var processContextFactory = new ArmProcessContextFactory(
context.Device.System.TickSource,
context.Device.Gpu,
$"{programId:x16}",
applicationControlProperties.DisplayVersionString.ToString(),
diskCacheEnabled,
codeStart,
codeSize);
result = process.Initialize(
creationInfo,
MemoryMarshal.Cast<byte, uint>(npdm.KernelCapabilityData),
resourceLimit,
memoryRegion,
processContextFactory);
if (result != Result.Success)
{
Logger.Error?.Print(LogClass.Loader, $"Process initialization returned error \"{result}\".");
return ProcessResult.Failed;
}
for (int index = 0; index < executables.Length; index++)
{
Logger.Info?.Print(LogClass.Loader, $"Loading image {index} at 0x{nsoBase[index]:x16}...");
result = LoadIntoMemory(process, executables[index], nsoBase[index]);
if (result != Result.Success)
{
Logger.Error?.Print(LogClass.Loader, $"Process initialization returned error \"{result}\".");
return ProcessResult.Failed;
}
}
process.DefaultCpuCore = meta.DefaultCpuId;
context.Processes.TryAdd(process.Pid, process);
// Keep the build ids because the tamper machine uses them to know which process to associate a
// tamper to and also keep the starting address of each executable inside a process because some
// memory modifications are relative to this address.
ProcessTamperInfo tamperInfo = new(
process,
buildIds,
nsoBase,
process.MemoryManager.HeapRegionStart,
process.MemoryManager.AliasRegionStart,
process.MemoryManager.CodeRegionStart);
// Once everything is loaded, we can load cheats.
device.Configuration.VirtualFileSystem.ModLoader.LoadCheats(programId, tamperInfo, device.TamperMachine);
return new ProcessResult(metaLoader, applicationControlProperties, diskCacheEnabled, allowCodeMemoryForJit, processContextFactory.DiskCacheLoadState, process.Pid, meta.MainThreadPriority, meta.MainThreadStackSize);
}
public static Result LoadIntoMemory(KProcess process, IExecutable image, ulong baseAddress)
{
ulong textStart = baseAddress + image.TextOffset;
ulong roStart = baseAddress + image.RoOffset;
ulong dataStart = baseAddress + image.DataOffset;
ulong bssStart = baseAddress + image.BssOffset;
ulong end = dataStart + (ulong)image.Data.Length;
if (image.BssSize != 0)
{
end = bssStart + image.BssSize;
}
process.CpuMemory.Write(textStart, image.Text);
process.CpuMemory.Write(roStart, image.Ro);
process.CpuMemory.Write(dataStart, image.Data);
process.CpuMemory.Fill(bssStart, image.BssSize, 0);
Result SetProcessMemoryPermission(ulong address, ulong size, KMemoryPermission permission)
{
if (size == 0)
{
return Result.Success;
}
size = BitUtils.AlignUp<ulong>(size, KPageTableBase.PageSize);
return process.MemoryManager.SetProcessMemoryPermission(address, size, permission);
}
Result result = SetProcessMemoryPermission(textStart, (ulong)image.Text.Length, KMemoryPermission.ReadAndExecute);
if (result != Result.Success)
{
return result;
}
result = SetProcessMemoryPermission(roStart, (ulong)image.Ro.Length, KMemoryPermission.Read);
if (result != Result.Success)
{
return result;
}
return SetProcessMemoryPermission(dataStart, end - dataStart, KMemoryPermission.ReadAndWrite);
}
}
}

View file

@ -0,0 +1,92 @@
using LibHac.Loader;
using LibHac.Ns;
using Ryujinx.Common.Logging;
using Ryujinx.Cpu;
using Ryujinx.HLE.Loaders.Processes.Extensions;
using Ryujinx.Horizon.Common;
namespace Ryujinx.HLE.Loaders.Processes
{
public struct ProcessResult
{
public static ProcessResult Failed => new(null, new ApplicationControlProperty(), false, false, null, 0, 0, 0);
private readonly byte _mainThreadPriority;
private readonly uint _mainThreadStackSize;
public readonly IDiskCacheLoadState DiskCacheLoadState;
public readonly MetaLoader MetaLoader;
public readonly ApplicationControlProperty ApplicationControlProperties;
public readonly ulong ProcessId;
public string Name;
public ulong ProgramId;
public readonly string ProgramIdText;
public readonly bool Is64Bit;
public readonly bool DiskCacheEnabled;
public readonly bool AllowCodeMemoryForJit;
public ProcessResult(
MetaLoader metaLoader,
ApplicationControlProperty applicationControlProperties,
bool diskCacheEnabled,
bool allowCodeMemoryForJit,
IDiskCacheLoadState diskCacheLoadState,
ulong pid,
byte mainThreadPriority,
uint mainThreadStackSize)
{
_mainThreadPriority = mainThreadPriority;
_mainThreadStackSize = mainThreadStackSize;
DiskCacheLoadState = diskCacheLoadState;
ProcessId = pid;
MetaLoader = metaLoader;
ApplicationControlProperties = applicationControlProperties;
if (metaLoader is not null)
{
ulong programId = metaLoader.GetProgramId();
Name = metaLoader.GetProgramName();
ProgramId = programId;
ProgramIdText = $"{programId:x16}";
Is64Bit = metaLoader.IsProgram64Bit();
}
DiskCacheEnabled = diskCacheEnabled;
AllowCodeMemoryForJit = allowCodeMemoryForJit;
}
public bool Start(Switch device)
{
device.Configuration.ContentManager.LoadEntries(device);
Result result = device.System.KernelContext.Processes[ProcessId].Start(_mainThreadPriority, _mainThreadStackSize);
if (result != Result.Success)
{
Logger.Error?.Print(LogClass.Loader, $"Process start returned error \"{result}\".");
return false;
}
// TODO: LibHac npdm currently doesn't support version field.
string version;
if (ProgramId > 0x0100000000007FFF)
{
version = ApplicationControlProperties.DisplayVersionString.ToString();
}
else
{
version = device.System.ContentManager.GetCurrentFirmwareVersion().VersionString;
}
Logger.Info?.Print(LogClass.Loader, $"Application Loaded: {Name} v{version} [{ProgramIdText}] [{(Is64Bit ? "64-bit" : "32-bit")}]");
return true;
}
}
}