From bc1aa15dd709a703b9af1a0f89ccded21fc08823 Mon Sep 17 00:00:00 2001 From: Aurora Wright Date: Mon, 28 Aug 2017 14:46:18 +0200 Subject: [PATCH] Add more checks, make the emuNAND SD check only occur when emuNAND is being booted --- source/emunand.c | 12 +++--------- source/fs.c | 15 ++++++--------- source/main.c | 3 +++ 3 files changed, 12 insertions(+), 18 deletions(-) diff --git a/source/emunand.c b/source/emunand.c index 94ae654..df8c6b5 100644 --- a/source/emunand.c +++ b/source/emunand.c @@ -28,6 +28,7 @@ * Code for locating the SDMMC struct by Normmatt */ + #include "emunand.h" #include "memory.h" #include "utils.h" @@ -35,7 +36,7 @@ #include "../build/bundled.h" u32 emuOffset, - emuHeader = 0; + emuHeader; void locateEmuNand(FirmwareSource *nandType) { @@ -77,6 +78,7 @@ void locateEmuNand(FirmwareSource *nandType) { emuOffset = nandOffset + 1; emuHeader = nandOffset + 1; + return; } //Check for Gateway EmuNAND @@ -84,14 +86,6 @@ void locateEmuNand(FirmwareSource *nandType) { emuOffset = nandOffset; emuHeader = nandOffset + nandSize; - } - - if(emuHeader != 0) - { - //Make sure the SD card isn't write protected - if((*(vu16 *)(SDMMC_BASE + REG_SDSTATUS0) & TMIO_STAT0_WRPROTECT) == 0) - error("The SD card is locked, EmuNAND can not be used.\nPlease turn the write protection switch off."); - return; } } diff --git a/source/fs.c b/source/fs.c index 0de88e6..90c70ec 100644 --- a/source/fs.c +++ b/source/fs.c @@ -66,6 +66,7 @@ bool mountFs(bool isSd, bool switchToCtrNand) u32 fileRead(void *dest, const char *path, u32 maxSize) { FIL file; + FRESULT result = FR_OK; u32 ret = 0; if(f_open(&file, path, FA_READ) != FR_OK) return ret; @@ -73,10 +74,10 @@ u32 fileRead(void *dest, const char *path, u32 maxSize) u32 size = f_size(&file); if(dest == NULL) ret = size; else if(size <= maxSize) - f_read(&file, dest, size, (unsigned int *)&ret); - f_close(&file); + result = f_read(&file, dest, size, (unsigned int *)&ret); + result |= f_close(&file); - return ret; + return result == FR_OK ? ret : 0; } u32 getFileSize(const char *path) @@ -181,9 +182,7 @@ bool payloadMenu(char *path) payloadNum++; } - f_closedir(&dir); - - if(!payloadNum) return false; + if(f_closedir(&dir) != FR_OK || !payloadNum) return false; u32 pressed = 0, selectedPayload = 0; @@ -281,9 +280,7 @@ u32 firmRead(void *dest, u32 firmType) if(tempVersion < firmVersion) firmVersion = tempVersion; } - f_closedir(&dir); - - if(firmVersion == 0xFFFFFFFF) goto exit; + if(f_closedir(&dir) != FR_OK || firmVersion == 0xFFFFFFFF) goto exit; //Complete the string with the .app name sprintf(path, "%s/%08x.app", folderPath, firmVersion); diff --git a/source/main.c b/source/main.c index 5c1d258..16eedee 100644 --- a/source/main.c +++ b/source/main.c @@ -37,6 +37,7 @@ #include "crypto.h" #include "memory.h" #include "screen.h" +#include "fatfs/sdmmc/sdmmc.h" extern CfgData configData; extern ConfigurationStatus needConfig; @@ -306,6 +307,8 @@ boot: { locateEmuNand(&nandType); if(nandType == FIRMWARE_SYSNAND) firmSource = FIRMWARE_SYSNAND; + else if((*(vu16 *)(SDMMC_BASE + REG_SDSTATUS0) & TMIO_STAT0_WRPROTECT) == 0) //Make sure the SD card isn't write protected + error("The SD card is locked, EmuNAND can not be used.\nPlease turn the write protection switch off."); } //Same if we're using EmuNAND as the FIRM source