Skip to content

Commit

Permalink
Fix #415
Browse files Browse the repository at this point in the history
  • Loading branch information
RocketRobz committed Dec 9, 2024
1 parent 5f7ca79 commit a69bf01
Show file tree
Hide file tree
Showing 14 changed files with 106 additions and 54 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ An alternative forwarder generator for 3DS users. YANBF forwarders are 3DS-mode
- [shutterbug2000](https://github.com/shutterbug2000): SDK5 support, help with DSi mode support, and some other implemented stuff
- [ahezard](https://github.com/ahezard): Starting the project, former lead developer
- [Pk11](https://github.com/Epicpkmn11): In-game menu, screenshot taking, manual loading, and translation management
- [Gericom](https://github.com/Gericom): Improving B4DS compatibility, parts of libtwl code used, and SD -> flashcard R/W patch for DSiWare
- [Gericom](https://github.com/Gericom): Improving B4DS compatibility, parts of libtwl code used, Pokémon Wii connection patch, and SD -> flashcard R/W patch for DSiWare

## Other
- [devkitPro](https://devkitpro.org): devkitARM and libnds
Expand Down
1 change: 1 addition & 0 deletions retail/bootloaderi/include/find.h
Original file line number Diff line number Diff line change
Expand Up @@ -140,5 +140,6 @@ u32* findCardIrqEnableOffset(const tNDSHeader* ndsHeader, const module_params_t*
u32* findSdCardResetOffset(const tNDSHeader* ndsHeader, const module_params_t* moduleParams);
u32* findSdCardFuncsOffset(const tNDSHeader* ndsHeader);
u32* findAutoPowerOffOffset(const tNDSHeader* ndsHeader);
u32* findResetOffset7(const tNDSHeader* ndsHeader);

#endif // FIND_H
2 changes: 1 addition & 1 deletion retail/bootloaderi/include/patch.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@

//extern bool cardReadFound; // patch_arm9.c

#define patchOffsetCacheFileVersion 110 // Change when new functions are being patched, some offsets removed,
#define patchOffsetCacheFileVersion 111 // Change when new functions are being patched, some offsets removed,
// the offset order changed, and/or the function signatures changed (not added)

typedef struct patchOffsetCacheContents {
Expand Down
22 changes: 22 additions & 0 deletions retail/bootloaderi/source/arm7/find_arm7.c
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,9 @@ static const u16 irqEnableStartSignatureThumb[5] = {0xB530, 0xB081, 0x4D07, 0x8
static const u16 irqEnableStartSignatureThumb3[5] = {0xB510, 0x1C04, 0xF7FF, 0xFFF4, 0x4B05}; // SDK 3
static const u16 irqEnableStartSignatureThumb5[5] = {0xB510, 0x1C04, 0xF7FF, 0xFFE4, 0x4B05}; // SDK 5

// Reset
static const u32 resetSignature3Eoo[] = {0xE92D4070, 0xE59F0098, 0xE5904004, 0xE3540000}; // eoo.dat (Pokemon)

// ARM7i start (SDK 5)
//static const u32 a7iStartSignatureConstant[3] = {0x6F696473, 0x616C775F, 0x0000006E}; // 'sdio_wlan'
//static const u32 a7iStartSignatureType1[5] = {0xE12FFF1E, 0xE92D47F8, 0xE24DD014, 0xE1A07000, 0xE5971000};
Expand Down Expand Up @@ -1404,6 +1407,25 @@ u32* findCardIrqEnableOffset(const tNDSHeader* ndsHeader, const module_params_t*
return cardIrqEnableOffset;
}

u32* findResetOffset7(const tNDSHeader* ndsHeader) {
// dbg_printf("findResetOffset\n");

u32* resetOffset = findOffset(
ndsHeader->arm7destination, newArm7ibinarySize,
resetSignature3Eoo, 4
);

if (resetOffset) {
dbg_printf("Reset found\n");
dbg_printf("\n");
return resetOffset;
} else {
dbg_printf("Reset not found\n");
}

return NULL;
}

/*u32* findA7iStartOffset(void) {
dbg_printf("findA7iStartOffset:\n");
Expand Down
34 changes: 10 additions & 24 deletions retail/bootloaderi/source/arm7/find_arm9.c
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ static const u32 resetSignature2Alt1[4] = {0xE92D000F, 0xE92D4010, 0xEB000026, 0
static const u32 resetSignature2Alt2[4] = {0xE92D4010, 0xE59F1078, 0xE1A04000, 0xE1D100B0}; // sdk2
static const u32 resetSignature3[4] = {0xE92D4010, 0xE59F106C, 0xE1A04000, 0xE1D100B0}; // sdk3
static const u32 resetSignature3Alt[4] = {0xE92D4010, 0xE59F1068, 0xE1A04000, 0xE1D100B0}; // sdk3 and sdk4
static const u32 resetSignature3Mb[1] = {0xE92D4008}; // sdk3
static const u32 resetSignature3Eoo[4] = {0xE92D4010, 0xE59F003C, 0xE5904000, 0xE3540000}; // eoo.dat (Pokemon)
static const u32 resetSignature4[4] = {0xE92D4070, 0xE59F10A0, 0xE1A04000, 0xE1D100B0}; // sdk4
static const u32 resetSignature4Alt[4] = {0xE92D4010, 0xE59F1084, 0xE1A04000, 0xE1D100B0}; // sdk4
static const u32 resetSignature5[4] = {0xE92D4038, 0xE59F1054, 0xE1A05000, 0xE1D100B0}; // sdk5
Expand All @@ -277,7 +277,6 @@ static const u32 resetSignature5Alt3[4] = {0xE92D4038, 0xE59F106C, 0xE1A05000, 0
static const u32 resetSignature5Alt4[4] = {0xE92D4038, 0xE59F1090, 0xE1A05000, 0xE1D100B0}; // sdk5

static const u32 resetConstant[1] = {RESET_PARAM};
static const u32 resetConstantMb[1] = {0x027FFE34};
static const u32 resetConstant5[1] = {RESET_PARAM_SDK5};

// Reset (TWL)
Expand Down Expand Up @@ -2949,31 +2948,18 @@ u32* findResetOffset(const tNDSHeader* ndsHeader, const module_params_t* moduleP
u32 * resetOffset = NULL;

if ((memcmp(getRomTid(ndsHeader), "NTRJ", 4) == 0) && (moduleParams->sdk_version < 0x5000000)) {
u32* resetConstOffset = findOffset(
resetOffset = findOffset(
(u32*)ndsHeader->arm9destination, iUncompressedSize,//ndsHeader->arm9binarySize,
resetConstantMb, 1
resetSignature3Eoo, 4
);

if (resetConstOffset) {
resetOffset = findOffsetBackwards(
resetConstOffset, 0x80,
resetSignature3Mb, 1
);
if (!resetOffset) {
resetOffset = findOffsetBackwards(
resetConstOffset, 0x80,
resetSignature3, 1
);
}

if (resetOffset) {
dbg_printf("Reset found\n");
dbg_printf("\n");
*softResetMb = true;
return resetOffset;
} else {
dbg_printf("Reset not found\n");
}
if (resetOffset) {
dbg_printf("Reset found\n");
dbg_printf("\n");
*softResetMb = true;
return resetOffset;
} else {
dbg_printf("Reset not found\n");
}
}

Expand Down
15 changes: 15 additions & 0 deletions retail/bootloaderi/source/arm7/patch_arm7.c
Original file line number Diff line number Diff line change
Expand Up @@ -565,6 +565,17 @@ static void patchCardCheckPullOut(cardengineArm7* ce7, const tNDSHeader* ndsHead
}
}

static void patchReset(cardengineArm7* ce7, const tNDSHeader* ndsHeader, const module_params_t* moduleParams) {
u32* offset = findResetOffset7(ndsHeader);
if (!offset) {
return;
}

offset[0] = 0xE59F0000; // ldr r0, =reset
offset[1] = 0xE12FFF10; // bx r0
offset[2] = (u32)ce7->patches->reset;
}

static void patchSdCardReset(const tNDSHeader* ndsHeader, const module_params_t* moduleParams) {
if (ndsHeader->unitCode == 0 || !dsiModeConfirmed) return;

Expand Down Expand Up @@ -709,6 +720,10 @@ u32 patchCardNdsArm7(
patchCardCheckPullOut(ce7, ndsHeader, moduleParams);
}

if (patchOffsetCache.resetMb) {
patchReset(ce7, ndsHeader, moduleParams);
}

if (a7GetReloc(ndsHeader, moduleParams)) {
patchMirrorCheck(ndsHeader, moduleParams);
u32 saveResult = 0;
Expand Down
1 change: 1 addition & 0 deletions retail/cardenginei/arm7/include/patcher/find.h
Original file line number Diff line number Diff line change
Expand Up @@ -94,5 +94,6 @@ u16* findSleepPatchOffsetThumb(const tNDSHeader* ndsHeader);
u32* findSleepInputWriteOffset(const tNDSHeader* ndsHeader, const module_params_t* moduleParams);
u32* findCardCheckPullOutOffset(const tNDSHeader* ndsHeader, const module_params_t* moduleParams);
u32* findCardIrqEnableOffset(const tNDSHeader* ndsHeader, const module_params_t* moduleParams);
u32* findResetOffset7(const tNDSHeader* ndsHeader);

#endif // FIND_H
1 change: 1 addition & 0 deletions retail/cardenginei/arm7/source/card_engine_header.s
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,7 @@ patches:
.word 0
.word 0
#endif
.word reset
.pool
@---------------------------------------------------------------------------------

Expand Down
6 changes: 3 additions & 3 deletions retail/cardenginei/arm7/source/cardengine.c
Original file line number Diff line number Diff line change
Expand Up @@ -460,7 +460,7 @@ void reset(void) {
if (*(u32*)(resetParam+8) == 0x44414F4C) { // 'LOAD'
fileWrite((char*)ndsHeader, &pageFile, 0x2BFE00, 0x160);
fileWrite((char*)ndsHeader->arm9destination, &pageFile, 0x14000, ndsHeader->arm9binarySize);
fileWrite((char*)0x022C0000, &pageFile, 0x2C0000, ndsHeader->arm7binarySize);
fileWrite((char*)0x02380000, &pageFile, 0x2C0000, ndsHeader->arm7binarySize);
}
fileWrite((char*)resetParam, &srParamsFile, 0, 0x10);
toncset((u32*)0x02000000, 0, 0x400);
Expand Down Expand Up @@ -541,7 +541,7 @@ void reset(void) {
*(u16*)0x02fffc40 = 2; // Boot Indicator (Cloneboot/Multiboot)
} else {
if (*(u32*)(resetParam+8) == 0x44414F4C) {
ndmaCopyWordsAsynch(1, (u32*)0x022C0000, ndsHeader->arm7destination, ndsHeader->arm7binarySize);
// ndmaCopyWordsAsynch(1, (u32*)0x022C0000, ndsHeader->arm7destination, ndsHeader->arm7binarySize);
*((u16*)(/*isSdk5(moduleParams) ? 0x02fffc40 :*/ 0x027ffc40)) = 2; // Boot Indicator (Cloneboot/Multiboot)
// tonccpy((u32*)0x027FFC40, (u32*)0x02344820, 0x40); // Multiboot info?
} else if (valueBits & ROMinRAM) {
Expand Down Expand Up @@ -573,7 +573,7 @@ void reset(void) {
moduleParams,
1
);
while (ndmaBusy(1));
// while (ndmaBusy(1));
patchCardNdsArm7(
(cardengineArm7*)ce7,
ndsHeader,
Expand Down
22 changes: 22 additions & 0 deletions retail/cardenginei/arm7/source/patcher/find_arm7.c
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,9 @@ static const u16 irqEnableStartSignatureThumb[5] = {0xB530, 0xB081, 0x4D07, 0x8
static const u16 irqEnableStartSignatureThumb3[5] = {0xB510, 0x1C04, 0xF7FF, 0xFFF4, 0x4B05}; // SDK 3
static const u16 irqEnableStartSignatureThumb5[5] = {0xB510, 0x1C04, 0xF7FF, 0xFFE4, 0x4B05}; // SDK 5

// Reset
static const u32 resetSignature3Eoo[] = {0xE92D4070, 0xE59F0098, 0xE5904004, 0xE3540000}; // eoo.dat (Pokemon)

u32 relocationStart = 0;
bool a7GetReloc(const tNDSHeader* ndsHeader, const module_params_t* moduleParams) {
extern u32 vAddrOfRelocSrc;
Expand Down Expand Up @@ -1009,3 +1012,22 @@ u32* findCardIrqEnableOffset(const tNDSHeader* ndsHeader, const module_params_t*
//dbg_printf("\n");
return cardIrqEnableOffset;
}

u32* findResetOffset7(const tNDSHeader* ndsHeader) {
// dbg_printf("findResetOffset\n");

u32* resetOffset = findOffset(
ndsHeader->arm7destination, ndsHeader->arm7binarySize,
resetSignature3Eoo, 4
);

if (resetOffset) {
// dbg_printf("Reset found\n");
// dbg_printf("\n");
return resetOffset;
} else {
// dbg_printf("Reset not found\n");
}

return NULL;
}
36 changes: 11 additions & 25 deletions retail/cardenginei/arm7/source/patcher/find_arm9.c
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ static const u32 resetSignature2Alt1[4] = {0xE92D000F, 0xE92D4010, 0xEB000026, 0
static const u32 resetSignature2Alt2[4] = {0xE92D4010, 0xE59F1078, 0xE1A04000, 0xE1D100B0}; // sdk2
static const u32 resetSignature3[4] = {0xE92D4010, 0xE59F106C, 0xE1A04000, 0xE1D100B0}; // sdk3
static const u32 resetSignature3Alt[4] = {0xE92D4010, 0xE59F1068, 0xE1A04000, 0xE1D100B0}; // sdk3
static const u32 resetSignature3Mb[1] = {0xE92D4008}; // sdk3
static const u32 resetSignature3Eoo[4] = {0xE92D4010, 0xE59F003C, 0xE5904000, 0xE3540000}; // eoo.dat (Pokemon)
static const u32 resetSignature4[4] = {0xE92D4070, 0xE59F10A0, 0xE1A04000, 0xE1D100B0}; // sdk4
static const u32 resetSignature4Alt[4] = {0xE92D4010, 0xE59F1084, 0xE1A04000, 0xE1D100B0}; // sdk4
static const u32 resetSignature5[4] = {0xE92D4038, 0xE59F1054, 0xE1A05000, 0xE1D100B0}; // sdk5
Expand All @@ -176,7 +176,6 @@ static const u32 resetSignature5Alt3[4] = {0xE92D4038, 0xE59F106C, 0xE1A05000, 0
static const u32 resetSignature5Alt4[4] = {0xE92D4038, 0xE59F1090, 0xE1A05000, 0xE1D100B0}; // sdk5

static const u32 resetConstant[1] = {RESET_PARAM};
static const u32 resetConstantMb[1] = {0x027FFE34};
static const u32 resetConstant5[1] = {RESET_PARAM_SDK5};

// Panic
Expand Down Expand Up @@ -1936,32 +1935,19 @@ u32* findResetOffset(const tNDSHeader* ndsHeader, const module_params_t* moduleP
u32 * resetOffset = NULL;

if ((memcmp(getRomTid(ndsHeader), "NTRJ", 4) == 0) && (moduleParams->sdk_version < 0x5000000)) {
u32* resetConstOffset = findOffset(
resetOffset = findOffset(
(u32*)ndsHeader->arm9destination, iUncompressedSize,//ndsHeader->arm9binarySize,
resetConstantMb, 1
resetSignature3Eoo, 4
);

if (resetConstOffset) {
resetOffset = findOffsetBackwards(
resetConstOffset, 0x80,
resetSignature3Mb, 1
);
if (!resetOffset) {
resetOffset = findOffsetBackwards(
resetConstOffset, 0x80,
resetSignature3, 1
);
}

if (resetOffset) {
/* dbg_printf("Reset found\n");
dbg_printf("\n");*/
*softResetMb = true;
return resetOffset;
} /* else {
dbg_printf("Reset not found\n");
} */
}
if (resetOffset) {
/* dbg_printf("Reset found\n");
dbg_printf("\n");*/
*softResetMb = true;
return resetOffset;
} /* else {
dbg_printf("Reset not found\n");
} */
}

resetOffset = findOffset(
Expand Down
16 changes: 16 additions & 0 deletions retail/cardenginei/arm7/source/patcher/patch_arm7.c
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,17 @@ static void patchCardCheckPullOut(cardengineArm7* ce7, const tNDSHeader* ndsHead
}
}

static void patchReset(cardengineArm7* ce7, const tNDSHeader* ndsHeader, const module_params_t* moduleParams) {
u32* offset = findResetOffset7(ndsHeader);
if (!offset) {
return;
}

offset[0] = 0xE59F0000; // ldr r0, =reset
offset[1] = 0xE12FFF10; // bx r0
offset[2] = (u32)ce7->patches->reset;
}

u32 patchCardNdsArm7(
cardengineArm7* ce7,
tNDSHeader* ndsHeader,
Expand All @@ -245,6 +256,11 @@ u32 patchCardNdsArm7(
patchCardCheckPullOut(ce7, ndsHeader, moduleParams);
}

extern bool softResetMb;
if (softResetMb) {
patchReset(ce7, ndsHeader, moduleParams);
}

if (a7GetReloc(ndsHeader, moduleParams)) {
patchMirrorCheck(ndsHeader, moduleParams);
u32 saveResult = 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,7 @@ patches:
.word 0
.word 0
.word 0
.word 0
.pool
@---------------------------------------------------------------------------------

Expand Down
1 change: 1 addition & 0 deletions retail/common/include/cardengine_header_arm7.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ typedef struct cardengineArm7Patches {
u32* newSwiHaltThumb;
u32* j_twlGetPitchTable;
u32* j_twlGetPitchTableThumb;
u32* reset;
} cardengineArm7Patches;

//
Expand Down

0 comments on commit a69bf01

Please sign in to comment.