changeset 99:c59011177e2e

cp2102-update-eeprom program written, compiles
author Mychaela Falconia <falcon@freecalypso.org>
date Thu, 28 Sep 2023 05:25:39 +0000
parents 1cacc1ae56f0
children 29bff463402e
files .hgignore cp2102/Makefile cp2102/update_eeprom.c
diffstat 3 files changed, 85 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/.hgignore	Thu Sep 28 04:45:13 2023 +0000
+++ b/.hgignore	Thu Sep 28 05:25:39 2023 +0000
@@ -8,6 +8,7 @@
 ^cp2102/cp2102-read-baudtab$
 ^cp2102/cp2102-read-eeprom$
 ^cp2102/cp2102-read-partno$
+^cp2102/cp2102-update-eeprom$
 ^cp2102/cp2102-write-eeprom$
 ^cp2102/file_rw_test$
 
--- a/cp2102/Makefile	Thu Sep 28 04:45:13 2023 +0000
+++ b/cp2102/Makefile	Thu Sep 28 05:25:39 2023 +0000
@@ -2,7 +2,7 @@
 CFLAGS=	-O2
 PROGS=	cp2102-decode-baudtab cp2102-decode-ee-desc cp2102-patch-ee-image \
 	cp2102-read-baudtab cp2102-read-eeprom cp2102-read-partno \
-	cp2102-write-eeprom
+	cp2102-update-eeprom cp2102-write-eeprom
 NOINST=	file_rw_test
 LIBS=	../libuwrap/libuwrap.a
 
@@ -22,6 +22,8 @@
 			read_eeprom_main.o
 READ_PARTNO_OBJS=	find_dev.o read_partno.o
 RW_TEST_OBJS=		intel_hex_in.o intel_hex_out.o file_rw_test.o
+UPDATE_EEPROM_OBJS=	apply_eeprom_patch.o find_dev.o read_eeprom.o \
+			update_eeprom.o write_eeprom.o
 WRITE_EEPROM_OBJS=	find_dev.o intel_hex_in.o write_eeprom.o \
 			write_eeprom_main.o
 
@@ -45,6 +47,9 @@
 cp2102-read-partno:	${READ_PARTNO_OBJS} ${LIBS}
 	${CC} ${CFLAGS} -o $@ ${READ_PARTNO_OBJS} ${LIBS} -lusb
 
+cp2102-update-eeprom:	${UPDATE_EEPROM_OBJS} ${LIBS}
+	${CC} ${CFLAGS} -o $@ ${UPDATE_EEPROM_OBJS} ${LIBS} -lusb
+
 cp2102-write-eeprom:	${WRITE_EEPROM_OBJS} ${LIBS}
 	${CC} ${CFLAGS} -o $@ ${WRITE_EEPROM_OBJS} ${LIBS} -lusb
 
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/cp2102/update_eeprom.c	Thu Sep 28 05:25:39 2023 +0000
@@ -0,0 +1,78 @@
+/*
+ * This program locates a CP2102 device via libusb, reads its internal EEPROM,
+ * applies user-requested patches to the EEPROM image, and then reprograms
+ * the physical EEPROM with the patched version.
+ */
+
+#include <sys/types.h>
+#include <string.h>
+#include <strings.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <usb.h>
+#include "../libuwrap/find_dev.h"
+#include "../libuwrap/open_close.h"
+#include "cp210x_defs.h"
+
+extern struct usb_device *find_cp2102_device();
+
+u_char eeprom[SIZE_EEPROM];
+char *device_selector, *baud_change;
+int no_detach;
+
+process_cmdline(argc, argv)
+	char **argv;
+{
+	int c;
+	extern int optind;
+	extern char *optarg;
+
+	while ((c = getopt(argc, argv, "b:d:n")) != EOF) {
+		switch (c) {
+		case 'b':
+			baud_change = optarg;
+			continue;
+		case 'd':
+			device_selector = optarg;
+			continue;
+		case 'n':
+			no_detach = 1;
+			continue;
+		default:
+		usage:
+			fprintf(stderr,
+				"usage: %s [hw-options] eeprom-changes\n",
+				argv[0]);
+			exit(1);
+		}
+	}
+	if (argc == optind && !baud_change)
+		goto usage;
+}
+
+main(argc, argv)
+	char **argv;
+{
+	struct usb_device *dev;
+	usb_dev_handle *usbh;
+	extern int optind;
+
+	process_cmdline(argc, argv);
+	dev = find_cp2102_device(device_selector);
+	usbh = usb_open(dev);
+	if (!usbh) {
+		fprintf(stderr, "error: usb_open() failed\n");
+		exit(1);
+	}
+	read_eeprom(usbh);
+	if (baud_change)
+		apply_eeprom_patch_baud(baud_change);
+	for (; optind < argc; optind++)
+		apply_eeprom_patch_file(argv[optind]);
+	if (!no_detach)
+		usbwrap_claim_all_ifs(usbh);
+	write_eeprom(usbh);
+	usb_close(usbh);
+	exit(0);
+}