comparison fteeprom/fteeprom-prog.c @ 47:2c092eb1621b

fteeprom-prog revamped: read from file or stdin, comments allowed, size determined from data (no more -b|-B), no more -e
author Mychaela Falconia <falcon@freecalypso.org>
date Mon, 22 Apr 2019 20:10:05 +0000
parents c4b9026c8875
children 47dbfd66bd58
comparison
equal deleted inserted replaced
46:e5d89313bc00 47:2c092eb1621b
5 #include <stdio.h> 5 #include <stdio.h>
6 #include <stdlib.h> 6 #include <stdlib.h>
7 #include <unistd.h> 7 #include <unistd.h>
8 #include <ftdi.h> 8 #include <ftdi.h>
9 9
10 char *device_selector; 10 unsigned eeprom_size;
11 unsigned eeprom_size = 64;
12 u_short eeprom[256]; 11 u_short eeprom[256];
13 int erase;
14 12
15 process_cmdline(argc, argv) 13 read_eeprom_common(inf, filename_for_errs)
16 char **argv; 14 FILE *inf;
15 char *filename_for_errs;
17 { 16 {
18 int c; 17 char linebuf[1024], *cp;
19 extern int optind; 18 int lineno, rc;
19 unsigned ptr = 0, input_off;
20 20
21 while ((c = getopt(argc, argv, "bBe")) != EOF) { 21 for (lineno = 1; fgets(linebuf, sizeof linebuf, inf); lineno++) {
22 switch (c) { 22 for (cp = linebuf; isspace(*cp); cp++)
23 case 'b': 23 ;
24 eeprom_size = 128; 24 if (*cp == '\0' || *cp == '#')
25 continue; 25 continue;
26 case 'B': 26 if (ptr >= 256) {
27 eeprom_size = 256; 27 fprintf(stderr,
28 continue; 28 "%s line %d: maximum EEPROM size exceeded\n",
29 case 'e': 29 filename_for_errs, lineno);
30 erase = 1;
31 continue;
32 default:
33 /* error msg already printed */
34 exit(1); 30 exit(1);
35 } 31 }
32 rc = sscanf(linebuf, "%x: %hx %hx %hx %hx %hx %hx %hx %hx",
33 &input_off, eeprom + ptr, eeprom + ptr + 1,
34 eeprom + ptr + 2, eeprom + ptr + 3,
35 eeprom + ptr + 4, eeprom + ptr + 5,
36 eeprom + ptr + 6, eeprom + ptr + 7);
37 if (rc != 9 || input_off != ptr * 2) {
38 fprintf(stderr, "%s line %d: invalid input\n",
39 filename_for_errs, lineno);
40 exit(1);
41 }
42 ptr += 8;
36 } 43 }
37 if (argc != optind + 1) { 44 if (ptr != 64 && ptr != 128 && ptr != 256) {
38 fprintf(stderr, "usage: %s [options] device-selector\n", 45 fprintf(stderr, "%s: not an EEPROM image of recognized size\n",
39 argv[0]); 46 filename_for_errs);
40 exit(1); 47 exit(1);
41 } 48 }
42 device_selector = argv[optind]; 49 eeprom_size = ptr;
50 }
51
52 read_eeprom_from_file(filename)
53 char *filename;
54 {
55 FILE *inf;
56
57 inf = fopen(filename, "r");
58 if (!inf) {
59 perror(filename);
60 exit(1);
61 }
62 read_eeprom_common(inf, filename);
63 fclose(inf);
43 } 64 }
44 65
45 read_eeprom_from_stdin() 66 read_eeprom_from_stdin()
46 { 67 {
47 unsigned n, off; 68 read_eeprom_common(stdin, "stdin");
48 int rc;
49
50 for (n = 0; n < eeprom_size; n += 8) {
51 rc = scanf("%x: %hx %hx %hx %hx %hx %hx %hx %hx", &off,
52 eeprom + n, eeprom + n + 1, eeprom + n + 2,
53 eeprom + n + 3, eeprom + n + 4, eeprom + n + 5,
54 eeprom + n + 6, eeprom + n + 7);
55 if (rc != 9 || off != n * 2) {
56 fprintf(stderr,
57 "ee2232-prog error: invalid input on stdin\n");
58 exit(1);
59 }
60 }
61 } 69 }
62 70
63 main(argc, argv) 71 main(argc, argv)
64 char **argv; 72 char **argv;
65 { 73 {
66 struct ftdi_context ftdi; 74 struct ftdi_context ftdi;
67 unsigned n; 75 unsigned n;
68 76
69 process_cmdline(argc, argv); 77 if (argc < 2 || argc > 3) {
70 if (erase) 78 fprintf(stderr, "usage: %s device-selector [eeprom-image]\n",
71 memset(eeprom, 0xFF, eeprom_size * 2); 79 argv[0]);
80 exit(1);
81 }
82 if (argv[2])
83 read_eeprom_from_file(argv[2]);
72 else 84 else
73 read_eeprom_from_stdin(); 85 read_eeprom_from_stdin();
74 ftdi_init(&ftdi); 86 ftdi_init(&ftdi);
75 if (ftdi_usb_open_string(&ftdi, device_selector) < 0) { 87 if (ftdi_usb_open_string(&ftdi, argv[1]) < 0) {
76 fprintf(stderr, "FTDI USB open failed: %s\n", ftdi.error_str); 88 fprintf(stderr, "FTDI USB open failed: %s\n", ftdi.error_str);
77 exit(1); 89 exit(1);
78 } 90 }
79 for (n = 0; n < eeprom_size; n++) { 91 for (n = 0; n < eeprom_size; n++) {
80 if (ftdi_write_eeprom_location(&ftdi, n, eeprom[n]) < 0) { 92 if (ftdi_write_eeprom_location(&ftdi, n, eeprom[n]) < 0) {