From 9e851d2dfd1fe9c297c9c3062b59a8a704a5f853 Mon Sep 17 00:00:00 2001 From: Aurora Date: Tue, 5 Apr 2016 05:27:28 +0200 Subject: [PATCH] Small code reorganization --- Makefile | 2 +- source/config.c | 113 ++++++++++++++++++++++++++++++++++++++++++++ source/config.h | 15 ++++++ source/draw.h | 8 +++- source/firm.c | 30 ++++-------- source/firm.h | 9 ++++ source/screeninit.c | 1 + source/screeninit.h | 2 - source/utils.c | 108 +----------------------------------------- source/utils.h | 4 +- 10 files changed, 157 insertions(+), 135 deletions(-) create mode 100644 source/config.c create mode 100644 source/config.h diff --git a/Makefile b/Makefile index be82e58..bef19c7 100644 --- a/Makefile +++ b/Makefile @@ -96,7 +96,7 @@ $(dir_build)/main.elf: $(objects_cfw) $(CC) -nostartfiles $(LDFLAGS) -T linker.ld $(OUTPUT_OPTION) $^ $(dir_build)/memory.o : CFLAGS+=-O3 -$(dir_build)/utils.o : CFLAGS += -DCONFIG_TITLE="\"$(name) $(version) configuration\"" +$(dir_build)/config.o : CFLAGS += -DCONFIG_TITLE="\"$(name) $(version) configuration\"" $(dir_build)/%.o: $(dir_source)/%.c $(dir_build)/patches.h $(dir_build)/loader.h $(dir_build)/screeninit.h @mkdir -p "$(@D)" diff --git a/source/config.c b/source/config.c new file mode 100644 index 0000000..515c800 --- /dev/null +++ b/source/config.c @@ -0,0 +1,113 @@ +/* +* config.c +* by Aurora Wright +* Copyright (c) 2016 All Rights Reserved +*/ + +#include "config.h" +#include "utils.h" +#include "screeninit.h" +#include "draw.h" +#include "fs.h" +#include "i2c.h" +#include "buttons.h" + +void configureCFW(const char *configPath, const char *patchedFirms[]) +{ + initScreens(); + + drawString(CONFIG_TITLE, 10, 10, COLOR_TITLE); + drawString("Press A to select, START to save and reboot", 10, 30, COLOR_WHITE); + + const char *optionsText[] = { "( ) Updated SysNAND mode (A9LH-only)", + "( ) Use pre-patched FIRMs", + "( ) Force A9LH detection", + "( ) Use 9.0 FIRM as default", + "( ) Use second EmuNAND as default", + "( ) Show current NAND in System Settings", + "( ) Show GBA boot screen in patched AGB_FIRM" }; + + u32 optionsAmount = sizeof(optionsText) / sizeof(char *); + + struct option { + int posY; + u32 enabled; + } options[optionsAmount]; + + //Parse the existing configuration + for(u32 i = 0; i < optionsAmount; i++) + options[i].enabled = (config >> i) & 1; + + options[optionsAmount].enabled = (config >> 10) & 3; + + //Pre-select the first configuration option + u32 selectedOption = 0; + + //Boring configuration menu + while(1) + { + u16 pressed = 0; + + do { + options[optionsAmount].posY = drawString("Screen-init brightness: 4( ) 3( ) 2( ) 1( )", 10, 53, selectedOption == optionsAmount ? COLOR_RED : COLOR_WHITE); + + for(u32 i = 0; i < optionsAmount; i++) + { + options[i].posY = drawString(optionsText[i], 10, !i ? options[optionsAmount].posY + 2 * SPACING_Y : options[i - 1].posY + SPACING_Y, selectedOption == i ? COLOR_RED : COLOR_WHITE); + drawCharacter('x', 10 + SPACING_X, options[i].posY, options[i].enabled ? (selectedOption == i ? COLOR_RED : COLOR_WHITE) : COLOR_BLACK); + } + + for(u32 i = 0; i < 4; i++) + drawCharacter('x', 10 + (26 + 5 * i) * SPACING_X, options[optionsAmount].posY, options[optionsAmount].enabled == i ? (selectedOption == optionsAmount ? COLOR_RED : COLOR_WHITE) : COLOR_BLACK); + + pressed = waitInput(); + } + while(!(pressed & MENU_BUTTONS)); + + switch(pressed) + { + case BUTTON_UP: + selectedOption = !selectedOption ? optionsAmount : selectedOption - 1; + break; + case BUTTON_DOWN: + selectedOption = selectedOption >= optionsAmount - 1 ? 0 : selectedOption + 1; + break; + case BUTTON_LEFT: + selectedOption = 0; + break; + case BUTTON_RIGHT: + selectedOption = optionsAmount - 1; + break; + case BUTTON_A: + if(selectedOption != optionsAmount) options[selectedOption].enabled = !options[selectedOption].enabled; + else options[optionsAmount].enabled = options[optionsAmount].enabled == 3 ? 0 : options[optionsAmount].enabled + 1; + break; + } + + if(pressed == BUTTON_START) break; + } + + //If the user has been using A9LH and the "Updated SysNAND" setting changed, delete the patched 9.0 FIRM + if(((config >> 16) & 1) && ((config & 1) != options[0].enabled)) fileDelete(patchedFirms[3]); + + //If the "Show GBA boot screen in patched AGB_FIRM" setting changed, delete the patched AGB_FIRM + if(((config >> 6) & 1) != options[6].enabled) fileDelete(patchedFirms[5]); + + //Preserve the last-used boot options (last 12 bits) + config &= 0xFFF000; + + //Parse and write the new configuration + for(u32 i = 0; i < optionsAmount; i++) + config |= options[i].enabled << i; + + config |= options[optionsAmount].enabled << 10; + + fileWrite(&config, configPath, 3); + + //Zero the last booted FIRM flag + CFG_BOOTENV = 0; + + //Reboot + i2cWriteRegister(I2C_DEV_MCU, 0x20, 1 << 2); + while(1); +} \ No newline at end of file diff --git a/source/config.h b/source/config.h new file mode 100644 index 0000000..add8049 --- /dev/null +++ b/source/config.h @@ -0,0 +1,15 @@ +/* +* config.h +* by Aurora Wright +* Copyright (c) 2016 All Rights Reserved +*/ + +#pragma once + +#include "types.h" + +#define CFG_BOOTENV (*(vu32 *)0x10010000) + +u32 config; + +void configureCFW(const char *configPath, const char *patchedFirms[]); \ No newline at end of file diff --git a/source/draw.h b/source/draw.h index ab69375..9b084c3 100644 --- a/source/draw.h +++ b/source/draw.h @@ -10,8 +10,12 @@ #include "types.h" -#define SPACING_Y 10 -#define SPACING_X 8 +#define SPACING_Y 10 +#define SPACING_X 8 +#define COLOR_TITLE 0xFF9900 +#define COLOR_WHITE 0xFFFFFF +#define COLOR_RED 0x0000FF +#define COLOR_BLACK 0x000000 void loadSplash(void); void clearScreens(void); diff --git a/source/firm.c b/source/firm.c index a488e88..3c8c64a 100755 --- a/source/firm.c +++ b/source/firm.c @@ -5,15 +5,16 @@ */ #include "firm.h" +#include "config.h" +#include "utils.h" +#include "fs.h" #include "patches.h" #include "memory.h" -#include "fs.h" #include "emunand.h" #include "crypto.h" #include "draw.h" #include "screeninit.h" #include "loader.h" -#include "utils.h" #include "buttons.h" #include "../build/patches.h" @@ -29,7 +30,7 @@ static const char *patchedFirms[] = { "/aurei/patched_firmware_sys.bin", "/aurei/patched_firmware90.bin", "/aurei/patched_firmware_twl.bin", "/aurei/patched_firmware_agb.bin" }; - +u32 config; static u32 firmSize, console, mode, @@ -39,7 +40,6 @@ static u32 firmSize, usePatchedFirm, emuOffset, emuHeader; -u32 config; void setupCFW(void) { @@ -252,16 +252,7 @@ static inline void patchTwlAgb(u32 whichFirm) twlAgbFirm->arm9Entry = (u8 *)0x801301C; } - struct patchData { - u32 offset[2]; - union { - u8 type0[8]; - u16 type1; - } patch; - u32 type; - }; - - static const struct patchData twlPatches[] = { + static const patchData twlPatches[] = { {{0x1650C0, 0x165D64}, {{ 6, 0x00, 0x20, 0x4E, 0xB0, 0x70, 0xBD }}, 0}, {{0x173A0E, 0x17474A}, { .type1 = 0x2001 }, 1}, {{0x174802, 0x17553E}, { .type1 = 0x2000 }, 2}, @@ -271,18 +262,17 @@ static inline void patchTwlAgb(u32 whichFirm) {{0x174D6A, 0x175AA6}, { .type1 = 0x2001 }, 2}, {{0x174E56, 0x175B92}, { .type1 = 0x2001 }, 1}, {{0x174E58, 0x175B94}, { .type1 = 0x4770 }, 1} - }; - - static const struct patchData agbPatches[] = { + }, + agbPatches[] = { {{0x9D2A8, 0x9DF64}, {{ 6, 0x00, 0x20, 0x4E, 0xB0, 0x70, 0xBD }}, 0}, {{0xD7A12, 0xD8B8A}, { .type1 = 0xEF26 }, 1} }; /* 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) */ - u32 numPatches = whichFirm ? (sizeof(agbPatches) / sizeof(struct patchData)) - !((config >> 6) & 1) : - (sizeof(twlPatches) / sizeof(struct patchData)); - const struct patchData *patches = whichFirm ? agbPatches : twlPatches; + u32 numPatches = whichFirm ? (sizeof(agbPatches) / sizeof(patchData)) - !((config >> 6) & 1) : + (sizeof(twlPatches) / sizeof(patchData)); + const patchData *patches = whichFirm ? agbPatches : twlPatches; //Patch for(u32 i = 0; i < numPatches; i++) diff --git a/source/firm.h b/source/firm.h index f647f43..40adb85 100644 --- a/source/firm.h +++ b/source/firm.h @@ -29,6 +29,15 @@ typedef struct firmHeader { firmSectionHeader section[4]; } firmHeader; +typedef struct patchData { + u32 offset[2]; + union { + u8 type0[8]; + u16 type1; + } patch; + u32 type; +} patchData; + void setupCFW(void); void loadFirm(void); void patchFirm(void); diff --git a/source/screeninit.c b/source/screeninit.c index 7702196..c727791 100644 --- a/source/screeninit.c +++ b/source/screeninit.c @@ -8,6 +8,7 @@ */ #include "screeninit.h" +#include "config.h" #include "memory.h" #include "draw.h" #include "i2c.h" diff --git a/source/screeninit.h b/source/screeninit.h index cbad6c9..3bf4c1e 100644 --- a/source/screeninit.h +++ b/source/screeninit.h @@ -13,7 +13,5 @@ #define PDN_GPU_CNT (*(vu8 *)0x10141200) -u32 config; - void deinitScreens(void); void initScreens(void); \ No newline at end of file diff --git a/source/utils.c b/source/utils.c index d854f20..dd9ef5d 100644 --- a/source/utils.c +++ b/source/utils.c @@ -11,17 +11,7 @@ #include "i2c.h" #include "buttons.h" -#define COLOR_TITLE 0xFF9900 -#define COLOR_WHITE 0xFFFFFF -#define COLOR_RED 0x0000FF -#define COLOR_BLACK 0x000000 - -struct option { - int posY; - u32 enabled; -}; - -static u32 waitInput(void) +u32 waitInput(void) { u32 pressedKey = 0; u32 key; @@ -47,102 +37,6 @@ static u32 waitInput(void) return key; } -void configureCFW(const char *configPath, const char *patchedFirms[]) -{ - initScreens(); - - drawString(CONFIG_TITLE, 10, 10, COLOR_TITLE); - drawString("Press A to select, START to save and reboot", 10, 30, COLOR_WHITE); - - const char *optionsText[] = { "( ) Updated SysNAND mode (A9LH-only)", - "( ) Use pre-patched FIRMs", - "( ) Force A9LH detection", - "( ) Use 9.0 FIRM as default", - "( ) Use second EmuNAND as default", - "( ) Show current NAND in System Settings", - "( ) Show GBA boot screen in patched AGB_FIRM" }; - - u32 optionsAmount = sizeof(optionsText) / sizeof(char *); - struct option options[optionsAmount]; - - //Parse the existing configuration - for(u32 i = 0; i < optionsAmount; i++) - options[i].enabled = (config >> i) & 1; - - options[optionsAmount].enabled = (config >> 10) & 3; - - //Pre-select the first configuration option - u32 selectedOption = 0; - - //Boring configuration menu - while(1) - { - u16 pressed = 0; - - do { - options[optionsAmount].posY = drawString("Screen-init brightness: 4( ) 3( ) 2( ) 1( )", 10, 53, selectedOption == optionsAmount ? COLOR_RED : COLOR_WHITE); - - for(u32 i = 0; i < optionsAmount; i++) - { - options[i].posY = drawString(optionsText[i], 10, !i ? options[optionsAmount].posY + 2 * SPACING_Y : options[i - 1].posY + SPACING_Y, selectedOption == i ? COLOR_RED : COLOR_WHITE); - drawCharacter('x', 10 + SPACING_X, options[i].posY, options[i].enabled ? (selectedOption == i ? COLOR_RED : COLOR_WHITE) : COLOR_BLACK); - } - - for(u32 i = 0; i < 4; i++) - drawCharacter('x', 10 + (26 + 5 * i) * SPACING_X, options[optionsAmount].posY, options[optionsAmount].enabled == i ? (selectedOption == optionsAmount ? COLOR_RED : COLOR_WHITE) : COLOR_BLACK); - - pressed = waitInput(); - } - while(!(pressed & MENU_BUTTONS)); - - switch(pressed) - { - case BUTTON_UP: - selectedOption = !selectedOption ? optionsAmount : selectedOption - 1; - break; - case BUTTON_DOWN: - selectedOption = selectedOption >= optionsAmount - 1 ? 0 : selectedOption + 1; - break; - case BUTTON_LEFT: - selectedOption = 0; - break; - case BUTTON_RIGHT: - selectedOption = optionsAmount - 1; - break; - case BUTTON_A: - if(selectedOption != optionsAmount) options[selectedOption].enabled = !options[selectedOption].enabled; - else options[optionsAmount].enabled = options[optionsAmount].enabled == 3 ? 0 : options[optionsAmount].enabled + 1; - break; - } - - if(pressed == BUTTON_START) break; - } - - //If the user has been using A9LH and the "Updated SysNAND" setting changed, delete the patched 9.0 FIRM - if(((config >> 16) & 1) && ((config & 1) != options[0].enabled)) fileDelete(patchedFirms[3]); - - //If the "Show GBA boot screen in patched AGB_FIRM" setting changed, delete the patched AGB_FIRM - if(((config >> 6) & 1) != options[6].enabled) fileDelete(patchedFirms[5]); - - //Preserve the last-used boot options (last 12 bits) - config &= 0xFFF000; - - //Parse and write the new configuration - for(u32 i = 0; i < optionsAmount; i++) - config |= options[i].enabled << i; - - config |= options[optionsAmount].enabled << 10; - - fileWrite(&config, configPath, 3); - - //Zero the last booted FIRM flag - CFG_BOOTENV = 0; - - //Reboot - i2cWriteRegister(I2C_DEV_MCU, 0x20, 1 << 2); - while(1); -} - void deleteFirms(const char *firmPaths[], u32 firms) { while(firms) diff --git a/source/utils.h b/source/utils.h index 0e257cf..1adc889 100644 --- a/source/utils.h +++ b/source/utils.h @@ -8,8 +8,6 @@ #include "types.h" -#define CFG_BOOTENV (*(vu32 *)0x10010000) - -void configureCFW(const char *configPath, const char *patchedFirms[]); +u32 waitInput(void); void deleteFirms(const char *firmPaths[], u32 firms); void error(const char *message); \ No newline at end of file