]> Chaos Git - corbenik/corbenik.git/commitdiff
Add ability to dump code sections 1/head
authorWolfvak <soherrera1@hotmail.com>
Mon, 6 Jun 2016 14:29:17 +0000 (11:29 -0300)
committerWolfvak <soherrera1@hotmail.com>
Mon, 6 Jun 2016 14:29:17 +0000 (11:29 -0300)
Only for regular titles and demos, not for system titiles/modules

external/loader/source/loader.c
external/loader/source/patcher.c
external/loader/source/patcher.h
source/config.h
source/menu.c

index 960764f5e5f65fd0cf57041e62ee43cb994e053a..9ddf277c7406041331a8923bc83e1cc0306f9999 100644 (file)
@@ -94,6 +94,9 @@ load_code(u64 progid, u16 progver, prog_addrs_t *shared, prog_addrs_t *original,
         lzss_decompress((u8 *)shared->text_addr + size);
     }
 
+    // Dump code section
+    dump_code(progid, (u8 *)shared->text_addr, shared->total_size << 12);
+
     // Patch segments
     patch_exe (progid, progver, (u8 *)shared->text_addr, shared->text_size << 12, original->text_size << 12,
                                 (u8 *)shared->data_addr, shared->data_size << 12, original->data_size << 12,
index 4551a4a2152a700017dd12eaad2c322250b9aff6..e4908f2c7da5473ca79fae44d23ad46690936142 100644 (file)
 #endif
 #include "../../../source/config.h"
 
+#define CODE_PATH PATH_EXEFS "/0000000000000000"
+
+static const char hexDigits[] = "0123456789ABCDEF";
+
 int
 fileOpen(Handle *file, FS_ArchiveID id, const char *path, int flags)
 {
@@ -99,7 +103,6 @@ loadTitleLocaleConfig(u64 progId, u8 *regionId, u8 *languageId)
     char path[] = "/corbenik/locale/0000000000000000";
     u32 i = 32;
     while (progId) {
-        static const char hexDigits[] = "0123456789ABCDEF";
         path[i--] = hexDigits[(u32)(progId & 0xF)];
         progId >>= 4;
     }
@@ -307,6 +310,50 @@ overlay_patch(u64 progId, u8 *code, u32 size)
     // TODO - Implement. Needs some thought. This should allow usage of files off SD rather than RomFS.
 }
 
+void
+dump_code(u64 progId, u8 *code_loc, u32 code_len)
+{
+    // If configuration was not loaded, or the option is disabled
+    if (failed_load_config || !config.options[OPTION_LOADER_DUMPCODE])
+        return;
+
+    u32 highTid = progId >> 0x20;
+
+    // Only regular titles and demos will get dumped
+    // Otherwise it's insanely slow
+    if (highTid != 0x00040000 && highTid != 0x00040002)
+        return;
+
+    char code_path[] = CODE_PATH;
+    u32 i = strlen(code_path) - 1;
+    Handle code_f;
+    while (progId) {
+        code_path[i--] = hexDigits[(u32)(progId & 0xF)];
+        progId >>= 4;
+    }
+
+    if (R_SUCCEEDED(fileOpen(&code_f, ARCHIVE_SDMC, code_path, FS_OPEN_READ)))
+    // Code was already dumped, do nothing
+    {
+        FSFILE_Close(code_f);
+        return;
+    }
+
+    else
+    {
+        if (R_SUCCEEDED(fileOpen(&code_f, ARCHIVE_SDMC, code_path, FS_OPEN_WRITE | FS_OPEN_CREATE)))
+        {
+            u32 len = 0;
+            FSFILE_Write(code_f, &len, 0, code_loc, code_len, FS_WRITE_FLUSH | FS_WRITE_UPDATE_TIME);
+            logstr("dumped code to ");
+            logstr(code_path);
+            logstr("\n");
+        }
+    }
+
+    return;
+}
+
 // This is only for the .code segment.
 void
 patch_exe(u64 progId, u16 progver,
index 460b3d90e54b85ac195c72f4397a83b054c1fd70..69350637b658e18245266542c9776256c7254492 100644 (file)
@@ -8,6 +8,8 @@ void patch_exe(u64 progId, u16 progver,
     u8* data, u32 data_size, u32 orig_data,
     u8* ro, u32 ro_size, u32 orig_ro);
 
+void dump_code(u64 progId, u8 *code_loc, u32 code_len);
+
 u32 get_text_extend(u64 progId, u16 progver, u32 size_orig);
 u32 get_ro_extend(u64 progId, u16 progver, u32 size_orig);
 u32 get_data_extend(u64 progId, u16 progver, u32 size_orig);
index 95371ba875207bdbfce3a31448c4857691bb0d07..bc01063e5c81936fce08f0cdebfcfbe17e5b2b59 100644 (file)
@@ -102,6 +102,10 @@ struct options_s
 // Which EmuNAND to use (currently only allows 10 total, but eh, I can change that if anyone truly needs it)
 #define OPTION_EMUNAND_INDEX 17
 
+// Dump titles' code section as they're loaded by the loader module
+// WARNING!! Slows down boot time considerably the first time!
+#define OPTION_LOADER_DUMPCODE 252
+
 // Save log files during boot and from loader.
 // This will slow things down a bit.
 #define OPTION_SAVE_LOGS 253
index e2b6ec308bd601b61a5085566a08fb37f5dbe6f2..74e772ab3e582d18e50c3c2bd198b247f13c6f93 100644 (file)
@@ -25,6 +25,7 @@ static struct options_s options[] = {
     { 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 },
+    { OPTION_LOADER_DUMPCODE, "  Dump code section", "Dumps code sections to SD card only the first time they're loaded.", boolean_val, 0, 0 },
 
     { 0, "", "", not_option, 0, 0 },