1 /*
2 *
3 * BlueZ - Bluetooth protocol stack for Linux
4 *
5 * Copyright (C) 2003-2008 Marcel Holtmann <marcel@holtmann.org>
6 *
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21 *
22 */
23
24 #ifdef HAVE_CONFIG_H
25 #include <config.h>
26 #endif
27
28 #include <stdio.h>
29 #include <errno.h>
30 #include <ctype.h>
31 #include <fcntl.h>
32 #include <unistd.h>
33 #include <stdint.h>
34 #include <stdlib.h>
35 #include <getopt.h>
36 #include <string.h>
37 #include <libgen.h>
38 #include <endian.h>
39 #include <byteswap.h>
40 #include <sys/types.h>
41 #include <sys/stat.h>
42
43 #include <usb.h>
44
45 #include "dfu.h"
46
47 #if __BYTE_ORDER == __LITTLE_ENDIAN
48 #define cpu_to_le16(d) (d)
49 #define cpu_to_le32(d) (d)
50 #define le16_to_cpu(d) (d)
51 #define le32_to_cpu(d) (d)
52 #elif __BYTE_ORDER == __BIG_ENDIAN
53 #define cpu_to_le16(d) bswap_16(d)
54 #define cpu_to_le32(d) bswap_32(d)
55 #define le16_to_cpu(d) bswap_16(d)
56 #define le32_to_cpu(d) bswap_32(d)
57 #else
58 #error "Unknown byte order"
59 #endif
60
61 #ifdef NEED_USB_GET_BUSSES
usb_get_busses(void)62 static inline struct usb_bus *usb_get_busses(void)
63 {
64 return usb_busses;
65 }
66 #endif
67
68 #ifndef USB_CLASS_WIRELESS
69 #define USB_CLASS_WIRELESS 0xe0
70 #endif
71
72 #ifndef USB_CLASS_APPLICATION
73 #define USB_CLASS_APPLICATION 0xfe
74 #endif
75
get_interface_number(struct usb_device * dev)76 static int get_interface_number(struct usb_device *dev)
77 {
78 int c, i, a;
79
80 for (c = 0; c < dev->descriptor.bNumConfigurations; c++) {
81 struct usb_config_descriptor *config = &dev->config[c];
82
83 for (i = 0; i < config->bNumInterfaces; i++) {
84 struct usb_interface *interface = &config->interface[i];
85
86 for (a = 0; a < interface->num_altsetting; a++) {
87 struct usb_interface_descriptor *desc = &interface->altsetting[a];
88
89 if (desc->bInterfaceClass != USB_CLASS_APPLICATION)
90 continue;
91 if (desc->bInterfaceSubClass != 0x01)
92 continue;
93 if (desc->bInterfaceProtocol != 0x00)
94 continue;
95
96 return desc->bInterfaceNumber;
97 }
98 }
99 }
100
101 return -1;
102 }
103
print_device(struct usb_device * dev)104 static void print_device(struct usb_device *dev)
105 {
106 printf("Bus %s Device %s: ID %04x:%04x Interface %d%s\n",
107 dev->bus->dirname, dev->filename,
108 dev->descriptor.idVendor, dev->descriptor.idProduct,
109 get_interface_number(dev),
110 dev->descriptor.bDeviceClass == USB_CLASS_APPLICATION ? " (DFU mode)" : "");
111 }
112
open_device(char * device,struct dfu_suffix * suffix)113 static struct usb_dev_handle *open_device(char *device, struct dfu_suffix *suffix)
114 {
115 struct usb_bus *bus;
116 struct usb_device *dev, *dfu_dev[10];
117 struct usb_dev_handle *udev;
118 struct dfu_status status;
119 char str[8];
120 int i, intf, sel, num = 0, try = 5, bus_id = -1, dev_id = -1;
121
122 printf("Scanning USB busses ... ");
123 fflush(stdout);
124
125 usb_find_busses();
126 usb_find_devices();
127
128 for (bus = usb_get_busses(); bus; bus = bus->next) {
129 if (bus_id > 0) {
130 snprintf(str, sizeof(str) - 1, "%03i", bus_id);
131 if (strcmp(str, bus->dirname))
132 continue;
133 }
134
135 for (dev = bus->devices; dev; dev = dev->next) {
136 if (bus_id > 0 && dev_id > 0) {
137 snprintf(str, sizeof(str) - 1, "%03i", dev_id);
138 if (strcmp(str, dev->filename))
139 continue;
140 }
141
142 if (dev->descriptor.bDeviceClass == USB_CLASS_HUB)
143 continue;
144
145 if (num > 9 || get_interface_number(dev) < 0)
146 continue;
147
148 dfu_dev[num++] = dev;
149 }
150 }
151
152 if (num < 1) {
153 printf("\rCan't find any DFU devices\n");
154 return NULL;
155 }
156
157 printf("\rAvailable devices with DFU support:\n\n");
158 for (i = 0; i < num; i++) {
159 printf("\t%2d) ", i + 1);
160 print_device(dfu_dev[i]);
161 }
162 printf("\n");
163
164 do {
165 printf("\rSelect device (abort with 0): ");
166 fflush(stdout);
167 memset(str, 0, sizeof(str));
168 if (!fgets(str, sizeof(str) - 1, stdin))
169 continue;
170 sel = atoi(str);
171 } while (!isdigit(str[0]) || sel < 0 || sel > num );
172
173 if (sel < 1)
174 return NULL;
175
176 sel--;
177 intf = get_interface_number(dfu_dev[sel]);
178 printf("\n");
179
180 udev = usb_open(dfu_dev[sel]);
181 if (!udev) {
182 printf("Can't open device: %s (%d)\n", strerror(errno), errno);
183 return NULL;
184 }
185
186 if (usb_claim_interface(udev, intf) < 0) {
187 printf("Can't claim interface: %s (%d)\n", strerror(errno), errno);
188 usb_close(udev);
189 return NULL;
190 }
191
192 if (dfu_get_status(udev, intf, &status) < 0) {
193 printf("Can't get status: %s (%d)\n", strerror(errno), errno);
194 goto error;
195 }
196
197 if (status.bState == DFU_STATE_ERROR) {
198 if (dfu_clear_status(udev, intf) < 0) {
199 printf("Can't clear status: %s (%d)\n", strerror(errno), errno);
200 goto error;
201 }
202 if (dfu_abort(udev, intf) < 0) {
203 printf("Can't abort previous action: %s (%d)\n", strerror(errno), errno);
204 goto error;
205 }
206 if (dfu_get_status(udev, intf, &status) < 0) {
207 printf("Can't get status: %s (%d)\n", strerror(errno), errno);
208 goto error;
209 }
210 }
211
212 if (status.bState == DFU_STATE_DFU_IDLE) {
213 if (suffix) {
214 suffix->idVendor = cpu_to_le16(0x0000);
215 suffix->idProduct = cpu_to_le16(0x0000);
216 suffix->bcdDevice = cpu_to_le16(0x0000);
217 }
218 return udev;
219 }
220
221 if (status.bState != DFU_STATE_APP_IDLE) {
222 printf("Device is not idle, can't detach it (state %d)\n", status.bState);
223 goto error;
224 }
225
226 printf("Switching device into DFU mode ... ");
227 fflush(stdout);
228
229 if (suffix) {
230 suffix->idVendor = cpu_to_le16(dfu_dev[sel]->descriptor.idVendor);
231 suffix->idProduct = cpu_to_le16(dfu_dev[sel]->descriptor.idProduct);
232 suffix->bcdDevice = cpu_to_le16(dfu_dev[sel]->descriptor.bcdDevice);
233 }
234
235 if (dfu_detach(udev, intf) < 0) {
236 printf("\rCan't detach device: %s (%d)\n", strerror(errno), errno);
237 goto error;
238 }
239
240 if (dfu_get_status(udev, intf, &status) < 0) {
241 printf("\rCan't get status: %s (%d)\n", strerror(errno), errno);
242 goto error;
243 }
244
245 if (status.bState != DFU_STATE_APP_DETACH) {
246 printf("\rDevice is not in detach mode, try again\n");
247 goto error;
248 }
249
250 usb_release_interface(udev, intf);
251 usb_reset(udev);
252 usb_close(udev);
253
254 bus = dfu_dev[sel]->bus;
255 num = 0;
256
257 while (num != 1 && try-- > 0) {
258 sleep(1);
259 usb_find_devices();
260
261 for (dev = bus->devices; dev; dev = dev->next) {
262 if (dev->descriptor.bDeviceClass != USB_CLASS_APPLICATION)
263 continue;
264
265 if (suffix && dev->descriptor.idVendor != le16_to_cpu(suffix->idVendor))
266 continue;
267
268 if (num > 9 || get_interface_number(dev) != 0)
269 continue;
270
271 dfu_dev[num++] = dev;
272 }
273 }
274
275 if (num != 1) {
276 printf("\rCan't identify device with DFU mode\n");
277 goto error;
278 }
279
280 printf("\r");
281
282 intf = 0;
283
284 udev = usb_open(dfu_dev[0]);
285 if (!udev) {
286 printf("Can't open device: %s (%d)\n", strerror(errno), errno);
287 return NULL;
288 }
289
290 if (usb_claim_interface(udev, intf) < 0) {
291 printf("Can't claim interface: %s (%d)\n", strerror(errno), errno);
292 usb_close(udev);
293 return NULL;
294 }
295
296 if (dfu_get_status(udev, intf, &status) < 0) {
297 printf("Can't get status: %s (%d)\n", strerror(errno), errno);
298 goto error;
299 }
300
301 if (status.bState != DFU_STATE_DFU_IDLE) {
302 printf("Device is not in DFU mode, can't use it\n");
303 goto error;
304 }
305
306 return udev;
307
308 error:
309 usb_release_interface(udev, intf);
310 usb_close(udev);
311 return NULL;
312 }
313
314 static void usage(void);
315
cmd_verify(char * device,int argc,char ** argv)316 static void cmd_verify(char *device, int argc, char **argv)
317 {
318 struct stat st;
319 struct dfu_suffix *suffix;
320 uint32_t crc;
321 uint16_t bcd;
322 char str[16];
323 unsigned char *buf;
324 unsigned long size;
325 char *filename;
326 int i, fd, len;
327
328 if (argc < 2) {
329 usage();
330 exit(1);
331 }
332
333 filename = argv[1];
334
335 if (stat(filename, &st) < 0) {
336 perror("Can't access firmware");
337 exit(1);
338 }
339
340 size = st.st_size;
341
342 if (!(buf = malloc(size))) {
343 perror("Unable to allocate file buffer");
344 exit(1);
345 }
346
347 if ((fd = open(filename, O_RDONLY)) < 0) {
348 perror("Can't open firmware");
349 free(buf);
350 exit(1);
351 }
352
353 if (read(fd, buf, size) < size) {
354 perror("Can't load firmware");
355 free(buf);
356 close(fd);
357 exit(1);
358 }
359
360 printf("Filename\t%s\n", basename(filename));
361 printf("Filesize\t%ld\n", size);
362
363 crc = crc32_init();
364 for (i = 0; i < size - 4; i++)
365 crc = crc32_byte(crc, buf[i]);
366 printf("Checksum\t%08x\n", crc);
367
368 printf("\n");
369 len = buf[size - 5];
370 printf("DFU suffix\t");
371 for (i = 0; i < len; i++) {
372 printf("%02x ", buf[size - len + i]);
373 }
374 printf("\n\n");
375
376 suffix = (struct dfu_suffix *) (buf + size - DFU_SUFFIX_SIZE);
377
378 printf("idVendor\t%04x\n", le16_to_cpu(suffix->idVendor));
379 printf("idProduct\t%04x\n", le16_to_cpu(suffix->idProduct));
380 printf("bcdDevice\t%x\n", le16_to_cpu(suffix->bcdDevice));
381
382 printf("\n");
383
384 bcd = le16_to_cpu(suffix->bcdDFU);
385
386 printf("bcdDFU\t\t%x.%x\n", bcd >> 8, bcd & 0xff);
387 printf("ucDfuSignature\t%c%c%c\n", suffix->ucDfuSignature[2],
388 suffix->ucDfuSignature[1], suffix->ucDfuSignature[0]);
389 printf("bLength\t\t%d\n", suffix->bLength);
390 printf("dwCRC\t\t%08x\n", le32_to_cpu(suffix->dwCRC));
391 printf("\n");
392
393 memset(str, 0, sizeof(str));
394 memcpy(str, buf, 8);
395
396 if (!strcmp(str, "CSR-dfu1") || !strcmp(str, "CSR-dfu2")) {
397 crc = crc32_init();
398 for (i = 0; i < size - DFU_SUFFIX_SIZE; i++)
399 crc = crc32_byte(crc, buf[i]);
400
401 printf("Firmware type\t%s\n", str);
402 printf("Firmware check\t%s checksum\n", crc == 0 ? "valid" : "corrupt");
403 printf("\n");
404 }
405
406 free(buf);
407
408 close(fd);
409 }
410
cmd_modify(char * device,int argc,char ** argv)411 static void cmd_modify(char *device, int argc, char **argv)
412 {
413 }
414
cmd_upgrade(char * device,int argc,char ** argv)415 static void cmd_upgrade(char *device, int argc, char **argv)
416 {
417 struct usb_dev_handle *udev;
418 struct dfu_status status;
419 struct dfu_suffix suffix;
420 struct stat st;
421 char *buf;
422 unsigned long filesize, count, timeout = 0;
423 char *filename;
424 uint32_t crc, dwCRC;
425 int fd, i, block, len, size, sent = 0, try = 10;
426
427 if (argc < 2) {
428 usage();
429 exit(1);
430 }
431
432 filename = argv[1];
433
434 if (stat(filename, &st) < 0) {
435 perror("Can't access firmware");
436 exit(1);
437 }
438
439 filesize = st.st_size;
440
441 if (!(buf = malloc(filesize))) {
442 perror("Unable to allocate file buffer");
443 exit(1);
444 }
445
446 if ((fd = open(filename, O_RDONLY)) < 0) {
447 perror("Can't open firmware");
448 free(buf);
449 exit(1);
450 }
451
452 if (read(fd, buf, filesize) < filesize) {
453 perror("Can't load firmware");
454 free(buf);
455 close(fd);
456 exit(1);
457 }
458
459 memcpy(&suffix, buf + filesize - DFU_SUFFIX_SIZE, sizeof(suffix));
460 dwCRC = le32_to_cpu(suffix.dwCRC);
461
462 printf("Filename\t%s\n", basename(filename));
463 printf("Filesize\t%ld\n", filesize);
464
465 crc = crc32_init();
466 for (i = 0; i < filesize - 4; i++)
467 crc = crc32_byte(crc, buf[i]);
468
469 printf("Checksum\t%08x (%s)\n", crc,
470 crc == dwCRC ? "valid" : "corrupt");
471
472 if (crc != dwCRC) {
473 free(buf);
474 close(fd);
475 exit(1);
476 }
477
478 printf("\n");
479
480 udev = open_device(device, &suffix);
481 if (!udev)
482 exit(1);
483
484 printf("\r" " " " " " " " " " ");
485 printf("\rFirmware download ... ");
486 fflush(stdout);
487
488 count = filesize - DFU_SUFFIX_SIZE;
489 block = 0;
490
491 while (count) {
492 size = (count > 1023) ? 1023 : count;
493
494 if (dfu_get_status(udev, 0, &status) < 0) {
495 if (try-- > 0) {
496 sleep(1);
497 continue;
498 }
499 printf("\rCan't get status: %s (%d)\n", strerror(errno), errno);
500 goto done;
501 }
502
503 if (status.bStatus != DFU_OK) {
504 if (try-- > 0) {
505 dfu_clear_status(udev, 0);
506 sleep(1);
507 continue;
508 }
509 printf("\rFirmware download ... aborting (status %d state %d)\n",
510 status.bStatus, status.bState);
511 goto done;
512 }
513
514 if (status.bState != DFU_STATE_DFU_IDLE &&
515 status.bState != DFU_STATE_DFU_DNLOAD_IDLE) {
516 sleep(1);
517 continue;
518 }
519
520 timeout = (status.bwPollTimeout[2] << 16) |
521 (status.bwPollTimeout[1] << 8) |
522 status.bwPollTimeout[0];
523
524 usleep(timeout * 1000);
525
526 len = dfu_download(udev, 0, block, buf + sent, size);
527 if (len < 0) {
528 if (try-- > 0) {
529 sleep(1);
530 continue;
531 }
532 printf("\rCan't upload next block: %s (%d)\n", strerror(errno), errno);
533 goto done;
534 }
535
536 printf("\rFirmware download ... %d bytes ", block * 1023 + len);
537 fflush(stdout);
538
539 sent += len;
540 count -= len;
541 block++;
542 }
543
544 printf("\r" " " " " " " " " " ");
545 printf("\rFinishing firmware download ... ");
546 fflush(stdout);
547
548 sleep(1);
549
550 if (dfu_get_status(udev, 0, &status) < 0) {
551 printf("\rCan't get status: %s (%d)\n", strerror(errno), errno);
552 goto done;
553 }
554
555 timeout = (status.bwPollTimeout[2] << 16) |
556 (status.bwPollTimeout[1] << 8) |
557 status.bwPollTimeout[0];
558
559 usleep(timeout * 1000);
560
561 if (count == 0) {
562 len = dfu_download(udev, 0, block, NULL, 0);
563 if (len < 0) {
564 printf("\rCan't send final block: %s (%d)\n", strerror(errno), errno);
565 goto done;
566 }
567 }
568
569 printf("\r" " " " " " " " " " ");
570 printf("\rWaiting for device ... ");
571 fflush(stdout);
572
573 sleep(10);
574
575 printf("\n");
576
577 done:
578 free(buf);
579 close(fd);
580
581 usb_release_interface(udev, 0);
582 usb_reset(udev);
583 usb_close(udev);
584 }
585
cmd_archive(char * device,int argc,char ** argv)586 static void cmd_archive(char *device, int argc, char **argv)
587 {
588 struct usb_dev_handle *udev;
589 struct dfu_status status;
590 struct dfu_suffix suffix;
591 char buf[2048];
592 unsigned long timeout = 0;
593 char *filename;
594 uint32_t crc;
595 int fd, i, n, len, try = 8;
596
597 if (argc < 2) {
598 usage();
599 exit(1);
600 }
601
602 filename = argv[1];
603
604 udev = open_device(device, &suffix);
605 if (!udev)
606 exit(1);
607
608 fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
609 if (fd < 0) {
610 printf("Can't open firmware file: %s (%d)\n", strerror(errno), errno);
611 goto done;
612 }
613
614 printf("\r" " " " " " " " " " ");
615 printf("\rFirmware upload ... ");
616 fflush(stdout);
617
618 crc = crc32_init();
619 n = 0;
620 while (1) {
621 if (dfu_get_status(udev, 0, &status) < 0) {
622 if (try-- > 0) {
623 sleep(1);
624 continue;
625 }
626 printf("\rCan't get status: %s (%d)\n", strerror(errno), errno);
627 goto done;
628 }
629
630 if (status.bStatus != DFU_OK) {
631 if (try-- > 0) {
632 dfu_clear_status(udev, 0);
633 sleep(1);
634 continue;
635 }
636 printf("\rFirmware upload ... aborting (status %d state %d)\n",
637 status.bStatus, status.bState);
638 goto done;
639 }
640
641 if (status.bState != DFU_STATE_DFU_IDLE &&
642 status.bState != DFU_STATE_UPLOAD_IDLE) {
643 sleep(1);
644 continue;
645 }
646
647 timeout = (status.bwPollTimeout[2] << 16) |
648 (status.bwPollTimeout[1] << 8) |
649 status.bwPollTimeout[0];
650
651 usleep(timeout * 1000);
652
653 len = dfu_upload(udev, 0, n, buf, 1023);
654 if (len < 0) {
655 if (try-- > 0) {
656 sleep(1);
657 continue;
658 }
659 printf("\rCan't upload next block: %s (%d)\n", strerror(errno), errno);
660 goto done;
661 }
662
663 printf("\rFirmware upload ... %d bytes ", n * 1023 + len);
664 fflush(stdout);
665
666 for (i = 0; i < len; i++)
667 crc = crc32_byte(crc, buf[i]);
668
669 if (len > 0) {
670 if (write(fd, buf, len) < 0) {
671 printf("\rCan't write next block: %s (%d)\n", strerror(errno), errno);
672 goto done;
673 }
674 }
675
676 n++;
677 if (len != 1023)
678 break;
679 }
680 printf("\n");
681
682 suffix.bcdDFU = cpu_to_le16(0x0100);
683 suffix.ucDfuSignature[0] = 'U';
684 suffix.ucDfuSignature[1] = 'F';
685 suffix.ucDfuSignature[2] = 'D';
686 suffix.bLength = DFU_SUFFIX_SIZE;
687
688 memcpy(buf, &suffix, DFU_SUFFIX_SIZE);
689 for (i = 0; i < DFU_SUFFIX_SIZE - 4; i++)
690 crc = crc32_byte(crc, buf[i]);
691
692 suffix.dwCRC = cpu_to_le32(crc);
693
694 if (write(fd, &suffix, DFU_SUFFIX_SIZE) < 0)
695 printf("Can't write suffix block: %s (%d)\n", strerror(errno), errno);
696
697 done:
698 close(fd);
699
700 usb_release_interface(udev, 0);
701 usb_reset(udev);
702 usb_close(udev);
703 }
704
705 struct {
706 char *cmd;
707 char *alt;
708 void (*func)(char *device, int argc, char **argv);
709 char *opt;
710 char *doc;
711 } command[] = {
712 { "verify", "check", cmd_verify, "<dfu-file>", "Check firmware file" },
713 { "modify", "change", cmd_modify, "<dfu-file>", "Change firmware attributes" },
714 { "upgrade", "download", cmd_upgrade, "<dfu-file>", "Download a new firmware" },
715 { "archive", "upload", cmd_archive, "<dfu-file>", "Upload the current firmware" },
716 { NULL, NULL, NULL, 0, 0 }
717 };
718
usage(void)719 static void usage(void)
720 {
721 int i;
722
723 printf("dfutool - Device Firmware Upgrade utility ver %s\n\n", VERSION);
724
725 printf("Usage:\n"
726 "\tdfutool [options] <command>\n"
727 "\n");
728
729 printf("Options:\n"
730 "\t-d, --device <device> USB device\n"
731 "\t-h, --help Display help\n"
732 "\n");
733
734 printf("Commands:\n");
735 for (i = 0; command[i].cmd; i++)
736 printf("\t%-8s %-10s\t%s\n", command[i].cmd,
737 command[i].opt ? command[i].opt : " ",
738 command[i].doc);
739 printf("\n");
740 }
741
742 static struct option main_options[] = {
743 { "help", 0, 0, 'h' },
744 { "device", 1, 0, 'd' },
745 { 0, 0, 0, 0 }
746 };
747
main(int argc,char * argv[])748 int main(int argc, char *argv[])
749 {
750 char *device = NULL;
751 int i, opt;
752
753 while ((opt = getopt_long(argc, argv, "+d:h", main_options, NULL)) != -1) {
754 switch(opt) {
755 case 'd':
756 device = strdup(optarg);
757 break;
758
759 case 'h':
760 usage();
761 exit(0);
762
763 default:
764 exit(0);
765 }
766 }
767
768 argc -= optind;
769 argv += optind;
770 optind = 0;
771
772 if (argc < 1) {
773 usage();
774 exit(1);
775 }
776
777 usb_init();
778
779 for (i = 0; command[i].cmd; i++) {
780 if (strcmp(command[i].cmd, argv[0]) && strcmp(command[i].alt, argv[0]))
781 continue;
782 command[i].func(device, argc, argv);
783 exit(0);
784 }
785
786 usage();
787 exit(1);
788 }
789