Skip to content

Commit

Permalink
Fix cloneboot not working with DSi-Enhanced ROMs running in DSi mode
Browse files Browse the repository at this point in the history
  • Loading branch information
RocketRobz committed Jan 22, 2024
1 parent d551492 commit 7036eba
Show file tree
Hide file tree
Showing 8 changed files with 44 additions and 36 deletions.
2 changes: 1 addition & 1 deletion retail/bootloader/source/arm7/main.arm7.c
Original file line number Diff line number Diff line change
Expand Up @@ -1259,7 +1259,7 @@ int arm7_main(void) {
REG_SCFG_ROM = 0x703;

u32 clonebootFlag = 0;
fileRead((char*)&clonebootFlag, &romFile, ((romSize-4) <= baseRomSize) ? (romSize-4) : baseRomSize, sizeof(u32));
fileRead((char*)&clonebootFlag, &romFile, ((romSize-0x88) <= baseRomSize) ? (romSize-0x88) : baseRomSize, sizeof(u32));
bool usesCloneboot = (clonebootFlag == 0x16361);
if (usesCloneboot) {
dbg_printf("Cloneboot detected\n");
Expand Down
4 changes: 2 additions & 2 deletions retail/bootloaderi/include/find.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@ u16* findCardReadStartOffsetThumb5Type0(const module_params_t* moduleParams, con
u16* findCardReadStartOffsetThumb5Type1(const module_params_t* moduleParams, const u16* cardReadEndOffset); // SDK 5
u32* findCardReadCachedEndOffset(const tNDSHeader* ndsHeader, const module_params_t* moduleParams);
u32* findCardReadCachedStartOffset(const module_params_t* moduleParams, const u32* cardReadCachedEndOffset);
u32* findCardRomInitOffset(const u32* cardReadEndOffset);
u16* findCardRomInitOffsetThumb(const u16* cardReadEndOffset);
// u32* findCardRomInitOffset(const u32* cardReadEndOffset);
// u16* findCardRomInitOffsetThumb(const u16* cardReadEndOffset);
//u32* findCardReadHashOffset(void);
u32* findCardPullOutOffset(const tNDSHeader* ndsHeader, const module_params_t* moduleParams);
u16* findCardPullOutOffsetThumb(const tNDSHeader* ndsHeader);
Expand Down
2 changes: 1 addition & 1 deletion retail/bootloaderi/include/patch.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ typedef struct patchOffsetCacheContents {
u32* heapPointerOffset;
u32 a9IsThumb;
u32* cardHashInitOffset;
u32* cardRomInitOffset;
u32* cardRomInitOffset; // Unused, remove when updating cache version
u32* cardReadStartOffset;
u32* cardReadEndOffset;
u32* cardPullOutOffset;
Expand Down
14 changes: 7 additions & 7 deletions retail/bootloaderi/source/arm7/find_arm9.c
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,11 @@ static const u16 cardReadStartSignatureThumb5Alt[1] = {0xB5F8};
//static const u32 cardReadHashSignature[3] = {0xE92D4010, 0xE59F000C, 0xE1A04003}; // SDK 5

// Card init (SDK 5)
static const u32 cardRomInitSignatureEarly[2] = {0xE92D4078, 0xE24DD00C};
static const u32 cardRomInitSignature[2] = {0xE92D4010, 0xE59F1064};
static const u32 cardRomInitSignatureAlt[2] = {0xE92D4010, 0xE59F1070};
static const u16 cardRomInitSignatureEarlyThumb[2] = {0xB578, 0xB083};
static const u16 cardRomInitSignatureThumb[2] = {0xB510, 0x4915};
// static const u32 cardRomInitSignatureEarly[2] = {0xE92D4078, 0xE24DD00C};
// static const u32 cardRomInitSignature[2] = {0xE92D4010, 0xE59F1064};
// static const u32 cardRomInitSignatureAlt[2] = {0xE92D4010, 0xE59F1070};
// static const u16 cardRomInitSignatureEarlyThumb[2] = {0xB578, 0xB083};
// static const u16 cardRomInitSignatureThumb[2] = {0xB510, 0x4915};

//static const u32 instructionBHI[1] = {0x8A000001};

Expand Down Expand Up @@ -862,7 +862,7 @@ u16* findCardReadStartOffsetThumb5Type1(const module_params_t* moduleParams, con
return cardReadStartOffset;
}

u32* findCardRomInitOffset(const u32* cardReadEndOffset) {
/* u32* findCardRomInitOffset(const u32* cardReadEndOffset) {
dbg_printf("findCardRomInitOffset\n");
u32* offset = findOffset(
Expand Down Expand Up @@ -931,7 +931,7 @@ u16* findCardRomInitOffsetThumb(const u16* cardReadEndOffset) {
dbg_printf("\n");
return offset;
}
} */

/*u32* findCardReadHashOffset(void) {
dbg_printf("findCardReadHashOffset\n");
Expand Down
7 changes: 6 additions & 1 deletion retail/bootloaderi/source/arm7/main.arm7.c
Original file line number Diff line number Diff line change
Expand Up @@ -1941,7 +1941,8 @@ int arm7_main(void) {
}

u32 clonebootFlag = 0;
fileRead((char*)&clonebootFlag, romFile, ((romSize-4) <= baseRomSize) ? (romSize-4) : baseRomSize, sizeof(u32));
const u32 clonebootOffset = ((romSize-0x88) <= baseRomSize) ? (romSize-0x88) : baseRomSize;
fileRead((char*)&clonebootFlag, romFile, clonebootOffset, sizeof(u32));
const bool usesCloneboot = (clonebootFlag == 0x16361);
if (usesCloneboot) {
dbg_printf("Cloneboot detected\n");
Expand Down Expand Up @@ -2127,6 +2128,10 @@ int arm7_main(void) {
loadOverlaysintoRAM(ndsHeader, moduleParams, romFile);
}

if (ROMsupportsDsiMode(ndsHeader) && usesCloneboot) {
fileRead((char*)0x02FFDC00, romFile, clonebootOffset, 0x88); // Pre-load RSA key
}

if (useApPatch) {
if (applyIpsPatch(ndsHeader, (u8*)IPS_LOCATION, (*(u8*)(IPS_LOCATION+apPatchSize-1) == 0xA9), (moduleParams->sdk_version < 0x2008000 && moduleParams->sdk_version != 0x20029A8), isSdk5(moduleParams), ROMinRAM, usesCloneboot)) {
dbg_printf("AP-fix applied\n");
Expand Down
4 changes: 2 additions & 2 deletions retail/bootloaderi/source/arm7/patch_arm7.c
Original file line number Diff line number Diff line change
Expand Up @@ -588,13 +588,13 @@ u32 patchCardNdsArm7(
newArm7binarySize = ndsHeader->arm7binarySize;
newArm7ibinarySize = __DSiHeader->arm7ibinarySize;

if ((ndsHeader->unitCode > 0) ? (REG_SCFG_EXT == 0) : (memcmp(ndsHeader->gameCode, "AYI", 3) == 0 && ndsHeader->arm7binarySize == 0x25F70) && *(u32*)DONOR_ROM_ARM7_SIZE_LOCATION != 0) {
/* if ((ndsHeader->unitCode > 0) ? (REG_SCFG_EXT == 0) : (memcmp(ndsHeader->gameCode, "AYI", 3) == 0 && ndsHeader->arm7binarySize == 0x25F70) && *(u32*)DONOR_ROM_ARM7_SIZE_LOCATION != 0) {
// Replace incompatible ARM7 binary
newArm7binarySize = *(u32*)DONOR_ROM_ARM7_SIZE_LOCATION;
newArm7ibinarySize = *(u32*)DONOR_ROM_ARM7I_SIZE_LOCATION;
tonccpy(ndsHeader->arm7destination, (u8*)DONOR_ROM_ARM7_LOCATION, newArm7binarySize);
toncset((u8*)DONOR_ROM_ARM7_LOCATION, 0, 0x30010);
}
} */

if (newArm7binarySize != patchOffsetCache.a7BinSize) {
rsetA7Cache();
Expand Down
6 changes: 3 additions & 3 deletions retail/bootloaderi/source/arm7/patch_arm9.c
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ static bool patchCardHashInit(const tNDSHeader* ndsHeader, const module_params_t
return true;
}

static void patchCardRomInit(u32* cardReadEndOffset, bool usesThumb) {
/* static void patchCardRomInit(u32* cardReadEndOffset, bool usesThumb) {
u32* cardRomInitOffset = patchOffsetCache.cardRomInitOffset;
if (!patchOffsetCache.cardRomInitOffset) {
cardRomInitOffset = usesThumb ? (u32*)findCardRomInitOffsetThumb((u16*)cardReadEndOffset) : findCardRomInitOffset(cardReadEndOffset);
Expand All @@ -174,7 +174,7 @@ static void patchCardRomInit(u32* cardReadEndOffset, bool usesThumb) {
dbg_printf("cardRomInit location : ");
dbg_hexa((u32)cardRomInitOffset);
dbg_printf("\n\n");
}
} */

static bool patchCardRead(cardengineArm9* ce9, const tNDSHeader* ndsHeader, const module_params_t* moduleParams, bool* usesThumbPtr, int* readTypePtr, int* sdk5ReadTypePtr, u32** cardReadEndOffsetPtr, u32 startOffset) {
bool usesThumb = patchOffsetCache.a9IsThumb;
Expand Down Expand Up @@ -2593,7 +2593,7 @@ u32 patchCardNdsArm9(cardengineArm9* ce9, const tNDSHeader* ndsHeader, const mod
fixForDifferentBios(ce9, ndsHeader, usesThumb, usesCloneboot);

if (ndsHeader->unitCode > 0 && dsiModeConfirmed) {
patchCardRomInit(cardReadEndOffset, usesThumb);
// patchCardRomInit(cardReadEndOffset, usesThumb);

if (!patchCardHashInit(ndsHeader, moduleParams, usesThumb)) {
dbg_printf("ERR_LOAD_OTHR\n\n");
Expand Down
41 changes: 22 additions & 19 deletions retail/cardenginei/arm9/source/cardengine.c
Original file line number Diff line number Diff line change
Expand Up @@ -600,12 +600,33 @@ extern void region0Fix();

void cardRead(u32* cacheStruct, u8* dst0, u32 src0, u32 len0) {
//nocashMessage("\narm9 cardRead\n");
#ifndef TWLSDK
#ifdef TWLSDK
u32 src = src0;
u8* dst = dst0;
u32 len = len0;

if (src == ndsHeader->romSize) {
tonccpy(dst, (u8*)0x02FFDC00, len); // Load pre-loaded RSA key
return;
}
#else
#ifdef GSDD
u32 src = src0;
u8* dst = dst0;
u32 len = len0;
#else
initialize();

if (!(ce9->valueBits & isSdk5) && !(ce9->valueBits & ROMinRAM)) {
debugRamMpuFix();
}

vu32* volatile cardStruct = (vu32* volatile)ce9->cardStruct0;

u32 src = ((ce9->valueBits & isSdk5) ? src0 : cardStruct[0]);
u8* dst = ((ce9->valueBits & isSdk5) ? dst0 : (u8*)(cardStruct[1]));
u32 len = ((ce9->valueBits & isSdk5) ? len0 : cardStruct[2]);
#endif
#endif

if (!flagsSet) {
Expand All @@ -626,24 +647,6 @@ void cardRead(u32* cacheStruct, u8* dst0, u32 src0, u32 len0) {

enableIPC_SYNC();

#ifdef TWLSDK
u32 src = src0;
u8* dst = dst0;
u32 len = len0;
#else
#ifdef GSDD
u32 src = src0;
u8* dst = dst0;
u32 len = len0;
#else
vu32* volatile cardStruct = (vu32* volatile)ce9->cardStruct0;

u32 src = ((ce9->valueBits & isSdk5) ? src0 : cardStruct[0]);
u8* dst = ((ce9->valueBits & isSdk5) ? dst0 : (u8*)(cardStruct[1]));
u32 len = ((ce9->valueBits & isSdk5) ? len0 : cardStruct[2]);
#endif
#endif

// Simulate ROM mirroring
while (src >= ce9->romPaddingSize) {
src -= ce9->romPaddingSize;
Expand Down

0 comments on commit 7036eba

Please sign in to comment.