2019-06-27 00:46:19 +02:00
|
|
|
/*
|
2020-01-24 11:10:40 +01:00
|
|
|
* Copyright (c) 2018-2020 Atmosphère-NX
|
2019-06-27 00:46:19 +02: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-12 00:02:10 +02:00
|
|
|
#include <stratosphere.hpp>
|
2019-06-27 00:46:19 +02:00
|
|
|
#include "ldr_ro_manager.hpp"
|
|
|
|
|
2019-10-24 11:30:10 +02:00
|
|
|
namespace ams::ldr::ro {
|
2019-06-27 00:46:19 +02:00
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
/* Convenience definitions. */
|
|
|
|
constexpr PinId InvalidPinId = {};
|
|
|
|
constexpr size_t ProcessCountMax = 0x40;
|
|
|
|
constexpr size_t ModuleCountMax = 0x20;
|
|
|
|
|
|
|
|
/* Types. */
|
|
|
|
struct ModuleInfo {
|
|
|
|
ldr::ModuleInfo info;
|
|
|
|
bool in_use;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct ProcessInfo {
|
|
|
|
PinId pin_id;
|
2019-10-15 06:40:05 +02:00
|
|
|
os::ProcessId process_id;
|
2019-10-28 05:43:01 +01:00
|
|
|
ncm::ProgramId program_id;
|
2019-11-21 13:03:19 +01:00
|
|
|
cfg::OverrideStatus override_status;
|
2019-10-28 05:43:01 +01:00
|
|
|
ncm::ProgramLocation loc;
|
2019-06-27 00:46:19 +02:00
|
|
|
ModuleInfo modules[ModuleCountMax];
|
|
|
|
bool in_use;
|
|
|
|
};
|
|
|
|
|
|
|
|
/* Globals. */
|
|
|
|
ProcessInfo g_process_infos[ProcessCountMax];
|
2020-02-23 08:16:53 +01:00
|
|
|
u64 g_cur_pin_id = 1;
|
2019-06-27 00:46:19 +02:00
|
|
|
|
|
|
|
/* Helpers. */
|
|
|
|
ProcessInfo *GetProcessInfo(PinId pin_id) {
|
|
|
|
for (size_t i = 0; i < ProcessCountMax; i++) {
|
|
|
|
ProcessInfo *info = &g_process_infos[i];
|
|
|
|
if (info->in_use && info->pin_id == pin_id) {
|
|
|
|
return info;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2019-10-15 06:40:05 +02:00
|
|
|
ProcessInfo *GetProcessInfo(os::ProcessId process_id) {
|
2019-06-27 00:46:19 +02:00
|
|
|
for (size_t i = 0; i < ProcessCountMax; i++) {
|
|
|
|
ProcessInfo *info = &g_process_infos[i];
|
|
|
|
if (info->in_use && info->process_id == process_id) {
|
|
|
|
return info;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
ProcessInfo *GetFreeProcessInfo() {
|
|
|
|
for (size_t i = 0; i < ProcessCountMax; i++) {
|
|
|
|
ProcessInfo *info = &g_process_infos[i];
|
|
|
|
if (!info->in_use) {
|
|
|
|
return info;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/* RO Manager API. */
|
2019-11-21 13:03:19 +01:00
|
|
|
Result PinProgram(PinId *out, const ncm::ProgramLocation &loc, const cfg::OverrideStatus &status) {
|
2019-06-27 00:46:19 +02:00
|
|
|
*out = InvalidPinId;
|
|
|
|
ProcessInfo *info = GetFreeProcessInfo();
|
2019-10-24 10:40:44 +02:00
|
|
|
R_UNLESS(info != nullptr, ldr::ResultTooManyProcesses());
|
2019-06-27 00:46:19 +02:00
|
|
|
|
|
|
|
std::memset(info, 0, sizeof(*info));
|
2020-02-23 08:16:53 +01:00
|
|
|
info->pin_id = { g_cur_pin_id++ };
|
2019-06-27 00:46:19 +02:00
|
|
|
info->loc = loc;
|
2019-11-21 13:03:19 +01:00
|
|
|
info->override_status = status;
|
2019-06-27 00:46:19 +02:00
|
|
|
info->in_use = true;
|
|
|
|
*out = info->pin_id;
|
2019-10-24 10:40:44 +02:00
|
|
|
return ResultSuccess();
|
2019-06-27 00:46:19 +02:00
|
|
|
}
|
|
|
|
|
2019-10-28 05:43:01 +01:00
|
|
|
Result UnpinProgram(PinId id) {
|
2019-06-27 00:46:19 +02:00
|
|
|
ProcessInfo *info = GetProcessInfo(id);
|
2019-10-24 10:40:44 +02:00
|
|
|
R_UNLESS(info != nullptr, ldr::ResultNotPinned());
|
2019-06-27 00:46:19 +02:00
|
|
|
|
|
|
|
info->in_use = false;
|
2019-10-24 10:40:44 +02:00
|
|
|
return ResultSuccess();
|
2019-06-27 00:46:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-11-21 13:03:19 +01:00
|
|
|
Result GetProgramLocationAndStatus(ncm::ProgramLocation *out, cfg::OverrideStatus *out_status, PinId id) {
|
2019-06-27 00:46:19 +02:00
|
|
|
ProcessInfo *info = GetProcessInfo(id);
|
2019-10-24 10:40:44 +02:00
|
|
|
R_UNLESS(info != nullptr, ldr::ResultNotPinned());
|
2019-06-27 00:46:19 +02:00
|
|
|
|
|
|
|
*out = info->loc;
|
2019-11-21 13:03:19 +01:00
|
|
|
*out_status = info->override_status;
|
2019-10-24 10:40:44 +02:00
|
|
|
return ResultSuccess();
|
2019-06-27 00:46:19 +02:00
|
|
|
}
|
|
|
|
|
2019-10-28 05:43:01 +01:00
|
|
|
Result RegisterProcess(PinId id, os::ProcessId process_id, ncm::ProgramId program_id) {
|
2019-06-27 00:46:19 +02:00
|
|
|
ProcessInfo *info = GetProcessInfo(id);
|
2019-10-24 10:40:44 +02:00
|
|
|
R_UNLESS(info != nullptr, ldr::ResultNotPinned());
|
2019-06-27 00:46:19 +02:00
|
|
|
|
2019-10-28 05:43:01 +01:00
|
|
|
info->program_id = program_id;
|
2019-06-27 00:46:19 +02:00
|
|
|
info->process_id = process_id;
|
2019-10-24 10:40:44 +02:00
|
|
|
return ResultSuccess();
|
2019-06-27 00:46:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
Result RegisterModule(PinId id, const u8 *build_id, uintptr_t address, size_t size) {
|
|
|
|
ProcessInfo *info = GetProcessInfo(id);
|
2019-10-24 10:40:44 +02:00
|
|
|
R_UNLESS(info != nullptr, ldr::ResultNotPinned());
|
2019-06-27 00:46:19 +02:00
|
|
|
|
|
|
|
/* Nintendo doesn't actually care about successful allocation. */
|
|
|
|
for (size_t i = 0; i < ModuleCountMax; i++) {
|
|
|
|
ModuleInfo *module = &info->modules[i];
|
2019-06-30 19:48:16 +02:00
|
|
|
if (module->in_use) {
|
2019-06-27 00:46:19 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::memcpy(module->info.build_id, build_id, sizeof(module->info.build_id));
|
|
|
|
module->info.base_address = address;
|
|
|
|
module->info.size = size;
|
2019-06-30 19:48:16 +02:00
|
|
|
module->in_use = true;
|
2019-06-27 00:46:19 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2019-10-24 10:40:44 +02:00
|
|
|
return ResultSuccess();
|
2019-06-27 00:46:19 +02:00
|
|
|
}
|
|
|
|
|
2019-10-15 06:40:05 +02:00
|
|
|
Result GetProcessModuleInfo(u32 *out_count, ldr::ModuleInfo *out, size_t max_out_count, os::ProcessId process_id) {
|
2019-06-27 00:46:19 +02:00
|
|
|
const ProcessInfo *info = GetProcessInfo(process_id);
|
2019-10-24 10:40:44 +02:00
|
|
|
R_UNLESS(info != nullptr, ldr::ResultNotPinned());
|
2019-06-27 00:46:19 +02:00
|
|
|
|
|
|
|
size_t count = 0;
|
|
|
|
for (size_t i = 0; i < ModuleCountMax && count < max_out_count; i++) {
|
|
|
|
const ModuleInfo *module = &info->modules[i];
|
|
|
|
if (!module->in_use) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
out[count++] = module->info;
|
|
|
|
}
|
|
|
|
|
|
|
|
*out_count = static_cast<u32>(count);
|
2019-10-24 10:40:44 +02:00
|
|
|
return ResultSuccess();
|
2019-06-27 00:46:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|