Skip to content

Commit a436ad8

Browse files
authored
Merge pull request #26 from ahezard/card
Card
2 parents c2ab84c + 5892622 commit a436ad8

File tree

10 files changed

+212
-47
lines changed

10 files changed

+212
-47
lines changed

arm9/source/main.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,7 @@ int main( int argc, char **argv) {
230230

231231
if(bootstrapini.GetInt("NDS-BOOTSTRAP","LOCK_ARM9_SCFG_EXT",0) == 1) {
232232
dbg_printf("ARM9_SCFG_EXT locked\n");
233-
REG_SCFG_EXT = 0x03000000; // NAND/SD Access
233+
REG_SCFG_EXT &= 0x7FFFFFFF; // Only lock bit 31
234234
}
235235

236236
if(bootstrapini.GetInt("NDS-BOOTSTRAP","NTR_MODE_SWITCH",0) == 1) {

arm9/source/nds_loader_arm9.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,7 @@ int runNds (const void* loader, u32 loaderSize, u32 cluster, bool initDisc, bool
280280

281281
// Direct CPU access to VRAM bank C
282282
VRAM_C_CR = VRAM_ENABLE | VRAM_C_LCD;
283+
VRAM_D_CR = VRAM_ENABLE | VRAM_D_LCD;
283284
// Load the loader/patcher into the correct address
284285
vramcpy (LCDC_BANK_C, loader, loaderSize);
285286

@@ -347,6 +348,7 @@ int runNds (const void* loader, u32 loaderSize, u32 cluster, bool initDisc, bool
347348
nocashMessage("Give the VRAM to the ARM7");
348349
// Give the VRAM to the ARM7
349350
VRAM_C_CR = VRAM_ENABLE | VRAM_C_ARM7_0x06000000;
351+
VRAM_D_CR = VRAM_ENABLE | VRAM_D_ARM7_0x06020000;
350352

351353
nocashMessage("Reset into a passme loop");
352354
// Reset into a passme loop
@@ -392,7 +394,7 @@ int runNdsFile (const char* filename, int argc, const char** argv) {
392394

393395
if(argv[0][0]=='s' && argv[0][1]=='d') havedsiSD = true;
394396

395-
installBootStub(havedsiSD);
397+
//installBootStub(havedsiSD);
396398

397399
return runNds (load_bin, load_bin_size, st.st_ino, true, true, argc, argv);
398400
}

bootloader/load.ld

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ ENTRY(_start)
44

55
MEMORY {
66

7-
vram : ORIGIN = 0x06000000, LENGTH = 128K
7+
vram : ORIGIN = 0x06000000, LENGTH = 256K
88
}
99

1010
__vram_start = ORIGIN(vram);

bootloader/source/arm9clear.arm.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ void __attribute__ ((long_call)) __attribute__((naked)) __attribute__((noreturn)
4949
VRAM_B_CR = 0;
5050
// Don't mess with the ARM7's VRAM
5151
// VRAM_C_CR = 0;
52-
VRAM_D_CR = 0;
52+
// VRAM_D_CR = 0;
5353
VRAM_E_CR = 0;
5454
VRAM_F_CR = 0;
5555
VRAM_G_CR = 0;

bootloader/source/boot.c

Lines changed: 38 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ Helpful information:
5454
#include "cardengine_bin.h"
5555
#include "boot.h"
5656
#include "hook.h"
57+
#include "common.h"
5758

5859
void arm7clearRAM();
5960
int sdmmc_sdcard_readsectors(u32 sector_no, u32 numsectors, void *out);
@@ -110,14 +111,6 @@ void boot_readFirmware (uint32 address, uint8 * buffer, uint32 size) {
110111
REG_SPICNT = 0;
111112
}
112113

113-
114-
static inline void copyLoop (u32* dest, const u32* src, u32 size) {
115-
size = (size +3) & ~3;
116-
do {
117-
*dest++ = *src++;
118-
} while (size -= 4);
119-
}
120-
121114
//#define resetCpu() __asm volatile("\tswi 0x000000\n");
122115

123116
/*-------------------------------------------------------------------------
@@ -335,27 +328,45 @@ int main (void) {
335328
nocashMessage("Load the NDS file");
336329
loadBinary_ARM7(fileCluster);
337330

338-
// Patch with DLDI if desired
339-
//if (wantToPatchDLDI) {
340-
// nocashMessage("wantToPatchDLDI");
341-
// dldiPatchBinary ((u8*)((u32*)NDS_HEAD)[0x0A], ((u32*)NDS_HEAD)[0x0B]);
342-
//}
343-
344-
copyLoop (SD_ENGINE_LOCATION, (u32*)cardengine_bin, cardengine_bin_size);
345-
346-
module_params_t* params = findModuleParams(NDS_HEAD);
347-
if(params)
348-
{
349-
ensureArm9Decompressed(NDS_HEAD, params);
350-
}
351-
352-
patchCardNds(NDS_HEAD,SD_ENGINE_LOCATION,params);
331+
//wantToPatchDLDI = wantToPatchDLDI && ((u32*)NDS_HEAD)[0x084] > 0x200;
353332

354-
// Find the DLDI reserved space in the file
355-
//u32 patchOffset = quickFind ((u8*)((u32*)NDS_HEAD)[0x0A], dldiMagicString, ((u32*)NDS_HEAD)[0x0B], sizeof(dldiMagicString));
356-
u32* wordCommandAddr = (u32 *) 0x2051BB4;
333+
// Patch with DLDI if desired
334+
if (wantToPatchDLDI) {
335+
nocashMessage("try to patch dldi");
336+
wantToPatchDLDI = wantToPatchDLDI && dldiPatchBinary ((u8*)((u32*)NDS_HEAD)[0x0A], ((u32*)NDS_HEAD)[0x0B]);
337+
if (wantToPatchDLDI) {
338+
nocashMessage("dldi patch successful");
339+
// Find the DLDI reserved space in the file
340+
u32 patchOffset = quickFind ((u8*)((u32*)NDS_HEAD)[0x0A], dldiMagicString, ((u32*)NDS_HEAD)[0x0B], sizeof(dldiMagicString));
341+
u32* wordCommandAddr = (u32 *) (((u32)((u32*)NDS_HEAD)[0x0A])+patchOffset+0x80);
342+
343+
int error = hookNdsHomebrew(NDS_HEAD, (const u32*)CHEAT_DATA_LOCATION, (u32*)CHEAT_ENGINE_LOCATION, (u32*)SD_ENGINE_LOCATION, wordCommandAddr);
344+
if(error == ERR_NONE) {
345+
nocashMessage("dldi hook Sucessfull");
346+
} else {
347+
nocashMessage("error during dldi hook");
348+
}
349+
} else {
350+
nocashMessage("dldi Patch Unsuccessful try to patch card");
351+
copyLoop (SD_ENGINE_LOCATION, (u32*)cardengine_bin, cardengine_bin_size);
352+
353+
module_params_t* params = findModuleParams(NDS_HEAD);
354+
if(params)
355+
{
356+
ensureArm9Decompressed(NDS_HEAD, params);
357+
}
358+
359+
patchCardNds(NDS_HEAD,SD_ENGINE_LOCATION,params);
360+
361+
int error = hookNdsRetail(NDS_HEAD, fileCluster, (const u32*)CHEAT_DATA_LOCATION, (u32*)CHEAT_ENGINE_LOCATION, (u32*)SD_ENGINE_LOCATION);
362+
if(error == ERR_NONE) {
363+
nocashMessage("card hook Sucessfull");
364+
} else {
365+
nocashMessage("error during card hook");
366+
}
367+
}
368+
}
357369

358-
hookNds(NDS_HEAD, fileCluster, (const u32*)CHEAT_DATA_LOCATION, (u32*)CHEAT_ENGINE_LOCATION, (u32*)SD_ENGINE_LOCATION, wordCommandAddr);
359370

360371
// Pass command line arguments to loaded program
361372
//passArgs_ARM7();

bootloader/source/card_patcher.c

Lines changed: 113 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ u32 arenaLowSignature[4] = {0xE1A00100,0xE2800627,0xE2800AFF,0xE5801DA0};
6363
//
6464
// Look in @data for @find and return the position of it.
6565
//
66-
u32 getOffsetA9(u32* addr, size_t size, u32* find, size_t sizeofFind, int direction)
66+
u32 getOffset(u32* addr, size_t size, u32* find, size_t sizeofFind, int direction)
6767
{
6868
u32* end = addr + size/sizeof(u32);
6969
u32* debug = (u32*)0x037D0000;
@@ -94,7 +94,7 @@ u32 getOffsetA9(u32* addr, size_t size, u32* find, size_t sizeofFind, int direct
9494
module_params_t* findModuleParams(const tNDSHeader* ndsHeader)
9595
{
9696
nocashMessage("Looking for moduleparams\n");
97-
uint32_t moduleparams = getOffsetA9((u32*)ndsHeader->arm9destination, ndsHeader->arm9binarySize, (u32*)moduleParamsSignature, 2, 1);
97+
uint32_t moduleparams = getOffset((u32*)ndsHeader->arm9destination, ndsHeader->arm9binarySize, (u32*)moduleParamsSignature, 2, 1);
9898
if(!moduleparams)
9999
{
100100
nocashMessage("No moduleparams?\n");
@@ -174,15 +174,15 @@ u32 patchCardNdsArm9 (const tNDSHeader* ndsHeader, u32* cardEngineLocation, modu
174174

175175
// Find the card read
176176
u32 cardReadEndOffset =
177-
getOffsetA9((u32*)ndsHeader->arm9destination, 0x00300000,//ndsHeader->arm9binarySize,
177+
getOffset((u32*)ndsHeader->arm9destination, 0x00300000,//ndsHeader->arm9binarySize,
178178
(u32*)a9cardReadSignature, 2, 1);
179179
if (!cardReadEndOffset) {
180180
nocashMessage("Card read end not found\n");
181181
return 0;
182182
}
183183
debug[1] = cardReadEndOffset;
184184
u32 cardReadStartOffset =
185-
getOffsetA9((u32*)cardReadEndOffset, -0xF9,
185+
getOffset((u32*)cardReadEndOffset, -0xF9,
186186
(u32*)cardReadStartSignature, 1, -1);
187187
if (!cardReadStartOffset) {
188188
nocashMessage("Card read start not found\n");
@@ -192,7 +192,7 @@ u32 patchCardNdsArm9 (const tNDSHeader* ndsHeader, u32* cardEngineLocation, modu
192192
nocashMessage("Card read found\n");
193193

194194
u32 cardPullOutOffset =
195-
getOffsetA9((u32*)ndsHeader->arm9destination, 0x00300000,//, ndsHeader->arm9binarySize,
195+
getOffset((u32*)ndsHeader->arm9destination, 0x00300000,//, ndsHeader->arm9binarySize,
196196
(u32*)cardPullOutSignature, 4, 1);
197197
if (!cardPullOutOffset) {
198198
nocashMessage("Card pull out handler not found\n");
@@ -203,15 +203,15 @@ u32 patchCardNdsArm9 (const tNDSHeader* ndsHeader, u32* cardEngineLocation, modu
203203

204204

205205
u32 cardReadCachedEndOffset =
206-
getOffsetA9((u32*)ndsHeader->arm9destination, 0x00300000,//ndsHeader->arm9binarySize,
206+
getOffset((u32*)ndsHeader->arm9destination, 0x00300000,//ndsHeader->arm9binarySize,
207207
(u32*)cardReadCachedEndSignature, 4, 1);
208208
if (!cardReadCachedEndOffset) {
209209
nocashMessage("Card read cached end not found\n");
210210
return 0;
211211
}
212212
debug[1] = cardReadCachedEndOffset;
213213
u32 cardReadCachedOffset =
214-
getOffsetA9((u32*)cardReadCachedEndOffset, -0xFF,
214+
getOffset((u32*)cardReadCachedEndOffset, -0xFF,
215215
(u32*)cardReadCachedStartSignature, 2, -1);
216216
if (!cardReadStartOffset) {
217217
nocashMessage("Card read cached start not found\n");
@@ -309,6 +309,107 @@ u32 patchCardNdsArm9 (const tNDSHeader* ndsHeader, u32* cardEngineLocation, modu
309309
return 0;
310310
}
311311

312+
u32 savePatchV2 (const tNDSHeader* ndsHeader, u32* cardEngineLocation, module_params_t* moduleParams) {
313+
314+
nocashMessage("\nArm7 (patch v2.0)\n");
315+
316+
// Find the relocation signature
317+
u32 relocationStart = getOffset((u32*)ndsHeader->arm7destination, ndsHeader->arm7binarySize,
318+
relocateStartSignature, 1, 1);
319+
if (!relocationStart) {
320+
nocashMessage("Relocation start not found\n");
321+
return 0;
322+
}
323+
324+
// Validate the relocation signature
325+
u32 forwardedRelocStartAddr = relocationStart + 4;
326+
if (!*(u32*)forwardedRelocStartAddr)
327+
forwardedRelocStartAddr += 4;
328+
u32 vAddrOfRelocSrc =
329+
*(u32*)(forwardedRelocStartAddr + 8);
330+
// sanity checks
331+
u32 relocationCheck1 =
332+
*(u32*)(forwardedRelocStartAddr + 0xC);
333+
u32 relocationCheck2 =
334+
*(u32*)(forwardedRelocStartAddr + 0x10);
335+
if ( vAddrOfRelocSrc != relocationCheck1
336+
|| vAddrOfRelocSrc != relocationCheck2) {
337+
nocashMessage("Error in relocation checking\n");
338+
return 0;
339+
}
340+
341+
342+
// Get the remaining details regarding relocation
343+
u32 valueAtRelocStart =
344+
*(u32*)forwardedRelocStartAddr;
345+
u32 relocDestAtSharedMem =
346+
*(u32*)valueAtRelocStart;
347+
if (relocDestAtSharedMem != 0x37F8000) { // shared memory in RAM
348+
// Try again
349+
vAddrOfRelocSrc +=
350+
*(u32*)valueAtRelocStart + 4;
351+
relocDestAtSharedMem =
352+
*(u32*)valueAtRelocStart + 0xC;
353+
if (relocDestAtSharedMem != 0x37F8000) {
354+
nocashMessage("Error in finding shared memory relocation area\n");
355+
return 0;
356+
}
357+
}
358+
printf("Relocation src:\t%08X\n", vAddrOfRelocSrc);
359+
printf("Relocation dst:\t%08X\n", relocDestAtSharedMem);
360+
}
361+
362+
363+
u32 savePatchV1 (const tNDSHeader* ndsHeader, u32* cardEngineLocation, module_params_t* moduleParams) {
364+
365+
nocashMessage("\nArm7 (patch v1.0)\n");
366+
367+
// Find the relocation signature
368+
u32 relocationStart = getOffset((u32*)ndsHeader->arm7destination, ndsHeader->arm7binarySize,
369+
relocateStartSignature, 1, 1);
370+
if (!relocationStart) {
371+
nocashMessage("Relocation start not found\n");
372+
return 0;
373+
}
374+
375+
// Validate the relocation signature
376+
u32 forwardedRelocStartAddr = relocationStart + 4;
377+
if (!*(u32*)forwardedRelocStartAddr)
378+
forwardedRelocStartAddr += 4;
379+
u32 vAddrOfRelocSrc =
380+
*(u32*)(forwardedRelocStartAddr + 8);
381+
// sanity checks
382+
u32 relocationCheck1 =
383+
*(u32*)(forwardedRelocStartAddr + 0xC);
384+
u32 relocationCheck2 =
385+
*(u32*)(forwardedRelocStartAddr + 0x10);
386+
if ( vAddrOfRelocSrc != relocationCheck1
387+
|| vAddrOfRelocSrc != relocationCheck2) {
388+
nocashMessage("Error in relocation checking\n");
389+
return 0;
390+
}
391+
392+
393+
// Get the remaining details regarding relocation
394+
u32 valueAtRelocStart =
395+
*(u32*)forwardedRelocStartAddr;
396+
u32 relocDestAtSharedMem =
397+
*(u32*)valueAtRelocStart;
398+
if (relocDestAtSharedMem != 0x37F8000) { // shared memory in RAM
399+
// Try again
400+
vAddrOfRelocSrc +=
401+
*(u32*)valueAtRelocStart + 4;
402+
relocDestAtSharedMem =
403+
*(u32*)valueAtRelocStart + 0xC;
404+
if (relocDestAtSharedMem != 0x37F8000) {
405+
nocashMessage("Error in finding shared memory relocation area\n");
406+
return 0;
407+
}
408+
}
409+
printf("Relocation src:\t%08X\n", vAddrOfRelocSrc);
410+
printf("Relocation dst:\t%08X\n", relocDestAtSharedMem);
411+
}
412+
312413
u32 patchCardNdsArm7 (const tNDSHeader* ndsHeader, u32* cardEngineLocation, module_params_t* moduleParams) {
313414
u32* debug = (u32*)0x037D0000;
314415

@@ -318,7 +419,7 @@ u32 patchCardNdsArm7 (const tNDSHeader* ndsHeader, u32* cardEngineLocation, modu
318419
}
319420

320421
u32 cardCheckPullOutOffset =
321-
getOffsetA9((u32*)ndsHeader->arm7destination, 0x00400000,//, ndsHeader->arm9binarySize,
422+
getOffset((u32*)ndsHeader->arm7destination, 0x00400000,//, ndsHeader->arm9binarySize,
322423
(u32*)cardCheckPullOutSignature, 4, 1);
323424
if (!cardCheckPullOutOffset) {
324425
nocashMessage("Card check pull out not found\n");
@@ -329,7 +430,7 @@ u32 patchCardNdsArm7 (const tNDSHeader* ndsHeader, u32* cardEngineLocation, modu
329430
}
330431

331432
u32 cardIrqEnableOffset =
332-
getOffsetA9((u32*)ndsHeader->arm7destination, 0x00400000,//, ndsHeader->arm9binarySize,
433+
getOffset((u32*)ndsHeader->arm7destination, 0x00400000,//, ndsHeader->arm9binarySize,
333434
(u32*)irqEnableStartSignature, 4, 1);
334435
if (!cardIrqEnableOffset) {
335436
nocashMessage("irq enable not found\n");
@@ -347,6 +448,9 @@ u32 patchCardNdsArm7 (const tNDSHeader* ndsHeader, u32* cardEngineLocation, modu
347448

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

451+
u32 saveResult = savePatchV2(ndsHeader, cardEngineLocation, moduleParams);
452+
if(!saveResult) saveResult = savePatchV1(ndsHeader, cardEngineLocation, moduleParams);
453+
350454
nocashMessage("ERR_NONE");
351455
return 0;
352456
}

bootloader/source/common.h

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,14 @@ static inline void dmaFill(const void* src, void* dest, uint32 size) {
4444
while(DMA_CR(3) & DMA_BUSY);
4545
}
4646

47-
static inline void copyLoop (u32* dest, const u32* src, size_t size) {
47+
/*static inline void copyLoop (u32* dest, const u32* src, size_t size) {
48+
do {
49+
*dest++ = *src++;
50+
} while (size -= 4);
51+
}*/
52+
53+
static inline void copyLoop (u32* dest, const u32* src, u32 size) {
54+
size = (size +3) & ~3;
4855
do {
4956
*dest++ = *src++;
5057
} while (size -= 4);

0 commit comments

Comments
 (0)