changeset 16:6b0d533046e5

bootutil: add c139-patch-dmagic
author Mychaela Falconia <falcon@freecalypso.org>
date Sat, 10 Jun 2023 05:49:00 +0000
parents 36e65605d16a
children 632d62e5efb4
files .hgignore bootutil/Makefile bootutil/c139-patch-dmagic.c
diffstat 3 files changed, 51 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/.hgignore	Sat Jun 10 05:35:37 2023 +0000
+++ b/.hgignore	Sat Jun 10 05:49:00 2023 +0000
@@ -11,4 +11,5 @@
 ^bootmatch/fc_patched_boot\.c$
 
 ^bootutil/c139-analyze-boot$
+^bootutil/c139-patch-dmagic$
 ^bootutil/c155-analyze-boot$
--- a/bootutil/Makefile	Sat Jun 10 05:35:37 2023 +0000
+++ b/bootutil/Makefile	Sat Jun 10 05:49:00 2023 +0000
@@ -1,6 +1,6 @@
 CC=	gcc
 CFLAGS=	-O2
-PROGS=	c139-analyze-boot c155-analyze-boot
+PROGS=	c139-analyze-boot c139-patch-dmagic c155-analyze-boot
 
 C139ANB_OBJS=	c11x_lockable.o c11x_nolock.o c139_lockable.o c139_main.o \
 		c139_nolock.o do_match.o fc_patched_boot.o
@@ -15,6 +15,9 @@
 c139-analyze-boot:	${C139ANB_OBJS}
 	${CC} ${CFLAGS} -o $@ ${C139ANB_OBJS}
 
+c139-patch-dmagic:	c139-patch-dmagic.c
+	${CC} ${CFLAGS} -o $@ $@.c
+
 c155-analyze-boot:	${C155ANB_OBJS}
 	${CC} ${CFLAGS} -o $@ ${C155ANB_OBJS}
 
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/bootutil/c139-patch-dmagic.c	Sat Jun 10 05:49:00 2023 +0000
@@ -0,0 +1,46 @@
+/*
+ * This program patches DD DD DD DD magic bytes into the given binary file
+ * at offset 0x2060, thereby turning a locked C11x/12x or C139/140 flash image
+ * into an unlocked one.
+ */
+
+#include <sys/types.h>
+#include <sys/file.h>
+#include <sys/stat.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+
+static u_char magic[4] = {0xDD, 0xDD, 0xDD, 0xDD};
+
+main(argc, argv)
+	char **argv;
+{
+	int fd;
+	struct stat st;
+
+	if (argc != 2) {
+		fprintf(stderr, "usage: %s flashimage.bin\n", argv[0]);
+		exit(1);
+	}
+	fd = open(argv[1], O_RDWR);
+	if (fd < 0) {
+		perror(argv[1]);
+		exit(1);
+	}
+	fstat(fd, &st);
+	if (!S_ISREG(st.st_mode)) {
+		fprintf(stderr, "error: %s is not a regular file\n", argv[1]);
+		exit(1);
+	}
+	if (st.st_size < 0x2064) {
+		fprintf(stderr, "error: %s is too short\n", argv[1]);
+		exit(1);
+	}
+	lseek(fd, 0x2060, SEEK_SET);
+	if (write(fd, magic, 4) != 4) {
+		perror("error writing to file");
+		exit(1);
+	}
+	exit(0);
+}