2016-10-10 01:34: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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
#include "emunand.h"
|
|
|
|
#include "fs.h"
|
|
|
|
#include "firm.h"
|
|
|
|
#include "utils.h"
|
|
|
|
#include "exceptions.h"
|
|
|
|
#include "draw.h"
|
2016-10-10 15:53:56 +02:00
|
|
|
#include "strings.h"
|
2016-10-10 01:34:53 +02:00
|
|
|
#include "buttons.h"
|
|
|
|
#include "pin.h"
|
2017-02-08 12:34:07 +01:00
|
|
|
#include "crypto.h"
|
2017-05-07 17:58:44 +02:00
|
|
|
#include "fmt.h"
|
2016-10-10 01:34:53 +02:00
|
|
|
|
|
|
|
extern CfgData configData;
|
2017-04-13 03:38:36 +02:00
|
|
|
extern ConfigurationStatus needConfig;
|
2016-10-10 01:34:53 +02:00
|
|
|
extern FirmwareSource firmSource;
|
|
|
|
|
|
|
|
void main(void)
|
|
|
|
{
|
2016-11-03 18:55:40 +01:00
|
|
|
bool isA9lhInstalled,
|
2017-04-14 14:54:31 +02:00
|
|
|
isSafeMode = false,
|
|
|
|
isNoForceFlagSet = false;
|
2017-04-13 03:38:36 +02:00
|
|
|
u32 emuHeader;
|
2016-10-10 01:34:53 +02:00
|
|
|
FirmwareType firmType;
|
|
|
|
FirmwareSource nandType;
|
|
|
|
|
|
|
|
//Mount SD or CTRNAND
|
|
|
|
bool isSdMode;
|
2016-10-12 02:28:08 +02:00
|
|
|
if(mountFs(true, false)) isSdMode = true;
|
2016-10-10 01:34:53 +02:00
|
|
|
else
|
|
|
|
{
|
|
|
|
firmSource = FIRMWARE_SYSNAND;
|
2016-10-12 02:28:08 +02:00
|
|
|
if(!mountFs(false, true)) error("Failed to mount SD and CTRNAND.");
|
2016-10-10 01:34:53 +02:00
|
|
|
isSdMode = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
//Attempt to read the configuration file
|
|
|
|
needConfig = readConfig() ? MODIFY_CONFIGURATION : CREATE_CONFIGURATION;
|
|
|
|
|
|
|
|
//Determine if this is a firmlaunch boot
|
|
|
|
if(ISFIRMLAUNCH)
|
|
|
|
{
|
2016-10-13 18:45:38 +02:00
|
|
|
if(needConfig == CREATE_CONFIGURATION) mcuPowerOff();
|
2016-10-10 01:34:53 +02:00
|
|
|
|
2016-11-12 01:49:52 +01:00
|
|
|
switch(launchedFirmTidLow[7])
|
|
|
|
{
|
|
|
|
case u'2':
|
|
|
|
firmType = (FirmwareType)(launchedFirmTidLow[5] - u'0');
|
|
|
|
break;
|
|
|
|
case u'3':
|
|
|
|
firmType = SAFE_FIRM;
|
|
|
|
break;
|
|
|
|
case u'1':
|
|
|
|
firmType = SYSUPDATER_FIRM;
|
|
|
|
break;
|
|
|
|
}
|
2016-10-10 01:34:53 +02:00
|
|
|
|
|
|
|
nandType = (FirmwareSource)BOOTCFG_NAND;
|
|
|
|
firmSource = (FirmwareSource)BOOTCFG_FIRM;
|
2016-10-13 15:19:37 +02:00
|
|
|
isA9lhInstalled = BOOTCFG_A9LH != 0;
|
2016-11-15 19:29:48 +01:00
|
|
|
|
|
|
|
goto boot;
|
2016-10-10 01:34:53 +02:00
|
|
|
}
|
2016-11-15 19:29:48 +01:00
|
|
|
|
|
|
|
if(ISA9LH)
|
|
|
|
{
|
|
|
|
detectAndProcessExceptionDumps();
|
|
|
|
installArm9Handlers();
|
|
|
|
}
|
|
|
|
|
|
|
|
firmType = NATIVE_FIRM;
|
|
|
|
isA9lhInstalled = ISA9LH;
|
|
|
|
|
|
|
|
//Get pressed buttons
|
|
|
|
u32 pressed = HID_PAD;
|
|
|
|
|
|
|
|
//If it's a MCU reboot, try to force boot options
|
|
|
|
if(ISA9LH && CFG_BOOTENV && needConfig != CREATE_CONFIGURATION)
|
|
|
|
{
|
2017-04-13 02:49:19 +02:00
|
|
|
|
2016-11-15 19:29:48 +01:00
|
|
|
//Always force a SysNAND boot when quitting AGB_FIRM
|
|
|
|
if(CFG_BOOTENV == 7)
|
|
|
|
{
|
|
|
|
nandType = FIRMWARE_SYSNAND;
|
|
|
|
firmSource = (BOOTCFG_NAND != 0) == (BOOTCFG_FIRM != 0) ? FIRMWARE_SYSNAND : (FirmwareSource)BOOTCFG_FIRM;
|
|
|
|
|
2017-04-14 14:54:31 +02:00
|
|
|
//Prevent multiple boot options-forcing
|
|
|
|
isNoForceFlagSet = true;
|
2016-11-15 19:29:48 +01:00
|
|
|
|
|
|
|
goto boot;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Else, force the last used boot options unless a button is pressed
|
|
|
|
or the no-forcing flag is set */
|
|
|
|
if(!pressed && !BOOTCFG_NOFORCEFLAG)
|
|
|
|
{
|
|
|
|
nandType = (FirmwareSource)BOOTCFG_NAND;
|
|
|
|
firmSource = (FirmwareSource)BOOTCFG_FIRM;
|
|
|
|
|
|
|
|
goto boot;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
u32 pinMode = MULTICONFIG(PIN);
|
2017-04-15 15:58:07 +02:00
|
|
|
bool pinExists = pinMode != 0 && verifyPin(pinMode);
|
2016-11-15 19:29:48 +01:00
|
|
|
|
|
|
|
//If no configuration file exists or SELECT is held, load configuration menu
|
|
|
|
bool shouldLoadConfigMenu = needConfig == CREATE_CONFIGURATION || ((pressed & (BUTTON_SELECT | BUTTON_L1)) == BUTTON_SELECT);
|
|
|
|
|
|
|
|
if(shouldLoadConfigMenu)
|
|
|
|
{
|
|
|
|
configMenu(isSdMode, pinExists, pinMode);
|
|
|
|
|
|
|
|
//Update pressed buttons
|
|
|
|
pressed = HID_PAD;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(ISA9LH && !CFG_BOOTENV && pressed == SAFE_MODE)
|
2016-10-10 01:34:53 +02:00
|
|
|
{
|
2016-11-15 19:29:48 +01:00
|
|
|
nandType = FIRMWARE_SYSNAND;
|
|
|
|
firmSource = FIRMWARE_SYSNAND;
|
|
|
|
|
|
|
|
isSafeMode = true;
|
|
|
|
|
|
|
|
//If the PIN has been verified, wait to make it easier to press the SAFE_MODE combo
|
|
|
|
if(pinExists && !shouldLoadConfigMenu)
|
2016-10-10 01:34:53 +02:00
|
|
|
{
|
2016-11-15 19:29:48 +01:00
|
|
|
while(HID_PAD & PIN_BUTTONS);
|
2016-11-29 20:11:30 +01:00
|
|
|
wait(2000ULL);
|
2016-10-10 01:34:53 +02:00
|
|
|
}
|
|
|
|
|
2016-11-15 19:29:48 +01:00
|
|
|
goto boot;
|
|
|
|
}
|
|
|
|
|
|
|
|
u32 splashMode = MULTICONFIG(SPLASH);
|
|
|
|
|
|
|
|
if(splashMode == 1 && loadSplash()) pressed = HID_PAD;
|
|
|
|
|
|
|
|
if((pressed & (BUTTON_START | BUTTON_L1)) == BUTTON_START)
|
|
|
|
{
|
|
|
|
payloadMenu();
|
|
|
|
pressed = HID_PAD;
|
|
|
|
}
|
|
|
|
else if(((pressed & SINGLE_PAYLOAD_BUTTONS) && !(pressed & (BUTTON_L1 | BUTTON_R1 | BUTTON_A))) ||
|
|
|
|
((pressed & L_PAYLOAD_BUTTONS) && (pressed & BUTTON_L1))) loadPayload(pressed, NULL);
|
2016-10-10 01:34:53 +02:00
|
|
|
|
2016-11-15 19:29:48 +01:00
|
|
|
if(splashMode == 2) loadSplash();
|
2016-10-10 01:34:53 +02:00
|
|
|
|
2016-11-15 19:29:48 +01:00
|
|
|
//If booting from CTRNAND, always use SysNAND
|
|
|
|
if(!isSdMode) nandType = FIRMWARE_SYSNAND;
|
2016-10-10 01:34:53 +02:00
|
|
|
|
2016-11-15 19:29:48 +01:00
|
|
|
//If R is pressed, boot the non-updated NAND with the FIRM of the opposite one
|
|
|
|
else if(pressed & BUTTON_R1)
|
|
|
|
{
|
|
|
|
if(CONFIG(USESYSFIRM))
|
2016-10-10 01:34:53 +02:00
|
|
|
{
|
2016-11-15 19:29:48 +01:00
|
|
|
nandType = FIRMWARE_EMUNAND;
|
|
|
|
firmSource = FIRMWARE_SYSNAND;
|
2016-10-10 01:34:53 +02:00
|
|
|
}
|
2016-11-15 19:29:48 +01:00
|
|
|
else
|
|
|
|
{
|
|
|
|
nandType = FIRMWARE_SYSNAND;
|
|
|
|
firmSource = FIRMWARE_EMUNAND;
|
|
|
|
}
|
|
|
|
}
|
2016-10-10 01:34:53 +02:00
|
|
|
|
2016-11-15 19:29:48 +01:00
|
|
|
/* Else, boot the NAND the user set to autoboot or the opposite one, depending on L,
|
|
|
|
with their own FIRM */
|
|
|
|
else firmSource = nandType = (CONFIG(AUTOBOOTSYS) == ((pressed & BUTTON_L1) == BUTTON_L1)) ? FIRMWARE_EMUNAND : FIRMWARE_SYSNAND;
|
|
|
|
|
|
|
|
//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 tempNand;
|
|
|
|
switch(pressed & DPAD_BUTTONS)
|
2016-10-10 01:34:53 +02:00
|
|
|
{
|
2016-11-15 19:29:48 +01:00
|
|
|
case BUTTON_UP:
|
|
|
|
tempNand = FIRMWARE_EMUNAND;
|
|
|
|
break;
|
|
|
|
case BUTTON_RIGHT:
|
|
|
|
tempNand = FIRMWARE_EMUNAND2;
|
|
|
|
break;
|
|
|
|
case BUTTON_DOWN:
|
|
|
|
tempNand = FIRMWARE_EMUNAND3;
|
|
|
|
break;
|
|
|
|
case BUTTON_LEFT:
|
|
|
|
tempNand = FIRMWARE_EMUNAND4;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
tempNand = (FirmwareSource)(1 + MULTICONFIG(DEFAULTEMU));
|
|
|
|
break;
|
2016-10-10 01:34:53 +02:00
|
|
|
}
|
2016-11-15 19:29:48 +01:00
|
|
|
|
|
|
|
if(nandType == FIRMWARE_EMUNAND) nandType = tempNand;
|
|
|
|
else firmSource = tempNand;
|
2016-10-10 01:34:53 +02:00
|
|
|
}
|
|
|
|
|
2016-11-15 19:29:48 +01:00
|
|
|
boot:
|
|
|
|
|
2016-10-10 01:34:53 +02:00
|
|
|
//If we need to boot EmuNAND, make sure it exists
|
|
|
|
if(nandType != FIRMWARE_SYSNAND)
|
|
|
|
{
|
|
|
|
locateEmuNand(&emuHeader, &nandType);
|
|
|
|
if(nandType == FIRMWARE_SYSNAND) firmSource = FIRMWARE_SYSNAND;
|
|
|
|
}
|
|
|
|
|
|
|
|
//Same if we're using EmuNAND as the FIRM source
|
|
|
|
else if(firmSource != FIRMWARE_SYSNAND)
|
|
|
|
locateEmuNand(&emuHeader, &firmSource);
|
|
|
|
|
|
|
|
if(!ISFIRMLAUNCH)
|
|
|
|
{
|
2017-04-14 14:54:31 +02:00
|
|
|
configData.config = (configData.config & 0xFFFFFF00) | ((u32)isNoForceFlagSet << 7) | ((u32)ISA9LH << 6) | ((u32)firmSource << 3) | (u32)nandType;
|
2017-04-13 03:38:36 +02:00
|
|
|
writeConfig(false);
|
2016-10-10 01:34:53 +02:00
|
|
|
}
|
|
|
|
|
2016-10-15 00:32:00 +02:00
|
|
|
if(isSdMode && !mountFs(false, false)) error("Failed to mount CTRNAND.");
|
|
|
|
|
2016-10-10 01:34:53 +02:00
|
|
|
bool loadFromStorage = CONFIG(LOADEXTFIRMSANDMODULES);
|
2016-11-03 18:55:40 +01:00
|
|
|
u32 firmVersion = loadFirm(&firmType, firmSource, loadFromStorage, isSafeMode);
|
2016-10-10 01:34:53 +02:00
|
|
|
|
2017-04-26 18:04:02 +02:00
|
|
|
bool doUnitinfoPatch = CONFIG(PATCHUNITINFO),
|
|
|
|
enableExceptionHandlers = CONFIG(ENABLEEXCEPTIONHANDLERS);
|
2016-10-10 01:34:53 +02:00
|
|
|
u32 res;
|
|
|
|
switch(firmType)
|
|
|
|
{
|
|
|
|
case NATIVE_FIRM:
|
2017-03-05 01:56:24 +01:00
|
|
|
res = patchNativeFirm(firmVersion, nandType, emuHeader, isA9lhInstalled, isSafeMode, doUnitinfoPatch, enableExceptionHandlers);
|
2016-10-10 01:34:53 +02:00
|
|
|
break;
|
|
|
|
case TWL_FIRM:
|
2017-03-05 01:56:24 +01:00
|
|
|
res = patchTwlFirm(firmVersion, doUnitinfoPatch);
|
2016-10-10 01:34:53 +02:00
|
|
|
break;
|
|
|
|
case AGB_FIRM:
|
2017-03-05 01:56:24 +01:00
|
|
|
res = patchAgbFirm(doUnitinfoPatch);
|
2016-10-10 01:34:53 +02:00
|
|
|
break;
|
2016-11-12 01:49:52 +01:00
|
|
|
case SAFE_FIRM:
|
|
|
|
case SYSUPDATER_FIRM:
|
|
|
|
case NATIVE_FIRM1X2X:
|
2017-03-05 01:56:24 +01:00
|
|
|
res = isA9lhInstalled ? patch1x2xNativeAndSafeFirm(enableExceptionHandlers) : 0;
|
2016-11-12 01:49:52 +01:00
|
|
|
break;
|
2016-10-10 01:34:53 +02:00
|
|
|
}
|
|
|
|
|
2016-10-10 15:53:56 +02:00
|
|
|
if(res != 0)
|
|
|
|
{
|
2017-05-08 01:11:24 +02:00
|
|
|
char patchesError[43];
|
2017-05-07 17:58:44 +02:00
|
|
|
sprintf(patchesError, "Failed to apply %u FIRM patch(es).", res);
|
2016-10-10 15:53:56 +02:00
|
|
|
error(patchesError);
|
|
|
|
}
|
2016-10-10 01:34:53 +02:00
|
|
|
|
|
|
|
launchFirm(firmType, loadFromStorage);
|
2017-02-08 12:34:07 +01:00
|
|
|
}
|