2018-09-07 16:00:13 +01:00
|
|
|
/*
|
2021-10-04 20:59:10 +01:00
|
|
|
* Copyright (c) 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
|
|
|
|
2021-09-30 06:52:50 +01:00
|
|
|
constinit os::SdkMutex g_scoped_code_mount_lock;
|
2018-05-01 23:49:20 +01:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-06-28 01:37:33 +01:00
|
|
|
/* ScopedCodeMount functionality. */
|
2024-10-30 02:24:52 +00:00
|
|
|
ScopedCodeMount::ScopedCodeMount(const ncm::ProgramLocation &loc, ncm::ContentMetaPlatform platform) : m_lk(g_scoped_code_mount_lock), m_has_status(false), m_mounted_ams(false), m_mounted_sd_or_code(false), m_mounted_code(false) {
|
2024-05-27 23:43:58 +01:00
|
|
|
m_result = this->Initialize(loc, platform);
|
2019-11-21 12:03:19 +00:00
|
|
|
}
|
|
|
|
|
2024-10-30 02:24:52 +00:00
|
|
|
ScopedCodeMount::ScopedCodeMount(const ncm::ProgramLocation &loc, const cfg::OverrideStatus &o, ncm::ContentMetaPlatform platform) : m_lk(g_scoped_code_mount_lock), m_override_status(o), m_has_status(true), m_mounted_ams(false), m_mounted_sd_or_code(false), m_mounted_code(false) {
|
2024-05-27 23:43:58 +01:00
|
|
|
m_result = this->Initialize(loc, platform);
|
2019-06-28 01:37:33 +01:00
|
|
|
}
|
|
|
|
|
2019-06-26 23:46:19 +01:00
|
|
|
ScopedCodeMount::~ScopedCodeMount() {
|
2020-03-09 10:10:12 +00:00
|
|
|
/* Unmount filesystems. */
|
2021-10-10 08:14:06 +01:00
|
|
|
if (m_mounted_ams) {
|
2020-03-09 10:10:12 +00:00
|
|
|
fs::Unmount(AtmosphereCodeMountName);
|
2019-06-26 23:46:19 +01:00
|
|
|
}
|
2021-10-10 08:14:06 +01:00
|
|
|
if (m_mounted_sd_or_code) {
|
2020-04-14 10:45:28 +01:00
|
|
|
fs::Unmount(SdOrCodeMountName);
|
|
|
|
}
|
2021-10-10 08:14:06 +01:00
|
|
|
if (m_mounted_code) {
|
2020-03-09 10:10:12 +00:00
|
|
|
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
|
|
|
|
2024-10-30 02:24:52 +00:00
|
|
|
Result ScopedCodeMount::Initialize(const ncm::ProgramLocation &loc, ncm::ContentMetaPlatform platform) {
|
2019-11-21 12:03:19 +00:00
|
|
|
/* Capture override status, if necessary. */
|
2020-03-09 10:10:12 +00:00
|
|
|
this->EnsureOverrideStatus(loc);
|
2021-10-10 08:14:06 +01:00
|
|
|
AMS_ABORT_UNLESS(m_has_status);
|
2019-11-21 12:03:19 +00:00
|
|
|
|
2020-03-09 10:10:12 +00:00
|
|
|
/* Get the content path. */
|
2021-10-11 05:37:29 +01:00
|
|
|
char content_path[fs::EntryNameLengthMax + 1];
|
2024-05-27 23:43:58 +01:00
|
|
|
R_TRY(GetProgramPath(content_path, sizeof(content_path), loc, platform));
|
|
|
|
|
|
|
|
/* Get the content attributes. */
|
|
|
|
const auto content_attributes = GetPlatformContentAttributes(platform);
|
2019-02-23 15:17:33 +00:00
|
|
|
|
2020-03-09 10:10:12 +00:00
|
|
|
/* Mount the atmosphere code file system. */
|
2024-05-27 23:43:58 +01:00
|
|
|
R_TRY(fs::MountCodeForAtmosphereWithRedirection(std::addressof(m_ams_code_verification_data), AtmosphereCodeMountName, content_path, content_attributes, loc.program_id, m_override_status.IsHbl(), m_override_status.IsProgramSpecific()));
|
2021-10-10 08:14:06 +01:00
|
|
|
m_mounted_ams = true;
|
2020-03-09 10:10:12 +00:00
|
|
|
|
2020-04-14 10:45:28 +01:00
|
|
|
/* Mount the sd or base code file system. */
|
2024-05-27 23:43:58 +01:00
|
|
|
R_TRY(fs::MountCodeForAtmosphere(std::addressof(m_sd_or_base_code_verification_data), SdOrCodeMountName, content_path, content_attributes, loc.program_id));
|
2021-10-10 08:14:06 +01:00
|
|
|
m_mounted_sd_or_code = true;
|
2020-04-14 10:45:28 +01:00
|
|
|
|
2020-03-09 10:10:12 +00:00
|
|
|
/* Mount the base code file system. */
|
2024-05-27 23:43:58 +01:00
|
|
|
if (R_SUCCEEDED(fs::MountCode(std::addressof(m_base_code_verification_data), CodeMountName, content_path, content_attributes, loc.program_id))) {
|
2021-10-10 08:14:06 +01:00
|
|
|
m_mounted_code = true;
|
2020-04-14 10:45:28 +01:00
|
|
|
}
|
2018-09-20 00:21:46 +01:00
|
|
|
|
2022-03-26 07:14:36 +00:00
|
|
|
R_SUCCEED();
|
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) {
|
2021-10-10 08:14:06 +01:00
|
|
|
if (m_has_status) {
|
2020-03-09 10:10:12 +00:00
|
|
|
return;
|
|
|
|
}
|
2021-10-10 08:14:06 +01:00
|
|
|
m_override_status = cfg::CaptureOverrideStatus(loc.program_id);
|
|
|
|
m_has_status = true;
|
2019-11-21 12:03:19 +00:00
|
|
|
}
|
|
|
|
|
2019-06-26 23:46:19 +01:00
|
|
|
/* Redirection API. */
|
2024-10-30 02:24:52 +00:00
|
|
|
Result GetProgramPath(char *out_path, size_t out_size, const ncm::ProgramLocation &loc, ncm::ContentMetaPlatform platform) {
|
2021-10-11 05:37:29 +01:00
|
|
|
/* Check for storage id none. */
|
|
|
|
if (static_cast<ncm::StorageId>(loc.storage_id) == ncm::StorageId::None) {
|
|
|
|
std::memset(out_path, 0, out_size);
|
|
|
|
std::memcpy(out_path, "/", std::min<size_t>(out_size, 2));
|
2022-03-26 07:14:36 +00:00
|
|
|
R_SUCCEED();
|
2021-10-11 05:37:29 +01:00
|
|
|
}
|
|
|
|
|
2024-05-27 23:43:58 +01:00
|
|
|
/* Get the content attributes. */
|
|
|
|
const auto content_attributes = GetPlatformContentAttributes(platform);
|
|
|
|
AMS_UNUSED(content_attributes);
|
|
|
|
|
2020-03-08 08:06:23 +00:00
|
|
|
lr::Path path;
|
2019-02-23 15:17:33 +00:00
|
|
|
|
2021-09-19 18:24:32 +01:00
|
|
|
/* Check that path registration is allowable. */
|
2021-09-19 18:42:28 +01:00
|
|
|
if (static_cast<ncm::StorageId>(loc.storage_id) == ncm::StorageId::Host) {
|
2021-09-19 18:24:32 +01:00
|
|
|
AMS_ABORT_UNLESS(spl::IsDevelopment());
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2021-10-11 05:37:29 +01:00
|
|
|
/* Fix directory separators in path. */
|
|
|
|
fs::Replace(path.str, sizeof(path.str), fs::StringTraits::AlternateDirectorySeparator, fs::StringTraits::DirectorySeparator);
|
2020-03-09 10:10:12 +00:00
|
|
|
|
2021-10-11 05:37:29 +01:00
|
|
|
/* Check that the path is valid. */
|
|
|
|
AMS_ABORT_UNLESS(path.IsValid());
|
|
|
|
|
|
|
|
/* Copy the output path. */
|
|
|
|
std::memset(out_path, 0, out_size);
|
|
|
|
std::memcpy(out_path, path.str, std::min(out_size, sizeof(path)));
|
2020-03-09 10:10:12 +00:00
|
|
|
|
2022-03-26 07:14:36 +00:00
|
|
|
R_SUCCEED();
|
2019-02-23 15:17:33 +00:00
|
|
|
}
|
|
|
|
|
2021-10-11 05:37:29 +01:00
|
|
|
Result RedirectProgramPath(const char *path, size_t size, const ncm::ProgramLocation &loc) {
|
|
|
|
/* Check for storage id none. */
|
|
|
|
if (static_cast<ncm::StorageId>(loc.storage_id) == ncm::StorageId::None) {
|
2022-03-26 07:14:36 +00:00
|
|
|
R_SUCCEED();
|
2021-10-11 05:37:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Open location resolver. */
|
|
|
|
lr::LocationResolver lr;
|
|
|
|
R_TRY(lr::OpenLocationResolver(std::addressof(lr), static_cast<ncm::StorageId>(loc.storage_id)));
|
|
|
|
|
2020-03-08 08:06:23 +00:00
|
|
|
/* Copy in path. */
|
|
|
|
lr::Path lr_path;
|
2021-10-11 05:37:29 +01:00
|
|
|
std::memcpy(lr_path.str, path, std::min(size, sizeof(lr_path.str)));
|
|
|
|
lr_path.str[sizeof(lr_path.str) - 1] = '\x00';
|
2020-03-08 08:06:23 +00:00
|
|
|
|
|
|
|
/* Redirect the path. */
|
|
|
|
lr.RedirectProgramPath(lr_path, loc.program_id);
|
2018-10-25 20:52:01 +01:00
|
|
|
|
2022-03-26 07:14:36 +00:00
|
|
|
R_SUCCEED();
|
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
|
|
|
|
2022-03-26 07:14:36 +00:00
|
|
|
R_SUCCEED();
|
2018-10-25 20:52:01 +01:00
|
|
|
}
|
|
|
|
|
2024-10-30 02:24:52 +00:00
|
|
|
fs::ContentAttributes GetPlatformContentAttributes(ncm::ContentMetaPlatform platform) {
|
2024-05-27 23:43:58 +01:00
|
|
|
switch (platform) {
|
2024-10-30 02:24:52 +00:00
|
|
|
case ncm::ContentMetaPlatform::Nx:
|
2024-05-27 23:43:58 +01:00
|
|
|
return fs::ContentAttributes_None;
|
|
|
|
AMS_UNREACHABLE_DEFAULT_CASE();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-25 20:52:01 +01:00
|
|
|
}
|