2018-09-07 17:00:13 +02:00
|
|
|
/*
|
2020-01-24 11:10:40 +01:00
|
|
|
* Copyright (c) 2018-2020 Atmosphère-NX
|
2018-09-07 17:00:13 +02:00
|
|
|
*
|
|
|
|
* 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/>.
|
|
|
|
*/
|
2018-04-19 23:28:27 +02:00
|
|
|
#include "ldr_content_management.hpp"
|
2018-05-02 00:49:20 +02:00
|
|
|
|
2019-10-24 11:30:10 +02:00
|
|
|
namespace ams::ldr {
|
2019-02-23 16:17:33 +01:00
|
|
|
|
2019-06-27 00:46:19 +02:00
|
|
|
namespace {
|
2019-02-23 16:17:33 +01:00
|
|
|
|
2020-04-08 11:21:35 +02:00
|
|
|
os::Mutex g_scoped_code_mount_lock(false);
|
2018-05-02 00:49:20 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-06-28 02:37:33 +02:00
|
|
|
/* ScopedCodeMount functionality. */
|
2020-03-09 11:10:12 +01:00
|
|
|
ScopedCodeMount::ScopedCodeMount(const ncm::ProgramLocation &loc) : lk(g_scoped_code_mount_lock), has_status(false), mounted_ams(false), mounted_code(false) {
|
2019-11-21 13:03:19 +01:00
|
|
|
this->result = this->Initialize(loc);
|
|
|
|
}
|
|
|
|
|
2020-03-09 11:10:12 +01:00
|
|
|
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) {
|
2019-06-28 02:37:33 +02:00
|
|
|
this->result = this->Initialize(loc);
|
|
|
|
}
|
|
|
|
|
2019-06-27 00:46:19 +02:00
|
|
|
ScopedCodeMount::~ScopedCodeMount() {
|
2020-03-09 11:10:12 +01:00
|
|
|
/* Unmount filesystems. */
|
|
|
|
if (this->mounted_ams) {
|
|
|
|
fs::Unmount(AtmosphereCodeMountName);
|
2019-06-27 00:46:19 +02:00
|
|
|
}
|
2020-03-09 11:10:12 +01:00
|
|
|
if (this->mounted_code) {
|
|
|
|
fs::Unmount(CodeMountName);
|
2019-06-27 00:46:19 +02:00
|
|
|
}
|
2019-02-23 16:17:33 +01:00
|
|
|
}
|
2019-03-19 21:29:31 +01:00
|
|
|
|
2019-10-28 05:43:01 +01:00
|
|
|
Result ScopedCodeMount::Initialize(const ncm::ProgramLocation &loc) {
|
2019-11-21 13:03:19 +01:00
|
|
|
/* Capture override status, if necessary. */
|
2020-03-09 11:10:12 +01:00
|
|
|
this->EnsureOverrideStatus(loc);
|
|
|
|
AMS_ABORT_UNLESS(this->has_status);
|
2019-11-21 13:03:19 +01:00
|
|
|
|
2020-03-09 11:10:12 +01:00
|
|
|
/* 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));
|
2019-02-23 16:17:33 +01:00
|
|
|
}
|
|
|
|
|
2020-03-09 11:10:12 +01:00
|
|
|
/* 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;
|
|
|
|
|
|
|
|
/* Mount the base code file system. */
|
|
|
|
R_TRY(fs::MountCodeForAtmosphere(CodeMountName, content_path, loc.program_id));
|
|
|
|
this->mounted_code = true;
|
2018-09-20 01:21:46 +02:00
|
|
|
|
2019-10-24 10:40:44 +02:00
|
|
|
return ResultSuccess();
|
2018-09-20 01:21:46 +02:00
|
|
|
}
|
2019-03-19 21:29:31 +01:00
|
|
|
|
2020-03-09 11:10:12 +01:00
|
|
|
void ScopedCodeMount::EnsureOverrideStatus(const ncm::ProgramLocation &loc) {
|
|
|
|
if (this->has_status) {
|
|
|
|
return;
|
|
|
|
}
|
2019-11-21 13:03:19 +01:00
|
|
|
this->override_status = cfg::CaptureOverrideStatus(loc.program_id);
|
|
|
|
this->has_status = true;
|
|
|
|
}
|
|
|
|
|
2019-06-27 00:46:19 +02:00
|
|
|
/* Redirection API. */
|
2019-10-28 05:43:01 +01:00
|
|
|
Result ResolveContentPath(char *out_path, const ncm::ProgramLocation &loc) {
|
2020-03-08 09:06:23 +01:00
|
|
|
lr::Path path;
|
2019-02-23 16:17:33 +01:00
|
|
|
|
2019-06-27 00:46:19 +02:00
|
|
|
/* Try to get the path from the registered resolver. */
|
2020-03-08 09:06:23 +01:00
|
|
|
lr::RegisteredLocationResolver reg;
|
|
|
|
R_TRY(lr::OpenRegisteredLocationResolver(std::addressof(reg)));
|
2019-03-19 21:29:31 +01:00
|
|
|
|
2020-03-08 09:06:23 +01:00
|
|
|
R_TRY_CATCH(reg.ResolveProgramPath(std::addressof(path), loc.program_id)) {
|
2019-10-24 10:40:44 +02:00
|
|
|
R_CATCH(lr::ResultProgramNotFound) {
|
2019-06-27 00:46:19 +02:00
|
|
|
/* Program wasn't found via registered resolver, fall back to the normal resolver. */
|
2020-03-08 09:06:23 +01:00
|
|
|
lr::LocationResolver lr;
|
|
|
|
R_TRY(lr::OpenLocationResolver(std::addressof(lr), static_cast<ncm::StorageId>(loc.storage_id)));
|
|
|
|
R_TRY(lr.ResolveProgramPath(std::addressof(path), loc.program_id));
|
2019-06-27 00:46:19 +02:00
|
|
|
}
|
|
|
|
} R_END_TRY_CATCH;
|
2019-02-23 16:17:33 +01:00
|
|
|
|
2020-03-08 09:06:23 +01:00
|
|
|
std::strncpy(out_path, path.str, fs::EntryNameLengthMax);
|
|
|
|
out_path[fs::EntryNameLengthMax - 1] = '\0';
|
2020-03-09 11:10:12 +01:00
|
|
|
|
|
|
|
fs::Replace(out_path, fs::EntryNameLengthMax + 1, fs::StringTraits::AlternateDirectorySeparator, fs::StringTraits::DirectorySeparator);
|
|
|
|
|
2019-10-24 10:40:44 +02:00
|
|
|
return ResultSuccess();
|
2019-02-23 16:17:33 +01:00
|
|
|
}
|
|
|
|
|
2019-10-28 05:43:01 +01:00
|
|
|
Result RedirectContentPath(const char *path, const ncm::ProgramLocation &loc) {
|
2020-03-08 09:06:23 +01:00
|
|
|
/* Copy in path. */
|
|
|
|
lr::Path lr_path;
|
|
|
|
std::strncpy(lr_path.str, path, sizeof(lr_path.str));
|
|
|
|
lr_path.str[sizeof(lr_path.str) - 1] = '\0';
|
|
|
|
|
|
|
|
/* Redirect the path. */
|
|
|
|
lr::LocationResolver lr;
|
|
|
|
R_TRY(lr::OpenLocationResolver(std::addressof(lr), static_cast<ncm::StorageId>(loc.storage_id)));
|
|
|
|
lr.RedirectProgramPath(lr_path, loc.program_id);
|
2018-10-25 21:52:01 +02:00
|
|
|
|
2020-03-08 09:06:23 +01:00
|
|
|
return ResultSuccess();
|
2018-10-25 21:52:01 +02:00
|
|
|
}
|
|
|
|
|
2019-10-28 05:43:01 +01:00
|
|
|
Result RedirectHtmlDocumentPathForHbl(const ncm::ProgramLocation &loc) {
|
2020-03-08 09:06:23 +01:00
|
|
|
lr::Path path;
|
2018-10-25 21:52:01 +02:00
|
|
|
|
2019-06-28 02:37:33 +02:00
|
|
|
/* Open a location resolver. */
|
2020-03-08 09:06:23 +01:00
|
|
|
lr::LocationResolver lr;
|
|
|
|
R_TRY(lr::OpenLocationResolver(std::addressof(lr), static_cast<ncm::StorageId>(loc.storage_id)));
|
2018-10-25 21:52:01 +02:00
|
|
|
|
2019-06-27 00:46:19 +02:00
|
|
|
/* If there's already a Html Document path, we don't need to set one. */
|
2020-03-08 09:06:23 +01:00
|
|
|
R_SUCCEED_IF(R_SUCCEEDED(lr.ResolveApplicationHtmlDocumentPath(std::addressof(path), loc.program_id)));
|
2018-10-25 21:52:01 +02:00
|
|
|
|
2019-06-27 00:46:19 +02:00
|
|
|
/* We just need to set this to any valid NCA path. Let's use the executable path. */
|
2020-03-08 09:06:23 +01:00
|
|
|
R_TRY(lr.ResolveProgramPath(std::addressof(path), loc.program_id));
|
|
|
|
lr.RedirectApplicationHtmlDocumentPath(path, loc.program_id, loc.program_id);
|
2018-10-25 21:52:01 +02:00
|
|
|
|
2019-10-24 10:40:44 +02:00
|
|
|
return ResultSuccess();
|
2018-10-25 21:52:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|