106 lines
2.1 KiB
C
Raw Normal View History

2016-03-19 17:30:56 +01:00
/*
* fs.c
*/
#include "fs.h"
#include "memory.h"
2016-03-19 17:30:56 +01:00
#include "fatfs/ff.h"
static FATFS fs;
u32 mountSD(void)
{
2016-03-19 17:30:56 +01:00
if(f_mount(&fs, "0:", 1) != FR_OK) return 0;
return 1;
}
u32 mountCTRNAND(void)
{
if(f_mount(&fs, "1:", 1) != FR_OK) return 0;
return 1;
}
u32 fileRead(void *dest, const char *path)
{
FIL file;
u32 size;
2016-03-19 17:30:56 +01:00
if(f_open(&file, path, FA_READ) == FR_OK)
{
unsigned int read;
size = f_size(&file);
f_read(&file, dest, size, &read);
f_close(&file);
2016-03-19 17:30:56 +01:00
}
else size = 0;
return size;
}
2016-03-19 17:30:56 +01:00
void fileWrite(const void *buffer, const char *path, u32 size)
{
FIL file;
if(f_open(&file, path, FA_WRITE | FA_OPEN_ALWAYS) == FR_OK)
{
unsigned int written;
f_write(&file, buffer, size, &written);
f_close(&file);
}
2016-03-19 17:30:56 +01:00
}
u32 firmRead(void *dest)
{
const char *firmFolders[] = { "00000002", "20000002" };
char path[48] = "1:/title/00040138/00000000/content";
memcpy(&path[18], firmFolders[console], 8);
2016-03-19 17:30:56 +01:00
DIR dir;
FILINFO info;
2016-03-19 17:30:56 +01:00
f_opendir(&dir, path);
u32 id = 0xFFFFFFFF;
//Parse the target directory
while(f_readdir(&dir, &info) == FR_OK && info.fname[0])
{
//Not a cxi
if(info.altname[9] != 'A') continue;
if(id != 0xFFFFFFFF) return 1;
//Convert the .app name to an integer
u32 tempId = 0;
for(char *tmp = info.altname; *tmp != '.'; tmp++)
{
tempId <<= 4;
tempId += *tmp > '9' ? *tmp - 'A' + 10 : *tmp - '0';
}
if((console && tempId >= 0x21) || (!console && tempId >= 0x52)) return 2;
//Found an older cxi
if(tempId < id) id = tempId;
}
f_closedir(&dir);
//Complete the string with the .app name
memcpy(&path[34], "/00000000.app", 14);
//Last digit of the .app
u32 i = 42;
//Convert back the .app name from integer to array
while(id)
{
static const char hexDigits[] = "0123456789ABCDEF";
path[i--] = hexDigits[id & 0xF];
id >>= 4;
}
fileRead(dest, path);
return 0;
2016-03-19 17:30:56 +01:00
}