From 7f937331075ed06ddef949ada6c148a7b9fcb4d9 Mon Sep 17 00:00:00 2001 From: Aurora Date: Tue, 30 Aug 2016 02:18:32 +0200 Subject: [PATCH 01/10] Rewrite the module copying function --- source/firm.c | 39 ++++++++++++++++++--------------------- source/memory.c | 2 +- 2 files changed, 19 insertions(+), 22 deletions(-) diff --git a/source/firm.c b/source/firm.c index fe7629f..53fc7db 100755 --- a/source/firm.c +++ b/source/firm.c @@ -351,33 +351,30 @@ static inline void patch2xNativeAndSafeFirm(void) static inline void copySection0AndInjectSystemModules(void) { - u8 *arm11Section0 = (u8 *)firm + section[0].offset; + u32 srcModuleSize, + dstModuleSize; - struct + for(u8 *src = (u8 *)firm + section[0].offset, *srcEnd = src + section[0].size, *dst = section[0].address; + src < srcEnd; src += srcModuleSize, dst += dstModuleSize) { - u32 size; - const u8 *addr; - } modules[5]; + srcModuleSize = *(u32 *)(src + 0x104) * 0x200; + char *moduleName = (char *)(src + 0x200); - u32 n = 0, - loaderIndex; - u8 *pos = arm11Section0; + void *module; - for(u8 *end = pos + section[0].size; pos < end; pos += modules[n++].size) - { - modules[n].addr = pos; - modules[n].size = *(u32 *)(pos + 0x104) * 0x200; + if(memcmp(moduleName, "loader", 6) == 0) + { + module = (void *)injector; + dstModuleSize = injector_size; + } + else + { + module = src; + dstModuleSize = srcModuleSize; + } - if(memcmp(modules[n].addr + 0x200, "loader", 7) == 0) loaderIndex = n; + memcpy(dst, module, dstModuleSize); } - - modules[loaderIndex].addr = injector; - modules[loaderIndex].size = injector_size; - - pos = section[0].address; - - for(u32 i = 0; i < n; pos += modules[i++].size) - memcpy(pos, modules[i].addr, modules[i].size); } static inline void launchFirm(FirmwareType firmType) diff --git a/source/memory.c b/source/memory.c index e7a05c9..03a36ae 100644 --- a/source/memory.c +++ b/source/memory.c @@ -41,7 +41,7 @@ void memset32(void *dest, u32 filler, u32 size) { u32 *dest32 = (u32 *)dest; - for (u32 i = 0; i < size / 4; i++) + for(u32 i = 0; i < size / 4; i++) dest32[i] = filler; } From f221915a9524dd7fda6957f5dab8606fd973e1c7 Mon Sep 17 00:00:00 2001 From: Aurora Date: Tue, 30 Aug 2016 16:56:19 +0200 Subject: [PATCH 02/10] Get rid of createDirectory and make fileWrite handle directory tree creation --- source/config.c | 6 +----- source/fs.c | 24 +++++++++++++++++------- source/fs.h | 1 - source/pin.c | 6 +----- 4 files changed, 19 insertions(+), 18 deletions(-) diff --git a/source/config.c b/source/config.c index b0fed6a..7518ae4 100644 --- a/source/config.c +++ b/source/config.c @@ -57,11 +57,7 @@ void writeConfig(const char *configPath, u32 configTemp) configData.formatVersionMinor = CONFIG_VERSIONMINOR; if(!fileWrite(&configData, configPath, sizeof(cfgData))) - { - createDirectory("luma"); - if(!fileWrite(&configData, configPath, sizeof(cfgData))) - error("Error writing the configuration file"); - } + error("Error writing the configuration file"); } } diff --git a/source/fs.c b/source/fs.c index f318026..f428bbe 100644 --- a/source/fs.c +++ b/source/fs.c @@ -64,7 +64,9 @@ bool fileWrite(const void *buffer, const char *path, u32 size) { FIL file; - if(f_open(&file, path, FA_WRITE | FA_OPEN_ALWAYS) == FR_OK) + FRESULT result = f_open(&file, path, FA_WRITE | FA_OPEN_ALWAYS); + + if(result == FR_OK) { unsigned int written; f_write(&file, buffer, size, &written); @@ -72,8 +74,21 @@ bool fileWrite(const void *buffer, const char *path, u32 size) return true; } + else if(result == FR_NO_PATH) + { + char folder[256]; - return false; + for(u32 i = 1; path[i] != 0; i++) + if(path[i] == '/') + { + memcpy(folder, path, i); + folder[i] = 0; + f_mkdir(folder); + } + + return fileWrite(buffer, path, size); + } + else return false; } void fileDelete(const char *path) @@ -81,11 +96,6 @@ void fileDelete(const char *path) f_unlink(path); } -void createDirectory(const char *path) -{ - f_mkdir(path); -} - void loadPayload(u32 pressed) { const char *pattern; diff --git a/source/fs.h b/source/fs.h index 7e7d9a9..d74f24f 100644 --- a/source/fs.h +++ b/source/fs.h @@ -33,6 +33,5 @@ u32 fileRead(void *dest, const char *path); u32 getFileSize(const char *path); bool fileWrite(const void *buffer, const char *path, u32 size); void fileDelete(const char *path); -void createDirectory(const char *path); void loadPayload(u32 pressed); u32 firmRead(void *dest, u32 firmType); \ No newline at end of file diff --git a/source/pin.c b/source/pin.c index 0f52200..e46a1d3 100644 --- a/source/pin.c +++ b/source/pin.c @@ -96,11 +96,7 @@ void newPin(bool allowSkipping) memcpy(pin.hash, tmp, 32); if(!fileWrite(&pin, "/luma/pin.bin", sizeof(PINData))) - { - createDirectory("luma"); - if(!fileWrite(&pin, "/luma/pin.bin", sizeof(PINData))) - error("Error writing the PIN file"); - } + error("Error writing the PIN file"); } bool verifyPin(void) From a76c15d018264edc46f1c40620b4f597923835c8 Mon Sep 17 00:00:00 2001 From: Aurora Date: Tue, 30 Aug 2016 17:48:41 +0200 Subject: [PATCH 03/10] Introduce a strcat replacement --- source/draw.c | 10 +--------- source/fs.c | 9 +++++---- source/strings.c | 41 +++++++++++++++++++++++++++++++++++++++++ source/strings.h | 28 ++++++++++++++++++++++++++++ 4 files changed, 75 insertions(+), 13 deletions(-) create mode 100644 source/strings.c create mode 100644 source/strings.h diff --git a/source/draw.c b/source/draw.c index a6a99f5..14b8b0e 100644 --- a/source/draw.c +++ b/source/draw.c @@ -26,20 +26,12 @@ */ #include "draw.h" +#include "strings.h" #include "screen.h" #include "utils.h" #include "fs.h" #include "font.h" -static inline int strlen(const char *string) -{ - char *stringEnd = (char *)string; - - while(*stringEnd) stringEnd++; - - return stringEnd - string; -} - bool loadSplash(void) { //Don't delay boot nor init the screens if no splash image is on the SD diff --git a/source/fs.c b/source/fs.c index f428bbe..67890de 100644 --- a/source/fs.c +++ b/source/fs.c @@ -22,6 +22,7 @@ #include "fs.h" #include "memory.h" +#include "strings.h" #include "cache.h" #include "screen.h" #include "fatfs/ff.h" @@ -127,8 +128,8 @@ void loadPayload(u32 pressed) memcpy(loaderAddress, loader, loader_size); - path[14] = '/'; - memcpy(&path[15], info.altname, 13); + concatenateStrings(path, "/"); + concatenateStrings(path, info.altname); loaderAddress[1] = fileRead((void *)0x24F00000, path); @@ -147,7 +148,7 @@ u32 firmRead(void *dest, u32 firmType) { "00000003", "20000003" }}; char path[48] = "1:/title/00040138/00000000/content"; - memcpy(&path[18], firmFolders[firmType][isN3DS ? 1 : 0], 8); + concatenateStrings(path, firmFolders[firmType][isN3DS ? 1 : 0]); DIR dir; FILINFO info; @@ -177,7 +178,7 @@ u32 firmRead(void *dest, u32 firmType) f_closedir(&dir); //Complete the string with the .app name - memcpy(&path[34], "/00000000.app", 14); + concatenateStrings(path, "/00000000.app"); //Last digit of the .app u32 i = 42; diff --git a/source/strings.c b/source/strings.c new file mode 100644 index 0000000..adaa712 --- /dev/null +++ b/source/strings.c @@ -0,0 +1,41 @@ +/* +* This file is part of Luma3DS +* Copyright (C) 2016 Aurora Wright, TuxSH +* +* This program is free software: you can redistribute it and/or modify +* it under the terms of the GNU General Public License as published by +* the Free Software Foundation, either version 3 of the License, or +* (at your option) any later version. +* +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with this program. If not, see . +* +* Additional Terms 7.b of GPLv3 applies to this file: Requiring preservation of specified +* reasonable legal notices or author attributions in that material or in the Appropriate Legal +* Notices displayed by works containing it. +*/ + +#include "strings.h" +#include "memory.h" + +int strlen(const char *string) +{ + char *stringEnd = (char *)string; + + while(*stringEnd) stringEnd++; + + return stringEnd - string; +} + +void concatenateStrings(char *destination, const char *source) +{ + int i = strlen(source), + j = strlen(destination); + + memcpy(&destination[j], source, i + 1); +} \ No newline at end of file diff --git a/source/strings.h b/source/strings.h new file mode 100644 index 0000000..f6b035f --- /dev/null +++ b/source/strings.h @@ -0,0 +1,28 @@ +/* +* This file is part of Luma3DS +* Copyright (C) 2016 Aurora Wright, TuxSH +* +* This program is free software: you can redistribute it and/or modify +* it under the terms of the GNU General Public License as published by +* the Free Software Foundation, either version 3 of the License, or +* (at your option) any later version. +* +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with this program. If not, see . +* +* Additional Terms 7.b of GPLv3 applies to this file: Requiring preservation of specified +* reasonable legal notices or author attributions in that material or in the Appropriate Legal +* Notices displayed by works containing it. +*/ + +#pragma once + +#include "types.h" + +int strlen(const char *string); +void concatenateStrings(char *destination, const char *source); \ No newline at end of file From a2003fba95bc78f20aa808e730e5f0ebda3e1b4c Mon Sep 17 00:00:00 2001 From: Aurora Date: Tue, 30 Aug 2016 19:48:21 +0200 Subject: [PATCH 04/10] Add -O3 optimization for the string functions --- Makefile | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Makefile b/Makefile index e964dd7..cf898ad 100644 --- a/Makefile +++ b/Makefile @@ -34,7 +34,7 @@ objects = $(patsubst $(dir_source)/%.s, $(dir_build)/%.o, \ $(call rwildcard, $(dir_source), *.s *.c))) bundled = $(dir_build)/rebootpatch.h $(dir_build)/emunandpatch.h $(dir_build)/svcGetCFWInfopatch.h $(dir_build)/twl_k11modulespatch.h \ - $(dir_build)/injector.h $(dir_build)/loader.h + $(dir_build)/injector.h $(dir_build)/loader.h .PHONY: all all: launcher a9lh ninjhax @@ -112,7 +112,7 @@ $(dir_build)/loader.h: $(dir_loader)/Makefile @$(MAKE) -C $(dir_loader) @bin2c -o $@ -n loader $(@D)/loader.bin -$(dir_build)/memory.o: CFLAGS += -O3 +$(dir_build)/memory.o $(dir_build)/strings.o: CFLAGS += -O3 $(dir_build)/config.o: CFLAGS += -DCONFIG_TITLE="\"$(name) $(revision) configuration\"" $(dir_build)/patches.o: CFLAGS += -DREVISION=\"$(revision)\" -DCOMMIT_HASH="0x$(commit)" From 5406d648bca3bf66dece5b1554d35d3e40faef1c Mon Sep 17 00:00:00 2001 From: Aurora Date: Tue, 30 Aug 2016 19:58:38 +0200 Subject: [PATCH 05/10] Fix derp --- source/fs.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/source/fs.c b/source/fs.c index 67890de..3eb247c 100644 --- a/source/fs.c +++ b/source/fs.c @@ -147,8 +147,9 @@ u32 firmRead(void *dest, u32 firmType) { "00000202", "20000202" }, { "00000003", "20000003" }}; - char path[48] = "1:/title/00040138/00000000/content"; + char path[48] = "1:/title/00040138/"; concatenateStrings(path, firmFolders[firmType][isN3DS ? 1 : 0]); + concatenateStrings(path, "/content"); DIR dir; FILINFO info; From 5de54d6f9fe7232a138ea1bed719fcdcfa465756 Mon Sep 17 00:00:00 2001 From: Aurora Date: Tue, 30 Aug 2016 21:11:11 +0200 Subject: [PATCH 06/10] Fix derp --- source/exceptions.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/source/exceptions.c b/source/exceptions.c index 81fad25..f7cd559 100644 --- a/source/exceptions.c +++ b/source/exceptions.c @@ -107,7 +107,7 @@ void detectAndProcessExceptionDumps(void) char *pathFolder = dumpHeader->processor == 9 ? "/luma/dumps/arm9" : "/luma/dumps/arm11"; findDumpFile(pathFolder, fileName); - memcpy(path, pathFolder, strlen(pathFolder)); + memcpy(path, pathFolder, strlen(pathFolder) + 1); concatenateStrings(path, "/"); concatenateStrings(path, fileName); From a84f393bd5c17be23160eafdb27cae74a102090d Mon Sep 17 00:00:00 2001 From: Aurora Date: Tue, 30 Aug 2016 21:32:15 +0200 Subject: [PATCH 07/10] Move the itoa function to strings.c --- source/fs.c | 11 +---------- source/strings.c | 14 ++++++++++++++ source/strings.h | 3 ++- 3 files changed, 17 insertions(+), 11 deletions(-) diff --git a/source/fs.c b/source/fs.c index 3eb247c..ff3e692 100644 --- a/source/fs.c +++ b/source/fs.c @@ -181,17 +181,8 @@ u32 firmRead(void *dest, u32 firmType) //Complete the string with the .app name concatenateStrings(path, "/00000000.app"); - //Last digit of the .app - u32 i = 42; - //Convert back the .app name from integer to array - u32 tempVersion = firmVersion; - while(tempVersion) - { - static const char hexDigits[] = "0123456789ABCDEF"; - path[i--] = hexDigits[tempVersion & 0xF]; - tempVersion >>= 4; - } + hexItoa(firmVersion, &path[35]); fileRead(dest, path); diff --git a/source/strings.c b/source/strings.c index adaa712..9d0bb2d 100644 --- a/source/strings.c +++ b/source/strings.c @@ -38,4 +38,18 @@ void concatenateStrings(char *destination, const char *source) j = strlen(destination); memcpy(&destination[j], source, i + 1); +} + +void hexItoa(u32 number, char *out) +{ + const char hexDigits[] = "0123456789ABCDEF"; + u32 i = 0; + + while(number > 0) + { + out[7 - i++] = hexDigits[number & 0xF]; + number >>= 4; + } + + for(; i < 8; i++) out[7 - i] = '0'; } \ No newline at end of file diff --git a/source/strings.h b/source/strings.h index f6b035f..f6ac599 100644 --- a/source/strings.h +++ b/source/strings.h @@ -25,4 +25,5 @@ #include "types.h" int strlen(const char *string); -void concatenateStrings(char *destination, const char *source); \ No newline at end of file +void concatenateStrings(char *destination, const char *source); +void hexItoa(u32 number, char *out); \ No newline at end of file From 61305688fd6971f6a121455d31b80825eec5a7e3 Mon Sep 17 00:00:00 2001 From: Aurora Date: Tue, 30 Aug 2016 21:34:03 +0200 Subject: [PATCH 08/10] Move the itoa function to strings.c --- source/exceptions.c | 14 -------------- 1 file changed, 14 deletions(-) diff --git a/source/exceptions.c b/source/exceptions.c index f7cd559..cfe54fc 100644 --- a/source/exceptions.c +++ b/source/exceptions.c @@ -80,20 +80,6 @@ void installArm11Handlers(u32 *exceptionsPage, u32 stackAddr, u32 codeSetOffset) } } -static void hexItoa(u32 n, char *out) -{ - const char hexDigits[] = "0123456789ABCDEF"; - u32 i = 0; - - while(n > 0) - { - out[7 - i++] = hexDigits[n & 0xF]; - n >>= 4; - } - - for(; i < 8; i++) out[7 - i] = '0'; -} - void detectAndProcessExceptionDumps(void) { volatile ExceptionDumpHeader *dumpHeader = (volatile ExceptionDumpHeader *)0x25000000; From b6d91375a5eff24a23f6fc55785e5f8906e1ac35 Mon Sep 17 00:00:00 2001 From: Aurora Date: Tue, 30 Aug 2016 21:50:04 +0200 Subject: [PATCH 09/10] Minor cleanup --- source/exceptions.c | 4 ++-- source/exceptions.h | 2 +- source/firm.c | 5 ++--- source/fs.c | 5 +---- source/patches.c | 12 ++++++------ source/patches.h | 4 ++-- 6 files changed, 14 insertions(+), 18 deletions(-) diff --git a/source/exceptions.c b/source/exceptions.c index cfe54fc..11fd75a 100644 --- a/source/exceptions.c +++ b/source/exceptions.c @@ -47,7 +47,7 @@ void installArm9Handlers(void) } } -void installArm11Handlers(u32 *exceptionsPage, u32 stackAddr, u32 codeSetOffset) +void installArm11Handlers(u32 *exceptionsPage, u32 stackAddress, u32 codeSetOffset) { u32 *initFPU; for(initFPU = exceptionsPage; initFPU < (exceptionsPage + 0x400) && (initFPU[0] != 0xE59F0008 || initFPU[1] != 0xE5900000); initFPU++); @@ -70,7 +70,7 @@ void installArm11Handlers(u32 *exceptionsPage, u32 stackAddr, u32 codeSetOffset) { switch(*pos) //Perform relocations { - case 0xFFFF3000: *pos = stackAddr; break; + case 0xFFFF3000: *pos = stackAddress; break; case 0xEBFFFFFE: *pos = MAKE_BRANCH_LINK(pos, initFPU); break; case 0xEAFFFFFE: *pos = MAKE_BRANCH(pos, mcuReboot); break; case 0xE12FFF1C: pos[1] = 0xFFFF0000 + 4 * (u32)(freeSpace - exceptionsPage) + pos[1] - 32; break; //bx r12 (mainHandler) diff --git a/source/exceptions.h b/source/exceptions.h index 0d6d8ba..f21d91a 100644 --- a/source/exceptions.h +++ b/source/exceptions.h @@ -43,5 +43,5 @@ typedef struct __attribute__((packed)) } ExceptionDumpHeader; void installArm9Handlers(void); -void installArm11Handlers(u32 *exceptionsPage, u32 stackAddr, u32 codeSetOffset); +void installArm11Handlers(u32 *exceptionsPage, u32 stackAddress, u32 codeSetOffset); void detectAndProcessExceptionDumps(void); \ No newline at end of file diff --git a/source/firm.c b/source/firm.c index a251a48..375ec64 100755 --- a/source/firm.c +++ b/source/firm.c @@ -348,9 +348,8 @@ static inline void patchNativeFirm(u32 firmVersion, FirmwareSource nandType, u32 if(DEV_OPTIONS != 2) { //Install arm11 exception handlers - u32 stackAddress, - codeSetOffset; - getInfoForArm11ExceptionHandlers(arm11Section1, section[1].size, &stackAddress, &codeSetOffset); + u32 codeSetOffset; + u32 stackAddress = getInfoForArm11ExceptionHandlers(arm11Section1, section[1].size, &codeSetOffset); installArm11Handlers(arm11ExceptionsPage, stackAddress, codeSetOffset); //Kernel9/Process9 debugging diff --git a/source/fs.c b/source/fs.c index e8adf2c..dd20a5a 100644 --- a/source/fs.c +++ b/source/fs.c @@ -110,8 +110,7 @@ void loadPayload(u32 pressed) else if(pressed & BUTTON_R1) pattern = PATTERN("r"); else if(pressed & BUTTON_A) pattern = PATTERN("a"); else if(pressed & BUTTON_START) pattern = PATTERN("start"); - else if(pressed & BUTTON_SELECT) pattern = PATTERN("select"); - else pattern = "nlc.bin"; + else pattern = PATTERN("select"); DIR dir; FILINFO info; @@ -133,8 +132,6 @@ void loadPayload(u32 pressed) concatenateStrings(path, info.altname); loaderAddress[1] = fileRead((void *)0x24F00000, path); - - if(pattern[0] == 'n') f_unlink(path); flushDCacheRange(loaderAddress, loader_size); flushICacheRange(loaderAddress, loader_size); diff --git a/source/patches.c b/source/patches.c index b9129a0..425e63c 100644 --- a/source/patches.c +++ b/source/patches.c @@ -237,19 +237,19 @@ void patchTwlBg(u8 *pos) src2[1] = 0xE800 | ((((u32)dst - (u32)src2 - 4) & 0xFFF) >> 1); } -void getInfoForArm11ExceptionHandlers(u8 *pos, u32 size, u32 *stackAddr, u32 *codeSetOffset) +u32 getInfoForArm11ExceptionHandlers(u8 *pos, u32 size, u32 *codeSetOffset) { //This function has to succeed. Crash if it doesn't (we'll get an exception dump of it anyways) const u8 callExceptionDispatcherPattern[] = {0x0F, 0x00, 0xBD, 0xE8, 0x13, 0x00, 0x02, 0xF1}; const u8 getTitleIDFromCodeSetPattern[] = {0xDC, 0x05, 0xC0, 0xE1, 0x20, 0x04, 0xA0, 0xE1}; - - *stackAddr = *((u32 *)memsearch(pos, callExceptionDispatcherPattern, size, 8) + 3); - + u32 *loadCodeSet = (u32 *)memsearch(pos, getTitleIDFromCodeSetPattern, size, 8); while((*loadCodeSet >> 20) != 0xE59 || ((*loadCodeSet >> 12) & 0xF) != 0) //ldr r0, [rX, #offset] loadCodeSet--; *codeSetOffset = *loadCodeSet & 0xFFF; + + return *((u32 *)memsearch(pos, callExceptionDispatcherPattern, size, 8) + 3); } void patchArm9ExceptionHandlersInstall(u8 *pos, u32 size) @@ -286,7 +286,7 @@ void patchArm9ExceptionHandlersInstall(u8 *pos, u32 size) } } -void patchSvcBreak9(u8 *pos, u32 size, u32 k9Address) +void patchSvcBreak9(u8 *pos, u32 size, u32 kernel9Address) { //Stub svcBreak with "bkpt 65535" so we can debug the panic. //Thanks @yellows8 and others for mentioning this idea on #3dsdev. @@ -295,7 +295,7 @@ void patchSvcBreak9(u8 *pos, u32 size, u32 k9Address) u32 *arm9SvcTable = (u32 *)memsearch(pos, svcHandlerPattern, size, 4); while(*arm9SvcTable) arm9SvcTable++; //Look for SVC0 (NULL) - u32 *addr = (u32 *)(pos + arm9SvcTable[0x3C] - k9Address); + u32 *addr = (u32 *)(pos + arm9SvcTable[0x3C] - kernel9Address); *addr = 0xE12FFF7F; } diff --git a/source/patches.h b/source/patches.h index fe80a94..a80a393 100644 --- a/source/patches.h +++ b/source/patches.h @@ -61,9 +61,9 @@ void implementSvcGetCFWInfo(u8 *pos, u32 *arm11SvcTable, u8 **freeK11Space); void applyLegacyFirmPatches(u8 *pos, FirmwareType firmType); void patchTwlBg(u8 *pos); -void getInfoForArm11ExceptionHandlers(u8 *pos, u32 size, u32 *stackAddr, u32 *codeSetOffset); +u32 getInfoForArm11ExceptionHandlers(u8 *pos, u32 size, u32 *codeSetOffset); void patchArm9ExceptionHandlersInstall(u8 *pos, u32 size); -void patchSvcBreak9(u8 *pos, u32 size, u32 k9Address); +void patchSvcBreak9(u8 *pos, u32 size, u32 kernel9Address); void patchSvcBreak11(u8 *pos, u32 *arm11SvcTable); void patchKernel9Panic(u8 *pos, u32 size, FirmwareType firmType); void patchKernel11Panic(u8 *pos, u32 size); From 7e8cf84c42a8b8661336cd3f438f0e197f78f376 Mon Sep 17 00:00:00 2001 From: Aurora Date: Tue, 30 Aug 2016 22:41:27 +0200 Subject: [PATCH 10/10] Fix yet another derp --- source/patches.c | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/source/patches.c b/source/patches.c index 425e63c..6c27f31 100644 --- a/source/patches.c +++ b/source/patches.c @@ -343,10 +343,6 @@ void patchK11ModuleChecks(u8 *pos, u32 size, u8 **freeK11Space) { //We have to detour a function in the ARM11 kernel because builtin modules //are compressed in memory and are only decompressed at runtime. - - //Inject our code into the free space - memcpy(*freeK11Space, k11modules, k11modules_size); - (*freeK11Space) += k11modules_size; //Find the code that decompresses the .code section of the builtin modules and detour it with a jump to our code const u8 pattern[] = { 0x00, 0x00, 0x94, 0xE5, 0x18, 0x10, 0x90, 0xE5, 0x28, 0x20, @@ -357,11 +353,16 @@ void patchK11ModuleChecks(u8 *pos, u32 size, u8 **freeK11Space) //We couldn't find the code that decompresses the module if(off == NULL) return; + //Inject our code into the free space + memcpy(*freeK11Space, k11modules, k11modules_size); + //Inject a jump instruction to our code at the offset we found //Construct a jump (BL) instruction to our code u32 offset = ((((u32)*freeK11Space) - ((u32)off + 8)) >> 2) & 0xFFFFFF; *off = offset | (1 << 24) | (0x5 << 25) | (0xE << 28); + + (*freeK11Space) += k11modules_size; } void patchP9AccessChecks(u8 *pos, u32 size)