ftdi_eeprom/main.c: Malloc the eeprom Buffer with the size exported by the API
[libftdi] / ftdi_eeprom / main.c
index a765d8c..931045f 100644 (file)
@@ -145,7 +145,7 @@ int main(int argc, char *argv[])
     int _read = 0, _erase = 0, _flash = 0;
 
     int my_eeprom_size = 0;
-    unsigned char eeprom_buf[FTDI_MAX_EEPROM_SIZE];
+    unsigned char *eeprom_buf = NULL;
     char *filename;
     int size_check;
     int i, argc_filename;
@@ -214,11 +214,7 @@ int main(int argc, char *argv[])
 
         i = ftdi_usb_open(ftdi, vendor_id, product_id);
 
-        if (i == 0)
-        {
-            printf("FTDI read eeprom: %d\n", ftdi_read_eeprom(ftdi));
-        }
-        else
+        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);
@@ -232,11 +228,16 @@ int main(int argc, char *argv[])
                 exit (-1);
             }
         }
-        eeprom_get_value(ftdi, CHIP_SIZE, &my_eeprom_size);
-        // TODO: Do we know the eeprom size already?
-        printf("EEPROM size: %d\n", my_eeprom_size);
     }
-
+    ftdi_eeprom_initdefaults (ftdi, cfg_getstr(cfg, "manufacturer"), 
+                              cfg_getstr(cfg, "product"), 
+                              cfg_getstr(cfg, "serial"));
+    
+    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", my_eeprom_size);
+    
     if (_read > 0)
     {
 
@@ -263,9 +264,16 @@ int main(int argc, char *argv[])
         printf("serial = \"%s\"\n", eeprom->serial);
         */
 
+        eeprom_buf = malloc(my_eeprom_size);
+        ftdi_get_eeprom_buf(ftdi, eeprom_buf, my_eeprom_size);
+
+        if (eeprom_buf == NULL)
+        {
+            fprintf(stderr, "Malloc failed, aborting\n");
+            goto cleanup;
+        }
         if (filename != NULL && strlen(filename) > 0)
         {
-            ftdi_get_eeprom_buf(ftdi, eeprom_buf, my_eeprom_size);
 
             FILE *fp = fopen (filename, "wb");
             fwrite (eeprom_buf, 1, my_eeprom_size, fp);
@@ -279,23 +287,9 @@ int main(int argc, char *argv[])
         goto cleanup;
     }
 
-    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"));
 
-    // TODO: Support all chip types
-    char *type = cfg_getstr(cfg, "chip_type");
-    if (!strcmp(type, "BM")) {
-        ftdi->type = TYPE_BM;
-    } else if (!strcmp(type, "R")) {
-        ftdi->type = TYPE_R;
-    } else {
-        ftdi->type = TYPE_AM;
-    }
-
     eeprom_set_value(ftdi, SELF_POWERED, cfg_getbool(cfg, "self_powered"));
     eeprom_set_value(ftdi, REMOTE_WAKEUP, cfg_getbool(cfg, "remote_wakeup"));
     eeprom_set_value(ftdi, MAX_POWER, cfg_getint(cfg, "max_power"));
@@ -381,6 +375,8 @@ int main(int argc, char *argv[])
     }
 
 cleanup:
+    if (eeprom_buf)
+        free(eeprom_buf);
     if (_read > 0 || _erase > 0 || _flash > 0)
     {
         printf("FTDI close: %d\n", ftdi_usb_close(ftdi));