/* * $Id$ */ #include #include /* * This function will patch binary file */ static char buf[512]; static void usage(void) { printf("usage: binpatch [-h] " "[ [ []]]\n"); printf("this function patches binary file at specified offset with\n"); printf("up to 4 bytes provided on command line \n"); printf("-h - prints this message\n\n"); printf(" - output file\n"); printf(" - input ifile\n"); printf(" - relocation address of image\n"); printf(" - offset of patch, offset in file is at off - reloc\n"); printf(" - byte 0 of patch\n"); printf(" - byte 1 of patch\n"); printf(" - byte 1 of patch\n"); printf(" - byte 1 of patch\n"); return; } int main(int argc, char **argv) { int c; FILE *ofp, *ifp; char patch[4], *end; int patchLen, tmp, i, off, cnt, patched, len, reloc; /* parse command line options */ while ((c = getopt(argc, argv, "h")) >= 0) { switch (c) { case 'h': usage(); return 0; default: usage(); return 1; } } if(argc < 6) { usage(); return 1; } /* Let us get offset in file */ reloc = strtol(argv[3], &end, 0); if(end == argv[3] || off < 0) { fprintf(stderr, "bad reloc value %s\n", argv[3]); return 1; } off = strtol(argv[4], &end, 0); if(end == argv[4] || off < 0 || off < reloc) { fprintf(stderr, "bad offset value %s\n", argv[4]); return 1; } off -= reloc; /* Let us get patch */ patchLen = argc - 5; for(i=0; i 0xff) { fprintf(stderr, "bad byte value %s\n", argv[5+i]); return 1; } patch[i] = tmp; } ifp = fopen(argv[2], "r"); if(ifp == NULL) { fprintf(stderr, "unable to open file %s\n", argv[2]); return 1; } ofp = fopen(argv[1], "w"); if(ofp == NULL) { fprintf(stderr, "unable to open file %s\n", argv[1]); return 1; } cnt = 0; patched = 0; for(;;) { len = fread(buf, 1, sizeof(buf), ifp); if(len == 0) { break; } if(cnt <= off && (cnt + len) > off) { /* Perform patch */ for(i=0; i off && cnt < (off + patchLen)) { /* Perform patch */ for(i=cnt-off; i