From 1514cc3fbace6793f002737b08f9cfcb140bed70 Mon Sep 17 00:00:00 2001 From: Marcel Holtmann Date: Thu, 21 Apr 2005 21:53:11 +0000 Subject: Add the firmware upgrade utility --- acinclude.m4 | 11 +- tools/Makefile.am | 23 +- tools/dfu.c | 169 ++++++++++++ tools/dfu.h | 108 ++++++++ tools/dfutool.1 | 53 ++++ tools/dfutool.c | 763 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 1119 insertions(+), 8 deletions(-) create mode 100644 tools/dfu.c create mode 100644 tools/dfu.h create mode 100644 tools/dfutool.1 create mode 100644 tools/dfutool.c diff --git a/acinclude.m4 b/acinclude.m4 index 9175cf45..15ee2ed8 100644 --- a/acinclude.m4 +++ b/acinclude.m4 @@ -76,7 +76,7 @@ AC_DEFUN([AC_PATH_BLUEZ], [ test -d "${bluez_prefix}/include" && BLUEZ_CFLAGS="$BLUEZ_CFLAGS -I${bluez_prefix}/include" CPPFLAGS="$CPPFLAGS $BLUEZ_CFLAGS" - AC_CHECK_HEADER(bluetooth/bluetooth.h,, AC_MSG_ERROR(Bluetooth header files not found)) + AC_CHECK_HEADER(bluetooth/bluetooth.h, dummy=yes, AC_MSG_ERROR(Bluetooth header files not found)) BLUEZ_LIBS="" if (test "${prefix}" = "${bluez_prefix}"); then @@ -88,7 +88,7 @@ AC_DEFUN([AC_PATH_BLUEZ], [ LDFLAGS="$LDFLAGS $BLUEZ_LIBS" AC_CHECK_LIB(bluetooth, hci_open_dev, BLUEZ_LIBS="$BLUEZ_LIBS -lbluetooth", AC_MSG_ERROR(Bluetooth library not found)) - AC_CHECK_LIB(bluetooth, sdp_connect,, AC_CHECK_LIB(sdp, sdp_connect, BLUEZ_LIBS="$BLUEZ_LIBS -lsdp")) + AC_CHECK_LIB(bluetooth, sdp_connect, dummy=yes, AC_CHECK_LIB(sdp, sdp_connect, BLUEZ_LIBS="$BLUEZ_LIBS -lsdp")) CPPFLAGS=$ac_save_CPPFLAGS LDFLAGS=$ac_save_LDFLAGS @@ -262,6 +262,7 @@ AC_DEFUN([AC_ARG_BLUEZ], [ initscripts_enable=no bluepin_enable=yes hid2hci_enable=${usb_found} + dfutool_enable=no bcm203x_enable=no AC_ARG_ENABLE(debug, AC_HELP_STRING([--enable-debug], [enable compiling with debugging information]), [ @@ -282,6 +283,7 @@ AC_DEFUN([AC_ARG_BLUEZ], [ initscripts_enable=${enableval} bluepin_enable=${enableval} hid2hci_enable=${enableval} + dfutool_enable=${enableval} bcm203x_enable=${enableval} ]) @@ -321,6 +323,10 @@ AC_DEFUN([AC_ARG_BLUEZ], [ hid2hci_enable=${enableval} ]) + AC_ARG_ENABLE(dfutool, AC_HELP_STRING([--enable-dfutool], [install DFU firmware upgrade utility]), [ + dfutool_enable=${enableval} + ]) + AC_ARG_ENABLE(bcm203x, AC_HELP_STRING([--enable-bcm203x], [install Broadcom 203x firmware loader]), [ bcm203x_enable=${enableval} ]) @@ -343,5 +349,6 @@ AC_DEFUN([AC_ARG_BLUEZ], [ AM_CONDITIONAL(INITSCRIPTS, test "${initscripts_enable}" = "yes") AM_CONDITIONAL(BLUEPIN, test "${bluepin_enable}" = "yes") AM_CONDITIONAL(HID2HCI, test "${hid2hci_enable}" = "yes" && test "${usb_found}" = "yes") + AM_CONDITIONAL(DFUTOOL, test "${dfutool_enable}" = "yes" && test "${usb_found}" = "yes") AM_CONDITIONAL(BCM203X, test "${bcm203x_enable}" = "yes" && test "${usb_found}" = "yes") ]) diff --git a/tools/Makefile.am b/tools/Makefile.am index 3dd69b68..9756a8d8 100644 --- a/tools/Makefile.am +++ b/tools/Makefile.am @@ -10,18 +10,24 @@ hid2hci_programs = hid2hci_manfiles = endif +if DFUTOOL +dfutool_programs = dfutool +dfutool_manfiles = dfutool.1 +else +dfutool_programs = +dfutool_manfiles = +endif + sbin_PROGRAMS = hciattach hciconfig $(hid2hci_programs) -bin_PROGRAMS = hcitool l2ping sdptool ciptool +bin_PROGRAMS = hcitool l2ping sdptool ciptool $(dfutool_programs) noinst_PROGRAMS = hcisecfilter ppporc pskey hciconfig_SOURCES = hciconfig.c csr.h csr.c - hciconfig_LDADD = @BLUEZ_LIBS@ hcitool_SOURCES = hcitool.c oui.h oui.c - hcitool_LDADD = @BLUEZ_LIBS@ l2ping_LDADD = @BLUEZ_LIBS@ @@ -33,17 +39,22 @@ ciptool_LDADD = @BLUEZ_LIBS@ ppporc_LDADD = @BLUEZ_LIBS@ pskey_SOURCES = pskey.c csr.h csr.c - pskey_LDADD = @BLUEZ_LIBS@ if HID2HCI hid2hci_LDADD = @USB_LIBS@ endif +if DFUTOOL +dfutool_SOURCES = dfutool.c dfu.h dfu.c +dfutool_LDADD = @USB_LIBS@ +endif + AM_CFLAGS = @BLUEZ_CFLAGS@ @USB_CFLAGS@ -man_MANS = hciattach.8 hciconfig.8 hcitool.1 l2ping.1 sdptool.1 ciptool.1 $(hid2hci_manfiles) +man_MANS = hciattach.8 hciconfig.8 hcitool.1 l2ping.1 sdptool.1 ciptool.1 \ + $(hid2hci_manfiles) $(dfutool_manfiles) -EXTRA_DIST = $(man_MANS) hid2hci.8 hid2hci.c +EXTRA_DIST = $(man_MANS) hid2hci.8 dfutool.1 MAINTAINERCLEANFILES = Makefile.in diff --git a/tools/dfu.c b/tools/dfu.c new file mode 100644 index 00000000..08e256b9 --- /dev/null +++ b/tools/dfu.c @@ -0,0 +1,169 @@ +/* + * + * BlueZ - Bluetooth protocol stack for Linux + * + * Copyright (C) 2003-2005 Marcel Holtmann + * + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 as + * published by the Free Software Foundation; + * + * Software distributed under the License is distributed on an "AS + * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or + * implied. See the License for the specific language governing + * rights and limitations under the License. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * + * $Id$ + */ + +#ifdef HAVE_CONFIG_H +#include +#endif + +#include +#include +#include + +#include + +#include "dfu.h" + +#ifndef USB_DIR_OUT +#define USB_DIR_OUT 0x00 +#endif + +#ifndef USB_DIR_IN +#define USB_DIR_IN 0x80 +#endif + +#ifndef USB_DT_DFU +#define USB_DT_DFU 0x21 +#endif + +#define DFU_PACKETSIZE 0x03ff /* CSR default value: 1023 */ +#define DFU_TIMEOUT 10000 + +static uint32_t dfu_crc32_table[] = { + 0x00000000, 0x77073096, 0xee0e612c, 0x990951ba, 0x076dc419, 0x706af48f, + 0xe963a535, 0x9e6495a3, 0x0edb8832, 0x79dcb8a4, 0xe0d5e91e, 0x97d2d988, + 0x09b64c2b, 0x7eb17cbd, 0xe7b82d07, 0x90bf1d91, 0x1db71064, 0x6ab020f2, + 0xf3b97148, 0x84be41de, 0x1adad47d, 0x6ddde4eb, 0xf4d4b551, 0x83d385c7, + 0x136c9856, 0x646ba8c0, 0xfd62f97a, 0x8a65c9ec, 0x14015c4f, 0x63066cd9, + 0xfa0f3d63, 0x8d080df5, 0x3b6e20c8, 0x4c69105e, 0xd56041e4, 0xa2677172, + 0x3c03e4d1, 0x4b04d447, 0xd20d85fd, 0xa50ab56b, 0x35b5a8fa, 0x42b2986c, + 0xdbbbc9d6, 0xacbcf940, 0x32d86ce3, 0x45df5c75, 0xdcd60dcf, 0xabd13d59, + 0x26d930ac, 0x51de003a, 0xc8d75180, 0xbfd06116, 0x21b4f4b5, 0x56b3c423, + 0xcfba9599, 0xb8bda50f, 0x2802b89e, 0x5f058808, 0xc60cd9b2, 0xb10be924, + 0x2f6f7c87, 0x58684c11, 0xc1611dab, 0xb6662d3d, 0x76dc4190, 0x01db7106, + 0x98d220bc, 0xefd5102a, 0x71b18589, 0x06b6b51f, 0x9fbfe4a5, 0xe8b8d433, + 0x7807c9a2, 0x0f00f934, 0x9609a88e, 0xe10e9818, 0x7f6a0dbb, 0x086d3d2d, + 0x91646c97, 0xe6635c01, 0x6b6b51f4, 0x1c6c6162, 0x856530d8, 0xf262004e, + 0x6c0695ed, 0x1b01a57b, 0x8208f4c1, 0xf50fc457, 0x65b0d9c6, 0x12b7e950, + 0x8bbeb8ea, 0xfcb9887c, 0x62dd1ddf, 0x15da2d49, 0x8cd37cf3, 0xfbd44c65, + 0x4db26158, 0x3ab551ce, 0xa3bc0074, 0xd4bb30e2, 0x4adfa541, 0x3dd895d7, + 0xa4d1c46d, 0xd3d6f4fb, 0x4369e96a, 0x346ed9fc, 0xad678846, 0xda60b8d0, + 0x44042d73, 0x33031de5, 0xaa0a4c5f, 0xdd0d7cc9, 0x5005713c, 0x270241aa, + 0xbe0b1010, 0xc90c2086, 0x5768b525, 0x206f85b3, 0xb966d409, 0xce61e49f, + 0x5edef90e, 0x29d9c998, 0xb0d09822, 0xc7d7a8b4, 0x59b33d17, 0x2eb40d81, + 0xb7bd5c3b, 0xc0ba6cad, 0xedb88320, 0x9abfb3b6, 0x03b6e20c, 0x74b1d29a, + 0xead54739, 0x9dd277af, 0x04db2615, 0x73dc1683, 0xe3630b12, 0x94643b84, + 0x0d6d6a3e, 0x7a6a5aa8, 0xe40ecf0b, 0x9309ff9d, 0x0a00ae27, 0x7d079eb1, + 0xf00f9344, 0x8708a3d2, 0x1e01f268, 0x6906c2fe, 0xf762575d, 0x806567cb, + 0x196c3671, 0x6e6b06e7, 0xfed41b76, 0x89d32be0, 0x10da7a5a, 0x67dd4acc, + 0xf9b9df6f, 0x8ebeeff9, 0x17b7be43, 0x60b08ed5, 0xd6d6a3e8, 0xa1d1937e, + 0x38d8c2c4, 0x4fdff252, 0xd1bb67f1, 0xa6bc5767, 0x3fb506dd, 0x48b2364b, + 0xd80d2bda, 0xaf0a1b4c, 0x36034af6, 0x41047a60, 0xdf60efc3, 0xa867df55, + 0x316e8eef, 0x4669be79, 0xcb61b38c, 0xbc66831a, 0x256fd2a0, 0x5268e236, + 0xcc0c7795, 0xbb0b4703, 0x220216b9, 0x5505262f, 0xc5ba3bbe, 0xb2bd0b28, + 0x2bb45a92, 0x5cb36a04, 0xc2d7ffa7, 0xb5d0cf31, 0x2cd99e8b, 0x5bdeae1d, + 0x9b64c2b0, 0xec63f226, 0x756aa39c, 0x026d930a, 0x9c0906a9, 0xeb0e363f, + 0x72076785, 0x05005713, 0x95bf4a82, 0xe2b87a14, 0x7bb12bae, 0x0cb61b38, + 0x92d28e9b, 0xe5d5be0d, 0x7cdcefb7, 0x0bdbdf21, 0x86d3d2d4, 0xf1d4e242, + 0x68ddb3f8, 0x1fda836e, 0x81be16cd, 0xf6b9265b, 0x6fb077e1, 0x18b74777, + 0x88085ae6, 0xff0f6a70, 0x66063bca, 0x11010b5c, 0x8f659eff, 0xf862ae69, + 0x616bffd3, 0x166ccf45, 0xa00ae278, 0xd70dd2ee, 0x4e048354, 0x3903b3c2, + 0xa7672661, 0xd06016f7, 0x4969474d, 0x3e6e77db, 0xaed16a4a, 0xd9d65adc, + 0x40df0b66, 0x37d83bf0, 0xa9bcae53, 0xdebb9ec5, 0x47b2cf7f, 0x30b5ffe9, + 0xbdbdf21c, 0xcabac28a, 0x53b39330, 0x24b4a3a6, 0xbad03605, 0xcdd70693, + 0x54de5729, 0x23d967bf, 0xb3667a2e, 0xc4614ab8, 0x5d681b02, 0x2a6f2b94, + 0xb40bbe37, 0xc30c8ea1, 0x5a05df1b, 0x2d02ef8d +}; + +uint32_t crc32_init(void) +{ + return 0xffffffff; +} + +uint32_t crc32_byte(uint32_t accum, uint8_t delta) +{ + return dfu_crc32_table[(accum ^ delta) & 0xff] ^ (accum >> 8); +} + +int dfu_detach(struct usb_dev_handle *udev, int intf) +{ + if (!udev) + return -EIO; + + return usb_control_msg(udev, USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE, + DFU_DETACH, 0x1388, intf, NULL, 0, DFU_TIMEOUT); +} + +int dfu_upload(struct usb_dev_handle *udev, int intf, int block, unsigned char *buffer, int size) +{ + if (!udev) + return -EIO; + + return usb_control_msg(udev, USB_TYPE_CLASS | USB_DIR_IN | USB_RECIP_INTERFACE, + DFU_UPLOAD, block, intf, buffer, size, DFU_TIMEOUT); +} + +int dfu_download(struct usb_dev_handle *udev, int intf, int block, unsigned char *buffer, int size) +{ + if (!udev) + return -EIO; + + return usb_control_msg(udev, USB_TYPE_CLASS | USB_DIR_OUT | USB_RECIP_INTERFACE, + DFU_DNLOAD, block, intf, buffer, size, DFU_TIMEOUT); +} + +int dfu_get_status(struct usb_dev_handle *udev, int intf, struct dfu_status *status) +{ + if (!udev || !status) + return -EIO; + + return usb_control_msg(udev, USB_TYPE_CLASS | USB_DIR_IN | USB_RECIP_INTERFACE, + DFU_GETSTATUS, 0, intf, (unsigned char *) status, DFU_STATUS_SIZE, DFU_TIMEOUT); +} + +int dfu_clear_status(struct usb_dev_handle *udev, int intf) +{ + if (!udev) + return -EIO; + + return usb_control_msg(udev, USB_TYPE_CLASS | USB_DIR_IN | USB_RECIP_INTERFACE, + DFU_CLRSTATUS, 0, intf, NULL, 0, DFU_TIMEOUT); +} + +int dfu_get_state(struct usb_dev_handle *udev, int intf, uint8_t *state) +{ + if (!udev || !state) + return -EIO; + + return usb_control_msg(udev, USB_TYPE_CLASS | USB_DIR_IN | USB_RECIP_INTERFACE, + DFU_GETSTATE, 0, intf, (unsigned char *) state, 1, DFU_TIMEOUT); +} + +int dfu_abort(struct usb_dev_handle *udev, int intf) +{ + if (!udev) + return -EIO; + + return usb_control_msg(udev, USB_TYPE_CLASS | USB_DIR_IN | USB_RECIP_INTERFACE, + DFU_ABORT, 0, intf, NULL, 0, DFU_TIMEOUT); +} diff --git a/tools/dfu.h b/tools/dfu.h new file mode 100644 index 00000000..1a253505 --- /dev/null +++ b/tools/dfu.h @@ -0,0 +1,108 @@ +/* + * + * BlueZ - Bluetooth protocol stack for Linux + * + * Copyright (C) 2003-2005 Marcel Holtmann + * + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 as + * published by the Free Software Foundation; + * + * Software distributed under the License is distributed on an "AS + * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or + * implied. See the License for the specific language governing + * rights and limitations under the License. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * + * $Id$ + */ + +#include + +/* CRC interface */ +uint32_t crc32_init(void); +uint32_t crc32_byte(uint32_t accum, uint8_t delta); + +/* DFU descriptor */ +struct usb_dfu_descriptor { + u_int8_t bLength; + u_int8_t bDescriptorType; + u_int8_t bmAttributes; + u_int16_t wDetachTimeout; + u_int16_t wTransferSize; +}; + +/* DFU commands */ +#define DFU_DETACH 0 +#define DFU_DNLOAD 1 +#define DFU_UPLOAD 2 +#define DFU_GETSTATUS 3 +#define DFU_CLRSTATUS 4 +#define DFU_GETSTATE 5 +#define DFU_ABORT 6 + +/* DFU status */ +struct dfu_status { + uint8_t bStatus; + uint8_t bwPollTimeout[3]; + uint8_t bState; + uint8_t iString; +} __attribute__ ((packed)); +#define DFU_STATUS_SIZE 6 + +/* DFU status */ +#define DFU_OK 0x00 +#define DFU_ERR_TARGET 0x01 +#define DFU_ERR_FILE 0x02 +#define DFU_ERR_WRITE 0x03 +#define DFU_ERR_ERASE 0x04 +#define DFU_ERR_CHECK_ERASED 0x05 +#define DFU_ERR_PROG 0x06 +#define DFU_ERR_VERIFY 0x07 +#define DFU_ERR_ADDRESS 0x08 +#define DFU_ERR_NOTDONE 0x09 +#define DFU_ERR_FIRMWARE 0x0a +#define DFU_ERR_VENDOR 0x0b +#define DFU_ERR_USBR 0x0c +#define DFU_ERR_POR 0x0d +#define DFU_ERR_UNKNOWN 0x0e +#define DFU_ERR_STALLEDPKT 0x0f + +/* DFU state */ +#define DFU_STATE_APP_IDLE 0 +#define DFU_STATE_APP_DETACH 1 +#define DFU_STATE_DFU_IDLE 2 +#define DFU_STATE_DFU_DNLOAD_SYNC 3 +#define DFU_STATE_DFU_DNLOAD_BUSY 4 +#define DFU_STATE_DFU_DNLOAD_IDLE 5 +#define DFU_STATE_DFU_MANIFEST_SYNC 6 +#define DFU_STATE_DFU_MANIFEST 7 +#define DFU_STATE_MANIFEST_WAIT_RESET 8 +#define DFU_STATE_UPLOAD_IDLE 9 +#define DFU_STATE_ERROR 10 + +/* DFU suffix */ +struct dfu_suffix { + uint16_t bcdDevice; + uint16_t idProduct; + uint16_t idVendor; + uint16_t bcdDFU; + uint8_t ucDfuSignature[3]; + uint8_t bLength; + uint32_t dwCRC; +} __attribute__ ((packed)); +#define DFU_SUFFIX_SIZE 16 + +/* DFU interface */ +int dfu_detach(struct usb_dev_handle *udev, int intf); +int dfu_upload(struct usb_dev_handle *udev, int intf, int block, unsigned char *buffer, int size); +int dfu_download(struct usb_dev_handle *udev, int intf, int block, unsigned char *buffer, int size); +int dfu_get_status(struct usb_dev_handle *udev, int intf, struct dfu_status *status); +int dfu_clear_status(struct usb_dev_handle *udev, int intf); +int dfu_get_state(struct usb_dev_handle *udev, int intf, uint8_t *state); +int dfu_abort(struct usb_dev_handle *udev, int intf); diff --git a/tools/dfutool.1 b/tools/dfutool.1 new file mode 100644 index 00000000..78b1fe7f --- /dev/null +++ b/tools/dfutool.1 @@ -0,0 +1,53 @@ +.\" +.\" This program is free software; you can redistribute it and/or modify +.\" it under the terms of the GNU General Public License as published by +.\" the Free Software Foundation; either version 2 of the License, or +.\" (at your option) any later version. +.\" +.\" This program is distributed in the hope that it will be useful, +.\" but WITHOUT ANY WARRANTY; without even the implied warranty of +.\" MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +.\" GNU General Public License for more details. +.\" +.\" You should have received a copy of the GNU General Public License +.\" along with this program; if not, write to the Free Software +.\" Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. +.\" +.\" +.TH DFUTOOL 1 "APRIL 21, 2005" "" "" + +.SH NAME +dfutool \- Device Firmware Upgrade utility +.SH SYNOPSIS +.BR "dfutool +[ +.I options +] < +.I command +> +.SH DESCRIPTION +.B dfutool +is used to verify, archive and upgrade firmware files. +.SH OPTIONS +.TP +.BI -h +Gives a list of possible commands. +.TP +.BI -d " " +The command specifies the device to use. +.SH COMMANDS +.TP +.BI verify " " +Display information about the firmware file. +.TP +.BI modify " " +Change DFU specific values in the firmware file. +.TP +.BI upgrade " " +Upgrade the device with a new firmware. +.TP +.BI archive " " +Archive the current firmware of the device. +.SH AUTHOR +Written by Marcel Holtmann . +.br diff --git a/tools/dfutool.c b/tools/dfutool.c new file mode 100644 index 00000000..47510cef --- /dev/null +++ b/tools/dfutool.c @@ -0,0 +1,763 @@ +/* + * + * BlueZ - Bluetooth protocol stack for Linux + * + * Copyright (C) 2003-2005 Marcel Holtmann + * + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 as + * published by the Free Software Foundation; + * + * Software distributed under the License is distributed on an "AS + * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or + * implied. See the License for the specific language governing + * rights and limitations under the License. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * + * $Id$ + */ + +#ifdef HAVE_CONFIG_H +#include +#endif + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include + +#include "dfu.h" + +#ifdef NEED_USB_GET_BUSSES +static inline struct usb_bus *usb_get_busses(void) +{ + return usb_busses; +} +#endif + +#ifndef USB_CLASS_WIRELESS +#define USB_CLASS_WIRELESS 0xe0 +#endif + +#ifndef USB_CLASS_APPLICATION +#define USB_CLASS_APPLICATION 0xfe +#endif + +static int get_interface_number(struct usb_device *dev) +{ + int c, i, a; + + for (c = 0; c < dev->descriptor.bNumConfigurations; c++) { + struct usb_config_descriptor *config = &dev->config[c]; + + for (i = 0; i < config->bNumInterfaces; i++) { + struct usb_interface *interface = &config->interface[i]; + + for (a = 0; a < interface->num_altsetting; a++) { + struct usb_interface_descriptor *desc = &interface->altsetting[a]; + + if (desc->bInterfaceClass != USB_CLASS_APPLICATION) + continue; + if (desc->bInterfaceSubClass != 0x01) + continue; + if (desc->bInterfaceProtocol != 0x00) + continue; + + return desc->bInterfaceNumber; + } + } + } + + return -1; +} + +static void print_device(struct usb_device *dev) +{ + printf("Bus %s Device %s: ID %04x:%04x Interface %d%s\n", + dev->bus->dirname, dev->filename, + dev->descriptor.idVendor, dev->descriptor.idProduct, + get_interface_number(dev), + dev->descriptor.bDeviceClass == USB_CLASS_APPLICATION ? " (DFU mode)" : ""); +} + +static struct usb_dev_handle *open_device(char *device, struct dfu_suffix *suffix) +{ + struct usb_bus *bus; + struct usb_device *dev, *dfu_dev[10]; + struct usb_dev_handle *udev; + struct dfu_status status; + char str[8]; + int i, intf, sel, num = 0, try = 5, bus_id = -1, dev_id = -1; + + printf("Scanning USB busses ... "); + fflush(stdout); + + usb_find_busses(); + usb_find_devices(); + + for (bus = usb_get_busses(); bus; bus = bus->next) { + if (bus_id > 0) { + snprintf(str, sizeof(str) - 1, "%03i", bus_id); + if (strcmp(str, bus->dirname)) + continue; + } + + for (dev = bus->devices; dev; dev = dev->next) { + if (bus_id > 0 && dev_id > 0) { + snprintf(str, sizeof(str) - 1, "%03i", dev_id); + if (strcmp(str, dev->filename)) + continue; + } + + if (dev->descriptor.bDeviceClass == USB_CLASS_HUB) + continue; + + if (num > 9 || get_interface_number(dev) < 0) + continue; + + dfu_dev[num++] = dev; + } + } + + if (num < 1) { + printf("\rCan't find any DFU devices\n"); + return NULL; + } + + printf("\rAvailable devices with DFU support:\n\n"); + for (i = 0; i < num; i++) { + printf("\t%2d) ", i + 1); + print_device(dfu_dev[i]); + } + printf("\n"); + + do { + printf("\rSelect device (abort with 0): "); + fflush(stdout); + memset(str, 0, sizeof(str)); + fgets(str, sizeof(str) - 1, stdin); + sel = atoi(str); + } while (!isdigit(str[0]) || sel < 0 || sel > num ); + + if (sel < 1) + return NULL; + + sel--; + intf = get_interface_number(dfu_dev[sel]); + printf("\n"); + + udev = usb_open(dfu_dev[sel]); + if (!udev) { + printf("Can't open device: %s (%d)\n", strerror(errno), errno); + return NULL; + } + + if (usb_claim_interface(udev, intf) < 0) { + printf("Can't claim interface: %s (%d)\n", strerror(errno), errno); + usb_close(udev); + return NULL; + } + + if (dfu_get_status(udev, intf, &status) < 0) { + printf("Can't get status: %s (%d)\n", strerror(errno), errno); + goto error; + } + + if (status.bState == DFU_STATE_ERROR) { + if (dfu_clear_status(udev, intf) < 0) { + printf("Can't clear status: %s (%d)\n", strerror(errno), errno); + goto error; + } + if (dfu_abort(udev, intf) < 0) { + printf("Can't abort previous action: %s (%d)\n", strerror(errno), errno); + goto error; + } + if (dfu_get_status(udev, intf, &status) < 0) { + printf("Can't get status: %s (%d)\n", strerror(errno), errno); + goto error; + } + } + + if (status.bState == DFU_STATE_DFU_IDLE) { + if (suffix) { + suffix->idVendor = 0x0000; + suffix->idProduct = 0x0000; + suffix->bcdDevice = 0x0000; + } + return udev; + } + + if (status.bState != DFU_STATE_APP_IDLE) { + printf("Device is not idle, can't detach it (state %d)\n", status.bState); + goto error; + } + + printf("Switching device into DFU mode ... "); + fflush(stdout); + + if (suffix) { + suffix->idVendor = dfu_dev[sel]->descriptor.idVendor; + suffix->idProduct = dfu_dev[sel]->descriptor.idProduct; + suffix->bcdDevice = dfu_dev[sel]->descriptor.bcdDevice; + } + + if (dfu_detach(udev, intf) < 0) { + printf("\rCan't detach device: %s (%d)\n", strerror(errno), errno); + goto error; + } + + if (dfu_get_status(udev, intf, &status) < 0) { + printf("\rCan't get status: %s (%d)\n", strerror(errno), errno); + goto error; + } + + if (status.bState != DFU_STATE_APP_DETACH) { + printf("\rDevice is not in detach mode, try again\n"); + goto error; + } + + usb_release_interface(udev, intf); + usb_reset(udev); + usb_close(udev); + + bus = dfu_dev[sel]->bus; + num = 0; + + while (num != 1 && try-- > 0) { + sleep(1); + usb_find_devices(); + + for (dev = bus->devices; dev; dev = dev->next) { + if (dev->descriptor.bDeviceClass != USB_CLASS_APPLICATION) + continue; + + if (suffix && dev->descriptor.idVendor != suffix->idVendor) + continue; + + if (num > 9 || get_interface_number(dev) != 0) + continue; + + dfu_dev[num++] = dev; + } + } + + if (num != 1) { + printf("\rCan't identify device with DFU mode\n"); + goto error; + } + + printf("\r"); + + intf = 0; + + udev = usb_open(dfu_dev[0]); + if (!udev) { + printf("Can't open device: %s (%d)\n", strerror(errno), errno); + return NULL; + } + + if (usb_claim_interface(udev, intf) < 0) { + printf("Can't claim interface: %s (%d)\n", strerror(errno), errno); + usb_close(udev); + return NULL; + } + + if (dfu_get_status(udev, intf, &status) < 0) { + printf("Can't get status: %s (%d)\n", strerror(errno), errno); + goto error; + } + + if (status.bState != DFU_STATE_DFU_IDLE) { + printf("Device is not in DFU mode, can't use it\n"); + goto error; + } + + return udev; + +error: + usb_release_interface(udev, intf); + usb_close(udev); + return NULL; +} + +static void usage(void); + +static void cmd_verify(char *device, int argc, char **argv) +{ + struct stat st; + struct dfu_suffix *suffix; + uint32_t crc; + char str[16]; + unsigned char *buf; + unsigned long size; + char *filename; + int i, fd, len; + + if (argc < 2) { + usage(); + exit(1); + } + + filename = argv[1]; + + if (stat(filename, &st) < 0) { + perror("Can't access firmware"); + exit(1); + } + + size = st.st_size; + + if (!(buf = malloc(size))) { + perror("Unable to allocate file buffer"); + exit(1); + } + + if ((fd = open(filename, O_RDONLY)) < 0) { + perror("Can't open firmware"); + free(buf); + exit(1); + } + + if (read(fd, buf, size) < size) { + perror("Can't load firmware"); + free(buf); + close(fd); + exit(1); + } + + printf("Filename\t%s\n", basename(filename)); + printf("Filesize\t%ld\n", size); + + crc = crc32_init(); + for (i = 0; i < size - 4; i++) + crc = crc32_byte(crc, buf[i]); + printf("Checksum\t%08x\n", crc); + + printf("\n"); + len = buf[size - 5]; + printf("DFU suffix\t"); + for (i = 0; i < len; i++) { + printf("%02x ", buf[size - len + i]); + } + printf("\n\n"); + + suffix = (struct dfu_suffix *) (buf + size - DFU_SUFFIX_SIZE); + + printf("idVendor\t%04x\n", suffix->idVendor); + printf("idProduct\t%04x\n", suffix->idProduct); + printf("bcdDevice\t%x\n", suffix->bcdDevice); + + printf("\n"); + + printf("bcdDFU\t\t%x.%x\n", suffix->bcdDFU >> 8, suffix->bcdDFU & 0xff); + printf("ucDfuSignature\t%c%c%c\n", suffix->ucDfuSignature[2], + suffix->ucDfuSignature[1], suffix->ucDfuSignature[0]); + printf("bLength\t\t%d\n", suffix->bLength); + printf("dwCRC\t\t%08x\n", suffix->dwCRC); + printf("\n"); + + memset(str, 0, sizeof(str)); + memcpy(str, buf, 8); + + if (!strcmp(str, "CSR-dfu1") || !strcmp(str, "CSR-dfu2")) { + crc = crc32_init(); + for (i = 0; i < size - DFU_SUFFIX_SIZE; i++) + crc = crc32_byte(crc, buf[i]); + + printf("Firmware type\t%s\n", str); + printf("Firmware check\t%s checksum\n", crc == 0 ? "valid" : "corrupt"); + printf("\n"); + } + + free(buf); + + close(fd); +} + +static void cmd_modify(char *device, int argc, char **argv) +{ +} + +static void cmd_upgrade(char *device, int argc, char **argv) +{ + struct usb_dev_handle *udev; + struct dfu_status status; + struct dfu_suffix suffix; + struct stat st; + unsigned char *buf; + unsigned long filesize, count, timeout = 0; + char *filename; + uint32_t crc; + int fd, i, block, len, size, sent = 0, try = 10; + + if (argc < 2) { + usage(); + exit(1); + } + + filename = argv[1]; + + if (stat(filename, &st) < 0) { + perror("Can't access firmware"); + exit(1); + } + + filesize = st.st_size; + + if (!(buf = malloc(filesize))) { + perror("Unable to allocate file buffer"); + exit(1); + } + + if ((fd = open(filename, O_RDONLY)) < 0) { + perror("Can't open firmware"); + free(buf); + exit(1); + } + + if (read(fd, buf, filesize) < filesize) { + perror("Can't load firmware"); + free(buf); + close(fd); + exit(1); + } + + memcpy(&suffix, buf + filesize - DFU_SUFFIX_SIZE, sizeof(suffix)); + + printf("Filename\t%s\n", basename(filename)); + printf("Filesize\t%ld\n", filesize); + + crc = crc32_init(); + for (i = 0; i < filesize - 4; i++) + crc = crc32_byte(crc, buf[i]); + printf("Checksum\t%08x (%s)\n", crc, + crc == suffix.dwCRC ? "valid" : "corrupt"); + + if (crc != suffix.dwCRC) { + free(buf); + close(fd); + exit(1); + } + + printf("\n"); + + udev = open_device(device, &suffix); + if (!udev) + exit(1); + + printf("\r" " " " " " " " " " "); + printf("\rFirmware download ... "); + fflush(stdout); + + count = filesize - DFU_SUFFIX_SIZE; + block = 0; + + while (count) { + size = (count > 1023) ? 1023 : count; + + if (dfu_get_status(udev, 0, &status) < 0) { + if (try-- > 0) { + sleep(1); + continue; + } + printf("\rCan't get status: %s (%d)\n", strerror(errno), errno); + goto done; + } + + if (status.bStatus != DFU_OK) { + if (try-- > 0) { + dfu_clear_status(udev, 0); + sleep(1); + continue; + } + printf("\rFirmware download ... aborting (status %d state %d)\n", + status.bStatus, status.bState); + goto done; + } + + if (status.bState != DFU_STATE_DFU_IDLE && + status.bState != DFU_STATE_DFU_DNLOAD_IDLE) { + sleep(1); + continue; + } + + timeout = (status.bwPollTimeout[2] << 16) | + (status.bwPollTimeout[1] << 8) | + status.bwPollTimeout[0]; + + usleep(timeout * 1000); + + len = dfu_download(udev, 0, block, buf + sent, size); + if (len < 0) { + if (try-- > 0) { + sleep(1); + continue; + } + printf("\rCan't upload next block: %s (%d)\n", strerror(errno), errno); + goto done; + } + + printf("\rFirmware download ... %d bytes ", block * 1023 + len); + fflush(stdout); + + sent += len; + count -= len; + block++; + } + + printf("\r" " " " " " " " " " "); + printf("\rFinishing firmware download ... "); + fflush(stdout); + + sleep(1); + + if (dfu_get_status(udev, 0, &status) < 0) { + printf("\rCan't get status: %s (%d)\n", strerror(errno), errno); + goto done; + } + + timeout = (status.bwPollTimeout[2] << 16) | + (status.bwPollTimeout[1] << 8) | + status.bwPollTimeout[0]; + + usleep(timeout * 1000); + + if (count == 0) { + len = dfu_download(udev, 0, block, NULL, 0); + if (len < 0) { + printf("\rCan't send final block: %s (%d)\n", strerror(errno), errno); + goto done; + } + } + + printf("\r" " " " " " " " " " "); + printf("\rWaiting for device ... "); + fflush(stdout); + + sleep(10); + + printf("\n"); + +done: + free(buf); + close(fd); + + usb_release_interface(udev, 0); + usb_reset(udev); + usb_close(udev); +} + +static void cmd_archive(char *device, int argc, char **argv) +{ + struct usb_dev_handle *udev; + struct dfu_status status; + struct dfu_suffix suffix; + unsigned char buf[2048]; + unsigned long timeout = 0; + char *filename; + uint32_t crc; + int fd, i, n, len, try = 8; + + if (argc < 2) { + usage(); + exit(1); + } + + filename = argv[1]; + + udev = open_device(device, &suffix); + if (!udev) + exit(1); + + fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH); + if (fd < 0) { + printf("Can't open firmware file: %s (%d)\n", strerror(errno), errno); + goto done; + } + + printf("\r" " " " " " " " " " "); + printf("\rFirmware upload ... "); + fflush(stdout); + + crc = crc32_init(); + n = 0; + while (1) { + if (dfu_get_status(udev, 0, &status) < 0) { + if (try-- > 0) { + sleep(1); + continue; + } + printf("\rCan't get status: %s (%d)\n", strerror(errno), errno); + goto done; + } + + if (status.bStatus != DFU_OK) { + if (try-- > 0) { + dfu_clear_status(udev, 0); + sleep(1); + continue; + } + printf("\rFirmware upload ... aborting (status %d state %d)\n", + status.bStatus, status.bState); + goto done; + } + + if (status.bState != DFU_STATE_DFU_IDLE && + status.bState != DFU_STATE_UPLOAD_IDLE) { + sleep(1); + continue; + } + + timeout = (status.bwPollTimeout[2] << 16) | + (status.bwPollTimeout[1] << 8) | + status.bwPollTimeout[0]; + + usleep(timeout * 1000); + + len = dfu_upload(udev, 0, n, buf, 1023); + if (len < 0) { + if (try-- > 0) { + sleep(1); + continue; + } + printf("\rCan't upload next block: %s (%d)\n", strerror(errno), errno); + goto done; + } + + printf("\rFirmware upload ... %d bytes ", n * 1023 + len); + fflush(stdout); + + for (i = 0; i < len; i++) + crc = crc32_byte(crc, buf[i]); + + if (len > 0) + write(fd, buf, len); + + n++; + if (len != 1023) + break; + } + printf("\n"); + + suffix.bcdDFU = 0x0100; + suffix.ucDfuSignature[0] = 'U'; + suffix.ucDfuSignature[1] = 'F'; + suffix.ucDfuSignature[2] = 'D'; + suffix.bLength = DFU_SUFFIX_SIZE; + + memcpy(buf, &suffix, DFU_SUFFIX_SIZE); + for (i = 0; i < DFU_SUFFIX_SIZE - 4; i++) + crc = crc32_byte(crc, buf[i]); + + suffix.dwCRC = crc; + + write(fd, &suffix, DFU_SUFFIX_SIZE); + +done: + close(fd); + + usb_release_interface(udev, 0); + usb_reset(udev); + usb_close(udev); +} + +struct { + char *cmd; + char *alt; + void (*func)(char *device, int argc, char **argv); + char *opt; + char *doc; +} command[] = { + { "verify", "check", cmd_verify, "", "Check firmware file" }, + { "modify", "change", cmd_modify, "", "Change firmware attributes" }, + { "upgrade", "download", cmd_upgrade, "", "Download a new firmware" }, + { "archive", "upload", cmd_archive, "", "Upload the current firmware" }, + { NULL, NULL, NULL, 0, 0 } +}; + +static void usage(void) +{ + int i; + + printf("dfutool - Device Firmware Upgrade utility ver %s\n\n", VERSION); + + printf("Usage:\n" + "\tdfutool [options] \n" + "\n"); + + printf("Options:\n" + "\t-d, --device USB device\n" + "\t-h, --help Display help\n" + "\n"); + + printf("Commands:\n"); + for (i = 0; command[i].cmd; i++) + printf("\t%-8s %-10s\t%s\n", command[i].cmd, + command[i].opt ? command[i].opt : " ", + command[i].doc); + printf("\n"); +} + +static struct option main_options[] = { + { "help", 0, 0, 'h' }, + { "device", 1, 0, 'd' }, + { 0, 0, 0, 0 } +}; + +int main(int argc, char *argv[]) +{ + char *device = NULL; + int i, opt; + + while ((opt = getopt_long(argc, argv, "+d:h", main_options, NULL)) != -1) { + switch(opt) { + case 'd': + device = strdup(optarg); + break; + + case 'h': + usage(); + exit(0); + + default: + exit(0); + } + } + + argc -= optind; + argv += optind; + optind = 0; + + if (argc < 1) { + usage(); + exit(1); + } + + usb_init(); + + for (i = 0; command[i].cmd; i++) { + if (strcmp(command[i].cmd, argv[0]) && strcmp(command[i].alt, argv[0])) + continue; + command[i].func(device, argc, argv); + exit(0); + } + + usage(); + exit(1); +} -- cgit