From: chaoskagami Date: Tue, 5 Jul 2016 13:27:44 +0000 (-0400) Subject: Remove xdeltas (they were missing two pointers) and add an autopatcher program and... X-Git-Url: https://chaos.moe/g/?a=commitdiff_plain;h=f8f84d2b06d9a191c50d46c960ec0d13b12544b3;p=console%2Fmisc_3ds.git Remove xdeltas (they were missing two pointers) and add an autopatcher program and source --- diff --git a/ntr_pathhack/ntr_move_paths.xdelta3 b/ntr_pathhack/ntr_move_paths.xdelta3 deleted file mode 100644 index ae82ee8..0000000 Binary files a/ntr_pathhack/ntr_move_paths.xdelta3 and /dev/null differ diff --git a/ntr_pathhack/ntr_move_paths_to_3ds_ntr.xdelta3 b/ntr_pathhack/ntr_move_paths_to_3ds_ntr.xdelta3 deleted file mode 100644 index 7b1ad6b..0000000 Binary files a/ntr_pathhack/ntr_move_paths_to_3ds_ntr.xdelta3 and /dev/null differ diff --git a/ntr_pathhack/ntr_path_patcher-linux32 b/ntr_pathhack/ntr_path_patcher-linux32 new file mode 100755 index 0000000..a769a5b Binary files /dev/null and b/ntr_pathhack/ntr_path_patcher-linux32 differ diff --git a/ntr_pathhack/ntr_path_patcher-linux64 b/ntr_pathhack/ntr_path_patcher-linux64 new file mode 100755 index 0000000..e1bad87 Binary files /dev/null and b/ntr_pathhack/ntr_path_patcher-linux64 differ diff --git a/ntr_pathhack/ntr_path_patcher-win64.exe b/ntr_pathhack/ntr_path_patcher-win64.exe new file mode 100755 index 0000000..4a341e8 Binary files /dev/null and b/ntr_pathhack/ntr_path_patcher-win64.exe differ diff --git a/ntr_pathhack/ntr_path_patcher.c b/ntr_pathhack/ntr_path_patcher.c new file mode 100644 index 0000000..27d164d --- /dev/null +++ b/ntr_pathhack/ntr_path_patcher.c @@ -0,0 +1,151 @@ +#include +#include +#include +#include + +#define RELOC_COUNT 9 + +#include +#include + +#define ALPHABET_LEN 256 +#define NOT_FOUND patlen +#define max(a, b) ((a < b) ? b : a) + +// Quick Search algorithm, adapted from +// http://igm.univ-mlv.fr/~lecroq/string/node19.html#SECTION00190 +uint32_t +memfind(uint8_t *startPos, uint32_t size, const void *pattern, uint32_t patternSize) +{ + const uint8_t *patternc = (const uint8_t *)pattern; + + // Preprocessing + uint32_t table[ALPHABET_LEN]; + + for (uint32_t i = 0; i < ALPHABET_LEN; ++i) + table[i] = patternSize + 1; + for (uint32_t i = 0; i < patternSize; ++i) + table[patternc[i]] = patternSize - i; + + // Searching + uint32_t j = 0; + + while (j <= size - patternSize) { + if (memcmp(patternc, startPos + j, patternSize) == 0) + return j; + j += table[startPos[j + patternSize]]; + } + + return 0; +} + +int main(int argc, char** argv) { + uint32_t size = 0; + uint32_t base = 0x100100; + uint8_t* mem = NULL; + FILE* ntr = NULL; + int expand = 0; + int vanilla = 0; + + static uint8_t* find_strings[RELOC_COUNT] = { + "/ntr.bin", + "/plugin/%s", + "/debug.flag", + "/axiwram.dmp", + "/pid0.dmp", + "/pid2.dmp", + "/pid3.dmp", + "/pidf.dmp", + "/arm11.bin", + }; // Nine buffers + + static uint8_t* fix_strings[RELOC_COUNT] = { + "/3ds/ntr/ntr.bin", + "/3ds/ntr/plugin/%s", + "/3ds/ntr/debug.flag", + "/3ds/ntr/axiwram.dmp", + "/3ds/ntr/pid0.dmp", + "/3ds/ntr/pid2.dmp", + "/3ds/ntr/pid3.dmp", + "/3ds/ntr/pidf.dmp", + "/3ds/ntr/arm11.bin", + }; // Nine buffers + + if (argc < 2) { + fprintf(stderr, "No NTR.bin provided to patch, see --help for usage or drop a file on the binary\n"); + exit(0); + } + + if (!strcmp(argv[1], "--help")) { + printf("Automated NTR path patcher r1\n"); + printf("Usage: %s [-v] NTR.bin\n", argv[0]); + printf(" -v Keep vanilla paths, do not redirect to `/3ds/ntr`\n"); + printf("An output file will be generated named `NTR_patched.bin`\n"); + exit(0); + } else if (!strcmp(argv[1], "-v")) { + printf("Keeping vanilla paths.\n"); + vanilla = 1; + } + + // Get size, allocate memory, clear and read to memory + ntr = fopen(argv[1], "rb"); + fseek(ntr, 0, SEEK_END); + size = ftell(ntr); + rewind(ntr); + mem = malloc(size + (0x100 * 9)); + memset(mem, 0, size + (0x100 * 9)); + fread(mem, 1, size, ntr); + fclose(ntr); + + fprintf(stderr, "size: %u\n", size); + + // Find location of strings to relocate. + for(int i=0; i < RELOC_COUNT; i++) { + char* str = find_strings[i]; + uint32_t strl = strlen(str); + uint32_t off = memfind(mem, size, str, strl); + + if (off == 0) { + fprintf(stderr, "String not found for \"%s\" (%d). Skipping.\n", str, strl); + continue; + } + + fprintf(stderr, "Found string \"%s\" in ntr: %x\n", str, off); + + // Clear string data + memset(&mem[off], 0, strl); + + off += base; // Rebase pointer relative to NTRs base. + + uint32_t patchme = (uint32_t*)memfind(mem, size, (uint8_t*)&off, 4); // Find xref + if (patchme == 0) { + fprintf(stderr, "Pointer for string \"%s\" is missing! Aborting.\n", str); + exit(1); + } + + fprintf(stderr, "Found pointer @ %08x\n", patchme); + + // New offset is end + patch count * buffer + uint32_t patch_off = size + (0x100 * expand); + if (vanilla) + strcpy(&mem[patch_off], str); + else + strcpy(&mem[patch_off], fix_strings[i]); + + expand += 1; + + uint32_t* patch_rel = (uint32_t*)(&mem[patchme]); + + // Rebase new pointer + *patch_rel = patch_off + base; + } + + if (expand) { + FILE* out_fix = fopen("ntr_fixed.bin", "wb"); + fwrite(mem, 1, size + (0x100 * expand), out_fix); + fclose(out_fix); + } + + free(mem); + return 0; +} diff --git a/ntr_pathhack/pathhack.txt b/ntr_pathhack/pathhack.txt index 8278a9c..941b1aa 100644 --- a/ntr_pathhack/pathhack.txt +++ b/ntr_pathhack/pathhack.txt @@ -1,11 +1,12 @@ -ntr_move_paths.bin - Patch ntr.bin with the pathhack but same paths as unmodified -ntr_move_paths_to_3ds_ntr.bin - Patch ntr with pathhack, put everything normally on root in /3ds/ntr and use a modified BootNTR (not provided here) +To use: compile patcher.c and drag a NTR.bin onto it. An ntr_patched.bin will be generated. -These patches are intended for 3.4p2 only. +Precompiled versions are provided for linux x86/x86_64 and Win64. ------------------------------------------------------------------------------- +-------------------------------- -The actual hack: add static buffers to the end of ntr.bin (256 each, FAT max) and patch refs to them keeping with the relocations (e.g. add 0x100100 to the offset rel to 0): +The actual hack: add static buffers to the end of ntr.bin (256 each, FAT max) and patch refs to them keeping with the relocations (e.g. add 0x100100 to the offset rel to 0.) + +For 3.4p1 for example: 0x3E624 - "/ntr.bin" 0x2C44C: 24 E7 0x3E724 - "/plugin/%s" @@ -27,4 +28,4 @@ The actual hack: add static buffers to the end of ntr.bin (256 each, FAT max) an Afterwards, null out the original strings. -Short version - you can now modify the paths NTR uses, you're welcome. Pop it open in a hex editor and modify the end buffers as needed (they're all 256 chars) or someone code a pathchanger +Short version - you can now modify the paths NTR uses, you're welcome. Pop it open in a hex editor and modify the end buffers as needed (they're all 256 chars.) The patcher tool here autocalculates offsets.