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:00:10 +00:00
|
|
|
#include <switch.h>
|
|
|
|
#include <algorithm>
|
|
|
|
#include <cstdio>
|
2018-04-27 09:17:07 +00:00
|
|
|
#include <cstring>
|
2018-06-19 18:07:31 +00:00
|
|
|
#include <functional>
|
2018-04-19 05:00:10 +00:00
|
|
|
#include "ldr_registration.hpp"
|
|
|
|
|
2019-04-05 04:05:41 +00:00
|
|
|
static Registration::List g_registration_list = {};
|
2018-04-19 05:00:10 +00:00
|
|
|
static u64 g_num_registered = 1;
|
|
|
|
|
2018-04-26 23:03:10 +00:00
|
|
|
Registration::Process *Registration::GetFreeProcess() {
|
2018-06-19 18:07:31 +00:00
|
|
|
auto process_it = std::find_if_not(g_registration_list.processes.begin(), g_registration_list.processes.end(), std::mem_fn(&Registration::Process::in_use));
|
|
|
|
if (process_it == g_registration_list.processes.end()) {
|
|
|
|
return nullptr;
|
2018-04-19 05:00:10 +00:00
|
|
|
}
|
2018-06-19 18:07:31 +00:00
|
|
|
return &*process_it;
|
2018-04-19 05:00:10 +00:00
|
|
|
}
|
|
|
|
|
2018-04-26 23:03:10 +00:00
|
|
|
Registration::Process *Registration::GetProcess(u64 index) {
|
2019-04-21 01:15:39 +00:00
|
|
|
for (unsigned int i = 0; i < Registration::MaxProcesses; i++) {
|
2018-04-27 02:50:27 +00:00
|
|
|
if (g_registration_list.processes[i].in_use && g_registration_list.processes[i].index == index) {
|
|
|
|
return &g_registration_list.processes[i];
|
|
|
|
}
|
2018-04-19 05:00:10 +00:00
|
|
|
}
|
2018-04-27 02:50:27 +00:00
|
|
|
return NULL;
|
2018-04-19 05:00:10 +00:00
|
|
|
}
|
|
|
|
|
2018-04-26 23:03:10 +00:00
|
|
|
Registration::Process *Registration::GetProcessByProcessId(u64 pid) {
|
2019-04-21 01:15:39 +00:00
|
|
|
for (unsigned int i = 0; i < Registration::MaxProcesses; i++) {
|
2018-04-27 02:50:27 +00:00
|
|
|
if (g_registration_list.processes[i].in_use && g_registration_list.processes[i].process_id == pid) {
|
|
|
|
return &g_registration_list.processes[i];
|
|
|
|
}
|
2018-04-21 01:34:29 +00:00
|
|
|
}
|
2018-04-27 02:50:27 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2019-06-17 23:29:09 +00:00
|
|
|
bool Registration::RegisterTidSid(const TidSid *tid_sid, u64 *out_index) {
|
2018-04-26 23:03:10 +00:00
|
|
|
Registration::Process *free_process = GetFreeProcess();
|
2018-04-19 05:00:10 +00:00
|
|
|
if (free_process == NULL) {
|
|
|
|
return false;
|
|
|
|
}
|
2019-06-17 23:29:09 +00:00
|
|
|
|
2018-04-19 05:00:10 +00:00
|
|
|
/* Reset the process. */
|
2019-04-05 04:05:41 +00:00
|
|
|
*free_process = {};
|
2018-04-19 05:00:10 +00:00
|
|
|
free_process->tid_sid = *tid_sid;
|
|
|
|
free_process->in_use = true;
|
|
|
|
free_process->index = g_num_registered++;
|
|
|
|
*out_index = free_process->index;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-04-26 23:03:10 +00:00
|
|
|
bool Registration::UnregisterIndex(u64 index) {
|
|
|
|
Registration::Process *target_process = GetProcess(index);
|
2018-04-19 05:00:10 +00:00
|
|
|
if (target_process == NULL) {
|
|
|
|
return false;
|
|
|
|
}
|
2019-06-17 23:29:09 +00:00
|
|
|
|
2018-04-19 05:00:10 +00:00
|
|
|
/* Reset the process. */
|
2019-04-05 04:05:41 +00:00
|
|
|
*target_process = {};
|
2018-04-19 05:00:10 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-04-22 01:52:49 +00:00
|
|
|
|
2018-04-26 23:03:10 +00:00
|
|
|
Result Registration::GetRegisteredTidSid(u64 index, Registration::TidSid *out) {
|
|
|
|
Registration::Process *target_process = GetProcess(index);
|
2018-04-22 01:52:49 +00:00
|
|
|
if (target_process == NULL) {
|
2019-03-28 22:06:50 +00:00
|
|
|
return ResultLoaderProcessNotRegistered;
|
2018-04-22 01:52:49 +00:00
|
|
|
}
|
2019-06-17 23:29:09 +00:00
|
|
|
|
2018-04-22 01:52:49 +00:00
|
|
|
*out = target_process->tid_sid;
|
2019-06-17 23:29:09 +00:00
|
|
|
|
2019-03-29 05:39:39 +00:00
|
|
|
return ResultSuccess;
|
2018-04-22 01:52:49 +00:00
|
|
|
}
|
|
|
|
|
2018-05-01 22:49:20 +00:00
|
|
|
void Registration::SetProcessIdTidAndIs64BitAddressSpace(u64 index, u64 process_id, u64 tid, bool is_64_bit_addspace) {
|
2018-04-26 23:03:10 +00:00
|
|
|
Registration::Process *target_process = GetProcess(index);
|
2018-04-19 05:00:10 +00:00
|
|
|
if (target_process == NULL) {
|
|
|
|
return;
|
|
|
|
}
|
2019-06-17 23:29:09 +00:00
|
|
|
|
2018-04-19 05:00:10 +00:00
|
|
|
target_process->process_id = process_id;
|
2018-05-01 22:49:20 +00:00
|
|
|
target_process->title_id = tid;
|
2018-04-26 20:53:33 +00:00
|
|
|
target_process->is_64_bit_addspace = is_64_bit_addspace;
|
2018-04-19 05:00:10 +00:00
|
|
|
}
|
|
|
|
|
2019-04-21 01:15:39 +00:00
|
|
|
void Registration::AddModuleInfo(u64 index, u64 base_address, u64 size, const unsigned char *build_id) {
|
2018-04-26 23:03:10 +00:00
|
|
|
Registration::Process *target_process = GetProcess(index);
|
2018-04-19 05:00:10 +00:00
|
|
|
if (target_process == NULL) {
|
|
|
|
return;
|
|
|
|
}
|
2018-06-19 18:07:31 +00:00
|
|
|
|
2019-04-21 01:15:39 +00:00
|
|
|
auto nso_info_it = std::find_if_not(target_process->module_infos.begin(), target_process->module_infos.end(), std::mem_fn(&Registration::ModuleInfoHolder::in_use));
|
|
|
|
if (nso_info_it != target_process->module_infos.end()) {
|
2018-06-19 18:07:31 +00:00
|
|
|
nso_info_it->info.base_address = base_address;
|
|
|
|
nso_info_it->info.size = size;
|
2019-04-21 01:15:39 +00:00
|
|
|
memcpy(nso_info_it->info.build_id, build_id, sizeof(nso_info_it->info.build_id));
|
2018-06-19 18:07:31 +00:00
|
|
|
nso_info_it->in_use = true;
|
2018-04-19 05:00:10 +00:00
|
|
|
}
|
2018-04-19 22:14:48 +00:00
|
|
|
}
|
|
|
|
|
2019-04-21 01:15:39 +00:00
|
|
|
Result Registration::GetProcessModuleInfo(LoaderModuleInfo *out, u32 max_out, u64 process_id, u32 *num_written) {
|
2018-04-26 23:03:10 +00:00
|
|
|
Registration::Process *target_process = GetProcessByProcessId(process_id);
|
2018-04-19 22:14:48 +00:00
|
|
|
if (target_process == NULL) {
|
2019-03-28 22:06:50 +00:00
|
|
|
return ResultLoaderProcessNotRegistered;
|
2018-04-19 22:14:48 +00:00
|
|
|
}
|
|
|
|
u32 cur = 0;
|
2019-06-17 23:29:09 +00:00
|
|
|
|
2019-04-21 01:15:39 +00:00
|
|
|
for (unsigned int i = 0; i < Registration::MaxModuleInfos && cur < max_out; i++) {
|
|
|
|
if (target_process->module_infos[i].in_use) {
|
|
|
|
out[cur++] = target_process->module_infos[i].info;
|
2018-04-19 22:14:48 +00:00
|
|
|
}
|
|
|
|
}
|
2019-06-17 23:29:09 +00:00
|
|
|
|
2018-04-19 22:14:48 +00:00
|
|
|
*num_written = cur;
|
|
|
|
|
2019-03-29 05:39:39 +00:00
|
|
|
return ResultSuccess;
|
2018-04-19 22:14:48 +00:00
|
|
|
}
|
2018-06-14 23:50:01 +00:00
|
|
|
|
|
|
|
void Registration::AssociatePidTidForMitM(u64 index) {
|
|
|
|
Registration::Process *target_process = GetProcess(index);
|
|
|
|
if (target_process == NULL) {
|
|
|
|
return;
|
|
|
|
}
|
2019-06-17 23:29:09 +00:00
|
|
|
|
2018-06-14 23:50:01 +00:00
|
|
|
Handle sm_hnd;
|
|
|
|
Result rc = svcConnectToNamedPort(&sm_hnd, "sm:");
|
|
|
|
if (R_SUCCEEDED(rc)) {
|
|
|
|
/* Initialize. */
|
|
|
|
{
|
|
|
|
IpcCommand c;
|
|
|
|
ipcInitialize(&c);
|
|
|
|
ipcSendPid(&c);
|
|
|
|
|
|
|
|
struct {
|
|
|
|
u64 magic;
|
|
|
|
u64 cmd_id;
|
|
|
|
u64 zero;
|
|
|
|
u64 reserved[2];
|
|
|
|
} *raw = (decltype(raw))ipcPrepareHeader(&c, sizeof(*raw));
|
|
|
|
|
|
|
|
raw->magic = SFCI_MAGIC;
|
|
|
|
raw->cmd_id = 0;
|
|
|
|
raw->zero = 0;
|
|
|
|
|
|
|
|
rc = ipcDispatch(sm_hnd);
|
|
|
|
|
|
|
|
if (R_SUCCEEDED(rc)) {
|
|
|
|
IpcParsedCommand r;
|
|
|
|
ipcParse(&r);
|
|
|
|
|
|
|
|
struct {
|
|
|
|
u64 magic;
|
|
|
|
u64 result;
|
|
|
|
} *resp = (decltype(resp))r.Raw;
|
|
|
|
|
|
|
|
rc = resp->result;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* Associate. */
|
|
|
|
if (R_SUCCEEDED(rc)) {
|
|
|
|
IpcCommand c;
|
|
|
|
ipcInitialize(&c);
|
|
|
|
struct {
|
|
|
|
u64 magic;
|
|
|
|
u64 cmd_id;
|
|
|
|
u64 process_id;
|
|
|
|
u64 title_id;
|
|
|
|
} *raw = (decltype(raw))ipcPrepareHeader(&c, sizeof(*raw));
|
2019-06-17 23:29:09 +00:00
|
|
|
|
2018-06-14 23:50:01 +00:00
|
|
|
raw->magic = SFCI_MAGIC;
|
|
|
|
raw->cmd_id = 65002;
|
|
|
|
raw->process_id = target_process->process_id;
|
|
|
|
raw->title_id = target_process->tid_sid.title_id;
|
2019-06-17 23:29:09 +00:00
|
|
|
|
2018-06-14 23:50:01 +00:00
|
|
|
ipcDispatch(sm_hnd);
|
|
|
|
}
|
|
|
|
svcCloseHandle(sm_hnd);
|
|
|
|
}
|
|
|
|
}
|