2018-09-07 15:00:13 +00:00
|
|
|
/*
|
2019-04-08 02:00:49 +00:00
|
|
|
* Copyright (c) 2018-2019 Atmosphère-NX
|
2018-09-07 15:00:13 +00: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/>.
|
|
|
|
*/
|
2019-06-17 23:29:09 +00:00
|
|
|
|
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-10-30 13:29:30 +00:00
|
|
|
Result ProcessManagerService::CreateProcess(Out<MovedHandle> proc_h, u64 index, u32 flags, CopiedHandle reslimit_h) {
|
2018-04-22 01:52:49 +00:00
|
|
|
Registration::TidSid tid_sid;
|
|
|
|
LaunchQueue::LaunchItem *launch_item;
|
|
|
|
char nca_path[FS_MAX_PATH] = {0};
|
2019-04-21 16:08:08 +00:00
|
|
|
|
2018-11-09 00:02:04 +00:00
|
|
|
ON_SCOPE_EXIT {
|
|
|
|
/* Loader doesn't persist the copied resource limit handle. */
|
|
|
|
svcCloseHandle(reslimit_h.handle);
|
|
|
|
};
|
2019-06-17 23:29:09 +00:00
|
|
|
|
|
|
|
R_TRY(Registration::GetRegisteredTidSid(index, &tid_sid));
|
|
|
|
|
2018-07-28 02:53:20 +00:00
|
|
|
if (tid_sid.storage_id != FsStorageId_None) {
|
2019-06-17 23:29:09 +00:00
|
|
|
R_TRY(ContentManagement::ResolveContentPathForTidSid(nca_path, &tid_sid));
|
2018-04-22 01:52:49 +00:00
|
|
|
}
|
2018-07-28 02:53:20 +00:00
|
|
|
|
2018-10-30 05:33:56 +00:00
|
|
|
launch_item = LaunchQueue::GetItem(tid_sid.title_id);
|
2019-06-17 23:29:09 +00:00
|
|
|
R_TRY(ProcessCreation::CreateProcess(proc_h.GetHandlePointer(), index, nca_path, launch_item, flags, reslimit_h.handle));
|
|
|
|
|
|
|
|
ContentManagement::SetCreatedTitle(tid_sid.title_id);
|
|
|
|
return ResultSuccess;
|
2018-04-19 05:15:17 +00:00
|
|
|
}
|
|
|
|
|
2018-10-30 13:29:30 +00:00
|
|
|
Result ProcessManagerService::GetProgramInfo(OutPointerWithServerSize<ProcessManagerService::ProgramInfo, 0x1> out_program_info, Registration::TidSid tid_sid) {
|
2018-04-21 05:58:42 +00:00
|
|
|
char nca_path[FS_MAX_PATH] = {0};
|
2019-06-17 23:29:09 +00:00
|
|
|
|
2018-04-21 03:38:51 +00:00
|
|
|
/* Zero output. */
|
2019-04-05 04:05:41 +00:00
|
|
|
std::fill(out_program_info.pointer, out_program_info.pointer + out_program_info.num_elements, ProcessManagerService::ProgramInfo{});
|
2019-06-17 23:29:09 +00:00
|
|
|
|
|
|
|
R_TRY(PopulateProgramInfoBuffer(out_program_info.pointer, &tid_sid));
|
|
|
|
|
2018-07-28 02:53:20 +00:00
|
|
|
if (tid_sid.storage_id != FsStorageId_None && tid_sid.title_id != out_program_info.pointer->title_id) {
|
2019-06-17 23:29:09 +00:00
|
|
|
R_TRY(ContentManagement::ResolveContentPathForTidSid(nca_path, &tid_sid));
|
|
|
|
R_TRY(ContentManagement::RedirectContentPath(nca_path, out_program_info.pointer->title_id, tid_sid.storage_id));
|
|
|
|
R_TRY(LaunchQueue::AddCopy(tid_sid.title_id, out_program_info.pointer->title_id));
|
2018-04-21 05:58:42 +00:00
|
|
|
}
|
2019-06-17 23:29:09 +00:00
|
|
|
|
|
|
|
return ResultSuccess;
|
2018-04-19 05:15:17 +00:00
|
|
|
}
|
|
|
|
|
2018-10-30 05:33:56 +00:00
|
|
|
Result ProcessManagerService::RegisterTitle(Out<u64> index, Registration::TidSid tid_sid) {
|
2019-03-28 22:06:50 +00:00
|
|
|
return Registration::RegisterTidSid(&tid_sid, index.GetPointer()) ? 0 : ResultLoaderTooManyProcesses;
|
2018-04-19 05:15:17 +00:00
|
|
|
}
|
|
|
|
|
2018-10-30 05:33:56 +00:00
|
|
|
Result ProcessManagerService::UnregisterTitle(u64 index) {
|
2019-03-28 22:06:50 +00:00
|
|
|
return Registration::UnregisterIndex(index) ? 0 : ResultLoaderProcessNotRegistered;
|
2018-04-21 05:58:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-10-30 05:33:56 +00:00
|
|
|
Result ProcessManagerService::PopulateProgramInfoBuffer(ProcessManagerService::ProgramInfo *out, Registration::TidSid *tid_sid) {
|
2018-04-21 05:58:42 +00:00
|
|
|
NpdmUtils::NpdmInfo info;
|
2019-06-17 23:29:09 +00:00
|
|
|
|
|
|
|
/* Mount code, load NPDM. */
|
|
|
|
{
|
|
|
|
bool mounted_code = false;
|
|
|
|
if (tid_sid->storage_id != FsStorageId_None) {
|
|
|
|
R_TRY(ContentManagement::MountCodeForTidSid(tid_sid));
|
|
|
|
mounted_code = true;
|
|
|
|
} else if (R_SUCCEEDED(ContentManagement::MountCodeNspOnSd(tid_sid->title_id))) {
|
|
|
|
mounted_code = true;
|
2018-07-28 02:53:20 +00:00
|
|
|
}
|
2019-06-17 23:29:09 +00:00
|
|
|
ON_SCOPE_EXIT {
|
|
|
|
if (mounted_code) {
|
|
|
|
ContentManagement::UnmountCode();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
R_TRY(NpdmUtils::LoadNpdm(tid_sid->title_id, &info));
|
2018-04-21 05:58:42 +00:00
|
|
|
}
|
2018-07-28 02:53:20 +00:00
|
|
|
|
2018-04-21 05:58:42 +00:00
|
|
|
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;
|
2019-06-17 23:29:09 +00:00
|
|
|
|
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;
|
2019-06-17 23:29:09 +00:00
|
|
|
|
2018-04-21 05:58:42 +00:00
|
|
|
size_t offset = 0;
|
2019-06-17 23:29:09 +00:00
|
|
|
|
|
|
|
/* Copy ACID Service Access Control. */
|
|
|
|
if (offset + info.acid->sac_size >= sizeof(out->ac_buffer)) {
|
|
|
|
return ResultLoaderInternalError;
|
2018-04-21 05:58:42 +00:00
|
|
|
}
|
2019-06-17 23:29:09 +00:00
|
|
|
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;
|
|
|
|
|
|
|
|
/* Copy ACI0 Service Access Control. */
|
|
|
|
if (offset + info.aci0->sac_size >= sizeof(out->ac_buffer)) {
|
|
|
|
return ResultLoaderInternalError;
|
|
|
|
}
|
|
|
|
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;
|
|
|
|
|
|
|
|
/* Copy ACID Filesystem Access Control. */
|
|
|
|
if (offset + info.acid->fac_size >= sizeof(out->ac_buffer)) {
|
|
|
|
return ResultLoaderInternalError;
|
|
|
|
}
|
|
|
|
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;
|
|
|
|
|
|
|
|
/* Copy ACI0 Filesystem Access Header. */
|
|
|
|
if (offset + info.aci0->fah_size >= sizeof(out->ac_buffer)) {
|
|
|
|
return ResultLoaderInternalError;
|
2018-04-21 05:58:42 +00:00
|
|
|
}
|
2019-06-17 23:29:09 +00:00
|
|
|
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;
|
|
|
|
|
|
|
|
/* Parse application type. */
|
|
|
|
out->application_type = NpdmUtils::GetApplicationType(reinterpret_cast<const u32 *>(info.acid_kac), info.acid->kac_size / sizeof(u32));
|
|
|
|
|
|
|
|
return ResultSuccess;
|
2018-05-05 18:41:39 +00:00
|
|
|
}
|