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

205 lines
6.2 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"
2015-08-05 03:57:37 +02:00
2016-02-08 03:37:03 +01:00
firmHeader *firmLocation = (firmHeader *)0x24000000;
2015-08-05 03:57:37 +02:00
firmSectionHeader *section;
vu32 *arm11Entry = (vu32*)0x1FFFFFF8;
2016-02-08 03:37:03 +01:00
u32 firmSize = 0;
u8 mode = 1,
console = 1,
a9lhSetup = 0,
updatedSys = 0;
2016-02-08 03:37:03 +01:00
u16 pressed;
2015-08-05 03:57:37 +02:00
void setupCFW(void){
2016-02-08 03:37:03 +01:00
//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;
2016-02-24 20:35:15 +01:00
//Determine if A9LH is installed via PDN_SPI_CNT and an user flag
if((*((u8*)0x101401C0) == 0x0) || fileExists("/rei/installeda9lh")){
a9lhSetup = 1;
//Check flag for > 9.2 SysNAND
if(fileExists("/rei/updatedsysnand")) updatedSys = 1;
}
/* 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;
}
//Load firm into FCRAM
u8 loadFirm(void){
//If not using an A9LH setup, load 9.0 FIRM from NAND
if(!a9lhSetup && !mode){
2016-02-08 03:37:03 +01:00
//Read FIRM from NAND and write to FCRAM
firmSize = console ? 0xF2000 : 0xE9000;
2016-02-08 03:37:03 +01:00
nandFirm0((u8*)firmLocation, firmSize, console);
//Check for correct decryption
2016-02-08 03:37:03 +01:00
if(memcmp((u8*)firmLocation, "FIRM", 4) != 0) return 1;
}
2016-02-19 04:53:19 +01:00
//Load FIRM from SD
2016-02-08 03:37:03 +01:00
else{
char firmPath[] = "/rei/firmware.bin";
char firmPath2[] = "/rei/firmware90.bin";
char *pathPtr = mode ? firmPath : firmPath2;
firmSize = fileSize(pathPtr);
if (!firmSize) return 1;
fileRead((u8*)firmLocation, pathPtr, firmSize);
2016-02-08 03:37:03 +01:00
}
section = firmLocation->section;
//Check that the loaded FIRM matches the console
if((((u32)section[2].address >> 8) & 0xFF) != (console ? 0x60 : 0x68)) return 1;
2016-02-08 03:37:03 +01:00
if(console) arm9loader((u8*)firmLocation + section[2].offset, mode);
return 0;
2015-08-05 11:54:00 +02:00
}
//Nand redirection
2016-02-08 03:37:03 +01:00
u8 loadEmu(void){
u32 emuOffset = 0,
emuHeader = 0,
emuRead = 0,
emuWrite = 0,
sdmmcOffset = 0,
mpuOffset = 0,
emuCodeOffset = 0;
2016-02-08 03:37:03 +01:00
//Read emunand code from SD
char path[] = "/rei/emunand/emunand.bin";
u32 size = fileSize(path);
2016-02-08 03:37:03 +01:00
if (!size) return 1;
if(!console || !mode) nandRedir[5] = 0xA4;
//Find offset for emuNAND code from the offset in nandRedir
u8 *emuCodeTmp = &nandRedir[4];
emuCodeOffset = *(u32*)emuCodeTmp - (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
2016-02-08 03:37:03 +01:00
u32 *pos_sdmmc = memsearch((u32*)emuCodeOffset, "SDMC", size, 4);
u32 *pos_offset = memsearch((u32*)emuCodeOffset, "NAND", size, 4);
u32 *pos_header = memsearch((u32*)emuCodeOffset, "NCSD", size, 4);
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){
u32 *pos_instr = memsearch((u32*)emuCodeOffset, "\xA6\x01\x08\x30", size, 4);
memcpy((u8*)pos_instr, emuInstr, sizeof(emuInstr));
}
2016-02-08 03:37:03 +01:00
//Add emunand hooks
memcpy((u8*)emuRead, nandRedir, sizeof(nandRedir));
memcpy((u8*)emuWrite, nandRedir, sizeof(nandRedir));
2016-02-08 03:37:03 +01:00
//Set MPU for emu code region
memcpy((u8*)mpuOffset, mpu, sizeof(mpu));
2016-02-08 03:37:03 +01:00
return 0;
}
//Patches
2016-02-08 03:37:03 +01:00
u8 patchFirm(void){
/* 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_L1 | BUTTON_R1)))){
if (loadEmu()) return 1;
}
else if (a9lhSetup){
//Patch FIRM partitions writes on SysNAND to protect A9LH
u32 writeOffset = 0;
getFIRMWrite(firmLocation, firmSize, &writeOffset);
memcpy((u8*)writeOffset, FIRMblock, sizeof(FIRMblock));
}
2016-02-08 03:37:03 +01:00
//Disable signature checks
2016-02-08 03:37:03 +01:00
u32 sigOffset = 0,
sigOffset2 = 0;
2016-02-08 03:37:03 +01:00
getSignatures(firmLocation, firmSize, &sigOffset, &sigOffset2);
memcpy((u8*)sigOffset, sigPat1, sizeof(sigPat1));
memcpy((u8*)sigOffset2, sigPat2, sizeof(sigPat2));
//Patch ARM9 entrypoint on N3DS to skip arm9loader
if(console){
u32 *arm9Entry = (u32*)&firmLocation->arm9Entry;
*arm9Entry = 0x801B01C;
}
//Patch FIRM reboots, not on 9.0 FIRM as it breaks firmlaunchhax
if(mode){
2016-02-08 03:37:03 +01:00
u32 rebootOffset = 0,
fOpenOffset = 0;
//Read reboot code from SD
char path[] = "/rei/reboot/reboot.bin";
2016-02-20 15:32:52 +01:00
u32 size = fileSize(path);
2016-02-08 03:37:03 +01:00
if (!size) return 1;
getReboot(firmLocation, firmSize, &rebootOffset);
2016-02-20 15:32:52 +01:00
fileRead((u8*)rebootOffset, path, size);
//Calculate the fOpen offset and put it in the right location
u32 *pos_fopen = memsearch((u32*)rebootOffset, "OPEN", size, 4);
getfOpen(firmLocation, firmSize, &fOpenOffset);
*pos_fopen = fOpenOffset;
2016-02-19 04:53:19 +01:00
//Write patched FIRM to SD
if(fileWrite((u8*)firmLocation, "/rei/patched_firmware.bin", firmSize) != 0) return 1;
2016-02-08 03:37:03 +01:00
}
return 0;
2015-08-05 03:57:37 +02:00
}
//De-initialize the screens, fixes N3DS 3D
void __attribute__((naked)) deinitScreen(void)
{
*arm11Entry = 0;
*(vu32*)0x10202A44 = 0;
*(vu32*)0x10202244 = 0;
*(vu32*)0x10202014 = 0;
while (!*arm11Entry);
((void (*)())*arm11Entry)();
}
//Firmlaunchhax
2015-08-05 03:57:37 +02:00
void launchFirm(void){
2016-02-08 03:37:03 +01:00
2015-08-05 03:57:37 +02:00
//Copy firm partitions to respective memory locations
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);
*arm11Entry = (u32)deinitScreen;
while (*arm11Entry);
*arm11Entry = (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
}