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"
|
2016-04-11 05:15:44 +02:00
|
|
|
#include "memory.h"
|
2016-08-30 17:48:41 +02:00
|
|
|
#include "strings.h"
|
2016-09-22 14:48:28 +02:00
|
|
|
#include "crypto.h"
|
2016-06-14 19:50:38 +02:00
|
|
|
#include "cache.h"
|
2016-06-10 21:48:22 +02:00
|
|
|
#include "screen.h"
|
2015-08-05 03:57:37 +02:00
|
|
|
#include "fatfs/ff.h"
|
2016-04-29 17:21:34 +02:00
|
|
|
#include "buttons.h"
|
2016-09-23 02:06:04 +02:00
|
|
|
#include "../build/bundled.h"
|
2015-08-05 03:57:37 +02:00
|
|
|
|
2016-04-11 05:15:44 +02:00
|
|
|
static FATFS sdFs,
|
|
|
|
nandFs;
|
2015-08-05 03:57:37 +02:00
|
|
|
|
2016-07-18 22:42:55 +02:00
|
|
|
void mountFs(void)
|
2016-04-02 17:58:06 +02:00
|
|
|
{
|
2016-07-18 22:42:55 +02:00
|
|
|
f_mount(&sdFs, "0:", 1);
|
2016-04-11 05:15:44 +02:00
|
|
|
f_mount(&nandFs, "1:", 0);
|
2015-08-05 03:57:37 +02:00
|
|
|
}
|
|
|
|
|
2016-09-13 14:54:14 +02:00
|
|
|
u32 fileRead(void *dest, const char *path, u32 maxSize)
|
2016-04-02 17:58:06 +02:00
|
|
|
{
|
2016-04-28 16:11:25 +02:00
|
|
|
FIL file;
|
2016-09-13 14:54:14 +02:00
|
|
|
u32 ret = 0;
|
2015-08-05 03:57:37 +02:00
|
|
|
|
2016-05-03 20:03:37 +02:00
|
|
|
if(f_open(&file, path, FA_READ) == FR_OK)
|
2016-04-02 17:58:06 +02:00
|
|
|
{
|
2016-09-13 14:54:14 +02:00
|
|
|
u32 size = f_size(&file);
|
|
|
|
if(dest == NULL) ret = size;
|
|
|
|
else if(!(maxSize > 0 && size > maxSize))
|
|
|
|
f_read(&file, dest, size, (unsigned int *)&ret);
|
2016-05-03 20:03:37 +02:00
|
|
|
f_close(&file);
|
2015-08-05 03:57:37 +02:00
|
|
|
}
|
|
|
|
|
2016-09-13 14:54:14 +02:00
|
|
|
return ret;
|
2015-08-05 03:57:37 +02:00
|
|
|
}
|
|
|
|
|
2016-06-27 13:21:08 +02:00
|
|
|
u32 getFileSize(const char *path)
|
|
|
|
{
|
2016-09-13 14:54:14 +02:00
|
|
|
return fileRead(NULL, path, 0);
|
2016-06-27 13:21:08 +02:00
|
|
|
}
|
|
|
|
|
2016-07-18 22:42:55 +02:00
|
|
|
bool fileWrite(const void *buffer, const char *path, u32 size)
|
2016-04-02 17:58:06 +02:00
|
|
|
{
|
2016-04-28 16:11:25 +02:00
|
|
|
FIL file;
|
2015-08-05 03:57:37 +02:00
|
|
|
|
2016-08-30 16:56:19 +02:00
|
|
|
FRESULT result = f_open(&file, path, FA_WRITE | FA_OPEN_ALWAYS);
|
|
|
|
|
|
|
|
if(result == FR_OK)
|
2016-04-28 16:11:25 +02:00
|
|
|
{
|
2016-05-03 20:03:37 +02:00
|
|
|
unsigned int written;
|
|
|
|
f_write(&file, buffer, size, &written);
|
2016-09-22 14:48:28 +02:00
|
|
|
f_truncate(&file);
|
2016-05-03 20:03:37 +02:00
|
|
|
f_close(&file);
|
2016-07-18 22:42:55 +02:00
|
|
|
|
|
|
|
return true;
|
2016-04-28 16:11:25 +02:00
|
|
|
}
|
2016-09-06 18:08:54 +02:00
|
|
|
|
|
|
|
if(result == FR_NO_PATH)
|
2016-08-30 16:56:19 +02:00
|
|
|
{
|
|
|
|
for(u32 i = 1; path[i] != 0; i++)
|
|
|
|
if(path[i] == '/')
|
|
|
|
{
|
2016-09-13 23:01:38 +02:00
|
|
|
char folder[i + 1];
|
2016-08-30 16:56:19 +02:00
|
|
|
memcpy(folder, path, i);
|
|
|
|
folder[i] = 0;
|
|
|
|
f_mkdir(folder);
|
|
|
|
}
|
|
|
|
|
|
|
|
return fileWrite(buffer, path, size);
|
|
|
|
}
|
2016-09-06 18:08:54 +02:00
|
|
|
|
|
|
|
return false;
|
2016-07-18 22:42:55 +02:00
|
|
|
}
|
|
|
|
|
2016-08-28 15:50:11 +02:00
|
|
|
void fileDelete(const char *path)
|
|
|
|
{
|
|
|
|
f_unlink(path);
|
|
|
|
}
|
|
|
|
|
2016-08-15 03:51:48 +02:00
|
|
|
void loadPayload(u32 pressed)
|
2016-04-02 17:58:06 +02:00
|
|
|
{
|
2016-04-29 14:28:37 +02:00
|
|
|
const char *pattern;
|
|
|
|
|
2016-09-08 00:49:55 +02:00
|
|
|
if(pressed & BUTTON_LEFT) pattern = PATTERN("left");
|
|
|
|
else if(pressed & BUTTON_RIGHT) pattern = PATTERN("right");
|
2016-04-29 14:28:37 +02:00
|
|
|
else if(pressed & BUTTON_UP) pattern = PATTERN("up");
|
|
|
|
else if(pressed & BUTTON_DOWN) pattern = PATTERN("down");
|
2016-09-08 00:49:55 +02:00
|
|
|
else if(pressed & BUTTON_START) pattern = PATTERN("start");
|
|
|
|
else if(pressed & BUTTON_B) pattern = PATTERN("b");
|
2016-04-29 14:28:37 +02:00
|
|
|
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");
|
2016-08-30 21:50:04 +02:00
|
|
|
else pattern = PATTERN("select");
|
2016-04-29 14:28:37 +02:00
|
|
|
|
2016-04-17 18:57:25 +02:00
|
|
|
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
|
|
|
|
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-09-18 20:10:24 +02:00
|
|
|
if(result == FR_OK && info.fname[0] != 0)
|
2016-04-29 14:28:37 +02:00
|
|
|
{
|
2016-09-12 16:13:41 +02:00
|
|
|
u32 *loaderAddress = (u32 *)0x24FFFF00;
|
2016-09-13 14:54:14 +02:00
|
|
|
u8 *payloadAddress = (u8 *)0x24F00000;
|
2016-04-29 14:28:37 +02:00
|
|
|
|
2016-09-23 02:06:04 +02:00
|
|
|
memcpy(loaderAddress, loader_bin, loader_bin_size);
|
2016-05-03 03:05:11 +02:00
|
|
|
|
2016-08-30 17:48:41 +02:00
|
|
|
concatenateStrings(path, "/");
|
|
|
|
concatenateStrings(path, info.altname);
|
2016-05-03 03:05:11 +02:00
|
|
|
|
2016-09-13 14:54:14 +02:00
|
|
|
u32 payloadSize = fileRead(payloadAddress, path, (u8 *)loaderAddress - payloadAddress);
|
|
|
|
|
|
|
|
if(payloadSize > 0)
|
|
|
|
{
|
|
|
|
loaderAddress[1] = payloadSize;
|
2016-04-29 14:28:37 +02:00
|
|
|
|
2016-09-26 13:53:58 +02:00
|
|
|
if(isA9lh) restoreShaHashBackup();
|
2016-09-13 14:54:14 +02:00
|
|
|
initScreens();
|
2016-07-18 16:58:29 +02:00
|
|
|
|
2016-09-23 02:06:04 +02:00
|
|
|
flushDCacheRange(loaderAddress, loader_bin_size);
|
|
|
|
flushICacheRange(loaderAddress, loader_bin_size);
|
2016-09-13 14:54:14 +02:00
|
|
|
|
|
|
|
((void (*)())loaderAddress)();
|
|
|
|
}
|
2016-04-29 14:28:37 +02:00
|
|
|
}
|
2016-03-23 02:27:53 +01:00
|
|
|
}
|
|
|
|
|
2016-07-18 16:58:29 +02:00
|
|
|
u32 firmRead(void *dest, u32 firmType)
|
2016-04-02 17:58:06 +02:00
|
|
|
{
|
2016-09-18 23:50:44 +02:00
|
|
|
const char *firmFolders[][2] = {{ "00000002", "20000002" },
|
2016-07-18 16:58:29 +02:00
|
|
|
{ "00000102", "20000102" },
|
|
|
|
{ "00000202", "20000202" },
|
|
|
|
{ "00000003", "20000003" }};
|
|
|
|
|
2016-08-30 19:58:38 +02:00
|
|
|
char path[48] = "1:/title/00040138/";
|
2016-08-30 17:48:41 +02:00
|
|
|
concatenateStrings(path, firmFolders[firmType][isN3DS ? 1 : 0]);
|
2016-08-30 19:58:38 +02:00
|
|
|
concatenateStrings(path, "/content");
|
2016-04-11 05:15:44 +02:00
|
|
|
|
|
|
|
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-07-19 20:03:35 +02:00
|
|
|
u32 firmVersion = 0xFFFFFFFF;
|
2016-04-11 05:15:44 +02:00
|
|
|
|
|
|
|
//Parse the target directory
|
2016-09-18 20:10:24 +02:00
|
|
|
while(f_readdir(&dir, &info) == FR_OK && info.fname[0] != 0)
|
2016-04-11 05:15:44 +02:00
|
|
|
{
|
|
|
|
//Not a cxi
|
2016-09-18 13:29:23 +02:00
|
|
|
if(info.fname[9] != 'a') continue;
|
2016-04-11 05:15:44 +02:00
|
|
|
|
|
|
|
//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-04-11 05:15:44 +02:00
|
|
|
{
|
2016-07-19 20:03:35 +02:00
|
|
|
tempVersion <<= 4;
|
|
|
|
tempVersion += *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
|
2016-07-19 20:03:35 +02:00
|
|
|
if(tempVersion < firmVersion) firmVersion = tempVersion;
|
2016-04-11 05:15:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
f_closedir(&dir);
|
|
|
|
|
|
|
|
//Complete the string with the .app name
|
2016-08-30 17:48:41 +02:00
|
|
|
concatenateStrings(path, "/00000000.app");
|
2016-04-11 05:15:44 +02:00
|
|
|
|
|
|
|
//Convert back the .app name from integer to array
|
2016-09-13 00:52:15 +02:00
|
|
|
hexItoa(firmVersion, &path[35], 8);
|
2016-04-11 05:15:44 +02:00
|
|
|
|
2016-09-13 14:54:14 +02:00
|
|
|
fileRead(dest, path, 0);
|
2016-07-18 16:58:29 +02:00
|
|
|
|
2016-07-19 20:03:35 +02:00
|
|
|
return firmVersion;
|
2016-08-30 17:09:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void findDumpFile(const char *path, char *fileName)
|
|
|
|
{
|
|
|
|
DIR dir;
|
|
|
|
FILINFO info;
|
2016-09-26 13:24:12 +02:00
|
|
|
FRESULT result;
|
2016-08-30 17:09:04 +02:00
|
|
|
u32 n = 0;
|
|
|
|
|
2016-09-26 13:24:12 +02:00
|
|
|
while(true)
|
2016-08-30 17:09:04 +02:00
|
|
|
{
|
2016-09-26 13:24:12 +02:00
|
|
|
result = f_findfirst(&dir, &info, path, fileName);
|
|
|
|
|
|
|
|
if(result != FR_OK || !info.fname[0]) break;
|
|
|
|
|
2016-08-30 17:09:04 +02:00
|
|
|
u32 i = 18,
|
|
|
|
tmp = ++n;
|
|
|
|
|
2016-09-08 18:47:13 +02:00
|
|
|
while(tmp > 0)
|
2016-08-30 17:09:04 +02:00
|
|
|
{
|
|
|
|
fileName[i--] = '0' + (tmp % 10);
|
|
|
|
tmp /= 10;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-26 13:24:12 +02:00
|
|
|
if(result == FR_OK) f_closedir(&dir);
|
2016-09-19 14:03:55 +02:00
|
|
|
}
|