From 13322ffede0d82c72af1d6f86e3eaf47f58f49c2 Mon Sep 17 00:00:00 2001 From: chaoskagami Date: Sun, 5 Jun 2016 03:10:41 -0400 Subject: [PATCH] Multiple things (again) * Replace all instances of 'service' with 'svc' or 'svc call' to get my terminology straight * Make logging in loader toggleable, add boot logging, and add an option to the menu. Hopefully this will cut down on improper issue reporting. --- external/loader/Makefile | 2 +- external/loader/source/config.h | 26 -------------------------- external/loader/source/interp.c | 4 +--- external/loader/source/loader.c | 5 ++--- external/loader/source/logger.c | 7 ++++++- external/loader/source/memory.c | 2 +- external/loader/source/patcher.c | 4 ++-- source/config.c | 2 +- source/config.h | 21 ++++++--------------- source/firm/firm.c | 7 ++++--- source/menu.c | 12 ++++++++---- source/patch/svc.c | 2 +- source/patch_format.h | 2 +- source/patcher.c | 6 +++--- source/std/draw.c | 25 +++++++++++++++++++++++-- source/std/draw.h | 2 ++ 16 files changed, 62 insertions(+), 67 deletions(-) delete mode 100644 external/loader/source/config.h diff --git a/external/loader/Makefile b/external/loader/Makefile index 3f2ec9a..15fd374 100644 --- a/external/loader/Makefile +++ b/external/loader/Makefile @@ -36,7 +36,7 @@ CFLAGS := -flto -Wall -Os -mword-relocations $(ERROR) \ -fomit-frame-pointer -ffunction-sections -fdata-sections -fshort-wchar \ $(ARCH) -CFLAGS += $(INCLUDE) -DARM11 -D_3DS +CFLAGS += $(INCLUDE) -DARM11 -D_3DS -DLOADER=1 CXXFLAGS := $(CFLAGS) -fno-rtti -fno-exceptions -std=gnu99 diff --git a/external/loader/source/config.h b/external/loader/source/config.h deleted file mode 100644 index a9bb6ae..0000000 --- a/external/loader/source/config.h +++ /dev/null @@ -1,26 +0,0 @@ -#ifndef __CONFIG_H -#define __CONFIG_H - -__attribute__((unused)) static unsigned int config_version = 1; - -#define CONFIG_MAGIC "OVAN" - -// Structure of config file -struct config_file -{ - char magic[4]; // "OVAN" for shits and giggles again. - - uint32_t config_ver; // Config file version. - - uint8_t options[256]; // Options in the menu - deliberately large to avoid - // config version bumps. - - uint64_t patch_ids[256]; // What patches are enabled by UUID. 256 is an - // arbitrary limit - contact me if you hit it. -} __attribute__((packed)); - -#define OPTION_LOADER_CPU_L2 11 // Enable L2 cache. -#define OPTION_LOADER_CPU_800MHZ 12 // Enable 800Mhz mode. -#define OPTION_LOADER_LANGEMU 13 // Enable 800Mhz mode. - -#endif diff --git a/external/loader/source/interp.c b/external/loader/source/interp.c index 92440af..512f51b 100644 --- a/external/loader/source/interp.c +++ b/external/loader/source/interp.c @@ -1,8 +1,6 @@ // 'Tis not ready for the world at large yet. // I don't want to delete it since I'm working on it though, // so it's temporarliy #if'd 0. -#define LOADER 1 - #include <3ds.h> #include "patcher.h" #include "fsldr.h" @@ -14,7 +12,7 @@ #define PATH_MAX 255 #define _MAX_LFN 255 #endif -#include "config.h" +#include "../../../source/config.h" #include "../../../source/patch_format.h" // Because I want to avoid malloc. diff --git a/external/loader/source/loader.c b/external/loader/source/loader.c index 722d0ff..1b76653 100644 --- a/external/loader/source/loader.c +++ b/external/loader/source/loader.c @@ -153,7 +153,8 @@ loader_LoadProcess(Handle *process, u64 prog_handle) u32 text_grow, data_grow, ro_grow; u16 progver; - openLogger(); + load_config(); // First order of business - we need the config file. + openLogger(); // Open logs if enabled in config. // make sure the cached info corrosponds to the current prog_handle if (g_cached_prog_handle != prog_handle) { @@ -184,8 +185,6 @@ loader_LoadProcess(Handle *process, u64 prog_handle) logu64(progid); logstr(" validated params\n"); - load_config(); // First order of business - we need the config file. - // Check and set the CPU mode. Possible values: 0 - Keep o3ds speed, 1 - // n3ds speed, -1 force o3ds // This is A-OK because the CPU speed parameter isn't passed through to any diff --git a/external/loader/source/logger.c b/external/loader/source/logger.c index 9b8708f..6555cdb 100644 --- a/external/loader/source/logger.c +++ b/external/loader/source/logger.c @@ -8,15 +8,20 @@ #define PATH_MAX 255 #define _MAX_LFN 255 #endif -#include "config.h" +#include "../../../source/config.h" #include "../../../source/patch_format.h" Handle log_file_hdl; int logger_is_initd = 0; +extern struct config_file config; + void openLogger() { + if (config.options[OPTION_SAVE_LOGS] == 0) { + logger_is_initd = -1; // Setting -1 effectively disables logs. + } Result r; if (logger_is_initd) diff --git a/external/loader/source/memory.c b/external/loader/source/memory.c index eb294b7..d0cb84d 100644 --- a/external/loader/source/memory.c +++ b/external/loader/source/memory.c @@ -7,7 +7,7 @@ #define PATH_MAX 255 #define _MAX_LFN 255 #endif -#include "config.h" +#include "../../../source/config.h" #include "../../../source/patch_format.h" int diff --git a/external/loader/source/patcher.c b/external/loader/source/patcher.c index aa32f54..598a072 100644 --- a/external/loader/source/patcher.c +++ b/external/loader/source/patcher.c @@ -11,7 +11,7 @@ #define PATH_MAX 255 #define _MAX_LFN 255 #endif -#include "config.h" +#include "../../../source/config.h" int fileOpen(Handle *file, FS_ArchiveID id, const char *path, int flags) @@ -30,7 +30,7 @@ fileOpen(Handle *file, FS_ArchiveID id, const char *path, int flags) return FSLDR_OpenFileDirectly(file, id, apath, ppath, flags, 0); } -static struct config_file config; +struct config_file config; static int failed_load_config = 1; void diff --git a/source/config.c b/source/config.c index 541e730..e391c4f 100644 --- a/source/config.c +++ b/source/config.c @@ -30,7 +30,7 @@ mk_structure() f_mkdir(PATH_PATCHES); f_mkdir(PATH_FIRMWARES); f_mkdir(PATH_MODULES); - f_mkdir(PATH_SERVICES); + f_mkdir(PATH_SVC); f_mkdir(PATH_KEYS); f_mkdir(PATH_EXEFS); f_mkdir(PATH_TEMP); diff --git a/source/config.h b/source/config.h index 5e08761..e612267 100644 --- a/source/config.h +++ b/source/config.h @@ -1,7 +1,7 @@ #ifndef __CONFIG_H #define __CONFIG_H -_UNUSED static unsigned int config_version = 1; +#define config_version 1 #define CONFIG_MAGIC "OVAN" @@ -54,8 +54,8 @@ struct options_s // Use builtin loader module replacer. #define OPTION_LOADER 2 -// Inject services (including backdoor for 11) -#define OPTION_SERVICES 3 +// Inject svc calls (including backdoor for 11) +#define OPTION_SVCS 3 // Use builtin ARM9 thread injector. #define OPTION_ARM9THREAD 4 @@ -87,7 +87,7 @@ struct options_s // Enable language emulation. #define OPTION_LOADER_LANGEMU 13 -// Force replacement of services. Normally you don't want this. +// Force replacement of non-null svcs. Normally you don't want this. #define OPTION_REPLACE_ALLOCATED_SVC 14 // Ignore patch UUID dependencies. Not recommended. @@ -107,24 +107,15 @@ struct options_s // change and causes caches to be regenerated. #define OPTION_RECONFIGURED 255 -// TODO - Every option beyond here is a patch now, so once I get listing -// implemented, these shall go. - -#define OPTION_SIGPATCH 0 // Use builtin signature patch. -#define OPTION_FIRMPROT 1 // Protect firmware from writes. - -#define OPTION_AADOWNGRADE 16 // Anti-anti-downgrade. -#define OPTION_MEMEXEC 17 // Prevent MPU from disabling execute permissions. -#define OPTION_UNITINFO 18 // Dev UNITINFO. Note that this is overkill. - //#define HEADER_COLOR 12 // Color of header text. //#define BG_COLOR 13 // Color of background. //#define TEXT_COLOR 14 // Color of most text. //#define ARROW_COLOR 15 // Color of Arrow. +#ifndef LOADER void load_config(); void save_config(); - +#endif /* [CORBENIK] version=1 diff --git a/source/firm/firm.c b/source/firm/firm.c index 98d24c6..8dcc59a 100644 --- a/source/firm/firm.c +++ b/source/firm/firm.c @@ -286,9 +286,6 @@ boot_firm() fprintf(BOTTOM_SCREEN, "Updated keyX keyslots\n"); } - fumount(); // Unmount SD. No longer needed. - fprintf(BOTTOM_SCREEN, "SD Unmounted.\n"); - for (firm_section_h *section = firm_loc->section; section < firm_loc->section + 4 && section->address != 0; section++) { memcpy((void *)section->address, (void *)firm_loc + section->offset, section->size); } @@ -296,6 +293,10 @@ boot_firm() wait(); + clear_disp(BOTTOM_SCREEN); + + fumount(); // Unmount SD. No longer needed. + // No fprintf will work from here on out. *a11_entry = (uint32_t)arm11_preboot_halt; diff --git a/source/menu.c b/source/menu.c index 76b8436..e02f36d 100644 --- a/source/menu.c +++ b/source/menu.c @@ -21,14 +21,14 @@ static struct options_s options[] = { // Patches. { 0, "\x1b[32;40mOptions\x1b[0m", "", not_option, 0, 0 }, - { OPTION_LOADER, "System Modules", "Replaces system modules (including loader)", boolean_val, 0, 0 }, + { OPTION_LOADER, "Loader Replacement", "Replaces loader with one capable of the below and also applying bytecode patches.", boolean_val, 0, 0 }, { OPTION_LOADER_CPU_L2, " CPU - L2 cache", "Forces the system to use the L2 cache. Ignored if not a N3DS.", boolean_val, 0, 0 }, { OPTION_LOADER_CPU_800MHZ, " CPU - 800Mhz", "Forces the system to run in 800Mhz mode. Ignored if not a N3DS.", boolean_val, 0, 0 }, { OPTION_LOADER_LANGEMU, " Language Emulation", "Reads language emulation configuration and imitates the region/language.", boolean_val, 0, 0 }, { 0, "", "", not_option, 0, 0 }, - { OPTION_SERVICES, "Service Replacement", "Replaces ARM11 services, including svcBackdoor. With 11.0 NATIVE_FIRM, you need this.", boolean_val, 0, 0 }, + { OPTION_SVCS, "SVC Replacement", "Replaces ARM11 svc calls, including svcBackdoor. With 11.0 NATIVE_FIRM, you probably want this.", boolean_val, 0, 0 }, { 0, "", "", not_option, 0, 0 }, @@ -44,10 +44,14 @@ static struct options_s options[] = { // Patches. { 0, "\x1b[32;40mDeveloper Options\x1b[0m", "", not_option, 0, 0 }, - { OPTION_REPLACE_ALLOCATED_SVC, "Force service replace", "Replace ARM11 services even if they exist. Don't use unless you know what you're doing.", + { OPTION_REPLACE_ALLOCATED_SVC, "Force svc replace", "Replace ARM11 svc calls even if they exist. Don't use this unless you know what you're doing.", boolean_val, 0, 0 }, - { OPTION_TRACE, "Debug Pauses", "After each important step, [WAIT] will be shown and you'll need to press a key. Debug.", boolean_val, 0, 0 }, + { OPTION_TRACE, "Step Through", "After each important step, [WAIT] will be shown and you'll need to press a key. Debug.", boolean_val, 0, 0 }, { OPTION_OVERLY_VERBOSE, "Verbose", "Output more debug information than the average user needs.", boolean_val, 0, 0 }, + { OPTION_SAVE_LOGS, "Logging", "Save logs to the corbenik folder. Slows operation a bit.", boolean_val, 0, 0 }, + + { OPTION_RECONFIGURED, "Reconfigured (DO NOT UNCHECK)", "If patches were reconfigured, this will be set. Do not untick this if ticked.", boolean_val, 0, + 0 }, // { OPTION_ARM9THREAD, "ARM9 Thread", boolean_val, 0, 0 }, // { IGNORE_PATCH_DEPS, "Ignore dependencies", boolean_val, 0, 0 }, diff --git a/source/patch/svc.c b/source/patch/svc.c index 7ec806e..281f9a6 100644 --- a/source/patch/svc.c +++ b/source/patch/svc.c @@ -33,7 +33,7 @@ PATCH(services) fprintf(stderr, "Svc: table at %x\n", (uint32_t)svcTable); - char str[] = PATH_SERVICES "/00.bin"; + char str[] = PATH_SVC "/00.bin"; char *at = str + (strlen(str) - 6); // FIXME - This is really slow. Some way to optimize it? for (uint32_t i = 0; i <= 0xf; i++) { diff --git a/source/patch_format.h b/source/patch_format.h index b2f76a3..c9af51b 100644 --- a/source/patch_format.h +++ b/source/patch_format.h @@ -38,7 +38,7 @@ #define PATH_PATCHES PATH_CFW "/patch" // Patch binary folder. #define PATH_FIRMWARES PATH_CFW "/firmware" // Firmware folder. #define PATH_MODULES PATH_CFW "/module" // Sysmodule location -#define PATH_SERVICES PATH_CFW "/svc" // Svc code location. +#define PATH_SVC PATH_CFW "/svc" // Svc code location. #define PATH_TEMP PATH_CFW "/cache" // Files that are transient and used to speed operation #define PATH_LOADER_CACHE PATH_TEMP "/loader" // Cached patch bytecode for loader. diff --git a/source/patcher.c b/source/patcher.c index 64dd5b6..b8ae1ed 100644 --- a/source/patcher.c +++ b/source/patcher.c @@ -62,16 +62,16 @@ patch_firm_all() // Replace loader? if (config.options[OPTION_LOADER]) { if (patch_modules()) { - abort("Fatal. Service patch has failed."); + abort("Fatal. Loader inject has failed."); } // This requires OPTION_SIGPATCH. wait(); } // Inject services? - if (config.options[OPTION_SERVICES]) { + if (config.options[OPTION_SVCS]) { if (patch_services()) { - abort("Fatal. Service patch has failed."); + abort("Fatal. Svc inject has failed."); } wait(); } diff --git a/source/std/draw.c b/source/std/draw.c index d8bff70..08f6283 100644 --- a/source/std/draw.c +++ b/source/std/draw.c @@ -8,14 +8,16 @@ #include "../fatfs/ff.h" #include "fs.h" #include "unused.h" +#include "../config.h" +#include "../patch_format.h" static unsigned int top_cursor_x = 0, top_cursor_y = 0; static unsigned int bottom_cursor_x = 0, bottom_cursor_y = 0; -#ifdef BUFFER -static char text_buffer_top[TEXT_TOP_HEIGHT * TEXT_TOP_WIDTH + 1]; static char text_buffer_bottom[TEXT_BOTTOM_HEIGHT * TEXT_BOTTOM_WIDTH + 1]; +#ifdef BUFFER +static char text_buffer_top[TEXT_TOP_HEIGHT * TEXT_TOP_WIDTH + 1]; static char color_buffer_top[TEXT_TOP_HEIGHT * TEXT_TOP_WIDTH + 1]; static char color_buffer_bottom[TEXT_BOTTOM_HEIGHT * TEXT_BOTTOM_WIDTH + 1]; #endif @@ -42,6 +44,22 @@ static uint32_t colors[16] = { void clear_disp(uint8_t *screen) { + // There's a reason the logging code is here rather than putc: + // writing a batch is faster. + if (screen == BOTTOM_SCREEN && config.options[OPTION_SAVE_LOGS]) { + FILE *f = fopen(PATH_CFW "/boot.log", "w"); + fseek(f, 0, SEEK_END); + for (int i = 0; i < TEXT_BOTTOM_HEIGHT; i++) { + char *text = text_buffer_bottom + (TEXT_BOTTOM_WIDTH * i); + if (text[0] == 0) + break; + fwrite(text, 1, strnlen(text, TEXT_BOTTOM_WIDTH), f); + fwrite("\n", 1, 1, f); + } + fclose(f); + memset(text_buffer_bottom, 0, TEXT_BOTTOM_WIDTH * TEXT_BOTTOM_HEIGHT); + } + if (screen == TOP_SCREEN) screen = framebuffers->top_left; else if (screen == BOTTOM_SCREEN) @@ -224,6 +242,7 @@ putc(void *buf, const int c) clear_disp(buf); cursor_x[0] = 0; cursor_y[0] = 0; + /* uint32_t col = SCREEN_TOP_HEIGHT * SCREEN_DEPTH; uint32_t one_c = 8 * SCREEN_DEPTH; for (unsigned int x = 0; x < width * 8; x++) { @@ -254,6 +273,8 @@ putc(void *buf, const int c) } #else + if (buf == BOTTOM_SCREEN) + text_buffer_bottom[cursor_y[0] * width + cursor_x[0]] = c; draw_character(screen, c, cursor_x[0], cursor_y[0], colors[(*color >> 4) & 0xF], colors[*color & 0xF]); #endif diff --git a/source/std/draw.h b/source/std/draw.h index f3c7641..bc5d864 100644 --- a/source/std/draw.h +++ b/source/std/draw.h @@ -66,6 +66,8 @@ void fflush(void *channel); void set_cursor(void *channel, unsigned int x, unsigned int y); +void clear_disp(uint8_t *screen); + // Like printf. Supports the following format specifiers: // %s - char* // %c - char -- 2.39.5