#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
/* patch an int32 or int64 field in the FFS superblock at SBOFF=8192 */
#define SBOFF 8192
int main(int argc, char **argv){
    FILE *f;
    long off; long long val;
    if (argc < 4) { fprintf(stderr,"usage: craft64 img off val [4|8]\n"); return 1; }
    f = fopen(argv[1], "r+b"); if (!f){ perror("open"); return 1; }
    off = SBOFF + strtoull(argv[2], NULL, 0);
    val = strtoull(argv[3], NULL, 0);
    int width = (argc > 4) ? atoi(argv[4]) : 8;
    fseek(f, off, SEEK_SET);
    if (width == 4) {
        int32_t v = (int32_t)val;
        fwrite(&v, 4, 1, f);
        printf("patched int32 sb+%ld = %d (0x%08x)\n", off-SBOFF, v, (uint32_t)v);
    } else {
        int64_t v = (int64_t)val;
        fwrite(&v, 8, 1, f);
        printf("patched int64 sb+%ld = %lld (0x%016llx)\n", off-SBOFF, (long long)v, (unsigned long long)v);
    }
    fclose(f); return 0;
}
