From 1e3362250f3374c414ca844de57981fe622e1ea3 Mon Sep 17 00:00:00 2001 From: Aurora Date: Thu, 17 Nov 2016 15:38:28 +0100 Subject: [PATCH] Cleanup, fixed exceptions derp, support RomFS from CTRNAND, patching features for NAND titles --- injector/patches/romfsredir.s | 19 +++++++-------- injector/source/patcher.c | 46 ++++++++++++++++++----------------- source/exceptions.c | 24 +++++++++--------- 3 files changed, 45 insertions(+), 44 deletions(-) diff --git a/injector/patches/romfsredir.s b/injector/patches/romfsredir.s index bd76583..b657313 100644 --- a/injector/patches/romfsredir.s +++ b/injector/patches/romfsredir.s @@ -15,7 +15,6 @@ cmp r3, #3 beq openRomfs load r12, fsOpenFileDirectly - add r12, r12, #4 nop ; Will be replaced with the original function opcode bx r12 @@ -42,7 +41,7 @@ str r12, [sp, #0x10] ; File DataPointer load r12, romfsFileNameSize str r12, [sp, #0x14] ; File PathSize - mov r3, #9 ; SDMC Archive ID + load r3, archive bl openFileDirectlyHook sub sp, sp, #0x5C ldmfd sp!, {r0, r1, lr} @@ -51,7 +50,6 @@ ; Once we have the sd romfs file opened, we'll open a subfile ; in order to skip the useless data. - fsOpenSubFile: stmfd sp!, {r1, r3-r11} mrc p15, 0, r4, c13, c0, 3 add r4, r4, #0x80 @@ -70,12 +68,13 @@ .pool .align 4 ; Part of these symbols will be set from outside - fsOpenFileDirectly : .word 0x00000000 + fsOpenFileDirectly : .word 0 fsOpenSubFileCmd : .word 0x08010100 - .word 0x00000000 ; File Offset - .word 0x00000000 - .word 0x00000000 ; File Size - .word 0x00000000 - romfsFileNameSize : .word 0x00000000 - romfsFileName : .word 0x00000000 ; File DataPointer + .word 0 ; File Offset + .word 0 + .word 0 ; File Size + .word 0 + archive : .word 0 + romfsFileNameSize : .word 0 + romfsFileName : .word 0 ; File DataPointer .close \ No newline at end of file diff --git a/injector/source/patcher.c b/injector/source/patcher.c index 3afd5bf..a066b8b 100644 --- a/injector/source/patcher.c +++ b/injector/source/patcher.c @@ -39,13 +39,14 @@ static Result fileOpen(IFile *file, FS_ArchiveID archiveId, const char *path, in return IFile_Open(file, archiveId, archivePath, filePath, flags); } -static Result openLumaFile(IFile *file, const char *path) +static u32 openLumaFile(IFile *file, const char *path) { Result res = fileOpen(file, ARCHIVE_SDMC, path, FS_OPEN_READ); - if((u32)res == 0xC88044AB) res = fileOpen(file, ARCHIVE_NAND_RW, path, FS_OPEN_READ); //Returned if SD is not mounted + if(R_SUCCEEDED(res)) return ARCHIVE_SDMC; - return res; + //Returned if SD is not mounted + return (u32)res == 0xC88044AB && R_SUCCEEDED(fileOpen(file, ARCHIVE_NAND_RW, path, FS_OPEN_READ)) ? ARCHIVE_NAND_RW : 0; } static inline void loadCFWInfo(void) @@ -89,7 +90,7 @@ static inline void loadCustomVerString(u16 *out, u32 *verStringSize, u32 current IFile file; - if(R_FAILED(openLumaFile(&file, paths[currentNand]))) return; + if(!openLumaFile(&file, paths[currentNand])) return; u64 fileSize; @@ -301,7 +302,7 @@ static inline bool loadTitleCodeSection(u64 progId, u8 *code, u32 size) IFile file; - if(R_FAILED(openLumaFile(&file, path))) return true; + if(!openLumaFile(&file, path)) return true; bool ret; u64 fileSize; @@ -329,7 +330,7 @@ static inline bool loadTitleLocaleConfig(u64 progId, u8 *regionId, u8 *languageI IFile file; - if(R_FAILED(openLumaFile(&file, path))) return true; + if(!openLumaFile(&file, path)) return true; bool ret = false; u64 fileSize; @@ -383,8 +384,9 @@ static inline bool patchRomfsRedirection(u64 progId, u8* code, u32 size) progIdToStr(path + 28, progId); IFile file; + u32 archive = openLumaFile(&file, path); - if(R_FAILED(openLumaFile(&file, path))) return true; + if(!archive) return true; bool ret = false; u64 romfsSize; @@ -397,33 +399,33 @@ static inline bool patchRomfsRedirection(u64 progId, u8* code, u32 size) if(R_FAILED(IFile_Read(&file, &total, &magic, 4)) || total != 4 || magic != 0x43465649) goto exit; u32 fsOpenFileDirectly = findFunctionCommand(code, size, 0x08030204), - fsOpenLinkFile = findFunctionCommand(code, size, 0x80C0000), throwFatalError = findThrowFatalError(code, size); if(fsOpenFileDirectly == 0xFFFFFFFF || throwFatalError == 0xFFFFFFFF) goto exit; //Setup the payload - memcpy(code + throwFatalError, romfsredir_bin, romfsredir_bin_size); - *((u32 *)(code + throwFatalError + 0x10)) = *(u32 *)(code + fsOpenFileDirectly); - *((u32 *)(code + throwFatalError + romfsredir_bin_size - 0x08)) = sizeof(path); - *((u64 *)(code + throwFatalError + romfsredir_bin_size - 0x10)) = romfsSize - 0x1000ULL; - *((u64 *)(code + throwFatalError + romfsredir_bin_size - 0x18)) = 0x1000ULL; - *((u32 *)(code + throwFatalError + romfsredir_bin_size - 0x20)) = fsOpenFileDirectly + 0x100000; + u8 *payload = code + throwFatalError; + memcpy(payload, romfsredir_bin, romfsredir_bin_size); + memcpy(payload + romfsredir_bin_size, path, sizeof(path)); + *(u32 *)(payload + 0xC) = *(u32 *)(code + fsOpenFileDirectly); + + u32 *payloadSymbols = (u32 *)(payload + romfsredir_bin_size - 0x24); + payloadSymbols[0] = 0x100000 + fsOpenFileDirectly + 4; + *(u64 *)(payloadSymbols + 2) = 0x1000ULL; + *(u64 *)(payloadSymbols + 4) = romfsSize - 0x1000ULL; + payloadSymbols[6] = archive; + payloadSymbols[7] = sizeof(path); + payloadSymbols[8] = 0x100000 + throwFatalError + romfsredir_bin_size; //String pointer //Place the hooks *(u32 *)(code + fsOpenFileDirectly) = MAKE_BRANCH(fsOpenFileDirectly, throwFatalError); + u32 fsOpenLinkFile = findFunctionCommand(code, size, 0x80C0000); + if(fsOpenLinkFile != 0xFFFFFFFF) { *(u32 *)(code + fsOpenLinkFile) = 0xE3A03003; //mov r3, #3 *(u32 *)(code + fsOpenLinkFile + 4) = MAKE_BRANCH(fsOpenLinkFile + 4, throwFatalError); - memcpy(code + fsOpenLinkFile + 8, path, sizeof(path)); - *(u32 *)(code + throwFatalError + romfsredir_bin_size - 4) = fsOpenLinkFile + 8 + 0x100000; //String pointer - } - else - { - memcpy(code + throwFatalError + romfsredir_bin_size, path, 0x30); - *(u32 *)(code + throwFatalError + romfsredir_bin_size - 4) = throwFatalError + romfsredir_bin_size + 0x100000; //String pointer } ret = true; @@ -670,7 +672,7 @@ void patchCode(u64 progId, u16 progVer, u8 *code, u32 size) ) != 3) goto error; } - else if(CONFIG(PATCHGAMES) && (u32)((progId & 0xFFFFFFF000000000LL) >> 0x24) == 0x0004000) + if(CONFIG(PATCHGAMES) && (u32)((progId >> 0x20) & 0xFFFFFFEDULL) == 0x00040000) { u8 regionId = 0xFF, languageId; diff --git a/source/exceptions.c b/source/exceptions.c index 6739bbf..09c05d0 100644 --- a/source/exceptions.c +++ b/source/exceptions.c @@ -123,20 +123,20 @@ void detectAndProcessExceptionDumps(void) { if((regs[16] & 0x20) == 0 && dumpHeader->codeDumpSize >= 4) { - u32 instr = *(vu32 *)(stackDump - 4); - if(instr == 0xE12FFF7E) drawString(specialExceptions[0], true, 10 + 32 * SPACING_X, posY, COLOR_WHITE); - else if(instr == 0xEF00003C) drawString(specialExceptions[1], true, 10 + 32 * SPACING_X, posY, COLOR_WHITE); + u32 instr = *(vu32 *)(stackDump - 4); + if(instr == 0xE12FFF7E) drawString(specialExceptions[0], true, 10 + 32 * SPACING_X, posY, COLOR_WHITE); + else if(instr == 0xEF00003C) drawString(specialExceptions[1], true, 10 + 32 * SPACING_X, posY, COLOR_WHITE); } else if((regs[16] & 0x20) == 0 && dumpHeader->codeDumpSize >= 2) { - u16 instr = *(vu16 *)(stackDump - 2); - if(instr == 0xDF3C) drawString(specialExceptions[1], true, 10 + 32 * SPACING_X, posY, COLOR_WHITE); + u16 instr = *(vu16 *)(stackDump - 2); + if(instr == 0xDF3C) drawString(specialExceptions[1], true, 10 + 32 * SPACING_X, posY, COLOR_WHITE); } } if(dumpHeader->processor == 11 && dumpHeader->additionalDataSize != 0) { - char processName[] = "Current process: "; + char processName[] = "Current process: "; memcpy(processName + sizeof(processName) - 9, (void *)additionalData, 8); posY = drawString(processName, true, 10, posY + SPACING_Y, COLOR_WHITE); } @@ -151,9 +151,9 @@ void detectAndProcessExceptionDumps(void) if(i != 16 || dumpHeader->processor != 9) { - drawString(registerNames[i + 1], true, 10 + 22 * SPACING_X, posY, COLOR_WHITE); - hexItoa(i == 16 ? regs[20] : regs[i + 1], hexString, 8, true); - drawString(hexString, true, 10 + 29 * SPACING_X, posY, COLOR_WHITE); + drawString(registerNames[i + 1], true, 10 + 22 * SPACING_X, posY, COLOR_WHITE); + hexItoa(i == 16 ? regs[20] : regs[i + 1], hexString, 8, true); + drawString(hexString, true, 10 + 29 * SPACING_X, posY, COLOR_WHITE); } } @@ -173,9 +173,9 @@ void detectAndProcessExceptionDumps(void) for(u32 i = 0; i < 8 && stackDump < additionalData; i++, stackDump++) { - char byteString[] = "00"; - hexItoa(*stackDump, byteString, 2, false); - drawString(byteString, false, 10 + 10 * SPACING_X + 3 * i * SPACING_X, posYBottom, COLOR_WHITE); + char byteString[] = "00"; + hexItoa(*stackDump, byteString, 2, false); + drawString(byteString, false, 10 + 10 * SPACING_X + 3 * i * SPACING_X, posYBottom, COLOR_WHITE); } }