#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
/* patch an int32 (default) or single byte field in the FFS sb at SBOFF=8192 */
#define SBOFF 8192
int main(int argc, char **argv){
    FILE *f;
    long off; long val;
    if (argc < 4) { fprintf(stderr,"usage: craft img off val [b]\n"); return 1; }
    f = fopen(argv[1], "r+b"); if (!f){ perror("open"); return 1; }
    off = SBOFF + atol(argv[2]);
    val = atol(argv[3]);
    if (argc > 4) {
        uint8_t b = (uint8_t)val;
        fseek(f, off, SEEK_SET); fwrite(&b, 1, 1, f);
        printf("patched byte sb+%ld = %d\n", off-SBOFF, b);
    } else {
        int32_t v = (int32_t)val;
        fseek(f, off, SEEK_SET); fwrite(&v, 4, 1, f);
        printf("patched int32 sb+%ld = %d (0x%08x)\n", off-SBOFF, v, (uint32_t)v);
    }
    fclose(f); return 0;
}
