From c6d3158b56a950fbddb20a88116457bcf6245c0e Mon Sep 17 00:00:00 2001 From: Aurora Date: Fri, 29 Apr 2016 14:28:37 +0200 Subject: [PATCH 1/4] Remade the chainloader to only try to load the right payload for the pressed button, got rid of the default payload (start now boots "start_NAME.bin"), sel_NAME is now select_NAME as there is no more SFN limitations anymore --- loader/source/buttons.h | 14 -------- loader/source/fatfs/ffconf.h | 4 +-- loader/source/fatfs/sdmmc/common.h | 13 +++++++- loader/source/main.c | 53 ++++-------------------------- loader/source/types.h | 14 -------- source/firm.c | 2 +- source/fs.c | 32 ++++++++++++++++-- source/fs.h | 2 +- source/loader.c | 4 +-- source/loader.h | 2 +- 10 files changed, 55 insertions(+), 85 deletions(-) delete mode 100644 loader/source/buttons.h delete mode 100644 loader/source/types.h diff --git a/loader/source/buttons.h b/loader/source/buttons.h deleted file mode 100644 index 336eaf54..00000000 --- a/loader/source/buttons.h +++ /dev/null @@ -1,14 +0,0 @@ -#pragma once - -#include "types.h" - -#define HID_PAD (*(vu32 *)0x10146000 ^ 0xFFF) -#define BUTTON_RIGHT (1 << 4) -#define BUTTON_LEFT (1 << 5) -#define BUTTON_UP (1 << 6) -#define BUTTON_DOWN (1 << 7) -#define BUTTON_A 1 -#define BUTTON_X (1 << 10) -#define BUTTON_Y (1 << 11) -#define BUTTON_R1 (1 << 8) -#define BUTTON_SELECT (1 << 2) \ No newline at end of file diff --git a/loader/source/fatfs/ffconf.h b/loader/source/fatfs/ffconf.h index fd246640..64183db3 100644 --- a/loader/source/fatfs/ffconf.h +++ b/loader/source/fatfs/ffconf.h @@ -15,7 +15,7 @@ / and optional writing functions as well. */ -#define _FS_MINIMIZE 1 +#define _FS_MINIMIZE 3 /* This option defines minimization level to remove some basic API functions. / / 0: All basic functions are enabled. @@ -34,7 +34,7 @@ / 2: Enable with LF-CRLF conversion. */ -#define _USE_FIND 1 +#define _USE_FIND 0 /* This option switches filtered directory read functions, f_findfirst() and / f_findnext(). (0:Disable, 1:Enable 2:Enable with matching altname[] too) */ diff --git a/loader/source/fatfs/sdmmc/common.h b/loader/source/fatfs/sdmmc/common.h index 90a327e1..f2427e6d 100644 --- a/loader/source/fatfs/sdmmc/common.h +++ b/loader/source/fatfs/sdmmc/common.h @@ -1,4 +1,15 @@ #pragma once #include -#include "../../types.h" \ No newline at end of file +#include +#include + +//Common data types +typedef uint8_t u8; +typedef uint16_t u16; +typedef uint32_t u32; +typedef uint64_t u64; +typedef volatile u8 vu8; +typedef volatile u16 vu16; +typedef volatile u32 vu32; +typedef volatile u64 vu64; \ No newline at end of file diff --git a/loader/source/main.c b/loader/source/main.c index 21f16ec8..926835cf 100644 --- a/loader/source/main.c +++ b/loader/source/main.c @@ -1,39 +1,6 @@ -#include "types.h" -#include "buttons.h" #include "fatfs/ff.h" #define PAYLOAD_ADDRESS 0x23F00000 -#define LOAD_PAYLOAD(a) loadPayload(a "_*.bin") - -static u32 loadPayload(const char *pattern) -{ - char path[29] = "/luma/payloads"; - - DIR dir; - FILINFO info; - - FRESULT result = f_findfirst(&dir, &info, path, pattern); - - f_closedir(&dir); - - if(result != FR_OK || !info.fname[0]) - return 0; - - path[14] = '/'; - u32 i; - for(i = 0; info.fname[i]; i++) - path[15 + i] = info.fname[i]; - path[15 + i] = '\0'; - - FIL payload; - unsigned int read; - - f_open(&payload, path, FA_READ); - f_read(&payload, (void *)PAYLOAD_ADDRESS, f_size(&payload), &read); - f_close(&payload); - - return 1; -} void main(void) { @@ -41,18 +8,12 @@ void main(void) f_mount(&fs, "0:", 1); - //Get pressed buttons - u32 pressed = HID_PAD; + FIL payload; + unsigned int read; - if(((pressed & BUTTON_RIGHT) && LOAD_PAYLOAD("right")) || - ((pressed & BUTTON_LEFT) && LOAD_PAYLOAD("left")) || - ((pressed & BUTTON_UP) && LOAD_PAYLOAD("up")) || - ((pressed & BUTTON_DOWN) && LOAD_PAYLOAD("down")) || - ((pressed & BUTTON_X) && LOAD_PAYLOAD("x")) || - ((pressed & BUTTON_Y) && LOAD_PAYLOAD("y")) || - ((pressed & BUTTON_R1) && LOAD_PAYLOAD("r")) || - ((pressed & BUTTON_A) && LOAD_PAYLOAD("a")) || - ((pressed & BUTTON_SELECT) && LOAD_PAYLOAD("sel")) || - LOAD_PAYLOAD("def")) - ((void (*)())PAYLOAD_ADDRESS)(); + f_open(&payload, (char *)0x24F02000, FA_READ); + f_read(&payload, (void *)PAYLOAD_ADDRESS, f_size(&payload), &read); + f_close(&payload); + + ((void (*)())PAYLOAD_ADDRESS)(); } \ No newline at end of file diff --git a/loader/source/types.h b/loader/source/types.h deleted file mode 100644 index 8ac964b2..00000000 --- a/loader/source/types.h +++ /dev/null @@ -1,14 +0,0 @@ -#pragma once - -#include -#include - -//Common data types -typedef uint8_t u8; -typedef uint16_t u16; -typedef uint32_t u32; -typedef uint64_t u64; -typedef volatile u8 vu8; -typedef volatile u16 vu16; -typedef volatile u32 vu32; -typedef volatile u64 vu64; \ No newline at end of file diff --git a/source/firm.c b/source/firm.c index 598e8181..45a669bc 100755 --- a/source/firm.c +++ b/source/firm.c @@ -129,7 +129,7 @@ void main(void) 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(); + loadPayload(pressed); //If no configuration file exists or SELECT is held, load configuration menu if(needConfig == 2 || (pressed & BUTTON_SELECT)) diff --git a/source/fs.c b/source/fs.c index 84be1103..77248d03 100644 --- a/source/fs.c +++ b/source/fs.c @@ -4,8 +4,11 @@ #include "fs.h" #include "memory.h" +#include "buttons.h" #include "fatfs/ff.h" +#define PATTERN(a) a "_*.bin" + static FATFS sdFs, nandFs; @@ -52,16 +55,39 @@ u32 fileWrite(const void *buffer, const char *path, u32 size) return result; } -u32 defPayloadExists(void) +u32 payloadExists(u32 pressed) { + const char *pattern; + + if(pressed & BUTTON_RIGHT) pattern = PATTERN("right"); + else if(pressed & BUTTON_LEFT) pattern = PATTERN("left"); + else if(pressed & BUTTON_UP) pattern = PATTERN("up"); + else if(pressed & BUTTON_DOWN) pattern = PATTERN("down"); + else if(pressed & BUTTON_X) pattern = PATTERN("x"); + else if(pressed & BUTTON_Y) pattern = PATTERN("y"); + 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"); + DIR dir; FILINFO info; + char path[] = "/luma/payloads"; - FRESULT result = f_findfirst(&dir, &info, "/luma/payloads", "def_*.bin"); + FRESULT result = f_findfirst(&dir, &info, path, pattern); f_closedir(&dir); - return (result == FR_OK && info.fname[0]); + if(result == FR_OK && info.fname[0]) + { + path[sizeof(path) - 1] = '/'; + memcpy((void *)0x24F02000, path, sizeof(path)); + memcpy((void *)(0x24F02000 + sizeof(path)), info.altname, 13); + + return 1; + } + + return 0; } void firmRead(void *dest, const char *firmFolder) diff --git a/source/fs.h b/source/fs.h index 46202044..a856c420 100644 --- a/source/fs.h +++ b/source/fs.h @@ -9,5 +9,5 @@ u32 mountFs(void); u32 fileRead(void *dest, const char *path, u32 size); u32 fileWrite(const void *buffer, const char *path, u32 size); -u32 defPayloadExists(void); +u32 payloadExists(u32 pressed); void firmRead(void *dest, const char *firmFolder); \ No newline at end of file diff --git a/source/loader.c b/source/loader.c index d2260314..c48184a6 100644 --- a/source/loader.c +++ b/source/loader.c @@ -10,9 +10,9 @@ #define PAYLOAD_ADDRESS 0x24F00000 -void loadPayload(void) +void loadPayload(u32 pressed) { - if(defPayloadExists()) + if(payloadExists(pressed)) { initScreens(); memcpy((void *)PAYLOAD_ADDRESS, loader, loader_size); diff --git a/source/loader.h b/source/loader.h index 300de99e..3fba42eb 100644 --- a/source/loader.h +++ b/source/loader.h @@ -6,4 +6,4 @@ #include "types.h" -void loadPayload(void); \ No newline at end of file +void loadPayload(u32 pressed); \ No newline at end of file From 85615d1916102743fff991ba01a51a61fdc2b542 Mon Sep 17 00:00:00 2001 From: Aurora Date: Fri, 29 Apr 2016 15:19:57 +0200 Subject: [PATCH 2/4] Fix external FIRM loading --- source/firm.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/source/firm.c b/source/firm.c index 45a669bc..10efe256 100755 --- a/source/firm.c +++ b/source/firm.c @@ -190,8 +190,9 @@ static inline void loadFirm(u32 firmType, u32 externalFirm) { section = firm->section; - u32 externalFirmLoaded = externalFirm && !fileRead(firm, "/luma/firmware.bin", 0) && - (((u32)section[2].address >> 8) & 0xFF) != (console ? 0x60 : 0x68); + u32 externalFirmLoaded = externalFirm && + !fileRead(firm, "/luma/firmware.bin", 0) && + (((u32)section[2].address >> 8) & 0xFF) == (console ? 0x60 : 0x68); /* 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 */ From df112b550b9fca83c3f7a9c71d11b32ff53f6200 Mon Sep 17 00:00:00 2001 From: Aurora Date: Fri, 29 Apr 2016 17:21:34 +0200 Subject: [PATCH 3/4] Move loadPayload to fs.c, and the path to the beginning of the chainloader --- loader/source/main.c | 2 +- loader/source/start.s | 5 +++++ source/firm.c | 1 - source/fs.c | 18 +++++++++++------- source/fs.h | 2 +- source/loader.c | 21 --------------------- source/loader.h | 9 --------- 7 files changed, 18 insertions(+), 40 deletions(-) delete mode 100644 source/loader.c delete mode 100644 source/loader.h diff --git a/loader/source/main.c b/loader/source/main.c index 926835cf..875108cf 100644 --- a/loader/source/main.c +++ b/loader/source/main.c @@ -11,7 +11,7 @@ void main(void) FIL payload; unsigned int read; - f_open(&payload, (char *)0x24F02000, FA_READ); + f_open(&payload, (char *)0x24F00004, FA_READ); f_read(&payload, (void *)PAYLOAD_ADDRESS, f_size(&payload), &read); f_close(&payload); diff --git a/loader/source/start.s b/loader/source/start.s index 71a18af4..09841e1a 100644 --- a/loader/source/start.s +++ b/loader/source/start.s @@ -2,6 +2,11 @@ .align 4 .global _start _start: + b start + + .word 0, 0, 0, 0, 0, 0, 0 + +start: @ Flush caches mov r0, #0 mcr p15, 0, r0, c7, c5, 0 @ flush I-cache diff --git a/source/firm.c b/source/firm.c index 10efe256..3c58dacb 100755 --- a/source/firm.c +++ b/source/firm.c @@ -12,7 +12,6 @@ #include "crypto.h" #include "draw.h" #include "screeninit.h" -#include "loader.h" #include "buttons.h" #include "../build/patches.h" diff --git a/source/fs.c b/source/fs.c index 77248d03..c1e1f1c9 100644 --- a/source/fs.c +++ b/source/fs.c @@ -4,9 +4,12 @@ #include "fs.h" #include "memory.h" -#include "buttons.h" +#include "screeninit.h" +#include "../build/loader.h" #include "fatfs/ff.h" +#include "buttons.h" +#define PAYLOAD_ADDRESS 0x24F00000 #define PATTERN(a) a "_*.bin" static FATFS sdFs, @@ -55,7 +58,7 @@ u32 fileWrite(const void *buffer, const char *path, u32 size) return result; } -u32 payloadExists(u32 pressed) +void loadPayload(u32 pressed) { const char *pattern; @@ -80,14 +83,15 @@ u32 payloadExists(u32 pressed) if(result == FR_OK && info.fname[0]) { + initScreens(); + memcpy((void *)PAYLOAD_ADDRESS, loader, loader_size); + path[sizeof(path) - 1] = '/'; - memcpy((void *)0x24F02000, path, sizeof(path)); - memcpy((void *)(0x24F02000 + sizeof(path)), info.altname, 13); + memcpy((void *)(PAYLOAD_ADDRESS + 4), path, sizeof(path)); + memcpy((void *)(PAYLOAD_ADDRESS + 4 + sizeof(path)), info.altname, 13); - return 1; + ((void (*)())PAYLOAD_ADDRESS)(); } - - return 0; } void firmRead(void *dest, const char *firmFolder) diff --git a/source/fs.h b/source/fs.h index a856c420..5f7180e0 100644 --- a/source/fs.h +++ b/source/fs.h @@ -9,5 +9,5 @@ u32 mountFs(void); u32 fileRead(void *dest, const char *path, u32 size); u32 fileWrite(const void *buffer, const char *path, u32 size); -u32 payloadExists(u32 pressed); +void loadPayload(u32 pressed); void firmRead(void *dest, const char *firmFolder); \ No newline at end of file diff --git a/source/loader.c b/source/loader.c deleted file mode 100644 index c48184a6..00000000 --- a/source/loader.c +++ /dev/null @@ -1,21 +0,0 @@ -/* -* loader.c -*/ - -#include "loader.h" -#include "fs.h" -#include "memory.h" -#include "screeninit.h" -#include "../build/loader.h" - -#define PAYLOAD_ADDRESS 0x24F00000 - -void loadPayload(u32 pressed) -{ - if(payloadExists(pressed)) - { - initScreens(); - memcpy((void *)PAYLOAD_ADDRESS, loader, loader_size); - ((void (*)())PAYLOAD_ADDRESS)(); - } -} \ No newline at end of file diff --git a/source/loader.h b/source/loader.h deleted file mode 100644 index 3fba42eb..00000000 --- a/source/loader.h +++ /dev/null @@ -1,9 +0,0 @@ -/* -* loader.h -*/ - -#pragma once - -#include "types.h" - -void loadPayload(u32 pressed); \ No newline at end of file From a28b50cee84866368438e480ff74a07c5c93048f Mon Sep 17 00:00:00 2001 From: Aurora Date: Fri, 29 Apr 2016 18:07:00 +0200 Subject: [PATCH 4/4] We do not need all that space anymore --- screeninit/linker.ld | 2 +- screeninit/source/main.c | 2 +- source/screeninit.c | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/screeninit/linker.ld b/screeninit/linker.ld index 220fb529..baf1b2fb 100755 --- a/screeninit/linker.ld +++ b/screeninit/linker.ld @@ -1,7 +1,7 @@ ENTRY(_start) SECTIONS { - . = 0x24F03000; + . = 0x24F02000; .text.start : { *(.text.start) } .text : { *(.text) } .data : { *(.data) } diff --git a/screeninit/source/main.c b/screeninit/source/main.c index 5d36e477..487a6247 100755 --- a/screeninit/source/main.c +++ b/screeninit/source/main.c @@ -3,7 +3,7 @@ void main(void) { const u32 brightness[4] = {0x5F, 0x4C, 0x39, 0x26}; - u32 brightnessLevel = *(vu32 *)0x24F03008; + u32 brightnessLevel = *(vu32 *)0x24F02008; vu32 *const arm11 = (u32 *)0x1FFFFFF8; *(vu32 *)0x10141200 = 0x1007F; diff --git a/source/screeninit.c b/source/screeninit.c index a8eb0128..11e6e111 100644 --- a/source/screeninit.c +++ b/source/screeninit.c @@ -12,7 +12,7 @@ #include "i2c.h" #include "../build/screeninit.h" -#define SCREENINIT_ADDRESS 0x24F03000 +#define SCREENINIT_ADDRESS 0x24F02000 vu32 *arm11Entry = (u32 *)0x1FFFFFF8;