From 44f41f11993945b0129741e39cef96d9f9cf10b6 Mon Sep 17 00:00:00 2001 From: Uwe Bonnes Date: Sat, 2 Jul 2011 13:34:00 +0200 Subject: [PATCH] Try to inhibit programming EEPROM with data for a different device and zero EEPROM memory image early --- src/ftdi.c | 14 +++++++++++++- src/ftdi.h | 4 ++++ 2 files changed, 17 insertions(+), 1 deletions(-) diff --git a/src/ftdi.c b/src/ftdi.c index 1779046..1616fdb 100644 --- a/src/ftdi.c +++ b/src/ftdi.c @@ -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 */ diff --git a/src/ftdi.h b/src/ftdi.h index 02fa838..20ecc26 100644 --- a/src/ftdi.h +++ b/src/ftdi.h @@ -200,6 +200,10 @@ struct ftdi_eeprom int vendor_id; /** product id */ int product_id; + + /** Was the eeprom structure initialized for the actual + connected device? **/ + int initialized_for_connected_device; /** self powered */ int self_powered; -- 1.7.1