diff fteeprom/ftee-fix-cksum.c @ 66:85256d5aa559

ftee-fix-cksum program written, compiles
author Mychaela Falconia <falcon@freecalypso.org>
date Tue, 12 Sep 2023 23:19:05 +0000
parents fteeprom/ftee-decode.c@225dc1d9f2f1
children
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/fteeprom/ftee-fix-cksum.c	Tue Sep 12 23:19:05 2023 +0000
@@ -0,0 +1,76 @@
+/*
+ * Sometimes it is useful to be able to edit FTDI EEPROM hex images
+ * by hand, for special situations and experiments that are too unique
+ * to automate.  However, the need for passing checksum gets in the way.
+ * This program reads an FTDI EEPROM image from a file (or from stdin),
+ * recomputes a new checksum and re-emits the checksum-fixed EEPROM hex
+ * image on stdout.
+ */
+
+#include <sys/types.h>
+#include <string.h>
+#include <strings.h>
+#include <stdio.h>
+#include <stdlib.h>
+
+extern unsigned eeprom_size;
+extern u_short eeprom[256];
+
+static unsigned chksum_size;
+
+static void
+do_checksum()
+{
+	u_short chksum = 0xAAAA;
+	unsigned n;
+
+	for (n = 0; n < chksum_size - 1; n++) {
+		chksum ^= eeprom[n];
+		chksum = (chksum << 1) | (chksum >> 15);
+	}
+	eeprom[n] = chksum;
+}
+
+static void
+emit_output()
+{
+	unsigned n, col;
+
+	for (n = 0; n < eeprom_size; n++) {
+		col = n & 7;
+		if (col == 0)
+			printf("%02X:", n * 2);
+		printf(" %04X", eeprom[n]);
+		if (col == 7)
+			putchar('\n');
+	}
+}
+
+main(argc, argv)
+	char **argv;
+{
+	if (argc != 2) {
+		fprintf(stderr, "usage: %s eeprom-image-file\n", argv[0]);
+		exit(1);
+	}
+	if (strcmp(argv[1], "-"))
+		read_eeprom_from_file(argv[1]);
+	else
+		read_eeprom_from_stdin();
+	switch (eeprom_size) {
+	case 64:
+		chksum_size = 64;
+		break;
+	case 128:
+	case 256:
+		chksum_size = 128;
+		break;
+	default:
+		fprintf(stderr,
+			"BUG: invalid EEPROM size not caught earlier\n");
+		exit(1);
+	}
+	do_checksum();
+	emit_output();
+	exit(0);
+}