Skip to content

Commit

Permalink
Merge pull request #26 from ahezard/card
Browse files Browse the repository at this point in the history
Card
  • Loading branch information
ahezard authored Jan 7, 2017
2 parents c2ab84c + 5892622 commit a436ad8
Show file tree
Hide file tree
Showing 10 changed files with 212 additions and 47 deletions.
2 changes: 1 addition & 1 deletion arm9/source/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
4 changes: 3 additions & 1 deletion arm9/source/nds_loader_arm9.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -392,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);
}
Expand Down
2 changes: 1 addition & 1 deletion bootloader/load.ld
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ ENTRY(_start)

MEMORY {

vram : ORIGIN = 0x06000000, LENGTH = 128K
vram : ORIGIN = 0x06000000, LENGTH = 256K
}

__vram_start = ORIGIN(vram);
Expand Down
2 changes: 1 addition & 1 deletion bootloader/source/arm9clear.arm.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
65 changes: 38 additions & 27 deletions bootloader/source/boot.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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");

/*-------------------------------------------------------------------------
Expand Down Expand Up @@ -335,27 +328,45 @@ 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]);
//}

copyLoop (SD_ENGINE_LOCATION, (u32*)cardengine_bin, cardengine_bin_size);

module_params_t* params = findModuleParams(NDS_HEAD);
if(params)
{
ensureArm9Decompressed(NDS_HEAD, params);
}

patchCardNds(NDS_HEAD,SD_ENGINE_LOCATION,params);
//wantToPatchDLDI = wantToPatchDLDI && ((u32*)NDS_HEAD)[0x084] > 0x200;

// 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;
// Patch with DLDI if desired
if (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);

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);
if(params)
{
ensureArm9Decompressed(NDS_HEAD, params);
}

patchCardNds(NDS_HEAD,SD_ENGINE_LOCATION,params);

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");
}
}
}

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();
Expand Down
122 changes: 113 additions & 9 deletions bootloader/source/card_patcher.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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");
Expand Down Expand Up @@ -174,15 +174,15 @@ 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");
return 0;
}
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");
Expand All @@ -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");
Expand All @@ -203,15 +203,15 @@ 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");
return 0;
}
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");
Expand Down Expand Up @@ -309,6 +309,107 @@ 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) {

nocashMessage("\nArm7 (patch v1.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 patchCardNdsArm7 (const tNDSHeader* ndsHeader, u32* cardEngineLocation, module_params_t* moduleParams) {
u32* debug = (u32*)0x037D0000;

Expand All @@ -318,7 +419,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");
Expand All @@ -329,7 +430,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");
Expand All @@ -347,6 +448,9 @@ u32 patchCardNdsArm7 (const tNDSHeader* ndsHeader, u32* cardEngineLocation, modu

copyLoop ((u32*)cardIrqEnableOffset, cardIrqEnablePatch, 0x30);

u32 saveResult = savePatchV2(ndsHeader, cardEngineLocation, moduleParams);
if(!saveResult) saveResult = savePatchV1(ndsHeader, cardEngineLocation, moduleParams);

nocashMessage("ERR_NONE");
return 0;
}
Expand Down
9 changes: 8 additions & 1 deletion bootloader/source/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Loading

0 comments on commit a436ad8

Please sign in to comment.