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