Merge branch 'master' into developer

Conflicts:
	source/firm.c
	source/patches.h
This commit is contained in:
TuxSH 2016-06-13 23:16:33 +02:00
commit 0ced86b55f
15 changed files with 472 additions and 375 deletions

18
source/cache.h Normal file
View File

@ -0,0 +1,18 @@
/*
* cache.h
* by TuxSH
*/
#pragma once
#include "types.h"
/***
The following functions flush the data cache, then waits for all memory transfers to be finished.
The data cache MUST be flushed before doing one of the following:
- rebooting
- powering down
- setting the ARM11 entrypoint to execute a function
***/
void flushEntireDCache(void);
void flushDCacheRange(void *startAddress, u32 size);

51
source/cache.s Normal file
View File

@ -0,0 +1,51 @@
@
@ cache.s
@ by TuxSH
@
@ This is part of Luma3DS, see LICENSE.txt for details
@
.text
.arm
.align 4
.global flushEntireDCache
.type flushEntireDCache, %function
flushEntireDCache:
@ Adpated from http://infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.ddi0155a/ch03s03s05.html ,
@ and https://github.com/gemarcano/libctr9_io/blob/master/src/ctr_system_ARM.c#L39 as well
@ Note: ARM's example is actually for a 8KB DCache (which is what the 3DS has)
mov r1, #0 @ segment counter
outer_loop:
mov r0, #0 @ line counter
inner_loop:
orr r2, r1, r0 @ generate segment and line address
mcr p15, 0, r2, c7, c14, 2 @ clean and flush the line
add r0, #0x20 @ increment to next line
cmp r0, #0x400
bne inner_loop
add r1, #0x40000000
cmp r1, #0
bne outer_loop
mcr p15, 0, r0, c7, c10, 4 @ drain write buffer
bx lr
.global flushDCacheRange
.type flushDCacheRange, %function
flushDCacheRange:
add r1, r0, r1 @ end address
bic r0, #0x1f @ align source address to cache line size (32 bytes)
flush_range_loop:
mcr p15, 0, r0, c7, c14, 1 @ clean and flush the line corresponding to the address r0 is holding
add r0, #0x20
cmp r0, r1
bls flush_range_loop
mcr p15, 0, r0, c7, c10, 4 @ drain write buffer
bx lr

View File

@ -289,7 +289,7 @@ void ctrNandInit(void)
sha(shaSum, cid, 0x10, SHA_256_MODE); sha(shaSum, cid, 0x10, SHA_256_MODE);
memcpy(nandCTR, shaSum, 0x10); memcpy(nandCTR, shaSum, 0x10);
if(console) if(isN3DS)
{ {
u8 keyY0x5[0x10] = {0x4D, 0x80, 0x4F, 0x4E, 0x99, 0x90, 0x19, 0x46, 0x13, 0xA2, 0x04, 0xAC, 0x58, 0x44, 0x60, 0xBE}; u8 keyY0x5[0x10] = {0x4D, 0x80, 0x4F, 0x4E, 0x99, 0x90, 0x19, 0x46, 0x13, 0xA2, 0x04, 0xAC, 0x58, 0x44, 0x60, 0xBE};
aes_setkey(0x05, keyY0x5, AES_KEYY, AES_INPUT_BE | AES_INPUT_NORMAL); aes_setkey(0x05, keyY0x5, AES_KEYY, AES_INPUT_BE | AES_INPUT_NORMAL);
@ -312,7 +312,7 @@ u32 ctrNandRead(u32 sector, u32 sectorCount, u8 *outbuf)
//Read //Read
u32 result; u32 result;
if(!firmSource) if(firmSource == FIRMWARE_SYSNAND)
result = sdmmc_nand_readsectors(sector + fatStart, sectorCount, outbuf); result = sdmmc_nand_readsectors(sector + fatStart, sectorCount, outbuf);
else else
{ {
@ -368,7 +368,7 @@ void arm9Loader(u8 *arm9Section, u32 mode)
key2[0x10] = {0x42, 0x3F, 0x81, 0x7A, 0x23, 0x52, 0x58, 0x31, 0x6E, 0x75, 0x8E, 0x3A, 0x39, 0x43, 0x2E, 0xD0}; key2[0x10] = {0x42, 0x3F, 0x81, 0x7A, 0x23, 0x52, 0x58, 0x31, 0x6E, 0x75, 0x8E, 0x3A, 0x39, 0x43, 0x2E, 0xD0};
u8 keyX[0x10]; u8 keyX[0x10];
aes_setkey(0x11, mode == 1 ? key2 : key1, AES_KEYNORMAL, AES_INPUT_BE | AES_INPUT_NORMAL); aes_setkey(0x11, mode == 2 ? key2 : key1, AES_KEYNORMAL, AES_INPUT_BE | AES_INPUT_NORMAL);
aes_use_keyslot(0x11); aes_use_keyslot(0x11);
aes(keyX, arm9Section + 0x60, 1, NULL, AES_ECB_DECRYPT_MODE, 0); aes(keyX, arm9Section + 0x60, 1, NULL, AES_ECB_DECRYPT_MODE, 0);
aes_setkey(arm9BinSlot, keyX, AES_KEYX, AES_INPUT_BE | AES_INPUT_NORMAL); aes_setkey(arm9BinSlot, keyX, AES_KEYX, AES_INPUT_BE | AES_INPUT_NORMAL);
@ -382,7 +382,7 @@ void arm9Loader(u8 *arm9Section, u32 mode)
aes(arm9Section + 0x800, arm9Section + 0x800, arm9BinSize / AES_BLOCK_SIZE, arm9BinCTR, AES_CTR_MODE, AES_INPUT_BE | AES_INPUT_NORMAL); aes(arm9Section + 0x800, arm9Section + 0x800, arm9BinSize / AES_BLOCK_SIZE, arm9BinCTR, AES_CTR_MODE, AES_INPUT_BE | AES_INPUT_NORMAL);
//Set >=9.6 KeyXs //Set >=9.6 KeyXs
if(mode == 1) if(mode == 2)
{ {
u8 keyData[0x10] = {0xDD, 0xDA, 0xA4, 0xC6, 0x2C, 0xC4, 0x50, 0xE9, 0xDA, 0xB6, 0x9B, 0x0D, 0x9D, 0x2A, 0x21, 0x98}, u8 keyData[0x10] = {0xDD, 0xDA, 0xA4, 0xC6, 0x2C, 0xC4, 0x50, 0xE9, 0xDA, 0xB6, 0x9B, 0x0D, 0x9D, 0x2A, 0x21, 0x98},
decKey[0x10]; decKey[0x10];

View File

@ -79,7 +79,8 @@
#define SHA_224_HASH_SIZE (224 / 8) #define SHA_224_HASH_SIZE (224 / 8)
#define SHA_1_HASH_SIZE (160 / 8) #define SHA_1_HASH_SIZE (160 / 8)
extern u32 emuOffset, console, firmSource; extern u32 emuOffset, isN3DS;
extern FirmwareSource firmSource;
void ctrNandInit(void); void ctrNandInit(void);
u32 ctrNandRead(u32 sector, u32 sectorCount, u8 *outbuf); u32 ctrNandRead(u32 sector, u32 sectorCount, u8 *outbuf);

View File

@ -7,12 +7,12 @@
#include "fatfs/sdmmc/sdmmc.h" #include "fatfs/sdmmc/sdmmc.h"
#include "../build/emunandpatch.h" #include "../build/emunandpatch.h"
void locateEmuNAND(u32 *off, u32 *head, u32 *emuNAND) void locateEmuNAND(u32 *off, u32 *head, FirmwareSource *emuNAND)
{ {
static u8 *const temp = (u8 *)0x24300000; static u8 *const temp = (u8 *)0x24300000;
const u32 nandSize = getMMCDevice(0)->total_size; const u32 nandSize = getMMCDevice(0)->total_size;
u32 nandOffset = *emuNAND == 1 ? 0 : u32 nandOffset = *emuNAND == FIRMWARE_EMUNAND ? 0 :
(nandSize > 0x200000 ? 0x400000 : 0x200000); (nandSize > 0x200000 ? 0x400000 : 0x200000);
//Check for RedNAND //Check for RedNAND
@ -35,7 +35,7 @@ void locateEmuNAND(u32 *off, u32 *head, u32 *emuNAND)
or to SysNAND if there isn't any */ or to SysNAND if there isn't any */
else else
{ {
(*emuNAND)--; *emuNAND = (*emuNAND == FIRMWARE_EMUNAND2) ? FIRMWARE_EMUNAND : FIRMWARE_SYSNAND;
if(*emuNAND) locateEmuNAND(off, head, emuNAND); if(*emuNAND) locateEmuNAND(off, head, emuNAND);
} }
} }

View File

@ -8,5 +8,5 @@
#define NCSD_MAGIC 0x4453434E #define NCSD_MAGIC 0x4453434E
void locateEmuNAND(u32 *off, u32 *head, u32 *emuNAND); void locateEmuNAND(u32 *off, u32 *head, FirmwareSource *emuNAND);
void patchEmuNAND(u8 *arm9Section, u32 arm9SectionSize, u8 *process9Offset, u32 process9Size, u32 emuOffset, u32 emuHeader, u32 branchAdditive); void patchEmuNAND(u8 *arm9Section, u32 arm9SectionSize, u8 *process9Offset, u32 process9Size, u32 emuOffset, u32 emuHeader, u32 branchAdditive);

View File

@ -8,6 +8,7 @@
#include "fs.h" #include "fs.h"
#include "patches.h" #include "patches.h"
#include "memory.h" #include "memory.h"
#include "cache.h"
#include "emunand.h" #include "emunand.h"
#include "crypto.h" #include "crypto.h"
#include "exceptions.h" #include "exceptions.h"
@ -20,24 +21,26 @@ static firmHeader *const firm = (firmHeader *)0x24000000;
static const firmSectionHeader *section; static const firmSectionHeader *section;
u32 config, u32 config,
console, isN3DS,
firmSource,
emuOffset; emuOffset;
FirmwareSource firmSource;
void main(void) void main(void)
{ {
u32 bootType, u32 isFirmlaunch,
firmType,
nandType,
a9lhMode,
updatedSys, updatedSys,
needConfig,
newConfig, newConfig,
emuHeader, emuHeader,
chronoStarted = 0; nbChronoStarted = 0;
FirmwareType firmType;
FirmwareSource nandType;
ConfigurationStatus needConfig;
A9LHMode a9lhMode;
//Detect the console being used //Detect the console being used
console = PDN_MPCORE_CFG == 7; isN3DS = PDN_MPCORE_CFG == 7;
//Mount filesystems. CTRNAND will be mounted only if/when needed //Mount filesystems. CTRNAND will be mounted only if/when needed
mountFs(); mountFs();
@ -45,43 +48,7 @@ void main(void)
const char configPath[] = "/luma/config.bin"; const char configPath[] = "/luma/config.bin";
//Attempt to read the configuration file //Attempt to read the configuration file
needConfig = fileRead(&config, configPath) ? 1 : 2; needConfig = fileRead(&config, configPath) ? MODIFY_CONFIGURATION : CREATE_CONFIGURATION;
//Determine if this is a firmlaunch boot
if(*(vu8 *)0x23F00005)
{
if(needConfig == 2) mcuReboot();
bootType = 1;
//'0' = NATIVE_FIRM, '1' = TWL_FIRM, '2' = AGB_FIRM
firmType = *(vu8 *)0x23F00009 == '3' ? 3 : *(vu8 *)0x23F00005 - '0';
nandType = BOOTCONFIG(0, 3);
firmSource = BOOTCONFIG(2, 1);
a9lhMode = BOOTCONFIG(3, 1);
updatedSys = a9lhMode && CONFIG(1);
}
else
{
//Get pressed buttons
u32 pressed = HID_PAD;
//If no configuration file exists or SELECT is held, load configuration menu
if(needConfig == 2 || ((pressed & BUTTON_SELECT) && !(pressed & BUTTON_L1)))
{
configureCFW(configPath);
//Zero the last booted FIRM flag
CFG_BOOTENV = 0;
chronoStarted = 1;
chrono(0);
chrono(2);
//Update pressed buttons
pressed = HID_PAD;
}
if(DEVMODE) if(DEVMODE)
{ {
@ -89,8 +56,44 @@ void main(void)
installArm9Handlers(); installArm9Handlers();
} }
bootType = 0; //Determine if this is a firmlaunch boot
firmType = 0; if(*(vu8 *)0x23F00005)
{
if(needConfig == CREATE_CONFIGURATION) mcuReboot();
isFirmlaunch = 1;
//'0' = NATIVE_FIRM, '1' = TWL_FIRM, '2' = AGB_FIRM
firmType = *(vu8 *)0x23F00009 == '3' ? SAFE_FIRM : (FirmwareType)(*(vu8 *)0x23F00005 - '0');
nandType = (FirmwareSource)BOOTCONFIG(0, 3);
firmSource = (FirmwareSource)BOOTCONFIG(2, 1);
a9lhMode = (A9LHMode)BOOTCONFIG(3, 1);
updatedSys = a9lhMode != NO_A9LH && CONFIG(1);
}
else
{
//Get pressed buttons
u32 pressed = HID_PAD;
//If no configuration file exists or SELECT is held, load configuration menu
if(needConfig == CREATE_CONFIGURATION || ((pressed & BUTTON_SELECT) && !(pressed & BUTTON_L1)))
{
configureCFW(configPath);
//Zero the last booted FIRM flag
CFG_BOOTENV = 0;
nbChronoStarted = 1;
chrono(0);
chrono(2);
//Update pressed buttons
pressed = HID_PAD;
}
isFirmlaunch = 0;
firmType = NATIVE_FIRM;
//Determine if booting with A9LH //Determine if booting with A9LH
u32 a9lhBoot = !PDN_SPI_CNT; u32 a9lhBoot = !PDN_SPI_CNT;
@ -98,31 +101,28 @@ void main(void)
//Determine if A9LH is installed and the user has an updated sysNAND //Determine if A9LH is installed and the user has an updated sysNAND
if(a9lhBoot || CONFIG(2)) if(a9lhBoot || CONFIG(2))
{ {
a9lhMode = 1; a9lhMode = A9LH_WITH_NFIRM_FIRMPROT;
updatedSys = CONFIG(1); updatedSys = CONFIG(1);
} }
else else
{ {
a9lhMode = 0; a9lhMode = NO_A9LH;
updatedSys = 0; updatedSys = 0;
} }
newConfig = a9lhMode << 3; newConfig = (u32)a9lhMode << 3;
if(a9lhBoot) if(a9lhBoot)
{ {
//Retrieve the last booted FIRM
u32 previousFirm = CFG_BOOTENV;
//If it's a MCU reboot, try to force boot options //If it's a MCU reboot, try to force boot options
if(previousFirm) if(CFG_BOOTENV)
{ {
//Always force a sysNAND boot when quitting AGB_FIRM //Always force a sysNAND boot when quitting AGB_FIRM
if(previousFirm == 7) if(CFG_BOOTENV == 7)
{ {
nandType = 0; nandType = FIRMWARE_SYSNAND;
firmSource = updatedSys ? 0 : BOOTCONFIG(2, 1); firmSource = updatedSys ? FIRMWARE_SYSNAND : (FirmwareSource)BOOTCONFIG(2, 1);
needConfig = 0; needConfig = DONT_CONFIGURE;
//Flag to prevent multiple boot options-forcing //Flag to prevent multiple boot options-forcing
newConfig |= 1 << 4; newConfig |= 1 << 4;
@ -132,19 +132,19 @@ void main(void)
or the no-forcing flag is set */ or the no-forcing flag is set */
else if(!pressed && !BOOTCONFIG(4, 1)) else if(!pressed && !BOOTCONFIG(4, 1))
{ {
nandType = BOOTCONFIG(0, 3); nandType = (FirmwareSource)BOOTCONFIG(0, 3);
firmSource = BOOTCONFIG(2, 1); firmSource = (FirmwareSource)BOOTCONFIG(2, 1);
needConfig = 0; needConfig = DONT_CONFIGURE;
} }
} }
//If the SAFE MODE combo is held, force a sysNAND boot //If the SAFE MODE combo is held, force a sysNAND boot
else if(pressed == SAFE_MODE) else if(pressed == SAFE_MODE)
{ {
a9lhMode = 2; a9lhMode = A9LH_WITH_SFIRM_FIRMPROT;
nandType = 0; nandType = FIRMWARE_SYSNAND;
firmSource = 0; firmSource = FIRMWARE_SYSNAND;
needConfig = 0; needConfig = DONT_CONFIGURE;
} }
} }
@ -159,45 +159,45 @@ void main(void)
//If screens are inited or the corresponding option is set, load splash screen //If screens are inited or the corresponding option is set, load splash screen
if((PDN_GPU_CNT != 1 || CONFIG(7)) && loadSplash()) if((PDN_GPU_CNT != 1 || CONFIG(7)) && loadSplash())
{ {
chronoStarted = 2; nbChronoStarted = 2;
chrono(0); chrono(0);
} }
//If R is pressed, boot the non-updated NAND with the FIRM of the opposite one //If R is pressed, boot the non-updated NAND with the FIRM of the opposite one
if(pressed & BUTTON_R1) if(pressed & BUTTON_R1)
{ {
nandType = updatedSys; nandType = (updatedSys) ? FIRMWARE_EMUNAND : FIRMWARE_SYSNAND;
firmSource = !nandType; firmSource = (updatedSys) ? FIRMWARE_SYSNAND : FIRMWARE_EMUNAND;
} }
/* Else, boot the NAND the user set to autoboot or the opposite one, depending on L, /* Else, boot the NAND the user set to autoboot or the opposite one, depending on L,
with their own FIRM */ with their own FIRM */
else else
{ {
nandType = CONFIG(0) != !(pressed & BUTTON_L1); nandType = (CONFIG(0) != !(pressed & BUTTON_L1)) ? FIRMWARE_EMUNAND : FIRMWARE_SYSNAND;
firmSource = nandType; firmSource = nandType;
} }
/* If we're booting emuNAND the second emuNAND is set as default and B isn't pressed, /* If we're booting emuNAND the second emuNAND is set as default and B isn't pressed,
or vice-versa, boot the second emuNAND */ or vice-versa, boot the second emuNAND */
if(nandType && (CONFIG(3) == !(pressed & BUTTON_B))) nandType = 2; if(nandType != FIRMWARE_SYSNAND && (CONFIG(3) == !(pressed & BUTTON_B))) nandType = FIRMWARE_EMUNAND2;
} }
} }
//If we need to boot emuNAND, make sure it exists //If we need to boot emuNAND, make sure it exists
if(nandType) if(nandType != FIRMWARE_SYSNAND)
{ {
locateEmuNAND(&emuOffset, &emuHeader, &nandType); locateEmuNAND(&emuOffset, &emuHeader, &nandType);
if(!nandType) firmSource = 0; if(nandType == FIRMWARE_SYSNAND) firmSource = FIRMWARE_SYSNAND;
} }
//Same if we're using emuNAND as the FIRM source //Same if we're using emuNAND as the FIRM source
else if(firmSource) else if(firmSource != FIRMWARE_SYSNAND)
locateEmuNAND(&emuOffset, &emuHeader, &firmSource); locateEmuNAND(&emuOffset, &emuHeader, &firmSource);
if(!bootType) if(!isFirmlaunch)
{ {
newConfig |= nandType | (firmSource << 2); newConfig |= (u32)nandType | ((u32)firmSource << 2);
/* If the boot configuration is different from previously, overwrite it. /* If the boot configuration is different from previously, overwrite it.
Just the no-forcing flag being set is not enough */ Just the no-forcing flag being set is not enough */
@ -210,14 +210,14 @@ void main(void)
} }
} }
loadFirm(firmType, !firmType && updatedSys == !firmSource); loadFirm(firmType, firmType == NATIVE_FIRM && firmSource == ((updatedSys) ? FIRMWARE_SYSNAND : FIRMWARE_EMUNAND));
switch(firmType) switch(firmType)
{ {
case 0: case NATIVE_FIRM:
patchNativeFirm(nandType, emuHeader, a9lhMode); patchNativeFirm(nandType, emuHeader, a9lhMode);
break; break;
case 3: case SAFE_FIRM:
patchSafeFirm(); patchSafeFirm();
break; break;
default: default:
@ -225,22 +225,22 @@ void main(void)
break; break;
} }
if(chronoStarted) if(nbChronoStarted)
{ {
if(chronoStarted == 2) chrono(3); if(nbChronoStarted == 2) chrono(3);
stopChrono(); stopChrono();
} }
launchFirm(firmType, bootType); launchFirm(firmType, isFirmlaunch);
} }
static inline void loadFirm(u32 firmType, u32 externalFirm) static inline void loadFirm(FirmwareType firmType, u32 externalFirm)
{ {
section = firm->section; section = firm->section;
u32 externalFirmLoaded = externalFirm && u32 externalFirmLoaded = externalFirm &&
fileRead(firm, "/luma/firmware.bin") && fileRead(firm, "/luma/firmware.bin") &&
(((u32)section[2].address >> 8) & 0xFF) == (console ? 0x60 : 0x68); (((u32)section[2].address >> 8) & 0xFF) == (isN3DS ? 0x60 : 0x68);
/* If the conditions to load the external FIRM aren't met, or reading fails, or the FIRM /* If the conditions to load the external FIRM aren't met, or reading fails, or the FIRM
doesn't match the console, load FIRM from CTRNAND */ doesn't match the console, load FIRM from CTRNAND */
@ -251,43 +251,46 @@ static inline void loadFirm(u32 firmType, u32 externalFirm)
{ "00000202", "20000202" }, { "00000202", "20000202" },
{ "00000003", "20000003" }}; { "00000003", "20000003" }};
firmRead(firm, firmFolders[firmType][console]); firmRead(firm, firmFolders[(u32)firmType][isN3DS]);
decryptExeFs((u8 *)firm); decryptExeFs((u8 *)firm);
} }
} }
static inline void patchNativeFirm(u32 nandType, u32 emuHeader, u32 a9lhMode) static inline void patchNativeFirm(FirmwareSource nandType, u32 emuHeader, A9LHMode a9lhMode)
{ {
u8 *arm9Section = (u8 *)firm + section[2].offset, u8 *arm9Section = (u8 *)firm + section[2].offset,
*arm11Section1 = (u8 *)firm + section[1].offset; *arm11Section1 = (u8 *)firm + section[1].offset;
u32 nativeFirmType; u32 is90Firm;
if(console) if(isN3DS)
{ {
//Determine the NATIVE_FIRM version u32 a9lVersion;
//Determine the NATIVE_FIRM/arm9loader version
switch(arm9Section[0x53]) switch(arm9Section[0x53])
{ {
case 0xFF: case 0xFF:
nativeFirmType = 0; a9lVersion = 0;
break; break;
case '1': case '1':
nativeFirmType = 2; a9lVersion = 1;
break; break;
default: default:
nativeFirmType = 1; a9lVersion = 2;
break; break;
} }
//Decrypt ARM9Bin and patch ARM9 entrypoint to skip arm9loader //Decrypt ARM9Bin and patch ARM9 entrypoint to skip arm9loader
arm9Loader(arm9Section, nativeFirmType); arm9Loader(arm9Section, a9lVersion);
firm->arm9Entry = (u8 *)0x801B01C; firm->arm9Entry = (u8 *)0x801B01C;
is90Firm = a9lVersion == 0;
} }
else else
{ {
//Determine if we're booting the 9.0 FIRM //Determine if we're booting the 9.0 FIRM
u8 firm90Hash[0x10] = {0x27, 0x2D, 0xFE, 0xEB, 0xAF, 0x3F, 0x6B, 0x3B, 0xF5, 0xDE, 0x4C, 0x41, 0xDE, 0x95, 0x27, 0x6A}; u8 firm90Hash[0x10] = {0x27, 0x2D, 0xFE, 0xEB, 0xAF, 0x3F, 0x6B, 0x3B, 0xF5, 0xDE, 0x4C, 0x41, 0xDE, 0x95, 0x27, 0x6A};
nativeFirmType = memcmp(section[2].hash, firm90Hash, 0x10) != 0; is90Firm = memcmp(section[2].hash, firm90Hash, 0x10) == 0;
} }
//Find the Process9 .code location, size and memory address //Find the Process9 .code location, size and memory address
@ -306,12 +309,12 @@ static inline void patchNativeFirm(u32 nandType, u32 emuHeader, u32 a9lhMode)
} }
//Apply FIRM0/1 writes patches on sysNAND to protect A9LH //Apply FIRM0/1 writes patches on sysNAND to protect A9LH
else if(a9lhMode) patchFirmWrites(process9Offset, process9Size); else if(a9lhMode != NO_A9LH) patchFirmWrites(process9Offset, process9Size);
//Apply firmlaunch patches, not on 9.0 FIRM as it breaks firmlaunchhax //Apply firmlaunch patches, not on 9.0 FIRM as it breaks firmlaunchhax
if(nativeFirmType || a9lhMode == 2) patchFirmlaunches(process9Offset, process9Size, process9MemAddr); if(!is90Firm || a9lhMode == A9LH_WITH_SFIRM_FIRMPROT) patchFirmlaunches(process9Offset, process9Size, process9MemAddr);
if(nativeFirmType == 1) if(!is90Firm)
{ {
//Apply anti-anti-DG patches for >= 11.0 firmwares //Apply anti-anti-DG patches for >= 11.0 firmwares
patchTitleInstallMinVersionCheck(process9Offset, process9Size); patchTitleInstallMinVersionCheck(process9Offset, process9Size);
@ -342,12 +345,12 @@ static inline void patchNativeFirm(u32 nandType, u32 emuHeader, u32 a9lhMode)
} }
} }
static inline void patchLegacyFirm(u32 firmType) static inline void patchLegacyFirm(FirmwareType firmType)
{ {
u8 *arm9Section = (u8 *)firm + section[3].offset; u8 *arm9Section = (u8 *)firm + section[3].offset;
//On N3DS, decrypt ARM9Bin and patch ARM9 entrypoint to skip arm9loader //On N3DS, decrypt ARM9Bin and patch ARM9 entrypoint to skip arm9loader
if(console) if(isN3DS)
{ {
arm9Loader(arm9Section, 0); arm9Loader(arm9Section, 0);
firm->arm9Entry = (u8 *)0x801301C; firm->arm9Entry = (u8 *)0x801301C;
@ -360,14 +363,14 @@ static inline void patchLegacyFirm(u32 firmType)
patchSvcBreak9(arm9Section, section[3].size, (u32)(section[3].address)); patchSvcBreak9(arm9Section, section[3].size, (u32)(section[3].address));
} }
applyLegacyFirmPatches((u8 *)firm, firmType, console); applyLegacyFirmPatches((u8 *)firm, firmType, isN3DS);
} }
static inline void patchSafeFirm(void) static inline void patchSafeFirm(void)
{ {
u8 *arm9Section = (u8 *)firm + section[2].offset; u8 *arm9Section = (u8 *)firm + section[2].offset;
if(console) if(isN3DS)
{ {
//Decrypt ARM9Bin and patch ARM9 entrypoint to skip arm9loader //Decrypt ARM9Bin and patch ARM9 entrypoint to skip arm9loader
arm9Loader(arm9Section, 0); arm9Loader(arm9Section, 0);
@ -398,11 +401,11 @@ static inline void copySection0AndInjectLoader(void)
memcpy(section[0].address + loaderOffset + injector_size, arm11Section0 + loaderOffset + loaderSize, section[0].size - (loaderOffset + loaderSize)); memcpy(section[0].address + loaderOffset + injector_size, arm11Section0 + loaderOffset + loaderSize, section[0].size - (loaderOffset + loaderSize));
} }
static inline void launchFirm(u32 firmType, u32 bootType) static inline void launchFirm(FirmwareType firmType, u32 isFirmlaunch)
{ {
//If we're booting NATIVE_FIRM, section0 needs to be copied separately to inject 3ds_injector //If we're booting NATIVE_FIRM, section0 needs to be copied separately to inject 3ds_injector
u32 sectionNum; u32 sectionNum;
if(!firmType) if(firmType == NATIVE_FIRM)
{ {
copySection0AndInjectLoader(); copySection0AndInjectLoader();
sectionNum = 1; sectionNum = 1;
@ -415,14 +418,14 @@ static inline void launchFirm(u32 firmType, u32 bootType)
//Determine the ARM11 entry to use //Determine the ARM11 entry to use
vu32 *arm11; vu32 *arm11;
if(bootType) arm11 = (u32 *)0x1FFFFFFC; if(isFirmlaunch) arm11 = (u32 *)0x1FFFFFFC;
else else
{ {
deinitScreens(); deinitScreens();
arm11 = (u32 *)0x1FFFFFF8; arm11 = (u32 *)0x1FFFFFF8;
} }
cleanInvalidateDCacheAndDMB(); //Ensure that all memory transfers have completed and that the data cache has been flushed flushEntireDCache(); //Ensure that all memory transfers have completed and that the data cache has been flushed
//Set ARM11 kernel entrypoint //Set ARM11 kernel entrypoint
*arm11 = (u32)firm->arm11Entry; *arm11 = (u32)firm->arm11Entry;

View File

@ -28,9 +28,23 @@ typedef struct firmHeader {
firmSectionHeader section[4]; firmSectionHeader section[4];
} firmHeader; } firmHeader;
static inline void loadFirm(u32 firmType, u32 externalFirm); typedef enum ConfigurationStatus
static inline void patchNativeFirm(u32 nandType, u32 emuHeader, u32 a9lhMode); {
static inline void patchLegacyFirm(u32 firmType); DONT_CONFIGURE = 0,
MODIFY_CONFIGURATION = 1,
CREATE_CONFIGURATION = 2
} ConfigurationStatus;
typedef enum A9LHMode
{
NO_A9LH = 0,
A9LH_WITH_NFIRM_FIRMPROT = 1,
A9LH_WITH_SFIRM_FIRMPROT = 2
} A9LHMode;
static inline void loadFirm(FirmwareType firmType, u32 externalFirm);
static inline void patchNativeFirm(FirmwareSource nandType, u32 emuHeader, A9LHMode a9lhMode);
static inline void patchLegacyFirm(FirmwareType firmType);
static inline void patchSafeFirm(void); static inline void patchSafeFirm(void);
static inline void copySection0AndInjectLoader(void); static inline void copySection0AndInjectLoader(void);
static inline void launchFirm(u32 sectionNum, u32 bootType); static inline void launchFirm(FirmwareType firmType, u32 isFirmlaunch);

View File

@ -61,8 +61,3 @@ u8 *memsearch(u8 *startPos, const void *pattern, u32 size, u32 patternSize)
return NULL; return NULL;
} }
void cleanInvalidateDCacheAndDMB(void)
{
((void (*)())0xFFFF0830)(); //Why write our own code when it's well implemented in the unprotected bootROM?
}

View File

@ -12,12 +12,3 @@ void memcpy(void *dest, const void *src, u32 size);
void memset32(void *dest, u32 filler, u32 size); void memset32(void *dest, u32 filler, u32 size);
int memcmp(const void *buf1, const void *buf2, u32 size); int memcmp(const void *buf1, const void *buf2, u32 size);
u8 *memsearch(u8 *startPos, const void *pattern, u32 size, u32 patternSize); u8 *memsearch(u8 *startPos, const void *pattern, u32 size, u32 patternSize);
/***
Cleans and invalidates the data cache, then waits for all memory transfers to be finished.
This function MUST be called before doing the following:
- rebooting
- powering down
- setting the ARM11 entrypoint to execute a function
***/
void cleanInvalidateDCacheAndDMB(void);

View File

@ -225,7 +225,7 @@ void patchTitleInstallMinVersionCheck(u8 *pos, u32 size)
if(off != NULL) off[4] = 0xE0; if(off != NULL) off[4] = 0xE0;
} }
void applyLegacyFirmPatches(u8 *pos, u32 firmType, u32 console) void applyLegacyFirmPatches(u8 *pos, FirmwareType firmType, u32 isN3DS)
{ {
const patchData twlPatches[] = { const patchData twlPatches[] = {
{{0x1650C0, 0x165D64}, {{ 6, 0x00, 0x20, 0x4E, 0xB0, 0x70, 0xBD }}, 0}, {{0x1650C0, 0x165D64}, {{ 6, 0x00, 0x20, 0x4E, 0xB0, 0x70, 0xBD }}, 0},
@ -245,9 +245,9 @@ void applyLegacyFirmPatches(u8 *pos, u32 firmType, u32 console)
/* Calculate the amount of patches to apply. Only count the boot screen patch for AGB_FIRM /* Calculate the amount of patches to apply. Only count the boot screen patch for AGB_FIRM
if the matching option was enabled (keep it as last) */ if the matching option was enabled (keep it as last) */
u32 numPatches = firmType == 1 ? (sizeof(twlPatches) / sizeof(patchData)) : u32 numPatches = firmType == TWL_FIRM ? (sizeof(twlPatches) / sizeof(patchData)) :
(sizeof(agbPatches) / sizeof(patchData) - !CONFIG(6)); (sizeof(agbPatches) / sizeof(patchData) - !CONFIG(6));
const patchData *patches = firmType == 1 ? twlPatches : agbPatches; const patchData *patches = firmType == TWL_FIRM ? twlPatches : agbPatches;
//Patch //Patch
for(u32 i = 0; i < numPatches; i++) for(u32 i = 0; i < numPatches; i++)
@ -255,12 +255,12 @@ void applyLegacyFirmPatches(u8 *pos, u32 firmType, u32 console)
switch(patches[i].type) switch(patches[i].type)
{ {
case 0: case 0:
memcpy(pos + patches[i].offset[console], patches[i].patch.type0 + 1, patches[i].patch.type0[0]); memcpy(pos + patches[i].offset[isN3DS], patches[i].patch.type0 + 1, patches[i].patch.type0[0]);
break; break;
case 2: case 2:
*(u16 *)(pos + patches[i].offset[console] + 2) = 0; *(u16 *)(pos + patches[i].offset[isN3DS] + 2) = 0;
case 1: case 1:
*(u16 *)(pos + patches[i].offset[console]) = patches[i].patch.type1; *(u16 *)(pos + patches[i].offset[isN3DS]) = patches[i].patch.type1;
break; break;
} }
} }

View File

@ -28,6 +28,6 @@ void patchSvcBreak11(u8 *pos, u32 size);
void patchUnitInfoValueSet(u8 *pos, u32 size); void patchUnitInfoValueSet(u8 *pos, u32 size);
void patchKernelFCRAMAndVRAMMappingPermissions(u8 *pos, u32 size); void patchKernelFCRAMAndVRAMMappingPermissions(u8 *pos, u32 size);
void reimplementSvcBackdoor(u8 *pos, u32 size); void reimplementSvcBackdoor(u8 *pos, u32 size);
void applyLegacyFirmPatches(u8 *pos, u32 firmType, u32 console); void applyLegacyFirmPatches(u8 *pos, FirmwareType firmType, u32 isN3DS);
u32 getLoader(u8 *pos, u32 *loaderSize); u32 getLoader(u8 *pos, u32 *loaderSize);
u8 *getUnitInfoValueSet(u8 *pos, u32 size); u8 *getUnitInfoValueSet(u8 *pos, u32 size);

View File

@ -8,6 +8,7 @@
#include "screen.h" #include "screen.h"
#include "config.h" #include "config.h"
#include "memory.h" #include "memory.h"
#include "cache.h"
#include "draw.h" #include "draw.h"
#include "i2c.h" #include "i2c.h"
@ -30,9 +31,11 @@ void __attribute__((naked)) arm11Stub(void)
static inline void invokeArm11Function(void (*func)()) static inline void invokeArm11Function(void (*func)())
{ {
static u32 hasCopiedStub = 0; static u32 hasCopiedStub = 0;
if(!hasCopiedStub++) memcpy((void *)ARM11_STUB_ADDRESS, arm11Stub, 0x40); if(!hasCopiedStub++)
{
cleanInvalidateDCacheAndDMB(); memcpy((void *)ARM11_STUB_ADDRESS, arm11Stub, 0x40);
flushDCacheRange((void *)ARM11_STUB_ADDRESS, 0x40);
}
*arm11Entry = (u32)func; *arm11Entry = (u32)func;
while(*arm11Entry); while(*arm11Entry);
@ -77,6 +80,7 @@ void updateBrightness(u32 brightnessLevel)
WAIT_FOR_ARM9(); WAIT_FOR_ARM9();
} }
flushDCacheRange(&brightnessValue, 4);
invokeArm11Function(ARM11); invokeArm11Function(ARM11);
} }
@ -116,6 +120,7 @@ void clearScreens(void)
WAIT_FOR_ARM9(); WAIT_FOR_ARM9();
} }
flushDCacheRange(fb, sizeof(struct fb));
invokeArm11Function(ARM11); invokeArm11Function(ARM11);
} }
@ -223,6 +228,8 @@ u32 initScreens(void)
if(needToInit) if(needToInit)
{ {
flushDCacheRange(&config, 4);
flushDCacheRange(fb, sizeof(struct fb));
invokeArm11Function(ARM11); invokeArm11Function(ARM11);
//Turn on backlight //Turn on backlight

View File

@ -16,3 +16,19 @@ typedef volatile u8 vu8;
typedef volatile u16 vu16; typedef volatile u16 vu16;
typedef volatile u32 vu32; typedef volatile u32 vu32;
typedef volatile u64 vu64; typedef volatile u64 vu64;
//Used by multiple files:
typedef enum FirmwareSource
{
FIRMWARE_SYSNAND = 0,
FIRMWARE_EMUNAND = 1,
FIRMWARE_EMUNAND2 = 2
} FirmwareSource;
typedef enum FirmwareType
{
NATIVE_FIRM = 0,
TWL_FIRM = 1,
AGB_FIRM = 2,
SAFE_FIRM = 3
} FirmwareType;

View File

@ -6,6 +6,7 @@
#include "i2c.h" #include "i2c.h"
#include "buttons.h" #include "buttons.h"
#include "memory.h" #include "memory.h"
#include "cache.h"
u32 waitInput(void) u32 waitInput(void)
{ {
@ -36,7 +37,7 @@ u32 waitInput(void)
void mcuReboot(void) void mcuReboot(void)
{ {
cleanInvalidateDCacheAndDMB(); //Ensure that all memory transfers have completed and that the data cache has been flushed flushEntireDCache(); //Ensure that all memory transfers have completed and that the data cache has been flushed
i2cWriteRegister(I2C_DEV_MCU, 0x20, 1 << 2); i2cWriteRegister(I2C_DEV_MCU, 0x20, 1 << 2);
while(1); while(1);
@ -44,7 +45,7 @@ void mcuReboot(void)
void mcuPowerOff(void) void mcuPowerOff(void)
{ {
cleanInvalidateDCacheAndDMB(); //Ensure that all memory transfers have completed and that the data cache has been flushed flushEntireDCache(); //Ensure that all memory transfers have completed and that the data cache has been flushed
i2cWriteRegister(I2C_DEV_MCU, 0x20, 1 << 0); i2cWriteRegister(I2C_DEV_MCU, 0x20, 1 << 0);
while(1); while(1);