summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorUwe Hermann <uwe@debian.org>2008-02-15 15:33:24 +0100
committerAndrej Shadura <andrewsh@debian.org>2008-02-15 15:33:24 +0100
commite1b659b4b5702eaa32ed6ff594ab555044255a71 (patch)
treebd5c473022a00afdf62ca7deef2807d7d434aaea
Import Upstream version 0.0+r4067
-rw-r--r--COPYING340
-rw-r--r--Makefile.am3
-rwxr-xr-xautogen.sh4
-rw-r--r--configure.ac40
-rw-r--r--src/Makefile.am16
-rw-r--r--src/commands.c375
-rw-r--r--src/commands.h30
-rw-r--r--src/dfu.c420
-rw-r--r--src/dfu.h103
-rw-r--r--src/main.c821
-rw-r--r--src/sam7dfu.c205
-rw-r--r--src/sam7dfu.h9
-rw-r--r--src/usb_dfu.h94
13 files changed, 2460 insertions, 0 deletions
diff --git a/COPYING b/COPYING
new file mode 100644
index 0000000..d60c31a
--- /dev/null
+++ b/COPYING
@@ -0,0 +1,340 @@
+ GNU GENERAL PUBLIC LICENSE
+ Version 2, June 1991
+
+ Copyright (C) 1989, 1991 Free Software Foundation, Inc.
+ 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ Everyone is permitted to copy and distribute verbatim copies
+ of this license document, but changing it is not allowed.
+
+ Preamble
+
+ The licenses for most software are designed to take away your
+freedom to share and change it. By contrast, the GNU General Public
+License is intended to guarantee your freedom to share and change free
+software--to make sure the software is free for all its users. This
+General Public License applies to most of the Free Software
+Foundation's software and to any other program whose authors commit to
+using it. (Some other Free Software Foundation software is covered by
+the GNU Library General Public License instead.) You can apply it to
+your programs, too.
+
+ When we speak of free software, we are referring to freedom, not
+price. Our General Public Licenses are designed to make sure that you
+have the freedom to distribute copies of free software (and charge for
+this service if you wish), that you receive source code or can get it
+if you want it, that you can change the software or use pieces of it
+in new free programs; and that you know you can do these things.
+
+ To protect your rights, we need to make restrictions that forbid
+anyone to deny you these rights or to ask you to surrender the rights.
+These restrictions translate to certain responsibilities for you if you
+distribute copies of the software, or if you modify it.
+
+ For example, if you distribute copies of such a program, whether
+gratis or for a fee, you must give the recipients all the rights that
+you have. You must make sure that they, too, receive or can get the
+source code. And you must show them these terms so they know their
+rights.
+
+ We protect your rights with two steps: (1) copyright the software, and
+(2) offer you this license which gives you legal permission to copy,
+distribute and/or modify the software.
+
+ Also, for each author's protection and ours, we want to make certain
+that everyone understands that there is no warranty for this free
+software. If the software is modified by someone else and passed on, we
+want its recipients to know that what they have is not the original, so
+that any problems introduced by others will not reflect on the original
+authors' reputations.
+
+ Finally, any free program is threatened constantly by software
+patents. We wish to avoid the danger that redistributors of a free
+program will individually obtain patent licenses, in effect making the
+program proprietary. To prevent this, we have made it clear that any
+patent must be licensed for everyone's free use or not licensed at all.
+
+ The precise terms and conditions for copying, distribution and
+modification follow.
+
+ GNU GENERAL PUBLIC LICENSE
+ TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
+
+ 0. This License applies to any program or other work which contains
+a notice placed by the copyright holder saying it may be distributed
+under the terms of this General Public License. The "Program", below,
+refers to any such program or work, and a "work based on the Program"
+means either the Program or any derivative work under copyright law:
+that is to say, a work containing the Program or a portion of it,
+either verbatim or with modifications and/or translated into another
+language. (Hereinafter, translation is included without limitation in
+the term "modification".) Each licensee is addressed as "you".
+
+Activities other than copying, distribution and modification are not
+covered by this License; they are outside its scope. The act of
+running the Program is not restricted, and the output from the Program
+is covered only if its contents constitute a work based on the
+Program (independent of having been made by running the Program).
+Whether that is true depends on what the Program does.
+
+ 1. You may copy and distribute verbatim copies of the Program's
+source code as you receive it, in any medium, provided that you
+conspicuously and appropriately publish on each copy an appropriate
+copyright notice and disclaimer of warranty; keep intact all the
+notices that refer to this License and to the absence of any warranty;
+and give any other recipients of the Program a copy of this License
+along with the Program.
+
+You may charge a fee for the physical act of transferring a copy, and
+you may at your option offer warranty protection in exchange for a fee.
+
+ 2. You may modify your copy or copies of the Program or any portion
+of it, thus forming a work based on the Program, and copy and
+distribute such modifications or work under the terms of Section 1
+above, provided that you also meet all of these conditions:
+
+ a) You must cause the modified files to carry prominent notices
+ stating that you changed the files and the date of any change.
+
+ b) You must cause any work that you distribute or publish, that in
+ whole or in part contains or is derived from the Program or any
+ part thereof, to be licensed as a whole at no charge to all third
+ parties under the terms of this License.
+
+ c) If the modified program normally reads commands interactively
+ when run, you must cause it, when started running for such
+ interactive use in the most ordinary way, to print or display an
+ announcement including an appropriate copyright notice and a
+ notice that there is no warranty (or else, saying that you provide
+ a warranty) and that users may redistribute the program under
+ these conditions, and telling the user how to view a copy of this
+ License. (Exception: if the Program itself is interactive but
+ does not normally print such an announcement, your work based on
+ the Program is not required to print an announcement.)
+
+These requirements apply to the modified work as a whole. If
+identifiable sections of that work are not derived from the Program,
+and can be reasonably considered independent and separate works in
+themselves, then this License, and its terms, do not apply to those
+sections when you distribute them as separate works. But when you
+distribute the same sections as part of a whole which is a work based
+on the Program, the distribution of the whole must be on the terms of
+this License, whose permissions for other licensees extend to the
+entire whole, and thus to each and every part regardless of who wrote it.
+
+Thus, it is not the intent of this section to claim rights or contest
+your rights to work written entirely by you; rather, the intent is to
+exercise the right to control the distribution of derivative or
+collective works based on the Program.
+
+In addition, mere aggregation of another work not based on the Program
+with the Program (or with a work based on the Program) on a volume of
+a storage or distribution medium does not bring the other work under
+the scope of this License.
+
+ 3. You may copy and distribute the Program (or a work based on it,
+under Section 2) in object code or executable form under the terms of
+Sections 1 and 2 above provided that you also do one of the following:
+
+ a) Accompany it with the complete corresponding machine-readable
+ source code, which must be distributed under the terms of Sections
+ 1 and 2 above on a medium customarily used for software interchange; or,
+
+ b) Accompany it with a written offer, valid for at least three
+ years, to give any third party, for a charge no more than your
+ cost of physically performing source distribution, a complete
+ machine-readable copy of the corresponding source code, to be
+ distributed under the terms of Sections 1 and 2 above on a medium
+ customarily used for software interchange; or,
+
+ c) Accompany it with the information you received as to the offer
+ to distribute corresponding source code. (This alternative is
+ allowed only for noncommercial distribution and only if you
+ received the program in object code or executable form with such
+ an offer, in accord with Subsection b above.)
+
+The source code for a work means the preferred form of the work for
+making modifications to it. For an executable work, complete source
+code means all the source code for all modules it contains, plus any
+associated interface definition files, plus the scripts used to
+control compilation and installation of the executable. However, as a
+special exception, the source code distributed need not include
+anything that is normally distributed (in either source or binary
+form) with the major components (compiler, kernel, and so on) of the
+operating system on which the executable runs, unless that component
+itself accompanies the executable.
+
+If distribution of executable or object code is made by offering
+access to copy from a designated place, then offering equivalent
+access to copy the source code from the same place counts as
+distribution of the source code, even though third parties are not
+compelled to copy the source along with the object code.
+
+ 4. You may not copy, modify, sublicense, or distribute the Program
+except as expressly provided under this License. Any attempt
+otherwise to copy, modify, sublicense or distribute the Program is
+void, and will automatically terminate your rights under this License.
+However, parties who have received copies, or rights, from you under
+this License will not have their licenses terminated so long as such
+parties remain in full compliance.
+
+ 5. You are not required to accept this License, since you have not
+signed it. However, nothing else grants you permission to modify or
+distribute the Program or its derivative works. These actions are
+prohibited by law if you do not accept this License. Therefore, by
+modifying or distributing the Program (or any work based on the
+Program), you indicate your acceptance of this License to do so, and
+all its terms and conditions for copying, distributing or modifying
+the Program or works based on it.
+
+ 6. Each time you redistribute the Program (or any work based on the
+Program), the recipient automatically receives a license from the
+original licensor to copy, distribute or modify the Program subject to
+these terms and conditions. You may not impose any further
+restrictions on the recipients' exercise of the rights granted herein.
+You are not responsible for enforcing compliance by third parties to
+this License.
+
+ 7. If, as a consequence of a court judgment or allegation of patent
+infringement or for any other reason (not limited to patent issues),
+conditions are imposed on you (whether by court order, agreement or
+otherwise) that contradict the conditions of this License, they do not
+excuse you from the conditions of this License. If you cannot
+distribute so as to satisfy simultaneously your obligations under this
+License and any other pertinent obligations, then as a consequence you
+may not distribute the Program at all. For example, if a patent
+license would not permit royalty-free redistribution of the Program by
+all those who receive copies directly or indirectly through you, then
+the only way you could satisfy both it and this License would be to
+refrain entirely from distribution of the Program.
+
+If any portion of this section is held invalid or unenforceable under
+any particular circumstance, the balance of the section is intended to
+apply and the section as a whole is intended to apply in other
+circumstances.
+
+It is not the purpose of this section to induce you to infringe any
+patents or other property right claims or to contest validity of any
+such claims; this section has the sole purpose of protecting the
+integrity of the free software distribution system, which is
+implemented by public license practices. Many people have made
+generous contributions to the wide range of software distributed
+through that system in reliance on consistent application of that
+system; it is up to the author/donor to decide if he or she is willing
+to distribute software through any other system and a licensee cannot
+impose that choice.
+
+This section is intended to make thoroughly clear what is believed to
+be a consequence of the rest of this License.
+
+ 8. If the distribution and/or use of the Program is restricted in
+certain countries either by patents or by copyrighted interfaces, the
+original copyright holder who places the Program under this License
+may add an explicit geographical distribution limitation excluding
+those countries, so that distribution is permitted only in or among
+countries not thus excluded. In such case, this License incorporates
+the limitation as if written in the body of this License.
+
+ 9. The Free Software Foundation may publish revised and/or new versions
+of the General Public License from time to time. Such new versions will
+be similar in spirit to the present version, but may differ in detail to
+address new problems or concerns.
+
+Each version is given a distinguishing version number. If the Program
+specifies a version number of this License which applies to it and "any
+later version", you have the option of following the terms and conditions
+either of that version or of any later version published by the Free
+Software Foundation. If the Program does not specify a version number of
+this License, you may choose any version ever published by the Free Software
+Foundation.
+
+ 10. If you wish to incorporate parts of the Program into other free
+programs whose distribution conditions are different, write to the author
+to ask for permission. For software which is copyrighted by the Free
+Software Foundation, write to the Free Software Foundation; we sometimes
+make exceptions for this. Our decision will be guided by the two goals
+of preserving the free status of all derivatives of our free software and
+of promoting the sharing and reuse of software generally.
+
+ NO WARRANTY
+
+ 11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY
+FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN
+OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES
+PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED
+OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
+MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS
+TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE
+PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING,
+REPAIR OR CORRECTION.
+
+ 12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
+WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR
+REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES,
+INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING
+OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED
+TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY
+YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER
+PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE
+POSSIBILITY OF SUCH DAMAGES.
+
+ END OF TERMS AND CONDITIONS
+
+ How to Apply These Terms to Your New Programs
+
+ If you develop a new program, and you want it to be of the greatest
+possible use to the public, the best way to achieve this is to make it
+free software which everyone can redistribute and change under these terms.
+
+ To do so, attach the following notices to the program. It is safest
+to attach them to the start of each source file to most effectively
+convey the exclusion of warranty; and each file should have at least
+the "copyright" line and a pointer to where the full notice is found.
+
+ <one line to give the program's name and a brief idea of what it does.>
+ Copyright (C) <year> <name of author>
+
+ 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+
+
+Also add information on how to contact you by electronic and paper mail.
+
+If the program is interactive, make it output a short notice like this
+when it starts in an interactive mode:
+
+ Gnomovision version 69, Copyright (C) year name of author
+ Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'.
+ This is free software, and you are welcome to redistribute it
+ under certain conditions; type `show c' for details.
+
+The hypothetical commands `show w' and `show c' should show the appropriate
+parts of the General Public License. Of course, the commands you use may
+be called something other than `show w' and `show c'; they could even be
+mouse-clicks or menu items--whatever suits your program.
+
+You should also get your employer (if you work as a programmer) or your
+school, if any, to sign a "copyright disclaimer" for the program, if
+necessary. Here is a sample; alter the names:
+
+ Yoyodyne, Inc., hereby disclaims all copyright interest in the program
+ `Gnomovision' (which makes passes at compilers) written by James Hacker.
+
+ <signature of Ty Coon>, 1 April 1989
+ Ty Coon, President of Vice
+
+This General Public License does not permit incorporating your program into
+proprietary programs. If your program is a subroutine library, you may
+consider it more useful to permit linking proprietary applications with the
+library. If this is what you want to do, use the GNU Library General
+Public License instead of this License.
diff --git a/Makefile.am b/Makefile.am
new file mode 100644
index 0000000..5ff2f58
--- /dev/null
+++ b/Makefile.am
@@ -0,0 +1,3 @@
+SUBDIRS = src
+
+EXTRA_DIST = autogen.sh
diff --git a/autogen.sh b/autogen.sh
new file mode 100755
index 0000000..6bd21eb
--- /dev/null
+++ b/autogen.sh
@@ -0,0 +1,4 @@
+#! /bin/sh
+AUTOMAKE="automake --foreign --add-missing --copy"
+export AUTOMAKE
+autoreconf
diff --git a/configure.ac b/configure.ac
new file mode 100644
index 0000000..593a909
--- /dev/null
+++ b/configure.ac
@@ -0,0 +1,40 @@
+# -*- Autoconf -*-
+# Process this file with autoconf to produce a configure script.
+
+AC_PREREQ(2.59)
+AC_INIT([dfu-util],[0.1])
+AC_CONFIG_AUX_DIR(m4)
+AM_INIT_AUTOMAKE(AC_PACKAGE_NAME, AC_PACKAGE_VERSION)
+#AC_CONFIG_SRCDIR([src/atmel.c])
+AM_CONFIG_HEADER([config.h])
+AC_C_BIGENDIAN
+
+AM_MAINTAINER_MODE
+
+# Checks for programs.
+AC_PROG_CC
+
+# Checks for libraries.
+
+PKG_CHECK_MODULES(USB, libusb >= 0.1.4,,
+ AC_MSG_ERROR([*** Required libusb >= 0.1.4 not installed ***]))
+AC_CHECK_LIB([usbpath],[usb_path2devnum],,,-lusb)
+
+LIBS="$LIBS $USB_LIBS"
+CFLAGS="$CFLAGS $USB_CFLAGS"
+
+# Checks for header files.
+AC_HEADER_STDC
+AC_CHECK_HEADERS([stdlib.h string.h stdio.h usbpath.h])
+
+# Checks for typedefs, structures, and compiler characteristics.
+AC_C_CONST
+AC_TYPE_SIZE_T
+
+# Checks for library functions.
+AC_FUNC_MALLOC
+AC_FUNC_MEMCMP
+AC_CHECK_FUNCS([memset])
+
+AC_CONFIG_FILES(Makefile src/Makefile)
+AC_OUTPUT
diff --git a/src/Makefile.am b/src/Makefile.am
new file mode 100644
index 0000000..75730dd
--- /dev/null
+++ b/src/Makefile.am
@@ -0,0 +1,16 @@
+AM_CFLAGS = -Wall
+
+dfu-version.h:
+ echo -e '#ifndef DFU_UTIL_VERSION' \
+ '\n#define DFU_UTIL_VERSION "'`svnversion`'"' \
+ '\n#endif' > dfu-version.h
+BUILT_SOURCES = dfu-version.h
+
+bin_PROGRAMS = dfu-util dfu-util_static
+dfu_util_SOURCES = main.c sam7dfu.c dfu.c dfu.h
+
+dfu_util_static_SOURCES = main.c sam7dfu.c dfu.c dfu.h
+dfu_util_static_LDFLAGS = -static
+
+# commands.c commands.h sam7dfu.c
+
diff --git a/src/commands.c b/src/commands.c
new file mode 100644
index 0000000..e782be6
--- /dev/null
+++ b/src/commands.c
@@ -0,0 +1,375 @@
+/*
+ * dfu-programmer
+ *
+ * $Id: commands.c,v 1.6 2006/06/25 00:01:37 schmidtw Exp $
+ *
+ * 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ */
+
+#include <stdio.h>
+#include <string.h>
+
+#include "config.h"
+#include "commands.h"
+#include "arguments.h"
+#include "intel_hex.h"
+#include "atmel.h"
+
+
+static int execute_erase( struct usb_dev_handle *device,
+ int interface,
+ struct programmer_arguments args )
+{
+ int result = 0;
+
+ if( 2 < debug ) {
+ fprintf( stderr, "%s: erase %d bytes\n", __FUNCTION__,
+ args.memory_size );
+ }
+
+ result = atmel_erase_flash( device, interface, ATMEL_ERASE_ALL );
+ if( 0 != result )
+ return result;
+
+ return atmel_blank_check( device, interface, 0, args.top_memory_address );
+}
+
+
+static int execute_flash( struct usb_dev_handle *device,
+ int interface,
+ struct programmer_arguments args )
+{
+ char *hex_data = NULL;
+ int usage = 0;
+ int retval = -1;
+ int result = 0;
+ char *buffer = NULL;
+ int i;
+
+ buffer = (char *) malloc( args.memory_size );
+ if( NULL == buffer ) {
+ fprintf( stderr, "Request for %d bytes of memory failed.\n",
+ args.memory_size );
+ goto error;
+ }
+
+ memset( buffer, 0, args.memory_size );
+
+ hex_data = intel_hex_to_buffer( args.com_flash_data.file,
+ args.memory_size, 0xff, &usage );
+ if( NULL == hex_data ) {
+ fprintf( stderr,
+ "Something went wrong with creating the memory image.\n" );
+ goto error;
+ }
+
+ if( 2 < debug ) {
+ fprintf( stderr, "%s: write %d/%d bytes\n", __FUNCTION__,
+ usage, args.memory_size );
+ }
+
+ result = atmel_flash( device, interface, 0, args.top_memory_address,
+ hex_data );
+
+ if( args.memory_size != result ) {
+ fprintf( stderr, "Error while flashing. (%d)\n", result );
+ goto error;
+ }
+
+ if( 0 == args.com_flash_data.suppress_validation ) {
+ fprintf( stderr, "Validating...\n" );
+
+ result = atmel_read_flash( device, interface, 0,
+ args.top_memory_address, buffer,
+ args.memory_size );
+
+ if( args.memory_size != result ) {
+ fprintf( stderr, "Error while validating.\n" );
+ goto error;
+ }
+
+ if( 0 != memcmp(hex_data, buffer, args.memory_size) ) {
+ fprintf( stderr, "Image did not validate.\n" );
+ goto error;
+ }
+ }
+
+ if( 0 == args.quiet ) {
+ fprintf( stderr, "%d bytes used (%.02f%%)\n", usage,
+ ((float)(usage*100)/(float)(args.top_memory_address)) );
+ }
+
+ retval = 0;
+
+error:
+ if( NULL != buffer ) {
+ free( buffer );
+ buffer = NULL;
+ }
+
+ if( NULL != hex_data ) {
+ free( hex_data );
+ hex_data = NULL;
+ }
+
+ return retval;
+}
+
+
+static int execute_start_app( struct usb_dev_handle *device,
+ int interface,
+ struct programmer_arguments args )
+{
+ return atmel_start_app( device, interface );
+}
+
+
+static int execute_get( struct usb_dev_handle *device,
+ int interface,
+ struct programmer_arguments args )
+{
+ struct atmel_device_info info;
+ char *message = NULL;
+ short value = 0;
+ int status;
+ int controller_error = 0;
+
+ if( device_8051 == args.device_type ) {
+ status = atmel_read_config_8051( device, interface, &info );
+ } else {
+ status = atmel_read_config_avr( device, interface, &info );
+ }
+
+ if( 0 != status ) {
+ fprintf( stderr, "Error reading %s config information.\n",
+ args.device_type_string );
+ return status;
+ }
+
+ switch( args.com_get_data.name ) {
+ case get_bootloader:
+ value = info.bootloaderVersion;
+ message = "Bootloader Version";
+ break;
+ case get_ID1:
+ value = info.bootID1;
+ message = "Device boot ID 1";
+ break;
+ case get_ID2:
+ value = info.bootID2;
+ message = "Device boot ID 2";
+ break;
+ case get_BSB:
+ value = info.bsb;
+ message = "Boot Status Byte";
+ if( device_8051 != args.device_type ) {
+ controller_error = 1;
+ }
+ break;
+ case get_SBV:
+ value = info.sbv;
+ message = "Software Boot Vector";
+ if( device_8051 != args.device_type ) {
+ controller_error = 1;
+ }
+ break;
+ case get_SSB:
+ value = info.ssb;
+ message = "Software Security Byte";
+ if( device_8051 != args.device_type ) {
+ controller_error = 1;
+ }
+ break;
+ case get_EB:
+ value = info.eb;
+ message = "Extra Byte";
+ if( device_8051 != args.device_type ) {
+ controller_error = 1;
+ }
+ break;
+ case get_manufacturer:
+ value = info.manufacturerCode;
+ message = "Manufacturer Code";
+ break;
+ case get_family:
+ value = info.familyCode;
+ message = "Family Code";
+ break;
+ case get_product_name:
+ value = info.productName;
+ message = "Product Name";
+ break;
+ case get_product_rev:
+ value = info.productRevision;
+ message = "Product Revision";
+ break;
+ case get_HSB:
+ value = info.hsb;
+ message = "Hardware Security Byte";
+ break;
+ }
+
+ if( 0 != controller_error ) {
+ fprintf( stderr, "%s requires 8051 based controller\n",
+ message );
+ return -1;
+ }
+
+ if( value < 0 ) {
+ fprintf( stderr, "The requested device info is unavailable.\n" );
+ return -2;
+ }
+
+ fprintf( stdout, "%s%s0x%02x (%d)\n",
+ ((0 == args.quiet) ? message : ""),
+ ((0 == args.quiet) ? ": " : ""),
+ value, value );
+ return 0;
+}
+
+
+static int execute_dump( struct usb_dev_handle *device,
+ int interface,
+ struct programmer_arguments args )
+{
+ int i = 0;
+ char *buffer = NULL;
+
+ buffer = (char *) malloc( (args.memory_size) );
+ if( NULL == buffer ) {
+ fprintf( stderr, "Request for %d bytes of memory failed.\n",
+ args.memory_size );
+ goto error;
+ }
+
+ if( 2 < debug ) {
+ fprintf( stderr, "%s: dump %d bytes\n", __FUNCTION__,
+ args.memory_size );
+ }
+
+ if( args.memory_size != atmel_read_flash(device, interface, 0,
+ args.top_memory_address, buffer,
+ args.memory_size) )
+ {
+ fprintf( stderr, "Error while validating.\n" );
+ return -1;
+ }
+
+ for( i = 0; i < args.memory_size; i++ ) {
+ fprintf( stdout, "%c", buffer[i] );
+ }
+
+ fflush( stdout );
+
+error:
+ if( NULL != buffer ) {
+ free( buffer );
+ buffer = NULL;
+ }
+
+ return 0;
+}
+
+
+static int execute_configure( struct usb_dev_handle *device,
+ int interface,
+ struct programmer_arguments args )
+{
+ int value = args.com_configure_data.value;
+ int name = args.com_configure_data.name;
+
+ if( device_8051 != args.device_type ) {
+ fprintf( stderr, "target doesn't support configure operation.\n" );
+ return -1;
+ }
+
+ if( (0xff & value) != value ) {
+ fprintf( stderr, "Value to configure must be in range 0-255.\n" );
+ return -1;
+ }
+
+ if( 0 != atmel_set_config(device, interface, name, value) )
+ {
+ fprintf( stderr, "Configuration set failed.\n" );
+ return -1;
+ }
+
+ return 0;
+}
+
+
+static int execute_dnload( struct usb_dev_handle *device,
+ int interface,
+ struct programmer_arguments args )
+{
+ if( device_SAM7 != args.device_type || device_NEO1973 != args.device_type) {
+ fprintf( stderr, "target doesn't support dfu download operation.\n" );
+ return -1;
+ }
+
+ if ( 0 > sam7dfu_do_dnloa(device, interface, 256, /* FIXME */
+ args.com_flash_data.file) )
+ {
+ fprintf( stderr, "Download failed.\n" );
+ return -1;
+ }
+}
+
+
+static int execute_upload( struct usb_dev_handle *device,
+ int interface,
+ struct programmer_arguments args )
+{
+ if( device_SAM7 != args.device_type || device_NEO1973 != args.device_type) {
+ fprintf( stderr, "target doesn't support dfu upload operation.\n" );
+ return -1;
+ }
+
+ if ( 0 > sam7dfu_do_upload(device, interface, 256, /* FIXME */
+ args.com_flash_data.file) )
+ {
+ fprintf( stderr, "Download failed.\n" );
+ return -1;
+ }
+}
+
+
+int execute_command( struct usb_dev_handle *device,
+ int interface,
+ struct programmer_arguments args )
+{
+ switch( args.command ) {
+ case com_erase:
+ return execute_erase( device, interface, args );
+ case com_flash:
+ return execute_flash( device, interface, args );
+ case com_start_app:
+ return execute_start_app( device, interface, args );
+ case com_get:
+ return execute_get( device, interface, args );
+ case com_dump:
+ return execute_dump( device, interface, args );
+ case com_configure:
+ return execute_configure( device, interface, args );
+ case com_dnload:
+ return execute_dnload( device, interface, args );
+ case com_upload:
+ return execute_upload( device, interface, args );
+ default:
+ fprintf( stderr, "Not supported at this time.\n" );
+ }
+
+ return -1;
+}
diff --git a/src/commands.h b/src/commands.h
new file mode 100644
index 0000000..1d156e3
--- /dev/null
+++ b/src/commands.h
@@ -0,0 +1,30 @@
+/*
+ * dfu-programmer
+ *
+ * $Id: commands.h,v 1.2 2005/09/25 01:27:42 schmidtw Exp $
+ *
+ * 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ */
+
+#ifndef __COMMANDS_H__
+#define __COMMANDS_H__
+
+#include <usb.h>
+#include "arguments.h"
+
+int execute_command( struct usb_dev_handle *device,
+ int interface,
+ struct programmer_arguments args );
+#endif
diff --git a/src/dfu.c b/src/dfu.c
new file mode 100644
index 0000000..c0b4f2b
--- /dev/null
+++ b/src/dfu.c
@@ -0,0 +1,420 @@
+/*
+ * dfu-programmer
+ *
+ * $Id: dfu.c,v 1.3 2006/06/20 06:28:04 schmidtw Exp $
+ *
+ * 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ */
+
+#include <stdio.h>
+#include <usb.h>
+#include "dfu.h"
+
+/* 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
+
+#define INVALID_DFU_TIMEOUT -1
+
+static int dfu_timeout = INVALID_DFU_TIMEOUT;
+static unsigned short transaction = 0;
+
+static int dfu_debug_level = 0;
+
+void dfu_init( const int timeout )
+{
+ if( timeout > 0 ) {
+ dfu_timeout = timeout;
+ } else {
+ if( 0 != dfu_debug_level )
+ fprintf( stderr, "dfu_init: Invalid timeout value.\n" );
+ }
+}
+
+static int dfu_verify_init( const char *function )
+{
+ if( INVALID_DFU_TIMEOUT == dfu_timeout ) {
+ if( 0 != dfu_debug_level )
+ fprintf( stderr,
+ "%s: dfu system not property initialized.\n",
+ function );
+ return -1;
+ }
+
+ return 0;
+}
+
+void dfu_debug( const int level )
+{
+ dfu_debug_level = level;
+}
+
+
+/*
+ * DFU_DETACH Request (DFU Spec 1.0, Section 5.1)
+ *
+ * device - the usb_dev_handle to communicate with
+ * interface - the interface to communicate with
+ * timeout - the timeout in ms the USB device should wait for a pending
+ * USB reset before giving up and terminating the operation
+ *
+ * returns 0 or < 0 on error
+ */
+int dfu_detach( struct usb_dev_handle *device,
+ const unsigned short interface,
+ const unsigned short timeout )
+{
+ if( 0 != dfu_verify_init(__FUNCTION__) )
+ return -1;
+
+ return usb_control_msg( device,
+ /* bmRequestType */ USB_ENDPOINT_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
+ /* bRequest */ DFU_DETACH,
+ /* wValue */ timeout,
+ /* wIndex */ interface,
+ /* Data */ NULL,
+ /* wLength */ 0,
+ dfu_timeout );
+}
+
+
+/*
+ * DFU_DNLOAD Request (DFU Spec 1.0, Section 6.1.1)
+ *
+ * device - the usb_dev_handle to communicate with
+ * interface - the interface to communicate with
+ * length - the total number of bytes to transfer to the USB
+ * device - must be less than wTransferSize
+ * data - the data to transfer
+ *
+ * returns the number of bytes written or < 0 on error
+ */
+int dfu_download( struct usb_dev_handle *device,
+ const unsigned short interface,
+ const unsigned short length,
+ char* data )
+{
+ int status;
+
+ if( 0 != dfu_verify_init(__FUNCTION__) )
+ return -1;
+
+ /* Sanity checks */
+ if( (0 != length) && (NULL == data) ) {
+ if( 0 != dfu_debug_level )
+ fprintf( stderr,
+ "%s: data was NULL, but length != 0\n",
+ __FUNCTION__ );
+ return -1;
+ }
+
+ if( (0 == length) && (NULL != data) ) {
+ if( 0 != dfu_debug_level )
+ fprintf( stderr,
+ "%s: data was not NULL, but length == 0\n",
+ __FUNCTION__ );
+ return -2;
+ }
+
+ status = usb_control_msg( device,
+ /* bmRequestType */ USB_ENDPOINT_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
+ /* bRequest */ DFU_DNLOAD,
+ /* wValue */ transaction++,
+ /* wIndex */ interface,
+ /* Data */ data,
+ /* wLength */ length,
+ dfu_timeout );
+ if( status < 0 ) {
+ fprintf( stderr, "%s error %d\n", __FUNCTION__, status );
+ }
+
+ return status;
+}
+
+
+/*
+ * DFU_UPLOAD Request (DFU Spec 1.0, Section 6.2)
+ *
+ * device - the usb_dev_handle to communicate with
+ * interface - the interface to communicate with
+ * length - the maximum number of bytes to receive from the USB
+ * device - must be less than wTransferSize
+ * data - the buffer to put the received data in
+ *
+ * returns the number of bytes received or < 0 on error
+ */
+int dfu_upload( struct usb_dev_handle *device,
+ const unsigned short interface,
+ const unsigned short length,
+ char* data )
+{
+ int status;
+
+ if( 0 != dfu_verify_init(__FUNCTION__) )
+ return -1;
+
+ /* Sanity checks */
+ if( (0 == length) || (NULL == data) ) {
+ if( 0 != dfu_debug_level )
+ fprintf( stderr,
+ "%s: data was NULL, or length is 0\n",
+ __FUNCTION__ );
+ return -1;
+ }
+
+ status = usb_control_msg( device,
+ /* bmRequestType */ USB_ENDPOINT_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
+ /* bRequest */ DFU_UPLOAD,
+ /* wValue */ transaction++,
+ /* wIndex */ interface,
+ /* Data */ data,
+ /* wLength */ length,
+ dfu_timeout );
+ if( status < 0 ) {
+ fprintf( stderr, "%s error %d\n", __FUNCTION__, status );
+ }
+
+ return status;
+}
+
+
+/*
+ * DFU_GETSTATUS Request (DFU Spec 1.0, Section 6.1.2)
+ *
+ * device - the usb_dev_handle to communicate with
+ * interface - the interface to communicate with
+ * status - the data structure to be populated with the results
+ *
+ * return the number of bytes read in or < 0 on an error
+ */
+int dfu_get_status( struct usb_dev_handle *device,
+ const unsigned short interface,
+ struct dfu_status *status )
+{
+ char buffer[6];
+ int result;
+
+ if( 0 != dfu_verify_init(__FUNCTION__) )
+ return -1;
+
+ /* Initialize the status data structure */
+ status->bStatus = DFU_STATUS_ERROR_UNKNOWN;
+ status->bwPollTimeout = 0;
+ status->bState = STATE_DFU_ERROR;
+ status->iString = 0;
+
+ result = usb_control_msg( device,
+ /* bmRequestType */ USB_ENDPOINT_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
+ /* bRequest */ DFU_GETSTATUS,
+ /* wValue */ 0,
+ /* wIndex */ interface,
+ /* Data */ buffer,
+ /* wLength */ 6,
+ dfu_timeout );
+
+ if( 6 == result ) {
+ status->bStatus = buffer[0];
+ status->bwPollTimeout = ((0xff & buffer[3]) << 16) |
+ ((0xff & buffer[2]) << 8) |
+ (0xff & buffer[1]);
+
+ status->bState = buffer[4];
+ status->iString = buffer[5];
+ }
+
+ return result;
+}
+
+
+/*
+ * DFU_CLRSTATUS Request (DFU Spec 1.0, Section 6.1.3)
+ *
+ * device - the usb_dev_handle to communicate with
+ * interface - the interface to communicate with
+ *
+ * return 0 or < 0 on an error
+ */
+int dfu_clear_status( struct usb_dev_handle *device,
+ const unsigned short interface )
+{
+ if( 0 != dfu_verify_init(__FUNCTION__) )
+ return -1;
+
+ return usb_control_msg( device,
+ /* bmRequestType */ USB_ENDPOINT_OUT| USB_TYPE_CLASS | USB_RECIP_INTERFACE,
+ /* bRequest */ DFU_CLRSTATUS,
+ /* wValue */ 0,
+ /* wIndex */ interface,
+ /* Data */ NULL,
+ /* wLength */ 0,
+ dfu_timeout );
+}
+
+
+/*
+ * DFU_GETSTATE Request (DFU Spec 1.0, Section 6.1.5)
+ *
+ * device - the usb_dev_handle to communicate with
+ * interface - the interface to communicate with
+ * length - the maximum number of bytes to receive from the USB
+ * device - must be less than wTransferSize
+ * data - the buffer to put the received data in
+ *
+ * returns the state or < 0 on error
+ */
+int dfu_get_state( struct usb_dev_handle *device,
+ const unsigned short interface )
+{
+ int result;
+ char buffer[1];
+
+ if( 0 != dfu_verify_init(__FUNCTION__) )
+ return -1;
+
+ result = usb_control_msg( device,
+ /* bmRequestType */ USB_ENDPOINT_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
+ /* bRequest */ DFU_GETSTATE,
+ /* wValue */ 0,
+ /* wIndex */ interface,
+ /* Data */ buffer,
+ /* wLength */ 1,
+ dfu_timeout );
+
+ /* Return the error if there is one. */
+ if( result < 1 ) {
+ return result;
+ }
+
+ /* Return the state. */
+ return buffer[0];
+}
+
+
+/*
+ * DFU_ABORT Request (DFU Spec 1.0, Section 6.1.4)
+ *
+ * device - the usb_dev_handle to communicate with
+ * interface - the interface to communicate with
+ *
+ * returns 0 or < 0 on an error
+ */
+int dfu_abort( struct usb_dev_handle *device,
+ const unsigned short interface )
+{
+ if( 0 != dfu_verify_init(__FUNCTION__) )
+ return -1;
+
+ return usb_control_msg( device,
+ /* bmRequestType */ USB_ENDPOINT_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
+ /* bRequest */ DFU_ABORT,
+ /* wValue */ 0,
+ /* wIndex */ interface,
+ /* Data */ NULL,
+ /* wLength */ 0,
+ dfu_timeout );
+}
+
+
+char* dfu_state_to_string( int state )
+{
+ char *message = NULL;
+
+ switch( state ) {
+ case STATE_APP_IDLE:
+ message = "appIDLE";
+ break;
+ case STATE_APP_DETACH:
+ message = "appDETACH";
+ break;
+ case STATE_DFU_IDLE:
+ message = "dfuIDLE";
+ break;
+ case STATE_DFU_DOWNLOAD_SYNC:
+ message = "dfuDNLOAD-SYNC";
+ break;
+ case STATE_DFU_DOWNLOAD_BUSY:
+ message = "dfuDNBUSY";
+ break;
+ case STATE_DFU_DOWNLOAD_IDLE:
+ message = "dfuDNLOAD-IDLE";
+ break;
+ case STATE_DFU_MANIFEST_SYNC:
+ message = "dfuMANIFEST-SYNC";
+ break;
+ case STATE_DFU_MANIFEST:
+ message = "dfuMANIFEST";
+ break;
+ case STATE_DFU_MANIFEST_WAIT_RESET:
+ message = "dfuMANIFEST-WAIT-RESET";
+ break;
+ case STATE_DFU_UPLOAD_IDLE:
+ message = "dfuUPLOAD-IDLE";
+ break;
+ case STATE_DFU_ERROR:
+ message = "dfuERROR";
+ break;
+ }
+
+ return message;
+}
+
+/* Chapter 6.1.2 */
+static const char *dfu_status_names[] = {
+ [DFU_STATUS_OK] = "No error condition is present",
+ [DFU_STATUS_errTARGET] =
+ "File is not targeted for use by this device",
+ [DFU_STATUS_errFILE] =
+ "File is for this device but fails some vendor-specific test",
+ [DFU_STATUS_errWRITE] =
+ "Device is unable to write memory",
+ [DFU_STATUS_errERASE] =
+ "Memory erase function failed",
+ [DFU_STATUS_errCHECK_ERASED] =
+ "Memory erase check failed",
+ [DFU_STATUS_errPROG] =
+ "Program memory function failed",
+ [DFU_STATUS_errVERIFY] =
+ "Programmed emmory failed verification",
+ [DFU_STATUS_errADDRESS] =
+ "Cannot program memory due to received address that is out of range",
+ [DFU_STATUS_errNOTDONE] =
+ "Received DFU_DNLOAD with wLength = 0, but device does not think that it has all data yet",
+ [DFU_STATUS_errFIRMWARE] =
+ "Device's firmware is corrupt. It cannot return to run-time (non-DFU) operations",
+ [DFU_STATUS_errVENDOR] =
+ "iString indicates a vendor specific error",
+ [DFU_STATUS_errUSBR] =
+ "Device detected unexpected USB reset signalling",
+ [DFU_STATUS_errPOR] =
+ "Device detected unexpected power on reset",
+ [DFU_STATUS_errUNKNOWN] =
+ "Something went wrong, but the device does not know what it was",
+ [DFU_STATUS_errSTALLEDPKT] =
+ "Device stalled an unexpected request",
+};
+
+
+const char *dfu_status_to_string(int status)
+{
+ if (status > DFU_STATUS_errSTALLEDPKT)
+ return "INVALID";
+ return dfu_status_names[status];
+}
+
diff --git a/src/dfu.h b/src/dfu.h
new file mode 100644
index 0000000..cd4b06c
--- /dev/null
+++ b/src/dfu.h
@@ -0,0 +1,103 @@
+/*
+ * dfu-programmer
+ *
+ * $Id: dfu.h,v 1.2 2005/09/25 01:27:42 schmidtw Exp $
+ *
+ * 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ */
+
+#ifndef __DFU_H__
+#define __DFU_H__
+
+#include <usb.h>
+#include "usb_dfu.h"
+
+/* DFU states */
+#define STATE_APP_IDLE 0x00
+#define STATE_APP_DETACH 0x01
+#define STATE_DFU_IDLE 0x02
+#define STATE_DFU_DOWNLOAD_SYNC 0x03
+#define STATE_DFU_DOWNLOAD_BUSY 0x04
+#define STATE_DFU_DOWNLOAD_IDLE 0x05
+#define STATE_DFU_MANIFEST_SYNC 0x06
+#define STATE_DFU_MANIFEST 0x07
+#define STATE_DFU_MANIFEST_WAIT_RESET 0x08
+#define STATE_DFU_UPLOAD_IDLE 0x09
+#define STATE_DFU_ERROR 0x0a
+
+
+/* DFU status */
+#define DFU_STATUS_OK 0x00
+#define DFU_STATUS_ERROR_TARGET 0x01
+#define DFU_STATUS_ERROR_FILE 0x02
+#define DFU_STATUS_ERROR_WRITE 0x03
+#define DFU_STATUS_ERROR_ERASE 0x04
+#define DFU_STATUS_ERROR_CHECK_ERASED 0x05
+#define DFU_STATUS_ERROR_PROG 0x06
+#define DFU_STATUS_ERROR_VERIFY 0x07
+#define DFU_STATUS_ERROR_ADDRESS 0x08
+#define DFU_STATUS_ERROR_NOTDONE 0x09
+#define DFU_STATUS_ERROR_FIRMWARE 0x0a
+#define DFU_STATUS_ERROR_VENDOR 0x0b
+#define DFU_STATUS_ERROR_USBR 0x0c
+#define DFU_STATUS_ERROR_POR 0x0d
+#define DFU_STATUS_ERROR_UNKNOWN 0x0e
+#define DFU_STATUS_ERROR_STALLEDPKT 0x0f
+
+
+/* This is based off of DFU_GETSTATUS
+ *
+ * 1 unsigned byte bStatus
+ * 3 unsigned byte bwPollTimeout
+ * 1 unsigned byte bState
+ * 1 unsigned byte iString
+*/
+
+struct dfu_status {
+ unsigned char bStatus;
+ unsigned int bwPollTimeout;
+ unsigned char bState;
+ unsigned char iString;
+};
+
+void dfu_init( const int timeout );
+void dfu_debug( const int level );
+int dfu_detach( struct usb_dev_handle *device,
+ const unsigned short interface,
+ const unsigned short timeout );
+int dfu_download( struct usb_dev_handle *device,
+ const unsigned short interface,
+ const unsigned short length,
+ char* data );
+int dfu_upload( struct usb_dev_handle *device,
+ const unsigned short interface,
+ const unsigned short length,
+ char* data );
+int dfu_get_status( struct usb_dev_handle *device,
+ const unsigned short interface,
+ struct dfu_status *status );
+int dfu_clear_status( struct usb_dev_handle *device,
+ const unsigned short interface );
+int dfu_get_state( struct usb_dev_handle *device,
+ const unsigned short interface );
+int dfu_abort( struct usb_dev_handle *device,
+ const unsigned short interface );
+
+char* dfu_state_to_string( int state );
+
+const char *dfu_status_to_string(int status);
+
+int debug;
+#endif
diff --git a/src/main.c b/src/main.c
new file mode 100644
index 0000000..c6836ca
--- /dev/null
+++ b/src/main.c
@@ -0,0 +1,821 @@
+/*
+ * dfu-util
+ *
+ * (C) 2007 by OpenMoko, Inc.
+ * Written by Harald Welte <laforge@openmoko.org>
+ *
+ * Based on existing code of dfu-programmer-0.4
+ *
+ * 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ */
+
+#include <stdio.h>
+#include <string.h>
+#include <getopt.h>
+#include <usb.h>
+#include <errno.h>
+#include <byteswap.h>
+#include <endian.h>
+
+#include "dfu.h"
+#include "usb_dfu.h"
+#include "sam7dfu.h"
+#include "dfu-version.h"
+#ifdef HAVE_CONFIG_H
+#include "config.h"
+#endif
+
+#ifdef HAVE_USBPATH_H
+#include <usbpath.h>
+#endif
+
+/* define a portable macro for swapping a 16bit word */
+#if defined(WORDS_BIGENDIAN)
+# if defined(__APPLE__) && defined (OSX)
+# include <libkern/OSByteOrder.h>
+# define LE2CPU16(x) OSSwapInt16(x)
+# else
+# define LE2CPU16(x) bswap_16(x)
+# endif
+#else
+# define LE2CPU16(x) (x)
+#endif
+
+int debug;
+static int verbose = 0;
+
+#define DFU_IFF_DFU 0x0001 /* DFU Mode, (not Runtime) */
+#define DFU_IFF_VENDOR 0x0100
+#define DFU_IFF_PRODUCT 0x0200
+#define DFU_IFF_CONFIG 0x0400
+#define DFU_IFF_IFACE 0x0800
+#define DFU_IFF_ALT 0x1000
+#define DFU_IFF_DEVNUM 0x2000
+#define DFU_IFF_PATH 0x4000
+
+struct usb_vendprod {
+ u_int16_t vendor;
+ u_int16_t product;
+};
+
+struct dfu_if {
+ u_int16_t vendor;
+ u_int16_t product;
+ u_int8_t configuration;
+ u_int8_t interface;
+ u_int8_t altsetting;
+ int bus;
+ u_int8_t devnum;
+ const char *path;
+ unsigned int flags;
+ struct usb_device *dev;
+
+ struct usb_dev_handle *dev_handle;
+};
+
+static int _get_first_cb(struct dfu_if *dif, void *v)
+{
+ struct dfu_if *v_dif = v;
+
+ memcpy(v_dif, dif, sizeof(*v_dif)-sizeof(struct usb_dev_handle *));
+
+ /* return a value that makes find_dfu_if return immediately */
+ return 1;
+}
+
+/* Find a DFU interface (and altsetting) in a given device */
+static int find_dfu_if(struct usb_device *dev, int (*handler)(struct dfu_if *, void *), void *v)
+{
+ struct usb_config_descriptor *cfg;
+ struct usb_interface_descriptor *intf;
+ struct usb_interface *uif;
+ struct dfu_if _dif, *dfu_if = &_dif;
+ int cfg_idx, intf_idx, alt_idx;
+ int rc;
+
+ memset(dfu_if, 0, sizeof(*dfu_if));
+
+ for (cfg_idx = 0; cfg_idx < dev->descriptor.bNumConfigurations;
+ cfg_idx++) {
+ cfg = &dev->config[cfg_idx];
+ /* in some cases, noticably FreeBSD if uid != 0,
+ * the configuration descriptors are empty */
+ if (!cfg)
+ return 0;
+ for (intf_idx = 0; intf_idx < cfg->bNumInterfaces;
+ intf_idx++) {
+ uif = &cfg->interface[intf_idx];
+ if (!uif)
+ return 0;
+ for (alt_idx = 0;
+ alt_idx < uif->num_altsetting; alt_idx++) {
+ intf = &uif->altsetting[alt_idx];
+ if (!intf)
+ return 0;
+ if (intf->bInterfaceClass == 0xfe &&
+ intf->bInterfaceSubClass == 1) {
+ dfu_if->dev = dev;
+ dfu_if->vendor =
+ dev->descriptor.idVendor;
+ dfu_if->product =
+ dev->descriptor.idProduct;
+ dfu_if->configuration = cfg_idx;
+ dfu_if->interface =
+ intf->bInterfaceNumber;
+ dfu_if->altsetting =
+ intf->bAlternateSetting;
+ if (intf->bInterfaceProtocol == 2)
+ dfu_if->flags |=
+ DFU_IFF_DFU;
+ else
+ dfu_if->flags &=
+ ~DFU_IFF_DFU;
+ if (!handler)
+ return 1;
+ rc = handler(dfu_if, v);
+ if (rc != 0)
+ return rc;
+ }
+ }
+ }
+ }
+
+ return 0;
+}
+
+static int get_first_dfu_if(struct dfu_if *dif)
+{
+ return find_dfu_if(dif->dev, &_get_first_cb, (void *) dif);
+}
+
+#define MAX_STR_LEN 64
+
+static int print_dfu_if(struct dfu_if *dfu_if, void *v)
+{
+ struct usb_device *dev = dfu_if->dev;
+ int if_name_str_idx;
+ char name[MAX_STR_LEN+1] = "UNDEFINED";
+
+ if_name_str_idx = dev->config[dfu_if->configuration].interface[dfu_if->interface].altsetting[dfu_if->altsetting].iInterface;
+ if (if_name_str_idx) {
+ if (!dfu_if->dev_handle)
+ dfu_if->dev_handle = usb_open(dfu_if->dev);
+ if (dfu_if->dev_handle)
+ usb_get_string_simple(dfu_if->dev_handle, if_name_str_idx, name, MAX_STR_LEN);
+ }
+
+ printf("Found %s: [0x%04x:0x%04x] devnum=%u, cfg=%u, intf=%u, alt=%u, name=\"%s\"\n",
+ dfu_if->flags & DFU_IFF_DFU ? "DFU" : "Runtime",
+ dev->descriptor.idVendor, dev->descriptor.idProduct,
+ dev->devnum, dfu_if->configuration, dfu_if->interface,
+ dfu_if->altsetting, name);
+
+ return 0;
+}
+
+static int alt_by_name(struct dfu_if *dfu_if, void *v)
+{
+ struct usb_device *dev = dfu_if->dev;
+ int if_name_str_idx;
+ char name[MAX_STR_LEN+1] = "UNDEFINED";
+
+ if_name_str_idx =
+ dev->config[dfu_if->configuration].interface[dfu_if->interface].
+ altsetting[dfu_if->altsetting].iInterface;
+ if (!if_name_str_idx)
+ return 0;
+ if (!dfu_if->dev_handle)
+ dfu_if->dev_handle = usb_open(dfu_if->dev);
+ if (!dfu_if->dev_handle)
+ return 0;
+ if (usb_get_string_simple(dfu_if->dev_handle, if_name_str_idx, name,
+ MAX_STR_LEN) < 0)
+ return 0; /* should we return an error here ? */
+ if (strcmp(name, v))
+ return 0;
+ /*
+ * Return altsetting+1 so that we can use return value 0 to indicate
+ * "not found".
+ */
+ return dfu_if->altsetting+1;
+}
+
+static int _count_cb(struct dfu_if *dif, void *v)
+{
+ int *count = v;
+
+ (*count)++;
+
+ return 0;
+}
+
+/* Count DFU interfaces within a single device */
+static int count_dfu_interfaces(struct usb_device *dev)
+{
+ int num_found = 0;
+
+ find_dfu_if(dev, &_count_cb, (void *) &num_found);
+
+ return num_found;
+}
+
+
+/* Iterate over all matching DFU capable devices within system */
+static int iterate_dfu_devices(struct dfu_if *dif,
+ int (*action)(struct usb_device *dev, void *user), void *user)
+{
+ struct usb_bus *usb_bus;
+ struct usb_device *dev;
+
+ /* Walk the tree and find our device. */
+ for (usb_bus = usb_get_busses(); NULL != usb_bus;
+ usb_bus = usb_bus->next) {
+ for (dev = usb_bus->devices; NULL != dev; dev = dev->next) {
+ int retval;
+
+ if (dif && (dif->flags &
+ (DFU_IFF_VENDOR|DFU_IFF_PRODUCT)) &&
+ (dev->descriptor.idVendor != dif->vendor ||
+ dev->descriptor.idProduct != dif->product))
+ continue;
+ if (dif && (dif->flags & DFU_IFF_DEVNUM) &&
+ (atoi(usb_bus->dirname) != dif->bus ||
+ dev->devnum != dif->devnum))
+ continue;
+ if (!count_dfu_interfaces(dev))
+ continue;
+
+ retval = action(dev, user);
+ if (retval)
+ return retval;
+ }
+ }
+ return 0;
+}
+
+
+static int found_dfu_device(struct usb_device *dev, void *user)
+{
+ struct dfu_if *dif = user;
+
+ dif->dev = dev;
+ return 1;
+}
+
+
+/* Find the first DFU-capable device, save it in dfu_if->dev */
+static int get_first_dfu_device(struct dfu_if *dif)
+{
+ return iterate_dfu_devices(dif, found_dfu_device, dif);
+}
+
+
+static int count_one_dfu_device(struct usb_device *dev, void *user)
+{
+ int *num = user;
+
+ (*num)++;
+ return 0;
+}
+
+
+/* Count DFU capable devices within system */
+static int count_dfu_devices(struct dfu_if *dif)
+{
+ int num_found = 0;
+
+ iterate_dfu_devices(dif, count_one_dfu_device, &num_found);
+ return num_found;
+}
+
+
+static int list_dfu_interfaces(void)
+{
+ struct usb_bus *usb_bus;
+ struct usb_device *dev;
+
+ /* Walk the tree and find our device. */
+ for (usb_bus = usb_get_busses(); NULL != usb_bus;
+ usb_bus = usb_bus->next ) {
+ for (dev = usb_bus->devices; NULL != dev; dev = dev->next) {
+ find_dfu_if(dev, &print_dfu_if, NULL);
+ }
+ }
+ return 0;
+}
+
+static int parse_vendprod(struct usb_vendprod *vp, const char *str)
+{
+ unsigned long vend, prod;
+ const char *colon;
+
+ colon = strchr(str, ':');
+ if (!colon || strlen(colon) < 2)
+ return -EINVAL;
+
+ vend = strtoul(str, NULL, 16);
+ prod = strtoul(colon+1, NULL, 16);
+
+ if (vend > 0xffff || prod > 0xffff)
+ return -EINVAL;
+
+ vp->vendor = vend;
+ vp->product = prod;
+
+ return 0;
+}
+
+
+#ifdef HAVE_USBPATH_H
+
+static int resolve_device_path(struct dfu_if *dif)
+{
+ int res;
+
+ res = usb_path2devnum(dif->path);
+ if (res < 0)
+ return -EINVAL;
+ if (!res)
+ return 0;
+
+ dif->bus = atoi(dif->path);
+ dif->devnum = res;
+ dif->flags |= DFU_IFF_DEVNUM;
+ return res;
+}
+
+#else /* HAVE_USBPATH_H */
+
+static int resolve_device_path(struct dfu_if *dif)
+{
+ fprintf(stderr,
+ "USB device paths are not supported by this dfu-util.\n");
+ exit(1);
+}
+
+#endif /* !HAVE_USBPATH_H */
+
+
+static void help(void)
+{
+ printf("Usage: dfu-util [options] ...\n"
+ " -h --help\t\t\tPrint this help message\n"
+ " -V --version\t\t\tPrint the version number\n"
+ " -l --list\t\t\tList the currently attached DFU capable USB devices\n"
+ " -d --device vendor:product\tSpecify Vendor/Product ID of DFU device\n"
+ " -p --path bus-port. ... .port\tSpecify path to DFU device\n"
+ " -c --cfg config_nr\t\tSpecify the Configuration of DFU device\n"
+ " -i --intf intf_nr\t\tSpecify the DFU Interface number\n"
+ " -a --alt alt\t\t\tSpecify the Altsetting of the DFU Interface\n"
+ "\t\t\t\tby name or by number\n"
+ " -t --transfer-size\t\tSpecify the number of bytes per USB Transfer\n"
+ " -U --upload file\t\tRead firmware from device into <file>\n"
+ " -D --download file\t\tWrite firmware from <file> into device\n"
+ " -R --reset\t\t\tIssue USB Reset signalling once we're finished\n"
+ );
+}
+
+static void print_version(void)
+{
+ printf("dfu-util version %s\n", VERSION "+svn" DFU_UTIL_VERSION);
+}
+
+static struct option opts[] = {
+ { "help", 0, 0, 'h' },
+ { "version", 0, 0, 'V' },
+ { "verbose", 0, 0, 'v' },
+ { "list", 0, 0, 'l' },
+ { "device", 1, 0, 'd' },
+ { "path", 1, 0, 'p' },
+ { "configuration", 1, 0, 'c' },
+ { "cfg", 1, 0, 'c' },
+ { "interface", 1, 0, 'i' },
+ { "intf", 1, 0, 'i' },
+ { "altsetting", 1, 0, 'a' },
+ { "alt", 1, 0, 'a' },
+ { "transfer-size", 1, 0, 't' },
+ { "upload", 1, 0, 'U' },
+ { "download", 1, 0, 'D' },
+ { "reset", 0, 0, 'R' },
+};
+
+enum mode {
+ MODE_NONE,
+ MODE_UPLOAD,
+ MODE_DOWNLOAD,
+};
+
+int main(int argc, char **argv)
+{
+ struct usb_vendprod vendprod;
+ struct dfu_if _rt_dif, _dif, *dif = &_dif;
+ int num_devs;
+ int num_ifs;
+ unsigned int transfer_size = 0;
+ enum mode mode = MODE_NONE;
+ struct dfu_status status;
+ struct usb_dfu_func_descriptor func_dfu;
+ char *filename = NULL;
+ char *alt_name = NULL; /* query alt name if non-NULL */
+ char *end;
+ int final_reset = 0;
+ int page_size = getpagesize();
+ int ret;
+
+ printf("dfu-util - (C) 2007 by OpenMoko Inc.\n"
+ "This program is Free Software and has ABSOLUTELY NO WARRANTY\n\n");
+
+ memset(dif, 0, sizeof(*dif));
+
+ usb_init();
+ //usb_set_debug(255);
+ usb_find_busses();
+ usb_find_devices();
+
+ while (1) {
+ int c, option_index = 0;
+ c = getopt_long(argc, argv, "hVvld:p:c:i:a:t:U:D:R", opts, &option_index);
+ if (c == -1)
+ break;
+
+ switch (c) {
+ case 'h':
+ help();
+ exit(0);
+ break;
+ case 'V':
+ print_version();
+ exit(0);
+ break;
+ case 'v':
+ verbose = 1;
+ break;
+ case 'l':
+ list_dfu_interfaces();
+ exit(0);
+ break;
+ case 'd':
+ /* Parse device */
+ if (parse_vendprod(&vendprod, optarg) < 0) {
+ fprintf(stderr, "unable to parse `%s'\n", optarg);
+ exit(2);
+ }
+ dif->vendor = vendprod.vendor;
+ dif->product = vendprod.product;
+ dif->flags |= (DFU_IFF_VENDOR | DFU_IFF_PRODUCT);
+ break;
+ case 'p':
+ /* Parse device path */
+ dif->path = optarg;
+ dif->flags |= DFU_IFF_PATH;
+ ret = resolve_device_path(dif);
+ if (ret < 0) {
+ fprintf(stderr, "unable to parse `%s'\n",
+ optarg);
+ exit(2);
+ }
+ if (!ret) {
+ fprintf(stderr, "cannot find `%s'\n", optarg);
+ exit(1);
+ }
+ break;
+ case 'c':
+ /* Configuration */
+ dif->configuration = atoi(optarg);
+ dif->flags |= DFU_IFF_CONFIG;
+ break;
+ case 'i':
+ /* Interface */
+ dif->interface = atoi(optarg);
+ dif->flags |= DFU_IFF_IFACE;
+ break;
+ case 'a':
+ /* Interface Alternate Setting */
+ dif->altsetting = strtoul(optarg, &end, 0);
+ if (*end)
+ alt_name = optarg;
+ dif->flags |= DFU_IFF_ALT;
+ break;
+ case 't':
+ transfer_size = atoi(optarg);
+ break;
+ case 'U':
+ mode = MODE_UPLOAD;
+ filename = optarg;
+ break;
+ case 'D':
+ mode = MODE_DOWNLOAD;
+ filename = optarg;
+ break;
+ case 'R':
+ final_reset = 1;
+ break;
+ default:
+ help();
+ exit(2);
+ }
+ }
+
+ if (mode == MODE_NONE) {
+ fprintf(stderr, "You need to specify one of -D or -U\n");
+ help();
+ exit(2);
+ }
+
+ if (!filename) {
+ fprintf(stderr, "You need to specify a filename to -D -r -U\n");
+ help();
+ exit(2);
+ }
+
+ dfu_init(5000);
+
+ num_devs = count_dfu_devices(dif);
+ if (num_devs == 0) {
+ fprintf(stderr, "No DFU capable USB device found\n");
+ exit(1);
+ } else if (num_devs > 1) {
+ /* We cannot safely support more than one DFU capable device
+ * with same vendor/product ID, since during DFU we need to do
+ * a USB bus reset, after which the target device will get a
+ * new address */
+ fprintf(stderr, "More than one DFU capable USB device found, "
+ "you might try `--list' and then disconnect all but one "
+ "device\n");
+ exit(3);
+ }
+ if (!get_first_dfu_device(dif))
+ exit(3);
+
+ /* We have exactly one device. It's usb_device is now in dif->dev */
+
+ printf("Opening USB Device 0x%04x:0x%04x...\n", dif->vendor, dif->product);
+ dif->dev_handle = usb_open(dif->dev);
+ if (!dif->dev_handle) {
+ fprintf(stderr, "Cannot open device: %s\n", usb_strerror());
+ exit(1);
+ }
+
+ /* try to find first DFU interface of device */
+ memcpy(&_rt_dif, dif, sizeof(_rt_dif));
+ if (!get_first_dfu_if(&_rt_dif))
+ exit(1);
+
+ if (!_rt_dif.flags & DFU_IFF_DFU) {
+ /* In the 'first round' during runtime mode, there can only be one
+ * DFU Interface descriptor according to the DFU Spec. */
+
+ /* FIXME: check if the selected device really has only one */
+
+ printf("Claiming USB DFU Runtime Interface...\n");
+ if (usb_claim_interface(_rt_dif.dev_handle, _rt_dif.interface) < 0) {
+ fprintf(stderr, "Cannot claim interface: %s\n", usb_strerror());
+ exit(1);
+ }
+
+ printf("Determining device status: ");
+ if (dfu_get_status(_rt_dif.dev_handle, _rt_dif.interface, &status ) < 0) {
+ fprintf(stderr, "error get_status: %s\n", usb_strerror());
+ exit(1);
+ }
+ printf("state = %s, status = %d\n", dfu_state_to_string(status.bState), status.bStatus);
+
+ switch (status.bState) {
+ case DFU_STATE_appIDLE:
+ case DFU_STATE_appDETACH:
+ printf("Device really in Runtime Mode, send DFU detach request...\n");
+ if (dfu_detach(_rt_dif.dev_handle, _rt_dif.interface, 1000) < 0) {
+ fprintf(stderr, "error detaching: %s\n", usb_strerror());
+ exit(1);
+ break;
+ }
+ printf("Resetting USB...\n");
+ ret = usb_reset(_rt_dif.dev_handle);
+ if (ret < 0 && ret != -ENODEV)
+ fprintf(stderr, "error resetting after detach: %s\n",
+ usb_strerror());
+ sleep(2);
+ break;
+ case DFU_STATE_dfuERROR:
+ printf("dfuERROR, clearing status\n");
+ if (dfu_clear_status(_rt_dif.dev_handle, _rt_dif.interface) < 0) {
+ fprintf(stderr, "error clear_status: %s\n", usb_strerror());
+ exit(1);
+ break;
+ }
+ break;
+ default:
+ fprintf(stderr, "WARNING: Runtime device already in DFU state ?!?\n");
+ goto dfustate;
+ break;
+ }
+
+ /* now we need to re-scan the bus and locate our device */
+ if (usb_find_devices() < 2)
+ printf("not at least 2 device changes found ?!?\n");
+
+ if (dif->flags & DFU_IFF_PATH) {
+ ret = resolve_device_path(dif);
+ if (ret < 0) {
+ fprintf(stderr,
+ "internal error: cannot re-parse `%s'\n",
+ dif->path);
+ abort();
+ }
+ if (!ret) {
+ fprintf(stderr,
+ "Can't resolve path after RESET?\n");
+ exit(1);
+ }
+ }
+
+ num_devs = count_dfu_devices(dif);
+ if (num_devs == 0) {
+ fprintf(stderr, "Lost device after RESET?\n");
+ exit(1);
+ } else if (num_devs > 1) {
+ fprintf(stderr, "More than one DFU capable USB device found, "
+ "you might try `--list' and then disconnect all but one "
+ "device\n");
+ exit(1);
+ }
+ if (!get_first_dfu_device(dif))
+ exit(3);
+
+ printf("Opening USB Device...\n");
+ dif->dev_handle = usb_open(dif->dev);
+ if (!dif->dev_handle) {
+ fprintf(stderr, "Cannot open device: %s\n", usb_strerror());
+ exit(1);
+ }
+ } else {
+ /* we're already in DFU mode, so we can skip the detach/reset
+ * procedure */
+ }
+
+dfustate:
+ if (alt_name) {
+ int n;
+
+ n = find_dfu_if(dif->dev, &alt_by_name, alt_name);
+ if (!n) {
+ fprintf(stderr, "No such Alternate Setting: \"%s\"\n",
+ alt_name);
+ exit(1);
+ }
+ if (n < 0) {
+ fprintf(stderr, "Error %d in name lookup\n", n);
+ exit(1);
+ }
+ dif->altsetting = n-1;
+ }
+
+ print_dfu_if(dif, NULL);
+
+ num_ifs = count_dfu_interfaces(dif->dev);
+ if (num_ifs < 0) {
+ fprintf(stderr, "No DFU Interface after RESET?!?\n");
+ exit(1);
+ } else if (num_ifs == 1) {
+ if (!get_first_dfu_if(dif)) {
+ fprintf(stderr, "Can't find the single available DFU IF\n");
+ exit(1);
+ }
+ } else if (num_ifs > 1 && !dif->flags & (DFU_IFF_IFACE|DFU_IFF_ALT)) {
+ fprintf(stderr, "We have %u DFU Interfaces/Altsettings, "
+ "you have to specify one via --intf / --alt options\n",
+ num_ifs);
+ exit(1);
+ }
+
+#if 0
+ printf("Setting Configuration %u...\n", dif->configuration);
+ if (usb_set_configuration(dif->dev_handle, dif->configuration) < 0) {
+ fprintf(stderr, "Cannot set configuration: %s\n", usb_strerror());
+ exit(1);
+ }
+#endif
+ printf("Claiming USB DFU Interface...\n");
+ if (usb_claim_interface(dif->dev_handle, dif->interface) < 0) {
+ fprintf(stderr, "Cannot claim interface: %s\n", usb_strerror());
+ exit(1);
+ }
+
+ printf("Setting Alternate Setting ...\n");
+ if (usb_set_altinterface(dif->dev_handle, dif->altsetting) < 0) {
+ fprintf(stderr, "Cannot set alternate interface: %s\n",
+ usb_strerror());
+ exit(1);
+ }
+
+status_again:
+ printf("Determining device status: ");
+ if (dfu_get_status(dif->dev_handle, dif->interface, &status ) < 0) {
+ fprintf(stderr, "error get_status: %s\n", usb_strerror());
+ exit(1);
+ }
+ printf("state = %s, status = %d\n", dfu_state_to_string(status.bState), status.bStatus);
+
+ switch (status.bState) {
+ case DFU_STATE_appIDLE:
+ case DFU_STATE_appDETACH:
+ fprintf(stderr, "Device still in Runtime Mode!\n");
+ exit(1);
+ break;
+ case DFU_STATE_dfuERROR:
+ printf("dfuERROR, clearing status\n");
+ if (dfu_clear_status(dif->dev_handle, dif->interface) < 0) {
+ fprintf(stderr, "error clear_status: %s\n", usb_strerror());
+ exit(1);
+ }
+ goto status_again;
+ break;
+ case DFU_STATE_dfuDNLOAD_IDLE:
+ case DFU_STATE_dfuUPLOAD_IDLE:
+ printf("aborting previous incomplete transfer\n");
+ if (dfu_abort(dif->dev_handle, dif->interface) < 0) {
+ fprintf(stderr, "can't send DFU_ABORT: %s\n", usb_strerror());
+ exit(1);
+ }
+ goto status_again;
+ break;
+ case DFU_STATE_dfuIDLE:
+ printf("dfuIDLE, continuing\n");
+ break;
+ }
+
+ if (!transfer_size) {
+ /* Obtain DFU functional descriptor */
+ ret = usb_get_descriptor(dif->dev_handle, 0x21, dif->interface,
+ &func_dfu, sizeof(func_dfu));
+ if (ret < 0) {
+ fprintf(stderr, "Error obtaining DFU functional "
+ "descriptor: %s\n", usb_strerror());
+ transfer_size = page_size;
+ } else {
+ func_dfu.wTransferSize = LE2CPU16(func_dfu.wTransferSize);
+ transfer_size = func_dfu.wTransferSize;
+ }
+ }
+
+ if (transfer_size > page_size)
+ transfer_size = page_size;
+
+ printf("Transfer Size = 0x%04x\n", transfer_size);
+
+ if (DFU_STATUS_OK != status.bStatus ) {
+ printf("WARNING: DFU Status: '%s'\n",
+ dfu_status_to_string(status.bStatus));
+ /* Clear our status & try again. */
+ dfu_clear_status(dif->dev_handle, dif->interface);
+ dfu_get_status(dif->dev_handle, dif->interface, &status);
+
+ if (DFU_STATUS_OK != status.bStatus) {
+ fprintf(stderr, "Error: %d\n", status.bStatus);
+ exit(1);
+ }
+ }
+
+ switch (mode) {
+ case MODE_UPLOAD:
+ if (sam7dfu_do_upload(dif->dev_handle, dif->interface,
+ transfer_size, filename) < 0)
+ exit(1);
+ break;
+ case MODE_DOWNLOAD:
+ if (sam7dfu_do_dnload(dif->dev_handle, dif->interface,
+ transfer_size, filename) < 0)
+ exit(1);
+ break;
+ default:
+ fprintf(stderr, "Unsupported mode: %u\n", mode);
+ exit(1);
+ }
+
+ if (final_reset) {
+ if (dfu_detach(dif->dev_handle, dif->interface, 1000) < 0) {
+ fprintf(stderr, "can't detach: %s\n", usb_strerror());
+ }
+ printf("Resetting USB to switch back to runtime mode\n");
+ ret = usb_reset(dif->dev_handle);
+ if (ret < 0 && ret != -ENODEV) {
+ fprintf(stderr, "error resetting after download: %s\n",
+ usb_strerror());
+ }
+ }
+
+ exit(0);
+}
+
diff --git a/src/sam7dfu.c b/src/sam7dfu.c
new file mode 100644
index 0000000..536b2e1
--- /dev/null
+++ b/src/sam7dfu.c
@@ -0,0 +1,205 @@
+/* This is supposed to be a "real" DFU implementation, just as specified in the
+ * USB DFU 1.0 Spec. Not overloaded like the Atmel one...
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+#include <errno.h>
+#include <string.h>
+#include <usb.h>
+
+#include "config.h"
+#include "dfu.h"
+#include "usb_dfu.h"
+
+/* ugly hack for Win32 */
+#ifndef O_BINARY
+#define O_BINARY 0
+#endif
+
+int sam7dfu_do_upload(struct usb_dev_handle *usb_handle, int interface,
+ int xfer_size, const char *fname)
+{
+ int ret, fd, total_bytes = 0;
+ char *buf = malloc(xfer_size);
+
+ if (!buf)
+ return -ENOMEM;
+
+ fd = creat(fname, 0644);
+ if (fd < 0) {
+ perror(fname);
+ ret = fd;
+ goto out_free;
+ }
+
+ while (1) {
+ int rc, write_rc;
+ rc = dfu_upload(usb_handle, interface, xfer_size, buf);
+ if (rc < 0) {
+ ret = rc;
+ goto out_close;
+ }
+ write_rc = write(fd, buf, rc);
+ if (write_rc < rc) {
+ fprintf(stderr, "Short write: %s\n",
+ strerror(errno));
+ ret = total_bytes;
+ goto out_close;
+ }
+ total_bytes += rc;
+ if (rc < xfer_size) {
+ /* last block, return */
+ ret = total_bytes;
+ goto out_close;
+ }
+ }
+ ret = 0;
+
+out_close:
+ close(fd);
+out_free:
+ free(buf);
+
+ return ret;
+}
+
+#define PROGRESS_BAR_WIDTH 50
+
+int sam7dfu_do_dnload(struct usb_dev_handle *usb_handle, int interface,
+ int xfer_size, const char *fname)
+{
+ int ret, fd, bytes_sent = 0;
+ unsigned int bytes_per_hash, hashes = 0;
+ char *buf = malloc(xfer_size);
+ struct stat st;
+ struct dfu_status dst;
+
+ if (!buf)
+ return -ENOMEM;
+
+ fd = open(fname, O_RDONLY|O_BINARY);
+ if (fd < 0) {
+ perror(fname);
+ ret = fd;
+ goto out_free;
+ }
+
+ ret = fstat(fd, &st);
+ if (ret < 0) {
+ perror(fname);
+ goto out_close;
+ perror(fname);
+ }
+
+ if (st.st_size <= 0 /* + DFU_HDR */) {
+ fprintf(stderr, "File seems a bit too small...\n");
+ ret = -EINVAL;
+ goto out_close;
+ }
+
+ bytes_per_hash = st.st_size / PROGRESS_BAR_WIDTH;
+ if (bytes_per_hash == 0)
+ bytes_per_hash = 1;
+ printf("bytes_per_hash=%u\n", bytes_per_hash);
+#if 0
+ read(fd, DFU_HDR);
+#endif
+ printf("Starting download: [");
+ fflush(stdout);
+ while (bytes_sent < st.st_size /* - DFU_HDR */) {
+ int hashes_todo;
+
+ ret = read(fd, buf, xfer_size);
+ if (ret < 0) {
+ perror(fname);
+ goto out_close;
+ }
+ ret = dfu_download(usb_handle, interface, ret, ret ? buf : NULL);
+ if (ret < 0) {
+ fprintf(stderr, "Error during download\n");
+ goto out_close;
+ }
+ bytes_sent += ret;
+
+ do {
+ ret = dfu_get_status(usb_handle, interface, &dst);
+ if (ret < 0) {
+ fprintf(stderr, "Error during download get_status\n");
+ goto out_close;
+ }
+ usleep(5000);
+ } while (dst.bState != DFU_STATE_dfuDNLOAD_IDLE);
+ if (dst.bStatus != DFU_STATUS_OK) {
+ printf(" failed!\n");
+ printf("state(%u) = %s, status(%u) = %s\n", dst.bState,
+ dfu_state_to_string(dst.bState), dst.bStatus,
+ dfu_status_to_string(dst.bStatus));
+ ret = -1;
+ goto out_close;
+ }
+
+ hashes_todo = (bytes_sent / bytes_per_hash) - hashes;
+ hashes += hashes_todo;
+ while (hashes_todo--)
+ putchar('#');
+ fflush(stdout);
+ }
+
+ /* send one zero sized download request to signalize end */
+ ret = dfu_download(usb_handle, interface, 0, NULL);
+ if (ret >= 0)
+ ret = bytes_sent;
+
+ printf("] finished!\n");
+ fflush(stdout);
+
+get_status:
+ /* Transition to MANIFEST_SYNC state */
+ ret = dfu_get_status(usb_handle, interface, &dst);
+ if (ret < 0) {
+ fprintf(stderr, "unable to read DFU status\n");
+ goto out_close;
+ }
+ printf("state(%u) = %s, status(%u) = %s\n", dst.bState,
+ dfu_state_to_string(dst.bState), dst.bStatus,
+ dfu_status_to_string(dst.bStatus));
+
+ /* FIXME: deal correctly with ManifestationTolerant=0 / WillDetach bits */
+ switch (dst.bState) {
+ case DFU_STATE_dfuMANIFEST_SYNC:
+ case DFU_STATE_dfuMANIFEST:
+ /* some devices (e.g. TAS1020b) need some time before we
+ * can obtain the status */
+ sleep(1);
+ goto get_status;
+ break;
+ case DFU_STATE_dfuIDLE:
+ break;
+ }
+#if 0
+ printf("Resetting USB...\n");
+ if (usb_reset(usb_handle) < 0) {
+ fprintf(stderr, "error resetting after download: %s\n",
+ usb_strerror());
+ }
+#endif
+ printf("Done!\n");
+out_close:
+ close(fd);
+out_free:
+ free(buf);
+
+ return ret;
+}
+
+void sam7dfu_init()
+{
+ dfu_debug( debug );
+ dfu_init( 5000 );
+}
+
+
diff --git a/src/sam7dfu.h b/src/sam7dfu.h
new file mode 100644
index 0000000..8ebe5a2
--- /dev/null
+++ b/src/sam7dfu.h
@@ -0,0 +1,9 @@
+#ifndef _SAM7DFU_H
+#define _SAM7DFU_H
+
+int sam7dfu_do_upload(struct usb_dev_handle *usb_handle, int interface,
+ int xfer_size, const char *fname);
+int sam7dfu_do_dnload(struct usb_dev_handle *usb_handle, int interface,
+ int xfer_size, const char *fname);
+
+#endif
diff --git a/src/usb_dfu.h b/src/usb_dfu.h
new file mode 100644
index 0000000..117d73c
--- /dev/null
+++ b/src/usb_dfu.h
@@ -0,0 +1,94 @@
+#ifndef _USB_DFU_H
+#define _USB_DFU_H
+/* USB Device Firmware Update Implementation for OpenPCD
+ * (C) 2006 by Harald Welte <hwelte@hmw-consulting.de>
+ *
+ * Protocol definitions for USB DFU
+ *
+ * This ought to be compliant to the USB DFU Spec 1.0 as available from
+ * http://www.usb.org/developers/devclass_docs/usbdfu10.pdf
+ *
+ */
+
+#include <sys/types.h>
+
+#define USB_DT_DFU 0x21
+
+struct usb_dfu_func_descriptor {
+ u_int8_t bLength;
+ u_int8_t bDescriptorType;
+ u_int8_t bmAttributes;
+#define USB_DFU_CAN_DOWNLOAD (1 << 0)
+#define USB_DFU_CAN_UPLOAD (1 << 1)
+#define USB_DFU_MANIFEST_TOL (1 << 2)
+#define USB_DFU_WILL_DETACH (1 << 3)
+ u_int16_t wDetachTimeOut;
+ u_int16_t wTransferSize;
+ u_int16_t bcdDFUVersion;
+} __attribute__ ((packed));
+
+#define USB_DT_DFU_SIZE 9
+
+#define USB_TYPE_DFU (USB_TYPE_CLASS|USB_RECIP_INTERFACE)
+
+/* DFU class-specific requests (Section 3, DFU Rev 1.1) */
+#define USB_REQ_DFU_DETACH 0x00
+#define USB_REQ_DFU_DNLOAD 0x01
+#define USB_REQ_DFU_UPLOAD 0x02
+#define USB_REQ_DFU_GETSTATUS 0x03
+#define USB_REQ_DFU_CLRSTATUS 0x04
+#define USB_REQ_DFU_GETSTATE 0x05
+#define USB_REQ_DFU_ABORT 0x06
+
+#if 0
+struct dfu_status {
+ u_int8_t bStatus;
+ u_int8_t bwPollTimeout[3];
+ u_int8_t bState;
+ u_int8_t iString;
+} __attribute__((packed));
+#endif
+
+#define DFU_STATUS_OK 0x00
+#define DFU_STATUS_errTARGET 0x01
+#define DFU_STATUS_errFILE 0x02
+#define DFU_STATUS_errWRITE 0x03
+#define DFU_STATUS_errERASE 0x04
+#define DFU_STATUS_errCHECK_ERASED 0x05
+#define DFU_STATUS_errPROG 0x06
+#define DFU_STATUS_errVERIFY 0x07
+#define DFU_STATUS_errADDRESS 0x08
+#define DFU_STATUS_errNOTDONE 0x09
+#define DFU_STATUS_errFIRMWARE 0x0a
+#define DFU_STATUS_errVENDOR 0x0b
+#define DFU_STATUS_errUSBR 0x0c
+#define DFU_STATUS_errPOR 0x0d
+#define DFU_STATUS_errUNKNOWN 0x0e
+#define DFU_STATUS_errSTALLEDPKT 0x0f
+
+enum dfu_state {
+ DFU_STATE_appIDLE = 0,
+ DFU_STATE_appDETACH = 1,
+ DFU_STATE_dfuIDLE = 2,
+ DFU_STATE_dfuDNLOAD_SYNC = 3,
+ DFU_STATE_dfuDNBUSY = 4,
+ DFU_STATE_dfuDNLOAD_IDLE = 5,
+ DFU_STATE_dfuMANIFEST_SYNC = 6,
+ DFU_STATE_dfuMANIFEST = 7,
+ DFU_STATE_dfuMANIFEST_WAIT_RST = 8,
+ DFU_STATE_dfuUPLOAD_IDLE = 9,
+ DFU_STATE_dfuERROR = 10,
+};
+
+/* this is the 'swapped' definition, i.e. after/before mirroring */
+struct dfu_file_suffix {
+ u_int32_t dwCRC; /* CRC32 ANSI X3.66 */
+ u_int8_t bLength; /* 16 bytes */
+ char ucDfuSignature[3]; /* "DFU" */
+ u_int16_t bcdDFU; /* Version */
+ u_int16_t idVendor; /* VendorID */
+ u_int16_t idProduct; /* ProductID */
+ u_int16_t bcdDevice; /* Device Revision, or 0xffff */
+} __attribute__ ((packed));
+
+#endif /* _USB_DFU_H */