xref: /aosp_15_r20/external/libusb/examples/fxload.c (revision 86b64dcb59b3a0b37502ecd56e119234366a6f7e)
1 /*
2  * Copyright © 2001 Stephen Williams ([email protected])
3  * Copyright © 2001-2002 David Brownell ([email protected])
4  * Copyright © 2008 Roger Williams ([email protected])
5  * Copyright © 2012 Pete Batard ([email protected])
6  * Copyright © 2013 Federico Manzan ([email protected])
7  *
8  *    This source code is free software; you can redistribute it
9  *    and/or modify it in source code form under the terms of the GNU
10  *    General Public License as published by the Free Software
11  *    Foundation; either version 2 of the License, or (at your option)
12  *    any later version.
13  *
14  *    This program is distributed in the hope that it will be useful,
15  *    but WITHOUT ANY WARRANTY; without even the implied warranty of
16  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  *    GNU General Public License for more details.
18  *
19  *    You should have received a copy of the GNU General Public License
20  *    along with this program; if not, write to the Free Software
21  *    Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
22  */
23 
24 #include <config.h>
25 
26 #include <stdlib.h>
27 #include <stdio.h>
28 #include <string.h>
29 #include <stdint.h>
30 #include <stdarg.h>
31 #include <sys/types.h>
32 #include <getopt.h>
33 
34 #include "libusb.h"
35 #include "ezusb.h"
36 
37 #if !defined(_WIN32) || defined(__CYGWIN__)
38 #include <syslog.h>
39 static bool dosyslog = false;
40 #include <strings.h>
41 #define libusb_strcasecmp strcasecmp
42 #else
43 #define libusb_strcasecmp _stricmp
44 #endif
45 
46 #ifndef FXLOAD_VERSION
47 #define FXLOAD_VERSION (__DATE__ " (libusb)")
48 #endif
49 
50 #ifndef ARRAYSIZE
51 #define ARRAYSIZE(A) (sizeof(A)/sizeof((A)[0]))
52 #endif
53 
logerror(const char * format,...)54 void logerror(const char *format, ...)
55 {
56 	va_list ap;
57 	va_start(ap, format);
58 
59 #if !defined(_WIN32) || defined(__CYGWIN__)
60 	if (dosyslog)
61 		vsyslog(LOG_ERR, format, ap);
62 	else
63 #endif
64 		vfprintf(stderr, format, ap);
65 	va_end(ap);
66 }
67 
print_usage(int error_code)68 static int print_usage(int error_code) {
69 	fprintf(stderr, "\nUsage: fxload [-v] [-V] [-t type] [-d vid:pid] [-p bus,addr] [-s loader] -i firmware\n");
70 	fprintf(stderr, "  -i <path>       -- Firmware to upload\n");
71 	fprintf(stderr, "  -s <path>       -- Second stage loader\n");
72 	fprintf(stderr, "  -t <type>       -- Target type: an21, fx, fx2, fx2lp, fx3\n");
73 	fprintf(stderr, "  -d <vid:pid>    -- Target device, as an USB VID:PID\n");
74 	fprintf(stderr, "  -p <bus,addr>   -- Target device, as a libusb bus number and device address path\n");
75 	fprintf(stderr, "  -v              -- Increase verbosity\n");
76 	fprintf(stderr, "  -q              -- Decrease verbosity (silent mode)\n");
77 	fprintf(stderr, "  -V              -- Print program version\n");
78 	return error_code;
79 }
80 
81 #define FIRMWARE 0
82 #define LOADER 1
main(int argc,char * argv[])83 int main(int argc, char*argv[])
84 {
85 	fx_known_device known_device[] = FX_KNOWN_DEVICES;
86 	const char *path[] = { NULL, NULL };
87 	const char *device_id = NULL;
88 	const char *device_path = getenv("DEVICE");
89 	const char *type = NULL;
90 	const char *fx_name[FX_TYPE_MAX] = FX_TYPE_NAMES;
91 	const char *ext, *img_name[] = IMG_TYPE_NAMES;
92 	int fx_type = FX_TYPE_UNDEFINED, img_type[ARRAYSIZE(path)];
93 	int opt, status;
94 	unsigned int i, j;
95 	unsigned vid = 0, pid = 0;
96 	unsigned busnum = 0, devaddr = 0, _busnum, _devaddr;
97 	libusb_device *dev, **devs;
98 	libusb_device_handle *device = NULL;
99 	struct libusb_device_descriptor desc;
100 
101 	while ((opt = getopt(argc, argv, "qvV?hd:p:i:I:s:S:t:")) != EOF)
102 		switch (opt) {
103 
104 		case 'd':
105 			device_id = optarg;
106 			if (sscanf(device_id, "%x:%x" , &vid, &pid) != 2 ) {
107 				fputs ("please specify VID & PID as \"vid:pid\" in hexadecimal format\n", stderr);
108 				return -1;
109 			}
110 			break;
111 
112 		case 'p':
113 			device_path = optarg;
114 			if (sscanf(device_path, "%u,%u", &busnum, &devaddr) != 2 ) {
115 				fputs ("please specify bus number & device number as \"bus,dev\" in decimal format\n", stderr);
116 				return -1;
117 			}
118 			break;
119 
120 		case 'i':
121 		case 'I':
122 			path[FIRMWARE] = optarg;
123 			break;
124 
125 		case 's':
126 		case 'S':
127 			path[LOADER] = optarg;
128 			break;
129 
130 		case 'V':
131 			puts(FXLOAD_VERSION);
132 			return 0;
133 
134 		case 't':
135 			type = optarg;
136 			break;
137 
138 		case 'v':
139 			verbose++;
140 			break;
141 
142 		case 'q':
143 			verbose--;
144 			break;
145 
146 		case '?':
147 		case 'h':
148 		default:
149 			return print_usage(-1);
150 
151 	}
152 
153 	if (path[FIRMWARE] == NULL) {
154 		logerror("no firmware specified!\n");
155 		return print_usage(-1);
156 	}
157 	if ((device_id != NULL) && (device_path != NULL)) {
158 		logerror("only one of -d or -p can be specified\n");
159 		return print_usage(-1);
160 	}
161 
162 	/* determine the target type */
163 	if (type != NULL) {
164 		for (i=0; i<FX_TYPE_MAX; i++) {
165 			if (strcmp(type, fx_name[i]) == 0) {
166 				fx_type = i;
167 				break;
168 			}
169 		}
170 		if (i >= FX_TYPE_MAX) {
171 			logerror("illegal microcontroller type: %s\n", type);
172 			return print_usage(-1);
173 		}
174 	}
175 
176 	/* open the device using libusb */
177 	status = libusb_init_context(/*ctx=*/NULL, /*options=*/NULL, /*num_options=*/0);
178 	if (status < 0) {
179 		logerror("libusb_init_context() failed: %s\n", libusb_error_name(status));
180 		return -1;
181 	}
182 	libusb_set_option(NULL, LIBUSB_OPTION_LOG_LEVEL, verbose);
183 
184 	/* try to pick up missing parameters from known devices */
185 	if ((type == NULL) || (device_id == NULL) || (device_path != NULL)) {
186 		if (libusb_get_device_list(NULL, &devs) < 0) {
187 			logerror("libusb_get_device_list() failed: %s\n", libusb_error_name(status));
188 			goto err;
189 		}
190 		for (i=0; (dev=devs[i]) != NULL; i++) {
191 			_busnum = libusb_get_bus_number(dev);
192 			_devaddr = libusb_get_device_address(dev);
193 			if ((type != NULL) && (device_path != NULL)) {
194 				// if both a type and bus,addr were specified, we just need to find our match
195 				if ((libusb_get_bus_number(dev) == busnum) && (libusb_get_device_address(dev) == devaddr))
196 					break;
197 			} else {
198 				status = libusb_get_device_descriptor(dev, &desc);
199 				if (status >= 0) {
200 					if (verbose >= 3) {
201 						logerror("examining %04x:%04x (%d,%d)\n",
202 							desc.idVendor, desc.idProduct, _busnum, _devaddr);
203 					}
204 					for (j=0; j<ARRAYSIZE(known_device); j++) {
205 						if ((desc.idVendor == known_device[j].vid)
206 							&& (desc.idProduct == known_device[j].pid)) {
207 							if (// nothing was specified
208 								((type == NULL) && (device_id == NULL) && (device_path == NULL)) ||
209 								// vid:pid was specified and we have a match
210 								((type == NULL) && (device_id != NULL) && (vid == desc.idVendor) && (pid == desc.idProduct)) ||
211 								// bus,addr was specified and we have a match
212 								((type == NULL) && (device_path != NULL) && (busnum == _busnum) && (devaddr == _devaddr)) ||
213 								// type was specified and we have a match
214 								((type != NULL) && (device_id == NULL) && (device_path == NULL) && (fx_type == known_device[j].type)) ) {
215 								fx_type = known_device[j].type;
216 								vid = desc.idVendor;
217 								pid = desc.idProduct;
218 								busnum = _busnum;
219 								devaddr = _devaddr;
220 								break;
221 							}
222 						}
223 					}
224 					if (j < ARRAYSIZE(known_device)) {
225 						if (verbose)
226 							logerror("found device '%s' [%04x:%04x] (%d,%d)\n",
227 								known_device[j].designation, vid, pid, busnum, devaddr);
228 						break;
229 					}
230 				}
231 			}
232 		}
233 		if (dev == NULL) {
234 			libusb_free_device_list(devs, 1);
235 			libusb_exit(NULL);
236 			logerror("could not find a known device - please specify type and/or vid:pid and/or bus,dev\n");
237 			return print_usage(-1);
238 		}
239 		status = libusb_open(dev, &device);
240 		libusb_free_device_list(devs, 1);
241 		if (status < 0) {
242 			logerror("libusb_open() failed: %s\n", libusb_error_name(status));
243 			goto err;
244 		}
245 	} else if (device_id != NULL) {
246 		device = libusb_open_device_with_vid_pid(NULL, (uint16_t)vid, (uint16_t)pid);
247 		if (device == NULL) {
248 			logerror("libusb_open() failed\n");
249 			goto err;
250 		}
251 	}
252 
253 	/* We need to claim the first interface */
254 	libusb_set_auto_detach_kernel_driver(device, 1);
255 	status = libusb_claim_interface(device, 0);
256 	if (status != LIBUSB_SUCCESS) {
257 		libusb_close(device);
258 		logerror("libusb_claim_interface failed: %s\n", libusb_error_name(status));
259 		goto err;
260 	}
261 
262 	if (verbose)
263 		logerror("microcontroller type: %s\n", fx_name[fx_type]);
264 
265 	for (i=0; i<ARRAYSIZE(path); i++) {
266 		if (path[i] != NULL) {
267 			ext = path[i] + strlen(path[i]) - 4;
268 			if ((libusb_strcasecmp(ext, ".hex") == 0) || (libusb_strcasecmp(ext, ".ihx") == 0))
269 				img_type[i] = IMG_TYPE_HEX;
270 			else if (libusb_strcasecmp(ext, ".iic") == 0)
271 				img_type[i] = IMG_TYPE_IIC;
272 			else if (libusb_strcasecmp(ext, ".bix") == 0)
273 				img_type[i] = IMG_TYPE_BIX;
274 			else if (libusb_strcasecmp(ext, ".img") == 0)
275 				img_type[i] = IMG_TYPE_IMG;
276 			else {
277 				logerror("%s is not a recognized image type\n", path[i]);
278 				goto err;
279 			}
280 		}
281 		if (verbose && path[i] != NULL)
282 			logerror("%s: type %s\n", path[i], img_name[img_type[i]]);
283 	}
284 
285 	if (path[LOADER] == NULL) {
286 		/* single stage, put into internal memory */
287 		if (verbose > 1)
288 			logerror("single stage: load on-chip memory\n");
289 		status = ezusb_load_ram(device, path[FIRMWARE], fx_type, img_type[FIRMWARE], 0);
290 	} else {
291 		/* two-stage, put loader into internal memory */
292 		if (verbose > 1)
293 			logerror("1st stage: load 2nd stage loader\n");
294 		status = ezusb_load_ram(device, path[LOADER], fx_type, img_type[LOADER], 0);
295 		if (status == 0) {
296 			/* two-stage, put firmware into internal memory */
297 			if (verbose > 1)
298 				logerror("2nd state: load on-chip memory\n");
299 			status = ezusb_load_ram(device, path[FIRMWARE], fx_type, img_type[FIRMWARE], 1);
300 		}
301 	}
302 
303 	libusb_release_interface(device, 0);
304 	libusb_close(device);
305 	libusb_exit(NULL);
306 	return status;
307 err:
308 	libusb_exit(NULL);
309 	return -1;
310 }
311