From b20c79eda4b68da7211b7e88abf392b7e7b6b295 Mon Sep 17 00:00:00 2001 From: TuxSH <1922548+TuxSH@users.noreply.github.com> Date: Fri, 14 Jul 2023 18:28:27 +0200 Subject: [PATCH] Revert some recent change wrt. remounting due to driver issues --- arm9/source/fatfs/diskio.c | 65 +++++++++++++++++++++++++++++++++----- arm9/source/fs.c | 40 +++++++++++++++++++++-- 2 files changed, 95 insertions(+), 10 deletions(-) diff --git a/arm9/source/fatfs/diskio.c b/arm9/source/fatfs/diskio.c index 8c0be863..98a19f63 100644 --- a/arm9/source/fatfs/diskio.c +++ b/arm9/source/fatfs/diskio.c @@ -39,12 +39,28 @@ DSTATUS disk_initialize ( BYTE pdrv /* Physical drive nmuber to identify the drive */ ) { - static u32 sdmmcInitResult = 4; + static u32 sdmmcInitResult = 4; + DSTATUS res = 0; - if(sdmmcInitResult == 4) sdmmcInitResult = sdmmc_sdcard_init(); + if(sdmmcInitResult == 4) + sdmmcInitResult = sdmmc_sdcard_init(); - return ((pdrv == SDCARD && !(sdmmcInitResult & 2)) || - (pdrv == CTRNAND && !(sdmmcInitResult & 1) && !ctrNandInit())) ? 0 : STA_NOINIT; + // Check physical drive initialized status + switch (pdrv) + { + case SDCARD: + res = (sdmmcInitResult & 2) == 0 ? 0 : STA_NOINIT; + break; + case CTRNAND: + // Always update CTRNAND parameters when remounting + res = (sdmmcInitResult & 1) == 0 && ctrNandInit() == 0 ? 0 : STA_NOINIT; + break; + default: + res = STA_NODISK; + break; + } + + return res; } @@ -60,8 +76,22 @@ DRESULT disk_read ( UINT count /* Number of sectors to read */ ) { - return ((pdrv == SDCARD && !sdmmc_sdcard_readsectors(sector, count, buff)) || - (pdrv == CTRNAND && !ctrNandRead(sector, count, buff))) ? RES_OK : RES_PARERR; + DRESULT res = RES_OK; + + switch (pdrv) + { + case SDCARD: + res = sdmmc_sdcard_readsectors(sector, count, buff) == 0 ? RES_OK : RES_PARERR; + break; + case CTRNAND: + res = ctrNandRead(sector, count, buff) == 0 ? RES_OK : RES_PARERR; + break; + default: + res = RES_NOTRDY; + break; + } + + return res; } @@ -79,8 +109,27 @@ DRESULT disk_write ( UINT count /* Number of sectors to write */ ) { - return ((pdrv == SDCARD && (*(vu16 *)(SDMMC_BASE + REG_SDSTATUS0) & TMIO_STAT0_WRPROTECT) != 0 && !sdmmc_sdcard_writesectors(sector, count, buff)) || - (pdrv == CTRNAND && !ctrNandWrite(sector, count, buff))) ? RES_OK : RES_PARERR; + DRESULT res = RES_OK; + + switch (pdrv) + { + case SDCARD: + { + if ((*(vu16 *)(SDMMC_BASE + REG_SDSTATUS0) & TMIO_STAT0_WRPROTECT) == 0) // why == 0? + res = RES_WRPRT; + else + res = sdmmc_sdcard_writesectors(sector, count, buff) == 0 ? RES_OK : RES_PARERR; + break; + } + case CTRNAND: + res = ctrNandWrite(sector, count, buff) == 0 ? RES_OK : RES_PARERR; + break; + default: + res = RES_NOTRDY; + break; + } + + return res; } #endif diff --git a/arm9/source/fs.c b/arm9/source/fs.c index 0a3dd1f0..06e339ed 100644 --- a/arm9/source/fs.c +++ b/arm9/source/fs.c @@ -72,16 +72,34 @@ bool mountSdCardPartition(bool switchMainDir) bool remountCtrNandPartition(bool switchMainDir) { static bool nandInitialized = false; + int res = FR_OK; + +#if 0 + Unfortunately the sdmmc driver is really flaky and returns TMIO_STAT_CMD_RESPEND as error. + (after timing out) + TODO: fix all this tech debt... one day, maybe? if (nandInitialized) { - if (f_unmount("nand:") != FR_OK) + res = f_unmount("nand:"); + if (res != FR_OK) + { + error("f_unmount returned %d", res); return false; + } nandInitialized = false; } +#endif if (!nandInitialized) - nandInitialized = f_mount(&nandFs, "nand:", 1) == FR_OK; + { + res = f_mount(&nandFs, "nand:", 1); + nandInitialized = res == FR_OK; + if (res != FR_OK) + { + error("f_mount returned %d", res); + } + } if (nandInitialized && switchMainDir) return f_chdrive("nand:") == FR_OK && switchToMainDir(false); @@ -475,16 +493,27 @@ bool doLumaUpgradeProcess(void) FirmwareSource oldCtrNandLocation = ctrNandLocation; bool ok = true, ok2 = true, ok3 = true; +#if 0 + Unfortunately the sdmmc driver is really flaky and returns TMIO_STAT_CMD_RESPEND as error. + (after timing out) + TODO: fix all this tech debt... one day, maybe? + // Ensure SysNAND CTRNAND is mounted if (isSdMode) { ctrNandLocation = FIRMWARE_SYSNAND; if (!remountCtrNandPartition(false)) { + error("failed to mount"); ctrNandLocation = oldCtrNandLocation; return false; } } +#else + (void)oldCtrNandLocation; + // Ensure CTRNAND is mounted + remountCtrNandPartition(false); +#endif // Try to boot.firm to CTRNAND, when applicable if (isSdMode && memcmp(launchedPathForFatfs, "sdmc:", 5) == 0) @@ -497,10 +526,17 @@ bool doLumaUpgradeProcess(void) fileDelete("sdmc:/luma/config.bin"); fileDelete("nand:/rw/luma/config.bin"); +#if 0 if (isSdMode) { ctrNandLocation = oldCtrNandLocation; ok3 = remountCtrNandPartition(false); + if (!ok3) + error("failed to unmount"); } +#else + (void)ok3; +#endif + return ok && ok2 && ok3; }