| libftdi: Patch ftdi_eeprom to support Google FTDI based debug boards. |
| |
| Patch allows user to change default vid/pid of FTDI (0403:60xx) to |
| any vid:pid via command line arguments (-v & -p). |
| Additionally patch allows caller to set the USB serialname from the |
| command line (-s). |
| |
| See crbug.com/242980 for details |
| diff --git a/ftdi_eeprom/main.c b/ftdi_eeprom/main.c |
| index 5e28034..9fac7e4 100644 |
| --- a/ftdi_eeprom/main.c |
| +++ b/ftdi_eeprom/main.c |
| @@ -35,6 +35,7 @@ |
| #include <stdlib.h> |
| #include <stdio.h> |
| #include <string.h> |
| +#include <getopt.h> |
| |
| #include <confuse.h> |
| #include <libusb.h> |
| @@ -143,8 +144,10 @@ int main(int argc, char *argv[]) |
| normal variables |
| */ |
| int _read = 0, _erase = 0, _flash = 0; |
| + unsigned int _vendor = 0, _product = 0; |
| |
| const int max_eeprom_size = 256; |
| + char _serial[max_eeprom_size]; |
| int my_eeprom_size = 0; |
| unsigned char *eeprom_buf = NULL; |
| char *filename; |
| @@ -157,39 +160,67 @@ int main(int argc, char *argv[]) |
| printf("\nFTDI eeprom generator v%s\n", EEPROM_VERSION_STRING); |
| printf ("(c) Intra2net AG and the libftdi developers <opensource@intra2net.com>\n"); |
| |
| - if (argc != 2 && argc != 3) |
| - { |
| - printf("Syntax: %s [commands] config-file\n", argv[0]); |
| - printf("Valid commands:\n"); |
| - printf("--read-eeprom Read eeprom and write to -filename- from config-file\n"); |
| - printf("--erase-eeprom Erase eeprom\n"); |
| - printf("--flash-eeprom Flash eeprom\n"); |
| - exit (-1); |
| - } |
| + int longval; |
| + struct option long_options[] = { |
| + {"read-eeprom", no_argument, &_read, 1}, |
| + {"erase-eeprom", no_argument, &_erase, 1}, |
| + {"flash-eeprom", no_argument, &_flash, 1}, |
| + {"vendor", required_argument, &longval, 'v'}, |
| + {"product", required_argument, &longval, 'p'}, |
| + {"serial", optional_argument, &longval, 's'}, |
| + {0, 0, 0, 0} |
| + }; |
| |
| - if (argc == 3) |
| - { |
| - if (strcmp(argv[1], "--read-eeprom") == 0) |
| - _read = 1; |
| - else if (strcmp(argv[1], "--erase-eeprom") == 0) |
| - _erase = 1; |
| - else if (strcmp(argv[1], "--flash-eeprom") == 0) |
| - _flash = 1; |
| - else |
| - { |
| - printf ("Can't open configuration file\n"); |
| - exit (-1); |
| - } |
| - argc_filename = 2; |
| - } |
| - else |
| - { |
| - argc_filename = 1; |
| + int option_index = 0; |
| + char c; |
| + while ((c = getopt_long(argc, argv, "v:p:s:", |
| + long_options, &option_index)) != -1) { |
| + switch (c) { |
| + case 'v': |
| + _vendor = strtoul(optarg, NULL, 0); |
| + break; |
| + case 'p': |
| + _product = strtoul(optarg, NULL, 0); |
| + break; |
| + case 's': |
| + strcpy(_serial, optarg); |
| + break; |
| + case 0: |
| + switch (longval) { |
| + case 'v': |
| + _vendor = strtoul(optarg, NULL, 0); |
| + break; |
| + case 'p': |
| + _product = strtoul(optarg, NULL, 0); |
| + break; |
| + case 's': |
| + strcpy(_serial, optarg); |
| + break; |
| + default: |
| + break; |
| + } |
| + default: |
| + break; |
| + } |
| } |
| |
| + if (!_read & !_erase & !_flash) |
| + { |
| + printf("Syntax: %s [switches|command] config-file\n", argv[0]); |
| + printf("Valid commands:\n"); |
| + printf("--read-eeprom Read eeprom and write to -filename- from config-file\n"); |
| + printf("--erase-eeprom Erase eeprom\n"); |
| + printf("--flash-eeprom Flash eeprom\n"); |
| + printf("--vendor|-v <num> Vendor id to probe for on USB\n"); |
| + printf("--product|-p <num> Product id to probe for on USB\n"); |
| + printf("--serial|-s <string> Serial string to override\n"); |
| + exit (-1); |
| + } |
| + argc_filename = optind; |
| + |
| if ((fp = fopen(argv[argc_filename], "r")) == NULL) |
| { |
| - printf ("Can't open configuration file\n"); |
| + printf ("Can't open configuration file %s\n", argv[argc_filename]); |
| exit (-1); |
| } |
| fclose (fp); |
| @@ -217,22 +248,26 @@ int main(int argc, char *argv[]) |
| |
| if (i != 0) |
| { |
| - int default_pid = cfg_getint(cfg, "default_pid"); |
| - printf("Unable to find FTDI devices under given vendor/product id: 0x%X/0x%X\n", vendor_id, product_id); |
| + printf("Unable to find FTDI devices under given vendor/product id: 0x%04x/0x%04x\n", |
| + vendor_id, product_id); |
| printf("Error code: %d (%s)\n", i, ftdi_get_error_string(ftdi)); |
| - printf("Retrying with default FTDI pid=%#04x.\n", default_pid); |
| - |
| - i = ftdi_usb_open(ftdi, 0x0403, default_pid); |
| + printf("Retrying with id:0x%04x/0x%04x\n", _vendor, _product); |
| + i = ftdi_usb_open(ftdi, _vendor, _product); |
| if (i != 0) |
| { |
| printf("Error: %s\n", ftdi->error_str); |
| exit (-1); |
| } |
| + vendor_id = _vendor; |
| + product_id = _product; |
| } |
| + printf("Found device at id:0x%04x/0x%04x\n", |
| + vendor_id, product_id); |
| } |
| ftdi_eeprom_initdefaults (ftdi, cfg_getstr(cfg, "manufacturer"), |
| cfg_getstr(cfg, "product"), |
| - cfg_getstr(cfg, "serial")); |
| + (_serial[0] == '\0') ? |
| + cfg_getstr(cfg, "serial") : _serial); |
| |
| printf("FTDI read eeprom: %d\n", ftdi_read_eeprom(ftdi)); |
| eeprom_get_value(ftdi, CHIP_SIZE, &my_eeprom_size); |
| @@ -276,7 +311,8 @@ int main(int argc, char *argv[]) |
| eeprom_set_value(ftdi, OUT_IS_ISOCHRONOUS, cfg_getbool(cfg, "out_is_isochronous")); |
| eeprom_set_value(ftdi, SUSPEND_PULL_DOWNS, cfg_getbool(cfg, "suspend_pull_downs")); |
| |
| - eeprom_set_value(ftdi, USE_SERIAL, cfg_getbool(cfg, "use_serial")); |
| + int use_serial = (_serial[0] == '\0') ? cfg_getbool(cfg, "use_serial") : 1; |
| + eeprom_set_value(ftdi, USE_SERIAL, use_serial); |
| eeprom_set_value(ftdi, USE_USB_VERSION, cfg_getbool(cfg, "change_usb_version")); |
| eeprom_set_value(ftdi, USB_VERSION, cfg_getint(cfg, "usb_version")); |
| eeprom_set_value(ftdi, CHIP_TYPE, cfg_getint(cfg, "eeprom_type")); |