annotate fteeprom/fteeprom-prog.c @ 77:86db7b4bfca4

fteeprom-prog-bad: make the "bad" version easily accessible for testing
author Mychaela Falconia <falcon@freecalypso.org>
date Thu, 17 Sep 2020 00:43:26 +0000
parents 47dbfd66bd58
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
31
16b625911e19 fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
1 #include <sys/types.h>
16b625911e19 fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
2 #include <ctype.h>
16b625911e19 fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
3 #include <string.h>
16b625911e19 fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
4 #include <strings.h>
16b625911e19 fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
5 #include <stdio.h>
16b625911e19 fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
6 #include <stdlib.h>
16b625911e19 fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
7 #include <unistd.h>
16b625911e19 fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
8 #include <ftdi.h>
16b625911e19 fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
9
47
2c092eb1621b fteeprom-prog revamped: read from file or stdin, comments allowed,
Mychaela Falconia <falcon@freecalypso.org>
parents: 42
diff changeset
10 unsigned eeprom_size;
31
16b625911e19 fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
11 u_short eeprom[256];
16b625911e19 fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
12
47
2c092eb1621b fteeprom-prog revamped: read from file or stdin, comments allowed,
Mychaela Falconia <falcon@freecalypso.org>
parents: 42
diff changeset
13 read_eeprom_common(inf, filename_for_errs)
2c092eb1621b fteeprom-prog revamped: read from file or stdin, comments allowed,
Mychaela Falconia <falcon@freecalypso.org>
parents: 42
diff changeset
14 FILE *inf;
2c092eb1621b fteeprom-prog revamped: read from file or stdin, comments allowed,
Mychaela Falconia <falcon@freecalypso.org>
parents: 42
diff changeset
15 char *filename_for_errs;
2c092eb1621b fteeprom-prog revamped: read from file or stdin, comments allowed,
Mychaela Falconia <falcon@freecalypso.org>
parents: 42
diff changeset
16 {
2c092eb1621b fteeprom-prog revamped: read from file or stdin, comments allowed,
Mychaela Falconia <falcon@freecalypso.org>
parents: 42
diff changeset
17 char linebuf[1024], *cp;
2c092eb1621b fteeprom-prog revamped: read from file or stdin, comments allowed,
Mychaela Falconia <falcon@freecalypso.org>
parents: 42
diff changeset
18 int lineno, rc;
2c092eb1621b fteeprom-prog revamped: read from file or stdin, comments allowed,
Mychaela Falconia <falcon@freecalypso.org>
parents: 42
diff changeset
19 unsigned ptr = 0, input_off;
2c092eb1621b fteeprom-prog revamped: read from file or stdin, comments allowed,
Mychaela Falconia <falcon@freecalypso.org>
parents: 42
diff changeset
20
2c092eb1621b fteeprom-prog revamped: read from file or stdin, comments allowed,
Mychaela Falconia <falcon@freecalypso.org>
parents: 42
diff changeset
21 for (lineno = 1; fgets(linebuf, sizeof linebuf, inf); lineno++) {
2c092eb1621b fteeprom-prog revamped: read from file or stdin, comments allowed,
Mychaela Falconia <falcon@freecalypso.org>
parents: 42
diff changeset
22 for (cp = linebuf; isspace(*cp); cp++)
2c092eb1621b fteeprom-prog revamped: read from file or stdin, comments allowed,
Mychaela Falconia <falcon@freecalypso.org>
parents: 42
diff changeset
23 ;
2c092eb1621b fteeprom-prog revamped: read from file or stdin, comments allowed,
Mychaela Falconia <falcon@freecalypso.org>
parents: 42
diff changeset
24 if (*cp == '\0' || *cp == '#')
42
c4b9026c8875 fteeprom-{prog,read}: -t option replaced with -b and -B
Mychaela Falconia <falcon@freecalypso.org>
parents: 31
diff changeset
25 continue;
47
2c092eb1621b fteeprom-prog revamped: read from file or stdin, comments allowed,
Mychaela Falconia <falcon@freecalypso.org>
parents: 42
diff changeset
26 if (ptr >= 256) {
2c092eb1621b fteeprom-prog revamped: read from file or stdin, comments allowed,
Mychaela Falconia <falcon@freecalypso.org>
parents: 42
diff changeset
27 fprintf(stderr,
2c092eb1621b fteeprom-prog revamped: read from file or stdin, comments allowed,
Mychaela Falconia <falcon@freecalypso.org>
parents: 42
diff changeset
28 "%s line %d: maximum EEPROM size exceeded\n",
2c092eb1621b fteeprom-prog revamped: read from file or stdin, comments allowed,
Mychaela Falconia <falcon@freecalypso.org>
parents: 42
diff changeset
29 filename_for_errs, lineno);
31
16b625911e19 fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
30 exit(1);
16b625911e19 fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
31 }
47
2c092eb1621b fteeprom-prog revamped: read from file or stdin, comments allowed,
Mychaela Falconia <falcon@freecalypso.org>
parents: 42
diff changeset
32 rc = sscanf(linebuf, "%x: %hx %hx %hx %hx %hx %hx %hx %hx",
2c092eb1621b fteeprom-prog revamped: read from file or stdin, comments allowed,
Mychaela Falconia <falcon@freecalypso.org>
parents: 42
diff changeset
33 &input_off, eeprom + ptr, eeprom + ptr + 1,
2c092eb1621b fteeprom-prog revamped: read from file or stdin, comments allowed,
Mychaela Falconia <falcon@freecalypso.org>
parents: 42
diff changeset
34 eeprom + ptr + 2, eeprom + ptr + 3,
2c092eb1621b fteeprom-prog revamped: read from file or stdin, comments allowed,
Mychaela Falconia <falcon@freecalypso.org>
parents: 42
diff changeset
35 eeprom + ptr + 4, eeprom + ptr + 5,
2c092eb1621b fteeprom-prog revamped: read from file or stdin, comments allowed,
Mychaela Falconia <falcon@freecalypso.org>
parents: 42
diff changeset
36 eeprom + ptr + 6, eeprom + ptr + 7);
2c092eb1621b fteeprom-prog revamped: read from file or stdin, comments allowed,
Mychaela Falconia <falcon@freecalypso.org>
parents: 42
diff changeset
37 if (rc != 9 || input_off != ptr * 2) {
2c092eb1621b fteeprom-prog revamped: read from file or stdin, comments allowed,
Mychaela Falconia <falcon@freecalypso.org>
parents: 42
diff changeset
38 fprintf(stderr, "%s line %d: invalid input\n",
2c092eb1621b fteeprom-prog revamped: read from file or stdin, comments allowed,
Mychaela Falconia <falcon@freecalypso.org>
parents: 42
diff changeset
39 filename_for_errs, lineno);
2c092eb1621b fteeprom-prog revamped: read from file or stdin, comments allowed,
Mychaela Falconia <falcon@freecalypso.org>
parents: 42
diff changeset
40 exit(1);
2c092eb1621b fteeprom-prog revamped: read from file or stdin, comments allowed,
Mychaela Falconia <falcon@freecalypso.org>
parents: 42
diff changeset
41 }
2c092eb1621b fteeprom-prog revamped: read from file or stdin, comments allowed,
Mychaela Falconia <falcon@freecalypso.org>
parents: 42
diff changeset
42 ptr += 8;
31
16b625911e19 fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
43 }
47
2c092eb1621b fteeprom-prog revamped: read from file or stdin, comments allowed,
Mychaela Falconia <falcon@freecalypso.org>
parents: 42
diff changeset
44 if (ptr != 64 && ptr != 128 && ptr != 256) {
2c092eb1621b fteeprom-prog revamped: read from file or stdin, comments allowed,
Mychaela Falconia <falcon@freecalypso.org>
parents: 42
diff changeset
45 fprintf(stderr, "%s: not an EEPROM image of recognized size\n",
2c092eb1621b fteeprom-prog revamped: read from file or stdin, comments allowed,
Mychaela Falconia <falcon@freecalypso.org>
parents: 42
diff changeset
46 filename_for_errs);
31
16b625911e19 fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
47 exit(1);
16b625911e19 fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
48 }
47
2c092eb1621b fteeprom-prog revamped: read from file or stdin, comments allowed,
Mychaela Falconia <falcon@freecalypso.org>
parents: 42
diff changeset
49 eeprom_size = ptr;
2c092eb1621b fteeprom-prog revamped: read from file or stdin, comments allowed,
Mychaela Falconia <falcon@freecalypso.org>
parents: 42
diff changeset
50 }
2c092eb1621b fteeprom-prog revamped: read from file or stdin, comments allowed,
Mychaela Falconia <falcon@freecalypso.org>
parents: 42
diff changeset
51
2c092eb1621b fteeprom-prog revamped: read from file or stdin, comments allowed,
Mychaela Falconia <falcon@freecalypso.org>
parents: 42
diff changeset
52 read_eeprom_from_file(filename)
2c092eb1621b fteeprom-prog revamped: read from file or stdin, comments allowed,
Mychaela Falconia <falcon@freecalypso.org>
parents: 42
diff changeset
53 char *filename;
2c092eb1621b fteeprom-prog revamped: read from file or stdin, comments allowed,
Mychaela Falconia <falcon@freecalypso.org>
parents: 42
diff changeset
54 {
2c092eb1621b fteeprom-prog revamped: read from file or stdin, comments allowed,
Mychaela Falconia <falcon@freecalypso.org>
parents: 42
diff changeset
55 FILE *inf;
2c092eb1621b fteeprom-prog revamped: read from file or stdin, comments allowed,
Mychaela Falconia <falcon@freecalypso.org>
parents: 42
diff changeset
56
2c092eb1621b fteeprom-prog revamped: read from file or stdin, comments allowed,
Mychaela Falconia <falcon@freecalypso.org>
parents: 42
diff changeset
57 inf = fopen(filename, "r");
2c092eb1621b fteeprom-prog revamped: read from file or stdin, comments allowed,
Mychaela Falconia <falcon@freecalypso.org>
parents: 42
diff changeset
58 if (!inf) {
2c092eb1621b fteeprom-prog revamped: read from file or stdin, comments allowed,
Mychaela Falconia <falcon@freecalypso.org>
parents: 42
diff changeset
59 perror(filename);
2c092eb1621b fteeprom-prog revamped: read from file or stdin, comments allowed,
Mychaela Falconia <falcon@freecalypso.org>
parents: 42
diff changeset
60 exit(1);
2c092eb1621b fteeprom-prog revamped: read from file or stdin, comments allowed,
Mychaela Falconia <falcon@freecalypso.org>
parents: 42
diff changeset
61 }
2c092eb1621b fteeprom-prog revamped: read from file or stdin, comments allowed,
Mychaela Falconia <falcon@freecalypso.org>
parents: 42
diff changeset
62 read_eeprom_common(inf, filename);
2c092eb1621b fteeprom-prog revamped: read from file or stdin, comments allowed,
Mychaela Falconia <falcon@freecalypso.org>
parents: 42
diff changeset
63 fclose(inf);
31
16b625911e19 fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
64 }
16b625911e19 fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
65
16b625911e19 fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
66 read_eeprom_from_stdin()
16b625911e19 fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
67 {
47
2c092eb1621b fteeprom-prog revamped: read from file or stdin, comments allowed,
Mychaela Falconia <falcon@freecalypso.org>
parents: 42
diff changeset
68 read_eeprom_common(stdin, "stdin");
31
16b625911e19 fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
69 }
16b625911e19 fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
70
16b625911e19 fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
71 main(argc, argv)
16b625911e19 fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
72 char **argv;
16b625911e19 fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
73 {
16b625911e19 fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
74 struct ftdi_context ftdi;
54
47dbfd66bd58 fteeprom-prog: added magic sequence from libftdi
Mychaela Falconia <falcon@freecalypso.org>
parents: 47
diff changeset
75 u_short modem_status;
31
16b625911e19 fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
76 unsigned n;
16b625911e19 fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
77
47
2c092eb1621b fteeprom-prog revamped: read from file or stdin, comments allowed,
Mychaela Falconia <falcon@freecalypso.org>
parents: 42
diff changeset
78 if (argc < 2 || argc > 3) {
2c092eb1621b fteeprom-prog revamped: read from file or stdin, comments allowed,
Mychaela Falconia <falcon@freecalypso.org>
parents: 42
diff changeset
79 fprintf(stderr, "usage: %s device-selector [eeprom-image]\n",
2c092eb1621b fteeprom-prog revamped: read from file or stdin, comments allowed,
Mychaela Falconia <falcon@freecalypso.org>
parents: 42
diff changeset
80 argv[0]);
2c092eb1621b fteeprom-prog revamped: read from file or stdin, comments allowed,
Mychaela Falconia <falcon@freecalypso.org>
parents: 42
diff changeset
81 exit(1);
2c092eb1621b fteeprom-prog revamped: read from file or stdin, comments allowed,
Mychaela Falconia <falcon@freecalypso.org>
parents: 42
diff changeset
82 }
2c092eb1621b fteeprom-prog revamped: read from file or stdin, comments allowed,
Mychaela Falconia <falcon@freecalypso.org>
parents: 42
diff changeset
83 if (argv[2])
2c092eb1621b fteeprom-prog revamped: read from file or stdin, comments allowed,
Mychaela Falconia <falcon@freecalypso.org>
parents: 42
diff changeset
84 read_eeprom_from_file(argv[2]);
31
16b625911e19 fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
85 else
16b625911e19 fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
86 read_eeprom_from_stdin();
16b625911e19 fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
87 ftdi_init(&ftdi);
47
2c092eb1621b fteeprom-prog revamped: read from file or stdin, comments allowed,
Mychaela Falconia <falcon@freecalypso.org>
parents: 42
diff changeset
88 if (ftdi_usb_open_string(&ftdi, argv[1]) < 0) {
31
16b625911e19 fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
89 fprintf(stderr, "FTDI USB open failed: %s\n", ftdi.error_str);
16b625911e19 fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
90 exit(1);
16b625911e19 fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
91 }
54
47dbfd66bd58 fteeprom-prog: added magic sequence from libftdi
Mychaela Falconia <falcon@freecalypso.org>
parents: 47
diff changeset
92 /* magic sequence apparently required for FT232R */
47dbfd66bd58 fteeprom-prog: added magic sequence from libftdi
Mychaela Falconia <falcon@freecalypso.org>
parents: 47
diff changeset
93 if (ftdi_usb_reset(&ftdi) < 0) {
47dbfd66bd58 fteeprom-prog: added magic sequence from libftdi
Mychaela Falconia <falcon@freecalypso.org>
parents: 47
diff changeset
94 fprintf(stderr, "ftdi_usb_reset() failed: %s\n",
47dbfd66bd58 fteeprom-prog: added magic sequence from libftdi
Mychaela Falconia <falcon@freecalypso.org>
parents: 47
diff changeset
95 ftdi.error_str);
47dbfd66bd58 fteeprom-prog: added magic sequence from libftdi
Mychaela Falconia <falcon@freecalypso.org>
parents: 47
diff changeset
96 exit(1);
47dbfd66bd58 fteeprom-prog: added magic sequence from libftdi
Mychaela Falconia <falcon@freecalypso.org>
parents: 47
diff changeset
97 }
47dbfd66bd58 fteeprom-prog: added magic sequence from libftdi
Mychaela Falconia <falcon@freecalypso.org>
parents: 47
diff changeset
98 if (ftdi_poll_modem_status(&ftdi, &modem_status) < 0) {
47dbfd66bd58 fteeprom-prog: added magic sequence from libftdi
Mychaela Falconia <falcon@freecalypso.org>
parents: 47
diff changeset
99 fprintf(stderr, "ftdi_poll_modem_status() failed: %s\n",
47dbfd66bd58 fteeprom-prog: added magic sequence from libftdi
Mychaela Falconia <falcon@freecalypso.org>
parents: 47
diff changeset
100 ftdi.error_str);
47dbfd66bd58 fteeprom-prog: added magic sequence from libftdi
Mychaela Falconia <falcon@freecalypso.org>
parents: 47
diff changeset
101 exit(1);
47dbfd66bd58 fteeprom-prog: added magic sequence from libftdi
Mychaela Falconia <falcon@freecalypso.org>
parents: 47
diff changeset
102 }
47dbfd66bd58 fteeprom-prog: added magic sequence from libftdi
Mychaela Falconia <falcon@freecalypso.org>
parents: 47
diff changeset
103 if (ftdi_set_latency_timer(&ftdi, 0x77) < 0) {
47dbfd66bd58 fteeprom-prog: added magic sequence from libftdi
Mychaela Falconia <falcon@freecalypso.org>
parents: 47
diff changeset
104 fprintf(stderr, "ftdi_set_latency_timer() failed: %s\n",
47dbfd66bd58 fteeprom-prog: added magic sequence from libftdi
Mychaela Falconia <falcon@freecalypso.org>
parents: 47
diff changeset
105 ftdi.error_str);
47dbfd66bd58 fteeprom-prog: added magic sequence from libftdi
Mychaela Falconia <falcon@freecalypso.org>
parents: 47
diff changeset
106 exit(1);
47dbfd66bd58 fteeprom-prog: added magic sequence from libftdi
Mychaela Falconia <falcon@freecalypso.org>
parents: 47
diff changeset
107 }
31
16b625911e19 fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
108 for (n = 0; n < eeprom_size; n++) {
16b625911e19 fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
109 if (ftdi_write_eeprom_location(&ftdi, n, eeprom[n]) < 0) {
16b625911e19 fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
110 fprintf(stderr, "EEPROM write error: %s\n",
16b625911e19 fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
111 ftdi.error_str);
16b625911e19 fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
112 exit(1);
16b625911e19 fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
113 }
16b625911e19 fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
114 }
16b625911e19 fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
115 ftdi_usb_close(&ftdi);
16b625911e19 fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
116 exit(0);
16b625911e19 fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
117 }