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