Hi Thomas,

On 12.02.2015 22:39, Thomas Jarosch wrote:
> ...
> 
> I've reviewed and applied the 01-cbusx.patch.
> Thanks for this high quality contribution.
> 
Thank you for applying it!

> How did you generate the patch file? Using "Write commit to file" in gitk?
> Please use "git format-patch HEAD~XXX" the next time. Then I can just
> apply them via "git am" without manually copying the commit msg etc. over.
> 
No, I'm not using stupid Git GUIs :-)
Simply used `git show >foo.patch`, assuming you want to apply it like a
regular patch file.
I rebased the ftdi_eeprom "--device" patch on top of libftdi's current
master branch and I'm attaching it here again in git-format-patch style.

> Regarding the ftdi_eeprom patch: It alters the current behavior.
> If someone uses "--erase" together with "--flash", it is no longer possible.
> Imagine someone wants to flash a shorter eeprom than
> the one that's currently on the chip, then "--erase"
> is really helpful to ensure there's no garbage left.
> 
> What do you think?
> 
I'm not sure if erasing before flashing makes any sense. Is it even
possible for the EEPROM size to change on one chip?

Nevertheless, my patch does not really affect ftdi_eeprom's behaviour in
this regard as ftdi_eeprom used the following check on command-line
arguments in 1162549f which effectively limits you to performing a
single command per ftdi_eeprom invocation:

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

What the patch changes is allowing an arbitrary number of parameters so
that the optional "--device" option can be supported.

So I see no reason not to apply it.

> Cheers,
> Thomas
> 
Regards,
Robin

> 
> --
> libftdi - see http://www.intra2net.com/en/developer/libftdi for details.
> To unsubscribe send a mail to libftdi+unsubscr...@developer.intra2net.com   
> 

-- 
Robin Haberkorn
Developer

metraTec GmbH
Werner-Heisenberg-Str. 1
39106 Magdeburg
Germany

haberk...@metratec.com
www.metratec.com


--
libftdi - see http://www.intra2net.com/en/developer/libftdi for details.
To unsubscribe send a mail to libftdi+unsubscr...@developer.intra2net.com   
>From a3508896c3823d11dd9340e975d3918687a70cdc Mon Sep 17 00:00:00 2001
From: Robin Haberkorn <haberk...@metratec.com>
Date: Tue, 27 Jan 2015 22:53:41 +0100
Subject: [PATCH] ftdi_eeprom: added --device option to specify FTDI device

 * previously, the device could only be selected using the "vendor_id",
   "product_id" and "default_pid" config file options.
   This did not guarantee that a device could be uniquely identified
   (e.g. there could be multiple devices with the same VID/PID).
   Also this severely limited the possibilities of changing a device's
   VID/PID using ftdi_eeprom - this only worked if the device happened
   to have FTDI's VID 0x0403 and a PID equal to "default_pid".
 * If the --device option is omitted, ftdi_eeprom defaults to the
   old behaviour of using the config file options.
 * The order of arguments is insignificant. If multiple
   'command' options (--read-eeprom, --erase-eeprom, --flash-eeprom)
   are given, only the last one will determine the action.
---
 ftdi_eeprom/main.c | 109 ++++++++++++++++++++++++++++++++++++-----------------
 1 file changed, 75 insertions(+), 34 deletions(-)

diff --git a/ftdi_eeprom/main.c b/ftdi_eeprom/main.c
index c19d111..5799c71 100644
--- a/ftdi_eeprom/main.c
+++ b/ftdi_eeprom/main.c
@@ -145,6 +145,20 @@ static void eeprom_get_value(struct ftdi_context *ftdi, enum ftdi_eeprom_value v
     }
 }
 
+static void usage(const char *program)
+{
+    fprintf(stderr, "Syntax: %s [...options...] <config-file>\n", program);
+    fprintf(stderr, "Valid Options:\n");
+    fprintf(stderr, "--device <description>  Specify device to open by decription string. One of:\n");
+    fprintf(stderr, "         d:<devicenode>\n");
+    fprintf(stderr, "         i:<vendor>:<product>\n");
+    fprintf(stderr, "         i:<vendor>:<product>:<index>\n");
+    fprintf(stderr, "         s:<vendor>:<product>:<serial>\n");
+    fprintf(stderr, "--read-eeprom           Read eeprom and write to -filename- from config-file\n");
+    fprintf(stderr, "--erase-eeprom          Erase eeprom\n");
+    fprintf(stderr, "--flash-eeprom          Flash eeprom\n");
+}
+
 int main(int argc, char *argv[])
 {
     /*
@@ -205,14 +219,20 @@ int main(int argc, char *argv[])
     /*
     normal variables
     */
-    int _read = 0, _erase = 0, _flash = 0;
+    enum {
+        COMMAND_READ = 1,
+        COMMAND_ERASE,
+        COMMAND_FLASH
+    } command = 0;
+    const char *cfg_filename = NULL;
+    const char *device_description = NULL;
 
     const int max_eeprom_size = 256;
     int my_eeprom_size = 0;
     unsigned char *eeprom_buf = NULL;
     char *filename;
     int size_check;
-    int i, argc_filename;
+    int i;
     FILE *fp;
 
     struct ftdi_context *ftdi = NULL;
@@ -220,37 +240,46 @@ int main(int argc, char *argv[])
     printf("\nFTDI eeprom generator v%s\n", EEPROM_VERSION_STRING);
     printf ("(c) Intra2net AG and the libftdi developers <opensou...@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);
-    }
-
-    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;
+    for (i = 1; i < argc; i++) {
+        if (*argv[i] != '-')
+        {
+            cfg_filename = argv[i];
+        }
+        else if (!strcmp(argv[i], "--device"))
+        {
+            if (i+1 >= argc)
+            {
+                usage(argv[0]);
+                exit(-1);
+            }
+            device_description = argv[++i];
+        }
+        else if (!strcmp(argv[i], "--read-eeprom"))
+        {
+            command = COMMAND_READ;
+        }
+        else if (!strcmp(argv[i], "--erase-eeprom"))
+        {
+            command = COMMAND_ERASE;
+        }
+        else if (!strcmp(argv[i], "--flash-eeprom"))
+        {
+            command = COMMAND_FLASH;
+        }
         else
         {
-            printf ("Can't open configuration file\n");
-            exit (-1);
+            usage(argv[0]);
+            exit(-1);
         }
-        argc_filename = 2;
     }
-    else
+
+    if (!cfg_filename)
     {
-        argc_filename = 1;
+        usage(argv[0]);
+        exit(-1);
     }
 
-    if ((fp = fopen(argv[argc_filename], "r")) == NULL)
+    if ((fp = fopen(cfg_filename, "r")) == NULL)
     {
         printf ("Can't open configuration file\n");
         exit (-1);
@@ -258,7 +287,7 @@ int main(int argc, char *argv[])
     fclose (fp);
 
     cfg = cfg_init(opts, 0);
-    cfg_parse(cfg, argv[argc_filename]);
+    cfg_parse(cfg, cfg_filename);
     filename = cfg_getstr(cfg, "filename");
 
     if (cfg_getbool(cfg, "self_powered") && cfg_getint(cfg, "max_power") > 0)
@@ -271,7 +300,19 @@ int main(int argc, char *argv[])
         return EXIT_FAILURE;
     }
 
-    if (_read > 0 || _erase > 0 || _flash > 0)
+    if (device_description != NULL)
+    {
+        i = ftdi_usb_open_string(ftdi, device_description);
+
+        if (i != 0)
+        {
+            printf("Unable to find FTDI device with description: %s\n",
+                   device_description);
+            printf("Error code: %d (%s)\n", i, ftdi_get_error_string(ftdi));
+            exit (-1);
+        }
+    }
+    else if (command > 0)
     {
         int vendor_id = cfg_getint(cfg, "vendor_id");
         int product_id = cfg_getint(cfg, "product_id");
@@ -301,7 +342,7 @@ int main(int argc, char *argv[])
     eeprom_get_value(ftdi, CHIP_SIZE, &my_eeprom_size);
     printf("EEPROM size: %d\n", my_eeprom_size);
 
-    if (_read > 0)
+    if (command == COMMAND_READ)
     {
         ftdi_eeprom_decode(ftdi, 0 /* debug: 1 */);
 
@@ -414,7 +455,7 @@ int main(int argc, char *argv[])
     eeprom_set_value(ftdi, CHANNEL_C_RS485, 0);
     eeprom_set_value(ftdi, CHANNEL_D_RS485, 0);
 
-    if (_erase > 0)
+    if (command == COMMAND_ERASE)
     {
         printf("FTDI erase eeprom: %d\n", ftdi_erase_eeprom(ftdi));
     }
@@ -424,20 +465,20 @@ int main(int argc, char *argv[])
 
     if (size_check == -1)
     {
-        printf ("Sorry, the eeprom can only contain 128 bytes (100 bytes for your strings).\n");
-        printf ("You need to short your string by: %d bytes\n", size_check);
+        printf ("Sorry, the eeprom can only contain 128 bytes.\n");
         goto cleanup;
     }
     else if (size_check < 0)
     {
         printf ("ftdi_eeprom_build(): error: %d\n", size_check);
+        goto cleanup;
     }
     else
     {
         printf ("Used eeprom space: %d bytes\n", my_eeprom_size-size_check);
     }
 
-    if (_flash > 0)
+    if (command == COMMAND_FLASH)
     {
         if (cfg_getbool(cfg, "flash_raw"))
         {
@@ -488,7 +529,7 @@ int main(int argc, char *argv[])
 cleanup:
     if (eeprom_buf)
         free(eeprom_buf);
-    if (_read > 0 || _erase > 0 || _flash > 0)
+    if (command > 0)
     {
         printf("FTDI close: %d\n", ftdi_usb_close(ftdi));
     }
-- 
1.9.1

Reply via email to