Merge changes from master, added support for netloader companion (payload must be "nlc.bin" and will be deleted after being read, moved exception vectors

This commit is contained in:
Aurora
2016-05-03 16:57:00 +02:00
38 changed files with 242 additions and 7517 deletions

View File

@@ -7,6 +7,7 @@
#include "types.h"
#define HID_PAD (*(vu32 *)0x10146000 ^ 0xFFF)
#define BUTTON_R1 (1 << 8)
#define BUTTON_L1 (1 << 9)
#define BUTTON_A 1
@@ -19,6 +20,7 @@
#define BUTTON_LEFT (1 << 5)
#define BUTTON_UP (1 << 6)
#define BUTTON_DOWN (1 << 7)
#define SAFE_MODE (BUTTON_R1 | BUTTON_L1 | BUTTON_A | BUTTON_UP)
#define OVERRIDE_BUTTONS (BUTTON_B ^ 0xFFF)
#define SINGLE_PAYLOAD_BUTTONS (BUTTON_LEFT | BUTTON_RIGHT | BUTTON_UP | BUTTON_DOWN | BUTTON_START | BUTTON_X | BUTTON_Y)

View File

@@ -10,9 +10,6 @@
#include "memory.h"
#include "font.h"
#define SCREEN_TOP_WIDTH 400
#define SCREEN_TOP_HEIGHT 240
static const struct fb {
u8 *top_left;
u8 *top_right;

View File

@@ -8,8 +8,12 @@
#include "types.h"
#define SPACING_Y 10
#define SPACING_X 8
#define SCREEN_TOP_WIDTH 400
#define SCREEN_TOP_HEIGHT 240
#define SPACING_Y 10
#define SPACING_X 8
#define COLOR_TITLE 0xFF9900
#define COLOR_WHITE 0xFFFFFF
#define COLOR_RED 0x0000FF

View File

@@ -15,31 +15,27 @@ void getEmunandSect(u32 *off, u32 *head, u32 *emuNAND)
(nandSize > 0x200000 ? 0x400000 : 0x200000);
//Check for RedNAND
if(sdmmc_sdcard_readsectors(nandOffset + 1, 1, temp) == 0)
if(!sdmmc_sdcard_readsectors(nandOffset + 1, 1, temp) &&
*(u32 *)(temp + 0x100) == NCSD_MAGIC)
{
if(*(u32 *)(temp + 0x100) == NCSD_MAGIC)
{
*off = nandOffset + 1;
*head = nandOffset + 1;
}
*off = nandOffset + 1;
*head = nandOffset + 1;
}
//Check for Gateway emuNAND
else if(sdmmc_sdcard_readsectors(nandOffset + nandSize, 1, temp) == 0)
{
if(*(u32 *)(temp + 0x100) == NCSD_MAGIC)
{
*off = nandOffset;
*head = nandOffset + nandSize;
}
//Check for Gateway emuNAND
else if(!sdmmc_sdcard_readsectors(nandOffset + nandSize, 1, temp) &&
*(u32 *)(temp + 0x100) == NCSD_MAGIC)
{
*off = nandOffset;
*head = nandOffset + nandSize;
}
/* Fallback to the first emuNAND if there's no second one,
or to SysNAND if there isn't any */
else
{
(*emuNAND)--;
if(*emuNAND) getEmunandSect(off, head, emuNAND);
}
}
/* Fallback to the first emuNAND if there's no second one,
or to SysNAND if there isn't any */
else
{
(*emuNAND)--;
if(*emuNAND) getEmunandSect(off, head, emuNAND);
}
}

View File

@@ -15,7 +15,7 @@
/ and optional writing functions as well. */
#define _FS_MINIMIZE 1
#define _FS_MINIMIZE 0
/* This option defines minimization level to remove some basic API functions.
/
/ 0: All basic functions are enabled.

View File

@@ -10,9 +10,9 @@
#include "memory.h"
#include "emunand.h"
#include "crypto.h"
#include "exceptions.h"
#include "draw.h"
#include "screeninit.h"
#include "exceptions.h"
#include "buttons.h"
#include "../build/patches.h"
@@ -29,26 +29,22 @@ void main(void)
u32 bootType,
firmType,
nandType,
a9lhInstalled,
a9lhMode,
updatedSys,
needConfig,
newConfig,
emuHeader;
//Detect the console being used
console = (PDN_MPCORE_CFG == 1) ? 0 : 1;
console = PDN_MPCORE_CFG == 7;
//Mount filesystems. CTRNAND will be mounted only if/when needed
mountFs();
//Attempt to read the configuration file
const char configPath[] = "/luma/config.bin";
if(!fileRead(&config, configPath, 4)) needConfig = 1;
else
{
config = 0;
needConfig = 2;
}
//Attempt to read the configuration file
needConfig = !fileRead(&config, configPath, 4) ? 1 : 2;
//Determine if this is a firmlaunch boot
if(*(vu8 *)0x23F00005)
@@ -58,104 +54,114 @@ void main(void)
bootType = 1;
//'0' = NATIVE_FIRM, '1' = TWL_FIRM, '2' = AGB_FIRM
firmType = *(vu8 *)0x23F00005 - '0';
firmType = *(vu8 *)0x23F00009 == '3' ? 3 : *(vu8 *)0x23F00005 - '0';
nandType = BOOTCONFIG(0, 3);
firmSource = BOOTCONFIG(2, 1);
a9lhInstalled = BOOTCONFIG(3, 1);
updatedSys = (a9lhInstalled && CONFIG(1)) ? 1 : 0;
a9lhMode = BOOTCONFIG(3, 1);
updatedSys = (a9lhMode && CONFIG(1)) ? 1 : 0;
}
else
{
//Only when "Enable developer features" is set
if(CONFIG(5))
{
detectAndProcessExceptionDumps();
installArm9Handlers();
}
//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))
configureCFW(configPath);
u32 devMode = CONFIG(5);
if(devMode) detectAndProcessExceptionDumps();
bootType = 0;
firmType = 0;
//Determine if booting with A9LH
u32 a9lhBoot = !PDN_SPI_CNT ? 1 : 0;
//Retrieve the last booted FIRM
u32 previousFirm = CFG_BOOTENV;
//Get pressed buttons
u32 pressed = HID_PAD;
u32 a9lhBoot = !PDN_SPI_CNT;
//Determine if A9LH is installed and the user has an updated sysNAND
if(a9lhBoot || CONFIG(2))
{
if(pressed == SAFE_MODE)
error("Using Safe Mode would brick you, or remove A9LH!");
a9lhInstalled = 1;
//Check setting for > 9.2 sysNAND
a9lhMode = 1;
updatedSys = CONFIG(1);
}
else
{
a9lhInstalled = 0;
a9lhMode = 0;
updatedSys = 0;
}
newConfig = a9lhInstalled << 3;
newConfig = a9lhMode << 3;
/* If booting with A9LH, it's a MCU reboot and a previous configuration exists,
try to force boot options */
if(a9lhBoot && previousFirm && needConfig == 1)
if(a9lhBoot)
{
//Always force a sysNAND boot when quitting AGB_FIRM
if(previousFirm == 7)
{
nandType = 0;
firmSource = updatedSys ? 0 : BOOTCONFIG(2, 1);
needConfig = 0;
//Retrieve the last booted FIRM
u32 previousFirm = CFG_BOOTENV;
//Flag to prevent multiple boot options-forcing
newConfig |= 1 << 4;
}
/* Else, force the last used boot options unless a payload button or A/L/R are pressed
or the no-forcing flag is set */
else if(!(pressed & OVERRIDE_BUTTONS) && !BOOTCONFIG(4, 1))
//If it's a MCU reboot, try to force boot options
if(previousFirm)
{
nandType = BOOTCONFIG(0, 3);
firmSource = BOOTCONFIG(2, 1);
needConfig = 0;
//Always force a sysNAND boot when quitting AGB_FIRM
if(previousFirm == 7)
{
nandType = 0;
firmSource = updatedSys ? 0 : BOOTCONFIG(2, 1);
needConfig--;
//Flag to prevent multiple boot options-forcing
newConfig |= 1 << 4;
}
/* Else, force the last used boot options unless a payload button or A/L/R are pressed
or the no-forcing flag is set */
else if(!(pressed & OVERRIDE_BUTTONS) && !BOOTCONFIG(4, 1))
{
nandType = BOOTCONFIG(0, 3);
firmSource = BOOTCONFIG(2, 1);
needConfig--;
}
}
//If the SAFE MODE combo is held, force a sysNAND boot
else if(pressed == SAFE_MODE)
{
a9lhMode++;
nandType = 0;
firmSource = 0;
needConfig--;
}
}
//Boot options aren't being forced
if(needConfig)
{
/* If L and R/Select or one of the single payload buttons are pressed and, if not using A9LH,
the Safe Mode combo is not pressed, chainload an external payload */
if(((pressed & SINGLE_PAYLOAD_BUTTONS) || ((pressed & BUTTON_L1) && (pressed & L_PAYLOAD_BUTTONS)))
&& pressed != SAFE_MODE)
loadPayload(pressed);
//If no configuration file exists or SELECT is held, load configuration menu
if(needConfig == 2 || (pressed & BUTTON_SELECT))
configureCFW(configPath);
/* If L and R/A/Select or one of the single payload buttons are pressed,
chainload an external payload */
if(devMode || (pressed & SINGLE_PAYLOAD_BUTTONS) || ((pressed & BUTTON_L1) && (pressed & L_PAYLOAD_BUTTONS)))
loadPayload(pressed, devMode);
//If screens are inited or the corresponding option is set, load splash screen
if(PDN_GPU_CNT != 1 || CONFIG(8)) loadSplash();
u32 autoBootSys = CONFIG(0);
//If R is pressed, boot the non-updated NAND with the FIRM of the opposite one
if(pressed & BUTTON_R1)
{
nandType = updatedSys;
firmSource = !nandType;
}
//Determine if we need to boot an emuNAND or sysNAND
nandType = (pressed & BUTTON_L1) ? autoBootSys : ((pressed & BUTTON_R1) ? updatedSys : !autoBootSys);
/* Else, boot the NAND the user set to autoboot or the opposite one, depending on L,
with their own FIRM */
else
{
nandType = CONFIG(0) != !(pressed & BUTTON_L1);
firmSource = nandType;
}
/* If we're booting emuNAND the second emuNAND is set as default and B isn't pressed,
or vice-versa, boot the second emuNAND */
if(nandType && ((!(pressed & BUTTON_B)) == CONFIG(3))) nandType++;
//Determine the NAND we should take the FIRM from
firmSource = (pressed & BUTTON_R1) ? !nandType : (nandType != 0);
if(nandType && (CONFIG(3) == !(pressed & BUTTON_B))) nandType++;
}
}
@@ -187,8 +193,18 @@ void main(void)
loadFirm(firmType, !firmType && updatedSys == !firmSource);
if(!firmType) patchNativeFirm(nandType, emuHeader, a9lhInstalled);
else patchTwlAgbFirm(firmType);
switch(firmType)
{
case 0:
patchNativeFirm(nandType, emuHeader, a9lhMode);
break;
case 3:
patchSafeFirm();
break;
default:
patchLegacyFirm(firmType);
break;
}
launchFirm(bootType);
}
@@ -205,16 +221,17 @@ static inline void loadFirm(u32 firmType, u32 externalFirm)
doesn't match the console, load FIRM from CTRNAND */
if(!externalFirmLoaded)
{
const char *firmFolders[3][2] = {{ "00000002", "20000002" },
const char *firmFolders[4][2] = {{ "00000002", "20000002" },
{ "00000102", "20000102" },
{ "00000202", "20000202" }};
{ "00000202", "20000202" },
{ "00000003", "20000003" }};
firmRead(firm, firmFolders[firmType][console]);
decryptExeFs((u8 *)firm);
}
}
static inline void patchNativeFirm(u32 nandType, u32 emuHeader, u32 a9lhInstalled)
static inline void patchNativeFirm(u32 nandType, u32 emuHeader, u32 a9lhMode)
{
u8 *arm9Section = (u8 *)firm + section[2].offset;
@@ -236,7 +253,7 @@ static inline void patchNativeFirm(u32 nandType, u32 emuHeader, u32 a9lhInstalle
nativeFirmType = (memcmp(section[2].hash, firm90Hash, 0x10) == 0) ? 0 : 1;
}
if(nativeFirmType || nandType)
if(nativeFirmType || nandType || a9lhMode == 2)
{
//Find the Process9 NCCH location
u8 *proc9Offset = getProc9(arm9Section, section[2].size);
@@ -245,16 +262,11 @@ static inline void patchNativeFirm(u32 nandType, u32 emuHeader, u32 a9lhInstalle
if(nandType) patchEmuNAND(arm9Section, proc9Offset, emuHeader);
//Apply FIRM reboot patches, not on 9.0 FIRM as it breaks firmlaunchhax
if(nativeFirmType) patchReboots(arm9Section, proc9Offset);
if(nativeFirmType || a9lhMode == 2) patchReboots(arm9Section, proc9Offset);
}
//Apply FIRM0/1 writes patches on sysNAND to protect A9LH
if(a9lhInstalled && !nandType)
{
u16 *writeOffset = getFirmWrite(arm9Section, section[2].size);
*writeOffset = writeBlock[0];
*(writeOffset + 1) = writeBlock[1];
}
if(a9lhMode && !nandType) patchFirmWrites(arm9Section, 1);
//Apply signature checks patches
u32 sigOffset,
@@ -348,7 +360,7 @@ static inline void injectLoader(void)
}
}
static inline void patchTwlAgbFirm(u32 firmType)
static inline void patchLegacyFirm(u32 firmType)
{
//On N3DS, decrypt ARM9Bin and patch ARM9 entrypoint to skip arm9loader
if(console)
@@ -396,6 +408,37 @@ static inline void patchTwlAgbFirm(u32 firmType)
}
}
static inline void patchSafeFirm(void)
{
u8 *arm9Section = (u8 *)firm + section[2].offset;
if(console)
{
//Decrypt ARM9Bin and patch ARM9 entrypoint to skip arm9loader
arm9Loader(arm9Section, 0);
firm->arm9Entry = (u8 *)0x801B01C;
}
//Apply FIRM0/1 writes patches to protect A9LH
patchFirmWrites(arm9Section, console);
}
static void patchFirmWrites(u8 *arm9Section, u32 mode)
{
if(mode)
{
u16 *writeOffset = getFirmWrite(arm9Section, section[2].size);
*writeOffset = writeBlock[0];
*(writeOffset + 1) = writeBlock[1];
}
else
{
u16 *writeOffset = getFirmWriteSafe(arm9Section, section[2].size);
*writeOffset = writeBlockSafe[0];
*(writeOffset + 1) = writeBlockSafe[1];
}
}
static inline void launchFirm(u32 bootType)
{
//Copy FIRM sections to respective memory locations

View File

@@ -37,9 +37,11 @@ typedef struct patchData {
} patchData;
static inline void loadFirm(u32 firmType, u32 externalFirm);
static inline void patchNativeFirm(u32 emuNAND, u32 emuHeader, u32 a9lhSetup);
static inline void patchNativeFirm(u32 nandType, u32 emuHeader, u32 a9lhMode);
static inline void patchEmuNAND(u8 *arm9Section, u8 *proc9Offset, u32 emuHeader);
static inline void patchReboots(u8 *arm9Section, u8 *proc9Offset);
static inline void injectLoader(void);
static inline void patchTwlAgbFirm(u32 firmType);
static inline void patchLegacyFirm(u32 firmType);
static inline void patchSafeFirm(void);
static void patchFirmWrites(u8 *arm9Section, u32 mode);
static inline void launchFirm(u32 bootType);

View File

@@ -5,13 +5,11 @@
#include "fs.h"
#include "memory.h"
#include "screeninit.h"
#include "exceptions.h"
#include "fatfs/ff.h"
#include "buttons.h"
#include "../build/loader.h"
#define PAYLOAD_ADDRESS 0x24F00000
#define PATTERN(a) a "_*.bin"
static FATFS sdFs,
nandFs;
@@ -58,7 +56,7 @@ u32 fileWrite(const void *buffer, const char *path, u32 size)
return result;
}
void loadPayload(u32 pressed)
void loadPayload(u32 pressed, u32 devMode)
{
const char *pattern;
@@ -71,11 +69,12 @@ void loadPayload(u32 pressed)
else if(pressed & BUTTON_R1) pattern = PATTERN("r");
else if(pressed & BUTTON_A) pattern = PATTERN("a");
else if(pressed & BUTTON_START) pattern = PATTERN("start");
else pattern = PATTERN("select");
else if(pressed & BUTTON_SELECT) pattern = PATTERN("select");
else pattern = "nlc.bin";
DIR dir;
FILINFO info;
char path[] = "/luma/payloads";
char path[28] = "/luma/payloads";
FRESULT result = f_findfirst(&dir, &info, path, pattern);
@@ -83,14 +82,31 @@ void loadPayload(u32 pressed)
if(result == FR_OK && info.fname[0])
{
//Only when "Enable developer features" is set
if(devMode) installArm9Handlers();
initScreens();
memcpy((void *)PAYLOAD_ADDRESS, loader, loader_size);
path[sizeof(path) - 1] = '/';
memcpy((void *)(PAYLOAD_ADDRESS + 4), path, sizeof(path));
memcpy((void *)(PAYLOAD_ADDRESS + 4 + sizeof(path)), info.altname, 13);
u32 *const loaderAddress = (u32 *)0x24FFFB00;
((void (*)())PAYLOAD_ADDRESS)();
memcpy(loaderAddress, loader, loader_size);
path[14] = '/';
memcpy(&path[15], info.altname, 13);
FIL payload;
unsigned int read;
f_open(&payload, path, FA_READ);
u32 size = f_size(&payload);
f_read(&payload, (void *)0x24F00000, size, &read);
f_close(&payload);
if(pattern[0] == 'n') f_unlink(path);
loaderAddress[1] = size;
((void (*)())loaderAddress)();
}
}

View File

@@ -6,9 +6,11 @@
#include "types.h"
#define PATTERN(a) a "_*.bin"
u32 mountFs(void);
u32 fileRead(void *dest, const char *path, u32 size);
u32 fileWrite(const void *buffer, const char *path, u32 size);
void findDumpFile(const char *path, char *fileName);
void loadPayload(u32 pressed);
void loadPayload(u32 pressed, u32 devMode);
void firmRead(void *dest, const char *firmFolder);

View File

@@ -13,7 +13,8 @@ const u32 mpuPatch[3] = {0x00360003, 0x00200603, 0x001C0603};
const u16 nandRedir[2] = {0x4C00, 0x47A0},
sigPatch[2] = {0x2000, 0x4770},
writeBlock[2] = {0x2000, 0x46C0};
writeBlock[2] = {0x2000, 0x46C0},
writeBlockSafe[2] = {0x2400, 0xE01D};
const u8 unitInfoPatch = 0xE3;
@@ -65,6 +66,14 @@ u16 *getFirmWrite(u8 *pos, u32 size)
return (u16 *)memsearch(off - 0x100, pattern, 0x100, 4);
}
u16 *getFirmWriteSafe(u8 *pos, u32 size)
{
//Look for FIRM writing code
const u8 pattern[] = {0x04, 0x1E, 0x1D, 0xDB};
return (u16 *)memsearch(pos, pattern, size, 4);
}
u8 *getUnitInfoValueSet(u8 *pos, u32 size)
{
//Look for UNITINFO value being set

View File

@@ -12,8 +12,9 @@
const u32 mpuPatch[3];
const u16 nandRedir[2],
sigPatch[2],
writeBlock[2];
const u8 unitInfoPatch;
writeBlock[2],
writeBlockSafe[2];
const u8 unitInfoPatch;
/**************************************************
* Functions
@@ -23,5 +24,6 @@ void getSigChecks(u8 *pos, u32 size, u32 *off, u32 *off2);
void *getReboot(u8 *pos, u32 size);
u32 getfOpen(u8 *proc9Offset, void *rebootOffset);
u16 *getFirmWrite(u8 *pos, u32 size);
u16 *getFirmWriteSafe(u8 *pos, u32 size);
u8 *getUnitInfoValueSet(u8 *pos, u32 size);
void *getLoader(u8 *pos, u32 size, u32 *loaderSize);

View File

@@ -12,8 +12,6 @@
#include "i2c.h"
#include "../build/screeninit.h"
#define SCREENINIT_ADDRESS 0x24F02000
vu32 *arm11Entry = (u32 *)0x1FFFFFF8;
void deinitScreens(void)
@@ -49,12 +47,14 @@ void initScreens(void)
{
if(PDN_GPU_CNT == 1)
{
memcpy((void *)SCREENINIT_ADDRESS, screeninit, screeninit_size);
u32 *const screenInitAddress = (u32 *)0x24FFFC00;
memcpy(screenInitAddress, screeninit, screeninit_size);
//Write brightness level for the stub to pick up
*(vu32 *)(SCREENINIT_ADDRESS + 8) = MULTICONFIG(0);
screenInitAddress[2] = MULTICONFIG(0);
*arm11Entry = SCREENINIT_ADDRESS;
*arm11Entry = (u32)screenInitAddress;
while(*arm11Entry);
//Turn on backlight

View File

@@ -9,7 +9,7 @@
#include "types.h"
#define PDN_GPU_CNT (*(vu8 *)0x10141200)
#define PDN_GPU_CNT (*(vu8 *)0x10141200)
void deinitScreens(void);
void initScreens(void);

View File

@@ -4,7 +4,7 @@
_start:
b start
.word 0
.word 0, 0
start:
@ Change the stack pointer

View File

@@ -3,9 +3,9 @@
*/
#include "utils.h"
#include "i2c.h"
#include "screeninit.h"
#include "draw.h"
#include "i2c.h"
#include "buttons.h"
u32 waitInput(void)
@@ -35,12 +35,6 @@ u32 waitInput(void)
return key;
}
void mcuShutDown(void)
{
i2cWriteRegister(I2C_DEV_MCU, 0x20, 1);
while(1);
}
void mcuReboot(void)
{
i2cWriteRegister(I2C_DEV_MCU, 0x20, 1 << 2);
@@ -57,5 +51,6 @@ void error(const char *message)
waitInput();
mcuShutDown();
i2cWriteRegister(I2C_DEV_MCU, 0x20, 1);
while(1);
}

View File

@@ -7,6 +7,5 @@
#include "types.h"
u32 waitInput(void);
void mcuShutDown(void);
void mcuReboot(void);
void error(const char *message);