This repository has been archived on 2022-05-31. You can view files and clone it, but cannot push or open issues or pull requests.
Luma3DS-3GX/source/firm.c

305 lines
10 KiB
C
Raw Normal View History

2015-08-05 03:57:37 +02:00
/*
* firm.c
* by Reisyukaku / Aurora Wright
* Copyright (c) 2016 All Rights Reserved
2015-08-05 03:57:37 +02:00
*/
#include "firm.h"
#include "patches.h"
#include "memory.h"
#include "fs.h"
#include "emunand.h"
#include "crypto.h"
2016-02-29 16:28:43 +01:00
#include "draw.h"
#include "screeninit.h"
#include "loader.h"
#include "utils.h"
#include "buttons.h"
#include "../build/patches.h"
//FIRM patches version
#define PATCH_VER 2
2015-08-05 03:57:37 +02:00
static firmHeader *const firm = (firmHeader *)0x24000000;
2016-03-10 14:58:11 +01:00
static const firmSectionHeader *section;
static u8 *arm9Section;
static const char *patchedFirms[] = { "/aurei/patched_firmware_sys.bin",
"/aurei/patched_firmware_emu.bin",
"/aurei/patched_firmware_em2.bin",
"/aurei/patched_firmware90.bin" };
static u32 firmSize,
console,
mode,
emuNAND,
a9lhSetup,
selectedFirm,
usePatchedFirm;
2015-08-05 03:57:37 +02:00
void setupCFW(void){
2016-02-08 03:37:03 +01:00
2016-03-13 16:48:57 +01:00
//Determine if booting with A9LH
u32 a9lhBoot = (PDN_SPI_CNT == 0x0) ? 1 : 0;
//Retrieve the last booted FIRM
u8 previousFirm = CFG_BOOTENV;
//Detect the console being used
console = (PDN_MPCORE_CFG == 1) ? 0 : 1;
//Get pressed buttons
u16 pressed = HID_PAD;
//Attempt to read the configuration file
const char configPath[] = "aurei/config.bin";
u32 config = 0;
u32 needConfig = fileRead(&config, configPath, 3) ? 1 : 2;
//Determine if A9LH is installed and the user has an updated sysNAND
u32 updatedSys;
if(a9lhBoot || (config >> 2) & 0x1){
2016-03-24 17:27:02 +01:00
if(pressed == SAFE_MODE)
error("Using Safe Mode would brick you, or remove A9LH!");
a9lhSetup = 1;
2016-03-23 15:48:59 +01:00
//Check setting for > 9.2 sysNAND
updatedSys = config & 0x1;
} else{
a9lhSetup = 0;
updatedSys = 0;
}
/* If booting with A9LH, it's a MCU reboot and a previous configuration exists,
try to force boot options */
if(a9lhBoot && previousFirm && needConfig == 1){
//Always force a sysNAND boot when quitting AGB_FIRM
if(previousFirm == 0x7){
if(!updatedSys) mode = (config >> 12) & 0x1;
emuNAND = 0;
needConfig = 0;
//Else, force the last used boot options unless A, L or R are pressed
} else if(!(pressed & OPTION_BUTTONS)){
mode = (config >> 12) & 0x1;
emuNAND = (config >> 13) & 0x3;
needConfig = 0;
}
}
if(needConfig){
2016-03-24 02:40:43 +01:00
/* If L and one of the payload buttons are pressed, and if not using A9LH
the Safe Mode combo is not pressed, chainload an external payload */
if((pressed & BUTTON_L1) && (pressed & PAYLOAD_BUTTONS) &&
pressed != SAFE_MODE) loadPayload();
//If no configuration file exists or SELECT is held, load configuration menu
if(needConfig == 2 || (pressed & BUTTON_SELECT))
configureCFW(configPath, patchedFirms[3]);
//If screens are inited, load splash screen
2016-03-13 16:48:57 +01:00
if(PDN_GPU_CNT != 0x1) loadSplash();
/* If L is pressed, boot 9.0 FIRM */
mode = ((config >> 3) & 0x1) ? ((!(pressed & BUTTON_L1R1)) ? 0 : 1) :
((pressed & BUTTON_L1) ? 0 : 1);
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 */
if((updatedSys && (!mode || (pressed & BUTTON_R1))) ||
(!updatedSys && mode && !(pressed & BUTTON_R1))){
//If not 9.0 FIRM and B is pressed, attempt booting the second emuNAND
emuNAND = (mode && ((!(pressed & BUTTON_B)) == ((config >> 4) & 0x1))) ? 2 : 1;
} else emuNAND = 0;
u32 tempConfig = (PATCH_VER << 17) | (a9lhSetup << 16) | (emuNAND << 13) | (mode << 12);
/* 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 *));
//If the boot configuration is different from previously, overwrite it
if((tempConfig & 0xFFF000) != (config & 0xFFF000)){
//Preserve user settings (first 12 bits)
tempConfig |= config & 0xFFF;
fileWrite(&tempConfig, configPath, 3);
}
}
/* 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) :
(((config >> 1) & 0x1) ? 4 : 0);
//If "Use pre-patched FIRMs" is set and the appropriate FIRM exists, use it
usePatchedFirm = (((config >> 1) & 0x1) && fileExists(patchedFirms[selectedFirm - 1])) ? 1 : 0;
}
//Load FIRM into FCRAM
void loadFirm(void){
//If not using an A9LH setup or the patched FIRM, load 9.0 FIRM from NAND
if(!usePatchedFirm && !a9lhSetup && !mode){
2016-02-08 03:37:03 +01:00
//Read FIRM from NAND and write to FCRAM
firmSize = console ? 0xF2000 : 0xE9000;
nandFirm0((u8 *)firm, firmSize, console);
//Check for correct decryption
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-02-19 04:53:19 +01:00
//Load FIRM from SD
2016-02-08 03:37:03 +01:00
else{
const char *path = usePatchedFirm ? patchedFirms[selectedFirm - 1] :
(mode ? "/aurei/firmware.bin" : "/aurei/firmware90.bin");
firmSize = fileSize(path);
2016-03-23 04:06:21 +01:00
if(!firmSize) error("aurei/firmware(90).bin doesn't exist");
fileRead(firm, path, firmSize);
2016-02-08 03:37:03 +01:00
}
section = firm->section;
//Check that the loaded FIRM matches the console
if((((u32)section[2].address >> 8) & 0xFF) != (console ? 0x60 : 0x68))
2016-03-23 04:06:21 +01:00
error("aurei/firmware(90).bin doesn't match this\nconsole, or it's encrypted");
2016-02-08 03:37:03 +01:00
arm9Section = (u8 *)firm + section[2].offset;
2016-03-21 19:20:26 +01:00
if(console && !usePatchedFirm) decryptArm9Bin(arm9Section, mode);
2015-08-05 11:54:00 +02:00
}
//NAND redirection
static void loadEmu(u8 *proc9Offset){
2016-02-08 03:37:03 +01:00
u32 emuOffset,
emuHeader = 0,
emuRead,
emuWrite;
2016-02-08 03:37:03 +01:00
//Look for emuNAND
getEmunandSect(&emuOffset, &emuHeader, emuNAND);
2016-03-21 04:39:00 +01:00
//No emuNAND detected
if(!emuHeader) error("No emuNAND has been detected");
2016-03-26 17:28:47 +01:00
//Copy emuNAND code
void *emuCodeOffset = getEmuCode(proc9Offset);
2016-03-23 15:48:59 +01:00
memcpy(emuCodeOffset, emunand, emunand_size);
//Add the data of the found emuNAND
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-03-23 15:48:59 +01:00
//Find and add the SDMMC struct
u32 *pos_sdmmc = (u32 *)memsearch(emuCodeOffset, "SDMC", emunand_size, 4);
*pos_sdmmc = getSDMMC(arm9Section, section[2].size);
//Calculate offset for the hooks
u32 branchOffset = (u32)emuCodeOffset - (u32)firm -
section[2].offset + (u32)section[2].address;
2016-02-08 03:37:03 +01:00
//Add emunand hooks
getEmuRW(arm9Section, section[2].size, &emuRead, &emuWrite);
*(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
//Set MPU for emu code region
u32 *mpuOffset = getMPU(arm9Section, section[2].size);
*mpuOffset = mpuPatch[0];
*(mpuOffset + 6) = mpuPatch[1];
*(mpuOffset + 9) = mpuPatch[2];
}
//Patches
void patchFirm(void){
//Skip patching
if(usePatchedFirm) return;
2016-02-08 03:37:03 +01:00
if(mode || emuNAND){
//Find the Process9 NCCH location
u8 *proc9Offset = getProc9(arm9Section, section[2].size);
//Apply emuNAND patches
if(emuNAND) loadEmu(proc9Offset);
//Patch FIRM reboots, not on 9.0 FIRM as it breaks firmlaunchhax
if(mode){
2016-03-26 17:28:47 +01:00
//Calculate offset for the firmlaunch code
2016-03-23 15:16:40 +01:00
void *rebootOffset = getReboot(arm9Section, section[2].size);
2016-03-26 17:28:47 +01:00
//Calculate offset for the fOpen function
u32 fOpenOffset = getfOpen(proc9Offset, rebootOffset);
2016-03-26 17:28:47 +01: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;
//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);
}
}
}
if(a9lhSetup && !emuNAND){
2016-03-23 15:48:59 +01:00
//Patch FIRM partitions writes on sysNAND to protect A9LH
u16 *writeOffset = getFirmWrite(arm9Section, section[2].size);
*writeOffset = writeBlock[0];
*(writeOffset + 1) = writeBlock[1];
}
2016-02-08 03:37:03 +01:00
//Disable signature checks
u32 sigOffset,
sigOffset2;
getSigChecks(arm9Section, section[2].size, &sigOffset, &sigOffset2);
*(u16 *)sigOffset = sigPatch[0];
*(u16 *)sigOffset2 = sigPatch[0];
*((u16 *)sigOffset2 + 1) = sigPatch[1];
2016-02-08 03:37:03 +01:00
//Replace the FIRM loader with the injector
u32 loaderOffset,
loaderSize;
getLoader((u8 *)firm + section[0].offset, section[0].size, &loaderOffset, &loaderSize);
if(injector_size <= (int)loaderSize){
memset((void *)loaderOffset, 0, loaderSize);
memcpy((void *)loaderOffset, injector, injector_size);
*((u32 *)loaderOffset + 0x41) = loaderSize / 0x200;
*((u32 *)loaderOffset + 0x69) = loaderSize / 0x200 - 5;
}
//Patch ARM9 entrypoint on N3DS to skip arm9loader
if(console)
firm->arm9Entry = (u8 *)0x801B01C;
//Write patched FIRM to SD if needed
if(selectedFirm)
if(!fileWrite(firm, patchedFirms[selectedFirm - 1], firmSize))
error("Couldn't write the patched FIRM (no free space?)");
2015-08-05 03:57:37 +02:00
}
void launchFirm(void){
2016-02-08 03:37:03 +01:00
if(console && mode) setKeyXs(arm9Section);
2015-08-05 03:57:37 +02:00
//Copy firm partitions to respective memory locations
memcpy(section[0].address, (u8 *)firm + section[0].offset, section[0].size);
memcpy(section[1].address, (u8 *)firm + section[1].offset, section[1].size);
memcpy(section[2].address, arm9Section, section[2].size);
2016-02-29 16:28:43 +01:00
//Fixes N3DS 3D
deinitScreens();
//Set ARM11 kernel entrypoint
*(vu32 *)0x1FFFFFF8 = (u32)firm->arm11Entry;
//Final jump to arm9 kernel
((void (*)())firm->arm9Entry)();
2015-08-05 03:57:37 +02:00
}