From ec1f149fc83c2e6554f414279bd224fb3b479ada Mon Sep 17 00:00:00 2001 From: ProjectRevoTPP Date: Fri, 17 Apr 2020 13:38:38 -0400 Subject: add metrowerk assembler patcher and automate it during the build. --- Makefile | 21 ++++++++++ tools/Makefile | 4 ++ tools/mwasmarm_patcher/mwasmarm_patcher.c | 70 +++++++++++++++++++++++++++++++ 3 files changed, 95 insertions(+) create mode 100644 tools/Makefile create mode 100644 tools/mwasmarm_patcher/mwasmarm_patcher.c diff --git a/Makefile b/Makefile index fac3e52f..06087477 100644 --- a/Makefile +++ b/Makefile @@ -4,6 +4,12 @@ default: all +ifeq ($(OS),Windows_NT) +EXE := .exe +else +EXE := +endif + ################ Target Executable and Sources ############### BUILD_DIR := build @@ -24,6 +30,14 @@ S_FILES := $(foreach dir,$(ASM_DIRS),$(wildcard $(dir)/*.s)) # Object files O_FILES := $(foreach file,$(C_FILES),$(BUILD_DIR)/$(file:.c=.o)) \ $(foreach file,$(S_FILES),$(BUILD_DIR)/$(file:.s=.o)) \ + +################### Universal Dependencies ################### + +# Make tools if out of date +DUMMY != make -s -C tools >&2 || echo FAIL +ifeq ($(DUMMY),FAIL) + $(error Failed to build tools) +endif ##################### Compiler Options ####################### @@ -57,10 +71,17 @@ CFLAGS = -O4,p -proc v5te -thumb -fp soft -lang c -Cpp_exceptions off -interwork # DS TOOLS TOOLS_DIR = tools SHA1SUM = sha1sum +MWASMARM_PATCHER = tools/mwasmarm_patcher/mwasmarm_patcher$(EXE) + +DUMMY != $(MWASMARM_PATCHER) $(MWASMARM) || echo FAIL +ifeq ($(DUMMY),FAIL) + $(error MWASMARM patcher returned an error) +endif ######################### Targets ########################### all: $(ROM) + $(info Test) @$(SHA1SUM) -c $(TARGET).sha1 clean: diff --git a/tools/Makefile b/tools/Makefile new file mode 100644 index 00000000..5a9eeb69 --- /dev/null +++ b/tools/Makefile @@ -0,0 +1,4 @@ +all: mwasmarm_patcher + +mwasmarm_patcher: mwasmarm_patcher/mwasmarm_patcher.c + gcc mwasmarm_patcher/mwasmarm_patcher.c -o mwasmarm_patcher/mwasmarm_patcher -lssl -lcrypto diff --git a/tools/mwasmarm_patcher/mwasmarm_patcher.c b/tools/mwasmarm_patcher/mwasmarm_patcher.c new file mode 100644 index 00000000..0d7470df --- /dev/null +++ b/tools/mwasmarm_patcher/mwasmarm_patcher.c @@ -0,0 +1,70 @@ +#include +#include +#include +#include +#include +#include + +// mwasmarm patcher v1.0 +// Patches the Metrowerk C compiler assembler to stop the line ending bug. + +char mwasmarm_unpatched_sha1[] = "9d63877c776245129b4727b41d3e9e63cfc9cd28"; +char mwasmarm_patched_sha1[] = "f5dea73bf90791e104cb59458bebae8b08a55484"; + +void fatal_printf(char *str, ...) { + va_list args; + va_start(args, str); + vprintf(str, args); + va_end(args); + exit(1); +} + +// return size in bytes +int get_file_size (FILE * fp) { + int curpos = ftell(fp); + fseek(fp, 0, SEEK_END); + int result = ftell(fp); + fseek(fp, curpos, SEEK_SET); + return result; +} + +void print_help(void) { + printf("mwasmarm patcher usage: input (example: mwasmarm_patcher mwasmarm.exe)\n"); +} + +int main(int argc, char *argv[]) { + unsigned char temp[SHA_DIGEST_LENGTH]; + char buf[SHA_DIGEST_LENGTH*2]; + + if (argc != 2) { + print_help(); + } else { + // Open the file and sha1 read it. + FILE *f = fopen(argv[1], "rb+"); + if(f == NULL) { + fatal_printf("ERROR: No file detected\n"); + } + int fsize = get_file_size(f); + char *string = malloc(fsize + 1); + fread(string, 1, fsize, f); + + // Check if sha1 matches either known assembler hashes. + SHA1(string, fsize, temp); + + for (int i=0; i < SHA_DIGEST_LENGTH; i++) { + sprintf((char*)&(buf[i*2]), "%02x", temp[i]); + } + + if(!strcmp(buf, mwasmarm_unpatched_sha1)) { + // Unpatched, perform the patch. + fseek(f, 0x57644, SEEK_SET); + fputc(0x05, f); + printf("Supported unpatched version detected: assembler patched\n"); + } else if(!strcmp(buf, mwasmarm_patched_sha1)) { + printf("Supported patched version detected: no action needed\n"); + } else { + fatal_printf("ERROR: Unsupported mwasmarm.exe version\n"); + } + } + return 0; +} -- cgit v1.2.3