2018-04-19 05:15:17 +00:00
|
|
|
#include <switch.h>
|
2018-06-12 22:00:09 +00:00
|
|
|
#include <stratosphere.hpp>
|
2018-04-19 05:15:17 +00:00
|
|
|
#include "ldr_process_manager.hpp"
|
|
|
|
#include "ldr_registration.hpp"
|
|
|
|
#include "ldr_launch_queue.hpp"
|
2018-04-21 05:58:42 +00:00
|
|
|
#include "ldr_content_management.hpp"
|
|
|
|
#include "ldr_npdm.hpp"
|
2018-04-19 05:15:17 +00:00
|
|
|
|
2018-04-21 01:34:29 +00:00
|
|
|
Result ProcessManagerService::dispatch(IpcParsedCommand &r, IpcCommand &out_c, u64 cmd_id, u8 *pointer_buffer, size_t pointer_buffer_size) {
|
2018-04-19 05:15:17 +00:00
|
|
|
Result rc = 0xF601;
|
2018-06-12 22:00:09 +00:00
|
|
|
|
2018-04-19 05:15:17 +00:00
|
|
|
switch ((ProcessManagerServiceCmd)cmd_id) {
|
|
|
|
case Pm_Cmd_CreateProcess:
|
2018-04-21 01:34:29 +00:00
|
|
|
rc = WrapIpcCommandImpl<&ProcessManagerService::create_process>(this, r, out_c, pointer_buffer, pointer_buffer_size);
|
2018-04-19 05:15:17 +00:00
|
|
|
break;
|
|
|
|
case Pm_Cmd_GetProgramInfo:
|
2018-04-21 01:34:29 +00:00
|
|
|
rc = WrapIpcCommandImpl<&ProcessManagerService::get_program_info>(this, r, out_c, pointer_buffer, pointer_buffer_size);
|
2018-04-19 05:15:17 +00:00
|
|
|
break;
|
2018-04-21 01:34:29 +00:00
|
|
|
case Pm_Cmd_RegisterTitle:
|
|
|
|
rc = WrapIpcCommandImpl<&ProcessManagerService::register_title>(this, r, out_c, pointer_buffer, pointer_buffer_size);
|
2018-04-19 05:15:17 +00:00
|
|
|
break;
|
|
|
|
case Pm_Cmd_UnregisterTitle:
|
2018-04-21 01:34:29 +00:00
|
|
|
rc = WrapIpcCommandImpl<&ProcessManagerService::unregister_title>(this, r, out_c, pointer_buffer, pointer_buffer_size);
|
2018-04-19 05:15:17 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2018-04-22 01:52:49 +00:00
|
|
|
std::tuple<Result, MovedHandle> ProcessManagerService::create_process(u64 flags, u64 index, CopiedHandle reslimit_h) {
|
|
|
|
Result rc;
|
|
|
|
Registration::TidSid tid_sid;
|
|
|
|
LaunchQueue::LaunchItem *launch_item;
|
|
|
|
char nca_path[FS_MAX_PATH] = {0};
|
|
|
|
Handle process_h = 0;
|
|
|
|
|
|
|
|
fprintf(stderr, "CreateProcess(%016lx, %016lx, %08x);\n", flags, index, reslimit_h.handle);
|
|
|
|
|
2018-04-26 23:03:10 +00:00
|
|
|
rc = Registration::GetRegisteredTidSid(index, &tid_sid);
|
2018-04-22 01:52:49 +00:00
|
|
|
if (R_FAILED(rc)) {
|
2018-05-05 18:41:39 +00:00
|
|
|
return {rc, MovedHandle{process_h}};
|
2018-04-22 01:52:49 +00:00
|
|
|
}
|
|
|
|
|
2018-05-08 08:59:18 +00:00
|
|
|
rc = ContentManagement::ResolveContentPathForTidSid(nca_path, &tid_sid);
|
2018-04-22 01:52:49 +00:00
|
|
|
if (R_FAILED(rc)) {
|
2018-05-05 18:41:39 +00:00
|
|
|
return {rc, MovedHandle{process_h}};
|
2018-04-22 01:52:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
launch_item = LaunchQueue::get_item(tid_sid.title_id);
|
|
|
|
|
|
|
|
rc = ProcessCreation::CreateProcess(&process_h, index, nca_path, launch_item, flags, reslimit_h.handle);
|
|
|
|
|
2018-05-01 22:49:20 +00:00
|
|
|
if (R_SUCCEEDED(rc)) {
|
|
|
|
ContentManagement::SetCreatedTitle(tid_sid.title_id);
|
|
|
|
}
|
|
|
|
|
2018-05-05 18:41:39 +00:00
|
|
|
return {rc, MovedHandle{process_h}};
|
2018-04-19 05:15:17 +00:00
|
|
|
}
|
|
|
|
|
2018-04-21 03:38:51 +00:00
|
|
|
std::tuple<Result> ProcessManagerService::get_program_info(Registration::TidSid tid_sid, OutPointerWithServerSize<ProcessManagerService::ProgramInfo, 0x1> out_program_info) {
|
2018-04-21 05:58:42 +00:00
|
|
|
Result rc;
|
|
|
|
char nca_path[FS_MAX_PATH] = {0};
|
2018-04-21 03:38:51 +00:00
|
|
|
/* Zero output. */
|
|
|
|
std::fill(out_program_info.pointer, out_program_info.pointer + out_program_info.num_elements, (const ProcessManagerService::ProgramInfo){0});
|
|
|
|
|
2018-04-21 05:58:42 +00:00
|
|
|
rc = populate_program_info_buffer(out_program_info.pointer, &tid_sid);
|
|
|
|
|
|
|
|
if (R_FAILED(rc)) {
|
2018-05-05 18:41:39 +00:00
|
|
|
return {rc};
|
2018-04-21 05:58:42 +00:00
|
|
|
}
|
|
|
|
|
2018-05-01 22:49:20 +00:00
|
|
|
if (tid_sid.title_id != out_program_info.pointer->title_id) {
|
2018-05-08 08:59:18 +00:00
|
|
|
rc = ContentManagement::ResolveContentPathForTidSid(nca_path, &tid_sid);
|
2018-04-21 05:58:42 +00:00
|
|
|
if (R_FAILED(rc)) {
|
2018-05-05 18:41:39 +00:00
|
|
|
return {rc};
|
2018-04-21 05:58:42 +00:00
|
|
|
}
|
|
|
|
|
2018-05-08 08:59:18 +00:00
|
|
|
rc = ContentManagement::RedirectContentPath(nca_path, out_program_info.pointer->title_id, tid_sid.storage_id);
|
2018-04-21 05:58:42 +00:00
|
|
|
if (R_FAILED(rc)) {
|
2018-05-05 18:41:39 +00:00
|
|
|
return {rc};
|
2018-04-21 05:58:42 +00:00
|
|
|
}
|
|
|
|
|
2018-05-01 22:49:20 +00:00
|
|
|
rc = LaunchQueue::add_copy(tid_sid.title_id, out_program_info.pointer->title_id);
|
2018-04-21 05:58:42 +00:00
|
|
|
}
|
2018-05-01 22:49:20 +00:00
|
|
|
|
2018-05-05 18:41:39 +00:00
|
|
|
return {rc};
|
2018-04-19 05:15:17 +00:00
|
|
|
}
|
|
|
|
|
2018-04-21 01:34:29 +00:00
|
|
|
std::tuple<Result, u64> ProcessManagerService::register_title(Registration::TidSid tid_sid) {
|
|
|
|
u64 out_index = 0;
|
2018-04-26 23:03:10 +00:00
|
|
|
if (Registration::RegisterTidSid(&tid_sid, &out_index)) {
|
2018-05-05 18:41:39 +00:00
|
|
|
return {0, out_index};
|
2018-04-19 05:15:17 +00:00
|
|
|
} else {
|
2018-05-05 18:41:39 +00:00
|
|
|
return {0xE09, out_index};
|
2018-04-19 05:15:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-21 01:34:29 +00:00
|
|
|
std::tuple<Result> ProcessManagerService::unregister_title(u64 index) {
|
2018-04-26 23:03:10 +00:00
|
|
|
if (Registration::UnregisterIndex(index)) {
|
2018-05-05 18:41:39 +00:00
|
|
|
return {0};
|
2018-04-19 05:15:17 +00:00
|
|
|
} else {
|
2018-05-05 18:41:39 +00:00
|
|
|
return {0x1009};
|
2018-04-19 05:15:17 +00:00
|
|
|
}
|
2018-04-21 05:58:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Result ProcessManagerService::populate_program_info_buffer(ProcessManagerService::ProgramInfo *out, Registration::TidSid *tid_sid) {
|
|
|
|
NpdmUtils::NpdmInfo info;
|
|
|
|
Result rc;
|
|
|
|
|
|
|
|
rc = ContentManagement::MountCodeForTidSid(tid_sid);
|
|
|
|
if (R_FAILED(rc)) {
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
rc = NpdmUtils::LoadNpdm(tid_sid->title_id, &info);
|
|
|
|
if (R_FAILED(rc)) {
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
ContentManagement::UnmountCode();
|
|
|
|
|
|
|
|
out->main_thread_priority = info.header->main_thread_prio;
|
|
|
|
out->default_cpu_id = info.header->default_cpuid;
|
|
|
|
out->main_thread_stack_size = info.header->main_stack_size;
|
2018-05-01 22:49:20 +00:00
|
|
|
out->title_id = info.aci0->title_id;
|
2018-04-21 05:58:42 +00:00
|
|
|
|
|
|
|
out->acid_fac_size = info.acid->fac_size;
|
|
|
|
out->aci0_sac_size = info.aci0->sac_size;
|
|
|
|
out->aci0_fah_size = info.aci0->fah_size;
|
|
|
|
|
|
|
|
size_t offset = 0;
|
|
|
|
rc = 0x19009;
|
|
|
|
if (offset + info.acid->sac_size < sizeof(out->ac_buffer)) {
|
|
|
|
out->acid_sac_size = info.acid->sac_size;
|
|
|
|
std::memcpy(out->ac_buffer + offset, info.acid_sac, out->acid_sac_size);
|
|
|
|
offset += out->acid_sac_size;
|
|
|
|
if (offset + info.aci0->sac_size < sizeof(out->ac_buffer)) {
|
|
|
|
out->aci0_sac_size = info.aci0->sac_size;
|
|
|
|
std::memcpy(out->ac_buffer + offset, info.aci0_sac, out->aci0_sac_size);
|
|
|
|
offset += out->aci0_sac_size;
|
|
|
|
if (offset + info.acid->fac_size < sizeof(out->ac_buffer)) {
|
|
|
|
out->acid_fac_size = info.acid->fac_size;
|
|
|
|
std::memcpy(out->ac_buffer + offset, info.acid_fac, out->acid_fac_size);
|
|
|
|
offset += out->acid_fac_size;
|
|
|
|
if (offset + info.aci0->fah_size < sizeof(out->ac_buffer)) {
|
|
|
|
out->aci0_fah_size = info.aci0->fah_size;
|
|
|
|
std::memcpy(out->ac_buffer + offset, info.aci0_fah, out->aci0_fah_size);
|
|
|
|
offset += out->aci0_fah_size;
|
|
|
|
rc = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Parse application type. */
|
|
|
|
if (R_SUCCEEDED(rc)) {
|
2018-04-24 08:08:11 +00:00
|
|
|
out->application_type = NpdmUtils::GetApplicationType((u32 *)info.acid_kac, info.acid->kac_size / sizeof(u32));
|
2018-04-21 05:58:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return rc;
|
2018-05-05 18:41:39 +00:00
|
|
|
}
|