FreeCalypso > hg > fc-usbser-tools
annotate fteeprom/fteeprom-erase.c @ 63:230b9790046b
artifacts/CP2102-std-baud: captured booty
author | Mychaela Falconia <falcon@freecalypso.org> |
---|---|
date | Mon, 11 Sep 2023 21:24:04 +0000 |
parents | d682cac178c2 |
children | 5cbde3c80c24 |
rev | line source |
---|---|
21
af801ab43a33
fteeprom-erase: convert to new local libs
Mychaela Falconia <falcon@freecalypso.org>
parents:
0
diff
changeset
|
1 /* |
af801ab43a33
fteeprom-erase: 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 commands that FTDI |
af801ab43a33
fteeprom-erase: convert to new local libs
Mychaela Falconia <falcon@freecalypso.org>
parents:
0
diff
changeset
|
3 * chip to erase its EEPROM in one fell swoop. This operation is known |
af801ab43a33
fteeprom-erase: convert to new local libs
Mychaela Falconia <falcon@freecalypso.org>
parents:
0
diff
changeset
|
4 * to work on FT2232C/D chips with 5V-powered EEPROMs, but is NOT appropriate |
af801ab43a33
fteeprom-erase: convert to new local libs
Mychaela Falconia <falcon@freecalypso.org>
parents:
0
diff
changeset
|
5 * for FT232R. This operation is also not recommended for external EEPROMs |
af801ab43a33
fteeprom-erase: convert to new local libs
Mychaela Falconia <falcon@freecalypso.org>
parents:
0
diff
changeset
|
6 * powered with 3.3V, as typically used with FT2232H. |
af801ab43a33
fteeprom-erase: convert to new local libs
Mychaela Falconia <falcon@freecalypso.org>
parents:
0
diff
changeset
|
7 * |
af801ab43a33
fteeprom-erase: convert to new local libs
Mychaela Falconia <falcon@freecalypso.org>
parents:
0
diff
changeset
|
8 * The present version has been converted to use our local libraries |
af801ab43a33
fteeprom-erase: convert to new local libs
Mychaela Falconia <falcon@freecalypso.org>
parents:
0
diff
changeset
|
9 * (libftmini and libuwrap) instead of libftdi - thus the external dependency |
af801ab43a33
fteeprom-erase: convert to new local libs
Mychaela Falconia <falcon@freecalypso.org>
parents:
0
diff
changeset
|
10 * is only libusb. |
af801ab43a33
fteeprom-erase: convert to new local libs
Mychaela Falconia <falcon@freecalypso.org>
parents:
0
diff
changeset
|
11 */ |
af801ab43a33
fteeprom-erase: convert to new local libs
Mychaela Falconia <falcon@freecalypso.org>
parents:
0
diff
changeset
|
12 |
0
11b8a30333b3
fteeprom: initial import from freecalypso-hwlab
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
13 #include <sys/types.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> |
21
af801ab43a33
fteeprom-erase: convert to new local libs
Mychaela Falconia <falcon@freecalypso.org>
parents:
0
diff
changeset
|
17 #include <usb.h> |
af801ab43a33
fteeprom-erase: convert to new local libs
Mychaela Falconia <falcon@freecalypso.org>
parents:
0
diff
changeset
|
18 #include "../libuwrap/find_dev.h" |
af801ab43a33
fteeprom-erase: convert to new local libs
Mychaela Falconia <falcon@freecalypso.org>
parents:
0
diff
changeset
|
19 #include "../libuwrap/open_close.h" |
af801ab43a33
fteeprom-erase: 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 |
46
d682cac178c2
fteeprom-erase: implement -d option for interface detach
Mychaela Falconia <falcon@freecalypso.org>
parents:
42
diff
changeset
|
22 char *device_selector; |
d682cac178c2
fteeprom-erase: implement -d option for interface detach
Mychaela Falconia <falcon@freecalypso.org>
parents:
42
diff
changeset
|
23 int detach_num; |
d682cac178c2
fteeprom-erase: implement -d option for interface detach
Mychaela Falconia <falcon@freecalypso.org>
parents:
42
diff
changeset
|
24 |
d682cac178c2
fteeprom-erase: implement -d option for interface detach
Mychaela Falconia <falcon@freecalypso.org>
parents:
42
diff
changeset
|
25 process_cmdline(argc, argv) |
d682cac178c2
fteeprom-erase: implement -d option for interface detach
Mychaela Falconia <falcon@freecalypso.org>
parents:
42
diff
changeset
|
26 char **argv; |
d682cac178c2
fteeprom-erase: implement -d option for interface detach
Mychaela Falconia <falcon@freecalypso.org>
parents:
42
diff
changeset
|
27 { |
d682cac178c2
fteeprom-erase: implement -d option for interface detach
Mychaela Falconia <falcon@freecalypso.org>
parents:
42
diff
changeset
|
28 int c; |
d682cac178c2
fteeprom-erase: implement -d option for interface detach
Mychaela Falconia <falcon@freecalypso.org>
parents:
42
diff
changeset
|
29 extern int optind; |
d682cac178c2
fteeprom-erase: implement -d option for interface detach
Mychaela Falconia <falcon@freecalypso.org>
parents:
42
diff
changeset
|
30 extern char *optarg; |
d682cac178c2
fteeprom-erase: implement -d option for interface detach
Mychaela Falconia <falcon@freecalypso.org>
parents:
42
diff
changeset
|
31 |
d682cac178c2
fteeprom-erase: implement -d option for interface detach
Mychaela Falconia <falcon@freecalypso.org>
parents:
42
diff
changeset
|
32 while ((c = getopt(argc, argv, "d:")) != EOF) { |
d682cac178c2
fteeprom-erase: implement -d option for interface detach
Mychaela Falconia <falcon@freecalypso.org>
parents:
42
diff
changeset
|
33 switch (c) { |
d682cac178c2
fteeprom-erase: implement -d option for interface detach
Mychaela Falconia <falcon@freecalypso.org>
parents:
42
diff
changeset
|
34 case 'd': |
d682cac178c2
fteeprom-erase: implement -d option for interface detach
Mychaela Falconia <falcon@freecalypso.org>
parents:
42
diff
changeset
|
35 detach_num = atoi(optarg); |
d682cac178c2
fteeprom-erase: implement -d option for interface detach
Mychaela Falconia <falcon@freecalypso.org>
parents:
42
diff
changeset
|
36 switch (detach_num) { |
d682cac178c2
fteeprom-erase: implement -d option for interface detach
Mychaela Falconia <falcon@freecalypso.org>
parents:
42
diff
changeset
|
37 case 0: |
d682cac178c2
fteeprom-erase: implement -d option for interface detach
Mychaela Falconia <falcon@freecalypso.org>
parents:
42
diff
changeset
|
38 case 1: |
d682cac178c2
fteeprom-erase: implement -d option for interface detach
Mychaela Falconia <falcon@freecalypso.org>
parents:
42
diff
changeset
|
39 case 2: |
d682cac178c2
fteeprom-erase: implement -d option for interface detach
Mychaela Falconia <falcon@freecalypso.org>
parents:
42
diff
changeset
|
40 case 4: |
d682cac178c2
fteeprom-erase: implement -d option for interface detach
Mychaela Falconia <falcon@freecalypso.org>
parents:
42
diff
changeset
|
41 break; |
d682cac178c2
fteeprom-erase: implement -d option for interface detach
Mychaela Falconia <falcon@freecalypso.org>
parents:
42
diff
changeset
|
42 default: |
d682cac178c2
fteeprom-erase: implement -d option for interface detach
Mychaela Falconia <falcon@freecalypso.org>
parents:
42
diff
changeset
|
43 fprintf(stderr, "error: invalid -d argument\n"); |
d682cac178c2
fteeprom-erase: implement -d option for interface detach
Mychaela Falconia <falcon@freecalypso.org>
parents:
42
diff
changeset
|
44 exit(1); |
d682cac178c2
fteeprom-erase: implement -d option for interface detach
Mychaela Falconia <falcon@freecalypso.org>
parents:
42
diff
changeset
|
45 } |
d682cac178c2
fteeprom-erase: implement -d option for interface detach
Mychaela Falconia <falcon@freecalypso.org>
parents:
42
diff
changeset
|
46 continue; |
d682cac178c2
fteeprom-erase: implement -d option for interface detach
Mychaela Falconia <falcon@freecalypso.org>
parents:
42
diff
changeset
|
47 default: |
d682cac178c2
fteeprom-erase: implement -d option for interface detach
Mychaela Falconia <falcon@freecalypso.org>
parents:
42
diff
changeset
|
48 /* error msg already printed */ |
d682cac178c2
fteeprom-erase: implement -d option for interface detach
Mychaela Falconia <falcon@freecalypso.org>
parents:
42
diff
changeset
|
49 exit(1); |
d682cac178c2
fteeprom-erase: implement -d option for interface detach
Mychaela Falconia <falcon@freecalypso.org>
parents:
42
diff
changeset
|
50 } |
d682cac178c2
fteeprom-erase: implement -d option for interface detach
Mychaela Falconia <falcon@freecalypso.org>
parents:
42
diff
changeset
|
51 } |
d682cac178c2
fteeprom-erase: implement -d option for interface detach
Mychaela Falconia <falcon@freecalypso.org>
parents:
42
diff
changeset
|
52 if (argc != optind + 1) { |
d682cac178c2
fteeprom-erase: implement -d option for interface detach
Mychaela Falconia <falcon@freecalypso.org>
parents:
42
diff
changeset
|
53 fprintf(stderr, "usage: %s [options] device-selector\n", |
d682cac178c2
fteeprom-erase: implement -d option for interface detach
Mychaela Falconia <falcon@freecalypso.org>
parents:
42
diff
changeset
|
54 argv[0]); |
d682cac178c2
fteeprom-erase: implement -d option for interface detach
Mychaela Falconia <falcon@freecalypso.org>
parents:
42
diff
changeset
|
55 exit(1); |
d682cac178c2
fteeprom-erase: implement -d option for interface detach
Mychaela Falconia <falcon@freecalypso.org>
parents:
42
diff
changeset
|
56 } |
d682cac178c2
fteeprom-erase: implement -d option for interface detach
Mychaela Falconia <falcon@freecalypso.org>
parents:
42
diff
changeset
|
57 device_selector = argv[optind]; |
d682cac178c2
fteeprom-erase: implement -d option for interface detach
Mychaela Falconia <falcon@freecalypso.org>
parents:
42
diff
changeset
|
58 } |
d682cac178c2
fteeprom-erase: implement -d option for interface detach
Mychaela Falconia <falcon@freecalypso.org>
parents:
42
diff
changeset
|
59 |
0
11b8a30333b3
fteeprom: initial import from freecalypso-hwlab
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
60 main(argc, argv) |
11b8a30333b3
fteeprom: initial import from freecalypso-hwlab
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
61 char **argv; |
11b8a30333b3
fteeprom: initial import from freecalypso-hwlab
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
62 { |
21
af801ab43a33
fteeprom-erase: convert to new local libs
Mychaela Falconia <falcon@freecalypso.org>
parents:
0
diff
changeset
|
63 struct usb_device *dev; |
af801ab43a33
fteeprom-erase: convert to new local libs
Mychaela Falconia <falcon@freecalypso.org>
parents:
0
diff
changeset
|
64 usb_dev_handle *usbh; |
0
11b8a30333b3
fteeprom: initial import from freecalypso-hwlab
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
65 |
46
d682cac178c2
fteeprom-erase: implement -d option for interface detach
Mychaela Falconia <falcon@freecalypso.org>
parents:
42
diff
changeset
|
66 process_cmdline(argc, argv); |
d682cac178c2
fteeprom-erase: implement -d option for interface detach
Mychaela Falconia <falcon@freecalypso.org>
parents:
42
diff
changeset
|
67 dev = find_usbdev_by_desc_string(device_selector); |
21
af801ab43a33
fteeprom-erase: convert to new local libs
Mychaela Falconia <falcon@freecalypso.org>
parents:
0
diff
changeset
|
68 if (!dev) { |
af801ab43a33
fteeprom-erase: convert to new local libs
Mychaela Falconia <falcon@freecalypso.org>
parents:
0
diff
changeset
|
69 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
|
70 exit(1); |
11b8a30333b3
fteeprom: initial import from freecalypso-hwlab
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
71 } |
42
2746b160afb7
fteeprom-erase: we don't need to claim the interface
Mychaela Falconia <falcon@freecalypso.org>
parents:
21
diff
changeset
|
72 usbh = usb_open(dev); |
2746b160afb7
fteeprom-erase: we don't need to claim the interface
Mychaela Falconia <falcon@freecalypso.org>
parents:
21
diff
changeset
|
73 if (!usbh) { |
2746b160afb7
fteeprom-erase: we don't need to claim the interface
Mychaela Falconia <falcon@freecalypso.org>
parents:
21
diff
changeset
|
74 fprintf(stderr, "error: usb_open() failed\n"); |
2746b160afb7
fteeprom-erase: we don't need to claim the interface
Mychaela Falconia <falcon@freecalypso.org>
parents:
21
diff
changeset
|
75 exit(1); |
2746b160afb7
fteeprom-erase: we don't need to claim the interface
Mychaela Falconia <falcon@freecalypso.org>
parents:
21
diff
changeset
|
76 } |
46
d682cac178c2
fteeprom-erase: implement -d option for interface detach
Mychaela Falconia <falcon@freecalypso.org>
parents:
42
diff
changeset
|
77 if (detach_num) |
d682cac178c2
fteeprom-erase: implement -d option for interface detach
Mychaela Falconia <falcon@freecalypso.org>
parents:
42
diff
changeset
|
78 usbwrap_claim_all_ifs(usbh, detach_num); |
21
af801ab43a33
fteeprom-erase: convert to new local libs
Mychaela Falconia <falcon@freecalypso.org>
parents:
0
diff
changeset
|
79 ftmini_erase_eeprom(usbh); |
42
2746b160afb7
fteeprom-erase: we don't need to claim the interface
Mychaela Falconia <falcon@freecalypso.org>
parents:
21
diff
changeset
|
80 usb_close(usbh); |
0
11b8a30333b3
fteeprom: initial import from freecalypso-hwlab
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
81 exit(0); |
11b8a30333b3
fteeprom: initial import from freecalypso-hwlab
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
82 } |