ftdi_eeprom: Change handling the EEPROM size
[libftdi] / ftdi_eeprom / main.c
index 25a3a5a..151a7d3 100644 (file)
@@ -114,6 +114,7 @@ int main(int argc, char *argv[])
         CFG_BOOL("use_serial", cfg_false, 0),
         CFG_BOOL("change_usb_version", cfg_false, 0),
         CFG_INT("usb_version", 0, 0),
+        CFG_INT("default_pid", 0x6001, 0),
         CFG_INT("max_power", 0, 0),
         CFG_STR("manufacturer", "Acme Inc.", 0),
         CFG_STR("product", "USB Serial Converter", 0),
@@ -143,9 +144,8 @@ int main(int argc, char *argv[])
     */
     int _read = 0, _erase = 0, _flash = 0;
 
-    const int my_eeprom_size = 128;                 /* TODO: Kill this. Check with Uwe how we can determine the eeprom size properly
-                                                             because it's initialized with -1. Maybe assume 128 bytes per default? */
-    unsigned char eeprom_buf[my_eeprom_size];
+    int my_eeprom_size = 0;
+    unsigned char eeprom_buf[FTDI_MAX_EEPROM_SIZE];
     char *filename;
     int size_check;
     int i, argc_filename;
@@ -203,7 +203,9 @@ int main(int argc, char *argv[])
         return EXIT_FAILURE;
     }
 
-    ftdi_eeprom_initdefaults (ftdi, "Acme Inc.", "FTDI Chip", NULL);
+    ftdi_eeprom_initdefaults (ftdi, cfg_getstr(cfg, "manufacturer"), 
+                              cfg_getstr(cfg, "product"), 
+                              cfg_getstr(cfg, "serial"));
 
     eeprom_set_value(ftdi, VENDOR_ID, cfg_getint(cfg, "vendor_id"));
     eeprom_set_value(ftdi, PRODUCT_ID, cfg_getint(cfg, "product_id"));
@@ -231,9 +233,6 @@ int main(int argc, char *argv[])
     eeprom_set_value(ftdi, USB_VERSION, cfg_getint(cfg, "usb_version"));
 
 
-    ftdi->eeprom->manufacturer = cfg_getstr(cfg, "manufacturer");
-    ftdi->eeprom->product = cfg_getstr(cfg, "product");
-    ftdi->eeprom->serial = cfg_getstr(cfg, "serial");
     eeprom_set_value(ftdi, HIGH_CURRENT, cfg_getbool(cfg, "high_current"));
     eeprom_set_value(ftdi, CBUS_FUNCTION_0, str_to_cbus(cfg_getstr(cfg, "cbus0"), 13));
     eeprom_set_value(ftdi, CBUS_FUNCTION_1, str_to_cbus(cfg_getstr(cfg, "cbus1"), 13));
@@ -261,16 +260,19 @@ int main(int argc, char *argv[])
 
         if (i == 0)
         {
+            printf("FTDI read eeprom: %d\n", ftdi_read_eeprom(ftdi));
+            eeprom_get_value(ftdi, CHIP_SIZE, &my_eeprom_size);
             // TODO: Do we know the eeprom size already?
-            printf("EEPROM size: %d\n", ftdi->eeprom->size);
+            printf("EEPROM size: %d\n", my_eeprom_size);
         }
         else
         {
+            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("Error code: %d (%s)\n", i, ftdi_get_error_string(ftdi));
-            printf("Retrying with default FTDI id.\n");
+            printf("Retrying with default FTDI pid=%#04x.\n", default_pid);
 
-            i = ftdi_usb_open(ftdi, 0x0403, 0x6001);
+            i = ftdi_usb_open(ftdi, 0x0403, default_pid);
             if (i != 0)
             {
                 printf("Error: %s\n", ftdi->error_str);
@@ -281,7 +283,6 @@ int main(int argc, char *argv[])
 
     if (_read > 0)
     {
-        printf("FTDI read eeprom: %d\n", ftdi_read_eeprom(ftdi));
 
         ftdi_eeprom_decode(ftdi, 0);
         /* Debug output */
@@ -352,15 +353,14 @@ int main(int argc, char *argv[])
                 fread(eeprom_buf, 1, my_eeprom_size, fp);
                 fclose(fp);
 
-                /* TODO: Dirty hack. Create an API for this. How about ftdi_set_eeprom_buf()? */
-                memcpy(ftdi->eeprom->buf, eeprom_buf, my_eeprom_size);
+                ftdi_set_eeprom_buf(ftdi, eeprom_buf, my_eeprom_size);
             }
         }
         printf ("FTDI write eeprom: %d\n", ftdi_write_eeprom(ftdi));
     }
 
     // Write to file?
-    if (filename != NULL && strlen(filename) > 0)
+    if (filename != NULL && strlen(filename) > 0 && !cfg_getbool(cfg, "flash_raw"))
     {
         fp = fopen(filename, "w");
         if (fp == NULL)