Skip to content

Commit

Permalink
Fix #1761
Browse files Browse the repository at this point in the history
  • Loading branch information
RocketRobz committed Jan 28, 2025
1 parent 30d2426 commit ef70ddd
Show file tree
Hide file tree
Showing 15 changed files with 123 additions and 70 deletions.
2 changes: 1 addition & 1 deletion retail/bootloaderi/include/patch.h
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ typedef struct patchOffsetCacheContents {
u32 ramClearChecked;
u32* ramClearIOffset;
u32* ramClearI2Offset;
u32* swiHaltOffset; // Unused, comment out when updating patchOffsetCacheContents
u32* swiHaltOffset;
u32* a7Swi12Offset;
u16* a7Swi24Offset;
u16* a7Swi25Offset;
Expand Down
8 changes: 4 additions & 4 deletions retail/bootloaderi/source/arm7/find_arm7.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@ static const u32 relocateStartSignature5Alt2[1] = {0x02FFFFFA};
static const u32 nextFunctiontSignature[1] = {0xE92D4000};
static const u32 relocateValidateSignature[1] = {0x400010C};

/* static const u32 swiHaltSignature1[1] = {0xE59FC004};
static const u32 swiHaltSignature1[1] = {0xE59FC004};
static const u32 swiHaltSignature2[1] = {0xE59FC000};
static const u16 swiHaltCmpSignature[1] = {0x2800};
static const u16 swiHaltMovSignature[1] = {0x200C};
static const u32 swiHaltConstSignature[1] = {0x4000004};
static const u32 swiHaltConstSignatureAlt[1] = {0x4000208}; */
static const u32 swiHaltConstSignatureAlt[1] = {0x4000208};

static const u32 swi12Signature[1] = {0x4770DF12}; // LZ77UnCompReadByCallbackWrite16bit
static const u16 swi24Signature[2] = {0xDF24,0x4770}; // SHA1_Init
Expand Down Expand Up @@ -325,7 +325,7 @@ bool a7GetReloc(const tNDSHeader* ndsHeader, const module_params_t* moduleParams
return true;
}

/* u32* findSwiHaltOffset(const tNDSHeader* ndsHeader, const module_params_t* moduleParams) {
u32* findSwiHaltOffset(const tNDSHeader* ndsHeader, const module_params_t* moduleParams) {
dbg_printf("findSwiHaltOffset:\n");

u32* swiHaltOffset = NULL;
Expand Down Expand Up @@ -415,7 +415,7 @@ u16* findSwiHaltThumbOffset(const tNDSHeader* ndsHeader, const module_params_t*

dbg_printf("\n");
return (u16*)swiHaltOffset;
} */
}

u32* a7_findSwi12Offset(const tNDSHeader* ndsHeader) {
dbg_printf("findSwi12Offset:\n");
Expand Down
7 changes: 4 additions & 3 deletions retail/bootloaderi/source/arm7/hook_arm9.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
#define b_saveOnFlashcard BIT(0)
#define b_ROMinRAM BIT(1)
#define b_eSdk2 BIT(2)
#define b_dsiMode BIT(3)
#define b_pingIpc BIT(3)
#define b_enableExceptionHandler BIT(4)
#define b_isSdk5 BIT(5)
#define b_overlaysCached BIT(6)
Expand Down Expand Up @@ -204,6 +204,7 @@ int hookNdsRetailArm9(
) {
nocashMessage("hookNdsRetailArm9");

extern bool pkmnGen5;
extern bool sharedWramEnabled;
extern bool scfgBios9i(void);
extern u32 iUncompressedSize;
Expand Down Expand Up @@ -234,8 +235,8 @@ int hookNdsRetailArm9(
if (!laterSdk) {
ce9->valueBits |= b_eSdk2;
}
if (dsiMode) {
ce9->valueBits |= b_dsiMode; // SDK 5
if (pkmnGen5) {
ce9->valueBits |= b_pingIpc;
}
if (enableExceptionHandler) {
ce9->valueBits |= b_enableExceptionHandler;
Expand Down
9 changes: 6 additions & 3 deletions retail/bootloaderi/source/arm7/patch_arm7.c
Original file line number Diff line number Diff line change
Expand Up @@ -150,10 +150,13 @@ u16* getOffsetFromBLThumb(const u16* blOffset) {

u32 vAddrOfRelocSrc = 0;
u32 relocDestAtSharedMem = 0;
/* u32 newSwiHaltAddr = 0;
u32 newSwiHaltAddr = 0;
// bool swiHaltPatched = false;

static void patchSwiHalt(const cardengineArm7* ce7, const tNDSHeader* ndsHeader, const module_params_t* moduleParams) {
extern bool pkmnGen5;
if (pkmnGen5) return;

u32* swiHaltOffset = patchOffsetCache.swiHaltOffset;
if (!patchOffsetCache.swiHaltOffset) {
swiHaltOffset = patchOffsetCache.a7IsThumb ? (u32*)findSwiHaltThumbOffset(ndsHeader, moduleParams) : findSwiHaltOffset(ndsHeader, moduleParams);
Expand Down Expand Up @@ -185,7 +188,7 @@ static void patchSwiHalt(const cardengineArm7* ce7, const tNDSHeader* ndsHeader,
dbg_printf("swiHalt location : ");
dbg_hexa((u32)swiHaltOffset);
dbg_printf("\n\n");
} */
}

void patchScfgExt(const tNDSHeader* ndsHeader) {
if (ndsHeader->unitCode == 0) return;
Expand Down Expand Up @@ -817,7 +820,7 @@ u32 patchCardNdsArm7(
}*/
}

// patchSwiHalt(ce7, ndsHeader, moduleParams);
patchSwiHalt(ce7, ndsHeader, moduleParams);

if (strcmp(romTid, "UBRP") == 0) {
operaRamPatch();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
extern u32 newArm7binarySize;
extern u32 vAddrOfRelocSrc;
extern u32 relocDestAtSharedMem;
// extern u32 newSwiHaltAddr;
extern u32 newSwiHaltAddr;

//
// Subroutine function signatures ARM7
Expand Down Expand Up @@ -246,7 +246,7 @@ u32 savePatchUniversal(const cardengineArm7* ce7, const tNDSHeader* ndsHeader, m
dbg_hexa((u32)eepromPageWrite);
dbg_printf("\n");
tonccpy(eepromPageWrite, (u16*)ce7->patches->arm7FunctionsThumb->eepromPageWrite, 0x14);
// newSwiHaltAddr = (u32)eepromPageWrite+0x14;
newSwiHaltAddr = (u32)eepromPageWrite+0x14;

u16* eepromPageProgBranch = (u16*)((u32)EepromProgJump + 0x6);
dbg_printf("Eeprom page prog branch:\t");
Expand Down Expand Up @@ -285,10 +285,10 @@ u32 savePatchUniversal(const cardengineArm7* ce7, const tNDSHeader* ndsHeader, m
*eepromRead = ce7->patches->arm7FunctionsDirect->eepromRead;

u32* eepromPageWrite = (u32*)((u32)EepromWriteJump + 0xA);
/* newSwiHaltAddr = *eepromPageWrite;
newSwiHaltAddr = *eepromPageWrite;
newSwiHaltAddr -= 0x37F8000;
newSwiHaltAddr += vAddrOfRelocSrc;
newSwiHaltAddr--; */
newSwiHaltAddr--;
dbg_printf("Eeprom page write:\t");
dbg_hexa((u32)eepromPageWrite);
dbg_printf("\n");
Expand Down Expand Up @@ -420,10 +420,10 @@ u32 savePatchInvertedThumb(const cardengineArm7* ce7, const tNDSHeader* ndsHeade
dbg_printf("\n");

u32* eepromPageWrite = (u32*)((u32)EepromWriteJump + 0xA);
/* newSwiHaltAddr = *eepromPageWrite;
newSwiHaltAddr = *eepromPageWrite;
newSwiHaltAddr -= 0x37F8000;
newSwiHaltAddr += vAddrOfRelocSrc;
newSwiHaltAddr--; */
newSwiHaltAddr--;
dbg_printf("Eeprom page write:\t");
dbg_hexa((u32)eepromPageWrite);
dbg_printf("\t:\t");
Expand Down
4 changes: 2 additions & 2 deletions retail/bootloaderi/source/arm7/save_patches/save_patch_v5.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
extern u32 newArm7binarySize;
extern u32 vAddrOfRelocSrc;
extern u32 relocDestAtSharedMem;
// extern u32 newSwiHaltAddr;
extern u32 newSwiHaltAddr;

//
// Subroutine function signatures ARM7
Expand Down Expand Up @@ -93,7 +93,7 @@ u32 savePatchV5(const cardengineArm7* ce7, const tNDSHeader* ndsHeader, const u3
dbg_hexa((u32)eepromPageWrite);
dbg_printf("\n");
tonccpy(eepromPageWrite, (u16*)ce7->patches->arm7FunctionsThumb->eepromPageWrite, 0x14);
// newSwiHaltAddr = (u32)eepromPageWrite+0x14;
newSwiHaltAddr = (u32)eepromPageWrite+0x14;

u16* eepromPageProgBranch = (u16*)(JumpTableFunc + 0x24);
dbg_printf("Eeprom page prog branch:\t");
Expand Down
2 changes: 1 addition & 1 deletion retail/cardenginei/arm7/source/card_engine_header.s
Original file line number Diff line number Diff line change
Expand Up @@ -389,7 +389,7 @@ j_newSwiHalt:
newSwiHalt:
@---------------------------------------------------------------------------------
push {lr}
bl runCardEngineCheck
bl runCardEngineCheckHalt
swi #0x060000
pop {pc}
@---------------------------------------------------------------------------------
Expand Down
100 changes: 71 additions & 29 deletions retail/cardenginei/arm7/source/cardengine.c
Original file line number Diff line number Diff line change
Expand Up @@ -1524,51 +1524,93 @@ void runCardEngineCheck(void) {
nocashMessage("runCardEngineCheck");
#endif

// if (lockMutex(&cardEgnineCommandMutex)) {
// if (tryLockMutex(&cardEgnineCommandMutex)) {
//if(!readOngoing)
//{

//nocashMessage("runCardEngineCheck mutex ok");

/*if (sharedAddr[3] == (vu32)0x5245424F) {
i2cWriteRegister(0x4A, 0x70, 0x01);
i2cWriteRegister(0x4A, 0x11, 0x01);
}*/
if (!(valueBits & gameOnFlashcard)) {
if (/* sharedAddr[3] == (vu32)0x020FF808 || sharedAddr[3] == (vu32)0x020FF80A || */ sharedAddr[3] >= (vu32)0x025FFB08 && sharedAddr[3] <= (vu32)0x025FFB0A) { // ARM9 Card Read
const bool isDma = (sharedAddr[3] == (vu32)0x025FFB0A);
if (!readOngoing ? start_cardRead_arm9() : resume_cardRead_arm9()) {
sharedAddr[3] = 0;
}
if (isDma) {
IPC_SendSync(0x3);
}
}
}

/* #ifdef DEBUG
if (sharedAddr[3] == (vu32)0x026FF800) {
log_arm9();
sharedAddr[3] = 0;
//IPC_SendSync(0x8);
} else
#endif
if (sharedAddr[3] == (vu32)0x025FFC01) {
//dmaLed = (sharedAddr[3] == (vu32)0x025FFC01);
nandRead();
sharedAddr[3] = 0;
} else if (sharedAddr[3] == (vu32)0x025FFC02) {
//dmaLed = (sharedAddr[3] == (vu32)0x025FFC02);
nandWrite();
sharedAddr[3] = 0;
} */

/*if (sharedAddr[3] == (vu32)0x025FBC01) {
dmaLed = false;
slot2Read();
sharedAddr[3] = 0;
IPC_SendSync(0x8);
}*/
//}
// unlockMutex(&cardEgnineCommandMutex);
// }
}

void runCardEngineCheckHalt(void) {
//dbg_printf("runCardEngineCheckHalt\n");
#ifdef DEBUG
nocashMessage("runCardEngineCheckHalt");
#endif

// if (lockMutex(&cardEgnineCommandMutex)) {
//if(!readOngoing)
//{

//nocashMessage("runCardEngineCheck mutex ok");

if (!(valueBits & gameOnFlashcard)) {
/* #ifndef TWLSDK
loadROMPartIntoRAM();
#endif */
if (/* sharedAddr[3] == (vu32)0x020FF808 || sharedAddr[3] == (vu32)0x020FF80A || */ sharedAddr[3] >= (vu32)0x025FFB08 && sharedAddr[3] <= (vu32)0x025FFB0A) { // ARM9 Card Read
const bool isDma = (sharedAddr[3] == (vu32)0x025FFB0A);
if (!readOngoing ? start_cardRead_arm9() : resume_cardRead_arm9()) {
/* bool useApFixOverlays = false;
u32 src = sharedAddr[2];
u32 dst = sharedAddr[0];
u32 len = sharedAddr[1];
if (src >= 0x80000000) {
src -= 0x80000000;
useApFixOverlays = true;
}
// readOngoing = true;
if (lockMutex(&saveMutex)) {
cardReadLED(true, isDma); // When a file is loading, turn on LED for card read indicator
fileRead((char*)dst, useApFixOverlays ? apFixOverlaysFile : romFile, src, len);
cardReadLED(false, isDma); // After loading is done, turn off LED for card read indicator
unlockMutex(&saveMutex);
}
// readOngoing = false; */
sharedAddr[3] = 0;
bool useApFixOverlays = false;
u32 src = sharedAddr[2];
u32 dst = sharedAddr[0];
u32 len = sharedAddr[1];
if (src >= 0x80000000) {
src -= 0x80000000;
useApFixOverlays = true;
}

// readOngoing = true;
if (lockMutex(&saveMutex)) {
cardReadLED(true, isDma); // When a file is loading, turn on LED for card read indicator
fileRead((char*)dst, useApFixOverlays ? apFixOverlaysFile : romFile, src, len);
cardReadLED(false, isDma); // After loading is done, turn off LED for card read indicator
unlockMutex(&saveMutex);
}
// readOngoing = false;
sharedAddr[3] = 0;
if (isDma) {
IPC_SendSync(0x3);
}
} /*else if (sharedAddr[3] == (vu32)0x026FFB0A) { // Card read DMA (Card data cache)
ndmaCopyWords(0, (u8*)sharedAddr[2], (u8*)(sharedAddr[0] >= 0x03000000 ? 0 : sharedAddr[0]), sharedAddr[1]);
sharedAddr[3] = 0;
IPC_SendSync(0x3);
}*/
}
}

#ifdef DEBUG
Expand Down
8 changes: 4 additions & 4 deletions retail/cardenginei/arm7/source/patcher/find_arm7.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ static const u32 relocateStartSignature5Alt[1] = {0x2106C0DE};
static const u32 nextFunctiontSignature[1] = {0xE92D4000};
static const u32 relocateValidateSignature[1] = {0x400010C};

/* static const u32 swiHaltSignature1[1] = {0xE59FC004};
static const u32 swiHaltSignature1[1] = {0xE59FC004};
static const u32 swiHaltSignature2[1] = {0xE59FC000};
static const u16 swiHaltCmpSignature[1] = {0x2800};
static const u32 swiHaltConstSignature[1] = {0x4000004};
static const u32 swiHaltConstSignatureAlt[1] = {0x4000208}; */
static const u32 swiHaltConstSignatureAlt[1] = {0x4000208};

static const u32 swi12Signature[1] = {0x4770DF12}; // LZ77UnCompReadByCallbackWrite16bit

Expand Down Expand Up @@ -223,7 +223,7 @@ bool a7GetReloc(const tNDSHeader* ndsHeader, const module_params_t* moduleParams
return true;
}

/* u32* findSwiHaltOffset(const tNDSHeader* ndsHeader, const module_params_t* moduleParams) {
u32* findSwiHaltOffset(const tNDSHeader* ndsHeader, const module_params_t* moduleParams) {
// dbg_printf("findSwiHaltOffset:\n");

u32* swiHaltOffset = NULL;
Expand Down Expand Up @@ -299,7 +299,7 @@ u16* findSwiHaltThumbOffset(const tNDSHeader* ndsHeader, const module_params_t*

// dbg_printf("\n");
return (u16*)swiHaltOffset;
} */
}

u32* a7_findSwi12Offset(const tNDSHeader* ndsHeader) {
//dbg_printf("findSwi12Offset:\n");
Expand Down
4 changes: 1 addition & 3 deletions retail/cardenginei/arm7/source/patcher/patch_arm7.c
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ u32 vAddrOfRelocSrc = 0;
u32 relocDestAtSharedMem = 0;
bool a7IsThumb = false;

#ifdef UNUSED
u32 newSwiHaltAddr = 0;
// bool swiHaltPatched = false;

Expand Down Expand Up @@ -92,7 +91,6 @@ static void patchSwiHalt(const cardengineArm7* ce7, const tNDSHeader* ndsHeader,
dbg_hexa((u32)swiHaltOffset);
dbg_printf("\n\n"); */
}
#endif

static void fixForDifferentBios(const cardengineArm7* ce7, const tNDSHeader* ndsHeader, const module_params_t* moduleParams) {
if (scfgRomBak & BIT(9)) {
Expand Down Expand Up @@ -395,7 +393,7 @@ u32 patchCardNdsArm7(
}
}

// patchSwiHalt(ce7, ndsHeader, moduleParams);
patchSwiHalt(ce7, ndsHeader, moduleParams);

fixForDifferentBios(ce7, ndsHeader, moduleParams);

Expand Down
12 changes: 6 additions & 6 deletions retail/cardenginei/arm7/source/patcher/save_patch_universal.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

extern u32 vAddrOfRelocSrc;
extern u32 relocDestAtSharedMem;
// extern u32 newSwiHaltAddr;
extern u32 newSwiHaltAddr;

//
// Subroutine function signatures ARM7
Expand Down Expand Up @@ -235,7 +235,7 @@ u32 savePatchUniversal(const cardengineArm7* ce7, const tNDSHeader* ndsHeader, m
dbg_hexa((u32)eepromPageWrite);
dbg_printf("\n");*/
tonccpy(eepromPageWrite, (u16*)ce7->patches->arm7FunctionsThumb->eepromPageWrite, 0x14);
// newSwiHaltAddr = (u32)eepromPageWrite+0x14;
newSwiHaltAddr = (u32)eepromPageWrite+0x14;

u16* eepromPageProgBranch = (u16*)((u32)EepromProgJump + 0x6);
/*dbg_printf("Eeprom page prog branch:\t");
Expand Down Expand Up @@ -274,10 +274,10 @@ u32 savePatchUniversal(const cardengineArm7* ce7, const tNDSHeader* ndsHeader, m
*eepromRead = ce7->patches->arm7Functions->eepromRead;

u32* eepromPageWrite = (u32*)((u32)EepromWriteJump + 0xA);
/* newSwiHaltAddr = *eepromPageWrite;
newSwiHaltAddr = *eepromPageWrite;
newSwiHaltAddr -= 0x37F8000;
newSwiHaltAddr += vAddrOfRelocSrc;
newSwiHaltAddr--; */
newSwiHaltAddr--;
/*dbg_printf("Eeprom page write:\t");
dbg_hexa((u32)eepromPageWrite);
dbg_printf("\n");*/
Expand Down Expand Up @@ -404,10 +404,10 @@ u32 savePatchInvertedThumb(const cardengineArm7* ce7, const tNDSHeader* ndsHeade
dbg_printf("\n"); */

u32* eepromPageWrite = (u32*)((u32)EepromWriteJump + 0xA);
/* newSwiHaltAddr = *eepromPageWrite;
newSwiHaltAddr = *eepromPageWrite;
newSwiHaltAddr -= 0x37F8000;
newSwiHaltAddr += vAddrOfRelocSrc;
newSwiHaltAddr--; */
newSwiHaltAddr--;
/* dbg_printf("Eeprom page write:\t");
dbg_hexa((u32)eepromPageWrite);
dbg_printf("\t:\t");
Expand Down
Loading

0 comments on commit ef70ddd

Please sign in to comment.