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>
|
2019-06-26 23:46:19 +01:00
|
|
|
#include "ldr_arguments.hpp"
|
|
|
|
#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 {
|
|
|
|
|
|
|
|
Result GetProgramInfoImpl(ProgramInfo *out, cfg::OverrideStatus *out_status, const ncm::ProgramLocation &loc) {
|
|
|
|
/* Zero output. */
|
|
|
|
std::memset(out, 0, sizeof(*out));
|
|
|
|
cfg::OverrideStatus status = {};
|
|
|
|
|
2021-10-09 22:49:53 +01:00
|
|
|
R_TRY(ldr::GetProgramInfo(out, std::addressof(status), loc));
|
2019-11-21 12:03:19 +00:00
|
|
|
|
|
|
|
if (loc.storage_id != static_cast<u8>(ncm::StorageId::None) && loc.program_id != out->program_id) {
|
2021-10-07 07:22:54 +01:00
|
|
|
char path[fs::EntryNameLengthMax];
|
2019-11-21 12:03:19 +00:00
|
|
|
const ncm::ProgramLocation new_loc = ncm::ProgramLocation::Make(out->program_id, static_cast<ncm::StorageId>(loc.storage_id));
|
|
|
|
|
|
|
|
R_TRY(ResolveContentPath(path, loc));
|
2021-10-07 07:22:54 +01:00
|
|
|
path[sizeof(path) - 1] = '\x00';
|
|
|
|
|
2019-11-21 12:03:19 +00:00
|
|
|
R_TRY(RedirectContentPath(path, new_loc));
|
|
|
|
|
|
|
|
const auto arg_info = args::Get(loc.program_id);
|
|
|
|
if (arg_info != nullptr) {
|
|
|
|
R_TRY(args::Set(new_loc.program_id, arg_info->args, arg_info->args_size));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (out_status != nullptr) {
|
|
|
|
*out_status = status;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ResultSuccess();
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-06-26 23:46:19 +01:00
|
|
|
/* Official commands. */
|
2021-10-05 08:11:36 +01:00
|
|
|
Result LoaderService::CreateProcess(sf::OutMoveHandle out, PinId id, u32 flags, sf::CopyHandle &&reslimit_h) {
|
2019-10-28 04:43:01 +00:00
|
|
|
ncm::ProgramLocation loc;
|
2019-11-21 12:03:19 +00:00
|
|
|
cfg::OverrideStatus override_status;
|
2021-10-07 07:22:54 +01:00
|
|
|
char path[fs::EntryNameLengthMax];
|
2019-06-26 23:46:19 +01:00
|
|
|
|
2019-11-21 12:03:19 +00:00
|
|
|
/* Get location and override status. */
|
2021-10-11 02:22:32 +01:00
|
|
|
R_TRY(ldr::GetProgramLocationAndOverrideStatusFromPinId(std::addressof(loc), std::addressof(override_status), id));
|
2019-06-26 23:46:19 +01:00
|
|
|
|
|
|
|
if (loc.storage_id != static_cast<u8>(ncm::StorageId::None)) {
|
|
|
|
R_TRY(ResolveContentPath(path, loc));
|
2021-10-07 07:22:54 +01:00
|
|
|
path[sizeof(path) - 1] = '\x00';
|
|
|
|
} else {
|
|
|
|
path[0] = '\x00';
|
2019-06-26 23:46:19 +01:00
|
|
|
}
|
|
|
|
|
2021-10-05 08:11:36 +01:00
|
|
|
/* Create the process. */
|
|
|
|
os::NativeHandle process_handle;
|
|
|
|
R_TRY(ldr::CreateProcess(std::addressof(process_handle), id, loc, override_status, path, flags, reslimit_h.GetOsHandle()));
|
|
|
|
|
|
|
|
/* Set output process handle. */
|
|
|
|
out.SetValue(process_handle, true);
|
|
|
|
return ResultSuccess();
|
2019-06-26 23:46:19 +01:00
|
|
|
}
|
|
|
|
|
2019-10-28 04:43:01 +00:00
|
|
|
Result LoaderService::GetProgramInfo(sf::Out<ProgramInfo> out, const ncm::ProgramLocation &loc) {
|
2019-11-21 12:03:19 +00:00
|
|
|
return GetProgramInfoImpl(out.GetPointer(), nullptr, loc);
|
2019-06-26 23:46:19 +01:00
|
|
|
}
|
|
|
|
|
2019-10-28 04:43:01 +00:00
|
|
|
Result LoaderService::PinProgram(sf::Out<PinId> out_id, const ncm::ProgramLocation &loc) {
|
2021-10-11 02:22:32 +01:00
|
|
|
return ldr::PinProgram(out_id.GetPointer(), loc, cfg::OverrideStatus{});
|
2019-06-26 23:46:19 +01:00
|
|
|
}
|
|
|
|
|
2019-10-28 04:43:01 +00:00
|
|
|
Result LoaderService::UnpinProgram(PinId id) {
|
2021-10-11 02:22:32 +01:00
|
|
|
return ldr::UnpinProgram(id);
|
2019-06-26 23:46:19 +01:00
|
|
|
}
|
|
|
|
|
2020-12-01 06:06:52 +00:00
|
|
|
Result LoaderService::SetProgramArgumentsDeprecated(ncm::ProgramId program_id, const sf::InPointerBuffer &args, u32 args_size) {
|
2019-10-28 04:43:01 +00:00
|
|
|
return args::Set(program_id, args.GetPointer(), std::min(args.GetSize(), size_t(args_size)));
|
2019-06-26 23:46:19 +01:00
|
|
|
}
|
|
|
|
|
2020-12-01 06:06:52 +00:00
|
|
|
Result LoaderService::SetProgramArguments(ncm::ProgramId program_id, const sf::InPointerBuffer &args) {
|
|
|
|
return args::Set(program_id, args.GetPointer(), args.GetSize());
|
|
|
|
}
|
|
|
|
|
2019-10-28 04:43:01 +00:00
|
|
|
Result LoaderService::FlushArguments() {
|
|
|
|
return args::Flush();
|
2019-06-26 23:46:19 +01:00
|
|
|
}
|
|
|
|
|
2019-10-15 05:40:05 +01:00
|
|
|
Result LoaderService::GetProcessModuleInfo(sf::Out<u32> count, const sf::OutPointerArray<ModuleInfo> &out, os::ProcessId process_id) {
|
2021-10-11 02:22:32 +01:00
|
|
|
*count = 0;
|
|
|
|
std::memset(out.GetPointer(), 0, out.GetSize() * sizeof(ldr::ModuleInfo));
|
|
|
|
return ldr::GetProcessModuleInfo(count.GetPointer(), out.GetPointer(), out.GetSize(), process_id);
|
2019-06-26 23:46:19 +01:00
|
|
|
}
|
|
|
|
|
2020-04-14 10:45:28 +01:00
|
|
|
Result LoaderService::SetEnabledProgramVerification(bool enabled) {
|
|
|
|
ldr::SetEnabledProgramVerification(enabled);
|
2020-04-14 11:20:23 +01:00
|
|
|
return ResultSuccess();
|
2020-04-14 10:45:28 +01:00
|
|
|
}
|
|
|
|
|
2019-06-26 23:46:19 +01:00
|
|
|
/* Atmosphere commands. */
|
2020-03-09 10:10:12 +00:00
|
|
|
Result LoaderService::AtmosphereRegisterExternalCode(sf::OutMoveHandle out, ncm::ProgramId program_id) {
|
2021-10-05 08:11:36 +01:00
|
|
|
os::NativeHandle handle;
|
|
|
|
R_TRY(fssystem::CreateExternalCode(std::addressof(handle), program_id));
|
|
|
|
|
|
|
|
out.SetValue(handle, true);
|
|
|
|
return ResultSuccess();
|
2019-06-26 23:46:19 +01:00
|
|
|
}
|
|
|
|
|
2020-03-09 10:10:12 +00:00
|
|
|
void LoaderService::AtmosphereUnregisterExternalCode(ncm::ProgramId program_id) {
|
|
|
|
fssystem::DestroyExternalCode(program_id);
|
2019-06-26 23:46:19 +01:00
|
|
|
}
|
|
|
|
|
2021-01-18 06:03:26 +00:00
|
|
|
void LoaderService::AtmosphereHasLaunchedBootProgram(sf::Out<bool> out, ncm::ProgramId program_id) {
|
|
|
|
out.SetValue(ldr::HasLaunchedBootProgram(program_id));
|
2019-06-26 23:46:19 +01:00
|
|
|
}
|
|
|
|
|
2019-11-21 12:03:19 +00:00
|
|
|
Result LoaderService::AtmosphereGetProgramInfo(sf::Out<ProgramInfo> out_program_info, sf::Out<cfg::OverrideStatus> out_status, const ncm::ProgramLocation &loc) {
|
|
|
|
return GetProgramInfoImpl(out_program_info.GetPointer(), out_status.GetPointer(), loc);
|
|
|
|
}
|
|
|
|
|
|
|
|
Result LoaderService::AtmospherePinProgram(sf::Out<PinId> out_id, const ncm::ProgramLocation &loc, const cfg::OverrideStatus &override_status) {
|
2021-10-11 02:22:32 +01:00
|
|
|
return ldr::PinProgram(out_id.GetPointer(), loc, override_status);
|
2019-11-21 12:03:19 +00:00
|
|
|
}
|
|
|
|
|
2019-06-26 23:46:19 +01:00
|
|
|
}
|