loader: completely rewrite.

This commit is contained in:
Michael Scire
2019-06-26 15:46:19 -07:00
parent 9217e4c5f9
commit 61fcf5e0f4
49 changed files with 2523 additions and 5817 deletions

View File

@@ -13,459 +13,331 @@
* 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 <stratosphere/cfg.hpp>
#include <cstring>
#include <switch.h>
#include <stratosphere.hpp>
#include <strings.h>
#include <vector>
#include <algorithm>
#include <map>
#include "ldr_registration.hpp"
#include "ldr_content_management.hpp"
#include "ldr_hid.hpp"
#include "ldr_npdm.hpp"
#include "ldr_ecs.hpp"
#include "ini.h"
namespace sts::ldr {
static FsFileSystem g_CodeFileSystem = {};
static FsFileSystem g_HblFileSystem = {};
namespace {
static std::vector<u64> g_created_titles;
static bool g_has_initialized_fs_dev = false;
/* DeviceNames. */
constexpr const char *CodeFileSystemDeviceName = "code";
constexpr const char *HblFileSystemDeviceName = "hbl";
constexpr const char *SdCardFileSystemDeviceName = "sdmc";
/* Default to Key R, hold disables override, HBL at atmosphere/hbl.nsp. */
static bool g_mounted_hbl_nsp = false;
static char g_hbl_sd_path[FS_MAX_PATH+1] = "@Sdcard:/atmosphere/hbl.nsp\x00";
constexpr const char *SdCardStorageMountPoint = "@Sdcard";
static OverrideKey g_default_override_key = {
.key_combination = KEY_L,
.override_by_default = true
};
/* Globals. */
bool g_has_mounted_sd_card = false;
struct HblOverrideConfig {
OverrideKey override_key;
u64 title_id;
bool override_any_app;
};
ncm::TitleId g_should_override_title_id;
bool g_should_override_hbl = false;
bool g_should_override_sd = false;
static HblOverrideConfig g_hbl_override_config = {
.override_key = {
.key_combination = KEY_R,
.override_by_default = true
},
.title_id = TitleId_AppletPhotoViewer,
.override_any_app = false
};
/* Helpers. */
inline void FixFileSystemPath(char *path) {
for (size_t i = 0; i < FS_MAX_PATH && path[i]; i++) {
if (path[i] == '\\') {
path[i] = '/';
}
}
}
/* Static buffer for loader.ini contents at runtime. */
static char g_config_ini_data[0x800];
inline const char *GetRelativePathStart(const char *relative_path) {
while (*relative_path == '/' || *relative_path == '\\') {
relative_path++;
}
return relative_path;
}
/* SetExternalContentSource extension */
static std::map<u64, ContentManagement::ExternalContentSource> g_external_content_sources;
void UpdateShouldOverrideCache(ncm::TitleId title_id) {
if (g_should_override_title_id != title_id) {
cfg::GetOverrideKeyHeldStatus(&g_should_override_hbl, &g_should_override_sd, title_id);
}
g_should_override_title_id = title_id;
}
Result ContentManagement::MountCode(u64 tid, FsStorageId sid) {
char path[FS_MAX_PATH] = {0};
void InvalidateShouldOverrideCache() {
g_should_override_title_id = {};
}
bool ShouldOverrideWithHbl(ncm::TitleId title_id) {
UpdateShouldOverrideCache(title_id);
return g_should_override_hbl;
}
bool ShouldOverrideWithSd(ncm::TitleId title_id) {
UpdateShouldOverrideCache(title_id);
return g_should_override_sd;
}
Result MountSdCardFileSystem() {
return fsdevMountSdmc();
}
Result MountNspFileSystem(const char *device_name, const char *path) {
FsFileSystem fs;
R_TRY(fsOpenFileSystemWithId(&fs, 0, FsFileSystemType_ApplicationPackage, path));
if(fsdevMountDevice(device_name, fs) == -1) {
std::abort();
}
return ResultSuccess;
}
Result MountHblFileSystem() {
char path[FS_MAX_PATH];
/* Print and fix path. */
std::snprintf(path, FS_MAX_PATH, "%s:/%s", SdCardStorageMountPoint, GetRelativePathStart(cfg::GetHblPath()));
FixFileSystemPath(path);
return MountNspFileSystem(HblFileSystemDeviceName, path);
}
Result MountSdCardCodeFileSystem(ncm::TitleId title_id) {
char path[FS_MAX_PATH];
/* Print and fix path. */
std::snprintf(path, FS_MAX_PATH, "%s:/atmosphere/titles/%016lx/exefs.nsp", SdCardStorageMountPoint, static_cast<u64>(title_id));
FixFileSystemPath(path);
return MountNspFileSystem(CodeFileSystemDeviceName, path);
}
Result MountCodeFileSystem(const ncm::TitleLocation &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.title_id), path, &fs));
if(fsdevMountDevice(CodeFileSystemDeviceName, fs) == -1) {
std::abort();
}
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::TitleId title_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/titles/%016lx/exefs/%s", static_cast<u64>(title_id), GetRelativePathStart(relative_path));
FixFileSystemPath(path);
return OpenFile(SdCardFileSystemDeviceName, path);
}
bool IsFileStubbed(ncm::TitleId title_id, 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 (!ShouldOverrideWithSd(title_id)) {
return false;
}
char path[FS_MAX_PATH];
std::snprintf(path, FS_MAX_PATH, "/atmosphere/titles/%016lx/exefs/%s.stub", static_cast<u64>(title_id), GetRelativePathStart(relative_path));
FixFileSystemPath(path);
FILE *f = OpenFile(SdCardFileSystemDeviceName, path);
if (f == nullptr) {
return false;
}
fclose(f);
return true;
}
bool IsMounted(const char *device_name) {
/* Allow nullptr device_name -- those are simply not openable. */
if (device_name == nullptr) {
return false;
}
char path[FS_MAX_PATH];
std::snprintf(path, FS_MAX_PATH, "%s:", device_name);
return FindDevice(device_name) >= 0;
}
FILE *OpenBaseExefsFile(ncm::TitleId title_id, const char *relative_path) {
/* Allow nullptr relative path -- those are simply not openable. */
if (relative_path == nullptr) {
return nullptr;
}
/* Check if stubbed. */
if (IsFileStubbed(title_id, relative_path)) {
return nullptr;
}
return OpenFile(CodeFileSystemDeviceName, relative_path);
}
/* We defer SD card mounting, so if relevant ensure it is mounted. */
if (!g_has_initialized_fs_dev) {
TryMountSdCard();
}
if (g_has_initialized_fs_dev) {
RefreshConfigurationData();
ScopedCodeMount::~ScopedCodeMount() {
/* Unmount devices. */
if (this->is_code_mounted) {
fsdevUnmountDevice(CodeFileSystemDeviceName);
}
if (this->is_hbl_mounted) {
fsdevUnmountDevice(HblFileSystemDeviceName);
}
/* Unmounting code means we should invalidate our configuration cache. */
InvalidateShouldOverrideCache();
}
if (ShouldOverrideContentsWithSD(tid) && R_SUCCEEDED(MountCodeNspOnSd(tid))) {
Result MountCode(ScopedCodeMount &out, const ncm::TitleLocation &loc) {
ScopedCodeMount mount;
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_ASSERT(MountSdCardFileSystem());
g_has_mounted_sd_card = true;
}
}
/* Check if we should override contents. */
if (ShouldOverrideWithHbl(loc.title_id)) {
/* Try to mount HBL. */
if (R_SUCCEEDED(MountHblFileSystem())) {
mount.SetHblMounted();
}
}
if (ShouldOverrideWithSd(loc.title_id)) {
/* Try to mount Code NSP on SD. */
if (R_SUCCEEDED(MountSdCardCodeFileSystem(loc.title_id))) {
mount.SetCodeMounted();
}
}
/* If we haven't already mounted code, mount it. */
if (!mount.IsCodeMounted()) {
R_TRY(MountCodeFileSystem(loc));
mount.SetCodeMounted();
}
/* Set out to scoped holder. */
out = std::move(mount);
return ResultSuccess;
}
R_TRY(ResolveContentPath(path, tid, sid));
Result OpenCodeFile(FILE *&out, ncm::TitleId title_id, const char *relative_path) {
FILE *f = nullptr;
const char *ecs_device_name = ecs::Get(title_id);
/* Fix up path. */
for (unsigned int i = 0; i < FS_MAX_PATH && path[i] != '\x00'; i++) {
if (path[i] == '\\') {
path[i] = '/';
if (IsMounted(ecs_device_name)) {
/* First priority: Open from external content. */
f = OpenFile(ecs_device_name, relative_path);
} else if (ShouldOverrideWithHbl(title_id)) {
/* Next, try to mount from HBL. */
f = OpenFile(HblFileSystemDeviceName, relative_path);
} else {
/* If not ECS or HBL, try a loose file on the SD. */
if (ShouldOverrideWithSd(title_id)) {
f = OpenLooseSdFile(title_id, relative_path);
}
/* If we fail, try the original exefs. */
if (f == nullptr) {
f = OpenBaseExefsFile(title_id, relative_path);
}
}
}
/* Always re-initialize fsp-ldr, in case it's closed */
DoWithSmSession([&]() {
R_ASSERT(fsldrInitialize());
});
ON_SCOPE_EXIT { fsldrExit(); };
R_TRY(fsldrOpenCodeFileSystem(tid, path, &g_CodeFileSystem));
fsdevMountDevice("code", g_CodeFileSystem);
TryMountHblNspOnSd();
return ResultSuccess;
}
Result ContentManagement::UnmountCode() {
if (g_mounted_hbl_nsp) {
fsdevUnmountDevice("hbl");
g_mounted_hbl_nsp = false;
}
fsdevUnmountDevice("code");
return ResultSuccess;
}
void ContentManagement::TryMountHblNspOnSd() {
char path[FS_MAX_PATH + 1];
strncpy(path, g_hbl_sd_path, FS_MAX_PATH);
path[FS_MAX_PATH] = 0;
for (unsigned int i = 0; i < FS_MAX_PATH && path[i] != '\x00'; i++) {
if (path[i] == '\\') {
path[i] = '/';
/* If nothing worked, we failed to find the path. */
if (f == nullptr) {
return ResultFsPathNotFound;
}
out = f;
return ResultSuccess;
}
if (g_has_initialized_fs_dev && !g_mounted_hbl_nsp && R_SUCCEEDED(fsOpenFileSystemWithId(&g_HblFileSystem, 0, FsFileSystemType_ApplicationPackage, path))) {
fsdevMountDevice("hbl", g_HblFileSystem);
g_mounted_hbl_nsp = true;
Result OpenCodeFileFromBaseExefs(FILE *&out, ncm::TitleId title_id, const char *relative_path) {
/* Open the file. */
FILE *f = OpenBaseExefsFile(title_id, relative_path);
if (f == nullptr) {
return ResultFsPathNotFound;
}
out = f;
return ResultSuccess;
}
}
Result ContentManagement::MountCodeNspOnSd(u64 tid) {
char path[FS_MAX_PATH+1] = {0};
snprintf(path, FS_MAX_PATH, "@Sdcard:/atmosphere/titles/%016lx/exefs.nsp", tid);
/* Redirection API. */
Result ResolveContentPath(char *out_path, const ncm::TitleLocation &loc) {
char path[FS_MAX_PATH];
R_TRY(fsOpenFileSystemWithId(&g_CodeFileSystem, 0, FsFileSystemType_ApplicationPackage, path));
fsdevMountDevice("code", g_CodeFileSystem);
TryMountHblNspOnSd();
/* Try to get the path from the registered resolver. */
LrRegisteredLocationResolver reg;
R_TRY(lrOpenRegisteredLocationResolver(&reg));
ON_SCOPE_EXIT { serviceClose(&reg.s); };
return ResultSuccess;
}
R_TRY_CATCH(lrRegLrResolveProgramPath(&reg, static_cast<u64>(loc.title_id), path)) {
R_CATCH(ResultLrProgramNotFound) {
/* Program wasn't found via registered resolver, fall back to the normal resolver. */
LrLocationResolver lr;
R_TRY(lrOpenLocationResolver(static_cast<FsStorageId>(loc.storage_id), &lr));
ON_SCOPE_EXIT { serviceClose(&lr.s); };
Result ContentManagement::MountCodeForTidSid(Registration::TidSid *tid_sid) {
return MountCode(tid_sid->title_id, tid_sid->storage_id);
}
R_TRY(lrLrResolveProgramPath(&lr, static_cast<u64>(loc.title_id), path));
}
} R_END_TRY_CATCH;
Result ContentManagement::ResolveContentPath(char *out_path, u64 tid, FsStorageId sid) {
char path[FS_MAX_PATH] = {0};
std::strncpy(out_path, path, FS_MAX_PATH);
out_path[FS_MAX_PATH - 1] = '\0';
FixFileSystemPath(out_path);
return ResultSuccess;
}
/* Try to get the path from the registered resolver. */
LrRegisteredLocationResolver reg;
R_TRY(lrOpenRegisteredLocationResolver(&reg));
ON_SCOPE_EXIT { serviceClose(&reg.s); };
Result RedirectContentPath(const char *path, const ncm::TitleLocation &loc) {
LrLocationResolver lr;
R_TRY(lrOpenLocationResolver(static_cast<FsStorageId>(loc.storage_id), &lr));
ON_SCOPE_EXIT { serviceClose(&lr.s); };
R_TRY_CATCH(lrRegLrResolveProgramPath(&reg, tid, path)) {
R_CATCH(ResultLrProgramNotFound) {
/* Program wasn't found via registered resolver, fall back to the normal resolver. */
LrLocationResolver lr;
R_TRY(lrOpenLocationResolver(sid, &lr));
ON_SCOPE_EXIT { serviceClose(&lr.s); };
return lrLrRedirectProgramPath(&lr, static_cast<u64>(loc.title_id), path);
}
R_TRY(lrLrResolveProgramPath(&lr, tid, path));
Result RedirectHtmlDocumentPathForHbl(const ncm::TitleLocation &loc) {
char path[FS_MAX_PATH];
strncpy(out_path, path, FS_MAX_PATH);
/* Open a locaiton resolver. */
LrLocationResolver lr;
R_TRY(lrOpenLocationResolver(static_cast<FsStorageId>(loc.storage_id), &lr));
ON_SCOPE_EXIT { serviceClose(&lr.s); };
/* If there's already a Html Document path, we don't need to set one. */
if (R_SUCCEEDED(lrLrResolveApplicationHtmlDocumentPath(&lr, static_cast<u64>(loc.title_id), path))) {
return ResultSuccess;
}
} R_END_TRY_CATCH;
strncpy(out_path, path, FS_MAX_PATH);
return ResultSuccess;
}
/* We just need to set this to any valid NCA path. Let's use the executable path. */
R_TRY(lrLrResolveProgramPath(&lr, static_cast<u64>(loc.title_id), path));
R_TRY(lrLrRedirectApplicationHtmlDocumentPath(&lr, static_cast<u64>(loc.title_id), path));
Result ContentManagement::ResolveContentPathForTidSid(char *out_path, Registration::TidSid *tid_sid) {
return ResolveContentPath(out_path, tid_sid->title_id, tid_sid->storage_id);
}
Result ContentManagement::RedirectContentPath(const char *path, u64 tid, FsStorageId sid) {
LrLocationResolver lr;
R_TRY(lrOpenLocationResolver(sid, &lr));
ON_SCOPE_EXIT { serviceClose(&lr.s); };
return lrLrRedirectProgramPath(&lr, tid, path);
}
Result ContentManagement::RedirectContentPathForTidSid(const char *path, Registration::TidSid *tid_sid) {
return RedirectContentPath(path, tid_sid->title_id, tid_sid->storage_id);
}
void ContentManagement::RedirectHtmlDocumentPathForHbl(u64 tid, FsStorageId sid) {
LrLocationResolver lr;
char path[FS_MAX_PATH] = {0};
/* Open resolver. */
if (R_FAILED(lrOpenLocationResolver(sid, &lr))) {
return;
return ResultSuccess;
}
/* Ensure close on exit. */
ON_SCOPE_EXIT { serviceClose(&lr.s); };
/* Only redirect the HTML document path if there is not one already. */
if (R_SUCCEEDED(lrLrResolveApplicationHtmlDocumentPath(&lr, tid, path))) {
return;
}
/* We just need to set this to any valid NCA path. Let's use the executable path. */
if (R_FAILED(lrLrResolveProgramPath(&lr, tid, path))) {
return;
}
lrLrRedirectApplicationHtmlDocumentPath(&lr, tid, path);
}
bool ContentManagement::HasCreatedTitle(u64 tid) {
return std::find(g_created_titles.begin(), g_created_titles.end(), tid) != g_created_titles.end();
}
void ContentManagement::SetCreatedTitle(u64 tid) {
if (!HasCreatedTitle(tid)) {
g_created_titles.push_back(tid);
}
}
static OverrideKey ParseOverrideKey(const char *value) {
OverrideKey cfg;
/* Parse on by default. */
if (value[0] == '!') {
cfg.override_by_default = true;
value++;
} else {
cfg.override_by_default = false;
}
/* Parse key combination. */
if (strcasecmp(value, "A") == 0) {
cfg.key_combination = KEY_A;
} else if (strcasecmp(value, "B") == 0) {
cfg.key_combination = KEY_B;
} else if (strcasecmp(value, "X") == 0) {
cfg.key_combination = KEY_X;
} else if (strcasecmp(value, "Y") == 0) {
cfg.key_combination = KEY_Y;
} else if (strcasecmp(value, "LS") == 0) {
cfg.key_combination = KEY_LSTICK;
} else if (strcasecmp(value, "RS") == 0) {
cfg.key_combination = KEY_RSTICK;
} else if (strcasecmp(value, "L") == 0) {
cfg.key_combination = KEY_L;
} else if (strcasecmp(value, "R") == 0) {
cfg.key_combination = KEY_R;
} else if (strcasecmp(value, "ZL") == 0) {
cfg.key_combination = KEY_ZL;
} else if (strcasecmp(value, "ZR") == 0) {
cfg.key_combination = KEY_ZR;
} else if (strcasecmp(value, "PLUS") == 0) {
cfg.key_combination = KEY_PLUS;
} else if (strcasecmp(value, "MINUS") == 0) {
cfg.key_combination = KEY_MINUS;
} else if (strcasecmp(value, "DLEFT") == 0) {
cfg.key_combination = KEY_DLEFT;
} else if (strcasecmp(value, "DUP") == 0) {
cfg.key_combination = KEY_DUP;
} else if (strcasecmp(value, "DRIGHT") == 0) {
cfg.key_combination = KEY_DRIGHT;
} else if (strcasecmp(value, "DDOWN") == 0) {
cfg.key_combination = KEY_DDOWN;
} else if (strcasecmp(value, "SL") == 0) {
cfg.key_combination = KEY_SL;
} else if (strcasecmp(value, "SR") == 0) {
cfg.key_combination = KEY_SR;
} else {
cfg.key_combination = 0;
}
return cfg;
}
static int LoaderIniHandler(void *user, const char *section, const char *name, const char *value) {
/* Taken and modified, with love, from Rajkosto's implementation. */
if (strcasecmp(section, "hbl_config") == 0) {
if (strcasecmp(name, "title_id") == 0) {
if (strcasecmp(value, "app") == 0) {
/* DEPRECATED */
g_hbl_override_config.override_any_app = true;
g_hbl_override_config.title_id = 0;
} else {
u64 override_tid = strtoul(value, NULL, 16);
if (override_tid != 0) {
g_hbl_override_config.title_id = override_tid;
}
}
} else if (strcasecmp(name, "path") == 0) {
while (*value == '/' || *value == '\\') {
value++;
}
snprintf(g_hbl_sd_path, FS_MAX_PATH, "@Sdcard:/%s", value);
g_hbl_sd_path[FS_MAX_PATH] = 0;
} else if (strcasecmp(name, "override_key") == 0) {
g_hbl_override_config.override_key = ParseOverrideKey(value);
} else if (strcasecmp(name, "override_any_app") == 0) {
if (strcasecmp(value, "true") == 0 || strcasecmp(value, "1") == 0) {
g_hbl_override_config.override_any_app = true;
} else if (strcasecmp(value, "false") == 0 || strcasecmp(value, "0") == 0) {
g_hbl_override_config.override_any_app = false;
} else {
/* I guess we default to not changing the value? */
}
}
} else if (strcasecmp(section, "default_config") == 0) {
if (strcasecmp(name, "override_key") == 0) {
g_default_override_key = ParseOverrideKey(value);
}
} else {
return 0;
}
return 1;
}
static int LoaderTitleSpecificIniHandler(void *user, const char *section, const char *name, const char *value) {
/* We'll output an override key when relevant. */
OverrideKey *user_cfg = reinterpret_cast<OverrideKey *>(user);
if (strcasecmp(section, "override_config") == 0) {
if (strcasecmp(name, "override_key") == 0) {
*user_cfg = ParseOverrideKey(value);
}
} else {
return 0;
}
return 1;
}
void ContentManagement::RefreshConfigurationData() {
FILE *config = fopen("sdmc:/atmosphere/loader.ini", "r");
if (config == NULL) {
return;
}
std::fill(g_config_ini_data, g_config_ini_data + 0x800, 0);
fread(g_config_ini_data, 1, 0x7FF, config);
fclose(config);
ini_parse_string(g_config_ini_data, LoaderIniHandler, NULL);
}
void ContentManagement::TryMountSdCard() {
/* Mount SD card, if psc, bus, and pcv have been created. */
if (!g_has_initialized_fs_dev && HasCreatedTitle(TitleId_Psc) && HasCreatedTitle(TitleId_Bus) && HasCreatedTitle(TitleId_Pcv)) {
bool can_mount = true;
DoWithSmSession([&]() {
Handle tmp_hnd = 0;
static const char * const required_active_services[] = {"pcv", "gpio", "pinmux", "psc:c"};
for (unsigned int i = 0; i < sizeof(required_active_services) / sizeof(required_active_services[0]); i++) {
if (R_FAILED(smGetServiceOriginal(&tmp_hnd, smEncodeName(required_active_services[i])))) {
can_mount = false;
break;
} else {
svcCloseHandle(tmp_hnd);
}
}
});
if (can_mount && R_SUCCEEDED(fsdevMountSdmc())) {
g_has_initialized_fs_dev = true;
}
}
}
static bool IsHBLTitleId(u64 tid) {
return ((g_hbl_override_config.override_any_app && TitleIdIsApplication(tid)) || (tid == g_hbl_override_config.title_id));
}
OverrideKey ContentManagement::GetTitleOverrideKey(u64 tid) {
OverrideKey cfg = g_default_override_key;
char path[FS_MAX_PATH+1] = {0};
snprintf(path, FS_MAX_PATH, "sdmc:/atmosphere/titles/%016lx/config.ini", tid);
FILE *config = fopen(path, "r");
if (config != NULL) {
ON_SCOPE_EXIT { fclose(config); };
/* Parse current title ini. */
ini_parse_file(config, LoaderTitleSpecificIniHandler, &cfg);
}
return cfg;
}
static bool ShouldOverrideContents(OverrideKey *cfg) {
u64 kDown = 0;
bool keys_triggered = (R_SUCCEEDED(HidManagement::GetKeysHeld(&kDown)) && ((kDown & cfg->key_combination) != 0));
return g_has_initialized_fs_dev && (cfg->override_by_default ^ keys_triggered);
}
bool ContentManagement::ShouldOverrideContentsWithHBL(u64 tid) {
if (g_mounted_hbl_nsp && tid >= TitleId_AppletStart && HasCreatedTitle(TitleId_AppletQlaunch)) {
/* Return whether we should override contents with HBL. */
return IsHBLTitleId(tid) && ShouldOverrideContents(&g_hbl_override_config.override_key);
} else {
/* Don't override if we failed to mount HBL or haven't launched qlaunch. */
return false;
}
}
bool ContentManagement::ShouldOverrideContentsWithSD(u64 tid) {
if (g_has_initialized_fs_dev) {
if (tid >= TitleId_AppletStart && HasCreatedTitle(TitleId_AppletQlaunch)) {
/* Check whether we should override with non-HBL. */
OverrideKey title_cfg = GetTitleOverrideKey(tid);
return ShouldOverrideContents(&title_cfg);
} else {
/* Always redirect before qlaunch. */
return true;
}
} else {
/* Never redirect before we can do so. */
return false;
}
}
/* SetExternalContentSource extension */
ContentManagement::ExternalContentSource *ContentManagement::GetExternalContentSource(u64 tid) {
auto i = g_external_content_sources.find(tid);
if (i == g_external_content_sources.end()) {
return nullptr;
} else {
return &i->second;
}
}
Result ContentManagement::SetExternalContentSource(u64 tid, FsFileSystem filesystem) {
if (g_external_content_sources.size() >= 16) {
return ResultLoaderTooManyArguments; /* TODO: Is this an appropriate error? */
}
/* Remove any existing ECS for this title. */
ClearExternalContentSource(tid);
char mountpoint[32];
ExternalContentSource::GenerateMountpointName(tid, mountpoint, sizeof(mountpoint));
if (fsdevMountDevice(mountpoint, filesystem) == -1) {
return ResultFsMountNameAlreadyExists;
}
g_external_content_sources.emplace(
std::piecewise_construct,
std::make_tuple(tid),
std::make_tuple(tid, mountpoint));
return ResultSuccess;
}
void ContentManagement::ClearExternalContentSource(u64 tid) {
auto i = g_external_content_sources.find(tid);
if (i != g_external_content_sources.end()) {
g_external_content_sources.erase(i);
}
}
void ContentManagement::ExternalContentSource::GenerateMountpointName(u64 tid, char *out, size_t max_length) {
snprintf(out, max_length, "ecs-%016lx", tid);
}
ContentManagement::ExternalContentSource::ExternalContentSource(u64 tid, const char *mountpoint) : tid(tid) {
strncpy(this->mountpoint, mountpoint, sizeof(this->mountpoint));
NpdmUtils::InvalidateCache(tid);
}
ContentManagement::ExternalContentSource::~ExternalContentSource() {
fsdevUnmountDevice(mountpoint);
}