r1078 - in trunk/src/host: . dfu-util dfu-util/src

laforge at sita.openmoko.org laforge at sita.openmoko.org
Thu Feb 22 22:33:16 CET 2007


Author: laforge
Date: 2007-02-22 22:33:15 +0100 (Thu, 22 Feb 2007)
New Revision: 1078

Added:
   trunk/src/host/dfu-util/
   trunk/src/host/dfu-util/COPYING
   trunk/src/host/dfu-util/Makefile.am
   trunk/src/host/dfu-util/autogen.sh
   trunk/src/host/dfu-util/src/
   trunk/src/host/dfu-util/src/Makefile.am
   trunk/src/host/dfu-util/src/commands.c
   trunk/src/host/dfu-util/src/commands.h
   trunk/src/host/dfu-util/src/dfu.c
   trunk/src/host/dfu-util/src/dfu.h
   trunk/src/host/dfu-util/src/main.c
   trunk/src/host/dfu-util/src/sam7dfu.c
   trunk/src/host/dfu-util/src/usb_dfu.h
Log:
initial (unfinished) version of new DFU utility (dfu-programmer just sucks as something generic, device independent)


Added: trunk/src/host/dfu-util/COPYING
===================================================================
--- trunk/src/host/dfu-util/COPYING	2007-02-22 08:13:36 UTC (rev 1077)
+++ trunk/src/host/dfu-util/COPYING	2007-02-22 21:33:15 UTC (rev 1078)
@@ -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.

Added: trunk/src/host/dfu-util/Makefile.am
===================================================================
--- trunk/src/host/dfu-util/Makefile.am	2007-02-22 08:13:36 UTC (rev 1077)
+++ trunk/src/host/dfu-util/Makefile.am	2007-02-22 21:33:15 UTC (rev 1078)
@@ -0,0 +1,3 @@
+SUBDIRS = src
+
+EXTRA_DIST = autogen.sh

Added: trunk/src/host/dfu-util/autogen.sh
===================================================================
--- trunk/src/host/dfu-util/autogen.sh	2007-02-22 08:13:36 UTC (rev 1077)
+++ trunk/src/host/dfu-util/autogen.sh	2007-02-22 21:33:15 UTC (rev 1078)
@@ -0,0 +1,5 @@
+#! /bin/sh
+aclocal -I m4
+autoheader
+automake --foreign --add-missing --copy
+autoconf


Property changes on: trunk/src/host/dfu-util/autogen.sh
___________________________________________________________________
Name: svn:executable
   + *

Added: trunk/src/host/dfu-util/src/Makefile.am
===================================================================
--- trunk/src/host/dfu-util/src/Makefile.am	2007-02-22 08:13:36 UTC (rev 1077)
+++ trunk/src/host/dfu-util/src/Makefile.am	2007-02-22 21:33:15 UTC (rev 1078)
@@ -0,0 +1,5 @@
+AM_CFLAGS = -Wall
+bin_PROGRAMS = dfu-util
+dfu_util_SOURCES = main.c sam7dfu.c dfu.c dfu.h
+# commands.c commands.h sam7dfu.c
+

Added: trunk/src/host/dfu-util/src/commands.c
===================================================================
--- trunk/src/host/dfu-util/src/commands.c	2007-02-22 08:13:36 UTC (rev 1077)
+++ trunk/src/host/dfu-util/src/commands.c	2007-02-22 21:33:15 UTC (rev 1078)
@@ -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;
+}

Added: trunk/src/host/dfu-util/src/commands.h
===================================================================
--- trunk/src/host/dfu-util/src/commands.h	2007-02-22 08:13:36 UTC (rev 1077)
+++ trunk/src/host/dfu-util/src/commands.h	2007-02-22 21:33:15 UTC (rev 1078)
@@ -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

Added: trunk/src/host/dfu-util/src/dfu.c
===================================================================
--- trunk/src/host/dfu-util/src/dfu.c	2007-02-22 08:13:36 UTC (rev 1077)
+++ trunk/src/host/dfu-util/src/dfu.c	2007-02-22 21:33:15 UTC (rev 1078)
@@ -0,0 +1,376 @@
+/*
+ * 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;
+}

Added: trunk/src/host/dfu-util/src/dfu.h
===================================================================
--- trunk/src/host/dfu-util/src/dfu.h	2007-02-22 08:13:36 UTC (rev 1077)
+++ trunk/src/host/dfu-util/src/dfu.h	2007-02-22 21:33:15 UTC (rev 1078)
@@ -0,0 +1,101 @@
+/*
+ * 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 );
+
+int debug;
+#endif

Added: trunk/src/host/dfu-util/src/main.c
===================================================================
--- trunk/src/host/dfu-util/src/main.c	2007-02-22 08:13:36 UTC (rev 1077)
+++ trunk/src/host/dfu-util/src/main.c	2007-02-22 21:33:15 UTC (rev 1078)
@@ -0,0 +1,458 @@
+/*
+ * dfu-util
+ *
+ * (C) 2007 by OpenMoko, Inc.
+ * Written by Harald Welte <laforge at 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 "dfu.h"
+#include "usb_dfu.h"
+//#include "config.h"
+
+
+int debug;
+
+static int verbose = 0;
+
+#define DFU_INTF_FLAG_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
+
+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;
+	u_int8_t flags;
+
+	struct usb_device *dev;
+	struct usb_dev_handle *dev_handle;
+};
+
+static int _assign_first_cb(struct dfu_if *dif, void *v)
+{
+	struct dfu_if **v_dif = v;
+	*v_dif = dif;
+
+	/* 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;
+	
+	for (cfg_idx = 0; cfg_idx < dev->descriptor.bNumConfigurations;
+	     cfg_idx++) {
+		cfg = &dev->config[cfg_idx];
+		for (intf_idx = 0; intf_idx < cfg->bNumInterfaces;
+		     intf_idx++) {
+			uif = &cfg->interface[intf_idx];
+			for (alt_idx = 0;
+			     alt_idx < uif->num_altsetting; alt_idx++) {
+				intf = &uif->altsetting[alt_idx];
+				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_idx;
+					dfu_if->altsetting = 
+						intf->bAlternateSetting;
+					if (intf->bInterfaceProtocol == 2)
+						dfu_if->flags = 
+							DFU_INTF_FLAG_DFU;
+					else
+						dfu_if->flags = 0;
+					if (!handler)
+						return 1;
+					rc = handler(dfu_if, v);
+					if (rc != 0)
+						return rc;
+				}
+			}
+		}
+	}
+
+	return 0;
+}
+
+static int print_dfu_if(struct dfu_if *dfu_if, void *v)
+{
+	struct usb_device *dev = dfu_if->dev;
+
+	printf("Found DFU %s: [0x%04x:0x%04x] devnum=%u, cfg=%u, intf=%u, alt=%u\n", 
+	       dfu_if->flags & DFU_INTF_FLAG_DFU ? "DFU" : "Runtime",
+	       dev->descriptor.idVendor, dev->descriptor.idProduct,
+	       dev->devnum, dfu_if->configuration, dfu_if->interface,
+	       dfu_if->altsetting);
+
+	return 0;
+}
+
+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;
+}
+
+/* Count DFU capable devices within system */
+static int count_dfu_devices(struct dfu_if *dif)
+{
+	struct usb_bus *usb_bus;
+	struct usb_device *dev;
+	int num_found = 0;
+
+	/* 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) {
+			if (!dif || 
+			    (dif->flags & (DFU_IFF_VENDOR|DFU_IFF_PRODUCT) == 0) ||
+			    (dev->descriptor.idVendor == dif->vendor &&
+			     dev->descriptor.idProduct == dif->product)) {
+				dif->dev = dev;
+				if (count_dfu_interfaces(dev) >= 1)
+					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);
+		}
+	}
+}
+
+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;
+}
+
+static void help(void)
+{
+	printf("Usage: dfu-util [options] ...\n"
+		"  -h --help\t\t\tPrint this help message\n"
+		"  -V --version\t\tPrint the version number\n"
+		"  -l --list\t\tList the currently attached DFU capable USB devices\n"
+		"  -d --device vendor:product\tSpecify Vendor/Product ID of 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_nr\t\tSpecify the Altseting of the DFU Interface\n"
+		"  -U --upload file\tRead firmware from device into <file>\n"
+		"  -D --download file\tWrite firmware from <file> into device\n"
+		);
+}
+
+static struct option opts[] = {
+	{ "help", 0, 0, 'h' },
+	{ "version", 0, 0, 'V' },
+	{ "verbose", 0, 0, 'v' },
+	{ "list", 0, 0, 'l' },
+	{ "device", 1, 0, 'd' },
+	{ "configuration", 1, 0, 'c' },
+	{ "cfg", 1, 0, 'c' },
+	{ "interface", 1, 0, 'i' },
+	{ "intf", 1, 0, 'i' },
+	{ "altsetting", 1, 0, 'a' },
+	{ "alt", 1, 0, 'a' },
+	{ "upload", 1, 0, 'U' },
+	{ "download", 1, 0, 'D' },
+};
+
+enum mode {
+	MODE_NONE,
+	MODE_UPLOAD,
+	MODE_DOWNLOAD,
+};
+
+int main(int argc, char **argv)
+{
+	struct usb_vendprod vendprod;
+	struct dfu_if _dif, *dif = &_dif;
+	int num_devs;
+	int num_ifs;
+	enum mode mode;
+	struct dfu_status status;
+	struct usb_dfu_func_descriptor func_dfu;
+	
+	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_find_busses();
+	usb_find_devices();
+
+	while (1) {
+		int c, option_index = 0;
+		c = getopt_long(argc, argv, "hVvld:c:i:a:UD", opts, &option_index);
+		if (c == -1)
+			break;
+
+		switch (c) {
+		case 'h':
+			help();
+			exit(0);
+			break;
+		case 'V':
+			/* FIXME */
+			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 '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 = atoi(optarg);
+			dif->flags |= DFU_IFF_ALT;
+			break;
+		case 'U':
+			mode = MODE_UPLOAD;
+			break;
+		case 'D':
+			mode = MODE_DOWNLOAD;
+			break;
+		default:
+			help();
+			exit(2);
+		}
+	}
+
+	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);
+	}
+	/* We have exactly one device. It's usb_device is now in dif->dev */
+
+	if (mode == MODE_NONE) {
+		fprintf(stderr, "You need to specify one of -D or -U\n");
+		help();
+		exit(2);
+	}
+
+	/* FIXME: check if there's only one interface/altsetting and use it */
+	if (!dif->flags & (DFU_IFF_CONFIG|DFU_IFF_ALT) != DFU_IFF_CONFIG|DFU_IFF_ALT) {
+		fprintf(stderr, "You have to specify --cfg and --alt!\n");
+		help();
+		exit(1);
+	}
+
+retry:
+	/* FIXME: need to re-scan and re-select same device after reset*/
+	if (find_dfu_if(dif->dev, NULL, NULL) != 1) {
+		fprintf(stderr, "Can't find device anymore !?!\n");
+		exit(1);
+	}
+
+	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);
+	}
+
+	printf("Setting Configuration...\n");
+	if (usb_set_configuration(dif->dev_handle, dif->configuration) < 0) {
+		fprintf(stderr, "Cannot set configuration: %s\n", usb_strerror());
+		exit(1);
+	}
+
+	printf("Claiming USB 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);
+	}
+
+	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 = %d, status = %d\n", status.bState, status.bStatus);
+
+	switch (status.bState) {
+	case STATE_APP_IDLE:
+		printf("Device in Runtime Mode, send DFU detach request...\n");
+		if (dfu_detach(dif->dev_handle, dif->interface, 1000) < 0) {
+			fprintf(stderr, "error detaching: %s\n", usb_strerror());
+			exit(1);
+			break;
+		}
+		printf("Resetting USB...\n");
+		if (usb_reset(dif->dev_handle) < 0) {
+			fprintf(stderr, "error resetting after detach: %s\n", usb_strerror());
+		}
+		sleep(2);
+		goto retry;
+		break;
+	case STATE_DFU_ERROR:
+		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);
+			break;
+		}
+		goto retry;
+		break;
+	case STATE_DFU_IDLE:
+		printf("dfuIDLE, continuing\n");
+		break;
+	}
+
+	{
+		int ret;
+		
+		ret = usb_get_descriptor(dif->dev_handle, 0x21, dif->interface,
+					 &func_dfu, sizeof(func_dfu));
+		if (ret < 0) {
+			fprintf(stderr, "error clear_status: %s\n",
+				usb_strerror());
+			exit(1);
+		}
+
+		printf("wTransferSize = 0x%04x\n", func_dfu.wTransferSize);
+			
+	}
+	
+	if (DFU_STATUS_OK != 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);
+		}
+        }
+#if 0
+	sam7dfu_do_upload(usb_handle, interface, func_dfu.wTransferSize, 
+			  "/tmp/dfu_upload.img");
+	//sam7dfu_do_dnload(usb_handle, interface, 252,
+	sam7dfu_do_dnload(usb_handle, interface, func_dfu.wTransferSize, 
+			  "/tmp/dfu_upload.img");
+#endif
+
+	exit(0);
+}
+

Added: trunk/src/host/dfu-util/src/sam7dfu.c
===================================================================
--- trunk/src/host/dfu-util/src/sam7dfu.c	2007-02-22 08:13:36 UTC (rev 1077)
+++ trunk/src/host/dfu-util/src/sam7dfu.c	2007-02-22 21:33:15 UTC (rev 1078)
@@ -0,0 +1,147 @@
+/* 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"
+
+int sam7dfu_do_upload(struct usb_dev_handle *usb_handle, int interface, 
+		      int xfer_size, const char *fname)
+{
+	int ret, fd, total_bytes;
+	char *buf = malloc(xfer_size);
+
+	if (!buf)
+		return -ENOMEM;
+
+	fd = creat(fname, 0644);
+	if (fd < 0) {
+		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));
+			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;
+}
+
+int sam7dfu_do_dnload(struct usb_dev_handle *usb_handle, int interface,
+		      int xfer_size, const char *fname)
+{
+	int ret, fd, bytes_sent = 0;
+	char *buf = malloc(xfer_size);
+	struct stat st;
+	struct dfu_status dst;
+
+	if (!buf)
+		return -ENOMEM;
+
+	fd = open(fname, O_RDONLY);
+	if (fd < 0) {
+		ret = fd;
+		goto out_free;
+	}
+	
+	ret = fstat(fd, &st);
+	if (ret < 0)
+		goto out_close;
+
+	if (st.st_size <= 0 /* + DFU_HDR */) {
+		fprintf(stderr, "File seems a bit too small...\n");
+		ret = -EINVAL;
+		goto out_close;	
+	}
+
+#if 0
+	read(fd, DFU_HDR);
+#endif
+	printf("Starting download: [");
+	while (bytes_sent < st.st_size /* - DFU_HDR */) {
+		ret = read(fd, buf, xfer_size);
+		if (ret < 0)
+			goto out_close;
+		ret = dfu_download(usb_handle, interface, ret, buf);
+		if (ret < 0)
+			goto out_close;
+		bytes_sent += ret;
+		do {
+			ret = dfu_get_status(usb_handle, interface, &dst);
+			if (ret < 0)
+				goto out_close;
+		} while (dst.bState != DFU_STATE_dfuDNLOAD_IDLE ||
+			 dst.bStatus != DFU_STATUS_OK);
+		putchar('#');
+		usleep(5000);
+		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");
+
+	/* Transition to MANIFEST_SYNC state */
+	ret = dfu_get_status(usb_handle, interface, &dst);
+	if (ret < 0) {
+		fprintf(stderr, "unable to transition to MANIFEST state\n");
+		goto out_close;
+	}
+		
+	printf("Resetting USB...\n");
+	sleep(1);
+	if (usb_reset(usb_handle) < 0) {
+		fprintf(stderr, "error resetting after download: %s\n", 
+			usb_strerror());
+	}
+out_close:
+	close(fd);
+out_free:
+	free(buf);
+
+	return ret;
+}
+
+void sam7dfu_init()
+{
+    dfu_debug( debug );
+    dfu_init( 5000 );
+}
+
+

Added: trunk/src/host/dfu-util/src/usb_dfu.h
===================================================================
--- trunk/src/host/dfu-util/src/usb_dfu.h	2007-02-22 08:13:36 UTC (rev 1077)
+++ trunk/src/host/dfu-util/src/usb_dfu.h	2007-02-22 21:33:15 UTC (rev 1078)
@@ -0,0 +1,83 @@
+#ifndef _USB_DFU_H
+#define _USB_DFU_H
+/* USB Device Firmware Update Implementation for OpenPCD
+ * (C) 2006 by Harald Welte <hwelte at 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,
+};
+
+#endif /* _USB_DFU_H */





More information about the commitlog mailing list