mirror of
https://github.com/Atmosphere-NX/Atmosphere.git
synced 2025-06-02 23:59:49 -04:00
loader: refactor to use fs bindings
This commit is contained in:
parent
4c5e980e07
commit
237b513408
30 changed files with 821 additions and 650 deletions
|
@ -13,255 +13,65 @@
|
|||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
#include <dirent.h>
|
||||
#include "ldr_content_management.hpp"
|
||||
#include "ldr_ecs.hpp"
|
||||
|
||||
namespace ams::ldr {
|
||||
|
||||
namespace {
|
||||
|
||||
/* DeviceNames. */
|
||||
constexpr const char *CodeFileSystemDeviceName = "code";
|
||||
constexpr const char *HblFileSystemDeviceName = "hbl";
|
||||
constexpr const char *SdCardFileSystemDeviceName = "sdmc";
|
||||
|
||||
constexpr const char *SdCardStorageMountPoint = "@Sdcard";
|
||||
|
||||
/* Globals. */
|
||||
bool g_has_mounted_sd_card = false;
|
||||
|
||||
/* Helpers. */
|
||||
inline void FixFileSystemPath(char *path) {
|
||||
/* Paths will fail when passed to FS if they use the wrong kinds of slashes. */
|
||||
for (size_t i = 0; i < FS_MAX_PATH && path[i]; i++) {
|
||||
if (path[i] == '\\') {
|
||||
path[i] = '/';
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
inline const char *GetRelativePathStart(const char *relative_path) {
|
||||
/* We assume filenames don't start with slashes when formatting. */
|
||||
while (*relative_path == '/' || *relative_path == '\\') {
|
||||
relative_path++;
|
||||
}
|
||||
return relative_path;
|
||||
}
|
||||
|
||||
Result MountSdCardFileSystem() {
|
||||
return fsdevMountSdmc();
|
||||
}
|
||||
|
||||
Result MountNspFileSystem(const char *device_name, const char *path) {
|
||||
FsFileSystem fs;
|
||||
R_TRY(fsOpenFileSystemWithId(&fs, 0, FsFileSystemType_ApplicationPackage, path));
|
||||
AMS_ABORT_UNLESS(fsdevMountDevice(device_name, fs) >= 0);
|
||||
return ResultSuccess();
|
||||
}
|
||||
|
||||
FILE *OpenFile(const char *device_name, const char *relative_path) {
|
||||
/* Allow nullptr device_name/relative path -- those are simply not openable. */
|
||||
if (device_name == nullptr || relative_path == nullptr) {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
char path[FS_MAX_PATH];
|
||||
std::snprintf(path, FS_MAX_PATH, "%s:/%s", device_name, GetRelativePathStart(relative_path));
|
||||
FixFileSystemPath(path);
|
||||
return fopen(path, "rb");
|
||||
}
|
||||
|
||||
FILE *OpenLooseSdFile(ncm::ProgramId program_id, const char *relative_path) {
|
||||
/* Allow nullptr relative path -- those are simply not openable. */
|
||||
if (relative_path == nullptr) {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
char path[FS_MAX_PATH];
|
||||
std::snprintf(path, FS_MAX_PATH, "/atmosphere/contents/%016lx/exefs/%s", static_cast<u64>(program_id), GetRelativePathStart(relative_path));
|
||||
FixFileSystemPath(path);
|
||||
return OpenFile(SdCardFileSystemDeviceName, path);
|
||||
}
|
||||
|
||||
bool IsFileStubbed(ncm::ProgramId program_id, const cfg::OverrideStatus &status, const char *relative_path) {
|
||||
/* Allow nullptr relative path -- those are simply not openable. */
|
||||
if (relative_path == nullptr) {
|
||||
return true;
|
||||
}
|
||||
|
||||
/* Only allow stubbing in the case where we're considering SD card content. */
|
||||
if (!status.IsProgramSpecific()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
char path[FS_MAX_PATH];
|
||||
std::snprintf(path, FS_MAX_PATH, "/atmosphere/contents/%016lx/exefs/%s.stub", static_cast<u64>(program_id), GetRelativePathStart(relative_path));
|
||||
FixFileSystemPath(path);
|
||||
FILE *f = OpenFile(SdCardFileSystemDeviceName, path);
|
||||
if (f == nullptr) {
|
||||
return false;
|
||||
}
|
||||
fclose(f);
|
||||
return true;
|
||||
}
|
||||
|
||||
FILE *OpenBaseExefsFile(ncm::ProgramId program_id, const cfg::OverrideStatus &status, const char *relative_path) {
|
||||
/* Allow nullptr relative path -- those are simply not openable. */
|
||||
if (relative_path == nullptr) {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
/* Check if stubbed. */
|
||||
if (IsFileStubbed(program_id, status, relative_path)) {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
return OpenFile(CodeFileSystemDeviceName, relative_path);
|
||||
}
|
||||
os::Mutex g_scoped_code_mount_lock;
|
||||
|
||||
}
|
||||
|
||||
/* ScopedCodeMount functionality. */
|
||||
ScopedCodeMount::ScopedCodeMount(const ncm::ProgramLocation &loc) : has_status(false), is_code_mounted(false), is_hbl_mounted(false) {
|
||||
ScopedCodeMount::ScopedCodeMount(const ncm::ProgramLocation &loc) : lk(g_scoped_code_mount_lock), has_status(false), mounted_ams(false), mounted_code(false) {
|
||||
this->result = this->Initialize(loc);
|
||||
}
|
||||
|
||||
ScopedCodeMount::ScopedCodeMount(const ncm::ProgramLocation &loc, const cfg::OverrideStatus &o) : override_status(o), has_status(true), is_code_mounted(false), is_hbl_mounted(false) {
|
||||
ScopedCodeMount::ScopedCodeMount(const ncm::ProgramLocation &loc, const cfg::OverrideStatus &o) : lk(g_scoped_code_mount_lock), override_status(o), has_status(true), mounted_ams(false), mounted_code(false) {
|
||||
this->result = this->Initialize(loc);
|
||||
}
|
||||
|
||||
ScopedCodeMount::~ScopedCodeMount() {
|
||||
/* Unmount devices. */
|
||||
if (this->is_code_mounted) {
|
||||
fsdevUnmountDevice(CodeFileSystemDeviceName);
|
||||
/* Unmount filesystems. */
|
||||
if (this->mounted_ams) {
|
||||
fs::Unmount(AtmosphereCodeMountName);
|
||||
}
|
||||
if (this->is_hbl_mounted) {
|
||||
fsdevUnmountDevice(HblFileSystemDeviceName);
|
||||
if (this->mounted_code) {
|
||||
fs::Unmount(CodeMountName);
|
||||
}
|
||||
}
|
||||
|
||||
Result ScopedCodeMount::MountCodeFileSystem(const ncm::ProgramLocation &loc) {
|
||||
char path[FS_MAX_PATH];
|
||||
|
||||
/* Try to get the content path. */
|
||||
R_TRY(ResolveContentPath(path, loc));
|
||||
|
||||
/* Try to mount the content path. */
|
||||
FsFileSystem fs;
|
||||
R_TRY(fsldrOpenCodeFileSystem(static_cast<u64>(loc.program_id), path, &fs));
|
||||
AMS_ABORT_UNLESS(fsdevMountDevice(CodeFileSystemDeviceName, fs) != -1);
|
||||
|
||||
/* Note that we mounted code. */
|
||||
this->is_code_mounted = true;
|
||||
return ResultSuccess();
|
||||
}
|
||||
|
||||
Result ScopedCodeMount::MountSdCardCodeFileSystem(const ncm::ProgramLocation &loc) {
|
||||
char path[FS_MAX_PATH];
|
||||
|
||||
/* Print and fix path. */
|
||||
std::snprintf(path, FS_MAX_PATH, "%s:/atmosphere/contents/%016lx/exefs.nsp", SdCardStorageMountPoint, static_cast<u64>(loc.program_id));
|
||||
FixFileSystemPath(path);
|
||||
R_TRY(MountNspFileSystem(CodeFileSystemDeviceName, path));
|
||||
|
||||
/* Note that we mounted code. */
|
||||
this->is_code_mounted = true;
|
||||
return ResultSuccess();
|
||||
}
|
||||
|
||||
Result ScopedCodeMount::MountHblFileSystem() {
|
||||
char path[FS_MAX_PATH];
|
||||
|
||||
/* Print and fix path. */
|
||||
std::snprintf(path, FS_MAX_PATH, "%s:/%s", SdCardStorageMountPoint, GetRelativePathStart(cfg::GetHblPath()));
|
||||
FixFileSystemPath(path);
|
||||
R_TRY(MountNspFileSystem(HblFileSystemDeviceName, path));
|
||||
|
||||
/* Note that we mounted HBL. */
|
||||
this->is_hbl_mounted = true;
|
||||
return ResultSuccess();
|
||||
}
|
||||
|
||||
|
||||
Result ScopedCodeMount::Initialize(const ncm::ProgramLocation &loc) {
|
||||
bool is_sd_initialized = cfg::IsSdCardInitialized();
|
||||
|
||||
/* Check if we're ready to mount the SD card. */
|
||||
if (!g_has_mounted_sd_card) {
|
||||
if (is_sd_initialized) {
|
||||
R_ABORT_UNLESS(MountSdCardFileSystem());
|
||||
g_has_mounted_sd_card = true;
|
||||
}
|
||||
}
|
||||
|
||||
/* Capture override status, if necessary. */
|
||||
if (!this->has_status) {
|
||||
this->InitializeOverrideStatus(loc);
|
||||
this->EnsureOverrideStatus(loc);
|
||||
AMS_ABORT_UNLESS(this->has_status);
|
||||
|
||||
/* Get the content path. */
|
||||
char content_path[fs::EntryNameLengthMax + 1] = "/";
|
||||
if (static_cast<ncm::StorageId>(loc.storage_id) != ncm::StorageId::None) {
|
||||
R_TRY(ResolveContentPath(content_path, loc));
|
||||
}
|
||||
|
||||
/* Check if we should override contents. */
|
||||
if (this->override_status.IsHbl()) {
|
||||
/* Try to mount HBL. */
|
||||
this->MountHblFileSystem();
|
||||
}
|
||||
if (this->override_status.IsProgramSpecific()) {
|
||||
/* Try to mount Code NSP on SD. */
|
||||
this->MountSdCardCodeFileSystem(loc);
|
||||
}
|
||||
/* Mount the atmosphere code file system. */
|
||||
R_TRY(fs::MountCodeForAtmosphereWithRedirection(AtmosphereCodeMountName, content_path, loc.program_id, this->override_status.IsHbl(), this->override_status.IsProgramSpecific()));
|
||||
this->mounted_ams = true;
|
||||
|
||||
/* If we haven't already mounted code, mount it. */
|
||||
if (!this->IsCodeMounted()) {
|
||||
R_TRY(this->MountCodeFileSystem(loc));
|
||||
}
|
||||
/* Mount the base code file system. */
|
||||
R_TRY(fs::MountCodeForAtmosphere(CodeMountName, content_path, loc.program_id));
|
||||
this->mounted_code = true;
|
||||
|
||||
return ResultSuccess();
|
||||
}
|
||||
|
||||
void ScopedCodeMount::InitializeOverrideStatus(const ncm::ProgramLocation &loc) {
|
||||
AMS_ABORT_UNLESS(!this->has_status);
|
||||
void ScopedCodeMount::EnsureOverrideStatus(const ncm::ProgramLocation &loc) {
|
||||
if (this->has_status) {
|
||||
return;
|
||||
}
|
||||
this->override_status = cfg::CaptureOverrideStatus(loc.program_id);
|
||||
this->has_status = true;
|
||||
}
|
||||
|
||||
Result OpenCodeFile(FILE *&out, ncm::ProgramId program_id, const cfg::OverrideStatus &status, const char *relative_path) {
|
||||
FILE *f = nullptr;
|
||||
const char *ecs_device_name = ecs::Get(program_id);
|
||||
|
||||
if (ecs_device_name != nullptr) {
|
||||
/* First priority: Open from external content. */
|
||||
f = OpenFile(ecs_device_name, relative_path);
|
||||
} else if (status.IsHbl()) {
|
||||
/* Next, try to open from HBL. */
|
||||
f = OpenFile(HblFileSystemDeviceName, relative_path);
|
||||
} else {
|
||||
/* If not ECS or HBL, try a loose file on the SD. */
|
||||
if (status.IsProgramSpecific()) {
|
||||
f = OpenLooseSdFile(program_id, relative_path);
|
||||
}
|
||||
|
||||
/* If we fail, try the original exefs. */
|
||||
if (f == nullptr) {
|
||||
f = OpenBaseExefsFile(program_id, status, relative_path);
|
||||
}
|
||||
}
|
||||
|
||||
/* If nothing worked, we failed to find the path. */
|
||||
R_UNLESS(f != nullptr, fs::ResultPathNotFound());
|
||||
|
||||
out = f;
|
||||
return ResultSuccess();
|
||||
}
|
||||
|
||||
Result OpenCodeFileFromBaseExefs(FILE *&out, ncm::ProgramId program_id, const cfg::OverrideStatus &status, const char *relative_path) {
|
||||
/* Open the file. */
|
||||
FILE *f = OpenBaseExefsFile(program_id, status, relative_path);
|
||||
R_UNLESS(f != nullptr, fs::ResultPathNotFound());
|
||||
|
||||
out = f;
|
||||
return ResultSuccess();
|
||||
}
|
||||
|
||||
/* Redirection API. */
|
||||
Result ResolveContentPath(char *out_path, const ncm::ProgramLocation &loc) {
|
||||
lr::Path path;
|
||||
|
@ -281,7 +91,9 @@ namespace ams::ldr {
|
|||
|
||||
std::strncpy(out_path, path.str, fs::EntryNameLengthMax);
|
||||
out_path[fs::EntryNameLengthMax - 1] = '\0';
|
||||
FixFileSystemPath(out_path);
|
||||
|
||||
fs::Replace(out_path, fs::EntryNameLengthMax + 1, fs::StringTraits::AlternateDirectorySeparator, fs::StringTraits::DirectorySeparator);
|
||||
|
||||
return ResultSuccess();
|
||||
}
|
||||
|
||||
|
|
|
@ -21,12 +21,14 @@ namespace ams::ldr {
|
|||
/* Utility reference to make code mounting automatic. */
|
||||
class ScopedCodeMount {
|
||||
NON_COPYABLE(ScopedCodeMount);
|
||||
NON_MOVEABLE(ScopedCodeMount);
|
||||
private:
|
||||
Result result;
|
||||
std::scoped_lock<os::Mutex> lk;
|
||||
cfg::OverrideStatus override_status;
|
||||
Result result;
|
||||
bool has_status;
|
||||
bool is_code_mounted;
|
||||
bool is_hbl_mounted;
|
||||
bool mounted_ams;
|
||||
bool mounted_code;
|
||||
public:
|
||||
ScopedCodeMount(const ncm::ProgramLocation &loc);
|
||||
ScopedCodeMount(const ncm::ProgramLocation &loc, const cfg::OverrideStatus &override_status);
|
||||
|
@ -36,32 +38,20 @@ namespace ams::ldr {
|
|||
return this->result;
|
||||
}
|
||||
|
||||
bool IsCodeMounted() const {
|
||||
return this->is_code_mounted;
|
||||
}
|
||||
|
||||
bool IsHblMounted() const {
|
||||
return this->is_hbl_mounted;
|
||||
}
|
||||
|
||||
const cfg::OverrideStatus &GetOverrideStatus() const {
|
||||
AMS_ABORT_UNLESS(this->has_status);
|
||||
return this->override_status;
|
||||
}
|
||||
|
||||
private:
|
||||
Result Initialize(const ncm::ProgramLocation &loc);
|
||||
|
||||
void InitializeOverrideStatus(const ncm::ProgramLocation &loc);
|
||||
|
||||
Result MountCodeFileSystem(const ncm::ProgramLocation &loc);
|
||||
Result MountSdCardCodeFileSystem(const ncm::ProgramLocation &loc);
|
||||
Result MountHblFileSystem();
|
||||
void EnsureOverrideStatus(const ncm::ProgramLocation &loc);
|
||||
};
|
||||
|
||||
/* Content Management API. */
|
||||
Result OpenCodeFile(FILE *&out, ncm::ProgramId program_id, const cfg::OverrideStatus &status, const char *relative_path);
|
||||
Result OpenCodeFileFromBaseExefs(FILE *&out, ncm::ProgramId program_id, const cfg::OverrideStatus &status, const char *relative_path);
|
||||
constexpr inline const char * const AtmosphereCodeMountName = "ams-code";
|
||||
constexpr inline const char * const CodeMountName = "code";
|
||||
|
||||
#define ENCODE_ATMOSPHERE_CODE_PATH(relative) "ams-code:" relative
|
||||
#define ENCODE_CODE_PATH(relative) "ams:" relative
|
||||
|
||||
/* Redirection API. */
|
||||
Result ResolveContentPath(char *out_path, const ncm::ProgramLocation &loc);
|
||||
|
|
|
@ -1,122 +0,0 @@
|
|||
/*
|
||||
* Copyright (c) 2018-2020 Atmosphère-NX
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify it
|
||||
* under the terms and conditions of the GNU General Public License,
|
||||
* version 2, as published by the Free Software Foundation.
|
||||
*
|
||||
* This program is distributed in the hope it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
||||
* more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
#include "ldr_ecs.hpp"
|
||||
|
||||
namespace ams::ldr::ecs {
|
||||
|
||||
namespace {
|
||||
|
||||
/* Convenience definition. */
|
||||
constexpr size_t DeviceNameSizeMax = 0x20;
|
||||
constexpr size_t MaxExternalContentSourceCount = 0x10;
|
||||
|
||||
/* Types. */
|
||||
class ExternalContentSource {
|
||||
NON_COPYABLE(ExternalContentSource);
|
||||
NON_MOVEABLE(ExternalContentSource);
|
||||
private:
|
||||
bool has_mounted = false;
|
||||
char device_name[DeviceNameSizeMax];
|
||||
os::ManagedHandle client;
|
||||
|
||||
Result Mount() {
|
||||
/* Create service. */
|
||||
Service srv;
|
||||
serviceCreate(&srv, client.Move());
|
||||
FsFileSystem fs = { srv };
|
||||
auto fs_guard = SCOPE_GUARD { fsFsClose(&fs); };
|
||||
|
||||
/* Try to mount. */
|
||||
R_UNLESS(fsdevMountDevice(device_name, fs) >= 0, fs::ResultMountNameAlreadyExists());
|
||||
fs_guard.Cancel();
|
||||
|
||||
this->has_mounted = true;
|
||||
return ResultSuccess();
|
||||
}
|
||||
public:
|
||||
ExternalContentSource(const char *dn, os::ManagedHandle client) : client(std::move(client)) {
|
||||
std::strncpy(this->device_name, dn, sizeof(this->device_name));
|
||||
this->device_name[sizeof(this->device_name) - 1] = '\0';
|
||||
}
|
||||
|
||||
Result EnsureMounted() {
|
||||
if (!this->has_mounted) {
|
||||
return Mount();
|
||||
}
|
||||
return ResultSuccess();
|
||||
}
|
||||
|
||||
~ExternalContentSource() {
|
||||
if (this->has_mounted) {
|
||||
fsdevUnmountDevice(this->device_name);
|
||||
}
|
||||
}
|
||||
|
||||
const char *GetDeviceName() const {
|
||||
return this->device_name;
|
||||
}
|
||||
};
|
||||
|
||||
/* Global storage. */
|
||||
std::unordered_map<u64, ExternalContentSource> g_map;
|
||||
}
|
||||
|
||||
/* API. */
|
||||
const char *Get(ncm::ProgramId program_id) {
|
||||
auto it = g_map.find(static_cast<u64>(program_id));
|
||||
if (it == g_map.end()) {
|
||||
return nullptr;
|
||||
}
|
||||
if (R_FAILED(it->second.EnsureMounted())) {
|
||||
g_map.erase(it);
|
||||
return nullptr;
|
||||
}
|
||||
return it->second.GetDeviceName();
|
||||
}
|
||||
|
||||
Result Set(Handle *out, ncm::ProgramId program_id) {
|
||||
/* TODO: Is this an appropriate error? */
|
||||
R_UNLESS(g_map.size() < MaxExternalContentSourceCount, ldr::ResultTooManyArguments());
|
||||
|
||||
/* Clear any sources. */
|
||||
R_ABORT_UNLESS(Clear(program_id));
|
||||
|
||||
/* Generate mountpoint. */
|
||||
char device_name[DeviceNameSizeMax];
|
||||
std::snprintf(device_name, DeviceNameSizeMax, "ecs-%016lx", static_cast<u64>(program_id));
|
||||
|
||||
/* Create session. */
|
||||
os::ManagedHandle server, client;
|
||||
R_TRY(svcCreateSession(server.GetPointer(), client.GetPointer(), 0, 0));
|
||||
|
||||
/* Do not create service yet. */
|
||||
/* Defer until we've handed the server side back so we don't deadlock on querying pointer buffer size. */
|
||||
|
||||
/* Add to map. */
|
||||
g_map.emplace(std::piecewise_construct,
|
||||
std::make_tuple(static_cast<u64>(program_id)),
|
||||
std::make_tuple(device_name, std::move(client)));
|
||||
*out = server.Move();
|
||||
return ResultSuccess();
|
||||
}
|
||||
|
||||
Result Clear(ncm::ProgramId program_id) {
|
||||
/* Delete if present. */
|
||||
g_map.erase(static_cast<u64>(program_id));
|
||||
return ResultSuccess();
|
||||
}
|
||||
|
||||
}
|
|
@ -1,26 +0,0 @@
|
|||
/*
|
||||
* Copyright (c) 2018-2020 Atmosphère-NX
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify it
|
||||
* under the terms and conditions of the GNU General Public License,
|
||||
* version 2, as published by the Free Software Foundation.
|
||||
*
|
||||
* This program is distributed in the hope it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
||||
* more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
#pragma once
|
||||
#include <stratosphere.hpp>
|
||||
|
||||
namespace ams::ldr::ecs {
|
||||
|
||||
/* External Content Source API. */
|
||||
const char *Get(ncm::ProgramId program_id);
|
||||
Result Set(Handle *out, ncm::ProgramId program_id);
|
||||
Result Clear(ncm::ProgramId program_id);
|
||||
|
||||
}
|
|
@ -49,7 +49,7 @@ namespace ams::ldr {
|
|||
if (IsTrackableSystemProgramId(program_id)) {
|
||||
return HasLaunchedSystemProgram(ncm::SystemProgramId{program_id.value});
|
||||
} else {
|
||||
return g_launched_programs.find(static_cast<u64>(program_id)) != g_launched_programs.end();
|
||||
return g_launched_programs.find(program_id.value) != g_launched_programs.end();
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -15,7 +15,6 @@
|
|||
*/
|
||||
#include "ldr_arguments.hpp"
|
||||
#include "ldr_content_management.hpp"
|
||||
#include "ldr_ecs.hpp"
|
||||
#include "ldr_process_creation.hpp"
|
||||
#include "ldr_launch_record.hpp"
|
||||
#include "ldr_loader_service.hpp"
|
||||
|
@ -97,12 +96,12 @@ namespace ams::ldr {
|
|||
}
|
||||
|
||||
/* Atmosphere commands. */
|
||||
Result LoaderService::AtmosphereSetExternalContentSource(sf::OutMoveHandle out, ncm::ProgramId program_id) {
|
||||
return ecs::Set(out.GetHandlePointer(), program_id);
|
||||
Result LoaderService::AtmosphereRegisterExternalCode(sf::OutMoveHandle out, ncm::ProgramId program_id) {
|
||||
return fssystem::CreateExternalCode(out.GetHandlePointer(), program_id);
|
||||
}
|
||||
|
||||
void LoaderService::AtmosphereClearExternalContentSource(ncm::ProgramId program_id) {
|
||||
R_ABORT_UNLESS(ecs::Clear(program_id));
|
||||
void LoaderService::AtmosphereUnregisterExternalCode(ncm::ProgramId program_id) {
|
||||
fssystem::DestroyExternalCode(program_id);
|
||||
}
|
||||
|
||||
void LoaderService::AtmosphereHasLaunchedProgram(sf::Out<bool> out, ncm::ProgramId program_id) {
|
||||
|
|
|
@ -30,8 +30,8 @@ namespace ams::ldr {
|
|||
virtual Result GetProcessModuleInfo(sf::Out<u32> count, const sf::OutPointerArray<ModuleInfo> &out, os::ProcessId process_id);
|
||||
|
||||
/* Atmosphere commands. */
|
||||
virtual Result AtmosphereSetExternalContentSource(sf::OutMoveHandle out, ncm::ProgramId program_id);
|
||||
virtual void AtmosphereClearExternalContentSource(ncm::ProgramId program_id);
|
||||
virtual Result AtmosphereRegisterExternalCode(sf::OutMoveHandle out, ncm::ProgramId program_id);
|
||||
virtual void AtmosphereUnregisterExternalCode(ncm::ProgramId program_id);
|
||||
virtual void AtmosphereHasLaunchedProgram(sf::Out<bool> out, ncm::ProgramId program_id);
|
||||
virtual Result AtmosphereGetProgramInfo(sf::Out<ProgramInfo> out_program_info, sf::Out<cfg::OverrideStatus> out_status, const ncm::ProgramLocation &loc);
|
||||
virtual Result AtmospherePinProgram(sf::Out<PinId> out_id, const ncm::ProgramLocation &loc, const cfg::OverrideStatus &override_status);
|
||||
|
@ -97,16 +97,16 @@ namespace ams::ldr {
|
|||
SetProgramArguments = 0,
|
||||
FlushArguments = 1,
|
||||
|
||||
AtmosphereSetExternalContentSource = 65000,
|
||||
AtmosphereClearExternalContentSource = 65001,
|
||||
AtmosphereRegisterExternalCode = 65000,
|
||||
AtmosphereUnregisterExternalCode = 65001,
|
||||
};
|
||||
public:
|
||||
DEFINE_SERVICE_DISPATCH_TABLE {
|
||||
MAKE_SERVICE_COMMAND_META(SetProgramArguments),
|
||||
MAKE_SERVICE_COMMAND_META(FlushArguments),
|
||||
|
||||
MAKE_SERVICE_COMMAND_META(AtmosphereSetExternalContentSource),
|
||||
MAKE_SERVICE_COMMAND_META(AtmosphereClearExternalContentSource),
|
||||
MAKE_SERVICE_COMMAND_META(AtmosphereRegisterExternalCode),
|
||||
MAKE_SERVICE_COMMAND_META(AtmosphereUnregisterExternalCode),
|
||||
};
|
||||
};
|
||||
|
||||
|
|
|
@ -23,7 +23,8 @@ namespace ams::ldr {
|
|||
|
||||
/* Convenience definitions. */
|
||||
constexpr size_t MetaCacheBufferSize = 0x8000;
|
||||
constexpr const char *MetaFilePath = "/main.npdm";
|
||||
constexpr inline const char AtmosphereMetaPath[] = ENCODE_ATMOSPHERE_CODE_PATH("/main.npdm");
|
||||
constexpr inline const char BaseMetaPath[] = ENCODE_CODE_PATH("/main.npdm");
|
||||
|
||||
/* Types. */
|
||||
struct MetaCache {
|
||||
|
@ -93,25 +94,23 @@ namespace ams::ldr {
|
|||
return ResultSuccess();
|
||||
}
|
||||
|
||||
Result LoadMetaFromFile(FILE *f, MetaCache *cache) {
|
||||
Result LoadMetaFromFile(fs::FileHandle file, MetaCache *cache) {
|
||||
/* Reset cache. */
|
||||
cache->meta = {};
|
||||
|
||||
/* Read from file. */
|
||||
size_t npdm_size = 0;
|
||||
s64 npdm_size = 0;
|
||||
{
|
||||
/* Get file size. */
|
||||
fseek(f, 0, SEEK_END);
|
||||
npdm_size = ftell(f);
|
||||
fseek(f, 0, SEEK_SET);
|
||||
R_TRY(fs::GetFileSize(std::addressof(npdm_size), file));
|
||||
|
||||
/* Read data into cache buffer. */
|
||||
R_UNLESS(npdm_size <= MetaCacheBufferSize, ResultTooLargeMeta());
|
||||
R_UNLESS(fread(cache->buffer, npdm_size, 1, f) == 1, ResultTooLargeMeta());
|
||||
R_UNLESS(npdm_size <= static_cast<s64>(MetaCacheBufferSize), ResultTooLargeMeta());
|
||||
R_TRY(fs::ReadFile(file, 0, cache->buffer, npdm_size));
|
||||
}
|
||||
|
||||
/* Ensure size is big enough. */
|
||||
R_UNLESS(npdm_size >= sizeof(Npdm), ResultInvalidMeta());
|
||||
R_UNLESS(npdm_size >= static_cast<s64>(sizeof(Npdm)), ResultInvalidMeta());
|
||||
|
||||
/* Validate the meta. */
|
||||
{
|
||||
|
@ -146,13 +145,12 @@ namespace ams::ldr {
|
|||
|
||||
/* API. */
|
||||
Result LoadMeta(Meta *out_meta, ncm::ProgramId program_id, const cfg::OverrideStatus &status) {
|
||||
FILE *f = nullptr;
|
||||
|
||||
/* Try to load meta from file. */
|
||||
R_TRY(OpenCodeFile(f, program_id, status, MetaFilePath));
|
||||
fs::FileHandle file;
|
||||
R_TRY(fs::OpenFile(std::addressof(file), AtmosphereMetaPath, fs::OpenMode_Read));
|
||||
{
|
||||
ON_SCOPE_EXIT { fclose(f); };
|
||||
R_TRY(LoadMetaFromFile(f, &g_meta_cache));
|
||||
ON_SCOPE_EXIT { fs::CloseFile(file); };
|
||||
R_TRY(LoadMetaFromFile(file, &g_meta_cache));
|
||||
}
|
||||
|
||||
/* Patch meta. Start by setting all program ids to the current program id. */
|
||||
|
@ -163,9 +161,9 @@ namespace ams::ldr {
|
|||
|
||||
/* For HBL, we need to copy some information from the base meta. */
|
||||
if (status.IsHbl()) {
|
||||
if (R_SUCCEEDED(OpenCodeFileFromBaseExefs(f, program_id, status, MetaFilePath))) {
|
||||
ON_SCOPE_EXIT { fclose(f); };
|
||||
if (R_SUCCEEDED(LoadMetaFromFile(f, &g_original_meta_cache))) {
|
||||
if (R_SUCCEEDED(fs::OpenFile(std::addressof(file), BaseMetaPath, fs::OpenMode_Read))) {
|
||||
ON_SCOPE_EXIT { fs::CloseFile(file); };
|
||||
if (R_SUCCEEDED(LoadMetaFromFile(file, &g_original_meta_cache))) {
|
||||
Meta *o_meta = &g_original_meta_cache.meta;
|
||||
|
||||
/* Fix pool partition. */
|
||||
|
|
|
@ -26,13 +26,42 @@ namespace ams::ldr {
|
|||
constexpr size_t NsoPatchesProtectedSize = sizeof(NsoHeader);
|
||||
constexpr size_t NsoPatchesProtectedOffset = sizeof(NsoHeader);
|
||||
|
||||
constexpr const char * const LoaderSdMountName = "#amsldr-sdpatch";
|
||||
static_assert(sizeof(LoaderSdMountName) <= fs::MountNameLengthMax);
|
||||
|
||||
os::Mutex g_ldr_sd_lock;
|
||||
bool g_mounted_sd;
|
||||
|
||||
bool EnsureSdCardMounted() {
|
||||
std::scoped_lock lk(g_ldr_sd_lock);
|
||||
|
||||
if (g_mounted_sd) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (!cfg::IsSdCardInitialized()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (R_FAILED(fs::MountSdCard(LoaderSdMountName))) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return (g_mounted_sd = true);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
/* Apply IPS patches. */
|
||||
void LocateAndApplyIpsPatchesToModule(const u8 *build_id, uintptr_t mapped_nso, size_t mapped_size) {
|
||||
if (!EnsureSdCardMounted()) {
|
||||
return;
|
||||
}
|
||||
|
||||
ro::ModuleId module_id;
|
||||
std::memcpy(&module_id.build_id, build_id, sizeof(module_id.build_id));
|
||||
ams::patcher::LocateAndApplyIpsPatchesToModule(NsoPatchesDirectory, NsoPatchesProtectedSize, NsoPatchesProtectedOffset, &module_id, reinterpret_cast<u8 *>(mapped_nso), mapped_size);
|
||||
ams::patcher::LocateAndApplyIpsPatchesToModule(LoaderSdMountName, NsoPatchesDirectory, NsoPatchesProtectedSize, NsoPatchesProtectedOffset, &module_id, reinterpret_cast<u8 *>(mapped_nso), mapped_size);
|
||||
}
|
||||
|
||||
}
|
|
@ -15,7 +15,6 @@
|
|||
*/
|
||||
#include "ldr_capabilities.hpp"
|
||||
#include "ldr_content_management.hpp"
|
||||
#include "ldr_ecs.hpp"
|
||||
#include "ldr_launch_record.hpp"
|
||||
#include "ldr_meta.hpp"
|
||||
#include "ldr_patcher.hpp"
|
||||
|
@ -47,25 +46,25 @@ namespace ams::ldr {
|
|||
Nso_Count,
|
||||
};
|
||||
|
||||
constexpr const char *GetNsoName(size_t idx) {
|
||||
AMS_ABORT_UNLESS(idx < Nso_Count);
|
||||
constexpr inline const char *NsoPaths[Nso_Count] = {
|
||||
ENCODE_ATMOSPHERE_CODE_PATH("/rtld"),
|
||||
ENCODE_ATMOSPHERE_CODE_PATH("/main"),
|
||||
ENCODE_ATMOSPHERE_CODE_PATH("/subsdk0"),
|
||||
ENCODE_ATMOSPHERE_CODE_PATH("/subsdk1"),
|
||||
ENCODE_ATMOSPHERE_CODE_PATH("/subsdk2"),
|
||||
ENCODE_ATMOSPHERE_CODE_PATH("/subsdk3"),
|
||||
ENCODE_ATMOSPHERE_CODE_PATH("/subsdk4"),
|
||||
ENCODE_ATMOSPHERE_CODE_PATH("/subsdk5"),
|
||||
ENCODE_ATMOSPHERE_CODE_PATH("/subsdk6"),
|
||||
ENCODE_ATMOSPHERE_CODE_PATH("/subsdk7"),
|
||||
ENCODE_ATMOSPHERE_CODE_PATH("/subsdk8"),
|
||||
ENCODE_ATMOSPHERE_CODE_PATH("/subsdk9"),
|
||||
ENCODE_ATMOSPHERE_CODE_PATH("/sdk"),
|
||||
};
|
||||
|
||||
constexpr const char *NsoNames[Nso_Count] = {
|
||||
"rtld",
|
||||
"main",
|
||||
"subsdk0",
|
||||
"subsdk1",
|
||||
"subsdk2",
|
||||
"subsdk3",
|
||||
"subsdk4",
|
||||
"subsdk5",
|
||||
"subsdk6",
|
||||
"subsdk7",
|
||||
"subsdk8",
|
||||
"subsdk9",
|
||||
"sdk",
|
||||
};
|
||||
return NsoNames[idx];
|
||||
constexpr const char *GetNsoPath(size_t idx) {
|
||||
AMS_ABORT_UNLESS(idx < Nso_Count);
|
||||
return NsoPaths[idx];
|
||||
}
|
||||
|
||||
struct ProcessInfo {
|
||||
|
@ -166,17 +165,21 @@ namespace ams::ldr {
|
|||
return static_cast<Acid::PoolPartition>((meta->acid->flags & Acid::AcidFlag_PoolPartitionMask) >> Acid::AcidFlag_PoolPartitionShift);
|
||||
}
|
||||
|
||||
Result LoadNsoHeaders(ncm::ProgramId program_id, const cfg::OverrideStatus &override_status, NsoHeader *nso_headers, bool *has_nso) {
|
||||
Result LoadNsoHeaders(NsoHeader *nso_headers, bool *has_nso) {
|
||||
/* Clear NSOs. */
|
||||
std::memset(nso_headers, 0, sizeof(*nso_headers) * Nso_Count);
|
||||
std::memset(has_nso, 0, sizeof(*has_nso) * Nso_Count);
|
||||
|
||||
for (size_t i = 0; i < Nso_Count; i++) {
|
||||
FILE *f = nullptr;
|
||||
if (R_SUCCEEDED(OpenCodeFile(f, program_id, override_status, GetNsoName(i)))) {
|
||||
ON_SCOPE_EXIT { fclose(f); };
|
||||
fs::FileHandle file;
|
||||
if (R_SUCCEEDED(fs::OpenFile(std::addressof(file), GetNsoPath(i), fs::OpenMode_Read))) {
|
||||
ON_SCOPE_EXIT { fs::CloseFile(file); };
|
||||
|
||||
/* Read NSO header. */
|
||||
R_UNLESS(fread(nso_headers + i, sizeof(*nso_headers), 1, f) == 1, ResultInvalidNso());
|
||||
size_t read_size;
|
||||
R_TRY(fs::ReadFile(std::addressof(read_size), file, 0, nso_headers + i, sizeof(*nso_headers)));
|
||||
R_UNLESS(read_size == sizeof(*nso_headers), ResultInvalidNso());
|
||||
|
||||
has_nso[i] = true;
|
||||
}
|
||||
}
|
||||
|
@ -437,7 +440,7 @@ namespace ams::ldr {
|
|||
return svcCreateProcess(out->process_handle.GetPointer(), ¶m, reinterpret_cast<const u32 *>(meta->aci_kac), meta->aci->kac_size / sizeof(u32));
|
||||
}
|
||||
|
||||
Result LoadNsoSegment(FILE *f, const NsoHeader::SegmentInfo *segment, size_t file_size, const u8 *file_hash, bool is_compressed, bool check_hash, uintptr_t map_base, uintptr_t map_end) {
|
||||
Result LoadNsoSegment(fs::FileHandle file, const NsoHeader::SegmentInfo *segment, size_t file_size, const u8 *file_hash, bool is_compressed, bool check_hash, uintptr_t map_base, uintptr_t map_end) {
|
||||
/* Select read size based on compression. */
|
||||
if (!is_compressed) {
|
||||
file_size = segment->size;
|
||||
|
@ -449,8 +452,9 @@ namespace ams::ldr {
|
|||
|
||||
/* Load data from file. */
|
||||
uintptr_t load_address = is_compressed ? map_end - file_size : map_base;
|
||||
fseek(f, segment->file_offset, SEEK_SET);
|
||||
R_UNLESS(fread(reinterpret_cast<void *>(load_address), file_size, 1, f) == 1, ResultInvalidNso());
|
||||
size_t read_size;
|
||||
R_TRY(fs::ReadFile(std::addressof(read_size), file, segment->file_offset, reinterpret_cast<void *>(load_address), file_size));
|
||||
R_UNLESS(read_size == file_size, ResultInvalidNso());
|
||||
|
||||
/* Uncompress if necessary. */
|
||||
if (is_compressed) {
|
||||
|
@ -460,8 +464,8 @@ namespace ams::ldr {
|
|||
|
||||
/* Check hash if necessary. */
|
||||
if (check_hash) {
|
||||
u8 hash[SHA256_HASH_SIZE];
|
||||
sha256CalculateHash(hash, reinterpret_cast<void *>(map_base), segment->size);
|
||||
u8 hash[crypto::Sha256Generator::HashSize];
|
||||
crypto::GenerateSha256Hash(hash, sizeof(hash), reinterpret_cast<void *>(map_base), segment->size);
|
||||
|
||||
R_UNLESS(std::memcmp(hash, file_hash, sizeof(hash)) == 0, ResultInvalidNso());
|
||||
}
|
||||
|
@ -469,19 +473,19 @@ namespace ams::ldr {
|
|||
return ResultSuccess();
|
||||
}
|
||||
|
||||
Result LoadNsoIntoProcessMemory(Handle process_handle, FILE *f, uintptr_t map_address, const NsoHeader *nso_header, uintptr_t nso_address, size_t nso_size) {
|
||||
Result LoadNsoIntoProcessMemory(Handle process_handle, fs::FileHandle file, uintptr_t map_address, const NsoHeader *nso_header, uintptr_t nso_address, size_t nso_size) {
|
||||
/* Map and read data from file. */
|
||||
{
|
||||
map::AutoCloseMap mapper(map_address, process_handle, nso_address, nso_size);
|
||||
R_TRY(mapper.GetResult());
|
||||
|
||||
/* Load NSO segments. */
|
||||
R_TRY(LoadNsoSegment(f, &nso_header->segments[NsoHeader::Segment_Text], nso_header->text_compressed_size, nso_header->text_hash, (nso_header->flags & NsoHeader::Flag_CompressedText) != 0,
|
||||
(nso_header->flags & NsoHeader::Flag_CheckHashText) != 0, map_address + nso_header->text_dst_offset, map_address + nso_size));
|
||||
R_TRY(LoadNsoSegment(f, &nso_header->segments[NsoHeader::Segment_Ro], nso_header->ro_compressed_size, nso_header->ro_hash, (nso_header->flags & NsoHeader::Flag_CompressedRo) != 0,
|
||||
(nso_header->flags & NsoHeader::Flag_CheckHashRo) != 0, map_address + nso_header->ro_dst_offset, map_address + nso_size));
|
||||
R_TRY(LoadNsoSegment(f, &nso_header->segments[NsoHeader::Segment_Rw], nso_header->rw_compressed_size, nso_header->rw_hash, (nso_header->flags & NsoHeader::Flag_CompressedRw) != 0,
|
||||
(nso_header->flags & NsoHeader::Flag_CheckHashRw) != 0, map_address + nso_header->rw_dst_offset, map_address + nso_size));
|
||||
R_TRY(LoadNsoSegment(file, &nso_header->segments[NsoHeader::Segment_Text], nso_header->text_compressed_size, nso_header->text_hash, (nso_header->flags & NsoHeader::Flag_CompressedText) != 0,
|
||||
(nso_header->flags & NsoHeader::Flag_CheckHashText) != 0, map_address + nso_header->text_dst_offset, map_address + nso_size));
|
||||
R_TRY(LoadNsoSegment(file, &nso_header->segments[NsoHeader::Segment_Ro], nso_header->ro_compressed_size, nso_header->ro_hash, (nso_header->flags & NsoHeader::Flag_CompressedRo) != 0,
|
||||
(nso_header->flags & NsoHeader::Flag_CheckHashRo) != 0, map_address + nso_header->ro_dst_offset, map_address + nso_size));
|
||||
R_TRY(LoadNsoSegment(file, &nso_header->segments[NsoHeader::Segment_Rw], nso_header->rw_compressed_size, nso_header->rw_hash, (nso_header->flags & NsoHeader::Flag_CompressedRw) != 0,
|
||||
(nso_header->flags & NsoHeader::Flag_CheckHashRw) != 0, map_address + nso_header->rw_dst_offset, map_address + nso_size));
|
||||
|
||||
/* Clear unused space to zero. */
|
||||
const size_t text_end = nso_header->text_dst_offset + nso_header->text_size;
|
||||
|
@ -513,20 +517,20 @@ namespace ams::ldr {
|
|||
return ResultSuccess();
|
||||
}
|
||||
|
||||
Result LoadNsosIntoProcessMemory(const ProcessInfo *process_info, const ncm::ProgramId program_id, const cfg::OverrideStatus &override_status, const NsoHeader *nso_headers, const bool *has_nso, const args::ArgumentInfo *arg_info) {
|
||||
Result LoadNsosIntoProcessMemory(const ProcessInfo *process_info, const NsoHeader *nso_headers, const bool *has_nso, const args::ArgumentInfo *arg_info) {
|
||||
const Handle process_handle = process_info->process_handle.Get();
|
||||
|
||||
/* Load each NSO. */
|
||||
for (size_t i = 0; i < Nso_Count; i++) {
|
||||
if (has_nso[i]) {
|
||||
FILE *f = nullptr;
|
||||
R_TRY(OpenCodeFile(f, program_id, override_status, GetNsoName(i)));
|
||||
ON_SCOPE_EXIT { fclose(f); };
|
||||
fs::FileHandle file;
|
||||
R_TRY(fs::OpenFile(std::addressof(file), GetNsoPath(i), fs::OpenMode_Read));
|
||||
ON_SCOPE_EXIT { fs::CloseFile(file); };
|
||||
|
||||
uintptr_t map_address = 0;
|
||||
R_TRY(map::LocateMappableSpace(&map_address, process_info->nso_size[i]));
|
||||
|
||||
R_TRY(LoadNsoIntoProcessMemory(process_handle, f, map_address, nso_headers + i, process_info->nso_address[i], process_info->nso_size[i]));
|
||||
R_TRY(LoadNsoIntoProcessMemory(process_handle, file, map_address, nso_headers + i, process_info->nso_address[i], process_info->nso_size[i]));
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -576,7 +580,7 @@ namespace ams::ldr {
|
|||
R_TRY(ValidateMeta(&meta, loc));
|
||||
|
||||
/* Load, validate NSOs. */
|
||||
R_TRY(LoadNsoHeaders(loc.program_id, override_status, nso_headers, has_nso));
|
||||
R_TRY(LoadNsoHeaders(nso_headers, has_nso));
|
||||
R_TRY(ValidateNsoHeaders(nso_headers, has_nso));
|
||||
|
||||
/* Actually create process. */
|
||||
|
@ -584,7 +588,7 @@ namespace ams::ldr {
|
|||
R_TRY(CreateProcessImpl(&info, &meta, nso_headers, has_nso, arg_info, flags, reslimit_h));
|
||||
|
||||
/* Load NSOs into process memory. */
|
||||
R_TRY(LoadNsosIntoProcessMemory(&info, loc.program_id, override_status, nso_headers, has_nso, arg_info));
|
||||
R_TRY(LoadNsosIntoProcessMemory(&info, nso_headers, has_nso, arg_info));
|
||||
|
||||
/* Register NSOs with ro manager. */
|
||||
{
|
||||
|
@ -603,13 +607,13 @@ namespace ams::ldr {
|
|||
}
|
||||
|
||||
/* If we're overriding for HBL, perform HTML document redirection. */
|
||||
if (mount.IsHblMounted()) {
|
||||
if (override_status.IsHbl()) {
|
||||
/* Don't validate result, failure is okay. */
|
||||
RedirectHtmlDocumentPathForHbl(loc);
|
||||
}
|
||||
|
||||
/* Clear the ECS entry for the program. */
|
||||
R_ABORT_UNLESS(ecs::Clear(loc.program_id));
|
||||
/* Clear the external code for the program. */
|
||||
fssystem::DestroyExternalCode(loc.program_id);
|
||||
|
||||
/* Note that we've created the program. */
|
||||
SetLaunchedProgram(loc.program_id);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue