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"
|
2015-08-21 20:11:23 +02:00
|
|
|
#include "crypto.h"
|
2015-08-05 03:57:37 +02:00
|
|
|
|
2016-01-16 13:57:56 +01:00
|
|
|
const firmHeader *firmLocation = (firmHeader *)0x24000000;
|
2015-08-05 03:57:37 +02:00
|
|
|
firmSectionHeader *section;
|
2015-08-07 09:01:42 +02:00
|
|
|
u32 emuOffset = 0;
|
|
|
|
u32 emuHeader = 0;
|
2015-08-05 03:57:37 +02:00
|
|
|
|
2015-08-21 20:11:23 +02:00
|
|
|
//Load firm into FCRAM
|
2016-01-16 13:57:56 +01:00
|
|
|
void loadFirm(void){
|
|
|
|
//Read FIRM from SD card and write to FCRAM
|
2016-01-18 21:24:12 +01:00
|
|
|
fileRead((u8*)firmLocation, "/rei/firmware.bin", 0);
|
2016-01-16 13:57:56 +01:00
|
|
|
section = firmLocation->section;
|
|
|
|
arm9loader((u8*)firmLocation + section[2].offset);
|
2015-08-05 11:54:00 +02:00
|
|
|
}
|
|
|
|
|
2015-08-21 20:11:23 +02:00
|
|
|
//Nand redirection
|
2015-08-07 09:01:42 +02:00
|
|
|
void loadEmu(void){
|
2015-08-21 20:11:23 +02:00
|
|
|
|
|
|
|
//Read emunand code from SD
|
2016-01-16 13:57:56 +01:00
|
|
|
u32 code = emuCode();
|
|
|
|
fileRead(code, "/rei/emunand/emunand.bin", 0);
|
|
|
|
u32 *pos_offset = memsearch(code, "NAND", 0x218, 4);
|
|
|
|
u32 *pos_header = memsearch(code, "NCSD", 0x218, 4);
|
2016-01-18 21:24:12 +01:00
|
|
|
getEmunand(&emuOffset, &emuHeader);
|
2016-01-16 13:57:56 +01:00
|
|
|
if (pos_offset && pos_header) {
|
|
|
|
*pos_offset = emuOffset;
|
|
|
|
*pos_header = emuHeader;
|
|
|
|
}
|
2016-01-18 21:24:12 +01:00
|
|
|
|
2015-08-07 09:01:42 +02:00
|
|
|
//Add emunand hooks
|
2016-01-16 13:57:56 +01:00
|
|
|
memcpy((u8*)emuHook(1), nandRedir, sizeof(nandRedir));
|
|
|
|
memcpy((u8*)emuHook(2), nandRedir, sizeof(nandRedir));
|
2015-08-07 09:01:42 +02:00
|
|
|
}
|
|
|
|
|
2015-08-21 20:11:23 +02:00
|
|
|
//Patches
|
|
|
|
void patchFirm(){
|
2015-08-07 09:01:42 +02:00
|
|
|
|
2015-08-21 20:11:23 +02:00
|
|
|
//Part1: Set MPU for payload area
|
2016-01-16 13:57:56 +01:00
|
|
|
memcpy((u8*)mpuCode(), mpu, sizeof(mpu));
|
2016-01-18 21:24:12 +01:00
|
|
|
|
2015-08-07 09:01:42 +02:00
|
|
|
//Part2: Disable signature checks
|
2016-01-16 13:57:56 +01:00
|
|
|
memcpy((u8*)sigPatch(1), sigPat1, sizeof(sigPat1));
|
|
|
|
memcpy((u8*)sigPatch(2), sigPat2, sizeof(sigPat2));
|
2015-08-05 03:57:37 +02:00
|
|
|
|
2015-08-07 09:01:42 +02:00
|
|
|
//Part3: Create arm9 thread
|
2016-01-16 13:57:56 +01:00
|
|
|
fileRead((u8*)threadCode(), "/rei/thread/arm9.bin", 0);
|
|
|
|
memcpy((u8*)threadHook(1), th1, sizeof(th1));
|
|
|
|
memcpy((u8*)threadHook(2), th2, sizeof(th2));
|
2015-08-05 03:57:37 +02:00
|
|
|
}
|
|
|
|
|
2015-08-21 20:11:23 +02:00
|
|
|
//Firmlaunchhax
|
2015-08-05 03:57:37 +02:00
|
|
|
void launchFirm(void){
|
2015-08-21 20:11:23 +02:00
|
|
|
|
2015-08-05 03:57:37 +02:00
|
|
|
//Set MPU
|
2015-08-06 07:17:10 +02:00
|
|
|
__asm__ (
|
|
|
|
"msr cpsr_c, #0xDF\n\t" //Set system mode, disable interrupts
|
|
|
|
"ldr r0, =0x10000035\n\t" //Memory area 0x10000000-0x18000000, enabled, 128MB
|
|
|
|
"ldr r4, =0x18000035\n\t" //Memory area 0x18000000-0x20000000, enabled, 128MB
|
|
|
|
"mcr p15, 0, r0, c6, c3, 0\n\t" //Set memory area 3 (0x10000000-0x18000000)
|
|
|
|
"mcr p15, 0, r4, c6, c4, 0\n\t" //Set memory area 4 (0x18000000-0x20000000)
|
|
|
|
"mrc p15, 0, r0, c2, c0, 0\n\t" //read data cacheable bit
|
|
|
|
"mrc p15, 0, r4, c2, c0, 1\n\t" //read inst cacheable bit
|
|
|
|
"mrc p15, 0, r1, c3, c0, 0\n\t" //read data writeable
|
|
|
|
"mrc p15, 0, r2, c5, c0, 2\n\t" //read data access permission
|
|
|
|
"mrc p15, 0, r3, c5, c0, 3\n\t" //read inst access permission
|
2015-08-05 03:57:37 +02:00
|
|
|
"orr r0, r0, #0x30\n\t"
|
|
|
|
"orr r4, r4, #0x30\n\t"
|
|
|
|
"orr r1, r1, #0x30\n\t"
|
|
|
|
"bic r2, r2, #0xF0000\n\t"
|
|
|
|
"bic r3, r3, #0xF0000\n\t"
|
|
|
|
"orr r2, r2, #0x30000\n\t"
|
|
|
|
"orr r3, r3, #0x30000\n\t"
|
2015-08-06 07:17:10 +02:00
|
|
|
"mcr p15, 0, r0, c2, c0, 0\n\t" //write data cacheable bit
|
|
|
|
"mcr p15, 0, r4, c2, c0, 1\n\t" //write inst cacheable bit
|
|
|
|
"mcr p15, 0, r1, c3, c0, 0\n\t" //write data writeable
|
|
|
|
"mcr p15, 0, r2, c5, c0, 2\n\t" //write data access permission
|
|
|
|
"mcr p15, 0, r3, c5, c0, 3\n\t" //write inst access permission
|
2015-08-05 03:57:37 +02:00
|
|
|
::: "r0", "r1", "r2", "r3", "r4"
|
|
|
|
);
|
2015-08-06 07:17:10 +02: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);
|
|
|
|
*(u32 *)0x1FFFFFF8 = (u32)firmLocation->arm11Entry;
|
|
|
|
|
|
|
|
//Final jump to arm9 binary
|
|
|
|
((void (*)())0x801B01C)();
|
|
|
|
}
|