2015-08-05 03:57:37 +02:00
|
|
|
/*
|
|
|
|
* firm.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 "firm.h"
|
|
|
|
#include "patches.h"
|
|
|
|
#include "memory.h"
|
|
|
|
#include "fs.h"
|
|
|
|
#include "emunand.h"
|
2015-08-21 20:11:23 +02:00
|
|
|
#include "crypto.h"
|
2016-02-29 16:28:43 +01:00
|
|
|
#include "draw.h"
|
2016-03-23 02:27:53 +01:00
|
|
|
#include "screeninit.h"
|
2016-03-08 15:13:55 +01:00
|
|
|
#include "loader.h"
|
2016-03-23 03:45:32 +01:00
|
|
|
#include "utils.h"
|
2016-03-23 02:27:53 +01:00
|
|
|
#include "buttons.h"
|
2016-03-23 14:59:52 +01:00
|
|
|
#include "../build/patches.h"
|
2016-03-23 02:27:53 +01:00
|
|
|
|
|
|
|
//FIRM patches version
|
|
|
|
#define PATCH_VER 1
|
2015-08-05 03:57:37 +02:00
|
|
|
|
2016-03-10 14:58:11 +01:00
|
|
|
static firmHeader *const firmLocation = (firmHeader *)0x24000000;
|
|
|
|
static const firmSectionHeader *section;
|
2016-03-23 02:27:53 +01:00
|
|
|
static u8 *arm9Section;
|
|
|
|
static const char *patchedFirms[] = { "/aurei/patched_firmware_sys.bin",
|
|
|
|
"/aurei/patched_firmware_emu.bin",
|
|
|
|
"/aurei/patched_firmware_em2.bin",
|
|
|
|
"/aurei/patched_firmware90.bin" };
|
2016-03-21 18:56:41 +01:00
|
|
|
static u32 firmSize,
|
2016-03-10 14:58:11 +01:00
|
|
|
mode = 1,
|
|
|
|
console = 1,
|
|
|
|
emuNAND = 0,
|
|
|
|
a9lhSetup = 0,
|
2016-03-23 02:27:53 +01:00
|
|
|
usePatchedFirm = 0,
|
|
|
|
selectedFirm = 0;
|
2015-08-05 03:57:37 +02:00
|
|
|
|
2016-02-25 20:19:20 +01:00
|
|
|
void setupCFW(void){
|
2016-02-08 03:37:03 +01:00
|
|
|
|
2016-03-13 16:48:57 +01:00
|
|
|
//Determine if booting with A9LH
|
|
|
|
u32 a9lhBoot = (PDN_SPI_CNT == 0x0) ? 1 : 0;
|
|
|
|
//Retrieve the last booted FIRM
|
|
|
|
u8 previousFirm = CFG_BOOTENV;
|
2016-02-19 21:32:07 +01:00
|
|
|
//Detect the console being used
|
2016-02-08 03:37:03 +01:00
|
|
|
if(PDN_MPCORE_CFG == 1) console = 0;
|
2016-02-19 21:32:07 +01:00
|
|
|
//Get pressed buttons
|
2016-03-21 18:56:41 +01:00
|
|
|
u16 pressed = HID_PAD;
|
2016-03-23 02:27:53 +01:00
|
|
|
u32 updatedSys = 0;
|
|
|
|
|
|
|
|
//Attempt to read the configuration file
|
|
|
|
const char configPath[] = "aurei/config.bin";
|
|
|
|
u16 config = 0;
|
|
|
|
u32 needConfig = fileRead((u8 *)&config, configPath, 2) ? 1 : 2;
|
2016-02-25 20:19:20 +01:00
|
|
|
|
2016-03-05 20:22:31 +01:00
|
|
|
//Determine if A9LH is installed
|
2016-03-23 02:27:53 +01:00
|
|
|
if(a9lhBoot || (config >> 2) & 0x1){
|
2016-02-19 21:32:07 +01:00
|
|
|
a9lhSetup = 1;
|
2016-03-23 15:48:59 +01:00
|
|
|
//Check setting for > 9.2 sysNAND
|
2016-03-23 02:27:53 +01:00
|
|
|
updatedSys = config & 0x1;
|
2016-02-19 21:32:07 +01:00
|
|
|
}
|
|
|
|
|
2016-03-23 02:27:53 +01:00
|
|
|
/* If booting with A9LH, it's a MCU reboot and a previous configuration exists,
|
|
|
|
try to force boot options */
|
|
|
|
if(a9lhBoot && previousFirm && needConfig == 1){
|
2016-03-05 15:35:06 +01:00
|
|
|
//Always force a sysNAND boot when quitting AGB_FIRM
|
2016-03-17 00:08:13 +01:00
|
|
|
if(previousFirm == 0x7){
|
2016-03-23 02:27:53 +01:00
|
|
|
if(!updatedSys) mode = (config >> 8) & 0x1;
|
|
|
|
needConfig = 0;
|
2016-03-05 20:22:31 +01:00
|
|
|
//Else, force the last used boot options unless A is pressed
|
2016-03-17 00:08:13 +01:00
|
|
|
} else if(!(pressed & BUTTON_A)){
|
2016-03-23 02:27:53 +01:00
|
|
|
mode = (config >> 8) & 0x1;
|
|
|
|
emuNAND = (config >> 9) & 0x1;
|
|
|
|
needConfig = 0;
|
2016-03-05 15:35:06 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-23 02:27:53 +01:00
|
|
|
if(needConfig){
|
2016-03-05 15:35:06 +01:00
|
|
|
|
2016-03-08 15:13:55 +01:00
|
|
|
//If L and R are pressed, chainload an external payload
|
2016-03-23 15:26:52 +01:00
|
|
|
if((pressed & BUTTON_L1R1) == BUTTON_L1R1) loadPayload();
|
2016-03-08 15:13:55 +01:00
|
|
|
|
2016-03-23 02:27:53 +01:00
|
|
|
//If no configuration file exists or SELECT is held, load configuration menu
|
2016-03-23 03:45:32 +01:00
|
|
|
if(needConfig == 2 || (pressed & BUTTON_SELECT))
|
2016-03-23 02:27:53 +01:00
|
|
|
configureCFW(configPath);
|
|
|
|
|
|
|
|
//If screens are inited, load splash screen
|
2016-03-13 16:48:57 +01:00
|
|
|
if(PDN_GPU_CNT != 0x1) loadSplash();
|
2016-03-09 14:59:20 +01:00
|
|
|
|
2016-03-23 15:48:59 +01:00
|
|
|
/* If L is pressed, and on an updated sysNAND setup the SAFE MODE combo
|
2016-03-05 15:35:06 +01:00
|
|
|
is not pressed, boot 9.0 FIRM */
|
|
|
|
if((pressed & BUTTON_L1) && !(updatedSys && pressed == SAFEMODE)) mode = 0;
|
2016-03-04 23:43:22 +01:00
|
|
|
|
2016-03-23 15:48:59 +01:00
|
|
|
/* If L or R aren't pressed on a 9.0/9.2 sysNAND, or the 9.0 FIRM is selected
|
|
|
|
or R is pressed on a > 9.2 sysNAND, boot emuNAND */
|
2016-03-05 15:35:06 +01:00
|
|
|
if((updatedSys && (!mode || ((pressed & BUTTON_R1) && pressed != SAFEMODE))) ||
|
2016-03-17 00:08:13 +01:00
|
|
|
(!updatedSys && mode && !(pressed & BUTTON_R1))){
|
|
|
|
//If not 9.0 FIRM and B is pressed, attempt booting the second emuNAND
|
|
|
|
if(mode && (pressed & BUTTON_B)) emuNAND = 2;
|
|
|
|
else emuNAND = 1;
|
|
|
|
}
|
2016-03-05 15:35:06 +01:00
|
|
|
|
2016-03-23 02:27:53 +01:00
|
|
|
/* If tha FIRM patches version is different or user switched to/from A9LH,
|
|
|
|
and "Use pre-patched FIRMs" is set, delete all patched FIRMs */
|
2016-03-23 14:59:52 +01:00
|
|
|
u16 bootConfig = (PATCH_VER << 11) | (a9lhSetup << 10);
|
2016-03-23 02:27:53 +01:00
|
|
|
if ((config >> 1) & 0x1 && bootConfig != (config & 0xFC00))
|
|
|
|
deleteFirms(patchedFirms, sizeof(patchedFirms) / sizeof(char *));
|
|
|
|
|
|
|
|
//We also need to remember the used boot mode on A9LH
|
|
|
|
if(a9lhBoot) bootConfig |= (mode << 8) | (emuNAND << 9);
|
|
|
|
|
2016-03-23 14:59:52 +01:00
|
|
|
//If the boot configuration is different from previously, overwrite it
|
2016-03-23 02:27:53 +01:00
|
|
|
if(bootConfig != (config & 0xFF00)){
|
2016-03-23 04:27:07 +01:00
|
|
|
//Preserve user settings (first byte)
|
2016-03-23 02:27:53 +01:00
|
|
|
u16 tempConfig = ((config & 0xFF) | bootConfig);
|
|
|
|
fileWrite((u8 *)&tempConfig, configPath, 2);
|
2016-03-05 15:35:06 +01:00
|
|
|
}
|
|
|
|
}
|
2016-03-04 23:43:22 +01:00
|
|
|
|
2016-03-23 02:27:53 +01:00
|
|
|
if(mode) selectedFirm = emuNAND ? (emuNAND == 1 ? 2 : 3) : 1;
|
2016-03-04 23:43:22 +01:00
|
|
|
|
|
|
|
//Skip decrypting and patching FIRM
|
2016-03-23 02:27:53 +01:00
|
|
|
if((config >> 1) & 0x1){
|
|
|
|
//Only needed with this setting
|
|
|
|
if(!mode) selectedFirm = 4;
|
|
|
|
if(fileExists(patchedFirms[selectedFirm - 1])) usePatchedFirm = 1;
|
2016-03-04 23:43:22 +01:00
|
|
|
}
|
2016-02-25 20:19:20 +01:00
|
|
|
}
|
|
|
|
|
2016-03-21 18:56:41 +01:00
|
|
|
//Load FIRM into FCRAM
|
2016-03-23 03:45:32 +01:00
|
|
|
void loadFirm(void){
|
2016-02-19 01:29:53 +01:00
|
|
|
|
2016-03-04 23:43:22 +01:00
|
|
|
//If not using an A9LH setup or the patched FIRM, load 9.0 FIRM from NAND
|
|
|
|
if(!usePatchedFirm && !a9lhSetup && !mode){
|
2016-02-08 03:37:03 +01:00
|
|
|
//Read FIRM from NAND and write to FCRAM
|
2016-02-11 00:45:24 +01:00
|
|
|
firmSize = console ? 0xF2000 : 0xE9000;
|
2016-03-06 18:29:47 +01:00
|
|
|
nandFirm0((u8 *)firmLocation, firmSize, console);
|
2016-02-25 20:19:20 +01:00
|
|
|
//Check for correct decryption
|
2016-03-23 03:45:32 +01:00
|
|
|
if(memcmp(firmLocation, "FIRM", 4) != 0)
|
2016-03-23 04:06:21 +01:00
|
|
|
error("Couldn't decrypt NAND FIRM0 (O3DS not on 9.x?)");
|
2016-02-08 03:37:03 +01:00
|
|
|
}
|
2016-02-19 04:53:19 +01:00
|
|
|
//Load FIRM from SD
|
2016-02-08 03:37:03 +01:00
|
|
|
else{
|
2016-03-23 02:27:53 +01:00
|
|
|
const char *path = usePatchedFirm ? patchedFirms[selectedFirm - 1] :
|
2016-03-20 01:00:45 +01:00
|
|
|
(mode ? "/aurei/firmware.bin" : "/aurei/firmware90.bin");
|
2016-03-04 23:43:22 +01:00
|
|
|
firmSize = fileSize(path);
|
2016-03-23 04:06:21 +01:00
|
|
|
if(!firmSize) error("aurei/firmware(90).bin doesn't exist");
|
2016-03-06 18:29:47 +01:00
|
|
|
fileRead((u8 *)firmLocation, path, firmSize);
|
2016-02-08 03:37:03 +01:00
|
|
|
}
|
2016-02-25 20:19:20 +01:00
|
|
|
|
|
|
|
section = firmLocation->section;
|
|
|
|
|
|
|
|
//Check that the loaded FIRM matches the console
|
2016-03-23 03:45:32 +01:00
|
|
|
if((((u32)section[2].address >> 8) & 0xFF) != (console ? 0x60 : 0x68))
|
2016-03-23 04:06:21 +01:00
|
|
|
error("aurei/firmware(90).bin doesn't match this\nconsole, or it's encrypted");
|
2016-02-08 03:37:03 +01:00
|
|
|
|
2016-03-21 18:56:41 +01:00
|
|
|
arm9Section = (u8 *)firmLocation + section[2].offset;
|
|
|
|
|
2016-03-21 19:20:26 +01:00
|
|
|
if(console && !usePatchedFirm) decryptArm9Bin(arm9Section, mode);
|
2015-08-05 11:54:00 +02:00
|
|
|
}
|
|
|
|
|
2016-03-21 18:56:41 +01:00
|
|
|
//NAND redirection
|
2016-03-23 03:45:32 +01:00
|
|
|
static void loadEmu(u8 *proc9Offset){
|
2016-02-08 03:37:03 +01:00
|
|
|
|
2016-03-14 14:51:30 +01:00
|
|
|
u32 emuOffset = 1,
|
|
|
|
emuHeader = 1,
|
2016-03-11 15:08:05 +01:00
|
|
|
emuRead,
|
2016-03-21 18:56:41 +01:00
|
|
|
emuWrite;
|
2016-02-08 03:37:03 +01:00
|
|
|
|
2016-03-21 18:56:41 +01:00
|
|
|
//Look for emuNAND
|
2016-03-17 00:08:13 +01:00
|
|
|
getEmunandSect(&emuOffset, &emuHeader, emuNAND);
|
2016-03-21 18:56:41 +01:00
|
|
|
|
2016-03-21 04:39:00 +01:00
|
|
|
//No emuNAND detected
|
2016-03-23 03:45:32 +01:00
|
|
|
if(!emuHeader) error("No emuNAND has been detected");
|
2016-03-21 18:56:41 +01:00
|
|
|
|
2016-03-23 15:48:59 +01:00
|
|
|
//Copy the emuNAND patch
|
|
|
|
void *emuCodeOffset = getEmuCode(arm9Section, section[2].size, proc9Offset);
|
|
|
|
memcpy(emuCodeOffset, emunand, emunand_size);
|
|
|
|
|
|
|
|
//Add the data of the found emuNAND
|
2016-03-23 14:59:52 +01:00
|
|
|
u32 *pos_offset = (u32 *)memsearch(emuCodeOffset, "NAND", emunand_size, 4);
|
|
|
|
u32 *pos_header = (u32 *)memsearch(emuCodeOffset, "NCSD", emunand_size, 4);
|
2016-02-08 03:37:03 +01:00
|
|
|
*pos_offset = emuOffset;
|
|
|
|
*pos_header = emuHeader;
|
2016-02-25 20:19:20 +01:00
|
|
|
|
2016-03-23 15:48:59 +01:00
|
|
|
//Find and add the SDMMC struct
|
2016-03-23 14:59:52 +01:00
|
|
|
u32 *pos_sdmmc = (u32 *)memsearch(emuCodeOffset, "SDMC", emunand_size, 4);
|
2016-03-21 18:56:41 +01:00
|
|
|
*pos_sdmmc = getSDMMC(arm9Section, section[2].size);
|
|
|
|
|
2016-03-21 03:20:15 +01:00
|
|
|
//Calculate offset for the hooks
|
2016-03-21 18:56:41 +01:00
|
|
|
*(u32 *)(nandRedir + 4) = (u32)emuCodeOffset - (u32)firmLocation -
|
2016-03-21 03:20:15 +01:00
|
|
|
section[2].offset + (u32)section[2].address;
|
2016-02-08 03:37:03 +01:00
|
|
|
|
2015-08-07 09:01:42 +02:00
|
|
|
//Add emunand hooks
|
2016-03-21 18:56:41 +01:00
|
|
|
getEmuRW(arm9Section, section[2].size, &emuRead, &emuWrite);
|
2016-03-06 16:24:42 +01:00
|
|
|
memcpy((void *)emuRead, nandRedir, sizeof(nandRedir));
|
|
|
|
memcpy((void *)emuWrite, nandRedir, sizeof(nandRedir));
|
2016-02-08 03:37:03 +01:00
|
|
|
|
2016-02-06 00:54:24 +01:00
|
|
|
//Set MPU for emu code region
|
2016-03-21 18:56:41 +01:00
|
|
|
void *mpuOffset = getMPU(arm9Section, section[2].size);
|
|
|
|
memcpy(mpuOffset, mpu, sizeof(mpu));
|
2015-08-07 09:01:42 +02:00
|
|
|
}
|
|
|
|
|
2015-08-21 20:11:23 +02:00
|
|
|
//Patches
|
2016-03-23 03:45:32 +01:00
|
|
|
void patchFirm(void){
|
2016-03-05 15:35:06 +01:00
|
|
|
|
|
|
|
//Skip patching
|
2016-03-23 03:45:32 +01:00
|
|
|
if(usePatchedFirm) return;
|
2016-02-08 03:37:03 +01:00
|
|
|
|
2016-03-21 18:56:41 +01:00
|
|
|
if(mode || emuNAND){
|
|
|
|
//Find the Process9 NCCH location
|
|
|
|
u8 *proc9Offset = getProc9(arm9Section, section[2].size);
|
|
|
|
|
|
|
|
//Apply emuNAND patches
|
2016-03-23 03:45:32 +01:00
|
|
|
if(emuNAND) loadEmu(proc9Offset);
|
2016-03-21 18:56:41 +01:00
|
|
|
|
|
|
|
//Patch FIRM reboots, not on 9.0 FIRM as it breaks firmlaunchhax
|
|
|
|
if(mode){
|
|
|
|
//Read reboot code from SD
|
2016-03-23 15:16:40 +01:00
|
|
|
void *rebootOffset = getReboot(arm9Section, section[2].size);
|
2016-03-23 14:59:52 +01:00
|
|
|
memcpy(rebootOffset, reboot, reboot_size);
|
2016-03-21 18:56:41 +01:00
|
|
|
|
|
|
|
//Calculate the fOpen offset and put it in the right location
|
2016-03-23 14:59:52 +01:00
|
|
|
u32 *pos_fopen = (u32 *)memsearch(rebootOffset, "OPEN", reboot_size, 4);
|
2016-03-21 18:56:41 +01:00
|
|
|
*pos_fopen = getfOpen(arm9Section, section[2].size, proc9Offset);
|
|
|
|
|
|
|
|
//Patch path for emuNAND-patched FIRM
|
|
|
|
if(emuNAND){
|
2016-03-23 14:59:52 +01:00
|
|
|
void *pos_path = memsearch(rebootOffset, L"sy", reboot_size, 4);
|
2016-03-21 18:56:41 +01:00
|
|
|
memcpy(pos_path, emuNAND == 1 ? L"emu" : L"em2", 5);
|
|
|
|
}
|
|
|
|
}
|
2016-02-19 21:32:07 +01:00
|
|
|
}
|
2016-03-21 18:56:41 +01:00
|
|
|
|
|
|
|
if(a9lhSetup && !emuNAND){
|
2016-03-23 15:48:59 +01:00
|
|
|
//Patch FIRM partitions writes on sysNAND to protect A9LH
|
2016-03-23 02:27:53 +01:00
|
|
|
void *writeOffset = getFirmWrite(arm9Section, section[2].size);
|
|
|
|
memcpy(writeOffset, writeBlock, sizeof(writeBlock));
|
2016-02-19 21:32:07 +01:00
|
|
|
}
|
2016-02-08 03:37:03 +01:00
|
|
|
|
2016-02-25 20:19:20 +01:00
|
|
|
//Disable signature checks
|
2016-03-11 15:08:05 +01:00
|
|
|
u32 sigOffset,
|
|
|
|
sigOffset2;
|
2016-02-08 23:46:01 +01:00
|
|
|
|
2016-02-08 03:37:03 +01:00
|
|
|
getSignatures(firmLocation, firmSize, &sigOffset, &sigOffset2);
|
2016-03-06 16:24:42 +01:00
|
|
|
memcpy((void *)sigOffset, sigPat1, sizeof(sigPat1));
|
|
|
|
memcpy((void *)sigOffset2, sigPat2, sizeof(sigPat2));
|
2016-02-08 03:37:03 +01:00
|
|
|
|
2016-02-26 02:41:47 +01:00
|
|
|
//Patch ARM9 entrypoint on N3DS to skip arm9loader
|
2016-03-11 15:08:05 +01:00
|
|
|
if(console)
|
|
|
|
firmLocation->arm9Entry = (u8 *)0x801B01C;
|
2016-02-26 02:41:47 +01:00
|
|
|
|
2016-03-04 23:43:22 +01:00
|
|
|
//Write patched FIRM to SD if needed
|
2016-03-23 02:27:53 +01:00
|
|
|
if(selectedFirm)
|
2016-03-23 03:45:32 +01:00
|
|
|
if(!fileWrite((u8 *)firmLocation, patchedFirms[selectedFirm - 1], firmSize))
|
|
|
|
error("Couldn't write the patched FIRM (no free space?)");
|
2015-08-05 03:57:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void launchFirm(void){
|
2016-02-08 03:37:03 +01:00
|
|
|
|
2016-03-21 18:56:41 +01:00
|
|
|
if(console && mode) setKeyXs(arm9Section);
|
2016-03-04 23:43:22 +01:00
|
|
|
|
2015-08-05 03:57:37 +02:00
|
|
|
//Copy firm partitions to respective memory locations
|
2016-03-06 19:05:41 +01:00
|
|
|
memcpy(section[0].address, (u8 *)firmLocation + section[0].offset, section[0].size);
|
|
|
|
memcpy(section[1].address, (u8 *)firmLocation + section[1].offset, section[1].size);
|
2016-03-21 18:56:41 +01:00
|
|
|
memcpy(section[2].address, arm9Section, section[2].size);
|
2016-02-29 16:28:43 +01:00
|
|
|
|
2016-03-23 02:27:53 +01:00
|
|
|
//Fixes N3DS 3D
|
|
|
|
deinitScreens();
|
2016-03-11 15:08:05 +01:00
|
|
|
|
2016-03-23 02:27:53 +01:00
|
|
|
//Set ARM11 kernel entrypoint
|
|
|
|
*(vu32 *)0x1FFFFFF8 = (u32)firmLocation->arm11Entry;
|
2016-03-11 15:08:05 +01:00
|
|
|
|
2016-03-23 02:27:53 +01:00
|
|
|
//Final jump to arm9 kernel
|
2016-02-26 02:41:47 +01:00
|
|
|
((void (*)())firmLocation->arm9Entry)();
|
2015-08-05 03:57:37 +02:00
|
|
|
}
|