FreeCalypso > hg > fc-usbser-tools
annotate fteeprom/fteeprom-prog.c @ 64:ca2250b4833d
fteeprom: factor out EEPROM image reading functions
author | Mychaela Falconia <falcon@freecalypso.org> |
---|---|
date | Tue, 12 Sep 2023 20:17:07 +0000 |
parents | a94bd4002076 |
children | 5cbde3c80c24 |
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; |
a94bd4002076
fteeprom-prog: implement -d option for interface detach
Mychaela Falconia <falcon@freecalypso.org>
parents:
41
diff
changeset
|
26 int detach_num, ft232r_mode; |
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 |
a94bd4002076
fteeprom-prog: implement -d option for interface detach
Mychaela Falconia <falcon@freecalypso.org>
parents:
41
diff
changeset
|
35 while ((c = getopt(argc, argv, "d:r")) != EOF) { |
a94bd4002076
fteeprom-prog: implement -d option for interface detach
Mychaela Falconia <falcon@freecalypso.org>
parents:
41
diff
changeset
|
36 switch (c) { |
a94bd4002076
fteeprom-prog: implement -d option for interface detach
Mychaela Falconia <falcon@freecalypso.org>
parents:
41
diff
changeset
|
37 case 'd': |
a94bd4002076
fteeprom-prog: implement -d option for interface detach
Mychaela Falconia <falcon@freecalypso.org>
parents:
41
diff
changeset
|
38 detach_num = atoi(optarg); |
a94bd4002076
fteeprom-prog: implement -d option for interface detach
Mychaela Falconia <falcon@freecalypso.org>
parents:
41
diff
changeset
|
39 switch (detach_num) { |
a94bd4002076
fteeprom-prog: implement -d option for interface detach
Mychaela Falconia <falcon@freecalypso.org>
parents:
41
diff
changeset
|
40 case 0: |
a94bd4002076
fteeprom-prog: implement -d option for interface detach
Mychaela Falconia <falcon@freecalypso.org>
parents:
41
diff
changeset
|
41 case 1: |
a94bd4002076
fteeprom-prog: implement -d option for interface detach
Mychaela Falconia <falcon@freecalypso.org>
parents:
41
diff
changeset
|
42 case 2: |
a94bd4002076
fteeprom-prog: implement -d option for interface detach
Mychaela Falconia <falcon@freecalypso.org>
parents:
41
diff
changeset
|
43 case 4: |
a94bd4002076
fteeprom-prog: implement -d option for interface detach
Mychaela Falconia <falcon@freecalypso.org>
parents:
41
diff
changeset
|
44 break; |
a94bd4002076
fteeprom-prog: implement -d option for interface detach
Mychaela Falconia <falcon@freecalypso.org>
parents:
41
diff
changeset
|
45 default: |
a94bd4002076
fteeprom-prog: implement -d option for interface detach
Mychaela Falconia <falcon@freecalypso.org>
parents:
41
diff
changeset
|
46 fprintf(stderr, "error: invalid -d argument\n"); |
a94bd4002076
fteeprom-prog: implement -d option for interface detach
Mychaela Falconia <falcon@freecalypso.org>
parents:
41
diff
changeset
|
47 exit(1); |
a94bd4002076
fteeprom-prog: implement -d option for interface detach
Mychaela Falconia <falcon@freecalypso.org>
parents:
41
diff
changeset
|
48 } |
a94bd4002076
fteeprom-prog: implement -d option for interface detach
Mychaela Falconia <falcon@freecalypso.org>
parents:
41
diff
changeset
|
49 continue; |
a94bd4002076
fteeprom-prog: implement -d option for interface detach
Mychaela Falconia <falcon@freecalypso.org>
parents:
41
diff
changeset
|
50 case 'r': |
a94bd4002076
fteeprom-prog: implement -d option for interface detach
Mychaela Falconia <falcon@freecalypso.org>
parents:
41
diff
changeset
|
51 ft232r_mode = 1; |
a94bd4002076
fteeprom-prog: implement -d option for interface detach
Mychaela Falconia <falcon@freecalypso.org>
parents:
41
diff
changeset
|
52 detach_num = 1; |
a94bd4002076
fteeprom-prog: implement -d option for interface detach
Mychaela Falconia <falcon@freecalypso.org>
parents:
41
diff
changeset
|
53 continue; |
a94bd4002076
fteeprom-prog: implement -d option for interface detach
Mychaela Falconia <falcon@freecalypso.org>
parents:
41
diff
changeset
|
54 default: |
a94bd4002076
fteeprom-prog: implement -d option for interface detach
Mychaela Falconia <falcon@freecalypso.org>
parents:
41
diff
changeset
|
55 /* error msg already printed */ |
a94bd4002076
fteeprom-prog: implement -d option for interface detach
Mychaela Falconia <falcon@freecalypso.org>
parents:
41
diff
changeset
|
56 exit(1); |
a94bd4002076
fteeprom-prog: implement -d option for interface detach
Mychaela Falconia <falcon@freecalypso.org>
parents:
41
diff
changeset
|
57 } |
a94bd4002076
fteeprom-prog: implement -d option for interface detach
Mychaela Falconia <falcon@freecalypso.org>
parents:
41
diff
changeset
|
58 } |
a94bd4002076
fteeprom-prog: implement -d option for interface detach
Mychaela Falconia <falcon@freecalypso.org>
parents:
41
diff
changeset
|
59 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
|
60 fprintf(stderr, |
a94bd4002076
fteeprom-prog: implement -d option for interface detach
Mychaela Falconia <falcon@freecalypso.org>
parents:
41
diff
changeset
|
61 "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
|
62 argv[0]); |
a94bd4002076
fteeprom-prog: implement -d option for interface detach
Mychaela Falconia <falcon@freecalypso.org>
parents:
41
diff
changeset
|
63 exit(1); |
a94bd4002076
fteeprom-prog: implement -d option for interface detach
Mychaela Falconia <falcon@freecalypso.org>
parents:
41
diff
changeset
|
64 } |
a94bd4002076
fteeprom-prog: implement -d option for interface detach
Mychaela Falconia <falcon@freecalypso.org>
parents:
41
diff
changeset
|
65 device_selector = argv[optind]; |
a94bd4002076
fteeprom-prog: implement -d option for interface detach
Mychaela Falconia <falcon@freecalypso.org>
parents:
41
diff
changeset
|
66 input_filename = argv[optind + 1]; |
a94bd4002076
fteeprom-prog: implement -d option for interface detach
Mychaela Falconia <falcon@freecalypso.org>
parents:
41
diff
changeset
|
67 } |
a94bd4002076
fteeprom-prog: implement -d option for interface detach
Mychaela Falconia <falcon@freecalypso.org>
parents:
41
diff
changeset
|
68 |
0
11b8a30333b3
fteeprom: initial import from freecalypso-hwlab
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
69 main(argc, argv) |
11b8a30333b3
fteeprom: initial import from freecalypso-hwlab
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
70 char **argv; |
11b8a30333b3
fteeprom: initial import from freecalypso-hwlab
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
71 { |
20
43b8e88dae02
fteeprom-prog: convert to new local libs
Mychaela Falconia <falcon@freecalypso.org>
parents:
0
diff
changeset
|
72 struct usb_device *dev; |
43b8e88dae02
fteeprom-prog: convert to new local libs
Mychaela Falconia <falcon@freecalypso.org>
parents:
0
diff
changeset
|
73 usb_dev_handle *usbh; |
0
11b8a30333b3
fteeprom: initial import from freecalypso-hwlab
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
74 unsigned n; |
11b8a30333b3
fteeprom: initial import from freecalypso-hwlab
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
75 |
45
a94bd4002076
fteeprom-prog: implement -d option for interface detach
Mychaela Falconia <falcon@freecalypso.org>
parents:
41
diff
changeset
|
76 process_cmdline(argc, argv); |
a94bd4002076
fteeprom-prog: implement -d option for interface detach
Mychaela Falconia <falcon@freecalypso.org>
parents:
41
diff
changeset
|
77 if (input_filename) |
a94bd4002076
fteeprom-prog: implement -d option for interface detach
Mychaela Falconia <falcon@freecalypso.org>
parents:
41
diff
changeset
|
78 read_eeprom_from_file(input_filename); |
a94bd4002076
fteeprom-prog: implement -d option for interface detach
Mychaela Falconia <falcon@freecalypso.org>
parents:
41
diff
changeset
|
79 else |
0
11b8a30333b3
fteeprom: initial import from freecalypso-hwlab
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
80 read_eeprom_from_stdin(); |
45
a94bd4002076
fteeprom-prog: implement -d option for interface detach
Mychaela Falconia <falcon@freecalypso.org>
parents:
41
diff
changeset
|
81 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
|
82 if (!dev) { |
43b8e88dae02
fteeprom-prog: convert to new local libs
Mychaela Falconia <falcon@freecalypso.org>
parents:
0
diff
changeset
|
83 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
|
84 exit(1); |
11b8a30333b3
fteeprom: initial import from freecalypso-hwlab
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
85 } |
41
940cde8a99b6
fteeprom-prog: we don't need to claim the interface
Mychaela Falconia <falcon@freecalypso.org>
parents:
23
diff
changeset
|
86 usbh = usb_open(dev); |
940cde8a99b6
fteeprom-prog: we don't need to claim the interface
Mychaela Falconia <falcon@freecalypso.org>
parents:
23
diff
changeset
|
87 if (!usbh) { |
940cde8a99b6
fteeprom-prog: we don't need to claim the interface
Mychaela Falconia <falcon@freecalypso.org>
parents:
23
diff
changeset
|
88 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
|
89 exit(1); |
940cde8a99b6
fteeprom-prog: we don't need to claim the interface
Mychaela Falconia <falcon@freecalypso.org>
parents:
23
diff
changeset
|
90 } |
45
a94bd4002076
fteeprom-prog: implement -d option for interface detach
Mychaela Falconia <falcon@freecalypso.org>
parents:
41
diff
changeset
|
91 if (detach_num) |
a94bd4002076
fteeprom-prog: implement -d option for interface detach
Mychaela Falconia <falcon@freecalypso.org>
parents:
41
diff
changeset
|
92 usbwrap_claim_all_ifs(usbh, detach_num); |
23
7e6dcceb5ee8
fteeprom-prog: implement -r option for FT232R mode
Mychaela Falconia <falcon@freecalypso.org>
parents:
20
diff
changeset
|
93 if (ft232r_mode) |
7e6dcceb5ee8
fteeprom-prog: implement -r option for FT232R mode
Mychaela Falconia <falcon@freecalypso.org>
parents:
20
diff
changeset
|
94 ft232r_eeprom_magic(usbh); |
20
43b8e88dae02
fteeprom-prog: convert to new local libs
Mychaela Falconia <falcon@freecalypso.org>
parents:
0
diff
changeset
|
95 for (n = 0; n < eeprom_size; n++) |
43b8e88dae02
fteeprom-prog: convert to new local libs
Mychaela Falconia <falcon@freecalypso.org>
parents:
0
diff
changeset
|
96 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
|
97 usb_close(usbh); |
0
11b8a30333b3
fteeprom: initial import from freecalypso-hwlab
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
98 exit(0); |
11b8a30333b3
fteeprom: initial import from freecalypso-hwlab
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
99 } |