2015-08-05 03:57:37 +02:00
|
|
|
/*
|
2016-07-05 16:05:53 +02:00
|
|
|
* This file is part of Luma3DS
|
|
|
|
* Copyright (C) 2016 Aurora Wright, TuxSH
|
|
|
|
*
|
|
|
|
* 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/>.
|
|
|
|
*
|
|
|
|
* Additional Terms 7.b of GPLv3 applies 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.
|
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"
|
2016-09-19 14:03:55 +02:00
|
|
|
#include "exceptions.h"
|
2015-08-05 03:57:37 +02:00
|
|
|
#include "patches.h"
|
|
|
|
#include "memory.h"
|
2016-08-30 21:02:45 +02:00
|
|
|
#include "strings.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-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-08-03 14:13:26 +02:00
|
|
|
#include "pin.h"
|
2016-05-25 14:34:43 +02:00
|
|
|
#include "../build/injector.h"
|
2016-09-11 03:29:39 +02:00
|
|
|
|
2016-08-29 13:45:57 +02:00
|
|
|
extern u16 launchedFirmTidLow[8]; //Defined in start.s
|
2016-08-13 20:49:40 +02:00
|
|
|
|
2016-09-12 16:13:41 +02:00
|
|
|
static firmHeader *firm = (firmHeader *)0x24000000;
|
2016-03-10 14:58:11 +01:00
|
|
|
static const firmSectionHeader *section;
|
2016-04-02 17:58:06 +02:00
|
|
|
|
2016-08-27 16:00:15 +02:00
|
|
|
u32 emuOffset;
|
2016-08-30 02:03:56 +02:00
|
|
|
bool isN3DS,
|
|
|
|
isDevUnit,
|
|
|
|
isFirmlaunch;
|
2016-09-08 02:12:29 +02:00
|
|
|
CfgData configData;
|
2016-06-12 22:14:52 +02:00
|
|
|
FirmwareSource firmSource;
|
2016-04-11 05:15:44 +02:00
|
|
|
|
|
|
|
void main(void)
|
|
|
|
{
|
2016-08-25 18:53:37 +02:00
|
|
|
bool isA9lh;
|
2016-08-26 21:38:03 +02:00
|
|
|
u32 configTemp,
|
2016-08-15 03:51:48 +02:00
|
|
|
emuHeader;
|
2016-06-12 22:14:52 +02:00
|
|
|
FirmwareType firmType;
|
|
|
|
FirmwareSource nandType;
|
|
|
|
ConfigurationStatus needConfig;
|
2016-07-01 20:27:28 +02:00
|
|
|
|
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-08-06 22:38:06 +02:00
|
|
|
//Detect dev units
|
|
|
|
isDevUnit = CFG_UNITINFO != 0;
|
|
|
|
|
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-05-03 01:17:22 +02:00
|
|
|
//Attempt to read the configuration file
|
2016-09-02 14:14:19 +02:00
|
|
|
needConfig = readConfig() ? MODIFY_CONFIGURATION : CREATE_CONFIGURATION;
|
2016-02-25 20:19:20 +01:00
|
|
|
|
2016-09-19 14:57:36 +02:00
|
|
|
u32 devMode = MULTICONFIG(DEVOPTIONS);
|
|
|
|
|
2016-04-11 05:15:44 +02:00
|
|
|
//Determine if this is a firmlaunch boot
|
2016-08-29 13:45:57 +02:00
|
|
|
if(launchedFirmTidLow[5] != 0)
|
2016-04-02 17:58:06 +02:00
|
|
|
{
|
2016-07-02 14:44:01 +02:00
|
|
|
isFirmlaunch = true;
|
2016-03-24 17:27:02 +01:00
|
|
|
|
2016-09-02 14:27:56 +02:00
|
|
|
if(needConfig == CREATE_CONFIGURATION) mcuReboot();
|
|
|
|
|
2016-04-11 05:15:44 +02:00
|
|
|
//'0' = NATIVE_FIRM, '1' = TWL_FIRM, '2' = AGB_FIRM
|
2016-08-29 13:45:57 +02:00
|
|
|
firmType = launchedFirmTidLow[7] == u'3' ? SAFE_FIRM : (FirmwareType)(launchedFirmTidLow[5] - u'0');
|
2016-04-02 17:58:06 +02:00
|
|
|
|
2016-09-04 13:40:46 +02:00
|
|
|
nandType = (FirmwareSource)BOOTCFG_NAND;
|
|
|
|
firmSource = (FirmwareSource)BOOTCFG_FIRM;
|
|
|
|
isA9lh = BOOTCFG_A9LH != 0;
|
2016-04-02 17:58:06 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-07-02 14:44:01 +02:00
|
|
|
isFirmlaunch = false;
|
2016-06-12 22:14:52 +02:00
|
|
|
firmType = NATIVE_FIRM;
|
2016-02-19 21:32:07 +01:00
|
|
|
|
2016-04-11 05:15:44 +02:00
|
|
|
//Determine if booting with A9LH
|
2016-07-18 18:51:04 +02:00
|
|
|
isA9lh = !PDN_SPI_CNT;
|
2016-04-11 05:15:44 +02:00
|
|
|
|
2016-09-19 18:36:47 +02:00
|
|
|
if(devMode != 0 && isA9lh) detectAndProcessExceptionDumps();
|
2016-09-03 02:02:03 +02:00
|
|
|
|
2016-09-03 19:11:44 +02:00
|
|
|
//Get pressed buttons
|
|
|
|
u32 pressed = HID_PAD;
|
|
|
|
|
2016-08-26 21:38:03 +02:00
|
|
|
//Save old options and begin saving the new boot configuration
|
2016-09-13 23:01:38 +02:00
|
|
|
configTemp = (configData.config & 0xFFFFFE00) | ((u32)isA9lh << 6);
|
2016-04-11 05:15:44 +02:00
|
|
|
|
2016-07-18 15:06:35 +02:00
|
|
|
//If it's a MCU reboot, try to force boot options
|
2016-07-18 18:51:04 +02:00
|
|
|
if(isA9lh && CFG_BOOTENV)
|
2016-04-02 17:58:06 +02:00
|
|
|
{
|
2016-07-18 15:06:35 +02:00
|
|
|
//Always force a sysNAND boot when quitting AGB_FIRM
|
|
|
|
if(CFG_BOOTENV == 7)
|
2016-05-03 01:17:22 +02:00
|
|
|
{
|
2016-07-18 15:06:35 +02:00
|
|
|
nandType = FIRMWARE_SYSNAND;
|
2016-09-11 19:17:56 +02:00
|
|
|
firmSource = CONFIG(USESYSFIRM) ? FIRMWARE_SYSNAND : (FirmwareSource)BOOTCFG_FIRM;
|
2016-07-18 15:06:35 +02:00
|
|
|
needConfig = DONT_CONFIGURE;
|
2016-05-03 01:17:22 +02:00
|
|
|
|
2016-07-18 15:06:35 +02:00
|
|
|
//Flag to prevent multiple boot options-forcing
|
2016-09-13 23:01:38 +02:00
|
|
|
configTemp |= 1 << 7;
|
2016-04-11 05:15:44 +02:00
|
|
|
}
|
2016-05-03 01:17:22 +02:00
|
|
|
|
2016-07-18 15:06:35 +02:00
|
|
|
/* Else, force the last used boot options unless a button is pressed
|
|
|
|
or the no-forcing flag is set */
|
2016-09-04 13:40:46 +02:00
|
|
|
else if(needConfig != CREATE_CONFIGURATION && !pressed && !BOOTCFG_NOFORCEFLAG)
|
2016-04-11 05:15:44 +02:00
|
|
|
{
|
2016-09-04 13:40:46 +02:00
|
|
|
nandType = (FirmwareSource)BOOTCFG_NAND;
|
|
|
|
firmSource = (FirmwareSource)BOOTCFG_FIRM;
|
2016-06-12 22:14:52 +02:00
|
|
|
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-09-19 14:03:55 +02:00
|
|
|
if(needConfig == DONT_CONFIGURE)
|
|
|
|
{
|
2016-09-19 14:57:36 +02:00
|
|
|
if(devMode != 0 && isA9lh) installArm9Handlers();
|
2016-09-19 14:03:55 +02:00
|
|
|
}
|
|
|
|
|
2016-04-11 05:15:44 +02:00
|
|
|
//Boot options aren't being forced
|
2016-09-19 14:03:55 +02:00
|
|
|
else
|
2016-04-11 05:15:44 +02:00
|
|
|
{
|
2016-09-19 14:57:36 +02:00
|
|
|
u32 pinMode = MULTICONFIG(PIN);
|
|
|
|
bool pinExists = pinMode != 0 && verifyPin(pinMode);
|
2016-07-18 15:06:35 +02:00
|
|
|
|
2016-08-15 13:23:27 +02:00
|
|
|
//If no configuration file exists or SELECT is held, load configuration menu
|
2016-08-28 13:49:15 +02:00
|
|
|
bool shouldLoadConfigMenu = needConfig == CREATE_CONFIGURATION || ((pressed & BUTTON_SELECT) && !(pressed & BUTTON_L1));
|
2016-07-18 15:06:35 +02:00
|
|
|
|
2016-08-28 13:49:15 +02:00
|
|
|
if(shouldLoadConfigMenu)
|
2016-08-15 13:23:27 +02:00
|
|
|
{
|
2016-09-19 14:57:36 +02:00
|
|
|
configMenu(pinExists, pinMode);
|
2016-08-15 16:50:58 +02:00
|
|
|
|
2016-08-15 13:23:27 +02:00
|
|
|
//Update pressed buttons
|
|
|
|
pressed = HID_PAD;
|
2016-09-19 14:57:36 +02:00
|
|
|
|
|
|
|
devMode = MULTICONFIG(DEVOPTIONS);
|
2016-05-01 19:31:58 +02:00
|
|
|
}
|
2016-04-21 23:39:05 +02:00
|
|
|
|
2016-09-19 14:57:36 +02:00
|
|
|
if(devMode != 0 && isA9lh) installArm9Handlers();
|
2016-09-19 14:03:55 +02:00
|
|
|
|
2016-08-03 16:58:03 +02:00
|
|
|
if(isA9lh && !CFG_BOOTENV && pressed == SAFE_MODE)
|
2016-05-01 19:31:58 +02:00
|
|
|
{
|
2016-08-03 16:58:03 +02:00
|
|
|
nandType = FIRMWARE_SYSNAND;
|
|
|
|
firmSource = FIRMWARE_SYSNAND;
|
2016-08-26 22:24:23 +02:00
|
|
|
|
|
|
|
//Flag to tell loader to init SD
|
2016-09-13 23:01:38 +02:00
|
|
|
configTemp |= 1 << 8;
|
2016-08-29 13:45:57 +02:00
|
|
|
|
|
|
|
//If the PIN has been verified, wait to make it easier to press the SAFE_MODE combo
|
|
|
|
if(pinExists && !shouldLoadConfigMenu)
|
|
|
|
{
|
|
|
|
while(HID_PAD & PIN_BUTTONS);
|
|
|
|
chrono(2);
|
|
|
|
}
|
2016-05-01 19:31:58 +02:00
|
|
|
}
|
|
|
|
else
|
2016-08-15 13:28:43 +02:00
|
|
|
{
|
2016-09-19 14:57:36 +02:00
|
|
|
u32 splashMode = MULTICONFIG(SPLASH);
|
|
|
|
|
|
|
|
if(splashMode == 1 && loadSplash()) pressed = HID_PAD;
|
2016-08-13 22:23:14 +02:00
|
|
|
|
2016-08-03 16:58:03 +02:00
|
|
|
/* If L and R/A/Select or one of the single payload buttons are pressed,
|
2016-08-28 13:49:15 +02:00
|
|
|
chainload an external payload */
|
2016-09-15 23:57:55 +02:00
|
|
|
bool shouldLoadPayload = ((pressed & SINGLE_PAYLOAD_BUTTONS) && !(pressed & (BUTTON_L1 | BUTTON_R1 | BUTTON_A))) ||
|
2016-09-08 00:49:55 +02:00
|
|
|
((pressed & L_PAYLOAD_BUTTONS) && (pressed & BUTTON_L1));
|
2016-08-03 16:58:03 +02:00
|
|
|
|
2016-08-15 03:51:48 +02:00
|
|
|
if(shouldLoadPayload) loadPayload(pressed);
|
2016-08-03 16:58:03 +02:00
|
|
|
|
2016-09-19 14:57:36 +02:00
|
|
|
if(splashMode == 2) loadSplash();
|
2016-08-27 16:00:15 +02:00
|
|
|
|
|
|
|
//Determine if the user chose to use the SysNAND FIRM as default for a R boot
|
2016-09-11 19:17:56 +02:00
|
|
|
bool useSysAsDefault = isA9lh ? CONFIG(USESYSFIRM) : false;
|
2016-04-11 05:15:44 +02:00
|
|
|
|
2016-08-03 16:58:03 +02:00
|
|
|
//If R is pressed, boot the non-updated NAND with the FIRM of the opposite one
|
|
|
|
if(pressed & BUTTON_R1)
|
|
|
|
{
|
2016-08-28 13:49:15 +02:00
|
|
|
nandType = useSysAsDefault ? FIRMWARE_EMUNAND : FIRMWARE_SYSNAND;
|
|
|
|
firmSource = useSysAsDefault ? FIRMWARE_SYSNAND : FIRMWARE_EMUNAND;
|
2016-08-03 16:58:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Else, boot the NAND the user set to autoboot or the opposite one, depending on L,
|
|
|
|
with their own FIRM */
|
|
|
|
else
|
|
|
|
{
|
2016-09-11 19:17:56 +02:00
|
|
|
nandType = (CONFIG(AUTOBOOTSYS) != !(pressed & BUTTON_L1)) ? FIRMWARE_EMUNAND : FIRMWARE_SYSNAND;
|
2016-08-03 16:58:03 +02:00
|
|
|
firmSource = nandType;
|
|
|
|
}
|
|
|
|
|
2016-09-13 23:01:38 +02:00
|
|
|
//If we're booting EmuNAND or using EmuNAND FIRM, determine which one from the directional pad buttons, or otherwise from the config
|
|
|
|
if(nandType == FIRMWARE_EMUNAND || firmSource == FIRMWARE_EMUNAND)
|
|
|
|
{
|
|
|
|
FirmwareSource temp;
|
2016-09-08 00:49:55 +02:00
|
|
|
switch(pressed & EMUNAND_BUTTONS)
|
|
|
|
{
|
|
|
|
case BUTTON_UP:
|
2016-09-13 23:01:38 +02:00
|
|
|
temp = FIRMWARE_EMUNAND;
|
2016-09-08 00:49:55 +02:00
|
|
|
break;
|
|
|
|
case BUTTON_RIGHT:
|
2016-09-13 23:01:38 +02:00
|
|
|
temp = FIRMWARE_EMUNAND2;
|
2016-09-08 00:49:55 +02:00
|
|
|
break;
|
|
|
|
case BUTTON_DOWN:
|
2016-09-13 23:01:38 +02:00
|
|
|
temp = FIRMWARE_EMUNAND3;
|
2016-09-08 00:49:55 +02:00
|
|
|
break;
|
|
|
|
case BUTTON_LEFT:
|
2016-09-13 23:01:38 +02:00
|
|
|
temp = FIRMWARE_EMUNAND4;
|
2016-09-08 00:49:55 +02:00
|
|
|
break;
|
|
|
|
default:
|
2016-09-13 23:01:38 +02:00
|
|
|
temp = (FirmwareSource)(1 + MULTICONFIG(DEFAULTEMU));
|
2016-09-08 00:49:55 +02:00
|
|
|
break;
|
|
|
|
}
|
2016-09-13 23:01:38 +02:00
|
|
|
|
|
|
|
if(nandType == FIRMWARE_EMUNAND) nandType = temp;
|
|
|
|
else firmSource = temp;
|
|
|
|
}
|
2016-05-01 19:31:58 +02:00
|
|
|
}
|
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-09-08 02:12:29 +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-09-07 18:04:31 +02:00
|
|
|
locateEmuNand(&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-09-08 02:12:29 +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-09-07 18:04:31 +02:00
|
|
|
locateEmuNand(&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-09-08 00:49:55 +02:00
|
|
|
configTemp |= (u32)nandType | ((u32)firmSource << 3);
|
2016-09-02 14:14:19 +02:00
|
|
|
writeConfig(needConfig, configTemp);
|
2016-03-31 15:57:02 +02:00
|
|
|
}
|
2016-04-11 05:15:44 +02:00
|
|
|
|
2016-09-19 14:03:55 +02:00
|
|
|
bool loadFromSd = CONFIG(SDFIRMSANDMODULES);
|
|
|
|
u32 firmVersion = loadFirm(&firmType, firmSource, loadFromSd);
|
2016-04-11 05:15:44 +02:00
|
|
|
|
2016-05-03 01:17:22 +02:00
|
|
|
switch(firmType)
|
|
|
|
{
|
2016-06-12 22:14:52 +02:00
|
|
|
case NATIVE_FIRM:
|
2016-09-19 14:57:36 +02:00
|
|
|
patchNativeFirm(firmVersion, nandType, emuHeader, isA9lh, devMode);
|
2016-05-03 01:17:22 +02:00
|
|
|
break;
|
2016-06-12 22:14:52 +02:00
|
|
|
case SAFE_FIRM:
|
2016-08-31 19:30:45 +02:00
|
|
|
case NATIVE_FIRM1X2X:
|
2016-09-19 14:57:36 +02:00
|
|
|
if(isA9lh) patch1x2xNativeAndSafeFirm(devMode);
|
2016-05-03 01:17:22 +02:00
|
|
|
break;
|
|
|
|
default:
|
2016-09-19 14:57:36 +02:00
|
|
|
patchLegacyFirm(firmType, firmVersion, devMode);
|
2016-05-03 01:17:22 +02:00
|
|
|
break;
|
|
|
|
}
|
2016-04-11 05:15:44 +02:00
|
|
|
|
2016-09-19 14:03:55 +02:00
|
|
|
launchFirm(firmType, loadFromSd);
|
2016-02-25 20:19:20 +01:00
|
|
|
}
|
|
|
|
|
2016-09-19 14:03:55 +02:00
|
|
|
static inline u32 loadFirm(FirmwareType *firmType, FirmwareSource firmSource, bool loadFromSd)
|
2016-04-02 17:58:06 +02:00
|
|
|
{
|
2016-04-11 05:15:44 +02:00
|
|
|
section = firm->section;
|
2016-08-30 02:03:56 +02:00
|
|
|
|
2016-09-18 23:50:44 +02:00
|
|
|
const char *firmwareFiles[] = {
|
2016-08-04 13:44:51 +02:00
|
|
|
"/luma/firmware.bin",
|
|
|
|
"/luma/firmware_twl.bin",
|
|
|
|
"/luma/firmware_agb.bin",
|
|
|
|
"/luma/firmware_safe.bin"
|
2016-09-18 23:50:44 +02:00
|
|
|
},
|
|
|
|
*cetkFiles[] = {
|
|
|
|
"/luma/cetk",
|
|
|
|
"/luma/cetk_twl",
|
|
|
|
"/luma/cetk_agb",
|
|
|
|
"/luma/cetk_safe"
|
2016-08-04 13:44:51 +02:00
|
|
|
};
|
2016-04-04 18:19:00 +02:00
|
|
|
|
2016-09-08 22:33:25 +02:00
|
|
|
//Load FIRM from CTRNAND
|
2016-09-02 15:05:52 +02:00
|
|
|
u32 firmVersion = firmRead(firm, (u32)*firmType);
|
2016-09-08 22:33:25 +02:00
|
|
|
|
2016-09-19 14:03:55 +02:00
|
|
|
bool mustLoadFromSd = false;
|
2016-04-28 16:11:25 +02:00
|
|
|
|
2016-09-02 15:05:52 +02:00
|
|
|
if(!isN3DS && *firmType == NATIVE_FIRM)
|
2016-08-04 13:44:51 +02:00
|
|
|
{
|
2016-09-02 15:05:52 +02:00
|
|
|
if(firmVersion < 0x18)
|
2016-08-27 13:11:55 +02:00
|
|
|
{
|
2016-09-02 15:15:22 +02:00
|
|
|
//We can't boot < 3.x EmuNANDs
|
2016-09-02 15:05:52 +02:00
|
|
|
if(firmSource != FIRMWARE_SYSNAND)
|
|
|
|
error("An old unsupported EmuNAND has been detected.\nLuma3DS is unable to boot it");
|
2016-08-28 02:38:30 +02:00
|
|
|
|
2016-09-04 14:05:29 +02:00
|
|
|
if(BOOTCFG_SAFEMODE != 0) error("SAFE_MODE is not supported on 1.x/2.x FIRM");
|
2016-08-27 00:34:25 +02:00
|
|
|
|
2016-09-02 15:05:52 +02:00
|
|
|
*firmType = NATIVE_FIRM1X2X;
|
2016-08-27 13:11:55 +02:00
|
|
|
}
|
2016-07-18 16:58:29 +02:00
|
|
|
|
2016-09-08 22:33:25 +02:00
|
|
|
//We can't boot a 3.x/4.x NATIVE_FIRM, load one from SD
|
2016-09-19 14:03:55 +02:00
|
|
|
else if(firmVersion < 0x25) mustLoadFromSd = true;
|
2016-09-02 15:05:52 +02:00
|
|
|
}
|
|
|
|
|
2016-09-19 14:03:55 +02:00
|
|
|
if(loadFromSd || mustLoadFromSd)
|
2016-09-18 23:50:44 +02:00
|
|
|
{
|
2016-09-19 14:03:55 +02:00
|
|
|
u32 firmSize = fileRead(firm, *firmType == NATIVE_FIRM1X2X ? firmwareFiles[0] : firmwareFiles[(u32)*firmType], 0x400000);
|
|
|
|
|
|
|
|
if(firmSize > 0)
|
2016-09-18 23:50:44 +02:00
|
|
|
{
|
2016-09-19 14:03:55 +02:00
|
|
|
if(memcmp(firm, "FIRM", 4) != 0)
|
|
|
|
{
|
|
|
|
u8 cetk[0xA50];
|
2016-09-18 23:50:44 +02:00
|
|
|
|
2016-09-19 14:03:55 +02:00
|
|
|
if(fileRead(cetk, *firmType == NATIVE_FIRM1X2X ? cetkFiles[0] : cetkFiles[(u32)*firmType], sizeof(cetk)))
|
|
|
|
decryptNusFirm(cetk, (u8 *)firm, firmSize);
|
|
|
|
}
|
2016-09-18 23:50:44 +02:00
|
|
|
|
2016-09-19 14:03:55 +02:00
|
|
|
//Check that the SD FIRM is right for the console from the ARM9 section address
|
|
|
|
if((section[3].offset ? section[3].address : section[2].address) != (isN3DS ? (u8 *)0x8006000 : (u8 *)0x8006800))
|
2016-09-19 17:07:56 +02:00
|
|
|
{
|
|
|
|
if(isFirmlaunch) mcuReboot();
|
|
|
|
error("The firmware.bin in /luma is not valid for your\nconsole, or corrupted");
|
|
|
|
}
|
2016-09-18 23:50:44 +02:00
|
|
|
|
2016-09-19 14:03:55 +02:00
|
|
|
firmVersion = 0xFFFFFFFF;
|
|
|
|
}
|
2016-09-18 23:50:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if(firmVersion != 0xFFFFFFFF)
|
2016-09-02 15:05:52 +02:00
|
|
|
{
|
2016-09-19 14:03:55 +02:00
|
|
|
if(mustLoadFromSd) error("An old unsupported FIRM has been detected.\nCopy a firmware.bin in /luma to boot");
|
2016-08-30 02:03:56 +02:00
|
|
|
decryptExeFs((u8 *)firm);
|
|
|
|
}
|
2016-09-08 22:33:25 +02:00
|
|
|
|
2016-07-18 16:58:29 +02:00
|
|
|
return firmVersion;
|
2015-08-05 11:54:00 +02:00
|
|
|
}
|
|
|
|
|
2016-09-19 14:57:36 +02:00
|
|
|
static inline void patchNativeFirm(u32 firmVersion, FirmwareSource nandType, u32 emuHeader, bool isA9lh, u32 devMode)
|
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
|
|
|
if(isN3DS)
|
2016-04-11 05:15:44 +02:00
|
|
|
{
|
2016-09-20 14:29:26 +02:00
|
|
|
//Decrypt ARM9Bin and patch ARM9 entrypoint to skip kernel9loader
|
|
|
|
kernel9Loader(arm9Section);
|
2016-04-11 05:15:44 +02:00
|
|
|
firm->arm9Entry = (u8 *)0x801B01C;
|
|
|
|
}
|
2016-04-03 17:56:09 +02:00
|
|
|
|
2016-07-18 16:58:29 +02:00
|
|
|
//Sets the 7.x NCCH KeyX and the 6.x gamecard save data KeyY on >= 6.0 O3DS FIRMs, if not using A9LH
|
2016-09-19 00:14:32 +02:00
|
|
|
else if(!isA9lh && firmVersion >= 0x29) set6x7xKeys();
|
2016-07-18 15:06:35 +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-09-03 18:00:22 +02:00
|
|
|
//Find Kernel11 SVC table and handler, exceptions page and free space locations
|
2016-09-10 21:12:53 +02:00
|
|
|
u32 baseK11VA;
|
2016-08-29 15:35:24 +02:00
|
|
|
u8 *freeK11Space;
|
2016-09-18 18:31:16 +02:00
|
|
|
u32 *arm11SvcHandler,
|
2016-09-03 15:41:45 +02:00
|
|
|
*arm11ExceptionsPage,
|
2016-09-10 21:35:23 +02:00
|
|
|
*arm11SvcTable = getKernel11Info(arm11Section1, section[1].size, &baseK11VA, &freeK11Space, &arm11SvcHandler, &arm11ExceptionsPage);
|
2016-08-29 15:35:24 +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-09-08 02:12:29 +02:00
|
|
|
//Apply EmuNAND patches
|
2016-07-02 14:44:01 +02:00
|
|
|
if(nandType != FIRMWARE_SYSNAND)
|
2016-05-25 14:34:43 +02:00
|
|
|
{
|
2016-05-25 20:08:37 +02:00
|
|
|
u32 branchAdditive = (u32)firm + section[2].offset - (u32)section[2].address;
|
2016-08-29 15:35:24 +02:00
|
|
|
patchEmuNand(arm9Section, section[2].size, process9Offset, process9Size, 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-07-18 15:32:32 +02:00
|
|
|
else if(isA9lh) patchFirmWrites(process9Offset, process9Size);
|
2016-05-11 19:28:28 +02:00
|
|
|
|
2016-07-19 20:03:35 +02:00
|
|
|
//Apply firmlaunch patches
|
2016-07-18 15:32:32 +02:00
|
|
|
patchFirmlaunches(process9Offset, process9Size, process9MemAddr);
|
2016-04-03 17:56:09 +02:00
|
|
|
|
2016-07-18 16:58:29 +02:00
|
|
|
//11.0 FIRM patches
|
|
|
|
if(firmVersion >= (isN3DS ? 0x21 : 0x52))
|
2016-05-27 04:16:49 +02:00
|
|
|
{
|
2016-07-18 16:58:29 +02:00
|
|
|
//Apply anti-anti-DG patches
|
2016-05-27 04:16:49 +02:00
|
|
|
patchTitleInstallMinVersionCheck(process9Offset, process9Size);
|
|
|
|
|
2016-08-03 14:13:26 +02:00
|
|
|
//Restore svcBackdoor
|
2016-09-10 21:12:53 +02:00
|
|
|
reimplementSvcBackdoor(arm11Section1, arm11SvcTable, baseK11VA, &freeK11Space);
|
2016-05-27 14:50:30 +02:00
|
|
|
}
|
2016-04-03 17:56:09 +02:00
|
|
|
|
2016-09-10 21:12:53 +02:00
|
|
|
implementSvcGetCFWInfo(arm11Section1, arm11SvcTable, baseK11VA, &freeK11Space);
|
2016-08-30 02:03:56 +02:00
|
|
|
|
2016-08-13 15:31:08 +02:00
|
|
|
//Apply UNITINFO patch
|
2016-09-19 14:57:36 +02:00
|
|
|
if(devMode == 2) patchUnitInfoValueSet(arm9Section, section[2].size);
|
2016-09-03 02:02:03 +02:00
|
|
|
|
2016-09-19 14:57:36 +02:00
|
|
|
if(devMode != 0 && isA9lh)
|
2016-08-23 20:39:42 +02:00
|
|
|
{
|
2016-09-19 14:03:55 +02:00
|
|
|
//ARM11 exception handlers
|
|
|
|
u32 codeSetOffset,
|
|
|
|
stackAddress = getInfoForArm11ExceptionHandlers(arm11Section1, section[1].size, &codeSetOffset);
|
2016-08-29 20:12:23 +02:00
|
|
|
installArm11Handlers(arm11ExceptionsPage, stackAddress, codeSetOffset);
|
2016-09-19 14:03:55 +02:00
|
|
|
patchSvcBreak11(arm11Section1, arm11SvcTable);
|
|
|
|
patchKernel11Panic(arm11Section1, section[1].size);
|
2016-08-30 02:03:56 +02:00
|
|
|
|
2016-09-19 14:03:55 +02:00
|
|
|
//ARM9 exception handlers
|
2016-08-29 20:12:23 +02:00
|
|
|
patchArm9ExceptionHandlersInstall(arm9Section, section[2].size);
|
2016-08-30 02:03:56 +02:00
|
|
|
patchSvcBreak9(arm9Section, section[2].size, (u32)section[2].address);
|
2016-09-02 22:43:15 +02:00
|
|
|
patchKernel9Panic(arm9Section, section[2].size);
|
2016-08-23 20:39:42 +02:00
|
|
|
}
|
|
|
|
|
2016-09-11 19:17:56 +02:00
|
|
|
if(CONFIG(PATCHACCESS))
|
2016-08-04 20:10:31 +02:00
|
|
|
{
|
2016-08-29 20:12:23 +02:00
|
|
|
patchArm11SvcAccessChecks(arm11SvcHandler);
|
2016-09-03 18:00:22 +02:00
|
|
|
patchK11ModuleChecks(arm11Section1, section[1].size, &freeK11Space);
|
2016-08-23 20:39:42 +02:00
|
|
|
patchP9AccessChecks(process9Offset, process9Size);
|
2016-08-04 20:10:31 +02:00
|
|
|
}
|
2015-08-07 09:01:42 +02:00
|
|
|
}
|
|
|
|
|
2016-09-19 14:57:36 +02:00
|
|
|
static inline void patchLegacyFirm(FirmwareType firmType, u32 firmVersion, u32 devMode)
|
2016-04-02 17:58:06 +02:00
|
|
|
{
|
2016-06-05 12:08:55 +02:00
|
|
|
u8 *arm9Section = (u8 *)firm + section[3].offset;
|
|
|
|
|
2016-09-20 14:29:26 +02:00
|
|
|
//On N3DS, decrypt ARM9Bin and patch ARM9 entrypoint to skip kernel9loader
|
2016-06-12 22:14:52 +02:00
|
|
|
if(isN3DS)
|
2016-05-25 14:34:43 +02:00
|
|
|
{
|
2016-09-20 14:29:26 +02:00
|
|
|
kernel9Loader(arm9Section);
|
2016-05-25 14:34:43 +02:00
|
|
|
firm->arm9Entry = (u8 *)0x801301C;
|
|
|
|
}
|
2016-09-03 02:02:03 +02:00
|
|
|
|
2016-09-19 14:57:36 +02:00
|
|
|
if(isN3DS || firmVersion >= (firmType == TWL_FIRM ? 0x16 : 0xB))
|
|
|
|
applyLegacyFirmPatches((u8 *)firm, firmType);
|
2016-09-08 18:47:13 +02:00
|
|
|
|
2016-08-23 20:39:42 +02:00
|
|
|
//Apply UNITINFO patch
|
2016-09-19 14:57:36 +02:00
|
|
|
if(devMode == 2) patchUnitInfoValueSet(arm9Section, section[3].size);
|
2016-06-04 21:11:15 +02:00
|
|
|
}
|
2016-04-02 17:58:06 +02:00
|
|
|
|
2016-09-19 14:57:36 +02:00
|
|
|
static inline void patch1x2xNativeAndSafeFirm(u32 devMode)
|
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-09-20 14:29:26 +02:00
|
|
|
//Decrypt ARM9Bin and patch ARM9 entrypoint to skip kernel9loader
|
|
|
|
kernel9Loader(arm9Section);
|
2016-05-25 14:34:43 +02:00
|
|
|
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-08-28 13:49:15 +02:00
|
|
|
else patchOldFirmWrites(arm9Section, section[2].size);
|
2016-08-23 20:39:42 +02:00
|
|
|
|
2016-09-19 14:57:36 +02:00
|
|
|
if(devMode != 0)
|
2016-08-23 20:39:42 +02:00
|
|
|
{
|
2016-09-19 14:03:55 +02:00
|
|
|
//ARM9 exception handlers
|
2016-08-29 20:12:23 +02:00
|
|
|
patchArm9ExceptionHandlersInstall(arm9Section, section[2].size);
|
2016-08-30 02:03:56 +02:00
|
|
|
patchSvcBreak9(arm9Section, section[2].size, (u32)section[2].address);
|
2016-08-23 20:39:42 +02:00
|
|
|
}
|
2016-05-10 23:06:32 +02:00
|
|
|
}
|
|
|
|
|
2016-09-19 14:03:55 +02:00
|
|
|
static inline void copySection0AndInjectSystemModules(FirmwareType firmType, bool loadFromSd)
|
2016-04-02 17:58:06 +02:00
|
|
|
{
|
2016-08-30 02:03:56 +02:00
|
|
|
u32 srcModuleSize,
|
|
|
|
dstModuleSize;
|
2016-05-12 14:48:52 +02:00
|
|
|
|
2016-08-30 02:03:56 +02:00
|
|
|
for(u8 *src = (u8 *)firm + section[0].offset, *srcEnd = src + section[0].size, *dst = section[0].address;
|
|
|
|
src < srcEnd; src += srcModuleSize, dst += dstModuleSize)
|
2016-06-27 13:21:08 +02:00
|
|
|
{
|
2016-08-30 02:03:56 +02:00
|
|
|
srcModuleSize = *(u32 *)(src + 0x104) * 0x200;
|
2016-09-12 16:13:41 +02:00
|
|
|
const char *moduleName = (char *)(src + 0x200);
|
2016-06-27 13:21:08 +02:00
|
|
|
|
2016-09-19 14:03:55 +02:00
|
|
|
u32 fileSize;
|
2016-08-12 15:17:19 +02:00
|
|
|
|
2016-09-19 14:03:55 +02:00
|
|
|
if(loadFromSd)
|
|
|
|
{
|
|
|
|
char fileName[30] = "/luma/sysmodules/";
|
|
|
|
const char *ext = ".cxi";
|
2016-06-27 13:21:08 +02:00
|
|
|
|
2016-09-19 14:03:55 +02:00
|
|
|
//Read modules from files if they exist
|
|
|
|
concatenateStrings(fileName, moduleName);
|
|
|
|
concatenateStrings(fileName, ext);
|
|
|
|
|
|
|
|
fileSize = fileRead(dst, fileName, 2 * srcModuleSize);
|
|
|
|
}
|
|
|
|
else fileSize = 0;
|
|
|
|
|
|
|
|
if(fileSize > 0) dstModuleSize = fileSize;
|
2016-06-27 13:21:08 +02:00
|
|
|
else
|
|
|
|
{
|
2016-09-12 16:13:41 +02:00
|
|
|
const void *module;
|
2016-08-30 02:03:56 +02:00
|
|
|
|
|
|
|
if(firmType == NATIVE_FIRM && memcmp(moduleName, "loader", 6) == 0)
|
|
|
|
{
|
2016-09-12 16:13:41 +02:00
|
|
|
module = injector;
|
2016-08-30 02:03:56 +02:00
|
|
|
dstModuleSize = injector_size;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
module = src;
|
|
|
|
dstModuleSize = srcModuleSize;
|
|
|
|
}
|
|
|
|
|
|
|
|
memcpy(dst, module, dstModuleSize);
|
|
|
|
}
|
2016-06-27 13:21:08 +02:00
|
|
|
}
|
2016-04-02 17:58:06 +02:00
|
|
|
}
|
2016-09-08 18:47:13 +02:00
|
|
|
|
2016-09-19 14:03:55 +02:00
|
|
|
static inline void launchFirm(FirmwareType firmType, bool loadFromSd)
|
2016-05-11 19:28:28 +02:00
|
|
|
{
|
2016-09-11 03:29:39 +02:00
|
|
|
//Allow module injection and/or inject 3ds_injector on new NATIVE_FIRMs and LGY FIRMs
|
2016-05-25 14:34:43 +02:00
|
|
|
u32 sectionNum;
|
2016-09-19 14:03:55 +02:00
|
|
|
if(firmType == NATIVE_FIRM || (loadFromSd && firmType != SAFE_FIRM && firmType != NATIVE_FIRM1X2X))
|
2016-09-11 03:29:39 +02:00
|
|
|
{
|
2016-09-19 14:03:55 +02:00
|
|
|
copySection0AndInjectSystemModules(firmType, loadFromSd);
|
2016-09-11 03:29:39 +02:00
|
|
|
sectionNum = 1;
|
|
|
|
}
|
|
|
|
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-09-14 22:31:25 +02:00
|
|
|
for(; sectionNum < 4 && section[sectionNum].size != 0; sectionNum++)
|
2016-05-12 02:59:21 +02:00
|
|
|
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-09-18 20:10:24 +02:00
|
|
|
if(isFirmlaunch) arm11 = (vu32 *)0x1FFFFFFC;
|
2016-04-11 05:15:44 +02:00
|
|
|
else
|
|
|
|
{
|
|
|
|
deinitScreens();
|
2016-09-18 20:10:24 +02:00
|
|
|
arm11 = (vu32 *)BRAHMA_ARM11_ENTRY;
|
2016-04-11 05:15:44 +02:00
|
|
|
}
|
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-09-15 19:53:51 +02:00
|
|
|
//Ensure that all memory transfers have completed and that the caches have been flushed
|
|
|
|
flushEntireDCache();
|
2016-07-18 16:58:29 +02:00
|
|
|
flushEntireICache();
|
|
|
|
|
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-08-30 02:03:56 +02:00
|
|
|
}
|