2015-08-05 03:57:37 +02:00
|
|
|
/*
|
2016-07-05 16:05:53 +02:00
|
|
|
* This file is part of Luma3DS
|
2020-04-25 14:26:21 +02:00
|
|
|
* Copyright (C) 2016-2020 Aurora Wright, TuxSH
|
2016-07-05 16:05:53 +02:00
|
|
|
*
|
|
|
|
* 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/>.
|
|
|
|
*
|
2017-06-05 02:02:04 +02:00
|
|
|
* Additional Terms 7.b and 7.c of GPLv3 apply 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.
|
|
|
|
* * Prohibiting misrepresentation of the origin of that material,
|
|
|
|
* or requiring that modified versions of such material be marked in
|
|
|
|
* reasonable ways as different from the original version.
|
2015-08-05 03:57:37 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "fs.h"
|
2016-04-11 05:15:44 +02:00
|
|
|
#include "memory.h"
|
2017-05-07 17:58:44 +02:00
|
|
|
#include "fmt.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"
|
2016-11-13 18:10:59 +01:00
|
|
|
#include "draw.h"
|
|
|
|
#include "utils.h"
|
2015-08-05 03:57:37 +02:00
|
|
|
#include "fatfs/ff.h"
|
2016-04-29 17:21:34 +02:00
|
|
|
#include "buttons.h"
|
2017-05-18 01:05:56 +02:00
|
|
|
#include "firm.h"
|
|
|
|
#include "crypto.h"
|
2018-05-26 12:25:24 +02:00
|
|
|
#include "strings.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-10-12 02:28:08 +02:00
|
|
|
static bool switchToMainDir(bool isSd)
|
2016-04-02 17:58:06 +02:00
|
|
|
{
|
2016-10-12 02:28:08 +02:00
|
|
|
const char *mainDir = isSd ? "/luma" : "/rw/luma";
|
|
|
|
|
|
|
|
switch(f_chdir(mainDir))
|
|
|
|
{
|
|
|
|
case FR_OK:
|
2016-11-15 20:18:28 +01:00
|
|
|
return true;
|
2016-10-12 02:28:08 +02:00
|
|
|
case FR_NO_PATH:
|
2017-08-28 02:40:05 +02:00
|
|
|
return f_mkdir(mainDir) == FR_OK && switchToMainDir(isSd);
|
2016-10-12 02:28:08 +02:00
|
|
|
default:
|
2016-11-15 20:18:28 +01:00
|
|
|
return false;
|
2016-10-12 02:28:08 +02:00
|
|
|
}
|
2015-08-05 03:57:37 +02:00
|
|
|
}
|
|
|
|
|
2016-10-12 02:28:08 +02:00
|
|
|
bool mountFs(bool isSd, bool switchToCtrNand)
|
2016-10-08 01:46:41 +02:00
|
|
|
{
|
2016-10-13 15:08:30 +02:00
|
|
|
return isSd ? f_mount(&sdFs, "0:", 1) == FR_OK && switchToMainDir(true) :
|
2016-10-12 02:28:08 +02:00
|
|
|
f_mount(&nandFs, "1:", 1) == FR_OK && (!switchToCtrNand || (f_chdrive("1:") == FR_OK && switchToMainDir(false)));
|
2016-10-08 01:46:41 +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;
|
2017-08-28 14:46:18 +02:00
|
|
|
FRESULT result = FR_OK;
|
2016-11-26 14:07:48 +01:00
|
|
|
u32 ret = 0;
|
2016-11-15 19:29:48 +01:00
|
|
|
|
2016-11-26 14:07:48 +01:00
|
|
|
if(f_open(&file, path, FA_READ) != FR_OK) return ret;
|
2015-08-05 03:57:37 +02:00
|
|
|
|
2016-11-15 19:29:48 +01:00
|
|
|
u32 size = f_size(&file);
|
|
|
|
if(dest == NULL) ret = size;
|
|
|
|
else if(size <= maxSize)
|
2017-08-28 14:46:18 +02:00
|
|
|
result = f_read(&file, dest, size, (unsigned int *)&ret);
|
|
|
|
result |= f_close(&file);
|
2015-08-05 03:57:37 +02:00
|
|
|
|
2017-08-28 14:46:18 +02:00
|
|
|
return result == FR_OK ? ret : 0;
|
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;
|
2018-05-24 00:55:38 +02:00
|
|
|
FRESULT result = FR_OK;
|
2015-08-05 03:57:37 +02:00
|
|
|
|
2016-10-12 02:28:08 +02:00
|
|
|
switch(f_open(&file, path, FA_WRITE | FA_OPEN_ALWAYS))
|
2016-04-28 16:11:25 +02:00
|
|
|
{
|
2016-10-12 02:28:08 +02:00
|
|
|
case FR_OK:
|
|
|
|
{
|
|
|
|
unsigned int written;
|
2017-08-28 02:40:05 +02:00
|
|
|
result = f_write(&file, buffer, size, &written);
|
|
|
|
if(result == FR_OK) result = f_truncate(&file);
|
|
|
|
result |= f_close(&file);
|
2016-07-18 22:42:55 +02:00
|
|
|
|
2017-08-28 02:40:05 +02:00
|
|
|
return result == FR_OK && (u32)written == size;
|
2016-10-12 02:28:08 +02:00
|
|
|
}
|
|
|
|
case FR_NO_PATH:
|
2016-10-12 02:45:49 +02:00
|
|
|
for(u32 i = 1; path[i] != 0; i++)
|
|
|
|
if(path[i] == '/')
|
|
|
|
{
|
|
|
|
char folder[i + 1];
|
|
|
|
memcpy(folder, path, i);
|
|
|
|
folder[i] = 0;
|
2017-08-28 02:40:05 +02:00
|
|
|
result = f_mkdir(folder);
|
2016-10-12 02:45:49 +02:00
|
|
|
}
|
2016-10-12 02:28:08 +02:00
|
|
|
|
2017-08-28 02:40:05 +02:00
|
|
|
return result == FR_OK && fileWrite(buffer, path, size);
|
2016-10-12 02:28:08 +02:00
|
|
|
default:
|
2016-11-15 19:29:48 +01:00
|
|
|
return false;
|
2016-08-30 16:56:19 +02:00
|
|
|
}
|
2016-07-18 22:42:55 +02:00
|
|
|
}
|
|
|
|
|
2017-08-28 02:40:05 +02:00
|
|
|
bool fileDelete(const char *path)
|
2016-08-28 15:50:11 +02:00
|
|
|
{
|
2017-08-28 02:40:05 +02:00
|
|
|
return f_unlink(path) == FR_OK;
|
2016-08-28 15:50:11 +02:00
|
|
|
}
|
|
|
|
|
2017-06-10 02:39:00 +02:00
|
|
|
bool findPayload(char *path, u32 pressed)
|
2016-04-02 17:58:06 +02:00
|
|
|
{
|
2017-06-10 02:39:00 +02:00
|
|
|
const char *pattern;
|
|
|
|
|
|
|
|
if(pressed & BUTTON_LEFT) pattern = PATTERN("left");
|
|
|
|
else if(pressed & BUTTON_RIGHT) pattern = PATTERN("right");
|
|
|
|
else if(pressed & BUTTON_UP) pattern = PATTERN("up");
|
|
|
|
else if(pressed & BUTTON_DOWN) pattern = PATTERN("down");
|
|
|
|
else if(pressed & BUTTON_START) pattern = PATTERN("start");
|
|
|
|
else if(pressed & BUTTON_B) pattern = PATTERN("b");
|
|
|
|
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 pattern = PATTERN("select");
|
2017-05-18 01:05:56 +02:00
|
|
|
|
2017-06-10 02:39:00 +02:00
|
|
|
DIR dir;
|
|
|
|
FILINFO info;
|
|
|
|
FRESULT result;
|
2017-05-18 01:05:56 +02:00
|
|
|
|
2017-06-10 02:39:00 +02:00
|
|
|
result = f_findfirst(&dir, &info, "payloads", pattern);
|
2017-05-18 01:05:56 +02:00
|
|
|
|
2017-06-10 02:39:00 +02:00
|
|
|
if(result != FR_OK) return false;
|
2016-03-05 23:27:02 +01:00
|
|
|
|
2017-06-10 02:39:00 +02:00
|
|
|
f_closedir(&dir);
|
2016-11-13 18:10:59 +01:00
|
|
|
|
2017-06-10 02:39:00 +02:00
|
|
|
if(!info.fname[0]) return false;
|
2017-05-20 00:45:05 +02:00
|
|
|
|
2017-06-10 02:39:00 +02:00
|
|
|
sprintf(path, "payloads/%s", info.fname);
|
2016-11-13 18:10:59 +01:00
|
|
|
|
2017-06-10 02:39:00 +02:00
|
|
|
return true;
|
2016-11-13 18:10:59 +01:00
|
|
|
}
|
|
|
|
|
2017-06-10 02:39:00 +02:00
|
|
|
bool payloadMenu(char *path)
|
2016-11-13 18:10:59 +01:00
|
|
|
{
|
|
|
|
DIR dir;
|
|
|
|
|
2017-06-10 02:39:00 +02:00
|
|
|
if(f_opendir(&dir, "payloads") != FR_OK) return false;
|
2016-11-13 18:10:59 +01:00
|
|
|
|
2016-11-15 19:29:48 +01:00
|
|
|
FILINFO info;
|
|
|
|
u32 payloadNum = 0;
|
|
|
|
char payloadList[20][49];
|
2016-04-29 14:28:37 +02:00
|
|
|
|
2016-11-15 19:29:48 +01:00
|
|
|
while(f_readdir(&dir, &info) == FR_OK && info.fname[0] != 0 && payloadNum < 20)
|
|
|
|
{
|
2016-12-01 00:51:07 +01:00
|
|
|
if(info.fname[0] == '.') continue;
|
2016-11-15 20:18:28 +01:00
|
|
|
|
2016-12-01 00:51:07 +01:00
|
|
|
u32 nameLength = strlen(info.fname);
|
2016-05-03 03:05:11 +02:00
|
|
|
|
2017-05-18 01:05:56 +02:00
|
|
|
if(nameLength < 6 || nameLength > 52) continue;
|
2016-12-01 00:51:07 +01:00
|
|
|
|
2017-05-18 01:05:56 +02:00
|
|
|
nameLength -= 5;
|
2016-12-01 00:51:07 +01:00
|
|
|
|
2017-05-18 01:05:56 +02:00
|
|
|
if(memcmp(info.fname + nameLength, ".firm", 5) != 0) continue;
|
2016-11-13 18:10:59 +01:00
|
|
|
|
2016-11-15 19:29:48 +01:00
|
|
|
memcpy(payloadList[payloadNum], info.fname, nameLength);
|
|
|
|
payloadList[payloadNum][nameLength] = 0;
|
|
|
|
payloadNum++;
|
|
|
|
}
|
2016-10-10 03:50:24 +02:00
|
|
|
|
2017-08-28 14:46:18 +02:00
|
|
|
if(f_closedir(&dir) != FR_OK || !payloadNum) return false;
|
2016-11-13 18:10:59 +01:00
|
|
|
|
2016-11-15 19:29:48 +01:00
|
|
|
u32 pressed = 0,
|
|
|
|
selectedPayload = 0;
|
2016-09-13 14:54:14 +02:00
|
|
|
|
2017-05-10 15:16:32 +02:00
|
|
|
if(payloadNum != 1)
|
2016-11-15 19:29:48 +01:00
|
|
|
{
|
2017-05-10 15:16:32 +02:00
|
|
|
initScreens();
|
2016-11-13 18:10:59 +01:00
|
|
|
|
2017-05-10 15:16:32 +02:00
|
|
|
drawString(true, 10, 10, COLOR_TITLE, "Luma3DS chainloader");
|
|
|
|
drawString(true, 10, 10 + SPACING_Y, COLOR_TITLE, "Press A to select, START to quit");
|
2016-11-13 22:13:24 +01:00
|
|
|
|
2017-05-10 15:16:32 +02:00
|
|
|
for(u32 i = 0, posY = 10 + 3 * SPACING_Y, color = COLOR_RED; i < payloadNum; i++, posY += SPACING_Y)
|
2016-11-15 19:29:48 +01:00
|
|
|
{
|
2017-05-10 15:16:32 +02:00
|
|
|
drawString(true, 10, posY, color, payloadList[i]);
|
|
|
|
if(color == COLOR_RED) color = COLOR_WHITE;
|
2016-09-13 14:54:14 +02:00
|
|
|
}
|
2016-11-15 19:29:48 +01:00
|
|
|
|
2017-05-10 15:16:32 +02:00
|
|
|
while(pressed != BUTTON_A && pressed != BUTTON_START)
|
|
|
|
{
|
|
|
|
do
|
|
|
|
{
|
2017-12-18 04:27:11 +01:00
|
|
|
pressed = waitInput(true) & MENU_BUTTONS;
|
2017-05-10 15:16:32 +02:00
|
|
|
}
|
2017-12-18 04:27:11 +01:00
|
|
|
while(!pressed);
|
2017-11-22 19:03:39 +01:00
|
|
|
|
2017-05-10 15:16:32 +02:00
|
|
|
u32 oldSelectedPayload = selectedPayload;
|
|
|
|
|
|
|
|
switch(pressed)
|
|
|
|
{
|
|
|
|
case BUTTON_UP:
|
|
|
|
selectedPayload = !selectedPayload ? payloadNum - 1 : selectedPayload - 1;
|
|
|
|
break;
|
|
|
|
case BUTTON_DOWN:
|
|
|
|
selectedPayload = selectedPayload == payloadNum - 1 ? 0 : selectedPayload + 1;
|
|
|
|
break;
|
|
|
|
case BUTTON_LEFT:
|
|
|
|
selectedPayload = 0;
|
|
|
|
break;
|
|
|
|
case BUTTON_RIGHT:
|
|
|
|
selectedPayload = payloadNum - 1;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(oldSelectedPayload == selectedPayload) continue;
|
|
|
|
|
|
|
|
drawString(true, 10, 10 + (3 + oldSelectedPayload) * SPACING_Y, COLOR_WHITE, payloadList[oldSelectedPayload]);
|
|
|
|
drawString(true, 10, 10 + (3 + selectedPayload) * SPACING_Y, COLOR_RED, payloadList[selectedPayload]);
|
|
|
|
}
|
2016-04-29 14:28:37 +02:00
|
|
|
}
|
2016-11-15 19:29:48 +01:00
|
|
|
|
2017-05-10 15:16:32 +02:00
|
|
|
if(pressed != BUTTON_START)
|
2016-11-15 19:29:48 +01:00
|
|
|
{
|
2017-05-18 01:05:56 +02:00
|
|
|
sprintf(path, "payloads/%s.firm", payloadList[selectedPayload]);
|
2017-06-10 02:39:00 +02:00
|
|
|
|
|
|
|
return true;
|
2016-11-15 19:29:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
while(HID_PAD & MENU_BUTTONS);
|
2016-11-29 20:11:30 +01:00
|
|
|
wait(2000ULL);
|
2017-06-10 02:39:00 +02:00
|
|
|
|
|
|
|
return false;
|
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
|
|
|
{
|
2017-08-16 18:02:35 +02:00
|
|
|
static const char *firmFolders[][2] = {{"00000002", "20000002"},
|
|
|
|
{"00000102", "20000102"},
|
|
|
|
{"00000202", "20000202"},
|
|
|
|
{"00000003", "20000003"},
|
|
|
|
{"00000001", "20000001"}};
|
2016-07-18 16:58:29 +02:00
|
|
|
|
2017-05-08 01:11:24 +02:00
|
|
|
char folderPath[35],
|
|
|
|
path[48];
|
|
|
|
|
2017-05-07 17:58:44 +02:00
|
|
|
sprintf(folderPath, "1:/title/00040138/%s/content", firmFolders[firmType][ISN3DS ? 1 : 0]);
|
2016-04-11 05:15:44 +02:00
|
|
|
|
|
|
|
DIR dir;
|
2016-07-19 20:03:35 +02:00
|
|
|
u32 firmVersion = 0xFFFFFFFF;
|
2016-04-11 05:15:44 +02:00
|
|
|
|
2017-05-07 17:58:44 +02:00
|
|
|
if(f_opendir(&dir, folderPath) != FR_OK) goto exit;
|
2016-11-15 19:29:48 +01:00
|
|
|
|
|
|
|
FILINFO info;
|
|
|
|
|
|
|
|
//Parse the target directory
|
|
|
|
while(f_readdir(&dir, &info) == FR_OK && info.fname[0] != 0)
|
2016-04-11 05:15:44 +02:00
|
|
|
{
|
2016-11-15 19:29:48 +01:00
|
|
|
//Not a cxi
|
2016-11-15 19:45:27 +01:00
|
|
|
if(info.fname[9] != 'a' || strlen(info.fname) != 12) continue;
|
2016-10-23 18:43:04 +02:00
|
|
|
|
2018-05-26 12:25:24 +02:00
|
|
|
u32 tempVersion = hexAtoi(info.altname, 8);
|
2016-10-07 14:27:30 +02:00
|
|
|
|
2016-11-15 19:29:48 +01:00
|
|
|
//Found an older cxi
|
|
|
|
if(tempVersion < firmVersion) firmVersion = tempVersion;
|
|
|
|
}
|
2016-10-07 14:27:30 +02:00
|
|
|
|
2017-08-28 14:46:18 +02:00
|
|
|
if(f_closedir(&dir) != FR_OK || firmVersion == 0xFFFFFFFF) goto exit;
|
2016-04-11 05:15:44 +02:00
|
|
|
|
2016-11-15 19:29:48 +01:00
|
|
|
//Complete the string with the .app name
|
2018-05-24 00:55:38 +02:00
|
|
|
sprintf(path, "%s/%08lx.app", folderPath, firmVersion);
|
2016-04-11 05:15:44 +02:00
|
|
|
|
2017-06-10 02:39:00 +02:00
|
|
|
if(fileRead(dest, path, 0x400000 + sizeof(Cxi) + 0x200) <= sizeof(Cxi) + 0x400) firmVersion = 0xFFFFFFFF;
|
2016-07-18 16:58:29 +02:00
|
|
|
|
2016-11-15 19:29:48 +01:00
|
|
|
exit:
|
2016-07-19 20:03:35 +02:00
|
|
|
return firmVersion;
|
2016-08-30 17:09:04 +02:00
|
|
|
}
|
|
|
|
|
2017-05-07 17:58:44 +02:00
|
|
|
void findDumpFile(const char *folderPath, char *fileName)
|
2016-08-30 17:09:04 +02:00
|
|
|
{
|
|
|
|
DIR dir;
|
2016-09-26 13:24:12 +02:00
|
|
|
FRESULT result;
|
2016-08-30 17:09:04 +02:00
|
|
|
|
2017-05-07 17:58:44 +02:00
|
|
|
for(u32 n = 0; n <= 99999999; n++)
|
2016-08-30 17:09:04 +02:00
|
|
|
{
|
2016-10-23 18:43:04 +02:00
|
|
|
FILINFO info;
|
|
|
|
|
2018-05-24 00:55:38 +02:00
|
|
|
sprintf(fileName, "crash_dump_%08lu.dmp", n);
|
2017-05-07 17:58:44 +02:00
|
|
|
result = f_findfirst(&dir, &info, folderPath, fileName);
|
2016-09-26 13:24:12 +02:00
|
|
|
|
2016-10-22 16:31:31 +02:00
|
|
|
if(result != FR_OK || !info.fname[0]) break;
|
2016-08-30 17:09:04 +02:00
|
|
|
}
|
|
|
|
|
2016-09-26 13:24:12 +02:00
|
|
|
if(result == FR_OK) f_closedir(&dir);
|
2017-05-07 17:58:44 +02:00
|
|
|
}
|