annotate fteeprom/fteeprom-prog.c @ 84:b36397a56bda

doc/CP2102-EEPROM-format: small error in analysis
author Mychaela Falconia <falcon@freecalypso.org>
date Wed, 27 Sep 2023 18:38:10 +0000
parents 5cbde3c80c24
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
20
43b8e88dae02 fteeprom-prog: convert to new local libs
Mychaela Falconia <falcon@freecalypso.org>
parents: 0
diff changeset
1 /*
43b8e88dae02 fteeprom-prog: convert to new local libs
Mychaela Falconia <falcon@freecalypso.org>
parents: 0
diff changeset
2 * This program connects to an FTDI chip via libusb and programs its EEPROM
43b8e88dae02 fteeprom-prog: convert to new local libs
Mychaela Falconia <falcon@freecalypso.org>
parents: 0
diff changeset
3 * with a raw hex image read from stdin or from a backup file.
43b8e88dae02 fteeprom-prog: convert to new local libs
Mychaela Falconia <falcon@freecalypso.org>
parents: 0
diff changeset
4 *
43b8e88dae02 fteeprom-prog: convert to new local libs
Mychaela Falconia <falcon@freecalypso.org>
parents: 0
diff changeset
5 * The present version has been converted to use our local libraries
43b8e88dae02 fteeprom-prog: convert to new local libs
Mychaela Falconia <falcon@freecalypso.org>
parents: 0
diff changeset
6 * (libftmini and libuwrap) instead of libftdi - thus the external dependency
43b8e88dae02 fteeprom-prog: convert to new local libs
Mychaela Falconia <falcon@freecalypso.org>
parents: 0
diff changeset
7 * is only libusb.
43b8e88dae02 fteeprom-prog: convert to new local libs
Mychaela Falconia <falcon@freecalypso.org>
parents: 0
diff changeset
8 */
43b8e88dae02 fteeprom-prog: convert to new local libs
Mychaela Falconia <falcon@freecalypso.org>
parents: 0
diff changeset
9
0
11b8a30333b3 fteeprom: initial import from freecalypso-hwlab
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
10 #include <sys/types.h>
11b8a30333b3 fteeprom: initial import from freecalypso-hwlab
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
11 #include <ctype.h>
11b8a30333b3 fteeprom: initial import from freecalypso-hwlab
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
12 #include <string.h>
11b8a30333b3 fteeprom: initial import from freecalypso-hwlab
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
13 #include <strings.h>
11b8a30333b3 fteeprom: initial import from freecalypso-hwlab
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
14 #include <stdio.h>
11b8a30333b3 fteeprom: initial import from freecalypso-hwlab
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
15 #include <stdlib.h>
11b8a30333b3 fteeprom: initial import from freecalypso-hwlab
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
16 #include <unistd.h>
20
43b8e88dae02 fteeprom-prog: convert to new local libs
Mychaela Falconia <falcon@freecalypso.org>
parents: 0
diff changeset
17 #include <usb.h>
43b8e88dae02 fteeprom-prog: convert to new local libs
Mychaela Falconia <falcon@freecalypso.org>
parents: 0
diff changeset
18 #include "../libuwrap/find_dev.h"
43b8e88dae02 fteeprom-prog: convert to new local libs
Mychaela Falconia <falcon@freecalypso.org>
parents: 0
diff changeset
19 #include "../libuwrap/open_close.h"
43b8e88dae02 fteeprom-prog: convert to new local libs
Mychaela Falconia <falcon@freecalypso.org>
parents: 0
diff changeset
20 #include "../libftmini/eeprom_func.h"
0
11b8a30333b3 fteeprom: initial import from freecalypso-hwlab
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
21
64
ca2250b4833d fteeprom: factor out EEPROM image reading functions
Mychaela Falconia <falcon@freecalypso.org>
parents: 45
diff changeset
22 extern unsigned eeprom_size;
ca2250b4833d fteeprom: factor out EEPROM image reading functions
Mychaela Falconia <falcon@freecalypso.org>
parents: 45
diff changeset
23 extern u_short eeprom[256];
ca2250b4833d fteeprom: factor out EEPROM image reading functions
Mychaela Falconia <falcon@freecalypso.org>
parents: 45
diff changeset
24
45
a94bd4002076 fteeprom-prog: implement -d option for interface detach
Mychaela Falconia <falcon@freecalypso.org>
parents: 41
diff changeset
25 char *device_selector, *input_filename;
68
5cbde3c80c24 fteeprom-{erase,prog}: detach logic: change to detach by default
Mychaela Falconia <falcon@freecalypso.org>
parents: 64
diff changeset
26 int ft232r_mode, no_detach;
0
11b8a30333b3 fteeprom: initial import from freecalypso-hwlab
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
27
45
a94bd4002076 fteeprom-prog: implement -d option for interface detach
Mychaela Falconia <falcon@freecalypso.org>
parents: 41
diff changeset
28 process_cmdline(argc, argv)
a94bd4002076 fteeprom-prog: implement -d option for interface detach
Mychaela Falconia <falcon@freecalypso.org>
parents: 41
diff changeset
29 char **argv;
a94bd4002076 fteeprom-prog: implement -d option for interface detach
Mychaela Falconia <falcon@freecalypso.org>
parents: 41
diff changeset
30 {
a94bd4002076 fteeprom-prog: implement -d option for interface detach
Mychaela Falconia <falcon@freecalypso.org>
parents: 41
diff changeset
31 int c;
a94bd4002076 fteeprom-prog: implement -d option for interface detach
Mychaela Falconia <falcon@freecalypso.org>
parents: 41
diff changeset
32 extern int optind;
a94bd4002076 fteeprom-prog: implement -d option for interface detach
Mychaela Falconia <falcon@freecalypso.org>
parents: 41
diff changeset
33 extern char *optarg;
a94bd4002076 fteeprom-prog: implement -d option for interface detach
Mychaela Falconia <falcon@freecalypso.org>
parents: 41
diff changeset
34
68
5cbde3c80c24 fteeprom-{erase,prog}: detach logic: change to detach by default
Mychaela Falconia <falcon@freecalypso.org>
parents: 64
diff changeset
35 while ((c = getopt(argc, argv, "nr")) != EOF) {
45
a94bd4002076 fteeprom-prog: implement -d option for interface detach
Mychaela Falconia <falcon@freecalypso.org>
parents: 41
diff changeset
36 switch (c) {
68
5cbde3c80c24 fteeprom-{erase,prog}: detach logic: change to detach by default
Mychaela Falconia <falcon@freecalypso.org>
parents: 64
diff changeset
37 case 'n':
5cbde3c80c24 fteeprom-{erase,prog}: detach logic: change to detach by default
Mychaela Falconia <falcon@freecalypso.org>
parents: 64
diff changeset
38 no_detach = 1;
45
a94bd4002076 fteeprom-prog: implement -d option for interface detach
Mychaela Falconia <falcon@freecalypso.org>
parents: 41
diff changeset
39 continue;
a94bd4002076 fteeprom-prog: implement -d option for interface detach
Mychaela Falconia <falcon@freecalypso.org>
parents: 41
diff changeset
40 case 'r':
a94bd4002076 fteeprom-prog: implement -d option for interface detach
Mychaela Falconia <falcon@freecalypso.org>
parents: 41
diff changeset
41 ft232r_mode = 1;
a94bd4002076 fteeprom-prog: implement -d option for interface detach
Mychaela Falconia <falcon@freecalypso.org>
parents: 41
diff changeset
42 continue;
a94bd4002076 fteeprom-prog: implement -d option for interface detach
Mychaela Falconia <falcon@freecalypso.org>
parents: 41
diff changeset
43 default:
a94bd4002076 fteeprom-prog: implement -d option for interface detach
Mychaela Falconia <falcon@freecalypso.org>
parents: 41
diff changeset
44 /* error msg already printed */
a94bd4002076 fteeprom-prog: implement -d option for interface detach
Mychaela Falconia <falcon@freecalypso.org>
parents: 41
diff changeset
45 exit(1);
a94bd4002076 fteeprom-prog: implement -d option for interface detach
Mychaela Falconia <falcon@freecalypso.org>
parents: 41
diff changeset
46 }
a94bd4002076 fteeprom-prog: implement -d option for interface detach
Mychaela Falconia <falcon@freecalypso.org>
parents: 41
diff changeset
47 }
a94bd4002076 fteeprom-prog: implement -d option for interface detach
Mychaela Falconia <falcon@freecalypso.org>
parents: 41
diff changeset
48 if (argc < optind + 1 || argc > optind + 2) {
a94bd4002076 fteeprom-prog: implement -d option for interface detach
Mychaela Falconia <falcon@freecalypso.org>
parents: 41
diff changeset
49 fprintf(stderr,
a94bd4002076 fteeprom-prog: implement -d option for interface detach
Mychaela Falconia <falcon@freecalypso.org>
parents: 41
diff changeset
50 "usage: %s [options] device-selector [eeprom-image]\n",
a94bd4002076 fteeprom-prog: implement -d option for interface detach
Mychaela Falconia <falcon@freecalypso.org>
parents: 41
diff changeset
51 argv[0]);
a94bd4002076 fteeprom-prog: implement -d option for interface detach
Mychaela Falconia <falcon@freecalypso.org>
parents: 41
diff changeset
52 exit(1);
a94bd4002076 fteeprom-prog: implement -d option for interface detach
Mychaela Falconia <falcon@freecalypso.org>
parents: 41
diff changeset
53 }
a94bd4002076 fteeprom-prog: implement -d option for interface detach
Mychaela Falconia <falcon@freecalypso.org>
parents: 41
diff changeset
54 device_selector = argv[optind];
a94bd4002076 fteeprom-prog: implement -d option for interface detach
Mychaela Falconia <falcon@freecalypso.org>
parents: 41
diff changeset
55 input_filename = argv[optind + 1];
a94bd4002076 fteeprom-prog: implement -d option for interface detach
Mychaela Falconia <falcon@freecalypso.org>
parents: 41
diff changeset
56 }
a94bd4002076 fteeprom-prog: implement -d option for interface detach
Mychaela Falconia <falcon@freecalypso.org>
parents: 41
diff changeset
57
0
11b8a30333b3 fteeprom: initial import from freecalypso-hwlab
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
58 main(argc, argv)
11b8a30333b3 fteeprom: initial import from freecalypso-hwlab
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
59 char **argv;
11b8a30333b3 fteeprom: initial import from freecalypso-hwlab
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
60 {
20
43b8e88dae02 fteeprom-prog: convert to new local libs
Mychaela Falconia <falcon@freecalypso.org>
parents: 0
diff changeset
61 struct usb_device *dev;
43b8e88dae02 fteeprom-prog: convert to new local libs
Mychaela Falconia <falcon@freecalypso.org>
parents: 0
diff changeset
62 usb_dev_handle *usbh;
0
11b8a30333b3 fteeprom: initial import from freecalypso-hwlab
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
63 unsigned n;
11b8a30333b3 fteeprom: initial import from freecalypso-hwlab
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
64
45
a94bd4002076 fteeprom-prog: implement -d option for interface detach
Mychaela Falconia <falcon@freecalypso.org>
parents: 41
diff changeset
65 process_cmdline(argc, argv);
a94bd4002076 fteeprom-prog: implement -d option for interface detach
Mychaela Falconia <falcon@freecalypso.org>
parents: 41
diff changeset
66 if (input_filename)
a94bd4002076 fteeprom-prog: implement -d option for interface detach
Mychaela Falconia <falcon@freecalypso.org>
parents: 41
diff changeset
67 read_eeprom_from_file(input_filename);
a94bd4002076 fteeprom-prog: implement -d option for interface detach
Mychaela Falconia <falcon@freecalypso.org>
parents: 41
diff changeset
68 else
0
11b8a30333b3 fteeprom: initial import from freecalypso-hwlab
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
69 read_eeprom_from_stdin();
45
a94bd4002076 fteeprom-prog: implement -d option for interface detach
Mychaela Falconia <falcon@freecalypso.org>
parents: 41
diff changeset
70 dev = find_usbdev_by_desc_string(device_selector);
20
43b8e88dae02 fteeprom-prog: convert to new local libs
Mychaela Falconia <falcon@freecalypso.org>
parents: 0
diff changeset
71 if (!dev) {
43b8e88dae02 fteeprom-prog: convert to new local libs
Mychaela Falconia <falcon@freecalypso.org>
parents: 0
diff changeset
72 fprintf(stderr, "error: specified USB device not found\n");
0
11b8a30333b3 fteeprom: initial import from freecalypso-hwlab
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
73 exit(1);
11b8a30333b3 fteeprom: initial import from freecalypso-hwlab
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
74 }
41
940cde8a99b6 fteeprom-prog: we don't need to claim the interface
Mychaela Falconia <falcon@freecalypso.org>
parents: 23
diff changeset
75 usbh = usb_open(dev);
940cde8a99b6 fteeprom-prog: we don't need to claim the interface
Mychaela Falconia <falcon@freecalypso.org>
parents: 23
diff changeset
76 if (!usbh) {
940cde8a99b6 fteeprom-prog: we don't need to claim the interface
Mychaela Falconia <falcon@freecalypso.org>
parents: 23
diff changeset
77 fprintf(stderr, "error: usb_open() failed\n");
940cde8a99b6 fteeprom-prog: we don't need to claim the interface
Mychaela Falconia <falcon@freecalypso.org>
parents: 23
diff changeset
78 exit(1);
940cde8a99b6 fteeprom-prog: we don't need to claim the interface
Mychaela Falconia <falcon@freecalypso.org>
parents: 23
diff changeset
79 }
68
5cbde3c80c24 fteeprom-{erase,prog}: detach logic: change to detach by default
Mychaela Falconia <falcon@freecalypso.org>
parents: 64
diff changeset
80 if (!no_detach)
5cbde3c80c24 fteeprom-{erase,prog}: detach logic: change to detach by default
Mychaela Falconia <falcon@freecalypso.org>
parents: 64
diff changeset
81 usbwrap_claim_all_ifs(usbh);
23
7e6dcceb5ee8 fteeprom-prog: implement -r option for FT232R mode
Mychaela Falconia <falcon@freecalypso.org>
parents: 20
diff changeset
82 if (ft232r_mode)
7e6dcceb5ee8 fteeprom-prog: implement -r option for FT232R mode
Mychaela Falconia <falcon@freecalypso.org>
parents: 20
diff changeset
83 ft232r_eeprom_magic(usbh);
20
43b8e88dae02 fteeprom-prog: convert to new local libs
Mychaela Falconia <falcon@freecalypso.org>
parents: 0
diff changeset
84 for (n = 0; n < eeprom_size; n++)
43b8e88dae02 fteeprom-prog: convert to new local libs
Mychaela Falconia <falcon@freecalypso.org>
parents: 0
diff changeset
85 ftmini_write_eeprom_loc(usbh, n, eeprom[n]);
41
940cde8a99b6 fteeprom-prog: we don't need to claim the interface
Mychaela Falconia <falcon@freecalypso.org>
parents: 23
diff changeset
86 usb_close(usbh);
0
11b8a30333b3 fteeprom: initial import from freecalypso-hwlab
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
87 exit(0);
11b8a30333b3 fteeprom: initial import from freecalypso-hwlab
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
88 }