2019-06-26 23:46:19 +01:00
|
|
|
/*
|
2021-10-04 20:59:10 +01:00
|
|
|
* Copyright (c) Atmosphère-NX
|
2019-06-26 23:46:19 +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>
|
2021-10-11 05:37:29 +01:00
|
|
|
#include "ldr_argument_store.hpp"
|
2019-06-26 23:46:19 +01:00
|
|
|
#include "ldr_content_management.hpp"
|
2020-04-14 10:45:28 +01:00
|
|
|
#include "ldr_development_manager.hpp"
|
2019-06-26 23:46:19 +01:00
|
|
|
#include "ldr_process_creation.hpp"
|
|
|
|
#include "ldr_launch_record.hpp"
|
|
|
|
#include "ldr_loader_service.hpp"
|
|
|
|
|
2019-10-24 10:30:10 +01:00
|
|
|
namespace ams::ldr {
|
2019-06-26 23:46:19 +01:00
|
|
|
|
2019-11-21 12:03:19 +00:00
|
|
|
namespace {
|
|
|
|
|
2021-10-11 05:37:29 +01:00
|
|
|
constinit ArgumentStore g_argument_store;
|
2019-11-21 12:03:19 +00:00
|
|
|
|
2021-10-11 05:37:29 +01:00
|
|
|
}
|
2019-11-21 12:03:19 +00:00
|
|
|
|
2021-10-07 07:22:54 +01:00
|
|
|
|
2021-10-11 05:37:29 +01:00
|
|
|
Result LoaderService::CreateProcess(os::NativeHandle *out, PinId pin_id, u32 flags, os::NativeHandle resource_limit) {
|
|
|
|
/* Declare program path, which we'll need later. */
|
2019-11-21 12:03:19 +00:00
|
|
|
|
2021-10-11 05:37:29 +01:00
|
|
|
/* Get the location and override status. */
|
|
|
|
ncm::ProgramLocation loc;
|
|
|
|
cfg::OverrideStatus override_status;
|
|
|
|
R_TRY(ldr::GetProgramLocationAndOverrideStatusFromPinId(std::addressof(loc), std::addressof(override_status), pin_id));
|
2019-11-21 12:03:19 +00:00
|
|
|
|
2021-10-11 05:37:29 +01:00
|
|
|
/* Get the program path. */
|
|
|
|
char path[fs::EntryNameLengthMax];
|
|
|
|
R_TRY(GetProgramPath(path, sizeof(path), loc));
|
|
|
|
path[sizeof(path) - 1] = '\x00';
|
2019-11-21 12:03:19 +00:00
|
|
|
|
2021-10-11 05:37:29 +01:00
|
|
|
/* Create the process. */
|
2022-03-26 21:48:33 +00:00
|
|
|
R_RETURN(ldr::CreateProcess(out, pin_id, loc, override_status, path, g_argument_store.Get(loc.program_id), flags, resource_limit));
|
2019-11-21 12:03:19 +00:00
|
|
|
}
|
|
|
|
|
2021-10-11 05:37:29 +01:00
|
|
|
Result LoaderService::GetProgramInfo(ProgramInfo *out, cfg::OverrideStatus *out_status, const ncm::ProgramLocation &loc) {
|
|
|
|
/* Zero output. */
|
|
|
|
std::memset(out, 0, sizeof(*out));
|
|
|
|
|
|
|
|
/* Get the program path. */
|
2021-10-07 07:22:54 +01:00
|
|
|
char path[fs::EntryNameLengthMax];
|
2021-10-11 05:37:29 +01:00
|
|
|
R_TRY(GetProgramPath(path, sizeof(path), loc));
|
|
|
|
path[sizeof(path) - 1] = '\x00';
|
2019-06-26 23:46:19 +01:00
|
|
|
|
2021-10-11 05:37:29 +01:00
|
|
|
/* Get the program info. */
|
|
|
|
cfg::OverrideStatus status;
|
|
|
|
R_TRY(ldr::GetProgramInfo(out, std::addressof(status), loc, path));
|
2019-06-26 23:46:19 +01:00
|
|
|
|
2021-10-11 05:37:29 +01:00
|
|
|
if (loc.program_id != out->program_id) {
|
|
|
|
/* Redirect the program path. */
|
|
|
|
const ncm::ProgramLocation new_loc = ncm::ProgramLocation::Make(out->program_id, static_cast<ncm::StorageId>(loc.storage_id));
|
|
|
|
R_TRY(RedirectProgramPath(path, sizeof(path), new_loc));
|
|
|
|
|
|
|
|
/* Update the arguments, as needed. */
|
|
|
|
if (const auto *entry = g_argument_store.Get(loc.program_id); entry != nullptr) {
|
|
|
|
R_TRY(this->SetProgramArgument(new_loc.program_id, entry->argument, entry->argument_size));
|
|
|
|
}
|
2019-06-26 23:46:19 +01:00
|
|
|
}
|
|
|
|
|
2021-10-11 05:37:29 +01:00
|
|
|
/* If we should, set the output status. */
|
|
|
|
if (out_status != nullptr) {
|
|
|
|
*out_status = status;
|
|
|
|
}
|
2021-10-05 08:11:36 +01:00
|
|
|
|
2022-03-26 07:14:36 +00:00
|
|
|
R_SUCCEED();
|
2019-06-26 23:46:19 +01:00
|
|
|
}
|
|
|
|
|
2021-10-11 05:37:29 +01:00
|
|
|
Result LoaderService::PinProgram(PinId *out, const ncm::ProgramLocation &loc, const cfg::OverrideStatus &status) {
|
|
|
|
*out = {};
|
2022-03-26 21:48:33 +00:00
|
|
|
R_RETURN(ldr::PinProgram(out, loc, status));
|
2019-06-26 23:46:19 +01:00
|
|
|
}
|
|
|
|
|
2019-10-28 04:43:01 +00:00
|
|
|
Result LoaderService::UnpinProgram(PinId id) {
|
2022-03-26 21:48:33 +00:00
|
|
|
R_RETURN(ldr::UnpinProgram(id));
|
2019-06-26 23:46:19 +01:00
|
|
|
}
|
|
|
|
|
2021-10-11 05:37:29 +01:00
|
|
|
Result LoaderService::SetProgramArgument(ncm::ProgramId program_id, const void *argument, size_t size) {
|
2022-03-26 21:48:33 +00:00
|
|
|
R_RETURN(g_argument_store.Set(program_id, argument, size));
|
2020-12-01 06:06:52 +00:00
|
|
|
}
|
|
|
|
|
2019-10-28 04:43:01 +00:00
|
|
|
Result LoaderService::FlushArguments() {
|
2022-03-26 21:48:33 +00:00
|
|
|
R_RETURN(g_argument_store.Flush());
|
2019-06-26 23:46:19 +01:00
|
|
|
}
|
|
|
|
|
2021-10-11 05:37:29 +01:00
|
|
|
Result LoaderService::GetProcessModuleInfo(u32 *out_count, ModuleInfo *out, size_t max_out_count, os::ProcessId process_id) {
|
|
|
|
*out_count = 0;
|
|
|
|
std::memset(out, 0, max_out_count * sizeof(*out));
|
2022-03-26 21:48:33 +00:00
|
|
|
R_RETURN(ldr::GetProcessModuleInfo(out_count, out, max_out_count, process_id));
|
2020-04-14 10:45:28 +01:00
|
|
|
}
|
|
|
|
|
2021-10-11 05:37:29 +01:00
|
|
|
Result LoaderService::RegisterExternalCode(os::NativeHandle *out, ncm::ProgramId program_id) {
|
2022-03-26 21:48:33 +00:00
|
|
|
R_RETURN(fssystem::CreateExternalCode(out, program_id));
|
2019-06-26 23:46:19 +01:00
|
|
|
}
|
|
|
|
|
2021-10-11 05:37:29 +01:00
|
|
|
void LoaderService::UnregisterExternalCode(ncm::ProgramId program_id) {
|
2020-03-09 10:10:12 +00:00
|
|
|
fssystem::DestroyExternalCode(program_id);
|
2019-06-26 23:46:19 +01:00
|
|
|
}
|
|
|
|
|
2021-10-11 05:37:29 +01:00
|
|
|
void LoaderService::HasLaunchedBootProgram(bool *out, ncm::ProgramId program_id) {
|
|
|
|
*out = ldr::HasLaunchedBootProgram(program_id);
|
2019-11-21 12:03:19 +00:00
|
|
|
}
|
|
|
|
|
2021-10-11 05:37:29 +01:00
|
|
|
Result LoaderService::SetEnabledProgramVerification(bool enabled) {
|
|
|
|
ldr::SetEnabledProgramVerification(enabled);
|
2022-03-26 07:14:36 +00:00
|
|
|
R_SUCCEED();
|
2019-11-21 12:03:19 +00:00
|
|
|
}
|
|
|
|
|
2019-06-26 23:46:19 +01:00
|
|
|
}
|