From 7bd0e4f5b4926292489d472f7a6d3c1426f0d2b7 Mon Sep 17 00:00:00 2001 From: Aurora Date: Sun, 11 Sep 2016 03:29:39 +0200 Subject: [PATCH] Revert "Remove some ifdefs" This reverts commit 7271850df2aca2aaa6fbba1d010b7c0f6708ed64. --- source/firm.c | 27 ++++++++++++++++++++++++--- source/firm.h | 4 ++++ source/patches.c | 25 +++++++++++++++++++++++-- source/patches.h | 10 +++++++++- 4 files changed, 60 insertions(+), 6 deletions(-) diff --git a/source/firm.c b/source/firm.c index 91be463..4c99d4c 100755 --- a/source/firm.c +++ b/source/firm.c @@ -35,7 +35,10 @@ #include "buttons.h" #include "pin.h" #include "../build/injector.h" + +#ifdef DEV #include "exceptions.h" +#endif extern u16 launchedFirmTidLow[8]; //Defined in start.s @@ -360,12 +363,19 @@ static inline void patchNativeFirm(u32 firmVersion, FirmwareSource nandType, u32 process9MemAddr; u8 *process9Offset = getProcess9(arm9Section + 0x15000, section[2].size - 0x15000, &process9Size, &process9MemAddr); +#ifdef DEV //Find Kernel11 SVC table and handler, exceptions page and free space locations u32 baseK11VA; u8 *freeK11Space; u32 *arm11SvcHandler, *arm11ExceptionsPage, *arm11SvcTable = getKernel11Info(arm11Section1, section[1].size, &baseK11VA, &freeK11Space, &arm11SvcHandler, &arm11ExceptionsPage); +#else + //Find Kernel11 SVC table and free space locations + u32 baseK11VA; + u8 *freeK11Space; + u32 *arm11SvcTable = getKernel11Info(arm11Section1, section[1].size, &baseK11VA, &freeK11Space); +#endif //Apply signature patches patchSignatureChecks(process9Offset, process9Size); @@ -511,7 +521,7 @@ static inline void copySection0AndInjectSystemModules(FirmwareType firmType) } } #else -static inline void copySection0AndInjectSystemModules(FirmwareType firmType) +static inline void copySection0AndInjectSystemModules(void) { u32 srcModuleSize, dstModuleSize; @@ -524,7 +534,7 @@ static inline void copySection0AndInjectSystemModules(FirmwareType firmType) void *module; - if(firmType == NATIVE_FIRM && memcmp(moduleName, "loader", 6) == 0) + if(memcmp(moduleName, "loader", 6) == 0) { module = (void *)injector; dstModuleSize = injector_size; @@ -542,7 +552,8 @@ static inline void copySection0AndInjectSystemModules(FirmwareType firmType) static inline void launchFirm(FirmwareType firmType) { - //Allow module injection and/or inject 3ds_injector on new NATIVE_FIRMs and LGY FIRMs (with DEV set) +#ifdef DEV + //Allow module injection and/or inject 3ds_injector on new NATIVE_FIRMs and LGY FIRMs u32 sectionNum; if(firmType != SAFE_FIRM && firmType != NATIVE_FIRM1X2X) { @@ -550,6 +561,16 @@ static inline void launchFirm(FirmwareType firmType) sectionNum = 1; } else sectionNum = 0; +#else + //If we're booting NATIVE_FIRM, section0 needs to be copied separately to inject 3ds_injector + u32 sectionNum; + if(firmType == NATIVE_FIRM) + { + copySection0AndInjectSystemModules(); + sectionNum = 1; + } + else sectionNum = 0; +#endif //Copy FIRM sections to respective memory locations for(; sectionNum < 4 && section[sectionNum].size; sectionNum++) diff --git a/source/firm.h b/source/firm.h index 172dff3..62bdcbd 100644 --- a/source/firm.h +++ b/source/firm.h @@ -52,6 +52,10 @@ static inline void patchNativeFirm(u32 firmVersion, FirmwareSource nandType, u32 static inline void patchLegacyFirm(FirmwareType firmType); static inline void patch1x2xNativeAndSafeFirm(void); +#ifdef DEV static inline void copySection0AndInjectSystemModules(FirmwareType firmType); +#else +static inline void copySection0AndInjectSystemModules(void); +#endif static inline void launchFirm(FirmwareType firmType); \ No newline at end of file diff --git a/source/patches.c b/source/patches.c index 5b76e14..ade20fb 100644 --- a/source/patches.c +++ b/source/patches.c @@ -45,6 +45,7 @@ u8 *getProcess9(u8 *pos, u32 size, u32 *process9Size, u32 *process9MemAddr) return off - 0x204 + (*(u32 *)(off - 0x64) * 0x200) + 0x200; } +#ifdef DEV u32 *getKernel11Info(u8 *pos, u32 size, u32 *baseK11VA, u8 **freeK11Space, u32 **arm11SvcHandler, u32 **arm11ExceptionsPage) { const u8 pattern[] = {0x00, 0xB0, 0x9C, 0xE5}; @@ -64,6 +65,26 @@ u32 *getKernel11Info(u8 *pos, u32 size, u32 *baseK11VA, u8 **freeK11Space, u32 * return arm11SvcTable; } +#else +u32 *getKernel11Info(u8 *pos, u32 size, u32 *baseK11VA, u8 **freeK11Space) +{ + const u8 pattern[] = {0x00, 0xB0, 0x9C, 0xE5}; + + u32 *arm11ExceptionsPage = (u32 *)memsearch(pos, pattern, size, sizeof(pattern)) - 0xB; + + u32 svcOffset = (-((arm11ExceptionsPage[2] & 0xFFFFFF) << 2) & (0xFFFFFF << 2)) - 8; //Branch offset + 8 for prefetch + u32 pointedInstructionVA = 0xFFFF0008 - svcOffset; + *baseK11VA = pointedInstructionVA & 0xFFFF0000; //This assumes that the pointed instruction has an offset < 0x10000, iirc that's always the case + u32 *arm11SvcTable = (u32 *)(pos + *(u32 *)(pos + pointedInstructionVA - *baseK11VA + 8) - *baseK11VA); //SVC handler address + while(*arm11SvcTable) arm11SvcTable++; //Look for SVC0 (NULL) + + const u8 pattern2[] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF}; + + *freeK11Space = memsearch(pos, pattern2, size, sizeof(pattern2)) + 1; + + return arm11SvcTable; +} +#endif void patchSignatureChecks(u8 *pos, u32 size) { @@ -166,9 +187,9 @@ void implementSvcGetCFWInfo(u8 *pos, u32 *arm11SvcTable, u32 baseK11VA, u8 **fre else isRelease = rev[4] == 0; #ifdef DEV - info->flags = 1 /* dev build */ | ((isRelease ? 1 : 0) << 1) /* is release */; + info->flags = 1 /* dev branch */ | ((isRelease ? 1 : 0) << 1) /* is release */; #else - info->flags = 0 /* non-dev build */ | ((isRelease ? 1 : 0) << 1) /* is release */; + info->flags = 0 /* master branch */ | ((isRelease ? 1 : 0) << 1) /* is release */; #endif arm11SvcTable[0x2E] = baseK11VA + *freeK11Space - pos; //Stubbed svc diff --git a/source/patches.h b/source/patches.h index 2e845dc..4f63d11 100644 --- a/source/patches.h +++ b/source/patches.h @@ -51,11 +51,19 @@ typedef struct __attribute__((packed)) u32 config; } CFWInfo; -extern bool isN3DS, isDevUnit; +extern bool isN3DS; + +#ifdef DEV +extern bool isDevUnit; +#endif u8 *getProcess9(u8 *pos, u32 size, u32 *process9Size, u32 *process9MemAddr); +#ifdef DEV u32 *getKernel11Info(u8 *pos, u32 size, u32 *baseK11VA, u8 **freeK11Space, u32 **arm11SvcHandler, u32 **arm11ExceptionsPage); +#else +u32 *getKernel11Info(u8 *pos, u32 size, u32 *baseK11VA, u8 **freeK11Space); +#endif void patchSignatureChecks(u8 *pos, u32 size); void patchTitleInstallMinVersionCheck(u8 *pos, u32 size);