Skip to content

Commit

Permalink
Patch apps received via Download Play to read save from SD instead of…
Browse files Browse the repository at this point in the history
… cartridge

Currently makes the received app not boot
  • Loading branch information
RocketRobz committed Dec 18, 2023
1 parent 8c00881 commit c5ff18b
Show file tree
Hide file tree
Showing 14 changed files with 453 additions and 28 deletions.
9 changes: 7 additions & 2 deletions retail/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ export LIBPATHS := $(foreach dir,$(LIBDIRS),-L$(dir)/lib)
export GAME_ICON := $(CURDIR)/$(ASSETS)/icon.bmp

#.PHONY: cardengine_arm7 cardengine_arm9 bootloader BootStrap clean
.PHONY: all dist release nightly bootloader bootloader2 bootloaderi bootloaderi2 cardengine_arm7 cardengine_arm9_igm cardengine_arm9_igm_extmem cardengine_arm9 cardengine_arm9_start cardengine_arm9_alt cardengine_arm9_alt2 cardengine_arm9_extmem cardenginei_arm7 cardenginei_arm7_alt cardenginei_arm7_twlsdk cardenginei_arm7_twlsdk3 cardenginei_arm7_dsiware cardenginei_arm7_dsiware3 cardenginei_arm7_cheat cardenginei_arm9_igm cardenginei_arm9 cardenginei_arm9_alt cardenginei_arm9_alt2 cardenginei_arm9_gsdd cardenginei_arm9_gsdd_alt cardenginei_arm9_dldi cardenginei_arm9_gsdd_dldi cardenginei_arm9_twlsdk cardenginei_arm9_twlsdk3 cardenginei_arm9_twlsdk_dldi cardenginei_arm9_twlsdk3_dldi cardenginei_arm9_dsiware cardenginei_arm9_dsiware3 arm7/$(TARGET).elf arm9/$(TARGET).elf clean
.PHONY: all dist release nightly bootloader bootloader2 bootloaderi bootloaderi2 cardengine_arm7 cardengine_arm9_igm cardengine_arm9_igm_extmem cardengine_arm9 cardengine_arm9_start cardengine_arm9_alt cardengine_arm9_alt2 cardengine_arm9_extmem cardenginei_arm7 cardenginei_arm7_alt cardenginei_arm7_twlsdk cardenginei_arm7_twlsdk3 cardenginei_arm7_dsiware cardenginei_arm7_dsiware3 cardenginei_arm7_cheat cardenginei_arm9_igm cardenginei_arm9 cardenginei_arm9_alt cardenginei_arm9_alt2 cardenginei_arm9_dlp cardenginei_arm9_gsdd cardenginei_arm9_gsdd_alt cardenginei_arm9_dldi cardenginei_arm9_gsdd_dldi cardenginei_arm9_twlsdk cardenginei_arm9_twlsdk3 cardenginei_arm9_twlsdk_dldi cardenginei_arm9_twlsdk3_dldi cardenginei_arm9_dsiware cardenginei_arm9_dsiware3 arm7/$(TARGET).elf arm9/$(TARGET).elf clean

all: $(OUTPUT)

Expand Down Expand Up @@ -169,7 +169,7 @@ bootloader2: $(DATA)
@$(MAKE) -C bootloader2

#---------------------------------------------------------------------------------
bootloaderi: $(DATA) cardenginei_arm7 cardenginei_arm7_alt cardenginei_arm7_twlsdk cardenginei_arm7_twlsdk3 cardenginei_arm7_dsiware cardenginei_arm7_dsiware3 cardenginei_arm7_cheat cardenginei_arm9_igm cardenginei_arm9 cardenginei_arm9_alt cardenginei_arm9_alt2 cardenginei_arm9_gsdd cardenginei_arm9_gsdd_alt cardenginei_arm9_dldi cardenginei_arm9_gsdd_dldi cardenginei_arm9_twlsdk cardenginei_arm9_twlsdk3 cardenginei_arm9_twlsdk_dldi cardenginei_arm9_twlsdk3_dldi cardenginei_arm9_dsiware cardenginei_arm9_dsiware3
bootloaderi: $(DATA) cardenginei_arm7 cardenginei_arm7_alt cardenginei_arm7_twlsdk cardenginei_arm7_twlsdk3 cardenginei_arm7_dsiware cardenginei_arm7_dsiware3 cardenginei_arm7_cheat cardenginei_arm9_igm cardenginei_arm9 cardenginei_arm9_alt cardenginei_arm9_alt2 cardenginei_arm9_dlp cardenginei_arm9_gsdd cardenginei_arm9_gsdd_alt cardenginei_arm9_dldi cardenginei_arm9_gsdd_dldi cardenginei_arm9_twlsdk cardenginei_arm9_twlsdk3 cardenginei_arm9_twlsdk_dldi cardenginei_arm9_twlsdk3_dldi cardenginei_arm9_dsiware cardenginei_arm9_dsiware3
@$(MAKE) -C bootloaderi

#---------------------------------------------------------------------------------
Expand Down Expand Up @@ -252,6 +252,10 @@ cardenginei_arm9_alt: $(DATA)
cardenginei_arm9_alt2: $(DATA)
@$(MAKE) -C cardenginei/arm9_alt2

#---------------------------------------------------------------------------------
cardenginei_arm9_dlp: $(DATA)
@$(MAKE) -C cardenginei/arm9_dlp

#---------------------------------------------------------------------------------
cardenginei_arm9_gsdd: $(DATA)
@$(MAKE) -C cardenginei/arm9_gsdd
Expand Down Expand Up @@ -323,6 +327,7 @@ clean:
@$(MAKE) -C cardenginei/arm9 clean
@$(MAKE) -C cardenginei/arm9_alt clean
@$(MAKE) -C cardenginei/arm9_alt2 clean
@$(MAKE) -C cardenginei/arm9_dlp clean
@$(MAKE) -C cardenginei/arm9_gsdd clean
@$(MAKE) -C cardenginei/arm9_gsdd_alt clean
@$(MAKE) -C cardenginei/arm9_dldi clean
Expand Down
10 changes: 7 additions & 3 deletions retail/arm9/source/conf_sd.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1166,7 +1166,8 @@ int loadFromSD(configuration* conf, const char *bootstrapPath) {
conf->dsiWramAccess = true;
}
if (conf->dsiWramAccess) {
if (strncmp(romTid, "ADA", 3) == 0 // Diamond
if (strncmp(romTid, "HND", 3) == 0 // DS Download Play
|| strncmp(romTid, "ADA", 3) == 0 // Diamond
|| strncmp(romTid, "APA", 3) == 0 // Pearl
|| strncmp(romTid, "Y3E", 3) == 0 // 2006-Nen 10-Gatsu Taikenban Soft
|| strncmp(romTid, "CPU", 3) == 0 // Platinum
Expand Down Expand Up @@ -1235,6 +1236,7 @@ int loadFromSD(configuration* conf, const char *bootstrapPath) {
}
fclose(cebin);

const bool dlp = (memcmp(romTid, "HND", 3) == 0);
const bool gsdd = (memcmp(romTid, "BO5", 3) == 0);

if (conf->gameOnFlashcard) {
Expand All @@ -1252,7 +1254,9 @@ int loadFromSD(configuration* conf, const char *bootstrapPath) {
fclose(cebin);
} else {
const char* ce9Path = conf->dsiWramAccess ? "nitro:/cardenginei_arm9.lz77" : "nitro:/cardenginei_arm9_alt.lz77";
if (gsdd) {
if (dlp) {
ce9Path = "nitro:/cardenginei_arm9_dlp.lz77";
} else if (gsdd) {
ce9Path = conf->dsiWramAccess ? "nitro:/cardenginei_arm9_gsdd.lz77" : "nitro:/cardenginei_arm9_gsdd_alt.lz77";
}

Expand All @@ -1264,7 +1268,7 @@ int loadFromSD(configuration* conf, const char *bootstrapPath) {
}
fclose(cebin);

if (!conf->dsiWramAccess && !gsdd) {
if (!conf->dsiWramAccess && !dlp && !gsdd) {
// Load ce9 binary (alt 2)
cebin = fopen("nitro:/cardenginei_arm9_alt2.lz77", "rb");
if (cebin) {
Expand Down
4 changes: 4 additions & 0 deletions retail/bootloaderi/source/arm7/hook_arm7.c
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
#define b_dsiBios BIT(18)
#define b_bootstrapOnFlashcard BIT(19)
#define b_ndmaDisabled BIT(20)
#define b_isDlp BIT(21)
#define b_scfgLocked BIT(31)

extern u32 newArm7binarySize;
Expand Down Expand Up @@ -399,6 +400,9 @@ int hookNdsRetailArm7(
if (ndmaDisabled) {
ce7->valueBits |= b_ndmaDisabled;
}
if (strncmp(romTid, "HND", 3) == 0) {
ce7->valueBits |= b_isDlp;
}
if (REG_SCFG_EXT == 0) {
ce7->valueBits |= b_scfgLocked;
}
Expand Down
4 changes: 4 additions & 0 deletions retail/bootloaderi/source/arm7/hook_arm9.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#define b_asyncCardRead BIT(12)
#define b_softResetMb BIT(13)
#define b_cloneboot BIT(14)
#define b_isDlp BIT(15)


static const int MAX_HANDLER_LEN = 50;
Expand Down Expand Up @@ -329,6 +330,9 @@ int hookNdsRetailArm9(
if (usesCloneboot) {
ce9->valueBits |= b_cloneboot;
}
if (strncmp(romTid, "HND", 3) == 0) {
ce9->valueBits |= b_isDlp;
}
ce9->mainScreen = mainScreen;
ce9->overlaysSize = overlaysSize;
ce9->romPaddingSize = romPaddingSize;
Expand Down
6 changes: 6 additions & 0 deletions retail/bootloaderi/source/arm7/main.arm7.c
Original file line number Diff line number Diff line change
Expand Up @@ -1588,6 +1588,9 @@ int arm7_main(void) {
extern u32 clusterCache;

u32 add = (moduleParams->sdk_version >= 0x2008000 || moduleParams->sdk_version == 0x20029A8) ? 0xC8000 : 0xE8000; // 0x027C8000 : 0x027E8000
if (memcmp(romTid, "HND", 3) == 0) {
add = 0x108000; // 0x02808000
}
tonccpy((char*)0x02700000+add, (char*)0x02700000, 0x10000); // Move FAT table cache elsewhere
romFile->fatTableCache = (u32*)((u32)romFile->fatTableCache+add);
savFile->fatTableCache = (u32*)((u32)savFile->fatTableCache+add);
Expand Down Expand Up @@ -2011,6 +2014,9 @@ int arm7_main(void) {
} else {
const bool laterSdk = (moduleParams->sdk_version >= 0x2008000 || moduleParams->sdk_version == 0x20029A8);
ce9Location = dsiWramAccess ? CARDENGINEI_ARM9_LOCATION_DSI_WRAM : (!laterSdk ? CARDENGINEI_ARM9_LOCATION2 : CARDENGINEI_ARM9_LOCATION);
if (memcmp(romTid, "HND", 3) == 0) {
ce9Location = CARDENGINEI_ARM9_LOCATION_DLP;
}
ce9size = 0x5000;
tonccpy((u32*)ce9Location, (u32*)((!dsiWramAccess && !laterSdk) ? CARDENGINEI_ARM9_BUFFERED_LOCATION2 : CARDENGINEI_ARM9_BUFFERED_LOCATION), ce9size);
}
Expand Down
75 changes: 57 additions & 18 deletions retail/cardenginei/arm7/source/cardengine.c
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@
#define dsiBios BIT(18)
#define bootstrapOnFlashcard BIT(19)
#define ndmaDisabled BIT(20)
#define isDlp BIT(21)
#define scfgLocked BIT(31)

#define REG_EXTKEYINPUT (*(vuint16*)0x04000136)
Expand Down Expand Up @@ -367,6 +368,10 @@ static void initialize(void) {
sharedAddr = (vu32*)CARDENGINE_SHARED_ADDRESS_SDK5;
ndsHeader = (tNDSHeader*)NDS_HEADER_SDK5;
personalData = (PERSONAL_DATA*)((u8*)NDS_HEADER_SDK5-0x180);
} else {
sharedAddr = (vu32*)CARDENGINE_SHARED_ADDRESS_SDK1;
ndsHeader = (tNDSHeader*)NDS_HEADER;
personalData = (PERSONAL_DATA*)((u8*)NDS_HEADER-0x180);
}
#endif

Expand Down Expand Up @@ -451,9 +456,11 @@ static void cardReadRAM(u8* dst, u32 src, u32 len/*, int romPartNo*/) {
}

void reset(void) {
register int i, reg;

#ifndef TWLSDK
u32 resetParam = ((valueBits & isSdk5) ? RESET_PARAM_SDK5 : RESET_PARAM);
if ((valueBits & slowSoftReset) || (*(u32*)(resetParam+0xC) > 0 && (*(u32*)CARDENGINEI_ARM9_LOCATION == 0 || (valueBits & isSdk5)))) {
if (((valueBits & isDlp) && *(u32*)(NDS_HEADER_SDK5+0xC) == 0) || (valueBits & slowSoftReset) || (*(u32*)(resetParam+0xC) > 0 && (*(u32*)CARDENGINEI_ARM9_LOCATION == 0 || (valueBits & isSdk5)))) {
REG_MASTER_VOLUME = 0;
int oldIME = enterCriticalSection();
//driveInitialize();
Expand All @@ -480,10 +487,10 @@ void reset(void) {
leaveCriticalSection(oldIME);
while (1);
}

if (!(valueBits & isDlp)) {
#endif

register int i, reg;

REG_IME = 0;

for (i = 0; i < 16; i++) {
Expand Down Expand Up @@ -522,37 +529,52 @@ void reset(void) {
*(vu32*)0x0380FFF8 = 0; // VBLANK_INTR_WAIT_FLAGS, ARM7 version
REG_POWERCNT = 1; // Turn off power to stuff

initialized = false;
funcsUnpatched = false;

#ifndef TWLSDK
}
#endif

initialized = false;
//ipcSyncHooked = false;
languageTimer = 0;

#ifndef TWLSDK
if (currentSrlAddr != *(u32*)(resetParam+0xC) || *(u32*)(resetParam+8) == 0x44414F4C) {
if ((valueBits & isDlp) || currentSrlAddr != *(u32*)(resetParam+0xC) || *(u32*)(resetParam+8) == 0x44414F4C) {
currentSrlAddr = *(u32*)(resetParam+0xC);
if (*(u32*)(resetParam+8) == 0x44414F4C) {
if (valueBits & isDlp) {
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) {
cardReadRAM((u8*)ndsHeader, currentSrlAddr, 0x160);
cardReadRAM((u8*)ndsHeader->arm9destination, currentSrlAddr+ndsHeader->arm9romOffset, ndsHeader->arm9binarySize);
cardReadRAM((u8*)ndsHeader->arm7destination, currentSrlAddr+ndsHeader->arm7romOffset, ndsHeader->arm7binarySize);
*(u16*)0x02fffc40 = 2; // Boot Indicator (Cloneboot/Multiboot)
} else {
fileRead((char*)ndsHeader, romFile, currentSrlAddr, 0x160);
fileRead((char*)ndsHeader->arm9destination, romFile, currentSrlAddr+ndsHeader->arm9romOffset, ndsHeader->arm9binarySize);
fileRead((char*)ndsHeader->arm7destination, romFile, currentSrlAddr+ndsHeader->arm7romOffset, ndsHeader->arm7binarySize);
if (*(u32*)(resetParam+8) == 0x44414F4C) {
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) {
cardReadRAM((u8*)ndsHeader, currentSrlAddr, 0x160);
cardReadRAM((u8*)ndsHeader->arm9destination, currentSrlAddr+ndsHeader->arm9romOffset, ndsHeader->arm9binarySize);
cardReadRAM((u8*)ndsHeader->arm7destination, currentSrlAddr+ndsHeader->arm7romOffset, ndsHeader->arm7binarySize);
} else {
fileRead((char*)ndsHeader, romFile, currentSrlAddr, 0x160);
fileRead((char*)ndsHeader->arm9destination, romFile, currentSrlAddr+ndsHeader->arm9romOffset, ndsHeader->arm9binarySize);
fileRead((char*)ndsHeader->arm7destination, romFile, currentSrlAddr+ndsHeader->arm7romOffset, ndsHeader->arm7binarySize);
}
}

moduleParams = getModuleParams(ndsHeader);
/*dbg_printf("sdk_version: ");
dbg_hexa(moduleParams->sdk_version);
dbg_printf("\n");*/
if (moduleParams->sdk_version > 0x5000000) {
valueBits |= isSdk5;
} else {
valueBits &= ~isSdk5;
}

ensureBinaryDecompressed(ndsHeader, moduleParams, resetParam);
ensureBinaryDecompressed(ndsHeader, moduleParams, (valueBits & isDlp) ? 0x44414F4 : resetParam);

patchCardNdsArm9(
(cardengineArm9*)CARDENGINEI_ARM9_LOCATION,
(cardengineArm9*)((valueBits & isDlp) ? CARDENGINEI_ARM9_LOCATION_DLP : CARDENGINEI_ARM9_LOCATION),
ndsHeader,
moduleParams,
1
Expand Down Expand Up @@ -582,7 +604,15 @@ void reset(void) {
ndmaCopyWordsAsynch(1, ndsHeader->arm7destination, (char*)DONOR_ROM_ARM7_LOCATION, ndsHeader->arm7binarySize);
while (ndmaBusy(0) || ndmaBusy(1));
} */
*(u32*)(resetParam+8) = 0;
if (valueBits & isDlp) {
toncset((u32*)0x022C0000, 0, ndsHeader->arm7binarySize);
if (!(valueBits & isSdk5)) {
tonccpy((u8*)0x027FF000, (u8*)0x02FFF000, 0x1000);
}
} else {
*(u32*)(resetParam+8) = 0;
}
valueBits &= ~isDlp;
} else {
//driveInitialize();

Expand Down Expand Up @@ -1574,6 +1604,15 @@ void myIrqHandlerVBlank(void) {
i2cWriteRegister(0x4A, 0x11, 0x01); // Reboot into error screen if SD card is removed
}

#ifndef TWLSDK
if (valueBits & isDlp) {
if (!(REG_EXTKEYINPUT & KEY_A) && *(u32*)(NDS_HEADER_SDK5+0xC) != 0 && !wifiIrq) {
IPC_SendSync(0x5);
reset();
}
}
#endif

if ((0 == (REG_KEYINPUT & igmHotkey) && 0 == (REG_EXTKEYINPUT & (((igmHotkey >> 10) & 3) | ((igmHotkey >> 6) & 0xC0))) && (valueBits & igmAccessible) && !wifiIrq) || returnToMenu || sharedAddr[5] == 0x4C4D4749 /* IGML */) {
#ifdef TWLSDK
igmText = (struct IgmText *)INGAME_MENU_LOCATION;
Expand Down
10 changes: 10 additions & 0 deletions retail/cardenginei/arm7/source/patcher/hook_arm9.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,12 @@
#include "nds_header.h"
#include "cardengine_header_arm9.h"

#define isSdk5_9 BIT(5)
#define isSdk5_7 BIT(13)
#define b_softResetMb BIT(13)
#define b_isDlp BIT(15)

extern u32 valueBits;
extern vu32* volatile sharedAddr;


Expand Down Expand Up @@ -122,11 +126,17 @@ int hookNdsRetailArm9(
extern u32 iUncompressedSize;
extern bool softResetMb;

if (valueBits & isSdk5_7) {
sharedAddr[1] |= isSdk5_9;
} else {
sharedAddr[1] &= ~isSdk5_9;
}
if (softResetMb) {
sharedAddr[1] |= b_softResetMb;
} else {
sharedAddr[1] &= ~b_softResetMb;
}
sharedAddr[1] &= ~b_isDlp;

u32* tableAddr = hookInterruptHandler((u32*)ndsHeader->arm9destination, iUncompressedSize);

Expand Down
4 changes: 2 additions & 2 deletions retail/cardenginei/arm7/source/patcher/patch_arm9.c
Original file line number Diff line number Diff line change
Expand Up @@ -479,7 +479,7 @@ static void patchMpu(const tNDSHeader* ndsHeader, const module_params_t* moduleP
}

static void patchMpu2(const tNDSHeader* ndsHeader, const module_params_t* moduleParams) {
if (moduleParams->sdk_version < 0x2008000 || moduleParams->sdk_version > 0x5000000) {
if (moduleParams->sdk_version > 0x5000000) {
return;
}

Expand Down Expand Up @@ -522,7 +522,7 @@ static void patchMpu2(const tNDSHeader* ndsHeader, const module_params_t* module
//Original code made loading slow, so new code is used
unpatchedFuncs->mpuDataOffset2 = mpuDataOffset;
unpatchedFuncs->mpuInitRegionOldData2 = *mpuDataOffset;
*mpuDataOffset = PAGE_128K | 0x027E0000 | 1;
*mpuDataOffset = 0;
//}

/*u32 mpuInitRegionNewData = PAGE_32M | 0x02000000 | 1;
Expand Down
11 changes: 8 additions & 3 deletions retail/cardenginei/arm9/source/misc.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
#define slowSoftReset BIT(10)
#define softResetMb BIT(13)
#define cloneboot BIT(14)
#define isDlp BIT(15)

#include "my_fat.h"

Expand Down Expand Up @@ -131,9 +132,9 @@ void enableIPC_SYNC(void) {
}

#ifndef TWLSDK
void initialize(void) {
static bool initialized = false;
static bool initialized = false;

void initialize(void) {
if (initialized) {
return;
}
Expand All @@ -150,6 +151,9 @@ void initialize(void) {
cacheAddressTable = (u32*)CACHE_ADDRESS_TABLE_LOCATION_TWLSDK;
#endif
}
} else {
sharedAddr = (vu32*)CARDENGINE_SHARED_ADDRESS_SDK1;
ndsHeader = (tNDSHeader*)NDS_HEADER;
}
#endif
initialized = true;
Expand Down Expand Up @@ -335,12 +339,13 @@ void reset(u32 param, u32 tid2) {
while (REG_VCOUNT == 191);
}

if (*(u32*)(RESET_PARAM+0xC) > 0) {
if ((ce9->valueBits & isDlp) || *(u32*)(RESET_PARAM+0xC) > 0) {
u32 newIrqTable = sharedAddr[2];
ce9->valueBits = sharedAddr[1];
ce9->irqTable = (u32*)newIrqTable;
ce9->cardStruct0 = sharedAddr[4];
sharedAddr[4] = 0;
initialized = false;
}
#endif

Expand Down
Loading

0 comments on commit c5ff18b

Please sign in to comment.