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"
|
2016-06-13 21:14:53 +02:00
|
|
|
#include "cache.h"
|
2015-08-05 03:57:37 +02:00
|
|
|
#include "emunand.h"
|
2015-08-21 20:11:23 +02:00
|
|
|
#include "crypto.h"
|
2016-05-03 16:57:00 +02:00
|
|
|
#include "exceptions.h"
|
2016-02-29 16:28:43 +01:00
|
|
|
#include "draw.h"
|
2016-06-10 21:48:22 +02:00
|
|
|
#include "screen.h"
|
2016-03-23 02:27:53 +01:00
|
|
|
#include "buttons.h"
|
2016-05-25 14:34:43 +02:00
|
|
|
#include "../build/injector.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,
|
2016-06-12 22:14:52 +02:00
|
|
|
isN3DS,
|
2016-04-14 17:10:55 +02:00
|
|
|
emuOffset;
|
2016-06-12 22:14:52 +02:00
|
|
|
|
|
|
|
FirmwareSource firmSource;
|
2016-04-11 05:15:44 +02:00
|
|
|
|
|
|
|
void main(void)
|
|
|
|
{
|
2016-06-12 22:14:52 +02:00
|
|
|
u32 isFirmlaunch,
|
2016-04-11 05:15:44 +02:00
|
|
|
updatedSys,
|
|
|
|
newConfig,
|
2016-05-13 05:01:32 +02:00
|
|
|
emuHeader,
|
2016-06-12 22:14:52 +02:00
|
|
|
nbChronoStarted = 0;
|
2016-04-02 17:58:06 +02:00
|
|
|
|
2016-06-12 22:14:52 +02:00
|
|
|
FirmwareType firmType;
|
|
|
|
FirmwareSource nandType;
|
|
|
|
ConfigurationStatus needConfig;
|
|
|
|
A9LHMode a9lhMode;
|
|
|
|
|
2016-02-19 21:32:07 +01:00
|
|
|
//Detect the console being used
|
2016-06-12 22:14:52 +02:00
|
|
|
isN3DS = 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-06-12 22:14:52 +02:00
|
|
|
needConfig = fileRead(&config, configPath) ? MODIFY_CONFIGURATION : CREATE_CONFIGURATION;
|
2016-02-25 20:19:20 +01:00
|
|
|
|
2016-06-13 23:16:33 +02:00
|
|
|
if(DEVMODE)
|
|
|
|
{
|
|
|
|
detectAndProcessExceptionDumps();
|
|
|
|
installArm9Handlers();
|
|
|
|
}
|
|
|
|
|
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-06-12 22:14:52 +02:00
|
|
|
if(needConfig == CREATE_CONFIGURATION) mcuReboot();
|
2016-04-15 15:58:40 +02:00
|
|
|
|
2016-06-12 22:14:52 +02:00
|
|
|
isFirmlaunch = 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-06-12 22:14:52 +02:00
|
|
|
firmType = *(vu8 *)0x23F00009 == '3' ? SAFE_FIRM : (FirmwareType)(*(vu8 *)0x23F00005 - '0');
|
2016-04-02 17:58:06 +02:00
|
|
|
|
2016-06-12 22:14:52 +02:00
|
|
|
nandType = (FirmwareSource)BOOTCONFIG(0, 3);
|
|
|
|
firmSource = (FirmwareSource)BOOTCONFIG(2, 1);
|
|
|
|
a9lhMode = (A9LHMode)BOOTCONFIG(3, 1);
|
|
|
|
updatedSys = a9lhMode != NO_A9LH && 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-06-12 22:14:52 +02:00
|
|
|
if(needConfig == CREATE_CONFIGURATION || ((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-06-12 22:14:52 +02:00
|
|
|
nbChronoStarted = 1;
|
2016-05-13 05:01:32 +02:00
|
|
|
chrono(0);
|
|
|
|
chrono(2);
|
2016-05-11 19:28:28 +02:00
|
|
|
|
2016-05-09 03:41:00 +02:00
|
|
|
//Update pressed buttons
|
|
|
|
pressed = HID_PAD;
|
|
|
|
}
|
|
|
|
|
2016-06-12 22:14:52 +02:00
|
|
|
isFirmlaunch = 0;
|
|
|
|
firmType = NATIVE_FIRM;
|
2016-06-13 23:16:33 +02: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-06-12 22:14:52 +02:00
|
|
|
a9lhMode = A9LH_WITH_NFIRM_FIRMPROT;
|
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-06-12 22:14:52 +02:00
|
|
|
a9lhMode = NO_A9LH;
|
2016-04-11 05:15:44 +02:00
|
|
|
updatedSys = 0;
|
2016-03-05 15:35:06 +01:00
|
|
|
}
|
|
|
|
|
2016-06-12 22:14:52 +02:00
|
|
|
newConfig = (u32)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
|
|
|
//If it's a MCU reboot, try to force boot options
|
2016-06-12 22:14:52 +02:00
|
|
|
if(CFG_BOOTENV)
|
2016-05-03 01:17:22 +02:00
|
|
|
{
|
|
|
|
//Always force a sysNAND boot when quitting AGB_FIRM
|
2016-06-12 22:14:52 +02:00
|
|
|
if(CFG_BOOTENV == 7)
|
2016-05-03 01:17:22 +02:00
|
|
|
{
|
2016-06-12 22:14:52 +02:00
|
|
|
nandType = FIRMWARE_SYSNAND;
|
|
|
|
firmSource = updatedSys ? FIRMWARE_SYSNAND : (FirmwareSource)BOOTCONFIG(2, 1);
|
|
|
|
needConfig = DONT_CONFIGURE;
|
2016-05-03 01:17:22 +02:00
|
|
|
|
|
|
|
//Flag to prevent multiple boot options-forcing
|
|
|
|
newConfig |= 1 << 4;
|
|
|
|
}
|
|
|
|
|
2016-05-27 04:16:49 +02:00
|
|
|
/* Else, force the last used boot options unless a button is pressed
|
2016-05-03 01:17:22 +02:00
|
|
|
or the no-forcing flag is set */
|
2016-05-27 04:16:49 +02:00
|
|
|
else if(!pressed && !BOOTCONFIG(4, 1))
|
2016-05-03 01:17:22 +02:00
|
|
|
{
|
2016-06-12 22:14:52 +02:00
|
|
|
nandType = (FirmwareSource)BOOTCONFIG(0, 3);
|
|
|
|
firmSource = (FirmwareSource)BOOTCONFIG(2, 1);
|
|
|
|
needConfig = DONT_CONFIGURE;
|
2016-05-03 01:17:22 +02:00
|
|
|
}
|
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-06-12 22:14:52 +02:00
|
|
|
a9lhMode = A9LH_WITH_SFIRM_FIRMPROT;
|
|
|
|
nandType = FIRMWARE_SYSNAND;
|
|
|
|
firmSource = FIRMWARE_SYSNAND;
|
|
|
|
needConfig = DONT_CONFIGURE;
|
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 */
|
2016-05-13 09:15:40 +02:00
|
|
|
if(DEVMODE || (pressed & SINGLE_PAYLOAD_BUTTONS) || ((pressed & BUTTON_L1) && (pressed & L_PAYLOAD_BUTTONS)))
|
2016-05-06 20:22:43 +02:00
|
|
|
loadPayload(pressed);
|
2016-03-31 15:57:02 +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-13 09:15:40 +02:00
|
|
|
if((PDN_GPU_CNT != 1 || CONFIG(7)) && loadSplash())
|
2016-05-13 05:01:32 +02:00
|
|
|
{
|
2016-06-12 22:14:52 +02:00
|
|
|
nbChronoStarted = 2;
|
2016-05-13 05:01:32 +02:00
|
|
|
chrono(0);
|
|
|
|
}
|
2016-05-11 19:28:28 +02:00
|
|
|
|
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)
|
|
|
|
{
|
2016-06-12 22:14:52 +02:00
|
|
|
nandType = (updatedSys) ? FIRMWARE_EMUNAND : FIRMWARE_SYSNAND;
|
|
|
|
firmSource = (updatedSys) ? FIRMWARE_SYSNAND : FIRMWARE_EMUNAND;
|
2016-05-01 19:31:58 +02:00
|
|
|
}
|
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
|
|
|
|
{
|
2016-06-12 22:14:52 +02:00
|
|
|
nandType = (CONFIG(0) != !(pressed & BUTTON_L1)) ? FIRMWARE_EMUNAND : FIRMWARE_SYSNAND;
|
2016-05-01 19:31:58 +02:00
|
|
|
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-06-12 22:14:52 +02:00
|
|
|
if(nandType != FIRMWARE_SYSNAND && (CONFIG(3) == !(pressed & BUTTON_B))) nandType = FIRMWARE_EMUNAND2;
|
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
|
2016-06-12 22:14:52 +02:00
|
|
|
if(nandType != FIRMWARE_SYSNAND)
|
2016-04-11 05:15:44 +02:00
|
|
|
{
|
2016-05-25 14:34:43 +02:00
|
|
|
locateEmuNAND(&emuOffset, &emuHeader, &nandType);
|
2016-06-12 22:14:52 +02:00
|
|
|
if(nandType == FIRMWARE_SYSNAND) firmSource = FIRMWARE_SYSNAND;
|
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
|
2016-06-12 22:14:52 +02:00
|
|
|
else if(firmSource != FIRMWARE_SYSNAND)
|
2016-05-25 14:34:43 +02:00
|
|
|
locateEmuNAND(&emuOffset, &emuHeader, &firmSource);
|
2016-03-04 23:43:22 +01:00
|
|
|
|
2016-06-12 22:14:52 +02:00
|
|
|
if(!isFirmlaunch)
|
2016-04-02 17:58:06 +02:00
|
|
|
{
|
2016-06-12 22:14:52 +02:00
|
|
|
newConfig |= (u32)nandType | ((u32)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-06-12 22:14:52 +02:00
|
|
|
loadFirm(firmType, firmType == NATIVE_FIRM && firmSource == ((updatedSys) ? FIRMWARE_SYSNAND : FIRMWARE_EMUNAND));
|
2016-05-06 20:22:43 +02:00
|
|
|
|
2016-05-03 01:17:22 +02:00
|
|
|
switch(firmType)
|
|
|
|
{
|
2016-06-12 22:14:52 +02:00
|
|
|
case NATIVE_FIRM:
|
2016-05-03 01:17:22 +02:00
|
|
|
patchNativeFirm(nandType, emuHeader, a9lhMode);
|
|
|
|
break;
|
2016-06-12 22:14:52 +02:00
|
|
|
case SAFE_FIRM:
|
2016-05-03 01:17:22 +02:00
|
|
|
patchSafeFirm();
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
patchLegacyFirm(firmType);
|
|
|
|
break;
|
|
|
|
}
|
2016-04-11 05:15:44 +02:00
|
|
|
|
2016-06-12 22:14:52 +02:00
|
|
|
if(nbChronoStarted)
|
2016-05-11 19:28:28 +02:00
|
|
|
{
|
2016-06-12 22:14:52 +02:00
|
|
|
if(nbChronoStarted == 2) chrono(3);
|
2016-05-11 19:28:28 +02:00
|
|
|
stopChrono();
|
|
|
|
}
|
|
|
|
|
2016-06-12 22:14:52 +02:00
|
|
|
launchFirm(firmType, isFirmlaunch);
|
2016-02-25 20:19:20 +01:00
|
|
|
}
|
|
|
|
|
2016-06-12 22:14:52 +02:00
|
|
|
static inline void loadFirm(FirmwareType 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-06-12 22:14:52 +02:00
|
|
|
(((u32)section[2].address >> 8) & 0xFF) == (isN3DS ? 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-06-12 22:14:52 +02:00
|
|
|
firmRead(firm, firmFolders[(u32)firmType][isN3DS]);
|
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-06-12 22:14:52 +02:00
|
|
|
static inline void patchNativeFirm(FirmwareSource nandType, u32 emuHeader, A9LHMode a9lhMode)
|
2016-04-03 17:56:09 +02:00
|
|
|
{
|
2016-05-27 14:50:30 +02:00
|
|
|
u8 *arm9Section = (u8 *)firm + section[2].offset,
|
|
|
|
*arm11Section1 = (u8 *)firm + section[1].offset;
|
2016-05-27 14:08:06 +02:00
|
|
|
|
2016-06-12 22:14:52 +02:00
|
|
|
u32 is90Firm;
|
2016-04-11 14:32:38 +02:00
|
|
|
|
2016-06-12 22:14:52 +02:00
|
|
|
if(isN3DS)
|
2016-04-11 05:15:44 +02:00
|
|
|
{
|
2016-06-12 22:14:52 +02:00
|
|
|
u32 a9lVersion;
|
|
|
|
|
|
|
|
//Determine the NATIVE_FIRM/arm9loader version
|
2016-05-05 04:43:44 +02:00
|
|
|
switch(arm9Section[0x53])
|
|
|
|
{
|
|
|
|
case 0xFF:
|
2016-06-12 22:14:52 +02:00
|
|
|
a9lVersion = 0;
|
2016-05-05 04:43:44 +02:00
|
|
|
break;
|
|
|
|
case '1':
|
2016-06-12 22:14:52 +02:00
|
|
|
a9lVersion = 1;
|
2016-05-05 04:43:44 +02:00
|
|
|
break;
|
|
|
|
default:
|
2016-06-12 22:14:52 +02:00
|
|
|
a9lVersion = 2;
|
2016-05-05 04:43:44 +02:00
|
|
|
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-06-12 22:14:52 +02:00
|
|
|
arm9Loader(arm9Section, a9lVersion);
|
2016-04-11 05:15:44 +02:00
|
|
|
firm->arm9Entry = (u8 *)0x801B01C;
|
2016-06-12 22:14:52 +02:00
|
|
|
is90Firm = a9lVersion == 0;
|
2016-04-11 05:15:44 +02:00
|
|
|
}
|
|
|
|
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-06-12 22:14:52 +02:00
|
|
|
is90Firm = 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-12 15:27:22 +02:00
|
|
|
//Find the Process9 .code location, size and memory address
|
2016-05-11 19:28:28 +02:00
|
|
|
u32 process9Size,
|
|
|
|
process9MemAddr;
|
|
|
|
u8 *process9Offset = getProcess9(arm9Section + 0x15000, section[2].size - 0x15000, &process9Size, &process9MemAddr);
|
2016-04-03 17:56:09 +02:00
|
|
|
|
2016-05-25 20:08:37 +02:00
|
|
|
//Apply signature patches
|
|
|
|
patchSignatureChecks(process9Offset, process9Size);
|
2016-05-27 14:08:06 +02:00
|
|
|
|
2016-05-11 19:28:28 +02:00
|
|
|
//Apply emuNAND patches
|
2016-05-25 14:34:43 +02:00
|
|
|
if(nandType)
|
|
|
|
{
|
2016-05-25 20:08:37 +02:00
|
|
|
u32 branchAdditive = (u32)firm + section[2].offset - (u32)section[2].address;
|
2016-05-25 15:26:51 +02:00
|
|
|
patchEmuNAND(arm9Section, section[2].size, process9Offset, process9Size, emuOffset, emuHeader, branchAdditive);
|
2016-05-25 14:34:43 +02:00
|
|
|
}
|
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-06-12 22:14:52 +02:00
|
|
|
else if(a9lhMode != NO_A9LH) patchFirmWrites(process9Offset, process9Size);
|
2016-05-11 19:28:28 +02:00
|
|
|
|
2016-05-25 14:34:43 +02:00
|
|
|
//Apply firmlaunch patches, not on 9.0 FIRM as it breaks firmlaunchhax
|
2016-06-12 22:14:52 +02:00
|
|
|
if(!is90Firm || a9lhMode == A9LH_WITH_SFIRM_FIRMPROT) patchFirmlaunches(process9Offset, process9Size, process9MemAddr);
|
2016-04-03 17:56:09 +02:00
|
|
|
|
2016-06-12 22:14:52 +02:00
|
|
|
if(!is90Firm)
|
2016-05-27 04:16:49 +02:00
|
|
|
{
|
|
|
|
//Apply anti-anti-DG patches for >= 11.0 firmwares
|
|
|
|
patchTitleInstallMinVersionCheck(process9Offset, process9Size);
|
|
|
|
|
|
|
|
//Does nothing if svcBackdoor is still there
|
2016-05-27 14:50:30 +02:00
|
|
|
reimplementSvcBackdoor(arm11Section1, section[1].size);
|
|
|
|
}
|
2016-04-03 17:56:09 +02:00
|
|
|
|
2016-05-13 09:15:40 +02:00
|
|
|
if(DEVMODE)
|
2016-04-26 22:00:29 +02:00
|
|
|
{
|
|
|
|
//Apply UNITINFO patch
|
2016-05-28 16:13:22 +02:00
|
|
|
if(DEVMODE == 2) patchUnitInfoValueSet(arm9Section, section[2].size);
|
2016-06-02 22:33:44 +02:00
|
|
|
|
|
|
|
//Install arm11 exception handlers
|
2016-06-07 19:25:45 +02:00
|
|
|
u32 stackAddress, codeSetOffset;
|
|
|
|
u32 *exceptionsPage = getInfoForArm11ExceptionHandlers(arm11Section1, section[1].size, &stackAddress, &codeSetOffset);
|
|
|
|
installArm11Handlers(exceptionsPage, stackAddress, codeSetOffset);
|
2016-06-02 22:33:44 +02:00
|
|
|
|
2016-06-05 12:08:55 +02:00
|
|
|
//Kernel9/Process9 debugging
|
|
|
|
patchExceptionHandlersInstall(arm9Section, section[2].size);
|
|
|
|
patchSvcBreak9(arm9Section, section[2].size, (u32)(section[2].address));
|
|
|
|
|
2016-06-07 19:25:45 +02:00
|
|
|
//Stub svcBreak11 with "bkpt 65535"
|
2016-06-04 21:11:15 +02:00
|
|
|
patchSvcBreak11(arm11Section1, section[1].size);
|
|
|
|
|
2016-05-08 01:23:22 +02:00
|
|
|
//Make FCRAM (and VRAM as a side effect) globally executable from arm11 kernel
|
2016-05-27 14:50:30 +02:00
|
|
|
patchKernelFCRAMAndVRAMMappingPermissions(arm11Section1, section[1].size);
|
2016-04-26 22:00:29 +02:00
|
|
|
}
|
2015-08-07 09:01:42 +02:00
|
|
|
}
|
|
|
|
|
2016-06-12 22:14:52 +02:00
|
|
|
static inline void patchLegacyFirm(FirmwareType firmType)
|
2016-04-02 17:58:06 +02:00
|
|
|
{
|
2016-06-05 12:08:55 +02:00
|
|
|
u8 *arm9Section = (u8 *)firm + section[3].offset;
|
|
|
|
|
2016-05-25 14:34:43 +02:00
|
|
|
//On N3DS, decrypt ARM9Bin and patch ARM9 entrypoint to skip arm9loader
|
2016-06-12 22:14:52 +02:00
|
|
|
if(isN3DS)
|
2016-05-25 14:34:43 +02:00
|
|
|
{
|
2016-06-05 12:08:55 +02:00
|
|
|
arm9Loader(arm9Section, 0);
|
2016-05-25 14:34:43 +02:00
|
|
|
firm->arm9Entry = (u8 *)0x801301C;
|
|
|
|
}
|
2016-06-05 12:08:55 +02:00
|
|
|
|
|
|
|
if(DEVMODE)
|
|
|
|
{
|
|
|
|
//Kernel9/Process9 debugging
|
|
|
|
patchExceptionHandlersInstall(arm9Section, section[3].size);
|
|
|
|
patchSvcBreak9(arm9Section, section[3].size, (u32)(section[3].address));
|
|
|
|
}
|
2016-04-02 17:58:06 +02:00
|
|
|
|
2016-06-12 22:14:52 +02:00
|
|
|
applyLegacyFirmPatches((u8 *)firm, firmType, isN3DS);
|
2016-06-04 21:11:15 +02:00
|
|
|
}
|
2016-04-02 17:58:06 +02:00
|
|
|
|
2016-05-25 14:34:43 +02:00
|
|
|
static inline void patchSafeFirm(void)
|
2016-05-10 23:06:32 +02:00
|
|
|
{
|
2016-05-25 14:34:43 +02:00
|
|
|
u8 *arm9Section = (u8 *)firm + section[2].offset;
|
2016-05-11 19:28:28 +02:00
|
|
|
|
2016-06-12 22:14:52 +02:00
|
|
|
if(isN3DS)
|
2016-05-11 19:28:28 +02:00
|
|
|
{
|
2016-05-25 14:34:43 +02:00
|
|
|
//Decrypt ARM9Bin and patch ARM9 entrypoint to skip arm9loader
|
|
|
|
arm9Loader(arm9Section, 0);
|
|
|
|
firm->arm9Entry = (u8 *)0x801B01C;
|
2016-05-12 02:59:21 +02:00
|
|
|
|
2016-05-25 14:34:43 +02:00
|
|
|
patchFirmWrites(arm9Section, section[2].size);
|
2016-05-11 19:28:28 +02:00
|
|
|
}
|
2016-06-27 13:21:08 +02:00
|
|
|
else patchFirmWriteSafe(arm9Section, section[2].size);
|
2016-06-05 12:08:55 +02:00
|
|
|
|
|
|
|
if(DEVMODE)
|
|
|
|
{
|
|
|
|
//Kernel9/Process9 debugging
|
|
|
|
patchExceptionHandlersInstall(arm9Section, section[2].size);
|
|
|
|
patchSvcBreak9(arm9Section, section[2].size, (u32)(section[2].address));
|
|
|
|
}
|
2016-05-10 23:06:32 +02:00
|
|
|
}
|
|
|
|
|
2016-06-27 13:21:08 +02:00
|
|
|
static inline void copySection0AndInjectSystemModules(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-06-27 13:21:08 +02:00
|
|
|
char fileName[] = "/luma/sysmodules/--------.cxi";
|
|
|
|
const char *ext = ".cxi";
|
2016-05-12 14:48:52 +02:00
|
|
|
|
2016-06-27 13:21:08 +02:00
|
|
|
struct
|
|
|
|
{
|
|
|
|
u32 size;
|
|
|
|
char name[8];
|
|
|
|
const u8 *addr;
|
|
|
|
} modules[5] = {{0}};
|
|
|
|
|
|
|
|
u8 *pos = arm11Section0;
|
|
|
|
u32 loaderIndex = 0;
|
|
|
|
for(u32 i = 0; i < 5; i++)
|
|
|
|
{
|
|
|
|
modules[i].addr = pos;
|
|
|
|
modules[i].size = *(u32 *)(pos + 0x104) * 0x200;
|
|
|
|
|
|
|
|
memcpy(modules[i].name, pos + 0x200, 8);
|
|
|
|
pos += modules[i].size;
|
|
|
|
|
|
|
|
//Read modules from files if they exist
|
|
|
|
u32 nameOff;
|
|
|
|
for(nameOff = 0; nameOff < 8 && modules[i].name[nameOff] != 0; nameOff++);
|
|
|
|
memcpy(fileName + 17, modules[i].name, nameOff);
|
|
|
|
memcpy(fileName + 17 + nameOff, ext, 5);
|
|
|
|
|
|
|
|
u32 fileSize = getFileSize(fileName);
|
|
|
|
if(fileSize != 0)
|
|
|
|
{
|
|
|
|
modules[i].addr = NULL;
|
|
|
|
modules[i].size = fileSize;
|
|
|
|
}
|
2016-05-09 03:41:00 +02:00
|
|
|
|
2016-06-27 13:21:08 +02:00
|
|
|
if(memcmp(modules[i].name, "loader", 7) == 0) loaderIndex = i;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(modules[loaderIndex].addr != NULL)
|
|
|
|
{
|
|
|
|
modules[loaderIndex].size = injector_size;
|
|
|
|
modules[loaderIndex].addr = injector;
|
|
|
|
}
|
|
|
|
|
|
|
|
pos = section[0].address;
|
|
|
|
for(u32 i = 0; i < 5; i++)
|
|
|
|
{
|
|
|
|
if(modules[i].addr != NULL)
|
|
|
|
memcpy(pos, modules[i].addr, modules[i].size);
|
|
|
|
else
|
|
|
|
{
|
|
|
|
//Read modules from files if they exist
|
|
|
|
u32 nameOff;
|
|
|
|
for(nameOff = 0; nameOff < 8 && modules[i].name[nameOff] != 0; nameOff++);
|
|
|
|
memcpy(fileName + 17, modules[i].name, nameOff);
|
|
|
|
memcpy(fileName + 17 + nameOff, ext, 5);
|
|
|
|
fileRead(pos, fileName);
|
|
|
|
}
|
|
|
|
|
|
|
|
pos += modules[i].size;
|
|
|
|
}
|
2016-04-02 17:58:06 +02:00
|
|
|
}
|
|
|
|
|
2016-06-12 22:14:52 +02:00
|
|
|
static inline void launchFirm(FirmwareType firmType, u32 isFirmlaunch)
|
2016-05-11 19:28:28 +02:00
|
|
|
{
|
2016-05-25 14:34:43 +02:00
|
|
|
//If we're booting NATIVE_FIRM, section0 needs to be copied separately to inject 3ds_injector
|
|
|
|
u32 sectionNum;
|
2016-06-12 22:14:52 +02:00
|
|
|
if(firmType == NATIVE_FIRM)
|
2016-05-11 19:28:28 +02:00
|
|
|
{
|
2016-06-27 13:21:08 +02:00
|
|
|
copySection0AndInjectSystemModules();
|
2016-05-25 14:34:43 +02:00
|
|
|
sectionNum = 1;
|
2016-02-19 21:32:07 +01:00
|
|
|
}
|
2016-05-25 14:34:43 +02:00
|
|
|
else sectionNum = 0;
|
2015-08-05 03:57:37 +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;
|
2016-06-12 22:14:52 +02:00
|
|
|
if(isFirmlaunch) arm11 = (u32 *)0x1FFFFFFC;
|
2016-04-11 05:15:44 +02:00
|
|
|
else
|
|
|
|
{
|
|
|
|
deinitScreens();
|
|
|
|
arm11 = (u32 *)0x1FFFFFF8;
|
|
|
|
}
|
2016-03-11 15:08:05 +01:00
|
|
|
|
2016-06-13 21:14:53 +02:00
|
|
|
flushEntireDCache(); //Ensure that all memory transfers have completed and that the data cache has been flushed
|
2016-06-14 19:50:38 +02:00
|
|
|
flushEntireICache();
|
2016-06-10 21:48:22 +02: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
|
|
|
}
|