1 /***************************************************************************
4 begin : Mon Apr 7 12:05:22 CEST 2003
5 copyright : (C) 2003-2014 by Intra2net AG and the libftdi developers
6 email : opensource@intra2net.com
7 ***************************************************************************/
9 /***************************************************************************
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the GNU General Public License version 2 as *
13 * published by the Free Software Foundation. *
15 ***************************************************************************/
19 - Merge Uwe's eeprom tool. Current features:
20 - Init eeprom defaults based upon eeprom type
21 - Read -> Already there
22 - Write -> Already there
23 - Erase -> Already there
25 - Ability to find device by PID/VID, product name or serial
28 - Out-of-the-box compatibility with FTDI's eeprom tool configuration files
43 #include <ftdi_eeprom_version.h>
45 static int parse_cbus(cfg_t *cfg, cfg_opt_t *opt, const char *value, void *result)
47 static const char* options[] =
49 "TXDEN", "PWREN", "RXLED", "TXLED", "TXRXLED", "SLEEP", "CLK48",
50 "CLK24", "CLK12", "CLK6", "IOMODE", "BB_WR", "BB_RD"
54 for (i=0; i<sizeof(options)/sizeof(*options); i++)
56 if (!(strcmp(options[i], value)))
63 cfg_error(cfg, "Invalid %s option '%s'", cfg_opt_name(opt), value);
67 static int parse_cbush(cfg_t *cfg, cfg_opt_t *opt, const char *value, void *result)
69 static const char* options[] =
71 "TRISTATE", "TXLED", "RXLED", "TXRXLED", "PWREN", "SLEEP",
72 "DRIVE_0", "DRIVE1", "IOMODE", "TXDEN", "CLK30", "CLK15", "CLK7_5"
76 for (i=0; i<sizeof(options)/sizeof(*options); i++)
78 if (!(strcmp(options[i], value)))
85 cfg_error(cfg, "Invalid %s option '%s'", cfg_opt_name(opt), value);
89 static int parse_cbusx(cfg_t *cfg, cfg_opt_t *opt, const char *value, void *result)
91 static const char* options[] =
93 "TRISTATE", "TXLED", "RXLED", "TXRXLED", "PWREN", "SLEEP",
94 "DRIVE_0", "DRIVE1", "IOMODE", "TXDEN", "CLK24", "CLK12",
95 "CLK6", "BAT_DETECT", "BAT_DETECT_NEG", "I2C_TXE", "I2C_RXF", "VBUS_SENSE",
96 "BB_WR", "BB_RD", "TIME_STAMP", "AWAKE"
100 for (i=0; i<sizeof(options)/sizeof(*options); i++)
102 if (!(strcmp(options[i], value)))
109 cfg_error(cfg, "Invalid %s option '%s'", cfg_opt_name(opt), value);
113 static int parse_chtype(cfg_t *cfg, cfg_opt_t *opt, const char *value, void *result)
121 { "UART", CHANNEL_IS_UART },
122 { "FIFO", CHANNEL_IS_FIFO },
123 { "OPTO", CHANNEL_IS_OPTO },
124 { "CPU", CHANNEL_IS_CPU },
125 { "FT1284", CHANNEL_IS_FT1284}
129 for (i=0; i<sizeof(options)/sizeof(*options); i++)
131 if (!(strcmp(options[i].key, value)))
133 *(int *)result = options[i].opt;
138 cfg_error(cfg, "Invalid %s option '%s'", cfg_opt_name(opt), value);
143 * @brief Set eeprom value
145 * \param ftdi pointer to ftdi_context
146 * \param value_name Enum of the value to set
147 * \param value Value to set
149 * Function will abort the program on error
151 static void eeprom_set_value(struct ftdi_context *ftdi, enum ftdi_eeprom_value value_name, int value)
153 if (ftdi_set_eeprom_value(ftdi, value_name, value) < 0)
155 printf("Unable to set eeprom value %d: %s. Aborting\n", value_name, ftdi_get_error_string(ftdi));
161 * @brief Get eeprom value
163 * \param ftdi pointer to ftdi_context
164 * \param value_name Enum of the value to get
165 * \param value Value to get
167 * Function will abort the program on error
169 static void eeprom_get_value(struct ftdi_context *ftdi, enum ftdi_eeprom_value value_name, int *value)
171 if (ftdi_get_eeprom_value(ftdi, value_name, value) < 0)
173 printf("Unable to get eeprom value %d: %s. Aborting\n", value_name, ftdi_get_error_string(ftdi));
178 static void usage(const char *program)
180 fprintf(stderr, "Syntax: %s [...options...] <config-file>\n", program);
181 fprintf(stderr, "Valid Options:\n");
182 fprintf(stderr, "--device <description> Specify device to open by description string. One of:\n");
183 fprintf(stderr, " d:<devicenode>\n");
184 fprintf(stderr, " i:<vendor>:<product>\n");
185 fprintf(stderr, " i:<vendor>:<product>:<index>\n");
186 fprintf(stderr, " s:<vendor>:<product>:<serial>\n");
187 fprintf(stderr, "--read-eeprom Read eeprom and write to -filename- from config-file\n");
188 fprintf(stderr, "--erase-eeprom Erase eeprom\n");
189 fprintf(stderr, "--flash-eeprom Flash eeprom\n");
192 int main(int argc, char *argv[])
195 configuration options
199 CFG_INT("vendor_id", 0, 0),
200 CFG_INT("product_id", 0, 0),
201 CFG_BOOL("self_powered", cfg_true, 0),
202 CFG_BOOL("remote_wakeup", cfg_true, 0),
203 CFG_BOOL("in_is_isochronous", cfg_false, 0),
204 CFG_BOOL("out_is_isochronous", cfg_false, 0),
205 CFG_BOOL("suspend_pull_downs", cfg_false, 0),
206 CFG_BOOL("use_serial", cfg_false, 0),
207 CFG_BOOL("change_usb_version", cfg_false, 0),
208 CFG_INT("usb_version", 0, 0),
209 CFG_INT("default_pid", 0x6001, 0),
210 CFG_INT("max_power", 0, 0),
211 CFG_STR("manufacturer", "Acme Inc.", 0),
212 CFG_STR("product", "USB Serial Converter", 0),
213 CFG_STR("serial", "08-15", 0),
214 CFG_INT("eeprom_type", 0x00, 0),
215 CFG_STR("filename", "", 0),
216 CFG_BOOL("flash_raw", cfg_false, 0),
217 CFG_BOOL("high_current", cfg_false, 0),
218 CFG_INT_CB("cbus0", -1, 0, parse_cbus),
219 CFG_INT_CB("cbus1", -1, 0, parse_cbus),
220 CFG_INT_CB("cbus2", -1, 0, parse_cbus),
221 CFG_INT_CB("cbus3", -1, 0, parse_cbus),
222 CFG_INT_CB("cbus4", -1, 0, parse_cbus),
223 CFG_INT_CB("cbush0", -1, 0, parse_cbush),
224 CFG_INT_CB("cbush1", -1, 0, parse_cbush),
225 CFG_INT_CB("cbush2", -1, 0, parse_cbush),
226 CFG_INT_CB("cbush3", -1, 0, parse_cbush),
227 CFG_INT_CB("cbush4", -1, 0, parse_cbush),
228 CFG_INT_CB("cbush5", -1, 0, parse_cbush),
229 CFG_INT_CB("cbush6", -1, 0, parse_cbush),
230 CFG_INT_CB("cbush7", -1, 0, parse_cbush),
231 CFG_INT_CB("cbush8", -1, 0, parse_cbush),
232 CFG_INT_CB("cbush9", -1, 0, parse_cbush),
233 CFG_INT_CB("cbusx0", -1, 0, parse_cbusx),
234 CFG_INT_CB("cbusx1", -1, 0, parse_cbusx),
235 CFG_INT_CB("cbusx2", -1, 0, parse_cbusx),
236 CFG_INT_CB("cbusx3", -1, 0, parse_cbusx),
237 CFG_BOOL("invert_txd", cfg_false, 0),
238 CFG_BOOL("invert_rxd", cfg_false, 0),
239 CFG_BOOL("invert_rts", cfg_false, 0),
240 CFG_BOOL("invert_cts", cfg_false, 0),
241 CFG_BOOL("invert_dtr", cfg_false, 0),
242 CFG_BOOL("invert_dsr", cfg_false, 0),
243 CFG_BOOL("invert_dcd", cfg_false, 0),
244 CFG_BOOL("invert_ri", cfg_false, 0),
245 CFG_INT_CB("cha_type", -1, 0, parse_chtype),
246 CFG_INT_CB("chb_type", -1, 0, parse_chtype),
247 CFG_BOOL("cha_vcp", cfg_true, 0),
248 CFG_BOOL("chb_vcp", cfg_true, 0),
249 CFG_BOOL("chc_vcp", cfg_true, 0),
250 CFG_BOOL("chd_vcp", cfg_true, 0),
251 CFG_BOOL("cha_rs485", cfg_false, 0),
252 CFG_BOOL("chb_rs485", cfg_false, 0),
253 CFG_BOOL("chc_rs485", cfg_false, 0),
254 CFG_BOOL("chd_rs485", cfg_false, 0),
255 CFG_FUNC("include", &cfg_include),
268 const char *cfg_filename = NULL;
269 const char *device_description = NULL;
271 const int max_eeprom_size = 256;
272 int my_eeprom_size = 0;
273 unsigned char *eeprom_buf = NULL;
279 struct ftdi_context *ftdi = NULL;
281 printf("\nFTDI eeprom generator v%s\n", EEPROM_VERSION_STRING);
282 printf ("(c) Intra2net AG and the libftdi developers <opensource@intra2net.com>\n");
284 for (i = 1; i < argc; i++) {
287 cfg_filename = argv[i];
289 else if (!strcmp(argv[i], "--device"))
296 device_description = argv[++i];
298 else if (!strcmp(argv[i], "--read-eeprom"))
300 command = COMMAND_READ;
302 else if (!strcmp(argv[i], "--erase-eeprom"))
304 command = COMMAND_ERASE;
306 else if (!strcmp(argv[i], "--flash-eeprom"))
308 command = COMMAND_FLASH;
323 if ((fp = fopen(cfg_filename, "r")) == NULL)
325 printf ("Can't open configuration file\n");
330 cfg = cfg_init(opts, 0);
331 cfg_parse(cfg, cfg_filename);
332 filename = cfg_getstr(cfg, "filename");
334 if (cfg_getbool(cfg, "self_powered") && cfg_getint(cfg, "max_power") > 0)
335 printf("Hint: Self powered devices should have a max_power setting of 0.\n");
337 if ((ftdi = ftdi_new()) == 0)
339 fprintf(stderr, "Failed to allocate ftdi structure :%s \n",
340 ftdi_get_error_string(ftdi));
344 if (device_description != NULL)
346 i = ftdi_usb_open_string(ftdi, device_description);
350 printf("Unable to find FTDI device with description: %s\n",
352 printf("Error code: %d (%s)\n", i, ftdi_get_error_string(ftdi));
356 else if (command > 0)
358 int vendor_id = cfg_getint(cfg, "vendor_id");
359 int product_id = cfg_getint(cfg, "product_id");
361 i = ftdi_usb_open(ftdi, vendor_id, product_id);
365 int default_pid = cfg_getint(cfg, "default_pid");
366 printf("Unable to find FTDI devices under given vendor/product id: 0x%X/0x%X\n", vendor_id, product_id);
367 printf("Error code: %d (%s)\n", i, ftdi_get_error_string(ftdi));
368 printf("Retrying with default FTDI pid=%#04x.\n", default_pid);
370 i = ftdi_usb_open(ftdi, 0x0403, default_pid);
373 printf("Error: %s\n", ftdi->error_str);
378 ftdi_eeprom_initdefaults (ftdi, cfg_getstr(cfg, "manufacturer"),
379 cfg_getstr(cfg, "product"),
380 cfg_getstr(cfg, "serial"));
382 printf("FTDI read eeprom: %d\n", ftdi_read_eeprom(ftdi));
383 eeprom_get_value(ftdi, CHIP_SIZE, &my_eeprom_size);
384 printf("EEPROM size: %d\n", my_eeprom_size);
386 if (command == COMMAND_READ)
388 ftdi_eeprom_decode(ftdi, 0 /* debug: 1 */);
390 eeprom_buf = malloc(my_eeprom_size);
391 ftdi_get_eeprom_buf(ftdi, eeprom_buf, my_eeprom_size);
393 if (eeprom_buf == NULL)
395 fprintf(stderr, "Malloc failed, aborting\n");
398 if (filename != NULL && strlen(filename) > 0)
400 FILE *fp = fopen (filename, "wb");
404 fwrite(eeprom_buf, 1, my_eeprom_size, fp);
408 fprintf(stderr, "Could not open output file %s: %s\n", filename, strerror(errno));
412 printf("Warning: Not writing eeprom, you must supply a valid filename\n");
418 eeprom_set_value(ftdi, VENDOR_ID, cfg_getint(cfg, "vendor_id"));
419 eeprom_set_value(ftdi, PRODUCT_ID, cfg_getint(cfg, "product_id"));
421 eeprom_set_value(ftdi, SELF_POWERED, cfg_getbool(cfg, "self_powered"));
422 eeprom_set_value(ftdi, REMOTE_WAKEUP, cfg_getbool(cfg, "remote_wakeup"));
423 eeprom_set_value(ftdi, MAX_POWER, cfg_getint(cfg, "max_power"));
425 eeprom_set_value(ftdi, IN_IS_ISOCHRONOUS, cfg_getbool(cfg, "in_is_isochronous"));
426 eeprom_set_value(ftdi, OUT_IS_ISOCHRONOUS, cfg_getbool(cfg, "out_is_isochronous"));
427 eeprom_set_value(ftdi, SUSPEND_PULL_DOWNS, cfg_getbool(cfg, "suspend_pull_downs"));
429 eeprom_set_value(ftdi, USE_SERIAL, cfg_getbool(cfg, "use_serial"));
430 eeprom_set_value(ftdi, USE_USB_VERSION, cfg_getbool(cfg, "change_usb_version"));
431 eeprom_set_value(ftdi, USB_VERSION, cfg_getint(cfg, "usb_version"));
432 eeprom_set_value(ftdi, CHIP_TYPE, cfg_getint(cfg, "eeprom_type"));
434 eeprom_set_value(ftdi, HIGH_CURRENT, cfg_getbool(cfg, "high_current"));
436 if (ftdi->type == TYPE_R)
438 if (cfg_getint(cfg, "cbus0") != -1)
439 eeprom_set_value(ftdi, CBUS_FUNCTION_0, cfg_getint(cfg, "cbus0"));
440 if (cfg_getint(cfg, "cbus1") != -1)
441 eeprom_set_value(ftdi, CBUS_FUNCTION_1, cfg_getint(cfg, "cbus1"));
442 if (cfg_getint(cfg, "cbus2") != -1)
443 eeprom_set_value(ftdi, CBUS_FUNCTION_2, cfg_getint(cfg, "cbus2"));
444 if (cfg_getint(cfg, "cbus3") != -1)
445 eeprom_set_value(ftdi, CBUS_FUNCTION_3, cfg_getint(cfg, "cbus3"));
446 if (cfg_getint(cfg, "cbus4") != -1)
447 eeprom_set_value(ftdi, CBUS_FUNCTION_4, cfg_getint(cfg, "cbus4"));
449 else if (ftdi->type == TYPE_232H)
451 if (cfg_getint(cfg, "cbush0") != -1)
452 eeprom_set_value(ftdi, CBUS_FUNCTION_0, cfg_getint(cfg, "cbush0"));
453 if (cfg_getint(cfg, "cbush1") != -1)
454 eeprom_set_value(ftdi, CBUS_FUNCTION_1, cfg_getint(cfg, "cbush1"));
455 if (cfg_getint(cfg, "cbush2") != -1)
456 eeprom_set_value(ftdi, CBUS_FUNCTION_2, cfg_getint(cfg, "cbush2"));
457 if (cfg_getint(cfg, "cbush3") != -1)
458 eeprom_set_value(ftdi, CBUS_FUNCTION_3, cfg_getint(cfg, "cbush3"));
459 if (cfg_getint(cfg, "cbush4") != -1)
460 eeprom_set_value(ftdi, CBUS_FUNCTION_4, cfg_getint(cfg, "cbush4"));
461 if (cfg_getint(cfg, "cbush5") != -1)
462 eeprom_set_value(ftdi, CBUS_FUNCTION_5, cfg_getint(cfg, "cbush5"));
463 if (cfg_getint(cfg, "cbush6") != -1)
464 eeprom_set_value(ftdi, CBUS_FUNCTION_6, cfg_getint(cfg, "cbush6"));
465 if (cfg_getint(cfg, "cbush7") != -1)
466 eeprom_set_value(ftdi, CBUS_FUNCTION_7, cfg_getint(cfg, "cbush7"));
467 if (cfg_getint(cfg, "cbush8") != -1)
468 eeprom_set_value(ftdi, CBUS_FUNCTION_8, cfg_getint(cfg, "cbush8"));
469 if (cfg_getint(cfg, "cbush9") != -1)
470 eeprom_set_value(ftdi, CBUS_FUNCTION_9, cfg_getint(cfg, "cbush9"));
472 else if (ftdi->type == TYPE_230X)
474 if (cfg_getint(cfg, "cbusx0") != -1)
475 eeprom_set_value(ftdi, CBUS_FUNCTION_0, cfg_getint(cfg, "cbusx0"));
476 if (cfg_getint(cfg, "cbusx1") != -1)
477 eeprom_set_value(ftdi, CBUS_FUNCTION_1, cfg_getint(cfg, "cbusx1"));
478 if (cfg_getint(cfg, "cbusx2") != -1)
479 eeprom_set_value(ftdi, CBUS_FUNCTION_2, cfg_getint(cfg, "cbusx2"));
480 if (cfg_getint(cfg, "cbusx3") != -1)
481 eeprom_set_value(ftdi, CBUS_FUNCTION_3, cfg_getint(cfg, "cbusx3"));
485 if (cfg_getbool(cfg, "invert_rxd")) invert |= INVERT_RXD;
486 if (cfg_getbool(cfg, "invert_txd")) invert |= INVERT_TXD;
487 if (cfg_getbool(cfg, "invert_rts")) invert |= INVERT_RTS;
488 if (cfg_getbool(cfg, "invert_cts")) invert |= INVERT_CTS;
489 if (cfg_getbool(cfg, "invert_dtr")) invert |= INVERT_DTR;
490 if (cfg_getbool(cfg, "invert_dsr")) invert |= INVERT_DSR;
491 if (cfg_getbool(cfg, "invert_dcd")) invert |= INVERT_DCD;
492 if (cfg_getbool(cfg, "invert_ri")) invert |= INVERT_RI;
493 eeprom_set_value(ftdi, INVERT, invert);
495 if (cfg_getint(cfg, "cha_type") != -1)
496 eeprom_set_value(ftdi, CHANNEL_A_TYPE, cfg_getint(cfg, "cha_type"));
497 if (cfg_getint(cfg, "chb_type") != -1)
498 eeprom_set_value(ftdi, CHANNEL_B_TYPE, cfg_getint(cfg, "chb_type"));
500 eeprom_set_value(ftdi, CHANNEL_A_DRIVER,
501 cfg_getbool(cfg, "cha_vcp") ? DRIVER_VCP : 0);
502 eeprom_set_value(ftdi, CHANNEL_B_DRIVER,
503 cfg_getbool(cfg, "chb_vcp") ? DRIVER_VCP : 0);
504 eeprom_set_value(ftdi, CHANNEL_C_DRIVER,
505 cfg_getbool(cfg, "chc_vcp") ? DRIVER_VCP : 0);
506 eeprom_set_value(ftdi, CHANNEL_D_DRIVER,
507 cfg_getbool(cfg, "chd_vcp") ? DRIVER_VCP : 0);
509 eeprom_set_value(ftdi, CHANNEL_A_RS485, cfg_getbool(cfg, "cha_rs485"));
510 eeprom_set_value(ftdi, CHANNEL_B_RS485, cfg_getbool(cfg, "chb_rs485"));
511 eeprom_set_value(ftdi, CHANNEL_C_RS485, cfg_getbool(cfg, "chc_rs485"));
512 eeprom_set_value(ftdi, CHANNEL_D_RS485, cfg_getbool(cfg, "chd_rs485"));
514 if (command == COMMAND_ERASE)
516 printf("FTDI erase eeprom: %d\n", ftdi_erase_eeprom(ftdi));
519 size_check = ftdi_eeprom_build(ftdi);
520 eeprom_get_value(ftdi, CHIP_SIZE, &my_eeprom_size);
522 if (size_check == -1)
524 printf ("Sorry, the eeprom can only contain 128 bytes.\n");
527 else if (size_check < 0)
529 printf ("ftdi_eeprom_build(): error: %d\n", size_check);
534 printf ("Used eeprom space: %d bytes\n", my_eeprom_size-size_check);
537 if (command == COMMAND_FLASH)
539 if (cfg_getbool(cfg, "flash_raw"))
541 if (filename != NULL && strlen(filename) > 0)
543 eeprom_buf = malloc(max_eeprom_size);
544 FILE *fp = fopen(filename, "rb");
547 printf ("Can't open eeprom file %s.\n", filename);
550 my_eeprom_size = fread(eeprom_buf, 1, max_eeprom_size, fp);
552 if (my_eeprom_size < 128)
554 printf ("Can't read eeprom file %s.\n", filename);
558 ftdi_set_eeprom_buf(ftdi, eeprom_buf, my_eeprom_size);
561 printf ("FTDI write eeprom: %d\n", ftdi_write_eeprom(ftdi));
562 libusb_reset_device(ftdi->usb_dev);
566 if (filename != NULL && strlen(filename) > 0 && !cfg_getbool(cfg, "flash_raw"))
568 fp = fopen(filename, "w");
571 printf ("Can't write eeprom file.\n");
575 printf ("Writing to file: %s\n", filename);
577 if (eeprom_buf == NULL)
578 eeprom_buf = malloc(my_eeprom_size);
579 ftdi_get_eeprom_buf(ftdi, eeprom_buf, my_eeprom_size);
581 fwrite(eeprom_buf, my_eeprom_size, 1, fp);
590 printf("FTDI close: %d\n", ftdi_usb_close(ftdi));