2015-08-05 03:57:37 +02:00
|
|
|
/*
|
|
|
|
* fs.c
|
2016-03-23 02:27:53 +01:00
|
|
|
* by Reisyukaku / Aurora Wright
|
|
|
|
* Copyright (c) 2016 All Rights Reserved
|
2015-08-05 03:57:37 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "fs.h"
|
2016-04-11 05:15:44 +02:00
|
|
|
#include "memory.h"
|
2015-08-05 03:57:37 +02:00
|
|
|
#include "fatfs/ff.h"
|
|
|
|
|
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)
|
|
|
|
{
|
2015-08-05 03:57:37 +02:00
|
|
|
FRESULT fr;
|
|
|
|
FIL fp;
|
2016-02-08 03:37:03 +01:00
|
|
|
unsigned int br = 0;
|
2015-08-05 03:57:37 +02:00
|
|
|
|
|
|
|
fr = f_open(&fp, path, FA_READ);
|
2016-04-02 17:58:06 +02:00
|
|
|
if(fr == FR_OK)
|
|
|
|
{
|
2016-03-05 23:27:02 +01:00
|
|
|
if(!size) size = f_size(&fp);
|
|
|
|
fr = f_read(&fp, dest, size, &br);
|
2015-08-05 03:57:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
f_close(&fp);
|
2016-04-13 01:08:13 +02:00
|
|
|
|
2016-03-05 23:27:02 +01:00
|
|
|
return fr ? 0 : 1;
|
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-03-05 23:27:02 +01:00
|
|
|
FRESULT fr;
|
2015-08-05 03:57:37 +02:00
|
|
|
FIL fp;
|
2016-02-08 03:37:03 +01:00
|
|
|
unsigned int br = 0;
|
2015-08-05 03:57:37 +02:00
|
|
|
|
2016-03-05 23:27:02 +01:00
|
|
|
fr = f_open(&fp, path, FA_WRITE | FA_OPEN_ALWAYS);
|
|
|
|
if(fr == FR_OK) fr = f_write(&fp, buffer, size, &br);
|
|
|
|
|
|
|
|
f_close(&fp);
|
2016-04-13 01:08:13 +02:00
|
|
|
|
2016-03-05 23:27:02 +01:00
|
|
|
return fr ? 0 : 1;
|
2016-01-23 09:53:45 +01:00
|
|
|
}
|
|
|
|
|
2016-04-02 17:58:06 +02:00
|
|
|
u32 fileSize(const char *path)
|
|
|
|
{
|
2016-01-23 09:53:45 +01:00
|
|
|
FIL fp;
|
2016-03-06 03:41:07 +01:00
|
|
|
u32 size = 0;
|
2016-01-23 09:53:45 +01:00
|
|
|
|
2016-03-05 23:27:02 +01:00
|
|
|
if(f_open(&fp, path, FA_READ) == FR_OK)
|
|
|
|
size = f_size(&fp);
|
|
|
|
|
|
|
|
f_close(&fp);
|
2016-04-13 01:08:13 +02:00
|
|
|
|
2016-03-05 23:27:02 +01:00
|
|
|
return size;
|
2016-02-20 15:29:32 +01:00
|
|
|
}
|
|
|
|
|
2016-04-02 17:58:06 +02:00
|
|
|
u32 fileExists(const char *path)
|
|
|
|
{
|
2016-02-20 15:29:32 +01:00
|
|
|
FIL fp;
|
2016-03-10 14:58:11 +01:00
|
|
|
u32 exists = 0;
|
2016-03-05 23:27:02 +01:00
|
|
|
|
|
|
|
if(f_open(&fp, path, FA_READ) == FR_OK) exists = 1;
|
|
|
|
|
2016-02-20 15:29:32 +01:00
|
|
|
f_close(&fp);
|
2016-04-13 01:08:13 +02:00
|
|
|
|
2016-02-20 15:29:32 +01:00
|
|
|
return exists;
|
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;
|
|
|
|
FILINFO info = { .lfname = NULL };
|
|
|
|
|
|
|
|
f_opendir(&dir, path);
|
|
|
|
|
|
|
|
u32 id = 0;
|
|
|
|
|
|
|
|
//Parse the target directory
|
|
|
|
while(f_readdir(&dir, &info) == FR_OK)
|
|
|
|
{
|
|
|
|
//We've parsed the whole folder
|
|
|
|
if(!info.fname[0]) break;
|
|
|
|
|
|
|
|
//Not a cxi
|
|
|
|
if(info.fname[9] != 'a' && info.fname[9] != 'A') continue;
|
|
|
|
|
|
|
|
//Convert the .app name to an integer
|
|
|
|
u32 tempId = 0;
|
2016-04-13 00:15:09 +02:00
|
|
|
for(char *tmp = info.fname; *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
|
|
|
}
|
|
|
|
|
|
|
|
//Found a newer 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
|
2016-04-13 00:15:09 +02:00
|
|
|
while(id > 0)
|
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
|
|
|
}
|