2015-08-05 03:57:37 +02:00
|
|
|
/*
|
|
|
|
* fs.c
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "fs.h"
|
2016-04-11 05:15:44 +02:00
|
|
|
#include "memory.h"
|
2016-04-29 14:28:37 +02:00
|
|
|
#include "buttons.h"
|
2015-08-05 03:57:37 +02:00
|
|
|
#include "fatfs/ff.h"
|
|
|
|
|
2016-04-29 14:28:37 +02:00
|
|
|
#define PATTERN(a) a "_*.bin"
|
|
|
|
|
2016-04-11 05:15:44 +02:00
|
|
|
static FATFS sdFs,
|
|
|
|
nandFs;
|
2015-08-05 03:57:37 +02:00
|
|
|
|
2016-04-11 05:15:44 +02:00
|
|
|
u32 mountFs(void)
|
2016-04-02 17:58:06 +02:00
|
|
|
{
|
2016-04-11 05:15:44 +02:00
|
|
|
if(f_mount(&sdFs, "0:", 1) != FR_OK) return 0;
|
|
|
|
f_mount(&nandFs, "1:", 0);
|
2016-04-13 01:08:13 +02:00
|
|
|
|
2016-03-05 23:27:02 +01:00
|
|
|
return 1;
|
2015-08-05 03:57:37 +02:00
|
|
|
}
|
|
|
|
|
2016-04-02 17:58:06 +02:00
|
|
|
u32 fileRead(void *dest, const char *path, u32 size)
|
|
|
|
{
|
2016-04-28 16:11:25 +02:00
|
|
|
FRESULT result;
|
|
|
|
FIL file;
|
2015-08-05 03:57:37 +02:00
|
|
|
|
2016-04-28 16:11:25 +02:00
|
|
|
result = f_open(&file, path, FA_READ);
|
|
|
|
if(result == FR_OK)
|
2016-04-02 17:58:06 +02:00
|
|
|
{
|
2016-04-28 16:11:25 +02:00
|
|
|
unsigned int read;
|
|
|
|
if(!size) size = f_size(&file);
|
|
|
|
result = f_read(&file, dest, size, &read);
|
2015-08-05 03:57:37 +02:00
|
|
|
}
|
|
|
|
|
2016-04-28 16:11:25 +02:00
|
|
|
f_close(&file);
|
2016-04-13 01:08:13 +02:00
|
|
|
|
2016-04-28 16:11:25 +02:00
|
|
|
return result;
|
2015-08-05 03:57:37 +02:00
|
|
|
}
|
|
|
|
|
2016-04-02 17:58:06 +02:00
|
|
|
u32 fileWrite(const void *buffer, const char *path, u32 size)
|
|
|
|
{
|
2016-04-28 16:11:25 +02:00
|
|
|
FRESULT result;
|
|
|
|
FIL file;
|
2015-08-05 03:57:37 +02:00
|
|
|
|
2016-04-28 16:11:25 +02:00
|
|
|
result = f_open(&file, path, FA_WRITE | FA_OPEN_ALWAYS);
|
|
|
|
if(result == FR_OK)
|
|
|
|
{
|
|
|
|
unsigned int read;
|
|
|
|
result = f_write(&file, buffer, size, &read);
|
|
|
|
}
|
2016-03-05 23:27:02 +01:00
|
|
|
|
2016-04-28 16:11:25 +02:00
|
|
|
f_close(&file);
|
2016-04-13 01:08:13 +02:00
|
|
|
|
2016-04-28 16:11:25 +02:00
|
|
|
return result;
|
2016-01-23 09:53:45 +01:00
|
|
|
}
|
|
|
|
|
2016-04-29 14:28:37 +02:00
|
|
|
u32 payloadExists(u32 pressed)
|
2016-04-02 17:58:06 +02:00
|
|
|
{
|
2016-04-29 14:28:37 +02:00
|
|
|
const char *pattern;
|
|
|
|
|
|
|
|
if(pressed & BUTTON_RIGHT) pattern = PATTERN("right");
|
|
|
|
else if(pressed & BUTTON_LEFT) pattern = PATTERN("left");
|
|
|
|
else if(pressed & BUTTON_UP) pattern = PATTERN("up");
|
|
|
|
else if(pressed & BUTTON_DOWN) pattern = PATTERN("down");
|
|
|
|
else if(pressed & BUTTON_X) pattern = PATTERN("x");
|
|
|
|
else if(pressed & BUTTON_Y) pattern = PATTERN("y");
|
|
|
|
else if(pressed & BUTTON_R1) pattern = PATTERN("r");
|
|
|
|
else if(pressed & BUTTON_A) pattern = PATTERN("a");
|
|
|
|
else if(pressed & BUTTON_START) pattern = PATTERN("start");
|
|
|
|
else pattern = PATTERN("select");
|
|
|
|
|
2016-04-17 18:57:25 +02:00
|
|
|
DIR dir;
|
2016-04-18 18:04:04 +02:00
|
|
|
FILINFO info;
|
2016-04-29 14:28:37 +02:00
|
|
|
char path[] = "/luma/payloads";
|
2016-03-05 23:27:02 +01:00
|
|
|
|
2016-04-29 14:28:37 +02:00
|
|
|
FRESULT result = f_findfirst(&dir, &info, path, pattern);
|
2016-03-05 23:27:02 +01:00
|
|
|
|
2016-04-17 18:57:25 +02:00
|
|
|
f_closedir(&dir);
|
2016-04-13 01:08:13 +02:00
|
|
|
|
2016-04-29 14:28:37 +02:00
|
|
|
if(result == FR_OK && info.fname[0])
|
|
|
|
{
|
|
|
|
path[sizeof(path) - 1] = '/';
|
|
|
|
memcpy((void *)0x24F02000, path, sizeof(path));
|
|
|
|
memcpy((void *)(0x24F02000 + sizeof(path)), info.altname, 13);
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
2016-03-23 02:27:53 +01:00
|
|
|
}
|
|
|
|
|
2016-04-11 14:32:38 +02:00
|
|
|
void firmRead(void *dest, const char *firmFolder)
|
2016-04-02 17:58:06 +02:00
|
|
|
{
|
2016-04-11 05:15:44 +02:00
|
|
|
char path[48] = "1:/title/00040138/00000000/content";
|
|
|
|
memcpy(&path[18], firmFolder, 8);
|
|
|
|
|
|
|
|
DIR dir;
|
2016-04-18 18:04:04 +02:00
|
|
|
FILINFO info;
|
2016-04-11 05:15:44 +02:00
|
|
|
|
|
|
|
f_opendir(&dir, path);
|
|
|
|
|
2016-04-27 16:16:25 +02:00
|
|
|
u32 id = 0xFFFFFFFF;
|
2016-04-11 05:15:44 +02:00
|
|
|
|
|
|
|
//Parse the target directory
|
2016-04-21 23:39:05 +02:00
|
|
|
while(f_readdir(&dir, &info) == FR_OK && info.fname[0])
|
2016-04-11 05:15:44 +02:00
|
|
|
{
|
|
|
|
//Not a cxi
|
2016-04-18 18:04:04 +02:00
|
|
|
if(info.altname[9] != 'A') continue;
|
2016-04-11 05:15:44 +02:00
|
|
|
|
|
|
|
//Convert the .app name to an integer
|
|
|
|
u32 tempId = 0;
|
2016-04-18 18:04:04 +02:00
|
|
|
for(char *tmp = info.altname; *tmp != '.'; tmp++)
|
2016-04-11 05:15:44 +02:00
|
|
|
{
|
2016-04-12 23:18:07 +02:00
|
|
|
tempId <<= 4;
|
2016-04-13 00:15:09 +02:00
|
|
|
tempId += *tmp > '9' ? *tmp - 'A' + 10 : *tmp - '0';
|
2016-04-11 05:15:44 +02:00
|
|
|
}
|
|
|
|
|
2016-04-27 15:37:13 +02:00
|
|
|
//Found an older cxi
|
|
|
|
if(tempId < id) id = tempId;
|
2016-04-11 05:15:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
f_closedir(&dir);
|
|
|
|
|
|
|
|
//Complete the string with the .app name
|
|
|
|
memcpy(&path[34], "/00000000.app", 14);
|
|
|
|
|
2016-04-24 19:23:40 +02:00
|
|
|
//Last digit of the .app
|
2016-04-11 05:15:44 +02:00
|
|
|
u32 i = 42;
|
|
|
|
|
|
|
|
//Convert back the .app name from integer to array
|
2016-04-26 01:30:03 +02:00
|
|
|
while(id)
|
2016-04-11 05:15:44 +02:00
|
|
|
{
|
2016-04-13 00:15:09 +02:00
|
|
|
static const char hexDigits[] = "0123456789ABCDEF";
|
|
|
|
path[i--] = hexDigits[id & 0xF];
|
|
|
|
id >>= 4;
|
2016-04-11 05:15:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
fileRead(dest, path, 0);
|
2015-08-05 03:57:37 +02:00
|
|
|
}
|