| 1 |
/* |
| 2 |
* dfu-util |
| 3 |
* |
| 4 |
* Copyright 2007-2008 by OpenMoko, Inc. |
| 5 |
* Copyright 2010-2012 Stefan Schmidt |
| 6 |
* Copyright 2013-2014 Hans Petter Selasky <[email protected]> |
| 7 |
* Copyright 2010-2021 Tormod Volden |
| 8 |
* |
| 9 |
* Originally written by Harald Welte <[email protected]> |
| 10 |
* |
| 11 |
* Based on existing code of dfu-programmer-0.4 |
| 12 |
* |
| 13 |
* This program is free software; you can redistribute it and/or modify |
| 14 |
* it under the terms of the GNU General Public License as published by |
| 15 |
* the Free Software Foundation; either version 2 of the License, or |
| 16 |
* (at your option) any later version. |
| 17 |
* |
| 18 |
* This program is distributed in the hope that it will be useful, |
| 19 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 20 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 21 |
* GNU General Public License for more details. |
| 22 |
* |
| 23 |
* You should have received a copy of the GNU General Public License |
| 24 |
* along with this program; if not, write to the Free Software |
| 25 |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 26 |
*/ |
| 27 |
|
| 28 |
#ifdef HAVE_CONFIG_H |
| 29 |
# include "config.h" |
| 30 |
#endif |
| 31 |
|
| 32 |
#include <stdlib.h> |
| 33 |
#include <stdio.h> |
| 34 |
#include <string.h> |
| 35 |
#include <getopt.h> |
| 36 |
#include <libusb.h> |
| 37 |
#include <errno.h> |
| 38 |
#include <fcntl.h> |
| 39 |
#include <limits.h> |
| 40 |
|
| 41 |
#include "portable.h" |
| 42 |
#include "dfu.h" |
| 43 |
#include "dfu_file.h" |
| 44 |
#include "dfu_load.h" |
| 45 |
#include "dfu_util.h" |
| 46 |
#include "dfuse.h" |
| 47 |
|
| 48 |
int verbose = 0; |
| 49 |
|
| 50 |
struct dfu_if *dfu_root = NULL; |
| 51 |
|
| 52 |
char *match_path = NULL; |
| 53 |
int match_vendor = -1; |
| 54 |
int match_product = -1; |
| 55 |
int match_vendor_dfu = -1; |
| 56 |
int match_product_dfu = -1; |
| 57 |
int match_config_index = -1; |
| 58 |
int match_iface_index = -1; |
| 59 |
int match_iface_alt_index = -1; |
| 60 |
int match_devnum = -1; |
| 61 |
const char *match_iface_alt_name = NULL; |
| 62 |
const char *match_serial = NULL; |
| 63 |
const char *match_serial_dfu = NULL; |
| 64 |
|
| 65 |
static int parse_match_value(const char *str, int default_value) |
| 66 |
{ |
| 67 |
char *remainder; |
| 68 |
int value; |
| 69 |
|
| 70 |
if (str == NULL) { |
| 71 |
value = default_value; |
| 72 |
} else if (*str == '*') { |
| 73 |
value = -1; /* Match anything */ |
| 74 |
} else if (*str == '-') { |
| 75 |
value = 0x10000; /* Impossible vendor/product ID */ |
| 76 |
} else { |
| 77 |
value = strtoul(str, &remainder, 16); |
| 78 |
if (remainder == str) { |
| 79 |
value = default_value; |
| 80 |
} |
| 81 |
} |
| 82 |
return value; |
| 83 |
} |
| 84 |
|
| 85 |
static void parse_vendprod(const char *str) |
| 86 |
{ |
| 87 |
const char *comma; |
| 88 |
const char *colon; |
| 89 |
|
| 90 |
/* Default to match any DFU device in runtime or DFU mode */ |
| 91 |
match_vendor = -1; |
| 92 |
match_product = -1; |
| 93 |
match_vendor_dfu = -1; |
| 94 |
match_product_dfu = -1; |
| 95 |
|
| 96 |
comma = strchr(str, ','); |
| 97 |
if (comma == str) { |
| 98 |
/* DFU mode vendor/product being specified without any runtime |
| 99 |
* vendor/product specification, so don't match any runtime device */ |
| 100 |
match_vendor = match_product = 0x10000; |
| 101 |
} else { |
| 102 |
colon = strchr(str, ':'); |
| 103 |
if (colon != NULL) { |
| 104 |
++colon; |
| 105 |
if ((comma != NULL) && (colon > comma)) { |
| 106 |
colon = NULL; |
| 107 |
} |
| 108 |
} |
| 109 |
match_vendor = parse_match_value(str, match_vendor); |
| 110 |
match_product = parse_match_value(colon, match_product); |
| 111 |
if (comma != NULL) { |
| 112 |
/* Both runtime and DFU mode vendor/product specifications are |
| 113 |
* available, so default DFU mode match components to the given |
| 114 |
* runtime match components */ |
| 115 |
match_vendor_dfu = match_vendor; |
| 116 |
match_product_dfu = match_product; |
| 117 |
} |
| 118 |
} |
| 119 |
if (comma != NULL) { |
| 120 |
++comma; |
| 121 |
colon = strchr(comma, ':'); |
| 122 |
if (colon != NULL) { |
| 123 |
++colon; |
| 124 |
} |
| 125 |
match_vendor_dfu = parse_match_value(comma, match_vendor_dfu); |
| 126 |
match_product_dfu = parse_match_value(colon, match_product_dfu); |
| 127 |
} |
| 128 |
} |
| 129 |
|
| 130 |
static void parse_serial(char *str) |
| 131 |
{ |
| 132 |
char *comma; |
| 133 |
|
| 134 |
match_serial = str; |
| 135 |
comma = strchr(str, ','); |
| 136 |
if (comma == NULL) { |
| 137 |
match_serial_dfu = match_serial; |
| 138 |
} else { |
| 139 |
*comma++ = 0; |
| 140 |
match_serial_dfu = comma; |
| 141 |
} |
| 142 |
if (*match_serial == 0) match_serial = NULL; |
| 143 |
if (*match_serial_dfu == 0) match_serial_dfu = NULL; |
| 144 |
} |
| 145 |
|
| 146 |
static int parse_number(char *str, char *nmb) |
| 147 |
{ |
| 148 |
char *endptr; |
| 149 |
long val; |
| 150 |
|
| 151 |
errno = 0; |
| 152 |
val = strtol(nmb, &endptr, 0); |
| 153 |
|
| 154 |
if ((errno == ERANGE && (val == LONG_MAX || val == LONG_MIN)) |
| 155 |
|| (errno != 0 && val == 0) || (*endptr != '\0')) { |
| 156 |
errx(EX_USAGE, "Something went wrong with the argument of --%s\n", str); |
| 157 |
} |
| 158 |
|
| 159 |
if (endptr == nmb) { |
| 160 |
errx(EX_USAGE, "No digits were found from the argument of --%s\n", str); |
| 161 |
} |
| 162 |
|
| 163 |
return (int)val; |
| 164 |
} |
| 165 |
|
| 166 |
static void help(void) |
| 167 |
{ |
| 168 |
fprintf(stderr, "Usage: dfu-util [options] ...\n" |
| 169 |
" -h --help\t\t\tPrint this help message\n" |
| 170 |
" -V --version\t\t\tPrint the version number\n" |
| 171 |
" -v --verbose\t\t\tPrint verbose debug statements\n" |
| 172 |
" -l --list\t\t\tList currently attached DFU capable devices\n"); |
| 173 |
fprintf(stderr, " -e --detach\t\t\tDetach currently attached DFU capable devices\n" |
| 174 |
" -E --detach-delay seconds\tTime to wait before reopening a device after detach\n" |
| 175 |
" -d --device <vendor>:<product>[,<vendor_dfu>:<product_dfu>]\n" |
| 176 |
"\t\t\t\tSpecify Vendor/Product ID(s) of DFU device\n" |
| 177 |
" -n --devnum <dnum>\t\tMatch given device number (devnum from --list)\n" |
| 178 |
" -p --path <bus-port. ... .port>\tSpecify path to DFU device\n" |
| 179 |
" -c --cfg <config_nr>\t\tSpecify the Configuration of DFU device\n" |
| 180 |
" -i --intf <intf_nr>\t\tSpecify the DFU Interface number\n" |
| 181 |
" -S --serial <serial_string>[,<serial_string_dfu>]\n" |
| 182 |
"\t\t\t\tSpecify Serial String of DFU device\n" |
| 183 |
" -a --alt <alt>\t\tSpecify the Altsetting of the DFU Interface\n" |
| 184 |
"\t\t\t\tby name or by number\n"); |
| 185 |
fprintf(stderr, " -t --transfer-size <size>\tSpecify the number of bytes per USB Transfer\n" |
| 186 |
" -U --upload <file>\t\tRead firmware from device into <file>\n" |
| 187 |
" -Z --upload-size <bytes>\tSpecify the expected upload size in bytes\n" |
| 188 |
" -D --download <file>\t\tWrite firmware from <file> into device\n" |
| 189 |
" -R --reset\t\t\tIssue USB Reset signalling once we're finished\n" |
| 190 |
" -w --wait\t\t\tWait for device to appear\n" |
| 191 |
" -s --dfuse-address address<:...>\tST DfuSe mode string, specifying target\n" |
| 192 |
"\t\t\t\taddress for raw file download or upload (not\n" |
| 193 |
"\t\t\t\tapplicable for DfuSe file (.dfu) downloads).\n" |
| 194 |
"\t\t\t\tAdd more DfuSe options separated with ':'\n" |
| 195 |
"\t\tleave\t\tLeave DFU mode (jump to application)\n" |
| 196 |
"\t\tmass-erase\tErase the whole device (requires \"force\")\n" |
| 197 |
"\t\tunprotect\tErase read protected device (requires \"force\")\n" |
| 198 |
"\t\twill-reset\tExpect device to reset (e.g. option bytes write)\n" |
| 199 |
"\t\tforce\t\tYou really know what you are doing!\n" |
| 200 |
"\t\t<length>\tLength of firmware to upload from device\n" |
| 201 |
); |
| 202 |
} |
| 203 |
|
| 204 |
static void print_version(void) |
| 205 |
{ |
| 206 |
printf(PACKAGE_STRING "\n\n"); |
| 207 |
printf("Copyright 2005-2009 Weston Schmidt, Harald Welte and OpenMoko Inc.\n" |
| 208 |
"Copyright 2010-2021 Tormod Volden and Stefan Schmidt\n" |
| 209 |
"This program is Free Software and has ABSOLUTELY NO WARRANTY\n" |
| 210 |
"Please report bugs to " PACKAGE_BUGREPORT "\n\n"); |
| 211 |
} |
| 212 |
|
| 213 |
static struct option opts[] = { |
| 214 |
{ "help", 0, 0, 'h' }, |
| 215 |
{ "version", 0, 0, 'V' }, |
| 216 |
{ "verbose", 0, 0, 'v' }, |
| 217 |
{ "list", 0, 0, 'l' }, |
| 218 |
{ "detach", 0, 0, 'e' }, |
| 219 |
{ "detach-delay", 1, 0, 'E' }, |
| 220 |
{ "device", 1, 0, 'd' }, |
| 221 |
{ "path", 1, 0, 'p' }, |
| 222 |
{ "configuration", 1, 0, 'c' }, |
| 223 |
{ "cfg", 1, 0, 'c' }, |
| 224 |
{ "interface", 1, 0, 'i' }, |
| 225 |
{ "intf", 1, 0, 'i' }, |
| 226 |
{ "altsetting", 1, 0, 'a' }, |
| 227 |
{ "alt", 1, 0, 'a' }, |
| 228 |
{ "serial", 1, 0, 'S' }, |
| 229 |
{ "transfer-size", 1, 0, 't' }, |
| 230 |
{ "upload", 1, 0, 'U' }, |
| 231 |
{ "upload-size", 1, 0, 'Z' }, |
| 232 |
{ "download", 1, 0, 'D' }, |
| 233 |
{ "reset", 0, 0, 'R' }, |
| 234 |
{ "dfuse-address", 1, 0, 's' }, |
| 235 |
{ "devnum",1, 0, 'n' }, |
| 236 |
{ "wait", 1, 0, 'w' }, |
| 237 |
{ 0, 0, 0, 0 } |
| 238 |
}; |
| 239 |
|
| 240 |
int main(int argc, char **argv) |
| 241 |
{ |
| 242 |
int expected_size = 0; |
| 243 |
unsigned int transfer_size = 0; |
| 244 |
enum mode mode = MODE_NONE; |
| 245 |
struct dfu_status status; |
| 246 |
libusb_context *ctx; |
| 247 |
struct dfu_file file; |
| 248 |
char *end; |
| 249 |
int final_reset = 0; |
| 250 |
int wait_device = 0; |
| 251 |
int ret; |
| 252 |
int dfuse_device = 0; |
| 253 |
int fd; |
| 254 |
const char *dfuse_options = NULL; |
| 255 |
int detach_delay = 5; |
| 256 |
uint16_t runtime_vendor; |
| 257 |
uint16_t runtime_product; |
| 258 |
|
| 259 |
memset(&file, 0, sizeof(file)); |
| 260 |
|
| 261 |
/* make sure all prints are flushed */ |
| 262 |
setvbuf(stdout, NULL, _IONBF, 0); |
| 263 |
|
| 264 |
while (1) { |
| 265 |
int c, option_index = 0; |
| 266 |
c = getopt_long(argc, argv, "hVvleE:d:p:c:i:a:S:t:U:D:Rs:Z:wn:", opts, |
| 267 |
&option_index); |
| 268 |
if (c == -1) |
| 269 |
break; |
| 270 |
|
| 271 |
switch (c) { |
| 272 |
case 'h': |
| 273 |
help(); |
| 274 |
exit(EX_OK); |
| 275 |
break; |
| 276 |
case 'V': |
| 277 |
mode = MODE_VERSION; |
| 278 |
break; |
| 279 |
case 'v': |
| 280 |
verbose++; |
| 281 |
break; |
| 282 |
case 'l': |
| 283 |
mode = MODE_LIST; |
| 284 |
break; |
| 285 |
case 'e': |
| 286 |
mode = MODE_DETACH; |
| 287 |
break; |
| 288 |
case 'E': |
| 289 |
detach_delay = parse_number("detach-delay", optarg); |
| 290 |
break; |
| 291 |
case 'd': |
| 292 |
parse_vendprod(optarg); |
| 293 |
break; |
| 294 |
case 'p': |
| 295 |
#if (defined(LIBUSB_API_VERSION) && LIBUSB_API_VERSION >= 0x01000102) || (defined(LIBUSBX_API_VERSION) && LIBUSBX_API_VERSION >= 0x01000102) |
| 296 |
match_path = optarg; |
| 297 |
#else |
| 298 |
errx(EX_SOFTWARE, "This dfu-util was built without USB path support"); |
| 299 |
#endif |
| 300 |
break; |
| 301 |
case 'c': |
| 302 |
/* Configuration */ |
| 303 |
match_config_index = parse_number("cfg", optarg); |
| 304 |
break; |
| 305 |
case 'i': |
| 306 |
/* Interface */ |
| 307 |
match_iface_index = parse_number("intf", optarg); |
| 308 |
break; |
| 309 |
case 'a': |
| 310 |
/* Interface Alternate Setting */ |
| 311 |
match_iface_alt_index = strtoul(optarg, &end, 0); |
| 312 |
if (*end) { |
| 313 |
match_iface_alt_name = optarg; |
| 314 |
match_iface_alt_index = -1; |
| 315 |
} |
| 316 |
break; |
| 317 |
case 'n': |
| 318 |
match_devnum = atoi(optarg); |
| 319 |
break; |
| 320 |
case 'S': |
| 321 |
parse_serial(optarg); |
| 322 |
break; |
| 323 |
case 't': |
| 324 |
transfer_size = parse_number("transfer-size", optarg); |
| 325 |
break; |
| 326 |
case 'U': |
| 327 |
mode = MODE_UPLOAD; |
| 328 |
file.name = optarg; |
| 329 |
break; |
| 330 |
case 'Z': |
| 331 |
expected_size = parse_number("upload-size", optarg); |
| 332 |
break; |
| 333 |
case 'D': |
| 334 |
mode = MODE_DOWNLOAD; |
| 335 |
file.name = optarg; |
| 336 |
break; |
| 337 |
case 'R': |
| 338 |
final_reset = 1; |
| 339 |
break; |
| 340 |
case 's': |
| 341 |
dfuse_options = optarg; |
| 342 |
break; |
| 343 |
case 'w': |
| 344 |
wait_device = 1; |
| 345 |
break; |
| 346 |
default: |
| 347 |
help(); |
| 348 |
exit(EX_USAGE); |
| 349 |
break; |
| 350 |
} |
| 351 |
} |
| 352 |
if (optind != argc) { |
| 353 |
fprintf(stderr, "Error: Unexpected argument: %s\n\n", argv[optind]); |
| 354 |
help(); |
| 355 |
exit(EX_USAGE); |
| 356 |
} |
| 357 |
|
| 358 |
print_version(); |
| 359 |
if (mode == MODE_VERSION) { |
| 360 |
exit(EX_OK); |
| 361 |
} |
| 362 |
|
| 363 |
#if defined(LIBUSB_API_VERSION) || defined(LIBUSBX_API_VERSION) |
| 364 |
if (verbose) { |
| 365 |
const struct libusb_version *ver; |
| 366 |
ver = libusb_get_version(); |
| 367 |
printf("libusb version %i.%i.%i%s (%i)\n", ver->major, |
| 368 |
ver->minor, ver->micro, ver->rc, ver->nano); |
| 369 |
} |
| 370 |
#else |
| 371 |
warnx("libusb version is ancient"); |
| 372 |
#endif |
| 373 |
|
| 374 |
if (mode == MODE_NONE && !dfuse_options) { |
| 375 |
fprintf(stderr, "You need to specify one of -D or -U\n"); |
| 376 |
help(); |
| 377 |
exit(EX_USAGE); |
| 378 |
} |
| 379 |
|
| 380 |
if (match_config_index == 0) { |
| 381 |
/* Handle "-c 0" (unconfigured device) as don't care */ |
| 382 |
match_config_index = -1; |
| 383 |
} |
| 384 |
|
| 385 |
if (mode == MODE_DOWNLOAD) { |
| 386 |
dfu_load_file(&file, MAYBE_SUFFIX, MAYBE_PREFIX); |
| 387 |
/* If the user didn't specify product and/or vendor IDs to match, |
| 388 |
* use any IDs from the file suffix for device matching */ |
| 389 |
if (match_vendor < 0 && file.idVendor != 0xffff) { |
| 390 |
match_vendor = file.idVendor; |
| 391 |
printf("Match vendor ID from file: %04x\n", match_vendor); |
| 392 |
} |
| 393 |
if (match_product < 0 && file.idProduct != 0xffff) { |
| 394 |
match_product = file.idProduct; |
| 395 |
printf("Match product ID from file: %04x\n", match_product); |
| 396 |
} |
| 397 |
} else if (mode == MODE_NONE && dfuse_options) { |
| 398 |
/* for DfuSe special commands, match any device */ |
| 399 |
mode = MODE_DOWNLOAD; |
| 400 |
file.idVendor = 0xffff; |
| 401 |
file.idProduct = 0xffff; |
| 402 |
} |
| 403 |
|
| 404 |
if (wait_device) { |
| 405 |
printf("Waiting for device, exit with ctrl-C\n"); |
| 406 |
} |
| 407 |
|
| 408 |
ret = libusb_init(&ctx); |
| 409 |
if (ret) |
| 410 |
errx(EX_IOERR, "unable to initialize libusb: %s", libusb_error_name(ret)); |
| 411 |
|
| 412 |
if (verbose > 2) { |
| 413 |
#if defined(LIBUSB_API_VERSION) && LIBUSB_API_VERSION >= 0x01000106 |
| 414 |
libusb_set_option(ctx, LIBUSB_OPTION_LOG_LEVEL, LIBUSB_LOG_LEVEL_DEBUG); |
| 415 |
#else |
| 416 |
libusb_set_debug(ctx, 255); |
| 417 |
#endif |
| 418 |
} |
| 419 |
probe: |
| 420 |
probe_devices(ctx); |
| 421 |
|
| 422 |
if (mode == MODE_LIST) { |
| 423 |
list_dfu_interfaces(); |
| 424 |
disconnect_devices(); |
| 425 |
libusb_exit(ctx); |
| 426 |
return EX_OK; |
| 427 |
} |
| 428 |
|
| 429 |
if (dfu_root == NULL) { |
| 430 |
if (wait_device) { |
| 431 |
milli_sleep(20); |
| 432 |
goto probe; |
| 433 |
} else { |
| 434 |
warnx("No DFU capable USB device available"); |
| 435 |
libusb_exit(ctx); |
| 436 |
return EX_IOERR; |
| 437 |
} |
| 438 |
} else if (file.bcdDFU == 0x11a && dfuse_multiple_alt(dfu_root)) { |
| 439 |
printf("Multiple alternate interfaces for DfuSe file\n"); |
| 440 |
} else if (dfu_root->next != NULL) { |
| 441 |
/* We cannot safely support more than one DFU capable device |
| 442 |
* with same vendor/product ID, since during DFU we need to do |
| 443 |
* a USB bus reset, after which the target device will get a |
| 444 |
* new address */ |
| 445 |
errx(EX_IOERR, "More than one DFU capable USB device found! " |
| 446 |
"Try `--list' and specify the serial number " |
| 447 |
"or disconnect all but one device\n"); |
| 448 |
} |
| 449 |
|
| 450 |
/* We have exactly one device. Its libusb_device is now in dfu_root->dev */ |
| 451 |
|
| 452 |
printf("Opening DFU capable USB device...\n"); |
| 453 |
ret = libusb_open(dfu_root->dev, &dfu_root->dev_handle); |
| 454 |
if (ret || !dfu_root->dev_handle) |
| 455 |
errx(EX_IOERR, "Cannot open device: %s", libusb_error_name(ret)); |
| 456 |
|
| 457 |
printf("Device ID %04x:%04x\n", dfu_root->vendor, dfu_root->product); |
| 458 |
|
| 459 |
/* If first interface is DFU it is likely not proper run-time */ |
| 460 |
if (dfu_root->interface > 0) |
| 461 |
printf("Run-Time device"); |
| 462 |
else |
| 463 |
printf("Device"); |
| 464 |
printf(" DFU version %04x\n", |
| 465 |
libusb_le16_to_cpu(dfu_root->func_dfu.bcdDFUVersion)); |
| 466 |
|
| 467 |
if (verbose) { |
| 468 |
printf("DFU attributes: (0x%02x)", dfu_root->func_dfu.bmAttributes); |
| 469 |
if (dfu_root->func_dfu.bmAttributes & USB_DFU_CAN_DOWNLOAD) |
| 470 |
printf(" bitCanDnload"); |
| 471 |
if (dfu_root->func_dfu.bmAttributes & USB_DFU_CAN_UPLOAD) |
| 472 |
printf(" bitCanUpload"); |
| 473 |
if (dfu_root->func_dfu.bmAttributes & USB_DFU_MANIFEST_TOL) |
| 474 |
printf(" bitManifestationTolerant"); |
| 475 |
if (dfu_root->func_dfu.bmAttributes & USB_DFU_WILL_DETACH) |
| 476 |
printf(" bitWillDetach"); |
| 477 |
printf("\n"); |
| 478 |
printf("Detach timeout %d ms\n", libusb_le16_to_cpu(dfu_root->func_dfu.wDetachTimeOut)); |
| 479 |
} |
| 480 |
|
| 481 |
/* Transition from run-Time mode to DFU mode */ |
| 482 |
if (!(dfu_root->flags & DFU_IFF_DFU)) { |
| 483 |
int err; |
| 484 |
/* In the 'first round' during runtime mode, there can only be one |
| 485 |
* DFU Interface descriptor according to the DFU Spec. */ |
| 486 |
|
| 487 |
/* FIXME: check if the selected device really has only one */ |
| 488 |
|
| 489 |
runtime_vendor = dfu_root->vendor; |
| 490 |
runtime_product = dfu_root->product; |
| 491 |
|
| 492 |
printf("Claiming USB DFU (Run-Time) Interface...\n"); |
| 493 |
ret = libusb_claim_interface(dfu_root->dev_handle, dfu_root->interface); |
| 494 |
if (ret < 0) { |
| 495 |
errx(EX_IOERR, "Cannot claim interface %d: %s", |
| 496 |
dfu_root->interface, libusb_error_name(ret)); |
| 497 |
} |
| 498 |
|
| 499 |
/* Needed for some devices where the DFU interface is not the first, |
| 500 |
* and should also be safe if there are multiple alt settings. |
| 501 |
* Otherwise skip the request since it might not be supported |
| 502 |
* by the device and the USB stack may or may not recover */ |
| 503 |
if (dfu_root->interface > 0 || dfu_root->flags & DFU_IFF_ALT) { |
| 504 |
printf("Setting Alternate Interface zero...\n"); |
| 505 |
ret = libusb_set_interface_alt_setting(dfu_root->dev_handle, dfu_root->interface, 0); |
| 506 |
if (ret < 0) { |
| 507 |
errx(EX_IOERR, "Cannot set alternate interface zero: %s", libusb_error_name(ret)); |
| 508 |
} |
| 509 |
} |
| 510 |
|
| 511 |
printf("Determining device status...\n"); |
| 512 |
err = dfu_get_status(dfu_root, &status); |
| 513 |
if (err == LIBUSB_ERROR_PIPE) { |
| 514 |
printf("Device does not implement get_status, assuming appIDLE\n"); |
| 515 |
status.bStatus = DFU_STATUS_OK; |
| 516 |
status.bwPollTimeout = 0; |
| 517 |
status.bState = DFU_STATE_appIDLE; |
| 518 |
status.iString = 0; |
| 519 |
} else if (err < 0) { |
| 520 |
errx(EX_IOERR, "error get_status: %s", libusb_error_name(err)); |
| 521 |
} else { |
| 522 |
printf("DFU state(%u) = %s, status(%u) = %s\n", status.bState, |
| 523 |
dfu_state_to_string(status.bState), status.bStatus, |
| 524 |
dfu_status_to_string(status.bStatus)); |
| 525 |
} |
| 526 |
milli_sleep(status.bwPollTimeout); |
| 527 |
|
| 528 |
switch (status.bState) { |
| 529 |
case DFU_STATE_appIDLE: |
| 530 |
case DFU_STATE_appDETACH: |
| 531 |
printf("Device really in Run-Time Mode, send DFU " |
| 532 |
"detach request...\n"); |
| 533 |
if (dfu_detach(dfu_root->dev_handle, |
| 534 |
dfu_root->interface, 1000) < 0) { |
| 535 |
warnx("error detaching"); |
| 536 |
} |
| 537 |
if (dfu_root->func_dfu.bmAttributes & USB_DFU_WILL_DETACH) { |
| 538 |
printf("Device will detach and reattach...\n"); |
| 539 |
} else { |
| 540 |
printf("Resetting USB...\n"); |
| 541 |
ret = libusb_reset_device(dfu_root->dev_handle); |
| 542 |
if (ret < 0 && ret != LIBUSB_ERROR_NOT_FOUND) |
| 543 |
errx(EX_IOERR, "error resetting " |
| 544 |
"after detach: %s", libusb_error_name(ret)); |
| 545 |
} |
| 546 |
break; |
| 547 |
case DFU_STATE_dfuERROR: |
| 548 |
printf("dfuERROR, clearing status\n"); |
| 549 |
if (dfu_clear_status(dfu_root->dev_handle, |
| 550 |
dfu_root->interface) < 0) { |
| 551 |
errx(EX_IOERR, "error clear_status"); |
| 552 |
} |
| 553 |
/* fall through */ |
| 554 |
default: |
| 555 |
warnx("WARNING: Device already in DFU mode? (bState=%d %s)", |
| 556 |
status.bState, dfu_state_to_string(status.bState)); |
| 557 |
libusb_release_interface(dfu_root->dev_handle, |
| 558 |
dfu_root->interface); |
| 559 |
goto dfustate; |
| 560 |
} |
| 561 |
libusb_release_interface(dfu_root->dev_handle, |
| 562 |
dfu_root->interface); |
| 563 |
libusb_close(dfu_root->dev_handle); |
| 564 |
dfu_root->dev_handle = NULL; |
| 565 |
|
| 566 |
/* keeping handles open might prevent re-enumeration */ |
| 567 |
disconnect_devices(); |
| 568 |
|
| 569 |
if (mode == MODE_DETACH) { |
| 570 |
libusb_exit(ctx); |
| 571 |
return EX_OK; |
| 572 |
} |
| 573 |
|
| 574 |
milli_sleep(detach_delay * 1000); |
| 575 |
|
| 576 |
/* Change match vendor and product to impossible values to force |
| 577 |
* only DFU mode matches in the following probe */ |
| 578 |
match_vendor = match_product = 0x10000; |
| 579 |
|
| 580 |
probe_devices(ctx); |
| 581 |
|
| 582 |
if (dfu_root == NULL) { |
| 583 |
errx(EX_IOERR, "Lost device after RESET?"); |
| 584 |
} else if (dfu_root->next != NULL) { |
| 585 |
errx(EX_IOERR, "More than one DFU capable USB device found! " |
| 586 |
"Try `--list' and specify the serial number " |
| 587 |
"or disconnect all but one device"); |
| 588 |
} |
| 589 |
|
| 590 |
/* Check for DFU mode device */ |
| 591 |
if (!(dfu_root->flags | DFU_IFF_DFU)) |
| 592 |
errx(EX_PROTOCOL, "Device is not in DFU mode"); |
| 593 |
|
| 594 |
printf("Opening DFU USB Device...\n"); |
| 595 |
ret = libusb_open(dfu_root->dev, &dfu_root->dev_handle); |
| 596 |
if (ret || !dfu_root->dev_handle) { |
| 597 |
errx(EX_IOERR, "Cannot open device"); |
| 598 |
} |
| 599 |
} else { |
| 600 |
/* we're already in DFU mode, so we can skip the detach/reset |
| 601 |
* procedure */ |
| 602 |
/* If a match vendor/product was specified, use that as the runtime |
| 603 |
* vendor/product, otherwise use the DFU mode vendor/product */ |
| 604 |
runtime_vendor = match_vendor < 0 ? dfu_root->vendor : match_vendor; |
| 605 |
runtime_product = match_product < 0 ? dfu_root->product : match_product; |
| 606 |
} |
| 607 |
|
| 608 |
dfustate: |
| 609 |
#if 0 |
| 610 |
printf("Setting Configuration %u...\n", dfu_root->configuration); |
| 611 |
ret = libusb_set_configuration(dfu_root->dev_handle, dfu_root->configuration); |
| 612 |
if (ret < 0) { |
| 613 |
errx(EX_IOERR, "Cannot set configuration: %s", libusb_error_name(ret)); |
| 614 |
} |
| 615 |
#endif |
| 616 |
printf("Claiming USB DFU Interface...\n"); |
| 617 |
ret = libusb_claim_interface(dfu_root->dev_handle, dfu_root->interface); |
| 618 |
if (ret < 0) { |
| 619 |
errx(EX_IOERR, "Cannot claim interface - %s", libusb_error_name(ret)); |
| 620 |
} |
| 621 |
|
| 622 |
if (dfu_root->flags & DFU_IFF_ALT) { |
| 623 |
printf("Setting Alternate Interface #%d ...\n", dfu_root->altsetting); |
| 624 |
ret = libusb_set_interface_alt_setting(dfu_root->dev_handle, dfu_root->interface, dfu_root->altsetting); |
| 625 |
if (ret < 0) { |
| 626 |
errx(EX_IOERR, "Cannot set alternate interface: %s", libusb_error_name(ret)); |
| 627 |
} |
| 628 |
} |
| 629 |
|
| 630 |
status_again: |
| 631 |
printf("Determining device status...\n"); |
| 632 |
ret = dfu_get_status(dfu_root, &status ); |
| 633 |
if (ret < 0) { |
| 634 |
errx(EX_IOERR, "error get_status: %s", libusb_error_name(ret)); |
| 635 |
} |
| 636 |
printf("DFU state(%u) = %s, status(%u) = %s\n", status.bState, |
| 637 |
dfu_state_to_string(status.bState), status.bStatus, |
| 638 |
dfu_status_to_string(status.bStatus)); |
| 639 |
|
| 640 |
milli_sleep(status.bwPollTimeout); |
| 641 |
|
| 642 |
switch (status.bState) { |
| 643 |
case DFU_STATE_appIDLE: |
| 644 |
case DFU_STATE_appDETACH: |
| 645 |
errx(EX_PROTOCOL, "Device still in Run-Time Mode!"); |
| 646 |
break; |
| 647 |
case DFU_STATE_dfuERROR: |
| 648 |
printf("Clearing status\n"); |
| 649 |
if (dfu_clear_status(dfu_root->dev_handle, dfu_root->interface) < 0) { |
| 650 |
errx(EX_IOERR, "error clear_status"); |
| 651 |
} |
| 652 |
goto status_again; |
| 653 |
break; |
| 654 |
case DFU_STATE_dfuDNLOAD_IDLE: |
| 655 |
case DFU_STATE_dfuUPLOAD_IDLE: |
| 656 |
printf("Aborting previous incomplete transfer\n"); |
| 657 |
if (dfu_abort(dfu_root->dev_handle, dfu_root->interface) < 0) { |
| 658 |
errx(EX_IOERR, "can't send DFU_ABORT"); |
| 659 |
} |
| 660 |
goto status_again; |
| 661 |
break; |
| 662 |
case DFU_STATE_dfuIDLE: |
| 663 |
default: |
| 664 |
break; |
| 665 |
} |
| 666 |
|
| 667 |
if (DFU_STATUS_OK != status.bStatus ) { |
| 668 |
printf("WARNING: DFU Status: '%s'\n", |
| 669 |
dfu_status_to_string(status.bStatus)); |
| 670 |
/* Clear our status & try again. */ |
| 671 |
if (dfu_clear_status(dfu_root->dev_handle, dfu_root->interface) < 0) |
| 672 |
errx(EX_IOERR, "USB communication error"); |
| 673 |
if (dfu_get_status(dfu_root, &status) < 0) |
| 674 |
errx(EX_IOERR, "USB communication error"); |
| 675 |
if (DFU_STATUS_OK != status.bStatus) |
| 676 |
errx(EX_PROTOCOL, "Status is not OK: %d", status.bStatus); |
| 677 |
|
| 678 |
milli_sleep(status.bwPollTimeout); |
| 679 |
} |
| 680 |
|
| 681 |
printf("DFU mode device DFU version %04x\n", |
| 682 |
libusb_le16_to_cpu(dfu_root->func_dfu.bcdDFUVersion)); |
| 683 |
|
| 684 |
if (dfu_root->func_dfu.bcdDFUVersion == libusb_cpu_to_le16(0x11a)) |
| 685 |
dfuse_device = 1; |
| 686 |
else if (dfuse_options) |
| 687 |
printf("Warning: DfuSe option used on non-DfuSe device\n"); |
| 688 |
|
| 689 |
/* Get from device or user, warn if overridden */ |
| 690 |
int func_dfu_transfer_size = libusb_le16_to_cpu(dfu_root->func_dfu.wTransferSize); |
| 691 |
if (func_dfu_transfer_size) { |
| 692 |
printf("Device returned transfer size %i\n", func_dfu_transfer_size); |
| 693 |
if (!transfer_size) |
| 694 |
transfer_size = func_dfu_transfer_size; |
| 695 |
else |
| 696 |
printf("Warning: Overriding device-reported transfer size\n"); |
| 697 |
} else { |
| 698 |
if (!transfer_size) |
| 699 |
errx(EX_USAGE, "Transfer size must be specified"); |
| 700 |
} |
| 701 |
|
| 702 |
#ifdef __linux__ |
| 703 |
/* limited to 4k in libusb Linux backend */ |
| 704 |
if ((int)transfer_size > 4096) { |
| 705 |
transfer_size = 4096; |
| 706 |
printf("Limited transfer size to %i\n", transfer_size); |
| 707 |
} |
| 708 |
#endif /* __linux__ */ |
| 709 |
|
| 710 |
if (transfer_size < dfu_root->bMaxPacketSize0) { |
| 711 |
transfer_size = dfu_root->bMaxPacketSize0; |
| 712 |
printf("Adjusted transfer size to %i\n", transfer_size); |
| 713 |
} |
| 714 |
|
| 715 |
switch (mode) { |
| 716 |
case MODE_UPLOAD: |
| 717 |
/* open for "exclusive" writing */ |
| 718 |
fd = open(file.name, O_WRONLY | O_BINARY | O_CREAT | O_EXCL | O_TRUNC, 0666); |
| 719 |
if (fd < 0) { |
| 720 |
warn("Cannot open file %s for writing", file.name); |
| 721 |
ret = EX_CANTCREAT; |
| 722 |
break; |
| 723 |
} |
| 724 |
|
| 725 |
if (dfuse_device || dfuse_options) { |
| 726 |
ret = dfuse_do_upload(dfu_root, transfer_size, fd, dfuse_options); |
| 727 |
} else { |
| 728 |
ret = dfuload_do_upload(dfu_root, transfer_size, expected_size, fd); |
| 729 |
} |
| 730 |
close(fd); |
| 731 |
if (ret < 0) |
| 732 |
ret = EX_IOERR; |
| 733 |
else |
| 734 |
ret = EX_OK; |
| 735 |
break; |
| 736 |
|
| 737 |
case MODE_DOWNLOAD: |
| 738 |
if (((file.idVendor != 0xffff && file.idVendor != runtime_vendor) || |
| 739 |
(file.idProduct != 0xffff && file.idProduct != runtime_product)) && |
| 740 |
((file.idVendor != 0xffff && file.idVendor != dfu_root->vendor) || |
| 741 |
(file.idProduct != 0xffff && file.idProduct != dfu_root->product))) { |
| 742 |
errx(EX_USAGE, "Error: File ID %04x:%04x does " |
| 743 |
"not match device (%04x:%04x or %04x:%04x)", |
| 744 |
file.idVendor, file.idProduct, |
| 745 |
runtime_vendor, runtime_product, |
| 746 |
dfu_root->vendor, dfu_root->product); |
| 747 |
} |
| 748 |
if (dfuse_device || dfuse_options || file.bcdDFU == 0x11a) { |
| 749 |
ret = dfuse_do_dnload(dfu_root, transfer_size, &file, dfuse_options); |
| 750 |
} else { |
| 751 |
ret = dfuload_do_dnload(dfu_root, transfer_size, &file); |
| 752 |
} |
| 753 |
if (ret < 0) |
| 754 |
ret = EX_IOERR; |
| 755 |
else |
| 756 |
ret = EX_OK; |
| 757 |
break; |
| 758 |
case MODE_DETACH: |
| 759 |
ret = dfu_detach(dfu_root->dev_handle, dfu_root->interface, 1000); |
| 760 |
if (ret < 0) { |
| 761 |
warnx("can't detach"); |
| 762 |
/* allow combination with final_reset */ |
| 763 |
ret = 0; |
| 764 |
} |
| 765 |
break; |
| 766 |
default: |
| 767 |
warnx("Unsupported mode: %u", mode); |
| 768 |
ret = EX_SOFTWARE; |
| 769 |
break; |
| 770 |
} |
| 771 |
|
| 772 |
if (!ret && final_reset) { |
| 773 |
ret = dfu_detach(dfu_root->dev_handle, dfu_root->interface, 1000); |
| 774 |
if (ret < 0) { |
| 775 |
/* Even if detach failed, just carry on to leave the |
| 776 |
device in a known state */ |
| 777 |
warnx("can't detach"); |
| 778 |
} |
| 779 |
printf("Resetting USB to switch back to Run-Time mode\n"); |
| 780 |
ret = libusb_reset_device(dfu_root->dev_handle); |
| 781 |
if (ret < 0 && ret != LIBUSB_ERROR_NOT_FOUND) { |
| 782 |
warnx("error resetting after download: %s", libusb_error_name(ret)); |
| 783 |
ret = EX_IOERR; |
| 784 |
} |
| 785 |
} |
| 786 |
|
| 787 |
libusb_close(dfu_root->dev_handle); |
| 788 |
dfu_root->dev_handle = NULL; |
| 789 |
|
| 790 |
disconnect_devices(); |
| 791 |
libusb_exit(ctx); |
| 792 |
return ret; |
| 793 |
} |