changeset 29:a7393d00996a

fc-duart28-conf: implement check-eeprom
author Mychaela Falconia <falcon@freecalypso.org>
date Sat, 09 Sep 2023 19:13:35 +0000
parents c4b4ebaa2117
children 8de3891460db
files duart28/Makefile duart28/eeprom_rd.c duart28/main.c
diffstat 3 files changed, 157 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
--- a/duart28/Makefile	Sat Sep 09 17:55:14 2023 +0000
+++ b/duart28/Makefile	Sat Sep 09 19:13:35 2023 +0000
@@ -1,7 +1,7 @@
 CC=	gcc
 CFLAGS=	-O2
 PROG=	fc-duart28-conf
-OBJS=	find_usb.o main.o
+OBJS=	eeprom_rd.o find_usb.o main.o
 LIBS=	../libftmini/libftmini.a ../libuwrap/libuwrap.a
 
 INSTALL_PREFIX=	/opt/freecalypso
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/duart28/eeprom_rd.c	Sat Sep 09 19:13:35 2023 +0000
@@ -0,0 +1,142 @@
+/*
+ * This module implements the steps of reading and validating the starting
+ * EEPROM content.
+ */
+
+#include <sys/types.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <strings.h>
+#include <usb.h>
+#include "../libftmini/eeprom_func.h"
+
+u_short eeprom[64];
+
+void
+read_eeprom(usbh)
+	usb_dev_handle *usbh;
+{
+	unsigned n;
+
+	for (n = 0; n < 64; n++)
+		eeprom[n] = ftmini_read_eeprom_loc(usbh, n);
+}
+
+static void
+verify_nonblank()
+{
+	unsigned n;
+
+	for (n = 0; n < 64; n++) {
+		if (eeprom[n] != 0xFFFF)
+			return;
+	}
+	fprintf(stderr, "error: EEPROM is blank (erased?)\n");
+	exit(1);
+}
+
+static void
+verify_eeprom_chksum()
+{
+	u_short chksum = 0xAAAA;
+	unsigned n;
+
+	for (n = 0; n < 64; n++) {
+		chksum ^= eeprom[n];
+		chksum = (chksum << 1) | (chksum >> 15);
+	}
+	if (chksum == 0)
+		return;
+	fprintf(stderr, "error: EEPROM checksum is wrong\n");
+	exit(1);
+}
+
+static int
+check_req_match()
+{
+	if (eeprom[0] != 0x0808)
+		return 0;
+	if (eeprom[1] != 0x0403)
+		return 0;
+	if (eeprom[2] != 0x6010 && eeprom[2] != 0x7152)
+		return 0;
+	if (eeprom[3] != 0x0500)
+		return 0;
+	if (eeprom[4] != 0x3280)
+		return 0;
+	if (eeprom[5] != 0x0000 && eeprom[5] != 0x0008)
+		return 0;
+	if (eeprom[6] != 0x0200)
+		return 0;
+	if (eeprom[7] != 0x1896)
+		return 0;
+	if (eeprom[8] != 0x12AE)
+		return 0;
+	if (eeprom[10] != 0x0046)
+		return 0;
+	if (eeprom[11] != 0x0318)
+		return 0;
+	if (eeprom[12] != 'F')
+		return 0;
+	if (eeprom[13] != 'r')
+		return 0;
+	if (eeprom[14] != 'e')
+		return 0;
+	if (eeprom[15] != 'e')
+		return 0;
+	if (eeprom[16] != 'C')
+		return 0;
+	if (eeprom[17] != 'a')
+		return 0;
+	if (eeprom[18] != 'l')
+		return 0;
+	if (eeprom[19] != 'y')
+		return 0;
+	if (eeprom[20] != 'p')
+		return 0;
+	if (eeprom[21] != 's')
+		return 0;
+	if (eeprom[22] != 'o')
+		return 0;
+	if (eeprom[23] != 0x0312)
+		return 0;
+	if (eeprom[24] != 'D')
+		return 0;
+	if (eeprom[25] != 'U')
+		return 0;
+	if (eeprom[26] != 'A')
+		return 0;
+	if (eeprom[27] != 'R')
+		return 0;
+	if (eeprom[28] != 'T')
+		return 0;
+	if (eeprom[29] != '2')
+		return 0;
+	if (eeprom[30] != '8')
+		return 0;
+	if (eeprom[31] != 'C' && eeprom[31] != 'S')
+		return 0;
+	return 1;
+}
+
+analyze_eeprom()
+{
+	verify_nonblank();
+	verify_eeprom_chksum();
+	if (!check_req_match()) {
+		fprintf(stderr,
+			"error: EEPROM content does not match FC DUART28\n");
+		exit(1);
+	}
+	if (eeprom[2] == 0x7152 && eeprom[31] == 'C') {
+		printf("EEPROM is programmed for DUART28C\n");
+		return 'C';
+	}
+	if (eeprom[2] == 0x6010 && eeprom[31] == 'S') {
+		printf("EEPROM is programmed for DUART28S\n");
+		return 'C';
+	}
+	printf("EEPROM is inconsistent between C and S configs!\n");
+	return '?';
+}
--- a/duart28/main.c	Sat Sep 09 17:55:14 2023 +0000
+++ b/duart28/main.c	Sat Sep 09 19:13:35 2023 +0000
@@ -8,6 +8,7 @@
 #include <string.h>
 #include <strings.h>
 #include <usb.h>
+#include "../libuwrap/open_close.h"
 
 extern struct usb_device *find_duart28_usbdev();
 
@@ -23,8 +24,19 @@
 
 oper_check_eeprom()
 {
-	fprintf(stderr, "error: check-eeprom command not yet implemented\n");
-	exit(1);
+	struct usb_device *dev;
+	usb_dev_handle *usbh;
+
+	dev = find_duart28_usbdev();
+	if (!dev) {
+		fprintf(stderr, "error: no DUART28 adapter found\n");
+		exit(1);
+	}
+	usbh = usbwrap_open_dev(dev, 1);
+	read_eeprom(usbh);
+	usbwrap_close_dev(usbh);
+	analyze_eeprom();
+	return 0;
 }
 
 oper_program(newconf)