From f992ea778b1cb0bb22c7939ec5b8ac27959c7b97 Mon Sep 17 00:00:00 2001 From: chaoskagami Date: Sun, 21 Aug 2016 15:28:56 -0400 Subject: [PATCH] Inject system modules in TWL/AGB (god apache, that was out of nowhere, gg) --- Makefile.am | 5 ++++- include/patch_format.h | 5 +++++ source/firm/firm.c | 2 +- source/patch/module.c | 24 ++++++++++++++++-------- 4 files changed, 26 insertions(+), 10 deletions(-) diff --git a/Makefile.am b/Makefile.am index 4acfea8..d7f4b63 100644 --- a/Makefile.am +++ b/Makefile.am @@ -9,6 +9,9 @@ all-local: mkdir -p out mkdir -p out@prefix@ mkdir -p out@libdir@/module + mkdir -p out@libdir@/module/native + mkdir -p out@libdir@/module/twl + mkdir -p out@libdir@/module/agb mkdir -p out@libdir@/firmware mkdir -p out@datarootdir@/keys mkdir -p out@datarootdir@/locale/emu @@ -19,7 +22,7 @@ all-local: mkdir -p out@sysconfdir@ mkdir -p out@localstatedir@ cp source/corbenik.bin out/arm9loaderhax.bin - cp external/loader/loader.cxi out@libdir@/module/loader.cxi + cp external/loader/loader.cxi out@libdir@/module/native/loader.cxi cp external/bits/*.bin out@libexecdir@/ cp host/termfont.bin out@datarootdir@/ cp patch/*.vco out@sbindir@ diff --git a/include/patch_format.h b/include/patch_format.h index 28668f9..9c3bd4b 100644 --- a/include/patch_format.h +++ b/include/patch_format.h @@ -21,6 +21,11 @@ // TODO - We also need to handle patches for internal system modules here, performing lzss decompression (and either recompression, or getting a patch to skip that if needed #define PATH_MODULES LIBDIR "/module" ///< Sysmodule location + +#define PATH_MODULE_NATIVE PATH_MODULES "/native" ///< Native FIRM Sysmodule location +#define PATH_MODULE_AGB PATH_MODULES "/agb" ///< AGB FIRM Sysmodule location +#define PATH_MODULE_TWL PATH_MODULES "/twl" ///< TWL FIRM Sysmodule location + #define PATH_PATCHES SBINDIR ///< Patch binary folder. #define PATH_BITS LIBEXECDIR ///< Path to misc bits we need (emunand code, reboot code, etc) diff --git a/source/firm/firm.c b/source/firm/firm.c index c132cb8..df5f046 100644 --- a/source/firm/firm.c +++ b/source/firm/firm.c @@ -558,7 +558,7 @@ boot_cfw() if (patch_firm_all() != 0) return; - if (config->options[OPTION_REBOOT] && config->options[OPTION_RECONFIGURED]) { + if (config->options[OPTION_REBOOT]) { fprintf(stderr, "Saving FIRM for reboot...\n"); if (!write_file(firm_loc, PATH_NATIVE_P, firm_size)) abort("Failed to save prepatched native\n"); diff --git a/source/patch/module.c b/source/patch/module.c index b6bf20f..aba1a61 100644 --- a/source/patch/module.c +++ b/source/patch/module.c @@ -2,6 +2,8 @@ /* Not possible to be implemented as bytecode. Hey, can't win em all. */ +firm_h* firm_modules; + void inject_module(char* fpath) { @@ -25,10 +27,10 @@ inject_module(char* fpath) fclose(f); int section_index = 0; - firm_section_h *sysmodule_section = &firm_loc->section[0]; + firm_section_h *sysmodule_section = &firm_modules->section[0]; ncch_h *module = (ncch_h *)temp; - ncch_h *sysmodule = (ncch_h *)((uint32_t)firm_loc + sysmodule_section->offset); + ncch_h *sysmodule = (ncch_h *)((uint32_t)firm_modules + sysmodule_section->offset); // Check if we want to replace an existing sysmodule while (sysmodule->magic == NCCH_MAGIC) { @@ -38,13 +40,13 @@ inject_module(char* fpath) uint32_t need_units = (module->contentSize - sysmodule->contentSize); memmove((uint8_t *)sysmodule + module->contentSize * 0x200, (uint8_t *)sysmodule + sysmodule->contentSize * 0x200, - ((uint32_t)firm_loc + firm_size) - ((uint32_t)sysmodule + (module->contentSize * 0x200))); + ((uint32_t)firm_modules + firm_size) - ((uint32_t)sysmodule + (module->contentSize * 0x200))); sysmodule_section->size += 0x200 * need_units; for (int i = 1; i < 4; i++) { - if (firm_loc->section[i].size != 0) { // The last section (3) is usually empty. - firm_loc->section[i].offset += 0x200 * need_units; - firm_loc->section[i].size += 0x200 * need_units; + if (firm_modules->section[i].size != 0) { // The last section (3) is usually empty. + firm_modules->section[i].offset += 0x200 * need_units; + firm_modules->section[i].size += 0x200 * need_units; } } @@ -56,7 +58,7 @@ inject_module(char* fpath) // NOTE - This doesn't change the sysmodule section size; it isn't needed to do so. fprintf(stderr, "Module: Shrink %lu units\n", sysmodule->contentSize - module->contentSize); uint32_t remaining_size = - sysmodule_section->size - (((uint32_t)sysmodule + sysmodule->contentSize * 0x200) - ((uint32_t)firm_loc + sysmodule_section->offset)); + sysmodule_section->size - (((uint32_t)sysmodule + sysmodule->contentSize * 0x200) - ((uint32_t)firm_modules + sysmodule_section->offset)); // Sysmodule section size - (End location of this sysmodule - // Sysmodule section) => memmove((uint8_t *)sysmodule + module->contentSize * 0x200, (uint8_t *)sysmodule + sysmodule->contentSize * 0x200, remaining_size); @@ -84,7 +86,13 @@ end_inj: int patch_modules() { - recurse_call(PATH_MODULES, inject_module); + firm_modules = firm_loc; + recurse_call(PATH_MODULE_NATIVE, inject_module); + firm_modules = twl_firm_loc; + recurse_call(PATH_MODULE_TWL, inject_module); + firm_modules = agb_firm_loc; + recurse_call(PATH_MODULE_AGB, inject_module); return 0; } + -- 2.39.5