From 67e229e5a92095d656492b2ba9b9b616cb49ccd3 Mon Sep 17 00:00:00 2001 From: Aurora Date: Mon, 18 Apr 2016 20:50:52 +0200 Subject: [PATCH] Cleanup of the injector --- injector/Makefile | 81 +++++------------------------------ injector/source/ifile.c | 41 +----------------- injector/source/ifile.h | 3 +- injector/source/loader.c | 3 +- injector/source/memory.c | 10 +++++ injector/source/memory.h | 5 +++ injector/source/patcher.c | 90 ++++++++++++++++++++++----------------- 7 files changed, 81 insertions(+), 152 deletions(-) create mode 100644 injector/source/memory.c create mode 100644 injector/source/memory.h diff --git a/injector/Makefile b/injector/Makefile index b6afb45..62744a2 100644 --- a/injector/Makefile +++ b/injector/Makefile @@ -7,7 +7,6 @@ $(error "Please set DEVKITARM in your environment. export DEVKITARM=dev endif TOPDIR ?= $(CURDIR) -MAKEROM ?= makerom include $(DEVKITARM)/3ds_rules #--------------------------------------------------------------------------------- @@ -20,23 +19,16 @@ include $(DEVKITARM)/3ds_rules TARGET := $(notdir $(CURDIR)) BUILD := build SOURCES := source -DATA := data -INCLUDES := include #--------------------------------------------------------------------------------- # options for code generation #--------------------------------------------------------------------------------- ARCH := -march=armv6k -mtune=mpcore -mfloat-abi=hard -mtp=soft -CFLAGS := -flto -Wall -O2 -mword-relocations \ - -ffast-math -ffunction-sections -fdata-sections \ - $(ARCH) +CFLAGS := -flto -Wall -O2 -mword-relocations \ + -ffast-math -fno-builtin -ffunction-sections -fdata-sections \ + $(ARCH) $(INCLUDE) -DARM11 -D_3DS -CFLAGS += $(INCLUDE) -DARM11 -D_3DS - -CXXFLAGS := $(CFLAGS) -fno-rtti -fno-exceptions -std=gnu99 - -ASFLAGS := $(ARCH) LDFLAGS = -flto -Xlinker --defsym="__start__=0x14000000" -specs=3dsx.specs $(ARCH) -Wl,-Map,$(notdir $*.map) LIBS := -lctru @@ -58,38 +50,17 @@ ifneq ($(BUILD),$(notdir $(CURDIR))) export OUTPUT := $(CURDIR)/$(TARGET) export TOPDIR := $(CURDIR) -export VPATH := $(foreach dir,$(SOURCES),$(CURDIR)/$(dir)) \ - $(foreach dir,$(DATA),$(CURDIR)/$(dir)) +export VPATH := $(foreach dir,$(SOURCES),$(CURDIR)/$(dir)) export DEPSDIR := $(CURDIR)/$(BUILD) CFILES := $(foreach dir,$(SOURCES),$(notdir $(wildcard $(dir)/*.c))) -CPPFILES := $(foreach dir,$(SOURCES),$(notdir $(wildcard $(dir)/*.cpp))) -SFILES := $(foreach dir,$(SOURCES),$(notdir $(wildcard $(dir)/*.s))) -PICAFILES := $(foreach dir,$(SOURCES),$(notdir $(wildcard $(dir)/*.v.pica))) -SHLISTFILES := $(foreach dir,$(SOURCES),$(notdir $(wildcard $(dir)/*.shlist))) -BINFILES := $(foreach dir,$(DATA),$(notdir $(wildcard $(dir)/*.*))) -#--------------------------------------------------------------------------------- -# use CXX for linking C++ projects, CC for standard C -#--------------------------------------------------------------------------------- -ifeq ($(strip $(CPPFILES)),) -#--------------------------------------------------------------------------------- - export LD := $(CC) -#--------------------------------------------------------------------------------- -else -#--------------------------------------------------------------------------------- - export LD := $(CXX) -#--------------------------------------------------------------------------------- -endif -#--------------------------------------------------------------------------------- +export LD := $(CC) -export OFILES := $(addsuffix .o,$(BINFILES)) \ - $(PICAFILES:.v.pica=.shbin.o) $(SHLISTFILES:.shlist=.shbin.o) \ - $(CPPFILES:.cpp=.o) $(CFILES:.c=.o) $(SFILES:.s=.o) +export OFILES := $(CFILES:.c=.o) -export INCLUDE := $(foreach dir,$(INCLUDES),-I$(CURDIR)/$(dir)) \ - $(foreach dir,$(LIBDIRS),-I$(dir)/include) \ +export INCLUDE := $(foreach dir,$(LIBDIRS),-I$(dir)/include) \ -I$(CURDIR)/$(BUILD) export LIBPATHS := $(foreach dir,$(LIBDIRS),-L$(dir)/lib) @@ -106,7 +77,7 @@ $(BUILD): #--------------------------------------------------------------------------------- clean: @echo clean ... - @rm -fr $(BUILD) $(OUTPUT).cxi $(TARGET).elf + @rm -fr $(BUILD) $(TARGET).elf #--------------------------------------------------------------------------------- @@ -118,42 +89,12 @@ DEPENDS := $(OFILES:.o=.d) # main targets #--------------------------------------------------------------------------------- $(OUTPUT).cxi : $(OUTPUT).elf - $(MAKEROM) -f ncch -rsf ../loader.rsf -nocodepadding -o $@ -elf $< + @makerom -f ncch -rsf ../loader.rsf -nocodepadding -o $@ -elf $< + +memory.o : CFLAGS += -O3 $(OUTPUT).elf : $(OFILES) -#--------------------------------------------------------------------------------- -# you need a rule like this for each extension you use as binary data -#--------------------------------------------------------------------------------- -%.bin.o : %.bin -#--------------------------------------------------------------------------------- - @echo $(notdir $<) - @$(bin2o) - -#--------------------------------------------------------------------------------- -# rules for assembling GPU shaders -#--------------------------------------------------------------------------------- -define shader-as - $(eval CURBIN := $(patsubst %.shbin.o,%.shbin,$(notdir $@))) - picasso -o $(CURBIN) $1 - bin2s $(CURBIN) | $(AS) -o $@ - echo "extern const u8" `(echo $(CURBIN) | sed -e 's/^\([0-9]\)/_\1/' | tr . _)`"_end[];" > `(echo $(CURBIN) | tr . _)`.h - echo "extern const u8" `(echo $(CURBIN) | sed -e 's/^\([0-9]\)/_\1/' | tr . _)`"[];" >> `(echo $(CURBIN) | tr . _)`.h - echo "extern const u32" `(echo $(CURBIN) | sed -e 's/^\([0-9]\)/_\1/' | tr . _)`_size";" >> `(echo $(CURBIN) | tr . _)`.h -endef - -%.shbin.o : %.v.pica %.g.pica - @echo $(notdir $^) - @$(call shader-as,$^) - -%.shbin.o : %.v.pica - @echo $(notdir $<) - @$(call shader-as,$<) - -%.shbin.o : %.shlist - @echo $(notdir $<) - @$(call shader-as,$(foreach file,$(shell cat $<),$(dir $<)/$(file))) - -include $(DEPENDS) #--------------------------------------------------------------------------------------- diff --git a/injector/source/ifile.c b/injector/source/ifile.c index e360abc..5faf2ae 100644 --- a/injector/source/ifile.c +++ b/injector/source/ifile.c @@ -63,43 +63,4 @@ Result IFile_Read(IFile *file, u64 *total, void *buffer, u32 len) *total = cur; return res; -} - -Result IFile_Write(IFile *file, u64 *total, void *buffer, u32 len, u32 flags) -{ - u32 written; - u32 left; - char *buf; - u64 cur; - Result res; - - if (len == 0) - { - *total = 0; - return 0; - } - - buf = (char *)buffer; - cur = 0; - left = len; - while (1) - { - res = FSFILE_Write(file->handle, &written, file->pos, buf, left, flags); - if (R_FAILED(res)) - { - break; - } - - cur += written; - file->pos += written; - if (written == left) - { - break; - } - buf += written; - left -= written; - } - - *total = cur; - return res; -} +} \ No newline at end of file diff --git a/injector/source/ifile.h b/injector/source/ifile.h index b60e180..bf2ae7e 100644 --- a/injector/source/ifile.h +++ b/injector/source/ifile.h @@ -12,5 +12,4 @@ typedef struct Result IFile_Open(IFile *file, FS_Archive archive, FS_Path path, u32 flags); Result IFile_Close(IFile *file); Result IFile_GetSize(IFile *file, u64 *size); -Result IFile_Read(IFile *file, u64 *total, void *buffer, u32 len); -Result IFile_Write(IFile *file, u64 *total, void *buffer, u32 len, u32 flags); +Result IFile_Read(IFile *file, u64 *total, void *buffer, u32 len); \ No newline at end of file diff --git a/injector/source/loader.c b/injector/source/loader.c index 0834771..bf663a7 100644 --- a/injector/source/loader.c +++ b/injector/source/loader.c @@ -1,6 +1,5 @@ #include <3ds.h> -#include -#include +#include "memory.h" #include "patcher.h" #include "exheader.h" #include "ifile.h" diff --git a/injector/source/memory.c b/injector/source/memory.c new file mode 100644 index 0000000..4f4c96a --- /dev/null +++ b/injector/source/memory.c @@ -0,0 +1,10 @@ +#include "memory.h" + +void memcpy(void *dest, const void *src, u32 size) +{ + u8 *destc = (u8 *)dest; + const u8 *srcc = (const u8 *)src; + + for(u32 i = 0; i < size; i++) + destc[i] = srcc[i]; +} \ No newline at end of file diff --git a/injector/source/memory.h b/injector/source/memory.h new file mode 100644 index 0000000..c1e1609 --- /dev/null +++ b/injector/source/memory.h @@ -0,0 +1,5 @@ +#pragma once + +#include <3ds/types.h> + +void memcpy(void *dest, const void *src, u32 size); \ No newline at end of file diff --git a/injector/source/patcher.c b/injector/source/patcher.c index a3a5b82..f2900bc 100644 --- a/injector/source/patcher.c +++ b/injector/source/patcher.c @@ -1,17 +1,28 @@ #include <3ds.h> -#include +#include "memory.h" #include "patcher.h" #include "ifile.h" #ifndef PATH_MAX #define PATH_MAX 255 -#define CONFIG(a) ((config >> (a + 16)) & 1) -#define MULTICONFIG(a) ((config >> (a * 2 + 6)) & 3) -#define BOOTCONFIG(a, b) ((config >> a) & b) +#define CONFIG(a) ((loadConfig() >> (a + 16)) & 1) +#define MULTICONFIG(a) ((loadConfig() >> (a * 2 + 6)) & 3) +#define BOOTCONFIG(a, b) ((loadConfig() >> a) & b) #endif -static u32 config = 0; -static u8 secureInfo[0x111] = {0}; +static int memcmp(const void *buf1, const void *buf2, u32 size) +{ + const u8 *buf1c = (const u8 *)buf1; + const u8 *buf2c = (const u8 *)buf2; + + for(u32 i = 0; i < size; i++) + { + int cmp = buf1c[i] - buf2c[i]; + if(cmp) return cmp; + } + + return 0; +} //Quick Search algorithm, adapted from http://igm.univ-mlv.fr/~lecroq/string/node19.html#SECTION00190 static u8 *memsearch(u8 *startPos, const void *pattern, u32 size, u32 patternSize) @@ -62,6 +73,15 @@ static u32 patchMemory(u8 *start, u32 size, const void *pattern, u32 patSize, in return i; } +static inline size_t strnlen (const char *string, size_t maxlen) +{ + size_t size; + + for(size = 0; *string && size < maxlen; string++, size++); + + return size; +} + static int fileOpen(IFile *file, FS_ArchiveID id, const char *path, int flags) { FS_Archive archive; @@ -79,43 +99,39 @@ static int fileOpen(IFile *file, FS_ArchiveID id, const char *path, int flags) return IFile_Open(file, archive, ppath, flags); } -static int loadSecureInfo(void) +static u32 secureInfoExists(void) { - if(secureInfo[0] == 0xFF) - return 0; + static u32 secureInfoExists = 0; - IFile file; - Result ret = fileOpen(&file, ARCHIVE_NAND_RW, "/sys/SecureInfo_C", FS_OPEN_READ); - if(R_SUCCEEDED(ret)) + if(!secureInfoExists) { - u64 total; - - ret = IFile_Read(&file, &total, secureInfo, 0x111); - IFile_Close(&file); - if(R_SUCCEEDED(ret) && total == 0x111) - secureInfo[0] = 0xFF; + IFile file; + if(R_SUCCEEDED(fileOpen(&file, ARCHIVE_NAND_RW, "/sys/SecureInfo_C", FS_OPEN_READ))) + { + secureInfoExists = 1; + IFile_Close(&file); + } } - return ret; + return secureInfoExists; } -static int loadConfig(void) +static u32 loadConfig(void) { - if(config) - return 0; + static u32 config = 0; - IFile file; - Result ret = fileOpen(&file, ARCHIVE_SDMC, "/aurei/config.bin", FS_OPEN_READ); - if(R_SUCCEEDED(ret)) + if(!config) { - u64 total; - - ret = IFile_Read(&file, &total, &config, 4); - IFile_Close(&file); - if(R_SUCCEEDED(ret)) config |= 1 << 4; + IFile file; + if(R_SUCCEEDED(fileOpen(&file, ARCHIVE_SDMC, "/aurei/config.bin", FS_OPEN_READ))) + { + u64 total; + if(R_SUCCEEDED(IFile_Read(&file, &total, &config, 4))) config |= 1 << 4; + IFile_Close(&file); + } } - return ret; + return config; } static int loadTitleLocaleConfig(u64 progId, u8 *regionId, u8 *languageId) @@ -372,7 +388,7 @@ void patchCode(u64 progId, u8 *code, u32 size) case 0x0004001000027000LL: // KOR MSET case 0x0004001000028000LL: // TWN MSET { - if(R_SUCCEEDED(loadConfig()) && CONFIG(6)) + if(CONFIG(6)) { static const u16 verPattern[] = u"Ver."; const u32 currentNand = BOOTCONFIG(0, 3); @@ -408,7 +424,7 @@ void patchCode(u64 progId, u8 *code, u32 size) sizeof(stopCartUpdatesPatch), 2 ); - if(R_SUCCEEDED(loadConfig()) && MULTICONFIG(1)) + if(MULTICONFIG(1)) { static const u8 cfgN3dsCpuPattern[] = { 0x40, 0xA0, 0xE1, 0x07, 0x00 @@ -444,7 +460,7 @@ void patchCode(u64 progId, u8 *code, u32 size) sizeof(secureinfoSigCheckPatch), 1 ); - if(R_SUCCEEDED(loadSecureInfo())) + if(secureInfoExists()) { static const u16 secureinfoFilenamePattern[] = u"SecureInfo_"; static const u16 secureinfoFilenamePatch[] = u"C"; @@ -463,7 +479,7 @@ void patchCode(u64 progId, u8 *code, u32 size) } default: - if(R_SUCCEEDED(loadConfig()) && CONFIG(4)) + if(CONFIG(4)) { u32 tidHigh = (progId & 0xFFFFFFF000000000LL) >> 0x24; @@ -473,9 +489,7 @@ void patchCode(u64 progId, u8 *code, u32 size) u8 regionId = 0xFF, languageId = 0xFF; - int ret = loadTitleLocaleConfig(progId, ®ionId, &languageId); - - if(R_SUCCEEDED(ret)) + if(R_SUCCEEDED(loadTitleLocaleConfig(progId, ®ionId, &languageId))) { u32 CFGUHandleOffset;