From 158dd1d55ce236f0b093980cf04d6387d8c156a7 Mon Sep 17 00:00:00 2001 From: ahezard Date: Fri, 6 Jan 2017 22:25:55 +0100 Subject: [PATCH 1/8] Give more wram to the bootloader (256k) --- arm9/source/nds_loader_arm9.c | 2 + bootloader/load.ld | 2 +- bootloader/source/arm9clear.arm.c | 2 +- bootloader/source/card_patcher.c | 70 +++++++++++++++++++++++++++---- 4 files changed, 65 insertions(+), 11 deletions(-) diff --git a/arm9/source/nds_loader_arm9.c b/arm9/source/nds_loader_arm9.c index 060c8d230..07e7d8306 100644 --- a/arm9/source/nds_loader_arm9.c +++ b/arm9/source/nds_loader_arm9.c @@ -280,6 +280,7 @@ int runNds (const void* loader, u32 loaderSize, u32 cluster, bool initDisc, bool // Direct CPU access to VRAM bank C VRAM_C_CR = VRAM_ENABLE | VRAM_C_LCD; + VRAM_D_CR = VRAM_ENABLE | VRAM_D_LCD; // Load the loader/patcher into the correct address vramcpy (LCDC_BANK_C, loader, loaderSize); @@ -347,6 +348,7 @@ int runNds (const void* loader, u32 loaderSize, u32 cluster, bool initDisc, bool nocashMessage("Give the VRAM to the ARM7"); // Give the VRAM to the ARM7 VRAM_C_CR = VRAM_ENABLE | VRAM_C_ARM7_0x06000000; + VRAM_D_CR = VRAM_ENABLE | VRAM_D_ARM7_0x06020000; nocashMessage("Reset into a passme loop"); // Reset into a passme loop diff --git a/bootloader/load.ld b/bootloader/load.ld index 53f37287e..b66aefef8 100644 --- a/bootloader/load.ld +++ b/bootloader/load.ld @@ -4,7 +4,7 @@ ENTRY(_start) MEMORY { - vram : ORIGIN = 0x06000000, LENGTH = 128K + vram : ORIGIN = 0x06000000, LENGTH = 256K } __vram_start = ORIGIN(vram); diff --git a/bootloader/source/arm9clear.arm.c b/bootloader/source/arm9clear.arm.c index c08809ee2..c9fdcf28a 100644 --- a/bootloader/source/arm9clear.arm.c +++ b/bootloader/source/arm9clear.arm.c @@ -49,7 +49,7 @@ void __attribute__ ((long_call)) __attribute__((naked)) __attribute__((noreturn) VRAM_B_CR = 0; // Don't mess with the ARM7's VRAM // VRAM_C_CR = 0; - VRAM_D_CR = 0; +// VRAM_D_CR = 0; VRAM_E_CR = 0; VRAM_F_CR = 0; VRAM_G_CR = 0; diff --git a/bootloader/source/card_patcher.c b/bootloader/source/card_patcher.c index 998b3de91..178f1274f 100644 --- a/bootloader/source/card_patcher.c +++ b/bootloader/source/card_patcher.c @@ -63,7 +63,7 @@ u32 arenaLowSignature[4] = {0xE1A00100,0xE2800627,0xE2800AFF,0xE5801DA0}; // // Look in @data for @find and return the position of it. // -u32 getOffsetA9(u32* addr, size_t size, u32* find, size_t sizeofFind, int direction) +u32 getOffset(u32* addr, size_t size, u32* find, size_t sizeofFind, int direction) { u32* end = addr + size/sizeof(u32); u32* debug = (u32*)0x037D0000; @@ -94,7 +94,7 @@ u32 getOffsetA9(u32* addr, size_t size, u32* find, size_t sizeofFind, int direct module_params_t* findModuleParams(const tNDSHeader* ndsHeader) { nocashMessage("Looking for moduleparams\n"); - uint32_t moduleparams = getOffsetA9((u32*)ndsHeader->arm9destination, ndsHeader->arm9binarySize, (u32*)moduleParamsSignature, 2, 1); + uint32_t moduleparams = getOffset((u32*)ndsHeader->arm9destination, ndsHeader->arm9binarySize, (u32*)moduleParamsSignature, 2, 1); if(!moduleparams) { nocashMessage("No moduleparams?\n"); @@ -174,7 +174,7 @@ u32 patchCardNdsArm9 (const tNDSHeader* ndsHeader, u32* cardEngineLocation, modu // Find the card read u32 cardReadEndOffset = - getOffsetA9((u32*)ndsHeader->arm9destination, 0x00300000,//ndsHeader->arm9binarySize, + getOffset((u32*)ndsHeader->arm9destination, 0x00300000,//ndsHeader->arm9binarySize, (u32*)a9cardReadSignature, 2, 1); if (!cardReadEndOffset) { nocashMessage("Card read end not found\n"); @@ -182,7 +182,7 @@ u32 patchCardNdsArm9 (const tNDSHeader* ndsHeader, u32* cardEngineLocation, modu } debug[1] = cardReadEndOffset; u32 cardReadStartOffset = - getOffsetA9((u32*)cardReadEndOffset, -0xF9, + getOffset((u32*)cardReadEndOffset, -0xF9, (u32*)cardReadStartSignature, 1, -1); if (!cardReadStartOffset) { nocashMessage("Card read start not found\n"); @@ -192,7 +192,7 @@ u32 patchCardNdsArm9 (const tNDSHeader* ndsHeader, u32* cardEngineLocation, modu nocashMessage("Card read found\n"); u32 cardPullOutOffset = - getOffsetA9((u32*)ndsHeader->arm9destination, 0x00300000,//, ndsHeader->arm9binarySize, + getOffset((u32*)ndsHeader->arm9destination, 0x00300000,//, ndsHeader->arm9binarySize, (u32*)cardPullOutSignature, 4, 1); if (!cardPullOutOffset) { nocashMessage("Card pull out handler not found\n"); @@ -203,7 +203,7 @@ u32 patchCardNdsArm9 (const tNDSHeader* ndsHeader, u32* cardEngineLocation, modu u32 cardReadCachedEndOffset = - getOffsetA9((u32*)ndsHeader->arm9destination, 0x00300000,//ndsHeader->arm9binarySize, + getOffset((u32*)ndsHeader->arm9destination, 0x00300000,//ndsHeader->arm9binarySize, (u32*)cardReadCachedEndSignature, 4, 1); if (!cardReadCachedEndOffset) { nocashMessage("Card read cached end not found\n"); @@ -211,7 +211,7 @@ u32 patchCardNdsArm9 (const tNDSHeader* ndsHeader, u32* cardEngineLocation, modu } debug[1] = cardReadCachedEndOffset; u32 cardReadCachedOffset = - getOffsetA9((u32*)cardReadCachedEndOffset, -0xFF, + getOffset((u32*)cardReadCachedEndOffset, -0xFF, (u32*)cardReadCachedStartSignature, 2, -1); if (!cardReadStartOffset) { nocashMessage("Card read cached start not found\n"); @@ -309,6 +309,56 @@ u32 patchCardNdsArm9 (const tNDSHeader* ndsHeader, u32* cardEngineLocation, modu return 0; } +u32 savePatchV1 (const tNDSHeader* ndsHeader, u32* cardEngineLocation, module_params_t* moduleParams) { + + u32 alignedA7size = (ndsHeader->arm7binarySize + 511) & 0xFFFFFE00; + + // Find the relocation signature + u32 relocationStart = getOffset((u32*)ndsHeader->arm7destination, alignedA7size, + relocateStartSignature, 4, 1); + if (!relocationStart) { + nocashMessage("Relocation start not found\n"); + return 0; + } + + // Validate the relocation signature + u32 forwardedRelocStartAddr = relocationStart + 4; + if (!*(u32*)forwardedRelocStartAddr) + forwardedRelocStartAddr += 4; + u32 vAddrOfRelocSrc = + *(u32*)(forwardedRelocStartAddr + 8); + // sanity checks + u32 relocationCheck1 = + *(u32*)(forwardedRelocStartAddr + 0xC); + u32 relocationCheck2 = + *(u32*)(forwardedRelocStartAddr + 0x10); + if ( vAddrOfRelocSrc != relocationCheck1 + || vAddrOfRelocSrc != relocationCheck2) { + nocashMessage("Error in relocation checking\n"); + return 0; + } + + + // Get the remaining details regarding relocation + u32 valueAtRelocStart = + *(u32*)forwardedRelocStartAddr; + u32 relocDestAtSharedMem = + *(u32*)valueAtRelocStart; + if (relocDestAtSharedMem != 0x37F8000) { // shared memory in RAM + // Try again + vAddrOfRelocSrc += + *(u32*)valueAtRelocStart + 4; + relocDestAtSharedMem = + *(u32*)valueAtRelocStart + 0xC; + if (relocDestAtSharedMem != 0x37F8000) { + printf("Error in finding shared memory relocation area\n"); + return 0; + } + } + printf("Relocation src:\t%08X\n", vAddrOfRelocSrc); + printf("Relocation dst:\t%08X\n", relocDestAtSharedMem); +} + u32 patchCardNdsArm7 (const tNDSHeader* ndsHeader, u32* cardEngineLocation, module_params_t* moduleParams) { u32* debug = (u32*)0x037D0000; @@ -318,7 +368,7 @@ u32 patchCardNdsArm7 (const tNDSHeader* ndsHeader, u32* cardEngineLocation, modu } u32 cardCheckPullOutOffset = - getOffsetA9((u32*)ndsHeader->arm7destination, 0x00400000,//, ndsHeader->arm9binarySize, + getOffset((u32*)ndsHeader->arm7destination, 0x00400000,//, ndsHeader->arm9binarySize, (u32*)cardCheckPullOutSignature, 4, 1); if (!cardCheckPullOutOffset) { nocashMessage("Card check pull out not found\n"); @@ -329,7 +379,7 @@ u32 patchCardNdsArm7 (const tNDSHeader* ndsHeader, u32* cardEngineLocation, modu } u32 cardIrqEnableOffset = - getOffsetA9((u32*)ndsHeader->arm7destination, 0x00400000,//, ndsHeader->arm9binarySize, + getOffset((u32*)ndsHeader->arm7destination, 0x00400000,//, ndsHeader->arm9binarySize, (u32*)irqEnableStartSignature, 4, 1); if (!cardIrqEnableOffset) { nocashMessage("irq enable not found\n"); @@ -347,6 +397,8 @@ u32 patchCardNdsArm7 (const tNDSHeader* ndsHeader, u32* cardEngineLocation, modu copyLoop ((u32*)cardIrqEnableOffset, cardIrqEnablePatch, 0x30); + savePatchV1(ndsHeader, cardEngineLocation, moduleParams); + nocashMessage("ERR_NONE"); return 0; } From 96fc30c7cef93f6185e8836f46f93db4ff7bf726 Mon Sep 17 00:00:00 2001 From: ahezard Date: Sat, 7 Jan 2017 00:18:05 +0100 Subject: [PATCH 2/8] Fix LOCK_ARM9_SCFG_EXT --- arm9/source/main.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/arm9/source/main.cpp b/arm9/source/main.cpp index 28049707a..759afafbe 100644 --- a/arm9/source/main.cpp +++ b/arm9/source/main.cpp @@ -230,7 +230,7 @@ int main( int argc, char **argv) { if(bootstrapini.GetInt("NDS-BOOTSTRAP","LOCK_ARM9_SCFG_EXT",0) == 1) { dbg_printf("ARM9_SCFG_EXT locked\n"); - REG_SCFG_EXT = 0x03000000; // NAND/SD Access + REG_SCFG_EXT &= 0x7FFFFFFF; // Only lock bit 31 } if(bootstrapini.GetInt("NDS-BOOTSTRAP","NTR_MODE_SWITCH",0) == 1) { From 463df82b55e18e28bbc9caf115e7a5bdcd1d4517 Mon Sep 17 00:00:00 2001 From: ahezard Date: Sat, 7 Jan 2017 01:56:47 +0100 Subject: [PATCH 3/8] Reintregrate dldi support --- bootloader/source/boot.c | 39 +++++++++++--------- bootloader/source/card_patcher.c | 62 +++++++++++++++++++++++++++++--- bootloader/source/hook.c | 43 +++++++++++++++++++++- bootloader/source/hook.h | 4 ++- 4 files changed, 124 insertions(+), 24 deletions(-) diff --git a/bootloader/source/boot.c b/bootloader/source/boot.c index 7ec6548bb..53a6d4556 100644 --- a/bootloader/source/boot.c +++ b/bootloader/source/boot.c @@ -335,27 +335,32 @@ int main (void) { nocashMessage("Load the NDS file"); loadBinary_ARM7(fileCluster); - // Patch with DLDI if desired - //if (wantToPatchDLDI) { - // nocashMessage("wantToPatchDLDI"); - // dldiPatchBinary ((u8*)((u32*)NDS_HEAD)[0x0A], ((u32*)NDS_HEAD)[0x0B]); - //} + wantToPatchDLDI = wantToPatchDLDI && ((u32*)NDS_HEAD)[0x084] > 0x200; - copyLoop (SD_ENGINE_LOCATION, (u32*)cardengine_bin, cardengine_bin_size); + // Patch with DLDI if desired + if (wantToPatchDLDI) { + nocashMessage("wantToPatchDLDI"); + wantToPatchDLDI = wantToPatchDLDI && dldiPatchBinary ((u8*)((u32*)NDS_HEAD)[0x0A], ((u32*)NDS_HEAD)[0x0B]); + if (wantToPatchDLDI) { + // Find the DLDI reserved space in the file + u32 patchOffset = quickFind ((u8*)((u32*)NDS_HEAD)[0x0A], dldiMagicString, ((u32*)NDS_HEAD)[0x0B], sizeof(dldiMagicString)); + u32* wordCommandAddr = (u32 *) (((u32)((u32*)NDS_HEAD)[0x0A])+patchOffset+0x80); + + } + } else { + copyLoop (SD_ENGINE_LOCATION, (u32*)cardengine_bin, cardengine_bin_size); - module_params_t* params = findModuleParams(NDS_HEAD); - if(params) - { - ensureArm9Decompressed(NDS_HEAD, params); + module_params_t* params = findModuleParams(NDS_HEAD); + if(params) + { + ensureArm9Decompressed(NDS_HEAD, params); + } + + patchCardNds(NDS_HEAD,SD_ENGINE_LOCATION,params); + + hookNdsRetail(NDS_HEAD, fileCluster, (const u32*)CHEAT_DATA_LOCATION, (u32*)CHEAT_ENGINE_LOCATION, (u32*)SD_ENGINE_LOCATION); } - patchCardNds(NDS_HEAD,SD_ENGINE_LOCATION,params); - - // Find the DLDI reserved space in the file - //u32 patchOffset = quickFind ((u8*)((u32*)NDS_HEAD)[0x0A], dldiMagicString, ((u32*)NDS_HEAD)[0x0B], sizeof(dldiMagicString)); - u32* wordCommandAddr = (u32 *) 0x2051BB4; - - hookNds(NDS_HEAD, fileCluster, (const u32*)CHEAT_DATA_LOCATION, (u32*)CHEAT_ENGINE_LOCATION, (u32*)SD_ENGINE_LOCATION, wordCommandAddr); // Pass command line arguments to loaded program //passArgs_ARM7(); diff --git a/bootloader/source/card_patcher.c b/bootloader/source/card_patcher.c index 178f1274f..eb211d7f9 100644 --- a/bootloader/source/card_patcher.c +++ b/bootloader/source/card_patcher.c @@ -309,13 +309,64 @@ u32 patchCardNdsArm9 (const tNDSHeader* ndsHeader, u32* cardEngineLocation, modu return 0; } +u32 savePatchV2 (const tNDSHeader* ndsHeader, u32* cardEngineLocation, module_params_t* moduleParams) { + + nocashMessage("\nArm7 (patch v2.0)\n"); + + // Find the relocation signature + u32 relocationStart = getOffset((u32*)ndsHeader->arm7destination, ndsHeader->arm7binarySize, + relocateStartSignature, 1, 1); + if (!relocationStart) { + nocashMessage("Relocation start not found\n"); + return 0; + } + + // Validate the relocation signature + u32 forwardedRelocStartAddr = relocationStart + 4; + if (!*(u32*)forwardedRelocStartAddr) + forwardedRelocStartAddr += 4; + u32 vAddrOfRelocSrc = + *(u32*)(forwardedRelocStartAddr + 8); + // sanity checks + u32 relocationCheck1 = + *(u32*)(forwardedRelocStartAddr + 0xC); + u32 relocationCheck2 = + *(u32*)(forwardedRelocStartAddr + 0x10); + if ( vAddrOfRelocSrc != relocationCheck1 + || vAddrOfRelocSrc != relocationCheck2) { + nocashMessage("Error in relocation checking\n"); + return 0; + } + + + // Get the remaining details regarding relocation + u32 valueAtRelocStart = + *(u32*)forwardedRelocStartAddr; + u32 relocDestAtSharedMem = + *(u32*)valueAtRelocStart; + if (relocDestAtSharedMem != 0x37F8000) { // shared memory in RAM + // Try again + vAddrOfRelocSrc += + *(u32*)valueAtRelocStart + 4; + relocDestAtSharedMem = + *(u32*)valueAtRelocStart + 0xC; + if (relocDestAtSharedMem != 0x37F8000) { + nocashMessage("Error in finding shared memory relocation area\n"); + return 0; + } + } + printf("Relocation src:\t%08X\n", vAddrOfRelocSrc); + printf("Relocation dst:\t%08X\n", relocDestAtSharedMem); +} + + u32 savePatchV1 (const tNDSHeader* ndsHeader, u32* cardEngineLocation, module_params_t* moduleParams) { - u32 alignedA7size = (ndsHeader->arm7binarySize + 511) & 0xFFFFFE00; + nocashMessage("\nArm7 (patch v1.0)\n"); // Find the relocation signature - u32 relocationStart = getOffset((u32*)ndsHeader->arm7destination, alignedA7size, - relocateStartSignature, 4, 1); + u32 relocationStart = getOffset((u32*)ndsHeader->arm7destination, ndsHeader->arm7binarySize, + relocateStartSignature, 1, 1); if (!relocationStart) { nocashMessage("Relocation start not found\n"); return 0; @@ -351,7 +402,7 @@ u32 savePatchV1 (const tNDSHeader* ndsHeader, u32* cardEngineLocation, module_pa relocDestAtSharedMem = *(u32*)valueAtRelocStart + 0xC; if (relocDestAtSharedMem != 0x37F8000) { - printf("Error in finding shared memory relocation area\n"); + nocashMessage("Error in finding shared memory relocation area\n"); return 0; } } @@ -397,7 +448,8 @@ u32 patchCardNdsArm7 (const tNDSHeader* ndsHeader, u32* cardEngineLocation, modu copyLoop ((u32*)cardIrqEnableOffset, cardIrqEnablePatch, 0x30); - savePatchV1(ndsHeader, cardEngineLocation, moduleParams); + u32 saveResult = savePatchV2(ndsHeader, cardEngineLocation, moduleParams); + if(!saveResult) saveResult = savePatchV1(ndsHeader, cardEngineLocation, moduleParams); nocashMessage("ERR_NONE"); return 0; diff --git a/bootloader/source/hook.c b/bootloader/source/hook.c index 9008bf8f6..dd0596e12 100644 --- a/bootloader/source/hook.c +++ b/bootloader/source/hook.c @@ -19,6 +19,7 @@ #include "hook.h" #include "common.h" #include "cardengine_bin.h" +#include "sdengine_bin.h" extern unsigned long cheat_engine_size; extern unsigned long intr_orig_return_offset; @@ -240,7 +241,47 @@ static u32* hookInterruptHandler (u32* addr, size_t size) { // 2 LCD V-Counter Match } -int hookNds (const tNDSHeader* ndsHeader, u32 fileCluster, const u32* cheatData, u32* cheatEngineLocation, u32* cardEngineLocation, u32* wordCommandAddr) { +int hookNdsHomebrew (const tNDSHeader* ndsHeader, const u32* cheatData, u32* cheatEngineLocation, u32* sdEngineLocation, u32* wordCommandAddr) { + u32* hookLocation = NULL; + u32* hookAccel = NULL; + + nocashMessage("hookNds"); + + if (!hookLocation) { + hookLocation = hookInterruptHandlerHomebrew((u32*)ndsHeader->arm7destination, ndsHeader->arm7binarySize); + } + + if (!hookLocation) { + nocashMessage("ERR_HOOK"); + return ERR_HOOK; + } + + hookAccel = hookAccelIPCHomebrew2007((u32*)ndsHeader->arm7destination, ndsHeader->arm7binarySize); + + if (!hookAccel) { + nocashMessage("ACCEL_IPC_2007_ERR"); + } else { + nocashMessage("ACCEL_IPC_2007_OK"); + } + + hookAccel = hookAccelIPCHomebrew2010((u32*)ndsHeader->arm7destination, ndsHeader->arm7binarySize); + + if (!hookAccel) { + nocashMessage("ACCEL_IPC_2010_ERR"); + } else { + nocashMessage("ACCEL_IPC_2010_OK"); + } + + copyLoop (sdEngineLocation, (u32*)sdengine_bin, sdengine_bin_size); + + sdEngineLocation[1] = myMemUncached(wordCommandAddr); + + nocashMessage("ERR_NONE"); + return ERR_NONE; +} + + +int hookNdsRetail (const tNDSHeader* ndsHeader, u32 fileCluster, const u32* cheatData, u32* cheatEngineLocation, u32* cardEngineLocation) { u32* hookLocation = NULL; u32* hookAccel = NULL; u32* debug = (u32*)0x037D0000; diff --git a/bootloader/source/hook.h b/bootloader/source/hook.h index 6d6b57336..ee2ec6982 100644 --- a/bootloader/source/hook.h +++ b/bootloader/source/hook.h @@ -23,4 +23,6 @@ arm7_hookGame Adds a hook in the game's ARM7 binary to our own code -------------------------------------------------------------------------*/ -int hookNds (const tNDSHeader* ndsHeader, u32 fileCluster, const u32* cheatData, u32* cheatEngineLocation, u32* cardEngineLocation, u32* wordCommandAddr); +int hookNdsRetail (const tNDSHeader* ndsHeader, u32 fileCluster, const u32* cheatData, u32* cheatEngineLocation, u32* cardEngineLocation); + +int hookNdsHomebrew (const tNDSHeader* ndsHeader, const u32* cheatData, u32* cheatEngineLocation, u32* sdEngineLocation, u32* wordCommandAddr); From daadada6a5a3bdc1365654eebd41d1148ccffa15 Mon Sep 17 00:00:00 2001 From: ahezard Date: Sat, 7 Jan 2017 01:58:00 +0100 Subject: [PATCH 4/8] bug fix --- bootloader/source/boot.c | 1 + 1 file changed, 1 insertion(+) diff --git a/bootloader/source/boot.c b/bootloader/source/boot.c index 53a6d4556..4ab8aaedd 100644 --- a/bootloader/source/boot.c +++ b/bootloader/source/boot.c @@ -346,6 +346,7 @@ int main (void) { u32 patchOffset = quickFind ((u8*)((u32*)NDS_HEAD)[0x0A], dldiMagicString, ((u32*)NDS_HEAD)[0x0B], sizeof(dldiMagicString)); u32* wordCommandAddr = (u32 *) (((u32)((u32*)NDS_HEAD)[0x0A])+patchOffset+0x80); + hookNdsHomebrew(NDS_HEAD, (const u32*)CHEAT_DATA_LOCATION, (u32*)CHEAT_ENGINE_LOCATION, (u32*)SD_ENGINE_LOCATION, wordCommandAddr); } } else { copyLoop (SD_ENGINE_LOCATION, (u32*)cardengine_bin, cardengine_bin_size); From 60dd5c38f5138fb8446114e97e4b6dd8102e3d06 Mon Sep 17 00:00:00 2001 From: ahezard Date: Sat, 7 Jan 2017 02:06:01 +0100 Subject: [PATCH 5/8] dldi + card working --- bootloader/source/boot.c | 26 +++++++++++++------------- bootloader/source/hook.c | 4 ++-- 2 files changed, 15 insertions(+), 15 deletions(-) diff --git a/bootloader/source/boot.c b/bootloader/source/boot.c index 4ab8aaedd..42eebed60 100644 --- a/bootloader/source/boot.c +++ b/bootloader/source/boot.c @@ -335,7 +335,7 @@ int main (void) { nocashMessage("Load the NDS file"); loadBinary_ARM7(fileCluster); - wantToPatchDLDI = wantToPatchDLDI && ((u32*)NDS_HEAD)[0x084] > 0x200; + //wantToPatchDLDI = wantToPatchDLDI && ((u32*)NDS_HEAD)[0x084] > 0x200; // Patch with DLDI if desired if (wantToPatchDLDI) { @@ -347,20 +347,20 @@ int main (void) { u32* wordCommandAddr = (u32 *) (((u32)((u32*)NDS_HEAD)[0x0A])+patchOffset+0x80); hookNdsHomebrew(NDS_HEAD, (const u32*)CHEAT_DATA_LOCATION, (u32*)CHEAT_ENGINE_LOCATION, (u32*)SD_ENGINE_LOCATION, wordCommandAddr); - } - } else { - copyLoop (SD_ENGINE_LOCATION, (u32*)cardengine_bin, cardengine_bin_size); + } else { + copyLoop (SD_ENGINE_LOCATION, (u32*)cardengine_bin, cardengine_bin_size); - module_params_t* params = findModuleParams(NDS_HEAD); - if(params) - { - ensureArm9Decompressed(NDS_HEAD, params); + module_params_t* params = findModuleParams(NDS_HEAD); + if(params) + { + ensureArm9Decompressed(NDS_HEAD, params); + } + + patchCardNds(NDS_HEAD,SD_ENGINE_LOCATION,params); + + hookNdsRetail(NDS_HEAD, fileCluster, (const u32*)CHEAT_DATA_LOCATION, (u32*)CHEAT_ENGINE_LOCATION, (u32*)SD_ENGINE_LOCATION); } - - patchCardNds(NDS_HEAD,SD_ENGINE_LOCATION,params); - - hookNdsRetail(NDS_HEAD, fileCluster, (const u32*)CHEAT_DATA_LOCATION, (u32*)CHEAT_ENGINE_LOCATION, (u32*)SD_ENGINE_LOCATION); - } + } // Pass command line arguments to loaded program diff --git a/bootloader/source/hook.c b/bootloader/source/hook.c index dd0596e12..1e082a428 100644 --- a/bootloader/source/hook.c +++ b/bootloader/source/hook.c @@ -245,7 +245,7 @@ int hookNdsHomebrew (const tNDSHeader* ndsHeader, const u32* cheatData, u32* che u32* hookLocation = NULL; u32* hookAccel = NULL; - nocashMessage("hookNds"); + nocashMessage("hookNdsHomebrew"); if (!hookLocation) { hookLocation = hookInterruptHandlerHomebrew((u32*)ndsHeader->arm7destination, ndsHeader->arm7binarySize); @@ -286,7 +286,7 @@ int hookNdsRetail (const tNDSHeader* ndsHeader, u32 fileCluster, const u32* chea u32* hookAccel = NULL; u32* debug = (u32*)0x037D0000; - nocashMessage("hookNds"); + nocashMessage("hookNdsRetail"); if (!hookLocation) { hookLocation = hookInterruptHandler((u32*)ndsHeader->arm7destination, ndsHeader->arm7binarySize); From 7705d74f90f8b6c1bd4de8e559ecbea1442420b2 Mon Sep 17 00:00:00 2001 From: ahezard Date: Sat, 7 Jan 2017 22:24:27 +0100 Subject: [PATCH 6/8] bug fix --- bootloader/source/boot.c | 27 ++++++++++++++++----------- bootloader/source/common.h | 9 ++++++++- 2 files changed, 24 insertions(+), 12 deletions(-) diff --git a/bootloader/source/boot.c b/bootloader/source/boot.c index 42eebed60..eaf79daef 100644 --- a/bootloader/source/boot.c +++ b/bootloader/source/boot.c @@ -54,6 +54,7 @@ Helpful information: #include "cardengine_bin.h" #include "boot.h" #include "hook.h" +#include "common.h" void arm7clearRAM(); int sdmmc_sdcard_readsectors(u32 sector_no, u32 numsectors, void *out); @@ -110,14 +111,6 @@ void boot_readFirmware (uint32 address, uint8 * buffer, uint32 size) { REG_SPICNT = 0; } - -static inline void copyLoop (u32* dest, const u32* src, u32 size) { - size = (size +3) & ~3; - do { - *dest++ = *src++; - } while (size -= 4); -} - //#define resetCpu() __asm volatile("\tswi 0x000000\n"); /*------------------------------------------------------------------------- @@ -339,15 +332,22 @@ int main (void) { // Patch with DLDI if desired if (wantToPatchDLDI) { - nocashMessage("wantToPatchDLDI"); + nocashMessage("try to patch dldi"); wantToPatchDLDI = wantToPatchDLDI && dldiPatchBinary ((u8*)((u32*)NDS_HEAD)[0x0A], ((u32*)NDS_HEAD)[0x0B]); if (wantToPatchDLDI) { + nocashMessage("dldi patch successful"); // Find the DLDI reserved space in the file u32 patchOffset = quickFind ((u8*)((u32*)NDS_HEAD)[0x0A], dldiMagicString, ((u32*)NDS_HEAD)[0x0B], sizeof(dldiMagicString)); u32* wordCommandAddr = (u32 *) (((u32)((u32*)NDS_HEAD)[0x0A])+patchOffset+0x80); - hookNdsHomebrew(NDS_HEAD, (const u32*)CHEAT_DATA_LOCATION, (u32*)CHEAT_ENGINE_LOCATION, (u32*)SD_ENGINE_LOCATION, wordCommandAddr); + int error = hookNdsHomebrew(NDS_HEAD, (const u32*)CHEAT_DATA_LOCATION, (u32*)CHEAT_ENGINE_LOCATION, (u32*)SD_ENGINE_LOCATION, wordCommandAddr); + if(error == ERR_NONE) { + nocashMessage("dldi hook Sucessfull"); + } else { + nocashMessage("error during dldi hook"); + } } else { + nocashMessage("dldi Patch Unsuccessful try to patch card"); copyLoop (SD_ENGINE_LOCATION, (u32*)cardengine_bin, cardengine_bin_size); module_params_t* params = findModuleParams(NDS_HEAD); @@ -358,7 +358,12 @@ int main (void) { patchCardNds(NDS_HEAD,SD_ENGINE_LOCATION,params); - hookNdsRetail(NDS_HEAD, fileCluster, (const u32*)CHEAT_DATA_LOCATION, (u32*)CHEAT_ENGINE_LOCATION, (u32*)SD_ENGINE_LOCATION); + int error = hookNdsRetail(NDS_HEAD, fileCluster, (const u32*)CHEAT_DATA_LOCATION, (u32*)CHEAT_ENGINE_LOCATION, (u32*)SD_ENGINE_LOCATION); + if(error == ERR_NONE) { + nocashMessage("card hook Sucessfull"); + } else { + nocashMessage("error during card hook"); + } } } diff --git a/bootloader/source/common.h b/bootloader/source/common.h index 4d959d172..e3a49009c 100644 --- a/bootloader/source/common.h +++ b/bootloader/source/common.h @@ -44,7 +44,14 @@ static inline void dmaFill(const void* src, void* dest, uint32 size) { while(DMA_CR(3) & DMA_BUSY); } -static inline void copyLoop (u32* dest, const u32* src, size_t size) { +/*static inline void copyLoop (u32* dest, const u32* src, size_t size) { + do { + *dest++ = *src++; + } while (size -= 4); +}*/ + +static inline void copyLoop (u32* dest, const u32* src, u32 size) { + size = (size +3) & ~3; do { *dest++ = *src++; } while (size -= 4); From 76f6ac17ccd614fae41052f7b9e7ca0d9f357e0f Mon Sep 17 00:00:00 2001 From: ahezard Date: Sat, 7 Jan 2017 23:36:13 +0100 Subject: [PATCH 7/8] Deactivate bootstub --- arm9/source/nds_loader_arm9.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/arm9/source/nds_loader_arm9.c b/arm9/source/nds_loader_arm9.c index 07e7d8306..8953c22fa 100644 --- a/arm9/source/nds_loader_arm9.c +++ b/arm9/source/nds_loader_arm9.c @@ -394,7 +394,7 @@ int runNdsFile (const char* filename, int argc, const char** argv) { if(argv[0][0]=='s' && argv[0][1]=='d') havedsiSD = true; - installBootStub(havedsiSD); + //installBootStub(havedsiSD); return runNds (load_bin, load_bin_size, st.st_ino, true, true, argc, argv); } From 5892622e8602b588d6f342f86d17f9b311260710 Mon Sep 17 00:00:00 2001 From: ahezard Date: Sat, 7 Jan 2017 23:40:01 +0100 Subject: [PATCH 8/8] rename bootstrap-dldi.nds to bootstrap.nds --- build_cia.sh | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/build_cia.sh b/build_cia.sh index 736fd1055..508f74e80 100644 --- a/build_cia.sh +++ b/build_cia.sh @@ -5,13 +5,11 @@ GAME_INFO="KBSE 01 NDSBOOTSTRAP" $DEVKITARM/bin/ndstool -c bootstrap.nds -7 nds-bootstrap.arm7.elf -9 nds-bootstrap.arm9.elf -g $GAME_INFO -b icon.bmp "$GAME_TITLE;$GAME_SUBTITLE1;$GAME_SUBTITLE2" -r9 0x2000000 -r7 0x2380000 -e9 0x2000000 -e7 0x2380000 cp bootstrap.nds bootstrap-nogba.nds -cp bootstrap.nds bootstrap-dldi.nds -$DEVKITARM/bin/dlditool dldi/dsisd.dldi bootstrap-dldi.nds +$DEVKITARM/bin/dlditool dldi/dsisd.dldi bootstrap.nds $DEVKITARM/bin/dlditool dldi/dsisd.dldi bootstrap-nogba.nds python patch_ndsheader_dsiware.py --mode dsi bootstrap.nds -python patch_ndsheader_dsiware.py --mode dsi bootstrap-dldi.nds python patch_ndsheader_dsiware.py --mode dsinogba bootstrap-nogba.nds #./make_cia.exe --srl=bootstrap.nds