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

261 lines
8.3 KiB
C
Raw Normal View History

2015-08-05 03:57:37 +02:00
/*
* firm.c
* by Reisyukaku
2015-08-15 04:28:26 +02:00
* Copyright (c) 2015 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 "loader.h"
2015-08-05 03:57:37 +02:00
2016-03-10 14:58:11 +01:00
static firmHeader *const firmLocation = (firmHeader *)0x24000000;
static const firmSectionHeader *section;
static u32 firmSize = 0,
mode = 1,
console = 1,
emuNAND = 0,
a9lhSetup = 0,
updatedSys = 0,
usePatchedFirm = 0;
static u16 pressed;
static const char *firmPathPatched = NULL;
2015-08-05 03:57:37 +02:00
void setupCFW(void){
2016-02-08 03:37:03 +01:00
//Determine if booting with A9LH via PDN_SPI_CNT
u32 a9lhBoot = (*(u8 *)0x101401C0 == 0x0) ? 1 : 0;
//Retrieve the last booted FIRM via CFG_BOOTENV
u8 previousFirm = *(u8 *)0x10010000;
u32 overrideConfig = 0;
2016-03-06 16:24:42 +01:00
const char lastConfigPath[] = "rei/lastbootcfg";
//Detect the console being used
2016-02-08 03:37:03 +01:00
if(PDN_MPCORE_CFG == 1) console = 0;
//Get pressed buttons
2016-02-08 03:37:03 +01:00
pressed = HID_PAD;
//Determine if A9LH is installed
if(a9lhBoot || fileExists("/rei/installeda9lh")){
a9lhSetup = 1;
//Check flag for > 9.2 SysNAND
if(fileExists("/rei/updatedsysnand")) updatedSys = 1;
}
//If booting with A9LH and it's a MCU reboot, try to force boot options
if(a9lhBoot && previousFirm && fileExists(lastConfigPath)){
u8 tempConfig;
2016-03-06 16:24:42 +01:00
fileRead(&tempConfig, lastConfigPath, 1);
//Always force a sysNAND boot when quitting AGB_FIRM
if(previousFirm == 0x7) {
2016-03-05 18:47:47 +01:00
if(!updatedSys) mode = tempConfig & 0x1;
overrideConfig = 1;
//Else, force the last used boot options unless A is pressed
} else if(!(pressed & BUTTON_A)) {
mode = tempConfig & 0x1;
emuNAND = (tempConfig >> 1) & 0x1;
overrideConfig = 1;
}
}
if(!overrideConfig){
//If L and R are pressed, chainload an external payload
if(a9lhBoot && (pressed & BUTTON_L1R1) == BUTTON_L1R1) loadPayload();
//Check if it's a no-screen-init A9LH boot via PDN_GPU_CNT
if(*(u8 *)0x10141200 != 0x1) loadSplash();
/* If L is pressed, and on an updated SysNAND setup the SAFE MODE combo
is not pressed, boot 9.0 FIRM */
if((pressed & BUTTON_L1) && !(updatedSys && pressed == SAFEMODE)) mode = 0;
/* 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) && pressed != SAFEMODE))) ||
(!updatedSys && mode && !(pressed & BUTTON_R1))) emuNAND = 1;
//Write the current boot options on A9LH
if(a9lhBoot){
u8 tempConfig = (mode | (emuNAND << 1)) & 0x3;
2016-03-06 16:24:42 +01:00
fileWrite(&tempConfig, lastConfigPath, 1);
}
}
if(mode) firmPathPatched = emuNAND ? "/rei/patched_firmware_emu.bin" :
"/rei/patched_firmware_sys.bin";
//Skip decrypting and patching FIRM
if(fileExists("/rei/usepatchedfw")){
//Only needed with this flag
if(!mode) firmPathPatched = "/rei/patched_firmware90.bin";
2016-03-05 18:47:47 +01:00
if(fileExists(firmPathPatched)) usePatchedFirm = 1;
}
}
//Load firm into FCRAM
u32 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 *)firmLocation, firmSize, console);
//Check for correct decryption
if(memcmp(firmLocation, "FIRM", 4) != 0) return 0;
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{
2016-03-06 16:24:42 +01:00
const char *path = usePatchedFirm ? firmPathPatched :
(mode ? "/rei/firmware.bin" : "/rei/firmware90.bin");
firmSize = fileSize(path);
2016-03-05 23:27:02 +01:00
if(!firmSize) return 0;
fileRead((u8 *)firmLocation, path, firmSize);
2016-02-08 03:37:03 +01:00
}
section = firmLocation->section;
//Check that the loaded FIRM matches the console
2016-03-05 23:27:02 +01:00
if((((u32)section[2].address >> 8) & 0xFF) != (console ? 0x60 : 0x68)) return 0;
2016-02-08 03:37:03 +01:00
if(console && !usePatchedFirm)
2016-03-06 19:05:41 +01:00
decArm9Bin((u8 *)firmLocation + section[2].offset, mode);
2016-02-08 03:37:03 +01:00
2016-03-05 23:27:02 +01:00
return 1;
2015-08-05 11:54:00 +02:00
}
//Nand redirection
2016-03-10 14:58:11 +01:00
static u32 loadEmu(void){
2016-02-08 03:37:03 +01:00
u32 emuOffset = 0,
emuHeader = 0,
emuRead,
emuWrite,
sdmmcOffset,
mpuOffset,
emuCodeOffset;
2016-02-08 03:37:03 +01:00
//Read emunand code from SD
2016-03-06 16:24:42 +01:00
const char path[] = "/rei/emunand/emunand.bin";
u32 size = fileSize(path);
2016-03-05 23:27:02 +01:00
if(!size) return 0;
if(!console || !mode) nandRedir[5] = 0xA4;
//Find offset for emuNAND code from the offset in nandRedir
2016-03-06 16:24:42 +01:00
emuCodeOffset = *(u32 *)(nandRedir + 4) - (u32)section[2].address +
section[2].offset + (u32)firmLocation;
fileRead((u8 *)emuCodeOffset, path, size);
2016-02-08 03:37:03 +01:00
//Find and patch emunand related offsets
u32 *pos_sdmmc = (u32 *)memsearch((void *)emuCodeOffset, "SDMC", size, 4);
u32 *pos_offset = (u32 *)memsearch((void *)emuCodeOffset, "NAND", size, 4);
u32 *pos_header = (u32 *)memsearch((void *)emuCodeOffset, "NCSD", size, 4);
2016-02-08 03:37:03 +01:00
getSDMMC(firmLocation, &sdmmcOffset, firmSize);
getEmunandSect(&emuOffset, &emuHeader);
getEmuRW(firmLocation, firmSize, &emuRead, &emuWrite);
2016-02-08 03:37:03 +01:00
getMPU(firmLocation, &mpuOffset, firmSize);
*pos_sdmmc = sdmmcOffset;
*pos_offset = emuOffset;
*pos_header = emuHeader;
//Patch emuNAND code in memory for O3DS and 9.0 N3DS
if(!console || !mode){
void *pos_instr = memsearch((void *)emuCodeOffset, "\xA6\x01\x08\x30", size, 4);
2016-03-06 16:24:42 +01:00
memcpy(pos_instr, emuInstr, sizeof(emuInstr));
}
2016-02-08 03:37:03 +01:00
//Add emunand hooks
2016-03-06 16:24:42 +01:00
memcpy((void *)emuRead, nandRedir, sizeof(nandRedir));
memcpy((void *)emuWrite, nandRedir, sizeof(nandRedir));
2016-02-08 03:37:03 +01:00
//Set MPU for emu code region
2016-03-06 16:24:42 +01:00
memcpy((void *)mpuOffset, mpu, sizeof(mpu));
2016-02-08 03:37:03 +01:00
2016-03-05 23:27:02 +01:00
return 1;
}
//Patches
u32 patchFirm(void){
//Skip patching
2016-03-05 23:27:02 +01:00
if(usePatchedFirm) return 1;
2016-02-08 03:37:03 +01:00
//Apply emuNAND patches
if(emuNAND){
2016-03-06 03:41:07 +01:00
if(!loadEmu()) return 0;
}
2016-03-05 23:27:02 +01:00
else if(a9lhSetup){
//Patch FIRM partitions writes on SysNAND to protect A9LH
u32 writeOffset = 0;
getFIRMWrite(firmLocation, firmSize, &writeOffset);
2016-03-06 16:24:42 +01:00
memcpy((void *)writeOffset, FIRMblock, sizeof(FIRMblock));
}
2016-02-08 03:37:03 +01:00
//Disable signature checks
u32 sigOffset,
sigOffset2;
2016-02-08 03:37:03 +01:00
getSignatures(firmLocation, firmSize, &sigOffset, &sigOffset2);
2016-03-06 16:24:42 +01:00
memcpy((void *)sigOffset, sigPat1, sizeof(sigPat1));
memcpy((void *)sigOffset2, sigPat2, sizeof(sigPat2));
2016-02-08 03:37:03 +01:00
//Patch ARM9 entrypoint on N3DS to skip arm9loader
if(console)
firmLocation->arm9Entry = (u8 *)0x801B01C;
//Patch FIRM reboots, not on 9.0 FIRM as it breaks firmlaunchhax
if(mode){
u32 rebootOffset,
fOpenOffset;
//Read reboot code from SD
2016-03-06 16:24:42 +01:00
const char path[] = "/rei/reboot/reboot.bin";
2016-02-20 15:32:52 +01:00
u32 size = fileSize(path);
2016-03-05 23:27:02 +01:00
if(!size) return 0;
getReboot(firmLocation, firmSize, &rebootOffset);
fileRead((u8 *)rebootOffset, path, size);
//Calculate the fOpen offset and put it in the right location
u32 *pos_fopen = (u32 *)memsearch((void *)rebootOffset, "OPEN", size, 4);
getfOpen(firmLocation, firmSize, &fOpenOffset);
*pos_fopen = fOpenOffset;
//Patch path for emuNAND-patched FIRM
if(emuNAND){
void *pos_path = memsearch((void *)rebootOffset, L"sy", size, 4);
2016-03-06 16:24:42 +01:00
memcpy(pos_path, L"emu", 5);
}
2016-02-08 03:37:03 +01:00
}
//Write patched FIRM to SD if needed
if(firmPathPatched)
if(!fileWrite((u8 *)firmLocation, firmPathPatched, firmSize)) return 0;
2016-03-05 23:27:02 +01:00
return 1;
2015-08-05 03:57:37 +02:00
}
void launchFirm(void){
2016-02-08 03:37:03 +01:00
2016-03-06 19:05:41 +01:00
if(console && mode) setKeyXs((u8 *)firmLocation + section[2].offset);
2015-08-05 03:57:37 +02:00
//Copy firm partitions to respective memory locations
2016-03-06 19:05:41 +01:00
memcpy(section[0].address, (u8 *)firmLocation + section[0].offset, section[0].size);
memcpy(section[1].address, (u8 *)firmLocation + section[1].offset, section[1].size);
memcpy(section[2].address, (u8 *)firmLocation + section[2].offset, section[2].size);
2016-02-29 16:28:43 +01:00
//Run ARM11 screen stuff
2016-03-10 14:58:11 +01:00
vu32 *const arm11 = (u32 *)0x1FFFFFF8;
2016-02-29 16:28:43 +01:00
*arm11 = (u32)shutdownLCD;
while(*arm11);
2016-02-29 16:28:43 +01:00
//Set ARM11 kernel
*arm11 = (u32)firmLocation->arm11Entry;
2015-08-05 03:57:37 +02:00
//Final jump to arm9 binary
((void (*)())firmLocation->arm9Entry)();
2015-08-05 03:57:37 +02:00
}