changeset 12:fe5f7ba7f154

c139-analyze-boot utility put together, compiles
author Mychaela Falconia <falcon@freecalypso.org>
date Sat, 10 Jun 2023 04:58:26 +0000
parents 2a62a7decd9f
children f232fb350e1c
files .hgignore bootutil/Makefile bootutil/c139_main.c bootutil/do_match.c
diffstat 4 files changed, 170 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/.hgignore	Sat Jun 10 03:23:10 2023 +0000
+++ b/.hgignore	Sat Jun 10 04:58:26 2023 +0000
@@ -9,3 +9,5 @@
 ^bootmatch/c139_nolock\.c$
 ^bootmatch/c155_boot\.c$
 ^bootmatch/fc_patched_boot\.c$
+
+^bootutil/c139-analyze-boot$
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/bootutil/Makefile	Sat Jun 10 04:58:26 2023 +0000
@@ -0,0 +1,29 @@
+CC=	gcc
+CFLAGS=	-O2
+PROGS=	c139-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
+
+all:	${PROGS}
+
+c139-analyze-boot:	${C139ANB_OBJS}
+	${CC} ${CFLAGS} -o $@ ${C139ANB_OBJS}
+
+c11x_lockable.o:	../bootmatch/c11x_lockable.c
+	${CC} ${CFLAGS} -c -o $@ $<
+
+c11x_nolock.o:	../bootmatch/c11x_nolock.c
+	${CC} ${CFLAGS} -c -o $@ $<
+
+c139_lockable.o:	../bootmatch/c139_lockable.c
+	${CC} ${CFLAGS} -c -o $@ $<
+
+c139_nolock.o:	../bootmatch/c139_nolock.c
+	${CC} ${CFLAGS} -c -o $@ $<
+
+fc_patched_boot.o:	../bootmatch/fc_patched_boot.c
+	${CC} ${CFLAGS} -c -o $@ $<
+
+clean:
+	rm -f *.o ${PROGS}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/bootutil/c139_main.c	Sat Jun 10 04:58:26 2023 +0000
@@ -0,0 +1,118 @@
+/*
+ * This C module is the main for c139-analyze-boot utility.
+ */
+
+#include <sys/types.h>
+#include <sys/file.h>
+#include <sys/stat.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <strings.h>
+#include <unistd.h>
+#include "../bootmatch/bootmatch.h"
+
+extern struct bootmatch bootmatch_c11x_lockable[];
+extern struct bootmatch bootmatch_c11x_nolock[];
+extern struct bootmatch bootmatch_c139_lockable[];
+extern struct bootmatch bootmatch_c139_nolock[];
+extern struct bootmatch bootmatch_fc_patch[];
+
+#define	LENGTH_OF_INTEREST	0x2064
+
+static u_char image[LENGTH_OF_INTEREST];
+
+static void
+read_bin_file(filename)
+	char *filename;
+{
+	int fd;
+	struct stat st;
+
+	fd = open(filename, O_RDONLY);
+	if (fd < 0) {
+		perror(filename);
+		exit(1);
+	}
+	fstat(fd, &st);
+	if (!S_ISREG(st.st_mode)) {
+		fprintf(stderr, "error: %s is not a regular file\n", filename);
+		exit(1);
+	}
+	if (st.st_size < LENGTH_OF_INTEREST) {
+		fprintf(stderr, "error: %s is too short\n", filename);
+		exit(1);
+	}
+	read(fd, image, LENGTH_OF_INTEREST);
+	close(fd);
+}
+
+static int
+check_810_signature()
+{
+	if (image[0x810] != '1')
+		return(0);
+	if (image[0x811] != '0')
+		return(0);
+	if (image[0x812] != '0')
+		return(0);
+	if (image[0x813] == '3' || image[0x813] == '4')
+		return(1);
+	else
+		return(0);
+}
+
+static void
+classify_by_lock_word()
+{
+	unsigned lword;
+
+	lword = ((unsigned) image[0x2060]) |
+		((unsigned) image[0x2061] << 8) |
+		((unsigned) image[0x2062] << 16) |
+		((unsigned) image[0x2063] << 24);
+	if (lword == 0xDDDDDDDD)
+		puts("unlocked");
+	else
+		puts("locked");
+}
+
+main(argc, argv)
+	char **argv;
+{
+	if (argc != 2) {
+		fprintf(stderr, "usage: %s flashdump.bin\n", argv[0]);
+		exit(1);
+	}
+	read_bin_file(argv[1]);
+	if (check_for_match(image, bootmatch_fc_patch)) {
+		puts("fc");
+		exit(0);
+	}
+	if (check_for_match(image, bootmatch_c11x_nolock)) {
+		puts("unlocked");
+		exit(0);
+	}
+	if (check_for_match(image, bootmatch_c11x_lockable)) {
+		classify_by_lock_word();
+		exit(0);
+	}
+	if (check_for_match(image, bootmatch_c139_nolock)) {
+		if (!check_810_signature()) {
+			puts("unknown");
+			exit(0);
+		}
+		puts("unlocked");
+		exit(0);
+	}
+	if (check_for_match(image, bootmatch_c139_lockable)) {
+		if (!check_810_signature()) {
+			puts("unknown");
+			exit(0);
+		}
+		classify_by_lock_word();
+		exit(0);
+	}
+	puts("unknown");
+	exit(0);
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/bootutil/do_match.c	Sat Jun 10 04:58:26 2023 +0000
@@ -0,0 +1,21 @@
+/*
+ * The function implemented in this module performs a single match-check
+ * between the boot image being analyzed and one of the reference versions
+ * from ../bootmatch directory.
+ */
+
+#include <sys/types.h>
+#include <string.h>
+#include <strings.h>
+#include "../bootmatch/bootmatch.h"
+
+check_for_match(image, bm)
+	u_char *image;
+	struct bootmatch *bm;
+{
+	for (; bm->nbytes; bm++) {
+		if (bcmp(image + bm->offset, bm->refbytes, bm->nbytes))
+			return(0);
+	}
+	return(1);
+}