Loader: Fix (all?) remaining bugs in ldr:pm.
Loader now works when booted as a KIP1. NOTE: ldr:ro still needs debugging.
This commit is contained in:
@@ -1,15 +1,23 @@
|
||||
#include <switch.h>
|
||||
#include <string.h>
|
||||
#include <vector>
|
||||
#include <algorithm>
|
||||
|
||||
#include "ldr_registration.hpp"
|
||||
#include "ldr_content_management.hpp"
|
||||
|
||||
static FsFileSystem g_CodeFileSystem = {0};
|
||||
|
||||
static std::vector<u64> g_created_titles;
|
||||
static bool g_has_initialized_fs_dev = false;
|
||||
|
||||
Result ContentManagement::MountCode(u64 tid, FsStorageId sid) {
|
||||
char path[FS_MAX_PATH] = {0};
|
||||
Result rc;
|
||||
|
||||
/* We defer SD card mounting, so if relevant ensure it is mounted. */
|
||||
TryMountSdCard();
|
||||
|
||||
if (R_FAILED(rc = GetContentPath(path, tid, sid))) {
|
||||
return rc;
|
||||
}
|
||||
@@ -99,4 +107,23 @@ Result ContentManagement::SetContentPath(const char *path, u64 tid, FsStorageId
|
||||
|
||||
Result ContentManagement::SetContentPathForTidSid(const char *path, Registration::TidSid *tid_sid) {
|
||||
return SetContentPath(path, tid_sid->title_id, tid_sid->storage_id);
|
||||
}
|
||||
|
||||
bool ContentManagement::HasCreatedTitle(u64 tid) {
|
||||
return std::find(g_created_titles.begin(), g_created_titles.end(), tid) != g_created_titles.end();
|
||||
}
|
||||
|
||||
void ContentManagement::SetCreatedTitle(u64 tid) {
|
||||
if (!HasCreatedTitle(tid)) {
|
||||
g_created_titles.push_back(tid);
|
||||
}
|
||||
}
|
||||
|
||||
void ContentManagement::TryMountSdCard() {
|
||||
/* Mount SD card, if psc, bus, and pcv have been created. */
|
||||
if (!g_has_initialized_fs_dev && HasCreatedTitle(0x0100000000000021) && HasCreatedTitle(0x010000000000000A) && HasCreatedTitle(0x010000000000001A)) {
|
||||
if (R_SUCCEEDED(fsdevMountSdmc())) {
|
||||
g_has_initialized_fs_dev = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -13,4 +13,8 @@ class ContentManagement {
|
||||
static Result SetContentPath(const char *path, u64 tid, FsStorageId sid);
|
||||
static Result GetContentPathForTidSid(char *out_path, Registration::TidSid *tid_sid);
|
||||
static Result SetContentPathForTidSid(const char *path, Registration::TidSid *tid_sid);
|
||||
|
||||
static bool HasCreatedTitle(u64 tid);
|
||||
static void SetCreatedTitle(u64 tid);
|
||||
static void TryMountSdCard();
|
||||
};
|
||||
|
||||
@@ -44,29 +44,30 @@ void __appInit(void) {
|
||||
|
||||
/* Initialize services we need (TODO: SPL) */
|
||||
rc = smInitialize();
|
||||
if (R_FAILED(rc))
|
||||
if (R_FAILED(rc)) {
|
||||
fatalSimple(MAKERESULT(Module_Libnx, LibnxError_InitFail_SM));
|
||||
}
|
||||
|
||||
rc = fsInitialize();
|
||||
if (R_FAILED(rc))
|
||||
if (R_FAILED(rc)) {
|
||||
fatalSimple(MAKERESULT(Module_Libnx, LibnxError_InitFail_FS));
|
||||
}
|
||||
|
||||
|
||||
rc = lrInitialize();
|
||||
if (R_FAILED(rc))
|
||||
if (R_FAILED(rc)) {
|
||||
fatalSimple(0xCAFE << 4 | 1);
|
||||
}
|
||||
|
||||
/* Disable.
|
||||
rc = fsldrInitialize();
|
||||
if (R_FAILED(rc))
|
||||
if (R_FAILED(rc)) {
|
||||
fatalSimple(0xCAFE << 4 | 2);
|
||||
*/
|
||||
|
||||
fsdevInit();
|
||||
}
|
||||
}
|
||||
|
||||
void __appExit(void) {
|
||||
/* Cleanup services. */
|
||||
fsdevExit();
|
||||
fsdevUnmountAll();
|
||||
fsldrExit();
|
||||
lrExit();
|
||||
fsExit();
|
||||
@@ -81,12 +82,12 @@ int main(int argc, char **argv)
|
||||
WaitableManager *server_manager = new WaitableManager(U64_MAX);
|
||||
|
||||
/* Add services to manager. */
|
||||
server_manager->add_waitable(new ServiceServer<ProcessManagerService>("dbg:pm", 1));
|
||||
server_manager->add_waitable(new ServiceServer<ShellService>("dbg:shel", 3));
|
||||
server_manager->add_waitable(new ServiceServer<DebugMonitorService>("dbg:dmnt", 2));
|
||||
server_manager->add_waitable(new ServiceServer<ProcessManagerService>("ldr:pm", 1));
|
||||
server_manager->add_waitable(new ServiceServer<ShellService>("ldr:shel", 3));
|
||||
server_manager->add_waitable(new ServiceServer<DebugMonitorService>("ldr:dmnt", 2));
|
||||
if (!kernelAbove300()) {
|
||||
/* On 1.0.0-2.3.0, Loader services ldr:ro instead of ro. */
|
||||
server_manager->add_waitable(new ServiceServer<RelocatableObjectsService>("dbg:ro", 0x20));
|
||||
server_manager->add_waitable(new ServiceServer<RelocatableObjectsService>("ldr:ro", 0x20));
|
||||
}
|
||||
|
||||
/* Loop forever, servicing our services. */
|
||||
|
||||
@@ -48,7 +48,7 @@ class AutoCloseMap {
|
||||
return rc;
|
||||
}
|
||||
|
||||
if (R_FAILED((rc = svcMapProcessMemory((void *)try_address, process_h, try_address, size)))) {
|
||||
if (R_FAILED((rc = svcMapProcessMemory((void *)try_address, process_h, address, size)))) {
|
||||
return rc;
|
||||
}
|
||||
|
||||
@@ -128,7 +128,7 @@ struct MappedCodeMemory {
|
||||
return rc;
|
||||
}
|
||||
|
||||
if (R_FAILED((rc = svcMapProcessMemory((void *)try_address, this->process_handle, try_address, size)))) {
|
||||
if (R_FAILED((rc = svcMapProcessMemory((void *)try_address, this->process_handle, this->code_memory_address, size)))) {
|
||||
return rc;
|
||||
}
|
||||
|
||||
@@ -138,7 +138,7 @@ struct MappedCodeMemory {
|
||||
|
||||
void Unmap() {
|
||||
if (this->IsMapped()) {
|
||||
if (R_FAILED(svcUnmapProcessMemory(this->mapped_address, this->process_handle, this->base_address, this->size))) {
|
||||
if (R_FAILED(svcUnmapProcessMemory(this->mapped_address, this->process_handle, this->code_memory_address, this->size))) {
|
||||
/* TODO: panic(). */
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,6 +5,7 @@
|
||||
#include "ldr_registration.hpp"
|
||||
|
||||
static NpdmUtils::NpdmCache g_npdm_cache = {0};
|
||||
static char g_npdm_path[FS_MAX_PATH] = {0};
|
||||
|
||||
Result NpdmUtils::LoadNpdmFromCache(u64 tid, NpdmInfo *out) {
|
||||
if (g_npdm_cache.info.title_id != tid) {
|
||||
@@ -14,12 +15,33 @@ Result NpdmUtils::LoadNpdmFromCache(u64 tid, NpdmInfo *out) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
FILE *NpdmUtils::OpenNpdmFromExeFS() {
|
||||
std::fill(g_npdm_path, g_npdm_path + FS_MAX_PATH, 0);
|
||||
snprintf(g_npdm_path, FS_MAX_PATH, "code:/main.npdm");
|
||||
return fopen(g_npdm_path, "rb");
|
||||
}
|
||||
|
||||
FILE *NpdmUtils::OpenNpdmFromSdCard(u64 title_id) {
|
||||
std::fill(g_npdm_path, g_npdm_path + FS_MAX_PATH, 0);
|
||||
snprintf(g_npdm_path, FS_MAX_PATH, "sdmc:/atmosphere/titles/%016lx/exefs/main.npdm", title_id);
|
||||
return fopen(g_npdm_path, "rb");
|
||||
}
|
||||
|
||||
|
||||
FILE *NpdmUtils::OpenNpdm(u64 title_id) {
|
||||
FILE *f_out = OpenNpdmFromSdCard(title_id);
|
||||
if (f_out != NULL) {
|
||||
return f_out;
|
||||
}
|
||||
return OpenNpdmFromExeFS();
|
||||
}
|
||||
|
||||
Result NpdmUtils::LoadNpdm(u64 tid, NpdmInfo *out) {
|
||||
Result rc;
|
||||
|
||||
g_npdm_cache.info = (const NpdmUtils::NpdmInfo){0};
|
||||
|
||||
FILE *f_npdm = fopen("code:/main.npdm", "rb");
|
||||
FILE *f_npdm = OpenNpdm(tid);
|
||||
if (f_npdm == NULL) {
|
||||
/* For generic "Couldn't open the file" error, just say the file doesn't exist. */
|
||||
return 0x202;
|
||||
@@ -220,10 +242,10 @@ Result NpdmUtils::ValidateCapabilityAgainstRestrictions(u32 *restrict_caps, size
|
||||
for (size_t i = 0; i < num_restrict_caps - 1; i++) {
|
||||
if ((restrict_caps[i] & 0x7F) == 0x3F) {
|
||||
r_desc = restrict_caps[i] >> 7;
|
||||
if ((restrict_caps[i+1] & 0x7F) != 0x3F) {
|
||||
if ((restrict_caps[i+1] & 0x7F) != 0x3F) {
|
||||
break;
|
||||
}
|
||||
u32 r_next_desc = restrict_caps[i++] >> 7;
|
||||
u32 r_next_desc = restrict_caps[++i] >> 7;
|
||||
u32 r_base_addr = r_desc & 0xFFFFFF;
|
||||
u32 r_base_size = r_next_desc & 0xFFFFFF;
|
||||
/* Size check the mapping. */
|
||||
@@ -329,7 +351,7 @@ Result NpdmUtils::ValidateCapabilityAgainstRestrictions(u32 *restrict_caps, size
|
||||
r_desc = restrict_caps[i] >> 16;
|
||||
desc &= 0x3FF;
|
||||
r_desc &= 0x3FF;
|
||||
if (desc <= r_desc) {
|
||||
if (desc > r_desc) {
|
||||
break;
|
||||
}
|
||||
/* Valid! */
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
#pragma once
|
||||
#include <switch.h>
|
||||
#include <cstdio>
|
||||
|
||||
#include "ldr_registration.hpp"
|
||||
|
||||
@@ -83,6 +84,10 @@ class NpdmUtils {
|
||||
static Result ValidateCapabilityAgainstRestrictions(u32 *restrict_caps, size_t num_restrict_caps, u32 *&cur_cap, size_t &caps_remaining);
|
||||
static Result ValidateCapabilities(u32 *acid_caps, size_t num_acid_caps, u32 *aci0_caps, size_t num_aci0_caps);
|
||||
|
||||
|
||||
static FILE *OpenNpdmFromExeFS();
|
||||
static FILE *OpenNpdmFromSdCard(u64 tid);
|
||||
static FILE *OpenNpdm(u64 tid);
|
||||
static Result LoadNpdm(u64 tid, NpdmInfo *out);
|
||||
static Result LoadNpdmFromCache(u64 tid, NpdmInfo *out);
|
||||
};
|
||||
@@ -2,7 +2,7 @@
|
||||
#include <algorithm>
|
||||
#include <cstdio>
|
||||
#include <cstring>
|
||||
#include <picosha2.hpp>
|
||||
#include "sha256.h"
|
||||
#include "ldr_nro.hpp"
|
||||
#include "ldr_registration.hpp"
|
||||
#include "ldr_map.hpp"
|
||||
@@ -35,6 +35,7 @@ Result NroUtils::LoadNro(Registration::Process *target_proc, Handle process_h, u
|
||||
unsigned int i;
|
||||
Result rc;
|
||||
u8 nro_hash[0x20];
|
||||
SHA256_CTX sha_ctx;
|
||||
/* Ensure there is an available NRO slot. */
|
||||
for (i = 0; i < NRO_INFO_MAX; i++) {
|
||||
if (!target_proc->nro_infos[i].in_use) {
|
||||
@@ -78,7 +79,10 @@ Result NroUtils::LoadNro(Registration::Process *target_proc, Handle process_h, u
|
||||
goto LOAD_NRO_END;
|
||||
}
|
||||
|
||||
picosha2::hash256((u8 *)nro, (u8 *)nro + nro->nro_size, nro_hash, nro_hash + sizeof(nro_hash));
|
||||
|
||||
sha256_init(&sha_ctx);
|
||||
sha256_update(&sha_ctx, (u8 *)nro, nro->nro_size);
|
||||
sha256_final(&sha_ctx, nro_hash);
|
||||
|
||||
if (!Registration::IsNroHashPresent(target_proc->index, nro_hash)) {
|
||||
rc = 0x6C09;
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
#include <algorithm>
|
||||
#include <cstdio>
|
||||
#include <cstring>
|
||||
#include <picosha2.hpp>
|
||||
#include "sha256.h"
|
||||
#include "lz4.h"
|
||||
#include "ldr_nso.hpp"
|
||||
#include "ldr_map.hpp"
|
||||
@@ -25,12 +25,26 @@ FILE *NsoUtils::OpenNsoFromSdCard(unsigned int index, u64 title_id) {
|
||||
return fopen(g_nso_path, "rb");
|
||||
}
|
||||
|
||||
bool NsoUtils::CheckNsoStubbed(unsigned int index, u64 title_id) {
|
||||
std::fill(g_nso_path, g_nso_path + FS_MAX_PATH, 0);
|
||||
snprintf(g_nso_path, FS_MAX_PATH, "sdmc:/atmosphere/titles/%016lx/exefs/%s.stub", title_id, NsoUtils::GetNsoFileName(index));
|
||||
FILE *f = fopen(g_nso_path, "rb");
|
||||
bool ret = (f != NULL);
|
||||
if (ret) {
|
||||
fclose(f);
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
FILE *NsoUtils::OpenNso(unsigned int index, u64 title_id) {
|
||||
FILE *f_out = OpenNsoFromSdCard(index, title_id);
|
||||
if (f_out != NULL) {
|
||||
return f_out;
|
||||
} else if (CheckNsoStubbed(index, title_id)) {
|
||||
return NULL;
|
||||
} else {
|
||||
return OpenNsoFromExeFS(index);
|
||||
}
|
||||
return OpenNsoFromExeFS(index);
|
||||
}
|
||||
|
||||
bool NsoUtils::IsNsoPresent(unsigned int index) {
|
||||
@@ -198,20 +212,28 @@ Result NsoUtils::LoadNsoSegment(unsigned int index, unsigned int segment, FILE *
|
||||
|
||||
u8 *dst_addr = map_base + g_nso_headers[index].segments[segment].dst_offset;
|
||||
u8 *load_addr = is_compressed ? map_end - size : dst_addr;
|
||||
|
||||
|
||||
fseek(f_nso, g_nso_headers[index].segments[segment].file_offset, SEEK_SET);
|
||||
if (fread(load_addr, 1, size, f_nso) != size) {
|
||||
return 0xA09;
|
||||
}
|
||||
|
||||
|
||||
if (is_compressed) {
|
||||
if (LZ4_decompress_safe((char *)load_addr, (char *)dst_addr, size, out_size) != (int)out_size) {
|
||||
return 0xA09;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
if (check_hash) {
|
||||
u8 hash[0x20] = {0};
|
||||
picosha2::hash256(dst_addr, dst_addr + out_size, hash, hash + sizeof(hash));
|
||||
SHA256_CTX sha_ctx;
|
||||
sha256_init(&sha_ctx);
|
||||
sha256_update(&sha_ctx, dst_addr, out_size);
|
||||
sha256_final(&sha_ctx, hash);
|
||||
|
||||
if (std::memcmp(g_nso_headers[index].section_hashes[segment], hash, sizeof(hash))) {
|
||||
return 0xA09;
|
||||
}
|
||||
@@ -242,6 +264,7 @@ Result NsoUtils::LoadNsosIntoProcessMemory(Handle process_h, u64 title_id, NsoLo
|
||||
return rc;
|
||||
}
|
||||
}
|
||||
|
||||
fclose(f_nso);
|
||||
f_nso = NULL;
|
||||
/* Zero out memory before .text. */
|
||||
@@ -254,7 +277,7 @@ Result NsoUtils::LoadNsosIntoProcessMemory(Handle process_h, u64 title_id, NsoLo
|
||||
u64 rw_base = ro_start + g_nso_headers[i].segments[1].decomp_size, rw_start = g_nso_headers[i].segments[2].dst_offset;
|
||||
std::fill(map_base + rw_base, map_base + rw_start, 0);
|
||||
/* Zero out .bss. */
|
||||
u64 bss_base = rw_base + g_nso_headers[i].segments[2].decomp_size, bss_size = g_nso_headers[i].segments[2].align_or_total_size;
|
||||
u64 bss_base = rw_start + g_nso_headers[i].segments[2].decomp_size, bss_size = g_nso_headers[i].segments[2].align_or_total_size;
|
||||
std::fill(map_base + bss_base, map_base + bss_base + bss_size, 0);
|
||||
|
||||
nso_map.Close();
|
||||
|
||||
@@ -83,6 +83,7 @@ class NsoUtils {
|
||||
|
||||
static FILE *OpenNsoFromExeFS(unsigned int index);
|
||||
static FILE *OpenNsoFromSdCard(unsigned int index, u64 title_id);
|
||||
static bool CheckNsoStubbed(unsigned int index, u64 title_id);
|
||||
static FILE *OpenNso(unsigned int index, u64 title_id);
|
||||
|
||||
static bool IsNsoPresent(unsigned int index);
|
||||
|
||||
@@ -153,7 +153,7 @@ Result ProcessCreation::CreateProcess(Handle *out_process_h, u64 index, char *nc
|
||||
process_info.code_addr = nso_extents.base_address;
|
||||
process_info.code_num_pages = nso_extents.total_size + 0xFFF;
|
||||
process_info.code_num_pages >>= 12;
|
||||
|
||||
|
||||
/* Call svcCreateProcess(). */
|
||||
rc = svcCreateProcess(&process_h, &process_info, (u32 *)npdm_info.aci0_kac, npdm_info.aci0->kac_size/sizeof(u32));
|
||||
if (R_FAILED(rc)) {
|
||||
@@ -168,7 +168,6 @@ Result ProcessCreation::CreateProcess(Handle *out_process_h, u64 index, char *nc
|
||||
rc = NsoUtils::LoadNsosIntoProcessMemory(process_h, npdm_info.aci0->title_id, &nso_extents, NULL, 0);
|
||||
}
|
||||
if (R_FAILED(rc)) {
|
||||
svcCloseHandle(process_h);
|
||||
goto CREATE_PROCESS_END;
|
||||
}
|
||||
|
||||
@@ -180,7 +179,7 @@ Result ProcessCreation::CreateProcess(Handle *out_process_h, u64 index, char *nc
|
||||
} else {
|
||||
is_64_bit_addspace = (npdm_info.header->mmu_flags & 0xE) == 0x2;
|
||||
}
|
||||
Registration::SetProcessIdTidMinAndIs64BitAddressSpace(index, process_id, npdm_info.aci0->title_id, is_64_bit_addspace);
|
||||
Registration::SetProcessIdTidAndIs64BitAddressSpace(index, process_id, npdm_info.aci0->title_id, is_64_bit_addspace);
|
||||
for (unsigned int i = 0; i < NSO_NUM_MAX; i++) {
|
||||
if (NsoUtils::IsNsoPresent(i)) {
|
||||
Registration::AddNsoInfo(index, nso_extents.nso_addresses[i], nso_extents.nso_sizes[i], NsoUtils::GetNsoBuildId(i));
|
||||
|
||||
@@ -6,9 +6,8 @@
|
||||
#include "ldr_npdm.hpp"
|
||||
|
||||
Result ProcessManagerService::dispatch(IpcParsedCommand &r, IpcCommand &out_c, u64 cmd_id, u8 *pointer_buffer, size_t pointer_buffer_size) {
|
||||
|
||||
Result rc = 0xF601;
|
||||
|
||||
|
||||
switch ((ProcessManagerServiceCmd)cmd_id) {
|
||||
case Pm_Cmd_CreateProcess:
|
||||
rc = WrapIpcCommandImpl<&ProcessManagerService::create_process>(this, r, out_c, pointer_buffer, pointer_buffer_size);
|
||||
@@ -25,6 +24,7 @@ Result ProcessManagerService::dispatch(IpcParsedCommand &r, IpcCommand &out_c, u
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
return rc;
|
||||
}
|
||||
|
||||
@@ -51,6 +51,10 @@ std::tuple<Result, MovedHandle> ProcessManagerService::create_process(u64 flags,
|
||||
|
||||
rc = ProcessCreation::CreateProcess(&process_h, index, nca_path, launch_item, flags, reslimit_h.handle);
|
||||
|
||||
if (R_SUCCEEDED(rc)) {
|
||||
ContentManagement::SetCreatedTitle(tid_sid.title_id);
|
||||
}
|
||||
|
||||
return std::make_tuple(rc, MovedHandle{process_h});
|
||||
}
|
||||
|
||||
@@ -66,20 +70,20 @@ std::tuple<Result> ProcessManagerService::get_program_info(Registration::TidSid
|
||||
return std::make_tuple(rc);
|
||||
}
|
||||
|
||||
if (tid_sid.title_id != out_program_info.pointer->title_id_min) {
|
||||
if (tid_sid.title_id != out_program_info.pointer->title_id) {
|
||||
rc = ContentManagement::GetContentPathForTidSid(nca_path, &tid_sid);
|
||||
if (R_FAILED(rc)) {
|
||||
return std::make_tuple(rc);
|
||||
}
|
||||
|
||||
rc = ContentManagement::SetContentPath(nca_path, out_program_info.pointer->title_id_min, tid_sid.storage_id);
|
||||
rc = ContentManagement::SetContentPath(nca_path, out_program_info.pointer->title_id, tid_sid.storage_id);
|
||||
if (R_FAILED(rc)) {
|
||||
return std::make_tuple(rc);
|
||||
}
|
||||
|
||||
rc = LaunchQueue::add_copy(tid_sid.title_id, out_program_info.pointer->title_id_min);
|
||||
rc = LaunchQueue::add_copy(tid_sid.title_id, out_program_info.pointer->title_id);
|
||||
}
|
||||
|
||||
|
||||
return std::make_tuple(rc);
|
||||
}
|
||||
|
||||
@@ -120,7 +124,7 @@ Result ProcessManagerService::populate_program_info_buffer(ProcessManagerService
|
||||
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;
|
||||
out->title_id_min = info.acid->title_id_range_min;
|
||||
out->title_id = info.aci0->title_id;
|
||||
|
||||
out->acid_fac_size = info.acid->fac_size;
|
||||
out->aci0_sac_size = info.aci0->sac_size;
|
||||
|
||||
@@ -18,7 +18,7 @@ class ProcessManagerService : IServiceObject {
|
||||
u8 default_cpu_id;
|
||||
u16 application_type;
|
||||
u32 main_thread_stack_size;
|
||||
u64 title_id_min;
|
||||
u64 title_id;
|
||||
u32 acid_sac_size;
|
||||
u32 aci0_sac_size;
|
||||
u32 acid_fac_size;
|
||||
|
||||
@@ -82,14 +82,14 @@ Result Registration::GetRegisteredTidSid(u64 index, Registration::TidSid *out) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
void Registration::SetProcessIdTidMinAndIs64BitAddressSpace(u64 index, u64 process_id, u64 tid_min, bool is_64_bit_addspace) {
|
||||
void Registration::SetProcessIdTidAndIs64BitAddressSpace(u64 index, u64 process_id, u64 tid, bool is_64_bit_addspace) {
|
||||
Registration::Process *target_process = GetProcess(index);
|
||||
if (target_process == NULL) {
|
||||
return;
|
||||
}
|
||||
|
||||
target_process->process_id = process_id;
|
||||
target_process->title_id_min = tid_min;
|
||||
target_process->title_id = tid;
|
||||
target_process->is_64_bit_addspace = is_64_bit_addspace;
|
||||
}
|
||||
|
||||
|
||||
@@ -46,7 +46,7 @@ class Registration {
|
||||
bool is_64_bit_addspace;
|
||||
u64 index;
|
||||
u64 process_id;
|
||||
u64 title_id_min;
|
||||
u64 title_id;
|
||||
Registration::TidSid tid_sid;
|
||||
Registration::NsoInfoHolder nso_infos[NSO_INFO_MAX];
|
||||
Registration::NroInfo nro_infos[NRO_INFO_MAX];
|
||||
@@ -66,7 +66,7 @@ class Registration {
|
||||
static Result GetRegisteredTidSid(u64 index, Registration::TidSid *out);
|
||||
static bool RegisterTidSid(const TidSid *tid_sid, u64 *out_index);
|
||||
static bool UnregisterIndex(u64 index);
|
||||
static void SetProcessIdTidMinAndIs64BitAddressSpace(u64 index, u64 process_id, u64 tid_min, bool is_64_bit_addspace);
|
||||
static void SetProcessIdTidAndIs64BitAddressSpace(u64 index, u64 process_id, u64 tid, bool is_64_bit_addspace);
|
||||
static void AddNsoInfo(u64 index, u64 base_address, u64 size, const unsigned char *build_id);
|
||||
static void CloseRoService(void *service, Handle process_h);
|
||||
static Result AddNrrInfo(u64 index, MappedCodeMemory *nrr_info);
|
||||
|
||||
@@ -120,7 +120,7 @@ std::tuple<Result> RelocatableObjectsService::load_nrr(PidDescriptor pid_desc, u
|
||||
goto LOAD_NRR_END;
|
||||
}
|
||||
|
||||
rc = NroUtils::ValidateNrrHeader((NroUtils::NrrHeader *)nrr_info.mapped_address, nrr_size, target_proc->title_id_min);
|
||||
rc = NroUtils::ValidateNrrHeader((NroUtils::NrrHeader *)nrr_info.mapped_address, nrr_size, target_proc->title_id);
|
||||
if (R_SUCCEEDED(rc)) {
|
||||
Registration::AddNrrInfo(target_proc->index, &nrr_info);
|
||||
}
|
||||
|
||||
158
stratosphere/loader/source/sha256.c
Normal file
158
stratosphere/loader/source/sha256.c
Normal file
@@ -0,0 +1,158 @@
|
||||
/*********************************************************************
|
||||
* Filename: sha256.c
|
||||
* Author: Brad Conte (brad AT bradconte.com)
|
||||
* Copyright:
|
||||
* Disclaimer: This code is presented "as is" without any guarantees.
|
||||
* Details: Implementation of the SHA-256 hashing algorithm.
|
||||
SHA-256 is one of the three algorithms in the SHA2
|
||||
specification. The others, SHA-384 and SHA-512, are not
|
||||
offered in this implementation.
|
||||
Algorithm specification can be found here:
|
||||
* http://csrc.nist.gov/publications/fips/fips180-2/fips180-2withchangenotice.pdf
|
||||
This implementation uses little endian byte order.
|
||||
*********************************************************************/
|
||||
|
||||
/*************************** HEADER FILES ***************************/
|
||||
#include <stdlib.h>
|
||||
#include <memory.h>
|
||||
#include "sha256.h"
|
||||
|
||||
/****************************** MACROS ******************************/
|
||||
#define ROTLEFT(a,b) (((a) << (b)) | ((a) >> (32-(b))))
|
||||
#define ROTRIGHT(a,b) (((a) >> (b)) | ((a) << (32-(b))))
|
||||
|
||||
#define CH(x,y,z) (((x) & (y)) ^ (~(x) & (z)))
|
||||
#define MAJ(x,y,z) (((x) & (y)) ^ ((x) & (z)) ^ ((y) & (z)))
|
||||
#define EP0(x) (ROTRIGHT(x,2) ^ ROTRIGHT(x,13) ^ ROTRIGHT(x,22))
|
||||
#define EP1(x) (ROTRIGHT(x,6) ^ ROTRIGHT(x,11) ^ ROTRIGHT(x,25))
|
||||
#define SIG0(x) (ROTRIGHT(x,7) ^ ROTRIGHT(x,18) ^ ((x) >> 3))
|
||||
#define SIG1(x) (ROTRIGHT(x,17) ^ ROTRIGHT(x,19) ^ ((x) >> 10))
|
||||
|
||||
/**************************** VARIABLES *****************************/
|
||||
static const WORD k[64] = {
|
||||
0x428a2f98,0x71374491,0xb5c0fbcf,0xe9b5dba5,0x3956c25b,0x59f111f1,0x923f82a4,0xab1c5ed5,
|
||||
0xd807aa98,0x12835b01,0x243185be,0x550c7dc3,0x72be5d74,0x80deb1fe,0x9bdc06a7,0xc19bf174,
|
||||
0xe49b69c1,0xefbe4786,0x0fc19dc6,0x240ca1cc,0x2de92c6f,0x4a7484aa,0x5cb0a9dc,0x76f988da,
|
||||
0x983e5152,0xa831c66d,0xb00327c8,0xbf597fc7,0xc6e00bf3,0xd5a79147,0x06ca6351,0x14292967,
|
||||
0x27b70a85,0x2e1b2138,0x4d2c6dfc,0x53380d13,0x650a7354,0x766a0abb,0x81c2c92e,0x92722c85,
|
||||
0xa2bfe8a1,0xa81a664b,0xc24b8b70,0xc76c51a3,0xd192e819,0xd6990624,0xf40e3585,0x106aa070,
|
||||
0x19a4c116,0x1e376c08,0x2748774c,0x34b0bcb5,0x391c0cb3,0x4ed8aa4a,0x5b9cca4f,0x682e6ff3,
|
||||
0x748f82ee,0x78a5636f,0x84c87814,0x8cc70208,0x90befffa,0xa4506ceb,0xbef9a3f7,0xc67178f2
|
||||
};
|
||||
|
||||
/*********************** FUNCTION DEFINITIONS ***********************/
|
||||
void sha256_transform(SHA256_CTX *ctx, const BYTE data[])
|
||||
{
|
||||
WORD a, b, c, d, e, f, g, h, i, j, t1, t2, m[64];
|
||||
|
||||
for (i = 0, j = 0; i < 16; ++i, j += 4)
|
||||
m[i] = (data[j] << 24) | (data[j + 1] << 16) | (data[j + 2] << 8) | (data[j + 3]);
|
||||
for ( ; i < 64; ++i)
|
||||
m[i] = SIG1(m[i - 2]) + m[i - 7] + SIG0(m[i - 15]) + m[i - 16];
|
||||
|
||||
a = ctx->state[0];
|
||||
b = ctx->state[1];
|
||||
c = ctx->state[2];
|
||||
d = ctx->state[3];
|
||||
e = ctx->state[4];
|
||||
f = ctx->state[5];
|
||||
g = ctx->state[6];
|
||||
h = ctx->state[7];
|
||||
|
||||
for (i = 0; i < 64; ++i) {
|
||||
t1 = h + EP1(e) + CH(e,f,g) + k[i] + m[i];
|
||||
t2 = EP0(a) + MAJ(a,b,c);
|
||||
h = g;
|
||||
g = f;
|
||||
f = e;
|
||||
e = d + t1;
|
||||
d = c;
|
||||
c = b;
|
||||
b = a;
|
||||
a = t1 + t2;
|
||||
}
|
||||
|
||||
ctx->state[0] += a;
|
||||
ctx->state[1] += b;
|
||||
ctx->state[2] += c;
|
||||
ctx->state[3] += d;
|
||||
ctx->state[4] += e;
|
||||
ctx->state[5] += f;
|
||||
ctx->state[6] += g;
|
||||
ctx->state[7] += h;
|
||||
}
|
||||
|
||||
void sha256_init(SHA256_CTX *ctx)
|
||||
{
|
||||
ctx->datalen = 0;
|
||||
ctx->bitlen = 0;
|
||||
ctx->state[0] = 0x6a09e667;
|
||||
ctx->state[1] = 0xbb67ae85;
|
||||
ctx->state[2] = 0x3c6ef372;
|
||||
ctx->state[3] = 0xa54ff53a;
|
||||
ctx->state[4] = 0x510e527f;
|
||||
ctx->state[5] = 0x9b05688c;
|
||||
ctx->state[6] = 0x1f83d9ab;
|
||||
ctx->state[7] = 0x5be0cd19;
|
||||
}
|
||||
|
||||
void sha256_update(SHA256_CTX *ctx, const BYTE data[], size_t len)
|
||||
{
|
||||
WORD i;
|
||||
|
||||
for (i = 0; i < len; ++i) {
|
||||
ctx->data[ctx->datalen] = data[i];
|
||||
ctx->datalen++;
|
||||
if (ctx->datalen == 64) {
|
||||
sha256_transform(ctx, ctx->data);
|
||||
ctx->bitlen += 512;
|
||||
ctx->datalen = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void sha256_final(SHA256_CTX *ctx, BYTE hash[])
|
||||
{
|
||||
WORD i;
|
||||
|
||||
i = ctx->datalen;
|
||||
|
||||
// Pad whatever data is left in the buffer.
|
||||
if (ctx->datalen < 56) {
|
||||
ctx->data[i++] = 0x80;
|
||||
while (i < 56)
|
||||
ctx->data[i++] = 0x00;
|
||||
}
|
||||
else {
|
||||
ctx->data[i++] = 0x80;
|
||||
while (i < 64)
|
||||
ctx->data[i++] = 0x00;
|
||||
sha256_transform(ctx, ctx->data);
|
||||
memset(ctx->data, 0, 56);
|
||||
}
|
||||
|
||||
// Append to the padding the total message's length in bits and transform.
|
||||
ctx->bitlen += ctx->datalen * 8;
|
||||
ctx->data[63] = ctx->bitlen;
|
||||
ctx->data[62] = ctx->bitlen >> 8;
|
||||
ctx->data[61] = ctx->bitlen >> 16;
|
||||
ctx->data[60] = ctx->bitlen >> 24;
|
||||
ctx->data[59] = ctx->bitlen >> 32;
|
||||
ctx->data[58] = ctx->bitlen >> 40;
|
||||
ctx->data[57] = ctx->bitlen >> 48;
|
||||
ctx->data[56] = ctx->bitlen >> 56;
|
||||
sha256_transform(ctx, ctx->data);
|
||||
|
||||
// Since this implementation uses little endian byte ordering and SHA uses big endian,
|
||||
// reverse all the bytes when copying the final state to the output hash.
|
||||
for (i = 0; i < 4; ++i) {
|
||||
hash[i] = (ctx->state[0] >> (24 - i * 8)) & 0x000000ff;
|
||||
hash[i + 4] = (ctx->state[1] >> (24 - i * 8)) & 0x000000ff;
|
||||
hash[i + 8] = (ctx->state[2] >> (24 - i * 8)) & 0x000000ff;
|
||||
hash[i + 12] = (ctx->state[3] >> (24 - i * 8)) & 0x000000ff;
|
||||
hash[i + 16] = (ctx->state[4] >> (24 - i * 8)) & 0x000000ff;
|
||||
hash[i + 20] = (ctx->state[5] >> (24 - i * 8)) & 0x000000ff;
|
||||
hash[i + 24] = (ctx->state[6] >> (24 - i * 8)) & 0x000000ff;
|
||||
hash[i + 28] = (ctx->state[7] >> (24 - i * 8)) & 0x000000ff;
|
||||
}
|
||||
}
|
||||
41
stratosphere/loader/source/sha256.h
Normal file
41
stratosphere/loader/source/sha256.h
Normal file
@@ -0,0 +1,41 @@
|
||||
/*********************************************************************
|
||||
* Filename: sha256.h
|
||||
* Author: Brad Conte (brad AT bradconte.com)
|
||||
* Copyright:
|
||||
* Disclaimer: This code is presented "as is" without any guarantees.
|
||||
* Details: Defines the API for the corresponding SHA1 implementation.
|
||||
*********************************************************************/
|
||||
|
||||
#if defined (__cplusplus)
|
||||
extern "C" {
|
||||
#endif
|
||||
#ifndef SHA256_H
|
||||
#define SHA256_H
|
||||
|
||||
/*************************** HEADER FILES ***************************/
|
||||
#include <stddef.h>
|
||||
|
||||
/****************************** MACROS ******************************/
|
||||
#define SHA256_BLOCK_SIZE 32 // SHA256 outputs a 32 byte digest
|
||||
|
||||
/**************************** DATA TYPES ****************************/
|
||||
typedef unsigned char BYTE; // 8-bit byte
|
||||
typedef unsigned int WORD; // 32-bit word, change to "long" for 16-bit machines
|
||||
|
||||
typedef struct {
|
||||
BYTE data[64];
|
||||
WORD datalen;
|
||||
unsigned long long bitlen;
|
||||
WORD state[8];
|
||||
} SHA256_CTX;
|
||||
|
||||
/*********************** FUNCTION DECLARATIONS **********************/
|
||||
void sha256_init(SHA256_CTX *ctx);
|
||||
void sha256_update(SHA256_CTX *ctx, const BYTE data[], size_t len);
|
||||
void sha256_final(SHA256_CTX *ctx, BYTE hash[]);
|
||||
|
||||
#endif // SHA256_H
|
||||
|
||||
#if defined (__cplusplus)
|
||||
}
|
||||
#endif
|
||||
Reference in New Issue
Block a user