diff --git a/STM32F1/platform.txt b/STM32F1/platform.txt index caca7fe..24864c1 100644 --- a/STM32F1/platform.txt +++ b/STM32F1/platform.txt @@ -107,15 +107,13 @@ recipe.output.save_file={build.project_name}.{build.variant}.bin # Uploader tools # ------------------- - - # Upload using Maple bootloader over DFU tools.maple_upload.cmd=maple_upload tools.maple_upload.cmd.windows=maple_upload.bat tools.maple_upload.path={runtime.hardware.path}/tools/win tools.maple_upload.path.macosx={runtime.hardware.path}/tools/macosx -tools.maple_upload.path.linux={runtime.hardware.path}/tools/linux - +tools.maple_upload.path.linux={runtime.hardware.path}/tools/linux +tools.maple_upload.path.linux64={runtime.hardware.path}/tools/linux64 tools.maple_upload.upload.params.verbose=-d tools.maple_upload.upload.params.quiet= tools.maple_upload.upload.pattern="{path}/{cmd}" {serial.port.file} {upload.altID} {upload.usbID} "{build.path}/{build.project_name}.bin" @@ -129,29 +127,18 @@ tools.serial_upload.cmd.macosx=serial_upload tools.serial_upload.path={runtime.hardware.path}/tools/win tools.serial_upload.path.macosx={runtime.hardware.path}/tools/macosx tools.serial_upload.path.linux={runtime.hardware.path}/tools/linux - +tools.serial_upload.path.linux64={runtime.hardware.path}/tools/linux64 tools.serial_upload.upload.params.verbose=-d tools.serial_upload.upload.params.quiet=n tools.serial_upload.upload.pattern="{path}/{cmd}" {serial.port.file} {upload.altID} {upload.usbID} "{build.path}/{build.project_name}.bin" - -tools.upload_router.cmd=upload_router -tools.upload_router.cmd.windows=upload_router.bat -tools.upload_router.path={runtime.hardware.path}/tools/win -tools.upload_router.path.macosx={runtime.hardware.path}/tools/macosx -tools.upload_router.path.linux={runtime.hardware.path}/tools/linux - -tools.upload_router.upload.params.verbose=-d -tools.upload_router.upload.params.quiet= -tools.upload_router.upload.pattern="{path}/{cmd}" {serial.port.file} {upload.altID} {upload.usbID} "{build.path}/{build.project_name}" {upload.protocol} {build.debuggingMode} "{runtime.ide.path}/hardware/tools/{build.gcc_ver}/bin/" - # stlink upload tools.stlink_upload.cmd=stlink_upload tools.stlink_upload.cmd.windows=stlink_upload.bat tools.stlink_upload.path.windows={runtime.hardware.path}/tools/win tools.stlink_upload.path.macosx={runtime.hardware.path}/tools/macosx tools.stlink_upload.path.linux={runtime.hardware.path}/tools/linux - +tools.stlink_upload.path.linux64={runtime.hardware.path}/tools/linux64 tools.stlink_upload.upload.params.verbose=-d tools.stlink_upload.upload.params.quiet= tools.stlink_upload.upload.pattern="{path}/{cmd}" {serial.port.file} {upload.altID} {upload.usbID} "{build.path}/{build.project_name}.bin" @@ -162,4 +149,4 @@ tools.bmp_upload.path={runtime.tools.arm-none-eabi-gcc.path}/bin/ tools.bmp_upload.upload.speed=230400 tools.bmp_upload.upload.params.verbose= tools.bmp_upload.upload.params.quiet=-q --batch-silent -tools.bmp_upload.upload.pattern="{path}{cmd}" -cd "{build.path}" -b {upload.speed} {upload.verbose} -ex "set debug remote 0" -ex "set target-async off" -ex "set remotetimeout 60" -ex "set mem inaccessible-by-default off" -ex "set confirm off" -ex "set height 0" -ex "target extended-remote {serial.port}" -ex "monitor swdp_scan" -ex "attach 1" -ex "x/wx 0x8000004" -ex "monitor erase_mass" -ex "echo 0x8000004 expect 0xffffffff after erase\n" -ex "x/wx 0x8000004" -ex "file {build.project_name}.elf" -ex "load" -ex "x/wx 0x08000004" -ex "tbreak main" -ex "run" -ex "echo \n\n\nUpload finished!" -ex "quit" +tools.bmp_upload.upload.pattern="{path}{cmd}" -cd "{build.path}" -b {upload.speed} {upload.verbose} -ex "set debug remote 0" -ex "set target-async off" -ex "set remotetimeout 60" -ex "set mem inaccessible-by-default off" -ex "set confirm off" -ex "set height 0" -ex "target extended-remote {serial.port}" -ex "monitor swdp_scan" -ex "attach 1" -ex "x/wx 0x8000004" -ex "monitor erase_mass" -ex "echo 0x8000004 expect 0xffffffff after erase\n" -ex "x/wx 0x8000004" -ex "file {build.project_name}.elf" -ex "load" -ex "x/wx 0x08000004" -ex "tbreak main" -ex "run" -ex "echo \n\n\nUpload finished!" -ex "quit" \ No newline at end of file diff --git a/tools/src/build_dfu-util.sh b/tools/linux/src/build_dfu-util.sh old mode 100755 new mode 100644 similarity index 100% rename from tools/src/build_dfu-util.sh rename to tools/linux/src/build_dfu-util.sh diff --git a/tools/src/dfu-util/AUTHORS b/tools/linux/src/dfu-util/AUTHORS old mode 100755 new mode 100644 similarity index 93% rename from tools/src/dfu-util/AUTHORS rename to tools/linux/src/dfu-util/AUTHORS index bc16c9c..1b36c73 --- a/tools/src/dfu-util/AUTHORS +++ b/tools/linux/src/dfu-util/AUTHORS @@ -1,30 +1,30 @@ -Authors ordered by first contribution. - -Harald Welte -Werner Almesberger -Michael Lauer -Jim Huang -Stefan Schmidt -Daniel Willmann -Mike Frysinger -Uwe Hermann -C. Scott Ananian -Bernard Blackham -Holger Freyther -Marc Singer -James Perkins -Tommi Keisala -Pascal Schweizer -Bradley Scott -Uwe Bonnes -Andrey Smirnov -Jussi Timperi -Hans Petter Selasky -Bo Shen -Henrique de Almeida Mendonca -Bernd Krumboeck -Dennis Meier -Veli-Pekka Peltola -Dave Hylands -Michael Grzeschik -Paul Fertser +Authors ordered by first contribution. + +Harald Welte +Werner Almesberger +Michael Lauer +Jim Huang +Stefan Schmidt +Daniel Willmann +Mike Frysinger +Uwe Hermann +C. Scott Ananian +Bernard Blackham +Holger Freyther +Marc Singer +James Perkins +Tommi Keisala +Pascal Schweizer +Bradley Scott +Uwe Bonnes +Andrey Smirnov +Jussi Timperi +Hans Petter Selasky +Bo Shen +Henrique de Almeida Mendonca +Bernd Krumboeck +Dennis Meier +Veli-Pekka Peltola +Dave Hylands +Michael Grzeschik +Paul Fertser diff --git a/tools/src/dfu-util/COPYING b/tools/linux/src/dfu-util/COPYING old mode 100755 new mode 100644 similarity index 98% rename from tools/src/dfu-util/COPYING rename to tools/linux/src/dfu-util/COPYING index fbdd65f..d60c31a --- a/tools/src/dfu-util/COPYING +++ b/tools/linux/src/dfu-util/COPYING @@ -1,340 +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. - - - Copyright (C) - - 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. - - , 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. + 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. + + + Copyright (C) + + 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. + + , 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/tools/src/dfu-util/ChangeLog b/tools/linux/src/dfu-util/ChangeLog old mode 100755 new mode 100644 similarity index 97% rename from tools/src/dfu-util/ChangeLog rename to tools/linux/src/dfu-util/ChangeLog index 35a1da7..37f1add --- a/tools/src/dfu-util/ChangeLog +++ b/tools/linux/src/dfu-util/ChangeLog @@ -1,93 +1,93 @@ -0.8: - o New, separate dfu-prefix tool (Uwe Bonnes) - o Allow filtering on serial number (Uwe Bonnes) - o Improved VID/PID/serial filtering (Bradley Scott) - o Support reading firmware from stdin (Tormod Volden) - o Warn if missing DFU suffix (Tormod Volden) - o Improved progress bar (Hans Petter Selasky) - o Fix dfuse leave option (Uwe Bonnes) - o Major code rework (Hans Petter Selasky) - o MS Visual Studio build support (Henrique Mendonca) - o dfuse-pack.py tool for .dfu files (Antonio Galeo) - o Many other fixes from many people - -2014-09-13: Tormod Volden - -0.7: - o Support for TI Stellaris devices (Tommi Keisala) - o Fix libusb detection on MacOSX (Marc Singer) - o Fix libusb detection on FreeBSD (Tormod Volden) - o Improved DfuSe support (Tormod Volden) - o Support all special commands (leave, unprotect, mass-erase) - o Arbitrary upload lengths - o "force" option for various possible (dangerous) overrides - -2012-10-07: Tormod Volden - -0.6: - o Add detach mode (Stefan Schmidt) - o Check return value on all libusb calls (Tormod Volden) - o Fix segmentation fault with -s option (Tormod Volden) - o Add DFU suffix manipulation tool (Stefan Schmidt) - o Port to Windows: (Tormod Volden, some parts based on work from Satz - Klauer) - o Port file handling to stdio streams - o Sleep() macros - o C99 types - o Pack structs - o Detect DfuSe device correctly on big-endian architectures (Tormod - Volden) - o Add dfuse progress indication on download (Tormod Volden) - o Cleanup: gcc pedantic, gcc extension, ... (Tormod Volden) - o Rely on page size from functional descriptor. Please report if you get - an error about it. (Tormod Volden) - o Add quirk for Maple since it reports wrong DFU version (Tormod Volden) - -2012-04-22: Stefan Schmidt - -0.5: - o DfuSe extension support for ST devices (Tormod Volden) - o Add initial support for bitWillDetach flag from DFU 1.1 (Tormod - Volden) - o Internal cleanup and some manual page fixes (Tormod Volden) - -2011-11-02: Stefan Schmidt - -0.4: - o Rework to use libusb-1.0 (Stefan Schmidt) - o DFU suffix support (Tormod Volden, Stefan Schmidt) - o Sspeed up DFU downloads directly into memory (Bernard Blackham) - o More flexible -d vid:pid parsing (Tormod Volden) - o Many bug fixes and cleanups - -2011-07-20: Stefan Schmidt - -0.3: - o quirks: Add OpenOCD to the poll timeout quirk table. - -2010-12-22: Stefan Schmidt - -0.2: - o Fix some typos on the website and the README (Antonio Ospite, Uwe - Hermann) - o Remove build rule for a static binary. We can use autotools for this. - (Mike Frysinger) - o Fix infinite loop in download error path (C. Scott Ananian) - o Break out to show the 'finished' in upload (C. Scott Ananian) - o Add GPLv2+ headers (Harald Welte) - o Remove dead code (commands.[ch]) remnescent of dfu-programmer (Harald - Welte) - o Simple quirk system with Openmoko quirk for missing bwPollTimeout (Tormod Volden) - o New default (1024) and clamping of transfer size (Tormod Volden) - o Verify sending of completion packet (Tormod Volden) - o Look for DFU functional descriptor among all descriptors (Tormod - Volden) - o Print out in which direction we are transferring data - o Abort in upload if the file already exists - -2010-11-17 Stefan Schmidt - -0.1: - Initial release - -2010-05-23 Stefan Schmidt +0.8: + o New, separate dfu-prefix tool (Uwe Bonnes) + o Allow filtering on serial number (Uwe Bonnes) + o Improved VID/PID/serial filtering (Bradley Scott) + o Support reading firmware from stdin (Tormod Volden) + o Warn if missing DFU suffix (Tormod Volden) + o Improved progress bar (Hans Petter Selasky) + o Fix dfuse leave option (Uwe Bonnes) + o Major code rework (Hans Petter Selasky) + o MS Visual Studio build support (Henrique Mendonca) + o dfuse-pack.py tool for .dfu files (Antonio Galeo) + o Many other fixes from many people + +2014-09-13: Tormod Volden + +0.7: + o Support for TI Stellaris devices (Tommi Keisala) + o Fix libusb detection on MacOSX (Marc Singer) + o Fix libusb detection on FreeBSD (Tormod Volden) + o Improved DfuSe support (Tormod Volden) + o Support all special commands (leave, unprotect, mass-erase) + o Arbitrary upload lengths + o "force" option for various possible (dangerous) overrides + +2012-10-07: Tormod Volden + +0.6: + o Add detach mode (Stefan Schmidt) + o Check return value on all libusb calls (Tormod Volden) + o Fix segmentation fault with -s option (Tormod Volden) + o Add DFU suffix manipulation tool (Stefan Schmidt) + o Port to Windows: (Tormod Volden, some parts based on work from Satz + Klauer) + o Port file handling to stdio streams + o Sleep() macros + o C99 types + o Pack structs + o Detect DfuSe device correctly on big-endian architectures (Tormod + Volden) + o Add dfuse progress indication on download (Tormod Volden) + o Cleanup: gcc pedantic, gcc extension, ... (Tormod Volden) + o Rely on page size from functional descriptor. Please report if you get + an error about it. (Tormod Volden) + o Add quirk for Maple since it reports wrong DFU version (Tormod Volden) + +2012-04-22: Stefan Schmidt + +0.5: + o DfuSe extension support for ST devices (Tormod Volden) + o Add initial support for bitWillDetach flag from DFU 1.1 (Tormod + Volden) + o Internal cleanup and some manual page fixes (Tormod Volden) + +2011-11-02: Stefan Schmidt + +0.4: + o Rework to use libusb-1.0 (Stefan Schmidt) + o DFU suffix support (Tormod Volden, Stefan Schmidt) + o Sspeed up DFU downloads directly into memory (Bernard Blackham) + o More flexible -d vid:pid parsing (Tormod Volden) + o Many bug fixes and cleanups + +2011-07-20: Stefan Schmidt + +0.3: + o quirks: Add OpenOCD to the poll timeout quirk table. + +2010-12-22: Stefan Schmidt + +0.2: + o Fix some typos on the website and the README (Antonio Ospite, Uwe + Hermann) + o Remove build rule for a static binary. We can use autotools for this. + (Mike Frysinger) + o Fix infinite loop in download error path (C. Scott Ananian) + o Break out to show the 'finished' in upload (C. Scott Ananian) + o Add GPLv2+ headers (Harald Welte) + o Remove dead code (commands.[ch]) remnescent of dfu-programmer (Harald + Welte) + o Simple quirk system with Openmoko quirk for missing bwPollTimeout (Tormod Volden) + o New default (1024) and clamping of transfer size (Tormod Volden) + o Verify sending of completion packet (Tormod Volden) + o Look for DFU functional descriptor among all descriptors (Tormod + Volden) + o Print out in which direction we are transferring data + o Abort in upload if the file already exists + +2010-11-17 Stefan Schmidt + +0.1: + Initial release + +2010-05-23 Stefan Schmidt diff --git a/tools/src/dfu-util/DEVICES.txt b/tools/linux/src/dfu-util/DEVICES.txt old mode 100755 new mode 100644 similarity index 96% rename from tools/src/dfu-util/DEVICES.txt rename to tools/linux/src/dfu-util/DEVICES.txt index 682e62c..bdd9f1f --- a/tools/src/dfu-util/DEVICES.txt +++ b/tools/linux/src/dfu-util/DEVICES.txt @@ -1,20 +1,20 @@ -List of supported software and hardware products: - -Software user (bootloader, etc) -------------------------------- -- Sam7DFU: http://www.openpcd.org/Sam7dfu -- U-boot: DFU patches -- Barebox: http://www.barebox.org/ -- Leaflabs: http://code.google.com/p/leaflabs/ -- Blackmagic DFU - -Products using DFU ------------------- -- OpenPCD (sam7dfu) -- Openmoko Neo 1973 and Freerunner (u-boot with DFU patches) -- Leaflabs Maple -- ATUSB from Qi Hardware -- STM32F105/7, STM32F2/F3/F4 in System Bootloader -- Blackmagic debug probe -- NXP LPC31xx/LPC43XX, e.g. LPC-Link and LPC-Link2, need binaries - with LPC prefix and encoding (LPC-Link) +List of supported software and hardware products: + +Software user (bootloader, etc) +------------------------------- +- Sam7DFU: http://www.openpcd.org/Sam7dfu +- U-boot: DFU patches +- Barebox: http://www.barebox.org/ +- Leaflabs: http://code.google.com/p/leaflabs/ +- Blackmagic DFU + +Products using DFU +------------------ +- OpenPCD (sam7dfu) +- Openmoko Neo 1973 and Freerunner (u-boot with DFU patches) +- Leaflabs Maple +- ATUSB from Qi Hardware +- STM32F105/7, STM32F2/F3/F4 in System Bootloader +- Blackmagic debug probe +- NXP LPC31xx/LPC43XX, e.g. LPC-Link and LPC-Link2, need binaries + with LPC prefix and encoding (LPC-Link) diff --git a/tools/src/dfu-util/Makefile.am b/tools/linux/src/dfu-util/Makefile.am old mode 100755 new mode 100644 similarity index 96% rename from tools/src/dfu-util/Makefile.am rename to tools/linux/src/dfu-util/Makefile.am index 7318dd8..641dda5 --- a/tools/src/dfu-util/Makefile.am +++ b/tools/linux/src/dfu-util/Makefile.am @@ -1,3 +1,3 @@ -SUBDIRS = src doc - -EXTRA_DIST = autogen.sh TODO DEVICES.txt dfuse-pack.py +SUBDIRS = src doc + +EXTRA_DIST = autogen.sh TODO DEVICES.txt dfuse-pack.py diff --git a/tools/src/dfu-util/README b/tools/linux/src/dfu-util/README old mode 100755 new mode 100644 similarity index 97% rename from tools/src/dfu-util/README rename to tools/linux/src/dfu-util/README index acda06a..0f8f262 --- a/tools/src/dfu-util/README +++ b/tools/linux/src/dfu-util/README @@ -1,20 +1,20 @@ -Dfu-util - Device Firmware Upgrade Utilities - -Dfu-util is the host side implementation of the DFU 1.0 [1] and DFU 1.1 [2] -specification of the USB forum. - -DFU is intended to download and upload firmware to devices connected over -USB. It ranges from small devices like micro-controller boards up to mobile -phones. With dfu-util you are able to download firmware to your device or -upload firmware from it. - -dfu-util has been tested with Openmoko Neo1973 and Freerunner and many -other devices. - -[1] DFU 1.0 spec: http://www.usb.org/developers/devclass_docs/usbdfu10.pdf -[2] DFU 1.1 spec: http://www.usb.org/developers/devclass_docs/DFU_1.1.pdf - -The official website is: - - http://dfu-util.gnumonks.org/ - +Dfu-util - Device Firmware Upgrade Utilities + +Dfu-util is the host side implementation of the DFU 1.0 [1] and DFU 1.1 [2] +specification of the USB forum. + +DFU is intended to download and upload firmware to devices connected over +USB. It ranges from small devices like micro-controller boards up to mobile +phones. With dfu-util you are able to download firmware to your device or +upload firmware from it. + +dfu-util has been tested with Openmoko Neo1973 and Freerunner and many +other devices. + +[1] DFU 1.0 spec: http://www.usb.org/developers/devclass_docs/usbdfu10.pdf +[2] DFU 1.1 spec: http://www.usb.org/developers/devclass_docs/DFU_1.1.pdf + +The official website is: + + http://dfu-util.gnumonks.org/ + diff --git a/tools/src/dfu-util/TODO b/tools/linux/src/dfu-util/TODO old mode 100755 new mode 100644 similarity index 96% rename from tools/src/dfu-util/TODO rename to tools/linux/src/dfu-util/TODO index 7574512..900c30c --- a/tools/src/dfu-util/TODO +++ b/tools/linux/src/dfu-util/TODO @@ -1,14 +1,14 @@ -DfuSe: -- Do erase and write in two separate passes when downloading -- Skip "Set Address" command when downloading contiguous blocks -- Implement "Get Commands" command - -Devices: -- Research iPhone/iPod/iPad support - Heavily modified dfu-util fork here: - https://github.com/planetbeing/xpwn/tree/master/dfu-util -- Test against Niftylights - -Non-Code: -- Logo -- Re-License as LGPL for usage as library? +DfuSe: +- Do erase and write in two separate passes when downloading +- Skip "Set Address" command when downloading contiguous blocks +- Implement "Get Commands" command + +Devices: +- Research iPhone/iPod/iPad support + Heavily modified dfu-util fork here: + https://github.com/planetbeing/xpwn/tree/master/dfu-util +- Test against Niftylights + +Non-Code: +- Logo +- Re-License as LGPL for usage as library? diff --git a/tools/src/dfu-util/autogen.sh b/tools/linux/src/dfu-util/autogen.sh old mode 100755 new mode 100644 similarity index 100% rename from tools/src/dfu-util/autogen.sh rename to tools/linux/src/dfu-util/autogen.sh diff --git a/tools/src/dfu-util/configure.ac b/tools/linux/src/dfu-util/configure.ac old mode 100755 new mode 100644 similarity index 96% rename from tools/src/dfu-util/configure.ac rename to tools/linux/src/dfu-util/configure.ac index 8810f56..8622114 --- a/tools/src/dfu-util/configure.ac +++ b/tools/linux/src/dfu-util/configure.ac @@ -1,41 +1,41 @@ -# -*- Autoconf -*- -# Process this file with autoconf to produce a configure script. - -AC_PREREQ(2.59) -AC_INIT([dfu-util],[0.8],[dfu-util@lists.gnumonks.org],,[http://dfu-util.gnumonks.org]) -AC_CONFIG_AUX_DIR(m4) -AM_INIT_AUTOMAKE([foreign]) -AC_CONFIG_HEADERS([config.h]) - -# Test for new silent rules and enable only if they are available -m4_ifdef([AM_SILENT_RULES], [AM_SILENT_RULES([yes])]) - -# Checks for programs. -AC_PROG_CC - -# Checks for libraries. -# On FreeBSD the libusb-1.0 is called libusb and resides in system location -AC_CHECK_LIB([usb], [libusb_init],, [native_libusb=no],) -AS_IF([test x$native_libusb = xno], [ - PKG_CHECK_MODULES([USB], [libusb-1.0 >= 1.0.0],, - AC_MSG_ERROR([*** Required libusb-1.0 >= 1.0.0 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([usbpath.h windows.h sysexits.h]) - -# Checks for typedefs, structures, and compiler characteristics. -AC_C_CONST -AC_TYPE_SIZE_T - -# Checks for library functions. -AC_FUNC_MEMCMP -AC_CHECK_FUNCS([ftruncate getpagesize nanosleep err]) - -AC_CONFIG_FILES(Makefile src/Makefile doc/Makefile) -AC_OUTPUT +# -*- Autoconf -*- +# Process this file with autoconf to produce a configure script. + +AC_PREREQ(2.59) +AC_INIT([dfu-util],[0.8],[dfu-util@lists.gnumonks.org],,[http://dfu-util.gnumonks.org]) +AC_CONFIG_AUX_DIR(m4) +AM_INIT_AUTOMAKE([foreign]) +AC_CONFIG_HEADERS([config.h]) + +# Test for new silent rules and enable only if they are available +m4_ifdef([AM_SILENT_RULES], [AM_SILENT_RULES([yes])]) + +# Checks for programs. +AC_PROG_CC + +# Checks for libraries. +# On FreeBSD the libusb-1.0 is called libusb and resides in system location +AC_CHECK_LIB([usb], [libusb_init],, [native_libusb=no],) +AS_IF([test x$native_libusb = xno], [ + PKG_CHECK_MODULES([USB], [libusb-1.0 >= 1.0.0],, + AC_MSG_ERROR([*** Required libusb-1.0 >= 1.0.0 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([usbpath.h windows.h sysexits.h]) + +# Checks for typedefs, structures, and compiler characteristics. +AC_C_CONST +AC_TYPE_SIZE_T + +# Checks for library functions. +AC_FUNC_MEMCMP +AC_CHECK_FUNCS([ftruncate getpagesize nanosleep err]) + +AC_CONFIG_FILES(Makefile src/Makefile doc/Makefile) +AC_OUTPUT diff --git a/tools/src/dfu-util/device-logs/README b/tools/linux/src/dfu-util/device-logs/README old mode 100755 new mode 100644 similarity index 96% rename from tools/src/dfu-util/device-logs/README rename to tools/linux/src/dfu-util/device-logs/README index c1de3e7..00d3d1a --- a/tools/src/dfu-util/device-logs/README +++ b/tools/linux/src/dfu-util/device-logs/README @@ -1,77 +1,77 @@ -Device: -------- -qi-hardware-atusb: -- Qi Hardware ben-wpan -- DFU implementation: - http://projects.qi-hardware.com/index.php/p/ben-wpan/source/tree/master/atusb/fw/usb -- Tester: Stefan Schmidt - -openpcd: -- OpenPCD RFID reader -- DFU implementation: SAM7DFU - http://www.openpcd.org/Sam7dfu, git://git.gnumonks.org/openpcd.git -- Tester: Stefan Schmidt - -simtrace: -- Sysmocom SimTrace -- DFU implementation: SAM7DFU - http://www.openpcd.org/Sam7dfu, git://git.gnumonks.org/openpcd.git -- Tester: Stefan Schmidt - -openmoko-freerunner: -- Openmoko Freerunner -- DFU implementation: Old U-Boot - http://git.openmoko.org/?p=u-boot.git;a=shortlog;h=refs/heads/mokopatches -- Tester: Stefan Schmidt - -openmoko-neo1973: -- Openmoko Neo1073 -- DFU implementation: Old U-Boot - http://git.openmoko.org/?p=u-boot.git;a=shortlog;h=refs/heads/mokopatches -- Tester: Stefan Schmidt - -tdk-bluetooth: -- TDK Corp. Bluetooth Adapter -- DFU implementation: closed soure -- Only upload has been tested -- Tester: Stefan Schmidt - -stm32f107: -- STM32 microcontrollers with built-in (ROM) DFU loader -- DFU implementation: Closed source but probably similar to the one - in their USB device libraries. Some relevant application notes: - http://www.st.com -> AN3156 and AN2606 -- Tested by Uwe Bonnes - -stm32f4discovery: -- STM32 microcontroller board with built-in (ROM) DFU loader -- DFU implementation: Closed source, probably similar to stm32f107. -- Tested by Joe Rothweiler - -dso-nano: -- DSO Nano pocket oscilloscope -- DFU implementation: Based on ST Microelectronics USB FS Library 1.0 - http://dsonano.googlecode.com/files/DS0201_OpenSource.rar -- Tester: Tormod Volden - -opc-20: -- Custom devices based on STM32F1xx -- DFU implementation: ST Microelectronics USB FS Device Library 3.1.0 - http://www.st.com -> um0424.zip -- Tester: Tormod Volden - -lpc-link, lpclink2: -- NXP LPCXpresso debug adapters -- Proprietary DFU implementation, uses special download files with - LPC prefix and encoding of the target firmware code -- Tested by Uwe Bonnes - -Adding the lsusb output and a download log of your device here helps -us to avoid regressions for hardware we cannot test while working on -the code. To extract the lsusb output use this command: -sudo lsusb -v -d $USBID > $DEVICE.lsusb -Prepare a description snippet as above, and send it to us. A log -(copy-paste of the command window) of a firmware download is also -nice, please use the double verbose option -v -v and include the -command line in the log file. - +Device: +------- +qi-hardware-atusb: +- Qi Hardware ben-wpan +- DFU implementation: + http://projects.qi-hardware.com/index.php/p/ben-wpan/source/tree/master/atusb/fw/usb +- Tester: Stefan Schmidt + +openpcd: +- OpenPCD RFID reader +- DFU implementation: SAM7DFU + http://www.openpcd.org/Sam7dfu, git://git.gnumonks.org/openpcd.git +- Tester: Stefan Schmidt + +simtrace: +- Sysmocom SimTrace +- DFU implementation: SAM7DFU + http://www.openpcd.org/Sam7dfu, git://git.gnumonks.org/openpcd.git +- Tester: Stefan Schmidt + +openmoko-freerunner: +- Openmoko Freerunner +- DFU implementation: Old U-Boot + http://git.openmoko.org/?p=u-boot.git;a=shortlog;h=refs/heads/mokopatches +- Tester: Stefan Schmidt + +openmoko-neo1973: +- Openmoko Neo1073 +- DFU implementation: Old U-Boot + http://git.openmoko.org/?p=u-boot.git;a=shortlog;h=refs/heads/mokopatches +- Tester: Stefan Schmidt + +tdk-bluetooth: +- TDK Corp. Bluetooth Adapter +- DFU implementation: closed soure +- Only upload has been tested +- Tester: Stefan Schmidt + +stm32f107: +- STM32 microcontrollers with built-in (ROM) DFU loader +- DFU implementation: Closed source but probably similar to the one + in their USB device libraries. Some relevant application notes: + http://www.st.com -> AN3156 and AN2606 +- Tested by Uwe Bonnes + +stm32f4discovery: +- STM32 microcontroller board with built-in (ROM) DFU loader +- DFU implementation: Closed source, probably similar to stm32f107. +- Tested by Joe Rothweiler + +dso-nano: +- DSO Nano pocket oscilloscope +- DFU implementation: Based on ST Microelectronics USB FS Library 1.0 + http://dsonano.googlecode.com/files/DS0201_OpenSource.rar +- Tester: Tormod Volden + +opc-20: +- Custom devices based on STM32F1xx +- DFU implementation: ST Microelectronics USB FS Device Library 3.1.0 + http://www.st.com -> um0424.zip +- Tester: Tormod Volden + +lpc-link, lpclink2: +- NXP LPCXpresso debug adapters +- Proprietary DFU implementation, uses special download files with + LPC prefix and encoding of the target firmware code +- Tested by Uwe Bonnes + +Adding the lsusb output and a download log of your device here helps +us to avoid regressions for hardware we cannot test while working on +the code. To extract the lsusb output use this command: +sudo lsusb -v -d $USBID > $DEVICE.lsusb +Prepare a description snippet as above, and send it to us. A log +(copy-paste of the command window) of a firmware download is also +nice, please use the double verbose option -v -v and include the +command line in the log file. + diff --git a/tools/src/dfu-util/device-logs/dsonano.lsusb b/tools/linux/src/dfu-util/device-logs/dsonano.lsusb old mode 100755 new mode 100644 similarity index 97% rename from tools/src/dfu-util/device-logs/dsonano.lsusb rename to tools/linux/src/dfu-util/device-logs/dsonano.lsusb index 44a5d28..140a7bc --- a/tools/src/dfu-util/device-logs/dsonano.lsusb +++ b/tools/linux/src/dfu-util/device-logs/dsonano.lsusb @@ -1,60 +1,60 @@ - -Bus 002 Device 004: ID 0483:df11 SGS Thomson Microelectronics -Device Descriptor: - bLength 18 - bDescriptorType 1 - bcdUSB 1.00 - bDeviceClass 0 (Defined at Interface level) - bDeviceSubClass 0 - bDeviceProtocol 0 - bMaxPacketSize0 64 - idVendor 0x0483 SGS Thomson Microelectronics - idProduct 0xdf11 - bcdDevice 1.1a - iManufacturer 1 STMicroelectronics - iProduct 2 STM32 DFU - iSerial 3 001 - bNumConfigurations 1 - Configuration Descriptor: - bLength 9 - bDescriptorType 2 - wTotalLength 36 - bNumInterfaces 1 - bConfigurationValue 1 - iConfiguration 0 - bmAttributes 0x80 - (Bus Powered) - MaxPower 64mA - Interface Descriptor: - bLength 9 - bDescriptorType 4 - bInterfaceNumber 0 - bAlternateSetting 0 - bNumEndpoints 0 - bInterfaceClass 254 Application Specific Interface - bInterfaceSubClass 1 Device Firmware Update - bInterfaceProtocol 0 - iInterface 4 @Internal Flash /0x08000000/12*001Ka,116*001Kg - Interface Descriptor: - bLength 9 - bDescriptorType 4 - bInterfaceNumber 0 - bAlternateSetting 1 - bNumEndpoints 0 - bInterfaceClass 254 Application Specific Interface - bInterfaceSubClass 1 Device Firmware Update - bInterfaceProtocol 0 - iInterface 5 @SPI Flash : M25P64/0x00000000/64*064Kg,64*064Kg - Device Firmware Upgrade Interface Descriptor: - bLength 9 - bDescriptorType 33 - bmAttributes 11 - Will Detach - Manifestation Intolerant - Upload Supported - Download Supported - wDetachTimeout 255 milliseconds - wTransferSize 1024 bytes - bcdDFUVersion 1.1a -Device Status: 0x0000 - (Bus Powered) + +Bus 002 Device 004: ID 0483:df11 SGS Thomson Microelectronics +Device Descriptor: + bLength 18 + bDescriptorType 1 + bcdUSB 1.00 + bDeviceClass 0 (Defined at Interface level) + bDeviceSubClass 0 + bDeviceProtocol 0 + bMaxPacketSize0 64 + idVendor 0x0483 SGS Thomson Microelectronics + idProduct 0xdf11 + bcdDevice 1.1a + iManufacturer 1 STMicroelectronics + iProduct 2 STM32 DFU + iSerial 3 001 + bNumConfigurations 1 + Configuration Descriptor: + bLength 9 + bDescriptorType 2 + wTotalLength 36 + bNumInterfaces 1 + bConfigurationValue 1 + iConfiguration 0 + bmAttributes 0x80 + (Bus Powered) + MaxPower 64mA + Interface Descriptor: + bLength 9 + bDescriptorType 4 + bInterfaceNumber 0 + bAlternateSetting 0 + bNumEndpoints 0 + bInterfaceClass 254 Application Specific Interface + bInterfaceSubClass 1 Device Firmware Update + bInterfaceProtocol 0 + iInterface 4 @Internal Flash /0x08000000/12*001Ka,116*001Kg + Interface Descriptor: + bLength 9 + bDescriptorType 4 + bInterfaceNumber 0 + bAlternateSetting 1 + bNumEndpoints 0 + bInterfaceClass 254 Application Specific Interface + bInterfaceSubClass 1 Device Firmware Update + bInterfaceProtocol 0 + iInterface 5 @SPI Flash : M25P64/0x00000000/64*064Kg,64*064Kg + Device Firmware Upgrade Interface Descriptor: + bLength 9 + bDescriptorType 33 + bmAttributes 11 + Will Detach + Manifestation Intolerant + Upload Supported + Download Supported + wDetachTimeout 255 milliseconds + wTransferSize 1024 bytes + bcdDFUVersion 1.1a +Device Status: 0x0000 + (Bus Powered) diff --git a/tools/src/dfu-util/device-logs/lpclink.log b/tools/linux/src/dfu-util/device-logs/lpclink.log old mode 100755 new mode 100644 similarity index 97% rename from tools/src/dfu-util/device-logs/lpclink.log rename to tools/linux/src/dfu-util/device-logs/lpclink.log index f2a04f7..7de4dd3 --- a/tools/src/dfu-util/device-logs/lpclink.log +++ b/tools/linux/src/dfu-util/device-logs/lpclink.log @@ -1,59 +1,59 @@ -(The on-board LPC3154 has some encryption key set and LPCXpressoWIN.enc -is encrypted.) - -$ lsusb | grep NXP -Bus 003 Device 011: ID 0471:df55 Philips (or NXP) LPCXpresso LPC-Link - -$ dfu-util -v -v -v -R -D /opt/lpc/lpcxpresso/bin/LPCXpressoWIN.enc - -dfu-util 0.7 - -Copyright 2005-2008 Weston Schmidt, Harald Welte and OpenMoko Inc. -Copyright 2010-2012 Tormod Volden and Stefan Schmidt -This program is Free Software and has ABSOLUTELY NO WARRANTY -Please report bugs to dfu-util@lists.gnumonks.org - -dfu-util: Invalid DFU suffix signature -dfu-util: A valid DFU suffix will be required in a future dfu-util release!!! -Deducing device DFU version from functional descriptor length -Opening DFU capable USB device... -ID 0471:df55 -Run-time device DFU version 0100 -Claiming USB DFU Runtime Interface... -Determining device status: -state = dfuIDLE, status = 0 -dfu-util: WARNING: Runtime device already in DFU state ?!? -Claiming USB DFU Interface... -Setting Alternate Setting #0 ... -Determining device status: -state = dfuIDLE, status = 0 -dfuIDLE, continuing -DFU mode device DFU version 0100 -Device returned transfer size 2048 -Copying data from PC to DFU device -Download [ ] 0% 0 bytes -Download [= ] 6% 2048 bytes -Download [=== ] 13% 4096 bytes -Download [==== ] 19% 6144 bytes -Download [====== ] 26% 8192 bytes -Download [======== ] 32% 10240 bytes -Download [========= ] 39% 12288 bytes -Download [=========== ] 45% 14336 bytes -Download [============= ] 52% 16384 bytes -Download [============== ] 59% 18432 bytes -Download [================ ] 65% 20480 bytes -Download [================== ] 72% 22528 bytes -Download [=================== ] 78% 24576 bytes -Download [===================== ] 85% 26624 bytes -Download [====================== ] 91% 28672 bytes -Download [======================== ] 98% 29192 bytes -Download [=========================] 100% 29192 bytes -Download done. -Sent a total of 29192 bytes -state(8) = dfuMANIFEST-WAIT-RESET, status(0) = No error condition is present -Done! -dfu-util: can't detach -Resetting USB to switch back to runtime mode - -$ lsusb | grep NXP -Bus 003 Device 012: ID 1fc9:0009 NXP Semiconductors +(The on-board LPC3154 has some encryption key set and LPCXpressoWIN.enc +is encrypted.) + +$ lsusb | grep NXP +Bus 003 Device 011: ID 0471:df55 Philips (or NXP) LPCXpresso LPC-Link + +$ dfu-util -v -v -v -R -D /opt/lpc/lpcxpresso/bin/LPCXpressoWIN.enc + +dfu-util 0.7 + +Copyright 2005-2008 Weston Schmidt, Harald Welte and OpenMoko Inc. +Copyright 2010-2012 Tormod Volden and Stefan Schmidt +This program is Free Software and has ABSOLUTELY NO WARRANTY +Please report bugs to dfu-util@lists.gnumonks.org + +dfu-util: Invalid DFU suffix signature +dfu-util: A valid DFU suffix will be required in a future dfu-util release!!! +Deducing device DFU version from functional descriptor length +Opening DFU capable USB device... +ID 0471:df55 +Run-time device DFU version 0100 +Claiming USB DFU Runtime Interface... +Determining device status: +state = dfuIDLE, status = 0 +dfu-util: WARNING: Runtime device already in DFU state ?!? +Claiming USB DFU Interface... +Setting Alternate Setting #0 ... +Determining device status: +state = dfuIDLE, status = 0 +dfuIDLE, continuing +DFU mode device DFU version 0100 +Device returned transfer size 2048 +Copying data from PC to DFU device +Download [ ] 0% 0 bytes +Download [= ] 6% 2048 bytes +Download [=== ] 13% 4096 bytes +Download [==== ] 19% 6144 bytes +Download [====== ] 26% 8192 bytes +Download [======== ] 32% 10240 bytes +Download [========= ] 39% 12288 bytes +Download [=========== ] 45% 14336 bytes +Download [============= ] 52% 16384 bytes +Download [============== ] 59% 18432 bytes +Download [================ ] 65% 20480 bytes +Download [================== ] 72% 22528 bytes +Download [=================== ] 78% 24576 bytes +Download [===================== ] 85% 26624 bytes +Download [====================== ] 91% 28672 bytes +Download [======================== ] 98% 29192 bytes +Download [=========================] 100% 29192 bytes +Download done. +Sent a total of 29192 bytes +state(8) = dfuMANIFEST-WAIT-RESET, status(0) = No error condition is present +Done! +dfu-util: can't detach +Resetting USB to switch back to runtime mode + +$ lsusb | grep NXP +Bus 003 Device 012: ID 1fc9:0009 NXP Semiconductors diff --git a/tools/src/dfu-util/device-logs/lpclink.lsusb b/tools/linux/src/dfu-util/device-logs/lpclink.lsusb old mode 100755 new mode 100644 similarity index 97% rename from tools/src/dfu-util/device-logs/lpclink.lsusb rename to tools/linux/src/dfu-util/device-logs/lpclink.lsusb index c80d2b7..867b2a2 --- a/tools/src/dfu-util/device-logs/lpclink.lsusb +++ b/tools/linux/src/dfu-util/device-logs/lpclink.lsusb @@ -1,58 +1,58 @@ - -Bus 003 Device 008: ID 0471:df55 Philips (or NXP) LPCXpresso LPC-Link -Device Descriptor: - bLength 18 - bDescriptorType 1 - bcdUSB 2.00 - bDeviceClass 0 (Defined at Interface level) - bDeviceSubClass 0 - bDeviceProtocol 0 - bMaxPacketSize0 64 - idVendor 0x0471 Philips (or NXP) - idProduct 0xdf55 LPCXpresso LPC-Link - bcdDevice 0.01 - iManufacturer 0 - iProduct 0 - iSerial 0 - bNumConfigurations 1 - Configuration Descriptor: - bLength 9 - bDescriptorType 2 - wTotalLength 25 - bNumInterfaces 1 - bConfigurationValue 1 - iConfiguration 0 - bmAttributes 0x80 - (Bus Powered) - MaxPower 100mA - Interface Descriptor: - bLength 9 - bDescriptorType 4 - bInterfaceNumber 0 - bAlternateSetting 0 - bNumEndpoints 0 - bInterfaceClass 254 Application Specific Interface - bInterfaceSubClass 1 Device Firmware Update - bInterfaceProtocol 0 - iInterface 0 - Device Firmware Upgrade Interface Descriptor: - bLength 7 - bDescriptorType 33 - bmAttributes 1 - Will Not Detach - Manifestation Intolerant - Upload Unsupported - Download Supported - wDetachTimeout 65535 milliseconds - wTransferSize 2048 bytes -Device Qualifier (for other device speed): - bLength 10 - bDescriptorType 6 - bcdUSB 2.00 - bDeviceClass 0 (Defined at Interface level) - bDeviceSubClass 0 - bDeviceProtocol 0 - bMaxPacketSize0 64 - bNumConfigurations 1 -Device Status: 0x0000 - (Bus Powered) + +Bus 003 Device 008: ID 0471:df55 Philips (or NXP) LPCXpresso LPC-Link +Device Descriptor: + bLength 18 + bDescriptorType 1 + bcdUSB 2.00 + bDeviceClass 0 (Defined at Interface level) + bDeviceSubClass 0 + bDeviceProtocol 0 + bMaxPacketSize0 64 + idVendor 0x0471 Philips (or NXP) + idProduct 0xdf55 LPCXpresso LPC-Link + bcdDevice 0.01 + iManufacturer 0 + iProduct 0 + iSerial 0 + bNumConfigurations 1 + Configuration Descriptor: + bLength 9 + bDescriptorType 2 + wTotalLength 25 + bNumInterfaces 1 + bConfigurationValue 1 + iConfiguration 0 + bmAttributes 0x80 + (Bus Powered) + MaxPower 100mA + Interface Descriptor: + bLength 9 + bDescriptorType 4 + bInterfaceNumber 0 + bAlternateSetting 0 + bNumEndpoints 0 + bInterfaceClass 254 Application Specific Interface + bInterfaceSubClass 1 Device Firmware Update + bInterfaceProtocol 0 + iInterface 0 + Device Firmware Upgrade Interface Descriptor: + bLength 7 + bDescriptorType 33 + bmAttributes 1 + Will Not Detach + Manifestation Intolerant + Upload Unsupported + Download Supported + wDetachTimeout 65535 milliseconds + wTransferSize 2048 bytes +Device Qualifier (for other device speed): + bLength 10 + bDescriptorType 6 + bcdUSB 2.00 + bDeviceClass 0 (Defined at Interface level) + bDeviceSubClass 0 + bDeviceProtocol 0 + bMaxPacketSize0 64 + bNumConfigurations 1 +Device Status: 0x0000 + (Bus Powered) diff --git a/tools/src/dfu-util/device-logs/lpclink2.log b/tools/linux/src/dfu-util/device-logs/lpclink2.log old mode 100755 new mode 100644 similarity index 97% rename from tools/src/dfu-util/device-logs/lpclink2.log rename to tools/linux/src/dfu-util/device-logs/lpclink2.log index f33e671..4681eff --- a/tools/src/dfu-util/device-logs/lpclink2.log +++ b/tools/linux/src/dfu-util/device-logs/lpclink2.log @@ -1,59 +1,59 @@ -$ lsusb | grep NXP -Bus 003 Device 013: ID 1fc9:000c NXP Semiconductors - -$ dfu-util -D ~/devel/dfu-util/firmware.bin.qthdr - -dfu-util 0.7 - -Copyright 2005-2008 Weston Schmidt, Harald Welte and OpenMoko Inc. -Copyright 2010-2012 Tormod Volden and Stefan Schmidt -This program is Free Software and has ABSOLUTELY NO WARRANTY -Please report bugs to dfu-util@lists.gnumonks.org - -dfu-util: Invalid DFU suffix signature -dfu-util: A valid DFU suffix will be required in a future dfu-util release!!! -Possible unencryptes NXP LPC DFU prefix with the following properties -Payload length: 39 kiByte -Opening DFU capable USB device... -ID 1fc9:000c -Run-time device DFU version 0100 -Claiming USB DFU Runtime Interface... -Determining device status: -state = dfuIDLE, status = 0 -dfu-util: WARNING: Runtime device already in DFU state ?!? -Claiming USB DFU Interface... -Setting Alternate Setting #0 ... -Determining device status: -state = dfuIDLE, status = 0 -dfuIDLE, continuing -DFU mode device DFU version 0100 -Device returned transfer size 2048 -Copying data from PC to DFU device -Download [ ] 0% 0 bytes -Download [= ] 4% 2048 bytes -Download [== ] 9% 4096 bytes -Download [=== ] 14% 6144 bytes -Download [==== ] 19% 8192 bytes -Download [====== ] 24% 10240 bytes -Download [======= ] 28% 12288 bytes -Download [======== ] 33% 14336 bytes -Download [========= ] 38% 16384 bytes -Download [========== ] 43% 18432 bytes -Download [============ ] 48% 20480 bytes -Download [============= ] 53% 22528 bytes -Download [============== ] 57% 24576 bytes -Download [=============== ] 62% 26624 bytes -Download [================ ] 67% 28672 bytes -Download [================== ] 72% 30720 bytes -Download [=================== ] 77% 32768 bytes -Download [==================== ] 82% 34816 bytes -Download [===================== ] 86% 36864 bytes -Download [====================== ] 91% 38912 bytes -Download [======================== ] 96% 40356 bytes -Download [=========================] 100% 40356 bytes -Download done. -Sent a total of 40356 bytes -dfu-util: unable to read DFU status - -$ lsusb | grep NXP -Bus 003 Device 014: ID 1fc9:0018 NXP Semiconductors +$ lsusb | grep NXP +Bus 003 Device 013: ID 1fc9:000c NXP Semiconductors + +$ dfu-util -D ~/devel/dfu-util/firmware.bin.qthdr + +dfu-util 0.7 + +Copyright 2005-2008 Weston Schmidt, Harald Welte and OpenMoko Inc. +Copyright 2010-2012 Tormod Volden and Stefan Schmidt +This program is Free Software and has ABSOLUTELY NO WARRANTY +Please report bugs to dfu-util@lists.gnumonks.org + +dfu-util: Invalid DFU suffix signature +dfu-util: A valid DFU suffix will be required in a future dfu-util release!!! +Possible unencryptes NXP LPC DFU prefix with the following properties +Payload length: 39 kiByte +Opening DFU capable USB device... +ID 1fc9:000c +Run-time device DFU version 0100 +Claiming USB DFU Runtime Interface... +Determining device status: +state = dfuIDLE, status = 0 +dfu-util: WARNING: Runtime device already in DFU state ?!? +Claiming USB DFU Interface... +Setting Alternate Setting #0 ... +Determining device status: +state = dfuIDLE, status = 0 +dfuIDLE, continuing +DFU mode device DFU version 0100 +Device returned transfer size 2048 +Copying data from PC to DFU device +Download [ ] 0% 0 bytes +Download [= ] 4% 2048 bytes +Download [== ] 9% 4096 bytes +Download [=== ] 14% 6144 bytes +Download [==== ] 19% 8192 bytes +Download [====== ] 24% 10240 bytes +Download [======= ] 28% 12288 bytes +Download [======== ] 33% 14336 bytes +Download [========= ] 38% 16384 bytes +Download [========== ] 43% 18432 bytes +Download [============ ] 48% 20480 bytes +Download [============= ] 53% 22528 bytes +Download [============== ] 57% 24576 bytes +Download [=============== ] 62% 26624 bytes +Download [================ ] 67% 28672 bytes +Download [================== ] 72% 30720 bytes +Download [=================== ] 77% 32768 bytes +Download [==================== ] 82% 34816 bytes +Download [===================== ] 86% 36864 bytes +Download [====================== ] 91% 38912 bytes +Download [======================== ] 96% 40356 bytes +Download [=========================] 100% 40356 bytes +Download done. +Sent a total of 40356 bytes +dfu-util: unable to read DFU status + +$ lsusb | grep NXP +Bus 003 Device 014: ID 1fc9:0018 NXP Semiconductors diff --git a/tools/src/dfu-util/device-logs/lpclink2.lsusb b/tools/linux/src/dfu-util/device-logs/lpclink2.lsusb old mode 100755 new mode 100644 similarity index 97% rename from tools/src/dfu-util/device-logs/lpclink2.lsusb rename to tools/linux/src/dfu-util/device-logs/lpclink2.lsusb index d8d6e0c..b833fca --- a/tools/src/dfu-util/device-logs/lpclink2.lsusb +++ b/tools/linux/src/dfu-util/device-logs/lpclink2.lsusb @@ -1,203 +1,203 @@ - -Bus 003 Device 007: ID 0c72:000c PEAK System PCAN-USB -Device Descriptor: - bLength 18 - bDescriptorType 1 - bcdUSB 1.00 - bDeviceClass 0 (Defined at Interface level) - bDeviceSubClass 0 - bDeviceProtocol 0 - bMaxPacketSize0 16 - idVendor 0x0c72 PEAK System - idProduct 0x000c PCAN-USB - bcdDevice 1c.ff - iManufacturer 0 - iProduct 3 VER1:PEAK -VER2:02.8.01 -DAT :06.05.2004 -TIME:09:35:37 - ... - iSerial 0 - bNumConfigurations 3 - Configuration Descriptor: - bLength 9 - bDescriptorType 2 - wTotalLength 46 - bNumInterfaces 1 - bConfigurationValue 1 - iConfiguration 0 - bmAttributes 0x80 - (Bus Powered) - MaxPower 200mA - Interface Descriptor: - bLength 9 - bDescriptorType 4 - bInterfaceNumber 0 - bAlternateSetting 0 - bNumEndpoints 4 - bInterfaceClass 0 (Defined at Interface level) - bInterfaceSubClass 0 - bInterfaceProtocol 0 - iInterface 0 - Endpoint Descriptor: - bLength 7 - bDescriptorType 5 - bEndpointAddress 0x81 EP 1 IN - bmAttributes 2 - Transfer Type Bulk - Synch Type None - Usage Type Data - wMaxPacketSize 0x0010 1x 16 bytes - bInterval 20 - Endpoint Descriptor: - bLength 7 - bDescriptorType 5 - bEndpointAddress 0x01 EP 1 OUT - bmAttributes 2 - Transfer Type Bulk - Synch Type None - Usage Type Data - wMaxPacketSize 0x0010 1x 16 bytes - bInterval 20 - Endpoint Descriptor: - bLength 7 - bDescriptorType 5 - bEndpointAddress 0x82 EP 2 IN - bmAttributes 2 - Transfer Type Bulk - Synch Type None - Usage Type Data - wMaxPacketSize 0x0040 1x 64 bytes - bInterval 1 - Endpoint Descriptor: - bLength 7 - bDescriptorType 5 - bEndpointAddress 0x02 EP 2 OUT - bmAttributes 2 - Transfer Type Bulk - Synch Type None - Usage Type Data - wMaxPacketSize 0x0040 1x 64 bytes - bInterval 1 - Configuration Descriptor: - bLength 9 - bDescriptorType 2 - wTotalLength 46 - bNumInterfaces 1 - bConfigurationValue 2 - iConfiguration 0 - bmAttributes 0x80 - (Bus Powered) - MaxPower 394mA - Interface Descriptor: - bLength 9 - bDescriptorType 4 - bInterfaceNumber 0 - bAlternateSetting 0 - bNumEndpoints 4 - bInterfaceClass 0 (Defined at Interface level) - bInterfaceSubClass 0 - bInterfaceProtocol 0 - iInterface 0 - Endpoint Descriptor: - bLength 7 - bDescriptorType 5 - bEndpointAddress 0x81 EP 1 IN - bmAttributes 2 - Transfer Type Bulk - Synch Type None - Usage Type Data - wMaxPacketSize 0x0010 1x 16 bytes - bInterval 20 - Endpoint Descriptor: - bLength 7 - bDescriptorType 5 - bEndpointAddress 0x01 EP 1 OUT - bmAttributes 2 - Transfer Type Bulk - Synch Type None - Usage Type Data - wMaxPacketSize 0x0010 1x 16 bytes - bInterval 20 - Endpoint Descriptor: - bLength 7 - bDescriptorType 5 - bEndpointAddress 0x82 EP 2 IN - bmAttributes 2 - Transfer Type Bulk - Synch Type None - Usage Type Data - wMaxPacketSize 0x0040 1x 64 bytes - bInterval 1 - Endpoint Descriptor: - bLength 7 - bDescriptorType 5 - bEndpointAddress 0x02 EP 2 OUT - bmAttributes 2 - Transfer Type Bulk - Synch Type None - Usage Type Data - wMaxPacketSize 0x0040 1x 64 bytes - bInterval 1 - Configuration Descriptor: - bLength 9 - bDescriptorType 2 - wTotalLength 46 - bNumInterfaces 1 - bConfigurationValue 3 - iConfiguration 0 - bmAttributes 0x80 - (Bus Powered) - MaxPower 200mA - Interface Descriptor: - bLength 9 - bDescriptorType 4 - bInterfaceNumber 0 - bAlternateSetting 0 - bNumEndpoints 4 - bInterfaceClass 0 (Defined at Interface level) - bInterfaceSubClass 0 - bInterfaceProtocol 0 - iInterface 0 - Endpoint Descriptor: - bLength 7 - bDescriptorType 5 - bEndpointAddress 0x81 EP 1 IN - bmAttributes 3 - Transfer Type Interrupt - Synch Type None - Usage Type Data - wMaxPacketSize 0x0010 1x 16 bytes - bInterval 1 - Endpoint Descriptor: - bLength 7 - bDescriptorType 5 - bEndpointAddress 0x01 EP 1 OUT - bmAttributes 3 - Transfer Type Interrupt - Synch Type None - Usage Type Data - wMaxPacketSize 0x0010 1x 16 bytes - bInterval 1 - Endpoint Descriptor: - bLength 7 - bDescriptorType 5 - bEndpointAddress 0x82 EP 2 IN - bmAttributes 2 - Transfer Type Bulk - Synch Type None - Usage Type Data - wMaxPacketSize 0x0040 1x 64 bytes - bInterval 1 - Endpoint Descriptor: - bLength 7 - bDescriptorType 5 - bEndpointAddress 0x02 EP 2 OUT - bmAttributes 2 - Transfer Type Bulk - Synch Type None - Usage Type Data - wMaxPacketSize 0x0040 1x 64 bytes - bInterval 1 -Device Status: 0x0001 - Self Powered + +Bus 003 Device 007: ID 0c72:000c PEAK System PCAN-USB +Device Descriptor: + bLength 18 + bDescriptorType 1 + bcdUSB 1.00 + bDeviceClass 0 (Defined at Interface level) + bDeviceSubClass 0 + bDeviceProtocol 0 + bMaxPacketSize0 16 + idVendor 0x0c72 PEAK System + idProduct 0x000c PCAN-USB + bcdDevice 1c.ff + iManufacturer 0 + iProduct 3 VER1:PEAK +VER2:02.8.01 +DAT :06.05.2004 +TIME:09:35:37 + ... + iSerial 0 + bNumConfigurations 3 + Configuration Descriptor: + bLength 9 + bDescriptorType 2 + wTotalLength 46 + bNumInterfaces 1 + bConfigurationValue 1 + iConfiguration 0 + bmAttributes 0x80 + (Bus Powered) + MaxPower 200mA + Interface Descriptor: + bLength 9 + bDescriptorType 4 + bInterfaceNumber 0 + bAlternateSetting 0 + bNumEndpoints 4 + bInterfaceClass 0 (Defined at Interface level) + bInterfaceSubClass 0 + bInterfaceProtocol 0 + iInterface 0 + Endpoint Descriptor: + bLength 7 + bDescriptorType 5 + bEndpointAddress 0x81 EP 1 IN + bmAttributes 2 + Transfer Type Bulk + Synch Type None + Usage Type Data + wMaxPacketSize 0x0010 1x 16 bytes + bInterval 20 + Endpoint Descriptor: + bLength 7 + bDescriptorType 5 + bEndpointAddress 0x01 EP 1 OUT + bmAttributes 2 + Transfer Type Bulk + Synch Type None + Usage Type Data + wMaxPacketSize 0x0010 1x 16 bytes + bInterval 20 + Endpoint Descriptor: + bLength 7 + bDescriptorType 5 + bEndpointAddress 0x82 EP 2 IN + bmAttributes 2 + Transfer Type Bulk + Synch Type None + Usage Type Data + wMaxPacketSize 0x0040 1x 64 bytes + bInterval 1 + Endpoint Descriptor: + bLength 7 + bDescriptorType 5 + bEndpointAddress 0x02 EP 2 OUT + bmAttributes 2 + Transfer Type Bulk + Synch Type None + Usage Type Data + wMaxPacketSize 0x0040 1x 64 bytes + bInterval 1 + Configuration Descriptor: + bLength 9 + bDescriptorType 2 + wTotalLength 46 + bNumInterfaces 1 + bConfigurationValue 2 + iConfiguration 0 + bmAttributes 0x80 + (Bus Powered) + MaxPower 394mA + Interface Descriptor: + bLength 9 + bDescriptorType 4 + bInterfaceNumber 0 + bAlternateSetting 0 + bNumEndpoints 4 + bInterfaceClass 0 (Defined at Interface level) + bInterfaceSubClass 0 + bInterfaceProtocol 0 + iInterface 0 + Endpoint Descriptor: + bLength 7 + bDescriptorType 5 + bEndpointAddress 0x81 EP 1 IN + bmAttributes 2 + Transfer Type Bulk + Synch Type None + Usage Type Data + wMaxPacketSize 0x0010 1x 16 bytes + bInterval 20 + Endpoint Descriptor: + bLength 7 + bDescriptorType 5 + bEndpointAddress 0x01 EP 1 OUT + bmAttributes 2 + Transfer Type Bulk + Synch Type None + Usage Type Data + wMaxPacketSize 0x0010 1x 16 bytes + bInterval 20 + Endpoint Descriptor: + bLength 7 + bDescriptorType 5 + bEndpointAddress 0x82 EP 2 IN + bmAttributes 2 + Transfer Type Bulk + Synch Type None + Usage Type Data + wMaxPacketSize 0x0040 1x 64 bytes + bInterval 1 + Endpoint Descriptor: + bLength 7 + bDescriptorType 5 + bEndpointAddress 0x02 EP 2 OUT + bmAttributes 2 + Transfer Type Bulk + Synch Type None + Usage Type Data + wMaxPacketSize 0x0040 1x 64 bytes + bInterval 1 + Configuration Descriptor: + bLength 9 + bDescriptorType 2 + wTotalLength 46 + bNumInterfaces 1 + bConfigurationValue 3 + iConfiguration 0 + bmAttributes 0x80 + (Bus Powered) + MaxPower 200mA + Interface Descriptor: + bLength 9 + bDescriptorType 4 + bInterfaceNumber 0 + bAlternateSetting 0 + bNumEndpoints 4 + bInterfaceClass 0 (Defined at Interface level) + bInterfaceSubClass 0 + bInterfaceProtocol 0 + iInterface 0 + Endpoint Descriptor: + bLength 7 + bDescriptorType 5 + bEndpointAddress 0x81 EP 1 IN + bmAttributes 3 + Transfer Type Interrupt + Synch Type None + Usage Type Data + wMaxPacketSize 0x0010 1x 16 bytes + bInterval 1 + Endpoint Descriptor: + bLength 7 + bDescriptorType 5 + bEndpointAddress 0x01 EP 1 OUT + bmAttributes 3 + Transfer Type Interrupt + Synch Type None + Usage Type Data + wMaxPacketSize 0x0010 1x 16 bytes + bInterval 1 + Endpoint Descriptor: + bLength 7 + bDescriptorType 5 + bEndpointAddress 0x82 EP 2 IN + bmAttributes 2 + Transfer Type Bulk + Synch Type None + Usage Type Data + wMaxPacketSize 0x0040 1x 64 bytes + bInterval 1 + Endpoint Descriptor: + bLength 7 + bDescriptorType 5 + bEndpointAddress 0x02 EP 2 OUT + bmAttributes 2 + Transfer Type Bulk + Synch Type None + Usage Type Data + wMaxPacketSize 0x0040 1x 64 bytes + bInterval 1 +Device Status: 0x0001 + Self Powered diff --git a/tools/src/dfu-util/device-logs/opc-20.lsusb b/tools/linux/src/dfu-util/device-logs/opc-20.lsusb old mode 100755 new mode 100644 similarity index 97% rename from tools/src/dfu-util/device-logs/opc-20.lsusb rename to tools/linux/src/dfu-util/device-logs/opc-20.lsusb index 25ad12f..580df90 --- a/tools/src/dfu-util/device-logs/opc-20.lsusb +++ b/tools/linux/src/dfu-util/device-logs/opc-20.lsusb @@ -1,60 +1,60 @@ - -Bus 001 Device 004: ID 0483:df11 SGS Thomson Microelectronics -Device Descriptor: - bLength 18 - bDescriptorType 1 - bcdUSB 1.00 - bDeviceClass 0 (Defined at Interface level) - bDeviceSubClass 0 - bDeviceProtocol 0 - bMaxPacketSize0 64 - idVendor 0x0483 SGS Thomson Microelectronics - idProduct 0xdf11 - bcdDevice 2.00 - iManufacturer 1 STMicroelectronics - iProduct 2 STM32 DFU - iSerial 3 ÿÿÿÿÿÿÿÿÿÿÿÿ - bNumConfigurations 1 - Configuration Descriptor: - bLength 9 - bDescriptorType 2 - wTotalLength 36 - bNumInterfaces 1 - bConfigurationValue 1 - iConfiguration 0 - bmAttributes 0xc0 - Self Powered - MaxPower 100mA - Interface Descriptor: - bLength 9 - bDescriptorType 4 - bInterfaceNumber 0 - bAlternateSetting 0 - bNumEndpoints 0 - bInterfaceClass 254 Application Specific Interface - bInterfaceSubClass 1 Device Firmware Update - bInterfaceProtocol 2 - iInterface 4 @Internal Flash /0x08000000/12*001Ka,116*001Kg - Interface Descriptor: - bLength 9 - bDescriptorType 4 - bInterfaceNumber 0 - bAlternateSetting 1 - bNumEndpoints 0 - bInterfaceClass 254 Application Specific Interface - bInterfaceSubClass 1 Device Firmware Update - bInterfaceProtocol 2 - iInterface 5 @SPI Flash : M25P64/0x00000000/128*64Kg - Device Firmware Upgrade Interface Descriptor: - bLength 9 - bDescriptorType 33 - bmAttributes 11 - Will Detach - Manifestation Intolerant - Upload Supported - Download Supported - wDetachTimeout 255 milliseconds - wTransferSize 1024 bytes - bcdDFUVersion 1a.01 -Device Status: 0x0001 - Self Powered + +Bus 001 Device 004: ID 0483:df11 SGS Thomson Microelectronics +Device Descriptor: + bLength 18 + bDescriptorType 1 + bcdUSB 1.00 + bDeviceClass 0 (Defined at Interface level) + bDeviceSubClass 0 + bDeviceProtocol 0 + bMaxPacketSize0 64 + idVendor 0x0483 SGS Thomson Microelectronics + idProduct 0xdf11 + bcdDevice 2.00 + iManufacturer 1 STMicroelectronics + iProduct 2 STM32 DFU + iSerial 3 ÿÿÿÿÿÿÿÿÿÿÿÿ + bNumConfigurations 1 + Configuration Descriptor: + bLength 9 + bDescriptorType 2 + wTotalLength 36 + bNumInterfaces 1 + bConfigurationValue 1 + iConfiguration 0 + bmAttributes 0xc0 + Self Powered + MaxPower 100mA + Interface Descriptor: + bLength 9 + bDescriptorType 4 + bInterfaceNumber 0 + bAlternateSetting 0 + bNumEndpoints 0 + bInterfaceClass 254 Application Specific Interface + bInterfaceSubClass 1 Device Firmware Update + bInterfaceProtocol 2 + iInterface 4 @Internal Flash /0x08000000/12*001Ka,116*001Kg + Interface Descriptor: + bLength 9 + bDescriptorType 4 + bInterfaceNumber 0 + bAlternateSetting 1 + bNumEndpoints 0 + bInterfaceClass 254 Application Specific Interface + bInterfaceSubClass 1 Device Firmware Update + bInterfaceProtocol 2 + iInterface 5 @SPI Flash : M25P64/0x00000000/128*64Kg + Device Firmware Upgrade Interface Descriptor: + bLength 9 + bDescriptorType 33 + bmAttributes 11 + Will Detach + Manifestation Intolerant + Upload Supported + Download Supported + wDetachTimeout 255 milliseconds + wTransferSize 1024 bytes + bcdDFUVersion 1a.01 +Device Status: 0x0001 + Self Powered diff --git a/tools/src/dfu-util/device-logs/openmoko-freerunner-dfumode.lsusb b/tools/linux/src/dfu-util/device-logs/openmoko-freerunner-dfumode.lsusb old mode 100755 new mode 100644 similarity index 97% rename from tools/src/dfu-util/device-logs/openmoko-freerunner-dfumode.lsusb rename to tools/linux/src/dfu-util/device-logs/openmoko-freerunner-dfumode.lsusb index 64bd018..4c0abfb --- a/tools/src/dfu-util/device-logs/openmoko-freerunner-dfumode.lsusb +++ b/tools/linux/src/dfu-util/device-logs/openmoko-freerunner-dfumode.lsusb @@ -1,109 +1,109 @@ -Bus 003 Device 017: ID 1d50:5119 OpenMoko, Inc. GTA01/GTA02 U-Boot Bootloader -Device Descriptor: - bLength 18 - bDescriptorType 1 - bcdUSB 1.00 - bDeviceClass 0 (Defined at Interface level) - bDeviceSubClass 0 - bDeviceProtocol 0 - bMaxPacketSize0 16 - idVendor 0x1d50 OpenMoko, Inc. - idProduct 0x5119 GTA01/GTA02 U-Boot Bootloader - bcdDevice 0.00 - iManufacturer 1 OpenMoko, Inc - iProduct 2 Neo1973 Bootloader U-Boot 1.3.2-moko12 - iSerial 3 0000000 - bNumConfigurations 1 - Configuration Descriptor: - bLength 9 - bDescriptorType 2 - wTotalLength 81 - bNumInterfaces 1 - bConfigurationValue 1 - iConfiguration 7 USB Device Firmware Upgrade - bmAttributes 0x80 - (Bus Powered) - MaxPower 100mA - Interface Descriptor: - bLength 9 - bDescriptorType 4 - bInterfaceNumber 0 - bAlternateSetting 0 - bNumEndpoints 0 - bInterfaceClass 254 Application Specific Interface - bInterfaceSubClass 1 Device Firmware Update - bInterfaceProtocol 2 - iInterface 8 RAM 0x32000000 - Interface Descriptor: - bLength 9 - bDescriptorType 4 - bInterfaceNumber 0 - bAlternateSetting 1 - bNumEndpoints 0 - bInterfaceClass 254 Application Specific Interface - bInterfaceSubClass 1 Device Firmware Update - bInterfaceProtocol 2 - iInterface 9 u-boot - Interface Descriptor: - bLength 9 - bDescriptorType 4 - bInterfaceNumber 0 - bAlternateSetting 2 - bNumEndpoints 0 - bInterfaceClass 254 Application Specific Interface - bInterfaceSubClass 1 Device Firmware Update - bInterfaceProtocol 2 - iInterface 10 u-boot_env - Interface Descriptor: - bLength 9 - bDescriptorType 4 - bInterfaceNumber 0 - bAlternateSetting 3 - bNumEndpoints 0 - bInterfaceClass 254 Application Specific Interface - bInterfaceSubClass 1 Device Firmware Update - bInterfaceProtocol 2 - iInterface 11 kernel - Interface Descriptor: - bLength 9 - bDescriptorType 4 - bInterfaceNumber 0 - bAlternateSetting 4 - bNumEndpoints 0 - bInterfaceClass 254 Application Specific Interface - bInterfaceSubClass 1 Device Firmware Update - bInterfaceProtocol 2 - iInterface 12 splash - Interface Descriptor: - bLength 9 - bDescriptorType 4 - bInterfaceNumber 0 - bAlternateSetting 5 - bNumEndpoints 0 - bInterfaceClass 254 Application Specific Interface - bInterfaceSubClass 1 Device Firmware Update - bInterfaceProtocol 2 - iInterface 13 factory - Interface Descriptor: - bLength 9 - bDescriptorType 4 - bInterfaceNumber 0 - bAlternateSetting 6 - bNumEndpoints 0 - bInterfaceClass 254 Application Specific Interface - bInterfaceSubClass 1 Device Firmware Update - bInterfaceProtocol 2 - iInterface 14 rootfs - Device Firmware Upgrade Interface Descriptor: - bLength 9 - bDescriptorType 33 - bmAttributes 7 - Will Not Detach - Manifestation Tolerant - Upload Supported - Download Supported - wDetachTimeout 65280 milliseconds - wTransferSize 4096 bytes - bcdDFUVersion 1.00 -Device Status: 0x0a00 - (Bus Powered) +Bus 003 Device 017: ID 1d50:5119 OpenMoko, Inc. GTA01/GTA02 U-Boot Bootloader +Device Descriptor: + bLength 18 + bDescriptorType 1 + bcdUSB 1.00 + bDeviceClass 0 (Defined at Interface level) + bDeviceSubClass 0 + bDeviceProtocol 0 + bMaxPacketSize0 16 + idVendor 0x1d50 OpenMoko, Inc. + idProduct 0x5119 GTA01/GTA02 U-Boot Bootloader + bcdDevice 0.00 + iManufacturer 1 OpenMoko, Inc + iProduct 2 Neo1973 Bootloader U-Boot 1.3.2-moko12 + iSerial 3 0000000 + bNumConfigurations 1 + Configuration Descriptor: + bLength 9 + bDescriptorType 2 + wTotalLength 81 + bNumInterfaces 1 + bConfigurationValue 1 + iConfiguration 7 USB Device Firmware Upgrade + bmAttributes 0x80 + (Bus Powered) + MaxPower 100mA + Interface Descriptor: + bLength 9 + bDescriptorType 4 + bInterfaceNumber 0 + bAlternateSetting 0 + bNumEndpoints 0 + bInterfaceClass 254 Application Specific Interface + bInterfaceSubClass 1 Device Firmware Update + bInterfaceProtocol 2 + iInterface 8 RAM 0x32000000 + Interface Descriptor: + bLength 9 + bDescriptorType 4 + bInterfaceNumber 0 + bAlternateSetting 1 + bNumEndpoints 0 + bInterfaceClass 254 Application Specific Interface + bInterfaceSubClass 1 Device Firmware Update + bInterfaceProtocol 2 + iInterface 9 u-boot + Interface Descriptor: + bLength 9 + bDescriptorType 4 + bInterfaceNumber 0 + bAlternateSetting 2 + bNumEndpoints 0 + bInterfaceClass 254 Application Specific Interface + bInterfaceSubClass 1 Device Firmware Update + bInterfaceProtocol 2 + iInterface 10 u-boot_env + Interface Descriptor: + bLength 9 + bDescriptorType 4 + bInterfaceNumber 0 + bAlternateSetting 3 + bNumEndpoints 0 + bInterfaceClass 254 Application Specific Interface + bInterfaceSubClass 1 Device Firmware Update + bInterfaceProtocol 2 + iInterface 11 kernel + Interface Descriptor: + bLength 9 + bDescriptorType 4 + bInterfaceNumber 0 + bAlternateSetting 4 + bNumEndpoints 0 + bInterfaceClass 254 Application Specific Interface + bInterfaceSubClass 1 Device Firmware Update + bInterfaceProtocol 2 + iInterface 12 splash + Interface Descriptor: + bLength 9 + bDescriptorType 4 + bInterfaceNumber 0 + bAlternateSetting 5 + bNumEndpoints 0 + bInterfaceClass 254 Application Specific Interface + bInterfaceSubClass 1 Device Firmware Update + bInterfaceProtocol 2 + iInterface 13 factory + Interface Descriptor: + bLength 9 + bDescriptorType 4 + bInterfaceNumber 0 + bAlternateSetting 6 + bNumEndpoints 0 + bInterfaceClass 254 Application Specific Interface + bInterfaceSubClass 1 Device Firmware Update + bInterfaceProtocol 2 + iInterface 14 rootfs + Device Firmware Upgrade Interface Descriptor: + bLength 9 + bDescriptorType 33 + bmAttributes 7 + Will Not Detach + Manifestation Tolerant + Upload Supported + Download Supported + wDetachTimeout 65280 milliseconds + wTransferSize 4096 bytes + bcdDFUVersion 1.00 +Device Status: 0x0a00 + (Bus Powered) diff --git a/tools/src/dfu-util/device-logs/openmoko-freerunner.lsusb b/tools/linux/src/dfu-util/device-logs/openmoko-freerunner.lsusb old mode 100755 new mode 100644 similarity index 97% rename from tools/src/dfu-util/device-logs/openmoko-freerunner.lsusb rename to tools/linux/src/dfu-util/device-logs/openmoko-freerunner.lsusb index cf148e5..835708d --- a/tools/src/dfu-util/device-logs/openmoko-freerunner.lsusb +++ b/tools/linux/src/dfu-util/device-logs/openmoko-freerunner.lsusb @@ -1,179 +1,179 @@ -Bus 005 Device 033: ID 1d50:5119 OpenMoko, Inc. GTA01/GTA02 U-Boot Bootloader -Device Descriptor: - bLength 18 - bDescriptorType 1 - bcdUSB 1.10 - bDeviceClass 2 Communications - bDeviceSubClass 0 - bDeviceProtocol 0 - bMaxPacketSize0 16 - idVendor 0x1d50 OpenMoko, Inc. - idProduct 0x5119 GTA01/GTA02 U-Boot Bootloader - bcdDevice 0.00 - iManufacturer 1 OpenMoko, Inc - iProduct 2 Neo1973 Bootloader U-Boot 1.3.2-moko12 - iSerial 3 0000000 - bNumConfigurations 2 - Configuration Descriptor: - bLength 9 - bDescriptorType 2 - wTotalLength 85 - bNumInterfaces 3 - bConfigurationValue 1 - iConfiguration 4 TTY via USB - bmAttributes 0x80 - (Bus Powered) - MaxPower 500mA - Interface Descriptor: - bLength 9 - bDescriptorType 4 - bInterfaceNumber 0 - bAlternateSetting 0 - bNumEndpoints 1 - bInterfaceClass 2 Communications - bInterfaceSubClass 2 Abstract (modem) - bInterfaceProtocol 1 AT-commands (v.25ter) - iInterface 6 Control Interface - CDC Header: - bcdCDC 0.6e - CDC Call Management: - bmCapabilities 0x00 - bDataInterface 1 - CDC ACM: - bmCapabilities 0x00 - CDC Union: - bMasterInterface 0 - bSlaveInterface 1 - Endpoint Descriptor: - bLength 7 - bDescriptorType 5 - bEndpointAddress 0x81 EP 1 IN - bmAttributes 3 - Transfer Type Interrupt - Synch Type None - Usage Type Data - wMaxPacketSize 0x0010 1x 16 bytes - bInterval 255 - Interface Descriptor: - bLength 9 - bDescriptorType 4 - bInterfaceNumber 1 - bAlternateSetting 0 - bNumEndpoints 2 - bInterfaceClass 10 CDC Data - bInterfaceSubClass 0 Unused - bInterfaceProtocol 0 - iInterface 5 Bulk Data Interface - Endpoint Descriptor: - bLength 7 - bDescriptorType 5 - bEndpointAddress 0x02 EP 2 OUT - bmAttributes 2 - Transfer Type Bulk - Synch Type None - Usage Type Data - wMaxPacketSize 0x0010 1x 16 bytes - bInterval 255 - Endpoint Descriptor: - bLength 7 - bDescriptorType 5 - bEndpointAddress 0x83 EP 3 IN - bmAttributes 2 - Transfer Type Bulk - Synch Type None - Usage Type Data - wMaxPacketSize 0x0010 1x 16 bytes - bInterval 255 - Interface Descriptor: - bLength 9 - bDescriptorType 4 - bInterfaceNumber 2 - bAlternateSetting 0 - bNumEndpoints 0 - bInterfaceClass 254 Application Specific Interface - bInterfaceSubClass 1 Device Firmware Update - bInterfaceProtocol 1 - iInterface 7 USB Device Firmware Upgrade - Device Firmware Upgrade Interface Descriptor: - bLength 9 - bDescriptorType 33 - bmAttributes 7 - Will Not Detach - Manifestation Tolerant - Upload Supported - Download Supported - wDetachTimeout 65280 milliseconds - wTransferSize 4096 bytes - bcdDFUVersion 1.00 - Configuration Descriptor: - bLength 9 - bDescriptorType 2 - wTotalLength 67 - bNumInterfaces 2 - bConfigurationValue 2 - iConfiguration 4 TTY via USB - bmAttributes 0x80 - (Bus Powered) - MaxPower 100mA - Interface Descriptor: - bLength 9 - bDescriptorType 4 - bInterfaceNumber 0 - bAlternateSetting 0 - bNumEndpoints 1 - bInterfaceClass 2 Communications - bInterfaceSubClass 2 Abstract (modem) - bInterfaceProtocol 1 AT-commands (v.25ter) - iInterface 6 Control Interface - CDC Header: - bcdCDC 0.6e - CDC Call Management: - bmCapabilities 0x00 - bDataInterface 1 - CDC ACM: - bmCapabilities 0x00 - CDC Union: - bMasterInterface 0 - bSlaveInterface 1 - Endpoint Descriptor: - bLength 7 - bDescriptorType 5 - bEndpointAddress 0x81 EP 1 IN - bmAttributes 3 - Transfer Type Interrupt - Synch Type None - Usage Type Data - wMaxPacketSize 0x0010 1x 16 bytes - bInterval 255 - Interface Descriptor: - bLength 9 - bDescriptorType 4 - bInterfaceNumber 1 - bAlternateSetting 0 - bNumEndpoints 2 - bInterfaceClass 10 CDC Data - bInterfaceSubClass 0 Unused - bInterfaceProtocol 0 - iInterface 5 Bulk Data Interface - Endpoint Descriptor: - bLength 7 - bDescriptorType 5 - bEndpointAddress 0x02 EP 2 OUT - bmAttributes 2 - Transfer Type Bulk - Synch Type None - Usage Type Data - wMaxPacketSize 0x0010 1x 16 bytes - bInterval 255 - Endpoint Descriptor: - bLength 7 - bDescriptorType 5 - bEndpointAddress 0x83 EP 3 IN - bmAttributes 2 - Transfer Type Bulk - Synch Type None - Usage Type Data - wMaxPacketSize 0x0010 1x 16 bytes - bInterval 255 -Device Status: 0x9a00 - (Bus Powered) +Bus 005 Device 033: ID 1d50:5119 OpenMoko, Inc. GTA01/GTA02 U-Boot Bootloader +Device Descriptor: + bLength 18 + bDescriptorType 1 + bcdUSB 1.10 + bDeviceClass 2 Communications + bDeviceSubClass 0 + bDeviceProtocol 0 + bMaxPacketSize0 16 + idVendor 0x1d50 OpenMoko, Inc. + idProduct 0x5119 GTA01/GTA02 U-Boot Bootloader + bcdDevice 0.00 + iManufacturer 1 OpenMoko, Inc + iProduct 2 Neo1973 Bootloader U-Boot 1.3.2-moko12 + iSerial 3 0000000 + bNumConfigurations 2 + Configuration Descriptor: + bLength 9 + bDescriptorType 2 + wTotalLength 85 + bNumInterfaces 3 + bConfigurationValue 1 + iConfiguration 4 TTY via USB + bmAttributes 0x80 + (Bus Powered) + MaxPower 500mA + Interface Descriptor: + bLength 9 + bDescriptorType 4 + bInterfaceNumber 0 + bAlternateSetting 0 + bNumEndpoints 1 + bInterfaceClass 2 Communications + bInterfaceSubClass 2 Abstract (modem) + bInterfaceProtocol 1 AT-commands (v.25ter) + iInterface 6 Control Interface + CDC Header: + bcdCDC 0.6e + CDC Call Management: + bmCapabilities 0x00 + bDataInterface 1 + CDC ACM: + bmCapabilities 0x00 + CDC Union: + bMasterInterface 0 + bSlaveInterface 1 + Endpoint Descriptor: + bLength 7 + bDescriptorType 5 + bEndpointAddress 0x81 EP 1 IN + bmAttributes 3 + Transfer Type Interrupt + Synch Type None + Usage Type Data + wMaxPacketSize 0x0010 1x 16 bytes + bInterval 255 + Interface Descriptor: + bLength 9 + bDescriptorType 4 + bInterfaceNumber 1 + bAlternateSetting 0 + bNumEndpoints 2 + bInterfaceClass 10 CDC Data + bInterfaceSubClass 0 Unused + bInterfaceProtocol 0 + iInterface 5 Bulk Data Interface + Endpoint Descriptor: + bLength 7 + bDescriptorType 5 + bEndpointAddress 0x02 EP 2 OUT + bmAttributes 2 + Transfer Type Bulk + Synch Type None + Usage Type Data + wMaxPacketSize 0x0010 1x 16 bytes + bInterval 255 + Endpoint Descriptor: + bLength 7 + bDescriptorType 5 + bEndpointAddress 0x83 EP 3 IN + bmAttributes 2 + Transfer Type Bulk + Synch Type None + Usage Type Data + wMaxPacketSize 0x0010 1x 16 bytes + bInterval 255 + Interface Descriptor: + bLength 9 + bDescriptorType 4 + bInterfaceNumber 2 + bAlternateSetting 0 + bNumEndpoints 0 + bInterfaceClass 254 Application Specific Interface + bInterfaceSubClass 1 Device Firmware Update + bInterfaceProtocol 1 + iInterface 7 USB Device Firmware Upgrade + Device Firmware Upgrade Interface Descriptor: + bLength 9 + bDescriptorType 33 + bmAttributes 7 + Will Not Detach + Manifestation Tolerant + Upload Supported + Download Supported + wDetachTimeout 65280 milliseconds + wTransferSize 4096 bytes + bcdDFUVersion 1.00 + Configuration Descriptor: + bLength 9 + bDescriptorType 2 + wTotalLength 67 + bNumInterfaces 2 + bConfigurationValue 2 + iConfiguration 4 TTY via USB + bmAttributes 0x80 + (Bus Powered) + MaxPower 100mA + Interface Descriptor: + bLength 9 + bDescriptorType 4 + bInterfaceNumber 0 + bAlternateSetting 0 + bNumEndpoints 1 + bInterfaceClass 2 Communications + bInterfaceSubClass 2 Abstract (modem) + bInterfaceProtocol 1 AT-commands (v.25ter) + iInterface 6 Control Interface + CDC Header: + bcdCDC 0.6e + CDC Call Management: + bmCapabilities 0x00 + bDataInterface 1 + CDC ACM: + bmCapabilities 0x00 + CDC Union: + bMasterInterface 0 + bSlaveInterface 1 + Endpoint Descriptor: + bLength 7 + bDescriptorType 5 + bEndpointAddress 0x81 EP 1 IN + bmAttributes 3 + Transfer Type Interrupt + Synch Type None + Usage Type Data + wMaxPacketSize 0x0010 1x 16 bytes + bInterval 255 + Interface Descriptor: + bLength 9 + bDescriptorType 4 + bInterfaceNumber 1 + bAlternateSetting 0 + bNumEndpoints 2 + bInterfaceClass 10 CDC Data + bInterfaceSubClass 0 Unused + bInterfaceProtocol 0 + iInterface 5 Bulk Data Interface + Endpoint Descriptor: + bLength 7 + bDescriptorType 5 + bEndpointAddress 0x02 EP 2 OUT + bmAttributes 2 + Transfer Type Bulk + Synch Type None + Usage Type Data + wMaxPacketSize 0x0010 1x 16 bytes + bInterval 255 + Endpoint Descriptor: + bLength 7 + bDescriptorType 5 + bEndpointAddress 0x83 EP 3 IN + bmAttributes 2 + Transfer Type Bulk + Synch Type None + Usage Type Data + wMaxPacketSize 0x0010 1x 16 bytes + bInterval 255 +Device Status: 0x9a00 + (Bus Powered) diff --git a/tools/src/dfu-util/device-logs/openmoko-neo1973.lsusb b/tools/linux/src/dfu-util/device-logs/openmoko-neo1973.lsusb old mode 100755 new mode 100644 similarity index 97% rename from tools/src/dfu-util/device-logs/openmoko-neo1973.lsusb rename to tools/linux/src/dfu-util/device-logs/openmoko-neo1973.lsusb index dc1fe7c..0778950 --- a/tools/src/dfu-util/device-logs/openmoko-neo1973.lsusb +++ b/tools/linux/src/dfu-util/device-logs/openmoko-neo1973.lsusb @@ -1,182 +1,182 @@ - -Bus 006 Device 020: ID 1457:5119 First International Computer, Inc. OpenMoko Neo1973 u-boot cdc_acm serial port -Device Descriptor: - bLength 18 - bDescriptorType 1 - bcdUSB 1.10 - bDeviceClass 2 Communications - bDeviceSubClass 0 - bDeviceProtocol 0 - bMaxPacketSize0 16 - idVendor 0x1457 First International Computer, Inc. - idProduct 0x5119 OpenMoko Neo1973 u-boot cdc_acm serial port - bcdDevice 0.00 - iManufacturer 1 - iProduct 2 - iSerial 3 - bNumConfigurations 2 - Configuration Descriptor: - bLength 9 - bDescriptorType 2 - wTotalLength 85 - bNumInterfaces 3 - bConfigurationValue 1 - iConfiguration 4 - bmAttributes 0x80 - (Bus Powered) - MaxPower 500mA - Interface Descriptor: - bLength 9 - bDescriptorType 4 - bInterfaceNumber 0 - bAlternateSetting 0 - bNumEndpoints 1 - bInterfaceClass 2 Communications - bInterfaceSubClass 2 Abstract (modem) - bInterfaceProtocol 1 AT-commands (v.25ter) - iInterface 6 - CDC Header: - bcdCDC 0.6e - CDC Call Management: - bmCapabilities 0x00 - bDataInterface 1 - CDC ACM: - bmCapabilities 0x00 - CDC Union: - bMasterInterface 0 - bSlaveInterface 1 - Endpoint Descriptor: - bLength 7 - bDescriptorType 5 - bEndpointAddress 0x81 EP 1 IN - bmAttributes 3 - Transfer Type Interrupt - Synch Type None - Usage Type Data - wMaxPacketSize 0x0010 1x 16 bytes - bInterval 255 - Interface Descriptor: - bLength 9 - bDescriptorType 4 - bInterfaceNumber 1 - bAlternateSetting 0 - bNumEndpoints 2 - bInterfaceClass 10 CDC Data - bInterfaceSubClass 0 Unused - bInterfaceProtocol 0 - iInterface 5 - Endpoint Descriptor: - bLength 7 - bDescriptorType 5 - bEndpointAddress 0x02 EP 2 OUT - bmAttributes 2 - Transfer Type Bulk - Synch Type None - Usage Type Data - wMaxPacketSize 0x0010 1x 16 bytes - bInterval 255 - Endpoint Descriptor: - bLength 7 - bDescriptorType 5 - bEndpointAddress 0x83 EP 3 IN - bmAttributes 2 - Transfer Type Bulk - Synch Type None - Usage Type Data - wMaxPacketSize 0x0010 1x 16 bytes - bInterval 255 - Interface Descriptor: - bLength 9 - bDescriptorType 4 - bInterfaceNumber 2 - bAlternateSetting 0 - bNumEndpoints 0 - bInterfaceClass 254 Application Specific Interface - bInterfaceSubClass 1 Device Firmware Update - bInterfaceProtocol 1 - iInterface 7 - Device Firmware Upgrade Interface Descriptor: - bLength 9 - bDescriptorType 33 - bmAttributes 7 - Will Not Detach - Manifestation Tolerant - Upload Supported - Download Supported - wDetachTimeout 65280 milliseconds - wTransferSize 4096 bytes - bcdDFUVersion 1.00 - Configuration Descriptor: - bLength 9 - bDescriptorType 2 - wTotalLength 67 - bNumInterfaces 2 - bConfigurationValue 2 - iConfiguration 4 - bmAttributes 0x80 - (Bus Powered) - MaxPower 100mA - Interface Descriptor: - bLength 9 - bDescriptorType 4 - bInterfaceNumber 0 - bAlternateSetting 0 - bNumEndpoints 1 - bInterfaceClass 2 Communications - bInterfaceSubClass 2 Abstract (modem) - bInterfaceProtocol 1 AT-commands (v.25ter) - iInterface 6 - CDC Header: - bcdCDC 0.6e - CDC Call Management: - bmCapabilities 0x00 - bDataInterface 1 - CDC ACM: - bmCapabilities 0x00 - CDC Union: - bMasterInterface 0 - bSlaveInterface 1 - Endpoint Descriptor: - bLength 7 - bDescriptorType 5 - bEndpointAddress 0x81 EP 1 IN - bmAttributes 3 - Transfer Type Interrupt - Synch Type None - Usage Type Data - wMaxPacketSize 0x0010 1x 16 bytes - bInterval 255 - Interface Descriptor: - bLength 9 - bDescriptorType 4 - bInterfaceNumber 1 - bAlternateSetting 0 - bNumEndpoints 2 - bInterfaceClass 10 CDC Data - bInterfaceSubClass 0 Unused - bInterfaceProtocol 0 - iInterface 5 - Endpoint Descriptor: - bLength 7 - bDescriptorType 5 - bEndpointAddress 0x02 EP 2 OUT - bmAttributes 2 - Transfer Type Bulk - Synch Type None - Usage Type Data - wMaxPacketSize 0x0010 1x 16 bytes - bInterval 255 - Endpoint Descriptor: - bLength 7 - bDescriptorType 5 - bEndpointAddress 0x83 EP 3 IN - bmAttributes 2 - Transfer Type Bulk - Synch Type None - Usage Type Data - wMaxPacketSize 0x0010 1x 16 bytes - bInterval 255 -Device Status: 0x0006 - (Bus Powered) - Remote Wakeup Enabled - Test Mode + +Bus 006 Device 020: ID 1457:5119 First International Computer, Inc. OpenMoko Neo1973 u-boot cdc_acm serial port +Device Descriptor: + bLength 18 + bDescriptorType 1 + bcdUSB 1.10 + bDeviceClass 2 Communications + bDeviceSubClass 0 + bDeviceProtocol 0 + bMaxPacketSize0 16 + idVendor 0x1457 First International Computer, Inc. + idProduct 0x5119 OpenMoko Neo1973 u-boot cdc_acm serial port + bcdDevice 0.00 + iManufacturer 1 + iProduct 2 + iSerial 3 + bNumConfigurations 2 + Configuration Descriptor: + bLength 9 + bDescriptorType 2 + wTotalLength 85 + bNumInterfaces 3 + bConfigurationValue 1 + iConfiguration 4 + bmAttributes 0x80 + (Bus Powered) + MaxPower 500mA + Interface Descriptor: + bLength 9 + bDescriptorType 4 + bInterfaceNumber 0 + bAlternateSetting 0 + bNumEndpoints 1 + bInterfaceClass 2 Communications + bInterfaceSubClass 2 Abstract (modem) + bInterfaceProtocol 1 AT-commands (v.25ter) + iInterface 6 + CDC Header: + bcdCDC 0.6e + CDC Call Management: + bmCapabilities 0x00 + bDataInterface 1 + CDC ACM: + bmCapabilities 0x00 + CDC Union: + bMasterInterface 0 + bSlaveInterface 1 + Endpoint Descriptor: + bLength 7 + bDescriptorType 5 + bEndpointAddress 0x81 EP 1 IN + bmAttributes 3 + Transfer Type Interrupt + Synch Type None + Usage Type Data + wMaxPacketSize 0x0010 1x 16 bytes + bInterval 255 + Interface Descriptor: + bLength 9 + bDescriptorType 4 + bInterfaceNumber 1 + bAlternateSetting 0 + bNumEndpoints 2 + bInterfaceClass 10 CDC Data + bInterfaceSubClass 0 Unused + bInterfaceProtocol 0 + iInterface 5 + Endpoint Descriptor: + bLength 7 + bDescriptorType 5 + bEndpointAddress 0x02 EP 2 OUT + bmAttributes 2 + Transfer Type Bulk + Synch Type None + Usage Type Data + wMaxPacketSize 0x0010 1x 16 bytes + bInterval 255 + Endpoint Descriptor: + bLength 7 + bDescriptorType 5 + bEndpointAddress 0x83 EP 3 IN + bmAttributes 2 + Transfer Type Bulk + Synch Type None + Usage Type Data + wMaxPacketSize 0x0010 1x 16 bytes + bInterval 255 + Interface Descriptor: + bLength 9 + bDescriptorType 4 + bInterfaceNumber 2 + bAlternateSetting 0 + bNumEndpoints 0 + bInterfaceClass 254 Application Specific Interface + bInterfaceSubClass 1 Device Firmware Update + bInterfaceProtocol 1 + iInterface 7 + Device Firmware Upgrade Interface Descriptor: + bLength 9 + bDescriptorType 33 + bmAttributes 7 + Will Not Detach + Manifestation Tolerant + Upload Supported + Download Supported + wDetachTimeout 65280 milliseconds + wTransferSize 4096 bytes + bcdDFUVersion 1.00 + Configuration Descriptor: + bLength 9 + bDescriptorType 2 + wTotalLength 67 + bNumInterfaces 2 + bConfigurationValue 2 + iConfiguration 4 + bmAttributes 0x80 + (Bus Powered) + MaxPower 100mA + Interface Descriptor: + bLength 9 + bDescriptorType 4 + bInterfaceNumber 0 + bAlternateSetting 0 + bNumEndpoints 1 + bInterfaceClass 2 Communications + bInterfaceSubClass 2 Abstract (modem) + bInterfaceProtocol 1 AT-commands (v.25ter) + iInterface 6 + CDC Header: + bcdCDC 0.6e + CDC Call Management: + bmCapabilities 0x00 + bDataInterface 1 + CDC ACM: + bmCapabilities 0x00 + CDC Union: + bMasterInterface 0 + bSlaveInterface 1 + Endpoint Descriptor: + bLength 7 + bDescriptorType 5 + bEndpointAddress 0x81 EP 1 IN + bmAttributes 3 + Transfer Type Interrupt + Synch Type None + Usage Type Data + wMaxPacketSize 0x0010 1x 16 bytes + bInterval 255 + Interface Descriptor: + bLength 9 + bDescriptorType 4 + bInterfaceNumber 1 + bAlternateSetting 0 + bNumEndpoints 2 + bInterfaceClass 10 CDC Data + bInterfaceSubClass 0 Unused + bInterfaceProtocol 0 + iInterface 5 + Endpoint Descriptor: + bLength 7 + bDescriptorType 5 + bEndpointAddress 0x02 EP 2 OUT + bmAttributes 2 + Transfer Type Bulk + Synch Type None + Usage Type Data + wMaxPacketSize 0x0010 1x 16 bytes + bInterval 255 + Endpoint Descriptor: + bLength 7 + bDescriptorType 5 + bEndpointAddress 0x83 EP 3 IN + bmAttributes 2 + Transfer Type Bulk + Synch Type None + Usage Type Data + wMaxPacketSize 0x0010 1x 16 bytes + bInterval 255 +Device Status: 0x0006 + (Bus Powered) + Remote Wakeup Enabled + Test Mode diff --git a/tools/src/dfu-util/device-logs/openpcd.lsusb b/tools/linux/src/dfu-util/device-logs/openpcd.lsusb old mode 100755 new mode 100644 similarity index 97% rename from tools/src/dfu-util/device-logs/openpcd.lsusb rename to tools/linux/src/dfu-util/device-logs/openpcd.lsusb index 8618647..f6255a9 --- a/tools/src/dfu-util/device-logs/openpcd.lsusb +++ b/tools/linux/src/dfu-util/device-logs/openpcd.lsusb @@ -1,60 +1,60 @@ - -Bus 006 Device 016: ID 16c0:076b VOTI OpenPCD 13.56MHz RFID Reader -Device Descriptor: - bLength 18 - bDescriptorType 1 - bcdUSB 1.00 - bDeviceClass 0 (Defined at Interface level) - bDeviceSubClass 0 - bDeviceProtocol 0 - bMaxPacketSize0 8 - idVendor 0x16c0 VOTI - idProduct 0x076b OpenPCD 13.56MHz RFID Reader - bcdDevice 0.00 - iManufacturer 1 - iProduct 2 OpenPCD RFID Simulator - DFU Mode - iSerial 0 - bNumConfigurations 1 - Configuration Descriptor: - bLength 9 - bDescriptorType 2 - wTotalLength 36 - bNumInterfaces 1 - bConfigurationValue 1 - iConfiguration 0 - bmAttributes 0x80 - (Bus Powered) - MaxPower 200mA - Interface Descriptor: - bLength 9 - bDescriptorType 4 - bInterfaceNumber 0 - bAlternateSetting 0 - bNumEndpoints 0 - bInterfaceClass 254 Application Specific Interface - bInterfaceSubClass 1 Device Firmware Update - bInterfaceProtocol 2 - iInterface 0 - Interface Descriptor: - bLength 9 - bDescriptorType 4 - bInterfaceNumber 0 - bAlternateSetting 1 - bNumEndpoints 0 - bInterfaceClass 254 Application Specific Interface - bInterfaceSubClass 1 Device Firmware Update - bInterfaceProtocol 2 - iInterface 0 - Device Firmware Upgrade Interface Descriptor: - bLength 9 - bDescriptorType 33 - bmAttributes 3 - Will Not Detach - Manifestation Intolerant - Upload Supported - Download Supported - wDetachTimeout 65280 milliseconds - wTransferSize 256 bytes - bcdDFUVersion 1.00 -Device Status: 0x0000 - (Bus Powered) + +Bus 006 Device 016: ID 16c0:076b VOTI OpenPCD 13.56MHz RFID Reader +Device Descriptor: + bLength 18 + bDescriptorType 1 + bcdUSB 1.00 + bDeviceClass 0 (Defined at Interface level) + bDeviceSubClass 0 + bDeviceProtocol 0 + bMaxPacketSize0 8 + idVendor 0x16c0 VOTI + idProduct 0x076b OpenPCD 13.56MHz RFID Reader + bcdDevice 0.00 + iManufacturer 1 + iProduct 2 OpenPCD RFID Simulator - DFU Mode + iSerial 0 + bNumConfigurations 1 + Configuration Descriptor: + bLength 9 + bDescriptorType 2 + wTotalLength 36 + bNumInterfaces 1 + bConfigurationValue 1 + iConfiguration 0 + bmAttributes 0x80 + (Bus Powered) + MaxPower 200mA + Interface Descriptor: + bLength 9 + bDescriptorType 4 + bInterfaceNumber 0 + bAlternateSetting 0 + bNumEndpoints 0 + bInterfaceClass 254 Application Specific Interface + bInterfaceSubClass 1 Device Firmware Update + bInterfaceProtocol 2 + iInterface 0 + Interface Descriptor: + bLength 9 + bDescriptorType 4 + bInterfaceNumber 0 + bAlternateSetting 1 + bNumEndpoints 0 + bInterfaceClass 254 Application Specific Interface + bInterfaceSubClass 1 Device Firmware Update + bInterfaceProtocol 2 + iInterface 0 + Device Firmware Upgrade Interface Descriptor: + bLength 9 + bDescriptorType 33 + bmAttributes 3 + Will Not Detach + Manifestation Intolerant + Upload Supported + Download Supported + wDetachTimeout 65280 milliseconds + wTransferSize 256 bytes + bcdDFUVersion 1.00 +Device Status: 0x0000 + (Bus Powered) diff --git a/tools/src/dfu-util/device-logs/qi-hardware-atusb.lsusb b/tools/linux/src/dfu-util/device-logs/qi-hardware-atusb.lsusb old mode 100755 new mode 100644 similarity index 97% rename from tools/src/dfu-util/device-logs/qi-hardware-atusb.lsusb rename to tools/linux/src/dfu-util/device-logs/qi-hardware-atusb.lsusb index d1da57a..bfc1701 --- a/tools/src/dfu-util/device-logs/qi-hardware-atusb.lsusb +++ b/tools/linux/src/dfu-util/device-logs/qi-hardware-atusb.lsusb @@ -1,59 +1,59 @@ - -Bus 006 Device 013: ID 20b7:1540 Qi Hardware ben-wpan, AT86RF230-based -Device Descriptor: - bLength 18 - bDescriptorType 1 - bcdUSB 2.00 - bDeviceClass 255 Vendor Specific Class - bDeviceSubClass 0 - bDeviceProtocol 0 - bMaxPacketSize0 64 - idVendor 0x20b7 Qi Hardware - idProduct 0x1540 ben-wpan, AT86RF230-based - bcdDevice 0.01 - iManufacturer 0 - iProduct 0 - iSerial 1 4630333438371508231a - bNumConfigurations 1 - Configuration Descriptor: - bLength 9 - bDescriptorType 2 - wTotalLength 34 - bNumInterfaces 2 - bConfigurationValue 1 - iConfiguration 0 - bmAttributes 0x80 - (Bus Powered) - MaxPower 40mA - Interface Descriptor: - bLength 9 - bDescriptorType 4 - bInterfaceNumber 0 - bAlternateSetting 0 - bNumEndpoints 1 - bInterfaceClass 255 Vendor Specific Class - bInterfaceSubClass 0 - bInterfaceProtocol 0 - iInterface 0 - Endpoint Descriptor: - bLength 7 - bDescriptorType 5 - bEndpointAddress 0x81 EP 1 IN - bmAttributes 2 - Transfer Type Bulk - Synch Type None - Usage Type Data - wMaxPacketSize 0x0040 1x 64 bytes - bInterval 0 - Interface Descriptor: - bLength 9 - bDescriptorType 4 - bInterfaceNumber 1 - bAlternateSetting 0 - bNumEndpoints 0 - bInterfaceClass 254 Application Specific Interface - bInterfaceSubClass 1 Device Firmware Update - bInterfaceProtocol 1 - iInterface 0 -Device Status: 0x0000 - (Bus Powered) + +Bus 006 Device 013: ID 20b7:1540 Qi Hardware ben-wpan, AT86RF230-based +Device Descriptor: + bLength 18 + bDescriptorType 1 + bcdUSB 2.00 + bDeviceClass 255 Vendor Specific Class + bDeviceSubClass 0 + bDeviceProtocol 0 + bMaxPacketSize0 64 + idVendor 0x20b7 Qi Hardware + idProduct 0x1540 ben-wpan, AT86RF230-based + bcdDevice 0.01 + iManufacturer 0 + iProduct 0 + iSerial 1 4630333438371508231a + bNumConfigurations 1 + Configuration Descriptor: + bLength 9 + bDescriptorType 2 + wTotalLength 34 + bNumInterfaces 2 + bConfigurationValue 1 + iConfiguration 0 + bmAttributes 0x80 + (Bus Powered) + MaxPower 40mA + Interface Descriptor: + bLength 9 + bDescriptorType 4 + bInterfaceNumber 0 + bAlternateSetting 0 + bNumEndpoints 1 + bInterfaceClass 255 Vendor Specific Class + bInterfaceSubClass 0 + bInterfaceProtocol 0 + iInterface 0 + Endpoint Descriptor: + bLength 7 + bDescriptorType 5 + bEndpointAddress 0x81 EP 1 IN + bmAttributes 2 + Transfer Type Bulk + Synch Type None + Usage Type Data + wMaxPacketSize 0x0040 1x 64 bytes + bInterval 0 + Interface Descriptor: + bLength 9 + bDescriptorType 4 + bInterfaceNumber 1 + bAlternateSetting 0 + bNumEndpoints 0 + bInterfaceClass 254 Application Specific Interface + bInterfaceSubClass 1 Device Firmware Update + bInterfaceProtocol 1 + iInterface 0 +Device Status: 0x0000 + (Bus Powered) diff --git a/tools/src/dfu-util/device-logs/simtrace.lsusb b/tools/linux/src/dfu-util/device-logs/simtrace.lsusb old mode 100755 new mode 100644 similarity index 97% rename from tools/src/dfu-util/device-logs/simtrace.lsusb rename to tools/linux/src/dfu-util/device-logs/simtrace.lsusb index fd51539..578ddf0 --- a/tools/src/dfu-util/device-logs/simtrace.lsusb +++ b/tools/linux/src/dfu-util/device-logs/simtrace.lsusb @@ -1,70 +1,70 @@ - -Bus 006 Device 017: ID 16c0:0762 VOTI -Device Descriptor: - bLength 18 - bDescriptorType 1 - bcdUSB 1.00 - bDeviceClass 0 (Defined at Interface level) - bDeviceSubClass 0 - bDeviceProtocol 0 - bMaxPacketSize0 8 - idVendor 0x16c0 VOTI - idProduct 0x0762 - bcdDevice 0.00 - iManufacturer 1 sysmocom - systems for mobile communications GmbH - iProduct 2 SimTrace SIM Sniffer - DFU Mode - iSerial 0 - bNumConfigurations 1 - Configuration Descriptor: - bLength 9 - bDescriptorType 2 - wTotalLength 45 - bNumInterfaces 1 - bConfigurationValue 1 - iConfiguration 3 SimTrace DFU Configuration - bmAttributes 0x80 - (Bus Powered) - MaxPower 200mA - Interface Descriptor: - bLength 9 - bDescriptorType 4 - bInterfaceNumber 0 - bAlternateSetting 0 - bNumEndpoints 0 - bInterfaceClass 254 Application Specific Interface - bInterfaceSubClass 1 Device Firmware Update - bInterfaceProtocol 2 - iInterface 4 SimTrace DFU Interface - Application Partition - Interface Descriptor: - bLength 9 - bDescriptorType 4 - bInterfaceNumber 0 - bAlternateSetting 1 - bNumEndpoints 0 - bInterfaceClass 254 Application Specific Interface - bInterfaceSubClass 1 Device Firmware Update - bInterfaceProtocol 2 - iInterface 5 SimTrace DFU Interface - Bootloader Partition - Interface Descriptor: - bLength 9 - bDescriptorType 4 - bInterfaceNumber 0 - bAlternateSetting 2 - bNumEndpoints 0 - bInterfaceClass 254 Application Specific Interface - bInterfaceSubClass 1 Device Firmware Update - bInterfaceProtocol 2 - iInterface 6 SimTrace DFU Interface - RAM - Device Firmware Upgrade Interface Descriptor: - bLength 9 - bDescriptorType 33 - bmAttributes 3 - Will Not Detach - Manifestation Intolerant - Upload Supported - Download Supported - wDetachTimeout 65280 milliseconds - wTransferSize 256 bytes - bcdDFUVersion 1.00 -Device Status: 0x0000 - (Bus Powered) + +Bus 006 Device 017: ID 16c0:0762 VOTI +Device Descriptor: + bLength 18 + bDescriptorType 1 + bcdUSB 1.00 + bDeviceClass 0 (Defined at Interface level) + bDeviceSubClass 0 + bDeviceProtocol 0 + bMaxPacketSize0 8 + idVendor 0x16c0 VOTI + idProduct 0x0762 + bcdDevice 0.00 + iManufacturer 1 sysmocom - systems for mobile communications GmbH + iProduct 2 SimTrace SIM Sniffer - DFU Mode + iSerial 0 + bNumConfigurations 1 + Configuration Descriptor: + bLength 9 + bDescriptorType 2 + wTotalLength 45 + bNumInterfaces 1 + bConfigurationValue 1 + iConfiguration 3 SimTrace DFU Configuration + bmAttributes 0x80 + (Bus Powered) + MaxPower 200mA + Interface Descriptor: + bLength 9 + bDescriptorType 4 + bInterfaceNumber 0 + bAlternateSetting 0 + bNumEndpoints 0 + bInterfaceClass 254 Application Specific Interface + bInterfaceSubClass 1 Device Firmware Update + bInterfaceProtocol 2 + iInterface 4 SimTrace DFU Interface - Application Partition + Interface Descriptor: + bLength 9 + bDescriptorType 4 + bInterfaceNumber 0 + bAlternateSetting 1 + bNumEndpoints 0 + bInterfaceClass 254 Application Specific Interface + bInterfaceSubClass 1 Device Firmware Update + bInterfaceProtocol 2 + iInterface 5 SimTrace DFU Interface - Bootloader Partition + Interface Descriptor: + bLength 9 + bDescriptorType 4 + bInterfaceNumber 0 + bAlternateSetting 2 + bNumEndpoints 0 + bInterfaceClass 254 Application Specific Interface + bInterfaceSubClass 1 Device Firmware Update + bInterfaceProtocol 2 + iInterface 6 SimTrace DFU Interface - RAM + Device Firmware Upgrade Interface Descriptor: + bLength 9 + bDescriptorType 33 + bmAttributes 3 + Will Not Detach + Manifestation Intolerant + Upload Supported + Download Supported + wDetachTimeout 65280 milliseconds + wTransferSize 256 bytes + bcdDFUVersion 1.00 +Device Status: 0x0000 + (Bus Powered) diff --git a/tools/src/dfu-util/device-logs/sparkcore.lsusb b/tools/linux/src/dfu-util/device-logs/sparkcore.lsusb old mode 100755 new mode 100644 similarity index 97% rename from tools/src/dfu-util/device-logs/sparkcore.lsusb rename to tools/linux/src/dfu-util/device-logs/sparkcore.lsusb index 79c529b..b6029ff --- a/tools/src/dfu-util/device-logs/sparkcore.lsusb +++ b/tools/linux/src/dfu-util/device-logs/sparkcore.lsusb @@ -1,60 +1,60 @@ - -Bus 001 Device 008: ID 1d50:607f OpenMoko, Inc. -Device Descriptor: - bLength 18 - bDescriptorType 1 - bcdUSB 1.00 - bDeviceClass 0 (Defined at Interface level) - bDeviceSubClass 0 - bDeviceProtocol 0 - bMaxPacketSize0 64 - idVendor 0x1d50 OpenMoko, Inc. - idProduct 0x607f - bcdDevice 2.00 - iManufacturer 1 Spark Devices - iProduct 2 CORE DFU - iSerial 3 8D80527B5055 - bNumConfigurations 1 - Configuration Descriptor: - bLength 9 - bDescriptorType 2 - wTotalLength 36 - bNumInterfaces 1 - bConfigurationValue 1 - iConfiguration 0 - bmAttributes 0xc0 - Self Powered - MaxPower 100mA - Interface Descriptor: - bLength 9 - bDescriptorType 4 - bInterfaceNumber 0 - bAlternateSetting 0 - bNumEndpoints 0 - bInterfaceClass 254 Application Specific Interface - bInterfaceSubClass 1 Device Firmware Update - bInterfaceProtocol 2 - iInterface 4 @Internal Flash /0x08000000/20*001Ka,108*001Kg - Interface Descriptor: - bLength 9 - bDescriptorType 4 - bInterfaceNumber 0 - bAlternateSetting 1 - bNumEndpoints 0 - bInterfaceClass 254 Application Specific Interface - bInterfaceSubClass 1 Device Firmware Update - bInterfaceProtocol 2 - iInterface 5 @SPI Flash : SST25x/0x00000000/512*04Kg - Device Firmware Upgrade Interface Descriptor: - bLength 9 - bDescriptorType 33 - bmAttributes 11 - Will Detach - Manifestation Intolerant - Upload Supported - Download Supported - wDetachTimeout 255 milliseconds - wTransferSize 1024 bytes - bcdDFUVersion 1.1a -Device Status: 0x0001 - Self Powered + +Bus 001 Device 008: ID 1d50:607f OpenMoko, Inc. +Device Descriptor: + bLength 18 + bDescriptorType 1 + bcdUSB 1.00 + bDeviceClass 0 (Defined at Interface level) + bDeviceSubClass 0 + bDeviceProtocol 0 + bMaxPacketSize0 64 + idVendor 0x1d50 OpenMoko, Inc. + idProduct 0x607f + bcdDevice 2.00 + iManufacturer 1 Spark Devices + iProduct 2 CORE DFU + iSerial 3 8D80527B5055 + bNumConfigurations 1 + Configuration Descriptor: + bLength 9 + bDescriptorType 2 + wTotalLength 36 + bNumInterfaces 1 + bConfigurationValue 1 + iConfiguration 0 + bmAttributes 0xc0 + Self Powered + MaxPower 100mA + Interface Descriptor: + bLength 9 + bDescriptorType 4 + bInterfaceNumber 0 + bAlternateSetting 0 + bNumEndpoints 0 + bInterfaceClass 254 Application Specific Interface + bInterfaceSubClass 1 Device Firmware Update + bInterfaceProtocol 2 + iInterface 4 @Internal Flash /0x08000000/20*001Ka,108*001Kg + Interface Descriptor: + bLength 9 + bDescriptorType 4 + bInterfaceNumber 0 + bAlternateSetting 1 + bNumEndpoints 0 + bInterfaceClass 254 Application Specific Interface + bInterfaceSubClass 1 Device Firmware Update + bInterfaceProtocol 2 + iInterface 5 @SPI Flash : SST25x/0x00000000/512*04Kg + Device Firmware Upgrade Interface Descriptor: + bLength 9 + bDescriptorType 33 + bmAttributes 11 + Will Detach + Manifestation Intolerant + Upload Supported + Download Supported + wDetachTimeout 255 milliseconds + wTransferSize 1024 bytes + bcdDFUVersion 1.1a +Device Status: 0x0001 + Self Powered diff --git a/tools/src/dfu-util/device-logs/stm32f107.bin-download b/tools/linux/src/dfu-util/device-logs/stm32f107.bin-download old mode 100755 new mode 100644 similarity index 98% rename from tools/src/dfu-util/device-logs/stm32f107.bin-download rename to tools/linux/src/dfu-util/device-logs/stm32f107.bin-download index 4d803fb..45b714f --- a/tools/src/dfu-util/device-logs/stm32f107.bin-download +++ b/tools/linux/src/dfu-util/device-logs/stm32f107.bin-download @@ -1,48 +1,48 @@ -> src/dfu-util --intf 0 --alt 0 -v -v -v -s 0x8000000 -D test3 -dfu-util 0.4 - -(C) 2005-2008 by Weston Schmidt, Harald Welte and OpenMoko Inc. -(C) 2010-2011 Tormod Volden (DfuSe support) -This program is Free Software and has ABSOLUTELY NO WARRANTY - -dfu-util does currently only support DFU version 1.0 - -Opening DFU USB device... ID 0483:df11 -Run-time device DFU version 011a -Found DFU: [0483:df11] devnum=0, cfg=1, intf=0, alt=0, name="@Internal Flash /0x08000000/128*002Kg" -Claiming USB DFU Interface... -Setting Alternate Setting #0 ... -Determining device status: state = dfuIDLE, status = 0 -dfuIDLE, continuing -DFU mode device DFU version 011a -Device returned transfer size 2048 -No valid DFU suffix signature -Warning: File has no DFU suffix -DfuSe interface name: "Internal Flash " -Memory segment at 0x08000000 128 x 2048 = 262144 (rew) -Uploading to address = 0x08000000, size = 16384 -Erasing page size 2048 at address 0x08000000, page starting at 0x08000000 - Download from image offset 00000000 to memory 08000000-080007ff, size 2048 - Setting address pointer to 0x08000000 -Erasing page size 2048 at address 0x08000800, page starting at 0x08000800 - Download from image offset 00000800 to memory 08000800-08000fff, size 2048 - Setting address pointer to 0x08000800 -Erasing page size 2048 at address 0x08001000, page starting at 0x08001000 - Download from image offset 00001000 to memory 08001000-080017ff, size 2048 - Setting address pointer to 0x08001000 -Erasing page size 2048 at address 0x08001800, page starting at 0x08001800 - Download from image offset 00001800 to memory 08001800-08001fff, size 2048 - Setting address pointer to 0x08001800 -Erasing page size 2048 at address 0x08002000, page starting at 0x08002000 - Download from image offset 00002000 to memory 08002000-080027ff, size 2048 - Setting address pointer to 0x08002000 -Erasing page size 2048 at address 0x08002800, page starting at 0x08002800 - Download from image offset 00002800 to memory 08002800-08002fff, size 2048 - Setting address pointer to 0x08002800 -Erasing page size 2048 at address 0x08003000, page starting at 0x08003000 - Download from image offset 00003000 to memory 08003000-080037ff, size 2048 - Setting address pointer to 0x08003000 -Erasing page size 2048 at address 0x08003800, page starting at 0x08003800 - Download from image offset 00003800 to memory 08003800-08003fff, size 2048 - Setting address pointer to 0x08003800 - +> src/dfu-util --intf 0 --alt 0 -v -v -v -s 0x8000000 -D test3 +dfu-util 0.4 + +(C) 2005-2008 by Weston Schmidt, Harald Welte and OpenMoko Inc. +(C) 2010-2011 Tormod Volden (DfuSe support) +This program is Free Software and has ABSOLUTELY NO WARRANTY + +dfu-util does currently only support DFU version 1.0 + +Opening DFU USB device... ID 0483:df11 +Run-time device DFU version 011a +Found DFU: [0483:df11] devnum=0, cfg=1, intf=0, alt=0, name="@Internal Flash /0x08000000/128*002Kg" +Claiming USB DFU Interface... +Setting Alternate Setting #0 ... +Determining device status: state = dfuIDLE, status = 0 +dfuIDLE, continuing +DFU mode device DFU version 011a +Device returned transfer size 2048 +No valid DFU suffix signature +Warning: File has no DFU suffix +DfuSe interface name: "Internal Flash " +Memory segment at 0x08000000 128 x 2048 = 262144 (rew) +Uploading to address = 0x08000000, size = 16384 +Erasing page size 2048 at address 0x08000000, page starting at 0x08000000 + Download from image offset 00000000 to memory 08000000-080007ff, size 2048 + Setting address pointer to 0x08000000 +Erasing page size 2048 at address 0x08000800, page starting at 0x08000800 + Download from image offset 00000800 to memory 08000800-08000fff, size 2048 + Setting address pointer to 0x08000800 +Erasing page size 2048 at address 0x08001000, page starting at 0x08001000 + Download from image offset 00001000 to memory 08001000-080017ff, size 2048 + Setting address pointer to 0x08001000 +Erasing page size 2048 at address 0x08001800, page starting at 0x08001800 + Download from image offset 00001800 to memory 08001800-08001fff, size 2048 + Setting address pointer to 0x08001800 +Erasing page size 2048 at address 0x08002000, page starting at 0x08002000 + Download from image offset 00002000 to memory 08002000-080027ff, size 2048 + Setting address pointer to 0x08002000 +Erasing page size 2048 at address 0x08002800, page starting at 0x08002800 + Download from image offset 00002800 to memory 08002800-08002fff, size 2048 + Setting address pointer to 0x08002800 +Erasing page size 2048 at address 0x08003000, page starting at 0x08003000 + Download from image offset 00003000 to memory 08003000-080037ff, size 2048 + Setting address pointer to 0x08003000 +Erasing page size 2048 at address 0x08003800, page starting at 0x08003800 + Download from image offset 00003800 to memory 08003800-08003fff, size 2048 + Setting address pointer to 0x08003800 + diff --git a/tools/src/dfu-util/device-logs/stm32f107.lsusb b/tools/linux/src/dfu-util/device-logs/stm32f107.lsusb old mode 100755 new mode 100644 similarity index 97% rename from tools/src/dfu-util/device-logs/stm32f107.lsusb rename to tools/linux/src/dfu-util/device-logs/stm32f107.lsusb index 89d8d38..14b45cd --- a/tools/src/dfu-util/device-logs/stm32f107.lsusb +++ b/tools/linux/src/dfu-util/device-logs/stm32f107.lsusb @@ -1,60 +1,60 @@ - -Bus 001 Device 028: ID 0483:df11 SGS Thomson Microelectronics STM Device in DFU Mode -Device Descriptor: - bLength 18 - bDescriptorType 1 - bcdUSB 1.00 - bDeviceClass 0 (Defined at Interface level) - bDeviceSubClass 0 - bDeviceProtocol 0 - bMaxPacketSize0 64 - idVendor 0x0483 SGS Thomson Microelectronics - idProduct 0xdf11 STM Device in DFU Mode - bcdDevice 20.00 - iManufacturer 1 STMicroelectronics - iProduct 2 STM32 0x418 DFU Bootloader - iSerial 3 STM32 - bNumConfigurations 1 - Configuration Descriptor: - bLength 9 - bDescriptorType 2 - wTotalLength 36 - bNumInterfaces 1 - bConfigurationValue 1 - iConfiguration 0 - bmAttributes 0xc0 - Self Powered - MaxPower 100mA - Interface Descriptor: - bLength 9 - bDescriptorType 4 - bInterfaceNumber 0 - bAlternateSetting 0 - bNumEndpoints 0 - bInterfaceClass 254 Application Specific Interface - bInterfaceSubClass 1 Device Firmware Update - bInterfaceProtocol 2 - iInterface 4 @Internal Flash /0x08000000/128*002Kg - Interface Descriptor: - bLength 9 - bDescriptorType 4 - bInterfaceNumber 0 - bAlternateSetting 1 - bNumEndpoints 0 - bInterfaceClass 254 Application Specific Interface - bInterfaceSubClass 1 Device Firmware Update - bInterfaceProtocol 2 - iInterface 5 @Option Bytes /0x1FFFF800/01*016 g - Device Firmware Upgrade Interface Descriptor: - bLength 9 - bDescriptorType 33 - bmAttributes 11 - Will Detach - Manifestation Intolerant - Upload Supported - Download Supported - wDetachTimeout 255 milliseconds - wTransferSize 2048 bytes - bcdDFUVersion 1.1a -Device Status: 0x0000 - (Bus Powered) + +Bus 001 Device 028: ID 0483:df11 SGS Thomson Microelectronics STM Device in DFU Mode +Device Descriptor: + bLength 18 + bDescriptorType 1 + bcdUSB 1.00 + bDeviceClass 0 (Defined at Interface level) + bDeviceSubClass 0 + bDeviceProtocol 0 + bMaxPacketSize0 64 + idVendor 0x0483 SGS Thomson Microelectronics + idProduct 0xdf11 STM Device in DFU Mode + bcdDevice 20.00 + iManufacturer 1 STMicroelectronics + iProduct 2 STM32 0x418 DFU Bootloader + iSerial 3 STM32 + bNumConfigurations 1 + Configuration Descriptor: + bLength 9 + bDescriptorType 2 + wTotalLength 36 + bNumInterfaces 1 + bConfigurationValue 1 + iConfiguration 0 + bmAttributes 0xc0 + Self Powered + MaxPower 100mA + Interface Descriptor: + bLength 9 + bDescriptorType 4 + bInterfaceNumber 0 + bAlternateSetting 0 + bNumEndpoints 0 + bInterfaceClass 254 Application Specific Interface + bInterfaceSubClass 1 Device Firmware Update + bInterfaceProtocol 2 + iInterface 4 @Internal Flash /0x08000000/128*002Kg + Interface Descriptor: + bLength 9 + bDescriptorType 4 + bInterfaceNumber 0 + bAlternateSetting 1 + bNumEndpoints 0 + bInterfaceClass 254 Application Specific Interface + bInterfaceSubClass 1 Device Firmware Update + bInterfaceProtocol 2 + iInterface 5 @Option Bytes /0x1FFFF800/01*016 g + Device Firmware Upgrade Interface Descriptor: + bLength 9 + bDescriptorType 33 + bmAttributes 11 + Will Detach + Manifestation Intolerant + Upload Supported + Download Supported + wDetachTimeout 255 milliseconds + wTransferSize 2048 bytes + bcdDFUVersion 1.1a +Device Status: 0x0000 + (Bus Powered) diff --git a/tools/src/dfu-util/device-logs/stm32f4discovery.bin-download b/tools/linux/src/dfu-util/device-logs/stm32f4discovery.bin-download old mode 100755 new mode 100644 similarity index 97% rename from tools/src/dfu-util/device-logs/stm32f4discovery.bin-download rename to tools/linux/src/dfu-util/device-logs/stm32f4discovery.bin-download index d5a2679..96e1722 --- a/tools/src/dfu-util/device-logs/stm32f4discovery.bin-download +++ b/tools/linux/src/dfu-util/device-logs/stm32f4discovery.bin-download @@ -1,36 +1,36 @@ -dfu-util --device 0483:df11 --alt 0 \ - --dfuse-address 0x08000000 \ - -v -v -v \ - --download arm/iotoggle.bin -No valid DFU suffix signature -Warning: File has no DFU suffix -dfu-util 0.5 - -(C) 2005-2008 by Weston Schmidt, Harald Welte and OpenMoko Inc. -(C) 2010-2011 Tormod Volden (DfuSe support) -This program is Free Software and has ABSOLUTELY NO WARRANTY - -dfu-util does currently only support DFU version 1.0 - -Filter on vendor = 0x0483 product = 0xdf11 -Opening DFU capable USB device... ID 0483:df11 -Run-time device DFU version 011a -Found DFU: [0483:df11] devnum=0, cfg=1, intf=0, alt=0, name="@Internal Flash /0x08000000/04*016Kg,01*064Kg,07*128Kg" -Claiming USB DFU Interface... -Setting Alternate Setting #0 ... -Determining device status: state = dfuERROR, status = 10 -dfuERROR, clearing status -Determining device status: state = dfuIDLE, status = 0 -dfuIDLE, continuing -DFU mode device DFU version 011a -Device returned transfer size 2048 -DfuSe interface name: "Internal Flash " -Memory segment at 0x08000000 4 x 16384 = 65536 (rew) -Memory segment at 0x08010000 1 x 65536 = 65536 (rew) -Memory segment at 0x08020000 7 x 131072 = 917504 (rew) -Uploading to address = 0x08000000, size = 2308 -Erasing page size 16384 at address 0x08000000, page starting at 0x08000000 - Download from image offset 00000000 to memory 08000000-080007ff, size 2048 - Setting address pointer to 0x08000000 - Download from image offset 00000800 to memory 08000800-08000903, size 260 - Setting address pointer to 0x08000800 +dfu-util --device 0483:df11 --alt 0 \ + --dfuse-address 0x08000000 \ + -v -v -v \ + --download arm/iotoggle.bin +No valid DFU suffix signature +Warning: File has no DFU suffix +dfu-util 0.5 + +(C) 2005-2008 by Weston Schmidt, Harald Welte and OpenMoko Inc. +(C) 2010-2011 Tormod Volden (DfuSe support) +This program is Free Software and has ABSOLUTELY NO WARRANTY + +dfu-util does currently only support DFU version 1.0 + +Filter on vendor = 0x0483 product = 0xdf11 +Opening DFU capable USB device... ID 0483:df11 +Run-time device DFU version 011a +Found DFU: [0483:df11] devnum=0, cfg=1, intf=0, alt=0, name="@Internal Flash /0x08000000/04*016Kg,01*064Kg,07*128Kg" +Claiming USB DFU Interface... +Setting Alternate Setting #0 ... +Determining device status: state = dfuERROR, status = 10 +dfuERROR, clearing status +Determining device status: state = dfuIDLE, status = 0 +dfuIDLE, continuing +DFU mode device DFU version 011a +Device returned transfer size 2048 +DfuSe interface name: "Internal Flash " +Memory segment at 0x08000000 4 x 16384 = 65536 (rew) +Memory segment at 0x08010000 1 x 65536 = 65536 (rew) +Memory segment at 0x08020000 7 x 131072 = 917504 (rew) +Uploading to address = 0x08000000, size = 2308 +Erasing page size 16384 at address 0x08000000, page starting at 0x08000000 + Download from image offset 00000000 to memory 08000000-080007ff, size 2048 + Setting address pointer to 0x08000000 + Download from image offset 00000800 to memory 08000800-08000903, size 260 + Setting address pointer to 0x08000800 diff --git a/tools/src/dfu-util/device-logs/stm32f4discovery.lsusb b/tools/linux/src/dfu-util/device-logs/stm32f4discovery.lsusb old mode 100755 new mode 100644 similarity index 97% rename from tools/src/dfu-util/device-logs/stm32f4discovery.lsusb rename to tools/linux/src/dfu-util/device-logs/stm32f4discovery.lsusb index c78530c..0b870de --- a/tools/src/dfu-util/device-logs/stm32f4discovery.lsusb +++ b/tools/linux/src/dfu-util/device-logs/stm32f4discovery.lsusb @@ -1,80 +1,80 @@ - -Bus 001 Device 010: ID 0483:df11 SGS Thomson Microelectronics STM Device in DFU Mode -Device Descriptor: - bLength 18 - bDescriptorType 1 - bcdUSB 1.00 - bDeviceClass 0 (Defined at Interface level) - bDeviceSubClass 0 - bDeviceProtocol 0 - bMaxPacketSize0 64 - idVendor 0x0483 SGS Thomson Microelectronics - idProduct 0xdf11 STM Device in DFU Mode - bcdDevice 21.00 - iManufacturer 1 STMicroelectronics - iProduct 2 STM32 BOOTLOADER - iSerial 3 315A28A0B956 - bNumConfigurations 1 - Configuration Descriptor: - bLength 9 - bDescriptorType 2 - wTotalLength 54 - bNumInterfaces 1 - bConfigurationValue 1 - iConfiguration 0 - bmAttributes 0xc0 - Self Powered - MaxPower 100mA - Interface Descriptor: - bLength 9 - bDescriptorType 4 - bInterfaceNumber 0 - bAlternateSetting 0 - bNumEndpoints 0 - bInterfaceClass 254 Application Specific Interface - bInterfaceSubClass 1 Device Firmware Update - bInterfaceProtocol 2 - iInterface 4 @Internal Flash /0x08000000/04*016Kg,01*064Kg,07*128Kg - Interface Descriptor: - bLength 9 - bDescriptorType 4 - bInterfaceNumber 0 - bAlternateSetting 1 - bNumEndpoints 0 - bInterfaceClass 254 Application Specific Interface - bInterfaceSubClass 1 Device Firmware Update - bInterfaceProtocol 2 - iInterface 5 @Option Bytes /0x1FFFC000/01*016 g - Interface Descriptor: - bLength 9 - bDescriptorType 4 - bInterfaceNumber 0 - bAlternateSetting 2 - bNumEndpoints 0 - bInterfaceClass 254 Application Specific Interface - bInterfaceSubClass 1 Device Firmware Update - bInterfaceProtocol 2 - iInterface 6 @OTP Memory /0x1FFF7800/01*512 g,01*016 g - Interface Descriptor: - bLength 9 - bDescriptorType 4 - bInterfaceNumber 0 - bAlternateSetting 3 - bNumEndpoints 0 - bInterfaceClass 254 Application Specific Interface - bInterfaceSubClass 1 Device Firmware Update - bInterfaceProtocol 2 - iInterface 7 @Device Feature/0xFFFF0000/01*004 g - Device Firmware Upgrade Interface Descriptor: - bLength 9 - bDescriptorType 33 - bmAttributes 11 - Will Detach - Manifestation Intolerant - Upload Supported - Download Supported - wDetachTimeout 255 milliseconds - wTransferSize 2048 bytes - bcdDFUVersion 1.1a -Device Status: 0x0001 - Self Powered + +Bus 001 Device 010: ID 0483:df11 SGS Thomson Microelectronics STM Device in DFU Mode +Device Descriptor: + bLength 18 + bDescriptorType 1 + bcdUSB 1.00 + bDeviceClass 0 (Defined at Interface level) + bDeviceSubClass 0 + bDeviceProtocol 0 + bMaxPacketSize0 64 + idVendor 0x0483 SGS Thomson Microelectronics + idProduct 0xdf11 STM Device in DFU Mode + bcdDevice 21.00 + iManufacturer 1 STMicroelectronics + iProduct 2 STM32 BOOTLOADER + iSerial 3 315A28A0B956 + bNumConfigurations 1 + Configuration Descriptor: + bLength 9 + bDescriptorType 2 + wTotalLength 54 + bNumInterfaces 1 + bConfigurationValue 1 + iConfiguration 0 + bmAttributes 0xc0 + Self Powered + MaxPower 100mA + Interface Descriptor: + bLength 9 + bDescriptorType 4 + bInterfaceNumber 0 + bAlternateSetting 0 + bNumEndpoints 0 + bInterfaceClass 254 Application Specific Interface + bInterfaceSubClass 1 Device Firmware Update + bInterfaceProtocol 2 + iInterface 4 @Internal Flash /0x08000000/04*016Kg,01*064Kg,07*128Kg + Interface Descriptor: + bLength 9 + bDescriptorType 4 + bInterfaceNumber 0 + bAlternateSetting 1 + bNumEndpoints 0 + bInterfaceClass 254 Application Specific Interface + bInterfaceSubClass 1 Device Firmware Update + bInterfaceProtocol 2 + iInterface 5 @Option Bytes /0x1FFFC000/01*016 g + Interface Descriptor: + bLength 9 + bDescriptorType 4 + bInterfaceNumber 0 + bAlternateSetting 2 + bNumEndpoints 0 + bInterfaceClass 254 Application Specific Interface + bInterfaceSubClass 1 Device Firmware Update + bInterfaceProtocol 2 + iInterface 6 @OTP Memory /0x1FFF7800/01*512 g,01*016 g + Interface Descriptor: + bLength 9 + bDescriptorType 4 + bInterfaceNumber 0 + bAlternateSetting 3 + bNumEndpoints 0 + bInterfaceClass 254 Application Specific Interface + bInterfaceSubClass 1 Device Firmware Update + bInterfaceProtocol 2 + iInterface 7 @Device Feature/0xFFFF0000/01*004 g + Device Firmware Upgrade Interface Descriptor: + bLength 9 + bDescriptorType 33 + bmAttributes 11 + Will Detach + Manifestation Intolerant + Upload Supported + Download Supported + wDetachTimeout 255 milliseconds + wTransferSize 2048 bytes + bcdDFUVersion 1.1a +Device Status: 0x0001 + Self Powered diff --git a/tools/src/dfu-util/device-logs/tdk-bluetooth.lsusb b/tools/linux/src/dfu-util/device-logs/tdk-bluetooth.lsusb old mode 100755 new mode 100644 similarity index 97% rename from tools/src/dfu-util/device-logs/tdk-bluetooth.lsusb rename to tools/linux/src/dfu-util/device-logs/tdk-bluetooth.lsusb index ac07bb7..c0cface --- a/tools/src/dfu-util/device-logs/tdk-bluetooth.lsusb +++ b/tools/linux/src/dfu-util/device-logs/tdk-bluetooth.lsusb @@ -1,269 +1,269 @@ - -Bus 006 Device 014: ID 04bf:0320 TDK Corp. Bluetooth Adapter -Device Descriptor: - bLength 18 - bDescriptorType 1 - bcdUSB 2.00 - bDeviceClass 224 Wireless - bDeviceSubClass 1 Radio Frequency - bDeviceProtocol 1 Bluetooth - bMaxPacketSize0 64 - idVendor 0x04bf TDK Corp. - idProduct 0x0320 Bluetooth Adapter - bcdDevice 26.52 - iManufacturer 1 Ezurio - iProduct 2 Turbo Bluetooth Adapter - iSerial 3 008098D4FFBD - bNumConfigurations 1 - Configuration Descriptor: - bLength 9 - bDescriptorType 2 - wTotalLength 193 - bNumInterfaces 3 - bConfigurationValue 1 - iConfiguration 0 - bmAttributes 0x80 - (Bus Powered) - MaxPower 64mA - Interface Descriptor: - bLength 9 - bDescriptorType 4 - bInterfaceNumber 0 - bAlternateSetting 0 - bNumEndpoints 3 - bInterfaceClass 224 Wireless - bInterfaceSubClass 1 Radio Frequency - bInterfaceProtocol 1 Bluetooth - iInterface 0 - Endpoint Descriptor: - bLength 7 - bDescriptorType 5 - bEndpointAddress 0x81 EP 1 IN - bmAttributes 3 - Transfer Type Interrupt - Synch Type None - Usage Type Data - wMaxPacketSize 0x0010 1x 16 bytes - bInterval 1 - Endpoint Descriptor: - bLength 7 - bDescriptorType 5 - bEndpointAddress 0x02 EP 2 OUT - bmAttributes 2 - Transfer Type Bulk - Synch Type None - Usage Type Data - wMaxPacketSize 0x0040 1x 64 bytes - bInterval 1 - Endpoint Descriptor: - bLength 7 - bDescriptorType 5 - bEndpointAddress 0x82 EP 2 IN - bmAttributes 2 - Transfer Type Bulk - Synch Type None - Usage Type Data - wMaxPacketSize 0x0040 1x 64 bytes - bInterval 1 - Interface Descriptor: - bLength 9 - bDescriptorType 4 - bInterfaceNumber 1 - bAlternateSetting 0 - bNumEndpoints 2 - bInterfaceClass 224 Wireless - bInterfaceSubClass 1 Radio Frequency - bInterfaceProtocol 1 Bluetooth - iInterface 0 - Endpoint Descriptor: - bLength 7 - bDescriptorType 5 - bEndpointAddress 0x03 EP 3 OUT - bmAttributes 1 - Transfer Type Isochronous - Synch Type None - Usage Type Data - wMaxPacketSize 0x0000 1x 0 bytes - bInterval 1 - Endpoint Descriptor: - bLength 7 - bDescriptorType 5 - bEndpointAddress 0x83 EP 3 IN - bmAttributes 1 - Transfer Type Isochronous - Synch Type None - Usage Type Data - wMaxPacketSize 0x0000 1x 0 bytes - bInterval 1 - Interface Descriptor: - bLength 9 - bDescriptorType 4 - bInterfaceNumber 1 - bAlternateSetting 1 - bNumEndpoints 2 - bInterfaceClass 224 Wireless - bInterfaceSubClass 1 Radio Frequency - bInterfaceProtocol 1 Bluetooth - iInterface 0 - Endpoint Descriptor: - bLength 7 - bDescriptorType 5 - bEndpointAddress 0x03 EP 3 OUT - bmAttributes 1 - Transfer Type Isochronous - Synch Type None - Usage Type Data - wMaxPacketSize 0x0009 1x 9 bytes - bInterval 1 - Endpoint Descriptor: - bLength 7 - bDescriptorType 5 - bEndpointAddress 0x83 EP 3 IN - bmAttributes 1 - Transfer Type Isochronous - Synch Type None - Usage Type Data - wMaxPacketSize 0x0009 1x 9 bytes - bInterval 1 - Interface Descriptor: - bLength 9 - bDescriptorType 4 - bInterfaceNumber 1 - bAlternateSetting 2 - bNumEndpoints 2 - bInterfaceClass 224 Wireless - bInterfaceSubClass 1 Radio Frequency - bInterfaceProtocol 1 Bluetooth - iInterface 0 - Endpoint Descriptor: - bLength 7 - bDescriptorType 5 - bEndpointAddress 0x03 EP 3 OUT - bmAttributes 1 - Transfer Type Isochronous - Synch Type None - Usage Type Data - wMaxPacketSize 0x0011 1x 17 bytes - bInterval 1 - Endpoint Descriptor: - bLength 7 - bDescriptorType 5 - bEndpointAddress 0x83 EP 3 IN - bmAttributes 1 - Transfer Type Isochronous - Synch Type None - Usage Type Data - wMaxPacketSize 0x0011 1x 17 bytes - bInterval 1 - Interface Descriptor: - bLength 9 - bDescriptorType 4 - bInterfaceNumber 1 - bAlternateSetting 3 - bNumEndpoints 2 - bInterfaceClass 224 Wireless - bInterfaceSubClass 1 Radio Frequency - bInterfaceProtocol 1 Bluetooth - iInterface 0 - Endpoint Descriptor: - bLength 7 - bDescriptorType 5 - bEndpointAddress 0x03 EP 3 OUT - bmAttributes 1 - Transfer Type Isochronous - Synch Type None - Usage Type Data - wMaxPacketSize 0x0019 1x 25 bytes - bInterval 1 - Endpoint Descriptor: - bLength 7 - bDescriptorType 5 - bEndpointAddress 0x83 EP 3 IN - bmAttributes 1 - Transfer Type Isochronous - Synch Type None - Usage Type Data - wMaxPacketSize 0x0019 1x 25 bytes - bInterval 1 - Interface Descriptor: - bLength 9 - bDescriptorType 4 - bInterfaceNumber 1 - bAlternateSetting 4 - bNumEndpoints 2 - bInterfaceClass 224 Wireless - bInterfaceSubClass 1 Radio Frequency - bInterfaceProtocol 1 Bluetooth - iInterface 0 - Endpoint Descriptor: - bLength 7 - bDescriptorType 5 - bEndpointAddress 0x03 EP 3 OUT - bmAttributes 1 - Transfer Type Isochronous - Synch Type None - Usage Type Data - wMaxPacketSize 0x0021 1x 33 bytes - bInterval 1 - Endpoint Descriptor: - bLength 7 - bDescriptorType 5 - bEndpointAddress 0x83 EP 3 IN - bmAttributes 1 - Transfer Type Isochronous - Synch Type None - Usage Type Data - wMaxPacketSize 0x0021 1x 33 bytes - bInterval 1 - Interface Descriptor: - bLength 9 - bDescriptorType 4 - bInterfaceNumber 1 - bAlternateSetting 5 - bNumEndpoints 2 - bInterfaceClass 224 Wireless - bInterfaceSubClass 1 Radio Frequency - bInterfaceProtocol 1 Bluetooth - iInterface 0 - Endpoint Descriptor: - bLength 7 - bDescriptorType 5 - bEndpointAddress 0x03 EP 3 OUT - bmAttributes 1 - Transfer Type Isochronous - Synch Type None - Usage Type Data - wMaxPacketSize 0x0031 1x 49 bytes - bInterval 1 - Endpoint Descriptor: - bLength 7 - bDescriptorType 5 - bEndpointAddress 0x83 EP 3 IN - bmAttributes 1 - Transfer Type Isochronous - Synch Type None - Usage Type Data - wMaxPacketSize 0x0031 1x 49 bytes - bInterval 1 - Interface Descriptor: - bLength 9 - bDescriptorType 4 - bInterfaceNumber 2 - bAlternateSetting 0 - bNumEndpoints 0 - bInterfaceClass 254 Application Specific Interface - bInterfaceSubClass 1 Device Firmware Update - bInterfaceProtocol 0 - iInterface 0 - Device Firmware Upgrade Interface Descriptor: - bLength 7 - bDescriptorType 33 - bmAttributes 7 - Will Not Detach - Manifestation Tolerant - Upload Supported - Download Supported - wDetachTimeout 5000 milliseconds - wTransferSize 1023 bytes -Device Status: 0x0000 - (Bus Powered) + +Bus 006 Device 014: ID 04bf:0320 TDK Corp. Bluetooth Adapter +Device Descriptor: + bLength 18 + bDescriptorType 1 + bcdUSB 2.00 + bDeviceClass 224 Wireless + bDeviceSubClass 1 Radio Frequency + bDeviceProtocol 1 Bluetooth + bMaxPacketSize0 64 + idVendor 0x04bf TDK Corp. + idProduct 0x0320 Bluetooth Adapter + bcdDevice 26.52 + iManufacturer 1 Ezurio + iProduct 2 Turbo Bluetooth Adapter + iSerial 3 008098D4FFBD + bNumConfigurations 1 + Configuration Descriptor: + bLength 9 + bDescriptorType 2 + wTotalLength 193 + bNumInterfaces 3 + bConfigurationValue 1 + iConfiguration 0 + bmAttributes 0x80 + (Bus Powered) + MaxPower 64mA + Interface Descriptor: + bLength 9 + bDescriptorType 4 + bInterfaceNumber 0 + bAlternateSetting 0 + bNumEndpoints 3 + bInterfaceClass 224 Wireless + bInterfaceSubClass 1 Radio Frequency + bInterfaceProtocol 1 Bluetooth + iInterface 0 + Endpoint Descriptor: + bLength 7 + bDescriptorType 5 + bEndpointAddress 0x81 EP 1 IN + bmAttributes 3 + Transfer Type Interrupt + Synch Type None + Usage Type Data + wMaxPacketSize 0x0010 1x 16 bytes + bInterval 1 + Endpoint Descriptor: + bLength 7 + bDescriptorType 5 + bEndpointAddress 0x02 EP 2 OUT + bmAttributes 2 + Transfer Type Bulk + Synch Type None + Usage Type Data + wMaxPacketSize 0x0040 1x 64 bytes + bInterval 1 + Endpoint Descriptor: + bLength 7 + bDescriptorType 5 + bEndpointAddress 0x82 EP 2 IN + bmAttributes 2 + Transfer Type Bulk + Synch Type None + Usage Type Data + wMaxPacketSize 0x0040 1x 64 bytes + bInterval 1 + Interface Descriptor: + bLength 9 + bDescriptorType 4 + bInterfaceNumber 1 + bAlternateSetting 0 + bNumEndpoints 2 + bInterfaceClass 224 Wireless + bInterfaceSubClass 1 Radio Frequency + bInterfaceProtocol 1 Bluetooth + iInterface 0 + Endpoint Descriptor: + bLength 7 + bDescriptorType 5 + bEndpointAddress 0x03 EP 3 OUT + bmAttributes 1 + Transfer Type Isochronous + Synch Type None + Usage Type Data + wMaxPacketSize 0x0000 1x 0 bytes + bInterval 1 + Endpoint Descriptor: + bLength 7 + bDescriptorType 5 + bEndpointAddress 0x83 EP 3 IN + bmAttributes 1 + Transfer Type Isochronous + Synch Type None + Usage Type Data + wMaxPacketSize 0x0000 1x 0 bytes + bInterval 1 + Interface Descriptor: + bLength 9 + bDescriptorType 4 + bInterfaceNumber 1 + bAlternateSetting 1 + bNumEndpoints 2 + bInterfaceClass 224 Wireless + bInterfaceSubClass 1 Radio Frequency + bInterfaceProtocol 1 Bluetooth + iInterface 0 + Endpoint Descriptor: + bLength 7 + bDescriptorType 5 + bEndpointAddress 0x03 EP 3 OUT + bmAttributes 1 + Transfer Type Isochronous + Synch Type None + Usage Type Data + wMaxPacketSize 0x0009 1x 9 bytes + bInterval 1 + Endpoint Descriptor: + bLength 7 + bDescriptorType 5 + bEndpointAddress 0x83 EP 3 IN + bmAttributes 1 + Transfer Type Isochronous + Synch Type None + Usage Type Data + wMaxPacketSize 0x0009 1x 9 bytes + bInterval 1 + Interface Descriptor: + bLength 9 + bDescriptorType 4 + bInterfaceNumber 1 + bAlternateSetting 2 + bNumEndpoints 2 + bInterfaceClass 224 Wireless + bInterfaceSubClass 1 Radio Frequency + bInterfaceProtocol 1 Bluetooth + iInterface 0 + Endpoint Descriptor: + bLength 7 + bDescriptorType 5 + bEndpointAddress 0x03 EP 3 OUT + bmAttributes 1 + Transfer Type Isochronous + Synch Type None + Usage Type Data + wMaxPacketSize 0x0011 1x 17 bytes + bInterval 1 + Endpoint Descriptor: + bLength 7 + bDescriptorType 5 + bEndpointAddress 0x83 EP 3 IN + bmAttributes 1 + Transfer Type Isochronous + Synch Type None + Usage Type Data + wMaxPacketSize 0x0011 1x 17 bytes + bInterval 1 + Interface Descriptor: + bLength 9 + bDescriptorType 4 + bInterfaceNumber 1 + bAlternateSetting 3 + bNumEndpoints 2 + bInterfaceClass 224 Wireless + bInterfaceSubClass 1 Radio Frequency + bInterfaceProtocol 1 Bluetooth + iInterface 0 + Endpoint Descriptor: + bLength 7 + bDescriptorType 5 + bEndpointAddress 0x03 EP 3 OUT + bmAttributes 1 + Transfer Type Isochronous + Synch Type None + Usage Type Data + wMaxPacketSize 0x0019 1x 25 bytes + bInterval 1 + Endpoint Descriptor: + bLength 7 + bDescriptorType 5 + bEndpointAddress 0x83 EP 3 IN + bmAttributes 1 + Transfer Type Isochronous + Synch Type None + Usage Type Data + wMaxPacketSize 0x0019 1x 25 bytes + bInterval 1 + Interface Descriptor: + bLength 9 + bDescriptorType 4 + bInterfaceNumber 1 + bAlternateSetting 4 + bNumEndpoints 2 + bInterfaceClass 224 Wireless + bInterfaceSubClass 1 Radio Frequency + bInterfaceProtocol 1 Bluetooth + iInterface 0 + Endpoint Descriptor: + bLength 7 + bDescriptorType 5 + bEndpointAddress 0x03 EP 3 OUT + bmAttributes 1 + Transfer Type Isochronous + Synch Type None + Usage Type Data + wMaxPacketSize 0x0021 1x 33 bytes + bInterval 1 + Endpoint Descriptor: + bLength 7 + bDescriptorType 5 + bEndpointAddress 0x83 EP 3 IN + bmAttributes 1 + Transfer Type Isochronous + Synch Type None + Usage Type Data + wMaxPacketSize 0x0021 1x 33 bytes + bInterval 1 + Interface Descriptor: + bLength 9 + bDescriptorType 4 + bInterfaceNumber 1 + bAlternateSetting 5 + bNumEndpoints 2 + bInterfaceClass 224 Wireless + bInterfaceSubClass 1 Radio Frequency + bInterfaceProtocol 1 Bluetooth + iInterface 0 + Endpoint Descriptor: + bLength 7 + bDescriptorType 5 + bEndpointAddress 0x03 EP 3 OUT + bmAttributes 1 + Transfer Type Isochronous + Synch Type None + Usage Type Data + wMaxPacketSize 0x0031 1x 49 bytes + bInterval 1 + Endpoint Descriptor: + bLength 7 + bDescriptorType 5 + bEndpointAddress 0x83 EP 3 IN + bmAttributes 1 + Transfer Type Isochronous + Synch Type None + Usage Type Data + wMaxPacketSize 0x0031 1x 49 bytes + bInterval 1 + Interface Descriptor: + bLength 9 + bDescriptorType 4 + bInterfaceNumber 2 + bAlternateSetting 0 + bNumEndpoints 0 + bInterfaceClass 254 Application Specific Interface + bInterfaceSubClass 1 Device Firmware Update + bInterfaceProtocol 0 + iInterface 0 + Device Firmware Upgrade Interface Descriptor: + bLength 7 + bDescriptorType 33 + bmAttributes 7 + Will Not Detach + Manifestation Tolerant + Upload Supported + Download Supported + wDetachTimeout 5000 milliseconds + wTransferSize 1023 bytes +Device Status: 0x0000 + (Bus Powered) diff --git a/tools/src/dfu-util/dfuse-pack.py b/tools/linux/src/dfu-util/dfuse-pack.py old mode 100755 new mode 100644 similarity index 97% rename from tools/src/dfu-util/dfuse-pack.py rename to tools/linux/src/dfu-util/dfuse-pack.py index 022df6a..875cc5c --- a/tools/src/dfu-util/dfuse-pack.py +++ b/tools/linux/src/dfu-util/dfuse-pack.py @@ -1,121 +1,121 @@ -#!/usr/bin/python - -# Written by Antonio Galea - 2010/11/18 -# Distributed under Gnu LGPL 3.0 -# see http://www.gnu.org/licenses/lgpl-3.0.txt - -import sys,struct,zlib,os -from optparse import OptionParser - -DEFAULT_DEVICE="0x0483:0xdf11" - -def named(tuple,names): - return dict(zip(names.split(),tuple)) -def consume(fmt,data,names): - n = struct.calcsize(fmt) - return named(struct.unpack(fmt,data[:n]),names),data[n:] -def cstring(string): - return string.split('\0',1)[0] -def compute_crc(data): - return 0xFFFFFFFF & -zlib.crc32(data) -1 - -def parse(file,dump_images=False): - print 'File: "%s"' % file - data = open(file,'rb').read() - crc = compute_crc(data[:-4]) - prefix, data = consume('<5sBIB',data,'signature version size targets') - print '%(signature)s v%(version)d, image size: %(size)d, targets: %(targets)d' % prefix - for t in range(prefix['targets']): - tprefix, data = consume('<6sBI255s2I',data,'signature altsetting named name size elements') - tprefix['num'] = t - if tprefix['named']: - tprefix['name'] = cstring(tprefix['name']) - else: - tprefix['name'] = '' - print '%(signature)s %(num)d, alt setting: %(altsetting)s, name: "%(name)s", size: %(size)d, elements: %(elements)d' % tprefix - tsize = tprefix['size'] - target, data = data[:tsize], data[tsize:] - for e in range(tprefix['elements']): - eprefix, target = consume('<2I',target,'address size') - eprefix['num'] = e - print ' %(num)d, address: 0x%(address)08x, size: %(size)d' % eprefix - esize = eprefix['size'] - image, target = target[:esize], target[esize:] - if dump_images: - out = '%s.target%d.image%d.bin' % (file,t,e) - open(out,'wb').write(image) - print ' DUMPED IMAGE TO "%s"' % out - if len(target): - print "target %d: PARSE ERROR" % t - suffix = named(struct.unpack('<4H3sBI',data[:16]),'device product vendor dfu ufd len crc') - print 'usb: %(vendor)04x:%(product)04x, device: 0x%(device)04x, dfu: 0x%(dfu)04x, %(ufd)s, %(len)d, 0x%(crc)08x' % suffix - if crc != suffix['crc']: - print "CRC ERROR: computed crc32 is 0x%08x" % crc - data = data[16:] - if data: - print "PARSE ERROR" - -def build(file,targets,device=DEFAULT_DEVICE): - data = '' - for t,target in enumerate(targets): - tdata = '' - for image in target: - tdata += struct.pack('<2I',image['address'],len(image['data']))+image['data'] - tdata = struct.pack('<6sBI255s2I','Target',0,1,'ST...',len(tdata),len(target)) + tdata - data += tdata - data = struct.pack('<5sBIB','DfuSe',1,len(data)+11,len(targets)) + data - v,d=map(lambda x: int(x,0) & 0xFFFF, device.split(':',1)) - data += struct.pack('<4H3sB',0,d,v,0x011a,'UFD',16) - crc = compute_crc(data) - data += struct.pack(' and -Harald Welte . Over time, nearly complete -support of DFU 1.0, DFU 1.1 and DfuSe ("1.1a") has been added. -.SH LICENCE -.B dfu-util -is covered by the GNU General Public License (GPL), version 2 or later. -.SH COPYRIGHT -This manual page was originally written by Uwe Hermann , -and is now part of the dfu-util project. +.TH DFU-UTIL 1 "September 23, 2012" +.SH NAME +dfu-util \- Device firmware update (DFU) USB programmer +.SH SYNOPSIS +.\" Listing devices +.HP +.B dfu-util +.B \-l +.RB [\| \-v \|] +.RB [\| \-d +.IR vid:pid [\|, vid:pid \|]\|] +.RB [\| \-p +.IR path \|] +.RB [\| \-c +.IR configuration \|] +.RB [\| \-i +.IR interface \|] +.RB [\| \-a +.IR alt-intf \|] +.RB [\| \-S +.IR serial [\|, serial \|]\|] +.\" Download to or upload from device +.HP +.B dfu-util +.RB [\| \-v \|] +.RB [\| \-d +.IR vid:pid [\|, vid:pid \|]\|] +.RB [\| \-p +.IR path \|] +.RB [\| \-c +.IR configuration \|] +.RB [\| \-i +.IR interface \|] +.RB [\| \-a +.IR alt-intf \|] +.RB [\| \-S +.IR serial [\|, serial \|]\|] +.RB [\| \-t +.IR size \|] +.RB [\| \-Z +.IR size \|] +.RB [\| \-s +.IR address \|] +.RB [\| \-R \|] +.RB [\| \-D \||\| \-U +.IR file \|] +.\" --help and --version +.HP +.B dfu-util +.RB [\| \-hV \|] +.SH DESCRIPTION +.B dfu-util +is a program that implements the host (computer) side of the USB DFU +(Universal Serial Bus Device Firmware Upgrade) protocol. +.sp +dfu-util communicates with devices that implement the device side of the +USB DFU protocol, and is often used to upgrade the firmware of such +devices. +.SH OPTIONS +.TP +.B "\-l, \-\-list" +List the currently attached DFU capable USB devices. +.TP +.BR "\-d, \-\-device" " [\fIRun-Time VENDOR\fP]:[\fIRun-Time PRODUCT\fP][,[\fIDFU Mode VENDOR\fP]:[\fIDFU Mode PRODUCT\fP]]" +.RS +Specify run-time and/or DFU mode vendor and/or product IDs of the DFU device +to work with. \fBVENDOR\fP and \fBPRODUCT\fP are hexadecimal numbers (no prefix +needed), "*" (match any), or "-" (match nothing). By default, any DFU capable +device in either run-time or DFU mode will be considered. +.sp +If you only have one standards-compliant DFU device attached to your computer, +this parameter is optional. However, as soon as you have multiple DFU devices +connected, dfu-util will detect this and abort, asking you to specify which +device to use. +.sp +If only run-time IDs are specified (e.g. "\fB--device 1457:51ab\fP"), then in +addition to the specified run-time IDs, any DFU mode devices will also be +considered. This is beneficial to allow a DFU capable device to be found +again after a switch to DFU mode, since the vendor and/or product ID of a +device usually changes in DFU mode. +.sp +If only DFU mode IDs are specified (e.g. "\fB--device ,951:26\fP"), then all +run-time devices will be ignored, making it easy to target a specific device in +DFU mode. +.sp +If both run-time and DFU mode IDs are specified (e.g. "\fB--device +1457:51ab,:2bc\fP"), then unspecified DFU mode components will use the run-time +value specified. +.sp +Examples: +.TP +.B "--device 1457:51ab,951:26" +.br +Work with a device in run-time mode with +vendor ID 0x1457 and product ID 0x51ab, or in DFU mode with vendor ID 0x0951 +and product ID 0x0026 +.sp +.TP +.B "--device 1457:51ab,:2bc" +.br +Work with a device in run-time mode with vendor ID 0x1457 and product ID +0x51ab, or in DFU mode with vendor ID 0x1457 and product ID 0x02bc +.sp +.TP +.B "--device 1457:51ab" +.br +Work with a device in run-time mode with vendor ID 0x1457 and product ID +0x51ab, or in DFU mode with any vendor and product ID +.sp +.TP +.B "--device ,951:26" +.br +Work with a device in DFU mode with vendor ID 0x0951 and product ID 0x0026 +.sp +.TP +.B "--device *,-" +.br +Work with any device in run-time mode, and ignore any device in DFU mode +.sp +.TP +.B "--device ," +.br +Ignore any device in run-time mode, and Work with any device in DFU mode +.RE +.TP +.BR "\-p, \-\-path" " BUS-PORT. ... .PORT" +Specify the path to the DFU device. +.TP +.BR "\-c, \-\-cfg" " CONFIG-NR" +Specify the configuration of the DFU device. Note that this is only used for matching, the configuration is not set by dfu-util. +.TP +.BR "\-i, \-\-intf" " INTF-NR" +Specify the DFU interface number. +.TP +.BR "\-a, \-\-alt" " ALT" +Specify the altsetting of the DFU interface by name or by number. +.TP +.BR "\-S, \-\-serial" " [\fIRun-Time SERIAL\fP][,[\fIDFU Mode SERIAL\fP]]" +Specify the run-time and DFU mode serial numbers used to further restrict +device matches. If multiple, identical DFU devices are simultaneously +connected to a system then vendor and product ID will be insufficient for +targeting a single device. In this situation, it may be possible to use this +parameter to specify a serial number which also must match. +.sp +If only a single serial number is specified, then the same serial number is +used in both run-time and DFU mode. An empty serial number will match any +serial number in the corresponding mode. +.TP +.B "\-t, \-\-transfer-size" " SIZE" +Specify the number of bytes per USB transfer. The optimal value is +usually determined automatically so this option is rarely useful. If +you need to use this option for a device, please report it as a bug. +.TP +.B "\-Z, \-\-upload-size" " SIZE" +Specify the expected upload size, in bytes. +.TP +.BR "\-U, \-\-upload" " FILE" +Read firmware from device into +.BR FILE . +.TP +.BR "\-D, \-\-download" " FILE" +Write firmware from +.B FILE +into device. When FILE is \-, the firmware is read from stdin. +.TP +.B "\-R, \-\-reset" +Issue USB reset signalling after upload or download has finished. +.TP +.BR "\-s, \-\-dfuse-address" " address" +Specify target address for raw binary download/upload on DfuSe devices. Do +.B not +use this for downloading DfuSe (.dfu) files. Modifiers can be added +to the address, separated by a colon, to perform special DfuSE commands such +as "leave" DFU mode, "unprotect" and "mass-erase" flash memory. +.TP +.B "\-v, \-\-verbose" +Print more information about dfu-util's operation. A second +.B -v +will turn on verbose logging of USB requests. Repeat this option to further +increase verbosity. +.TP +.B "\-h, \-\-help" +Show a help text and exit. +.TP +.B "\-V, \-\-version" +Show version information and exit. +.SH EXAMPLES +.SS Using dfu-util in the OpenMoko project +(with the Neo1973 hardware) +.PP +Flashing the rootfs: +.br +.B " $ dfu-util -a rootfs -R -D /path/to/openmoko-devel-image.jffs2" +.PP +Flashing the kernel: +.br +.B " $ dfu-util -a kernel -R -D /path/to/uImage" +.PP +Flashing the bootloader: +.br +.B " $ dfu-util -a u-boot -R -D /path/to/u-boot.bin" +.PP +Copying a kernel into RAM: +.br +.B " $ dfu-util -a 0 -R -D /path/to/uImage" +.sp +Once this has finished, the kernel will be available at the default load +address of 0x32000000 in Neo1973 RAM. +.B Note: +You cannot transfer more than 2MB of data into RAM using this method. +.sp +.SS Using dfu-util with a DfuSe device +.PP +Flashing a +.B .dfu +(special DfuSe format) file to the device: +.br +.B " $ dfu-util -a 0 -D /path/to/dfuse-image.dfu" +.PP +Reading out 1 KB of flash starting at address 0x8000000: +.br +.B " $ dfu-util -a 0 -s 0x08000000:1024 -U newfile.bin" +.PP +Flashing a binary file to address 0x8004000 of device memory and +ask the device to leave DFU mode: +.br +.B " $ dfu-util -a 0 -s 0x08004000:leave -D /path/to/image.bin" +.\" There are no bugs of course +.SH BUGS +Please report any bugs to the dfu-util mailing list at +.BR dfu-util@lists.gnumonks.org . +Please use the +.IR --verbose " option (repeated as necessary) to provide more" +information in your bug report. +.SH SEE ALSO +The dfu-util home page is +.B http://dfu-util.gnumonks.org +.SH HISTORY +dfu-util was originally written for the OpenMoko project by +Weston Schmidt and +Harald Welte . Over time, nearly complete +support of DFU 1.0, DFU 1.1 and DfuSe ("1.1a") has been added. +.SH LICENCE +.B dfu-util +is covered by the GNU General Public License (GPL), version 2 or later. +.SH COPYRIGHT +This manual page was originally written by Uwe Hermann , +and is now part of the dfu-util project. diff --git a/tools/src/dfu-util/msvc/README_msvc.txt b/tools/linux/src/dfu-util/msvc/README_msvc.txt old mode 100755 new mode 100644 similarity index 97% rename from tools/src/dfu-util/msvc/README_msvc.txt rename to tools/linux/src/dfu-util/msvc/README_msvc.txt index 25aeeed..6e68ec6 --- a/tools/src/dfu-util/msvc/README_msvc.txt +++ b/tools/linux/src/dfu-util/msvc/README_msvc.txt @@ -1,10 +1,10 @@ -# (C) Roger Meier -# (C) Pascal Schweizer -# msvc folder is GPL-2.0+, LGPL-2.1+, BSD-3-Clause or MIT license(SPDX) - -Building dfu-util native on Windows with Visual Studio - -3rd party dependencies: -- libusbx ( git clone https://github.com/libusbx/libusbx.git ) - - getopt (part of libusbx: libusbx/examples/getopt) - +# (C) Roger Meier +# (C) Pascal Schweizer +# msvc folder is GPL-2.0+, LGPL-2.1+, BSD-3-Clause or MIT license(SPDX) + +Building dfu-util native on Windows with Visual Studio + +3rd party dependencies: +- libusbx ( git clone https://github.com/libusbx/libusbx.git ) + - getopt (part of libusbx: libusbx/examples/getopt) + diff --git a/tools/src/dfu-util/msvc/dfu-suffix_2010.vcxproj b/tools/linux/src/dfu-util/msvc/dfu-suffix_2010.vcxproj old mode 100755 new mode 100644 similarity index 97% rename from tools/src/dfu-util/msvc/dfu-suffix_2010.vcxproj rename to tools/linux/src/dfu-util/msvc/dfu-suffix_2010.vcxproj index 87b7ead..0c316c2 --- a/tools/src/dfu-util/msvc/dfu-suffix_2010.vcxproj +++ b/tools/linux/src/dfu-util/msvc/dfu-suffix_2010.vcxproj @@ -1,100 +1,100 @@ - - - - - Debug - Win32 - - - Release - Win32 - - - - {8F7600A2-3B37-4956-B39B-A1D43EF29EDA} - dfusuffix - dfu-suffix - - - - Application - true - MultiByte - - - Application - false - true - MultiByte - - - - - - - - - - - - - $(SolutionDir)..\..\libusbx\examples\getopt;$(SolutionDir)..\..\libusbx\libusb;$(IncludePath) - $(SolutionDir)..\$(Platform)\$(ProjectName)\$(Configuration)\ - $(SolutionDir)..\$(Platform)\$(ProjectName)\$(Configuration)\ - $(SolutionDir)..\$(Platform)\$(ProjectName)\$(Configuration)\dll;$(LibraryPath) - $(VCInstallDir)atlmfc\lib;$(VCInstallDir)lib - $(ExecutablePath) - - - $(ExecutablePath) - $(SolutionDir)..\..\libusbx\examples\getopt;$(SolutionDir)..\..\libusbx\libusb;$(IncludePath) - $(SolutionDir)..\$(Platform)\$(ProjectName)\$(Configuration)\dll;$(LibraryPath) - $(SolutionDir)..\$(Platform)\$(ProjectName)\$(Configuration)\ - $(SolutionDir)..\$(Platform)\$(ProjectName)\$(Configuration)\ - - - - Level3 - Disabled - HAVE_WINDOWS_H;_CRT_SECURE_NO_WARNINGS;%(PreprocessorDefinitions) - MultiThreadedDebug - - - true - - - - - Level3 - MaxSpeed - true - true - HAVE_WINDOWS_H;_CRT_SECURE_NO_WARNINGS;%(PreprocessorDefinitions) - MultiThreaded - - - true - true - true - - - - - - - - - - - - - {a2169bc8-cf99-40bf-83f3-b0e38f7067bd} - - - {349ee8f9-7d25-4909-aaf5-ff3fade72187} - - - - - + + + + + Debug + Win32 + + + Release + Win32 + + + + {8F7600A2-3B37-4956-B39B-A1D43EF29EDA} + dfusuffix + dfu-suffix + + + + Application + true + MultiByte + + + Application + false + true + MultiByte + + + + + + + + + + + + + $(SolutionDir)..\..\libusbx\examples\getopt;$(SolutionDir)..\..\libusbx\libusb;$(IncludePath) + $(SolutionDir)..\$(Platform)\$(ProjectName)\$(Configuration)\ + $(SolutionDir)..\$(Platform)\$(ProjectName)\$(Configuration)\ + $(SolutionDir)..\$(Platform)\$(ProjectName)\$(Configuration)\dll;$(LibraryPath) + $(VCInstallDir)atlmfc\lib;$(VCInstallDir)lib + $(ExecutablePath) + + + $(ExecutablePath) + $(SolutionDir)..\..\libusbx\examples\getopt;$(SolutionDir)..\..\libusbx\libusb;$(IncludePath) + $(SolutionDir)..\$(Platform)\$(ProjectName)\$(Configuration)\dll;$(LibraryPath) + $(SolutionDir)..\$(Platform)\$(ProjectName)\$(Configuration)\ + $(SolutionDir)..\$(Platform)\$(ProjectName)\$(Configuration)\ + + + + Level3 + Disabled + HAVE_WINDOWS_H;_CRT_SECURE_NO_WARNINGS;%(PreprocessorDefinitions) + MultiThreadedDebug + + + true + + + + + Level3 + MaxSpeed + true + true + HAVE_WINDOWS_H;_CRT_SECURE_NO_WARNINGS;%(PreprocessorDefinitions) + MultiThreaded + + + true + true + true + + + + + + + + + + + + + {a2169bc8-cf99-40bf-83f3-b0e38f7067bd} + + + {349ee8f9-7d25-4909-aaf5-ff3fade72187} + + + + + \ No newline at end of file diff --git a/tools/src/dfu-util/msvc/dfu-util_2010.sln b/tools/linux/src/dfu-util/msvc/dfu-util_2010.sln old mode 100755 new mode 100644 similarity index 98% rename from tools/src/dfu-util/msvc/dfu-util_2010.sln rename to tools/linux/src/dfu-util/msvc/dfu-util_2010.sln index e6c932c..ef79723 --- a/tools/src/dfu-util/msvc/dfu-util_2010.sln +++ b/tools/linux/src/dfu-util/msvc/dfu-util_2010.sln @@ -1,54 +1,54 @@ - -Microsoft Visual Studio Solution File, Format Version 11.00 -# Visual Studio 2010 -Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "dfu-util", "dfu-util_2010.vcxproj", "{0E071A60-7EF2-4427-BAA8-9143CACB5BCB}" -EndProject -Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{C4F8746D-B27E-4806-95E5-2052174E923B}" -EndProject -Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "dfu-suffix", "dfu-suffix_2010.vcxproj", "{8F7600A2-3B37-4956-B39B-A1D43EF29EDA}" -EndProject -Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "getopt_2010", "..\..\libusbx\msvc\getopt_2010.vcxproj", "{AE83E1B4-CE06-47EE-B7A3-C3A1D7C2D71E}" -EndProject -Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "libusb-1.0 (static)", "..\..\libusbx\msvc\libusb_static_2010.vcxproj", "{349EE8F9-7D25-4909-AAF5-FF3FADE72187}" -EndProject -Global - GlobalSection(SolutionConfigurationPlatforms) = preSolution - Debug|Win32 = Debug|Win32 - Debug|x64 = Debug|x64 - Release|Win32 = Release|Win32 - Release|x64 = Release|x64 - EndGlobalSection - GlobalSection(ProjectConfigurationPlatforms) = postSolution - {0E071A60-7EF2-4427-BAA8-9143CACB5BCB}.Debug|Win32.ActiveCfg = Debug|Win32 - {0E071A60-7EF2-4427-BAA8-9143CACB5BCB}.Debug|Win32.Build.0 = Debug|Win32 - {0E071A60-7EF2-4427-BAA8-9143CACB5BCB}.Debug|x64.ActiveCfg = Debug|Win32 - {0E071A60-7EF2-4427-BAA8-9143CACB5BCB}.Release|Win32.ActiveCfg = Release|Win32 - {0E071A60-7EF2-4427-BAA8-9143CACB5BCB}.Release|Win32.Build.0 = Release|Win32 - {0E071A60-7EF2-4427-BAA8-9143CACB5BCB}.Release|x64.ActiveCfg = Release|Win32 - {8F7600A2-3B37-4956-B39B-A1D43EF29EDA}.Debug|Win32.ActiveCfg = Debug|Win32 - {8F7600A2-3B37-4956-B39B-A1D43EF29EDA}.Debug|Win32.Build.0 = Debug|Win32 - {8F7600A2-3B37-4956-B39B-A1D43EF29EDA}.Debug|x64.ActiveCfg = Debug|Win32 - {8F7600A2-3B37-4956-B39B-A1D43EF29EDA}.Release|Win32.ActiveCfg = Release|Win32 - {8F7600A2-3B37-4956-B39B-A1D43EF29EDA}.Release|Win32.Build.0 = Release|Win32 - {8F7600A2-3B37-4956-B39B-A1D43EF29EDA}.Release|x64.ActiveCfg = Release|Win32 - {AE83E1B4-CE06-47EE-B7A3-C3A1D7C2D71E}.Debug|Win32.ActiveCfg = Debug|Win32 - {AE83E1B4-CE06-47EE-B7A3-C3A1D7C2D71E}.Debug|Win32.Build.0 = Debug|Win32 - {AE83E1B4-CE06-47EE-B7A3-C3A1D7C2D71E}.Debug|x64.ActiveCfg = Debug|x64 - {AE83E1B4-CE06-47EE-B7A3-C3A1D7C2D71E}.Debug|x64.Build.0 = Debug|x64 - {AE83E1B4-CE06-47EE-B7A3-C3A1D7C2D71E}.Release|Win32.ActiveCfg = Release|Win32 - {AE83E1B4-CE06-47EE-B7A3-C3A1D7C2D71E}.Release|Win32.Build.0 = Release|Win32 - {AE83E1B4-CE06-47EE-B7A3-C3A1D7C2D71E}.Release|x64.ActiveCfg = Release|x64 - {AE83E1B4-CE06-47EE-B7A3-C3A1D7C2D71E}.Release|x64.Build.0 = Release|x64 - {349EE8F9-7D25-4909-AAF5-FF3FADE72187}.Debug|Win32.ActiveCfg = Debug|Win32 - {349EE8F9-7D25-4909-AAF5-FF3FADE72187}.Debug|Win32.Build.0 = Debug|Win32 - {349EE8F9-7D25-4909-AAF5-FF3FADE72187}.Debug|x64.ActiveCfg = Debug|x64 - {349EE8F9-7D25-4909-AAF5-FF3FADE72187}.Debug|x64.Build.0 = Debug|x64 - {349EE8F9-7D25-4909-AAF5-FF3FADE72187}.Release|Win32.ActiveCfg = Release|Win32 - {349EE8F9-7D25-4909-AAF5-FF3FADE72187}.Release|Win32.Build.0 = Release|Win32 - {349EE8F9-7D25-4909-AAF5-FF3FADE72187}.Release|x64.ActiveCfg = Release|x64 - {349EE8F9-7D25-4909-AAF5-FF3FADE72187}.Release|x64.Build.0 = Release|x64 - EndGlobalSection - GlobalSection(SolutionProperties) = preSolution - HideSolutionNode = FALSE - EndGlobalSection -EndGlobal + +Microsoft Visual Studio Solution File, Format Version 11.00 +# Visual Studio 2010 +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "dfu-util", "dfu-util_2010.vcxproj", "{0E071A60-7EF2-4427-BAA8-9143CACB5BCB}" +EndProject +Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{C4F8746D-B27E-4806-95E5-2052174E923B}" +EndProject +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "dfu-suffix", "dfu-suffix_2010.vcxproj", "{8F7600A2-3B37-4956-B39B-A1D43EF29EDA}" +EndProject +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "getopt_2010", "..\..\libusbx\msvc\getopt_2010.vcxproj", "{AE83E1B4-CE06-47EE-B7A3-C3A1D7C2D71E}" +EndProject +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "libusb-1.0 (static)", "..\..\libusbx\msvc\libusb_static_2010.vcxproj", "{349EE8F9-7D25-4909-AAF5-FF3FADE72187}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Win32 = Debug|Win32 + Debug|x64 = Debug|x64 + Release|Win32 = Release|Win32 + Release|x64 = Release|x64 + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {0E071A60-7EF2-4427-BAA8-9143CACB5BCB}.Debug|Win32.ActiveCfg = Debug|Win32 + {0E071A60-7EF2-4427-BAA8-9143CACB5BCB}.Debug|Win32.Build.0 = Debug|Win32 + {0E071A60-7EF2-4427-BAA8-9143CACB5BCB}.Debug|x64.ActiveCfg = Debug|Win32 + {0E071A60-7EF2-4427-BAA8-9143CACB5BCB}.Release|Win32.ActiveCfg = Release|Win32 + {0E071A60-7EF2-4427-BAA8-9143CACB5BCB}.Release|Win32.Build.0 = Release|Win32 + {0E071A60-7EF2-4427-BAA8-9143CACB5BCB}.Release|x64.ActiveCfg = Release|Win32 + {8F7600A2-3B37-4956-B39B-A1D43EF29EDA}.Debug|Win32.ActiveCfg = Debug|Win32 + {8F7600A2-3B37-4956-B39B-A1D43EF29EDA}.Debug|Win32.Build.0 = Debug|Win32 + {8F7600A2-3B37-4956-B39B-A1D43EF29EDA}.Debug|x64.ActiveCfg = Debug|Win32 + {8F7600A2-3B37-4956-B39B-A1D43EF29EDA}.Release|Win32.ActiveCfg = Release|Win32 + {8F7600A2-3B37-4956-B39B-A1D43EF29EDA}.Release|Win32.Build.0 = Release|Win32 + {8F7600A2-3B37-4956-B39B-A1D43EF29EDA}.Release|x64.ActiveCfg = Release|Win32 + {AE83E1B4-CE06-47EE-B7A3-C3A1D7C2D71E}.Debug|Win32.ActiveCfg = Debug|Win32 + {AE83E1B4-CE06-47EE-B7A3-C3A1D7C2D71E}.Debug|Win32.Build.0 = Debug|Win32 + {AE83E1B4-CE06-47EE-B7A3-C3A1D7C2D71E}.Debug|x64.ActiveCfg = Debug|x64 + {AE83E1B4-CE06-47EE-B7A3-C3A1D7C2D71E}.Debug|x64.Build.0 = Debug|x64 + {AE83E1B4-CE06-47EE-B7A3-C3A1D7C2D71E}.Release|Win32.ActiveCfg = Release|Win32 + {AE83E1B4-CE06-47EE-B7A3-C3A1D7C2D71E}.Release|Win32.Build.0 = Release|Win32 + {AE83E1B4-CE06-47EE-B7A3-C3A1D7C2D71E}.Release|x64.ActiveCfg = Release|x64 + {AE83E1B4-CE06-47EE-B7A3-C3A1D7C2D71E}.Release|x64.Build.0 = Release|x64 + {349EE8F9-7D25-4909-AAF5-FF3FADE72187}.Debug|Win32.ActiveCfg = Debug|Win32 + {349EE8F9-7D25-4909-AAF5-FF3FADE72187}.Debug|Win32.Build.0 = Debug|Win32 + {349EE8F9-7D25-4909-AAF5-FF3FADE72187}.Debug|x64.ActiveCfg = Debug|x64 + {349EE8F9-7D25-4909-AAF5-FF3FADE72187}.Debug|x64.Build.0 = Debug|x64 + {349EE8F9-7D25-4909-AAF5-FF3FADE72187}.Release|Win32.ActiveCfg = Release|Win32 + {349EE8F9-7D25-4909-AAF5-FF3FADE72187}.Release|Win32.Build.0 = Release|Win32 + {349EE8F9-7D25-4909-AAF5-FF3FADE72187}.Release|x64.ActiveCfg = Release|x64 + {349EE8F9-7D25-4909-AAF5-FF3FADE72187}.Release|x64.Build.0 = Release|x64 + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection +EndGlobal diff --git a/tools/src/dfu-util/msvc/dfu-util_2010.vcxproj b/tools/linux/src/dfu-util/msvc/dfu-util_2010.vcxproj old mode 100755 new mode 100644 similarity index 97% rename from tools/src/dfu-util/msvc/dfu-util_2010.vcxproj rename to tools/linux/src/dfu-util/msvc/dfu-util_2010.vcxproj index cb07b64..17a8bee --- a/tools/src/dfu-util/msvc/dfu-util_2010.vcxproj +++ b/tools/linux/src/dfu-util/msvc/dfu-util_2010.vcxproj @@ -1,120 +1,120 @@ - - - - - Debug - Win32 - - - Release - Win32 - - - - {0E071A60-7EF2-4427-BAA8-9143CACB5BCB} - dfuutil - dfu-util - - - - Application - true - MultiByte - - - Application - false - true - MultiByte - - - - - - - - - - - - - $(SolutionDir)..\..\libusbx\examples\getopt;$(SolutionDir)..\..\libusbx\libusb;$(IncludePath) - $(SolutionDir)..\$(Platform)\$(ProjectName)\$(Configuration)\ - $(SolutionDir)..\$(Platform)\$(ProjectName)\$(Configuration)\ - $(SolutionDir)..\$(Platform)\getopt\$(Configuration);$(LibraryPath) - $(VCInstallDir)atlmfc\lib;$(VCInstallDir)lib - $(ExecutablePath) - - - $(ExecutablePath) - $(SolutionDir)..\..\libusbx\examples\getopt;$(SolutionDir)..\..\libusbx\libusb;$(IncludePath) - $(SolutionDir)..\$(Platform)\getopt\$(Configuration);$(LibraryPath) - $(SolutionDir)..\$(Platform)\$(ProjectName)\$(Configuration)\ - $(SolutionDir)..\$(Platform)\$(ProjectName)\$(Configuration)\ - - - - Level3 - Disabled - HAVE_WINDOWS_H;_CRT_SECURE_NO_WARNINGS;%(PreprocessorDefinitions) - MultiThreadedDebug - - - true - - - - - - - - - Level3 - MaxSpeed - true - true - HAVE_WINDOWS_H;_CRT_SECURE_NO_WARNINGS;%(PreprocessorDefinitions) - MultiThreaded - - - true - true - true - - - copy $(SolutionDir)..\$(Platform)\$(Configuration)\dll\libusb-1.0.dll $(SolutionDir)..\$(Platform)\$(ProjectName)\$(Configuration)\ - - - - - - - - - - - - - - - - - - - - - - - - - - {a2169bc8-cf99-40bf-83f3-b0e38f7067bd} - - - {349ee8f9-7d25-4909-aaf5-ff3fade72187} - - - - - + + + + + Debug + Win32 + + + Release + Win32 + + + + {0E071A60-7EF2-4427-BAA8-9143CACB5BCB} + dfuutil + dfu-util + + + + Application + true + MultiByte + + + Application + false + true + MultiByte + + + + + + + + + + + + + $(SolutionDir)..\..\libusbx\examples\getopt;$(SolutionDir)..\..\libusbx\libusb;$(IncludePath) + $(SolutionDir)..\$(Platform)\$(ProjectName)\$(Configuration)\ + $(SolutionDir)..\$(Platform)\$(ProjectName)\$(Configuration)\ + $(SolutionDir)..\$(Platform)\getopt\$(Configuration);$(LibraryPath) + $(VCInstallDir)atlmfc\lib;$(VCInstallDir)lib + $(ExecutablePath) + + + $(ExecutablePath) + $(SolutionDir)..\..\libusbx\examples\getopt;$(SolutionDir)..\..\libusbx\libusb;$(IncludePath) + $(SolutionDir)..\$(Platform)\getopt\$(Configuration);$(LibraryPath) + $(SolutionDir)..\$(Platform)\$(ProjectName)\$(Configuration)\ + $(SolutionDir)..\$(Platform)\$(ProjectName)\$(Configuration)\ + + + + Level3 + Disabled + HAVE_WINDOWS_H;_CRT_SECURE_NO_WARNINGS;%(PreprocessorDefinitions) + MultiThreadedDebug + + + true + + + + + + + + + Level3 + MaxSpeed + true + true + HAVE_WINDOWS_H;_CRT_SECURE_NO_WARNINGS;%(PreprocessorDefinitions) + MultiThreaded + + + true + true + true + + + copy $(SolutionDir)..\$(Platform)\$(Configuration)\dll\libusb-1.0.dll $(SolutionDir)..\$(Platform)\$(ProjectName)\$(Configuration)\ + + + + + + + + + + + + + + + + + + + + + + + + + + {a2169bc8-cf99-40bf-83f3-b0e38f7067bd} + + + {349ee8f9-7d25-4909-aaf5-ff3fade72187} + + + + + \ No newline at end of file diff --git a/tools/src/dfu-util/src/Makefile.am b/tools/linux/src/dfu-util/src/Makefile.am old mode 100755 new mode 100644 similarity index 94% rename from tools/src/dfu-util/src/Makefile.am rename to tools/linux/src/dfu-util/src/Makefile.am index b1f810a..70179c4 --- a/tools/src/dfu-util/src/Makefile.am +++ b/tools/linux/src/dfu-util/src/Makefile.am @@ -1,28 +1,28 @@ -AM_CFLAGS = -Wall -Wextra - -bin_PROGRAMS = dfu-util dfu-suffix dfu-prefix -dfu_util_SOURCES = main.c \ - portable.h \ - dfu_load.c \ - dfu_load.h \ - dfu_util.c \ - dfu_util.h \ - dfuse.c \ - dfuse.h \ - dfuse_mem.c \ - dfuse_mem.h \ - dfu.c \ - dfu.h \ - usb_dfu.h \ - dfu_file.c \ - dfu_file.h \ - quirks.c \ - quirks.h - -dfu_suffix_SOURCES = suffix.c \ - dfu_file.h \ - dfu_file.c - -dfu_prefix_SOURCES = prefix.c \ - dfu_file.h \ - dfu_file.c +AM_CFLAGS = -Wall -Wextra + +bin_PROGRAMS = dfu-util dfu-suffix dfu-prefix +dfu_util_SOURCES = main.c \ + portable.h \ + dfu_load.c \ + dfu_load.h \ + dfu_util.c \ + dfu_util.h \ + dfuse.c \ + dfuse.h \ + dfuse_mem.c \ + dfuse_mem.h \ + dfu.c \ + dfu.h \ + usb_dfu.h \ + dfu_file.c \ + dfu_file.h \ + quirks.c \ + quirks.h + +dfu_suffix_SOURCES = suffix.c \ + dfu_file.h \ + dfu_file.c + +dfu_prefix_SOURCES = prefix.c \ + dfu_file.h \ + dfu_file.c diff --git a/tools/src/dfu-util/src/dfu.c b/tools/linux/src/dfu-util/src/dfu.c old mode 100755 new mode 100644 similarity index 96% rename from tools/src/dfu-util/src/dfu.c rename to tools/linux/src/dfu-util/src/dfu.c index 050ad50..14d7673 --- a/tools/src/dfu-util/src/dfu.c +++ b/tools/linux/src/dfu-util/src/dfu.c @@ -1,357 +1,357 @@ -/* - * Low-level DFU communication routines, originally taken from - * $Id: dfu.c,v 1.3 2006/06/20 06:28:04 schmidtw Exp $ - * (part of dfu-programmer). - * - * Copyright 2005-2006 Weston Schmidt - * Copyright 2011-2014 Tormod Volden - * - * 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 -#include - -#include - -#include "portable.h" -#include "dfu.h" -#include "quirks.h" - -static int dfu_timeout = 5000; /* 5 seconds - default */ - -/* - * 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( libusb_device_handle *device, - const unsigned short interface, - const unsigned short timeout ) -{ - return libusb_control_transfer( device, - /* bmRequestType */ LIBUSB_ENDPOINT_OUT | LIBUSB_REQUEST_TYPE_CLASS | LIBUSB_RECIPIENT_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( libusb_device_handle *device, - const unsigned short interface, - const unsigned short length, - const unsigned short transaction, - unsigned char* data ) -{ - int status; - - status = libusb_control_transfer( device, - /* bmRequestType */ LIBUSB_ENDPOINT_OUT | LIBUSB_REQUEST_TYPE_CLASS | LIBUSB_RECIPIENT_INTERFACE, - /* bRequest */ DFU_DNLOAD, - /* wValue */ transaction, - /* wIndex */ interface, - /* Data */ data, - /* wLength */ length, - dfu_timeout ); - 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( libusb_device_handle *device, - const unsigned short interface, - const unsigned short length, - const unsigned short transaction, - unsigned char* data ) -{ - int status; - - status = libusb_control_transfer( device, - /* bmRequestType */ LIBUSB_ENDPOINT_IN | LIBUSB_REQUEST_TYPE_CLASS | LIBUSB_RECIPIENT_INTERFACE, - /* bRequest */ DFU_UPLOAD, - /* wValue */ transaction, - /* wIndex */ interface, - /* Data */ data, - /* wLength */ length, - dfu_timeout ); - 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 dfu_if *dif, struct dfu_status *status ) -{ - unsigned char buffer[6]; - int result; - - /* Initialize the status data structure */ - status->bStatus = DFU_STATUS_ERROR_UNKNOWN; - status->bwPollTimeout = 0; - status->bState = STATE_DFU_ERROR; - status->iString = 0; - - result = libusb_control_transfer( dif->dev_handle, - /* bmRequestType */ LIBUSB_ENDPOINT_IN | LIBUSB_REQUEST_TYPE_CLASS | LIBUSB_RECIPIENT_INTERFACE, - /* bRequest */ DFU_GETSTATUS, - /* wValue */ 0, - /* wIndex */ dif->interface, - /* Data */ buffer, - /* wLength */ 6, - dfu_timeout ); - - if( 6 == result ) { - status->bStatus = buffer[0]; - if (dif->quirks & QUIRK_POLLTIMEOUT) - status->bwPollTimeout = DEFAULT_POLLTIMEOUT; - else - 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( libusb_device_handle *device, - const unsigned short interface ) -{ - return libusb_control_transfer( device, - /* bmRequestType */ LIBUSB_ENDPOINT_OUT| LIBUSB_REQUEST_TYPE_CLASS | LIBUSB_RECIPIENT_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( libusb_device_handle *device, - const unsigned short interface ) -{ - int result; - unsigned char buffer[1]; - - result = libusb_control_transfer( device, - /* bmRequestType */ LIBUSB_ENDPOINT_IN | LIBUSB_REQUEST_TYPE_CLASS | LIBUSB_RECIPIENT_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 -1; - - /* 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( libusb_device_handle *device, - const unsigned short interface ) -{ - return libusb_control_transfer( device, - /* bmRequestType */ LIBUSB_ENDPOINT_OUT | LIBUSB_REQUEST_TYPE_CLASS | LIBUSB_RECIPIENT_INTERFACE, - /* bRequest */ DFU_ABORT, - /* wValue */ 0, - /* wIndex */ interface, - /* Data */ NULL, - /* wLength */ 0, - dfu_timeout ); -} - - -const char* dfu_state_to_string( int state ) -{ - const char *message; - - 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; - default: - message = NULL; - 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 memory 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]; -} - -int dfu_abort_to_idle(struct dfu_if *dif) -{ - int ret; - struct dfu_status dst; - - ret = dfu_abort(dif->dev_handle, dif->interface); - if (ret < 0) { - errx(EX_IOERR, "Error sending dfu abort request"); - exit(1); - } - ret = dfu_get_status(dif, &dst); - if (ret < 0) { - errx(EX_IOERR, "Error during abort get_status"); - exit(1); - } - if (dst.bState != DFU_STATE_dfuIDLE) { - errx(EX_IOERR, "Failed to enter idle state on abort"); - exit(1); - } - milli_sleep(dst.bwPollTimeout); - return ret; -} +/* + * Low-level DFU communication routines, originally taken from + * $Id: dfu.c,v 1.3 2006/06/20 06:28:04 schmidtw Exp $ + * (part of dfu-programmer). + * + * Copyright 2005-2006 Weston Schmidt + * Copyright 2011-2014 Tormod Volden + * + * 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 +#include + +#include + +#include "portable.h" +#include "dfu.h" +#include "quirks.h" + +static int dfu_timeout = 5000; /* 5 seconds - default */ + +/* + * 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( libusb_device_handle *device, + const unsigned short interface, + const unsigned short timeout ) +{ + return libusb_control_transfer( device, + /* bmRequestType */ LIBUSB_ENDPOINT_OUT | LIBUSB_REQUEST_TYPE_CLASS | LIBUSB_RECIPIENT_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( libusb_device_handle *device, + const unsigned short interface, + const unsigned short length, + const unsigned short transaction, + unsigned char* data ) +{ + int status; + + status = libusb_control_transfer( device, + /* bmRequestType */ LIBUSB_ENDPOINT_OUT | LIBUSB_REQUEST_TYPE_CLASS | LIBUSB_RECIPIENT_INTERFACE, + /* bRequest */ DFU_DNLOAD, + /* wValue */ transaction, + /* wIndex */ interface, + /* Data */ data, + /* wLength */ length, + dfu_timeout ); + 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( libusb_device_handle *device, + const unsigned short interface, + const unsigned short length, + const unsigned short transaction, + unsigned char* data ) +{ + int status; + + status = libusb_control_transfer( device, + /* bmRequestType */ LIBUSB_ENDPOINT_IN | LIBUSB_REQUEST_TYPE_CLASS | LIBUSB_RECIPIENT_INTERFACE, + /* bRequest */ DFU_UPLOAD, + /* wValue */ transaction, + /* wIndex */ interface, + /* Data */ data, + /* wLength */ length, + dfu_timeout ); + 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 dfu_if *dif, struct dfu_status *status ) +{ + unsigned char buffer[6]; + int result; + + /* Initialize the status data structure */ + status->bStatus = DFU_STATUS_ERROR_UNKNOWN; + status->bwPollTimeout = 0; + status->bState = STATE_DFU_ERROR; + status->iString = 0; + + result = libusb_control_transfer( dif->dev_handle, + /* bmRequestType */ LIBUSB_ENDPOINT_IN | LIBUSB_REQUEST_TYPE_CLASS | LIBUSB_RECIPIENT_INTERFACE, + /* bRequest */ DFU_GETSTATUS, + /* wValue */ 0, + /* wIndex */ dif->interface, + /* Data */ buffer, + /* wLength */ 6, + dfu_timeout ); + + if( 6 == result ) { + status->bStatus = buffer[0]; + if (dif->quirks & QUIRK_POLLTIMEOUT) + status->bwPollTimeout = DEFAULT_POLLTIMEOUT; + else + 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( libusb_device_handle *device, + const unsigned short interface ) +{ + return libusb_control_transfer( device, + /* bmRequestType */ LIBUSB_ENDPOINT_OUT| LIBUSB_REQUEST_TYPE_CLASS | LIBUSB_RECIPIENT_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( libusb_device_handle *device, + const unsigned short interface ) +{ + int result; + unsigned char buffer[1]; + + result = libusb_control_transfer( device, + /* bmRequestType */ LIBUSB_ENDPOINT_IN | LIBUSB_REQUEST_TYPE_CLASS | LIBUSB_RECIPIENT_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 -1; + + /* 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( libusb_device_handle *device, + const unsigned short interface ) +{ + return libusb_control_transfer( device, + /* bmRequestType */ LIBUSB_ENDPOINT_OUT | LIBUSB_REQUEST_TYPE_CLASS | LIBUSB_RECIPIENT_INTERFACE, + /* bRequest */ DFU_ABORT, + /* wValue */ 0, + /* wIndex */ interface, + /* Data */ NULL, + /* wLength */ 0, + dfu_timeout ); +} + + +const char* dfu_state_to_string( int state ) +{ + const char *message; + + 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; + default: + message = NULL; + 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 memory 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]; +} + +int dfu_abort_to_idle(struct dfu_if *dif) +{ + int ret; + struct dfu_status dst; + + ret = dfu_abort(dif->dev_handle, dif->interface); + if (ret < 0) { + errx(EX_IOERR, "Error sending dfu abort request"); + exit(1); + } + ret = dfu_get_status(dif, &dst); + if (ret < 0) { + errx(EX_IOERR, "Error during abort get_status"); + exit(1); + } + if (dst.bState != DFU_STATE_dfuIDLE) { + errx(EX_IOERR, "Failed to enter idle state on abort"); + exit(1); + } + milli_sleep(dst.bwPollTimeout); + return ret; +} diff --git a/tools/src/dfu-util/src/dfu.h b/tools/linux/src/dfu-util/src/dfu.h old mode 100755 new mode 100644 similarity index 97% rename from tools/src/dfu-util/src/dfu.h rename to tools/linux/src/dfu-util/src/dfu.h index 39f18c3..8e3caeb --- a/tools/src/dfu-util/src/dfu.h +++ b/tools/linux/src/dfu-util/src/dfu.h @@ -1,133 +1,133 @@ -/* - * 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 -#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 - -/* DFU commands */ -#define DFU_DETACH 0 -#define DFU_DNLOAD 1 -#define DFU_UPLOAD 2 -#define DFU_GETSTATUS 3 -#define DFU_CLRSTATUS 4 -#define DFU_GETSTATE 5 -#define DFU_ABORT 6 - -/* DFU interface */ -#define DFU_IFF_DFU 0x0001 /* DFU Mode, (not Runtime) */ - -/* 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; -}; - -struct dfu_if { - struct usb_dfu_func_descriptor func_dfu; - uint16_t quirks; - uint16_t busnum; - uint16_t devnum; - uint16_t vendor; - uint16_t product; - uint16_t bcdDevice; - uint8_t configuration; - uint8_t interface; - uint8_t altsetting; - uint8_t flags; - uint8_t bMaxPacketSize0; - char *alt_name; - char *serial_name; - libusb_device *dev; - libusb_device_handle *dev_handle; - struct dfu_if *next; -}; - -int dfu_detach( libusb_device_handle *device, - const unsigned short interface, - const unsigned short timeout ); -int dfu_download( libusb_device_handle *device, - const unsigned short interface, - const unsigned short length, - const unsigned short transaction, - unsigned char* data ); -int dfu_upload( libusb_device_handle *device, - const unsigned short interface, - const unsigned short length, - const unsigned short transaction, - unsigned char* data ); -int dfu_get_status( struct dfu_if *dif, - struct dfu_status *status ); -int dfu_clear_status( libusb_device_handle *device, - const unsigned short interface ); -int dfu_get_state( libusb_device_handle *device, - const unsigned short interface ); -int dfu_abort( libusb_device_handle *device, - const unsigned short interface ); -int dfu_abort_to_idle( struct dfu_if *dif); - -const char *dfu_state_to_string( int state ); - -const char *dfu_status_to_string( int status ); - -#endif /* DFU_H */ +/* + * 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 +#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 + +/* DFU commands */ +#define DFU_DETACH 0 +#define DFU_DNLOAD 1 +#define DFU_UPLOAD 2 +#define DFU_GETSTATUS 3 +#define DFU_CLRSTATUS 4 +#define DFU_GETSTATE 5 +#define DFU_ABORT 6 + +/* DFU interface */ +#define DFU_IFF_DFU 0x0001 /* DFU Mode, (not Runtime) */ + +/* 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; +}; + +struct dfu_if { + struct usb_dfu_func_descriptor func_dfu; + uint16_t quirks; + uint16_t busnum; + uint16_t devnum; + uint16_t vendor; + uint16_t product; + uint16_t bcdDevice; + uint8_t configuration; + uint8_t interface; + uint8_t altsetting; + uint8_t flags; + uint8_t bMaxPacketSize0; + char *alt_name; + char *serial_name; + libusb_device *dev; + libusb_device_handle *dev_handle; + struct dfu_if *next; +}; + +int dfu_detach( libusb_device_handle *device, + const unsigned short interface, + const unsigned short timeout ); +int dfu_download( libusb_device_handle *device, + const unsigned short interface, + const unsigned short length, + const unsigned short transaction, + unsigned char* data ); +int dfu_upload( libusb_device_handle *device, + const unsigned short interface, + const unsigned short length, + const unsigned short transaction, + unsigned char* data ); +int dfu_get_status( struct dfu_if *dif, + struct dfu_status *status ); +int dfu_clear_status( libusb_device_handle *device, + const unsigned short interface ); +int dfu_get_state( libusb_device_handle *device, + const unsigned short interface ); +int dfu_abort( libusb_device_handle *device, + const unsigned short interface ); +int dfu_abort_to_idle( struct dfu_if *dif); + +const char *dfu_state_to_string( int state ); + +const char *dfu_status_to_string( int status ); + +#endif /* DFU_H */ diff --git a/tools/src/dfu-util/src/dfu_file.c b/tools/linux/src/dfu-util/src/dfu_file.c old mode 100755 new mode 100644 similarity index 97% rename from tools/src/dfu-util/src/dfu_file.c rename to tools/linux/src/dfu-util/src/dfu_file.c index 2f5a585..7c897d4 --- a/tools/src/dfu-util/src/dfu_file.c +++ b/tools/linux/src/dfu-util/src/dfu_file.c @@ -1,444 +1,444 @@ -/* - * Load or store DFU files including suffix and prefix - * - * Copyright 2014 Tormod Volden - * Copyright 2012 Stefan Schmidt - * - * 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 -#include -#include -#include -#include -#include -#include - -#include "portable.h" -#include "dfu_file.h" - -#define DFU_SUFFIX_LENGTH 16 -#define LMDFU_PREFIX_LENGTH 8 -#define LPCDFU_PREFIX_LENGTH 16 -#define PROGRESS_BAR_WIDTH 25 -#define STDIN_CHUNK_SIZE 65536 - -static const unsigned long crc32_table[] = { - 0x00000000, 0x77073096, 0xee0e612c, 0x990951ba, 0x076dc419, 0x706af48f, - 0xe963a535, 0x9e6495a3, 0x0edb8832, 0x79dcb8a4, 0xe0d5e91e, 0x97d2d988, - 0x09b64c2b, 0x7eb17cbd, 0xe7b82d07, 0x90bf1d91, 0x1db71064, 0x6ab020f2, - 0xf3b97148, 0x84be41de, 0x1adad47d, 0x6ddde4eb, 0xf4d4b551, 0x83d385c7, - 0x136c9856, 0x646ba8c0, 0xfd62f97a, 0x8a65c9ec, 0x14015c4f, 0x63066cd9, - 0xfa0f3d63, 0x8d080df5, 0x3b6e20c8, 0x4c69105e, 0xd56041e4, 0xa2677172, - 0x3c03e4d1, 0x4b04d447, 0xd20d85fd, 0xa50ab56b, 0x35b5a8fa, 0x42b2986c, - 0xdbbbc9d6, 0xacbcf940, 0x32d86ce3, 0x45df5c75, 0xdcd60dcf, 0xabd13d59, - 0x26d930ac, 0x51de003a, 0xc8d75180, 0xbfd06116, 0x21b4f4b5, 0x56b3c423, - 0xcfba9599, 0xb8bda50f, 0x2802b89e, 0x5f058808, 0xc60cd9b2, 0xb10be924, - 0x2f6f7c87, 0x58684c11, 0xc1611dab, 0xb6662d3d, 0x76dc4190, 0x01db7106, - 0x98d220bc, 0xefd5102a, 0x71b18589, 0x06b6b51f, 0x9fbfe4a5, 0xe8b8d433, - 0x7807c9a2, 0x0f00f934, 0x9609a88e, 0xe10e9818, 0x7f6a0dbb, 0x086d3d2d, - 0x91646c97, 0xe6635c01, 0x6b6b51f4, 0x1c6c6162, 0x856530d8, 0xf262004e, - 0x6c0695ed, 0x1b01a57b, 0x8208f4c1, 0xf50fc457, 0x65b0d9c6, 0x12b7e950, - 0x8bbeb8ea, 0xfcb9887c, 0x62dd1ddf, 0x15da2d49, 0x8cd37cf3, 0xfbd44c65, - 0x4db26158, 0x3ab551ce, 0xa3bc0074, 0xd4bb30e2, 0x4adfa541, 0x3dd895d7, - 0xa4d1c46d, 0xd3d6f4fb, 0x4369e96a, 0x346ed9fc, 0xad678846, 0xda60b8d0, - 0x44042d73, 0x33031de5, 0xaa0a4c5f, 0xdd0d7cc9, 0x5005713c, 0x270241aa, - 0xbe0b1010, 0xc90c2086, 0x5768b525, 0x206f85b3, 0xb966d409, 0xce61e49f, - 0x5edef90e, 0x29d9c998, 0xb0d09822, 0xc7d7a8b4, 0x59b33d17, 0x2eb40d81, - 0xb7bd5c3b, 0xc0ba6cad, 0xedb88320, 0x9abfb3b6, 0x03b6e20c, 0x74b1d29a, - 0xead54739, 0x9dd277af, 0x04db2615, 0x73dc1683, 0xe3630b12, 0x94643b84, - 0x0d6d6a3e, 0x7a6a5aa8, 0xe40ecf0b, 0x9309ff9d, 0x0a00ae27, 0x7d079eb1, - 0xf00f9344, 0x8708a3d2, 0x1e01f268, 0x6906c2fe, 0xf762575d, 0x806567cb, - 0x196c3671, 0x6e6b06e7, 0xfed41b76, 0x89d32be0, 0x10da7a5a, 0x67dd4acc, - 0xf9b9df6f, 0x8ebeeff9, 0x17b7be43, 0x60b08ed5, 0xd6d6a3e8, 0xa1d1937e, - 0x38d8c2c4, 0x4fdff252, 0xd1bb67f1, 0xa6bc5767, 0x3fb506dd, 0x48b2364b, - 0xd80d2bda, 0xaf0a1b4c, 0x36034af6, 0x41047a60, 0xdf60efc3, 0xa867df55, - 0x316e8eef, 0x4669be79, 0xcb61b38c, 0xbc66831a, 0x256fd2a0, 0x5268e236, - 0xcc0c7795, 0xbb0b4703, 0x220216b9, 0x5505262f, 0xc5ba3bbe, 0xb2bd0b28, - 0x2bb45a92, 0x5cb36a04, 0xc2d7ffa7, 0xb5d0cf31, 0x2cd99e8b, 0x5bdeae1d, - 0x9b64c2b0, 0xec63f226, 0x756aa39c, 0x026d930a, 0x9c0906a9, 0xeb0e363f, - 0x72076785, 0x05005713, 0x95bf4a82, 0xe2b87a14, 0x7bb12bae, 0x0cb61b38, - 0x92d28e9b, 0xe5d5be0d, 0x7cdcefb7, 0x0bdbdf21, 0x86d3d2d4, 0xf1d4e242, - 0x68ddb3f8, 0x1fda836e, 0x81be16cd, 0xf6b9265b, 0x6fb077e1, 0x18b74777, - 0x88085ae6, 0xff0f6a70, 0x66063bca, 0x11010b5c, 0x8f659eff, 0xf862ae69, - 0x616bffd3, 0x166ccf45, 0xa00ae278, 0xd70dd2ee, 0x4e048354, 0x3903b3c2, - 0xa7672661, 0xd06016f7, 0x4969474d, 0x3e6e77db, 0xaed16a4a, 0xd9d65adc, - 0x40df0b66, 0x37d83bf0, 0xa9bcae53, 0xdebb9ec5, 0x47b2cf7f, 0x30b5ffe9, - 0xbdbdf21c, 0xcabac28a, 0x53b39330, 0x24b4a3a6, 0xbad03605, 0xcdd70693, - 0x54de5729, 0x23d967bf, 0xb3667a2e, 0xc4614ab8, 0x5d681b02, 0x2a6f2b94, - 0xb40bbe37, 0xc30c8ea1, 0x5a05df1b, 0x2d02ef8d}; - -static uint32_t crc32_byte(uint32_t accum, uint8_t delta) -{ - return crc32_table[(accum ^ delta) & 0xff] ^ (accum >> 8); -} - -static int probe_prefix(struct dfu_file *file) -{ - uint8_t *prefix = file->firmware; - - if (file->size.total < LMDFU_PREFIX_LENGTH) - return 1; - if ((prefix[0] == 0x01) && (prefix[1] == 0x00)) { - file->prefix_type = LMDFU_PREFIX; - file->size.prefix = LMDFU_PREFIX_LENGTH; - file->lmdfu_address = 1024 * ((prefix[3] << 8) | prefix[2]); - } - else if (((prefix[0] & 0x3f) == 0x1a) && ((prefix[1] & 0x3f)== 0x3f)) { - file->prefix_type = LPCDFU_UNENCRYPTED_PREFIX; - file->size.prefix = LPCDFU_PREFIX_LENGTH; - } - - if (file->size.prefix + file->size.suffix > file->size.total) - return 1; - return 0; -} - -void dfu_progress_bar(const char *desc, unsigned long long curr, - unsigned long long max) -{ - static char buf[PROGRESS_BAR_WIDTH + 1]; - static unsigned long long last_progress = -1; - static time_t last_time; - time_t curr_time = time(NULL); - unsigned long long progress; - unsigned long long x; - - /* check for not known maximum */ - if (max < curr) - max = curr + 1; - /* make none out of none give zero */ - if (max == 0 && curr == 0) - max = 1; - - /* compute completion */ - progress = (PROGRESS_BAR_WIDTH * curr) / max; - if (progress > PROGRESS_BAR_WIDTH) - progress = PROGRESS_BAR_WIDTH; - if (progress == last_progress && - curr_time == last_time) - return; - last_progress = progress; - last_time = curr_time; - - for (x = 0; x != PROGRESS_BAR_WIDTH; x++) { - if (x < progress) - buf[x] = '='; - else - buf[x] = ' '; - } - buf[x] = 0; - - printf("\r%s\t[%s] %3lld%% %12lld bytes", desc, buf, - (100ULL * curr) / max, curr); - - if (progress == PROGRESS_BAR_WIDTH) - printf("\n%s done.\n", desc); -} - -void *dfu_malloc(size_t size) -{ - void *ptr = malloc(size); - if (ptr == NULL) - errx(EX_SOFTWARE, "Cannot allocate memory of size %d bytes", (int)size); - return (ptr); -} - -uint32_t dfu_file_write_crc(int f, uint32_t crc, const void *buf, int size) -{ - int x; - - /* compute CRC */ - for (x = 0; x != size; x++) - crc = crc32_byte(crc, ((uint8_t *)buf)[x]); - - /* write data */ - if (write(f, buf, size) != size) - err(EX_IOERR, "Could not write %d bytes to file %d", size, f); - - return (crc); -} - -void dfu_load_file(struct dfu_file *file, enum suffix_req check_suffix, enum prefix_req check_prefix) -{ - off_t offset; - int f; - int i; - int res; - - file->size.prefix = 0; - file->size.suffix = 0; - - /* default values, if no valid suffix is found */ - file->bcdDFU = 0; - file->idVendor = 0xffff; /* wildcard value */ - file->idProduct = 0xffff; /* wildcard value */ - file->bcdDevice = 0xffff; /* wildcard value */ - - /* default values, if no valid prefix is found */ - file->lmdfu_address = 0; - - free(file->firmware); - - if (!strcmp(file->name, "-")) { - int read_bytes; - -#ifdef WIN32 - _setmode( _fileno( stdin ), _O_BINARY ); -#endif - file->firmware = (uint8_t*) dfu_malloc(STDIN_CHUNK_SIZE); - read_bytes = fread(file->firmware, 1, STDIN_CHUNK_SIZE, stdin); - file->size.total = read_bytes; - while (read_bytes == STDIN_CHUNK_SIZE) { - file->firmware = (uint8_t*) realloc(file->firmware, file->size.total + STDIN_CHUNK_SIZE); - if (!file->firmware) - err(EX_IOERR, "Could not allocate firmware buffer"); - read_bytes = fread(file->firmware + file->size.total, 1, STDIN_CHUNK_SIZE, stdin); - file->size.total += read_bytes; - } - if (verbose) - printf("Read %i bytes from stdin\n", file->size.total); - /* Never require suffix when reading from stdin */ - check_suffix = MAYBE_SUFFIX; - } else { - f = open(file->name, O_RDONLY | O_BINARY); - if (f < 0) - err(EX_IOERR, "Could not open file %s for reading", file->name); - - offset = lseek(f, 0, SEEK_END); - - if ((int)offset < 0 || (int)offset != offset) - err(EX_IOERR, "File size is too big"); - - if (lseek(f, 0, SEEK_SET) != 0) - err(EX_IOERR, "Could not seek to beginning"); - - file->size.total = offset; - file->firmware = dfu_malloc(file->size.total); - - if (read(f, file->firmware, file->size.total) != file->size.total) { - err(EX_IOERR, "Could not read %d bytes from %s", - file->size.total, file->name); - } - close(f); - } - - /* Check for possible DFU file suffix by trying to parse one */ - { - uint32_t crc = 0xffffffff; - const uint8_t *dfusuffix; - int missing_suffix = 0; - const char *reason; - - if (file->size.total < DFU_SUFFIX_LENGTH) { - reason = "File too short for DFU suffix"; - missing_suffix = 1; - goto checked; - } - - dfusuffix = file->firmware + file->size.total - - DFU_SUFFIX_LENGTH; - - for (i = 0; i < file->size.total - 4; i++) - crc = crc32_byte(crc, file->firmware[i]); - - if (dfusuffix[10] != 'D' || - dfusuffix[9] != 'F' || - dfusuffix[8] != 'U') { - reason = "Invalid DFU suffix signature"; - missing_suffix = 1; - goto checked; - } - - file->dwCRC = (dfusuffix[15] << 24) + - (dfusuffix[14] << 16) + - (dfusuffix[13] << 8) + - dfusuffix[12]; - - if (file->dwCRC != crc) { - reason = "DFU suffix CRC does not match"; - missing_suffix = 1; - goto checked; - } - - /* At this point we believe we have a DFU suffix - so we require further checks to succeed */ - - file->bcdDFU = (dfusuffix[7] << 8) + dfusuffix[6]; - - if (verbose) - printf("DFU suffix version %x\n", file->bcdDFU); - - file->size.suffix = dfusuffix[11]; - - if (file->size.suffix < DFU_SUFFIX_LENGTH) { - errx(EX_IOERR, "Unsupported DFU suffix length %d", - file->size.suffix); - } - - if (file->size.suffix > file->size.total) { - errx(EX_IOERR, "Invalid DFU suffix length %d", - file->size.suffix); - } - - file->idVendor = (dfusuffix[5] << 8) + dfusuffix[4]; - file->idProduct = (dfusuffix[3] << 8) + dfusuffix[2]; - file->bcdDevice = (dfusuffix[1] << 8) + dfusuffix[0]; - -checked: - if (missing_suffix) { - if (check_suffix == NEEDS_SUFFIX) { - warnx("%s", reason); - errx(EX_IOERR, "Valid DFU suffix needed"); - } else if (check_suffix == MAYBE_SUFFIX) { - warnx("%s", reason); - warnx("A valid DFU suffix will be required in " - "a future dfu-util release!!!"); - } - } else { - if (check_suffix == NO_SUFFIX) { - errx(EX_SOFTWARE, "Please remove existing DFU suffix before adding a new one.\n"); - } - } - } - res = probe_prefix(file); - if ((res || file->size.prefix == 0) && check_prefix == NEEDS_PREFIX) - errx(EX_IOERR, "Valid DFU prefix needed"); - if (file->size.prefix && check_prefix == NO_PREFIX) - errx(EX_IOERR, "A prefix already exists, please delete it first"); - if (file->size.prefix && verbose) { - uint8_t *data = file->firmware; - if (file->prefix_type == LMDFU_PREFIX) - printf("Possible TI Stellaris DFU prefix with " - "the following properties\n" - "Address: 0x%08x\n" - "Payload length: %d\n", - file->lmdfu_address, - data[4] | (data[5] << 8) | - (data[6] << 16) | (data[7] << 14)); - else if (file->prefix_type == LPCDFU_UNENCRYPTED_PREFIX) - printf("Possible unencrypted NXP LPC DFU prefix with " - "the following properties\n" - "Payload length: %d kiByte\n", - data[2] >>1 | (data[3] << 7) ); - else - errx(EX_IOERR, "Unknown DFU prefix type"); - } -} - -void dfu_store_file(struct dfu_file *file, int write_suffix, int write_prefix) -{ - uint32_t crc = 0xffffffff; - int f; - - f = open(file->name, O_WRONLY | O_BINARY | O_TRUNC | O_CREAT, 0666); - if (f < 0) - err(EX_IOERR, "Could not open file %s for writing", file->name); - - /* write prefix, if any */ - if (write_prefix) { - if (file->prefix_type == LMDFU_PREFIX) { - uint8_t lmdfu_prefix[LMDFU_PREFIX_LENGTH]; - uint32_t addr = file->lmdfu_address / 1024; - - /* lmdfu_dfu_prefix payload length excludes prefix and suffix */ - uint32_t len = file->size.total - - file->size.prefix - file->size.suffix; - - lmdfu_prefix[0] = 0x01; /* STELLARIS_DFU_PROG */ - lmdfu_prefix[1] = 0x00; /* Reserved */ - lmdfu_prefix[2] = (uint8_t)(addr & 0xff); - lmdfu_prefix[3] = (uint8_t)(addr >> 8); - lmdfu_prefix[4] = (uint8_t)(len & 0xff); - lmdfu_prefix[5] = (uint8_t)(len >> 8) & 0xff; - lmdfu_prefix[6] = (uint8_t)(len >> 16) & 0xff; - lmdfu_prefix[7] = (uint8_t)(len >> 24); - - crc = dfu_file_write_crc(f, crc, lmdfu_prefix, LMDFU_PREFIX_LENGTH); - } - if (file->prefix_type == LPCDFU_UNENCRYPTED_PREFIX) { - uint8_t lpcdfu_prefix[LPCDFU_PREFIX_LENGTH] = {0}; - int i; - - /* Payload is firmware and prefix rounded to 512 bytes */ - uint32_t len = (file->size.total - file->size.suffix + 511) /512; - - lpcdfu_prefix[0] = 0x1a; /* Unencypted*/ - lpcdfu_prefix[1] = 0x3f; /* Reserved */ - lpcdfu_prefix[2] = (uint8_t)(len & 0xff); - lpcdfu_prefix[3] = (uint8_t)((len >> 8) & 0xff); - for (i = 12; i < LPCDFU_PREFIX_LENGTH; i++) - lpcdfu_prefix[i] = 0xff; - - crc = dfu_file_write_crc(f, crc, lpcdfu_prefix, LPCDFU_PREFIX_LENGTH); - } - } - /* write firmware binary */ - crc = dfu_file_write_crc(f, crc, file->firmware + file->size.prefix, - file->size.total - file->size.prefix - file->size.suffix); - - /* write suffix, if any */ - if (write_suffix) { - uint8_t dfusuffix[DFU_SUFFIX_LENGTH]; - - dfusuffix[0] = file->bcdDevice & 0xff; - dfusuffix[1] = file->bcdDevice >> 8; - dfusuffix[2] = file->idProduct & 0xff; - dfusuffix[3] = file->idProduct >> 8; - dfusuffix[4] = file->idVendor & 0xff; - dfusuffix[5] = file->idVendor >> 8; - dfusuffix[6] = file->bcdDFU & 0xff; - dfusuffix[7] = file->bcdDFU >> 8; - dfusuffix[8] = 'U'; - dfusuffix[9] = 'F'; - dfusuffix[10] = 'D'; - dfusuffix[11] = DFU_SUFFIX_LENGTH; - - crc = dfu_file_write_crc(f, crc, dfusuffix, - DFU_SUFFIX_LENGTH - 4); - - dfusuffix[12] = crc; - dfusuffix[13] = crc >> 8; - dfusuffix[14] = crc >> 16; - dfusuffix[15] = crc >> 24; - - crc = dfu_file_write_crc(f, crc, dfusuffix + 12, 4); - } - close(f); -} - -void show_suffix_and_prefix(struct dfu_file *file) -{ - if (file->size.prefix == LMDFU_PREFIX_LENGTH) { - printf("The file %s contains a TI Stellaris DFU prefix with the following properties:\n", file->name); - printf("Address:\t0x%08x\n", file->lmdfu_address); - } else if (file->size.prefix == LPCDFU_PREFIX_LENGTH) { - uint8_t * prefix = file->firmware; - printf("The file %s contains a NXP unencrypted LPC DFU prefix with the following properties:\n", file->name); - printf("Size:\t%5d kiB\n", prefix[2]>>1|prefix[3]<<7); - } else if (file->size.prefix != 0) { - printf("The file %s contains an unknown prefix\n", file->name); - } - if (file->size.suffix > 0) { - printf("The file %s contains a DFU suffix with the following properties:\n", file->name); - printf("BCD device:\t0x%04X\n", file->bcdDevice); - printf("Product ID:\t0x%04X\n",file->idProduct); - printf("Vendor ID:\t0x%04X\n", file->idVendor); - printf("BCD DFU:\t0x%04X\n", file->bcdDFU); - printf("Length:\t\t%i\n", file->size.suffix); - printf("CRC:\t\t0x%08X\n", file->dwCRC); - } -} +/* + * Load or store DFU files including suffix and prefix + * + * Copyright 2014 Tormod Volden + * Copyright 2012 Stefan Schmidt + * + * 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 +#include +#include +#include +#include +#include +#include + +#include "portable.h" +#include "dfu_file.h" + +#define DFU_SUFFIX_LENGTH 16 +#define LMDFU_PREFIX_LENGTH 8 +#define LPCDFU_PREFIX_LENGTH 16 +#define PROGRESS_BAR_WIDTH 25 +#define STDIN_CHUNK_SIZE 65536 + +static const unsigned long crc32_table[] = { + 0x00000000, 0x77073096, 0xee0e612c, 0x990951ba, 0x076dc419, 0x706af48f, + 0xe963a535, 0x9e6495a3, 0x0edb8832, 0x79dcb8a4, 0xe0d5e91e, 0x97d2d988, + 0x09b64c2b, 0x7eb17cbd, 0xe7b82d07, 0x90bf1d91, 0x1db71064, 0x6ab020f2, + 0xf3b97148, 0x84be41de, 0x1adad47d, 0x6ddde4eb, 0xf4d4b551, 0x83d385c7, + 0x136c9856, 0x646ba8c0, 0xfd62f97a, 0x8a65c9ec, 0x14015c4f, 0x63066cd9, + 0xfa0f3d63, 0x8d080df5, 0x3b6e20c8, 0x4c69105e, 0xd56041e4, 0xa2677172, + 0x3c03e4d1, 0x4b04d447, 0xd20d85fd, 0xa50ab56b, 0x35b5a8fa, 0x42b2986c, + 0xdbbbc9d6, 0xacbcf940, 0x32d86ce3, 0x45df5c75, 0xdcd60dcf, 0xabd13d59, + 0x26d930ac, 0x51de003a, 0xc8d75180, 0xbfd06116, 0x21b4f4b5, 0x56b3c423, + 0xcfba9599, 0xb8bda50f, 0x2802b89e, 0x5f058808, 0xc60cd9b2, 0xb10be924, + 0x2f6f7c87, 0x58684c11, 0xc1611dab, 0xb6662d3d, 0x76dc4190, 0x01db7106, + 0x98d220bc, 0xefd5102a, 0x71b18589, 0x06b6b51f, 0x9fbfe4a5, 0xe8b8d433, + 0x7807c9a2, 0x0f00f934, 0x9609a88e, 0xe10e9818, 0x7f6a0dbb, 0x086d3d2d, + 0x91646c97, 0xe6635c01, 0x6b6b51f4, 0x1c6c6162, 0x856530d8, 0xf262004e, + 0x6c0695ed, 0x1b01a57b, 0x8208f4c1, 0xf50fc457, 0x65b0d9c6, 0x12b7e950, + 0x8bbeb8ea, 0xfcb9887c, 0x62dd1ddf, 0x15da2d49, 0x8cd37cf3, 0xfbd44c65, + 0x4db26158, 0x3ab551ce, 0xa3bc0074, 0xd4bb30e2, 0x4adfa541, 0x3dd895d7, + 0xa4d1c46d, 0xd3d6f4fb, 0x4369e96a, 0x346ed9fc, 0xad678846, 0xda60b8d0, + 0x44042d73, 0x33031de5, 0xaa0a4c5f, 0xdd0d7cc9, 0x5005713c, 0x270241aa, + 0xbe0b1010, 0xc90c2086, 0x5768b525, 0x206f85b3, 0xb966d409, 0xce61e49f, + 0x5edef90e, 0x29d9c998, 0xb0d09822, 0xc7d7a8b4, 0x59b33d17, 0x2eb40d81, + 0xb7bd5c3b, 0xc0ba6cad, 0xedb88320, 0x9abfb3b6, 0x03b6e20c, 0x74b1d29a, + 0xead54739, 0x9dd277af, 0x04db2615, 0x73dc1683, 0xe3630b12, 0x94643b84, + 0x0d6d6a3e, 0x7a6a5aa8, 0xe40ecf0b, 0x9309ff9d, 0x0a00ae27, 0x7d079eb1, + 0xf00f9344, 0x8708a3d2, 0x1e01f268, 0x6906c2fe, 0xf762575d, 0x806567cb, + 0x196c3671, 0x6e6b06e7, 0xfed41b76, 0x89d32be0, 0x10da7a5a, 0x67dd4acc, + 0xf9b9df6f, 0x8ebeeff9, 0x17b7be43, 0x60b08ed5, 0xd6d6a3e8, 0xa1d1937e, + 0x38d8c2c4, 0x4fdff252, 0xd1bb67f1, 0xa6bc5767, 0x3fb506dd, 0x48b2364b, + 0xd80d2bda, 0xaf0a1b4c, 0x36034af6, 0x41047a60, 0xdf60efc3, 0xa867df55, + 0x316e8eef, 0x4669be79, 0xcb61b38c, 0xbc66831a, 0x256fd2a0, 0x5268e236, + 0xcc0c7795, 0xbb0b4703, 0x220216b9, 0x5505262f, 0xc5ba3bbe, 0xb2bd0b28, + 0x2bb45a92, 0x5cb36a04, 0xc2d7ffa7, 0xb5d0cf31, 0x2cd99e8b, 0x5bdeae1d, + 0x9b64c2b0, 0xec63f226, 0x756aa39c, 0x026d930a, 0x9c0906a9, 0xeb0e363f, + 0x72076785, 0x05005713, 0x95bf4a82, 0xe2b87a14, 0x7bb12bae, 0x0cb61b38, + 0x92d28e9b, 0xe5d5be0d, 0x7cdcefb7, 0x0bdbdf21, 0x86d3d2d4, 0xf1d4e242, + 0x68ddb3f8, 0x1fda836e, 0x81be16cd, 0xf6b9265b, 0x6fb077e1, 0x18b74777, + 0x88085ae6, 0xff0f6a70, 0x66063bca, 0x11010b5c, 0x8f659eff, 0xf862ae69, + 0x616bffd3, 0x166ccf45, 0xa00ae278, 0xd70dd2ee, 0x4e048354, 0x3903b3c2, + 0xa7672661, 0xd06016f7, 0x4969474d, 0x3e6e77db, 0xaed16a4a, 0xd9d65adc, + 0x40df0b66, 0x37d83bf0, 0xa9bcae53, 0xdebb9ec5, 0x47b2cf7f, 0x30b5ffe9, + 0xbdbdf21c, 0xcabac28a, 0x53b39330, 0x24b4a3a6, 0xbad03605, 0xcdd70693, + 0x54de5729, 0x23d967bf, 0xb3667a2e, 0xc4614ab8, 0x5d681b02, 0x2a6f2b94, + 0xb40bbe37, 0xc30c8ea1, 0x5a05df1b, 0x2d02ef8d}; + +static uint32_t crc32_byte(uint32_t accum, uint8_t delta) +{ + return crc32_table[(accum ^ delta) & 0xff] ^ (accum >> 8); +} + +static int probe_prefix(struct dfu_file *file) +{ + uint8_t *prefix = file->firmware; + + if (file->size.total < LMDFU_PREFIX_LENGTH) + return 1; + if ((prefix[0] == 0x01) && (prefix[1] == 0x00)) { + file->prefix_type = LMDFU_PREFIX; + file->size.prefix = LMDFU_PREFIX_LENGTH; + file->lmdfu_address = 1024 * ((prefix[3] << 8) | prefix[2]); + } + else if (((prefix[0] & 0x3f) == 0x1a) && ((prefix[1] & 0x3f)== 0x3f)) { + file->prefix_type = LPCDFU_UNENCRYPTED_PREFIX; + file->size.prefix = LPCDFU_PREFIX_LENGTH; + } + + if (file->size.prefix + file->size.suffix > file->size.total) + return 1; + return 0; +} + +void dfu_progress_bar(const char *desc, unsigned long long curr, + unsigned long long max) +{ + static char buf[PROGRESS_BAR_WIDTH + 1]; + static unsigned long long last_progress = -1; + static time_t last_time; + time_t curr_time = time(NULL); + unsigned long long progress; + unsigned long long x; + + /* check for not known maximum */ + if (max < curr) + max = curr + 1; + /* make none out of none give zero */ + if (max == 0 && curr == 0) + max = 1; + + /* compute completion */ + progress = (PROGRESS_BAR_WIDTH * curr) / max; + if (progress > PROGRESS_BAR_WIDTH) + progress = PROGRESS_BAR_WIDTH; + if (progress == last_progress && + curr_time == last_time) + return; + last_progress = progress; + last_time = curr_time; + + for (x = 0; x != PROGRESS_BAR_WIDTH; x++) { + if (x < progress) + buf[x] = '='; + else + buf[x] = ' '; + } + buf[x] = 0; + + printf("\r%s\t[%s] %3lld%% %12lld bytes", desc, buf, + (100ULL * curr) / max, curr); + + if (progress == PROGRESS_BAR_WIDTH) + printf("\n%s done.\n", desc); +} + +void *dfu_malloc(size_t size) +{ + void *ptr = malloc(size); + if (ptr == NULL) + errx(EX_SOFTWARE, "Cannot allocate memory of size %d bytes", (int)size); + return (ptr); +} + +uint32_t dfu_file_write_crc(int f, uint32_t crc, const void *buf, int size) +{ + int x; + + /* compute CRC */ + for (x = 0; x != size; x++) + crc = crc32_byte(crc, ((uint8_t *)buf)[x]); + + /* write data */ + if (write(f, buf, size) != size) + err(EX_IOERR, "Could not write %d bytes to file %d", size, f); + + return (crc); +} + +void dfu_load_file(struct dfu_file *file, enum suffix_req check_suffix, enum prefix_req check_prefix) +{ + off_t offset; + int f; + int i; + int res; + + file->size.prefix = 0; + file->size.suffix = 0; + + /* default values, if no valid suffix is found */ + file->bcdDFU = 0; + file->idVendor = 0xffff; /* wildcard value */ + file->idProduct = 0xffff; /* wildcard value */ + file->bcdDevice = 0xffff; /* wildcard value */ + + /* default values, if no valid prefix is found */ + file->lmdfu_address = 0; + + free(file->firmware); + + if (!strcmp(file->name, "-")) { + int read_bytes; + +#ifdef WIN32 + _setmode( _fileno( stdin ), _O_BINARY ); +#endif + file->firmware = (uint8_t*) dfu_malloc(STDIN_CHUNK_SIZE); + read_bytes = fread(file->firmware, 1, STDIN_CHUNK_SIZE, stdin); + file->size.total = read_bytes; + while (read_bytes == STDIN_CHUNK_SIZE) { + file->firmware = (uint8_t*) realloc(file->firmware, file->size.total + STDIN_CHUNK_SIZE); + if (!file->firmware) + err(EX_IOERR, "Could not allocate firmware buffer"); + read_bytes = fread(file->firmware + file->size.total, 1, STDIN_CHUNK_SIZE, stdin); + file->size.total += read_bytes; + } + if (verbose) + printf("Read %i bytes from stdin\n", file->size.total); + /* Never require suffix when reading from stdin */ + check_suffix = MAYBE_SUFFIX; + } else { + f = open(file->name, O_RDONLY | O_BINARY); + if (f < 0) + err(EX_IOERR, "Could not open file %s for reading", file->name); + + offset = lseek(f, 0, SEEK_END); + + if ((int)offset < 0 || (int)offset != offset) + err(EX_IOERR, "File size is too big"); + + if (lseek(f, 0, SEEK_SET) != 0) + err(EX_IOERR, "Could not seek to beginning"); + + file->size.total = offset; + file->firmware = dfu_malloc(file->size.total); + + if (read(f, file->firmware, file->size.total) != file->size.total) { + err(EX_IOERR, "Could not read %d bytes from %s", + file->size.total, file->name); + } + close(f); + } + + /* Check for possible DFU file suffix by trying to parse one */ + { + uint32_t crc = 0xffffffff; + const uint8_t *dfusuffix; + int missing_suffix = 0; + const char *reason; + + if (file->size.total < DFU_SUFFIX_LENGTH) { + reason = "File too short for DFU suffix"; + missing_suffix = 1; + goto checked; + } + + dfusuffix = file->firmware + file->size.total - + DFU_SUFFIX_LENGTH; + + for (i = 0; i < file->size.total - 4; i++) + crc = crc32_byte(crc, file->firmware[i]); + + if (dfusuffix[10] != 'D' || + dfusuffix[9] != 'F' || + dfusuffix[8] != 'U') { + reason = "Invalid DFU suffix signature"; + missing_suffix = 1; + goto checked; + } + + file->dwCRC = (dfusuffix[15] << 24) + + (dfusuffix[14] << 16) + + (dfusuffix[13] << 8) + + dfusuffix[12]; + + if (file->dwCRC != crc) { + reason = "DFU suffix CRC does not match"; + missing_suffix = 1; + goto checked; + } + + /* At this point we believe we have a DFU suffix + so we require further checks to succeed */ + + file->bcdDFU = (dfusuffix[7] << 8) + dfusuffix[6]; + + if (verbose) + printf("DFU suffix version %x\n", file->bcdDFU); + + file->size.suffix = dfusuffix[11]; + + if (file->size.suffix < DFU_SUFFIX_LENGTH) { + errx(EX_IOERR, "Unsupported DFU suffix length %d", + file->size.suffix); + } + + if (file->size.suffix > file->size.total) { + errx(EX_IOERR, "Invalid DFU suffix length %d", + file->size.suffix); + } + + file->idVendor = (dfusuffix[5] << 8) + dfusuffix[4]; + file->idProduct = (dfusuffix[3] << 8) + dfusuffix[2]; + file->bcdDevice = (dfusuffix[1] << 8) + dfusuffix[0]; + +checked: + if (missing_suffix) { + if (check_suffix == NEEDS_SUFFIX) { + warnx("%s", reason); + errx(EX_IOERR, "Valid DFU suffix needed"); + } else if (check_suffix == MAYBE_SUFFIX) { + warnx("%s", reason); + warnx("A valid DFU suffix will be required in " + "a future dfu-util release!!!"); + } + } else { + if (check_suffix == NO_SUFFIX) { + errx(EX_SOFTWARE, "Please remove existing DFU suffix before adding a new one.\n"); + } + } + } + res = probe_prefix(file); + if ((res || file->size.prefix == 0) && check_prefix == NEEDS_PREFIX) + errx(EX_IOERR, "Valid DFU prefix needed"); + if (file->size.prefix && check_prefix == NO_PREFIX) + errx(EX_IOERR, "A prefix already exists, please delete it first"); + if (file->size.prefix && verbose) { + uint8_t *data = file->firmware; + if (file->prefix_type == LMDFU_PREFIX) + printf("Possible TI Stellaris DFU prefix with " + "the following properties\n" + "Address: 0x%08x\n" + "Payload length: %d\n", + file->lmdfu_address, + data[4] | (data[5] << 8) | + (data[6] << 16) | (data[7] << 14)); + else if (file->prefix_type == LPCDFU_UNENCRYPTED_PREFIX) + printf("Possible unencrypted NXP LPC DFU prefix with " + "the following properties\n" + "Payload length: %d kiByte\n", + data[2] >>1 | (data[3] << 7) ); + else + errx(EX_IOERR, "Unknown DFU prefix type"); + } +} + +void dfu_store_file(struct dfu_file *file, int write_suffix, int write_prefix) +{ + uint32_t crc = 0xffffffff; + int f; + + f = open(file->name, O_WRONLY | O_BINARY | O_TRUNC | O_CREAT, 0666); + if (f < 0) + err(EX_IOERR, "Could not open file %s for writing", file->name); + + /* write prefix, if any */ + if (write_prefix) { + if (file->prefix_type == LMDFU_PREFIX) { + uint8_t lmdfu_prefix[LMDFU_PREFIX_LENGTH]; + uint32_t addr = file->lmdfu_address / 1024; + + /* lmdfu_dfu_prefix payload length excludes prefix and suffix */ + uint32_t len = file->size.total - + file->size.prefix - file->size.suffix; + + lmdfu_prefix[0] = 0x01; /* STELLARIS_DFU_PROG */ + lmdfu_prefix[1] = 0x00; /* Reserved */ + lmdfu_prefix[2] = (uint8_t)(addr & 0xff); + lmdfu_prefix[3] = (uint8_t)(addr >> 8); + lmdfu_prefix[4] = (uint8_t)(len & 0xff); + lmdfu_prefix[5] = (uint8_t)(len >> 8) & 0xff; + lmdfu_prefix[6] = (uint8_t)(len >> 16) & 0xff; + lmdfu_prefix[7] = (uint8_t)(len >> 24); + + crc = dfu_file_write_crc(f, crc, lmdfu_prefix, LMDFU_PREFIX_LENGTH); + } + if (file->prefix_type == LPCDFU_UNENCRYPTED_PREFIX) { + uint8_t lpcdfu_prefix[LPCDFU_PREFIX_LENGTH] = {0}; + int i; + + /* Payload is firmware and prefix rounded to 512 bytes */ + uint32_t len = (file->size.total - file->size.suffix + 511) /512; + + lpcdfu_prefix[0] = 0x1a; /* Unencypted*/ + lpcdfu_prefix[1] = 0x3f; /* Reserved */ + lpcdfu_prefix[2] = (uint8_t)(len & 0xff); + lpcdfu_prefix[3] = (uint8_t)((len >> 8) & 0xff); + for (i = 12; i < LPCDFU_PREFIX_LENGTH; i++) + lpcdfu_prefix[i] = 0xff; + + crc = dfu_file_write_crc(f, crc, lpcdfu_prefix, LPCDFU_PREFIX_LENGTH); + } + } + /* write firmware binary */ + crc = dfu_file_write_crc(f, crc, file->firmware + file->size.prefix, + file->size.total - file->size.prefix - file->size.suffix); + + /* write suffix, if any */ + if (write_suffix) { + uint8_t dfusuffix[DFU_SUFFIX_LENGTH]; + + dfusuffix[0] = file->bcdDevice & 0xff; + dfusuffix[1] = file->bcdDevice >> 8; + dfusuffix[2] = file->idProduct & 0xff; + dfusuffix[3] = file->idProduct >> 8; + dfusuffix[4] = file->idVendor & 0xff; + dfusuffix[5] = file->idVendor >> 8; + dfusuffix[6] = file->bcdDFU & 0xff; + dfusuffix[7] = file->bcdDFU >> 8; + dfusuffix[8] = 'U'; + dfusuffix[9] = 'F'; + dfusuffix[10] = 'D'; + dfusuffix[11] = DFU_SUFFIX_LENGTH; + + crc = dfu_file_write_crc(f, crc, dfusuffix, + DFU_SUFFIX_LENGTH - 4); + + dfusuffix[12] = crc; + dfusuffix[13] = crc >> 8; + dfusuffix[14] = crc >> 16; + dfusuffix[15] = crc >> 24; + + crc = dfu_file_write_crc(f, crc, dfusuffix + 12, 4); + } + close(f); +} + +void show_suffix_and_prefix(struct dfu_file *file) +{ + if (file->size.prefix == LMDFU_PREFIX_LENGTH) { + printf("The file %s contains a TI Stellaris DFU prefix with the following properties:\n", file->name); + printf("Address:\t0x%08x\n", file->lmdfu_address); + } else if (file->size.prefix == LPCDFU_PREFIX_LENGTH) { + uint8_t * prefix = file->firmware; + printf("The file %s contains a NXP unencrypted LPC DFU prefix with the following properties:\n", file->name); + printf("Size:\t%5d kiB\n", prefix[2]>>1|prefix[3]<<7); + } else if (file->size.prefix != 0) { + printf("The file %s contains an unknown prefix\n", file->name); + } + if (file->size.suffix > 0) { + printf("The file %s contains a DFU suffix with the following properties:\n", file->name); + printf("BCD device:\t0x%04X\n", file->bcdDevice); + printf("Product ID:\t0x%04X\n",file->idProduct); + printf("Vendor ID:\t0x%04X\n", file->idVendor); + printf("BCD DFU:\t0x%04X\n", file->bcdDFU); + printf("Length:\t\t%i\n", file->size.suffix); + printf("CRC:\t\t0x%08X\n", file->dwCRC); + } +} diff --git a/tools/src/dfu-util/src/dfu_file.h b/tools/linux/src/dfu-util/src/dfu_file.h old mode 100755 new mode 100644 similarity index 95% rename from tools/src/dfu-util/src/dfu_file.h rename to tools/linux/src/dfu-util/src/dfu_file.h index 31c90b8..abebd44 --- a/tools/src/dfu-util/src/dfu_file.h +++ b/tools/linux/src/dfu-util/src/dfu_file.h @@ -1,60 +1,60 @@ - -#ifndef DFU_FILE_H -#define DFU_FILE_H - -#include - -struct dfu_file { - /* File name */ - const char *name; - /* Pointer to file loaded into memory */ - uint8_t *firmware; - /* Different sizes */ - struct { - int total; - int prefix; - int suffix; - } size; - /* From prefix fields */ - uint32_t lmdfu_address; - /* From prefix fields */ - uint32_t prefix_type; - - /* From DFU suffix fields */ - uint32_t dwCRC; - uint16_t bcdDFU; - uint16_t idVendor; - uint16_t idProduct; - uint16_t bcdDevice; -}; - -enum suffix_req { - NO_SUFFIX, - NEEDS_SUFFIX, - MAYBE_SUFFIX -}; - -enum prefix_req { - NO_PREFIX, - NEEDS_PREFIX, - MAYBE_PREFIX -}; - -enum prefix_type { - ZERO_PREFIX, - LMDFU_PREFIX, - LPCDFU_UNENCRYPTED_PREFIX -}; - -extern int verbose; - -void dfu_load_file(struct dfu_file *file, enum suffix_req check_suffix, enum prefix_req check_prefix); -void dfu_store_file(struct dfu_file *file, int write_suffix, int write_prefix); - -void dfu_progress_bar(const char *desc, unsigned long long curr, - unsigned long long max); -void *dfu_malloc(size_t size); -uint32_t dfu_file_write_crc(int f, uint32_t crc, const void *buf, int size); -void show_suffix_and_prefix(struct dfu_file *file); - -#endif /* DFU_FILE_H */ + +#ifndef DFU_FILE_H +#define DFU_FILE_H + +#include + +struct dfu_file { + /* File name */ + const char *name; + /* Pointer to file loaded into memory */ + uint8_t *firmware; + /* Different sizes */ + struct { + int total; + int prefix; + int suffix; + } size; + /* From prefix fields */ + uint32_t lmdfu_address; + /* From prefix fields */ + uint32_t prefix_type; + + /* From DFU suffix fields */ + uint32_t dwCRC; + uint16_t bcdDFU; + uint16_t idVendor; + uint16_t idProduct; + uint16_t bcdDevice; +}; + +enum suffix_req { + NO_SUFFIX, + NEEDS_SUFFIX, + MAYBE_SUFFIX +}; + +enum prefix_req { + NO_PREFIX, + NEEDS_PREFIX, + MAYBE_PREFIX +}; + +enum prefix_type { + ZERO_PREFIX, + LMDFU_PREFIX, + LPCDFU_UNENCRYPTED_PREFIX +}; + +extern int verbose; + +void dfu_load_file(struct dfu_file *file, enum suffix_req check_suffix, enum prefix_req check_prefix); +void dfu_store_file(struct dfu_file *file, int write_suffix, int write_prefix); + +void dfu_progress_bar(const char *desc, unsigned long long curr, + unsigned long long max); +void *dfu_malloc(size_t size); +uint32_t dfu_file_write_crc(int f, uint32_t crc, const void *buf, int size); +void show_suffix_and_prefix(struct dfu_file *file); + +#endif /* DFU_FILE_H */ diff --git a/tools/src/dfu-util/src/dfu_load.c b/tools/linux/src/dfu-util/src/dfu_load.c old mode 100755 new mode 100644 similarity index 96% rename from tools/src/dfu-util/src/dfu_load.c rename to tools/linux/src/dfu-util/src/dfu_load.c index d4ff51c..64f7009 --- a/tools/src/dfu-util/src/dfu_load.c +++ b/tools/linux/src/dfu-util/src/dfu_load.c @@ -1,196 +1,196 @@ -/* - * DFU transfer routines - * - * This is supposed to be a general DFU implementation, as specified in the - * USB DFU 1.0 and 1.1 specification. - * - * The code was originally intended to interface with a USB device running the - * "sam7dfu" firmware (see http://www.openpcd.org/) on an AT91SAM7 processor. - * - * Copyright 2007-2008 Harald Welte - * Copyright 2013 Hans Petter Selasky - * Copyright 2014 Tormod Volden - * - * 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 -#include -#include -#include - -#include - -#include "portable.h" -#include "dfu.h" -#include "usb_dfu.h" -#include "dfu_file.h" -#include "dfu_load.h" -#include "quirks.h" - -int dfuload_do_upload(struct dfu_if *dif, int xfer_size, - int expected_size, int fd) -{ - int total_bytes = 0; - unsigned short transaction = 0; - unsigned char *buf; - int ret; - - buf = dfu_malloc(xfer_size); - - printf("Copying data from DFU device to PC\n"); - dfu_progress_bar("Upload", 0, 1); - - while (1) { - int rc; - rc = dfu_upload(dif->dev_handle, dif->interface, - xfer_size, transaction++, buf); - if (rc < 0) { - warnx("Error during upload"); - ret = rc; - goto out_free; - } - - dfu_file_write_crc(fd, 0, buf, rc); - total_bytes += rc; - - if (total_bytes < 0) - errx(EX_SOFTWARE, "Received too many bytes (wraparound)"); - - if (rc < xfer_size) { - /* last block, return */ - ret = total_bytes; - break; - } - dfu_progress_bar("Upload", total_bytes, expected_size); - } - ret = 0; - -out_free: - dfu_progress_bar("Upload", total_bytes, total_bytes); - if (total_bytes == 0) - printf("\nFailed.\n"); - free(buf); - if (verbose) - printf("Received a total of %i bytes\n", total_bytes); - if (expected_size != 0 && total_bytes != expected_size) - errx(EX_SOFTWARE, "Unexpected number of bytes uploaded from device"); - return ret; -} - -int dfuload_do_dnload(struct dfu_if *dif, int xfer_size, struct dfu_file *file) -{ - int bytes_sent; - int expected_size; - unsigned char *buf; - unsigned short transaction = 0; - struct dfu_status dst; - int ret; - - printf("Copying data from PC to DFU device\n"); - - buf = file->firmware; - expected_size = file->size.total - file->size.suffix; - bytes_sent = 0; - - dfu_progress_bar("Download", 0, 1); - while (bytes_sent < expected_size) { - int bytes_left; - int chunk_size; - - bytes_left = expected_size - bytes_sent; - if (bytes_left < xfer_size) - chunk_size = bytes_left; - else - chunk_size = xfer_size; - - ret = dfu_download(dif->dev_handle, dif->interface, - chunk_size, transaction++, chunk_size ? buf : NULL); - if (ret < 0) { - warnx("Error during download"); - goto out; - } - bytes_sent += chunk_size; - buf += chunk_size; - - do { - ret = dfu_get_status(dif, &dst); - if (ret < 0) { - errx(EX_IOERR, "Error during download get_status"); - goto out; - } - - if (dst.bState == DFU_STATE_dfuDNLOAD_IDLE || - dst.bState == DFU_STATE_dfuERROR) - break; - - /* Wait while device executes flashing */ - milli_sleep(dst.bwPollTimeout); - - } while (1); - 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; - } - dfu_progress_bar("Download", bytes_sent, bytes_sent + bytes_left); - } - - /* send one zero sized download request to signalize end */ - ret = dfu_download(dif->dev_handle, dif->interface, - 0, transaction, NULL); - if (ret < 0) { - errx(EX_IOERR, "Error sending completion packet"); - goto out; - } - - dfu_progress_bar("Download", bytes_sent, bytes_sent); - - if (verbose) - printf("Sent a total of %i bytes\n", bytes_sent); - -get_status: - /* Transition to MANIFEST_SYNC state */ - ret = dfu_get_status(dif, &dst); - if (ret < 0) { - warnx("unable to read DFU status after completion"); - goto out; - } - printf("state(%u) = %s, status(%u) = %s\n", dst.bState, - dfu_state_to_string(dst.bState), dst.bStatus, - dfu_status_to_string(dst.bStatus)); - - milli_sleep(dst.bwPollTimeout); - - /* 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 */ - milli_sleep(1000); - goto get_status; - break; - case DFU_STATE_dfuIDLE: - break; - } - printf("Done!\n"); - -out: - return bytes_sent; -} +/* + * DFU transfer routines + * + * This is supposed to be a general DFU implementation, as specified in the + * USB DFU 1.0 and 1.1 specification. + * + * The code was originally intended to interface with a USB device running the + * "sam7dfu" firmware (see http://www.openpcd.org/) on an AT91SAM7 processor. + * + * Copyright 2007-2008 Harald Welte + * Copyright 2013 Hans Petter Selasky + * Copyright 2014 Tormod Volden + * + * 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 +#include +#include +#include + +#include + +#include "portable.h" +#include "dfu.h" +#include "usb_dfu.h" +#include "dfu_file.h" +#include "dfu_load.h" +#include "quirks.h" + +int dfuload_do_upload(struct dfu_if *dif, int xfer_size, + int expected_size, int fd) +{ + int total_bytes = 0; + unsigned short transaction = 0; + unsigned char *buf; + int ret; + + buf = dfu_malloc(xfer_size); + + printf("Copying data from DFU device to PC\n"); + dfu_progress_bar("Upload", 0, 1); + + while (1) { + int rc; + rc = dfu_upload(dif->dev_handle, dif->interface, + xfer_size, transaction++, buf); + if (rc < 0) { + warnx("Error during upload"); + ret = rc; + goto out_free; + } + + dfu_file_write_crc(fd, 0, buf, rc); + total_bytes += rc; + + if (total_bytes < 0) + errx(EX_SOFTWARE, "Received too many bytes (wraparound)"); + + if (rc < xfer_size) { + /* last block, return */ + ret = total_bytes; + break; + } + dfu_progress_bar("Upload", total_bytes, expected_size); + } + ret = 0; + +out_free: + dfu_progress_bar("Upload", total_bytes, total_bytes); + if (total_bytes == 0) + printf("\nFailed.\n"); + free(buf); + if (verbose) + printf("Received a total of %i bytes\n", total_bytes); + if (expected_size != 0 && total_bytes != expected_size) + errx(EX_SOFTWARE, "Unexpected number of bytes uploaded from device"); + return ret; +} + +int dfuload_do_dnload(struct dfu_if *dif, int xfer_size, struct dfu_file *file) +{ + int bytes_sent; + int expected_size; + unsigned char *buf; + unsigned short transaction = 0; + struct dfu_status dst; + int ret; + + printf("Copying data from PC to DFU device\n"); + + buf = file->firmware; + expected_size = file->size.total - file->size.suffix; + bytes_sent = 0; + + dfu_progress_bar("Download", 0, 1); + while (bytes_sent < expected_size) { + int bytes_left; + int chunk_size; + + bytes_left = expected_size - bytes_sent; + if (bytes_left < xfer_size) + chunk_size = bytes_left; + else + chunk_size = xfer_size; + + ret = dfu_download(dif->dev_handle, dif->interface, + chunk_size, transaction++, chunk_size ? buf : NULL); + if (ret < 0) { + warnx("Error during download"); + goto out; + } + bytes_sent += chunk_size; + buf += chunk_size; + + do { + ret = dfu_get_status(dif, &dst); + if (ret < 0) { + errx(EX_IOERR, "Error during download get_status"); + goto out; + } + + if (dst.bState == DFU_STATE_dfuDNLOAD_IDLE || + dst.bState == DFU_STATE_dfuERROR) + break; + + /* Wait while device executes flashing */ + milli_sleep(dst.bwPollTimeout); + + } while (1); + 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; + } + dfu_progress_bar("Download", bytes_sent, bytes_sent + bytes_left); + } + + /* send one zero sized download request to signalize end */ + ret = dfu_download(dif->dev_handle, dif->interface, + 0, transaction, NULL); + if (ret < 0) { + errx(EX_IOERR, "Error sending completion packet"); + goto out; + } + + dfu_progress_bar("Download", bytes_sent, bytes_sent); + + if (verbose) + printf("Sent a total of %i bytes\n", bytes_sent); + +get_status: + /* Transition to MANIFEST_SYNC state */ + ret = dfu_get_status(dif, &dst); + if (ret < 0) { + warnx("unable to read DFU status after completion"); + goto out; + } + printf("state(%u) = %s, status(%u) = %s\n", dst.bState, + dfu_state_to_string(dst.bState), dst.bStatus, + dfu_status_to_string(dst.bStatus)); + + milli_sleep(dst.bwPollTimeout); + + /* 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 */ + milli_sleep(1000); + goto get_status; + break; + case DFU_STATE_dfuIDLE: + break; + } + printf("Done!\n"); + +out: + return bytes_sent; +} diff --git a/tools/src/dfu-util/src/dfu_load.h b/tools/linux/src/dfu-util/src/dfu_load.h old mode 100755 new mode 100644 similarity index 97% rename from tools/src/dfu-util/src/dfu_load.h rename to tools/linux/src/dfu-util/src/dfu_load.h index fe363aa..be23e9b --- a/tools/src/dfu-util/src/dfu_load.h +++ b/tools/linux/src/dfu-util/src/dfu_load.h @@ -1,7 +1,7 @@ -#ifndef DFU_LOAD_H -#define DFU_LOAD_H - -int dfuload_do_upload(struct dfu_if *dif, int xfer_size, int expected_size, int fd); -int dfuload_do_dnload(struct dfu_if *dif, int xfer_size, struct dfu_file *file); - -#endif /* DFU_LOAD_H */ +#ifndef DFU_LOAD_H +#define DFU_LOAD_H + +int dfuload_do_upload(struct dfu_if *dif, int xfer_size, int expected_size, int fd); +int dfuload_do_dnload(struct dfu_if *dif, int xfer_size, struct dfu_file *file); + +#endif /* DFU_LOAD_H */ diff --git a/tools/src/dfu-util/src/dfu_util.c b/tools/linux/src/dfu-util/src/dfu_util.c old mode 100755 new mode 100644 similarity index 96% rename from tools/src/dfu-util/src/dfu_util.c rename to tools/linux/src/dfu-util/src/dfu_util.c index d9ad2d0..b94c7cc --- a/tools/src/dfu-util/src/dfu_util.c +++ b/tools/linux/src/dfu-util/src/dfu_util.c @@ -1,346 +1,346 @@ -/* - * Functions for detecting DFU USB entities - * - * Written by Harald Welte - * Copyright 2007-2008 by OpenMoko, Inc. - * Copyright 2013 Hans Petter Selasky - * - * 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 -#include -#include -#include -#include - -#include "portable.h" -#include "dfu.h" -#include "usb_dfu.h" -#include "dfu_file.h" -#include "dfu_load.h" -#include "dfu_util.h" -#include "dfuse.h" -#include "quirks.h" - -#ifdef HAVE_USBPATH_H -#include -#endif - -/* - * Look for a descriptor in a concatenated descriptor list. Will - * return upon the first match of the given descriptor type. Returns length of - * found descriptor, limited to res_size - */ -static int find_descriptor(const uint8_t *desc_list, int list_len, - uint8_t desc_type, void *res_buf, int res_size) -{ - int p = 0; - - if (list_len < 2) - return (-1); - - while (p + 1 < list_len) { - int desclen; - - desclen = (int) desc_list[p]; - if (desclen == 0) { - warnx("Invalid descriptor list"); - return -1; - } - if (desc_list[p + 1] == desc_type) { - if (desclen > res_size) - desclen = res_size; - if (p + desclen > list_len) - desclen = list_len - p; - memcpy(res_buf, &desc_list[p], desclen); - return desclen; - } - p += (int) desc_list[p]; - } - return -1; -} - -static void probe_configuration(libusb_device *dev, struct libusb_device_descriptor *desc) -{ - struct usb_dfu_func_descriptor func_dfu; - libusb_device_handle *devh; - struct dfu_if *pdfu; - struct libusb_config_descriptor *cfg; - const struct libusb_interface_descriptor *intf; - const struct libusb_interface *uif; - char alt_name[MAX_DESC_STR_LEN + 1]; - char serial_name[MAX_DESC_STR_LEN + 1]; - int cfg_idx; - int intf_idx; - int alt_idx; - int ret; - int has_dfu; - - for (cfg_idx = 0; cfg_idx != desc->bNumConfigurations; cfg_idx++) { - memset(&func_dfu, 0, sizeof(func_dfu)); - has_dfu = 0; - - ret = libusb_get_config_descriptor(dev, cfg_idx, &cfg); - if (ret != 0) - return; - if (match_config_index > -1 && match_config_index != cfg->bConfigurationValue) { - libusb_free_config_descriptor(cfg); - continue; - } - - /* - * In some cases, noticably FreeBSD if uid != 0, - * the configuration descriptors are empty - */ - if (!cfg) - return; - - ret = find_descriptor(cfg->extra, cfg->extra_length, - USB_DT_DFU, &func_dfu, sizeof(func_dfu)); - if (ret > -1) - goto found_dfu; - - for (intf_idx = 0; intf_idx < cfg->bNumInterfaces; - intf_idx++) { - uif = &cfg->interface[intf_idx]; - if (!uif) - break; - - for (alt_idx = 0; alt_idx < cfg->interface[intf_idx].num_altsetting; - alt_idx++) { - intf = &uif->altsetting[alt_idx]; - - ret = find_descriptor(intf->extra, intf->extra_length, USB_DT_DFU, - &func_dfu, sizeof(func_dfu)); - if (ret > -1) - goto found_dfu; - - if (intf->bInterfaceClass != 0xfe || - intf->bInterfaceSubClass != 1) - continue; - - has_dfu = 1; - } - } - if (has_dfu) { - /* - * Finally try to retrieve it requesting the - * device directly This is not supported on - * all devices for non-standard types - */ - if (libusb_open(dev, &devh) == 0) { - ret = libusb_get_descriptor(devh, USB_DT_DFU, 0, - (void *)&func_dfu, sizeof(func_dfu)); - libusb_close(devh); - if (ret > -1) - goto found_dfu; - } - warnx("Device has DFU interface, " - "but has no DFU functional descriptor"); - - /* fake version 1.0 */ - func_dfu.bLength = 7; - func_dfu.bcdDFUVersion = libusb_cpu_to_le16(0x0100); - goto found_dfu; - } - libusb_free_config_descriptor(cfg); - continue; - -found_dfu: - if (func_dfu.bLength == 7) { - printf("Deducing device DFU version from functional descriptor " - "length\n"); - func_dfu.bcdDFUVersion = libusb_cpu_to_le16(0x0100); - } else if (func_dfu.bLength < 9) { - printf("Error obtaining DFU functional descriptor\n"); - printf("Please report this as a bug!\n"); - printf("Warning: Assuming DFU version 1.0\n"); - func_dfu.bcdDFUVersion = libusb_cpu_to_le16(0x0100); - printf("Warning: Transfer size can not be detected\n"); - func_dfu.wTransferSize = 0; - } - - for (intf_idx = 0; intf_idx < cfg->bNumInterfaces; - intf_idx++) { - if (match_iface_index > -1 && match_iface_index != intf_idx) - continue; - - uif = &cfg->interface[intf_idx]; - if (!uif) - break; - - for (alt_idx = 0; - alt_idx < uif->num_altsetting; alt_idx++) { - int dfu_mode; - - intf = &uif->altsetting[alt_idx]; - - if (intf->bInterfaceClass != 0xfe || - intf->bInterfaceSubClass != 1) - continue; - - dfu_mode = (intf->bInterfaceProtocol == 2); - /* e.g. DSO Nano has bInterfaceProtocol 0 instead of 2 */ - if (func_dfu.bcdDFUVersion == 0x011a && intf->bInterfaceProtocol == 0) - dfu_mode = 1; - - if (dfu_mode && - match_iface_alt_index > -1 && match_iface_alt_index != alt_idx) - continue; - - if (dfu_mode) { - if ((match_vendor_dfu >= 0 && match_vendor_dfu != desc->idVendor) || - (match_product_dfu >= 0 && match_product_dfu != desc->idProduct)) { - continue; - } - } else { - if ((match_vendor >= 0 && match_vendor != desc->idVendor) || - (match_product >= 0 && match_product != desc->idProduct)) { - continue; - } - } - - if (libusb_open(dev, &devh)) { - warnx("Cannot open DFU device %04x:%04x", desc->idVendor, desc->idProduct); - break; - } - if (intf->iInterface != 0) - ret = libusb_get_string_descriptor_ascii(devh, - intf->iInterface, (void *)alt_name, MAX_DESC_STR_LEN); - else - ret = -1; - if (ret < 1) - strcpy(alt_name, "UNKNOWN"); - if (desc->iSerialNumber != 0) - ret = libusb_get_string_descriptor_ascii(devh, - desc->iSerialNumber, (void *)serial_name, MAX_DESC_STR_LEN); - else - ret = -1; - if (ret < 1) - strcpy(serial_name, "UNKNOWN"); - libusb_close(devh); - - if (dfu_mode && - match_iface_alt_name != NULL && strcmp(alt_name, match_iface_alt_name)) - continue; - - if (dfu_mode) { - if (match_serial_dfu != NULL && strcmp(match_serial_dfu, serial_name)) - continue; - } else { - if (match_serial != NULL && strcmp(match_serial, serial_name)) - continue; - } - - pdfu = dfu_malloc(sizeof(*pdfu)); - - memset(pdfu, 0, sizeof(*pdfu)); - - pdfu->func_dfu = func_dfu; - pdfu->dev = libusb_ref_device(dev); - pdfu->quirks = get_quirks(desc->idVendor, - desc->idProduct, desc->bcdDevice); - pdfu->vendor = desc->idVendor; - pdfu->product = desc->idProduct; - pdfu->bcdDevice = desc->bcdDevice; - pdfu->configuration = cfg->bConfigurationValue; - pdfu->interface = intf->bInterfaceNumber; - pdfu->altsetting = intf->bAlternateSetting; - pdfu->devnum = libusb_get_device_address(dev); - pdfu->busnum = libusb_get_bus_number(dev); - pdfu->alt_name = strdup(alt_name); - if (pdfu->alt_name == NULL) - errx(EX_SOFTWARE, "Out of memory"); - pdfu->serial_name = strdup(serial_name); - if (pdfu->serial_name == NULL) - errx(EX_SOFTWARE, "Out of memory"); - if (dfu_mode) - pdfu->flags |= DFU_IFF_DFU; - if (pdfu->quirks & QUIRK_FORCE_DFU11) { - pdfu->func_dfu.bcdDFUVersion = - libusb_cpu_to_le16(0x0110); - } - pdfu->bMaxPacketSize0 = desc->bMaxPacketSize0; - - /* queue into list */ - pdfu->next = dfu_root; - dfu_root = pdfu; - } - } - libusb_free_config_descriptor(cfg); - } -} - -void probe_devices(libusb_context *ctx) -{ - libusb_device **list; - ssize_t num_devs; - ssize_t i; - - num_devs = libusb_get_device_list(ctx, &list); - for (i = 0; i < num_devs; ++i) { - struct libusb_device_descriptor desc; - struct libusb_device *dev = list[i]; - - if (match_bus > -1 && match_bus != libusb_get_bus_number(dev)) - continue; - if (match_device > -1 && match_device != libusb_get_device_address(dev)) - continue; - if (libusb_get_device_descriptor(dev, &desc)) - continue; - probe_configuration(dev, &desc); - } - libusb_free_device_list(list, 0); -} - -void disconnect_devices(void) -{ - struct dfu_if *pdfu; - struct dfu_if *prev = NULL; - - for (pdfu = dfu_root; pdfu != NULL; pdfu = pdfu->next) { - free(prev); - libusb_unref_device(pdfu->dev); - free(pdfu->alt_name); - free(pdfu->serial_name); - prev = pdfu; - } - free(prev); - dfu_root = NULL; -} - -void print_dfu_if(struct dfu_if *dfu_if) -{ - printf("Found %s: [%04x:%04x] ver=%04x, devnum=%u, cfg=%u, intf=%u, " - "alt=%u, name=\"%s\", serial=\"%s\"\n", - dfu_if->flags & DFU_IFF_DFU ? "DFU" : "Runtime", - dfu_if->vendor, dfu_if->product, - dfu_if->bcdDevice, dfu_if->devnum, - dfu_if->configuration, dfu_if->interface, - dfu_if->altsetting, dfu_if->alt_name, - dfu_if->serial_name); -} - -/* Walk the device tree and print out DFU devices */ -void list_dfu_interfaces(void) -{ - struct dfu_if *pdfu; - - for (pdfu = dfu_root; pdfu != NULL; pdfu = pdfu->next) - print_dfu_if(pdfu); -} +/* + * Functions for detecting DFU USB entities + * + * Written by Harald Welte + * Copyright 2007-2008 by OpenMoko, Inc. + * Copyright 2013 Hans Petter Selasky + * + * 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 +#include +#include +#include +#include + +#include "portable.h" +#include "dfu.h" +#include "usb_dfu.h" +#include "dfu_file.h" +#include "dfu_load.h" +#include "dfu_util.h" +#include "dfuse.h" +#include "quirks.h" + +#ifdef HAVE_USBPATH_H +#include +#endif + +/* + * Look for a descriptor in a concatenated descriptor list. Will + * return upon the first match of the given descriptor type. Returns length of + * found descriptor, limited to res_size + */ +static int find_descriptor(const uint8_t *desc_list, int list_len, + uint8_t desc_type, void *res_buf, int res_size) +{ + int p = 0; + + if (list_len < 2) + return (-1); + + while (p + 1 < list_len) { + int desclen; + + desclen = (int) desc_list[p]; + if (desclen == 0) { + warnx("Invalid descriptor list"); + return -1; + } + if (desc_list[p + 1] == desc_type) { + if (desclen > res_size) + desclen = res_size; + if (p + desclen > list_len) + desclen = list_len - p; + memcpy(res_buf, &desc_list[p], desclen); + return desclen; + } + p += (int) desc_list[p]; + } + return -1; +} + +static void probe_configuration(libusb_device *dev, struct libusb_device_descriptor *desc) +{ + struct usb_dfu_func_descriptor func_dfu; + libusb_device_handle *devh; + struct dfu_if *pdfu; + struct libusb_config_descriptor *cfg; + const struct libusb_interface_descriptor *intf; + const struct libusb_interface *uif; + char alt_name[MAX_DESC_STR_LEN + 1]; + char serial_name[MAX_DESC_STR_LEN + 1]; + int cfg_idx; + int intf_idx; + int alt_idx; + int ret; + int has_dfu; + + for (cfg_idx = 0; cfg_idx != desc->bNumConfigurations; cfg_idx++) { + memset(&func_dfu, 0, sizeof(func_dfu)); + has_dfu = 0; + + ret = libusb_get_config_descriptor(dev, cfg_idx, &cfg); + if (ret != 0) + return; + if (match_config_index > -1 && match_config_index != cfg->bConfigurationValue) { + libusb_free_config_descriptor(cfg); + continue; + } + + /* + * In some cases, noticably FreeBSD if uid != 0, + * the configuration descriptors are empty + */ + if (!cfg) + return; + + ret = find_descriptor(cfg->extra, cfg->extra_length, + USB_DT_DFU, &func_dfu, sizeof(func_dfu)); + if (ret > -1) + goto found_dfu; + + for (intf_idx = 0; intf_idx < cfg->bNumInterfaces; + intf_idx++) { + uif = &cfg->interface[intf_idx]; + if (!uif) + break; + + for (alt_idx = 0; alt_idx < cfg->interface[intf_idx].num_altsetting; + alt_idx++) { + intf = &uif->altsetting[alt_idx]; + + ret = find_descriptor(intf->extra, intf->extra_length, USB_DT_DFU, + &func_dfu, sizeof(func_dfu)); + if (ret > -1) + goto found_dfu; + + if (intf->bInterfaceClass != 0xfe || + intf->bInterfaceSubClass != 1) + continue; + + has_dfu = 1; + } + } + if (has_dfu) { + /* + * Finally try to retrieve it requesting the + * device directly This is not supported on + * all devices for non-standard types + */ + if (libusb_open(dev, &devh) == 0) { + ret = libusb_get_descriptor(devh, USB_DT_DFU, 0, + (void *)&func_dfu, sizeof(func_dfu)); + libusb_close(devh); + if (ret > -1) + goto found_dfu; + } + warnx("Device has DFU interface, " + "but has no DFU functional descriptor"); + + /* fake version 1.0 */ + func_dfu.bLength = 7; + func_dfu.bcdDFUVersion = libusb_cpu_to_le16(0x0100); + goto found_dfu; + } + libusb_free_config_descriptor(cfg); + continue; + +found_dfu: + if (func_dfu.bLength == 7) { + printf("Deducing device DFU version from functional descriptor " + "length\n"); + func_dfu.bcdDFUVersion = libusb_cpu_to_le16(0x0100); + } else if (func_dfu.bLength < 9) { + printf("Error obtaining DFU functional descriptor\n"); + printf("Please report this as a bug!\n"); + printf("Warning: Assuming DFU version 1.0\n"); + func_dfu.bcdDFUVersion = libusb_cpu_to_le16(0x0100); + printf("Warning: Transfer size can not be detected\n"); + func_dfu.wTransferSize = 0; + } + + for (intf_idx = 0; intf_idx < cfg->bNumInterfaces; + intf_idx++) { + if (match_iface_index > -1 && match_iface_index != intf_idx) + continue; + + uif = &cfg->interface[intf_idx]; + if (!uif) + break; + + for (alt_idx = 0; + alt_idx < uif->num_altsetting; alt_idx++) { + int dfu_mode; + + intf = &uif->altsetting[alt_idx]; + + if (intf->bInterfaceClass != 0xfe || + intf->bInterfaceSubClass != 1) + continue; + + dfu_mode = (intf->bInterfaceProtocol == 2); + /* e.g. DSO Nano has bInterfaceProtocol 0 instead of 2 */ + if (func_dfu.bcdDFUVersion == 0x011a && intf->bInterfaceProtocol == 0) + dfu_mode = 1; + + if (dfu_mode && + match_iface_alt_index > -1 && match_iface_alt_index != alt_idx) + continue; + + if (dfu_mode) { + if ((match_vendor_dfu >= 0 && match_vendor_dfu != desc->idVendor) || + (match_product_dfu >= 0 && match_product_dfu != desc->idProduct)) { + continue; + } + } else { + if ((match_vendor >= 0 && match_vendor != desc->idVendor) || + (match_product >= 0 && match_product != desc->idProduct)) { + continue; + } + } + + if (libusb_open(dev, &devh)) { + warnx("Cannot open DFU device %04x:%04x", desc->idVendor, desc->idProduct); + break; + } + if (intf->iInterface != 0) + ret = libusb_get_string_descriptor_ascii(devh, + intf->iInterface, (void *)alt_name, MAX_DESC_STR_LEN); + else + ret = -1; + if (ret < 1) + strcpy(alt_name, "UNKNOWN"); + if (desc->iSerialNumber != 0) + ret = libusb_get_string_descriptor_ascii(devh, + desc->iSerialNumber, (void *)serial_name, MAX_DESC_STR_LEN); + else + ret = -1; + if (ret < 1) + strcpy(serial_name, "UNKNOWN"); + libusb_close(devh); + + if (dfu_mode && + match_iface_alt_name != NULL && strcmp(alt_name, match_iface_alt_name)) + continue; + + if (dfu_mode) { + if (match_serial_dfu != NULL && strcmp(match_serial_dfu, serial_name)) + continue; + } else { + if (match_serial != NULL && strcmp(match_serial, serial_name)) + continue; + } + + pdfu = dfu_malloc(sizeof(*pdfu)); + + memset(pdfu, 0, sizeof(*pdfu)); + + pdfu->func_dfu = func_dfu; + pdfu->dev = libusb_ref_device(dev); + pdfu->quirks = get_quirks(desc->idVendor, + desc->idProduct, desc->bcdDevice); + pdfu->vendor = desc->idVendor; + pdfu->product = desc->idProduct; + pdfu->bcdDevice = desc->bcdDevice; + pdfu->configuration = cfg->bConfigurationValue; + pdfu->interface = intf->bInterfaceNumber; + pdfu->altsetting = intf->bAlternateSetting; + pdfu->devnum = libusb_get_device_address(dev); + pdfu->busnum = libusb_get_bus_number(dev); + pdfu->alt_name = strdup(alt_name); + if (pdfu->alt_name == NULL) + errx(EX_SOFTWARE, "Out of memory"); + pdfu->serial_name = strdup(serial_name); + if (pdfu->serial_name == NULL) + errx(EX_SOFTWARE, "Out of memory"); + if (dfu_mode) + pdfu->flags |= DFU_IFF_DFU; + if (pdfu->quirks & QUIRK_FORCE_DFU11) { + pdfu->func_dfu.bcdDFUVersion = + libusb_cpu_to_le16(0x0110); + } + pdfu->bMaxPacketSize0 = desc->bMaxPacketSize0; + + /* queue into list */ + pdfu->next = dfu_root; + dfu_root = pdfu; + } + } + libusb_free_config_descriptor(cfg); + } +} + +void probe_devices(libusb_context *ctx) +{ + libusb_device **list; + ssize_t num_devs; + ssize_t i; + + num_devs = libusb_get_device_list(ctx, &list); + for (i = 0; i < num_devs; ++i) { + struct libusb_device_descriptor desc; + struct libusb_device *dev = list[i]; + + if (match_bus > -1 && match_bus != libusb_get_bus_number(dev)) + continue; + if (match_device > -1 && match_device != libusb_get_device_address(dev)) + continue; + if (libusb_get_device_descriptor(dev, &desc)) + continue; + probe_configuration(dev, &desc); + } + libusb_free_device_list(list, 0); +} + +void disconnect_devices(void) +{ + struct dfu_if *pdfu; + struct dfu_if *prev = NULL; + + for (pdfu = dfu_root; pdfu != NULL; pdfu = pdfu->next) { + free(prev); + libusb_unref_device(pdfu->dev); + free(pdfu->alt_name); + free(pdfu->serial_name); + prev = pdfu; + } + free(prev); + dfu_root = NULL; +} + +void print_dfu_if(struct dfu_if *dfu_if) +{ + printf("Found %s: [%04x:%04x] ver=%04x, devnum=%u, cfg=%u, intf=%u, " + "alt=%u, name=\"%s\", serial=\"%s\"\n", + dfu_if->flags & DFU_IFF_DFU ? "DFU" : "Runtime", + dfu_if->vendor, dfu_if->product, + dfu_if->bcdDevice, dfu_if->devnum, + dfu_if->configuration, dfu_if->interface, + dfu_if->altsetting, dfu_if->alt_name, + dfu_if->serial_name); +} + +/* Walk the device tree and print out DFU devices */ +void list_dfu_interfaces(void) +{ + struct dfu_if *pdfu; + + for (pdfu = dfu_root; pdfu != NULL; pdfu = pdfu->next) + print_dfu_if(pdfu); +} diff --git a/tools/src/dfu-util/src/dfu_util.h b/tools/linux/src/dfu-util/src/dfu_util.h old mode 100755 new mode 100644 similarity index 95% rename from tools/src/dfu-util/src/dfu_util.h rename to tools/linux/src/dfu-util/src/dfu_util.h index 24e50c1..fc0c19d --- a/tools/src/dfu-util/src/dfu_util.h +++ b/tools/linux/src/dfu-util/src/dfu_util.h @@ -1,36 +1,36 @@ -#ifndef DFU_UTIL_H -#define DFU_UTIL_H - -/* USB string descriptor should contain max 126 UTF-16 characters - * but 253 would even accomodate any UTF-8 encoding */ -#define MAX_DESC_STR_LEN 253 - -enum mode { - MODE_NONE, - MODE_VERSION, - MODE_LIST, - MODE_DETACH, - MODE_UPLOAD, - MODE_DOWNLOAD -}; - -extern struct dfu_if *dfu_root; -extern int match_bus; -extern int match_device; -extern int match_vendor; -extern int match_product; -extern int match_vendor_dfu; -extern int match_product_dfu; -extern int match_config_index; -extern int match_iface_index; -extern int match_iface_alt_index; -extern const char *match_iface_alt_name; -extern const char *match_serial; -extern const char *match_serial_dfu; - -void probe_devices(libusb_context *); -void disconnect_devices(void); -void print_dfu_if(struct dfu_if *); -void list_dfu_interfaces(void); - -#endif /* DFU_UTIL_H */ +#ifndef DFU_UTIL_H +#define DFU_UTIL_H + +/* USB string descriptor should contain max 126 UTF-16 characters + * but 253 would even accomodate any UTF-8 encoding */ +#define MAX_DESC_STR_LEN 253 + +enum mode { + MODE_NONE, + MODE_VERSION, + MODE_LIST, + MODE_DETACH, + MODE_UPLOAD, + MODE_DOWNLOAD +}; + +extern struct dfu_if *dfu_root; +extern int match_bus; +extern int match_device; +extern int match_vendor; +extern int match_product; +extern int match_vendor_dfu; +extern int match_product_dfu; +extern int match_config_index; +extern int match_iface_index; +extern int match_iface_alt_index; +extern const char *match_iface_alt_name; +extern const char *match_serial; +extern const char *match_serial_dfu; + +void probe_devices(libusb_context *); +void disconnect_devices(void); +void print_dfu_if(struct dfu_if *); +void list_dfu_interfaces(void); + +#endif /* DFU_UTIL_H */ diff --git a/tools/src/dfu-util/src/dfuse.c b/tools/linux/src/dfu-util/src/dfuse.c old mode 100755 new mode 100644 similarity index 96% rename from tools/src/dfu-util/src/dfuse.c rename to tools/linux/src/dfu-util/src/dfuse.c index 39bc72d..fce29fe --- a/tools/src/dfu-util/src/dfuse.c +++ b/tools/linux/src/dfu-util/src/dfuse.c @@ -1,652 +1,652 @@ -/* - * DfuSe specific functions - * - * This implements the ST Microsystems DFU extensions (DfuSe) - * as per the DfuSe 1.1a specification (ST documents AN3156, AN2606) - * The DfuSe file format is described in ST document UM0391. - * - * Copyright 2010-2014 Tormod Volden - * - * 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 -#include -#include -#include - -#include "portable.h" -#include "dfu.h" -#include "usb_dfu.h" -#include "dfu_file.h" -#include "dfuse.h" -#include "dfuse_mem.h" - -#define DFU_TIMEOUT 5000 - -extern int verbose; -static unsigned int last_erased_page = 1; /* non-aligned value, won't match */ -static struct memsegment *mem_layout; -static unsigned int dfuse_address = 0; -static unsigned int dfuse_length = 0; -static int dfuse_force = 0; -static int dfuse_leave = 0; -static int dfuse_unprotect = 0; -static int dfuse_mass_erase = 0; - -unsigned int quad2uint(unsigned char *p) -{ - return (*p + (*(p + 1) << 8) + (*(p + 2) << 16) + (*(p + 3) << 24)); -} - -void dfuse_parse_options(const char *options) -{ - char *end; - const char *endword; - unsigned int number; - - /* address, possibly empty, must be first */ - if (*options != ':') { - endword = strchr(options, ':'); - if (!endword) - endword = options + strlen(options); /* GNU strchrnul */ - - number = strtoul(options, &end, 0); - if (end == endword) { - dfuse_address = number; - } else { - errx(EX_IOERR, "Invalid dfuse address: %s", options); - } - options = endword; - } - - while (*options) { - if (*options == ':') { - options++; - continue; - } - endword = strchr(options, ':'); - if (!endword) - endword = options + strlen(options); - - if (!strncmp(options, "force", endword - options)) { - dfuse_force++; - options += 5; - continue; - } - if (!strncmp(options, "leave", endword - options)) { - dfuse_leave = 1; - options += 5; - continue; - } - if (!strncmp(options, "unprotect", endword - options)) { - dfuse_unprotect = 1; - options += 9; - continue; - } - if (!strncmp(options, "mass-erase", endword - options)) { - dfuse_mass_erase = 1; - options += 10; - continue; - } - - /* any valid number is interpreted as upload length */ - number = strtoul(options, &end, 0); - if (end == endword) { - dfuse_length = number; - } else { - errx(EX_IOERR, "Invalid dfuse modifier: %s", options); - } - options = endword; - } -} - -/* DFU_UPLOAD request for DfuSe 1.1a */ -int dfuse_upload(struct dfu_if *dif, const unsigned short length, - unsigned char *data, unsigned short transaction) -{ - int status; - - status = libusb_control_transfer(dif->dev_handle, - /* bmRequestType */ LIBUSB_ENDPOINT_IN | - LIBUSB_REQUEST_TYPE_CLASS | - LIBUSB_RECIPIENT_INTERFACE, - /* bRequest */ DFU_UPLOAD, - /* wValue */ transaction, - /* wIndex */ dif->interface, - /* Data */ data, - /* wLength */ length, - DFU_TIMEOUT); - if (status < 0) { - errx(EX_IOERR, "%s: libusb_control_msg returned %d", - __FUNCTION__, status); - } - return status; -} - -/* DFU_DNLOAD request for DfuSe 1.1a */ -int dfuse_download(struct dfu_if *dif, const unsigned short length, - unsigned char *data, unsigned short transaction) -{ - int status; - - status = libusb_control_transfer(dif->dev_handle, - /* bmRequestType */ LIBUSB_ENDPOINT_OUT | - LIBUSB_REQUEST_TYPE_CLASS | - LIBUSB_RECIPIENT_INTERFACE, - /* bRequest */ DFU_DNLOAD, - /* wValue */ transaction, - /* wIndex */ dif->interface, - /* Data */ data, - /* wLength */ length, - DFU_TIMEOUT); - if (status < 0) { - errx(EX_IOERR, "%s: libusb_control_transfer returned %d", - __FUNCTION__, status); - } - return status; -} - -/* DfuSe only commands */ -/* Leaves the device in dfuDNLOAD-IDLE state */ -int dfuse_special_command(struct dfu_if *dif, unsigned int address, - enum dfuse_command command) -{ - const char* dfuse_command_name[] = { "SET_ADDRESS" , "ERASE_PAGE", - "MASS_ERASE", "READ_UNPROTECT"}; - unsigned char buf[5]; - int length; - int ret; - struct dfu_status dst; - int firstpoll = 1; - - if (command == ERASE_PAGE) { - struct memsegment *segment; - int page_size; - - segment = find_segment(mem_layout, address); - if (!segment || !(segment->memtype & DFUSE_ERASABLE)) { - errx(EX_IOERR, "Page at 0x%08x can not be erased", - address); - } - page_size = segment->pagesize; - if (verbose > 1) - printf("Erasing page size %i at address 0x%08x, page " - "starting at 0x%08x\n", page_size, address, - address & ~(page_size - 1)); - buf[0] = 0x41; /* Erase command */ - length = 5; - last_erased_page = address & ~(page_size - 1); - } else if (command == SET_ADDRESS) { - if (verbose > 2) - printf(" Setting address pointer to 0x%08x\n", - address); - buf[0] = 0x21; /* Set Address Pointer command */ - length = 5; - } else if (command == MASS_ERASE) { - buf[0] = 0x41; /* Mass erase command when length = 1 */ - length = 1; - } else if (command == READ_UNPROTECT) { - buf[0] = 0x92; - length = 1; - } else { - errx(EX_IOERR, "Non-supported special command %d", command); - } - buf[1] = address & 0xff; - buf[2] = (address >> 8) & 0xff; - buf[3] = (address >> 16) & 0xff; - buf[4] = (address >> 24) & 0xff; - - ret = dfuse_download(dif, length, buf, 0); - if (ret < 0) { - errx(EX_IOERR, "Error during special command \"%s\" download", - dfuse_command_name[command]); - } - do { - ret = dfu_get_status(dif, &dst); - if (ret < 0) { - errx(EX_IOERR, "Error during special command \"%s\" get_status", - dfuse_command_name[command]); - } - if (firstpoll) { - firstpoll = 0; - if (dst.bState != DFU_STATE_dfuDNBUSY) { - printf("state(%u) = %s, status(%u) = %s\n", dst.bState, - dfu_state_to_string(dst.bState), dst.bStatus, - dfu_status_to_string(dst.bStatus)); - errx(EX_IOERR, "Wrong state after command \"%s\" download", - dfuse_command_name[command]); - } - } - /* wait while command is executed */ - if (verbose) - printf(" Poll timeout %i ms\n", dst.bwPollTimeout); - milli_sleep(dst.bwPollTimeout); - if (command == READ_UNPROTECT) - return ret; - } while (dst.bState == DFU_STATE_dfuDNBUSY); - - if (dst.bStatus != DFU_STATUS_OK) { - errx(EX_IOERR, "%s not correctly executed", - dfuse_command_name[command]); - } - return ret; -} - -int dfuse_dnload_chunk(struct dfu_if *dif, unsigned char *data, int size, - int transaction) -{ - int bytes_sent; - struct dfu_status dst; - int ret; - - ret = dfuse_download(dif, size, size ? data : NULL, transaction); - if (ret < 0) { - errx(EX_IOERR, "Error during download"); - return ret; - } - bytes_sent = ret; - - do { - ret = dfu_get_status(dif, &dst); - if (ret < 0) { - errx(EX_IOERR, "Error during download get_status"); - return ret; - } - milli_sleep(dst.bwPollTimeout); - } while (dst.bState != DFU_STATE_dfuDNLOAD_IDLE && - dst.bState != DFU_STATE_dfuERROR && - dst.bState != DFU_STATE_dfuMANIFEST); - - if (dst.bState == DFU_STATE_dfuMANIFEST) - printf("Transitioning to dfuMANIFEST state\n"); - - 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)); - return -1; - } - return bytes_sent; -} - -int dfuse_do_upload(struct dfu_if *dif, int xfer_size, int fd, - const char *dfuse_options) -{ - int total_bytes = 0; - int upload_limit = 0; - unsigned char *buf; - int transaction; - int ret; - - buf = dfu_malloc(xfer_size); - - if (dfuse_options) - dfuse_parse_options(dfuse_options); - if (dfuse_length) - upload_limit = dfuse_length; - if (dfuse_address) { - struct memsegment *segment; - - mem_layout = parse_memory_layout((char *)dif->alt_name); - if (!mem_layout) - errx(EX_IOERR, "Failed to parse memory layout"); - - segment = find_segment(mem_layout, dfuse_address); - if (!dfuse_force && - (!segment || !(segment->memtype & DFUSE_READABLE))) - errx(EX_IOERR, "Page at 0x%08x is not readable", - dfuse_address); - - if (!upload_limit) { - upload_limit = segment->end - dfuse_address + 1; - printf("Limiting upload to end of memory segment, " - "%i bytes\n", upload_limit); - } - dfuse_special_command(dif, dfuse_address, SET_ADDRESS); - dfu_abort_to_idle(dif); - } else { - /* Boot loader decides the start address, unknown to us */ - /* Use a short length to lower risk of running out of bounds */ - if (!upload_limit) - upload_limit = 0x4000; - printf("Limiting default upload to %i bytes\n", upload_limit); - } - - dfu_progress_bar("Upload", 0, 1); - - transaction = 2; - while (1) { - int rc; - - /* last chunk can be smaller than original xfer_size */ - if (upload_limit - total_bytes < xfer_size) - xfer_size = upload_limit - total_bytes; - rc = dfuse_upload(dif, xfer_size, buf, transaction++); - if (rc < 0) { - ret = rc; - goto out_free; - } - - dfu_file_write_crc(fd, 0, buf, rc); - total_bytes += rc; - - if (total_bytes < 0) - errx(EX_SOFTWARE, "Received too many bytes"); - - if (rc < xfer_size || total_bytes >= upload_limit) { - /* last block, return successfully */ - ret = total_bytes; - break; - } - dfu_progress_bar("Upload", total_bytes, upload_limit); - } - - dfu_progress_bar("Upload", total_bytes, total_bytes); - - dfu_abort_to_idle(dif); - if (dfuse_leave) { - dfuse_special_command(dif, dfuse_address, SET_ADDRESS); - dfuse_dnload_chunk(dif, NULL, 0, 2); /* Zero-size */ - } - - out_free: - free(buf); - - return ret; -} - -/* Writes an element of any size to the device, taking care of page erases */ -/* returns 0 on success, otherwise -EINVAL */ -int dfuse_dnload_element(struct dfu_if *dif, unsigned int dwElementAddress, - unsigned int dwElementSize, unsigned char *data, - int xfer_size) -{ - int p; - int ret; - struct memsegment *segment; - - /* Check at least that we can write to the last address */ - segment = - find_segment(mem_layout, dwElementAddress + dwElementSize - 1); - if (!segment || !(segment->memtype & DFUSE_WRITEABLE)) { - errx(EX_IOERR, "Last page at 0x%08x is not writeable", - dwElementAddress + dwElementSize - 1); - } - - dfu_progress_bar("Download", 0, 1); - - for (p = 0; p < (int)dwElementSize; p += xfer_size) { - int page_size; - unsigned int erase_address; - unsigned int address = dwElementAddress + p; - int chunk_size = xfer_size; - - segment = find_segment(mem_layout, address); - if (!segment || !(segment->memtype & DFUSE_WRITEABLE)) { - errx(EX_IOERR, "Page at 0x%08x is not writeable", - address); - } - page_size = segment->pagesize; - - /* check if this is the last chunk */ - if (p + chunk_size > (int)dwElementSize) - chunk_size = dwElementSize - p; - - /* Erase only for flash memory downloads */ - if ((segment->memtype & DFUSE_ERASABLE) && !dfuse_mass_erase) { - /* erase all involved pages */ - for (erase_address = address; - erase_address < address + chunk_size; - erase_address += page_size) - if ((erase_address & ~(page_size - 1)) != - last_erased_page) - dfuse_special_command(dif, - erase_address, - ERASE_PAGE); - - if (((address + chunk_size - 1) & ~(page_size - 1)) != - last_erased_page) { - if (verbose > 2) - printf(" Chunk extends into next page," - " erase it as well\n"); - dfuse_special_command(dif, - address + chunk_size - 1, - ERASE_PAGE); - } - } - - if (verbose) { - printf(" Download from image offset " - "%08x to memory %08x-%08x, size %i\n", - p, address, address + chunk_size - 1, - chunk_size); - } else { - dfu_progress_bar("Download", p, dwElementSize); - } - - dfuse_special_command(dif, address, SET_ADDRESS); - - /* transaction = 2 for no address offset */ - ret = dfuse_dnload_chunk(dif, data + p, chunk_size, 2); - if (ret != chunk_size) { - errx(EX_IOERR, "Failed to write whole chunk: " - "%i of %i bytes", ret, chunk_size); - return -EINVAL; - } - } - if (!verbose) - dfu_progress_bar("Download", dwElementSize, dwElementSize); - return 0; -} - -static void -dfuse_memcpy(unsigned char *dst, unsigned char **src, int *rem, int size) -{ - if (size > *rem) { - errx(EX_IOERR, "Corrupt DfuSe file: " - "Cannot read %d bytes from %d bytes", size, *rem); - } - if (dst != NULL) - memcpy(dst, *src, size); - (*src) += size; - (*rem) -= size; -} - -/* Download raw binary file to DfuSe device */ -int dfuse_do_bin_dnload(struct dfu_if *dif, int xfer_size, - struct dfu_file *file, unsigned int start_address) -{ - unsigned int dwElementAddress; - unsigned int dwElementSize; - unsigned char *data; - int ret; - - dwElementAddress = start_address; - dwElementSize = file->size.total - - file->size.suffix - file->size.prefix; - - printf("Downloading to address = 0x%08x, size = %i\n", - dwElementAddress, dwElementSize); - - data = file->firmware + file->size.prefix; - - ret = dfuse_dnload_element(dif, dwElementAddress, dwElementSize, data, - xfer_size); - if (ret != 0) - goto out_free; - - printf("File downloaded successfully\n"); - ret = dwElementSize; - - out_free: - return ret; -} - -/* Parse a DfuSe file and download contents to device */ -int dfuse_do_dfuse_dnload(struct dfu_if *dif, int xfer_size, - struct dfu_file *file) -{ - uint8_t dfuprefix[11]; - uint8_t targetprefix[274]; - uint8_t elementheader[8]; - int image; - int element; - int bTargets; - int bAlternateSetting; - int dwNbElements; - unsigned int dwElementAddress; - unsigned int dwElementSize; - uint8_t *data; - int ret; - int rem; - int bFirstAddressSaved = 0; - - rem = file->size.total - file->size.prefix - file->size.suffix; - data = file->firmware + file->size.prefix; - - /* Must be larger than a minimal DfuSe header and suffix */ - if (rem < (int)(sizeof(dfuprefix) + - sizeof(targetprefix) + sizeof(elementheader))) { - errx(EX_SOFTWARE, "File too small for a DfuSe file"); - } - - dfuse_memcpy(dfuprefix, &data, &rem, sizeof(dfuprefix)); - - if (strncmp((char *)dfuprefix, "DfuSe", 5)) { - errx(EX_IOERR, "No valid DfuSe signature"); - return -EINVAL; - } - if (dfuprefix[5] != 0x01) { - errx(EX_IOERR, "DFU format revision %i not supported", - dfuprefix[5]); - return -EINVAL; - } - bTargets = dfuprefix[10]; - printf("file contains %i DFU images\n", bTargets); - - for (image = 1; image <= bTargets; image++) { - printf("parsing DFU image %i\n", image); - dfuse_memcpy(targetprefix, &data, &rem, sizeof(targetprefix)); - if (strncmp((char *)targetprefix, "Target", 6)) { - errx(EX_IOERR, "No valid target signature"); - return -EINVAL; - } - bAlternateSetting = targetprefix[6]; - dwNbElements = quad2uint((unsigned char *)targetprefix + 270); - printf("image for alternate setting %i, ", bAlternateSetting); - printf("(%i elements, ", dwNbElements); - printf("total size = %i)\n", - quad2uint((unsigned char *)targetprefix + 266)); - if (bAlternateSetting != dif->altsetting) - printf("Warning: Image does not match current alternate" - " setting.\n" - "Please rerun with the correct -a option setting" - " to download this image!\n"); - for (element = 1; element <= dwNbElements; element++) { - printf("parsing element %i, ", element); - dfuse_memcpy(elementheader, &data, &rem, sizeof(elementheader)); - dwElementAddress = - quad2uint((unsigned char *)elementheader); - dwElementSize = - quad2uint((unsigned char *)elementheader + 4); - printf("address = 0x%08x, ", dwElementAddress); - printf("size = %i\n", dwElementSize); - - if (!bFirstAddressSaved) { - bFirstAddressSaved = 1; - dfuse_address = dwElementAddress; - } - /* sanity check */ - if ((int)dwElementSize > rem) - errx(EX_SOFTWARE, "File too small for element size"); - - if (bAlternateSetting == dif->altsetting) { - ret = dfuse_dnload_element(dif, dwElementAddress, - dwElementSize, data, xfer_size); - } else { - ret = 0; - } - - /* advance read pointer */ - dfuse_memcpy(NULL, &data, &rem, dwElementSize); - - if (ret != 0) - return ret; - } - } - - if (rem != 0) - warnx("%d bytes leftover", rem); - - printf("done parsing DfuSe file\n"); - - return 0; -} - -int dfuse_do_dnload(struct dfu_if *dif, int xfer_size, struct dfu_file *file, - const char *dfuse_options) -{ - int ret; - - if (dfuse_options) - dfuse_parse_options(dfuse_options); - mem_layout = parse_memory_layout((char *)dif->alt_name); - if (!mem_layout) { - errx(EX_IOERR, "Failed to parse memory layout"); - } - if (dfuse_unprotect) { - if (!dfuse_force) { - errx(EX_IOERR, "The read unprotect command " - "will erase the flash memory" - "and can only be used with force\n"); - } - dfuse_special_command(dif, 0, READ_UNPROTECT); - printf("Device disconnects, erases flash and resets now\n"); - exit(0); - } - if (dfuse_mass_erase) { - if (!dfuse_force) { - errx(EX_IOERR, "The mass erase command " - "can only be used with force"); - } - printf("Performing mass erase, this can take a moment\n"); - dfuse_special_command(dif, 0, MASS_ERASE); - } - if (dfuse_address) { - if (file->bcdDFU == 0x11a) { - errx(EX_IOERR, "This is a DfuSe file, not " - "meant for raw download"); - } - ret = dfuse_do_bin_dnload(dif, xfer_size, file, dfuse_address); - } else { - if (file->bcdDFU != 0x11a) { - warnx("Only DfuSe file version 1.1a is supported"); - errx(EX_IOERR, "(for raw binary download, use the " - "--dfuse-address option)"); - } - ret = dfuse_do_dfuse_dnload(dif, xfer_size, file); - } - free_segment_list(mem_layout); - - dfu_abort_to_idle(dif); - - if (dfuse_leave) { - dfuse_special_command(dif, dfuse_address, SET_ADDRESS); - dfuse_dnload_chunk(dif, NULL, 0, 2); /* Zero-size */ - } - return ret; -} +/* + * DfuSe specific functions + * + * This implements the ST Microsystems DFU extensions (DfuSe) + * as per the DfuSe 1.1a specification (ST documents AN3156, AN2606) + * The DfuSe file format is described in ST document UM0391. + * + * Copyright 2010-2014 Tormod Volden + * + * 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 +#include +#include +#include + +#include "portable.h" +#include "dfu.h" +#include "usb_dfu.h" +#include "dfu_file.h" +#include "dfuse.h" +#include "dfuse_mem.h" + +#define DFU_TIMEOUT 5000 + +extern int verbose; +static unsigned int last_erased_page = 1; /* non-aligned value, won't match */ +static struct memsegment *mem_layout; +static unsigned int dfuse_address = 0; +static unsigned int dfuse_length = 0; +static int dfuse_force = 0; +static int dfuse_leave = 0; +static int dfuse_unprotect = 0; +static int dfuse_mass_erase = 0; + +unsigned int quad2uint(unsigned char *p) +{ + return (*p + (*(p + 1) << 8) + (*(p + 2) << 16) + (*(p + 3) << 24)); +} + +void dfuse_parse_options(const char *options) +{ + char *end; + const char *endword; + unsigned int number; + + /* address, possibly empty, must be first */ + if (*options != ':') { + endword = strchr(options, ':'); + if (!endword) + endword = options + strlen(options); /* GNU strchrnul */ + + number = strtoul(options, &end, 0); + if (end == endword) { + dfuse_address = number; + } else { + errx(EX_IOERR, "Invalid dfuse address: %s", options); + } + options = endword; + } + + while (*options) { + if (*options == ':') { + options++; + continue; + } + endword = strchr(options, ':'); + if (!endword) + endword = options + strlen(options); + + if (!strncmp(options, "force", endword - options)) { + dfuse_force++; + options += 5; + continue; + } + if (!strncmp(options, "leave", endword - options)) { + dfuse_leave = 1; + options += 5; + continue; + } + if (!strncmp(options, "unprotect", endword - options)) { + dfuse_unprotect = 1; + options += 9; + continue; + } + if (!strncmp(options, "mass-erase", endword - options)) { + dfuse_mass_erase = 1; + options += 10; + continue; + } + + /* any valid number is interpreted as upload length */ + number = strtoul(options, &end, 0); + if (end == endword) { + dfuse_length = number; + } else { + errx(EX_IOERR, "Invalid dfuse modifier: %s", options); + } + options = endword; + } +} + +/* DFU_UPLOAD request for DfuSe 1.1a */ +int dfuse_upload(struct dfu_if *dif, const unsigned short length, + unsigned char *data, unsigned short transaction) +{ + int status; + + status = libusb_control_transfer(dif->dev_handle, + /* bmRequestType */ LIBUSB_ENDPOINT_IN | + LIBUSB_REQUEST_TYPE_CLASS | + LIBUSB_RECIPIENT_INTERFACE, + /* bRequest */ DFU_UPLOAD, + /* wValue */ transaction, + /* wIndex */ dif->interface, + /* Data */ data, + /* wLength */ length, + DFU_TIMEOUT); + if (status < 0) { + errx(EX_IOERR, "%s: libusb_control_msg returned %d", + __FUNCTION__, status); + } + return status; +} + +/* DFU_DNLOAD request for DfuSe 1.1a */ +int dfuse_download(struct dfu_if *dif, const unsigned short length, + unsigned char *data, unsigned short transaction) +{ + int status; + + status = libusb_control_transfer(dif->dev_handle, + /* bmRequestType */ LIBUSB_ENDPOINT_OUT | + LIBUSB_REQUEST_TYPE_CLASS | + LIBUSB_RECIPIENT_INTERFACE, + /* bRequest */ DFU_DNLOAD, + /* wValue */ transaction, + /* wIndex */ dif->interface, + /* Data */ data, + /* wLength */ length, + DFU_TIMEOUT); + if (status < 0) { + errx(EX_IOERR, "%s: libusb_control_transfer returned %d", + __FUNCTION__, status); + } + return status; +} + +/* DfuSe only commands */ +/* Leaves the device in dfuDNLOAD-IDLE state */ +int dfuse_special_command(struct dfu_if *dif, unsigned int address, + enum dfuse_command command) +{ + const char* dfuse_command_name[] = { "SET_ADDRESS" , "ERASE_PAGE", + "MASS_ERASE", "READ_UNPROTECT"}; + unsigned char buf[5]; + int length; + int ret; + struct dfu_status dst; + int firstpoll = 1; + + if (command == ERASE_PAGE) { + struct memsegment *segment; + int page_size; + + segment = find_segment(mem_layout, address); + if (!segment || !(segment->memtype & DFUSE_ERASABLE)) { + errx(EX_IOERR, "Page at 0x%08x can not be erased", + address); + } + page_size = segment->pagesize; + if (verbose > 1) + printf("Erasing page size %i at address 0x%08x, page " + "starting at 0x%08x\n", page_size, address, + address & ~(page_size - 1)); + buf[0] = 0x41; /* Erase command */ + length = 5; + last_erased_page = address & ~(page_size - 1); + } else if (command == SET_ADDRESS) { + if (verbose > 2) + printf(" Setting address pointer to 0x%08x\n", + address); + buf[0] = 0x21; /* Set Address Pointer command */ + length = 5; + } else if (command == MASS_ERASE) { + buf[0] = 0x41; /* Mass erase command when length = 1 */ + length = 1; + } else if (command == READ_UNPROTECT) { + buf[0] = 0x92; + length = 1; + } else { + errx(EX_IOERR, "Non-supported special command %d", command); + } + buf[1] = address & 0xff; + buf[2] = (address >> 8) & 0xff; + buf[3] = (address >> 16) & 0xff; + buf[4] = (address >> 24) & 0xff; + + ret = dfuse_download(dif, length, buf, 0); + if (ret < 0) { + errx(EX_IOERR, "Error during special command \"%s\" download", + dfuse_command_name[command]); + } + do { + ret = dfu_get_status(dif, &dst); + if (ret < 0) { + errx(EX_IOERR, "Error during special command \"%s\" get_status", + dfuse_command_name[command]); + } + if (firstpoll) { + firstpoll = 0; + if (dst.bState != DFU_STATE_dfuDNBUSY) { + printf("state(%u) = %s, status(%u) = %s\n", dst.bState, + dfu_state_to_string(dst.bState), dst.bStatus, + dfu_status_to_string(dst.bStatus)); + errx(EX_IOERR, "Wrong state after command \"%s\" download", + dfuse_command_name[command]); + } + } + /* wait while command is executed */ + if (verbose) + printf(" Poll timeout %i ms\n", dst.bwPollTimeout); + milli_sleep(dst.bwPollTimeout); + if (command == READ_UNPROTECT) + return ret; + } while (dst.bState == DFU_STATE_dfuDNBUSY); + + if (dst.bStatus != DFU_STATUS_OK) { + errx(EX_IOERR, "%s not correctly executed", + dfuse_command_name[command]); + } + return ret; +} + +int dfuse_dnload_chunk(struct dfu_if *dif, unsigned char *data, int size, + int transaction) +{ + int bytes_sent; + struct dfu_status dst; + int ret; + + ret = dfuse_download(dif, size, size ? data : NULL, transaction); + if (ret < 0) { + errx(EX_IOERR, "Error during download"); + return ret; + } + bytes_sent = ret; + + do { + ret = dfu_get_status(dif, &dst); + if (ret < 0) { + errx(EX_IOERR, "Error during download get_status"); + return ret; + } + milli_sleep(dst.bwPollTimeout); + } while (dst.bState != DFU_STATE_dfuDNLOAD_IDLE && + dst.bState != DFU_STATE_dfuERROR && + dst.bState != DFU_STATE_dfuMANIFEST); + + if (dst.bState == DFU_STATE_dfuMANIFEST) + printf("Transitioning to dfuMANIFEST state\n"); + + 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)); + return -1; + } + return bytes_sent; +} + +int dfuse_do_upload(struct dfu_if *dif, int xfer_size, int fd, + const char *dfuse_options) +{ + int total_bytes = 0; + int upload_limit = 0; + unsigned char *buf; + int transaction; + int ret; + + buf = dfu_malloc(xfer_size); + + if (dfuse_options) + dfuse_parse_options(dfuse_options); + if (dfuse_length) + upload_limit = dfuse_length; + if (dfuse_address) { + struct memsegment *segment; + + mem_layout = parse_memory_layout((char *)dif->alt_name); + if (!mem_layout) + errx(EX_IOERR, "Failed to parse memory layout"); + + segment = find_segment(mem_layout, dfuse_address); + if (!dfuse_force && + (!segment || !(segment->memtype & DFUSE_READABLE))) + errx(EX_IOERR, "Page at 0x%08x is not readable", + dfuse_address); + + if (!upload_limit) { + upload_limit = segment->end - dfuse_address + 1; + printf("Limiting upload to end of memory segment, " + "%i bytes\n", upload_limit); + } + dfuse_special_command(dif, dfuse_address, SET_ADDRESS); + dfu_abort_to_idle(dif); + } else { + /* Boot loader decides the start address, unknown to us */ + /* Use a short length to lower risk of running out of bounds */ + if (!upload_limit) + upload_limit = 0x4000; + printf("Limiting default upload to %i bytes\n", upload_limit); + } + + dfu_progress_bar("Upload", 0, 1); + + transaction = 2; + while (1) { + int rc; + + /* last chunk can be smaller than original xfer_size */ + if (upload_limit - total_bytes < xfer_size) + xfer_size = upload_limit - total_bytes; + rc = dfuse_upload(dif, xfer_size, buf, transaction++); + if (rc < 0) { + ret = rc; + goto out_free; + } + + dfu_file_write_crc(fd, 0, buf, rc); + total_bytes += rc; + + if (total_bytes < 0) + errx(EX_SOFTWARE, "Received too many bytes"); + + if (rc < xfer_size || total_bytes >= upload_limit) { + /* last block, return successfully */ + ret = total_bytes; + break; + } + dfu_progress_bar("Upload", total_bytes, upload_limit); + } + + dfu_progress_bar("Upload", total_bytes, total_bytes); + + dfu_abort_to_idle(dif); + if (dfuse_leave) { + dfuse_special_command(dif, dfuse_address, SET_ADDRESS); + dfuse_dnload_chunk(dif, NULL, 0, 2); /* Zero-size */ + } + + out_free: + free(buf); + + return ret; +} + +/* Writes an element of any size to the device, taking care of page erases */ +/* returns 0 on success, otherwise -EINVAL */ +int dfuse_dnload_element(struct dfu_if *dif, unsigned int dwElementAddress, + unsigned int dwElementSize, unsigned char *data, + int xfer_size) +{ + int p; + int ret; + struct memsegment *segment; + + /* Check at least that we can write to the last address */ + segment = + find_segment(mem_layout, dwElementAddress + dwElementSize - 1); + if (!segment || !(segment->memtype & DFUSE_WRITEABLE)) { + errx(EX_IOERR, "Last page at 0x%08x is not writeable", + dwElementAddress + dwElementSize - 1); + } + + dfu_progress_bar("Download", 0, 1); + + for (p = 0; p < (int)dwElementSize; p += xfer_size) { + int page_size; + unsigned int erase_address; + unsigned int address = dwElementAddress + p; + int chunk_size = xfer_size; + + segment = find_segment(mem_layout, address); + if (!segment || !(segment->memtype & DFUSE_WRITEABLE)) { + errx(EX_IOERR, "Page at 0x%08x is not writeable", + address); + } + page_size = segment->pagesize; + + /* check if this is the last chunk */ + if (p + chunk_size > (int)dwElementSize) + chunk_size = dwElementSize - p; + + /* Erase only for flash memory downloads */ + if ((segment->memtype & DFUSE_ERASABLE) && !dfuse_mass_erase) { + /* erase all involved pages */ + for (erase_address = address; + erase_address < address + chunk_size; + erase_address += page_size) + if ((erase_address & ~(page_size - 1)) != + last_erased_page) + dfuse_special_command(dif, + erase_address, + ERASE_PAGE); + + if (((address + chunk_size - 1) & ~(page_size - 1)) != + last_erased_page) { + if (verbose > 2) + printf(" Chunk extends into next page," + " erase it as well\n"); + dfuse_special_command(dif, + address + chunk_size - 1, + ERASE_PAGE); + } + } + + if (verbose) { + printf(" Download from image offset " + "%08x to memory %08x-%08x, size %i\n", + p, address, address + chunk_size - 1, + chunk_size); + } else { + dfu_progress_bar("Download", p, dwElementSize); + } + + dfuse_special_command(dif, address, SET_ADDRESS); + + /* transaction = 2 for no address offset */ + ret = dfuse_dnload_chunk(dif, data + p, chunk_size, 2); + if (ret != chunk_size) { + errx(EX_IOERR, "Failed to write whole chunk: " + "%i of %i bytes", ret, chunk_size); + return -EINVAL; + } + } + if (!verbose) + dfu_progress_bar("Download", dwElementSize, dwElementSize); + return 0; +} + +static void +dfuse_memcpy(unsigned char *dst, unsigned char **src, int *rem, int size) +{ + if (size > *rem) { + errx(EX_IOERR, "Corrupt DfuSe file: " + "Cannot read %d bytes from %d bytes", size, *rem); + } + if (dst != NULL) + memcpy(dst, *src, size); + (*src) += size; + (*rem) -= size; +} + +/* Download raw binary file to DfuSe device */ +int dfuse_do_bin_dnload(struct dfu_if *dif, int xfer_size, + struct dfu_file *file, unsigned int start_address) +{ + unsigned int dwElementAddress; + unsigned int dwElementSize; + unsigned char *data; + int ret; + + dwElementAddress = start_address; + dwElementSize = file->size.total - + file->size.suffix - file->size.prefix; + + printf("Downloading to address = 0x%08x, size = %i\n", + dwElementAddress, dwElementSize); + + data = file->firmware + file->size.prefix; + + ret = dfuse_dnload_element(dif, dwElementAddress, dwElementSize, data, + xfer_size); + if (ret != 0) + goto out_free; + + printf("File downloaded successfully\n"); + ret = dwElementSize; + + out_free: + return ret; +} + +/* Parse a DfuSe file and download contents to device */ +int dfuse_do_dfuse_dnload(struct dfu_if *dif, int xfer_size, + struct dfu_file *file) +{ + uint8_t dfuprefix[11]; + uint8_t targetprefix[274]; + uint8_t elementheader[8]; + int image; + int element; + int bTargets; + int bAlternateSetting; + int dwNbElements; + unsigned int dwElementAddress; + unsigned int dwElementSize; + uint8_t *data; + int ret; + int rem; + int bFirstAddressSaved = 0; + + rem = file->size.total - file->size.prefix - file->size.suffix; + data = file->firmware + file->size.prefix; + + /* Must be larger than a minimal DfuSe header and suffix */ + if (rem < (int)(sizeof(dfuprefix) + + sizeof(targetprefix) + sizeof(elementheader))) { + errx(EX_SOFTWARE, "File too small for a DfuSe file"); + } + + dfuse_memcpy(dfuprefix, &data, &rem, sizeof(dfuprefix)); + + if (strncmp((char *)dfuprefix, "DfuSe", 5)) { + errx(EX_IOERR, "No valid DfuSe signature"); + return -EINVAL; + } + if (dfuprefix[5] != 0x01) { + errx(EX_IOERR, "DFU format revision %i not supported", + dfuprefix[5]); + return -EINVAL; + } + bTargets = dfuprefix[10]; + printf("file contains %i DFU images\n", bTargets); + + for (image = 1; image <= bTargets; image++) { + printf("parsing DFU image %i\n", image); + dfuse_memcpy(targetprefix, &data, &rem, sizeof(targetprefix)); + if (strncmp((char *)targetprefix, "Target", 6)) { + errx(EX_IOERR, "No valid target signature"); + return -EINVAL; + } + bAlternateSetting = targetprefix[6]; + dwNbElements = quad2uint((unsigned char *)targetprefix + 270); + printf("image for alternate setting %i, ", bAlternateSetting); + printf("(%i elements, ", dwNbElements); + printf("total size = %i)\n", + quad2uint((unsigned char *)targetprefix + 266)); + if (bAlternateSetting != dif->altsetting) + printf("Warning: Image does not match current alternate" + " setting.\n" + "Please rerun with the correct -a option setting" + " to download this image!\n"); + for (element = 1; element <= dwNbElements; element++) { + printf("parsing element %i, ", element); + dfuse_memcpy(elementheader, &data, &rem, sizeof(elementheader)); + dwElementAddress = + quad2uint((unsigned char *)elementheader); + dwElementSize = + quad2uint((unsigned char *)elementheader + 4); + printf("address = 0x%08x, ", dwElementAddress); + printf("size = %i\n", dwElementSize); + + if (!bFirstAddressSaved) { + bFirstAddressSaved = 1; + dfuse_address = dwElementAddress; + } + /* sanity check */ + if ((int)dwElementSize > rem) + errx(EX_SOFTWARE, "File too small for element size"); + + if (bAlternateSetting == dif->altsetting) { + ret = dfuse_dnload_element(dif, dwElementAddress, + dwElementSize, data, xfer_size); + } else { + ret = 0; + } + + /* advance read pointer */ + dfuse_memcpy(NULL, &data, &rem, dwElementSize); + + if (ret != 0) + return ret; + } + } + + if (rem != 0) + warnx("%d bytes leftover", rem); + + printf("done parsing DfuSe file\n"); + + return 0; +} + +int dfuse_do_dnload(struct dfu_if *dif, int xfer_size, struct dfu_file *file, + const char *dfuse_options) +{ + int ret; + + if (dfuse_options) + dfuse_parse_options(dfuse_options); + mem_layout = parse_memory_layout((char *)dif->alt_name); + if (!mem_layout) { + errx(EX_IOERR, "Failed to parse memory layout"); + } + if (dfuse_unprotect) { + if (!dfuse_force) { + errx(EX_IOERR, "The read unprotect command " + "will erase the flash memory" + "and can only be used with force\n"); + } + dfuse_special_command(dif, 0, READ_UNPROTECT); + printf("Device disconnects, erases flash and resets now\n"); + exit(0); + } + if (dfuse_mass_erase) { + if (!dfuse_force) { + errx(EX_IOERR, "The mass erase command " + "can only be used with force"); + } + printf("Performing mass erase, this can take a moment\n"); + dfuse_special_command(dif, 0, MASS_ERASE); + } + if (dfuse_address) { + if (file->bcdDFU == 0x11a) { + errx(EX_IOERR, "This is a DfuSe file, not " + "meant for raw download"); + } + ret = dfuse_do_bin_dnload(dif, xfer_size, file, dfuse_address); + } else { + if (file->bcdDFU != 0x11a) { + warnx("Only DfuSe file version 1.1a is supported"); + errx(EX_IOERR, "(for raw binary download, use the " + "--dfuse-address option)"); + } + ret = dfuse_do_dfuse_dnload(dif, xfer_size, file); + } + free_segment_list(mem_layout); + + dfu_abort_to_idle(dif); + + if (dfuse_leave) { + dfuse_special_command(dif, dfuse_address, SET_ADDRESS); + dfuse_dnload_chunk(dif, NULL, 0, 2); /* Zero-size */ + } + return ret; +} diff --git a/tools/src/dfu-util/src/dfuse.h b/tools/linux/src/dfu-util/src/dfuse.h old mode 100755 new mode 100644 similarity index 97% rename from tools/src/dfu-util/src/dfuse.h rename to tools/linux/src/dfu-util/src/dfuse.h index e82f058..ed1108c --- a/tools/src/dfu-util/src/dfuse.h +++ b/tools/linux/src/dfu-util/src/dfuse.h @@ -1,35 +1,35 @@ -/* This implements the ST Microsystems DFU extensions (DfuSe) - * as per the DfuSe 1.1a specification (Document UM0391) - * - * (C) 2010-2012 Tormod Volden - * - * 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 DFUSE_H -#define DFUSE_H - -#include "dfu.h" - -enum dfuse_command { SET_ADDRESS, ERASE_PAGE, MASS_ERASE, READ_UNPROTECT }; - -int dfuse_special_command(struct dfu_if *dif, unsigned int address, - enum dfuse_command command); -int dfuse_do_upload(struct dfu_if *dif, int xfer_size, int fd, - const char *dfuse_options); -int dfuse_do_dnload(struct dfu_if *dif, int xfer_size, struct dfu_file *file, - const char *dfuse_options); - -#endif /* DFUSE_H */ +/* This implements the ST Microsystems DFU extensions (DfuSe) + * as per the DfuSe 1.1a specification (Document UM0391) + * + * (C) 2010-2012 Tormod Volden + * + * 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 DFUSE_H +#define DFUSE_H + +#include "dfu.h" + +enum dfuse_command { SET_ADDRESS, ERASE_PAGE, MASS_ERASE, READ_UNPROTECT }; + +int dfuse_special_command(struct dfu_if *dif, unsigned int address, + enum dfuse_command command); +int dfuse_do_upload(struct dfu_if *dif, int xfer_size, int fd, + const char *dfuse_options); +int dfuse_do_dnload(struct dfu_if *dif, int xfer_size, struct dfu_file *file, + const char *dfuse_options); + +#endif /* DFUSE_H */ diff --git a/tools/src/dfu-util/src/dfuse_mem.c b/tools/linux/src/dfu-util/src/dfuse_mem.c old mode 100755 new mode 100644 similarity index 96% rename from tools/src/dfu-util/src/dfuse_mem.c rename to tools/linux/src/dfu-util/src/dfuse_mem.c index ae339fb..a91aacf --- a/tools/src/dfu-util/src/dfuse_mem.c +++ b/tools/linux/src/dfu-util/src/dfuse_mem.c @@ -1,198 +1,198 @@ -/* - * Helper functions for reading the memory map of a device - * following the ST DfuSe 1.1a specification. - * - * Copyright 2011-2014 Tormod Volden - * - * 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 -#include -#include -#include - -#include "portable.h" -#include "dfu_file.h" -#include "dfuse_mem.h" - -int add_segment(struct memsegment **segment_list, struct memsegment segment) -{ - struct memsegment *new_element; - - new_element = dfu_malloc(sizeof(struct memsegment)); - *new_element = segment; - new_element->next = NULL; - - if (*segment_list == NULL) - /* list can be empty on first call */ - *segment_list = new_element; - else { - struct memsegment *next_element; - - /* find last element in list */ - next_element = *segment_list; - while (next_element->next != NULL) - next_element = next_element->next; - next_element->next = new_element; - } - return 0; -} - -struct memsegment *find_segment(struct memsegment *segment_list, - unsigned int address) -{ - while (segment_list != NULL) { - if (segment_list->start <= address && - segment_list->end >= address) - return segment_list; - segment_list = segment_list->next; - } - return NULL; -} - -void free_segment_list(struct memsegment *segment_list) -{ - struct memsegment *next_element; - - while (segment_list->next != NULL) { - next_element = segment_list->next; - free(segment_list); - segment_list = next_element; - } - free(segment_list); -} - -/* Parse memory map from interface descriptor string - * encoded as per ST document UM0424 section 4.3.2. - */ -struct memsegment *parse_memory_layout(char *intf_desc) -{ - - char multiplier, memtype; - unsigned int address; - int sectors, size; - char *name, *typestring; - int ret; - int count = 0; - char separator; - int scanned; - struct memsegment *segment_list = NULL; - struct memsegment segment; - - name = dfu_malloc(strlen(intf_desc)); - - ret = sscanf(intf_desc, "@%[^/]%n", name, &scanned); - if (ret < 1) { - free(name); - warnx("Could not read name, sscanf returned %d", ret); - return NULL; - } - printf("DfuSe interface name: \"%s\"\n", name); - - intf_desc += scanned; - typestring = dfu_malloc(strlen(intf_desc)); - - while (ret = sscanf(intf_desc, "/0x%x/%n", &address, &scanned), - ret > 0) { - - intf_desc += scanned; - while (ret = sscanf(intf_desc, "%d*%d%c%[^,/]%n", - §ors, &size, &multiplier, typestring, - &scanned), ret > 2) { - intf_desc += scanned; - - count++; - memtype = 0; - if (ret == 4) { - if (strlen(typestring) == 1 - && typestring[0] != '/') - memtype = typestring[0]; - else { - warnx("Parsing type identifier '%s' " - "failed for segment %i", - typestring, count); - continue; - } - } - - /* Quirk for STM32F4 devices */ - if (strcmp(name, "Device Feature") == 0) - memtype = 'e'; - - switch (multiplier) { - case 'B': - break; - case 'K': - size *= 1024; - break; - case 'M': - size *= 1024 * 1024; - break; - case 'a': - case 'b': - case 'c': - case 'd': - case 'e': - case 'f': - case 'g': - if (!memtype) { - warnx("Non-valid multiplier '%c', " - "interpreted as type " - "identifier instead", - multiplier); - memtype = multiplier; - break; - } - /* fallthrough if memtype was already set */ - default: - warnx("Non-valid multiplier '%c', " - "assuming bytes", multiplier); - } - - if (!memtype) { - warnx("No valid type for segment %d\n", count); - continue; - } - - segment.start = address; - segment.end = address + sectors * size - 1; - segment.pagesize = size; - segment.memtype = memtype & 7; - add_segment(&segment_list, segment); - - if (verbose) - printf("Memory segment at 0x%08x %3d x %4d = " - "%5d (%s%s%s)\n", - address, sectors, size, sectors * size, - memtype & DFUSE_READABLE ? "r" : "", - memtype & DFUSE_ERASABLE ? "e" : "", - memtype & DFUSE_WRITEABLE ? "w" : ""); - - address += sectors * size; - - separator = *intf_desc; - if (separator == ',') - intf_desc += 1; - else - break; - } /* while per segment */ - - } /* while per address */ - free(name); - free(typestring); - - return segment_list; -} +/* + * Helper functions for reading the memory map of a device + * following the ST DfuSe 1.1a specification. + * + * Copyright 2011-2014 Tormod Volden + * + * 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 +#include +#include +#include + +#include "portable.h" +#include "dfu_file.h" +#include "dfuse_mem.h" + +int add_segment(struct memsegment **segment_list, struct memsegment segment) +{ + struct memsegment *new_element; + + new_element = dfu_malloc(sizeof(struct memsegment)); + *new_element = segment; + new_element->next = NULL; + + if (*segment_list == NULL) + /* list can be empty on first call */ + *segment_list = new_element; + else { + struct memsegment *next_element; + + /* find last element in list */ + next_element = *segment_list; + while (next_element->next != NULL) + next_element = next_element->next; + next_element->next = new_element; + } + return 0; +} + +struct memsegment *find_segment(struct memsegment *segment_list, + unsigned int address) +{ + while (segment_list != NULL) { + if (segment_list->start <= address && + segment_list->end >= address) + return segment_list; + segment_list = segment_list->next; + } + return NULL; +} + +void free_segment_list(struct memsegment *segment_list) +{ + struct memsegment *next_element; + + while (segment_list->next != NULL) { + next_element = segment_list->next; + free(segment_list); + segment_list = next_element; + } + free(segment_list); +} + +/* Parse memory map from interface descriptor string + * encoded as per ST document UM0424 section 4.3.2. + */ +struct memsegment *parse_memory_layout(char *intf_desc) +{ + + char multiplier, memtype; + unsigned int address; + int sectors, size; + char *name, *typestring; + int ret; + int count = 0; + char separator; + int scanned; + struct memsegment *segment_list = NULL; + struct memsegment segment; + + name = dfu_malloc(strlen(intf_desc)); + + ret = sscanf(intf_desc, "@%[^/]%n", name, &scanned); + if (ret < 1) { + free(name); + warnx("Could not read name, sscanf returned %d", ret); + return NULL; + } + printf("DfuSe interface name: \"%s\"\n", name); + + intf_desc += scanned; + typestring = dfu_malloc(strlen(intf_desc)); + + while (ret = sscanf(intf_desc, "/0x%x/%n", &address, &scanned), + ret > 0) { + + intf_desc += scanned; + while (ret = sscanf(intf_desc, "%d*%d%c%[^,/]%n", + §ors, &size, &multiplier, typestring, + &scanned), ret > 2) { + intf_desc += scanned; + + count++; + memtype = 0; + if (ret == 4) { + if (strlen(typestring) == 1 + && typestring[0] != '/') + memtype = typestring[0]; + else { + warnx("Parsing type identifier '%s' " + "failed for segment %i", + typestring, count); + continue; + } + } + + /* Quirk for STM32F4 devices */ + if (strcmp(name, "Device Feature") == 0) + memtype = 'e'; + + switch (multiplier) { + case 'B': + break; + case 'K': + size *= 1024; + break; + case 'M': + size *= 1024 * 1024; + break; + case 'a': + case 'b': + case 'c': + case 'd': + case 'e': + case 'f': + case 'g': + if (!memtype) { + warnx("Non-valid multiplier '%c', " + "interpreted as type " + "identifier instead", + multiplier); + memtype = multiplier; + break; + } + /* fallthrough if memtype was already set */ + default: + warnx("Non-valid multiplier '%c', " + "assuming bytes", multiplier); + } + + if (!memtype) { + warnx("No valid type for segment %d\n", count); + continue; + } + + segment.start = address; + segment.end = address + sectors * size - 1; + segment.pagesize = size; + segment.memtype = memtype & 7; + add_segment(&segment_list, segment); + + if (verbose) + printf("Memory segment at 0x%08x %3d x %4d = " + "%5d (%s%s%s)\n", + address, sectors, size, sectors * size, + memtype & DFUSE_READABLE ? "r" : "", + memtype & DFUSE_ERASABLE ? "e" : "", + memtype & DFUSE_WRITEABLE ? "w" : ""); + + address += sectors * size; + + separator = *intf_desc; + if (separator == ',') + intf_desc += 1; + else + break; + } /* while per segment */ + + } /* while per address */ + free(name); + free(typestring); + + return segment_list; +} diff --git a/tools/src/dfu-util/src/dfuse_mem.h b/tools/linux/src/dfu-util/src/dfuse_mem.h old mode 100755 new mode 100644 similarity index 96% rename from tools/src/dfu-util/src/dfuse_mem.h rename to tools/linux/src/dfu-util/src/dfuse_mem.h index 8fe7cc4..0181f0c --- a/tools/src/dfu-util/src/dfuse_mem.h +++ b/tools/linux/src/dfu-util/src/dfuse_mem.h @@ -1,44 +1,44 @@ -/* Helper functions for reading the memory map in a device - * following the ST DfuSe 1.1a specification. - * - * (C) 2011 Tormod Volden - * - * 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 DFUSE_MEM_H -#define DFUSE_MEM_H - -#define DFUSE_READABLE 1 -#define DFUSE_ERASABLE 2 -#define DFUSE_WRITEABLE 4 - -struct memsegment { - unsigned int start; - unsigned int end; - int pagesize; - int memtype; - struct memsegment *next; -}; - -int add_segment(struct memsegment **list, struct memsegment new_element); - -struct memsegment *find_segment(struct memsegment *list, unsigned int address); - -void free_segment_list(struct memsegment *list); - -struct memsegment *parse_memory_layout(char *intf_desc_str); - -#endif /* DFUSE_MEM_H */ +/* Helper functions for reading the memory map in a device + * following the ST DfuSe 1.1a specification. + * + * (C) 2011 Tormod Volden + * + * 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 DFUSE_MEM_H +#define DFUSE_MEM_H + +#define DFUSE_READABLE 1 +#define DFUSE_ERASABLE 2 +#define DFUSE_WRITEABLE 4 + +struct memsegment { + unsigned int start; + unsigned int end; + int pagesize; + int memtype; + struct memsegment *next; +}; + +int add_segment(struct memsegment **list, struct memsegment new_element); + +struct memsegment *find_segment(struct memsegment *list, unsigned int address); + +void free_segment_list(struct memsegment *list); + +struct memsegment *parse_memory_layout(char *intf_desc_str); + +#endif /* DFUSE_MEM_H */ diff --git a/tools/src/dfu-util/src/main.c b/tools/linux/src/dfu-util/src/main.c old mode 100755 new mode 100644 similarity index 96% rename from tools/src/dfu-util/src/main.c rename to tools/linux/src/dfu-util/src/main.c index f59d27d..acaed2f --- a/tools/src/dfu-util/src/main.c +++ b/tools/linux/src/dfu-util/src/main.c @@ -1,699 +1,699 @@ -/* - * dfu-util - * - * Copyright 2007-2008 by OpenMoko, Inc. - * Copyright 2013-2014 Hans Petter Selasky - * - * Written by Harald Welte - * - * 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 -#include -#include -#include -#include -#include -#include - -#include "portable.h" -#include "dfu.h" -#include "usb_dfu.h" -#include "dfu_file.h" -#include "dfu_load.h" -#include "dfu_util.h" -#include "dfuse.h" -#include "quirks.h" - -#ifdef HAVE_USBPATH_H -#include -#endif - -int verbose = 0; - -struct dfu_if *dfu_root = NULL; - -int match_bus = -1; -int match_device = -1; -int match_vendor = -1; -int match_product = -1; -int match_vendor_dfu = -1; -int match_product_dfu = -1; -int match_config_index = -1; -int match_iface_index = -1; -int match_iface_alt_index = -1; -const char *match_iface_alt_name = NULL; -const char *match_serial = NULL; -const char *match_serial_dfu = NULL; - -static int parse_match_value(const char *str, int default_value) -{ - char *remainder; - int value; - - if (str == NULL) { - value = default_value; - } else if (*str == '*') { - value = -1; /* Match anything */ - } else if (*str == '-') { - value = 0x10000; /* Impossible vendor/product ID */ - } else { - value = strtoul(str, &remainder, 16); - if (remainder == str) { - value = default_value; - } - } - return value; -} - -static void parse_vendprod(const char *str) -{ - const char *comma; - const char *colon; - - /* Default to match any DFU device in runtime or DFU mode */ - match_vendor = -1; - match_product = -1; - match_vendor_dfu = -1; - match_product_dfu = -1; - - comma = strchr(str, ','); - if (comma == str) { - /* DFU mode vendor/product being specified without any runtime - * vendor/product specification, so don't match any runtime device */ - match_vendor = match_product = 0x10000; - } else { - colon = strchr(str, ':'); - if (colon != NULL) { - ++colon; - if ((comma != NULL) && (colon > comma)) { - colon = NULL; - } - } - match_vendor = parse_match_value(str, match_vendor); - match_product = parse_match_value(colon, match_product); - if (comma != NULL) { - /* Both runtime and DFU mode vendor/product specifications are - * available, so default DFU mode match components to the given - * runtime match components */ - match_vendor_dfu = match_vendor; - match_product_dfu = match_product; - } - } - if (comma != NULL) { - ++comma; - colon = strchr(comma, ':'); - if (colon != NULL) { - ++colon; - } - match_vendor_dfu = parse_match_value(comma, match_vendor_dfu); - match_product_dfu = parse_match_value(colon, match_product_dfu); - } -} - -static void parse_serial(char *str) -{ - char *comma; - - match_serial = str; - comma = strchr(str, ','); - if (comma == NULL) { - match_serial_dfu = match_serial; - } else { - *comma++ = 0; - match_serial_dfu = comma; - } - if (*match_serial == 0) match_serial = NULL; - if (*match_serial_dfu == 0) match_serial_dfu = NULL; -} - -#ifdef HAVE_USBPATH_H - -static int resolve_device_path(char *path) -{ - int res; - - res = usb_path2devnum(path); - if (res < 0) - return -EINVAL; - if (!res) - return 0; - - match_bus = atoi(path); - match_device = res; - - return 0; -} - -#else /* HAVE_USBPATH_H */ - -static int resolve_device_path(char *path) -{ - (void)path; /* Eliminate unused variable warning */ - errx(EX_SOFTWARE, "USB device paths are not supported by this dfu-util.\n"); -} - -#endif /* !HAVE_USBPATH_H */ - -static void help(void) -{ - fprintf(stderr, "Usage: dfu-util [options] ...\n" - " -h --help\t\t\tPrint this help message\n" - " -V --version\t\t\tPrint the version number\n" - " -v --verbose\t\t\tPrint verbose debug statements\n" - " -l --list\t\t\tList currently attached DFU capable devices\n"); - fprintf(stderr, " -e --detach\t\t\tDetach currently attached DFU capable devices\n" - " -E --detach-delay seconds\tTime to wait before reopening a device after detach\n" - " -d --device :[,:]\n" - "\t\t\t\tSpecify Vendor/Product ID(s) of DFU device\n" - " -p --path \tSpecify path to DFU device\n" - " -c --cfg \t\tSpecify the Configuration of DFU device\n" - " -i --intf \t\tSpecify the DFU Interface number\n" - " -S --serial [,]\n" - "\t\t\t\tSpecify Serial String of DFU device\n" - " -a --alt \t\tSpecify the Altsetting of the DFU Interface\n" - "\t\t\t\tby name or by number\n"); - fprintf(stderr, " -t --transfer-size \tSpecify the number of bytes per USB Transfer\n" - " -U --upload \t\tRead firmware from device into \n" - " -Z --upload-size \tSpecify the expected upload size in bytes\n" - " -D --download \t\tWrite firmware from into device\n" - " -R --reset\t\t\tIssue USB Reset signalling once we're finished\n" - " -s --dfuse-address
\tST DfuSe mode, specify target address for\n" - "\t\t\t\traw file download or upload. Not applicable for\n" - "\t\t\t\tDfuSe file (.dfu) downloads\n" - ); - exit(EX_USAGE); -} - -static void print_version(void) -{ - printf(PACKAGE_STRING "\n\n"); - printf("Copyright 2005-2009 Weston Schmidt, Harald Welte and OpenMoko Inc.\n" - "Copyright 2010-2014 Tormod Volden and Stefan Schmidt\n" - "This program is Free Software and has ABSOLUTELY NO WARRANTY\n" - "Please report bugs to " PACKAGE_BUGREPORT "\n\n"); -} - -static struct option opts[] = { - { "help", 0, 0, 'h' }, - { "version", 0, 0, 'V' }, - { "verbose", 0, 0, 'v' }, - { "list", 0, 0, 'l' }, - { "detach", 0, 0, 'e' }, - { "detach-delay", 1, 0, 'E' }, - { "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' }, - { "serial", 1, 0, 'S' }, - { "transfer-size", 1, 0, 't' }, - { "upload", 1, 0, 'U' }, - { "upload-size", 1, 0, 'Z' }, - { "download", 1, 0, 'D' }, - { "reset", 0, 0, 'R' }, - { "dfuse-address", 1, 0, 's' }, - { 0, 0, 0, 0 } -}; - -int main(int argc, char **argv) -{ - int expected_size = 0; - unsigned int transfer_size = 0; - enum mode mode = MODE_NONE; - struct dfu_status status; - libusb_context *ctx; - struct dfu_file file; - char *end; - int final_reset = 0; - int ret; - int dfuse_device = 0; - int fd; - const char *dfuse_options = NULL; - int detach_delay = 5; - uint16_t runtime_vendor; - uint16_t runtime_product; - - memset(&file, 0, sizeof(file)); - - /* make sure all prints are flushed */ - setvbuf(stdout, NULL, _IONBF, 0); - - while (1) { - int c, option_index = 0; - c = getopt_long(argc, argv, "hVvleE:d:p:c:i:a:S:t:U:D:Rs:Z:", opts, - &option_index); - if (c == -1) - break; - - switch (c) { - case 'h': - help(); - break; - case 'V': - mode = MODE_VERSION; - break; - case 'v': - verbose++; - break; - case 'l': - mode = MODE_LIST; - break; - case 'e': - mode = MODE_DETACH; - match_iface_alt_index = 0; - match_iface_index = 0; - break; - case 'E': - detach_delay = atoi(optarg); - break; - case 'd': - parse_vendprod(optarg); - break; - case 'p': - /* Parse device path */ - ret = resolve_device_path(optarg); - if (ret < 0) - errx(EX_SOFTWARE, "Unable to parse '%s'", optarg); - if (!ret) - errx(EX_SOFTWARE, "Cannot find '%s'", optarg); - break; - case 'c': - /* Configuration */ - match_config_index = atoi(optarg); - break; - case 'i': - /* Interface */ - match_iface_index = atoi(optarg); - break; - case 'a': - /* Interface Alternate Setting */ - match_iface_alt_index = strtoul(optarg, &end, 0); - if (*end) { - match_iface_alt_name = optarg; - match_iface_alt_index = -1; - } - break; - case 'S': - parse_serial(optarg); - break; - case 't': - transfer_size = atoi(optarg); - break; - case 'U': - mode = MODE_UPLOAD; - file.name = optarg; - break; - case 'Z': - expected_size = atoi(optarg); - break; - case 'D': - mode = MODE_DOWNLOAD; - file.name = optarg; - break; - case 'R': - final_reset = 1; - break; - case 's': - dfuse_options = optarg; - break; - default: - help(); - break; - } - } - - print_version(); - if (mode == MODE_VERSION) { - exit(0); - } - - if (mode == MODE_NONE) { - fprintf(stderr, "You need to specify one of -D or -U\n"); - help(); - } - - if (match_config_index == 0) { - /* Handle "-c 0" (unconfigured device) as don't care */ - match_config_index = -1; - } - - if (mode == MODE_DOWNLOAD) { - dfu_load_file(&file, MAYBE_SUFFIX, MAYBE_PREFIX); - /* If the user didn't specify product and/or vendor IDs to match, - * use any IDs from the file suffix for device matching */ - if (match_vendor < 0 && file.idVendor != 0xffff) { - match_vendor = file.idVendor; - printf("Match vendor ID from file: %04x\n", match_vendor); - } - if (match_product < 0 && file.idProduct != 0xffff) { - match_product = file.idProduct; - printf("Match product ID from file: %04x\n", match_product); - } - } - - ret = libusb_init(&ctx); - if (ret) - errx(EX_IOERR, "unable to initialize libusb: %i", ret); - - if (verbose > 2) { - libusb_set_debug(ctx, 255); - } - - probe_devices(ctx); - - if (mode == MODE_LIST) { - list_dfu_interfaces(); - exit(0); - } - - if (dfu_root == NULL) { - errx(EX_IOERR, "No DFU capable USB device available"); - } else if (dfu_root->next != NULL) { - /* 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 */ - errx(EX_IOERR, "More than one DFU capable USB device found! " - "Try `--list' and specify the serial number " - "or disconnect all but one device\n"); - } - - /* We have exactly one device. Its libusb_device is now in dfu_root->dev */ - - printf("Opening DFU capable USB device...\n"); - ret = libusb_open(dfu_root->dev, &dfu_root->dev_handle); - if (ret || !dfu_root->dev_handle) - errx(EX_IOERR, "Cannot open device"); - - printf("ID %04x:%04x\n", dfu_root->vendor, dfu_root->product); - - printf("Run-time device DFU version %04x\n", - libusb_le16_to_cpu(dfu_root->func_dfu.bcdDFUVersion)); - - /* Transition from run-Time mode to DFU mode */ - if (!(dfu_root->flags & DFU_IFF_DFU)) { - int err; - /* 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 */ - - runtime_vendor = dfu_root->vendor; - runtime_product = dfu_root->product; - - printf("Claiming USB DFU Runtime Interface...\n"); - if (libusb_claim_interface(dfu_root->dev_handle, dfu_root->interface) < 0) { - errx(EX_IOERR, "Cannot claim interface %d", - dfu_root->interface); - } - - if (libusb_set_interface_alt_setting(dfu_root->dev_handle, dfu_root->interface, 0) < 0) { - errx(EX_IOERR, "Cannot set alt interface zero"); - } - - printf("Determining device status: "); - - err = dfu_get_status(dfu_root, &status); - if (err == LIBUSB_ERROR_PIPE) { - printf("Device does not implement get_status, assuming appIDLE\n"); - status.bStatus = DFU_STATUS_OK; - status.bwPollTimeout = 0; - status.bState = DFU_STATE_appIDLE; - status.iString = 0; - } else if (err < 0) { - errx(EX_IOERR, "error get_status"); - } else { - printf("state = %s, status = %d\n", - dfu_state_to_string(status.bState), status.bStatus); - } - milli_sleep(status.bwPollTimeout); - - 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(dfu_root->dev_handle, - dfu_root->interface, 1000) < 0) { - warnx("error detaching"); - } - if (dfu_root->func_dfu.bmAttributes & USB_DFU_WILL_DETACH) { - printf("Device will detach and reattach...\n"); - } else { - printf("Resetting USB...\n"); - ret = libusb_reset_device(dfu_root->dev_handle); - if (ret < 0 && ret != LIBUSB_ERROR_NOT_FOUND) - errx(EX_IOERR, "error resetting " - "after detach"); - } - break; - case DFU_STATE_dfuERROR: - printf("dfuERROR, clearing status\n"); - if (dfu_clear_status(dfu_root->dev_handle, - dfu_root->interface) < 0) { - errx(EX_IOERR, "error clear_status"); - } - /* fall through */ - default: - warnx("WARNING: Runtime device already in DFU state ?!?"); - libusb_release_interface(dfu_root->dev_handle, - dfu_root->interface); - goto dfustate; - } - libusb_release_interface(dfu_root->dev_handle, - dfu_root->interface); - libusb_close(dfu_root->dev_handle); - dfu_root->dev_handle = NULL; - - if (mode == MODE_DETACH) { - libusb_exit(ctx); - exit(0); - } - - /* keeping handles open might prevent re-enumeration */ - disconnect_devices(); - - milli_sleep(detach_delay * 1000); - - /* Change match vendor and product to impossible values to force - * only DFU mode matches in the following probe */ - match_vendor = match_product = 0x10000; - - probe_devices(ctx); - - if (dfu_root == NULL) { - errx(EX_IOERR, "Lost device after RESET?"); - } else if (dfu_root->next != NULL) { - errx(EX_IOERR, "More than one DFU capable USB device found! " - "Try `--list' and specify the serial number " - "or disconnect all but one device"); - } - - /* Check for DFU mode device */ - if (!(dfu_root->flags | DFU_IFF_DFU)) - errx(EX_SOFTWARE, "Device is not in DFU mode"); - - printf("Opening DFU USB Device...\n"); - ret = libusb_open(dfu_root->dev, &dfu_root->dev_handle); - if (ret || !dfu_root->dev_handle) { - errx(EX_IOERR, "Cannot open device"); - } - } else { - /* we're already in DFU mode, so we can skip the detach/reset - * procedure */ - /* If a match vendor/product was specified, use that as the runtime - * vendor/product, otherwise use the DFU mode vendor/product */ - runtime_vendor = match_vendor < 0 ? dfu_root->vendor : match_vendor; - runtime_product = match_product < 0 ? dfu_root->product : match_product; - } - -dfustate: -#if 0 - printf("Setting Configuration %u...\n", dfu_root->configuration); - if (libusb_set_configuration(dfu_root->dev_handle, dfu_root->configuration) < 0) { - errx(EX_IOERR, "Cannot set configuration"); - } -#endif - printf("Claiming USB DFU Interface...\n"); - if (libusb_claim_interface(dfu_root->dev_handle, dfu_root->interface) < 0) { - errx(EX_IOERR, "Cannot claim interface"); - } - - printf("Setting Alternate Setting #%d ...\n", dfu_root->altsetting); - if (libusb_set_interface_alt_setting(dfu_root->dev_handle, dfu_root->interface, dfu_root->altsetting) < 0) { - errx(EX_IOERR, "Cannot set alternate interface"); - } - -status_again: - printf("Determining device status: "); - if (dfu_get_status(dfu_root, &status ) < 0) { - errx(EX_IOERR, "error get_status"); - } - printf("state = %s, status = %d\n", - dfu_state_to_string(status.bState), status.bStatus); - - milli_sleep(status.bwPollTimeout); - - switch (status.bState) { - case DFU_STATE_appIDLE: - case DFU_STATE_appDETACH: - errx(EX_IOERR, "Device still in Runtime Mode!"); - break; - case DFU_STATE_dfuERROR: - printf("dfuERROR, clearing status\n"); - if (dfu_clear_status(dfu_root->dev_handle, dfu_root->interface) < 0) { - errx(EX_IOERR, "error clear_status"); - } - goto status_again; - break; - case DFU_STATE_dfuDNLOAD_IDLE: - case DFU_STATE_dfuUPLOAD_IDLE: - printf("aborting previous incomplete transfer\n"); - if (dfu_abort(dfu_root->dev_handle, dfu_root->interface) < 0) { - errx(EX_IOERR, "can't send DFU_ABORT"); - } - goto status_again; - break; - case DFU_STATE_dfuIDLE: - printf("dfuIDLE, continuing\n"); - break; - default: - break; - } - - if (DFU_STATUS_OK != status.bStatus ) { - printf("WARNING: DFU Status: '%s'\n", - dfu_status_to_string(status.bStatus)); - /* Clear our status & try again. */ - if (dfu_clear_status(dfu_root->dev_handle, dfu_root->interface) < 0) - errx(EX_IOERR, "USB communication error"); - if (dfu_get_status(dfu_root, &status) < 0) - errx(EX_IOERR, "USB communication error"); - if (DFU_STATUS_OK != status.bStatus) - errx(EX_SOFTWARE, "Status is not OK: %d", status.bStatus); - - milli_sleep(status.bwPollTimeout); - } - - printf("DFU mode device DFU version %04x\n", - libusb_le16_to_cpu(dfu_root->func_dfu.bcdDFUVersion)); - - if (dfu_root->func_dfu.bcdDFUVersion == libusb_cpu_to_le16(0x11a)) - dfuse_device = 1; - - /* If not overridden by the user */ - if (!transfer_size) { - transfer_size = libusb_le16_to_cpu( - dfu_root->func_dfu.wTransferSize); - if (transfer_size) { - printf("Device returned transfer size %i\n", - transfer_size); - } else { - errx(EX_IOERR, "Transfer size must be specified"); - } - } - -#ifdef HAVE_GETPAGESIZE -/* autotools lie when cross-compiling for Windows using mingw32/64 */ -#ifndef __MINGW32__ - /* limitation of Linux usbdevio */ - if ((int)transfer_size > getpagesize()) { - transfer_size = getpagesize(); - printf("Limited transfer size to %i\n", transfer_size); - } -#endif /* __MINGW32__ */ -#endif /* HAVE_GETPAGESIZE */ - - if (transfer_size < dfu_root->bMaxPacketSize0) { - transfer_size = dfu_root->bMaxPacketSize0; - printf("Adjusted transfer size to %i\n", transfer_size); - } - - switch (mode) { - case MODE_UPLOAD: - /* open for "exclusive" writing */ - fd = open(file.name, O_WRONLY | O_BINARY | O_CREAT | O_EXCL | O_TRUNC, 0666); - if (fd < 0) - err(EX_IOERR, "Cannot open file %s for writing", file.name); - - if (dfuse_device || dfuse_options) { - if (dfuse_do_upload(dfu_root, transfer_size, fd, - dfuse_options) < 0) - exit(1); - } else { - if (dfuload_do_upload(dfu_root, transfer_size, - expected_size, fd) < 0) { - exit(1); - } - } - close(fd); - break; - - case MODE_DOWNLOAD: - if (((file.idVendor != 0xffff && file.idVendor != runtime_vendor) || - (file.idProduct != 0xffff && file.idProduct != runtime_product)) && - ((file.idVendor != 0xffff && file.idVendor != dfu_root->vendor) || - (file.idProduct != 0xffff && file.idProduct != dfu_root->product))) { - errx(EX_IOERR, "Error: File ID %04x:%04x does " - "not match device (%04x:%04x or %04x:%04x)", - file.idVendor, file.idProduct, - runtime_vendor, runtime_product, - dfu_root->vendor, dfu_root->product); - } - if (dfuse_device || dfuse_options || file.bcdDFU == 0x11a) { - if (dfuse_do_dnload(dfu_root, transfer_size, &file, - dfuse_options) < 0) - exit(1); - } else { - if (dfuload_do_dnload(dfu_root, transfer_size, &file) < 0) - exit(1); - } - break; - case MODE_DETACH: - if (dfu_detach(dfu_root->dev_handle, dfu_root->interface, 1000) < 0) { - warnx("can't detach"); - } - break; - default: - errx(EX_IOERR, "Unsupported mode: %u", mode); - break; - } - - if (final_reset) { - if (dfu_detach(dfu_root->dev_handle, dfu_root->interface, 1000) < 0) { - /* Even if detach failed, just carry on to leave the - device in a known state */ - warnx("can't detach"); - } - printf("Resetting USB to switch back to runtime mode\n"); - ret = libusb_reset_device(dfu_root->dev_handle); - if (ret < 0 && ret != LIBUSB_ERROR_NOT_FOUND) { - errx(EX_IOERR, "error resetting after download"); - } - } - - libusb_close(dfu_root->dev_handle); - dfu_root->dev_handle = NULL; - libusb_exit(ctx); - - return (0); -} +/* + * dfu-util + * + * Copyright 2007-2008 by OpenMoko, Inc. + * Copyright 2013-2014 Hans Petter Selasky + * + * Written by Harald Welte + * + * 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 +#include +#include +#include +#include +#include +#include + +#include "portable.h" +#include "dfu.h" +#include "usb_dfu.h" +#include "dfu_file.h" +#include "dfu_load.h" +#include "dfu_util.h" +#include "dfuse.h" +#include "quirks.h" + +#ifdef HAVE_USBPATH_H +#include +#endif + +int verbose = 0; + +struct dfu_if *dfu_root = NULL; + +int match_bus = -1; +int match_device = -1; +int match_vendor = -1; +int match_product = -1; +int match_vendor_dfu = -1; +int match_product_dfu = -1; +int match_config_index = -1; +int match_iface_index = -1; +int match_iface_alt_index = -1; +const char *match_iface_alt_name = NULL; +const char *match_serial = NULL; +const char *match_serial_dfu = NULL; + +static int parse_match_value(const char *str, int default_value) +{ + char *remainder; + int value; + + if (str == NULL) { + value = default_value; + } else if (*str == '*') { + value = -1; /* Match anything */ + } else if (*str == '-') { + value = 0x10000; /* Impossible vendor/product ID */ + } else { + value = strtoul(str, &remainder, 16); + if (remainder == str) { + value = default_value; + } + } + return value; +} + +static void parse_vendprod(const char *str) +{ + const char *comma; + const char *colon; + + /* Default to match any DFU device in runtime or DFU mode */ + match_vendor = -1; + match_product = -1; + match_vendor_dfu = -1; + match_product_dfu = -1; + + comma = strchr(str, ','); + if (comma == str) { + /* DFU mode vendor/product being specified without any runtime + * vendor/product specification, so don't match any runtime device */ + match_vendor = match_product = 0x10000; + } else { + colon = strchr(str, ':'); + if (colon != NULL) { + ++colon; + if ((comma != NULL) && (colon > comma)) { + colon = NULL; + } + } + match_vendor = parse_match_value(str, match_vendor); + match_product = parse_match_value(colon, match_product); + if (comma != NULL) { + /* Both runtime and DFU mode vendor/product specifications are + * available, so default DFU mode match components to the given + * runtime match components */ + match_vendor_dfu = match_vendor; + match_product_dfu = match_product; + } + } + if (comma != NULL) { + ++comma; + colon = strchr(comma, ':'); + if (colon != NULL) { + ++colon; + } + match_vendor_dfu = parse_match_value(comma, match_vendor_dfu); + match_product_dfu = parse_match_value(colon, match_product_dfu); + } +} + +static void parse_serial(char *str) +{ + char *comma; + + match_serial = str; + comma = strchr(str, ','); + if (comma == NULL) { + match_serial_dfu = match_serial; + } else { + *comma++ = 0; + match_serial_dfu = comma; + } + if (*match_serial == 0) match_serial = NULL; + if (*match_serial_dfu == 0) match_serial_dfu = NULL; +} + +#ifdef HAVE_USBPATH_H + +static int resolve_device_path(char *path) +{ + int res; + + res = usb_path2devnum(path); + if (res < 0) + return -EINVAL; + if (!res) + return 0; + + match_bus = atoi(path); + match_device = res; + + return 0; +} + +#else /* HAVE_USBPATH_H */ + +static int resolve_device_path(char *path) +{ + (void)path; /* Eliminate unused variable warning */ + errx(EX_SOFTWARE, "USB device paths are not supported by this dfu-util.\n"); +} + +#endif /* !HAVE_USBPATH_H */ + +static void help(void) +{ + fprintf(stderr, "Usage: dfu-util [options] ...\n" + " -h --help\t\t\tPrint this help message\n" + " -V --version\t\t\tPrint the version number\n" + " -v --verbose\t\t\tPrint verbose debug statements\n" + " -l --list\t\t\tList currently attached DFU capable devices\n"); + fprintf(stderr, " -e --detach\t\t\tDetach currently attached DFU capable devices\n" + " -E --detach-delay seconds\tTime to wait before reopening a device after detach\n" + " -d --device :[,:]\n" + "\t\t\t\tSpecify Vendor/Product ID(s) of DFU device\n" + " -p --path \tSpecify path to DFU device\n" + " -c --cfg \t\tSpecify the Configuration of DFU device\n" + " -i --intf \t\tSpecify the DFU Interface number\n" + " -S --serial [,]\n" + "\t\t\t\tSpecify Serial String of DFU device\n" + " -a --alt \t\tSpecify the Altsetting of the DFU Interface\n" + "\t\t\t\tby name or by number\n"); + fprintf(stderr, " -t --transfer-size \tSpecify the number of bytes per USB Transfer\n" + " -U --upload \t\tRead firmware from device into \n" + " -Z --upload-size \tSpecify the expected upload size in bytes\n" + " -D --download \t\tWrite firmware from into device\n" + " -R --reset\t\t\tIssue USB Reset signalling once we're finished\n" + " -s --dfuse-address
\tST DfuSe mode, specify target address for\n" + "\t\t\t\traw file download or upload. Not applicable for\n" + "\t\t\t\tDfuSe file (.dfu) downloads\n" + ); + exit(EX_USAGE); +} + +static void print_version(void) +{ + printf(PACKAGE_STRING "\n\n"); + printf("Copyright 2005-2009 Weston Schmidt, Harald Welte and OpenMoko Inc.\n" + "Copyright 2010-2014 Tormod Volden and Stefan Schmidt\n" + "This program is Free Software and has ABSOLUTELY NO WARRANTY\n" + "Please report bugs to " PACKAGE_BUGREPORT "\n\n"); +} + +static struct option opts[] = { + { "help", 0, 0, 'h' }, + { "version", 0, 0, 'V' }, + { "verbose", 0, 0, 'v' }, + { "list", 0, 0, 'l' }, + { "detach", 0, 0, 'e' }, + { "detach-delay", 1, 0, 'E' }, + { "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' }, + { "serial", 1, 0, 'S' }, + { "transfer-size", 1, 0, 't' }, + { "upload", 1, 0, 'U' }, + { "upload-size", 1, 0, 'Z' }, + { "download", 1, 0, 'D' }, + { "reset", 0, 0, 'R' }, + { "dfuse-address", 1, 0, 's' }, + { 0, 0, 0, 0 } +}; + +int main(int argc, char **argv) +{ + int expected_size = 0; + unsigned int transfer_size = 0; + enum mode mode = MODE_NONE; + struct dfu_status status; + libusb_context *ctx; + struct dfu_file file; + char *end; + int final_reset = 0; + int ret; + int dfuse_device = 0; + int fd; + const char *dfuse_options = NULL; + int detach_delay = 5; + uint16_t runtime_vendor; + uint16_t runtime_product; + + memset(&file, 0, sizeof(file)); + + /* make sure all prints are flushed */ + setvbuf(stdout, NULL, _IONBF, 0); + + while (1) { + int c, option_index = 0; + c = getopt_long(argc, argv, "hVvleE:d:p:c:i:a:S:t:U:D:Rs:Z:", opts, + &option_index); + if (c == -1) + break; + + switch (c) { + case 'h': + help(); + break; + case 'V': + mode = MODE_VERSION; + break; + case 'v': + verbose++; + break; + case 'l': + mode = MODE_LIST; + break; + case 'e': + mode = MODE_DETACH; + match_iface_alt_index = 0; + match_iface_index = 0; + break; + case 'E': + detach_delay = atoi(optarg); + break; + case 'd': + parse_vendprod(optarg); + break; + case 'p': + /* Parse device path */ + ret = resolve_device_path(optarg); + if (ret < 0) + errx(EX_SOFTWARE, "Unable to parse '%s'", optarg); + if (!ret) + errx(EX_SOFTWARE, "Cannot find '%s'", optarg); + break; + case 'c': + /* Configuration */ + match_config_index = atoi(optarg); + break; + case 'i': + /* Interface */ + match_iface_index = atoi(optarg); + break; + case 'a': + /* Interface Alternate Setting */ + match_iface_alt_index = strtoul(optarg, &end, 0); + if (*end) { + match_iface_alt_name = optarg; + match_iface_alt_index = -1; + } + break; + case 'S': + parse_serial(optarg); + break; + case 't': + transfer_size = atoi(optarg); + break; + case 'U': + mode = MODE_UPLOAD; + file.name = optarg; + break; + case 'Z': + expected_size = atoi(optarg); + break; + case 'D': + mode = MODE_DOWNLOAD; + file.name = optarg; + break; + case 'R': + final_reset = 1; + break; + case 's': + dfuse_options = optarg; + break; + default: + help(); + break; + } + } + + print_version(); + if (mode == MODE_VERSION) { + exit(0); + } + + if (mode == MODE_NONE) { + fprintf(stderr, "You need to specify one of -D or -U\n"); + help(); + } + + if (match_config_index == 0) { + /* Handle "-c 0" (unconfigured device) as don't care */ + match_config_index = -1; + } + + if (mode == MODE_DOWNLOAD) { + dfu_load_file(&file, MAYBE_SUFFIX, MAYBE_PREFIX); + /* If the user didn't specify product and/or vendor IDs to match, + * use any IDs from the file suffix for device matching */ + if (match_vendor < 0 && file.idVendor != 0xffff) { + match_vendor = file.idVendor; + printf("Match vendor ID from file: %04x\n", match_vendor); + } + if (match_product < 0 && file.idProduct != 0xffff) { + match_product = file.idProduct; + printf("Match product ID from file: %04x\n", match_product); + } + } + + ret = libusb_init(&ctx); + if (ret) + errx(EX_IOERR, "unable to initialize libusb: %i", ret); + + if (verbose > 2) { + libusb_set_debug(ctx, 255); + } + + probe_devices(ctx); + + if (mode == MODE_LIST) { + list_dfu_interfaces(); + exit(0); + } + + if (dfu_root == NULL) { + errx(EX_IOERR, "No DFU capable USB device available"); + } else if (dfu_root->next != NULL) { + /* 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 */ + errx(EX_IOERR, "More than one DFU capable USB device found! " + "Try `--list' and specify the serial number " + "or disconnect all but one device\n"); + } + + /* We have exactly one device. Its libusb_device is now in dfu_root->dev */ + + printf("Opening DFU capable USB device...\n"); + ret = libusb_open(dfu_root->dev, &dfu_root->dev_handle); + if (ret || !dfu_root->dev_handle) + errx(EX_IOERR, "Cannot open device"); + + printf("ID %04x:%04x\n", dfu_root->vendor, dfu_root->product); + + printf("Run-time device DFU version %04x\n", + libusb_le16_to_cpu(dfu_root->func_dfu.bcdDFUVersion)); + + /* Transition from run-Time mode to DFU mode */ + if (!(dfu_root->flags & DFU_IFF_DFU)) { + int err; + /* 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 */ + + runtime_vendor = dfu_root->vendor; + runtime_product = dfu_root->product; + + printf("Claiming USB DFU Runtime Interface...\n"); + if (libusb_claim_interface(dfu_root->dev_handle, dfu_root->interface) < 0) { + errx(EX_IOERR, "Cannot claim interface %d", + dfu_root->interface); + } + + if (libusb_set_interface_alt_setting(dfu_root->dev_handle, dfu_root->interface, 0) < 0) { + errx(EX_IOERR, "Cannot set alt interface zero"); + } + + printf("Determining device status: "); + + err = dfu_get_status(dfu_root, &status); + if (err == LIBUSB_ERROR_PIPE) { + printf("Device does not implement get_status, assuming appIDLE\n"); + status.bStatus = DFU_STATUS_OK; + status.bwPollTimeout = 0; + status.bState = DFU_STATE_appIDLE; + status.iString = 0; + } else if (err < 0) { + errx(EX_IOERR, "error get_status"); + } else { + printf("state = %s, status = %d\n", + dfu_state_to_string(status.bState), status.bStatus); + } + milli_sleep(status.bwPollTimeout); + + 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(dfu_root->dev_handle, + dfu_root->interface, 1000) < 0) { + warnx("error detaching"); + } + if (dfu_root->func_dfu.bmAttributes & USB_DFU_WILL_DETACH) { + printf("Device will detach and reattach...\n"); + } else { + printf("Resetting USB...\n"); + ret = libusb_reset_device(dfu_root->dev_handle); + if (ret < 0 && ret != LIBUSB_ERROR_NOT_FOUND) + errx(EX_IOERR, "error resetting " + "after detach"); + } + break; + case DFU_STATE_dfuERROR: + printf("dfuERROR, clearing status\n"); + if (dfu_clear_status(dfu_root->dev_handle, + dfu_root->interface) < 0) { + errx(EX_IOERR, "error clear_status"); + } + /* fall through */ + default: + warnx("WARNING: Runtime device already in DFU state ?!?"); + libusb_release_interface(dfu_root->dev_handle, + dfu_root->interface); + goto dfustate; + } + libusb_release_interface(dfu_root->dev_handle, + dfu_root->interface); + libusb_close(dfu_root->dev_handle); + dfu_root->dev_handle = NULL; + + if (mode == MODE_DETACH) { + libusb_exit(ctx); + exit(0); + } + + /* keeping handles open might prevent re-enumeration */ + disconnect_devices(); + + milli_sleep(detach_delay * 1000); + + /* Change match vendor and product to impossible values to force + * only DFU mode matches in the following probe */ + match_vendor = match_product = 0x10000; + + probe_devices(ctx); + + if (dfu_root == NULL) { + errx(EX_IOERR, "Lost device after RESET?"); + } else if (dfu_root->next != NULL) { + errx(EX_IOERR, "More than one DFU capable USB device found! " + "Try `--list' and specify the serial number " + "or disconnect all but one device"); + } + + /* Check for DFU mode device */ + if (!(dfu_root->flags | DFU_IFF_DFU)) + errx(EX_SOFTWARE, "Device is not in DFU mode"); + + printf("Opening DFU USB Device...\n"); + ret = libusb_open(dfu_root->dev, &dfu_root->dev_handle); + if (ret || !dfu_root->dev_handle) { + errx(EX_IOERR, "Cannot open device"); + } + } else { + /* we're already in DFU mode, so we can skip the detach/reset + * procedure */ + /* If a match vendor/product was specified, use that as the runtime + * vendor/product, otherwise use the DFU mode vendor/product */ + runtime_vendor = match_vendor < 0 ? dfu_root->vendor : match_vendor; + runtime_product = match_product < 0 ? dfu_root->product : match_product; + } + +dfustate: +#if 0 + printf("Setting Configuration %u...\n", dfu_root->configuration); + if (libusb_set_configuration(dfu_root->dev_handle, dfu_root->configuration) < 0) { + errx(EX_IOERR, "Cannot set configuration"); + } +#endif + printf("Claiming USB DFU Interface...\n"); + if (libusb_claim_interface(dfu_root->dev_handle, dfu_root->interface) < 0) { + errx(EX_IOERR, "Cannot claim interface"); + } + + printf("Setting Alternate Setting #%d ...\n", dfu_root->altsetting); + if (libusb_set_interface_alt_setting(dfu_root->dev_handle, dfu_root->interface, dfu_root->altsetting) < 0) { + errx(EX_IOERR, "Cannot set alternate interface"); + } + +status_again: + printf("Determining device status: "); + if (dfu_get_status(dfu_root, &status ) < 0) { + errx(EX_IOERR, "error get_status"); + } + printf("state = %s, status = %d\n", + dfu_state_to_string(status.bState), status.bStatus); + + milli_sleep(status.bwPollTimeout); + + switch (status.bState) { + case DFU_STATE_appIDLE: + case DFU_STATE_appDETACH: + errx(EX_IOERR, "Device still in Runtime Mode!"); + break; + case DFU_STATE_dfuERROR: + printf("dfuERROR, clearing status\n"); + if (dfu_clear_status(dfu_root->dev_handle, dfu_root->interface) < 0) { + errx(EX_IOERR, "error clear_status"); + } + goto status_again; + break; + case DFU_STATE_dfuDNLOAD_IDLE: + case DFU_STATE_dfuUPLOAD_IDLE: + printf("aborting previous incomplete transfer\n"); + if (dfu_abort(dfu_root->dev_handle, dfu_root->interface) < 0) { + errx(EX_IOERR, "can't send DFU_ABORT"); + } + goto status_again; + break; + case DFU_STATE_dfuIDLE: + printf("dfuIDLE, continuing\n"); + break; + default: + break; + } + + if (DFU_STATUS_OK != status.bStatus ) { + printf("WARNING: DFU Status: '%s'\n", + dfu_status_to_string(status.bStatus)); + /* Clear our status & try again. */ + if (dfu_clear_status(dfu_root->dev_handle, dfu_root->interface) < 0) + errx(EX_IOERR, "USB communication error"); + if (dfu_get_status(dfu_root, &status) < 0) + errx(EX_IOERR, "USB communication error"); + if (DFU_STATUS_OK != status.bStatus) + errx(EX_SOFTWARE, "Status is not OK: %d", status.bStatus); + + milli_sleep(status.bwPollTimeout); + } + + printf("DFU mode device DFU version %04x\n", + libusb_le16_to_cpu(dfu_root->func_dfu.bcdDFUVersion)); + + if (dfu_root->func_dfu.bcdDFUVersion == libusb_cpu_to_le16(0x11a)) + dfuse_device = 1; + + /* If not overridden by the user */ + if (!transfer_size) { + transfer_size = libusb_le16_to_cpu( + dfu_root->func_dfu.wTransferSize); + if (transfer_size) { + printf("Device returned transfer size %i\n", + transfer_size); + } else { + errx(EX_IOERR, "Transfer size must be specified"); + } + } + +#ifdef HAVE_GETPAGESIZE +/* autotools lie when cross-compiling for Windows using mingw32/64 */ +#ifndef __MINGW32__ + /* limitation of Linux usbdevio */ + if ((int)transfer_size > getpagesize()) { + transfer_size = getpagesize(); + printf("Limited transfer size to %i\n", transfer_size); + } +#endif /* __MINGW32__ */ +#endif /* HAVE_GETPAGESIZE */ + + if (transfer_size < dfu_root->bMaxPacketSize0) { + transfer_size = dfu_root->bMaxPacketSize0; + printf("Adjusted transfer size to %i\n", transfer_size); + } + + switch (mode) { + case MODE_UPLOAD: + /* open for "exclusive" writing */ + fd = open(file.name, O_WRONLY | O_BINARY | O_CREAT | O_EXCL | O_TRUNC, 0666); + if (fd < 0) + err(EX_IOERR, "Cannot open file %s for writing", file.name); + + if (dfuse_device || dfuse_options) { + if (dfuse_do_upload(dfu_root, transfer_size, fd, + dfuse_options) < 0) + exit(1); + } else { + if (dfuload_do_upload(dfu_root, transfer_size, + expected_size, fd) < 0) { + exit(1); + } + } + close(fd); + break; + + case MODE_DOWNLOAD: + if (((file.idVendor != 0xffff && file.idVendor != runtime_vendor) || + (file.idProduct != 0xffff && file.idProduct != runtime_product)) && + ((file.idVendor != 0xffff && file.idVendor != dfu_root->vendor) || + (file.idProduct != 0xffff && file.idProduct != dfu_root->product))) { + errx(EX_IOERR, "Error: File ID %04x:%04x does " + "not match device (%04x:%04x or %04x:%04x)", + file.idVendor, file.idProduct, + runtime_vendor, runtime_product, + dfu_root->vendor, dfu_root->product); + } + if (dfuse_device || dfuse_options || file.bcdDFU == 0x11a) { + if (dfuse_do_dnload(dfu_root, transfer_size, &file, + dfuse_options) < 0) + exit(1); + } else { + if (dfuload_do_dnload(dfu_root, transfer_size, &file) < 0) + exit(1); + } + break; + case MODE_DETACH: + if (dfu_detach(dfu_root->dev_handle, dfu_root->interface, 1000) < 0) { + warnx("can't detach"); + } + break; + default: + errx(EX_IOERR, "Unsupported mode: %u", mode); + break; + } + + if (final_reset) { + if (dfu_detach(dfu_root->dev_handle, dfu_root->interface, 1000) < 0) { + /* Even if detach failed, just carry on to leave the + device in a known state */ + warnx("can't detach"); + } + printf("Resetting USB to switch back to runtime mode\n"); + ret = libusb_reset_device(dfu_root->dev_handle); + if (ret < 0 && ret != LIBUSB_ERROR_NOT_FOUND) { + errx(EX_IOERR, "error resetting after download"); + } + } + + libusb_close(dfu_root->dev_handle); + dfu_root->dev_handle = NULL; + libusb_exit(ctx); + + return (0); +} diff --git a/tools/src/dfu-util/src/portable.h b/tools/linux/src/dfu-util/src/portable.h old mode 100755 new mode 100644 similarity index 95% rename from tools/src/dfu-util/src/portable.h rename to tools/linux/src/dfu-util/src/portable.h index 2609232..cf8d5df --- a/tools/src/dfu-util/src/portable.h +++ b/tools/linux/src/dfu-util/src/portable.h @@ -1,72 +1,72 @@ - -#ifndef PORTABLE_H -#define PORTABLE_H - -#ifdef HAVE_CONFIG_H -# include "config.h" -#else -# define PACKAGE "dfu-util" -# define PACKAGE_VERSION "0.8-msvc" -# define PACKAGE_STRING "dfu-util 0.8-msvc" -# define PACKAGE_BUGREPORT "dfu-util@lists.gnumonks.org" -#endif /* HAVE_CONFIG_H */ - -#ifdef HAVE_FTRUNCATE -# include -#else -# include -#endif /* HAVE_FTRUNCATE */ - -#ifdef HAVE_NANOSLEEP -# include -# define milli_sleep(msec) do {\ - if (msec) {\ - struct timespec nanosleepDelay = { (msec) / 1000, ((msec) % 1000) * 1000000 };\ - nanosleep(&nanosleepDelay, NULL);\ - } } while (0) -#elif defined HAVE_WINDOWS_H -# define milli_sleep(msec) do {\ - if (msec) {\ - Sleep(msec);\ - } } while (0) -#else -# error "Can't get no sleep! Please report" -#endif /* HAVE_NANOSLEEP */ - -#ifdef HAVE_ERR -# include -#else -# include -# include -# define warnx(...) do {\ - fprintf(stderr, __VA_ARGS__);\ - fprintf(stderr, "\n"); } while (0) -# define errx(eval, ...) do {\ - warnx(__VA_ARGS__);\ - exit(eval); } while (0) -# define warn(...) do {\ - fprintf(stderr, "%s: ", strerror(errno));\ - warnx(__VA_ARGS__); } while (0) -# define err(eval, ...) do {\ - warn(__VA_ARGS__);\ - exit(eval); } while (0) -#endif /* HAVE_ERR */ - -#ifdef HAVE_SYSEXITS_H -# include -#else -# define EX_OK 0 /* successful termination */ -# define EX_USAGE 64 /* command line usage error */ -# define EX_SOFTWARE 70 /* internal software error */ -# define EX_IOERR 74 /* input/output error */ -#endif /* HAVE_SYSEXITS_H */ - -#ifndef O_BINARY -# define O_BINARY 0 -#endif - -#ifndef off_t -# define off_t long int -#endif - -#endif /* PORTABLE_H */ + +#ifndef PORTABLE_H +#define PORTABLE_H + +#ifdef HAVE_CONFIG_H +# include "config.h" +#else +# define PACKAGE "dfu-util" +# define PACKAGE_VERSION "0.8-msvc" +# define PACKAGE_STRING "dfu-util 0.8-msvc" +# define PACKAGE_BUGREPORT "dfu-util@lists.gnumonks.org" +#endif /* HAVE_CONFIG_H */ + +#ifdef HAVE_FTRUNCATE +# include +#else +# include +#endif /* HAVE_FTRUNCATE */ + +#ifdef HAVE_NANOSLEEP +# include +# define milli_sleep(msec) do {\ + if (msec) {\ + struct timespec nanosleepDelay = { (msec) / 1000, ((msec) % 1000) * 1000000 };\ + nanosleep(&nanosleepDelay, NULL);\ + } } while (0) +#elif defined HAVE_WINDOWS_H +# define milli_sleep(msec) do {\ + if (msec) {\ + Sleep(msec);\ + } } while (0) +#else +# error "Can't get no sleep! Please report" +#endif /* HAVE_NANOSLEEP */ + +#ifdef HAVE_ERR +# include +#else +# include +# include +# define warnx(...) do {\ + fprintf(stderr, __VA_ARGS__);\ + fprintf(stderr, "\n"); } while (0) +# define errx(eval, ...) do {\ + warnx(__VA_ARGS__);\ + exit(eval); } while (0) +# define warn(...) do {\ + fprintf(stderr, "%s: ", strerror(errno));\ + warnx(__VA_ARGS__); } while (0) +# define err(eval, ...) do {\ + warn(__VA_ARGS__);\ + exit(eval); } while (0) +#endif /* HAVE_ERR */ + +#ifdef HAVE_SYSEXITS_H +# include +#else +# define EX_OK 0 /* successful termination */ +# define EX_USAGE 64 /* command line usage error */ +# define EX_SOFTWARE 70 /* internal software error */ +# define EX_IOERR 74 /* input/output error */ +#endif /* HAVE_SYSEXITS_H */ + +#ifndef O_BINARY +# define O_BINARY 0 +#endif + +#ifndef off_t +# define off_t long int +#endif + +#endif /* PORTABLE_H */ diff --git a/tools/src/dfu-util/src/prefix.c b/tools/linux/src/dfu-util/src/prefix.c old mode 100755 new mode 100644 similarity index 96% rename from tools/src/dfu-util/src/prefix.c rename to tools/linux/src/dfu-util/src/prefix.c index 05692f9..be8e3fa --- a/tools/src/dfu-util/src/prefix.c +++ b/tools/linux/src/dfu-util/src/prefix.c @@ -1,176 +1,176 @@ -/* - * dfu-prefix - * - * Copyright 2011-2012 Stefan Schmidt - * Copyright 2013 Hans Petter Selasky - * Copyright 2014 Uwe Bonnes - * - * 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 -#include -#include -#include -#include - -#include "portable.h" -#include "dfu_file.h" - -enum mode { - MODE_NONE, - MODE_ADD, - MODE_DEL, - MODE_CHECK -}; - -int verbose; - -static void help(void) -{ - fprintf(stderr, "Usage: dfu-prefix [options] ...\n" - " -h --help\t\t\tPrint this help message\n" - " -V --version\t\t\tPrint the version number\n" - " -c --check \t\tCheck DFU prefix of \n" - " -D --delete \t\tDelete DFU prefix from \n" - " -a --add \t\tAdd DFU prefix to \n" - "In combination with -a:\n" - ); - fprintf(stderr, " -s --stellaris-address
Add TI Stellaris address prefix to \n" - "In combination with -D or -c:\n" - " -T --stellaris\t\tAct on TI Stellaris address prefix of \n" - "In combination with -a or -D or -c:\n" - " -L --lpc-prefix\t\tUse NXP LPC DFU prefix format\n" - ); - exit(EX_USAGE); -} - -static void print_version(void) -{ - printf("dfu-prefix (%s) %s\n\n", PACKAGE, PACKAGE_VERSION); - printf("Copyright 2011-2012 Stefan Schmidt, 2014 Uwe Bonnes\n" - "This program is Free Software and has ABSOLUTELY NO WARRANTY\n" - "Please report bugs to %s\n\n", PACKAGE_BUGREPORT); - -} - -static struct option opts[] = { - { "help", 0, 0, 'h' }, - { "version", 0, 0, 'V' }, - { "check", 1, 0, 'c' }, - { "add", 1, 0, 'a' }, - { "delete", 1, 0, 'D' }, - { "stellaris-address", 1, 0, 's' }, - { "stellaris", 0, 0, 'T' }, - { "LPC", 0, 0, 'L' }, -}; -int main(int argc, char **argv) -{ - struct dfu_file file; - enum mode mode = MODE_NONE; - enum prefix_type type = ZERO_PREFIX; - uint32_t lmdfu_flash_address = 0; - char *end; - - /* make sure all prints are flushed */ - setvbuf(stdout, NULL, _IONBF, 0); - - print_version(); - - memset(&file, 0, sizeof(file)); - - while (1) { - int c, option_index = 0; - c = getopt_long(argc, argv, "hVc:a:D:p:v:d:s:TL", opts, - &option_index); - if (c == -1) - break; - - switch (c) { - case 'h': - help(); - break; - case 'V': - exit(0); - break; - case 'D': - file.name = optarg; - mode = MODE_DEL; - break; - case 'c': - file.name = optarg; - mode = MODE_CHECK; - break; - case 'a': - file.name = optarg; - mode = MODE_ADD; - break; - case 's': - lmdfu_flash_address = strtoul(optarg, &end, 0); - if (*end) { - errx(EX_IOERR, "Invalid lmdfu " - "address: %s", optarg); - } - /* fall-through */ - case 'T': - type = LMDFU_PREFIX; - break; - case 'L': - type = LPCDFU_UNENCRYPTED_PREFIX; - break; - default: - help(); - break; - } - } - - if (!file.name) { - fprintf(stderr, "You need to specify a filename\n"); - help(); - } - - switch(mode) { - case MODE_ADD: - if (type == ZERO_PREFIX) - errx(EX_IOERR, "Prefix type must be specified"); - dfu_load_file(&file, MAYBE_SUFFIX, NO_PREFIX); - file.lmdfu_address = lmdfu_flash_address; - file.prefix_type = type; - printf("Adding prefix to file\n"); - dfu_store_file(&file, file.size.suffix != 0, 1); - break; - - case MODE_CHECK: - dfu_load_file(&file, MAYBE_SUFFIX, MAYBE_PREFIX); - show_suffix_and_prefix(&file); - if (type > ZERO_PREFIX && file.prefix_type != type) - errx(EX_IOERR, "No prefix of requested type"); - break; - - case MODE_DEL: - dfu_load_file(&file, MAYBE_SUFFIX, NEEDS_PREFIX); - if (type > ZERO_PREFIX && file.prefix_type != type) - errx(EX_IOERR, "No prefix of requested type"); - printf("Removing prefix from file\n"); - /* if there was a suffix, rewrite it */ - dfu_store_file(&file, file.size.suffix != 0, 0); - break; - - default: - help(); - break; - } - return (0); -} +/* + * dfu-prefix + * + * Copyright 2011-2012 Stefan Schmidt + * Copyright 2013 Hans Petter Selasky + * Copyright 2014 Uwe Bonnes + * + * 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 +#include +#include +#include +#include + +#include "portable.h" +#include "dfu_file.h" + +enum mode { + MODE_NONE, + MODE_ADD, + MODE_DEL, + MODE_CHECK +}; + +int verbose; + +static void help(void) +{ + fprintf(stderr, "Usage: dfu-prefix [options] ...\n" + " -h --help\t\t\tPrint this help message\n" + " -V --version\t\t\tPrint the version number\n" + " -c --check \t\tCheck DFU prefix of \n" + " -D --delete \t\tDelete DFU prefix from \n" + " -a --add \t\tAdd DFU prefix to \n" + "In combination with -a:\n" + ); + fprintf(stderr, " -s --stellaris-address
Add TI Stellaris address prefix to \n" + "In combination with -D or -c:\n" + " -T --stellaris\t\tAct on TI Stellaris address prefix of \n" + "In combination with -a or -D or -c:\n" + " -L --lpc-prefix\t\tUse NXP LPC DFU prefix format\n" + ); + exit(EX_USAGE); +} + +static void print_version(void) +{ + printf("dfu-prefix (%s) %s\n\n", PACKAGE, PACKAGE_VERSION); + printf("Copyright 2011-2012 Stefan Schmidt, 2014 Uwe Bonnes\n" + "This program is Free Software and has ABSOLUTELY NO WARRANTY\n" + "Please report bugs to %s\n\n", PACKAGE_BUGREPORT); + +} + +static struct option opts[] = { + { "help", 0, 0, 'h' }, + { "version", 0, 0, 'V' }, + { "check", 1, 0, 'c' }, + { "add", 1, 0, 'a' }, + { "delete", 1, 0, 'D' }, + { "stellaris-address", 1, 0, 's' }, + { "stellaris", 0, 0, 'T' }, + { "LPC", 0, 0, 'L' }, +}; +int main(int argc, char **argv) +{ + struct dfu_file file; + enum mode mode = MODE_NONE; + enum prefix_type type = ZERO_PREFIX; + uint32_t lmdfu_flash_address = 0; + char *end; + + /* make sure all prints are flushed */ + setvbuf(stdout, NULL, _IONBF, 0); + + print_version(); + + memset(&file, 0, sizeof(file)); + + while (1) { + int c, option_index = 0; + c = getopt_long(argc, argv, "hVc:a:D:p:v:d:s:TL", opts, + &option_index); + if (c == -1) + break; + + switch (c) { + case 'h': + help(); + break; + case 'V': + exit(0); + break; + case 'D': + file.name = optarg; + mode = MODE_DEL; + break; + case 'c': + file.name = optarg; + mode = MODE_CHECK; + break; + case 'a': + file.name = optarg; + mode = MODE_ADD; + break; + case 's': + lmdfu_flash_address = strtoul(optarg, &end, 0); + if (*end) { + errx(EX_IOERR, "Invalid lmdfu " + "address: %s", optarg); + } + /* fall-through */ + case 'T': + type = LMDFU_PREFIX; + break; + case 'L': + type = LPCDFU_UNENCRYPTED_PREFIX; + break; + default: + help(); + break; + } + } + + if (!file.name) { + fprintf(stderr, "You need to specify a filename\n"); + help(); + } + + switch(mode) { + case MODE_ADD: + if (type == ZERO_PREFIX) + errx(EX_IOERR, "Prefix type must be specified"); + dfu_load_file(&file, MAYBE_SUFFIX, NO_PREFIX); + file.lmdfu_address = lmdfu_flash_address; + file.prefix_type = type; + printf("Adding prefix to file\n"); + dfu_store_file(&file, file.size.suffix != 0, 1); + break; + + case MODE_CHECK: + dfu_load_file(&file, MAYBE_SUFFIX, MAYBE_PREFIX); + show_suffix_and_prefix(&file); + if (type > ZERO_PREFIX && file.prefix_type != type) + errx(EX_IOERR, "No prefix of requested type"); + break; + + case MODE_DEL: + dfu_load_file(&file, MAYBE_SUFFIX, NEEDS_PREFIX); + if (type > ZERO_PREFIX && file.prefix_type != type) + errx(EX_IOERR, "No prefix of requested type"); + printf("Removing prefix from file\n"); + /* if there was a suffix, rewrite it */ + dfu_store_file(&file, file.size.suffix != 0, 0); + break; + + default: + help(); + break; + } + return (0); +} diff --git a/tools/src/dfu-util/src/quirks.c b/tools/linux/src/dfu-util/src/quirks.c old mode 100755 new mode 100644 similarity index 97% rename from tools/src/dfu-util/src/quirks.c rename to tools/linux/src/dfu-util/src/quirks.c index c90e585..de394a6 --- a/tools/src/dfu-util/src/quirks.c +++ b/tools/linux/src/dfu-util/src/quirks.c @@ -1,56 +1,56 @@ -/* - * Simple quirk system for dfu-util - * - * Copyright 2010-2014 Tormod Volden - * - * 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 -#include "quirks.h" - -uint16_t get_quirks(uint16_t vendor, uint16_t product, uint16_t bcdDevice) -{ - uint16_t quirks = 0; - - /* Device returns bogus bwPollTimeout values */ - if ((vendor == VENDOR_OPENMOKO || vendor == VENDOR_FIC) && - product >= PRODUCT_FREERUNNER_FIRST && - product <= PRODUCT_FREERUNNER_LAST) - quirks |= QUIRK_POLLTIMEOUT; - - if (vendor == VENDOR_VOTI && - product == PRODUCT_OPENPCD) - quirks |= QUIRK_POLLTIMEOUT; - - /* Reports wrong DFU version in DFU descriptor */ - if (vendor == VENDOR_LEAFLABS && - product == PRODUCT_MAPLE3 && - bcdDevice == 0x0200) - quirks |= QUIRK_FORCE_DFU11; - - /* old devices(bcdDevice == 0) return bogus bwPollTimeout values */ - if (vendor == VENDOR_SIEMENS && - (product == PRODUCT_PXM40 || product == PRODUCT_PXM50) && - bcdDevice == 0) - quirks |= QUIRK_POLLTIMEOUT; - - /* M-Audio Transit returns bogus bwPollTimeout values */ - if (vendor == VENDOR_MIDIMAN && - product == PRODUCT_TRANSIT) - quirks |= QUIRK_POLLTIMEOUT; - - return (quirks); -} +/* + * Simple quirk system for dfu-util + * + * Copyright 2010-2014 Tormod Volden + * + * 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 +#include "quirks.h" + +uint16_t get_quirks(uint16_t vendor, uint16_t product, uint16_t bcdDevice) +{ + uint16_t quirks = 0; + + /* Device returns bogus bwPollTimeout values */ + if ((vendor == VENDOR_OPENMOKO || vendor == VENDOR_FIC) && + product >= PRODUCT_FREERUNNER_FIRST && + product <= PRODUCT_FREERUNNER_LAST) + quirks |= QUIRK_POLLTIMEOUT; + + if (vendor == VENDOR_VOTI && + product == PRODUCT_OPENPCD) + quirks |= QUIRK_POLLTIMEOUT; + + /* Reports wrong DFU version in DFU descriptor */ + if (vendor == VENDOR_LEAFLABS && + product == PRODUCT_MAPLE3 && + bcdDevice == 0x0200) + quirks |= QUIRK_FORCE_DFU11; + + /* old devices(bcdDevice == 0) return bogus bwPollTimeout values */ + if (vendor == VENDOR_SIEMENS && + (product == PRODUCT_PXM40 || product == PRODUCT_PXM50) && + bcdDevice == 0) + quirks |= QUIRK_POLLTIMEOUT; + + /* M-Audio Transit returns bogus bwPollTimeout values */ + if (vendor == VENDOR_MIDIMAN && + product == PRODUCT_TRANSIT) + quirks |= QUIRK_POLLTIMEOUT; + + return (quirks); +} diff --git a/tools/src/dfu-util/src/quirks.h b/tools/linux/src/dfu-util/src/quirks.h old mode 100755 new mode 100644 similarity index 97% rename from tools/src/dfu-util/src/quirks.h rename to tools/linux/src/dfu-util/src/quirks.h index 3193f25..0e4b3ec --- a/tools/src/dfu-util/src/quirks.h +++ b/tools/linux/src/dfu-util/src/quirks.h @@ -1,27 +1,27 @@ -#ifndef DFU_QUIRKS_H -#define DFU_QUIRKS_H - -#define VENDOR_OPENMOKO 0x1d50 /* Openmoko Freerunner / GTA02 */ -#define VENDOR_FIC 0x1457 /* Openmoko Freerunner / GTA02 */ -#define VENDOR_VOTI 0x16c0 /* OpenPCD Reader */ -#define VENDOR_LEAFLABS 0x1eaf /* Maple */ -#define VENDOR_SIEMENS 0x0908 /* Siemens AG */ -#define VENDOR_MIDIMAN 0x0763 /* Midiman */ - -#define PRODUCT_FREERUNNER_FIRST 0x5117 -#define PRODUCT_FREERUNNER_LAST 0x5126 -#define PRODUCT_OPENPCD 0x076b -#define PRODUCT_MAPLE3 0x0003 /* rev 3 and 5 */ -#define PRODUCT_PXM40 0x02c4 /* Siemens AG, PXM 40 */ -#define PRODUCT_PXM50 0x02c5 /* Siemens AG, PXM 50 */ -#define PRODUCT_TRANSIT 0x2806 /* M-Audio Transit (Midiman) */ - -#define QUIRK_POLLTIMEOUT (1<<0) -#define QUIRK_FORCE_DFU11 (1<<1) - -/* Fallback value, works for OpenMoko */ -#define DEFAULT_POLLTIMEOUT 5 - -uint16_t get_quirks(uint16_t vendor, uint16_t product, uint16_t bcdDevice); - -#endif /* DFU_QUIRKS_H */ +#ifndef DFU_QUIRKS_H +#define DFU_QUIRKS_H + +#define VENDOR_OPENMOKO 0x1d50 /* Openmoko Freerunner / GTA02 */ +#define VENDOR_FIC 0x1457 /* Openmoko Freerunner / GTA02 */ +#define VENDOR_VOTI 0x16c0 /* OpenPCD Reader */ +#define VENDOR_LEAFLABS 0x1eaf /* Maple */ +#define VENDOR_SIEMENS 0x0908 /* Siemens AG */ +#define VENDOR_MIDIMAN 0x0763 /* Midiman */ + +#define PRODUCT_FREERUNNER_FIRST 0x5117 +#define PRODUCT_FREERUNNER_LAST 0x5126 +#define PRODUCT_OPENPCD 0x076b +#define PRODUCT_MAPLE3 0x0003 /* rev 3 and 5 */ +#define PRODUCT_PXM40 0x02c4 /* Siemens AG, PXM 40 */ +#define PRODUCT_PXM50 0x02c5 /* Siemens AG, PXM 50 */ +#define PRODUCT_TRANSIT 0x2806 /* M-Audio Transit (Midiman) */ + +#define QUIRK_POLLTIMEOUT (1<<0) +#define QUIRK_FORCE_DFU11 (1<<1) + +/* Fallback value, works for OpenMoko */ +#define DEFAULT_POLLTIMEOUT 5 + +uint16_t get_quirks(uint16_t vendor, uint16_t product, uint16_t bcdDevice); + +#endif /* DFU_QUIRKS_H */ diff --git a/tools/src/dfu-util/src/suffix.c b/tools/linux/src/dfu-util/src/suffix.c old mode 100755 new mode 100644 similarity index 96% rename from tools/src/dfu-util/src/suffix.c rename to tools/linux/src/dfu-util/src/suffix.c index ba02a01..0df248f --- a/tools/src/dfu-util/src/suffix.c +++ b/tools/linux/src/dfu-util/src/suffix.c @@ -1,176 +1,176 @@ -/* - * dfu-suffix - * - * Copyright 2011-2012 Stefan Schmidt - * Copyright 2013 Hans Petter Selasky - * Copyright 2014 Tormod Volden - * - * 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 -#include -#include -#include -#include - -#include "portable.h" -#include "dfu_file.h" - -enum mode { - MODE_NONE, - MODE_ADD, - MODE_DEL, - MODE_CHECK -}; - -int verbose; - -static void help(void) -{ - fprintf(stderr, "Usage: dfu-suffix [options] ...\n" - " -h --help\t\t\tPrint this help message\n" - " -V --version\t\t\tPrint the version number\n" - " -c --check \t\tCheck DFU suffix of \n" - " -a --add \t\tAdd DFU suffix to \n" - " -D --delete \t\tDelete DFU suffix from \n" - " -p --pid \t\tAdd product ID into DFU suffix in \n" - " -v --vid \t\tAdd vendor ID into DFU suffix in \n" - " -d --did \t\tAdd device ID into DFU suffix in \n" - " -S --spec \t\tAdd DFU specification ID into DFU suffix in \n" - ); - exit(EX_USAGE); -} - -static void print_version(void) -{ - printf("dfu-suffix (%s) %s\n\n", PACKAGE, PACKAGE_VERSION); - printf("Copyright 2011-2012 Stefan Schmidt, 2013-2014 Tormod Volden\n" - "This program is Free Software and has ABSOLUTELY NO WARRANTY\n" - "Please report bugs to %s\n\n", PACKAGE_BUGREPORT); - -} - -static struct option opts[] = { - { "help", 0, 0, 'h' }, - { "version", 0, 0, 'V' }, - { "check", 1, 0, 'c' }, - { "add", 1, 0, 'a' }, - { "delete", 1, 0, 'D' }, - { "pid", 1, 0, 'p' }, - { "vid", 1, 0, 'v' }, - { "did", 1, 0, 'd' }, - { "spec", 1, 0, 'S' }, -}; - -int main(int argc, char **argv) -{ - struct dfu_file file; - int pid, vid, did, spec; - enum mode mode = MODE_NONE; - - /* make sure all prints are flushed */ - setvbuf(stdout, NULL, _IONBF, 0); - - print_version(); - - pid = vid = did = 0xffff; - spec = 0x0100; /* Default to bcdDFU version 1.0 */ - memset(&file, 0, sizeof(file)); - - while (1) { - int c, option_index = 0; - c = getopt_long(argc, argv, "hVc:a:D:p:v:d:S:s:T", opts, - &option_index); - if (c == -1) - break; - - switch (c) { - case 'h': - help(); - break; - case 'V': - exit(0); - break; - case 'D': - file.name = optarg; - mode = MODE_DEL; - break; - case 'p': - pid = strtol(optarg, NULL, 16); - break; - case 'v': - vid = strtol(optarg, NULL, 16); - break; - case 'd': - did = strtol(optarg, NULL, 16); - break; - case 'S': - spec = strtol(optarg, NULL, 16); - break; - case 'c': - file.name = optarg; - mode = MODE_CHECK; - break; - case 'a': - file.name = optarg; - mode = MODE_ADD; - break; - default: - help(); - break; - } - } - - if (!file.name) { - fprintf(stderr, "You need to specify a filename\n"); - help(); - } - - if (spec != 0x0100 && spec != 0x011a) { - fprintf(stderr, "Only DFU specification 0x0100 and 0x011a supported\n"); - help(); - } - - switch(mode) { - case MODE_ADD: - dfu_load_file(&file, NO_SUFFIX, MAYBE_PREFIX); - file.idVendor = vid; - file.idProduct = pid; - file.bcdDevice = did; - file.bcdDFU = spec; - /* always write suffix, rewrite prefix if there was one */ - dfu_store_file(&file, 1, file.size.prefix != 0); - printf("Suffix successfully added to file\n"); - break; - - case MODE_CHECK: - dfu_load_file(&file, NEEDS_SUFFIX, MAYBE_PREFIX); - show_suffix_and_prefix(&file); - break; - - case MODE_DEL: - dfu_load_file(&file, NEEDS_SUFFIX, MAYBE_PREFIX); - dfu_store_file(&file, 0, file.size.prefix != 0); - if (file.size.suffix) /* had a suffix */ - printf("Suffix successfully removed from file\n"); - break; - - default: - help(); - break; - } - return (0); -} +/* + * dfu-suffix + * + * Copyright 2011-2012 Stefan Schmidt + * Copyright 2013 Hans Petter Selasky + * Copyright 2014 Tormod Volden + * + * 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 +#include +#include +#include +#include + +#include "portable.h" +#include "dfu_file.h" + +enum mode { + MODE_NONE, + MODE_ADD, + MODE_DEL, + MODE_CHECK +}; + +int verbose; + +static void help(void) +{ + fprintf(stderr, "Usage: dfu-suffix [options] ...\n" + " -h --help\t\t\tPrint this help message\n" + " -V --version\t\t\tPrint the version number\n" + " -c --check \t\tCheck DFU suffix of \n" + " -a --add \t\tAdd DFU suffix to \n" + " -D --delete \t\tDelete DFU suffix from \n" + " -p --pid \t\tAdd product ID into DFU suffix in \n" + " -v --vid \t\tAdd vendor ID into DFU suffix in \n" + " -d --did \t\tAdd device ID into DFU suffix in \n" + " -S --spec \t\tAdd DFU specification ID into DFU suffix in \n" + ); + exit(EX_USAGE); +} + +static void print_version(void) +{ + printf("dfu-suffix (%s) %s\n\n", PACKAGE, PACKAGE_VERSION); + printf("Copyright 2011-2012 Stefan Schmidt, 2013-2014 Tormod Volden\n" + "This program is Free Software and has ABSOLUTELY NO WARRANTY\n" + "Please report bugs to %s\n\n", PACKAGE_BUGREPORT); + +} + +static struct option opts[] = { + { "help", 0, 0, 'h' }, + { "version", 0, 0, 'V' }, + { "check", 1, 0, 'c' }, + { "add", 1, 0, 'a' }, + { "delete", 1, 0, 'D' }, + { "pid", 1, 0, 'p' }, + { "vid", 1, 0, 'v' }, + { "did", 1, 0, 'd' }, + { "spec", 1, 0, 'S' }, +}; + +int main(int argc, char **argv) +{ + struct dfu_file file; + int pid, vid, did, spec; + enum mode mode = MODE_NONE; + + /* make sure all prints are flushed */ + setvbuf(stdout, NULL, _IONBF, 0); + + print_version(); + + pid = vid = did = 0xffff; + spec = 0x0100; /* Default to bcdDFU version 1.0 */ + memset(&file, 0, sizeof(file)); + + while (1) { + int c, option_index = 0; + c = getopt_long(argc, argv, "hVc:a:D:p:v:d:S:s:T", opts, + &option_index); + if (c == -1) + break; + + switch (c) { + case 'h': + help(); + break; + case 'V': + exit(0); + break; + case 'D': + file.name = optarg; + mode = MODE_DEL; + break; + case 'p': + pid = strtol(optarg, NULL, 16); + break; + case 'v': + vid = strtol(optarg, NULL, 16); + break; + case 'd': + did = strtol(optarg, NULL, 16); + break; + case 'S': + spec = strtol(optarg, NULL, 16); + break; + case 'c': + file.name = optarg; + mode = MODE_CHECK; + break; + case 'a': + file.name = optarg; + mode = MODE_ADD; + break; + default: + help(); + break; + } + } + + if (!file.name) { + fprintf(stderr, "You need to specify a filename\n"); + help(); + } + + if (spec != 0x0100 && spec != 0x011a) { + fprintf(stderr, "Only DFU specification 0x0100 and 0x011a supported\n"); + help(); + } + + switch(mode) { + case MODE_ADD: + dfu_load_file(&file, NO_SUFFIX, MAYBE_PREFIX); + file.idVendor = vid; + file.idProduct = pid; + file.bcdDevice = did; + file.bcdDFU = spec; + /* always write suffix, rewrite prefix if there was one */ + dfu_store_file(&file, 1, file.size.prefix != 0); + printf("Suffix successfully added to file\n"); + break; + + case MODE_CHECK: + dfu_load_file(&file, NEEDS_SUFFIX, MAYBE_PREFIX); + show_suffix_and_prefix(&file); + break; + + case MODE_DEL: + dfu_load_file(&file, NEEDS_SUFFIX, MAYBE_PREFIX); + dfu_store_file(&file, 0, file.size.prefix != 0); + if (file.size.suffix) /* had a suffix */ + printf("Suffix successfully removed from file\n"); + break; + + default: + help(); + break; + } + return (0); +} diff --git a/tools/src/dfu-util/src/usb_dfu.h b/tools/linux/src/dfu-util/src/usb_dfu.h old mode 100755 new mode 100644 similarity index 96% rename from tools/src/dfu-util/src/usb_dfu.h rename to tools/linux/src/dfu-util/src/usb_dfu.h index f6fe270..660bedc --- a/tools/src/dfu-util/src/usb_dfu.h +++ b/tools/linux/src/dfu-util/src/usb_dfu.h @@ -1,99 +1,99 @@ -#ifndef USB_DFU_H -#define USB_DFU_H -/* USB Device Firmware Update Implementation for OpenPCD - * (C) 2006 by Harald Welte - * - * 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 - * - * 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 - -#define USB_DT_DFU 0x21 - -#ifdef _MSC_VER -# pragma pack(push) -# pragma pack(1) -#endif /* _MSC_VER */ -struct usb_dfu_func_descriptor { - uint8_t bLength; - uint8_t bDescriptorType; - uint8_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) - uint16_t wDetachTimeOut; - uint16_t wTransferSize; - uint16_t bcdDFUVersion; -#ifdef _MSC_VER -}; -# pragma pack(pop) -#elif defined __GNUC__ -} __attribute__ ((packed)); -#else - #warning "No way to pack struct on this compiler? This will break!" -#endif /* _MSC_VER */ - -#define USB_DT_DFU_SIZE 9 - -#define USB_TYPE_DFU (LIBUSB_REQUEST_TYPE_CLASS|LIBUSB_RECIPIENT_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 - -/* DFU_GETSTATUS bStatus values (Section 6.1.2, DFU Rev 1.1) */ -#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 */ +#ifndef USB_DFU_H +#define USB_DFU_H +/* USB Device Firmware Update Implementation for OpenPCD + * (C) 2006 by Harald Welte + * + * 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 + * + * 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 + +#define USB_DT_DFU 0x21 + +#ifdef _MSC_VER +# pragma pack(push) +# pragma pack(1) +#endif /* _MSC_VER */ +struct usb_dfu_func_descriptor { + uint8_t bLength; + uint8_t bDescriptorType; + uint8_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) + uint16_t wDetachTimeOut; + uint16_t wTransferSize; + uint16_t bcdDFUVersion; +#ifdef _MSC_VER +}; +# pragma pack(pop) +#elif defined __GNUC__ +} __attribute__ ((packed)); +#else + #warning "No way to pack struct on this compiler? This will break!" +#endif /* _MSC_VER */ + +#define USB_DT_DFU_SIZE 9 + +#define USB_TYPE_DFU (LIBUSB_REQUEST_TYPE_CLASS|LIBUSB_RECIPIENT_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 + +/* DFU_GETSTATUS bStatus values (Section 6.1.2, DFU Rev 1.1) */ +#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 */ diff --git a/tools/src/dfu-util/www/build.html b/tools/linux/src/dfu-util/www/build.html old mode 100755 new mode 100644 similarity index 96% rename from tools/src/dfu-util/www/build.html rename to tools/linux/src/dfu-util/www/build.html index 2e6de0c..f3036e4 --- a/tools/src/dfu-util/www/build.html +++ b/tools/linux/src/dfu-util/www/build.html @@ -1,147 +1,147 @@ - - - - - - - Building dfu-util from source - - - - - - - - - -
-

How to build dfu-util from source

- -

Prerequisites for building from git

-

Mac OS X

-

-First install MacPorts (and if you are on 10.6 or older, the Java Developer Package) and then run: -

-
-	sudo port install libusb-devel git-core
-
- -

FreeBSD

-
-	sudo pkg_add -r git pkgconf
-
- -

Ubuntu and Debian and derivatives

-
-	sudo apt-get build-dep dfu-util
-	sudo apt-get install libusb-1.0-0-dev
-
- -

Get the source code and build it

-

-The first time you will have to clone the git repository: -

-
-	git clone git://gitorious.org/dfu-util/dfu-util.git
-	cd dfu-util
-
-

-If you later want to update to latest git version, just run this: -

-
-	make maintainer-clean
-	git pull
-
-

-To build the source: -

-
-	./autogen.sh
-	./configure  # on most systems
-	make
-
- -

-If you are building on Mac OS X, replace the ./configure command with: -

-
-	./configure --libdir=/opt/local/lib --includedir=/opt/local/include  # on MacOSX only
-
- -

-Your dfu-util binary will be inside the src folder. Use it from there, or install it to /usr/local/bin by running "sudo make install". -

- -

Cross-building for Windows

- -

-Windows binaries can be built in a MinGW -environment, on a Windows computer or cross-hosted in another OS. -To build it on a Debian or Ubuntu host, first install build dependencies: -

-
-	sudo apt-get build-dep libusb-1.0-0 dfu-util
-	sudo apt-get install mingw32
-
- -

-The below example builds dfu-util 0.8 and libusb 1.0.19 from unpacked release -tarballs. If you instead build from git, you will have to run "./autogen.sh" -before running the "./configure" steps. -

- -
-mkdir -p build
-cd libusb-1.0.19
-PKG_CONFIG_PATH=$PWD/../build/lib/pkgconfig ./configure \
-    --host=i586-mingw32msvc --prefix=$PWD/../build
-# WINVER workaround needed for 1.0.19 only
-make CFLAGS="-DWINVER=0x0501"
-make install
-cd ..
-
-cd dfu-util-0.8
-PKG_CONFIG_PATH=$PWD/../build/lib/pkgconfig ./configure \
-    --host=i586-mingw32msvc --prefix=$PWD/../build
-make
-make install
-cd ..
-
-The build files will now be in build/bin. -

- -

Building on Windows using MinGW

-This assumes using release tarballs or having run ./autogen.sh on -the git sources. -
-cd libusb-1.0.19
-./configure --prefix=$HOME
-# WINVER workaround needed for 1.0.19 only
-# MKDIR_P setting should not really be needed...
-make CFLAGS="-DWINVER=0x0501" MKDIR_P="mkdir -p"
-make install
-cd ..
-
-cd dfu-util-0.8
-./configure USB_CFLAGS="-I$HOME/include/libusb-1.0" \
-            USB_LIBS="-L $HOME/lib -lusb-1.0" PKG_CONFIG=true
-make
-make install
-cd ..
-
-To link libusb statically into dfu-util.exe use instead of "make": -
-make LDFLAGS=-static
-
-The built executables (and DLL) will now be under $HOME/bin. - -

-[Back to dfu-util main page] -

- -
- - + + + + + + + Building dfu-util from source + + + + + + + + + +
+

How to build dfu-util from source

+ +

Prerequisites for building from git

+

Mac OS X

+

+First install MacPorts (and if you are on 10.6 or older, the Java Developer Package) and then run: +

+
+	sudo port install libusb-devel git-core
+
+ +

FreeBSD

+
+	sudo pkg_add -r git pkgconf
+
+ +

Ubuntu and Debian and derivatives

+
+	sudo apt-get build-dep dfu-util
+	sudo apt-get install libusb-1.0-0-dev
+
+ +

Get the source code and build it

+

+The first time you will have to clone the git repository: +

+
+	git clone git://gitorious.org/dfu-util/dfu-util.git
+	cd dfu-util
+
+

+If you later want to update to latest git version, just run this: +

+
+	make maintainer-clean
+	git pull
+
+

+To build the source: +

+
+	./autogen.sh
+	./configure  # on most systems
+	make
+
+ +

+If you are building on Mac OS X, replace the ./configure command with: +

+
+	./configure --libdir=/opt/local/lib --includedir=/opt/local/include  # on MacOSX only
+
+ +

+Your dfu-util binary will be inside the src folder. Use it from there, or install it to /usr/local/bin by running "sudo make install". +

+ +

Cross-building for Windows

+ +

+Windows binaries can be built in a MinGW +environment, on a Windows computer or cross-hosted in another OS. +To build it on a Debian or Ubuntu host, first install build dependencies: +

+
+	sudo apt-get build-dep libusb-1.0-0 dfu-util
+	sudo apt-get install mingw32
+
+ +

+The below example builds dfu-util 0.8 and libusb 1.0.19 from unpacked release +tarballs. If you instead build from git, you will have to run "./autogen.sh" +before running the "./configure" steps. +

+ +
+mkdir -p build
+cd libusb-1.0.19
+PKG_CONFIG_PATH=$PWD/../build/lib/pkgconfig ./configure \
+    --host=i586-mingw32msvc --prefix=$PWD/../build
+# WINVER workaround needed for 1.0.19 only
+make CFLAGS="-DWINVER=0x0501"
+make install
+cd ..
+
+cd dfu-util-0.8
+PKG_CONFIG_PATH=$PWD/../build/lib/pkgconfig ./configure \
+    --host=i586-mingw32msvc --prefix=$PWD/../build
+make
+make install
+cd ..
+
+The build files will now be in build/bin. +

+ +

Building on Windows using MinGW

+This assumes using release tarballs or having run ./autogen.sh on +the git sources. +
+cd libusb-1.0.19
+./configure --prefix=$HOME
+# WINVER workaround needed for 1.0.19 only
+# MKDIR_P setting should not really be needed...
+make CFLAGS="-DWINVER=0x0501" MKDIR_P="mkdir -p"
+make install
+cd ..
+
+cd dfu-util-0.8
+./configure USB_CFLAGS="-I$HOME/include/libusb-1.0" \
+            USB_LIBS="-L $HOME/lib -lusb-1.0" PKG_CONFIG=true
+make
+make install
+cd ..
+
+To link libusb statically into dfu-util.exe use instead of "make": +
+make LDFLAGS=-static
+
+The built executables (and DLL) will now be under $HOME/bin. + +

+[Back to dfu-util main page] +

+ +
+ + diff --git a/tools/src/dfu-util/www/dfu-util.1.html b/tools/linux/src/dfu-util/www/dfu-util.1.html old mode 100755 new mode 100644 similarity index 95% rename from tools/src/dfu-util/www/dfu-util.1.html rename to tools/linux/src/dfu-util/www/dfu-util.1.html index 667a369..62ca40b --- a/tools/src/dfu-util/www/dfu-util.1.html +++ b/tools/linux/src/dfu-util/www/dfu-util.1.html @@ -1,411 +1,411 @@ - - -Man page of DFU-UTIL - - -

DFU-UTIL(1)

- -  -

NAME

- -dfu-util - Device firmware update (DFU) USB programmer -  -

SYNOPSIS

- - -
-
-dfu-util - --l - -[-v] - -[-d - -vid:pid[,vid:pid]] - -[-p - -path] - -[-c - -configuration] - -[-i - -interface] - -[-a - -alt-intf] - -[-S - -serial[,serial]] - - -
-dfu-util - -[-v] - -[-d - -vid:pid[,vid:pid]] - -[-p - -path] - -[-c - -configuration] - -[-i - -interface] - -[-a - -alt-intf] - -[-S - -serial[,serial]] - -[-t - -size] - -[-Z - -size] - -[-s - -address] - -[-R] - -[-D|-U - -file] - - -
-dfu-util - -[-hV] - -
-  -

DESCRIPTION

- -dfu-util - -is a program that implements the host (computer) side of the USB DFU -(Universal Serial Bus Device Firmware Upgrade) protocol. -

-dfu-util communicates with devices that implement the device side of the -USB DFU protocol, and is often used to upgrade the firmware of such -devices. -  -

OPTIONS

- -
-
-l, --list - -
-List the currently attached DFU capable USB devices. -
-d, --device [Run-Time VENDOR]:[Run-Time PRODUCT][,[DFU Mode VENDOR]:[DFU Mode PRODUCT]] - -
-
-Specify run-time and/or DFU mode vendor and/or product IDs of the DFU device -to work with. VENDOR and PRODUCT are hexadecimal numbers (no prefix -needed), "*" (match any), or "-" (match nothing). By default, any DFU capable -device in either run-time or DFU mode will be considered. -

-If you only have one standards-compliant DFU device attached to your computer, -this parameter is optional. However, as soon as you have multiple DFU devices -connected, dfu-util will detect this and abort, asking you to specify which -device to use. -

-If only run-time IDs are specified (e.g. "--device 1457:51ab"), then in -addition to the specified run-time IDs, any DFU mode devices will also be -considered. This is beneficial to allow a DFU capable device to be found -again after a switch to DFU mode, since the vendor and/or product ID of a -device usually changes in DFU mode. -

-If only DFU mode IDs are specified (e.g. "--device ,951:26"), then all -run-time devices will be ignored, making it easy to target a specific device in -DFU mode. -

-If both run-time and DFU mode IDs are specified (e.g. "--device -1457:51ab,:2bc"), then unspecified DFU mode components will use the run-time -value specified. -

-Examples: -

-
--device 1457:51ab,951:26 - -
-
- -Work with a device in run-time mode with -vendor ID 0x1457 and product ID 0x51ab, or in DFU mode with vendor ID 0x0951 -and product ID 0x0026 -

-

--device 1457:51ab,:2bc - -
-
- -Work with a device in run-time mode with vendor ID 0x1457 and product ID -0x51ab, or in DFU mode with vendor ID 0x1457 and product ID 0x02bc -

-

--device 1457:51ab - -
-
- -Work with a device in run-time mode with vendor ID 0x1457 and product ID -0x51ab, or in DFU mode with any vendor and product ID -

-

--device ,951:26 - -
-
- -Work with a device in DFU mode with vendor ID 0x0951 and product ID 0x0026 -

-

--device *,- - -
-
- -Work with any device in run-time mode, and ignore any device in DFU mode -

-

--device , - -
-
- -Ignore any device in run-time mode, and Work with any device in DFU mode -
-
- -
-p, --path BUS-PORT. ... .PORT - -
-Specify the path to the DFU device. -
-c, --cfg CONFIG-NR - -
-Specify the configuration of the DFU device. Note that this is only used for matching, the configuration is not set by dfu-util. -
-i, --intf INTF-NR - -
-Specify the DFU interface number. -
-a, --alt ALT - -
-Specify the altsetting of the DFU interface by name or by number. -
-S, --serial [Run-Time SERIAL][,[DFU Mode SERIAL]] - -
-Specify the run-time and DFU mode serial numbers used to further restrict -device matches. If multiple, identical DFU devices are simultaneously -connected to a system then vendor and product ID will be insufficient for -targeting a single device. In this situation, it may be possible to use this -parameter to specify a serial number which also must match. -

-If only a single serial number is specified, then the same serial number is -used in both run-time and DFU mode. An empty serial number will match any -serial number in the corresponding mode. -

-t, --transfer-size SIZE - -
-Specify the number of bytes per USB transfer. The optimal value is -usually determined automatically so this option is rarely useful. If -you need to use this option for a device, please report it as a bug. -
-Z, --upload-size SIZE - -
-Specify the expected upload size, in bytes. -
-U, --upload FILE - -
-Read firmware from device into -FILE. - -
-D, --download FILE - -
-Write firmware from -FILE - -into device. -
-R, --reset - -
-Issue USB reset signalling after upload or download has finished. -
-s, --dfuse-address address - -
-Specify target address for raw binary download/upload on DfuSe devices. Do -not - -use this for downloading DfuSe (.dfu) files. Modifiers can be added -to the address, separated by a colon, to perform special DfuSE commands such -as "leave" DFU mode, "unprotect" and "mass-erase" flash memory. -
-v, --verbose - -
-Print more information about dfu-util's operation. A second --v - -will turn on verbose logging of USB requests. Repeat this option to further -increase verbosity. -
-h, --help - -
-Show a help text and exit. -
-V, --version - -
-Show version information and exit. -
-  -

EXAMPLES

- -  -

Using dfu-util in the OpenMoko project

- -(with the Neo1973 hardware) -

- -Flashing the rootfs: -
- - $ dfu-util -a rootfs -R -D /path/to/openmoko-devel-image.jffs2 - -

- -Flashing the kernel: -
- - $ dfu-util -a kernel -R -D /path/to/uImage - -

- -Flashing the bootloader: -
- - $ dfu-util -a u-boot -R -D /path/to/u-boot.bin - -

- -Copying a kernel into RAM: -
- - $ dfu-util -a 0 -R -D /path/to/uImage - -

-Once this has finished, the kernel will be available at the default load -address of 0x32000000 in Neo1973 RAM. -Note: - -You cannot transfer more than 2MB of data into RAM using this method. -

-  -

Using dfu-util with a DfuSe device

- -

- -Flashing a -.dfu - -(special DfuSe format) file to the device: -
- - $ dfu-util -a 0 -D /path/to/dfuse-image.dfu - -

- -Reading out 1 KB of flash starting at address 0x8000000: -
- - $ dfu-util -a 0 -s 0x08000000:1024 -U newfile.bin - -

- -Flashing a binary file to address 0x8004000 of device memory and -ask the device to leave DFU mode: -
- - $ dfu-util -a 0 -s 0x08004000:leave -D /path/to/image.bin - - -  -

BUGS

- -Please report any bugs to the dfu-util mailing list at -dfu-util@lists.gnumonks.org. - -Please use the ---verbose option (repeated as necessary) to provide more - -information in your bug report. -  -

SEE ALSO

- -The dfu-util home page is -http://dfu-util.gnumonks.org - -  -

HISTORY

- -dfu-util was originally written for the OpenMoko project by -Weston Schmidt <weston_schmidt@yahoo.com> and -Harald Welte <hwelte@hmw-consulting.de>. Over time, nearly complete -support of DFU 1.0, DFU 1.1 and DfuSe ("1.1a") has been added. -  -

LICENCE

- -dfu-util - -is covered by the GNU General Public License (GPL), version 2 or later. -  -

COPYRIGHT

- -This manual page was originally written by Uwe Hermann <uwe@hermann-uwe.de>, -and is now part of the dfu-util project. -

- -


- 

Index

-
-
NAME
-
SYNOPSIS
-
DESCRIPTION
-
OPTIONS
-
EXAMPLES
-
-
Using dfu-util in the OpenMoko project
-
Using dfu-util with a DfuSe device
-
-
BUGS
-
SEE ALSO
-
HISTORY
-
LICENCE
-
COPYRIGHT
-
-
-This document was created by man2html, -using the doc/dfu-util.1 manual page from dfu-util 0.8.
-Time: 14:40:57 GMT, September 13, 2014 - - + + +Man page of DFU-UTIL + + +

DFU-UTIL(1)

+ +  +

NAME

+ +dfu-util - Device firmware update (DFU) USB programmer +  +

SYNOPSIS

+ + +
+
+dfu-util + +-l + +[-v] + +[-d + +vid:pid[,vid:pid]] + +[-p + +path] + +[-c + +configuration] + +[-i + +interface] + +[-a + +alt-intf] + +[-S + +serial[,serial]] + + +
+dfu-util + +[-v] + +[-d + +vid:pid[,vid:pid]] + +[-p + +path] + +[-c + +configuration] + +[-i + +interface] + +[-a + +alt-intf] + +[-S + +serial[,serial]] + +[-t + +size] + +[-Z + +size] + +[-s + +address] + +[-R] + +[-D|-U + +file] + + +
+dfu-util + +[-hV] + +
+  +

DESCRIPTION

+ +dfu-util + +is a program that implements the host (computer) side of the USB DFU +(Universal Serial Bus Device Firmware Upgrade) protocol. +

+dfu-util communicates with devices that implement the device side of the +USB DFU protocol, and is often used to upgrade the firmware of such +devices. +  +

OPTIONS

+ +
+
-l, --list + +
+List the currently attached DFU capable USB devices. +
-d, --device [Run-Time VENDOR]:[Run-Time PRODUCT][,[DFU Mode VENDOR]:[DFU Mode PRODUCT]] + +
+
+Specify run-time and/or DFU mode vendor and/or product IDs of the DFU device +to work with. VENDOR and PRODUCT are hexadecimal numbers (no prefix +needed), "*" (match any), or "-" (match nothing). By default, any DFU capable +device in either run-time or DFU mode will be considered. +

+If you only have one standards-compliant DFU device attached to your computer, +this parameter is optional. However, as soon as you have multiple DFU devices +connected, dfu-util will detect this and abort, asking you to specify which +device to use. +

+If only run-time IDs are specified (e.g. "--device 1457:51ab"), then in +addition to the specified run-time IDs, any DFU mode devices will also be +considered. This is beneficial to allow a DFU capable device to be found +again after a switch to DFU mode, since the vendor and/or product ID of a +device usually changes in DFU mode. +

+If only DFU mode IDs are specified (e.g. "--device ,951:26"), then all +run-time devices will be ignored, making it easy to target a specific device in +DFU mode. +

+If both run-time and DFU mode IDs are specified (e.g. "--device +1457:51ab,:2bc"), then unspecified DFU mode components will use the run-time +value specified. +

+Examples: +

+
--device 1457:51ab,951:26 + +
+
+ +Work with a device in run-time mode with +vendor ID 0x1457 and product ID 0x51ab, or in DFU mode with vendor ID 0x0951 +and product ID 0x0026 +

+

--device 1457:51ab,:2bc + +
+
+ +Work with a device in run-time mode with vendor ID 0x1457 and product ID +0x51ab, or in DFU mode with vendor ID 0x1457 and product ID 0x02bc +

+

--device 1457:51ab + +
+
+ +Work with a device in run-time mode with vendor ID 0x1457 and product ID +0x51ab, or in DFU mode with any vendor and product ID +

+

--device ,951:26 + +
+
+ +Work with a device in DFU mode with vendor ID 0x0951 and product ID 0x0026 +

+

--device *,- + +
+
+ +Work with any device in run-time mode, and ignore any device in DFU mode +

+

--device , + +
+
+ +Ignore any device in run-time mode, and Work with any device in DFU mode +
+
+ +
-p, --path BUS-PORT. ... .PORT + +
+Specify the path to the DFU device. +
-c, --cfg CONFIG-NR + +
+Specify the configuration of the DFU device. Note that this is only used for matching, the configuration is not set by dfu-util. +
-i, --intf INTF-NR + +
+Specify the DFU interface number. +
-a, --alt ALT + +
+Specify the altsetting of the DFU interface by name or by number. +
-S, --serial [Run-Time SERIAL][,[DFU Mode SERIAL]] + +
+Specify the run-time and DFU mode serial numbers used to further restrict +device matches. If multiple, identical DFU devices are simultaneously +connected to a system then vendor and product ID will be insufficient for +targeting a single device. In this situation, it may be possible to use this +parameter to specify a serial number which also must match. +

+If only a single serial number is specified, then the same serial number is +used in both run-time and DFU mode. An empty serial number will match any +serial number in the corresponding mode. +

-t, --transfer-size SIZE + +
+Specify the number of bytes per USB transfer. The optimal value is +usually determined automatically so this option is rarely useful. If +you need to use this option for a device, please report it as a bug. +
-Z, --upload-size SIZE + +
+Specify the expected upload size, in bytes. +
-U, --upload FILE + +
+Read firmware from device into +FILE. + +
-D, --download FILE + +
+Write firmware from +FILE + +into device. +
-R, --reset + +
+Issue USB reset signalling after upload or download has finished. +
-s, --dfuse-address address + +
+Specify target address for raw binary download/upload on DfuSe devices. Do +not + +use this for downloading DfuSe (.dfu) files. Modifiers can be added +to the address, separated by a colon, to perform special DfuSE commands such +as "leave" DFU mode, "unprotect" and "mass-erase" flash memory. +
-v, --verbose + +
+Print more information about dfu-util's operation. A second +-v + +will turn on verbose logging of USB requests. Repeat this option to further +increase verbosity. +
-h, --help + +
+Show a help text and exit. +
-V, --version + +
+Show version information and exit. +
+  +

EXAMPLES

+ +  +

Using dfu-util in the OpenMoko project

+ +(with the Neo1973 hardware) +

+ +Flashing the rootfs: +
+ + $ dfu-util -a rootfs -R -D /path/to/openmoko-devel-image.jffs2 + +

+ +Flashing the kernel: +
+ + $ dfu-util -a kernel -R -D /path/to/uImage + +

+ +Flashing the bootloader: +
+ + $ dfu-util -a u-boot -R -D /path/to/u-boot.bin + +

+ +Copying a kernel into RAM: +
+ + $ dfu-util -a 0 -R -D /path/to/uImage + +

+Once this has finished, the kernel will be available at the default load +address of 0x32000000 in Neo1973 RAM. +Note: + +You cannot transfer more than 2MB of data into RAM using this method. +

+  +

Using dfu-util with a DfuSe device

+ +

+ +Flashing a +.dfu + +(special DfuSe format) file to the device: +
+ + $ dfu-util -a 0 -D /path/to/dfuse-image.dfu + +

+ +Reading out 1 KB of flash starting at address 0x8000000: +
+ + $ dfu-util -a 0 -s 0x08000000:1024 -U newfile.bin + +

+ +Flashing a binary file to address 0x8004000 of device memory and +ask the device to leave DFU mode: +
+ + $ dfu-util -a 0 -s 0x08004000:leave -D /path/to/image.bin + + +  +

BUGS

+ +Please report any bugs to the dfu-util mailing list at +dfu-util@lists.gnumonks.org. + +Please use the +--verbose option (repeated as necessary) to provide more + +information in your bug report. +  +

SEE ALSO

+ +The dfu-util home page is +http://dfu-util.gnumonks.org + +  +

HISTORY

+ +dfu-util was originally written for the OpenMoko project by +Weston Schmidt <weston_schmidt@yahoo.com> and +Harald Welte <hwelte@hmw-consulting.de>. Over time, nearly complete +support of DFU 1.0, DFU 1.1 and DfuSe ("1.1a") has been added. +  +

LICENCE

+ +dfu-util + +is covered by the GNU General Public License (GPL), version 2 or later. +  +

COPYRIGHT

+ +This manual page was originally written by Uwe Hermann <uwe@hermann-uwe.de>, +and is now part of the dfu-util project. +

+ +


+ 

Index

+
+
NAME
+
SYNOPSIS
+
DESCRIPTION
+
OPTIONS
+
EXAMPLES
+
+
Using dfu-util in the OpenMoko project
+
Using dfu-util with a DfuSe device
+
+
BUGS
+
SEE ALSO
+
HISTORY
+
LICENCE
+
COPYRIGHT
+
+
+This document was created by man2html, +using the doc/dfu-util.1 manual page from dfu-util 0.8.
+Time: 14:40:57 GMT, September 13, 2014 + + diff --git a/tools/src/dfu-util/www/dfuse.html b/tools/linux/src/dfu-util/www/dfuse.html old mode 100755 new mode 100644 similarity index 97% rename from tools/src/dfu-util/www/dfuse.html rename to tools/linux/src/dfu-util/www/dfuse.html index ebb9784..35e4ffa --- a/tools/src/dfu-util/www/dfuse.html +++ b/tools/linux/src/dfu-util/www/dfuse.html @@ -1,135 +1,135 @@ - - - - - - DfuSe and dfu-util - - - - - - - - - -
-

Using dfu-util with DfuSe devices

-

DfuSe

-

- DfuSe (DFU with ST Microsystems extensions) is a protocol based on - DFU 1.1. However, in expanding the functionality of the DFU protocol, - ST Microsystems broke all compatibility with the DFU 1.1 standard. - DfuSe devices report the DFU version as "1.1a". -

-

- DfuSe can be used to download firmware and other data - from a host computer to a conforming device (or upload in the - opposite direction) over USB similar to standard DFU. -

-

- The main difference from standard DFU is that the target address in - the device (flash) memory is specified by the host, so that a - download can be performed to parts of the device memory. The host - program is also responsible for erasing flash pages before they - are written to. -

-

.dfu files

-

- A special file format is defined by ST Microsystems to carry firmware - for DfuSe devices. The file contains target information such as address - and alternate interface information in addition to the binary data. - Several blocks of binary data can be combined in one .dfu file. -

-

Alternate interfaces

-

- Different memory locations of the device may have different - characteristics that the host program (dfu-util) has to take - into considerations, such as flash memory page size, read-only - versus read-write segments, the need to erase, and so on. - These parameters are reported by the device in the string - descriptors meant for describing the USB interfaces. - The host program decodes these strings to build a memory map of - the device. Different memory units or address spaces are listed - in separate alternate interface settings that must be selected - according to the memory unit to access. -

-

- Note that dfu-util leaves it to the user to select alternate - interface. When parsing a .dfu file it will skip file segments - not matching the selected alternate interface. Also, some - DfuSe device firmware implementations ignore the setting of - alternate interface and deduct the memory unit from the - address, since they have no address space overlap. -

-

DfuSe special commands

-

- DfuSe special commands are used by the host program during normal - downloads or uploads, such as SET_ADDRESS and ERASE_PAGE. Also - the normal DFU_DNLOAD and DFU_UPLOAD commands have special - implementations in DfuSe. - Many DfuSe devices also support commands to leave DFU mode, - read unprotect the flash memory or mass erase the flash memory. - dfu-util (from version 0.7) - supports adding "leave", "unprotect", or "mass-erase" - to the -s option argument to send such requests in combination - with a download request. These modifiers are separated with a colon. -

-

- Some DfuSe devices have their DfuSe bootloader running from flash - memory. Erasing the whole flash memory would therefore destroy - the DfuSe bootloader itself and practically brick the device - for most users. Any use of modifiers such as "unprotect" - and "mass-erase" therefore needs to be combined with the "force" - modifer. This is not included in the examples, to not encourage - ignorant users to copy and paste such instructions and shoot - themselves in the foot. -

-

- Devices based on for instance STM32F103 all run the bootloader - from flash, since there is no USB bootloader in ROM. -

-

- For instance STM32F107, STM32F2xx and STM32F4xx devices have a - DfuSe bootloader in ROM, so the flash can be erased while - keeping the device available for USB DFU transfers as long - as the device designers use this built-in bootloader and have - not implemented another DfuSe bootloader in flash that the user is - dependent upon. -

-

- Well-written bootloaders running from flash will report their - own memory region as read-only and not eraseable, but this does - not prevent dfu-util from sending a "unprotect" or "mass-erase" - request which overrides this, if the user insists. -

-

Example usage

-

- Flashing a .dfu (special DfuSe format) file to the device: -

-
-         $ dfu-util -a 0 -D /path/to/dfuse-image.dfu
-	
-

- Reading out 1 KB of flash starting at address 0x8000000: -

-
-         $ dfu-util -a 0 -s 0x08000000:1024 -U newfile.bin
-	
-

- Flashing a binary file to address 0x8004000 of device memory and ask - the device to leave DFU mode: -

-
-         $ dfu-util -a 0 -s 0x08004000:leave -D /path/to/image.bin
-	
-

- [Back to dfu-util main page] -

- -
- - + + + + + + DfuSe and dfu-util + + + + + + + + + +
+

Using dfu-util with DfuSe devices

+

DfuSe

+

+ DfuSe (DFU with ST Microsystems extensions) is a protocol based on + DFU 1.1. However, in expanding the functionality of the DFU protocol, + ST Microsystems broke all compatibility with the DFU 1.1 standard. + DfuSe devices report the DFU version as "1.1a". +

+

+ DfuSe can be used to download firmware and other data + from a host computer to a conforming device (or upload in the + opposite direction) over USB similar to standard DFU. +

+

+ The main difference from standard DFU is that the target address in + the device (flash) memory is specified by the host, so that a + download can be performed to parts of the device memory. The host + program is also responsible for erasing flash pages before they + are written to. +

+

.dfu files

+

+ A special file format is defined by ST Microsystems to carry firmware + for DfuSe devices. The file contains target information such as address + and alternate interface information in addition to the binary data. + Several blocks of binary data can be combined in one .dfu file. +

+

Alternate interfaces

+

+ Different memory locations of the device may have different + characteristics that the host program (dfu-util) has to take + into considerations, such as flash memory page size, read-only + versus read-write segments, the need to erase, and so on. + These parameters are reported by the device in the string + descriptors meant for describing the USB interfaces. + The host program decodes these strings to build a memory map of + the device. Different memory units or address spaces are listed + in separate alternate interface settings that must be selected + according to the memory unit to access. +

+

+ Note that dfu-util leaves it to the user to select alternate + interface. When parsing a .dfu file it will skip file segments + not matching the selected alternate interface. Also, some + DfuSe device firmware implementations ignore the setting of + alternate interface and deduct the memory unit from the + address, since they have no address space overlap. +

+

DfuSe special commands

+

+ DfuSe special commands are used by the host program during normal + downloads or uploads, such as SET_ADDRESS and ERASE_PAGE. Also + the normal DFU_DNLOAD and DFU_UPLOAD commands have special + implementations in DfuSe. + Many DfuSe devices also support commands to leave DFU mode, + read unprotect the flash memory or mass erase the flash memory. + dfu-util (from version 0.7) + supports adding "leave", "unprotect", or "mass-erase" + to the -s option argument to send such requests in combination + with a download request. These modifiers are separated with a colon. +

+

+ Some DfuSe devices have their DfuSe bootloader running from flash + memory. Erasing the whole flash memory would therefore destroy + the DfuSe bootloader itself and practically brick the device + for most users. Any use of modifiers such as "unprotect" + and "mass-erase" therefore needs to be combined with the "force" + modifer. This is not included in the examples, to not encourage + ignorant users to copy and paste such instructions and shoot + themselves in the foot. +

+

+ Devices based on for instance STM32F103 all run the bootloader + from flash, since there is no USB bootloader in ROM. +

+

+ For instance STM32F107, STM32F2xx and STM32F4xx devices have a + DfuSe bootloader in ROM, so the flash can be erased while + keeping the device available for USB DFU transfers as long + as the device designers use this built-in bootloader and have + not implemented another DfuSe bootloader in flash that the user is + dependent upon. +

+

+ Well-written bootloaders running from flash will report their + own memory region as read-only and not eraseable, but this does + not prevent dfu-util from sending a "unprotect" or "mass-erase" + request which overrides this, if the user insists. +

+

Example usage

+

+ Flashing a .dfu (special DfuSe format) file to the device: +

+
+         $ dfu-util -a 0 -D /path/to/dfuse-image.dfu
+	
+

+ Reading out 1 KB of flash starting at address 0x8000000: +

+
+         $ dfu-util -a 0 -s 0x08000000:1024 -U newfile.bin
+	
+

+ Flashing a binary file to address 0x8004000 of device memory and ask + the device to leave DFU mode: +

+
+         $ dfu-util -a 0 -s 0x08004000:leave -D /path/to/image.bin
+	
+

+ [Back to dfu-util main page] +

+ +
+ + diff --git a/tools/src/dfu-util/www/index.html b/tools/linux/src/dfu-util/www/index.html old mode 100755 new mode 100644 similarity index 97% rename from tools/src/dfu-util/www/index.html rename to tools/linux/src/dfu-util/www/index.html index 9bb3223..108ddaf --- a/tools/src/dfu-util/www/index.html +++ b/tools/linux/src/dfu-util/www/index.html @@ -1,119 +1,119 @@ - - - - - - dfu-util Homepage - - - - - - - - - -
-

dfu-util - Device Firmware Upgrade Utilities

-

Description

-

- dfu-util is a host side implementation of the DFU 1.0 and DFU 1.1 specifications of the USB forum. - - DFU is intended to download and upload firmware to/from devices connected - over USB. It ranges from small devices like micro-controller boards - to mobile phones. Using dfu-util you can download firmware to your - DFU-enabled device or upload firmware from it. dfu-util has been - tested with the Openmoko Neo1973 and Freerunner and many other devices. -

-

- See the manual page for examples of use. -

-

Supported Devices

- -

Releases

-

- Releases of the dfu-util software can be found in the - releases folder. - The current release is 0.8. -

-

- We offer binaries for Microsoft Windows and some other platforms. - dfu-util uses libusb 1.0 to access your device, so - on Windows you have to register the device with the WinUSB driver - (alternatively libusb-win32 or libusbK), please see the libusbx wiki - for more details. -

-

- Mac OS X users can also get dfu-util from Homebrew with "brew install dfu-util" or from MacPorts. -

-

- Most Linux distributions ship dfu-util in binary packages for those - who do not want to compile dfu-util from source. - On Debian, Ubuntu, Fedora and Gentoo you can install it through the - normal software package tools. For other distributions -(namely OpenSuSe, Mandriva, and CentOS) Holger Freyther was kind enough to -provide binary packages through the Open Build Service. -

-

Development

-

- Development happens in a GIT repository. Browse it via the web -interface or clone it with: -

-
-	git clone git://gitorious.org/dfu-util/dfu-util.git
-	
-

- See our build instructions for how to - build the source on different platforms. -

-

License

-

- This software is licensed under the GPL version 2. -

-

Contact

-

- If you have questions about the development or use of dfu-util please - send an e-mail to our dedicated -mailing list for dfu-util. -

-

People

-

- dfu-util was originally written by - Harald Welte partially based on code from - dfu-programmer 0.4 and is currently maintained by Stefan Schmidt and - Tormod Volden. -

- -
- - + + + + + + dfu-util Homepage + + + + + + + + + +
+

dfu-util - Device Firmware Upgrade Utilities

+

Description

+

+ dfu-util is a host side implementation of the DFU 1.0 and DFU 1.1 specifications of the USB forum. + + DFU is intended to download and upload firmware to/from devices connected + over USB. It ranges from small devices like micro-controller boards + to mobile phones. Using dfu-util you can download firmware to your + DFU-enabled device or upload firmware from it. dfu-util has been + tested with the Openmoko Neo1973 and Freerunner and many other devices. +

+

+ See the manual page for examples of use. +

+

Supported Devices

+ +

Releases

+

+ Releases of the dfu-util software can be found in the + releases folder. + The current release is 0.8. +

+

+ We offer binaries for Microsoft Windows and some other platforms. + dfu-util uses libusb 1.0 to access your device, so + on Windows you have to register the device with the WinUSB driver + (alternatively libusb-win32 or libusbK), please see the libusbx wiki + for more details. +

+

+ Mac OS X users can also get dfu-util from Homebrew with "brew install dfu-util" or from MacPorts. +

+

+ Most Linux distributions ship dfu-util in binary packages for those + who do not want to compile dfu-util from source. + On Debian, Ubuntu, Fedora and Gentoo you can install it through the + normal software package tools. For other distributions +(namely OpenSuSe, Mandriva, and CentOS) Holger Freyther was kind enough to +provide binary packages through the Open Build Service. +

+

Development

+

+ Development happens in a GIT repository. Browse it via the web +interface or clone it with: +

+
+	git clone git://gitorious.org/dfu-util/dfu-util.git
+	
+

+ See our build instructions for how to + build the source on different platforms. +

+

License

+

+ This software is licensed under the GPL version 2. +

+

Contact

+

+ If you have questions about the development or use of dfu-util please + send an e-mail to our dedicated +mailing list for dfu-util. +

+

People

+

+ dfu-util was originally written by + Harald Welte partially based on code from + dfu-programmer 0.4 and is currently maintained by Stefan Schmidt and + Tormod Volden. +

+ +
+ + diff --git a/tools/src/dfu-util/www/simple.css b/tools/linux/src/dfu-util/www/simple.css old mode 100755 new mode 100644 similarity index 92% rename from tools/src/dfu-util/www/simple.css rename to tools/linux/src/dfu-util/www/simple.css index 10b7c1d..98100bc --- a/tools/src/dfu-util/www/simple.css +++ b/tools/linux/src/dfu-util/www/simple.css @@ -1,56 +1,56 @@ -body { - margin: 10px; - font-size: 0.82em; - background-color: #EEE; -} - -h1 { - clear: both; - padding: 0 0 12px 0; - margin: 0; - font-size: 2em; - font-weight: bold; -} - -h2 { - clear: both; - margin: 0; - font-size: 1.5em; - font-weight: normal; -} - -h3 { - clear: both; - margin: 15px 0 0 0; - font-size: 1.0em; - font-weight: bold; -} - -p { - line-height: 20px; - padding: 8px 0 8px 0; - margin: 0; - font-size: 1.1em; -} - -pre { - white-space: pre-wrap; - background-color: #CCC; - padding: 3px; -} - -a:hover { - background-color: #DDD; -} - -#middlebox { - width: 600px; - margin: 0px auto; - text-align: left; -} - -#footer { - height: 100px; - padding: 28px 3px 0 0; - margin: 20px 0 20px 0; -} +body { + margin: 10px; + font-size: 0.82em; + background-color: #EEE; +} + +h1 { + clear: both; + padding: 0 0 12px 0; + margin: 0; + font-size: 2em; + font-weight: bold; +} + +h2 { + clear: both; + margin: 0; + font-size: 1.5em; + font-weight: normal; +} + +h3 { + clear: both; + margin: 15px 0 0 0; + font-size: 1.0em; + font-weight: bold; +} + +p { + line-height: 20px; + padding: 8px 0 8px 0; + margin: 0; + font-size: 1.1em; +} + +pre { + white-space: pre-wrap; + background-color: #CCC; + padding: 3px; +} + +a:hover { + background-color: #DDD; +} + +#middlebox { + width: 600px; + margin: 0px auto; + text-align: left; +} + +#footer { + height: 100px; + padding: 28px 3px 0 0; + margin: 20px 0 20px 0; +} diff --git a/tools/src/maple_loader/README.md b/tools/linux/src/maple_loader/README.md similarity index 100% rename from tools/src/maple_loader/README.md rename to tools/linux/src/maple_loader/README.md diff --git a/tools/src/maple_loader/build.xml b/tools/linux/src/maple_loader/build.xml similarity index 97% rename from tools/src/maple_loader/build.xml rename to tools/linux/src/maple_loader/build.xml index 63ae9c2..80bdd6f 100644 --- a/tools/src/maple_loader/build.xml +++ b/tools/linux/src/maple_loader/build.xml @@ -1,73 +1,73 @@ - - - - - - - - - - - Builds, tests, and runs the project maple_loader. - - - + + + + + + + + + + + Builds, tests, and runs the project maple_loader. + + + diff --git a/tools/src/maple_loader/build/built-jar.properties b/tools/linux/src/maple_loader/build/built-jar.properties similarity index 96% rename from tools/src/maple_loader/build/built-jar.properties rename to tools/linux/src/maple_loader/build/built-jar.properties index 94002ae..10752d5 100644 --- a/tools/src/maple_loader/build/built-jar.properties +++ b/tools/linux/src/maple_loader/build/built-jar.properties @@ -1,4 +1,4 @@ -#Mon, 20 Jul 2015 11:21:26 +1000 - - -C\:\\Users\\rclark\\Desktop\\maple-asp-master\\installer\\maple_loader= +#Mon, 20 Jul 2015 11:21:26 +1000 + + +C\:\\Users\\rclark\\Desktop\\maple-asp-master\\installer\\maple_loader= diff --git a/tools/src/maple_loader/build/classes/CliTemplate/CliMain.class b/tools/linux/src/maple_loader/build/classes/CliTemplate/CliMain.class similarity index 100% rename from tools/src/maple_loader/build/classes/CliTemplate/CliMain.class rename to tools/linux/src/maple_loader/build/classes/CliTemplate/CliMain.class diff --git a/tools/src/maple_loader/build/classes/CliTemplate/DFUUploader.class b/tools/linux/src/maple_loader/build/classes/CliTemplate/DFUUploader.class similarity index 100% rename from tools/src/maple_loader/build/classes/CliTemplate/DFUUploader.class rename to tools/linux/src/maple_loader/build/classes/CliTemplate/DFUUploader.class diff --git a/tools/src/maple_loader/build/classes/CliTemplate/ExecCommand.class b/tools/linux/src/maple_loader/build/classes/CliTemplate/ExecCommand.class similarity index 100% rename from tools/src/maple_loader/build/classes/CliTemplate/ExecCommand.class rename to tools/linux/src/maple_loader/build/classes/CliTemplate/ExecCommand.class diff --git a/tools/src/maple_loader/build/classes/processing/app/Base.class b/tools/linux/src/maple_loader/build/classes/processing/app/Base.class similarity index 100% rename from tools/src/maple_loader/build/classes/processing/app/Base.class rename to tools/linux/src/maple_loader/build/classes/processing/app/Base.class diff --git a/tools/src/maple_loader/build/classes/processing/app/Preferences.class b/tools/linux/src/maple_loader/build/classes/processing/app/Preferences.class similarity index 100% rename from tools/src/maple_loader/build/classes/processing/app/Preferences.class rename to tools/linux/src/maple_loader/build/classes/processing/app/Preferences.class diff --git a/tools/src/maple_loader/build/classes/processing/app/Serial.class b/tools/linux/src/maple_loader/build/classes/processing/app/Serial.class similarity index 100% rename from tools/src/maple_loader/build/classes/processing/app/Serial.class rename to tools/linux/src/maple_loader/build/classes/processing/app/Serial.class diff --git a/tools/src/maple_loader/build/classes/processing/app/SerialException.class b/tools/linux/src/maple_loader/build/classes/processing/app/SerialException.class similarity index 100% rename from tools/src/maple_loader/build/classes/processing/app/SerialException.class rename to tools/linux/src/maple_loader/build/classes/processing/app/SerialException.class diff --git a/tools/src/maple_loader/build/classes/processing/app/debug/MessageConsumer.class b/tools/linux/src/maple_loader/build/classes/processing/app/debug/MessageConsumer.class similarity index 100% rename from tools/src/maple_loader/build/classes/processing/app/debug/MessageConsumer.class rename to tools/linux/src/maple_loader/build/classes/processing/app/debug/MessageConsumer.class diff --git a/tools/src/maple_loader/build/classes/processing/app/debug/MessageSiphon.class b/tools/linux/src/maple_loader/build/classes/processing/app/debug/MessageSiphon.class similarity index 100% rename from tools/src/maple_loader/build/classes/processing/app/debug/MessageSiphon.class rename to tools/linux/src/maple_loader/build/classes/processing/app/debug/MessageSiphon.class diff --git a/tools/src/maple_loader/build/classes/processing/app/debug/RunnerException.class b/tools/linux/src/maple_loader/build/classes/processing/app/debug/RunnerException.class similarity index 100% rename from tools/src/maple_loader/build/classes/processing/app/debug/RunnerException.class rename to tools/linux/src/maple_loader/build/classes/processing/app/debug/RunnerException.class diff --git a/tools/src/maple_loader/build/classes/processing/app/helpers/ProcessUtils.class b/tools/linux/src/maple_loader/build/classes/processing/app/helpers/ProcessUtils.class similarity index 100% rename from tools/src/maple_loader/build/classes/processing/app/helpers/ProcessUtils.class rename to tools/linux/src/maple_loader/build/classes/processing/app/helpers/ProcessUtils.class diff --git a/tools/src/maple_loader/dist/README.TXT b/tools/linux/src/maple_loader/dist/README.TXT similarity index 99% rename from tools/src/maple_loader/dist/README.TXT rename to tools/linux/src/maple_loader/dist/README.TXT index 795e0cf..255b89c 100644 --- a/tools/src/maple_loader/dist/README.TXT +++ b/tools/linux/src/maple_loader/dist/README.TXT @@ -29,4 +29,4 @@ the projects runtime path. * To set a main class in a standard Java project, right-click the project node in the Projects window and choose Properties. Then click Run and enter the class name in the Main Class field. Alternatively, you can manually type the -class name in the manifest Main-Class element. +class name in the manifest Main-Class element. diff --git a/tools/src/maple_loader/dist/lib/jssc.jar b/tools/linux/src/maple_loader/dist/lib/jssc.jar similarity index 100% rename from tools/src/maple_loader/dist/lib/jssc.jar rename to tools/linux/src/maple_loader/dist/lib/jssc.jar diff --git a/tools/src/maple_loader/dist/maple_loader.jar b/tools/linux/src/maple_loader/dist/maple_loader.jar similarity index 100% rename from tools/src/maple_loader/dist/maple_loader.jar rename to tools/linux/src/maple_loader/dist/maple_loader.jar diff --git a/tools/src/maple_loader/jars/jssc.jar b/tools/linux/src/maple_loader/jars/jssc.jar similarity index 100% rename from tools/src/maple_loader/jars/jssc.jar rename to tools/linux/src/maple_loader/jars/jssc.jar diff --git a/tools/src/maple_loader/manifest.mf b/tools/linux/src/maple_loader/manifest.mf similarity index 100% rename from tools/src/maple_loader/manifest.mf rename to tools/linux/src/maple_loader/manifest.mf diff --git a/tools/src/maple_loader/nbproject/build-impl.xml b/tools/linux/src/maple_loader/nbproject/build-impl.xml similarity index 98% rename from tools/src/maple_loader/nbproject/build-impl.xml rename to tools/linux/src/maple_loader/nbproject/build-impl.xml index 080006a..a66f349 100644 --- a/tools/src/maple_loader/nbproject/build-impl.xml +++ b/tools/linux/src/maple_loader/nbproject/build-impl.xml @@ -1,1413 +1,1413 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Must set src.dir - Must set test.src.dir - Must set build.dir - Must set dist.dir - Must set build.classes.dir - Must set dist.javadoc.dir - Must set build.test.classes.dir - Must set build.test.results.dir - Must set build.classes.excludes - Must set dist.jar - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Must set javac.includes - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - No tests executed. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Must set JVM to use for profiling in profiler.info.jvm - Must set profiler agent JVM arguments in profiler.info.jvmargs.agent - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Must select some files in the IDE or set javac.includes - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - To run this application from the command line without Ant, try: - - java -jar "${dist.jar.resolved}" - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Must select one file in the IDE or set run.class - - - - Must select one file in the IDE or set run.class - - - - - - - - - - - - - - - - - - - - - - - Must select one file in the IDE or set debug.class - - - - - Must select one file in the IDE or set debug.class - - - - - Must set fix.includes - - - - - - - - - - This target only works when run from inside the NetBeans IDE. - - - - - - - - - Must select one file in the IDE or set profile.class - This target only works when run from inside the NetBeans IDE. - - - - - - - - - This target only works when run from inside the NetBeans IDE. - - - - - - - - - - - - - This target only works when run from inside the NetBeans IDE. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Must select one file in the IDE or set run.class - - - - - - Must select some files in the IDE or set test.includes - - - - - Must select one file in the IDE or set run.class - - - - - Must select one file in the IDE or set applet.url - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Must select some files in the IDE or set javac.includes - - - - - - - - - - - - - - - - - - - - Some tests failed; see details above. - - - - - - - - - Must select some files in the IDE or set test.includes - - - - Some tests failed; see details above. - - - - Must select some files in the IDE or set test.class - Must select some method in the IDE or set test.method - - - - Some tests failed; see details above. - - - - - Must select one file in the IDE or set test.class - - - - Must select one file in the IDE or set test.class - Must select some method in the IDE or set test.method - - - - - - - - - - - - - - Must select one file in the IDE or set applet.url - - - - - - - - - Must select one file in the IDE or set applet.url - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Must set src.dir + Must set test.src.dir + Must set build.dir + Must set dist.dir + Must set build.classes.dir + Must set dist.javadoc.dir + Must set build.test.classes.dir + Must set build.test.results.dir + Must set build.classes.excludes + Must set dist.jar + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Must set javac.includes + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + No tests executed. + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Must set JVM to use for profiling in profiler.info.jvm + Must set profiler agent JVM arguments in profiler.info.jvmargs.agent + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Must select some files in the IDE or set javac.includes + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + To run this application from the command line without Ant, try: + + java -jar "${dist.jar.resolved}" + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Must select one file in the IDE or set run.class + + + + Must select one file in the IDE or set run.class + + + + + + + + + + + + + + + + + + + + + + + Must select one file in the IDE or set debug.class + + + + + Must select one file in the IDE or set debug.class + + + + + Must set fix.includes + + + + + + + + + + This target only works when run from inside the NetBeans IDE. + + + + + + + + + Must select one file in the IDE or set profile.class + This target only works when run from inside the NetBeans IDE. + + + + + + + + + This target only works when run from inside the NetBeans IDE. + + + + + + + + + + + + + This target only works when run from inside the NetBeans IDE. + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Must select one file in the IDE or set run.class + + + + + + Must select some files in the IDE or set test.includes + + + + + Must select one file in the IDE or set run.class + + + + + Must select one file in the IDE or set applet.url + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Must select some files in the IDE or set javac.includes + + + + + + + + + + + + + + + + + + + + Some tests failed; see details above. + + + + + + + + + Must select some files in the IDE or set test.includes + + + + Some tests failed; see details above. + + + + Must select some files in the IDE or set test.class + Must select some method in the IDE or set test.method + + + + Some tests failed; see details above. + + + + + Must select one file in the IDE or set test.class + + + + Must select one file in the IDE or set test.class + Must select some method in the IDE or set test.method + + + + + + + + + + + + + + Must select one file in the IDE or set applet.url + + + + + + + + + Must select one file in the IDE or set applet.url + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/tools/src/maple_loader/nbproject/genfiles.properties b/tools/linux/src/maple_loader/nbproject/genfiles.properties similarity index 98% rename from tools/src/maple_loader/nbproject/genfiles.properties rename to tools/linux/src/maple_loader/nbproject/genfiles.properties index 1007bfc..c136721 100644 --- a/tools/src/maple_loader/nbproject/genfiles.properties +++ b/tools/linux/src/maple_loader/nbproject/genfiles.properties @@ -1,8 +1,8 @@ -build.xml.data.CRC32=2e6a03ba -build.xml.script.CRC32=4676ee6b -build.xml.stylesheet.CRC32=8064a381@1.75.2.48 -# This file is used by a NetBeans-based IDE to track changes in generated files such as build-impl.xml. -# Do not edit this file. You may delete it but then the IDE will never regenerate such files for you. -nbproject/build-impl.xml.data.CRC32=2e6a03ba -nbproject/build-impl.xml.script.CRC32=392b3f79 -nbproject/build-impl.xml.stylesheet.CRC32=876e7a8f@1.75.2.48 +build.xml.data.CRC32=2e6a03ba +build.xml.script.CRC32=4676ee6b +build.xml.stylesheet.CRC32=8064a381@1.75.2.48 +# This file is used by a NetBeans-based IDE to track changes in generated files such as build-impl.xml. +# Do not edit this file. You may delete it but then the IDE will never regenerate such files for you. +nbproject/build-impl.xml.data.CRC32=2e6a03ba +nbproject/build-impl.xml.script.CRC32=392b3f79 +nbproject/build-impl.xml.stylesheet.CRC32=876e7a8f@1.75.2.48 diff --git a/tools/src/maple_loader/nbproject/private/config.properties b/tools/linux/src/maple_loader/nbproject/private/config.properties similarity index 100% rename from tools/src/maple_loader/nbproject/private/config.properties rename to tools/linux/src/maple_loader/nbproject/private/config.properties diff --git a/tools/src/maple_loader/nbproject/private/private.properties b/tools/linux/src/maple_loader/nbproject/private/private.properties similarity index 96% rename from tools/src/maple_loader/nbproject/private/private.properties rename to tools/linux/src/maple_loader/nbproject/private/private.properties index ab05185..e5c9f10 100644 --- a/tools/src/maple_loader/nbproject/private/private.properties +++ b/tools/linux/src/maple_loader/nbproject/private/private.properties @@ -1,6 +1,6 @@ -compile.on.save=true -do.depend=false -do.jar=true -javac.debug=true -javadoc.preview=true -user.properties.file=C:\\Users\\rclark\\AppData\\Roaming\\NetBeans\\8.0.2\\build.properties +compile.on.save=true +do.depend=false +do.jar=true +javac.debug=true +javadoc.preview=true +user.properties.file=C:\\Users\\rclark\\AppData\\Roaming\\NetBeans\\8.0.2\\build.properties diff --git a/tools/src/maple_loader/nbproject/private/private.xml b/tools/linux/src/maple_loader/nbproject/private/private.xml similarity index 98% rename from tools/src/maple_loader/nbproject/private/private.xml rename to tools/linux/src/maple_loader/nbproject/private/private.xml index 9feb3fe..a1bbd60 100644 --- a/tools/src/maple_loader/nbproject/private/private.xml +++ b/tools/linux/src/maple_loader/nbproject/private/private.xml @@ -1,10 +1,10 @@ - - - - - - file:/C:/Users/rclark/Desktop/maple-asp-master/installer/maple_loader/src/CliTemplate/CliMain.java - file:/C:/Users/rclark/Desktop/maple-asp-master/installer/maple_loader/src/CliTemplate/DFUUploader.java - - - + + + + + + file:/C:/Users/rclark/Desktop/maple-asp-master/installer/maple_loader/src/CliTemplate/CliMain.java + file:/C:/Users/rclark/Desktop/maple-asp-master/installer/maple_loader/src/CliTemplate/DFUUploader.java + + + diff --git a/tools/src/maple_loader/nbproject/project.properties b/tools/linux/src/maple_loader/nbproject/project.properties similarity index 96% rename from tools/src/maple_loader/nbproject/project.properties rename to tools/linux/src/maple_loader/nbproject/project.properties index 984cc6a..7f48d71 100644 --- a/tools/src/maple_loader/nbproject/project.properties +++ b/tools/linux/src/maple_loader/nbproject/project.properties @@ -1,79 +1,79 @@ -annotation.processing.enabled=true -annotation.processing.enabled.in.editor=false -annotation.processing.processors.list= -annotation.processing.run.all.processors=true -annotation.processing.source.output=${build.generated.sources.dir}/ap-source-output -application.title=maple_loader -application.vendor=bob -build.classes.dir=${build.dir}/classes -build.classes.excludes=**/*.java,**/*.form -# This directory is removed when the project is cleaned: -build.dir=build -build.generated.dir=${build.dir}/generated -build.generated.sources.dir=${build.dir}/generated-sources -# Only compile against the classpath explicitly listed here: -build.sysclasspath=ignore -build.test.classes.dir=${build.dir}/test/classes -build.test.results.dir=${build.dir}/test/results -# Uncomment to specify the preferred debugger connection transport: -#debug.transport=dt_socket -debug.classpath=\ - ${run.classpath} -debug.test.classpath=\ - ${run.test.classpath} -# Files in build.classes.dir which should be excluded from distribution jar -dist.archive.excludes= -# This directory is removed when the project is cleaned: -dist.dir=dist -dist.jar=${dist.dir}/maple_loader.jar -dist.javadoc.dir=${dist.dir}/javadoc -endorsed.classpath= -excludes= -file.reference.jssc.jar=dist/lib/jssc.jar -file.reference.jssc.jar-1=jars/jssc.jar -includes=** -jar.compress=false -javac.classpath=\ - ${file.reference.jssc.jar}:\ - ${file.reference.jssc.jar-1} -# Space-separated list of extra javac options -javac.compilerargs= -javac.deprecation=false -javac.processorpath=\ - ${javac.classpath} -javac.source=1.7 -javac.target=1.7 -javac.test.classpath=\ - ${javac.classpath}:\ - ${build.classes.dir} -javac.test.processorpath=\ - ${javac.test.classpath} -javadoc.additionalparam= -javadoc.author=false -javadoc.encoding=${source.encoding} -javadoc.noindex=false -javadoc.nonavbar=false -javadoc.notree=false -javadoc.private=false -javadoc.splitindex=true -javadoc.use=true -javadoc.version=false -javadoc.windowtitle= -main.class=CliTemplate.CliMain -manifest.file=manifest.mf -meta.inf.dir=${src.dir}/META-INF -mkdist.disabled=false -platform.active=default_platform -run.classpath=\ - ${javac.classpath}:\ - ${build.classes.dir} -# Space-separated list of JVM arguments used when running the project. -# You may also define separate properties like run-sys-prop.name=value instead of -Dname=value. -# To set system properties for unit tests define test-sys-prop.name=value: -run.jvmargs= -run.test.classpath=\ - ${javac.test.classpath}:\ - ${build.test.classes.dir} -source.encoding=UTF-8 -src.dir=src -test.src.dir=test +annotation.processing.enabled=true +annotation.processing.enabled.in.editor=false +annotation.processing.processors.list= +annotation.processing.run.all.processors=true +annotation.processing.source.output=${build.generated.sources.dir}/ap-source-output +application.title=maple_loader +application.vendor=bob +build.classes.dir=${build.dir}/classes +build.classes.excludes=**/*.java,**/*.form +# This directory is removed when the project is cleaned: +build.dir=build +build.generated.dir=${build.dir}/generated +build.generated.sources.dir=${build.dir}/generated-sources +# Only compile against the classpath explicitly listed here: +build.sysclasspath=ignore +build.test.classes.dir=${build.dir}/test/classes +build.test.results.dir=${build.dir}/test/results +# Uncomment to specify the preferred debugger connection transport: +#debug.transport=dt_socket +debug.classpath=\ + ${run.classpath} +debug.test.classpath=\ + ${run.test.classpath} +# Files in build.classes.dir which should be excluded from distribution jar +dist.archive.excludes= +# This directory is removed when the project is cleaned: +dist.dir=dist +dist.jar=${dist.dir}/maple_loader.jar +dist.javadoc.dir=${dist.dir}/javadoc +endorsed.classpath= +excludes= +file.reference.jssc.jar=dist/lib/jssc.jar +file.reference.jssc.jar-1=jars/jssc.jar +includes=** +jar.compress=false +javac.classpath=\ + ${file.reference.jssc.jar}:\ + ${file.reference.jssc.jar-1} +# Space-separated list of extra javac options +javac.compilerargs= +javac.deprecation=false +javac.processorpath=\ + ${javac.classpath} +javac.source=1.7 +javac.target=1.7 +javac.test.classpath=\ + ${javac.classpath}:\ + ${build.classes.dir} +javac.test.processorpath=\ + ${javac.test.classpath} +javadoc.additionalparam= +javadoc.author=false +javadoc.encoding=${source.encoding} +javadoc.noindex=false +javadoc.nonavbar=false +javadoc.notree=false +javadoc.private=false +javadoc.splitindex=true +javadoc.use=true +javadoc.version=false +javadoc.windowtitle= +main.class=CliTemplate.CliMain +manifest.file=manifest.mf +meta.inf.dir=${src.dir}/META-INF +mkdist.disabled=false +platform.active=default_platform +run.classpath=\ + ${javac.classpath}:\ + ${build.classes.dir} +# Space-separated list of JVM arguments used when running the project. +# You may also define separate properties like run-sys-prop.name=value instead of -Dname=value. +# To set system properties for unit tests define test-sys-prop.name=value: +run.jvmargs= +run.test.classpath=\ + ${javac.test.classpath}:\ + ${build.test.classes.dir} +source.encoding=UTF-8 +src.dir=src +test.src.dir=test diff --git a/tools/src/maple_loader/nbproject/project.xml b/tools/linux/src/maple_loader/nbproject/project.xml similarity index 97% rename from tools/src/maple_loader/nbproject/project.xml rename to tools/linux/src/maple_loader/nbproject/project.xml index 076fe22..92218a9 100644 --- a/tools/src/maple_loader/nbproject/project.xml +++ b/tools/linux/src/maple_loader/nbproject/project.xml @@ -1,15 +1,15 @@ - - - org.netbeans.modules.java.j2seproject - - - maple_loader - - - - - - - - - + + + org.netbeans.modules.java.j2seproject + + + maple_loader + + + + + + + + + diff --git a/tools/src/maple_loader/src/CliTemplate/CliMain.java b/tools/linux/src/maple_loader/src/CliTemplate/CliMain.java similarity index 100% rename from tools/src/maple_loader/src/CliTemplate/CliMain.java rename to tools/linux/src/maple_loader/src/CliTemplate/CliMain.java diff --git a/tools/src/maple_loader/src/CliTemplate/DFUUploader.java b/tools/linux/src/maple_loader/src/CliTemplate/DFUUploader.java similarity index 100% rename from tools/src/maple_loader/src/CliTemplate/DFUUploader.java rename to tools/linux/src/maple_loader/src/CliTemplate/DFUUploader.java diff --git a/tools/src/maple_loader/src/CliTemplate/ExecCommand.java b/tools/linux/src/maple_loader/src/CliTemplate/ExecCommand.java similarity index 100% rename from tools/src/maple_loader/src/CliTemplate/ExecCommand.java rename to tools/linux/src/maple_loader/src/CliTemplate/ExecCommand.java diff --git a/tools/src/maple_loader/src/processing/app/Base.java b/tools/linux/src/maple_loader/src/processing/app/Base.java similarity index 100% rename from tools/src/maple_loader/src/processing/app/Base.java rename to tools/linux/src/maple_loader/src/processing/app/Base.java diff --git a/tools/src/maple_loader/src/processing/app/Preferences.java b/tools/linux/src/maple_loader/src/processing/app/Preferences.java similarity index 100% rename from tools/src/maple_loader/src/processing/app/Preferences.java rename to tools/linux/src/maple_loader/src/processing/app/Preferences.java diff --git a/tools/src/maple_loader/src/processing/app/Serial.java b/tools/linux/src/maple_loader/src/processing/app/Serial.java similarity index 100% rename from tools/src/maple_loader/src/processing/app/Serial.java rename to tools/linux/src/maple_loader/src/processing/app/Serial.java diff --git a/tools/src/maple_loader/src/processing/app/SerialException.java b/tools/linux/src/maple_loader/src/processing/app/SerialException.java similarity index 100% rename from tools/src/maple_loader/src/processing/app/SerialException.java rename to tools/linux/src/maple_loader/src/processing/app/SerialException.java diff --git a/tools/src/maple_loader/src/processing/app/debug/MessageConsumer.java b/tools/linux/src/maple_loader/src/processing/app/debug/MessageConsumer.java similarity index 100% rename from tools/src/maple_loader/src/processing/app/debug/MessageConsumer.java rename to tools/linux/src/maple_loader/src/processing/app/debug/MessageConsumer.java diff --git a/tools/src/maple_loader/src/processing/app/debug/MessageSiphon.java b/tools/linux/src/maple_loader/src/processing/app/debug/MessageSiphon.java similarity index 100% rename from tools/src/maple_loader/src/processing/app/debug/MessageSiphon.java rename to tools/linux/src/maple_loader/src/processing/app/debug/MessageSiphon.java diff --git a/tools/src/maple_loader/src/processing/app/debug/RunnerException.java b/tools/linux/src/maple_loader/src/processing/app/debug/RunnerException.java similarity index 100% rename from tools/src/maple_loader/src/processing/app/debug/RunnerException.java rename to tools/linux/src/maple_loader/src/processing/app/debug/RunnerException.java diff --git a/tools/src/maple_loader/src/processing/app/helpers/ProcessUtils.java b/tools/linux/src/maple_loader/src/processing/app/helpers/ProcessUtils.java similarity index 100% rename from tools/src/maple_loader/src/processing/app/helpers/ProcessUtils.java rename to tools/linux/src/maple_loader/src/processing/app/helpers/ProcessUtils.java diff --git a/tools/src/stm32flash_serial/src/AUTHORS b/tools/linux/src/stm32flash_serial/src/AUTHORS similarity index 100% rename from tools/src/stm32flash_serial/src/AUTHORS rename to tools/linux/src/stm32flash_serial/src/AUTHORS diff --git a/tools/src/stm32flash_serial/src/Android.mk b/tools/linux/src/stm32flash_serial/src/Android.mk similarity index 100% rename from tools/src/stm32flash_serial/src/Android.mk rename to tools/linux/src/stm32flash_serial/src/Android.mk diff --git a/tools/src/stm32flash_serial/src/HOWTO b/tools/linux/src/stm32flash_serial/src/HOWTO similarity index 100% rename from tools/src/stm32flash_serial/src/HOWTO rename to tools/linux/src/stm32flash_serial/src/HOWTO diff --git a/tools/src/stm32flash_serial/src/I2C.txt b/tools/linux/src/stm32flash_serial/src/I2C.txt similarity index 100% rename from tools/src/stm32flash_serial/src/I2C.txt rename to tools/linux/src/stm32flash_serial/src/I2C.txt diff --git a/tools/src/stm32flash_serial/src/Makefile b/tools/linux/src/stm32flash_serial/src/Makefile similarity index 100% rename from tools/src/stm32flash_serial/src/Makefile rename to tools/linux/src/stm32flash_serial/src/Makefile diff --git a/tools/src/stm32flash_serial/src/TODO b/tools/linux/src/stm32flash_serial/src/TODO similarity index 100% rename from tools/src/stm32flash_serial/src/TODO rename to tools/linux/src/stm32flash_serial/src/TODO diff --git a/tools/src/stm32flash_serial/src/dev_table.c b/tools/linux/src/stm32flash_serial/src/dev_table.c similarity index 100% rename from tools/src/stm32flash_serial/src/dev_table.c rename to tools/linux/src/stm32flash_serial/src/dev_table.c diff --git a/tools/src/stm32flash_serial/src/gpl-2.0.txt b/tools/linux/src/stm32flash_serial/src/gpl-2.0.txt similarity index 100% rename from tools/src/stm32flash_serial/src/gpl-2.0.txt rename to tools/linux/src/stm32flash_serial/src/gpl-2.0.txt diff --git a/tools/src/stm32flash_serial/src/i2c.c b/tools/linux/src/stm32flash_serial/src/i2c.c similarity index 100% rename from tools/src/stm32flash_serial/src/i2c.c rename to tools/linux/src/stm32flash_serial/src/i2c.c diff --git a/tools/src/stm32flash_serial/src/init.c b/tools/linux/src/stm32flash_serial/src/init.c similarity index 100% rename from tools/src/stm32flash_serial/src/init.c rename to tools/linux/src/stm32flash_serial/src/init.c diff --git a/tools/src/stm32flash_serial/src/init.h b/tools/linux/src/stm32flash_serial/src/init.h similarity index 100% rename from tools/src/stm32flash_serial/src/init.h rename to tools/linux/src/stm32flash_serial/src/init.h diff --git a/tools/src/stm32flash_serial/src/main.c b/tools/linux/src/stm32flash_serial/src/main.c similarity index 100% rename from tools/src/stm32flash_serial/src/main.c rename to tools/linux/src/stm32flash_serial/src/main.c diff --git a/tools/src/stm32flash_serial/src/parsers/Android.mk b/tools/linux/src/stm32flash_serial/src/parsers/Android.mk similarity index 100% rename from tools/src/stm32flash_serial/src/parsers/Android.mk rename to tools/linux/src/stm32flash_serial/src/parsers/Android.mk diff --git a/tools/src/stm32flash_serial/src/parsers/Makefile b/tools/linux/src/stm32flash_serial/src/parsers/Makefile similarity index 100% rename from tools/src/stm32flash_serial/src/parsers/Makefile rename to tools/linux/src/stm32flash_serial/src/parsers/Makefile diff --git a/tools/src/stm32flash_serial/src/parsers/binary.c b/tools/linux/src/stm32flash_serial/src/parsers/binary.c similarity index 100% rename from tools/src/stm32flash_serial/src/parsers/binary.c rename to tools/linux/src/stm32flash_serial/src/parsers/binary.c diff --git a/tools/src/stm32flash_serial/src/parsers/binary.h b/tools/linux/src/stm32flash_serial/src/parsers/binary.h similarity index 100% rename from tools/src/stm32flash_serial/src/parsers/binary.h rename to tools/linux/src/stm32flash_serial/src/parsers/binary.h diff --git a/tools/src/stm32flash_serial/src/parsers/hex.c b/tools/linux/src/stm32flash_serial/src/parsers/hex.c similarity index 100% rename from tools/src/stm32flash_serial/src/parsers/hex.c rename to tools/linux/src/stm32flash_serial/src/parsers/hex.c diff --git a/tools/src/stm32flash_serial/src/parsers/hex.h b/tools/linux/src/stm32flash_serial/src/parsers/hex.h similarity index 100% rename from tools/src/stm32flash_serial/src/parsers/hex.h rename to tools/linux/src/stm32flash_serial/src/parsers/hex.h diff --git a/tools/src/stm32flash_serial/src/parsers/parser.h b/tools/linux/src/stm32flash_serial/src/parsers/parser.h similarity index 100% rename from tools/src/stm32flash_serial/src/parsers/parser.h rename to tools/linux/src/stm32flash_serial/src/parsers/parser.h diff --git a/tools/src/stm32flash_serial/src/port.c b/tools/linux/src/stm32flash_serial/src/port.c similarity index 100% rename from tools/src/stm32flash_serial/src/port.c rename to tools/linux/src/stm32flash_serial/src/port.c diff --git a/tools/src/stm32flash_serial/src/port.h b/tools/linux/src/stm32flash_serial/src/port.h similarity index 100% rename from tools/src/stm32flash_serial/src/port.h rename to tools/linux/src/stm32flash_serial/src/port.h diff --git a/tools/src/stm32flash_serial/src/protocol.txt b/tools/linux/src/stm32flash_serial/src/protocol.txt similarity index 100% rename from tools/src/stm32flash_serial/src/protocol.txt rename to tools/linux/src/stm32flash_serial/src/protocol.txt diff --git a/tools/src/stm32flash_serial/src/serial.h b/tools/linux/src/stm32flash_serial/src/serial.h similarity index 100% rename from tools/src/stm32flash_serial/src/serial.h rename to tools/linux/src/stm32flash_serial/src/serial.h diff --git a/tools/src/stm32flash_serial/src/serial_common.c b/tools/linux/src/stm32flash_serial/src/serial_common.c similarity index 100% rename from tools/src/stm32flash_serial/src/serial_common.c rename to tools/linux/src/stm32flash_serial/src/serial_common.c diff --git a/tools/src/stm32flash_serial/src/serial_platform.c b/tools/linux/src/stm32flash_serial/src/serial_platform.c similarity index 100% rename from tools/src/stm32flash_serial/src/serial_platform.c rename to tools/linux/src/stm32flash_serial/src/serial_platform.c diff --git a/tools/src/stm32flash_serial/src/serial_posix.c b/tools/linux/src/stm32flash_serial/src/serial_posix.c similarity index 100% rename from tools/src/stm32flash_serial/src/serial_posix.c rename to tools/linux/src/stm32flash_serial/src/serial_posix.c diff --git a/tools/src/stm32flash_serial/src/serial_w32.c b/tools/linux/src/stm32flash_serial/src/serial_w32.c similarity index 100% rename from tools/src/stm32flash_serial/src/serial_w32.c rename to tools/linux/src/stm32flash_serial/src/serial_w32.c diff --git a/tools/src/stm32flash_serial/src/stm32.c b/tools/linux/src/stm32flash_serial/src/stm32.c similarity index 100% rename from tools/src/stm32flash_serial/src/stm32.c rename to tools/linux/src/stm32flash_serial/src/stm32.c diff --git a/tools/src/stm32flash_serial/src/stm32.h b/tools/linux/src/stm32flash_serial/src/stm32.h similarity index 100% rename from tools/src/stm32flash_serial/src/stm32.h rename to tools/linux/src/stm32flash_serial/src/stm32.h diff --git a/tools/src/stm32flash_serial/src/stm32flash.1 b/tools/linux/src/stm32flash_serial/src/stm32flash.1 similarity index 100% rename from tools/src/stm32flash_serial/src/stm32flash.1 rename to tools/linux/src/stm32flash_serial/src/stm32flash.1 diff --git a/tools/src/stm32flash_serial/src/utils.c b/tools/linux/src/stm32flash_serial/src/utils.c similarity index 100% rename from tools/src/stm32flash_serial/src/utils.c rename to tools/linux/src/stm32flash_serial/src/utils.c diff --git a/tools/src/stm32flash_serial/src/utils.h b/tools/linux/src/stm32flash_serial/src/utils.h similarity index 100% rename from tools/src/stm32flash_serial/src/utils.h rename to tools/linux/src/stm32flash_serial/src/utils.h diff --git a/tools/src/upload-reset/upload-reset.c b/tools/linux/src/upload-reset/upload-reset.c similarity index 100% rename from tools/src/upload-reset/upload-reset.c rename to tools/linux/src/upload-reset/upload-reset.c diff --git a/tools/linux64/45-maple.rules b/tools/linux64/45-maple.rules new file mode 100644 index 0000000..d1bda5f --- /dev/null +++ b/tools/linux64/45-maple.rules @@ -0,0 +1,5 @@ +ATTRS{idProduct}=="1001", ATTRS{idVendor}=="0110", MODE="664", GROUP="plugdev" +ATTRS{idProduct}=="1002", ATTRS{idVendor}=="0110", MODE="664", GROUP="plugdev" +ATTRS{idProduct}=="0003", ATTRS{idVendor}=="1eaf", MODE="664", GROUP="plugdev" SYMLINK+="maple" +ATTRS{idProduct}=="0004", ATTRS{idVendor}=="1eaf", MODE="664", GROUP="plugdev" SYMLINK+="maple" + diff --git a/tools/linux64/49-stlinkv1.rules b/tools/linux64/49-stlinkv1.rules new file mode 100644 index 0000000..d474d6a --- /dev/null +++ b/tools/linux64/49-stlinkv1.rules @@ -0,0 +1,11 @@ +# stm32 discovery boards, with onboard st/linkv1 +# ie, STM32VL + +SUBSYSTEMS=="usb", ATTRS{idVendor}=="0483", ATTRS{idProduct}=="3744", \ + MODE:="0666", \ + SYMLINK+="stlinkv1_%n" + +# If you share your linux system with other users, or just don't like the +# idea of write permission for everybody, you can replace MODE:="0666" with +# OWNER:="yourusername" to create the device owned by you, or with +# GROUP:="somegroupname" and mange access using standard unix groups. diff --git a/tools/linux64/49-stlinkv2-1.rules b/tools/linux64/49-stlinkv2-1.rules new file mode 100644 index 0000000..a5a79b9 --- /dev/null +++ b/tools/linux64/49-stlinkv2-1.rules @@ -0,0 +1,12 @@ +# stm32 nucleo boards, with onboard st/linkv2-1 +# ie, STM32F0, STM32F4. +# STM32VL has st/linkv1, which is quite different + +SUBSYSTEMS=="usb", ATTRS{idVendor}=="0483", ATTRS{idProduct}=="374b", \ + MODE:="0666", \ + SYMLINK+="stlinkv2-1_%n" + +# If you share your linux system with other users, or just don't like the +# idea of write permission for everybody, you can replace MODE:="0666" with +# OWNER:="yourusername" to create the device owned by you, or with +# GROUP:="somegroupname" and mange access using standard unix groups. diff --git a/tools/linux64/49-stlinkv2.rules b/tools/linux64/49-stlinkv2.rules new file mode 100644 index 0000000..a11215c --- /dev/null +++ b/tools/linux64/49-stlinkv2.rules @@ -0,0 +1,12 @@ +# stm32 discovery boards, with onboard st/linkv2 +# ie, STM32L, STM32F4. +# STM32VL has st/linkv1, which is quite different + +SUBSYSTEMS=="usb", ATTRS{idVendor}=="0483", ATTRS{idProduct}=="3748", \ + MODE:="0666", \ + SYMLINK+="stlinkv2_%n" + +# If you share your linux system with other users, or just don't like the +# idea of write permission for everybody, you can replace MODE:="0666" with +# OWNER:="yourusername" to create the device owned by you, or with +# GROUP:="somegroupname" and mange access using standard unix groups. diff --git a/tools/linux64/dfu-util/dfu-prefix b/tools/linux64/dfu-util/dfu-prefix new file mode 100644 index 0000000..88e2d04 Binary files /dev/null and b/tools/linux64/dfu-util/dfu-prefix differ diff --git a/tools/linux64/dfu-util/dfu-suffix b/tools/linux64/dfu-util/dfu-suffix new file mode 100644 index 0000000..e96df29 Binary files /dev/null and b/tools/linux64/dfu-util/dfu-suffix differ diff --git a/tools/linux64/dfu-util/dfu-util b/tools/linux64/dfu-util/dfu-util new file mode 100644 index 0000000..e9a9599 Binary files /dev/null and b/tools/linux64/dfu-util/dfu-util differ diff --git a/tools/linux64/install.sh b/tools/linux64/install.sh new file mode 100644 index 0000000..29ddb2f --- /dev/null +++ b/tools/linux64/install.sh @@ -0,0 +1,24 @@ +#!/bin/sh + +if sudo [ -w /etc/udev/rules.d ]; then + echo "Copying Maple-specific udev rules..." + sudo cp -v 45-maple.rules /etc/udev/rules.d/45-maple.rules + sudo chown root:root /etc/udev/rules.d/45-maple.rules + sudo chmod 644 /etc/udev/rules.d/45-maple.rules + sudo cp -v 49-stlinkv1.rules /etc/udev/rules.d/49-stlinkv1.rules + sudo chown root:root /etc/udev/rules.d/49-stlinkv1.rules + sudo chmod 644 /etc/udev/rules.d/49-stlinkv1.rules + sudo cp -v 49-stlinkv2.rules /etc/udev/rules.d/49-stlinkv2.rules + sudo chown root:root /etc/udev/rules.d/49-stlinkv2.rules + sudo chmod 644 /etc/udev/rules.d/49-stlinkv2.rules + sudo cp -v 49-stlinkv2-1.rules /etc/udev/rules.d/49-stlinkv2-1.rules + sudo chown root:root /etc/udev/rules.d/49-stlinkv2-1.rules + sudo chmod 644 /etc/udev/rules.d/49-stlinkv2-1.rules + echo "Reloading udev rules" + sudo udevadm control --reload-rules + echo "Adding current user to dialout group" + sudo adduser $USER dialout +else + echo "Couldn't copy to /etc/udev/rules.d/; you probably have to run this script as root? Or your distribution of Linux doesn't include udev; try running the IDE itself as root." +fi + diff --git a/tools/linux64/maple_upload b/tools/linux64/maple_upload new file mode 100644 index 0000000..e799f3a --- /dev/null +++ b/tools/linux64/maple_upload @@ -0,0 +1,40 @@ +#!/bin/bash + +#set -e + + + +if [ $# -lt 4 ]; then + echo "Usage: $0 $# " >&2 + exit 1 +fi +dummy_port="$1"; altID="$2"; usbID="$3"; binfile="$4"; dummy_port_fullpath="/dev/$1" +if [ $# -eq 5 ]; then + dfuse_addr="--dfuse-address $5" +else + dfuse_addr="" +fi + + +# Get the directory where the script is running. +DIR=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd ) + +# ----------------- IMPORTANT ----------------- +# The 2nd parameter to upload-reset is the delay after resetting before it exits +# This value is in milliseonds +# You may need to tune this to your system +# 750ms to 1500ms seems to work on my Mac + + +"${DIR}/upload-reset" ${dummy_port_fullpath} 750 + + +#DFU_UTIL=$(dirname $0)/dfu-util/dfu-util +DFU_UTIL=/usr/bin/dfu-util +DFU_UTIL=${DIR}/dfu-util/dfu-util +if [ ! -x "${DFU_UTIL}" ]; then + echo "$0: error: cannot find ${DFU_UTIL}" >&2 + exit 2 +fi + +"${DFU_UTIL}" -d ${usbID} -a ${altID} -D ${binfile} ${dfuse_addr} -R diff --git a/tools/linux64/readme.txt b/tools/linux64/readme.txt new file mode 100644 index 0000000..2d13beb --- /dev/null +++ b/tools/linux64/readme.txt @@ -0,0 +1 @@ +The maple upload script needs its rights to be set to 755 \ No newline at end of file diff --git a/tools/linux64/serial_upload b/tools/linux64/serial_upload new file mode 100644 index 0000000..05d17c6 --- /dev/null +++ b/tools/linux64/serial_upload @@ -0,0 +1,2 @@ +#!/bin/bash +$(dirname $0)/stm32flash/stm32flash -g 0x8000000 -b 230400 -w "$4" /dev/"$1" diff --git a/tools/linux64/src/build_dfu-util.sh b/tools/linux64/src/build_dfu-util.sh new file mode 100644 index 0000000..3563f57 --- /dev/null +++ b/tools/linux64/src/build_dfu-util.sh @@ -0,0 +1,15 @@ +#!/bin/bash + +sudo apt-get build-dep dfu-util +sudo apt-get install build-essentials +sudo apt-get install libusb-1.0-0-dev +sudo apt-get install autoconf automake autotools-dev + +cd dfu-util +./autogen.sh +./configure +make +cp src/dfu-util ../../linux/dfu-util +cp src/dfu-suffix ../../linux/dfu-util +cp src/dfu-prefix ../../linux/dfu-util + diff --git a/tools/linux64/src/dfu-util/AUTHORS b/tools/linux64/src/dfu-util/AUTHORS new file mode 100644 index 0000000..1b36c73 --- /dev/null +++ b/tools/linux64/src/dfu-util/AUTHORS @@ -0,0 +1,30 @@ +Authors ordered by first contribution. + +Harald Welte +Werner Almesberger +Michael Lauer +Jim Huang +Stefan Schmidt +Daniel Willmann +Mike Frysinger +Uwe Hermann +C. Scott Ananian +Bernard Blackham +Holger Freyther +Marc Singer +James Perkins +Tommi Keisala +Pascal Schweizer +Bradley Scott +Uwe Bonnes +Andrey Smirnov +Jussi Timperi +Hans Petter Selasky +Bo Shen +Henrique de Almeida Mendonca +Bernd Krumboeck +Dennis Meier +Veli-Pekka Peltola +Dave Hylands +Michael Grzeschik +Paul Fertser diff --git a/tools/linux64/src/dfu-util/COPYING b/tools/linux64/src/dfu-util/COPYING new file mode 100644 index 0000000..d60c31a --- /dev/null +++ b/tools/linux64/src/dfu-util/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. + + + Copyright (C) + + 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. + + , 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/tools/linux64/src/dfu-util/ChangeLog b/tools/linux64/src/dfu-util/ChangeLog new file mode 100644 index 0000000..37f1add --- /dev/null +++ b/tools/linux64/src/dfu-util/ChangeLog @@ -0,0 +1,93 @@ +0.8: + o New, separate dfu-prefix tool (Uwe Bonnes) + o Allow filtering on serial number (Uwe Bonnes) + o Improved VID/PID/serial filtering (Bradley Scott) + o Support reading firmware from stdin (Tormod Volden) + o Warn if missing DFU suffix (Tormod Volden) + o Improved progress bar (Hans Petter Selasky) + o Fix dfuse leave option (Uwe Bonnes) + o Major code rework (Hans Petter Selasky) + o MS Visual Studio build support (Henrique Mendonca) + o dfuse-pack.py tool for .dfu files (Antonio Galeo) + o Many other fixes from many people + +2014-09-13: Tormod Volden + +0.7: + o Support for TI Stellaris devices (Tommi Keisala) + o Fix libusb detection on MacOSX (Marc Singer) + o Fix libusb detection on FreeBSD (Tormod Volden) + o Improved DfuSe support (Tormod Volden) + o Support all special commands (leave, unprotect, mass-erase) + o Arbitrary upload lengths + o "force" option for various possible (dangerous) overrides + +2012-10-07: Tormod Volden + +0.6: + o Add detach mode (Stefan Schmidt) + o Check return value on all libusb calls (Tormod Volden) + o Fix segmentation fault with -s option (Tormod Volden) + o Add DFU suffix manipulation tool (Stefan Schmidt) + o Port to Windows: (Tormod Volden, some parts based on work from Satz + Klauer) + o Port file handling to stdio streams + o Sleep() macros + o C99 types + o Pack structs + o Detect DfuSe device correctly on big-endian architectures (Tormod + Volden) + o Add dfuse progress indication on download (Tormod Volden) + o Cleanup: gcc pedantic, gcc extension, ... (Tormod Volden) + o Rely on page size from functional descriptor. Please report if you get + an error about it. (Tormod Volden) + o Add quirk for Maple since it reports wrong DFU version (Tormod Volden) + +2012-04-22: Stefan Schmidt + +0.5: + o DfuSe extension support for ST devices (Tormod Volden) + o Add initial support for bitWillDetach flag from DFU 1.1 (Tormod + Volden) + o Internal cleanup and some manual page fixes (Tormod Volden) + +2011-11-02: Stefan Schmidt + +0.4: + o Rework to use libusb-1.0 (Stefan Schmidt) + o DFU suffix support (Tormod Volden, Stefan Schmidt) + o Sspeed up DFU downloads directly into memory (Bernard Blackham) + o More flexible -d vid:pid parsing (Tormod Volden) + o Many bug fixes and cleanups + +2011-07-20: Stefan Schmidt + +0.3: + o quirks: Add OpenOCD to the poll timeout quirk table. + +2010-12-22: Stefan Schmidt + +0.2: + o Fix some typos on the website and the README (Antonio Ospite, Uwe + Hermann) + o Remove build rule for a static binary. We can use autotools for this. + (Mike Frysinger) + o Fix infinite loop in download error path (C. Scott Ananian) + o Break out to show the 'finished' in upload (C. Scott Ananian) + o Add GPLv2+ headers (Harald Welte) + o Remove dead code (commands.[ch]) remnescent of dfu-programmer (Harald + Welte) + o Simple quirk system with Openmoko quirk for missing bwPollTimeout (Tormod Volden) + o New default (1024) and clamping of transfer size (Tormod Volden) + o Verify sending of completion packet (Tormod Volden) + o Look for DFU functional descriptor among all descriptors (Tormod + Volden) + o Print out in which direction we are transferring data + o Abort in upload if the file already exists + +2010-11-17 Stefan Schmidt + +0.1: + Initial release + +2010-05-23 Stefan Schmidt diff --git a/tools/linux64/src/dfu-util/DEVICES.txt b/tools/linux64/src/dfu-util/DEVICES.txt new file mode 100644 index 0000000..bdd9f1f --- /dev/null +++ b/tools/linux64/src/dfu-util/DEVICES.txt @@ -0,0 +1,20 @@ +List of supported software and hardware products: + +Software user (bootloader, etc) +------------------------------- +- Sam7DFU: http://www.openpcd.org/Sam7dfu +- U-boot: DFU patches +- Barebox: http://www.barebox.org/ +- Leaflabs: http://code.google.com/p/leaflabs/ +- Blackmagic DFU + +Products using DFU +------------------ +- OpenPCD (sam7dfu) +- Openmoko Neo 1973 and Freerunner (u-boot with DFU patches) +- Leaflabs Maple +- ATUSB from Qi Hardware +- STM32F105/7, STM32F2/F3/F4 in System Bootloader +- Blackmagic debug probe +- NXP LPC31xx/LPC43XX, e.g. LPC-Link and LPC-Link2, need binaries + with LPC prefix and encoding (LPC-Link) diff --git a/tools/linux64/src/dfu-util/Makefile.am b/tools/linux64/src/dfu-util/Makefile.am new file mode 100644 index 0000000..641dda5 --- /dev/null +++ b/tools/linux64/src/dfu-util/Makefile.am @@ -0,0 +1,3 @@ +SUBDIRS = src doc + +EXTRA_DIST = autogen.sh TODO DEVICES.txt dfuse-pack.py diff --git a/tools/linux64/src/dfu-util/README b/tools/linux64/src/dfu-util/README new file mode 100644 index 0000000..0f8f262 --- /dev/null +++ b/tools/linux64/src/dfu-util/README @@ -0,0 +1,20 @@ +Dfu-util - Device Firmware Upgrade Utilities + +Dfu-util is the host side implementation of the DFU 1.0 [1] and DFU 1.1 [2] +specification of the USB forum. + +DFU is intended to download and upload firmware to devices connected over +USB. It ranges from small devices like micro-controller boards up to mobile +phones. With dfu-util you are able to download firmware to your device or +upload firmware from it. + +dfu-util has been tested with Openmoko Neo1973 and Freerunner and many +other devices. + +[1] DFU 1.0 spec: http://www.usb.org/developers/devclass_docs/usbdfu10.pdf +[2] DFU 1.1 spec: http://www.usb.org/developers/devclass_docs/DFU_1.1.pdf + +The official website is: + + http://dfu-util.gnumonks.org/ + diff --git a/tools/linux64/src/dfu-util/TODO b/tools/linux64/src/dfu-util/TODO new file mode 100644 index 0000000..900c30c --- /dev/null +++ b/tools/linux64/src/dfu-util/TODO @@ -0,0 +1,14 @@ +DfuSe: +- Do erase and write in two separate passes when downloading +- Skip "Set Address" command when downloading contiguous blocks +- Implement "Get Commands" command + +Devices: +- Research iPhone/iPod/iPad support + Heavily modified dfu-util fork here: + https://github.com/planetbeing/xpwn/tree/master/dfu-util +- Test against Niftylights + +Non-Code: +- Logo +- Re-License as LGPL for usage as library? diff --git a/tools/linux64/src/dfu-util/autogen.sh b/tools/linux64/src/dfu-util/autogen.sh new file mode 100644 index 0000000..e67aed3 --- /dev/null +++ b/tools/linux64/src/dfu-util/autogen.sh @@ -0,0 +1,2 @@ +#! /bin/sh +autoreconf -v -i diff --git a/tools/linux64/src/dfu-util/configure.ac b/tools/linux64/src/dfu-util/configure.ac new file mode 100644 index 0000000..8622114 --- /dev/null +++ b/tools/linux64/src/dfu-util/configure.ac @@ -0,0 +1,41 @@ +# -*- Autoconf -*- +# Process this file with autoconf to produce a configure script. + +AC_PREREQ(2.59) +AC_INIT([dfu-util],[0.8],[dfu-util@lists.gnumonks.org],,[http://dfu-util.gnumonks.org]) +AC_CONFIG_AUX_DIR(m4) +AM_INIT_AUTOMAKE([foreign]) +AC_CONFIG_HEADERS([config.h]) + +# Test for new silent rules and enable only if they are available +m4_ifdef([AM_SILENT_RULES], [AM_SILENT_RULES([yes])]) + +# Checks for programs. +AC_PROG_CC + +# Checks for libraries. +# On FreeBSD the libusb-1.0 is called libusb and resides in system location +AC_CHECK_LIB([usb], [libusb_init],, [native_libusb=no],) +AS_IF([test x$native_libusb = xno], [ + PKG_CHECK_MODULES([USB], [libusb-1.0 >= 1.0.0],, + AC_MSG_ERROR([*** Required libusb-1.0 >= 1.0.0 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([usbpath.h windows.h sysexits.h]) + +# Checks for typedefs, structures, and compiler characteristics. +AC_C_CONST +AC_TYPE_SIZE_T + +# Checks for library functions. +AC_FUNC_MEMCMP +AC_CHECK_FUNCS([ftruncate getpagesize nanosleep err]) + +AC_CONFIG_FILES(Makefile src/Makefile doc/Makefile) +AC_OUTPUT diff --git a/tools/linux64/src/dfu-util/device-logs/README b/tools/linux64/src/dfu-util/device-logs/README new file mode 100644 index 0000000..00d3d1a --- /dev/null +++ b/tools/linux64/src/dfu-util/device-logs/README @@ -0,0 +1,77 @@ +Device: +------- +qi-hardware-atusb: +- Qi Hardware ben-wpan +- DFU implementation: + http://projects.qi-hardware.com/index.php/p/ben-wpan/source/tree/master/atusb/fw/usb +- Tester: Stefan Schmidt + +openpcd: +- OpenPCD RFID reader +- DFU implementation: SAM7DFU + http://www.openpcd.org/Sam7dfu, git://git.gnumonks.org/openpcd.git +- Tester: Stefan Schmidt + +simtrace: +- Sysmocom SimTrace +- DFU implementation: SAM7DFU + http://www.openpcd.org/Sam7dfu, git://git.gnumonks.org/openpcd.git +- Tester: Stefan Schmidt + +openmoko-freerunner: +- Openmoko Freerunner +- DFU implementation: Old U-Boot + http://git.openmoko.org/?p=u-boot.git;a=shortlog;h=refs/heads/mokopatches +- Tester: Stefan Schmidt + +openmoko-neo1973: +- Openmoko Neo1073 +- DFU implementation: Old U-Boot + http://git.openmoko.org/?p=u-boot.git;a=shortlog;h=refs/heads/mokopatches +- Tester: Stefan Schmidt + +tdk-bluetooth: +- TDK Corp. Bluetooth Adapter +- DFU implementation: closed soure +- Only upload has been tested +- Tester: Stefan Schmidt + +stm32f107: +- STM32 microcontrollers with built-in (ROM) DFU loader +- DFU implementation: Closed source but probably similar to the one + in their USB device libraries. Some relevant application notes: + http://www.st.com -> AN3156 and AN2606 +- Tested by Uwe Bonnes + +stm32f4discovery: +- STM32 microcontroller board with built-in (ROM) DFU loader +- DFU implementation: Closed source, probably similar to stm32f107. +- Tested by Joe Rothweiler + +dso-nano: +- DSO Nano pocket oscilloscope +- DFU implementation: Based on ST Microelectronics USB FS Library 1.0 + http://dsonano.googlecode.com/files/DS0201_OpenSource.rar +- Tester: Tormod Volden + +opc-20: +- Custom devices based on STM32F1xx +- DFU implementation: ST Microelectronics USB FS Device Library 3.1.0 + http://www.st.com -> um0424.zip +- Tester: Tormod Volden + +lpc-link, lpclink2: +- NXP LPCXpresso debug adapters +- Proprietary DFU implementation, uses special download files with + LPC prefix and encoding of the target firmware code +- Tested by Uwe Bonnes + +Adding the lsusb output and a download log of your device here helps +us to avoid regressions for hardware we cannot test while working on +the code. To extract the lsusb output use this command: +sudo lsusb -v -d $USBID > $DEVICE.lsusb +Prepare a description snippet as above, and send it to us. A log +(copy-paste of the command window) of a firmware download is also +nice, please use the double verbose option -v -v and include the +command line in the log file. + diff --git a/tools/linux64/src/dfu-util/device-logs/dsonano.lsusb b/tools/linux64/src/dfu-util/device-logs/dsonano.lsusb new file mode 100644 index 0000000..140a7bc --- /dev/null +++ b/tools/linux64/src/dfu-util/device-logs/dsonano.lsusb @@ -0,0 +1,60 @@ + +Bus 002 Device 004: ID 0483:df11 SGS Thomson Microelectronics +Device Descriptor: + bLength 18 + bDescriptorType 1 + bcdUSB 1.00 + bDeviceClass 0 (Defined at Interface level) + bDeviceSubClass 0 + bDeviceProtocol 0 + bMaxPacketSize0 64 + idVendor 0x0483 SGS Thomson Microelectronics + idProduct 0xdf11 + bcdDevice 1.1a + iManufacturer 1 STMicroelectronics + iProduct 2 STM32 DFU + iSerial 3 001 + bNumConfigurations 1 + Configuration Descriptor: + bLength 9 + bDescriptorType 2 + wTotalLength 36 + bNumInterfaces 1 + bConfigurationValue 1 + iConfiguration 0 + bmAttributes 0x80 + (Bus Powered) + MaxPower 64mA + Interface Descriptor: + bLength 9 + bDescriptorType 4 + bInterfaceNumber 0 + bAlternateSetting 0 + bNumEndpoints 0 + bInterfaceClass 254 Application Specific Interface + bInterfaceSubClass 1 Device Firmware Update + bInterfaceProtocol 0 + iInterface 4 @Internal Flash /0x08000000/12*001Ka,116*001Kg + Interface Descriptor: + bLength 9 + bDescriptorType 4 + bInterfaceNumber 0 + bAlternateSetting 1 + bNumEndpoints 0 + bInterfaceClass 254 Application Specific Interface + bInterfaceSubClass 1 Device Firmware Update + bInterfaceProtocol 0 + iInterface 5 @SPI Flash : M25P64/0x00000000/64*064Kg,64*064Kg + Device Firmware Upgrade Interface Descriptor: + bLength 9 + bDescriptorType 33 + bmAttributes 11 + Will Detach + Manifestation Intolerant + Upload Supported + Download Supported + wDetachTimeout 255 milliseconds + wTransferSize 1024 bytes + bcdDFUVersion 1.1a +Device Status: 0x0000 + (Bus Powered) diff --git a/tools/linux64/src/dfu-util/device-logs/lpclink.log b/tools/linux64/src/dfu-util/device-logs/lpclink.log new file mode 100644 index 0000000..7de4dd3 --- /dev/null +++ b/tools/linux64/src/dfu-util/device-logs/lpclink.log @@ -0,0 +1,59 @@ +(The on-board LPC3154 has some encryption key set and LPCXpressoWIN.enc +is encrypted.) + +$ lsusb | grep NXP +Bus 003 Device 011: ID 0471:df55 Philips (or NXP) LPCXpresso LPC-Link + +$ dfu-util -v -v -v -R -D /opt/lpc/lpcxpresso/bin/LPCXpressoWIN.enc + +dfu-util 0.7 + +Copyright 2005-2008 Weston Schmidt, Harald Welte and OpenMoko Inc. +Copyright 2010-2012 Tormod Volden and Stefan Schmidt +This program is Free Software and has ABSOLUTELY NO WARRANTY +Please report bugs to dfu-util@lists.gnumonks.org + +dfu-util: Invalid DFU suffix signature +dfu-util: A valid DFU suffix will be required in a future dfu-util release!!! +Deducing device DFU version from functional descriptor length +Opening DFU capable USB device... +ID 0471:df55 +Run-time device DFU version 0100 +Claiming USB DFU Runtime Interface... +Determining device status: +state = dfuIDLE, status = 0 +dfu-util: WARNING: Runtime device already in DFU state ?!? +Claiming USB DFU Interface... +Setting Alternate Setting #0 ... +Determining device status: +state = dfuIDLE, status = 0 +dfuIDLE, continuing +DFU mode device DFU version 0100 +Device returned transfer size 2048 +Copying data from PC to DFU device +Download [ ] 0% 0 bytes +Download [= ] 6% 2048 bytes +Download [=== ] 13% 4096 bytes +Download [==== ] 19% 6144 bytes +Download [====== ] 26% 8192 bytes +Download [======== ] 32% 10240 bytes +Download [========= ] 39% 12288 bytes +Download [=========== ] 45% 14336 bytes +Download [============= ] 52% 16384 bytes +Download [============== ] 59% 18432 bytes +Download [================ ] 65% 20480 bytes +Download [================== ] 72% 22528 bytes +Download [=================== ] 78% 24576 bytes +Download [===================== ] 85% 26624 bytes +Download [====================== ] 91% 28672 bytes +Download [======================== ] 98% 29192 bytes +Download [=========================] 100% 29192 bytes +Download done. +Sent a total of 29192 bytes +state(8) = dfuMANIFEST-WAIT-RESET, status(0) = No error condition is present +Done! +dfu-util: can't detach +Resetting USB to switch back to runtime mode + +$ lsusb | grep NXP +Bus 003 Device 012: ID 1fc9:0009 NXP Semiconductors diff --git a/tools/linux64/src/dfu-util/device-logs/lpclink.lsusb b/tools/linux64/src/dfu-util/device-logs/lpclink.lsusb new file mode 100644 index 0000000..867b2a2 --- /dev/null +++ b/tools/linux64/src/dfu-util/device-logs/lpclink.lsusb @@ -0,0 +1,58 @@ + +Bus 003 Device 008: ID 0471:df55 Philips (or NXP) LPCXpresso LPC-Link +Device Descriptor: + bLength 18 + bDescriptorType 1 + bcdUSB 2.00 + bDeviceClass 0 (Defined at Interface level) + bDeviceSubClass 0 + bDeviceProtocol 0 + bMaxPacketSize0 64 + idVendor 0x0471 Philips (or NXP) + idProduct 0xdf55 LPCXpresso LPC-Link + bcdDevice 0.01 + iManufacturer 0 + iProduct 0 + iSerial 0 + bNumConfigurations 1 + Configuration Descriptor: + bLength 9 + bDescriptorType 2 + wTotalLength 25 + bNumInterfaces 1 + bConfigurationValue 1 + iConfiguration 0 + bmAttributes 0x80 + (Bus Powered) + MaxPower 100mA + Interface Descriptor: + bLength 9 + bDescriptorType 4 + bInterfaceNumber 0 + bAlternateSetting 0 + bNumEndpoints 0 + bInterfaceClass 254 Application Specific Interface + bInterfaceSubClass 1 Device Firmware Update + bInterfaceProtocol 0 + iInterface 0 + Device Firmware Upgrade Interface Descriptor: + bLength 7 + bDescriptorType 33 + bmAttributes 1 + Will Not Detach + Manifestation Intolerant + Upload Unsupported + Download Supported + wDetachTimeout 65535 milliseconds + wTransferSize 2048 bytes +Device Qualifier (for other device speed): + bLength 10 + bDescriptorType 6 + bcdUSB 2.00 + bDeviceClass 0 (Defined at Interface level) + bDeviceSubClass 0 + bDeviceProtocol 0 + bMaxPacketSize0 64 + bNumConfigurations 1 +Device Status: 0x0000 + (Bus Powered) diff --git a/tools/linux64/src/dfu-util/device-logs/lpclink2.log b/tools/linux64/src/dfu-util/device-logs/lpclink2.log new file mode 100644 index 0000000..4681eff --- /dev/null +++ b/tools/linux64/src/dfu-util/device-logs/lpclink2.log @@ -0,0 +1,59 @@ +$ lsusb | grep NXP +Bus 003 Device 013: ID 1fc9:000c NXP Semiconductors + +$ dfu-util -D ~/devel/dfu-util/firmware.bin.qthdr + +dfu-util 0.7 + +Copyright 2005-2008 Weston Schmidt, Harald Welte and OpenMoko Inc. +Copyright 2010-2012 Tormod Volden and Stefan Schmidt +This program is Free Software and has ABSOLUTELY NO WARRANTY +Please report bugs to dfu-util@lists.gnumonks.org + +dfu-util: Invalid DFU suffix signature +dfu-util: A valid DFU suffix will be required in a future dfu-util release!!! +Possible unencryptes NXP LPC DFU prefix with the following properties +Payload length: 39 kiByte +Opening DFU capable USB device... +ID 1fc9:000c +Run-time device DFU version 0100 +Claiming USB DFU Runtime Interface... +Determining device status: +state = dfuIDLE, status = 0 +dfu-util: WARNING: Runtime device already in DFU state ?!? +Claiming USB DFU Interface... +Setting Alternate Setting #0 ... +Determining device status: +state = dfuIDLE, status = 0 +dfuIDLE, continuing +DFU mode device DFU version 0100 +Device returned transfer size 2048 +Copying data from PC to DFU device +Download [ ] 0% 0 bytes +Download [= ] 4% 2048 bytes +Download [== ] 9% 4096 bytes +Download [=== ] 14% 6144 bytes +Download [==== ] 19% 8192 bytes +Download [====== ] 24% 10240 bytes +Download [======= ] 28% 12288 bytes +Download [======== ] 33% 14336 bytes +Download [========= ] 38% 16384 bytes +Download [========== ] 43% 18432 bytes +Download [============ ] 48% 20480 bytes +Download [============= ] 53% 22528 bytes +Download [============== ] 57% 24576 bytes +Download [=============== ] 62% 26624 bytes +Download [================ ] 67% 28672 bytes +Download [================== ] 72% 30720 bytes +Download [=================== ] 77% 32768 bytes +Download [==================== ] 82% 34816 bytes +Download [===================== ] 86% 36864 bytes +Download [====================== ] 91% 38912 bytes +Download [======================== ] 96% 40356 bytes +Download [=========================] 100% 40356 bytes +Download done. +Sent a total of 40356 bytes +dfu-util: unable to read DFU status + +$ lsusb | grep NXP +Bus 003 Device 014: ID 1fc9:0018 NXP Semiconductors diff --git a/tools/linux64/src/dfu-util/device-logs/lpclink2.lsusb b/tools/linux64/src/dfu-util/device-logs/lpclink2.lsusb new file mode 100644 index 0000000..b833fca --- /dev/null +++ b/tools/linux64/src/dfu-util/device-logs/lpclink2.lsusb @@ -0,0 +1,203 @@ + +Bus 003 Device 007: ID 0c72:000c PEAK System PCAN-USB +Device Descriptor: + bLength 18 + bDescriptorType 1 + bcdUSB 1.00 + bDeviceClass 0 (Defined at Interface level) + bDeviceSubClass 0 + bDeviceProtocol 0 + bMaxPacketSize0 16 + idVendor 0x0c72 PEAK System + idProduct 0x000c PCAN-USB + bcdDevice 1c.ff + iManufacturer 0 + iProduct 3 VER1:PEAK +VER2:02.8.01 +DAT :06.05.2004 +TIME:09:35:37 + ... + iSerial 0 + bNumConfigurations 3 + Configuration Descriptor: + bLength 9 + bDescriptorType 2 + wTotalLength 46 + bNumInterfaces 1 + bConfigurationValue 1 + iConfiguration 0 + bmAttributes 0x80 + (Bus Powered) + MaxPower 200mA + Interface Descriptor: + bLength 9 + bDescriptorType 4 + bInterfaceNumber 0 + bAlternateSetting 0 + bNumEndpoints 4 + bInterfaceClass 0 (Defined at Interface level) + bInterfaceSubClass 0 + bInterfaceProtocol 0 + iInterface 0 + Endpoint Descriptor: + bLength 7 + bDescriptorType 5 + bEndpointAddress 0x81 EP 1 IN + bmAttributes 2 + Transfer Type Bulk + Synch Type None + Usage Type Data + wMaxPacketSize 0x0010 1x 16 bytes + bInterval 20 + Endpoint Descriptor: + bLength 7 + bDescriptorType 5 + bEndpointAddress 0x01 EP 1 OUT + bmAttributes 2 + Transfer Type Bulk + Synch Type None + Usage Type Data + wMaxPacketSize 0x0010 1x 16 bytes + bInterval 20 + Endpoint Descriptor: + bLength 7 + bDescriptorType 5 + bEndpointAddress 0x82 EP 2 IN + bmAttributes 2 + Transfer Type Bulk + Synch Type None + Usage Type Data + wMaxPacketSize 0x0040 1x 64 bytes + bInterval 1 + Endpoint Descriptor: + bLength 7 + bDescriptorType 5 + bEndpointAddress 0x02 EP 2 OUT + bmAttributes 2 + Transfer Type Bulk + Synch Type None + Usage Type Data + wMaxPacketSize 0x0040 1x 64 bytes + bInterval 1 + Configuration Descriptor: + bLength 9 + bDescriptorType 2 + wTotalLength 46 + bNumInterfaces 1 + bConfigurationValue 2 + iConfiguration 0 + bmAttributes 0x80 + (Bus Powered) + MaxPower 394mA + Interface Descriptor: + bLength 9 + bDescriptorType 4 + bInterfaceNumber 0 + bAlternateSetting 0 + bNumEndpoints 4 + bInterfaceClass 0 (Defined at Interface level) + bInterfaceSubClass 0 + bInterfaceProtocol 0 + iInterface 0 + Endpoint Descriptor: + bLength 7 + bDescriptorType 5 + bEndpointAddress 0x81 EP 1 IN + bmAttributes 2 + Transfer Type Bulk + Synch Type None + Usage Type Data + wMaxPacketSize 0x0010 1x 16 bytes + bInterval 20 + Endpoint Descriptor: + bLength 7 + bDescriptorType 5 + bEndpointAddress 0x01 EP 1 OUT + bmAttributes 2 + Transfer Type Bulk + Synch Type None + Usage Type Data + wMaxPacketSize 0x0010 1x 16 bytes + bInterval 20 + Endpoint Descriptor: + bLength 7 + bDescriptorType 5 + bEndpointAddress 0x82 EP 2 IN + bmAttributes 2 + Transfer Type Bulk + Synch Type None + Usage Type Data + wMaxPacketSize 0x0040 1x 64 bytes + bInterval 1 + Endpoint Descriptor: + bLength 7 + bDescriptorType 5 + bEndpointAddress 0x02 EP 2 OUT + bmAttributes 2 + Transfer Type Bulk + Synch Type None + Usage Type Data + wMaxPacketSize 0x0040 1x 64 bytes + bInterval 1 + Configuration Descriptor: + bLength 9 + bDescriptorType 2 + wTotalLength 46 + bNumInterfaces 1 + bConfigurationValue 3 + iConfiguration 0 + bmAttributes 0x80 + (Bus Powered) + MaxPower 200mA + Interface Descriptor: + bLength 9 + bDescriptorType 4 + bInterfaceNumber 0 + bAlternateSetting 0 + bNumEndpoints 4 + bInterfaceClass 0 (Defined at Interface level) + bInterfaceSubClass 0 + bInterfaceProtocol 0 + iInterface 0 + Endpoint Descriptor: + bLength 7 + bDescriptorType 5 + bEndpointAddress 0x81 EP 1 IN + bmAttributes 3 + Transfer Type Interrupt + Synch Type None + Usage Type Data + wMaxPacketSize 0x0010 1x 16 bytes + bInterval 1 + Endpoint Descriptor: + bLength 7 + bDescriptorType 5 + bEndpointAddress 0x01 EP 1 OUT + bmAttributes 3 + Transfer Type Interrupt + Synch Type None + Usage Type Data + wMaxPacketSize 0x0010 1x 16 bytes + bInterval 1 + Endpoint Descriptor: + bLength 7 + bDescriptorType 5 + bEndpointAddress 0x82 EP 2 IN + bmAttributes 2 + Transfer Type Bulk + Synch Type None + Usage Type Data + wMaxPacketSize 0x0040 1x 64 bytes + bInterval 1 + Endpoint Descriptor: + bLength 7 + bDescriptorType 5 + bEndpointAddress 0x02 EP 2 OUT + bmAttributes 2 + Transfer Type Bulk + Synch Type None + Usage Type Data + wMaxPacketSize 0x0040 1x 64 bytes + bInterval 1 +Device Status: 0x0001 + Self Powered diff --git a/tools/linux64/src/dfu-util/device-logs/opc-20.lsusb b/tools/linux64/src/dfu-util/device-logs/opc-20.lsusb new file mode 100644 index 0000000..580df90 --- /dev/null +++ b/tools/linux64/src/dfu-util/device-logs/opc-20.lsusb @@ -0,0 +1,60 @@ + +Bus 001 Device 004: ID 0483:df11 SGS Thomson Microelectronics +Device Descriptor: + bLength 18 + bDescriptorType 1 + bcdUSB 1.00 + bDeviceClass 0 (Defined at Interface level) + bDeviceSubClass 0 + bDeviceProtocol 0 + bMaxPacketSize0 64 + idVendor 0x0483 SGS Thomson Microelectronics + idProduct 0xdf11 + bcdDevice 2.00 + iManufacturer 1 STMicroelectronics + iProduct 2 STM32 DFU + iSerial 3 ÿÿÿÿÿÿÿÿÿÿÿÿ + bNumConfigurations 1 + Configuration Descriptor: + bLength 9 + bDescriptorType 2 + wTotalLength 36 + bNumInterfaces 1 + bConfigurationValue 1 + iConfiguration 0 + bmAttributes 0xc0 + Self Powered + MaxPower 100mA + Interface Descriptor: + bLength 9 + bDescriptorType 4 + bInterfaceNumber 0 + bAlternateSetting 0 + bNumEndpoints 0 + bInterfaceClass 254 Application Specific Interface + bInterfaceSubClass 1 Device Firmware Update + bInterfaceProtocol 2 + iInterface 4 @Internal Flash /0x08000000/12*001Ka,116*001Kg + Interface Descriptor: + bLength 9 + bDescriptorType 4 + bInterfaceNumber 0 + bAlternateSetting 1 + bNumEndpoints 0 + bInterfaceClass 254 Application Specific Interface + bInterfaceSubClass 1 Device Firmware Update + bInterfaceProtocol 2 + iInterface 5 @SPI Flash : M25P64/0x00000000/128*64Kg + Device Firmware Upgrade Interface Descriptor: + bLength 9 + bDescriptorType 33 + bmAttributes 11 + Will Detach + Manifestation Intolerant + Upload Supported + Download Supported + wDetachTimeout 255 milliseconds + wTransferSize 1024 bytes + bcdDFUVersion 1a.01 +Device Status: 0x0001 + Self Powered diff --git a/tools/linux64/src/dfu-util/device-logs/openmoko-freerunner-dfumode.lsusb b/tools/linux64/src/dfu-util/device-logs/openmoko-freerunner-dfumode.lsusb new file mode 100644 index 0000000..4c0abfb --- /dev/null +++ b/tools/linux64/src/dfu-util/device-logs/openmoko-freerunner-dfumode.lsusb @@ -0,0 +1,109 @@ +Bus 003 Device 017: ID 1d50:5119 OpenMoko, Inc. GTA01/GTA02 U-Boot Bootloader +Device Descriptor: + bLength 18 + bDescriptorType 1 + bcdUSB 1.00 + bDeviceClass 0 (Defined at Interface level) + bDeviceSubClass 0 + bDeviceProtocol 0 + bMaxPacketSize0 16 + idVendor 0x1d50 OpenMoko, Inc. + idProduct 0x5119 GTA01/GTA02 U-Boot Bootloader + bcdDevice 0.00 + iManufacturer 1 OpenMoko, Inc + iProduct 2 Neo1973 Bootloader U-Boot 1.3.2-moko12 + iSerial 3 0000000 + bNumConfigurations 1 + Configuration Descriptor: + bLength 9 + bDescriptorType 2 + wTotalLength 81 + bNumInterfaces 1 + bConfigurationValue 1 + iConfiguration 7 USB Device Firmware Upgrade + bmAttributes 0x80 + (Bus Powered) + MaxPower 100mA + Interface Descriptor: + bLength 9 + bDescriptorType 4 + bInterfaceNumber 0 + bAlternateSetting 0 + bNumEndpoints 0 + bInterfaceClass 254 Application Specific Interface + bInterfaceSubClass 1 Device Firmware Update + bInterfaceProtocol 2 + iInterface 8 RAM 0x32000000 + Interface Descriptor: + bLength 9 + bDescriptorType 4 + bInterfaceNumber 0 + bAlternateSetting 1 + bNumEndpoints 0 + bInterfaceClass 254 Application Specific Interface + bInterfaceSubClass 1 Device Firmware Update + bInterfaceProtocol 2 + iInterface 9 u-boot + Interface Descriptor: + bLength 9 + bDescriptorType 4 + bInterfaceNumber 0 + bAlternateSetting 2 + bNumEndpoints 0 + bInterfaceClass 254 Application Specific Interface + bInterfaceSubClass 1 Device Firmware Update + bInterfaceProtocol 2 + iInterface 10 u-boot_env + Interface Descriptor: + bLength 9 + bDescriptorType 4 + bInterfaceNumber 0 + bAlternateSetting 3 + bNumEndpoints 0 + bInterfaceClass 254 Application Specific Interface + bInterfaceSubClass 1 Device Firmware Update + bInterfaceProtocol 2 + iInterface 11 kernel + Interface Descriptor: + bLength 9 + bDescriptorType 4 + bInterfaceNumber 0 + bAlternateSetting 4 + bNumEndpoints 0 + bInterfaceClass 254 Application Specific Interface + bInterfaceSubClass 1 Device Firmware Update + bInterfaceProtocol 2 + iInterface 12 splash + Interface Descriptor: + bLength 9 + bDescriptorType 4 + bInterfaceNumber 0 + bAlternateSetting 5 + bNumEndpoints 0 + bInterfaceClass 254 Application Specific Interface + bInterfaceSubClass 1 Device Firmware Update + bInterfaceProtocol 2 + iInterface 13 factory + Interface Descriptor: + bLength 9 + bDescriptorType 4 + bInterfaceNumber 0 + bAlternateSetting 6 + bNumEndpoints 0 + bInterfaceClass 254 Application Specific Interface + bInterfaceSubClass 1 Device Firmware Update + bInterfaceProtocol 2 + iInterface 14 rootfs + Device Firmware Upgrade Interface Descriptor: + bLength 9 + bDescriptorType 33 + bmAttributes 7 + Will Not Detach + Manifestation Tolerant + Upload Supported + Download Supported + wDetachTimeout 65280 milliseconds + wTransferSize 4096 bytes + bcdDFUVersion 1.00 +Device Status: 0x0a00 + (Bus Powered) diff --git a/tools/linux64/src/dfu-util/device-logs/openmoko-freerunner.lsusb b/tools/linux64/src/dfu-util/device-logs/openmoko-freerunner.lsusb new file mode 100644 index 0000000..835708d --- /dev/null +++ b/tools/linux64/src/dfu-util/device-logs/openmoko-freerunner.lsusb @@ -0,0 +1,179 @@ +Bus 005 Device 033: ID 1d50:5119 OpenMoko, Inc. GTA01/GTA02 U-Boot Bootloader +Device Descriptor: + bLength 18 + bDescriptorType 1 + bcdUSB 1.10 + bDeviceClass 2 Communications + bDeviceSubClass 0 + bDeviceProtocol 0 + bMaxPacketSize0 16 + idVendor 0x1d50 OpenMoko, Inc. + idProduct 0x5119 GTA01/GTA02 U-Boot Bootloader + bcdDevice 0.00 + iManufacturer 1 OpenMoko, Inc + iProduct 2 Neo1973 Bootloader U-Boot 1.3.2-moko12 + iSerial 3 0000000 + bNumConfigurations 2 + Configuration Descriptor: + bLength 9 + bDescriptorType 2 + wTotalLength 85 + bNumInterfaces 3 + bConfigurationValue 1 + iConfiguration 4 TTY via USB + bmAttributes 0x80 + (Bus Powered) + MaxPower 500mA + Interface Descriptor: + bLength 9 + bDescriptorType 4 + bInterfaceNumber 0 + bAlternateSetting 0 + bNumEndpoints 1 + bInterfaceClass 2 Communications + bInterfaceSubClass 2 Abstract (modem) + bInterfaceProtocol 1 AT-commands (v.25ter) + iInterface 6 Control Interface + CDC Header: + bcdCDC 0.6e + CDC Call Management: + bmCapabilities 0x00 + bDataInterface 1 + CDC ACM: + bmCapabilities 0x00 + CDC Union: + bMasterInterface 0 + bSlaveInterface 1 + Endpoint Descriptor: + bLength 7 + bDescriptorType 5 + bEndpointAddress 0x81 EP 1 IN + bmAttributes 3 + Transfer Type Interrupt + Synch Type None + Usage Type Data + wMaxPacketSize 0x0010 1x 16 bytes + bInterval 255 + Interface Descriptor: + bLength 9 + bDescriptorType 4 + bInterfaceNumber 1 + bAlternateSetting 0 + bNumEndpoints 2 + bInterfaceClass 10 CDC Data + bInterfaceSubClass 0 Unused + bInterfaceProtocol 0 + iInterface 5 Bulk Data Interface + Endpoint Descriptor: + bLength 7 + bDescriptorType 5 + bEndpointAddress 0x02 EP 2 OUT + bmAttributes 2 + Transfer Type Bulk + Synch Type None + Usage Type Data + wMaxPacketSize 0x0010 1x 16 bytes + bInterval 255 + Endpoint Descriptor: + bLength 7 + bDescriptorType 5 + bEndpointAddress 0x83 EP 3 IN + bmAttributes 2 + Transfer Type Bulk + Synch Type None + Usage Type Data + wMaxPacketSize 0x0010 1x 16 bytes + bInterval 255 + Interface Descriptor: + bLength 9 + bDescriptorType 4 + bInterfaceNumber 2 + bAlternateSetting 0 + bNumEndpoints 0 + bInterfaceClass 254 Application Specific Interface + bInterfaceSubClass 1 Device Firmware Update + bInterfaceProtocol 1 + iInterface 7 USB Device Firmware Upgrade + Device Firmware Upgrade Interface Descriptor: + bLength 9 + bDescriptorType 33 + bmAttributes 7 + Will Not Detach + Manifestation Tolerant + Upload Supported + Download Supported + wDetachTimeout 65280 milliseconds + wTransferSize 4096 bytes + bcdDFUVersion 1.00 + Configuration Descriptor: + bLength 9 + bDescriptorType 2 + wTotalLength 67 + bNumInterfaces 2 + bConfigurationValue 2 + iConfiguration 4 TTY via USB + bmAttributes 0x80 + (Bus Powered) + MaxPower 100mA + Interface Descriptor: + bLength 9 + bDescriptorType 4 + bInterfaceNumber 0 + bAlternateSetting 0 + bNumEndpoints 1 + bInterfaceClass 2 Communications + bInterfaceSubClass 2 Abstract (modem) + bInterfaceProtocol 1 AT-commands (v.25ter) + iInterface 6 Control Interface + CDC Header: + bcdCDC 0.6e + CDC Call Management: + bmCapabilities 0x00 + bDataInterface 1 + CDC ACM: + bmCapabilities 0x00 + CDC Union: + bMasterInterface 0 + bSlaveInterface 1 + Endpoint Descriptor: + bLength 7 + bDescriptorType 5 + bEndpointAddress 0x81 EP 1 IN + bmAttributes 3 + Transfer Type Interrupt + Synch Type None + Usage Type Data + wMaxPacketSize 0x0010 1x 16 bytes + bInterval 255 + Interface Descriptor: + bLength 9 + bDescriptorType 4 + bInterfaceNumber 1 + bAlternateSetting 0 + bNumEndpoints 2 + bInterfaceClass 10 CDC Data + bInterfaceSubClass 0 Unused + bInterfaceProtocol 0 + iInterface 5 Bulk Data Interface + Endpoint Descriptor: + bLength 7 + bDescriptorType 5 + bEndpointAddress 0x02 EP 2 OUT + bmAttributes 2 + Transfer Type Bulk + Synch Type None + Usage Type Data + wMaxPacketSize 0x0010 1x 16 bytes + bInterval 255 + Endpoint Descriptor: + bLength 7 + bDescriptorType 5 + bEndpointAddress 0x83 EP 3 IN + bmAttributes 2 + Transfer Type Bulk + Synch Type None + Usage Type Data + wMaxPacketSize 0x0010 1x 16 bytes + bInterval 255 +Device Status: 0x9a00 + (Bus Powered) diff --git a/tools/linux64/src/dfu-util/device-logs/openmoko-neo1973.lsusb b/tools/linux64/src/dfu-util/device-logs/openmoko-neo1973.lsusb new file mode 100644 index 0000000..0778950 --- /dev/null +++ b/tools/linux64/src/dfu-util/device-logs/openmoko-neo1973.lsusb @@ -0,0 +1,182 @@ + +Bus 006 Device 020: ID 1457:5119 First International Computer, Inc. OpenMoko Neo1973 u-boot cdc_acm serial port +Device Descriptor: + bLength 18 + bDescriptorType 1 + bcdUSB 1.10 + bDeviceClass 2 Communications + bDeviceSubClass 0 + bDeviceProtocol 0 + bMaxPacketSize0 16 + idVendor 0x1457 First International Computer, Inc. + idProduct 0x5119 OpenMoko Neo1973 u-boot cdc_acm serial port + bcdDevice 0.00 + iManufacturer 1 + iProduct 2 + iSerial 3 + bNumConfigurations 2 + Configuration Descriptor: + bLength 9 + bDescriptorType 2 + wTotalLength 85 + bNumInterfaces 3 + bConfigurationValue 1 + iConfiguration 4 + bmAttributes 0x80 + (Bus Powered) + MaxPower 500mA + Interface Descriptor: + bLength 9 + bDescriptorType 4 + bInterfaceNumber 0 + bAlternateSetting 0 + bNumEndpoints 1 + bInterfaceClass 2 Communications + bInterfaceSubClass 2 Abstract (modem) + bInterfaceProtocol 1 AT-commands (v.25ter) + iInterface 6 + CDC Header: + bcdCDC 0.6e + CDC Call Management: + bmCapabilities 0x00 + bDataInterface 1 + CDC ACM: + bmCapabilities 0x00 + CDC Union: + bMasterInterface 0 + bSlaveInterface 1 + Endpoint Descriptor: + bLength 7 + bDescriptorType 5 + bEndpointAddress 0x81 EP 1 IN + bmAttributes 3 + Transfer Type Interrupt + Synch Type None + Usage Type Data + wMaxPacketSize 0x0010 1x 16 bytes + bInterval 255 + Interface Descriptor: + bLength 9 + bDescriptorType 4 + bInterfaceNumber 1 + bAlternateSetting 0 + bNumEndpoints 2 + bInterfaceClass 10 CDC Data + bInterfaceSubClass 0 Unused + bInterfaceProtocol 0 + iInterface 5 + Endpoint Descriptor: + bLength 7 + bDescriptorType 5 + bEndpointAddress 0x02 EP 2 OUT + bmAttributes 2 + Transfer Type Bulk + Synch Type None + Usage Type Data + wMaxPacketSize 0x0010 1x 16 bytes + bInterval 255 + Endpoint Descriptor: + bLength 7 + bDescriptorType 5 + bEndpointAddress 0x83 EP 3 IN + bmAttributes 2 + Transfer Type Bulk + Synch Type None + Usage Type Data + wMaxPacketSize 0x0010 1x 16 bytes + bInterval 255 + Interface Descriptor: + bLength 9 + bDescriptorType 4 + bInterfaceNumber 2 + bAlternateSetting 0 + bNumEndpoints 0 + bInterfaceClass 254 Application Specific Interface + bInterfaceSubClass 1 Device Firmware Update + bInterfaceProtocol 1 + iInterface 7 + Device Firmware Upgrade Interface Descriptor: + bLength 9 + bDescriptorType 33 + bmAttributes 7 + Will Not Detach + Manifestation Tolerant + Upload Supported + Download Supported + wDetachTimeout 65280 milliseconds + wTransferSize 4096 bytes + bcdDFUVersion 1.00 + Configuration Descriptor: + bLength 9 + bDescriptorType 2 + wTotalLength 67 + bNumInterfaces 2 + bConfigurationValue 2 + iConfiguration 4 + bmAttributes 0x80 + (Bus Powered) + MaxPower 100mA + Interface Descriptor: + bLength 9 + bDescriptorType 4 + bInterfaceNumber 0 + bAlternateSetting 0 + bNumEndpoints 1 + bInterfaceClass 2 Communications + bInterfaceSubClass 2 Abstract (modem) + bInterfaceProtocol 1 AT-commands (v.25ter) + iInterface 6 + CDC Header: + bcdCDC 0.6e + CDC Call Management: + bmCapabilities 0x00 + bDataInterface 1 + CDC ACM: + bmCapabilities 0x00 + CDC Union: + bMasterInterface 0 + bSlaveInterface 1 + Endpoint Descriptor: + bLength 7 + bDescriptorType 5 + bEndpointAddress 0x81 EP 1 IN + bmAttributes 3 + Transfer Type Interrupt + Synch Type None + Usage Type Data + wMaxPacketSize 0x0010 1x 16 bytes + bInterval 255 + Interface Descriptor: + bLength 9 + bDescriptorType 4 + bInterfaceNumber 1 + bAlternateSetting 0 + bNumEndpoints 2 + bInterfaceClass 10 CDC Data + bInterfaceSubClass 0 Unused + bInterfaceProtocol 0 + iInterface 5 + Endpoint Descriptor: + bLength 7 + bDescriptorType 5 + bEndpointAddress 0x02 EP 2 OUT + bmAttributes 2 + Transfer Type Bulk + Synch Type None + Usage Type Data + wMaxPacketSize 0x0010 1x 16 bytes + bInterval 255 + Endpoint Descriptor: + bLength 7 + bDescriptorType 5 + bEndpointAddress 0x83 EP 3 IN + bmAttributes 2 + Transfer Type Bulk + Synch Type None + Usage Type Data + wMaxPacketSize 0x0010 1x 16 bytes + bInterval 255 +Device Status: 0x0006 + (Bus Powered) + Remote Wakeup Enabled + Test Mode diff --git a/tools/linux64/src/dfu-util/device-logs/openpcd.lsusb b/tools/linux64/src/dfu-util/device-logs/openpcd.lsusb new file mode 100644 index 0000000..f6255a9 --- /dev/null +++ b/tools/linux64/src/dfu-util/device-logs/openpcd.lsusb @@ -0,0 +1,60 @@ + +Bus 006 Device 016: ID 16c0:076b VOTI OpenPCD 13.56MHz RFID Reader +Device Descriptor: + bLength 18 + bDescriptorType 1 + bcdUSB 1.00 + bDeviceClass 0 (Defined at Interface level) + bDeviceSubClass 0 + bDeviceProtocol 0 + bMaxPacketSize0 8 + idVendor 0x16c0 VOTI + idProduct 0x076b OpenPCD 13.56MHz RFID Reader + bcdDevice 0.00 + iManufacturer 1 + iProduct 2 OpenPCD RFID Simulator - DFU Mode + iSerial 0 + bNumConfigurations 1 + Configuration Descriptor: + bLength 9 + bDescriptorType 2 + wTotalLength 36 + bNumInterfaces 1 + bConfigurationValue 1 + iConfiguration 0 + bmAttributes 0x80 + (Bus Powered) + MaxPower 200mA + Interface Descriptor: + bLength 9 + bDescriptorType 4 + bInterfaceNumber 0 + bAlternateSetting 0 + bNumEndpoints 0 + bInterfaceClass 254 Application Specific Interface + bInterfaceSubClass 1 Device Firmware Update + bInterfaceProtocol 2 + iInterface 0 + Interface Descriptor: + bLength 9 + bDescriptorType 4 + bInterfaceNumber 0 + bAlternateSetting 1 + bNumEndpoints 0 + bInterfaceClass 254 Application Specific Interface + bInterfaceSubClass 1 Device Firmware Update + bInterfaceProtocol 2 + iInterface 0 + Device Firmware Upgrade Interface Descriptor: + bLength 9 + bDescriptorType 33 + bmAttributes 3 + Will Not Detach + Manifestation Intolerant + Upload Supported + Download Supported + wDetachTimeout 65280 milliseconds + wTransferSize 256 bytes + bcdDFUVersion 1.00 +Device Status: 0x0000 + (Bus Powered) diff --git a/tools/linux64/src/dfu-util/device-logs/qi-hardware-atusb.lsusb b/tools/linux64/src/dfu-util/device-logs/qi-hardware-atusb.lsusb new file mode 100644 index 0000000..bfc1701 --- /dev/null +++ b/tools/linux64/src/dfu-util/device-logs/qi-hardware-atusb.lsusb @@ -0,0 +1,59 @@ + +Bus 006 Device 013: ID 20b7:1540 Qi Hardware ben-wpan, AT86RF230-based +Device Descriptor: + bLength 18 + bDescriptorType 1 + bcdUSB 2.00 + bDeviceClass 255 Vendor Specific Class + bDeviceSubClass 0 + bDeviceProtocol 0 + bMaxPacketSize0 64 + idVendor 0x20b7 Qi Hardware + idProduct 0x1540 ben-wpan, AT86RF230-based + bcdDevice 0.01 + iManufacturer 0 + iProduct 0 + iSerial 1 4630333438371508231a + bNumConfigurations 1 + Configuration Descriptor: + bLength 9 + bDescriptorType 2 + wTotalLength 34 + bNumInterfaces 2 + bConfigurationValue 1 + iConfiguration 0 + bmAttributes 0x80 + (Bus Powered) + MaxPower 40mA + Interface Descriptor: + bLength 9 + bDescriptorType 4 + bInterfaceNumber 0 + bAlternateSetting 0 + bNumEndpoints 1 + bInterfaceClass 255 Vendor Specific Class + bInterfaceSubClass 0 + bInterfaceProtocol 0 + iInterface 0 + Endpoint Descriptor: + bLength 7 + bDescriptorType 5 + bEndpointAddress 0x81 EP 1 IN + bmAttributes 2 + Transfer Type Bulk + Synch Type None + Usage Type Data + wMaxPacketSize 0x0040 1x 64 bytes + bInterval 0 + Interface Descriptor: + bLength 9 + bDescriptorType 4 + bInterfaceNumber 1 + bAlternateSetting 0 + bNumEndpoints 0 + bInterfaceClass 254 Application Specific Interface + bInterfaceSubClass 1 Device Firmware Update + bInterfaceProtocol 1 + iInterface 0 +Device Status: 0x0000 + (Bus Powered) diff --git a/tools/linux64/src/dfu-util/device-logs/simtrace.lsusb b/tools/linux64/src/dfu-util/device-logs/simtrace.lsusb new file mode 100644 index 0000000..578ddf0 --- /dev/null +++ b/tools/linux64/src/dfu-util/device-logs/simtrace.lsusb @@ -0,0 +1,70 @@ + +Bus 006 Device 017: ID 16c0:0762 VOTI +Device Descriptor: + bLength 18 + bDescriptorType 1 + bcdUSB 1.00 + bDeviceClass 0 (Defined at Interface level) + bDeviceSubClass 0 + bDeviceProtocol 0 + bMaxPacketSize0 8 + idVendor 0x16c0 VOTI + idProduct 0x0762 + bcdDevice 0.00 + iManufacturer 1 sysmocom - systems for mobile communications GmbH + iProduct 2 SimTrace SIM Sniffer - DFU Mode + iSerial 0 + bNumConfigurations 1 + Configuration Descriptor: + bLength 9 + bDescriptorType 2 + wTotalLength 45 + bNumInterfaces 1 + bConfigurationValue 1 + iConfiguration 3 SimTrace DFU Configuration + bmAttributes 0x80 + (Bus Powered) + MaxPower 200mA + Interface Descriptor: + bLength 9 + bDescriptorType 4 + bInterfaceNumber 0 + bAlternateSetting 0 + bNumEndpoints 0 + bInterfaceClass 254 Application Specific Interface + bInterfaceSubClass 1 Device Firmware Update + bInterfaceProtocol 2 + iInterface 4 SimTrace DFU Interface - Application Partition + Interface Descriptor: + bLength 9 + bDescriptorType 4 + bInterfaceNumber 0 + bAlternateSetting 1 + bNumEndpoints 0 + bInterfaceClass 254 Application Specific Interface + bInterfaceSubClass 1 Device Firmware Update + bInterfaceProtocol 2 + iInterface 5 SimTrace DFU Interface - Bootloader Partition + Interface Descriptor: + bLength 9 + bDescriptorType 4 + bInterfaceNumber 0 + bAlternateSetting 2 + bNumEndpoints 0 + bInterfaceClass 254 Application Specific Interface + bInterfaceSubClass 1 Device Firmware Update + bInterfaceProtocol 2 + iInterface 6 SimTrace DFU Interface - RAM + Device Firmware Upgrade Interface Descriptor: + bLength 9 + bDescriptorType 33 + bmAttributes 3 + Will Not Detach + Manifestation Intolerant + Upload Supported + Download Supported + wDetachTimeout 65280 milliseconds + wTransferSize 256 bytes + bcdDFUVersion 1.00 +Device Status: 0x0000 + (Bus Powered) diff --git a/tools/linux64/src/dfu-util/device-logs/sparkcore.lsusb b/tools/linux64/src/dfu-util/device-logs/sparkcore.lsusb new file mode 100644 index 0000000..b6029ff --- /dev/null +++ b/tools/linux64/src/dfu-util/device-logs/sparkcore.lsusb @@ -0,0 +1,60 @@ + +Bus 001 Device 008: ID 1d50:607f OpenMoko, Inc. +Device Descriptor: + bLength 18 + bDescriptorType 1 + bcdUSB 1.00 + bDeviceClass 0 (Defined at Interface level) + bDeviceSubClass 0 + bDeviceProtocol 0 + bMaxPacketSize0 64 + idVendor 0x1d50 OpenMoko, Inc. + idProduct 0x607f + bcdDevice 2.00 + iManufacturer 1 Spark Devices + iProduct 2 CORE DFU + iSerial 3 8D80527B5055 + bNumConfigurations 1 + Configuration Descriptor: + bLength 9 + bDescriptorType 2 + wTotalLength 36 + bNumInterfaces 1 + bConfigurationValue 1 + iConfiguration 0 + bmAttributes 0xc0 + Self Powered + MaxPower 100mA + Interface Descriptor: + bLength 9 + bDescriptorType 4 + bInterfaceNumber 0 + bAlternateSetting 0 + bNumEndpoints 0 + bInterfaceClass 254 Application Specific Interface + bInterfaceSubClass 1 Device Firmware Update + bInterfaceProtocol 2 + iInterface 4 @Internal Flash /0x08000000/20*001Ka,108*001Kg + Interface Descriptor: + bLength 9 + bDescriptorType 4 + bInterfaceNumber 0 + bAlternateSetting 1 + bNumEndpoints 0 + bInterfaceClass 254 Application Specific Interface + bInterfaceSubClass 1 Device Firmware Update + bInterfaceProtocol 2 + iInterface 5 @SPI Flash : SST25x/0x00000000/512*04Kg + Device Firmware Upgrade Interface Descriptor: + bLength 9 + bDescriptorType 33 + bmAttributes 11 + Will Detach + Manifestation Intolerant + Upload Supported + Download Supported + wDetachTimeout 255 milliseconds + wTransferSize 1024 bytes + bcdDFUVersion 1.1a +Device Status: 0x0001 + Self Powered diff --git a/tools/linux64/src/dfu-util/device-logs/stm32f107.bin-download b/tools/linux64/src/dfu-util/device-logs/stm32f107.bin-download new file mode 100644 index 0000000..45b714f --- /dev/null +++ b/tools/linux64/src/dfu-util/device-logs/stm32f107.bin-download @@ -0,0 +1,48 @@ +> src/dfu-util --intf 0 --alt 0 -v -v -v -s 0x8000000 -D test3 +dfu-util 0.4 + +(C) 2005-2008 by Weston Schmidt, Harald Welte and OpenMoko Inc. +(C) 2010-2011 Tormod Volden (DfuSe support) +This program is Free Software and has ABSOLUTELY NO WARRANTY + +dfu-util does currently only support DFU version 1.0 + +Opening DFU USB device... ID 0483:df11 +Run-time device DFU version 011a +Found DFU: [0483:df11] devnum=0, cfg=1, intf=0, alt=0, name="@Internal Flash /0x08000000/128*002Kg" +Claiming USB DFU Interface... +Setting Alternate Setting #0 ... +Determining device status: state = dfuIDLE, status = 0 +dfuIDLE, continuing +DFU mode device DFU version 011a +Device returned transfer size 2048 +No valid DFU suffix signature +Warning: File has no DFU suffix +DfuSe interface name: "Internal Flash " +Memory segment at 0x08000000 128 x 2048 = 262144 (rew) +Uploading to address = 0x08000000, size = 16384 +Erasing page size 2048 at address 0x08000000, page starting at 0x08000000 + Download from image offset 00000000 to memory 08000000-080007ff, size 2048 + Setting address pointer to 0x08000000 +Erasing page size 2048 at address 0x08000800, page starting at 0x08000800 + Download from image offset 00000800 to memory 08000800-08000fff, size 2048 + Setting address pointer to 0x08000800 +Erasing page size 2048 at address 0x08001000, page starting at 0x08001000 + Download from image offset 00001000 to memory 08001000-080017ff, size 2048 + Setting address pointer to 0x08001000 +Erasing page size 2048 at address 0x08001800, page starting at 0x08001800 + Download from image offset 00001800 to memory 08001800-08001fff, size 2048 + Setting address pointer to 0x08001800 +Erasing page size 2048 at address 0x08002000, page starting at 0x08002000 + Download from image offset 00002000 to memory 08002000-080027ff, size 2048 + Setting address pointer to 0x08002000 +Erasing page size 2048 at address 0x08002800, page starting at 0x08002800 + Download from image offset 00002800 to memory 08002800-08002fff, size 2048 + Setting address pointer to 0x08002800 +Erasing page size 2048 at address 0x08003000, page starting at 0x08003000 + Download from image offset 00003000 to memory 08003000-080037ff, size 2048 + Setting address pointer to 0x08003000 +Erasing page size 2048 at address 0x08003800, page starting at 0x08003800 + Download from image offset 00003800 to memory 08003800-08003fff, size 2048 + Setting address pointer to 0x08003800 + diff --git a/tools/linux64/src/dfu-util/device-logs/stm32f107.lsusb b/tools/linux64/src/dfu-util/device-logs/stm32f107.lsusb new file mode 100644 index 0000000..14b45cd --- /dev/null +++ b/tools/linux64/src/dfu-util/device-logs/stm32f107.lsusb @@ -0,0 +1,60 @@ + +Bus 001 Device 028: ID 0483:df11 SGS Thomson Microelectronics STM Device in DFU Mode +Device Descriptor: + bLength 18 + bDescriptorType 1 + bcdUSB 1.00 + bDeviceClass 0 (Defined at Interface level) + bDeviceSubClass 0 + bDeviceProtocol 0 + bMaxPacketSize0 64 + idVendor 0x0483 SGS Thomson Microelectronics + idProduct 0xdf11 STM Device in DFU Mode + bcdDevice 20.00 + iManufacturer 1 STMicroelectronics + iProduct 2 STM32 0x418 DFU Bootloader + iSerial 3 STM32 + bNumConfigurations 1 + Configuration Descriptor: + bLength 9 + bDescriptorType 2 + wTotalLength 36 + bNumInterfaces 1 + bConfigurationValue 1 + iConfiguration 0 + bmAttributes 0xc0 + Self Powered + MaxPower 100mA + Interface Descriptor: + bLength 9 + bDescriptorType 4 + bInterfaceNumber 0 + bAlternateSetting 0 + bNumEndpoints 0 + bInterfaceClass 254 Application Specific Interface + bInterfaceSubClass 1 Device Firmware Update + bInterfaceProtocol 2 + iInterface 4 @Internal Flash /0x08000000/128*002Kg + Interface Descriptor: + bLength 9 + bDescriptorType 4 + bInterfaceNumber 0 + bAlternateSetting 1 + bNumEndpoints 0 + bInterfaceClass 254 Application Specific Interface + bInterfaceSubClass 1 Device Firmware Update + bInterfaceProtocol 2 + iInterface 5 @Option Bytes /0x1FFFF800/01*016 g + Device Firmware Upgrade Interface Descriptor: + bLength 9 + bDescriptorType 33 + bmAttributes 11 + Will Detach + Manifestation Intolerant + Upload Supported + Download Supported + wDetachTimeout 255 milliseconds + wTransferSize 2048 bytes + bcdDFUVersion 1.1a +Device Status: 0x0000 + (Bus Powered) diff --git a/tools/linux64/src/dfu-util/device-logs/stm32f4discovery.bin-download b/tools/linux64/src/dfu-util/device-logs/stm32f4discovery.bin-download new file mode 100644 index 0000000..96e1722 --- /dev/null +++ b/tools/linux64/src/dfu-util/device-logs/stm32f4discovery.bin-download @@ -0,0 +1,36 @@ +dfu-util --device 0483:df11 --alt 0 \ + --dfuse-address 0x08000000 \ + -v -v -v \ + --download arm/iotoggle.bin +No valid DFU suffix signature +Warning: File has no DFU suffix +dfu-util 0.5 + +(C) 2005-2008 by Weston Schmidt, Harald Welte and OpenMoko Inc. +(C) 2010-2011 Tormod Volden (DfuSe support) +This program is Free Software and has ABSOLUTELY NO WARRANTY + +dfu-util does currently only support DFU version 1.0 + +Filter on vendor = 0x0483 product = 0xdf11 +Opening DFU capable USB device... ID 0483:df11 +Run-time device DFU version 011a +Found DFU: [0483:df11] devnum=0, cfg=1, intf=0, alt=0, name="@Internal Flash /0x08000000/04*016Kg,01*064Kg,07*128Kg" +Claiming USB DFU Interface... +Setting Alternate Setting #0 ... +Determining device status: state = dfuERROR, status = 10 +dfuERROR, clearing status +Determining device status: state = dfuIDLE, status = 0 +dfuIDLE, continuing +DFU mode device DFU version 011a +Device returned transfer size 2048 +DfuSe interface name: "Internal Flash " +Memory segment at 0x08000000 4 x 16384 = 65536 (rew) +Memory segment at 0x08010000 1 x 65536 = 65536 (rew) +Memory segment at 0x08020000 7 x 131072 = 917504 (rew) +Uploading to address = 0x08000000, size = 2308 +Erasing page size 16384 at address 0x08000000, page starting at 0x08000000 + Download from image offset 00000000 to memory 08000000-080007ff, size 2048 + Setting address pointer to 0x08000000 + Download from image offset 00000800 to memory 08000800-08000903, size 260 + Setting address pointer to 0x08000800 diff --git a/tools/linux64/src/dfu-util/device-logs/stm32f4discovery.lsusb b/tools/linux64/src/dfu-util/device-logs/stm32f4discovery.lsusb new file mode 100644 index 0000000..0b870de --- /dev/null +++ b/tools/linux64/src/dfu-util/device-logs/stm32f4discovery.lsusb @@ -0,0 +1,80 @@ + +Bus 001 Device 010: ID 0483:df11 SGS Thomson Microelectronics STM Device in DFU Mode +Device Descriptor: + bLength 18 + bDescriptorType 1 + bcdUSB 1.00 + bDeviceClass 0 (Defined at Interface level) + bDeviceSubClass 0 + bDeviceProtocol 0 + bMaxPacketSize0 64 + idVendor 0x0483 SGS Thomson Microelectronics + idProduct 0xdf11 STM Device in DFU Mode + bcdDevice 21.00 + iManufacturer 1 STMicroelectronics + iProduct 2 STM32 BOOTLOADER + iSerial 3 315A28A0B956 + bNumConfigurations 1 + Configuration Descriptor: + bLength 9 + bDescriptorType 2 + wTotalLength 54 + bNumInterfaces 1 + bConfigurationValue 1 + iConfiguration 0 + bmAttributes 0xc0 + Self Powered + MaxPower 100mA + Interface Descriptor: + bLength 9 + bDescriptorType 4 + bInterfaceNumber 0 + bAlternateSetting 0 + bNumEndpoints 0 + bInterfaceClass 254 Application Specific Interface + bInterfaceSubClass 1 Device Firmware Update + bInterfaceProtocol 2 + iInterface 4 @Internal Flash /0x08000000/04*016Kg,01*064Kg,07*128Kg + Interface Descriptor: + bLength 9 + bDescriptorType 4 + bInterfaceNumber 0 + bAlternateSetting 1 + bNumEndpoints 0 + bInterfaceClass 254 Application Specific Interface + bInterfaceSubClass 1 Device Firmware Update + bInterfaceProtocol 2 + iInterface 5 @Option Bytes /0x1FFFC000/01*016 g + Interface Descriptor: + bLength 9 + bDescriptorType 4 + bInterfaceNumber 0 + bAlternateSetting 2 + bNumEndpoints 0 + bInterfaceClass 254 Application Specific Interface + bInterfaceSubClass 1 Device Firmware Update + bInterfaceProtocol 2 + iInterface 6 @OTP Memory /0x1FFF7800/01*512 g,01*016 g + Interface Descriptor: + bLength 9 + bDescriptorType 4 + bInterfaceNumber 0 + bAlternateSetting 3 + bNumEndpoints 0 + bInterfaceClass 254 Application Specific Interface + bInterfaceSubClass 1 Device Firmware Update + bInterfaceProtocol 2 + iInterface 7 @Device Feature/0xFFFF0000/01*004 g + Device Firmware Upgrade Interface Descriptor: + bLength 9 + bDescriptorType 33 + bmAttributes 11 + Will Detach + Manifestation Intolerant + Upload Supported + Download Supported + wDetachTimeout 255 milliseconds + wTransferSize 2048 bytes + bcdDFUVersion 1.1a +Device Status: 0x0001 + Self Powered diff --git a/tools/linux64/src/dfu-util/device-logs/tdk-bluetooth.lsusb b/tools/linux64/src/dfu-util/device-logs/tdk-bluetooth.lsusb new file mode 100644 index 0000000..c0cface --- /dev/null +++ b/tools/linux64/src/dfu-util/device-logs/tdk-bluetooth.lsusb @@ -0,0 +1,269 @@ + +Bus 006 Device 014: ID 04bf:0320 TDK Corp. Bluetooth Adapter +Device Descriptor: + bLength 18 + bDescriptorType 1 + bcdUSB 2.00 + bDeviceClass 224 Wireless + bDeviceSubClass 1 Radio Frequency + bDeviceProtocol 1 Bluetooth + bMaxPacketSize0 64 + idVendor 0x04bf TDK Corp. + idProduct 0x0320 Bluetooth Adapter + bcdDevice 26.52 + iManufacturer 1 Ezurio + iProduct 2 Turbo Bluetooth Adapter + iSerial 3 008098D4FFBD + bNumConfigurations 1 + Configuration Descriptor: + bLength 9 + bDescriptorType 2 + wTotalLength 193 + bNumInterfaces 3 + bConfigurationValue 1 + iConfiguration 0 + bmAttributes 0x80 + (Bus Powered) + MaxPower 64mA + Interface Descriptor: + bLength 9 + bDescriptorType 4 + bInterfaceNumber 0 + bAlternateSetting 0 + bNumEndpoints 3 + bInterfaceClass 224 Wireless + bInterfaceSubClass 1 Radio Frequency + bInterfaceProtocol 1 Bluetooth + iInterface 0 + Endpoint Descriptor: + bLength 7 + bDescriptorType 5 + bEndpointAddress 0x81 EP 1 IN + bmAttributes 3 + Transfer Type Interrupt + Synch Type None + Usage Type Data + wMaxPacketSize 0x0010 1x 16 bytes + bInterval 1 + Endpoint Descriptor: + bLength 7 + bDescriptorType 5 + bEndpointAddress 0x02 EP 2 OUT + bmAttributes 2 + Transfer Type Bulk + Synch Type None + Usage Type Data + wMaxPacketSize 0x0040 1x 64 bytes + bInterval 1 + Endpoint Descriptor: + bLength 7 + bDescriptorType 5 + bEndpointAddress 0x82 EP 2 IN + bmAttributes 2 + Transfer Type Bulk + Synch Type None + Usage Type Data + wMaxPacketSize 0x0040 1x 64 bytes + bInterval 1 + Interface Descriptor: + bLength 9 + bDescriptorType 4 + bInterfaceNumber 1 + bAlternateSetting 0 + bNumEndpoints 2 + bInterfaceClass 224 Wireless + bInterfaceSubClass 1 Radio Frequency + bInterfaceProtocol 1 Bluetooth + iInterface 0 + Endpoint Descriptor: + bLength 7 + bDescriptorType 5 + bEndpointAddress 0x03 EP 3 OUT + bmAttributes 1 + Transfer Type Isochronous + Synch Type None + Usage Type Data + wMaxPacketSize 0x0000 1x 0 bytes + bInterval 1 + Endpoint Descriptor: + bLength 7 + bDescriptorType 5 + bEndpointAddress 0x83 EP 3 IN + bmAttributes 1 + Transfer Type Isochronous + Synch Type None + Usage Type Data + wMaxPacketSize 0x0000 1x 0 bytes + bInterval 1 + Interface Descriptor: + bLength 9 + bDescriptorType 4 + bInterfaceNumber 1 + bAlternateSetting 1 + bNumEndpoints 2 + bInterfaceClass 224 Wireless + bInterfaceSubClass 1 Radio Frequency + bInterfaceProtocol 1 Bluetooth + iInterface 0 + Endpoint Descriptor: + bLength 7 + bDescriptorType 5 + bEndpointAddress 0x03 EP 3 OUT + bmAttributes 1 + Transfer Type Isochronous + Synch Type None + Usage Type Data + wMaxPacketSize 0x0009 1x 9 bytes + bInterval 1 + Endpoint Descriptor: + bLength 7 + bDescriptorType 5 + bEndpointAddress 0x83 EP 3 IN + bmAttributes 1 + Transfer Type Isochronous + Synch Type None + Usage Type Data + wMaxPacketSize 0x0009 1x 9 bytes + bInterval 1 + Interface Descriptor: + bLength 9 + bDescriptorType 4 + bInterfaceNumber 1 + bAlternateSetting 2 + bNumEndpoints 2 + bInterfaceClass 224 Wireless + bInterfaceSubClass 1 Radio Frequency + bInterfaceProtocol 1 Bluetooth + iInterface 0 + Endpoint Descriptor: + bLength 7 + bDescriptorType 5 + bEndpointAddress 0x03 EP 3 OUT + bmAttributes 1 + Transfer Type Isochronous + Synch Type None + Usage Type Data + wMaxPacketSize 0x0011 1x 17 bytes + bInterval 1 + Endpoint Descriptor: + bLength 7 + bDescriptorType 5 + bEndpointAddress 0x83 EP 3 IN + bmAttributes 1 + Transfer Type Isochronous + Synch Type None + Usage Type Data + wMaxPacketSize 0x0011 1x 17 bytes + bInterval 1 + Interface Descriptor: + bLength 9 + bDescriptorType 4 + bInterfaceNumber 1 + bAlternateSetting 3 + bNumEndpoints 2 + bInterfaceClass 224 Wireless + bInterfaceSubClass 1 Radio Frequency + bInterfaceProtocol 1 Bluetooth + iInterface 0 + Endpoint Descriptor: + bLength 7 + bDescriptorType 5 + bEndpointAddress 0x03 EP 3 OUT + bmAttributes 1 + Transfer Type Isochronous + Synch Type None + Usage Type Data + wMaxPacketSize 0x0019 1x 25 bytes + bInterval 1 + Endpoint Descriptor: + bLength 7 + bDescriptorType 5 + bEndpointAddress 0x83 EP 3 IN + bmAttributes 1 + Transfer Type Isochronous + Synch Type None + Usage Type Data + wMaxPacketSize 0x0019 1x 25 bytes + bInterval 1 + Interface Descriptor: + bLength 9 + bDescriptorType 4 + bInterfaceNumber 1 + bAlternateSetting 4 + bNumEndpoints 2 + bInterfaceClass 224 Wireless + bInterfaceSubClass 1 Radio Frequency + bInterfaceProtocol 1 Bluetooth + iInterface 0 + Endpoint Descriptor: + bLength 7 + bDescriptorType 5 + bEndpointAddress 0x03 EP 3 OUT + bmAttributes 1 + Transfer Type Isochronous + Synch Type None + Usage Type Data + wMaxPacketSize 0x0021 1x 33 bytes + bInterval 1 + Endpoint Descriptor: + bLength 7 + bDescriptorType 5 + bEndpointAddress 0x83 EP 3 IN + bmAttributes 1 + Transfer Type Isochronous + Synch Type None + Usage Type Data + wMaxPacketSize 0x0021 1x 33 bytes + bInterval 1 + Interface Descriptor: + bLength 9 + bDescriptorType 4 + bInterfaceNumber 1 + bAlternateSetting 5 + bNumEndpoints 2 + bInterfaceClass 224 Wireless + bInterfaceSubClass 1 Radio Frequency + bInterfaceProtocol 1 Bluetooth + iInterface 0 + Endpoint Descriptor: + bLength 7 + bDescriptorType 5 + bEndpointAddress 0x03 EP 3 OUT + bmAttributes 1 + Transfer Type Isochronous + Synch Type None + Usage Type Data + wMaxPacketSize 0x0031 1x 49 bytes + bInterval 1 + Endpoint Descriptor: + bLength 7 + bDescriptorType 5 + bEndpointAddress 0x83 EP 3 IN + bmAttributes 1 + Transfer Type Isochronous + Synch Type None + Usage Type Data + wMaxPacketSize 0x0031 1x 49 bytes + bInterval 1 + Interface Descriptor: + bLength 9 + bDescriptorType 4 + bInterfaceNumber 2 + bAlternateSetting 0 + bNumEndpoints 0 + bInterfaceClass 254 Application Specific Interface + bInterfaceSubClass 1 Device Firmware Update + bInterfaceProtocol 0 + iInterface 0 + Device Firmware Upgrade Interface Descriptor: + bLength 7 + bDescriptorType 33 + bmAttributes 7 + Will Not Detach + Manifestation Tolerant + Upload Supported + Download Supported + wDetachTimeout 5000 milliseconds + wTransferSize 1023 bytes +Device Status: 0x0000 + (Bus Powered) diff --git a/tools/linux64/src/dfu-util/dfuse-pack.py b/tools/linux64/src/dfu-util/dfuse-pack.py new file mode 100644 index 0000000..875cc5c --- /dev/null +++ b/tools/linux64/src/dfu-util/dfuse-pack.py @@ -0,0 +1,121 @@ +#!/usr/bin/python + +# Written by Antonio Galea - 2010/11/18 +# Distributed under Gnu LGPL 3.0 +# see http://www.gnu.org/licenses/lgpl-3.0.txt + +import sys,struct,zlib,os +from optparse import OptionParser + +DEFAULT_DEVICE="0x0483:0xdf11" + +def named(tuple,names): + return dict(zip(names.split(),tuple)) +def consume(fmt,data,names): + n = struct.calcsize(fmt) + return named(struct.unpack(fmt,data[:n]),names),data[n:] +def cstring(string): + return string.split('\0',1)[0] +def compute_crc(data): + return 0xFFFFFFFF & -zlib.crc32(data) -1 + +def parse(file,dump_images=False): + print 'File: "%s"' % file + data = open(file,'rb').read() + crc = compute_crc(data[:-4]) + prefix, data = consume('<5sBIB',data,'signature version size targets') + print '%(signature)s v%(version)d, image size: %(size)d, targets: %(targets)d' % prefix + for t in range(prefix['targets']): + tprefix, data = consume('<6sBI255s2I',data,'signature altsetting named name size elements') + tprefix['num'] = t + if tprefix['named']: + tprefix['name'] = cstring(tprefix['name']) + else: + tprefix['name'] = '' + print '%(signature)s %(num)d, alt setting: %(altsetting)s, name: "%(name)s", size: %(size)d, elements: %(elements)d' % tprefix + tsize = tprefix['size'] + target, data = data[:tsize], data[tsize:] + for e in range(tprefix['elements']): + eprefix, target = consume('<2I',target,'address size') + eprefix['num'] = e + print ' %(num)d, address: 0x%(address)08x, size: %(size)d' % eprefix + esize = eprefix['size'] + image, target = target[:esize], target[esize:] + if dump_images: + out = '%s.target%d.image%d.bin' % (file,t,e) + open(out,'wb').write(image) + print ' DUMPED IMAGE TO "%s"' % out + if len(target): + print "target %d: PARSE ERROR" % t + suffix = named(struct.unpack('<4H3sBI',data[:16]),'device product vendor dfu ufd len crc') + print 'usb: %(vendor)04x:%(product)04x, device: 0x%(device)04x, dfu: 0x%(dfu)04x, %(ufd)s, %(len)d, 0x%(crc)08x' % suffix + if crc != suffix['crc']: + print "CRC ERROR: computed crc32 is 0x%08x" % crc + data = data[16:] + if data: + print "PARSE ERROR" + +def build(file,targets,device=DEFAULT_DEVICE): + data = '' + for t,target in enumerate(targets): + tdata = '' + for image in target: + tdata += struct.pack('<2I',image['address'],len(image['data']))+image['data'] + tdata = struct.pack('<6sBI255s2I','Target',0,1,'ST...',len(tdata),len(target)) + tdata + data += tdata + data = struct.pack('<5sBIB','DfuSe',1,len(data)+11,len(targets)) + data + v,d=map(lambda x: int(x,0) & 0xFFFF, device.split(':',1)) + data += struct.pack('<4H3sB',0,d,v,0x011a,'UFD',16) + crc = compute_crc(data) + data += struct.pack(' and +Harald Welte . Over time, nearly complete +support of DFU 1.0, DFU 1.1 and DfuSe ("1.1a") has been added. +.SH LICENCE +.B dfu-util +is covered by the GNU General Public License (GPL), version 2 or later. +.SH COPYRIGHT +This manual page was originally written by Uwe Hermann , +and is now part of the dfu-util project. diff --git a/tools/linux64/src/dfu-util/msvc/README_msvc.txt b/tools/linux64/src/dfu-util/msvc/README_msvc.txt new file mode 100644 index 0000000..6e68ec6 --- /dev/null +++ b/tools/linux64/src/dfu-util/msvc/README_msvc.txt @@ -0,0 +1,10 @@ +# (C) Roger Meier +# (C) Pascal Schweizer +# msvc folder is GPL-2.0+, LGPL-2.1+, BSD-3-Clause or MIT license(SPDX) + +Building dfu-util native on Windows with Visual Studio + +3rd party dependencies: +- libusbx ( git clone https://github.com/libusbx/libusbx.git ) + - getopt (part of libusbx: libusbx/examples/getopt) + diff --git a/tools/linux64/src/dfu-util/msvc/dfu-suffix_2010.vcxproj b/tools/linux64/src/dfu-util/msvc/dfu-suffix_2010.vcxproj new file mode 100644 index 0000000..0c316c2 --- /dev/null +++ b/tools/linux64/src/dfu-util/msvc/dfu-suffix_2010.vcxproj @@ -0,0 +1,100 @@ + + + + + Debug + Win32 + + + Release + Win32 + + + + {8F7600A2-3B37-4956-B39B-A1D43EF29EDA} + dfusuffix + dfu-suffix + + + + Application + true + MultiByte + + + Application + false + true + MultiByte + + + + + + + + + + + + + $(SolutionDir)..\..\libusbx\examples\getopt;$(SolutionDir)..\..\libusbx\libusb;$(IncludePath) + $(SolutionDir)..\$(Platform)\$(ProjectName)\$(Configuration)\ + $(SolutionDir)..\$(Platform)\$(ProjectName)\$(Configuration)\ + $(SolutionDir)..\$(Platform)\$(ProjectName)\$(Configuration)\dll;$(LibraryPath) + $(VCInstallDir)atlmfc\lib;$(VCInstallDir)lib + $(ExecutablePath) + + + $(ExecutablePath) + $(SolutionDir)..\..\libusbx\examples\getopt;$(SolutionDir)..\..\libusbx\libusb;$(IncludePath) + $(SolutionDir)..\$(Platform)\$(ProjectName)\$(Configuration)\dll;$(LibraryPath) + $(SolutionDir)..\$(Platform)\$(ProjectName)\$(Configuration)\ + $(SolutionDir)..\$(Platform)\$(ProjectName)\$(Configuration)\ + + + + Level3 + Disabled + HAVE_WINDOWS_H;_CRT_SECURE_NO_WARNINGS;%(PreprocessorDefinitions) + MultiThreadedDebug + + + true + + + + + Level3 + MaxSpeed + true + true + HAVE_WINDOWS_H;_CRT_SECURE_NO_WARNINGS;%(PreprocessorDefinitions) + MultiThreaded + + + true + true + true + + + + + + + + + + + + + {a2169bc8-cf99-40bf-83f3-b0e38f7067bd} + + + {349ee8f9-7d25-4909-aaf5-ff3fade72187} + + + + + + \ No newline at end of file diff --git a/tools/linux64/src/dfu-util/msvc/dfu-util_2010.sln b/tools/linux64/src/dfu-util/msvc/dfu-util_2010.sln new file mode 100644 index 0000000..ef79723 --- /dev/null +++ b/tools/linux64/src/dfu-util/msvc/dfu-util_2010.sln @@ -0,0 +1,54 @@ + +Microsoft Visual Studio Solution File, Format Version 11.00 +# Visual Studio 2010 +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "dfu-util", "dfu-util_2010.vcxproj", "{0E071A60-7EF2-4427-BAA8-9143CACB5BCB}" +EndProject +Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{C4F8746D-B27E-4806-95E5-2052174E923B}" +EndProject +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "dfu-suffix", "dfu-suffix_2010.vcxproj", "{8F7600A2-3B37-4956-B39B-A1D43EF29EDA}" +EndProject +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "getopt_2010", "..\..\libusbx\msvc\getopt_2010.vcxproj", "{AE83E1B4-CE06-47EE-B7A3-C3A1D7C2D71E}" +EndProject +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "libusb-1.0 (static)", "..\..\libusbx\msvc\libusb_static_2010.vcxproj", "{349EE8F9-7D25-4909-AAF5-FF3FADE72187}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Win32 = Debug|Win32 + Debug|x64 = Debug|x64 + Release|Win32 = Release|Win32 + Release|x64 = Release|x64 + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {0E071A60-7EF2-4427-BAA8-9143CACB5BCB}.Debug|Win32.ActiveCfg = Debug|Win32 + {0E071A60-7EF2-4427-BAA8-9143CACB5BCB}.Debug|Win32.Build.0 = Debug|Win32 + {0E071A60-7EF2-4427-BAA8-9143CACB5BCB}.Debug|x64.ActiveCfg = Debug|Win32 + {0E071A60-7EF2-4427-BAA8-9143CACB5BCB}.Release|Win32.ActiveCfg = Release|Win32 + {0E071A60-7EF2-4427-BAA8-9143CACB5BCB}.Release|Win32.Build.0 = Release|Win32 + {0E071A60-7EF2-4427-BAA8-9143CACB5BCB}.Release|x64.ActiveCfg = Release|Win32 + {8F7600A2-3B37-4956-B39B-A1D43EF29EDA}.Debug|Win32.ActiveCfg = Debug|Win32 + {8F7600A2-3B37-4956-B39B-A1D43EF29EDA}.Debug|Win32.Build.0 = Debug|Win32 + {8F7600A2-3B37-4956-B39B-A1D43EF29EDA}.Debug|x64.ActiveCfg = Debug|Win32 + {8F7600A2-3B37-4956-B39B-A1D43EF29EDA}.Release|Win32.ActiveCfg = Release|Win32 + {8F7600A2-3B37-4956-B39B-A1D43EF29EDA}.Release|Win32.Build.0 = Release|Win32 + {8F7600A2-3B37-4956-B39B-A1D43EF29EDA}.Release|x64.ActiveCfg = Release|Win32 + {AE83E1B4-CE06-47EE-B7A3-C3A1D7C2D71E}.Debug|Win32.ActiveCfg = Debug|Win32 + {AE83E1B4-CE06-47EE-B7A3-C3A1D7C2D71E}.Debug|Win32.Build.0 = Debug|Win32 + {AE83E1B4-CE06-47EE-B7A3-C3A1D7C2D71E}.Debug|x64.ActiveCfg = Debug|x64 + {AE83E1B4-CE06-47EE-B7A3-C3A1D7C2D71E}.Debug|x64.Build.0 = Debug|x64 + {AE83E1B4-CE06-47EE-B7A3-C3A1D7C2D71E}.Release|Win32.ActiveCfg = Release|Win32 + {AE83E1B4-CE06-47EE-B7A3-C3A1D7C2D71E}.Release|Win32.Build.0 = Release|Win32 + {AE83E1B4-CE06-47EE-B7A3-C3A1D7C2D71E}.Release|x64.ActiveCfg = Release|x64 + {AE83E1B4-CE06-47EE-B7A3-C3A1D7C2D71E}.Release|x64.Build.0 = Release|x64 + {349EE8F9-7D25-4909-AAF5-FF3FADE72187}.Debug|Win32.ActiveCfg = Debug|Win32 + {349EE8F9-7D25-4909-AAF5-FF3FADE72187}.Debug|Win32.Build.0 = Debug|Win32 + {349EE8F9-7D25-4909-AAF5-FF3FADE72187}.Debug|x64.ActiveCfg = Debug|x64 + {349EE8F9-7D25-4909-AAF5-FF3FADE72187}.Debug|x64.Build.0 = Debug|x64 + {349EE8F9-7D25-4909-AAF5-FF3FADE72187}.Release|Win32.ActiveCfg = Release|Win32 + {349EE8F9-7D25-4909-AAF5-FF3FADE72187}.Release|Win32.Build.0 = Release|Win32 + {349EE8F9-7D25-4909-AAF5-FF3FADE72187}.Release|x64.ActiveCfg = Release|x64 + {349EE8F9-7D25-4909-AAF5-FF3FADE72187}.Release|x64.Build.0 = Release|x64 + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection +EndGlobal diff --git a/tools/linux64/src/dfu-util/msvc/dfu-util_2010.vcxproj b/tools/linux64/src/dfu-util/msvc/dfu-util_2010.vcxproj new file mode 100644 index 0000000..17a8bee --- /dev/null +++ b/tools/linux64/src/dfu-util/msvc/dfu-util_2010.vcxproj @@ -0,0 +1,120 @@ + + + + + Debug + Win32 + + + Release + Win32 + + + + {0E071A60-7EF2-4427-BAA8-9143CACB5BCB} + dfuutil + dfu-util + + + + Application + true + MultiByte + + + Application + false + true + MultiByte + + + + + + + + + + + + + $(SolutionDir)..\..\libusbx\examples\getopt;$(SolutionDir)..\..\libusbx\libusb;$(IncludePath) + $(SolutionDir)..\$(Platform)\$(ProjectName)\$(Configuration)\ + $(SolutionDir)..\$(Platform)\$(ProjectName)\$(Configuration)\ + $(SolutionDir)..\$(Platform)\getopt\$(Configuration);$(LibraryPath) + $(VCInstallDir)atlmfc\lib;$(VCInstallDir)lib + $(ExecutablePath) + + + $(ExecutablePath) + $(SolutionDir)..\..\libusbx\examples\getopt;$(SolutionDir)..\..\libusbx\libusb;$(IncludePath) + $(SolutionDir)..\$(Platform)\getopt\$(Configuration);$(LibraryPath) + $(SolutionDir)..\$(Platform)\$(ProjectName)\$(Configuration)\ + $(SolutionDir)..\$(Platform)\$(ProjectName)\$(Configuration)\ + + + + Level3 + Disabled + HAVE_WINDOWS_H;_CRT_SECURE_NO_WARNINGS;%(PreprocessorDefinitions) + MultiThreadedDebug + + + true + + + + + + + + + Level3 + MaxSpeed + true + true + HAVE_WINDOWS_H;_CRT_SECURE_NO_WARNINGS;%(PreprocessorDefinitions) + MultiThreaded + + + true + true + true + + + copy $(SolutionDir)..\$(Platform)\$(Configuration)\dll\libusb-1.0.dll $(SolutionDir)..\$(Platform)\$(ProjectName)\$(Configuration)\ + + + + + + + + + + + + + + + + + + + + + + + + + + {a2169bc8-cf99-40bf-83f3-b0e38f7067bd} + + + {349ee8f9-7d25-4909-aaf5-ff3fade72187} + + + + + + \ No newline at end of file diff --git a/tools/linux64/src/dfu-util/src/Makefile.am b/tools/linux64/src/dfu-util/src/Makefile.am new file mode 100644 index 0000000..70179c4 --- /dev/null +++ b/tools/linux64/src/dfu-util/src/Makefile.am @@ -0,0 +1,28 @@ +AM_CFLAGS = -Wall -Wextra + +bin_PROGRAMS = dfu-util dfu-suffix dfu-prefix +dfu_util_SOURCES = main.c \ + portable.h \ + dfu_load.c \ + dfu_load.h \ + dfu_util.c \ + dfu_util.h \ + dfuse.c \ + dfuse.h \ + dfuse_mem.c \ + dfuse_mem.h \ + dfu.c \ + dfu.h \ + usb_dfu.h \ + dfu_file.c \ + dfu_file.h \ + quirks.c \ + quirks.h + +dfu_suffix_SOURCES = suffix.c \ + dfu_file.h \ + dfu_file.c + +dfu_prefix_SOURCES = prefix.c \ + dfu_file.h \ + dfu_file.c diff --git a/tools/linux64/src/dfu-util/src/dfu.c b/tools/linux64/src/dfu-util/src/dfu.c new file mode 100644 index 0000000..14d7673 --- /dev/null +++ b/tools/linux64/src/dfu-util/src/dfu.c @@ -0,0 +1,357 @@ +/* + * Low-level DFU communication routines, originally taken from + * $Id: dfu.c,v 1.3 2006/06/20 06:28:04 schmidtw Exp $ + * (part of dfu-programmer). + * + * Copyright 2005-2006 Weston Schmidt + * Copyright 2011-2014 Tormod Volden + * + * 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 +#include + +#include + +#include "portable.h" +#include "dfu.h" +#include "quirks.h" + +static int dfu_timeout = 5000; /* 5 seconds - default */ + +/* + * 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( libusb_device_handle *device, + const unsigned short interface, + const unsigned short timeout ) +{ + return libusb_control_transfer( device, + /* bmRequestType */ LIBUSB_ENDPOINT_OUT | LIBUSB_REQUEST_TYPE_CLASS | LIBUSB_RECIPIENT_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( libusb_device_handle *device, + const unsigned short interface, + const unsigned short length, + const unsigned short transaction, + unsigned char* data ) +{ + int status; + + status = libusb_control_transfer( device, + /* bmRequestType */ LIBUSB_ENDPOINT_OUT | LIBUSB_REQUEST_TYPE_CLASS | LIBUSB_RECIPIENT_INTERFACE, + /* bRequest */ DFU_DNLOAD, + /* wValue */ transaction, + /* wIndex */ interface, + /* Data */ data, + /* wLength */ length, + dfu_timeout ); + 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( libusb_device_handle *device, + const unsigned short interface, + const unsigned short length, + const unsigned short transaction, + unsigned char* data ) +{ + int status; + + status = libusb_control_transfer( device, + /* bmRequestType */ LIBUSB_ENDPOINT_IN | LIBUSB_REQUEST_TYPE_CLASS | LIBUSB_RECIPIENT_INTERFACE, + /* bRequest */ DFU_UPLOAD, + /* wValue */ transaction, + /* wIndex */ interface, + /* Data */ data, + /* wLength */ length, + dfu_timeout ); + 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 dfu_if *dif, struct dfu_status *status ) +{ + unsigned char buffer[6]; + int result; + + /* Initialize the status data structure */ + status->bStatus = DFU_STATUS_ERROR_UNKNOWN; + status->bwPollTimeout = 0; + status->bState = STATE_DFU_ERROR; + status->iString = 0; + + result = libusb_control_transfer( dif->dev_handle, + /* bmRequestType */ LIBUSB_ENDPOINT_IN | LIBUSB_REQUEST_TYPE_CLASS | LIBUSB_RECIPIENT_INTERFACE, + /* bRequest */ DFU_GETSTATUS, + /* wValue */ 0, + /* wIndex */ dif->interface, + /* Data */ buffer, + /* wLength */ 6, + dfu_timeout ); + + if( 6 == result ) { + status->bStatus = buffer[0]; + if (dif->quirks & QUIRK_POLLTIMEOUT) + status->bwPollTimeout = DEFAULT_POLLTIMEOUT; + else + 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( libusb_device_handle *device, + const unsigned short interface ) +{ + return libusb_control_transfer( device, + /* bmRequestType */ LIBUSB_ENDPOINT_OUT| LIBUSB_REQUEST_TYPE_CLASS | LIBUSB_RECIPIENT_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( libusb_device_handle *device, + const unsigned short interface ) +{ + int result; + unsigned char buffer[1]; + + result = libusb_control_transfer( device, + /* bmRequestType */ LIBUSB_ENDPOINT_IN | LIBUSB_REQUEST_TYPE_CLASS | LIBUSB_RECIPIENT_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 -1; + + /* 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( libusb_device_handle *device, + const unsigned short interface ) +{ + return libusb_control_transfer( device, + /* bmRequestType */ LIBUSB_ENDPOINT_OUT | LIBUSB_REQUEST_TYPE_CLASS | LIBUSB_RECIPIENT_INTERFACE, + /* bRequest */ DFU_ABORT, + /* wValue */ 0, + /* wIndex */ interface, + /* Data */ NULL, + /* wLength */ 0, + dfu_timeout ); +} + + +const char* dfu_state_to_string( int state ) +{ + const char *message; + + 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; + default: + message = NULL; + 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 memory 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]; +} + +int dfu_abort_to_idle(struct dfu_if *dif) +{ + int ret; + struct dfu_status dst; + + ret = dfu_abort(dif->dev_handle, dif->interface); + if (ret < 0) { + errx(EX_IOERR, "Error sending dfu abort request"); + exit(1); + } + ret = dfu_get_status(dif, &dst); + if (ret < 0) { + errx(EX_IOERR, "Error during abort get_status"); + exit(1); + } + if (dst.bState != DFU_STATE_dfuIDLE) { + errx(EX_IOERR, "Failed to enter idle state on abort"); + exit(1); + } + milli_sleep(dst.bwPollTimeout); + return ret; +} diff --git a/tools/linux64/src/dfu-util/src/dfu.h b/tools/linux64/src/dfu-util/src/dfu.h new file mode 100644 index 0000000..8e3caeb --- /dev/null +++ b/tools/linux64/src/dfu-util/src/dfu.h @@ -0,0 +1,133 @@ +/* + * 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 +#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 + +/* DFU commands */ +#define DFU_DETACH 0 +#define DFU_DNLOAD 1 +#define DFU_UPLOAD 2 +#define DFU_GETSTATUS 3 +#define DFU_CLRSTATUS 4 +#define DFU_GETSTATE 5 +#define DFU_ABORT 6 + +/* DFU interface */ +#define DFU_IFF_DFU 0x0001 /* DFU Mode, (not Runtime) */ + +/* 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; +}; + +struct dfu_if { + struct usb_dfu_func_descriptor func_dfu; + uint16_t quirks; + uint16_t busnum; + uint16_t devnum; + uint16_t vendor; + uint16_t product; + uint16_t bcdDevice; + uint8_t configuration; + uint8_t interface; + uint8_t altsetting; + uint8_t flags; + uint8_t bMaxPacketSize0; + char *alt_name; + char *serial_name; + libusb_device *dev; + libusb_device_handle *dev_handle; + struct dfu_if *next; +}; + +int dfu_detach( libusb_device_handle *device, + const unsigned short interface, + const unsigned short timeout ); +int dfu_download( libusb_device_handle *device, + const unsigned short interface, + const unsigned short length, + const unsigned short transaction, + unsigned char* data ); +int dfu_upload( libusb_device_handle *device, + const unsigned short interface, + const unsigned short length, + const unsigned short transaction, + unsigned char* data ); +int dfu_get_status( struct dfu_if *dif, + struct dfu_status *status ); +int dfu_clear_status( libusb_device_handle *device, + const unsigned short interface ); +int dfu_get_state( libusb_device_handle *device, + const unsigned short interface ); +int dfu_abort( libusb_device_handle *device, + const unsigned short interface ); +int dfu_abort_to_idle( struct dfu_if *dif); + +const char *dfu_state_to_string( int state ); + +const char *dfu_status_to_string( int status ); + +#endif /* DFU_H */ diff --git a/tools/linux64/src/dfu-util/src/dfu_file.c b/tools/linux64/src/dfu-util/src/dfu_file.c new file mode 100644 index 0000000..7c897d4 --- /dev/null +++ b/tools/linux64/src/dfu-util/src/dfu_file.c @@ -0,0 +1,444 @@ +/* + * Load or store DFU files including suffix and prefix + * + * Copyright 2014 Tormod Volden + * Copyright 2012 Stefan Schmidt + * + * 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 +#include +#include +#include +#include +#include +#include + +#include "portable.h" +#include "dfu_file.h" + +#define DFU_SUFFIX_LENGTH 16 +#define LMDFU_PREFIX_LENGTH 8 +#define LPCDFU_PREFIX_LENGTH 16 +#define PROGRESS_BAR_WIDTH 25 +#define STDIN_CHUNK_SIZE 65536 + +static const unsigned long crc32_table[] = { + 0x00000000, 0x77073096, 0xee0e612c, 0x990951ba, 0x076dc419, 0x706af48f, + 0xe963a535, 0x9e6495a3, 0x0edb8832, 0x79dcb8a4, 0xe0d5e91e, 0x97d2d988, + 0x09b64c2b, 0x7eb17cbd, 0xe7b82d07, 0x90bf1d91, 0x1db71064, 0x6ab020f2, + 0xf3b97148, 0x84be41de, 0x1adad47d, 0x6ddde4eb, 0xf4d4b551, 0x83d385c7, + 0x136c9856, 0x646ba8c0, 0xfd62f97a, 0x8a65c9ec, 0x14015c4f, 0x63066cd9, + 0xfa0f3d63, 0x8d080df5, 0x3b6e20c8, 0x4c69105e, 0xd56041e4, 0xa2677172, + 0x3c03e4d1, 0x4b04d447, 0xd20d85fd, 0xa50ab56b, 0x35b5a8fa, 0x42b2986c, + 0xdbbbc9d6, 0xacbcf940, 0x32d86ce3, 0x45df5c75, 0xdcd60dcf, 0xabd13d59, + 0x26d930ac, 0x51de003a, 0xc8d75180, 0xbfd06116, 0x21b4f4b5, 0x56b3c423, + 0xcfba9599, 0xb8bda50f, 0x2802b89e, 0x5f058808, 0xc60cd9b2, 0xb10be924, + 0x2f6f7c87, 0x58684c11, 0xc1611dab, 0xb6662d3d, 0x76dc4190, 0x01db7106, + 0x98d220bc, 0xefd5102a, 0x71b18589, 0x06b6b51f, 0x9fbfe4a5, 0xe8b8d433, + 0x7807c9a2, 0x0f00f934, 0x9609a88e, 0xe10e9818, 0x7f6a0dbb, 0x086d3d2d, + 0x91646c97, 0xe6635c01, 0x6b6b51f4, 0x1c6c6162, 0x856530d8, 0xf262004e, + 0x6c0695ed, 0x1b01a57b, 0x8208f4c1, 0xf50fc457, 0x65b0d9c6, 0x12b7e950, + 0x8bbeb8ea, 0xfcb9887c, 0x62dd1ddf, 0x15da2d49, 0x8cd37cf3, 0xfbd44c65, + 0x4db26158, 0x3ab551ce, 0xa3bc0074, 0xd4bb30e2, 0x4adfa541, 0x3dd895d7, + 0xa4d1c46d, 0xd3d6f4fb, 0x4369e96a, 0x346ed9fc, 0xad678846, 0xda60b8d0, + 0x44042d73, 0x33031de5, 0xaa0a4c5f, 0xdd0d7cc9, 0x5005713c, 0x270241aa, + 0xbe0b1010, 0xc90c2086, 0x5768b525, 0x206f85b3, 0xb966d409, 0xce61e49f, + 0x5edef90e, 0x29d9c998, 0xb0d09822, 0xc7d7a8b4, 0x59b33d17, 0x2eb40d81, + 0xb7bd5c3b, 0xc0ba6cad, 0xedb88320, 0x9abfb3b6, 0x03b6e20c, 0x74b1d29a, + 0xead54739, 0x9dd277af, 0x04db2615, 0x73dc1683, 0xe3630b12, 0x94643b84, + 0x0d6d6a3e, 0x7a6a5aa8, 0xe40ecf0b, 0x9309ff9d, 0x0a00ae27, 0x7d079eb1, + 0xf00f9344, 0x8708a3d2, 0x1e01f268, 0x6906c2fe, 0xf762575d, 0x806567cb, + 0x196c3671, 0x6e6b06e7, 0xfed41b76, 0x89d32be0, 0x10da7a5a, 0x67dd4acc, + 0xf9b9df6f, 0x8ebeeff9, 0x17b7be43, 0x60b08ed5, 0xd6d6a3e8, 0xa1d1937e, + 0x38d8c2c4, 0x4fdff252, 0xd1bb67f1, 0xa6bc5767, 0x3fb506dd, 0x48b2364b, + 0xd80d2bda, 0xaf0a1b4c, 0x36034af6, 0x41047a60, 0xdf60efc3, 0xa867df55, + 0x316e8eef, 0x4669be79, 0xcb61b38c, 0xbc66831a, 0x256fd2a0, 0x5268e236, + 0xcc0c7795, 0xbb0b4703, 0x220216b9, 0x5505262f, 0xc5ba3bbe, 0xb2bd0b28, + 0x2bb45a92, 0x5cb36a04, 0xc2d7ffa7, 0xb5d0cf31, 0x2cd99e8b, 0x5bdeae1d, + 0x9b64c2b0, 0xec63f226, 0x756aa39c, 0x026d930a, 0x9c0906a9, 0xeb0e363f, + 0x72076785, 0x05005713, 0x95bf4a82, 0xe2b87a14, 0x7bb12bae, 0x0cb61b38, + 0x92d28e9b, 0xe5d5be0d, 0x7cdcefb7, 0x0bdbdf21, 0x86d3d2d4, 0xf1d4e242, + 0x68ddb3f8, 0x1fda836e, 0x81be16cd, 0xf6b9265b, 0x6fb077e1, 0x18b74777, + 0x88085ae6, 0xff0f6a70, 0x66063bca, 0x11010b5c, 0x8f659eff, 0xf862ae69, + 0x616bffd3, 0x166ccf45, 0xa00ae278, 0xd70dd2ee, 0x4e048354, 0x3903b3c2, + 0xa7672661, 0xd06016f7, 0x4969474d, 0x3e6e77db, 0xaed16a4a, 0xd9d65adc, + 0x40df0b66, 0x37d83bf0, 0xa9bcae53, 0xdebb9ec5, 0x47b2cf7f, 0x30b5ffe9, + 0xbdbdf21c, 0xcabac28a, 0x53b39330, 0x24b4a3a6, 0xbad03605, 0xcdd70693, + 0x54de5729, 0x23d967bf, 0xb3667a2e, 0xc4614ab8, 0x5d681b02, 0x2a6f2b94, + 0xb40bbe37, 0xc30c8ea1, 0x5a05df1b, 0x2d02ef8d}; + +static uint32_t crc32_byte(uint32_t accum, uint8_t delta) +{ + return crc32_table[(accum ^ delta) & 0xff] ^ (accum >> 8); +} + +static int probe_prefix(struct dfu_file *file) +{ + uint8_t *prefix = file->firmware; + + if (file->size.total < LMDFU_PREFIX_LENGTH) + return 1; + if ((prefix[0] == 0x01) && (prefix[1] == 0x00)) { + file->prefix_type = LMDFU_PREFIX; + file->size.prefix = LMDFU_PREFIX_LENGTH; + file->lmdfu_address = 1024 * ((prefix[3] << 8) | prefix[2]); + } + else if (((prefix[0] & 0x3f) == 0x1a) && ((prefix[1] & 0x3f)== 0x3f)) { + file->prefix_type = LPCDFU_UNENCRYPTED_PREFIX; + file->size.prefix = LPCDFU_PREFIX_LENGTH; + } + + if (file->size.prefix + file->size.suffix > file->size.total) + return 1; + return 0; +} + +void dfu_progress_bar(const char *desc, unsigned long long curr, + unsigned long long max) +{ + static char buf[PROGRESS_BAR_WIDTH + 1]; + static unsigned long long last_progress = -1; + static time_t last_time; + time_t curr_time = time(NULL); + unsigned long long progress; + unsigned long long x; + + /* check for not known maximum */ + if (max < curr) + max = curr + 1; + /* make none out of none give zero */ + if (max == 0 && curr == 0) + max = 1; + + /* compute completion */ + progress = (PROGRESS_BAR_WIDTH * curr) / max; + if (progress > PROGRESS_BAR_WIDTH) + progress = PROGRESS_BAR_WIDTH; + if (progress == last_progress && + curr_time == last_time) + return; + last_progress = progress; + last_time = curr_time; + + for (x = 0; x != PROGRESS_BAR_WIDTH; x++) { + if (x < progress) + buf[x] = '='; + else + buf[x] = ' '; + } + buf[x] = 0; + + printf("\r%s\t[%s] %3lld%% %12lld bytes", desc, buf, + (100ULL * curr) / max, curr); + + if (progress == PROGRESS_BAR_WIDTH) + printf("\n%s done.\n", desc); +} + +void *dfu_malloc(size_t size) +{ + void *ptr = malloc(size); + if (ptr == NULL) + errx(EX_SOFTWARE, "Cannot allocate memory of size %d bytes", (int)size); + return (ptr); +} + +uint32_t dfu_file_write_crc(int f, uint32_t crc, const void *buf, int size) +{ + int x; + + /* compute CRC */ + for (x = 0; x != size; x++) + crc = crc32_byte(crc, ((uint8_t *)buf)[x]); + + /* write data */ + if (write(f, buf, size) != size) + err(EX_IOERR, "Could not write %d bytes to file %d", size, f); + + return (crc); +} + +void dfu_load_file(struct dfu_file *file, enum suffix_req check_suffix, enum prefix_req check_prefix) +{ + off_t offset; + int f; + int i; + int res; + + file->size.prefix = 0; + file->size.suffix = 0; + + /* default values, if no valid suffix is found */ + file->bcdDFU = 0; + file->idVendor = 0xffff; /* wildcard value */ + file->idProduct = 0xffff; /* wildcard value */ + file->bcdDevice = 0xffff; /* wildcard value */ + + /* default values, if no valid prefix is found */ + file->lmdfu_address = 0; + + free(file->firmware); + + if (!strcmp(file->name, "-")) { + int read_bytes; + +#ifdef WIN32 + _setmode( _fileno( stdin ), _O_BINARY ); +#endif + file->firmware = (uint8_t*) dfu_malloc(STDIN_CHUNK_SIZE); + read_bytes = fread(file->firmware, 1, STDIN_CHUNK_SIZE, stdin); + file->size.total = read_bytes; + while (read_bytes == STDIN_CHUNK_SIZE) { + file->firmware = (uint8_t*) realloc(file->firmware, file->size.total + STDIN_CHUNK_SIZE); + if (!file->firmware) + err(EX_IOERR, "Could not allocate firmware buffer"); + read_bytes = fread(file->firmware + file->size.total, 1, STDIN_CHUNK_SIZE, stdin); + file->size.total += read_bytes; + } + if (verbose) + printf("Read %i bytes from stdin\n", file->size.total); + /* Never require suffix when reading from stdin */ + check_suffix = MAYBE_SUFFIX; + } else { + f = open(file->name, O_RDONLY | O_BINARY); + if (f < 0) + err(EX_IOERR, "Could not open file %s for reading", file->name); + + offset = lseek(f, 0, SEEK_END); + + if ((int)offset < 0 || (int)offset != offset) + err(EX_IOERR, "File size is too big"); + + if (lseek(f, 0, SEEK_SET) != 0) + err(EX_IOERR, "Could not seek to beginning"); + + file->size.total = offset; + file->firmware = dfu_malloc(file->size.total); + + if (read(f, file->firmware, file->size.total) != file->size.total) { + err(EX_IOERR, "Could not read %d bytes from %s", + file->size.total, file->name); + } + close(f); + } + + /* Check for possible DFU file suffix by trying to parse one */ + { + uint32_t crc = 0xffffffff; + const uint8_t *dfusuffix; + int missing_suffix = 0; + const char *reason; + + if (file->size.total < DFU_SUFFIX_LENGTH) { + reason = "File too short for DFU suffix"; + missing_suffix = 1; + goto checked; + } + + dfusuffix = file->firmware + file->size.total - + DFU_SUFFIX_LENGTH; + + for (i = 0; i < file->size.total - 4; i++) + crc = crc32_byte(crc, file->firmware[i]); + + if (dfusuffix[10] != 'D' || + dfusuffix[9] != 'F' || + dfusuffix[8] != 'U') { + reason = "Invalid DFU suffix signature"; + missing_suffix = 1; + goto checked; + } + + file->dwCRC = (dfusuffix[15] << 24) + + (dfusuffix[14] << 16) + + (dfusuffix[13] << 8) + + dfusuffix[12]; + + if (file->dwCRC != crc) { + reason = "DFU suffix CRC does not match"; + missing_suffix = 1; + goto checked; + } + + /* At this point we believe we have a DFU suffix + so we require further checks to succeed */ + + file->bcdDFU = (dfusuffix[7] << 8) + dfusuffix[6]; + + if (verbose) + printf("DFU suffix version %x\n", file->bcdDFU); + + file->size.suffix = dfusuffix[11]; + + if (file->size.suffix < DFU_SUFFIX_LENGTH) { + errx(EX_IOERR, "Unsupported DFU suffix length %d", + file->size.suffix); + } + + if (file->size.suffix > file->size.total) { + errx(EX_IOERR, "Invalid DFU suffix length %d", + file->size.suffix); + } + + file->idVendor = (dfusuffix[5] << 8) + dfusuffix[4]; + file->idProduct = (dfusuffix[3] << 8) + dfusuffix[2]; + file->bcdDevice = (dfusuffix[1] << 8) + dfusuffix[0]; + +checked: + if (missing_suffix) { + if (check_suffix == NEEDS_SUFFIX) { + warnx("%s", reason); + errx(EX_IOERR, "Valid DFU suffix needed"); + } else if (check_suffix == MAYBE_SUFFIX) { + warnx("%s", reason); + warnx("A valid DFU suffix will be required in " + "a future dfu-util release!!!"); + } + } else { + if (check_suffix == NO_SUFFIX) { + errx(EX_SOFTWARE, "Please remove existing DFU suffix before adding a new one.\n"); + } + } + } + res = probe_prefix(file); + if ((res || file->size.prefix == 0) && check_prefix == NEEDS_PREFIX) + errx(EX_IOERR, "Valid DFU prefix needed"); + if (file->size.prefix && check_prefix == NO_PREFIX) + errx(EX_IOERR, "A prefix already exists, please delete it first"); + if (file->size.prefix && verbose) { + uint8_t *data = file->firmware; + if (file->prefix_type == LMDFU_PREFIX) + printf("Possible TI Stellaris DFU prefix with " + "the following properties\n" + "Address: 0x%08x\n" + "Payload length: %d\n", + file->lmdfu_address, + data[4] | (data[5] << 8) | + (data[6] << 16) | (data[7] << 14)); + else if (file->prefix_type == LPCDFU_UNENCRYPTED_PREFIX) + printf("Possible unencrypted NXP LPC DFU prefix with " + "the following properties\n" + "Payload length: %d kiByte\n", + data[2] >>1 | (data[3] << 7) ); + else + errx(EX_IOERR, "Unknown DFU prefix type"); + } +} + +void dfu_store_file(struct dfu_file *file, int write_suffix, int write_prefix) +{ + uint32_t crc = 0xffffffff; + int f; + + f = open(file->name, O_WRONLY | O_BINARY | O_TRUNC | O_CREAT, 0666); + if (f < 0) + err(EX_IOERR, "Could not open file %s for writing", file->name); + + /* write prefix, if any */ + if (write_prefix) { + if (file->prefix_type == LMDFU_PREFIX) { + uint8_t lmdfu_prefix[LMDFU_PREFIX_LENGTH]; + uint32_t addr = file->lmdfu_address / 1024; + + /* lmdfu_dfu_prefix payload length excludes prefix and suffix */ + uint32_t len = file->size.total - + file->size.prefix - file->size.suffix; + + lmdfu_prefix[0] = 0x01; /* STELLARIS_DFU_PROG */ + lmdfu_prefix[1] = 0x00; /* Reserved */ + lmdfu_prefix[2] = (uint8_t)(addr & 0xff); + lmdfu_prefix[3] = (uint8_t)(addr >> 8); + lmdfu_prefix[4] = (uint8_t)(len & 0xff); + lmdfu_prefix[5] = (uint8_t)(len >> 8) & 0xff; + lmdfu_prefix[6] = (uint8_t)(len >> 16) & 0xff; + lmdfu_prefix[7] = (uint8_t)(len >> 24); + + crc = dfu_file_write_crc(f, crc, lmdfu_prefix, LMDFU_PREFIX_LENGTH); + } + if (file->prefix_type == LPCDFU_UNENCRYPTED_PREFIX) { + uint8_t lpcdfu_prefix[LPCDFU_PREFIX_LENGTH] = {0}; + int i; + + /* Payload is firmware and prefix rounded to 512 bytes */ + uint32_t len = (file->size.total - file->size.suffix + 511) /512; + + lpcdfu_prefix[0] = 0x1a; /* Unencypted*/ + lpcdfu_prefix[1] = 0x3f; /* Reserved */ + lpcdfu_prefix[2] = (uint8_t)(len & 0xff); + lpcdfu_prefix[3] = (uint8_t)((len >> 8) & 0xff); + for (i = 12; i < LPCDFU_PREFIX_LENGTH; i++) + lpcdfu_prefix[i] = 0xff; + + crc = dfu_file_write_crc(f, crc, lpcdfu_prefix, LPCDFU_PREFIX_LENGTH); + } + } + /* write firmware binary */ + crc = dfu_file_write_crc(f, crc, file->firmware + file->size.prefix, + file->size.total - file->size.prefix - file->size.suffix); + + /* write suffix, if any */ + if (write_suffix) { + uint8_t dfusuffix[DFU_SUFFIX_LENGTH]; + + dfusuffix[0] = file->bcdDevice & 0xff; + dfusuffix[1] = file->bcdDevice >> 8; + dfusuffix[2] = file->idProduct & 0xff; + dfusuffix[3] = file->idProduct >> 8; + dfusuffix[4] = file->idVendor & 0xff; + dfusuffix[5] = file->idVendor >> 8; + dfusuffix[6] = file->bcdDFU & 0xff; + dfusuffix[7] = file->bcdDFU >> 8; + dfusuffix[8] = 'U'; + dfusuffix[9] = 'F'; + dfusuffix[10] = 'D'; + dfusuffix[11] = DFU_SUFFIX_LENGTH; + + crc = dfu_file_write_crc(f, crc, dfusuffix, + DFU_SUFFIX_LENGTH - 4); + + dfusuffix[12] = crc; + dfusuffix[13] = crc >> 8; + dfusuffix[14] = crc >> 16; + dfusuffix[15] = crc >> 24; + + crc = dfu_file_write_crc(f, crc, dfusuffix + 12, 4); + } + close(f); +} + +void show_suffix_and_prefix(struct dfu_file *file) +{ + if (file->size.prefix == LMDFU_PREFIX_LENGTH) { + printf("The file %s contains a TI Stellaris DFU prefix with the following properties:\n", file->name); + printf("Address:\t0x%08x\n", file->lmdfu_address); + } else if (file->size.prefix == LPCDFU_PREFIX_LENGTH) { + uint8_t * prefix = file->firmware; + printf("The file %s contains a NXP unencrypted LPC DFU prefix with the following properties:\n", file->name); + printf("Size:\t%5d kiB\n", prefix[2]>>1|prefix[3]<<7); + } else if (file->size.prefix != 0) { + printf("The file %s contains an unknown prefix\n", file->name); + } + if (file->size.suffix > 0) { + printf("The file %s contains a DFU suffix with the following properties:\n", file->name); + printf("BCD device:\t0x%04X\n", file->bcdDevice); + printf("Product ID:\t0x%04X\n",file->idProduct); + printf("Vendor ID:\t0x%04X\n", file->idVendor); + printf("BCD DFU:\t0x%04X\n", file->bcdDFU); + printf("Length:\t\t%i\n", file->size.suffix); + printf("CRC:\t\t0x%08X\n", file->dwCRC); + } +} diff --git a/tools/linux64/src/dfu-util/src/dfu_file.h b/tools/linux64/src/dfu-util/src/dfu_file.h new file mode 100644 index 0000000..abebd44 --- /dev/null +++ b/tools/linux64/src/dfu-util/src/dfu_file.h @@ -0,0 +1,60 @@ + +#ifndef DFU_FILE_H +#define DFU_FILE_H + +#include + +struct dfu_file { + /* File name */ + const char *name; + /* Pointer to file loaded into memory */ + uint8_t *firmware; + /* Different sizes */ + struct { + int total; + int prefix; + int suffix; + } size; + /* From prefix fields */ + uint32_t lmdfu_address; + /* From prefix fields */ + uint32_t prefix_type; + + /* From DFU suffix fields */ + uint32_t dwCRC; + uint16_t bcdDFU; + uint16_t idVendor; + uint16_t idProduct; + uint16_t bcdDevice; +}; + +enum suffix_req { + NO_SUFFIX, + NEEDS_SUFFIX, + MAYBE_SUFFIX +}; + +enum prefix_req { + NO_PREFIX, + NEEDS_PREFIX, + MAYBE_PREFIX +}; + +enum prefix_type { + ZERO_PREFIX, + LMDFU_PREFIX, + LPCDFU_UNENCRYPTED_PREFIX +}; + +extern int verbose; + +void dfu_load_file(struct dfu_file *file, enum suffix_req check_suffix, enum prefix_req check_prefix); +void dfu_store_file(struct dfu_file *file, int write_suffix, int write_prefix); + +void dfu_progress_bar(const char *desc, unsigned long long curr, + unsigned long long max); +void *dfu_malloc(size_t size); +uint32_t dfu_file_write_crc(int f, uint32_t crc, const void *buf, int size); +void show_suffix_and_prefix(struct dfu_file *file); + +#endif /* DFU_FILE_H */ diff --git a/tools/linux64/src/dfu-util/src/dfu_load.c b/tools/linux64/src/dfu-util/src/dfu_load.c new file mode 100644 index 0000000..64f7009 --- /dev/null +++ b/tools/linux64/src/dfu-util/src/dfu_load.c @@ -0,0 +1,196 @@ +/* + * DFU transfer routines + * + * This is supposed to be a general DFU implementation, as specified in the + * USB DFU 1.0 and 1.1 specification. + * + * The code was originally intended to interface with a USB device running the + * "sam7dfu" firmware (see http://www.openpcd.org/) on an AT91SAM7 processor. + * + * Copyright 2007-2008 Harald Welte + * Copyright 2013 Hans Petter Selasky + * Copyright 2014 Tormod Volden + * + * 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 +#include +#include +#include + +#include + +#include "portable.h" +#include "dfu.h" +#include "usb_dfu.h" +#include "dfu_file.h" +#include "dfu_load.h" +#include "quirks.h" + +int dfuload_do_upload(struct dfu_if *dif, int xfer_size, + int expected_size, int fd) +{ + int total_bytes = 0; + unsigned short transaction = 0; + unsigned char *buf; + int ret; + + buf = dfu_malloc(xfer_size); + + printf("Copying data from DFU device to PC\n"); + dfu_progress_bar("Upload", 0, 1); + + while (1) { + int rc; + rc = dfu_upload(dif->dev_handle, dif->interface, + xfer_size, transaction++, buf); + if (rc < 0) { + warnx("Error during upload"); + ret = rc; + goto out_free; + } + + dfu_file_write_crc(fd, 0, buf, rc); + total_bytes += rc; + + if (total_bytes < 0) + errx(EX_SOFTWARE, "Received too many bytes (wraparound)"); + + if (rc < xfer_size) { + /* last block, return */ + ret = total_bytes; + break; + } + dfu_progress_bar("Upload", total_bytes, expected_size); + } + ret = 0; + +out_free: + dfu_progress_bar("Upload", total_bytes, total_bytes); + if (total_bytes == 0) + printf("\nFailed.\n"); + free(buf); + if (verbose) + printf("Received a total of %i bytes\n", total_bytes); + if (expected_size != 0 && total_bytes != expected_size) + errx(EX_SOFTWARE, "Unexpected number of bytes uploaded from device"); + return ret; +} + +int dfuload_do_dnload(struct dfu_if *dif, int xfer_size, struct dfu_file *file) +{ + int bytes_sent; + int expected_size; + unsigned char *buf; + unsigned short transaction = 0; + struct dfu_status dst; + int ret; + + printf("Copying data from PC to DFU device\n"); + + buf = file->firmware; + expected_size = file->size.total - file->size.suffix; + bytes_sent = 0; + + dfu_progress_bar("Download", 0, 1); + while (bytes_sent < expected_size) { + int bytes_left; + int chunk_size; + + bytes_left = expected_size - bytes_sent; + if (bytes_left < xfer_size) + chunk_size = bytes_left; + else + chunk_size = xfer_size; + + ret = dfu_download(dif->dev_handle, dif->interface, + chunk_size, transaction++, chunk_size ? buf : NULL); + if (ret < 0) { + warnx("Error during download"); + goto out; + } + bytes_sent += chunk_size; + buf += chunk_size; + + do { + ret = dfu_get_status(dif, &dst); + if (ret < 0) { + errx(EX_IOERR, "Error during download get_status"); + goto out; + } + + if (dst.bState == DFU_STATE_dfuDNLOAD_IDLE || + dst.bState == DFU_STATE_dfuERROR) + break; + + /* Wait while device executes flashing */ + milli_sleep(dst.bwPollTimeout); + + } while (1); + 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; + } + dfu_progress_bar("Download", bytes_sent, bytes_sent + bytes_left); + } + + /* send one zero sized download request to signalize end */ + ret = dfu_download(dif->dev_handle, dif->interface, + 0, transaction, NULL); + if (ret < 0) { + errx(EX_IOERR, "Error sending completion packet"); + goto out; + } + + dfu_progress_bar("Download", bytes_sent, bytes_sent); + + if (verbose) + printf("Sent a total of %i bytes\n", bytes_sent); + +get_status: + /* Transition to MANIFEST_SYNC state */ + ret = dfu_get_status(dif, &dst); + if (ret < 0) { + warnx("unable to read DFU status after completion"); + goto out; + } + printf("state(%u) = %s, status(%u) = %s\n", dst.bState, + dfu_state_to_string(dst.bState), dst.bStatus, + dfu_status_to_string(dst.bStatus)); + + milli_sleep(dst.bwPollTimeout); + + /* 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 */ + milli_sleep(1000); + goto get_status; + break; + case DFU_STATE_dfuIDLE: + break; + } + printf("Done!\n"); + +out: + return bytes_sent; +} diff --git a/tools/linux64/src/dfu-util/src/dfu_load.h b/tools/linux64/src/dfu-util/src/dfu_load.h new file mode 100644 index 0000000..be23e9b --- /dev/null +++ b/tools/linux64/src/dfu-util/src/dfu_load.h @@ -0,0 +1,7 @@ +#ifndef DFU_LOAD_H +#define DFU_LOAD_H + +int dfuload_do_upload(struct dfu_if *dif, int xfer_size, int expected_size, int fd); +int dfuload_do_dnload(struct dfu_if *dif, int xfer_size, struct dfu_file *file); + +#endif /* DFU_LOAD_H */ diff --git a/tools/linux64/src/dfu-util/src/dfu_util.c b/tools/linux64/src/dfu-util/src/dfu_util.c new file mode 100644 index 0000000..b94c7cc --- /dev/null +++ b/tools/linux64/src/dfu-util/src/dfu_util.c @@ -0,0 +1,346 @@ +/* + * Functions for detecting DFU USB entities + * + * Written by Harald Welte + * Copyright 2007-2008 by OpenMoko, Inc. + * Copyright 2013 Hans Petter Selasky + * + * 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 +#include +#include +#include +#include + +#include "portable.h" +#include "dfu.h" +#include "usb_dfu.h" +#include "dfu_file.h" +#include "dfu_load.h" +#include "dfu_util.h" +#include "dfuse.h" +#include "quirks.h" + +#ifdef HAVE_USBPATH_H +#include +#endif + +/* + * Look for a descriptor in a concatenated descriptor list. Will + * return upon the first match of the given descriptor type. Returns length of + * found descriptor, limited to res_size + */ +static int find_descriptor(const uint8_t *desc_list, int list_len, + uint8_t desc_type, void *res_buf, int res_size) +{ + int p = 0; + + if (list_len < 2) + return (-1); + + while (p + 1 < list_len) { + int desclen; + + desclen = (int) desc_list[p]; + if (desclen == 0) { + warnx("Invalid descriptor list"); + return -1; + } + if (desc_list[p + 1] == desc_type) { + if (desclen > res_size) + desclen = res_size; + if (p + desclen > list_len) + desclen = list_len - p; + memcpy(res_buf, &desc_list[p], desclen); + return desclen; + } + p += (int) desc_list[p]; + } + return -1; +} + +static void probe_configuration(libusb_device *dev, struct libusb_device_descriptor *desc) +{ + struct usb_dfu_func_descriptor func_dfu; + libusb_device_handle *devh; + struct dfu_if *pdfu; + struct libusb_config_descriptor *cfg; + const struct libusb_interface_descriptor *intf; + const struct libusb_interface *uif; + char alt_name[MAX_DESC_STR_LEN + 1]; + char serial_name[MAX_DESC_STR_LEN + 1]; + int cfg_idx; + int intf_idx; + int alt_idx; + int ret; + int has_dfu; + + for (cfg_idx = 0; cfg_idx != desc->bNumConfigurations; cfg_idx++) { + memset(&func_dfu, 0, sizeof(func_dfu)); + has_dfu = 0; + + ret = libusb_get_config_descriptor(dev, cfg_idx, &cfg); + if (ret != 0) + return; + if (match_config_index > -1 && match_config_index != cfg->bConfigurationValue) { + libusb_free_config_descriptor(cfg); + continue; + } + + /* + * In some cases, noticably FreeBSD if uid != 0, + * the configuration descriptors are empty + */ + if (!cfg) + return; + + ret = find_descriptor(cfg->extra, cfg->extra_length, + USB_DT_DFU, &func_dfu, sizeof(func_dfu)); + if (ret > -1) + goto found_dfu; + + for (intf_idx = 0; intf_idx < cfg->bNumInterfaces; + intf_idx++) { + uif = &cfg->interface[intf_idx]; + if (!uif) + break; + + for (alt_idx = 0; alt_idx < cfg->interface[intf_idx].num_altsetting; + alt_idx++) { + intf = &uif->altsetting[alt_idx]; + + ret = find_descriptor(intf->extra, intf->extra_length, USB_DT_DFU, + &func_dfu, sizeof(func_dfu)); + if (ret > -1) + goto found_dfu; + + if (intf->bInterfaceClass != 0xfe || + intf->bInterfaceSubClass != 1) + continue; + + has_dfu = 1; + } + } + if (has_dfu) { + /* + * Finally try to retrieve it requesting the + * device directly This is not supported on + * all devices for non-standard types + */ + if (libusb_open(dev, &devh) == 0) { + ret = libusb_get_descriptor(devh, USB_DT_DFU, 0, + (void *)&func_dfu, sizeof(func_dfu)); + libusb_close(devh); + if (ret > -1) + goto found_dfu; + } + warnx("Device has DFU interface, " + "but has no DFU functional descriptor"); + + /* fake version 1.0 */ + func_dfu.bLength = 7; + func_dfu.bcdDFUVersion = libusb_cpu_to_le16(0x0100); + goto found_dfu; + } + libusb_free_config_descriptor(cfg); + continue; + +found_dfu: + if (func_dfu.bLength == 7) { + printf("Deducing device DFU version from functional descriptor " + "length\n"); + func_dfu.bcdDFUVersion = libusb_cpu_to_le16(0x0100); + } else if (func_dfu.bLength < 9) { + printf("Error obtaining DFU functional descriptor\n"); + printf("Please report this as a bug!\n"); + printf("Warning: Assuming DFU version 1.0\n"); + func_dfu.bcdDFUVersion = libusb_cpu_to_le16(0x0100); + printf("Warning: Transfer size can not be detected\n"); + func_dfu.wTransferSize = 0; + } + + for (intf_idx = 0; intf_idx < cfg->bNumInterfaces; + intf_idx++) { + if (match_iface_index > -1 && match_iface_index != intf_idx) + continue; + + uif = &cfg->interface[intf_idx]; + if (!uif) + break; + + for (alt_idx = 0; + alt_idx < uif->num_altsetting; alt_idx++) { + int dfu_mode; + + intf = &uif->altsetting[alt_idx]; + + if (intf->bInterfaceClass != 0xfe || + intf->bInterfaceSubClass != 1) + continue; + + dfu_mode = (intf->bInterfaceProtocol == 2); + /* e.g. DSO Nano has bInterfaceProtocol 0 instead of 2 */ + if (func_dfu.bcdDFUVersion == 0x011a && intf->bInterfaceProtocol == 0) + dfu_mode = 1; + + if (dfu_mode && + match_iface_alt_index > -1 && match_iface_alt_index != alt_idx) + continue; + + if (dfu_mode) { + if ((match_vendor_dfu >= 0 && match_vendor_dfu != desc->idVendor) || + (match_product_dfu >= 0 && match_product_dfu != desc->idProduct)) { + continue; + } + } else { + if ((match_vendor >= 0 && match_vendor != desc->idVendor) || + (match_product >= 0 && match_product != desc->idProduct)) { + continue; + } + } + + if (libusb_open(dev, &devh)) { + warnx("Cannot open DFU device %04x:%04x", desc->idVendor, desc->idProduct); + break; + } + if (intf->iInterface != 0) + ret = libusb_get_string_descriptor_ascii(devh, + intf->iInterface, (void *)alt_name, MAX_DESC_STR_LEN); + else + ret = -1; + if (ret < 1) + strcpy(alt_name, "UNKNOWN"); + if (desc->iSerialNumber != 0) + ret = libusb_get_string_descriptor_ascii(devh, + desc->iSerialNumber, (void *)serial_name, MAX_DESC_STR_LEN); + else + ret = -1; + if (ret < 1) + strcpy(serial_name, "UNKNOWN"); + libusb_close(devh); + + if (dfu_mode && + match_iface_alt_name != NULL && strcmp(alt_name, match_iface_alt_name)) + continue; + + if (dfu_mode) { + if (match_serial_dfu != NULL && strcmp(match_serial_dfu, serial_name)) + continue; + } else { + if (match_serial != NULL && strcmp(match_serial, serial_name)) + continue; + } + + pdfu = dfu_malloc(sizeof(*pdfu)); + + memset(pdfu, 0, sizeof(*pdfu)); + + pdfu->func_dfu = func_dfu; + pdfu->dev = libusb_ref_device(dev); + pdfu->quirks = get_quirks(desc->idVendor, + desc->idProduct, desc->bcdDevice); + pdfu->vendor = desc->idVendor; + pdfu->product = desc->idProduct; + pdfu->bcdDevice = desc->bcdDevice; + pdfu->configuration = cfg->bConfigurationValue; + pdfu->interface = intf->bInterfaceNumber; + pdfu->altsetting = intf->bAlternateSetting; + pdfu->devnum = libusb_get_device_address(dev); + pdfu->busnum = libusb_get_bus_number(dev); + pdfu->alt_name = strdup(alt_name); + if (pdfu->alt_name == NULL) + errx(EX_SOFTWARE, "Out of memory"); + pdfu->serial_name = strdup(serial_name); + if (pdfu->serial_name == NULL) + errx(EX_SOFTWARE, "Out of memory"); + if (dfu_mode) + pdfu->flags |= DFU_IFF_DFU; + if (pdfu->quirks & QUIRK_FORCE_DFU11) { + pdfu->func_dfu.bcdDFUVersion = + libusb_cpu_to_le16(0x0110); + } + pdfu->bMaxPacketSize0 = desc->bMaxPacketSize0; + + /* queue into list */ + pdfu->next = dfu_root; + dfu_root = pdfu; + } + } + libusb_free_config_descriptor(cfg); + } +} + +void probe_devices(libusb_context *ctx) +{ + libusb_device **list; + ssize_t num_devs; + ssize_t i; + + num_devs = libusb_get_device_list(ctx, &list); + for (i = 0; i < num_devs; ++i) { + struct libusb_device_descriptor desc; + struct libusb_device *dev = list[i]; + + if (match_bus > -1 && match_bus != libusb_get_bus_number(dev)) + continue; + if (match_device > -1 && match_device != libusb_get_device_address(dev)) + continue; + if (libusb_get_device_descriptor(dev, &desc)) + continue; + probe_configuration(dev, &desc); + } + libusb_free_device_list(list, 0); +} + +void disconnect_devices(void) +{ + struct dfu_if *pdfu; + struct dfu_if *prev = NULL; + + for (pdfu = dfu_root; pdfu != NULL; pdfu = pdfu->next) { + free(prev); + libusb_unref_device(pdfu->dev); + free(pdfu->alt_name); + free(pdfu->serial_name); + prev = pdfu; + } + free(prev); + dfu_root = NULL; +} + +void print_dfu_if(struct dfu_if *dfu_if) +{ + printf("Found %s: [%04x:%04x] ver=%04x, devnum=%u, cfg=%u, intf=%u, " + "alt=%u, name=\"%s\", serial=\"%s\"\n", + dfu_if->flags & DFU_IFF_DFU ? "DFU" : "Runtime", + dfu_if->vendor, dfu_if->product, + dfu_if->bcdDevice, dfu_if->devnum, + dfu_if->configuration, dfu_if->interface, + dfu_if->altsetting, dfu_if->alt_name, + dfu_if->serial_name); +} + +/* Walk the device tree and print out DFU devices */ +void list_dfu_interfaces(void) +{ + struct dfu_if *pdfu; + + for (pdfu = dfu_root; pdfu != NULL; pdfu = pdfu->next) + print_dfu_if(pdfu); +} diff --git a/tools/linux64/src/dfu-util/src/dfu_util.h b/tools/linux64/src/dfu-util/src/dfu_util.h new file mode 100644 index 0000000..fc0c19d --- /dev/null +++ b/tools/linux64/src/dfu-util/src/dfu_util.h @@ -0,0 +1,36 @@ +#ifndef DFU_UTIL_H +#define DFU_UTIL_H + +/* USB string descriptor should contain max 126 UTF-16 characters + * but 253 would even accomodate any UTF-8 encoding */ +#define MAX_DESC_STR_LEN 253 + +enum mode { + MODE_NONE, + MODE_VERSION, + MODE_LIST, + MODE_DETACH, + MODE_UPLOAD, + MODE_DOWNLOAD +}; + +extern struct dfu_if *dfu_root; +extern int match_bus; +extern int match_device; +extern int match_vendor; +extern int match_product; +extern int match_vendor_dfu; +extern int match_product_dfu; +extern int match_config_index; +extern int match_iface_index; +extern int match_iface_alt_index; +extern const char *match_iface_alt_name; +extern const char *match_serial; +extern const char *match_serial_dfu; + +void probe_devices(libusb_context *); +void disconnect_devices(void); +void print_dfu_if(struct dfu_if *); +void list_dfu_interfaces(void); + +#endif /* DFU_UTIL_H */ diff --git a/tools/linux64/src/dfu-util/src/dfuse.c b/tools/linux64/src/dfu-util/src/dfuse.c new file mode 100644 index 0000000..fce29fe --- /dev/null +++ b/tools/linux64/src/dfu-util/src/dfuse.c @@ -0,0 +1,652 @@ +/* + * DfuSe specific functions + * + * This implements the ST Microsystems DFU extensions (DfuSe) + * as per the DfuSe 1.1a specification (ST documents AN3156, AN2606) + * The DfuSe file format is described in ST document UM0391. + * + * Copyright 2010-2014 Tormod Volden + * + * 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 +#include +#include +#include + +#include "portable.h" +#include "dfu.h" +#include "usb_dfu.h" +#include "dfu_file.h" +#include "dfuse.h" +#include "dfuse_mem.h" + +#define DFU_TIMEOUT 5000 + +extern int verbose; +static unsigned int last_erased_page = 1; /* non-aligned value, won't match */ +static struct memsegment *mem_layout; +static unsigned int dfuse_address = 0; +static unsigned int dfuse_length = 0; +static int dfuse_force = 0; +static int dfuse_leave = 0; +static int dfuse_unprotect = 0; +static int dfuse_mass_erase = 0; + +unsigned int quad2uint(unsigned char *p) +{ + return (*p + (*(p + 1) << 8) + (*(p + 2) << 16) + (*(p + 3) << 24)); +} + +void dfuse_parse_options(const char *options) +{ + char *end; + const char *endword; + unsigned int number; + + /* address, possibly empty, must be first */ + if (*options != ':') { + endword = strchr(options, ':'); + if (!endword) + endword = options + strlen(options); /* GNU strchrnul */ + + number = strtoul(options, &end, 0); + if (end == endword) { + dfuse_address = number; + } else { + errx(EX_IOERR, "Invalid dfuse address: %s", options); + } + options = endword; + } + + while (*options) { + if (*options == ':') { + options++; + continue; + } + endword = strchr(options, ':'); + if (!endword) + endword = options + strlen(options); + + if (!strncmp(options, "force", endword - options)) { + dfuse_force++; + options += 5; + continue; + } + if (!strncmp(options, "leave", endword - options)) { + dfuse_leave = 1; + options += 5; + continue; + } + if (!strncmp(options, "unprotect", endword - options)) { + dfuse_unprotect = 1; + options += 9; + continue; + } + if (!strncmp(options, "mass-erase", endword - options)) { + dfuse_mass_erase = 1; + options += 10; + continue; + } + + /* any valid number is interpreted as upload length */ + number = strtoul(options, &end, 0); + if (end == endword) { + dfuse_length = number; + } else { + errx(EX_IOERR, "Invalid dfuse modifier: %s", options); + } + options = endword; + } +} + +/* DFU_UPLOAD request for DfuSe 1.1a */ +int dfuse_upload(struct dfu_if *dif, const unsigned short length, + unsigned char *data, unsigned short transaction) +{ + int status; + + status = libusb_control_transfer(dif->dev_handle, + /* bmRequestType */ LIBUSB_ENDPOINT_IN | + LIBUSB_REQUEST_TYPE_CLASS | + LIBUSB_RECIPIENT_INTERFACE, + /* bRequest */ DFU_UPLOAD, + /* wValue */ transaction, + /* wIndex */ dif->interface, + /* Data */ data, + /* wLength */ length, + DFU_TIMEOUT); + if (status < 0) { + errx(EX_IOERR, "%s: libusb_control_msg returned %d", + __FUNCTION__, status); + } + return status; +} + +/* DFU_DNLOAD request for DfuSe 1.1a */ +int dfuse_download(struct dfu_if *dif, const unsigned short length, + unsigned char *data, unsigned short transaction) +{ + int status; + + status = libusb_control_transfer(dif->dev_handle, + /* bmRequestType */ LIBUSB_ENDPOINT_OUT | + LIBUSB_REQUEST_TYPE_CLASS | + LIBUSB_RECIPIENT_INTERFACE, + /* bRequest */ DFU_DNLOAD, + /* wValue */ transaction, + /* wIndex */ dif->interface, + /* Data */ data, + /* wLength */ length, + DFU_TIMEOUT); + if (status < 0) { + errx(EX_IOERR, "%s: libusb_control_transfer returned %d", + __FUNCTION__, status); + } + return status; +} + +/* DfuSe only commands */ +/* Leaves the device in dfuDNLOAD-IDLE state */ +int dfuse_special_command(struct dfu_if *dif, unsigned int address, + enum dfuse_command command) +{ + const char* dfuse_command_name[] = { "SET_ADDRESS" , "ERASE_PAGE", + "MASS_ERASE", "READ_UNPROTECT"}; + unsigned char buf[5]; + int length; + int ret; + struct dfu_status dst; + int firstpoll = 1; + + if (command == ERASE_PAGE) { + struct memsegment *segment; + int page_size; + + segment = find_segment(mem_layout, address); + if (!segment || !(segment->memtype & DFUSE_ERASABLE)) { + errx(EX_IOERR, "Page at 0x%08x can not be erased", + address); + } + page_size = segment->pagesize; + if (verbose > 1) + printf("Erasing page size %i at address 0x%08x, page " + "starting at 0x%08x\n", page_size, address, + address & ~(page_size - 1)); + buf[0] = 0x41; /* Erase command */ + length = 5; + last_erased_page = address & ~(page_size - 1); + } else if (command == SET_ADDRESS) { + if (verbose > 2) + printf(" Setting address pointer to 0x%08x\n", + address); + buf[0] = 0x21; /* Set Address Pointer command */ + length = 5; + } else if (command == MASS_ERASE) { + buf[0] = 0x41; /* Mass erase command when length = 1 */ + length = 1; + } else if (command == READ_UNPROTECT) { + buf[0] = 0x92; + length = 1; + } else { + errx(EX_IOERR, "Non-supported special command %d", command); + } + buf[1] = address & 0xff; + buf[2] = (address >> 8) & 0xff; + buf[3] = (address >> 16) & 0xff; + buf[4] = (address >> 24) & 0xff; + + ret = dfuse_download(dif, length, buf, 0); + if (ret < 0) { + errx(EX_IOERR, "Error during special command \"%s\" download", + dfuse_command_name[command]); + } + do { + ret = dfu_get_status(dif, &dst); + if (ret < 0) { + errx(EX_IOERR, "Error during special command \"%s\" get_status", + dfuse_command_name[command]); + } + if (firstpoll) { + firstpoll = 0; + if (dst.bState != DFU_STATE_dfuDNBUSY) { + printf("state(%u) = %s, status(%u) = %s\n", dst.bState, + dfu_state_to_string(dst.bState), dst.bStatus, + dfu_status_to_string(dst.bStatus)); + errx(EX_IOERR, "Wrong state after command \"%s\" download", + dfuse_command_name[command]); + } + } + /* wait while command is executed */ + if (verbose) + printf(" Poll timeout %i ms\n", dst.bwPollTimeout); + milli_sleep(dst.bwPollTimeout); + if (command == READ_UNPROTECT) + return ret; + } while (dst.bState == DFU_STATE_dfuDNBUSY); + + if (dst.bStatus != DFU_STATUS_OK) { + errx(EX_IOERR, "%s not correctly executed", + dfuse_command_name[command]); + } + return ret; +} + +int dfuse_dnload_chunk(struct dfu_if *dif, unsigned char *data, int size, + int transaction) +{ + int bytes_sent; + struct dfu_status dst; + int ret; + + ret = dfuse_download(dif, size, size ? data : NULL, transaction); + if (ret < 0) { + errx(EX_IOERR, "Error during download"); + return ret; + } + bytes_sent = ret; + + do { + ret = dfu_get_status(dif, &dst); + if (ret < 0) { + errx(EX_IOERR, "Error during download get_status"); + return ret; + } + milli_sleep(dst.bwPollTimeout); + } while (dst.bState != DFU_STATE_dfuDNLOAD_IDLE && + dst.bState != DFU_STATE_dfuERROR && + dst.bState != DFU_STATE_dfuMANIFEST); + + if (dst.bState == DFU_STATE_dfuMANIFEST) + printf("Transitioning to dfuMANIFEST state\n"); + + 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)); + return -1; + } + return bytes_sent; +} + +int dfuse_do_upload(struct dfu_if *dif, int xfer_size, int fd, + const char *dfuse_options) +{ + int total_bytes = 0; + int upload_limit = 0; + unsigned char *buf; + int transaction; + int ret; + + buf = dfu_malloc(xfer_size); + + if (dfuse_options) + dfuse_parse_options(dfuse_options); + if (dfuse_length) + upload_limit = dfuse_length; + if (dfuse_address) { + struct memsegment *segment; + + mem_layout = parse_memory_layout((char *)dif->alt_name); + if (!mem_layout) + errx(EX_IOERR, "Failed to parse memory layout"); + + segment = find_segment(mem_layout, dfuse_address); + if (!dfuse_force && + (!segment || !(segment->memtype & DFUSE_READABLE))) + errx(EX_IOERR, "Page at 0x%08x is not readable", + dfuse_address); + + if (!upload_limit) { + upload_limit = segment->end - dfuse_address + 1; + printf("Limiting upload to end of memory segment, " + "%i bytes\n", upload_limit); + } + dfuse_special_command(dif, dfuse_address, SET_ADDRESS); + dfu_abort_to_idle(dif); + } else { + /* Boot loader decides the start address, unknown to us */ + /* Use a short length to lower risk of running out of bounds */ + if (!upload_limit) + upload_limit = 0x4000; + printf("Limiting default upload to %i bytes\n", upload_limit); + } + + dfu_progress_bar("Upload", 0, 1); + + transaction = 2; + while (1) { + int rc; + + /* last chunk can be smaller than original xfer_size */ + if (upload_limit - total_bytes < xfer_size) + xfer_size = upload_limit - total_bytes; + rc = dfuse_upload(dif, xfer_size, buf, transaction++); + if (rc < 0) { + ret = rc; + goto out_free; + } + + dfu_file_write_crc(fd, 0, buf, rc); + total_bytes += rc; + + if (total_bytes < 0) + errx(EX_SOFTWARE, "Received too many bytes"); + + if (rc < xfer_size || total_bytes >= upload_limit) { + /* last block, return successfully */ + ret = total_bytes; + break; + } + dfu_progress_bar("Upload", total_bytes, upload_limit); + } + + dfu_progress_bar("Upload", total_bytes, total_bytes); + + dfu_abort_to_idle(dif); + if (dfuse_leave) { + dfuse_special_command(dif, dfuse_address, SET_ADDRESS); + dfuse_dnload_chunk(dif, NULL, 0, 2); /* Zero-size */ + } + + out_free: + free(buf); + + return ret; +} + +/* Writes an element of any size to the device, taking care of page erases */ +/* returns 0 on success, otherwise -EINVAL */ +int dfuse_dnload_element(struct dfu_if *dif, unsigned int dwElementAddress, + unsigned int dwElementSize, unsigned char *data, + int xfer_size) +{ + int p; + int ret; + struct memsegment *segment; + + /* Check at least that we can write to the last address */ + segment = + find_segment(mem_layout, dwElementAddress + dwElementSize - 1); + if (!segment || !(segment->memtype & DFUSE_WRITEABLE)) { + errx(EX_IOERR, "Last page at 0x%08x is not writeable", + dwElementAddress + dwElementSize - 1); + } + + dfu_progress_bar("Download", 0, 1); + + for (p = 0; p < (int)dwElementSize; p += xfer_size) { + int page_size; + unsigned int erase_address; + unsigned int address = dwElementAddress + p; + int chunk_size = xfer_size; + + segment = find_segment(mem_layout, address); + if (!segment || !(segment->memtype & DFUSE_WRITEABLE)) { + errx(EX_IOERR, "Page at 0x%08x is not writeable", + address); + } + page_size = segment->pagesize; + + /* check if this is the last chunk */ + if (p + chunk_size > (int)dwElementSize) + chunk_size = dwElementSize - p; + + /* Erase only for flash memory downloads */ + if ((segment->memtype & DFUSE_ERASABLE) && !dfuse_mass_erase) { + /* erase all involved pages */ + for (erase_address = address; + erase_address < address + chunk_size; + erase_address += page_size) + if ((erase_address & ~(page_size - 1)) != + last_erased_page) + dfuse_special_command(dif, + erase_address, + ERASE_PAGE); + + if (((address + chunk_size - 1) & ~(page_size - 1)) != + last_erased_page) { + if (verbose > 2) + printf(" Chunk extends into next page," + " erase it as well\n"); + dfuse_special_command(dif, + address + chunk_size - 1, + ERASE_PAGE); + } + } + + if (verbose) { + printf(" Download from image offset " + "%08x to memory %08x-%08x, size %i\n", + p, address, address + chunk_size - 1, + chunk_size); + } else { + dfu_progress_bar("Download", p, dwElementSize); + } + + dfuse_special_command(dif, address, SET_ADDRESS); + + /* transaction = 2 for no address offset */ + ret = dfuse_dnload_chunk(dif, data + p, chunk_size, 2); + if (ret != chunk_size) { + errx(EX_IOERR, "Failed to write whole chunk: " + "%i of %i bytes", ret, chunk_size); + return -EINVAL; + } + } + if (!verbose) + dfu_progress_bar("Download", dwElementSize, dwElementSize); + return 0; +} + +static void +dfuse_memcpy(unsigned char *dst, unsigned char **src, int *rem, int size) +{ + if (size > *rem) { + errx(EX_IOERR, "Corrupt DfuSe file: " + "Cannot read %d bytes from %d bytes", size, *rem); + } + if (dst != NULL) + memcpy(dst, *src, size); + (*src) += size; + (*rem) -= size; +} + +/* Download raw binary file to DfuSe device */ +int dfuse_do_bin_dnload(struct dfu_if *dif, int xfer_size, + struct dfu_file *file, unsigned int start_address) +{ + unsigned int dwElementAddress; + unsigned int dwElementSize; + unsigned char *data; + int ret; + + dwElementAddress = start_address; + dwElementSize = file->size.total - + file->size.suffix - file->size.prefix; + + printf("Downloading to address = 0x%08x, size = %i\n", + dwElementAddress, dwElementSize); + + data = file->firmware + file->size.prefix; + + ret = dfuse_dnload_element(dif, dwElementAddress, dwElementSize, data, + xfer_size); + if (ret != 0) + goto out_free; + + printf("File downloaded successfully\n"); + ret = dwElementSize; + + out_free: + return ret; +} + +/* Parse a DfuSe file and download contents to device */ +int dfuse_do_dfuse_dnload(struct dfu_if *dif, int xfer_size, + struct dfu_file *file) +{ + uint8_t dfuprefix[11]; + uint8_t targetprefix[274]; + uint8_t elementheader[8]; + int image; + int element; + int bTargets; + int bAlternateSetting; + int dwNbElements; + unsigned int dwElementAddress; + unsigned int dwElementSize; + uint8_t *data; + int ret; + int rem; + int bFirstAddressSaved = 0; + + rem = file->size.total - file->size.prefix - file->size.suffix; + data = file->firmware + file->size.prefix; + + /* Must be larger than a minimal DfuSe header and suffix */ + if (rem < (int)(sizeof(dfuprefix) + + sizeof(targetprefix) + sizeof(elementheader))) { + errx(EX_SOFTWARE, "File too small for a DfuSe file"); + } + + dfuse_memcpy(dfuprefix, &data, &rem, sizeof(dfuprefix)); + + if (strncmp((char *)dfuprefix, "DfuSe", 5)) { + errx(EX_IOERR, "No valid DfuSe signature"); + return -EINVAL; + } + if (dfuprefix[5] != 0x01) { + errx(EX_IOERR, "DFU format revision %i not supported", + dfuprefix[5]); + return -EINVAL; + } + bTargets = dfuprefix[10]; + printf("file contains %i DFU images\n", bTargets); + + for (image = 1; image <= bTargets; image++) { + printf("parsing DFU image %i\n", image); + dfuse_memcpy(targetprefix, &data, &rem, sizeof(targetprefix)); + if (strncmp((char *)targetprefix, "Target", 6)) { + errx(EX_IOERR, "No valid target signature"); + return -EINVAL; + } + bAlternateSetting = targetprefix[6]; + dwNbElements = quad2uint((unsigned char *)targetprefix + 270); + printf("image for alternate setting %i, ", bAlternateSetting); + printf("(%i elements, ", dwNbElements); + printf("total size = %i)\n", + quad2uint((unsigned char *)targetprefix + 266)); + if (bAlternateSetting != dif->altsetting) + printf("Warning: Image does not match current alternate" + " setting.\n" + "Please rerun with the correct -a option setting" + " to download this image!\n"); + for (element = 1; element <= dwNbElements; element++) { + printf("parsing element %i, ", element); + dfuse_memcpy(elementheader, &data, &rem, sizeof(elementheader)); + dwElementAddress = + quad2uint((unsigned char *)elementheader); + dwElementSize = + quad2uint((unsigned char *)elementheader + 4); + printf("address = 0x%08x, ", dwElementAddress); + printf("size = %i\n", dwElementSize); + + if (!bFirstAddressSaved) { + bFirstAddressSaved = 1; + dfuse_address = dwElementAddress; + } + /* sanity check */ + if ((int)dwElementSize > rem) + errx(EX_SOFTWARE, "File too small for element size"); + + if (bAlternateSetting == dif->altsetting) { + ret = dfuse_dnload_element(dif, dwElementAddress, + dwElementSize, data, xfer_size); + } else { + ret = 0; + } + + /* advance read pointer */ + dfuse_memcpy(NULL, &data, &rem, dwElementSize); + + if (ret != 0) + return ret; + } + } + + if (rem != 0) + warnx("%d bytes leftover", rem); + + printf("done parsing DfuSe file\n"); + + return 0; +} + +int dfuse_do_dnload(struct dfu_if *dif, int xfer_size, struct dfu_file *file, + const char *dfuse_options) +{ + int ret; + + if (dfuse_options) + dfuse_parse_options(dfuse_options); + mem_layout = parse_memory_layout((char *)dif->alt_name); + if (!mem_layout) { + errx(EX_IOERR, "Failed to parse memory layout"); + } + if (dfuse_unprotect) { + if (!dfuse_force) { + errx(EX_IOERR, "The read unprotect command " + "will erase the flash memory" + "and can only be used with force\n"); + } + dfuse_special_command(dif, 0, READ_UNPROTECT); + printf("Device disconnects, erases flash and resets now\n"); + exit(0); + } + if (dfuse_mass_erase) { + if (!dfuse_force) { + errx(EX_IOERR, "The mass erase command " + "can only be used with force"); + } + printf("Performing mass erase, this can take a moment\n"); + dfuse_special_command(dif, 0, MASS_ERASE); + } + if (dfuse_address) { + if (file->bcdDFU == 0x11a) { + errx(EX_IOERR, "This is a DfuSe file, not " + "meant for raw download"); + } + ret = dfuse_do_bin_dnload(dif, xfer_size, file, dfuse_address); + } else { + if (file->bcdDFU != 0x11a) { + warnx("Only DfuSe file version 1.1a is supported"); + errx(EX_IOERR, "(for raw binary download, use the " + "--dfuse-address option)"); + } + ret = dfuse_do_dfuse_dnload(dif, xfer_size, file); + } + free_segment_list(mem_layout); + + dfu_abort_to_idle(dif); + + if (dfuse_leave) { + dfuse_special_command(dif, dfuse_address, SET_ADDRESS); + dfuse_dnload_chunk(dif, NULL, 0, 2); /* Zero-size */ + } + return ret; +} diff --git a/tools/linux64/src/dfu-util/src/dfuse.h b/tools/linux64/src/dfu-util/src/dfuse.h new file mode 100644 index 0000000..ed1108c --- /dev/null +++ b/tools/linux64/src/dfu-util/src/dfuse.h @@ -0,0 +1,35 @@ +/* This implements the ST Microsystems DFU extensions (DfuSe) + * as per the DfuSe 1.1a specification (Document UM0391) + * + * (C) 2010-2012 Tormod Volden + * + * 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 DFUSE_H +#define DFUSE_H + +#include "dfu.h" + +enum dfuse_command { SET_ADDRESS, ERASE_PAGE, MASS_ERASE, READ_UNPROTECT }; + +int dfuse_special_command(struct dfu_if *dif, unsigned int address, + enum dfuse_command command); +int dfuse_do_upload(struct dfu_if *dif, int xfer_size, int fd, + const char *dfuse_options); +int dfuse_do_dnload(struct dfu_if *dif, int xfer_size, struct dfu_file *file, + const char *dfuse_options); + +#endif /* DFUSE_H */ diff --git a/tools/linux64/src/dfu-util/src/dfuse_mem.c b/tools/linux64/src/dfu-util/src/dfuse_mem.c new file mode 100644 index 0000000..a91aacf --- /dev/null +++ b/tools/linux64/src/dfu-util/src/dfuse_mem.c @@ -0,0 +1,198 @@ +/* + * Helper functions for reading the memory map of a device + * following the ST DfuSe 1.1a specification. + * + * Copyright 2011-2014 Tormod Volden + * + * 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 +#include +#include +#include + +#include "portable.h" +#include "dfu_file.h" +#include "dfuse_mem.h" + +int add_segment(struct memsegment **segment_list, struct memsegment segment) +{ + struct memsegment *new_element; + + new_element = dfu_malloc(sizeof(struct memsegment)); + *new_element = segment; + new_element->next = NULL; + + if (*segment_list == NULL) + /* list can be empty on first call */ + *segment_list = new_element; + else { + struct memsegment *next_element; + + /* find last element in list */ + next_element = *segment_list; + while (next_element->next != NULL) + next_element = next_element->next; + next_element->next = new_element; + } + return 0; +} + +struct memsegment *find_segment(struct memsegment *segment_list, + unsigned int address) +{ + while (segment_list != NULL) { + if (segment_list->start <= address && + segment_list->end >= address) + return segment_list; + segment_list = segment_list->next; + } + return NULL; +} + +void free_segment_list(struct memsegment *segment_list) +{ + struct memsegment *next_element; + + while (segment_list->next != NULL) { + next_element = segment_list->next; + free(segment_list); + segment_list = next_element; + } + free(segment_list); +} + +/* Parse memory map from interface descriptor string + * encoded as per ST document UM0424 section 4.3.2. + */ +struct memsegment *parse_memory_layout(char *intf_desc) +{ + + char multiplier, memtype; + unsigned int address; + int sectors, size; + char *name, *typestring; + int ret; + int count = 0; + char separator; + int scanned; + struct memsegment *segment_list = NULL; + struct memsegment segment; + + name = dfu_malloc(strlen(intf_desc)); + + ret = sscanf(intf_desc, "@%[^/]%n", name, &scanned); + if (ret < 1) { + free(name); + warnx("Could not read name, sscanf returned %d", ret); + return NULL; + } + printf("DfuSe interface name: \"%s\"\n", name); + + intf_desc += scanned; + typestring = dfu_malloc(strlen(intf_desc)); + + while (ret = sscanf(intf_desc, "/0x%x/%n", &address, &scanned), + ret > 0) { + + intf_desc += scanned; + while (ret = sscanf(intf_desc, "%d*%d%c%[^,/]%n", + §ors, &size, &multiplier, typestring, + &scanned), ret > 2) { + intf_desc += scanned; + + count++; + memtype = 0; + if (ret == 4) { + if (strlen(typestring) == 1 + && typestring[0] != '/') + memtype = typestring[0]; + else { + warnx("Parsing type identifier '%s' " + "failed for segment %i", + typestring, count); + continue; + } + } + + /* Quirk for STM32F4 devices */ + if (strcmp(name, "Device Feature") == 0) + memtype = 'e'; + + switch (multiplier) { + case 'B': + break; + case 'K': + size *= 1024; + break; + case 'M': + size *= 1024 * 1024; + break; + case 'a': + case 'b': + case 'c': + case 'd': + case 'e': + case 'f': + case 'g': + if (!memtype) { + warnx("Non-valid multiplier '%c', " + "interpreted as type " + "identifier instead", + multiplier); + memtype = multiplier; + break; + } + /* fallthrough if memtype was already set */ + default: + warnx("Non-valid multiplier '%c', " + "assuming bytes", multiplier); + } + + if (!memtype) { + warnx("No valid type for segment %d\n", count); + continue; + } + + segment.start = address; + segment.end = address + sectors * size - 1; + segment.pagesize = size; + segment.memtype = memtype & 7; + add_segment(&segment_list, segment); + + if (verbose) + printf("Memory segment at 0x%08x %3d x %4d = " + "%5d (%s%s%s)\n", + address, sectors, size, sectors * size, + memtype & DFUSE_READABLE ? "r" : "", + memtype & DFUSE_ERASABLE ? "e" : "", + memtype & DFUSE_WRITEABLE ? "w" : ""); + + address += sectors * size; + + separator = *intf_desc; + if (separator == ',') + intf_desc += 1; + else + break; + } /* while per segment */ + + } /* while per address */ + free(name); + free(typestring); + + return segment_list; +} diff --git a/tools/linux64/src/dfu-util/src/dfuse_mem.h b/tools/linux64/src/dfu-util/src/dfuse_mem.h new file mode 100644 index 0000000..0181f0c --- /dev/null +++ b/tools/linux64/src/dfu-util/src/dfuse_mem.h @@ -0,0 +1,44 @@ +/* Helper functions for reading the memory map in a device + * following the ST DfuSe 1.1a specification. + * + * (C) 2011 Tormod Volden + * + * 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 DFUSE_MEM_H +#define DFUSE_MEM_H + +#define DFUSE_READABLE 1 +#define DFUSE_ERASABLE 2 +#define DFUSE_WRITEABLE 4 + +struct memsegment { + unsigned int start; + unsigned int end; + int pagesize; + int memtype; + struct memsegment *next; +}; + +int add_segment(struct memsegment **list, struct memsegment new_element); + +struct memsegment *find_segment(struct memsegment *list, unsigned int address); + +void free_segment_list(struct memsegment *list); + +struct memsegment *parse_memory_layout(char *intf_desc_str); + +#endif /* DFUSE_MEM_H */ diff --git a/tools/linux64/src/dfu-util/src/main.c b/tools/linux64/src/dfu-util/src/main.c new file mode 100644 index 0000000..acaed2f --- /dev/null +++ b/tools/linux64/src/dfu-util/src/main.c @@ -0,0 +1,699 @@ +/* + * dfu-util + * + * Copyright 2007-2008 by OpenMoko, Inc. + * Copyright 2013-2014 Hans Petter Selasky + * + * Written by Harald Welte + * + * 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 +#include +#include +#include +#include +#include +#include + +#include "portable.h" +#include "dfu.h" +#include "usb_dfu.h" +#include "dfu_file.h" +#include "dfu_load.h" +#include "dfu_util.h" +#include "dfuse.h" +#include "quirks.h" + +#ifdef HAVE_USBPATH_H +#include +#endif + +int verbose = 0; + +struct dfu_if *dfu_root = NULL; + +int match_bus = -1; +int match_device = -1; +int match_vendor = -1; +int match_product = -1; +int match_vendor_dfu = -1; +int match_product_dfu = -1; +int match_config_index = -1; +int match_iface_index = -1; +int match_iface_alt_index = -1; +const char *match_iface_alt_name = NULL; +const char *match_serial = NULL; +const char *match_serial_dfu = NULL; + +static int parse_match_value(const char *str, int default_value) +{ + char *remainder; + int value; + + if (str == NULL) { + value = default_value; + } else if (*str == '*') { + value = -1; /* Match anything */ + } else if (*str == '-') { + value = 0x10000; /* Impossible vendor/product ID */ + } else { + value = strtoul(str, &remainder, 16); + if (remainder == str) { + value = default_value; + } + } + return value; +} + +static void parse_vendprod(const char *str) +{ + const char *comma; + const char *colon; + + /* Default to match any DFU device in runtime or DFU mode */ + match_vendor = -1; + match_product = -1; + match_vendor_dfu = -1; + match_product_dfu = -1; + + comma = strchr(str, ','); + if (comma == str) { + /* DFU mode vendor/product being specified without any runtime + * vendor/product specification, so don't match any runtime device */ + match_vendor = match_product = 0x10000; + } else { + colon = strchr(str, ':'); + if (colon != NULL) { + ++colon; + if ((comma != NULL) && (colon > comma)) { + colon = NULL; + } + } + match_vendor = parse_match_value(str, match_vendor); + match_product = parse_match_value(colon, match_product); + if (comma != NULL) { + /* Both runtime and DFU mode vendor/product specifications are + * available, so default DFU mode match components to the given + * runtime match components */ + match_vendor_dfu = match_vendor; + match_product_dfu = match_product; + } + } + if (comma != NULL) { + ++comma; + colon = strchr(comma, ':'); + if (colon != NULL) { + ++colon; + } + match_vendor_dfu = parse_match_value(comma, match_vendor_dfu); + match_product_dfu = parse_match_value(colon, match_product_dfu); + } +} + +static void parse_serial(char *str) +{ + char *comma; + + match_serial = str; + comma = strchr(str, ','); + if (comma == NULL) { + match_serial_dfu = match_serial; + } else { + *comma++ = 0; + match_serial_dfu = comma; + } + if (*match_serial == 0) match_serial = NULL; + if (*match_serial_dfu == 0) match_serial_dfu = NULL; +} + +#ifdef HAVE_USBPATH_H + +static int resolve_device_path(char *path) +{ + int res; + + res = usb_path2devnum(path); + if (res < 0) + return -EINVAL; + if (!res) + return 0; + + match_bus = atoi(path); + match_device = res; + + return 0; +} + +#else /* HAVE_USBPATH_H */ + +static int resolve_device_path(char *path) +{ + (void)path; /* Eliminate unused variable warning */ + errx(EX_SOFTWARE, "USB device paths are not supported by this dfu-util.\n"); +} + +#endif /* !HAVE_USBPATH_H */ + +static void help(void) +{ + fprintf(stderr, "Usage: dfu-util [options] ...\n" + " -h --help\t\t\tPrint this help message\n" + " -V --version\t\t\tPrint the version number\n" + " -v --verbose\t\t\tPrint verbose debug statements\n" + " -l --list\t\t\tList currently attached DFU capable devices\n"); + fprintf(stderr, " -e --detach\t\t\tDetach currently attached DFU capable devices\n" + " -E --detach-delay seconds\tTime to wait before reopening a device after detach\n" + " -d --device :[,:]\n" + "\t\t\t\tSpecify Vendor/Product ID(s) of DFU device\n" + " -p --path \tSpecify path to DFU device\n" + " -c --cfg \t\tSpecify the Configuration of DFU device\n" + " -i --intf \t\tSpecify the DFU Interface number\n" + " -S --serial [,]\n" + "\t\t\t\tSpecify Serial String of DFU device\n" + " -a --alt \t\tSpecify the Altsetting of the DFU Interface\n" + "\t\t\t\tby name or by number\n"); + fprintf(stderr, " -t --transfer-size \tSpecify the number of bytes per USB Transfer\n" + " -U --upload \t\tRead firmware from device into \n" + " -Z --upload-size \tSpecify the expected upload size in bytes\n" + " -D --download \t\tWrite firmware from into device\n" + " -R --reset\t\t\tIssue USB Reset signalling once we're finished\n" + " -s --dfuse-address
\tST DfuSe mode, specify target address for\n" + "\t\t\t\traw file download or upload. Not applicable for\n" + "\t\t\t\tDfuSe file (.dfu) downloads\n" + ); + exit(EX_USAGE); +} + +static void print_version(void) +{ + printf(PACKAGE_STRING "\n\n"); + printf("Copyright 2005-2009 Weston Schmidt, Harald Welte and OpenMoko Inc.\n" + "Copyright 2010-2014 Tormod Volden and Stefan Schmidt\n" + "This program is Free Software and has ABSOLUTELY NO WARRANTY\n" + "Please report bugs to " PACKAGE_BUGREPORT "\n\n"); +} + +static struct option opts[] = { + { "help", 0, 0, 'h' }, + { "version", 0, 0, 'V' }, + { "verbose", 0, 0, 'v' }, + { "list", 0, 0, 'l' }, + { "detach", 0, 0, 'e' }, + { "detach-delay", 1, 0, 'E' }, + { "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' }, + { "serial", 1, 0, 'S' }, + { "transfer-size", 1, 0, 't' }, + { "upload", 1, 0, 'U' }, + { "upload-size", 1, 0, 'Z' }, + { "download", 1, 0, 'D' }, + { "reset", 0, 0, 'R' }, + { "dfuse-address", 1, 0, 's' }, + { 0, 0, 0, 0 } +}; + +int main(int argc, char **argv) +{ + int expected_size = 0; + unsigned int transfer_size = 0; + enum mode mode = MODE_NONE; + struct dfu_status status; + libusb_context *ctx; + struct dfu_file file; + char *end; + int final_reset = 0; + int ret; + int dfuse_device = 0; + int fd; + const char *dfuse_options = NULL; + int detach_delay = 5; + uint16_t runtime_vendor; + uint16_t runtime_product; + + memset(&file, 0, sizeof(file)); + + /* make sure all prints are flushed */ + setvbuf(stdout, NULL, _IONBF, 0); + + while (1) { + int c, option_index = 0; + c = getopt_long(argc, argv, "hVvleE:d:p:c:i:a:S:t:U:D:Rs:Z:", opts, + &option_index); + if (c == -1) + break; + + switch (c) { + case 'h': + help(); + break; + case 'V': + mode = MODE_VERSION; + break; + case 'v': + verbose++; + break; + case 'l': + mode = MODE_LIST; + break; + case 'e': + mode = MODE_DETACH; + match_iface_alt_index = 0; + match_iface_index = 0; + break; + case 'E': + detach_delay = atoi(optarg); + break; + case 'd': + parse_vendprod(optarg); + break; + case 'p': + /* Parse device path */ + ret = resolve_device_path(optarg); + if (ret < 0) + errx(EX_SOFTWARE, "Unable to parse '%s'", optarg); + if (!ret) + errx(EX_SOFTWARE, "Cannot find '%s'", optarg); + break; + case 'c': + /* Configuration */ + match_config_index = atoi(optarg); + break; + case 'i': + /* Interface */ + match_iface_index = atoi(optarg); + break; + case 'a': + /* Interface Alternate Setting */ + match_iface_alt_index = strtoul(optarg, &end, 0); + if (*end) { + match_iface_alt_name = optarg; + match_iface_alt_index = -1; + } + break; + case 'S': + parse_serial(optarg); + break; + case 't': + transfer_size = atoi(optarg); + break; + case 'U': + mode = MODE_UPLOAD; + file.name = optarg; + break; + case 'Z': + expected_size = atoi(optarg); + break; + case 'D': + mode = MODE_DOWNLOAD; + file.name = optarg; + break; + case 'R': + final_reset = 1; + break; + case 's': + dfuse_options = optarg; + break; + default: + help(); + break; + } + } + + print_version(); + if (mode == MODE_VERSION) { + exit(0); + } + + if (mode == MODE_NONE) { + fprintf(stderr, "You need to specify one of -D or -U\n"); + help(); + } + + if (match_config_index == 0) { + /* Handle "-c 0" (unconfigured device) as don't care */ + match_config_index = -1; + } + + if (mode == MODE_DOWNLOAD) { + dfu_load_file(&file, MAYBE_SUFFIX, MAYBE_PREFIX); + /* If the user didn't specify product and/or vendor IDs to match, + * use any IDs from the file suffix for device matching */ + if (match_vendor < 0 && file.idVendor != 0xffff) { + match_vendor = file.idVendor; + printf("Match vendor ID from file: %04x\n", match_vendor); + } + if (match_product < 0 && file.idProduct != 0xffff) { + match_product = file.idProduct; + printf("Match product ID from file: %04x\n", match_product); + } + } + + ret = libusb_init(&ctx); + if (ret) + errx(EX_IOERR, "unable to initialize libusb: %i", ret); + + if (verbose > 2) { + libusb_set_debug(ctx, 255); + } + + probe_devices(ctx); + + if (mode == MODE_LIST) { + list_dfu_interfaces(); + exit(0); + } + + if (dfu_root == NULL) { + errx(EX_IOERR, "No DFU capable USB device available"); + } else if (dfu_root->next != NULL) { + /* 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 */ + errx(EX_IOERR, "More than one DFU capable USB device found! " + "Try `--list' and specify the serial number " + "or disconnect all but one device\n"); + } + + /* We have exactly one device. Its libusb_device is now in dfu_root->dev */ + + printf("Opening DFU capable USB device...\n"); + ret = libusb_open(dfu_root->dev, &dfu_root->dev_handle); + if (ret || !dfu_root->dev_handle) + errx(EX_IOERR, "Cannot open device"); + + printf("ID %04x:%04x\n", dfu_root->vendor, dfu_root->product); + + printf("Run-time device DFU version %04x\n", + libusb_le16_to_cpu(dfu_root->func_dfu.bcdDFUVersion)); + + /* Transition from run-Time mode to DFU mode */ + if (!(dfu_root->flags & DFU_IFF_DFU)) { + int err; + /* 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 */ + + runtime_vendor = dfu_root->vendor; + runtime_product = dfu_root->product; + + printf("Claiming USB DFU Runtime Interface...\n"); + if (libusb_claim_interface(dfu_root->dev_handle, dfu_root->interface) < 0) { + errx(EX_IOERR, "Cannot claim interface %d", + dfu_root->interface); + } + + if (libusb_set_interface_alt_setting(dfu_root->dev_handle, dfu_root->interface, 0) < 0) { + errx(EX_IOERR, "Cannot set alt interface zero"); + } + + printf("Determining device status: "); + + err = dfu_get_status(dfu_root, &status); + if (err == LIBUSB_ERROR_PIPE) { + printf("Device does not implement get_status, assuming appIDLE\n"); + status.bStatus = DFU_STATUS_OK; + status.bwPollTimeout = 0; + status.bState = DFU_STATE_appIDLE; + status.iString = 0; + } else if (err < 0) { + errx(EX_IOERR, "error get_status"); + } else { + printf("state = %s, status = %d\n", + dfu_state_to_string(status.bState), status.bStatus); + } + milli_sleep(status.bwPollTimeout); + + 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(dfu_root->dev_handle, + dfu_root->interface, 1000) < 0) { + warnx("error detaching"); + } + if (dfu_root->func_dfu.bmAttributes & USB_DFU_WILL_DETACH) { + printf("Device will detach and reattach...\n"); + } else { + printf("Resetting USB...\n"); + ret = libusb_reset_device(dfu_root->dev_handle); + if (ret < 0 && ret != LIBUSB_ERROR_NOT_FOUND) + errx(EX_IOERR, "error resetting " + "after detach"); + } + break; + case DFU_STATE_dfuERROR: + printf("dfuERROR, clearing status\n"); + if (dfu_clear_status(dfu_root->dev_handle, + dfu_root->interface) < 0) { + errx(EX_IOERR, "error clear_status"); + } + /* fall through */ + default: + warnx("WARNING: Runtime device already in DFU state ?!?"); + libusb_release_interface(dfu_root->dev_handle, + dfu_root->interface); + goto dfustate; + } + libusb_release_interface(dfu_root->dev_handle, + dfu_root->interface); + libusb_close(dfu_root->dev_handle); + dfu_root->dev_handle = NULL; + + if (mode == MODE_DETACH) { + libusb_exit(ctx); + exit(0); + } + + /* keeping handles open might prevent re-enumeration */ + disconnect_devices(); + + milli_sleep(detach_delay * 1000); + + /* Change match vendor and product to impossible values to force + * only DFU mode matches in the following probe */ + match_vendor = match_product = 0x10000; + + probe_devices(ctx); + + if (dfu_root == NULL) { + errx(EX_IOERR, "Lost device after RESET?"); + } else if (dfu_root->next != NULL) { + errx(EX_IOERR, "More than one DFU capable USB device found! " + "Try `--list' and specify the serial number " + "or disconnect all but one device"); + } + + /* Check for DFU mode device */ + if (!(dfu_root->flags | DFU_IFF_DFU)) + errx(EX_SOFTWARE, "Device is not in DFU mode"); + + printf("Opening DFU USB Device...\n"); + ret = libusb_open(dfu_root->dev, &dfu_root->dev_handle); + if (ret || !dfu_root->dev_handle) { + errx(EX_IOERR, "Cannot open device"); + } + } else { + /* we're already in DFU mode, so we can skip the detach/reset + * procedure */ + /* If a match vendor/product was specified, use that as the runtime + * vendor/product, otherwise use the DFU mode vendor/product */ + runtime_vendor = match_vendor < 0 ? dfu_root->vendor : match_vendor; + runtime_product = match_product < 0 ? dfu_root->product : match_product; + } + +dfustate: +#if 0 + printf("Setting Configuration %u...\n", dfu_root->configuration); + if (libusb_set_configuration(dfu_root->dev_handle, dfu_root->configuration) < 0) { + errx(EX_IOERR, "Cannot set configuration"); + } +#endif + printf("Claiming USB DFU Interface...\n"); + if (libusb_claim_interface(dfu_root->dev_handle, dfu_root->interface) < 0) { + errx(EX_IOERR, "Cannot claim interface"); + } + + printf("Setting Alternate Setting #%d ...\n", dfu_root->altsetting); + if (libusb_set_interface_alt_setting(dfu_root->dev_handle, dfu_root->interface, dfu_root->altsetting) < 0) { + errx(EX_IOERR, "Cannot set alternate interface"); + } + +status_again: + printf("Determining device status: "); + if (dfu_get_status(dfu_root, &status ) < 0) { + errx(EX_IOERR, "error get_status"); + } + printf("state = %s, status = %d\n", + dfu_state_to_string(status.bState), status.bStatus); + + milli_sleep(status.bwPollTimeout); + + switch (status.bState) { + case DFU_STATE_appIDLE: + case DFU_STATE_appDETACH: + errx(EX_IOERR, "Device still in Runtime Mode!"); + break; + case DFU_STATE_dfuERROR: + printf("dfuERROR, clearing status\n"); + if (dfu_clear_status(dfu_root->dev_handle, dfu_root->interface) < 0) { + errx(EX_IOERR, "error clear_status"); + } + goto status_again; + break; + case DFU_STATE_dfuDNLOAD_IDLE: + case DFU_STATE_dfuUPLOAD_IDLE: + printf("aborting previous incomplete transfer\n"); + if (dfu_abort(dfu_root->dev_handle, dfu_root->interface) < 0) { + errx(EX_IOERR, "can't send DFU_ABORT"); + } + goto status_again; + break; + case DFU_STATE_dfuIDLE: + printf("dfuIDLE, continuing\n"); + break; + default: + break; + } + + if (DFU_STATUS_OK != status.bStatus ) { + printf("WARNING: DFU Status: '%s'\n", + dfu_status_to_string(status.bStatus)); + /* Clear our status & try again. */ + if (dfu_clear_status(dfu_root->dev_handle, dfu_root->interface) < 0) + errx(EX_IOERR, "USB communication error"); + if (dfu_get_status(dfu_root, &status) < 0) + errx(EX_IOERR, "USB communication error"); + if (DFU_STATUS_OK != status.bStatus) + errx(EX_SOFTWARE, "Status is not OK: %d", status.bStatus); + + milli_sleep(status.bwPollTimeout); + } + + printf("DFU mode device DFU version %04x\n", + libusb_le16_to_cpu(dfu_root->func_dfu.bcdDFUVersion)); + + if (dfu_root->func_dfu.bcdDFUVersion == libusb_cpu_to_le16(0x11a)) + dfuse_device = 1; + + /* If not overridden by the user */ + if (!transfer_size) { + transfer_size = libusb_le16_to_cpu( + dfu_root->func_dfu.wTransferSize); + if (transfer_size) { + printf("Device returned transfer size %i\n", + transfer_size); + } else { + errx(EX_IOERR, "Transfer size must be specified"); + } + } + +#ifdef HAVE_GETPAGESIZE +/* autotools lie when cross-compiling for Windows using mingw32/64 */ +#ifndef __MINGW32__ + /* limitation of Linux usbdevio */ + if ((int)transfer_size > getpagesize()) { + transfer_size = getpagesize(); + printf("Limited transfer size to %i\n", transfer_size); + } +#endif /* __MINGW32__ */ +#endif /* HAVE_GETPAGESIZE */ + + if (transfer_size < dfu_root->bMaxPacketSize0) { + transfer_size = dfu_root->bMaxPacketSize0; + printf("Adjusted transfer size to %i\n", transfer_size); + } + + switch (mode) { + case MODE_UPLOAD: + /* open for "exclusive" writing */ + fd = open(file.name, O_WRONLY | O_BINARY | O_CREAT | O_EXCL | O_TRUNC, 0666); + if (fd < 0) + err(EX_IOERR, "Cannot open file %s for writing", file.name); + + if (dfuse_device || dfuse_options) { + if (dfuse_do_upload(dfu_root, transfer_size, fd, + dfuse_options) < 0) + exit(1); + } else { + if (dfuload_do_upload(dfu_root, transfer_size, + expected_size, fd) < 0) { + exit(1); + } + } + close(fd); + break; + + case MODE_DOWNLOAD: + if (((file.idVendor != 0xffff && file.idVendor != runtime_vendor) || + (file.idProduct != 0xffff && file.idProduct != runtime_product)) && + ((file.idVendor != 0xffff && file.idVendor != dfu_root->vendor) || + (file.idProduct != 0xffff && file.idProduct != dfu_root->product))) { + errx(EX_IOERR, "Error: File ID %04x:%04x does " + "not match device (%04x:%04x or %04x:%04x)", + file.idVendor, file.idProduct, + runtime_vendor, runtime_product, + dfu_root->vendor, dfu_root->product); + } + if (dfuse_device || dfuse_options || file.bcdDFU == 0x11a) { + if (dfuse_do_dnload(dfu_root, transfer_size, &file, + dfuse_options) < 0) + exit(1); + } else { + if (dfuload_do_dnload(dfu_root, transfer_size, &file) < 0) + exit(1); + } + break; + case MODE_DETACH: + if (dfu_detach(dfu_root->dev_handle, dfu_root->interface, 1000) < 0) { + warnx("can't detach"); + } + break; + default: + errx(EX_IOERR, "Unsupported mode: %u", mode); + break; + } + + if (final_reset) { + if (dfu_detach(dfu_root->dev_handle, dfu_root->interface, 1000) < 0) { + /* Even if detach failed, just carry on to leave the + device in a known state */ + warnx("can't detach"); + } + printf("Resetting USB to switch back to runtime mode\n"); + ret = libusb_reset_device(dfu_root->dev_handle); + if (ret < 0 && ret != LIBUSB_ERROR_NOT_FOUND) { + errx(EX_IOERR, "error resetting after download"); + } + } + + libusb_close(dfu_root->dev_handle); + dfu_root->dev_handle = NULL; + libusb_exit(ctx); + + return (0); +} diff --git a/tools/linux64/src/dfu-util/src/portable.h b/tools/linux64/src/dfu-util/src/portable.h new file mode 100644 index 0000000..cf8d5df --- /dev/null +++ b/tools/linux64/src/dfu-util/src/portable.h @@ -0,0 +1,72 @@ + +#ifndef PORTABLE_H +#define PORTABLE_H + +#ifdef HAVE_CONFIG_H +# include "config.h" +#else +# define PACKAGE "dfu-util" +# define PACKAGE_VERSION "0.8-msvc" +# define PACKAGE_STRING "dfu-util 0.8-msvc" +# define PACKAGE_BUGREPORT "dfu-util@lists.gnumonks.org" +#endif /* HAVE_CONFIG_H */ + +#ifdef HAVE_FTRUNCATE +# include +#else +# include +#endif /* HAVE_FTRUNCATE */ + +#ifdef HAVE_NANOSLEEP +# include +# define milli_sleep(msec) do {\ + if (msec) {\ + struct timespec nanosleepDelay = { (msec) / 1000, ((msec) % 1000) * 1000000 };\ + nanosleep(&nanosleepDelay, NULL);\ + } } while (0) +#elif defined HAVE_WINDOWS_H +# define milli_sleep(msec) do {\ + if (msec) {\ + Sleep(msec);\ + } } while (0) +#else +# error "Can't get no sleep! Please report" +#endif /* HAVE_NANOSLEEP */ + +#ifdef HAVE_ERR +# include +#else +# include +# include +# define warnx(...) do {\ + fprintf(stderr, __VA_ARGS__);\ + fprintf(stderr, "\n"); } while (0) +# define errx(eval, ...) do {\ + warnx(__VA_ARGS__);\ + exit(eval); } while (0) +# define warn(...) do {\ + fprintf(stderr, "%s: ", strerror(errno));\ + warnx(__VA_ARGS__); } while (0) +# define err(eval, ...) do {\ + warn(__VA_ARGS__);\ + exit(eval); } while (0) +#endif /* HAVE_ERR */ + +#ifdef HAVE_SYSEXITS_H +# include +#else +# define EX_OK 0 /* successful termination */ +# define EX_USAGE 64 /* command line usage error */ +# define EX_SOFTWARE 70 /* internal software error */ +# define EX_IOERR 74 /* input/output error */ +#endif /* HAVE_SYSEXITS_H */ + +#ifndef O_BINARY +# define O_BINARY 0 +#endif + +#ifndef off_t +# define off_t long int +#endif + +#endif /* PORTABLE_H */ diff --git a/tools/linux64/src/dfu-util/src/prefix.c b/tools/linux64/src/dfu-util/src/prefix.c new file mode 100644 index 0000000..be8e3fa --- /dev/null +++ b/tools/linux64/src/dfu-util/src/prefix.c @@ -0,0 +1,176 @@ +/* + * dfu-prefix + * + * Copyright 2011-2012 Stefan Schmidt + * Copyright 2013 Hans Petter Selasky + * Copyright 2014 Uwe Bonnes + * + * 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 +#include +#include +#include +#include + +#include "portable.h" +#include "dfu_file.h" + +enum mode { + MODE_NONE, + MODE_ADD, + MODE_DEL, + MODE_CHECK +}; + +int verbose; + +static void help(void) +{ + fprintf(stderr, "Usage: dfu-prefix [options] ...\n" + " -h --help\t\t\tPrint this help message\n" + " -V --version\t\t\tPrint the version number\n" + " -c --check \t\tCheck DFU prefix of \n" + " -D --delete \t\tDelete DFU prefix from \n" + " -a --add \t\tAdd DFU prefix to \n" + "In combination with -a:\n" + ); + fprintf(stderr, " -s --stellaris-address
Add TI Stellaris address prefix to \n" + "In combination with -D or -c:\n" + " -T --stellaris\t\tAct on TI Stellaris address prefix of \n" + "In combination with -a or -D or -c:\n" + " -L --lpc-prefix\t\tUse NXP LPC DFU prefix format\n" + ); + exit(EX_USAGE); +} + +static void print_version(void) +{ + printf("dfu-prefix (%s) %s\n\n", PACKAGE, PACKAGE_VERSION); + printf("Copyright 2011-2012 Stefan Schmidt, 2014 Uwe Bonnes\n" + "This program is Free Software and has ABSOLUTELY NO WARRANTY\n" + "Please report bugs to %s\n\n", PACKAGE_BUGREPORT); + +} + +static struct option opts[] = { + { "help", 0, 0, 'h' }, + { "version", 0, 0, 'V' }, + { "check", 1, 0, 'c' }, + { "add", 1, 0, 'a' }, + { "delete", 1, 0, 'D' }, + { "stellaris-address", 1, 0, 's' }, + { "stellaris", 0, 0, 'T' }, + { "LPC", 0, 0, 'L' }, +}; +int main(int argc, char **argv) +{ + struct dfu_file file; + enum mode mode = MODE_NONE; + enum prefix_type type = ZERO_PREFIX; + uint32_t lmdfu_flash_address = 0; + char *end; + + /* make sure all prints are flushed */ + setvbuf(stdout, NULL, _IONBF, 0); + + print_version(); + + memset(&file, 0, sizeof(file)); + + while (1) { + int c, option_index = 0; + c = getopt_long(argc, argv, "hVc:a:D:p:v:d:s:TL", opts, + &option_index); + if (c == -1) + break; + + switch (c) { + case 'h': + help(); + break; + case 'V': + exit(0); + break; + case 'D': + file.name = optarg; + mode = MODE_DEL; + break; + case 'c': + file.name = optarg; + mode = MODE_CHECK; + break; + case 'a': + file.name = optarg; + mode = MODE_ADD; + break; + case 's': + lmdfu_flash_address = strtoul(optarg, &end, 0); + if (*end) { + errx(EX_IOERR, "Invalid lmdfu " + "address: %s", optarg); + } + /* fall-through */ + case 'T': + type = LMDFU_PREFIX; + break; + case 'L': + type = LPCDFU_UNENCRYPTED_PREFIX; + break; + default: + help(); + break; + } + } + + if (!file.name) { + fprintf(stderr, "You need to specify a filename\n"); + help(); + } + + switch(mode) { + case MODE_ADD: + if (type == ZERO_PREFIX) + errx(EX_IOERR, "Prefix type must be specified"); + dfu_load_file(&file, MAYBE_SUFFIX, NO_PREFIX); + file.lmdfu_address = lmdfu_flash_address; + file.prefix_type = type; + printf("Adding prefix to file\n"); + dfu_store_file(&file, file.size.suffix != 0, 1); + break; + + case MODE_CHECK: + dfu_load_file(&file, MAYBE_SUFFIX, MAYBE_PREFIX); + show_suffix_and_prefix(&file); + if (type > ZERO_PREFIX && file.prefix_type != type) + errx(EX_IOERR, "No prefix of requested type"); + break; + + case MODE_DEL: + dfu_load_file(&file, MAYBE_SUFFIX, NEEDS_PREFIX); + if (type > ZERO_PREFIX && file.prefix_type != type) + errx(EX_IOERR, "No prefix of requested type"); + printf("Removing prefix from file\n"); + /* if there was a suffix, rewrite it */ + dfu_store_file(&file, file.size.suffix != 0, 0); + break; + + default: + help(); + break; + } + return (0); +} diff --git a/tools/linux64/src/dfu-util/src/quirks.c b/tools/linux64/src/dfu-util/src/quirks.c new file mode 100644 index 0000000..de394a6 --- /dev/null +++ b/tools/linux64/src/dfu-util/src/quirks.c @@ -0,0 +1,56 @@ +/* + * Simple quirk system for dfu-util + * + * Copyright 2010-2014 Tormod Volden + * + * 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 +#include "quirks.h" + +uint16_t get_quirks(uint16_t vendor, uint16_t product, uint16_t bcdDevice) +{ + uint16_t quirks = 0; + + /* Device returns bogus bwPollTimeout values */ + if ((vendor == VENDOR_OPENMOKO || vendor == VENDOR_FIC) && + product >= PRODUCT_FREERUNNER_FIRST && + product <= PRODUCT_FREERUNNER_LAST) + quirks |= QUIRK_POLLTIMEOUT; + + if (vendor == VENDOR_VOTI && + product == PRODUCT_OPENPCD) + quirks |= QUIRK_POLLTIMEOUT; + + /* Reports wrong DFU version in DFU descriptor */ + if (vendor == VENDOR_LEAFLABS && + product == PRODUCT_MAPLE3 && + bcdDevice == 0x0200) + quirks |= QUIRK_FORCE_DFU11; + + /* old devices(bcdDevice == 0) return bogus bwPollTimeout values */ + if (vendor == VENDOR_SIEMENS && + (product == PRODUCT_PXM40 || product == PRODUCT_PXM50) && + bcdDevice == 0) + quirks |= QUIRK_POLLTIMEOUT; + + /* M-Audio Transit returns bogus bwPollTimeout values */ + if (vendor == VENDOR_MIDIMAN && + product == PRODUCT_TRANSIT) + quirks |= QUIRK_POLLTIMEOUT; + + return (quirks); +} diff --git a/tools/linux64/src/dfu-util/src/quirks.h b/tools/linux64/src/dfu-util/src/quirks.h new file mode 100644 index 0000000..0e4b3ec --- /dev/null +++ b/tools/linux64/src/dfu-util/src/quirks.h @@ -0,0 +1,27 @@ +#ifndef DFU_QUIRKS_H +#define DFU_QUIRKS_H + +#define VENDOR_OPENMOKO 0x1d50 /* Openmoko Freerunner / GTA02 */ +#define VENDOR_FIC 0x1457 /* Openmoko Freerunner / GTA02 */ +#define VENDOR_VOTI 0x16c0 /* OpenPCD Reader */ +#define VENDOR_LEAFLABS 0x1eaf /* Maple */ +#define VENDOR_SIEMENS 0x0908 /* Siemens AG */ +#define VENDOR_MIDIMAN 0x0763 /* Midiman */ + +#define PRODUCT_FREERUNNER_FIRST 0x5117 +#define PRODUCT_FREERUNNER_LAST 0x5126 +#define PRODUCT_OPENPCD 0x076b +#define PRODUCT_MAPLE3 0x0003 /* rev 3 and 5 */ +#define PRODUCT_PXM40 0x02c4 /* Siemens AG, PXM 40 */ +#define PRODUCT_PXM50 0x02c5 /* Siemens AG, PXM 50 */ +#define PRODUCT_TRANSIT 0x2806 /* M-Audio Transit (Midiman) */ + +#define QUIRK_POLLTIMEOUT (1<<0) +#define QUIRK_FORCE_DFU11 (1<<1) + +/* Fallback value, works for OpenMoko */ +#define DEFAULT_POLLTIMEOUT 5 + +uint16_t get_quirks(uint16_t vendor, uint16_t product, uint16_t bcdDevice); + +#endif /* DFU_QUIRKS_H */ diff --git a/tools/linux64/src/dfu-util/src/suffix.c b/tools/linux64/src/dfu-util/src/suffix.c new file mode 100644 index 0000000..0df248f --- /dev/null +++ b/tools/linux64/src/dfu-util/src/suffix.c @@ -0,0 +1,176 @@ +/* + * dfu-suffix + * + * Copyright 2011-2012 Stefan Schmidt + * Copyright 2013 Hans Petter Selasky + * Copyright 2014 Tormod Volden + * + * 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 +#include +#include +#include +#include + +#include "portable.h" +#include "dfu_file.h" + +enum mode { + MODE_NONE, + MODE_ADD, + MODE_DEL, + MODE_CHECK +}; + +int verbose; + +static void help(void) +{ + fprintf(stderr, "Usage: dfu-suffix [options] ...\n" + " -h --help\t\t\tPrint this help message\n" + " -V --version\t\t\tPrint the version number\n" + " -c --check \t\tCheck DFU suffix of \n" + " -a --add \t\tAdd DFU suffix to \n" + " -D --delete \t\tDelete DFU suffix from \n" + " -p --pid \t\tAdd product ID into DFU suffix in \n" + " -v --vid \t\tAdd vendor ID into DFU suffix in \n" + " -d --did \t\tAdd device ID into DFU suffix in \n" + " -S --spec \t\tAdd DFU specification ID into DFU suffix in \n" + ); + exit(EX_USAGE); +} + +static void print_version(void) +{ + printf("dfu-suffix (%s) %s\n\n", PACKAGE, PACKAGE_VERSION); + printf("Copyright 2011-2012 Stefan Schmidt, 2013-2014 Tormod Volden\n" + "This program is Free Software and has ABSOLUTELY NO WARRANTY\n" + "Please report bugs to %s\n\n", PACKAGE_BUGREPORT); + +} + +static struct option opts[] = { + { "help", 0, 0, 'h' }, + { "version", 0, 0, 'V' }, + { "check", 1, 0, 'c' }, + { "add", 1, 0, 'a' }, + { "delete", 1, 0, 'D' }, + { "pid", 1, 0, 'p' }, + { "vid", 1, 0, 'v' }, + { "did", 1, 0, 'd' }, + { "spec", 1, 0, 'S' }, +}; + +int main(int argc, char **argv) +{ + struct dfu_file file; + int pid, vid, did, spec; + enum mode mode = MODE_NONE; + + /* make sure all prints are flushed */ + setvbuf(stdout, NULL, _IONBF, 0); + + print_version(); + + pid = vid = did = 0xffff; + spec = 0x0100; /* Default to bcdDFU version 1.0 */ + memset(&file, 0, sizeof(file)); + + while (1) { + int c, option_index = 0; + c = getopt_long(argc, argv, "hVc:a:D:p:v:d:S:s:T", opts, + &option_index); + if (c == -1) + break; + + switch (c) { + case 'h': + help(); + break; + case 'V': + exit(0); + break; + case 'D': + file.name = optarg; + mode = MODE_DEL; + break; + case 'p': + pid = strtol(optarg, NULL, 16); + break; + case 'v': + vid = strtol(optarg, NULL, 16); + break; + case 'd': + did = strtol(optarg, NULL, 16); + break; + case 'S': + spec = strtol(optarg, NULL, 16); + break; + case 'c': + file.name = optarg; + mode = MODE_CHECK; + break; + case 'a': + file.name = optarg; + mode = MODE_ADD; + break; + default: + help(); + break; + } + } + + if (!file.name) { + fprintf(stderr, "You need to specify a filename\n"); + help(); + } + + if (spec != 0x0100 && spec != 0x011a) { + fprintf(stderr, "Only DFU specification 0x0100 and 0x011a supported\n"); + help(); + } + + switch(mode) { + case MODE_ADD: + dfu_load_file(&file, NO_SUFFIX, MAYBE_PREFIX); + file.idVendor = vid; + file.idProduct = pid; + file.bcdDevice = did; + file.bcdDFU = spec; + /* always write suffix, rewrite prefix if there was one */ + dfu_store_file(&file, 1, file.size.prefix != 0); + printf("Suffix successfully added to file\n"); + break; + + case MODE_CHECK: + dfu_load_file(&file, NEEDS_SUFFIX, MAYBE_PREFIX); + show_suffix_and_prefix(&file); + break; + + case MODE_DEL: + dfu_load_file(&file, NEEDS_SUFFIX, MAYBE_PREFIX); + dfu_store_file(&file, 0, file.size.prefix != 0); + if (file.size.suffix) /* had a suffix */ + printf("Suffix successfully removed from file\n"); + break; + + default: + help(); + break; + } + return (0); +} diff --git a/tools/linux64/src/dfu-util/src/usb_dfu.h b/tools/linux64/src/dfu-util/src/usb_dfu.h new file mode 100644 index 0000000..660bedc --- /dev/null +++ b/tools/linux64/src/dfu-util/src/usb_dfu.h @@ -0,0 +1,99 @@ +#ifndef USB_DFU_H +#define USB_DFU_H +/* USB Device Firmware Update Implementation for OpenPCD + * (C) 2006 by Harald Welte + * + * 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 + * + * 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 + +#define USB_DT_DFU 0x21 + +#ifdef _MSC_VER +# pragma pack(push) +# pragma pack(1) +#endif /* _MSC_VER */ +struct usb_dfu_func_descriptor { + uint8_t bLength; + uint8_t bDescriptorType; + uint8_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) + uint16_t wDetachTimeOut; + uint16_t wTransferSize; + uint16_t bcdDFUVersion; +#ifdef _MSC_VER +}; +# pragma pack(pop) +#elif defined __GNUC__ +} __attribute__ ((packed)); +#else + #warning "No way to pack struct on this compiler? This will break!" +#endif /* _MSC_VER */ + +#define USB_DT_DFU_SIZE 9 + +#define USB_TYPE_DFU (LIBUSB_REQUEST_TYPE_CLASS|LIBUSB_RECIPIENT_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 + +/* DFU_GETSTATUS bStatus values (Section 6.1.2, DFU Rev 1.1) */ +#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 */ diff --git a/tools/linux64/src/dfu-util/www/build.html b/tools/linux64/src/dfu-util/www/build.html new file mode 100644 index 0000000..f3036e4 --- /dev/null +++ b/tools/linux64/src/dfu-util/www/build.html @@ -0,0 +1,147 @@ + + + + + + + Building dfu-util from source + + + + + + + + + +
+

How to build dfu-util from source

+ +

Prerequisites for building from git

+

Mac OS X

+

+First install MacPorts (and if you are on 10.6 or older, the Java Developer Package) and then run: +

+
+	sudo port install libusb-devel git-core
+
+ +

FreeBSD

+
+	sudo pkg_add -r git pkgconf
+
+ +

Ubuntu and Debian and derivatives

+
+	sudo apt-get build-dep dfu-util
+	sudo apt-get install libusb-1.0-0-dev
+
+ +

Get the source code and build it

+

+The first time you will have to clone the git repository: +

+
+	git clone git://gitorious.org/dfu-util/dfu-util.git
+	cd dfu-util
+
+

+If you later want to update to latest git version, just run this: +

+
+	make maintainer-clean
+	git pull
+
+

+To build the source: +

+
+	./autogen.sh
+	./configure  # on most systems
+	make
+
+ +

+If you are building on Mac OS X, replace the ./configure command with: +

+
+	./configure --libdir=/opt/local/lib --includedir=/opt/local/include  # on MacOSX only
+
+ +

+Your dfu-util binary will be inside the src folder. Use it from there, or install it to /usr/local/bin by running "sudo make install". +

+ +

Cross-building for Windows

+ +

+Windows binaries can be built in a MinGW +environment, on a Windows computer or cross-hosted in another OS. +To build it on a Debian or Ubuntu host, first install build dependencies: +

+
+	sudo apt-get build-dep libusb-1.0-0 dfu-util
+	sudo apt-get install mingw32
+
+ +

+The below example builds dfu-util 0.8 and libusb 1.0.19 from unpacked release +tarballs. If you instead build from git, you will have to run "./autogen.sh" +before running the "./configure" steps. +

+ +
+mkdir -p build
+cd libusb-1.0.19
+PKG_CONFIG_PATH=$PWD/../build/lib/pkgconfig ./configure \
+    --host=i586-mingw32msvc --prefix=$PWD/../build
+# WINVER workaround needed for 1.0.19 only
+make CFLAGS="-DWINVER=0x0501"
+make install
+cd ..
+
+cd dfu-util-0.8
+PKG_CONFIG_PATH=$PWD/../build/lib/pkgconfig ./configure \
+    --host=i586-mingw32msvc --prefix=$PWD/../build
+make
+make install
+cd ..
+
+The build files will now be in build/bin. +

+ +

Building on Windows using MinGW

+This assumes using release tarballs or having run ./autogen.sh on +the git sources. +
+cd libusb-1.0.19
+./configure --prefix=$HOME
+# WINVER workaround needed for 1.0.19 only
+# MKDIR_P setting should not really be needed...
+make CFLAGS="-DWINVER=0x0501" MKDIR_P="mkdir -p"
+make install
+cd ..
+
+cd dfu-util-0.8
+./configure USB_CFLAGS="-I$HOME/include/libusb-1.0" \
+            USB_LIBS="-L $HOME/lib -lusb-1.0" PKG_CONFIG=true
+make
+make install
+cd ..
+
+To link libusb statically into dfu-util.exe use instead of "make": +
+make LDFLAGS=-static
+
+The built executables (and DLL) will now be under $HOME/bin. + +

+[Back to dfu-util main page] +

+ +
+ + diff --git a/tools/linux64/src/dfu-util/www/dfu-util.1.html b/tools/linux64/src/dfu-util/www/dfu-util.1.html new file mode 100644 index 0000000..62ca40b --- /dev/null +++ b/tools/linux64/src/dfu-util/www/dfu-util.1.html @@ -0,0 +1,411 @@ + + +Man page of DFU-UTIL + + +

DFU-UTIL(1)

+ +  +

NAME

+ +dfu-util - Device firmware update (DFU) USB programmer +  +

SYNOPSIS

+ + +
+
+dfu-util + +-l + +[-v] + +[-d + +vid:pid[,vid:pid]] + +[-p + +path] + +[-c + +configuration] + +[-i + +interface] + +[-a + +alt-intf] + +[-S + +serial[,serial]] + + +
+dfu-util + +[-v] + +[-d + +vid:pid[,vid:pid]] + +[-p + +path] + +[-c + +configuration] + +[-i + +interface] + +[-a + +alt-intf] + +[-S + +serial[,serial]] + +[-t + +size] + +[-Z + +size] + +[-s + +address] + +[-R] + +[-D|-U + +file] + + +
+dfu-util + +[-hV] + +
+  +

DESCRIPTION

+ +dfu-util + +is a program that implements the host (computer) side of the USB DFU +(Universal Serial Bus Device Firmware Upgrade) protocol. +

+dfu-util communicates with devices that implement the device side of the +USB DFU protocol, and is often used to upgrade the firmware of such +devices. +  +

OPTIONS

+ +
+
-l, --list + +
+List the currently attached DFU capable USB devices. +
-d, --device [Run-Time VENDOR]:[Run-Time PRODUCT][,[DFU Mode VENDOR]:[DFU Mode PRODUCT]] + +
+
+Specify run-time and/or DFU mode vendor and/or product IDs of the DFU device +to work with. VENDOR and PRODUCT are hexadecimal numbers (no prefix +needed), "*" (match any), or "-" (match nothing). By default, any DFU capable +device in either run-time or DFU mode will be considered. +

+If you only have one standards-compliant DFU device attached to your computer, +this parameter is optional. However, as soon as you have multiple DFU devices +connected, dfu-util will detect this and abort, asking you to specify which +device to use. +

+If only run-time IDs are specified (e.g. "--device 1457:51ab"), then in +addition to the specified run-time IDs, any DFU mode devices will also be +considered. This is beneficial to allow a DFU capable device to be found +again after a switch to DFU mode, since the vendor and/or product ID of a +device usually changes in DFU mode. +

+If only DFU mode IDs are specified (e.g. "--device ,951:26"), then all +run-time devices will be ignored, making it easy to target a specific device in +DFU mode. +

+If both run-time and DFU mode IDs are specified (e.g. "--device +1457:51ab,:2bc"), then unspecified DFU mode components will use the run-time +value specified. +

+Examples: +

+
--device 1457:51ab,951:26 + +
+
+ +Work with a device in run-time mode with +vendor ID 0x1457 and product ID 0x51ab, or in DFU mode with vendor ID 0x0951 +and product ID 0x0026 +

+

--device 1457:51ab,:2bc + +
+
+ +Work with a device in run-time mode with vendor ID 0x1457 and product ID +0x51ab, or in DFU mode with vendor ID 0x1457 and product ID 0x02bc +

+

--device 1457:51ab + +
+
+ +Work with a device in run-time mode with vendor ID 0x1457 and product ID +0x51ab, or in DFU mode with any vendor and product ID +

+

--device ,951:26 + +
+
+ +Work with a device in DFU mode with vendor ID 0x0951 and product ID 0x0026 +

+

--device *,- + +
+
+ +Work with any device in run-time mode, and ignore any device in DFU mode +

+

--device , + +
+
+ +Ignore any device in run-time mode, and Work with any device in DFU mode +
+
+ +
-p, --path BUS-PORT. ... .PORT + +
+Specify the path to the DFU device. +
-c, --cfg CONFIG-NR + +
+Specify the configuration of the DFU device. Note that this is only used for matching, the configuration is not set by dfu-util. +
-i, --intf INTF-NR + +
+Specify the DFU interface number. +
-a, --alt ALT + +
+Specify the altsetting of the DFU interface by name or by number. +
-S, --serial [Run-Time SERIAL][,[DFU Mode SERIAL]] + +
+Specify the run-time and DFU mode serial numbers used to further restrict +device matches. If multiple, identical DFU devices are simultaneously +connected to a system then vendor and product ID will be insufficient for +targeting a single device. In this situation, it may be possible to use this +parameter to specify a serial number which also must match. +

+If only a single serial number is specified, then the same serial number is +used in both run-time and DFU mode. An empty serial number will match any +serial number in the corresponding mode. +

-t, --transfer-size SIZE + +
+Specify the number of bytes per USB transfer. The optimal value is +usually determined automatically so this option is rarely useful. If +you need to use this option for a device, please report it as a bug. +
-Z, --upload-size SIZE + +
+Specify the expected upload size, in bytes. +
-U, --upload FILE + +
+Read firmware from device into +FILE. + +
-D, --download FILE + +
+Write firmware from +FILE + +into device. +
-R, --reset + +
+Issue USB reset signalling after upload or download has finished. +
-s, --dfuse-address address + +
+Specify target address for raw binary download/upload on DfuSe devices. Do +not + +use this for downloading DfuSe (.dfu) files. Modifiers can be added +to the address, separated by a colon, to perform special DfuSE commands such +as "leave" DFU mode, "unprotect" and "mass-erase" flash memory. +
-v, --verbose + +
+Print more information about dfu-util's operation. A second +-v + +will turn on verbose logging of USB requests. Repeat this option to further +increase verbosity. +
-h, --help + +
+Show a help text and exit. +
-V, --version + +
+Show version information and exit. +
+  +

EXAMPLES

+ +  +

Using dfu-util in the OpenMoko project

+ +(with the Neo1973 hardware) +

+ +Flashing the rootfs: +
+ + $ dfu-util -a rootfs -R -D /path/to/openmoko-devel-image.jffs2 + +

+ +Flashing the kernel: +
+ + $ dfu-util -a kernel -R -D /path/to/uImage + +

+ +Flashing the bootloader: +
+ + $ dfu-util -a u-boot -R -D /path/to/u-boot.bin + +

+ +Copying a kernel into RAM: +
+ + $ dfu-util -a 0 -R -D /path/to/uImage + +

+Once this has finished, the kernel will be available at the default load +address of 0x32000000 in Neo1973 RAM. +Note: + +You cannot transfer more than 2MB of data into RAM using this method. +

+  +

Using dfu-util with a DfuSe device

+ +

+ +Flashing a +.dfu + +(special DfuSe format) file to the device: +
+ + $ dfu-util -a 0 -D /path/to/dfuse-image.dfu + +

+ +Reading out 1 KB of flash starting at address 0x8000000: +
+ + $ dfu-util -a 0 -s 0x08000000:1024 -U newfile.bin + +

+ +Flashing a binary file to address 0x8004000 of device memory and +ask the device to leave DFU mode: +
+ + $ dfu-util -a 0 -s 0x08004000:leave -D /path/to/image.bin + + +  +

BUGS

+ +Please report any bugs to the dfu-util mailing list at +dfu-util@lists.gnumonks.org. + +Please use the +--verbose option (repeated as necessary) to provide more + +information in your bug report. +  +

SEE ALSO

+ +The dfu-util home page is +http://dfu-util.gnumonks.org + +  +

HISTORY

+ +dfu-util was originally written for the OpenMoko project by +Weston Schmidt <weston_schmidt@yahoo.com> and +Harald Welte <hwelte@hmw-consulting.de>. Over time, nearly complete +support of DFU 1.0, DFU 1.1 and DfuSe ("1.1a") has been added. +  +

LICENCE

+ +dfu-util + +is covered by the GNU General Public License (GPL), version 2 or later. +  +

COPYRIGHT

+ +This manual page was originally written by Uwe Hermann <uwe@hermann-uwe.de>, +and is now part of the dfu-util project. +

+ +


+ 

Index

+
+
NAME
+
SYNOPSIS
+
DESCRIPTION
+
OPTIONS
+
EXAMPLES
+
+
Using dfu-util in the OpenMoko project
+
Using dfu-util with a DfuSe device
+
+
BUGS
+
SEE ALSO
+
HISTORY
+
LICENCE
+
COPYRIGHT
+
+
+This document was created by man2html, +using the doc/dfu-util.1 manual page from dfu-util 0.8.
+Time: 14:40:57 GMT, September 13, 2014 + + diff --git a/tools/linux64/src/dfu-util/www/dfuse.html b/tools/linux64/src/dfu-util/www/dfuse.html new file mode 100644 index 0000000..35e4ffa --- /dev/null +++ b/tools/linux64/src/dfu-util/www/dfuse.html @@ -0,0 +1,135 @@ + + + + + + DfuSe and dfu-util + + + + + + + + + +
+

Using dfu-util with DfuSe devices

+

DfuSe

+

+ DfuSe (DFU with ST Microsystems extensions) is a protocol based on + DFU 1.1. However, in expanding the functionality of the DFU protocol, + ST Microsystems broke all compatibility with the DFU 1.1 standard. + DfuSe devices report the DFU version as "1.1a". +

+

+ DfuSe can be used to download firmware and other data + from a host computer to a conforming device (or upload in the + opposite direction) over USB similar to standard DFU. +

+

+ The main difference from standard DFU is that the target address in + the device (flash) memory is specified by the host, so that a + download can be performed to parts of the device memory. The host + program is also responsible for erasing flash pages before they + are written to. +

+

.dfu files

+

+ A special file format is defined by ST Microsystems to carry firmware + for DfuSe devices. The file contains target information such as address + and alternate interface information in addition to the binary data. + Several blocks of binary data can be combined in one .dfu file. +

+

Alternate interfaces

+

+ Different memory locations of the device may have different + characteristics that the host program (dfu-util) has to take + into considerations, such as flash memory page size, read-only + versus read-write segments, the need to erase, and so on. + These parameters are reported by the device in the string + descriptors meant for describing the USB interfaces. + The host program decodes these strings to build a memory map of + the device. Different memory units or address spaces are listed + in separate alternate interface settings that must be selected + according to the memory unit to access. +

+

+ Note that dfu-util leaves it to the user to select alternate + interface. When parsing a .dfu file it will skip file segments + not matching the selected alternate interface. Also, some + DfuSe device firmware implementations ignore the setting of + alternate interface and deduct the memory unit from the + address, since they have no address space overlap. +

+

DfuSe special commands

+

+ DfuSe special commands are used by the host program during normal + downloads or uploads, such as SET_ADDRESS and ERASE_PAGE. Also + the normal DFU_DNLOAD and DFU_UPLOAD commands have special + implementations in DfuSe. + Many DfuSe devices also support commands to leave DFU mode, + read unprotect the flash memory or mass erase the flash memory. + dfu-util (from version 0.7) + supports adding "leave", "unprotect", or "mass-erase" + to the -s option argument to send such requests in combination + with a download request. These modifiers are separated with a colon. +

+

+ Some DfuSe devices have their DfuSe bootloader running from flash + memory. Erasing the whole flash memory would therefore destroy + the DfuSe bootloader itself and practically brick the device + for most users. Any use of modifiers such as "unprotect" + and "mass-erase" therefore needs to be combined with the "force" + modifer. This is not included in the examples, to not encourage + ignorant users to copy and paste such instructions and shoot + themselves in the foot. +

+

+ Devices based on for instance STM32F103 all run the bootloader + from flash, since there is no USB bootloader in ROM. +

+

+ For instance STM32F107, STM32F2xx and STM32F4xx devices have a + DfuSe bootloader in ROM, so the flash can be erased while + keeping the device available for USB DFU transfers as long + as the device designers use this built-in bootloader and have + not implemented another DfuSe bootloader in flash that the user is + dependent upon. +

+

+ Well-written bootloaders running from flash will report their + own memory region as read-only and not eraseable, but this does + not prevent dfu-util from sending a "unprotect" or "mass-erase" + request which overrides this, if the user insists. +

+

Example usage

+

+ Flashing a .dfu (special DfuSe format) file to the device: +

+
+         $ dfu-util -a 0 -D /path/to/dfuse-image.dfu
+	
+

+ Reading out 1 KB of flash starting at address 0x8000000: +

+
+         $ dfu-util -a 0 -s 0x08000000:1024 -U newfile.bin
+	
+

+ Flashing a binary file to address 0x8004000 of device memory and ask + the device to leave DFU mode: +

+
+         $ dfu-util -a 0 -s 0x08004000:leave -D /path/to/image.bin
+	
+

+ [Back to dfu-util main page] +

+ +
+ + diff --git a/tools/linux64/src/dfu-util/www/index.html b/tools/linux64/src/dfu-util/www/index.html new file mode 100644 index 0000000..108ddaf --- /dev/null +++ b/tools/linux64/src/dfu-util/www/index.html @@ -0,0 +1,119 @@ + + + + + + dfu-util Homepage + + + + + + + + + +
+

dfu-util - Device Firmware Upgrade Utilities

+

Description

+

+ dfu-util is a host side implementation of the DFU 1.0 and DFU 1.1 specifications of the USB forum. + + DFU is intended to download and upload firmware to/from devices connected + over USB. It ranges from small devices like micro-controller boards + to mobile phones. Using dfu-util you can download firmware to your + DFU-enabled device or upload firmware from it. dfu-util has been + tested with the Openmoko Neo1973 and Freerunner and many other devices. +

+

+ See the manual page for examples of use. +

+

Supported Devices

+ +

Releases

+

+ Releases of the dfu-util software can be found in the + releases folder. + The current release is 0.8. +

+

+ We offer binaries for Microsoft Windows and some other platforms. + dfu-util uses libusb 1.0 to access your device, so + on Windows you have to register the device with the WinUSB driver + (alternatively libusb-win32 or libusbK), please see the libusbx wiki + for more details. +

+

+ Mac OS X users can also get dfu-util from Homebrew with "brew install dfu-util" or from MacPorts. +

+

+ Most Linux distributions ship dfu-util in binary packages for those + who do not want to compile dfu-util from source. + On Debian, Ubuntu, Fedora and Gentoo you can install it through the + normal software package tools. For other distributions +(namely OpenSuSe, Mandriva, and CentOS) Holger Freyther was kind enough to +provide binary packages through the Open Build Service. +

+

Development

+

+ Development happens in a GIT repository. Browse it via the web +interface or clone it with: +

+
+	git clone git://gitorious.org/dfu-util/dfu-util.git
+	
+

+ See our build instructions for how to + build the source on different platforms. +

+

License

+

+ This software is licensed under the GPL version 2. +

+

Contact

+

+ If you have questions about the development or use of dfu-util please + send an e-mail to our dedicated +mailing list for dfu-util. +

+

People

+

+ dfu-util was originally written by + Harald Welte partially based on code from + dfu-programmer 0.4 and is currently maintained by Stefan Schmidt and + Tormod Volden. +

+ +
+ + diff --git a/tools/linux64/src/dfu-util/www/simple.css b/tools/linux64/src/dfu-util/www/simple.css new file mode 100644 index 0000000..98100bc --- /dev/null +++ b/tools/linux64/src/dfu-util/www/simple.css @@ -0,0 +1,56 @@ +body { + margin: 10px; + font-size: 0.82em; + background-color: #EEE; +} + +h1 { + clear: both; + padding: 0 0 12px 0; + margin: 0; + font-size: 2em; + font-weight: bold; +} + +h2 { + clear: both; + margin: 0; + font-size: 1.5em; + font-weight: normal; +} + +h3 { + clear: both; + margin: 15px 0 0 0; + font-size: 1.0em; + font-weight: bold; +} + +p { + line-height: 20px; + padding: 8px 0 8px 0; + margin: 0; + font-size: 1.1em; +} + +pre { + white-space: pre-wrap; + background-color: #CCC; + padding: 3px; +} + +a:hover { + background-color: #DDD; +} + +#middlebox { + width: 600px; + margin: 0px auto; + text-align: left; +} + +#footer { + height: 100px; + padding: 28px 3px 0 0; + margin: 20px 0 20px 0; +} diff --git a/tools/linux64/src/maple_loader/README.md b/tools/linux64/src/maple_loader/README.md new file mode 100644 index 0000000..c6c9379 --- /dev/null +++ b/tools/linux64/src/maple_loader/README.md @@ -0,0 +1,5 @@ +These files build the maple_loader.jar file used on Windows to reset the Sketch via USB Serial, so that the bootloader will run in dfu upload mode, ready for a new sketch to be uploaded + +The files were written by @bobC (github) and have been slightly modified by me (Roger Clark), so that dfu-util no longer attempts to reset the board after upload. +This change to dfu-util's reset command line argument, was required because dfu-util was showing errors on some Windows systems, because the bootloader had reset its self after upload, +before dfu-util had chance to tell it to reset. \ No newline at end of file diff --git a/tools/linux64/src/maple_loader/build.xml b/tools/linux64/src/maple_loader/build.xml new file mode 100644 index 0000000..80bdd6f --- /dev/null +++ b/tools/linux64/src/maple_loader/build.xml @@ -0,0 +1,73 @@ + + + + + + + + + + + Builds, tests, and runs the project maple_loader. + + + diff --git a/tools/linux64/src/maple_loader/build/built-jar.properties b/tools/linux64/src/maple_loader/build/built-jar.properties new file mode 100644 index 0000000..10752d5 --- /dev/null +++ b/tools/linux64/src/maple_loader/build/built-jar.properties @@ -0,0 +1,4 @@ +#Mon, 20 Jul 2015 11:21:26 +1000 + + +C\:\\Users\\rclark\\Desktop\\maple-asp-master\\installer\\maple_loader= diff --git a/tools/linux64/src/maple_loader/build/classes/CliTemplate/CliMain.class b/tools/linux64/src/maple_loader/build/classes/CliTemplate/CliMain.class new file mode 100644 index 0000000..37ee630 Binary files /dev/null and b/tools/linux64/src/maple_loader/build/classes/CliTemplate/CliMain.class differ diff --git a/tools/linux64/src/maple_loader/build/classes/CliTemplate/DFUUploader.class b/tools/linux64/src/maple_loader/build/classes/CliTemplate/DFUUploader.class new file mode 100644 index 0000000..77087b0 Binary files /dev/null and b/tools/linux64/src/maple_loader/build/classes/CliTemplate/DFUUploader.class differ diff --git a/tools/linux64/src/maple_loader/build/classes/CliTemplate/ExecCommand.class b/tools/linux64/src/maple_loader/build/classes/CliTemplate/ExecCommand.class new file mode 100644 index 0000000..ad95f79 Binary files /dev/null and b/tools/linux64/src/maple_loader/build/classes/CliTemplate/ExecCommand.class differ diff --git a/tools/linux64/src/maple_loader/build/classes/processing/app/Base.class b/tools/linux64/src/maple_loader/build/classes/processing/app/Base.class new file mode 100644 index 0000000..4aa0bde Binary files /dev/null and b/tools/linux64/src/maple_loader/build/classes/processing/app/Base.class differ diff --git a/tools/linux64/src/maple_loader/build/classes/processing/app/Preferences.class b/tools/linux64/src/maple_loader/build/classes/processing/app/Preferences.class new file mode 100644 index 0000000..89cf010 Binary files /dev/null and b/tools/linux64/src/maple_loader/build/classes/processing/app/Preferences.class differ diff --git a/tools/linux64/src/maple_loader/build/classes/processing/app/Serial.class b/tools/linux64/src/maple_loader/build/classes/processing/app/Serial.class new file mode 100644 index 0000000..cceccdd Binary files /dev/null and b/tools/linux64/src/maple_loader/build/classes/processing/app/Serial.class differ diff --git a/tools/linux64/src/maple_loader/build/classes/processing/app/SerialException.class b/tools/linux64/src/maple_loader/build/classes/processing/app/SerialException.class new file mode 100644 index 0000000..71048dd Binary files /dev/null and b/tools/linux64/src/maple_loader/build/classes/processing/app/SerialException.class differ diff --git a/tools/linux64/src/maple_loader/build/classes/processing/app/debug/MessageConsumer.class b/tools/linux64/src/maple_loader/build/classes/processing/app/debug/MessageConsumer.class new file mode 100644 index 0000000..37250e7 Binary files /dev/null and b/tools/linux64/src/maple_loader/build/classes/processing/app/debug/MessageConsumer.class differ diff --git a/tools/linux64/src/maple_loader/build/classes/processing/app/debug/MessageSiphon.class b/tools/linux64/src/maple_loader/build/classes/processing/app/debug/MessageSiphon.class new file mode 100644 index 0000000..e22c8d4 Binary files /dev/null and b/tools/linux64/src/maple_loader/build/classes/processing/app/debug/MessageSiphon.class differ diff --git a/tools/linux64/src/maple_loader/build/classes/processing/app/debug/RunnerException.class b/tools/linux64/src/maple_loader/build/classes/processing/app/debug/RunnerException.class new file mode 100644 index 0000000..710f796 Binary files /dev/null and b/tools/linux64/src/maple_loader/build/classes/processing/app/debug/RunnerException.class differ diff --git a/tools/linux64/src/maple_loader/build/classes/processing/app/helpers/ProcessUtils.class b/tools/linux64/src/maple_loader/build/classes/processing/app/helpers/ProcessUtils.class new file mode 100644 index 0000000..27eca62 Binary files /dev/null and b/tools/linux64/src/maple_loader/build/classes/processing/app/helpers/ProcessUtils.class differ diff --git a/tools/linux64/src/maple_loader/dist/README.TXT b/tools/linux64/src/maple_loader/dist/README.TXT new file mode 100644 index 0000000..255b89c --- /dev/null +++ b/tools/linux64/src/maple_loader/dist/README.TXT @@ -0,0 +1,32 @@ +======================== +BUILD OUTPUT DESCRIPTION +======================== + +When you build an Java application project that has a main class, the IDE +automatically copies all of the JAR +files on the projects classpath to your projects dist/lib folder. The IDE +also adds each of the JAR files to the Class-Path element in the application +JAR files manifest file (MANIFEST.MF). + +To run the project from the command line, go to the dist folder and +type the following: + +java -jar "maple_loader.jar" + +To distribute this project, zip up the dist folder (including the lib folder) +and distribute the ZIP file. + +Notes: + +* If two JAR files on the project classpath have the same name, only the first +JAR file is copied to the lib folder. +* Only JAR files are copied to the lib folder. +If the classpath contains other types of files or folders, these files (folders) +are not copied. +* If a library on the projects classpath also has a Class-Path element +specified in the manifest,the content of the Class-Path element has to be on +the projects runtime path. +* To set a main class in a standard Java project, right-click the project node +in the Projects window and choose Properties. Then click Run and enter the +class name in the Main Class field. Alternatively, you can manually type the +class name in the manifest Main-Class element. diff --git a/tools/linux64/src/maple_loader/dist/lib/jssc.jar b/tools/linux64/src/maple_loader/dist/lib/jssc.jar new file mode 100644 index 0000000..eb74f15 Binary files /dev/null and b/tools/linux64/src/maple_loader/dist/lib/jssc.jar differ diff --git a/tools/linux64/src/maple_loader/dist/maple_loader.jar b/tools/linux64/src/maple_loader/dist/maple_loader.jar new file mode 100644 index 0000000..e1f9965 Binary files /dev/null and b/tools/linux64/src/maple_loader/dist/maple_loader.jar differ diff --git a/tools/linux64/src/maple_loader/jars/jssc.jar b/tools/linux64/src/maple_loader/jars/jssc.jar new file mode 100644 index 0000000..eb74f15 Binary files /dev/null and b/tools/linux64/src/maple_loader/jars/jssc.jar differ diff --git a/tools/linux64/src/maple_loader/manifest.mf b/tools/linux64/src/maple_loader/manifest.mf new file mode 100644 index 0000000..328e8e5 --- /dev/null +++ b/tools/linux64/src/maple_loader/manifest.mf @@ -0,0 +1,3 @@ +Manifest-Version: 1.0 +X-COMMENT: Main-Class will be added automatically by build + diff --git a/tools/linux64/src/maple_loader/nbproject/build-impl.xml b/tools/linux64/src/maple_loader/nbproject/build-impl.xml new file mode 100644 index 0000000..a66f349 --- /dev/null +++ b/tools/linux64/src/maple_loader/nbproject/build-impl.xml @@ -0,0 +1,1413 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Must set src.dir + Must set test.src.dir + Must set build.dir + Must set dist.dir + Must set build.classes.dir + Must set dist.javadoc.dir + Must set build.test.classes.dir + Must set build.test.results.dir + Must set build.classes.excludes + Must set dist.jar + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Must set javac.includes + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + No tests executed. + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Must set JVM to use for profiling in profiler.info.jvm + Must set profiler agent JVM arguments in profiler.info.jvmargs.agent + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Must select some files in the IDE or set javac.includes + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + To run this application from the command line without Ant, try: + + java -jar "${dist.jar.resolved}" + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Must select one file in the IDE or set run.class + + + + Must select one file in the IDE or set run.class + + + + + + + + + + + + + + + + + + + + + + + Must select one file in the IDE or set debug.class + + + + + Must select one file in the IDE or set debug.class + + + + + Must set fix.includes + + + + + + + + + + This target only works when run from inside the NetBeans IDE. + + + + + + + + + Must select one file in the IDE or set profile.class + This target only works when run from inside the NetBeans IDE. + + + + + + + + + This target only works when run from inside the NetBeans IDE. + + + + + + + + + + + + + This target only works when run from inside the NetBeans IDE. + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Must select one file in the IDE or set run.class + + + + + + Must select some files in the IDE or set test.includes + + + + + Must select one file in the IDE or set run.class + + + + + Must select one file in the IDE or set applet.url + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Must select some files in the IDE or set javac.includes + + + + + + + + + + + + + + + + + + + + Some tests failed; see details above. + + + + + + + + + Must select some files in the IDE or set test.includes + + + + Some tests failed; see details above. + + + + Must select some files in the IDE or set test.class + Must select some method in the IDE or set test.method + + + + Some tests failed; see details above. + + + + + Must select one file in the IDE or set test.class + + + + Must select one file in the IDE or set test.class + Must select some method in the IDE or set test.method + + + + + + + + + + + + + + Must select one file in the IDE or set applet.url + + + + + + + + + Must select one file in the IDE or set applet.url + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/tools/linux64/src/maple_loader/nbproject/genfiles.properties b/tools/linux64/src/maple_loader/nbproject/genfiles.properties new file mode 100644 index 0000000..c136721 --- /dev/null +++ b/tools/linux64/src/maple_loader/nbproject/genfiles.properties @@ -0,0 +1,8 @@ +build.xml.data.CRC32=2e6a03ba +build.xml.script.CRC32=4676ee6b +build.xml.stylesheet.CRC32=8064a381@1.75.2.48 +# This file is used by a NetBeans-based IDE to track changes in generated files such as build-impl.xml. +# Do not edit this file. You may delete it but then the IDE will never regenerate such files for you. +nbproject/build-impl.xml.data.CRC32=2e6a03ba +nbproject/build-impl.xml.script.CRC32=392b3f79 +nbproject/build-impl.xml.stylesheet.CRC32=876e7a8f@1.75.2.48 diff --git a/tools/linux64/src/maple_loader/nbproject/private/config.properties b/tools/linux64/src/maple_loader/nbproject/private/config.properties new file mode 100644 index 0000000..e69de29 diff --git a/tools/linux64/src/maple_loader/nbproject/private/private.properties b/tools/linux64/src/maple_loader/nbproject/private/private.properties new file mode 100644 index 0000000..e5c9f10 --- /dev/null +++ b/tools/linux64/src/maple_loader/nbproject/private/private.properties @@ -0,0 +1,6 @@ +compile.on.save=true +do.depend=false +do.jar=true +javac.debug=true +javadoc.preview=true +user.properties.file=C:\\Users\\rclark\\AppData\\Roaming\\NetBeans\\8.0.2\\build.properties diff --git a/tools/linux64/src/maple_loader/nbproject/private/private.xml b/tools/linux64/src/maple_loader/nbproject/private/private.xml new file mode 100644 index 0000000..a1bbd60 --- /dev/null +++ b/tools/linux64/src/maple_loader/nbproject/private/private.xml @@ -0,0 +1,10 @@ + + + + + + file:/C:/Users/rclark/Desktop/maple-asp-master/installer/maple_loader/src/CliTemplate/CliMain.java + file:/C:/Users/rclark/Desktop/maple-asp-master/installer/maple_loader/src/CliTemplate/DFUUploader.java + + + diff --git a/tools/linux64/src/maple_loader/nbproject/project.properties b/tools/linux64/src/maple_loader/nbproject/project.properties new file mode 100644 index 0000000..7f48d71 --- /dev/null +++ b/tools/linux64/src/maple_loader/nbproject/project.properties @@ -0,0 +1,79 @@ +annotation.processing.enabled=true +annotation.processing.enabled.in.editor=false +annotation.processing.processors.list= +annotation.processing.run.all.processors=true +annotation.processing.source.output=${build.generated.sources.dir}/ap-source-output +application.title=maple_loader +application.vendor=bob +build.classes.dir=${build.dir}/classes +build.classes.excludes=**/*.java,**/*.form +# This directory is removed when the project is cleaned: +build.dir=build +build.generated.dir=${build.dir}/generated +build.generated.sources.dir=${build.dir}/generated-sources +# Only compile against the classpath explicitly listed here: +build.sysclasspath=ignore +build.test.classes.dir=${build.dir}/test/classes +build.test.results.dir=${build.dir}/test/results +# Uncomment to specify the preferred debugger connection transport: +#debug.transport=dt_socket +debug.classpath=\ + ${run.classpath} +debug.test.classpath=\ + ${run.test.classpath} +# Files in build.classes.dir which should be excluded from distribution jar +dist.archive.excludes= +# This directory is removed when the project is cleaned: +dist.dir=dist +dist.jar=${dist.dir}/maple_loader.jar +dist.javadoc.dir=${dist.dir}/javadoc +endorsed.classpath= +excludes= +file.reference.jssc.jar=dist/lib/jssc.jar +file.reference.jssc.jar-1=jars/jssc.jar +includes=** +jar.compress=false +javac.classpath=\ + ${file.reference.jssc.jar}:\ + ${file.reference.jssc.jar-1} +# Space-separated list of extra javac options +javac.compilerargs= +javac.deprecation=false +javac.processorpath=\ + ${javac.classpath} +javac.source=1.7 +javac.target=1.7 +javac.test.classpath=\ + ${javac.classpath}:\ + ${build.classes.dir} +javac.test.processorpath=\ + ${javac.test.classpath} +javadoc.additionalparam= +javadoc.author=false +javadoc.encoding=${source.encoding} +javadoc.noindex=false +javadoc.nonavbar=false +javadoc.notree=false +javadoc.private=false +javadoc.splitindex=true +javadoc.use=true +javadoc.version=false +javadoc.windowtitle= +main.class=CliTemplate.CliMain +manifest.file=manifest.mf +meta.inf.dir=${src.dir}/META-INF +mkdist.disabled=false +platform.active=default_platform +run.classpath=\ + ${javac.classpath}:\ + ${build.classes.dir} +# Space-separated list of JVM arguments used when running the project. +# You may also define separate properties like run-sys-prop.name=value instead of -Dname=value. +# To set system properties for unit tests define test-sys-prop.name=value: +run.jvmargs= +run.test.classpath=\ + ${javac.test.classpath}:\ + ${build.test.classes.dir} +source.encoding=UTF-8 +src.dir=src +test.src.dir=test diff --git a/tools/linux64/src/maple_loader/nbproject/project.xml b/tools/linux64/src/maple_loader/nbproject/project.xml new file mode 100644 index 0000000..92218a9 --- /dev/null +++ b/tools/linux64/src/maple_loader/nbproject/project.xml @@ -0,0 +1,15 @@ + + + org.netbeans.modules.java.j2seproject + + + maple_loader + + + + + + + + + diff --git a/tools/linux64/src/maple_loader/src/CliTemplate/CliMain.java b/tools/linux64/src/maple_loader/src/CliTemplate/CliMain.java new file mode 100644 index 0000000..c7dc9f0 --- /dev/null +++ b/tools/linux64/src/maple_loader/src/CliTemplate/CliMain.java @@ -0,0 +1,60 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +package CliTemplate; + +import java.io.IOException; +import java.text.MessageFormat; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import processing.app.Preferences; + +//import processing.app.I18n; +import processing.app.helpers.ProcessUtils; + +/** + * + * @author cousinr + */ +public class CliMain { + + + /** + * @param args the command line arguments + */ + public static void main(String[] args) { + + String comPort = args[0]; // + String altIf = args[1]; // + String usbID = args[2]; // "1EAF:0003"; + String binFile = args[3]; // bin file + + System.out.println("maple_loader v0.1"); + + Preferences.set ("serial.port",comPort); + Preferences.set ("serial.parity","N"); + Preferences.setInteger ("serial.databits", 8); + Preferences.setInteger ("serial.debug_rate",9600); + Preferences.setInteger ("serial.stopbits",1); + + Preferences.setInteger ("programDelay",1200); + + Preferences.set ("upload.usbID", usbID); + Preferences.set ("upload.altID", altIf); + Preferences.setBoolean ("upload.auto_reset", true); + Preferences.setBoolean ("upload.verbose", false); + + // + DFUUploader dfuUploader = new DFUUploader(); + try { + //dfuUploader.uploadViaDFU(binFile); + dfuUploader.uploadViaDFU(binFile); + } catch (Exception e) + { + System.err.print (MessageFormat.format("an error occurred! {0}\n", e.getMessage())); + } + } +} diff --git a/tools/linux64/src/maple_loader/src/CliTemplate/DFUUploader.java b/tools/linux64/src/maple_loader/src/CliTemplate/DFUUploader.java new file mode 100644 index 0000000..3dee0b4 --- /dev/null +++ b/tools/linux64/src/maple_loader/src/CliTemplate/DFUUploader.java @@ -0,0 +1,345 @@ +/* + DFUUploader - uploader implementation using DFU + + Copyright (c) 2010 + Andrew Meyer + + 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 + +*/ + +package CliTemplate; + +import java.io.BufferedReader; +import java.io.File; +import java.io.InputStreamReader; +import java.util.ArrayList; +import java.util.Collection; +import java.util.List; +import processing.app.Preferences; +import processing.app.Serial; +import processing.app.debug.MessageConsumer; +import processing.app.debug.MessageSiphon; +import processing.app.debug.RunnerException; + +/** + * + * @author bob + */ +public class DFUUploader implements MessageConsumer { + + boolean firstErrorFound; + boolean secondErrorFound; + // part of the PdeMessageConsumer interface + boolean notFoundError; + boolean verbose; + RunnerException exception; + + static final String SUPER_BADNESS = + "Compiler error!"; + + public boolean uploadUsingPreferences(String binPath, boolean verbose) + throws RunnerException { + + this.verbose = verbose; + + return uploadViaDFU(binPath); + } + + // works with old and new versions of dfu-util + private boolean found_device (String dfuData, String usbID) + { + return dfuData.contains(("Found DFU: [0x"+usbID.substring(0,4)).toUpperCase()) || + dfuData.contains(("Found DFU: ["+usbID.substring(0,4)).toUpperCase()); + } + + public boolean uploadViaDFU (String binPath) + throws RunnerException { + + this.verbose = Preferences.getBoolean ("upload.verbose"); + + /* todo, check for size overruns! */ + String fileType="bin"; + + if (fileType.equals("bin")) { + String usbID = Preferences.get("upload.usbID"); + if (usbID == null) { + /* fall back on default */ + /* this isnt great because is default Avrdude or dfu-util? */ + usbID = Preferences.get("upload.usbID"); + } + + /* if auto-reset, then emit the reset pulse on dtr/rts */ + if (Preferences.get("upload.auto_reset") != null) { + if (Preferences.get("upload.auto_reset").toLowerCase().equals("true")) { + System.out.println("Resetting to bootloader via DTR pulse"); + emitResetPulse(); + } + } else { + System.out.println("Resetting to bootloader via DTR pulse"); + emitResetPulse(); + } + + String dfuList = new String(); + List commandCheck = new ArrayList(); + commandCheck.add("dfu-util"); + commandCheck.add("-l"); + long startChecking = System.currentTimeMillis(); + System.out.println("Searching for DFU device [" + usbID + "]..."); + do { + try { + Thread.sleep(100); + } catch (InterruptedException e) {} + dfuList = executeCheckCommand(commandCheck); + //System.out.println(dfuList); + } + while ( !found_device (dfuList.toUpperCase(), usbID) && (System.currentTimeMillis() - startChecking < 7000)); + + if ( !found_device (dfuList.toUpperCase(), usbID) ) + { + System.out.println(dfuList); + System.err.println("Couldn't find the DFU device: [" + usbID + "]"); + return false; + } + System.out.println("Found it!"); + + /* todo, add handle to let user choose altIf at upload time! */ + String altIf = Preferences.get("upload.altID"); + + List commandDownloader = new ArrayList(); + commandDownloader.add("dfu-util"); + commandDownloader.add("-a "+altIf); + commandDownloader.add("-R"); + commandDownloader.add("-d "+usbID); + commandDownloader.add("-D"+ binPath); //"./thisbin.bin"); + + return executeUploadCommand(commandDownloader); + } + + System.err.println("Only .bin files are supported at this time"); + return false; + } + + /* we need to ensure both RTS and DTR are low to start, + then pulse DTR on its own. This is the reset signal + maple responds to + */ + private void emitResetPulse() throws RunnerException { + + /* wait a while for the device to reboot */ + int programDelay = Preferences.getInteger("programDelay"); + + try { + Serial serialPort = new Serial(); + + // try to toggle DTR/RTS (old scheme) + serialPort.setRTS(false); + serialPort.setDTR(false); + serialPort.setDTR(true); + try { + Thread.sleep(50); + } catch (InterruptedException e) {} + serialPort.setDTR(false); + + // try magic number + serialPort.setRTS(true); + serialPort.setDTR(true); + try { + Thread.sleep(50); + } catch (InterruptedException e) {} + serialPort.setDTR(false); + try { + Thread.sleep(50); + } catch (InterruptedException e) {} + serialPort.write("1EAF"); + try { + Thread.sleep(50); + } catch (InterruptedException e) {} + serialPort.dispose(); + + } catch(Exception e) { + System.err.println("Reset via USB Serial Failed! Did you select the right serial port?\nAssuming the board is in perpetual bootloader mode and continuing to attempt dfu programming...\n"); + } + } + + protected String executeCheckCommand(Collection commandDownloader) + throws RunnerException + { + firstErrorFound = false; // haven't found any errors yet + secondErrorFound = false; + notFoundError = false; + int result=0; // pre-initialized to quiet a bogus warning from jikes + + String userdir = System.getProperty("user.dir") + File.separator; + String returnStr = new String(); + + try { + String[] commandArray = new String[commandDownloader.size()]; + commandDownloader.toArray(commandArray); + + String armBasePath; + + //armBasePath = new String(Base.getHardwarePath() + "/tools/arm/bin/"); + armBasePath = ""; + + commandArray[0] = armBasePath + commandArray[0]; + + if (verbose || Preferences.getBoolean("upload.verbose")) { + for(int i = 0; i < commandArray.length; i++) { + System.out.print(commandArray[i] + " "); + } + System.out.println(); + } + + Process process = Runtime.getRuntime().exec(commandArray); + BufferedReader stdInput = new BufferedReader(new + InputStreamReader(process.getInputStream())); + BufferedReader stdError = new BufferedReader(new + InputStreamReader(process.getErrorStream())); + + // wait for the process to finish. if interrupted + // before waitFor returns, continue waiting + // + boolean busy = true; + while (busy) { + try { + result = process.waitFor(); + busy = false; + } catch (InterruptedException intExc) { + } + } + + String s; + while ((s = stdInput.readLine()) != null) { + returnStr += s + "\n"; + } + + process.destroy(); + + if(exception!=null) { + exception.hideStackTrace(); + throw exception; + } + if(result!=0) return "Error!"; + } catch (Exception e) { + e.printStackTrace(); + } + //System.out.println("result2 is "+result); + // if the result isn't a known, expected value it means that something + // is fairly wrong, one possibility is that jikes has crashed. + // + if (exception != null) throw exception; + + if ((result != 0) && (result != 1 )) { + exception = new RunnerException(SUPER_BADNESS); + } + + return returnStr; // ? true : false; + + } + + // Need to overload this from Uploader to use the system-wide dfu-util + protected boolean executeUploadCommand(Collection commandDownloader) + throws RunnerException + { + firstErrorFound = false; // haven't found any errors yet + secondErrorFound = false; + notFoundError = false; + int result=0; // pre-initialized to quiet a bogus warning from jikes + + String userdir = System.getProperty("user.dir") + File.separator; + + try { + String[] commandArray = new String[commandDownloader.size()]; + commandDownloader.toArray(commandArray); + + String armBasePath; + + //armBasePath = new String(Base.getHardwarePath() + "/tools/arm/bin/"); + armBasePath = ""; + + commandArray[0] = armBasePath + commandArray[0]; + + if (verbose || Preferences.getBoolean("upload.verbose")) { + for(int i = 0; i < commandArray.length; i++) { + System.out.print(commandArray[i] + " "); + } + System.out.println(); + } + + Process process = Runtime.getRuntime().exec(commandArray); + new MessageSiphon(process.getInputStream(), this); + new MessageSiphon(process.getErrorStream(), this); + + // wait for the process to finish. if interrupted + // before waitFor returns, continue waiting + // + boolean compiling = true; + while (compiling) { + try { + result = process.waitFor(); + compiling = false; + } catch (InterruptedException intExc) { + } + } + if(exception!=null) { + exception.hideStackTrace(); + throw exception; + } + if(result!=0) + return false; + } catch (Exception e) { + e.printStackTrace(); + } + //System.out.println("result2 is "+result); + // if the result isn't a known, expected value it means that something + // is fairly wrong, one possibility is that jikes has crashed. + // + if (exception != null) throw exception; + + if ((result != 0) && (result != 1 )) { + exception = new RunnerException(SUPER_BADNESS); + //editor.error(exception); + //PdeBase.openURL(BUGS_URL); + //throw new PdeException(SUPER_BADNESS); + } + + return (result == 0); // ? true : false; + + } + + // deal with messages from dfu-util... + public void message(String s) { + + if(s.indexOf("dfu-util - (C) ") != -1) { return; } + if(s.indexOf("This program is Free Software and has ABSOLUTELY NO WARRANTY") != -1) { return; } + + if(s.indexOf("No DFU capable USB device found") != -1) { + System.err.print(s); + exception = new RunnerException("Problem uploading via dfu-util: No Maple found"); + return; + } + + if(s.indexOf("Operation not perimitted") != -1) { + System.err.print(s); + exception = new RunnerException("Problem uploading via dfu-util: Insufficient privilages"); + return; + } + + // else just print everything... + System.out.print(s); + } + +} diff --git a/tools/linux64/src/maple_loader/src/CliTemplate/ExecCommand.java b/tools/linux64/src/maple_loader/src/CliTemplate/ExecCommand.java new file mode 100644 index 0000000..3d6c106 --- /dev/null +++ b/tools/linux64/src/maple_loader/src/CliTemplate/ExecCommand.java @@ -0,0 +1,119 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +package CliTemplate; + +import java.io.IOException; +import java.text.MessageFormat; +import java.util.ArrayList; +import java.util.List; + +import processing.app.debug.MessageConsumer; +import processing.app.debug.MessageSiphon; +import processing.app.debug.RunnerException; +import processing.app.helpers.ProcessUtils; + +/** + * + * @author cousinr + */ +public class ExecCommand implements MessageConsumer { + + private boolean verbose = true; + private boolean firstErrorFound; + private boolean secondErrorFound; + private RunnerException exception; + + /** + * Either succeeds or throws a RunnerException fit for public consumption. + * + * @param command + * @throws RunnerException + */ + public void execAsynchronously(String[] command) throws RunnerException { + + // eliminate any empty array entries + List stringList = new ArrayList<>(); + for (String string : command) { + string = string.trim(); + if (string.length() != 0) + stringList.add(string); + } + command = stringList.toArray(new String[stringList.size()]); + if (command.length == 0) + return; + int result = 0; + + if (verbose) { + for (String c : command) + System.out.print(c + " "); + System.out.println(); + } + + firstErrorFound = false; // haven't found any errors yet + secondErrorFound = false; + + Process process; + try { + process = ProcessUtils.exec(command); + } catch (IOException e) { + RunnerException re = new RunnerException(e.getMessage()); + re.hideStackTrace(); + throw re; + } + + MessageSiphon in = new MessageSiphon(process.getInputStream(), this); + MessageSiphon err = new MessageSiphon(process.getErrorStream(), this); + + // wait for the process to finish. if interrupted + // before waitFor returns, continue waiting + boolean compiling = true; + while (compiling) { + try { + in.join(); + err.join(); + result = process.waitFor(); + //System.out.println("result is " + result); + compiling = false; + } catch (InterruptedException ignored) { } + } + + // an error was queued up by message(), barf this back to compile(), + // which will barf it back to Editor. if you're having trouble + // discerning the imagery, consider how cows regurgitate their food + // to digest it, and the fact that they have five stomaches. + // + //System.out.println("throwing up " + exception); + if (exception != null) + throw exception; + + if (result > 1) { + // a failure in the tool (e.g. unable to locate a sub-executable) + System.err.println(MessageFormat.format("{0} returned {1}", command[0], result)); + } + + if (result != 0) { + RunnerException re = new RunnerException(MessageFormat.format("exit code: {0}", result)); + re.hideStackTrace(); + throw re; + } + } + + /** + * Part of the MessageConsumer interface, this is called + * whenever a piece (usually a line) of error message is spewed + * out from the compiler. The errors are parsed for their contents + * and line number, which is then reported back to Editor. + * @param s + */ + @Override + public void message(String s) { + int i; + + + System.err.print(s); + } + +} diff --git a/tools/linux64/src/maple_loader/src/processing/app/Base.java b/tools/linux64/src/maple_loader/src/processing/app/Base.java new file mode 100644 index 0000000..c3a174d --- /dev/null +++ b/tools/linux64/src/maple_loader/src/processing/app/Base.java @@ -0,0 +1,53 @@ +/* -*- mode: java; c-basic-offset: 2; indent-tabs-mode: nil -*- */ + +/* + Part of the Processing project - http://processing.org + + Copyright (c) 2004-10 Ben Fry and Casey Reas + Copyright (c) 2001-04 Massachusetts Institute of Technology + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License version 2 + as published by the Free Software Foundation. + + 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 +*/ + +package processing.app; + + +/** + * The base class for the main processing application. + * Primary role of this class is for platform identification and + * general interaction with the system (launching URLs, loading + * files and images, etc) that comes from that. + */ +public class Base { + + /** + * returns true if running on windows. + */ + static public boolean isWindows() { + //return PApplet.platform == PConstants.WINDOWS; + return System.getProperty("os.name").indexOf("Windows") != -1; + } + + + /** + * true if running on linux. + */ + static public boolean isLinux() { + //return PApplet.platform == PConstants.LINUX; + return System.getProperty("os.name").indexOf("Linux") != -1; + } + + + +} diff --git a/tools/linux64/src/maple_loader/src/processing/app/Preferences.java b/tools/linux64/src/maple_loader/src/processing/app/Preferences.java new file mode 100644 index 0000000..6368e38 --- /dev/null +++ b/tools/linux64/src/maple_loader/src/processing/app/Preferences.java @@ -0,0 +1,157 @@ +/* -*- mode: java; c-basic-offset: 2; indent-tabs-mode: nil -*- */ + +/* + Part of the Processing project - http://processing.org + + Copyright (c) 2004-09 Ben Fry and Casey Reas + Copyright (c) 2001-04 Massachusetts Institute of Technology + + 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 +*/ + +package processing.app; + +import java.io.*; +import java.util.*; + + +/** + * Storage class for user preferences and environment settings. + *

+ * This class no longer uses the Properties class, since + * properties files are iso8859-1, which is highly likely to + * be a problem when trying to save sketch folders and locations. + *

+ * The GUI portion in here is really ugly, as it uses exact layout. This was + * done in frustration one evening (and pre-Swing), but that's long since past, + * and it should all be moved to a proper swing layout like BoxLayout. + *

+ * This is very poorly put together, that the preferences panel and the actual + * preferences i/o is part of the same code. But there hasn't yet been a + * compelling reason to bother with the separation aside from concern about + * being lectured by strangers who feel that it doesn't look like what they + * learned in CS class. + *

+ * Would also be possible to change this to use the Java Preferences API. + * Some useful articles + * here and + * here. + * However, haven't implemented this yet for lack of time, but more + * importantly, because it would entail writing to the registry (on Windows), + * or an obscure file location (on Mac OS X) and make it far more difficult to + * find the preferences to tweak them by hand (no! stay out of regedit!) + * or to reset the preferences by simply deleting the preferences.txt file. + */ +public class Preferences { + + // what to call the feller + + static final String PREFS_FILE = "preferences.txt"; + + + // prompt text stuff + + static final String PROMPT_YES = "Yes"; + static final String PROMPT_NO = "No"; + static final String PROMPT_CANCEL = "Cancel"; + static final String PROMPT_OK = "OK"; + static final String PROMPT_BROWSE = "Browse"; + + /** + * Standardized width for buttons. Mac OS X 10.3 wants 70 as its default, + * Windows XP needs 66, and my Ubuntu machine needs 80+, so 80 seems proper. + */ + static public int BUTTON_WIDTH = 80; + + /** + * Standardized button height. Mac OS X 10.3 (Java 1.4) wants 29, + * presumably because it now includes the blue border, where it didn't + * in Java 1.3. Windows XP only wants 23 (not sure what default Linux + * would be). Because of the disparity, on Mac OS X, it will be set + * inside a static block. + */ + static public int BUTTON_HEIGHT = 24; + + // value for the size bars, buttons, etc + + static final int GRID_SIZE = 33; + + + // indents and spacing standards. these probably need to be modified + // per platform as well, since macosx is so huge, windows is smaller, + // and linux is all over the map + + static final int GUI_BIG = 13; + static final int GUI_BETWEEN = 10; + static final int GUI_SMALL = 6; + + + + // data model + + static Hashtable table = new Hashtable();; + static File preferencesFile; + + + static protected void init(String commandLinePrefs) { + + + } + + + public Preferences() { + + } + + // ................................................................. + + // ................................................................. + + // ................................................................. + + // ................................................................. + + + + static public String get(String attribute) { + return (String) table.get(attribute); + } + + static public void set(String attribute, String value) { + table.put(attribute, value); + } + + + static public boolean getBoolean(String attribute) { + String value = get(attribute); + return (new Boolean(value)).booleanValue(); + } + + + static public void setBoolean(String attribute, boolean value) { + set(attribute, value ? "true" : "false"); + } + + + static public int getInteger(String attribute) { + return Integer.parseInt(get(attribute)); + } + + + static public void setInteger(String key, int value) { + set(key, String.valueOf(value)); + } + +} diff --git a/tools/linux64/src/maple_loader/src/processing/app/Serial.java b/tools/linux64/src/maple_loader/src/processing/app/Serial.java new file mode 100644 index 0000000..04566a7 --- /dev/null +++ b/tools/linux64/src/maple_loader/src/processing/app/Serial.java @@ -0,0 +1,527 @@ +/* -*- mode: jde; c-basic-offset: 2; indent-tabs-mode: nil -*- */ + +/* + PSerial - class for serial port goodness + Part of the Processing project - http://processing.org + + Copyright (c) 2004 Ben Fry & Casey Reas + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + This library 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 + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General + Public License along with this library; if not, write to the + Free Software Foundation, Inc., 59 Temple Place, Suite 330, + Boston, MA 02111-1307 USA +*/ + +package processing.app; +//import processing.core.*; + + +import java.io.*; +import java.text.MessageFormat; +import java.util.*; +import jssc.SerialPort; +import jssc.SerialPortEvent; +import jssc.SerialPortEventListener; +import jssc.SerialPortException; +import jssc.SerialPortList; +import processing.app.debug.MessageConsumer; + + +public class Serial implements SerialPortEventListener { + + //PApplet parent; + + // properties can be passed in for default values + // otherwise defaults to 9600 N81 + + // these could be made static, which might be a solution + // for the classloading problem.. because if code ran again, + // the static class would have an object that could be closed + + SerialPort port; + + int rate; + int parity; + int databits; + int stopbits; + boolean monitor = false; + + // read buffer and streams + + InputStream input; + OutputStream output; + + byte buffer[] = new byte[32768]; + int bufferIndex; + int bufferLast; + + MessageConsumer consumer; + + public Serial(boolean monitor) throws SerialException { + this(Preferences.get("serial.port"), + Preferences.getInteger("serial.debug_rate"), + Preferences.get("serial.parity").charAt(0), + Preferences.getInteger("serial.databits"), + new Float(Preferences.get("serial.stopbits")).floatValue()); + this.monitor = monitor; + } + + public Serial() throws SerialException { + this(Preferences.get("serial.port"), + Preferences.getInteger("serial.debug_rate"), + Preferences.get("serial.parity").charAt(0), + Preferences.getInteger("serial.databits"), + new Float(Preferences.get("serial.stopbits")).floatValue()); + } + + public Serial(int irate) throws SerialException { + this(Preferences.get("serial.port"), irate, + Preferences.get("serial.parity").charAt(0), + Preferences.getInteger("serial.databits"), + new Float(Preferences.get("serial.stopbits")).floatValue()); + } + + public Serial(String iname, int irate) throws SerialException { + this(iname, irate, Preferences.get("serial.parity").charAt(0), + Preferences.getInteger("serial.databits"), + new Float(Preferences.get("serial.stopbits")).floatValue()); + } + + public Serial(String iname) throws SerialException { + this(iname, Preferences.getInteger("serial.debug_rate"), + Preferences.get("serial.parity").charAt(0), + Preferences.getInteger("serial.databits"), + new Float(Preferences.get("serial.stopbits")).floatValue()); + } + + public Serial(String iname, int irate, + char iparity, int idatabits, float istopbits) + throws SerialException { + //if (port != null) port.close(); + //this.parent = parent; + //parent.attach(this); + + this.rate = irate; + + parity = SerialPort.PARITY_NONE; + if (iparity == 'E') parity = SerialPort.PARITY_EVEN; + if (iparity == 'O') parity = SerialPort.PARITY_ODD; + + this.databits = idatabits; + + stopbits = SerialPort.STOPBITS_1; + if (istopbits == 1.5f) stopbits = SerialPort.STOPBITS_1_5; + if (istopbits == 2) stopbits = SerialPort.STOPBITS_2; + + try { + port = new SerialPort(iname); + port.openPort(); + port.setParams(rate, databits, stopbits, parity, true, true); + port.addEventListener(this); + } catch (Exception e) { + throw new SerialException(MessageFormat.format("Error opening serial port ''{0}''.", iname), e); + } + + if (port == null) { + throw new SerialException("Serial port '" + iname + "' not found. Did you select the right one from the Tools > Serial Port menu?"); + } + } + + + public void setup() { + //parent.registerCall(this, DISPOSE); + } + + public void dispose() throws IOException { + if (port != null) { + try { + if (port.isOpened()) { + port.closePort(); // close the port + } + } catch (SerialPortException e) { + throw new IOException(e); + } finally { + port = null; + } + } + } + + public void addListener(MessageConsumer consumer) { + this.consumer = consumer; + } + + public synchronized void serialEvent(SerialPortEvent serialEvent) { + if (serialEvent.isRXCHAR()) { + try { + byte[] buf = port.readBytes(serialEvent.getEventValue()); + if (buf.length > 0) { + if (bufferLast == buffer.length) { + byte temp[] = new byte[bufferLast << 1]; + System.arraycopy(buffer, 0, temp, 0, bufferLast); + buffer = temp; + } + if (monitor) { + System.out.print(new String(buf)); + } + if (this.consumer != null) { + this.consumer.message(new String(buf)); + } + } + } catch (SerialPortException e) { + errorMessage("serialEvent", e); + } + } + } + + + /** + * Returns the number of bytes that have been read from serial + * and are waiting to be dealt with by the user. + */ + public synchronized int available() { + return (bufferLast - bufferIndex); + } + + + /** + * Ignore all the bytes read so far and empty the buffer. + */ + public synchronized void clear() { + bufferLast = 0; + bufferIndex = 0; + } + + + /** + * Returns a number between 0 and 255 for the next byte that's + * waiting in the buffer. + * Returns -1 if there was no byte (although the user should + * first check available() to see if things are ready to avoid this) + */ + public synchronized int read() { + if (bufferIndex == bufferLast) return -1; + + int outgoing = buffer[bufferIndex++] & 0xff; + if (bufferIndex == bufferLast) { // rewind + bufferIndex = 0; + bufferLast = 0; + } + return outgoing; + } + + + /** + * Returns the next byte in the buffer as a char. + * Returns -1, or 0xffff, if nothing is there. + */ + public synchronized char readChar() { + if (bufferIndex == bufferLast) return (char)(-1); + return (char) read(); + } + + + /** + * Return a byte array of anything that's in the serial buffer. + * Not particularly memory/speed efficient, because it creates + * a byte array on each read, but it's easier to use than + * readBytes(byte b[]) (see below). + */ + public synchronized byte[] readBytes() { + if (bufferIndex == bufferLast) return null; + + int length = bufferLast - bufferIndex; + byte outgoing[] = new byte[length]; + System.arraycopy(buffer, bufferIndex, outgoing, 0, length); + + bufferIndex = 0; // rewind + bufferLast = 0; + return outgoing; + } + + + /** + * Grab whatever is in the serial buffer, and stuff it into a + * byte buffer passed in by the user. This is more memory/time + * efficient than readBytes() returning a byte[] array. + *

+ * Returns an int for how many bytes were read. If more bytes + * are available than can fit into the byte array, only those + * that will fit are read. + */ + public synchronized int readBytes(byte outgoing[]) { + if (bufferIndex == bufferLast) return 0; + + int length = bufferLast - bufferIndex; + if (length > outgoing.length) length = outgoing.length; + System.arraycopy(buffer, bufferIndex, outgoing, 0, length); + + bufferIndex += length; + if (bufferIndex == bufferLast) { + bufferIndex = 0; // rewind + bufferLast = 0; + } + return length; + } + + + /** + * Reads from the serial port into a buffer of bytes up to and + * including a particular character. If the character isn't in + * the serial buffer, then 'null' is returned. + */ + public synchronized byte[] readBytesUntil(int interesting) { + if (bufferIndex == bufferLast) return null; + byte what = (byte)interesting; + + int found = -1; + for (int k = bufferIndex; k < bufferLast; k++) { + if (buffer[k] == what) { + found = k; + break; + } + } + if (found == -1) return null; + + int length = found - bufferIndex + 1; + byte outgoing[] = new byte[length]; + System.arraycopy(buffer, bufferIndex, outgoing, 0, length); + + bufferIndex = 0; // rewind + bufferLast = 0; + return outgoing; + } + + + /** + * Reads from the serial port into a buffer of bytes until a + * particular character. If the character isn't in the serial + * buffer, then 'null' is returned. + *

+ * If outgoing[] is not big enough, then -1 is returned, + * and an error message is printed on the console. + * If nothing is in the buffer, zero is returned. + * If 'interesting' byte is not in the buffer, then 0 is returned. + */ + public synchronized int readBytesUntil(int interesting, byte outgoing[]) { + if (bufferIndex == bufferLast) return 0; + byte what = (byte)interesting; + + int found = -1; + for (int k = bufferIndex; k < bufferLast; k++) { + if (buffer[k] == what) { + found = k; + break; + } + } + if (found == -1) return 0; + + int length = found - bufferIndex + 1; + if (length > outgoing.length) { + System.err.println("readBytesUntil() byte buffer is" + + " too small for the " + length + + " bytes up to and including char " + interesting); + return -1; + } + //byte outgoing[] = new byte[length]; + System.arraycopy(buffer, bufferIndex, outgoing, 0, length); + + bufferIndex += length; + if (bufferIndex == bufferLast) { + bufferIndex = 0; // rewind + bufferLast = 0; + } + return length; + } + + + /** + * Return whatever has been read from the serial port so far + * as a String. It assumes that the incoming characters are ASCII. + *

+ * If you want to move Unicode data, you can first convert the + * String to a byte stream in the representation of your choice + * (i.e. UTF8 or two-byte Unicode data), and send it as a byte array. + */ + public synchronized String readString() { + if (bufferIndex == bufferLast) return null; + return new String(readBytes()); + } + + + /** + * Combination of readBytesUntil and readString. See caveats in + * each function. Returns null if it still hasn't found what + * you're looking for. + *

+ * If you want to move Unicode data, you can first convert the + * String to a byte stream in the representation of your choice + * (i.e. UTF8 or two-byte Unicode data), and send it as a byte array. + */ + public synchronized String readStringUntil(int interesting) { + byte b[] = readBytesUntil(interesting); + if (b == null) return null; + return new String(b); + } + + + /** + * This will handle both ints, bytes and chars transparently. + */ + public void write(int what) { // will also cover char + try { + port.writeInt(what & 0xff); + } catch (SerialPortException e) { + errorMessage("write", e); + } + } + + + public void write(byte bytes[]) { + try { + port.writeBytes(bytes); + } catch (SerialPortException e) { + errorMessage("write", e); + } + } + + + /** + * Write a String to the output. Note that this doesn't account + * for Unicode (two bytes per char), nor will it send UTF8 + * characters.. It assumes that you mean to send a byte buffer + * (most often the case for networking and serial i/o) and + * will only use the bottom 8 bits of each char in the string. + * (Meaning that internally it uses String.getBytes) + *

+ * If you want to move Unicode data, you can first convert the + * String to a byte stream in the representation of your choice + * (i.e. UTF8 or two-byte Unicode data), and send it as a byte array. + */ + public void write(String what) { + write(what.getBytes()); + } + + public void setDTR(boolean state) { + try { + port.setDTR(state); + } catch (SerialPortException e) { + errorMessage("setDTR", e); + } + } + + public void setRTS(boolean state) { + try { + port.setRTS(state); + } catch (SerialPortException e) { + errorMessage("setRTS", e); + } + } + + static public List list() { + return Arrays.asList(SerialPortList.getPortNames()); + } + + + /** + * General error reporting, all corraled here just in case + * I think of something slightly more intelligent to do. + */ + static public void errorMessage(String where, Throwable e) { + System.err.println("Error inside Serial." + where + "()"); + e.printStackTrace(); + } +} + + + /* + class SerialMenuListener implements ItemListener { + //public SerialMenuListener() { } + + public void itemStateChanged(ItemEvent e) { + int count = serialMenu.getItemCount(); + for (int i = 0; i < count; i++) { + ((CheckboxMenuItem)serialMenu.getItem(i)).setState(false); + } + CheckboxMenuItem item = (CheckboxMenuItem)e.getSource(); + item.setState(true); + String name = item.getLabel(); + //System.out.println(item.getLabel()); + PdeBase.properties.put("serial.port", name); + //System.out.println("set to " + get("serial.port")); + } + } + */ + + + /* + protected Vector buildPortList() { + // get list of names for serial ports + // have the default port checked (if present) + Vector list = new Vector(); + + //SerialMenuListener listener = new SerialMenuListener(); + boolean problem = false; + + // if this is failing, it may be because + // lib/javax.comm.properties is missing. + // java is weird about how it searches for java.comm.properties + // so it tends to be very fragile. i.e. quotes in the CLASSPATH + // environment variable will hose things. + try { + //System.out.println("building port list"); + Enumeration portList = CommPortIdentifier.getPortIdentifiers(); + while (portList.hasMoreElements()) { + CommPortIdentifier portId = + (CommPortIdentifier) portList.nextElement(); + //System.out.println(portId); + + if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) { + //if (portId.getName().equals(port)) { + String name = portId.getName(); + //CheckboxMenuItem mi = + //new CheckboxMenuItem(name, name.equals(defaultName)); + + //mi.addItemListener(listener); + //serialMenu.add(mi); + list.addElement(name); + } + } + } catch (UnsatisfiedLinkError e) { + e.printStackTrace(); + problem = true; + + } catch (Exception e) { + System.out.println("exception building serial menu"); + e.printStackTrace(); + } + + //if (serialMenu.getItemCount() == 0) { + //System.out.println("dimming serial menu"); + //serialMenu.setEnabled(false); + //} + + // only warn them if this is the first time + if (problem && PdeBase.firstTime) { + JOptionPane.showMessageDialog(this, //frame, + "Serial port support not installed.\n" + + "Check the readme for instructions\n" + + "if you need to use the serial port. ", + "Serial Port Warning", + JOptionPane.WARNING_MESSAGE); + } + return list; + } + */ + + + diff --git a/tools/linux64/src/maple_loader/src/processing/app/SerialException.java b/tools/linux64/src/maple_loader/src/processing/app/SerialException.java new file mode 100644 index 0000000..525c240 --- /dev/null +++ b/tools/linux64/src/maple_loader/src/processing/app/SerialException.java @@ -0,0 +1,39 @@ +/* -*- mode: jde; c-basic-offset: 2; indent-tabs-mode: nil -*- */ + +/* + Copyright (c) 2007 David A. Mellis + + 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 +*/ + +package processing.app; + +public class SerialException extends Exception { + public SerialException() { + super(); + } + + public SerialException(String message) { + super(message); + } + + public SerialException(String message, Throwable cause) { + super(message, cause); + } + + public SerialException(Throwable cause) { + super(cause); + } +} diff --git a/tools/linux64/src/maple_loader/src/processing/app/debug/MessageConsumer.java b/tools/linux64/src/maple_loader/src/processing/app/debug/MessageConsumer.java new file mode 100644 index 0000000..5e20429 --- /dev/null +++ b/tools/linux64/src/maple_loader/src/processing/app/debug/MessageConsumer.java @@ -0,0 +1,42 @@ +/* -*- mode: java; c-basic-offset: 2; indent-tabs-mode: nil -*- */ + +/* + Part of the Processing project - http://processing.org + + Copyright (c) 2004-06 Ben Fry and Casey Reas + Copyright (c) 2001-04 Massachusetts Institute of Technology + + 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 +*/ + +package processing.app.debug; + + +/** + * Interface for dealing with parser/compiler output. + *

+ * Different instances of MessageStream need to do different things with + * messages. In particular, a stream instance used for parsing output from + * the compiler compiler has to interpret its messages differently than one + * parsing output from the runtime. + *

+ * Classes which consume messages and do something with them + * should implement this interface. + */ +public interface MessageConsumer { + + public void message(String s); + +} diff --git a/tools/linux64/src/maple_loader/src/processing/app/debug/MessageSiphon.java b/tools/linux64/src/maple_loader/src/processing/app/debug/MessageSiphon.java new file mode 100644 index 0000000..26901c3 --- /dev/null +++ b/tools/linux64/src/maple_loader/src/processing/app/debug/MessageSiphon.java @@ -0,0 +1,104 @@ +/* -*- mode: java; c-basic-offset: 2; indent-tabs-mode: nil -*- */ + +/* + Part of the Processing project - http://processing.org + + Copyright (c) 2004-06 Ben Fry and Casey Reas + Copyright (c) 2001-04 Massachusetts Institute of Technology + + 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 +*/ + +package processing.app.debug; + +import java.io.BufferedReader; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.net.SocketException; + +/** + * Slurps up messages from compiler. + */ +public class MessageSiphon implements Runnable { + + private final BufferedReader streamReader; + private final MessageConsumer consumer; + + private Thread thread; + private boolean canRun; + + public MessageSiphon(InputStream stream, MessageConsumer consumer) { + this.streamReader = new BufferedReader(new InputStreamReader(stream)); + this.consumer = consumer; + this.canRun = true; + + thread = new Thread(this); + // don't set priority too low, otherwise exceptions won't + // bubble up in time (i.e. compile errors have a weird delay) + //thread.setPriority(Thread.MIN_PRIORITY); + thread.setPriority(Thread.MAX_PRIORITY - 1); + thread.start(); + } + + + public void run() { + try { + // process data until we hit EOF; this will happily block + // (effectively sleeping the thread) until new data comes in. + // when the program is finally done, null will come through. + // + String currentLine; + while (canRun && (currentLine = streamReader.readLine()) != null) { + // \n is added again because readLine() strips it out + //EditorConsole.systemOut.println("messaging in"); + consumer.message(currentLine + "\n"); + //EditorConsole.systemOut.println("messaging out"); + } + //EditorConsole.systemOut.println("messaging thread done"); + } catch (NullPointerException npe) { + // Fairly common exception during shutdown + } catch (SocketException e) { + // socket has been close while we were wainting for data. nothing to see here, move along + } catch (Exception e) { + // On Linux and sometimes on Mac OS X, a "bad file descriptor" + // message comes up when closing an applet that's run externally. + // That message just gets supressed here.. + String mess = e.getMessage(); + if ((mess != null) && + (mess.indexOf("Bad file descriptor") != -1)) { + //if (e.getMessage().indexOf("Bad file descriptor") == -1) { + //System.err.println("MessageSiphon err " + e); + //e.printStackTrace(); + } else { + e.printStackTrace(); + } + } finally { + thread = null; + } + } + + // Wait until the MessageSiphon thread is complete. + public void join() throws java.lang.InterruptedException { + // Grab a temp copy in case another thread nulls the "thread" + // member variable + Thread t = thread; + if (t != null) t.join(); + } + + public void stop() { + this.canRun = false; + } + +} diff --git a/tools/linux64/src/maple_loader/src/processing/app/debug/RunnerException.java b/tools/linux64/src/maple_loader/src/processing/app/debug/RunnerException.java new file mode 100644 index 0000000..0a67d1e --- /dev/null +++ b/tools/linux64/src/maple_loader/src/processing/app/debug/RunnerException.java @@ -0,0 +1,161 @@ +/* -*- mode: java; c-basic-offset: 2; indent-tabs-mode: nil -*- */ + +/* + Part of the Processing project - http://processing.org + + Copyright (c) 2004-08 Ben Fry and Casey Reas + Copyright (c) 2001-04 Massachusetts Institute of Technology + + 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 +*/ + +package processing.app.debug; + + +/** + * An exception with a line number attached that occurs + * during either compile time or run time. + */ +@SuppressWarnings("serial") +public class RunnerException extends Exception { + protected String message; + protected int codeIndex; + protected int codeLine; + protected int codeColumn; + protected boolean showStackTrace; + + + public RunnerException(String message) { + this(message, true); + } + + public RunnerException(String message, boolean showStackTrace) { + this(message, -1, -1, -1, showStackTrace); + } + + public RunnerException(String message, int file, int line) { + this(message, file, line, -1, true); + } + + + public RunnerException(String message, int file, int line, int column) { + this(message, file, line, column, true); + } + + + public RunnerException(String message, int file, int line, int column, + boolean showStackTrace) { + this.message = message; + this.codeIndex = file; + this.codeLine = line; + this.codeColumn = column; + this.showStackTrace = showStackTrace; + } + + + public RunnerException(Exception e) { + super(e); + this.showStackTrace = true; + } + + /** + * Override getMessage() in Throwable, so that I can set + * the message text outside the constructor. + */ + public String getMessage() { + return message; + } + + + public void setMessage(String message) { + this.message = message; + } + + + public int getCodeIndex() { + return codeIndex; + } + + + public void setCodeIndex(int index) { + codeIndex = index; + } + + + public boolean hasCodeIndex() { + return codeIndex != -1; + } + + + public int getCodeLine() { + return codeLine; + } + + + public void setCodeLine(int line) { + this.codeLine = line; + } + + + public boolean hasCodeLine() { + return codeLine != -1; + } + + + public void setCodeColumn(int column) { + this.codeColumn = column; + } + + + public int getCodeColumn() { + return codeColumn; + } + + + public void showStackTrace() { + showStackTrace = true; + } + + + public void hideStackTrace() { + showStackTrace = false; + } + + + /** + * Nix the java.lang crap out of an exception message + * because it scares the children. + *

+ * This function must be static to be used with super() + * in each of the constructors above. + */ + /* + static public final String massage(String msg) { + if (msg.indexOf("java.lang.") == 0) { + //int dot = msg.lastIndexOf('.'); + msg = msg.substring("java.lang.".length()); + } + return msg; + //return (dot == -1) ? msg : msg.substring(dot+1); + } + */ + + + public void printStackTrace() { + if (showStackTrace) { + super.printStackTrace(); + } + } +} diff --git a/tools/linux64/src/maple_loader/src/processing/app/helpers/ProcessUtils.java b/tools/linux64/src/maple_loader/src/processing/app/helpers/ProcessUtils.java new file mode 100644 index 0000000..c023f58 --- /dev/null +++ b/tools/linux64/src/maple_loader/src/processing/app/helpers/ProcessUtils.java @@ -0,0 +1,32 @@ +package processing.app.helpers; + +//import processing.app.Base; + +import java.io.IOException; +import java.util.Map; + +import processing.app.Base; + +public class ProcessUtils { + + public static Process exec(String[] command) throws IOException { + // No problems on linux and mac + if (!Base.isWindows()) { + return Runtime.getRuntime().exec(command); + } + + // Brutal hack to workaround windows command line parsing. + // http://stackoverflow.com/questions/5969724/java-runtime-exec-fails-to-escape-characters-properly + // http://msdn.microsoft.com/en-us/library/a1y7w461.aspx + // http://bugs.sun.com/view_bug.do?bug_id=6468220 + // http://bugs.sun.com/view_bug.do?bug_id=6518827 + String[] cmdLine = new String[command.length]; + for (int i = 0; i < command.length; i++) + cmdLine[i] = command[i].replace("\"", "\\\""); + + ProcessBuilder pb = new ProcessBuilder(cmdLine); + Map env = pb.environment(); + env.put("CYGWIN", "nodosfilewarning"); + return pb.start(); + } +} diff --git a/tools/linux64/src/stm32flash_serial/src/AUTHORS b/tools/linux64/src/stm32flash_serial/src/AUTHORS new file mode 100644 index 0000000..d096f22 --- /dev/null +++ b/tools/linux64/src/stm32flash_serial/src/AUTHORS @@ -0,0 +1,19 @@ +Authors ordered by first contribution. + +Geoffrey McRae +Bret Olmsted +Tormod Volden +Jakob Malm +Reuben Dowle +Matthias Kubisch +Paul Fertser +Daniel Strnad +Jérémie Rapin +Christian Pointner +Mats Erik Andersson +Alexey Borovik +Antonio Borneo +Armin van der Togt +Brian Silverman +Georg Hofmann +Luis Rodrigues diff --git a/tools/linux64/src/stm32flash_serial/src/Android.mk b/tools/linux64/src/stm32flash_serial/src/Android.mk new file mode 100644 index 0000000..7be3d00 --- /dev/null +++ b/tools/linux64/src/stm32flash_serial/src/Android.mk @@ -0,0 +1,20 @@ +TOP_LOCAL_PATH := $(call my-dir) + +include $(call all-named-subdir-makefiles, parsers) + +LOCAL_PATH := $(TOP_LOCAL_PATH) + +include $(CLEAR_VARS) +LOCAL_MODULE := stm32flash +LOCAL_SRC_FILES := \ + dev_table.c \ + i2c.c \ + init.c \ + main.c \ + port.c \ + serial_common.c \ + serial_platform.c \ + stm32.c \ + utils.c +LOCAL_STATIC_LIBRARIES := libparsers +include $(BUILD_EXECUTABLE) diff --git a/tools/linux64/src/stm32flash_serial/src/HOWTO b/tools/linux64/src/stm32flash_serial/src/HOWTO new file mode 100644 index 0000000..d8f32eb --- /dev/null +++ b/tools/linux64/src/stm32flash_serial/src/HOWTO @@ -0,0 +1,35 @@ +Add new interfaces: +===================================================================== +Current version 0.4 supports the following interfaces: +- UART Windows (either "COMn" and "\\.\COMn"); +- UART posix/Linux (e.g. "/dev/ttyUSB0"); +- I2C Linux through standard driver "i2c-dev" (e.g. "/dev/i2c-n"). + +Starting from version 0.4, the back-end of stm32flash is modular and +ready to be expanded to support new interfaces. +I'm planning adding SPI on Linux through standard driver "spidev". +You are invited to contribute with more interfaces. + +To add a new interface you need to add a new file, populate the struct +port_interface (check at the end of files i2c.c, serial_posix.c and +serial_w32.c) and provide the relative functions to operate on the +interface: open/close, read/write, get_cfg_str and the optional gpio. +The include the new drive in Makefile and register the new struct +port_interface in file port.c in struct port_interface *ports[]. + +There are several USB-I2C adapter in the market, each providing its +own libraries to communicate with the I2C bus. +Could be interesting to provide as back-end a bridge between stm32flash +and such libraries (I have no plan on this item). + + +Add new STM32 devices: +===================================================================== +Add a new line in file dev_table.c, in table devices[]. +The fields of the table are listed in stm32.h, struct stm32_dev. + + +Cross compile on Linux host for Windows target with MinGW: +===================================================================== +I'm using a 64 bit Arch Linux machines, and I usually run: + make CC=x86_64-w64-mingw32-gcc AR=x86_64-w64-mingw32-ar diff --git a/tools/linux64/src/stm32flash_serial/src/I2C.txt b/tools/linux64/src/stm32flash_serial/src/I2C.txt new file mode 100644 index 0000000..4c05ff6 --- /dev/null +++ b/tools/linux64/src/stm32flash_serial/src/I2C.txt @@ -0,0 +1,94 @@ +About I2C back-end communication in stm32flash +========================================================================== + +Starting from version v0.4, beside the serial communication port, +stm32flash adds support for I2C port to talk with STM32 bootloader. + +The current I2C back-end supports only the API provided by Linux kernel +driver "i2c-dev", so only I2C controllers with Linux kernel driver can be +used. +In Linux source code, most of the drivers for I2C and SMBUS controllers +are in + ./drivers/i2c/busses/ +Only I2C is supported by STM32 bootloader, so check the section below +about SMBUS. +No I2C support for Windows is available in stm32flash v0.4. + +Thanks to the new modular back-end, stm32flash can be easily extended to +support new back-ends and API. Check HOWTO file in stm32flash source code +for details. + +In the market there are several USB-to-I2C dongles; most of them are not +supported by kernel drivers. Manufacturer provide proprietary userspace +libraries using not standardized API. +These API and dongles could be supported in feature versions. + +There are currently 3 versions of STM32 bootloader for I2C communications: +- v1.0 using I2C clock stretching synchronization between host and STM32; +- v1.1 superset of v1.0, adds non stretching commands; +- v1.2 superset of v1.1, adds CRC command and compatibility with i2cdetect. +Details in ST application note AN2606. +All the bootloaders above are tested and working with stm32flash. + + +SMBUS controllers +========================================================================== + +Almost 50% of the drivers in Linux source code folder + ./drivers/i2c/busses/ +are for controllers that "only" support SMBUS protocol. They can NOT +operate with STM32 bootloader. +To identify if your controller supports I2C, use command: + i2cdetect -F n +where "n" is the number of the I2C interface (e.g. n=3 for "/dev/i2c-3"). +Controllers that supports I2C will report + I2C yes +Controller that support both I2C and SMBUS are ok. + +If you are interested on details about SMBUS protocol, you can download +the current specs from + http://smbus.org/specs/smbus20.pdf +and you can read the files in Linux source code + ./Documentation/i2c/i2c-protocol + ./Documentation/i2c/smbus-protocol + + +About bootloader v1.0 +========================================================================== + +Version v1.0 can have issues with some I2C controllers due to use of clock +stretching during commands that require long operations, like flash erase +and programming. + +Clock stretching is a technique to synchronize host and I2C device. When +I2C device wants to force a delay in the communication, it push "low" the +I2C clock; the I2C controller detects it and waits until I2C clock returns +"high". +Most I2C controllers set a "timeout" for clock stretching, ranging from +few milli-seconds to seconds depending on specific HW or SW driver. + +It is possible that the timeout in your I2C controller is smaller than the +delay required for flash erase or programming. In this case the I2C +controller will timeout and report error to stm32flash. +There is no possibility for stm32flash to retry, so it can only signal the +error and exit. + +To by-pass the issue with bootloader v1.0 you can modify the kernel driver +of your I2C controller. Not an easy job, since every controller has its own +way to handle the timeout. + +In my case I'm using the I2C controller integrated in the VGA port of my +laptop HP EliteBook 8460p. I built the 0.25$ VGA-to-I2C adapter reported in + http://www.paintyourdragon.com/?p=43 +To change the timeout of the I2C controller I had to modify the kernel file + drivers/gpu/drm/radeon/radeon_i2c.c +line 969 +- i2c->bit.timeout = usecs_to_jiffies(2200); /* from VESA */ ++ i2c->bit.timeout = msecs_to_jiffies(5000); /* 5s for STM32 */ +and recompile it. +Then + $> modprobe i2c-dev + $> chmod 666 /dev/i2c-7 + #> stm32flash -a 0x39 /dev/i2c-7 + +2014-09-16 Antonio Borneo diff --git a/tools/linux64/src/stm32flash_serial/src/Makefile b/tools/linux64/src/stm32flash_serial/src/Makefile new file mode 100644 index 0000000..0328d55 --- /dev/null +++ b/tools/linux64/src/stm32flash_serial/src/Makefile @@ -0,0 +1,38 @@ +PREFIX = /usr/local +CFLAGS += -Wall -g + +INSTALL = install + +OBJS = dev_table.o \ + i2c.o \ + init.o \ + main.o \ + port.o \ + serial_common.o \ + serial_platform.o \ + stm32.o \ + utils.o + +LIBOBJS = parsers/parsers.a + +all: stm32flash + +serial_platform.o: serial_posix.c serial_w32.c + +parsers/parsers.a: + cd parsers && $(MAKE) parsers.a + +stm32flash: $(OBJS) $(LIBOBJS) + $(CC) $(LDFLAGS) -o $@ $(OBJS) $(LIBOBJS) + +clean: + rm -f $(OBJS) stm32flash + cd parsers && $(MAKE) $@ + +install: all + $(INSTALL) -d $(DESTDIR)$(PREFIX)/bin + $(INSTALL) -m 755 stm32flash $(DESTDIR)$(PREFIX)/bin + $(INSTALL) -d $(DESTDIR)$(PREFIX)/share/man/man1 + $(INSTALL) -m 644 stm32flash.1 $(DESTDIR)$(PREFIX)/share/man/man1 + +.PHONY: all clean install diff --git a/tools/linux64/src/stm32flash_serial/src/TODO b/tools/linux64/src/stm32flash_serial/src/TODO new file mode 100644 index 0000000..41df614 --- /dev/null +++ b/tools/linux64/src/stm32flash_serial/src/TODO @@ -0,0 +1,7 @@ + +stm32: +- Add support for variable page size + +AUTHORS: +- Add contributors from Geoffrey's commits + diff --git a/tools/linux64/src/stm32flash_serial/src/dev_table.c b/tools/linux64/src/stm32flash_serial/src/dev_table.c new file mode 100644 index 0000000..399cd9d --- /dev/null +++ b/tools/linux64/src/stm32flash_serial/src/dev_table.c @@ -0,0 +1,70 @@ +/* + stm32flash - Open Source ST STM32 flash program for *nix + Copyright (C) 2010 Geoffrey McRae + Copyright (C) 2014 Antonio Borneo + + 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. +*/ + +#include "stm32.h" + +/* + * Device table, corresponds to the "Bootloader device-dependant parameters" + * table in ST document AN2606. + * Note that the option bytes upper range is inclusive! + */ +const stm32_dev_t devices[] = { + /* F0 */ + {0x440, "STM32F051xx" , 0x20001000, 0x20002000, 0x08000000, 0x08010000, 4, 1024, 0x1FFFF800, 0x1FFFF80B, 0x1FFFEC00, 0x1FFFF800}, + {0x444, "STM32F030/F031" , 0x20001000, 0x20002000, 0x08000000, 0x08010000, 4, 1024, 0x1FFFF800, 0x1FFFF80B, 0x1FFFEC00, 0x1FFFF800}, + {0x445, "STM32F042xx" , 0x20001800, 0x20001800, 0x08000000, 0x08008000, 4, 1024, 0x1FFFF800, 0x1FFFF80F, 0x1FFFC400, 0x1FFFF800}, + {0x448, "STM32F072xx" , 0x20001800, 0x20004000, 0x08000000, 0x08020000, 2, 2048, 0x1FFFF800, 0x1FFFF80F, 0x1FFFC800, 0x1FFFF800}, + /* F1 */ + {0x412, "Low-density" , 0x20000200, 0x20002800, 0x08000000, 0x08008000, 4, 1024, 0x1FFFF800, 0x1FFFF80F, 0x1FFFF000, 0x1FFFF800}, + {0x410, "Medium-density" , 0x20000200, 0x20005000, 0x08000000, 0x08020000, 4, 1024, 0x1FFFF800, 0x1FFFF80F, 0x1FFFF000, 0x1FFFF800}, + {0x414, "High-density" , 0x20000200, 0x20010000, 0x08000000, 0x08080000, 2, 2048, 0x1FFFF800, 0x1FFFF80F, 0x1FFFF000, 0x1FFFF800}, + {0x420, "Medium-density VL" , 0x20000200, 0x20002000, 0x08000000, 0x08020000, 4, 1024, 0x1FFFF800, 0x1FFFF80F, 0x1FFFF000, 0x1FFFF800}, + {0x428, "High-density VL" , 0x20000200, 0x20008000, 0x08000000, 0x08080000, 2, 2048, 0x1FFFF800, 0x1FFFF80F, 0x1FFFF000, 0x1FFFF800}, + {0x418, "Connectivity line" , 0x20001000, 0x20010000, 0x08000000, 0x08040000, 2, 2048, 0x1FFFF800, 0x1FFFF80F, 0x1FFFB000, 0x1FFFF800}, + {0x430, "XL-density" , 0x20000800, 0x20018000, 0x08000000, 0x08100000, 2, 2048, 0x1FFFF800, 0x1FFFF80F, 0x1FFFE000, 0x1FFFF800}, + /* Note that F2 and F4 devices have sectors of different page sizes + and only the first sectors (of one page size) are included here */ + /* F2 */ + {0x411, "STM32F2xx" , 0x20002000, 0x20020000, 0x08000000, 0x08100000, 4, 16384, 0x1FFFC000, 0x1FFFC00F, 0x1FFF0000, 0x1FFF77DF}, + /* F3 */ + {0x432, "STM32F373/8" , 0x20001400, 0x20008000, 0x08000000, 0x08040000, 2, 2048, 0x1FFFF800, 0x1FFFF80F, 0x1FFFD800, 0x1FFFF800}, + {0x422, "F302xB/303xB/358" , 0x20001400, 0x20010000, 0x08000000, 0x08040000, 2, 2048, 0x1FFFF800, 0x1FFFF80F, 0x1FFFD800, 0x1FFFF800}, + {0x439, "STM32F302x4(6/8)" , 0x20001800, 0x20004000, 0x08000000, 0x08040000, 2, 2048, 0x1FFFF800, 0x1FFFF80F, 0x1FFFD800, 0x1FFFF800}, + {0x438, "F303x4/334/328" , 0x20001800, 0x20003000, 0x08000000, 0x08040000, 2, 2048, 0x1FFFF800, 0x1FFFF80F, 0x1FFFD800, 0x1FFFF800}, + /* F4 */ + {0x413, "STM32F40/1" , 0x20002000, 0x20020000, 0x08000000, 0x08100000, 4, 16384, 0x1FFFC000, 0x1FFFC00F, 0x1FFF0000, 0x1FFF77DF}, + /* 0x419 is also used for STM32F429/39 but with other bootloader ID... */ + {0x419, "STM32F427/37" , 0x20002000, 0x20030000, 0x08000000, 0x08100000, 4, 16384, 0x1FFFC000, 0x1FFFC00F, 0x1FFF0000, 0x1FFF77FF}, + {0x423, "STM32F401xB(C)" , 0x20003000, 0x20010000, 0x08000000, 0x08100000, 4, 16384, 0x1FFFC000, 0x1FFFC00F, 0x1FFF0000, 0x1FFF77FF}, + {0x433, "STM32F401xD(E)" , 0x20003000, 0x20018000, 0x08000000, 0x08100000, 4, 16384, 0x1FFFC000, 0x1FFFC00F, 0x1FFF0000, 0x1FFF77FF}, + /* L0 */ + {0x417, "L05xxx/06xxx" , 0x20001000, 0x20002000, 0x08000000, 0x08010000, 32, 128, 0x1FF80000, 0x1FF8000F, 0x1FF00000, 0x1FF01000}, + /* L1 */ + {0x416, "L1xxx6(8/B)" , 0x20000800, 0x20004000, 0x08000000, 0x08020000, 16, 256, 0x1FF80000, 0x1FF8000F, 0x1FF00000, 0x1FF01000}, + {0x429, "L1xxx6(8/B)A" , 0x20001000, 0x20008000, 0x08000000, 0x08020000, 16, 256, 0x1FF80000, 0x1FF8000F, 0x1FF00000, 0x1FF01000}, + {0x427, "L1xxxC" , 0x20001000, 0x20008000, 0x08000000, 0x08020000, 16, 256, 0x1FF80000, 0x1FF8000F, 0x1FF00000, 0x1FF02000}, + {0x436, "L1xxxD" , 0x20001000, 0x2000C000, 0x08000000, 0x08060000, 16, 256, 0x1ff80000, 0x1ff8000F, 0x1FF00000, 0x1FF02000}, + {0x437, "L1xxxE" , 0x20001000, 0x20014000, 0x08000000, 0x08060000, 16, 256, 0x1ff80000, 0x1ff8000F, 0x1FF00000, 0x1FF02000}, + /* These are not (yet) in AN2606: */ + {0x641, "Medium_Density PL" , 0x20000200, 0x00005000, 0x08000000, 0x08020000, 4, 1024, 0x1FFFF800, 0x1FFFF80F, 0x1FFFF000, 0x1FFFF800}, + {0x9a8, "STM32W-128K" , 0x20000200, 0x20002000, 0x08000000, 0x08020000, 1, 1024, 0, 0, 0, 0}, + {0x9b0, "STM32W-256K" , 0x20000200, 0x20004000, 0x08000000, 0x08040000, 1, 2048, 0, 0, 0, 0}, + {0x0} +}; diff --git a/tools/linux64/src/stm32flash_serial/src/gpl-2.0.txt b/tools/linux64/src/stm32flash_serial/src/gpl-2.0.txt new file mode 100644 index 0000000..d159169 --- /dev/null +++ b/tools/linux64/src/stm32flash_serial/src/gpl-2.0.txt @@ -0,0 +1,339 @@ + GNU GENERAL PUBLIC LICENSE + Version 2, June 1991 + + Copyright (C) 1989, 1991 Free Software Foundation, Inc., + 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 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 Lesser 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. + + + Copyright (C) + + 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., + 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 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. + + , 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 Lesser General +Public License instead of this License. diff --git a/tools/linux64/src/stm32flash_serial/src/i2c.c b/tools/linux64/src/stm32flash_serial/src/i2c.c new file mode 100644 index 0000000..10e6bb1 --- /dev/null +++ b/tools/linux64/src/stm32flash_serial/src/i2c.c @@ -0,0 +1,209 @@ +/* + stm32flash - Open Source ST STM32 flash program for *nix + Copyright (C) 2014 Antonio Borneo + + 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. +*/ + + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "serial.h" +#include "port.h" + + +#if !defined(__linux__) + +static port_err_t i2c_open(struct port_interface *port, + struct port_options *ops) +{ + return PORT_ERR_NODEV; +} + +struct port_interface port_i2c = { + .name = "i2c", + .open = i2c_open, +}; + +#else + +#ifdef __ANDROID__ +#define I2C_SLAVE 0x0703 /* Use this slave address */ +#define I2C_FUNCS 0x0705 /* Get the adapter functionality mask */ +/* To determine what functionality is present */ +#define I2C_FUNC_I2C 0x00000001 +#else +#include +#include +#endif + +#include + +struct i2c_priv { + int fd; + int addr; +}; + +static port_err_t i2c_open(struct port_interface *port, + struct port_options *ops) +{ + struct i2c_priv *h; + int fd, addr, ret; + unsigned long funcs; + + /* 1. check device name match */ + if (strncmp(ops->device, "/dev/i2c-", strlen("/dev/i2c-"))) + return PORT_ERR_NODEV; + + /* 2. check options */ + addr = ops->bus_addr; + if (addr < 0x03 || addr > 0x77) { + fprintf(stderr, "I2C address out of range [0x03-0x77]\n"); + return PORT_ERR_UNKNOWN; + } + + /* 3. open it */ + h = calloc(sizeof(*h), 1); + if (h == NULL) { + fprintf(stderr, "End of memory\n"); + return PORT_ERR_UNKNOWN; + } + fd = open(ops->device, O_RDWR); + if (fd < 0) { + fprintf(stderr, "Unable to open special file \"%s\"\n", + ops->device); + free(h); + return PORT_ERR_UNKNOWN; + } + + /* 3.5. Check capabilities */ + ret = ioctl(fd, I2C_FUNCS, &funcs); + if (ret < 0) { + fprintf(stderr, "I2C ioctl(funcs) error %d\n", errno); + close(fd); + free(h); + return PORT_ERR_UNKNOWN; + } + if ((funcs & I2C_FUNC_I2C) == 0) { + fprintf(stderr, "Error: controller is not I2C, only SMBUS.\n"); + close(fd); + free(h); + return PORT_ERR_UNKNOWN; + } + + /* 4. set options */ + ret = ioctl(fd, I2C_SLAVE, addr); + if (ret < 0) { + fprintf(stderr, "I2C ioctl(slave) error %d\n", errno); + close(fd); + free(h); + return PORT_ERR_UNKNOWN; + } + + h->fd = fd; + h->addr = addr; + port->private = h; + return PORT_ERR_OK; +} + +static port_err_t i2c_close(struct port_interface *port) +{ + struct i2c_priv *h; + + h = (struct i2c_priv *)port->private; + if (h == NULL) + return PORT_ERR_UNKNOWN; + close(h->fd); + free(h); + port->private = NULL; + return PORT_ERR_OK; +} + +static port_err_t i2c_read(struct port_interface *port, void *buf, + size_t nbyte) +{ + struct i2c_priv *h; + int ret; + + h = (struct i2c_priv *)port->private; + if (h == NULL) + return PORT_ERR_UNKNOWN; + ret = read(h->fd, buf, nbyte); + if (ret != nbyte) + return PORT_ERR_UNKNOWN; + return PORT_ERR_OK; +} + +static port_err_t i2c_write(struct port_interface *port, void *buf, + size_t nbyte) +{ + struct i2c_priv *h; + int ret; + + h = (struct i2c_priv *)port->private; + if (h == NULL) + return PORT_ERR_UNKNOWN; + ret = write(h->fd, buf, nbyte); + if (ret != nbyte) + return PORT_ERR_UNKNOWN; + return PORT_ERR_OK; +} + +static port_err_t i2c_gpio(struct port_interface *port, serial_gpio_t n, + int level) +{ + return PORT_ERR_OK; +} + +static const char *i2c_get_cfg_str(struct port_interface *port) +{ + struct i2c_priv *h; + static char str[11]; + + h = (struct i2c_priv *)port->private; + if (h == NULL) + return "INVALID"; + snprintf(str, sizeof(str), "addr 0x%2x", h->addr); + return str; +} + +static struct varlen_cmd i2c_cmd_get_reply[] = { + {0x10, 11}, + {0x11, 17}, + {0x12, 18}, + { /* sentinel */ } +}; + +struct port_interface port_i2c = { + .name = "i2c", + .flags = PORT_STRETCH_W, + .open = i2c_open, + .close = i2c_close, + .read = i2c_read, + .write = i2c_write, + .gpio = i2c_gpio, + .cmd_get_reply = i2c_cmd_get_reply, + .get_cfg_str = i2c_get_cfg_str, +}; + +#endif diff --git a/tools/linux64/src/stm32flash_serial/src/init.c b/tools/linux64/src/stm32flash_serial/src/init.c new file mode 100644 index 0000000..77a571b --- /dev/null +++ b/tools/linux64/src/stm32flash_serial/src/init.c @@ -0,0 +1,219 @@ +/* + stm32flash - Open Source ST STM32 flash program for *nix + Copyright (C) 2010 Geoffrey McRae + Copyright (C) 2013 Antonio Borneo + + 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. +*/ + + +#include +#include +#include +#include +#include +#include +#include +#include +#include "init.h" +#include "serial.h" +#include "stm32.h" +#include "port.h" + +struct gpio_list { + struct gpio_list *next; + int gpio; +}; + + +static int write_to(const char *filename, const char *value) +{ + int fd, ret; + + fd = open(filename, O_WRONLY); + if (fd < 0) { + fprintf(stderr, "Cannot open file \"%s\"\n", filename); + return 0; + } + ret = write(fd, value, strlen(value)); + if (ret < 0) { + fprintf(stderr, "Error writing in file \"%s\"\n", filename); + close(fd); + return 0; + } + close(fd); + return 1; +} + +#if !defined(__linux__) +static int drive_gpio(int n, int level, struct gpio_list **gpio_to_release) +{ + fprintf(stderr, "GPIO control only available in Linux\n"); + return 0; +} +#else +static int drive_gpio(int n, int level, struct gpio_list **gpio_to_release) +{ + char num[16]; /* sized to carry MAX_INT */ + char file[48]; /* sized to carry longest filename */ + struct stat buf; + struct gpio_list *new; + int ret; + + sprintf(file, "/sys/class/gpio/gpio%d/direction", n); + ret = stat(file, &buf); + if (ret) { + /* file miss, GPIO not exported yet */ + sprintf(num, "%d", n); + ret = write_to("/sys/class/gpio/export", num); + if (!ret) + return 0; + ret = stat(file, &buf); + if (ret) { + fprintf(stderr, "GPIO %d not available\n", n); + return 0; + } + new = (struct gpio_list *)malloc(sizeof(struct gpio_list)); + if (new == NULL) { + fprintf(stderr, "Out of memory\n"); + return 0; + } + new->gpio = n; + new->next = *gpio_to_release; + *gpio_to_release = new; + } + + return write_to(file, level ? "high" : "low"); +} +#endif + +static int release_gpio(int n) +{ + char num[16]; /* sized to carry MAX_INT */ + + sprintf(num, "%d", n); + return write_to("/sys/class/gpio/unexport", num); +} + +static int gpio_sequence(struct port_interface *port, const char *s, size_t l) +{ + struct gpio_list *gpio_to_release = NULL, *to_free; + int ret, level, gpio; + + ret = 1; + while (ret == 1 && *s && l > 0) { + if (*s == '-') { + level = 0; + s++; + l--; + } else + level = 1; + + if (isdigit(*s)) { + gpio = atoi(s); + while (isdigit(*s)) { + s++; + l--; + } + } else if (!strncmp(s, "rts", 3)) { + gpio = -GPIO_RTS; + s += 3; + l -= 3; + } else if (!strncmp(s, "dtr", 3)) { + gpio = -GPIO_DTR; + s += 3; + l -= 3; + } else if (!strncmp(s, "brk", 3)) { + gpio = -GPIO_BRK; + s += 3; + l -= 3; + } else { + fprintf(stderr, "Character \'%c\' is not a digit\n", *s); + ret = 0; + break; + } + + if (*s && (l > 0)) { + if (*s == ',') { + s++; + l--; + } else { + fprintf(stderr, "Character \'%c\' is not a separator\n", *s); + ret = 0; + break; + } + } + if (gpio < 0) + ret = (port->gpio(port, -gpio, level) == PORT_ERR_OK); + else + ret = drive_gpio(gpio, level, &gpio_to_release); + usleep(100000); + } + + while (gpio_to_release) { + release_gpio(gpio_to_release->gpio); + to_free = gpio_to_release; + gpio_to_release = gpio_to_release->next; + free(to_free); + } + usleep(500000); + return ret; +} + +static int gpio_bl_entry(struct port_interface *port, const char *seq) +{ + char *s; + + if (seq == NULL || seq[0] == ':') + return 1; + + s = strchr(seq, ':'); + if (s == NULL) + return gpio_sequence(port, seq, strlen(seq)); + + return gpio_sequence(port, seq, s - seq); +} + +static int gpio_bl_exit(struct port_interface *port, const char *seq) +{ + char *s; + + if (seq == NULL) + return 1; + + s = strchr(seq, ':'); + if (s == NULL || s[1] == '\0') + return 1; + + return gpio_sequence(port, s + 1, strlen(s + 1)); +} + +int init_bl_entry(struct port_interface *port, const char *seq) +{ + if (seq) + return gpio_bl_entry(port, seq); + + return 1; +} + +int init_bl_exit(stm32_t *stm, struct port_interface *port, const char *seq) +{ + if (seq) + return gpio_bl_exit(port, seq); + + if (stm32_reset_device(stm) != STM32_ERR_OK) + return 0; + return 1; +} diff --git a/tools/linux64/src/stm32flash_serial/src/init.h b/tools/linux64/src/stm32flash_serial/src/init.h new file mode 100644 index 0000000..6075b51 --- /dev/null +++ b/tools/linux64/src/stm32flash_serial/src/init.h @@ -0,0 +1,31 @@ +/* + stm32flash - Open Source ST STM32 flash program for *nix + Copyright (C) 2010 Geoffrey McRae + Copyright (C) 2013 Antonio Borneo + + 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. +*/ + + +#ifndef _INIT_H +#define _INIT_H + +#include "stm32.h" +#include "port.h" + +int init_bl_entry(struct port_interface *port, const char *seq); +int init_bl_exit(stm32_t *stm, struct port_interface *port, const char *seq); + +#endif diff --git a/tools/linux64/src/stm32flash_serial/src/main.c b/tools/linux64/src/stm32flash_serial/src/main.c new file mode 100644 index 0000000..f081d61 --- /dev/null +++ b/tools/linux64/src/stm32flash_serial/src/main.c @@ -0,0 +1,774 @@ +/* + stm32flash - Open Source ST STM32 flash program for *nix + Copyright 2010 Geoffrey McRae + Copyright 2011 Steve Markgraf + Copyright 2012 Tormod Volden + Copyright 2013 Antonio Borneo + + 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. +*/ + +#include +#include +#include +#include +#include +#include +#include + +#include "init.h" +#include "utils.h" +#include "serial.h" +#include "stm32.h" +#include "parsers/parser.h" +#include "port.h" + +#include "parsers/binary.h" +#include "parsers/hex.h" + +#define VERSION "Arduino_STM32_0.9" + +/* device globals */ +stm32_t *stm = NULL; + +void *p_st = NULL; +parser_t *parser = NULL; + +/* settings */ +struct port_options port_opts = { + .device = NULL, + .baudRate = SERIAL_BAUD_57600, + .serial_mode = "8e1", + .bus_addr = 0, + .rx_frame_max = STM32_MAX_RX_FRAME, + .tx_frame_max = STM32_MAX_TX_FRAME, +}; +int rd = 0; +int wr = 0; +int wu = 0; +int rp = 0; +int ur = 0; +int eraseOnly = 0; +int crc = 0; +int npages = 0; +int spage = 0; +int no_erase = 0; +char verify = 0; +int retry = 10; +char exec_flag = 0; +uint32_t execute = 0; +char init_flag = 1; +char force_binary = 0; +char reset_flag = 0; +char *filename; +char *gpio_seq = NULL; +uint32_t start_addr = 0; +uint32_t readwrite_len = 0; + +/* functions */ +int parse_options(int argc, char *argv[]); +void show_help(char *name); + +static int is_addr_in_ram(uint32_t addr) +{ + return addr >= stm->dev->ram_start && addr < stm->dev->ram_end; +} + +static int is_addr_in_flash(uint32_t addr) +{ + return addr >= stm->dev->fl_start && addr < stm->dev->fl_end; +} + +static int flash_addr_to_page_floor(uint32_t addr) +{ + if (!is_addr_in_flash(addr)) + return 0; + + return (addr - stm->dev->fl_start) / stm->dev->fl_ps; +} + +static int flash_addr_to_page_ceil(uint32_t addr) +{ + if (!(addr >= stm->dev->fl_start && addr <= stm->dev->fl_end)) + return 0; + + return (addr + stm->dev->fl_ps - 1 - stm->dev->fl_start) + / stm->dev->fl_ps; +} + +static uint32_t flash_page_to_addr(int page) +{ + return stm->dev->fl_start + page * stm->dev->fl_ps; +} + +int main(int argc, char* argv[]) { + struct port_interface *port = NULL; + int ret = 1; + stm32_err_t s_err; + parser_err_t perr; + FILE *diag = stdout; + + fprintf(diag, "stm32flash " VERSION "\n\n"); + fprintf(diag, "http://github.com/rogerclarkmelbourne/arduino_stm32\n\n"); + if (parse_options(argc, argv) != 0) + goto close; + + if (rd && filename[0] == '-') { + diag = stderr; + } + + if (wr) { + /* first try hex */ + if (!force_binary) { + parser = &PARSER_HEX; + p_st = parser->init(); + if (!p_st) { + fprintf(stderr, "%s Parser failed to initialize\n", parser->name); + goto close; + } + } + + if (force_binary || (perr = parser->open(p_st, filename, 0)) != PARSER_ERR_OK) { + if (force_binary || perr == PARSER_ERR_INVALID_FILE) { + if (!force_binary) { + parser->close(p_st); + p_st = NULL; + } + + /* now try binary */ + parser = &PARSER_BINARY; + p_st = parser->init(); + if (!p_st) { + fprintf(stderr, "%s Parser failed to initialize\n", parser->name); + goto close; + } + perr = parser->open(p_st, filename, 0); + } + + /* if still have an error, fail */ + if (perr != PARSER_ERR_OK) { + fprintf(stderr, "%s ERROR: %s\n", parser->name, parser_errstr(perr)); + if (perr == PARSER_ERR_SYSTEM) perror(filename); + goto close; + } + } + + fprintf(diag, "Using Parser : %s\n", parser->name); + } else { + parser = &PARSER_BINARY; + p_st = parser->init(); + if (!p_st) { + fprintf(stderr, "%s Parser failed to initialize\n", parser->name); + goto close; + } + } + + if (port_open(&port_opts, &port) != PORT_ERR_OK) { + fprintf(stderr, "Failed to open port: %s\n", port_opts.device); + goto close; + } + + fprintf(diag, "Interface %s: %s\n", port->name, port->get_cfg_str(port)); + if (init_flag && init_bl_entry(port, gpio_seq) == 0) + goto close; + stm = stm32_init(port, init_flag); + if (!stm) + goto close; + + fprintf(diag, "Version : 0x%02x\n", stm->bl_version); + if (port->flags & PORT_GVR_ETX) { + fprintf(diag, "Option 1 : 0x%02x\n", stm->option1); + fprintf(diag, "Option 2 : 0x%02x\n", stm->option2); + } + fprintf(diag, "Device ID : 0x%04x (%s)\n", stm->pid, stm->dev->name); + fprintf(diag, "- RAM : %dKiB (%db reserved by bootloader)\n", (stm->dev->ram_end - 0x20000000) / 1024, stm->dev->ram_start - 0x20000000); + fprintf(diag, "- Flash : %dKiB (sector size: %dx%d)\n", (stm->dev->fl_end - stm->dev->fl_start ) / 1024, stm->dev->fl_pps, stm->dev->fl_ps); + fprintf(diag, "- Option RAM : %db\n", stm->dev->opt_end - stm->dev->opt_start + 1); + fprintf(diag, "- System RAM : %dKiB\n", (stm->dev->mem_end - stm->dev->mem_start) / 1024); + + uint8_t buffer[256]; + uint32_t addr, start, end; + unsigned int len; + int failed = 0; + int first_page, num_pages; + + /* + * Cleanup addresses: + * + * Starting from options + * start_addr, readwrite_len, spage, npages + * and using device memory size, compute + * start, end, first_page, num_pages + */ + if (start_addr || readwrite_len) { + start = start_addr; + + if (is_addr_in_flash(start)) + end = stm->dev->fl_end; + else { + no_erase = 1; + if (is_addr_in_ram(start)) + end = stm->dev->ram_end; + else + end = start + sizeof(uint32_t); + } + + if (readwrite_len && (end > start + readwrite_len)) + end = start + readwrite_len; + + first_page = flash_addr_to_page_floor(start); + if (!first_page && end == stm->dev->fl_end) + num_pages = 0xff; /* mass erase */ + else + num_pages = flash_addr_to_page_ceil(end) - first_page; + } else if (!spage && !npages) { + start = stm->dev->fl_start; + end = stm->dev->fl_end; + first_page = 0; + num_pages = 0xff; /* mass erase */ + } else { + first_page = spage; + start = flash_page_to_addr(first_page); + if (start > stm->dev->fl_end) { + fprintf(stderr, "Address range exceeds flash size.\n"); + goto close; + } + + if (npages) { + num_pages = npages; + end = flash_page_to_addr(first_page + num_pages); + if (end > stm->dev->fl_end) + end = stm->dev->fl_end; + } else { + end = stm->dev->fl_end; + num_pages = flash_addr_to_page_ceil(end) - first_page; + } + + if (!first_page && end == stm->dev->fl_end) + num_pages = 0xff; /* mass erase */ + } + + if (rd) { + unsigned int max_len = port_opts.rx_frame_max; + + fprintf(diag, "Memory read\n"); + + perr = parser->open(p_st, filename, 1); + if (perr != PARSER_ERR_OK) { + fprintf(stderr, "%s ERROR: %s\n", parser->name, parser_errstr(perr)); + if (perr == PARSER_ERR_SYSTEM) + perror(filename); + goto close; + } + + fflush(diag); + addr = start; + while(addr < end) { + uint32_t left = end - addr; + len = max_len > left ? left : max_len; + s_err = stm32_read_memory(stm, addr, buffer, len); + if (s_err != STM32_ERR_OK) { + fprintf(stderr, "Failed to read memory at address 0x%08x, target write-protected?\n", addr); + goto close; + } + if (parser->write(p_st, buffer, len) != PARSER_ERR_OK) + { + fprintf(stderr, "Failed to write data to file\n"); + goto close; + } + addr += len; + + fprintf(diag, + "\rRead address 0x%08x (%.2f%%) ", + addr, + (100.0f / (float)(end - start)) * (float)(addr - start) + ); + fflush(diag); + } + fprintf(diag, "Done.\n"); + ret = 0; + goto close; + } else if (rp) { + fprintf(stdout, "Read-Protecting flash\n"); + /* the device automatically performs a reset after the sending the ACK */ + reset_flag = 0; + stm32_readprot_memory(stm); + fprintf(stdout, "Done.\n"); + } else if (ur) { + fprintf(stdout, "Read-UnProtecting flash\n"); + /* the device automatically performs a reset after the sending the ACK */ + reset_flag = 0; + stm32_runprot_memory(stm); + fprintf(stdout, "Done.\n"); + } else if (eraseOnly) { + ret = 0; + fprintf(stdout, "Erasing flash\n"); + + if (num_pages != 0xff && + (start != flash_page_to_addr(first_page) + || end != flash_page_to_addr(first_page + num_pages))) { + fprintf(stderr, "Specified start & length are invalid (must be page aligned)\n"); + ret = 1; + goto close; + } + + s_err = stm32_erase_memory(stm, first_page, num_pages); + if (s_err != STM32_ERR_OK) { + fprintf(stderr, "Failed to erase memory\n"); + ret = 1; + goto close; + } + } else if (wu) { + fprintf(diag, "Write-unprotecting flash\n"); + /* the device automatically performs a reset after the sending the ACK */ + reset_flag = 0; + stm32_wunprot_memory(stm); + fprintf(diag, "Done.\n"); + + } else if (wr) { + fprintf(diag, "Write to memory\n"); + + off_t offset = 0; + ssize_t r; + unsigned int size; + unsigned int max_wlen, max_rlen; + + max_wlen = port_opts.tx_frame_max - 2; /* skip len and crc */ + max_wlen &= ~3; /* 32 bit aligned */ + + max_rlen = port_opts.rx_frame_max; + max_rlen = max_rlen < max_wlen ? max_rlen : max_wlen; + + /* Assume data from stdin is whole device */ + if (filename[0] == '-' && filename[1] == '\0') + size = end - start; + else + size = parser->size(p_st); + + // TODO: It is possible to write to non-page boundaries, by reading out flash + // from partial pages and combining with the input data + // if ((start % stm->dev->fl_ps) != 0 || (end % stm->dev->fl_ps) != 0) { + // fprintf(stderr, "Specified start & length are invalid (must be page aligned)\n"); + // goto close; + // } + + // TODO: If writes are not page aligned, we should probably read out existing flash + // contents first, so it can be preserved and combined with new data + if (!no_erase && num_pages) { + fprintf(diag, "Erasing memory\n"); + s_err = stm32_erase_memory(stm, first_page, num_pages); + if (s_err != STM32_ERR_OK) { + fprintf(stderr, "Failed to erase memory\n"); + goto close; + } + } + + fflush(diag); + addr = start; + while(addr < end && offset < size) { + uint32_t left = end - addr; + len = max_wlen > left ? left : max_wlen; + len = len > size - offset ? size - offset : len; + + if (parser->read(p_st, buffer, &len) != PARSER_ERR_OK) + goto close; + + if (len == 0) { + if (filename[0] == '-') { + break; + } else { + fprintf(stderr, "Failed to read input file\n"); + goto close; + } + } + + again: + s_err = stm32_write_memory(stm, addr, buffer, len); + if (s_err != STM32_ERR_OK) { + fprintf(stderr, "Failed to write memory at address 0x%08x\n", addr); + goto close; + } + + if (verify) { + uint8_t compare[len]; + unsigned int offset, rlen; + + offset = 0; + while (offset < len) { + rlen = len - offset; + rlen = rlen < max_rlen ? rlen : max_rlen; + s_err = stm32_read_memory(stm, addr + offset, compare + offset, rlen); + if (s_err != STM32_ERR_OK) { + fprintf(stderr, "Failed to read memory at address 0x%08x\n", addr + offset); + goto close; + } + offset += rlen; + } + + for(r = 0; r < len; ++r) + if (buffer[r] != compare[r]) { + if (failed == retry) { + fprintf(stderr, "Failed to verify at address 0x%08x, expected 0x%02x and found 0x%02x\n", + (uint32_t)(addr + r), + buffer [r], + compare[r] + ); + goto close; + } + ++failed; + goto again; + } + + failed = 0; + } + + addr += len; + offset += len; + + fprintf(diag, + "\rWrote %saddress 0x%08x (%.2f%%) ", + verify ? "and verified " : "", + addr, + (100.0f / size) * offset + ); + fflush(diag); + + } + + fprintf(diag, "Done.\n"); + ret = 0; + goto close; + } else if (crc) { + uint32_t crc_val = 0; + + fprintf(diag, "CRC computation\n"); + + s_err = stm32_crc_wrapper(stm, start, end - start, &crc_val); + if (s_err != STM32_ERR_OK) { + fprintf(stderr, "Failed to read CRC\n"); + goto close; + } + fprintf(diag, "CRC(0x%08x-0x%08x) = 0x%08x\n", start, end, + crc_val); + ret = 0; + goto close; + } else + ret = 0; + +close: + if (stm && exec_flag && ret == 0) { + if (execute == 0) + execute = stm->dev->fl_start; + + fprintf(diag, "\nStarting execution at address 0x%08x... ", execute); + fflush(diag); + if (stm32_go(stm, execute) == STM32_ERR_OK) { + reset_flag = 0; + fprintf(diag, "done.\n"); + } else + fprintf(diag, "failed.\n"); + } + + if (stm && reset_flag) { + fprintf(diag, "\nResetting device... "); + fflush(diag); + if (init_bl_exit(stm, port, gpio_seq)) + fprintf(diag, "done.\n"); + else fprintf(diag, "failed.\n"); + } + + if (p_st ) parser->close(p_st); + if (stm ) stm32_close (stm); + if (port) + port->close(port); + + fprintf(diag, "\n"); + return ret; +} + +int parse_options(int argc, char *argv[]) +{ + int c; + char *pLen; + + while ((c = getopt(argc, argv, "a:b:m:r:w:e:vn:g:jkfcChuos:S:F:i:R")) != -1) { + switch(c) { + case 'a': + port_opts.bus_addr = strtoul(optarg, NULL, 0); + break; + + case 'b': + port_opts.baudRate = serial_get_baud(strtoul(optarg, NULL, 0)); + if (port_opts.baudRate == SERIAL_BAUD_INVALID) { + serial_baud_t baudrate; + fprintf(stderr, "Invalid baud rate, valid options are:\n"); + for (baudrate = SERIAL_BAUD_1200; baudrate != SERIAL_BAUD_INVALID; ++baudrate) + fprintf(stderr, " %d\n", serial_get_baud_int(baudrate)); + return 1; + } + break; + + case 'm': + if (strlen(optarg) != 3 + || serial_get_bits(optarg) == SERIAL_BITS_INVALID + || serial_get_parity(optarg) == SERIAL_PARITY_INVALID + || serial_get_stopbit(optarg) == SERIAL_STOPBIT_INVALID) { + fprintf(stderr, "Invalid serial mode\n"); + return 1; + } + port_opts.serial_mode = optarg; + break; + + case 'r': + case 'w': + rd = rd || c == 'r'; + wr = wr || c == 'w'; + if (rd && wr) { + fprintf(stderr, "ERROR: Invalid options, can't read & write at the same time\n"); + return 1; + } + filename = optarg; + if (filename[0] == '-') { + force_binary = 1; + } + break; + case 'e': + if (readwrite_len || start_addr) { + fprintf(stderr, "ERROR: Invalid options, can't specify start page / num pages and start address/length\n"); + return 1; + } + npages = strtoul(optarg, NULL, 0); + if (npages > 0xFF || npages < 0) { + fprintf(stderr, "ERROR: You need to specify a page count between 0 and 255"); + return 1; + } + if (!npages) + no_erase = 1; + break; + case 'u': + wu = 1; + if (rd || wr) { + fprintf(stderr, "ERROR: Invalid options, can't write unprotect and read/write at the same time\n"); + return 1; + } + break; + + case 'j': + rp = 1; + if (rd || wr) { + fprintf(stderr, "ERROR: Invalid options, can't read protect and read/write at the same time\n"); + return 1; + } + break; + + case 'k': + ur = 1; + if (rd || wr) { + fprintf(stderr, "ERROR: Invalid options, can't read unprotect and read/write at the same time\n"); + return 1; + } + break; + + case 'o': + eraseOnly = 1; + if (rd || wr) { + fprintf(stderr, "ERROR: Invalid options, can't erase-only and read/write at the same time\n"); + return 1; + } + break; + + case 'v': + verify = 1; + break; + + case 'n': + retry = strtoul(optarg, NULL, 0); + break; + + case 'g': + exec_flag = 1; + execute = strtoul(optarg, NULL, 0); + if (execute % 4 != 0) { + fprintf(stderr, "ERROR: Execution address must be word-aligned\n"); + return 1; + } + break; + case 's': + if (readwrite_len || start_addr) { + fprintf(stderr, "ERROR: Invalid options, can't specify start page / num pages and start address/length\n"); + return 1; + } + spage = strtoul(optarg, NULL, 0); + break; + case 'S': + if (spage || npages) { + fprintf(stderr, "ERROR: Invalid options, can't specify start page / num pages and start address/length\n"); + return 1; + } else { + start_addr = strtoul(optarg, &pLen, 0); + if (*pLen == ':') { + pLen++; + readwrite_len = strtoul(pLen, NULL, 0); + if (readwrite_len == 0) { + fprintf(stderr, "ERROR: Invalid options, can't specify zero length\n"); + return 1; + } + } + } + break; + case 'F': + port_opts.rx_frame_max = strtoul(optarg, &pLen, 0); + if (*pLen == ':') { + pLen++; + port_opts.tx_frame_max = strtoul(pLen, NULL, 0); + } + if (port_opts.rx_frame_max < 0 + || port_opts.tx_frame_max < 0) { + fprintf(stderr, "ERROR: Invalid negative value for option -F\n"); + return 1; + } + if (port_opts.rx_frame_max == 0) + port_opts.rx_frame_max = STM32_MAX_RX_FRAME; + if (port_opts.tx_frame_max == 0) + port_opts.tx_frame_max = STM32_MAX_TX_FRAME; + if (port_opts.rx_frame_max < 20 + || port_opts.tx_frame_max < 5) { + fprintf(stderr, "ERROR: current code cannot work with small frames.\n"); + fprintf(stderr, "min(RX) = 20, min(TX) = 5\n"); + return 1; + } + if (port_opts.rx_frame_max > STM32_MAX_RX_FRAME) { + fprintf(stderr, "WARNING: Ignore RX length in option -F\n"); + port_opts.rx_frame_max = STM32_MAX_RX_FRAME; + } + if (port_opts.tx_frame_max > STM32_MAX_TX_FRAME) { + fprintf(stderr, "WARNING: Ignore TX length in option -F\n"); + port_opts.tx_frame_max = STM32_MAX_TX_FRAME; + } + break; + case 'f': + force_binary = 1; + break; + + case 'c': + init_flag = 0; + break; + + case 'h': + show_help(argv[0]); + exit(0); + + case 'i': + gpio_seq = optarg; + break; + + case 'R': + reset_flag = 1; + break; + + case 'C': + crc = 1; + break; + } + } + + for (c = optind; c < argc; ++c) { + if (port_opts.device) { + fprintf(stderr, "ERROR: Invalid parameter specified\n"); + show_help(argv[0]); + return 1; + } + port_opts.device = argv[c]; + } + + if (port_opts.device == NULL) { + fprintf(stderr, "ERROR: Device not specified\n"); + show_help(argv[0]); + return 1; + } + + if (!wr && verify) { + fprintf(stderr, "ERROR: Invalid usage, -v is only valid when writing\n"); + show_help(argv[0]); + return 1; + } + + return 0; +} + +void show_help(char *name) { + fprintf(stderr, + "Usage: %s [-bvngfhc] [-[rw] filename] [tty_device | i2c_device]\n" + " -a bus_address Bus address (e.g. for I2C port)\n" + " -b rate Baud rate (default 57600)\n" + " -m mode Serial port mode (default 8e1)\n" + " -r filename Read flash to file (or - stdout)\n" + " -w filename Write flash from file (or - stdout)\n" + " -C Compute CRC of flash content\n" + " -u Disable the flash write-protection\n" + " -j Enable the flash read-protection\n" + " -k Disable the flash read-protection\n" + " -o Erase only\n" + " -e n Only erase n pages before writing the flash\n" + " -v Verify writes\n" + " -n count Retry failed writes up to count times (default 10)\n" + " -g address Start execution at specified address (0 = flash start)\n" + " -S address[:length] Specify start address and optionally length for\n" + " read/write/erase operations\n" + " -F RX_length[:TX_length] Specify the max length of RX and TX frame\n" + " -s start_page Flash at specified page (0 = flash start)\n" + " -f Force binary parser\n" + " -h Show this help\n" + " -c Resume the connection (don't send initial INIT)\n" + " *Baud rate must be kept the same as the first init*\n" + " This is useful if the reset fails\n" + " -i GPIO_string GPIO sequence to enter/exit bootloader mode\n" + " GPIO_string=[entry_seq][:[exit_seq]]\n" + " sequence=[-]n[,sequence]\n" + " -R Reset device at exit.\n" + "\n" + "Examples:\n" + " Get device information:\n" + " %s /dev/ttyS0\n" + " or:\n" + " %s /dev/i2c-0\n" + "\n" + " Write with verify and then start execution:\n" + " %s -w filename -v -g 0x0 /dev/ttyS0\n" + "\n" + " Read flash to file:\n" + " %s -r filename /dev/ttyS0\n" + "\n" + " Read 100 bytes of flash from 0x1000 to stdout:\n" + " %s -r - -S 0x1000:100 /dev/ttyS0\n" + "\n" + " Start execution:\n" + " %s -g 0x0 /dev/ttyS0\n" + "\n" + " GPIO sequence:\n" + " - entry sequence: GPIO_3=low, GPIO_2=low, GPIO_2=high\n" + " - exit sequence: GPIO_3=high, GPIO_2=low, GPIO_2=high\n" + " %s -i -3,-2,2:3,-2,2 /dev/ttyS0\n", + name, + name, + name, + name, + name, + name, + name, + name + ); +} + diff --git a/tools/linux64/src/stm32flash_serial/src/parsers/Android.mk b/tools/linux64/src/stm32flash_serial/src/parsers/Android.mk new file mode 100644 index 0000000..afec18c --- /dev/null +++ b/tools/linux64/src/stm32flash_serial/src/parsers/Android.mk @@ -0,0 +1,6 @@ +LOCAL_PATH := $(call my-dir) + +include $(CLEAR_VARS) +LOCAL_MODULE := libparsers +LOCAL_SRC_FILES := binary.c hex.c +include $(BUILD_STATIC_LIBRARY) diff --git a/tools/linux64/src/stm32flash_serial/src/parsers/Makefile b/tools/linux64/src/stm32flash_serial/src/parsers/Makefile new file mode 100644 index 0000000..bb7df1e --- /dev/null +++ b/tools/linux64/src/stm32flash_serial/src/parsers/Makefile @@ -0,0 +1,12 @@ + +CFLAGS += -Wall -g + +all: parsers.a + +parsers.a: binary.o hex.o + $(AR) rc $@ binary.o hex.o + +clean: + rm -f *.o parsers.a + +.PHONY: all clean diff --git a/tools/linux64/src/stm32flash_serial/src/parsers/binary.c b/tools/linux64/src/stm32flash_serial/src/parsers/binary.c new file mode 100644 index 0000000..f491952 --- /dev/null +++ b/tools/linux64/src/stm32flash_serial/src/parsers/binary.c @@ -0,0 +1,140 @@ +/* + stm32flash - Open Source ST STM32 flash program for *nix + Copyright (C) 2010 Geoffrey McRae + + 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. +*/ + + +#include +#include +#include +#include +#include + +#include "binary.h" + +typedef struct { + int fd; + char write; + struct stat stat; +} binary_t; + +void* binary_init() { + return calloc(sizeof(binary_t), 1); +} + +parser_err_t binary_open(void *storage, const char *filename, const char write) { + binary_t *st = storage; + if (write) { + if (filename[0] == '-') + st->fd = 1; + else + st->fd = open( + filename, +#ifndef __WIN32__ + O_WRONLY | O_CREAT | O_TRUNC, +#else + O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, +#endif +#ifndef __WIN32__ + S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH +#else + 0 +#endif + ); + st->stat.st_size = 0; + } else { + if (filename[0] == '-') { + st->fd = 0; + } else { + if (stat(filename, &st->stat) != 0) + return PARSER_ERR_INVALID_FILE; + st->fd = open(filename, +#ifndef __WIN32__ + O_RDONLY +#else + O_RDONLY | O_BINARY +#endif + ); + } + } + + st->write = write; + return st->fd == -1 ? PARSER_ERR_SYSTEM : PARSER_ERR_OK; +} + +parser_err_t binary_close(void *storage) { + binary_t *st = storage; + + if (st->fd) close(st->fd); + free(st); + return PARSER_ERR_OK; +} + +unsigned int binary_size(void *storage) { + binary_t *st = storage; + return st->stat.st_size; +} + +parser_err_t binary_read(void *storage, void *data, unsigned int *len) { + binary_t *st = storage; + unsigned int left = *len; + if (st->write) return PARSER_ERR_WRONLY; + + ssize_t r; + while(left > 0) { + r = read(st->fd, data, left); + /* If there is no data to read at all, return OK, but with zero read */ + if (r == 0 && left == *len) { + *len = 0; + return PARSER_ERR_OK; + } + if (r <= 0) return PARSER_ERR_SYSTEM; + left -= r; + data += r; + } + + *len = *len - left; + return PARSER_ERR_OK; +} + +parser_err_t binary_write(void *storage, void *data, unsigned int len) { + binary_t *st = storage; + if (!st->write) return PARSER_ERR_RDONLY; + + ssize_t r; + while(len > 0) { + r = write(st->fd, data, len); + if (r < 1) return PARSER_ERR_SYSTEM; + st->stat.st_size += r; + + len -= r; + data += r; + } + + return PARSER_ERR_OK; +} + +parser_t PARSER_BINARY = { + "Raw BINARY", + binary_init, + binary_open, + binary_close, + binary_size, + binary_read, + binary_write +}; + diff --git a/tools/linux64/src/stm32flash_serial/src/parsers/binary.h b/tools/linux64/src/stm32flash_serial/src/parsers/binary.h new file mode 100644 index 0000000..d989acf --- /dev/null +++ b/tools/linux64/src/stm32flash_serial/src/parsers/binary.h @@ -0,0 +1,27 @@ +/* + stm32flash - Open Source ST STM32 flash program for *nix + Copyright (C) 2010 Geoffrey McRae + + 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. +*/ + + +#ifndef _PARSER_BINARY_H +#define _PARSER_BINARY_H + +#include "parser.h" + +extern parser_t PARSER_BINARY; +#endif diff --git a/tools/linux64/src/stm32flash_serial/src/parsers/hex.c b/tools/linux64/src/stm32flash_serial/src/parsers/hex.c new file mode 100644 index 0000000..3baf856 --- /dev/null +++ b/tools/linux64/src/stm32flash_serial/src/parsers/hex.c @@ -0,0 +1,224 @@ +/* + stm32flash - Open Source ST STM32 flash program for *nix + Copyright (C) 2010 Geoffrey McRae + + 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. +*/ + + +#include +#include +#include +#include +#include +#include +#include + +#include "hex.h" +#include "../utils.h" + +typedef struct { + size_t data_len, offset; + uint8_t *data; + uint8_t base; +} hex_t; + +void* hex_init() { + return calloc(sizeof(hex_t), 1); +} + +parser_err_t hex_open(void *storage, const char *filename, const char write) { + hex_t *st = storage; + if (write) { + return PARSER_ERR_RDONLY; + } else { + char mark; + int i, fd; + uint8_t checksum; + unsigned int c; + uint32_t base = 0; + unsigned int last_address = 0x0; + + fd = open(filename, O_RDONLY); + if (fd < 0) + return PARSER_ERR_SYSTEM; + + /* read in the file */ + + while(read(fd, &mark, 1) != 0) { + if (mark == '\n' || mark == '\r') continue; + if (mark != ':') + return PARSER_ERR_INVALID_FILE; + + char buffer[9]; + unsigned int reclen, address, type; + uint8_t *record = NULL; + + /* get the reclen, address, and type */ + buffer[8] = 0; + if (read(fd, &buffer, 8) != 8) return PARSER_ERR_INVALID_FILE; + if (sscanf(buffer, "%2x%4x%2x", &reclen, &address, &type) != 3) { + close(fd); + return PARSER_ERR_INVALID_FILE; + } + + /* setup the checksum */ + checksum = + reclen + + ((address & 0xFF00) >> 8) + + ((address & 0x00FF) >> 0) + + type; + + switch(type) { + /* data record */ + case 0: + c = address - last_address; + st->data = realloc(st->data, st->data_len + c + reclen); + + /* if there is a gap, set it to 0xff and increment the length */ + if (c > 0) { + memset(&st->data[st->data_len], 0xff, c); + st->data_len += c; + } + + last_address = address + reclen; + record = &st->data[st->data_len]; + st->data_len += reclen; + break; + + /* extended segment address record */ + case 2: + base = 0; + break; + + /* extended linear address record */ + case 4: + base = address; + break; + } + + buffer[2] = 0; + for(i = 0; i < reclen; ++i) { + if (read(fd, &buffer, 2) != 2 || sscanf(buffer, "%2x", &c) != 1) { + close(fd); + return PARSER_ERR_INVALID_FILE; + } + + /* add the byte to the checksum */ + checksum += c; + + switch(type) { + case 0: + if (record != NULL) { + record[i] = c; + } else { + return PARSER_ERR_INVALID_FILE; + } + break; + + case 2: + case 4: + base = (base << 8) | c; + break; + } + } + + /* read, scan, and verify the checksum */ + if ( + read(fd, &buffer, 2 ) != 2 || + sscanf(buffer, "%2x", &c) != 1 || + (uint8_t)(checksum + c) != 0x00 + ) { + close(fd); + return PARSER_ERR_INVALID_FILE; + } + + switch(type) { + /* EOF */ + case 1: + close(fd); + return PARSER_ERR_OK; + + /* address record */ + case 2: base = base << 4; + case 4: base = be_u32(base); + /* Reset last_address since our base changed */ + last_address = 0; + + if (st->base == 0) { + st->base = base; + break; + } + + /* we cant cope with files out of order */ + if (base < st->base) { + close(fd); + return PARSER_ERR_INVALID_FILE; + } + + /* if there is a gap, enlarge and fill with zeros */ + unsigned int len = base - st->base; + if (len > st->data_len) { + st->data = realloc(st->data, len); + memset(&st->data[st->data_len], 0, len - st->data_len); + st->data_len = len; + } + break; + } + } + + close(fd); + return PARSER_ERR_OK; + } +} + +parser_err_t hex_close(void *storage) { + hex_t *st = storage; + if (st) free(st->data); + free(st); + return PARSER_ERR_OK; +} + +unsigned int hex_size(void *storage) { + hex_t *st = storage; + return st->data_len; +} + +parser_err_t hex_read(void *storage, void *data, unsigned int *len) { + hex_t *st = storage; + unsigned int left = st->data_len - st->offset; + unsigned int get = left > *len ? *len : left; + + memcpy(data, &st->data[st->offset], get); + st->offset += get; + + *len = get; + return PARSER_ERR_OK; +} + +parser_err_t hex_write(void *storage, void *data, unsigned int len) { + return PARSER_ERR_RDONLY; +} + +parser_t PARSER_HEX = { + "Intel HEX", + hex_init, + hex_open, + hex_close, + hex_size, + hex_read, + hex_write +}; + diff --git a/tools/linux64/src/stm32flash_serial/src/parsers/hex.h b/tools/linux64/src/stm32flash_serial/src/parsers/hex.h new file mode 100644 index 0000000..02413c9 --- /dev/null +++ b/tools/linux64/src/stm32flash_serial/src/parsers/hex.h @@ -0,0 +1,27 @@ +/* + stm32flash - Open Source ST STM32 flash program for *nix + Copyright (C) 2010 Geoffrey McRae + + 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. +*/ + + +#ifndef _PARSER_HEX_H +#define _PARSER_HEX_H + +#include "parser.h" + +extern parser_t PARSER_HEX; +#endif diff --git a/tools/linux64/src/stm32flash_serial/src/parsers/parser.h b/tools/linux64/src/stm32flash_serial/src/parsers/parser.h new file mode 100644 index 0000000..c2fae3c --- /dev/null +++ b/tools/linux64/src/stm32flash_serial/src/parsers/parser.h @@ -0,0 +1,56 @@ +/* + stm32flash - Open Source ST STM32 flash program for *nix + Copyright (C) 2010 Geoffrey McRae + + 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. +*/ + + +#ifndef _H_PARSER +#define _H_PARSER + +enum parser_err { + PARSER_ERR_OK, + PARSER_ERR_SYSTEM, + PARSER_ERR_INVALID_FILE, + PARSER_ERR_WRONLY, + PARSER_ERR_RDONLY +}; +typedef enum parser_err parser_err_t; + +struct parser { + const char *name; + void* (*init )(); /* initialise the parser */ + parser_err_t (*open )(void *storage, const char *filename, const char write); /* open the file for read|write */ + parser_err_t (*close)(void *storage); /* close and free the parser */ + unsigned int (*size )(void *storage); /* get the total data size */ + parser_err_t (*read )(void *storage, void *data, unsigned int *len); /* read a block of data */ + parser_err_t (*write)(void *storage, void *data, unsigned int len); /* write a block of data */ +}; +typedef struct parser parser_t; + +static inline const char* parser_errstr(parser_err_t err) { + switch(err) { + case PARSER_ERR_OK : return "OK"; + case PARSER_ERR_SYSTEM : return "System Error"; + case PARSER_ERR_INVALID_FILE: return "Invalid File"; + case PARSER_ERR_WRONLY : return "Parser can only write"; + case PARSER_ERR_RDONLY : return "Parser can only read"; + default: + return "Unknown Error"; + } +} + +#endif diff --git a/tools/linux64/src/stm32flash_serial/src/port.c b/tools/linux64/src/stm32flash_serial/src/port.c new file mode 100644 index 0000000..08e58cc --- /dev/null +++ b/tools/linux64/src/stm32flash_serial/src/port.c @@ -0,0 +1,59 @@ +/* + stm32flash - Open Source ST STM32 flash program for *nix + Copyright (C) 2014 Antonio Borneo + + 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. +*/ + +#include +#include + +#include "serial.h" +#include "port.h" + + +extern struct port_interface port_serial; +extern struct port_interface port_i2c; + +static struct port_interface *ports[] = { + &port_serial, + &port_i2c, + NULL, +}; + + +port_err_t port_open(struct port_options *ops, struct port_interface **outport) +{ + int ret; + static struct port_interface **port; + + for (port = ports; *port; port++) { + ret = (*port)->open(*port, ops); + if (ret == PORT_ERR_NODEV) + continue; + if (ret == PORT_ERR_OK) + break; + fprintf(stderr, "Error probing interface \"%s\"\n", + (*port)->name); + } + if (*port == NULL) { + fprintf(stderr, "Cannot handle device \"%s\"\n", + ops->device); + return PORT_ERR_UNKNOWN; + } + + *outport = *port; + return PORT_ERR_OK; +} diff --git a/tools/linux64/src/stm32flash_serial/src/port.h b/tools/linux64/src/stm32flash_serial/src/port.h new file mode 100644 index 0000000..290f034 --- /dev/null +++ b/tools/linux64/src/stm32flash_serial/src/port.h @@ -0,0 +1,75 @@ +/* + stm32flash - Open Source ST STM32 flash program for *nix + Copyright (C) 2014 Antonio Borneo + + 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. +*/ + + +#ifndef _H_PORT +#define _H_PORT + +typedef enum { + PORT_ERR_OK = 0, + PORT_ERR_NODEV, /* No such device */ + PORT_ERR_TIMEDOUT, /* Operation timed out */ + PORT_ERR_UNKNOWN, +} port_err_t; + +/* flags */ +#define PORT_BYTE (1 << 0) /* byte (not frame) oriented */ +#define PORT_GVR_ETX (1 << 1) /* cmd GVR returns protection status */ +#define PORT_CMD_INIT (1 << 2) /* use INIT cmd to autodetect speed */ +#define PORT_RETRY (1 << 3) /* allowed read() retry after timeout */ +#define PORT_STRETCH_W (1 << 4) /* warning for no-stretching commands */ + +/* all options and flags used to open and configure an interface */ +struct port_options { + const char *device; + serial_baud_t baudRate; + const char *serial_mode; + int bus_addr; + int rx_frame_max; + int tx_frame_max; +}; + +/* + * Specify the length of reply for command GET + * This is helpful for frame-oriented protocols, e.g. i2c, to avoid time + * consuming try-fail-timeout-retry operation. + * On byte-oriented protocols, i.e. UART, this information would be skipped + * after read the first byte, so not needed. + */ +struct varlen_cmd { + uint8_t version; + uint8_t length; +}; + +struct port_interface { + const char *name; + unsigned flags; + port_err_t (*open)(struct port_interface *port, struct port_options *ops); + port_err_t (*close)(struct port_interface *port); + port_err_t (*read)(struct port_interface *port, void *buf, size_t nbyte); + port_err_t (*write)(struct port_interface *port, void *buf, size_t nbyte); + port_err_t (*gpio)(struct port_interface *port, serial_gpio_t n, int level); + const char *(*get_cfg_str)(struct port_interface *port); + struct varlen_cmd *cmd_get_reply; + void *private; +}; + +port_err_t port_open(struct port_options *ops, struct port_interface **outport); + +#endif diff --git a/tools/linux64/src/stm32flash_serial/src/protocol.txt b/tools/linux64/src/stm32flash_serial/src/protocol.txt new file mode 100644 index 0000000..0391099 --- /dev/null +++ b/tools/linux64/src/stm32flash_serial/src/protocol.txt @@ -0,0 +1,19 @@ +The communication protocol used by ST bootloader is documented in following ST +application notes, depending on communication port. + +In current version of stm32flash are supported only UART and I2C ports. + +* AN3154: CAN protocol used in the STM32 bootloader + http://www.st.com/web/en/resource/technical/document/application_note/CD00264321.pdf + +* AN3155: USART protocol used in the STM32(TM) bootloader + http://www.st.com/web/en/resource/technical/document/application_note/CD00264342.pdf + +* AN4221: I2C protocol used in the STM32 bootloader + http://www.st.com/web/en/resource/technical/document/application_note/DM00072315.pdf + +* AN4286: SPI protocol used in the STM32 bootloader + http://www.st.com/web/en/resource/technical/document/application_note/DM00081379.pdf + +Boot mode selection for STM32 is documented in ST application note AN2606, available in ST website: + http://www.st.com/web/en/resource/technical/document/application_note/CD00167594.pdf diff --git a/tools/linux64/src/stm32flash_serial/src/serial.h b/tools/linux64/src/stm32flash_serial/src/serial.h new file mode 100644 index 0000000..227ba16 --- /dev/null +++ b/tools/linux64/src/stm32flash_serial/src/serial.h @@ -0,0 +1,90 @@ +/* + stm32flash - Open Source ST STM32 flash program for *nix + Copyright (C) 2010 Geoffrey McRae + + 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. +*/ + + +#ifndef _SERIAL_H +#define _SERIAL_H + +typedef struct serial serial_t; + +typedef enum { + SERIAL_PARITY_NONE, + SERIAL_PARITY_EVEN, + SERIAL_PARITY_ODD, + + SERIAL_PARITY_INVALID +} serial_parity_t; + +typedef enum { + SERIAL_BITS_5, + SERIAL_BITS_6, + SERIAL_BITS_7, + SERIAL_BITS_8, + + SERIAL_BITS_INVALID +} serial_bits_t; + +typedef enum { + SERIAL_BAUD_1200, + SERIAL_BAUD_1800, + SERIAL_BAUD_2400, + SERIAL_BAUD_4800, + SERIAL_BAUD_9600, + SERIAL_BAUD_19200, + SERIAL_BAUD_38400, + SERIAL_BAUD_57600, + SERIAL_BAUD_115200, + SERIAL_BAUD_128000, + SERIAL_BAUD_230400, + SERIAL_BAUD_256000, + SERIAL_BAUD_460800, + SERIAL_BAUD_500000, + SERIAL_BAUD_576000, + SERIAL_BAUD_921600, + SERIAL_BAUD_1000000, + SERIAL_BAUD_1500000, + SERIAL_BAUD_2000000, + + SERIAL_BAUD_INVALID +} serial_baud_t; + +typedef enum { + SERIAL_STOPBIT_1, + SERIAL_STOPBIT_2, + + SERIAL_STOPBIT_INVALID +} serial_stopbit_t; + +typedef enum { + GPIO_RTS = 1, + GPIO_DTR, + GPIO_BRK, +} serial_gpio_t; + +/* common helper functions */ +serial_baud_t serial_get_baud(const unsigned int baud); +unsigned int serial_get_baud_int(const serial_baud_t baud); +serial_bits_t serial_get_bits(const char *mode); +unsigned int serial_get_bits_int(const serial_bits_t bits); +serial_parity_t serial_get_parity(const char *mode); +char serial_get_parity_str(const serial_parity_t parity); +serial_stopbit_t serial_get_stopbit(const char *mode); +unsigned int serial_get_stopbit_int(const serial_stopbit_t stopbit); + +#endif diff --git a/tools/linux64/src/stm32flash_serial/src/serial_common.c b/tools/linux64/src/stm32flash_serial/src/serial_common.c new file mode 100644 index 0000000..43e48e1 --- /dev/null +++ b/tools/linux64/src/stm32flash_serial/src/serial_common.c @@ -0,0 +1,154 @@ +/* + stm32flash - Open Source ST STM32 flash program for *nix + Copyright (C) 2010 Geoffrey McRae + + 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. +*/ + +#include "serial.h" + +serial_baud_t serial_get_baud(const unsigned int baud) { + switch(baud) { + case 1200: return SERIAL_BAUD_1200 ; + case 1800: return SERIAL_BAUD_1800 ; + case 2400: return SERIAL_BAUD_2400 ; + case 4800: return SERIAL_BAUD_4800 ; + case 9600: return SERIAL_BAUD_9600 ; + case 19200: return SERIAL_BAUD_19200 ; + case 38400: return SERIAL_BAUD_38400 ; + case 57600: return SERIAL_BAUD_57600 ; + case 115200: return SERIAL_BAUD_115200; + case 128000: return SERIAL_BAUD_128000; + case 230400: return SERIAL_BAUD_230400; + case 256000: return SERIAL_BAUD_256000; + case 460800: return SERIAL_BAUD_460800; + case 500000: return SERIAL_BAUD_500000; + case 576000: return SERIAL_BAUD_576000; + case 921600: return SERIAL_BAUD_921600; + case 1000000: return SERIAL_BAUD_1000000; + case 1500000: return SERIAL_BAUD_1500000; + case 2000000: return SERIAL_BAUD_2000000; + + default: + return SERIAL_BAUD_INVALID; + } +} + +unsigned int serial_get_baud_int(const serial_baud_t baud) { + switch(baud) { + case SERIAL_BAUD_1200 : return 1200 ; + case SERIAL_BAUD_1800 : return 1800 ; + case SERIAL_BAUD_2400 : return 2400 ; + case SERIAL_BAUD_4800 : return 4800 ; + case SERIAL_BAUD_9600 : return 9600 ; + case SERIAL_BAUD_19200 : return 19200 ; + case SERIAL_BAUD_38400 : return 38400 ; + case SERIAL_BAUD_57600 : return 57600 ; + case SERIAL_BAUD_115200: return 115200; + case SERIAL_BAUD_128000: return 128000; + case SERIAL_BAUD_230400: return 230400; + case SERIAL_BAUD_256000: return 256000; + case SERIAL_BAUD_460800: return 460800; + case SERIAL_BAUD_500000: return 500000; + case SERIAL_BAUD_576000: return 576000; + case SERIAL_BAUD_921600: return 921600; + case SERIAL_BAUD_1000000: return 1000000; + case SERIAL_BAUD_1500000: return 1500000; + case SERIAL_BAUD_2000000: return 2000000; + + case SERIAL_BAUD_INVALID: + default: + return 0; + } +} + +serial_bits_t serial_get_bits(const char *mode) { + if (!mode) + return SERIAL_BITS_INVALID; + switch(mode[0]) { + case '5': return SERIAL_BITS_5; + case '6': return SERIAL_BITS_6; + case '7': return SERIAL_BITS_7; + case '8': return SERIAL_BITS_8; + + default: + return SERIAL_BITS_INVALID; + } +} + +unsigned int serial_get_bits_int(const serial_bits_t bits) { + switch(bits) { + case SERIAL_BITS_5: return 5; + case SERIAL_BITS_6: return 6; + case SERIAL_BITS_7: return 7; + case SERIAL_BITS_8: return 8; + + default: + return 0; + } +} + +serial_parity_t serial_get_parity(const char *mode) { + if (!mode || !mode[0]) + return SERIAL_PARITY_INVALID; + switch(mode[1]) { + case 'N': + case 'n': + return SERIAL_PARITY_NONE; + case 'E': + case 'e': + return SERIAL_PARITY_EVEN; + case 'O': + case 'o': + return SERIAL_PARITY_ODD; + + default: + return SERIAL_PARITY_INVALID; + } +} + +char serial_get_parity_str(const serial_parity_t parity) { + switch(parity) { + case SERIAL_PARITY_NONE: return 'N'; + case SERIAL_PARITY_EVEN: return 'E'; + case SERIAL_PARITY_ODD : return 'O'; + + default: + return ' '; + } +} + +serial_stopbit_t serial_get_stopbit(const char *mode) { + if (!mode || !mode[0] || !mode[1]) + return SERIAL_STOPBIT_INVALID; + switch(mode[2]) { + case '1': return SERIAL_STOPBIT_1; + case '2': return SERIAL_STOPBIT_2; + + default: + return SERIAL_STOPBIT_INVALID; + } +} + +unsigned int serial_get_stopbit_int(const serial_stopbit_t stopbit) { + switch(stopbit) { + case SERIAL_STOPBIT_1: return 1; + case SERIAL_STOPBIT_2: return 2; + + default: + return 0; + } +} + diff --git a/tools/linux64/src/stm32flash_serial/src/serial_platform.c b/tools/linux64/src/stm32flash_serial/src/serial_platform.c new file mode 100644 index 0000000..98e2569 --- /dev/null +++ b/tools/linux64/src/stm32flash_serial/src/serial_platform.c @@ -0,0 +1,5 @@ +#if defined(__WIN32__) || defined(__CYGWIN__) +# include "serial_w32.c" +#else +# include "serial_posix.c" +#endif diff --git a/tools/linux64/src/stm32flash_serial/src/serial_posix.c b/tools/linux64/src/stm32flash_serial/src/serial_posix.c new file mode 100644 index 0000000..284b35b --- /dev/null +++ b/tools/linux64/src/stm32flash_serial/src/serial_posix.c @@ -0,0 +1,395 @@ +/* + stm32flash - Open Source ST STM32 flash program for *nix + Copyright (C) 2010 Geoffrey McRae + + 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. +*/ + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "serial.h" +#include "port.h" + +struct serial { + int fd; + struct termios oldtio; + struct termios newtio; + char setup_str[11]; +}; + +static serial_t *serial_open(const char *device) +{ + serial_t *h = calloc(sizeof(serial_t), 1); + + h->fd = open(device, O_RDWR | O_NOCTTY | O_NDELAY); + if (h->fd < 0) { + free(h); + return NULL; + } + fcntl(h->fd, F_SETFL, 0); + + tcgetattr(h->fd, &h->oldtio); + tcgetattr(h->fd, &h->newtio); + + return h; +} + +static void serial_flush(const serial_t *h) +{ + tcflush(h->fd, TCIFLUSH); +} + +static void serial_close(serial_t *h) +{ + serial_flush(h); + tcsetattr(h->fd, TCSANOW, &h->oldtio); + close(h->fd); + free(h); +} + +static port_err_t serial_setup(serial_t *h, const serial_baud_t baud, + const serial_bits_t bits, + const serial_parity_t parity, + const serial_stopbit_t stopbit) +{ + speed_t port_baud; + tcflag_t port_bits; + tcflag_t port_parity; + tcflag_t port_stop; + struct termios settings; + + switch (baud) { + case SERIAL_BAUD_1200: port_baud = B1200; break; + case SERIAL_BAUD_1800: port_baud = B1800; break; + case SERIAL_BAUD_2400: port_baud = B2400; break; + case SERIAL_BAUD_4800: port_baud = B4800; break; + case SERIAL_BAUD_9600: port_baud = B9600; break; + case SERIAL_BAUD_19200: port_baud = B19200; break; + case SERIAL_BAUD_38400: port_baud = B38400; break; + case SERIAL_BAUD_57600: port_baud = B57600; break; + case SERIAL_BAUD_115200: port_baud = B115200; break; + case SERIAL_BAUD_230400: port_baud = B230400; break; +#ifdef B460800 + case SERIAL_BAUD_460800: port_baud = B460800; break; +#endif /* B460800 */ +#ifdef B921600 + case SERIAL_BAUD_921600: port_baud = B921600; break; +#endif /* B921600 */ +#ifdef B500000 + case SERIAL_BAUD_500000: port_baud = B500000; break; +#endif /* B500000 */ +#ifdef B576000 + case SERIAL_BAUD_576000: port_baud = B576000; break; +#endif /* B576000 */ +#ifdef B1000000 + case SERIAL_BAUD_1000000: port_baud = B1000000; break; +#endif /* B1000000 */ +#ifdef B1500000 + case SERIAL_BAUD_1500000: port_baud = B1500000; break; +#endif /* B1500000 */ +#ifdef B2000000 + case SERIAL_BAUD_2000000: port_baud = B2000000; break; +#endif /* B2000000 */ + + case SERIAL_BAUD_INVALID: + default: + return PORT_ERR_UNKNOWN; + } + + switch (bits) { + case SERIAL_BITS_5: port_bits = CS5; break; + case SERIAL_BITS_6: port_bits = CS6; break; + case SERIAL_BITS_7: port_bits = CS7; break; + case SERIAL_BITS_8: port_bits = CS8; break; + + default: + return PORT_ERR_UNKNOWN; + } + + switch (parity) { + case SERIAL_PARITY_NONE: port_parity = 0; break; + case SERIAL_PARITY_EVEN: port_parity = PARENB; break; + case SERIAL_PARITY_ODD: port_parity = PARENB | PARODD; break; + + default: + return PORT_ERR_UNKNOWN; + } + + switch (stopbit) { + case SERIAL_STOPBIT_1: port_stop = 0; break; + case SERIAL_STOPBIT_2: port_stop = CSTOPB; break; + + default: + return PORT_ERR_UNKNOWN; + } + + /* reset the settings */ +#ifndef __sun /* Used by GNU and BSD. Ignore __SVR4 in test. */ + cfmakeraw(&h->newtio); +#else /* __sun */ + h->newtio.c_iflag &= ~(IGNBRK | BRKINT | PARMRK | ISTRIP | INLCR + | IGNCR | ICRNL | IXON); + if (port_parity) + h->newtio.c_iflag |= INPCK; + + h->newtio.c_oflag &= ~OPOST; + h->newtio.c_lflag &= ~(ECHO | ECHONL | ICANON | ISIG | IEXTEN); + h->newtio.c_cflag &= ~(CSIZE | PARENB); + h->newtio.c_cflag |= CS8; +#endif /* __sun */ +#ifdef __QNXNTO__ + h->newtio.c_cflag &= ~(CSIZE | IHFLOW | OHFLOW); +#else + h->newtio.c_cflag &= ~(CSIZE | CRTSCTS); +#endif + h->newtio.c_cflag &= ~(CSIZE | CRTSCTS); + h->newtio.c_iflag &= ~(IXON | IXOFF | IXANY | IGNPAR); + h->newtio.c_lflag &= ~(ECHOK | ECHOCTL | ECHOKE); + h->newtio.c_oflag &= ~(OPOST | ONLCR); + + /* setup the new settings */ + cfsetispeed(&h->newtio, port_baud); + cfsetospeed(&h->newtio, port_baud); + h->newtio.c_cflag |= + port_parity | + port_bits | + port_stop | + CLOCAL | + CREAD; + + h->newtio.c_cc[VMIN] = 0; + h->newtio.c_cc[VTIME] = 5; /* in units of 0.1 s */ + + /* set the settings */ + serial_flush(h); + if (tcsetattr(h->fd, TCSANOW, &h->newtio) != 0) + return PORT_ERR_UNKNOWN; + +/* this check fails on CDC-ACM devices, bits 16 and 17 of cflag differ! + * it has been disabled below for now -jcw, 2015-11-09 + if (settings.c_cflag != h->newtio.c_cflag) + fprintf(stderr, "c_cflag mismatch %lx\n", + settings.c_cflag ^ h->newtio.c_cflag); + */ + + /* confirm they were set */ + tcgetattr(h->fd, &settings); + if (settings.c_iflag != h->newtio.c_iflag || + settings.c_oflag != h->newtio.c_oflag || + //settings.c_cflag != h->newtio.c_cflag || + settings.c_lflag != h->newtio.c_lflag) + return PORT_ERR_UNKNOWN; + + snprintf(h->setup_str, sizeof(h->setup_str), "%u %d%c%d", + serial_get_baud_int(baud), + serial_get_bits_int(bits), + serial_get_parity_str(parity), + serial_get_stopbit_int(stopbit)); + return PORT_ERR_OK; +} + +/* + * Roger clark. + * This function is no longer used. But has just been commented out in case it needs + * to be reinstated in the future + +static int startswith(const char *haystack, const char *needle) { + return strncmp(haystack, needle, strlen(needle)) == 0; +} +*/ + +static int is_tty(const char *path) { + char resolved[PATH_MAX]; + + if(!realpath(path, resolved)) return 0; + + + /* + * Roger Clark + * Commented out this check, because on OSX some devices are /dev/cu + * and some users use symbolic links to devices, hence the name may not even start + * with /dev + + if(startswith(resolved, "/dev/tty")) return 1; + + return 0; + */ + + return 1; +} + +static port_err_t serial_posix_open(struct port_interface *port, + struct port_options *ops) +{ + serial_t *h; + + /* 1. check device name match */ + if (!is_tty(ops->device)) + return PORT_ERR_NODEV; + + /* 2. check options */ + if (ops->baudRate == SERIAL_BAUD_INVALID) + return PORT_ERR_UNKNOWN; + if (serial_get_bits(ops->serial_mode) == SERIAL_BITS_INVALID) + return PORT_ERR_UNKNOWN; + if (serial_get_parity(ops->serial_mode) == SERIAL_PARITY_INVALID) + return PORT_ERR_UNKNOWN; + if (serial_get_stopbit(ops->serial_mode) == SERIAL_STOPBIT_INVALID) + return PORT_ERR_UNKNOWN; + + /* 3. open it */ + h = serial_open(ops->device); + if (h == NULL) + return PORT_ERR_UNKNOWN; + + /* 4. set options */ + if (serial_setup(h, ops->baudRate, + serial_get_bits(ops->serial_mode), + serial_get_parity(ops->serial_mode), + serial_get_stopbit(ops->serial_mode) + ) != PORT_ERR_OK) { + serial_close(h); + return PORT_ERR_UNKNOWN; + } + + port->private = h; + return PORT_ERR_OK; +} + +static port_err_t serial_posix_close(struct port_interface *port) +{ + serial_t *h; + + h = (serial_t *)port->private; + if (h == NULL) + return PORT_ERR_UNKNOWN; + + serial_close(h); + port->private = NULL; + return PORT_ERR_OK; +} + +static port_err_t serial_posix_read(struct port_interface *port, void *buf, + size_t nbyte) +{ + serial_t *h; + ssize_t r; + uint8_t *pos = (uint8_t *)buf; + + h = (serial_t *)port->private; + if (h == NULL) + return PORT_ERR_UNKNOWN; + + while (nbyte) { + r = read(h->fd, pos, nbyte); + if (r == 0) + return PORT_ERR_TIMEDOUT; + if (r < 0) + return PORT_ERR_UNKNOWN; + + nbyte -= r; + pos += r; + } + return PORT_ERR_OK; +} + +static port_err_t serial_posix_write(struct port_interface *port, void *buf, + size_t nbyte) +{ + serial_t *h; + ssize_t r; + const uint8_t *pos = (const uint8_t *)buf; + + h = (serial_t *)port->private; + if (h == NULL) + return PORT_ERR_UNKNOWN; + + while (nbyte) { + r = write(h->fd, pos, nbyte); + if (r < 1) + return PORT_ERR_UNKNOWN; + + nbyte -= r; + pos += r; + } + return PORT_ERR_OK; +} + +static port_err_t serial_posix_gpio(struct port_interface *port, + serial_gpio_t n, int level) +{ + serial_t *h; + int bit, lines; + + h = (serial_t *)port->private; + if (h == NULL) + return PORT_ERR_UNKNOWN; + + switch (n) { + case GPIO_RTS: + bit = TIOCM_RTS; + break; + + case GPIO_DTR: + bit = TIOCM_DTR; + break; + + case GPIO_BRK: + if (level == 0) + return PORT_ERR_OK; + if (tcsendbreak(h->fd, 1)) + return PORT_ERR_UNKNOWN; + return PORT_ERR_OK; + + default: + return PORT_ERR_UNKNOWN; + } + + /* handle RTS/DTR */ + if (ioctl(h->fd, TIOCMGET, &lines)) + return PORT_ERR_UNKNOWN; + lines = level ? lines | bit : lines & ~bit; + if (ioctl(h->fd, TIOCMSET, &lines)) + return PORT_ERR_UNKNOWN; + + return PORT_ERR_OK; +} + +static const char *serial_posix_get_cfg_str(struct port_interface *port) +{ + serial_t *h; + + h = (serial_t *)port->private; + return h ? h->setup_str : "INVALID"; +} + +struct port_interface port_serial = { + .name = "serial_posix", + .flags = PORT_BYTE | PORT_GVR_ETX | PORT_CMD_INIT | PORT_RETRY, + .open = serial_posix_open, + .close = serial_posix_close, + .read = serial_posix_read, + .write = serial_posix_write, + .gpio = serial_posix_gpio, + .get_cfg_str = serial_posix_get_cfg_str, +}; diff --git a/tools/linux64/src/stm32flash_serial/src/serial_w32.c b/tools/linux64/src/stm32flash_serial/src/serial_w32.c new file mode 100644 index 0000000..56772c0 --- /dev/null +++ b/tools/linux64/src/stm32flash_serial/src/serial_w32.c @@ -0,0 +1,341 @@ +/* + stm32flash - Open Source ST STM32 flash program for *nix + Copyright (C) 2010 Geoffrey McRae + Copyright (C) 2010 Gareth McMullin + + 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. +*/ + +#include +#include +#include +#include +#include +#include +#include +#include + +#include "serial.h" +#include "port.h" + +struct serial { + HANDLE fd; + DCB oldtio; + DCB newtio; + char setup_str[11]; +}; + +static serial_t *serial_open(const char *device) +{ + serial_t *h = calloc(sizeof(serial_t), 1); + char *devName; + + /* timeout in ms */ + COMMTIMEOUTS timeouts = {MAXDWORD, MAXDWORD, 500, 0, 0}; + + /* Fix the device name if required */ + if (strlen(device) > 4 && device[0] != '\\') { + devName = calloc(1, strlen(device) + 5); + sprintf(devName, "\\\\.\\%s", device); + } else { + devName = (char *)device; + } + + /* Create file handle for port */ + h->fd = CreateFile(devName, GENERIC_READ | GENERIC_WRITE, + 0, /* Exclusive access */ + NULL, /* No security */ + OPEN_EXISTING, + 0, /* No overlap */ + NULL); + + if (devName != device) + free(devName); + + if (h->fd == INVALID_HANDLE_VALUE) { + if (GetLastError() == ERROR_FILE_NOT_FOUND) + fprintf(stderr, "File not found: %s\n", device); + return NULL; + } + + SetupComm(h->fd, 4096, 4096); /* Set input and output buffer size */ + + SetCommTimeouts(h->fd, &timeouts); + + SetCommMask(h->fd, EV_ERR); /* Notify us of error events */ + + GetCommState(h->fd, &h->oldtio); /* Retrieve port parameters */ + GetCommState(h->fd, &h->newtio); /* Retrieve port parameters */ + + /* PurgeComm(h->fd, PURGE_RXABORT | PURGE_TXCLEAR | PURGE_TXABORT | PURGE_TXCLEAR); */ + + return h; +} + +static void serial_flush(const serial_t *h) +{ + /* We shouldn't need to flush in non-overlapping (blocking) mode */ + /* tcflush(h->fd, TCIFLUSH); */ +} + +static void serial_close(serial_t *h) +{ + serial_flush(h); + SetCommState(h->fd, &h->oldtio); + CloseHandle(h->fd); + free(h); +} + +static port_err_t serial_setup(serial_t *h, + const serial_baud_t baud, + const serial_bits_t bits, + const serial_parity_t parity, + const serial_stopbit_t stopbit) +{ + switch (baud) { + case SERIAL_BAUD_1200: h->newtio.BaudRate = CBR_1200; break; + /* case SERIAL_BAUD_1800: h->newtio.BaudRate = CBR_1800; break; */ + case SERIAL_BAUD_2400: h->newtio.BaudRate = CBR_2400; break; + case SERIAL_BAUD_4800: h->newtio.BaudRate = CBR_4800; break; + case SERIAL_BAUD_9600: h->newtio.BaudRate = CBR_9600; break; + case SERIAL_BAUD_19200: h->newtio.BaudRate = CBR_19200; break; + case SERIAL_BAUD_38400: h->newtio.BaudRate = CBR_38400; break; + case SERIAL_BAUD_57600: h->newtio.BaudRate = CBR_57600; break; + case SERIAL_BAUD_115200: h->newtio.BaudRate = CBR_115200; break; + case SERIAL_BAUD_128000: h->newtio.BaudRate = CBR_128000; break; + case SERIAL_BAUD_256000: h->newtio.BaudRate = CBR_256000; break; + /* These are not defined in WinBase.h and might work or not */ + case SERIAL_BAUD_230400: h->newtio.BaudRate = 230400; break; + case SERIAL_BAUD_460800: h->newtio.BaudRate = 460800; break; + case SERIAL_BAUD_500000: h->newtio.BaudRate = 500000; break; + case SERIAL_BAUD_576000: h->newtio.BaudRate = 576000; break; + case SERIAL_BAUD_921600: h->newtio.BaudRate = 921600; break; + case SERIAL_BAUD_1000000: h->newtio.BaudRate = 1000000; break; + case SERIAL_BAUD_1500000: h->newtio.BaudRate = 1500000; break; + case SERIAL_BAUD_2000000: h->newtio.BaudRate = 2000000; break; + case SERIAL_BAUD_INVALID: + + default: + return PORT_ERR_UNKNOWN; + } + + switch (bits) { + case SERIAL_BITS_5: h->newtio.ByteSize = 5; break; + case SERIAL_BITS_6: h->newtio.ByteSize = 6; break; + case SERIAL_BITS_7: h->newtio.ByteSize = 7; break; + case SERIAL_BITS_8: h->newtio.ByteSize = 8; break; + + default: + return PORT_ERR_UNKNOWN; + } + + switch (parity) { + case SERIAL_PARITY_NONE: h->newtio.Parity = NOPARITY; break; + case SERIAL_PARITY_EVEN: h->newtio.Parity = EVENPARITY; break; + case SERIAL_PARITY_ODD: h->newtio.Parity = ODDPARITY; break; + + default: + return PORT_ERR_UNKNOWN; + } + + switch (stopbit) { + case SERIAL_STOPBIT_1: h->newtio.StopBits = ONESTOPBIT; break; + case SERIAL_STOPBIT_2: h->newtio.StopBits = TWOSTOPBITS; break; + + default: + return PORT_ERR_UNKNOWN; + } + + /* reset the settings */ + h->newtio.fOutxCtsFlow = FALSE; + h->newtio.fOutxDsrFlow = FALSE; + h->newtio.fOutX = FALSE; + h->newtio.fInX = FALSE; + h->newtio.fNull = 0; + h->newtio.fAbortOnError = 0; + + /* set the settings */ + serial_flush(h); + if (!SetCommState(h->fd, &h->newtio)) + return PORT_ERR_UNKNOWN; + + snprintf(h->setup_str, sizeof(h->setup_str), "%u %d%c%d", + serial_get_baud_int(baud), + serial_get_bits_int(bits), + serial_get_parity_str(parity), + serial_get_stopbit_int(stopbit) + ); + return PORT_ERR_OK; +} + +static port_err_t serial_w32_open(struct port_interface *port, + struct port_options *ops) +{ + serial_t *h; + + /* 1. check device name match */ + if (!((strlen(ops->device) == 4 || strlen(ops->device) == 5) + && !strncmp(ops->device, "COM", 3) && isdigit(ops->device[3])) + && !(!strncmp(ops->device, "\\\\.\\COM", strlen("\\\\.\\COM")) + && isdigit(ops->device[strlen("\\\\.\\COM")]))) + return PORT_ERR_NODEV; + + /* 2. check options */ + if (ops->baudRate == SERIAL_BAUD_INVALID) + return PORT_ERR_UNKNOWN; + if (serial_get_bits(ops->serial_mode) == SERIAL_BITS_INVALID) + return PORT_ERR_UNKNOWN; + if (serial_get_parity(ops->serial_mode) == SERIAL_PARITY_INVALID) + return PORT_ERR_UNKNOWN; + if (serial_get_stopbit(ops->serial_mode) == SERIAL_STOPBIT_INVALID) + return PORT_ERR_UNKNOWN; + + /* 3. open it */ + h = serial_open(ops->device); + if (h == NULL) + return PORT_ERR_UNKNOWN; + + /* 4. set options */ + if (serial_setup(h, ops->baudRate, + serial_get_bits(ops->serial_mode), + serial_get_parity(ops->serial_mode), + serial_get_stopbit(ops->serial_mode) + ) != PORT_ERR_OK) { + serial_close(h); + return PORT_ERR_UNKNOWN; + } + + port->private = h; + return PORT_ERR_OK; +} + +static port_err_t serial_w32_close(struct port_interface *port) +{ + serial_t *h; + + h = (serial_t *)port->private; + if (h == NULL) + return PORT_ERR_UNKNOWN; + + serial_close(h); + port->private = NULL; + return PORT_ERR_OK; +} + +static port_err_t serial_w32_read(struct port_interface *port, void *buf, + size_t nbyte) +{ + serial_t *h; + DWORD r; + uint8_t *pos = (uint8_t *)buf; + + h = (serial_t *)port->private; + if (h == NULL) + return PORT_ERR_UNKNOWN; + + while (nbyte) { + ReadFile(h->fd, pos, nbyte, &r, NULL); + if (r == 0) + return PORT_ERR_TIMEDOUT; + if (r < 0) + return PORT_ERR_UNKNOWN; + + nbyte -= r; + pos += r; + } + return PORT_ERR_OK; +} + +static port_err_t serial_w32_write(struct port_interface *port, void *buf, + size_t nbyte) +{ + serial_t *h; + DWORD r; + uint8_t *pos = (uint8_t *)buf; + + h = (serial_t *)port->private; + if (h == NULL) + return PORT_ERR_UNKNOWN; + + while (nbyte) { + if (!WriteFile(h->fd, pos, nbyte, &r, NULL)) + return PORT_ERR_UNKNOWN; + if (r < 1) + return PORT_ERR_UNKNOWN; + + nbyte -= r; + pos += r; + } + return PORT_ERR_OK; +} + +static port_err_t serial_w32_gpio(struct port_interface *port, + serial_gpio_t n, int level) +{ + serial_t *h; + int bit; + + h = (serial_t *)port->private; + if (h == NULL) + return PORT_ERR_UNKNOWN; + + switch (n) { + case GPIO_RTS: + bit = level ? SETRTS : CLRRTS; + break; + + case GPIO_DTR: + bit = level ? SETDTR : CLRDTR; + break; + + case GPIO_BRK: + if (level == 0) + return PORT_ERR_OK; + if (EscapeCommFunction(h->fd, SETBREAK) == 0) + return PORT_ERR_UNKNOWN; + usleep(500000); + if (EscapeCommFunction(h->fd, CLRBREAK) == 0) + return PORT_ERR_UNKNOWN; + return PORT_ERR_OK; + + default: + return PORT_ERR_UNKNOWN; + } + + /* handle RTS/DTR */ + if (EscapeCommFunction(h->fd, bit) == 0) + return PORT_ERR_UNKNOWN; + + return PORT_ERR_OK; +} + +static const char *serial_w32_get_cfg_str(struct port_interface *port) +{ + serial_t *h; + + h = (serial_t *)port->private; + return h ? h->setup_str : "INVALID"; +} + +struct port_interface port_serial = { + .name = "serial_w32", + .flags = PORT_BYTE | PORT_GVR_ETX | PORT_CMD_INIT | PORT_RETRY, + .open = serial_w32_open, + .close = serial_w32_close, + .read = serial_w32_read, + .write = serial_w32_write, + .gpio = serial_w32_gpio, + .get_cfg_str = serial_w32_get_cfg_str, +}; diff --git a/tools/linux64/src/stm32flash_serial/src/stm32.c b/tools/linux64/src/stm32flash_serial/src/stm32.c new file mode 100644 index 0000000..74047d2 --- /dev/null +++ b/tools/linux64/src/stm32flash_serial/src/stm32.c @@ -0,0 +1,1048 @@ +/* + stm32flash - Open Source ST STM32 flash program for *nix + Copyright 2010 Geoffrey McRae + Copyright 2012-2014 Tormod Volden + + 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. +*/ + +#include +#include +#include +#include +#include +#include + +#include "stm32.h" +#include "port.h" +#include "utils.h" + +#define STM32_ACK 0x79 +#define STM32_NACK 0x1F +#define STM32_BUSY 0x76 + +#define STM32_CMD_INIT 0x7F +#define STM32_CMD_GET 0x00 /* get the version and command supported */ +#define STM32_CMD_GVR 0x01 /* get version and read protection status */ +#define STM32_CMD_GID 0x02 /* get ID */ +#define STM32_CMD_RM 0x11 /* read memory */ +#define STM32_CMD_GO 0x21 /* go */ +#define STM32_CMD_WM 0x31 /* write memory */ +#define STM32_CMD_WM_NS 0x32 /* no-stretch write memory */ +#define STM32_CMD_ER 0x43 /* erase */ +#define STM32_CMD_EE 0x44 /* extended erase */ +#define STM32_CMD_EE_NS 0x45 /* extended erase no-stretch */ +#define STM32_CMD_WP 0x63 /* write protect */ +#define STM32_CMD_WP_NS 0x64 /* write protect no-stretch */ +#define STM32_CMD_UW 0x73 /* write unprotect */ +#define STM32_CMD_UW_NS 0x74 /* write unprotect no-stretch */ +#define STM32_CMD_RP 0x82 /* readout protect */ +#define STM32_CMD_RP_NS 0x83 /* readout protect no-stretch */ +#define STM32_CMD_UR 0x92 /* readout unprotect */ +#define STM32_CMD_UR_NS 0x93 /* readout unprotect no-stretch */ +#define STM32_CMD_CRC 0xA1 /* compute CRC */ +#define STM32_CMD_ERR 0xFF /* not a valid command */ + +#define STM32_RESYNC_TIMEOUT 35 /* seconds */ +#define STM32_MASSERASE_TIMEOUT 35 /* seconds */ +#define STM32_SECTERASE_TIMEOUT 5 /* seconds */ +#define STM32_BLKWRITE_TIMEOUT 1 /* seconds */ +#define STM32_WUNPROT_TIMEOUT 1 /* seconds */ +#define STM32_WPROT_TIMEOUT 1 /* seconds */ +#define STM32_RPROT_TIMEOUT 1 /* seconds */ + +#define STM32_CMD_GET_LENGTH 17 /* bytes in the reply */ + +struct stm32_cmd { + uint8_t get; + uint8_t gvr; + uint8_t gid; + uint8_t rm; + uint8_t go; + uint8_t wm; + uint8_t er; /* this may be extended erase */ + uint8_t wp; + uint8_t uw; + uint8_t rp; + uint8_t ur; + uint8_t crc; +}; + +/* Reset code for ARMv7-M (Cortex-M3) and ARMv6-M (Cortex-M0) + * see ARMv7-M or ARMv6-M Architecture Reference Manual (table B3-8) + * or "The definitive guide to the ARM Cortex-M3", section 14.4. + */ +static const uint8_t stm_reset_code[] = { + 0x01, 0x49, // ldr r1, [pc, #4] ; () + 0x02, 0x4A, // ldr r2, [pc, #8] ; () + 0x0A, 0x60, // str r2, [r1, #0] + 0xfe, 0xe7, // endless: b endless + 0x0c, 0xed, 0x00, 0xe0, // .word 0xe000ed0c = NVIC AIRCR register address + 0x04, 0x00, 0xfa, 0x05 // .word 0x05fa0004 = VECTKEY | SYSRESETREQ +}; + +static const uint32_t stm_reset_code_length = sizeof(stm_reset_code); + +extern const stm32_dev_t devices[]; + +static void stm32_warn_stretching(const char *f) +{ + fprintf(stderr, "Attention !!!\n"); + fprintf(stderr, "\tThis %s error could be caused by your I2C\n", f); + fprintf(stderr, "\tcontroller not accepting \"clock stretching\"\n"); + fprintf(stderr, "\tas required by bootloader.\n"); + fprintf(stderr, "\tCheck \"I2C.txt\" in stm32flash source code.\n"); +} + +static stm32_err_t stm32_get_ack_timeout(const stm32_t *stm, time_t timeout) +{ + struct port_interface *port = stm->port; + uint8_t byte; + port_err_t p_err; + time_t t0, t1; + + if (!(port->flags & PORT_RETRY)) + timeout = 0; + + if (timeout) + time(&t0); + + do { + p_err = port->read(port, &byte, 1); + if (p_err == PORT_ERR_TIMEDOUT && timeout) { + time(&t1); + if (t1 < t0 + timeout) + continue; + } + + if (p_err != PORT_ERR_OK) { + fprintf(stderr, "Failed to read ACK byte\n"); + return STM32_ERR_UNKNOWN; + } + + if (byte == STM32_ACK) + return STM32_ERR_OK; + if (byte == STM32_NACK) + return STM32_ERR_NACK; + if (byte != STM32_BUSY) { + fprintf(stderr, "Got byte 0x%02x instead of ACK\n", + byte); + return STM32_ERR_UNKNOWN; + } + } while (1); +} + +static stm32_err_t stm32_get_ack(const stm32_t *stm) +{ + return stm32_get_ack_timeout(stm, 0); +} + +static stm32_err_t stm32_send_command_timeout(const stm32_t *stm, + const uint8_t cmd, + time_t timeout) +{ + struct port_interface *port = stm->port; + stm32_err_t s_err; + port_err_t p_err; + uint8_t buf[2]; + + buf[0] = cmd; + buf[1] = cmd ^ 0xFF; + p_err = port->write(port, buf, 2); + if (p_err != PORT_ERR_OK) { + fprintf(stderr, "Failed to send command\n"); + return STM32_ERR_UNKNOWN; + } + s_err = stm32_get_ack_timeout(stm, timeout); + if (s_err == STM32_ERR_OK) + return STM32_ERR_OK; + if (s_err == STM32_ERR_NACK) + fprintf(stderr, "Got NACK from device on command 0x%02x\n", cmd); + else + fprintf(stderr, "Unexpected reply from device on command 0x%02x\n", cmd); + return STM32_ERR_UNKNOWN; +} + +static stm32_err_t stm32_send_command(const stm32_t *stm, const uint8_t cmd) +{ + return stm32_send_command_timeout(stm, cmd, 0); +} + +/* if we have lost sync, send a wrong command and expect a NACK */ +static stm32_err_t stm32_resync(const stm32_t *stm) +{ + struct port_interface *port = stm->port; + port_err_t p_err; + uint8_t buf[2], ack; + time_t t0, t1; + + time(&t0); + t1 = t0; + + buf[0] = STM32_CMD_ERR; + buf[1] = STM32_CMD_ERR ^ 0xFF; + while (t1 < t0 + STM32_RESYNC_TIMEOUT) { + p_err = port->write(port, buf, 2); + if (p_err != PORT_ERR_OK) { + usleep(500000); + time(&t1); + continue; + } + p_err = port->read(port, &ack, 1); + if (p_err != PORT_ERR_OK) { + time(&t1); + continue; + } + if (ack == STM32_NACK) + return STM32_ERR_OK; + time(&t1); + } + return STM32_ERR_UNKNOWN; +} + +/* + * some command receive reply frame with variable length, and length is + * embedded in reply frame itself. + * We can guess the length, but if we guess wrong the protocol gets out + * of sync. + * Use resync for frame oriented interfaces (e.g. I2C) and byte-by-byte + * read for byte oriented interfaces (e.g. UART). + * + * to run safely, data buffer should be allocated for 256+1 bytes + * + * len is value of the first byte in the frame. + */ +static stm32_err_t stm32_guess_len_cmd(const stm32_t *stm, uint8_t cmd, + uint8_t *data, unsigned int len) +{ + struct port_interface *port = stm->port; + port_err_t p_err; + + if (stm32_send_command(stm, cmd) != STM32_ERR_OK) + return STM32_ERR_UNKNOWN; + if (port->flags & PORT_BYTE) { + /* interface is UART-like */ + p_err = port->read(port, data, 1); + if (p_err != PORT_ERR_OK) + return STM32_ERR_UNKNOWN; + len = data[0]; + p_err = port->read(port, data + 1, len + 1); + if (p_err != PORT_ERR_OK) + return STM32_ERR_UNKNOWN; + return STM32_ERR_OK; + } + + p_err = port->read(port, data, len + 2); + if (p_err == PORT_ERR_OK && len == data[0]) + return STM32_ERR_OK; + if (p_err != PORT_ERR_OK) { + /* restart with only one byte */ + if (stm32_resync(stm) != STM32_ERR_OK) + return STM32_ERR_UNKNOWN; + if (stm32_send_command(stm, cmd) != STM32_ERR_OK) + return STM32_ERR_UNKNOWN; + p_err = port->read(port, data, 1); + if (p_err != PORT_ERR_OK) + return STM32_ERR_UNKNOWN; + } + + fprintf(stderr, "Re sync (len = %d)\n", data[0]); + if (stm32_resync(stm) != STM32_ERR_OK) + return STM32_ERR_UNKNOWN; + + len = data[0]; + if (stm32_send_command(stm, cmd) != STM32_ERR_OK) + return STM32_ERR_UNKNOWN; + p_err = port->read(port, data, len + 2); + if (p_err != PORT_ERR_OK) + return STM32_ERR_UNKNOWN; + return STM32_ERR_OK; +} + +/* + * Some interface, e.g. UART, requires a specific init sequence to let STM32 + * autodetect the interface speed. + * The sequence is only required one time after reset. + * stm32flash has command line flag "-c" to prevent sending the init sequence + * in case it was already sent before. + * User can easily forget adding "-c". In this case the bootloader would + * interpret the init sequence as part of a command message, then waiting for + * the rest of the message blocking the interface. + * This function sends the init sequence and, in case of timeout, recovers + * the interface. + */ +static stm32_err_t stm32_send_init_seq(const stm32_t *stm) +{ + struct port_interface *port = stm->port; + port_err_t p_err; + uint8_t byte, cmd = STM32_CMD_INIT; + + p_err = port->write(port, &cmd, 1); + if (p_err != PORT_ERR_OK) { + fprintf(stderr, "Failed to send init to device\n"); + return STM32_ERR_UNKNOWN; + } + p_err = port->read(port, &byte, 1); + if (p_err == PORT_ERR_OK && byte == STM32_ACK) + return STM32_ERR_OK; + if (p_err == PORT_ERR_OK && byte == STM32_NACK) { + /* We could get error later, but let's continue, for now. */ + fprintf(stderr, + "Warning: the interface was not closed properly.\n"); + return STM32_ERR_OK; + } + if (p_err != PORT_ERR_TIMEDOUT) { + fprintf(stderr, "Failed to init device.\n"); + return STM32_ERR_UNKNOWN; + } + + /* + * Check if previous STM32_CMD_INIT was taken as first byte + * of a command. Send a new byte, we should get back a NACK. + */ + p_err = port->write(port, &cmd, 1); + if (p_err != PORT_ERR_OK) { + fprintf(stderr, "Failed to send init to device\n"); + return STM32_ERR_UNKNOWN; + } + p_err = port->read(port, &byte, 1); + if (p_err == PORT_ERR_OK && byte == STM32_NACK) + return STM32_ERR_OK; + fprintf(stderr, "Failed to init device.\n"); + return STM32_ERR_UNKNOWN; +} + +/* find newer command by higher code */ +#define newer(prev, a) (((prev) == STM32_CMD_ERR) \ + ? (a) \ + : (((prev) > (a)) ? (prev) : (a))) + +stm32_t *stm32_init(struct port_interface *port, const char init) +{ + uint8_t len, val, buf[257]; + stm32_t *stm; + int i, new_cmds; + + stm = calloc(sizeof(stm32_t), 1); + stm->cmd = malloc(sizeof(stm32_cmd_t)); + memset(stm->cmd, STM32_CMD_ERR, sizeof(stm32_cmd_t)); + stm->port = port; + + if ((port->flags & PORT_CMD_INIT) && init) + if (stm32_send_init_seq(stm) != STM32_ERR_OK) + return NULL; + + /* get the version and read protection status */ + if (stm32_send_command(stm, STM32_CMD_GVR) != STM32_ERR_OK) { + stm32_close(stm); + return NULL; + } + + /* From AN, only UART bootloader returns 3 bytes */ + len = (port->flags & PORT_GVR_ETX) ? 3 : 1; + if (port->read(port, buf, len) != PORT_ERR_OK) + return NULL; + stm->version = buf[0]; + stm->option1 = (port->flags & PORT_GVR_ETX) ? buf[1] : 0; + stm->option2 = (port->flags & PORT_GVR_ETX) ? buf[2] : 0; + if (stm32_get_ack(stm) != STM32_ERR_OK) { + stm32_close(stm); + return NULL; + } + + /* get the bootloader information */ + len = STM32_CMD_GET_LENGTH; + if (port->cmd_get_reply) + for (i = 0; port->cmd_get_reply[i].length; i++) + if (stm->version == port->cmd_get_reply[i].version) { + len = port->cmd_get_reply[i].length; + break; + } + if (stm32_guess_len_cmd(stm, STM32_CMD_GET, buf, len) != STM32_ERR_OK) + return NULL; + len = buf[0] + 1; + stm->bl_version = buf[1]; + new_cmds = 0; + for (i = 1; i < len; i++) { + val = buf[i + 1]; + switch (val) { + case STM32_CMD_GET: + stm->cmd->get = val; break; + case STM32_CMD_GVR: + stm->cmd->gvr = val; break; + case STM32_CMD_GID: + stm->cmd->gid = val; break; + case STM32_CMD_RM: + stm->cmd->rm = val; break; + case STM32_CMD_GO: + stm->cmd->go = val; break; + case STM32_CMD_WM: + case STM32_CMD_WM_NS: + stm->cmd->wm = newer(stm->cmd->wm, val); + break; + case STM32_CMD_ER: + case STM32_CMD_EE: + case STM32_CMD_EE_NS: + stm->cmd->er = newer(stm->cmd->er, val); + break; + case STM32_CMD_WP: + case STM32_CMD_WP_NS: + stm->cmd->wp = newer(stm->cmd->wp, val); + break; + case STM32_CMD_UW: + case STM32_CMD_UW_NS: + stm->cmd->uw = newer(stm->cmd->uw, val); + break; + case STM32_CMD_RP: + case STM32_CMD_RP_NS: + stm->cmd->rp = newer(stm->cmd->rp, val); + break; + case STM32_CMD_UR: + case STM32_CMD_UR_NS: + stm->cmd->ur = newer(stm->cmd->ur, val); + break; + case STM32_CMD_CRC: + stm->cmd->crc = newer(stm->cmd->crc, val); + break; + default: + if (new_cmds++ == 0) + fprintf(stderr, + "GET returns unknown commands (0x%2x", + val); + else + fprintf(stderr, ", 0x%2x", val); + } + } + if (new_cmds) + fprintf(stderr, ")\n"); + if (stm32_get_ack(stm) != STM32_ERR_OK) { + stm32_close(stm); + return NULL; + } + + if (stm->cmd->get == STM32_CMD_ERR + || stm->cmd->gvr == STM32_CMD_ERR + || stm->cmd->gid == STM32_CMD_ERR) { + fprintf(stderr, "Error: bootloader did not returned correct information from GET command\n"); + return NULL; + } + + /* get the device ID */ + if (stm32_guess_len_cmd(stm, stm->cmd->gid, buf, 1) != STM32_ERR_OK) { + stm32_close(stm); + return NULL; + } + len = buf[0] + 1; + if (len < 2) { + stm32_close(stm); + fprintf(stderr, "Only %d bytes sent in the PID, unknown/unsupported device\n", len); + return NULL; + } + stm->pid = (buf[1] << 8) | buf[2]; + if (len > 2) { + fprintf(stderr, "This bootloader returns %d extra bytes in PID:", len); + for (i = 2; i <= len ; i++) + fprintf(stderr, " %02x", buf[i]); + fprintf(stderr, "\n"); + } + if (stm32_get_ack(stm) != STM32_ERR_OK) { + stm32_close(stm); + return NULL; + } + + stm->dev = devices; + while (stm->dev->id != 0x00 && stm->dev->id != stm->pid) + ++stm->dev; + + if (!stm->dev->id) { + fprintf(stderr, "Unknown/unsupported device (Device ID: 0x%03x)\n", stm->pid); + stm32_close(stm); + return NULL; + } + + return stm; +} + +void stm32_close(stm32_t *stm) +{ + if (stm) + free(stm->cmd); + free(stm); +} + +stm32_err_t stm32_read_memory(const stm32_t *stm, uint32_t address, + uint8_t data[], unsigned int len) +{ + struct port_interface *port = stm->port; + uint8_t buf[5]; + + if (!len) + return STM32_ERR_OK; + + if (len > 256) { + fprintf(stderr, "Error: READ length limit at 256 bytes\n"); + return STM32_ERR_UNKNOWN; + } + + if (stm->cmd->rm == STM32_CMD_ERR) { + fprintf(stderr, "Error: READ command not implemented in bootloader.\n"); + return STM32_ERR_NO_CMD; + } + + if (stm32_send_command(stm, stm->cmd->rm) != STM32_ERR_OK) + return STM32_ERR_UNKNOWN; + + buf[0] = address >> 24; + buf[1] = (address >> 16) & 0xFF; + buf[2] = (address >> 8) & 0xFF; + buf[3] = address & 0xFF; + buf[4] = buf[0] ^ buf[1] ^ buf[2] ^ buf[3]; + if (port->write(port, buf, 5) != PORT_ERR_OK) + return STM32_ERR_UNKNOWN; + if (stm32_get_ack(stm) != STM32_ERR_OK) + return STM32_ERR_UNKNOWN; + + if (stm32_send_command(stm, len - 1) != STM32_ERR_OK) + return STM32_ERR_UNKNOWN; + + if (port->read(port, data, len) != PORT_ERR_OK) + return STM32_ERR_UNKNOWN; + + return STM32_ERR_OK; +} + +stm32_err_t stm32_write_memory(const stm32_t *stm, uint32_t address, + const uint8_t data[], unsigned int len) +{ + struct port_interface *port = stm->port; + uint8_t cs, buf[256 + 2]; + unsigned int i, aligned_len; + stm32_err_t s_err; + + if (!len) + return STM32_ERR_OK; + + if (len > 256) { + fprintf(stderr, "Error: READ length limit at 256 bytes\n"); + return STM32_ERR_UNKNOWN; + } + + /* must be 32bit aligned */ + if (address & 0x3 || len & 0x3) { + fprintf(stderr, "Error: WRITE address and length must be 4 byte aligned\n"); + return STM32_ERR_UNKNOWN; + } + + if (stm->cmd->wm == STM32_CMD_ERR) { + fprintf(stderr, "Error: WRITE command not implemented in bootloader.\n"); + return STM32_ERR_NO_CMD; + } + + /* send the address and checksum */ + if (stm32_send_command(stm, stm->cmd->wm) != STM32_ERR_OK) + return STM32_ERR_UNKNOWN; + + buf[0] = address >> 24; + buf[1] = (address >> 16) & 0xFF; + buf[2] = (address >> 8) & 0xFF; + buf[3] = address & 0xFF; + buf[4] = buf[0] ^ buf[1] ^ buf[2] ^ buf[3]; + if (port->write(port, buf, 5) != PORT_ERR_OK) + return STM32_ERR_UNKNOWN; + if (stm32_get_ack(stm) != STM32_ERR_OK) + return STM32_ERR_UNKNOWN; + + aligned_len = (len + 3) & ~3; + cs = aligned_len - 1; + buf[0] = aligned_len - 1; + for (i = 0; i < len; i++) { + cs ^= data[i]; + buf[i + 1] = data[i]; + } + /* padding data */ + for (i = len; i < aligned_len; i++) { + cs ^= 0xFF; + buf[i + 1] = 0xFF; + } + buf[aligned_len + 1] = cs; + if (port->write(port, buf, aligned_len + 2) != PORT_ERR_OK) + return STM32_ERR_UNKNOWN; + + s_err = stm32_get_ack_timeout(stm, STM32_BLKWRITE_TIMEOUT); + if (s_err != STM32_ERR_OK) { + if (port->flags & PORT_STRETCH_W + && stm->cmd->wm != STM32_CMD_WM_NS) + stm32_warn_stretching("write"); + return STM32_ERR_UNKNOWN; + } + return STM32_ERR_OK; +} + +stm32_err_t stm32_wunprot_memory(const stm32_t *stm) +{ + struct port_interface *port = stm->port; + stm32_err_t s_err; + + if (stm->cmd->uw == STM32_CMD_ERR) { + fprintf(stderr, "Error: WRITE UNPROTECT command not implemented in bootloader.\n"); + return STM32_ERR_NO_CMD; + } + + if (stm32_send_command(stm, stm->cmd->uw) != STM32_ERR_OK) + return STM32_ERR_UNKNOWN; + + s_err = stm32_get_ack_timeout(stm, STM32_WUNPROT_TIMEOUT); + if (s_err == STM32_NACK) { + fprintf(stderr, "Error: Failed to WRITE UNPROTECT\n"); + return STM32_ERR_UNKNOWN; + } + if (s_err != STM32_ERR_OK) { + if (port->flags & PORT_STRETCH_W + && stm->cmd->uw != STM32_CMD_UW_NS) + stm32_warn_stretching("WRITE UNPROTECT"); + return STM32_ERR_UNKNOWN; + } + return STM32_ERR_OK; +} + +stm32_err_t stm32_wprot_memory(const stm32_t *stm) +{ + struct port_interface *port = stm->port; + stm32_err_t s_err; + + if (stm->cmd->wp == STM32_CMD_ERR) { + fprintf(stderr, "Error: WRITE PROTECT command not implemented in bootloader.\n"); + return STM32_ERR_NO_CMD; + } + + if (stm32_send_command(stm, stm->cmd->wp) != STM32_ERR_OK) + return STM32_ERR_UNKNOWN; + + s_err = stm32_get_ack_timeout(stm, STM32_WPROT_TIMEOUT); + if (s_err == STM32_NACK) { + fprintf(stderr, "Error: Failed to WRITE PROTECT\n"); + return STM32_ERR_UNKNOWN; + } + if (s_err != STM32_ERR_OK) { + if (port->flags & PORT_STRETCH_W + && stm->cmd->wp != STM32_CMD_WP_NS) + stm32_warn_stretching("WRITE PROTECT"); + return STM32_ERR_UNKNOWN; + } + return STM32_ERR_OK; +} + +stm32_err_t stm32_runprot_memory(const stm32_t *stm) +{ + struct port_interface *port = stm->port; + stm32_err_t s_err; + + if (stm->cmd->ur == STM32_CMD_ERR) { + fprintf(stderr, "Error: READOUT UNPROTECT command not implemented in bootloader.\n"); + return STM32_ERR_NO_CMD; + } + + if (stm32_send_command(stm, stm->cmd->ur) != STM32_ERR_OK) + return STM32_ERR_UNKNOWN; + + s_err = stm32_get_ack_timeout(stm, STM32_MASSERASE_TIMEOUT); + if (s_err == STM32_NACK) { + fprintf(stderr, "Error: Failed to READOUT UNPROTECT\n"); + return STM32_ERR_UNKNOWN; + } + if (s_err != STM32_ERR_OK) { + if (port->flags & PORT_STRETCH_W + && stm->cmd->ur != STM32_CMD_UR_NS) + stm32_warn_stretching("READOUT UNPROTECT"); + return STM32_ERR_UNKNOWN; + } + return STM32_ERR_OK; +} + +stm32_err_t stm32_readprot_memory(const stm32_t *stm) +{ + struct port_interface *port = stm->port; + stm32_err_t s_err; + + if (stm->cmd->rp == STM32_CMD_ERR) { + fprintf(stderr, "Error: READOUT PROTECT command not implemented in bootloader.\n"); + return STM32_ERR_NO_CMD; + } + + if (stm32_send_command(stm, stm->cmd->rp) != STM32_ERR_OK) + return STM32_ERR_UNKNOWN; + + s_err = stm32_get_ack_timeout(stm, STM32_RPROT_TIMEOUT); + if (s_err == STM32_NACK) { + fprintf(stderr, "Error: Failed to READOUT PROTECT\n"); + return STM32_ERR_UNKNOWN; + } + if (s_err != STM32_ERR_OK) { + if (port->flags & PORT_STRETCH_W + && stm->cmd->rp != STM32_CMD_RP_NS) + stm32_warn_stretching("READOUT PROTECT"); + return STM32_ERR_UNKNOWN; + } + return STM32_ERR_OK; +} + +stm32_err_t stm32_erase_memory(const stm32_t *stm, uint8_t spage, uint8_t pages) +{ + struct port_interface *port = stm->port; + stm32_err_t s_err; + port_err_t p_err; + + if (!pages) + return STM32_ERR_OK; + + if (stm->cmd->er == STM32_CMD_ERR) { + fprintf(stderr, "Error: ERASE command not implemented in bootloader.\n"); + return STM32_ERR_NO_CMD; + } + + if (stm32_send_command(stm, stm->cmd->er) != STM32_ERR_OK) { + fprintf(stderr, "Can't initiate chip erase!\n"); + return STM32_ERR_UNKNOWN; + } + + /* The erase command reported by the bootloader is either 0x43, 0x44 or 0x45 */ + /* 0x44 is Extended Erase, a 2 byte based protocol and needs to be handled differently. */ + /* 0x45 is clock no-stretching version of Extended Erase for I2C port. */ + if (stm->cmd->er != STM32_CMD_ER) { + /* Not all chips using Extended Erase support mass erase */ + /* Currently known as not supporting mass erase is the Ultra Low Power STM32L15xx range */ + /* So if someone has not overridden the default, but uses one of these chips, take it out of */ + /* mass erase mode, so it will be done page by page. This maximum might not be correct either! */ + if (stm->pid == 0x416 && pages == 0xFF) + pages = 0xF8; /* works for the STM32L152RB with 128Kb flash */ + + if (pages == 0xFF) { + uint8_t buf[3]; + + /* 0xFFFF the magic number for mass erase */ + buf[0] = 0xFF; + buf[1] = 0xFF; + buf[2] = 0x00; /* checksum */ + if (port->write(port, buf, 3) != PORT_ERR_OK) { + fprintf(stderr, "Mass erase error.\n"); + return STM32_ERR_UNKNOWN; + } + s_err = stm32_get_ack_timeout(stm, STM32_MASSERASE_TIMEOUT); + if (s_err != STM32_ERR_OK) { + fprintf(stderr, "Mass erase failed. Try specifying the number of pages to be erased.\n"); + if (port->flags & PORT_STRETCH_W + && stm->cmd->er != STM32_CMD_EE_NS) + stm32_warn_stretching("erase"); + return STM32_ERR_UNKNOWN; + } + return STM32_ERR_OK; + } + + uint16_t pg_num; + uint8_t pg_byte; + uint8_t cs = 0; + uint8_t *buf; + int i = 0; + + buf = malloc(2 + 2 * pages + 1); + if (!buf) + return STM32_ERR_UNKNOWN; + + /* Number of pages to be erased - 1, two bytes, MSB first */ + pg_byte = (pages - 1) >> 8; + buf[i++] = pg_byte; + cs ^= pg_byte; + pg_byte = (pages - 1) & 0xFF; + buf[i++] = pg_byte; + cs ^= pg_byte; + + for (pg_num = spage; pg_num < spage + pages; pg_num++) { + pg_byte = pg_num >> 8; + cs ^= pg_byte; + buf[i++] = pg_byte; + pg_byte = pg_num & 0xFF; + cs ^= pg_byte; + buf[i++] = pg_byte; + } + buf[i++] = cs; + p_err = port->write(port, buf, i); + free(buf); + if (p_err != PORT_ERR_OK) { + fprintf(stderr, "Page-by-page erase error.\n"); + return STM32_ERR_UNKNOWN; + } + + s_err = stm32_get_ack_timeout(stm, pages * STM32_SECTERASE_TIMEOUT); + if (s_err != STM32_ERR_OK) { + fprintf(stderr, "Page-by-page erase failed. Check the maximum pages your device supports.\n"); + if (port->flags & PORT_STRETCH_W + && stm->cmd->er != STM32_CMD_EE_NS) + stm32_warn_stretching("erase"); + return STM32_ERR_UNKNOWN; + } + + return STM32_ERR_OK; + } + + /* And now the regular erase (0x43) for all other chips */ + if (pages == 0xFF) { + s_err = stm32_send_command_timeout(stm, 0xFF, STM32_MASSERASE_TIMEOUT); + if (s_err != STM32_ERR_OK) { + if (port->flags & PORT_STRETCH_W) + stm32_warn_stretching("erase"); + return STM32_ERR_UNKNOWN; + } + return STM32_ERR_OK; + } else { + uint8_t cs = 0; + uint8_t pg_num; + uint8_t *buf; + int i = 0; + + buf = malloc(1 + pages + 1); + if (!buf) + return STM32_ERR_UNKNOWN; + + buf[i++] = pages - 1; + cs ^= (pages-1); + for (pg_num = spage; pg_num < (pages + spage); pg_num++) { + buf[i++] = pg_num; + cs ^= pg_num; + } + buf[i++] = cs; + p_err = port->write(port, buf, i); + free(buf); + if (p_err != PORT_ERR_OK) { + fprintf(stderr, "Erase failed.\n"); + return STM32_ERR_UNKNOWN; + } + s_err = stm32_get_ack_timeout(stm, STM32_MASSERASE_TIMEOUT); + if (s_err != STM32_ERR_OK) { + if (port->flags & PORT_STRETCH_W) + stm32_warn_stretching("erase"); + return STM32_ERR_UNKNOWN; + } + return STM32_ERR_OK; + } +} + +static stm32_err_t stm32_run_raw_code(const stm32_t *stm, + uint32_t target_address, + const uint8_t *code, uint32_t code_size) +{ + uint32_t stack_le = le_u32(0x20002000); + uint32_t code_address_le = le_u32(target_address + 8); + uint32_t length = code_size + 8; + uint8_t *mem, *pos; + uint32_t address, w; + + /* Must be 32-bit aligned */ + if (target_address & 0x3) { + fprintf(stderr, "Error: code address must be 4 byte aligned\n"); + return STM32_ERR_UNKNOWN; + } + + mem = malloc(length); + if (!mem) + return STM32_ERR_UNKNOWN; + + memcpy(mem, &stack_le, sizeof(uint32_t)); + memcpy(mem + 4, &code_address_le, sizeof(uint32_t)); + memcpy(mem + 8, code, code_size); + + pos = mem; + address = target_address; + while (length > 0) { + w = length > 256 ? 256 : length; + if (stm32_write_memory(stm, address, pos, w) != STM32_ERR_OK) { + free(mem); + return STM32_ERR_UNKNOWN; + } + + address += w; + pos += w; + length -= w; + } + + free(mem); + return stm32_go(stm, target_address); +} + +stm32_err_t stm32_go(const stm32_t *stm, uint32_t address) +{ + struct port_interface *port = stm->port; + uint8_t buf[5]; + + if (stm->cmd->go == STM32_CMD_ERR) { + fprintf(stderr, "Error: GO command not implemented in bootloader.\n"); + return STM32_ERR_NO_CMD; + } + + if (stm32_send_command(stm, stm->cmd->go) != STM32_ERR_OK) + return STM32_ERR_UNKNOWN; + + buf[0] = address >> 24; + buf[1] = (address >> 16) & 0xFF; + buf[2] = (address >> 8) & 0xFF; + buf[3] = address & 0xFF; + buf[4] = buf[0] ^ buf[1] ^ buf[2] ^ buf[3]; + if (port->write(port, buf, 5) != PORT_ERR_OK) + return STM32_ERR_UNKNOWN; + + if (stm32_get_ack(stm) != STM32_ERR_OK) + return STM32_ERR_UNKNOWN; + return STM32_ERR_OK; +} + +stm32_err_t stm32_reset_device(const stm32_t *stm) +{ + uint32_t target_address = stm->dev->ram_start; + + return stm32_run_raw_code(stm, target_address, stm_reset_code, stm_reset_code_length); +} + +stm32_err_t stm32_crc_memory(const stm32_t *stm, uint32_t address, + uint32_t length, uint32_t *crc) +{ + struct port_interface *port = stm->port; + uint8_t buf[5]; + + if (address & 0x3 || length & 0x3) { + fprintf(stderr, "Start and end addresses must be 4 byte aligned\n"); + return STM32_ERR_UNKNOWN; + } + + if (stm->cmd->crc == STM32_CMD_ERR) { + fprintf(stderr, "Error: CRC command not implemented in bootloader.\n"); + return STM32_ERR_NO_CMD; + } + + if (stm32_send_command(stm, stm->cmd->crc) != STM32_ERR_OK) + return STM32_ERR_UNKNOWN; + + buf[0] = address >> 24; + buf[1] = (address >> 16) & 0xFF; + buf[2] = (address >> 8) & 0xFF; + buf[3] = address & 0xFF; + buf[4] = buf[0] ^ buf[1] ^ buf[2] ^ buf[3]; + if (port->write(port, buf, 5) != PORT_ERR_OK) + return STM32_ERR_UNKNOWN; + + if (stm32_get_ack(stm) != STM32_ERR_OK) + return STM32_ERR_UNKNOWN; + + buf[0] = length >> 24; + buf[1] = (length >> 16) & 0xFF; + buf[2] = (length >> 8) & 0xFF; + buf[3] = length & 0xFF; + buf[4] = buf[0] ^ buf[1] ^ buf[2] ^ buf[3]; + if (port->write(port, buf, 5) != PORT_ERR_OK) + return STM32_ERR_UNKNOWN; + + if (stm32_get_ack(stm) != STM32_ERR_OK) + return STM32_ERR_UNKNOWN; + + if (stm32_get_ack(stm) != STM32_ERR_OK) + return STM32_ERR_UNKNOWN; + + if (port->read(port, buf, 5) != PORT_ERR_OK) + return STM32_ERR_UNKNOWN; + + if (buf[4] != (buf[0] ^ buf[1] ^ buf[2] ^ buf[3])) + return STM32_ERR_UNKNOWN; + + *crc = (buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3]; + return STM32_ERR_OK; +} + +/* + * CRC computed by STM32 is similar to the standard crc32_be() + * implemented, for example, in Linux kernel in ./lib/crc32.c + * But STM32 computes it on units of 32 bits word and swaps the + * bytes of the word before the computation. + * Due to byte swap, I cannot use any CRC available in existing + * libraries, so here is a simple not optimized implementation. + */ +#define CRCPOLY_BE 0x04c11db7 +#define CRC_MSBMASK 0x80000000 +#define CRC_INIT_VALUE 0xFFFFFFFF +uint32_t stm32_sw_crc(uint32_t crc, uint8_t *buf, unsigned int len) +{ + int i; + uint32_t data; + + if (len & 0x3) { + fprintf(stderr, "Buffer length must be multiple of 4 bytes\n"); + return 0; + } + + while (len) { + data = *buf++; + data |= *buf++ << 8; + data |= *buf++ << 16; + data |= *buf++ << 24; + len -= 4; + + crc ^= data; + + for (i = 0; i < 32; i++) + if (crc & CRC_MSBMASK) + crc = (crc << 1) ^ CRCPOLY_BE; + else + crc = (crc << 1); + } + return crc; +} + +stm32_err_t stm32_crc_wrapper(const stm32_t *stm, uint32_t address, + uint32_t length, uint32_t *crc) +{ + uint8_t buf[256]; + uint32_t start, total_len, len, current_crc; + + if (address & 0x3 || length & 0x3) { + fprintf(stderr, "Start and end addresses must be 4 byte aligned\n"); + return STM32_ERR_UNKNOWN; + } + + if (stm->cmd->crc != STM32_CMD_ERR) + return stm32_crc_memory(stm, address, length, crc); + + start = address; + total_len = length; + current_crc = CRC_INIT_VALUE; + while (length) { + len = length > 256 ? 256 : length; + if (stm32_read_memory(stm, address, buf, len) != STM32_ERR_OK) { + fprintf(stderr, + "Failed to read memory at address 0x%08x, target write-protected?\n", + address); + return STM32_ERR_UNKNOWN; + } + current_crc = stm32_sw_crc(current_crc, buf, len); + length -= len; + address += len; + + fprintf(stderr, + "\rCRC address 0x%08x (%.2f%%) ", + address, + (100.0f / (float)total_len) * (float)(address - start) + ); + fflush(stderr); + } + fprintf(stderr, "Done.\n"); + *crc = current_crc; + return STM32_ERR_OK; +} diff --git a/tools/linux64/src/stm32flash_serial/src/stm32.h b/tools/linux64/src/stm32flash_serial/src/stm32.h new file mode 100644 index 0000000..1688fcb --- /dev/null +++ b/tools/linux64/src/stm32flash_serial/src/stm32.h @@ -0,0 +1,84 @@ +/* + stm32flash - Open Source ST STM32 flash program for *nix + Copyright (C) 2010 Geoffrey McRae + + 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. +*/ + + +#ifndef _STM32_H +#define _STM32_H + +#include +#include "serial.h" + +#define STM32_MAX_RX_FRAME 256 /* cmd read memory */ +#define STM32_MAX_TX_FRAME (1 + 256 + 1) /* cmd write memory */ + +typedef enum { + STM32_ERR_OK = 0, + STM32_ERR_UNKNOWN, /* Generic error */ + STM32_ERR_NACK, + STM32_ERR_NO_CMD, /* Command not available in bootloader */ +} stm32_err_t; + +typedef struct stm32 stm32_t; +typedef struct stm32_cmd stm32_cmd_t; +typedef struct stm32_dev stm32_dev_t; + +struct stm32 { + const serial_t *serial; + struct port_interface *port; + uint8_t bl_version; + uint8_t version; + uint8_t option1, option2; + uint16_t pid; + stm32_cmd_t *cmd; + const stm32_dev_t *dev; +}; + +struct stm32_dev { + uint16_t id; + const char *name; + uint32_t ram_start, ram_end; + uint32_t fl_start, fl_end; + uint16_t fl_pps; // pages per sector + uint16_t fl_ps; // page size + uint32_t opt_start, opt_end; + uint32_t mem_start, mem_end; +}; + +stm32_t *stm32_init(struct port_interface *port, const char init); +void stm32_close(stm32_t *stm); +stm32_err_t stm32_read_memory(const stm32_t *stm, uint32_t address, + uint8_t data[], unsigned int len); +stm32_err_t stm32_write_memory(const stm32_t *stm, uint32_t address, + const uint8_t data[], unsigned int len); +stm32_err_t stm32_wunprot_memory(const stm32_t *stm); +stm32_err_t stm32_wprot_memory(const stm32_t *stm); +stm32_err_t stm32_erase_memory(const stm32_t *stm, uint8_t spage, + uint8_t pages); +stm32_err_t stm32_go(const stm32_t *stm, uint32_t address); +stm32_err_t stm32_reset_device(const stm32_t *stm); +stm32_err_t stm32_readprot_memory(const stm32_t *stm); +stm32_err_t stm32_runprot_memory(const stm32_t *stm); +stm32_err_t stm32_crc_memory(const stm32_t *stm, uint32_t address, + uint32_t length, uint32_t *crc); +stm32_err_t stm32_crc_wrapper(const stm32_t *stm, uint32_t address, + uint32_t length, uint32_t *crc); +uint32_t stm32_sw_crc(uint32_t crc, uint8_t *buf, unsigned int len); + +#endif + diff --git a/tools/linux64/src/stm32flash_serial/src/stm32flash.1 b/tools/linux64/src/stm32flash_serial/src/stm32flash.1 new file mode 100644 index 0000000..d37292f --- /dev/null +++ b/tools/linux64/src/stm32flash_serial/src/stm32flash.1 @@ -0,0 +1,407 @@ +.TH STM32FLASH 1 "2013\-11\-03" STM32FLASH "User command" +.SH NAME +stm32flash \- flashing utility for STM32 and STM32W through UART or I2C +.SH SYNOPSIS +.B stm32flash +.RB [ \-cfhjkouvCR ] +.RB [ \-a +.IR bus_address ] +.RB [ \-b +.IR baud_rate ] +.RB [ \-m +.IR serial_mode ] +.RB [ \-r +.IR filename ] +.RB [ \-w +.IR filename ] +.RB [ \-e +.IR num ] +.RB [ \-n +.IR count ] +.RB [ \-g +.IR address ] +.RB [ \-s +.IR start_page ] +.RB [ \-S +.IR address [: length ]] +.RB [ \-F +.IR RX_length [: TX_length ]] +.RB [ \-i +.IR GPIO_string ] +.RI [ tty_device +.R | +.IR i2c_device ] + +.SH DESCRIPTION +.B stm32flash +reads or writes the flash memory of STM32 and STM32W. + +It requires the STM32[W] to embed a bootloader compliant with ST +application note AN3155. +.B stm32flash +uses the serial port +.I tty_device +to interact with the bootloader of STM32[W]. + +.SH OPTIONS +.TP +.BI "\-a" " bus_address" +Specify address on bus for +.IR i2c_device . +This option is mandatory for I2C interface. + +.TP +.BI "\-b" " baud_rate" +Specify baud rate speed of +.IR tty_device . +Please notice that the ST bootloader can automatically detect the baud rate, +as explaned in chapter 2 of AN3155. +This option could be required together with option +.B "\-c" +or if following interaction with bootloader is expected. +Default is +.IR 57600 . + +.TP +.BI "\-m" " mode" +Specify the format of UART data. +.I mode +is a three characters long string where each character specifies, in +this strict order, character size, parity and stop bits. +The only values currenly used are +.I 8e1 +for standard STM32 bootloader and +.I 8n1 +for standard STM32W bootloader. +Default is +.IR 8e1 . + +.TP +.BI "\-r" " filename" +Specify to read the STM32[W] flash and write its content in +.I filename +in raw binary format (see below +.BR "FORMAT CONVERSION" ). + +.TP +.BI "\-w" " filename" +Specify to write the STM32[W] flash with the content of +.IR filename . +File format can be either raw binary or intel hex (see below +.BR "FORMAT CONVERSION" ). +The file format is automatically detected. +To by\-pass format detection and force binary mode (e.g. to +write an intel hex content in STM32[W] flash), use +.B \-f +option. + +.TP +.B \-u +Specify to disable write\-protection from STM32[W] flash. +The STM32[W] will be reset after this operation. + +.TP +.B \-j +Enable the flash read\-protection. + +.TP +.B \-k +Disable the flash read\-protection. + +.TP +.B \-o +Erase only. + +.TP +.BI "\-e" " num" +Specify to erase only +.I num +pages before writing the flash. Default is to erase the whole flash. With +.B \-e 0 +the flash would not be erased. + +.TP +.B \-v +Specify to verify flash content after write operation. + +.TP +.BI "\-n" " count" +Specify to retry failed writes up to +.I count +times. Default is 10 times. + +.TP +.BI "\-g" " address" +Specify address to start execution from (0 = flash start). + +.TP +.BI "\-s" " start_page" +Specify flash page offset (0 = flash start). + +.TP +.BI "\-S" " address" "[:" "length" "]" +Specify start address and optionally length for read/write/erase/crc operations. + +.TP +.BI "\-F" " RX_length" "[:" "TX_length" "]" +Specify the maximum frame size for the current interface. +Due to STM32 bootloader protocol, host will never handle frames bigger than +256 byte in RX or 258 byte in TX. +Due to current code, lowest limit in RX is 20 byte (to read a complete reply +of command GET). Minimum limit in TX is 5 byte, required by protocol. + +.TP +.B \-f +Force binary parser while reading file with +.BR "\-w" "." + +.TP +.B \-h +Show help. + +.TP +.B \-c +Specify to resume the existing UART connection and don't send initial +INIT sequence to detect baud rate. Baud rate must be kept the same as the +existing connection. This is useful if the reset fails. + +.TP +.BI "\-i" " GPIO_string" +Specify the GPIO sequences on the host to force STM32[W] to enter and +exit bootloader mode. GPIO can either be real GPIO connected from host to +STM32[W] beside the UART connection, or UART's modem signals used as +GPIO. (See below +.B BOOTLOADER GPIO SEQUENCE +for the format of +.I GPIO_string +and further explanation). + +.TP +.B \-C +Specify to compute CRC on memory content. +By default the CRC is computed on the whole flash content. +Use +.B "\-S" +to provide different memory address range. + +.TP +.B \-R +Specify to reset the device at exit. +This option is ignored if either +.BR "\-g" "," +.BR "\-j" "," +.B "\-k" +or +.B "\-u" +is also specified. + +.SH BOOTLOADER GPIO SEQUENCE +This feature is currently available on Linux host only. + +As explained in ST application note AN2606, after reset the STM32 will +execute either the application program in user flash or the bootloader, +depending on the level applied at specific pins of STM32 during reset. + +STM32 bootloader is automatically activated by configuring the pins +BOOT0="high" and BOOT1="low" and then by applying a reset. +Application program in user flash is activated by configuring the pin +BOOT0="low" (the level on BOOT1 is ignored) and then by applying a reset. + +When GPIO from host computer are connected to either configuration and +reset pins of STM32, +.B stm32flash +can control the host GPIO to reset STM32 and to force execution of +bootloader or execution of application program. + +The sequence of GPIO values to entry to and exit from bootloader mode is +provided with command line option +.B "\-i" +.IR "GPIO_string" . + +.PD 0 +The format of +.IR "GPIO_string" " is:" +.RS +GPIO_string = [entry sequence][:[exit sequence]] +.P +sequence = [\-]n[,sequence] +.RE +.P +In the above sequences, negative numbers correspond to GPIO at "low" level; +numbers without sign correspond to GPIO at "high" level. +The value "n" can either be the GPIO number on the host system or the +string "rts", "dtr" or "brk". The strings "rts" and "dtr" drive the +corresponding UART's modem lines RTS and DTR as GPIO. +The string "brk" forces the UART to send a BREAK sequence on TX line; +after BREAK the UART is returned in normal "non\-break" mode. +Note: the string "\-brk" has no effect and is ignored. +.PD + +.PD 0 +As example, let's suppose the following connection between host and STM32: +.IP \(bu 2 +host GPIO_3 connected to reset pin of STM32; +.IP \(bu 2 +host GPIO_4 connected to STM32 pin BOOT0; +.IP \(bu 2 +host GPIO_5 connected to STM32 pin BOOT1. +.PD +.P + +In this case, the sequence to enter in bootloader mode is: first put +GPIO_4="high" and GPIO_5="low"; then send reset pulse by GPIO_3="low" +followed by GPIO_3="high". +The corresponding string for +.I GPIO_string +is "4,\-5,\-3,3". + +To exit from bootloade and run the application program, the sequence is: +put GPIO_4="low"; then send reset pulse. +The corresponding string for +.I GPIO_string +is "\-4,\-3,3". + +The complete command line flag is "\-i 4,\-5,\-3,3:\-4,\-3,3". + +STM32W uses pad PA5 to select boot mode; if during reset PA5 is "low" then +STM32W will enter in bootloader mode; if PA5 is "high" it will execute the +program in flash. + +As example, supposing GPIO_3 connected to PA5 and GPIO_2 to STM32W's reset. +The command: +.PD 0 +.RS +stm32flash \-i \-3,\-2,2:3,\-2,2 /dev/ttyS0 +.RE +provides: +.IP \(bu 2 +entry sequence: GPIO_3=low, GPIO_2=low, GPIO_2=high +.IP \(bu 2 +exit sequence: GPIO_3=high, GPIO_2=low, GPIO_2=high +.PD + +.SH EXAMPLES +Get device information: +.RS +.PD 0 +.P +stm32flash /dev/ttyS0 +.PD +.RE + +Write with verify and then start execution: +.RS +.PD 0 +.P +stm32flash \-w filename \-v \-g 0x0 /dev/ttyS0 +.PD +.RE + +Read flash to file: +.RS +.PD 0 +.P +stm32flash \-r filename /dev/ttyS0 +.PD +.RE + +Start execution: +.RS +.PD 0 +.P +stm32flash \-g 0x0 /dev/ttyS0 +.PD +.RE + +Specify: +.PD 0 +.IP \(bu 2 +entry sequence: RTS=low, DTR=low, DTR=high +.IP \(bu 2 +exit sequence: RTS=high, DTR=low, DTR=high +.P +.RS +stm32flash \-i \-rts,\-dtr,dtr:rts,\-dtr,dtr /dev/ttyS0 +.PD +.RE + +.SH FORMAT CONVERSION +Flash images provided by ST or created with ST tools are often in file +format Motorola S\-Record. +Conversion between raw binary, intel hex and Motorola S\-Record can be +done through software package SRecord. + +.SH AUTHORS +The original software package +.B stm32flash +is written by +.I Geoffrey McRae +and is since 2012 maintained by +.IR "Tormod Volden " . + +Man page and extension to STM32W and I2C are written by +.IR "Antonio Borneo " . + +Please report any bugs at the project homepage +http://stm32flash.googlecode.com . + +.SH SEE ALSO +.BR "srec_cat" "(1)," " srec_intel" "(5)," " srec_motorola" "(5)." + +The communication protocol used by ST bootloader is documented in +following ST application notes, depending on communication port. +The current version of +.B stm32flash +only supports +.I UART +and +.I I2C +ports. +.PD 0 +.P +.IP \(bu 2 +AN3154: CAN protocol used in the STM32 bootloader +.P +.RS +http://www.st.com/web/en/resource/technical/document/application_note/CD00264321.pdf +.RE + +.P +.IP \(bu 2 +AN3155: USART protocol used in the STM32(TM) bootloader +.P +.RS +http://www.st.com/web/en/resource/technical/document/application_note/CD00264342.pdf +.RE + +.P +.IP \(bu 2 +AN4221: I2C protocol used in the STM32 bootloader +.P +.RS +http://www.st.com/web/en/resource/technical/document/application_note/DM00072315.pdf +.RE + +.P +.IP \(bu 2 +AN4286: SPI protocol used in the STM32 bootloader +.P +.RS +http://www.st.com/web/en/resource/technical/document/application_note/DM00081379.pdf +.RE + +.PD + + +Boot mode selection for STM32 is documented in ST application note +AN2606, available from the ST website: +.PD 0 +.P +http://www.st.com/web/en/resource/technical/document/application_note/CD00167594.pdf +.PD + +.SH LICENSE +.B stm32flash +is distributed under GNU GENERAL PUBLIC LICENSE Version 2. +Copy of the license is available within the source code in the file +.IR "gpl\-2.0.txt" . diff --git a/tools/linux64/src/stm32flash_serial/src/utils.c b/tools/linux64/src/stm32flash_serial/src/utils.c new file mode 100644 index 0000000..271bb3e --- /dev/null +++ b/tools/linux64/src/stm32flash_serial/src/utils.c @@ -0,0 +1,45 @@ +/* + stm32flash - Open Source ST STM32 flash program for *nix + Copyright (C) 2010 Geoffrey McRae + + 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. +*/ + +#include +#include "utils.h" + +/* detect CPU endian */ +char cpu_le() { + const uint32_t cpu_le_test = 0x12345678; + return ((const unsigned char*)&cpu_le_test)[0] == 0x78; +} + +uint32_t be_u32(const uint32_t v) { + if (cpu_le()) + return ((v & 0xFF000000) >> 24) | + ((v & 0x00FF0000) >> 8) | + ((v & 0x0000FF00) << 8) | + ((v & 0x000000FF) << 24); + return v; +} + +uint32_t le_u32(const uint32_t v) { + if (!cpu_le()) + return ((v & 0xFF000000) >> 24) | + ((v & 0x00FF0000) >> 8) | + ((v & 0x0000FF00) << 8) | + ((v & 0x000000FF) << 24); + return v; +} diff --git a/tools/linux64/src/stm32flash_serial/src/utils.h b/tools/linux64/src/stm32flash_serial/src/utils.h new file mode 100644 index 0000000..a8d37d2 --- /dev/null +++ b/tools/linux64/src/stm32flash_serial/src/utils.h @@ -0,0 +1,30 @@ +/* + stm32flash - Open Source ST STM32 flash program for *nix + Copyright (C) 2010 Geoffrey McRae + + 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. +*/ + + +#ifndef _H_UTILS +#define _H_UTILS + +#include + +char cpu_le(); +uint32_t be_u32(const uint32_t v); +uint32_t le_u32(const uint32_t v); + +#endif diff --git a/tools/linux64/src/upload-reset/upload-reset.c b/tools/linux64/src/upload-reset/upload-reset.c new file mode 100644 index 0000000..1d03bff --- /dev/null +++ b/tools/linux64/src/upload-reset/upload-reset.c @@ -0,0 +1,161 @@ +/* Copyright (C) 2015 Roger Clark + * + * 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. + * + * + * Utility to send the reset sequence on RTS and DTR and chars + * which resets the libmaple and causes the bootloader to be run + * + * + * + * Terminal control code by Heiko Noordhof (see copyright below) + */ + + + +/* Copyright (C) 2003 Heiko Noordhof + * + * 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. + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +/* Function prototypes (belong in a seperate header file) */ +int openserial(char *devicename); +void closeserial(void); +int setDTR(unsigned short level); +int setRTS(unsigned short level); + + +/* Two globals for use by this module only */ +static int fd; +static struct termios oldterminfo; + + +void closeserial(void) +{ + tcsetattr(fd, TCSANOW, &oldterminfo); + close(fd); +} + + +int openserial(char *devicename) +{ + struct termios attr; + + if ((fd = open(devicename, O_RDWR)) == -1) return 0; /* Error */ + atexit(closeserial); + + if (tcgetattr(fd, &oldterminfo) == -1) return 0; /* Error */ + attr = oldterminfo; + attr.c_cflag |= CRTSCTS | CLOCAL; + attr.c_oflag = 0; + if (tcflush(fd, TCIOFLUSH) == -1) return 0; /* Error */ + if (tcsetattr(fd, TCSANOW, &attr) == -1) return 0; /* Error */ + + /* Set the lines to a known state, and */ + /* finally return non-zero is successful. */ + return setRTS(0) && setDTR(0); +} + + +/* For the two functions below: + * level=0 to set line to LOW + * level=1 to set line to HIGH + */ + +int setRTS(unsigned short level) +{ + int status; + + if (ioctl(fd, TIOCMGET, &status) == -1) { + perror("setRTS(): TIOCMGET"); + return 0; + } + if (level) status |= TIOCM_RTS; + else status &= ~TIOCM_RTS; + if (ioctl(fd, TIOCMSET, &status) == -1) { + perror("setRTS(): TIOCMSET"); + return 0; + } + return 1; +} + + +int setDTR(unsigned short level) +{ + int status; + + if (ioctl(fd, TIOCMGET, &status) == -1) { + perror("setDTR(): TIOCMGET"); + return 0; + } + if (level) status |= TIOCM_DTR; + else status &= ~TIOCM_DTR; + if (ioctl(fd, TIOCMSET, &status) == -1) { + perror("setDTR: TIOCMSET"); + return 0; + } + return 1; +} + +/* This portion of code was written by Roger Clark + * It was informed by various other pieces of code written by Leaflabs to reset their + * Maple and Maple mini boards + */ + +main(int argc, char *argv[]) +{ + + if (argc<2 || argc >3) + { + printf("Usage upload-reset \n\r"); + return; + } + + if (openserial(argv[1])) + { + // Send magic sequence of DTR and RTS followed by the magic word "1EAF" + setRTS(false); + setDTR(false); + setDTR(true); + + usleep(50000L); + + setDTR(false); + setRTS(true); + setDTR(true); + + usleep(50000L); + + setDTR(false); + + usleep(50000L); + + write(fd,"1EAF",4); + + closeserial(); + if (argc==3) + { + usleep(atol(argv[2])*1000L); + } + } + else + { + printf("Failed to open serial device.\n\r"); + } +} diff --git a/tools/linux64/stlink/st-flash b/tools/linux64/stlink/st-flash new file mode 100644 index 0000000..951b11a Binary files /dev/null and b/tools/linux64/stlink/st-flash differ diff --git a/tools/linux64/stlink/st-info b/tools/linux64/stlink/st-info new file mode 100644 index 0000000..0787606 Binary files /dev/null and b/tools/linux64/stlink/st-info differ diff --git a/tools/linux64/stlink/st-term b/tools/linux64/stlink/st-term new file mode 100644 index 0000000..b5425db Binary files /dev/null and b/tools/linux64/stlink/st-term differ diff --git a/tools/linux64/stlink/st-util b/tools/linux64/stlink/st-util new file mode 100644 index 0000000..c6a0078 Binary files /dev/null and b/tools/linux64/stlink/st-util differ diff --git a/tools/linux64/stlink_upload b/tools/linux64/stlink_upload new file mode 100644 index 0000000..24e528d --- /dev/null +++ b/tools/linux64/stlink_upload @@ -0,0 +1,45 @@ +#!/bin/bash + +# Check for leaf device. +function leaf_status() +{ + +this_leaf_status=$(lsusb |grep "1eaf" | awk '{ print $NF}') +# Find the mode of the leaf bootloader +case $this_leaf_status in + "1eaf:0003") + echo "dfu" + ;; + "1eaf:0004") + echo "ttyACMx" + ;; + *) + #echo "$this_leaf_status" + echo "unknown" + ;; +esac +} + +# You will need the usb-reset code, see https://github.com/rogerclarkmelbourne/Arduino_STM32/wiki/Using-a-generic-stm32-board-on-linux-with-Maple-bootloader +# +USBRESET=$(which usb-reset) || USBRESET="./usb-reset" + +# Check to see if a maple compatible board is attached +LEAF_STATUS=$(leaf_status) +echo "USB Status [$LEAF_STATUS]" + +$(dirname $0)/stlink/st-flash write "$4" 0x8000000 + +sleep 4 +# Reset the usb device to bring up the tty rather than DFU +"$USBRESET" "/dev/bus/usb/$(lsusb |grep "1eaf" |awk '{print $2,$4}'|sed 's/\://g'|sed 's/ /\//g')" >/dev/null 2>&1 +# Check to see if a maple compatible board is attached +LEAF_STATUS=$(leaf_status) +echo "USB Status [$LEAF_STATUS]" +# Check to see if the tty came up +TTY_DEV=$(find /dev -cmin -2 |grep ttyAC) +echo -e "Waiting for tty device $TTY_DEV \n" +sleep 20 +echo -e "$TTY_DEV should now be available.\n" +exit 0 + diff --git a/tools/linux64/stm32flash/stm32flash b/tools/linux64/stm32flash/stm32flash new file mode 100644 index 0000000..16872bb Binary files /dev/null and b/tools/linux64/stm32flash/stm32flash differ diff --git a/tools/linux64/upload-reset b/tools/linux64/upload-reset new file mode 100644 index 0000000..26985b8 Binary files /dev/null and b/tools/linux64/upload-reset differ diff --git a/tools/linux64/upload_router b/tools/linux64/upload_router new file mode 100644 index 0000000..b74244d --- /dev/null +++ b/tools/linux64/upload_router @@ -0,0 +1,112 @@ +#!/bin/bash +# Translates the windows Arduino IDE upload call - something like.. +# +# upload_router ttyUSB0 1 1EAF:0003 /tmp/build9114565021046468906.tmp/STM32_Blink.cpp maple_dfu 0 +# +# to the linux dfu-util equivalent of the form... +# +# dfu-util -D ./STM32_Blink.cpp.bin -d 1eaf:0003 --intf 0 --alt 1 +# +# + +function leaf_status() +{ + +this_leaf_status=$(lsusb |grep "1eaf" | awk '{ print $NF}') +# Find the mode of the leaf bootloader +case $this_leaf_status in + "1eaf:0003") + echo "dfu" + ;; + "1eaf:0004") + echo "ttyACMx" + ;; + *) + #echo "$this_leaf_status" + echo "unknown" + ;; +esac +} + + +DEVICE="$3" +# Lowercase the 1eaf device name, since in Windows land everybody shouts. +DEVICE=${DEVICE,,} + +BINFILE="$4.bin" +INTERFACE="$6" +ALT_INTERFACE="$2" + +# You will need the usb-reset code, see https://github.com/rogerclarkmelbourne/Arduino_STM32/wiki/Using-a-generic-stm32-board-on-linux-with-Maple-bootloader +# +USBRESET=$(which usb-reset) || USBRESET="./usb-reset" + +# Check to see if a maple compatible board is attached +LEAF_STATUS=$(leaf_status) + +# Borard not found, or no boot loader on board. +if [[ $(leaf_status) = "unknown" ]] +then + echo "STM32 Maple Bootloader compatible board not found." + sleep 5 + exit 1 +fi + +# We got this far, so we need to get the board in bootloader mode. +# After the timeout period, the board goes back in to serial mode, we need it in dfu mode, which happens for the first few seconds at power on +# so we ask the user to unplug and re-plug the board. +echo -e "\n\rSTM32 Maple board is in $LEAF_STATUS mode." + +echo "Please unplug and replug the USB cable to the Maple device." +sleep 2 +# On unplugging the board will be "unknown" +while [[ $(leaf_status) != "unknown" ]] +do + echo -n "." + sleep 1 +done +# On re-plugging the board will be "dfu" +while [[ $(leaf_status) != "dfu" ]] +do + echo -n "." + sleep 1 +done + +echo -e "\n\rProgramming STM32 device with dfu-util" +until dfu-util -D "$BINFILE" -d "$DEVICE" --intf "$INTERFACE" --alt "$ALT_INTERFACE" 2>&1 +do + echo -n "." + sleep 1 +done + +echo -e "\n\rUnplug and replug the USB cable to the STM32 board again please...." +while [[ $(leaf_status) != "unknown" ]] +do + echo -n "." + sleep 1 +done + +echo -e "\n\rReconnecting" +while [[ $(leaf_status) = "unknown" ]] +do + echo -n "." + sleep 1 +done + +echo -e "\n\rWaiting for bootloader to exit." +for i in {1..6} +do + echo -n "." + sleep 1 +done + +"$USBRESET" "/dev/bus/usb/$(lsusb |grep "1eaf" |awk '{print $2,$4}'|sed 's/\://g'|sed 's/ /\//g')" >/dev/null 2>&1 + +while [[ $(leaf_status) = "unknown" ]] +do + echo -n "." + sleep 1 +done +THIS_TTY=$(find /dev/ttyACM* -cmin -2) +echo -e "\n\rSTM32 Maple board serial port re-created..." +echo -e "\n\rSerial port is $THIS_TTY Please allow 15 seconds before attempting to read from serial port." diff --git a/tools/macosx/src/build_dfu-util.sh b/tools/macosx/src/build_dfu-util.sh new file mode 100644 index 0000000..3563f57 --- /dev/null +++ b/tools/macosx/src/build_dfu-util.sh @@ -0,0 +1,15 @@ +#!/bin/bash + +sudo apt-get build-dep dfu-util +sudo apt-get install build-essentials +sudo apt-get install libusb-1.0-0-dev +sudo apt-get install autoconf automake autotools-dev + +cd dfu-util +./autogen.sh +./configure +make +cp src/dfu-util ../../linux/dfu-util +cp src/dfu-suffix ../../linux/dfu-util +cp src/dfu-prefix ../../linux/dfu-util + diff --git a/tools/macosx/src/dfu-util/AUTHORS b/tools/macosx/src/dfu-util/AUTHORS new file mode 100644 index 0000000..1b36c73 --- /dev/null +++ b/tools/macosx/src/dfu-util/AUTHORS @@ -0,0 +1,30 @@ +Authors ordered by first contribution. + +Harald Welte +Werner Almesberger +Michael Lauer +Jim Huang +Stefan Schmidt +Daniel Willmann +Mike Frysinger +Uwe Hermann +C. Scott Ananian +Bernard Blackham +Holger Freyther +Marc Singer +James Perkins +Tommi Keisala +Pascal Schweizer +Bradley Scott +Uwe Bonnes +Andrey Smirnov +Jussi Timperi +Hans Petter Selasky +Bo Shen +Henrique de Almeida Mendonca +Bernd Krumboeck +Dennis Meier +Veli-Pekka Peltola +Dave Hylands +Michael Grzeschik +Paul Fertser diff --git a/tools/macosx/src/dfu-util/COPYING b/tools/macosx/src/dfu-util/COPYING new file mode 100644 index 0000000..d60c31a --- /dev/null +++ b/tools/macosx/src/dfu-util/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. + + + Copyright (C) + + 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. + + , 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/tools/macosx/src/dfu-util/ChangeLog b/tools/macosx/src/dfu-util/ChangeLog new file mode 100644 index 0000000..37f1add --- /dev/null +++ b/tools/macosx/src/dfu-util/ChangeLog @@ -0,0 +1,93 @@ +0.8: + o New, separate dfu-prefix tool (Uwe Bonnes) + o Allow filtering on serial number (Uwe Bonnes) + o Improved VID/PID/serial filtering (Bradley Scott) + o Support reading firmware from stdin (Tormod Volden) + o Warn if missing DFU suffix (Tormod Volden) + o Improved progress bar (Hans Petter Selasky) + o Fix dfuse leave option (Uwe Bonnes) + o Major code rework (Hans Petter Selasky) + o MS Visual Studio build support (Henrique Mendonca) + o dfuse-pack.py tool for .dfu files (Antonio Galeo) + o Many other fixes from many people + +2014-09-13: Tormod Volden + +0.7: + o Support for TI Stellaris devices (Tommi Keisala) + o Fix libusb detection on MacOSX (Marc Singer) + o Fix libusb detection on FreeBSD (Tormod Volden) + o Improved DfuSe support (Tormod Volden) + o Support all special commands (leave, unprotect, mass-erase) + o Arbitrary upload lengths + o "force" option for various possible (dangerous) overrides + +2012-10-07: Tormod Volden + +0.6: + o Add detach mode (Stefan Schmidt) + o Check return value on all libusb calls (Tormod Volden) + o Fix segmentation fault with -s option (Tormod Volden) + o Add DFU suffix manipulation tool (Stefan Schmidt) + o Port to Windows: (Tormod Volden, some parts based on work from Satz + Klauer) + o Port file handling to stdio streams + o Sleep() macros + o C99 types + o Pack structs + o Detect DfuSe device correctly on big-endian architectures (Tormod + Volden) + o Add dfuse progress indication on download (Tormod Volden) + o Cleanup: gcc pedantic, gcc extension, ... (Tormod Volden) + o Rely on page size from functional descriptor. Please report if you get + an error about it. (Tormod Volden) + o Add quirk for Maple since it reports wrong DFU version (Tormod Volden) + +2012-04-22: Stefan Schmidt + +0.5: + o DfuSe extension support for ST devices (Tormod Volden) + o Add initial support for bitWillDetach flag from DFU 1.1 (Tormod + Volden) + o Internal cleanup and some manual page fixes (Tormod Volden) + +2011-11-02: Stefan Schmidt + +0.4: + o Rework to use libusb-1.0 (Stefan Schmidt) + o DFU suffix support (Tormod Volden, Stefan Schmidt) + o Sspeed up DFU downloads directly into memory (Bernard Blackham) + o More flexible -d vid:pid parsing (Tormod Volden) + o Many bug fixes and cleanups + +2011-07-20: Stefan Schmidt + +0.3: + o quirks: Add OpenOCD to the poll timeout quirk table. + +2010-12-22: Stefan Schmidt + +0.2: + o Fix some typos on the website and the README (Antonio Ospite, Uwe + Hermann) + o Remove build rule for a static binary. We can use autotools for this. + (Mike Frysinger) + o Fix infinite loop in download error path (C. Scott Ananian) + o Break out to show the 'finished' in upload (C. Scott Ananian) + o Add GPLv2+ headers (Harald Welte) + o Remove dead code (commands.[ch]) remnescent of dfu-programmer (Harald + Welte) + o Simple quirk system with Openmoko quirk for missing bwPollTimeout (Tormod Volden) + o New default (1024) and clamping of transfer size (Tormod Volden) + o Verify sending of completion packet (Tormod Volden) + o Look for DFU functional descriptor among all descriptors (Tormod + Volden) + o Print out in which direction we are transferring data + o Abort in upload if the file already exists + +2010-11-17 Stefan Schmidt + +0.1: + Initial release + +2010-05-23 Stefan Schmidt diff --git a/tools/macosx/src/dfu-util/DEVICES.txt b/tools/macosx/src/dfu-util/DEVICES.txt new file mode 100644 index 0000000..bdd9f1f --- /dev/null +++ b/tools/macosx/src/dfu-util/DEVICES.txt @@ -0,0 +1,20 @@ +List of supported software and hardware products: + +Software user (bootloader, etc) +------------------------------- +- Sam7DFU: http://www.openpcd.org/Sam7dfu +- U-boot: DFU patches +- Barebox: http://www.barebox.org/ +- Leaflabs: http://code.google.com/p/leaflabs/ +- Blackmagic DFU + +Products using DFU +------------------ +- OpenPCD (sam7dfu) +- Openmoko Neo 1973 and Freerunner (u-boot with DFU patches) +- Leaflabs Maple +- ATUSB from Qi Hardware +- STM32F105/7, STM32F2/F3/F4 in System Bootloader +- Blackmagic debug probe +- NXP LPC31xx/LPC43XX, e.g. LPC-Link and LPC-Link2, need binaries + with LPC prefix and encoding (LPC-Link) diff --git a/tools/macosx/src/dfu-util/Makefile.am b/tools/macosx/src/dfu-util/Makefile.am new file mode 100644 index 0000000..641dda5 --- /dev/null +++ b/tools/macosx/src/dfu-util/Makefile.am @@ -0,0 +1,3 @@ +SUBDIRS = src doc + +EXTRA_DIST = autogen.sh TODO DEVICES.txt dfuse-pack.py diff --git a/tools/macosx/src/dfu-util/README b/tools/macosx/src/dfu-util/README new file mode 100644 index 0000000..0f8f262 --- /dev/null +++ b/tools/macosx/src/dfu-util/README @@ -0,0 +1,20 @@ +Dfu-util - Device Firmware Upgrade Utilities + +Dfu-util is the host side implementation of the DFU 1.0 [1] and DFU 1.1 [2] +specification of the USB forum. + +DFU is intended to download and upload firmware to devices connected over +USB. It ranges from small devices like micro-controller boards up to mobile +phones. With dfu-util you are able to download firmware to your device or +upload firmware from it. + +dfu-util has been tested with Openmoko Neo1973 and Freerunner and many +other devices. + +[1] DFU 1.0 spec: http://www.usb.org/developers/devclass_docs/usbdfu10.pdf +[2] DFU 1.1 spec: http://www.usb.org/developers/devclass_docs/DFU_1.1.pdf + +The official website is: + + http://dfu-util.gnumonks.org/ + diff --git a/tools/macosx/src/dfu-util/TODO b/tools/macosx/src/dfu-util/TODO new file mode 100644 index 0000000..900c30c --- /dev/null +++ b/tools/macosx/src/dfu-util/TODO @@ -0,0 +1,14 @@ +DfuSe: +- Do erase and write in two separate passes when downloading +- Skip "Set Address" command when downloading contiguous blocks +- Implement "Get Commands" command + +Devices: +- Research iPhone/iPod/iPad support + Heavily modified dfu-util fork here: + https://github.com/planetbeing/xpwn/tree/master/dfu-util +- Test against Niftylights + +Non-Code: +- Logo +- Re-License as LGPL for usage as library? diff --git a/tools/macosx/src/dfu-util/autogen.sh b/tools/macosx/src/dfu-util/autogen.sh new file mode 100644 index 0000000..e67aed3 --- /dev/null +++ b/tools/macosx/src/dfu-util/autogen.sh @@ -0,0 +1,2 @@ +#! /bin/sh +autoreconf -v -i diff --git a/tools/macosx/src/dfu-util/configure.ac b/tools/macosx/src/dfu-util/configure.ac new file mode 100644 index 0000000..8622114 --- /dev/null +++ b/tools/macosx/src/dfu-util/configure.ac @@ -0,0 +1,41 @@ +# -*- Autoconf -*- +# Process this file with autoconf to produce a configure script. + +AC_PREREQ(2.59) +AC_INIT([dfu-util],[0.8],[dfu-util@lists.gnumonks.org],,[http://dfu-util.gnumonks.org]) +AC_CONFIG_AUX_DIR(m4) +AM_INIT_AUTOMAKE([foreign]) +AC_CONFIG_HEADERS([config.h]) + +# Test for new silent rules and enable only if they are available +m4_ifdef([AM_SILENT_RULES], [AM_SILENT_RULES([yes])]) + +# Checks for programs. +AC_PROG_CC + +# Checks for libraries. +# On FreeBSD the libusb-1.0 is called libusb and resides in system location +AC_CHECK_LIB([usb], [libusb_init],, [native_libusb=no],) +AS_IF([test x$native_libusb = xno], [ + PKG_CHECK_MODULES([USB], [libusb-1.0 >= 1.0.0],, + AC_MSG_ERROR([*** Required libusb-1.0 >= 1.0.0 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([usbpath.h windows.h sysexits.h]) + +# Checks for typedefs, structures, and compiler characteristics. +AC_C_CONST +AC_TYPE_SIZE_T + +# Checks for library functions. +AC_FUNC_MEMCMP +AC_CHECK_FUNCS([ftruncate getpagesize nanosleep err]) + +AC_CONFIG_FILES(Makefile src/Makefile doc/Makefile) +AC_OUTPUT diff --git a/tools/macosx/src/dfu-util/device-logs/README b/tools/macosx/src/dfu-util/device-logs/README new file mode 100644 index 0000000..00d3d1a --- /dev/null +++ b/tools/macosx/src/dfu-util/device-logs/README @@ -0,0 +1,77 @@ +Device: +------- +qi-hardware-atusb: +- Qi Hardware ben-wpan +- DFU implementation: + http://projects.qi-hardware.com/index.php/p/ben-wpan/source/tree/master/atusb/fw/usb +- Tester: Stefan Schmidt + +openpcd: +- OpenPCD RFID reader +- DFU implementation: SAM7DFU + http://www.openpcd.org/Sam7dfu, git://git.gnumonks.org/openpcd.git +- Tester: Stefan Schmidt + +simtrace: +- Sysmocom SimTrace +- DFU implementation: SAM7DFU + http://www.openpcd.org/Sam7dfu, git://git.gnumonks.org/openpcd.git +- Tester: Stefan Schmidt + +openmoko-freerunner: +- Openmoko Freerunner +- DFU implementation: Old U-Boot + http://git.openmoko.org/?p=u-boot.git;a=shortlog;h=refs/heads/mokopatches +- Tester: Stefan Schmidt + +openmoko-neo1973: +- Openmoko Neo1073 +- DFU implementation: Old U-Boot + http://git.openmoko.org/?p=u-boot.git;a=shortlog;h=refs/heads/mokopatches +- Tester: Stefan Schmidt + +tdk-bluetooth: +- TDK Corp. Bluetooth Adapter +- DFU implementation: closed soure +- Only upload has been tested +- Tester: Stefan Schmidt + +stm32f107: +- STM32 microcontrollers with built-in (ROM) DFU loader +- DFU implementation: Closed source but probably similar to the one + in their USB device libraries. Some relevant application notes: + http://www.st.com -> AN3156 and AN2606 +- Tested by Uwe Bonnes + +stm32f4discovery: +- STM32 microcontroller board with built-in (ROM) DFU loader +- DFU implementation: Closed source, probably similar to stm32f107. +- Tested by Joe Rothweiler + +dso-nano: +- DSO Nano pocket oscilloscope +- DFU implementation: Based on ST Microelectronics USB FS Library 1.0 + http://dsonano.googlecode.com/files/DS0201_OpenSource.rar +- Tester: Tormod Volden + +opc-20: +- Custom devices based on STM32F1xx +- DFU implementation: ST Microelectronics USB FS Device Library 3.1.0 + http://www.st.com -> um0424.zip +- Tester: Tormod Volden + +lpc-link, lpclink2: +- NXP LPCXpresso debug adapters +- Proprietary DFU implementation, uses special download files with + LPC prefix and encoding of the target firmware code +- Tested by Uwe Bonnes + +Adding the lsusb output and a download log of your device here helps +us to avoid regressions for hardware we cannot test while working on +the code. To extract the lsusb output use this command: +sudo lsusb -v -d $USBID > $DEVICE.lsusb +Prepare a description snippet as above, and send it to us. A log +(copy-paste of the command window) of a firmware download is also +nice, please use the double verbose option -v -v and include the +command line in the log file. + diff --git a/tools/macosx/src/dfu-util/device-logs/dsonano.lsusb b/tools/macosx/src/dfu-util/device-logs/dsonano.lsusb new file mode 100644 index 0000000..140a7bc --- /dev/null +++ b/tools/macosx/src/dfu-util/device-logs/dsonano.lsusb @@ -0,0 +1,60 @@ + +Bus 002 Device 004: ID 0483:df11 SGS Thomson Microelectronics +Device Descriptor: + bLength 18 + bDescriptorType 1 + bcdUSB 1.00 + bDeviceClass 0 (Defined at Interface level) + bDeviceSubClass 0 + bDeviceProtocol 0 + bMaxPacketSize0 64 + idVendor 0x0483 SGS Thomson Microelectronics + idProduct 0xdf11 + bcdDevice 1.1a + iManufacturer 1 STMicroelectronics + iProduct 2 STM32 DFU + iSerial 3 001 + bNumConfigurations 1 + Configuration Descriptor: + bLength 9 + bDescriptorType 2 + wTotalLength 36 + bNumInterfaces 1 + bConfigurationValue 1 + iConfiguration 0 + bmAttributes 0x80 + (Bus Powered) + MaxPower 64mA + Interface Descriptor: + bLength 9 + bDescriptorType 4 + bInterfaceNumber 0 + bAlternateSetting 0 + bNumEndpoints 0 + bInterfaceClass 254 Application Specific Interface + bInterfaceSubClass 1 Device Firmware Update + bInterfaceProtocol 0 + iInterface 4 @Internal Flash /0x08000000/12*001Ka,116*001Kg + Interface Descriptor: + bLength 9 + bDescriptorType 4 + bInterfaceNumber 0 + bAlternateSetting 1 + bNumEndpoints 0 + bInterfaceClass 254 Application Specific Interface + bInterfaceSubClass 1 Device Firmware Update + bInterfaceProtocol 0 + iInterface 5 @SPI Flash : M25P64/0x00000000/64*064Kg,64*064Kg + Device Firmware Upgrade Interface Descriptor: + bLength 9 + bDescriptorType 33 + bmAttributes 11 + Will Detach + Manifestation Intolerant + Upload Supported + Download Supported + wDetachTimeout 255 milliseconds + wTransferSize 1024 bytes + bcdDFUVersion 1.1a +Device Status: 0x0000 + (Bus Powered) diff --git a/tools/macosx/src/dfu-util/device-logs/lpclink.log b/tools/macosx/src/dfu-util/device-logs/lpclink.log new file mode 100644 index 0000000..7de4dd3 --- /dev/null +++ b/tools/macosx/src/dfu-util/device-logs/lpclink.log @@ -0,0 +1,59 @@ +(The on-board LPC3154 has some encryption key set and LPCXpressoWIN.enc +is encrypted.) + +$ lsusb | grep NXP +Bus 003 Device 011: ID 0471:df55 Philips (or NXP) LPCXpresso LPC-Link + +$ dfu-util -v -v -v -R -D /opt/lpc/lpcxpresso/bin/LPCXpressoWIN.enc + +dfu-util 0.7 + +Copyright 2005-2008 Weston Schmidt, Harald Welte and OpenMoko Inc. +Copyright 2010-2012 Tormod Volden and Stefan Schmidt +This program is Free Software and has ABSOLUTELY NO WARRANTY +Please report bugs to dfu-util@lists.gnumonks.org + +dfu-util: Invalid DFU suffix signature +dfu-util: A valid DFU suffix will be required in a future dfu-util release!!! +Deducing device DFU version from functional descriptor length +Opening DFU capable USB device... +ID 0471:df55 +Run-time device DFU version 0100 +Claiming USB DFU Runtime Interface... +Determining device status: +state = dfuIDLE, status = 0 +dfu-util: WARNING: Runtime device already in DFU state ?!? +Claiming USB DFU Interface... +Setting Alternate Setting #0 ... +Determining device status: +state = dfuIDLE, status = 0 +dfuIDLE, continuing +DFU mode device DFU version 0100 +Device returned transfer size 2048 +Copying data from PC to DFU device +Download [ ] 0% 0 bytes +Download [= ] 6% 2048 bytes +Download [=== ] 13% 4096 bytes +Download [==== ] 19% 6144 bytes +Download [====== ] 26% 8192 bytes +Download [======== ] 32% 10240 bytes +Download [========= ] 39% 12288 bytes +Download [=========== ] 45% 14336 bytes +Download [============= ] 52% 16384 bytes +Download [============== ] 59% 18432 bytes +Download [================ ] 65% 20480 bytes +Download [================== ] 72% 22528 bytes +Download [=================== ] 78% 24576 bytes +Download [===================== ] 85% 26624 bytes +Download [====================== ] 91% 28672 bytes +Download [======================== ] 98% 29192 bytes +Download [=========================] 100% 29192 bytes +Download done. +Sent a total of 29192 bytes +state(8) = dfuMANIFEST-WAIT-RESET, status(0) = No error condition is present +Done! +dfu-util: can't detach +Resetting USB to switch back to runtime mode + +$ lsusb | grep NXP +Bus 003 Device 012: ID 1fc9:0009 NXP Semiconductors diff --git a/tools/macosx/src/dfu-util/device-logs/lpclink.lsusb b/tools/macosx/src/dfu-util/device-logs/lpclink.lsusb new file mode 100644 index 0000000..867b2a2 --- /dev/null +++ b/tools/macosx/src/dfu-util/device-logs/lpclink.lsusb @@ -0,0 +1,58 @@ + +Bus 003 Device 008: ID 0471:df55 Philips (or NXP) LPCXpresso LPC-Link +Device Descriptor: + bLength 18 + bDescriptorType 1 + bcdUSB 2.00 + bDeviceClass 0 (Defined at Interface level) + bDeviceSubClass 0 + bDeviceProtocol 0 + bMaxPacketSize0 64 + idVendor 0x0471 Philips (or NXP) + idProduct 0xdf55 LPCXpresso LPC-Link + bcdDevice 0.01 + iManufacturer 0 + iProduct 0 + iSerial 0 + bNumConfigurations 1 + Configuration Descriptor: + bLength 9 + bDescriptorType 2 + wTotalLength 25 + bNumInterfaces 1 + bConfigurationValue 1 + iConfiguration 0 + bmAttributes 0x80 + (Bus Powered) + MaxPower 100mA + Interface Descriptor: + bLength 9 + bDescriptorType 4 + bInterfaceNumber 0 + bAlternateSetting 0 + bNumEndpoints 0 + bInterfaceClass 254 Application Specific Interface + bInterfaceSubClass 1 Device Firmware Update + bInterfaceProtocol 0 + iInterface 0 + Device Firmware Upgrade Interface Descriptor: + bLength 7 + bDescriptorType 33 + bmAttributes 1 + Will Not Detach + Manifestation Intolerant + Upload Unsupported + Download Supported + wDetachTimeout 65535 milliseconds + wTransferSize 2048 bytes +Device Qualifier (for other device speed): + bLength 10 + bDescriptorType 6 + bcdUSB 2.00 + bDeviceClass 0 (Defined at Interface level) + bDeviceSubClass 0 + bDeviceProtocol 0 + bMaxPacketSize0 64 + bNumConfigurations 1 +Device Status: 0x0000 + (Bus Powered) diff --git a/tools/macosx/src/dfu-util/device-logs/lpclink2.log b/tools/macosx/src/dfu-util/device-logs/lpclink2.log new file mode 100644 index 0000000..4681eff --- /dev/null +++ b/tools/macosx/src/dfu-util/device-logs/lpclink2.log @@ -0,0 +1,59 @@ +$ lsusb | grep NXP +Bus 003 Device 013: ID 1fc9:000c NXP Semiconductors + +$ dfu-util -D ~/devel/dfu-util/firmware.bin.qthdr + +dfu-util 0.7 + +Copyright 2005-2008 Weston Schmidt, Harald Welte and OpenMoko Inc. +Copyright 2010-2012 Tormod Volden and Stefan Schmidt +This program is Free Software and has ABSOLUTELY NO WARRANTY +Please report bugs to dfu-util@lists.gnumonks.org + +dfu-util: Invalid DFU suffix signature +dfu-util: A valid DFU suffix will be required in a future dfu-util release!!! +Possible unencryptes NXP LPC DFU prefix with the following properties +Payload length: 39 kiByte +Opening DFU capable USB device... +ID 1fc9:000c +Run-time device DFU version 0100 +Claiming USB DFU Runtime Interface... +Determining device status: +state = dfuIDLE, status = 0 +dfu-util: WARNING: Runtime device already in DFU state ?!? +Claiming USB DFU Interface... +Setting Alternate Setting #0 ... +Determining device status: +state = dfuIDLE, status = 0 +dfuIDLE, continuing +DFU mode device DFU version 0100 +Device returned transfer size 2048 +Copying data from PC to DFU device +Download [ ] 0% 0 bytes +Download [= ] 4% 2048 bytes +Download [== ] 9% 4096 bytes +Download [=== ] 14% 6144 bytes +Download [==== ] 19% 8192 bytes +Download [====== ] 24% 10240 bytes +Download [======= ] 28% 12288 bytes +Download [======== ] 33% 14336 bytes +Download [========= ] 38% 16384 bytes +Download [========== ] 43% 18432 bytes +Download [============ ] 48% 20480 bytes +Download [============= ] 53% 22528 bytes +Download [============== ] 57% 24576 bytes +Download [=============== ] 62% 26624 bytes +Download [================ ] 67% 28672 bytes +Download [================== ] 72% 30720 bytes +Download [=================== ] 77% 32768 bytes +Download [==================== ] 82% 34816 bytes +Download [===================== ] 86% 36864 bytes +Download [====================== ] 91% 38912 bytes +Download [======================== ] 96% 40356 bytes +Download [=========================] 100% 40356 bytes +Download done. +Sent a total of 40356 bytes +dfu-util: unable to read DFU status + +$ lsusb | grep NXP +Bus 003 Device 014: ID 1fc9:0018 NXP Semiconductors diff --git a/tools/macosx/src/dfu-util/device-logs/lpclink2.lsusb b/tools/macosx/src/dfu-util/device-logs/lpclink2.lsusb new file mode 100644 index 0000000..b833fca --- /dev/null +++ b/tools/macosx/src/dfu-util/device-logs/lpclink2.lsusb @@ -0,0 +1,203 @@ + +Bus 003 Device 007: ID 0c72:000c PEAK System PCAN-USB +Device Descriptor: + bLength 18 + bDescriptorType 1 + bcdUSB 1.00 + bDeviceClass 0 (Defined at Interface level) + bDeviceSubClass 0 + bDeviceProtocol 0 + bMaxPacketSize0 16 + idVendor 0x0c72 PEAK System + idProduct 0x000c PCAN-USB + bcdDevice 1c.ff + iManufacturer 0 + iProduct 3 VER1:PEAK +VER2:02.8.01 +DAT :06.05.2004 +TIME:09:35:37 + ... + iSerial 0 + bNumConfigurations 3 + Configuration Descriptor: + bLength 9 + bDescriptorType 2 + wTotalLength 46 + bNumInterfaces 1 + bConfigurationValue 1 + iConfiguration 0 + bmAttributes 0x80 + (Bus Powered) + MaxPower 200mA + Interface Descriptor: + bLength 9 + bDescriptorType 4 + bInterfaceNumber 0 + bAlternateSetting 0 + bNumEndpoints 4 + bInterfaceClass 0 (Defined at Interface level) + bInterfaceSubClass 0 + bInterfaceProtocol 0 + iInterface 0 + Endpoint Descriptor: + bLength 7 + bDescriptorType 5 + bEndpointAddress 0x81 EP 1 IN + bmAttributes 2 + Transfer Type Bulk + Synch Type None + Usage Type Data + wMaxPacketSize 0x0010 1x 16 bytes + bInterval 20 + Endpoint Descriptor: + bLength 7 + bDescriptorType 5 + bEndpointAddress 0x01 EP 1 OUT + bmAttributes 2 + Transfer Type Bulk + Synch Type None + Usage Type Data + wMaxPacketSize 0x0010 1x 16 bytes + bInterval 20 + Endpoint Descriptor: + bLength 7 + bDescriptorType 5 + bEndpointAddress 0x82 EP 2 IN + bmAttributes 2 + Transfer Type Bulk + Synch Type None + Usage Type Data + wMaxPacketSize 0x0040 1x 64 bytes + bInterval 1 + Endpoint Descriptor: + bLength 7 + bDescriptorType 5 + bEndpointAddress 0x02 EP 2 OUT + bmAttributes 2 + Transfer Type Bulk + Synch Type None + Usage Type Data + wMaxPacketSize 0x0040 1x 64 bytes + bInterval 1 + Configuration Descriptor: + bLength 9 + bDescriptorType 2 + wTotalLength 46 + bNumInterfaces 1 + bConfigurationValue 2 + iConfiguration 0 + bmAttributes 0x80 + (Bus Powered) + MaxPower 394mA + Interface Descriptor: + bLength 9 + bDescriptorType 4 + bInterfaceNumber 0 + bAlternateSetting 0 + bNumEndpoints 4 + bInterfaceClass 0 (Defined at Interface level) + bInterfaceSubClass 0 + bInterfaceProtocol 0 + iInterface 0 + Endpoint Descriptor: + bLength 7 + bDescriptorType 5 + bEndpointAddress 0x81 EP 1 IN + bmAttributes 2 + Transfer Type Bulk + Synch Type None + Usage Type Data + wMaxPacketSize 0x0010 1x 16 bytes + bInterval 20 + Endpoint Descriptor: + bLength 7 + bDescriptorType 5 + bEndpointAddress 0x01 EP 1 OUT + bmAttributes 2 + Transfer Type Bulk + Synch Type None + Usage Type Data + wMaxPacketSize 0x0010 1x 16 bytes + bInterval 20 + Endpoint Descriptor: + bLength 7 + bDescriptorType 5 + bEndpointAddress 0x82 EP 2 IN + bmAttributes 2 + Transfer Type Bulk + Synch Type None + Usage Type Data + wMaxPacketSize 0x0040 1x 64 bytes + bInterval 1 + Endpoint Descriptor: + bLength 7 + bDescriptorType 5 + bEndpointAddress 0x02 EP 2 OUT + bmAttributes 2 + Transfer Type Bulk + Synch Type None + Usage Type Data + wMaxPacketSize 0x0040 1x 64 bytes + bInterval 1 + Configuration Descriptor: + bLength 9 + bDescriptorType 2 + wTotalLength 46 + bNumInterfaces 1 + bConfigurationValue 3 + iConfiguration 0 + bmAttributes 0x80 + (Bus Powered) + MaxPower 200mA + Interface Descriptor: + bLength 9 + bDescriptorType 4 + bInterfaceNumber 0 + bAlternateSetting 0 + bNumEndpoints 4 + bInterfaceClass 0 (Defined at Interface level) + bInterfaceSubClass 0 + bInterfaceProtocol 0 + iInterface 0 + Endpoint Descriptor: + bLength 7 + bDescriptorType 5 + bEndpointAddress 0x81 EP 1 IN + bmAttributes 3 + Transfer Type Interrupt + Synch Type None + Usage Type Data + wMaxPacketSize 0x0010 1x 16 bytes + bInterval 1 + Endpoint Descriptor: + bLength 7 + bDescriptorType 5 + bEndpointAddress 0x01 EP 1 OUT + bmAttributes 3 + Transfer Type Interrupt + Synch Type None + Usage Type Data + wMaxPacketSize 0x0010 1x 16 bytes + bInterval 1 + Endpoint Descriptor: + bLength 7 + bDescriptorType 5 + bEndpointAddress 0x82 EP 2 IN + bmAttributes 2 + Transfer Type Bulk + Synch Type None + Usage Type Data + wMaxPacketSize 0x0040 1x 64 bytes + bInterval 1 + Endpoint Descriptor: + bLength 7 + bDescriptorType 5 + bEndpointAddress 0x02 EP 2 OUT + bmAttributes 2 + Transfer Type Bulk + Synch Type None + Usage Type Data + wMaxPacketSize 0x0040 1x 64 bytes + bInterval 1 +Device Status: 0x0001 + Self Powered diff --git a/tools/macosx/src/dfu-util/device-logs/opc-20.lsusb b/tools/macosx/src/dfu-util/device-logs/opc-20.lsusb new file mode 100644 index 0000000..580df90 --- /dev/null +++ b/tools/macosx/src/dfu-util/device-logs/opc-20.lsusb @@ -0,0 +1,60 @@ + +Bus 001 Device 004: ID 0483:df11 SGS Thomson Microelectronics +Device Descriptor: + bLength 18 + bDescriptorType 1 + bcdUSB 1.00 + bDeviceClass 0 (Defined at Interface level) + bDeviceSubClass 0 + bDeviceProtocol 0 + bMaxPacketSize0 64 + idVendor 0x0483 SGS Thomson Microelectronics + idProduct 0xdf11 + bcdDevice 2.00 + iManufacturer 1 STMicroelectronics + iProduct 2 STM32 DFU + iSerial 3 ÿÿÿÿÿÿÿÿÿÿÿÿ + bNumConfigurations 1 + Configuration Descriptor: + bLength 9 + bDescriptorType 2 + wTotalLength 36 + bNumInterfaces 1 + bConfigurationValue 1 + iConfiguration 0 + bmAttributes 0xc0 + Self Powered + MaxPower 100mA + Interface Descriptor: + bLength 9 + bDescriptorType 4 + bInterfaceNumber 0 + bAlternateSetting 0 + bNumEndpoints 0 + bInterfaceClass 254 Application Specific Interface + bInterfaceSubClass 1 Device Firmware Update + bInterfaceProtocol 2 + iInterface 4 @Internal Flash /0x08000000/12*001Ka,116*001Kg + Interface Descriptor: + bLength 9 + bDescriptorType 4 + bInterfaceNumber 0 + bAlternateSetting 1 + bNumEndpoints 0 + bInterfaceClass 254 Application Specific Interface + bInterfaceSubClass 1 Device Firmware Update + bInterfaceProtocol 2 + iInterface 5 @SPI Flash : M25P64/0x00000000/128*64Kg + Device Firmware Upgrade Interface Descriptor: + bLength 9 + bDescriptorType 33 + bmAttributes 11 + Will Detach + Manifestation Intolerant + Upload Supported + Download Supported + wDetachTimeout 255 milliseconds + wTransferSize 1024 bytes + bcdDFUVersion 1a.01 +Device Status: 0x0001 + Self Powered diff --git a/tools/macosx/src/dfu-util/device-logs/openmoko-freerunner-dfumode.lsusb b/tools/macosx/src/dfu-util/device-logs/openmoko-freerunner-dfumode.lsusb new file mode 100644 index 0000000..4c0abfb --- /dev/null +++ b/tools/macosx/src/dfu-util/device-logs/openmoko-freerunner-dfumode.lsusb @@ -0,0 +1,109 @@ +Bus 003 Device 017: ID 1d50:5119 OpenMoko, Inc. GTA01/GTA02 U-Boot Bootloader +Device Descriptor: + bLength 18 + bDescriptorType 1 + bcdUSB 1.00 + bDeviceClass 0 (Defined at Interface level) + bDeviceSubClass 0 + bDeviceProtocol 0 + bMaxPacketSize0 16 + idVendor 0x1d50 OpenMoko, Inc. + idProduct 0x5119 GTA01/GTA02 U-Boot Bootloader + bcdDevice 0.00 + iManufacturer 1 OpenMoko, Inc + iProduct 2 Neo1973 Bootloader U-Boot 1.3.2-moko12 + iSerial 3 0000000 + bNumConfigurations 1 + Configuration Descriptor: + bLength 9 + bDescriptorType 2 + wTotalLength 81 + bNumInterfaces 1 + bConfigurationValue 1 + iConfiguration 7 USB Device Firmware Upgrade + bmAttributes 0x80 + (Bus Powered) + MaxPower 100mA + Interface Descriptor: + bLength 9 + bDescriptorType 4 + bInterfaceNumber 0 + bAlternateSetting 0 + bNumEndpoints 0 + bInterfaceClass 254 Application Specific Interface + bInterfaceSubClass 1 Device Firmware Update + bInterfaceProtocol 2 + iInterface 8 RAM 0x32000000 + Interface Descriptor: + bLength 9 + bDescriptorType 4 + bInterfaceNumber 0 + bAlternateSetting 1 + bNumEndpoints 0 + bInterfaceClass 254 Application Specific Interface + bInterfaceSubClass 1 Device Firmware Update + bInterfaceProtocol 2 + iInterface 9 u-boot + Interface Descriptor: + bLength 9 + bDescriptorType 4 + bInterfaceNumber 0 + bAlternateSetting 2 + bNumEndpoints 0 + bInterfaceClass 254 Application Specific Interface + bInterfaceSubClass 1 Device Firmware Update + bInterfaceProtocol 2 + iInterface 10 u-boot_env + Interface Descriptor: + bLength 9 + bDescriptorType 4 + bInterfaceNumber 0 + bAlternateSetting 3 + bNumEndpoints 0 + bInterfaceClass 254 Application Specific Interface + bInterfaceSubClass 1 Device Firmware Update + bInterfaceProtocol 2 + iInterface 11 kernel + Interface Descriptor: + bLength 9 + bDescriptorType 4 + bInterfaceNumber 0 + bAlternateSetting 4 + bNumEndpoints 0 + bInterfaceClass 254 Application Specific Interface + bInterfaceSubClass 1 Device Firmware Update + bInterfaceProtocol 2 + iInterface 12 splash + Interface Descriptor: + bLength 9 + bDescriptorType 4 + bInterfaceNumber 0 + bAlternateSetting 5 + bNumEndpoints 0 + bInterfaceClass 254 Application Specific Interface + bInterfaceSubClass 1 Device Firmware Update + bInterfaceProtocol 2 + iInterface 13 factory + Interface Descriptor: + bLength 9 + bDescriptorType 4 + bInterfaceNumber 0 + bAlternateSetting 6 + bNumEndpoints 0 + bInterfaceClass 254 Application Specific Interface + bInterfaceSubClass 1 Device Firmware Update + bInterfaceProtocol 2 + iInterface 14 rootfs + Device Firmware Upgrade Interface Descriptor: + bLength 9 + bDescriptorType 33 + bmAttributes 7 + Will Not Detach + Manifestation Tolerant + Upload Supported + Download Supported + wDetachTimeout 65280 milliseconds + wTransferSize 4096 bytes + bcdDFUVersion 1.00 +Device Status: 0x0a00 + (Bus Powered) diff --git a/tools/macosx/src/dfu-util/device-logs/openmoko-freerunner.lsusb b/tools/macosx/src/dfu-util/device-logs/openmoko-freerunner.lsusb new file mode 100644 index 0000000..835708d --- /dev/null +++ b/tools/macosx/src/dfu-util/device-logs/openmoko-freerunner.lsusb @@ -0,0 +1,179 @@ +Bus 005 Device 033: ID 1d50:5119 OpenMoko, Inc. GTA01/GTA02 U-Boot Bootloader +Device Descriptor: + bLength 18 + bDescriptorType 1 + bcdUSB 1.10 + bDeviceClass 2 Communications + bDeviceSubClass 0 + bDeviceProtocol 0 + bMaxPacketSize0 16 + idVendor 0x1d50 OpenMoko, Inc. + idProduct 0x5119 GTA01/GTA02 U-Boot Bootloader + bcdDevice 0.00 + iManufacturer 1 OpenMoko, Inc + iProduct 2 Neo1973 Bootloader U-Boot 1.3.2-moko12 + iSerial 3 0000000 + bNumConfigurations 2 + Configuration Descriptor: + bLength 9 + bDescriptorType 2 + wTotalLength 85 + bNumInterfaces 3 + bConfigurationValue 1 + iConfiguration 4 TTY via USB + bmAttributes 0x80 + (Bus Powered) + MaxPower 500mA + Interface Descriptor: + bLength 9 + bDescriptorType 4 + bInterfaceNumber 0 + bAlternateSetting 0 + bNumEndpoints 1 + bInterfaceClass 2 Communications + bInterfaceSubClass 2 Abstract (modem) + bInterfaceProtocol 1 AT-commands (v.25ter) + iInterface 6 Control Interface + CDC Header: + bcdCDC 0.6e + CDC Call Management: + bmCapabilities 0x00 + bDataInterface 1 + CDC ACM: + bmCapabilities 0x00 + CDC Union: + bMasterInterface 0 + bSlaveInterface 1 + Endpoint Descriptor: + bLength 7 + bDescriptorType 5 + bEndpointAddress 0x81 EP 1 IN + bmAttributes 3 + Transfer Type Interrupt + Synch Type None + Usage Type Data + wMaxPacketSize 0x0010 1x 16 bytes + bInterval 255 + Interface Descriptor: + bLength 9 + bDescriptorType 4 + bInterfaceNumber 1 + bAlternateSetting 0 + bNumEndpoints 2 + bInterfaceClass 10 CDC Data + bInterfaceSubClass 0 Unused + bInterfaceProtocol 0 + iInterface 5 Bulk Data Interface + Endpoint Descriptor: + bLength 7 + bDescriptorType 5 + bEndpointAddress 0x02 EP 2 OUT + bmAttributes 2 + Transfer Type Bulk + Synch Type None + Usage Type Data + wMaxPacketSize 0x0010 1x 16 bytes + bInterval 255 + Endpoint Descriptor: + bLength 7 + bDescriptorType 5 + bEndpointAddress 0x83 EP 3 IN + bmAttributes 2 + Transfer Type Bulk + Synch Type None + Usage Type Data + wMaxPacketSize 0x0010 1x 16 bytes + bInterval 255 + Interface Descriptor: + bLength 9 + bDescriptorType 4 + bInterfaceNumber 2 + bAlternateSetting 0 + bNumEndpoints 0 + bInterfaceClass 254 Application Specific Interface + bInterfaceSubClass 1 Device Firmware Update + bInterfaceProtocol 1 + iInterface 7 USB Device Firmware Upgrade + Device Firmware Upgrade Interface Descriptor: + bLength 9 + bDescriptorType 33 + bmAttributes 7 + Will Not Detach + Manifestation Tolerant + Upload Supported + Download Supported + wDetachTimeout 65280 milliseconds + wTransferSize 4096 bytes + bcdDFUVersion 1.00 + Configuration Descriptor: + bLength 9 + bDescriptorType 2 + wTotalLength 67 + bNumInterfaces 2 + bConfigurationValue 2 + iConfiguration 4 TTY via USB + bmAttributes 0x80 + (Bus Powered) + MaxPower 100mA + Interface Descriptor: + bLength 9 + bDescriptorType 4 + bInterfaceNumber 0 + bAlternateSetting 0 + bNumEndpoints 1 + bInterfaceClass 2 Communications + bInterfaceSubClass 2 Abstract (modem) + bInterfaceProtocol 1 AT-commands (v.25ter) + iInterface 6 Control Interface + CDC Header: + bcdCDC 0.6e + CDC Call Management: + bmCapabilities 0x00 + bDataInterface 1 + CDC ACM: + bmCapabilities 0x00 + CDC Union: + bMasterInterface 0 + bSlaveInterface 1 + Endpoint Descriptor: + bLength 7 + bDescriptorType 5 + bEndpointAddress 0x81 EP 1 IN + bmAttributes 3 + Transfer Type Interrupt + Synch Type None + Usage Type Data + wMaxPacketSize 0x0010 1x 16 bytes + bInterval 255 + Interface Descriptor: + bLength 9 + bDescriptorType 4 + bInterfaceNumber 1 + bAlternateSetting 0 + bNumEndpoints 2 + bInterfaceClass 10 CDC Data + bInterfaceSubClass 0 Unused + bInterfaceProtocol 0 + iInterface 5 Bulk Data Interface + Endpoint Descriptor: + bLength 7 + bDescriptorType 5 + bEndpointAddress 0x02 EP 2 OUT + bmAttributes 2 + Transfer Type Bulk + Synch Type None + Usage Type Data + wMaxPacketSize 0x0010 1x 16 bytes + bInterval 255 + Endpoint Descriptor: + bLength 7 + bDescriptorType 5 + bEndpointAddress 0x83 EP 3 IN + bmAttributes 2 + Transfer Type Bulk + Synch Type None + Usage Type Data + wMaxPacketSize 0x0010 1x 16 bytes + bInterval 255 +Device Status: 0x9a00 + (Bus Powered) diff --git a/tools/macosx/src/dfu-util/device-logs/openmoko-neo1973.lsusb b/tools/macosx/src/dfu-util/device-logs/openmoko-neo1973.lsusb new file mode 100644 index 0000000..0778950 --- /dev/null +++ b/tools/macosx/src/dfu-util/device-logs/openmoko-neo1973.lsusb @@ -0,0 +1,182 @@ + +Bus 006 Device 020: ID 1457:5119 First International Computer, Inc. OpenMoko Neo1973 u-boot cdc_acm serial port +Device Descriptor: + bLength 18 + bDescriptorType 1 + bcdUSB 1.10 + bDeviceClass 2 Communications + bDeviceSubClass 0 + bDeviceProtocol 0 + bMaxPacketSize0 16 + idVendor 0x1457 First International Computer, Inc. + idProduct 0x5119 OpenMoko Neo1973 u-boot cdc_acm serial port + bcdDevice 0.00 + iManufacturer 1 + iProduct 2 + iSerial 3 + bNumConfigurations 2 + Configuration Descriptor: + bLength 9 + bDescriptorType 2 + wTotalLength 85 + bNumInterfaces 3 + bConfigurationValue 1 + iConfiguration 4 + bmAttributes 0x80 + (Bus Powered) + MaxPower 500mA + Interface Descriptor: + bLength 9 + bDescriptorType 4 + bInterfaceNumber 0 + bAlternateSetting 0 + bNumEndpoints 1 + bInterfaceClass 2 Communications + bInterfaceSubClass 2 Abstract (modem) + bInterfaceProtocol 1 AT-commands (v.25ter) + iInterface 6 + CDC Header: + bcdCDC 0.6e + CDC Call Management: + bmCapabilities 0x00 + bDataInterface 1 + CDC ACM: + bmCapabilities 0x00 + CDC Union: + bMasterInterface 0 + bSlaveInterface 1 + Endpoint Descriptor: + bLength 7 + bDescriptorType 5 + bEndpointAddress 0x81 EP 1 IN + bmAttributes 3 + Transfer Type Interrupt + Synch Type None + Usage Type Data + wMaxPacketSize 0x0010 1x 16 bytes + bInterval 255 + Interface Descriptor: + bLength 9 + bDescriptorType 4 + bInterfaceNumber 1 + bAlternateSetting 0 + bNumEndpoints 2 + bInterfaceClass 10 CDC Data + bInterfaceSubClass 0 Unused + bInterfaceProtocol 0 + iInterface 5 + Endpoint Descriptor: + bLength 7 + bDescriptorType 5 + bEndpointAddress 0x02 EP 2 OUT + bmAttributes 2 + Transfer Type Bulk + Synch Type None + Usage Type Data + wMaxPacketSize 0x0010 1x 16 bytes + bInterval 255 + Endpoint Descriptor: + bLength 7 + bDescriptorType 5 + bEndpointAddress 0x83 EP 3 IN + bmAttributes 2 + Transfer Type Bulk + Synch Type None + Usage Type Data + wMaxPacketSize 0x0010 1x 16 bytes + bInterval 255 + Interface Descriptor: + bLength 9 + bDescriptorType 4 + bInterfaceNumber 2 + bAlternateSetting 0 + bNumEndpoints 0 + bInterfaceClass 254 Application Specific Interface + bInterfaceSubClass 1 Device Firmware Update + bInterfaceProtocol 1 + iInterface 7 + Device Firmware Upgrade Interface Descriptor: + bLength 9 + bDescriptorType 33 + bmAttributes 7 + Will Not Detach + Manifestation Tolerant + Upload Supported + Download Supported + wDetachTimeout 65280 milliseconds + wTransferSize 4096 bytes + bcdDFUVersion 1.00 + Configuration Descriptor: + bLength 9 + bDescriptorType 2 + wTotalLength 67 + bNumInterfaces 2 + bConfigurationValue 2 + iConfiguration 4 + bmAttributes 0x80 + (Bus Powered) + MaxPower 100mA + Interface Descriptor: + bLength 9 + bDescriptorType 4 + bInterfaceNumber 0 + bAlternateSetting 0 + bNumEndpoints 1 + bInterfaceClass 2 Communications + bInterfaceSubClass 2 Abstract (modem) + bInterfaceProtocol 1 AT-commands (v.25ter) + iInterface 6 + CDC Header: + bcdCDC 0.6e + CDC Call Management: + bmCapabilities 0x00 + bDataInterface 1 + CDC ACM: + bmCapabilities 0x00 + CDC Union: + bMasterInterface 0 + bSlaveInterface 1 + Endpoint Descriptor: + bLength 7 + bDescriptorType 5 + bEndpointAddress 0x81 EP 1 IN + bmAttributes 3 + Transfer Type Interrupt + Synch Type None + Usage Type Data + wMaxPacketSize 0x0010 1x 16 bytes + bInterval 255 + Interface Descriptor: + bLength 9 + bDescriptorType 4 + bInterfaceNumber 1 + bAlternateSetting 0 + bNumEndpoints 2 + bInterfaceClass 10 CDC Data + bInterfaceSubClass 0 Unused + bInterfaceProtocol 0 + iInterface 5 + Endpoint Descriptor: + bLength 7 + bDescriptorType 5 + bEndpointAddress 0x02 EP 2 OUT + bmAttributes 2 + Transfer Type Bulk + Synch Type None + Usage Type Data + wMaxPacketSize 0x0010 1x 16 bytes + bInterval 255 + Endpoint Descriptor: + bLength 7 + bDescriptorType 5 + bEndpointAddress 0x83 EP 3 IN + bmAttributes 2 + Transfer Type Bulk + Synch Type None + Usage Type Data + wMaxPacketSize 0x0010 1x 16 bytes + bInterval 255 +Device Status: 0x0006 + (Bus Powered) + Remote Wakeup Enabled + Test Mode diff --git a/tools/macosx/src/dfu-util/device-logs/openpcd.lsusb b/tools/macosx/src/dfu-util/device-logs/openpcd.lsusb new file mode 100644 index 0000000..f6255a9 --- /dev/null +++ b/tools/macosx/src/dfu-util/device-logs/openpcd.lsusb @@ -0,0 +1,60 @@ + +Bus 006 Device 016: ID 16c0:076b VOTI OpenPCD 13.56MHz RFID Reader +Device Descriptor: + bLength 18 + bDescriptorType 1 + bcdUSB 1.00 + bDeviceClass 0 (Defined at Interface level) + bDeviceSubClass 0 + bDeviceProtocol 0 + bMaxPacketSize0 8 + idVendor 0x16c0 VOTI + idProduct 0x076b OpenPCD 13.56MHz RFID Reader + bcdDevice 0.00 + iManufacturer 1 + iProduct 2 OpenPCD RFID Simulator - DFU Mode + iSerial 0 + bNumConfigurations 1 + Configuration Descriptor: + bLength 9 + bDescriptorType 2 + wTotalLength 36 + bNumInterfaces 1 + bConfigurationValue 1 + iConfiguration 0 + bmAttributes 0x80 + (Bus Powered) + MaxPower 200mA + Interface Descriptor: + bLength 9 + bDescriptorType 4 + bInterfaceNumber 0 + bAlternateSetting 0 + bNumEndpoints 0 + bInterfaceClass 254 Application Specific Interface + bInterfaceSubClass 1 Device Firmware Update + bInterfaceProtocol 2 + iInterface 0 + Interface Descriptor: + bLength 9 + bDescriptorType 4 + bInterfaceNumber 0 + bAlternateSetting 1 + bNumEndpoints 0 + bInterfaceClass 254 Application Specific Interface + bInterfaceSubClass 1 Device Firmware Update + bInterfaceProtocol 2 + iInterface 0 + Device Firmware Upgrade Interface Descriptor: + bLength 9 + bDescriptorType 33 + bmAttributes 3 + Will Not Detach + Manifestation Intolerant + Upload Supported + Download Supported + wDetachTimeout 65280 milliseconds + wTransferSize 256 bytes + bcdDFUVersion 1.00 +Device Status: 0x0000 + (Bus Powered) diff --git a/tools/macosx/src/dfu-util/device-logs/qi-hardware-atusb.lsusb b/tools/macosx/src/dfu-util/device-logs/qi-hardware-atusb.lsusb new file mode 100644 index 0000000..bfc1701 --- /dev/null +++ b/tools/macosx/src/dfu-util/device-logs/qi-hardware-atusb.lsusb @@ -0,0 +1,59 @@ + +Bus 006 Device 013: ID 20b7:1540 Qi Hardware ben-wpan, AT86RF230-based +Device Descriptor: + bLength 18 + bDescriptorType 1 + bcdUSB 2.00 + bDeviceClass 255 Vendor Specific Class + bDeviceSubClass 0 + bDeviceProtocol 0 + bMaxPacketSize0 64 + idVendor 0x20b7 Qi Hardware + idProduct 0x1540 ben-wpan, AT86RF230-based + bcdDevice 0.01 + iManufacturer 0 + iProduct 0 + iSerial 1 4630333438371508231a + bNumConfigurations 1 + Configuration Descriptor: + bLength 9 + bDescriptorType 2 + wTotalLength 34 + bNumInterfaces 2 + bConfigurationValue 1 + iConfiguration 0 + bmAttributes 0x80 + (Bus Powered) + MaxPower 40mA + Interface Descriptor: + bLength 9 + bDescriptorType 4 + bInterfaceNumber 0 + bAlternateSetting 0 + bNumEndpoints 1 + bInterfaceClass 255 Vendor Specific Class + bInterfaceSubClass 0 + bInterfaceProtocol 0 + iInterface 0 + Endpoint Descriptor: + bLength 7 + bDescriptorType 5 + bEndpointAddress 0x81 EP 1 IN + bmAttributes 2 + Transfer Type Bulk + Synch Type None + Usage Type Data + wMaxPacketSize 0x0040 1x 64 bytes + bInterval 0 + Interface Descriptor: + bLength 9 + bDescriptorType 4 + bInterfaceNumber 1 + bAlternateSetting 0 + bNumEndpoints 0 + bInterfaceClass 254 Application Specific Interface + bInterfaceSubClass 1 Device Firmware Update + bInterfaceProtocol 1 + iInterface 0 +Device Status: 0x0000 + (Bus Powered) diff --git a/tools/macosx/src/dfu-util/device-logs/simtrace.lsusb b/tools/macosx/src/dfu-util/device-logs/simtrace.lsusb new file mode 100644 index 0000000..578ddf0 --- /dev/null +++ b/tools/macosx/src/dfu-util/device-logs/simtrace.lsusb @@ -0,0 +1,70 @@ + +Bus 006 Device 017: ID 16c0:0762 VOTI +Device Descriptor: + bLength 18 + bDescriptorType 1 + bcdUSB 1.00 + bDeviceClass 0 (Defined at Interface level) + bDeviceSubClass 0 + bDeviceProtocol 0 + bMaxPacketSize0 8 + idVendor 0x16c0 VOTI + idProduct 0x0762 + bcdDevice 0.00 + iManufacturer 1 sysmocom - systems for mobile communications GmbH + iProduct 2 SimTrace SIM Sniffer - DFU Mode + iSerial 0 + bNumConfigurations 1 + Configuration Descriptor: + bLength 9 + bDescriptorType 2 + wTotalLength 45 + bNumInterfaces 1 + bConfigurationValue 1 + iConfiguration 3 SimTrace DFU Configuration + bmAttributes 0x80 + (Bus Powered) + MaxPower 200mA + Interface Descriptor: + bLength 9 + bDescriptorType 4 + bInterfaceNumber 0 + bAlternateSetting 0 + bNumEndpoints 0 + bInterfaceClass 254 Application Specific Interface + bInterfaceSubClass 1 Device Firmware Update + bInterfaceProtocol 2 + iInterface 4 SimTrace DFU Interface - Application Partition + Interface Descriptor: + bLength 9 + bDescriptorType 4 + bInterfaceNumber 0 + bAlternateSetting 1 + bNumEndpoints 0 + bInterfaceClass 254 Application Specific Interface + bInterfaceSubClass 1 Device Firmware Update + bInterfaceProtocol 2 + iInterface 5 SimTrace DFU Interface - Bootloader Partition + Interface Descriptor: + bLength 9 + bDescriptorType 4 + bInterfaceNumber 0 + bAlternateSetting 2 + bNumEndpoints 0 + bInterfaceClass 254 Application Specific Interface + bInterfaceSubClass 1 Device Firmware Update + bInterfaceProtocol 2 + iInterface 6 SimTrace DFU Interface - RAM + Device Firmware Upgrade Interface Descriptor: + bLength 9 + bDescriptorType 33 + bmAttributes 3 + Will Not Detach + Manifestation Intolerant + Upload Supported + Download Supported + wDetachTimeout 65280 milliseconds + wTransferSize 256 bytes + bcdDFUVersion 1.00 +Device Status: 0x0000 + (Bus Powered) diff --git a/tools/macosx/src/dfu-util/device-logs/sparkcore.lsusb b/tools/macosx/src/dfu-util/device-logs/sparkcore.lsusb new file mode 100644 index 0000000..b6029ff --- /dev/null +++ b/tools/macosx/src/dfu-util/device-logs/sparkcore.lsusb @@ -0,0 +1,60 @@ + +Bus 001 Device 008: ID 1d50:607f OpenMoko, Inc. +Device Descriptor: + bLength 18 + bDescriptorType 1 + bcdUSB 1.00 + bDeviceClass 0 (Defined at Interface level) + bDeviceSubClass 0 + bDeviceProtocol 0 + bMaxPacketSize0 64 + idVendor 0x1d50 OpenMoko, Inc. + idProduct 0x607f + bcdDevice 2.00 + iManufacturer 1 Spark Devices + iProduct 2 CORE DFU + iSerial 3 8D80527B5055 + bNumConfigurations 1 + Configuration Descriptor: + bLength 9 + bDescriptorType 2 + wTotalLength 36 + bNumInterfaces 1 + bConfigurationValue 1 + iConfiguration 0 + bmAttributes 0xc0 + Self Powered + MaxPower 100mA + Interface Descriptor: + bLength 9 + bDescriptorType 4 + bInterfaceNumber 0 + bAlternateSetting 0 + bNumEndpoints 0 + bInterfaceClass 254 Application Specific Interface + bInterfaceSubClass 1 Device Firmware Update + bInterfaceProtocol 2 + iInterface 4 @Internal Flash /0x08000000/20*001Ka,108*001Kg + Interface Descriptor: + bLength 9 + bDescriptorType 4 + bInterfaceNumber 0 + bAlternateSetting 1 + bNumEndpoints 0 + bInterfaceClass 254 Application Specific Interface + bInterfaceSubClass 1 Device Firmware Update + bInterfaceProtocol 2 + iInterface 5 @SPI Flash : SST25x/0x00000000/512*04Kg + Device Firmware Upgrade Interface Descriptor: + bLength 9 + bDescriptorType 33 + bmAttributes 11 + Will Detach + Manifestation Intolerant + Upload Supported + Download Supported + wDetachTimeout 255 milliseconds + wTransferSize 1024 bytes + bcdDFUVersion 1.1a +Device Status: 0x0001 + Self Powered diff --git a/tools/macosx/src/dfu-util/device-logs/stm32f107.bin-download b/tools/macosx/src/dfu-util/device-logs/stm32f107.bin-download new file mode 100644 index 0000000..45b714f --- /dev/null +++ b/tools/macosx/src/dfu-util/device-logs/stm32f107.bin-download @@ -0,0 +1,48 @@ +> src/dfu-util --intf 0 --alt 0 -v -v -v -s 0x8000000 -D test3 +dfu-util 0.4 + +(C) 2005-2008 by Weston Schmidt, Harald Welte and OpenMoko Inc. +(C) 2010-2011 Tormod Volden (DfuSe support) +This program is Free Software and has ABSOLUTELY NO WARRANTY + +dfu-util does currently only support DFU version 1.0 + +Opening DFU USB device... ID 0483:df11 +Run-time device DFU version 011a +Found DFU: [0483:df11] devnum=0, cfg=1, intf=0, alt=0, name="@Internal Flash /0x08000000/128*002Kg" +Claiming USB DFU Interface... +Setting Alternate Setting #0 ... +Determining device status: state = dfuIDLE, status = 0 +dfuIDLE, continuing +DFU mode device DFU version 011a +Device returned transfer size 2048 +No valid DFU suffix signature +Warning: File has no DFU suffix +DfuSe interface name: "Internal Flash " +Memory segment at 0x08000000 128 x 2048 = 262144 (rew) +Uploading to address = 0x08000000, size = 16384 +Erasing page size 2048 at address 0x08000000, page starting at 0x08000000 + Download from image offset 00000000 to memory 08000000-080007ff, size 2048 + Setting address pointer to 0x08000000 +Erasing page size 2048 at address 0x08000800, page starting at 0x08000800 + Download from image offset 00000800 to memory 08000800-08000fff, size 2048 + Setting address pointer to 0x08000800 +Erasing page size 2048 at address 0x08001000, page starting at 0x08001000 + Download from image offset 00001000 to memory 08001000-080017ff, size 2048 + Setting address pointer to 0x08001000 +Erasing page size 2048 at address 0x08001800, page starting at 0x08001800 + Download from image offset 00001800 to memory 08001800-08001fff, size 2048 + Setting address pointer to 0x08001800 +Erasing page size 2048 at address 0x08002000, page starting at 0x08002000 + Download from image offset 00002000 to memory 08002000-080027ff, size 2048 + Setting address pointer to 0x08002000 +Erasing page size 2048 at address 0x08002800, page starting at 0x08002800 + Download from image offset 00002800 to memory 08002800-08002fff, size 2048 + Setting address pointer to 0x08002800 +Erasing page size 2048 at address 0x08003000, page starting at 0x08003000 + Download from image offset 00003000 to memory 08003000-080037ff, size 2048 + Setting address pointer to 0x08003000 +Erasing page size 2048 at address 0x08003800, page starting at 0x08003800 + Download from image offset 00003800 to memory 08003800-08003fff, size 2048 + Setting address pointer to 0x08003800 + diff --git a/tools/macosx/src/dfu-util/device-logs/stm32f107.lsusb b/tools/macosx/src/dfu-util/device-logs/stm32f107.lsusb new file mode 100644 index 0000000..14b45cd --- /dev/null +++ b/tools/macosx/src/dfu-util/device-logs/stm32f107.lsusb @@ -0,0 +1,60 @@ + +Bus 001 Device 028: ID 0483:df11 SGS Thomson Microelectronics STM Device in DFU Mode +Device Descriptor: + bLength 18 + bDescriptorType 1 + bcdUSB 1.00 + bDeviceClass 0 (Defined at Interface level) + bDeviceSubClass 0 + bDeviceProtocol 0 + bMaxPacketSize0 64 + idVendor 0x0483 SGS Thomson Microelectronics + idProduct 0xdf11 STM Device in DFU Mode + bcdDevice 20.00 + iManufacturer 1 STMicroelectronics + iProduct 2 STM32 0x418 DFU Bootloader + iSerial 3 STM32 + bNumConfigurations 1 + Configuration Descriptor: + bLength 9 + bDescriptorType 2 + wTotalLength 36 + bNumInterfaces 1 + bConfigurationValue 1 + iConfiguration 0 + bmAttributes 0xc0 + Self Powered + MaxPower 100mA + Interface Descriptor: + bLength 9 + bDescriptorType 4 + bInterfaceNumber 0 + bAlternateSetting 0 + bNumEndpoints 0 + bInterfaceClass 254 Application Specific Interface + bInterfaceSubClass 1 Device Firmware Update + bInterfaceProtocol 2 + iInterface 4 @Internal Flash /0x08000000/128*002Kg + Interface Descriptor: + bLength 9 + bDescriptorType 4 + bInterfaceNumber 0 + bAlternateSetting 1 + bNumEndpoints 0 + bInterfaceClass 254 Application Specific Interface + bInterfaceSubClass 1 Device Firmware Update + bInterfaceProtocol 2 + iInterface 5 @Option Bytes /0x1FFFF800/01*016 g + Device Firmware Upgrade Interface Descriptor: + bLength 9 + bDescriptorType 33 + bmAttributes 11 + Will Detach + Manifestation Intolerant + Upload Supported + Download Supported + wDetachTimeout 255 milliseconds + wTransferSize 2048 bytes + bcdDFUVersion 1.1a +Device Status: 0x0000 + (Bus Powered) diff --git a/tools/macosx/src/dfu-util/device-logs/stm32f4discovery.bin-download b/tools/macosx/src/dfu-util/device-logs/stm32f4discovery.bin-download new file mode 100644 index 0000000..96e1722 --- /dev/null +++ b/tools/macosx/src/dfu-util/device-logs/stm32f4discovery.bin-download @@ -0,0 +1,36 @@ +dfu-util --device 0483:df11 --alt 0 \ + --dfuse-address 0x08000000 \ + -v -v -v \ + --download arm/iotoggle.bin +No valid DFU suffix signature +Warning: File has no DFU suffix +dfu-util 0.5 + +(C) 2005-2008 by Weston Schmidt, Harald Welte and OpenMoko Inc. +(C) 2010-2011 Tormod Volden (DfuSe support) +This program is Free Software and has ABSOLUTELY NO WARRANTY + +dfu-util does currently only support DFU version 1.0 + +Filter on vendor = 0x0483 product = 0xdf11 +Opening DFU capable USB device... ID 0483:df11 +Run-time device DFU version 011a +Found DFU: [0483:df11] devnum=0, cfg=1, intf=0, alt=0, name="@Internal Flash /0x08000000/04*016Kg,01*064Kg,07*128Kg" +Claiming USB DFU Interface... +Setting Alternate Setting #0 ... +Determining device status: state = dfuERROR, status = 10 +dfuERROR, clearing status +Determining device status: state = dfuIDLE, status = 0 +dfuIDLE, continuing +DFU mode device DFU version 011a +Device returned transfer size 2048 +DfuSe interface name: "Internal Flash " +Memory segment at 0x08000000 4 x 16384 = 65536 (rew) +Memory segment at 0x08010000 1 x 65536 = 65536 (rew) +Memory segment at 0x08020000 7 x 131072 = 917504 (rew) +Uploading to address = 0x08000000, size = 2308 +Erasing page size 16384 at address 0x08000000, page starting at 0x08000000 + Download from image offset 00000000 to memory 08000000-080007ff, size 2048 + Setting address pointer to 0x08000000 + Download from image offset 00000800 to memory 08000800-08000903, size 260 + Setting address pointer to 0x08000800 diff --git a/tools/macosx/src/dfu-util/device-logs/stm32f4discovery.lsusb b/tools/macosx/src/dfu-util/device-logs/stm32f4discovery.lsusb new file mode 100644 index 0000000..0b870de --- /dev/null +++ b/tools/macosx/src/dfu-util/device-logs/stm32f4discovery.lsusb @@ -0,0 +1,80 @@ + +Bus 001 Device 010: ID 0483:df11 SGS Thomson Microelectronics STM Device in DFU Mode +Device Descriptor: + bLength 18 + bDescriptorType 1 + bcdUSB 1.00 + bDeviceClass 0 (Defined at Interface level) + bDeviceSubClass 0 + bDeviceProtocol 0 + bMaxPacketSize0 64 + idVendor 0x0483 SGS Thomson Microelectronics + idProduct 0xdf11 STM Device in DFU Mode + bcdDevice 21.00 + iManufacturer 1 STMicroelectronics + iProduct 2 STM32 BOOTLOADER + iSerial 3 315A28A0B956 + bNumConfigurations 1 + Configuration Descriptor: + bLength 9 + bDescriptorType 2 + wTotalLength 54 + bNumInterfaces 1 + bConfigurationValue 1 + iConfiguration 0 + bmAttributes 0xc0 + Self Powered + MaxPower 100mA + Interface Descriptor: + bLength 9 + bDescriptorType 4 + bInterfaceNumber 0 + bAlternateSetting 0 + bNumEndpoints 0 + bInterfaceClass 254 Application Specific Interface + bInterfaceSubClass 1 Device Firmware Update + bInterfaceProtocol 2 + iInterface 4 @Internal Flash /0x08000000/04*016Kg,01*064Kg,07*128Kg + Interface Descriptor: + bLength 9 + bDescriptorType 4 + bInterfaceNumber 0 + bAlternateSetting 1 + bNumEndpoints 0 + bInterfaceClass 254 Application Specific Interface + bInterfaceSubClass 1 Device Firmware Update + bInterfaceProtocol 2 + iInterface 5 @Option Bytes /0x1FFFC000/01*016 g + Interface Descriptor: + bLength 9 + bDescriptorType 4 + bInterfaceNumber 0 + bAlternateSetting 2 + bNumEndpoints 0 + bInterfaceClass 254 Application Specific Interface + bInterfaceSubClass 1 Device Firmware Update + bInterfaceProtocol 2 + iInterface 6 @OTP Memory /0x1FFF7800/01*512 g,01*016 g + Interface Descriptor: + bLength 9 + bDescriptorType 4 + bInterfaceNumber 0 + bAlternateSetting 3 + bNumEndpoints 0 + bInterfaceClass 254 Application Specific Interface + bInterfaceSubClass 1 Device Firmware Update + bInterfaceProtocol 2 + iInterface 7 @Device Feature/0xFFFF0000/01*004 g + Device Firmware Upgrade Interface Descriptor: + bLength 9 + bDescriptorType 33 + bmAttributes 11 + Will Detach + Manifestation Intolerant + Upload Supported + Download Supported + wDetachTimeout 255 milliseconds + wTransferSize 2048 bytes + bcdDFUVersion 1.1a +Device Status: 0x0001 + Self Powered diff --git a/tools/macosx/src/dfu-util/device-logs/tdk-bluetooth.lsusb b/tools/macosx/src/dfu-util/device-logs/tdk-bluetooth.lsusb new file mode 100644 index 0000000..c0cface --- /dev/null +++ b/tools/macosx/src/dfu-util/device-logs/tdk-bluetooth.lsusb @@ -0,0 +1,269 @@ + +Bus 006 Device 014: ID 04bf:0320 TDK Corp. Bluetooth Adapter +Device Descriptor: + bLength 18 + bDescriptorType 1 + bcdUSB 2.00 + bDeviceClass 224 Wireless + bDeviceSubClass 1 Radio Frequency + bDeviceProtocol 1 Bluetooth + bMaxPacketSize0 64 + idVendor 0x04bf TDK Corp. + idProduct 0x0320 Bluetooth Adapter + bcdDevice 26.52 + iManufacturer 1 Ezurio + iProduct 2 Turbo Bluetooth Adapter + iSerial 3 008098D4FFBD + bNumConfigurations 1 + Configuration Descriptor: + bLength 9 + bDescriptorType 2 + wTotalLength 193 + bNumInterfaces 3 + bConfigurationValue 1 + iConfiguration 0 + bmAttributes 0x80 + (Bus Powered) + MaxPower 64mA + Interface Descriptor: + bLength 9 + bDescriptorType 4 + bInterfaceNumber 0 + bAlternateSetting 0 + bNumEndpoints 3 + bInterfaceClass 224 Wireless + bInterfaceSubClass 1 Radio Frequency + bInterfaceProtocol 1 Bluetooth + iInterface 0 + Endpoint Descriptor: + bLength 7 + bDescriptorType 5 + bEndpointAddress 0x81 EP 1 IN + bmAttributes 3 + Transfer Type Interrupt + Synch Type None + Usage Type Data + wMaxPacketSize 0x0010 1x 16 bytes + bInterval 1 + Endpoint Descriptor: + bLength 7 + bDescriptorType 5 + bEndpointAddress 0x02 EP 2 OUT + bmAttributes 2 + Transfer Type Bulk + Synch Type None + Usage Type Data + wMaxPacketSize 0x0040 1x 64 bytes + bInterval 1 + Endpoint Descriptor: + bLength 7 + bDescriptorType 5 + bEndpointAddress 0x82 EP 2 IN + bmAttributes 2 + Transfer Type Bulk + Synch Type None + Usage Type Data + wMaxPacketSize 0x0040 1x 64 bytes + bInterval 1 + Interface Descriptor: + bLength 9 + bDescriptorType 4 + bInterfaceNumber 1 + bAlternateSetting 0 + bNumEndpoints 2 + bInterfaceClass 224 Wireless + bInterfaceSubClass 1 Radio Frequency + bInterfaceProtocol 1 Bluetooth + iInterface 0 + Endpoint Descriptor: + bLength 7 + bDescriptorType 5 + bEndpointAddress 0x03 EP 3 OUT + bmAttributes 1 + Transfer Type Isochronous + Synch Type None + Usage Type Data + wMaxPacketSize 0x0000 1x 0 bytes + bInterval 1 + Endpoint Descriptor: + bLength 7 + bDescriptorType 5 + bEndpointAddress 0x83 EP 3 IN + bmAttributes 1 + Transfer Type Isochronous + Synch Type None + Usage Type Data + wMaxPacketSize 0x0000 1x 0 bytes + bInterval 1 + Interface Descriptor: + bLength 9 + bDescriptorType 4 + bInterfaceNumber 1 + bAlternateSetting 1 + bNumEndpoints 2 + bInterfaceClass 224 Wireless + bInterfaceSubClass 1 Radio Frequency + bInterfaceProtocol 1 Bluetooth + iInterface 0 + Endpoint Descriptor: + bLength 7 + bDescriptorType 5 + bEndpointAddress 0x03 EP 3 OUT + bmAttributes 1 + Transfer Type Isochronous + Synch Type None + Usage Type Data + wMaxPacketSize 0x0009 1x 9 bytes + bInterval 1 + Endpoint Descriptor: + bLength 7 + bDescriptorType 5 + bEndpointAddress 0x83 EP 3 IN + bmAttributes 1 + Transfer Type Isochronous + Synch Type None + Usage Type Data + wMaxPacketSize 0x0009 1x 9 bytes + bInterval 1 + Interface Descriptor: + bLength 9 + bDescriptorType 4 + bInterfaceNumber 1 + bAlternateSetting 2 + bNumEndpoints 2 + bInterfaceClass 224 Wireless + bInterfaceSubClass 1 Radio Frequency + bInterfaceProtocol 1 Bluetooth + iInterface 0 + Endpoint Descriptor: + bLength 7 + bDescriptorType 5 + bEndpointAddress 0x03 EP 3 OUT + bmAttributes 1 + Transfer Type Isochronous + Synch Type None + Usage Type Data + wMaxPacketSize 0x0011 1x 17 bytes + bInterval 1 + Endpoint Descriptor: + bLength 7 + bDescriptorType 5 + bEndpointAddress 0x83 EP 3 IN + bmAttributes 1 + Transfer Type Isochronous + Synch Type None + Usage Type Data + wMaxPacketSize 0x0011 1x 17 bytes + bInterval 1 + Interface Descriptor: + bLength 9 + bDescriptorType 4 + bInterfaceNumber 1 + bAlternateSetting 3 + bNumEndpoints 2 + bInterfaceClass 224 Wireless + bInterfaceSubClass 1 Radio Frequency + bInterfaceProtocol 1 Bluetooth + iInterface 0 + Endpoint Descriptor: + bLength 7 + bDescriptorType 5 + bEndpointAddress 0x03 EP 3 OUT + bmAttributes 1 + Transfer Type Isochronous + Synch Type None + Usage Type Data + wMaxPacketSize 0x0019 1x 25 bytes + bInterval 1 + Endpoint Descriptor: + bLength 7 + bDescriptorType 5 + bEndpointAddress 0x83 EP 3 IN + bmAttributes 1 + Transfer Type Isochronous + Synch Type None + Usage Type Data + wMaxPacketSize 0x0019 1x 25 bytes + bInterval 1 + Interface Descriptor: + bLength 9 + bDescriptorType 4 + bInterfaceNumber 1 + bAlternateSetting 4 + bNumEndpoints 2 + bInterfaceClass 224 Wireless + bInterfaceSubClass 1 Radio Frequency + bInterfaceProtocol 1 Bluetooth + iInterface 0 + Endpoint Descriptor: + bLength 7 + bDescriptorType 5 + bEndpointAddress 0x03 EP 3 OUT + bmAttributes 1 + Transfer Type Isochronous + Synch Type None + Usage Type Data + wMaxPacketSize 0x0021 1x 33 bytes + bInterval 1 + Endpoint Descriptor: + bLength 7 + bDescriptorType 5 + bEndpointAddress 0x83 EP 3 IN + bmAttributes 1 + Transfer Type Isochronous + Synch Type None + Usage Type Data + wMaxPacketSize 0x0021 1x 33 bytes + bInterval 1 + Interface Descriptor: + bLength 9 + bDescriptorType 4 + bInterfaceNumber 1 + bAlternateSetting 5 + bNumEndpoints 2 + bInterfaceClass 224 Wireless + bInterfaceSubClass 1 Radio Frequency + bInterfaceProtocol 1 Bluetooth + iInterface 0 + Endpoint Descriptor: + bLength 7 + bDescriptorType 5 + bEndpointAddress 0x03 EP 3 OUT + bmAttributes 1 + Transfer Type Isochronous + Synch Type None + Usage Type Data + wMaxPacketSize 0x0031 1x 49 bytes + bInterval 1 + Endpoint Descriptor: + bLength 7 + bDescriptorType 5 + bEndpointAddress 0x83 EP 3 IN + bmAttributes 1 + Transfer Type Isochronous + Synch Type None + Usage Type Data + wMaxPacketSize 0x0031 1x 49 bytes + bInterval 1 + Interface Descriptor: + bLength 9 + bDescriptorType 4 + bInterfaceNumber 2 + bAlternateSetting 0 + bNumEndpoints 0 + bInterfaceClass 254 Application Specific Interface + bInterfaceSubClass 1 Device Firmware Update + bInterfaceProtocol 0 + iInterface 0 + Device Firmware Upgrade Interface Descriptor: + bLength 7 + bDescriptorType 33 + bmAttributes 7 + Will Not Detach + Manifestation Tolerant + Upload Supported + Download Supported + wDetachTimeout 5000 milliseconds + wTransferSize 1023 bytes +Device Status: 0x0000 + (Bus Powered) diff --git a/tools/macosx/src/dfu-util/dfuse-pack.py b/tools/macosx/src/dfu-util/dfuse-pack.py new file mode 100644 index 0000000..875cc5c --- /dev/null +++ b/tools/macosx/src/dfu-util/dfuse-pack.py @@ -0,0 +1,121 @@ +#!/usr/bin/python + +# Written by Antonio Galea - 2010/11/18 +# Distributed under Gnu LGPL 3.0 +# see http://www.gnu.org/licenses/lgpl-3.0.txt + +import sys,struct,zlib,os +from optparse import OptionParser + +DEFAULT_DEVICE="0x0483:0xdf11" + +def named(tuple,names): + return dict(zip(names.split(),tuple)) +def consume(fmt,data,names): + n = struct.calcsize(fmt) + return named(struct.unpack(fmt,data[:n]),names),data[n:] +def cstring(string): + return string.split('\0',1)[0] +def compute_crc(data): + return 0xFFFFFFFF & -zlib.crc32(data) -1 + +def parse(file,dump_images=False): + print 'File: "%s"' % file + data = open(file,'rb').read() + crc = compute_crc(data[:-4]) + prefix, data = consume('<5sBIB',data,'signature version size targets') + print '%(signature)s v%(version)d, image size: %(size)d, targets: %(targets)d' % prefix + for t in range(prefix['targets']): + tprefix, data = consume('<6sBI255s2I',data,'signature altsetting named name size elements') + tprefix['num'] = t + if tprefix['named']: + tprefix['name'] = cstring(tprefix['name']) + else: + tprefix['name'] = '' + print '%(signature)s %(num)d, alt setting: %(altsetting)s, name: "%(name)s", size: %(size)d, elements: %(elements)d' % tprefix + tsize = tprefix['size'] + target, data = data[:tsize], data[tsize:] + for e in range(tprefix['elements']): + eprefix, target = consume('<2I',target,'address size') + eprefix['num'] = e + print ' %(num)d, address: 0x%(address)08x, size: %(size)d' % eprefix + esize = eprefix['size'] + image, target = target[:esize], target[esize:] + if dump_images: + out = '%s.target%d.image%d.bin' % (file,t,e) + open(out,'wb').write(image) + print ' DUMPED IMAGE TO "%s"' % out + if len(target): + print "target %d: PARSE ERROR" % t + suffix = named(struct.unpack('<4H3sBI',data[:16]),'device product vendor dfu ufd len crc') + print 'usb: %(vendor)04x:%(product)04x, device: 0x%(device)04x, dfu: 0x%(dfu)04x, %(ufd)s, %(len)d, 0x%(crc)08x' % suffix + if crc != suffix['crc']: + print "CRC ERROR: computed crc32 is 0x%08x" % crc + data = data[16:] + if data: + print "PARSE ERROR" + +def build(file,targets,device=DEFAULT_DEVICE): + data = '' + for t,target in enumerate(targets): + tdata = '' + for image in target: + tdata += struct.pack('<2I',image['address'],len(image['data']))+image['data'] + tdata = struct.pack('<6sBI255s2I','Target',0,1,'ST...',len(tdata),len(target)) + tdata + data += tdata + data = struct.pack('<5sBIB','DfuSe',1,len(data)+11,len(targets)) + data + v,d=map(lambda x: int(x,0) & 0xFFFF, device.split(':',1)) + data += struct.pack('<4H3sB',0,d,v,0x011a,'UFD',16) + crc = compute_crc(data) + data += struct.pack(' and +Harald Welte . Over time, nearly complete +support of DFU 1.0, DFU 1.1 and DfuSe ("1.1a") has been added. +.SH LICENCE +.B dfu-util +is covered by the GNU General Public License (GPL), version 2 or later. +.SH COPYRIGHT +This manual page was originally written by Uwe Hermann , +and is now part of the dfu-util project. diff --git a/tools/macosx/src/dfu-util/msvc/README_msvc.txt b/tools/macosx/src/dfu-util/msvc/README_msvc.txt new file mode 100644 index 0000000..6e68ec6 --- /dev/null +++ b/tools/macosx/src/dfu-util/msvc/README_msvc.txt @@ -0,0 +1,10 @@ +# (C) Roger Meier +# (C) Pascal Schweizer +# msvc folder is GPL-2.0+, LGPL-2.1+, BSD-3-Clause or MIT license(SPDX) + +Building dfu-util native on Windows with Visual Studio + +3rd party dependencies: +- libusbx ( git clone https://github.com/libusbx/libusbx.git ) + - getopt (part of libusbx: libusbx/examples/getopt) + diff --git a/tools/macosx/src/dfu-util/msvc/dfu-suffix_2010.vcxproj b/tools/macosx/src/dfu-util/msvc/dfu-suffix_2010.vcxproj new file mode 100644 index 0000000..0c316c2 --- /dev/null +++ b/tools/macosx/src/dfu-util/msvc/dfu-suffix_2010.vcxproj @@ -0,0 +1,100 @@ + + + + + Debug + Win32 + + + Release + Win32 + + + + {8F7600A2-3B37-4956-B39B-A1D43EF29EDA} + dfusuffix + dfu-suffix + + + + Application + true + MultiByte + + + Application + false + true + MultiByte + + + + + + + + + + + + + $(SolutionDir)..\..\libusbx\examples\getopt;$(SolutionDir)..\..\libusbx\libusb;$(IncludePath) + $(SolutionDir)..\$(Platform)\$(ProjectName)\$(Configuration)\ + $(SolutionDir)..\$(Platform)\$(ProjectName)\$(Configuration)\ + $(SolutionDir)..\$(Platform)\$(ProjectName)\$(Configuration)\dll;$(LibraryPath) + $(VCInstallDir)atlmfc\lib;$(VCInstallDir)lib + $(ExecutablePath) + + + $(ExecutablePath) + $(SolutionDir)..\..\libusbx\examples\getopt;$(SolutionDir)..\..\libusbx\libusb;$(IncludePath) + $(SolutionDir)..\$(Platform)\$(ProjectName)\$(Configuration)\dll;$(LibraryPath) + $(SolutionDir)..\$(Platform)\$(ProjectName)\$(Configuration)\ + $(SolutionDir)..\$(Platform)\$(ProjectName)\$(Configuration)\ + + + + Level3 + Disabled + HAVE_WINDOWS_H;_CRT_SECURE_NO_WARNINGS;%(PreprocessorDefinitions) + MultiThreadedDebug + + + true + + + + + Level3 + MaxSpeed + true + true + HAVE_WINDOWS_H;_CRT_SECURE_NO_WARNINGS;%(PreprocessorDefinitions) + MultiThreaded + + + true + true + true + + + + + + + + + + + + + {a2169bc8-cf99-40bf-83f3-b0e38f7067bd} + + + {349ee8f9-7d25-4909-aaf5-ff3fade72187} + + + + + + \ No newline at end of file diff --git a/tools/macosx/src/dfu-util/msvc/dfu-util_2010.sln b/tools/macosx/src/dfu-util/msvc/dfu-util_2010.sln new file mode 100644 index 0000000..ef79723 --- /dev/null +++ b/tools/macosx/src/dfu-util/msvc/dfu-util_2010.sln @@ -0,0 +1,54 @@ + +Microsoft Visual Studio Solution File, Format Version 11.00 +# Visual Studio 2010 +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "dfu-util", "dfu-util_2010.vcxproj", "{0E071A60-7EF2-4427-BAA8-9143CACB5BCB}" +EndProject +Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{C4F8746D-B27E-4806-95E5-2052174E923B}" +EndProject +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "dfu-suffix", "dfu-suffix_2010.vcxproj", "{8F7600A2-3B37-4956-B39B-A1D43EF29EDA}" +EndProject +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "getopt_2010", "..\..\libusbx\msvc\getopt_2010.vcxproj", "{AE83E1B4-CE06-47EE-B7A3-C3A1D7C2D71E}" +EndProject +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "libusb-1.0 (static)", "..\..\libusbx\msvc\libusb_static_2010.vcxproj", "{349EE8F9-7D25-4909-AAF5-FF3FADE72187}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Win32 = Debug|Win32 + Debug|x64 = Debug|x64 + Release|Win32 = Release|Win32 + Release|x64 = Release|x64 + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {0E071A60-7EF2-4427-BAA8-9143CACB5BCB}.Debug|Win32.ActiveCfg = Debug|Win32 + {0E071A60-7EF2-4427-BAA8-9143CACB5BCB}.Debug|Win32.Build.0 = Debug|Win32 + {0E071A60-7EF2-4427-BAA8-9143CACB5BCB}.Debug|x64.ActiveCfg = Debug|Win32 + {0E071A60-7EF2-4427-BAA8-9143CACB5BCB}.Release|Win32.ActiveCfg = Release|Win32 + {0E071A60-7EF2-4427-BAA8-9143CACB5BCB}.Release|Win32.Build.0 = Release|Win32 + {0E071A60-7EF2-4427-BAA8-9143CACB5BCB}.Release|x64.ActiveCfg = Release|Win32 + {8F7600A2-3B37-4956-B39B-A1D43EF29EDA}.Debug|Win32.ActiveCfg = Debug|Win32 + {8F7600A2-3B37-4956-B39B-A1D43EF29EDA}.Debug|Win32.Build.0 = Debug|Win32 + {8F7600A2-3B37-4956-B39B-A1D43EF29EDA}.Debug|x64.ActiveCfg = Debug|Win32 + {8F7600A2-3B37-4956-B39B-A1D43EF29EDA}.Release|Win32.ActiveCfg = Release|Win32 + {8F7600A2-3B37-4956-B39B-A1D43EF29EDA}.Release|Win32.Build.0 = Release|Win32 + {8F7600A2-3B37-4956-B39B-A1D43EF29EDA}.Release|x64.ActiveCfg = Release|Win32 + {AE83E1B4-CE06-47EE-B7A3-C3A1D7C2D71E}.Debug|Win32.ActiveCfg = Debug|Win32 + {AE83E1B4-CE06-47EE-B7A3-C3A1D7C2D71E}.Debug|Win32.Build.0 = Debug|Win32 + {AE83E1B4-CE06-47EE-B7A3-C3A1D7C2D71E}.Debug|x64.ActiveCfg = Debug|x64 + {AE83E1B4-CE06-47EE-B7A3-C3A1D7C2D71E}.Debug|x64.Build.0 = Debug|x64 + {AE83E1B4-CE06-47EE-B7A3-C3A1D7C2D71E}.Release|Win32.ActiveCfg = Release|Win32 + {AE83E1B4-CE06-47EE-B7A3-C3A1D7C2D71E}.Release|Win32.Build.0 = Release|Win32 + {AE83E1B4-CE06-47EE-B7A3-C3A1D7C2D71E}.Release|x64.ActiveCfg = Release|x64 + {AE83E1B4-CE06-47EE-B7A3-C3A1D7C2D71E}.Release|x64.Build.0 = Release|x64 + {349EE8F9-7D25-4909-AAF5-FF3FADE72187}.Debug|Win32.ActiveCfg = Debug|Win32 + {349EE8F9-7D25-4909-AAF5-FF3FADE72187}.Debug|Win32.Build.0 = Debug|Win32 + {349EE8F9-7D25-4909-AAF5-FF3FADE72187}.Debug|x64.ActiveCfg = Debug|x64 + {349EE8F9-7D25-4909-AAF5-FF3FADE72187}.Debug|x64.Build.0 = Debug|x64 + {349EE8F9-7D25-4909-AAF5-FF3FADE72187}.Release|Win32.ActiveCfg = Release|Win32 + {349EE8F9-7D25-4909-AAF5-FF3FADE72187}.Release|Win32.Build.0 = Release|Win32 + {349EE8F9-7D25-4909-AAF5-FF3FADE72187}.Release|x64.ActiveCfg = Release|x64 + {349EE8F9-7D25-4909-AAF5-FF3FADE72187}.Release|x64.Build.0 = Release|x64 + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection +EndGlobal diff --git a/tools/macosx/src/dfu-util/msvc/dfu-util_2010.vcxproj b/tools/macosx/src/dfu-util/msvc/dfu-util_2010.vcxproj new file mode 100644 index 0000000..17a8bee --- /dev/null +++ b/tools/macosx/src/dfu-util/msvc/dfu-util_2010.vcxproj @@ -0,0 +1,120 @@ + + + + + Debug + Win32 + + + Release + Win32 + + + + {0E071A60-7EF2-4427-BAA8-9143CACB5BCB} + dfuutil + dfu-util + + + + Application + true + MultiByte + + + Application + false + true + MultiByte + + + + + + + + + + + + + $(SolutionDir)..\..\libusbx\examples\getopt;$(SolutionDir)..\..\libusbx\libusb;$(IncludePath) + $(SolutionDir)..\$(Platform)\$(ProjectName)\$(Configuration)\ + $(SolutionDir)..\$(Platform)\$(ProjectName)\$(Configuration)\ + $(SolutionDir)..\$(Platform)\getopt\$(Configuration);$(LibraryPath) + $(VCInstallDir)atlmfc\lib;$(VCInstallDir)lib + $(ExecutablePath) + + + $(ExecutablePath) + $(SolutionDir)..\..\libusbx\examples\getopt;$(SolutionDir)..\..\libusbx\libusb;$(IncludePath) + $(SolutionDir)..\$(Platform)\getopt\$(Configuration);$(LibraryPath) + $(SolutionDir)..\$(Platform)\$(ProjectName)\$(Configuration)\ + $(SolutionDir)..\$(Platform)\$(ProjectName)\$(Configuration)\ + + + + Level3 + Disabled + HAVE_WINDOWS_H;_CRT_SECURE_NO_WARNINGS;%(PreprocessorDefinitions) + MultiThreadedDebug + + + true + + + + + + + + + Level3 + MaxSpeed + true + true + HAVE_WINDOWS_H;_CRT_SECURE_NO_WARNINGS;%(PreprocessorDefinitions) + MultiThreaded + + + true + true + true + + + copy $(SolutionDir)..\$(Platform)\$(Configuration)\dll\libusb-1.0.dll $(SolutionDir)..\$(Platform)\$(ProjectName)\$(Configuration)\ + + + + + + + + + + + + + + + + + + + + + + + + + + {a2169bc8-cf99-40bf-83f3-b0e38f7067bd} + + + {349ee8f9-7d25-4909-aaf5-ff3fade72187} + + + + + + \ No newline at end of file diff --git a/tools/macosx/src/dfu-util/src/Makefile.am b/tools/macosx/src/dfu-util/src/Makefile.am new file mode 100644 index 0000000..70179c4 --- /dev/null +++ b/tools/macosx/src/dfu-util/src/Makefile.am @@ -0,0 +1,28 @@ +AM_CFLAGS = -Wall -Wextra + +bin_PROGRAMS = dfu-util dfu-suffix dfu-prefix +dfu_util_SOURCES = main.c \ + portable.h \ + dfu_load.c \ + dfu_load.h \ + dfu_util.c \ + dfu_util.h \ + dfuse.c \ + dfuse.h \ + dfuse_mem.c \ + dfuse_mem.h \ + dfu.c \ + dfu.h \ + usb_dfu.h \ + dfu_file.c \ + dfu_file.h \ + quirks.c \ + quirks.h + +dfu_suffix_SOURCES = suffix.c \ + dfu_file.h \ + dfu_file.c + +dfu_prefix_SOURCES = prefix.c \ + dfu_file.h \ + dfu_file.c diff --git a/tools/macosx/src/dfu-util/src/dfu.c b/tools/macosx/src/dfu-util/src/dfu.c new file mode 100644 index 0000000..14d7673 --- /dev/null +++ b/tools/macosx/src/dfu-util/src/dfu.c @@ -0,0 +1,357 @@ +/* + * Low-level DFU communication routines, originally taken from + * $Id: dfu.c,v 1.3 2006/06/20 06:28:04 schmidtw Exp $ + * (part of dfu-programmer). + * + * Copyright 2005-2006 Weston Schmidt + * Copyright 2011-2014 Tormod Volden + * + * 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 +#include + +#include + +#include "portable.h" +#include "dfu.h" +#include "quirks.h" + +static int dfu_timeout = 5000; /* 5 seconds - default */ + +/* + * 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( libusb_device_handle *device, + const unsigned short interface, + const unsigned short timeout ) +{ + return libusb_control_transfer( device, + /* bmRequestType */ LIBUSB_ENDPOINT_OUT | LIBUSB_REQUEST_TYPE_CLASS | LIBUSB_RECIPIENT_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( libusb_device_handle *device, + const unsigned short interface, + const unsigned short length, + const unsigned short transaction, + unsigned char* data ) +{ + int status; + + status = libusb_control_transfer( device, + /* bmRequestType */ LIBUSB_ENDPOINT_OUT | LIBUSB_REQUEST_TYPE_CLASS | LIBUSB_RECIPIENT_INTERFACE, + /* bRequest */ DFU_DNLOAD, + /* wValue */ transaction, + /* wIndex */ interface, + /* Data */ data, + /* wLength */ length, + dfu_timeout ); + 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( libusb_device_handle *device, + const unsigned short interface, + const unsigned short length, + const unsigned short transaction, + unsigned char* data ) +{ + int status; + + status = libusb_control_transfer( device, + /* bmRequestType */ LIBUSB_ENDPOINT_IN | LIBUSB_REQUEST_TYPE_CLASS | LIBUSB_RECIPIENT_INTERFACE, + /* bRequest */ DFU_UPLOAD, + /* wValue */ transaction, + /* wIndex */ interface, + /* Data */ data, + /* wLength */ length, + dfu_timeout ); + 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 dfu_if *dif, struct dfu_status *status ) +{ + unsigned char buffer[6]; + int result; + + /* Initialize the status data structure */ + status->bStatus = DFU_STATUS_ERROR_UNKNOWN; + status->bwPollTimeout = 0; + status->bState = STATE_DFU_ERROR; + status->iString = 0; + + result = libusb_control_transfer( dif->dev_handle, + /* bmRequestType */ LIBUSB_ENDPOINT_IN | LIBUSB_REQUEST_TYPE_CLASS | LIBUSB_RECIPIENT_INTERFACE, + /* bRequest */ DFU_GETSTATUS, + /* wValue */ 0, + /* wIndex */ dif->interface, + /* Data */ buffer, + /* wLength */ 6, + dfu_timeout ); + + if( 6 == result ) { + status->bStatus = buffer[0]; + if (dif->quirks & QUIRK_POLLTIMEOUT) + status->bwPollTimeout = DEFAULT_POLLTIMEOUT; + else + 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( libusb_device_handle *device, + const unsigned short interface ) +{ + return libusb_control_transfer( device, + /* bmRequestType */ LIBUSB_ENDPOINT_OUT| LIBUSB_REQUEST_TYPE_CLASS | LIBUSB_RECIPIENT_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( libusb_device_handle *device, + const unsigned short interface ) +{ + int result; + unsigned char buffer[1]; + + result = libusb_control_transfer( device, + /* bmRequestType */ LIBUSB_ENDPOINT_IN | LIBUSB_REQUEST_TYPE_CLASS | LIBUSB_RECIPIENT_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 -1; + + /* 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( libusb_device_handle *device, + const unsigned short interface ) +{ + return libusb_control_transfer( device, + /* bmRequestType */ LIBUSB_ENDPOINT_OUT | LIBUSB_REQUEST_TYPE_CLASS | LIBUSB_RECIPIENT_INTERFACE, + /* bRequest */ DFU_ABORT, + /* wValue */ 0, + /* wIndex */ interface, + /* Data */ NULL, + /* wLength */ 0, + dfu_timeout ); +} + + +const char* dfu_state_to_string( int state ) +{ + const char *message; + + 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; + default: + message = NULL; + 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 memory 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]; +} + +int dfu_abort_to_idle(struct dfu_if *dif) +{ + int ret; + struct dfu_status dst; + + ret = dfu_abort(dif->dev_handle, dif->interface); + if (ret < 0) { + errx(EX_IOERR, "Error sending dfu abort request"); + exit(1); + } + ret = dfu_get_status(dif, &dst); + if (ret < 0) { + errx(EX_IOERR, "Error during abort get_status"); + exit(1); + } + if (dst.bState != DFU_STATE_dfuIDLE) { + errx(EX_IOERR, "Failed to enter idle state on abort"); + exit(1); + } + milli_sleep(dst.bwPollTimeout); + return ret; +} diff --git a/tools/macosx/src/dfu-util/src/dfu.h b/tools/macosx/src/dfu-util/src/dfu.h new file mode 100644 index 0000000..8e3caeb --- /dev/null +++ b/tools/macosx/src/dfu-util/src/dfu.h @@ -0,0 +1,133 @@ +/* + * 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 +#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 + +/* DFU commands */ +#define DFU_DETACH 0 +#define DFU_DNLOAD 1 +#define DFU_UPLOAD 2 +#define DFU_GETSTATUS 3 +#define DFU_CLRSTATUS 4 +#define DFU_GETSTATE 5 +#define DFU_ABORT 6 + +/* DFU interface */ +#define DFU_IFF_DFU 0x0001 /* DFU Mode, (not Runtime) */ + +/* 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; +}; + +struct dfu_if { + struct usb_dfu_func_descriptor func_dfu; + uint16_t quirks; + uint16_t busnum; + uint16_t devnum; + uint16_t vendor; + uint16_t product; + uint16_t bcdDevice; + uint8_t configuration; + uint8_t interface; + uint8_t altsetting; + uint8_t flags; + uint8_t bMaxPacketSize0; + char *alt_name; + char *serial_name; + libusb_device *dev; + libusb_device_handle *dev_handle; + struct dfu_if *next; +}; + +int dfu_detach( libusb_device_handle *device, + const unsigned short interface, + const unsigned short timeout ); +int dfu_download( libusb_device_handle *device, + const unsigned short interface, + const unsigned short length, + const unsigned short transaction, + unsigned char* data ); +int dfu_upload( libusb_device_handle *device, + const unsigned short interface, + const unsigned short length, + const unsigned short transaction, + unsigned char* data ); +int dfu_get_status( struct dfu_if *dif, + struct dfu_status *status ); +int dfu_clear_status( libusb_device_handle *device, + const unsigned short interface ); +int dfu_get_state( libusb_device_handle *device, + const unsigned short interface ); +int dfu_abort( libusb_device_handle *device, + const unsigned short interface ); +int dfu_abort_to_idle( struct dfu_if *dif); + +const char *dfu_state_to_string( int state ); + +const char *dfu_status_to_string( int status ); + +#endif /* DFU_H */ diff --git a/tools/macosx/src/dfu-util/src/dfu_file.c b/tools/macosx/src/dfu-util/src/dfu_file.c new file mode 100644 index 0000000..7c897d4 --- /dev/null +++ b/tools/macosx/src/dfu-util/src/dfu_file.c @@ -0,0 +1,444 @@ +/* + * Load or store DFU files including suffix and prefix + * + * Copyright 2014 Tormod Volden + * Copyright 2012 Stefan Schmidt + * + * 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 +#include +#include +#include +#include +#include +#include + +#include "portable.h" +#include "dfu_file.h" + +#define DFU_SUFFIX_LENGTH 16 +#define LMDFU_PREFIX_LENGTH 8 +#define LPCDFU_PREFIX_LENGTH 16 +#define PROGRESS_BAR_WIDTH 25 +#define STDIN_CHUNK_SIZE 65536 + +static const unsigned long crc32_table[] = { + 0x00000000, 0x77073096, 0xee0e612c, 0x990951ba, 0x076dc419, 0x706af48f, + 0xe963a535, 0x9e6495a3, 0x0edb8832, 0x79dcb8a4, 0xe0d5e91e, 0x97d2d988, + 0x09b64c2b, 0x7eb17cbd, 0xe7b82d07, 0x90bf1d91, 0x1db71064, 0x6ab020f2, + 0xf3b97148, 0x84be41de, 0x1adad47d, 0x6ddde4eb, 0xf4d4b551, 0x83d385c7, + 0x136c9856, 0x646ba8c0, 0xfd62f97a, 0x8a65c9ec, 0x14015c4f, 0x63066cd9, + 0xfa0f3d63, 0x8d080df5, 0x3b6e20c8, 0x4c69105e, 0xd56041e4, 0xa2677172, + 0x3c03e4d1, 0x4b04d447, 0xd20d85fd, 0xa50ab56b, 0x35b5a8fa, 0x42b2986c, + 0xdbbbc9d6, 0xacbcf940, 0x32d86ce3, 0x45df5c75, 0xdcd60dcf, 0xabd13d59, + 0x26d930ac, 0x51de003a, 0xc8d75180, 0xbfd06116, 0x21b4f4b5, 0x56b3c423, + 0xcfba9599, 0xb8bda50f, 0x2802b89e, 0x5f058808, 0xc60cd9b2, 0xb10be924, + 0x2f6f7c87, 0x58684c11, 0xc1611dab, 0xb6662d3d, 0x76dc4190, 0x01db7106, + 0x98d220bc, 0xefd5102a, 0x71b18589, 0x06b6b51f, 0x9fbfe4a5, 0xe8b8d433, + 0x7807c9a2, 0x0f00f934, 0x9609a88e, 0xe10e9818, 0x7f6a0dbb, 0x086d3d2d, + 0x91646c97, 0xe6635c01, 0x6b6b51f4, 0x1c6c6162, 0x856530d8, 0xf262004e, + 0x6c0695ed, 0x1b01a57b, 0x8208f4c1, 0xf50fc457, 0x65b0d9c6, 0x12b7e950, + 0x8bbeb8ea, 0xfcb9887c, 0x62dd1ddf, 0x15da2d49, 0x8cd37cf3, 0xfbd44c65, + 0x4db26158, 0x3ab551ce, 0xa3bc0074, 0xd4bb30e2, 0x4adfa541, 0x3dd895d7, + 0xa4d1c46d, 0xd3d6f4fb, 0x4369e96a, 0x346ed9fc, 0xad678846, 0xda60b8d0, + 0x44042d73, 0x33031de5, 0xaa0a4c5f, 0xdd0d7cc9, 0x5005713c, 0x270241aa, + 0xbe0b1010, 0xc90c2086, 0x5768b525, 0x206f85b3, 0xb966d409, 0xce61e49f, + 0x5edef90e, 0x29d9c998, 0xb0d09822, 0xc7d7a8b4, 0x59b33d17, 0x2eb40d81, + 0xb7bd5c3b, 0xc0ba6cad, 0xedb88320, 0x9abfb3b6, 0x03b6e20c, 0x74b1d29a, + 0xead54739, 0x9dd277af, 0x04db2615, 0x73dc1683, 0xe3630b12, 0x94643b84, + 0x0d6d6a3e, 0x7a6a5aa8, 0xe40ecf0b, 0x9309ff9d, 0x0a00ae27, 0x7d079eb1, + 0xf00f9344, 0x8708a3d2, 0x1e01f268, 0x6906c2fe, 0xf762575d, 0x806567cb, + 0x196c3671, 0x6e6b06e7, 0xfed41b76, 0x89d32be0, 0x10da7a5a, 0x67dd4acc, + 0xf9b9df6f, 0x8ebeeff9, 0x17b7be43, 0x60b08ed5, 0xd6d6a3e8, 0xa1d1937e, + 0x38d8c2c4, 0x4fdff252, 0xd1bb67f1, 0xa6bc5767, 0x3fb506dd, 0x48b2364b, + 0xd80d2bda, 0xaf0a1b4c, 0x36034af6, 0x41047a60, 0xdf60efc3, 0xa867df55, + 0x316e8eef, 0x4669be79, 0xcb61b38c, 0xbc66831a, 0x256fd2a0, 0x5268e236, + 0xcc0c7795, 0xbb0b4703, 0x220216b9, 0x5505262f, 0xc5ba3bbe, 0xb2bd0b28, + 0x2bb45a92, 0x5cb36a04, 0xc2d7ffa7, 0xb5d0cf31, 0x2cd99e8b, 0x5bdeae1d, + 0x9b64c2b0, 0xec63f226, 0x756aa39c, 0x026d930a, 0x9c0906a9, 0xeb0e363f, + 0x72076785, 0x05005713, 0x95bf4a82, 0xe2b87a14, 0x7bb12bae, 0x0cb61b38, + 0x92d28e9b, 0xe5d5be0d, 0x7cdcefb7, 0x0bdbdf21, 0x86d3d2d4, 0xf1d4e242, + 0x68ddb3f8, 0x1fda836e, 0x81be16cd, 0xf6b9265b, 0x6fb077e1, 0x18b74777, + 0x88085ae6, 0xff0f6a70, 0x66063bca, 0x11010b5c, 0x8f659eff, 0xf862ae69, + 0x616bffd3, 0x166ccf45, 0xa00ae278, 0xd70dd2ee, 0x4e048354, 0x3903b3c2, + 0xa7672661, 0xd06016f7, 0x4969474d, 0x3e6e77db, 0xaed16a4a, 0xd9d65adc, + 0x40df0b66, 0x37d83bf0, 0xa9bcae53, 0xdebb9ec5, 0x47b2cf7f, 0x30b5ffe9, + 0xbdbdf21c, 0xcabac28a, 0x53b39330, 0x24b4a3a6, 0xbad03605, 0xcdd70693, + 0x54de5729, 0x23d967bf, 0xb3667a2e, 0xc4614ab8, 0x5d681b02, 0x2a6f2b94, + 0xb40bbe37, 0xc30c8ea1, 0x5a05df1b, 0x2d02ef8d}; + +static uint32_t crc32_byte(uint32_t accum, uint8_t delta) +{ + return crc32_table[(accum ^ delta) & 0xff] ^ (accum >> 8); +} + +static int probe_prefix(struct dfu_file *file) +{ + uint8_t *prefix = file->firmware; + + if (file->size.total < LMDFU_PREFIX_LENGTH) + return 1; + if ((prefix[0] == 0x01) && (prefix[1] == 0x00)) { + file->prefix_type = LMDFU_PREFIX; + file->size.prefix = LMDFU_PREFIX_LENGTH; + file->lmdfu_address = 1024 * ((prefix[3] << 8) | prefix[2]); + } + else if (((prefix[0] & 0x3f) == 0x1a) && ((prefix[1] & 0x3f)== 0x3f)) { + file->prefix_type = LPCDFU_UNENCRYPTED_PREFIX; + file->size.prefix = LPCDFU_PREFIX_LENGTH; + } + + if (file->size.prefix + file->size.suffix > file->size.total) + return 1; + return 0; +} + +void dfu_progress_bar(const char *desc, unsigned long long curr, + unsigned long long max) +{ + static char buf[PROGRESS_BAR_WIDTH + 1]; + static unsigned long long last_progress = -1; + static time_t last_time; + time_t curr_time = time(NULL); + unsigned long long progress; + unsigned long long x; + + /* check for not known maximum */ + if (max < curr) + max = curr + 1; + /* make none out of none give zero */ + if (max == 0 && curr == 0) + max = 1; + + /* compute completion */ + progress = (PROGRESS_BAR_WIDTH * curr) / max; + if (progress > PROGRESS_BAR_WIDTH) + progress = PROGRESS_BAR_WIDTH; + if (progress == last_progress && + curr_time == last_time) + return; + last_progress = progress; + last_time = curr_time; + + for (x = 0; x != PROGRESS_BAR_WIDTH; x++) { + if (x < progress) + buf[x] = '='; + else + buf[x] = ' '; + } + buf[x] = 0; + + printf("\r%s\t[%s] %3lld%% %12lld bytes", desc, buf, + (100ULL * curr) / max, curr); + + if (progress == PROGRESS_BAR_WIDTH) + printf("\n%s done.\n", desc); +} + +void *dfu_malloc(size_t size) +{ + void *ptr = malloc(size); + if (ptr == NULL) + errx(EX_SOFTWARE, "Cannot allocate memory of size %d bytes", (int)size); + return (ptr); +} + +uint32_t dfu_file_write_crc(int f, uint32_t crc, const void *buf, int size) +{ + int x; + + /* compute CRC */ + for (x = 0; x != size; x++) + crc = crc32_byte(crc, ((uint8_t *)buf)[x]); + + /* write data */ + if (write(f, buf, size) != size) + err(EX_IOERR, "Could not write %d bytes to file %d", size, f); + + return (crc); +} + +void dfu_load_file(struct dfu_file *file, enum suffix_req check_suffix, enum prefix_req check_prefix) +{ + off_t offset; + int f; + int i; + int res; + + file->size.prefix = 0; + file->size.suffix = 0; + + /* default values, if no valid suffix is found */ + file->bcdDFU = 0; + file->idVendor = 0xffff; /* wildcard value */ + file->idProduct = 0xffff; /* wildcard value */ + file->bcdDevice = 0xffff; /* wildcard value */ + + /* default values, if no valid prefix is found */ + file->lmdfu_address = 0; + + free(file->firmware); + + if (!strcmp(file->name, "-")) { + int read_bytes; + +#ifdef WIN32 + _setmode( _fileno( stdin ), _O_BINARY ); +#endif + file->firmware = (uint8_t*) dfu_malloc(STDIN_CHUNK_SIZE); + read_bytes = fread(file->firmware, 1, STDIN_CHUNK_SIZE, stdin); + file->size.total = read_bytes; + while (read_bytes == STDIN_CHUNK_SIZE) { + file->firmware = (uint8_t*) realloc(file->firmware, file->size.total + STDIN_CHUNK_SIZE); + if (!file->firmware) + err(EX_IOERR, "Could not allocate firmware buffer"); + read_bytes = fread(file->firmware + file->size.total, 1, STDIN_CHUNK_SIZE, stdin); + file->size.total += read_bytes; + } + if (verbose) + printf("Read %i bytes from stdin\n", file->size.total); + /* Never require suffix when reading from stdin */ + check_suffix = MAYBE_SUFFIX; + } else { + f = open(file->name, O_RDONLY | O_BINARY); + if (f < 0) + err(EX_IOERR, "Could not open file %s for reading", file->name); + + offset = lseek(f, 0, SEEK_END); + + if ((int)offset < 0 || (int)offset != offset) + err(EX_IOERR, "File size is too big"); + + if (lseek(f, 0, SEEK_SET) != 0) + err(EX_IOERR, "Could not seek to beginning"); + + file->size.total = offset; + file->firmware = dfu_malloc(file->size.total); + + if (read(f, file->firmware, file->size.total) != file->size.total) { + err(EX_IOERR, "Could not read %d bytes from %s", + file->size.total, file->name); + } + close(f); + } + + /* Check for possible DFU file suffix by trying to parse one */ + { + uint32_t crc = 0xffffffff; + const uint8_t *dfusuffix; + int missing_suffix = 0; + const char *reason; + + if (file->size.total < DFU_SUFFIX_LENGTH) { + reason = "File too short for DFU suffix"; + missing_suffix = 1; + goto checked; + } + + dfusuffix = file->firmware + file->size.total - + DFU_SUFFIX_LENGTH; + + for (i = 0; i < file->size.total - 4; i++) + crc = crc32_byte(crc, file->firmware[i]); + + if (dfusuffix[10] != 'D' || + dfusuffix[9] != 'F' || + dfusuffix[8] != 'U') { + reason = "Invalid DFU suffix signature"; + missing_suffix = 1; + goto checked; + } + + file->dwCRC = (dfusuffix[15] << 24) + + (dfusuffix[14] << 16) + + (dfusuffix[13] << 8) + + dfusuffix[12]; + + if (file->dwCRC != crc) { + reason = "DFU suffix CRC does not match"; + missing_suffix = 1; + goto checked; + } + + /* At this point we believe we have a DFU suffix + so we require further checks to succeed */ + + file->bcdDFU = (dfusuffix[7] << 8) + dfusuffix[6]; + + if (verbose) + printf("DFU suffix version %x\n", file->bcdDFU); + + file->size.suffix = dfusuffix[11]; + + if (file->size.suffix < DFU_SUFFIX_LENGTH) { + errx(EX_IOERR, "Unsupported DFU suffix length %d", + file->size.suffix); + } + + if (file->size.suffix > file->size.total) { + errx(EX_IOERR, "Invalid DFU suffix length %d", + file->size.suffix); + } + + file->idVendor = (dfusuffix[5] << 8) + dfusuffix[4]; + file->idProduct = (dfusuffix[3] << 8) + dfusuffix[2]; + file->bcdDevice = (dfusuffix[1] << 8) + dfusuffix[0]; + +checked: + if (missing_suffix) { + if (check_suffix == NEEDS_SUFFIX) { + warnx("%s", reason); + errx(EX_IOERR, "Valid DFU suffix needed"); + } else if (check_suffix == MAYBE_SUFFIX) { + warnx("%s", reason); + warnx("A valid DFU suffix will be required in " + "a future dfu-util release!!!"); + } + } else { + if (check_suffix == NO_SUFFIX) { + errx(EX_SOFTWARE, "Please remove existing DFU suffix before adding a new one.\n"); + } + } + } + res = probe_prefix(file); + if ((res || file->size.prefix == 0) && check_prefix == NEEDS_PREFIX) + errx(EX_IOERR, "Valid DFU prefix needed"); + if (file->size.prefix && check_prefix == NO_PREFIX) + errx(EX_IOERR, "A prefix already exists, please delete it first"); + if (file->size.prefix && verbose) { + uint8_t *data = file->firmware; + if (file->prefix_type == LMDFU_PREFIX) + printf("Possible TI Stellaris DFU prefix with " + "the following properties\n" + "Address: 0x%08x\n" + "Payload length: %d\n", + file->lmdfu_address, + data[4] | (data[5] << 8) | + (data[6] << 16) | (data[7] << 14)); + else if (file->prefix_type == LPCDFU_UNENCRYPTED_PREFIX) + printf("Possible unencrypted NXP LPC DFU prefix with " + "the following properties\n" + "Payload length: %d kiByte\n", + data[2] >>1 | (data[3] << 7) ); + else + errx(EX_IOERR, "Unknown DFU prefix type"); + } +} + +void dfu_store_file(struct dfu_file *file, int write_suffix, int write_prefix) +{ + uint32_t crc = 0xffffffff; + int f; + + f = open(file->name, O_WRONLY | O_BINARY | O_TRUNC | O_CREAT, 0666); + if (f < 0) + err(EX_IOERR, "Could not open file %s for writing", file->name); + + /* write prefix, if any */ + if (write_prefix) { + if (file->prefix_type == LMDFU_PREFIX) { + uint8_t lmdfu_prefix[LMDFU_PREFIX_LENGTH]; + uint32_t addr = file->lmdfu_address / 1024; + + /* lmdfu_dfu_prefix payload length excludes prefix and suffix */ + uint32_t len = file->size.total - + file->size.prefix - file->size.suffix; + + lmdfu_prefix[0] = 0x01; /* STELLARIS_DFU_PROG */ + lmdfu_prefix[1] = 0x00; /* Reserved */ + lmdfu_prefix[2] = (uint8_t)(addr & 0xff); + lmdfu_prefix[3] = (uint8_t)(addr >> 8); + lmdfu_prefix[4] = (uint8_t)(len & 0xff); + lmdfu_prefix[5] = (uint8_t)(len >> 8) & 0xff; + lmdfu_prefix[6] = (uint8_t)(len >> 16) & 0xff; + lmdfu_prefix[7] = (uint8_t)(len >> 24); + + crc = dfu_file_write_crc(f, crc, lmdfu_prefix, LMDFU_PREFIX_LENGTH); + } + if (file->prefix_type == LPCDFU_UNENCRYPTED_PREFIX) { + uint8_t lpcdfu_prefix[LPCDFU_PREFIX_LENGTH] = {0}; + int i; + + /* Payload is firmware and prefix rounded to 512 bytes */ + uint32_t len = (file->size.total - file->size.suffix + 511) /512; + + lpcdfu_prefix[0] = 0x1a; /* Unencypted*/ + lpcdfu_prefix[1] = 0x3f; /* Reserved */ + lpcdfu_prefix[2] = (uint8_t)(len & 0xff); + lpcdfu_prefix[3] = (uint8_t)((len >> 8) & 0xff); + for (i = 12; i < LPCDFU_PREFIX_LENGTH; i++) + lpcdfu_prefix[i] = 0xff; + + crc = dfu_file_write_crc(f, crc, lpcdfu_prefix, LPCDFU_PREFIX_LENGTH); + } + } + /* write firmware binary */ + crc = dfu_file_write_crc(f, crc, file->firmware + file->size.prefix, + file->size.total - file->size.prefix - file->size.suffix); + + /* write suffix, if any */ + if (write_suffix) { + uint8_t dfusuffix[DFU_SUFFIX_LENGTH]; + + dfusuffix[0] = file->bcdDevice & 0xff; + dfusuffix[1] = file->bcdDevice >> 8; + dfusuffix[2] = file->idProduct & 0xff; + dfusuffix[3] = file->idProduct >> 8; + dfusuffix[4] = file->idVendor & 0xff; + dfusuffix[5] = file->idVendor >> 8; + dfusuffix[6] = file->bcdDFU & 0xff; + dfusuffix[7] = file->bcdDFU >> 8; + dfusuffix[8] = 'U'; + dfusuffix[9] = 'F'; + dfusuffix[10] = 'D'; + dfusuffix[11] = DFU_SUFFIX_LENGTH; + + crc = dfu_file_write_crc(f, crc, dfusuffix, + DFU_SUFFIX_LENGTH - 4); + + dfusuffix[12] = crc; + dfusuffix[13] = crc >> 8; + dfusuffix[14] = crc >> 16; + dfusuffix[15] = crc >> 24; + + crc = dfu_file_write_crc(f, crc, dfusuffix + 12, 4); + } + close(f); +} + +void show_suffix_and_prefix(struct dfu_file *file) +{ + if (file->size.prefix == LMDFU_PREFIX_LENGTH) { + printf("The file %s contains a TI Stellaris DFU prefix with the following properties:\n", file->name); + printf("Address:\t0x%08x\n", file->lmdfu_address); + } else if (file->size.prefix == LPCDFU_PREFIX_LENGTH) { + uint8_t * prefix = file->firmware; + printf("The file %s contains a NXP unencrypted LPC DFU prefix with the following properties:\n", file->name); + printf("Size:\t%5d kiB\n", prefix[2]>>1|prefix[3]<<7); + } else if (file->size.prefix != 0) { + printf("The file %s contains an unknown prefix\n", file->name); + } + if (file->size.suffix > 0) { + printf("The file %s contains a DFU suffix with the following properties:\n", file->name); + printf("BCD device:\t0x%04X\n", file->bcdDevice); + printf("Product ID:\t0x%04X\n",file->idProduct); + printf("Vendor ID:\t0x%04X\n", file->idVendor); + printf("BCD DFU:\t0x%04X\n", file->bcdDFU); + printf("Length:\t\t%i\n", file->size.suffix); + printf("CRC:\t\t0x%08X\n", file->dwCRC); + } +} diff --git a/tools/macosx/src/dfu-util/src/dfu_file.h b/tools/macosx/src/dfu-util/src/dfu_file.h new file mode 100644 index 0000000..abebd44 --- /dev/null +++ b/tools/macosx/src/dfu-util/src/dfu_file.h @@ -0,0 +1,60 @@ + +#ifndef DFU_FILE_H +#define DFU_FILE_H + +#include + +struct dfu_file { + /* File name */ + const char *name; + /* Pointer to file loaded into memory */ + uint8_t *firmware; + /* Different sizes */ + struct { + int total; + int prefix; + int suffix; + } size; + /* From prefix fields */ + uint32_t lmdfu_address; + /* From prefix fields */ + uint32_t prefix_type; + + /* From DFU suffix fields */ + uint32_t dwCRC; + uint16_t bcdDFU; + uint16_t idVendor; + uint16_t idProduct; + uint16_t bcdDevice; +}; + +enum suffix_req { + NO_SUFFIX, + NEEDS_SUFFIX, + MAYBE_SUFFIX +}; + +enum prefix_req { + NO_PREFIX, + NEEDS_PREFIX, + MAYBE_PREFIX +}; + +enum prefix_type { + ZERO_PREFIX, + LMDFU_PREFIX, + LPCDFU_UNENCRYPTED_PREFIX +}; + +extern int verbose; + +void dfu_load_file(struct dfu_file *file, enum suffix_req check_suffix, enum prefix_req check_prefix); +void dfu_store_file(struct dfu_file *file, int write_suffix, int write_prefix); + +void dfu_progress_bar(const char *desc, unsigned long long curr, + unsigned long long max); +void *dfu_malloc(size_t size); +uint32_t dfu_file_write_crc(int f, uint32_t crc, const void *buf, int size); +void show_suffix_and_prefix(struct dfu_file *file); + +#endif /* DFU_FILE_H */ diff --git a/tools/macosx/src/dfu-util/src/dfu_load.c b/tools/macosx/src/dfu-util/src/dfu_load.c new file mode 100644 index 0000000..64f7009 --- /dev/null +++ b/tools/macosx/src/dfu-util/src/dfu_load.c @@ -0,0 +1,196 @@ +/* + * DFU transfer routines + * + * This is supposed to be a general DFU implementation, as specified in the + * USB DFU 1.0 and 1.1 specification. + * + * The code was originally intended to interface with a USB device running the + * "sam7dfu" firmware (see http://www.openpcd.org/) on an AT91SAM7 processor. + * + * Copyright 2007-2008 Harald Welte + * Copyright 2013 Hans Petter Selasky + * Copyright 2014 Tormod Volden + * + * 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 +#include +#include +#include + +#include + +#include "portable.h" +#include "dfu.h" +#include "usb_dfu.h" +#include "dfu_file.h" +#include "dfu_load.h" +#include "quirks.h" + +int dfuload_do_upload(struct dfu_if *dif, int xfer_size, + int expected_size, int fd) +{ + int total_bytes = 0; + unsigned short transaction = 0; + unsigned char *buf; + int ret; + + buf = dfu_malloc(xfer_size); + + printf("Copying data from DFU device to PC\n"); + dfu_progress_bar("Upload", 0, 1); + + while (1) { + int rc; + rc = dfu_upload(dif->dev_handle, dif->interface, + xfer_size, transaction++, buf); + if (rc < 0) { + warnx("Error during upload"); + ret = rc; + goto out_free; + } + + dfu_file_write_crc(fd, 0, buf, rc); + total_bytes += rc; + + if (total_bytes < 0) + errx(EX_SOFTWARE, "Received too many bytes (wraparound)"); + + if (rc < xfer_size) { + /* last block, return */ + ret = total_bytes; + break; + } + dfu_progress_bar("Upload", total_bytes, expected_size); + } + ret = 0; + +out_free: + dfu_progress_bar("Upload", total_bytes, total_bytes); + if (total_bytes == 0) + printf("\nFailed.\n"); + free(buf); + if (verbose) + printf("Received a total of %i bytes\n", total_bytes); + if (expected_size != 0 && total_bytes != expected_size) + errx(EX_SOFTWARE, "Unexpected number of bytes uploaded from device"); + return ret; +} + +int dfuload_do_dnload(struct dfu_if *dif, int xfer_size, struct dfu_file *file) +{ + int bytes_sent; + int expected_size; + unsigned char *buf; + unsigned short transaction = 0; + struct dfu_status dst; + int ret; + + printf("Copying data from PC to DFU device\n"); + + buf = file->firmware; + expected_size = file->size.total - file->size.suffix; + bytes_sent = 0; + + dfu_progress_bar("Download", 0, 1); + while (bytes_sent < expected_size) { + int bytes_left; + int chunk_size; + + bytes_left = expected_size - bytes_sent; + if (bytes_left < xfer_size) + chunk_size = bytes_left; + else + chunk_size = xfer_size; + + ret = dfu_download(dif->dev_handle, dif->interface, + chunk_size, transaction++, chunk_size ? buf : NULL); + if (ret < 0) { + warnx("Error during download"); + goto out; + } + bytes_sent += chunk_size; + buf += chunk_size; + + do { + ret = dfu_get_status(dif, &dst); + if (ret < 0) { + errx(EX_IOERR, "Error during download get_status"); + goto out; + } + + if (dst.bState == DFU_STATE_dfuDNLOAD_IDLE || + dst.bState == DFU_STATE_dfuERROR) + break; + + /* Wait while device executes flashing */ + milli_sleep(dst.bwPollTimeout); + + } while (1); + 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; + } + dfu_progress_bar("Download", bytes_sent, bytes_sent + bytes_left); + } + + /* send one zero sized download request to signalize end */ + ret = dfu_download(dif->dev_handle, dif->interface, + 0, transaction, NULL); + if (ret < 0) { + errx(EX_IOERR, "Error sending completion packet"); + goto out; + } + + dfu_progress_bar("Download", bytes_sent, bytes_sent); + + if (verbose) + printf("Sent a total of %i bytes\n", bytes_sent); + +get_status: + /* Transition to MANIFEST_SYNC state */ + ret = dfu_get_status(dif, &dst); + if (ret < 0) { + warnx("unable to read DFU status after completion"); + goto out; + } + printf("state(%u) = %s, status(%u) = %s\n", dst.bState, + dfu_state_to_string(dst.bState), dst.bStatus, + dfu_status_to_string(dst.bStatus)); + + milli_sleep(dst.bwPollTimeout); + + /* 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 */ + milli_sleep(1000); + goto get_status; + break; + case DFU_STATE_dfuIDLE: + break; + } + printf("Done!\n"); + +out: + return bytes_sent; +} diff --git a/tools/macosx/src/dfu-util/src/dfu_load.h b/tools/macosx/src/dfu-util/src/dfu_load.h new file mode 100644 index 0000000..be23e9b --- /dev/null +++ b/tools/macosx/src/dfu-util/src/dfu_load.h @@ -0,0 +1,7 @@ +#ifndef DFU_LOAD_H +#define DFU_LOAD_H + +int dfuload_do_upload(struct dfu_if *dif, int xfer_size, int expected_size, int fd); +int dfuload_do_dnload(struct dfu_if *dif, int xfer_size, struct dfu_file *file); + +#endif /* DFU_LOAD_H */ diff --git a/tools/macosx/src/dfu-util/src/dfu_util.c b/tools/macosx/src/dfu-util/src/dfu_util.c new file mode 100644 index 0000000..b94c7cc --- /dev/null +++ b/tools/macosx/src/dfu-util/src/dfu_util.c @@ -0,0 +1,346 @@ +/* + * Functions for detecting DFU USB entities + * + * Written by Harald Welte + * Copyright 2007-2008 by OpenMoko, Inc. + * Copyright 2013 Hans Petter Selasky + * + * 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 +#include +#include +#include +#include + +#include "portable.h" +#include "dfu.h" +#include "usb_dfu.h" +#include "dfu_file.h" +#include "dfu_load.h" +#include "dfu_util.h" +#include "dfuse.h" +#include "quirks.h" + +#ifdef HAVE_USBPATH_H +#include +#endif + +/* + * Look for a descriptor in a concatenated descriptor list. Will + * return upon the first match of the given descriptor type. Returns length of + * found descriptor, limited to res_size + */ +static int find_descriptor(const uint8_t *desc_list, int list_len, + uint8_t desc_type, void *res_buf, int res_size) +{ + int p = 0; + + if (list_len < 2) + return (-1); + + while (p + 1 < list_len) { + int desclen; + + desclen = (int) desc_list[p]; + if (desclen == 0) { + warnx("Invalid descriptor list"); + return -1; + } + if (desc_list[p + 1] == desc_type) { + if (desclen > res_size) + desclen = res_size; + if (p + desclen > list_len) + desclen = list_len - p; + memcpy(res_buf, &desc_list[p], desclen); + return desclen; + } + p += (int) desc_list[p]; + } + return -1; +} + +static void probe_configuration(libusb_device *dev, struct libusb_device_descriptor *desc) +{ + struct usb_dfu_func_descriptor func_dfu; + libusb_device_handle *devh; + struct dfu_if *pdfu; + struct libusb_config_descriptor *cfg; + const struct libusb_interface_descriptor *intf; + const struct libusb_interface *uif; + char alt_name[MAX_DESC_STR_LEN + 1]; + char serial_name[MAX_DESC_STR_LEN + 1]; + int cfg_idx; + int intf_idx; + int alt_idx; + int ret; + int has_dfu; + + for (cfg_idx = 0; cfg_idx != desc->bNumConfigurations; cfg_idx++) { + memset(&func_dfu, 0, sizeof(func_dfu)); + has_dfu = 0; + + ret = libusb_get_config_descriptor(dev, cfg_idx, &cfg); + if (ret != 0) + return; + if (match_config_index > -1 && match_config_index != cfg->bConfigurationValue) { + libusb_free_config_descriptor(cfg); + continue; + } + + /* + * In some cases, noticably FreeBSD if uid != 0, + * the configuration descriptors are empty + */ + if (!cfg) + return; + + ret = find_descriptor(cfg->extra, cfg->extra_length, + USB_DT_DFU, &func_dfu, sizeof(func_dfu)); + if (ret > -1) + goto found_dfu; + + for (intf_idx = 0; intf_idx < cfg->bNumInterfaces; + intf_idx++) { + uif = &cfg->interface[intf_idx]; + if (!uif) + break; + + for (alt_idx = 0; alt_idx < cfg->interface[intf_idx].num_altsetting; + alt_idx++) { + intf = &uif->altsetting[alt_idx]; + + ret = find_descriptor(intf->extra, intf->extra_length, USB_DT_DFU, + &func_dfu, sizeof(func_dfu)); + if (ret > -1) + goto found_dfu; + + if (intf->bInterfaceClass != 0xfe || + intf->bInterfaceSubClass != 1) + continue; + + has_dfu = 1; + } + } + if (has_dfu) { + /* + * Finally try to retrieve it requesting the + * device directly This is not supported on + * all devices for non-standard types + */ + if (libusb_open(dev, &devh) == 0) { + ret = libusb_get_descriptor(devh, USB_DT_DFU, 0, + (void *)&func_dfu, sizeof(func_dfu)); + libusb_close(devh); + if (ret > -1) + goto found_dfu; + } + warnx("Device has DFU interface, " + "but has no DFU functional descriptor"); + + /* fake version 1.0 */ + func_dfu.bLength = 7; + func_dfu.bcdDFUVersion = libusb_cpu_to_le16(0x0100); + goto found_dfu; + } + libusb_free_config_descriptor(cfg); + continue; + +found_dfu: + if (func_dfu.bLength == 7) { + printf("Deducing device DFU version from functional descriptor " + "length\n"); + func_dfu.bcdDFUVersion = libusb_cpu_to_le16(0x0100); + } else if (func_dfu.bLength < 9) { + printf("Error obtaining DFU functional descriptor\n"); + printf("Please report this as a bug!\n"); + printf("Warning: Assuming DFU version 1.0\n"); + func_dfu.bcdDFUVersion = libusb_cpu_to_le16(0x0100); + printf("Warning: Transfer size can not be detected\n"); + func_dfu.wTransferSize = 0; + } + + for (intf_idx = 0; intf_idx < cfg->bNumInterfaces; + intf_idx++) { + if (match_iface_index > -1 && match_iface_index != intf_idx) + continue; + + uif = &cfg->interface[intf_idx]; + if (!uif) + break; + + for (alt_idx = 0; + alt_idx < uif->num_altsetting; alt_idx++) { + int dfu_mode; + + intf = &uif->altsetting[alt_idx]; + + if (intf->bInterfaceClass != 0xfe || + intf->bInterfaceSubClass != 1) + continue; + + dfu_mode = (intf->bInterfaceProtocol == 2); + /* e.g. DSO Nano has bInterfaceProtocol 0 instead of 2 */ + if (func_dfu.bcdDFUVersion == 0x011a && intf->bInterfaceProtocol == 0) + dfu_mode = 1; + + if (dfu_mode && + match_iface_alt_index > -1 && match_iface_alt_index != alt_idx) + continue; + + if (dfu_mode) { + if ((match_vendor_dfu >= 0 && match_vendor_dfu != desc->idVendor) || + (match_product_dfu >= 0 && match_product_dfu != desc->idProduct)) { + continue; + } + } else { + if ((match_vendor >= 0 && match_vendor != desc->idVendor) || + (match_product >= 0 && match_product != desc->idProduct)) { + continue; + } + } + + if (libusb_open(dev, &devh)) { + warnx("Cannot open DFU device %04x:%04x", desc->idVendor, desc->idProduct); + break; + } + if (intf->iInterface != 0) + ret = libusb_get_string_descriptor_ascii(devh, + intf->iInterface, (void *)alt_name, MAX_DESC_STR_LEN); + else + ret = -1; + if (ret < 1) + strcpy(alt_name, "UNKNOWN"); + if (desc->iSerialNumber != 0) + ret = libusb_get_string_descriptor_ascii(devh, + desc->iSerialNumber, (void *)serial_name, MAX_DESC_STR_LEN); + else + ret = -1; + if (ret < 1) + strcpy(serial_name, "UNKNOWN"); + libusb_close(devh); + + if (dfu_mode && + match_iface_alt_name != NULL && strcmp(alt_name, match_iface_alt_name)) + continue; + + if (dfu_mode) { + if (match_serial_dfu != NULL && strcmp(match_serial_dfu, serial_name)) + continue; + } else { + if (match_serial != NULL && strcmp(match_serial, serial_name)) + continue; + } + + pdfu = dfu_malloc(sizeof(*pdfu)); + + memset(pdfu, 0, sizeof(*pdfu)); + + pdfu->func_dfu = func_dfu; + pdfu->dev = libusb_ref_device(dev); + pdfu->quirks = get_quirks(desc->idVendor, + desc->idProduct, desc->bcdDevice); + pdfu->vendor = desc->idVendor; + pdfu->product = desc->idProduct; + pdfu->bcdDevice = desc->bcdDevice; + pdfu->configuration = cfg->bConfigurationValue; + pdfu->interface = intf->bInterfaceNumber; + pdfu->altsetting = intf->bAlternateSetting; + pdfu->devnum = libusb_get_device_address(dev); + pdfu->busnum = libusb_get_bus_number(dev); + pdfu->alt_name = strdup(alt_name); + if (pdfu->alt_name == NULL) + errx(EX_SOFTWARE, "Out of memory"); + pdfu->serial_name = strdup(serial_name); + if (pdfu->serial_name == NULL) + errx(EX_SOFTWARE, "Out of memory"); + if (dfu_mode) + pdfu->flags |= DFU_IFF_DFU; + if (pdfu->quirks & QUIRK_FORCE_DFU11) { + pdfu->func_dfu.bcdDFUVersion = + libusb_cpu_to_le16(0x0110); + } + pdfu->bMaxPacketSize0 = desc->bMaxPacketSize0; + + /* queue into list */ + pdfu->next = dfu_root; + dfu_root = pdfu; + } + } + libusb_free_config_descriptor(cfg); + } +} + +void probe_devices(libusb_context *ctx) +{ + libusb_device **list; + ssize_t num_devs; + ssize_t i; + + num_devs = libusb_get_device_list(ctx, &list); + for (i = 0; i < num_devs; ++i) { + struct libusb_device_descriptor desc; + struct libusb_device *dev = list[i]; + + if (match_bus > -1 && match_bus != libusb_get_bus_number(dev)) + continue; + if (match_device > -1 && match_device != libusb_get_device_address(dev)) + continue; + if (libusb_get_device_descriptor(dev, &desc)) + continue; + probe_configuration(dev, &desc); + } + libusb_free_device_list(list, 0); +} + +void disconnect_devices(void) +{ + struct dfu_if *pdfu; + struct dfu_if *prev = NULL; + + for (pdfu = dfu_root; pdfu != NULL; pdfu = pdfu->next) { + free(prev); + libusb_unref_device(pdfu->dev); + free(pdfu->alt_name); + free(pdfu->serial_name); + prev = pdfu; + } + free(prev); + dfu_root = NULL; +} + +void print_dfu_if(struct dfu_if *dfu_if) +{ + printf("Found %s: [%04x:%04x] ver=%04x, devnum=%u, cfg=%u, intf=%u, " + "alt=%u, name=\"%s\", serial=\"%s\"\n", + dfu_if->flags & DFU_IFF_DFU ? "DFU" : "Runtime", + dfu_if->vendor, dfu_if->product, + dfu_if->bcdDevice, dfu_if->devnum, + dfu_if->configuration, dfu_if->interface, + dfu_if->altsetting, dfu_if->alt_name, + dfu_if->serial_name); +} + +/* Walk the device tree and print out DFU devices */ +void list_dfu_interfaces(void) +{ + struct dfu_if *pdfu; + + for (pdfu = dfu_root; pdfu != NULL; pdfu = pdfu->next) + print_dfu_if(pdfu); +} diff --git a/tools/macosx/src/dfu-util/src/dfu_util.h b/tools/macosx/src/dfu-util/src/dfu_util.h new file mode 100644 index 0000000..fc0c19d --- /dev/null +++ b/tools/macosx/src/dfu-util/src/dfu_util.h @@ -0,0 +1,36 @@ +#ifndef DFU_UTIL_H +#define DFU_UTIL_H + +/* USB string descriptor should contain max 126 UTF-16 characters + * but 253 would even accomodate any UTF-8 encoding */ +#define MAX_DESC_STR_LEN 253 + +enum mode { + MODE_NONE, + MODE_VERSION, + MODE_LIST, + MODE_DETACH, + MODE_UPLOAD, + MODE_DOWNLOAD +}; + +extern struct dfu_if *dfu_root; +extern int match_bus; +extern int match_device; +extern int match_vendor; +extern int match_product; +extern int match_vendor_dfu; +extern int match_product_dfu; +extern int match_config_index; +extern int match_iface_index; +extern int match_iface_alt_index; +extern const char *match_iface_alt_name; +extern const char *match_serial; +extern const char *match_serial_dfu; + +void probe_devices(libusb_context *); +void disconnect_devices(void); +void print_dfu_if(struct dfu_if *); +void list_dfu_interfaces(void); + +#endif /* DFU_UTIL_H */ diff --git a/tools/macosx/src/dfu-util/src/dfuse.c b/tools/macosx/src/dfu-util/src/dfuse.c new file mode 100644 index 0000000..fce29fe --- /dev/null +++ b/tools/macosx/src/dfu-util/src/dfuse.c @@ -0,0 +1,652 @@ +/* + * DfuSe specific functions + * + * This implements the ST Microsystems DFU extensions (DfuSe) + * as per the DfuSe 1.1a specification (ST documents AN3156, AN2606) + * The DfuSe file format is described in ST document UM0391. + * + * Copyright 2010-2014 Tormod Volden + * + * 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 +#include +#include +#include + +#include "portable.h" +#include "dfu.h" +#include "usb_dfu.h" +#include "dfu_file.h" +#include "dfuse.h" +#include "dfuse_mem.h" + +#define DFU_TIMEOUT 5000 + +extern int verbose; +static unsigned int last_erased_page = 1; /* non-aligned value, won't match */ +static struct memsegment *mem_layout; +static unsigned int dfuse_address = 0; +static unsigned int dfuse_length = 0; +static int dfuse_force = 0; +static int dfuse_leave = 0; +static int dfuse_unprotect = 0; +static int dfuse_mass_erase = 0; + +unsigned int quad2uint(unsigned char *p) +{ + return (*p + (*(p + 1) << 8) + (*(p + 2) << 16) + (*(p + 3) << 24)); +} + +void dfuse_parse_options(const char *options) +{ + char *end; + const char *endword; + unsigned int number; + + /* address, possibly empty, must be first */ + if (*options != ':') { + endword = strchr(options, ':'); + if (!endword) + endword = options + strlen(options); /* GNU strchrnul */ + + number = strtoul(options, &end, 0); + if (end == endword) { + dfuse_address = number; + } else { + errx(EX_IOERR, "Invalid dfuse address: %s", options); + } + options = endword; + } + + while (*options) { + if (*options == ':') { + options++; + continue; + } + endword = strchr(options, ':'); + if (!endword) + endword = options + strlen(options); + + if (!strncmp(options, "force", endword - options)) { + dfuse_force++; + options += 5; + continue; + } + if (!strncmp(options, "leave", endword - options)) { + dfuse_leave = 1; + options += 5; + continue; + } + if (!strncmp(options, "unprotect", endword - options)) { + dfuse_unprotect = 1; + options += 9; + continue; + } + if (!strncmp(options, "mass-erase", endword - options)) { + dfuse_mass_erase = 1; + options += 10; + continue; + } + + /* any valid number is interpreted as upload length */ + number = strtoul(options, &end, 0); + if (end == endword) { + dfuse_length = number; + } else { + errx(EX_IOERR, "Invalid dfuse modifier: %s", options); + } + options = endword; + } +} + +/* DFU_UPLOAD request for DfuSe 1.1a */ +int dfuse_upload(struct dfu_if *dif, const unsigned short length, + unsigned char *data, unsigned short transaction) +{ + int status; + + status = libusb_control_transfer(dif->dev_handle, + /* bmRequestType */ LIBUSB_ENDPOINT_IN | + LIBUSB_REQUEST_TYPE_CLASS | + LIBUSB_RECIPIENT_INTERFACE, + /* bRequest */ DFU_UPLOAD, + /* wValue */ transaction, + /* wIndex */ dif->interface, + /* Data */ data, + /* wLength */ length, + DFU_TIMEOUT); + if (status < 0) { + errx(EX_IOERR, "%s: libusb_control_msg returned %d", + __FUNCTION__, status); + } + return status; +} + +/* DFU_DNLOAD request for DfuSe 1.1a */ +int dfuse_download(struct dfu_if *dif, const unsigned short length, + unsigned char *data, unsigned short transaction) +{ + int status; + + status = libusb_control_transfer(dif->dev_handle, + /* bmRequestType */ LIBUSB_ENDPOINT_OUT | + LIBUSB_REQUEST_TYPE_CLASS | + LIBUSB_RECIPIENT_INTERFACE, + /* bRequest */ DFU_DNLOAD, + /* wValue */ transaction, + /* wIndex */ dif->interface, + /* Data */ data, + /* wLength */ length, + DFU_TIMEOUT); + if (status < 0) { + errx(EX_IOERR, "%s: libusb_control_transfer returned %d", + __FUNCTION__, status); + } + return status; +} + +/* DfuSe only commands */ +/* Leaves the device in dfuDNLOAD-IDLE state */ +int dfuse_special_command(struct dfu_if *dif, unsigned int address, + enum dfuse_command command) +{ + const char* dfuse_command_name[] = { "SET_ADDRESS" , "ERASE_PAGE", + "MASS_ERASE", "READ_UNPROTECT"}; + unsigned char buf[5]; + int length; + int ret; + struct dfu_status dst; + int firstpoll = 1; + + if (command == ERASE_PAGE) { + struct memsegment *segment; + int page_size; + + segment = find_segment(mem_layout, address); + if (!segment || !(segment->memtype & DFUSE_ERASABLE)) { + errx(EX_IOERR, "Page at 0x%08x can not be erased", + address); + } + page_size = segment->pagesize; + if (verbose > 1) + printf("Erasing page size %i at address 0x%08x, page " + "starting at 0x%08x\n", page_size, address, + address & ~(page_size - 1)); + buf[0] = 0x41; /* Erase command */ + length = 5; + last_erased_page = address & ~(page_size - 1); + } else if (command == SET_ADDRESS) { + if (verbose > 2) + printf(" Setting address pointer to 0x%08x\n", + address); + buf[0] = 0x21; /* Set Address Pointer command */ + length = 5; + } else if (command == MASS_ERASE) { + buf[0] = 0x41; /* Mass erase command when length = 1 */ + length = 1; + } else if (command == READ_UNPROTECT) { + buf[0] = 0x92; + length = 1; + } else { + errx(EX_IOERR, "Non-supported special command %d", command); + } + buf[1] = address & 0xff; + buf[2] = (address >> 8) & 0xff; + buf[3] = (address >> 16) & 0xff; + buf[4] = (address >> 24) & 0xff; + + ret = dfuse_download(dif, length, buf, 0); + if (ret < 0) { + errx(EX_IOERR, "Error during special command \"%s\" download", + dfuse_command_name[command]); + } + do { + ret = dfu_get_status(dif, &dst); + if (ret < 0) { + errx(EX_IOERR, "Error during special command \"%s\" get_status", + dfuse_command_name[command]); + } + if (firstpoll) { + firstpoll = 0; + if (dst.bState != DFU_STATE_dfuDNBUSY) { + printf("state(%u) = %s, status(%u) = %s\n", dst.bState, + dfu_state_to_string(dst.bState), dst.bStatus, + dfu_status_to_string(dst.bStatus)); + errx(EX_IOERR, "Wrong state after command \"%s\" download", + dfuse_command_name[command]); + } + } + /* wait while command is executed */ + if (verbose) + printf(" Poll timeout %i ms\n", dst.bwPollTimeout); + milli_sleep(dst.bwPollTimeout); + if (command == READ_UNPROTECT) + return ret; + } while (dst.bState == DFU_STATE_dfuDNBUSY); + + if (dst.bStatus != DFU_STATUS_OK) { + errx(EX_IOERR, "%s not correctly executed", + dfuse_command_name[command]); + } + return ret; +} + +int dfuse_dnload_chunk(struct dfu_if *dif, unsigned char *data, int size, + int transaction) +{ + int bytes_sent; + struct dfu_status dst; + int ret; + + ret = dfuse_download(dif, size, size ? data : NULL, transaction); + if (ret < 0) { + errx(EX_IOERR, "Error during download"); + return ret; + } + bytes_sent = ret; + + do { + ret = dfu_get_status(dif, &dst); + if (ret < 0) { + errx(EX_IOERR, "Error during download get_status"); + return ret; + } + milli_sleep(dst.bwPollTimeout); + } while (dst.bState != DFU_STATE_dfuDNLOAD_IDLE && + dst.bState != DFU_STATE_dfuERROR && + dst.bState != DFU_STATE_dfuMANIFEST); + + if (dst.bState == DFU_STATE_dfuMANIFEST) + printf("Transitioning to dfuMANIFEST state\n"); + + 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)); + return -1; + } + return bytes_sent; +} + +int dfuse_do_upload(struct dfu_if *dif, int xfer_size, int fd, + const char *dfuse_options) +{ + int total_bytes = 0; + int upload_limit = 0; + unsigned char *buf; + int transaction; + int ret; + + buf = dfu_malloc(xfer_size); + + if (dfuse_options) + dfuse_parse_options(dfuse_options); + if (dfuse_length) + upload_limit = dfuse_length; + if (dfuse_address) { + struct memsegment *segment; + + mem_layout = parse_memory_layout((char *)dif->alt_name); + if (!mem_layout) + errx(EX_IOERR, "Failed to parse memory layout"); + + segment = find_segment(mem_layout, dfuse_address); + if (!dfuse_force && + (!segment || !(segment->memtype & DFUSE_READABLE))) + errx(EX_IOERR, "Page at 0x%08x is not readable", + dfuse_address); + + if (!upload_limit) { + upload_limit = segment->end - dfuse_address + 1; + printf("Limiting upload to end of memory segment, " + "%i bytes\n", upload_limit); + } + dfuse_special_command(dif, dfuse_address, SET_ADDRESS); + dfu_abort_to_idle(dif); + } else { + /* Boot loader decides the start address, unknown to us */ + /* Use a short length to lower risk of running out of bounds */ + if (!upload_limit) + upload_limit = 0x4000; + printf("Limiting default upload to %i bytes\n", upload_limit); + } + + dfu_progress_bar("Upload", 0, 1); + + transaction = 2; + while (1) { + int rc; + + /* last chunk can be smaller than original xfer_size */ + if (upload_limit - total_bytes < xfer_size) + xfer_size = upload_limit - total_bytes; + rc = dfuse_upload(dif, xfer_size, buf, transaction++); + if (rc < 0) { + ret = rc; + goto out_free; + } + + dfu_file_write_crc(fd, 0, buf, rc); + total_bytes += rc; + + if (total_bytes < 0) + errx(EX_SOFTWARE, "Received too many bytes"); + + if (rc < xfer_size || total_bytes >= upload_limit) { + /* last block, return successfully */ + ret = total_bytes; + break; + } + dfu_progress_bar("Upload", total_bytes, upload_limit); + } + + dfu_progress_bar("Upload", total_bytes, total_bytes); + + dfu_abort_to_idle(dif); + if (dfuse_leave) { + dfuse_special_command(dif, dfuse_address, SET_ADDRESS); + dfuse_dnload_chunk(dif, NULL, 0, 2); /* Zero-size */ + } + + out_free: + free(buf); + + return ret; +} + +/* Writes an element of any size to the device, taking care of page erases */ +/* returns 0 on success, otherwise -EINVAL */ +int dfuse_dnload_element(struct dfu_if *dif, unsigned int dwElementAddress, + unsigned int dwElementSize, unsigned char *data, + int xfer_size) +{ + int p; + int ret; + struct memsegment *segment; + + /* Check at least that we can write to the last address */ + segment = + find_segment(mem_layout, dwElementAddress + dwElementSize - 1); + if (!segment || !(segment->memtype & DFUSE_WRITEABLE)) { + errx(EX_IOERR, "Last page at 0x%08x is not writeable", + dwElementAddress + dwElementSize - 1); + } + + dfu_progress_bar("Download", 0, 1); + + for (p = 0; p < (int)dwElementSize; p += xfer_size) { + int page_size; + unsigned int erase_address; + unsigned int address = dwElementAddress + p; + int chunk_size = xfer_size; + + segment = find_segment(mem_layout, address); + if (!segment || !(segment->memtype & DFUSE_WRITEABLE)) { + errx(EX_IOERR, "Page at 0x%08x is not writeable", + address); + } + page_size = segment->pagesize; + + /* check if this is the last chunk */ + if (p + chunk_size > (int)dwElementSize) + chunk_size = dwElementSize - p; + + /* Erase only for flash memory downloads */ + if ((segment->memtype & DFUSE_ERASABLE) && !dfuse_mass_erase) { + /* erase all involved pages */ + for (erase_address = address; + erase_address < address + chunk_size; + erase_address += page_size) + if ((erase_address & ~(page_size - 1)) != + last_erased_page) + dfuse_special_command(dif, + erase_address, + ERASE_PAGE); + + if (((address + chunk_size - 1) & ~(page_size - 1)) != + last_erased_page) { + if (verbose > 2) + printf(" Chunk extends into next page," + " erase it as well\n"); + dfuse_special_command(dif, + address + chunk_size - 1, + ERASE_PAGE); + } + } + + if (verbose) { + printf(" Download from image offset " + "%08x to memory %08x-%08x, size %i\n", + p, address, address + chunk_size - 1, + chunk_size); + } else { + dfu_progress_bar("Download", p, dwElementSize); + } + + dfuse_special_command(dif, address, SET_ADDRESS); + + /* transaction = 2 for no address offset */ + ret = dfuse_dnload_chunk(dif, data + p, chunk_size, 2); + if (ret != chunk_size) { + errx(EX_IOERR, "Failed to write whole chunk: " + "%i of %i bytes", ret, chunk_size); + return -EINVAL; + } + } + if (!verbose) + dfu_progress_bar("Download", dwElementSize, dwElementSize); + return 0; +} + +static void +dfuse_memcpy(unsigned char *dst, unsigned char **src, int *rem, int size) +{ + if (size > *rem) { + errx(EX_IOERR, "Corrupt DfuSe file: " + "Cannot read %d bytes from %d bytes", size, *rem); + } + if (dst != NULL) + memcpy(dst, *src, size); + (*src) += size; + (*rem) -= size; +} + +/* Download raw binary file to DfuSe device */ +int dfuse_do_bin_dnload(struct dfu_if *dif, int xfer_size, + struct dfu_file *file, unsigned int start_address) +{ + unsigned int dwElementAddress; + unsigned int dwElementSize; + unsigned char *data; + int ret; + + dwElementAddress = start_address; + dwElementSize = file->size.total - + file->size.suffix - file->size.prefix; + + printf("Downloading to address = 0x%08x, size = %i\n", + dwElementAddress, dwElementSize); + + data = file->firmware + file->size.prefix; + + ret = dfuse_dnload_element(dif, dwElementAddress, dwElementSize, data, + xfer_size); + if (ret != 0) + goto out_free; + + printf("File downloaded successfully\n"); + ret = dwElementSize; + + out_free: + return ret; +} + +/* Parse a DfuSe file and download contents to device */ +int dfuse_do_dfuse_dnload(struct dfu_if *dif, int xfer_size, + struct dfu_file *file) +{ + uint8_t dfuprefix[11]; + uint8_t targetprefix[274]; + uint8_t elementheader[8]; + int image; + int element; + int bTargets; + int bAlternateSetting; + int dwNbElements; + unsigned int dwElementAddress; + unsigned int dwElementSize; + uint8_t *data; + int ret; + int rem; + int bFirstAddressSaved = 0; + + rem = file->size.total - file->size.prefix - file->size.suffix; + data = file->firmware + file->size.prefix; + + /* Must be larger than a minimal DfuSe header and suffix */ + if (rem < (int)(sizeof(dfuprefix) + + sizeof(targetprefix) + sizeof(elementheader))) { + errx(EX_SOFTWARE, "File too small for a DfuSe file"); + } + + dfuse_memcpy(dfuprefix, &data, &rem, sizeof(dfuprefix)); + + if (strncmp((char *)dfuprefix, "DfuSe", 5)) { + errx(EX_IOERR, "No valid DfuSe signature"); + return -EINVAL; + } + if (dfuprefix[5] != 0x01) { + errx(EX_IOERR, "DFU format revision %i not supported", + dfuprefix[5]); + return -EINVAL; + } + bTargets = dfuprefix[10]; + printf("file contains %i DFU images\n", bTargets); + + for (image = 1; image <= bTargets; image++) { + printf("parsing DFU image %i\n", image); + dfuse_memcpy(targetprefix, &data, &rem, sizeof(targetprefix)); + if (strncmp((char *)targetprefix, "Target", 6)) { + errx(EX_IOERR, "No valid target signature"); + return -EINVAL; + } + bAlternateSetting = targetprefix[6]; + dwNbElements = quad2uint((unsigned char *)targetprefix + 270); + printf("image for alternate setting %i, ", bAlternateSetting); + printf("(%i elements, ", dwNbElements); + printf("total size = %i)\n", + quad2uint((unsigned char *)targetprefix + 266)); + if (bAlternateSetting != dif->altsetting) + printf("Warning: Image does not match current alternate" + " setting.\n" + "Please rerun with the correct -a option setting" + " to download this image!\n"); + for (element = 1; element <= dwNbElements; element++) { + printf("parsing element %i, ", element); + dfuse_memcpy(elementheader, &data, &rem, sizeof(elementheader)); + dwElementAddress = + quad2uint((unsigned char *)elementheader); + dwElementSize = + quad2uint((unsigned char *)elementheader + 4); + printf("address = 0x%08x, ", dwElementAddress); + printf("size = %i\n", dwElementSize); + + if (!bFirstAddressSaved) { + bFirstAddressSaved = 1; + dfuse_address = dwElementAddress; + } + /* sanity check */ + if ((int)dwElementSize > rem) + errx(EX_SOFTWARE, "File too small for element size"); + + if (bAlternateSetting == dif->altsetting) { + ret = dfuse_dnload_element(dif, dwElementAddress, + dwElementSize, data, xfer_size); + } else { + ret = 0; + } + + /* advance read pointer */ + dfuse_memcpy(NULL, &data, &rem, dwElementSize); + + if (ret != 0) + return ret; + } + } + + if (rem != 0) + warnx("%d bytes leftover", rem); + + printf("done parsing DfuSe file\n"); + + return 0; +} + +int dfuse_do_dnload(struct dfu_if *dif, int xfer_size, struct dfu_file *file, + const char *dfuse_options) +{ + int ret; + + if (dfuse_options) + dfuse_parse_options(dfuse_options); + mem_layout = parse_memory_layout((char *)dif->alt_name); + if (!mem_layout) { + errx(EX_IOERR, "Failed to parse memory layout"); + } + if (dfuse_unprotect) { + if (!dfuse_force) { + errx(EX_IOERR, "The read unprotect command " + "will erase the flash memory" + "and can only be used with force\n"); + } + dfuse_special_command(dif, 0, READ_UNPROTECT); + printf("Device disconnects, erases flash and resets now\n"); + exit(0); + } + if (dfuse_mass_erase) { + if (!dfuse_force) { + errx(EX_IOERR, "The mass erase command " + "can only be used with force"); + } + printf("Performing mass erase, this can take a moment\n"); + dfuse_special_command(dif, 0, MASS_ERASE); + } + if (dfuse_address) { + if (file->bcdDFU == 0x11a) { + errx(EX_IOERR, "This is a DfuSe file, not " + "meant for raw download"); + } + ret = dfuse_do_bin_dnload(dif, xfer_size, file, dfuse_address); + } else { + if (file->bcdDFU != 0x11a) { + warnx("Only DfuSe file version 1.1a is supported"); + errx(EX_IOERR, "(for raw binary download, use the " + "--dfuse-address option)"); + } + ret = dfuse_do_dfuse_dnload(dif, xfer_size, file); + } + free_segment_list(mem_layout); + + dfu_abort_to_idle(dif); + + if (dfuse_leave) { + dfuse_special_command(dif, dfuse_address, SET_ADDRESS); + dfuse_dnload_chunk(dif, NULL, 0, 2); /* Zero-size */ + } + return ret; +} diff --git a/tools/macosx/src/dfu-util/src/dfuse.h b/tools/macosx/src/dfu-util/src/dfuse.h new file mode 100644 index 0000000..ed1108c --- /dev/null +++ b/tools/macosx/src/dfu-util/src/dfuse.h @@ -0,0 +1,35 @@ +/* This implements the ST Microsystems DFU extensions (DfuSe) + * as per the DfuSe 1.1a specification (Document UM0391) + * + * (C) 2010-2012 Tormod Volden + * + * 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 DFUSE_H +#define DFUSE_H + +#include "dfu.h" + +enum dfuse_command { SET_ADDRESS, ERASE_PAGE, MASS_ERASE, READ_UNPROTECT }; + +int dfuse_special_command(struct dfu_if *dif, unsigned int address, + enum dfuse_command command); +int dfuse_do_upload(struct dfu_if *dif, int xfer_size, int fd, + const char *dfuse_options); +int dfuse_do_dnload(struct dfu_if *dif, int xfer_size, struct dfu_file *file, + const char *dfuse_options); + +#endif /* DFUSE_H */ diff --git a/tools/macosx/src/dfu-util/src/dfuse_mem.c b/tools/macosx/src/dfu-util/src/dfuse_mem.c new file mode 100644 index 0000000..a91aacf --- /dev/null +++ b/tools/macosx/src/dfu-util/src/dfuse_mem.c @@ -0,0 +1,198 @@ +/* + * Helper functions for reading the memory map of a device + * following the ST DfuSe 1.1a specification. + * + * Copyright 2011-2014 Tormod Volden + * + * 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 +#include +#include +#include + +#include "portable.h" +#include "dfu_file.h" +#include "dfuse_mem.h" + +int add_segment(struct memsegment **segment_list, struct memsegment segment) +{ + struct memsegment *new_element; + + new_element = dfu_malloc(sizeof(struct memsegment)); + *new_element = segment; + new_element->next = NULL; + + if (*segment_list == NULL) + /* list can be empty on first call */ + *segment_list = new_element; + else { + struct memsegment *next_element; + + /* find last element in list */ + next_element = *segment_list; + while (next_element->next != NULL) + next_element = next_element->next; + next_element->next = new_element; + } + return 0; +} + +struct memsegment *find_segment(struct memsegment *segment_list, + unsigned int address) +{ + while (segment_list != NULL) { + if (segment_list->start <= address && + segment_list->end >= address) + return segment_list; + segment_list = segment_list->next; + } + return NULL; +} + +void free_segment_list(struct memsegment *segment_list) +{ + struct memsegment *next_element; + + while (segment_list->next != NULL) { + next_element = segment_list->next; + free(segment_list); + segment_list = next_element; + } + free(segment_list); +} + +/* Parse memory map from interface descriptor string + * encoded as per ST document UM0424 section 4.3.2. + */ +struct memsegment *parse_memory_layout(char *intf_desc) +{ + + char multiplier, memtype; + unsigned int address; + int sectors, size; + char *name, *typestring; + int ret; + int count = 0; + char separator; + int scanned; + struct memsegment *segment_list = NULL; + struct memsegment segment; + + name = dfu_malloc(strlen(intf_desc)); + + ret = sscanf(intf_desc, "@%[^/]%n", name, &scanned); + if (ret < 1) { + free(name); + warnx("Could not read name, sscanf returned %d", ret); + return NULL; + } + printf("DfuSe interface name: \"%s\"\n", name); + + intf_desc += scanned; + typestring = dfu_malloc(strlen(intf_desc)); + + while (ret = sscanf(intf_desc, "/0x%x/%n", &address, &scanned), + ret > 0) { + + intf_desc += scanned; + while (ret = sscanf(intf_desc, "%d*%d%c%[^,/]%n", + §ors, &size, &multiplier, typestring, + &scanned), ret > 2) { + intf_desc += scanned; + + count++; + memtype = 0; + if (ret == 4) { + if (strlen(typestring) == 1 + && typestring[0] != '/') + memtype = typestring[0]; + else { + warnx("Parsing type identifier '%s' " + "failed for segment %i", + typestring, count); + continue; + } + } + + /* Quirk for STM32F4 devices */ + if (strcmp(name, "Device Feature") == 0) + memtype = 'e'; + + switch (multiplier) { + case 'B': + break; + case 'K': + size *= 1024; + break; + case 'M': + size *= 1024 * 1024; + break; + case 'a': + case 'b': + case 'c': + case 'd': + case 'e': + case 'f': + case 'g': + if (!memtype) { + warnx("Non-valid multiplier '%c', " + "interpreted as type " + "identifier instead", + multiplier); + memtype = multiplier; + break; + } + /* fallthrough if memtype was already set */ + default: + warnx("Non-valid multiplier '%c', " + "assuming bytes", multiplier); + } + + if (!memtype) { + warnx("No valid type for segment %d\n", count); + continue; + } + + segment.start = address; + segment.end = address + sectors * size - 1; + segment.pagesize = size; + segment.memtype = memtype & 7; + add_segment(&segment_list, segment); + + if (verbose) + printf("Memory segment at 0x%08x %3d x %4d = " + "%5d (%s%s%s)\n", + address, sectors, size, sectors * size, + memtype & DFUSE_READABLE ? "r" : "", + memtype & DFUSE_ERASABLE ? "e" : "", + memtype & DFUSE_WRITEABLE ? "w" : ""); + + address += sectors * size; + + separator = *intf_desc; + if (separator == ',') + intf_desc += 1; + else + break; + } /* while per segment */ + + } /* while per address */ + free(name); + free(typestring); + + return segment_list; +} diff --git a/tools/macosx/src/dfu-util/src/dfuse_mem.h b/tools/macosx/src/dfu-util/src/dfuse_mem.h new file mode 100644 index 0000000..0181f0c --- /dev/null +++ b/tools/macosx/src/dfu-util/src/dfuse_mem.h @@ -0,0 +1,44 @@ +/* Helper functions for reading the memory map in a device + * following the ST DfuSe 1.1a specification. + * + * (C) 2011 Tormod Volden + * + * 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 DFUSE_MEM_H +#define DFUSE_MEM_H + +#define DFUSE_READABLE 1 +#define DFUSE_ERASABLE 2 +#define DFUSE_WRITEABLE 4 + +struct memsegment { + unsigned int start; + unsigned int end; + int pagesize; + int memtype; + struct memsegment *next; +}; + +int add_segment(struct memsegment **list, struct memsegment new_element); + +struct memsegment *find_segment(struct memsegment *list, unsigned int address); + +void free_segment_list(struct memsegment *list); + +struct memsegment *parse_memory_layout(char *intf_desc_str); + +#endif /* DFUSE_MEM_H */ diff --git a/tools/macosx/src/dfu-util/src/main.c b/tools/macosx/src/dfu-util/src/main.c new file mode 100644 index 0000000..acaed2f --- /dev/null +++ b/tools/macosx/src/dfu-util/src/main.c @@ -0,0 +1,699 @@ +/* + * dfu-util + * + * Copyright 2007-2008 by OpenMoko, Inc. + * Copyright 2013-2014 Hans Petter Selasky + * + * Written by Harald Welte + * + * 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 +#include +#include +#include +#include +#include +#include + +#include "portable.h" +#include "dfu.h" +#include "usb_dfu.h" +#include "dfu_file.h" +#include "dfu_load.h" +#include "dfu_util.h" +#include "dfuse.h" +#include "quirks.h" + +#ifdef HAVE_USBPATH_H +#include +#endif + +int verbose = 0; + +struct dfu_if *dfu_root = NULL; + +int match_bus = -1; +int match_device = -1; +int match_vendor = -1; +int match_product = -1; +int match_vendor_dfu = -1; +int match_product_dfu = -1; +int match_config_index = -1; +int match_iface_index = -1; +int match_iface_alt_index = -1; +const char *match_iface_alt_name = NULL; +const char *match_serial = NULL; +const char *match_serial_dfu = NULL; + +static int parse_match_value(const char *str, int default_value) +{ + char *remainder; + int value; + + if (str == NULL) { + value = default_value; + } else if (*str == '*') { + value = -1; /* Match anything */ + } else if (*str == '-') { + value = 0x10000; /* Impossible vendor/product ID */ + } else { + value = strtoul(str, &remainder, 16); + if (remainder == str) { + value = default_value; + } + } + return value; +} + +static void parse_vendprod(const char *str) +{ + const char *comma; + const char *colon; + + /* Default to match any DFU device in runtime or DFU mode */ + match_vendor = -1; + match_product = -1; + match_vendor_dfu = -1; + match_product_dfu = -1; + + comma = strchr(str, ','); + if (comma == str) { + /* DFU mode vendor/product being specified without any runtime + * vendor/product specification, so don't match any runtime device */ + match_vendor = match_product = 0x10000; + } else { + colon = strchr(str, ':'); + if (colon != NULL) { + ++colon; + if ((comma != NULL) && (colon > comma)) { + colon = NULL; + } + } + match_vendor = parse_match_value(str, match_vendor); + match_product = parse_match_value(colon, match_product); + if (comma != NULL) { + /* Both runtime and DFU mode vendor/product specifications are + * available, so default DFU mode match components to the given + * runtime match components */ + match_vendor_dfu = match_vendor; + match_product_dfu = match_product; + } + } + if (comma != NULL) { + ++comma; + colon = strchr(comma, ':'); + if (colon != NULL) { + ++colon; + } + match_vendor_dfu = parse_match_value(comma, match_vendor_dfu); + match_product_dfu = parse_match_value(colon, match_product_dfu); + } +} + +static void parse_serial(char *str) +{ + char *comma; + + match_serial = str; + comma = strchr(str, ','); + if (comma == NULL) { + match_serial_dfu = match_serial; + } else { + *comma++ = 0; + match_serial_dfu = comma; + } + if (*match_serial == 0) match_serial = NULL; + if (*match_serial_dfu == 0) match_serial_dfu = NULL; +} + +#ifdef HAVE_USBPATH_H + +static int resolve_device_path(char *path) +{ + int res; + + res = usb_path2devnum(path); + if (res < 0) + return -EINVAL; + if (!res) + return 0; + + match_bus = atoi(path); + match_device = res; + + return 0; +} + +#else /* HAVE_USBPATH_H */ + +static int resolve_device_path(char *path) +{ + (void)path; /* Eliminate unused variable warning */ + errx(EX_SOFTWARE, "USB device paths are not supported by this dfu-util.\n"); +} + +#endif /* !HAVE_USBPATH_H */ + +static void help(void) +{ + fprintf(stderr, "Usage: dfu-util [options] ...\n" + " -h --help\t\t\tPrint this help message\n" + " -V --version\t\t\tPrint the version number\n" + " -v --verbose\t\t\tPrint verbose debug statements\n" + " -l --list\t\t\tList currently attached DFU capable devices\n"); + fprintf(stderr, " -e --detach\t\t\tDetach currently attached DFU capable devices\n" + " -E --detach-delay seconds\tTime to wait before reopening a device after detach\n" + " -d --device :[,:]\n" + "\t\t\t\tSpecify Vendor/Product ID(s) of DFU device\n" + " -p --path \tSpecify path to DFU device\n" + " -c --cfg \t\tSpecify the Configuration of DFU device\n" + " -i --intf \t\tSpecify the DFU Interface number\n" + " -S --serial [,]\n" + "\t\t\t\tSpecify Serial String of DFU device\n" + " -a --alt \t\tSpecify the Altsetting of the DFU Interface\n" + "\t\t\t\tby name or by number\n"); + fprintf(stderr, " -t --transfer-size \tSpecify the number of bytes per USB Transfer\n" + " -U --upload \t\tRead firmware from device into \n" + " -Z --upload-size \tSpecify the expected upload size in bytes\n" + " -D --download \t\tWrite firmware from into device\n" + " -R --reset\t\t\tIssue USB Reset signalling once we're finished\n" + " -s --dfuse-address

\tST DfuSe mode, specify target address for\n" + "\t\t\t\traw file download or upload. Not applicable for\n" + "\t\t\t\tDfuSe file (.dfu) downloads\n" + ); + exit(EX_USAGE); +} + +static void print_version(void) +{ + printf(PACKAGE_STRING "\n\n"); + printf("Copyright 2005-2009 Weston Schmidt, Harald Welte and OpenMoko Inc.\n" + "Copyright 2010-2014 Tormod Volden and Stefan Schmidt\n" + "This program is Free Software and has ABSOLUTELY NO WARRANTY\n" + "Please report bugs to " PACKAGE_BUGREPORT "\n\n"); +} + +static struct option opts[] = { + { "help", 0, 0, 'h' }, + { "version", 0, 0, 'V' }, + { "verbose", 0, 0, 'v' }, + { "list", 0, 0, 'l' }, + { "detach", 0, 0, 'e' }, + { "detach-delay", 1, 0, 'E' }, + { "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' }, + { "serial", 1, 0, 'S' }, + { "transfer-size", 1, 0, 't' }, + { "upload", 1, 0, 'U' }, + { "upload-size", 1, 0, 'Z' }, + { "download", 1, 0, 'D' }, + { "reset", 0, 0, 'R' }, + { "dfuse-address", 1, 0, 's' }, + { 0, 0, 0, 0 } +}; + +int main(int argc, char **argv) +{ + int expected_size = 0; + unsigned int transfer_size = 0; + enum mode mode = MODE_NONE; + struct dfu_status status; + libusb_context *ctx; + struct dfu_file file; + char *end; + int final_reset = 0; + int ret; + int dfuse_device = 0; + int fd; + const char *dfuse_options = NULL; + int detach_delay = 5; + uint16_t runtime_vendor; + uint16_t runtime_product; + + memset(&file, 0, sizeof(file)); + + /* make sure all prints are flushed */ + setvbuf(stdout, NULL, _IONBF, 0); + + while (1) { + int c, option_index = 0; + c = getopt_long(argc, argv, "hVvleE:d:p:c:i:a:S:t:U:D:Rs:Z:", opts, + &option_index); + if (c == -1) + break; + + switch (c) { + case 'h': + help(); + break; + case 'V': + mode = MODE_VERSION; + break; + case 'v': + verbose++; + break; + case 'l': + mode = MODE_LIST; + break; + case 'e': + mode = MODE_DETACH; + match_iface_alt_index = 0; + match_iface_index = 0; + break; + case 'E': + detach_delay = atoi(optarg); + break; + case 'd': + parse_vendprod(optarg); + break; + case 'p': + /* Parse device path */ + ret = resolve_device_path(optarg); + if (ret < 0) + errx(EX_SOFTWARE, "Unable to parse '%s'", optarg); + if (!ret) + errx(EX_SOFTWARE, "Cannot find '%s'", optarg); + break; + case 'c': + /* Configuration */ + match_config_index = atoi(optarg); + break; + case 'i': + /* Interface */ + match_iface_index = atoi(optarg); + break; + case 'a': + /* Interface Alternate Setting */ + match_iface_alt_index = strtoul(optarg, &end, 0); + if (*end) { + match_iface_alt_name = optarg; + match_iface_alt_index = -1; + } + break; + case 'S': + parse_serial(optarg); + break; + case 't': + transfer_size = atoi(optarg); + break; + case 'U': + mode = MODE_UPLOAD; + file.name = optarg; + break; + case 'Z': + expected_size = atoi(optarg); + break; + case 'D': + mode = MODE_DOWNLOAD; + file.name = optarg; + break; + case 'R': + final_reset = 1; + break; + case 's': + dfuse_options = optarg; + break; + default: + help(); + break; + } + } + + print_version(); + if (mode == MODE_VERSION) { + exit(0); + } + + if (mode == MODE_NONE) { + fprintf(stderr, "You need to specify one of -D or -U\n"); + help(); + } + + if (match_config_index == 0) { + /* Handle "-c 0" (unconfigured device) as don't care */ + match_config_index = -1; + } + + if (mode == MODE_DOWNLOAD) { + dfu_load_file(&file, MAYBE_SUFFIX, MAYBE_PREFIX); + /* If the user didn't specify product and/or vendor IDs to match, + * use any IDs from the file suffix for device matching */ + if (match_vendor < 0 && file.idVendor != 0xffff) { + match_vendor = file.idVendor; + printf("Match vendor ID from file: %04x\n", match_vendor); + } + if (match_product < 0 && file.idProduct != 0xffff) { + match_product = file.idProduct; + printf("Match product ID from file: %04x\n", match_product); + } + } + + ret = libusb_init(&ctx); + if (ret) + errx(EX_IOERR, "unable to initialize libusb: %i", ret); + + if (verbose > 2) { + libusb_set_debug(ctx, 255); + } + + probe_devices(ctx); + + if (mode == MODE_LIST) { + list_dfu_interfaces(); + exit(0); + } + + if (dfu_root == NULL) { + errx(EX_IOERR, "No DFU capable USB device available"); + } else if (dfu_root->next != NULL) { + /* 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 */ + errx(EX_IOERR, "More than one DFU capable USB device found! " + "Try `--list' and specify the serial number " + "or disconnect all but one device\n"); + } + + /* We have exactly one device. Its libusb_device is now in dfu_root->dev */ + + printf("Opening DFU capable USB device...\n"); + ret = libusb_open(dfu_root->dev, &dfu_root->dev_handle); + if (ret || !dfu_root->dev_handle) + errx(EX_IOERR, "Cannot open device"); + + printf("ID %04x:%04x\n", dfu_root->vendor, dfu_root->product); + + printf("Run-time device DFU version %04x\n", + libusb_le16_to_cpu(dfu_root->func_dfu.bcdDFUVersion)); + + /* Transition from run-Time mode to DFU mode */ + if (!(dfu_root->flags & DFU_IFF_DFU)) { + int err; + /* 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 */ + + runtime_vendor = dfu_root->vendor; + runtime_product = dfu_root->product; + + printf("Claiming USB DFU Runtime Interface...\n"); + if (libusb_claim_interface(dfu_root->dev_handle, dfu_root->interface) < 0) { + errx(EX_IOERR, "Cannot claim interface %d", + dfu_root->interface); + } + + if (libusb_set_interface_alt_setting(dfu_root->dev_handle, dfu_root->interface, 0) < 0) { + errx(EX_IOERR, "Cannot set alt interface zero"); + } + + printf("Determining device status: "); + + err = dfu_get_status(dfu_root, &status); + if (err == LIBUSB_ERROR_PIPE) { + printf("Device does not implement get_status, assuming appIDLE\n"); + status.bStatus = DFU_STATUS_OK; + status.bwPollTimeout = 0; + status.bState = DFU_STATE_appIDLE; + status.iString = 0; + } else if (err < 0) { + errx(EX_IOERR, "error get_status"); + } else { + printf("state = %s, status = %d\n", + dfu_state_to_string(status.bState), status.bStatus); + } + milli_sleep(status.bwPollTimeout); + + 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(dfu_root->dev_handle, + dfu_root->interface, 1000) < 0) { + warnx("error detaching"); + } + if (dfu_root->func_dfu.bmAttributes & USB_DFU_WILL_DETACH) { + printf("Device will detach and reattach...\n"); + } else { + printf("Resetting USB...\n"); + ret = libusb_reset_device(dfu_root->dev_handle); + if (ret < 0 && ret != LIBUSB_ERROR_NOT_FOUND) + errx(EX_IOERR, "error resetting " + "after detach"); + } + break; + case DFU_STATE_dfuERROR: + printf("dfuERROR, clearing status\n"); + if (dfu_clear_status(dfu_root->dev_handle, + dfu_root->interface) < 0) { + errx(EX_IOERR, "error clear_status"); + } + /* fall through */ + default: + warnx("WARNING: Runtime device already in DFU state ?!?"); + libusb_release_interface(dfu_root->dev_handle, + dfu_root->interface); + goto dfustate; + } + libusb_release_interface(dfu_root->dev_handle, + dfu_root->interface); + libusb_close(dfu_root->dev_handle); + dfu_root->dev_handle = NULL; + + if (mode == MODE_DETACH) { + libusb_exit(ctx); + exit(0); + } + + /* keeping handles open might prevent re-enumeration */ + disconnect_devices(); + + milli_sleep(detach_delay * 1000); + + /* Change match vendor and product to impossible values to force + * only DFU mode matches in the following probe */ + match_vendor = match_product = 0x10000; + + probe_devices(ctx); + + if (dfu_root == NULL) { + errx(EX_IOERR, "Lost device after RESET?"); + } else if (dfu_root->next != NULL) { + errx(EX_IOERR, "More than one DFU capable USB device found! " + "Try `--list' and specify the serial number " + "or disconnect all but one device"); + } + + /* Check for DFU mode device */ + if (!(dfu_root->flags | DFU_IFF_DFU)) + errx(EX_SOFTWARE, "Device is not in DFU mode"); + + printf("Opening DFU USB Device...\n"); + ret = libusb_open(dfu_root->dev, &dfu_root->dev_handle); + if (ret || !dfu_root->dev_handle) { + errx(EX_IOERR, "Cannot open device"); + } + } else { + /* we're already in DFU mode, so we can skip the detach/reset + * procedure */ + /* If a match vendor/product was specified, use that as the runtime + * vendor/product, otherwise use the DFU mode vendor/product */ + runtime_vendor = match_vendor < 0 ? dfu_root->vendor : match_vendor; + runtime_product = match_product < 0 ? dfu_root->product : match_product; + } + +dfustate: +#if 0 + printf("Setting Configuration %u...\n", dfu_root->configuration); + if (libusb_set_configuration(dfu_root->dev_handle, dfu_root->configuration) < 0) { + errx(EX_IOERR, "Cannot set configuration"); + } +#endif + printf("Claiming USB DFU Interface...\n"); + if (libusb_claim_interface(dfu_root->dev_handle, dfu_root->interface) < 0) { + errx(EX_IOERR, "Cannot claim interface"); + } + + printf("Setting Alternate Setting #%d ...\n", dfu_root->altsetting); + if (libusb_set_interface_alt_setting(dfu_root->dev_handle, dfu_root->interface, dfu_root->altsetting) < 0) { + errx(EX_IOERR, "Cannot set alternate interface"); + } + +status_again: + printf("Determining device status: "); + if (dfu_get_status(dfu_root, &status ) < 0) { + errx(EX_IOERR, "error get_status"); + } + printf("state = %s, status = %d\n", + dfu_state_to_string(status.bState), status.bStatus); + + milli_sleep(status.bwPollTimeout); + + switch (status.bState) { + case DFU_STATE_appIDLE: + case DFU_STATE_appDETACH: + errx(EX_IOERR, "Device still in Runtime Mode!"); + break; + case DFU_STATE_dfuERROR: + printf("dfuERROR, clearing status\n"); + if (dfu_clear_status(dfu_root->dev_handle, dfu_root->interface) < 0) { + errx(EX_IOERR, "error clear_status"); + } + goto status_again; + break; + case DFU_STATE_dfuDNLOAD_IDLE: + case DFU_STATE_dfuUPLOAD_IDLE: + printf("aborting previous incomplete transfer\n"); + if (dfu_abort(dfu_root->dev_handle, dfu_root->interface) < 0) { + errx(EX_IOERR, "can't send DFU_ABORT"); + } + goto status_again; + break; + case DFU_STATE_dfuIDLE: + printf("dfuIDLE, continuing\n"); + break; + default: + break; + } + + if (DFU_STATUS_OK != status.bStatus ) { + printf("WARNING: DFU Status: '%s'\n", + dfu_status_to_string(status.bStatus)); + /* Clear our status & try again. */ + if (dfu_clear_status(dfu_root->dev_handle, dfu_root->interface) < 0) + errx(EX_IOERR, "USB communication error"); + if (dfu_get_status(dfu_root, &status) < 0) + errx(EX_IOERR, "USB communication error"); + if (DFU_STATUS_OK != status.bStatus) + errx(EX_SOFTWARE, "Status is not OK: %d", status.bStatus); + + milli_sleep(status.bwPollTimeout); + } + + printf("DFU mode device DFU version %04x\n", + libusb_le16_to_cpu(dfu_root->func_dfu.bcdDFUVersion)); + + if (dfu_root->func_dfu.bcdDFUVersion == libusb_cpu_to_le16(0x11a)) + dfuse_device = 1; + + /* If not overridden by the user */ + if (!transfer_size) { + transfer_size = libusb_le16_to_cpu( + dfu_root->func_dfu.wTransferSize); + if (transfer_size) { + printf("Device returned transfer size %i\n", + transfer_size); + } else { + errx(EX_IOERR, "Transfer size must be specified"); + } + } + +#ifdef HAVE_GETPAGESIZE +/* autotools lie when cross-compiling for Windows using mingw32/64 */ +#ifndef __MINGW32__ + /* limitation of Linux usbdevio */ + if ((int)transfer_size > getpagesize()) { + transfer_size = getpagesize(); + printf("Limited transfer size to %i\n", transfer_size); + } +#endif /* __MINGW32__ */ +#endif /* HAVE_GETPAGESIZE */ + + if (transfer_size < dfu_root->bMaxPacketSize0) { + transfer_size = dfu_root->bMaxPacketSize0; + printf("Adjusted transfer size to %i\n", transfer_size); + } + + switch (mode) { + case MODE_UPLOAD: + /* open for "exclusive" writing */ + fd = open(file.name, O_WRONLY | O_BINARY | O_CREAT | O_EXCL | O_TRUNC, 0666); + if (fd < 0) + err(EX_IOERR, "Cannot open file %s for writing", file.name); + + if (dfuse_device || dfuse_options) { + if (dfuse_do_upload(dfu_root, transfer_size, fd, + dfuse_options) < 0) + exit(1); + } else { + if (dfuload_do_upload(dfu_root, transfer_size, + expected_size, fd) < 0) { + exit(1); + } + } + close(fd); + break; + + case MODE_DOWNLOAD: + if (((file.idVendor != 0xffff && file.idVendor != runtime_vendor) || + (file.idProduct != 0xffff && file.idProduct != runtime_product)) && + ((file.idVendor != 0xffff && file.idVendor != dfu_root->vendor) || + (file.idProduct != 0xffff && file.idProduct != dfu_root->product))) { + errx(EX_IOERR, "Error: File ID %04x:%04x does " + "not match device (%04x:%04x or %04x:%04x)", + file.idVendor, file.idProduct, + runtime_vendor, runtime_product, + dfu_root->vendor, dfu_root->product); + } + if (dfuse_device || dfuse_options || file.bcdDFU == 0x11a) { + if (dfuse_do_dnload(dfu_root, transfer_size, &file, + dfuse_options) < 0) + exit(1); + } else { + if (dfuload_do_dnload(dfu_root, transfer_size, &file) < 0) + exit(1); + } + break; + case MODE_DETACH: + if (dfu_detach(dfu_root->dev_handle, dfu_root->interface, 1000) < 0) { + warnx("can't detach"); + } + break; + default: + errx(EX_IOERR, "Unsupported mode: %u", mode); + break; + } + + if (final_reset) { + if (dfu_detach(dfu_root->dev_handle, dfu_root->interface, 1000) < 0) { + /* Even if detach failed, just carry on to leave the + device in a known state */ + warnx("can't detach"); + } + printf("Resetting USB to switch back to runtime mode\n"); + ret = libusb_reset_device(dfu_root->dev_handle); + if (ret < 0 && ret != LIBUSB_ERROR_NOT_FOUND) { + errx(EX_IOERR, "error resetting after download"); + } + } + + libusb_close(dfu_root->dev_handle); + dfu_root->dev_handle = NULL; + libusb_exit(ctx); + + return (0); +} diff --git a/tools/macosx/src/dfu-util/src/portable.h b/tools/macosx/src/dfu-util/src/portable.h new file mode 100644 index 0000000..cf8d5df --- /dev/null +++ b/tools/macosx/src/dfu-util/src/portable.h @@ -0,0 +1,72 @@ + +#ifndef PORTABLE_H +#define PORTABLE_H + +#ifdef HAVE_CONFIG_H +# include "config.h" +#else +# define PACKAGE "dfu-util" +# define PACKAGE_VERSION "0.8-msvc" +# define PACKAGE_STRING "dfu-util 0.8-msvc" +# define PACKAGE_BUGREPORT "dfu-util@lists.gnumonks.org" +#endif /* HAVE_CONFIG_H */ + +#ifdef HAVE_FTRUNCATE +# include +#else +# include +#endif /* HAVE_FTRUNCATE */ + +#ifdef HAVE_NANOSLEEP +# include +# define milli_sleep(msec) do {\ + if (msec) {\ + struct timespec nanosleepDelay = { (msec) / 1000, ((msec) % 1000) * 1000000 };\ + nanosleep(&nanosleepDelay, NULL);\ + } } while (0) +#elif defined HAVE_WINDOWS_H +# define milli_sleep(msec) do {\ + if (msec) {\ + Sleep(msec);\ + } } while (0) +#else +# error "Can't get no sleep! Please report" +#endif /* HAVE_NANOSLEEP */ + +#ifdef HAVE_ERR +# include +#else +# include +# include +# define warnx(...) do {\ + fprintf(stderr, __VA_ARGS__);\ + fprintf(stderr, "\n"); } while (0) +# define errx(eval, ...) do {\ + warnx(__VA_ARGS__);\ + exit(eval); } while (0) +# define warn(...) do {\ + fprintf(stderr, "%s: ", strerror(errno));\ + warnx(__VA_ARGS__); } while (0) +# define err(eval, ...) do {\ + warn(__VA_ARGS__);\ + exit(eval); } while (0) +#endif /* HAVE_ERR */ + +#ifdef HAVE_SYSEXITS_H +# include +#else +# define EX_OK 0 /* successful termination */ +# define EX_USAGE 64 /* command line usage error */ +# define EX_SOFTWARE 70 /* internal software error */ +# define EX_IOERR 74 /* input/output error */ +#endif /* HAVE_SYSEXITS_H */ + +#ifndef O_BINARY +# define O_BINARY 0 +#endif + +#ifndef off_t +# define off_t long int +#endif + +#endif /* PORTABLE_H */ diff --git a/tools/macosx/src/dfu-util/src/prefix.c b/tools/macosx/src/dfu-util/src/prefix.c new file mode 100644 index 0000000..be8e3fa --- /dev/null +++ b/tools/macosx/src/dfu-util/src/prefix.c @@ -0,0 +1,176 @@ +/* + * dfu-prefix + * + * Copyright 2011-2012 Stefan Schmidt + * Copyright 2013 Hans Petter Selasky + * Copyright 2014 Uwe Bonnes + * + * 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 +#include +#include +#include +#include + +#include "portable.h" +#include "dfu_file.h" + +enum mode { + MODE_NONE, + MODE_ADD, + MODE_DEL, + MODE_CHECK +}; + +int verbose; + +static void help(void) +{ + fprintf(stderr, "Usage: dfu-prefix [options] ...\n" + " -h --help\t\t\tPrint this help message\n" + " -V --version\t\t\tPrint the version number\n" + " -c --check \t\tCheck DFU prefix of \n" + " -D --delete \t\tDelete DFU prefix from \n" + " -a --add \t\tAdd DFU prefix to \n" + "In combination with -a:\n" + ); + fprintf(stderr, " -s --stellaris-address
Add TI Stellaris address prefix to \n" + "In combination with -D or -c:\n" + " -T --stellaris\t\tAct on TI Stellaris address prefix of \n" + "In combination with -a or -D or -c:\n" + " -L --lpc-prefix\t\tUse NXP LPC DFU prefix format\n" + ); + exit(EX_USAGE); +} + +static void print_version(void) +{ + printf("dfu-prefix (%s) %s\n\n", PACKAGE, PACKAGE_VERSION); + printf("Copyright 2011-2012 Stefan Schmidt, 2014 Uwe Bonnes\n" + "This program is Free Software and has ABSOLUTELY NO WARRANTY\n" + "Please report bugs to %s\n\n", PACKAGE_BUGREPORT); + +} + +static struct option opts[] = { + { "help", 0, 0, 'h' }, + { "version", 0, 0, 'V' }, + { "check", 1, 0, 'c' }, + { "add", 1, 0, 'a' }, + { "delete", 1, 0, 'D' }, + { "stellaris-address", 1, 0, 's' }, + { "stellaris", 0, 0, 'T' }, + { "LPC", 0, 0, 'L' }, +}; +int main(int argc, char **argv) +{ + struct dfu_file file; + enum mode mode = MODE_NONE; + enum prefix_type type = ZERO_PREFIX; + uint32_t lmdfu_flash_address = 0; + char *end; + + /* make sure all prints are flushed */ + setvbuf(stdout, NULL, _IONBF, 0); + + print_version(); + + memset(&file, 0, sizeof(file)); + + while (1) { + int c, option_index = 0; + c = getopt_long(argc, argv, "hVc:a:D:p:v:d:s:TL", opts, + &option_index); + if (c == -1) + break; + + switch (c) { + case 'h': + help(); + break; + case 'V': + exit(0); + break; + case 'D': + file.name = optarg; + mode = MODE_DEL; + break; + case 'c': + file.name = optarg; + mode = MODE_CHECK; + break; + case 'a': + file.name = optarg; + mode = MODE_ADD; + break; + case 's': + lmdfu_flash_address = strtoul(optarg, &end, 0); + if (*end) { + errx(EX_IOERR, "Invalid lmdfu " + "address: %s", optarg); + } + /* fall-through */ + case 'T': + type = LMDFU_PREFIX; + break; + case 'L': + type = LPCDFU_UNENCRYPTED_PREFIX; + break; + default: + help(); + break; + } + } + + if (!file.name) { + fprintf(stderr, "You need to specify a filename\n"); + help(); + } + + switch(mode) { + case MODE_ADD: + if (type == ZERO_PREFIX) + errx(EX_IOERR, "Prefix type must be specified"); + dfu_load_file(&file, MAYBE_SUFFIX, NO_PREFIX); + file.lmdfu_address = lmdfu_flash_address; + file.prefix_type = type; + printf("Adding prefix to file\n"); + dfu_store_file(&file, file.size.suffix != 0, 1); + break; + + case MODE_CHECK: + dfu_load_file(&file, MAYBE_SUFFIX, MAYBE_PREFIX); + show_suffix_and_prefix(&file); + if (type > ZERO_PREFIX && file.prefix_type != type) + errx(EX_IOERR, "No prefix of requested type"); + break; + + case MODE_DEL: + dfu_load_file(&file, MAYBE_SUFFIX, NEEDS_PREFIX); + if (type > ZERO_PREFIX && file.prefix_type != type) + errx(EX_IOERR, "No prefix of requested type"); + printf("Removing prefix from file\n"); + /* if there was a suffix, rewrite it */ + dfu_store_file(&file, file.size.suffix != 0, 0); + break; + + default: + help(); + break; + } + return (0); +} diff --git a/tools/macosx/src/dfu-util/src/quirks.c b/tools/macosx/src/dfu-util/src/quirks.c new file mode 100644 index 0000000..de394a6 --- /dev/null +++ b/tools/macosx/src/dfu-util/src/quirks.c @@ -0,0 +1,56 @@ +/* + * Simple quirk system for dfu-util + * + * Copyright 2010-2014 Tormod Volden + * + * 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 +#include "quirks.h" + +uint16_t get_quirks(uint16_t vendor, uint16_t product, uint16_t bcdDevice) +{ + uint16_t quirks = 0; + + /* Device returns bogus bwPollTimeout values */ + if ((vendor == VENDOR_OPENMOKO || vendor == VENDOR_FIC) && + product >= PRODUCT_FREERUNNER_FIRST && + product <= PRODUCT_FREERUNNER_LAST) + quirks |= QUIRK_POLLTIMEOUT; + + if (vendor == VENDOR_VOTI && + product == PRODUCT_OPENPCD) + quirks |= QUIRK_POLLTIMEOUT; + + /* Reports wrong DFU version in DFU descriptor */ + if (vendor == VENDOR_LEAFLABS && + product == PRODUCT_MAPLE3 && + bcdDevice == 0x0200) + quirks |= QUIRK_FORCE_DFU11; + + /* old devices(bcdDevice == 0) return bogus bwPollTimeout values */ + if (vendor == VENDOR_SIEMENS && + (product == PRODUCT_PXM40 || product == PRODUCT_PXM50) && + bcdDevice == 0) + quirks |= QUIRK_POLLTIMEOUT; + + /* M-Audio Transit returns bogus bwPollTimeout values */ + if (vendor == VENDOR_MIDIMAN && + product == PRODUCT_TRANSIT) + quirks |= QUIRK_POLLTIMEOUT; + + return (quirks); +} diff --git a/tools/macosx/src/dfu-util/src/quirks.h b/tools/macosx/src/dfu-util/src/quirks.h new file mode 100644 index 0000000..0e4b3ec --- /dev/null +++ b/tools/macosx/src/dfu-util/src/quirks.h @@ -0,0 +1,27 @@ +#ifndef DFU_QUIRKS_H +#define DFU_QUIRKS_H + +#define VENDOR_OPENMOKO 0x1d50 /* Openmoko Freerunner / GTA02 */ +#define VENDOR_FIC 0x1457 /* Openmoko Freerunner / GTA02 */ +#define VENDOR_VOTI 0x16c0 /* OpenPCD Reader */ +#define VENDOR_LEAFLABS 0x1eaf /* Maple */ +#define VENDOR_SIEMENS 0x0908 /* Siemens AG */ +#define VENDOR_MIDIMAN 0x0763 /* Midiman */ + +#define PRODUCT_FREERUNNER_FIRST 0x5117 +#define PRODUCT_FREERUNNER_LAST 0x5126 +#define PRODUCT_OPENPCD 0x076b +#define PRODUCT_MAPLE3 0x0003 /* rev 3 and 5 */ +#define PRODUCT_PXM40 0x02c4 /* Siemens AG, PXM 40 */ +#define PRODUCT_PXM50 0x02c5 /* Siemens AG, PXM 50 */ +#define PRODUCT_TRANSIT 0x2806 /* M-Audio Transit (Midiman) */ + +#define QUIRK_POLLTIMEOUT (1<<0) +#define QUIRK_FORCE_DFU11 (1<<1) + +/* Fallback value, works for OpenMoko */ +#define DEFAULT_POLLTIMEOUT 5 + +uint16_t get_quirks(uint16_t vendor, uint16_t product, uint16_t bcdDevice); + +#endif /* DFU_QUIRKS_H */ diff --git a/tools/macosx/src/dfu-util/src/suffix.c b/tools/macosx/src/dfu-util/src/suffix.c new file mode 100644 index 0000000..0df248f --- /dev/null +++ b/tools/macosx/src/dfu-util/src/suffix.c @@ -0,0 +1,176 @@ +/* + * dfu-suffix + * + * Copyright 2011-2012 Stefan Schmidt + * Copyright 2013 Hans Petter Selasky + * Copyright 2014 Tormod Volden + * + * 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 +#include +#include +#include +#include + +#include "portable.h" +#include "dfu_file.h" + +enum mode { + MODE_NONE, + MODE_ADD, + MODE_DEL, + MODE_CHECK +}; + +int verbose; + +static void help(void) +{ + fprintf(stderr, "Usage: dfu-suffix [options] ...\n" + " -h --help\t\t\tPrint this help message\n" + " -V --version\t\t\tPrint the version number\n" + " -c --check \t\tCheck DFU suffix of \n" + " -a --add \t\tAdd DFU suffix to \n" + " -D --delete \t\tDelete DFU suffix from \n" + " -p --pid \t\tAdd product ID into DFU suffix in \n" + " -v --vid \t\tAdd vendor ID into DFU suffix in \n" + " -d --did \t\tAdd device ID into DFU suffix in \n" + " -S --spec \t\tAdd DFU specification ID into DFU suffix in \n" + ); + exit(EX_USAGE); +} + +static void print_version(void) +{ + printf("dfu-suffix (%s) %s\n\n", PACKAGE, PACKAGE_VERSION); + printf("Copyright 2011-2012 Stefan Schmidt, 2013-2014 Tormod Volden\n" + "This program is Free Software and has ABSOLUTELY NO WARRANTY\n" + "Please report bugs to %s\n\n", PACKAGE_BUGREPORT); + +} + +static struct option opts[] = { + { "help", 0, 0, 'h' }, + { "version", 0, 0, 'V' }, + { "check", 1, 0, 'c' }, + { "add", 1, 0, 'a' }, + { "delete", 1, 0, 'D' }, + { "pid", 1, 0, 'p' }, + { "vid", 1, 0, 'v' }, + { "did", 1, 0, 'd' }, + { "spec", 1, 0, 'S' }, +}; + +int main(int argc, char **argv) +{ + struct dfu_file file; + int pid, vid, did, spec; + enum mode mode = MODE_NONE; + + /* make sure all prints are flushed */ + setvbuf(stdout, NULL, _IONBF, 0); + + print_version(); + + pid = vid = did = 0xffff; + spec = 0x0100; /* Default to bcdDFU version 1.0 */ + memset(&file, 0, sizeof(file)); + + while (1) { + int c, option_index = 0; + c = getopt_long(argc, argv, "hVc:a:D:p:v:d:S:s:T", opts, + &option_index); + if (c == -1) + break; + + switch (c) { + case 'h': + help(); + break; + case 'V': + exit(0); + break; + case 'D': + file.name = optarg; + mode = MODE_DEL; + break; + case 'p': + pid = strtol(optarg, NULL, 16); + break; + case 'v': + vid = strtol(optarg, NULL, 16); + break; + case 'd': + did = strtol(optarg, NULL, 16); + break; + case 'S': + spec = strtol(optarg, NULL, 16); + break; + case 'c': + file.name = optarg; + mode = MODE_CHECK; + break; + case 'a': + file.name = optarg; + mode = MODE_ADD; + break; + default: + help(); + break; + } + } + + if (!file.name) { + fprintf(stderr, "You need to specify a filename\n"); + help(); + } + + if (spec != 0x0100 && spec != 0x011a) { + fprintf(stderr, "Only DFU specification 0x0100 and 0x011a supported\n"); + help(); + } + + switch(mode) { + case MODE_ADD: + dfu_load_file(&file, NO_SUFFIX, MAYBE_PREFIX); + file.idVendor = vid; + file.idProduct = pid; + file.bcdDevice = did; + file.bcdDFU = spec; + /* always write suffix, rewrite prefix if there was one */ + dfu_store_file(&file, 1, file.size.prefix != 0); + printf("Suffix successfully added to file\n"); + break; + + case MODE_CHECK: + dfu_load_file(&file, NEEDS_SUFFIX, MAYBE_PREFIX); + show_suffix_and_prefix(&file); + break; + + case MODE_DEL: + dfu_load_file(&file, NEEDS_SUFFIX, MAYBE_PREFIX); + dfu_store_file(&file, 0, file.size.prefix != 0); + if (file.size.suffix) /* had a suffix */ + printf("Suffix successfully removed from file\n"); + break; + + default: + help(); + break; + } + return (0); +} diff --git a/tools/macosx/src/dfu-util/src/usb_dfu.h b/tools/macosx/src/dfu-util/src/usb_dfu.h new file mode 100644 index 0000000..660bedc --- /dev/null +++ b/tools/macosx/src/dfu-util/src/usb_dfu.h @@ -0,0 +1,99 @@ +#ifndef USB_DFU_H +#define USB_DFU_H +/* USB Device Firmware Update Implementation for OpenPCD + * (C) 2006 by Harald Welte + * + * 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 + * + * 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 + +#define USB_DT_DFU 0x21 + +#ifdef _MSC_VER +# pragma pack(push) +# pragma pack(1) +#endif /* _MSC_VER */ +struct usb_dfu_func_descriptor { + uint8_t bLength; + uint8_t bDescriptorType; + uint8_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) + uint16_t wDetachTimeOut; + uint16_t wTransferSize; + uint16_t bcdDFUVersion; +#ifdef _MSC_VER +}; +# pragma pack(pop) +#elif defined __GNUC__ +} __attribute__ ((packed)); +#else + #warning "No way to pack struct on this compiler? This will break!" +#endif /* _MSC_VER */ + +#define USB_DT_DFU_SIZE 9 + +#define USB_TYPE_DFU (LIBUSB_REQUEST_TYPE_CLASS|LIBUSB_RECIPIENT_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 + +/* DFU_GETSTATUS bStatus values (Section 6.1.2, DFU Rev 1.1) */ +#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 */ diff --git a/tools/macosx/src/dfu-util/www/build.html b/tools/macosx/src/dfu-util/www/build.html new file mode 100644 index 0000000..f3036e4 --- /dev/null +++ b/tools/macosx/src/dfu-util/www/build.html @@ -0,0 +1,147 @@ + + + + + + + Building dfu-util from source + + + + + + + + + +
+

How to build dfu-util from source

+ +

Prerequisites for building from git

+

Mac OS X

+

+First install MacPorts (and if you are on 10.6 or older, the Java Developer Package) and then run: +

+
+	sudo port install libusb-devel git-core
+
+ +

FreeBSD

+
+	sudo pkg_add -r git pkgconf
+
+ +

Ubuntu and Debian and derivatives

+
+	sudo apt-get build-dep dfu-util
+	sudo apt-get install libusb-1.0-0-dev
+
+ +

Get the source code and build it

+

+The first time you will have to clone the git repository: +

+
+	git clone git://gitorious.org/dfu-util/dfu-util.git
+	cd dfu-util
+
+

+If you later want to update to latest git version, just run this: +

+
+	make maintainer-clean
+	git pull
+
+

+To build the source: +

+
+	./autogen.sh
+	./configure  # on most systems
+	make
+
+ +

+If you are building on Mac OS X, replace the ./configure command with: +

+
+	./configure --libdir=/opt/local/lib --includedir=/opt/local/include  # on MacOSX only
+
+ +

+Your dfu-util binary will be inside the src folder. Use it from there, or install it to /usr/local/bin by running "sudo make install". +

+ +

Cross-building for Windows

+ +

+Windows binaries can be built in a MinGW +environment, on a Windows computer or cross-hosted in another OS. +To build it on a Debian or Ubuntu host, first install build dependencies: +

+
+	sudo apt-get build-dep libusb-1.0-0 dfu-util
+	sudo apt-get install mingw32
+
+ +

+The below example builds dfu-util 0.8 and libusb 1.0.19 from unpacked release +tarballs. If you instead build from git, you will have to run "./autogen.sh" +before running the "./configure" steps. +

+ +
+mkdir -p build
+cd libusb-1.0.19
+PKG_CONFIG_PATH=$PWD/../build/lib/pkgconfig ./configure \
+    --host=i586-mingw32msvc --prefix=$PWD/../build
+# WINVER workaround needed for 1.0.19 only
+make CFLAGS="-DWINVER=0x0501"
+make install
+cd ..
+
+cd dfu-util-0.8
+PKG_CONFIG_PATH=$PWD/../build/lib/pkgconfig ./configure \
+    --host=i586-mingw32msvc --prefix=$PWD/../build
+make
+make install
+cd ..
+
+The build files will now be in build/bin. +

+ +

Building on Windows using MinGW

+This assumes using release tarballs or having run ./autogen.sh on +the git sources. +
+cd libusb-1.0.19
+./configure --prefix=$HOME
+# WINVER workaround needed for 1.0.19 only
+# MKDIR_P setting should not really be needed...
+make CFLAGS="-DWINVER=0x0501" MKDIR_P="mkdir -p"
+make install
+cd ..
+
+cd dfu-util-0.8
+./configure USB_CFLAGS="-I$HOME/include/libusb-1.0" \
+            USB_LIBS="-L $HOME/lib -lusb-1.0" PKG_CONFIG=true
+make
+make install
+cd ..
+
+To link libusb statically into dfu-util.exe use instead of "make": +
+make LDFLAGS=-static
+
+The built executables (and DLL) will now be under $HOME/bin. + +

+[Back to dfu-util main page] +

+ +
+ + diff --git a/tools/macosx/src/dfu-util/www/dfu-util.1.html b/tools/macosx/src/dfu-util/www/dfu-util.1.html new file mode 100644 index 0000000..62ca40b --- /dev/null +++ b/tools/macosx/src/dfu-util/www/dfu-util.1.html @@ -0,0 +1,411 @@ + + +Man page of DFU-UTIL + + +

DFU-UTIL(1)

+ +  +

NAME

+ +dfu-util - Device firmware update (DFU) USB programmer +  +

SYNOPSIS

+ + +
+
+dfu-util + +-l + +[-v] + +[-d + +vid:pid[,vid:pid]] + +[-p + +path] + +[-c + +configuration] + +[-i + +interface] + +[-a + +alt-intf] + +[-S + +serial[,serial]] + + +
+dfu-util + +[-v] + +[-d + +vid:pid[,vid:pid]] + +[-p + +path] + +[-c + +configuration] + +[-i + +interface] + +[-a + +alt-intf] + +[-S + +serial[,serial]] + +[-t + +size] + +[-Z + +size] + +[-s + +address] + +[-R] + +[-D|-U + +file] + + +
+dfu-util + +[-hV] + +
+  +

DESCRIPTION

+ +dfu-util + +is a program that implements the host (computer) side of the USB DFU +(Universal Serial Bus Device Firmware Upgrade) protocol. +

+dfu-util communicates with devices that implement the device side of the +USB DFU protocol, and is often used to upgrade the firmware of such +devices. +  +

OPTIONS

+ +
+
-l, --list + +
+List the currently attached DFU capable USB devices. +
-d, --device [Run-Time VENDOR]:[Run-Time PRODUCT][,[DFU Mode VENDOR]:[DFU Mode PRODUCT]] + +
+
+Specify run-time and/or DFU mode vendor and/or product IDs of the DFU device +to work with. VENDOR and PRODUCT are hexadecimal numbers (no prefix +needed), "*" (match any), or "-" (match nothing). By default, any DFU capable +device in either run-time or DFU mode will be considered. +

+If you only have one standards-compliant DFU device attached to your computer, +this parameter is optional. However, as soon as you have multiple DFU devices +connected, dfu-util will detect this and abort, asking you to specify which +device to use. +

+If only run-time IDs are specified (e.g. "--device 1457:51ab"), then in +addition to the specified run-time IDs, any DFU mode devices will also be +considered. This is beneficial to allow a DFU capable device to be found +again after a switch to DFU mode, since the vendor and/or product ID of a +device usually changes in DFU mode. +

+If only DFU mode IDs are specified (e.g. "--device ,951:26"), then all +run-time devices will be ignored, making it easy to target a specific device in +DFU mode. +

+If both run-time and DFU mode IDs are specified (e.g. "--device +1457:51ab,:2bc"), then unspecified DFU mode components will use the run-time +value specified. +

+Examples: +

+
--device 1457:51ab,951:26 + +
+
+ +Work with a device in run-time mode with +vendor ID 0x1457 and product ID 0x51ab, or in DFU mode with vendor ID 0x0951 +and product ID 0x0026 +

+

--device 1457:51ab,:2bc + +
+
+ +Work with a device in run-time mode with vendor ID 0x1457 and product ID +0x51ab, or in DFU mode with vendor ID 0x1457 and product ID 0x02bc +

+

--device 1457:51ab + +
+
+ +Work with a device in run-time mode with vendor ID 0x1457 and product ID +0x51ab, or in DFU mode with any vendor and product ID +

+

--device ,951:26 + +
+
+ +Work with a device in DFU mode with vendor ID 0x0951 and product ID 0x0026 +

+

--device *,- + +
+
+ +Work with any device in run-time mode, and ignore any device in DFU mode +

+

--device , + +
+
+ +Ignore any device in run-time mode, and Work with any device in DFU mode +
+
+ +
-p, --path BUS-PORT. ... .PORT + +
+Specify the path to the DFU device. +
-c, --cfg CONFIG-NR + +
+Specify the configuration of the DFU device. Note that this is only used for matching, the configuration is not set by dfu-util. +
-i, --intf INTF-NR + +
+Specify the DFU interface number. +
-a, --alt ALT + +
+Specify the altsetting of the DFU interface by name or by number. +
-S, --serial [Run-Time SERIAL][,[DFU Mode SERIAL]] + +
+Specify the run-time and DFU mode serial numbers used to further restrict +device matches. If multiple, identical DFU devices are simultaneously +connected to a system then vendor and product ID will be insufficient for +targeting a single device. In this situation, it may be possible to use this +parameter to specify a serial number which also must match. +

+If only a single serial number is specified, then the same serial number is +used in both run-time and DFU mode. An empty serial number will match any +serial number in the corresponding mode. +

-t, --transfer-size SIZE + +
+Specify the number of bytes per USB transfer. The optimal value is +usually determined automatically so this option is rarely useful. If +you need to use this option for a device, please report it as a bug. +
-Z, --upload-size SIZE + +
+Specify the expected upload size, in bytes. +
-U, --upload FILE + +
+Read firmware from device into +FILE. + +
-D, --download FILE + +
+Write firmware from +FILE + +into device. +
-R, --reset + +
+Issue USB reset signalling after upload or download has finished. +
-s, --dfuse-address address + +
+Specify target address for raw binary download/upload on DfuSe devices. Do +not + +use this for downloading DfuSe (.dfu) files. Modifiers can be added +to the address, separated by a colon, to perform special DfuSE commands such +as "leave" DFU mode, "unprotect" and "mass-erase" flash memory. +
-v, --verbose + +
+Print more information about dfu-util's operation. A second +-v + +will turn on verbose logging of USB requests. Repeat this option to further +increase verbosity. +
-h, --help + +
+Show a help text and exit. +
-V, --version + +
+Show version information and exit. +
+  +

EXAMPLES

+ +  +

Using dfu-util in the OpenMoko project

+ +(with the Neo1973 hardware) +

+ +Flashing the rootfs: +
+ + $ dfu-util -a rootfs -R -D /path/to/openmoko-devel-image.jffs2 + +

+ +Flashing the kernel: +
+ + $ dfu-util -a kernel -R -D /path/to/uImage + +

+ +Flashing the bootloader: +
+ + $ dfu-util -a u-boot -R -D /path/to/u-boot.bin + +

+ +Copying a kernel into RAM: +
+ + $ dfu-util -a 0 -R -D /path/to/uImage + +

+Once this has finished, the kernel will be available at the default load +address of 0x32000000 in Neo1973 RAM. +Note: + +You cannot transfer more than 2MB of data into RAM using this method. +

+  +

Using dfu-util with a DfuSe device

+ +

+ +Flashing a +.dfu + +(special DfuSe format) file to the device: +
+ + $ dfu-util -a 0 -D /path/to/dfuse-image.dfu + +

+ +Reading out 1 KB of flash starting at address 0x8000000: +
+ + $ dfu-util -a 0 -s 0x08000000:1024 -U newfile.bin + +

+ +Flashing a binary file to address 0x8004000 of device memory and +ask the device to leave DFU mode: +
+ + $ dfu-util -a 0 -s 0x08004000:leave -D /path/to/image.bin + + +  +

BUGS

+ +Please report any bugs to the dfu-util mailing list at +dfu-util@lists.gnumonks.org. + +Please use the +--verbose option (repeated as necessary) to provide more + +information in your bug report. +  +

SEE ALSO

+ +The dfu-util home page is +http://dfu-util.gnumonks.org + +  +

HISTORY

+ +dfu-util was originally written for the OpenMoko project by +Weston Schmidt <weston_schmidt@yahoo.com> and +Harald Welte <hwelte@hmw-consulting.de>. Over time, nearly complete +support of DFU 1.0, DFU 1.1 and DfuSe ("1.1a") has been added. +  +

LICENCE

+ +dfu-util + +is covered by the GNU General Public License (GPL), version 2 or later. +  +

COPYRIGHT

+ +This manual page was originally written by Uwe Hermann <uwe@hermann-uwe.de>, +and is now part of the dfu-util project. +

+ +


+ 

Index

+
+
NAME
+
SYNOPSIS
+
DESCRIPTION
+
OPTIONS
+
EXAMPLES
+
+
Using dfu-util in the OpenMoko project
+
Using dfu-util with a DfuSe device
+
+
BUGS
+
SEE ALSO
+
HISTORY
+
LICENCE
+
COPYRIGHT
+
+
+This document was created by man2html, +using the doc/dfu-util.1 manual page from dfu-util 0.8.
+Time: 14:40:57 GMT, September 13, 2014 + + diff --git a/tools/macosx/src/dfu-util/www/dfuse.html b/tools/macosx/src/dfu-util/www/dfuse.html new file mode 100644 index 0000000..35e4ffa --- /dev/null +++ b/tools/macosx/src/dfu-util/www/dfuse.html @@ -0,0 +1,135 @@ + + + + + + DfuSe and dfu-util + + + + + + + + + +
+

Using dfu-util with DfuSe devices

+

DfuSe

+

+ DfuSe (DFU with ST Microsystems extensions) is a protocol based on + DFU 1.1. However, in expanding the functionality of the DFU protocol, + ST Microsystems broke all compatibility with the DFU 1.1 standard. + DfuSe devices report the DFU version as "1.1a". +

+

+ DfuSe can be used to download firmware and other data + from a host computer to a conforming device (or upload in the + opposite direction) over USB similar to standard DFU. +

+

+ The main difference from standard DFU is that the target address in + the device (flash) memory is specified by the host, so that a + download can be performed to parts of the device memory. The host + program is also responsible for erasing flash pages before they + are written to. +

+

.dfu files

+

+ A special file format is defined by ST Microsystems to carry firmware + for DfuSe devices. The file contains target information such as address + and alternate interface information in addition to the binary data. + Several blocks of binary data can be combined in one .dfu file. +

+

Alternate interfaces

+

+ Different memory locations of the device may have different + characteristics that the host program (dfu-util) has to take + into considerations, such as flash memory page size, read-only + versus read-write segments, the need to erase, and so on. + These parameters are reported by the device in the string + descriptors meant for describing the USB interfaces. + The host program decodes these strings to build a memory map of + the device. Different memory units or address spaces are listed + in separate alternate interface settings that must be selected + according to the memory unit to access. +

+

+ Note that dfu-util leaves it to the user to select alternate + interface. When parsing a .dfu file it will skip file segments + not matching the selected alternate interface. Also, some + DfuSe device firmware implementations ignore the setting of + alternate interface and deduct the memory unit from the + address, since they have no address space overlap. +

+

DfuSe special commands

+

+ DfuSe special commands are used by the host program during normal + downloads or uploads, such as SET_ADDRESS and ERASE_PAGE. Also + the normal DFU_DNLOAD and DFU_UPLOAD commands have special + implementations in DfuSe. + Many DfuSe devices also support commands to leave DFU mode, + read unprotect the flash memory or mass erase the flash memory. + dfu-util (from version 0.7) + supports adding "leave", "unprotect", or "mass-erase" + to the -s option argument to send such requests in combination + with a download request. These modifiers are separated with a colon. +

+

+ Some DfuSe devices have their DfuSe bootloader running from flash + memory. Erasing the whole flash memory would therefore destroy + the DfuSe bootloader itself and practically brick the device + for most users. Any use of modifiers such as "unprotect" + and "mass-erase" therefore needs to be combined with the "force" + modifer. This is not included in the examples, to not encourage + ignorant users to copy and paste such instructions and shoot + themselves in the foot. +

+

+ Devices based on for instance STM32F103 all run the bootloader + from flash, since there is no USB bootloader in ROM. +

+

+ For instance STM32F107, STM32F2xx and STM32F4xx devices have a + DfuSe bootloader in ROM, so the flash can be erased while + keeping the device available for USB DFU transfers as long + as the device designers use this built-in bootloader and have + not implemented another DfuSe bootloader in flash that the user is + dependent upon. +

+

+ Well-written bootloaders running from flash will report their + own memory region as read-only and not eraseable, but this does + not prevent dfu-util from sending a "unprotect" or "mass-erase" + request which overrides this, if the user insists. +

+

Example usage

+

+ Flashing a .dfu (special DfuSe format) file to the device: +

+
+         $ dfu-util -a 0 -D /path/to/dfuse-image.dfu
+	
+

+ Reading out 1 KB of flash starting at address 0x8000000: +

+
+         $ dfu-util -a 0 -s 0x08000000:1024 -U newfile.bin
+	
+

+ Flashing a binary file to address 0x8004000 of device memory and ask + the device to leave DFU mode: +

+
+         $ dfu-util -a 0 -s 0x08004000:leave -D /path/to/image.bin
+	
+

+ [Back to dfu-util main page] +

+ +
+ + diff --git a/tools/macosx/src/dfu-util/www/index.html b/tools/macosx/src/dfu-util/www/index.html new file mode 100644 index 0000000..108ddaf --- /dev/null +++ b/tools/macosx/src/dfu-util/www/index.html @@ -0,0 +1,119 @@ + + + + + + dfu-util Homepage + + + + + + + + + +
+

dfu-util - Device Firmware Upgrade Utilities

+

Description

+

+ dfu-util is a host side implementation of the DFU 1.0 and DFU 1.1 specifications of the USB forum. + + DFU is intended to download and upload firmware to/from devices connected + over USB. It ranges from small devices like micro-controller boards + to mobile phones. Using dfu-util you can download firmware to your + DFU-enabled device or upload firmware from it. dfu-util has been + tested with the Openmoko Neo1973 and Freerunner and many other devices. +

+

+ See the manual page for examples of use. +

+

Supported Devices

+ +

Releases

+

+ Releases of the dfu-util software can be found in the + releases folder. + The current release is 0.8. +

+

+ We offer binaries for Microsoft Windows and some other platforms. + dfu-util uses libusb 1.0 to access your device, so + on Windows you have to register the device with the WinUSB driver + (alternatively libusb-win32 or libusbK), please see the libusbx wiki + for more details. +

+

+ Mac OS X users can also get dfu-util from Homebrew with "brew install dfu-util" or from MacPorts. +

+

+ Most Linux distributions ship dfu-util in binary packages for those + who do not want to compile dfu-util from source. + On Debian, Ubuntu, Fedora and Gentoo you can install it through the + normal software package tools. For other distributions +(namely OpenSuSe, Mandriva, and CentOS) Holger Freyther was kind enough to +provide binary packages through the Open Build Service. +

+

Development

+

+ Development happens in a GIT repository. Browse it via the web +interface or clone it with: +

+
+	git clone git://gitorious.org/dfu-util/dfu-util.git
+	
+

+ See our build instructions for how to + build the source on different platforms. +

+

License

+

+ This software is licensed under the GPL version 2. +

+

Contact

+

+ If you have questions about the development or use of dfu-util please + send an e-mail to our dedicated +mailing list for dfu-util. +

+

People

+

+ dfu-util was originally written by + Harald Welte partially based on code from + dfu-programmer 0.4 and is currently maintained by Stefan Schmidt and + Tormod Volden. +

+ +
+ + diff --git a/tools/macosx/src/dfu-util/www/simple.css b/tools/macosx/src/dfu-util/www/simple.css new file mode 100644 index 0000000..98100bc --- /dev/null +++ b/tools/macosx/src/dfu-util/www/simple.css @@ -0,0 +1,56 @@ +body { + margin: 10px; + font-size: 0.82em; + background-color: #EEE; +} + +h1 { + clear: both; + padding: 0 0 12px 0; + margin: 0; + font-size: 2em; + font-weight: bold; +} + +h2 { + clear: both; + margin: 0; + font-size: 1.5em; + font-weight: normal; +} + +h3 { + clear: both; + margin: 15px 0 0 0; + font-size: 1.0em; + font-weight: bold; +} + +p { + line-height: 20px; + padding: 8px 0 8px 0; + margin: 0; + font-size: 1.1em; +} + +pre { + white-space: pre-wrap; + background-color: #CCC; + padding: 3px; +} + +a:hover { + background-color: #DDD; +} + +#middlebox { + width: 600px; + margin: 0px auto; + text-align: left; +} + +#footer { + height: 100px; + padding: 28px 3px 0 0; + margin: 20px 0 20px 0; +} diff --git a/tools/macosx/src/maple_loader/README.md b/tools/macosx/src/maple_loader/README.md new file mode 100644 index 0000000..c6c9379 --- /dev/null +++ b/tools/macosx/src/maple_loader/README.md @@ -0,0 +1,5 @@ +These files build the maple_loader.jar file used on Windows to reset the Sketch via USB Serial, so that the bootloader will run in dfu upload mode, ready for a new sketch to be uploaded + +The files were written by @bobC (github) and have been slightly modified by me (Roger Clark), so that dfu-util no longer attempts to reset the board after upload. +This change to dfu-util's reset command line argument, was required because dfu-util was showing errors on some Windows systems, because the bootloader had reset its self after upload, +before dfu-util had chance to tell it to reset. \ No newline at end of file diff --git a/tools/macosx/src/maple_loader/build.xml b/tools/macosx/src/maple_loader/build.xml new file mode 100644 index 0000000..80bdd6f --- /dev/null +++ b/tools/macosx/src/maple_loader/build.xml @@ -0,0 +1,73 @@ + + + + + + + + + + + Builds, tests, and runs the project maple_loader. + + + diff --git a/tools/macosx/src/maple_loader/build/built-jar.properties b/tools/macosx/src/maple_loader/build/built-jar.properties new file mode 100644 index 0000000..10752d5 --- /dev/null +++ b/tools/macosx/src/maple_loader/build/built-jar.properties @@ -0,0 +1,4 @@ +#Mon, 20 Jul 2015 11:21:26 +1000 + + +C\:\\Users\\rclark\\Desktop\\maple-asp-master\\installer\\maple_loader= diff --git a/tools/macosx/src/maple_loader/build/classes/CliTemplate/CliMain.class b/tools/macosx/src/maple_loader/build/classes/CliTemplate/CliMain.class new file mode 100644 index 0000000..37ee630 Binary files /dev/null and b/tools/macosx/src/maple_loader/build/classes/CliTemplate/CliMain.class differ diff --git a/tools/macosx/src/maple_loader/build/classes/CliTemplate/DFUUploader.class b/tools/macosx/src/maple_loader/build/classes/CliTemplate/DFUUploader.class new file mode 100644 index 0000000..77087b0 Binary files /dev/null and b/tools/macosx/src/maple_loader/build/classes/CliTemplate/DFUUploader.class differ diff --git a/tools/macosx/src/maple_loader/build/classes/CliTemplate/ExecCommand.class b/tools/macosx/src/maple_loader/build/classes/CliTemplate/ExecCommand.class new file mode 100644 index 0000000..ad95f79 Binary files /dev/null and b/tools/macosx/src/maple_loader/build/classes/CliTemplate/ExecCommand.class differ diff --git a/tools/macosx/src/maple_loader/build/classes/processing/app/Base.class b/tools/macosx/src/maple_loader/build/classes/processing/app/Base.class new file mode 100644 index 0000000..4aa0bde Binary files /dev/null and b/tools/macosx/src/maple_loader/build/classes/processing/app/Base.class differ diff --git a/tools/macosx/src/maple_loader/build/classes/processing/app/Preferences.class b/tools/macosx/src/maple_loader/build/classes/processing/app/Preferences.class new file mode 100644 index 0000000..89cf010 Binary files /dev/null and b/tools/macosx/src/maple_loader/build/classes/processing/app/Preferences.class differ diff --git a/tools/macosx/src/maple_loader/build/classes/processing/app/Serial.class b/tools/macosx/src/maple_loader/build/classes/processing/app/Serial.class new file mode 100644 index 0000000..cceccdd Binary files /dev/null and b/tools/macosx/src/maple_loader/build/classes/processing/app/Serial.class differ diff --git a/tools/macosx/src/maple_loader/build/classes/processing/app/SerialException.class b/tools/macosx/src/maple_loader/build/classes/processing/app/SerialException.class new file mode 100644 index 0000000..71048dd Binary files /dev/null and b/tools/macosx/src/maple_loader/build/classes/processing/app/SerialException.class differ diff --git a/tools/macosx/src/maple_loader/build/classes/processing/app/debug/MessageConsumer.class b/tools/macosx/src/maple_loader/build/classes/processing/app/debug/MessageConsumer.class new file mode 100644 index 0000000..37250e7 Binary files /dev/null and b/tools/macosx/src/maple_loader/build/classes/processing/app/debug/MessageConsumer.class differ diff --git a/tools/macosx/src/maple_loader/build/classes/processing/app/debug/MessageSiphon.class b/tools/macosx/src/maple_loader/build/classes/processing/app/debug/MessageSiphon.class new file mode 100644 index 0000000..e22c8d4 Binary files /dev/null and b/tools/macosx/src/maple_loader/build/classes/processing/app/debug/MessageSiphon.class differ diff --git a/tools/macosx/src/maple_loader/build/classes/processing/app/debug/RunnerException.class b/tools/macosx/src/maple_loader/build/classes/processing/app/debug/RunnerException.class new file mode 100644 index 0000000..710f796 Binary files /dev/null and b/tools/macosx/src/maple_loader/build/classes/processing/app/debug/RunnerException.class differ diff --git a/tools/macosx/src/maple_loader/build/classes/processing/app/helpers/ProcessUtils.class b/tools/macosx/src/maple_loader/build/classes/processing/app/helpers/ProcessUtils.class new file mode 100644 index 0000000..27eca62 Binary files /dev/null and b/tools/macosx/src/maple_loader/build/classes/processing/app/helpers/ProcessUtils.class differ diff --git a/tools/macosx/src/maple_loader/dist/README.TXT b/tools/macosx/src/maple_loader/dist/README.TXT new file mode 100644 index 0000000..255b89c --- /dev/null +++ b/tools/macosx/src/maple_loader/dist/README.TXT @@ -0,0 +1,32 @@ +======================== +BUILD OUTPUT DESCRIPTION +======================== + +When you build an Java application project that has a main class, the IDE +automatically copies all of the JAR +files on the projects classpath to your projects dist/lib folder. The IDE +also adds each of the JAR files to the Class-Path element in the application +JAR files manifest file (MANIFEST.MF). + +To run the project from the command line, go to the dist folder and +type the following: + +java -jar "maple_loader.jar" + +To distribute this project, zip up the dist folder (including the lib folder) +and distribute the ZIP file. + +Notes: + +* If two JAR files on the project classpath have the same name, only the first +JAR file is copied to the lib folder. +* Only JAR files are copied to the lib folder. +If the classpath contains other types of files or folders, these files (folders) +are not copied. +* If a library on the projects classpath also has a Class-Path element +specified in the manifest,the content of the Class-Path element has to be on +the projects runtime path. +* To set a main class in a standard Java project, right-click the project node +in the Projects window and choose Properties. Then click Run and enter the +class name in the Main Class field. Alternatively, you can manually type the +class name in the manifest Main-Class element. diff --git a/tools/macosx/src/maple_loader/dist/lib/jssc.jar b/tools/macosx/src/maple_loader/dist/lib/jssc.jar new file mode 100644 index 0000000..eb74f15 Binary files /dev/null and b/tools/macosx/src/maple_loader/dist/lib/jssc.jar differ diff --git a/tools/macosx/src/maple_loader/dist/maple_loader.jar b/tools/macosx/src/maple_loader/dist/maple_loader.jar new file mode 100644 index 0000000..e1f9965 Binary files /dev/null and b/tools/macosx/src/maple_loader/dist/maple_loader.jar differ diff --git a/tools/macosx/src/maple_loader/jars/jssc.jar b/tools/macosx/src/maple_loader/jars/jssc.jar new file mode 100644 index 0000000..eb74f15 Binary files /dev/null and b/tools/macosx/src/maple_loader/jars/jssc.jar differ diff --git a/tools/macosx/src/maple_loader/manifest.mf b/tools/macosx/src/maple_loader/manifest.mf new file mode 100644 index 0000000..328e8e5 --- /dev/null +++ b/tools/macosx/src/maple_loader/manifest.mf @@ -0,0 +1,3 @@ +Manifest-Version: 1.0 +X-COMMENT: Main-Class will be added automatically by build + diff --git a/tools/macosx/src/maple_loader/nbproject/build-impl.xml b/tools/macosx/src/maple_loader/nbproject/build-impl.xml new file mode 100644 index 0000000..a66f349 --- /dev/null +++ b/tools/macosx/src/maple_loader/nbproject/build-impl.xml @@ -0,0 +1,1413 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Must set src.dir + Must set test.src.dir + Must set build.dir + Must set dist.dir + Must set build.classes.dir + Must set dist.javadoc.dir + Must set build.test.classes.dir + Must set build.test.results.dir + Must set build.classes.excludes + Must set dist.jar + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Must set javac.includes + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + No tests executed. + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Must set JVM to use for profiling in profiler.info.jvm + Must set profiler agent JVM arguments in profiler.info.jvmargs.agent + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Must select some files in the IDE or set javac.includes + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + To run this application from the command line without Ant, try: + + java -jar "${dist.jar.resolved}" + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Must select one file in the IDE or set run.class + + + + Must select one file in the IDE or set run.class + + + + + + + + + + + + + + + + + + + + + + + Must select one file in the IDE or set debug.class + + + + + Must select one file in the IDE or set debug.class + + + + + Must set fix.includes + + + + + + + + + + This target only works when run from inside the NetBeans IDE. + + + + + + + + + Must select one file in the IDE or set profile.class + This target only works when run from inside the NetBeans IDE. + + + + + + + + + This target only works when run from inside the NetBeans IDE. + + + + + + + + + + + + + This target only works when run from inside the NetBeans IDE. + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Must select one file in the IDE or set run.class + + + + + + Must select some files in the IDE or set test.includes + + + + + Must select one file in the IDE or set run.class + + + + + Must select one file in the IDE or set applet.url + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Must select some files in the IDE or set javac.includes + + + + + + + + + + + + + + + + + + + + Some tests failed; see details above. + + + + + + + + + Must select some files in the IDE or set test.includes + + + + Some tests failed; see details above. + + + + Must select some files in the IDE or set test.class + Must select some method in the IDE or set test.method + + + + Some tests failed; see details above. + + + + + Must select one file in the IDE or set test.class + + + + Must select one file in the IDE or set test.class + Must select some method in the IDE or set test.method + + + + + + + + + + + + + + Must select one file in the IDE or set applet.url + + + + + + + + + Must select one file in the IDE or set applet.url + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/tools/macosx/src/maple_loader/nbproject/genfiles.properties b/tools/macosx/src/maple_loader/nbproject/genfiles.properties new file mode 100644 index 0000000..c136721 --- /dev/null +++ b/tools/macosx/src/maple_loader/nbproject/genfiles.properties @@ -0,0 +1,8 @@ +build.xml.data.CRC32=2e6a03ba +build.xml.script.CRC32=4676ee6b +build.xml.stylesheet.CRC32=8064a381@1.75.2.48 +# This file is used by a NetBeans-based IDE to track changes in generated files such as build-impl.xml. +# Do not edit this file. You may delete it but then the IDE will never regenerate such files for you. +nbproject/build-impl.xml.data.CRC32=2e6a03ba +nbproject/build-impl.xml.script.CRC32=392b3f79 +nbproject/build-impl.xml.stylesheet.CRC32=876e7a8f@1.75.2.48 diff --git a/tools/macosx/src/maple_loader/nbproject/private/config.properties b/tools/macosx/src/maple_loader/nbproject/private/config.properties new file mode 100644 index 0000000..e69de29 diff --git a/tools/macosx/src/maple_loader/nbproject/private/private.properties b/tools/macosx/src/maple_loader/nbproject/private/private.properties new file mode 100644 index 0000000..e5c9f10 --- /dev/null +++ b/tools/macosx/src/maple_loader/nbproject/private/private.properties @@ -0,0 +1,6 @@ +compile.on.save=true +do.depend=false +do.jar=true +javac.debug=true +javadoc.preview=true +user.properties.file=C:\\Users\\rclark\\AppData\\Roaming\\NetBeans\\8.0.2\\build.properties diff --git a/tools/macosx/src/maple_loader/nbproject/private/private.xml b/tools/macosx/src/maple_loader/nbproject/private/private.xml new file mode 100644 index 0000000..a1bbd60 --- /dev/null +++ b/tools/macosx/src/maple_loader/nbproject/private/private.xml @@ -0,0 +1,10 @@ + + + + + + file:/C:/Users/rclark/Desktop/maple-asp-master/installer/maple_loader/src/CliTemplate/CliMain.java + file:/C:/Users/rclark/Desktop/maple-asp-master/installer/maple_loader/src/CliTemplate/DFUUploader.java + + + diff --git a/tools/macosx/src/maple_loader/nbproject/project.properties b/tools/macosx/src/maple_loader/nbproject/project.properties new file mode 100644 index 0000000..7f48d71 --- /dev/null +++ b/tools/macosx/src/maple_loader/nbproject/project.properties @@ -0,0 +1,79 @@ +annotation.processing.enabled=true +annotation.processing.enabled.in.editor=false +annotation.processing.processors.list= +annotation.processing.run.all.processors=true +annotation.processing.source.output=${build.generated.sources.dir}/ap-source-output +application.title=maple_loader +application.vendor=bob +build.classes.dir=${build.dir}/classes +build.classes.excludes=**/*.java,**/*.form +# This directory is removed when the project is cleaned: +build.dir=build +build.generated.dir=${build.dir}/generated +build.generated.sources.dir=${build.dir}/generated-sources +# Only compile against the classpath explicitly listed here: +build.sysclasspath=ignore +build.test.classes.dir=${build.dir}/test/classes +build.test.results.dir=${build.dir}/test/results +# Uncomment to specify the preferred debugger connection transport: +#debug.transport=dt_socket +debug.classpath=\ + ${run.classpath} +debug.test.classpath=\ + ${run.test.classpath} +# Files in build.classes.dir which should be excluded from distribution jar +dist.archive.excludes= +# This directory is removed when the project is cleaned: +dist.dir=dist +dist.jar=${dist.dir}/maple_loader.jar +dist.javadoc.dir=${dist.dir}/javadoc +endorsed.classpath= +excludes= +file.reference.jssc.jar=dist/lib/jssc.jar +file.reference.jssc.jar-1=jars/jssc.jar +includes=** +jar.compress=false +javac.classpath=\ + ${file.reference.jssc.jar}:\ + ${file.reference.jssc.jar-1} +# Space-separated list of extra javac options +javac.compilerargs= +javac.deprecation=false +javac.processorpath=\ + ${javac.classpath} +javac.source=1.7 +javac.target=1.7 +javac.test.classpath=\ + ${javac.classpath}:\ + ${build.classes.dir} +javac.test.processorpath=\ + ${javac.test.classpath} +javadoc.additionalparam= +javadoc.author=false +javadoc.encoding=${source.encoding} +javadoc.noindex=false +javadoc.nonavbar=false +javadoc.notree=false +javadoc.private=false +javadoc.splitindex=true +javadoc.use=true +javadoc.version=false +javadoc.windowtitle= +main.class=CliTemplate.CliMain +manifest.file=manifest.mf +meta.inf.dir=${src.dir}/META-INF +mkdist.disabled=false +platform.active=default_platform +run.classpath=\ + ${javac.classpath}:\ + ${build.classes.dir} +# Space-separated list of JVM arguments used when running the project. +# You may also define separate properties like run-sys-prop.name=value instead of -Dname=value. +# To set system properties for unit tests define test-sys-prop.name=value: +run.jvmargs= +run.test.classpath=\ + ${javac.test.classpath}:\ + ${build.test.classes.dir} +source.encoding=UTF-8 +src.dir=src +test.src.dir=test diff --git a/tools/macosx/src/maple_loader/nbproject/project.xml b/tools/macosx/src/maple_loader/nbproject/project.xml new file mode 100644 index 0000000..92218a9 --- /dev/null +++ b/tools/macosx/src/maple_loader/nbproject/project.xml @@ -0,0 +1,15 @@ + + + org.netbeans.modules.java.j2seproject + + + maple_loader + + + + + + + + + diff --git a/tools/macosx/src/maple_loader/src/CliTemplate/CliMain.java b/tools/macosx/src/maple_loader/src/CliTemplate/CliMain.java new file mode 100644 index 0000000..c7dc9f0 --- /dev/null +++ b/tools/macosx/src/maple_loader/src/CliTemplate/CliMain.java @@ -0,0 +1,60 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +package CliTemplate; + +import java.io.IOException; +import java.text.MessageFormat; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import processing.app.Preferences; + +//import processing.app.I18n; +import processing.app.helpers.ProcessUtils; + +/** + * + * @author cousinr + */ +public class CliMain { + + + /** + * @param args the command line arguments + */ + public static void main(String[] args) { + + String comPort = args[0]; // + String altIf = args[1]; // + String usbID = args[2]; // "1EAF:0003"; + String binFile = args[3]; // bin file + + System.out.println("maple_loader v0.1"); + + Preferences.set ("serial.port",comPort); + Preferences.set ("serial.parity","N"); + Preferences.setInteger ("serial.databits", 8); + Preferences.setInteger ("serial.debug_rate",9600); + Preferences.setInteger ("serial.stopbits",1); + + Preferences.setInteger ("programDelay",1200); + + Preferences.set ("upload.usbID", usbID); + Preferences.set ("upload.altID", altIf); + Preferences.setBoolean ("upload.auto_reset", true); + Preferences.setBoolean ("upload.verbose", false); + + // + DFUUploader dfuUploader = new DFUUploader(); + try { + //dfuUploader.uploadViaDFU(binFile); + dfuUploader.uploadViaDFU(binFile); + } catch (Exception e) + { + System.err.print (MessageFormat.format("an error occurred! {0}\n", e.getMessage())); + } + } +} diff --git a/tools/macosx/src/maple_loader/src/CliTemplate/DFUUploader.java b/tools/macosx/src/maple_loader/src/CliTemplate/DFUUploader.java new file mode 100644 index 0000000..3dee0b4 --- /dev/null +++ b/tools/macosx/src/maple_loader/src/CliTemplate/DFUUploader.java @@ -0,0 +1,345 @@ +/* + DFUUploader - uploader implementation using DFU + + Copyright (c) 2010 + Andrew Meyer + + 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 + +*/ + +package CliTemplate; + +import java.io.BufferedReader; +import java.io.File; +import java.io.InputStreamReader; +import java.util.ArrayList; +import java.util.Collection; +import java.util.List; +import processing.app.Preferences; +import processing.app.Serial; +import processing.app.debug.MessageConsumer; +import processing.app.debug.MessageSiphon; +import processing.app.debug.RunnerException; + +/** + * + * @author bob + */ +public class DFUUploader implements MessageConsumer { + + boolean firstErrorFound; + boolean secondErrorFound; + // part of the PdeMessageConsumer interface + boolean notFoundError; + boolean verbose; + RunnerException exception; + + static final String SUPER_BADNESS = + "Compiler error!"; + + public boolean uploadUsingPreferences(String binPath, boolean verbose) + throws RunnerException { + + this.verbose = verbose; + + return uploadViaDFU(binPath); + } + + // works with old and new versions of dfu-util + private boolean found_device (String dfuData, String usbID) + { + return dfuData.contains(("Found DFU: [0x"+usbID.substring(0,4)).toUpperCase()) || + dfuData.contains(("Found DFU: ["+usbID.substring(0,4)).toUpperCase()); + } + + public boolean uploadViaDFU (String binPath) + throws RunnerException { + + this.verbose = Preferences.getBoolean ("upload.verbose"); + + /* todo, check for size overruns! */ + String fileType="bin"; + + if (fileType.equals("bin")) { + String usbID = Preferences.get("upload.usbID"); + if (usbID == null) { + /* fall back on default */ + /* this isnt great because is default Avrdude or dfu-util? */ + usbID = Preferences.get("upload.usbID"); + } + + /* if auto-reset, then emit the reset pulse on dtr/rts */ + if (Preferences.get("upload.auto_reset") != null) { + if (Preferences.get("upload.auto_reset").toLowerCase().equals("true")) { + System.out.println("Resetting to bootloader via DTR pulse"); + emitResetPulse(); + } + } else { + System.out.println("Resetting to bootloader via DTR pulse"); + emitResetPulse(); + } + + String dfuList = new String(); + List commandCheck = new ArrayList(); + commandCheck.add("dfu-util"); + commandCheck.add("-l"); + long startChecking = System.currentTimeMillis(); + System.out.println("Searching for DFU device [" + usbID + "]..."); + do { + try { + Thread.sleep(100); + } catch (InterruptedException e) {} + dfuList = executeCheckCommand(commandCheck); + //System.out.println(dfuList); + } + while ( !found_device (dfuList.toUpperCase(), usbID) && (System.currentTimeMillis() - startChecking < 7000)); + + if ( !found_device (dfuList.toUpperCase(), usbID) ) + { + System.out.println(dfuList); + System.err.println("Couldn't find the DFU device: [" + usbID + "]"); + return false; + } + System.out.println("Found it!"); + + /* todo, add handle to let user choose altIf at upload time! */ + String altIf = Preferences.get("upload.altID"); + + List commandDownloader = new ArrayList(); + commandDownloader.add("dfu-util"); + commandDownloader.add("-a "+altIf); + commandDownloader.add("-R"); + commandDownloader.add("-d "+usbID); + commandDownloader.add("-D"+ binPath); //"./thisbin.bin"); + + return executeUploadCommand(commandDownloader); + } + + System.err.println("Only .bin files are supported at this time"); + return false; + } + + /* we need to ensure both RTS and DTR are low to start, + then pulse DTR on its own. This is the reset signal + maple responds to + */ + private void emitResetPulse() throws RunnerException { + + /* wait a while for the device to reboot */ + int programDelay = Preferences.getInteger("programDelay"); + + try { + Serial serialPort = new Serial(); + + // try to toggle DTR/RTS (old scheme) + serialPort.setRTS(false); + serialPort.setDTR(false); + serialPort.setDTR(true); + try { + Thread.sleep(50); + } catch (InterruptedException e) {} + serialPort.setDTR(false); + + // try magic number + serialPort.setRTS(true); + serialPort.setDTR(true); + try { + Thread.sleep(50); + } catch (InterruptedException e) {} + serialPort.setDTR(false); + try { + Thread.sleep(50); + } catch (InterruptedException e) {} + serialPort.write("1EAF"); + try { + Thread.sleep(50); + } catch (InterruptedException e) {} + serialPort.dispose(); + + } catch(Exception e) { + System.err.println("Reset via USB Serial Failed! Did you select the right serial port?\nAssuming the board is in perpetual bootloader mode and continuing to attempt dfu programming...\n"); + } + } + + protected String executeCheckCommand(Collection commandDownloader) + throws RunnerException + { + firstErrorFound = false; // haven't found any errors yet + secondErrorFound = false; + notFoundError = false; + int result=0; // pre-initialized to quiet a bogus warning from jikes + + String userdir = System.getProperty("user.dir") + File.separator; + String returnStr = new String(); + + try { + String[] commandArray = new String[commandDownloader.size()]; + commandDownloader.toArray(commandArray); + + String armBasePath; + + //armBasePath = new String(Base.getHardwarePath() + "/tools/arm/bin/"); + armBasePath = ""; + + commandArray[0] = armBasePath + commandArray[0]; + + if (verbose || Preferences.getBoolean("upload.verbose")) { + for(int i = 0; i < commandArray.length; i++) { + System.out.print(commandArray[i] + " "); + } + System.out.println(); + } + + Process process = Runtime.getRuntime().exec(commandArray); + BufferedReader stdInput = new BufferedReader(new + InputStreamReader(process.getInputStream())); + BufferedReader stdError = new BufferedReader(new + InputStreamReader(process.getErrorStream())); + + // wait for the process to finish. if interrupted + // before waitFor returns, continue waiting + // + boolean busy = true; + while (busy) { + try { + result = process.waitFor(); + busy = false; + } catch (InterruptedException intExc) { + } + } + + String s; + while ((s = stdInput.readLine()) != null) { + returnStr += s + "\n"; + } + + process.destroy(); + + if(exception!=null) { + exception.hideStackTrace(); + throw exception; + } + if(result!=0) return "Error!"; + } catch (Exception e) { + e.printStackTrace(); + } + //System.out.println("result2 is "+result); + // if the result isn't a known, expected value it means that something + // is fairly wrong, one possibility is that jikes has crashed. + // + if (exception != null) throw exception; + + if ((result != 0) && (result != 1 )) { + exception = new RunnerException(SUPER_BADNESS); + } + + return returnStr; // ? true : false; + + } + + // Need to overload this from Uploader to use the system-wide dfu-util + protected boolean executeUploadCommand(Collection commandDownloader) + throws RunnerException + { + firstErrorFound = false; // haven't found any errors yet + secondErrorFound = false; + notFoundError = false; + int result=0; // pre-initialized to quiet a bogus warning from jikes + + String userdir = System.getProperty("user.dir") + File.separator; + + try { + String[] commandArray = new String[commandDownloader.size()]; + commandDownloader.toArray(commandArray); + + String armBasePath; + + //armBasePath = new String(Base.getHardwarePath() + "/tools/arm/bin/"); + armBasePath = ""; + + commandArray[0] = armBasePath + commandArray[0]; + + if (verbose || Preferences.getBoolean("upload.verbose")) { + for(int i = 0; i < commandArray.length; i++) { + System.out.print(commandArray[i] + " "); + } + System.out.println(); + } + + Process process = Runtime.getRuntime().exec(commandArray); + new MessageSiphon(process.getInputStream(), this); + new MessageSiphon(process.getErrorStream(), this); + + // wait for the process to finish. if interrupted + // before waitFor returns, continue waiting + // + boolean compiling = true; + while (compiling) { + try { + result = process.waitFor(); + compiling = false; + } catch (InterruptedException intExc) { + } + } + if(exception!=null) { + exception.hideStackTrace(); + throw exception; + } + if(result!=0) + return false; + } catch (Exception e) { + e.printStackTrace(); + } + //System.out.println("result2 is "+result); + // if the result isn't a known, expected value it means that something + // is fairly wrong, one possibility is that jikes has crashed. + // + if (exception != null) throw exception; + + if ((result != 0) && (result != 1 )) { + exception = new RunnerException(SUPER_BADNESS); + //editor.error(exception); + //PdeBase.openURL(BUGS_URL); + //throw new PdeException(SUPER_BADNESS); + } + + return (result == 0); // ? true : false; + + } + + // deal with messages from dfu-util... + public void message(String s) { + + if(s.indexOf("dfu-util - (C) ") != -1) { return; } + if(s.indexOf("This program is Free Software and has ABSOLUTELY NO WARRANTY") != -1) { return; } + + if(s.indexOf("No DFU capable USB device found") != -1) { + System.err.print(s); + exception = new RunnerException("Problem uploading via dfu-util: No Maple found"); + return; + } + + if(s.indexOf("Operation not perimitted") != -1) { + System.err.print(s); + exception = new RunnerException("Problem uploading via dfu-util: Insufficient privilages"); + return; + } + + // else just print everything... + System.out.print(s); + } + +} diff --git a/tools/macosx/src/maple_loader/src/CliTemplate/ExecCommand.java b/tools/macosx/src/maple_loader/src/CliTemplate/ExecCommand.java new file mode 100644 index 0000000..3d6c106 --- /dev/null +++ b/tools/macosx/src/maple_loader/src/CliTemplate/ExecCommand.java @@ -0,0 +1,119 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +package CliTemplate; + +import java.io.IOException; +import java.text.MessageFormat; +import java.util.ArrayList; +import java.util.List; + +import processing.app.debug.MessageConsumer; +import processing.app.debug.MessageSiphon; +import processing.app.debug.RunnerException; +import processing.app.helpers.ProcessUtils; + +/** + * + * @author cousinr + */ +public class ExecCommand implements MessageConsumer { + + private boolean verbose = true; + private boolean firstErrorFound; + private boolean secondErrorFound; + private RunnerException exception; + + /** + * Either succeeds or throws a RunnerException fit for public consumption. + * + * @param command + * @throws RunnerException + */ + public void execAsynchronously(String[] command) throws RunnerException { + + // eliminate any empty array entries + List stringList = new ArrayList<>(); + for (String string : command) { + string = string.trim(); + if (string.length() != 0) + stringList.add(string); + } + command = stringList.toArray(new String[stringList.size()]); + if (command.length == 0) + return; + int result = 0; + + if (verbose) { + for (String c : command) + System.out.print(c + " "); + System.out.println(); + } + + firstErrorFound = false; // haven't found any errors yet + secondErrorFound = false; + + Process process; + try { + process = ProcessUtils.exec(command); + } catch (IOException e) { + RunnerException re = new RunnerException(e.getMessage()); + re.hideStackTrace(); + throw re; + } + + MessageSiphon in = new MessageSiphon(process.getInputStream(), this); + MessageSiphon err = new MessageSiphon(process.getErrorStream(), this); + + // wait for the process to finish. if interrupted + // before waitFor returns, continue waiting + boolean compiling = true; + while (compiling) { + try { + in.join(); + err.join(); + result = process.waitFor(); + //System.out.println("result is " + result); + compiling = false; + } catch (InterruptedException ignored) { } + } + + // an error was queued up by message(), barf this back to compile(), + // which will barf it back to Editor. if you're having trouble + // discerning the imagery, consider how cows regurgitate their food + // to digest it, and the fact that they have five stomaches. + // + //System.out.println("throwing up " + exception); + if (exception != null) + throw exception; + + if (result > 1) { + // a failure in the tool (e.g. unable to locate a sub-executable) + System.err.println(MessageFormat.format("{0} returned {1}", command[0], result)); + } + + if (result != 0) { + RunnerException re = new RunnerException(MessageFormat.format("exit code: {0}", result)); + re.hideStackTrace(); + throw re; + } + } + + /** + * Part of the MessageConsumer interface, this is called + * whenever a piece (usually a line) of error message is spewed + * out from the compiler. The errors are parsed for their contents + * and line number, which is then reported back to Editor. + * @param s + */ + @Override + public void message(String s) { + int i; + + + System.err.print(s); + } + +} diff --git a/tools/macosx/src/maple_loader/src/processing/app/Base.java b/tools/macosx/src/maple_loader/src/processing/app/Base.java new file mode 100644 index 0000000..c3a174d --- /dev/null +++ b/tools/macosx/src/maple_loader/src/processing/app/Base.java @@ -0,0 +1,53 @@ +/* -*- mode: java; c-basic-offset: 2; indent-tabs-mode: nil -*- */ + +/* + Part of the Processing project - http://processing.org + + Copyright (c) 2004-10 Ben Fry and Casey Reas + Copyright (c) 2001-04 Massachusetts Institute of Technology + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License version 2 + as published by the Free Software Foundation. + + 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 +*/ + +package processing.app; + + +/** + * The base class for the main processing application. + * Primary role of this class is for platform identification and + * general interaction with the system (launching URLs, loading + * files and images, etc) that comes from that. + */ +public class Base { + + /** + * returns true if running on windows. + */ + static public boolean isWindows() { + //return PApplet.platform == PConstants.WINDOWS; + return System.getProperty("os.name").indexOf("Windows") != -1; + } + + + /** + * true if running on linux. + */ + static public boolean isLinux() { + //return PApplet.platform == PConstants.LINUX; + return System.getProperty("os.name").indexOf("Linux") != -1; + } + + + +} diff --git a/tools/macosx/src/maple_loader/src/processing/app/Preferences.java b/tools/macosx/src/maple_loader/src/processing/app/Preferences.java new file mode 100644 index 0000000..6368e38 --- /dev/null +++ b/tools/macosx/src/maple_loader/src/processing/app/Preferences.java @@ -0,0 +1,157 @@ +/* -*- mode: java; c-basic-offset: 2; indent-tabs-mode: nil -*- */ + +/* + Part of the Processing project - http://processing.org + + Copyright (c) 2004-09 Ben Fry and Casey Reas + Copyright (c) 2001-04 Massachusetts Institute of Technology + + 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 +*/ + +package processing.app; + +import java.io.*; +import java.util.*; + + +/** + * Storage class for user preferences and environment settings. + *

+ * This class no longer uses the Properties class, since + * properties files are iso8859-1, which is highly likely to + * be a problem when trying to save sketch folders and locations. + *

+ * The GUI portion in here is really ugly, as it uses exact layout. This was + * done in frustration one evening (and pre-Swing), but that's long since past, + * and it should all be moved to a proper swing layout like BoxLayout. + *

+ * This is very poorly put together, that the preferences panel and the actual + * preferences i/o is part of the same code. But there hasn't yet been a + * compelling reason to bother with the separation aside from concern about + * being lectured by strangers who feel that it doesn't look like what they + * learned in CS class. + *

+ * Would also be possible to change this to use the Java Preferences API. + * Some useful articles + * here and + * here. + * However, haven't implemented this yet for lack of time, but more + * importantly, because it would entail writing to the registry (on Windows), + * or an obscure file location (on Mac OS X) and make it far more difficult to + * find the preferences to tweak them by hand (no! stay out of regedit!) + * or to reset the preferences by simply deleting the preferences.txt file. + */ +public class Preferences { + + // what to call the feller + + static final String PREFS_FILE = "preferences.txt"; + + + // prompt text stuff + + static final String PROMPT_YES = "Yes"; + static final String PROMPT_NO = "No"; + static final String PROMPT_CANCEL = "Cancel"; + static final String PROMPT_OK = "OK"; + static final String PROMPT_BROWSE = "Browse"; + + /** + * Standardized width for buttons. Mac OS X 10.3 wants 70 as its default, + * Windows XP needs 66, and my Ubuntu machine needs 80+, so 80 seems proper. + */ + static public int BUTTON_WIDTH = 80; + + /** + * Standardized button height. Mac OS X 10.3 (Java 1.4) wants 29, + * presumably because it now includes the blue border, where it didn't + * in Java 1.3. Windows XP only wants 23 (not sure what default Linux + * would be). Because of the disparity, on Mac OS X, it will be set + * inside a static block. + */ + static public int BUTTON_HEIGHT = 24; + + // value for the size bars, buttons, etc + + static final int GRID_SIZE = 33; + + + // indents and spacing standards. these probably need to be modified + // per platform as well, since macosx is so huge, windows is smaller, + // and linux is all over the map + + static final int GUI_BIG = 13; + static final int GUI_BETWEEN = 10; + static final int GUI_SMALL = 6; + + + + // data model + + static Hashtable table = new Hashtable();; + static File preferencesFile; + + + static protected void init(String commandLinePrefs) { + + + } + + + public Preferences() { + + } + + // ................................................................. + + // ................................................................. + + // ................................................................. + + // ................................................................. + + + + static public String get(String attribute) { + return (String) table.get(attribute); + } + + static public void set(String attribute, String value) { + table.put(attribute, value); + } + + + static public boolean getBoolean(String attribute) { + String value = get(attribute); + return (new Boolean(value)).booleanValue(); + } + + + static public void setBoolean(String attribute, boolean value) { + set(attribute, value ? "true" : "false"); + } + + + static public int getInteger(String attribute) { + return Integer.parseInt(get(attribute)); + } + + + static public void setInteger(String key, int value) { + set(key, String.valueOf(value)); + } + +} diff --git a/tools/macosx/src/maple_loader/src/processing/app/Serial.java b/tools/macosx/src/maple_loader/src/processing/app/Serial.java new file mode 100644 index 0000000..04566a7 --- /dev/null +++ b/tools/macosx/src/maple_loader/src/processing/app/Serial.java @@ -0,0 +1,527 @@ +/* -*- mode: jde; c-basic-offset: 2; indent-tabs-mode: nil -*- */ + +/* + PSerial - class for serial port goodness + Part of the Processing project - http://processing.org + + Copyright (c) 2004 Ben Fry & Casey Reas + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + This library 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 + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General + Public License along with this library; if not, write to the + Free Software Foundation, Inc., 59 Temple Place, Suite 330, + Boston, MA 02111-1307 USA +*/ + +package processing.app; +//import processing.core.*; + + +import java.io.*; +import java.text.MessageFormat; +import java.util.*; +import jssc.SerialPort; +import jssc.SerialPortEvent; +import jssc.SerialPortEventListener; +import jssc.SerialPortException; +import jssc.SerialPortList; +import processing.app.debug.MessageConsumer; + + +public class Serial implements SerialPortEventListener { + + //PApplet parent; + + // properties can be passed in for default values + // otherwise defaults to 9600 N81 + + // these could be made static, which might be a solution + // for the classloading problem.. because if code ran again, + // the static class would have an object that could be closed + + SerialPort port; + + int rate; + int parity; + int databits; + int stopbits; + boolean monitor = false; + + // read buffer and streams + + InputStream input; + OutputStream output; + + byte buffer[] = new byte[32768]; + int bufferIndex; + int bufferLast; + + MessageConsumer consumer; + + public Serial(boolean monitor) throws SerialException { + this(Preferences.get("serial.port"), + Preferences.getInteger("serial.debug_rate"), + Preferences.get("serial.parity").charAt(0), + Preferences.getInteger("serial.databits"), + new Float(Preferences.get("serial.stopbits")).floatValue()); + this.monitor = monitor; + } + + public Serial() throws SerialException { + this(Preferences.get("serial.port"), + Preferences.getInteger("serial.debug_rate"), + Preferences.get("serial.parity").charAt(0), + Preferences.getInteger("serial.databits"), + new Float(Preferences.get("serial.stopbits")).floatValue()); + } + + public Serial(int irate) throws SerialException { + this(Preferences.get("serial.port"), irate, + Preferences.get("serial.parity").charAt(0), + Preferences.getInteger("serial.databits"), + new Float(Preferences.get("serial.stopbits")).floatValue()); + } + + public Serial(String iname, int irate) throws SerialException { + this(iname, irate, Preferences.get("serial.parity").charAt(0), + Preferences.getInteger("serial.databits"), + new Float(Preferences.get("serial.stopbits")).floatValue()); + } + + public Serial(String iname) throws SerialException { + this(iname, Preferences.getInteger("serial.debug_rate"), + Preferences.get("serial.parity").charAt(0), + Preferences.getInteger("serial.databits"), + new Float(Preferences.get("serial.stopbits")).floatValue()); + } + + public Serial(String iname, int irate, + char iparity, int idatabits, float istopbits) + throws SerialException { + //if (port != null) port.close(); + //this.parent = parent; + //parent.attach(this); + + this.rate = irate; + + parity = SerialPort.PARITY_NONE; + if (iparity == 'E') parity = SerialPort.PARITY_EVEN; + if (iparity == 'O') parity = SerialPort.PARITY_ODD; + + this.databits = idatabits; + + stopbits = SerialPort.STOPBITS_1; + if (istopbits == 1.5f) stopbits = SerialPort.STOPBITS_1_5; + if (istopbits == 2) stopbits = SerialPort.STOPBITS_2; + + try { + port = new SerialPort(iname); + port.openPort(); + port.setParams(rate, databits, stopbits, parity, true, true); + port.addEventListener(this); + } catch (Exception e) { + throw new SerialException(MessageFormat.format("Error opening serial port ''{0}''.", iname), e); + } + + if (port == null) { + throw new SerialException("Serial port '" + iname + "' not found. Did you select the right one from the Tools > Serial Port menu?"); + } + } + + + public void setup() { + //parent.registerCall(this, DISPOSE); + } + + public void dispose() throws IOException { + if (port != null) { + try { + if (port.isOpened()) { + port.closePort(); // close the port + } + } catch (SerialPortException e) { + throw new IOException(e); + } finally { + port = null; + } + } + } + + public void addListener(MessageConsumer consumer) { + this.consumer = consumer; + } + + public synchronized void serialEvent(SerialPortEvent serialEvent) { + if (serialEvent.isRXCHAR()) { + try { + byte[] buf = port.readBytes(serialEvent.getEventValue()); + if (buf.length > 0) { + if (bufferLast == buffer.length) { + byte temp[] = new byte[bufferLast << 1]; + System.arraycopy(buffer, 0, temp, 0, bufferLast); + buffer = temp; + } + if (monitor) { + System.out.print(new String(buf)); + } + if (this.consumer != null) { + this.consumer.message(new String(buf)); + } + } + } catch (SerialPortException e) { + errorMessage("serialEvent", e); + } + } + } + + + /** + * Returns the number of bytes that have been read from serial + * and are waiting to be dealt with by the user. + */ + public synchronized int available() { + return (bufferLast - bufferIndex); + } + + + /** + * Ignore all the bytes read so far and empty the buffer. + */ + public synchronized void clear() { + bufferLast = 0; + bufferIndex = 0; + } + + + /** + * Returns a number between 0 and 255 for the next byte that's + * waiting in the buffer. + * Returns -1 if there was no byte (although the user should + * first check available() to see if things are ready to avoid this) + */ + public synchronized int read() { + if (bufferIndex == bufferLast) return -1; + + int outgoing = buffer[bufferIndex++] & 0xff; + if (bufferIndex == bufferLast) { // rewind + bufferIndex = 0; + bufferLast = 0; + } + return outgoing; + } + + + /** + * Returns the next byte in the buffer as a char. + * Returns -1, or 0xffff, if nothing is there. + */ + public synchronized char readChar() { + if (bufferIndex == bufferLast) return (char)(-1); + return (char) read(); + } + + + /** + * Return a byte array of anything that's in the serial buffer. + * Not particularly memory/speed efficient, because it creates + * a byte array on each read, but it's easier to use than + * readBytes(byte b[]) (see below). + */ + public synchronized byte[] readBytes() { + if (bufferIndex == bufferLast) return null; + + int length = bufferLast - bufferIndex; + byte outgoing[] = new byte[length]; + System.arraycopy(buffer, bufferIndex, outgoing, 0, length); + + bufferIndex = 0; // rewind + bufferLast = 0; + return outgoing; + } + + + /** + * Grab whatever is in the serial buffer, and stuff it into a + * byte buffer passed in by the user. This is more memory/time + * efficient than readBytes() returning a byte[] array. + *

+ * Returns an int for how many bytes were read. If more bytes + * are available than can fit into the byte array, only those + * that will fit are read. + */ + public synchronized int readBytes(byte outgoing[]) { + if (bufferIndex == bufferLast) return 0; + + int length = bufferLast - bufferIndex; + if (length > outgoing.length) length = outgoing.length; + System.arraycopy(buffer, bufferIndex, outgoing, 0, length); + + bufferIndex += length; + if (bufferIndex == bufferLast) { + bufferIndex = 0; // rewind + bufferLast = 0; + } + return length; + } + + + /** + * Reads from the serial port into a buffer of bytes up to and + * including a particular character. If the character isn't in + * the serial buffer, then 'null' is returned. + */ + public synchronized byte[] readBytesUntil(int interesting) { + if (bufferIndex == bufferLast) return null; + byte what = (byte)interesting; + + int found = -1; + for (int k = bufferIndex; k < bufferLast; k++) { + if (buffer[k] == what) { + found = k; + break; + } + } + if (found == -1) return null; + + int length = found - bufferIndex + 1; + byte outgoing[] = new byte[length]; + System.arraycopy(buffer, bufferIndex, outgoing, 0, length); + + bufferIndex = 0; // rewind + bufferLast = 0; + return outgoing; + } + + + /** + * Reads from the serial port into a buffer of bytes until a + * particular character. If the character isn't in the serial + * buffer, then 'null' is returned. + *

+ * If outgoing[] is not big enough, then -1 is returned, + * and an error message is printed on the console. + * If nothing is in the buffer, zero is returned. + * If 'interesting' byte is not in the buffer, then 0 is returned. + */ + public synchronized int readBytesUntil(int interesting, byte outgoing[]) { + if (bufferIndex == bufferLast) return 0; + byte what = (byte)interesting; + + int found = -1; + for (int k = bufferIndex; k < bufferLast; k++) { + if (buffer[k] == what) { + found = k; + break; + } + } + if (found == -1) return 0; + + int length = found - bufferIndex + 1; + if (length > outgoing.length) { + System.err.println("readBytesUntil() byte buffer is" + + " too small for the " + length + + " bytes up to and including char " + interesting); + return -1; + } + //byte outgoing[] = new byte[length]; + System.arraycopy(buffer, bufferIndex, outgoing, 0, length); + + bufferIndex += length; + if (bufferIndex == bufferLast) { + bufferIndex = 0; // rewind + bufferLast = 0; + } + return length; + } + + + /** + * Return whatever has been read from the serial port so far + * as a String. It assumes that the incoming characters are ASCII. + *

+ * If you want to move Unicode data, you can first convert the + * String to a byte stream in the representation of your choice + * (i.e. UTF8 or two-byte Unicode data), and send it as a byte array. + */ + public synchronized String readString() { + if (bufferIndex == bufferLast) return null; + return new String(readBytes()); + } + + + /** + * Combination of readBytesUntil and readString. See caveats in + * each function. Returns null if it still hasn't found what + * you're looking for. + *

+ * If you want to move Unicode data, you can first convert the + * String to a byte stream in the representation of your choice + * (i.e. UTF8 or two-byte Unicode data), and send it as a byte array. + */ + public synchronized String readStringUntil(int interesting) { + byte b[] = readBytesUntil(interesting); + if (b == null) return null; + return new String(b); + } + + + /** + * This will handle both ints, bytes and chars transparently. + */ + public void write(int what) { // will also cover char + try { + port.writeInt(what & 0xff); + } catch (SerialPortException e) { + errorMessage("write", e); + } + } + + + public void write(byte bytes[]) { + try { + port.writeBytes(bytes); + } catch (SerialPortException e) { + errorMessage("write", e); + } + } + + + /** + * Write a String to the output. Note that this doesn't account + * for Unicode (two bytes per char), nor will it send UTF8 + * characters.. It assumes that you mean to send a byte buffer + * (most often the case for networking and serial i/o) and + * will only use the bottom 8 bits of each char in the string. + * (Meaning that internally it uses String.getBytes) + *

+ * If you want to move Unicode data, you can first convert the + * String to a byte stream in the representation of your choice + * (i.e. UTF8 or two-byte Unicode data), and send it as a byte array. + */ + public void write(String what) { + write(what.getBytes()); + } + + public void setDTR(boolean state) { + try { + port.setDTR(state); + } catch (SerialPortException e) { + errorMessage("setDTR", e); + } + } + + public void setRTS(boolean state) { + try { + port.setRTS(state); + } catch (SerialPortException e) { + errorMessage("setRTS", e); + } + } + + static public List list() { + return Arrays.asList(SerialPortList.getPortNames()); + } + + + /** + * General error reporting, all corraled here just in case + * I think of something slightly more intelligent to do. + */ + static public void errorMessage(String where, Throwable e) { + System.err.println("Error inside Serial." + where + "()"); + e.printStackTrace(); + } +} + + + /* + class SerialMenuListener implements ItemListener { + //public SerialMenuListener() { } + + public void itemStateChanged(ItemEvent e) { + int count = serialMenu.getItemCount(); + for (int i = 0; i < count; i++) { + ((CheckboxMenuItem)serialMenu.getItem(i)).setState(false); + } + CheckboxMenuItem item = (CheckboxMenuItem)e.getSource(); + item.setState(true); + String name = item.getLabel(); + //System.out.println(item.getLabel()); + PdeBase.properties.put("serial.port", name); + //System.out.println("set to " + get("serial.port")); + } + } + */ + + + /* + protected Vector buildPortList() { + // get list of names for serial ports + // have the default port checked (if present) + Vector list = new Vector(); + + //SerialMenuListener listener = new SerialMenuListener(); + boolean problem = false; + + // if this is failing, it may be because + // lib/javax.comm.properties is missing. + // java is weird about how it searches for java.comm.properties + // so it tends to be very fragile. i.e. quotes in the CLASSPATH + // environment variable will hose things. + try { + //System.out.println("building port list"); + Enumeration portList = CommPortIdentifier.getPortIdentifiers(); + while (portList.hasMoreElements()) { + CommPortIdentifier portId = + (CommPortIdentifier) portList.nextElement(); + //System.out.println(portId); + + if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) { + //if (portId.getName().equals(port)) { + String name = portId.getName(); + //CheckboxMenuItem mi = + //new CheckboxMenuItem(name, name.equals(defaultName)); + + //mi.addItemListener(listener); + //serialMenu.add(mi); + list.addElement(name); + } + } + } catch (UnsatisfiedLinkError e) { + e.printStackTrace(); + problem = true; + + } catch (Exception e) { + System.out.println("exception building serial menu"); + e.printStackTrace(); + } + + //if (serialMenu.getItemCount() == 0) { + //System.out.println("dimming serial menu"); + //serialMenu.setEnabled(false); + //} + + // only warn them if this is the first time + if (problem && PdeBase.firstTime) { + JOptionPane.showMessageDialog(this, //frame, + "Serial port support not installed.\n" + + "Check the readme for instructions\n" + + "if you need to use the serial port. ", + "Serial Port Warning", + JOptionPane.WARNING_MESSAGE); + } + return list; + } + */ + + + diff --git a/tools/macosx/src/maple_loader/src/processing/app/SerialException.java b/tools/macosx/src/maple_loader/src/processing/app/SerialException.java new file mode 100644 index 0000000..525c240 --- /dev/null +++ b/tools/macosx/src/maple_loader/src/processing/app/SerialException.java @@ -0,0 +1,39 @@ +/* -*- mode: jde; c-basic-offset: 2; indent-tabs-mode: nil -*- */ + +/* + Copyright (c) 2007 David A. Mellis + + 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 +*/ + +package processing.app; + +public class SerialException extends Exception { + public SerialException() { + super(); + } + + public SerialException(String message) { + super(message); + } + + public SerialException(String message, Throwable cause) { + super(message, cause); + } + + public SerialException(Throwable cause) { + super(cause); + } +} diff --git a/tools/macosx/src/maple_loader/src/processing/app/debug/MessageConsumer.java b/tools/macosx/src/maple_loader/src/processing/app/debug/MessageConsumer.java new file mode 100644 index 0000000..5e20429 --- /dev/null +++ b/tools/macosx/src/maple_loader/src/processing/app/debug/MessageConsumer.java @@ -0,0 +1,42 @@ +/* -*- mode: java; c-basic-offset: 2; indent-tabs-mode: nil -*- */ + +/* + Part of the Processing project - http://processing.org + + Copyright (c) 2004-06 Ben Fry and Casey Reas + Copyright (c) 2001-04 Massachusetts Institute of Technology + + 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 +*/ + +package processing.app.debug; + + +/** + * Interface for dealing with parser/compiler output. + *

+ * Different instances of MessageStream need to do different things with + * messages. In particular, a stream instance used for parsing output from + * the compiler compiler has to interpret its messages differently than one + * parsing output from the runtime. + *

+ * Classes which consume messages and do something with them + * should implement this interface. + */ +public interface MessageConsumer { + + public void message(String s); + +} diff --git a/tools/macosx/src/maple_loader/src/processing/app/debug/MessageSiphon.java b/tools/macosx/src/maple_loader/src/processing/app/debug/MessageSiphon.java new file mode 100644 index 0000000..26901c3 --- /dev/null +++ b/tools/macosx/src/maple_loader/src/processing/app/debug/MessageSiphon.java @@ -0,0 +1,104 @@ +/* -*- mode: java; c-basic-offset: 2; indent-tabs-mode: nil -*- */ + +/* + Part of the Processing project - http://processing.org + + Copyright (c) 2004-06 Ben Fry and Casey Reas + Copyright (c) 2001-04 Massachusetts Institute of Technology + + 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 +*/ + +package processing.app.debug; + +import java.io.BufferedReader; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.net.SocketException; + +/** + * Slurps up messages from compiler. + */ +public class MessageSiphon implements Runnable { + + private final BufferedReader streamReader; + private final MessageConsumer consumer; + + private Thread thread; + private boolean canRun; + + public MessageSiphon(InputStream stream, MessageConsumer consumer) { + this.streamReader = new BufferedReader(new InputStreamReader(stream)); + this.consumer = consumer; + this.canRun = true; + + thread = new Thread(this); + // don't set priority too low, otherwise exceptions won't + // bubble up in time (i.e. compile errors have a weird delay) + //thread.setPriority(Thread.MIN_PRIORITY); + thread.setPriority(Thread.MAX_PRIORITY - 1); + thread.start(); + } + + + public void run() { + try { + // process data until we hit EOF; this will happily block + // (effectively sleeping the thread) until new data comes in. + // when the program is finally done, null will come through. + // + String currentLine; + while (canRun && (currentLine = streamReader.readLine()) != null) { + // \n is added again because readLine() strips it out + //EditorConsole.systemOut.println("messaging in"); + consumer.message(currentLine + "\n"); + //EditorConsole.systemOut.println("messaging out"); + } + //EditorConsole.systemOut.println("messaging thread done"); + } catch (NullPointerException npe) { + // Fairly common exception during shutdown + } catch (SocketException e) { + // socket has been close while we were wainting for data. nothing to see here, move along + } catch (Exception e) { + // On Linux and sometimes on Mac OS X, a "bad file descriptor" + // message comes up when closing an applet that's run externally. + // That message just gets supressed here.. + String mess = e.getMessage(); + if ((mess != null) && + (mess.indexOf("Bad file descriptor") != -1)) { + //if (e.getMessage().indexOf("Bad file descriptor") == -1) { + //System.err.println("MessageSiphon err " + e); + //e.printStackTrace(); + } else { + e.printStackTrace(); + } + } finally { + thread = null; + } + } + + // Wait until the MessageSiphon thread is complete. + public void join() throws java.lang.InterruptedException { + // Grab a temp copy in case another thread nulls the "thread" + // member variable + Thread t = thread; + if (t != null) t.join(); + } + + public void stop() { + this.canRun = false; + } + +} diff --git a/tools/macosx/src/maple_loader/src/processing/app/debug/RunnerException.java b/tools/macosx/src/maple_loader/src/processing/app/debug/RunnerException.java new file mode 100644 index 0000000..0a67d1e --- /dev/null +++ b/tools/macosx/src/maple_loader/src/processing/app/debug/RunnerException.java @@ -0,0 +1,161 @@ +/* -*- mode: java; c-basic-offset: 2; indent-tabs-mode: nil -*- */ + +/* + Part of the Processing project - http://processing.org + + Copyright (c) 2004-08 Ben Fry and Casey Reas + Copyright (c) 2001-04 Massachusetts Institute of Technology + + 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 +*/ + +package processing.app.debug; + + +/** + * An exception with a line number attached that occurs + * during either compile time or run time. + */ +@SuppressWarnings("serial") +public class RunnerException extends Exception { + protected String message; + protected int codeIndex; + protected int codeLine; + protected int codeColumn; + protected boolean showStackTrace; + + + public RunnerException(String message) { + this(message, true); + } + + public RunnerException(String message, boolean showStackTrace) { + this(message, -1, -1, -1, showStackTrace); + } + + public RunnerException(String message, int file, int line) { + this(message, file, line, -1, true); + } + + + public RunnerException(String message, int file, int line, int column) { + this(message, file, line, column, true); + } + + + public RunnerException(String message, int file, int line, int column, + boolean showStackTrace) { + this.message = message; + this.codeIndex = file; + this.codeLine = line; + this.codeColumn = column; + this.showStackTrace = showStackTrace; + } + + + public RunnerException(Exception e) { + super(e); + this.showStackTrace = true; + } + + /** + * Override getMessage() in Throwable, so that I can set + * the message text outside the constructor. + */ + public String getMessage() { + return message; + } + + + public void setMessage(String message) { + this.message = message; + } + + + public int getCodeIndex() { + return codeIndex; + } + + + public void setCodeIndex(int index) { + codeIndex = index; + } + + + public boolean hasCodeIndex() { + return codeIndex != -1; + } + + + public int getCodeLine() { + return codeLine; + } + + + public void setCodeLine(int line) { + this.codeLine = line; + } + + + public boolean hasCodeLine() { + return codeLine != -1; + } + + + public void setCodeColumn(int column) { + this.codeColumn = column; + } + + + public int getCodeColumn() { + return codeColumn; + } + + + public void showStackTrace() { + showStackTrace = true; + } + + + public void hideStackTrace() { + showStackTrace = false; + } + + + /** + * Nix the java.lang crap out of an exception message + * because it scares the children. + *

+ * This function must be static to be used with super() + * in each of the constructors above. + */ + /* + static public final String massage(String msg) { + if (msg.indexOf("java.lang.") == 0) { + //int dot = msg.lastIndexOf('.'); + msg = msg.substring("java.lang.".length()); + } + return msg; + //return (dot == -1) ? msg : msg.substring(dot+1); + } + */ + + + public void printStackTrace() { + if (showStackTrace) { + super.printStackTrace(); + } + } +} diff --git a/tools/macosx/src/maple_loader/src/processing/app/helpers/ProcessUtils.java b/tools/macosx/src/maple_loader/src/processing/app/helpers/ProcessUtils.java new file mode 100644 index 0000000..c023f58 --- /dev/null +++ b/tools/macosx/src/maple_loader/src/processing/app/helpers/ProcessUtils.java @@ -0,0 +1,32 @@ +package processing.app.helpers; + +//import processing.app.Base; + +import java.io.IOException; +import java.util.Map; + +import processing.app.Base; + +public class ProcessUtils { + + public static Process exec(String[] command) throws IOException { + // No problems on linux and mac + if (!Base.isWindows()) { + return Runtime.getRuntime().exec(command); + } + + // Brutal hack to workaround windows command line parsing. + // http://stackoverflow.com/questions/5969724/java-runtime-exec-fails-to-escape-characters-properly + // http://msdn.microsoft.com/en-us/library/a1y7w461.aspx + // http://bugs.sun.com/view_bug.do?bug_id=6468220 + // http://bugs.sun.com/view_bug.do?bug_id=6518827 + String[] cmdLine = new String[command.length]; + for (int i = 0; i < command.length; i++) + cmdLine[i] = command[i].replace("\"", "\\\""); + + ProcessBuilder pb = new ProcessBuilder(cmdLine); + Map env = pb.environment(); + env.put("CYGWIN", "nodosfilewarning"); + return pb.start(); + } +} diff --git a/tools/macosx/src/stm32flash_serial/src/AUTHORS b/tools/macosx/src/stm32flash_serial/src/AUTHORS new file mode 100644 index 0000000..d096f22 --- /dev/null +++ b/tools/macosx/src/stm32flash_serial/src/AUTHORS @@ -0,0 +1,19 @@ +Authors ordered by first contribution. + +Geoffrey McRae +Bret Olmsted +Tormod Volden +Jakob Malm +Reuben Dowle +Matthias Kubisch +Paul Fertser +Daniel Strnad +Jérémie Rapin +Christian Pointner +Mats Erik Andersson +Alexey Borovik +Antonio Borneo +Armin van der Togt +Brian Silverman +Georg Hofmann +Luis Rodrigues diff --git a/tools/macosx/src/stm32flash_serial/src/Android.mk b/tools/macosx/src/stm32flash_serial/src/Android.mk new file mode 100644 index 0000000..7be3d00 --- /dev/null +++ b/tools/macosx/src/stm32flash_serial/src/Android.mk @@ -0,0 +1,20 @@ +TOP_LOCAL_PATH := $(call my-dir) + +include $(call all-named-subdir-makefiles, parsers) + +LOCAL_PATH := $(TOP_LOCAL_PATH) + +include $(CLEAR_VARS) +LOCAL_MODULE := stm32flash +LOCAL_SRC_FILES := \ + dev_table.c \ + i2c.c \ + init.c \ + main.c \ + port.c \ + serial_common.c \ + serial_platform.c \ + stm32.c \ + utils.c +LOCAL_STATIC_LIBRARIES := libparsers +include $(BUILD_EXECUTABLE) diff --git a/tools/macosx/src/stm32flash_serial/src/HOWTO b/tools/macosx/src/stm32flash_serial/src/HOWTO new file mode 100644 index 0000000..d8f32eb --- /dev/null +++ b/tools/macosx/src/stm32flash_serial/src/HOWTO @@ -0,0 +1,35 @@ +Add new interfaces: +===================================================================== +Current version 0.4 supports the following interfaces: +- UART Windows (either "COMn" and "\\.\COMn"); +- UART posix/Linux (e.g. "/dev/ttyUSB0"); +- I2C Linux through standard driver "i2c-dev" (e.g. "/dev/i2c-n"). + +Starting from version 0.4, the back-end of stm32flash is modular and +ready to be expanded to support new interfaces. +I'm planning adding SPI on Linux through standard driver "spidev". +You are invited to contribute with more interfaces. + +To add a new interface you need to add a new file, populate the struct +port_interface (check at the end of files i2c.c, serial_posix.c and +serial_w32.c) and provide the relative functions to operate on the +interface: open/close, read/write, get_cfg_str and the optional gpio. +The include the new drive in Makefile and register the new struct +port_interface in file port.c in struct port_interface *ports[]. + +There are several USB-I2C adapter in the market, each providing its +own libraries to communicate with the I2C bus. +Could be interesting to provide as back-end a bridge between stm32flash +and such libraries (I have no plan on this item). + + +Add new STM32 devices: +===================================================================== +Add a new line in file dev_table.c, in table devices[]. +The fields of the table are listed in stm32.h, struct stm32_dev. + + +Cross compile on Linux host for Windows target with MinGW: +===================================================================== +I'm using a 64 bit Arch Linux machines, and I usually run: + make CC=x86_64-w64-mingw32-gcc AR=x86_64-w64-mingw32-ar diff --git a/tools/macosx/src/stm32flash_serial/src/I2C.txt b/tools/macosx/src/stm32flash_serial/src/I2C.txt new file mode 100644 index 0000000..4c05ff6 --- /dev/null +++ b/tools/macosx/src/stm32flash_serial/src/I2C.txt @@ -0,0 +1,94 @@ +About I2C back-end communication in stm32flash +========================================================================== + +Starting from version v0.4, beside the serial communication port, +stm32flash adds support for I2C port to talk with STM32 bootloader. + +The current I2C back-end supports only the API provided by Linux kernel +driver "i2c-dev", so only I2C controllers with Linux kernel driver can be +used. +In Linux source code, most of the drivers for I2C and SMBUS controllers +are in + ./drivers/i2c/busses/ +Only I2C is supported by STM32 bootloader, so check the section below +about SMBUS. +No I2C support for Windows is available in stm32flash v0.4. + +Thanks to the new modular back-end, stm32flash can be easily extended to +support new back-ends and API. Check HOWTO file in stm32flash source code +for details. + +In the market there are several USB-to-I2C dongles; most of them are not +supported by kernel drivers. Manufacturer provide proprietary userspace +libraries using not standardized API. +These API and dongles could be supported in feature versions. + +There are currently 3 versions of STM32 bootloader for I2C communications: +- v1.0 using I2C clock stretching synchronization between host and STM32; +- v1.1 superset of v1.0, adds non stretching commands; +- v1.2 superset of v1.1, adds CRC command and compatibility with i2cdetect. +Details in ST application note AN2606. +All the bootloaders above are tested and working with stm32flash. + + +SMBUS controllers +========================================================================== + +Almost 50% of the drivers in Linux source code folder + ./drivers/i2c/busses/ +are for controllers that "only" support SMBUS protocol. They can NOT +operate with STM32 bootloader. +To identify if your controller supports I2C, use command: + i2cdetect -F n +where "n" is the number of the I2C interface (e.g. n=3 for "/dev/i2c-3"). +Controllers that supports I2C will report + I2C yes +Controller that support both I2C and SMBUS are ok. + +If you are interested on details about SMBUS protocol, you can download +the current specs from + http://smbus.org/specs/smbus20.pdf +and you can read the files in Linux source code + ./Documentation/i2c/i2c-protocol + ./Documentation/i2c/smbus-protocol + + +About bootloader v1.0 +========================================================================== + +Version v1.0 can have issues with some I2C controllers due to use of clock +stretching during commands that require long operations, like flash erase +and programming. + +Clock stretching is a technique to synchronize host and I2C device. When +I2C device wants to force a delay in the communication, it push "low" the +I2C clock; the I2C controller detects it and waits until I2C clock returns +"high". +Most I2C controllers set a "timeout" for clock stretching, ranging from +few milli-seconds to seconds depending on specific HW or SW driver. + +It is possible that the timeout in your I2C controller is smaller than the +delay required for flash erase or programming. In this case the I2C +controller will timeout and report error to stm32flash. +There is no possibility for stm32flash to retry, so it can only signal the +error and exit. + +To by-pass the issue with bootloader v1.0 you can modify the kernel driver +of your I2C controller. Not an easy job, since every controller has its own +way to handle the timeout. + +In my case I'm using the I2C controller integrated in the VGA port of my +laptop HP EliteBook 8460p. I built the 0.25$ VGA-to-I2C adapter reported in + http://www.paintyourdragon.com/?p=43 +To change the timeout of the I2C controller I had to modify the kernel file + drivers/gpu/drm/radeon/radeon_i2c.c +line 969 +- i2c->bit.timeout = usecs_to_jiffies(2200); /* from VESA */ ++ i2c->bit.timeout = msecs_to_jiffies(5000); /* 5s for STM32 */ +and recompile it. +Then + $> modprobe i2c-dev + $> chmod 666 /dev/i2c-7 + #> stm32flash -a 0x39 /dev/i2c-7 + +2014-09-16 Antonio Borneo diff --git a/tools/macosx/src/stm32flash_serial/src/Makefile b/tools/macosx/src/stm32flash_serial/src/Makefile new file mode 100644 index 0000000..0328d55 --- /dev/null +++ b/tools/macosx/src/stm32flash_serial/src/Makefile @@ -0,0 +1,38 @@ +PREFIX = /usr/local +CFLAGS += -Wall -g + +INSTALL = install + +OBJS = dev_table.o \ + i2c.o \ + init.o \ + main.o \ + port.o \ + serial_common.o \ + serial_platform.o \ + stm32.o \ + utils.o + +LIBOBJS = parsers/parsers.a + +all: stm32flash + +serial_platform.o: serial_posix.c serial_w32.c + +parsers/parsers.a: + cd parsers && $(MAKE) parsers.a + +stm32flash: $(OBJS) $(LIBOBJS) + $(CC) $(LDFLAGS) -o $@ $(OBJS) $(LIBOBJS) + +clean: + rm -f $(OBJS) stm32flash + cd parsers && $(MAKE) $@ + +install: all + $(INSTALL) -d $(DESTDIR)$(PREFIX)/bin + $(INSTALL) -m 755 stm32flash $(DESTDIR)$(PREFIX)/bin + $(INSTALL) -d $(DESTDIR)$(PREFIX)/share/man/man1 + $(INSTALL) -m 644 stm32flash.1 $(DESTDIR)$(PREFIX)/share/man/man1 + +.PHONY: all clean install diff --git a/tools/macosx/src/stm32flash_serial/src/TODO b/tools/macosx/src/stm32flash_serial/src/TODO new file mode 100644 index 0000000..41df614 --- /dev/null +++ b/tools/macosx/src/stm32flash_serial/src/TODO @@ -0,0 +1,7 @@ + +stm32: +- Add support for variable page size + +AUTHORS: +- Add contributors from Geoffrey's commits + diff --git a/tools/macosx/src/stm32flash_serial/src/dev_table.c b/tools/macosx/src/stm32flash_serial/src/dev_table.c new file mode 100644 index 0000000..399cd9d --- /dev/null +++ b/tools/macosx/src/stm32flash_serial/src/dev_table.c @@ -0,0 +1,70 @@ +/* + stm32flash - Open Source ST STM32 flash program for *nix + Copyright (C) 2010 Geoffrey McRae + Copyright (C) 2014 Antonio Borneo + + 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. +*/ + +#include "stm32.h" + +/* + * Device table, corresponds to the "Bootloader device-dependant parameters" + * table in ST document AN2606. + * Note that the option bytes upper range is inclusive! + */ +const stm32_dev_t devices[] = { + /* F0 */ + {0x440, "STM32F051xx" , 0x20001000, 0x20002000, 0x08000000, 0x08010000, 4, 1024, 0x1FFFF800, 0x1FFFF80B, 0x1FFFEC00, 0x1FFFF800}, + {0x444, "STM32F030/F031" , 0x20001000, 0x20002000, 0x08000000, 0x08010000, 4, 1024, 0x1FFFF800, 0x1FFFF80B, 0x1FFFEC00, 0x1FFFF800}, + {0x445, "STM32F042xx" , 0x20001800, 0x20001800, 0x08000000, 0x08008000, 4, 1024, 0x1FFFF800, 0x1FFFF80F, 0x1FFFC400, 0x1FFFF800}, + {0x448, "STM32F072xx" , 0x20001800, 0x20004000, 0x08000000, 0x08020000, 2, 2048, 0x1FFFF800, 0x1FFFF80F, 0x1FFFC800, 0x1FFFF800}, + /* F1 */ + {0x412, "Low-density" , 0x20000200, 0x20002800, 0x08000000, 0x08008000, 4, 1024, 0x1FFFF800, 0x1FFFF80F, 0x1FFFF000, 0x1FFFF800}, + {0x410, "Medium-density" , 0x20000200, 0x20005000, 0x08000000, 0x08020000, 4, 1024, 0x1FFFF800, 0x1FFFF80F, 0x1FFFF000, 0x1FFFF800}, + {0x414, "High-density" , 0x20000200, 0x20010000, 0x08000000, 0x08080000, 2, 2048, 0x1FFFF800, 0x1FFFF80F, 0x1FFFF000, 0x1FFFF800}, + {0x420, "Medium-density VL" , 0x20000200, 0x20002000, 0x08000000, 0x08020000, 4, 1024, 0x1FFFF800, 0x1FFFF80F, 0x1FFFF000, 0x1FFFF800}, + {0x428, "High-density VL" , 0x20000200, 0x20008000, 0x08000000, 0x08080000, 2, 2048, 0x1FFFF800, 0x1FFFF80F, 0x1FFFF000, 0x1FFFF800}, + {0x418, "Connectivity line" , 0x20001000, 0x20010000, 0x08000000, 0x08040000, 2, 2048, 0x1FFFF800, 0x1FFFF80F, 0x1FFFB000, 0x1FFFF800}, + {0x430, "XL-density" , 0x20000800, 0x20018000, 0x08000000, 0x08100000, 2, 2048, 0x1FFFF800, 0x1FFFF80F, 0x1FFFE000, 0x1FFFF800}, + /* Note that F2 and F4 devices have sectors of different page sizes + and only the first sectors (of one page size) are included here */ + /* F2 */ + {0x411, "STM32F2xx" , 0x20002000, 0x20020000, 0x08000000, 0x08100000, 4, 16384, 0x1FFFC000, 0x1FFFC00F, 0x1FFF0000, 0x1FFF77DF}, + /* F3 */ + {0x432, "STM32F373/8" , 0x20001400, 0x20008000, 0x08000000, 0x08040000, 2, 2048, 0x1FFFF800, 0x1FFFF80F, 0x1FFFD800, 0x1FFFF800}, + {0x422, "F302xB/303xB/358" , 0x20001400, 0x20010000, 0x08000000, 0x08040000, 2, 2048, 0x1FFFF800, 0x1FFFF80F, 0x1FFFD800, 0x1FFFF800}, + {0x439, "STM32F302x4(6/8)" , 0x20001800, 0x20004000, 0x08000000, 0x08040000, 2, 2048, 0x1FFFF800, 0x1FFFF80F, 0x1FFFD800, 0x1FFFF800}, + {0x438, "F303x4/334/328" , 0x20001800, 0x20003000, 0x08000000, 0x08040000, 2, 2048, 0x1FFFF800, 0x1FFFF80F, 0x1FFFD800, 0x1FFFF800}, + /* F4 */ + {0x413, "STM32F40/1" , 0x20002000, 0x20020000, 0x08000000, 0x08100000, 4, 16384, 0x1FFFC000, 0x1FFFC00F, 0x1FFF0000, 0x1FFF77DF}, + /* 0x419 is also used for STM32F429/39 but with other bootloader ID... */ + {0x419, "STM32F427/37" , 0x20002000, 0x20030000, 0x08000000, 0x08100000, 4, 16384, 0x1FFFC000, 0x1FFFC00F, 0x1FFF0000, 0x1FFF77FF}, + {0x423, "STM32F401xB(C)" , 0x20003000, 0x20010000, 0x08000000, 0x08100000, 4, 16384, 0x1FFFC000, 0x1FFFC00F, 0x1FFF0000, 0x1FFF77FF}, + {0x433, "STM32F401xD(E)" , 0x20003000, 0x20018000, 0x08000000, 0x08100000, 4, 16384, 0x1FFFC000, 0x1FFFC00F, 0x1FFF0000, 0x1FFF77FF}, + /* L0 */ + {0x417, "L05xxx/06xxx" , 0x20001000, 0x20002000, 0x08000000, 0x08010000, 32, 128, 0x1FF80000, 0x1FF8000F, 0x1FF00000, 0x1FF01000}, + /* L1 */ + {0x416, "L1xxx6(8/B)" , 0x20000800, 0x20004000, 0x08000000, 0x08020000, 16, 256, 0x1FF80000, 0x1FF8000F, 0x1FF00000, 0x1FF01000}, + {0x429, "L1xxx6(8/B)A" , 0x20001000, 0x20008000, 0x08000000, 0x08020000, 16, 256, 0x1FF80000, 0x1FF8000F, 0x1FF00000, 0x1FF01000}, + {0x427, "L1xxxC" , 0x20001000, 0x20008000, 0x08000000, 0x08020000, 16, 256, 0x1FF80000, 0x1FF8000F, 0x1FF00000, 0x1FF02000}, + {0x436, "L1xxxD" , 0x20001000, 0x2000C000, 0x08000000, 0x08060000, 16, 256, 0x1ff80000, 0x1ff8000F, 0x1FF00000, 0x1FF02000}, + {0x437, "L1xxxE" , 0x20001000, 0x20014000, 0x08000000, 0x08060000, 16, 256, 0x1ff80000, 0x1ff8000F, 0x1FF00000, 0x1FF02000}, + /* These are not (yet) in AN2606: */ + {0x641, "Medium_Density PL" , 0x20000200, 0x00005000, 0x08000000, 0x08020000, 4, 1024, 0x1FFFF800, 0x1FFFF80F, 0x1FFFF000, 0x1FFFF800}, + {0x9a8, "STM32W-128K" , 0x20000200, 0x20002000, 0x08000000, 0x08020000, 1, 1024, 0, 0, 0, 0}, + {0x9b0, "STM32W-256K" , 0x20000200, 0x20004000, 0x08000000, 0x08040000, 1, 2048, 0, 0, 0, 0}, + {0x0} +}; diff --git a/tools/macosx/src/stm32flash_serial/src/gpl-2.0.txt b/tools/macosx/src/stm32flash_serial/src/gpl-2.0.txt new file mode 100644 index 0000000..d159169 --- /dev/null +++ b/tools/macosx/src/stm32flash_serial/src/gpl-2.0.txt @@ -0,0 +1,339 @@ + GNU GENERAL PUBLIC LICENSE + Version 2, June 1991 + + Copyright (C) 1989, 1991 Free Software Foundation, Inc., + 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 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 Lesser 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. + + + Copyright (C) + + 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., + 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 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. + + , 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 Lesser General +Public License instead of this License. diff --git a/tools/macosx/src/stm32flash_serial/src/i2c.c b/tools/macosx/src/stm32flash_serial/src/i2c.c new file mode 100644 index 0000000..10e6bb1 --- /dev/null +++ b/tools/macosx/src/stm32flash_serial/src/i2c.c @@ -0,0 +1,209 @@ +/* + stm32flash - Open Source ST STM32 flash program for *nix + Copyright (C) 2014 Antonio Borneo + + 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. +*/ + + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "serial.h" +#include "port.h" + + +#if !defined(__linux__) + +static port_err_t i2c_open(struct port_interface *port, + struct port_options *ops) +{ + return PORT_ERR_NODEV; +} + +struct port_interface port_i2c = { + .name = "i2c", + .open = i2c_open, +}; + +#else + +#ifdef __ANDROID__ +#define I2C_SLAVE 0x0703 /* Use this slave address */ +#define I2C_FUNCS 0x0705 /* Get the adapter functionality mask */ +/* To determine what functionality is present */ +#define I2C_FUNC_I2C 0x00000001 +#else +#include +#include +#endif + +#include + +struct i2c_priv { + int fd; + int addr; +}; + +static port_err_t i2c_open(struct port_interface *port, + struct port_options *ops) +{ + struct i2c_priv *h; + int fd, addr, ret; + unsigned long funcs; + + /* 1. check device name match */ + if (strncmp(ops->device, "/dev/i2c-", strlen("/dev/i2c-"))) + return PORT_ERR_NODEV; + + /* 2. check options */ + addr = ops->bus_addr; + if (addr < 0x03 || addr > 0x77) { + fprintf(stderr, "I2C address out of range [0x03-0x77]\n"); + return PORT_ERR_UNKNOWN; + } + + /* 3. open it */ + h = calloc(sizeof(*h), 1); + if (h == NULL) { + fprintf(stderr, "End of memory\n"); + return PORT_ERR_UNKNOWN; + } + fd = open(ops->device, O_RDWR); + if (fd < 0) { + fprintf(stderr, "Unable to open special file \"%s\"\n", + ops->device); + free(h); + return PORT_ERR_UNKNOWN; + } + + /* 3.5. Check capabilities */ + ret = ioctl(fd, I2C_FUNCS, &funcs); + if (ret < 0) { + fprintf(stderr, "I2C ioctl(funcs) error %d\n", errno); + close(fd); + free(h); + return PORT_ERR_UNKNOWN; + } + if ((funcs & I2C_FUNC_I2C) == 0) { + fprintf(stderr, "Error: controller is not I2C, only SMBUS.\n"); + close(fd); + free(h); + return PORT_ERR_UNKNOWN; + } + + /* 4. set options */ + ret = ioctl(fd, I2C_SLAVE, addr); + if (ret < 0) { + fprintf(stderr, "I2C ioctl(slave) error %d\n", errno); + close(fd); + free(h); + return PORT_ERR_UNKNOWN; + } + + h->fd = fd; + h->addr = addr; + port->private = h; + return PORT_ERR_OK; +} + +static port_err_t i2c_close(struct port_interface *port) +{ + struct i2c_priv *h; + + h = (struct i2c_priv *)port->private; + if (h == NULL) + return PORT_ERR_UNKNOWN; + close(h->fd); + free(h); + port->private = NULL; + return PORT_ERR_OK; +} + +static port_err_t i2c_read(struct port_interface *port, void *buf, + size_t nbyte) +{ + struct i2c_priv *h; + int ret; + + h = (struct i2c_priv *)port->private; + if (h == NULL) + return PORT_ERR_UNKNOWN; + ret = read(h->fd, buf, nbyte); + if (ret != nbyte) + return PORT_ERR_UNKNOWN; + return PORT_ERR_OK; +} + +static port_err_t i2c_write(struct port_interface *port, void *buf, + size_t nbyte) +{ + struct i2c_priv *h; + int ret; + + h = (struct i2c_priv *)port->private; + if (h == NULL) + return PORT_ERR_UNKNOWN; + ret = write(h->fd, buf, nbyte); + if (ret != nbyte) + return PORT_ERR_UNKNOWN; + return PORT_ERR_OK; +} + +static port_err_t i2c_gpio(struct port_interface *port, serial_gpio_t n, + int level) +{ + return PORT_ERR_OK; +} + +static const char *i2c_get_cfg_str(struct port_interface *port) +{ + struct i2c_priv *h; + static char str[11]; + + h = (struct i2c_priv *)port->private; + if (h == NULL) + return "INVALID"; + snprintf(str, sizeof(str), "addr 0x%2x", h->addr); + return str; +} + +static struct varlen_cmd i2c_cmd_get_reply[] = { + {0x10, 11}, + {0x11, 17}, + {0x12, 18}, + { /* sentinel */ } +}; + +struct port_interface port_i2c = { + .name = "i2c", + .flags = PORT_STRETCH_W, + .open = i2c_open, + .close = i2c_close, + .read = i2c_read, + .write = i2c_write, + .gpio = i2c_gpio, + .cmd_get_reply = i2c_cmd_get_reply, + .get_cfg_str = i2c_get_cfg_str, +}; + +#endif diff --git a/tools/macosx/src/stm32flash_serial/src/init.c b/tools/macosx/src/stm32flash_serial/src/init.c new file mode 100644 index 0000000..77a571b --- /dev/null +++ b/tools/macosx/src/stm32flash_serial/src/init.c @@ -0,0 +1,219 @@ +/* + stm32flash - Open Source ST STM32 flash program for *nix + Copyright (C) 2010 Geoffrey McRae + Copyright (C) 2013 Antonio Borneo + + 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. +*/ + + +#include +#include +#include +#include +#include +#include +#include +#include +#include "init.h" +#include "serial.h" +#include "stm32.h" +#include "port.h" + +struct gpio_list { + struct gpio_list *next; + int gpio; +}; + + +static int write_to(const char *filename, const char *value) +{ + int fd, ret; + + fd = open(filename, O_WRONLY); + if (fd < 0) { + fprintf(stderr, "Cannot open file \"%s\"\n", filename); + return 0; + } + ret = write(fd, value, strlen(value)); + if (ret < 0) { + fprintf(stderr, "Error writing in file \"%s\"\n", filename); + close(fd); + return 0; + } + close(fd); + return 1; +} + +#if !defined(__linux__) +static int drive_gpio(int n, int level, struct gpio_list **gpio_to_release) +{ + fprintf(stderr, "GPIO control only available in Linux\n"); + return 0; +} +#else +static int drive_gpio(int n, int level, struct gpio_list **gpio_to_release) +{ + char num[16]; /* sized to carry MAX_INT */ + char file[48]; /* sized to carry longest filename */ + struct stat buf; + struct gpio_list *new; + int ret; + + sprintf(file, "/sys/class/gpio/gpio%d/direction", n); + ret = stat(file, &buf); + if (ret) { + /* file miss, GPIO not exported yet */ + sprintf(num, "%d", n); + ret = write_to("/sys/class/gpio/export", num); + if (!ret) + return 0; + ret = stat(file, &buf); + if (ret) { + fprintf(stderr, "GPIO %d not available\n", n); + return 0; + } + new = (struct gpio_list *)malloc(sizeof(struct gpio_list)); + if (new == NULL) { + fprintf(stderr, "Out of memory\n"); + return 0; + } + new->gpio = n; + new->next = *gpio_to_release; + *gpio_to_release = new; + } + + return write_to(file, level ? "high" : "low"); +} +#endif + +static int release_gpio(int n) +{ + char num[16]; /* sized to carry MAX_INT */ + + sprintf(num, "%d", n); + return write_to("/sys/class/gpio/unexport", num); +} + +static int gpio_sequence(struct port_interface *port, const char *s, size_t l) +{ + struct gpio_list *gpio_to_release = NULL, *to_free; + int ret, level, gpio; + + ret = 1; + while (ret == 1 && *s && l > 0) { + if (*s == '-') { + level = 0; + s++; + l--; + } else + level = 1; + + if (isdigit(*s)) { + gpio = atoi(s); + while (isdigit(*s)) { + s++; + l--; + } + } else if (!strncmp(s, "rts", 3)) { + gpio = -GPIO_RTS; + s += 3; + l -= 3; + } else if (!strncmp(s, "dtr", 3)) { + gpio = -GPIO_DTR; + s += 3; + l -= 3; + } else if (!strncmp(s, "brk", 3)) { + gpio = -GPIO_BRK; + s += 3; + l -= 3; + } else { + fprintf(stderr, "Character \'%c\' is not a digit\n", *s); + ret = 0; + break; + } + + if (*s && (l > 0)) { + if (*s == ',') { + s++; + l--; + } else { + fprintf(stderr, "Character \'%c\' is not a separator\n", *s); + ret = 0; + break; + } + } + if (gpio < 0) + ret = (port->gpio(port, -gpio, level) == PORT_ERR_OK); + else + ret = drive_gpio(gpio, level, &gpio_to_release); + usleep(100000); + } + + while (gpio_to_release) { + release_gpio(gpio_to_release->gpio); + to_free = gpio_to_release; + gpio_to_release = gpio_to_release->next; + free(to_free); + } + usleep(500000); + return ret; +} + +static int gpio_bl_entry(struct port_interface *port, const char *seq) +{ + char *s; + + if (seq == NULL || seq[0] == ':') + return 1; + + s = strchr(seq, ':'); + if (s == NULL) + return gpio_sequence(port, seq, strlen(seq)); + + return gpio_sequence(port, seq, s - seq); +} + +static int gpio_bl_exit(struct port_interface *port, const char *seq) +{ + char *s; + + if (seq == NULL) + return 1; + + s = strchr(seq, ':'); + if (s == NULL || s[1] == '\0') + return 1; + + return gpio_sequence(port, s + 1, strlen(s + 1)); +} + +int init_bl_entry(struct port_interface *port, const char *seq) +{ + if (seq) + return gpio_bl_entry(port, seq); + + return 1; +} + +int init_bl_exit(stm32_t *stm, struct port_interface *port, const char *seq) +{ + if (seq) + return gpio_bl_exit(port, seq); + + if (stm32_reset_device(stm) != STM32_ERR_OK) + return 0; + return 1; +} diff --git a/tools/macosx/src/stm32flash_serial/src/init.h b/tools/macosx/src/stm32flash_serial/src/init.h new file mode 100644 index 0000000..6075b51 --- /dev/null +++ b/tools/macosx/src/stm32flash_serial/src/init.h @@ -0,0 +1,31 @@ +/* + stm32flash - Open Source ST STM32 flash program for *nix + Copyright (C) 2010 Geoffrey McRae + Copyright (C) 2013 Antonio Borneo + + 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. +*/ + + +#ifndef _INIT_H +#define _INIT_H + +#include "stm32.h" +#include "port.h" + +int init_bl_entry(struct port_interface *port, const char *seq); +int init_bl_exit(stm32_t *stm, struct port_interface *port, const char *seq); + +#endif diff --git a/tools/macosx/src/stm32flash_serial/src/main.c b/tools/macosx/src/stm32flash_serial/src/main.c new file mode 100644 index 0000000..f081d61 --- /dev/null +++ b/tools/macosx/src/stm32flash_serial/src/main.c @@ -0,0 +1,774 @@ +/* + stm32flash - Open Source ST STM32 flash program for *nix + Copyright 2010 Geoffrey McRae + Copyright 2011 Steve Markgraf + Copyright 2012 Tormod Volden + Copyright 2013 Antonio Borneo + + 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. +*/ + +#include +#include +#include +#include +#include +#include +#include + +#include "init.h" +#include "utils.h" +#include "serial.h" +#include "stm32.h" +#include "parsers/parser.h" +#include "port.h" + +#include "parsers/binary.h" +#include "parsers/hex.h" + +#define VERSION "Arduino_STM32_0.9" + +/* device globals */ +stm32_t *stm = NULL; + +void *p_st = NULL; +parser_t *parser = NULL; + +/* settings */ +struct port_options port_opts = { + .device = NULL, + .baudRate = SERIAL_BAUD_57600, + .serial_mode = "8e1", + .bus_addr = 0, + .rx_frame_max = STM32_MAX_RX_FRAME, + .tx_frame_max = STM32_MAX_TX_FRAME, +}; +int rd = 0; +int wr = 0; +int wu = 0; +int rp = 0; +int ur = 0; +int eraseOnly = 0; +int crc = 0; +int npages = 0; +int spage = 0; +int no_erase = 0; +char verify = 0; +int retry = 10; +char exec_flag = 0; +uint32_t execute = 0; +char init_flag = 1; +char force_binary = 0; +char reset_flag = 0; +char *filename; +char *gpio_seq = NULL; +uint32_t start_addr = 0; +uint32_t readwrite_len = 0; + +/* functions */ +int parse_options(int argc, char *argv[]); +void show_help(char *name); + +static int is_addr_in_ram(uint32_t addr) +{ + return addr >= stm->dev->ram_start && addr < stm->dev->ram_end; +} + +static int is_addr_in_flash(uint32_t addr) +{ + return addr >= stm->dev->fl_start && addr < stm->dev->fl_end; +} + +static int flash_addr_to_page_floor(uint32_t addr) +{ + if (!is_addr_in_flash(addr)) + return 0; + + return (addr - stm->dev->fl_start) / stm->dev->fl_ps; +} + +static int flash_addr_to_page_ceil(uint32_t addr) +{ + if (!(addr >= stm->dev->fl_start && addr <= stm->dev->fl_end)) + return 0; + + return (addr + stm->dev->fl_ps - 1 - stm->dev->fl_start) + / stm->dev->fl_ps; +} + +static uint32_t flash_page_to_addr(int page) +{ + return stm->dev->fl_start + page * stm->dev->fl_ps; +} + +int main(int argc, char* argv[]) { + struct port_interface *port = NULL; + int ret = 1; + stm32_err_t s_err; + parser_err_t perr; + FILE *diag = stdout; + + fprintf(diag, "stm32flash " VERSION "\n\n"); + fprintf(diag, "http://github.com/rogerclarkmelbourne/arduino_stm32\n\n"); + if (parse_options(argc, argv) != 0) + goto close; + + if (rd && filename[0] == '-') { + diag = stderr; + } + + if (wr) { + /* first try hex */ + if (!force_binary) { + parser = &PARSER_HEX; + p_st = parser->init(); + if (!p_st) { + fprintf(stderr, "%s Parser failed to initialize\n", parser->name); + goto close; + } + } + + if (force_binary || (perr = parser->open(p_st, filename, 0)) != PARSER_ERR_OK) { + if (force_binary || perr == PARSER_ERR_INVALID_FILE) { + if (!force_binary) { + parser->close(p_st); + p_st = NULL; + } + + /* now try binary */ + parser = &PARSER_BINARY; + p_st = parser->init(); + if (!p_st) { + fprintf(stderr, "%s Parser failed to initialize\n", parser->name); + goto close; + } + perr = parser->open(p_st, filename, 0); + } + + /* if still have an error, fail */ + if (perr != PARSER_ERR_OK) { + fprintf(stderr, "%s ERROR: %s\n", parser->name, parser_errstr(perr)); + if (perr == PARSER_ERR_SYSTEM) perror(filename); + goto close; + } + } + + fprintf(diag, "Using Parser : %s\n", parser->name); + } else { + parser = &PARSER_BINARY; + p_st = parser->init(); + if (!p_st) { + fprintf(stderr, "%s Parser failed to initialize\n", parser->name); + goto close; + } + } + + if (port_open(&port_opts, &port) != PORT_ERR_OK) { + fprintf(stderr, "Failed to open port: %s\n", port_opts.device); + goto close; + } + + fprintf(diag, "Interface %s: %s\n", port->name, port->get_cfg_str(port)); + if (init_flag && init_bl_entry(port, gpio_seq) == 0) + goto close; + stm = stm32_init(port, init_flag); + if (!stm) + goto close; + + fprintf(diag, "Version : 0x%02x\n", stm->bl_version); + if (port->flags & PORT_GVR_ETX) { + fprintf(diag, "Option 1 : 0x%02x\n", stm->option1); + fprintf(diag, "Option 2 : 0x%02x\n", stm->option2); + } + fprintf(diag, "Device ID : 0x%04x (%s)\n", stm->pid, stm->dev->name); + fprintf(diag, "- RAM : %dKiB (%db reserved by bootloader)\n", (stm->dev->ram_end - 0x20000000) / 1024, stm->dev->ram_start - 0x20000000); + fprintf(diag, "- Flash : %dKiB (sector size: %dx%d)\n", (stm->dev->fl_end - stm->dev->fl_start ) / 1024, stm->dev->fl_pps, stm->dev->fl_ps); + fprintf(diag, "- Option RAM : %db\n", stm->dev->opt_end - stm->dev->opt_start + 1); + fprintf(diag, "- System RAM : %dKiB\n", (stm->dev->mem_end - stm->dev->mem_start) / 1024); + + uint8_t buffer[256]; + uint32_t addr, start, end; + unsigned int len; + int failed = 0; + int first_page, num_pages; + + /* + * Cleanup addresses: + * + * Starting from options + * start_addr, readwrite_len, spage, npages + * and using device memory size, compute + * start, end, first_page, num_pages + */ + if (start_addr || readwrite_len) { + start = start_addr; + + if (is_addr_in_flash(start)) + end = stm->dev->fl_end; + else { + no_erase = 1; + if (is_addr_in_ram(start)) + end = stm->dev->ram_end; + else + end = start + sizeof(uint32_t); + } + + if (readwrite_len && (end > start + readwrite_len)) + end = start + readwrite_len; + + first_page = flash_addr_to_page_floor(start); + if (!first_page && end == stm->dev->fl_end) + num_pages = 0xff; /* mass erase */ + else + num_pages = flash_addr_to_page_ceil(end) - first_page; + } else if (!spage && !npages) { + start = stm->dev->fl_start; + end = stm->dev->fl_end; + first_page = 0; + num_pages = 0xff; /* mass erase */ + } else { + first_page = spage; + start = flash_page_to_addr(first_page); + if (start > stm->dev->fl_end) { + fprintf(stderr, "Address range exceeds flash size.\n"); + goto close; + } + + if (npages) { + num_pages = npages; + end = flash_page_to_addr(first_page + num_pages); + if (end > stm->dev->fl_end) + end = stm->dev->fl_end; + } else { + end = stm->dev->fl_end; + num_pages = flash_addr_to_page_ceil(end) - first_page; + } + + if (!first_page && end == stm->dev->fl_end) + num_pages = 0xff; /* mass erase */ + } + + if (rd) { + unsigned int max_len = port_opts.rx_frame_max; + + fprintf(diag, "Memory read\n"); + + perr = parser->open(p_st, filename, 1); + if (perr != PARSER_ERR_OK) { + fprintf(stderr, "%s ERROR: %s\n", parser->name, parser_errstr(perr)); + if (perr == PARSER_ERR_SYSTEM) + perror(filename); + goto close; + } + + fflush(diag); + addr = start; + while(addr < end) { + uint32_t left = end - addr; + len = max_len > left ? left : max_len; + s_err = stm32_read_memory(stm, addr, buffer, len); + if (s_err != STM32_ERR_OK) { + fprintf(stderr, "Failed to read memory at address 0x%08x, target write-protected?\n", addr); + goto close; + } + if (parser->write(p_st, buffer, len) != PARSER_ERR_OK) + { + fprintf(stderr, "Failed to write data to file\n"); + goto close; + } + addr += len; + + fprintf(diag, + "\rRead address 0x%08x (%.2f%%) ", + addr, + (100.0f / (float)(end - start)) * (float)(addr - start) + ); + fflush(diag); + } + fprintf(diag, "Done.\n"); + ret = 0; + goto close; + } else if (rp) { + fprintf(stdout, "Read-Protecting flash\n"); + /* the device automatically performs a reset after the sending the ACK */ + reset_flag = 0; + stm32_readprot_memory(stm); + fprintf(stdout, "Done.\n"); + } else if (ur) { + fprintf(stdout, "Read-UnProtecting flash\n"); + /* the device automatically performs a reset after the sending the ACK */ + reset_flag = 0; + stm32_runprot_memory(stm); + fprintf(stdout, "Done.\n"); + } else if (eraseOnly) { + ret = 0; + fprintf(stdout, "Erasing flash\n"); + + if (num_pages != 0xff && + (start != flash_page_to_addr(first_page) + || end != flash_page_to_addr(first_page + num_pages))) { + fprintf(stderr, "Specified start & length are invalid (must be page aligned)\n"); + ret = 1; + goto close; + } + + s_err = stm32_erase_memory(stm, first_page, num_pages); + if (s_err != STM32_ERR_OK) { + fprintf(stderr, "Failed to erase memory\n"); + ret = 1; + goto close; + } + } else if (wu) { + fprintf(diag, "Write-unprotecting flash\n"); + /* the device automatically performs a reset after the sending the ACK */ + reset_flag = 0; + stm32_wunprot_memory(stm); + fprintf(diag, "Done.\n"); + + } else if (wr) { + fprintf(diag, "Write to memory\n"); + + off_t offset = 0; + ssize_t r; + unsigned int size; + unsigned int max_wlen, max_rlen; + + max_wlen = port_opts.tx_frame_max - 2; /* skip len and crc */ + max_wlen &= ~3; /* 32 bit aligned */ + + max_rlen = port_opts.rx_frame_max; + max_rlen = max_rlen < max_wlen ? max_rlen : max_wlen; + + /* Assume data from stdin is whole device */ + if (filename[0] == '-' && filename[1] == '\0') + size = end - start; + else + size = parser->size(p_st); + + // TODO: It is possible to write to non-page boundaries, by reading out flash + // from partial pages and combining with the input data + // if ((start % stm->dev->fl_ps) != 0 || (end % stm->dev->fl_ps) != 0) { + // fprintf(stderr, "Specified start & length are invalid (must be page aligned)\n"); + // goto close; + // } + + // TODO: If writes are not page aligned, we should probably read out existing flash + // contents first, so it can be preserved and combined with new data + if (!no_erase && num_pages) { + fprintf(diag, "Erasing memory\n"); + s_err = stm32_erase_memory(stm, first_page, num_pages); + if (s_err != STM32_ERR_OK) { + fprintf(stderr, "Failed to erase memory\n"); + goto close; + } + } + + fflush(diag); + addr = start; + while(addr < end && offset < size) { + uint32_t left = end - addr; + len = max_wlen > left ? left : max_wlen; + len = len > size - offset ? size - offset : len; + + if (parser->read(p_st, buffer, &len) != PARSER_ERR_OK) + goto close; + + if (len == 0) { + if (filename[0] == '-') { + break; + } else { + fprintf(stderr, "Failed to read input file\n"); + goto close; + } + } + + again: + s_err = stm32_write_memory(stm, addr, buffer, len); + if (s_err != STM32_ERR_OK) { + fprintf(stderr, "Failed to write memory at address 0x%08x\n", addr); + goto close; + } + + if (verify) { + uint8_t compare[len]; + unsigned int offset, rlen; + + offset = 0; + while (offset < len) { + rlen = len - offset; + rlen = rlen < max_rlen ? rlen : max_rlen; + s_err = stm32_read_memory(stm, addr + offset, compare + offset, rlen); + if (s_err != STM32_ERR_OK) { + fprintf(stderr, "Failed to read memory at address 0x%08x\n", addr + offset); + goto close; + } + offset += rlen; + } + + for(r = 0; r < len; ++r) + if (buffer[r] != compare[r]) { + if (failed == retry) { + fprintf(stderr, "Failed to verify at address 0x%08x, expected 0x%02x and found 0x%02x\n", + (uint32_t)(addr + r), + buffer [r], + compare[r] + ); + goto close; + } + ++failed; + goto again; + } + + failed = 0; + } + + addr += len; + offset += len; + + fprintf(diag, + "\rWrote %saddress 0x%08x (%.2f%%) ", + verify ? "and verified " : "", + addr, + (100.0f / size) * offset + ); + fflush(diag); + + } + + fprintf(diag, "Done.\n"); + ret = 0; + goto close; + } else if (crc) { + uint32_t crc_val = 0; + + fprintf(diag, "CRC computation\n"); + + s_err = stm32_crc_wrapper(stm, start, end - start, &crc_val); + if (s_err != STM32_ERR_OK) { + fprintf(stderr, "Failed to read CRC\n"); + goto close; + } + fprintf(diag, "CRC(0x%08x-0x%08x) = 0x%08x\n", start, end, + crc_val); + ret = 0; + goto close; + } else + ret = 0; + +close: + if (stm && exec_flag && ret == 0) { + if (execute == 0) + execute = stm->dev->fl_start; + + fprintf(diag, "\nStarting execution at address 0x%08x... ", execute); + fflush(diag); + if (stm32_go(stm, execute) == STM32_ERR_OK) { + reset_flag = 0; + fprintf(diag, "done.\n"); + } else + fprintf(diag, "failed.\n"); + } + + if (stm && reset_flag) { + fprintf(diag, "\nResetting device... "); + fflush(diag); + if (init_bl_exit(stm, port, gpio_seq)) + fprintf(diag, "done.\n"); + else fprintf(diag, "failed.\n"); + } + + if (p_st ) parser->close(p_st); + if (stm ) stm32_close (stm); + if (port) + port->close(port); + + fprintf(diag, "\n"); + return ret; +} + +int parse_options(int argc, char *argv[]) +{ + int c; + char *pLen; + + while ((c = getopt(argc, argv, "a:b:m:r:w:e:vn:g:jkfcChuos:S:F:i:R")) != -1) { + switch(c) { + case 'a': + port_opts.bus_addr = strtoul(optarg, NULL, 0); + break; + + case 'b': + port_opts.baudRate = serial_get_baud(strtoul(optarg, NULL, 0)); + if (port_opts.baudRate == SERIAL_BAUD_INVALID) { + serial_baud_t baudrate; + fprintf(stderr, "Invalid baud rate, valid options are:\n"); + for (baudrate = SERIAL_BAUD_1200; baudrate != SERIAL_BAUD_INVALID; ++baudrate) + fprintf(stderr, " %d\n", serial_get_baud_int(baudrate)); + return 1; + } + break; + + case 'm': + if (strlen(optarg) != 3 + || serial_get_bits(optarg) == SERIAL_BITS_INVALID + || serial_get_parity(optarg) == SERIAL_PARITY_INVALID + || serial_get_stopbit(optarg) == SERIAL_STOPBIT_INVALID) { + fprintf(stderr, "Invalid serial mode\n"); + return 1; + } + port_opts.serial_mode = optarg; + break; + + case 'r': + case 'w': + rd = rd || c == 'r'; + wr = wr || c == 'w'; + if (rd && wr) { + fprintf(stderr, "ERROR: Invalid options, can't read & write at the same time\n"); + return 1; + } + filename = optarg; + if (filename[0] == '-') { + force_binary = 1; + } + break; + case 'e': + if (readwrite_len || start_addr) { + fprintf(stderr, "ERROR: Invalid options, can't specify start page / num pages and start address/length\n"); + return 1; + } + npages = strtoul(optarg, NULL, 0); + if (npages > 0xFF || npages < 0) { + fprintf(stderr, "ERROR: You need to specify a page count between 0 and 255"); + return 1; + } + if (!npages) + no_erase = 1; + break; + case 'u': + wu = 1; + if (rd || wr) { + fprintf(stderr, "ERROR: Invalid options, can't write unprotect and read/write at the same time\n"); + return 1; + } + break; + + case 'j': + rp = 1; + if (rd || wr) { + fprintf(stderr, "ERROR: Invalid options, can't read protect and read/write at the same time\n"); + return 1; + } + break; + + case 'k': + ur = 1; + if (rd || wr) { + fprintf(stderr, "ERROR: Invalid options, can't read unprotect and read/write at the same time\n"); + return 1; + } + break; + + case 'o': + eraseOnly = 1; + if (rd || wr) { + fprintf(stderr, "ERROR: Invalid options, can't erase-only and read/write at the same time\n"); + return 1; + } + break; + + case 'v': + verify = 1; + break; + + case 'n': + retry = strtoul(optarg, NULL, 0); + break; + + case 'g': + exec_flag = 1; + execute = strtoul(optarg, NULL, 0); + if (execute % 4 != 0) { + fprintf(stderr, "ERROR: Execution address must be word-aligned\n"); + return 1; + } + break; + case 's': + if (readwrite_len || start_addr) { + fprintf(stderr, "ERROR: Invalid options, can't specify start page / num pages and start address/length\n"); + return 1; + } + spage = strtoul(optarg, NULL, 0); + break; + case 'S': + if (spage || npages) { + fprintf(stderr, "ERROR: Invalid options, can't specify start page / num pages and start address/length\n"); + return 1; + } else { + start_addr = strtoul(optarg, &pLen, 0); + if (*pLen == ':') { + pLen++; + readwrite_len = strtoul(pLen, NULL, 0); + if (readwrite_len == 0) { + fprintf(stderr, "ERROR: Invalid options, can't specify zero length\n"); + return 1; + } + } + } + break; + case 'F': + port_opts.rx_frame_max = strtoul(optarg, &pLen, 0); + if (*pLen == ':') { + pLen++; + port_opts.tx_frame_max = strtoul(pLen, NULL, 0); + } + if (port_opts.rx_frame_max < 0 + || port_opts.tx_frame_max < 0) { + fprintf(stderr, "ERROR: Invalid negative value for option -F\n"); + return 1; + } + if (port_opts.rx_frame_max == 0) + port_opts.rx_frame_max = STM32_MAX_RX_FRAME; + if (port_opts.tx_frame_max == 0) + port_opts.tx_frame_max = STM32_MAX_TX_FRAME; + if (port_opts.rx_frame_max < 20 + || port_opts.tx_frame_max < 5) { + fprintf(stderr, "ERROR: current code cannot work with small frames.\n"); + fprintf(stderr, "min(RX) = 20, min(TX) = 5\n"); + return 1; + } + if (port_opts.rx_frame_max > STM32_MAX_RX_FRAME) { + fprintf(stderr, "WARNING: Ignore RX length in option -F\n"); + port_opts.rx_frame_max = STM32_MAX_RX_FRAME; + } + if (port_opts.tx_frame_max > STM32_MAX_TX_FRAME) { + fprintf(stderr, "WARNING: Ignore TX length in option -F\n"); + port_opts.tx_frame_max = STM32_MAX_TX_FRAME; + } + break; + case 'f': + force_binary = 1; + break; + + case 'c': + init_flag = 0; + break; + + case 'h': + show_help(argv[0]); + exit(0); + + case 'i': + gpio_seq = optarg; + break; + + case 'R': + reset_flag = 1; + break; + + case 'C': + crc = 1; + break; + } + } + + for (c = optind; c < argc; ++c) { + if (port_opts.device) { + fprintf(stderr, "ERROR: Invalid parameter specified\n"); + show_help(argv[0]); + return 1; + } + port_opts.device = argv[c]; + } + + if (port_opts.device == NULL) { + fprintf(stderr, "ERROR: Device not specified\n"); + show_help(argv[0]); + return 1; + } + + if (!wr && verify) { + fprintf(stderr, "ERROR: Invalid usage, -v is only valid when writing\n"); + show_help(argv[0]); + return 1; + } + + return 0; +} + +void show_help(char *name) { + fprintf(stderr, + "Usage: %s [-bvngfhc] [-[rw] filename] [tty_device | i2c_device]\n" + " -a bus_address Bus address (e.g. for I2C port)\n" + " -b rate Baud rate (default 57600)\n" + " -m mode Serial port mode (default 8e1)\n" + " -r filename Read flash to file (or - stdout)\n" + " -w filename Write flash from file (or - stdout)\n" + " -C Compute CRC of flash content\n" + " -u Disable the flash write-protection\n" + " -j Enable the flash read-protection\n" + " -k Disable the flash read-protection\n" + " -o Erase only\n" + " -e n Only erase n pages before writing the flash\n" + " -v Verify writes\n" + " -n count Retry failed writes up to count times (default 10)\n" + " -g address Start execution at specified address (0 = flash start)\n" + " -S address[:length] Specify start address and optionally length for\n" + " read/write/erase operations\n" + " -F RX_length[:TX_length] Specify the max length of RX and TX frame\n" + " -s start_page Flash at specified page (0 = flash start)\n" + " -f Force binary parser\n" + " -h Show this help\n" + " -c Resume the connection (don't send initial INIT)\n" + " *Baud rate must be kept the same as the first init*\n" + " This is useful if the reset fails\n" + " -i GPIO_string GPIO sequence to enter/exit bootloader mode\n" + " GPIO_string=[entry_seq][:[exit_seq]]\n" + " sequence=[-]n[,sequence]\n" + " -R Reset device at exit.\n" + "\n" + "Examples:\n" + " Get device information:\n" + " %s /dev/ttyS0\n" + " or:\n" + " %s /dev/i2c-0\n" + "\n" + " Write with verify and then start execution:\n" + " %s -w filename -v -g 0x0 /dev/ttyS0\n" + "\n" + " Read flash to file:\n" + " %s -r filename /dev/ttyS0\n" + "\n" + " Read 100 bytes of flash from 0x1000 to stdout:\n" + " %s -r - -S 0x1000:100 /dev/ttyS0\n" + "\n" + " Start execution:\n" + " %s -g 0x0 /dev/ttyS0\n" + "\n" + " GPIO sequence:\n" + " - entry sequence: GPIO_3=low, GPIO_2=low, GPIO_2=high\n" + " - exit sequence: GPIO_3=high, GPIO_2=low, GPIO_2=high\n" + " %s -i -3,-2,2:3,-2,2 /dev/ttyS0\n", + name, + name, + name, + name, + name, + name, + name, + name + ); +} + diff --git a/tools/macosx/src/stm32flash_serial/src/parsers/Android.mk b/tools/macosx/src/stm32flash_serial/src/parsers/Android.mk new file mode 100644 index 0000000..afec18c --- /dev/null +++ b/tools/macosx/src/stm32flash_serial/src/parsers/Android.mk @@ -0,0 +1,6 @@ +LOCAL_PATH := $(call my-dir) + +include $(CLEAR_VARS) +LOCAL_MODULE := libparsers +LOCAL_SRC_FILES := binary.c hex.c +include $(BUILD_STATIC_LIBRARY) diff --git a/tools/macosx/src/stm32flash_serial/src/parsers/Makefile b/tools/macosx/src/stm32flash_serial/src/parsers/Makefile new file mode 100644 index 0000000..bb7df1e --- /dev/null +++ b/tools/macosx/src/stm32flash_serial/src/parsers/Makefile @@ -0,0 +1,12 @@ + +CFLAGS += -Wall -g + +all: parsers.a + +parsers.a: binary.o hex.o + $(AR) rc $@ binary.o hex.o + +clean: + rm -f *.o parsers.a + +.PHONY: all clean diff --git a/tools/macosx/src/stm32flash_serial/src/parsers/binary.c b/tools/macosx/src/stm32flash_serial/src/parsers/binary.c new file mode 100644 index 0000000..f491952 --- /dev/null +++ b/tools/macosx/src/stm32flash_serial/src/parsers/binary.c @@ -0,0 +1,140 @@ +/* + stm32flash - Open Source ST STM32 flash program for *nix + Copyright (C) 2010 Geoffrey McRae + + 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. +*/ + + +#include +#include +#include +#include +#include + +#include "binary.h" + +typedef struct { + int fd; + char write; + struct stat stat; +} binary_t; + +void* binary_init() { + return calloc(sizeof(binary_t), 1); +} + +parser_err_t binary_open(void *storage, const char *filename, const char write) { + binary_t *st = storage; + if (write) { + if (filename[0] == '-') + st->fd = 1; + else + st->fd = open( + filename, +#ifndef __WIN32__ + O_WRONLY | O_CREAT | O_TRUNC, +#else + O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, +#endif +#ifndef __WIN32__ + S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH +#else + 0 +#endif + ); + st->stat.st_size = 0; + } else { + if (filename[0] == '-') { + st->fd = 0; + } else { + if (stat(filename, &st->stat) != 0) + return PARSER_ERR_INVALID_FILE; + st->fd = open(filename, +#ifndef __WIN32__ + O_RDONLY +#else + O_RDONLY | O_BINARY +#endif + ); + } + } + + st->write = write; + return st->fd == -1 ? PARSER_ERR_SYSTEM : PARSER_ERR_OK; +} + +parser_err_t binary_close(void *storage) { + binary_t *st = storage; + + if (st->fd) close(st->fd); + free(st); + return PARSER_ERR_OK; +} + +unsigned int binary_size(void *storage) { + binary_t *st = storage; + return st->stat.st_size; +} + +parser_err_t binary_read(void *storage, void *data, unsigned int *len) { + binary_t *st = storage; + unsigned int left = *len; + if (st->write) return PARSER_ERR_WRONLY; + + ssize_t r; + while(left > 0) { + r = read(st->fd, data, left); + /* If there is no data to read at all, return OK, but with zero read */ + if (r == 0 && left == *len) { + *len = 0; + return PARSER_ERR_OK; + } + if (r <= 0) return PARSER_ERR_SYSTEM; + left -= r; + data += r; + } + + *len = *len - left; + return PARSER_ERR_OK; +} + +parser_err_t binary_write(void *storage, void *data, unsigned int len) { + binary_t *st = storage; + if (!st->write) return PARSER_ERR_RDONLY; + + ssize_t r; + while(len > 0) { + r = write(st->fd, data, len); + if (r < 1) return PARSER_ERR_SYSTEM; + st->stat.st_size += r; + + len -= r; + data += r; + } + + return PARSER_ERR_OK; +} + +parser_t PARSER_BINARY = { + "Raw BINARY", + binary_init, + binary_open, + binary_close, + binary_size, + binary_read, + binary_write +}; + diff --git a/tools/macosx/src/stm32flash_serial/src/parsers/binary.h b/tools/macosx/src/stm32flash_serial/src/parsers/binary.h new file mode 100644 index 0000000..d989acf --- /dev/null +++ b/tools/macosx/src/stm32flash_serial/src/parsers/binary.h @@ -0,0 +1,27 @@ +/* + stm32flash - Open Source ST STM32 flash program for *nix + Copyright (C) 2010 Geoffrey McRae + + 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. +*/ + + +#ifndef _PARSER_BINARY_H +#define _PARSER_BINARY_H + +#include "parser.h" + +extern parser_t PARSER_BINARY; +#endif diff --git a/tools/macosx/src/stm32flash_serial/src/parsers/hex.c b/tools/macosx/src/stm32flash_serial/src/parsers/hex.c new file mode 100644 index 0000000..3baf856 --- /dev/null +++ b/tools/macosx/src/stm32flash_serial/src/parsers/hex.c @@ -0,0 +1,224 @@ +/* + stm32flash - Open Source ST STM32 flash program for *nix + Copyright (C) 2010 Geoffrey McRae + + 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. +*/ + + +#include +#include +#include +#include +#include +#include +#include + +#include "hex.h" +#include "../utils.h" + +typedef struct { + size_t data_len, offset; + uint8_t *data; + uint8_t base; +} hex_t; + +void* hex_init() { + return calloc(sizeof(hex_t), 1); +} + +parser_err_t hex_open(void *storage, const char *filename, const char write) { + hex_t *st = storage; + if (write) { + return PARSER_ERR_RDONLY; + } else { + char mark; + int i, fd; + uint8_t checksum; + unsigned int c; + uint32_t base = 0; + unsigned int last_address = 0x0; + + fd = open(filename, O_RDONLY); + if (fd < 0) + return PARSER_ERR_SYSTEM; + + /* read in the file */ + + while(read(fd, &mark, 1) != 0) { + if (mark == '\n' || mark == '\r') continue; + if (mark != ':') + return PARSER_ERR_INVALID_FILE; + + char buffer[9]; + unsigned int reclen, address, type; + uint8_t *record = NULL; + + /* get the reclen, address, and type */ + buffer[8] = 0; + if (read(fd, &buffer, 8) != 8) return PARSER_ERR_INVALID_FILE; + if (sscanf(buffer, "%2x%4x%2x", &reclen, &address, &type) != 3) { + close(fd); + return PARSER_ERR_INVALID_FILE; + } + + /* setup the checksum */ + checksum = + reclen + + ((address & 0xFF00) >> 8) + + ((address & 0x00FF) >> 0) + + type; + + switch(type) { + /* data record */ + case 0: + c = address - last_address; + st->data = realloc(st->data, st->data_len + c + reclen); + + /* if there is a gap, set it to 0xff and increment the length */ + if (c > 0) { + memset(&st->data[st->data_len], 0xff, c); + st->data_len += c; + } + + last_address = address + reclen; + record = &st->data[st->data_len]; + st->data_len += reclen; + break; + + /* extended segment address record */ + case 2: + base = 0; + break; + + /* extended linear address record */ + case 4: + base = address; + break; + } + + buffer[2] = 0; + for(i = 0; i < reclen; ++i) { + if (read(fd, &buffer, 2) != 2 || sscanf(buffer, "%2x", &c) != 1) { + close(fd); + return PARSER_ERR_INVALID_FILE; + } + + /* add the byte to the checksum */ + checksum += c; + + switch(type) { + case 0: + if (record != NULL) { + record[i] = c; + } else { + return PARSER_ERR_INVALID_FILE; + } + break; + + case 2: + case 4: + base = (base << 8) | c; + break; + } + } + + /* read, scan, and verify the checksum */ + if ( + read(fd, &buffer, 2 ) != 2 || + sscanf(buffer, "%2x", &c) != 1 || + (uint8_t)(checksum + c) != 0x00 + ) { + close(fd); + return PARSER_ERR_INVALID_FILE; + } + + switch(type) { + /* EOF */ + case 1: + close(fd); + return PARSER_ERR_OK; + + /* address record */ + case 2: base = base << 4; + case 4: base = be_u32(base); + /* Reset last_address since our base changed */ + last_address = 0; + + if (st->base == 0) { + st->base = base; + break; + } + + /* we cant cope with files out of order */ + if (base < st->base) { + close(fd); + return PARSER_ERR_INVALID_FILE; + } + + /* if there is a gap, enlarge and fill with zeros */ + unsigned int len = base - st->base; + if (len > st->data_len) { + st->data = realloc(st->data, len); + memset(&st->data[st->data_len], 0, len - st->data_len); + st->data_len = len; + } + break; + } + } + + close(fd); + return PARSER_ERR_OK; + } +} + +parser_err_t hex_close(void *storage) { + hex_t *st = storage; + if (st) free(st->data); + free(st); + return PARSER_ERR_OK; +} + +unsigned int hex_size(void *storage) { + hex_t *st = storage; + return st->data_len; +} + +parser_err_t hex_read(void *storage, void *data, unsigned int *len) { + hex_t *st = storage; + unsigned int left = st->data_len - st->offset; + unsigned int get = left > *len ? *len : left; + + memcpy(data, &st->data[st->offset], get); + st->offset += get; + + *len = get; + return PARSER_ERR_OK; +} + +parser_err_t hex_write(void *storage, void *data, unsigned int len) { + return PARSER_ERR_RDONLY; +} + +parser_t PARSER_HEX = { + "Intel HEX", + hex_init, + hex_open, + hex_close, + hex_size, + hex_read, + hex_write +}; + diff --git a/tools/macosx/src/stm32flash_serial/src/parsers/hex.h b/tools/macosx/src/stm32flash_serial/src/parsers/hex.h new file mode 100644 index 0000000..02413c9 --- /dev/null +++ b/tools/macosx/src/stm32flash_serial/src/parsers/hex.h @@ -0,0 +1,27 @@ +/* + stm32flash - Open Source ST STM32 flash program for *nix + Copyright (C) 2010 Geoffrey McRae + + 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. +*/ + + +#ifndef _PARSER_HEX_H +#define _PARSER_HEX_H + +#include "parser.h" + +extern parser_t PARSER_HEX; +#endif diff --git a/tools/macosx/src/stm32flash_serial/src/parsers/parser.h b/tools/macosx/src/stm32flash_serial/src/parsers/parser.h new file mode 100644 index 0000000..c2fae3c --- /dev/null +++ b/tools/macosx/src/stm32flash_serial/src/parsers/parser.h @@ -0,0 +1,56 @@ +/* + stm32flash - Open Source ST STM32 flash program for *nix + Copyright (C) 2010 Geoffrey McRae + + 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. +*/ + + +#ifndef _H_PARSER +#define _H_PARSER + +enum parser_err { + PARSER_ERR_OK, + PARSER_ERR_SYSTEM, + PARSER_ERR_INVALID_FILE, + PARSER_ERR_WRONLY, + PARSER_ERR_RDONLY +}; +typedef enum parser_err parser_err_t; + +struct parser { + const char *name; + void* (*init )(); /* initialise the parser */ + parser_err_t (*open )(void *storage, const char *filename, const char write); /* open the file for read|write */ + parser_err_t (*close)(void *storage); /* close and free the parser */ + unsigned int (*size )(void *storage); /* get the total data size */ + parser_err_t (*read )(void *storage, void *data, unsigned int *len); /* read a block of data */ + parser_err_t (*write)(void *storage, void *data, unsigned int len); /* write a block of data */ +}; +typedef struct parser parser_t; + +static inline const char* parser_errstr(parser_err_t err) { + switch(err) { + case PARSER_ERR_OK : return "OK"; + case PARSER_ERR_SYSTEM : return "System Error"; + case PARSER_ERR_INVALID_FILE: return "Invalid File"; + case PARSER_ERR_WRONLY : return "Parser can only write"; + case PARSER_ERR_RDONLY : return "Parser can only read"; + default: + return "Unknown Error"; + } +} + +#endif diff --git a/tools/macosx/src/stm32flash_serial/src/port.c b/tools/macosx/src/stm32flash_serial/src/port.c new file mode 100644 index 0000000..08e58cc --- /dev/null +++ b/tools/macosx/src/stm32flash_serial/src/port.c @@ -0,0 +1,59 @@ +/* + stm32flash - Open Source ST STM32 flash program for *nix + Copyright (C) 2014 Antonio Borneo + + 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. +*/ + +#include +#include + +#include "serial.h" +#include "port.h" + + +extern struct port_interface port_serial; +extern struct port_interface port_i2c; + +static struct port_interface *ports[] = { + &port_serial, + &port_i2c, + NULL, +}; + + +port_err_t port_open(struct port_options *ops, struct port_interface **outport) +{ + int ret; + static struct port_interface **port; + + for (port = ports; *port; port++) { + ret = (*port)->open(*port, ops); + if (ret == PORT_ERR_NODEV) + continue; + if (ret == PORT_ERR_OK) + break; + fprintf(stderr, "Error probing interface \"%s\"\n", + (*port)->name); + } + if (*port == NULL) { + fprintf(stderr, "Cannot handle device \"%s\"\n", + ops->device); + return PORT_ERR_UNKNOWN; + } + + *outport = *port; + return PORT_ERR_OK; +} diff --git a/tools/macosx/src/stm32flash_serial/src/port.h b/tools/macosx/src/stm32flash_serial/src/port.h new file mode 100644 index 0000000..290f034 --- /dev/null +++ b/tools/macosx/src/stm32flash_serial/src/port.h @@ -0,0 +1,75 @@ +/* + stm32flash - Open Source ST STM32 flash program for *nix + Copyright (C) 2014 Antonio Borneo + + 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. +*/ + + +#ifndef _H_PORT +#define _H_PORT + +typedef enum { + PORT_ERR_OK = 0, + PORT_ERR_NODEV, /* No such device */ + PORT_ERR_TIMEDOUT, /* Operation timed out */ + PORT_ERR_UNKNOWN, +} port_err_t; + +/* flags */ +#define PORT_BYTE (1 << 0) /* byte (not frame) oriented */ +#define PORT_GVR_ETX (1 << 1) /* cmd GVR returns protection status */ +#define PORT_CMD_INIT (1 << 2) /* use INIT cmd to autodetect speed */ +#define PORT_RETRY (1 << 3) /* allowed read() retry after timeout */ +#define PORT_STRETCH_W (1 << 4) /* warning for no-stretching commands */ + +/* all options and flags used to open and configure an interface */ +struct port_options { + const char *device; + serial_baud_t baudRate; + const char *serial_mode; + int bus_addr; + int rx_frame_max; + int tx_frame_max; +}; + +/* + * Specify the length of reply for command GET + * This is helpful for frame-oriented protocols, e.g. i2c, to avoid time + * consuming try-fail-timeout-retry operation. + * On byte-oriented protocols, i.e. UART, this information would be skipped + * after read the first byte, so not needed. + */ +struct varlen_cmd { + uint8_t version; + uint8_t length; +}; + +struct port_interface { + const char *name; + unsigned flags; + port_err_t (*open)(struct port_interface *port, struct port_options *ops); + port_err_t (*close)(struct port_interface *port); + port_err_t (*read)(struct port_interface *port, void *buf, size_t nbyte); + port_err_t (*write)(struct port_interface *port, void *buf, size_t nbyte); + port_err_t (*gpio)(struct port_interface *port, serial_gpio_t n, int level); + const char *(*get_cfg_str)(struct port_interface *port); + struct varlen_cmd *cmd_get_reply; + void *private; +}; + +port_err_t port_open(struct port_options *ops, struct port_interface **outport); + +#endif diff --git a/tools/macosx/src/stm32flash_serial/src/protocol.txt b/tools/macosx/src/stm32flash_serial/src/protocol.txt new file mode 100644 index 0000000..0391099 --- /dev/null +++ b/tools/macosx/src/stm32flash_serial/src/protocol.txt @@ -0,0 +1,19 @@ +The communication protocol used by ST bootloader is documented in following ST +application notes, depending on communication port. + +In current version of stm32flash are supported only UART and I2C ports. + +* AN3154: CAN protocol used in the STM32 bootloader + http://www.st.com/web/en/resource/technical/document/application_note/CD00264321.pdf + +* AN3155: USART protocol used in the STM32(TM) bootloader + http://www.st.com/web/en/resource/technical/document/application_note/CD00264342.pdf + +* AN4221: I2C protocol used in the STM32 bootloader + http://www.st.com/web/en/resource/technical/document/application_note/DM00072315.pdf + +* AN4286: SPI protocol used in the STM32 bootloader + http://www.st.com/web/en/resource/technical/document/application_note/DM00081379.pdf + +Boot mode selection for STM32 is documented in ST application note AN2606, available in ST website: + http://www.st.com/web/en/resource/technical/document/application_note/CD00167594.pdf diff --git a/tools/macosx/src/stm32flash_serial/src/serial.h b/tools/macosx/src/stm32flash_serial/src/serial.h new file mode 100644 index 0000000..227ba16 --- /dev/null +++ b/tools/macosx/src/stm32flash_serial/src/serial.h @@ -0,0 +1,90 @@ +/* + stm32flash - Open Source ST STM32 flash program for *nix + Copyright (C) 2010 Geoffrey McRae + + 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. +*/ + + +#ifndef _SERIAL_H +#define _SERIAL_H + +typedef struct serial serial_t; + +typedef enum { + SERIAL_PARITY_NONE, + SERIAL_PARITY_EVEN, + SERIAL_PARITY_ODD, + + SERIAL_PARITY_INVALID +} serial_parity_t; + +typedef enum { + SERIAL_BITS_5, + SERIAL_BITS_6, + SERIAL_BITS_7, + SERIAL_BITS_8, + + SERIAL_BITS_INVALID +} serial_bits_t; + +typedef enum { + SERIAL_BAUD_1200, + SERIAL_BAUD_1800, + SERIAL_BAUD_2400, + SERIAL_BAUD_4800, + SERIAL_BAUD_9600, + SERIAL_BAUD_19200, + SERIAL_BAUD_38400, + SERIAL_BAUD_57600, + SERIAL_BAUD_115200, + SERIAL_BAUD_128000, + SERIAL_BAUD_230400, + SERIAL_BAUD_256000, + SERIAL_BAUD_460800, + SERIAL_BAUD_500000, + SERIAL_BAUD_576000, + SERIAL_BAUD_921600, + SERIAL_BAUD_1000000, + SERIAL_BAUD_1500000, + SERIAL_BAUD_2000000, + + SERIAL_BAUD_INVALID +} serial_baud_t; + +typedef enum { + SERIAL_STOPBIT_1, + SERIAL_STOPBIT_2, + + SERIAL_STOPBIT_INVALID +} serial_stopbit_t; + +typedef enum { + GPIO_RTS = 1, + GPIO_DTR, + GPIO_BRK, +} serial_gpio_t; + +/* common helper functions */ +serial_baud_t serial_get_baud(const unsigned int baud); +unsigned int serial_get_baud_int(const serial_baud_t baud); +serial_bits_t serial_get_bits(const char *mode); +unsigned int serial_get_bits_int(const serial_bits_t bits); +serial_parity_t serial_get_parity(const char *mode); +char serial_get_parity_str(const serial_parity_t parity); +serial_stopbit_t serial_get_stopbit(const char *mode); +unsigned int serial_get_stopbit_int(const serial_stopbit_t stopbit); + +#endif diff --git a/tools/macosx/src/stm32flash_serial/src/serial_common.c b/tools/macosx/src/stm32flash_serial/src/serial_common.c new file mode 100644 index 0000000..43e48e1 --- /dev/null +++ b/tools/macosx/src/stm32flash_serial/src/serial_common.c @@ -0,0 +1,154 @@ +/* + stm32flash - Open Source ST STM32 flash program for *nix + Copyright (C) 2010 Geoffrey McRae + + 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. +*/ + +#include "serial.h" + +serial_baud_t serial_get_baud(const unsigned int baud) { + switch(baud) { + case 1200: return SERIAL_BAUD_1200 ; + case 1800: return SERIAL_BAUD_1800 ; + case 2400: return SERIAL_BAUD_2400 ; + case 4800: return SERIAL_BAUD_4800 ; + case 9600: return SERIAL_BAUD_9600 ; + case 19200: return SERIAL_BAUD_19200 ; + case 38400: return SERIAL_BAUD_38400 ; + case 57600: return SERIAL_BAUD_57600 ; + case 115200: return SERIAL_BAUD_115200; + case 128000: return SERIAL_BAUD_128000; + case 230400: return SERIAL_BAUD_230400; + case 256000: return SERIAL_BAUD_256000; + case 460800: return SERIAL_BAUD_460800; + case 500000: return SERIAL_BAUD_500000; + case 576000: return SERIAL_BAUD_576000; + case 921600: return SERIAL_BAUD_921600; + case 1000000: return SERIAL_BAUD_1000000; + case 1500000: return SERIAL_BAUD_1500000; + case 2000000: return SERIAL_BAUD_2000000; + + default: + return SERIAL_BAUD_INVALID; + } +} + +unsigned int serial_get_baud_int(const serial_baud_t baud) { + switch(baud) { + case SERIAL_BAUD_1200 : return 1200 ; + case SERIAL_BAUD_1800 : return 1800 ; + case SERIAL_BAUD_2400 : return 2400 ; + case SERIAL_BAUD_4800 : return 4800 ; + case SERIAL_BAUD_9600 : return 9600 ; + case SERIAL_BAUD_19200 : return 19200 ; + case SERIAL_BAUD_38400 : return 38400 ; + case SERIAL_BAUD_57600 : return 57600 ; + case SERIAL_BAUD_115200: return 115200; + case SERIAL_BAUD_128000: return 128000; + case SERIAL_BAUD_230400: return 230400; + case SERIAL_BAUD_256000: return 256000; + case SERIAL_BAUD_460800: return 460800; + case SERIAL_BAUD_500000: return 500000; + case SERIAL_BAUD_576000: return 576000; + case SERIAL_BAUD_921600: return 921600; + case SERIAL_BAUD_1000000: return 1000000; + case SERIAL_BAUD_1500000: return 1500000; + case SERIAL_BAUD_2000000: return 2000000; + + case SERIAL_BAUD_INVALID: + default: + return 0; + } +} + +serial_bits_t serial_get_bits(const char *mode) { + if (!mode) + return SERIAL_BITS_INVALID; + switch(mode[0]) { + case '5': return SERIAL_BITS_5; + case '6': return SERIAL_BITS_6; + case '7': return SERIAL_BITS_7; + case '8': return SERIAL_BITS_8; + + default: + return SERIAL_BITS_INVALID; + } +} + +unsigned int serial_get_bits_int(const serial_bits_t bits) { + switch(bits) { + case SERIAL_BITS_5: return 5; + case SERIAL_BITS_6: return 6; + case SERIAL_BITS_7: return 7; + case SERIAL_BITS_8: return 8; + + default: + return 0; + } +} + +serial_parity_t serial_get_parity(const char *mode) { + if (!mode || !mode[0]) + return SERIAL_PARITY_INVALID; + switch(mode[1]) { + case 'N': + case 'n': + return SERIAL_PARITY_NONE; + case 'E': + case 'e': + return SERIAL_PARITY_EVEN; + case 'O': + case 'o': + return SERIAL_PARITY_ODD; + + default: + return SERIAL_PARITY_INVALID; + } +} + +char serial_get_parity_str(const serial_parity_t parity) { + switch(parity) { + case SERIAL_PARITY_NONE: return 'N'; + case SERIAL_PARITY_EVEN: return 'E'; + case SERIAL_PARITY_ODD : return 'O'; + + default: + return ' '; + } +} + +serial_stopbit_t serial_get_stopbit(const char *mode) { + if (!mode || !mode[0] || !mode[1]) + return SERIAL_STOPBIT_INVALID; + switch(mode[2]) { + case '1': return SERIAL_STOPBIT_1; + case '2': return SERIAL_STOPBIT_2; + + default: + return SERIAL_STOPBIT_INVALID; + } +} + +unsigned int serial_get_stopbit_int(const serial_stopbit_t stopbit) { + switch(stopbit) { + case SERIAL_STOPBIT_1: return 1; + case SERIAL_STOPBIT_2: return 2; + + default: + return 0; + } +} + diff --git a/tools/macosx/src/stm32flash_serial/src/serial_platform.c b/tools/macosx/src/stm32flash_serial/src/serial_platform.c new file mode 100644 index 0000000..98e2569 --- /dev/null +++ b/tools/macosx/src/stm32flash_serial/src/serial_platform.c @@ -0,0 +1,5 @@ +#if defined(__WIN32__) || defined(__CYGWIN__) +# include "serial_w32.c" +#else +# include "serial_posix.c" +#endif diff --git a/tools/macosx/src/stm32flash_serial/src/serial_posix.c b/tools/macosx/src/stm32flash_serial/src/serial_posix.c new file mode 100644 index 0000000..284b35b --- /dev/null +++ b/tools/macosx/src/stm32flash_serial/src/serial_posix.c @@ -0,0 +1,395 @@ +/* + stm32flash - Open Source ST STM32 flash program for *nix + Copyright (C) 2010 Geoffrey McRae + + 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. +*/ + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "serial.h" +#include "port.h" + +struct serial { + int fd; + struct termios oldtio; + struct termios newtio; + char setup_str[11]; +}; + +static serial_t *serial_open(const char *device) +{ + serial_t *h = calloc(sizeof(serial_t), 1); + + h->fd = open(device, O_RDWR | O_NOCTTY | O_NDELAY); + if (h->fd < 0) { + free(h); + return NULL; + } + fcntl(h->fd, F_SETFL, 0); + + tcgetattr(h->fd, &h->oldtio); + tcgetattr(h->fd, &h->newtio); + + return h; +} + +static void serial_flush(const serial_t *h) +{ + tcflush(h->fd, TCIFLUSH); +} + +static void serial_close(serial_t *h) +{ + serial_flush(h); + tcsetattr(h->fd, TCSANOW, &h->oldtio); + close(h->fd); + free(h); +} + +static port_err_t serial_setup(serial_t *h, const serial_baud_t baud, + const serial_bits_t bits, + const serial_parity_t parity, + const serial_stopbit_t stopbit) +{ + speed_t port_baud; + tcflag_t port_bits; + tcflag_t port_parity; + tcflag_t port_stop; + struct termios settings; + + switch (baud) { + case SERIAL_BAUD_1200: port_baud = B1200; break; + case SERIAL_BAUD_1800: port_baud = B1800; break; + case SERIAL_BAUD_2400: port_baud = B2400; break; + case SERIAL_BAUD_4800: port_baud = B4800; break; + case SERIAL_BAUD_9600: port_baud = B9600; break; + case SERIAL_BAUD_19200: port_baud = B19200; break; + case SERIAL_BAUD_38400: port_baud = B38400; break; + case SERIAL_BAUD_57600: port_baud = B57600; break; + case SERIAL_BAUD_115200: port_baud = B115200; break; + case SERIAL_BAUD_230400: port_baud = B230400; break; +#ifdef B460800 + case SERIAL_BAUD_460800: port_baud = B460800; break; +#endif /* B460800 */ +#ifdef B921600 + case SERIAL_BAUD_921600: port_baud = B921600; break; +#endif /* B921600 */ +#ifdef B500000 + case SERIAL_BAUD_500000: port_baud = B500000; break; +#endif /* B500000 */ +#ifdef B576000 + case SERIAL_BAUD_576000: port_baud = B576000; break; +#endif /* B576000 */ +#ifdef B1000000 + case SERIAL_BAUD_1000000: port_baud = B1000000; break; +#endif /* B1000000 */ +#ifdef B1500000 + case SERIAL_BAUD_1500000: port_baud = B1500000; break; +#endif /* B1500000 */ +#ifdef B2000000 + case SERIAL_BAUD_2000000: port_baud = B2000000; break; +#endif /* B2000000 */ + + case SERIAL_BAUD_INVALID: + default: + return PORT_ERR_UNKNOWN; + } + + switch (bits) { + case SERIAL_BITS_5: port_bits = CS5; break; + case SERIAL_BITS_6: port_bits = CS6; break; + case SERIAL_BITS_7: port_bits = CS7; break; + case SERIAL_BITS_8: port_bits = CS8; break; + + default: + return PORT_ERR_UNKNOWN; + } + + switch (parity) { + case SERIAL_PARITY_NONE: port_parity = 0; break; + case SERIAL_PARITY_EVEN: port_parity = PARENB; break; + case SERIAL_PARITY_ODD: port_parity = PARENB | PARODD; break; + + default: + return PORT_ERR_UNKNOWN; + } + + switch (stopbit) { + case SERIAL_STOPBIT_1: port_stop = 0; break; + case SERIAL_STOPBIT_2: port_stop = CSTOPB; break; + + default: + return PORT_ERR_UNKNOWN; + } + + /* reset the settings */ +#ifndef __sun /* Used by GNU and BSD. Ignore __SVR4 in test. */ + cfmakeraw(&h->newtio); +#else /* __sun */ + h->newtio.c_iflag &= ~(IGNBRK | BRKINT | PARMRK | ISTRIP | INLCR + | IGNCR | ICRNL | IXON); + if (port_parity) + h->newtio.c_iflag |= INPCK; + + h->newtio.c_oflag &= ~OPOST; + h->newtio.c_lflag &= ~(ECHO | ECHONL | ICANON | ISIG | IEXTEN); + h->newtio.c_cflag &= ~(CSIZE | PARENB); + h->newtio.c_cflag |= CS8; +#endif /* __sun */ +#ifdef __QNXNTO__ + h->newtio.c_cflag &= ~(CSIZE | IHFLOW | OHFLOW); +#else + h->newtio.c_cflag &= ~(CSIZE | CRTSCTS); +#endif + h->newtio.c_cflag &= ~(CSIZE | CRTSCTS); + h->newtio.c_iflag &= ~(IXON | IXOFF | IXANY | IGNPAR); + h->newtio.c_lflag &= ~(ECHOK | ECHOCTL | ECHOKE); + h->newtio.c_oflag &= ~(OPOST | ONLCR); + + /* setup the new settings */ + cfsetispeed(&h->newtio, port_baud); + cfsetospeed(&h->newtio, port_baud); + h->newtio.c_cflag |= + port_parity | + port_bits | + port_stop | + CLOCAL | + CREAD; + + h->newtio.c_cc[VMIN] = 0; + h->newtio.c_cc[VTIME] = 5; /* in units of 0.1 s */ + + /* set the settings */ + serial_flush(h); + if (tcsetattr(h->fd, TCSANOW, &h->newtio) != 0) + return PORT_ERR_UNKNOWN; + +/* this check fails on CDC-ACM devices, bits 16 and 17 of cflag differ! + * it has been disabled below for now -jcw, 2015-11-09 + if (settings.c_cflag != h->newtio.c_cflag) + fprintf(stderr, "c_cflag mismatch %lx\n", + settings.c_cflag ^ h->newtio.c_cflag); + */ + + /* confirm they were set */ + tcgetattr(h->fd, &settings); + if (settings.c_iflag != h->newtio.c_iflag || + settings.c_oflag != h->newtio.c_oflag || + //settings.c_cflag != h->newtio.c_cflag || + settings.c_lflag != h->newtio.c_lflag) + return PORT_ERR_UNKNOWN; + + snprintf(h->setup_str, sizeof(h->setup_str), "%u %d%c%d", + serial_get_baud_int(baud), + serial_get_bits_int(bits), + serial_get_parity_str(parity), + serial_get_stopbit_int(stopbit)); + return PORT_ERR_OK; +} + +/* + * Roger clark. + * This function is no longer used. But has just been commented out in case it needs + * to be reinstated in the future + +static int startswith(const char *haystack, const char *needle) { + return strncmp(haystack, needle, strlen(needle)) == 0; +} +*/ + +static int is_tty(const char *path) { + char resolved[PATH_MAX]; + + if(!realpath(path, resolved)) return 0; + + + /* + * Roger Clark + * Commented out this check, because on OSX some devices are /dev/cu + * and some users use symbolic links to devices, hence the name may not even start + * with /dev + + if(startswith(resolved, "/dev/tty")) return 1; + + return 0; + */ + + return 1; +} + +static port_err_t serial_posix_open(struct port_interface *port, + struct port_options *ops) +{ + serial_t *h; + + /* 1. check device name match */ + if (!is_tty(ops->device)) + return PORT_ERR_NODEV; + + /* 2. check options */ + if (ops->baudRate == SERIAL_BAUD_INVALID) + return PORT_ERR_UNKNOWN; + if (serial_get_bits(ops->serial_mode) == SERIAL_BITS_INVALID) + return PORT_ERR_UNKNOWN; + if (serial_get_parity(ops->serial_mode) == SERIAL_PARITY_INVALID) + return PORT_ERR_UNKNOWN; + if (serial_get_stopbit(ops->serial_mode) == SERIAL_STOPBIT_INVALID) + return PORT_ERR_UNKNOWN; + + /* 3. open it */ + h = serial_open(ops->device); + if (h == NULL) + return PORT_ERR_UNKNOWN; + + /* 4. set options */ + if (serial_setup(h, ops->baudRate, + serial_get_bits(ops->serial_mode), + serial_get_parity(ops->serial_mode), + serial_get_stopbit(ops->serial_mode) + ) != PORT_ERR_OK) { + serial_close(h); + return PORT_ERR_UNKNOWN; + } + + port->private = h; + return PORT_ERR_OK; +} + +static port_err_t serial_posix_close(struct port_interface *port) +{ + serial_t *h; + + h = (serial_t *)port->private; + if (h == NULL) + return PORT_ERR_UNKNOWN; + + serial_close(h); + port->private = NULL; + return PORT_ERR_OK; +} + +static port_err_t serial_posix_read(struct port_interface *port, void *buf, + size_t nbyte) +{ + serial_t *h; + ssize_t r; + uint8_t *pos = (uint8_t *)buf; + + h = (serial_t *)port->private; + if (h == NULL) + return PORT_ERR_UNKNOWN; + + while (nbyte) { + r = read(h->fd, pos, nbyte); + if (r == 0) + return PORT_ERR_TIMEDOUT; + if (r < 0) + return PORT_ERR_UNKNOWN; + + nbyte -= r; + pos += r; + } + return PORT_ERR_OK; +} + +static port_err_t serial_posix_write(struct port_interface *port, void *buf, + size_t nbyte) +{ + serial_t *h; + ssize_t r; + const uint8_t *pos = (const uint8_t *)buf; + + h = (serial_t *)port->private; + if (h == NULL) + return PORT_ERR_UNKNOWN; + + while (nbyte) { + r = write(h->fd, pos, nbyte); + if (r < 1) + return PORT_ERR_UNKNOWN; + + nbyte -= r; + pos += r; + } + return PORT_ERR_OK; +} + +static port_err_t serial_posix_gpio(struct port_interface *port, + serial_gpio_t n, int level) +{ + serial_t *h; + int bit, lines; + + h = (serial_t *)port->private; + if (h == NULL) + return PORT_ERR_UNKNOWN; + + switch (n) { + case GPIO_RTS: + bit = TIOCM_RTS; + break; + + case GPIO_DTR: + bit = TIOCM_DTR; + break; + + case GPIO_BRK: + if (level == 0) + return PORT_ERR_OK; + if (tcsendbreak(h->fd, 1)) + return PORT_ERR_UNKNOWN; + return PORT_ERR_OK; + + default: + return PORT_ERR_UNKNOWN; + } + + /* handle RTS/DTR */ + if (ioctl(h->fd, TIOCMGET, &lines)) + return PORT_ERR_UNKNOWN; + lines = level ? lines | bit : lines & ~bit; + if (ioctl(h->fd, TIOCMSET, &lines)) + return PORT_ERR_UNKNOWN; + + return PORT_ERR_OK; +} + +static const char *serial_posix_get_cfg_str(struct port_interface *port) +{ + serial_t *h; + + h = (serial_t *)port->private; + return h ? h->setup_str : "INVALID"; +} + +struct port_interface port_serial = { + .name = "serial_posix", + .flags = PORT_BYTE | PORT_GVR_ETX | PORT_CMD_INIT | PORT_RETRY, + .open = serial_posix_open, + .close = serial_posix_close, + .read = serial_posix_read, + .write = serial_posix_write, + .gpio = serial_posix_gpio, + .get_cfg_str = serial_posix_get_cfg_str, +}; diff --git a/tools/macosx/src/stm32flash_serial/src/serial_w32.c b/tools/macosx/src/stm32flash_serial/src/serial_w32.c new file mode 100644 index 0000000..56772c0 --- /dev/null +++ b/tools/macosx/src/stm32flash_serial/src/serial_w32.c @@ -0,0 +1,341 @@ +/* + stm32flash - Open Source ST STM32 flash program for *nix + Copyright (C) 2010 Geoffrey McRae + Copyright (C) 2010 Gareth McMullin + + 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. +*/ + +#include +#include +#include +#include +#include +#include +#include +#include + +#include "serial.h" +#include "port.h" + +struct serial { + HANDLE fd; + DCB oldtio; + DCB newtio; + char setup_str[11]; +}; + +static serial_t *serial_open(const char *device) +{ + serial_t *h = calloc(sizeof(serial_t), 1); + char *devName; + + /* timeout in ms */ + COMMTIMEOUTS timeouts = {MAXDWORD, MAXDWORD, 500, 0, 0}; + + /* Fix the device name if required */ + if (strlen(device) > 4 && device[0] != '\\') { + devName = calloc(1, strlen(device) + 5); + sprintf(devName, "\\\\.\\%s", device); + } else { + devName = (char *)device; + } + + /* Create file handle for port */ + h->fd = CreateFile(devName, GENERIC_READ | GENERIC_WRITE, + 0, /* Exclusive access */ + NULL, /* No security */ + OPEN_EXISTING, + 0, /* No overlap */ + NULL); + + if (devName != device) + free(devName); + + if (h->fd == INVALID_HANDLE_VALUE) { + if (GetLastError() == ERROR_FILE_NOT_FOUND) + fprintf(stderr, "File not found: %s\n", device); + return NULL; + } + + SetupComm(h->fd, 4096, 4096); /* Set input and output buffer size */ + + SetCommTimeouts(h->fd, &timeouts); + + SetCommMask(h->fd, EV_ERR); /* Notify us of error events */ + + GetCommState(h->fd, &h->oldtio); /* Retrieve port parameters */ + GetCommState(h->fd, &h->newtio); /* Retrieve port parameters */ + + /* PurgeComm(h->fd, PURGE_RXABORT | PURGE_TXCLEAR | PURGE_TXABORT | PURGE_TXCLEAR); */ + + return h; +} + +static void serial_flush(const serial_t *h) +{ + /* We shouldn't need to flush in non-overlapping (blocking) mode */ + /* tcflush(h->fd, TCIFLUSH); */ +} + +static void serial_close(serial_t *h) +{ + serial_flush(h); + SetCommState(h->fd, &h->oldtio); + CloseHandle(h->fd); + free(h); +} + +static port_err_t serial_setup(serial_t *h, + const serial_baud_t baud, + const serial_bits_t bits, + const serial_parity_t parity, + const serial_stopbit_t stopbit) +{ + switch (baud) { + case SERIAL_BAUD_1200: h->newtio.BaudRate = CBR_1200; break; + /* case SERIAL_BAUD_1800: h->newtio.BaudRate = CBR_1800; break; */ + case SERIAL_BAUD_2400: h->newtio.BaudRate = CBR_2400; break; + case SERIAL_BAUD_4800: h->newtio.BaudRate = CBR_4800; break; + case SERIAL_BAUD_9600: h->newtio.BaudRate = CBR_9600; break; + case SERIAL_BAUD_19200: h->newtio.BaudRate = CBR_19200; break; + case SERIAL_BAUD_38400: h->newtio.BaudRate = CBR_38400; break; + case SERIAL_BAUD_57600: h->newtio.BaudRate = CBR_57600; break; + case SERIAL_BAUD_115200: h->newtio.BaudRate = CBR_115200; break; + case SERIAL_BAUD_128000: h->newtio.BaudRate = CBR_128000; break; + case SERIAL_BAUD_256000: h->newtio.BaudRate = CBR_256000; break; + /* These are not defined in WinBase.h and might work or not */ + case SERIAL_BAUD_230400: h->newtio.BaudRate = 230400; break; + case SERIAL_BAUD_460800: h->newtio.BaudRate = 460800; break; + case SERIAL_BAUD_500000: h->newtio.BaudRate = 500000; break; + case SERIAL_BAUD_576000: h->newtio.BaudRate = 576000; break; + case SERIAL_BAUD_921600: h->newtio.BaudRate = 921600; break; + case SERIAL_BAUD_1000000: h->newtio.BaudRate = 1000000; break; + case SERIAL_BAUD_1500000: h->newtio.BaudRate = 1500000; break; + case SERIAL_BAUD_2000000: h->newtio.BaudRate = 2000000; break; + case SERIAL_BAUD_INVALID: + + default: + return PORT_ERR_UNKNOWN; + } + + switch (bits) { + case SERIAL_BITS_5: h->newtio.ByteSize = 5; break; + case SERIAL_BITS_6: h->newtio.ByteSize = 6; break; + case SERIAL_BITS_7: h->newtio.ByteSize = 7; break; + case SERIAL_BITS_8: h->newtio.ByteSize = 8; break; + + default: + return PORT_ERR_UNKNOWN; + } + + switch (parity) { + case SERIAL_PARITY_NONE: h->newtio.Parity = NOPARITY; break; + case SERIAL_PARITY_EVEN: h->newtio.Parity = EVENPARITY; break; + case SERIAL_PARITY_ODD: h->newtio.Parity = ODDPARITY; break; + + default: + return PORT_ERR_UNKNOWN; + } + + switch (stopbit) { + case SERIAL_STOPBIT_1: h->newtio.StopBits = ONESTOPBIT; break; + case SERIAL_STOPBIT_2: h->newtio.StopBits = TWOSTOPBITS; break; + + default: + return PORT_ERR_UNKNOWN; + } + + /* reset the settings */ + h->newtio.fOutxCtsFlow = FALSE; + h->newtio.fOutxDsrFlow = FALSE; + h->newtio.fOutX = FALSE; + h->newtio.fInX = FALSE; + h->newtio.fNull = 0; + h->newtio.fAbortOnError = 0; + + /* set the settings */ + serial_flush(h); + if (!SetCommState(h->fd, &h->newtio)) + return PORT_ERR_UNKNOWN; + + snprintf(h->setup_str, sizeof(h->setup_str), "%u %d%c%d", + serial_get_baud_int(baud), + serial_get_bits_int(bits), + serial_get_parity_str(parity), + serial_get_stopbit_int(stopbit) + ); + return PORT_ERR_OK; +} + +static port_err_t serial_w32_open(struct port_interface *port, + struct port_options *ops) +{ + serial_t *h; + + /* 1. check device name match */ + if (!((strlen(ops->device) == 4 || strlen(ops->device) == 5) + && !strncmp(ops->device, "COM", 3) && isdigit(ops->device[3])) + && !(!strncmp(ops->device, "\\\\.\\COM", strlen("\\\\.\\COM")) + && isdigit(ops->device[strlen("\\\\.\\COM")]))) + return PORT_ERR_NODEV; + + /* 2. check options */ + if (ops->baudRate == SERIAL_BAUD_INVALID) + return PORT_ERR_UNKNOWN; + if (serial_get_bits(ops->serial_mode) == SERIAL_BITS_INVALID) + return PORT_ERR_UNKNOWN; + if (serial_get_parity(ops->serial_mode) == SERIAL_PARITY_INVALID) + return PORT_ERR_UNKNOWN; + if (serial_get_stopbit(ops->serial_mode) == SERIAL_STOPBIT_INVALID) + return PORT_ERR_UNKNOWN; + + /* 3. open it */ + h = serial_open(ops->device); + if (h == NULL) + return PORT_ERR_UNKNOWN; + + /* 4. set options */ + if (serial_setup(h, ops->baudRate, + serial_get_bits(ops->serial_mode), + serial_get_parity(ops->serial_mode), + serial_get_stopbit(ops->serial_mode) + ) != PORT_ERR_OK) { + serial_close(h); + return PORT_ERR_UNKNOWN; + } + + port->private = h; + return PORT_ERR_OK; +} + +static port_err_t serial_w32_close(struct port_interface *port) +{ + serial_t *h; + + h = (serial_t *)port->private; + if (h == NULL) + return PORT_ERR_UNKNOWN; + + serial_close(h); + port->private = NULL; + return PORT_ERR_OK; +} + +static port_err_t serial_w32_read(struct port_interface *port, void *buf, + size_t nbyte) +{ + serial_t *h; + DWORD r; + uint8_t *pos = (uint8_t *)buf; + + h = (serial_t *)port->private; + if (h == NULL) + return PORT_ERR_UNKNOWN; + + while (nbyte) { + ReadFile(h->fd, pos, nbyte, &r, NULL); + if (r == 0) + return PORT_ERR_TIMEDOUT; + if (r < 0) + return PORT_ERR_UNKNOWN; + + nbyte -= r; + pos += r; + } + return PORT_ERR_OK; +} + +static port_err_t serial_w32_write(struct port_interface *port, void *buf, + size_t nbyte) +{ + serial_t *h; + DWORD r; + uint8_t *pos = (uint8_t *)buf; + + h = (serial_t *)port->private; + if (h == NULL) + return PORT_ERR_UNKNOWN; + + while (nbyte) { + if (!WriteFile(h->fd, pos, nbyte, &r, NULL)) + return PORT_ERR_UNKNOWN; + if (r < 1) + return PORT_ERR_UNKNOWN; + + nbyte -= r; + pos += r; + } + return PORT_ERR_OK; +} + +static port_err_t serial_w32_gpio(struct port_interface *port, + serial_gpio_t n, int level) +{ + serial_t *h; + int bit; + + h = (serial_t *)port->private; + if (h == NULL) + return PORT_ERR_UNKNOWN; + + switch (n) { + case GPIO_RTS: + bit = level ? SETRTS : CLRRTS; + break; + + case GPIO_DTR: + bit = level ? SETDTR : CLRDTR; + break; + + case GPIO_BRK: + if (level == 0) + return PORT_ERR_OK; + if (EscapeCommFunction(h->fd, SETBREAK) == 0) + return PORT_ERR_UNKNOWN; + usleep(500000); + if (EscapeCommFunction(h->fd, CLRBREAK) == 0) + return PORT_ERR_UNKNOWN; + return PORT_ERR_OK; + + default: + return PORT_ERR_UNKNOWN; + } + + /* handle RTS/DTR */ + if (EscapeCommFunction(h->fd, bit) == 0) + return PORT_ERR_UNKNOWN; + + return PORT_ERR_OK; +} + +static const char *serial_w32_get_cfg_str(struct port_interface *port) +{ + serial_t *h; + + h = (serial_t *)port->private; + return h ? h->setup_str : "INVALID"; +} + +struct port_interface port_serial = { + .name = "serial_w32", + .flags = PORT_BYTE | PORT_GVR_ETX | PORT_CMD_INIT | PORT_RETRY, + .open = serial_w32_open, + .close = serial_w32_close, + .read = serial_w32_read, + .write = serial_w32_write, + .gpio = serial_w32_gpio, + .get_cfg_str = serial_w32_get_cfg_str, +}; diff --git a/tools/macosx/src/stm32flash_serial/src/stm32.c b/tools/macosx/src/stm32flash_serial/src/stm32.c new file mode 100644 index 0000000..74047d2 --- /dev/null +++ b/tools/macosx/src/stm32flash_serial/src/stm32.c @@ -0,0 +1,1048 @@ +/* + stm32flash - Open Source ST STM32 flash program for *nix + Copyright 2010 Geoffrey McRae + Copyright 2012-2014 Tormod Volden + + 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. +*/ + +#include +#include +#include +#include +#include +#include + +#include "stm32.h" +#include "port.h" +#include "utils.h" + +#define STM32_ACK 0x79 +#define STM32_NACK 0x1F +#define STM32_BUSY 0x76 + +#define STM32_CMD_INIT 0x7F +#define STM32_CMD_GET 0x00 /* get the version and command supported */ +#define STM32_CMD_GVR 0x01 /* get version and read protection status */ +#define STM32_CMD_GID 0x02 /* get ID */ +#define STM32_CMD_RM 0x11 /* read memory */ +#define STM32_CMD_GO 0x21 /* go */ +#define STM32_CMD_WM 0x31 /* write memory */ +#define STM32_CMD_WM_NS 0x32 /* no-stretch write memory */ +#define STM32_CMD_ER 0x43 /* erase */ +#define STM32_CMD_EE 0x44 /* extended erase */ +#define STM32_CMD_EE_NS 0x45 /* extended erase no-stretch */ +#define STM32_CMD_WP 0x63 /* write protect */ +#define STM32_CMD_WP_NS 0x64 /* write protect no-stretch */ +#define STM32_CMD_UW 0x73 /* write unprotect */ +#define STM32_CMD_UW_NS 0x74 /* write unprotect no-stretch */ +#define STM32_CMD_RP 0x82 /* readout protect */ +#define STM32_CMD_RP_NS 0x83 /* readout protect no-stretch */ +#define STM32_CMD_UR 0x92 /* readout unprotect */ +#define STM32_CMD_UR_NS 0x93 /* readout unprotect no-stretch */ +#define STM32_CMD_CRC 0xA1 /* compute CRC */ +#define STM32_CMD_ERR 0xFF /* not a valid command */ + +#define STM32_RESYNC_TIMEOUT 35 /* seconds */ +#define STM32_MASSERASE_TIMEOUT 35 /* seconds */ +#define STM32_SECTERASE_TIMEOUT 5 /* seconds */ +#define STM32_BLKWRITE_TIMEOUT 1 /* seconds */ +#define STM32_WUNPROT_TIMEOUT 1 /* seconds */ +#define STM32_WPROT_TIMEOUT 1 /* seconds */ +#define STM32_RPROT_TIMEOUT 1 /* seconds */ + +#define STM32_CMD_GET_LENGTH 17 /* bytes in the reply */ + +struct stm32_cmd { + uint8_t get; + uint8_t gvr; + uint8_t gid; + uint8_t rm; + uint8_t go; + uint8_t wm; + uint8_t er; /* this may be extended erase */ + uint8_t wp; + uint8_t uw; + uint8_t rp; + uint8_t ur; + uint8_t crc; +}; + +/* Reset code for ARMv7-M (Cortex-M3) and ARMv6-M (Cortex-M0) + * see ARMv7-M or ARMv6-M Architecture Reference Manual (table B3-8) + * or "The definitive guide to the ARM Cortex-M3", section 14.4. + */ +static const uint8_t stm_reset_code[] = { + 0x01, 0x49, // ldr r1, [pc, #4] ; () + 0x02, 0x4A, // ldr r2, [pc, #8] ; () + 0x0A, 0x60, // str r2, [r1, #0] + 0xfe, 0xe7, // endless: b endless + 0x0c, 0xed, 0x00, 0xe0, // .word 0xe000ed0c = NVIC AIRCR register address + 0x04, 0x00, 0xfa, 0x05 // .word 0x05fa0004 = VECTKEY | SYSRESETREQ +}; + +static const uint32_t stm_reset_code_length = sizeof(stm_reset_code); + +extern const stm32_dev_t devices[]; + +static void stm32_warn_stretching(const char *f) +{ + fprintf(stderr, "Attention !!!\n"); + fprintf(stderr, "\tThis %s error could be caused by your I2C\n", f); + fprintf(stderr, "\tcontroller not accepting \"clock stretching\"\n"); + fprintf(stderr, "\tas required by bootloader.\n"); + fprintf(stderr, "\tCheck \"I2C.txt\" in stm32flash source code.\n"); +} + +static stm32_err_t stm32_get_ack_timeout(const stm32_t *stm, time_t timeout) +{ + struct port_interface *port = stm->port; + uint8_t byte; + port_err_t p_err; + time_t t0, t1; + + if (!(port->flags & PORT_RETRY)) + timeout = 0; + + if (timeout) + time(&t0); + + do { + p_err = port->read(port, &byte, 1); + if (p_err == PORT_ERR_TIMEDOUT && timeout) { + time(&t1); + if (t1 < t0 + timeout) + continue; + } + + if (p_err != PORT_ERR_OK) { + fprintf(stderr, "Failed to read ACK byte\n"); + return STM32_ERR_UNKNOWN; + } + + if (byte == STM32_ACK) + return STM32_ERR_OK; + if (byte == STM32_NACK) + return STM32_ERR_NACK; + if (byte != STM32_BUSY) { + fprintf(stderr, "Got byte 0x%02x instead of ACK\n", + byte); + return STM32_ERR_UNKNOWN; + } + } while (1); +} + +static stm32_err_t stm32_get_ack(const stm32_t *stm) +{ + return stm32_get_ack_timeout(stm, 0); +} + +static stm32_err_t stm32_send_command_timeout(const stm32_t *stm, + const uint8_t cmd, + time_t timeout) +{ + struct port_interface *port = stm->port; + stm32_err_t s_err; + port_err_t p_err; + uint8_t buf[2]; + + buf[0] = cmd; + buf[1] = cmd ^ 0xFF; + p_err = port->write(port, buf, 2); + if (p_err != PORT_ERR_OK) { + fprintf(stderr, "Failed to send command\n"); + return STM32_ERR_UNKNOWN; + } + s_err = stm32_get_ack_timeout(stm, timeout); + if (s_err == STM32_ERR_OK) + return STM32_ERR_OK; + if (s_err == STM32_ERR_NACK) + fprintf(stderr, "Got NACK from device on command 0x%02x\n", cmd); + else + fprintf(stderr, "Unexpected reply from device on command 0x%02x\n", cmd); + return STM32_ERR_UNKNOWN; +} + +static stm32_err_t stm32_send_command(const stm32_t *stm, const uint8_t cmd) +{ + return stm32_send_command_timeout(stm, cmd, 0); +} + +/* if we have lost sync, send a wrong command and expect a NACK */ +static stm32_err_t stm32_resync(const stm32_t *stm) +{ + struct port_interface *port = stm->port; + port_err_t p_err; + uint8_t buf[2], ack; + time_t t0, t1; + + time(&t0); + t1 = t0; + + buf[0] = STM32_CMD_ERR; + buf[1] = STM32_CMD_ERR ^ 0xFF; + while (t1 < t0 + STM32_RESYNC_TIMEOUT) { + p_err = port->write(port, buf, 2); + if (p_err != PORT_ERR_OK) { + usleep(500000); + time(&t1); + continue; + } + p_err = port->read(port, &ack, 1); + if (p_err != PORT_ERR_OK) { + time(&t1); + continue; + } + if (ack == STM32_NACK) + return STM32_ERR_OK; + time(&t1); + } + return STM32_ERR_UNKNOWN; +} + +/* + * some command receive reply frame with variable length, and length is + * embedded in reply frame itself. + * We can guess the length, but if we guess wrong the protocol gets out + * of sync. + * Use resync for frame oriented interfaces (e.g. I2C) and byte-by-byte + * read for byte oriented interfaces (e.g. UART). + * + * to run safely, data buffer should be allocated for 256+1 bytes + * + * len is value of the first byte in the frame. + */ +static stm32_err_t stm32_guess_len_cmd(const stm32_t *stm, uint8_t cmd, + uint8_t *data, unsigned int len) +{ + struct port_interface *port = stm->port; + port_err_t p_err; + + if (stm32_send_command(stm, cmd) != STM32_ERR_OK) + return STM32_ERR_UNKNOWN; + if (port->flags & PORT_BYTE) { + /* interface is UART-like */ + p_err = port->read(port, data, 1); + if (p_err != PORT_ERR_OK) + return STM32_ERR_UNKNOWN; + len = data[0]; + p_err = port->read(port, data + 1, len + 1); + if (p_err != PORT_ERR_OK) + return STM32_ERR_UNKNOWN; + return STM32_ERR_OK; + } + + p_err = port->read(port, data, len + 2); + if (p_err == PORT_ERR_OK && len == data[0]) + return STM32_ERR_OK; + if (p_err != PORT_ERR_OK) { + /* restart with only one byte */ + if (stm32_resync(stm) != STM32_ERR_OK) + return STM32_ERR_UNKNOWN; + if (stm32_send_command(stm, cmd) != STM32_ERR_OK) + return STM32_ERR_UNKNOWN; + p_err = port->read(port, data, 1); + if (p_err != PORT_ERR_OK) + return STM32_ERR_UNKNOWN; + } + + fprintf(stderr, "Re sync (len = %d)\n", data[0]); + if (stm32_resync(stm) != STM32_ERR_OK) + return STM32_ERR_UNKNOWN; + + len = data[0]; + if (stm32_send_command(stm, cmd) != STM32_ERR_OK) + return STM32_ERR_UNKNOWN; + p_err = port->read(port, data, len + 2); + if (p_err != PORT_ERR_OK) + return STM32_ERR_UNKNOWN; + return STM32_ERR_OK; +} + +/* + * Some interface, e.g. UART, requires a specific init sequence to let STM32 + * autodetect the interface speed. + * The sequence is only required one time after reset. + * stm32flash has command line flag "-c" to prevent sending the init sequence + * in case it was already sent before. + * User can easily forget adding "-c". In this case the bootloader would + * interpret the init sequence as part of a command message, then waiting for + * the rest of the message blocking the interface. + * This function sends the init sequence and, in case of timeout, recovers + * the interface. + */ +static stm32_err_t stm32_send_init_seq(const stm32_t *stm) +{ + struct port_interface *port = stm->port; + port_err_t p_err; + uint8_t byte, cmd = STM32_CMD_INIT; + + p_err = port->write(port, &cmd, 1); + if (p_err != PORT_ERR_OK) { + fprintf(stderr, "Failed to send init to device\n"); + return STM32_ERR_UNKNOWN; + } + p_err = port->read(port, &byte, 1); + if (p_err == PORT_ERR_OK && byte == STM32_ACK) + return STM32_ERR_OK; + if (p_err == PORT_ERR_OK && byte == STM32_NACK) { + /* We could get error later, but let's continue, for now. */ + fprintf(stderr, + "Warning: the interface was not closed properly.\n"); + return STM32_ERR_OK; + } + if (p_err != PORT_ERR_TIMEDOUT) { + fprintf(stderr, "Failed to init device.\n"); + return STM32_ERR_UNKNOWN; + } + + /* + * Check if previous STM32_CMD_INIT was taken as first byte + * of a command. Send a new byte, we should get back a NACK. + */ + p_err = port->write(port, &cmd, 1); + if (p_err != PORT_ERR_OK) { + fprintf(stderr, "Failed to send init to device\n"); + return STM32_ERR_UNKNOWN; + } + p_err = port->read(port, &byte, 1); + if (p_err == PORT_ERR_OK && byte == STM32_NACK) + return STM32_ERR_OK; + fprintf(stderr, "Failed to init device.\n"); + return STM32_ERR_UNKNOWN; +} + +/* find newer command by higher code */ +#define newer(prev, a) (((prev) == STM32_CMD_ERR) \ + ? (a) \ + : (((prev) > (a)) ? (prev) : (a))) + +stm32_t *stm32_init(struct port_interface *port, const char init) +{ + uint8_t len, val, buf[257]; + stm32_t *stm; + int i, new_cmds; + + stm = calloc(sizeof(stm32_t), 1); + stm->cmd = malloc(sizeof(stm32_cmd_t)); + memset(stm->cmd, STM32_CMD_ERR, sizeof(stm32_cmd_t)); + stm->port = port; + + if ((port->flags & PORT_CMD_INIT) && init) + if (stm32_send_init_seq(stm) != STM32_ERR_OK) + return NULL; + + /* get the version and read protection status */ + if (stm32_send_command(stm, STM32_CMD_GVR) != STM32_ERR_OK) { + stm32_close(stm); + return NULL; + } + + /* From AN, only UART bootloader returns 3 bytes */ + len = (port->flags & PORT_GVR_ETX) ? 3 : 1; + if (port->read(port, buf, len) != PORT_ERR_OK) + return NULL; + stm->version = buf[0]; + stm->option1 = (port->flags & PORT_GVR_ETX) ? buf[1] : 0; + stm->option2 = (port->flags & PORT_GVR_ETX) ? buf[2] : 0; + if (stm32_get_ack(stm) != STM32_ERR_OK) { + stm32_close(stm); + return NULL; + } + + /* get the bootloader information */ + len = STM32_CMD_GET_LENGTH; + if (port->cmd_get_reply) + for (i = 0; port->cmd_get_reply[i].length; i++) + if (stm->version == port->cmd_get_reply[i].version) { + len = port->cmd_get_reply[i].length; + break; + } + if (stm32_guess_len_cmd(stm, STM32_CMD_GET, buf, len) != STM32_ERR_OK) + return NULL; + len = buf[0] + 1; + stm->bl_version = buf[1]; + new_cmds = 0; + for (i = 1; i < len; i++) { + val = buf[i + 1]; + switch (val) { + case STM32_CMD_GET: + stm->cmd->get = val; break; + case STM32_CMD_GVR: + stm->cmd->gvr = val; break; + case STM32_CMD_GID: + stm->cmd->gid = val; break; + case STM32_CMD_RM: + stm->cmd->rm = val; break; + case STM32_CMD_GO: + stm->cmd->go = val; break; + case STM32_CMD_WM: + case STM32_CMD_WM_NS: + stm->cmd->wm = newer(stm->cmd->wm, val); + break; + case STM32_CMD_ER: + case STM32_CMD_EE: + case STM32_CMD_EE_NS: + stm->cmd->er = newer(stm->cmd->er, val); + break; + case STM32_CMD_WP: + case STM32_CMD_WP_NS: + stm->cmd->wp = newer(stm->cmd->wp, val); + break; + case STM32_CMD_UW: + case STM32_CMD_UW_NS: + stm->cmd->uw = newer(stm->cmd->uw, val); + break; + case STM32_CMD_RP: + case STM32_CMD_RP_NS: + stm->cmd->rp = newer(stm->cmd->rp, val); + break; + case STM32_CMD_UR: + case STM32_CMD_UR_NS: + stm->cmd->ur = newer(stm->cmd->ur, val); + break; + case STM32_CMD_CRC: + stm->cmd->crc = newer(stm->cmd->crc, val); + break; + default: + if (new_cmds++ == 0) + fprintf(stderr, + "GET returns unknown commands (0x%2x", + val); + else + fprintf(stderr, ", 0x%2x", val); + } + } + if (new_cmds) + fprintf(stderr, ")\n"); + if (stm32_get_ack(stm) != STM32_ERR_OK) { + stm32_close(stm); + return NULL; + } + + if (stm->cmd->get == STM32_CMD_ERR + || stm->cmd->gvr == STM32_CMD_ERR + || stm->cmd->gid == STM32_CMD_ERR) { + fprintf(stderr, "Error: bootloader did not returned correct information from GET command\n"); + return NULL; + } + + /* get the device ID */ + if (stm32_guess_len_cmd(stm, stm->cmd->gid, buf, 1) != STM32_ERR_OK) { + stm32_close(stm); + return NULL; + } + len = buf[0] + 1; + if (len < 2) { + stm32_close(stm); + fprintf(stderr, "Only %d bytes sent in the PID, unknown/unsupported device\n", len); + return NULL; + } + stm->pid = (buf[1] << 8) | buf[2]; + if (len > 2) { + fprintf(stderr, "This bootloader returns %d extra bytes in PID:", len); + for (i = 2; i <= len ; i++) + fprintf(stderr, " %02x", buf[i]); + fprintf(stderr, "\n"); + } + if (stm32_get_ack(stm) != STM32_ERR_OK) { + stm32_close(stm); + return NULL; + } + + stm->dev = devices; + while (stm->dev->id != 0x00 && stm->dev->id != stm->pid) + ++stm->dev; + + if (!stm->dev->id) { + fprintf(stderr, "Unknown/unsupported device (Device ID: 0x%03x)\n", stm->pid); + stm32_close(stm); + return NULL; + } + + return stm; +} + +void stm32_close(stm32_t *stm) +{ + if (stm) + free(stm->cmd); + free(stm); +} + +stm32_err_t stm32_read_memory(const stm32_t *stm, uint32_t address, + uint8_t data[], unsigned int len) +{ + struct port_interface *port = stm->port; + uint8_t buf[5]; + + if (!len) + return STM32_ERR_OK; + + if (len > 256) { + fprintf(stderr, "Error: READ length limit at 256 bytes\n"); + return STM32_ERR_UNKNOWN; + } + + if (stm->cmd->rm == STM32_CMD_ERR) { + fprintf(stderr, "Error: READ command not implemented in bootloader.\n"); + return STM32_ERR_NO_CMD; + } + + if (stm32_send_command(stm, stm->cmd->rm) != STM32_ERR_OK) + return STM32_ERR_UNKNOWN; + + buf[0] = address >> 24; + buf[1] = (address >> 16) & 0xFF; + buf[2] = (address >> 8) & 0xFF; + buf[3] = address & 0xFF; + buf[4] = buf[0] ^ buf[1] ^ buf[2] ^ buf[3]; + if (port->write(port, buf, 5) != PORT_ERR_OK) + return STM32_ERR_UNKNOWN; + if (stm32_get_ack(stm) != STM32_ERR_OK) + return STM32_ERR_UNKNOWN; + + if (stm32_send_command(stm, len - 1) != STM32_ERR_OK) + return STM32_ERR_UNKNOWN; + + if (port->read(port, data, len) != PORT_ERR_OK) + return STM32_ERR_UNKNOWN; + + return STM32_ERR_OK; +} + +stm32_err_t stm32_write_memory(const stm32_t *stm, uint32_t address, + const uint8_t data[], unsigned int len) +{ + struct port_interface *port = stm->port; + uint8_t cs, buf[256 + 2]; + unsigned int i, aligned_len; + stm32_err_t s_err; + + if (!len) + return STM32_ERR_OK; + + if (len > 256) { + fprintf(stderr, "Error: READ length limit at 256 bytes\n"); + return STM32_ERR_UNKNOWN; + } + + /* must be 32bit aligned */ + if (address & 0x3 || len & 0x3) { + fprintf(stderr, "Error: WRITE address and length must be 4 byte aligned\n"); + return STM32_ERR_UNKNOWN; + } + + if (stm->cmd->wm == STM32_CMD_ERR) { + fprintf(stderr, "Error: WRITE command not implemented in bootloader.\n"); + return STM32_ERR_NO_CMD; + } + + /* send the address and checksum */ + if (stm32_send_command(stm, stm->cmd->wm) != STM32_ERR_OK) + return STM32_ERR_UNKNOWN; + + buf[0] = address >> 24; + buf[1] = (address >> 16) & 0xFF; + buf[2] = (address >> 8) & 0xFF; + buf[3] = address & 0xFF; + buf[4] = buf[0] ^ buf[1] ^ buf[2] ^ buf[3]; + if (port->write(port, buf, 5) != PORT_ERR_OK) + return STM32_ERR_UNKNOWN; + if (stm32_get_ack(stm) != STM32_ERR_OK) + return STM32_ERR_UNKNOWN; + + aligned_len = (len + 3) & ~3; + cs = aligned_len - 1; + buf[0] = aligned_len - 1; + for (i = 0; i < len; i++) { + cs ^= data[i]; + buf[i + 1] = data[i]; + } + /* padding data */ + for (i = len; i < aligned_len; i++) { + cs ^= 0xFF; + buf[i + 1] = 0xFF; + } + buf[aligned_len + 1] = cs; + if (port->write(port, buf, aligned_len + 2) != PORT_ERR_OK) + return STM32_ERR_UNKNOWN; + + s_err = stm32_get_ack_timeout(stm, STM32_BLKWRITE_TIMEOUT); + if (s_err != STM32_ERR_OK) { + if (port->flags & PORT_STRETCH_W + && stm->cmd->wm != STM32_CMD_WM_NS) + stm32_warn_stretching("write"); + return STM32_ERR_UNKNOWN; + } + return STM32_ERR_OK; +} + +stm32_err_t stm32_wunprot_memory(const stm32_t *stm) +{ + struct port_interface *port = stm->port; + stm32_err_t s_err; + + if (stm->cmd->uw == STM32_CMD_ERR) { + fprintf(stderr, "Error: WRITE UNPROTECT command not implemented in bootloader.\n"); + return STM32_ERR_NO_CMD; + } + + if (stm32_send_command(stm, stm->cmd->uw) != STM32_ERR_OK) + return STM32_ERR_UNKNOWN; + + s_err = stm32_get_ack_timeout(stm, STM32_WUNPROT_TIMEOUT); + if (s_err == STM32_NACK) { + fprintf(stderr, "Error: Failed to WRITE UNPROTECT\n"); + return STM32_ERR_UNKNOWN; + } + if (s_err != STM32_ERR_OK) { + if (port->flags & PORT_STRETCH_W + && stm->cmd->uw != STM32_CMD_UW_NS) + stm32_warn_stretching("WRITE UNPROTECT"); + return STM32_ERR_UNKNOWN; + } + return STM32_ERR_OK; +} + +stm32_err_t stm32_wprot_memory(const stm32_t *stm) +{ + struct port_interface *port = stm->port; + stm32_err_t s_err; + + if (stm->cmd->wp == STM32_CMD_ERR) { + fprintf(stderr, "Error: WRITE PROTECT command not implemented in bootloader.\n"); + return STM32_ERR_NO_CMD; + } + + if (stm32_send_command(stm, stm->cmd->wp) != STM32_ERR_OK) + return STM32_ERR_UNKNOWN; + + s_err = stm32_get_ack_timeout(stm, STM32_WPROT_TIMEOUT); + if (s_err == STM32_NACK) { + fprintf(stderr, "Error: Failed to WRITE PROTECT\n"); + return STM32_ERR_UNKNOWN; + } + if (s_err != STM32_ERR_OK) { + if (port->flags & PORT_STRETCH_W + && stm->cmd->wp != STM32_CMD_WP_NS) + stm32_warn_stretching("WRITE PROTECT"); + return STM32_ERR_UNKNOWN; + } + return STM32_ERR_OK; +} + +stm32_err_t stm32_runprot_memory(const stm32_t *stm) +{ + struct port_interface *port = stm->port; + stm32_err_t s_err; + + if (stm->cmd->ur == STM32_CMD_ERR) { + fprintf(stderr, "Error: READOUT UNPROTECT command not implemented in bootloader.\n"); + return STM32_ERR_NO_CMD; + } + + if (stm32_send_command(stm, stm->cmd->ur) != STM32_ERR_OK) + return STM32_ERR_UNKNOWN; + + s_err = stm32_get_ack_timeout(stm, STM32_MASSERASE_TIMEOUT); + if (s_err == STM32_NACK) { + fprintf(stderr, "Error: Failed to READOUT UNPROTECT\n"); + return STM32_ERR_UNKNOWN; + } + if (s_err != STM32_ERR_OK) { + if (port->flags & PORT_STRETCH_W + && stm->cmd->ur != STM32_CMD_UR_NS) + stm32_warn_stretching("READOUT UNPROTECT"); + return STM32_ERR_UNKNOWN; + } + return STM32_ERR_OK; +} + +stm32_err_t stm32_readprot_memory(const stm32_t *stm) +{ + struct port_interface *port = stm->port; + stm32_err_t s_err; + + if (stm->cmd->rp == STM32_CMD_ERR) { + fprintf(stderr, "Error: READOUT PROTECT command not implemented in bootloader.\n"); + return STM32_ERR_NO_CMD; + } + + if (stm32_send_command(stm, stm->cmd->rp) != STM32_ERR_OK) + return STM32_ERR_UNKNOWN; + + s_err = stm32_get_ack_timeout(stm, STM32_RPROT_TIMEOUT); + if (s_err == STM32_NACK) { + fprintf(stderr, "Error: Failed to READOUT PROTECT\n"); + return STM32_ERR_UNKNOWN; + } + if (s_err != STM32_ERR_OK) { + if (port->flags & PORT_STRETCH_W + && stm->cmd->rp != STM32_CMD_RP_NS) + stm32_warn_stretching("READOUT PROTECT"); + return STM32_ERR_UNKNOWN; + } + return STM32_ERR_OK; +} + +stm32_err_t stm32_erase_memory(const stm32_t *stm, uint8_t spage, uint8_t pages) +{ + struct port_interface *port = stm->port; + stm32_err_t s_err; + port_err_t p_err; + + if (!pages) + return STM32_ERR_OK; + + if (stm->cmd->er == STM32_CMD_ERR) { + fprintf(stderr, "Error: ERASE command not implemented in bootloader.\n"); + return STM32_ERR_NO_CMD; + } + + if (stm32_send_command(stm, stm->cmd->er) != STM32_ERR_OK) { + fprintf(stderr, "Can't initiate chip erase!\n"); + return STM32_ERR_UNKNOWN; + } + + /* The erase command reported by the bootloader is either 0x43, 0x44 or 0x45 */ + /* 0x44 is Extended Erase, a 2 byte based protocol and needs to be handled differently. */ + /* 0x45 is clock no-stretching version of Extended Erase for I2C port. */ + if (stm->cmd->er != STM32_CMD_ER) { + /* Not all chips using Extended Erase support mass erase */ + /* Currently known as not supporting mass erase is the Ultra Low Power STM32L15xx range */ + /* So if someone has not overridden the default, but uses one of these chips, take it out of */ + /* mass erase mode, so it will be done page by page. This maximum might not be correct either! */ + if (stm->pid == 0x416 && pages == 0xFF) + pages = 0xF8; /* works for the STM32L152RB with 128Kb flash */ + + if (pages == 0xFF) { + uint8_t buf[3]; + + /* 0xFFFF the magic number for mass erase */ + buf[0] = 0xFF; + buf[1] = 0xFF; + buf[2] = 0x00; /* checksum */ + if (port->write(port, buf, 3) != PORT_ERR_OK) { + fprintf(stderr, "Mass erase error.\n"); + return STM32_ERR_UNKNOWN; + } + s_err = stm32_get_ack_timeout(stm, STM32_MASSERASE_TIMEOUT); + if (s_err != STM32_ERR_OK) { + fprintf(stderr, "Mass erase failed. Try specifying the number of pages to be erased.\n"); + if (port->flags & PORT_STRETCH_W + && stm->cmd->er != STM32_CMD_EE_NS) + stm32_warn_stretching("erase"); + return STM32_ERR_UNKNOWN; + } + return STM32_ERR_OK; + } + + uint16_t pg_num; + uint8_t pg_byte; + uint8_t cs = 0; + uint8_t *buf; + int i = 0; + + buf = malloc(2 + 2 * pages + 1); + if (!buf) + return STM32_ERR_UNKNOWN; + + /* Number of pages to be erased - 1, two bytes, MSB first */ + pg_byte = (pages - 1) >> 8; + buf[i++] = pg_byte; + cs ^= pg_byte; + pg_byte = (pages - 1) & 0xFF; + buf[i++] = pg_byte; + cs ^= pg_byte; + + for (pg_num = spage; pg_num < spage + pages; pg_num++) { + pg_byte = pg_num >> 8; + cs ^= pg_byte; + buf[i++] = pg_byte; + pg_byte = pg_num & 0xFF; + cs ^= pg_byte; + buf[i++] = pg_byte; + } + buf[i++] = cs; + p_err = port->write(port, buf, i); + free(buf); + if (p_err != PORT_ERR_OK) { + fprintf(stderr, "Page-by-page erase error.\n"); + return STM32_ERR_UNKNOWN; + } + + s_err = stm32_get_ack_timeout(stm, pages * STM32_SECTERASE_TIMEOUT); + if (s_err != STM32_ERR_OK) { + fprintf(stderr, "Page-by-page erase failed. Check the maximum pages your device supports.\n"); + if (port->flags & PORT_STRETCH_W + && stm->cmd->er != STM32_CMD_EE_NS) + stm32_warn_stretching("erase"); + return STM32_ERR_UNKNOWN; + } + + return STM32_ERR_OK; + } + + /* And now the regular erase (0x43) for all other chips */ + if (pages == 0xFF) { + s_err = stm32_send_command_timeout(stm, 0xFF, STM32_MASSERASE_TIMEOUT); + if (s_err != STM32_ERR_OK) { + if (port->flags & PORT_STRETCH_W) + stm32_warn_stretching("erase"); + return STM32_ERR_UNKNOWN; + } + return STM32_ERR_OK; + } else { + uint8_t cs = 0; + uint8_t pg_num; + uint8_t *buf; + int i = 0; + + buf = malloc(1 + pages + 1); + if (!buf) + return STM32_ERR_UNKNOWN; + + buf[i++] = pages - 1; + cs ^= (pages-1); + for (pg_num = spage; pg_num < (pages + spage); pg_num++) { + buf[i++] = pg_num; + cs ^= pg_num; + } + buf[i++] = cs; + p_err = port->write(port, buf, i); + free(buf); + if (p_err != PORT_ERR_OK) { + fprintf(stderr, "Erase failed.\n"); + return STM32_ERR_UNKNOWN; + } + s_err = stm32_get_ack_timeout(stm, STM32_MASSERASE_TIMEOUT); + if (s_err != STM32_ERR_OK) { + if (port->flags & PORT_STRETCH_W) + stm32_warn_stretching("erase"); + return STM32_ERR_UNKNOWN; + } + return STM32_ERR_OK; + } +} + +static stm32_err_t stm32_run_raw_code(const stm32_t *stm, + uint32_t target_address, + const uint8_t *code, uint32_t code_size) +{ + uint32_t stack_le = le_u32(0x20002000); + uint32_t code_address_le = le_u32(target_address + 8); + uint32_t length = code_size + 8; + uint8_t *mem, *pos; + uint32_t address, w; + + /* Must be 32-bit aligned */ + if (target_address & 0x3) { + fprintf(stderr, "Error: code address must be 4 byte aligned\n"); + return STM32_ERR_UNKNOWN; + } + + mem = malloc(length); + if (!mem) + return STM32_ERR_UNKNOWN; + + memcpy(mem, &stack_le, sizeof(uint32_t)); + memcpy(mem + 4, &code_address_le, sizeof(uint32_t)); + memcpy(mem + 8, code, code_size); + + pos = mem; + address = target_address; + while (length > 0) { + w = length > 256 ? 256 : length; + if (stm32_write_memory(stm, address, pos, w) != STM32_ERR_OK) { + free(mem); + return STM32_ERR_UNKNOWN; + } + + address += w; + pos += w; + length -= w; + } + + free(mem); + return stm32_go(stm, target_address); +} + +stm32_err_t stm32_go(const stm32_t *stm, uint32_t address) +{ + struct port_interface *port = stm->port; + uint8_t buf[5]; + + if (stm->cmd->go == STM32_CMD_ERR) { + fprintf(stderr, "Error: GO command not implemented in bootloader.\n"); + return STM32_ERR_NO_CMD; + } + + if (stm32_send_command(stm, stm->cmd->go) != STM32_ERR_OK) + return STM32_ERR_UNKNOWN; + + buf[0] = address >> 24; + buf[1] = (address >> 16) & 0xFF; + buf[2] = (address >> 8) & 0xFF; + buf[3] = address & 0xFF; + buf[4] = buf[0] ^ buf[1] ^ buf[2] ^ buf[3]; + if (port->write(port, buf, 5) != PORT_ERR_OK) + return STM32_ERR_UNKNOWN; + + if (stm32_get_ack(stm) != STM32_ERR_OK) + return STM32_ERR_UNKNOWN; + return STM32_ERR_OK; +} + +stm32_err_t stm32_reset_device(const stm32_t *stm) +{ + uint32_t target_address = stm->dev->ram_start; + + return stm32_run_raw_code(stm, target_address, stm_reset_code, stm_reset_code_length); +} + +stm32_err_t stm32_crc_memory(const stm32_t *stm, uint32_t address, + uint32_t length, uint32_t *crc) +{ + struct port_interface *port = stm->port; + uint8_t buf[5]; + + if (address & 0x3 || length & 0x3) { + fprintf(stderr, "Start and end addresses must be 4 byte aligned\n"); + return STM32_ERR_UNKNOWN; + } + + if (stm->cmd->crc == STM32_CMD_ERR) { + fprintf(stderr, "Error: CRC command not implemented in bootloader.\n"); + return STM32_ERR_NO_CMD; + } + + if (stm32_send_command(stm, stm->cmd->crc) != STM32_ERR_OK) + return STM32_ERR_UNKNOWN; + + buf[0] = address >> 24; + buf[1] = (address >> 16) & 0xFF; + buf[2] = (address >> 8) & 0xFF; + buf[3] = address & 0xFF; + buf[4] = buf[0] ^ buf[1] ^ buf[2] ^ buf[3]; + if (port->write(port, buf, 5) != PORT_ERR_OK) + return STM32_ERR_UNKNOWN; + + if (stm32_get_ack(stm) != STM32_ERR_OK) + return STM32_ERR_UNKNOWN; + + buf[0] = length >> 24; + buf[1] = (length >> 16) & 0xFF; + buf[2] = (length >> 8) & 0xFF; + buf[3] = length & 0xFF; + buf[4] = buf[0] ^ buf[1] ^ buf[2] ^ buf[3]; + if (port->write(port, buf, 5) != PORT_ERR_OK) + return STM32_ERR_UNKNOWN; + + if (stm32_get_ack(stm) != STM32_ERR_OK) + return STM32_ERR_UNKNOWN; + + if (stm32_get_ack(stm) != STM32_ERR_OK) + return STM32_ERR_UNKNOWN; + + if (port->read(port, buf, 5) != PORT_ERR_OK) + return STM32_ERR_UNKNOWN; + + if (buf[4] != (buf[0] ^ buf[1] ^ buf[2] ^ buf[3])) + return STM32_ERR_UNKNOWN; + + *crc = (buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3]; + return STM32_ERR_OK; +} + +/* + * CRC computed by STM32 is similar to the standard crc32_be() + * implemented, for example, in Linux kernel in ./lib/crc32.c + * But STM32 computes it on units of 32 bits word and swaps the + * bytes of the word before the computation. + * Due to byte swap, I cannot use any CRC available in existing + * libraries, so here is a simple not optimized implementation. + */ +#define CRCPOLY_BE 0x04c11db7 +#define CRC_MSBMASK 0x80000000 +#define CRC_INIT_VALUE 0xFFFFFFFF +uint32_t stm32_sw_crc(uint32_t crc, uint8_t *buf, unsigned int len) +{ + int i; + uint32_t data; + + if (len & 0x3) { + fprintf(stderr, "Buffer length must be multiple of 4 bytes\n"); + return 0; + } + + while (len) { + data = *buf++; + data |= *buf++ << 8; + data |= *buf++ << 16; + data |= *buf++ << 24; + len -= 4; + + crc ^= data; + + for (i = 0; i < 32; i++) + if (crc & CRC_MSBMASK) + crc = (crc << 1) ^ CRCPOLY_BE; + else + crc = (crc << 1); + } + return crc; +} + +stm32_err_t stm32_crc_wrapper(const stm32_t *stm, uint32_t address, + uint32_t length, uint32_t *crc) +{ + uint8_t buf[256]; + uint32_t start, total_len, len, current_crc; + + if (address & 0x3 || length & 0x3) { + fprintf(stderr, "Start and end addresses must be 4 byte aligned\n"); + return STM32_ERR_UNKNOWN; + } + + if (stm->cmd->crc != STM32_CMD_ERR) + return stm32_crc_memory(stm, address, length, crc); + + start = address; + total_len = length; + current_crc = CRC_INIT_VALUE; + while (length) { + len = length > 256 ? 256 : length; + if (stm32_read_memory(stm, address, buf, len) != STM32_ERR_OK) { + fprintf(stderr, + "Failed to read memory at address 0x%08x, target write-protected?\n", + address); + return STM32_ERR_UNKNOWN; + } + current_crc = stm32_sw_crc(current_crc, buf, len); + length -= len; + address += len; + + fprintf(stderr, + "\rCRC address 0x%08x (%.2f%%) ", + address, + (100.0f / (float)total_len) * (float)(address - start) + ); + fflush(stderr); + } + fprintf(stderr, "Done.\n"); + *crc = current_crc; + return STM32_ERR_OK; +} diff --git a/tools/macosx/src/stm32flash_serial/src/stm32.h b/tools/macosx/src/stm32flash_serial/src/stm32.h new file mode 100644 index 0000000..1688fcb --- /dev/null +++ b/tools/macosx/src/stm32flash_serial/src/stm32.h @@ -0,0 +1,84 @@ +/* + stm32flash - Open Source ST STM32 flash program for *nix + Copyright (C) 2010 Geoffrey McRae + + 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. +*/ + + +#ifndef _STM32_H +#define _STM32_H + +#include +#include "serial.h" + +#define STM32_MAX_RX_FRAME 256 /* cmd read memory */ +#define STM32_MAX_TX_FRAME (1 + 256 + 1) /* cmd write memory */ + +typedef enum { + STM32_ERR_OK = 0, + STM32_ERR_UNKNOWN, /* Generic error */ + STM32_ERR_NACK, + STM32_ERR_NO_CMD, /* Command not available in bootloader */ +} stm32_err_t; + +typedef struct stm32 stm32_t; +typedef struct stm32_cmd stm32_cmd_t; +typedef struct stm32_dev stm32_dev_t; + +struct stm32 { + const serial_t *serial; + struct port_interface *port; + uint8_t bl_version; + uint8_t version; + uint8_t option1, option2; + uint16_t pid; + stm32_cmd_t *cmd; + const stm32_dev_t *dev; +}; + +struct stm32_dev { + uint16_t id; + const char *name; + uint32_t ram_start, ram_end; + uint32_t fl_start, fl_end; + uint16_t fl_pps; // pages per sector + uint16_t fl_ps; // page size + uint32_t opt_start, opt_end; + uint32_t mem_start, mem_end; +}; + +stm32_t *stm32_init(struct port_interface *port, const char init); +void stm32_close(stm32_t *stm); +stm32_err_t stm32_read_memory(const stm32_t *stm, uint32_t address, + uint8_t data[], unsigned int len); +stm32_err_t stm32_write_memory(const stm32_t *stm, uint32_t address, + const uint8_t data[], unsigned int len); +stm32_err_t stm32_wunprot_memory(const stm32_t *stm); +stm32_err_t stm32_wprot_memory(const stm32_t *stm); +stm32_err_t stm32_erase_memory(const stm32_t *stm, uint8_t spage, + uint8_t pages); +stm32_err_t stm32_go(const stm32_t *stm, uint32_t address); +stm32_err_t stm32_reset_device(const stm32_t *stm); +stm32_err_t stm32_readprot_memory(const stm32_t *stm); +stm32_err_t stm32_runprot_memory(const stm32_t *stm); +stm32_err_t stm32_crc_memory(const stm32_t *stm, uint32_t address, + uint32_t length, uint32_t *crc); +stm32_err_t stm32_crc_wrapper(const stm32_t *stm, uint32_t address, + uint32_t length, uint32_t *crc); +uint32_t stm32_sw_crc(uint32_t crc, uint8_t *buf, unsigned int len); + +#endif + diff --git a/tools/macosx/src/stm32flash_serial/src/stm32flash.1 b/tools/macosx/src/stm32flash_serial/src/stm32flash.1 new file mode 100644 index 0000000..d37292f --- /dev/null +++ b/tools/macosx/src/stm32flash_serial/src/stm32flash.1 @@ -0,0 +1,407 @@ +.TH STM32FLASH 1 "2013\-11\-03" STM32FLASH "User command" +.SH NAME +stm32flash \- flashing utility for STM32 and STM32W through UART or I2C +.SH SYNOPSIS +.B stm32flash +.RB [ \-cfhjkouvCR ] +.RB [ \-a +.IR bus_address ] +.RB [ \-b +.IR baud_rate ] +.RB [ \-m +.IR serial_mode ] +.RB [ \-r +.IR filename ] +.RB [ \-w +.IR filename ] +.RB [ \-e +.IR num ] +.RB [ \-n +.IR count ] +.RB [ \-g +.IR address ] +.RB [ \-s +.IR start_page ] +.RB [ \-S +.IR address [: length ]] +.RB [ \-F +.IR RX_length [: TX_length ]] +.RB [ \-i +.IR GPIO_string ] +.RI [ tty_device +.R | +.IR i2c_device ] + +.SH DESCRIPTION +.B stm32flash +reads or writes the flash memory of STM32 and STM32W. + +It requires the STM32[W] to embed a bootloader compliant with ST +application note AN3155. +.B stm32flash +uses the serial port +.I tty_device +to interact with the bootloader of STM32[W]. + +.SH OPTIONS +.TP +.BI "\-a" " bus_address" +Specify address on bus for +.IR i2c_device . +This option is mandatory for I2C interface. + +.TP +.BI "\-b" " baud_rate" +Specify baud rate speed of +.IR tty_device . +Please notice that the ST bootloader can automatically detect the baud rate, +as explaned in chapter 2 of AN3155. +This option could be required together with option +.B "\-c" +or if following interaction with bootloader is expected. +Default is +.IR 57600 . + +.TP +.BI "\-m" " mode" +Specify the format of UART data. +.I mode +is a three characters long string where each character specifies, in +this strict order, character size, parity and stop bits. +The only values currenly used are +.I 8e1 +for standard STM32 bootloader and +.I 8n1 +for standard STM32W bootloader. +Default is +.IR 8e1 . + +.TP +.BI "\-r" " filename" +Specify to read the STM32[W] flash and write its content in +.I filename +in raw binary format (see below +.BR "FORMAT CONVERSION" ). + +.TP +.BI "\-w" " filename" +Specify to write the STM32[W] flash with the content of +.IR filename . +File format can be either raw binary or intel hex (see below +.BR "FORMAT CONVERSION" ). +The file format is automatically detected. +To by\-pass format detection and force binary mode (e.g. to +write an intel hex content in STM32[W] flash), use +.B \-f +option. + +.TP +.B \-u +Specify to disable write\-protection from STM32[W] flash. +The STM32[W] will be reset after this operation. + +.TP +.B \-j +Enable the flash read\-protection. + +.TP +.B \-k +Disable the flash read\-protection. + +.TP +.B \-o +Erase only. + +.TP +.BI "\-e" " num" +Specify to erase only +.I num +pages before writing the flash. Default is to erase the whole flash. With +.B \-e 0 +the flash would not be erased. + +.TP +.B \-v +Specify to verify flash content after write operation. + +.TP +.BI "\-n" " count" +Specify to retry failed writes up to +.I count +times. Default is 10 times. + +.TP +.BI "\-g" " address" +Specify address to start execution from (0 = flash start). + +.TP +.BI "\-s" " start_page" +Specify flash page offset (0 = flash start). + +.TP +.BI "\-S" " address" "[:" "length" "]" +Specify start address and optionally length for read/write/erase/crc operations. + +.TP +.BI "\-F" " RX_length" "[:" "TX_length" "]" +Specify the maximum frame size for the current interface. +Due to STM32 bootloader protocol, host will never handle frames bigger than +256 byte in RX or 258 byte in TX. +Due to current code, lowest limit in RX is 20 byte (to read a complete reply +of command GET). Minimum limit in TX is 5 byte, required by protocol. + +.TP +.B \-f +Force binary parser while reading file with +.BR "\-w" "." + +.TP +.B \-h +Show help. + +.TP +.B \-c +Specify to resume the existing UART connection and don't send initial +INIT sequence to detect baud rate. Baud rate must be kept the same as the +existing connection. This is useful if the reset fails. + +.TP +.BI "\-i" " GPIO_string" +Specify the GPIO sequences on the host to force STM32[W] to enter and +exit bootloader mode. GPIO can either be real GPIO connected from host to +STM32[W] beside the UART connection, or UART's modem signals used as +GPIO. (See below +.B BOOTLOADER GPIO SEQUENCE +for the format of +.I GPIO_string +and further explanation). + +.TP +.B \-C +Specify to compute CRC on memory content. +By default the CRC is computed on the whole flash content. +Use +.B "\-S" +to provide different memory address range. + +.TP +.B \-R +Specify to reset the device at exit. +This option is ignored if either +.BR "\-g" "," +.BR "\-j" "," +.B "\-k" +or +.B "\-u" +is also specified. + +.SH BOOTLOADER GPIO SEQUENCE +This feature is currently available on Linux host only. + +As explained in ST application note AN2606, after reset the STM32 will +execute either the application program in user flash or the bootloader, +depending on the level applied at specific pins of STM32 during reset. + +STM32 bootloader is automatically activated by configuring the pins +BOOT0="high" and BOOT1="low" and then by applying a reset. +Application program in user flash is activated by configuring the pin +BOOT0="low" (the level on BOOT1 is ignored) and then by applying a reset. + +When GPIO from host computer are connected to either configuration and +reset pins of STM32, +.B stm32flash +can control the host GPIO to reset STM32 and to force execution of +bootloader or execution of application program. + +The sequence of GPIO values to entry to and exit from bootloader mode is +provided with command line option +.B "\-i" +.IR "GPIO_string" . + +.PD 0 +The format of +.IR "GPIO_string" " is:" +.RS +GPIO_string = [entry sequence][:[exit sequence]] +.P +sequence = [\-]n[,sequence] +.RE +.P +In the above sequences, negative numbers correspond to GPIO at "low" level; +numbers without sign correspond to GPIO at "high" level. +The value "n" can either be the GPIO number on the host system or the +string "rts", "dtr" or "brk". The strings "rts" and "dtr" drive the +corresponding UART's modem lines RTS and DTR as GPIO. +The string "brk" forces the UART to send a BREAK sequence on TX line; +after BREAK the UART is returned in normal "non\-break" mode. +Note: the string "\-brk" has no effect and is ignored. +.PD + +.PD 0 +As example, let's suppose the following connection between host and STM32: +.IP \(bu 2 +host GPIO_3 connected to reset pin of STM32; +.IP \(bu 2 +host GPIO_4 connected to STM32 pin BOOT0; +.IP \(bu 2 +host GPIO_5 connected to STM32 pin BOOT1. +.PD +.P + +In this case, the sequence to enter in bootloader mode is: first put +GPIO_4="high" and GPIO_5="low"; then send reset pulse by GPIO_3="low" +followed by GPIO_3="high". +The corresponding string for +.I GPIO_string +is "4,\-5,\-3,3". + +To exit from bootloade and run the application program, the sequence is: +put GPIO_4="low"; then send reset pulse. +The corresponding string for +.I GPIO_string +is "\-4,\-3,3". + +The complete command line flag is "\-i 4,\-5,\-3,3:\-4,\-3,3". + +STM32W uses pad PA5 to select boot mode; if during reset PA5 is "low" then +STM32W will enter in bootloader mode; if PA5 is "high" it will execute the +program in flash. + +As example, supposing GPIO_3 connected to PA5 and GPIO_2 to STM32W's reset. +The command: +.PD 0 +.RS +stm32flash \-i \-3,\-2,2:3,\-2,2 /dev/ttyS0 +.RE +provides: +.IP \(bu 2 +entry sequence: GPIO_3=low, GPIO_2=low, GPIO_2=high +.IP \(bu 2 +exit sequence: GPIO_3=high, GPIO_2=low, GPIO_2=high +.PD + +.SH EXAMPLES +Get device information: +.RS +.PD 0 +.P +stm32flash /dev/ttyS0 +.PD +.RE + +Write with verify and then start execution: +.RS +.PD 0 +.P +stm32flash \-w filename \-v \-g 0x0 /dev/ttyS0 +.PD +.RE + +Read flash to file: +.RS +.PD 0 +.P +stm32flash \-r filename /dev/ttyS0 +.PD +.RE + +Start execution: +.RS +.PD 0 +.P +stm32flash \-g 0x0 /dev/ttyS0 +.PD +.RE + +Specify: +.PD 0 +.IP \(bu 2 +entry sequence: RTS=low, DTR=low, DTR=high +.IP \(bu 2 +exit sequence: RTS=high, DTR=low, DTR=high +.P +.RS +stm32flash \-i \-rts,\-dtr,dtr:rts,\-dtr,dtr /dev/ttyS0 +.PD +.RE + +.SH FORMAT CONVERSION +Flash images provided by ST or created with ST tools are often in file +format Motorola S\-Record. +Conversion between raw binary, intel hex and Motorola S\-Record can be +done through software package SRecord. + +.SH AUTHORS +The original software package +.B stm32flash +is written by +.I Geoffrey McRae +and is since 2012 maintained by +.IR "Tormod Volden " . + +Man page and extension to STM32W and I2C are written by +.IR "Antonio Borneo " . + +Please report any bugs at the project homepage +http://stm32flash.googlecode.com . + +.SH SEE ALSO +.BR "srec_cat" "(1)," " srec_intel" "(5)," " srec_motorola" "(5)." + +The communication protocol used by ST bootloader is documented in +following ST application notes, depending on communication port. +The current version of +.B stm32flash +only supports +.I UART +and +.I I2C +ports. +.PD 0 +.P +.IP \(bu 2 +AN3154: CAN protocol used in the STM32 bootloader +.P +.RS +http://www.st.com/web/en/resource/technical/document/application_note/CD00264321.pdf +.RE + +.P +.IP \(bu 2 +AN3155: USART protocol used in the STM32(TM) bootloader +.P +.RS +http://www.st.com/web/en/resource/technical/document/application_note/CD00264342.pdf +.RE + +.P +.IP \(bu 2 +AN4221: I2C protocol used in the STM32 bootloader +.P +.RS +http://www.st.com/web/en/resource/technical/document/application_note/DM00072315.pdf +.RE + +.P +.IP \(bu 2 +AN4286: SPI protocol used in the STM32 bootloader +.P +.RS +http://www.st.com/web/en/resource/technical/document/application_note/DM00081379.pdf +.RE + +.PD + + +Boot mode selection for STM32 is documented in ST application note +AN2606, available from the ST website: +.PD 0 +.P +http://www.st.com/web/en/resource/technical/document/application_note/CD00167594.pdf +.PD + +.SH LICENSE +.B stm32flash +is distributed under GNU GENERAL PUBLIC LICENSE Version 2. +Copy of the license is available within the source code in the file +.IR "gpl\-2.0.txt" . diff --git a/tools/macosx/src/stm32flash_serial/src/utils.c b/tools/macosx/src/stm32flash_serial/src/utils.c new file mode 100644 index 0000000..271bb3e --- /dev/null +++ b/tools/macosx/src/stm32flash_serial/src/utils.c @@ -0,0 +1,45 @@ +/* + stm32flash - Open Source ST STM32 flash program for *nix + Copyright (C) 2010 Geoffrey McRae + + 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. +*/ + +#include +#include "utils.h" + +/* detect CPU endian */ +char cpu_le() { + const uint32_t cpu_le_test = 0x12345678; + return ((const unsigned char*)&cpu_le_test)[0] == 0x78; +} + +uint32_t be_u32(const uint32_t v) { + if (cpu_le()) + return ((v & 0xFF000000) >> 24) | + ((v & 0x00FF0000) >> 8) | + ((v & 0x0000FF00) << 8) | + ((v & 0x000000FF) << 24); + return v; +} + +uint32_t le_u32(const uint32_t v) { + if (!cpu_le()) + return ((v & 0xFF000000) >> 24) | + ((v & 0x00FF0000) >> 8) | + ((v & 0x0000FF00) << 8) | + ((v & 0x000000FF) << 24); + return v; +} diff --git a/tools/macosx/src/stm32flash_serial/src/utils.h b/tools/macosx/src/stm32flash_serial/src/utils.h new file mode 100644 index 0000000..a8d37d2 --- /dev/null +++ b/tools/macosx/src/stm32flash_serial/src/utils.h @@ -0,0 +1,30 @@ +/* + stm32flash - Open Source ST STM32 flash program for *nix + Copyright (C) 2010 Geoffrey McRae + + 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. +*/ + + +#ifndef _H_UTILS +#define _H_UTILS + +#include + +char cpu_le(); +uint32_t be_u32(const uint32_t v); +uint32_t le_u32(const uint32_t v); + +#endif diff --git a/tools/macosx/src/upload-reset/upload-reset.c b/tools/macosx/src/upload-reset/upload-reset.c new file mode 100644 index 0000000..1d03bff --- /dev/null +++ b/tools/macosx/src/upload-reset/upload-reset.c @@ -0,0 +1,161 @@ +/* Copyright (C) 2015 Roger Clark + * + * 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. + * + * + * Utility to send the reset sequence on RTS and DTR and chars + * which resets the libmaple and causes the bootloader to be run + * + * + * + * Terminal control code by Heiko Noordhof (see copyright below) + */ + + + +/* Copyright (C) 2003 Heiko Noordhof + * + * 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. + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +/* Function prototypes (belong in a seperate header file) */ +int openserial(char *devicename); +void closeserial(void); +int setDTR(unsigned short level); +int setRTS(unsigned short level); + + +/* Two globals for use by this module only */ +static int fd; +static struct termios oldterminfo; + + +void closeserial(void) +{ + tcsetattr(fd, TCSANOW, &oldterminfo); + close(fd); +} + + +int openserial(char *devicename) +{ + struct termios attr; + + if ((fd = open(devicename, O_RDWR)) == -1) return 0; /* Error */ + atexit(closeserial); + + if (tcgetattr(fd, &oldterminfo) == -1) return 0; /* Error */ + attr = oldterminfo; + attr.c_cflag |= CRTSCTS | CLOCAL; + attr.c_oflag = 0; + if (tcflush(fd, TCIOFLUSH) == -1) return 0; /* Error */ + if (tcsetattr(fd, TCSANOW, &attr) == -1) return 0; /* Error */ + + /* Set the lines to a known state, and */ + /* finally return non-zero is successful. */ + return setRTS(0) && setDTR(0); +} + + +/* For the two functions below: + * level=0 to set line to LOW + * level=1 to set line to HIGH + */ + +int setRTS(unsigned short level) +{ + int status; + + if (ioctl(fd, TIOCMGET, &status) == -1) { + perror("setRTS(): TIOCMGET"); + return 0; + } + if (level) status |= TIOCM_RTS; + else status &= ~TIOCM_RTS; + if (ioctl(fd, TIOCMSET, &status) == -1) { + perror("setRTS(): TIOCMSET"); + return 0; + } + return 1; +} + + +int setDTR(unsigned short level) +{ + int status; + + if (ioctl(fd, TIOCMGET, &status) == -1) { + perror("setDTR(): TIOCMGET"); + return 0; + } + if (level) status |= TIOCM_DTR; + else status &= ~TIOCM_DTR; + if (ioctl(fd, TIOCMSET, &status) == -1) { + perror("setDTR: TIOCMSET"); + return 0; + } + return 1; +} + +/* This portion of code was written by Roger Clark + * It was informed by various other pieces of code written by Leaflabs to reset their + * Maple and Maple mini boards + */ + +main(int argc, char *argv[]) +{ + + if (argc<2 || argc >3) + { + printf("Usage upload-reset \n\r"); + return; + } + + if (openserial(argv[1])) + { + // Send magic sequence of DTR and RTS followed by the magic word "1EAF" + setRTS(false); + setDTR(false); + setDTR(true); + + usleep(50000L); + + setDTR(false); + setRTS(true); + setDTR(true); + + usleep(50000L); + + setDTR(false); + + usleep(50000L); + + write(fd,"1EAF",4); + + closeserial(); + if (argc==3) + { + usleep(atol(argv[2])*1000L); + } + } + else + { + printf("Failed to open serial device.\n\r"); + } +} diff --git a/tools/src/texane-stlink b/tools/src/texane-stlink deleted file mode 160000 index 1b46200..0000000 --- a/tools/src/texane-stlink +++ /dev/null @@ -1 +0,0 @@ -Subproject commit 1b46200b12ff8764a869a7365eb8253622100fed diff --git a/tools/win/debugging.bat b/tools/win/debugging.bat deleted file mode 100644 index c45729e..0000000 --- a/tools/win/debugging.bat +++ /dev/null @@ -1,21 +0,0 @@ -@Echo off -set fileName=%~n2 -set BaseDir=C:\Debugging -set OutputDir=%BaseDir%\%fileName% -IF NOT EXIST %BaseDir%\*.* (echo Need to create directory %BaseDir%) -IF NOT EXIST %OutputDir%\*.* (md %OutputDir%) -set sizeFile=%fileName%.size.txt -set nmFile=%fileName%.nm.txt -set dasmFile=%fileName%.dasm.txt -set pcompFile=%fileName%.pcomp.txt -cd %1 - -arm-none-eabi-objdump.exe -d -S -l %2.elf > %OutputDir%\%dasmFile% - -arm-none-eabi-nm.exe --format="sysv" -C -l %2.elf > %OutputDir%\%nmFile% - -arm-none-eabi-size.exe -t -A %2.elf > %OutputDir%\%sizeFile% -arm-none-eabi-size.exe -t -A -x %2.elf >> %OutputDir%\%sizeFile% -start %OutputDir% - -echo Done! \ No newline at end of file diff --git a/tools/win/src/build_dfu-util.sh b/tools/win/src/build_dfu-util.sh new file mode 100644 index 0000000..3563f57 --- /dev/null +++ b/tools/win/src/build_dfu-util.sh @@ -0,0 +1,15 @@ +#!/bin/bash + +sudo apt-get build-dep dfu-util +sudo apt-get install build-essentials +sudo apt-get install libusb-1.0-0-dev +sudo apt-get install autoconf automake autotools-dev + +cd dfu-util +./autogen.sh +./configure +make +cp src/dfu-util ../../linux/dfu-util +cp src/dfu-suffix ../../linux/dfu-util +cp src/dfu-prefix ../../linux/dfu-util + diff --git a/tools/win/src/dfu-util/AUTHORS b/tools/win/src/dfu-util/AUTHORS new file mode 100644 index 0000000..1b36c73 --- /dev/null +++ b/tools/win/src/dfu-util/AUTHORS @@ -0,0 +1,30 @@ +Authors ordered by first contribution. + +Harald Welte +Werner Almesberger +Michael Lauer +Jim Huang +Stefan Schmidt +Daniel Willmann +Mike Frysinger +Uwe Hermann +C. Scott Ananian +Bernard Blackham +Holger Freyther +Marc Singer +James Perkins +Tommi Keisala +Pascal Schweizer +Bradley Scott +Uwe Bonnes +Andrey Smirnov +Jussi Timperi +Hans Petter Selasky +Bo Shen +Henrique de Almeida Mendonca +Bernd Krumboeck +Dennis Meier +Veli-Pekka Peltola +Dave Hylands +Michael Grzeschik +Paul Fertser diff --git a/tools/win/src/dfu-util/COPYING b/tools/win/src/dfu-util/COPYING new file mode 100644 index 0000000..d60c31a --- /dev/null +++ b/tools/win/src/dfu-util/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. + + + Copyright (C) + + 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. + + , 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/tools/win/src/dfu-util/ChangeLog b/tools/win/src/dfu-util/ChangeLog new file mode 100644 index 0000000..37f1add --- /dev/null +++ b/tools/win/src/dfu-util/ChangeLog @@ -0,0 +1,93 @@ +0.8: + o New, separate dfu-prefix tool (Uwe Bonnes) + o Allow filtering on serial number (Uwe Bonnes) + o Improved VID/PID/serial filtering (Bradley Scott) + o Support reading firmware from stdin (Tormod Volden) + o Warn if missing DFU suffix (Tormod Volden) + o Improved progress bar (Hans Petter Selasky) + o Fix dfuse leave option (Uwe Bonnes) + o Major code rework (Hans Petter Selasky) + o MS Visual Studio build support (Henrique Mendonca) + o dfuse-pack.py tool for .dfu files (Antonio Galeo) + o Many other fixes from many people + +2014-09-13: Tormod Volden + +0.7: + o Support for TI Stellaris devices (Tommi Keisala) + o Fix libusb detection on MacOSX (Marc Singer) + o Fix libusb detection on FreeBSD (Tormod Volden) + o Improved DfuSe support (Tormod Volden) + o Support all special commands (leave, unprotect, mass-erase) + o Arbitrary upload lengths + o "force" option for various possible (dangerous) overrides + +2012-10-07: Tormod Volden + +0.6: + o Add detach mode (Stefan Schmidt) + o Check return value on all libusb calls (Tormod Volden) + o Fix segmentation fault with -s option (Tormod Volden) + o Add DFU suffix manipulation tool (Stefan Schmidt) + o Port to Windows: (Tormod Volden, some parts based on work from Satz + Klauer) + o Port file handling to stdio streams + o Sleep() macros + o C99 types + o Pack structs + o Detect DfuSe device correctly on big-endian architectures (Tormod + Volden) + o Add dfuse progress indication on download (Tormod Volden) + o Cleanup: gcc pedantic, gcc extension, ... (Tormod Volden) + o Rely on page size from functional descriptor. Please report if you get + an error about it. (Tormod Volden) + o Add quirk for Maple since it reports wrong DFU version (Tormod Volden) + +2012-04-22: Stefan Schmidt + +0.5: + o DfuSe extension support for ST devices (Tormod Volden) + o Add initial support for bitWillDetach flag from DFU 1.1 (Tormod + Volden) + o Internal cleanup and some manual page fixes (Tormod Volden) + +2011-11-02: Stefan Schmidt + +0.4: + o Rework to use libusb-1.0 (Stefan Schmidt) + o DFU suffix support (Tormod Volden, Stefan Schmidt) + o Sspeed up DFU downloads directly into memory (Bernard Blackham) + o More flexible -d vid:pid parsing (Tormod Volden) + o Many bug fixes and cleanups + +2011-07-20: Stefan Schmidt + +0.3: + o quirks: Add OpenOCD to the poll timeout quirk table. + +2010-12-22: Stefan Schmidt + +0.2: + o Fix some typos on the website and the README (Antonio Ospite, Uwe + Hermann) + o Remove build rule for a static binary. We can use autotools for this. + (Mike Frysinger) + o Fix infinite loop in download error path (C. Scott Ananian) + o Break out to show the 'finished' in upload (C. Scott Ananian) + o Add GPLv2+ headers (Harald Welte) + o Remove dead code (commands.[ch]) remnescent of dfu-programmer (Harald + Welte) + o Simple quirk system with Openmoko quirk for missing bwPollTimeout (Tormod Volden) + o New default (1024) and clamping of transfer size (Tormod Volden) + o Verify sending of completion packet (Tormod Volden) + o Look for DFU functional descriptor among all descriptors (Tormod + Volden) + o Print out in which direction we are transferring data + o Abort in upload if the file already exists + +2010-11-17 Stefan Schmidt + +0.1: + Initial release + +2010-05-23 Stefan Schmidt diff --git a/tools/win/src/dfu-util/DEVICES.txt b/tools/win/src/dfu-util/DEVICES.txt new file mode 100644 index 0000000..bdd9f1f --- /dev/null +++ b/tools/win/src/dfu-util/DEVICES.txt @@ -0,0 +1,20 @@ +List of supported software and hardware products: + +Software user (bootloader, etc) +------------------------------- +- Sam7DFU: http://www.openpcd.org/Sam7dfu +- U-boot: DFU patches +- Barebox: http://www.barebox.org/ +- Leaflabs: http://code.google.com/p/leaflabs/ +- Blackmagic DFU + +Products using DFU +------------------ +- OpenPCD (sam7dfu) +- Openmoko Neo 1973 and Freerunner (u-boot with DFU patches) +- Leaflabs Maple +- ATUSB from Qi Hardware +- STM32F105/7, STM32F2/F3/F4 in System Bootloader +- Blackmagic debug probe +- NXP LPC31xx/LPC43XX, e.g. LPC-Link and LPC-Link2, need binaries + with LPC prefix and encoding (LPC-Link) diff --git a/tools/win/src/dfu-util/Makefile.am b/tools/win/src/dfu-util/Makefile.am new file mode 100644 index 0000000..641dda5 --- /dev/null +++ b/tools/win/src/dfu-util/Makefile.am @@ -0,0 +1,3 @@ +SUBDIRS = src doc + +EXTRA_DIST = autogen.sh TODO DEVICES.txt dfuse-pack.py diff --git a/tools/win/src/dfu-util/README b/tools/win/src/dfu-util/README new file mode 100644 index 0000000..0f8f262 --- /dev/null +++ b/tools/win/src/dfu-util/README @@ -0,0 +1,20 @@ +Dfu-util - Device Firmware Upgrade Utilities + +Dfu-util is the host side implementation of the DFU 1.0 [1] and DFU 1.1 [2] +specification of the USB forum. + +DFU is intended to download and upload firmware to devices connected over +USB. It ranges from small devices like micro-controller boards up to mobile +phones. With dfu-util you are able to download firmware to your device or +upload firmware from it. + +dfu-util has been tested with Openmoko Neo1973 and Freerunner and many +other devices. + +[1] DFU 1.0 spec: http://www.usb.org/developers/devclass_docs/usbdfu10.pdf +[2] DFU 1.1 spec: http://www.usb.org/developers/devclass_docs/DFU_1.1.pdf + +The official website is: + + http://dfu-util.gnumonks.org/ + diff --git a/tools/win/src/dfu-util/TODO b/tools/win/src/dfu-util/TODO new file mode 100644 index 0000000..900c30c --- /dev/null +++ b/tools/win/src/dfu-util/TODO @@ -0,0 +1,14 @@ +DfuSe: +- Do erase and write in two separate passes when downloading +- Skip "Set Address" command when downloading contiguous blocks +- Implement "Get Commands" command + +Devices: +- Research iPhone/iPod/iPad support + Heavily modified dfu-util fork here: + https://github.com/planetbeing/xpwn/tree/master/dfu-util +- Test against Niftylights + +Non-Code: +- Logo +- Re-License as LGPL for usage as library? diff --git a/tools/win/src/dfu-util/autogen.sh b/tools/win/src/dfu-util/autogen.sh new file mode 100644 index 0000000..e67aed3 --- /dev/null +++ b/tools/win/src/dfu-util/autogen.sh @@ -0,0 +1,2 @@ +#! /bin/sh +autoreconf -v -i diff --git a/tools/win/src/dfu-util/configure.ac b/tools/win/src/dfu-util/configure.ac new file mode 100644 index 0000000..8622114 --- /dev/null +++ b/tools/win/src/dfu-util/configure.ac @@ -0,0 +1,41 @@ +# -*- Autoconf -*- +# Process this file with autoconf to produce a configure script. + +AC_PREREQ(2.59) +AC_INIT([dfu-util],[0.8],[dfu-util@lists.gnumonks.org],,[http://dfu-util.gnumonks.org]) +AC_CONFIG_AUX_DIR(m4) +AM_INIT_AUTOMAKE([foreign]) +AC_CONFIG_HEADERS([config.h]) + +# Test for new silent rules and enable only if they are available +m4_ifdef([AM_SILENT_RULES], [AM_SILENT_RULES([yes])]) + +# Checks for programs. +AC_PROG_CC + +# Checks for libraries. +# On FreeBSD the libusb-1.0 is called libusb and resides in system location +AC_CHECK_LIB([usb], [libusb_init],, [native_libusb=no],) +AS_IF([test x$native_libusb = xno], [ + PKG_CHECK_MODULES([USB], [libusb-1.0 >= 1.0.0],, + AC_MSG_ERROR([*** Required libusb-1.0 >= 1.0.0 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([usbpath.h windows.h sysexits.h]) + +# Checks for typedefs, structures, and compiler characteristics. +AC_C_CONST +AC_TYPE_SIZE_T + +# Checks for library functions. +AC_FUNC_MEMCMP +AC_CHECK_FUNCS([ftruncate getpagesize nanosleep err]) + +AC_CONFIG_FILES(Makefile src/Makefile doc/Makefile) +AC_OUTPUT diff --git a/tools/win/src/dfu-util/device-logs/README b/tools/win/src/dfu-util/device-logs/README new file mode 100644 index 0000000..00d3d1a --- /dev/null +++ b/tools/win/src/dfu-util/device-logs/README @@ -0,0 +1,77 @@ +Device: +------- +qi-hardware-atusb: +- Qi Hardware ben-wpan +- DFU implementation: + http://projects.qi-hardware.com/index.php/p/ben-wpan/source/tree/master/atusb/fw/usb +- Tester: Stefan Schmidt + +openpcd: +- OpenPCD RFID reader +- DFU implementation: SAM7DFU + http://www.openpcd.org/Sam7dfu, git://git.gnumonks.org/openpcd.git +- Tester: Stefan Schmidt + +simtrace: +- Sysmocom SimTrace +- DFU implementation: SAM7DFU + http://www.openpcd.org/Sam7dfu, git://git.gnumonks.org/openpcd.git +- Tester: Stefan Schmidt + +openmoko-freerunner: +- Openmoko Freerunner +- DFU implementation: Old U-Boot + http://git.openmoko.org/?p=u-boot.git;a=shortlog;h=refs/heads/mokopatches +- Tester: Stefan Schmidt + +openmoko-neo1973: +- Openmoko Neo1073 +- DFU implementation: Old U-Boot + http://git.openmoko.org/?p=u-boot.git;a=shortlog;h=refs/heads/mokopatches +- Tester: Stefan Schmidt + +tdk-bluetooth: +- TDK Corp. Bluetooth Adapter +- DFU implementation: closed soure +- Only upload has been tested +- Tester: Stefan Schmidt + +stm32f107: +- STM32 microcontrollers with built-in (ROM) DFU loader +- DFU implementation: Closed source but probably similar to the one + in their USB device libraries. Some relevant application notes: + http://www.st.com -> AN3156 and AN2606 +- Tested by Uwe Bonnes + +stm32f4discovery: +- STM32 microcontroller board with built-in (ROM) DFU loader +- DFU implementation: Closed source, probably similar to stm32f107. +- Tested by Joe Rothweiler + +dso-nano: +- DSO Nano pocket oscilloscope +- DFU implementation: Based on ST Microelectronics USB FS Library 1.0 + http://dsonano.googlecode.com/files/DS0201_OpenSource.rar +- Tester: Tormod Volden + +opc-20: +- Custom devices based on STM32F1xx +- DFU implementation: ST Microelectronics USB FS Device Library 3.1.0 + http://www.st.com -> um0424.zip +- Tester: Tormod Volden + +lpc-link, lpclink2: +- NXP LPCXpresso debug adapters +- Proprietary DFU implementation, uses special download files with + LPC prefix and encoding of the target firmware code +- Tested by Uwe Bonnes + +Adding the lsusb output and a download log of your device here helps +us to avoid regressions for hardware we cannot test while working on +the code. To extract the lsusb output use this command: +sudo lsusb -v -d $USBID > $DEVICE.lsusb +Prepare a description snippet as above, and send it to us. A log +(copy-paste of the command window) of a firmware download is also +nice, please use the double verbose option -v -v and include the +command line in the log file. + diff --git a/tools/win/src/dfu-util/device-logs/dsonano.lsusb b/tools/win/src/dfu-util/device-logs/dsonano.lsusb new file mode 100644 index 0000000..140a7bc --- /dev/null +++ b/tools/win/src/dfu-util/device-logs/dsonano.lsusb @@ -0,0 +1,60 @@ + +Bus 002 Device 004: ID 0483:df11 SGS Thomson Microelectronics +Device Descriptor: + bLength 18 + bDescriptorType 1 + bcdUSB 1.00 + bDeviceClass 0 (Defined at Interface level) + bDeviceSubClass 0 + bDeviceProtocol 0 + bMaxPacketSize0 64 + idVendor 0x0483 SGS Thomson Microelectronics + idProduct 0xdf11 + bcdDevice 1.1a + iManufacturer 1 STMicroelectronics + iProduct 2 STM32 DFU + iSerial 3 001 + bNumConfigurations 1 + Configuration Descriptor: + bLength 9 + bDescriptorType 2 + wTotalLength 36 + bNumInterfaces 1 + bConfigurationValue 1 + iConfiguration 0 + bmAttributes 0x80 + (Bus Powered) + MaxPower 64mA + Interface Descriptor: + bLength 9 + bDescriptorType 4 + bInterfaceNumber 0 + bAlternateSetting 0 + bNumEndpoints 0 + bInterfaceClass 254 Application Specific Interface + bInterfaceSubClass 1 Device Firmware Update + bInterfaceProtocol 0 + iInterface 4 @Internal Flash /0x08000000/12*001Ka,116*001Kg + Interface Descriptor: + bLength 9 + bDescriptorType 4 + bInterfaceNumber 0 + bAlternateSetting 1 + bNumEndpoints 0 + bInterfaceClass 254 Application Specific Interface + bInterfaceSubClass 1 Device Firmware Update + bInterfaceProtocol 0 + iInterface 5 @SPI Flash : M25P64/0x00000000/64*064Kg,64*064Kg + Device Firmware Upgrade Interface Descriptor: + bLength 9 + bDescriptorType 33 + bmAttributes 11 + Will Detach + Manifestation Intolerant + Upload Supported + Download Supported + wDetachTimeout 255 milliseconds + wTransferSize 1024 bytes + bcdDFUVersion 1.1a +Device Status: 0x0000 + (Bus Powered) diff --git a/tools/win/src/dfu-util/device-logs/lpclink.log b/tools/win/src/dfu-util/device-logs/lpclink.log new file mode 100644 index 0000000..7de4dd3 --- /dev/null +++ b/tools/win/src/dfu-util/device-logs/lpclink.log @@ -0,0 +1,59 @@ +(The on-board LPC3154 has some encryption key set and LPCXpressoWIN.enc +is encrypted.) + +$ lsusb | grep NXP +Bus 003 Device 011: ID 0471:df55 Philips (or NXP) LPCXpresso LPC-Link + +$ dfu-util -v -v -v -R -D /opt/lpc/lpcxpresso/bin/LPCXpressoWIN.enc + +dfu-util 0.7 + +Copyright 2005-2008 Weston Schmidt, Harald Welte and OpenMoko Inc. +Copyright 2010-2012 Tormod Volden and Stefan Schmidt +This program is Free Software and has ABSOLUTELY NO WARRANTY +Please report bugs to dfu-util@lists.gnumonks.org + +dfu-util: Invalid DFU suffix signature +dfu-util: A valid DFU suffix will be required in a future dfu-util release!!! +Deducing device DFU version from functional descriptor length +Opening DFU capable USB device... +ID 0471:df55 +Run-time device DFU version 0100 +Claiming USB DFU Runtime Interface... +Determining device status: +state = dfuIDLE, status = 0 +dfu-util: WARNING: Runtime device already in DFU state ?!? +Claiming USB DFU Interface... +Setting Alternate Setting #0 ... +Determining device status: +state = dfuIDLE, status = 0 +dfuIDLE, continuing +DFU mode device DFU version 0100 +Device returned transfer size 2048 +Copying data from PC to DFU device +Download [ ] 0% 0 bytes +Download [= ] 6% 2048 bytes +Download [=== ] 13% 4096 bytes +Download [==== ] 19% 6144 bytes +Download [====== ] 26% 8192 bytes +Download [======== ] 32% 10240 bytes +Download [========= ] 39% 12288 bytes +Download [=========== ] 45% 14336 bytes +Download [============= ] 52% 16384 bytes +Download [============== ] 59% 18432 bytes +Download [================ ] 65% 20480 bytes +Download [================== ] 72% 22528 bytes +Download [=================== ] 78% 24576 bytes +Download [===================== ] 85% 26624 bytes +Download [====================== ] 91% 28672 bytes +Download [======================== ] 98% 29192 bytes +Download [=========================] 100% 29192 bytes +Download done. +Sent a total of 29192 bytes +state(8) = dfuMANIFEST-WAIT-RESET, status(0) = No error condition is present +Done! +dfu-util: can't detach +Resetting USB to switch back to runtime mode + +$ lsusb | grep NXP +Bus 003 Device 012: ID 1fc9:0009 NXP Semiconductors diff --git a/tools/win/src/dfu-util/device-logs/lpclink.lsusb b/tools/win/src/dfu-util/device-logs/lpclink.lsusb new file mode 100644 index 0000000..867b2a2 --- /dev/null +++ b/tools/win/src/dfu-util/device-logs/lpclink.lsusb @@ -0,0 +1,58 @@ + +Bus 003 Device 008: ID 0471:df55 Philips (or NXP) LPCXpresso LPC-Link +Device Descriptor: + bLength 18 + bDescriptorType 1 + bcdUSB 2.00 + bDeviceClass 0 (Defined at Interface level) + bDeviceSubClass 0 + bDeviceProtocol 0 + bMaxPacketSize0 64 + idVendor 0x0471 Philips (or NXP) + idProduct 0xdf55 LPCXpresso LPC-Link + bcdDevice 0.01 + iManufacturer 0 + iProduct 0 + iSerial 0 + bNumConfigurations 1 + Configuration Descriptor: + bLength 9 + bDescriptorType 2 + wTotalLength 25 + bNumInterfaces 1 + bConfigurationValue 1 + iConfiguration 0 + bmAttributes 0x80 + (Bus Powered) + MaxPower 100mA + Interface Descriptor: + bLength 9 + bDescriptorType 4 + bInterfaceNumber 0 + bAlternateSetting 0 + bNumEndpoints 0 + bInterfaceClass 254 Application Specific Interface + bInterfaceSubClass 1 Device Firmware Update + bInterfaceProtocol 0 + iInterface 0 + Device Firmware Upgrade Interface Descriptor: + bLength 7 + bDescriptorType 33 + bmAttributes 1 + Will Not Detach + Manifestation Intolerant + Upload Unsupported + Download Supported + wDetachTimeout 65535 milliseconds + wTransferSize 2048 bytes +Device Qualifier (for other device speed): + bLength 10 + bDescriptorType 6 + bcdUSB 2.00 + bDeviceClass 0 (Defined at Interface level) + bDeviceSubClass 0 + bDeviceProtocol 0 + bMaxPacketSize0 64 + bNumConfigurations 1 +Device Status: 0x0000 + (Bus Powered) diff --git a/tools/win/src/dfu-util/device-logs/lpclink2.log b/tools/win/src/dfu-util/device-logs/lpclink2.log new file mode 100644 index 0000000..4681eff --- /dev/null +++ b/tools/win/src/dfu-util/device-logs/lpclink2.log @@ -0,0 +1,59 @@ +$ lsusb | grep NXP +Bus 003 Device 013: ID 1fc9:000c NXP Semiconductors + +$ dfu-util -D ~/devel/dfu-util/firmware.bin.qthdr + +dfu-util 0.7 + +Copyright 2005-2008 Weston Schmidt, Harald Welte and OpenMoko Inc. +Copyright 2010-2012 Tormod Volden and Stefan Schmidt +This program is Free Software and has ABSOLUTELY NO WARRANTY +Please report bugs to dfu-util@lists.gnumonks.org + +dfu-util: Invalid DFU suffix signature +dfu-util: A valid DFU suffix will be required in a future dfu-util release!!! +Possible unencryptes NXP LPC DFU prefix with the following properties +Payload length: 39 kiByte +Opening DFU capable USB device... +ID 1fc9:000c +Run-time device DFU version 0100 +Claiming USB DFU Runtime Interface... +Determining device status: +state = dfuIDLE, status = 0 +dfu-util: WARNING: Runtime device already in DFU state ?!? +Claiming USB DFU Interface... +Setting Alternate Setting #0 ... +Determining device status: +state = dfuIDLE, status = 0 +dfuIDLE, continuing +DFU mode device DFU version 0100 +Device returned transfer size 2048 +Copying data from PC to DFU device +Download [ ] 0% 0 bytes +Download [= ] 4% 2048 bytes +Download [== ] 9% 4096 bytes +Download [=== ] 14% 6144 bytes +Download [==== ] 19% 8192 bytes +Download [====== ] 24% 10240 bytes +Download [======= ] 28% 12288 bytes +Download [======== ] 33% 14336 bytes +Download [========= ] 38% 16384 bytes +Download [========== ] 43% 18432 bytes +Download [============ ] 48% 20480 bytes +Download [============= ] 53% 22528 bytes +Download [============== ] 57% 24576 bytes +Download [=============== ] 62% 26624 bytes +Download [================ ] 67% 28672 bytes +Download [================== ] 72% 30720 bytes +Download [=================== ] 77% 32768 bytes +Download [==================== ] 82% 34816 bytes +Download [===================== ] 86% 36864 bytes +Download [====================== ] 91% 38912 bytes +Download [======================== ] 96% 40356 bytes +Download [=========================] 100% 40356 bytes +Download done. +Sent a total of 40356 bytes +dfu-util: unable to read DFU status + +$ lsusb | grep NXP +Bus 003 Device 014: ID 1fc9:0018 NXP Semiconductors diff --git a/tools/win/src/dfu-util/device-logs/lpclink2.lsusb b/tools/win/src/dfu-util/device-logs/lpclink2.lsusb new file mode 100644 index 0000000..b833fca --- /dev/null +++ b/tools/win/src/dfu-util/device-logs/lpclink2.lsusb @@ -0,0 +1,203 @@ + +Bus 003 Device 007: ID 0c72:000c PEAK System PCAN-USB +Device Descriptor: + bLength 18 + bDescriptorType 1 + bcdUSB 1.00 + bDeviceClass 0 (Defined at Interface level) + bDeviceSubClass 0 + bDeviceProtocol 0 + bMaxPacketSize0 16 + idVendor 0x0c72 PEAK System + idProduct 0x000c PCAN-USB + bcdDevice 1c.ff + iManufacturer 0 + iProduct 3 VER1:PEAK +VER2:02.8.01 +DAT :06.05.2004 +TIME:09:35:37 + ... + iSerial 0 + bNumConfigurations 3 + Configuration Descriptor: + bLength 9 + bDescriptorType 2 + wTotalLength 46 + bNumInterfaces 1 + bConfigurationValue 1 + iConfiguration 0 + bmAttributes 0x80 + (Bus Powered) + MaxPower 200mA + Interface Descriptor: + bLength 9 + bDescriptorType 4 + bInterfaceNumber 0 + bAlternateSetting 0 + bNumEndpoints 4 + bInterfaceClass 0 (Defined at Interface level) + bInterfaceSubClass 0 + bInterfaceProtocol 0 + iInterface 0 + Endpoint Descriptor: + bLength 7 + bDescriptorType 5 + bEndpointAddress 0x81 EP 1 IN + bmAttributes 2 + Transfer Type Bulk + Synch Type None + Usage Type Data + wMaxPacketSize 0x0010 1x 16 bytes + bInterval 20 + Endpoint Descriptor: + bLength 7 + bDescriptorType 5 + bEndpointAddress 0x01 EP 1 OUT + bmAttributes 2 + Transfer Type Bulk + Synch Type None + Usage Type Data + wMaxPacketSize 0x0010 1x 16 bytes + bInterval 20 + Endpoint Descriptor: + bLength 7 + bDescriptorType 5 + bEndpointAddress 0x82 EP 2 IN + bmAttributes 2 + Transfer Type Bulk + Synch Type None + Usage Type Data + wMaxPacketSize 0x0040 1x 64 bytes + bInterval 1 + Endpoint Descriptor: + bLength 7 + bDescriptorType 5 + bEndpointAddress 0x02 EP 2 OUT + bmAttributes 2 + Transfer Type Bulk + Synch Type None + Usage Type Data + wMaxPacketSize 0x0040 1x 64 bytes + bInterval 1 + Configuration Descriptor: + bLength 9 + bDescriptorType 2 + wTotalLength 46 + bNumInterfaces 1 + bConfigurationValue 2 + iConfiguration 0 + bmAttributes 0x80 + (Bus Powered) + MaxPower 394mA + Interface Descriptor: + bLength 9 + bDescriptorType 4 + bInterfaceNumber 0 + bAlternateSetting 0 + bNumEndpoints 4 + bInterfaceClass 0 (Defined at Interface level) + bInterfaceSubClass 0 + bInterfaceProtocol 0 + iInterface 0 + Endpoint Descriptor: + bLength 7 + bDescriptorType 5 + bEndpointAddress 0x81 EP 1 IN + bmAttributes 2 + Transfer Type Bulk + Synch Type None + Usage Type Data + wMaxPacketSize 0x0010 1x 16 bytes + bInterval 20 + Endpoint Descriptor: + bLength 7 + bDescriptorType 5 + bEndpointAddress 0x01 EP 1 OUT + bmAttributes 2 + Transfer Type Bulk + Synch Type None + Usage Type Data + wMaxPacketSize 0x0010 1x 16 bytes + bInterval 20 + Endpoint Descriptor: + bLength 7 + bDescriptorType 5 + bEndpointAddress 0x82 EP 2 IN + bmAttributes 2 + Transfer Type Bulk + Synch Type None + Usage Type Data + wMaxPacketSize 0x0040 1x 64 bytes + bInterval 1 + Endpoint Descriptor: + bLength 7 + bDescriptorType 5 + bEndpointAddress 0x02 EP 2 OUT + bmAttributes 2 + Transfer Type Bulk + Synch Type None + Usage Type Data + wMaxPacketSize 0x0040 1x 64 bytes + bInterval 1 + Configuration Descriptor: + bLength 9 + bDescriptorType 2 + wTotalLength 46 + bNumInterfaces 1 + bConfigurationValue 3 + iConfiguration 0 + bmAttributes 0x80 + (Bus Powered) + MaxPower 200mA + Interface Descriptor: + bLength 9 + bDescriptorType 4 + bInterfaceNumber 0 + bAlternateSetting 0 + bNumEndpoints 4 + bInterfaceClass 0 (Defined at Interface level) + bInterfaceSubClass 0 + bInterfaceProtocol 0 + iInterface 0 + Endpoint Descriptor: + bLength 7 + bDescriptorType 5 + bEndpointAddress 0x81 EP 1 IN + bmAttributes 3 + Transfer Type Interrupt + Synch Type None + Usage Type Data + wMaxPacketSize 0x0010 1x 16 bytes + bInterval 1 + Endpoint Descriptor: + bLength 7 + bDescriptorType 5 + bEndpointAddress 0x01 EP 1 OUT + bmAttributes 3 + Transfer Type Interrupt + Synch Type None + Usage Type Data + wMaxPacketSize 0x0010 1x 16 bytes + bInterval 1 + Endpoint Descriptor: + bLength 7 + bDescriptorType 5 + bEndpointAddress 0x82 EP 2 IN + bmAttributes 2 + Transfer Type Bulk + Synch Type None + Usage Type Data + wMaxPacketSize 0x0040 1x 64 bytes + bInterval 1 + Endpoint Descriptor: + bLength 7 + bDescriptorType 5 + bEndpointAddress 0x02 EP 2 OUT + bmAttributes 2 + Transfer Type Bulk + Synch Type None + Usage Type Data + wMaxPacketSize 0x0040 1x 64 bytes + bInterval 1 +Device Status: 0x0001 + Self Powered diff --git a/tools/win/src/dfu-util/device-logs/opc-20.lsusb b/tools/win/src/dfu-util/device-logs/opc-20.lsusb new file mode 100644 index 0000000..580df90 --- /dev/null +++ b/tools/win/src/dfu-util/device-logs/opc-20.lsusb @@ -0,0 +1,60 @@ + +Bus 001 Device 004: ID 0483:df11 SGS Thomson Microelectronics +Device Descriptor: + bLength 18 + bDescriptorType 1 + bcdUSB 1.00 + bDeviceClass 0 (Defined at Interface level) + bDeviceSubClass 0 + bDeviceProtocol 0 + bMaxPacketSize0 64 + idVendor 0x0483 SGS Thomson Microelectronics + idProduct 0xdf11 + bcdDevice 2.00 + iManufacturer 1 STMicroelectronics + iProduct 2 STM32 DFU + iSerial 3 ÿÿÿÿÿÿÿÿÿÿÿÿ + bNumConfigurations 1 + Configuration Descriptor: + bLength 9 + bDescriptorType 2 + wTotalLength 36 + bNumInterfaces 1 + bConfigurationValue 1 + iConfiguration 0 + bmAttributes 0xc0 + Self Powered + MaxPower 100mA + Interface Descriptor: + bLength 9 + bDescriptorType 4 + bInterfaceNumber 0 + bAlternateSetting 0 + bNumEndpoints 0 + bInterfaceClass 254 Application Specific Interface + bInterfaceSubClass 1 Device Firmware Update + bInterfaceProtocol 2 + iInterface 4 @Internal Flash /0x08000000/12*001Ka,116*001Kg + Interface Descriptor: + bLength 9 + bDescriptorType 4 + bInterfaceNumber 0 + bAlternateSetting 1 + bNumEndpoints 0 + bInterfaceClass 254 Application Specific Interface + bInterfaceSubClass 1 Device Firmware Update + bInterfaceProtocol 2 + iInterface 5 @SPI Flash : M25P64/0x00000000/128*64Kg + Device Firmware Upgrade Interface Descriptor: + bLength 9 + bDescriptorType 33 + bmAttributes 11 + Will Detach + Manifestation Intolerant + Upload Supported + Download Supported + wDetachTimeout 255 milliseconds + wTransferSize 1024 bytes + bcdDFUVersion 1a.01 +Device Status: 0x0001 + Self Powered diff --git a/tools/win/src/dfu-util/device-logs/openmoko-freerunner-dfumode.lsusb b/tools/win/src/dfu-util/device-logs/openmoko-freerunner-dfumode.lsusb new file mode 100644 index 0000000..4c0abfb --- /dev/null +++ b/tools/win/src/dfu-util/device-logs/openmoko-freerunner-dfumode.lsusb @@ -0,0 +1,109 @@ +Bus 003 Device 017: ID 1d50:5119 OpenMoko, Inc. GTA01/GTA02 U-Boot Bootloader +Device Descriptor: + bLength 18 + bDescriptorType 1 + bcdUSB 1.00 + bDeviceClass 0 (Defined at Interface level) + bDeviceSubClass 0 + bDeviceProtocol 0 + bMaxPacketSize0 16 + idVendor 0x1d50 OpenMoko, Inc. + idProduct 0x5119 GTA01/GTA02 U-Boot Bootloader + bcdDevice 0.00 + iManufacturer 1 OpenMoko, Inc + iProduct 2 Neo1973 Bootloader U-Boot 1.3.2-moko12 + iSerial 3 0000000 + bNumConfigurations 1 + Configuration Descriptor: + bLength 9 + bDescriptorType 2 + wTotalLength 81 + bNumInterfaces 1 + bConfigurationValue 1 + iConfiguration 7 USB Device Firmware Upgrade + bmAttributes 0x80 + (Bus Powered) + MaxPower 100mA + Interface Descriptor: + bLength 9 + bDescriptorType 4 + bInterfaceNumber 0 + bAlternateSetting 0 + bNumEndpoints 0 + bInterfaceClass 254 Application Specific Interface + bInterfaceSubClass 1 Device Firmware Update + bInterfaceProtocol 2 + iInterface 8 RAM 0x32000000 + Interface Descriptor: + bLength 9 + bDescriptorType 4 + bInterfaceNumber 0 + bAlternateSetting 1 + bNumEndpoints 0 + bInterfaceClass 254 Application Specific Interface + bInterfaceSubClass 1 Device Firmware Update + bInterfaceProtocol 2 + iInterface 9 u-boot + Interface Descriptor: + bLength 9 + bDescriptorType 4 + bInterfaceNumber 0 + bAlternateSetting 2 + bNumEndpoints 0 + bInterfaceClass 254 Application Specific Interface + bInterfaceSubClass 1 Device Firmware Update + bInterfaceProtocol 2 + iInterface 10 u-boot_env + Interface Descriptor: + bLength 9 + bDescriptorType 4 + bInterfaceNumber 0 + bAlternateSetting 3 + bNumEndpoints 0 + bInterfaceClass 254 Application Specific Interface + bInterfaceSubClass 1 Device Firmware Update + bInterfaceProtocol 2 + iInterface 11 kernel + Interface Descriptor: + bLength 9 + bDescriptorType 4 + bInterfaceNumber 0 + bAlternateSetting 4 + bNumEndpoints 0 + bInterfaceClass 254 Application Specific Interface + bInterfaceSubClass 1 Device Firmware Update + bInterfaceProtocol 2 + iInterface 12 splash + Interface Descriptor: + bLength 9 + bDescriptorType 4 + bInterfaceNumber 0 + bAlternateSetting 5 + bNumEndpoints 0 + bInterfaceClass 254 Application Specific Interface + bInterfaceSubClass 1 Device Firmware Update + bInterfaceProtocol 2 + iInterface 13 factory + Interface Descriptor: + bLength 9 + bDescriptorType 4 + bInterfaceNumber 0 + bAlternateSetting 6 + bNumEndpoints 0 + bInterfaceClass 254 Application Specific Interface + bInterfaceSubClass 1 Device Firmware Update + bInterfaceProtocol 2 + iInterface 14 rootfs + Device Firmware Upgrade Interface Descriptor: + bLength 9 + bDescriptorType 33 + bmAttributes 7 + Will Not Detach + Manifestation Tolerant + Upload Supported + Download Supported + wDetachTimeout 65280 milliseconds + wTransferSize 4096 bytes + bcdDFUVersion 1.00 +Device Status: 0x0a00 + (Bus Powered) diff --git a/tools/win/src/dfu-util/device-logs/openmoko-freerunner.lsusb b/tools/win/src/dfu-util/device-logs/openmoko-freerunner.lsusb new file mode 100644 index 0000000..835708d --- /dev/null +++ b/tools/win/src/dfu-util/device-logs/openmoko-freerunner.lsusb @@ -0,0 +1,179 @@ +Bus 005 Device 033: ID 1d50:5119 OpenMoko, Inc. GTA01/GTA02 U-Boot Bootloader +Device Descriptor: + bLength 18 + bDescriptorType 1 + bcdUSB 1.10 + bDeviceClass 2 Communications + bDeviceSubClass 0 + bDeviceProtocol 0 + bMaxPacketSize0 16 + idVendor 0x1d50 OpenMoko, Inc. + idProduct 0x5119 GTA01/GTA02 U-Boot Bootloader + bcdDevice 0.00 + iManufacturer 1 OpenMoko, Inc + iProduct 2 Neo1973 Bootloader U-Boot 1.3.2-moko12 + iSerial 3 0000000 + bNumConfigurations 2 + Configuration Descriptor: + bLength 9 + bDescriptorType 2 + wTotalLength 85 + bNumInterfaces 3 + bConfigurationValue 1 + iConfiguration 4 TTY via USB + bmAttributes 0x80 + (Bus Powered) + MaxPower 500mA + Interface Descriptor: + bLength 9 + bDescriptorType 4 + bInterfaceNumber 0 + bAlternateSetting 0 + bNumEndpoints 1 + bInterfaceClass 2 Communications + bInterfaceSubClass 2 Abstract (modem) + bInterfaceProtocol 1 AT-commands (v.25ter) + iInterface 6 Control Interface + CDC Header: + bcdCDC 0.6e + CDC Call Management: + bmCapabilities 0x00 + bDataInterface 1 + CDC ACM: + bmCapabilities 0x00 + CDC Union: + bMasterInterface 0 + bSlaveInterface 1 + Endpoint Descriptor: + bLength 7 + bDescriptorType 5 + bEndpointAddress 0x81 EP 1 IN + bmAttributes 3 + Transfer Type Interrupt + Synch Type None + Usage Type Data + wMaxPacketSize 0x0010 1x 16 bytes + bInterval 255 + Interface Descriptor: + bLength 9 + bDescriptorType 4 + bInterfaceNumber 1 + bAlternateSetting 0 + bNumEndpoints 2 + bInterfaceClass 10 CDC Data + bInterfaceSubClass 0 Unused + bInterfaceProtocol 0 + iInterface 5 Bulk Data Interface + Endpoint Descriptor: + bLength 7 + bDescriptorType 5 + bEndpointAddress 0x02 EP 2 OUT + bmAttributes 2 + Transfer Type Bulk + Synch Type None + Usage Type Data + wMaxPacketSize 0x0010 1x 16 bytes + bInterval 255 + Endpoint Descriptor: + bLength 7 + bDescriptorType 5 + bEndpointAddress 0x83 EP 3 IN + bmAttributes 2 + Transfer Type Bulk + Synch Type None + Usage Type Data + wMaxPacketSize 0x0010 1x 16 bytes + bInterval 255 + Interface Descriptor: + bLength 9 + bDescriptorType 4 + bInterfaceNumber 2 + bAlternateSetting 0 + bNumEndpoints 0 + bInterfaceClass 254 Application Specific Interface + bInterfaceSubClass 1 Device Firmware Update + bInterfaceProtocol 1 + iInterface 7 USB Device Firmware Upgrade + Device Firmware Upgrade Interface Descriptor: + bLength 9 + bDescriptorType 33 + bmAttributes 7 + Will Not Detach + Manifestation Tolerant + Upload Supported + Download Supported + wDetachTimeout 65280 milliseconds + wTransferSize 4096 bytes + bcdDFUVersion 1.00 + Configuration Descriptor: + bLength 9 + bDescriptorType 2 + wTotalLength 67 + bNumInterfaces 2 + bConfigurationValue 2 + iConfiguration 4 TTY via USB + bmAttributes 0x80 + (Bus Powered) + MaxPower 100mA + Interface Descriptor: + bLength 9 + bDescriptorType 4 + bInterfaceNumber 0 + bAlternateSetting 0 + bNumEndpoints 1 + bInterfaceClass 2 Communications + bInterfaceSubClass 2 Abstract (modem) + bInterfaceProtocol 1 AT-commands (v.25ter) + iInterface 6 Control Interface + CDC Header: + bcdCDC 0.6e + CDC Call Management: + bmCapabilities 0x00 + bDataInterface 1 + CDC ACM: + bmCapabilities 0x00 + CDC Union: + bMasterInterface 0 + bSlaveInterface 1 + Endpoint Descriptor: + bLength 7 + bDescriptorType 5 + bEndpointAddress 0x81 EP 1 IN + bmAttributes 3 + Transfer Type Interrupt + Synch Type None + Usage Type Data + wMaxPacketSize 0x0010 1x 16 bytes + bInterval 255 + Interface Descriptor: + bLength 9 + bDescriptorType 4 + bInterfaceNumber 1 + bAlternateSetting 0 + bNumEndpoints 2 + bInterfaceClass 10 CDC Data + bInterfaceSubClass 0 Unused + bInterfaceProtocol 0 + iInterface 5 Bulk Data Interface + Endpoint Descriptor: + bLength 7 + bDescriptorType 5 + bEndpointAddress 0x02 EP 2 OUT + bmAttributes 2 + Transfer Type Bulk + Synch Type None + Usage Type Data + wMaxPacketSize 0x0010 1x 16 bytes + bInterval 255 + Endpoint Descriptor: + bLength 7 + bDescriptorType 5 + bEndpointAddress 0x83 EP 3 IN + bmAttributes 2 + Transfer Type Bulk + Synch Type None + Usage Type Data + wMaxPacketSize 0x0010 1x 16 bytes + bInterval 255 +Device Status: 0x9a00 + (Bus Powered) diff --git a/tools/win/src/dfu-util/device-logs/openmoko-neo1973.lsusb b/tools/win/src/dfu-util/device-logs/openmoko-neo1973.lsusb new file mode 100644 index 0000000..0778950 --- /dev/null +++ b/tools/win/src/dfu-util/device-logs/openmoko-neo1973.lsusb @@ -0,0 +1,182 @@ + +Bus 006 Device 020: ID 1457:5119 First International Computer, Inc. OpenMoko Neo1973 u-boot cdc_acm serial port +Device Descriptor: + bLength 18 + bDescriptorType 1 + bcdUSB 1.10 + bDeviceClass 2 Communications + bDeviceSubClass 0 + bDeviceProtocol 0 + bMaxPacketSize0 16 + idVendor 0x1457 First International Computer, Inc. + idProduct 0x5119 OpenMoko Neo1973 u-boot cdc_acm serial port + bcdDevice 0.00 + iManufacturer 1 + iProduct 2 + iSerial 3 + bNumConfigurations 2 + Configuration Descriptor: + bLength 9 + bDescriptorType 2 + wTotalLength 85 + bNumInterfaces 3 + bConfigurationValue 1 + iConfiguration 4 + bmAttributes 0x80 + (Bus Powered) + MaxPower 500mA + Interface Descriptor: + bLength 9 + bDescriptorType 4 + bInterfaceNumber 0 + bAlternateSetting 0 + bNumEndpoints 1 + bInterfaceClass 2 Communications + bInterfaceSubClass 2 Abstract (modem) + bInterfaceProtocol 1 AT-commands (v.25ter) + iInterface 6 + CDC Header: + bcdCDC 0.6e + CDC Call Management: + bmCapabilities 0x00 + bDataInterface 1 + CDC ACM: + bmCapabilities 0x00 + CDC Union: + bMasterInterface 0 + bSlaveInterface 1 + Endpoint Descriptor: + bLength 7 + bDescriptorType 5 + bEndpointAddress 0x81 EP 1 IN + bmAttributes 3 + Transfer Type Interrupt + Synch Type None + Usage Type Data + wMaxPacketSize 0x0010 1x 16 bytes + bInterval 255 + Interface Descriptor: + bLength 9 + bDescriptorType 4 + bInterfaceNumber 1 + bAlternateSetting 0 + bNumEndpoints 2 + bInterfaceClass 10 CDC Data + bInterfaceSubClass 0 Unused + bInterfaceProtocol 0 + iInterface 5 + Endpoint Descriptor: + bLength 7 + bDescriptorType 5 + bEndpointAddress 0x02 EP 2 OUT + bmAttributes 2 + Transfer Type Bulk + Synch Type None + Usage Type Data + wMaxPacketSize 0x0010 1x 16 bytes + bInterval 255 + Endpoint Descriptor: + bLength 7 + bDescriptorType 5 + bEndpointAddress 0x83 EP 3 IN + bmAttributes 2 + Transfer Type Bulk + Synch Type None + Usage Type Data + wMaxPacketSize 0x0010 1x 16 bytes + bInterval 255 + Interface Descriptor: + bLength 9 + bDescriptorType 4 + bInterfaceNumber 2 + bAlternateSetting 0 + bNumEndpoints 0 + bInterfaceClass 254 Application Specific Interface + bInterfaceSubClass 1 Device Firmware Update + bInterfaceProtocol 1 + iInterface 7 + Device Firmware Upgrade Interface Descriptor: + bLength 9 + bDescriptorType 33 + bmAttributes 7 + Will Not Detach + Manifestation Tolerant + Upload Supported + Download Supported + wDetachTimeout 65280 milliseconds + wTransferSize 4096 bytes + bcdDFUVersion 1.00 + Configuration Descriptor: + bLength 9 + bDescriptorType 2 + wTotalLength 67 + bNumInterfaces 2 + bConfigurationValue 2 + iConfiguration 4 + bmAttributes 0x80 + (Bus Powered) + MaxPower 100mA + Interface Descriptor: + bLength 9 + bDescriptorType 4 + bInterfaceNumber 0 + bAlternateSetting 0 + bNumEndpoints 1 + bInterfaceClass 2 Communications + bInterfaceSubClass 2 Abstract (modem) + bInterfaceProtocol 1 AT-commands (v.25ter) + iInterface 6 + CDC Header: + bcdCDC 0.6e + CDC Call Management: + bmCapabilities 0x00 + bDataInterface 1 + CDC ACM: + bmCapabilities 0x00 + CDC Union: + bMasterInterface 0 + bSlaveInterface 1 + Endpoint Descriptor: + bLength 7 + bDescriptorType 5 + bEndpointAddress 0x81 EP 1 IN + bmAttributes 3 + Transfer Type Interrupt + Synch Type None + Usage Type Data + wMaxPacketSize 0x0010 1x 16 bytes + bInterval 255 + Interface Descriptor: + bLength 9 + bDescriptorType 4 + bInterfaceNumber 1 + bAlternateSetting 0 + bNumEndpoints 2 + bInterfaceClass 10 CDC Data + bInterfaceSubClass 0 Unused + bInterfaceProtocol 0 + iInterface 5 + Endpoint Descriptor: + bLength 7 + bDescriptorType 5 + bEndpointAddress 0x02 EP 2 OUT + bmAttributes 2 + Transfer Type Bulk + Synch Type None + Usage Type Data + wMaxPacketSize 0x0010 1x 16 bytes + bInterval 255 + Endpoint Descriptor: + bLength 7 + bDescriptorType 5 + bEndpointAddress 0x83 EP 3 IN + bmAttributes 2 + Transfer Type Bulk + Synch Type None + Usage Type Data + wMaxPacketSize 0x0010 1x 16 bytes + bInterval 255 +Device Status: 0x0006 + (Bus Powered) + Remote Wakeup Enabled + Test Mode diff --git a/tools/win/src/dfu-util/device-logs/openpcd.lsusb b/tools/win/src/dfu-util/device-logs/openpcd.lsusb new file mode 100644 index 0000000..f6255a9 --- /dev/null +++ b/tools/win/src/dfu-util/device-logs/openpcd.lsusb @@ -0,0 +1,60 @@ + +Bus 006 Device 016: ID 16c0:076b VOTI OpenPCD 13.56MHz RFID Reader +Device Descriptor: + bLength 18 + bDescriptorType 1 + bcdUSB 1.00 + bDeviceClass 0 (Defined at Interface level) + bDeviceSubClass 0 + bDeviceProtocol 0 + bMaxPacketSize0 8 + idVendor 0x16c0 VOTI + idProduct 0x076b OpenPCD 13.56MHz RFID Reader + bcdDevice 0.00 + iManufacturer 1 + iProduct 2 OpenPCD RFID Simulator - DFU Mode + iSerial 0 + bNumConfigurations 1 + Configuration Descriptor: + bLength 9 + bDescriptorType 2 + wTotalLength 36 + bNumInterfaces 1 + bConfigurationValue 1 + iConfiguration 0 + bmAttributes 0x80 + (Bus Powered) + MaxPower 200mA + Interface Descriptor: + bLength 9 + bDescriptorType 4 + bInterfaceNumber 0 + bAlternateSetting 0 + bNumEndpoints 0 + bInterfaceClass 254 Application Specific Interface + bInterfaceSubClass 1 Device Firmware Update + bInterfaceProtocol 2 + iInterface 0 + Interface Descriptor: + bLength 9 + bDescriptorType 4 + bInterfaceNumber 0 + bAlternateSetting 1 + bNumEndpoints 0 + bInterfaceClass 254 Application Specific Interface + bInterfaceSubClass 1 Device Firmware Update + bInterfaceProtocol 2 + iInterface 0 + Device Firmware Upgrade Interface Descriptor: + bLength 9 + bDescriptorType 33 + bmAttributes 3 + Will Not Detach + Manifestation Intolerant + Upload Supported + Download Supported + wDetachTimeout 65280 milliseconds + wTransferSize 256 bytes + bcdDFUVersion 1.00 +Device Status: 0x0000 + (Bus Powered) diff --git a/tools/win/src/dfu-util/device-logs/qi-hardware-atusb.lsusb b/tools/win/src/dfu-util/device-logs/qi-hardware-atusb.lsusb new file mode 100644 index 0000000..bfc1701 --- /dev/null +++ b/tools/win/src/dfu-util/device-logs/qi-hardware-atusb.lsusb @@ -0,0 +1,59 @@ + +Bus 006 Device 013: ID 20b7:1540 Qi Hardware ben-wpan, AT86RF230-based +Device Descriptor: + bLength 18 + bDescriptorType 1 + bcdUSB 2.00 + bDeviceClass 255 Vendor Specific Class + bDeviceSubClass 0 + bDeviceProtocol 0 + bMaxPacketSize0 64 + idVendor 0x20b7 Qi Hardware + idProduct 0x1540 ben-wpan, AT86RF230-based + bcdDevice 0.01 + iManufacturer 0 + iProduct 0 + iSerial 1 4630333438371508231a + bNumConfigurations 1 + Configuration Descriptor: + bLength 9 + bDescriptorType 2 + wTotalLength 34 + bNumInterfaces 2 + bConfigurationValue 1 + iConfiguration 0 + bmAttributes 0x80 + (Bus Powered) + MaxPower 40mA + Interface Descriptor: + bLength 9 + bDescriptorType 4 + bInterfaceNumber 0 + bAlternateSetting 0 + bNumEndpoints 1 + bInterfaceClass 255 Vendor Specific Class + bInterfaceSubClass 0 + bInterfaceProtocol 0 + iInterface 0 + Endpoint Descriptor: + bLength 7 + bDescriptorType 5 + bEndpointAddress 0x81 EP 1 IN + bmAttributes 2 + Transfer Type Bulk + Synch Type None + Usage Type Data + wMaxPacketSize 0x0040 1x 64 bytes + bInterval 0 + Interface Descriptor: + bLength 9 + bDescriptorType 4 + bInterfaceNumber 1 + bAlternateSetting 0 + bNumEndpoints 0 + bInterfaceClass 254 Application Specific Interface + bInterfaceSubClass 1 Device Firmware Update + bInterfaceProtocol 1 + iInterface 0 +Device Status: 0x0000 + (Bus Powered) diff --git a/tools/win/src/dfu-util/device-logs/simtrace.lsusb b/tools/win/src/dfu-util/device-logs/simtrace.lsusb new file mode 100644 index 0000000..578ddf0 --- /dev/null +++ b/tools/win/src/dfu-util/device-logs/simtrace.lsusb @@ -0,0 +1,70 @@ + +Bus 006 Device 017: ID 16c0:0762 VOTI +Device Descriptor: + bLength 18 + bDescriptorType 1 + bcdUSB 1.00 + bDeviceClass 0 (Defined at Interface level) + bDeviceSubClass 0 + bDeviceProtocol 0 + bMaxPacketSize0 8 + idVendor 0x16c0 VOTI + idProduct 0x0762 + bcdDevice 0.00 + iManufacturer 1 sysmocom - systems for mobile communications GmbH + iProduct 2 SimTrace SIM Sniffer - DFU Mode + iSerial 0 + bNumConfigurations 1 + Configuration Descriptor: + bLength 9 + bDescriptorType 2 + wTotalLength 45 + bNumInterfaces 1 + bConfigurationValue 1 + iConfiguration 3 SimTrace DFU Configuration + bmAttributes 0x80 + (Bus Powered) + MaxPower 200mA + Interface Descriptor: + bLength 9 + bDescriptorType 4 + bInterfaceNumber 0 + bAlternateSetting 0 + bNumEndpoints 0 + bInterfaceClass 254 Application Specific Interface + bInterfaceSubClass 1 Device Firmware Update + bInterfaceProtocol 2 + iInterface 4 SimTrace DFU Interface - Application Partition + Interface Descriptor: + bLength 9 + bDescriptorType 4 + bInterfaceNumber 0 + bAlternateSetting 1 + bNumEndpoints 0 + bInterfaceClass 254 Application Specific Interface + bInterfaceSubClass 1 Device Firmware Update + bInterfaceProtocol 2 + iInterface 5 SimTrace DFU Interface - Bootloader Partition + Interface Descriptor: + bLength 9 + bDescriptorType 4 + bInterfaceNumber 0 + bAlternateSetting 2 + bNumEndpoints 0 + bInterfaceClass 254 Application Specific Interface + bInterfaceSubClass 1 Device Firmware Update + bInterfaceProtocol 2 + iInterface 6 SimTrace DFU Interface - RAM + Device Firmware Upgrade Interface Descriptor: + bLength 9 + bDescriptorType 33 + bmAttributes 3 + Will Not Detach + Manifestation Intolerant + Upload Supported + Download Supported + wDetachTimeout 65280 milliseconds + wTransferSize 256 bytes + bcdDFUVersion 1.00 +Device Status: 0x0000 + (Bus Powered) diff --git a/tools/win/src/dfu-util/device-logs/sparkcore.lsusb b/tools/win/src/dfu-util/device-logs/sparkcore.lsusb new file mode 100644 index 0000000..b6029ff --- /dev/null +++ b/tools/win/src/dfu-util/device-logs/sparkcore.lsusb @@ -0,0 +1,60 @@ + +Bus 001 Device 008: ID 1d50:607f OpenMoko, Inc. +Device Descriptor: + bLength 18 + bDescriptorType 1 + bcdUSB 1.00 + bDeviceClass 0 (Defined at Interface level) + bDeviceSubClass 0 + bDeviceProtocol 0 + bMaxPacketSize0 64 + idVendor 0x1d50 OpenMoko, Inc. + idProduct 0x607f + bcdDevice 2.00 + iManufacturer 1 Spark Devices + iProduct 2 CORE DFU + iSerial 3 8D80527B5055 + bNumConfigurations 1 + Configuration Descriptor: + bLength 9 + bDescriptorType 2 + wTotalLength 36 + bNumInterfaces 1 + bConfigurationValue 1 + iConfiguration 0 + bmAttributes 0xc0 + Self Powered + MaxPower 100mA + Interface Descriptor: + bLength 9 + bDescriptorType 4 + bInterfaceNumber 0 + bAlternateSetting 0 + bNumEndpoints 0 + bInterfaceClass 254 Application Specific Interface + bInterfaceSubClass 1 Device Firmware Update + bInterfaceProtocol 2 + iInterface 4 @Internal Flash /0x08000000/20*001Ka,108*001Kg + Interface Descriptor: + bLength 9 + bDescriptorType 4 + bInterfaceNumber 0 + bAlternateSetting 1 + bNumEndpoints 0 + bInterfaceClass 254 Application Specific Interface + bInterfaceSubClass 1 Device Firmware Update + bInterfaceProtocol 2 + iInterface 5 @SPI Flash : SST25x/0x00000000/512*04Kg + Device Firmware Upgrade Interface Descriptor: + bLength 9 + bDescriptorType 33 + bmAttributes 11 + Will Detach + Manifestation Intolerant + Upload Supported + Download Supported + wDetachTimeout 255 milliseconds + wTransferSize 1024 bytes + bcdDFUVersion 1.1a +Device Status: 0x0001 + Self Powered diff --git a/tools/win/src/dfu-util/device-logs/stm32f107.bin-download b/tools/win/src/dfu-util/device-logs/stm32f107.bin-download new file mode 100644 index 0000000..45b714f --- /dev/null +++ b/tools/win/src/dfu-util/device-logs/stm32f107.bin-download @@ -0,0 +1,48 @@ +> src/dfu-util --intf 0 --alt 0 -v -v -v -s 0x8000000 -D test3 +dfu-util 0.4 + +(C) 2005-2008 by Weston Schmidt, Harald Welte and OpenMoko Inc. +(C) 2010-2011 Tormod Volden (DfuSe support) +This program is Free Software and has ABSOLUTELY NO WARRANTY + +dfu-util does currently only support DFU version 1.0 + +Opening DFU USB device... ID 0483:df11 +Run-time device DFU version 011a +Found DFU: [0483:df11] devnum=0, cfg=1, intf=0, alt=0, name="@Internal Flash /0x08000000/128*002Kg" +Claiming USB DFU Interface... +Setting Alternate Setting #0 ... +Determining device status: state = dfuIDLE, status = 0 +dfuIDLE, continuing +DFU mode device DFU version 011a +Device returned transfer size 2048 +No valid DFU suffix signature +Warning: File has no DFU suffix +DfuSe interface name: "Internal Flash " +Memory segment at 0x08000000 128 x 2048 = 262144 (rew) +Uploading to address = 0x08000000, size = 16384 +Erasing page size 2048 at address 0x08000000, page starting at 0x08000000 + Download from image offset 00000000 to memory 08000000-080007ff, size 2048 + Setting address pointer to 0x08000000 +Erasing page size 2048 at address 0x08000800, page starting at 0x08000800 + Download from image offset 00000800 to memory 08000800-08000fff, size 2048 + Setting address pointer to 0x08000800 +Erasing page size 2048 at address 0x08001000, page starting at 0x08001000 + Download from image offset 00001000 to memory 08001000-080017ff, size 2048 + Setting address pointer to 0x08001000 +Erasing page size 2048 at address 0x08001800, page starting at 0x08001800 + Download from image offset 00001800 to memory 08001800-08001fff, size 2048 + Setting address pointer to 0x08001800 +Erasing page size 2048 at address 0x08002000, page starting at 0x08002000 + Download from image offset 00002000 to memory 08002000-080027ff, size 2048 + Setting address pointer to 0x08002000 +Erasing page size 2048 at address 0x08002800, page starting at 0x08002800 + Download from image offset 00002800 to memory 08002800-08002fff, size 2048 + Setting address pointer to 0x08002800 +Erasing page size 2048 at address 0x08003000, page starting at 0x08003000 + Download from image offset 00003000 to memory 08003000-080037ff, size 2048 + Setting address pointer to 0x08003000 +Erasing page size 2048 at address 0x08003800, page starting at 0x08003800 + Download from image offset 00003800 to memory 08003800-08003fff, size 2048 + Setting address pointer to 0x08003800 + diff --git a/tools/win/src/dfu-util/device-logs/stm32f107.lsusb b/tools/win/src/dfu-util/device-logs/stm32f107.lsusb new file mode 100644 index 0000000..14b45cd --- /dev/null +++ b/tools/win/src/dfu-util/device-logs/stm32f107.lsusb @@ -0,0 +1,60 @@ + +Bus 001 Device 028: ID 0483:df11 SGS Thomson Microelectronics STM Device in DFU Mode +Device Descriptor: + bLength 18 + bDescriptorType 1 + bcdUSB 1.00 + bDeviceClass 0 (Defined at Interface level) + bDeviceSubClass 0 + bDeviceProtocol 0 + bMaxPacketSize0 64 + idVendor 0x0483 SGS Thomson Microelectronics + idProduct 0xdf11 STM Device in DFU Mode + bcdDevice 20.00 + iManufacturer 1 STMicroelectronics + iProduct 2 STM32 0x418 DFU Bootloader + iSerial 3 STM32 + bNumConfigurations 1 + Configuration Descriptor: + bLength 9 + bDescriptorType 2 + wTotalLength 36 + bNumInterfaces 1 + bConfigurationValue 1 + iConfiguration 0 + bmAttributes 0xc0 + Self Powered + MaxPower 100mA + Interface Descriptor: + bLength 9 + bDescriptorType 4 + bInterfaceNumber 0 + bAlternateSetting 0 + bNumEndpoints 0 + bInterfaceClass 254 Application Specific Interface + bInterfaceSubClass 1 Device Firmware Update + bInterfaceProtocol 2 + iInterface 4 @Internal Flash /0x08000000/128*002Kg + Interface Descriptor: + bLength 9 + bDescriptorType 4 + bInterfaceNumber 0 + bAlternateSetting 1 + bNumEndpoints 0 + bInterfaceClass 254 Application Specific Interface + bInterfaceSubClass 1 Device Firmware Update + bInterfaceProtocol 2 + iInterface 5 @Option Bytes /0x1FFFF800/01*016 g + Device Firmware Upgrade Interface Descriptor: + bLength 9 + bDescriptorType 33 + bmAttributes 11 + Will Detach + Manifestation Intolerant + Upload Supported + Download Supported + wDetachTimeout 255 milliseconds + wTransferSize 2048 bytes + bcdDFUVersion 1.1a +Device Status: 0x0000 + (Bus Powered) diff --git a/tools/win/src/dfu-util/device-logs/stm32f4discovery.bin-download b/tools/win/src/dfu-util/device-logs/stm32f4discovery.bin-download new file mode 100644 index 0000000..96e1722 --- /dev/null +++ b/tools/win/src/dfu-util/device-logs/stm32f4discovery.bin-download @@ -0,0 +1,36 @@ +dfu-util --device 0483:df11 --alt 0 \ + --dfuse-address 0x08000000 \ + -v -v -v \ + --download arm/iotoggle.bin +No valid DFU suffix signature +Warning: File has no DFU suffix +dfu-util 0.5 + +(C) 2005-2008 by Weston Schmidt, Harald Welte and OpenMoko Inc. +(C) 2010-2011 Tormod Volden (DfuSe support) +This program is Free Software and has ABSOLUTELY NO WARRANTY + +dfu-util does currently only support DFU version 1.0 + +Filter on vendor = 0x0483 product = 0xdf11 +Opening DFU capable USB device... ID 0483:df11 +Run-time device DFU version 011a +Found DFU: [0483:df11] devnum=0, cfg=1, intf=0, alt=0, name="@Internal Flash /0x08000000/04*016Kg,01*064Kg,07*128Kg" +Claiming USB DFU Interface... +Setting Alternate Setting #0 ... +Determining device status: state = dfuERROR, status = 10 +dfuERROR, clearing status +Determining device status: state = dfuIDLE, status = 0 +dfuIDLE, continuing +DFU mode device DFU version 011a +Device returned transfer size 2048 +DfuSe interface name: "Internal Flash " +Memory segment at 0x08000000 4 x 16384 = 65536 (rew) +Memory segment at 0x08010000 1 x 65536 = 65536 (rew) +Memory segment at 0x08020000 7 x 131072 = 917504 (rew) +Uploading to address = 0x08000000, size = 2308 +Erasing page size 16384 at address 0x08000000, page starting at 0x08000000 + Download from image offset 00000000 to memory 08000000-080007ff, size 2048 + Setting address pointer to 0x08000000 + Download from image offset 00000800 to memory 08000800-08000903, size 260 + Setting address pointer to 0x08000800 diff --git a/tools/win/src/dfu-util/device-logs/stm32f4discovery.lsusb b/tools/win/src/dfu-util/device-logs/stm32f4discovery.lsusb new file mode 100644 index 0000000..0b870de --- /dev/null +++ b/tools/win/src/dfu-util/device-logs/stm32f4discovery.lsusb @@ -0,0 +1,80 @@ + +Bus 001 Device 010: ID 0483:df11 SGS Thomson Microelectronics STM Device in DFU Mode +Device Descriptor: + bLength 18 + bDescriptorType 1 + bcdUSB 1.00 + bDeviceClass 0 (Defined at Interface level) + bDeviceSubClass 0 + bDeviceProtocol 0 + bMaxPacketSize0 64 + idVendor 0x0483 SGS Thomson Microelectronics + idProduct 0xdf11 STM Device in DFU Mode + bcdDevice 21.00 + iManufacturer 1 STMicroelectronics + iProduct 2 STM32 BOOTLOADER + iSerial 3 315A28A0B956 + bNumConfigurations 1 + Configuration Descriptor: + bLength 9 + bDescriptorType 2 + wTotalLength 54 + bNumInterfaces 1 + bConfigurationValue 1 + iConfiguration 0 + bmAttributes 0xc0 + Self Powered + MaxPower 100mA + Interface Descriptor: + bLength 9 + bDescriptorType 4 + bInterfaceNumber 0 + bAlternateSetting 0 + bNumEndpoints 0 + bInterfaceClass 254 Application Specific Interface + bInterfaceSubClass 1 Device Firmware Update + bInterfaceProtocol 2 + iInterface 4 @Internal Flash /0x08000000/04*016Kg,01*064Kg,07*128Kg + Interface Descriptor: + bLength 9 + bDescriptorType 4 + bInterfaceNumber 0 + bAlternateSetting 1 + bNumEndpoints 0 + bInterfaceClass 254 Application Specific Interface + bInterfaceSubClass 1 Device Firmware Update + bInterfaceProtocol 2 + iInterface 5 @Option Bytes /0x1FFFC000/01*016 g + Interface Descriptor: + bLength 9 + bDescriptorType 4 + bInterfaceNumber 0 + bAlternateSetting 2 + bNumEndpoints 0 + bInterfaceClass 254 Application Specific Interface + bInterfaceSubClass 1 Device Firmware Update + bInterfaceProtocol 2 + iInterface 6 @OTP Memory /0x1FFF7800/01*512 g,01*016 g + Interface Descriptor: + bLength 9 + bDescriptorType 4 + bInterfaceNumber 0 + bAlternateSetting 3 + bNumEndpoints 0 + bInterfaceClass 254 Application Specific Interface + bInterfaceSubClass 1 Device Firmware Update + bInterfaceProtocol 2 + iInterface 7 @Device Feature/0xFFFF0000/01*004 g + Device Firmware Upgrade Interface Descriptor: + bLength 9 + bDescriptorType 33 + bmAttributes 11 + Will Detach + Manifestation Intolerant + Upload Supported + Download Supported + wDetachTimeout 255 milliseconds + wTransferSize 2048 bytes + bcdDFUVersion 1.1a +Device Status: 0x0001 + Self Powered diff --git a/tools/win/src/dfu-util/device-logs/tdk-bluetooth.lsusb b/tools/win/src/dfu-util/device-logs/tdk-bluetooth.lsusb new file mode 100644 index 0000000..c0cface --- /dev/null +++ b/tools/win/src/dfu-util/device-logs/tdk-bluetooth.lsusb @@ -0,0 +1,269 @@ + +Bus 006 Device 014: ID 04bf:0320 TDK Corp. Bluetooth Adapter +Device Descriptor: + bLength 18 + bDescriptorType 1 + bcdUSB 2.00 + bDeviceClass 224 Wireless + bDeviceSubClass 1 Radio Frequency + bDeviceProtocol 1 Bluetooth + bMaxPacketSize0 64 + idVendor 0x04bf TDK Corp. + idProduct 0x0320 Bluetooth Adapter + bcdDevice 26.52 + iManufacturer 1 Ezurio + iProduct 2 Turbo Bluetooth Adapter + iSerial 3 008098D4FFBD + bNumConfigurations 1 + Configuration Descriptor: + bLength 9 + bDescriptorType 2 + wTotalLength 193 + bNumInterfaces 3 + bConfigurationValue 1 + iConfiguration 0 + bmAttributes 0x80 + (Bus Powered) + MaxPower 64mA + Interface Descriptor: + bLength 9 + bDescriptorType 4 + bInterfaceNumber 0 + bAlternateSetting 0 + bNumEndpoints 3 + bInterfaceClass 224 Wireless + bInterfaceSubClass 1 Radio Frequency + bInterfaceProtocol 1 Bluetooth + iInterface 0 + Endpoint Descriptor: + bLength 7 + bDescriptorType 5 + bEndpointAddress 0x81 EP 1 IN + bmAttributes 3 + Transfer Type Interrupt + Synch Type None + Usage Type Data + wMaxPacketSize 0x0010 1x 16 bytes + bInterval 1 + Endpoint Descriptor: + bLength 7 + bDescriptorType 5 + bEndpointAddress 0x02 EP 2 OUT + bmAttributes 2 + Transfer Type Bulk + Synch Type None + Usage Type Data + wMaxPacketSize 0x0040 1x 64 bytes + bInterval 1 + Endpoint Descriptor: + bLength 7 + bDescriptorType 5 + bEndpointAddress 0x82 EP 2 IN + bmAttributes 2 + Transfer Type Bulk + Synch Type None + Usage Type Data + wMaxPacketSize 0x0040 1x 64 bytes + bInterval 1 + Interface Descriptor: + bLength 9 + bDescriptorType 4 + bInterfaceNumber 1 + bAlternateSetting 0 + bNumEndpoints 2 + bInterfaceClass 224 Wireless + bInterfaceSubClass 1 Radio Frequency + bInterfaceProtocol 1 Bluetooth + iInterface 0 + Endpoint Descriptor: + bLength 7 + bDescriptorType 5 + bEndpointAddress 0x03 EP 3 OUT + bmAttributes 1 + Transfer Type Isochronous + Synch Type None + Usage Type Data + wMaxPacketSize 0x0000 1x 0 bytes + bInterval 1 + Endpoint Descriptor: + bLength 7 + bDescriptorType 5 + bEndpointAddress 0x83 EP 3 IN + bmAttributes 1 + Transfer Type Isochronous + Synch Type None + Usage Type Data + wMaxPacketSize 0x0000 1x 0 bytes + bInterval 1 + Interface Descriptor: + bLength 9 + bDescriptorType 4 + bInterfaceNumber 1 + bAlternateSetting 1 + bNumEndpoints 2 + bInterfaceClass 224 Wireless + bInterfaceSubClass 1 Radio Frequency + bInterfaceProtocol 1 Bluetooth + iInterface 0 + Endpoint Descriptor: + bLength 7 + bDescriptorType 5 + bEndpointAddress 0x03 EP 3 OUT + bmAttributes 1 + Transfer Type Isochronous + Synch Type None + Usage Type Data + wMaxPacketSize 0x0009 1x 9 bytes + bInterval 1 + Endpoint Descriptor: + bLength 7 + bDescriptorType 5 + bEndpointAddress 0x83 EP 3 IN + bmAttributes 1 + Transfer Type Isochronous + Synch Type None + Usage Type Data + wMaxPacketSize 0x0009 1x 9 bytes + bInterval 1 + Interface Descriptor: + bLength 9 + bDescriptorType 4 + bInterfaceNumber 1 + bAlternateSetting 2 + bNumEndpoints 2 + bInterfaceClass 224 Wireless + bInterfaceSubClass 1 Radio Frequency + bInterfaceProtocol 1 Bluetooth + iInterface 0 + Endpoint Descriptor: + bLength 7 + bDescriptorType 5 + bEndpointAddress 0x03 EP 3 OUT + bmAttributes 1 + Transfer Type Isochronous + Synch Type None + Usage Type Data + wMaxPacketSize 0x0011 1x 17 bytes + bInterval 1 + Endpoint Descriptor: + bLength 7 + bDescriptorType 5 + bEndpointAddress 0x83 EP 3 IN + bmAttributes 1 + Transfer Type Isochronous + Synch Type None + Usage Type Data + wMaxPacketSize 0x0011 1x 17 bytes + bInterval 1 + Interface Descriptor: + bLength 9 + bDescriptorType 4 + bInterfaceNumber 1 + bAlternateSetting 3 + bNumEndpoints 2 + bInterfaceClass 224 Wireless + bInterfaceSubClass 1 Radio Frequency + bInterfaceProtocol 1 Bluetooth + iInterface 0 + Endpoint Descriptor: + bLength 7 + bDescriptorType 5 + bEndpointAddress 0x03 EP 3 OUT + bmAttributes 1 + Transfer Type Isochronous + Synch Type None + Usage Type Data + wMaxPacketSize 0x0019 1x 25 bytes + bInterval 1 + Endpoint Descriptor: + bLength 7 + bDescriptorType 5 + bEndpointAddress 0x83 EP 3 IN + bmAttributes 1 + Transfer Type Isochronous + Synch Type None + Usage Type Data + wMaxPacketSize 0x0019 1x 25 bytes + bInterval 1 + Interface Descriptor: + bLength 9 + bDescriptorType 4 + bInterfaceNumber 1 + bAlternateSetting 4 + bNumEndpoints 2 + bInterfaceClass 224 Wireless + bInterfaceSubClass 1 Radio Frequency + bInterfaceProtocol 1 Bluetooth + iInterface 0 + Endpoint Descriptor: + bLength 7 + bDescriptorType 5 + bEndpointAddress 0x03 EP 3 OUT + bmAttributes 1 + Transfer Type Isochronous + Synch Type None + Usage Type Data + wMaxPacketSize 0x0021 1x 33 bytes + bInterval 1 + Endpoint Descriptor: + bLength 7 + bDescriptorType 5 + bEndpointAddress 0x83 EP 3 IN + bmAttributes 1 + Transfer Type Isochronous + Synch Type None + Usage Type Data + wMaxPacketSize 0x0021 1x 33 bytes + bInterval 1 + Interface Descriptor: + bLength 9 + bDescriptorType 4 + bInterfaceNumber 1 + bAlternateSetting 5 + bNumEndpoints 2 + bInterfaceClass 224 Wireless + bInterfaceSubClass 1 Radio Frequency + bInterfaceProtocol 1 Bluetooth + iInterface 0 + Endpoint Descriptor: + bLength 7 + bDescriptorType 5 + bEndpointAddress 0x03 EP 3 OUT + bmAttributes 1 + Transfer Type Isochronous + Synch Type None + Usage Type Data + wMaxPacketSize 0x0031 1x 49 bytes + bInterval 1 + Endpoint Descriptor: + bLength 7 + bDescriptorType 5 + bEndpointAddress 0x83 EP 3 IN + bmAttributes 1 + Transfer Type Isochronous + Synch Type None + Usage Type Data + wMaxPacketSize 0x0031 1x 49 bytes + bInterval 1 + Interface Descriptor: + bLength 9 + bDescriptorType 4 + bInterfaceNumber 2 + bAlternateSetting 0 + bNumEndpoints 0 + bInterfaceClass 254 Application Specific Interface + bInterfaceSubClass 1 Device Firmware Update + bInterfaceProtocol 0 + iInterface 0 + Device Firmware Upgrade Interface Descriptor: + bLength 7 + bDescriptorType 33 + bmAttributes 7 + Will Not Detach + Manifestation Tolerant + Upload Supported + Download Supported + wDetachTimeout 5000 milliseconds + wTransferSize 1023 bytes +Device Status: 0x0000 + (Bus Powered) diff --git a/tools/win/src/dfu-util/dfuse-pack.py b/tools/win/src/dfu-util/dfuse-pack.py new file mode 100644 index 0000000..875cc5c --- /dev/null +++ b/tools/win/src/dfu-util/dfuse-pack.py @@ -0,0 +1,121 @@ +#!/usr/bin/python + +# Written by Antonio Galea - 2010/11/18 +# Distributed under Gnu LGPL 3.0 +# see http://www.gnu.org/licenses/lgpl-3.0.txt + +import sys,struct,zlib,os +from optparse import OptionParser + +DEFAULT_DEVICE="0x0483:0xdf11" + +def named(tuple,names): + return dict(zip(names.split(),tuple)) +def consume(fmt,data,names): + n = struct.calcsize(fmt) + return named(struct.unpack(fmt,data[:n]),names),data[n:] +def cstring(string): + return string.split('\0',1)[0] +def compute_crc(data): + return 0xFFFFFFFF & -zlib.crc32(data) -1 + +def parse(file,dump_images=False): + print 'File: "%s"' % file + data = open(file,'rb').read() + crc = compute_crc(data[:-4]) + prefix, data = consume('<5sBIB',data,'signature version size targets') + print '%(signature)s v%(version)d, image size: %(size)d, targets: %(targets)d' % prefix + for t in range(prefix['targets']): + tprefix, data = consume('<6sBI255s2I',data,'signature altsetting named name size elements') + tprefix['num'] = t + if tprefix['named']: + tprefix['name'] = cstring(tprefix['name']) + else: + tprefix['name'] = '' + print '%(signature)s %(num)d, alt setting: %(altsetting)s, name: "%(name)s", size: %(size)d, elements: %(elements)d' % tprefix + tsize = tprefix['size'] + target, data = data[:tsize], data[tsize:] + for e in range(tprefix['elements']): + eprefix, target = consume('<2I',target,'address size') + eprefix['num'] = e + print ' %(num)d, address: 0x%(address)08x, size: %(size)d' % eprefix + esize = eprefix['size'] + image, target = target[:esize], target[esize:] + if dump_images: + out = '%s.target%d.image%d.bin' % (file,t,e) + open(out,'wb').write(image) + print ' DUMPED IMAGE TO "%s"' % out + if len(target): + print "target %d: PARSE ERROR" % t + suffix = named(struct.unpack('<4H3sBI',data[:16]),'device product vendor dfu ufd len crc') + print 'usb: %(vendor)04x:%(product)04x, device: 0x%(device)04x, dfu: 0x%(dfu)04x, %(ufd)s, %(len)d, 0x%(crc)08x' % suffix + if crc != suffix['crc']: + print "CRC ERROR: computed crc32 is 0x%08x" % crc + data = data[16:] + if data: + print "PARSE ERROR" + +def build(file,targets,device=DEFAULT_DEVICE): + data = '' + for t,target in enumerate(targets): + tdata = '' + for image in target: + tdata += struct.pack('<2I',image['address'],len(image['data']))+image['data'] + tdata = struct.pack('<6sBI255s2I','Target',0,1,'ST...',len(tdata),len(target)) + tdata + data += tdata + data = struct.pack('<5sBIB','DfuSe',1,len(data)+11,len(targets)) + data + v,d=map(lambda x: int(x,0) & 0xFFFF, device.split(':',1)) + data += struct.pack('<4H3sB',0,d,v,0x011a,'UFD',16) + crc = compute_crc(data) + data += struct.pack(' and +Harald Welte . Over time, nearly complete +support of DFU 1.0, DFU 1.1 and DfuSe ("1.1a") has been added. +.SH LICENCE +.B dfu-util +is covered by the GNU General Public License (GPL), version 2 or later. +.SH COPYRIGHT +This manual page was originally written by Uwe Hermann , +and is now part of the dfu-util project. diff --git a/tools/win/src/dfu-util/msvc/README_msvc.txt b/tools/win/src/dfu-util/msvc/README_msvc.txt new file mode 100644 index 0000000..6e68ec6 --- /dev/null +++ b/tools/win/src/dfu-util/msvc/README_msvc.txt @@ -0,0 +1,10 @@ +# (C) Roger Meier +# (C) Pascal Schweizer +# msvc folder is GPL-2.0+, LGPL-2.1+, BSD-3-Clause or MIT license(SPDX) + +Building dfu-util native on Windows with Visual Studio + +3rd party dependencies: +- libusbx ( git clone https://github.com/libusbx/libusbx.git ) + - getopt (part of libusbx: libusbx/examples/getopt) + diff --git a/tools/win/src/dfu-util/msvc/dfu-suffix_2010.vcxproj b/tools/win/src/dfu-util/msvc/dfu-suffix_2010.vcxproj new file mode 100644 index 0000000..0c316c2 --- /dev/null +++ b/tools/win/src/dfu-util/msvc/dfu-suffix_2010.vcxproj @@ -0,0 +1,100 @@ + + + + + Debug + Win32 + + + Release + Win32 + + + + {8F7600A2-3B37-4956-B39B-A1D43EF29EDA} + dfusuffix + dfu-suffix + + + + Application + true + MultiByte + + + Application + false + true + MultiByte + + + + + + + + + + + + + $(SolutionDir)..\..\libusbx\examples\getopt;$(SolutionDir)..\..\libusbx\libusb;$(IncludePath) + $(SolutionDir)..\$(Platform)\$(ProjectName)\$(Configuration)\ + $(SolutionDir)..\$(Platform)\$(ProjectName)\$(Configuration)\ + $(SolutionDir)..\$(Platform)\$(ProjectName)\$(Configuration)\dll;$(LibraryPath) + $(VCInstallDir)atlmfc\lib;$(VCInstallDir)lib + $(ExecutablePath) + + + $(ExecutablePath) + $(SolutionDir)..\..\libusbx\examples\getopt;$(SolutionDir)..\..\libusbx\libusb;$(IncludePath) + $(SolutionDir)..\$(Platform)\$(ProjectName)\$(Configuration)\dll;$(LibraryPath) + $(SolutionDir)..\$(Platform)\$(ProjectName)\$(Configuration)\ + $(SolutionDir)..\$(Platform)\$(ProjectName)\$(Configuration)\ + + + + Level3 + Disabled + HAVE_WINDOWS_H;_CRT_SECURE_NO_WARNINGS;%(PreprocessorDefinitions) + MultiThreadedDebug + + + true + + + + + Level3 + MaxSpeed + true + true + HAVE_WINDOWS_H;_CRT_SECURE_NO_WARNINGS;%(PreprocessorDefinitions) + MultiThreaded + + + true + true + true + + + + + + + + + + + + + {a2169bc8-cf99-40bf-83f3-b0e38f7067bd} + + + {349ee8f9-7d25-4909-aaf5-ff3fade72187} + + + + + + \ No newline at end of file diff --git a/tools/win/src/dfu-util/msvc/dfu-util_2010.sln b/tools/win/src/dfu-util/msvc/dfu-util_2010.sln new file mode 100644 index 0000000..ef79723 --- /dev/null +++ b/tools/win/src/dfu-util/msvc/dfu-util_2010.sln @@ -0,0 +1,54 @@ + +Microsoft Visual Studio Solution File, Format Version 11.00 +# Visual Studio 2010 +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "dfu-util", "dfu-util_2010.vcxproj", "{0E071A60-7EF2-4427-BAA8-9143CACB5BCB}" +EndProject +Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{C4F8746D-B27E-4806-95E5-2052174E923B}" +EndProject +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "dfu-suffix", "dfu-suffix_2010.vcxproj", "{8F7600A2-3B37-4956-B39B-A1D43EF29EDA}" +EndProject +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "getopt_2010", "..\..\libusbx\msvc\getopt_2010.vcxproj", "{AE83E1B4-CE06-47EE-B7A3-C3A1D7C2D71E}" +EndProject +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "libusb-1.0 (static)", "..\..\libusbx\msvc\libusb_static_2010.vcxproj", "{349EE8F9-7D25-4909-AAF5-FF3FADE72187}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Win32 = Debug|Win32 + Debug|x64 = Debug|x64 + Release|Win32 = Release|Win32 + Release|x64 = Release|x64 + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {0E071A60-7EF2-4427-BAA8-9143CACB5BCB}.Debug|Win32.ActiveCfg = Debug|Win32 + {0E071A60-7EF2-4427-BAA8-9143CACB5BCB}.Debug|Win32.Build.0 = Debug|Win32 + {0E071A60-7EF2-4427-BAA8-9143CACB5BCB}.Debug|x64.ActiveCfg = Debug|Win32 + {0E071A60-7EF2-4427-BAA8-9143CACB5BCB}.Release|Win32.ActiveCfg = Release|Win32 + {0E071A60-7EF2-4427-BAA8-9143CACB5BCB}.Release|Win32.Build.0 = Release|Win32 + {0E071A60-7EF2-4427-BAA8-9143CACB5BCB}.Release|x64.ActiveCfg = Release|Win32 + {8F7600A2-3B37-4956-B39B-A1D43EF29EDA}.Debug|Win32.ActiveCfg = Debug|Win32 + {8F7600A2-3B37-4956-B39B-A1D43EF29EDA}.Debug|Win32.Build.0 = Debug|Win32 + {8F7600A2-3B37-4956-B39B-A1D43EF29EDA}.Debug|x64.ActiveCfg = Debug|Win32 + {8F7600A2-3B37-4956-B39B-A1D43EF29EDA}.Release|Win32.ActiveCfg = Release|Win32 + {8F7600A2-3B37-4956-B39B-A1D43EF29EDA}.Release|Win32.Build.0 = Release|Win32 + {8F7600A2-3B37-4956-B39B-A1D43EF29EDA}.Release|x64.ActiveCfg = Release|Win32 + {AE83E1B4-CE06-47EE-B7A3-C3A1D7C2D71E}.Debug|Win32.ActiveCfg = Debug|Win32 + {AE83E1B4-CE06-47EE-B7A3-C3A1D7C2D71E}.Debug|Win32.Build.0 = Debug|Win32 + {AE83E1B4-CE06-47EE-B7A3-C3A1D7C2D71E}.Debug|x64.ActiveCfg = Debug|x64 + {AE83E1B4-CE06-47EE-B7A3-C3A1D7C2D71E}.Debug|x64.Build.0 = Debug|x64 + {AE83E1B4-CE06-47EE-B7A3-C3A1D7C2D71E}.Release|Win32.ActiveCfg = Release|Win32 + {AE83E1B4-CE06-47EE-B7A3-C3A1D7C2D71E}.Release|Win32.Build.0 = Release|Win32 + {AE83E1B4-CE06-47EE-B7A3-C3A1D7C2D71E}.Release|x64.ActiveCfg = Release|x64 + {AE83E1B4-CE06-47EE-B7A3-C3A1D7C2D71E}.Release|x64.Build.0 = Release|x64 + {349EE8F9-7D25-4909-AAF5-FF3FADE72187}.Debug|Win32.ActiveCfg = Debug|Win32 + {349EE8F9-7D25-4909-AAF5-FF3FADE72187}.Debug|Win32.Build.0 = Debug|Win32 + {349EE8F9-7D25-4909-AAF5-FF3FADE72187}.Debug|x64.ActiveCfg = Debug|x64 + {349EE8F9-7D25-4909-AAF5-FF3FADE72187}.Debug|x64.Build.0 = Debug|x64 + {349EE8F9-7D25-4909-AAF5-FF3FADE72187}.Release|Win32.ActiveCfg = Release|Win32 + {349EE8F9-7D25-4909-AAF5-FF3FADE72187}.Release|Win32.Build.0 = Release|Win32 + {349EE8F9-7D25-4909-AAF5-FF3FADE72187}.Release|x64.ActiveCfg = Release|x64 + {349EE8F9-7D25-4909-AAF5-FF3FADE72187}.Release|x64.Build.0 = Release|x64 + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection +EndGlobal diff --git a/tools/win/src/dfu-util/msvc/dfu-util_2010.vcxproj b/tools/win/src/dfu-util/msvc/dfu-util_2010.vcxproj new file mode 100644 index 0000000..17a8bee --- /dev/null +++ b/tools/win/src/dfu-util/msvc/dfu-util_2010.vcxproj @@ -0,0 +1,120 @@ + + + + + Debug + Win32 + + + Release + Win32 + + + + {0E071A60-7EF2-4427-BAA8-9143CACB5BCB} + dfuutil + dfu-util + + + + Application + true + MultiByte + + + Application + false + true + MultiByte + + + + + + + + + + + + + $(SolutionDir)..\..\libusbx\examples\getopt;$(SolutionDir)..\..\libusbx\libusb;$(IncludePath) + $(SolutionDir)..\$(Platform)\$(ProjectName)\$(Configuration)\ + $(SolutionDir)..\$(Platform)\$(ProjectName)\$(Configuration)\ + $(SolutionDir)..\$(Platform)\getopt\$(Configuration);$(LibraryPath) + $(VCInstallDir)atlmfc\lib;$(VCInstallDir)lib + $(ExecutablePath) + + + $(ExecutablePath) + $(SolutionDir)..\..\libusbx\examples\getopt;$(SolutionDir)..\..\libusbx\libusb;$(IncludePath) + $(SolutionDir)..\$(Platform)\getopt\$(Configuration);$(LibraryPath) + $(SolutionDir)..\$(Platform)\$(ProjectName)\$(Configuration)\ + $(SolutionDir)..\$(Platform)\$(ProjectName)\$(Configuration)\ + + + + Level3 + Disabled + HAVE_WINDOWS_H;_CRT_SECURE_NO_WARNINGS;%(PreprocessorDefinitions) + MultiThreadedDebug + + + true + + + + + + + + + Level3 + MaxSpeed + true + true + HAVE_WINDOWS_H;_CRT_SECURE_NO_WARNINGS;%(PreprocessorDefinitions) + MultiThreaded + + + true + true + true + + + copy $(SolutionDir)..\$(Platform)\$(Configuration)\dll\libusb-1.0.dll $(SolutionDir)..\$(Platform)\$(ProjectName)\$(Configuration)\ + + + + + + + + + + + + + + + + + + + + + + + + + + {a2169bc8-cf99-40bf-83f3-b0e38f7067bd} + + + {349ee8f9-7d25-4909-aaf5-ff3fade72187} + + + + + + \ No newline at end of file diff --git a/tools/win/src/dfu-util/src/Makefile.am b/tools/win/src/dfu-util/src/Makefile.am new file mode 100644 index 0000000..70179c4 --- /dev/null +++ b/tools/win/src/dfu-util/src/Makefile.am @@ -0,0 +1,28 @@ +AM_CFLAGS = -Wall -Wextra + +bin_PROGRAMS = dfu-util dfu-suffix dfu-prefix +dfu_util_SOURCES = main.c \ + portable.h \ + dfu_load.c \ + dfu_load.h \ + dfu_util.c \ + dfu_util.h \ + dfuse.c \ + dfuse.h \ + dfuse_mem.c \ + dfuse_mem.h \ + dfu.c \ + dfu.h \ + usb_dfu.h \ + dfu_file.c \ + dfu_file.h \ + quirks.c \ + quirks.h + +dfu_suffix_SOURCES = suffix.c \ + dfu_file.h \ + dfu_file.c + +dfu_prefix_SOURCES = prefix.c \ + dfu_file.h \ + dfu_file.c diff --git a/tools/win/src/dfu-util/src/dfu.c b/tools/win/src/dfu-util/src/dfu.c new file mode 100644 index 0000000..14d7673 --- /dev/null +++ b/tools/win/src/dfu-util/src/dfu.c @@ -0,0 +1,357 @@ +/* + * Low-level DFU communication routines, originally taken from + * $Id: dfu.c,v 1.3 2006/06/20 06:28:04 schmidtw Exp $ + * (part of dfu-programmer). + * + * Copyright 2005-2006 Weston Schmidt + * Copyright 2011-2014 Tormod Volden + * + * 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 +#include + +#include + +#include "portable.h" +#include "dfu.h" +#include "quirks.h" + +static int dfu_timeout = 5000; /* 5 seconds - default */ + +/* + * 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( libusb_device_handle *device, + const unsigned short interface, + const unsigned short timeout ) +{ + return libusb_control_transfer( device, + /* bmRequestType */ LIBUSB_ENDPOINT_OUT | LIBUSB_REQUEST_TYPE_CLASS | LIBUSB_RECIPIENT_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( libusb_device_handle *device, + const unsigned short interface, + const unsigned short length, + const unsigned short transaction, + unsigned char* data ) +{ + int status; + + status = libusb_control_transfer( device, + /* bmRequestType */ LIBUSB_ENDPOINT_OUT | LIBUSB_REQUEST_TYPE_CLASS | LIBUSB_RECIPIENT_INTERFACE, + /* bRequest */ DFU_DNLOAD, + /* wValue */ transaction, + /* wIndex */ interface, + /* Data */ data, + /* wLength */ length, + dfu_timeout ); + 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( libusb_device_handle *device, + const unsigned short interface, + const unsigned short length, + const unsigned short transaction, + unsigned char* data ) +{ + int status; + + status = libusb_control_transfer( device, + /* bmRequestType */ LIBUSB_ENDPOINT_IN | LIBUSB_REQUEST_TYPE_CLASS | LIBUSB_RECIPIENT_INTERFACE, + /* bRequest */ DFU_UPLOAD, + /* wValue */ transaction, + /* wIndex */ interface, + /* Data */ data, + /* wLength */ length, + dfu_timeout ); + 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 dfu_if *dif, struct dfu_status *status ) +{ + unsigned char buffer[6]; + int result; + + /* Initialize the status data structure */ + status->bStatus = DFU_STATUS_ERROR_UNKNOWN; + status->bwPollTimeout = 0; + status->bState = STATE_DFU_ERROR; + status->iString = 0; + + result = libusb_control_transfer( dif->dev_handle, + /* bmRequestType */ LIBUSB_ENDPOINT_IN | LIBUSB_REQUEST_TYPE_CLASS | LIBUSB_RECIPIENT_INTERFACE, + /* bRequest */ DFU_GETSTATUS, + /* wValue */ 0, + /* wIndex */ dif->interface, + /* Data */ buffer, + /* wLength */ 6, + dfu_timeout ); + + if( 6 == result ) { + status->bStatus = buffer[0]; + if (dif->quirks & QUIRK_POLLTIMEOUT) + status->bwPollTimeout = DEFAULT_POLLTIMEOUT; + else + 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( libusb_device_handle *device, + const unsigned short interface ) +{ + return libusb_control_transfer( device, + /* bmRequestType */ LIBUSB_ENDPOINT_OUT| LIBUSB_REQUEST_TYPE_CLASS | LIBUSB_RECIPIENT_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( libusb_device_handle *device, + const unsigned short interface ) +{ + int result; + unsigned char buffer[1]; + + result = libusb_control_transfer( device, + /* bmRequestType */ LIBUSB_ENDPOINT_IN | LIBUSB_REQUEST_TYPE_CLASS | LIBUSB_RECIPIENT_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 -1; + + /* 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( libusb_device_handle *device, + const unsigned short interface ) +{ + return libusb_control_transfer( device, + /* bmRequestType */ LIBUSB_ENDPOINT_OUT | LIBUSB_REQUEST_TYPE_CLASS | LIBUSB_RECIPIENT_INTERFACE, + /* bRequest */ DFU_ABORT, + /* wValue */ 0, + /* wIndex */ interface, + /* Data */ NULL, + /* wLength */ 0, + dfu_timeout ); +} + + +const char* dfu_state_to_string( int state ) +{ + const char *message; + + 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; + default: + message = NULL; + 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 memory 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]; +} + +int dfu_abort_to_idle(struct dfu_if *dif) +{ + int ret; + struct dfu_status dst; + + ret = dfu_abort(dif->dev_handle, dif->interface); + if (ret < 0) { + errx(EX_IOERR, "Error sending dfu abort request"); + exit(1); + } + ret = dfu_get_status(dif, &dst); + if (ret < 0) { + errx(EX_IOERR, "Error during abort get_status"); + exit(1); + } + if (dst.bState != DFU_STATE_dfuIDLE) { + errx(EX_IOERR, "Failed to enter idle state on abort"); + exit(1); + } + milli_sleep(dst.bwPollTimeout); + return ret; +} diff --git a/tools/win/src/dfu-util/src/dfu.h b/tools/win/src/dfu-util/src/dfu.h new file mode 100644 index 0000000..8e3caeb --- /dev/null +++ b/tools/win/src/dfu-util/src/dfu.h @@ -0,0 +1,133 @@ +/* + * 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 +#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 + +/* DFU commands */ +#define DFU_DETACH 0 +#define DFU_DNLOAD 1 +#define DFU_UPLOAD 2 +#define DFU_GETSTATUS 3 +#define DFU_CLRSTATUS 4 +#define DFU_GETSTATE 5 +#define DFU_ABORT 6 + +/* DFU interface */ +#define DFU_IFF_DFU 0x0001 /* DFU Mode, (not Runtime) */ + +/* 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; +}; + +struct dfu_if { + struct usb_dfu_func_descriptor func_dfu; + uint16_t quirks; + uint16_t busnum; + uint16_t devnum; + uint16_t vendor; + uint16_t product; + uint16_t bcdDevice; + uint8_t configuration; + uint8_t interface; + uint8_t altsetting; + uint8_t flags; + uint8_t bMaxPacketSize0; + char *alt_name; + char *serial_name; + libusb_device *dev; + libusb_device_handle *dev_handle; + struct dfu_if *next; +}; + +int dfu_detach( libusb_device_handle *device, + const unsigned short interface, + const unsigned short timeout ); +int dfu_download( libusb_device_handle *device, + const unsigned short interface, + const unsigned short length, + const unsigned short transaction, + unsigned char* data ); +int dfu_upload( libusb_device_handle *device, + const unsigned short interface, + const unsigned short length, + const unsigned short transaction, + unsigned char* data ); +int dfu_get_status( struct dfu_if *dif, + struct dfu_status *status ); +int dfu_clear_status( libusb_device_handle *device, + const unsigned short interface ); +int dfu_get_state( libusb_device_handle *device, + const unsigned short interface ); +int dfu_abort( libusb_device_handle *device, + const unsigned short interface ); +int dfu_abort_to_idle( struct dfu_if *dif); + +const char *dfu_state_to_string( int state ); + +const char *dfu_status_to_string( int status ); + +#endif /* DFU_H */ diff --git a/tools/win/src/dfu-util/src/dfu_file.c b/tools/win/src/dfu-util/src/dfu_file.c new file mode 100644 index 0000000..7c897d4 --- /dev/null +++ b/tools/win/src/dfu-util/src/dfu_file.c @@ -0,0 +1,444 @@ +/* + * Load or store DFU files including suffix and prefix + * + * Copyright 2014 Tormod Volden + * Copyright 2012 Stefan Schmidt + * + * 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 +#include +#include +#include +#include +#include +#include + +#include "portable.h" +#include "dfu_file.h" + +#define DFU_SUFFIX_LENGTH 16 +#define LMDFU_PREFIX_LENGTH 8 +#define LPCDFU_PREFIX_LENGTH 16 +#define PROGRESS_BAR_WIDTH 25 +#define STDIN_CHUNK_SIZE 65536 + +static const unsigned long crc32_table[] = { + 0x00000000, 0x77073096, 0xee0e612c, 0x990951ba, 0x076dc419, 0x706af48f, + 0xe963a535, 0x9e6495a3, 0x0edb8832, 0x79dcb8a4, 0xe0d5e91e, 0x97d2d988, + 0x09b64c2b, 0x7eb17cbd, 0xe7b82d07, 0x90bf1d91, 0x1db71064, 0x6ab020f2, + 0xf3b97148, 0x84be41de, 0x1adad47d, 0x6ddde4eb, 0xf4d4b551, 0x83d385c7, + 0x136c9856, 0x646ba8c0, 0xfd62f97a, 0x8a65c9ec, 0x14015c4f, 0x63066cd9, + 0xfa0f3d63, 0x8d080df5, 0x3b6e20c8, 0x4c69105e, 0xd56041e4, 0xa2677172, + 0x3c03e4d1, 0x4b04d447, 0xd20d85fd, 0xa50ab56b, 0x35b5a8fa, 0x42b2986c, + 0xdbbbc9d6, 0xacbcf940, 0x32d86ce3, 0x45df5c75, 0xdcd60dcf, 0xabd13d59, + 0x26d930ac, 0x51de003a, 0xc8d75180, 0xbfd06116, 0x21b4f4b5, 0x56b3c423, + 0xcfba9599, 0xb8bda50f, 0x2802b89e, 0x5f058808, 0xc60cd9b2, 0xb10be924, + 0x2f6f7c87, 0x58684c11, 0xc1611dab, 0xb6662d3d, 0x76dc4190, 0x01db7106, + 0x98d220bc, 0xefd5102a, 0x71b18589, 0x06b6b51f, 0x9fbfe4a5, 0xe8b8d433, + 0x7807c9a2, 0x0f00f934, 0x9609a88e, 0xe10e9818, 0x7f6a0dbb, 0x086d3d2d, + 0x91646c97, 0xe6635c01, 0x6b6b51f4, 0x1c6c6162, 0x856530d8, 0xf262004e, + 0x6c0695ed, 0x1b01a57b, 0x8208f4c1, 0xf50fc457, 0x65b0d9c6, 0x12b7e950, + 0x8bbeb8ea, 0xfcb9887c, 0x62dd1ddf, 0x15da2d49, 0x8cd37cf3, 0xfbd44c65, + 0x4db26158, 0x3ab551ce, 0xa3bc0074, 0xd4bb30e2, 0x4adfa541, 0x3dd895d7, + 0xa4d1c46d, 0xd3d6f4fb, 0x4369e96a, 0x346ed9fc, 0xad678846, 0xda60b8d0, + 0x44042d73, 0x33031de5, 0xaa0a4c5f, 0xdd0d7cc9, 0x5005713c, 0x270241aa, + 0xbe0b1010, 0xc90c2086, 0x5768b525, 0x206f85b3, 0xb966d409, 0xce61e49f, + 0x5edef90e, 0x29d9c998, 0xb0d09822, 0xc7d7a8b4, 0x59b33d17, 0x2eb40d81, + 0xb7bd5c3b, 0xc0ba6cad, 0xedb88320, 0x9abfb3b6, 0x03b6e20c, 0x74b1d29a, + 0xead54739, 0x9dd277af, 0x04db2615, 0x73dc1683, 0xe3630b12, 0x94643b84, + 0x0d6d6a3e, 0x7a6a5aa8, 0xe40ecf0b, 0x9309ff9d, 0x0a00ae27, 0x7d079eb1, + 0xf00f9344, 0x8708a3d2, 0x1e01f268, 0x6906c2fe, 0xf762575d, 0x806567cb, + 0x196c3671, 0x6e6b06e7, 0xfed41b76, 0x89d32be0, 0x10da7a5a, 0x67dd4acc, + 0xf9b9df6f, 0x8ebeeff9, 0x17b7be43, 0x60b08ed5, 0xd6d6a3e8, 0xa1d1937e, + 0x38d8c2c4, 0x4fdff252, 0xd1bb67f1, 0xa6bc5767, 0x3fb506dd, 0x48b2364b, + 0xd80d2bda, 0xaf0a1b4c, 0x36034af6, 0x41047a60, 0xdf60efc3, 0xa867df55, + 0x316e8eef, 0x4669be79, 0xcb61b38c, 0xbc66831a, 0x256fd2a0, 0x5268e236, + 0xcc0c7795, 0xbb0b4703, 0x220216b9, 0x5505262f, 0xc5ba3bbe, 0xb2bd0b28, + 0x2bb45a92, 0x5cb36a04, 0xc2d7ffa7, 0xb5d0cf31, 0x2cd99e8b, 0x5bdeae1d, + 0x9b64c2b0, 0xec63f226, 0x756aa39c, 0x026d930a, 0x9c0906a9, 0xeb0e363f, + 0x72076785, 0x05005713, 0x95bf4a82, 0xe2b87a14, 0x7bb12bae, 0x0cb61b38, + 0x92d28e9b, 0xe5d5be0d, 0x7cdcefb7, 0x0bdbdf21, 0x86d3d2d4, 0xf1d4e242, + 0x68ddb3f8, 0x1fda836e, 0x81be16cd, 0xf6b9265b, 0x6fb077e1, 0x18b74777, + 0x88085ae6, 0xff0f6a70, 0x66063bca, 0x11010b5c, 0x8f659eff, 0xf862ae69, + 0x616bffd3, 0x166ccf45, 0xa00ae278, 0xd70dd2ee, 0x4e048354, 0x3903b3c2, + 0xa7672661, 0xd06016f7, 0x4969474d, 0x3e6e77db, 0xaed16a4a, 0xd9d65adc, + 0x40df0b66, 0x37d83bf0, 0xa9bcae53, 0xdebb9ec5, 0x47b2cf7f, 0x30b5ffe9, + 0xbdbdf21c, 0xcabac28a, 0x53b39330, 0x24b4a3a6, 0xbad03605, 0xcdd70693, + 0x54de5729, 0x23d967bf, 0xb3667a2e, 0xc4614ab8, 0x5d681b02, 0x2a6f2b94, + 0xb40bbe37, 0xc30c8ea1, 0x5a05df1b, 0x2d02ef8d}; + +static uint32_t crc32_byte(uint32_t accum, uint8_t delta) +{ + return crc32_table[(accum ^ delta) & 0xff] ^ (accum >> 8); +} + +static int probe_prefix(struct dfu_file *file) +{ + uint8_t *prefix = file->firmware; + + if (file->size.total < LMDFU_PREFIX_LENGTH) + return 1; + if ((prefix[0] == 0x01) && (prefix[1] == 0x00)) { + file->prefix_type = LMDFU_PREFIX; + file->size.prefix = LMDFU_PREFIX_LENGTH; + file->lmdfu_address = 1024 * ((prefix[3] << 8) | prefix[2]); + } + else if (((prefix[0] & 0x3f) == 0x1a) && ((prefix[1] & 0x3f)== 0x3f)) { + file->prefix_type = LPCDFU_UNENCRYPTED_PREFIX; + file->size.prefix = LPCDFU_PREFIX_LENGTH; + } + + if (file->size.prefix + file->size.suffix > file->size.total) + return 1; + return 0; +} + +void dfu_progress_bar(const char *desc, unsigned long long curr, + unsigned long long max) +{ + static char buf[PROGRESS_BAR_WIDTH + 1]; + static unsigned long long last_progress = -1; + static time_t last_time; + time_t curr_time = time(NULL); + unsigned long long progress; + unsigned long long x; + + /* check for not known maximum */ + if (max < curr) + max = curr + 1; + /* make none out of none give zero */ + if (max == 0 && curr == 0) + max = 1; + + /* compute completion */ + progress = (PROGRESS_BAR_WIDTH * curr) / max; + if (progress > PROGRESS_BAR_WIDTH) + progress = PROGRESS_BAR_WIDTH; + if (progress == last_progress && + curr_time == last_time) + return; + last_progress = progress; + last_time = curr_time; + + for (x = 0; x != PROGRESS_BAR_WIDTH; x++) { + if (x < progress) + buf[x] = '='; + else + buf[x] = ' '; + } + buf[x] = 0; + + printf("\r%s\t[%s] %3lld%% %12lld bytes", desc, buf, + (100ULL * curr) / max, curr); + + if (progress == PROGRESS_BAR_WIDTH) + printf("\n%s done.\n", desc); +} + +void *dfu_malloc(size_t size) +{ + void *ptr = malloc(size); + if (ptr == NULL) + errx(EX_SOFTWARE, "Cannot allocate memory of size %d bytes", (int)size); + return (ptr); +} + +uint32_t dfu_file_write_crc(int f, uint32_t crc, const void *buf, int size) +{ + int x; + + /* compute CRC */ + for (x = 0; x != size; x++) + crc = crc32_byte(crc, ((uint8_t *)buf)[x]); + + /* write data */ + if (write(f, buf, size) != size) + err(EX_IOERR, "Could not write %d bytes to file %d", size, f); + + return (crc); +} + +void dfu_load_file(struct dfu_file *file, enum suffix_req check_suffix, enum prefix_req check_prefix) +{ + off_t offset; + int f; + int i; + int res; + + file->size.prefix = 0; + file->size.suffix = 0; + + /* default values, if no valid suffix is found */ + file->bcdDFU = 0; + file->idVendor = 0xffff; /* wildcard value */ + file->idProduct = 0xffff; /* wildcard value */ + file->bcdDevice = 0xffff; /* wildcard value */ + + /* default values, if no valid prefix is found */ + file->lmdfu_address = 0; + + free(file->firmware); + + if (!strcmp(file->name, "-")) { + int read_bytes; + +#ifdef WIN32 + _setmode( _fileno( stdin ), _O_BINARY ); +#endif + file->firmware = (uint8_t*) dfu_malloc(STDIN_CHUNK_SIZE); + read_bytes = fread(file->firmware, 1, STDIN_CHUNK_SIZE, stdin); + file->size.total = read_bytes; + while (read_bytes == STDIN_CHUNK_SIZE) { + file->firmware = (uint8_t*) realloc(file->firmware, file->size.total + STDIN_CHUNK_SIZE); + if (!file->firmware) + err(EX_IOERR, "Could not allocate firmware buffer"); + read_bytes = fread(file->firmware + file->size.total, 1, STDIN_CHUNK_SIZE, stdin); + file->size.total += read_bytes; + } + if (verbose) + printf("Read %i bytes from stdin\n", file->size.total); + /* Never require suffix when reading from stdin */ + check_suffix = MAYBE_SUFFIX; + } else { + f = open(file->name, O_RDONLY | O_BINARY); + if (f < 0) + err(EX_IOERR, "Could not open file %s for reading", file->name); + + offset = lseek(f, 0, SEEK_END); + + if ((int)offset < 0 || (int)offset != offset) + err(EX_IOERR, "File size is too big"); + + if (lseek(f, 0, SEEK_SET) != 0) + err(EX_IOERR, "Could not seek to beginning"); + + file->size.total = offset; + file->firmware = dfu_malloc(file->size.total); + + if (read(f, file->firmware, file->size.total) != file->size.total) { + err(EX_IOERR, "Could not read %d bytes from %s", + file->size.total, file->name); + } + close(f); + } + + /* Check for possible DFU file suffix by trying to parse one */ + { + uint32_t crc = 0xffffffff; + const uint8_t *dfusuffix; + int missing_suffix = 0; + const char *reason; + + if (file->size.total < DFU_SUFFIX_LENGTH) { + reason = "File too short for DFU suffix"; + missing_suffix = 1; + goto checked; + } + + dfusuffix = file->firmware + file->size.total - + DFU_SUFFIX_LENGTH; + + for (i = 0; i < file->size.total - 4; i++) + crc = crc32_byte(crc, file->firmware[i]); + + if (dfusuffix[10] != 'D' || + dfusuffix[9] != 'F' || + dfusuffix[8] != 'U') { + reason = "Invalid DFU suffix signature"; + missing_suffix = 1; + goto checked; + } + + file->dwCRC = (dfusuffix[15] << 24) + + (dfusuffix[14] << 16) + + (dfusuffix[13] << 8) + + dfusuffix[12]; + + if (file->dwCRC != crc) { + reason = "DFU suffix CRC does not match"; + missing_suffix = 1; + goto checked; + } + + /* At this point we believe we have a DFU suffix + so we require further checks to succeed */ + + file->bcdDFU = (dfusuffix[7] << 8) + dfusuffix[6]; + + if (verbose) + printf("DFU suffix version %x\n", file->bcdDFU); + + file->size.suffix = dfusuffix[11]; + + if (file->size.suffix < DFU_SUFFIX_LENGTH) { + errx(EX_IOERR, "Unsupported DFU suffix length %d", + file->size.suffix); + } + + if (file->size.suffix > file->size.total) { + errx(EX_IOERR, "Invalid DFU suffix length %d", + file->size.suffix); + } + + file->idVendor = (dfusuffix[5] << 8) + dfusuffix[4]; + file->idProduct = (dfusuffix[3] << 8) + dfusuffix[2]; + file->bcdDevice = (dfusuffix[1] << 8) + dfusuffix[0]; + +checked: + if (missing_suffix) { + if (check_suffix == NEEDS_SUFFIX) { + warnx("%s", reason); + errx(EX_IOERR, "Valid DFU suffix needed"); + } else if (check_suffix == MAYBE_SUFFIX) { + warnx("%s", reason); + warnx("A valid DFU suffix will be required in " + "a future dfu-util release!!!"); + } + } else { + if (check_suffix == NO_SUFFIX) { + errx(EX_SOFTWARE, "Please remove existing DFU suffix before adding a new one.\n"); + } + } + } + res = probe_prefix(file); + if ((res || file->size.prefix == 0) && check_prefix == NEEDS_PREFIX) + errx(EX_IOERR, "Valid DFU prefix needed"); + if (file->size.prefix && check_prefix == NO_PREFIX) + errx(EX_IOERR, "A prefix already exists, please delete it first"); + if (file->size.prefix && verbose) { + uint8_t *data = file->firmware; + if (file->prefix_type == LMDFU_PREFIX) + printf("Possible TI Stellaris DFU prefix with " + "the following properties\n" + "Address: 0x%08x\n" + "Payload length: %d\n", + file->lmdfu_address, + data[4] | (data[5] << 8) | + (data[6] << 16) | (data[7] << 14)); + else if (file->prefix_type == LPCDFU_UNENCRYPTED_PREFIX) + printf("Possible unencrypted NXP LPC DFU prefix with " + "the following properties\n" + "Payload length: %d kiByte\n", + data[2] >>1 | (data[3] << 7) ); + else + errx(EX_IOERR, "Unknown DFU prefix type"); + } +} + +void dfu_store_file(struct dfu_file *file, int write_suffix, int write_prefix) +{ + uint32_t crc = 0xffffffff; + int f; + + f = open(file->name, O_WRONLY | O_BINARY | O_TRUNC | O_CREAT, 0666); + if (f < 0) + err(EX_IOERR, "Could not open file %s for writing", file->name); + + /* write prefix, if any */ + if (write_prefix) { + if (file->prefix_type == LMDFU_PREFIX) { + uint8_t lmdfu_prefix[LMDFU_PREFIX_LENGTH]; + uint32_t addr = file->lmdfu_address / 1024; + + /* lmdfu_dfu_prefix payload length excludes prefix and suffix */ + uint32_t len = file->size.total - + file->size.prefix - file->size.suffix; + + lmdfu_prefix[0] = 0x01; /* STELLARIS_DFU_PROG */ + lmdfu_prefix[1] = 0x00; /* Reserved */ + lmdfu_prefix[2] = (uint8_t)(addr & 0xff); + lmdfu_prefix[3] = (uint8_t)(addr >> 8); + lmdfu_prefix[4] = (uint8_t)(len & 0xff); + lmdfu_prefix[5] = (uint8_t)(len >> 8) & 0xff; + lmdfu_prefix[6] = (uint8_t)(len >> 16) & 0xff; + lmdfu_prefix[7] = (uint8_t)(len >> 24); + + crc = dfu_file_write_crc(f, crc, lmdfu_prefix, LMDFU_PREFIX_LENGTH); + } + if (file->prefix_type == LPCDFU_UNENCRYPTED_PREFIX) { + uint8_t lpcdfu_prefix[LPCDFU_PREFIX_LENGTH] = {0}; + int i; + + /* Payload is firmware and prefix rounded to 512 bytes */ + uint32_t len = (file->size.total - file->size.suffix + 511) /512; + + lpcdfu_prefix[0] = 0x1a; /* Unencypted*/ + lpcdfu_prefix[1] = 0x3f; /* Reserved */ + lpcdfu_prefix[2] = (uint8_t)(len & 0xff); + lpcdfu_prefix[3] = (uint8_t)((len >> 8) & 0xff); + for (i = 12; i < LPCDFU_PREFIX_LENGTH; i++) + lpcdfu_prefix[i] = 0xff; + + crc = dfu_file_write_crc(f, crc, lpcdfu_prefix, LPCDFU_PREFIX_LENGTH); + } + } + /* write firmware binary */ + crc = dfu_file_write_crc(f, crc, file->firmware + file->size.prefix, + file->size.total - file->size.prefix - file->size.suffix); + + /* write suffix, if any */ + if (write_suffix) { + uint8_t dfusuffix[DFU_SUFFIX_LENGTH]; + + dfusuffix[0] = file->bcdDevice & 0xff; + dfusuffix[1] = file->bcdDevice >> 8; + dfusuffix[2] = file->idProduct & 0xff; + dfusuffix[3] = file->idProduct >> 8; + dfusuffix[4] = file->idVendor & 0xff; + dfusuffix[5] = file->idVendor >> 8; + dfusuffix[6] = file->bcdDFU & 0xff; + dfusuffix[7] = file->bcdDFU >> 8; + dfusuffix[8] = 'U'; + dfusuffix[9] = 'F'; + dfusuffix[10] = 'D'; + dfusuffix[11] = DFU_SUFFIX_LENGTH; + + crc = dfu_file_write_crc(f, crc, dfusuffix, + DFU_SUFFIX_LENGTH - 4); + + dfusuffix[12] = crc; + dfusuffix[13] = crc >> 8; + dfusuffix[14] = crc >> 16; + dfusuffix[15] = crc >> 24; + + crc = dfu_file_write_crc(f, crc, dfusuffix + 12, 4); + } + close(f); +} + +void show_suffix_and_prefix(struct dfu_file *file) +{ + if (file->size.prefix == LMDFU_PREFIX_LENGTH) { + printf("The file %s contains a TI Stellaris DFU prefix with the following properties:\n", file->name); + printf("Address:\t0x%08x\n", file->lmdfu_address); + } else if (file->size.prefix == LPCDFU_PREFIX_LENGTH) { + uint8_t * prefix = file->firmware; + printf("The file %s contains a NXP unencrypted LPC DFU prefix with the following properties:\n", file->name); + printf("Size:\t%5d kiB\n", prefix[2]>>1|prefix[3]<<7); + } else if (file->size.prefix != 0) { + printf("The file %s contains an unknown prefix\n", file->name); + } + if (file->size.suffix > 0) { + printf("The file %s contains a DFU suffix with the following properties:\n", file->name); + printf("BCD device:\t0x%04X\n", file->bcdDevice); + printf("Product ID:\t0x%04X\n",file->idProduct); + printf("Vendor ID:\t0x%04X\n", file->idVendor); + printf("BCD DFU:\t0x%04X\n", file->bcdDFU); + printf("Length:\t\t%i\n", file->size.suffix); + printf("CRC:\t\t0x%08X\n", file->dwCRC); + } +} diff --git a/tools/win/src/dfu-util/src/dfu_file.h b/tools/win/src/dfu-util/src/dfu_file.h new file mode 100644 index 0000000..abebd44 --- /dev/null +++ b/tools/win/src/dfu-util/src/dfu_file.h @@ -0,0 +1,60 @@ + +#ifndef DFU_FILE_H +#define DFU_FILE_H + +#include + +struct dfu_file { + /* File name */ + const char *name; + /* Pointer to file loaded into memory */ + uint8_t *firmware; + /* Different sizes */ + struct { + int total; + int prefix; + int suffix; + } size; + /* From prefix fields */ + uint32_t lmdfu_address; + /* From prefix fields */ + uint32_t prefix_type; + + /* From DFU suffix fields */ + uint32_t dwCRC; + uint16_t bcdDFU; + uint16_t idVendor; + uint16_t idProduct; + uint16_t bcdDevice; +}; + +enum suffix_req { + NO_SUFFIX, + NEEDS_SUFFIX, + MAYBE_SUFFIX +}; + +enum prefix_req { + NO_PREFIX, + NEEDS_PREFIX, + MAYBE_PREFIX +}; + +enum prefix_type { + ZERO_PREFIX, + LMDFU_PREFIX, + LPCDFU_UNENCRYPTED_PREFIX +}; + +extern int verbose; + +void dfu_load_file(struct dfu_file *file, enum suffix_req check_suffix, enum prefix_req check_prefix); +void dfu_store_file(struct dfu_file *file, int write_suffix, int write_prefix); + +void dfu_progress_bar(const char *desc, unsigned long long curr, + unsigned long long max); +void *dfu_malloc(size_t size); +uint32_t dfu_file_write_crc(int f, uint32_t crc, const void *buf, int size); +void show_suffix_and_prefix(struct dfu_file *file); + +#endif /* DFU_FILE_H */ diff --git a/tools/win/src/dfu-util/src/dfu_load.c b/tools/win/src/dfu-util/src/dfu_load.c new file mode 100644 index 0000000..64f7009 --- /dev/null +++ b/tools/win/src/dfu-util/src/dfu_load.c @@ -0,0 +1,196 @@ +/* + * DFU transfer routines + * + * This is supposed to be a general DFU implementation, as specified in the + * USB DFU 1.0 and 1.1 specification. + * + * The code was originally intended to interface with a USB device running the + * "sam7dfu" firmware (see http://www.openpcd.org/) on an AT91SAM7 processor. + * + * Copyright 2007-2008 Harald Welte + * Copyright 2013 Hans Petter Selasky + * Copyright 2014 Tormod Volden + * + * 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 +#include +#include +#include + +#include + +#include "portable.h" +#include "dfu.h" +#include "usb_dfu.h" +#include "dfu_file.h" +#include "dfu_load.h" +#include "quirks.h" + +int dfuload_do_upload(struct dfu_if *dif, int xfer_size, + int expected_size, int fd) +{ + int total_bytes = 0; + unsigned short transaction = 0; + unsigned char *buf; + int ret; + + buf = dfu_malloc(xfer_size); + + printf("Copying data from DFU device to PC\n"); + dfu_progress_bar("Upload", 0, 1); + + while (1) { + int rc; + rc = dfu_upload(dif->dev_handle, dif->interface, + xfer_size, transaction++, buf); + if (rc < 0) { + warnx("Error during upload"); + ret = rc; + goto out_free; + } + + dfu_file_write_crc(fd, 0, buf, rc); + total_bytes += rc; + + if (total_bytes < 0) + errx(EX_SOFTWARE, "Received too many bytes (wraparound)"); + + if (rc < xfer_size) { + /* last block, return */ + ret = total_bytes; + break; + } + dfu_progress_bar("Upload", total_bytes, expected_size); + } + ret = 0; + +out_free: + dfu_progress_bar("Upload", total_bytes, total_bytes); + if (total_bytes == 0) + printf("\nFailed.\n"); + free(buf); + if (verbose) + printf("Received a total of %i bytes\n", total_bytes); + if (expected_size != 0 && total_bytes != expected_size) + errx(EX_SOFTWARE, "Unexpected number of bytes uploaded from device"); + return ret; +} + +int dfuload_do_dnload(struct dfu_if *dif, int xfer_size, struct dfu_file *file) +{ + int bytes_sent; + int expected_size; + unsigned char *buf; + unsigned short transaction = 0; + struct dfu_status dst; + int ret; + + printf("Copying data from PC to DFU device\n"); + + buf = file->firmware; + expected_size = file->size.total - file->size.suffix; + bytes_sent = 0; + + dfu_progress_bar("Download", 0, 1); + while (bytes_sent < expected_size) { + int bytes_left; + int chunk_size; + + bytes_left = expected_size - bytes_sent; + if (bytes_left < xfer_size) + chunk_size = bytes_left; + else + chunk_size = xfer_size; + + ret = dfu_download(dif->dev_handle, dif->interface, + chunk_size, transaction++, chunk_size ? buf : NULL); + if (ret < 0) { + warnx("Error during download"); + goto out; + } + bytes_sent += chunk_size; + buf += chunk_size; + + do { + ret = dfu_get_status(dif, &dst); + if (ret < 0) { + errx(EX_IOERR, "Error during download get_status"); + goto out; + } + + if (dst.bState == DFU_STATE_dfuDNLOAD_IDLE || + dst.bState == DFU_STATE_dfuERROR) + break; + + /* Wait while device executes flashing */ + milli_sleep(dst.bwPollTimeout); + + } while (1); + 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; + } + dfu_progress_bar("Download", bytes_sent, bytes_sent + bytes_left); + } + + /* send one zero sized download request to signalize end */ + ret = dfu_download(dif->dev_handle, dif->interface, + 0, transaction, NULL); + if (ret < 0) { + errx(EX_IOERR, "Error sending completion packet"); + goto out; + } + + dfu_progress_bar("Download", bytes_sent, bytes_sent); + + if (verbose) + printf("Sent a total of %i bytes\n", bytes_sent); + +get_status: + /* Transition to MANIFEST_SYNC state */ + ret = dfu_get_status(dif, &dst); + if (ret < 0) { + warnx("unable to read DFU status after completion"); + goto out; + } + printf("state(%u) = %s, status(%u) = %s\n", dst.bState, + dfu_state_to_string(dst.bState), dst.bStatus, + dfu_status_to_string(dst.bStatus)); + + milli_sleep(dst.bwPollTimeout); + + /* 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 */ + milli_sleep(1000); + goto get_status; + break; + case DFU_STATE_dfuIDLE: + break; + } + printf("Done!\n"); + +out: + return bytes_sent; +} diff --git a/tools/win/src/dfu-util/src/dfu_load.h b/tools/win/src/dfu-util/src/dfu_load.h new file mode 100644 index 0000000..be23e9b --- /dev/null +++ b/tools/win/src/dfu-util/src/dfu_load.h @@ -0,0 +1,7 @@ +#ifndef DFU_LOAD_H +#define DFU_LOAD_H + +int dfuload_do_upload(struct dfu_if *dif, int xfer_size, int expected_size, int fd); +int dfuload_do_dnload(struct dfu_if *dif, int xfer_size, struct dfu_file *file); + +#endif /* DFU_LOAD_H */ diff --git a/tools/win/src/dfu-util/src/dfu_util.c b/tools/win/src/dfu-util/src/dfu_util.c new file mode 100644 index 0000000..b94c7cc --- /dev/null +++ b/tools/win/src/dfu-util/src/dfu_util.c @@ -0,0 +1,346 @@ +/* + * Functions for detecting DFU USB entities + * + * Written by Harald Welte + * Copyright 2007-2008 by OpenMoko, Inc. + * Copyright 2013 Hans Petter Selasky + * + * 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 +#include +#include +#include +#include + +#include "portable.h" +#include "dfu.h" +#include "usb_dfu.h" +#include "dfu_file.h" +#include "dfu_load.h" +#include "dfu_util.h" +#include "dfuse.h" +#include "quirks.h" + +#ifdef HAVE_USBPATH_H +#include +#endif + +/* + * Look for a descriptor in a concatenated descriptor list. Will + * return upon the first match of the given descriptor type. Returns length of + * found descriptor, limited to res_size + */ +static int find_descriptor(const uint8_t *desc_list, int list_len, + uint8_t desc_type, void *res_buf, int res_size) +{ + int p = 0; + + if (list_len < 2) + return (-1); + + while (p + 1 < list_len) { + int desclen; + + desclen = (int) desc_list[p]; + if (desclen == 0) { + warnx("Invalid descriptor list"); + return -1; + } + if (desc_list[p + 1] == desc_type) { + if (desclen > res_size) + desclen = res_size; + if (p + desclen > list_len) + desclen = list_len - p; + memcpy(res_buf, &desc_list[p], desclen); + return desclen; + } + p += (int) desc_list[p]; + } + return -1; +} + +static void probe_configuration(libusb_device *dev, struct libusb_device_descriptor *desc) +{ + struct usb_dfu_func_descriptor func_dfu; + libusb_device_handle *devh; + struct dfu_if *pdfu; + struct libusb_config_descriptor *cfg; + const struct libusb_interface_descriptor *intf; + const struct libusb_interface *uif; + char alt_name[MAX_DESC_STR_LEN + 1]; + char serial_name[MAX_DESC_STR_LEN + 1]; + int cfg_idx; + int intf_idx; + int alt_idx; + int ret; + int has_dfu; + + for (cfg_idx = 0; cfg_idx != desc->bNumConfigurations; cfg_idx++) { + memset(&func_dfu, 0, sizeof(func_dfu)); + has_dfu = 0; + + ret = libusb_get_config_descriptor(dev, cfg_idx, &cfg); + if (ret != 0) + return; + if (match_config_index > -1 && match_config_index != cfg->bConfigurationValue) { + libusb_free_config_descriptor(cfg); + continue; + } + + /* + * In some cases, noticably FreeBSD if uid != 0, + * the configuration descriptors are empty + */ + if (!cfg) + return; + + ret = find_descriptor(cfg->extra, cfg->extra_length, + USB_DT_DFU, &func_dfu, sizeof(func_dfu)); + if (ret > -1) + goto found_dfu; + + for (intf_idx = 0; intf_idx < cfg->bNumInterfaces; + intf_idx++) { + uif = &cfg->interface[intf_idx]; + if (!uif) + break; + + for (alt_idx = 0; alt_idx < cfg->interface[intf_idx].num_altsetting; + alt_idx++) { + intf = &uif->altsetting[alt_idx]; + + ret = find_descriptor(intf->extra, intf->extra_length, USB_DT_DFU, + &func_dfu, sizeof(func_dfu)); + if (ret > -1) + goto found_dfu; + + if (intf->bInterfaceClass != 0xfe || + intf->bInterfaceSubClass != 1) + continue; + + has_dfu = 1; + } + } + if (has_dfu) { + /* + * Finally try to retrieve it requesting the + * device directly This is not supported on + * all devices for non-standard types + */ + if (libusb_open(dev, &devh) == 0) { + ret = libusb_get_descriptor(devh, USB_DT_DFU, 0, + (void *)&func_dfu, sizeof(func_dfu)); + libusb_close(devh); + if (ret > -1) + goto found_dfu; + } + warnx("Device has DFU interface, " + "but has no DFU functional descriptor"); + + /* fake version 1.0 */ + func_dfu.bLength = 7; + func_dfu.bcdDFUVersion = libusb_cpu_to_le16(0x0100); + goto found_dfu; + } + libusb_free_config_descriptor(cfg); + continue; + +found_dfu: + if (func_dfu.bLength == 7) { + printf("Deducing device DFU version from functional descriptor " + "length\n"); + func_dfu.bcdDFUVersion = libusb_cpu_to_le16(0x0100); + } else if (func_dfu.bLength < 9) { + printf("Error obtaining DFU functional descriptor\n"); + printf("Please report this as a bug!\n"); + printf("Warning: Assuming DFU version 1.0\n"); + func_dfu.bcdDFUVersion = libusb_cpu_to_le16(0x0100); + printf("Warning: Transfer size can not be detected\n"); + func_dfu.wTransferSize = 0; + } + + for (intf_idx = 0; intf_idx < cfg->bNumInterfaces; + intf_idx++) { + if (match_iface_index > -1 && match_iface_index != intf_idx) + continue; + + uif = &cfg->interface[intf_idx]; + if (!uif) + break; + + for (alt_idx = 0; + alt_idx < uif->num_altsetting; alt_idx++) { + int dfu_mode; + + intf = &uif->altsetting[alt_idx]; + + if (intf->bInterfaceClass != 0xfe || + intf->bInterfaceSubClass != 1) + continue; + + dfu_mode = (intf->bInterfaceProtocol == 2); + /* e.g. DSO Nano has bInterfaceProtocol 0 instead of 2 */ + if (func_dfu.bcdDFUVersion == 0x011a && intf->bInterfaceProtocol == 0) + dfu_mode = 1; + + if (dfu_mode && + match_iface_alt_index > -1 && match_iface_alt_index != alt_idx) + continue; + + if (dfu_mode) { + if ((match_vendor_dfu >= 0 && match_vendor_dfu != desc->idVendor) || + (match_product_dfu >= 0 && match_product_dfu != desc->idProduct)) { + continue; + } + } else { + if ((match_vendor >= 0 && match_vendor != desc->idVendor) || + (match_product >= 0 && match_product != desc->idProduct)) { + continue; + } + } + + if (libusb_open(dev, &devh)) { + warnx("Cannot open DFU device %04x:%04x", desc->idVendor, desc->idProduct); + break; + } + if (intf->iInterface != 0) + ret = libusb_get_string_descriptor_ascii(devh, + intf->iInterface, (void *)alt_name, MAX_DESC_STR_LEN); + else + ret = -1; + if (ret < 1) + strcpy(alt_name, "UNKNOWN"); + if (desc->iSerialNumber != 0) + ret = libusb_get_string_descriptor_ascii(devh, + desc->iSerialNumber, (void *)serial_name, MAX_DESC_STR_LEN); + else + ret = -1; + if (ret < 1) + strcpy(serial_name, "UNKNOWN"); + libusb_close(devh); + + if (dfu_mode && + match_iface_alt_name != NULL && strcmp(alt_name, match_iface_alt_name)) + continue; + + if (dfu_mode) { + if (match_serial_dfu != NULL && strcmp(match_serial_dfu, serial_name)) + continue; + } else { + if (match_serial != NULL && strcmp(match_serial, serial_name)) + continue; + } + + pdfu = dfu_malloc(sizeof(*pdfu)); + + memset(pdfu, 0, sizeof(*pdfu)); + + pdfu->func_dfu = func_dfu; + pdfu->dev = libusb_ref_device(dev); + pdfu->quirks = get_quirks(desc->idVendor, + desc->idProduct, desc->bcdDevice); + pdfu->vendor = desc->idVendor; + pdfu->product = desc->idProduct; + pdfu->bcdDevice = desc->bcdDevice; + pdfu->configuration = cfg->bConfigurationValue; + pdfu->interface = intf->bInterfaceNumber; + pdfu->altsetting = intf->bAlternateSetting; + pdfu->devnum = libusb_get_device_address(dev); + pdfu->busnum = libusb_get_bus_number(dev); + pdfu->alt_name = strdup(alt_name); + if (pdfu->alt_name == NULL) + errx(EX_SOFTWARE, "Out of memory"); + pdfu->serial_name = strdup(serial_name); + if (pdfu->serial_name == NULL) + errx(EX_SOFTWARE, "Out of memory"); + if (dfu_mode) + pdfu->flags |= DFU_IFF_DFU; + if (pdfu->quirks & QUIRK_FORCE_DFU11) { + pdfu->func_dfu.bcdDFUVersion = + libusb_cpu_to_le16(0x0110); + } + pdfu->bMaxPacketSize0 = desc->bMaxPacketSize0; + + /* queue into list */ + pdfu->next = dfu_root; + dfu_root = pdfu; + } + } + libusb_free_config_descriptor(cfg); + } +} + +void probe_devices(libusb_context *ctx) +{ + libusb_device **list; + ssize_t num_devs; + ssize_t i; + + num_devs = libusb_get_device_list(ctx, &list); + for (i = 0; i < num_devs; ++i) { + struct libusb_device_descriptor desc; + struct libusb_device *dev = list[i]; + + if (match_bus > -1 && match_bus != libusb_get_bus_number(dev)) + continue; + if (match_device > -1 && match_device != libusb_get_device_address(dev)) + continue; + if (libusb_get_device_descriptor(dev, &desc)) + continue; + probe_configuration(dev, &desc); + } + libusb_free_device_list(list, 0); +} + +void disconnect_devices(void) +{ + struct dfu_if *pdfu; + struct dfu_if *prev = NULL; + + for (pdfu = dfu_root; pdfu != NULL; pdfu = pdfu->next) { + free(prev); + libusb_unref_device(pdfu->dev); + free(pdfu->alt_name); + free(pdfu->serial_name); + prev = pdfu; + } + free(prev); + dfu_root = NULL; +} + +void print_dfu_if(struct dfu_if *dfu_if) +{ + printf("Found %s: [%04x:%04x] ver=%04x, devnum=%u, cfg=%u, intf=%u, " + "alt=%u, name=\"%s\", serial=\"%s\"\n", + dfu_if->flags & DFU_IFF_DFU ? "DFU" : "Runtime", + dfu_if->vendor, dfu_if->product, + dfu_if->bcdDevice, dfu_if->devnum, + dfu_if->configuration, dfu_if->interface, + dfu_if->altsetting, dfu_if->alt_name, + dfu_if->serial_name); +} + +/* Walk the device tree and print out DFU devices */ +void list_dfu_interfaces(void) +{ + struct dfu_if *pdfu; + + for (pdfu = dfu_root; pdfu != NULL; pdfu = pdfu->next) + print_dfu_if(pdfu); +} diff --git a/tools/win/src/dfu-util/src/dfu_util.h b/tools/win/src/dfu-util/src/dfu_util.h new file mode 100644 index 0000000..fc0c19d --- /dev/null +++ b/tools/win/src/dfu-util/src/dfu_util.h @@ -0,0 +1,36 @@ +#ifndef DFU_UTIL_H +#define DFU_UTIL_H + +/* USB string descriptor should contain max 126 UTF-16 characters + * but 253 would even accomodate any UTF-8 encoding */ +#define MAX_DESC_STR_LEN 253 + +enum mode { + MODE_NONE, + MODE_VERSION, + MODE_LIST, + MODE_DETACH, + MODE_UPLOAD, + MODE_DOWNLOAD +}; + +extern struct dfu_if *dfu_root; +extern int match_bus; +extern int match_device; +extern int match_vendor; +extern int match_product; +extern int match_vendor_dfu; +extern int match_product_dfu; +extern int match_config_index; +extern int match_iface_index; +extern int match_iface_alt_index; +extern const char *match_iface_alt_name; +extern const char *match_serial; +extern const char *match_serial_dfu; + +void probe_devices(libusb_context *); +void disconnect_devices(void); +void print_dfu_if(struct dfu_if *); +void list_dfu_interfaces(void); + +#endif /* DFU_UTIL_H */ diff --git a/tools/win/src/dfu-util/src/dfuse.c b/tools/win/src/dfu-util/src/dfuse.c new file mode 100644 index 0000000..fce29fe --- /dev/null +++ b/tools/win/src/dfu-util/src/dfuse.c @@ -0,0 +1,652 @@ +/* + * DfuSe specific functions + * + * This implements the ST Microsystems DFU extensions (DfuSe) + * as per the DfuSe 1.1a specification (ST documents AN3156, AN2606) + * The DfuSe file format is described in ST document UM0391. + * + * Copyright 2010-2014 Tormod Volden + * + * 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 +#include +#include +#include + +#include "portable.h" +#include "dfu.h" +#include "usb_dfu.h" +#include "dfu_file.h" +#include "dfuse.h" +#include "dfuse_mem.h" + +#define DFU_TIMEOUT 5000 + +extern int verbose; +static unsigned int last_erased_page = 1; /* non-aligned value, won't match */ +static struct memsegment *mem_layout; +static unsigned int dfuse_address = 0; +static unsigned int dfuse_length = 0; +static int dfuse_force = 0; +static int dfuse_leave = 0; +static int dfuse_unprotect = 0; +static int dfuse_mass_erase = 0; + +unsigned int quad2uint(unsigned char *p) +{ + return (*p + (*(p + 1) << 8) + (*(p + 2) << 16) + (*(p + 3) << 24)); +} + +void dfuse_parse_options(const char *options) +{ + char *end; + const char *endword; + unsigned int number; + + /* address, possibly empty, must be first */ + if (*options != ':') { + endword = strchr(options, ':'); + if (!endword) + endword = options + strlen(options); /* GNU strchrnul */ + + number = strtoul(options, &end, 0); + if (end == endword) { + dfuse_address = number; + } else { + errx(EX_IOERR, "Invalid dfuse address: %s", options); + } + options = endword; + } + + while (*options) { + if (*options == ':') { + options++; + continue; + } + endword = strchr(options, ':'); + if (!endword) + endword = options + strlen(options); + + if (!strncmp(options, "force", endword - options)) { + dfuse_force++; + options += 5; + continue; + } + if (!strncmp(options, "leave", endword - options)) { + dfuse_leave = 1; + options += 5; + continue; + } + if (!strncmp(options, "unprotect", endword - options)) { + dfuse_unprotect = 1; + options += 9; + continue; + } + if (!strncmp(options, "mass-erase", endword - options)) { + dfuse_mass_erase = 1; + options += 10; + continue; + } + + /* any valid number is interpreted as upload length */ + number = strtoul(options, &end, 0); + if (end == endword) { + dfuse_length = number; + } else { + errx(EX_IOERR, "Invalid dfuse modifier: %s", options); + } + options = endword; + } +} + +/* DFU_UPLOAD request for DfuSe 1.1a */ +int dfuse_upload(struct dfu_if *dif, const unsigned short length, + unsigned char *data, unsigned short transaction) +{ + int status; + + status = libusb_control_transfer(dif->dev_handle, + /* bmRequestType */ LIBUSB_ENDPOINT_IN | + LIBUSB_REQUEST_TYPE_CLASS | + LIBUSB_RECIPIENT_INTERFACE, + /* bRequest */ DFU_UPLOAD, + /* wValue */ transaction, + /* wIndex */ dif->interface, + /* Data */ data, + /* wLength */ length, + DFU_TIMEOUT); + if (status < 0) { + errx(EX_IOERR, "%s: libusb_control_msg returned %d", + __FUNCTION__, status); + } + return status; +} + +/* DFU_DNLOAD request for DfuSe 1.1a */ +int dfuse_download(struct dfu_if *dif, const unsigned short length, + unsigned char *data, unsigned short transaction) +{ + int status; + + status = libusb_control_transfer(dif->dev_handle, + /* bmRequestType */ LIBUSB_ENDPOINT_OUT | + LIBUSB_REQUEST_TYPE_CLASS | + LIBUSB_RECIPIENT_INTERFACE, + /* bRequest */ DFU_DNLOAD, + /* wValue */ transaction, + /* wIndex */ dif->interface, + /* Data */ data, + /* wLength */ length, + DFU_TIMEOUT); + if (status < 0) { + errx(EX_IOERR, "%s: libusb_control_transfer returned %d", + __FUNCTION__, status); + } + return status; +} + +/* DfuSe only commands */ +/* Leaves the device in dfuDNLOAD-IDLE state */ +int dfuse_special_command(struct dfu_if *dif, unsigned int address, + enum dfuse_command command) +{ + const char* dfuse_command_name[] = { "SET_ADDRESS" , "ERASE_PAGE", + "MASS_ERASE", "READ_UNPROTECT"}; + unsigned char buf[5]; + int length; + int ret; + struct dfu_status dst; + int firstpoll = 1; + + if (command == ERASE_PAGE) { + struct memsegment *segment; + int page_size; + + segment = find_segment(mem_layout, address); + if (!segment || !(segment->memtype & DFUSE_ERASABLE)) { + errx(EX_IOERR, "Page at 0x%08x can not be erased", + address); + } + page_size = segment->pagesize; + if (verbose > 1) + printf("Erasing page size %i at address 0x%08x, page " + "starting at 0x%08x\n", page_size, address, + address & ~(page_size - 1)); + buf[0] = 0x41; /* Erase command */ + length = 5; + last_erased_page = address & ~(page_size - 1); + } else if (command == SET_ADDRESS) { + if (verbose > 2) + printf(" Setting address pointer to 0x%08x\n", + address); + buf[0] = 0x21; /* Set Address Pointer command */ + length = 5; + } else if (command == MASS_ERASE) { + buf[0] = 0x41; /* Mass erase command when length = 1 */ + length = 1; + } else if (command == READ_UNPROTECT) { + buf[0] = 0x92; + length = 1; + } else { + errx(EX_IOERR, "Non-supported special command %d", command); + } + buf[1] = address & 0xff; + buf[2] = (address >> 8) & 0xff; + buf[3] = (address >> 16) & 0xff; + buf[4] = (address >> 24) & 0xff; + + ret = dfuse_download(dif, length, buf, 0); + if (ret < 0) { + errx(EX_IOERR, "Error during special command \"%s\" download", + dfuse_command_name[command]); + } + do { + ret = dfu_get_status(dif, &dst); + if (ret < 0) { + errx(EX_IOERR, "Error during special command \"%s\" get_status", + dfuse_command_name[command]); + } + if (firstpoll) { + firstpoll = 0; + if (dst.bState != DFU_STATE_dfuDNBUSY) { + printf("state(%u) = %s, status(%u) = %s\n", dst.bState, + dfu_state_to_string(dst.bState), dst.bStatus, + dfu_status_to_string(dst.bStatus)); + errx(EX_IOERR, "Wrong state after command \"%s\" download", + dfuse_command_name[command]); + } + } + /* wait while command is executed */ + if (verbose) + printf(" Poll timeout %i ms\n", dst.bwPollTimeout); + milli_sleep(dst.bwPollTimeout); + if (command == READ_UNPROTECT) + return ret; + } while (dst.bState == DFU_STATE_dfuDNBUSY); + + if (dst.bStatus != DFU_STATUS_OK) { + errx(EX_IOERR, "%s not correctly executed", + dfuse_command_name[command]); + } + return ret; +} + +int dfuse_dnload_chunk(struct dfu_if *dif, unsigned char *data, int size, + int transaction) +{ + int bytes_sent; + struct dfu_status dst; + int ret; + + ret = dfuse_download(dif, size, size ? data : NULL, transaction); + if (ret < 0) { + errx(EX_IOERR, "Error during download"); + return ret; + } + bytes_sent = ret; + + do { + ret = dfu_get_status(dif, &dst); + if (ret < 0) { + errx(EX_IOERR, "Error during download get_status"); + return ret; + } + milli_sleep(dst.bwPollTimeout); + } while (dst.bState != DFU_STATE_dfuDNLOAD_IDLE && + dst.bState != DFU_STATE_dfuERROR && + dst.bState != DFU_STATE_dfuMANIFEST); + + if (dst.bState == DFU_STATE_dfuMANIFEST) + printf("Transitioning to dfuMANIFEST state\n"); + + 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)); + return -1; + } + return bytes_sent; +} + +int dfuse_do_upload(struct dfu_if *dif, int xfer_size, int fd, + const char *dfuse_options) +{ + int total_bytes = 0; + int upload_limit = 0; + unsigned char *buf; + int transaction; + int ret; + + buf = dfu_malloc(xfer_size); + + if (dfuse_options) + dfuse_parse_options(dfuse_options); + if (dfuse_length) + upload_limit = dfuse_length; + if (dfuse_address) { + struct memsegment *segment; + + mem_layout = parse_memory_layout((char *)dif->alt_name); + if (!mem_layout) + errx(EX_IOERR, "Failed to parse memory layout"); + + segment = find_segment(mem_layout, dfuse_address); + if (!dfuse_force && + (!segment || !(segment->memtype & DFUSE_READABLE))) + errx(EX_IOERR, "Page at 0x%08x is not readable", + dfuse_address); + + if (!upload_limit) { + upload_limit = segment->end - dfuse_address + 1; + printf("Limiting upload to end of memory segment, " + "%i bytes\n", upload_limit); + } + dfuse_special_command(dif, dfuse_address, SET_ADDRESS); + dfu_abort_to_idle(dif); + } else { + /* Boot loader decides the start address, unknown to us */ + /* Use a short length to lower risk of running out of bounds */ + if (!upload_limit) + upload_limit = 0x4000; + printf("Limiting default upload to %i bytes\n", upload_limit); + } + + dfu_progress_bar("Upload", 0, 1); + + transaction = 2; + while (1) { + int rc; + + /* last chunk can be smaller than original xfer_size */ + if (upload_limit - total_bytes < xfer_size) + xfer_size = upload_limit - total_bytes; + rc = dfuse_upload(dif, xfer_size, buf, transaction++); + if (rc < 0) { + ret = rc; + goto out_free; + } + + dfu_file_write_crc(fd, 0, buf, rc); + total_bytes += rc; + + if (total_bytes < 0) + errx(EX_SOFTWARE, "Received too many bytes"); + + if (rc < xfer_size || total_bytes >= upload_limit) { + /* last block, return successfully */ + ret = total_bytes; + break; + } + dfu_progress_bar("Upload", total_bytes, upload_limit); + } + + dfu_progress_bar("Upload", total_bytes, total_bytes); + + dfu_abort_to_idle(dif); + if (dfuse_leave) { + dfuse_special_command(dif, dfuse_address, SET_ADDRESS); + dfuse_dnload_chunk(dif, NULL, 0, 2); /* Zero-size */ + } + + out_free: + free(buf); + + return ret; +} + +/* Writes an element of any size to the device, taking care of page erases */ +/* returns 0 on success, otherwise -EINVAL */ +int dfuse_dnload_element(struct dfu_if *dif, unsigned int dwElementAddress, + unsigned int dwElementSize, unsigned char *data, + int xfer_size) +{ + int p; + int ret; + struct memsegment *segment; + + /* Check at least that we can write to the last address */ + segment = + find_segment(mem_layout, dwElementAddress + dwElementSize - 1); + if (!segment || !(segment->memtype & DFUSE_WRITEABLE)) { + errx(EX_IOERR, "Last page at 0x%08x is not writeable", + dwElementAddress + dwElementSize - 1); + } + + dfu_progress_bar("Download", 0, 1); + + for (p = 0; p < (int)dwElementSize; p += xfer_size) { + int page_size; + unsigned int erase_address; + unsigned int address = dwElementAddress + p; + int chunk_size = xfer_size; + + segment = find_segment(mem_layout, address); + if (!segment || !(segment->memtype & DFUSE_WRITEABLE)) { + errx(EX_IOERR, "Page at 0x%08x is not writeable", + address); + } + page_size = segment->pagesize; + + /* check if this is the last chunk */ + if (p + chunk_size > (int)dwElementSize) + chunk_size = dwElementSize - p; + + /* Erase only for flash memory downloads */ + if ((segment->memtype & DFUSE_ERASABLE) && !dfuse_mass_erase) { + /* erase all involved pages */ + for (erase_address = address; + erase_address < address + chunk_size; + erase_address += page_size) + if ((erase_address & ~(page_size - 1)) != + last_erased_page) + dfuse_special_command(dif, + erase_address, + ERASE_PAGE); + + if (((address + chunk_size - 1) & ~(page_size - 1)) != + last_erased_page) { + if (verbose > 2) + printf(" Chunk extends into next page," + " erase it as well\n"); + dfuse_special_command(dif, + address + chunk_size - 1, + ERASE_PAGE); + } + } + + if (verbose) { + printf(" Download from image offset " + "%08x to memory %08x-%08x, size %i\n", + p, address, address + chunk_size - 1, + chunk_size); + } else { + dfu_progress_bar("Download", p, dwElementSize); + } + + dfuse_special_command(dif, address, SET_ADDRESS); + + /* transaction = 2 for no address offset */ + ret = dfuse_dnload_chunk(dif, data + p, chunk_size, 2); + if (ret != chunk_size) { + errx(EX_IOERR, "Failed to write whole chunk: " + "%i of %i bytes", ret, chunk_size); + return -EINVAL; + } + } + if (!verbose) + dfu_progress_bar("Download", dwElementSize, dwElementSize); + return 0; +} + +static void +dfuse_memcpy(unsigned char *dst, unsigned char **src, int *rem, int size) +{ + if (size > *rem) { + errx(EX_IOERR, "Corrupt DfuSe file: " + "Cannot read %d bytes from %d bytes", size, *rem); + } + if (dst != NULL) + memcpy(dst, *src, size); + (*src) += size; + (*rem) -= size; +} + +/* Download raw binary file to DfuSe device */ +int dfuse_do_bin_dnload(struct dfu_if *dif, int xfer_size, + struct dfu_file *file, unsigned int start_address) +{ + unsigned int dwElementAddress; + unsigned int dwElementSize; + unsigned char *data; + int ret; + + dwElementAddress = start_address; + dwElementSize = file->size.total - + file->size.suffix - file->size.prefix; + + printf("Downloading to address = 0x%08x, size = %i\n", + dwElementAddress, dwElementSize); + + data = file->firmware + file->size.prefix; + + ret = dfuse_dnload_element(dif, dwElementAddress, dwElementSize, data, + xfer_size); + if (ret != 0) + goto out_free; + + printf("File downloaded successfully\n"); + ret = dwElementSize; + + out_free: + return ret; +} + +/* Parse a DfuSe file and download contents to device */ +int dfuse_do_dfuse_dnload(struct dfu_if *dif, int xfer_size, + struct dfu_file *file) +{ + uint8_t dfuprefix[11]; + uint8_t targetprefix[274]; + uint8_t elementheader[8]; + int image; + int element; + int bTargets; + int bAlternateSetting; + int dwNbElements; + unsigned int dwElementAddress; + unsigned int dwElementSize; + uint8_t *data; + int ret; + int rem; + int bFirstAddressSaved = 0; + + rem = file->size.total - file->size.prefix - file->size.suffix; + data = file->firmware + file->size.prefix; + + /* Must be larger than a minimal DfuSe header and suffix */ + if (rem < (int)(sizeof(dfuprefix) + + sizeof(targetprefix) + sizeof(elementheader))) { + errx(EX_SOFTWARE, "File too small for a DfuSe file"); + } + + dfuse_memcpy(dfuprefix, &data, &rem, sizeof(dfuprefix)); + + if (strncmp((char *)dfuprefix, "DfuSe", 5)) { + errx(EX_IOERR, "No valid DfuSe signature"); + return -EINVAL; + } + if (dfuprefix[5] != 0x01) { + errx(EX_IOERR, "DFU format revision %i not supported", + dfuprefix[5]); + return -EINVAL; + } + bTargets = dfuprefix[10]; + printf("file contains %i DFU images\n", bTargets); + + for (image = 1; image <= bTargets; image++) { + printf("parsing DFU image %i\n", image); + dfuse_memcpy(targetprefix, &data, &rem, sizeof(targetprefix)); + if (strncmp((char *)targetprefix, "Target", 6)) { + errx(EX_IOERR, "No valid target signature"); + return -EINVAL; + } + bAlternateSetting = targetprefix[6]; + dwNbElements = quad2uint((unsigned char *)targetprefix + 270); + printf("image for alternate setting %i, ", bAlternateSetting); + printf("(%i elements, ", dwNbElements); + printf("total size = %i)\n", + quad2uint((unsigned char *)targetprefix + 266)); + if (bAlternateSetting != dif->altsetting) + printf("Warning: Image does not match current alternate" + " setting.\n" + "Please rerun with the correct -a option setting" + " to download this image!\n"); + for (element = 1; element <= dwNbElements; element++) { + printf("parsing element %i, ", element); + dfuse_memcpy(elementheader, &data, &rem, sizeof(elementheader)); + dwElementAddress = + quad2uint((unsigned char *)elementheader); + dwElementSize = + quad2uint((unsigned char *)elementheader + 4); + printf("address = 0x%08x, ", dwElementAddress); + printf("size = %i\n", dwElementSize); + + if (!bFirstAddressSaved) { + bFirstAddressSaved = 1; + dfuse_address = dwElementAddress; + } + /* sanity check */ + if ((int)dwElementSize > rem) + errx(EX_SOFTWARE, "File too small for element size"); + + if (bAlternateSetting == dif->altsetting) { + ret = dfuse_dnload_element(dif, dwElementAddress, + dwElementSize, data, xfer_size); + } else { + ret = 0; + } + + /* advance read pointer */ + dfuse_memcpy(NULL, &data, &rem, dwElementSize); + + if (ret != 0) + return ret; + } + } + + if (rem != 0) + warnx("%d bytes leftover", rem); + + printf("done parsing DfuSe file\n"); + + return 0; +} + +int dfuse_do_dnload(struct dfu_if *dif, int xfer_size, struct dfu_file *file, + const char *dfuse_options) +{ + int ret; + + if (dfuse_options) + dfuse_parse_options(dfuse_options); + mem_layout = parse_memory_layout((char *)dif->alt_name); + if (!mem_layout) { + errx(EX_IOERR, "Failed to parse memory layout"); + } + if (dfuse_unprotect) { + if (!dfuse_force) { + errx(EX_IOERR, "The read unprotect command " + "will erase the flash memory" + "and can only be used with force\n"); + } + dfuse_special_command(dif, 0, READ_UNPROTECT); + printf("Device disconnects, erases flash and resets now\n"); + exit(0); + } + if (dfuse_mass_erase) { + if (!dfuse_force) { + errx(EX_IOERR, "The mass erase command " + "can only be used with force"); + } + printf("Performing mass erase, this can take a moment\n"); + dfuse_special_command(dif, 0, MASS_ERASE); + } + if (dfuse_address) { + if (file->bcdDFU == 0x11a) { + errx(EX_IOERR, "This is a DfuSe file, not " + "meant for raw download"); + } + ret = dfuse_do_bin_dnload(dif, xfer_size, file, dfuse_address); + } else { + if (file->bcdDFU != 0x11a) { + warnx("Only DfuSe file version 1.1a is supported"); + errx(EX_IOERR, "(for raw binary download, use the " + "--dfuse-address option)"); + } + ret = dfuse_do_dfuse_dnload(dif, xfer_size, file); + } + free_segment_list(mem_layout); + + dfu_abort_to_idle(dif); + + if (dfuse_leave) { + dfuse_special_command(dif, dfuse_address, SET_ADDRESS); + dfuse_dnload_chunk(dif, NULL, 0, 2); /* Zero-size */ + } + return ret; +} diff --git a/tools/win/src/dfu-util/src/dfuse.h b/tools/win/src/dfu-util/src/dfuse.h new file mode 100644 index 0000000..ed1108c --- /dev/null +++ b/tools/win/src/dfu-util/src/dfuse.h @@ -0,0 +1,35 @@ +/* This implements the ST Microsystems DFU extensions (DfuSe) + * as per the DfuSe 1.1a specification (Document UM0391) + * + * (C) 2010-2012 Tormod Volden + * + * 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 DFUSE_H +#define DFUSE_H + +#include "dfu.h" + +enum dfuse_command { SET_ADDRESS, ERASE_PAGE, MASS_ERASE, READ_UNPROTECT }; + +int dfuse_special_command(struct dfu_if *dif, unsigned int address, + enum dfuse_command command); +int dfuse_do_upload(struct dfu_if *dif, int xfer_size, int fd, + const char *dfuse_options); +int dfuse_do_dnload(struct dfu_if *dif, int xfer_size, struct dfu_file *file, + const char *dfuse_options); + +#endif /* DFUSE_H */ diff --git a/tools/win/src/dfu-util/src/dfuse_mem.c b/tools/win/src/dfu-util/src/dfuse_mem.c new file mode 100644 index 0000000..a91aacf --- /dev/null +++ b/tools/win/src/dfu-util/src/dfuse_mem.c @@ -0,0 +1,198 @@ +/* + * Helper functions for reading the memory map of a device + * following the ST DfuSe 1.1a specification. + * + * Copyright 2011-2014 Tormod Volden + * + * 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 +#include +#include +#include + +#include "portable.h" +#include "dfu_file.h" +#include "dfuse_mem.h" + +int add_segment(struct memsegment **segment_list, struct memsegment segment) +{ + struct memsegment *new_element; + + new_element = dfu_malloc(sizeof(struct memsegment)); + *new_element = segment; + new_element->next = NULL; + + if (*segment_list == NULL) + /* list can be empty on first call */ + *segment_list = new_element; + else { + struct memsegment *next_element; + + /* find last element in list */ + next_element = *segment_list; + while (next_element->next != NULL) + next_element = next_element->next; + next_element->next = new_element; + } + return 0; +} + +struct memsegment *find_segment(struct memsegment *segment_list, + unsigned int address) +{ + while (segment_list != NULL) { + if (segment_list->start <= address && + segment_list->end >= address) + return segment_list; + segment_list = segment_list->next; + } + return NULL; +} + +void free_segment_list(struct memsegment *segment_list) +{ + struct memsegment *next_element; + + while (segment_list->next != NULL) { + next_element = segment_list->next; + free(segment_list); + segment_list = next_element; + } + free(segment_list); +} + +/* Parse memory map from interface descriptor string + * encoded as per ST document UM0424 section 4.3.2. + */ +struct memsegment *parse_memory_layout(char *intf_desc) +{ + + char multiplier, memtype; + unsigned int address; + int sectors, size; + char *name, *typestring; + int ret; + int count = 0; + char separator; + int scanned; + struct memsegment *segment_list = NULL; + struct memsegment segment; + + name = dfu_malloc(strlen(intf_desc)); + + ret = sscanf(intf_desc, "@%[^/]%n", name, &scanned); + if (ret < 1) { + free(name); + warnx("Could not read name, sscanf returned %d", ret); + return NULL; + } + printf("DfuSe interface name: \"%s\"\n", name); + + intf_desc += scanned; + typestring = dfu_malloc(strlen(intf_desc)); + + while (ret = sscanf(intf_desc, "/0x%x/%n", &address, &scanned), + ret > 0) { + + intf_desc += scanned; + while (ret = sscanf(intf_desc, "%d*%d%c%[^,/]%n", + §ors, &size, &multiplier, typestring, + &scanned), ret > 2) { + intf_desc += scanned; + + count++; + memtype = 0; + if (ret == 4) { + if (strlen(typestring) == 1 + && typestring[0] != '/') + memtype = typestring[0]; + else { + warnx("Parsing type identifier '%s' " + "failed for segment %i", + typestring, count); + continue; + } + } + + /* Quirk for STM32F4 devices */ + if (strcmp(name, "Device Feature") == 0) + memtype = 'e'; + + switch (multiplier) { + case 'B': + break; + case 'K': + size *= 1024; + break; + case 'M': + size *= 1024 * 1024; + break; + case 'a': + case 'b': + case 'c': + case 'd': + case 'e': + case 'f': + case 'g': + if (!memtype) { + warnx("Non-valid multiplier '%c', " + "interpreted as type " + "identifier instead", + multiplier); + memtype = multiplier; + break; + } + /* fallthrough if memtype was already set */ + default: + warnx("Non-valid multiplier '%c', " + "assuming bytes", multiplier); + } + + if (!memtype) { + warnx("No valid type for segment %d\n", count); + continue; + } + + segment.start = address; + segment.end = address + sectors * size - 1; + segment.pagesize = size; + segment.memtype = memtype & 7; + add_segment(&segment_list, segment); + + if (verbose) + printf("Memory segment at 0x%08x %3d x %4d = " + "%5d (%s%s%s)\n", + address, sectors, size, sectors * size, + memtype & DFUSE_READABLE ? "r" : "", + memtype & DFUSE_ERASABLE ? "e" : "", + memtype & DFUSE_WRITEABLE ? "w" : ""); + + address += sectors * size; + + separator = *intf_desc; + if (separator == ',') + intf_desc += 1; + else + break; + } /* while per segment */ + + } /* while per address */ + free(name); + free(typestring); + + return segment_list; +} diff --git a/tools/win/src/dfu-util/src/dfuse_mem.h b/tools/win/src/dfu-util/src/dfuse_mem.h new file mode 100644 index 0000000..0181f0c --- /dev/null +++ b/tools/win/src/dfu-util/src/dfuse_mem.h @@ -0,0 +1,44 @@ +/* Helper functions for reading the memory map in a device + * following the ST DfuSe 1.1a specification. + * + * (C) 2011 Tormod Volden + * + * 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 DFUSE_MEM_H +#define DFUSE_MEM_H + +#define DFUSE_READABLE 1 +#define DFUSE_ERASABLE 2 +#define DFUSE_WRITEABLE 4 + +struct memsegment { + unsigned int start; + unsigned int end; + int pagesize; + int memtype; + struct memsegment *next; +}; + +int add_segment(struct memsegment **list, struct memsegment new_element); + +struct memsegment *find_segment(struct memsegment *list, unsigned int address); + +void free_segment_list(struct memsegment *list); + +struct memsegment *parse_memory_layout(char *intf_desc_str); + +#endif /* DFUSE_MEM_H */ diff --git a/tools/win/src/dfu-util/src/main.c b/tools/win/src/dfu-util/src/main.c new file mode 100644 index 0000000..acaed2f --- /dev/null +++ b/tools/win/src/dfu-util/src/main.c @@ -0,0 +1,699 @@ +/* + * dfu-util + * + * Copyright 2007-2008 by OpenMoko, Inc. + * Copyright 2013-2014 Hans Petter Selasky + * + * Written by Harald Welte + * + * 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 +#include +#include +#include +#include +#include +#include + +#include "portable.h" +#include "dfu.h" +#include "usb_dfu.h" +#include "dfu_file.h" +#include "dfu_load.h" +#include "dfu_util.h" +#include "dfuse.h" +#include "quirks.h" + +#ifdef HAVE_USBPATH_H +#include +#endif + +int verbose = 0; + +struct dfu_if *dfu_root = NULL; + +int match_bus = -1; +int match_device = -1; +int match_vendor = -1; +int match_product = -1; +int match_vendor_dfu = -1; +int match_product_dfu = -1; +int match_config_index = -1; +int match_iface_index = -1; +int match_iface_alt_index = -1; +const char *match_iface_alt_name = NULL; +const char *match_serial = NULL; +const char *match_serial_dfu = NULL; + +static int parse_match_value(const char *str, int default_value) +{ + char *remainder; + int value; + + if (str == NULL) { + value = default_value; + } else if (*str == '*') { + value = -1; /* Match anything */ + } else if (*str == '-') { + value = 0x10000; /* Impossible vendor/product ID */ + } else { + value = strtoul(str, &remainder, 16); + if (remainder == str) { + value = default_value; + } + } + return value; +} + +static void parse_vendprod(const char *str) +{ + const char *comma; + const char *colon; + + /* Default to match any DFU device in runtime or DFU mode */ + match_vendor = -1; + match_product = -1; + match_vendor_dfu = -1; + match_product_dfu = -1; + + comma = strchr(str, ','); + if (comma == str) { + /* DFU mode vendor/product being specified without any runtime + * vendor/product specification, so don't match any runtime device */ + match_vendor = match_product = 0x10000; + } else { + colon = strchr(str, ':'); + if (colon != NULL) { + ++colon; + if ((comma != NULL) && (colon > comma)) { + colon = NULL; + } + } + match_vendor = parse_match_value(str, match_vendor); + match_product = parse_match_value(colon, match_product); + if (comma != NULL) { + /* Both runtime and DFU mode vendor/product specifications are + * available, so default DFU mode match components to the given + * runtime match components */ + match_vendor_dfu = match_vendor; + match_product_dfu = match_product; + } + } + if (comma != NULL) { + ++comma; + colon = strchr(comma, ':'); + if (colon != NULL) { + ++colon; + } + match_vendor_dfu = parse_match_value(comma, match_vendor_dfu); + match_product_dfu = parse_match_value(colon, match_product_dfu); + } +} + +static void parse_serial(char *str) +{ + char *comma; + + match_serial = str; + comma = strchr(str, ','); + if (comma == NULL) { + match_serial_dfu = match_serial; + } else { + *comma++ = 0; + match_serial_dfu = comma; + } + if (*match_serial == 0) match_serial = NULL; + if (*match_serial_dfu == 0) match_serial_dfu = NULL; +} + +#ifdef HAVE_USBPATH_H + +static int resolve_device_path(char *path) +{ + int res; + + res = usb_path2devnum(path); + if (res < 0) + return -EINVAL; + if (!res) + return 0; + + match_bus = atoi(path); + match_device = res; + + return 0; +} + +#else /* HAVE_USBPATH_H */ + +static int resolve_device_path(char *path) +{ + (void)path; /* Eliminate unused variable warning */ + errx(EX_SOFTWARE, "USB device paths are not supported by this dfu-util.\n"); +} + +#endif /* !HAVE_USBPATH_H */ + +static void help(void) +{ + fprintf(stderr, "Usage: dfu-util [options] ...\n" + " -h --help\t\t\tPrint this help message\n" + " -V --version\t\t\tPrint the version number\n" + " -v --verbose\t\t\tPrint verbose debug statements\n" + " -l --list\t\t\tList currently attached DFU capable devices\n"); + fprintf(stderr, " -e --detach\t\t\tDetach currently attached DFU capable devices\n" + " -E --detach-delay seconds\tTime to wait before reopening a device after detach\n" + " -d --device :[,:]\n" + "\t\t\t\tSpecify Vendor/Product ID(s) of DFU device\n" + " -p --path \tSpecify path to DFU device\n" + " -c --cfg \t\tSpecify the Configuration of DFU device\n" + " -i --intf \t\tSpecify the DFU Interface number\n" + " -S --serial [,]\n" + "\t\t\t\tSpecify Serial String of DFU device\n" + " -a --alt \t\tSpecify the Altsetting of the DFU Interface\n" + "\t\t\t\tby name or by number\n"); + fprintf(stderr, " -t --transfer-size \tSpecify the number of bytes per USB Transfer\n" + " -U --upload \t\tRead firmware from device into \n" + " -Z --upload-size \tSpecify the expected upload size in bytes\n" + " -D --download \t\tWrite firmware from into device\n" + " -R --reset\t\t\tIssue USB Reset signalling once we're finished\n" + " -s --dfuse-address

\tST DfuSe mode, specify target address for\n" + "\t\t\t\traw file download or upload. Not applicable for\n" + "\t\t\t\tDfuSe file (.dfu) downloads\n" + ); + exit(EX_USAGE); +} + +static void print_version(void) +{ + printf(PACKAGE_STRING "\n\n"); + printf("Copyright 2005-2009 Weston Schmidt, Harald Welte and OpenMoko Inc.\n" + "Copyright 2010-2014 Tormod Volden and Stefan Schmidt\n" + "This program is Free Software and has ABSOLUTELY NO WARRANTY\n" + "Please report bugs to " PACKAGE_BUGREPORT "\n\n"); +} + +static struct option opts[] = { + { "help", 0, 0, 'h' }, + { "version", 0, 0, 'V' }, + { "verbose", 0, 0, 'v' }, + { "list", 0, 0, 'l' }, + { "detach", 0, 0, 'e' }, + { "detach-delay", 1, 0, 'E' }, + { "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' }, + { "serial", 1, 0, 'S' }, + { "transfer-size", 1, 0, 't' }, + { "upload", 1, 0, 'U' }, + { "upload-size", 1, 0, 'Z' }, + { "download", 1, 0, 'D' }, + { "reset", 0, 0, 'R' }, + { "dfuse-address", 1, 0, 's' }, + { 0, 0, 0, 0 } +}; + +int main(int argc, char **argv) +{ + int expected_size = 0; + unsigned int transfer_size = 0; + enum mode mode = MODE_NONE; + struct dfu_status status; + libusb_context *ctx; + struct dfu_file file; + char *end; + int final_reset = 0; + int ret; + int dfuse_device = 0; + int fd; + const char *dfuse_options = NULL; + int detach_delay = 5; + uint16_t runtime_vendor; + uint16_t runtime_product; + + memset(&file, 0, sizeof(file)); + + /* make sure all prints are flushed */ + setvbuf(stdout, NULL, _IONBF, 0); + + while (1) { + int c, option_index = 0; + c = getopt_long(argc, argv, "hVvleE:d:p:c:i:a:S:t:U:D:Rs:Z:", opts, + &option_index); + if (c == -1) + break; + + switch (c) { + case 'h': + help(); + break; + case 'V': + mode = MODE_VERSION; + break; + case 'v': + verbose++; + break; + case 'l': + mode = MODE_LIST; + break; + case 'e': + mode = MODE_DETACH; + match_iface_alt_index = 0; + match_iface_index = 0; + break; + case 'E': + detach_delay = atoi(optarg); + break; + case 'd': + parse_vendprod(optarg); + break; + case 'p': + /* Parse device path */ + ret = resolve_device_path(optarg); + if (ret < 0) + errx(EX_SOFTWARE, "Unable to parse '%s'", optarg); + if (!ret) + errx(EX_SOFTWARE, "Cannot find '%s'", optarg); + break; + case 'c': + /* Configuration */ + match_config_index = atoi(optarg); + break; + case 'i': + /* Interface */ + match_iface_index = atoi(optarg); + break; + case 'a': + /* Interface Alternate Setting */ + match_iface_alt_index = strtoul(optarg, &end, 0); + if (*end) { + match_iface_alt_name = optarg; + match_iface_alt_index = -1; + } + break; + case 'S': + parse_serial(optarg); + break; + case 't': + transfer_size = atoi(optarg); + break; + case 'U': + mode = MODE_UPLOAD; + file.name = optarg; + break; + case 'Z': + expected_size = atoi(optarg); + break; + case 'D': + mode = MODE_DOWNLOAD; + file.name = optarg; + break; + case 'R': + final_reset = 1; + break; + case 's': + dfuse_options = optarg; + break; + default: + help(); + break; + } + } + + print_version(); + if (mode == MODE_VERSION) { + exit(0); + } + + if (mode == MODE_NONE) { + fprintf(stderr, "You need to specify one of -D or -U\n"); + help(); + } + + if (match_config_index == 0) { + /* Handle "-c 0" (unconfigured device) as don't care */ + match_config_index = -1; + } + + if (mode == MODE_DOWNLOAD) { + dfu_load_file(&file, MAYBE_SUFFIX, MAYBE_PREFIX); + /* If the user didn't specify product and/or vendor IDs to match, + * use any IDs from the file suffix for device matching */ + if (match_vendor < 0 && file.idVendor != 0xffff) { + match_vendor = file.idVendor; + printf("Match vendor ID from file: %04x\n", match_vendor); + } + if (match_product < 0 && file.idProduct != 0xffff) { + match_product = file.idProduct; + printf("Match product ID from file: %04x\n", match_product); + } + } + + ret = libusb_init(&ctx); + if (ret) + errx(EX_IOERR, "unable to initialize libusb: %i", ret); + + if (verbose > 2) { + libusb_set_debug(ctx, 255); + } + + probe_devices(ctx); + + if (mode == MODE_LIST) { + list_dfu_interfaces(); + exit(0); + } + + if (dfu_root == NULL) { + errx(EX_IOERR, "No DFU capable USB device available"); + } else if (dfu_root->next != NULL) { + /* 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 */ + errx(EX_IOERR, "More than one DFU capable USB device found! " + "Try `--list' and specify the serial number " + "or disconnect all but one device\n"); + } + + /* We have exactly one device. Its libusb_device is now in dfu_root->dev */ + + printf("Opening DFU capable USB device...\n"); + ret = libusb_open(dfu_root->dev, &dfu_root->dev_handle); + if (ret || !dfu_root->dev_handle) + errx(EX_IOERR, "Cannot open device"); + + printf("ID %04x:%04x\n", dfu_root->vendor, dfu_root->product); + + printf("Run-time device DFU version %04x\n", + libusb_le16_to_cpu(dfu_root->func_dfu.bcdDFUVersion)); + + /* Transition from run-Time mode to DFU mode */ + if (!(dfu_root->flags & DFU_IFF_DFU)) { + int err; + /* 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 */ + + runtime_vendor = dfu_root->vendor; + runtime_product = dfu_root->product; + + printf("Claiming USB DFU Runtime Interface...\n"); + if (libusb_claim_interface(dfu_root->dev_handle, dfu_root->interface) < 0) { + errx(EX_IOERR, "Cannot claim interface %d", + dfu_root->interface); + } + + if (libusb_set_interface_alt_setting(dfu_root->dev_handle, dfu_root->interface, 0) < 0) { + errx(EX_IOERR, "Cannot set alt interface zero"); + } + + printf("Determining device status: "); + + err = dfu_get_status(dfu_root, &status); + if (err == LIBUSB_ERROR_PIPE) { + printf("Device does not implement get_status, assuming appIDLE\n"); + status.bStatus = DFU_STATUS_OK; + status.bwPollTimeout = 0; + status.bState = DFU_STATE_appIDLE; + status.iString = 0; + } else if (err < 0) { + errx(EX_IOERR, "error get_status"); + } else { + printf("state = %s, status = %d\n", + dfu_state_to_string(status.bState), status.bStatus); + } + milli_sleep(status.bwPollTimeout); + + 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(dfu_root->dev_handle, + dfu_root->interface, 1000) < 0) { + warnx("error detaching"); + } + if (dfu_root->func_dfu.bmAttributes & USB_DFU_WILL_DETACH) { + printf("Device will detach and reattach...\n"); + } else { + printf("Resetting USB...\n"); + ret = libusb_reset_device(dfu_root->dev_handle); + if (ret < 0 && ret != LIBUSB_ERROR_NOT_FOUND) + errx(EX_IOERR, "error resetting " + "after detach"); + } + break; + case DFU_STATE_dfuERROR: + printf("dfuERROR, clearing status\n"); + if (dfu_clear_status(dfu_root->dev_handle, + dfu_root->interface) < 0) { + errx(EX_IOERR, "error clear_status"); + } + /* fall through */ + default: + warnx("WARNING: Runtime device already in DFU state ?!?"); + libusb_release_interface(dfu_root->dev_handle, + dfu_root->interface); + goto dfustate; + } + libusb_release_interface(dfu_root->dev_handle, + dfu_root->interface); + libusb_close(dfu_root->dev_handle); + dfu_root->dev_handle = NULL; + + if (mode == MODE_DETACH) { + libusb_exit(ctx); + exit(0); + } + + /* keeping handles open might prevent re-enumeration */ + disconnect_devices(); + + milli_sleep(detach_delay * 1000); + + /* Change match vendor and product to impossible values to force + * only DFU mode matches in the following probe */ + match_vendor = match_product = 0x10000; + + probe_devices(ctx); + + if (dfu_root == NULL) { + errx(EX_IOERR, "Lost device after RESET?"); + } else if (dfu_root->next != NULL) { + errx(EX_IOERR, "More than one DFU capable USB device found! " + "Try `--list' and specify the serial number " + "or disconnect all but one device"); + } + + /* Check for DFU mode device */ + if (!(dfu_root->flags | DFU_IFF_DFU)) + errx(EX_SOFTWARE, "Device is not in DFU mode"); + + printf("Opening DFU USB Device...\n"); + ret = libusb_open(dfu_root->dev, &dfu_root->dev_handle); + if (ret || !dfu_root->dev_handle) { + errx(EX_IOERR, "Cannot open device"); + } + } else { + /* we're already in DFU mode, so we can skip the detach/reset + * procedure */ + /* If a match vendor/product was specified, use that as the runtime + * vendor/product, otherwise use the DFU mode vendor/product */ + runtime_vendor = match_vendor < 0 ? dfu_root->vendor : match_vendor; + runtime_product = match_product < 0 ? dfu_root->product : match_product; + } + +dfustate: +#if 0 + printf("Setting Configuration %u...\n", dfu_root->configuration); + if (libusb_set_configuration(dfu_root->dev_handle, dfu_root->configuration) < 0) { + errx(EX_IOERR, "Cannot set configuration"); + } +#endif + printf("Claiming USB DFU Interface...\n"); + if (libusb_claim_interface(dfu_root->dev_handle, dfu_root->interface) < 0) { + errx(EX_IOERR, "Cannot claim interface"); + } + + printf("Setting Alternate Setting #%d ...\n", dfu_root->altsetting); + if (libusb_set_interface_alt_setting(dfu_root->dev_handle, dfu_root->interface, dfu_root->altsetting) < 0) { + errx(EX_IOERR, "Cannot set alternate interface"); + } + +status_again: + printf("Determining device status: "); + if (dfu_get_status(dfu_root, &status ) < 0) { + errx(EX_IOERR, "error get_status"); + } + printf("state = %s, status = %d\n", + dfu_state_to_string(status.bState), status.bStatus); + + milli_sleep(status.bwPollTimeout); + + switch (status.bState) { + case DFU_STATE_appIDLE: + case DFU_STATE_appDETACH: + errx(EX_IOERR, "Device still in Runtime Mode!"); + break; + case DFU_STATE_dfuERROR: + printf("dfuERROR, clearing status\n"); + if (dfu_clear_status(dfu_root->dev_handle, dfu_root->interface) < 0) { + errx(EX_IOERR, "error clear_status"); + } + goto status_again; + break; + case DFU_STATE_dfuDNLOAD_IDLE: + case DFU_STATE_dfuUPLOAD_IDLE: + printf("aborting previous incomplete transfer\n"); + if (dfu_abort(dfu_root->dev_handle, dfu_root->interface) < 0) { + errx(EX_IOERR, "can't send DFU_ABORT"); + } + goto status_again; + break; + case DFU_STATE_dfuIDLE: + printf("dfuIDLE, continuing\n"); + break; + default: + break; + } + + if (DFU_STATUS_OK != status.bStatus ) { + printf("WARNING: DFU Status: '%s'\n", + dfu_status_to_string(status.bStatus)); + /* Clear our status & try again. */ + if (dfu_clear_status(dfu_root->dev_handle, dfu_root->interface) < 0) + errx(EX_IOERR, "USB communication error"); + if (dfu_get_status(dfu_root, &status) < 0) + errx(EX_IOERR, "USB communication error"); + if (DFU_STATUS_OK != status.bStatus) + errx(EX_SOFTWARE, "Status is not OK: %d", status.bStatus); + + milli_sleep(status.bwPollTimeout); + } + + printf("DFU mode device DFU version %04x\n", + libusb_le16_to_cpu(dfu_root->func_dfu.bcdDFUVersion)); + + if (dfu_root->func_dfu.bcdDFUVersion == libusb_cpu_to_le16(0x11a)) + dfuse_device = 1; + + /* If not overridden by the user */ + if (!transfer_size) { + transfer_size = libusb_le16_to_cpu( + dfu_root->func_dfu.wTransferSize); + if (transfer_size) { + printf("Device returned transfer size %i\n", + transfer_size); + } else { + errx(EX_IOERR, "Transfer size must be specified"); + } + } + +#ifdef HAVE_GETPAGESIZE +/* autotools lie when cross-compiling for Windows using mingw32/64 */ +#ifndef __MINGW32__ + /* limitation of Linux usbdevio */ + if ((int)transfer_size > getpagesize()) { + transfer_size = getpagesize(); + printf("Limited transfer size to %i\n", transfer_size); + } +#endif /* __MINGW32__ */ +#endif /* HAVE_GETPAGESIZE */ + + if (transfer_size < dfu_root->bMaxPacketSize0) { + transfer_size = dfu_root->bMaxPacketSize0; + printf("Adjusted transfer size to %i\n", transfer_size); + } + + switch (mode) { + case MODE_UPLOAD: + /* open for "exclusive" writing */ + fd = open(file.name, O_WRONLY | O_BINARY | O_CREAT | O_EXCL | O_TRUNC, 0666); + if (fd < 0) + err(EX_IOERR, "Cannot open file %s for writing", file.name); + + if (dfuse_device || dfuse_options) { + if (dfuse_do_upload(dfu_root, transfer_size, fd, + dfuse_options) < 0) + exit(1); + } else { + if (dfuload_do_upload(dfu_root, transfer_size, + expected_size, fd) < 0) { + exit(1); + } + } + close(fd); + break; + + case MODE_DOWNLOAD: + if (((file.idVendor != 0xffff && file.idVendor != runtime_vendor) || + (file.idProduct != 0xffff && file.idProduct != runtime_product)) && + ((file.idVendor != 0xffff && file.idVendor != dfu_root->vendor) || + (file.idProduct != 0xffff && file.idProduct != dfu_root->product))) { + errx(EX_IOERR, "Error: File ID %04x:%04x does " + "not match device (%04x:%04x or %04x:%04x)", + file.idVendor, file.idProduct, + runtime_vendor, runtime_product, + dfu_root->vendor, dfu_root->product); + } + if (dfuse_device || dfuse_options || file.bcdDFU == 0x11a) { + if (dfuse_do_dnload(dfu_root, transfer_size, &file, + dfuse_options) < 0) + exit(1); + } else { + if (dfuload_do_dnload(dfu_root, transfer_size, &file) < 0) + exit(1); + } + break; + case MODE_DETACH: + if (dfu_detach(dfu_root->dev_handle, dfu_root->interface, 1000) < 0) { + warnx("can't detach"); + } + break; + default: + errx(EX_IOERR, "Unsupported mode: %u", mode); + break; + } + + if (final_reset) { + if (dfu_detach(dfu_root->dev_handle, dfu_root->interface, 1000) < 0) { + /* Even if detach failed, just carry on to leave the + device in a known state */ + warnx("can't detach"); + } + printf("Resetting USB to switch back to runtime mode\n"); + ret = libusb_reset_device(dfu_root->dev_handle); + if (ret < 0 && ret != LIBUSB_ERROR_NOT_FOUND) { + errx(EX_IOERR, "error resetting after download"); + } + } + + libusb_close(dfu_root->dev_handle); + dfu_root->dev_handle = NULL; + libusb_exit(ctx); + + return (0); +} diff --git a/tools/win/src/dfu-util/src/portable.h b/tools/win/src/dfu-util/src/portable.h new file mode 100644 index 0000000..cf8d5df --- /dev/null +++ b/tools/win/src/dfu-util/src/portable.h @@ -0,0 +1,72 @@ + +#ifndef PORTABLE_H +#define PORTABLE_H + +#ifdef HAVE_CONFIG_H +# include "config.h" +#else +# define PACKAGE "dfu-util" +# define PACKAGE_VERSION "0.8-msvc" +# define PACKAGE_STRING "dfu-util 0.8-msvc" +# define PACKAGE_BUGREPORT "dfu-util@lists.gnumonks.org" +#endif /* HAVE_CONFIG_H */ + +#ifdef HAVE_FTRUNCATE +# include +#else +# include +#endif /* HAVE_FTRUNCATE */ + +#ifdef HAVE_NANOSLEEP +# include +# define milli_sleep(msec) do {\ + if (msec) {\ + struct timespec nanosleepDelay = { (msec) / 1000, ((msec) % 1000) * 1000000 };\ + nanosleep(&nanosleepDelay, NULL);\ + } } while (0) +#elif defined HAVE_WINDOWS_H +# define milli_sleep(msec) do {\ + if (msec) {\ + Sleep(msec);\ + } } while (0) +#else +# error "Can't get no sleep! Please report" +#endif /* HAVE_NANOSLEEP */ + +#ifdef HAVE_ERR +# include +#else +# include +# include +# define warnx(...) do {\ + fprintf(stderr, __VA_ARGS__);\ + fprintf(stderr, "\n"); } while (0) +# define errx(eval, ...) do {\ + warnx(__VA_ARGS__);\ + exit(eval); } while (0) +# define warn(...) do {\ + fprintf(stderr, "%s: ", strerror(errno));\ + warnx(__VA_ARGS__); } while (0) +# define err(eval, ...) do {\ + warn(__VA_ARGS__);\ + exit(eval); } while (0) +#endif /* HAVE_ERR */ + +#ifdef HAVE_SYSEXITS_H +# include +#else +# define EX_OK 0 /* successful termination */ +# define EX_USAGE 64 /* command line usage error */ +# define EX_SOFTWARE 70 /* internal software error */ +# define EX_IOERR 74 /* input/output error */ +#endif /* HAVE_SYSEXITS_H */ + +#ifndef O_BINARY +# define O_BINARY 0 +#endif + +#ifndef off_t +# define off_t long int +#endif + +#endif /* PORTABLE_H */ diff --git a/tools/win/src/dfu-util/src/prefix.c b/tools/win/src/dfu-util/src/prefix.c new file mode 100644 index 0000000..be8e3fa --- /dev/null +++ b/tools/win/src/dfu-util/src/prefix.c @@ -0,0 +1,176 @@ +/* + * dfu-prefix + * + * Copyright 2011-2012 Stefan Schmidt + * Copyright 2013 Hans Petter Selasky + * Copyright 2014 Uwe Bonnes + * + * 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 +#include +#include +#include +#include + +#include "portable.h" +#include "dfu_file.h" + +enum mode { + MODE_NONE, + MODE_ADD, + MODE_DEL, + MODE_CHECK +}; + +int verbose; + +static void help(void) +{ + fprintf(stderr, "Usage: dfu-prefix [options] ...\n" + " -h --help\t\t\tPrint this help message\n" + " -V --version\t\t\tPrint the version number\n" + " -c --check \t\tCheck DFU prefix of \n" + " -D --delete \t\tDelete DFU prefix from \n" + " -a --add \t\tAdd DFU prefix to \n" + "In combination with -a:\n" + ); + fprintf(stderr, " -s --stellaris-address
Add TI Stellaris address prefix to \n" + "In combination with -D or -c:\n" + " -T --stellaris\t\tAct on TI Stellaris address prefix of \n" + "In combination with -a or -D or -c:\n" + " -L --lpc-prefix\t\tUse NXP LPC DFU prefix format\n" + ); + exit(EX_USAGE); +} + +static void print_version(void) +{ + printf("dfu-prefix (%s) %s\n\n", PACKAGE, PACKAGE_VERSION); + printf("Copyright 2011-2012 Stefan Schmidt, 2014 Uwe Bonnes\n" + "This program is Free Software and has ABSOLUTELY NO WARRANTY\n" + "Please report bugs to %s\n\n", PACKAGE_BUGREPORT); + +} + +static struct option opts[] = { + { "help", 0, 0, 'h' }, + { "version", 0, 0, 'V' }, + { "check", 1, 0, 'c' }, + { "add", 1, 0, 'a' }, + { "delete", 1, 0, 'D' }, + { "stellaris-address", 1, 0, 's' }, + { "stellaris", 0, 0, 'T' }, + { "LPC", 0, 0, 'L' }, +}; +int main(int argc, char **argv) +{ + struct dfu_file file; + enum mode mode = MODE_NONE; + enum prefix_type type = ZERO_PREFIX; + uint32_t lmdfu_flash_address = 0; + char *end; + + /* make sure all prints are flushed */ + setvbuf(stdout, NULL, _IONBF, 0); + + print_version(); + + memset(&file, 0, sizeof(file)); + + while (1) { + int c, option_index = 0; + c = getopt_long(argc, argv, "hVc:a:D:p:v:d:s:TL", opts, + &option_index); + if (c == -1) + break; + + switch (c) { + case 'h': + help(); + break; + case 'V': + exit(0); + break; + case 'D': + file.name = optarg; + mode = MODE_DEL; + break; + case 'c': + file.name = optarg; + mode = MODE_CHECK; + break; + case 'a': + file.name = optarg; + mode = MODE_ADD; + break; + case 's': + lmdfu_flash_address = strtoul(optarg, &end, 0); + if (*end) { + errx(EX_IOERR, "Invalid lmdfu " + "address: %s", optarg); + } + /* fall-through */ + case 'T': + type = LMDFU_PREFIX; + break; + case 'L': + type = LPCDFU_UNENCRYPTED_PREFIX; + break; + default: + help(); + break; + } + } + + if (!file.name) { + fprintf(stderr, "You need to specify a filename\n"); + help(); + } + + switch(mode) { + case MODE_ADD: + if (type == ZERO_PREFIX) + errx(EX_IOERR, "Prefix type must be specified"); + dfu_load_file(&file, MAYBE_SUFFIX, NO_PREFIX); + file.lmdfu_address = lmdfu_flash_address; + file.prefix_type = type; + printf("Adding prefix to file\n"); + dfu_store_file(&file, file.size.suffix != 0, 1); + break; + + case MODE_CHECK: + dfu_load_file(&file, MAYBE_SUFFIX, MAYBE_PREFIX); + show_suffix_and_prefix(&file); + if (type > ZERO_PREFIX && file.prefix_type != type) + errx(EX_IOERR, "No prefix of requested type"); + break; + + case MODE_DEL: + dfu_load_file(&file, MAYBE_SUFFIX, NEEDS_PREFIX); + if (type > ZERO_PREFIX && file.prefix_type != type) + errx(EX_IOERR, "No prefix of requested type"); + printf("Removing prefix from file\n"); + /* if there was a suffix, rewrite it */ + dfu_store_file(&file, file.size.suffix != 0, 0); + break; + + default: + help(); + break; + } + return (0); +} diff --git a/tools/win/src/dfu-util/src/quirks.c b/tools/win/src/dfu-util/src/quirks.c new file mode 100644 index 0000000..de394a6 --- /dev/null +++ b/tools/win/src/dfu-util/src/quirks.c @@ -0,0 +1,56 @@ +/* + * Simple quirk system for dfu-util + * + * Copyright 2010-2014 Tormod Volden + * + * 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 +#include "quirks.h" + +uint16_t get_quirks(uint16_t vendor, uint16_t product, uint16_t bcdDevice) +{ + uint16_t quirks = 0; + + /* Device returns bogus bwPollTimeout values */ + if ((vendor == VENDOR_OPENMOKO || vendor == VENDOR_FIC) && + product >= PRODUCT_FREERUNNER_FIRST && + product <= PRODUCT_FREERUNNER_LAST) + quirks |= QUIRK_POLLTIMEOUT; + + if (vendor == VENDOR_VOTI && + product == PRODUCT_OPENPCD) + quirks |= QUIRK_POLLTIMEOUT; + + /* Reports wrong DFU version in DFU descriptor */ + if (vendor == VENDOR_LEAFLABS && + product == PRODUCT_MAPLE3 && + bcdDevice == 0x0200) + quirks |= QUIRK_FORCE_DFU11; + + /* old devices(bcdDevice == 0) return bogus bwPollTimeout values */ + if (vendor == VENDOR_SIEMENS && + (product == PRODUCT_PXM40 || product == PRODUCT_PXM50) && + bcdDevice == 0) + quirks |= QUIRK_POLLTIMEOUT; + + /* M-Audio Transit returns bogus bwPollTimeout values */ + if (vendor == VENDOR_MIDIMAN && + product == PRODUCT_TRANSIT) + quirks |= QUIRK_POLLTIMEOUT; + + return (quirks); +} diff --git a/tools/win/src/dfu-util/src/quirks.h b/tools/win/src/dfu-util/src/quirks.h new file mode 100644 index 0000000..0e4b3ec --- /dev/null +++ b/tools/win/src/dfu-util/src/quirks.h @@ -0,0 +1,27 @@ +#ifndef DFU_QUIRKS_H +#define DFU_QUIRKS_H + +#define VENDOR_OPENMOKO 0x1d50 /* Openmoko Freerunner / GTA02 */ +#define VENDOR_FIC 0x1457 /* Openmoko Freerunner / GTA02 */ +#define VENDOR_VOTI 0x16c0 /* OpenPCD Reader */ +#define VENDOR_LEAFLABS 0x1eaf /* Maple */ +#define VENDOR_SIEMENS 0x0908 /* Siemens AG */ +#define VENDOR_MIDIMAN 0x0763 /* Midiman */ + +#define PRODUCT_FREERUNNER_FIRST 0x5117 +#define PRODUCT_FREERUNNER_LAST 0x5126 +#define PRODUCT_OPENPCD 0x076b +#define PRODUCT_MAPLE3 0x0003 /* rev 3 and 5 */ +#define PRODUCT_PXM40 0x02c4 /* Siemens AG, PXM 40 */ +#define PRODUCT_PXM50 0x02c5 /* Siemens AG, PXM 50 */ +#define PRODUCT_TRANSIT 0x2806 /* M-Audio Transit (Midiman) */ + +#define QUIRK_POLLTIMEOUT (1<<0) +#define QUIRK_FORCE_DFU11 (1<<1) + +/* Fallback value, works for OpenMoko */ +#define DEFAULT_POLLTIMEOUT 5 + +uint16_t get_quirks(uint16_t vendor, uint16_t product, uint16_t bcdDevice); + +#endif /* DFU_QUIRKS_H */ diff --git a/tools/win/src/dfu-util/src/suffix.c b/tools/win/src/dfu-util/src/suffix.c new file mode 100644 index 0000000..0df248f --- /dev/null +++ b/tools/win/src/dfu-util/src/suffix.c @@ -0,0 +1,176 @@ +/* + * dfu-suffix + * + * Copyright 2011-2012 Stefan Schmidt + * Copyright 2013 Hans Petter Selasky + * Copyright 2014 Tormod Volden + * + * 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 +#include +#include +#include +#include + +#include "portable.h" +#include "dfu_file.h" + +enum mode { + MODE_NONE, + MODE_ADD, + MODE_DEL, + MODE_CHECK +}; + +int verbose; + +static void help(void) +{ + fprintf(stderr, "Usage: dfu-suffix [options] ...\n" + " -h --help\t\t\tPrint this help message\n" + " -V --version\t\t\tPrint the version number\n" + " -c --check \t\tCheck DFU suffix of \n" + " -a --add \t\tAdd DFU suffix to \n" + " -D --delete \t\tDelete DFU suffix from \n" + " -p --pid \t\tAdd product ID into DFU suffix in \n" + " -v --vid \t\tAdd vendor ID into DFU suffix in \n" + " -d --did \t\tAdd device ID into DFU suffix in \n" + " -S --spec \t\tAdd DFU specification ID into DFU suffix in \n" + ); + exit(EX_USAGE); +} + +static void print_version(void) +{ + printf("dfu-suffix (%s) %s\n\n", PACKAGE, PACKAGE_VERSION); + printf("Copyright 2011-2012 Stefan Schmidt, 2013-2014 Tormod Volden\n" + "This program is Free Software and has ABSOLUTELY NO WARRANTY\n" + "Please report bugs to %s\n\n", PACKAGE_BUGREPORT); + +} + +static struct option opts[] = { + { "help", 0, 0, 'h' }, + { "version", 0, 0, 'V' }, + { "check", 1, 0, 'c' }, + { "add", 1, 0, 'a' }, + { "delete", 1, 0, 'D' }, + { "pid", 1, 0, 'p' }, + { "vid", 1, 0, 'v' }, + { "did", 1, 0, 'd' }, + { "spec", 1, 0, 'S' }, +}; + +int main(int argc, char **argv) +{ + struct dfu_file file; + int pid, vid, did, spec; + enum mode mode = MODE_NONE; + + /* make sure all prints are flushed */ + setvbuf(stdout, NULL, _IONBF, 0); + + print_version(); + + pid = vid = did = 0xffff; + spec = 0x0100; /* Default to bcdDFU version 1.0 */ + memset(&file, 0, sizeof(file)); + + while (1) { + int c, option_index = 0; + c = getopt_long(argc, argv, "hVc:a:D:p:v:d:S:s:T", opts, + &option_index); + if (c == -1) + break; + + switch (c) { + case 'h': + help(); + break; + case 'V': + exit(0); + break; + case 'D': + file.name = optarg; + mode = MODE_DEL; + break; + case 'p': + pid = strtol(optarg, NULL, 16); + break; + case 'v': + vid = strtol(optarg, NULL, 16); + break; + case 'd': + did = strtol(optarg, NULL, 16); + break; + case 'S': + spec = strtol(optarg, NULL, 16); + break; + case 'c': + file.name = optarg; + mode = MODE_CHECK; + break; + case 'a': + file.name = optarg; + mode = MODE_ADD; + break; + default: + help(); + break; + } + } + + if (!file.name) { + fprintf(stderr, "You need to specify a filename\n"); + help(); + } + + if (spec != 0x0100 && spec != 0x011a) { + fprintf(stderr, "Only DFU specification 0x0100 and 0x011a supported\n"); + help(); + } + + switch(mode) { + case MODE_ADD: + dfu_load_file(&file, NO_SUFFIX, MAYBE_PREFIX); + file.idVendor = vid; + file.idProduct = pid; + file.bcdDevice = did; + file.bcdDFU = spec; + /* always write suffix, rewrite prefix if there was one */ + dfu_store_file(&file, 1, file.size.prefix != 0); + printf("Suffix successfully added to file\n"); + break; + + case MODE_CHECK: + dfu_load_file(&file, NEEDS_SUFFIX, MAYBE_PREFIX); + show_suffix_and_prefix(&file); + break; + + case MODE_DEL: + dfu_load_file(&file, NEEDS_SUFFIX, MAYBE_PREFIX); + dfu_store_file(&file, 0, file.size.prefix != 0); + if (file.size.suffix) /* had a suffix */ + printf("Suffix successfully removed from file\n"); + break; + + default: + help(); + break; + } + return (0); +} diff --git a/tools/win/src/dfu-util/src/usb_dfu.h b/tools/win/src/dfu-util/src/usb_dfu.h new file mode 100644 index 0000000..660bedc --- /dev/null +++ b/tools/win/src/dfu-util/src/usb_dfu.h @@ -0,0 +1,99 @@ +#ifndef USB_DFU_H +#define USB_DFU_H +/* USB Device Firmware Update Implementation for OpenPCD + * (C) 2006 by Harald Welte + * + * 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 + * + * 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 + +#define USB_DT_DFU 0x21 + +#ifdef _MSC_VER +# pragma pack(push) +# pragma pack(1) +#endif /* _MSC_VER */ +struct usb_dfu_func_descriptor { + uint8_t bLength; + uint8_t bDescriptorType; + uint8_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) + uint16_t wDetachTimeOut; + uint16_t wTransferSize; + uint16_t bcdDFUVersion; +#ifdef _MSC_VER +}; +# pragma pack(pop) +#elif defined __GNUC__ +} __attribute__ ((packed)); +#else + #warning "No way to pack struct on this compiler? This will break!" +#endif /* _MSC_VER */ + +#define USB_DT_DFU_SIZE 9 + +#define USB_TYPE_DFU (LIBUSB_REQUEST_TYPE_CLASS|LIBUSB_RECIPIENT_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 + +/* DFU_GETSTATUS bStatus values (Section 6.1.2, DFU Rev 1.1) */ +#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 */ diff --git a/tools/win/src/dfu-util/www/build.html b/tools/win/src/dfu-util/www/build.html new file mode 100644 index 0000000..f3036e4 --- /dev/null +++ b/tools/win/src/dfu-util/www/build.html @@ -0,0 +1,147 @@ + + + + + + + Building dfu-util from source + + + + + + + + + +
+

How to build dfu-util from source

+ +

Prerequisites for building from git

+

Mac OS X

+

+First install MacPorts (and if you are on 10.6 or older, the Java Developer Package) and then run: +

+
+	sudo port install libusb-devel git-core
+
+ +

FreeBSD

+
+	sudo pkg_add -r git pkgconf
+
+ +

Ubuntu and Debian and derivatives

+
+	sudo apt-get build-dep dfu-util
+	sudo apt-get install libusb-1.0-0-dev
+
+ +

Get the source code and build it

+

+The first time you will have to clone the git repository: +

+
+	git clone git://gitorious.org/dfu-util/dfu-util.git
+	cd dfu-util
+
+

+If you later want to update to latest git version, just run this: +

+
+	make maintainer-clean
+	git pull
+
+

+To build the source: +

+
+	./autogen.sh
+	./configure  # on most systems
+	make
+
+ +

+If you are building on Mac OS X, replace the ./configure command with: +

+
+	./configure --libdir=/opt/local/lib --includedir=/opt/local/include  # on MacOSX only
+
+ +

+Your dfu-util binary will be inside the src folder. Use it from there, or install it to /usr/local/bin by running "sudo make install". +

+ +

Cross-building for Windows

+ +

+Windows binaries can be built in a MinGW +environment, on a Windows computer or cross-hosted in another OS. +To build it on a Debian or Ubuntu host, first install build dependencies: +

+
+	sudo apt-get build-dep libusb-1.0-0 dfu-util
+	sudo apt-get install mingw32
+
+ +

+The below example builds dfu-util 0.8 and libusb 1.0.19 from unpacked release +tarballs. If you instead build from git, you will have to run "./autogen.sh" +before running the "./configure" steps. +

+ +
+mkdir -p build
+cd libusb-1.0.19
+PKG_CONFIG_PATH=$PWD/../build/lib/pkgconfig ./configure \
+    --host=i586-mingw32msvc --prefix=$PWD/../build
+# WINVER workaround needed for 1.0.19 only
+make CFLAGS="-DWINVER=0x0501"
+make install
+cd ..
+
+cd dfu-util-0.8
+PKG_CONFIG_PATH=$PWD/../build/lib/pkgconfig ./configure \
+    --host=i586-mingw32msvc --prefix=$PWD/../build
+make
+make install
+cd ..
+
+The build files will now be in build/bin. +

+ +

Building on Windows using MinGW

+This assumes using release tarballs or having run ./autogen.sh on +the git sources. +
+cd libusb-1.0.19
+./configure --prefix=$HOME
+# WINVER workaround needed for 1.0.19 only
+# MKDIR_P setting should not really be needed...
+make CFLAGS="-DWINVER=0x0501" MKDIR_P="mkdir -p"
+make install
+cd ..
+
+cd dfu-util-0.8
+./configure USB_CFLAGS="-I$HOME/include/libusb-1.0" \
+            USB_LIBS="-L $HOME/lib -lusb-1.0" PKG_CONFIG=true
+make
+make install
+cd ..
+
+To link libusb statically into dfu-util.exe use instead of "make": +
+make LDFLAGS=-static
+
+The built executables (and DLL) will now be under $HOME/bin. + +

+[Back to dfu-util main page] +

+ +
+ + diff --git a/tools/win/src/dfu-util/www/dfu-util.1.html b/tools/win/src/dfu-util/www/dfu-util.1.html new file mode 100644 index 0000000..62ca40b --- /dev/null +++ b/tools/win/src/dfu-util/www/dfu-util.1.html @@ -0,0 +1,411 @@ + + +Man page of DFU-UTIL + + +

DFU-UTIL(1)

+ +  +

NAME

+ +dfu-util - Device firmware update (DFU) USB programmer +  +

SYNOPSIS

+ + +
+
+dfu-util + +-l + +[-v] + +[-d + +vid:pid[,vid:pid]] + +[-p + +path] + +[-c + +configuration] + +[-i + +interface] + +[-a + +alt-intf] + +[-S + +serial[,serial]] + + +
+dfu-util + +[-v] + +[-d + +vid:pid[,vid:pid]] + +[-p + +path] + +[-c + +configuration] + +[-i + +interface] + +[-a + +alt-intf] + +[-S + +serial[,serial]] + +[-t + +size] + +[-Z + +size] + +[-s + +address] + +[-R] + +[-D|-U + +file] + + +
+dfu-util + +[-hV] + +
+  +

DESCRIPTION

+ +dfu-util + +is a program that implements the host (computer) side of the USB DFU +(Universal Serial Bus Device Firmware Upgrade) protocol. +

+dfu-util communicates with devices that implement the device side of the +USB DFU protocol, and is often used to upgrade the firmware of such +devices. +  +

OPTIONS

+ +
+
-l, --list + +
+List the currently attached DFU capable USB devices. +
-d, --device [Run-Time VENDOR]:[Run-Time PRODUCT][,[DFU Mode VENDOR]:[DFU Mode PRODUCT]] + +
+
+Specify run-time and/or DFU mode vendor and/or product IDs of the DFU device +to work with. VENDOR and PRODUCT are hexadecimal numbers (no prefix +needed), "*" (match any), or "-" (match nothing). By default, any DFU capable +device in either run-time or DFU mode will be considered. +

+If you only have one standards-compliant DFU device attached to your computer, +this parameter is optional. However, as soon as you have multiple DFU devices +connected, dfu-util will detect this and abort, asking you to specify which +device to use. +

+If only run-time IDs are specified (e.g. "--device 1457:51ab"), then in +addition to the specified run-time IDs, any DFU mode devices will also be +considered. This is beneficial to allow a DFU capable device to be found +again after a switch to DFU mode, since the vendor and/or product ID of a +device usually changes in DFU mode. +

+If only DFU mode IDs are specified (e.g. "--device ,951:26"), then all +run-time devices will be ignored, making it easy to target a specific device in +DFU mode. +

+If both run-time and DFU mode IDs are specified (e.g. "--device +1457:51ab,:2bc"), then unspecified DFU mode components will use the run-time +value specified. +

+Examples: +

+
--device 1457:51ab,951:26 + +
+
+ +Work with a device in run-time mode with +vendor ID 0x1457 and product ID 0x51ab, or in DFU mode with vendor ID 0x0951 +and product ID 0x0026 +

+

--device 1457:51ab,:2bc + +
+
+ +Work with a device in run-time mode with vendor ID 0x1457 and product ID +0x51ab, or in DFU mode with vendor ID 0x1457 and product ID 0x02bc +

+

--device 1457:51ab + +
+
+ +Work with a device in run-time mode with vendor ID 0x1457 and product ID +0x51ab, or in DFU mode with any vendor and product ID +

+

--device ,951:26 + +
+
+ +Work with a device in DFU mode with vendor ID 0x0951 and product ID 0x0026 +

+

--device *,- + +
+
+ +Work with any device in run-time mode, and ignore any device in DFU mode +

+

--device , + +
+
+ +Ignore any device in run-time mode, and Work with any device in DFU mode +
+
+ +
-p, --path BUS-PORT. ... .PORT + +
+Specify the path to the DFU device. +
-c, --cfg CONFIG-NR + +
+Specify the configuration of the DFU device. Note that this is only used for matching, the configuration is not set by dfu-util. +
-i, --intf INTF-NR + +
+Specify the DFU interface number. +
-a, --alt ALT + +
+Specify the altsetting of the DFU interface by name or by number. +
-S, --serial [Run-Time SERIAL][,[DFU Mode SERIAL]] + +
+Specify the run-time and DFU mode serial numbers used to further restrict +device matches. If multiple, identical DFU devices are simultaneously +connected to a system then vendor and product ID will be insufficient for +targeting a single device. In this situation, it may be possible to use this +parameter to specify a serial number which also must match. +

+If only a single serial number is specified, then the same serial number is +used in both run-time and DFU mode. An empty serial number will match any +serial number in the corresponding mode. +

-t, --transfer-size SIZE + +
+Specify the number of bytes per USB transfer. The optimal value is +usually determined automatically so this option is rarely useful. If +you need to use this option for a device, please report it as a bug. +
-Z, --upload-size SIZE + +
+Specify the expected upload size, in bytes. +
-U, --upload FILE + +
+Read firmware from device into +FILE. + +
-D, --download FILE + +
+Write firmware from +FILE + +into device. +
-R, --reset + +
+Issue USB reset signalling after upload or download has finished. +
-s, --dfuse-address address + +
+Specify target address for raw binary download/upload on DfuSe devices. Do +not + +use this for downloading DfuSe (.dfu) files. Modifiers can be added +to the address, separated by a colon, to perform special DfuSE commands such +as "leave" DFU mode, "unprotect" and "mass-erase" flash memory. +
-v, --verbose + +
+Print more information about dfu-util's operation. A second +-v + +will turn on verbose logging of USB requests. Repeat this option to further +increase verbosity. +
-h, --help + +
+Show a help text and exit. +
-V, --version + +
+Show version information and exit. +
+  +

EXAMPLES

+ +  +

Using dfu-util in the OpenMoko project

+ +(with the Neo1973 hardware) +

+ +Flashing the rootfs: +
+ + $ dfu-util -a rootfs -R -D /path/to/openmoko-devel-image.jffs2 + +

+ +Flashing the kernel: +
+ + $ dfu-util -a kernel -R -D /path/to/uImage + +

+ +Flashing the bootloader: +
+ + $ dfu-util -a u-boot -R -D /path/to/u-boot.bin + +

+ +Copying a kernel into RAM: +
+ + $ dfu-util -a 0 -R -D /path/to/uImage + +

+Once this has finished, the kernel will be available at the default load +address of 0x32000000 in Neo1973 RAM. +Note: + +You cannot transfer more than 2MB of data into RAM using this method. +

+  +

Using dfu-util with a DfuSe device

+ +

+ +Flashing a +.dfu + +(special DfuSe format) file to the device: +
+ + $ dfu-util -a 0 -D /path/to/dfuse-image.dfu + +

+ +Reading out 1 KB of flash starting at address 0x8000000: +
+ + $ dfu-util -a 0 -s 0x08000000:1024 -U newfile.bin + +

+ +Flashing a binary file to address 0x8004000 of device memory and +ask the device to leave DFU mode: +
+ + $ dfu-util -a 0 -s 0x08004000:leave -D /path/to/image.bin + + +  +

BUGS

+ +Please report any bugs to the dfu-util mailing list at +dfu-util@lists.gnumonks.org. + +Please use the +--verbose option (repeated as necessary) to provide more + +information in your bug report. +  +

SEE ALSO

+ +The dfu-util home page is +http://dfu-util.gnumonks.org + +  +

HISTORY

+ +dfu-util was originally written for the OpenMoko project by +Weston Schmidt <weston_schmidt@yahoo.com> and +Harald Welte <hwelte@hmw-consulting.de>. Over time, nearly complete +support of DFU 1.0, DFU 1.1 and DfuSe ("1.1a") has been added. +  +

LICENCE

+ +dfu-util + +is covered by the GNU General Public License (GPL), version 2 or later. +  +

COPYRIGHT

+ +This manual page was originally written by Uwe Hermann <uwe@hermann-uwe.de>, +and is now part of the dfu-util project. +

+ +


+ 

Index

+
+
NAME
+
SYNOPSIS
+
DESCRIPTION
+
OPTIONS
+
EXAMPLES
+
+
Using dfu-util in the OpenMoko project
+
Using dfu-util with a DfuSe device
+
+
BUGS
+
SEE ALSO
+
HISTORY
+
LICENCE
+
COPYRIGHT
+
+
+This document was created by man2html, +using the doc/dfu-util.1 manual page from dfu-util 0.8.
+Time: 14:40:57 GMT, September 13, 2014 + + diff --git a/tools/win/src/dfu-util/www/dfuse.html b/tools/win/src/dfu-util/www/dfuse.html new file mode 100644 index 0000000..35e4ffa --- /dev/null +++ b/tools/win/src/dfu-util/www/dfuse.html @@ -0,0 +1,135 @@ + + + + + + DfuSe and dfu-util + + + + + + + + + +
+

Using dfu-util with DfuSe devices

+

DfuSe

+

+ DfuSe (DFU with ST Microsystems extensions) is a protocol based on + DFU 1.1. However, in expanding the functionality of the DFU protocol, + ST Microsystems broke all compatibility with the DFU 1.1 standard. + DfuSe devices report the DFU version as "1.1a". +

+

+ DfuSe can be used to download firmware and other data + from a host computer to a conforming device (or upload in the + opposite direction) over USB similar to standard DFU. +

+

+ The main difference from standard DFU is that the target address in + the device (flash) memory is specified by the host, so that a + download can be performed to parts of the device memory. The host + program is also responsible for erasing flash pages before they + are written to. +

+

.dfu files

+

+ A special file format is defined by ST Microsystems to carry firmware + for DfuSe devices. The file contains target information such as address + and alternate interface information in addition to the binary data. + Several blocks of binary data can be combined in one .dfu file. +

+

Alternate interfaces

+

+ Different memory locations of the device may have different + characteristics that the host program (dfu-util) has to take + into considerations, such as flash memory page size, read-only + versus read-write segments, the need to erase, and so on. + These parameters are reported by the device in the string + descriptors meant for describing the USB interfaces. + The host program decodes these strings to build a memory map of + the device. Different memory units or address spaces are listed + in separate alternate interface settings that must be selected + according to the memory unit to access. +

+

+ Note that dfu-util leaves it to the user to select alternate + interface. When parsing a .dfu file it will skip file segments + not matching the selected alternate interface. Also, some + DfuSe device firmware implementations ignore the setting of + alternate interface and deduct the memory unit from the + address, since they have no address space overlap. +

+

DfuSe special commands

+

+ DfuSe special commands are used by the host program during normal + downloads or uploads, such as SET_ADDRESS and ERASE_PAGE. Also + the normal DFU_DNLOAD and DFU_UPLOAD commands have special + implementations in DfuSe. + Many DfuSe devices also support commands to leave DFU mode, + read unprotect the flash memory or mass erase the flash memory. + dfu-util (from version 0.7) + supports adding "leave", "unprotect", or "mass-erase" + to the -s option argument to send such requests in combination + with a download request. These modifiers are separated with a colon. +

+

+ Some DfuSe devices have their DfuSe bootloader running from flash + memory. Erasing the whole flash memory would therefore destroy + the DfuSe bootloader itself and practically brick the device + for most users. Any use of modifiers such as "unprotect" + and "mass-erase" therefore needs to be combined with the "force" + modifer. This is not included in the examples, to not encourage + ignorant users to copy and paste such instructions and shoot + themselves in the foot. +

+

+ Devices based on for instance STM32F103 all run the bootloader + from flash, since there is no USB bootloader in ROM. +

+

+ For instance STM32F107, STM32F2xx and STM32F4xx devices have a + DfuSe bootloader in ROM, so the flash can be erased while + keeping the device available for USB DFU transfers as long + as the device designers use this built-in bootloader and have + not implemented another DfuSe bootloader in flash that the user is + dependent upon. +

+

+ Well-written bootloaders running from flash will report their + own memory region as read-only and not eraseable, but this does + not prevent dfu-util from sending a "unprotect" or "mass-erase" + request which overrides this, if the user insists. +

+

Example usage

+

+ Flashing a .dfu (special DfuSe format) file to the device: +

+
+         $ dfu-util -a 0 -D /path/to/dfuse-image.dfu
+	
+

+ Reading out 1 KB of flash starting at address 0x8000000: +

+
+         $ dfu-util -a 0 -s 0x08000000:1024 -U newfile.bin
+	
+

+ Flashing a binary file to address 0x8004000 of device memory and ask + the device to leave DFU mode: +

+
+         $ dfu-util -a 0 -s 0x08004000:leave -D /path/to/image.bin
+	
+

+ [Back to dfu-util main page] +

+ +
+ + diff --git a/tools/win/src/dfu-util/www/index.html b/tools/win/src/dfu-util/www/index.html new file mode 100644 index 0000000..108ddaf --- /dev/null +++ b/tools/win/src/dfu-util/www/index.html @@ -0,0 +1,119 @@ + + + + + + dfu-util Homepage + + + + + + + + + +
+

dfu-util - Device Firmware Upgrade Utilities

+

Description

+

+ dfu-util is a host side implementation of the DFU 1.0 and DFU 1.1 specifications of the USB forum. + + DFU is intended to download and upload firmware to/from devices connected + over USB. It ranges from small devices like micro-controller boards + to mobile phones. Using dfu-util you can download firmware to your + DFU-enabled device or upload firmware from it. dfu-util has been + tested with the Openmoko Neo1973 and Freerunner and many other devices. +

+

+ See the manual page for examples of use. +

+

Supported Devices

+ +

Releases

+

+ Releases of the dfu-util software can be found in the + releases folder. + The current release is 0.8. +

+

+ We offer binaries for Microsoft Windows and some other platforms. + dfu-util uses libusb 1.0 to access your device, so + on Windows you have to register the device with the WinUSB driver + (alternatively libusb-win32 or libusbK), please see the libusbx wiki + for more details. +

+

+ Mac OS X users can also get dfu-util from Homebrew with "brew install dfu-util" or from MacPorts. +

+

+ Most Linux distributions ship dfu-util in binary packages for those + who do not want to compile dfu-util from source. + On Debian, Ubuntu, Fedora and Gentoo you can install it through the + normal software package tools. For other distributions +(namely OpenSuSe, Mandriva, and CentOS) Holger Freyther was kind enough to +provide binary packages through the Open Build Service. +

+

Development

+

+ Development happens in a GIT repository. Browse it via the web +interface or clone it with: +

+
+	git clone git://gitorious.org/dfu-util/dfu-util.git
+	
+

+ See our build instructions for how to + build the source on different platforms. +

+

License

+

+ This software is licensed under the GPL version 2. +

+

Contact

+

+ If you have questions about the development or use of dfu-util please + send an e-mail to our dedicated +mailing list for dfu-util. +

+

People

+

+ dfu-util was originally written by + Harald Welte partially based on code from + dfu-programmer 0.4 and is currently maintained by Stefan Schmidt and + Tormod Volden. +

+ +
+ + diff --git a/tools/win/src/dfu-util/www/simple.css b/tools/win/src/dfu-util/www/simple.css new file mode 100644 index 0000000..98100bc --- /dev/null +++ b/tools/win/src/dfu-util/www/simple.css @@ -0,0 +1,56 @@ +body { + margin: 10px; + font-size: 0.82em; + background-color: #EEE; +} + +h1 { + clear: both; + padding: 0 0 12px 0; + margin: 0; + font-size: 2em; + font-weight: bold; +} + +h2 { + clear: both; + margin: 0; + font-size: 1.5em; + font-weight: normal; +} + +h3 { + clear: both; + margin: 15px 0 0 0; + font-size: 1.0em; + font-weight: bold; +} + +p { + line-height: 20px; + padding: 8px 0 8px 0; + margin: 0; + font-size: 1.1em; +} + +pre { + white-space: pre-wrap; + background-color: #CCC; + padding: 3px; +} + +a:hover { + background-color: #DDD; +} + +#middlebox { + width: 600px; + margin: 0px auto; + text-align: left; +} + +#footer { + height: 100px; + padding: 28px 3px 0 0; + margin: 20px 0 20px 0; +} diff --git a/tools/win/src/maple_loader/README.md b/tools/win/src/maple_loader/README.md new file mode 100644 index 0000000..c6c9379 --- /dev/null +++ b/tools/win/src/maple_loader/README.md @@ -0,0 +1,5 @@ +These files build the maple_loader.jar file used on Windows to reset the Sketch via USB Serial, so that the bootloader will run in dfu upload mode, ready for a new sketch to be uploaded + +The files were written by @bobC (github) and have been slightly modified by me (Roger Clark), so that dfu-util no longer attempts to reset the board after upload. +This change to dfu-util's reset command line argument, was required because dfu-util was showing errors on some Windows systems, because the bootloader had reset its self after upload, +before dfu-util had chance to tell it to reset. \ No newline at end of file diff --git a/tools/win/src/maple_loader/build.xml b/tools/win/src/maple_loader/build.xml new file mode 100644 index 0000000..80bdd6f --- /dev/null +++ b/tools/win/src/maple_loader/build.xml @@ -0,0 +1,73 @@ + + + + + + + + + + + Builds, tests, and runs the project maple_loader. + + + diff --git a/tools/win/src/maple_loader/build/built-jar.properties b/tools/win/src/maple_loader/build/built-jar.properties new file mode 100644 index 0000000..10752d5 --- /dev/null +++ b/tools/win/src/maple_loader/build/built-jar.properties @@ -0,0 +1,4 @@ +#Mon, 20 Jul 2015 11:21:26 +1000 + + +C\:\\Users\\rclark\\Desktop\\maple-asp-master\\installer\\maple_loader= diff --git a/tools/win/src/maple_loader/build/classes/CliTemplate/CliMain.class b/tools/win/src/maple_loader/build/classes/CliTemplate/CliMain.class new file mode 100644 index 0000000..37ee630 Binary files /dev/null and b/tools/win/src/maple_loader/build/classes/CliTemplate/CliMain.class differ diff --git a/tools/win/src/maple_loader/build/classes/CliTemplate/DFUUploader.class b/tools/win/src/maple_loader/build/classes/CliTemplate/DFUUploader.class new file mode 100644 index 0000000..77087b0 Binary files /dev/null and b/tools/win/src/maple_loader/build/classes/CliTemplate/DFUUploader.class differ diff --git a/tools/win/src/maple_loader/build/classes/CliTemplate/ExecCommand.class b/tools/win/src/maple_loader/build/classes/CliTemplate/ExecCommand.class new file mode 100644 index 0000000..ad95f79 Binary files /dev/null and b/tools/win/src/maple_loader/build/classes/CliTemplate/ExecCommand.class differ diff --git a/tools/win/src/maple_loader/build/classes/processing/app/Base.class b/tools/win/src/maple_loader/build/classes/processing/app/Base.class new file mode 100644 index 0000000..4aa0bde Binary files /dev/null and b/tools/win/src/maple_loader/build/classes/processing/app/Base.class differ diff --git a/tools/win/src/maple_loader/build/classes/processing/app/Preferences.class b/tools/win/src/maple_loader/build/classes/processing/app/Preferences.class new file mode 100644 index 0000000..89cf010 Binary files /dev/null and b/tools/win/src/maple_loader/build/classes/processing/app/Preferences.class differ diff --git a/tools/win/src/maple_loader/build/classes/processing/app/Serial.class b/tools/win/src/maple_loader/build/classes/processing/app/Serial.class new file mode 100644 index 0000000..cceccdd Binary files /dev/null and b/tools/win/src/maple_loader/build/classes/processing/app/Serial.class differ diff --git a/tools/win/src/maple_loader/build/classes/processing/app/SerialException.class b/tools/win/src/maple_loader/build/classes/processing/app/SerialException.class new file mode 100644 index 0000000..71048dd Binary files /dev/null and b/tools/win/src/maple_loader/build/classes/processing/app/SerialException.class differ diff --git a/tools/win/src/maple_loader/build/classes/processing/app/debug/MessageConsumer.class b/tools/win/src/maple_loader/build/classes/processing/app/debug/MessageConsumer.class new file mode 100644 index 0000000..37250e7 Binary files /dev/null and b/tools/win/src/maple_loader/build/classes/processing/app/debug/MessageConsumer.class differ diff --git a/tools/win/src/maple_loader/build/classes/processing/app/debug/MessageSiphon.class b/tools/win/src/maple_loader/build/classes/processing/app/debug/MessageSiphon.class new file mode 100644 index 0000000..e22c8d4 Binary files /dev/null and b/tools/win/src/maple_loader/build/classes/processing/app/debug/MessageSiphon.class differ diff --git a/tools/win/src/maple_loader/build/classes/processing/app/debug/RunnerException.class b/tools/win/src/maple_loader/build/classes/processing/app/debug/RunnerException.class new file mode 100644 index 0000000..710f796 Binary files /dev/null and b/tools/win/src/maple_loader/build/classes/processing/app/debug/RunnerException.class differ diff --git a/tools/win/src/maple_loader/build/classes/processing/app/helpers/ProcessUtils.class b/tools/win/src/maple_loader/build/classes/processing/app/helpers/ProcessUtils.class new file mode 100644 index 0000000..27eca62 Binary files /dev/null and b/tools/win/src/maple_loader/build/classes/processing/app/helpers/ProcessUtils.class differ diff --git a/tools/win/src/maple_loader/dist/README.TXT b/tools/win/src/maple_loader/dist/README.TXT new file mode 100644 index 0000000..255b89c --- /dev/null +++ b/tools/win/src/maple_loader/dist/README.TXT @@ -0,0 +1,32 @@ +======================== +BUILD OUTPUT DESCRIPTION +======================== + +When you build an Java application project that has a main class, the IDE +automatically copies all of the JAR +files on the projects classpath to your projects dist/lib folder. The IDE +also adds each of the JAR files to the Class-Path element in the application +JAR files manifest file (MANIFEST.MF). + +To run the project from the command line, go to the dist folder and +type the following: + +java -jar "maple_loader.jar" + +To distribute this project, zip up the dist folder (including the lib folder) +and distribute the ZIP file. + +Notes: + +* If two JAR files on the project classpath have the same name, only the first +JAR file is copied to the lib folder. +* Only JAR files are copied to the lib folder. +If the classpath contains other types of files or folders, these files (folders) +are not copied. +* If a library on the projects classpath also has a Class-Path element +specified in the manifest,the content of the Class-Path element has to be on +the projects runtime path. +* To set a main class in a standard Java project, right-click the project node +in the Projects window and choose Properties. Then click Run and enter the +class name in the Main Class field. Alternatively, you can manually type the +class name in the manifest Main-Class element. diff --git a/tools/win/src/maple_loader/dist/lib/jssc.jar b/tools/win/src/maple_loader/dist/lib/jssc.jar new file mode 100644 index 0000000..eb74f15 Binary files /dev/null and b/tools/win/src/maple_loader/dist/lib/jssc.jar differ diff --git a/tools/win/src/maple_loader/dist/maple_loader.jar b/tools/win/src/maple_loader/dist/maple_loader.jar new file mode 100644 index 0000000..e1f9965 Binary files /dev/null and b/tools/win/src/maple_loader/dist/maple_loader.jar differ diff --git a/tools/win/src/maple_loader/jars/jssc.jar b/tools/win/src/maple_loader/jars/jssc.jar new file mode 100644 index 0000000..eb74f15 Binary files /dev/null and b/tools/win/src/maple_loader/jars/jssc.jar differ diff --git a/tools/win/src/maple_loader/manifest.mf b/tools/win/src/maple_loader/manifest.mf new file mode 100644 index 0000000..328e8e5 --- /dev/null +++ b/tools/win/src/maple_loader/manifest.mf @@ -0,0 +1,3 @@ +Manifest-Version: 1.0 +X-COMMENT: Main-Class will be added automatically by build + diff --git a/tools/win/src/maple_loader/nbproject/build-impl.xml b/tools/win/src/maple_loader/nbproject/build-impl.xml new file mode 100644 index 0000000..a66f349 --- /dev/null +++ b/tools/win/src/maple_loader/nbproject/build-impl.xml @@ -0,0 +1,1413 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Must set src.dir + Must set test.src.dir + Must set build.dir + Must set dist.dir + Must set build.classes.dir + Must set dist.javadoc.dir + Must set build.test.classes.dir + Must set build.test.results.dir + Must set build.classes.excludes + Must set dist.jar + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Must set javac.includes + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + No tests executed. + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Must set JVM to use for profiling in profiler.info.jvm + Must set profiler agent JVM arguments in profiler.info.jvmargs.agent + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Must select some files in the IDE or set javac.includes + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + To run this application from the command line without Ant, try: + + java -jar "${dist.jar.resolved}" + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Must select one file in the IDE or set run.class + + + + Must select one file in the IDE or set run.class + + + + + + + + + + + + + + + + + + + + + + + Must select one file in the IDE or set debug.class + + + + + Must select one file in the IDE or set debug.class + + + + + Must set fix.includes + + + + + + + + + + This target only works when run from inside the NetBeans IDE. + + + + + + + + + Must select one file in the IDE or set profile.class + This target only works when run from inside the NetBeans IDE. + + + + + + + + + This target only works when run from inside the NetBeans IDE. + + + + + + + + + + + + + This target only works when run from inside the NetBeans IDE. + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Must select one file in the IDE or set run.class + + + + + + Must select some files in the IDE or set test.includes + + + + + Must select one file in the IDE or set run.class + + + + + Must select one file in the IDE or set applet.url + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Must select some files in the IDE or set javac.includes + + + + + + + + + + + + + + + + + + + + Some tests failed; see details above. + + + + + + + + + Must select some files in the IDE or set test.includes + + + + Some tests failed; see details above. + + + + Must select some files in the IDE or set test.class + Must select some method in the IDE or set test.method + + + + Some tests failed; see details above. + + + + + Must select one file in the IDE or set test.class + + + + Must select one file in the IDE or set test.class + Must select some method in the IDE or set test.method + + + + + + + + + + + + + + Must select one file in the IDE or set applet.url + + + + + + + + + Must select one file in the IDE or set applet.url + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/tools/win/src/maple_loader/nbproject/genfiles.properties b/tools/win/src/maple_loader/nbproject/genfiles.properties new file mode 100644 index 0000000..c136721 --- /dev/null +++ b/tools/win/src/maple_loader/nbproject/genfiles.properties @@ -0,0 +1,8 @@ +build.xml.data.CRC32=2e6a03ba +build.xml.script.CRC32=4676ee6b +build.xml.stylesheet.CRC32=8064a381@1.75.2.48 +# This file is used by a NetBeans-based IDE to track changes in generated files such as build-impl.xml. +# Do not edit this file. You may delete it but then the IDE will never regenerate such files for you. +nbproject/build-impl.xml.data.CRC32=2e6a03ba +nbproject/build-impl.xml.script.CRC32=392b3f79 +nbproject/build-impl.xml.stylesheet.CRC32=876e7a8f@1.75.2.48 diff --git a/tools/win/src/maple_loader/nbproject/private/config.properties b/tools/win/src/maple_loader/nbproject/private/config.properties new file mode 100644 index 0000000..e69de29 diff --git a/tools/win/src/maple_loader/nbproject/private/private.properties b/tools/win/src/maple_loader/nbproject/private/private.properties new file mode 100644 index 0000000..e5c9f10 --- /dev/null +++ b/tools/win/src/maple_loader/nbproject/private/private.properties @@ -0,0 +1,6 @@ +compile.on.save=true +do.depend=false +do.jar=true +javac.debug=true +javadoc.preview=true +user.properties.file=C:\\Users\\rclark\\AppData\\Roaming\\NetBeans\\8.0.2\\build.properties diff --git a/tools/win/src/maple_loader/nbproject/private/private.xml b/tools/win/src/maple_loader/nbproject/private/private.xml new file mode 100644 index 0000000..a1bbd60 --- /dev/null +++ b/tools/win/src/maple_loader/nbproject/private/private.xml @@ -0,0 +1,10 @@ + + + + + + file:/C:/Users/rclark/Desktop/maple-asp-master/installer/maple_loader/src/CliTemplate/CliMain.java + file:/C:/Users/rclark/Desktop/maple-asp-master/installer/maple_loader/src/CliTemplate/DFUUploader.java + + + diff --git a/tools/win/src/maple_loader/nbproject/project.properties b/tools/win/src/maple_loader/nbproject/project.properties new file mode 100644 index 0000000..7f48d71 --- /dev/null +++ b/tools/win/src/maple_loader/nbproject/project.properties @@ -0,0 +1,79 @@ +annotation.processing.enabled=true +annotation.processing.enabled.in.editor=false +annotation.processing.processors.list= +annotation.processing.run.all.processors=true +annotation.processing.source.output=${build.generated.sources.dir}/ap-source-output +application.title=maple_loader +application.vendor=bob +build.classes.dir=${build.dir}/classes +build.classes.excludes=**/*.java,**/*.form +# This directory is removed when the project is cleaned: +build.dir=build +build.generated.dir=${build.dir}/generated +build.generated.sources.dir=${build.dir}/generated-sources +# Only compile against the classpath explicitly listed here: +build.sysclasspath=ignore +build.test.classes.dir=${build.dir}/test/classes +build.test.results.dir=${build.dir}/test/results +# Uncomment to specify the preferred debugger connection transport: +#debug.transport=dt_socket +debug.classpath=\ + ${run.classpath} +debug.test.classpath=\ + ${run.test.classpath} +# Files in build.classes.dir which should be excluded from distribution jar +dist.archive.excludes= +# This directory is removed when the project is cleaned: +dist.dir=dist +dist.jar=${dist.dir}/maple_loader.jar +dist.javadoc.dir=${dist.dir}/javadoc +endorsed.classpath= +excludes= +file.reference.jssc.jar=dist/lib/jssc.jar +file.reference.jssc.jar-1=jars/jssc.jar +includes=** +jar.compress=false +javac.classpath=\ + ${file.reference.jssc.jar}:\ + ${file.reference.jssc.jar-1} +# Space-separated list of extra javac options +javac.compilerargs= +javac.deprecation=false +javac.processorpath=\ + ${javac.classpath} +javac.source=1.7 +javac.target=1.7 +javac.test.classpath=\ + ${javac.classpath}:\ + ${build.classes.dir} +javac.test.processorpath=\ + ${javac.test.classpath} +javadoc.additionalparam= +javadoc.author=false +javadoc.encoding=${source.encoding} +javadoc.noindex=false +javadoc.nonavbar=false +javadoc.notree=false +javadoc.private=false +javadoc.splitindex=true +javadoc.use=true +javadoc.version=false +javadoc.windowtitle= +main.class=CliTemplate.CliMain +manifest.file=manifest.mf +meta.inf.dir=${src.dir}/META-INF +mkdist.disabled=false +platform.active=default_platform +run.classpath=\ + ${javac.classpath}:\ + ${build.classes.dir} +# Space-separated list of JVM arguments used when running the project. +# You may also define separate properties like run-sys-prop.name=value instead of -Dname=value. +# To set system properties for unit tests define test-sys-prop.name=value: +run.jvmargs= +run.test.classpath=\ + ${javac.test.classpath}:\ + ${build.test.classes.dir} +source.encoding=UTF-8 +src.dir=src +test.src.dir=test diff --git a/tools/win/src/maple_loader/nbproject/project.xml b/tools/win/src/maple_loader/nbproject/project.xml new file mode 100644 index 0000000..92218a9 --- /dev/null +++ b/tools/win/src/maple_loader/nbproject/project.xml @@ -0,0 +1,15 @@ + + + org.netbeans.modules.java.j2seproject + + + maple_loader + + + + + + + + + diff --git a/tools/win/src/maple_loader/src/CliTemplate/CliMain.java b/tools/win/src/maple_loader/src/CliTemplate/CliMain.java new file mode 100644 index 0000000..c7dc9f0 --- /dev/null +++ b/tools/win/src/maple_loader/src/CliTemplate/CliMain.java @@ -0,0 +1,60 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +package CliTemplate; + +import java.io.IOException; +import java.text.MessageFormat; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import processing.app.Preferences; + +//import processing.app.I18n; +import processing.app.helpers.ProcessUtils; + +/** + * + * @author cousinr + */ +public class CliMain { + + + /** + * @param args the command line arguments + */ + public static void main(String[] args) { + + String comPort = args[0]; // + String altIf = args[1]; // + String usbID = args[2]; // "1EAF:0003"; + String binFile = args[3]; // bin file + + System.out.println("maple_loader v0.1"); + + Preferences.set ("serial.port",comPort); + Preferences.set ("serial.parity","N"); + Preferences.setInteger ("serial.databits", 8); + Preferences.setInteger ("serial.debug_rate",9600); + Preferences.setInteger ("serial.stopbits",1); + + Preferences.setInteger ("programDelay",1200); + + Preferences.set ("upload.usbID", usbID); + Preferences.set ("upload.altID", altIf); + Preferences.setBoolean ("upload.auto_reset", true); + Preferences.setBoolean ("upload.verbose", false); + + // + DFUUploader dfuUploader = new DFUUploader(); + try { + //dfuUploader.uploadViaDFU(binFile); + dfuUploader.uploadViaDFU(binFile); + } catch (Exception e) + { + System.err.print (MessageFormat.format("an error occurred! {0}\n", e.getMessage())); + } + } +} diff --git a/tools/win/src/maple_loader/src/CliTemplate/DFUUploader.java b/tools/win/src/maple_loader/src/CliTemplate/DFUUploader.java new file mode 100644 index 0000000..3dee0b4 --- /dev/null +++ b/tools/win/src/maple_loader/src/CliTemplate/DFUUploader.java @@ -0,0 +1,345 @@ +/* + DFUUploader - uploader implementation using DFU + + Copyright (c) 2010 + Andrew Meyer + + 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 + +*/ + +package CliTemplate; + +import java.io.BufferedReader; +import java.io.File; +import java.io.InputStreamReader; +import java.util.ArrayList; +import java.util.Collection; +import java.util.List; +import processing.app.Preferences; +import processing.app.Serial; +import processing.app.debug.MessageConsumer; +import processing.app.debug.MessageSiphon; +import processing.app.debug.RunnerException; + +/** + * + * @author bob + */ +public class DFUUploader implements MessageConsumer { + + boolean firstErrorFound; + boolean secondErrorFound; + // part of the PdeMessageConsumer interface + boolean notFoundError; + boolean verbose; + RunnerException exception; + + static final String SUPER_BADNESS = + "Compiler error!"; + + public boolean uploadUsingPreferences(String binPath, boolean verbose) + throws RunnerException { + + this.verbose = verbose; + + return uploadViaDFU(binPath); + } + + // works with old and new versions of dfu-util + private boolean found_device (String dfuData, String usbID) + { + return dfuData.contains(("Found DFU: [0x"+usbID.substring(0,4)).toUpperCase()) || + dfuData.contains(("Found DFU: ["+usbID.substring(0,4)).toUpperCase()); + } + + public boolean uploadViaDFU (String binPath) + throws RunnerException { + + this.verbose = Preferences.getBoolean ("upload.verbose"); + + /* todo, check for size overruns! */ + String fileType="bin"; + + if (fileType.equals("bin")) { + String usbID = Preferences.get("upload.usbID"); + if (usbID == null) { + /* fall back on default */ + /* this isnt great because is default Avrdude or dfu-util? */ + usbID = Preferences.get("upload.usbID"); + } + + /* if auto-reset, then emit the reset pulse on dtr/rts */ + if (Preferences.get("upload.auto_reset") != null) { + if (Preferences.get("upload.auto_reset").toLowerCase().equals("true")) { + System.out.println("Resetting to bootloader via DTR pulse"); + emitResetPulse(); + } + } else { + System.out.println("Resetting to bootloader via DTR pulse"); + emitResetPulse(); + } + + String dfuList = new String(); + List commandCheck = new ArrayList(); + commandCheck.add("dfu-util"); + commandCheck.add("-l"); + long startChecking = System.currentTimeMillis(); + System.out.println("Searching for DFU device [" + usbID + "]..."); + do { + try { + Thread.sleep(100); + } catch (InterruptedException e) {} + dfuList = executeCheckCommand(commandCheck); + //System.out.println(dfuList); + } + while ( !found_device (dfuList.toUpperCase(), usbID) && (System.currentTimeMillis() - startChecking < 7000)); + + if ( !found_device (dfuList.toUpperCase(), usbID) ) + { + System.out.println(dfuList); + System.err.println("Couldn't find the DFU device: [" + usbID + "]"); + return false; + } + System.out.println("Found it!"); + + /* todo, add handle to let user choose altIf at upload time! */ + String altIf = Preferences.get("upload.altID"); + + List commandDownloader = new ArrayList(); + commandDownloader.add("dfu-util"); + commandDownloader.add("-a "+altIf); + commandDownloader.add("-R"); + commandDownloader.add("-d "+usbID); + commandDownloader.add("-D"+ binPath); //"./thisbin.bin"); + + return executeUploadCommand(commandDownloader); + } + + System.err.println("Only .bin files are supported at this time"); + return false; + } + + /* we need to ensure both RTS and DTR are low to start, + then pulse DTR on its own. This is the reset signal + maple responds to + */ + private void emitResetPulse() throws RunnerException { + + /* wait a while for the device to reboot */ + int programDelay = Preferences.getInteger("programDelay"); + + try { + Serial serialPort = new Serial(); + + // try to toggle DTR/RTS (old scheme) + serialPort.setRTS(false); + serialPort.setDTR(false); + serialPort.setDTR(true); + try { + Thread.sleep(50); + } catch (InterruptedException e) {} + serialPort.setDTR(false); + + // try magic number + serialPort.setRTS(true); + serialPort.setDTR(true); + try { + Thread.sleep(50); + } catch (InterruptedException e) {} + serialPort.setDTR(false); + try { + Thread.sleep(50); + } catch (InterruptedException e) {} + serialPort.write("1EAF"); + try { + Thread.sleep(50); + } catch (InterruptedException e) {} + serialPort.dispose(); + + } catch(Exception e) { + System.err.println("Reset via USB Serial Failed! Did you select the right serial port?\nAssuming the board is in perpetual bootloader mode and continuing to attempt dfu programming...\n"); + } + } + + protected String executeCheckCommand(Collection commandDownloader) + throws RunnerException + { + firstErrorFound = false; // haven't found any errors yet + secondErrorFound = false; + notFoundError = false; + int result=0; // pre-initialized to quiet a bogus warning from jikes + + String userdir = System.getProperty("user.dir") + File.separator; + String returnStr = new String(); + + try { + String[] commandArray = new String[commandDownloader.size()]; + commandDownloader.toArray(commandArray); + + String armBasePath; + + //armBasePath = new String(Base.getHardwarePath() + "/tools/arm/bin/"); + armBasePath = ""; + + commandArray[0] = armBasePath + commandArray[0]; + + if (verbose || Preferences.getBoolean("upload.verbose")) { + for(int i = 0; i < commandArray.length; i++) { + System.out.print(commandArray[i] + " "); + } + System.out.println(); + } + + Process process = Runtime.getRuntime().exec(commandArray); + BufferedReader stdInput = new BufferedReader(new + InputStreamReader(process.getInputStream())); + BufferedReader stdError = new BufferedReader(new + InputStreamReader(process.getErrorStream())); + + // wait for the process to finish. if interrupted + // before waitFor returns, continue waiting + // + boolean busy = true; + while (busy) { + try { + result = process.waitFor(); + busy = false; + } catch (InterruptedException intExc) { + } + } + + String s; + while ((s = stdInput.readLine()) != null) { + returnStr += s + "\n"; + } + + process.destroy(); + + if(exception!=null) { + exception.hideStackTrace(); + throw exception; + } + if(result!=0) return "Error!"; + } catch (Exception e) { + e.printStackTrace(); + } + //System.out.println("result2 is "+result); + // if the result isn't a known, expected value it means that something + // is fairly wrong, one possibility is that jikes has crashed. + // + if (exception != null) throw exception; + + if ((result != 0) && (result != 1 )) { + exception = new RunnerException(SUPER_BADNESS); + } + + return returnStr; // ? true : false; + + } + + // Need to overload this from Uploader to use the system-wide dfu-util + protected boolean executeUploadCommand(Collection commandDownloader) + throws RunnerException + { + firstErrorFound = false; // haven't found any errors yet + secondErrorFound = false; + notFoundError = false; + int result=0; // pre-initialized to quiet a bogus warning from jikes + + String userdir = System.getProperty("user.dir") + File.separator; + + try { + String[] commandArray = new String[commandDownloader.size()]; + commandDownloader.toArray(commandArray); + + String armBasePath; + + //armBasePath = new String(Base.getHardwarePath() + "/tools/arm/bin/"); + armBasePath = ""; + + commandArray[0] = armBasePath + commandArray[0]; + + if (verbose || Preferences.getBoolean("upload.verbose")) { + for(int i = 0; i < commandArray.length; i++) { + System.out.print(commandArray[i] + " "); + } + System.out.println(); + } + + Process process = Runtime.getRuntime().exec(commandArray); + new MessageSiphon(process.getInputStream(), this); + new MessageSiphon(process.getErrorStream(), this); + + // wait for the process to finish. if interrupted + // before waitFor returns, continue waiting + // + boolean compiling = true; + while (compiling) { + try { + result = process.waitFor(); + compiling = false; + } catch (InterruptedException intExc) { + } + } + if(exception!=null) { + exception.hideStackTrace(); + throw exception; + } + if(result!=0) + return false; + } catch (Exception e) { + e.printStackTrace(); + } + //System.out.println("result2 is "+result); + // if the result isn't a known, expected value it means that something + // is fairly wrong, one possibility is that jikes has crashed. + // + if (exception != null) throw exception; + + if ((result != 0) && (result != 1 )) { + exception = new RunnerException(SUPER_BADNESS); + //editor.error(exception); + //PdeBase.openURL(BUGS_URL); + //throw new PdeException(SUPER_BADNESS); + } + + return (result == 0); // ? true : false; + + } + + // deal with messages from dfu-util... + public void message(String s) { + + if(s.indexOf("dfu-util - (C) ") != -1) { return; } + if(s.indexOf("This program is Free Software and has ABSOLUTELY NO WARRANTY") != -1) { return; } + + if(s.indexOf("No DFU capable USB device found") != -1) { + System.err.print(s); + exception = new RunnerException("Problem uploading via dfu-util: No Maple found"); + return; + } + + if(s.indexOf("Operation not perimitted") != -1) { + System.err.print(s); + exception = new RunnerException("Problem uploading via dfu-util: Insufficient privilages"); + return; + } + + // else just print everything... + System.out.print(s); + } + +} diff --git a/tools/win/src/maple_loader/src/CliTemplate/ExecCommand.java b/tools/win/src/maple_loader/src/CliTemplate/ExecCommand.java new file mode 100644 index 0000000..3d6c106 --- /dev/null +++ b/tools/win/src/maple_loader/src/CliTemplate/ExecCommand.java @@ -0,0 +1,119 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +package CliTemplate; + +import java.io.IOException; +import java.text.MessageFormat; +import java.util.ArrayList; +import java.util.List; + +import processing.app.debug.MessageConsumer; +import processing.app.debug.MessageSiphon; +import processing.app.debug.RunnerException; +import processing.app.helpers.ProcessUtils; + +/** + * + * @author cousinr + */ +public class ExecCommand implements MessageConsumer { + + private boolean verbose = true; + private boolean firstErrorFound; + private boolean secondErrorFound; + private RunnerException exception; + + /** + * Either succeeds or throws a RunnerException fit for public consumption. + * + * @param command + * @throws RunnerException + */ + public void execAsynchronously(String[] command) throws RunnerException { + + // eliminate any empty array entries + List stringList = new ArrayList<>(); + for (String string : command) { + string = string.trim(); + if (string.length() != 0) + stringList.add(string); + } + command = stringList.toArray(new String[stringList.size()]); + if (command.length == 0) + return; + int result = 0; + + if (verbose) { + for (String c : command) + System.out.print(c + " "); + System.out.println(); + } + + firstErrorFound = false; // haven't found any errors yet + secondErrorFound = false; + + Process process; + try { + process = ProcessUtils.exec(command); + } catch (IOException e) { + RunnerException re = new RunnerException(e.getMessage()); + re.hideStackTrace(); + throw re; + } + + MessageSiphon in = new MessageSiphon(process.getInputStream(), this); + MessageSiphon err = new MessageSiphon(process.getErrorStream(), this); + + // wait for the process to finish. if interrupted + // before waitFor returns, continue waiting + boolean compiling = true; + while (compiling) { + try { + in.join(); + err.join(); + result = process.waitFor(); + //System.out.println("result is " + result); + compiling = false; + } catch (InterruptedException ignored) { } + } + + // an error was queued up by message(), barf this back to compile(), + // which will barf it back to Editor. if you're having trouble + // discerning the imagery, consider how cows regurgitate their food + // to digest it, and the fact that they have five stomaches. + // + //System.out.println("throwing up " + exception); + if (exception != null) + throw exception; + + if (result > 1) { + // a failure in the tool (e.g. unable to locate a sub-executable) + System.err.println(MessageFormat.format("{0} returned {1}", command[0], result)); + } + + if (result != 0) { + RunnerException re = new RunnerException(MessageFormat.format("exit code: {0}", result)); + re.hideStackTrace(); + throw re; + } + } + + /** + * Part of the MessageConsumer interface, this is called + * whenever a piece (usually a line) of error message is spewed + * out from the compiler. The errors are parsed for their contents + * and line number, which is then reported back to Editor. + * @param s + */ + @Override + public void message(String s) { + int i; + + + System.err.print(s); + } + +} diff --git a/tools/win/src/maple_loader/src/processing/app/Base.java b/tools/win/src/maple_loader/src/processing/app/Base.java new file mode 100644 index 0000000..c3a174d --- /dev/null +++ b/tools/win/src/maple_loader/src/processing/app/Base.java @@ -0,0 +1,53 @@ +/* -*- mode: java; c-basic-offset: 2; indent-tabs-mode: nil -*- */ + +/* + Part of the Processing project - http://processing.org + + Copyright (c) 2004-10 Ben Fry and Casey Reas + Copyright (c) 2001-04 Massachusetts Institute of Technology + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License version 2 + as published by the Free Software Foundation. + + 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 +*/ + +package processing.app; + + +/** + * The base class for the main processing application. + * Primary role of this class is for platform identification and + * general interaction with the system (launching URLs, loading + * files and images, etc) that comes from that. + */ +public class Base { + + /** + * returns true if running on windows. + */ + static public boolean isWindows() { + //return PApplet.platform == PConstants.WINDOWS; + return System.getProperty("os.name").indexOf("Windows") != -1; + } + + + /** + * true if running on linux. + */ + static public boolean isLinux() { + //return PApplet.platform == PConstants.LINUX; + return System.getProperty("os.name").indexOf("Linux") != -1; + } + + + +} diff --git a/tools/win/src/maple_loader/src/processing/app/Preferences.java b/tools/win/src/maple_loader/src/processing/app/Preferences.java new file mode 100644 index 0000000..6368e38 --- /dev/null +++ b/tools/win/src/maple_loader/src/processing/app/Preferences.java @@ -0,0 +1,157 @@ +/* -*- mode: java; c-basic-offset: 2; indent-tabs-mode: nil -*- */ + +/* + Part of the Processing project - http://processing.org + + Copyright (c) 2004-09 Ben Fry and Casey Reas + Copyright (c) 2001-04 Massachusetts Institute of Technology + + 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 +*/ + +package processing.app; + +import java.io.*; +import java.util.*; + + +/** + * Storage class for user preferences and environment settings. + *

+ * This class no longer uses the Properties class, since + * properties files are iso8859-1, which is highly likely to + * be a problem when trying to save sketch folders and locations. + *

+ * The GUI portion in here is really ugly, as it uses exact layout. This was + * done in frustration one evening (and pre-Swing), but that's long since past, + * and it should all be moved to a proper swing layout like BoxLayout. + *

+ * This is very poorly put together, that the preferences panel and the actual + * preferences i/o is part of the same code. But there hasn't yet been a + * compelling reason to bother with the separation aside from concern about + * being lectured by strangers who feel that it doesn't look like what they + * learned in CS class. + *

+ * Would also be possible to change this to use the Java Preferences API. + * Some useful articles + * here and + * here. + * However, haven't implemented this yet for lack of time, but more + * importantly, because it would entail writing to the registry (on Windows), + * or an obscure file location (on Mac OS X) and make it far more difficult to + * find the preferences to tweak them by hand (no! stay out of regedit!) + * or to reset the preferences by simply deleting the preferences.txt file. + */ +public class Preferences { + + // what to call the feller + + static final String PREFS_FILE = "preferences.txt"; + + + // prompt text stuff + + static final String PROMPT_YES = "Yes"; + static final String PROMPT_NO = "No"; + static final String PROMPT_CANCEL = "Cancel"; + static final String PROMPT_OK = "OK"; + static final String PROMPT_BROWSE = "Browse"; + + /** + * Standardized width for buttons. Mac OS X 10.3 wants 70 as its default, + * Windows XP needs 66, and my Ubuntu machine needs 80+, so 80 seems proper. + */ + static public int BUTTON_WIDTH = 80; + + /** + * Standardized button height. Mac OS X 10.3 (Java 1.4) wants 29, + * presumably because it now includes the blue border, where it didn't + * in Java 1.3. Windows XP only wants 23 (not sure what default Linux + * would be). Because of the disparity, on Mac OS X, it will be set + * inside a static block. + */ + static public int BUTTON_HEIGHT = 24; + + // value for the size bars, buttons, etc + + static final int GRID_SIZE = 33; + + + // indents and spacing standards. these probably need to be modified + // per platform as well, since macosx is so huge, windows is smaller, + // and linux is all over the map + + static final int GUI_BIG = 13; + static final int GUI_BETWEEN = 10; + static final int GUI_SMALL = 6; + + + + // data model + + static Hashtable table = new Hashtable();; + static File preferencesFile; + + + static protected void init(String commandLinePrefs) { + + + } + + + public Preferences() { + + } + + // ................................................................. + + // ................................................................. + + // ................................................................. + + // ................................................................. + + + + static public String get(String attribute) { + return (String) table.get(attribute); + } + + static public void set(String attribute, String value) { + table.put(attribute, value); + } + + + static public boolean getBoolean(String attribute) { + String value = get(attribute); + return (new Boolean(value)).booleanValue(); + } + + + static public void setBoolean(String attribute, boolean value) { + set(attribute, value ? "true" : "false"); + } + + + static public int getInteger(String attribute) { + return Integer.parseInt(get(attribute)); + } + + + static public void setInteger(String key, int value) { + set(key, String.valueOf(value)); + } + +} diff --git a/tools/win/src/maple_loader/src/processing/app/Serial.java b/tools/win/src/maple_loader/src/processing/app/Serial.java new file mode 100644 index 0000000..04566a7 --- /dev/null +++ b/tools/win/src/maple_loader/src/processing/app/Serial.java @@ -0,0 +1,527 @@ +/* -*- mode: jde; c-basic-offset: 2; indent-tabs-mode: nil -*- */ + +/* + PSerial - class for serial port goodness + Part of the Processing project - http://processing.org + + Copyright (c) 2004 Ben Fry & Casey Reas + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + This library 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 + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General + Public License along with this library; if not, write to the + Free Software Foundation, Inc., 59 Temple Place, Suite 330, + Boston, MA 02111-1307 USA +*/ + +package processing.app; +//import processing.core.*; + + +import java.io.*; +import java.text.MessageFormat; +import java.util.*; +import jssc.SerialPort; +import jssc.SerialPortEvent; +import jssc.SerialPortEventListener; +import jssc.SerialPortException; +import jssc.SerialPortList; +import processing.app.debug.MessageConsumer; + + +public class Serial implements SerialPortEventListener { + + //PApplet parent; + + // properties can be passed in for default values + // otherwise defaults to 9600 N81 + + // these could be made static, which might be a solution + // for the classloading problem.. because if code ran again, + // the static class would have an object that could be closed + + SerialPort port; + + int rate; + int parity; + int databits; + int stopbits; + boolean monitor = false; + + // read buffer and streams + + InputStream input; + OutputStream output; + + byte buffer[] = new byte[32768]; + int bufferIndex; + int bufferLast; + + MessageConsumer consumer; + + public Serial(boolean monitor) throws SerialException { + this(Preferences.get("serial.port"), + Preferences.getInteger("serial.debug_rate"), + Preferences.get("serial.parity").charAt(0), + Preferences.getInteger("serial.databits"), + new Float(Preferences.get("serial.stopbits")).floatValue()); + this.monitor = monitor; + } + + public Serial() throws SerialException { + this(Preferences.get("serial.port"), + Preferences.getInteger("serial.debug_rate"), + Preferences.get("serial.parity").charAt(0), + Preferences.getInteger("serial.databits"), + new Float(Preferences.get("serial.stopbits")).floatValue()); + } + + public Serial(int irate) throws SerialException { + this(Preferences.get("serial.port"), irate, + Preferences.get("serial.parity").charAt(0), + Preferences.getInteger("serial.databits"), + new Float(Preferences.get("serial.stopbits")).floatValue()); + } + + public Serial(String iname, int irate) throws SerialException { + this(iname, irate, Preferences.get("serial.parity").charAt(0), + Preferences.getInteger("serial.databits"), + new Float(Preferences.get("serial.stopbits")).floatValue()); + } + + public Serial(String iname) throws SerialException { + this(iname, Preferences.getInteger("serial.debug_rate"), + Preferences.get("serial.parity").charAt(0), + Preferences.getInteger("serial.databits"), + new Float(Preferences.get("serial.stopbits")).floatValue()); + } + + public Serial(String iname, int irate, + char iparity, int idatabits, float istopbits) + throws SerialException { + //if (port != null) port.close(); + //this.parent = parent; + //parent.attach(this); + + this.rate = irate; + + parity = SerialPort.PARITY_NONE; + if (iparity == 'E') parity = SerialPort.PARITY_EVEN; + if (iparity == 'O') parity = SerialPort.PARITY_ODD; + + this.databits = idatabits; + + stopbits = SerialPort.STOPBITS_1; + if (istopbits == 1.5f) stopbits = SerialPort.STOPBITS_1_5; + if (istopbits == 2) stopbits = SerialPort.STOPBITS_2; + + try { + port = new SerialPort(iname); + port.openPort(); + port.setParams(rate, databits, stopbits, parity, true, true); + port.addEventListener(this); + } catch (Exception e) { + throw new SerialException(MessageFormat.format("Error opening serial port ''{0}''.", iname), e); + } + + if (port == null) { + throw new SerialException("Serial port '" + iname + "' not found. Did you select the right one from the Tools > Serial Port menu?"); + } + } + + + public void setup() { + //parent.registerCall(this, DISPOSE); + } + + public void dispose() throws IOException { + if (port != null) { + try { + if (port.isOpened()) { + port.closePort(); // close the port + } + } catch (SerialPortException e) { + throw new IOException(e); + } finally { + port = null; + } + } + } + + public void addListener(MessageConsumer consumer) { + this.consumer = consumer; + } + + public synchronized void serialEvent(SerialPortEvent serialEvent) { + if (serialEvent.isRXCHAR()) { + try { + byte[] buf = port.readBytes(serialEvent.getEventValue()); + if (buf.length > 0) { + if (bufferLast == buffer.length) { + byte temp[] = new byte[bufferLast << 1]; + System.arraycopy(buffer, 0, temp, 0, bufferLast); + buffer = temp; + } + if (monitor) { + System.out.print(new String(buf)); + } + if (this.consumer != null) { + this.consumer.message(new String(buf)); + } + } + } catch (SerialPortException e) { + errorMessage("serialEvent", e); + } + } + } + + + /** + * Returns the number of bytes that have been read from serial + * and are waiting to be dealt with by the user. + */ + public synchronized int available() { + return (bufferLast - bufferIndex); + } + + + /** + * Ignore all the bytes read so far and empty the buffer. + */ + public synchronized void clear() { + bufferLast = 0; + bufferIndex = 0; + } + + + /** + * Returns a number between 0 and 255 for the next byte that's + * waiting in the buffer. + * Returns -1 if there was no byte (although the user should + * first check available() to see if things are ready to avoid this) + */ + public synchronized int read() { + if (bufferIndex == bufferLast) return -1; + + int outgoing = buffer[bufferIndex++] & 0xff; + if (bufferIndex == bufferLast) { // rewind + bufferIndex = 0; + bufferLast = 0; + } + return outgoing; + } + + + /** + * Returns the next byte in the buffer as a char. + * Returns -1, or 0xffff, if nothing is there. + */ + public synchronized char readChar() { + if (bufferIndex == bufferLast) return (char)(-1); + return (char) read(); + } + + + /** + * Return a byte array of anything that's in the serial buffer. + * Not particularly memory/speed efficient, because it creates + * a byte array on each read, but it's easier to use than + * readBytes(byte b[]) (see below). + */ + public synchronized byte[] readBytes() { + if (bufferIndex == bufferLast) return null; + + int length = bufferLast - bufferIndex; + byte outgoing[] = new byte[length]; + System.arraycopy(buffer, bufferIndex, outgoing, 0, length); + + bufferIndex = 0; // rewind + bufferLast = 0; + return outgoing; + } + + + /** + * Grab whatever is in the serial buffer, and stuff it into a + * byte buffer passed in by the user. This is more memory/time + * efficient than readBytes() returning a byte[] array. + *

+ * Returns an int for how many bytes were read. If more bytes + * are available than can fit into the byte array, only those + * that will fit are read. + */ + public synchronized int readBytes(byte outgoing[]) { + if (bufferIndex == bufferLast) return 0; + + int length = bufferLast - bufferIndex; + if (length > outgoing.length) length = outgoing.length; + System.arraycopy(buffer, bufferIndex, outgoing, 0, length); + + bufferIndex += length; + if (bufferIndex == bufferLast) { + bufferIndex = 0; // rewind + bufferLast = 0; + } + return length; + } + + + /** + * Reads from the serial port into a buffer of bytes up to and + * including a particular character. If the character isn't in + * the serial buffer, then 'null' is returned. + */ + public synchronized byte[] readBytesUntil(int interesting) { + if (bufferIndex == bufferLast) return null; + byte what = (byte)interesting; + + int found = -1; + for (int k = bufferIndex; k < bufferLast; k++) { + if (buffer[k] == what) { + found = k; + break; + } + } + if (found == -1) return null; + + int length = found - bufferIndex + 1; + byte outgoing[] = new byte[length]; + System.arraycopy(buffer, bufferIndex, outgoing, 0, length); + + bufferIndex = 0; // rewind + bufferLast = 0; + return outgoing; + } + + + /** + * Reads from the serial port into a buffer of bytes until a + * particular character. If the character isn't in the serial + * buffer, then 'null' is returned. + *

+ * If outgoing[] is not big enough, then -1 is returned, + * and an error message is printed on the console. + * If nothing is in the buffer, zero is returned. + * If 'interesting' byte is not in the buffer, then 0 is returned. + */ + public synchronized int readBytesUntil(int interesting, byte outgoing[]) { + if (bufferIndex == bufferLast) return 0; + byte what = (byte)interesting; + + int found = -1; + for (int k = bufferIndex; k < bufferLast; k++) { + if (buffer[k] == what) { + found = k; + break; + } + } + if (found == -1) return 0; + + int length = found - bufferIndex + 1; + if (length > outgoing.length) { + System.err.println("readBytesUntil() byte buffer is" + + " too small for the " + length + + " bytes up to and including char " + interesting); + return -1; + } + //byte outgoing[] = new byte[length]; + System.arraycopy(buffer, bufferIndex, outgoing, 0, length); + + bufferIndex += length; + if (bufferIndex == bufferLast) { + bufferIndex = 0; // rewind + bufferLast = 0; + } + return length; + } + + + /** + * Return whatever has been read from the serial port so far + * as a String. It assumes that the incoming characters are ASCII. + *

+ * If you want to move Unicode data, you can first convert the + * String to a byte stream in the representation of your choice + * (i.e. UTF8 or two-byte Unicode data), and send it as a byte array. + */ + public synchronized String readString() { + if (bufferIndex == bufferLast) return null; + return new String(readBytes()); + } + + + /** + * Combination of readBytesUntil and readString. See caveats in + * each function. Returns null if it still hasn't found what + * you're looking for. + *

+ * If you want to move Unicode data, you can first convert the + * String to a byte stream in the representation of your choice + * (i.e. UTF8 or two-byte Unicode data), and send it as a byte array. + */ + public synchronized String readStringUntil(int interesting) { + byte b[] = readBytesUntil(interesting); + if (b == null) return null; + return new String(b); + } + + + /** + * This will handle both ints, bytes and chars transparently. + */ + public void write(int what) { // will also cover char + try { + port.writeInt(what & 0xff); + } catch (SerialPortException e) { + errorMessage("write", e); + } + } + + + public void write(byte bytes[]) { + try { + port.writeBytes(bytes); + } catch (SerialPortException e) { + errorMessage("write", e); + } + } + + + /** + * Write a String to the output. Note that this doesn't account + * for Unicode (two bytes per char), nor will it send UTF8 + * characters.. It assumes that you mean to send a byte buffer + * (most often the case for networking and serial i/o) and + * will only use the bottom 8 bits of each char in the string. + * (Meaning that internally it uses String.getBytes) + *

+ * If you want to move Unicode data, you can first convert the + * String to a byte stream in the representation of your choice + * (i.e. UTF8 or two-byte Unicode data), and send it as a byte array. + */ + public void write(String what) { + write(what.getBytes()); + } + + public void setDTR(boolean state) { + try { + port.setDTR(state); + } catch (SerialPortException e) { + errorMessage("setDTR", e); + } + } + + public void setRTS(boolean state) { + try { + port.setRTS(state); + } catch (SerialPortException e) { + errorMessage("setRTS", e); + } + } + + static public List list() { + return Arrays.asList(SerialPortList.getPortNames()); + } + + + /** + * General error reporting, all corraled here just in case + * I think of something slightly more intelligent to do. + */ + static public void errorMessage(String where, Throwable e) { + System.err.println("Error inside Serial." + where + "()"); + e.printStackTrace(); + } +} + + + /* + class SerialMenuListener implements ItemListener { + //public SerialMenuListener() { } + + public void itemStateChanged(ItemEvent e) { + int count = serialMenu.getItemCount(); + for (int i = 0; i < count; i++) { + ((CheckboxMenuItem)serialMenu.getItem(i)).setState(false); + } + CheckboxMenuItem item = (CheckboxMenuItem)e.getSource(); + item.setState(true); + String name = item.getLabel(); + //System.out.println(item.getLabel()); + PdeBase.properties.put("serial.port", name); + //System.out.println("set to " + get("serial.port")); + } + } + */ + + + /* + protected Vector buildPortList() { + // get list of names for serial ports + // have the default port checked (if present) + Vector list = new Vector(); + + //SerialMenuListener listener = new SerialMenuListener(); + boolean problem = false; + + // if this is failing, it may be because + // lib/javax.comm.properties is missing. + // java is weird about how it searches for java.comm.properties + // so it tends to be very fragile. i.e. quotes in the CLASSPATH + // environment variable will hose things. + try { + //System.out.println("building port list"); + Enumeration portList = CommPortIdentifier.getPortIdentifiers(); + while (portList.hasMoreElements()) { + CommPortIdentifier portId = + (CommPortIdentifier) portList.nextElement(); + //System.out.println(portId); + + if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) { + //if (portId.getName().equals(port)) { + String name = portId.getName(); + //CheckboxMenuItem mi = + //new CheckboxMenuItem(name, name.equals(defaultName)); + + //mi.addItemListener(listener); + //serialMenu.add(mi); + list.addElement(name); + } + } + } catch (UnsatisfiedLinkError e) { + e.printStackTrace(); + problem = true; + + } catch (Exception e) { + System.out.println("exception building serial menu"); + e.printStackTrace(); + } + + //if (serialMenu.getItemCount() == 0) { + //System.out.println("dimming serial menu"); + //serialMenu.setEnabled(false); + //} + + // only warn them if this is the first time + if (problem && PdeBase.firstTime) { + JOptionPane.showMessageDialog(this, //frame, + "Serial port support not installed.\n" + + "Check the readme for instructions\n" + + "if you need to use the serial port. ", + "Serial Port Warning", + JOptionPane.WARNING_MESSAGE); + } + return list; + } + */ + + + diff --git a/tools/win/src/maple_loader/src/processing/app/SerialException.java b/tools/win/src/maple_loader/src/processing/app/SerialException.java new file mode 100644 index 0000000..525c240 --- /dev/null +++ b/tools/win/src/maple_loader/src/processing/app/SerialException.java @@ -0,0 +1,39 @@ +/* -*- mode: jde; c-basic-offset: 2; indent-tabs-mode: nil -*- */ + +/* + Copyright (c) 2007 David A. Mellis + + 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 +*/ + +package processing.app; + +public class SerialException extends Exception { + public SerialException() { + super(); + } + + public SerialException(String message) { + super(message); + } + + public SerialException(String message, Throwable cause) { + super(message, cause); + } + + public SerialException(Throwable cause) { + super(cause); + } +} diff --git a/tools/win/src/maple_loader/src/processing/app/debug/MessageConsumer.java b/tools/win/src/maple_loader/src/processing/app/debug/MessageConsumer.java new file mode 100644 index 0000000..5e20429 --- /dev/null +++ b/tools/win/src/maple_loader/src/processing/app/debug/MessageConsumer.java @@ -0,0 +1,42 @@ +/* -*- mode: java; c-basic-offset: 2; indent-tabs-mode: nil -*- */ + +/* + Part of the Processing project - http://processing.org + + Copyright (c) 2004-06 Ben Fry and Casey Reas + Copyright (c) 2001-04 Massachusetts Institute of Technology + + 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 +*/ + +package processing.app.debug; + + +/** + * Interface for dealing with parser/compiler output. + *

+ * Different instances of MessageStream need to do different things with + * messages. In particular, a stream instance used for parsing output from + * the compiler compiler has to interpret its messages differently than one + * parsing output from the runtime. + *

+ * Classes which consume messages and do something with them + * should implement this interface. + */ +public interface MessageConsumer { + + public void message(String s); + +} diff --git a/tools/win/src/maple_loader/src/processing/app/debug/MessageSiphon.java b/tools/win/src/maple_loader/src/processing/app/debug/MessageSiphon.java new file mode 100644 index 0000000..26901c3 --- /dev/null +++ b/tools/win/src/maple_loader/src/processing/app/debug/MessageSiphon.java @@ -0,0 +1,104 @@ +/* -*- mode: java; c-basic-offset: 2; indent-tabs-mode: nil -*- */ + +/* + Part of the Processing project - http://processing.org + + Copyright (c) 2004-06 Ben Fry and Casey Reas + Copyright (c) 2001-04 Massachusetts Institute of Technology + + 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 +*/ + +package processing.app.debug; + +import java.io.BufferedReader; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.net.SocketException; + +/** + * Slurps up messages from compiler. + */ +public class MessageSiphon implements Runnable { + + private final BufferedReader streamReader; + private final MessageConsumer consumer; + + private Thread thread; + private boolean canRun; + + public MessageSiphon(InputStream stream, MessageConsumer consumer) { + this.streamReader = new BufferedReader(new InputStreamReader(stream)); + this.consumer = consumer; + this.canRun = true; + + thread = new Thread(this); + // don't set priority too low, otherwise exceptions won't + // bubble up in time (i.e. compile errors have a weird delay) + //thread.setPriority(Thread.MIN_PRIORITY); + thread.setPriority(Thread.MAX_PRIORITY - 1); + thread.start(); + } + + + public void run() { + try { + // process data until we hit EOF; this will happily block + // (effectively sleeping the thread) until new data comes in. + // when the program is finally done, null will come through. + // + String currentLine; + while (canRun && (currentLine = streamReader.readLine()) != null) { + // \n is added again because readLine() strips it out + //EditorConsole.systemOut.println("messaging in"); + consumer.message(currentLine + "\n"); + //EditorConsole.systemOut.println("messaging out"); + } + //EditorConsole.systemOut.println("messaging thread done"); + } catch (NullPointerException npe) { + // Fairly common exception during shutdown + } catch (SocketException e) { + // socket has been close while we were wainting for data. nothing to see here, move along + } catch (Exception e) { + // On Linux and sometimes on Mac OS X, a "bad file descriptor" + // message comes up when closing an applet that's run externally. + // That message just gets supressed here.. + String mess = e.getMessage(); + if ((mess != null) && + (mess.indexOf("Bad file descriptor") != -1)) { + //if (e.getMessage().indexOf("Bad file descriptor") == -1) { + //System.err.println("MessageSiphon err " + e); + //e.printStackTrace(); + } else { + e.printStackTrace(); + } + } finally { + thread = null; + } + } + + // Wait until the MessageSiphon thread is complete. + public void join() throws java.lang.InterruptedException { + // Grab a temp copy in case another thread nulls the "thread" + // member variable + Thread t = thread; + if (t != null) t.join(); + } + + public void stop() { + this.canRun = false; + } + +} diff --git a/tools/win/src/maple_loader/src/processing/app/debug/RunnerException.java b/tools/win/src/maple_loader/src/processing/app/debug/RunnerException.java new file mode 100644 index 0000000..0a67d1e --- /dev/null +++ b/tools/win/src/maple_loader/src/processing/app/debug/RunnerException.java @@ -0,0 +1,161 @@ +/* -*- mode: java; c-basic-offset: 2; indent-tabs-mode: nil -*- */ + +/* + Part of the Processing project - http://processing.org + + Copyright (c) 2004-08 Ben Fry and Casey Reas + Copyright (c) 2001-04 Massachusetts Institute of Technology + + 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 +*/ + +package processing.app.debug; + + +/** + * An exception with a line number attached that occurs + * during either compile time or run time. + */ +@SuppressWarnings("serial") +public class RunnerException extends Exception { + protected String message; + protected int codeIndex; + protected int codeLine; + protected int codeColumn; + protected boolean showStackTrace; + + + public RunnerException(String message) { + this(message, true); + } + + public RunnerException(String message, boolean showStackTrace) { + this(message, -1, -1, -1, showStackTrace); + } + + public RunnerException(String message, int file, int line) { + this(message, file, line, -1, true); + } + + + public RunnerException(String message, int file, int line, int column) { + this(message, file, line, column, true); + } + + + public RunnerException(String message, int file, int line, int column, + boolean showStackTrace) { + this.message = message; + this.codeIndex = file; + this.codeLine = line; + this.codeColumn = column; + this.showStackTrace = showStackTrace; + } + + + public RunnerException(Exception e) { + super(e); + this.showStackTrace = true; + } + + /** + * Override getMessage() in Throwable, so that I can set + * the message text outside the constructor. + */ + public String getMessage() { + return message; + } + + + public void setMessage(String message) { + this.message = message; + } + + + public int getCodeIndex() { + return codeIndex; + } + + + public void setCodeIndex(int index) { + codeIndex = index; + } + + + public boolean hasCodeIndex() { + return codeIndex != -1; + } + + + public int getCodeLine() { + return codeLine; + } + + + public void setCodeLine(int line) { + this.codeLine = line; + } + + + public boolean hasCodeLine() { + return codeLine != -1; + } + + + public void setCodeColumn(int column) { + this.codeColumn = column; + } + + + public int getCodeColumn() { + return codeColumn; + } + + + public void showStackTrace() { + showStackTrace = true; + } + + + public void hideStackTrace() { + showStackTrace = false; + } + + + /** + * Nix the java.lang crap out of an exception message + * because it scares the children. + *

+ * This function must be static to be used with super() + * in each of the constructors above. + */ + /* + static public final String massage(String msg) { + if (msg.indexOf("java.lang.") == 0) { + //int dot = msg.lastIndexOf('.'); + msg = msg.substring("java.lang.".length()); + } + return msg; + //return (dot == -1) ? msg : msg.substring(dot+1); + } + */ + + + public void printStackTrace() { + if (showStackTrace) { + super.printStackTrace(); + } + } +} diff --git a/tools/win/src/maple_loader/src/processing/app/helpers/ProcessUtils.java b/tools/win/src/maple_loader/src/processing/app/helpers/ProcessUtils.java new file mode 100644 index 0000000..c023f58 --- /dev/null +++ b/tools/win/src/maple_loader/src/processing/app/helpers/ProcessUtils.java @@ -0,0 +1,32 @@ +package processing.app.helpers; + +//import processing.app.Base; + +import java.io.IOException; +import java.util.Map; + +import processing.app.Base; + +public class ProcessUtils { + + public static Process exec(String[] command) throws IOException { + // No problems on linux and mac + if (!Base.isWindows()) { + return Runtime.getRuntime().exec(command); + } + + // Brutal hack to workaround windows command line parsing. + // http://stackoverflow.com/questions/5969724/java-runtime-exec-fails-to-escape-characters-properly + // http://msdn.microsoft.com/en-us/library/a1y7w461.aspx + // http://bugs.sun.com/view_bug.do?bug_id=6468220 + // http://bugs.sun.com/view_bug.do?bug_id=6518827 + String[] cmdLine = new String[command.length]; + for (int i = 0; i < command.length; i++) + cmdLine[i] = command[i].replace("\"", "\\\""); + + ProcessBuilder pb = new ProcessBuilder(cmdLine); + Map env = pb.environment(); + env.put("CYGWIN", "nodosfilewarning"); + return pb.start(); + } +} diff --git a/tools/win/src/stm32flash_serial/src/AUTHORS b/tools/win/src/stm32flash_serial/src/AUTHORS new file mode 100644 index 0000000..d096f22 --- /dev/null +++ b/tools/win/src/stm32flash_serial/src/AUTHORS @@ -0,0 +1,19 @@ +Authors ordered by first contribution. + +Geoffrey McRae +Bret Olmsted +Tormod Volden +Jakob Malm +Reuben Dowle +Matthias Kubisch +Paul Fertser +Daniel Strnad +Jérémie Rapin +Christian Pointner +Mats Erik Andersson +Alexey Borovik +Antonio Borneo +Armin van der Togt +Brian Silverman +Georg Hofmann +Luis Rodrigues diff --git a/tools/win/src/stm32flash_serial/src/Android.mk b/tools/win/src/stm32flash_serial/src/Android.mk new file mode 100644 index 0000000..7be3d00 --- /dev/null +++ b/tools/win/src/stm32flash_serial/src/Android.mk @@ -0,0 +1,20 @@ +TOP_LOCAL_PATH := $(call my-dir) + +include $(call all-named-subdir-makefiles, parsers) + +LOCAL_PATH := $(TOP_LOCAL_PATH) + +include $(CLEAR_VARS) +LOCAL_MODULE := stm32flash +LOCAL_SRC_FILES := \ + dev_table.c \ + i2c.c \ + init.c \ + main.c \ + port.c \ + serial_common.c \ + serial_platform.c \ + stm32.c \ + utils.c +LOCAL_STATIC_LIBRARIES := libparsers +include $(BUILD_EXECUTABLE) diff --git a/tools/win/src/stm32flash_serial/src/HOWTO b/tools/win/src/stm32flash_serial/src/HOWTO new file mode 100644 index 0000000..d8f32eb --- /dev/null +++ b/tools/win/src/stm32flash_serial/src/HOWTO @@ -0,0 +1,35 @@ +Add new interfaces: +===================================================================== +Current version 0.4 supports the following interfaces: +- UART Windows (either "COMn" and "\\.\COMn"); +- UART posix/Linux (e.g. "/dev/ttyUSB0"); +- I2C Linux through standard driver "i2c-dev" (e.g. "/dev/i2c-n"). + +Starting from version 0.4, the back-end of stm32flash is modular and +ready to be expanded to support new interfaces. +I'm planning adding SPI on Linux through standard driver "spidev". +You are invited to contribute with more interfaces. + +To add a new interface you need to add a new file, populate the struct +port_interface (check at the end of files i2c.c, serial_posix.c and +serial_w32.c) and provide the relative functions to operate on the +interface: open/close, read/write, get_cfg_str and the optional gpio. +The include the new drive in Makefile and register the new struct +port_interface in file port.c in struct port_interface *ports[]. + +There are several USB-I2C adapter in the market, each providing its +own libraries to communicate with the I2C bus. +Could be interesting to provide as back-end a bridge between stm32flash +and such libraries (I have no plan on this item). + + +Add new STM32 devices: +===================================================================== +Add a new line in file dev_table.c, in table devices[]. +The fields of the table are listed in stm32.h, struct stm32_dev. + + +Cross compile on Linux host for Windows target with MinGW: +===================================================================== +I'm using a 64 bit Arch Linux machines, and I usually run: + make CC=x86_64-w64-mingw32-gcc AR=x86_64-w64-mingw32-ar diff --git a/tools/win/src/stm32flash_serial/src/I2C.txt b/tools/win/src/stm32flash_serial/src/I2C.txt new file mode 100644 index 0000000..4c05ff6 --- /dev/null +++ b/tools/win/src/stm32flash_serial/src/I2C.txt @@ -0,0 +1,94 @@ +About I2C back-end communication in stm32flash +========================================================================== + +Starting from version v0.4, beside the serial communication port, +stm32flash adds support for I2C port to talk with STM32 bootloader. + +The current I2C back-end supports only the API provided by Linux kernel +driver "i2c-dev", so only I2C controllers with Linux kernel driver can be +used. +In Linux source code, most of the drivers for I2C and SMBUS controllers +are in + ./drivers/i2c/busses/ +Only I2C is supported by STM32 bootloader, so check the section below +about SMBUS. +No I2C support for Windows is available in stm32flash v0.4. + +Thanks to the new modular back-end, stm32flash can be easily extended to +support new back-ends and API. Check HOWTO file in stm32flash source code +for details. + +In the market there are several USB-to-I2C dongles; most of them are not +supported by kernel drivers. Manufacturer provide proprietary userspace +libraries using not standardized API. +These API and dongles could be supported in feature versions. + +There are currently 3 versions of STM32 bootloader for I2C communications: +- v1.0 using I2C clock stretching synchronization between host and STM32; +- v1.1 superset of v1.0, adds non stretching commands; +- v1.2 superset of v1.1, adds CRC command and compatibility with i2cdetect. +Details in ST application note AN2606. +All the bootloaders above are tested and working with stm32flash. + + +SMBUS controllers +========================================================================== + +Almost 50% of the drivers in Linux source code folder + ./drivers/i2c/busses/ +are for controllers that "only" support SMBUS protocol. They can NOT +operate with STM32 bootloader. +To identify if your controller supports I2C, use command: + i2cdetect -F n +where "n" is the number of the I2C interface (e.g. n=3 for "/dev/i2c-3"). +Controllers that supports I2C will report + I2C yes +Controller that support both I2C and SMBUS are ok. + +If you are interested on details about SMBUS protocol, you can download +the current specs from + http://smbus.org/specs/smbus20.pdf +and you can read the files in Linux source code + ./Documentation/i2c/i2c-protocol + ./Documentation/i2c/smbus-protocol + + +About bootloader v1.0 +========================================================================== + +Version v1.0 can have issues with some I2C controllers due to use of clock +stretching during commands that require long operations, like flash erase +and programming. + +Clock stretching is a technique to synchronize host and I2C device. When +I2C device wants to force a delay in the communication, it push "low" the +I2C clock; the I2C controller detects it and waits until I2C clock returns +"high". +Most I2C controllers set a "timeout" for clock stretching, ranging from +few milli-seconds to seconds depending on specific HW or SW driver. + +It is possible that the timeout in your I2C controller is smaller than the +delay required for flash erase or programming. In this case the I2C +controller will timeout and report error to stm32flash. +There is no possibility for stm32flash to retry, so it can only signal the +error and exit. + +To by-pass the issue with bootloader v1.0 you can modify the kernel driver +of your I2C controller. Not an easy job, since every controller has its own +way to handle the timeout. + +In my case I'm using the I2C controller integrated in the VGA port of my +laptop HP EliteBook 8460p. I built the 0.25$ VGA-to-I2C adapter reported in + http://www.paintyourdragon.com/?p=43 +To change the timeout of the I2C controller I had to modify the kernel file + drivers/gpu/drm/radeon/radeon_i2c.c +line 969 +- i2c->bit.timeout = usecs_to_jiffies(2200); /* from VESA */ ++ i2c->bit.timeout = msecs_to_jiffies(5000); /* 5s for STM32 */ +and recompile it. +Then + $> modprobe i2c-dev + $> chmod 666 /dev/i2c-7 + #> stm32flash -a 0x39 /dev/i2c-7 + +2014-09-16 Antonio Borneo diff --git a/tools/win/src/stm32flash_serial/src/Makefile b/tools/win/src/stm32flash_serial/src/Makefile new file mode 100644 index 0000000..0328d55 --- /dev/null +++ b/tools/win/src/stm32flash_serial/src/Makefile @@ -0,0 +1,38 @@ +PREFIX = /usr/local +CFLAGS += -Wall -g + +INSTALL = install + +OBJS = dev_table.o \ + i2c.o \ + init.o \ + main.o \ + port.o \ + serial_common.o \ + serial_platform.o \ + stm32.o \ + utils.o + +LIBOBJS = parsers/parsers.a + +all: stm32flash + +serial_platform.o: serial_posix.c serial_w32.c + +parsers/parsers.a: + cd parsers && $(MAKE) parsers.a + +stm32flash: $(OBJS) $(LIBOBJS) + $(CC) $(LDFLAGS) -o $@ $(OBJS) $(LIBOBJS) + +clean: + rm -f $(OBJS) stm32flash + cd parsers && $(MAKE) $@ + +install: all + $(INSTALL) -d $(DESTDIR)$(PREFIX)/bin + $(INSTALL) -m 755 stm32flash $(DESTDIR)$(PREFIX)/bin + $(INSTALL) -d $(DESTDIR)$(PREFIX)/share/man/man1 + $(INSTALL) -m 644 stm32flash.1 $(DESTDIR)$(PREFIX)/share/man/man1 + +.PHONY: all clean install diff --git a/tools/win/src/stm32flash_serial/src/TODO b/tools/win/src/stm32flash_serial/src/TODO new file mode 100644 index 0000000..41df614 --- /dev/null +++ b/tools/win/src/stm32flash_serial/src/TODO @@ -0,0 +1,7 @@ + +stm32: +- Add support for variable page size + +AUTHORS: +- Add contributors from Geoffrey's commits + diff --git a/tools/win/src/stm32flash_serial/src/dev_table.c b/tools/win/src/stm32flash_serial/src/dev_table.c new file mode 100644 index 0000000..399cd9d --- /dev/null +++ b/tools/win/src/stm32flash_serial/src/dev_table.c @@ -0,0 +1,70 @@ +/* + stm32flash - Open Source ST STM32 flash program for *nix + Copyright (C) 2010 Geoffrey McRae + Copyright (C) 2014 Antonio Borneo + + 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. +*/ + +#include "stm32.h" + +/* + * Device table, corresponds to the "Bootloader device-dependant parameters" + * table in ST document AN2606. + * Note that the option bytes upper range is inclusive! + */ +const stm32_dev_t devices[] = { + /* F0 */ + {0x440, "STM32F051xx" , 0x20001000, 0x20002000, 0x08000000, 0x08010000, 4, 1024, 0x1FFFF800, 0x1FFFF80B, 0x1FFFEC00, 0x1FFFF800}, + {0x444, "STM32F030/F031" , 0x20001000, 0x20002000, 0x08000000, 0x08010000, 4, 1024, 0x1FFFF800, 0x1FFFF80B, 0x1FFFEC00, 0x1FFFF800}, + {0x445, "STM32F042xx" , 0x20001800, 0x20001800, 0x08000000, 0x08008000, 4, 1024, 0x1FFFF800, 0x1FFFF80F, 0x1FFFC400, 0x1FFFF800}, + {0x448, "STM32F072xx" , 0x20001800, 0x20004000, 0x08000000, 0x08020000, 2, 2048, 0x1FFFF800, 0x1FFFF80F, 0x1FFFC800, 0x1FFFF800}, + /* F1 */ + {0x412, "Low-density" , 0x20000200, 0x20002800, 0x08000000, 0x08008000, 4, 1024, 0x1FFFF800, 0x1FFFF80F, 0x1FFFF000, 0x1FFFF800}, + {0x410, "Medium-density" , 0x20000200, 0x20005000, 0x08000000, 0x08020000, 4, 1024, 0x1FFFF800, 0x1FFFF80F, 0x1FFFF000, 0x1FFFF800}, + {0x414, "High-density" , 0x20000200, 0x20010000, 0x08000000, 0x08080000, 2, 2048, 0x1FFFF800, 0x1FFFF80F, 0x1FFFF000, 0x1FFFF800}, + {0x420, "Medium-density VL" , 0x20000200, 0x20002000, 0x08000000, 0x08020000, 4, 1024, 0x1FFFF800, 0x1FFFF80F, 0x1FFFF000, 0x1FFFF800}, + {0x428, "High-density VL" , 0x20000200, 0x20008000, 0x08000000, 0x08080000, 2, 2048, 0x1FFFF800, 0x1FFFF80F, 0x1FFFF000, 0x1FFFF800}, + {0x418, "Connectivity line" , 0x20001000, 0x20010000, 0x08000000, 0x08040000, 2, 2048, 0x1FFFF800, 0x1FFFF80F, 0x1FFFB000, 0x1FFFF800}, + {0x430, "XL-density" , 0x20000800, 0x20018000, 0x08000000, 0x08100000, 2, 2048, 0x1FFFF800, 0x1FFFF80F, 0x1FFFE000, 0x1FFFF800}, + /* Note that F2 and F4 devices have sectors of different page sizes + and only the first sectors (of one page size) are included here */ + /* F2 */ + {0x411, "STM32F2xx" , 0x20002000, 0x20020000, 0x08000000, 0x08100000, 4, 16384, 0x1FFFC000, 0x1FFFC00F, 0x1FFF0000, 0x1FFF77DF}, + /* F3 */ + {0x432, "STM32F373/8" , 0x20001400, 0x20008000, 0x08000000, 0x08040000, 2, 2048, 0x1FFFF800, 0x1FFFF80F, 0x1FFFD800, 0x1FFFF800}, + {0x422, "F302xB/303xB/358" , 0x20001400, 0x20010000, 0x08000000, 0x08040000, 2, 2048, 0x1FFFF800, 0x1FFFF80F, 0x1FFFD800, 0x1FFFF800}, + {0x439, "STM32F302x4(6/8)" , 0x20001800, 0x20004000, 0x08000000, 0x08040000, 2, 2048, 0x1FFFF800, 0x1FFFF80F, 0x1FFFD800, 0x1FFFF800}, + {0x438, "F303x4/334/328" , 0x20001800, 0x20003000, 0x08000000, 0x08040000, 2, 2048, 0x1FFFF800, 0x1FFFF80F, 0x1FFFD800, 0x1FFFF800}, + /* F4 */ + {0x413, "STM32F40/1" , 0x20002000, 0x20020000, 0x08000000, 0x08100000, 4, 16384, 0x1FFFC000, 0x1FFFC00F, 0x1FFF0000, 0x1FFF77DF}, + /* 0x419 is also used for STM32F429/39 but with other bootloader ID... */ + {0x419, "STM32F427/37" , 0x20002000, 0x20030000, 0x08000000, 0x08100000, 4, 16384, 0x1FFFC000, 0x1FFFC00F, 0x1FFF0000, 0x1FFF77FF}, + {0x423, "STM32F401xB(C)" , 0x20003000, 0x20010000, 0x08000000, 0x08100000, 4, 16384, 0x1FFFC000, 0x1FFFC00F, 0x1FFF0000, 0x1FFF77FF}, + {0x433, "STM32F401xD(E)" , 0x20003000, 0x20018000, 0x08000000, 0x08100000, 4, 16384, 0x1FFFC000, 0x1FFFC00F, 0x1FFF0000, 0x1FFF77FF}, + /* L0 */ + {0x417, "L05xxx/06xxx" , 0x20001000, 0x20002000, 0x08000000, 0x08010000, 32, 128, 0x1FF80000, 0x1FF8000F, 0x1FF00000, 0x1FF01000}, + /* L1 */ + {0x416, "L1xxx6(8/B)" , 0x20000800, 0x20004000, 0x08000000, 0x08020000, 16, 256, 0x1FF80000, 0x1FF8000F, 0x1FF00000, 0x1FF01000}, + {0x429, "L1xxx6(8/B)A" , 0x20001000, 0x20008000, 0x08000000, 0x08020000, 16, 256, 0x1FF80000, 0x1FF8000F, 0x1FF00000, 0x1FF01000}, + {0x427, "L1xxxC" , 0x20001000, 0x20008000, 0x08000000, 0x08020000, 16, 256, 0x1FF80000, 0x1FF8000F, 0x1FF00000, 0x1FF02000}, + {0x436, "L1xxxD" , 0x20001000, 0x2000C000, 0x08000000, 0x08060000, 16, 256, 0x1ff80000, 0x1ff8000F, 0x1FF00000, 0x1FF02000}, + {0x437, "L1xxxE" , 0x20001000, 0x20014000, 0x08000000, 0x08060000, 16, 256, 0x1ff80000, 0x1ff8000F, 0x1FF00000, 0x1FF02000}, + /* These are not (yet) in AN2606: */ + {0x641, "Medium_Density PL" , 0x20000200, 0x00005000, 0x08000000, 0x08020000, 4, 1024, 0x1FFFF800, 0x1FFFF80F, 0x1FFFF000, 0x1FFFF800}, + {0x9a8, "STM32W-128K" , 0x20000200, 0x20002000, 0x08000000, 0x08020000, 1, 1024, 0, 0, 0, 0}, + {0x9b0, "STM32W-256K" , 0x20000200, 0x20004000, 0x08000000, 0x08040000, 1, 2048, 0, 0, 0, 0}, + {0x0} +}; diff --git a/tools/win/src/stm32flash_serial/src/gpl-2.0.txt b/tools/win/src/stm32flash_serial/src/gpl-2.0.txt new file mode 100644 index 0000000..d159169 --- /dev/null +++ b/tools/win/src/stm32flash_serial/src/gpl-2.0.txt @@ -0,0 +1,339 @@ + GNU GENERAL PUBLIC LICENSE + Version 2, June 1991 + + Copyright (C) 1989, 1991 Free Software Foundation, Inc., + 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 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 Lesser 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. + + + Copyright (C) + + 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., + 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 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. + + , 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 Lesser General +Public License instead of this License. diff --git a/tools/win/src/stm32flash_serial/src/i2c.c b/tools/win/src/stm32flash_serial/src/i2c.c new file mode 100644 index 0000000..10e6bb1 --- /dev/null +++ b/tools/win/src/stm32flash_serial/src/i2c.c @@ -0,0 +1,209 @@ +/* + stm32flash - Open Source ST STM32 flash program for *nix + Copyright (C) 2014 Antonio Borneo + + 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. +*/ + + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "serial.h" +#include "port.h" + + +#if !defined(__linux__) + +static port_err_t i2c_open(struct port_interface *port, + struct port_options *ops) +{ + return PORT_ERR_NODEV; +} + +struct port_interface port_i2c = { + .name = "i2c", + .open = i2c_open, +}; + +#else + +#ifdef __ANDROID__ +#define I2C_SLAVE 0x0703 /* Use this slave address */ +#define I2C_FUNCS 0x0705 /* Get the adapter functionality mask */ +/* To determine what functionality is present */ +#define I2C_FUNC_I2C 0x00000001 +#else +#include +#include +#endif + +#include + +struct i2c_priv { + int fd; + int addr; +}; + +static port_err_t i2c_open(struct port_interface *port, + struct port_options *ops) +{ + struct i2c_priv *h; + int fd, addr, ret; + unsigned long funcs; + + /* 1. check device name match */ + if (strncmp(ops->device, "/dev/i2c-", strlen("/dev/i2c-"))) + return PORT_ERR_NODEV; + + /* 2. check options */ + addr = ops->bus_addr; + if (addr < 0x03 || addr > 0x77) { + fprintf(stderr, "I2C address out of range [0x03-0x77]\n"); + return PORT_ERR_UNKNOWN; + } + + /* 3. open it */ + h = calloc(sizeof(*h), 1); + if (h == NULL) { + fprintf(stderr, "End of memory\n"); + return PORT_ERR_UNKNOWN; + } + fd = open(ops->device, O_RDWR); + if (fd < 0) { + fprintf(stderr, "Unable to open special file \"%s\"\n", + ops->device); + free(h); + return PORT_ERR_UNKNOWN; + } + + /* 3.5. Check capabilities */ + ret = ioctl(fd, I2C_FUNCS, &funcs); + if (ret < 0) { + fprintf(stderr, "I2C ioctl(funcs) error %d\n", errno); + close(fd); + free(h); + return PORT_ERR_UNKNOWN; + } + if ((funcs & I2C_FUNC_I2C) == 0) { + fprintf(stderr, "Error: controller is not I2C, only SMBUS.\n"); + close(fd); + free(h); + return PORT_ERR_UNKNOWN; + } + + /* 4. set options */ + ret = ioctl(fd, I2C_SLAVE, addr); + if (ret < 0) { + fprintf(stderr, "I2C ioctl(slave) error %d\n", errno); + close(fd); + free(h); + return PORT_ERR_UNKNOWN; + } + + h->fd = fd; + h->addr = addr; + port->private = h; + return PORT_ERR_OK; +} + +static port_err_t i2c_close(struct port_interface *port) +{ + struct i2c_priv *h; + + h = (struct i2c_priv *)port->private; + if (h == NULL) + return PORT_ERR_UNKNOWN; + close(h->fd); + free(h); + port->private = NULL; + return PORT_ERR_OK; +} + +static port_err_t i2c_read(struct port_interface *port, void *buf, + size_t nbyte) +{ + struct i2c_priv *h; + int ret; + + h = (struct i2c_priv *)port->private; + if (h == NULL) + return PORT_ERR_UNKNOWN; + ret = read(h->fd, buf, nbyte); + if (ret != nbyte) + return PORT_ERR_UNKNOWN; + return PORT_ERR_OK; +} + +static port_err_t i2c_write(struct port_interface *port, void *buf, + size_t nbyte) +{ + struct i2c_priv *h; + int ret; + + h = (struct i2c_priv *)port->private; + if (h == NULL) + return PORT_ERR_UNKNOWN; + ret = write(h->fd, buf, nbyte); + if (ret != nbyte) + return PORT_ERR_UNKNOWN; + return PORT_ERR_OK; +} + +static port_err_t i2c_gpio(struct port_interface *port, serial_gpio_t n, + int level) +{ + return PORT_ERR_OK; +} + +static const char *i2c_get_cfg_str(struct port_interface *port) +{ + struct i2c_priv *h; + static char str[11]; + + h = (struct i2c_priv *)port->private; + if (h == NULL) + return "INVALID"; + snprintf(str, sizeof(str), "addr 0x%2x", h->addr); + return str; +} + +static struct varlen_cmd i2c_cmd_get_reply[] = { + {0x10, 11}, + {0x11, 17}, + {0x12, 18}, + { /* sentinel */ } +}; + +struct port_interface port_i2c = { + .name = "i2c", + .flags = PORT_STRETCH_W, + .open = i2c_open, + .close = i2c_close, + .read = i2c_read, + .write = i2c_write, + .gpio = i2c_gpio, + .cmd_get_reply = i2c_cmd_get_reply, + .get_cfg_str = i2c_get_cfg_str, +}; + +#endif diff --git a/tools/win/src/stm32flash_serial/src/init.c b/tools/win/src/stm32flash_serial/src/init.c new file mode 100644 index 0000000..77a571b --- /dev/null +++ b/tools/win/src/stm32flash_serial/src/init.c @@ -0,0 +1,219 @@ +/* + stm32flash - Open Source ST STM32 flash program for *nix + Copyright (C) 2010 Geoffrey McRae + Copyright (C) 2013 Antonio Borneo + + 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. +*/ + + +#include +#include +#include +#include +#include +#include +#include +#include +#include "init.h" +#include "serial.h" +#include "stm32.h" +#include "port.h" + +struct gpio_list { + struct gpio_list *next; + int gpio; +}; + + +static int write_to(const char *filename, const char *value) +{ + int fd, ret; + + fd = open(filename, O_WRONLY); + if (fd < 0) { + fprintf(stderr, "Cannot open file \"%s\"\n", filename); + return 0; + } + ret = write(fd, value, strlen(value)); + if (ret < 0) { + fprintf(stderr, "Error writing in file \"%s\"\n", filename); + close(fd); + return 0; + } + close(fd); + return 1; +} + +#if !defined(__linux__) +static int drive_gpio(int n, int level, struct gpio_list **gpio_to_release) +{ + fprintf(stderr, "GPIO control only available in Linux\n"); + return 0; +} +#else +static int drive_gpio(int n, int level, struct gpio_list **gpio_to_release) +{ + char num[16]; /* sized to carry MAX_INT */ + char file[48]; /* sized to carry longest filename */ + struct stat buf; + struct gpio_list *new; + int ret; + + sprintf(file, "/sys/class/gpio/gpio%d/direction", n); + ret = stat(file, &buf); + if (ret) { + /* file miss, GPIO not exported yet */ + sprintf(num, "%d", n); + ret = write_to("/sys/class/gpio/export", num); + if (!ret) + return 0; + ret = stat(file, &buf); + if (ret) { + fprintf(stderr, "GPIO %d not available\n", n); + return 0; + } + new = (struct gpio_list *)malloc(sizeof(struct gpio_list)); + if (new == NULL) { + fprintf(stderr, "Out of memory\n"); + return 0; + } + new->gpio = n; + new->next = *gpio_to_release; + *gpio_to_release = new; + } + + return write_to(file, level ? "high" : "low"); +} +#endif + +static int release_gpio(int n) +{ + char num[16]; /* sized to carry MAX_INT */ + + sprintf(num, "%d", n); + return write_to("/sys/class/gpio/unexport", num); +} + +static int gpio_sequence(struct port_interface *port, const char *s, size_t l) +{ + struct gpio_list *gpio_to_release = NULL, *to_free; + int ret, level, gpio; + + ret = 1; + while (ret == 1 && *s && l > 0) { + if (*s == '-') { + level = 0; + s++; + l--; + } else + level = 1; + + if (isdigit(*s)) { + gpio = atoi(s); + while (isdigit(*s)) { + s++; + l--; + } + } else if (!strncmp(s, "rts", 3)) { + gpio = -GPIO_RTS; + s += 3; + l -= 3; + } else if (!strncmp(s, "dtr", 3)) { + gpio = -GPIO_DTR; + s += 3; + l -= 3; + } else if (!strncmp(s, "brk", 3)) { + gpio = -GPIO_BRK; + s += 3; + l -= 3; + } else { + fprintf(stderr, "Character \'%c\' is not a digit\n", *s); + ret = 0; + break; + } + + if (*s && (l > 0)) { + if (*s == ',') { + s++; + l--; + } else { + fprintf(stderr, "Character \'%c\' is not a separator\n", *s); + ret = 0; + break; + } + } + if (gpio < 0) + ret = (port->gpio(port, -gpio, level) == PORT_ERR_OK); + else + ret = drive_gpio(gpio, level, &gpio_to_release); + usleep(100000); + } + + while (gpio_to_release) { + release_gpio(gpio_to_release->gpio); + to_free = gpio_to_release; + gpio_to_release = gpio_to_release->next; + free(to_free); + } + usleep(500000); + return ret; +} + +static int gpio_bl_entry(struct port_interface *port, const char *seq) +{ + char *s; + + if (seq == NULL || seq[0] == ':') + return 1; + + s = strchr(seq, ':'); + if (s == NULL) + return gpio_sequence(port, seq, strlen(seq)); + + return gpio_sequence(port, seq, s - seq); +} + +static int gpio_bl_exit(struct port_interface *port, const char *seq) +{ + char *s; + + if (seq == NULL) + return 1; + + s = strchr(seq, ':'); + if (s == NULL || s[1] == '\0') + return 1; + + return gpio_sequence(port, s + 1, strlen(s + 1)); +} + +int init_bl_entry(struct port_interface *port, const char *seq) +{ + if (seq) + return gpio_bl_entry(port, seq); + + return 1; +} + +int init_bl_exit(stm32_t *stm, struct port_interface *port, const char *seq) +{ + if (seq) + return gpio_bl_exit(port, seq); + + if (stm32_reset_device(stm) != STM32_ERR_OK) + return 0; + return 1; +} diff --git a/tools/win/src/stm32flash_serial/src/init.h b/tools/win/src/stm32flash_serial/src/init.h new file mode 100644 index 0000000..6075b51 --- /dev/null +++ b/tools/win/src/stm32flash_serial/src/init.h @@ -0,0 +1,31 @@ +/* + stm32flash - Open Source ST STM32 flash program for *nix + Copyright (C) 2010 Geoffrey McRae + Copyright (C) 2013 Antonio Borneo + + 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. +*/ + + +#ifndef _INIT_H +#define _INIT_H + +#include "stm32.h" +#include "port.h" + +int init_bl_entry(struct port_interface *port, const char *seq); +int init_bl_exit(stm32_t *stm, struct port_interface *port, const char *seq); + +#endif diff --git a/tools/win/src/stm32flash_serial/src/main.c b/tools/win/src/stm32flash_serial/src/main.c new file mode 100644 index 0000000..f081d61 --- /dev/null +++ b/tools/win/src/stm32flash_serial/src/main.c @@ -0,0 +1,774 @@ +/* + stm32flash - Open Source ST STM32 flash program for *nix + Copyright 2010 Geoffrey McRae + Copyright 2011 Steve Markgraf + Copyright 2012 Tormod Volden + Copyright 2013 Antonio Borneo + + 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. +*/ + +#include +#include +#include +#include +#include +#include +#include + +#include "init.h" +#include "utils.h" +#include "serial.h" +#include "stm32.h" +#include "parsers/parser.h" +#include "port.h" + +#include "parsers/binary.h" +#include "parsers/hex.h" + +#define VERSION "Arduino_STM32_0.9" + +/* device globals */ +stm32_t *stm = NULL; + +void *p_st = NULL; +parser_t *parser = NULL; + +/* settings */ +struct port_options port_opts = { + .device = NULL, + .baudRate = SERIAL_BAUD_57600, + .serial_mode = "8e1", + .bus_addr = 0, + .rx_frame_max = STM32_MAX_RX_FRAME, + .tx_frame_max = STM32_MAX_TX_FRAME, +}; +int rd = 0; +int wr = 0; +int wu = 0; +int rp = 0; +int ur = 0; +int eraseOnly = 0; +int crc = 0; +int npages = 0; +int spage = 0; +int no_erase = 0; +char verify = 0; +int retry = 10; +char exec_flag = 0; +uint32_t execute = 0; +char init_flag = 1; +char force_binary = 0; +char reset_flag = 0; +char *filename; +char *gpio_seq = NULL; +uint32_t start_addr = 0; +uint32_t readwrite_len = 0; + +/* functions */ +int parse_options(int argc, char *argv[]); +void show_help(char *name); + +static int is_addr_in_ram(uint32_t addr) +{ + return addr >= stm->dev->ram_start && addr < stm->dev->ram_end; +} + +static int is_addr_in_flash(uint32_t addr) +{ + return addr >= stm->dev->fl_start && addr < stm->dev->fl_end; +} + +static int flash_addr_to_page_floor(uint32_t addr) +{ + if (!is_addr_in_flash(addr)) + return 0; + + return (addr - stm->dev->fl_start) / stm->dev->fl_ps; +} + +static int flash_addr_to_page_ceil(uint32_t addr) +{ + if (!(addr >= stm->dev->fl_start && addr <= stm->dev->fl_end)) + return 0; + + return (addr + stm->dev->fl_ps - 1 - stm->dev->fl_start) + / stm->dev->fl_ps; +} + +static uint32_t flash_page_to_addr(int page) +{ + return stm->dev->fl_start + page * stm->dev->fl_ps; +} + +int main(int argc, char* argv[]) { + struct port_interface *port = NULL; + int ret = 1; + stm32_err_t s_err; + parser_err_t perr; + FILE *diag = stdout; + + fprintf(diag, "stm32flash " VERSION "\n\n"); + fprintf(diag, "http://github.com/rogerclarkmelbourne/arduino_stm32\n\n"); + if (parse_options(argc, argv) != 0) + goto close; + + if (rd && filename[0] == '-') { + diag = stderr; + } + + if (wr) { + /* first try hex */ + if (!force_binary) { + parser = &PARSER_HEX; + p_st = parser->init(); + if (!p_st) { + fprintf(stderr, "%s Parser failed to initialize\n", parser->name); + goto close; + } + } + + if (force_binary || (perr = parser->open(p_st, filename, 0)) != PARSER_ERR_OK) { + if (force_binary || perr == PARSER_ERR_INVALID_FILE) { + if (!force_binary) { + parser->close(p_st); + p_st = NULL; + } + + /* now try binary */ + parser = &PARSER_BINARY; + p_st = parser->init(); + if (!p_st) { + fprintf(stderr, "%s Parser failed to initialize\n", parser->name); + goto close; + } + perr = parser->open(p_st, filename, 0); + } + + /* if still have an error, fail */ + if (perr != PARSER_ERR_OK) { + fprintf(stderr, "%s ERROR: %s\n", parser->name, parser_errstr(perr)); + if (perr == PARSER_ERR_SYSTEM) perror(filename); + goto close; + } + } + + fprintf(diag, "Using Parser : %s\n", parser->name); + } else { + parser = &PARSER_BINARY; + p_st = parser->init(); + if (!p_st) { + fprintf(stderr, "%s Parser failed to initialize\n", parser->name); + goto close; + } + } + + if (port_open(&port_opts, &port) != PORT_ERR_OK) { + fprintf(stderr, "Failed to open port: %s\n", port_opts.device); + goto close; + } + + fprintf(diag, "Interface %s: %s\n", port->name, port->get_cfg_str(port)); + if (init_flag && init_bl_entry(port, gpio_seq) == 0) + goto close; + stm = stm32_init(port, init_flag); + if (!stm) + goto close; + + fprintf(diag, "Version : 0x%02x\n", stm->bl_version); + if (port->flags & PORT_GVR_ETX) { + fprintf(diag, "Option 1 : 0x%02x\n", stm->option1); + fprintf(diag, "Option 2 : 0x%02x\n", stm->option2); + } + fprintf(diag, "Device ID : 0x%04x (%s)\n", stm->pid, stm->dev->name); + fprintf(diag, "- RAM : %dKiB (%db reserved by bootloader)\n", (stm->dev->ram_end - 0x20000000) / 1024, stm->dev->ram_start - 0x20000000); + fprintf(diag, "- Flash : %dKiB (sector size: %dx%d)\n", (stm->dev->fl_end - stm->dev->fl_start ) / 1024, stm->dev->fl_pps, stm->dev->fl_ps); + fprintf(diag, "- Option RAM : %db\n", stm->dev->opt_end - stm->dev->opt_start + 1); + fprintf(diag, "- System RAM : %dKiB\n", (stm->dev->mem_end - stm->dev->mem_start) / 1024); + + uint8_t buffer[256]; + uint32_t addr, start, end; + unsigned int len; + int failed = 0; + int first_page, num_pages; + + /* + * Cleanup addresses: + * + * Starting from options + * start_addr, readwrite_len, spage, npages + * and using device memory size, compute + * start, end, first_page, num_pages + */ + if (start_addr || readwrite_len) { + start = start_addr; + + if (is_addr_in_flash(start)) + end = stm->dev->fl_end; + else { + no_erase = 1; + if (is_addr_in_ram(start)) + end = stm->dev->ram_end; + else + end = start + sizeof(uint32_t); + } + + if (readwrite_len && (end > start + readwrite_len)) + end = start + readwrite_len; + + first_page = flash_addr_to_page_floor(start); + if (!first_page && end == stm->dev->fl_end) + num_pages = 0xff; /* mass erase */ + else + num_pages = flash_addr_to_page_ceil(end) - first_page; + } else if (!spage && !npages) { + start = stm->dev->fl_start; + end = stm->dev->fl_end; + first_page = 0; + num_pages = 0xff; /* mass erase */ + } else { + first_page = spage; + start = flash_page_to_addr(first_page); + if (start > stm->dev->fl_end) { + fprintf(stderr, "Address range exceeds flash size.\n"); + goto close; + } + + if (npages) { + num_pages = npages; + end = flash_page_to_addr(first_page + num_pages); + if (end > stm->dev->fl_end) + end = stm->dev->fl_end; + } else { + end = stm->dev->fl_end; + num_pages = flash_addr_to_page_ceil(end) - first_page; + } + + if (!first_page && end == stm->dev->fl_end) + num_pages = 0xff; /* mass erase */ + } + + if (rd) { + unsigned int max_len = port_opts.rx_frame_max; + + fprintf(diag, "Memory read\n"); + + perr = parser->open(p_st, filename, 1); + if (perr != PARSER_ERR_OK) { + fprintf(stderr, "%s ERROR: %s\n", parser->name, parser_errstr(perr)); + if (perr == PARSER_ERR_SYSTEM) + perror(filename); + goto close; + } + + fflush(diag); + addr = start; + while(addr < end) { + uint32_t left = end - addr; + len = max_len > left ? left : max_len; + s_err = stm32_read_memory(stm, addr, buffer, len); + if (s_err != STM32_ERR_OK) { + fprintf(stderr, "Failed to read memory at address 0x%08x, target write-protected?\n", addr); + goto close; + } + if (parser->write(p_st, buffer, len) != PARSER_ERR_OK) + { + fprintf(stderr, "Failed to write data to file\n"); + goto close; + } + addr += len; + + fprintf(diag, + "\rRead address 0x%08x (%.2f%%) ", + addr, + (100.0f / (float)(end - start)) * (float)(addr - start) + ); + fflush(diag); + } + fprintf(diag, "Done.\n"); + ret = 0; + goto close; + } else if (rp) { + fprintf(stdout, "Read-Protecting flash\n"); + /* the device automatically performs a reset after the sending the ACK */ + reset_flag = 0; + stm32_readprot_memory(stm); + fprintf(stdout, "Done.\n"); + } else if (ur) { + fprintf(stdout, "Read-UnProtecting flash\n"); + /* the device automatically performs a reset after the sending the ACK */ + reset_flag = 0; + stm32_runprot_memory(stm); + fprintf(stdout, "Done.\n"); + } else if (eraseOnly) { + ret = 0; + fprintf(stdout, "Erasing flash\n"); + + if (num_pages != 0xff && + (start != flash_page_to_addr(first_page) + || end != flash_page_to_addr(first_page + num_pages))) { + fprintf(stderr, "Specified start & length are invalid (must be page aligned)\n"); + ret = 1; + goto close; + } + + s_err = stm32_erase_memory(stm, first_page, num_pages); + if (s_err != STM32_ERR_OK) { + fprintf(stderr, "Failed to erase memory\n"); + ret = 1; + goto close; + } + } else if (wu) { + fprintf(diag, "Write-unprotecting flash\n"); + /* the device automatically performs a reset after the sending the ACK */ + reset_flag = 0; + stm32_wunprot_memory(stm); + fprintf(diag, "Done.\n"); + + } else if (wr) { + fprintf(diag, "Write to memory\n"); + + off_t offset = 0; + ssize_t r; + unsigned int size; + unsigned int max_wlen, max_rlen; + + max_wlen = port_opts.tx_frame_max - 2; /* skip len and crc */ + max_wlen &= ~3; /* 32 bit aligned */ + + max_rlen = port_opts.rx_frame_max; + max_rlen = max_rlen < max_wlen ? max_rlen : max_wlen; + + /* Assume data from stdin is whole device */ + if (filename[0] == '-' && filename[1] == '\0') + size = end - start; + else + size = parser->size(p_st); + + // TODO: It is possible to write to non-page boundaries, by reading out flash + // from partial pages and combining with the input data + // if ((start % stm->dev->fl_ps) != 0 || (end % stm->dev->fl_ps) != 0) { + // fprintf(stderr, "Specified start & length are invalid (must be page aligned)\n"); + // goto close; + // } + + // TODO: If writes are not page aligned, we should probably read out existing flash + // contents first, so it can be preserved and combined with new data + if (!no_erase && num_pages) { + fprintf(diag, "Erasing memory\n"); + s_err = stm32_erase_memory(stm, first_page, num_pages); + if (s_err != STM32_ERR_OK) { + fprintf(stderr, "Failed to erase memory\n"); + goto close; + } + } + + fflush(diag); + addr = start; + while(addr < end && offset < size) { + uint32_t left = end - addr; + len = max_wlen > left ? left : max_wlen; + len = len > size - offset ? size - offset : len; + + if (parser->read(p_st, buffer, &len) != PARSER_ERR_OK) + goto close; + + if (len == 0) { + if (filename[0] == '-') { + break; + } else { + fprintf(stderr, "Failed to read input file\n"); + goto close; + } + } + + again: + s_err = stm32_write_memory(stm, addr, buffer, len); + if (s_err != STM32_ERR_OK) { + fprintf(stderr, "Failed to write memory at address 0x%08x\n", addr); + goto close; + } + + if (verify) { + uint8_t compare[len]; + unsigned int offset, rlen; + + offset = 0; + while (offset < len) { + rlen = len - offset; + rlen = rlen < max_rlen ? rlen : max_rlen; + s_err = stm32_read_memory(stm, addr + offset, compare + offset, rlen); + if (s_err != STM32_ERR_OK) { + fprintf(stderr, "Failed to read memory at address 0x%08x\n", addr + offset); + goto close; + } + offset += rlen; + } + + for(r = 0; r < len; ++r) + if (buffer[r] != compare[r]) { + if (failed == retry) { + fprintf(stderr, "Failed to verify at address 0x%08x, expected 0x%02x and found 0x%02x\n", + (uint32_t)(addr + r), + buffer [r], + compare[r] + ); + goto close; + } + ++failed; + goto again; + } + + failed = 0; + } + + addr += len; + offset += len; + + fprintf(diag, + "\rWrote %saddress 0x%08x (%.2f%%) ", + verify ? "and verified " : "", + addr, + (100.0f / size) * offset + ); + fflush(diag); + + } + + fprintf(diag, "Done.\n"); + ret = 0; + goto close; + } else if (crc) { + uint32_t crc_val = 0; + + fprintf(diag, "CRC computation\n"); + + s_err = stm32_crc_wrapper(stm, start, end - start, &crc_val); + if (s_err != STM32_ERR_OK) { + fprintf(stderr, "Failed to read CRC\n"); + goto close; + } + fprintf(diag, "CRC(0x%08x-0x%08x) = 0x%08x\n", start, end, + crc_val); + ret = 0; + goto close; + } else + ret = 0; + +close: + if (stm && exec_flag && ret == 0) { + if (execute == 0) + execute = stm->dev->fl_start; + + fprintf(diag, "\nStarting execution at address 0x%08x... ", execute); + fflush(diag); + if (stm32_go(stm, execute) == STM32_ERR_OK) { + reset_flag = 0; + fprintf(diag, "done.\n"); + } else + fprintf(diag, "failed.\n"); + } + + if (stm && reset_flag) { + fprintf(diag, "\nResetting device... "); + fflush(diag); + if (init_bl_exit(stm, port, gpio_seq)) + fprintf(diag, "done.\n"); + else fprintf(diag, "failed.\n"); + } + + if (p_st ) parser->close(p_st); + if (stm ) stm32_close (stm); + if (port) + port->close(port); + + fprintf(diag, "\n"); + return ret; +} + +int parse_options(int argc, char *argv[]) +{ + int c; + char *pLen; + + while ((c = getopt(argc, argv, "a:b:m:r:w:e:vn:g:jkfcChuos:S:F:i:R")) != -1) { + switch(c) { + case 'a': + port_opts.bus_addr = strtoul(optarg, NULL, 0); + break; + + case 'b': + port_opts.baudRate = serial_get_baud(strtoul(optarg, NULL, 0)); + if (port_opts.baudRate == SERIAL_BAUD_INVALID) { + serial_baud_t baudrate; + fprintf(stderr, "Invalid baud rate, valid options are:\n"); + for (baudrate = SERIAL_BAUD_1200; baudrate != SERIAL_BAUD_INVALID; ++baudrate) + fprintf(stderr, " %d\n", serial_get_baud_int(baudrate)); + return 1; + } + break; + + case 'm': + if (strlen(optarg) != 3 + || serial_get_bits(optarg) == SERIAL_BITS_INVALID + || serial_get_parity(optarg) == SERIAL_PARITY_INVALID + || serial_get_stopbit(optarg) == SERIAL_STOPBIT_INVALID) { + fprintf(stderr, "Invalid serial mode\n"); + return 1; + } + port_opts.serial_mode = optarg; + break; + + case 'r': + case 'w': + rd = rd || c == 'r'; + wr = wr || c == 'w'; + if (rd && wr) { + fprintf(stderr, "ERROR: Invalid options, can't read & write at the same time\n"); + return 1; + } + filename = optarg; + if (filename[0] == '-') { + force_binary = 1; + } + break; + case 'e': + if (readwrite_len || start_addr) { + fprintf(stderr, "ERROR: Invalid options, can't specify start page / num pages and start address/length\n"); + return 1; + } + npages = strtoul(optarg, NULL, 0); + if (npages > 0xFF || npages < 0) { + fprintf(stderr, "ERROR: You need to specify a page count between 0 and 255"); + return 1; + } + if (!npages) + no_erase = 1; + break; + case 'u': + wu = 1; + if (rd || wr) { + fprintf(stderr, "ERROR: Invalid options, can't write unprotect and read/write at the same time\n"); + return 1; + } + break; + + case 'j': + rp = 1; + if (rd || wr) { + fprintf(stderr, "ERROR: Invalid options, can't read protect and read/write at the same time\n"); + return 1; + } + break; + + case 'k': + ur = 1; + if (rd || wr) { + fprintf(stderr, "ERROR: Invalid options, can't read unprotect and read/write at the same time\n"); + return 1; + } + break; + + case 'o': + eraseOnly = 1; + if (rd || wr) { + fprintf(stderr, "ERROR: Invalid options, can't erase-only and read/write at the same time\n"); + return 1; + } + break; + + case 'v': + verify = 1; + break; + + case 'n': + retry = strtoul(optarg, NULL, 0); + break; + + case 'g': + exec_flag = 1; + execute = strtoul(optarg, NULL, 0); + if (execute % 4 != 0) { + fprintf(stderr, "ERROR: Execution address must be word-aligned\n"); + return 1; + } + break; + case 's': + if (readwrite_len || start_addr) { + fprintf(stderr, "ERROR: Invalid options, can't specify start page / num pages and start address/length\n"); + return 1; + } + spage = strtoul(optarg, NULL, 0); + break; + case 'S': + if (spage || npages) { + fprintf(stderr, "ERROR: Invalid options, can't specify start page / num pages and start address/length\n"); + return 1; + } else { + start_addr = strtoul(optarg, &pLen, 0); + if (*pLen == ':') { + pLen++; + readwrite_len = strtoul(pLen, NULL, 0); + if (readwrite_len == 0) { + fprintf(stderr, "ERROR: Invalid options, can't specify zero length\n"); + return 1; + } + } + } + break; + case 'F': + port_opts.rx_frame_max = strtoul(optarg, &pLen, 0); + if (*pLen == ':') { + pLen++; + port_opts.tx_frame_max = strtoul(pLen, NULL, 0); + } + if (port_opts.rx_frame_max < 0 + || port_opts.tx_frame_max < 0) { + fprintf(stderr, "ERROR: Invalid negative value for option -F\n"); + return 1; + } + if (port_opts.rx_frame_max == 0) + port_opts.rx_frame_max = STM32_MAX_RX_FRAME; + if (port_opts.tx_frame_max == 0) + port_opts.tx_frame_max = STM32_MAX_TX_FRAME; + if (port_opts.rx_frame_max < 20 + || port_opts.tx_frame_max < 5) { + fprintf(stderr, "ERROR: current code cannot work with small frames.\n"); + fprintf(stderr, "min(RX) = 20, min(TX) = 5\n"); + return 1; + } + if (port_opts.rx_frame_max > STM32_MAX_RX_FRAME) { + fprintf(stderr, "WARNING: Ignore RX length in option -F\n"); + port_opts.rx_frame_max = STM32_MAX_RX_FRAME; + } + if (port_opts.tx_frame_max > STM32_MAX_TX_FRAME) { + fprintf(stderr, "WARNING: Ignore TX length in option -F\n"); + port_opts.tx_frame_max = STM32_MAX_TX_FRAME; + } + break; + case 'f': + force_binary = 1; + break; + + case 'c': + init_flag = 0; + break; + + case 'h': + show_help(argv[0]); + exit(0); + + case 'i': + gpio_seq = optarg; + break; + + case 'R': + reset_flag = 1; + break; + + case 'C': + crc = 1; + break; + } + } + + for (c = optind; c < argc; ++c) { + if (port_opts.device) { + fprintf(stderr, "ERROR: Invalid parameter specified\n"); + show_help(argv[0]); + return 1; + } + port_opts.device = argv[c]; + } + + if (port_opts.device == NULL) { + fprintf(stderr, "ERROR: Device not specified\n"); + show_help(argv[0]); + return 1; + } + + if (!wr && verify) { + fprintf(stderr, "ERROR: Invalid usage, -v is only valid when writing\n"); + show_help(argv[0]); + return 1; + } + + return 0; +} + +void show_help(char *name) { + fprintf(stderr, + "Usage: %s [-bvngfhc] [-[rw] filename] [tty_device | i2c_device]\n" + " -a bus_address Bus address (e.g. for I2C port)\n" + " -b rate Baud rate (default 57600)\n" + " -m mode Serial port mode (default 8e1)\n" + " -r filename Read flash to file (or - stdout)\n" + " -w filename Write flash from file (or - stdout)\n" + " -C Compute CRC of flash content\n" + " -u Disable the flash write-protection\n" + " -j Enable the flash read-protection\n" + " -k Disable the flash read-protection\n" + " -o Erase only\n" + " -e n Only erase n pages before writing the flash\n" + " -v Verify writes\n" + " -n count Retry failed writes up to count times (default 10)\n" + " -g address Start execution at specified address (0 = flash start)\n" + " -S address[:length] Specify start address and optionally length for\n" + " read/write/erase operations\n" + " -F RX_length[:TX_length] Specify the max length of RX and TX frame\n" + " -s start_page Flash at specified page (0 = flash start)\n" + " -f Force binary parser\n" + " -h Show this help\n" + " -c Resume the connection (don't send initial INIT)\n" + " *Baud rate must be kept the same as the first init*\n" + " This is useful if the reset fails\n" + " -i GPIO_string GPIO sequence to enter/exit bootloader mode\n" + " GPIO_string=[entry_seq][:[exit_seq]]\n" + " sequence=[-]n[,sequence]\n" + " -R Reset device at exit.\n" + "\n" + "Examples:\n" + " Get device information:\n" + " %s /dev/ttyS0\n" + " or:\n" + " %s /dev/i2c-0\n" + "\n" + " Write with verify and then start execution:\n" + " %s -w filename -v -g 0x0 /dev/ttyS0\n" + "\n" + " Read flash to file:\n" + " %s -r filename /dev/ttyS0\n" + "\n" + " Read 100 bytes of flash from 0x1000 to stdout:\n" + " %s -r - -S 0x1000:100 /dev/ttyS0\n" + "\n" + " Start execution:\n" + " %s -g 0x0 /dev/ttyS0\n" + "\n" + " GPIO sequence:\n" + " - entry sequence: GPIO_3=low, GPIO_2=low, GPIO_2=high\n" + " - exit sequence: GPIO_3=high, GPIO_2=low, GPIO_2=high\n" + " %s -i -3,-2,2:3,-2,2 /dev/ttyS0\n", + name, + name, + name, + name, + name, + name, + name, + name + ); +} + diff --git a/tools/win/src/stm32flash_serial/src/parsers/Android.mk b/tools/win/src/stm32flash_serial/src/parsers/Android.mk new file mode 100644 index 0000000..afec18c --- /dev/null +++ b/tools/win/src/stm32flash_serial/src/parsers/Android.mk @@ -0,0 +1,6 @@ +LOCAL_PATH := $(call my-dir) + +include $(CLEAR_VARS) +LOCAL_MODULE := libparsers +LOCAL_SRC_FILES := binary.c hex.c +include $(BUILD_STATIC_LIBRARY) diff --git a/tools/win/src/stm32flash_serial/src/parsers/Makefile b/tools/win/src/stm32flash_serial/src/parsers/Makefile new file mode 100644 index 0000000..bb7df1e --- /dev/null +++ b/tools/win/src/stm32flash_serial/src/parsers/Makefile @@ -0,0 +1,12 @@ + +CFLAGS += -Wall -g + +all: parsers.a + +parsers.a: binary.o hex.o + $(AR) rc $@ binary.o hex.o + +clean: + rm -f *.o parsers.a + +.PHONY: all clean diff --git a/tools/win/src/stm32flash_serial/src/parsers/binary.c b/tools/win/src/stm32flash_serial/src/parsers/binary.c new file mode 100644 index 0000000..f491952 --- /dev/null +++ b/tools/win/src/stm32flash_serial/src/parsers/binary.c @@ -0,0 +1,140 @@ +/* + stm32flash - Open Source ST STM32 flash program for *nix + Copyright (C) 2010 Geoffrey McRae + + 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. +*/ + + +#include +#include +#include +#include +#include + +#include "binary.h" + +typedef struct { + int fd; + char write; + struct stat stat; +} binary_t; + +void* binary_init() { + return calloc(sizeof(binary_t), 1); +} + +parser_err_t binary_open(void *storage, const char *filename, const char write) { + binary_t *st = storage; + if (write) { + if (filename[0] == '-') + st->fd = 1; + else + st->fd = open( + filename, +#ifndef __WIN32__ + O_WRONLY | O_CREAT | O_TRUNC, +#else + O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, +#endif +#ifndef __WIN32__ + S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH +#else + 0 +#endif + ); + st->stat.st_size = 0; + } else { + if (filename[0] == '-') { + st->fd = 0; + } else { + if (stat(filename, &st->stat) != 0) + return PARSER_ERR_INVALID_FILE; + st->fd = open(filename, +#ifndef __WIN32__ + O_RDONLY +#else + O_RDONLY | O_BINARY +#endif + ); + } + } + + st->write = write; + return st->fd == -1 ? PARSER_ERR_SYSTEM : PARSER_ERR_OK; +} + +parser_err_t binary_close(void *storage) { + binary_t *st = storage; + + if (st->fd) close(st->fd); + free(st); + return PARSER_ERR_OK; +} + +unsigned int binary_size(void *storage) { + binary_t *st = storage; + return st->stat.st_size; +} + +parser_err_t binary_read(void *storage, void *data, unsigned int *len) { + binary_t *st = storage; + unsigned int left = *len; + if (st->write) return PARSER_ERR_WRONLY; + + ssize_t r; + while(left > 0) { + r = read(st->fd, data, left); + /* If there is no data to read at all, return OK, but with zero read */ + if (r == 0 && left == *len) { + *len = 0; + return PARSER_ERR_OK; + } + if (r <= 0) return PARSER_ERR_SYSTEM; + left -= r; + data += r; + } + + *len = *len - left; + return PARSER_ERR_OK; +} + +parser_err_t binary_write(void *storage, void *data, unsigned int len) { + binary_t *st = storage; + if (!st->write) return PARSER_ERR_RDONLY; + + ssize_t r; + while(len > 0) { + r = write(st->fd, data, len); + if (r < 1) return PARSER_ERR_SYSTEM; + st->stat.st_size += r; + + len -= r; + data += r; + } + + return PARSER_ERR_OK; +} + +parser_t PARSER_BINARY = { + "Raw BINARY", + binary_init, + binary_open, + binary_close, + binary_size, + binary_read, + binary_write +}; + diff --git a/tools/win/src/stm32flash_serial/src/parsers/binary.h b/tools/win/src/stm32flash_serial/src/parsers/binary.h new file mode 100644 index 0000000..d989acf --- /dev/null +++ b/tools/win/src/stm32flash_serial/src/parsers/binary.h @@ -0,0 +1,27 @@ +/* + stm32flash - Open Source ST STM32 flash program for *nix + Copyright (C) 2010 Geoffrey McRae + + 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. +*/ + + +#ifndef _PARSER_BINARY_H +#define _PARSER_BINARY_H + +#include "parser.h" + +extern parser_t PARSER_BINARY; +#endif diff --git a/tools/win/src/stm32flash_serial/src/parsers/hex.c b/tools/win/src/stm32flash_serial/src/parsers/hex.c new file mode 100644 index 0000000..3baf856 --- /dev/null +++ b/tools/win/src/stm32flash_serial/src/parsers/hex.c @@ -0,0 +1,224 @@ +/* + stm32flash - Open Source ST STM32 flash program for *nix + Copyright (C) 2010 Geoffrey McRae + + 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. +*/ + + +#include +#include +#include +#include +#include +#include +#include + +#include "hex.h" +#include "../utils.h" + +typedef struct { + size_t data_len, offset; + uint8_t *data; + uint8_t base; +} hex_t; + +void* hex_init() { + return calloc(sizeof(hex_t), 1); +} + +parser_err_t hex_open(void *storage, const char *filename, const char write) { + hex_t *st = storage; + if (write) { + return PARSER_ERR_RDONLY; + } else { + char mark; + int i, fd; + uint8_t checksum; + unsigned int c; + uint32_t base = 0; + unsigned int last_address = 0x0; + + fd = open(filename, O_RDONLY); + if (fd < 0) + return PARSER_ERR_SYSTEM; + + /* read in the file */ + + while(read(fd, &mark, 1) != 0) { + if (mark == '\n' || mark == '\r') continue; + if (mark != ':') + return PARSER_ERR_INVALID_FILE; + + char buffer[9]; + unsigned int reclen, address, type; + uint8_t *record = NULL; + + /* get the reclen, address, and type */ + buffer[8] = 0; + if (read(fd, &buffer, 8) != 8) return PARSER_ERR_INVALID_FILE; + if (sscanf(buffer, "%2x%4x%2x", &reclen, &address, &type) != 3) { + close(fd); + return PARSER_ERR_INVALID_FILE; + } + + /* setup the checksum */ + checksum = + reclen + + ((address & 0xFF00) >> 8) + + ((address & 0x00FF) >> 0) + + type; + + switch(type) { + /* data record */ + case 0: + c = address - last_address; + st->data = realloc(st->data, st->data_len + c + reclen); + + /* if there is a gap, set it to 0xff and increment the length */ + if (c > 0) { + memset(&st->data[st->data_len], 0xff, c); + st->data_len += c; + } + + last_address = address + reclen; + record = &st->data[st->data_len]; + st->data_len += reclen; + break; + + /* extended segment address record */ + case 2: + base = 0; + break; + + /* extended linear address record */ + case 4: + base = address; + break; + } + + buffer[2] = 0; + for(i = 0; i < reclen; ++i) { + if (read(fd, &buffer, 2) != 2 || sscanf(buffer, "%2x", &c) != 1) { + close(fd); + return PARSER_ERR_INVALID_FILE; + } + + /* add the byte to the checksum */ + checksum += c; + + switch(type) { + case 0: + if (record != NULL) { + record[i] = c; + } else { + return PARSER_ERR_INVALID_FILE; + } + break; + + case 2: + case 4: + base = (base << 8) | c; + break; + } + } + + /* read, scan, and verify the checksum */ + if ( + read(fd, &buffer, 2 ) != 2 || + sscanf(buffer, "%2x", &c) != 1 || + (uint8_t)(checksum + c) != 0x00 + ) { + close(fd); + return PARSER_ERR_INVALID_FILE; + } + + switch(type) { + /* EOF */ + case 1: + close(fd); + return PARSER_ERR_OK; + + /* address record */ + case 2: base = base << 4; + case 4: base = be_u32(base); + /* Reset last_address since our base changed */ + last_address = 0; + + if (st->base == 0) { + st->base = base; + break; + } + + /* we cant cope with files out of order */ + if (base < st->base) { + close(fd); + return PARSER_ERR_INVALID_FILE; + } + + /* if there is a gap, enlarge and fill with zeros */ + unsigned int len = base - st->base; + if (len > st->data_len) { + st->data = realloc(st->data, len); + memset(&st->data[st->data_len], 0, len - st->data_len); + st->data_len = len; + } + break; + } + } + + close(fd); + return PARSER_ERR_OK; + } +} + +parser_err_t hex_close(void *storage) { + hex_t *st = storage; + if (st) free(st->data); + free(st); + return PARSER_ERR_OK; +} + +unsigned int hex_size(void *storage) { + hex_t *st = storage; + return st->data_len; +} + +parser_err_t hex_read(void *storage, void *data, unsigned int *len) { + hex_t *st = storage; + unsigned int left = st->data_len - st->offset; + unsigned int get = left > *len ? *len : left; + + memcpy(data, &st->data[st->offset], get); + st->offset += get; + + *len = get; + return PARSER_ERR_OK; +} + +parser_err_t hex_write(void *storage, void *data, unsigned int len) { + return PARSER_ERR_RDONLY; +} + +parser_t PARSER_HEX = { + "Intel HEX", + hex_init, + hex_open, + hex_close, + hex_size, + hex_read, + hex_write +}; + diff --git a/tools/win/src/stm32flash_serial/src/parsers/hex.h b/tools/win/src/stm32flash_serial/src/parsers/hex.h new file mode 100644 index 0000000..02413c9 --- /dev/null +++ b/tools/win/src/stm32flash_serial/src/parsers/hex.h @@ -0,0 +1,27 @@ +/* + stm32flash - Open Source ST STM32 flash program for *nix + Copyright (C) 2010 Geoffrey McRae + + 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. +*/ + + +#ifndef _PARSER_HEX_H +#define _PARSER_HEX_H + +#include "parser.h" + +extern parser_t PARSER_HEX; +#endif diff --git a/tools/win/src/stm32flash_serial/src/parsers/parser.h b/tools/win/src/stm32flash_serial/src/parsers/parser.h new file mode 100644 index 0000000..c2fae3c --- /dev/null +++ b/tools/win/src/stm32flash_serial/src/parsers/parser.h @@ -0,0 +1,56 @@ +/* + stm32flash - Open Source ST STM32 flash program for *nix + Copyright (C) 2010 Geoffrey McRae + + 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. +*/ + + +#ifndef _H_PARSER +#define _H_PARSER + +enum parser_err { + PARSER_ERR_OK, + PARSER_ERR_SYSTEM, + PARSER_ERR_INVALID_FILE, + PARSER_ERR_WRONLY, + PARSER_ERR_RDONLY +}; +typedef enum parser_err parser_err_t; + +struct parser { + const char *name; + void* (*init )(); /* initialise the parser */ + parser_err_t (*open )(void *storage, const char *filename, const char write); /* open the file for read|write */ + parser_err_t (*close)(void *storage); /* close and free the parser */ + unsigned int (*size )(void *storage); /* get the total data size */ + parser_err_t (*read )(void *storage, void *data, unsigned int *len); /* read a block of data */ + parser_err_t (*write)(void *storage, void *data, unsigned int len); /* write a block of data */ +}; +typedef struct parser parser_t; + +static inline const char* parser_errstr(parser_err_t err) { + switch(err) { + case PARSER_ERR_OK : return "OK"; + case PARSER_ERR_SYSTEM : return "System Error"; + case PARSER_ERR_INVALID_FILE: return "Invalid File"; + case PARSER_ERR_WRONLY : return "Parser can only write"; + case PARSER_ERR_RDONLY : return "Parser can only read"; + default: + return "Unknown Error"; + } +} + +#endif diff --git a/tools/win/src/stm32flash_serial/src/port.c b/tools/win/src/stm32flash_serial/src/port.c new file mode 100644 index 0000000..08e58cc --- /dev/null +++ b/tools/win/src/stm32flash_serial/src/port.c @@ -0,0 +1,59 @@ +/* + stm32flash - Open Source ST STM32 flash program for *nix + Copyright (C) 2014 Antonio Borneo + + 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. +*/ + +#include +#include + +#include "serial.h" +#include "port.h" + + +extern struct port_interface port_serial; +extern struct port_interface port_i2c; + +static struct port_interface *ports[] = { + &port_serial, + &port_i2c, + NULL, +}; + + +port_err_t port_open(struct port_options *ops, struct port_interface **outport) +{ + int ret; + static struct port_interface **port; + + for (port = ports; *port; port++) { + ret = (*port)->open(*port, ops); + if (ret == PORT_ERR_NODEV) + continue; + if (ret == PORT_ERR_OK) + break; + fprintf(stderr, "Error probing interface \"%s\"\n", + (*port)->name); + } + if (*port == NULL) { + fprintf(stderr, "Cannot handle device \"%s\"\n", + ops->device); + return PORT_ERR_UNKNOWN; + } + + *outport = *port; + return PORT_ERR_OK; +} diff --git a/tools/win/src/stm32flash_serial/src/port.h b/tools/win/src/stm32flash_serial/src/port.h new file mode 100644 index 0000000..290f034 --- /dev/null +++ b/tools/win/src/stm32flash_serial/src/port.h @@ -0,0 +1,75 @@ +/* + stm32flash - Open Source ST STM32 flash program for *nix + Copyright (C) 2014 Antonio Borneo + + 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. +*/ + + +#ifndef _H_PORT +#define _H_PORT + +typedef enum { + PORT_ERR_OK = 0, + PORT_ERR_NODEV, /* No such device */ + PORT_ERR_TIMEDOUT, /* Operation timed out */ + PORT_ERR_UNKNOWN, +} port_err_t; + +/* flags */ +#define PORT_BYTE (1 << 0) /* byte (not frame) oriented */ +#define PORT_GVR_ETX (1 << 1) /* cmd GVR returns protection status */ +#define PORT_CMD_INIT (1 << 2) /* use INIT cmd to autodetect speed */ +#define PORT_RETRY (1 << 3) /* allowed read() retry after timeout */ +#define PORT_STRETCH_W (1 << 4) /* warning for no-stretching commands */ + +/* all options and flags used to open and configure an interface */ +struct port_options { + const char *device; + serial_baud_t baudRate; + const char *serial_mode; + int bus_addr; + int rx_frame_max; + int tx_frame_max; +}; + +/* + * Specify the length of reply for command GET + * This is helpful for frame-oriented protocols, e.g. i2c, to avoid time + * consuming try-fail-timeout-retry operation. + * On byte-oriented protocols, i.e. UART, this information would be skipped + * after read the first byte, so not needed. + */ +struct varlen_cmd { + uint8_t version; + uint8_t length; +}; + +struct port_interface { + const char *name; + unsigned flags; + port_err_t (*open)(struct port_interface *port, struct port_options *ops); + port_err_t (*close)(struct port_interface *port); + port_err_t (*read)(struct port_interface *port, void *buf, size_t nbyte); + port_err_t (*write)(struct port_interface *port, void *buf, size_t nbyte); + port_err_t (*gpio)(struct port_interface *port, serial_gpio_t n, int level); + const char *(*get_cfg_str)(struct port_interface *port); + struct varlen_cmd *cmd_get_reply; + void *private; +}; + +port_err_t port_open(struct port_options *ops, struct port_interface **outport); + +#endif diff --git a/tools/win/src/stm32flash_serial/src/protocol.txt b/tools/win/src/stm32flash_serial/src/protocol.txt new file mode 100644 index 0000000..0391099 --- /dev/null +++ b/tools/win/src/stm32flash_serial/src/protocol.txt @@ -0,0 +1,19 @@ +The communication protocol used by ST bootloader is documented in following ST +application notes, depending on communication port. + +In current version of stm32flash are supported only UART and I2C ports. + +* AN3154: CAN protocol used in the STM32 bootloader + http://www.st.com/web/en/resource/technical/document/application_note/CD00264321.pdf + +* AN3155: USART protocol used in the STM32(TM) bootloader + http://www.st.com/web/en/resource/technical/document/application_note/CD00264342.pdf + +* AN4221: I2C protocol used in the STM32 bootloader + http://www.st.com/web/en/resource/technical/document/application_note/DM00072315.pdf + +* AN4286: SPI protocol used in the STM32 bootloader + http://www.st.com/web/en/resource/technical/document/application_note/DM00081379.pdf + +Boot mode selection for STM32 is documented in ST application note AN2606, available in ST website: + http://www.st.com/web/en/resource/technical/document/application_note/CD00167594.pdf diff --git a/tools/win/src/stm32flash_serial/src/serial.h b/tools/win/src/stm32flash_serial/src/serial.h new file mode 100644 index 0000000..227ba16 --- /dev/null +++ b/tools/win/src/stm32flash_serial/src/serial.h @@ -0,0 +1,90 @@ +/* + stm32flash - Open Source ST STM32 flash program for *nix + Copyright (C) 2010 Geoffrey McRae + + 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. +*/ + + +#ifndef _SERIAL_H +#define _SERIAL_H + +typedef struct serial serial_t; + +typedef enum { + SERIAL_PARITY_NONE, + SERIAL_PARITY_EVEN, + SERIAL_PARITY_ODD, + + SERIAL_PARITY_INVALID +} serial_parity_t; + +typedef enum { + SERIAL_BITS_5, + SERIAL_BITS_6, + SERIAL_BITS_7, + SERIAL_BITS_8, + + SERIAL_BITS_INVALID +} serial_bits_t; + +typedef enum { + SERIAL_BAUD_1200, + SERIAL_BAUD_1800, + SERIAL_BAUD_2400, + SERIAL_BAUD_4800, + SERIAL_BAUD_9600, + SERIAL_BAUD_19200, + SERIAL_BAUD_38400, + SERIAL_BAUD_57600, + SERIAL_BAUD_115200, + SERIAL_BAUD_128000, + SERIAL_BAUD_230400, + SERIAL_BAUD_256000, + SERIAL_BAUD_460800, + SERIAL_BAUD_500000, + SERIAL_BAUD_576000, + SERIAL_BAUD_921600, + SERIAL_BAUD_1000000, + SERIAL_BAUD_1500000, + SERIAL_BAUD_2000000, + + SERIAL_BAUD_INVALID +} serial_baud_t; + +typedef enum { + SERIAL_STOPBIT_1, + SERIAL_STOPBIT_2, + + SERIAL_STOPBIT_INVALID +} serial_stopbit_t; + +typedef enum { + GPIO_RTS = 1, + GPIO_DTR, + GPIO_BRK, +} serial_gpio_t; + +/* common helper functions */ +serial_baud_t serial_get_baud(const unsigned int baud); +unsigned int serial_get_baud_int(const serial_baud_t baud); +serial_bits_t serial_get_bits(const char *mode); +unsigned int serial_get_bits_int(const serial_bits_t bits); +serial_parity_t serial_get_parity(const char *mode); +char serial_get_parity_str(const serial_parity_t parity); +serial_stopbit_t serial_get_stopbit(const char *mode); +unsigned int serial_get_stopbit_int(const serial_stopbit_t stopbit); + +#endif diff --git a/tools/win/src/stm32flash_serial/src/serial_common.c b/tools/win/src/stm32flash_serial/src/serial_common.c new file mode 100644 index 0000000..43e48e1 --- /dev/null +++ b/tools/win/src/stm32flash_serial/src/serial_common.c @@ -0,0 +1,154 @@ +/* + stm32flash - Open Source ST STM32 flash program for *nix + Copyright (C) 2010 Geoffrey McRae + + 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. +*/ + +#include "serial.h" + +serial_baud_t serial_get_baud(const unsigned int baud) { + switch(baud) { + case 1200: return SERIAL_BAUD_1200 ; + case 1800: return SERIAL_BAUD_1800 ; + case 2400: return SERIAL_BAUD_2400 ; + case 4800: return SERIAL_BAUD_4800 ; + case 9600: return SERIAL_BAUD_9600 ; + case 19200: return SERIAL_BAUD_19200 ; + case 38400: return SERIAL_BAUD_38400 ; + case 57600: return SERIAL_BAUD_57600 ; + case 115200: return SERIAL_BAUD_115200; + case 128000: return SERIAL_BAUD_128000; + case 230400: return SERIAL_BAUD_230400; + case 256000: return SERIAL_BAUD_256000; + case 460800: return SERIAL_BAUD_460800; + case 500000: return SERIAL_BAUD_500000; + case 576000: return SERIAL_BAUD_576000; + case 921600: return SERIAL_BAUD_921600; + case 1000000: return SERIAL_BAUD_1000000; + case 1500000: return SERIAL_BAUD_1500000; + case 2000000: return SERIAL_BAUD_2000000; + + default: + return SERIAL_BAUD_INVALID; + } +} + +unsigned int serial_get_baud_int(const serial_baud_t baud) { + switch(baud) { + case SERIAL_BAUD_1200 : return 1200 ; + case SERIAL_BAUD_1800 : return 1800 ; + case SERIAL_BAUD_2400 : return 2400 ; + case SERIAL_BAUD_4800 : return 4800 ; + case SERIAL_BAUD_9600 : return 9600 ; + case SERIAL_BAUD_19200 : return 19200 ; + case SERIAL_BAUD_38400 : return 38400 ; + case SERIAL_BAUD_57600 : return 57600 ; + case SERIAL_BAUD_115200: return 115200; + case SERIAL_BAUD_128000: return 128000; + case SERIAL_BAUD_230400: return 230400; + case SERIAL_BAUD_256000: return 256000; + case SERIAL_BAUD_460800: return 460800; + case SERIAL_BAUD_500000: return 500000; + case SERIAL_BAUD_576000: return 576000; + case SERIAL_BAUD_921600: return 921600; + case SERIAL_BAUD_1000000: return 1000000; + case SERIAL_BAUD_1500000: return 1500000; + case SERIAL_BAUD_2000000: return 2000000; + + case SERIAL_BAUD_INVALID: + default: + return 0; + } +} + +serial_bits_t serial_get_bits(const char *mode) { + if (!mode) + return SERIAL_BITS_INVALID; + switch(mode[0]) { + case '5': return SERIAL_BITS_5; + case '6': return SERIAL_BITS_6; + case '7': return SERIAL_BITS_7; + case '8': return SERIAL_BITS_8; + + default: + return SERIAL_BITS_INVALID; + } +} + +unsigned int serial_get_bits_int(const serial_bits_t bits) { + switch(bits) { + case SERIAL_BITS_5: return 5; + case SERIAL_BITS_6: return 6; + case SERIAL_BITS_7: return 7; + case SERIAL_BITS_8: return 8; + + default: + return 0; + } +} + +serial_parity_t serial_get_parity(const char *mode) { + if (!mode || !mode[0]) + return SERIAL_PARITY_INVALID; + switch(mode[1]) { + case 'N': + case 'n': + return SERIAL_PARITY_NONE; + case 'E': + case 'e': + return SERIAL_PARITY_EVEN; + case 'O': + case 'o': + return SERIAL_PARITY_ODD; + + default: + return SERIAL_PARITY_INVALID; + } +} + +char serial_get_parity_str(const serial_parity_t parity) { + switch(parity) { + case SERIAL_PARITY_NONE: return 'N'; + case SERIAL_PARITY_EVEN: return 'E'; + case SERIAL_PARITY_ODD : return 'O'; + + default: + return ' '; + } +} + +serial_stopbit_t serial_get_stopbit(const char *mode) { + if (!mode || !mode[0] || !mode[1]) + return SERIAL_STOPBIT_INVALID; + switch(mode[2]) { + case '1': return SERIAL_STOPBIT_1; + case '2': return SERIAL_STOPBIT_2; + + default: + return SERIAL_STOPBIT_INVALID; + } +} + +unsigned int serial_get_stopbit_int(const serial_stopbit_t stopbit) { + switch(stopbit) { + case SERIAL_STOPBIT_1: return 1; + case SERIAL_STOPBIT_2: return 2; + + default: + return 0; + } +} + diff --git a/tools/win/src/stm32flash_serial/src/serial_platform.c b/tools/win/src/stm32flash_serial/src/serial_platform.c new file mode 100644 index 0000000..98e2569 --- /dev/null +++ b/tools/win/src/stm32flash_serial/src/serial_platform.c @@ -0,0 +1,5 @@ +#if defined(__WIN32__) || defined(__CYGWIN__) +# include "serial_w32.c" +#else +# include "serial_posix.c" +#endif diff --git a/tools/win/src/stm32flash_serial/src/serial_posix.c b/tools/win/src/stm32flash_serial/src/serial_posix.c new file mode 100644 index 0000000..284b35b --- /dev/null +++ b/tools/win/src/stm32flash_serial/src/serial_posix.c @@ -0,0 +1,395 @@ +/* + stm32flash - Open Source ST STM32 flash program for *nix + Copyright (C) 2010 Geoffrey McRae + + 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. +*/ + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "serial.h" +#include "port.h" + +struct serial { + int fd; + struct termios oldtio; + struct termios newtio; + char setup_str[11]; +}; + +static serial_t *serial_open(const char *device) +{ + serial_t *h = calloc(sizeof(serial_t), 1); + + h->fd = open(device, O_RDWR | O_NOCTTY | O_NDELAY); + if (h->fd < 0) { + free(h); + return NULL; + } + fcntl(h->fd, F_SETFL, 0); + + tcgetattr(h->fd, &h->oldtio); + tcgetattr(h->fd, &h->newtio); + + return h; +} + +static void serial_flush(const serial_t *h) +{ + tcflush(h->fd, TCIFLUSH); +} + +static void serial_close(serial_t *h) +{ + serial_flush(h); + tcsetattr(h->fd, TCSANOW, &h->oldtio); + close(h->fd); + free(h); +} + +static port_err_t serial_setup(serial_t *h, const serial_baud_t baud, + const serial_bits_t bits, + const serial_parity_t parity, + const serial_stopbit_t stopbit) +{ + speed_t port_baud; + tcflag_t port_bits; + tcflag_t port_parity; + tcflag_t port_stop; + struct termios settings; + + switch (baud) { + case SERIAL_BAUD_1200: port_baud = B1200; break; + case SERIAL_BAUD_1800: port_baud = B1800; break; + case SERIAL_BAUD_2400: port_baud = B2400; break; + case SERIAL_BAUD_4800: port_baud = B4800; break; + case SERIAL_BAUD_9600: port_baud = B9600; break; + case SERIAL_BAUD_19200: port_baud = B19200; break; + case SERIAL_BAUD_38400: port_baud = B38400; break; + case SERIAL_BAUD_57600: port_baud = B57600; break; + case SERIAL_BAUD_115200: port_baud = B115200; break; + case SERIAL_BAUD_230400: port_baud = B230400; break; +#ifdef B460800 + case SERIAL_BAUD_460800: port_baud = B460800; break; +#endif /* B460800 */ +#ifdef B921600 + case SERIAL_BAUD_921600: port_baud = B921600; break; +#endif /* B921600 */ +#ifdef B500000 + case SERIAL_BAUD_500000: port_baud = B500000; break; +#endif /* B500000 */ +#ifdef B576000 + case SERIAL_BAUD_576000: port_baud = B576000; break; +#endif /* B576000 */ +#ifdef B1000000 + case SERIAL_BAUD_1000000: port_baud = B1000000; break; +#endif /* B1000000 */ +#ifdef B1500000 + case SERIAL_BAUD_1500000: port_baud = B1500000; break; +#endif /* B1500000 */ +#ifdef B2000000 + case SERIAL_BAUD_2000000: port_baud = B2000000; break; +#endif /* B2000000 */ + + case SERIAL_BAUD_INVALID: + default: + return PORT_ERR_UNKNOWN; + } + + switch (bits) { + case SERIAL_BITS_5: port_bits = CS5; break; + case SERIAL_BITS_6: port_bits = CS6; break; + case SERIAL_BITS_7: port_bits = CS7; break; + case SERIAL_BITS_8: port_bits = CS8; break; + + default: + return PORT_ERR_UNKNOWN; + } + + switch (parity) { + case SERIAL_PARITY_NONE: port_parity = 0; break; + case SERIAL_PARITY_EVEN: port_parity = PARENB; break; + case SERIAL_PARITY_ODD: port_parity = PARENB | PARODD; break; + + default: + return PORT_ERR_UNKNOWN; + } + + switch (stopbit) { + case SERIAL_STOPBIT_1: port_stop = 0; break; + case SERIAL_STOPBIT_2: port_stop = CSTOPB; break; + + default: + return PORT_ERR_UNKNOWN; + } + + /* reset the settings */ +#ifndef __sun /* Used by GNU and BSD. Ignore __SVR4 in test. */ + cfmakeraw(&h->newtio); +#else /* __sun */ + h->newtio.c_iflag &= ~(IGNBRK | BRKINT | PARMRK | ISTRIP | INLCR + | IGNCR | ICRNL | IXON); + if (port_parity) + h->newtio.c_iflag |= INPCK; + + h->newtio.c_oflag &= ~OPOST; + h->newtio.c_lflag &= ~(ECHO | ECHONL | ICANON | ISIG | IEXTEN); + h->newtio.c_cflag &= ~(CSIZE | PARENB); + h->newtio.c_cflag |= CS8; +#endif /* __sun */ +#ifdef __QNXNTO__ + h->newtio.c_cflag &= ~(CSIZE | IHFLOW | OHFLOW); +#else + h->newtio.c_cflag &= ~(CSIZE | CRTSCTS); +#endif + h->newtio.c_cflag &= ~(CSIZE | CRTSCTS); + h->newtio.c_iflag &= ~(IXON | IXOFF | IXANY | IGNPAR); + h->newtio.c_lflag &= ~(ECHOK | ECHOCTL | ECHOKE); + h->newtio.c_oflag &= ~(OPOST | ONLCR); + + /* setup the new settings */ + cfsetispeed(&h->newtio, port_baud); + cfsetospeed(&h->newtio, port_baud); + h->newtio.c_cflag |= + port_parity | + port_bits | + port_stop | + CLOCAL | + CREAD; + + h->newtio.c_cc[VMIN] = 0; + h->newtio.c_cc[VTIME] = 5; /* in units of 0.1 s */ + + /* set the settings */ + serial_flush(h); + if (tcsetattr(h->fd, TCSANOW, &h->newtio) != 0) + return PORT_ERR_UNKNOWN; + +/* this check fails on CDC-ACM devices, bits 16 and 17 of cflag differ! + * it has been disabled below for now -jcw, 2015-11-09 + if (settings.c_cflag != h->newtio.c_cflag) + fprintf(stderr, "c_cflag mismatch %lx\n", + settings.c_cflag ^ h->newtio.c_cflag); + */ + + /* confirm they were set */ + tcgetattr(h->fd, &settings); + if (settings.c_iflag != h->newtio.c_iflag || + settings.c_oflag != h->newtio.c_oflag || + //settings.c_cflag != h->newtio.c_cflag || + settings.c_lflag != h->newtio.c_lflag) + return PORT_ERR_UNKNOWN; + + snprintf(h->setup_str, sizeof(h->setup_str), "%u %d%c%d", + serial_get_baud_int(baud), + serial_get_bits_int(bits), + serial_get_parity_str(parity), + serial_get_stopbit_int(stopbit)); + return PORT_ERR_OK; +} + +/* + * Roger clark. + * This function is no longer used. But has just been commented out in case it needs + * to be reinstated in the future + +static int startswith(const char *haystack, const char *needle) { + return strncmp(haystack, needle, strlen(needle)) == 0; +} +*/ + +static int is_tty(const char *path) { + char resolved[PATH_MAX]; + + if(!realpath(path, resolved)) return 0; + + + /* + * Roger Clark + * Commented out this check, because on OSX some devices are /dev/cu + * and some users use symbolic links to devices, hence the name may not even start + * with /dev + + if(startswith(resolved, "/dev/tty")) return 1; + + return 0; + */ + + return 1; +} + +static port_err_t serial_posix_open(struct port_interface *port, + struct port_options *ops) +{ + serial_t *h; + + /* 1. check device name match */ + if (!is_tty(ops->device)) + return PORT_ERR_NODEV; + + /* 2. check options */ + if (ops->baudRate == SERIAL_BAUD_INVALID) + return PORT_ERR_UNKNOWN; + if (serial_get_bits(ops->serial_mode) == SERIAL_BITS_INVALID) + return PORT_ERR_UNKNOWN; + if (serial_get_parity(ops->serial_mode) == SERIAL_PARITY_INVALID) + return PORT_ERR_UNKNOWN; + if (serial_get_stopbit(ops->serial_mode) == SERIAL_STOPBIT_INVALID) + return PORT_ERR_UNKNOWN; + + /* 3. open it */ + h = serial_open(ops->device); + if (h == NULL) + return PORT_ERR_UNKNOWN; + + /* 4. set options */ + if (serial_setup(h, ops->baudRate, + serial_get_bits(ops->serial_mode), + serial_get_parity(ops->serial_mode), + serial_get_stopbit(ops->serial_mode) + ) != PORT_ERR_OK) { + serial_close(h); + return PORT_ERR_UNKNOWN; + } + + port->private = h; + return PORT_ERR_OK; +} + +static port_err_t serial_posix_close(struct port_interface *port) +{ + serial_t *h; + + h = (serial_t *)port->private; + if (h == NULL) + return PORT_ERR_UNKNOWN; + + serial_close(h); + port->private = NULL; + return PORT_ERR_OK; +} + +static port_err_t serial_posix_read(struct port_interface *port, void *buf, + size_t nbyte) +{ + serial_t *h; + ssize_t r; + uint8_t *pos = (uint8_t *)buf; + + h = (serial_t *)port->private; + if (h == NULL) + return PORT_ERR_UNKNOWN; + + while (nbyte) { + r = read(h->fd, pos, nbyte); + if (r == 0) + return PORT_ERR_TIMEDOUT; + if (r < 0) + return PORT_ERR_UNKNOWN; + + nbyte -= r; + pos += r; + } + return PORT_ERR_OK; +} + +static port_err_t serial_posix_write(struct port_interface *port, void *buf, + size_t nbyte) +{ + serial_t *h; + ssize_t r; + const uint8_t *pos = (const uint8_t *)buf; + + h = (serial_t *)port->private; + if (h == NULL) + return PORT_ERR_UNKNOWN; + + while (nbyte) { + r = write(h->fd, pos, nbyte); + if (r < 1) + return PORT_ERR_UNKNOWN; + + nbyte -= r; + pos += r; + } + return PORT_ERR_OK; +} + +static port_err_t serial_posix_gpio(struct port_interface *port, + serial_gpio_t n, int level) +{ + serial_t *h; + int bit, lines; + + h = (serial_t *)port->private; + if (h == NULL) + return PORT_ERR_UNKNOWN; + + switch (n) { + case GPIO_RTS: + bit = TIOCM_RTS; + break; + + case GPIO_DTR: + bit = TIOCM_DTR; + break; + + case GPIO_BRK: + if (level == 0) + return PORT_ERR_OK; + if (tcsendbreak(h->fd, 1)) + return PORT_ERR_UNKNOWN; + return PORT_ERR_OK; + + default: + return PORT_ERR_UNKNOWN; + } + + /* handle RTS/DTR */ + if (ioctl(h->fd, TIOCMGET, &lines)) + return PORT_ERR_UNKNOWN; + lines = level ? lines | bit : lines & ~bit; + if (ioctl(h->fd, TIOCMSET, &lines)) + return PORT_ERR_UNKNOWN; + + return PORT_ERR_OK; +} + +static const char *serial_posix_get_cfg_str(struct port_interface *port) +{ + serial_t *h; + + h = (serial_t *)port->private; + return h ? h->setup_str : "INVALID"; +} + +struct port_interface port_serial = { + .name = "serial_posix", + .flags = PORT_BYTE | PORT_GVR_ETX | PORT_CMD_INIT | PORT_RETRY, + .open = serial_posix_open, + .close = serial_posix_close, + .read = serial_posix_read, + .write = serial_posix_write, + .gpio = serial_posix_gpio, + .get_cfg_str = serial_posix_get_cfg_str, +}; diff --git a/tools/win/src/stm32flash_serial/src/serial_w32.c b/tools/win/src/stm32flash_serial/src/serial_w32.c new file mode 100644 index 0000000..56772c0 --- /dev/null +++ b/tools/win/src/stm32flash_serial/src/serial_w32.c @@ -0,0 +1,341 @@ +/* + stm32flash - Open Source ST STM32 flash program for *nix + Copyright (C) 2010 Geoffrey McRae + Copyright (C) 2010 Gareth McMullin + + 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. +*/ + +#include +#include +#include +#include +#include +#include +#include +#include + +#include "serial.h" +#include "port.h" + +struct serial { + HANDLE fd; + DCB oldtio; + DCB newtio; + char setup_str[11]; +}; + +static serial_t *serial_open(const char *device) +{ + serial_t *h = calloc(sizeof(serial_t), 1); + char *devName; + + /* timeout in ms */ + COMMTIMEOUTS timeouts = {MAXDWORD, MAXDWORD, 500, 0, 0}; + + /* Fix the device name if required */ + if (strlen(device) > 4 && device[0] != '\\') { + devName = calloc(1, strlen(device) + 5); + sprintf(devName, "\\\\.\\%s", device); + } else { + devName = (char *)device; + } + + /* Create file handle for port */ + h->fd = CreateFile(devName, GENERIC_READ | GENERIC_WRITE, + 0, /* Exclusive access */ + NULL, /* No security */ + OPEN_EXISTING, + 0, /* No overlap */ + NULL); + + if (devName != device) + free(devName); + + if (h->fd == INVALID_HANDLE_VALUE) { + if (GetLastError() == ERROR_FILE_NOT_FOUND) + fprintf(stderr, "File not found: %s\n", device); + return NULL; + } + + SetupComm(h->fd, 4096, 4096); /* Set input and output buffer size */ + + SetCommTimeouts(h->fd, &timeouts); + + SetCommMask(h->fd, EV_ERR); /* Notify us of error events */ + + GetCommState(h->fd, &h->oldtio); /* Retrieve port parameters */ + GetCommState(h->fd, &h->newtio); /* Retrieve port parameters */ + + /* PurgeComm(h->fd, PURGE_RXABORT | PURGE_TXCLEAR | PURGE_TXABORT | PURGE_TXCLEAR); */ + + return h; +} + +static void serial_flush(const serial_t *h) +{ + /* We shouldn't need to flush in non-overlapping (blocking) mode */ + /* tcflush(h->fd, TCIFLUSH); */ +} + +static void serial_close(serial_t *h) +{ + serial_flush(h); + SetCommState(h->fd, &h->oldtio); + CloseHandle(h->fd); + free(h); +} + +static port_err_t serial_setup(serial_t *h, + const serial_baud_t baud, + const serial_bits_t bits, + const serial_parity_t parity, + const serial_stopbit_t stopbit) +{ + switch (baud) { + case SERIAL_BAUD_1200: h->newtio.BaudRate = CBR_1200; break; + /* case SERIAL_BAUD_1800: h->newtio.BaudRate = CBR_1800; break; */ + case SERIAL_BAUD_2400: h->newtio.BaudRate = CBR_2400; break; + case SERIAL_BAUD_4800: h->newtio.BaudRate = CBR_4800; break; + case SERIAL_BAUD_9600: h->newtio.BaudRate = CBR_9600; break; + case SERIAL_BAUD_19200: h->newtio.BaudRate = CBR_19200; break; + case SERIAL_BAUD_38400: h->newtio.BaudRate = CBR_38400; break; + case SERIAL_BAUD_57600: h->newtio.BaudRate = CBR_57600; break; + case SERIAL_BAUD_115200: h->newtio.BaudRate = CBR_115200; break; + case SERIAL_BAUD_128000: h->newtio.BaudRate = CBR_128000; break; + case SERIAL_BAUD_256000: h->newtio.BaudRate = CBR_256000; break; + /* These are not defined in WinBase.h and might work or not */ + case SERIAL_BAUD_230400: h->newtio.BaudRate = 230400; break; + case SERIAL_BAUD_460800: h->newtio.BaudRate = 460800; break; + case SERIAL_BAUD_500000: h->newtio.BaudRate = 500000; break; + case SERIAL_BAUD_576000: h->newtio.BaudRate = 576000; break; + case SERIAL_BAUD_921600: h->newtio.BaudRate = 921600; break; + case SERIAL_BAUD_1000000: h->newtio.BaudRate = 1000000; break; + case SERIAL_BAUD_1500000: h->newtio.BaudRate = 1500000; break; + case SERIAL_BAUD_2000000: h->newtio.BaudRate = 2000000; break; + case SERIAL_BAUD_INVALID: + + default: + return PORT_ERR_UNKNOWN; + } + + switch (bits) { + case SERIAL_BITS_5: h->newtio.ByteSize = 5; break; + case SERIAL_BITS_6: h->newtio.ByteSize = 6; break; + case SERIAL_BITS_7: h->newtio.ByteSize = 7; break; + case SERIAL_BITS_8: h->newtio.ByteSize = 8; break; + + default: + return PORT_ERR_UNKNOWN; + } + + switch (parity) { + case SERIAL_PARITY_NONE: h->newtio.Parity = NOPARITY; break; + case SERIAL_PARITY_EVEN: h->newtio.Parity = EVENPARITY; break; + case SERIAL_PARITY_ODD: h->newtio.Parity = ODDPARITY; break; + + default: + return PORT_ERR_UNKNOWN; + } + + switch (stopbit) { + case SERIAL_STOPBIT_1: h->newtio.StopBits = ONESTOPBIT; break; + case SERIAL_STOPBIT_2: h->newtio.StopBits = TWOSTOPBITS; break; + + default: + return PORT_ERR_UNKNOWN; + } + + /* reset the settings */ + h->newtio.fOutxCtsFlow = FALSE; + h->newtio.fOutxDsrFlow = FALSE; + h->newtio.fOutX = FALSE; + h->newtio.fInX = FALSE; + h->newtio.fNull = 0; + h->newtio.fAbortOnError = 0; + + /* set the settings */ + serial_flush(h); + if (!SetCommState(h->fd, &h->newtio)) + return PORT_ERR_UNKNOWN; + + snprintf(h->setup_str, sizeof(h->setup_str), "%u %d%c%d", + serial_get_baud_int(baud), + serial_get_bits_int(bits), + serial_get_parity_str(parity), + serial_get_stopbit_int(stopbit) + ); + return PORT_ERR_OK; +} + +static port_err_t serial_w32_open(struct port_interface *port, + struct port_options *ops) +{ + serial_t *h; + + /* 1. check device name match */ + if (!((strlen(ops->device) == 4 || strlen(ops->device) == 5) + && !strncmp(ops->device, "COM", 3) && isdigit(ops->device[3])) + && !(!strncmp(ops->device, "\\\\.\\COM", strlen("\\\\.\\COM")) + && isdigit(ops->device[strlen("\\\\.\\COM")]))) + return PORT_ERR_NODEV; + + /* 2. check options */ + if (ops->baudRate == SERIAL_BAUD_INVALID) + return PORT_ERR_UNKNOWN; + if (serial_get_bits(ops->serial_mode) == SERIAL_BITS_INVALID) + return PORT_ERR_UNKNOWN; + if (serial_get_parity(ops->serial_mode) == SERIAL_PARITY_INVALID) + return PORT_ERR_UNKNOWN; + if (serial_get_stopbit(ops->serial_mode) == SERIAL_STOPBIT_INVALID) + return PORT_ERR_UNKNOWN; + + /* 3. open it */ + h = serial_open(ops->device); + if (h == NULL) + return PORT_ERR_UNKNOWN; + + /* 4. set options */ + if (serial_setup(h, ops->baudRate, + serial_get_bits(ops->serial_mode), + serial_get_parity(ops->serial_mode), + serial_get_stopbit(ops->serial_mode) + ) != PORT_ERR_OK) { + serial_close(h); + return PORT_ERR_UNKNOWN; + } + + port->private = h; + return PORT_ERR_OK; +} + +static port_err_t serial_w32_close(struct port_interface *port) +{ + serial_t *h; + + h = (serial_t *)port->private; + if (h == NULL) + return PORT_ERR_UNKNOWN; + + serial_close(h); + port->private = NULL; + return PORT_ERR_OK; +} + +static port_err_t serial_w32_read(struct port_interface *port, void *buf, + size_t nbyte) +{ + serial_t *h; + DWORD r; + uint8_t *pos = (uint8_t *)buf; + + h = (serial_t *)port->private; + if (h == NULL) + return PORT_ERR_UNKNOWN; + + while (nbyte) { + ReadFile(h->fd, pos, nbyte, &r, NULL); + if (r == 0) + return PORT_ERR_TIMEDOUT; + if (r < 0) + return PORT_ERR_UNKNOWN; + + nbyte -= r; + pos += r; + } + return PORT_ERR_OK; +} + +static port_err_t serial_w32_write(struct port_interface *port, void *buf, + size_t nbyte) +{ + serial_t *h; + DWORD r; + uint8_t *pos = (uint8_t *)buf; + + h = (serial_t *)port->private; + if (h == NULL) + return PORT_ERR_UNKNOWN; + + while (nbyte) { + if (!WriteFile(h->fd, pos, nbyte, &r, NULL)) + return PORT_ERR_UNKNOWN; + if (r < 1) + return PORT_ERR_UNKNOWN; + + nbyte -= r; + pos += r; + } + return PORT_ERR_OK; +} + +static port_err_t serial_w32_gpio(struct port_interface *port, + serial_gpio_t n, int level) +{ + serial_t *h; + int bit; + + h = (serial_t *)port->private; + if (h == NULL) + return PORT_ERR_UNKNOWN; + + switch (n) { + case GPIO_RTS: + bit = level ? SETRTS : CLRRTS; + break; + + case GPIO_DTR: + bit = level ? SETDTR : CLRDTR; + break; + + case GPIO_BRK: + if (level == 0) + return PORT_ERR_OK; + if (EscapeCommFunction(h->fd, SETBREAK) == 0) + return PORT_ERR_UNKNOWN; + usleep(500000); + if (EscapeCommFunction(h->fd, CLRBREAK) == 0) + return PORT_ERR_UNKNOWN; + return PORT_ERR_OK; + + default: + return PORT_ERR_UNKNOWN; + } + + /* handle RTS/DTR */ + if (EscapeCommFunction(h->fd, bit) == 0) + return PORT_ERR_UNKNOWN; + + return PORT_ERR_OK; +} + +static const char *serial_w32_get_cfg_str(struct port_interface *port) +{ + serial_t *h; + + h = (serial_t *)port->private; + return h ? h->setup_str : "INVALID"; +} + +struct port_interface port_serial = { + .name = "serial_w32", + .flags = PORT_BYTE | PORT_GVR_ETX | PORT_CMD_INIT | PORT_RETRY, + .open = serial_w32_open, + .close = serial_w32_close, + .read = serial_w32_read, + .write = serial_w32_write, + .gpio = serial_w32_gpio, + .get_cfg_str = serial_w32_get_cfg_str, +}; diff --git a/tools/win/src/stm32flash_serial/src/stm32.c b/tools/win/src/stm32flash_serial/src/stm32.c new file mode 100644 index 0000000..74047d2 --- /dev/null +++ b/tools/win/src/stm32flash_serial/src/stm32.c @@ -0,0 +1,1048 @@ +/* + stm32flash - Open Source ST STM32 flash program for *nix + Copyright 2010 Geoffrey McRae + Copyright 2012-2014 Tormod Volden + + 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. +*/ + +#include +#include +#include +#include +#include +#include + +#include "stm32.h" +#include "port.h" +#include "utils.h" + +#define STM32_ACK 0x79 +#define STM32_NACK 0x1F +#define STM32_BUSY 0x76 + +#define STM32_CMD_INIT 0x7F +#define STM32_CMD_GET 0x00 /* get the version and command supported */ +#define STM32_CMD_GVR 0x01 /* get version and read protection status */ +#define STM32_CMD_GID 0x02 /* get ID */ +#define STM32_CMD_RM 0x11 /* read memory */ +#define STM32_CMD_GO 0x21 /* go */ +#define STM32_CMD_WM 0x31 /* write memory */ +#define STM32_CMD_WM_NS 0x32 /* no-stretch write memory */ +#define STM32_CMD_ER 0x43 /* erase */ +#define STM32_CMD_EE 0x44 /* extended erase */ +#define STM32_CMD_EE_NS 0x45 /* extended erase no-stretch */ +#define STM32_CMD_WP 0x63 /* write protect */ +#define STM32_CMD_WP_NS 0x64 /* write protect no-stretch */ +#define STM32_CMD_UW 0x73 /* write unprotect */ +#define STM32_CMD_UW_NS 0x74 /* write unprotect no-stretch */ +#define STM32_CMD_RP 0x82 /* readout protect */ +#define STM32_CMD_RP_NS 0x83 /* readout protect no-stretch */ +#define STM32_CMD_UR 0x92 /* readout unprotect */ +#define STM32_CMD_UR_NS 0x93 /* readout unprotect no-stretch */ +#define STM32_CMD_CRC 0xA1 /* compute CRC */ +#define STM32_CMD_ERR 0xFF /* not a valid command */ + +#define STM32_RESYNC_TIMEOUT 35 /* seconds */ +#define STM32_MASSERASE_TIMEOUT 35 /* seconds */ +#define STM32_SECTERASE_TIMEOUT 5 /* seconds */ +#define STM32_BLKWRITE_TIMEOUT 1 /* seconds */ +#define STM32_WUNPROT_TIMEOUT 1 /* seconds */ +#define STM32_WPROT_TIMEOUT 1 /* seconds */ +#define STM32_RPROT_TIMEOUT 1 /* seconds */ + +#define STM32_CMD_GET_LENGTH 17 /* bytes in the reply */ + +struct stm32_cmd { + uint8_t get; + uint8_t gvr; + uint8_t gid; + uint8_t rm; + uint8_t go; + uint8_t wm; + uint8_t er; /* this may be extended erase */ + uint8_t wp; + uint8_t uw; + uint8_t rp; + uint8_t ur; + uint8_t crc; +}; + +/* Reset code for ARMv7-M (Cortex-M3) and ARMv6-M (Cortex-M0) + * see ARMv7-M or ARMv6-M Architecture Reference Manual (table B3-8) + * or "The definitive guide to the ARM Cortex-M3", section 14.4. + */ +static const uint8_t stm_reset_code[] = { + 0x01, 0x49, // ldr r1, [pc, #4] ; () + 0x02, 0x4A, // ldr r2, [pc, #8] ; () + 0x0A, 0x60, // str r2, [r1, #0] + 0xfe, 0xe7, // endless: b endless + 0x0c, 0xed, 0x00, 0xe0, // .word 0xe000ed0c = NVIC AIRCR register address + 0x04, 0x00, 0xfa, 0x05 // .word 0x05fa0004 = VECTKEY | SYSRESETREQ +}; + +static const uint32_t stm_reset_code_length = sizeof(stm_reset_code); + +extern const stm32_dev_t devices[]; + +static void stm32_warn_stretching(const char *f) +{ + fprintf(stderr, "Attention !!!\n"); + fprintf(stderr, "\tThis %s error could be caused by your I2C\n", f); + fprintf(stderr, "\tcontroller not accepting \"clock stretching\"\n"); + fprintf(stderr, "\tas required by bootloader.\n"); + fprintf(stderr, "\tCheck \"I2C.txt\" in stm32flash source code.\n"); +} + +static stm32_err_t stm32_get_ack_timeout(const stm32_t *stm, time_t timeout) +{ + struct port_interface *port = stm->port; + uint8_t byte; + port_err_t p_err; + time_t t0, t1; + + if (!(port->flags & PORT_RETRY)) + timeout = 0; + + if (timeout) + time(&t0); + + do { + p_err = port->read(port, &byte, 1); + if (p_err == PORT_ERR_TIMEDOUT && timeout) { + time(&t1); + if (t1 < t0 + timeout) + continue; + } + + if (p_err != PORT_ERR_OK) { + fprintf(stderr, "Failed to read ACK byte\n"); + return STM32_ERR_UNKNOWN; + } + + if (byte == STM32_ACK) + return STM32_ERR_OK; + if (byte == STM32_NACK) + return STM32_ERR_NACK; + if (byte != STM32_BUSY) { + fprintf(stderr, "Got byte 0x%02x instead of ACK\n", + byte); + return STM32_ERR_UNKNOWN; + } + } while (1); +} + +static stm32_err_t stm32_get_ack(const stm32_t *stm) +{ + return stm32_get_ack_timeout(stm, 0); +} + +static stm32_err_t stm32_send_command_timeout(const stm32_t *stm, + const uint8_t cmd, + time_t timeout) +{ + struct port_interface *port = stm->port; + stm32_err_t s_err; + port_err_t p_err; + uint8_t buf[2]; + + buf[0] = cmd; + buf[1] = cmd ^ 0xFF; + p_err = port->write(port, buf, 2); + if (p_err != PORT_ERR_OK) { + fprintf(stderr, "Failed to send command\n"); + return STM32_ERR_UNKNOWN; + } + s_err = stm32_get_ack_timeout(stm, timeout); + if (s_err == STM32_ERR_OK) + return STM32_ERR_OK; + if (s_err == STM32_ERR_NACK) + fprintf(stderr, "Got NACK from device on command 0x%02x\n", cmd); + else + fprintf(stderr, "Unexpected reply from device on command 0x%02x\n", cmd); + return STM32_ERR_UNKNOWN; +} + +static stm32_err_t stm32_send_command(const stm32_t *stm, const uint8_t cmd) +{ + return stm32_send_command_timeout(stm, cmd, 0); +} + +/* if we have lost sync, send a wrong command and expect a NACK */ +static stm32_err_t stm32_resync(const stm32_t *stm) +{ + struct port_interface *port = stm->port; + port_err_t p_err; + uint8_t buf[2], ack; + time_t t0, t1; + + time(&t0); + t1 = t0; + + buf[0] = STM32_CMD_ERR; + buf[1] = STM32_CMD_ERR ^ 0xFF; + while (t1 < t0 + STM32_RESYNC_TIMEOUT) { + p_err = port->write(port, buf, 2); + if (p_err != PORT_ERR_OK) { + usleep(500000); + time(&t1); + continue; + } + p_err = port->read(port, &ack, 1); + if (p_err != PORT_ERR_OK) { + time(&t1); + continue; + } + if (ack == STM32_NACK) + return STM32_ERR_OK; + time(&t1); + } + return STM32_ERR_UNKNOWN; +} + +/* + * some command receive reply frame with variable length, and length is + * embedded in reply frame itself. + * We can guess the length, but if we guess wrong the protocol gets out + * of sync. + * Use resync for frame oriented interfaces (e.g. I2C) and byte-by-byte + * read for byte oriented interfaces (e.g. UART). + * + * to run safely, data buffer should be allocated for 256+1 bytes + * + * len is value of the first byte in the frame. + */ +static stm32_err_t stm32_guess_len_cmd(const stm32_t *stm, uint8_t cmd, + uint8_t *data, unsigned int len) +{ + struct port_interface *port = stm->port; + port_err_t p_err; + + if (stm32_send_command(stm, cmd) != STM32_ERR_OK) + return STM32_ERR_UNKNOWN; + if (port->flags & PORT_BYTE) { + /* interface is UART-like */ + p_err = port->read(port, data, 1); + if (p_err != PORT_ERR_OK) + return STM32_ERR_UNKNOWN; + len = data[0]; + p_err = port->read(port, data + 1, len + 1); + if (p_err != PORT_ERR_OK) + return STM32_ERR_UNKNOWN; + return STM32_ERR_OK; + } + + p_err = port->read(port, data, len + 2); + if (p_err == PORT_ERR_OK && len == data[0]) + return STM32_ERR_OK; + if (p_err != PORT_ERR_OK) { + /* restart with only one byte */ + if (stm32_resync(stm) != STM32_ERR_OK) + return STM32_ERR_UNKNOWN; + if (stm32_send_command(stm, cmd) != STM32_ERR_OK) + return STM32_ERR_UNKNOWN; + p_err = port->read(port, data, 1); + if (p_err != PORT_ERR_OK) + return STM32_ERR_UNKNOWN; + } + + fprintf(stderr, "Re sync (len = %d)\n", data[0]); + if (stm32_resync(stm) != STM32_ERR_OK) + return STM32_ERR_UNKNOWN; + + len = data[0]; + if (stm32_send_command(stm, cmd) != STM32_ERR_OK) + return STM32_ERR_UNKNOWN; + p_err = port->read(port, data, len + 2); + if (p_err != PORT_ERR_OK) + return STM32_ERR_UNKNOWN; + return STM32_ERR_OK; +} + +/* + * Some interface, e.g. UART, requires a specific init sequence to let STM32 + * autodetect the interface speed. + * The sequence is only required one time after reset. + * stm32flash has command line flag "-c" to prevent sending the init sequence + * in case it was already sent before. + * User can easily forget adding "-c". In this case the bootloader would + * interpret the init sequence as part of a command message, then waiting for + * the rest of the message blocking the interface. + * This function sends the init sequence and, in case of timeout, recovers + * the interface. + */ +static stm32_err_t stm32_send_init_seq(const stm32_t *stm) +{ + struct port_interface *port = stm->port; + port_err_t p_err; + uint8_t byte, cmd = STM32_CMD_INIT; + + p_err = port->write(port, &cmd, 1); + if (p_err != PORT_ERR_OK) { + fprintf(stderr, "Failed to send init to device\n"); + return STM32_ERR_UNKNOWN; + } + p_err = port->read(port, &byte, 1); + if (p_err == PORT_ERR_OK && byte == STM32_ACK) + return STM32_ERR_OK; + if (p_err == PORT_ERR_OK && byte == STM32_NACK) { + /* We could get error later, but let's continue, for now. */ + fprintf(stderr, + "Warning: the interface was not closed properly.\n"); + return STM32_ERR_OK; + } + if (p_err != PORT_ERR_TIMEDOUT) { + fprintf(stderr, "Failed to init device.\n"); + return STM32_ERR_UNKNOWN; + } + + /* + * Check if previous STM32_CMD_INIT was taken as first byte + * of a command. Send a new byte, we should get back a NACK. + */ + p_err = port->write(port, &cmd, 1); + if (p_err != PORT_ERR_OK) { + fprintf(stderr, "Failed to send init to device\n"); + return STM32_ERR_UNKNOWN; + } + p_err = port->read(port, &byte, 1); + if (p_err == PORT_ERR_OK && byte == STM32_NACK) + return STM32_ERR_OK; + fprintf(stderr, "Failed to init device.\n"); + return STM32_ERR_UNKNOWN; +} + +/* find newer command by higher code */ +#define newer(prev, a) (((prev) == STM32_CMD_ERR) \ + ? (a) \ + : (((prev) > (a)) ? (prev) : (a))) + +stm32_t *stm32_init(struct port_interface *port, const char init) +{ + uint8_t len, val, buf[257]; + stm32_t *stm; + int i, new_cmds; + + stm = calloc(sizeof(stm32_t), 1); + stm->cmd = malloc(sizeof(stm32_cmd_t)); + memset(stm->cmd, STM32_CMD_ERR, sizeof(stm32_cmd_t)); + stm->port = port; + + if ((port->flags & PORT_CMD_INIT) && init) + if (stm32_send_init_seq(stm) != STM32_ERR_OK) + return NULL; + + /* get the version and read protection status */ + if (stm32_send_command(stm, STM32_CMD_GVR) != STM32_ERR_OK) { + stm32_close(stm); + return NULL; + } + + /* From AN, only UART bootloader returns 3 bytes */ + len = (port->flags & PORT_GVR_ETX) ? 3 : 1; + if (port->read(port, buf, len) != PORT_ERR_OK) + return NULL; + stm->version = buf[0]; + stm->option1 = (port->flags & PORT_GVR_ETX) ? buf[1] : 0; + stm->option2 = (port->flags & PORT_GVR_ETX) ? buf[2] : 0; + if (stm32_get_ack(stm) != STM32_ERR_OK) { + stm32_close(stm); + return NULL; + } + + /* get the bootloader information */ + len = STM32_CMD_GET_LENGTH; + if (port->cmd_get_reply) + for (i = 0; port->cmd_get_reply[i].length; i++) + if (stm->version == port->cmd_get_reply[i].version) { + len = port->cmd_get_reply[i].length; + break; + } + if (stm32_guess_len_cmd(stm, STM32_CMD_GET, buf, len) != STM32_ERR_OK) + return NULL; + len = buf[0] + 1; + stm->bl_version = buf[1]; + new_cmds = 0; + for (i = 1; i < len; i++) { + val = buf[i + 1]; + switch (val) { + case STM32_CMD_GET: + stm->cmd->get = val; break; + case STM32_CMD_GVR: + stm->cmd->gvr = val; break; + case STM32_CMD_GID: + stm->cmd->gid = val; break; + case STM32_CMD_RM: + stm->cmd->rm = val; break; + case STM32_CMD_GO: + stm->cmd->go = val; break; + case STM32_CMD_WM: + case STM32_CMD_WM_NS: + stm->cmd->wm = newer(stm->cmd->wm, val); + break; + case STM32_CMD_ER: + case STM32_CMD_EE: + case STM32_CMD_EE_NS: + stm->cmd->er = newer(stm->cmd->er, val); + break; + case STM32_CMD_WP: + case STM32_CMD_WP_NS: + stm->cmd->wp = newer(stm->cmd->wp, val); + break; + case STM32_CMD_UW: + case STM32_CMD_UW_NS: + stm->cmd->uw = newer(stm->cmd->uw, val); + break; + case STM32_CMD_RP: + case STM32_CMD_RP_NS: + stm->cmd->rp = newer(stm->cmd->rp, val); + break; + case STM32_CMD_UR: + case STM32_CMD_UR_NS: + stm->cmd->ur = newer(stm->cmd->ur, val); + break; + case STM32_CMD_CRC: + stm->cmd->crc = newer(stm->cmd->crc, val); + break; + default: + if (new_cmds++ == 0) + fprintf(stderr, + "GET returns unknown commands (0x%2x", + val); + else + fprintf(stderr, ", 0x%2x", val); + } + } + if (new_cmds) + fprintf(stderr, ")\n"); + if (stm32_get_ack(stm) != STM32_ERR_OK) { + stm32_close(stm); + return NULL; + } + + if (stm->cmd->get == STM32_CMD_ERR + || stm->cmd->gvr == STM32_CMD_ERR + || stm->cmd->gid == STM32_CMD_ERR) { + fprintf(stderr, "Error: bootloader did not returned correct information from GET command\n"); + return NULL; + } + + /* get the device ID */ + if (stm32_guess_len_cmd(stm, stm->cmd->gid, buf, 1) != STM32_ERR_OK) { + stm32_close(stm); + return NULL; + } + len = buf[0] + 1; + if (len < 2) { + stm32_close(stm); + fprintf(stderr, "Only %d bytes sent in the PID, unknown/unsupported device\n", len); + return NULL; + } + stm->pid = (buf[1] << 8) | buf[2]; + if (len > 2) { + fprintf(stderr, "This bootloader returns %d extra bytes in PID:", len); + for (i = 2; i <= len ; i++) + fprintf(stderr, " %02x", buf[i]); + fprintf(stderr, "\n"); + } + if (stm32_get_ack(stm) != STM32_ERR_OK) { + stm32_close(stm); + return NULL; + } + + stm->dev = devices; + while (stm->dev->id != 0x00 && stm->dev->id != stm->pid) + ++stm->dev; + + if (!stm->dev->id) { + fprintf(stderr, "Unknown/unsupported device (Device ID: 0x%03x)\n", stm->pid); + stm32_close(stm); + return NULL; + } + + return stm; +} + +void stm32_close(stm32_t *stm) +{ + if (stm) + free(stm->cmd); + free(stm); +} + +stm32_err_t stm32_read_memory(const stm32_t *stm, uint32_t address, + uint8_t data[], unsigned int len) +{ + struct port_interface *port = stm->port; + uint8_t buf[5]; + + if (!len) + return STM32_ERR_OK; + + if (len > 256) { + fprintf(stderr, "Error: READ length limit at 256 bytes\n"); + return STM32_ERR_UNKNOWN; + } + + if (stm->cmd->rm == STM32_CMD_ERR) { + fprintf(stderr, "Error: READ command not implemented in bootloader.\n"); + return STM32_ERR_NO_CMD; + } + + if (stm32_send_command(stm, stm->cmd->rm) != STM32_ERR_OK) + return STM32_ERR_UNKNOWN; + + buf[0] = address >> 24; + buf[1] = (address >> 16) & 0xFF; + buf[2] = (address >> 8) & 0xFF; + buf[3] = address & 0xFF; + buf[4] = buf[0] ^ buf[1] ^ buf[2] ^ buf[3]; + if (port->write(port, buf, 5) != PORT_ERR_OK) + return STM32_ERR_UNKNOWN; + if (stm32_get_ack(stm) != STM32_ERR_OK) + return STM32_ERR_UNKNOWN; + + if (stm32_send_command(stm, len - 1) != STM32_ERR_OK) + return STM32_ERR_UNKNOWN; + + if (port->read(port, data, len) != PORT_ERR_OK) + return STM32_ERR_UNKNOWN; + + return STM32_ERR_OK; +} + +stm32_err_t stm32_write_memory(const stm32_t *stm, uint32_t address, + const uint8_t data[], unsigned int len) +{ + struct port_interface *port = stm->port; + uint8_t cs, buf[256 + 2]; + unsigned int i, aligned_len; + stm32_err_t s_err; + + if (!len) + return STM32_ERR_OK; + + if (len > 256) { + fprintf(stderr, "Error: READ length limit at 256 bytes\n"); + return STM32_ERR_UNKNOWN; + } + + /* must be 32bit aligned */ + if (address & 0x3 || len & 0x3) { + fprintf(stderr, "Error: WRITE address and length must be 4 byte aligned\n"); + return STM32_ERR_UNKNOWN; + } + + if (stm->cmd->wm == STM32_CMD_ERR) { + fprintf(stderr, "Error: WRITE command not implemented in bootloader.\n"); + return STM32_ERR_NO_CMD; + } + + /* send the address and checksum */ + if (stm32_send_command(stm, stm->cmd->wm) != STM32_ERR_OK) + return STM32_ERR_UNKNOWN; + + buf[0] = address >> 24; + buf[1] = (address >> 16) & 0xFF; + buf[2] = (address >> 8) & 0xFF; + buf[3] = address & 0xFF; + buf[4] = buf[0] ^ buf[1] ^ buf[2] ^ buf[3]; + if (port->write(port, buf, 5) != PORT_ERR_OK) + return STM32_ERR_UNKNOWN; + if (stm32_get_ack(stm) != STM32_ERR_OK) + return STM32_ERR_UNKNOWN; + + aligned_len = (len + 3) & ~3; + cs = aligned_len - 1; + buf[0] = aligned_len - 1; + for (i = 0; i < len; i++) { + cs ^= data[i]; + buf[i + 1] = data[i]; + } + /* padding data */ + for (i = len; i < aligned_len; i++) { + cs ^= 0xFF; + buf[i + 1] = 0xFF; + } + buf[aligned_len + 1] = cs; + if (port->write(port, buf, aligned_len + 2) != PORT_ERR_OK) + return STM32_ERR_UNKNOWN; + + s_err = stm32_get_ack_timeout(stm, STM32_BLKWRITE_TIMEOUT); + if (s_err != STM32_ERR_OK) { + if (port->flags & PORT_STRETCH_W + && stm->cmd->wm != STM32_CMD_WM_NS) + stm32_warn_stretching("write"); + return STM32_ERR_UNKNOWN; + } + return STM32_ERR_OK; +} + +stm32_err_t stm32_wunprot_memory(const stm32_t *stm) +{ + struct port_interface *port = stm->port; + stm32_err_t s_err; + + if (stm->cmd->uw == STM32_CMD_ERR) { + fprintf(stderr, "Error: WRITE UNPROTECT command not implemented in bootloader.\n"); + return STM32_ERR_NO_CMD; + } + + if (stm32_send_command(stm, stm->cmd->uw) != STM32_ERR_OK) + return STM32_ERR_UNKNOWN; + + s_err = stm32_get_ack_timeout(stm, STM32_WUNPROT_TIMEOUT); + if (s_err == STM32_NACK) { + fprintf(stderr, "Error: Failed to WRITE UNPROTECT\n"); + return STM32_ERR_UNKNOWN; + } + if (s_err != STM32_ERR_OK) { + if (port->flags & PORT_STRETCH_W + && stm->cmd->uw != STM32_CMD_UW_NS) + stm32_warn_stretching("WRITE UNPROTECT"); + return STM32_ERR_UNKNOWN; + } + return STM32_ERR_OK; +} + +stm32_err_t stm32_wprot_memory(const stm32_t *stm) +{ + struct port_interface *port = stm->port; + stm32_err_t s_err; + + if (stm->cmd->wp == STM32_CMD_ERR) { + fprintf(stderr, "Error: WRITE PROTECT command not implemented in bootloader.\n"); + return STM32_ERR_NO_CMD; + } + + if (stm32_send_command(stm, stm->cmd->wp) != STM32_ERR_OK) + return STM32_ERR_UNKNOWN; + + s_err = stm32_get_ack_timeout(stm, STM32_WPROT_TIMEOUT); + if (s_err == STM32_NACK) { + fprintf(stderr, "Error: Failed to WRITE PROTECT\n"); + return STM32_ERR_UNKNOWN; + } + if (s_err != STM32_ERR_OK) { + if (port->flags & PORT_STRETCH_W + && stm->cmd->wp != STM32_CMD_WP_NS) + stm32_warn_stretching("WRITE PROTECT"); + return STM32_ERR_UNKNOWN; + } + return STM32_ERR_OK; +} + +stm32_err_t stm32_runprot_memory(const stm32_t *stm) +{ + struct port_interface *port = stm->port; + stm32_err_t s_err; + + if (stm->cmd->ur == STM32_CMD_ERR) { + fprintf(stderr, "Error: READOUT UNPROTECT command not implemented in bootloader.\n"); + return STM32_ERR_NO_CMD; + } + + if (stm32_send_command(stm, stm->cmd->ur) != STM32_ERR_OK) + return STM32_ERR_UNKNOWN; + + s_err = stm32_get_ack_timeout(stm, STM32_MASSERASE_TIMEOUT); + if (s_err == STM32_NACK) { + fprintf(stderr, "Error: Failed to READOUT UNPROTECT\n"); + return STM32_ERR_UNKNOWN; + } + if (s_err != STM32_ERR_OK) { + if (port->flags & PORT_STRETCH_W + && stm->cmd->ur != STM32_CMD_UR_NS) + stm32_warn_stretching("READOUT UNPROTECT"); + return STM32_ERR_UNKNOWN; + } + return STM32_ERR_OK; +} + +stm32_err_t stm32_readprot_memory(const stm32_t *stm) +{ + struct port_interface *port = stm->port; + stm32_err_t s_err; + + if (stm->cmd->rp == STM32_CMD_ERR) { + fprintf(stderr, "Error: READOUT PROTECT command not implemented in bootloader.\n"); + return STM32_ERR_NO_CMD; + } + + if (stm32_send_command(stm, stm->cmd->rp) != STM32_ERR_OK) + return STM32_ERR_UNKNOWN; + + s_err = stm32_get_ack_timeout(stm, STM32_RPROT_TIMEOUT); + if (s_err == STM32_NACK) { + fprintf(stderr, "Error: Failed to READOUT PROTECT\n"); + return STM32_ERR_UNKNOWN; + } + if (s_err != STM32_ERR_OK) { + if (port->flags & PORT_STRETCH_W + && stm->cmd->rp != STM32_CMD_RP_NS) + stm32_warn_stretching("READOUT PROTECT"); + return STM32_ERR_UNKNOWN; + } + return STM32_ERR_OK; +} + +stm32_err_t stm32_erase_memory(const stm32_t *stm, uint8_t spage, uint8_t pages) +{ + struct port_interface *port = stm->port; + stm32_err_t s_err; + port_err_t p_err; + + if (!pages) + return STM32_ERR_OK; + + if (stm->cmd->er == STM32_CMD_ERR) { + fprintf(stderr, "Error: ERASE command not implemented in bootloader.\n"); + return STM32_ERR_NO_CMD; + } + + if (stm32_send_command(stm, stm->cmd->er) != STM32_ERR_OK) { + fprintf(stderr, "Can't initiate chip erase!\n"); + return STM32_ERR_UNKNOWN; + } + + /* The erase command reported by the bootloader is either 0x43, 0x44 or 0x45 */ + /* 0x44 is Extended Erase, a 2 byte based protocol and needs to be handled differently. */ + /* 0x45 is clock no-stretching version of Extended Erase for I2C port. */ + if (stm->cmd->er != STM32_CMD_ER) { + /* Not all chips using Extended Erase support mass erase */ + /* Currently known as not supporting mass erase is the Ultra Low Power STM32L15xx range */ + /* So if someone has not overridden the default, but uses one of these chips, take it out of */ + /* mass erase mode, so it will be done page by page. This maximum might not be correct either! */ + if (stm->pid == 0x416 && pages == 0xFF) + pages = 0xF8; /* works for the STM32L152RB with 128Kb flash */ + + if (pages == 0xFF) { + uint8_t buf[3]; + + /* 0xFFFF the magic number for mass erase */ + buf[0] = 0xFF; + buf[1] = 0xFF; + buf[2] = 0x00; /* checksum */ + if (port->write(port, buf, 3) != PORT_ERR_OK) { + fprintf(stderr, "Mass erase error.\n"); + return STM32_ERR_UNKNOWN; + } + s_err = stm32_get_ack_timeout(stm, STM32_MASSERASE_TIMEOUT); + if (s_err != STM32_ERR_OK) { + fprintf(stderr, "Mass erase failed. Try specifying the number of pages to be erased.\n"); + if (port->flags & PORT_STRETCH_W + && stm->cmd->er != STM32_CMD_EE_NS) + stm32_warn_stretching("erase"); + return STM32_ERR_UNKNOWN; + } + return STM32_ERR_OK; + } + + uint16_t pg_num; + uint8_t pg_byte; + uint8_t cs = 0; + uint8_t *buf; + int i = 0; + + buf = malloc(2 + 2 * pages + 1); + if (!buf) + return STM32_ERR_UNKNOWN; + + /* Number of pages to be erased - 1, two bytes, MSB first */ + pg_byte = (pages - 1) >> 8; + buf[i++] = pg_byte; + cs ^= pg_byte; + pg_byte = (pages - 1) & 0xFF; + buf[i++] = pg_byte; + cs ^= pg_byte; + + for (pg_num = spage; pg_num < spage + pages; pg_num++) { + pg_byte = pg_num >> 8; + cs ^= pg_byte; + buf[i++] = pg_byte; + pg_byte = pg_num & 0xFF; + cs ^= pg_byte; + buf[i++] = pg_byte; + } + buf[i++] = cs; + p_err = port->write(port, buf, i); + free(buf); + if (p_err != PORT_ERR_OK) { + fprintf(stderr, "Page-by-page erase error.\n"); + return STM32_ERR_UNKNOWN; + } + + s_err = stm32_get_ack_timeout(stm, pages * STM32_SECTERASE_TIMEOUT); + if (s_err != STM32_ERR_OK) { + fprintf(stderr, "Page-by-page erase failed. Check the maximum pages your device supports.\n"); + if (port->flags & PORT_STRETCH_W + && stm->cmd->er != STM32_CMD_EE_NS) + stm32_warn_stretching("erase"); + return STM32_ERR_UNKNOWN; + } + + return STM32_ERR_OK; + } + + /* And now the regular erase (0x43) for all other chips */ + if (pages == 0xFF) { + s_err = stm32_send_command_timeout(stm, 0xFF, STM32_MASSERASE_TIMEOUT); + if (s_err != STM32_ERR_OK) { + if (port->flags & PORT_STRETCH_W) + stm32_warn_stretching("erase"); + return STM32_ERR_UNKNOWN; + } + return STM32_ERR_OK; + } else { + uint8_t cs = 0; + uint8_t pg_num; + uint8_t *buf; + int i = 0; + + buf = malloc(1 + pages + 1); + if (!buf) + return STM32_ERR_UNKNOWN; + + buf[i++] = pages - 1; + cs ^= (pages-1); + for (pg_num = spage; pg_num < (pages + spage); pg_num++) { + buf[i++] = pg_num; + cs ^= pg_num; + } + buf[i++] = cs; + p_err = port->write(port, buf, i); + free(buf); + if (p_err != PORT_ERR_OK) { + fprintf(stderr, "Erase failed.\n"); + return STM32_ERR_UNKNOWN; + } + s_err = stm32_get_ack_timeout(stm, STM32_MASSERASE_TIMEOUT); + if (s_err != STM32_ERR_OK) { + if (port->flags & PORT_STRETCH_W) + stm32_warn_stretching("erase"); + return STM32_ERR_UNKNOWN; + } + return STM32_ERR_OK; + } +} + +static stm32_err_t stm32_run_raw_code(const stm32_t *stm, + uint32_t target_address, + const uint8_t *code, uint32_t code_size) +{ + uint32_t stack_le = le_u32(0x20002000); + uint32_t code_address_le = le_u32(target_address + 8); + uint32_t length = code_size + 8; + uint8_t *mem, *pos; + uint32_t address, w; + + /* Must be 32-bit aligned */ + if (target_address & 0x3) { + fprintf(stderr, "Error: code address must be 4 byte aligned\n"); + return STM32_ERR_UNKNOWN; + } + + mem = malloc(length); + if (!mem) + return STM32_ERR_UNKNOWN; + + memcpy(mem, &stack_le, sizeof(uint32_t)); + memcpy(mem + 4, &code_address_le, sizeof(uint32_t)); + memcpy(mem + 8, code, code_size); + + pos = mem; + address = target_address; + while (length > 0) { + w = length > 256 ? 256 : length; + if (stm32_write_memory(stm, address, pos, w) != STM32_ERR_OK) { + free(mem); + return STM32_ERR_UNKNOWN; + } + + address += w; + pos += w; + length -= w; + } + + free(mem); + return stm32_go(stm, target_address); +} + +stm32_err_t stm32_go(const stm32_t *stm, uint32_t address) +{ + struct port_interface *port = stm->port; + uint8_t buf[5]; + + if (stm->cmd->go == STM32_CMD_ERR) { + fprintf(stderr, "Error: GO command not implemented in bootloader.\n"); + return STM32_ERR_NO_CMD; + } + + if (stm32_send_command(stm, stm->cmd->go) != STM32_ERR_OK) + return STM32_ERR_UNKNOWN; + + buf[0] = address >> 24; + buf[1] = (address >> 16) & 0xFF; + buf[2] = (address >> 8) & 0xFF; + buf[3] = address & 0xFF; + buf[4] = buf[0] ^ buf[1] ^ buf[2] ^ buf[3]; + if (port->write(port, buf, 5) != PORT_ERR_OK) + return STM32_ERR_UNKNOWN; + + if (stm32_get_ack(stm) != STM32_ERR_OK) + return STM32_ERR_UNKNOWN; + return STM32_ERR_OK; +} + +stm32_err_t stm32_reset_device(const stm32_t *stm) +{ + uint32_t target_address = stm->dev->ram_start; + + return stm32_run_raw_code(stm, target_address, stm_reset_code, stm_reset_code_length); +} + +stm32_err_t stm32_crc_memory(const stm32_t *stm, uint32_t address, + uint32_t length, uint32_t *crc) +{ + struct port_interface *port = stm->port; + uint8_t buf[5]; + + if (address & 0x3 || length & 0x3) { + fprintf(stderr, "Start and end addresses must be 4 byte aligned\n"); + return STM32_ERR_UNKNOWN; + } + + if (stm->cmd->crc == STM32_CMD_ERR) { + fprintf(stderr, "Error: CRC command not implemented in bootloader.\n"); + return STM32_ERR_NO_CMD; + } + + if (stm32_send_command(stm, stm->cmd->crc) != STM32_ERR_OK) + return STM32_ERR_UNKNOWN; + + buf[0] = address >> 24; + buf[1] = (address >> 16) & 0xFF; + buf[2] = (address >> 8) & 0xFF; + buf[3] = address & 0xFF; + buf[4] = buf[0] ^ buf[1] ^ buf[2] ^ buf[3]; + if (port->write(port, buf, 5) != PORT_ERR_OK) + return STM32_ERR_UNKNOWN; + + if (stm32_get_ack(stm) != STM32_ERR_OK) + return STM32_ERR_UNKNOWN; + + buf[0] = length >> 24; + buf[1] = (length >> 16) & 0xFF; + buf[2] = (length >> 8) & 0xFF; + buf[3] = length & 0xFF; + buf[4] = buf[0] ^ buf[1] ^ buf[2] ^ buf[3]; + if (port->write(port, buf, 5) != PORT_ERR_OK) + return STM32_ERR_UNKNOWN; + + if (stm32_get_ack(stm) != STM32_ERR_OK) + return STM32_ERR_UNKNOWN; + + if (stm32_get_ack(stm) != STM32_ERR_OK) + return STM32_ERR_UNKNOWN; + + if (port->read(port, buf, 5) != PORT_ERR_OK) + return STM32_ERR_UNKNOWN; + + if (buf[4] != (buf[0] ^ buf[1] ^ buf[2] ^ buf[3])) + return STM32_ERR_UNKNOWN; + + *crc = (buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3]; + return STM32_ERR_OK; +} + +/* + * CRC computed by STM32 is similar to the standard crc32_be() + * implemented, for example, in Linux kernel in ./lib/crc32.c + * But STM32 computes it on units of 32 bits word and swaps the + * bytes of the word before the computation. + * Due to byte swap, I cannot use any CRC available in existing + * libraries, so here is a simple not optimized implementation. + */ +#define CRCPOLY_BE 0x04c11db7 +#define CRC_MSBMASK 0x80000000 +#define CRC_INIT_VALUE 0xFFFFFFFF +uint32_t stm32_sw_crc(uint32_t crc, uint8_t *buf, unsigned int len) +{ + int i; + uint32_t data; + + if (len & 0x3) { + fprintf(stderr, "Buffer length must be multiple of 4 bytes\n"); + return 0; + } + + while (len) { + data = *buf++; + data |= *buf++ << 8; + data |= *buf++ << 16; + data |= *buf++ << 24; + len -= 4; + + crc ^= data; + + for (i = 0; i < 32; i++) + if (crc & CRC_MSBMASK) + crc = (crc << 1) ^ CRCPOLY_BE; + else + crc = (crc << 1); + } + return crc; +} + +stm32_err_t stm32_crc_wrapper(const stm32_t *stm, uint32_t address, + uint32_t length, uint32_t *crc) +{ + uint8_t buf[256]; + uint32_t start, total_len, len, current_crc; + + if (address & 0x3 || length & 0x3) { + fprintf(stderr, "Start and end addresses must be 4 byte aligned\n"); + return STM32_ERR_UNKNOWN; + } + + if (stm->cmd->crc != STM32_CMD_ERR) + return stm32_crc_memory(stm, address, length, crc); + + start = address; + total_len = length; + current_crc = CRC_INIT_VALUE; + while (length) { + len = length > 256 ? 256 : length; + if (stm32_read_memory(stm, address, buf, len) != STM32_ERR_OK) { + fprintf(stderr, + "Failed to read memory at address 0x%08x, target write-protected?\n", + address); + return STM32_ERR_UNKNOWN; + } + current_crc = stm32_sw_crc(current_crc, buf, len); + length -= len; + address += len; + + fprintf(stderr, + "\rCRC address 0x%08x (%.2f%%) ", + address, + (100.0f / (float)total_len) * (float)(address - start) + ); + fflush(stderr); + } + fprintf(stderr, "Done.\n"); + *crc = current_crc; + return STM32_ERR_OK; +} diff --git a/tools/win/src/stm32flash_serial/src/stm32.h b/tools/win/src/stm32flash_serial/src/stm32.h new file mode 100644 index 0000000..1688fcb --- /dev/null +++ b/tools/win/src/stm32flash_serial/src/stm32.h @@ -0,0 +1,84 @@ +/* + stm32flash - Open Source ST STM32 flash program for *nix + Copyright (C) 2010 Geoffrey McRae + + 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. +*/ + + +#ifndef _STM32_H +#define _STM32_H + +#include +#include "serial.h" + +#define STM32_MAX_RX_FRAME 256 /* cmd read memory */ +#define STM32_MAX_TX_FRAME (1 + 256 + 1) /* cmd write memory */ + +typedef enum { + STM32_ERR_OK = 0, + STM32_ERR_UNKNOWN, /* Generic error */ + STM32_ERR_NACK, + STM32_ERR_NO_CMD, /* Command not available in bootloader */ +} stm32_err_t; + +typedef struct stm32 stm32_t; +typedef struct stm32_cmd stm32_cmd_t; +typedef struct stm32_dev stm32_dev_t; + +struct stm32 { + const serial_t *serial; + struct port_interface *port; + uint8_t bl_version; + uint8_t version; + uint8_t option1, option2; + uint16_t pid; + stm32_cmd_t *cmd; + const stm32_dev_t *dev; +}; + +struct stm32_dev { + uint16_t id; + const char *name; + uint32_t ram_start, ram_end; + uint32_t fl_start, fl_end; + uint16_t fl_pps; // pages per sector + uint16_t fl_ps; // page size + uint32_t opt_start, opt_end; + uint32_t mem_start, mem_end; +}; + +stm32_t *stm32_init(struct port_interface *port, const char init); +void stm32_close(stm32_t *stm); +stm32_err_t stm32_read_memory(const stm32_t *stm, uint32_t address, + uint8_t data[], unsigned int len); +stm32_err_t stm32_write_memory(const stm32_t *stm, uint32_t address, + const uint8_t data[], unsigned int len); +stm32_err_t stm32_wunprot_memory(const stm32_t *stm); +stm32_err_t stm32_wprot_memory(const stm32_t *stm); +stm32_err_t stm32_erase_memory(const stm32_t *stm, uint8_t spage, + uint8_t pages); +stm32_err_t stm32_go(const stm32_t *stm, uint32_t address); +stm32_err_t stm32_reset_device(const stm32_t *stm); +stm32_err_t stm32_readprot_memory(const stm32_t *stm); +stm32_err_t stm32_runprot_memory(const stm32_t *stm); +stm32_err_t stm32_crc_memory(const stm32_t *stm, uint32_t address, + uint32_t length, uint32_t *crc); +stm32_err_t stm32_crc_wrapper(const stm32_t *stm, uint32_t address, + uint32_t length, uint32_t *crc); +uint32_t stm32_sw_crc(uint32_t crc, uint8_t *buf, unsigned int len); + +#endif + diff --git a/tools/win/src/stm32flash_serial/src/stm32flash.1 b/tools/win/src/stm32flash_serial/src/stm32flash.1 new file mode 100644 index 0000000..d37292f --- /dev/null +++ b/tools/win/src/stm32flash_serial/src/stm32flash.1 @@ -0,0 +1,407 @@ +.TH STM32FLASH 1 "2013\-11\-03" STM32FLASH "User command" +.SH NAME +stm32flash \- flashing utility for STM32 and STM32W through UART or I2C +.SH SYNOPSIS +.B stm32flash +.RB [ \-cfhjkouvCR ] +.RB [ \-a +.IR bus_address ] +.RB [ \-b +.IR baud_rate ] +.RB [ \-m +.IR serial_mode ] +.RB [ \-r +.IR filename ] +.RB [ \-w +.IR filename ] +.RB [ \-e +.IR num ] +.RB [ \-n +.IR count ] +.RB [ \-g +.IR address ] +.RB [ \-s +.IR start_page ] +.RB [ \-S +.IR address [: length ]] +.RB [ \-F +.IR RX_length [: TX_length ]] +.RB [ \-i +.IR GPIO_string ] +.RI [ tty_device +.R | +.IR i2c_device ] + +.SH DESCRIPTION +.B stm32flash +reads or writes the flash memory of STM32 and STM32W. + +It requires the STM32[W] to embed a bootloader compliant with ST +application note AN3155. +.B stm32flash +uses the serial port +.I tty_device +to interact with the bootloader of STM32[W]. + +.SH OPTIONS +.TP +.BI "\-a" " bus_address" +Specify address on bus for +.IR i2c_device . +This option is mandatory for I2C interface. + +.TP +.BI "\-b" " baud_rate" +Specify baud rate speed of +.IR tty_device . +Please notice that the ST bootloader can automatically detect the baud rate, +as explaned in chapter 2 of AN3155. +This option could be required together with option +.B "\-c" +or if following interaction with bootloader is expected. +Default is +.IR 57600 . + +.TP +.BI "\-m" " mode" +Specify the format of UART data. +.I mode +is a three characters long string where each character specifies, in +this strict order, character size, parity and stop bits. +The only values currenly used are +.I 8e1 +for standard STM32 bootloader and +.I 8n1 +for standard STM32W bootloader. +Default is +.IR 8e1 . + +.TP +.BI "\-r" " filename" +Specify to read the STM32[W] flash and write its content in +.I filename +in raw binary format (see below +.BR "FORMAT CONVERSION" ). + +.TP +.BI "\-w" " filename" +Specify to write the STM32[W] flash with the content of +.IR filename . +File format can be either raw binary or intel hex (see below +.BR "FORMAT CONVERSION" ). +The file format is automatically detected. +To by\-pass format detection and force binary mode (e.g. to +write an intel hex content in STM32[W] flash), use +.B \-f +option. + +.TP +.B \-u +Specify to disable write\-protection from STM32[W] flash. +The STM32[W] will be reset after this operation. + +.TP +.B \-j +Enable the flash read\-protection. + +.TP +.B \-k +Disable the flash read\-protection. + +.TP +.B \-o +Erase only. + +.TP +.BI "\-e" " num" +Specify to erase only +.I num +pages before writing the flash. Default is to erase the whole flash. With +.B \-e 0 +the flash would not be erased. + +.TP +.B \-v +Specify to verify flash content after write operation. + +.TP +.BI "\-n" " count" +Specify to retry failed writes up to +.I count +times. Default is 10 times. + +.TP +.BI "\-g" " address" +Specify address to start execution from (0 = flash start). + +.TP +.BI "\-s" " start_page" +Specify flash page offset (0 = flash start). + +.TP +.BI "\-S" " address" "[:" "length" "]" +Specify start address and optionally length for read/write/erase/crc operations. + +.TP +.BI "\-F" " RX_length" "[:" "TX_length" "]" +Specify the maximum frame size for the current interface. +Due to STM32 bootloader protocol, host will never handle frames bigger than +256 byte in RX or 258 byte in TX. +Due to current code, lowest limit in RX is 20 byte (to read a complete reply +of command GET). Minimum limit in TX is 5 byte, required by protocol. + +.TP +.B \-f +Force binary parser while reading file with +.BR "\-w" "." + +.TP +.B \-h +Show help. + +.TP +.B \-c +Specify to resume the existing UART connection and don't send initial +INIT sequence to detect baud rate. Baud rate must be kept the same as the +existing connection. This is useful if the reset fails. + +.TP +.BI "\-i" " GPIO_string" +Specify the GPIO sequences on the host to force STM32[W] to enter and +exit bootloader mode. GPIO can either be real GPIO connected from host to +STM32[W] beside the UART connection, or UART's modem signals used as +GPIO. (See below +.B BOOTLOADER GPIO SEQUENCE +for the format of +.I GPIO_string +and further explanation). + +.TP +.B \-C +Specify to compute CRC on memory content. +By default the CRC is computed on the whole flash content. +Use +.B "\-S" +to provide different memory address range. + +.TP +.B \-R +Specify to reset the device at exit. +This option is ignored if either +.BR "\-g" "," +.BR "\-j" "," +.B "\-k" +or +.B "\-u" +is also specified. + +.SH BOOTLOADER GPIO SEQUENCE +This feature is currently available on Linux host only. + +As explained in ST application note AN2606, after reset the STM32 will +execute either the application program in user flash or the bootloader, +depending on the level applied at specific pins of STM32 during reset. + +STM32 bootloader is automatically activated by configuring the pins +BOOT0="high" and BOOT1="low" and then by applying a reset. +Application program in user flash is activated by configuring the pin +BOOT0="low" (the level on BOOT1 is ignored) and then by applying a reset. + +When GPIO from host computer are connected to either configuration and +reset pins of STM32, +.B stm32flash +can control the host GPIO to reset STM32 and to force execution of +bootloader or execution of application program. + +The sequence of GPIO values to entry to and exit from bootloader mode is +provided with command line option +.B "\-i" +.IR "GPIO_string" . + +.PD 0 +The format of +.IR "GPIO_string" " is:" +.RS +GPIO_string = [entry sequence][:[exit sequence]] +.P +sequence = [\-]n[,sequence] +.RE +.P +In the above sequences, negative numbers correspond to GPIO at "low" level; +numbers without sign correspond to GPIO at "high" level. +The value "n" can either be the GPIO number on the host system or the +string "rts", "dtr" or "brk". The strings "rts" and "dtr" drive the +corresponding UART's modem lines RTS and DTR as GPIO. +The string "brk" forces the UART to send a BREAK sequence on TX line; +after BREAK the UART is returned in normal "non\-break" mode. +Note: the string "\-brk" has no effect and is ignored. +.PD + +.PD 0 +As example, let's suppose the following connection between host and STM32: +.IP \(bu 2 +host GPIO_3 connected to reset pin of STM32; +.IP \(bu 2 +host GPIO_4 connected to STM32 pin BOOT0; +.IP \(bu 2 +host GPIO_5 connected to STM32 pin BOOT1. +.PD +.P + +In this case, the sequence to enter in bootloader mode is: first put +GPIO_4="high" and GPIO_5="low"; then send reset pulse by GPIO_3="low" +followed by GPIO_3="high". +The corresponding string for +.I GPIO_string +is "4,\-5,\-3,3". + +To exit from bootloade and run the application program, the sequence is: +put GPIO_4="low"; then send reset pulse. +The corresponding string for +.I GPIO_string +is "\-4,\-3,3". + +The complete command line flag is "\-i 4,\-5,\-3,3:\-4,\-3,3". + +STM32W uses pad PA5 to select boot mode; if during reset PA5 is "low" then +STM32W will enter in bootloader mode; if PA5 is "high" it will execute the +program in flash. + +As example, supposing GPIO_3 connected to PA5 and GPIO_2 to STM32W's reset. +The command: +.PD 0 +.RS +stm32flash \-i \-3,\-2,2:3,\-2,2 /dev/ttyS0 +.RE +provides: +.IP \(bu 2 +entry sequence: GPIO_3=low, GPIO_2=low, GPIO_2=high +.IP \(bu 2 +exit sequence: GPIO_3=high, GPIO_2=low, GPIO_2=high +.PD + +.SH EXAMPLES +Get device information: +.RS +.PD 0 +.P +stm32flash /dev/ttyS0 +.PD +.RE + +Write with verify and then start execution: +.RS +.PD 0 +.P +stm32flash \-w filename \-v \-g 0x0 /dev/ttyS0 +.PD +.RE + +Read flash to file: +.RS +.PD 0 +.P +stm32flash \-r filename /dev/ttyS0 +.PD +.RE + +Start execution: +.RS +.PD 0 +.P +stm32flash \-g 0x0 /dev/ttyS0 +.PD +.RE + +Specify: +.PD 0 +.IP \(bu 2 +entry sequence: RTS=low, DTR=low, DTR=high +.IP \(bu 2 +exit sequence: RTS=high, DTR=low, DTR=high +.P +.RS +stm32flash \-i \-rts,\-dtr,dtr:rts,\-dtr,dtr /dev/ttyS0 +.PD +.RE + +.SH FORMAT CONVERSION +Flash images provided by ST or created with ST tools are often in file +format Motorola S\-Record. +Conversion between raw binary, intel hex and Motorola S\-Record can be +done through software package SRecord. + +.SH AUTHORS +The original software package +.B stm32flash +is written by +.I Geoffrey McRae +and is since 2012 maintained by +.IR "Tormod Volden " . + +Man page and extension to STM32W and I2C are written by +.IR "Antonio Borneo " . + +Please report any bugs at the project homepage +http://stm32flash.googlecode.com . + +.SH SEE ALSO +.BR "srec_cat" "(1)," " srec_intel" "(5)," " srec_motorola" "(5)." + +The communication protocol used by ST bootloader is documented in +following ST application notes, depending on communication port. +The current version of +.B stm32flash +only supports +.I UART +and +.I I2C +ports. +.PD 0 +.P +.IP \(bu 2 +AN3154: CAN protocol used in the STM32 bootloader +.P +.RS +http://www.st.com/web/en/resource/technical/document/application_note/CD00264321.pdf +.RE + +.P +.IP \(bu 2 +AN3155: USART protocol used in the STM32(TM) bootloader +.P +.RS +http://www.st.com/web/en/resource/technical/document/application_note/CD00264342.pdf +.RE + +.P +.IP \(bu 2 +AN4221: I2C protocol used in the STM32 bootloader +.P +.RS +http://www.st.com/web/en/resource/technical/document/application_note/DM00072315.pdf +.RE + +.P +.IP \(bu 2 +AN4286: SPI protocol used in the STM32 bootloader +.P +.RS +http://www.st.com/web/en/resource/technical/document/application_note/DM00081379.pdf +.RE + +.PD + + +Boot mode selection for STM32 is documented in ST application note +AN2606, available from the ST website: +.PD 0 +.P +http://www.st.com/web/en/resource/technical/document/application_note/CD00167594.pdf +.PD + +.SH LICENSE +.B stm32flash +is distributed under GNU GENERAL PUBLIC LICENSE Version 2. +Copy of the license is available within the source code in the file +.IR "gpl\-2.0.txt" . diff --git a/tools/win/src/stm32flash_serial/src/utils.c b/tools/win/src/stm32flash_serial/src/utils.c new file mode 100644 index 0000000..271bb3e --- /dev/null +++ b/tools/win/src/stm32flash_serial/src/utils.c @@ -0,0 +1,45 @@ +/* + stm32flash - Open Source ST STM32 flash program for *nix + Copyright (C) 2010 Geoffrey McRae + + 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. +*/ + +#include +#include "utils.h" + +/* detect CPU endian */ +char cpu_le() { + const uint32_t cpu_le_test = 0x12345678; + return ((const unsigned char*)&cpu_le_test)[0] == 0x78; +} + +uint32_t be_u32(const uint32_t v) { + if (cpu_le()) + return ((v & 0xFF000000) >> 24) | + ((v & 0x00FF0000) >> 8) | + ((v & 0x0000FF00) << 8) | + ((v & 0x000000FF) << 24); + return v; +} + +uint32_t le_u32(const uint32_t v) { + if (!cpu_le()) + return ((v & 0xFF000000) >> 24) | + ((v & 0x00FF0000) >> 8) | + ((v & 0x0000FF00) << 8) | + ((v & 0x000000FF) << 24); + return v; +} diff --git a/tools/win/src/stm32flash_serial/src/utils.h b/tools/win/src/stm32flash_serial/src/utils.h new file mode 100644 index 0000000..a8d37d2 --- /dev/null +++ b/tools/win/src/stm32flash_serial/src/utils.h @@ -0,0 +1,30 @@ +/* + stm32flash - Open Source ST STM32 flash program for *nix + Copyright (C) 2010 Geoffrey McRae + + 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. +*/ + + +#ifndef _H_UTILS +#define _H_UTILS + +#include + +char cpu_le(); +uint32_t be_u32(const uint32_t v); +uint32_t le_u32(const uint32_t v); + +#endif diff --git a/tools/win/src/upload-reset/upload-reset.c b/tools/win/src/upload-reset/upload-reset.c new file mode 100644 index 0000000..1d03bff --- /dev/null +++ b/tools/win/src/upload-reset/upload-reset.c @@ -0,0 +1,161 @@ +/* Copyright (C) 2015 Roger Clark + * + * 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. + * + * + * Utility to send the reset sequence on RTS and DTR and chars + * which resets the libmaple and causes the bootloader to be run + * + * + * + * Terminal control code by Heiko Noordhof (see copyright below) + */ + + + +/* Copyright (C) 2003 Heiko Noordhof + * + * 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. + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +/* Function prototypes (belong in a seperate header file) */ +int openserial(char *devicename); +void closeserial(void); +int setDTR(unsigned short level); +int setRTS(unsigned short level); + + +/* Two globals for use by this module only */ +static int fd; +static struct termios oldterminfo; + + +void closeserial(void) +{ + tcsetattr(fd, TCSANOW, &oldterminfo); + close(fd); +} + + +int openserial(char *devicename) +{ + struct termios attr; + + if ((fd = open(devicename, O_RDWR)) == -1) return 0; /* Error */ + atexit(closeserial); + + if (tcgetattr(fd, &oldterminfo) == -1) return 0; /* Error */ + attr = oldterminfo; + attr.c_cflag |= CRTSCTS | CLOCAL; + attr.c_oflag = 0; + if (tcflush(fd, TCIOFLUSH) == -1) return 0; /* Error */ + if (tcsetattr(fd, TCSANOW, &attr) == -1) return 0; /* Error */ + + /* Set the lines to a known state, and */ + /* finally return non-zero is successful. */ + return setRTS(0) && setDTR(0); +} + + +/* For the two functions below: + * level=0 to set line to LOW + * level=1 to set line to HIGH + */ + +int setRTS(unsigned short level) +{ + int status; + + if (ioctl(fd, TIOCMGET, &status) == -1) { + perror("setRTS(): TIOCMGET"); + return 0; + } + if (level) status |= TIOCM_RTS; + else status &= ~TIOCM_RTS; + if (ioctl(fd, TIOCMSET, &status) == -1) { + perror("setRTS(): TIOCMSET"); + return 0; + } + return 1; +} + + +int setDTR(unsigned short level) +{ + int status; + + if (ioctl(fd, TIOCMGET, &status) == -1) { + perror("setDTR(): TIOCMGET"); + return 0; + } + if (level) status |= TIOCM_DTR; + else status &= ~TIOCM_DTR; + if (ioctl(fd, TIOCMSET, &status) == -1) { + perror("setDTR: TIOCMSET"); + return 0; + } + return 1; +} + +/* This portion of code was written by Roger Clark + * It was informed by various other pieces of code written by Leaflabs to reset their + * Maple and Maple mini boards + */ + +main(int argc, char *argv[]) +{ + + if (argc<2 || argc >3) + { + printf("Usage upload-reset \n\r"); + return; + } + + if (openserial(argv[1])) + { + // Send magic sequence of DTR and RTS followed by the magic word "1EAF" + setRTS(false); + setDTR(false); + setDTR(true); + + usleep(50000L); + + setDTR(false); + setRTS(true); + setDTR(true); + + usleep(50000L); + + setDTR(false); + + usleep(50000L); + + write(fd,"1EAF",4); + + closeserial(); + if (argc==3) + { + usleep(atol(argv[2])*1000L); + } + } + else + { + printf("Failed to open serial device.\n\r"); + } +} diff --git a/tools/win/upload_router.bat b/tools/win/upload_router.bat deleted file mode 100644 index c242e99..0000000 --- a/tools/win/upload_router.bat +++ /dev/null @@ -1,56 +0,0 @@ -@echo off -rem: Note %~dp0 get path of this batch file -rem: Need to change drive if My Documents is on a drive other than C: -set driverLetter=%~dp0 -set driverLetter=%driverLetter:~0,2% -%driverLetter% -cd %~dp0 -rem: the two line below are needed to fix path issues with incorrect slashes before the bin file name -set str1=%4 -set str1=%str1:/=\% -set str1=%str1: =% -set str=%str1%.bin -set elf=%str1% - - -set str1=%7 -set str1=%str1:/=\% -set gcc=%str1: =% - - -if %6 == 1 (goto debug) else (if %5 == maple_serial (goto Serial) else (if %5 == maple_dfu (goto maple_loader) else (goto STLink))) -exit - -:debug -debugging.bat %gcc% %elf% -exit - - -:maple_loader - -rem:java -jar maple_loader.jar %1 %2 %3 %4 %5 %6 %7 %8 %9 -echo %1 -echo %2 -echo %3 -java -jar maple_loader.jar %1 %2 %3 %str% -exit - -:Serial -stm32flash -g 0x8000000 -b 230400 -w %str% %1 -rem: C:\Python27\python.exe stm32loader.py -e -w -p %1 -g -b 115200 %str% - -rem: ------------- use STM's own uploader -rem: ---- Need to remove the COM bit from the comm port as the STM prog just wants the number -set commport=%1 -set commportnum=%commport:COM=% -rem: --- The maple board may nee the -i setting to be -i STM32_Med-density_128K or STM32_Med-density_64K -rem: ---- 64 bit version -rem: "%ProgramFiles(x86)%\STMicroelectronics\Software\Flash Loader Demonstrator\STMFlashLoader.exe" -c --pn %commportnum% --br 230400 -i STM32_High-density_256K -e --all -d --fn %str% --a 0x8000000 -r --a 0x8000000 - -rem: -- 32 bit version -rem: "%ProgramFiles%\STMicroelectronics\Software\Flash Loader Demonstrator\STMFlashLoader.exe" -c --pn %commportnum% --br 230400 -i STM32_Med-density_64K -e --all -d --fn %str% --a 0x8000000 -r --a 0x8000000 -exit - -:STLink -stlink\ST-LINK_CLI.exe -c SWD -P %str% 0x8000000 -Rst -Run -exit \ No newline at end of file