commit f48499265ba2c6f39a1c3f35955aaa3b72ecaaa8
Author: Louis Burda <quent.burda@gmail.com>
Date: Fri, 29 Apr 2022 17:28:25 +0200
Simple binary patch
Diffstat:
3 files changed, 68 insertions(+), 0 deletions(-)
diff --git a/.gitignore b/.gitignore
@@ -0,0 +1 @@
+fwpatch
diff --git a/Makefile b/Makefile
@@ -0,0 +1,7 @@
+CFLAGS = -g
+
+.PHONY: all
+
+all: fwpatch
+
+fwpatch: fwpatch.c
diff --git a/fwpatch.c b/fwpatch.c
@@ -0,0 +1,60 @@
+#include <stdlib.h>
+#include <stdio.h>
+#include <stdarg.h>
+#include <stdint.h>
+
+const char usage[] = "USAGE: fwpatch TARGET OFFSET FILE";
+
+void
+die(const char *fmt, ...)
+{
+ va_list ap;
+
+ va_start(ap, fmt);
+ vfprintf(stderr, fmt, ap);
+ va_end(ap);
+
+ exit(1);
+}
+
+int
+main(int argc, const char **argv)
+{
+ size_t addr, size;
+ FILE *file;
+ char *end, *data;
+
+ if (argc != 4)
+ die(usage);
+
+ addr = strtoull(argv[2], &end, 0);
+ if (*end) die("Invalid offset: %s", argv[2]);
+
+ file = fopen(argv[3], "rb");
+ if (!file) die("Failed to open: %s", argv[3]);
+
+ fseek(file, 0, SEEK_END);
+ size = ftell(file);
+
+ data = malloc(size);
+ if (!data) die("Out of memory\n");
+
+ fseek(file, 0, SEEK_SET);
+ if (fread(data, 1, size, file) != size)
+ die("Read failed\n");
+
+ fclose(file);
+
+ file = fopen(argv[1], "r+b");
+ if (!file) die("Failed to open: %s", argv[1]);
+
+ fseek(file, addr, SEEK_SET);
+ if (ftell(file) != addr) die("Invalid offset\n");
+
+ if (fwrite(data, 1, size, file) != size)
+ die("Write failed\n");
+
+ fclose(file);
+
+ free(data);
+}