]> Chaos Git - corbenik/corbenik.git/commitdiff
Multiple bits here:
authorchaoskagami <chaos.kagami@gmail.com>
Sun, 5 Jun 2016 04:53:56 +0000 (00:53 -0400)
committerchaoskagami <chaos.kagami@gmail.com>
Sun, 5 Jun 2016 04:56:32 +0000 (00:56 -0400)
 * service -> svc because I'm a dumbass
 * Added a few new opcodes: OP_OR, OP_XOR, OP_NOT, OP_VER
 * I'd make a joke about stability here, but meh.

15 files changed:
external/Makefile
external/loader/source/interp.h
external/loader/source/loader.c
external/loader/source/patcher.c
external/loader/source/patcher.h
external/svc/7b.s [moved from external/service/7b.s with 100% similarity]
external/svc/Makefile [moved from external/service/Makefile with 100% similarity]
external/svc/README.md [moved from external/service/README.md with 67% similarity]
external/svc/link.ld [moved from external/service/link.ld with 100% similarity]
external/svc/stub.s [moved from external/service/stub.s with 100% similarity]
host/bytecode_asm.py
source/fatfs/sdmmc.c
source/firm/firm.c
source/interp.c
source/patch_format.h

index e2306df15e91dca0814ed9aa1085f2e795748d95..68c70ee0c3feb6e1f4bc0f8fe40599d3c616dcb6 100644 (file)
@@ -1,13 +1,13 @@
 .PHONY: all
-all: loader service
+all: loader svc
        mkdir -p ../out/corbenik/module
-       mkdir -p ../out/corbenik/service
+       mkdir -p ../out/corbenik/svc
        cp loader/loader.cxi ../out/corbenik/module/loader.cxi
-       cp service/7b.bin ../out/corbenik/service/7b.bin
+       cp svc/7b.bin ../out/corbenik/svc/7b.bin
 
 .PHONY: clean
-clean: clean_loader clean_service
-       rm -rf ../out/corbenik/service
+clean: clean_loader clean_svc
+       rm -rf ../out/corbenik/svc
        rm -rf ../out/corbenik/module
 
 .PHONY: loader
@@ -18,10 +18,10 @@ loader:
 clean_loader:
        make -C loader clean
 
-.PHONY: service
-service:
-       make -C service
+.PHONY: svc
+svc:
+       make -C svc
 
-.PHONY: clean_service
-clean_service:
-       make -C service clean
+.PHONY: clean_svc
+clean_svc:
+       make -C svc clean
index d48da3275c2b8b942f5f647d2cd3bd5e1da51669..091b4c14e5604eea09cf23ab62f0088c3007f6f3 100644 (file)
@@ -1,6 +1,6 @@
 #ifndef __INTERP_H
 #define __INTERP_H
 
-int execb(uint64_t tid, uint8_t *search_mem, uint32_t search_len);
+int execb(uint64_t tid, uint16_t ver, uint8_t *search_mem, uint32_t search_len);
 
 #endif
index f9f3229f6c97d37a2707856bbd16cefc5952087a..722d0ffe6a658a4b6294da12289cb0de8eaea773 100644 (file)
@@ -49,7 +49,7 @@ allocate_shared_mem(prog_addrs_t *shared, prog_addrs_t *vaddr, int flags)
 }
 
 static Result
-load_code(u64 progid, prog_addrs_t *shared, prog_addrs_t *original, u64 prog_handle, int is_compressed)
+load_code(u64 progid, u16 progver, prog_addrs_t *shared, prog_addrs_t *original, u64 prog_handle, int is_compressed)
 {
     Handle handle;
     FS_Path archivePath;
@@ -95,9 +95,9 @@ load_code(u64 progid, prog_addrs_t *shared, prog_addrs_t *original, u64 prog_han
     }
 
     // Patch segments
-    patch_text(progid, (u8 *)shared->text_addr, shared->text_size << 12, original->text_size << 12);
-    patch_data(progid, (u8 *)shared->data_addr, shared->data_size << 12, original->data_size << 12);
-    patch_ro(progid, (u8 *)shared->ro_addr, shared->ro_size << 12, original->ro_size << 12);
+    patch_text(progid, progver, (u8 *)shared->text_addr, shared->text_size << 12, original->text_size << 12);
+    patch_data(progid, progver, (u8 *)shared->data_addr, shared->data_size << 12, original->data_size << 12);
+    patch_ro(progid, progver, (u8 *)shared->ro_addr, shared->ro_size << 12, original->ro_size << 12);
 
     return 0;
 }
@@ -151,6 +151,7 @@ loader_LoadProcess(Handle *process, u64 prog_handle)
     u32 data_mem_size;
     u64 progid;
     u32 text_grow, data_grow, ro_grow;
+    u16 progver;
 
     openLogger();
 
@@ -178,6 +179,7 @@ loader_LoadProcess(Handle *process, u64 prog_handle)
 
     // load code
     progid = g_exheader.arm11systemlocalcaps.programid;
+    progver = g_exheader.codesetinfo.flags.remasterversion[0] + g_exheader.codesetinfo.flags.remasterversion[1] * 0x100;
 
     logu64(progid);
     logstr("  validated params\n");
@@ -206,9 +208,9 @@ loader_LoadProcess(Handle *process, u64 prog_handle)
     original_vaddr.total_size = original_vaddr.text_size + original_vaddr.ro_size + original_vaddr.data_size;
 
     // Allow changing code, ro, data sizes to allow adding code
-    text_grow = get_text_extend(progid, g_exheader.codesetinfo.text.codesize);
-    ro_grow = get_ro_extend(progid, g_exheader.codesetinfo.ro.codesize);
-    data_grow = get_data_extend(progid, g_exheader.codesetinfo.data.codesize);
+    text_grow = get_text_extend(progid, progver, g_exheader.codesetinfo.text.codesize);
+    ro_grow = get_ro_extend(progid, progver, g_exheader.codesetinfo.ro.codesize);
+    data_grow = get_data_extend(progid, progver, g_exheader.codesetinfo.data.codesize);
 
     // One page is 4096 bytes, thus all the 4095 constants.
 
@@ -226,7 +228,7 @@ loader_LoadProcess(Handle *process, u64 prog_handle)
         return res;
     }
 
-    if ((res = load_code(progid, &shared_addr, &original_vaddr, prog_handle, g_exheader.codesetinfo.flags.flag & 1)) >= 0) {
+    if ((res = load_code(progid, progver, &shared_addr, &original_vaddr, prog_handle, g_exheader.codesetinfo.flags.flag & 1)) >= 0) {
         memcpy(&codesetinfo.name, g_exheader.codesetinfo.name, 8);
         codesetinfo.program_id = progid;
         codesetinfo.text_addr = vaddr.text_addr;
index d11695f647c4a01b5f8ce7f367f16e76ee01dd32..a6c6396988b5361fb1525b265a145a6a286409da 100644 (file)
@@ -309,45 +309,45 @@ overlay_patch(u64 progId, u8 *code, u32 size)
 
 // This is only for the .data segment.
 void
-patch_data(u64 progId, u8 *data, u32 size, u32 orig_size)
+patch_data(u64 progId, u16 progver, u8 *data, u32 size, u32 orig_size)
 {
 }
 
 // This is only for the .ro segment.
 void
-patch_ro(u64 progId, u8 *ro, u32 size, u32 orig_size)
+patch_ro(u64 progId, u16 progver, u8 *ro, u32 size, u32 orig_size)
 {
 }
 
 // This is only for the .code segment.
 void
-patch_text(u64 progId, u8 *text, u32 size, u32 orig_size)
+patch_text(u64 progId, u16 progver, u8 *text, u32 size, u32 orig_size)
 {
     if (progId == 0x0004013000008002LL)
         adjust_cpu_settings(progId, text, orig_size);
 
-    execb(progId, text, orig_size);
+    execb(progId, progver, text, orig_size);
 
     language_emu(progId, text, orig_size);
 }
 
 // Gets how many bytes .text must be extended by for patches to fit.
 u32
-get_text_extend(u64 progId, u32 size_orig)
+get_text_extend(u64 progId, u16 progver, u32 size_orig)
 {
     return 0; // Stub - nothing needs this yet
 }
 
 // Gets how many bytes .ro must be extended.
 u32
-get_ro_extend(u64 progId, u32 size_orig)
+get_ro_extend(u64 progId, u16 progver, u32 size_orig)
 {
     return 0; // Stub - nothing needs this yet
 }
 
 // Again, same, but for .data.
 u32
-get_data_extend(u64 progId, u32 size_orig)
+get_data_extend(u64 progId, u16 progver, u32 size_orig)
 {
     return 0; // Stub - nothing needs this yet
 }
index 7f09e4594348da4d618378a09d63a0aa3de60f37..44a6223ba22400283be2a8f047da89ccd66d1145 100644 (file)
@@ -3,13 +3,13 @@
 
 #include <3ds/types.h>
 
-void patch_text(u64 progId, u8 *text, u32 size, u32 orig_size);
-void patch_data(u64 progId, u8 *data, u32 size, u32 orig_size);
-void patch_ro(u64 progId, u8 *ro, u32 size, u32 orig_size);
+void patch_text(u64 progId, u16 progver, u8 *text, u32 size, u32 orig_size);
+void patch_data(u64 progId, u16 progver, u8 *data, u32 size, u32 orig_size);
+void patch_ro(u64 progId, u16 progver, u8 *ro, u32 size, u32 orig_size);
 
-u32 get_text_extend(u64 progId, u32 size_orig);
-u32 get_ro_extend(u64 progId, u32 size_orig);
-u32 get_data_extend(u64 progId, u32 size_orig);
+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);
 
 void load_config();
 
similarity index 100%
rename from external/service/7b.s
rename to external/svc/7b.s
similarity index 67%
rename from external/service/README.md
rename to external/svc/README.md
index 5cb9cd37b731be5ab8d579cf7d11148eea1b7988..406932ed9fb0ed4a5cbb54adbd69cce514ac3423 100644 (file)
@@ -1,3 +1,3 @@
-This directory contains service fragments for ARM11. This includes svcbackdoor.
+This directory contains svc fragments for ARM11. This includes svcbackdoor.
 
 As easy as it would be to rely on armips (bleh) I've instead taken the harder route of clever gas and objcopy use. No armips will ever be required for this repo.
similarity index 100%
rename from external/service/stub.s
rename to external/svc/stub.s
index 2f882e15f6a6b4394b598c11df04d0c10e30d257..a963501c3315081466a08b0de7e03b73694d3404 100755 (executable)
@@ -1,6 +1,13 @@
 #!/usr/bin/env python2
 # -*- encoding: utf8 -*-
 
+# This assembler is very dumb and needs severe improvement.
+# Maybe I should rewrite it as an LLVM backend. That would
+# be much easier to maintain and far less hackish.
+
+# Either way, expect this code to change, a lot. The bytecode
+# format probably won't.
+
 import os
 import sys
 import re
@@ -164,8 +171,12 @@ def parse_op(token_list, instr_offs):
                if s != 2:
                        syn_err("invalid number of arguments")
 
-               # We cut corners and calculate stuff manually.
-               return bytearray.fromhex("09") + bytearray.fromhex(token_list[1])
+               return bytearray.fromhex("0C") + bytearray.fromhex(token_list[1])
+       elif token_list[0] == "ver":
+               if s != 2:
+                       syn_err("invalid number of arguments")
+
+               return bytearray.fromhex("0D") + bytearray.fromhex(token_list[1])
 
 def pad_zero_r(x, c):
        while len(x) < c:
@@ -217,6 +228,8 @@ with open(in_file, "r") as ins:
                                offsets += [size]
                                size += len(bytes)
 
+               offsets += [size+1] # So we can jump past the last instruction for 'exit' type behavior
+
                ins.seek(0)
 
                for line in ins:
index e9310446cbeff5dfd18c499adb02de83dcc71f23..f2fd67eae83582a8c8c265c895978aa32049c78a 100644 (file)
@@ -383,36 +383,36 @@ InitSD()
 #ifdef DATA32_SUPPORT
     *(volatile uint16_t *)0x10006100 |= 0x402u; // SDDATACTL32
 #else
-    *(volatile uint16_t *)0x10006100 |= 0x402u; // SDDATACTL32
+    *(volatile uint16_t *)0x10006100 |= 0x402u;  // SDDATACTL32
 #endif
     *(volatile uint16_t *)0x100060D8 = (*(volatile uint16_t *)0x100060D8 & 0xFFDD) | 2;
 #ifdef DATA32_SUPPORT
     *(volatile uint16_t *)0x10006100 &= 0xFFFFu; // SDDATACTL32
     *(volatile uint16_t *)0x100060D8 &= 0xFFDFu; // SDDATACTL
-    *(volatile uint16_t *)0x10006104 = 512; // SDBLKLEN32
+    *(volatile uint16_t *)0x10006104 = 512;      // SDBLKLEN32
 #else
     *(volatile uint16_t *)0x10006100 &= 0xFFFDu; // SDDATACTL32
     *(volatile uint16_t *)0x100060D8 &= 0xFFDDu; // SDDATACTL
-    *(volatile uint16_t *)0x10006104 = 0; // SDBLKLEN32
+    *(volatile uint16_t *)0x10006104 = 0;        // SDBLKLEN32
 #endif
-    *(volatile uint16_t *)0x10006108 = 1; // SDBLKCOUNT32
-    *(volatile uint16_t *)0x100060E0 &= 0xFFFEu; // SDRESET
-    *(volatile uint16_t *)0x100060E0 |= 1u; // SDRESET
-    *(volatile uint16_t *)0x10006020 |= TMIO_MASK_ALL; // SDIR_MASK0
+    *(volatile uint16_t *)0x10006108 = 1;                    // SDBLKCOUNT32
+    *(volatile uint16_t *)0x100060E0 &= 0xFFFEu;             // SDRESET
+    *(volatile uint16_t *)0x100060E0 |= 1u;                  // SDRESET
+    *(volatile uint16_t *)0x10006020 |= TMIO_MASK_ALL;       // SDIR_MASK0
     *(volatile uint16_t *)0x10006022 |= TMIO_MASK_ALL >> 16; // SDIR_MASK1
-    *(volatile uint16_t *)0x100060FC |= 0xDBu; // SDCTL_RESERVED7
-    *(volatile uint16_t *)0x100060FE |= 0xDBu; // SDCTL_RESERVED8
-    *(volatile uint16_t *)0x10006002 &= 0xFFFCu; // SDPORTSEL
+    *(volatile uint16_t *)0x100060FC |= 0xDBu;               // SDCTL_RESERVED7
+    *(volatile uint16_t *)0x100060FE |= 0xDBu;               // SDCTL_RESERVED8
+    *(volatile uint16_t *)0x10006002 &= 0xFFFCu;             // SDPORTSEL
 #ifdef DATA32_SUPPORT
     *(volatile uint16_t *)0x10006024 = 0x20;
     *(volatile uint16_t *)0x10006028 = 0x40EE;
 #else
-    *(volatile uint16_t *)0x10006024 = 0x40; // Nintendo sets this to 0x20
-    *(volatile uint16_t *)0x10006028 = 0x40EB; // Nintendo sets this to 0x40EE
+    *(volatile uint16_t *)0x10006024 = 0x40;     // Nintendo sets this to 0x20
+    *(volatile uint16_t *)0x10006028 = 0x40EB;   // Nintendo sets this to 0x40EE
 #endif
     *(volatile uint16_t *)0x10006002 &= 0xFFFCu; ////SDPORTSEL
-    *(volatile uint16_t *)0x10006026 = 512; // SDBLKLEN
-    *(volatile uint16_t *)0x10006008 = 0; // SDSTOP
+    *(volatile uint16_t *)0x10006026 = 512;      // SDBLKLEN
+    *(volatile uint16_t *)0x10006008 = 0;        // SDSTOP
 
     inittarget(&handelSD);
 }
index 367445703363c3506dbfcc699b67e6a40a6a9629..98d24c678174a7c8b9b75d8be3a55c8e8415057c 100644 (file)
@@ -360,8 +360,8 @@ load_firms()
         fprintf(BOTTOM_SCREEN, "]\n  TWL_FIRM failed to load.\n");
         state = 1;
     } else {
-        fprintf(stderr, "]\n");
         find_proc9(twl_firm_loc, &twl_firm_proc9, &twl_firm_p9_exefs);
+        fprintf(stderr, "]\n");
     }
 
     fprintf(BOTTOM_SCREEN, "AGB_FIRM\n  [");
@@ -369,8 +369,8 @@ load_firms()
         fprintf(BOTTOM_SCREEN, "]\n  AGB_FIRM failed to load.\n");
         state = 1;
     } else {
-        fprintf(stderr, "]\n");
         find_proc9(agb_firm_loc, &agb_firm_proc9, &agb_firm_p9_exefs);
+        fprintf(stderr, "]\n");
     }
 
     firm_loaded = 1; // Loaded.
index 646071691c888a708c117d903b6063bea68e6714..ad1fb77198a526a4693252d5033ab75f28891970 100644 (file)
@@ -21,6 +21,7 @@
 #define OP_OR 0x0A
 #define OP_XOR 0x0B
 #define OP_NOT 0x0C
+#define OP_VER 0x0D
 
 #define OP_NEXT 0xFF
 
@@ -46,7 +47,7 @@ struct mode modes[19];
 int init_bytecode = 0;
 
 int
-exec_bytecode(uint8_t *bytecode, uint32_t len, int debug)
+exec_bytecode(uint8_t *bytecode, uint16_t ver, uint32_t len, int debug)
 {
     if (!init_bytecode) {
 #ifndef LOADER
@@ -298,6 +299,19 @@ exec_bytecode(uint8_t *bytecode, uint32_t len, int debug)
                 }
                 code += 2;
                 break;
+            case OP_VER:
+                if (debug)
+                    log("ver\n");
+                code++;
+                if (!test_was_false) {
+                    if (*(uint16_t *)code != ver) {
+                        test_was_false = 1;
+                    }
+                } else {
+                    test_was_false = 0;
+                }
+                code += 2;
+                break;
             case OP_NEXT:
                 if (debug)
                     log("next\n");
@@ -337,12 +351,13 @@ exec_bytecode(uint8_t *bytecode, uint32_t len, int debug)
 
 #ifdef LOADER
 int
-execb(uint64_t tid, uint8_t *search_mem, uint32_t search_len)
+execb(uint64_t tid, uint16_t ver, uint8_t *search_mem, uint32_t search_len)
 {
 #else
 int
 execb(char *filename, int build_cache)
 {
+    uint16_t ver = 0; // FIXME - Provide native_firm version
 #endif
     uint32_t patch_len;
 #ifdef LOADER
@@ -480,7 +495,6 @@ execb(char *filename, int build_cache)
         patch_mem = (uint8_t *)FCRAM_PATCH_LOC;
         patch_len = len;
     }
-
 #endif
 
     int debug = 0;
@@ -490,5 +504,5 @@ execb(char *filename, int build_cache)
     }
 #endif
 
-    return exec_bytecode(patch_mem, patch_len, debug);
+    return exec_bytecode(patch_mem, ver, patch_len, debug);
 }
index 9c38441b40460e8a654beed7755498a1d398a592..b2f76a3ae7a603f63b5b5ccf759bb03b70a69925 100644 (file)
 #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 "/service"   // Service code location.
+#define PATH_SERVICES 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.
 #define PATH_KEYS PATH_CFW "/keys"            // Keyfiles will be loaded from this dir, and
                                               // additionally the root if not found.
-#define PATH_EXEFS PATH_CFW "/exe"            // ExeFS overrides, named like '<titleid>.exefs'
+#define PATH_EXEFS PATH_CFW "/exe"            // ExeFS overrides, named like '<titleid>.exefs' - NYI
 
 #define PATH_NATIVE_F PATH_FIRMWARES "/native"
 #define PATH_AGB_F PATH_FIRMWARES "/agb"