Compare commits

...

3 Commits

Author SHA1 Message Date
Aurora
4bdba9f8e9 Even moar clean-up 2016-03-06 16:31:16 +01:00
Aurora
99829b3cf7 (Hopefully last) clean-up 2016-03-06 05:04:28 +01:00
Aurora
d4a3ce2d0d Clean-up of the filesystem functions 2016-03-05 23:33:11 +01:00
16 changed files with 133 additions and 158 deletions

View File

@@ -1,16 +1,9 @@
// From http://github.com/b1l1s/ctr
#include "crypto.h"
#include <stddef.h>
#include "memory.h"
#include "fatfs/sdmmc/sdmmc.h"
//Nand key#2 (0x12C10)
u8 key2[0x10] = {
0x42, 0x3F, 0x81, 0x7A, 0x23, 0x52, 0x58, 0x31, 0x6E, 0x75, 0x8E, 0x3A, 0x39, 0x43, 0x2E, 0xD0
};
/****************************************************************
* Crypto Libs
****************************************************************/
@@ -234,16 +227,20 @@ void aes(void* dst, const void* src, u32 blockCount, void* iv, u32 mode, u32 ivM
* Nand/FIRM Crypto stuff
****************************************************************/
//Nand key#2 (0x12C10)
u8 key2[0x10] = {
0x42, 0x3F, 0x81, 0x7A, 0x23, 0x52, 0x58, 0x31, 0x6E, 0x75, 0x8E, 0x3A, 0x39, 0x43, 0x2E, 0xD0
};
//Get Nand CTR key
void getNandCTR(u8 *buf, u8 console) {
u8 *addr = console ? (u8*)0x080D8BBC : (u8*)0x080D797C;
u8 keyLen = 0x10; //CTR length
addr += 0x0F;
while (keyLen --) { *(buf++) = *(addr--); }
void getNandCTR(u8 *buf, u8 console){
u8 *addr = (console ? (u8*)0x080D8BBC : (u8*)0x080D797C) + 0x0F;
for(u8 keyLen = 0x10; keyLen; keyLen--)
*(buf++) = *(addr--);
}
//Read firm0 from NAND and write to buffer
void nandFirm0(u8 *outbuf, const u32 size, u8 console){
void nandFirm0(u8 *outbuf, u32 size, u8 console){
u8 CTR[0x10];
getNandCTR(CTR, console);
aes_advctr(CTR, 0x0B130000/0x10, AES_INPUT_BE | AES_INPUT_NORMAL);
@@ -256,7 +253,6 @@ void nandFirm0(u8 *outbuf, const u32 size, u8 console){
void decArm9Bin(void *armHdr, u8 mode){
//Firm keys
u8 keyX[0x10];
u8 keyY[0x10];
u8 CTR[0x10];
u8 slot = mode ? 0x16 : 0x15;
@@ -264,9 +260,14 @@ void decArm9Bin(void *armHdr, u8 mode){
//Setup keys needed for arm9bin decryption
memcpy(keyY, armHdr+0x10, 0x10);
memcpy(CTR, armHdr+0x20, 0x10);
u32 size = atoi(armHdr+0x30);
u32 size = 0;
//http://stackoverflow.com/questions/12791077/atoi-implementation-in-c
for(u8 *tmp = armHdr+0x30; *tmp; tmp++)
size = (size<<3)+(size<<1)+(*tmp)-'0';
if(mode){
u8 keyX[0x10];
//Set 0x11 to key2 for the arm9bin and misc keys
aes_setkey(0x11, key2, AES_KEYNORMAL, AES_INPUT_BE | AES_INPUT_NORMAL);
aes_use_keyslot(0x11);
@@ -285,9 +286,10 @@ void decArm9Bin(void *armHdr, u8 mode){
//Sets the N3DS 9.6 KeyXs
void setKeyXs(void *armHdr){
//Set keys 0x19..0x1F keyXs
void *keyData = armHdr+0x89814;
void *decKey = keyData+0x10;
//Set keys 0x19..0x1F keyXs
aes_setkey(0x11, key2, AES_KEYNORMAL, AES_INPUT_BE | AES_INPUT_NORMAL);
aes_use_keyslot(0x11);
for(u8 slot = 0x19; slot < 0x20; slot++){

View File

@@ -1,9 +1,8 @@
// From http://github.com/b1l1s/ctr
#ifndef __CRYPTO_H
#define __CRYPTO_H
#ifndef CRYPTO_INC
#define CRYPTO_INC
#include <stdint.h>
#include "types.h"
/**************************AES****************************/
@@ -50,8 +49,8 @@
#define AES_KEYY 2
//NAND/FIRM stuff
void nandFirm0(u8 *outbuf, const u32 size, u8 console);
void nandFirm0(u8 *outbuf, u32 size, u8 console);
void decArm9Bin(void *armHdr, u8 mode);
void setKeyXs(void *armHdr);
#endif /*__CRYPTO_H*/
#endif

View File

@@ -7,7 +7,6 @@
#include "draw.h"
#include "fs.h"
#include "memory.h"
#include "types.h"
static struct fb *fb = (struct fb *)0x23FFFE00;
@@ -24,7 +23,7 @@ void shutdownLCD(void){
*(vu32*)0x10202014 = 0;
//Wait for the ARM11 entrypoint to be set
while (!*arm11);
while(!*arm11);
//Jump to it
((void (*)())*arm11)();
}
@@ -37,8 +36,8 @@ void clearScreen(void){
void loadSplash(void){
//Check if it's a no-screen-init A9LH boot via PDN_GPU_CNT
if (*(u8*)0x10141200 == 0x1) return;
if(*(u8*)0x10141200 == 0x1) return;
clearScreen();
if(fileRead(fb->top_left, "/rei/splash.bin", 0x46500) != 0) return;
if(!fileRead(fb->top_left, "/rei/splash.bin", 0x46500)) return;
u64 i = 0xFFFFFF; while(--i) __asm("mov r0, r0"); //Less Ghetto sleep func
}

View File

@@ -4,6 +4,9 @@
* Copyright (c) 2015 All Rights Reserved
*/
#ifndef DRAW_INC
#define DRAW_INC
#include "types.h"
struct fb {
@@ -14,3 +17,5 @@ struct fb {
void loadSplash(void);
void shutdownLCD(void);
#endif

View File

@@ -6,15 +6,14 @@
#include "emunand.h"
#include "memory.h"
#include "fatfs/ff.h"
#include "fatfs/sdmmc/sdmmc.h"
static u8 *temp = (u8*)0x24300000;
void getEmunandSect(u32 *off, u32 *head){
u32 nandSize = getMMCDevice(0)->total_size;
if (sdmmc_sdcard_readsectors(nandSize, 1, temp) == 0) {
if (*(u32*)(temp + 0x100) == NCSD_MAGIC) {
if(sdmmc_sdcard_readsectors(nandSize, 1, temp) == 0){
if(*(u32*)(temp + 0x100) == NCSD_MAGIC){
*off = 0;
*head = nandSize;
}
@@ -27,13 +26,13 @@ void getSDMMC(void *pos, u32 *off, u32 size){
*off = (u32)memsearch(pos, pattern, size, 4) - 1;
//Get DCD values
unsigned char buf[4];
int p;
u8 buf[4],
p;
u32 addr = 0,
additive = 0;
memcpy((void*)buf, (void*)(*off+0x0A), 4);
memcpy(buf, (void *)(*off+0x0A), 4);
for (p = 0; p < 4; p++) addr |= ((u32) buf[p]) << (8 * p);
memcpy((void*)buf, (void*)(*off+0x0E), 4);
memcpy(buf, (void *)(*off+0x0E), 4);
for (p = 0; p < 4; p++) additive |= ((u32) buf[p]) << (8 * p);
//Return result

View File

@@ -4,8 +4,8 @@
* Copyright (c) 2015 All Rights Reserved
*/
#ifndef EMU_INC
#define EMU_INC
#ifndef EMUNAND_INC
#define EMUNAND_INC
#include "types.h"

View File

@@ -31,7 +31,7 @@ void setupCFW(void){
//Retrieve the last booted FIRM via CFG_BOOTENV
u8 previousFirm = *(u8*)0x10010000;
u8 overrideConfig = 0;
char lastConfigPath[] = "rei/lastbootcfg";
const char lastConfigPath[] = "rei/lastbootcfg";
//Detect the console being used
if(PDN_MPCORE_CFG == 1) console = 0;
@@ -49,7 +49,7 @@ void setupCFW(void){
//If booting with A9LH and it's a MCU reboot, try to force boot options
if(a9lhBoot && previousFirm && fileExists(lastConfigPath)){
u8 tempConfig;
fileRead((u8*)&tempConfig, lastConfigPath, 1);
fileRead(&tempConfig, lastConfigPath, 1);
//Always force a sysNAND boot when quitting AGB_FIRM
if(previousFirm == 0x7) {
@@ -77,7 +77,7 @@ void setupCFW(void){
//Write the current boot options on A9LH
if(a9lhBoot){
u8 tempConfig = (mode | (emuNAND << 1)) & 0x3;
fileWrite((u8*)&tempConfig, lastConfigPath, 1);
fileWrite(&tempConfig, lastConfigPath, 1);
}
}
@@ -101,26 +101,26 @@ u8 loadFirm(void){
firmSize = console ? 0xF2000 : 0xE9000;
nandFirm0((u8*)firmLocation, firmSize, console);
//Check for correct decryption
if(memcmp((u8*)firmLocation, "FIRM", 4) != 0) return 1;
if(memcmp((u8*)firmLocation, "FIRM", 4) != 0) return 0;
}
//Load FIRM from SD
else{
char *path = usePatchedFirm ? firmPathPatched :
const char *path = usePatchedFirm ? firmPathPatched :
(mode ? "/rei/firmware.bin" : "/rei/firmware90.bin");
firmSize = fileSize(path);
if (!firmSize) return 1;
if(!firmSize) return 0;
fileRead((u8*)firmLocation, path, firmSize);
}
section = firmLocation->section;
//Check that the loaded FIRM matches the console
if((((u32)section[2].address >> 8) & 0xFF) != (console ? 0x60 : 0x68)) return 1;
if((((u32)section[2].address >> 8) & 0xFF) != (console ? 0x60 : 0x68)) return 0;
if(console && !usePatchedFirm)
decArm9Bin((u8*)firmLocation + section[2].offset, mode);
return 0;
return 1;
}
//Nand redirection
@@ -135,20 +135,19 @@ u8 loadEmu(void){
emuCodeOffset = 0;
//Read emunand code from SD
char path[] = "/rei/emunand/emunand.bin";
const char path[] = "/rei/emunand/emunand.bin";
u32 size = fileSize(path);
if (!size) return 1;
if(!size) return 0;
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 +
emuCodeOffset = *(u32 *)(nandRedir + 4) - (u32)section[2].address +
section[2].offset + (u32)firmLocation;
fileRead((u8*)emuCodeOffset, path, size);
//Find and patch emunand related offsets
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);
u32 *pos_sdmmc = (u32 *)memsearch((u32*)emuCodeOffset, "SDMC", size, 4);
u32 *pos_offset = (u32 *)memsearch((u32*)emuCodeOffset, "NAND", size, 4);
u32 *pos_header = (u32 *)memsearch((u32*)emuCodeOffset, "NCSD", size, 4);
getSDMMC(firmLocation, &sdmmcOffset, firmSize);
getEmunandSect(&emuOffset, &emuHeader);
getEmuRW(firmLocation, firmSize, &emuRead, &emuWrite);
@@ -159,35 +158,35 @@ u8 loadEmu(void){
//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));
void *pos_instr = memsearch((u32*)emuCodeOffset, "\xA6\x01\x08\x30", size, 4);
memcpy(pos_instr, emuInstr, sizeof(emuInstr));
}
//Add emunand hooks
memcpy((u8*)emuRead, nandRedir, sizeof(nandRedir));
memcpy((u8*)emuWrite, nandRedir, sizeof(nandRedir));
memcpy((void *)emuRead, nandRedir, sizeof(nandRedir));
memcpy((void *)emuWrite, nandRedir, sizeof(nandRedir));
//Set MPU for emu code region
memcpy((u8*)mpuOffset, mpu, sizeof(mpu));
memcpy((void *)mpuOffset, mpu, sizeof(mpu));
return 0;
return 1;
}
//Patches
u8 patchFirm(void){
//Skip patching
if(usePatchedFirm) return 0;
if(usePatchedFirm) return 1;
//Apply emuNAND patches
if(emuNAND){
if (loadEmu()) return 1;
if(!loadEmu()) return 0;
}
else if (a9lhSetup){
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));
memcpy((void *)writeOffset, FIRMblock, sizeof(FIRMblock));
}
//Disable signature checks
@@ -195,8 +194,8 @@ u8 patchFirm(void){
sigOffset2 = 0;
getSignatures(firmLocation, firmSize, &sigOffset, &sigOffset2);
memcpy((u8*)sigOffset, sigPat1, sizeof(sigPat1));
memcpy((u8*)sigOffset2, sigPat2, sizeof(sigPat2));
memcpy((void *)sigOffset, sigPat1, sizeof(sigPat1));
memcpy((void *)sigOffset2, sigPat2, sizeof(sigPat2));
//Patch ARM9 entrypoint on N3DS to skip arm9loader
if(console){
@@ -210,29 +209,29 @@ u8 patchFirm(void){
fOpenOffset = 0;
//Read reboot code from SD
char path[] = "/rei/reboot/reboot.bin";
const char path[] = "/rei/reboot/reboot.bin";
u32 size = fileSize(path);
if (!size) return 1;
if(!size) return 0;
getReboot(firmLocation, firmSize, &rebootOffset);
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);
u32 *pos_fopen = (u32 *)memsearch((u32*)rebootOffset, "OPEN", size, 4);
getfOpen(firmLocation, firmSize, &fOpenOffset);
*pos_fopen = fOpenOffset;
//Patch path for emuNAND-patched FIRM
if(emuNAND){
u32 *pos_path = memsearch((u32*)rebootOffset, L"sy", size, 4);
memcpy((u8*)pos_path, L"emu", 5);
void *pos_path = memsearch((u32*)rebootOffset, L"sy", size, 4);
memcpy(pos_path, L"emu", 5);
}
}
//Write patched FIRM to SD if needed
if(firmPathPatched)
if(fileWrite((u8*)firmLocation, firmPathPatched, firmSize) != 0) return 1;
if(!fileWrite((u8*)firmLocation, firmPathPatched, firmSize)) return 0;
return 0;
return 1;
}
void launchFirm(void){

View File

@@ -3,6 +3,7 @@
* by Reisyukaku
* Copyright (c) 2015 All Rights Reserved
*/
#ifndef FIRM_INC
#define FIRM_INC

View File

@@ -2,86 +2,60 @@
* fs.c
*/
#include <stddef.h>
#include "fs.h"
#include "fatfs/ff.h"
static FATFS fs;
int mountSD()
{
if (f_mount(&fs, "0:", 1) != FR_OK) {
//printF("Failed to mount SD card!");
u8 mountSD(void){
if(f_mount(&fs, "0:", 1) != FR_OK) return 0;
return 1;
}
//printF("Mounted SD card");
return 0;
}
int fileReadOffset(u8 *dest, const char *path, u32 size, u32 offset){
u8 fileRead(u8 *dest, const char *path, u32 size){
FRESULT fr;
FIL fp;
unsigned int br = 0;
fr = f_open(&fp, path, FA_READ);
if (fr != FR_OK)goto error;
if (!size) size = f_size(&fp);
if (offset) {
fr = f_lseek(&fp, offset);
if (fr != FR_OK) goto error;
}
if(fr == FR_OK){
if(!size) size = f_size(&fp);
fr = f_read(&fp, dest, size, &br);
if (fr != FR_OK) goto error;
}
fr = f_close(&fp);
if (fr != FR_OK) goto error;
return 0;
error:
f_close(&fp);
return fr;
return fr ? 0 : 1;
}
int fileRead(u8 *dest, const char *path, u32 size){
return fileReadOffset(dest, path, size, 0);
}
int fileWrite(const u8 *buffer, const char *path, u32 size){
FRESULT fr = 1;
u8 fileWrite(const u8 *buffer, const char *path, u32 size){
FRESULT fr;
FIL fp;
unsigned int br = 0;
f_unlink(path);
if(f_open(&fp, path, FA_WRITE | FA_OPEN_ALWAYS) == FR_OK){
fr = f_write(&fp, buffer, size, &br);
fr = f_open(&fp, path, FA_WRITE | FA_OPEN_ALWAYS);
if(fr == FR_OK) fr = f_write(&fp, buffer, size, &br);
f_close(&fp);
if (fr == FR_OK && br == size) return 0;
}
return fr;
return fr ? 0 : 1;
}
int fileSize(const char* path){
FRESULT fr;
u32 fileSize(const char* path){
FIL fp;
int size = 0;
fr = f_open(&fp, path, FA_READ);
if (fr != FR_OK)goto error;
u32 size = 0;
if(f_open(&fp, path, FA_READ) == FR_OK)
size = f_size(&fp);
error:
f_close(&fp);
return size;
}
int fileExists(const char* path){
FRESULT fr;
u8 fileExists(const char* path){
FIL fp;
int exists = 1;
fr = f_open(&fp, path, FA_READ);
if (fr != FR_OK)exists = 0;
u8 exists = 0;
if(f_open(&fp, path, FA_READ) == FR_OK) exists = 1;
f_close(&fp);
return exists;
}

View File

@@ -2,16 +2,15 @@
* fs.h
*/
#ifndef __fs_h__
#define __fs_h__
#ifndef FS_INC
#define FS_INC
#include "types.h"
int mountSD();
int fileReadOffset(u8 *dest, const char *path, u32 size, u32 offset);
int fileRead(u8 *dest, const char *path, u32 size);
int fileWrite(const u8 *buffer, const char *path, u32 size);
int fileSize(const char* path);
int fileExists(const char* path);
u8 mountSD(void);
u8 fileRead(u8 *dest, const char *path, u32 size);
u8 fileWrite(const u8 *buffer, const char *path, u32 size);
u32 fileSize(const char* path);
u8 fileExists(const char* path);
#endif

View File

@@ -14,8 +14,8 @@ u8 main(){
mountSD();
loadSplash();
setupCFW();
if (loadFirm()) return 1;
if (patchFirm()) return 1;
if(!loadFirm()) return 0;
if(!patchFirm()) return 0;
launchFirm();
return 0;
return 1;
}

View File

@@ -3,36 +3,35 @@
* by Reisyukaku
* Copyright (c) 2015 All Rights Reserved
*/
#include "memory.h"
void memcpy(void *dest, const void *src, u32 size){
char *destc = (char *)dest;
const char *srcc = (const char *)src;
u32 i; for (i = 0; i < size; i++) {
u8 *destc = (u8 *)dest;
const u8 *srcc = (const u8 *)src;
for(u32 i = 0; i < size; i++)
destc[i] = srcc[i];
}
}
void memset(void *dest, int filler, u32 size){
char *destc = (char *)dest;
u32 i; for (i = 0; i < size; i++) {
destc[i] = filler;
}
u8 *destc = (u8 *)dest;
for(u32 i = 0; i < size; i++)
destc[i] = (u8)filler;
}
int memcmp(const void *buf1, const void *buf2, u32 size){
const char *buf1c = (const char *)buf1;
const char *buf2c = (const char *)buf2;
u32 i; for (i = 0; i < size; i++) {
const u8 *buf1c = (const u8 *)buf1;
const u8 *buf2c = (const u8 *)buf2;
for(u32 i = 0; i < size; i++){
int cmp = buf1c[i] - buf2c[i];
if (cmp) return cmp;
if(cmp) return cmp;
}
return 0;
}
void *memsearch(void *start_pos, void *search, u32 size, u32 size_search){
for (void *pos = start_pos + size - size_search; pos >= start_pos; pos--) {
if (memcmp(pos, search, size_search) == 0) return pos;
for(void *pos = start_pos + size - size_search; pos >= start_pos; pos--){
if(memcmp(pos, search, size_search) == 0) return pos;
}
return NULL;
}

View File

@@ -3,8 +3,9 @@
* by Reisyukaku
* Copyright (c) 2015 All Rights Reserved
*/
#ifndef MEM_INC
#define MEM_INC
#ifndef MEMORY_INC
#define MEMORY_INC
#include "types.h"

View File

@@ -11,9 +11,6 @@
* Patches
**************************************************/
/*
* MPU
*/
u8 mpu[0x2C] = { //MPU shit
0x03, 0x00, 0x36, 0x00, 0x00, 0x00, 0x10, 0x10, 0x01, 0x00, 0x00, 0x01, 0x03, 0x00, 0x36, 0x00,
0x00, 0x00, 0x00, 0x20, 0x01, 0x01, 0x01, 0x01, 0x03, 0x06, 0x20, 0x00, 0x00, 0x00, 0x00, 0x08,
@@ -22,12 +19,11 @@ u8 mpu[0x2C] = { //MPU shit
u8 nandRedir[0x08] = {0x00, 0x4C, 0xA0, 0x47, 0xC0, 0xA5, 0x01, 0x08}; //Branch to emunand function
/*
* Sig checks
*/
u8 sigPat1[2] = {0x00, 0x20};
u8 sigPat2[4] = {0x00, 0x20, 0x70, 0x47};
u8 FIRMblock[4] = {0x00, 0x20, 0xC0, 0x46};
u8 emuInstr[5] = {0xA5, 0x01, 0x08, 0x30, 0xA5};
/**************************************************

View File

@@ -3,6 +3,7 @@
* by Reisyukaku
* Copyright (c) 2015 All Rights Reserved
*/
#ifndef PATCHES_INC
#define PATCHES_INC

View File

@@ -3,6 +3,7 @@
* by Reisyukaku
* Copyright (c) 2015 All Rights Reserved
*/
#ifndef TYPES_INC
#define TYPES_INC