From c1fd2eda20a60fd449826761775656cc629a696e Mon Sep 17 00:00:00 2001 From: Michael Scire Date: Sat, 28 Jul 2018 20:01:09 -0700 Subject: [PATCH] Loader: Skeleton IPS/IPS32 patching support. --- stratosphere/loader/source/ldr_nso.cpp | 4 + stratosphere/loader/source/ldr_patcher.cpp | 104 +++++++++++++++++++++ stratosphere/loader/source/ldr_patcher.hpp | 10 ++ 3 files changed, 118 insertions(+) create mode 100644 stratosphere/loader/source/ldr_patcher.cpp create mode 100644 stratosphere/loader/source/ldr_patcher.hpp diff --git a/stratosphere/loader/source/ldr_nso.cpp b/stratosphere/loader/source/ldr_nso.cpp index b01e5d25f..69825e06f 100644 --- a/stratosphere/loader/source/ldr_nso.cpp +++ b/stratosphere/loader/source/ldr_nso.cpp @@ -7,6 +7,7 @@ #include "ldr_nso.hpp" #include "ldr_map.hpp" #include "ldr_random.hpp" +#include "ldr_patcher.hpp" static NsoUtils::NsoHeader g_nso_headers[NSO_NUM_MAX] = {0}; static bool g_nso_present[NSO_NUM_MAX] = {0}; @@ -283,6 +284,9 @@ Result NsoUtils::LoadNsosIntoProcessMemory(Handle process_h, u64 title_id, NsoLo 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); + /* Apply patches to loaded module. */ + PatchUtils::ApplyPatches(title_id, &g_nso_headers[i], map_base, bss_base); + nso_map.Close(); for (unsigned int seg = 0; seg < 3; seg++) { diff --git a/stratosphere/loader/source/ldr_patcher.cpp b/stratosphere/loader/source/ldr_patcher.cpp new file mode 100644 index 000000000..87efe19fb --- /dev/null +++ b/stratosphere/loader/source/ldr_patcher.cpp @@ -0,0 +1,104 @@ +#include +#include +#include +#include +#include + +#include +#include "ldr_patcher.hpp" + +#define IPS_MAGIC "PATCH" +#define IPS_TAIL "EOF" + +#define IPS32_MAGIC "IPS32" +#define IPS32_TAIL "EEOF" + +static inline u8 HexNybbleToU8(const char nybble) { + if ('0' <= nybble && nybble <= '9') { + return nybble - '0'; + } else if ('a' <= nybble && nybble <= 'f') { + return nybble - 'a'; + } else { + return nybble - 'A'; + } +} + +static bool MatchesBuildId(const char *name, size_t name_len, const u8 *build_id) { + /* Validate name is hex build id. */ + for (unsigned int i = 0; i < name_len - 4; i++) { + if (!(('0' <= name[i] && name[i] <= '9') || + ('a' <= name[i] && name[i] <= 'f') || + ('A' <= name[i] && name[i] <= 'F'))) { + return false; + } + } + + /* Read build id from name. */ + u8 build_id_from_name[0x20] = {0}; + for (unsigned int name_ofs = 0, id_ofs = 0; name_ofs < name_len - 4; id_ofs++) { + build_id_from_name[id_ofs] |= HexNybbleToU8(name[name_ofs++]) << 4; + build_id_from_name[id_ofs] |= HexNybbleToU8(name[name_ofs++]); + } + + return memcmp(build_id, build_id_from_name, sizeof(build_id_from_name)) == 0; +} + +static inline void PatchNsoByte(u8 *nso, size_t max_size, size_t offset, u8 val) { + /* TODO: Be smarter about this. */ + if (sizeof(NsoUtils::NsoHeader) <= offset && offset < max_size) { + nso[offset - sizeof(NsoUtils::NsoHeader)] = val; + } +} + +static void ApplyIpsPatch(u8 *mapped_nso, size_t mapped_size, FILE *ips) { + /* TODO */ +} + +static void ApplyIps32Patch(u8 *mapped_nso, size_t mapped_size, FILE *ips) { + /* TODO */ +} + +void PatchUtils::ApplyPatches(u64 title_id, const NsoUtils::NsoHeader *header, u8 *mapped_nso, size_t mapped_size) { + /* Inspect all patches from //exefs/patches/<*>/<*>.ips. */ + char path[FS_MAX_PATH+1] = {0}; + snprintf(path, sizeof(path) - 1, "sdmc:/atmosphere/titles/%016lx/exefs/patches", title_id); + DIR *patches_dir = opendir(path); + struct dirent *pdir_ent; + if (patches_dir != NULL) { + /* Iterate over the patches directory to find patch subdirectories. */ + while ((pdir_ent = readdir(patches_dir)) != NULL) { + if (strcmp(pdir_ent->d_name, ".") == 0 || strcmp(pdir_ent->d_name, "..") == 0) { + continue; + } + snprintf(path, sizeof(path) - 1, "sdmc:/atmosphere/titles/%016lx/exefs/patches/%s", title_id, pdir_ent->d_name); + DIR *patch_dir = opendir(path); + struct dirent *ent; + if (patch_dir != NULL) { + /* Iterate over the patch subdirectory to find .ips patches. */ + while ((ent = readdir(patch_dir)) != NULL) { + if (strcmp(ent->d_name, ".") == 0 || strcmp(ent->d_name, "..") == 0) { + continue; + } + size_t name_len = strlen(ent->d_name); + if ((4 < name_len && name_len <= 0x44) && ((name_len & 1) == 0) && strcmp(ent->d_name + name_len - 4, ".ips") == 0 && MatchesBuildId(ent->d_name, name_len, header->build_id)) { + snprintf(path, sizeof(path) - 1, "sdmc:/atmosphere/titles/%016lx/exefs/patches/%s/%s", title_id, pdir_ent->d_name, ent->d_name); + FILE *f_ips = fopen(path, "rb"); + if (f_ips != NULL) { + u8 header[5]; + if (fread(header, 5, 1, f_ips) == 1) { + if (memcmp(header, IPS_MAGIC, 5) == 0) { + ApplyIpsPatch(mapped_nso, mapped_size, f_ips); + } else if (memcmp(header, IPS32_MAGIC, 5) == 0) { + ApplyIps32Patch(mapped_nso, mapped_size, f_ips); + } + } + fclose(f_ips); + } + } + } + closedir(patch_dir); + } + } + closedir(patches_dir); + } +} \ No newline at end of file diff --git a/stratosphere/loader/source/ldr_patcher.hpp b/stratosphere/loader/source/ldr_patcher.hpp new file mode 100644 index 000000000..cea953a73 --- /dev/null +++ b/stratosphere/loader/source/ldr_patcher.hpp @@ -0,0 +1,10 @@ +#pragma once +#include <switch.h> +#include <cstdio> + +#include "ldr_nso.hpp" + +class PatchUtils { + public: + static void ApplyPatches(u64 title_id, const NsoUtils::NsoHeader *header, u8 *mapped_nso, size_t size); +}; \ No newline at end of file