From 5e5dd530c7cecdc200c356a2b93e6c46092ed711 Mon Sep 17 00:00:00 2001 From: Michael Scire Date: Tue, 24 Apr 2018 03:14:40 -0600 Subject: [PATCH] Clean up Nso fopen semantics --- stratosphere/loader/source/ldr_nso.cpp | 38 ++++++++++--------- stratosphere/loader/source/ldr_nso.hpp | 6 ++- .../loader/source/ldr_process_creation.cpp | 3 +- 3 files changed, 26 insertions(+), 21 deletions(-) diff --git a/stratosphere/loader/source/ldr_nso.cpp b/stratosphere/loader/source/ldr_nso.cpp index f3372cbbc..4ed4cffa5 100644 --- a/stratosphere/loader/source/ldr_nso.cpp +++ b/stratosphere/loader/source/ldr_nso.cpp @@ -6,14 +6,26 @@ static NsoUtils::NsoHeader g_nso_headers[NSO_NUM_MAX] = {0}; static bool g_nso_present[NSO_NUM_MAX] = {0}; -void NsoUtils::GetNsoCodePath(char *content_path, unsigned int index) { - std::fill(content_path, content_path + FS_MAX_PATH, 0); - snprintf(content_path, FS_MAX_PATH, "code:/%s", NsoUtils::GetNsoFileName(index)); +static char g_nso_path[FS_MAX_PATH] = {0}; + +FILE *NsoUtils::OpenNsoFromExeFS(unsigned int index) { + std::fill(g_nso_path, g_nso_path + FS_MAX_PATH, 0); + snprintf(g_nso_path, FS_MAX_PATH, "code:/%s", NsoUtils::GetNsoFileName(index)); + return fopen(g_nso_path, "rb"); } -void NsoUtils::GetNsoSdPath(char *content_path, u64 title_id, unsigned int index) { - std::fill(content_path, content_path + FS_MAX_PATH, 0); - snprintf(content_path, FS_MAX_PATH, "sdmc:/atmosphere/titles/%016lx/exefs/%s", title_id, NsoUtils::GetNsoFileName(index)); +FILE *NsoUtils::OpenNsoFromSdCard(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", title_id, NsoUtils::GetNsoFileName(index)); + return fopen(g_nso_path, "rb"); +} + +FILE *NsoUtils::OpenNso(unsigned int index, u64 title_id) { + FILE *f_out = OpenNsoFromSdCard(index, title_id); + if (f_out != NULL) { + return f_out; + } + return OpenNsoFromExeFS(index); } bool NsoUtils::IsNsoPresent(unsigned int index) { @@ -21,7 +33,6 @@ bool NsoUtils::IsNsoPresent(unsigned int index) { } Result NsoUtils::LoadNsoHeaders(u64 title_id) { - char nso_path[FS_MAX_PATH] = {0}; FILE *f_nso; /* Zero out the cache. */ @@ -29,17 +40,8 @@ Result NsoUtils::LoadNsoHeaders(u64 title_id) { std::fill(g_nso_headers, g_nso_headers + NSO_NUM_MAX, (const NsoUtils::NsoHeader &){0}); for (unsigned int i = 0; i < NSO_NUM_MAX; i++) { - GetNsoSdPath(nso_path, title_id, i); - if ((f_nso = fopen(nso_path, "rb")) != NULL) { - if (fread(&g_nso_headers[i], sizeof(NsoUtils::NsoHeader), 1, f_nso) != sizeof(NsoUtils::NsoHeader)) { - return 0xA09; - } - g_nso_present[i] = true; - fclose(f_nso); - continue; - } - GetNsoCodePath(nso_path, i); - if ((f_nso = fopen(nso_path, "rb")) != NULL) { + f_nso = OpenNso(i, title_id); + if (f_nso != NULL) { if (fread(&g_nso_headers[i], sizeof(NsoUtils::NsoHeader), 1, f_nso) != sizeof(NsoUtils::NsoHeader)) { return 0xA09; } diff --git a/stratosphere/loader/source/ldr_nso.hpp b/stratosphere/loader/source/ldr_nso.hpp index 19e514cba..f36c2832b 100644 --- a/stratosphere/loader/source/ldr_nso.hpp +++ b/stratosphere/loader/source/ldr_nso.hpp @@ -1,5 +1,6 @@ #pragma once #include +#include #define MAGIC_NSO0 0x304F534E #define NSO_NUM_MAX 13 @@ -64,8 +65,9 @@ class NsoUtils { } } - static void GetNsoCodePath(char *content_path, unsigned int index); - static void GetNsoSdPath(char *content_path, u64 title_id, unsigned int index); + static FILE *OpenNsoFromExeFS(unsigned int index); + static FILE *OpenNsoFromSdCard(unsigned int index, u64 title_id); + static FILE *OpenNso(unsigned int index, u64 title_id); static bool IsNsoPresent(unsigned int index); static Result LoadNsoHeaders(u64 title_id); diff --git a/stratosphere/loader/source/ldr_process_creation.cpp b/stratosphere/loader/source/ldr_process_creation.cpp index 3761aa6e7..0384eb946 100644 --- a/stratosphere/loader/source/ldr_process_creation.cpp +++ b/stratosphere/loader/source/ldr_process_creation.cpp @@ -90,6 +90,7 @@ Result ProcessCreation::CreateProcess(Handle *out_process_h, u64 index, char *nc NpdmUtils::NpdmInfo npdm_info = {0}; ProcessInfo process_info = {0}; Registration::Process *target_process; + Handle process_h = 0; Result rc; /* Get the process from the registration queue. */ @@ -143,7 +144,7 @@ Result ProcessCreation::CreateProcess(Handle *out_process_h, u64 index, char *nc /* TODO: Figure out where NSOs will be mapped, and how much space they (and arguments) will take up. */ /* Call svcCreateProcess(). */ - rc = svcCreateProcess(out_process_h, &process_info, (u32 *)npdm_info.aci0_kac, npdm_info.aci0->kac_size/sizeof(u32)); + rc = svcCreateProcess(&process_h, &process_info, (u32 *)npdm_info.aci0_kac, npdm_info.aci0->kac_size/sizeof(u32)); if (R_FAILED(rc)) { goto CREATE_PROCESS_END; }