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