view fteeprom/fteeprom-prog.c @ 45:a94bd4002076

fteeprom-prog: implement -d option for interface detach
author Mychaela Falconia <falcon@freecalypso.org>
date Mon, 11 Sep 2023 02:48:22 +0000
parents 940cde8a99b6
children ca2250b4833d
line wrap: on
line source

/*
 * This program connects to an FTDI chip via libusb and programs its EEPROM
 * with a raw hex image read from stdin or from a backup file.
 *
 * The present version has been converted to use our local libraries
 * (libftmini and libuwrap) instead of libftdi - thus the external dependency
 * is only libusb.
 */

#include <sys/types.h>
#include <ctype.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 "../libftmini/eeprom_func.h"

char *device_selector, *input_filename;
int detach_num, ft232r_mode;
unsigned eeprom_size;
u_short eeprom[256];

process_cmdline(argc, argv)
	char **argv;
{
	int c;
	extern int optind;
	extern char *optarg;

	while ((c = getopt(argc, argv, "d:r")) != EOF) {
		switch (c) {
		case 'd':
			detach_num = atoi(optarg);
			switch (detach_num) {
			case 0:
			case 1:
			case 2:
			case 4:
				break;
			default:
				fprintf(stderr, "error: invalid -d argument\n");
				exit(1);
			}
			continue;
		case 'r':
			ft232r_mode = 1;
			detach_num = 1;
			continue;
		default:
			/* error msg already printed */
			exit(1);
		}
	}
	if (argc < optind + 1 || argc > optind + 2) {
		fprintf(stderr,
			"usage: %s [options] device-selector [eeprom-image]\n",
			argv[0]);
		exit(1);
	}
	device_selector = argv[optind];
	input_filename = argv[optind + 1];
}

read_eeprom_common(inf, filename_for_errs)
	FILE *inf;
	char *filename_for_errs;
{
	char linebuf[1024], *cp;
	int lineno, rc;
	unsigned ptr = 0, input_off;

	for (lineno = 1; fgets(linebuf, sizeof linebuf, inf); lineno++) {
		for (cp = linebuf; isspace(*cp); cp++)
			;
		if (*cp == '\0' || *cp == '#')
			continue;
		if (ptr >= 256) {
			fprintf(stderr,
				"%s line %d: maximum EEPROM size exceeded\n",
				filename_for_errs, lineno);
			exit(1);
		}
		rc = sscanf(linebuf, "%x: %hx %hx %hx %hx %hx %hx %hx %hx",
			    &input_off, eeprom + ptr, eeprom + ptr + 1,
			    eeprom + ptr + 2, eeprom + ptr + 3,
			    eeprom + ptr + 4, eeprom + ptr + 5,
			    eeprom + ptr + 6, eeprom + ptr + 7);
		if (rc != 9 || input_off != ptr * 2) {
			fprintf(stderr, "%s line %d: invalid input\n",
				filename_for_errs, lineno);
			exit(1);
		}
		ptr += 8;
	}
	if (ptr != 64 && ptr != 128 && ptr != 256) {
		fprintf(stderr, "%s: not an EEPROM image of recognized size\n",
			filename_for_errs);
		exit(1);
	}
	eeprom_size = ptr;
}

read_eeprom_from_file(filename)
	char *filename;
{
	FILE *inf;

	inf = fopen(filename, "r");
	if (!inf) {
		perror(filename);
		exit(1);
	}
	read_eeprom_common(inf, filename);
	fclose(inf);
}

read_eeprom_from_stdin()
{
	read_eeprom_common(stdin, "stdin");
}

main(argc, argv)
	char **argv;
{
	struct usb_device *dev;
	usb_dev_handle *usbh;
	unsigned n;

	process_cmdline(argc, argv);
	if (input_filename)
		read_eeprom_from_file(input_filename);
	else
		read_eeprom_from_stdin();
	dev = find_usbdev_by_desc_string(device_selector);
	if (!dev) {
		fprintf(stderr, "error: specified USB device not found\n");
		exit(1);
	}
	usbh = usb_open(dev);
	if (!usbh) {
		fprintf(stderr, "error: usb_open() failed\n");
		exit(1);
	}
	if (detach_num)
		usbwrap_claim_all_ifs(usbh, detach_num);
	if (ft232r_mode)
		ft232r_eeprom_magic(usbh);
	for (n = 0; n < eeprom_size; n++)
		ftmini_write_eeprom_loc(usbh, n, eeprom[n]);
	usb_close(usbh);
	exit(0);
}