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