From d635e5a9b395b01542fa980a9f14010762c60369 Mon Sep 17 00:00:00 2001 From: chaoskagami Date: Mon, 13 Jun 2016 15:35:03 -0400 Subject: [PATCH] Looks like it was some kind of issue with globals or order of application or something. I'm not really sure. --- Makefile | 2 +- host/copy.sh | 4 +--- source/patch/emunand.c | 37 +++++++++++++++++++------------------ source/patch/module.c | 4 ++-- source/patcher.c | 34 +++++++++++++++++----------------- 5 files changed, 40 insertions(+), 41 deletions(-) diff --git a/Makefile b/Makefile index f36b9f4..03791aa 100644 --- a/Makefile +++ b/Makefile @@ -20,7 +20,7 @@ dir_out := out REVISION := r$(shell git rev-list --count HEAD):$(shell git rev-parse HEAD | head -c8) ASFLAGS := -mlittle-endian -mcpu=arm946e-s -march=armv5te -CFLAGS := -MMD -MP -Wall -Wextra -Werror -fomit-frame-pointer -Os $(ASFLAGS) -fshort-wchar -fno-builtin -std=c11 -DVERSION=\"$(REVISION)\" +CFLAGS := -MMD -MP -Wall -Wextra -Werror -fomit-frame-pointer -Os $(ASFLAGS) -fshort-wchar -fno-builtin -std=gnu11 -DVERSION=\"$(REVISION)\" FLAGS := dir_out=$(abspath $(dir_out)) --no-print-directory LDFLAGS := -nostdlib -Wl,-z,defs -lgcc -Wl,-Map,$(dir_build)/link.map diff --git a/host/copy.sh b/host/copy.sh index 6837bb4..2bae3ba 100755 --- a/host/copy.sh +++ b/host/copy.sh @@ -9,9 +9,7 @@ dev=/dev/sdb mnt=/mnt/ext1 mount ${dev}1 $mnt || exit 0 -cp out/arm9loaderhax.bin $mnt/anim/boot/none.bin || exit 0 -cp out/arm9loaderhax.bin $mnt/anim/boot/r.bin || exit 0 -cp out/arm9loaderhax.bin $mnt/anim/boot/l.bin || exit 0 +cp out/arm9loaderhax.bin $mnt/arm9loaderhax.bin || exit 0 rm -rf $mnt/corbenik cp -r out/corbenik $mnt/ || exit 0 cp -r input/corbenik $mnt/ || exit 0 diff --git a/source/patch/emunand.c b/source/patch/emunand.c index 0f3186a..7cc3aba 100644 --- a/source/patch/emunand.c +++ b/source/patch/emunand.c @@ -13,7 +13,7 @@ #include "../firm/headers.h" #include "../patch_format.h" -uint8_t *temp = (uint8_t *)FCRAM_JUNK_LOC; +uint8_t *emunand_temp = (uint8_t *)FCRAM_JUNK_LOC; void verify_loop_emunand(char *filename) @@ -24,7 +24,7 @@ verify_loop_emunand(char *filename) uint32_t offset = get_file_sector(filename); // Get the sector of the file // Check for RedNAND image on SD - if (!sdmmc_sdcard_readsectors(offset, 1, temp) && *(uint32_t *)(temp + 0x100) == NCSD_MAGIC) { + if (!sdmmc_sdcard_readsectors(offset, 1, emunand_temp) && *(uint32_t *)(emunand_temp + 0x100) == NCSD_MAGIC) { fprintf(stderr, "emunand: found NCSD magic\n"); } else { abort("emunand: selected NAND image is not valid.\n"); @@ -43,7 +43,7 @@ verify_emunand(uint32_t index, uint32_t *off, uint32_t *head) offset = 0x200000 * index; // Check for RedNAND/Normal physical layout on SD - if (!sdmmc_sdcard_readsectors(offset + 1, 1, temp) && *(uint32_t *)(temp + 0x100) == NCSD_MAGIC) { + if (!sdmmc_sdcard_readsectors(offset + 1, 1, emunand_temp) && *(uint32_t *)(emunand_temp + 0x100) == NCSD_MAGIC) { *off = offset + 1; *head = offset + 1; @@ -51,7 +51,7 @@ verify_emunand(uint32_t index, uint32_t *off, uint32_t *head) fprintf(stderr, "emunand: layout is normal\n"); } // Check for GW EmuNAND on SD - else if (!sdmmc_sdcard_readsectors(offset + nandSize, 1, temp) && *(uint32_t *)(temp + 0x100) == NCSD_MAGIC) { + else if (!sdmmc_sdcard_readsectors(offset + nandSize, 1, emunand_temp) && *(uint32_t *)(emunand_temp + 0x100) == NCSD_MAGIC) { *off = offset; *head = offset + nandSize; @@ -68,7 +68,7 @@ getEmuCode(uint8_t *pos, uint32_t size) const uint8_t pattern[] = { 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0x00 }; // Looking for the last free space before Process9 - void *ret = memfind(pos + 0x13500, size - 0x13500, pattern, 6) + 0x455; + uint8_t *ret = memfind(pos + 0x13500, size - 0x13500, pattern, 6) + 0x455; if (ret) { fprintf(stderr, "emunand: free space @ %x\n", ret); @@ -101,13 +101,16 @@ patchNANDRW(uint8_t *pos, uint32_t size, uint32_t branchOffset) const uint8_t pattern[] = { 0x1E, 0x00, 0xC8, 0x05 }; uint16_t *readOffset = (uint16_t *)memfind(pos, size, pattern, 4) - 3; - uint16_t *writeOffset = (uint16_t *)memfind((uint8_t *)(readOffset + 5), 0x100, pattern, 4) - 3; + uint16_t *writeOffset = (uint16_t *)memfind((uint8_t*)(readOffset + 5), 0x100, pattern, 4) - 3; - *readOffset = nandRedir[0]; + if (!readOffset || !writeOffset) + abort("emunand: pattern for r/w missing!\n"); + + readOffset[0] = nandRedir[0]; readOffset[1] = nandRedir[1]; ((uint32_t *)readOffset)[1] = branchOffset; - *writeOffset = nandRedir[0]; + writeOffset[0] = nandRedir[0]; writeOffset[1] = nandRedir[1]; ((uint32_t *)writeOffset)[1] = branchOffset; @@ -135,9 +138,6 @@ patchMPU(uint8_t *pos, uint32_t size) void patch_emunand(uint32_t index) { - // uint8_t *arm9Section, uint32_t arm9SectionSize, uint8_t *process9Offset, uint32_t process9Size, uint32_t emuOffset, uint32_t emuHeader, uint32_t - // branchAdditive) - // ARM9 section. uint8_t *arm9Section = (uint8_t *)firm_loc + firm_loc->section[2].offset; uint32_t arm9SectionSize = firm_loc->section[2].size; @@ -158,22 +158,23 @@ patch_emunand(uint32_t index) fread(emuCodeOffset, 1, emunand_size, f); fclose(f); - uint32_t branchOffset = (uint32_t)emuCodeOffset - ((uint32_t)firm_loc + firm_loc->section[2].offset - firm_loc->section[2].address); + uint32_t branchOffset = (uintptr_t)emuCodeOffset - ((uintptr_t)firm_loc + firm_loc->section[2].offset - firm_loc->section[2].address); - fprintf(stderr, "emunand: read in emunand code - %x\n", emuCodeOffset); + fprintf(stderr, "emunand: read in emunand code\n"); // Add the data of the found emuNAND - uint32_t *pos_offset = (uint32_t *)memfind(emuCodeOffset, emunand_size, "NAND", 4), *pos_head = (uint32_t *)memfind(emuCodeOffset, emunand_size, "NCSD", 4), - *pos_sdmmc = (uint32_t *)memfind(emuCodeOffset, emunand_size, "SDMC", 4); + uint32_t *pos_offset = (uint32_t *)memfind(emuCodeOffset, emunand_size, "NAND", 4), + *pos_head = (uint32_t *)memfind(emuCodeOffset, emunand_size, "NCSD", 4), + *pos_sdmmc = (uint32_t *)memfind(emuCodeOffset, emunand_size, "SDMC", 4); + + if (!pos_offset || !pos_head || !pos_sdmmc) + abort("emunand: couldn't find pattern in hook?\n"); verify_emunand(index, pos_offset, pos_head); fprintf(stderr, "emunand: nand is on sector %u\n", *pos_offset); fprintf(stderr, "emunand: head is on sector %u\n", *pos_head); - if (!pos_offset || !pos_head || !pos_sdmmc) - abort("emunand: couldn't find pattern in hook?\n"); - // Add emuNAND hooks patchNANDRW(process9Offset, process9Size, branchOffset); diff --git a/source/patch/module.c b/source/patch/module.c index 61ede7c..17b4485 100644 --- a/source/patch/module.c +++ b/source/patch/module.c @@ -11,8 +11,8 @@ PATCH(modules) return 2; } - uint32_t size = fsize(f); - fread((uint8_t *)FCRAM_JUNK_LOC, 1, size, f); + size_t size = fsize(f); + fread((void *)FCRAM_JUNK_LOC, 1, size, f); fclose(f); // Look for the section that holds all the sysmodules diff --git a/source/patcher.c b/source/patcher.c index 066cc10..698c36b 100644 --- a/source/patcher.c +++ b/source/patcher.c @@ -61,15 +61,6 @@ patch_firm_all() { execb(PATH_LOADER_CACHE "/BOOT", 0); - // Replace loader? - if (config.options[OPTION_LOADER]) { - if (patch_modules()) { - abort("Fatal. Loader inject has failed."); - } - // This requires OPTION_SIGPATCH. - wait(); - } - // Hook firmlaunch? if (config.options[OPTION_REBOOT]) { patch_reboot(); @@ -77,6 +68,14 @@ patch_firm_all() wait(); } + // Use EmuNAND? + if (config.options[OPTION_EMUNAND]) { + // Yes. + patch_emunand(config.options[OPTION_EMUNAND_INDEX]); + + wait(); + } + // Inject services? if (config.options[OPTION_SVCS]) { if (patch_services()) { @@ -85,19 +84,20 @@ patch_firm_all() wait(); } - // Use ARM9 hook thread? - if (config.options[OPTION_ARM9THREAD]) { - // Yes. - - // FIXME - NYI + // Replace loader? + if (config.options[OPTION_LOADER]) { + if (patch_modules()) { + abort("Fatal. Loader inject has failed."); + } + // This requires OPTION_SIGPATCH. wait(); } - // Use EmuNAND? - if (config.options[OPTION_EMUNAND]) { + // Use ARM9 hook thread? + if (config.options[OPTION_ARM9THREAD]) { // Yes. - patch_emunand(config.options[OPTION_EMUNAND_INDEX]); + // FIXME - NYI wait(); } -- 2.39.5