You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
145 lines
2.9 KiB
C
145 lines
2.9 KiB
C
#include <stdio.h>
|
|
#include <stdint.h>
|
|
|
|
#ifndef WIN32
|
|
//%%%%
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include <unistd.h>
|
|
#else
|
|
//%%%%
|
|
#include <windows.h>
|
|
#include "getopt.h"
|
|
#include "printf.h"
|
|
#endif
|
|
|
|
#include "patcher.h"
|
|
|
|
|
|
//#######################################################################################################
|
|
void main(int argc, char* argv[]) {
|
|
|
|
FILE* in;
|
|
FILE* out;
|
|
uint8_t* buf;
|
|
uint32_t fsize;
|
|
int opt;
|
|
uint8_t outfilename[100];
|
|
int oflag=0,bflag=0;
|
|
uint32_t res;
|
|
|
|
// parsing command line
|
|
|
|
while ((opt = getopt(argc, argv, "o:bh")) != -1) {
|
|
switch (opt) {
|
|
case 'h':
|
|
|
|
printf("\n Program for automatic patching of Balong V7 bootloaders\n\n\
|
|
%s [options] <usbloader file>\n\n\
|
|
Following options are allowed:\n\n\
|
|
-o file - Name of the output file (by default, only a patch test is performed)\n\
|
|
-b - Add a patch that disables checking of defective blocks\n\
|
|
\n",argv[0]);
|
|
return;
|
|
|
|
case 'o':
|
|
strcpy(outfilename,optarg);
|
|
oflag=1;
|
|
break;
|
|
|
|
case 'b':
|
|
bflag=1;
|
|
break;
|
|
|
|
case '?':
|
|
case ':':
|
|
return;
|
|
|
|
}
|
|
}
|
|
|
|
printf("\n Program for automatic modification of Balong V7 loaders, (c) forth32");
|
|
printf("\n English Version (c) robertzaage, 2022");
|
|
|
|
if (optind>=argc) {
|
|
printf("\n - Name of the file to load is not specified\n - add -h option for help\n");
|
|
return;
|
|
}
|
|
|
|
in=fopen(argv[optind],"rb");
|
|
if (in == 0) {
|
|
printf("\n Can't open file %s",argv[optind]);
|
|
return;
|
|
}
|
|
|
|
// determine file size
|
|
fseek(in,0,SEEK_END);
|
|
fsize=ftell(in);
|
|
rewind(in);
|
|
|
|
// select a buffer and read the whole file there
|
|
buf=malloc(fsize);
|
|
fread(buf,1,fsize,in);
|
|
fclose(in);
|
|
|
|
//==================================================================================
|
|
|
|
res=pv7r1(buf, fsize);
|
|
if (res != 0) {
|
|
printf("\n* V7R1 type signature found at offset %08x",res);
|
|
goto endpatch;
|
|
}
|
|
|
|
res=pv7r2(buf, fsize);
|
|
if (res != 0) {
|
|
printf("\n* V7R2 type signature found at offset %08x",res);
|
|
goto endpatch;
|
|
}
|
|
|
|
res=pv7r11(buf, fsize);
|
|
if (res != 0) {
|
|
printf("\n* V7R11 type signature found at offset %08x",res);
|
|
goto endpatch;
|
|
}
|
|
|
|
res=pv7r22(buf, fsize);
|
|
if (res != 0) {
|
|
printf("\n* V7R22 type signature found at offset %08x",res);
|
|
goto endpatch;
|
|
}
|
|
|
|
res=pv7r22_2(buf, fsize);
|
|
if (res != 0) {
|
|
printf("\n* V7R22_2 type signature found at offset %08x",res);
|
|
goto endpatch;
|
|
}
|
|
|
|
res=pv7r22_3(buf, fsize);
|
|
if (res != 0) {
|
|
printf("\n* V7R22_3 type signature found at offset %08x",res);
|
|
goto endpatch;
|
|
}
|
|
|
|
printf("\n! Patch eraseall signature not found");
|
|
|
|
//==================================================================================
|
|
endpatch:
|
|
|
|
if (bflag) {
|
|
res=perasebad(buf, fsize);
|
|
if (res != 0) printf("\n* Found isbad signature at offset %08x",res);
|
|
else printf("\n! Signature isbad not found");
|
|
}
|
|
|
|
if (oflag) {
|
|
out=fopen(outfilename,"wb");
|
|
if (out != 0) {
|
|
fwrite(buf,1,fsize,out);
|
|
fclose(out);
|
|
}
|
|
else printf("\n Error opening output file %s",outfilename);
|
|
}
|
|
free(buf);
|
|
printf("\n");
|
|
}
|