Skip to content

Commit b4d94da

Browse files
committed
Added "updated sysNAND" mode, FIRM writes patch, propered emuNAND code location
Thanks to delebile for the patch!
1 parent 8872a24 commit b4d94da

7 files changed

Lines changed: 70 additions & 47 deletions

File tree

Makefile

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ objects_cfw = $(patsubst $(dir_source)/%.s, $(dir_build)/%.o, \
3333

3434

3535
.PHONY: all
36-
all: launcher a9lh emunand emunando3ds reboot reboot2 rebootntr ninjhax
36+
all: launcher a9lh emunand emunando3ds reboot rebootntr ninjhax
3737

3838
.PHONY: launcher
3939
launcher: $(dir_out)/$(name).dat
@@ -42,16 +42,13 @@ launcher: $(dir_out)/$(name).dat
4242
a9lh: $(dir_out)/arm9loaderhax.bin
4343

4444
.PHONY: emunand
45-
emunand: $(dir_out)/rei-n3ds/emunand/emunand.bin
45+
emunand: $(dir_out)/rei-n3ds/emunand/emunand.bin $(dir_out)/rei-n3ds/emunand/emunand90.bin
4646

4747
.PHONY: emunando3ds
4848
emunando3ds: $(dir_out)/rei-o3ds/emunand/emunand.bin
4949

5050
.PHONY: reboot
51-
reboot: $(dir_out)/rei-o3ds/reboot/reboot1.bin
52-
53-
.PHONY: reboot2
54-
reboot2: $(dir_out)/rei-o3ds/reboot/reboot2.bin
51+
reboot: $(dir_out)/rei-o3ds/reboot/reboot1.bin $(dir_out)/rei-o3ds/reboot/reboot2.bin
5552

5653
.PHONY: rebootntr
5754
rebootntr: $(dir_out)/ntr-o3ds/reboot/reboot1.bin $(dir_out)/ntr-o3ds/reboot/reboot2.bin
@@ -70,7 +67,7 @@ $(dir_out)/$(name).dat: $(dir_build)/main.bin $(dir_out)/rei-n3ds/ $(dir_out)/re
7067
dd if=$(dir_build)/main.bin of=$@ bs=512 seek=144
7168

7269
$(dir_out)/arm9loaderhax.bin: $(dir_build)/main.bin $(dir_out)/rei-n3ds/ $(dir_out)/rei-o3ds/
73-
@cp $(dir_build)/main.bin $(dir_out)/arm9loaderhax.bin
70+
@cp -av $(dir_build)/main.bin $(dir_out)/arm9loaderhax.bin
7471

7572
$(dir_out)/3ds/$(name):
7673
@mkdir -p "$(dir_out)/3ds/$(name)"
@@ -94,7 +91,10 @@ $(dir_out)/rei-n3ds/emunand/emunand.bin: $(dir_emu)/emuCode.s
9491
$(dir_out)/rei-o3ds/emunand/emunand.bin: $(dir_emu)/emuCodeo3ds.s
9592
@armips $<
9693
@mkdir -p "$(dir_out)/rei-o3ds/emunand"
97-
@mv emunand.bin $(dir_out)/rei-o3ds/emunand
94+
@cp -av emunand.bin $(dir_out)/rei-o3ds/emunand
95+
96+
$(dir_out)/rei-n3ds/emunand/emunand90.bin: $(dir_out)/rei-o3ds/emunand/emunand.bin
97+
@mv emunand.bin $(dir_out)/rei-n3ds/emunand/emunand90.bin
9898

9999
$(dir_out)/rei-o3ds/reboot/reboot1.bin: $(dir_reboot)/rebootCode.s
100100
@armips $<

source/emunand.c

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -53,12 +53,4 @@ void getMPU(void *pos, u32 *off, u32 size){
5353
unsigned char pattern[] = {0x03, 0x00, 0x24, 0x00, 0x00};
5454

5555
*off = (u32)memsearch(pos, pattern, size, 5);
56-
}
57-
58-
void getEmuCode(void *pos, u32 *off, u32 size){
59-
void *proc9 = memsearch(pos, "Process9", size, 8);
60-
unsigned char pattern[] = {0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF};
61-
62-
//Looking for the last spot before Process9
63-
*off = (u32)memsearch(pos, pattern, size - (size - (u32)(proc9 - pos)), 6) + 0xF;
6456
}

source/emunand.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,5 @@ void getEmunandSect(u32 *off, u32 *head);
1515
void getSDMMC(void *pos, u32 *off, u32 size);
1616
void getEmuRW(void *pos, u32 size, u32 *readOff, u32 *writeOff);
1717
void getMPU(void *pos, u32 *off, u32 size);
18-
void getEmuCode(void *pos, u32 *off, u32 size);
1918

2019
#endif

source/firm.c

Lines changed: 47 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -15,40 +15,47 @@ firmHeader *firmLocation = (firmHeader *)0x24000000;
1515
firmSectionHeader *section;
1616
u32 firmSize = 0;
1717
u8 mode = 1,
18-
console = 1;
18+
console = 1,
19+
a9lhSetup = 0,
20+
updatedSys = 0;
1921
u16 pressed;
2022

2123
//Load firm into FCRAM
22-
u8 loadFirm(u8 a9lh){
24+
u8 loadFirm(u8 a9lhBoot){
2325

26+
//Detect the console being used
2427
if(PDN_MPCORE_CFG == 1) console = 0;
28+
//Get pressed buttons
2529
pressed = HID_PAD;
30+
//Determine if A9LH is installed
31+
if(a9lhBoot || fileSize("/rei/installeda9lh")){
32+
a9lhSetup = 1;
33+
//Check flag for > 9.2 SysNAND
34+
if(fileSize("/rei/updatedsysnand")) updatedSys = 1;
35+
}
36+
2637
section = firmLocation->section;
2738

28-
//If L and R are pressed, boot SysNAND with the 9.0 FIRM
29-
if((pressed & BUTTON_L1R1) == BUTTON_L1R1) mode = 0;
39+
/* If L and R are pressed on a 9.0/2 SysNAND, or L on an updated
40+
SysNAND, boot 9.0 FIRM */
41+
if((!updatedSys & ((pressed & BUTTON_L1R1) == BUTTON_L1R1)) |
42+
(updatedSys & (pressed == BUTTON_L1))) mode = 0;
3043

3144
//If not using an A9LH setup, do so by decrypting FIRM0
32-
if(!a9lh && !fileSize("/rei/installeda9lh") && !mode){
45+
if(!a9lhSetup && !mode){
3346
//Read FIRM from NAND and write to FCRAM
3447
firmSize = console ? 0xF2000 : 0xE9000;
3548
nandFirm0((u8*)firmLocation, firmSize, console);
3649
if(memcmp((u8*)firmLocation, "FIRM", 4) != 0) return 1;
3750
}
3851
//Load FIRM from SD
3952
else{
40-
if (!mode){
41-
char firmPath[] = "/rei/firmware90.bin";
42-
firmSize = fileSize(firmPath);
43-
if (!firmSize) return 1;
44-
fileRead((u8*)firmLocation, firmPath, firmSize);
45-
}
46-
else {
47-
char firmPath[] = "/rei/firmware.bin";
48-
firmSize = fileSize(firmPath);
49-
if (!firmSize) return 1;
50-
fileRead((u8*)firmLocation, firmPath, firmSize);
51-
}
53+
char firmPath[] = "/rei/firmware.bin";
54+
char firmPath2[] = "/rei/firmware90.bin";
55+
char *pathPtr = mode ? firmPath : firmPath2;
56+
firmSize = fileSize(pathPtr);
57+
if (!firmSize) return 1;
58+
fileRead((u8*)firmLocation, pathPtr, firmSize);
5259
}
5360
if((((u32)section[2].address >> 8) & 0xFF) != (console ? 0x60 : 0x68)) return 1;
5461

@@ -69,11 +76,16 @@ u8 loadEmu(void){
6976
emuCodeOffset = 0;
7077

7178
//Read emunand code from SD
72-
const char path[] = "/rei/emunand/emunand.bin";
73-
u32 size = fileSize(path);
79+
char path[] = "/rei/emunand/emunand.bin";
80+
char path2[] = "/rei/emunand/emunand90.bin";
81+
char *pathPtr = ((!mode) & console) ? path2 : path;
82+
u32 size = fileSize(pathPtr);
7483
if (!size) return 1;
75-
getEmuCode(firmLocation, &emuCodeOffset, firmSize);
76-
fileRead((u8*)emuCodeOffset, path, size);
84+
if(!console | !mode) nandRedir[5] = 0xA4;
85+
u8 *emuCodeTmp = &nandRedir[4];
86+
emuCodeOffset = *(u32*)emuCodeTmp - (u32)section[2].address +
87+
section[2].offset + (u32)firmLocation;
88+
fileRead((u8*)emuCodeOffset, pathPtr, size);
7789

7890
//Find and patch emunand related offsets
7991
u32 *pos_sdmmc = memsearch((u32*)emuCodeOffset, "SDMC", size, 4);
@@ -88,7 +100,6 @@ u8 loadEmu(void){
88100
*pos_header = emuHeader;
89101

90102
//Add emunand hooks
91-
if(!console) nandRedir[5] = 0xA4;
92103
memcpy((u8*)emuRead, nandRedir, sizeof(nandRedir));
93104
memcpy((u8*)emuWrite, nandRedir, sizeof(nandRedir));
94105

@@ -101,8 +112,18 @@ u8 loadEmu(void){
101112
//Patches
102113
u8 patchFirm(void){
103114

104-
//If L is pressed, boot SysNAND with the SD FIRM
105-
if(mode && !(pressed & BUTTON_L1)) if (loadEmu()) return 1;
115+
/* If L is pressed on a 9.0/9.2 SysNAND, or L+R on a > 9.2 SysNAND,
116+
or the 9.0 FIRM is loaded on a > 9.2 SysNAND, boot emuNAND */
117+
if((updatedSys & (!mode | (pressed == BUTTON_L1R1))) |
118+
((!updatedSys) & mode & !(pressed & BUTTON_L1))){
119+
if (loadEmu()) return 1;
120+
}
121+
else if (a9lhSetup){
122+
//Patch FIRM partitions writes on SysNAND to protect A9LH
123+
u32 writeOffset = 0;
124+
getFIRMWrite(firmLocation, firmSize, &writeOffset);
125+
memcpy((u8*)writeOffset, FIRMblock, sizeof(FIRMblock));
126+
}
106127

107128
u32 sigOffset = 0,
108129
sigOffset2 = 0;
@@ -112,8 +133,8 @@ u8 patchFirm(void){
112133
memcpy((u8*)sigOffset, sigPat1, sizeof(sigPat1));
113134
memcpy((u8*)sigOffset2, sigPat2, sizeof(sigPat2));
114135

115-
//Apply FIRM reboot patch. Not needed with A9LH and N3DS.
116-
if(!console && !a9lh && mode &&
136+
//Apply FIRM reboot patch. Not needed with A9LH and N3DS
137+
if(!console && !a9lhSetup && mode &&
117138
((fileSize("/rei/reversereboot") > 0) == (pressed & BUTTON_A))){
118139
u32 rebootOffset = 0,
119140
rebootOffset2 = 0;

source/main.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,14 @@
1010
#include "firm.h"
1111
#include "draw.h"
1212

13-
u8 a9lh = 0;
13+
u8 a9lhBoot = 0;
1414

1515
u8 main(){
1616
mountSD();
17-
//Detect A9LH mode checking PDN_GPU_CNT2 register.
18-
if (!*((u32*)0x10141204)) a9lh = 1;
17+
//Detect an A9LH boot checking PDN_GPU_CNT register
18+
if (*((u8*)0x10141200) == 0x1) a9lhBoot = 1;
1919
else loadSplash();
20-
if (loadFirm(a9lh)) return 1;
20+
if (loadFirm(a9lhBoot)) return 1;
2121
if (patchFirm()) return 1;
2222
launchFirm();
2323
return 0;

source/patches.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ u8 nandRedir[0x08] = {0x00, 0x4C, 0xA0, 0x47, 0xC0, 0xA5, 0x01, 0x08}; //Bran
2727
*/
2828
u8 sigPat1[2] = {0x00, 0x20};
2929
u8 sigPat2[4] = {0x00, 0x20, 0x70, 0x47};
30+
u8 FIRMblock[4] = {0x00, 0x20, 0xC0, 0x46};
3031

3132
/**************************************************
3233
* Functions
@@ -48,4 +49,12 @@ void getReboot(void *pos, u32 size, u32 *off, u32 *off2){
4849

4950
*off = (u32)memsearch(pos, pattern, size, 5) + 2;
5051
*off2 = (u32)memsearch(pos, pattern2, size, 5);
52+
}
53+
54+
void getFIRMWrite(void *pos, u32 size, u32 *off){
55+
//Look for FIRM writing code
56+
void *firmwrite = memsearch(pos, "exe:/", size, 5);
57+
unsigned char pattern[] = {0x00, 0x28, 0x01, 0xDA};
58+
59+
*off = (u32)memsearch(firmwrite - 0x100, pattern, 0x100, 4);
5160
}

source/patches.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,13 @@ u8 mpu[0x2C];
1515
u8 nandRedir[0x08];
1616
u8 sigPat1[2];
1717
u8 sigPat2[4];
18+
u8 FIRMblock[4];
1819

1920
/**************************************************
2021
* Functions
2122
**************************************************/
2223
void getSignatures(void *pos, u32 size, u32 *off, u32 *off2);
2324
void getReboot(void *pos, u32 size, u32 *off, u32 *off2);
25+
void getFIRMWrite(void *pos, u32 size, u32 *off);
2426

2527
#endif

0 commit comments

Comments
 (0)