view fteeprom/ftee-fix-cksum.c @ 88:ea7b411aad27

cp2102-decode-ee-desc: decode interface descriptor
author Mychaela Falconia <falcon@freecalypso.org>
date Wed, 27 Sep 2023 19:39:44 +0000
parents 85256d5aa559
children
line wrap: on
line source

/*
 * 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);
}