| diff --git a/src/main.c b/src/main.c |
| index 81e6e93..972cc92 100644 |
| --- a/src/main.c |
| +++ b/src/main.c |
| @@ -21,6 +21,7 @@ |
| #include <stdlib.h> |
| #include <stdio.h> |
| #include <string.h> |
| +#include <getopt.h> |
| |
| #include <confuse.h> |
| #include <ftdi.h> |
| @@ -90,6 +91,9 @@ int main(int argc, char *argv[]) |
| normal variables |
| */ |
| int _read = 0, _erase = 0, _flash = 0; |
| + int _vendor = 0x0403, _product = 0x6011; |
| + char _serial[128] = {'\0'}; |
| + |
| unsigned char eeprom_buf[128]; |
| char *filename; |
| int size_check; |
| @@ -102,35 +106,68 @@ int main(int argc, char *argv[]) |
| printf("\nFTDI eeprom generator v%s\n", VERSION); |
| printf ("(c) Intra2net AG <opensource@intra2net.com>\n"); |
| |
| - if (argc != 2 && argc != 3) |
| + int longval = 0; |
| + 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", required_argument, &longval, 's'}, |
| + {0, 0, 0, 0} |
| + }; |
| + |
| + 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 [commands] config-file\n", argv[0]); |
| + 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("--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); |
| } |
| |
| - if (argc == 3) |
| - { |
| - if (strcmp(argv[1], "--read-eeprom") == 0) |
| - _read = 1; |
| - if (strcmp(argv[1], "--erase-eeprom") == 0) |
| - _erase = 1; |
| - if (strcmp(argv[1], "--flash-eeprom") == 0) |
| - _flash = 1; |
| - |
| - argc_filename = 2; |
| - } |
| - else |
| - { |
| - argc_filename = 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); |
| @@ -163,14 +200,16 @@ int main(int argc, char *argv[]) |
| eeprom.out_is_isochronous = cfg_getbool(cfg, "out_is_isochronous"); |
| eeprom.suspend_pull_downs = cfg_getbool(cfg, "suspend_pull_downs"); |
| |
| - eeprom.use_serial = cfg_getbool(cfg, "use_serial"); |
| + eeprom.use_serial = (_serial[0] == '\0') ? |
| + cfg_getbool(cfg, "use_serial") : 1; |
| eeprom.change_usb_version = cfg_getbool(cfg, "change_usb_version"); |
| eeprom.usb_version = cfg_getint(cfg, "usb_version"); |
| |
| |
| eeprom.manufacturer = cfg_getstr(cfg, "manufacturer"); |
| eeprom.product = cfg_getstr(cfg, "product"); |
| - eeprom.serial = cfg_getstr(cfg, "serial"); |
| + eeprom.serial = (_serial[0] == '\0') ? |
| + cfg_getstr(cfg, "serial") : _serial; |
| eeprom.high_current = cfg_getbool(cfg, "high_current"); |
| eeprom.cbus_function[0] = str_to_cbus(cfg_getstr(cfg, "cbus0"), 13); |
| eeprom.cbus_function[1] = str_to_cbus(cfg_getstr(cfg, "cbus1"), 13); |
| @@ -198,11 +237,12 @@ int main(int argc, char *argv[]) |
| } |
| else |
| { |
| - printf("Unable to find FTDI devices under given vendor/product id: 0x%X/0x%X\n", eeprom.vendor_id, eeprom.product_id); |
| - printf("Error code: %d (%s)\n", i, ftdi_get_error_string(&ftdi)); |
| - printf("Retrying with default FTDI id.\n"); |
| + printf("Unable to find FTDI devices under given id:0x%02x/0x%02x\n", |
| + eeprom.vendor_id, eeprom.product_id); |
| |
| - i = ftdi_usb_open(&ftdi, 0x0403, 0x6001); |
| + printf("Error code: %d (%s)\n", i, ftdi_get_error_string(&ftdi)); |
| + printf("Retrying with id:0x%02x/0x%02x\n", _vendor, _product); |
| + i = ftdi_usb_open(&ftdi, _vendor, _product); |
| if (i != 0) |
| { |
| printf("Error: %s\n", ftdi.error_str); |