From d4a3ce2d0d9dbd771add4c1d65bfc1476b566c7e Mon Sep 17 00:00:00 2001 From: Aurora Date: Sat, 5 Mar 2016 23:27:02 +0100 Subject: [PATCH] Clean-up of the filesystem functions --- source/draw.c | 2 +- source/firm.c | 24 ++++++++--------- source/fs.c | 73 ++++++++++++++++++--------------------------------- source/fs.h | 3 +-- source/main.c | 6 ++--- 5 files changed, 42 insertions(+), 66 deletions(-) diff --git a/source/draw.c b/source/draw.c index dc31184..d37dcca 100644 --- a/source/draw.c +++ b/source/draw.c @@ -39,6 +39,6 @@ void loadSplash(void){ //Check if it's a no-screen-init A9LH boot via PDN_GPU_CNT if (*(u8*)0x10141200 == 0x1) return; clearScreen(); - if(fileRead(fb->top_left, "/rei/splash.bin", 0x46500) != 0) return; + if(!fileRead(fb->top_left, "/rei/splash.bin", 0x46500)) return; u64 i = 0xFFFFFF; while(--i) __asm("mov r0, r0"); //Less Ghetto sleep func } \ No newline at end of file diff --git a/source/firm.c b/source/firm.c index e5a7e12..abcb8c6 100755 --- a/source/firm.c +++ b/source/firm.c @@ -101,26 +101,26 @@ u8 loadFirm(void){ firmSize = console ? 0xF2000 : 0xE9000; nandFirm0((u8*)firmLocation, firmSize, console); //Check for correct decryption - if(memcmp((u8*)firmLocation, "FIRM", 4) != 0) return 1; + if(memcmp((u8*)firmLocation, "FIRM", 4) != 0) return 0; } //Load FIRM from SD else{ char *path = usePatchedFirm ? firmPathPatched : (mode ? "/rei/firmware.bin" : "/rei/firmware90.bin"); firmSize = fileSize(path); - if (!firmSize) return 1; + if(!firmSize) return 0; fileRead((u8*)firmLocation, path, firmSize); } section = firmLocation->section; //Check that the loaded FIRM matches the console - if((((u32)section[2].address >> 8) & 0xFF) != (console ? 0x60 : 0x68)) return 1; + if((((u32)section[2].address >> 8) & 0xFF) != (console ? 0x60 : 0x68)) return 0; if(console && !usePatchedFirm) decArm9Bin((u8*)firmLocation + section[2].offset, mode); - return 0; + return 1; } //Nand redirection @@ -137,7 +137,7 @@ u8 loadEmu(void){ //Read emunand code from SD char path[] = "/rei/emunand/emunand.bin"; u32 size = fileSize(path); - if (!size) return 1; + if(!size) return 0; if(!console || !mode) nandRedir[5] = 0xA4; //Find offset for emuNAND code from the offset in nandRedir u8 *emuCodeTmp = &nandRedir[4]; @@ -170,20 +170,20 @@ u8 loadEmu(void){ //Set MPU for emu code region memcpy((u8*)mpuOffset, mpu, sizeof(mpu)); - return 0; + return 1; } //Patches u8 patchFirm(void){ //Skip patching - if(usePatchedFirm) return 0; + if(usePatchedFirm) return 1; //Apply emuNAND patches if(emuNAND){ - if (loadEmu()) return 1; + if(loadEmu()) return 0; } - else if (a9lhSetup){ + else if(a9lhSetup){ //Patch FIRM partitions writes on SysNAND to protect A9LH u32 writeOffset = 0; getFIRMWrite(firmLocation, firmSize, &writeOffset); @@ -212,7 +212,7 @@ u8 patchFirm(void){ //Read reboot code from SD char path[] = "/rei/reboot/reboot.bin"; u32 size = fileSize(path); - if (!size) return 1; + if(!size) return 0; getReboot(firmLocation, firmSize, &rebootOffset); fileRead((u8*)rebootOffset, path, size); @@ -230,9 +230,9 @@ u8 patchFirm(void){ //Write patched FIRM to SD if needed if(firmPathPatched) - if(fileWrite((u8*)firmLocation, firmPathPatched, firmSize) != 0) return 1; + if(!fileWrite((u8*)firmLocation, firmPathPatched, firmSize)) return 0; - return 0; + return 1; } void launchFirm(void){ diff --git a/source/fs.c b/source/fs.c index c4f9edb..d7cbd3c 100644 --- a/source/fs.c +++ b/source/fs.c @@ -8,80 +8,57 @@ static FATFS fs; -int mountSD() -{ - if (f_mount(&fs, "0:", 1) != FR_OK) { - //printF("Failed to mount SD card!"); - return 1; - } - //printF("Mounted SD card"); - return 0; +int mountSD(void){ + if(f_mount(&fs, "0:", 1) != FR_OK) return 0; + return 1; } -int fileReadOffset(u8 *dest, const char *path, u32 size, u32 offset){ +int fileRead(u8 *dest, const char *path, u32 size){ FRESULT fr; FIL fp; unsigned int br = 0; fr = f_open(&fp, path, FA_READ); - if (fr != FR_OK)goto error; - - if (!size) size = f_size(&fp); - if (offset) { - fr = f_lseek(&fp, offset); - if (fr != FR_OK) goto error; + if(fr == FR_OK){ + if(!size) size = f_size(&fp); + fr = f_read(&fp, dest, size, &br); } - fr = f_read(&fp, dest, size, &br); - if (fr != FR_OK) goto error; - - fr = f_close(&fp); - if (fr != FR_OK) goto error; - return 0; - -error: f_close(&fp); - return fr; -} - -int fileRead(u8 *dest, const char *path, u32 size){ - return fileReadOffset(dest, path, size, 0); + return fr ? 0 : 1; } int fileWrite(const u8 *buffer, const char *path, u32 size){ - FRESULT fr = 1; + FRESULT fr; FIL fp; unsigned int br = 0; f_unlink(path); - if(f_open(&fp, path, FA_WRITE | FA_OPEN_ALWAYS) == FR_OK){ - fr = f_write(&fp, buffer, size, &br); - f_close(&fp); - if (fr == FR_OK && br == size) return 0; - } - return fr; + + fr = f_open(&fp, path, FA_WRITE | FA_OPEN_ALWAYS); + if(fr == FR_OK) fr = f_write(&fp, buffer, size, &br); + + f_close(&fp); + return fr ? 0 : 1; } int fileSize(const char* path){ - FRESULT fr; FIL fp; - int size = 0; + int size = 0; - fr = f_open(&fp, path, FA_READ); - if (fr != FR_OK)goto error; - - size = f_size(&fp); - error: - f_close(&fp); - return size; + if(f_open(&fp, path, FA_READ) == FR_OK) + size = f_size(&fp); + + f_close(&fp); + return size; } int fileExists(const char* path){ - FRESULT fr; FIL fp; - int exists = 1; - fr = f_open(&fp, path, FA_READ); - if (fr != FR_OK)exists = 0; + int exists = 0; + + if(f_open(&fp, path, FA_READ) == FR_OK) exists = 1; + f_close(&fp); return exists; } \ No newline at end of file diff --git a/source/fs.h b/source/fs.h index ba908ea..16beed7 100644 --- a/source/fs.h +++ b/source/fs.h @@ -7,8 +7,7 @@ #include "types.h" -int mountSD(); -int fileReadOffset(u8 *dest, const char *path, u32 size, u32 offset); +int mountSD(void); int fileRead(u8 *dest, const char *path, u32 size); int fileWrite(const u8 *buffer, const char *path, u32 size); int fileSize(const char* path); diff --git a/source/main.c b/source/main.c index d1bd5dd..532513f 100644 --- a/source/main.c +++ b/source/main.c @@ -14,8 +14,8 @@ u8 main(){ mountSD(); loadSplash(); setupCFW(); - if (loadFirm()) return 1; - if (patchFirm()) return 1; + if (!loadFirm()) return 0; + if (!patchFirm()) return 0; launchFirm(); - return 0; + return 1; } \ No newline at end of file