Skip to content

Commit

Permalink
Create pack for DSiWare save patches for B4DS mode
Browse files Browse the repository at this point in the history
Only *1st Class Poker & BlackJack* is included for now
  • Loading branch information
RocketRobz committed Jan 10, 2025
1 parent 5613693 commit c910c26
Show file tree
Hide file tree
Showing 18 changed files with 360 additions and 17 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ retail/nitrofiles/cardenginei_arm7*.bin
retail/nitrofiles/cardenginei_arm9*.bin
retail/nitrofiles/preLoadSettings*.pck
retail/nitrofiles/apfix.pck
retail/nitrofiles/dsi2dsSavePatches.pck
retail/nitrofiles_dec/cardengine_arm7*.bin
retail/nitrofiles_dec/cardengine_arm9*.bin
retail/nitrofiles_dec/cardenginei_arm7*.bin
Expand Down
9 changes: 7 additions & 2 deletions retail/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ endif
export GAME_ICON := $(CURDIR)/$(ASSETS)/icon.bmp

#.PHONY: cardengine_arm7 cardengine_arm9 bootloader BootStrap clean
.PHONY: all dist release nightly bootloader bootloader1 bootloader2 bootloaderi bootloaderi1 bootloaderi2 cardengine_arm7 cardengine_arm7_music cardengine_arm9_igm cardengine_arm9_igm_extmem cardengine_arm9 cardengine_arm9_32 cardengine_arm9_start cardengine_arm9_start_foto cardengine_arm9_alt cardengine_arm9_alt_gsdd cardengine_arm9_alt2 cardengine_arm9_alt3 cardengine_arm9_extmem cardengine_arm9_extmem_gsdd cardengine_arm9_extmem_start cardengine_arm9_extmem_foto 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_gsdd 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 preLoadSettings apfix arm7/$(TARGET).elf arm9/$(TARGET).elf clean
.PHONY: all dist release nightly bootloader bootloader1 bootloader2 bootloaderi bootloaderi1 bootloaderi2 cardengine_arm7 cardengine_arm7_music cardengine_arm9_igm cardengine_arm9_igm_extmem cardengine_arm9 cardengine_arm9_32 cardengine_arm9_start cardengine_arm9_start_foto cardengine_arm9_alt cardengine_arm9_alt_gsdd cardengine_arm9_alt2 cardengine_arm9_alt3 cardengine_arm9_extmem cardengine_arm9_extmem_gsdd cardengine_arm9_extmem_start cardengine_arm9_extmem_foto 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_gsdd 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 preLoadSettings apfix dsi2dsSavePatches arm7/$(TARGET).elf arm9/$(TARGET).elf clean

all: $(OUTPUT)

Expand Down Expand Up @@ -101,7 +101,7 @@ arm7/$(TARGET).elf:
@$(MAKE) -C arm7

#---------------------------------------------------------------------------------
arm9/$(TARGET).elf: bootloader bootloader1 bootloader2 bootloaderi bootloaderi1 bootloaderi2 preLoadSettings apfix
arm9/$(TARGET).elf: bootloader bootloader1 bootloader2 bootloaderi bootloaderi1 bootloaderi2 preLoadSettings apfix dsi2dsSavePatches
@$(MAKE) -C arm9

#---------------------------------------------------------------------------------
Expand Down Expand Up @@ -272,6 +272,10 @@ preLoadSettings:
apfix:
@$(MAKE) -C apfix

#---------------------------------------------------------------------------------
dsi2dsSavePatches:
@$(MAKE) -C dsi2dsSavePatches

#---------------------------------------------------------------------------------
#$(BUILD):
# @[ -d $@ ] || mkdir -p $@
Expand Down Expand Up @@ -321,6 +325,7 @@ clean:
@rm -f $(NITRODATA)/preLoadSettingsDSi.pck
@rm -f $(NITRODATA)/preLoadSettings3DS.pck
@rm -f $(NITRODATA)/apfix.pck
@rm -f $(NITRODATA)/dsi2dsSavePatches.pck
@$(MAKE) -C bootloader clean
@$(MAKE) -C bootloader1 clean
@$(MAKE) -C bootloader2 clean
Expand Down
3 changes: 3 additions & 0 deletions retail/arm9/include/configuration.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ typedef struct configuration {
// char* gbaPath;
// char* gbaSavPath;
char* apPatchPath;
char* dsi2dsSavePatchPath;
u32 donorFileSize;
u32 donorFileOffset;
u32 romSize;
Expand All @@ -35,6 +36,8 @@ typedef struct configuration {
u32 wideCheatSize;
u32 apPatchOffset;
u32 apPatchSize;
u32 dsi2dsSavePatchOffset;
u32 dsi2dsSavePatchSize;
u32 cheatSize;
u32 musicsSize;
u32 dataToPreloadAddr[4];
Expand Down
2 changes: 1 addition & 1 deletion retail/arm9/include/nds_loader_arm9.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ extern "C"

//#define LOAD_DEFAULT_NDS 0

int runNds(u32 cluster, u32 saveCluster, u32 donorTwlCluster, /* u32 gbaCluster, u32 gbaSavCluster, */ u32 wideCheatCluster, u32 apPatchCluster, u32 cheatCluster, u32 patchOffsetCacheCluster, u32 ramDumpCluster, u32 srParamsCluster, u32 screenshotCluster, u32 apFixOverlaysCluster, u32 musicCluster, u32 pageFileCluster, u32 manualCluster, u32 sharedFontCluster, configuration* conf);
int runNds(u32 cluster, u32 saveCluster, u32 donorTwlCluster, /* u32 gbaCluster, u32 gbaSavCluster, */ u32 wideCheatCluster, u32 apPatchCluster, u32 dsi2dsSavePatchCluster, u32 cheatCluster, u32 patchOffsetCacheCluster, u32 ramDumpCluster, u32 srParamsCluster, u32 screenshotCluster, u32 apFixOverlaysCluster, u32 musicCluster, u32 pageFileCluster, u32 manualCluster, u32 sharedFontCluster, configuration* conf);

#ifdef __cplusplus
}
Expand Down
70 changes: 70 additions & 0 deletions retail/arm9/source/conf_sd.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,69 @@ static void loadApFix(configuration* conf, const char* bootstrapPath, const char
fclose(file);
}

static void loadDSi2DSSavePatch(configuration* conf, const char* bootstrapPath, const char* romTid, const u8 romVersion, const u16 headerCRC) {
FILE *file = fopen("nitro:/dsi2dsSavePatches.pck", "rb");
if (!file) {
return;
}

char buf[5] = {0};
fread(buf, 1, 4, file);
if (strcmp(buf, ".PCK") != 0) {
return;
}

u32 fileCount;
fread(&fileCount, 1, sizeof(fileCount), file);

u32 offset = 0, size = 0;

// Try binary search for the game
int left = 0;
int right = fileCount;

while (left <= right) {
int mid = left + ((right - left) / 2);
fseek(file, 16 + mid * 16, SEEK_SET);
fread(buf, 1, 4, file);
int cmp = strcmp(buf, romTid);
if (cmp == 0) { // TID matches, check other info
u16 crc;
u8 ver;
fread(&crc, 1, sizeof(crc), file);
fread(&offset, 1, sizeof(offset), file);
fread(&size, 1, sizeof(size), file);
fread(&ver, 1, sizeof(ver), file);

if ((crc == 0 || crc == headerCRC) && (ver == 0xFF || ver == romVersion)) {
break;
} else if (crc < headerCRC) {
offset = 0;
size = 0;

left = mid + 1;
} else {
offset = 0;
size = 0;

right = mid - 1;
}
} else if (cmp < 0) {
left = mid + 1;
} else {
right = mid - 1;
}
}

if (offset > 0) {
sprintf(conf->dsi2dsSavePatchPath, bootstrapPath);
conf->dsi2dsSavePatchOffset = offsetOfOpenedNitroFile+offset;
conf->dsi2dsSavePatchSize = size;
}

fclose(file);
}

static int loadCardEngineBinary(const char* cardenginePath, u8* location) {
FILE* cebin = fopen(cardenginePath, "rb");
int size;
Expand Down Expand Up @@ -950,6 +1013,7 @@ int loadFromSD(configuration* conf, const char *bootstrapPath) {

char romTid[5] = {0};
u8 unitCode = 0;
u8 romVersion = 0;
u32 ndsArm9BinOffset = 0;
u32 ndsArm9Offset = 0;
u32 ndsArm7BinOffset = 0;
Expand Down Expand Up @@ -977,6 +1041,8 @@ int loadFromSD(configuration* conf, const char *bootstrapPath) {
fread(&romTid, 1, 4, ndsFile);
fseek(ndsFile, 0x12, SEEK_SET);
fread(&unitCode, 1, 1, ndsFile);
fseek(ndsFile, 0x1E, SEEK_SET);
fread(&romVersion, 1, 1, ndsFile);
fseek(ndsFile, 0x20, SEEK_SET);
fread(&ndsArm9BinOffset, sizeof(u32), 1, ndsFile);
fseek(ndsFile, 0x28, SEEK_SET);
Expand Down Expand Up @@ -2373,6 +2439,10 @@ int loadFromSD(configuration* conf, const char *bootstrapPath) {
}
}
}

if (!isDSiMode() || !scfgSdmmcEnabled) {
loadDSi2DSSavePatch(conf, bootstrapPath, romTid, romVersion, headerCRC);
}
} else {
loadApFix(conf, bootstrapPath, romTid, headerCRC);
}
Expand Down
8 changes: 7 additions & 1 deletion retail/arm9/source/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -409,6 +409,7 @@ static int runNdsFile(configuration* conf) {
// struct stat stGbaSav;
struct stat stWideCheat;
struct stat stApPatch;
struct stat stDSi2DSSavePatch;
struct stat stCheat;
struct stat stPatchOffsetCache;
struct stat stRamDump;
Expand All @@ -425,6 +426,7 @@ static int runNdsFile(configuration* conf) {
// u32 clusterGbaSav = 0;
u32 clusterWideCheat = 0;
u32 clusterApPatch = 0;
u32 clusterDSi2DSSave = 0;
u32 clusterCheat = 0;
u32 clusterPatchOffsetCache = 0;
u32 clusterRamDump = 0;
Expand Down Expand Up @@ -478,6 +480,10 @@ static int runNdsFile(configuration* conf) {
clusterApPatch = stApPatch.st_ino;
}

if (stat(conf->dsi2dsSavePatchPath, &stDSi2DSSavePatch) >= 0) {
clusterDSi2DSSave = stDSi2DSSavePatch.st_ino;
}

if (stat(patchOffsetCacheFilePath, &stPatchOffsetCache) >= 0) {
clusterPatchOffsetCache = stPatchOffsetCache.st_ino;
}
Expand Down Expand Up @@ -532,7 +538,7 @@ static int runNdsFile(configuration* conf) {
clusterTwlFont = stTwlFont.st_ino;
}

return runNds(st.st_ino, clusterSav, clusterDonor, /* clusterGba, clusterGbaSav, */ clusterWideCheat, clusterApPatch, clusterCheat, clusterPatchOffsetCache, clusterRamDump, clusterSrParams, clusterScreenshot, apFixOverlaysCluster, musicCluster, clusterPageFile, clusterManual, clusterTwlFont, conf);
return runNds(st.st_ino, clusterSav, clusterDonor, /* clusterGba, clusterGbaSav, */ clusterWideCheat, clusterApPatch, clusterDSi2DSSave, clusterCheat, clusterPatchOffsetCache, clusterRamDump, clusterSrParams, clusterScreenshot, apFixOverlaysCluster, musicCluster, clusterPageFile, clusterManual, clusterTwlFont, conf);
}

int main(int argc, char** argv) {
Expand Down
5 changes: 4 additions & 1 deletion retail/arm9/source/nds_loader_arm9.c
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ static bool dldiPatchLoader (data_t *binData, u32 binSize, bool clearBSS)
return true;
}

int runNds(u32 cluster, u32 saveCluster, u32 donorTwlCluster, /* u32 gbaCluster, u32 gbaSavCluster, */ u32 wideCheatCluster, u32 apPatchCluster, u32 cheatCluster, u32 patchOffsetCacheCluster, u32 ramDumpCluster, u32 srParamsCluster, u32 screenshotCluster, u32 apFixOverlaysCluster, u32 musicCluster, u32 pageFileCluster, u32 manualCluster, u32 sharedFontCluster, configuration* conf) {
int runNds(u32 cluster, u32 saveCluster, u32 donorTwlCluster, /* u32 gbaCluster, u32 gbaSavCluster, */ u32 wideCheatCluster, u32 apPatchCluster, u32 dsi2dsSavePatchCluster, u32 cheatCluster, u32 patchOffsetCacheCluster, u32 ramDumpCluster, u32 srParamsCluster, u32 screenshotCluster, u32 apFixOverlaysCluster, u32 musicCluster, u32 pageFileCluster, u32 manualCluster, u32 sharedFontCluster, configuration* conf) {
nocashMessage("runNds");

// Load bootloader binary
Expand Down Expand Up @@ -279,6 +279,9 @@ int runNds(u32 cluster, u32 saveCluster, u32 donorTwlCluster, /* u32 gbaCluster,
loader->apPatchFileCluster = apPatchCluster;
loader->apPatchOffset = conf->apPatchOffset;
loader->apPatchSize = conf->apPatchSize;
loader->dsi2dsSavePatchFileCluster = dsi2dsSavePatchCluster;
loader->dsi2dsSavePatchOffset = conf->dsi2dsSavePatchOffset;
loader->dsi2dsSavePatchSize = conf->dsi2dsSavePatchSize;
loader->cheatFileCluster = cheatCluster;
loader->cheatSize = conf->cheatSize;
loader->patchOffsetCacheFileCluster = patchOffsetCacheCluster;
Expand Down
8 changes: 4 additions & 4 deletions retail/bootloader/source/arm7/dsi2ds_patches.c
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ void patchDSiModeToDSMode(cardengineArm9* ce9, const tNDSHeader* ndsHeader) {

// 1st Class Poker & BlackJack (USA)
else if (strcmp(romTid, "KYPE") == 0) {
setBL(0x02012E50, (u32)dsiSaveOpen);
/* setBL(0x02012E50, (u32)dsiSaveOpen); // Part of .pck file
setBL(0x02012EC4, (u32)dsiSaveGetLength);
setBL(0x02012ED8, (u32)dsiSaveClose);
setBL(0x02012EF8, (u32)dsiSaveSeek);
Expand All @@ -281,13 +281,13 @@ void patchDSiModeToDSMode(cardengineArm9* ce9, const tNDSHeader* ndsHeader) {
setBL(0x02013184, (u32)dsiSaveSeek);
setBL(0x0201319C, (u32)dsiSaveWrite);
setBL(0x020131B0, (u32)dsiSaveClose);
setBL(0x020131FC, (u32)dsiSaveClose);
setBL(0x020131FC, (u32)dsiSaveClose); */
*(u32*)0x0204A92C = 0xE3A00001; // mov r0, #1 (Enable NitroFS reads)
}

// 1st Class Poker & BlackJack (Europe)
else if (strcmp(romTid, "KYPP") == 0) {
setBL(0x02012E40, (u32)dsiSaveOpen);
/* setBL(0x02012E40, (u32)dsiSaveOpen); // Part of .pck file
setBL(0x02012EB4, (u32)dsiSaveGetLength);
setBL(0x02012EC8, (u32)dsiSaveClose);
setBL(0x02012EE8, (u32)dsiSaveSeek);
Expand All @@ -304,7 +304,7 @@ void patchDSiModeToDSMode(cardengineArm9* ce9, const tNDSHeader* ndsHeader) {
setBL(0x02013174, (u32)dsiSaveSeek);
setBL(0x0201318C, (u32)dsiSaveWrite);
setBL(0x020131A0, (u32)dsiSaveClose);
setBL(0x020131EC, (u32)dsiSaveClose);
setBL(0x020131EC, (u32)dsiSaveClose); */
*(u32*)0x0204A920 = 0xE3A00001; // mov r0, #1 (Enable NitroFS reads)
}

Expand Down
10 changes: 9 additions & 1 deletion retail/bootloader/source/arm7/load_crt0.s
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,9 @@
.global apPatchFileCluster
.global apPatchOffset
.global apPatchSize
.global apPatchIsCheat
.global dsi2dsSavePatchFileCluster
.global dsi2dsSavePatchOffset
.global dsi2dsSavePatchSize
.global cheatFileCluster
.global cheatSize
.global patchOffsetCacheFileCluster
Expand Down Expand Up @@ -142,6 +144,12 @@ apPatchOffset:
.word 0x00000000
apPatchSize:
.word 0x00000000
dsi2dsSavePatchFileCluster:
.word 0x00000000
dsi2dsSavePatchOffset:
.word 0x00000000
dsi2dsSavePatchSize:
.word 0x00000000
cheatFileCluster:
.word 0x00000000
cheatSize:
Expand Down
79 changes: 79 additions & 0 deletions retail/bootloader/source/arm7/patch_arm9.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include "cardengine_header_arm9.h"
#include "unpatched_funcs.h"
#include "debug_file.h"
#include "my_fat.h"
#include "tonccpy.h"
#include "value_bits.h"

Expand Down Expand Up @@ -2034,6 +2035,82 @@ void patchTwlFontLoad(u32 heapAllocAddr, u32 newCodeAddr) {
setBL(heapAllocAddr, newCodeAddr);
}

void patchTwlSaveFuncs(const cardengineArm9* ce9) {
extern u32 dsi2dsSavePatchFileCluster;
extern u32 dsi2dsSavePatchOffset;
extern u32 dsi2dsSavePatchSize;
if (dsi2dsSavePatchFileCluster == CLUSTER_FREE || dsi2dsSavePatchOffset == 0 || dsi2dsSavePatchSize == 0) {
return;
}

const u32* dsiSaveGetResultCode = ce9->patches->dsiSaveGetResultCode;
const u32* dsiSaveCreate = ce9->patches->dsiSaveCreate;
const u32* dsiSaveDelete = ce9->patches->dsiSaveDelete;
const u32* dsiSaveGetInfo = ce9->patches->dsiSaveGetInfo;
const u32* dsiSaveSetLength = ce9->patches->dsiSaveSetLength;
const u32* dsiSaveOpen = ce9->patches->dsiSaveOpen;
const u32* dsiSaveOpenR = ce9->patches->dsiSaveOpenR;
const u32* dsiSaveClose = ce9->patches->dsiSaveClose;
const u32* dsiSaveGetLength = ce9->patches->dsiSaveGetLength;
const u32* dsiSaveGetPosition = ce9->patches->dsiSaveGetPosition;
const u32* dsiSaveSeek = ce9->patches->dsiSaveSeek;
const u32* dsiSaveRead = ce9->patches->dsiSaveRead;
const u32* dsiSaveWrite = ce9->patches->dsiSaveWrite;

aFile file;
getFileFromCluster(&file, dsi2dsSavePatchFileCluster);

for (u32 i = 0; i < dsi2dsSavePatchSize; i += 8) {
u32 patchData[2];
fileRead((char*)patchData, &file, dsi2dsSavePatchOffset+i, 8);

switch (patchData[1]) {
case 0x52544547: // 'GETR'
setBL(patchData[0], (u32)dsiSaveGetResultCode);
break;
case 0x41455243: // 'CREA'
setBL(patchData[0], (u32)dsiSaveCreate);
break;
case 0x454C4544: // 'DELE'
setBL(patchData[0], (u32)dsiSaveDelete);
break;
case 0x49544547: // 'GETI'
setBL(patchData[0], (u32)dsiSaveGetInfo);
break;
case 0x4C544553: // 'SETL'
setBL(patchData[0], (u32)dsiSaveSetLength);
break;
case 0x4E45504F: // 'OPEN'
setBL(patchData[0], (u32)dsiSaveOpen);
break;
case 0x5245504F: // 'OPER'
setBL(patchData[0], (u32)dsiSaveOpenR);
break;
case 0x534F4C43: // 'CLOS'
setBL(patchData[0], (u32)dsiSaveClose);
break;
case 0x4C544547: // 'GETL'
setBL(patchData[0], (u32)dsiSaveGetLength);
break;
case 0x50544547: // 'GETP'
setBL(patchData[0], (u32)dsiSaveGetPosition);
break;
case 0x4B454553: // 'SEEK'
setBL(patchData[0], (u32)dsiSaveSeek);
break;
case 0x44414552: // 'READ'
setBL(patchData[0], (u32)dsiSaveRead);
break;
case 0x54495257: // 'WRIT'
setBL(patchData[0], (u32)dsiSaveWrite);
break;
default:
*(u32*)(patchData[0]) = patchData[1];
break;
}
}
}

void codeCopy(u32* dst, u32* src, u32 len) {
tonccpy(dst, src, len);

Expand Down Expand Up @@ -2591,6 +2668,8 @@ void patchCardNdsArm9Cont(cardengineArm9* ce9, const tNDSHeader* ndsHeader, cons

patchSharedFontPath(ce9, ndsHeader, moduleParams);

patchTwlSaveFuncs(ce9);

// Further patching in order for DSiWare to boot with NTR ARM7 binary
extern u8 arm7newUnitCode;
extern u32 arm7mbk;
Expand Down
Loading

0 comments on commit c910c26

Please sign in to comment.