2015-08-05 03:57:37 +02:00
|
|
|
/*
|
|
|
|
* firm.c
|
|
|
|
*/
|
|
|
|
|
|
|
|
#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"
|
|
|
|
#include "buttons.h"
|
2016-03-23 14:59:52 +01:00
|
|
|
#include "../build/patches.h"
|
2016-03-23 02:27:53 +01: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-04-02 17:58:06 +02:00
|
|
|
|
2016-04-11 05:15:44 +02:00
|
|
|
u32 config,
|
|
|
|
console,
|
2016-04-14 17:10:55 +02:00
|
|
|
firmSource,
|
|
|
|
emuOffset;
|
2016-04-11 05:15:44 +02:00
|
|
|
|
|
|
|
void main(void)
|
|
|
|
{
|
|
|
|
u32 bootType,
|
|
|
|
firmType,
|
|
|
|
nandType,
|
2016-05-03 01:17:22 +02:00
|
|
|
a9lhMode,
|
2016-04-11 05:15:44 +02:00
|
|
|
updatedSys,
|
|
|
|
needConfig,
|
|
|
|
newConfig,
|
|
|
|
emuHeader;
|
2016-05-11 19:28:28 +02:00
|
|
|
|
|
|
|
u64 chronoStarted = 0;
|
2016-04-02 17:58:06 +02:00
|
|
|
|
2016-02-19 21:32:07 +01:00
|
|
|
//Detect the console being used
|
2016-05-03 01:17:22 +02:00
|
|
|
console = PDN_MPCORE_CFG == 7;
|
2016-04-02 17:58:06 +02:00
|
|
|
|
2016-04-11 05:15:44 +02:00
|
|
|
//Mount filesystems. CTRNAND will be mounted only if/when needed
|
|
|
|
mountFs();
|
2016-03-23 02:27:53 +01:00
|
|
|
|
2016-04-23 01:43:11 +02:00
|
|
|
const char configPath[] = "/luma/config.bin";
|
2016-05-03 01:17:22 +02:00
|
|
|
|
|
|
|
//Attempt to read the configuration file
|
2016-05-03 20:03:37 +02:00
|
|
|
needConfig = fileRead(&config, configPath) ? 1 : 2;
|
2016-02-25 20:19:20 +01:00
|
|
|
|
2016-04-11 05:15:44 +02:00
|
|
|
//Determine if this is a firmlaunch boot
|
|
|
|
if(*(vu8 *)0x23F00005)
|
2016-04-02 17:58:06 +02:00
|
|
|
{
|
2016-04-15 16:10:32 +02:00
|
|
|
if(needConfig == 2) mcuReboot();
|
2016-04-15 15:58:40 +02:00
|
|
|
|
2016-04-11 05:15:44 +02:00
|
|
|
bootType = 1;
|
2016-03-24 17:27:02 +01:00
|
|
|
|
2016-04-11 05:15:44 +02:00
|
|
|
//'0' = NATIVE_FIRM, '1' = TWL_FIRM, '2' = AGB_FIRM
|
2016-05-03 01:17:22 +02:00
|
|
|
firmType = *(vu8 *)0x23F00009 == '3' ? 3 : *(vu8 *)0x23F00005 - '0';
|
2016-04-02 17:58:06 +02:00
|
|
|
|
2016-04-12 23:18:07 +02:00
|
|
|
nandType = BOOTCONFIG(0, 3);
|
|
|
|
firmSource = BOOTCONFIG(2, 1);
|
2016-05-03 01:17:22 +02:00
|
|
|
a9lhMode = BOOTCONFIG(3, 1);
|
2016-05-03 20:03:37 +02:00
|
|
|
updatedSys = a9lhMode && CONFIG(1);
|
2016-04-02 17:58:06 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-05-03 01:17:22 +02:00
|
|
|
//Get pressed buttons
|
|
|
|
u32 pressed = HID_PAD;
|
|
|
|
|
|
|
|
//If no configuration file exists or SELECT is held, load configuration menu
|
2016-05-11 19:28:28 +02:00
|
|
|
if(needConfig == 2 || ((pressed & BUTTON_SELECT) && !(pressed & BUTTON_L1)))
|
2016-05-09 03:41:00 +02:00
|
|
|
{
|
2016-05-03 01:17:22 +02:00
|
|
|
configureCFW(configPath);
|
|
|
|
|
2016-05-09 03:41:00 +02:00
|
|
|
//Zero the last booted FIRM flag
|
|
|
|
CFG_BOOTENV = 0;
|
|
|
|
|
2016-05-11 19:28:28 +02:00
|
|
|
chronoStarted = chrono();
|
|
|
|
while(chrono() - chronoStarted < 2 * TICKS_PER_SEC); //Wait for 2s
|
|
|
|
chronoStarted = 1;
|
|
|
|
|
2016-05-09 03:41:00 +02:00
|
|
|
//Update pressed buttons
|
|
|
|
pressed = HID_PAD;
|
|
|
|
}
|
|
|
|
|
2016-04-11 05:15:44 +02:00
|
|
|
bootType = 0;
|
2016-04-11 14:32:38 +02:00
|
|
|
firmType = 0;
|
2016-02-19 21:32:07 +01:00
|
|
|
|
2016-04-11 05:15:44 +02:00
|
|
|
//Determine if booting with A9LH
|
2016-05-03 01:17:22 +02:00
|
|
|
u32 a9lhBoot = !PDN_SPI_CNT;
|
2016-04-11 05:15:44 +02:00
|
|
|
|
|
|
|
//Determine if A9LH is installed and the user has an updated sysNAND
|
2016-04-12 23:18:07 +02:00
|
|
|
if(a9lhBoot || CONFIG(2))
|
2016-04-02 17:58:06 +02:00
|
|
|
{
|
2016-05-03 01:17:22 +02:00
|
|
|
a9lhMode = 1;
|
2016-04-12 23:18:07 +02:00
|
|
|
updatedSys = CONFIG(1);
|
2016-04-02 17:58:06 +02:00
|
|
|
}
|
2016-04-11 05:15:44 +02:00
|
|
|
else
|
2016-04-02 17:58:06 +02:00
|
|
|
{
|
2016-05-03 01:17:22 +02:00
|
|
|
a9lhMode = 0;
|
2016-04-11 05:15:44 +02:00
|
|
|
updatedSys = 0;
|
2016-03-05 15:35:06 +01:00
|
|
|
}
|
|
|
|
|
2016-05-03 01:17:22 +02:00
|
|
|
newConfig = a9lhMode << 3;
|
2016-04-11 05:15:44 +02:00
|
|
|
|
2016-05-03 01:17:22 +02:00
|
|
|
if(a9lhBoot)
|
2016-04-02 17:58:06 +02:00
|
|
|
{
|
2016-05-03 01:17:22 +02:00
|
|
|
//Retrieve the last booted FIRM
|
|
|
|
u32 previousFirm = CFG_BOOTENV;
|
2016-04-11 05:15:44 +02:00
|
|
|
|
2016-05-03 01:17:22 +02:00
|
|
|
//If it's a MCU reboot, try to force boot options
|
|
|
|
if(previousFirm)
|
|
|
|
{
|
|
|
|
//Always force a sysNAND boot when quitting AGB_FIRM
|
|
|
|
if(previousFirm == 7)
|
|
|
|
{
|
|
|
|
nandType = 0;
|
|
|
|
firmSource = updatedSys ? 0 : BOOTCONFIG(2, 1);
|
|
|
|
needConfig--;
|
|
|
|
|
|
|
|
//Flag to prevent multiple boot options-forcing
|
|
|
|
newConfig |= 1 << 4;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Else, force the last used boot options unless a payload button or A/L/R are pressed
|
|
|
|
or the no-forcing flag is set */
|
|
|
|
else if(!(pressed & OVERRIDE_BUTTONS) && !BOOTCONFIG(4, 1))
|
|
|
|
{
|
|
|
|
nandType = BOOTCONFIG(0, 3);
|
|
|
|
firmSource = BOOTCONFIG(2, 1);
|
|
|
|
needConfig--;
|
|
|
|
}
|
2016-04-11 05:15:44 +02:00
|
|
|
}
|
2016-05-03 01:17:22 +02:00
|
|
|
|
|
|
|
//If the SAFE MODE combo is held, force a sysNAND boot
|
|
|
|
else if(pressed == SAFE_MODE)
|
2016-04-11 05:15:44 +02:00
|
|
|
{
|
2016-05-03 01:17:22 +02:00
|
|
|
a9lhMode++;
|
|
|
|
nandType = 0;
|
|
|
|
firmSource = 0;
|
|
|
|
needConfig--;
|
2016-04-11 05:15:44 +02:00
|
|
|
}
|
2016-04-02 18:48:31 +02:00
|
|
|
}
|
2016-03-05 15:35:06 +01:00
|
|
|
|
2016-04-11 05:15:44 +02:00
|
|
|
//Boot options aren't being forced
|
|
|
|
if(needConfig)
|
|
|
|
{
|
2016-05-03 01:17:22 +02:00
|
|
|
/* If L and R/A/Select or one of the single payload buttons are pressed,
|
|
|
|
chainload an external payload */
|
|
|
|
if((pressed & SINGLE_PAYLOAD_BUTTONS) || ((pressed & BUTTON_L1) && (pressed & L_PAYLOAD_BUTTONS)))
|
2016-04-29 14:28:37 +02:00
|
|
|
loadPayload(pressed);
|
2016-03-31 01:38:28 +02:00
|
|
|
|
2016-04-11 05:15:44 +02:00
|
|
|
//If screens are inited or the corresponding option is set, load splash screen
|
2016-05-11 19:28:28 +02:00
|
|
|
if((PDN_GPU_CNT != 1 || CONFIG(7)) && loadSplash())
|
|
|
|
chronoStarted = chrono();
|
|
|
|
|
2016-05-01 19:31:58 +02:00
|
|
|
//If R is pressed, boot the non-updated NAND with the FIRM of the opposite one
|
|
|
|
if(pressed & BUTTON_R1)
|
|
|
|
{
|
|
|
|
nandType = updatedSys;
|
|
|
|
firmSource = !nandType;
|
|
|
|
}
|
2016-04-21 23:39:05 +02:00
|
|
|
|
2016-05-01 19:31:58 +02:00
|
|
|
/* Else, boot the NAND the user set to autoboot or the opposite one, depending on L,
|
|
|
|
with their own FIRM */
|
|
|
|
else
|
|
|
|
{
|
|
|
|
nandType = CONFIG(0) != !(pressed & BUTTON_L1);
|
|
|
|
firmSource = nandType;
|
|
|
|
}
|
2016-04-11 05:15:44 +02:00
|
|
|
|
|
|
|
/* If we're booting emuNAND the second emuNAND is set as default and B isn't pressed,
|
|
|
|
or vice-versa, boot the second emuNAND */
|
2016-05-01 19:31:58 +02:00
|
|
|
if(nandType && (CONFIG(3) == !(pressed & BUTTON_B))) nandType++;
|
2016-03-05 15:35:06 +01:00
|
|
|
}
|
2016-04-11 05:15:44 +02:00
|
|
|
}
|
2016-04-04 18:19:00 +02:00
|
|
|
|
2016-04-11 05:15:44 +02:00
|
|
|
//If we need to boot emuNAND, make sure it exists
|
|
|
|
if(nandType)
|
|
|
|
{
|
|
|
|
getEmunandSect(&emuOffset, &emuHeader, &nandType);
|
|
|
|
if(!nandType) firmSource = 0;
|
2016-03-05 15:35:06 +01:00
|
|
|
}
|
2016-03-04 23:43:22 +01:00
|
|
|
|
2016-04-11 05:15:44 +02:00
|
|
|
//Same if we're using emuNAND as the FIRM source
|
|
|
|
else if(firmSource)
|
|
|
|
getEmunandSect(&emuOffset, &emuHeader, &firmSource);
|
2016-03-04 23:43:22 +01:00
|
|
|
|
2016-04-11 05:15:44 +02:00
|
|
|
if(!bootType)
|
2016-04-02 17:58:06 +02:00
|
|
|
{
|
2016-04-12 23:18:07 +02:00
|
|
|
newConfig |= nandType | (firmSource << 2);
|
2016-04-02 17:58:06 +02:00
|
|
|
|
2016-04-11 05:15:44 +02:00
|
|
|
/* If the boot configuration is different from previously, overwrite it.
|
|
|
|
Just the no-forcing flag being set is not enough */
|
2016-04-13 01:08:13 +02:00
|
|
|
if((newConfig & 0x2F) != (config & 0x3F))
|
2016-04-11 05:15:44 +02:00
|
|
|
{
|
2016-04-14 17:10:55 +02:00
|
|
|
//Preserve user settings (last 26 bits)
|
2016-04-12 23:18:07 +02:00
|
|
|
newConfig |= config & 0xFFFFFFC0;
|
2016-04-11 05:15:44 +02:00
|
|
|
|
2016-04-12 23:18:07 +02:00
|
|
|
fileWrite(&newConfig, configPath, 4);
|
2016-04-11 05:15:44 +02:00
|
|
|
}
|
2016-03-31 15:57:02 +02:00
|
|
|
}
|
2016-04-11 05:15:44 +02:00
|
|
|
|
2016-04-19 17:17:39 +02:00
|
|
|
loadFirm(firmType, !firmType && updatedSys == !firmSource);
|
2016-04-11 05:15:44 +02:00
|
|
|
|
2016-05-03 01:17:22 +02:00
|
|
|
switch(firmType)
|
|
|
|
{
|
|
|
|
case 0:
|
|
|
|
patchNativeFirm(nandType, emuHeader, a9lhMode);
|
|
|
|
break;
|
|
|
|
case 3:
|
|
|
|
patchSafeFirm();
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
patchLegacyFirm(firmType);
|
|
|
|
break;
|
|
|
|
}
|
2016-04-11 05:15:44 +02:00
|
|
|
|
2016-05-11 19:28:28 +02:00
|
|
|
if(chronoStarted)
|
|
|
|
{
|
|
|
|
while(chronoStarted > 1 && chrono() - chronoStarted < 3 * TICKS_PER_SEC);
|
|
|
|
stopChrono();
|
|
|
|
}
|
|
|
|
|
2016-05-09 03:41:00 +02:00
|
|
|
launchFirm(!firmType, bootType);
|
2016-02-25 20:19:20 +01:00
|
|
|
}
|
|
|
|
|
2016-04-11 05:15:44 +02:00
|
|
|
static inline void loadFirm(u32 firmType, u32 externalFirm)
|
2016-04-02 17:58:06 +02:00
|
|
|
{
|
2016-04-11 05:15:44 +02:00
|
|
|
section = firm->section;
|
2016-04-04 18:19:00 +02:00
|
|
|
|
2016-04-29 15:19:57 +02:00
|
|
|
u32 externalFirmLoaded = externalFirm &&
|
2016-05-03 20:03:37 +02:00
|
|
|
fileRead(firm, "/luma/firmware.bin") &&
|
2016-04-29 15:19:57 +02:00
|
|
|
(((u32)section[2].address >> 8) & 0xFF) == (console ? 0x60 : 0x68);
|
2016-04-28 16:11:25 +02:00
|
|
|
|
2016-04-21 04:06:40 +02:00
|
|
|
/* If the conditions to load the external FIRM aren't met, or reading fails, or the FIRM
|
2016-04-28 16:11:25 +02:00
|
|
|
doesn't match the console, load FIRM from CTRNAND */
|
|
|
|
if(!externalFirmLoaded)
|
2016-04-04 18:07:13 +02:00
|
|
|
{
|
2016-05-03 01:17:22 +02:00
|
|
|
const char *firmFolders[4][2] = {{ "00000002", "20000002" },
|
2016-04-14 17:10:55 +02:00
|
|
|
{ "00000102", "20000102" },
|
2016-05-03 01:17:22 +02:00
|
|
|
{ "00000202", "20000202" },
|
|
|
|
{ "00000003", "20000003" }};
|
2016-04-14 17:10:55 +02:00
|
|
|
|
2016-04-11 14:32:38 +02:00
|
|
|
firmRead(firm, firmFolders[firmType][console]);
|
2016-04-11 05:15:44 +02:00
|
|
|
decryptExeFs((u8 *)firm);
|
2016-04-04 18:07:13 +02:00
|
|
|
}
|
2015-08-05 11:54:00 +02:00
|
|
|
}
|
|
|
|
|
2016-05-03 01:17:22 +02:00
|
|
|
static inline void patchNativeFirm(u32 nandType, u32 emuHeader, u32 a9lhMode)
|
2016-04-03 17:56:09 +02:00
|
|
|
{
|
2016-04-11 05:15:44 +02:00
|
|
|
u8 *arm9Section = (u8 *)firm + section[2].offset;
|
2016-05-10 23:06:32 +02:00
|
|
|
|
2016-04-11 14:32:38 +02:00
|
|
|
u32 nativeFirmType;
|
|
|
|
|
2016-04-11 05:15:44 +02:00
|
|
|
if(console)
|
|
|
|
{
|
2016-05-05 04:43:44 +02:00
|
|
|
//Determine the NATIVE_FIRM version
|
|
|
|
switch(arm9Section[0x53])
|
|
|
|
{
|
|
|
|
case 0xFF:
|
|
|
|
nativeFirmType = 0;
|
|
|
|
break;
|
|
|
|
case '1':
|
|
|
|
nativeFirmType = 1;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
nativeFirmType = 2;
|
|
|
|
break;
|
|
|
|
}
|
2016-04-03 17:56:09 +02:00
|
|
|
|
2016-04-11 05:15:44 +02:00
|
|
|
//Decrypt ARM9Bin and patch ARM9 entrypoint to skip arm9loader
|
2016-04-12 14:32:38 +02:00
|
|
|
arm9Loader(arm9Section, nativeFirmType);
|
2016-04-11 05:15:44 +02:00
|
|
|
firm->arm9Entry = (u8 *)0x801B01C;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
//Determine if we're booting the 9.0 FIRM
|
|
|
|
u8 firm90Hash[0x10] = {0x27, 0x2D, 0xFE, 0xEB, 0xAF, 0x3F, 0x6B, 0x3B, 0xF5, 0xDE, 0x4C, 0x41, 0xDE, 0x95, 0x27, 0x6A};
|
2016-05-03 20:03:37 +02:00
|
|
|
nativeFirmType = memcmp(section[2].hash, firm90Hash, 0x10) != 0;
|
2016-04-11 05:15:44 +02:00
|
|
|
}
|
2016-04-03 17:56:09 +02:00
|
|
|
|
2016-05-11 19:28:28 +02:00
|
|
|
u32 process9Size,
|
|
|
|
process9MemAddr;
|
2016-04-03 17:56:09 +02:00
|
|
|
|
2016-05-11 19:28:28 +02:00
|
|
|
//Find the Process9 NCCH location
|
|
|
|
u8 *process9Offset = getProcess9(arm9Section + 0x15000, section[2].size - 0x15000, &process9Size, &process9MemAddr);
|
2016-04-03 17:56:09 +02:00
|
|
|
|
2016-05-11 19:28:28 +02:00
|
|
|
//Apply emuNAND patches
|
|
|
|
if(nandType) patchEmuNAND(arm9Section, process9Offset, process9Size, emuHeader);
|
2016-04-03 17:56:09 +02:00
|
|
|
|
2016-04-11 05:15:44 +02:00
|
|
|
//Apply FIRM0/1 writes patches on sysNAND to protect A9LH
|
2016-05-11 19:28:28 +02:00
|
|
|
else if(a9lhMode) patchFirmWrites(process9Offset, process9Size, 1);
|
|
|
|
|
|
|
|
//Apply FIRM reboot patches, not on 9.0 FIRM as it breaks firmlaunchhax
|
|
|
|
if(nativeFirmType || a9lhMode == 2) patchReboots(process9Offset, process9Size, process9MemAddr);
|
2016-04-03 17:56:09 +02:00
|
|
|
|
2016-04-11 05:15:44 +02:00
|
|
|
//Apply signature checks patches
|
|
|
|
u32 sigOffset,
|
|
|
|
sigOffset2;
|
2016-04-03 17:56:09 +02:00
|
|
|
|
2016-05-11 19:28:28 +02:00
|
|
|
getSigChecks(process9Offset, process9Size, &sigOffset, &sigOffset2);
|
2016-04-11 05:15:44 +02:00
|
|
|
*(u16 *)sigOffset = sigPatch[0];
|
|
|
|
*(u16 *)sigOffset2 = sigPatch[0];
|
|
|
|
*((u16 *)sigOffset2 + 1) = sigPatch[1];
|
2016-04-03 17:56:09 +02:00
|
|
|
|
2016-05-11 19:28:28 +02:00
|
|
|
//Does nothing if svcBackdoor is still there
|
|
|
|
reimplementSvcBackdoor();
|
|
|
|
|
2016-05-07 23:40:02 +02:00
|
|
|
//Replace the FIRM loader with the injector while copying section0
|
|
|
|
copySection0AndInjectLoader();
|
2016-04-03 17:56:09 +02:00
|
|
|
}
|
|
|
|
|
2016-05-11 19:28:28 +02:00
|
|
|
static inline void patchEmuNAND(u8 *arm9Section, u8 *process9Offset, u32 process9Size, u32 emuHeader)
|
2016-04-02 17:58:06 +02:00
|
|
|
{
|
2016-04-11 22:16:51 +02:00
|
|
|
//Copy emuNAND code
|
2016-05-11 19:28:28 +02:00
|
|
|
void *emuCodeOffset = getEmuCode(arm9Section);
|
2016-03-23 15:48:59 +01:00
|
|
|
memcpy(emuCodeOffset, emunand, emunand_size);
|
|
|
|
|
2016-04-11 22:16:51 +02:00
|
|
|
//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-05-11 19:28:28 +02:00
|
|
|
*pos_sdmmc = getSDMMC(process9Offset, process9Size);
|
2016-03-21 18:56:41 +01:00
|
|
|
|
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
|
|
|
|
2016-04-11 22:16:51 +02:00
|
|
|
//Add emuNAND hooks
|
2016-03-31 15:57:02 +02:00
|
|
|
u32 emuRead,
|
|
|
|
emuWrite;
|
|
|
|
|
2016-05-11 19:28:28 +02:00
|
|
|
getEmuRW(process9Offset, process9Size, &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-05-11 19:28:28 +02:00
|
|
|
static inline void patchReboots(u8 *process9Offset, u32 process9Size, u32 process9MemAddr)
|
2016-04-02 17:58:06 +02:00
|
|
|
{
|
2016-05-11 19:28:28 +02:00
|
|
|
u32 fOpenOffset;
|
2016-04-02 17:58:06 +02:00
|
|
|
|
2016-05-11 19:28:28 +02:00
|
|
|
//Calculate offset for the firmlaunch code
|
|
|
|
void *rebootOffset = getReboot(process9Offset, process9Size, process9MemAddr, &fOpenOffset);
|
2016-04-02 17:58:06 +02:00
|
|
|
|
|
|
|
//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-05-11 19:28:28 +02:00
|
|
|
static inline void reimplementSvcBackdoor(void)
|
2016-05-10 23:06:32 +02:00
|
|
|
{
|
2016-05-11 19:28:28 +02:00
|
|
|
u8 *arm11Section1 = (u8 *)firm + section[1].offset;
|
|
|
|
|
2016-05-12 02:59:21 +02:00
|
|
|
u32 exceptionsPage;
|
2016-05-11 19:28:28 +02:00
|
|
|
|
2016-05-12 02:59:21 +02:00
|
|
|
u32 *svcTable = getSvcAndExceptions(arm11Section1, section[1].size, &exceptionsPage);
|
2016-05-11 19:28:28 +02:00
|
|
|
|
|
|
|
if(!svcTable[0x7B])
|
|
|
|
{
|
|
|
|
u32 *freeSpace;
|
|
|
|
|
2016-05-12 02:59:21 +02:00
|
|
|
for(freeSpace = (u32 *)exceptionsPage; *freeSpace != 0xFFFFFFFF; freeSpace++);
|
|
|
|
|
|
|
|
memcpy(freeSpace, svcBackdoor, 40);
|
|
|
|
|
|
|
|
svcTable[0x7B] = 0xFFFF0000 + (u32)((u8 *)freeSpace - exceptionsPage);
|
2016-05-11 19:28:28 +02:00
|
|
|
}
|
2016-05-10 23:06:32 +02:00
|
|
|
}
|
|
|
|
|
2016-05-07 23:40:02 +02:00
|
|
|
static inline void copySection0AndInjectLoader(void)
|
2016-04-02 17:58:06 +02:00
|
|
|
{
|
2016-05-07 23:40:02 +02:00
|
|
|
u8 *arm11Section0 = (u8 *)firm + section[0].offset;
|
2016-05-09 03:41:00 +02:00
|
|
|
u32 loaderSize;
|
|
|
|
u32 loaderOffset = getLoader(arm11Section0, &loaderSize);
|
|
|
|
|
|
|
|
memcpy(section[0].address, arm11Section0, loaderOffset);
|
|
|
|
memcpy(section[0].address + loaderOffset, injector, injector_size);
|
|
|
|
memcpy(section[0].address + loaderOffset + injector_size, arm11Section0 + loaderOffset + loaderSize, section[0].size - (loaderOffset + loaderSize));
|
2016-04-02 17:58:06 +02:00
|
|
|
}
|
|
|
|
|
2016-05-11 19:28:28 +02:00
|
|
|
static inline void patchSafeFirm(void)
|
|
|
|
{
|
|
|
|
u8 *arm9Section = (u8 *)firm + section[2].offset;
|
|
|
|
|
|
|
|
if(console)
|
|
|
|
{
|
|
|
|
//Decrypt ARM9Bin and patch ARM9 entrypoint to skip arm9loader
|
|
|
|
arm9Loader(arm9Section, 0);
|
|
|
|
firm->arm9Entry = (u8 *)0x801B01C;
|
|
|
|
}
|
|
|
|
|
|
|
|
//Apply FIRM0/1 writes patches to protect A9LH
|
|
|
|
patchFirmWrites(arm9Section, section[2].size, console);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void patchFirmWrites(u8 *offset, u32 size, u32 mode)
|
|
|
|
{
|
|
|
|
if(mode)
|
|
|
|
{
|
|
|
|
u16 *writeOffset = getFirmWrite(offset, size);
|
|
|
|
*writeOffset = writeBlock[0];
|
|
|
|
*(writeOffset + 1) = writeBlock[1];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
u16 *writeOffset = getFirmWriteSafe(offset, size);
|
|
|
|
*writeOffset = writeBlockSafe[0];
|
|
|
|
*(writeOffset + 1) = writeBlockSafe[1];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-03 01:17:22 +02:00
|
|
|
static inline void patchLegacyFirm(u32 firmType)
|
2016-04-02 17:58:06 +02:00
|
|
|
{
|
2016-04-11 05:15:44 +02:00
|
|
|
//On N3DS, decrypt ARM9Bin and patch ARM9 entrypoint to skip arm9loader
|
|
|
|
if(console)
|
2016-04-04 18:07:13 +02:00
|
|
|
{
|
2016-04-11 05:15:44 +02:00
|
|
|
arm9Loader((u8 *)firm + section[3].offset, 0);
|
|
|
|
firm->arm9Entry = (u8 *)0x801301C;
|
2016-04-04 18:07:13 +02:00
|
|
|
}
|
2016-04-03 17:56:09 +02:00
|
|
|
|
2016-04-14 17:10:55 +02:00
|
|
|
const patchData twlPatches[] = {
|
2016-04-11 05:15:44 +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}
|
|
|
|
},
|
|
|
|
agbPatches[] = {
|
|
|
|
{{0x9D2A8, 0x9DF64}, {{ 6, 0x00, 0x20, 0x4E, 0xB0, 0x70, 0xBD }}, 0},
|
|
|
|
{{0xD7A12, 0xD8B8A}, { .type1 = 0xEF26 }, 1}
|
|
|
|
};
|
2016-04-02 17:58:06 +02:00
|
|
|
|
2016-04-11 05:15:44 +02:00
|
|
|
/* Calculate the amount of patches to apply. Only count the boot screen patch for AGB_FIRM
|
|
|
|
if the matching option was enabled (keep it as last) */
|
2016-04-21 04:06:40 +02:00
|
|
|
u32 numPatches = firmType == 1 ? (sizeof(twlPatches) / sizeof(patchData)) :
|
2016-04-26 21:51:57 +02:00
|
|
|
(sizeof(agbPatches) / sizeof(patchData) - !CONFIG(6));
|
2016-04-11 18:34:37 +02:00
|
|
|
const patchData *patches = firmType == 1 ? twlPatches : agbPatches;
|
2016-03-21 18:56:41 +01:00
|
|
|
|
2016-04-11 05:15:44 +02:00
|
|
|
//Patch
|
|
|
|
for(u32 i = 0; i < numPatches; i++)
|
2016-04-02 17:58:06 +02:00
|
|
|
{
|
2016-04-11 05:15:44 +02:00
|
|
|
switch(patches[i].type)
|
|
|
|
{
|
|
|
|
case 0:
|
|
|
|
memcpy((u8 *)firm + patches[i].offset[console], patches[i].patch.type0 + 1, patches[i].patch.type0[0]);
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
*(u16 *)((u8 *)firm + patches[i].offset[console] + 2) = 0;
|
|
|
|
case 1:
|
|
|
|
*(u16 *)((u8 *)firm + patches[i].offset[console]) = patches[i].patch.type1;
|
|
|
|
break;
|
|
|
|
}
|
2016-02-19 21:32:07 +01:00
|
|
|
}
|
2015-08-05 03:57:37 +02:00
|
|
|
}
|
|
|
|
|
2016-05-12 02:59:21 +02:00
|
|
|
static inline void launchFirm(u32 sectionNum, u32 bootType)
|
2016-04-02 17:58:06 +02:00
|
|
|
{
|
2016-04-26 20:06:31 +02:00
|
|
|
//Copy FIRM sections to respective memory locations
|
2016-05-12 02:59:21 +02:00
|
|
|
for(; sectionNum < 4 && section[sectionNum].size; sectionNum++)
|
|
|
|
memcpy(section[sectionNum].address, (u8 *)firm + section[sectionNum].offset, section[sectionNum].size);
|
2016-02-29 16:28:43 +01:00
|
|
|
|
2016-04-11 05:15:44 +02:00
|
|
|
//Determine the ARM11 entry to use
|
|
|
|
vu32 *arm11;
|
|
|
|
if(bootType) arm11 = (u32 *)0x1FFFFFFC;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
deinitScreens();
|
|
|
|
arm11 = (u32 *)0x1FFFFFF8;
|
|
|
|
}
|
2016-03-11 15:08:05 +01:00
|
|
|
|
2016-03-23 02:27:53 +01:00
|
|
|
//Set ARM11 kernel entrypoint
|
2016-04-11 05:15:44 +02:00
|
|
|
*arm11 = (u32)firm->arm11Entry;
|
2016-03-11 15:08:05 +01:00
|
|
|
|
2016-04-24 19:23:40 +02:00
|
|
|
//Final jump to ARM9 kernel
|
2016-03-27 19:19:35 +02:00
|
|
|
((void (*)())firm->arm9Entry)();
|
2016-05-11 19:28:28 +02:00
|
|
|
}
|