From 096c8a89b7cbbae4e4e92ed06e58718a3d87944d Mon Sep 17 00:00:00 2001 From: Phillip Stephens Date: Thu, 6 Oct 2022 14:24:10 -0700 Subject: [PATCH] Improve metroidbuildinfo, use file instead of string passed from command line Former-commit-id: cca1bc81f5b4eeffd8d248a704cfe141ba074305 --- Makefile | 2 ++ asm/MetroidPrime/CFrontEndUI.s | 1 + tools/Makefile | 7 +++++-- tools/metroidbuildinfo.c | 29 +++++++++++++++++++---------- 4 files changed, 27 insertions(+), 12 deletions(-) diff --git a/Makefile b/Makefile index 70953a06..b67036b0 100644 --- a/Makefile +++ b/Makefile @@ -94,6 +94,7 @@ endif CC = $(WINE) tools/mwcc_compiler/$(MWCC_VERSION)/mwcceppc.exe LD := $(WINE) tools/mwcc_compiler/$(MWLD_VERSION)/mwldeppc.exe ELF2DOL := tools/elf2dol +METROIDBUILDINFO := tools/metroidbuildinfo TRANSFORM_DEP := tools/transform-dep.py FRANK := tools/franklite.py @@ -163,6 +164,7 @@ $(LDSCRIPT): ldscript.lcf $(DOL): $(ELF) | tools $(QUIET) $(ELF2DOL) $< $@ + $(METROIDBUILDINFO) $@ buildstrings/$(NAME).$(VERSION).build $(QUIET) $(SHA1SUM) -c sha1/$(NAME).$(VERSION).sha1 ifneq ($(findstring -map,$(LDFLAGS)),) $(QUIET) $(PYTHON) tools/calcprogress.py $(DOL) $(MAP) diff --git a/asm/MetroidPrime/CFrontEndUI.s b/asm/MetroidPrime/CFrontEndUI.s index 47c62785..4956ad3b 100644 --- a/asm/MetroidPrime/CFrontEndUI.s +++ b/asm/MetroidPrime/CFrontEndUI.s @@ -127,6 +127,7 @@ MetroidBuildInfo: .ascii "!#$MetroidBuildInfo!#$" .global BuildString BuildString: +#.asciz "PAD_PAD_PAD_PAD_PAD_PAD_PAD_PAD_PAD" .if version == 2 .asciz "Build v1.097 12/19/2002 16:03:43" .asciz "AD" diff --git a/tools/Makefile b/tools/Makefile index 653c0e14..60fbadca 100644 --- a/tools/Makefile +++ b/tools/Makefile @@ -3,10 +3,13 @@ CFLAGS := -O3 -Wall -s default: all -all: elf2dol +all: elf2dol metroidbuildinfo elf2dol: elf2dol.c $(CC) $(CFLAGS) -o $@ $^ +metroidbuildinfo: metroidbuildinfo.c + $(CC) $(CFLAGS) -o $@ $^ + clean: - $(RM) elf2dol + $(RM) elf2dol metroidbuildinfo diff --git a/tools/metroidbuildinfo.c b/tools/metroidbuildinfo.c index fbbff2bf..ec827bc3 100644 --- a/tools/metroidbuildinfo.c +++ b/tools/metroidbuildinfo.c @@ -2,7 +2,7 @@ #include #include -#define VERSION_MAX_LEN 35 +#define VERSION_MAX_LEN (size_t)(35) #define METROID_BUILD_INFO_TAG "!#$MetroidBuildInfo!#$" void* memmem(const void* l, size_t l_len, const void* s, size_t s_len) { @@ -44,7 +44,7 @@ int main(int argc, const char* argv[]) { ); fprintf(stdout, "Usage:\n" - "\tmetroidbuildinfo \n"); + "\tmetroidbuildinfo \n"); return -1; } @@ -55,13 +55,22 @@ int main(int argc, const char* argv[]) { return -2; } - /* Verify string length, if the string is too long hard error */ - if (strlen(argv[2]) > VERSION_MAX_LEN) { - fprintf(stderr, "Version string '%s'\ntoo long, got %i, expected %i\n", argv[2], ver_len, - VERSION_MAX_LEN); + char build_string[36] = {0}; + FILE* build = fopen(argv[2], "rb"); + + if (!build) { + fprintf(stderr, "Unable to open '%s'\nPlease ensure the file exists!\n", argv[2]); return -3; } - /* Lets include the null terminator */ + size_t read_len = fread(build_string, 1, 35, build); + fclose(build); + + if (read_len <= 0) { + fprintf(stderr, "Empty file %s specified for build version!\n", argv[2]); + return -4; + } + + build_string[strcspn(build_string, "\n")] = '\0'; /* Get source length */ fseek(source, 0, SEEK_END); @@ -70,7 +79,7 @@ int main(int argc, const char* argv[]) { void* source_buf = malloc(source_len); if (source_buf == NULL) { fprintf(stderr, "Unable to allocate buffer of size %zubytes!\n", source_len); - return -4; + return -5; } fread(source_buf, 1, source_len, source); fclose(source); @@ -84,7 +93,7 @@ int main(int argc, const char* argv[]) { } /* Lets actually copy over the build string */ - strcpy(ptr + strlen(METROID_BUILD_INFO_TAG), argv[2]); + strcpy(ptr + strlen(METROID_BUILD_INFO_TAG), build_string); /* Now attempt to open the target file */ FILE* target = fopen(argv[1], "wb"); @@ -101,5 +110,5 @@ int main(int argc, const char* argv[]) { /* Don't leak */ free(source_buf); - return 1; + return 0; }