From a9570cef144e672714d487868d2aa882367c5c05 Mon Sep 17 00:00:00 2001 From: Aurora Date: Tue, 6 Sep 2016 14:09:29 +0200 Subject: [PATCH 1/2] Minor stuff --- injector/Makefile | 2 +- injector/source/memory.c | 40 +++++++++++++++++++++++++ injector/source/memory.h | 4 ++- injector/source/patcher.c | 62 ++------------------------------------- injector/source/strings.c | 20 +++++++++++++ injector/source/strings.h | 6 ++++ patches/svcGetCFWInfo.s | 1 - source/memory.c | 6 ++-- 8 files changed, 75 insertions(+), 66 deletions(-) create mode 100644 injector/source/strings.c create mode 100644 injector/source/strings.h diff --git a/injector/Makefile b/injector/Makefile index 6fa5844..e733480 100755 --- a/injector/Makefile +++ b/injector/Makefile @@ -43,7 +43,7 @@ clean: $(dir_build)/$(name).elf: $(objects) $(LINK.o) $(OUTPUT_OPTION) $^ $(LIBPATHS) $(LIBS) -$(dir_build)/memory.o : CFLAGS += -O3 +$(dir_build)/memory.o $(dir_build)/strings.o: CFLAGS += -O3 $(dir_build)/%.o: $(dir_source)/%.c @mkdir -p "$(@D)" diff --git a/injector/source/memory.c b/injector/source/memory.c index 4f4c96a..8c36f5f 100644 --- a/injector/source/memory.c +++ b/injector/source/memory.c @@ -7,4 +7,44 @@ void memcpy(void *dest, const void *src, u32 size) for(u32 i = 0; i < size; i++) destc[i] = srcc[i]; +} + +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 +u8 *memsearch(u8 *startPos, const void *pattern, u32 size, u32 patternSize) +{ + const u8 *patternc = (const u8 *)pattern; + + //Preprocessing + u32 table[256]; + + for(u32 i = 0; i < 256; i++) + table[i] = patternSize + 1; + for(u32 i = 0; i < patternSize; i++) + table[patternc[i]] = patternSize - i; + + //Searching + u32 j = 0; + + while(j <= size - patternSize) + { + if(memcmp(pattern, startPos + j, patternSize) == 0) + return startPos + j; + j += table[startPos[j + patternSize]]; + } + + return NULL; } \ No newline at end of file diff --git a/injector/source/memory.h b/injector/source/memory.h index c1e1609..b7cf593 100644 --- a/injector/source/memory.h +++ b/injector/source/memory.h @@ -2,4 +2,6 @@ #include <3ds/types.h> -void memcpy(void *dest, const void *src, u32 size); \ No newline at end of file +void memcpy(void *dest, const void *src, u32 size); +int memcmp(const void *buf1, const void *buf2, u32 size); +u8 *memsearch(u8 *startPos, const void *pattern, u32 size, u32 patternSize); \ No newline at end of file diff --git a/injector/source/patcher.c b/injector/source/patcher.c index 636e6ad..9c69eb4 100644 --- a/injector/source/patcher.c +++ b/injector/source/patcher.c @@ -1,51 +1,12 @@ #include <3ds.h> -#include "memory.h" #include "patcher.h" +#include "memory.h" +#include "strings.h" #include "ifile.h" #include "CFWInfo.h" static CFWInfo info; -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) -{ - const u8 *patternc = (const u8 *)pattern; - - //Preprocessing - u32 table[256]; - - for(u32 i = 0; i < 256; ++i) - table[i] = patternSize + 1; - for(u32 i = 0; i < patternSize; ++i) - table[patternc[i]] = patternSize - i; - - //Searching - u32 j = 0; - - while(j <= size - patternSize) - { - if(memcmp(patternc, startPos + j, patternSize) == 0) - return startPos + j; - j += table[startPos[j + patternSize]]; - } - - return NULL; -} - static void patchMemory(u8 *start, u32 size, const void *pattern, u32 patSize, int offset, const void *replace, u32 repSize, u32 count) { for(u32 i = 0; i < count; i++) @@ -65,15 +26,6 @@ static void patchMemory(u8 *start, u32 size, const void *pattern, u32 patSize, 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 archiveId, const char *path, int flags) { FS_Path filePath = {PATH_ASCII, strnlen(path, PATH_MAX) + 1, path}, @@ -115,16 +67,6 @@ static bool secureInfoExists(void) return exists; } -static void progIdToStr(char *strEnd, u64 progId) -{ - while(progId) - { - static const char hexDigits[] = "0123456789ABCDEF"; - *strEnd-- = hexDigits[(u32)(progId & 0xF)]; - progId >>= 4; - } -} - static void loadTitleCodeSection(u64 progId, u8 *code, u32 size) { /* Here we look for "/luma/code_sections/[u64 titleID in hex, uppercase].bin" diff --git a/injector/source/strings.c b/injector/source/strings.c new file mode 100644 index 0000000..4f34404 --- /dev/null +++ b/injector/source/strings.c @@ -0,0 +1,20 @@ +#include "strings.h" + +size_t strnlen(const char *string, size_t maxlen) +{ + size_t size; + + for(size = 0; *string && size < maxlen; string++, size++); + + return size; +} + +void progIdToStr(char *strEnd, u64 progId) +{ + while(progId) + { + static const char hexDigits[] = "0123456789ABCDEF"; + *strEnd-- = hexDigits[(u32)(progId & 0xF)]; + progId >>= 4; + } +} \ No newline at end of file diff --git a/injector/source/strings.h b/injector/source/strings.h new file mode 100644 index 0000000..6599118 --- /dev/null +++ b/injector/source/strings.h @@ -0,0 +1,6 @@ +#pragma once + +#include <3ds/types.h> + +size_t strnlen(const char *string, size_t maxlen); +void progIdToStr(char *strEnd, u64 progId); \ No newline at end of file diff --git a/patches/svcGetCFWInfo.s b/patches/svcGetCFWInfo.s index 945cb58..3950ead 100644 --- a/patches/svcGetCFWInfo.s +++ b/patches/svcGetCFWInfo.s @@ -23,7 +23,6 @@ .arm.little .create "build/svcGetCFWInfo.bin", 0 - .arm adr r1, infoStart diff --git a/source/memory.c b/source/memory.c index 03a36ae..48d4060 100644 --- a/source/memory.c +++ b/source/memory.c @@ -66,9 +66,9 @@ u8 *memsearch(u8 *startPos, const void *pattern, u32 size, u32 patternSize) //Preprocessing u32 table[256]; - for(u32 i = 0; i < 256; ++i) + for(u32 i = 0; i < 256; i++) table[i] = patternSize + 1; - for(u32 i = 0; i < patternSize; ++i) + for(u32 i = 0; i < patternSize; i++) table[patternc[i]] = patternSize - i; //Searching @@ -76,7 +76,7 @@ u8 *memsearch(u8 *startPos, const void *pattern, u32 size, u32 patternSize) while(j <= size - patternSize) { - if(memcmp(patternc, startPos + j, patternSize) == 0) + if(memcmp(pattern, startPos + j, patternSize) == 0) return startPos + j; j += table[startPos[j + patternSize]]; } From d3d315784cde592f64273f99d3bf8a423b9ed0e4 Mon Sep 17 00:00:00 2001 From: Aurora Date: Tue, 6 Sep 2016 15:52:08 +0200 Subject: [PATCH 2/2] Fix derp --- source/emunand.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/source/emunand.c b/source/emunand.c index d34d6c3..9d31e40 100644 --- a/source/emunand.c +++ b/source/emunand.c @@ -58,7 +58,7 @@ void locateEmuNand(u32 *off, u32 *head, FirmwareSource *emuNand) } } -static inline void *getFreeK9Space(u8 *pos, u32 size) +static inline u8 *getFreeK9Space(u8 *pos, u32 size) { const u8 pattern[] = {0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0x00}; @@ -108,7 +108,7 @@ static inline void patchMpu(u8 *pos, u32 size) void patchEmuNand(u8 *arm9Section, u32 arm9SectionSize, u8 *process9Offset, u32 process9Size, u32 emuHeader, u32 branchAdditive) { //Copy emuNAND code - void *freeK9Space = getFreeK9Space(arm9Section, arm9SectionSize); + u8 *freeK9Space = getFreeK9Space(arm9Section, arm9SectionSize); memcpy(freeK9Space, emunand, emunand_size); //Add the data of the found emuNAND