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