This repository has been archived on 2022-05-31. You can view files and clone it, but cannot push or open issues or pull requests.
Luma3DS-3GX/source/fs.c

206 lines
5.2 KiB
C
Raw Normal View History

2015-08-05 03:57:37 +02:00
/*
2016-07-05 16:05:53 +02:00
* This file is part of Luma3DS
* Copyright (C) 2016 Aurora Wright, TuxSH
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* Additional Terms 7.b of GPLv3 applies to this file: Requiring preservation of specified
* reasonable legal notices or author attributions in that material or in the Appropriate Legal
* Notices displayed by works containing it.
2015-08-05 03:57:37 +02:00
*/
#include "fs.h"
#include "memory.h"
#include "cache.h"
#include "screen.h"
2015-08-05 03:57:37 +02:00
#include "fatfs/ff.h"
#include "buttons.h"
2016-04-29 18:07:00 +02:00
#include "../build/loader.h"
2015-08-05 03:57:37 +02:00
static FATFS sdFs,
nandFs;
2015-08-05 03:57:37 +02:00
void mountFs(void)
{
f_mount(&sdFs, "0:", 1);
f_mount(&nandFs, "1:", 0);
2015-08-05 03:57:37 +02:00
}
u32 fileRead(void *dest, const char *path)
{
2016-04-28 16:11:25 +02:00
FIL file;
u32 size;
2015-08-05 03:57:37 +02:00
if(f_open(&file, path, FA_READ) == FR_OK)
{
2016-04-28 16:11:25 +02:00
unsigned int read;
size = f_size(&file);
if(dest != NULL)
f_read(&file, dest, size, &read);
f_close(&file);
2015-08-05 03:57:37 +02:00
}
else size = 0;
2015-08-05 03:57:37 +02:00
return size;
2015-08-05 03:57:37 +02:00
}
u32 getFileSize(const char *path)
{
return fileRead(NULL, path);
}
bool fileWrite(const void *buffer, const char *path, u32 size)
{
2016-04-28 16:11:25 +02:00
FIL file;
2015-08-05 03:57:37 +02:00
if(f_open(&file, path, FA_WRITE | FA_OPEN_ALWAYS) == FR_OK)
2016-04-28 16:11:25 +02:00
{
unsigned int written;
f_write(&file, buffer, size, &written);
f_close(&file);
return true;
2016-04-28 16:11:25 +02:00
}
return false;
}
void createDirectory(const char *path)
{
f_mkdir(path);
}
void loadPayload(u32 pressed, bool needToInitScreens)
{
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 if(pressed & BUTTON_SELECT) pattern = PATTERN("select");
else pattern = "nlc.bin";
DIR dir;
2016-04-18 18:04:04 +02:00
FILINFO info;
2016-05-03 03:05:11 +02:00
char path[28] = "/luma/payloads";
2016-03-05 23:27:02 +01:00
FRESULT result = f_findfirst(&dir, &info, path, pattern);
2016-03-05 23:27:02 +01:00
f_closedir(&dir);
if(result == FR_OK && info.fname[0])
{
if(needToInitScreens) initScreens();
u32 *const loaderAddress = (u32 *)0x24FFFF00;
2016-05-03 03:05:11 +02:00
memcpy(loaderAddress, loader, loader_size);
path[14] = '/';
memcpy(&path[15], info.altname, 13);
loaderAddress[1] = fileRead((void *)0x24F00000, path);
2016-05-12 15:08:12 +02:00
if(pattern[0] == 'n') f_unlink(path);
flushDCacheRange(loaderAddress, loader_size);
flushICacheRange(loaderAddress, loader_size);
2016-05-03 03:05:11 +02:00
((void (*)())loaderAddress)();
}
}
void findDumpFile(const char *path, char *fileName)
{
DIR dir;
FILINFO info;
u32 n = 0;
while(f_findfirst(&dir, &info, path, fileName) == FR_OK && info.fname[0])
{
u32 i = 18,
tmp = ++n;
while(tmp)
{
fileName[i--] = '0' + (tmp % 10);
tmp /= 10;
}
}
f_closedir(&dir);
}
u32 firmRead(void *dest, u32 firmType)
{
const char *firmFolders[4][2] = {{ "00000002", "20000002" },
{ "00000102", "20000102" },
{ "00000202", "20000202" },
{ "00000003", "20000003" }};
char path[48] = "1:/title/00040138/00000000/content";
memcpy(&path[18], firmFolders[firmType][isN3DS ? 1 : 0], 8);
DIR dir;
2016-04-18 18:04:04 +02:00
FILINFO info;
f_opendir(&dir, path);
2016-07-19 20:03:35 +02:00
u32 firmVersion = 0xFFFFFFFF;
//Parse the target directory
2016-04-21 23:39:05 +02:00
while(f_readdir(&dir, &info) == FR_OK && info.fname[0])
{
//Not a cxi
2016-04-18 18:04:04 +02:00
if(info.altname[9] != 'A') continue;
//Convert the .app name to an integer
2016-07-19 20:03:35 +02:00
u32 tempVersion = 0;
2016-04-18 18:04:04 +02:00
for(char *tmp = info.altname; *tmp != '.'; tmp++)
{
2016-07-19 20:03:35 +02:00
tempVersion <<= 4;
tempVersion += *tmp > '9' ? *tmp - 'A' + 10 : *tmp - '0';
}
//Found an older cxi
2016-07-19 20:03:35 +02:00
if(tempVersion < firmVersion) firmVersion = tempVersion;
}
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
u32 i = 42;
//Convert back the .app name from integer to array
2016-07-19 20:03:35 +02:00
u32 tempVersion = firmVersion;
while(tempVersion)
{
static const char hexDigits[] = "0123456789ABCDEF";
2016-07-19 20:03:35 +02:00
path[i--] = hexDigits[tempVersion & 0xF];
tempVersion >>= 4;
}
fileRead(dest, path);
2016-07-19 20:03:35 +02:00
return firmVersion;
2016-05-27 15:15:25 +02:00
}