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"
|
2016-04-05 05:27:28 +02:00
|
|
|
#include "config.h"
|
|
|
|
#include "utils.h"
|
|
|
|
#include "fs.h"
|
2015-08-05 03:57:37 +02:00
|
|
|
#include "patches.h"
|
|
|
|
#include "memory.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 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
|
2016-04-05 02:29:36 +02:00
|
|
|
#define PATCH_VER 4
|
2015-08-05 03:57:37 +02:00
|
|
|
|
2016-03-27 19:19:35 +02:00
|
|
|
static firmHeader *const firm = (firmHeader *)0x24000000;
|
2016-03-10 14:58:11 +01:00
|
|
|
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",
|
2016-04-03 17:56:09 +02:00
|
|
|
"/aurei/patched_firmware90.bin",
|
|
|
|
"/aurei/patched_firmware_twl.bin",
|
|
|
|
"/aurei/patched_firmware_agb.bin" };
|
2016-04-05 05:27:28 +02:00
|
|
|
u32 config;
|
2016-03-21 18:56:41 +01:00
|
|
|
static u32 firmSize,
|
2016-03-27 16:26:09 +02:00
|
|
|
console,
|
|
|
|
mode,
|
|
|
|
emuNAND,
|
|
|
|
a9lhSetup,
|
|
|
|
selectedFirm,
|
2016-03-31 15:57:02 +02:00
|
|
|
usePatchedFirm,
|
|
|
|
emuOffset,
|
2016-04-05 02:20:05 +02:00
|
|
|
emuHeader;
|
2015-08-05 03:57:37 +02:00
|
|
|
|
2016-04-02 17:58:06 +02:00
|
|
|
void setupCFW(void)
|
|
|
|
{
|
2016-03-13 16:48:57 +01:00
|
|
|
//Determine if booting with A9LH
|
2016-04-02 17:58:06 +02:00
|
|
|
u32 a9lhBoot = !PDN_SPI_CNT ? 1 : 0;
|
|
|
|
|
2016-03-13 16:48:57 +01:00
|
|
|
//Retrieve the last booted FIRM
|
2016-04-02 22:02:08 +02:00
|
|
|
u32 previousFirm = CFG_BOOTENV;
|
2016-04-02 17:58:06 +02:00
|
|
|
|
2016-02-19 21:32:07 +01:00
|
|
|
//Detect the console being used
|
2016-03-27 16:26:09 +02:00
|
|
|
console = (PDN_MPCORE_CFG == 1) ? 0 : 1;
|
2016-04-02 17:58:06 +02:00
|
|
|
|
2016-02-19 21:32:07 +01:00
|
|
|
//Get pressed buttons
|
2016-04-02 22:02:08 +02:00
|
|
|
u32 pressed = HID_PAD;
|
2016-03-23 02:27:53 +01:00
|
|
|
|
|
|
|
//Attempt to read the configuration file
|
2016-04-03 18:13:05 +02:00
|
|
|
const char configPath[] = "/aurei/config.bin";
|
2016-04-04 01:08:42 +02:00
|
|
|
u32 needConfig;
|
|
|
|
if(fileRead(&config, configPath, 3)) needConfig = 1;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
config = 0;
|
|
|
|
needConfig = 2;
|
|
|
|
}
|
2016-02-25 20:19:20 +01:00
|
|
|
|
2016-03-27 16:26:09 +02:00
|
|
|
//Determine if A9LH is installed and the user has an updated sysNAND
|
|
|
|
u32 updatedSys;
|
|
|
|
|
2016-04-06 14:51:07 +02:00
|
|
|
if(a9lhBoot || CONFIG(2, 1))
|
2016-04-02 17:58:06 +02:00
|
|
|
{
|
2016-03-24 17:27:02 +01:00
|
|
|
if(pressed == SAFE_MODE)
|
|
|
|
error("Using Safe Mode would brick you, or remove A9LH!");
|
|
|
|
|
2016-02-19 21:32:07 +01:00
|
|
|
a9lhSetup = 1;
|
2016-04-02 17:58:06 +02:00
|
|
|
|
2016-03-23 15:48:59 +01:00
|
|
|
//Check setting for > 9.2 sysNAND
|
2016-04-06 14:51:07 +02:00
|
|
|
updatedSys = CONFIG(0, 1);
|
2016-04-02 17:58:06 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-03-27 16:26:09 +02:00
|
|
|
a9lhSetup = 0;
|
|
|
|
updatedSys = 0;
|
2016-02-19 21:32:07 +01:00
|
|
|
}
|
|
|
|
|
2016-03-31 18:20:36 +02:00
|
|
|
u32 tempConfig = (PATCH_VER << 17) | (a9lhSetup << 16);
|
|
|
|
|
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 */
|
2016-04-02 17:58:06 +02:00
|
|
|
if(a9lhBoot && previousFirm && needConfig == 1)
|
|
|
|
{
|
2016-03-05 15:35:06 +01:00
|
|
|
//Always force a sysNAND boot when quitting AGB_FIRM
|
2016-04-02 17:58:06 +02:00
|
|
|
if(previousFirm == 7)
|
|
|
|
{
|
2016-04-06 14:51:07 +02:00
|
|
|
mode = updatedSys ? 1 : CONFIG(12, 1);
|
2016-03-27 16:26:09 +02:00
|
|
|
emuNAND = 0;
|
2016-04-02 17:58:06 +02:00
|
|
|
needConfig = 0;
|
|
|
|
|
2016-03-31 16:04:12 +02:00
|
|
|
//Flag to prevent multiple boot options-forcing
|
2016-03-31 15:57:02 +02:00
|
|
|
tempConfig |= 1 << 15;
|
2016-04-02 17:58:06 +02:00
|
|
|
}
|
2016-04-04 18:19:00 +02:00
|
|
|
/* Else, force the last used boot options unless a payload button or A/L/R are pressed
|
2016-03-31 16:04:12 +02:00
|
|
|
or the no-forcing flag is set */
|
2016-04-06 14:51:07 +02:00
|
|
|
else if(!(pressed & OVERRIDE_BUTTONS) && !CONFIG(15, 1))
|
2016-04-02 17:58:06 +02:00
|
|
|
{
|
2016-04-06 14:51:07 +02:00
|
|
|
mode = CONFIG(12, 1);
|
|
|
|
emuNAND = CONFIG(13, 3);
|
2016-03-23 02:27:53 +01:00
|
|
|
needConfig = 0;
|
2016-03-05 15:35:06 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-02 17:58:06 +02:00
|
|
|
//Boot options aren't being forced
|
|
|
|
if(needConfig)
|
|
|
|
{
|
2016-04-04 18:19:00 +02:00
|
|
|
/* If L and R/Select or one of the single payload buttons are pressed and, if not using A9LH,
|
2016-03-24 02:40:43 +01:00
|
|
|
the Safe Mode combo is not pressed, chainload an external payload */
|
2016-04-04 18:07:13 +02:00
|
|
|
if(((pressed & SINGLE_PAYLOAD_BUTTONS) || ((pressed & BUTTON_L1) && (pressed & L_PAYLOAD_BUTTONS)))
|
|
|
|
&& pressed != SAFE_MODE)
|
2016-04-02 17:58:06 +02:00
|
|
|
loadPayload();
|
2016-03-24 17:05:19 +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-04-04 01:08:42 +02:00
|
|
|
configureCFW(configPath, patchedFirms);
|
2016-03-23 02:27:53 +01:00
|
|
|
|
2016-04-05 23:01:46 +02:00
|
|
|
//If screens are inited or the corresponding option is set, load splash screen
|
2016-04-06 14:51:07 +02:00
|
|
|
if(PDN_GPU_CNT != 1 || CONFIG(7, 1)) loadSplash();
|
2016-03-09 14:59:20 +01:00
|
|
|
|
2016-04-02 17:58:06 +02:00
|
|
|
/* If L is pressed, or L or R are not pressed when it is the default FIRM,
|
|
|
|
boot 9.0 FIRM */
|
2016-04-06 14:51:07 +02:00
|
|
|
mode = CONFIG(3, 1) ? ((!(pressed & BUTTON_L1R1)) ? 0 : 1) :
|
|
|
|
((pressed & BUTTON_L1) ? 0 : 1);
|
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-04-02 17:58:06 +02:00
|
|
|
if((updatedSys && (!mode || (pressed & BUTTON_R1))) || (!updatedSys && mode && !(pressed & BUTTON_R1)))
|
|
|
|
{
|
2016-04-04 18:07:13 +02:00
|
|
|
/* If not 9.0 FIRM and the second emuNAND is set as default and B isn't pressed, or vice-versa,
|
|
|
|
attempt to boot it */
|
2016-04-06 14:51:07 +02:00
|
|
|
emuNAND = (mode && ((!(pressed & BUTTON_B)) == CONFIG(4, 1))) ? 2 : 1;
|
2016-04-02 18:48:31 +02:00
|
|
|
}
|
|
|
|
else emuNAND = 0;
|
2016-03-05 15:35:06 +01:00
|
|
|
|
2016-03-31 15:57:02 +02:00
|
|
|
/* If tha FIRM patches version is different or user switched to/from A9LH,
|
|
|
|
delete all patched FIRMs */
|
|
|
|
if((tempConfig & 0xFF0000) != (config & 0xFF0000))
|
|
|
|
deleteFirms(patchedFirms, sizeof(patchedFirms) / sizeof(char *));
|
|
|
|
}
|
2016-03-31 01:38:28 +02:00
|
|
|
|
2016-04-06 14:51:07 +02:00
|
|
|
u32 usePatchedFirmSet = CONFIG(1, 1);
|
2016-03-31 15:57:02 +02:00
|
|
|
|
2016-04-02 17:58:06 +02:00
|
|
|
while(1)
|
|
|
|
{
|
2016-03-31 15:57:02 +02:00
|
|
|
/* Determine which patched FIRM we need to write or attempt to use (if any).
|
|
|
|
Patched 9.0 FIRM is only needed if "Use pre-patched FIRMs" is set */
|
|
|
|
selectedFirm = mode ? (emuNAND ? (emuNAND == 1 ? 2 : 3) : 1) :
|
|
|
|
(usePatchedFirmSet ? 4 : 0);
|
|
|
|
|
|
|
|
//If "Use pre-patched FIRMs" is set and the appropriate FIRM exists, use it
|
|
|
|
if(usePatchedFirmSet && fileExists(patchedFirms[selectedFirm - 1]))
|
|
|
|
usePatchedFirm = 1;
|
2016-04-02 17:58:06 +02:00
|
|
|
|
2016-04-04 18:07:13 +02:00
|
|
|
/* If the user is booting EmuNAND but the chosen one is not found,
|
|
|
|
force 9.6/10.x FIRM and re-detect the patched FIRMs */
|
|
|
|
else if(emuNAND && !getEmunandSect(&emuOffset, &emuHeader, &emuNAND))
|
2016-04-02 17:58:06 +02:00
|
|
|
{
|
2016-04-04 18:07:13 +02:00
|
|
|
mode = 1;
|
|
|
|
continue;
|
2016-03-05 15:35:06 +01:00
|
|
|
}
|
2016-04-04 18:19:00 +02:00
|
|
|
|
2016-03-31 15:57:02 +02:00
|
|
|
break;
|
2016-03-05 15:35:06 +01:00
|
|
|
}
|
2016-03-04 23:43:22 +01:00
|
|
|
|
2016-03-31 15:57:02 +02:00
|
|
|
tempConfig |= (emuNAND << 13) | (mode << 12);
|
2016-03-04 23:43:22 +01:00
|
|
|
|
2016-04-05 23:01:46 +02:00
|
|
|
/* If the boot configuration is different from previously, overwrite it.
|
|
|
|
Just the no-forcing flag being set is not enough */
|
|
|
|
if((tempConfig & 0xFF7000) != (config & 0xFFF000))
|
2016-04-02 17:58:06 +02:00
|
|
|
{
|
2016-03-31 15:57:02 +02:00
|
|
|
//Preserve user settings (first 12 bits)
|
|
|
|
tempConfig |= config & 0xFFF;
|
2016-04-02 17:58:06 +02:00
|
|
|
|
2016-03-31 15:57:02 +02:00
|
|
|
fileWrite(&tempConfig, configPath, 3);
|
|
|
|
}
|
2016-02-25 20:19:20 +01:00
|
|
|
}
|
|
|
|
|
2016-03-21 18:56:41 +01:00
|
|
|
//Load FIRM into FCRAM
|
2016-04-02 17:58:06 +02:00
|
|
|
void loadFirm(void)
|
|
|
|
{
|
2016-03-04 23:43:22 +01:00
|
|
|
//If not using an A9LH setup or the patched FIRM, load 9.0 FIRM from NAND
|
2016-04-02 17:58:06 +02:00
|
|
|
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-27 19:19:35 +02:00
|
|
|
nandFirm0((u8 *)firm, firmSize, console);
|
2016-04-02 17:58:06 +02:00
|
|
|
|
2016-02-25 20:19:20 +01:00
|
|
|
//Check for correct decryption
|
2016-03-27 19:19:35 +02:00
|
|
|
if(memcmp(firm, "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-04-04 18:19:00 +02:00
|
|
|
|
2016-02-19 04:53:19 +01:00
|
|
|
//Load FIRM from SD
|
2016-04-02 17:58:06 +02:00
|
|
|
else
|
|
|
|
{
|
2016-03-23 02:27:53 +01:00
|
|
|
const char *path = usePatchedFirm ? patchedFirms[selectedFirm - 1] :
|
2016-04-02 17:58:06 +02:00
|
|
|
(mode ? "/aurei/firmware.bin" : "/aurei/firmware90.bin");
|
2016-03-04 23:43:22 +01:00
|
|
|
firmSize = fileSize(path);
|
2016-04-04 18:07:13 +02:00
|
|
|
if(!firmSize) error(mode ? "aurei/firmware.bin doesn't exist" :
|
|
|
|
"aurei/firmware90.bin doesn't exist");
|
2016-03-27 19:19:35 +02:00
|
|
|
fileRead(firm, path, firmSize);
|
2016-02-08 03:37:03 +01:00
|
|
|
}
|
2016-02-25 20:19:20 +01:00
|
|
|
|
2016-03-27 19:19:35 +02:00
|
|
|
section = firm->section;
|
2016-02-25 20:19:20 +01:00
|
|
|
|
|
|
|
//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-04-04 18:07:13 +02:00
|
|
|
error(mode ? "aurei/firmware.bin doesn't match this console,\nor it's encrypted" :
|
|
|
|
"aurei/firmware90.bin doesn't match this console,\nor it's encrypted");
|
2016-02-08 03:37:03 +01:00
|
|
|
|
2016-03-27 19:19:35 +02:00
|
|
|
arm9Section = (u8 *)firm + section[2].offset;
|
2016-03-21 18:56:41 +01:00
|
|
|
|
2016-04-04 18:07:13 +02:00
|
|
|
//On N3DS, decrypt ARM9Bin and patch ARM9 entrypoint to skip arm9loader
|
|
|
|
if(console && !usePatchedFirm)
|
|
|
|
{
|
|
|
|
decryptArm9Bin(arm9Section, mode);
|
|
|
|
firm->arm9Entry = (u8 *)0x801B01C;
|
|
|
|
}
|
2015-08-05 11:54:00 +02:00
|
|
|
}
|
|
|
|
|
2016-04-04 18:07:13 +02:00
|
|
|
static inline void patchTwlAgb(u32 whichFirm)
|
2016-04-03 17:56:09 +02:00
|
|
|
{
|
2016-04-03 18:13:05 +02:00
|
|
|
static firmHeader *const twlAgbFirm = (firmHeader *)0x25000000;
|
2016-04-03 17:56:09 +02:00
|
|
|
|
2016-04-04 18:07:13 +02:00
|
|
|
const char *path = whichFirm ? "/aurei/firmware_agb.bin" : "/aurei/firmware_twl.bin";
|
2016-04-03 17:56:09 +02:00
|
|
|
u32 size = fileSize(path);
|
|
|
|
|
2016-04-03 18:10:48 +02:00
|
|
|
//Skip patching if the file doesn't exist
|
2016-04-03 17:56:09 +02:00
|
|
|
if(!size) return;
|
|
|
|
|
|
|
|
fileRead(twlAgbFirm, path, size);
|
|
|
|
|
2016-04-03 18:13:05 +02:00
|
|
|
static const firmSectionHeader *twlAgbSection = twlAgbFirm->section;
|
2016-04-03 17:56:09 +02:00
|
|
|
|
|
|
|
//Check that the loaded FIRM matches the console
|
|
|
|
if((((u32)twlAgbSection[3].address >> 8) & 0xFF) != (console ? 0x60 : 0x68))
|
2016-04-04 18:07:13 +02:00
|
|
|
error(whichFirm ? "aurei/firmware_agb.bin doesn't match this\nconsole, or it's encrypted" :
|
|
|
|
"aurei/firmware_twl.bin doesn't match this\nconsole, or it's encrypted");
|
2016-04-03 17:56:09 +02:00
|
|
|
|
2016-04-04 18:07:13 +02:00
|
|
|
//On N3DS, decrypt ARM9Bin and patch ARM9 entrypoint to skip arm9loader
|
|
|
|
if(console)
|
|
|
|
{
|
|
|
|
decryptArm9Bin((u8 *)twlAgbFirm + twlAgbSection[3].offset, 0);
|
|
|
|
twlAgbFirm->arm9Entry = (u8 *)0x801301C;
|
|
|
|
}
|
2016-04-03 17:56:09 +02:00
|
|
|
|
2016-04-05 05:27:28 +02:00
|
|
|
static const patchData twlPatches[] = {
|
2016-04-03 17:56:09 +02:00
|
|
|
{{0x1650C0, 0x165D64}, {{ 6, 0x00, 0x20, 0x4E, 0xB0, 0x70, 0xBD }}, 0},
|
|
|
|
{{0x173A0E, 0x17474A}, { .type1 = 0x2001 }, 1},
|
|
|
|
{{0x174802, 0x17553E}, { .type1 = 0x2000 }, 2},
|
|
|
|
{{0x174964, 0x1756A0}, { .type1 = 0x2000 }, 2},
|
|
|
|
{{0x174D52, 0x175A8E}, { .type1 = 0x2001 }, 2},
|
|
|
|
{{0x174D5E, 0x175A9A}, { .type1 = 0x2001 }, 2},
|
|
|
|
{{0x174D6A, 0x175AA6}, { .type1 = 0x2001 }, 2},
|
|
|
|
{{0x174E56, 0x175B92}, { .type1 = 0x2001 }, 1},
|
|
|
|
{{0x174E58, 0x175B94}, { .type1 = 0x4770 }, 1}
|
2016-04-05 05:27:28 +02:00
|
|
|
},
|
|
|
|
agbPatches[] = {
|
2016-04-03 17:56:09 +02:00
|
|
|
{{0x9D2A8, 0x9DF64}, {{ 6, 0x00, 0x20, 0x4E, 0xB0, 0x70, 0xBD }}, 0},
|
|
|
|
{{0xD7A12, 0xD8B8A}, { .type1 = 0xEF26 }, 1}
|
|
|
|
};
|
|
|
|
|
2016-04-04 18:19:00 +02:00
|
|
|
/* Calculate the amount of patches to apply. Only count the boot screen patch for AGB_FIRM
|
2016-04-04 01:08:42 +02:00
|
|
|
if the matching option was enabled (keep it as last) */
|
2016-04-06 14:51:07 +02:00
|
|
|
u32 numPatches = whichFirm ? (sizeof(agbPatches) / sizeof(patchData) - !CONFIG(6, 1)) :
|
2016-04-05 05:27:28 +02:00
|
|
|
(sizeof(twlPatches) / sizeof(patchData));
|
|
|
|
const patchData *patches = whichFirm ? agbPatches : twlPatches;
|
2016-04-03 17:56:09 +02:00
|
|
|
|
|
|
|
//Patch
|
|
|
|
for(u32 i = 0; i < numPatches; i++)
|
|
|
|
{
|
|
|
|
switch(patches[i].type)
|
|
|
|
{
|
|
|
|
case 0:
|
|
|
|
memcpy((u8 *)twlAgbFirm + patches[i].offset[console], patches[i].patch.type0 + 1, patches[i].patch.type0[0]);
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
*(u16 *)((u8 *)twlAgbFirm + patches[i].offset[console] + 2) = 0;
|
|
|
|
case 1:
|
|
|
|
*(u16 *)((u8 *)twlAgbFirm + patches[i].offset[console]) = patches[i].patch.type1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-04 18:07:13 +02:00
|
|
|
fileWrite(twlAgbFirm, whichFirm ? patchedFirms[5] : patchedFirms[4], size);
|
2016-04-03 17:56:09 +02:00
|
|
|
}
|
|
|
|
|
2016-03-21 18:56:41 +01:00
|
|
|
//NAND redirection
|
2016-04-02 17:58:06 +02:00
|
|
|
static inline void patchEmuNAND(u8 *proc9Offset)
|
|
|
|
{
|
2016-03-26 17:28:47 +01:00
|
|
|
//Copy emuNAND code
|
2016-03-29 17:43:53 +02:00
|
|
|
void *emuCodeOffset = getEmuCode(proc9Offset);
|
2016-03-23 15:48:59 +01:00
|
|
|
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-27 19:19:35 +02:00
|
|
|
u32 branchOffset = (u32)emuCodeOffset - (u32)firm -
|
2016-03-26 17:24:16 +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-31 15:57:02 +02:00
|
|
|
u32 emuRead,
|
|
|
|
emuWrite;
|
|
|
|
|
2016-03-21 18:56:41 +01:00
|
|
|
getEmuRW(arm9Section, section[2].size, &emuRead, &emuWrite);
|
2016-03-26 17:24:16 +01:00
|
|
|
*(u16 *)emuRead = nandRedir[0];
|
|
|
|
*((u16 *)emuRead + 1) = nandRedir[1];
|
|
|
|
*((u32 *)emuRead + 1) = branchOffset;
|
|
|
|
*(u16 *)emuWrite = nandRedir[0];
|
|
|
|
*((u16 *)emuWrite + 1) = nandRedir[1];
|
|
|
|
*((u32 *)emuWrite + 1) = branchOffset;
|
2016-02-08 03:37:03 +01:00
|
|
|
|
2016-02-06 00:54:24 +01:00
|
|
|
//Set MPU for emu code region
|
2016-03-26 17:24:16 +01:00
|
|
|
u32 *mpuOffset = getMPU(arm9Section, section[2].size);
|
|
|
|
*mpuOffset = mpuPatch[0];
|
|
|
|
*(mpuOffset + 6) = mpuPatch[1];
|
|
|
|
*(mpuOffset + 9) = mpuPatch[2];
|
2015-08-07 09:01:42 +02:00
|
|
|
}
|
|
|
|
|
2016-04-02 17:58:06 +02:00
|
|
|
static inline void patchReboots(u8 *proc9Offset)
|
|
|
|
{
|
|
|
|
//Calculate offset for the firmlaunch code
|
|
|
|
void *rebootOffset = getReboot(arm9Section, section[2].size);
|
|
|
|
|
|
|
|
//Calculate offset for the fOpen function
|
|
|
|
u32 fOpenOffset = getfOpen(proc9Offset, rebootOffset);
|
|
|
|
|
|
|
|
//Copy firmlaunch code
|
|
|
|
memcpy(rebootOffset, reboot, reboot_size);
|
|
|
|
|
|
|
|
//Put the fOpen offset in the right location
|
|
|
|
u32 *pos_fopen = (u32 *)memsearch(rebootOffset, "OPEN", reboot_size, 4);
|
|
|
|
*pos_fopen = fOpenOffset;
|
2016-03-05 15:35:06 +01:00
|
|
|
|
2016-04-02 17:58:06 +02:00
|
|
|
//Patch path for emuNAND-patched FIRM
|
|
|
|
if(emuNAND)
|
|
|
|
{
|
|
|
|
void *pos_path = memsearch(rebootOffset, L"sy", reboot_size, 4);
|
|
|
|
memcpy(pos_path, emuNAND == 1 ? L"emu" : L"em2", 5);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void injectLoader(void)
|
|
|
|
{
|
|
|
|
u32 loaderOffset,
|
|
|
|
loaderSize;
|
|
|
|
|
|
|
|
getLoader((u8 *)firm + section[0].offset, section[0].size, &loaderOffset, &loaderSize);
|
|
|
|
|
|
|
|
//Check that the injector CXI isn't larger than the original
|
|
|
|
if(injector_size <= (int)loaderSize)
|
|
|
|
{
|
|
|
|
memset((void *)loaderOffset, 0, loaderSize);
|
|
|
|
memcpy((void *)loaderOffset, injector, injector_size);
|
|
|
|
|
|
|
|
//Patch content size and ExeFS size to match the repaced loader's ones
|
|
|
|
*((u32 *)loaderOffset + 0x41) = loaderSize / 0x200;
|
|
|
|
*((u32 *)loaderOffset + 0x69) = loaderSize / 0x200 - 5;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//Patches
|
|
|
|
void patchFirm(void)
|
|
|
|
{
|
2016-04-04 18:07:13 +02:00
|
|
|
if(mode)
|
|
|
|
{
|
|
|
|
//Only patch AGB_FIRM/TWL_FIRM if the patched ones don't already exist
|
|
|
|
if(!fileExists(patchedFirms[4])) patchTwlAgb(0);
|
|
|
|
if(!fileExists(patchedFirms[5])) patchTwlAgb(1);
|
|
|
|
}
|
2016-04-03 17:56:09 +02:00
|
|
|
|
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-04-02 17:58:06 +02:00
|
|
|
if(mode || emuNAND)
|
|
|
|
{
|
2016-03-21 18:56:41 +01:00
|
|
|
//Find the Process9 NCCH location
|
|
|
|
u8 *proc9Offset = getProc9(arm9Section, section[2].size);
|
|
|
|
|
|
|
|
//Apply emuNAND patches
|
2016-04-02 17:58:06 +02:00
|
|
|
if(emuNAND) patchEmuNAND(proc9Offset);
|
|
|
|
|
|
|
|
//Apply FIRM reboot patches, not on 9.0 FIRM as it breaks firmlaunchhax
|
|
|
|
if(mode) patchReboots(proc9Offset);
|
2016-02-19 21:32:07 +01:00
|
|
|
}
|
2016-03-21 18:56:41 +01:00
|
|
|
|
2016-04-02 17:58:06 +02:00
|
|
|
//Apply FIRM0/1 writes patches on sysNAND to protect A9LH
|
|
|
|
if(a9lhSetup && !emuNAND)
|
|
|
|
{
|
2016-03-26 17:24:16 +01:00
|
|
|
u16 *writeOffset = getFirmWrite(arm9Section, section[2].size);
|
|
|
|
*writeOffset = writeBlock[0];
|
|
|
|
*(writeOffset + 1) = writeBlock[1];
|
2016-02-19 21:32:07 +01:00
|
|
|
}
|
2016-02-08 03:37:03 +01:00
|
|
|
|
2016-04-02 17:58:06 +02:00
|
|
|
//Apply signature checks patches
|
2016-03-11 15:08:05 +01:00
|
|
|
u32 sigOffset,
|
|
|
|
sigOffset2;
|
2016-02-08 23:46:01 +01:00
|
|
|
|
2016-03-27 19:19:35 +02:00
|
|
|
getSigChecks(arm9Section, section[2].size, &sigOffset, &sigOffset2);
|
2016-03-26 17:24:16 +01:00
|
|
|
*(u16 *)sigOffset = sigPatch[0];
|
|
|
|
*(u16 *)sigOffset2 = sigPatch[0];
|
|
|
|
*((u16 *)sigOffset2 + 1) = sigPatch[1];
|
2016-02-08 03:37:03 +01:00
|
|
|
|
2016-03-29 17:43:53 +02:00
|
|
|
//Replace the FIRM loader with the injector
|
2016-04-02 17:58:06 +02:00
|
|
|
injectLoader();
|
2016-03-29 17:43:53 +02: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-27 19:19:35 +02:00
|
|
|
if(!fileWrite(firm, patchedFirms[selectedFirm - 1], firmSize))
|
2016-03-23 03:45:32 +01:00
|
|
|
error("Couldn't write the patched FIRM (no free space?)");
|
2015-08-05 03:57:37 +02:00
|
|
|
}
|
|
|
|
|
2016-04-02 17:58:06 +02:00
|
|
|
void launchFirm(void)
|
|
|
|
{
|
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-27 19:19:35 +02:00
|
|
|
memcpy(section[0].address, (u8 *)firm + section[0].offset, section[0].size);
|
|
|
|
memcpy(section[1].address, (u8 *)firm + 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
|
2016-03-27 19:19:35 +02:00
|
|
|
*(vu32 *)0x1FFFFFF8 = (u32)firm->arm11Entry;
|
2016-03-11 15:08:05 +01:00
|
|
|
|
2016-03-23 02:27:53 +01:00
|
|
|
//Final jump to arm9 kernel
|
2016-03-27 19:19:35 +02:00
|
|
|
((void (*)())firm->arm9Entry)();
|
2015-08-05 03:57:37 +02:00
|
|
|
}
|