From 419a0117eea1f07c9038f050633020c76232dc34 Mon Sep 17 00:00:00 2001 From: chaoskagami Date: Wed, 3 Aug 2016 05:38:17 -0400 Subject: [PATCH] More malloc'ing (and linker script adjustments) --- include/option.h | 4 ++++ source/display.c | 2 +- source/firm/firm.c | 4 ++-- source/interp.c | 4 ++++ source/linker.ld | 29 +++++++++++++++++++++++++---- source/main.c | 6 +++--- source/menu.c | 4 ++-- source/option.c | 36 ++++++++++++++++++++---------------- source/patcher.c | 14 +++++++------- source/std/draw.c | 10 +++++----- source/std/fs.c | 2 +- 11 files changed, 74 insertions(+), 41 deletions(-) diff --git a/include/option.h b/include/option.h index ba5f154..9057b27 100644 --- a/include/option.h +++ b/include/option.h @@ -26,7 +26,11 @@ struct patch_state uint8_t state; } __attribute__((packed)); +#ifdef LOADER extern struct config_file config; +#else +extern struct config_file *config; +#endif enum type { diff --git a/source/display.c b/source/display.c index a58dd00..96dacc0 100644 --- a/source/display.c +++ b/source/display.c @@ -17,7 +17,7 @@ void show_help(char* help) { void accent_color(void* screen, int fg) { char color[] = "\x1b[30m"; if (!fg) color[2] = '4'; - color[3] = ("01234567")[config.options[OPTION_ACCENT_COLOR]]; + color[3] = ("01234567")[config->options[OPTION_ACCENT_COLOR]]; fprintf(screen, "%s", color); } diff --git a/source/firm/firm.c b/source/firm/firm.c index dadcf7d..f8ab2da 100644 --- a/source/firm/firm.c +++ b/source/firm/firm.c @@ -557,7 +557,7 @@ boot_cfw() load_firms(); - if (config.options[OPTION_RECONFIGURED]) { + if (config->options[OPTION_RECONFIGURED]) { fprintf(stderr, "Generating patch cache...\n"); generate_patch_cache(); } @@ -566,7 +566,7 @@ boot_cfw() if (patch_firm_all() != 0) return; - if (config.options[OPTION_REBOOT] && config.options[OPTION_RECONFIGURED]) { + if (config->options[OPTION_REBOOT] && config->options[OPTION_RECONFIGURED]) { fprintf(stderr, "Saving FIRM for reboot...\n"); if (!write_file(firm_loc, PATH_NATIVE_P, firm_size)) abort("Failed to save prepatched native\n"); diff --git a/source/interp.c b/source/interp.c index eed4b45..716d0e2 100644 --- a/source/interp.c +++ b/source/interp.c @@ -721,7 +721,11 @@ execb(char *filename, int build_cache) #endif int debug = 0; +#ifdef LOADER if (config.options[OPTION_OVERLY_VERBOSE]) { +#else + if (config->options[OPTION_OVERLY_VERBOSE]) { +#endif debug = 1; } diff --git a/source/linker.ld b/source/linker.ld index aba1eaa..ce3aa9e 100644 --- a/source/linker.ld +++ b/source/linker.ld @@ -8,24 +8,45 @@ SECTIONS *(.text.start) } + . = ALIGN(4); + .text : { *(.text) } + . = ALIGN(4); + .data : { *(.data) } + . = ALIGN(4); + + .rodata : { + *(.rodata) + } + + . = ALIGN(4); + + .rel : { + *(.rel) + } + + . = ALIGN(4); + + .symtab : { + *(.symtab) + } + + . = ALIGN(4); + .bss : { __bss_start = .; *(.bss COMMON) } __bss_end = .; - .rodata : { - *(.rodata) - } - . = ALIGN(4); + __end__ = 0x20400000; } diff --git a/source/main.c b/source/main.c index c3a2da5..cc8844c 100644 --- a/source/main.c +++ b/source/main.c @@ -51,12 +51,12 @@ main(int argc, char** argv) if (CFG_BOOTENV == 7) { fprintf(stderr, "Rebooted from AGB, disabling EmuNAND.\n"); - config.options[OPTION_EMUNAND] = 0; + config->options[OPTION_EMUNAND] = 0; } // Autoboot. Non-standard code path. - if (config.options[OPTION_AUTOBOOT] && !(ctr_hid_get_buttons() & CTR_HID_RT)) { - if (config.options[OPTION_SILENCE]) + if (config->options[OPTION_AUTOBOOT] && !(ctr_hid_get_buttons() & CTR_HID_RT)) { + if (config->options[OPTION_SILENCE]) shut_up(); // This does exactly what it sounds like. doing_autoboot = 1; } else { diff --git a/source/menu.c b/source/menu.c index 3743a87..fa9a4e1 100644 --- a/source/menu.c +++ b/source/menu.c @@ -66,7 +66,7 @@ void header(char *append) { set_cursor(TOP_SCREEN, 0, 0); - fill_line(stdout, 0, config.options[OPTION_ACCENT_COLOR]); + fill_line(stdout, 0, config->options[OPTION_ACCENT_COLOR]); accent_color(TOP_SCREEN, 0); fprintf(stdout, "\x1b[30m ." FW_NAME " // %s\x1b[0m\n\n", append); } @@ -181,7 +181,7 @@ menu_patches() void menu_options() { - show_menu(options, config.options); + show_menu(options, config->options); } #ifndef REL diff --git a/source/option.c b/source/option.c index 0125d49..1d8d710 100644 --- a/source/option.c +++ b/source/option.c @@ -2,22 +2,23 @@ FILE *conf_handle; -struct config_file config; +struct config_file *config; extern uint8_t *enable_list; void list_patches_build(char *name, int desc_is_fname); void regenerate_config() { - memset(&config, 0, sizeof(config)); - memcpy(&(config.magic), CONFIG_MAGIC, 4); - config.config_ver = config_version; - config.options[OPTION_ACCENT_COLOR] = 2; + for(int i=0; i < 4; i++) + config->magic[i] = CONFIG_MAGIC[i]; + + config->config_ver = config_version; + config->options[OPTION_ACCENT_COLOR] = 2; if (!(conf_handle = fopen(PATH_CONFIG, "w"))) abort("Failed to open config for write?\n"); - fwrite(&config, 1, sizeof(config), conf_handle); + fwrite(config, 1, sizeof(struct config_file), conf_handle); fclose(conf_handle); fprintf(BOTTOM_SCREEN, "Config file written.\n"); @@ -57,9 +58,9 @@ update_config() { int updated = 0; - if (config.options[OPTION_ACCENT_COLOR] == 0) { + if (config->options[OPTION_ACCENT_COLOR] == 0) { fprintf(stderr, "Config update: accent color\n"); - config.options[OPTION_ACCENT_COLOR] = 2; + config->options[OPTION_ACCENT_COLOR] = 2; updated = 1; } @@ -71,6 +72,9 @@ update_config() void load_config() { + config = (struct config_file*)malloc(sizeof(struct config_file)); + memset(config, 0, sizeof(struct config_file)); + mk_structure(); // Make directory structure if needed. // Zero on success. @@ -81,21 +85,21 @@ load_config() PATH_CONFIG); regenerate_config(); } else { - fread(&config, 1, sizeof(config), conf_handle); + fread(config, 1, sizeof(struct config_file), conf_handle); fclose(conf_handle); - if (memcmp(&(config.magic), CONFIG_MAGIC, 4)) { + if (memcmp(&(config->magic), CONFIG_MAGIC, 4)) { fprintf(BOTTOM_SCREEN, "Config file at:\n" " %s\n" "has incorrect magic:\n" " '%c%c%c%c'\n" "Regenerating with defaults.\n", - PATH_CONFIG, config.magic[0], config.magic[1], config.magic[2], config.magic[3]); + PATH_CONFIG, config->magic[0], config->magic[1], config->magic[2], config->magic[3]); f_unlink(PATH_CONFIG); regenerate_config(); } - if (config.config_ver < config_version) { + if (config->config_ver < config_version) { fprintf(BOTTOM_SCREEN, "Config file has outdated version:\n" " %s\n" "Regenerating with defaults...\n", @@ -107,7 +111,7 @@ load_config() list_patches_build(PATH_PATCHES, 0); - if (!config.options[OPTION_SILENCE]) + if (!config->options[OPTION_SILENCE]) fprintf(BOTTOM_SCREEN, "Config file loaded.\n"); update_config(); @@ -125,10 +129,10 @@ save_config() if (!(conf_handle = fopen(PATH_CONFIG, "w"))) abort("Failed to open config for write?\n"); - config.options[OPTION_RECONFIGURED] = 0; // This should not persist to disk. + config->options[OPTION_RECONFIGURED] = 0; // This should not persist to disk. - fwrite(&config, 1, sizeof(config), conf_handle); + fwrite(config, 1, sizeof(struct config_file), conf_handle); fclose(conf_handle); - config.options[OPTION_RECONFIGURED] = 1; // Save caches on boot. + config->options[OPTION_RECONFIGURED] = 1; // Save caches on boot. } diff --git a/source/patcher.c b/source/patcher.c index dc919af..375307d 100644 --- a/source/patcher.c +++ b/source/patcher.c @@ -18,7 +18,7 @@ extern struct options_s* patches; void wait() { - if (config.options[OPTION_TRACE] && !doing_autoboot) { + if (config->options[OPTION_TRACE] && !doing_autoboot) { fprintf(stderr, "[Waiting...]"); wait_key(0); // No delay on traces. } @@ -58,22 +58,22 @@ patch_firm_all() fprintf(stderr, "VM exited without issue\n"); // Hook firmlaunch? - if (config.options[OPTION_REBOOT]) { + if (config->options[OPTION_REBOOT]) { patch_reboot(); wait(); } // Use EmuNAND? - if (config.options[OPTION_EMUNAND]) { + if (config->options[OPTION_EMUNAND]) { // Yes. - patch_emunand(config.options[OPTION_EMUNAND_INDEX]); + patch_emunand(config->options[OPTION_EMUNAND_INDEX]); wait(); } // Inject services? - if (config.options[OPTION_SVCS]) { + if (config->options[OPTION_SVCS]) { if (patch_services()) { abort("Fatal. Svc inject has failed."); } @@ -81,7 +81,7 @@ patch_firm_all() } // Replace loader? - if (config.options[OPTION_LOADER]) { + if (config->options[OPTION_LOADER]) { if (patch_modules()) { abort("Fatal. Loader inject has failed."); } @@ -90,7 +90,7 @@ patch_firm_all() } // Use ARM9 hook thread? - if (config.options[OPTION_ARM9THREAD]) { + if (config->options[OPTION_ARM9THREAD]) { // Yes. // FIXME - NYI diff --git a/source/std/draw.c b/source/std/draw.c index 410b219..8157300 100644 --- a/source/std/draw.c +++ b/source/std/draw.c @@ -209,7 +209,7 @@ void set_font(const char* filename) { } void dump_log(unsigned int force) { - if(!config.options[OPTION_SAVE_LOGS]) + if(!config->options[OPTION_SAVE_LOGS]) return; if (force == 0 && log_size < LOG_BUFFER_SIZE-1) @@ -241,7 +241,7 @@ clear_disp(uint8_t *screen) screen[j + 1] = top_bg[i]; screen[j + 2] = top_bg[i + 1]; screen[j + 3] = top_bg[i + 2]; - if (!kill_output && config.options[OPTION_DIM_MODE]) { + if (!kill_output && config->options[OPTION_DIM_MODE]) { screen[j + 1] = alphamap[screen[j + 1]]; screen[j + 2] = alphamap[screen[j + 2]]; screen[j + 3] = alphamap[screen[j + 3]]; @@ -256,7 +256,7 @@ clear_disp(uint8_t *screen) screen[j + 1] = bottom_bg[i]; screen[j + 2] = bottom_bg[i + 1]; screen[j + 3] = bottom_bg[i + 2]; - if (!kill_output && config.options[OPTION_DIM_MODE]) { + if (!kill_output && config->options[OPTION_DIM_MODE]) { screen[j + 1] = alphamap[screen[j + 1]]; screen[j + 2] = alphamap[screen[j + 2]]; screen[j + 3] = alphamap[screen[j + 3]]; @@ -327,7 +327,7 @@ draw_character(uint8_t *screen, const uint32_t character, int ch_x, int ch_y, co screen[pos + 1] = buffer_bg[pos_b]; screen[pos + 2] = buffer_bg[pos_b + 1]; screen[pos + 3] = buffer_bg[pos_b + 2]; - if (config.options[OPTION_DIM_MODE]) { + if (config->options[OPTION_DIM_MODE]) { screen[pos + 1] = alphamap[screen[pos + 1]]; screen[pos + 2] = alphamap[screen[pos + 2]]; screen[pos + 3] = alphamap[screen[pos + 3]]; @@ -343,7 +343,7 @@ draw_character(uint8_t *screen, const uint32_t character, int ch_x, int ch_y, co screen[pos + 1] = buffer_bg[pos_b]; screen[pos + 2] = buffer_bg[pos_b + 1]; screen[pos + 3] = buffer_bg[pos_b + 2]; - if (config.options[OPTION_DIM_MODE]) { + if (config->options[OPTION_DIM_MODE]) { screen[pos + 1] = alphamap[screen[pos + 1]]; screen[pos + 2] = alphamap[screen[pos + 2]]; screen[pos + 3] = alphamap[screen[pos + 3]]; diff --git a/source/std/fs.c b/source/std/fs.c index fb5d4da..9ef5e1e 100644 --- a/source/std/fs.c +++ b/source/std/fs.c @@ -80,7 +80,7 @@ fumount(void) if (f_mount(NULL, "SD:", 1)) return 1; - config.options[OPTION_SAVE_LOGS] = 0; // FS unmounted, can't log anymore + config->options[OPTION_SAVE_LOGS] = 0; // FS unmounted, can't log anymore return 0; } -- 2.39.5