From 99829b3cf7148c969b84868c8486b88a94b6619a Mon Sep 17 00:00:00 2001 From: Aurora Date: Sun, 6 Mar 2016 03:41:07 +0100 Subject: [PATCH] (Hopefully last) clean-up --- source/crypto.c | 14 ++++++++------ source/crypto.h | 7 +++---- source/draw.c | 1 - source/draw.h | 7 ++++++- source/emunand.c | 1 - source/emunand.h | 4 ++-- source/firm.c | 2 +- source/firm.h | 1 + source/fs.c | 17 +++++++---------- source/fs.h | 16 ++++++++-------- source/memory.c | 11 ++++++----- source/memory.h | 5 +++-- source/patches.h | 1 + source/types.h | 1 + 14 files changed, 47 insertions(+), 41 deletions(-) diff --git a/source/crypto.c b/source/crypto.c index ab1418e..8ac28da 100755 --- a/source/crypto.c +++ b/source/crypto.c @@ -1,8 +1,6 @@ // From http://github.com/b1l1s/ctr #include "crypto.h" - -#include #include "memory.h" #include "fatfs/sdmmc/sdmmc.h" @@ -256,7 +254,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 +261,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); @@ -286,11 +288,11 @@ void decArm9Bin(void *armHdr, u8 mode){ void setKeyXs(void *armHdr){ //Set keys 0x19..0x1F keyXs - void *keyData = armHdr+0x89814; - void *decKey = keyData+0x10; aes_setkey(0x11, key2, AES_KEYNORMAL, AES_INPUT_BE | AES_INPUT_NORMAL); aes_use_keyslot(0x11); for(u8 slot = 0x19; slot < 0x20; slot++){ + void *keyData = armHdr+0x89814; + void *decKey = keyData+0x10; aes(decKey, keyData, 1, NULL, AES_ECB_DECRYPT_MODE, 0); aes_setkey(slot, decKey, AES_KEYX, AES_INPUT_BE | AES_INPUT_NORMAL); *(u8*)(keyData+0xF) += 1; diff --git a/source/crypto.h b/source/crypto.h index cf9912d..34c56d1 100755 --- a/source/crypto.h +++ b/source/crypto.h @@ -1,9 +1,8 @@ // From http://github.com/b1l1s/ctr -#ifndef __CRYPTO_H -#define __CRYPTO_H +#ifndef CRYPTO_INC +#define CRYPTO_INC -#include #include "types.h" /**************************AES****************************/ @@ -54,4 +53,4 @@ void nandFirm0(u8 *outbuf, const u32 size, u8 console); void decArm9Bin(void *armHdr, u8 mode); void setKeyXs(void *armHdr); -#endif /*__CRYPTO_H*/ +#endif \ No newline at end of file diff --git a/source/draw.c b/source/draw.c index d37dcca..0b5926a 100644 --- a/source/draw.c +++ b/source/draw.c @@ -7,7 +7,6 @@ #include "draw.h" #include "fs.h" #include "memory.h" -#include "types.h" static struct fb *fb = (struct fb *)0x23FFFE00; diff --git a/source/draw.h b/source/draw.h index f0e0561..4431274 100644 --- a/source/draw.h +++ b/source/draw.h @@ -4,6 +4,9 @@ * Copyright (c) 2015 All Rights Reserved */ +#ifndef DRAW_INC +#define DRAW_INC + #include "types.h" struct fb { @@ -13,4 +16,6 @@ struct fb { }; void loadSplash(void); -void shutdownLCD(void); \ No newline at end of file +void shutdownLCD(void); + +#endif \ No newline at end of file diff --git a/source/emunand.c b/source/emunand.c index aa8e193..3f103ea 100644 --- a/source/emunand.c +++ b/source/emunand.c @@ -6,7 +6,6 @@ #include "emunand.h" #include "memory.h" -#include "fatfs/ff.h" #include "fatfs/sdmmc/sdmmc.h" static u8 *temp = (u8*)0x24300000; diff --git a/source/emunand.h b/source/emunand.h index 702c7c7..3b93ddc 100644 --- a/source/emunand.h +++ b/source/emunand.h @@ -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" diff --git a/source/firm.c b/source/firm.c index abcb8c6..dc3a22d 100755 --- a/source/firm.c +++ b/source/firm.c @@ -181,7 +181,7 @@ u8 patchFirm(void){ //Apply emuNAND patches if(emuNAND){ - if(loadEmu()) return 0; + if(!loadEmu()) return 0; } else if(a9lhSetup){ //Patch FIRM partitions writes on SysNAND to protect A9LH diff --git a/source/firm.h b/source/firm.h index f1f3f77..ec48426 100644 --- a/source/firm.h +++ b/source/firm.h @@ -3,6 +3,7 @@ * by Reisyukaku * Copyright (c) 2015 All Rights Reserved */ + #ifndef FIRM_INC #define FIRM_INC diff --git a/source/fs.c b/source/fs.c index d7cbd3c..5d98230 100644 --- a/source/fs.c +++ b/source/fs.c @@ -2,18 +2,17 @@ * fs.c */ -#include #include "fs.h" #include "fatfs/ff.h" static FATFS fs; -int mountSD(void){ +u8 mountSD(void){ if(f_mount(&fs, "0:", 1) != FR_OK) return 0; return 1; } -int fileRead(u8 *dest, const char *path, u32 size){ +u8 fileRead(u8 *dest, const char *path, u32 size){ FRESULT fr; FIL fp; unsigned int br = 0; @@ -28,13 +27,11 @@ int fileRead(u8 *dest, const char *path, u32 size){ return fr ? 0 : 1; } -int fileWrite(const u8 *buffer, const char *path, u32 size){ +u8 fileWrite(const u8 *buffer, const char *path, u32 size){ FRESULT fr; FIL fp; unsigned int br = 0; - f_unlink(path); - fr = f_open(&fp, path, FA_WRITE | FA_OPEN_ALWAYS); if(fr == FR_OK) fr = f_write(&fp, buffer, size, &br); @@ -42,9 +39,9 @@ int fileWrite(const u8 *buffer, const char *path, u32 size){ return fr ? 0 : 1; } -int fileSize(const char* path){ +u32 fileSize(const char* path){ FIL fp; - int size = 0; + u32 size = 0; if(f_open(&fp, path, FA_READ) == FR_OK) size = f_size(&fp); @@ -53,9 +50,9 @@ int fileSize(const char* path){ return size; } -int fileExists(const char* path){ +u8 fileExists(const char* path){ FIL fp; - int exists = 0; + u8 exists = 0; if(f_open(&fp, path, FA_READ) == FR_OK) exists = 1; diff --git a/source/fs.h b/source/fs.h index 16beed7..4b422ec 100644 --- a/source/fs.h +++ b/source/fs.h @@ -2,15 +2,15 @@ * fs.h */ -#ifndef __fs_h__ -#define __fs_h__ +#ifndef FS_INC +#define FS_INC #include "types.h" -int mountSD(void); -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 +#endif \ No newline at end of file diff --git a/source/memory.c b/source/memory.c index 0ef3e7e..2e6bf8e 100644 --- a/source/memory.c +++ b/source/memory.c @@ -3,27 +3,28 @@ * 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++) { + char *destc = (char *)dest; + const char *srcc = (const char *)src; destc[i] = srcc[i]; } } void memset(void *dest, int filler, u32 size){ - char *destc = (char *)dest; u32 i; for (i = 0; i < size; i++) { + char *destc = (char *)dest; destc[i] = 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 char *buf1c = (const char *)buf1; + const char *buf2c = (const char *)buf2; int cmp = buf1c[i] - buf2c[i]; if (cmp) return cmp; } diff --git a/source/memory.h b/source/memory.h index f577f0c..c5436dc 100644 --- a/source/memory.h +++ b/source/memory.h @@ -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" diff --git a/source/patches.h b/source/patches.h index e00a34e..e5f574b 100644 --- a/source/patches.h +++ b/source/patches.h @@ -3,6 +3,7 @@ * by Reisyukaku * Copyright (c) 2015 All Rights Reserved */ + #ifndef PATCHES_INC #define PATCHES_INC diff --git a/source/types.h b/source/types.h index 676a93f..5f4f982 100644 --- a/source/types.h +++ b/source/types.h @@ -3,6 +3,7 @@ * by Reisyukaku * Copyright (c) 2015 All Rights Reserved */ + #ifndef TYPES_INC #define TYPES_INC