tool to fix the gba header's checksum

This commit is contained in:
Lucy 2024-11-14 12:56:12 +01:00
parent e6c0b45679
commit f32540fabc

40
bootfix.C Normal file
View file

@ -0,0 +1,40 @@
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
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;
}