Try to inhibit programming EEPROM with data for a different device and zero EEPROM...
[libftdi] / src / ftdi.c
index 1779046..1616fdb 100644 (file)
@@ -63,6 +63,8 @@ static void ftdi_usb_close_internal (struct ftdi_context *ftdi)
     {
         libusb_close (ftdi->usb_dev);
         ftdi->usb_dev = NULL;
+        if(ftdi->eeprom)
+            ftdi->eeprom->initialized_for_connected_device = 0;
     }
 }
 
@@ -2177,7 +2179,7 @@ int ftdi_set_error_char(struct ftdi_context *ftdi,
 }
 
 /**
-    Init eeprom with default values.
+    Init eeprom with default values for the connected device
     \param ftdi pointer to ftdi_context
     \param manufacturer String to use as Manufacturer
     \param product String to use as Product description
@@ -2186,6 +2188,7 @@ int ftdi_set_error_char(struct ftdi_context *ftdi,
     \retval  0: all fine
     \retval -1: No struct ftdi_context
     \retval -2: No struct ftdi_eeprom
+    \retval -3: No connected device or device not yet opened
 */
 int ftdi_eeprom_initdefaults(struct ftdi_context *ftdi, char * manufacturer,
                              char * product, char * serial)
@@ -2201,6 +2204,9 @@ int ftdi_eeprom_initdefaults(struct ftdi_context *ftdi, char * manufacturer,
     eeprom = ftdi->eeprom;
     memset(eeprom, 0, sizeof(struct ftdi_eeprom));
 
+    if (ftdi->usb_dev == NULL)
+        ftdi_error_return(-3, "No connected device or device not yet opened");
+
     eeprom->vendor_id = 0x0403;
     eeprom->use_serial = USE_SERIAL_NUM;
     if ((ftdi->type == TYPE_AM) || (ftdi->type == TYPE_BM) ||
@@ -2269,6 +2275,7 @@ int ftdi_eeprom_initdefaults(struct ftdi_context *ftdi, char * manufacturer,
         }
         eeprom->size = -1;
     }
+    eeprom->initialized_for_connected_device = 1;
     return 0;
 }
 /*FTD2XX doesn't check for values not fitting in the ACBUS Signal oprtions*/
@@ -3755,6 +3762,7 @@ int ftdi_write_eeprom_location(struct ftdi_context *ftdi, int eeprom_addr,
     \retval  0: all fine
     \retval -1: read failed
     \retval -2: USB device unavailable
+    \retval -3: EEPROM not initialized for the connected device;
 */
 int ftdi_write_eeprom(struct ftdi_context *ftdi)
 {
@@ -3764,6 +3772,10 @@ int ftdi_write_eeprom(struct ftdi_context *ftdi)
 
     if (ftdi == NULL || ftdi->usb_dev == NULL)
         ftdi_error_return(-2, "USB device unavailable");
+
+    if(ftdi->eeprom->initialized_for_connected_device == 0)
+        ftdi_error_return(-3, "EEPROM not initialized for the connected device");
+
     eeprom = ftdi->eeprom->buf;
 
     /* These commands were traced while running MProg */