From f32540fabce7d5dcb048d26919826c30dcb982b7 Mon Sep 17 00:00:00 2001 From: Lucy Date: Thu, 14 Nov 2024 12:56:12 +0100 Subject: [PATCH] tool to fix the gba header's checksum --- bootfix.C | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 bootfix.C diff --git a/bootfix.C b/bootfix.C new file mode 100644 index 0000000..57448e8 --- /dev/null +++ b/bootfix.C @@ -0,0 +1,40 @@ +#include +#include +#include +#include + +int main(int argc, char** argv){ + if(argc!=2){ + fprintf(stderr, "missing argument"); + return 1; + } + FILE* f = fopen(argv[1], "rb+"); + if(f == NULL){ + fprintf(stderr, "can't open dst file <%s>\n", argv[1]); + return 1; + } + uint8_t h[224] = {0}; + fseek(f, 0, SEEK_END); + size_t s = ftell(f); + if(s!=sizeof(h)){ + fprintf(stderr, "header is %zu but should be %zu", s, sizeof(h)); + return 1; + } + rewind(f); + printf("reading\n"); + fread(h, 1, sizeof(h), f); + printf("calculating header checksum..\n"); + int8_t chk = 0; + for(int i=156;i<=184;i++){ + chk += h[i]; + } + uint8_t sum = -(0x19+chk); + h[185] = sum; + printf("checksum is %hhu\n", sum); + fseek(f, 0, SEEK_SET); + // write sum + fwrite(h, 1, sizeof(h), f); + fflush(f); + fclose(f); + return 0; +}