Skip to content

Commit

Permalink
Check for SCFG SD/MMC access for DSiWare SD->flashcard redirection
Browse files Browse the repository at this point in the history
  • Loading branch information
RocketRobz committed Dec 4, 2024
1 parent 00465a3 commit 09008fc
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 14 deletions.
4 changes: 4 additions & 0 deletions retail/arm7/source/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,10 @@ int main(void) {

*(u32*)0x02FFFDF0 = REG_SCFG_EXT;

*(vu32*)0x4004820 = 0x8B7F0305;
*(u8*)0x02FFFDF4 = (*(vu32*)0x4004820 != 0) ? 1 : 0; // SD/MMC access is enabled in SCFG
*(vu32*)0x4004820 = 0;

// read User Settings from firmware
readUserSettings();
irqInit();
Expand Down
9 changes: 5 additions & 4 deletions retail/arm9/source/conf_sd.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -746,7 +746,8 @@ int loadFromSD(configuration* conf, const char *bootstrapPath) {
fatMountSimple("fat", dldiGetInternal());

conf->sdFound = (access("sd:/", F_OK) == 0);
bool flashcardFound = (access("fat:/", F_OK) == 0);
const bool flashcardFound = (access("fat:/", F_OK) == 0);
const bool scfgSdmmcEnabled = (*(u8*)0x02FFFDF4 == 1);

if (!conf->sdFound && !flashcardFound) {
consoleDemoInit();
Expand Down Expand Up @@ -1432,7 +1433,7 @@ int loadFromSD(configuration* conf, const char *bootstrapPath) {
fclose(cebin);
}

if (isDSiMode() && unitCode > 0 && conf->sdFound) {
if (isDSiMode() && unitCode > 0 && scfgSdmmcEnabled) {
const bool sdNandFound = conf->sdNand && (access(conf->gameOnFlashcard ? "fat:/shared1" : "sd:/shared1", F_OK) == 0);
const bool sdPhotoFound = conf->sdNand && (access(conf->gameOnFlashcard ? "fat:/photo" : "sd:/photo", F_OK) == 0);

Expand Down Expand Up @@ -1512,7 +1513,7 @@ int loadFromSD(configuration* conf, const char *bootstrapPath) {
conf->valueBits2 |= BIT(6);
}

if (!conf->isDSiWare || !conf->sdFound) {
if (!conf->isDSiWare || !scfgSdmmcEnabled) {
// Load external cheat engine binary
loadCardEngineBinary("nitro:/cardenginei_arm7_cheat.bin", (u8*)CHEAT_ENGINE_BUFFERED_LOCATION);

Expand Down Expand Up @@ -2469,7 +2470,7 @@ int loadFromSD(configuration* conf, const char *bootstrapPath) {
// Create AP-fixed overlay binary
createApFixOverlayBin(conf);

if ((!dsiFeatures() || conf->b4dsMode) || !conf->sdFound || !conf->isDSiWare) {
if ((!dsiFeatures() || conf->b4dsMode) || !scfgSdmmcEnabled || !conf->isDSiWare) {
// Update modified date
FILE *savFile = fopen(conf->savPath, "r+");
if (savFile) {
Expand Down
18 changes: 11 additions & 7 deletions retail/bootloaderi/source/arm7/main.arm7.c
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ extern u8 dmaRomRead_LED;
extern u8 soundFreq;

extern u8 _io_dldi_size;
bool scfgSdmmcEnabled = false;

bool useTwlCfg = false;
u8 twlCfgCountry = 0;
Expand Down Expand Up @@ -313,6 +314,9 @@ static void resetMemory_ARM7(void) {
*(vu32*)0x0380FFF8 = 0; // VBLANK_INTR_WAIT_FLAGS, ARM7 version
REG_POWERCNT = 1; // Turn off power to stuff

scfgSdmmcEnabled = (*(u8*)0x02FFFDF4 == 1);
*(u8*)0x02FFFDF4 = 0;

useTwlCfg = ((*(u8*)0x02000400 != 0) && (*(u8*)0x02000401 == 0) && (*(u8*)0x02000402 == 0) && (*(u8*)0x02000404 == 0) && (*(u8*)0x02000448 != 0));
twlCfgCountry = *(u8*)0x02000405;
twlCfgLang = *(u8*)0x02000406;
Expand Down Expand Up @@ -1211,7 +1215,7 @@ static void startBinary_ARM7(void) {

static void setMemoryAddress(const tNDSHeader* ndsHeader, const module_params_t* moduleParams) {
if (ROMsupportsDsiMode(ndsHeader) && dsiModeConfirmed) {
if (isDSiWare && dsiSD && !(REG_SCFG_ROM & BIT(9))) {
if (isDSiWare && scfgSdmmcEnabled && !(REG_SCFG_ROM & BIT(9))) {
u32* deviceListAddr = (u32*)(*(u32*)0x02FFE1D4);

dbg_printf("Device list address: ");
Expand Down Expand Up @@ -1382,7 +1386,7 @@ static void setMemoryAddress(const tNDSHeader* ndsHeader, const module_params_t*
toncset((u32*)0x02FFFD60, 0, 0xA0);
}

if (isDSiWare && dsiSD && !(REG_SCFG_ROM & BIT(9))) {
if (isDSiWare && scfgSdmmcEnabled && !(REG_SCFG_ROM & BIT(9))) {
*(u16*)(0x02FFFC40) = 3; // Boot Indicator (NAND/SD)
return;
}
Expand Down Expand Up @@ -1571,7 +1575,7 @@ int arm7_main(void) {
dsiModeConfirmed = dsiMode && ROMsupportsDsiMode(&dsiHeaderTemp.ndshdr);
}
const char* romTid = getRomTid(&dsiHeaderTemp.ndshdr);
if (!isDSiWare || !dsiSD || (REG_SCFG_ROM & BIT(9))) {
if (!isDSiWare || !scfgSdmmcEnabled || (REG_SCFG_ROM & BIT(9))) {
extern u32 clusterCacheSize;
clusterCacheSize = 0x10000;
if (dsiModeConfirmed && ROMsupportsDsiMode(&dsiHeaderTemp.ndshdr)) {
Expand All @@ -1596,7 +1600,7 @@ int arm7_main(void) {
&& patchOffsetCache.type == 0) { // 0 = Regular, 1 = B4DS, 2 = HB
fileRead((char*)&patchOffsetCache, &patchOffsetCacheFile, 0, sizeof(patchOffsetCacheContents));
} else {
if (srlAddr == 0 && (!isDSiWare || !dsiSD || (REG_SCFG_ROM & BIT(9)))) pleaseWaitOutput();
if (srlAddr == 0 && (!isDSiWare || !scfgSdmmcEnabled || (REG_SCFG_ROM & BIT(9)))) pleaseWaitOutput();
patchOffsetCache.ver = patchOffsetCacheFileVersion;
patchOffsetCache.type = 0;
}
Expand Down Expand Up @@ -1642,7 +1646,7 @@ int arm7_main(void) {
*(u32*)0x02FFE1A0 = *(u32*)DONOR_ROM_MBK6_LOCATION;
*(u32*)0x02FFE1D4 = *(u32*)DONOR_ROM_DEVICE_LIST_LOCATION;
}
if (!isDSiWare || !dsiSD || (REG_SCFG_ROM & BIT(9))) {
if (!isDSiWare || !scfgSdmmcEnabled || (REG_SCFG_ROM & BIT(9))) {
/* if (consoleModel > 0) {
tonccpy((char*)0x0DF80000, (char*)0x02700000, 0x80000); // Move FAT table cache to debug RAM
romFile->fatTableCache = (u32*)((u32)romFile->fatTableCache+0xB880000);
Expand Down Expand Up @@ -1732,7 +1736,7 @@ int arm7_main(void) {
tonccpy(cheatEngineBuffer, (char*)CHEAT_ENGINE_BUFFERED_LOCATION, 0x400);
toncset((char*)CHEAT_ENGINE_BUFFERED_LOCATION, 0, 0x400);

if (isDSiWare && dsiSD && !(REG_SCFG_ROM & BIT(9))) {
if (isDSiWare && scfgSdmmcEnabled && !(REG_SCFG_ROM & BIT(9))) {
extern void patchSharedFontPath(const cardengineArm9* ce9, const tNDSHeader* ndsHeader, const module_params_t* moduleParams, const ltd_module_params_t* ltdModuleParams);

const bool twlTouch = (cdcReadReg(CDC_SOUND, 0x22) == 0xF0 || i2cBricked);
Expand Down Expand Up @@ -2379,7 +2383,7 @@ int arm7_main(void) {
//fileWrite((char*)dsiHeaderTemp.arm9idestination, &ramDumpFile, 0, dsiHeaderTemp.arm9ibinarySize); // Dump (decrypted?) arm9 binary
}

if (ROMsupportsDsiMode(ndsHeader) && isDSiWare && dsiSD && !(REG_SCFG_ROM & BIT(9))) {
if (ROMsupportsDsiMode(ndsHeader) && isDSiWare && scfgSdmmcEnabled && !(REG_SCFG_ROM & BIT(9))) {
*(vu32*)0x400481C = 0; // Reset SD IRQ stat register
*(vu32*)0x4004820 = 0x8B7F0305; // Set SD IRQ mask register (Data won't read without the correct bytes!)
} /*else if (!isDSiWare) {
Expand Down
7 changes: 4 additions & 3 deletions retail/bootloaderi/source/arm7/patch_common.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ u16 patchOffsetCacheFileNewCrc = 0;
patchOffsetCacheContents patchOffsetCache;

extern bool gbaRomFound;
extern bool scfgSdmmcEnabled;
extern u8 dsiSD;
extern bool i2cBricked;
extern int sharedFontRegion;
Expand All @@ -54,8 +55,8 @@ void dsiWarePatch(cardengineArm9* ce9, const tNDSHeader* ndsHeader) {
const char* dataPub = "dataPub:";
const char* dataPrv = "dataPrv:";

const bool sdmmcMode = (dsiSD && !(REG_SCFG_ROM & BIT(9)));
const bool saveOnFlashcardNtr = (saveOnFlashcard && (!dsiSD || (REG_SCFG_ROM & BIT(9))));
const bool sdmmcMode = (scfgSdmmcEnabled && !(REG_SCFG_ROM & BIT(9)));
const bool saveOnFlashcardNtr = (saveOnFlashcard && (!scfgSdmmcEnabled || (REG_SCFG_ROM & BIT(9))));

const u32* dsiSaveCheckExists = ce9->patches->dsiSaveCheckExists;
const u32* dsiSaveGetResultCode = ce9->patches->dsiSaveGetResultCode;
Expand Down Expand Up @@ -15775,7 +15776,7 @@ void patchBinary(cardengineArm9* ce9, const tNDSHeader* ndsHeader, module_params

const char* romTid = getRomTid(ndsHeader);

const bool saveOnFlashcardNtr = (saveOnFlashcard && (!dsiSD || (REG_SCFG_ROM & BIT(9))));
const bool saveOnFlashcardNtr = (saveOnFlashcard && (!scfgSdmmcEnabled || (REG_SCFG_ROM & BIT(9))));

const u32* dsiSaveGetResultCode = ce9->patches->dsiSaveGetResultCode;
const u32* dsiSaveCreate = ce9->patches->dsiSaveCreate;
Expand Down

0 comments on commit 09008fc

Please sign in to comment.