@@ -63,7 +63,7 @@ u32 arenaLowSignature[4] = {0xE1A00100,0xE2800627,0xE2800AFF,0xE5801DA0};
63
63
//
64
64
// Look in @data for @find and return the position of it.
65
65
//
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 )
67
67
{
68
68
u32 * end = addr + size /sizeof (u32 );
69
69
u32 * debug = (u32 * )0x037D0000 ;
@@ -94,7 +94,7 @@ u32 getOffsetA9(u32* addr, size_t size, u32* find, size_t sizeofFind, int direct
94
94
module_params_t * findModuleParams (const tNDSHeader * ndsHeader )
95
95
{
96
96
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 );
98
98
if (!moduleparams )
99
99
{
100
100
nocashMessage ("No moduleparams?\n" );
@@ -174,15 +174,15 @@ u32 patchCardNdsArm9 (const tNDSHeader* ndsHeader, u32* cardEngineLocation, modu
174
174
175
175
// Find the card read
176
176
u32 cardReadEndOffset =
177
- getOffsetA9 ((u32 * )ndsHeader -> arm9destination , 0x00300000 ,//ndsHeader->arm9binarySize,
177
+ getOffset ((u32 * )ndsHeader -> arm9destination , 0x00300000 ,//ndsHeader->arm9binarySize,
178
178
(u32 * )a9cardReadSignature , 2 , 1 );
179
179
if (!cardReadEndOffset ) {
180
180
nocashMessage ("Card read end not found\n" );
181
181
return 0 ;
182
182
}
183
183
debug [1 ] = cardReadEndOffset ;
184
184
u32 cardReadStartOffset =
185
- getOffsetA9 ((u32 * )cardReadEndOffset , -0xF9 ,
185
+ getOffset ((u32 * )cardReadEndOffset , -0xF9 ,
186
186
(u32 * )cardReadStartSignature , 1 , -1 );
187
187
if (!cardReadStartOffset ) {
188
188
nocashMessage ("Card read start not found\n" );
@@ -192,7 +192,7 @@ u32 patchCardNdsArm9 (const tNDSHeader* ndsHeader, u32* cardEngineLocation, modu
192
192
nocashMessage ("Card read found\n" );
193
193
194
194
u32 cardPullOutOffset =
195
- getOffsetA9 ((u32 * )ndsHeader -> arm9destination , 0x00300000 ,//, ndsHeader->arm9binarySize,
195
+ getOffset ((u32 * )ndsHeader -> arm9destination , 0x00300000 ,//, ndsHeader->arm9binarySize,
196
196
(u32 * )cardPullOutSignature , 4 , 1 );
197
197
if (!cardPullOutOffset ) {
198
198
nocashMessage ("Card pull out handler not found\n" );
@@ -203,15 +203,15 @@ u32 patchCardNdsArm9 (const tNDSHeader* ndsHeader, u32* cardEngineLocation, modu
203
203
204
204
205
205
u32 cardReadCachedEndOffset =
206
- getOffsetA9 ((u32 * )ndsHeader -> arm9destination , 0x00300000 ,//ndsHeader->arm9binarySize,
206
+ getOffset ((u32 * )ndsHeader -> arm9destination , 0x00300000 ,//ndsHeader->arm9binarySize,
207
207
(u32 * )cardReadCachedEndSignature , 4 , 1 );
208
208
if (!cardReadCachedEndOffset ) {
209
209
nocashMessage ("Card read cached end not found\n" );
210
210
return 0 ;
211
211
}
212
212
debug [1 ] = cardReadCachedEndOffset ;
213
213
u32 cardReadCachedOffset =
214
- getOffsetA9 ((u32 * )cardReadCachedEndOffset , -0xFF ,
214
+ getOffset ((u32 * )cardReadCachedEndOffset , -0xFF ,
215
215
(u32 * )cardReadCachedStartSignature , 2 , -1 );
216
216
if (!cardReadStartOffset ) {
217
217
nocashMessage ("Card read cached start not found\n" );
@@ -309,6 +309,107 @@ u32 patchCardNdsArm9 (const tNDSHeader* ndsHeader, u32* cardEngineLocation, modu
309
309
return 0 ;
310
310
}
311
311
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
+
312
413
u32 patchCardNdsArm7 (const tNDSHeader * ndsHeader , u32 * cardEngineLocation , module_params_t * moduleParams ) {
313
414
u32 * debug = (u32 * )0x037D0000 ;
314
415
@@ -318,7 +419,7 @@ u32 patchCardNdsArm7 (const tNDSHeader* ndsHeader, u32* cardEngineLocation, modu
318
419
}
319
420
320
421
u32 cardCheckPullOutOffset =
321
- getOffsetA9 ((u32 * )ndsHeader -> arm7destination , 0x00400000 ,//, ndsHeader->arm9binarySize,
422
+ getOffset ((u32 * )ndsHeader -> arm7destination , 0x00400000 ,//, ndsHeader->arm9binarySize,
322
423
(u32 * )cardCheckPullOutSignature , 4 , 1 );
323
424
if (!cardCheckPullOutOffset ) {
324
425
nocashMessage ("Card check pull out not found\n" );
@@ -329,7 +430,7 @@ u32 patchCardNdsArm7 (const tNDSHeader* ndsHeader, u32* cardEngineLocation, modu
329
430
}
330
431
331
432
u32 cardIrqEnableOffset =
332
- getOffsetA9 ((u32 * )ndsHeader -> arm7destination , 0x00400000 ,//, ndsHeader->arm9binarySize,
433
+ getOffset ((u32 * )ndsHeader -> arm7destination , 0x00400000 ,//, ndsHeader->arm9binarySize,
333
434
(u32 * )irqEnableStartSignature , 4 , 1 );
334
435
if (!cardIrqEnableOffset ) {
335
436
nocashMessage ("irq enable not found\n" );
@@ -347,6 +448,9 @@ u32 patchCardNdsArm7 (const tNDSHeader* ndsHeader, u32* cardEngineLocation, modu
347
448
348
449
copyLoop ((u32 * )cardIrqEnableOffset , cardIrqEnablePatch , 0x30 );
349
450
451
+ u32 saveResult = savePatchV2 (ndsHeader , cardEngineLocation , moduleParams );
452
+ if (!saveResult ) saveResult = savePatchV1 (ndsHeader , cardEngineLocation , moduleParams );
453
+
350
454
nocashMessage ("ERR_NONE" );
351
455
return 0 ;
352
456
}
0 commit comments