Clean-up of the filesystem functions

This commit is contained in:
Aurora 2016-03-05 23:27:02 +01:00
parent a58cb05f2c
commit d4a3ce2d0d
5 changed files with 42 additions and 66 deletions

View File

@ -39,6 +39,6 @@ void loadSplash(void){
//Check if it's a no-screen-init A9LH boot via PDN_GPU_CNT //Check if it's a no-screen-init A9LH boot via PDN_GPU_CNT
if (*(u8*)0x10141200 == 0x1) return; if (*(u8*)0x10141200 == 0x1) return;
clearScreen(); 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 u64 i = 0xFFFFFF; while(--i) __asm("mov r0, r0"); //Less Ghetto sleep func
} }

View File

@ -101,26 +101,26 @@ u8 loadFirm(void){
firmSize = console ? 0xF2000 : 0xE9000; firmSize = console ? 0xF2000 : 0xE9000;
nandFirm0((u8*)firmLocation, firmSize, console); nandFirm0((u8*)firmLocation, firmSize, console);
//Check for correct decryption //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 //Load FIRM from SD
else{ else{
char *path = usePatchedFirm ? firmPathPatched : char *path = usePatchedFirm ? firmPathPatched :
(mode ? "/rei/firmware.bin" : "/rei/firmware90.bin"); (mode ? "/rei/firmware.bin" : "/rei/firmware90.bin");
firmSize = fileSize(path); firmSize = fileSize(path);
if (!firmSize) return 1; if(!firmSize) return 0;
fileRead((u8*)firmLocation, path, firmSize); fileRead((u8*)firmLocation, path, firmSize);
} }
section = firmLocation->section; section = firmLocation->section;
//Check that the loaded FIRM matches the console //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) if(console && !usePatchedFirm)
decArm9Bin((u8*)firmLocation + section[2].offset, mode); decArm9Bin((u8*)firmLocation + section[2].offset, mode);
return 0; return 1;
} }
//Nand redirection //Nand redirection
@ -137,7 +137,7 @@ u8 loadEmu(void){
//Read emunand code from SD //Read emunand code from SD
char path[] = "/rei/emunand/emunand.bin"; char path[] = "/rei/emunand/emunand.bin";
u32 size = fileSize(path); u32 size = fileSize(path);
if (!size) return 1; if(!size) return 0;
if(!console || !mode) nandRedir[5] = 0xA4; if(!console || !mode) nandRedir[5] = 0xA4;
//Find offset for emuNAND code from the offset in nandRedir //Find offset for emuNAND code from the offset in nandRedir
u8 *emuCodeTmp = &nandRedir[4]; u8 *emuCodeTmp = &nandRedir[4];
@ -170,20 +170,20 @@ u8 loadEmu(void){
//Set MPU for emu code region //Set MPU for emu code region
memcpy((u8*)mpuOffset, mpu, sizeof(mpu)); memcpy((u8*)mpuOffset, mpu, sizeof(mpu));
return 0; return 1;
} }
//Patches //Patches
u8 patchFirm(void){ u8 patchFirm(void){
//Skip patching //Skip patching
if(usePatchedFirm) return 0; if(usePatchedFirm) return 1;
//Apply emuNAND patches //Apply emuNAND patches
if(emuNAND){ 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 //Patch FIRM partitions writes on SysNAND to protect A9LH
u32 writeOffset = 0; u32 writeOffset = 0;
getFIRMWrite(firmLocation, firmSize, &writeOffset); getFIRMWrite(firmLocation, firmSize, &writeOffset);
@ -212,7 +212,7 @@ u8 patchFirm(void){
//Read reboot code from SD //Read reboot code from SD
char path[] = "/rei/reboot/reboot.bin"; char path[] = "/rei/reboot/reboot.bin";
u32 size = fileSize(path); u32 size = fileSize(path);
if (!size) return 1; if(!size) return 0;
getReboot(firmLocation, firmSize, &rebootOffset); getReboot(firmLocation, firmSize, &rebootOffset);
fileRead((u8*)rebootOffset, path, size); fileRead((u8*)rebootOffset, path, size);
@ -230,9 +230,9 @@ u8 patchFirm(void){
//Write patched FIRM to SD if needed //Write patched FIRM to SD if needed
if(firmPathPatched) 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){ void launchFirm(void){

View File

@ -8,80 +8,57 @@
static FATFS fs; static FATFS fs;
int mountSD() int mountSD(void){
{ if(f_mount(&fs, "0:", 1) != FR_OK) return 0;
if (f_mount(&fs, "0:", 1) != FR_OK) { return 1;
//printF("Failed to mount SD card!");
return 1;
}
//printF("Mounted SD card");
return 0;
} }
int fileReadOffset(u8 *dest, const char *path, u32 size, u32 offset){ int fileRead(u8 *dest, const char *path, u32 size){
FRESULT fr; FRESULT fr;
FIL fp; FIL fp;
unsigned int br = 0; unsigned int br = 0;
fr = f_open(&fp, path, FA_READ); fr = f_open(&fp, path, FA_READ);
if (fr != FR_OK)goto error; if(fr == FR_OK){
if(!size) size = f_size(&fp);
if (!size) size = f_size(&fp); fr = f_read(&fp, dest, size, &br);
if (offset) {
fr = f_lseek(&fp, offset);
if (fr != FR_OK) goto error;
} }
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); f_close(&fp);
return fr; return fr ? 0 : 1;
}
int fileRead(u8 *dest, const char *path, u32 size){
return fileReadOffset(dest, path, size, 0);
} }
int fileWrite(const u8 *buffer, const char *path, u32 size){ int fileWrite(const u8 *buffer, const char *path, u32 size){
FRESULT fr = 1; FRESULT fr;
FIL fp; FIL fp;
unsigned int br = 0; unsigned int br = 0;
f_unlink(path); f_unlink(path);
if(f_open(&fp, path, FA_WRITE | FA_OPEN_ALWAYS) == FR_OK){
fr = f_write(&fp, buffer, size, &br); fr = f_open(&fp, path, FA_WRITE | FA_OPEN_ALWAYS);
f_close(&fp); if(fr == FR_OK) fr = f_write(&fp, buffer, size, &br);
if (fr == FR_OK && br == size) return 0;
} f_close(&fp);
return fr; return fr ? 0 : 1;
} }
int fileSize(const char* path){ int fileSize(const char* path){
FRESULT fr;
FIL fp; FIL fp;
int size = 0; int size = 0;
fr = f_open(&fp, path, FA_READ); if(f_open(&fp, path, FA_READ) == FR_OK)
if (fr != FR_OK)goto error; size = f_size(&fp);
size = f_size(&fp); f_close(&fp);
error: return size;
f_close(&fp);
return size;
} }
int fileExists(const char* path){ int fileExists(const char* path){
FRESULT fr;
FIL fp; FIL fp;
int exists = 1; int exists = 0;
fr = f_open(&fp, path, FA_READ);
if (fr != FR_OK)exists = 0; if(f_open(&fp, path, FA_READ) == FR_OK) exists = 1;
f_close(&fp); f_close(&fp);
return exists; return exists;
} }

View File

@ -7,8 +7,7 @@
#include "types.h" #include "types.h"
int mountSD(); int mountSD(void);
int fileReadOffset(u8 *dest, const char *path, u32 size, u32 offset);
int fileRead(u8 *dest, const char *path, u32 size); int fileRead(u8 *dest, const char *path, u32 size);
int fileWrite(const u8 *buffer, const char *path, u32 size); int fileWrite(const u8 *buffer, const char *path, u32 size);
int fileSize(const char* path); int fileSize(const char* path);

View File

@ -14,8 +14,8 @@ u8 main(){
mountSD(); mountSD();
loadSplash(); loadSplash();
setupCFW(); setupCFW();
if (loadFirm()) return 1; if (!loadFirm()) return 0;
if (patchFirm()) return 1; if (!patchFirm()) return 0;
launchFirm(); launchFirm();
return 0; return 1;
} }