eeprom handling: Add support for arbitrary user data
[libftdi] / ftdi_eeprom / main.c
... / ...
CommitLineData
1/***************************************************************************
2 main.c - description
3 -------------------
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 ***************************************************************************/
8
9/***************************************************************************
10 * *
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. *
14 * *
15 ***************************************************************************/
16
17/*
18 TODO:
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
24 - Decode on stdout
25 - Ability to find device by PID/VID, product name or serial
26
27 TODO nice-to-have:
28 - Out-of-the-box compatibility with FTDI's eeprom tool configuration files
29 */
30
31#ifdef HAVE_CONFIG_H
32#include <config.h>
33#endif
34
35#include <stdlib.h>
36#include <stdio.h>
37#include <string.h>
38#include <errno.h>
39#include <sys/stat.h>
40
41#include <confuse.h>
42#include <libusb.h>
43#include <ftdi.h>
44#include <ftdi_eeprom_version.h>
45
46static int parse_cbus(cfg_t *cfg, cfg_opt_t *opt, const char *value, void *result)
47{
48 static const char* options[] =
49 {
50 "TXDEN", "PWREN", "RXLED", "TXLED", "TXRXLED", "SLEEP", "CLK48",
51 "CLK24", "CLK12", "CLK6", "IOMODE", "BB_WR", "BB_RD"
52 };
53
54 int i;
55 for (i=0; i<sizeof(options)/sizeof(*options); i++)
56 {
57 if (!(strcmp(options[i], value)))
58 {
59 *(int *)result = i;
60 return 0;
61 }
62 }
63
64 cfg_error(cfg, "Invalid %s option '%s'", cfg_opt_name(opt), value);
65 return -1;
66}
67
68static int parse_cbush(cfg_t *cfg, cfg_opt_t *opt, const char *value, void *result)
69{
70 static const char* options[] =
71 {
72 "TRISTATE", "TXLED", "RXLED", "TXRXLED", "PWREN", "SLEEP",
73 "DRIVE_0", "DRIVE1", "IOMODE", "TXDEN", "CLK30", "CLK15", "CLK7_5"
74 };
75
76 int i;
77 for (i=0; i<sizeof(options)/sizeof(*options); i++)
78 {
79 if (!(strcmp(options[i], value)))
80 {
81 *(int *)result = i;
82 return 0;
83 }
84 }
85
86 cfg_error(cfg, "Invalid %s option '%s'", cfg_opt_name(opt), value);
87 return -1;
88}
89
90static int parse_cbusx(cfg_t *cfg, cfg_opt_t *opt, const char *value, void *result)
91{
92 static const char* options[] =
93 {
94 "TRISTATE", "TXLED", "RXLED", "TXRXLED", "PWREN", "SLEEP",
95 "DRIVE_0", "DRIVE1", "IOMODE", "TXDEN", "CLK24", "CLK12",
96 "CLK6", "BAT_DETECT", "BAT_DETECT_NEG", "I2C_TXE", "I2C_RXF", "VBUS_SENSE",
97 "BB_WR", "BB_RD", "TIME_STAMP", "AWAKE"
98 };
99
100 int i;
101 for (i=0; i<sizeof(options)/sizeof(*options); i++)
102 {
103 if (!(strcmp(options[i], value)))
104 {
105 *(int *)result = i;
106 return 0;
107 }
108 }
109
110 cfg_error(cfg, "Invalid %s option '%s'", cfg_opt_name(opt), value);
111 return -1;
112}
113
114static int parse_chtype(cfg_t *cfg, cfg_opt_t *opt, const char *value, void *result)
115{
116 static const struct
117 {
118 char* key;
119 int opt;
120 } options[] =
121 {
122 { "UART", CHANNEL_IS_UART },
123 { "FIFO", CHANNEL_IS_FIFO },
124 { "OPTO", CHANNEL_IS_OPTO },
125 { "CPU", CHANNEL_IS_CPU },
126 { "FT1284", CHANNEL_IS_FT1284}
127 };
128
129 int i;
130 for (i=0; i<sizeof(options)/sizeof(*options); i++)
131 {
132 if (!(strcmp(options[i].key, value)))
133 {
134 *(int *)result = options[i].opt;
135 return 0;
136 }
137 }
138
139 cfg_error(cfg, "Invalid %s option '%s'", cfg_opt_name(opt), value);
140 return -1;
141}
142
143/**
144 * @brief Set eeprom value
145 *
146 * \param ftdi pointer to ftdi_context
147 * \param value_name Enum of the value to set
148 * \param value Value to set
149 *
150 * Function will abort the program on error
151 **/
152static void eeprom_set_value(struct ftdi_context *ftdi, enum ftdi_eeprom_value value_name, int value)
153{
154 if (ftdi_set_eeprom_value(ftdi, value_name, value) < 0)
155 {
156 printf("Unable to set eeprom value %d: %s. Aborting\n", value_name, ftdi_get_error_string(ftdi));
157 exit (-1);
158 }
159}
160
161/**
162 * @brief Get eeprom value
163 *
164 * \param ftdi pointer to ftdi_context
165 * \param value_name Enum of the value to get
166 * \param value Value to get
167 *
168 * Function will abort the program on error
169 **/
170static void eeprom_get_value(struct ftdi_context *ftdi, enum ftdi_eeprom_value value_name, int *value)
171{
172 if (ftdi_get_eeprom_value(ftdi, value_name, value) < 0)
173 {
174 printf("Unable to get eeprom value %d: %s. Aborting\n", value_name, ftdi_get_error_string(ftdi));
175 exit (-1);
176 }
177}
178
179static void usage(const char *program)
180{
181 fprintf(stderr, "Syntax: %s [...options...] <config-file>\n", program);
182 fprintf(stderr, "Valid Options:\n");
183 fprintf(stderr, "--device <description> Specify device to open by description string. One of:\n");
184 fprintf(stderr, " d:<devicenode>\n");
185 fprintf(stderr, " i:<vendor>:<product>\n");
186 fprintf(stderr, " i:<vendor>:<product>:<index>\n");
187 fprintf(stderr, " s:<vendor>:<product>:<serial>\n");
188 fprintf(stderr, "--read-eeprom Read eeprom and write to -filename- from config-file\n");
189 fprintf(stderr, "--build-eeprom Build eeprom image\n");
190 fprintf(stderr, "--erase-eeprom Erase eeprom\n");
191 fprintf(stderr, "--flash-eeprom Flash eeprom\n");
192}
193
194int main(int argc, char *argv[])
195{
196 /*
197 configuration options
198 */
199 cfg_opt_t opts[] =
200 {
201 CFG_INT("vendor_id", 0, 0),
202 CFG_INT("product_id", 0, 0),
203 CFG_BOOL("self_powered", cfg_true, 0),
204 CFG_BOOL("remote_wakeup", cfg_true, 0),
205 CFG_BOOL("in_is_isochronous", cfg_false, 0),
206 CFG_BOOL("out_is_isochronous", cfg_false, 0),
207 CFG_BOOL("suspend_pull_downs", cfg_false, 0),
208 CFG_BOOL("use_serial", cfg_false, 0),
209 CFG_BOOL("change_usb_version", cfg_false, 0),
210 CFG_INT("usb_version", 0, 0),
211 CFG_INT("default_pid", 0x6001, 0),
212 CFG_INT("max_power", 0, 0),
213 CFG_STR("manufacturer", "Acme Inc.", 0),
214 CFG_STR("product", "USB Serial Converter", 0),
215 CFG_STR("serial", "08-15", 0),
216 CFG_INT("eeprom_type", 0x00, 0),
217 CFG_STR("filename", "", 0),
218 CFG_BOOL("flash_raw", cfg_false, 0),
219 CFG_BOOL("high_current", cfg_false, 0),
220 CFG_INT_CB("cbus0", -1, 0, parse_cbus),
221 CFG_INT_CB("cbus1", -1, 0, parse_cbus),
222 CFG_INT_CB("cbus2", -1, 0, parse_cbus),
223 CFG_INT_CB("cbus3", -1, 0, parse_cbus),
224 CFG_INT_CB("cbus4", -1, 0, parse_cbus),
225 CFG_INT_CB("cbush0", -1, 0, parse_cbush),
226 CFG_INT_CB("cbush1", -1, 0, parse_cbush),
227 CFG_INT_CB("cbush2", -1, 0, parse_cbush),
228 CFG_INT_CB("cbush3", -1, 0, parse_cbush),
229 CFG_INT_CB("cbush4", -1, 0, parse_cbush),
230 CFG_INT_CB("cbush5", -1, 0, parse_cbush),
231 CFG_INT_CB("cbush6", -1, 0, parse_cbush),
232 CFG_INT_CB("cbush7", -1, 0, parse_cbush),
233 CFG_INT_CB("cbush8", -1, 0, parse_cbush),
234 CFG_INT_CB("cbush9", -1, 0, parse_cbush),
235 CFG_INT_CB("cbusx0", -1, 0, parse_cbusx),
236 CFG_INT_CB("cbusx1", -1, 0, parse_cbusx),
237 CFG_INT_CB("cbusx2", -1, 0, parse_cbusx),
238 CFG_INT_CB("cbusx3", -1, 0, parse_cbusx),
239 CFG_BOOL("invert_txd", cfg_false, 0),
240 CFG_BOOL("invert_rxd", cfg_false, 0),
241 CFG_BOOL("invert_rts", cfg_false, 0),
242 CFG_BOOL("invert_cts", cfg_false, 0),
243 CFG_BOOL("invert_dtr", cfg_false, 0),
244 CFG_BOOL("invert_dsr", cfg_false, 0),
245 CFG_BOOL("invert_dcd", cfg_false, 0),
246 CFG_BOOL("invert_ri", cfg_false, 0),
247 CFG_INT_CB("cha_type", -1, 0, parse_chtype),
248 CFG_INT_CB("chb_type", -1, 0, parse_chtype),
249 CFG_BOOL("cha_vcp", cfg_true, 0),
250 CFG_BOOL("chb_vcp", cfg_true, 0),
251 CFG_BOOL("chc_vcp", cfg_true, 0),
252 CFG_BOOL("chd_vcp", cfg_true, 0),
253 CFG_BOOL("cha_rs485", cfg_false, 0),
254 CFG_BOOL("chb_rs485", cfg_false, 0),
255 CFG_BOOL("chc_rs485", cfg_false, 0),
256 CFG_BOOL("chd_rs485", cfg_false, 0),
257 CFG_FUNC("include", &cfg_include),
258 CFG_INT("user_data_addr", 0x18, 0),
259 CFG_STR("user_data_file", "", 0),
260 CFG_END()
261 };
262 cfg_t *cfg;
263
264 /*
265 normal variables
266 */
267 enum {
268 COMMAND_READ = 1,
269 COMMAND_ERASE,
270 COMMAND_FLASH,
271 COMMAND_BUILD
272 } command = 0;
273 const char *cfg_filename = NULL;
274 const char *device_description = NULL;
275 const char *user_data_file = NULL;
276 char *user_data_buffer = NULL;
277
278 const int max_eeprom_size = 256;
279 int my_eeprom_size = 0;
280 unsigned char *eeprom_buf = NULL;
281 char *filename;
282 int size_check;
283 int i;
284 FILE *fp;
285
286 struct ftdi_context *ftdi = NULL;
287
288 printf("\nFTDI eeprom generator v%s\n", EEPROM_VERSION_STRING);
289 printf ("(c) Intra2net AG and the libftdi developers <opensource@intra2net.com>\n");
290
291 for (i = 1; i < argc; i++) {
292 if (*argv[i] != '-')
293 {
294 cfg_filename = argv[i];
295 }
296 else if (!strcmp(argv[i], "--device"))
297 {
298 if (i+1 >= argc)
299 {
300 usage(argv[0]);
301 exit(-1);
302 }
303 device_description = argv[++i];
304 }
305 else if (!strcmp(argv[i], "--read-eeprom"))
306 {
307 command = COMMAND_READ;
308 }
309 else if (!strcmp(argv[i], "--erase-eeprom"))
310 {
311 command = COMMAND_ERASE;
312 }
313 else if (!strcmp(argv[i], "--flash-eeprom"))
314 {
315 command = COMMAND_FLASH;
316 }
317 else if (!strcmp(argv[i], "--build-eeprom"))
318 {
319 command = COMMAND_BUILD;
320 }
321 else
322 {
323 usage(argv[0]);
324 exit(-1);
325 }
326 }
327
328 if (!cfg_filename)
329 {
330 usage(argv[0]);
331 exit(-1);
332 }
333
334 if ((fp = fopen(cfg_filename, "r")) == NULL)
335 {
336 printf ("Can't open configuration file\n");
337 exit (-1);
338 }
339 fclose (fp);
340
341 cfg = cfg_init(opts, 0);
342 cfg_parse(cfg, cfg_filename);
343 filename = cfg_getstr(cfg, "filename");
344
345 if (cfg_getbool(cfg, "self_powered") && cfg_getint(cfg, "max_power") > 0)
346 printf("Hint: Self powered devices should have a max_power setting of 0.\n");
347
348 if ((ftdi = ftdi_new()) == 0)
349 {
350 fprintf(stderr, "Failed to allocate ftdi structure :%s \n",
351 ftdi_get_error_string(ftdi));
352 return EXIT_FAILURE;
353 }
354
355 if (device_description != NULL)
356 {
357 i = ftdi_usb_open_string(ftdi, device_description);
358
359 if (i != 0)
360 {
361 printf("Unable to find FTDI device with description: %s\n",
362 device_description);
363 printf("Error code: %d (%s)\n", i, ftdi_get_error_string(ftdi));
364 exit (-1);
365 }
366 }
367 else if (command > 0)
368 {
369 int vendor_id = cfg_getint(cfg, "vendor_id");
370 int product_id = cfg_getint(cfg, "product_id");
371
372 i = ftdi_usb_open(ftdi, vendor_id, product_id);
373
374 if (i != 0)
375 {
376 int default_pid = cfg_getint(cfg, "default_pid");
377 printf("Unable to find FTDI devices under given vendor/product id: 0x%X/0x%X\n", vendor_id, product_id);
378 printf("Error code: %d (%s)\n", i, ftdi_get_error_string(ftdi));
379 printf("Retrying with default FTDI pid=%#04x.\n", default_pid);
380
381 i = ftdi_usb_open(ftdi, 0x0403, default_pid);
382 if (i != 0)
383 {
384 printf("Error: %s\n", ftdi->error_str);
385 exit (-1);
386 }
387 }
388 }
389 ftdi_eeprom_initdefaults (ftdi, cfg_getstr(cfg, "manufacturer"),
390 cfg_getstr(cfg, "product"),
391 cfg_getstr(cfg, "serial"));
392
393 printf("FTDI read eeprom: %d\n", ftdi_read_eeprom(ftdi));
394 eeprom_get_value(ftdi, CHIP_SIZE, &my_eeprom_size);
395 printf("EEPROM size: %d\n", my_eeprom_size);
396
397 if (command == COMMAND_READ)
398 {
399 ftdi_eeprom_decode(ftdi, 0 /* debug: 1 */);
400
401 eeprom_buf = malloc(my_eeprom_size);
402 ftdi_get_eeprom_buf(ftdi, eeprom_buf, my_eeprom_size);
403
404 if (eeprom_buf == NULL)
405 {
406 fprintf(stderr, "Malloc failed, aborting\n");
407 goto cleanup;
408 }
409 if (filename != NULL && strlen(filename) > 0)
410 {
411 FILE *fp = fopen (filename, "wb");
412
413 if(fp)
414 {
415 fwrite(eeprom_buf, 1, my_eeprom_size, fp);
416 fclose(fp);
417 }
418 else
419 fprintf(stderr, "Could not open output file %s: %s\n", filename, strerror(errno));
420 }
421 else
422 {
423 printf("Warning: Not writing eeprom, you must supply a valid filename\n");
424 }
425
426 goto cleanup;
427 }
428
429 eeprom_set_value(ftdi, VENDOR_ID, cfg_getint(cfg, "vendor_id"));
430 eeprom_set_value(ftdi, PRODUCT_ID, cfg_getint(cfg, "product_id"));
431
432 eeprom_set_value(ftdi, SELF_POWERED, cfg_getbool(cfg, "self_powered"));
433 eeprom_set_value(ftdi, REMOTE_WAKEUP, cfg_getbool(cfg, "remote_wakeup"));
434 eeprom_set_value(ftdi, MAX_POWER, cfg_getint(cfg, "max_power"));
435
436 eeprom_set_value(ftdi, IN_IS_ISOCHRONOUS, cfg_getbool(cfg, "in_is_isochronous"));
437 eeprom_set_value(ftdi, OUT_IS_ISOCHRONOUS, cfg_getbool(cfg, "out_is_isochronous"));
438 eeprom_set_value(ftdi, SUSPEND_PULL_DOWNS, cfg_getbool(cfg, "suspend_pull_downs"));
439
440 eeprom_set_value(ftdi, USE_SERIAL, cfg_getbool(cfg, "use_serial"));
441 eeprom_set_value(ftdi, USE_USB_VERSION, cfg_getbool(cfg, "change_usb_version"));
442 eeprom_set_value(ftdi, USB_VERSION, cfg_getint(cfg, "usb_version"));
443 eeprom_set_value(ftdi, CHIP_TYPE, cfg_getint(cfg, "eeprom_type"));
444
445 eeprom_set_value(ftdi, HIGH_CURRENT, cfg_getbool(cfg, "high_current"));
446
447 if (ftdi->type == TYPE_R)
448 {
449 if (cfg_getint(cfg, "cbus0") != -1)
450 eeprom_set_value(ftdi, CBUS_FUNCTION_0, cfg_getint(cfg, "cbus0"));
451 if (cfg_getint(cfg, "cbus1") != -1)
452 eeprom_set_value(ftdi, CBUS_FUNCTION_1, cfg_getint(cfg, "cbus1"));
453 if (cfg_getint(cfg, "cbus2") != -1)
454 eeprom_set_value(ftdi, CBUS_FUNCTION_2, cfg_getint(cfg, "cbus2"));
455 if (cfg_getint(cfg, "cbus3") != -1)
456 eeprom_set_value(ftdi, CBUS_FUNCTION_3, cfg_getint(cfg, "cbus3"));
457 if (cfg_getint(cfg, "cbus4") != -1)
458 eeprom_set_value(ftdi, CBUS_FUNCTION_4, cfg_getint(cfg, "cbus4"));
459 }
460 else if (ftdi->type == TYPE_232H)
461 {
462 if (cfg_getint(cfg, "cbush0") != -1)
463 eeprom_set_value(ftdi, CBUS_FUNCTION_0, cfg_getint(cfg, "cbush0"));
464 if (cfg_getint(cfg, "cbush1") != -1)
465 eeprom_set_value(ftdi, CBUS_FUNCTION_1, cfg_getint(cfg, "cbush1"));
466 if (cfg_getint(cfg, "cbush2") != -1)
467 eeprom_set_value(ftdi, CBUS_FUNCTION_2, cfg_getint(cfg, "cbush2"));
468 if (cfg_getint(cfg, "cbush3") != -1)
469 eeprom_set_value(ftdi, CBUS_FUNCTION_3, cfg_getint(cfg, "cbush3"));
470 if (cfg_getint(cfg, "cbush4") != -1)
471 eeprom_set_value(ftdi, CBUS_FUNCTION_4, cfg_getint(cfg, "cbush4"));
472 if (cfg_getint(cfg, "cbush5") != -1)
473 eeprom_set_value(ftdi, CBUS_FUNCTION_5, cfg_getint(cfg, "cbush5"));
474 if (cfg_getint(cfg, "cbush6") != -1)
475 eeprom_set_value(ftdi, CBUS_FUNCTION_6, cfg_getint(cfg, "cbush6"));
476 if (cfg_getint(cfg, "cbush7") != -1)
477 eeprom_set_value(ftdi, CBUS_FUNCTION_7, cfg_getint(cfg, "cbush7"));
478 if (cfg_getint(cfg, "cbush8") != -1)
479 eeprom_set_value(ftdi, CBUS_FUNCTION_8, cfg_getint(cfg, "cbush8"));
480 if (cfg_getint(cfg, "cbush9") != -1)
481 eeprom_set_value(ftdi, CBUS_FUNCTION_9, cfg_getint(cfg, "cbush9"));
482 }
483 else if (ftdi->type == TYPE_230X)
484 {
485 if (cfg_getint(cfg, "cbusx0") != -1)
486 eeprom_set_value(ftdi, CBUS_FUNCTION_0, cfg_getint(cfg, "cbusx0"));
487 if (cfg_getint(cfg, "cbusx1") != -1)
488 eeprom_set_value(ftdi, CBUS_FUNCTION_1, cfg_getint(cfg, "cbusx1"));
489 if (cfg_getint(cfg, "cbusx2") != -1)
490 eeprom_set_value(ftdi, CBUS_FUNCTION_2, cfg_getint(cfg, "cbusx2"));
491 if (cfg_getint(cfg, "cbusx3") != -1)
492 eeprom_set_value(ftdi, CBUS_FUNCTION_3, cfg_getint(cfg, "cbusx3"));
493 }
494
495 int invert = 0;
496 if (cfg_getbool(cfg, "invert_rxd")) invert |= INVERT_RXD;
497 if (cfg_getbool(cfg, "invert_txd")) invert |= INVERT_TXD;
498 if (cfg_getbool(cfg, "invert_rts")) invert |= INVERT_RTS;
499 if (cfg_getbool(cfg, "invert_cts")) invert |= INVERT_CTS;
500 if (cfg_getbool(cfg, "invert_dtr")) invert |= INVERT_DTR;
501 if (cfg_getbool(cfg, "invert_dsr")) invert |= INVERT_DSR;
502 if (cfg_getbool(cfg, "invert_dcd")) invert |= INVERT_DCD;
503 if (cfg_getbool(cfg, "invert_ri")) invert |= INVERT_RI;
504 eeprom_set_value(ftdi, INVERT, invert);
505
506 if (cfg_getint(cfg, "cha_type") != -1)
507 eeprom_set_value(ftdi, CHANNEL_A_TYPE, cfg_getint(cfg, "cha_type"));
508 if (cfg_getint(cfg, "chb_type") != -1)
509 eeprom_set_value(ftdi, CHANNEL_B_TYPE, cfg_getint(cfg, "chb_type"));
510
511 eeprom_set_value(ftdi, CHANNEL_A_DRIVER,
512 cfg_getbool(cfg, "cha_vcp") ? DRIVER_VCP : 0);
513 eeprom_set_value(ftdi, CHANNEL_B_DRIVER,
514 cfg_getbool(cfg, "chb_vcp") ? DRIVER_VCP : 0);
515 eeprom_set_value(ftdi, CHANNEL_C_DRIVER,
516 cfg_getbool(cfg, "chc_vcp") ? DRIVER_VCP : 0);
517 eeprom_set_value(ftdi, CHANNEL_D_DRIVER,
518 cfg_getbool(cfg, "chd_vcp") ? DRIVER_VCP : 0);
519
520 eeprom_set_value(ftdi, CHANNEL_A_RS485, cfg_getbool(cfg, "cha_rs485"));
521 eeprom_set_value(ftdi, CHANNEL_B_RS485, cfg_getbool(cfg, "chb_rs485"));
522 eeprom_set_value(ftdi, CHANNEL_C_RS485, cfg_getbool(cfg, "chc_rs485"));
523 eeprom_set_value(ftdi, CHANNEL_D_RS485, cfg_getbool(cfg, "chd_rs485"));
524
525 /* Arbitrary user data */
526 eeprom_set_value(ftdi, USER_DATA_ADDR, cfg_getint(cfg, "user_data_addr"));
527 user_data_file = cfg_getstr(cfg, "user_data_file");
528 if (user_data_file && strlen(user_data_file) > 0)
529 {
530 int data_size;
531 struct stat st;
532
533 printf("User data file: %s\n", user_data_file);
534 /* Allocate a buffer for the user data */
535 user_data_buffer = (char *)malloc(max_eeprom_size);
536 if (user_data_buffer == NULL)
537 {
538 fprintf(stderr, "Malloc failed, aborting\n");
539 goto cleanup;
540 }
541
542 if (stat(user_data_file, &st))
543 {
544 printf ("Can't stat user data file %s.\n", user_data_file);
545 exit (-1);
546 }
547 if (st.st_size > max_eeprom_size)
548 printf("Warning: %s is too big, only reading %d bytes\n",
549 user_data_file, max_eeprom_size);
550 /* Read the user data file, no more than max_eeprom_size bytes */
551 FILE *fp = fopen(user_data_file, "rb");
552 if (fp == NULL)
553 {
554 printf ("Can't open user data file %s.\n", user_data_file);
555 exit (-1);
556 }
557 data_size = fread(user_data_buffer, 1, max_eeprom_size, fp);
558 fclose(fp);
559 if (data_size < 1)
560 {
561 printf ("Can't read user data file %s.\n", user_data_file);
562 exit (-1);
563 }
564 printf("User data size: %d\n", data_size);
565
566 ftdi_set_eeprom_user_data(ftdi, user_data_buffer, data_size);
567 }
568
569
570 if (command == COMMAND_ERASE)
571 {
572 printf("FTDI erase eeprom: %d\n", ftdi_erase_eeprom(ftdi));
573 }
574
575 size_check = ftdi_eeprom_build(ftdi);
576 eeprom_get_value(ftdi, CHIP_SIZE, &my_eeprom_size);
577
578 if (size_check == -1)
579 {
580 printf ("Sorry, the eeprom can only contain %d bytes.\n", my_eeprom_size);
581 goto cleanup;
582 }
583 else if (size_check < 0)
584 {
585 printf ("ftdi_eeprom_build(): error: %d\n", size_check);
586 goto cleanup;
587 }
588 else
589 {
590 printf ("Used eeprom space: %d bytes\n", my_eeprom_size-size_check);
591 }
592
593 if (command == COMMAND_FLASH)
594 {
595 if (cfg_getbool(cfg, "flash_raw"))
596 {
597 if (filename != NULL && strlen(filename) > 0)
598 {
599 eeprom_buf = malloc(max_eeprom_size);
600 FILE *fp = fopen(filename, "rb");
601 if (fp == NULL)
602 {
603 printf ("Can't open eeprom file %s.\n", filename);
604 exit (-1);
605 }
606 my_eeprom_size = fread(eeprom_buf, 1, max_eeprom_size, fp);
607 fclose(fp);
608 if (my_eeprom_size < 128)
609 {
610 printf ("Can't read eeprom file %s.\n", filename);
611 exit (-1);
612 }
613
614 ftdi_set_eeprom_buf(ftdi, eeprom_buf, my_eeprom_size);
615 }
616 }
617 printf ("FTDI write eeprom: %d\n", ftdi_write_eeprom(ftdi));
618 libusb_reset_device(ftdi->usb_dev);
619 }
620
621 // Write to file?
622 if (filename != NULL && strlen(filename) > 0 && !cfg_getbool(cfg, "flash_raw"))
623 {
624 fp = fopen(filename, "w");
625 if (fp == NULL)
626 {
627 printf ("Can't write eeprom file.\n");
628 exit (-1);
629 }
630 else
631 printf ("Writing to file: %s\n", filename);
632
633 if (eeprom_buf == NULL)
634 eeprom_buf = malloc(my_eeprom_size);
635 ftdi_get_eeprom_buf(ftdi, eeprom_buf, my_eeprom_size);
636
637 fwrite(eeprom_buf, my_eeprom_size, 1, fp);
638 fclose(fp);
639 }
640
641cleanup:
642 if (eeprom_buf)
643 free(eeprom_buf);
644 if (user_data_buffer)
645 free(user_data_buffer);
646 if (command > 0)
647 {
648 printf("FTDI close: %d\n", ftdi_usb_close(ftdi));
649 }
650
651 ftdi_deinit (ftdi);
652 ftdi_free (ftdi);
653
654 cfg_free(cfg);
655
656 printf("\n");
657 return 0;
658}