pax_global_header00006660000000000000000000000064124776540520014526gustar00rootroot0000000000000052 comment=a746bbe1f30e25484298c8f3f3f23251e8a7f4e7 lpctools-v1.07/000077500000000000000000000000001247765405200135025ustar00rootroot00000000000000lpctools-v1.07/.gitignore000066400000000000000000000006361247765405200154770ustar00rootroot00000000000000# # NOTE! Don't add files that are generated in specific # subdirectories here. Add them in the ".gitignore" file # in that subdirectory instead. # # NOTE! Please use 'git ls-files -i --exclude-standard' # command after changing this file, to see if there are # any tracked files which get ignored after the change. # # Normal rules # *.o *.d *.bin *.map */objs/* *.zip *.dump lpcisp lpcprog lpc_binary_check tags lpctools-v1.07/LICENSE000066400000000000000000001045131247765405200145130ustar00rootroot00000000000000 GNU GENERAL PUBLIC LICENSE Version 3, 29 June 2007 Copyright (C) 2007 Free Software Foundation, Inc. Everyone is permitted to copy and distribute verbatim copies of this license document, but changing it is not allowed. Preamble The GNU General Public License is a free, copyleft license for software and other kinds of works. The licenses for most software and other practical works are designed to take away your freedom to share and change the works. By contrast, the GNU General Public License is intended to guarantee your freedom to share and change all versions of a program--to make sure it remains free software for all its users. We, the Free Software Foundation, use the GNU General Public License for most of our software; it applies also to any other work released this way by its authors. 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 them 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 prevent others from denying you these rights or asking you to surrender the rights. Therefore, you have certain responsibilities if you distribute copies of the software, or if you modify it: responsibilities to respect the freedom of others. For example, if you distribute copies of such a program, whether gratis or for a fee, you must pass on to the recipients the same freedoms that you received. 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. Developers that use the GNU GPL protect your rights with two steps: (1) assert copyright on the software, and (2) offer you this License giving you legal permission to copy, distribute and/or modify it. For the developers' and authors' protection, the GPL clearly explains that there is no warranty for this free software. For both users' and authors' sake, the GPL requires that modified versions be marked as changed, so that their problems will not be attributed erroneously to authors of previous versions. Some devices are designed to deny users access to install or run modified versions of the software inside them, although the manufacturer can do so. This is fundamentally incompatible with the aim of protecting users' freedom to change the software. The systematic pattern of such abuse occurs in the area of products for individuals to use, which is precisely where it is most unacceptable. Therefore, we have designed this version of the GPL to prohibit the practice for those products. If such problems arise substantially in other domains, we stand ready to extend this provision to those domains in future versions of the GPL, as needed to protect the freedom of users. Finally, every program is threatened constantly by software patents. States should not allow patents to restrict development and use of software on general-purpose computers, but in those that do, we wish to avoid the special danger that patents applied to a free program could make it effectively proprietary. To prevent this, the GPL assures that patents cannot be used to render the program non-free. The precise terms and conditions for copying, distribution and modification follow. TERMS AND CONDITIONS 0. Definitions. "This License" refers to version 3 of the GNU General Public License. "Copyright" also means copyright-like laws that apply to other kinds of works, such as semiconductor masks. "The Program" refers to any copyrightable work licensed under this License. Each licensee is addressed as "you". "Licensees" and "recipients" may be individuals or organizations. To "modify" a work means to copy from or adapt all or part of the work in a fashion requiring copyright permission, other than the making of an exact copy. The resulting work is called a "modified version" of the earlier work or a work "based on" the earlier work. A "covered work" means either the unmodified Program or a work based on the Program. To "propagate" a work means to do anything with it that, without permission, would make you directly or secondarily liable for infringement under applicable copyright law, except executing it on a computer or modifying a private copy. Propagation includes copying, distribution (with or without modification), making available to the public, and in some countries other activities as well. To "convey" a work means any kind of propagation that enables other parties to make or receive copies. Mere interaction with a user through a computer network, with no transfer of a copy, is not conveying. An interactive user interface displays "Appropriate Legal Notices" to the extent that it includes a convenient and prominently visible feature that (1) displays an appropriate copyright notice, and (2) tells the user that there is no warranty for the work (except to the extent that warranties are provided), that licensees may convey the work under this License, and how to view a copy of this License. If the interface presents a list of user commands or options, such as a menu, a prominent item in the list meets this criterion. 1. Source Code. The "source code" for a work means the preferred form of the work for making modifications to it. "Object code" means any non-source form of a work. A "Standard Interface" means an interface that either is an official standard defined by a recognized standards body, or, in the case of interfaces specified for a particular programming language, one that is widely used among developers working in that language. The "System Libraries" of an executable work include anything, other than the work as a whole, that (a) is included in the normal form of packaging a Major Component, but which is not part of that Major Component, and (b) serves only to enable use of the work with that Major Component, or to implement a Standard Interface for which an implementation is available to the public in source code form. A "Major Component", in this context, means a major essential component (kernel, window system, and so on) of the specific operating system (if any) on which the executable work runs, or a compiler used to produce the work, or an object code interpreter used to run it. The "Corresponding Source" for a work in object code form means all the source code needed to generate, install, and (for an executable work) run the object code and to modify the work, including scripts to control those activities. However, it does not include the work's System Libraries, or general-purpose tools or generally available free programs which are used unmodified in performing those activities but which are not part of the work. For example, Corresponding Source includes interface definition files associated with source files for the work, and the source code for shared libraries and dynamically linked subprograms that the work is specifically designed to require, such as by intimate data communication or control flow between those subprograms and other parts of the work. The Corresponding Source need not include anything that users can regenerate automatically from other parts of the Corresponding Source. The Corresponding Source for a work in source code form is that same work. 2. Basic Permissions. All rights granted under this License are granted for the term of copyright on the Program, and are irrevocable provided the stated conditions are met. This License explicitly affirms your unlimited permission to run the unmodified Program. The output from running a covered work is covered by this License only if the output, given its content, constitutes a covered work. This License acknowledges your rights of fair use or other equivalent, as provided by copyright law. You may make, run and propagate covered works that you do not convey, without conditions so long as your license otherwise remains in force. You may convey covered works to others for the sole purpose of having them make modifications exclusively for you, or provide you with facilities for running those works, provided that you comply with the terms of this License in conveying all material for which you do not control copyright. Those thus making or running the covered works for you must do so exclusively on your behalf, under your direction and control, on terms that prohibit them from making any copies of your copyrighted material outside their relationship with you. Conveying under any other circumstances is permitted solely under the conditions stated below. Sublicensing is not allowed; section 10 makes it unnecessary. 3. Protecting Users' Legal Rights From Anti-Circumvention Law. No covered work shall be deemed part of an effective technological measure under any applicable law fulfilling obligations under article 11 of the WIPO copyright treaty adopted on 20 December 1996, or similar laws prohibiting or restricting circumvention of such measures. When you convey a covered work, you waive any legal power to forbid circumvention of technological measures to the extent such circumvention is effected by exercising rights under this License with respect to the covered work, and you disclaim any intention to limit operation or modification of the work as a means of enforcing, against the work's users, your or third parties' legal rights to forbid circumvention of technological measures. 4. Conveying Verbatim Copies. You may convey 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; keep intact all notices stating that this License and any non-permissive terms added in accord with section 7 apply to the code; keep intact all notices of the absence of any warranty; and give all recipients a copy of this License along with the Program. You may charge any price or no price for each copy that you convey, and you may offer support or warranty protection for a fee. 5. Conveying Modified Source Versions. You may convey a work based on the Program, or the modifications to produce it from the Program, in the form of source code under the terms of section 4, provided that you also meet all of these conditions: a) The work must carry prominent notices stating that you modified it, and giving a relevant date. b) The work must carry prominent notices stating that it is released under this License and any conditions added under section 7. This requirement modifies the requirement in section 4 to "keep intact all notices". c) You must license the entire work, as a whole, under this License to anyone who comes into possession of a copy. This License will therefore apply, along with any applicable section 7 additional terms, to the whole of the work, and all its parts, regardless of how they are packaged. This License gives no permission to license the work in any other way, but it does not invalidate such permission if you have separately received it. d) If the work has interactive user interfaces, each must display Appropriate Legal Notices; however, if the Program has interactive interfaces that do not display Appropriate Legal Notices, your work need not make them do so. A compilation of a covered work with other separate and independent works, which are not by their nature extensions of the covered work, and which are not combined with it such as to form a larger program, in or on a volume of a storage or distribution medium, is called an "aggregate" if the compilation and its resulting copyright are not used to limit the access or legal rights of the compilation's users beyond what the individual works permit. Inclusion of a covered work in an aggregate does not cause this License to apply to the other parts of the aggregate. 6. Conveying Non-Source Forms. You may convey a covered work in object code form under the terms of sections 4 and 5, provided that you also convey the machine-readable Corresponding Source under the terms of this License, in one of these ways: a) Convey the object code in, or embodied in, a physical product (including a physical distribution medium), accompanied by the Corresponding Source fixed on a durable physical medium customarily used for software interchange. b) Convey the object code in, or embodied in, a physical product (including a physical distribution medium), accompanied by a written offer, valid for at least three years and valid for as long as you offer spare parts or customer support for that product model, to give anyone who possesses the object code either (1) a copy of the Corresponding Source for all the software in the product that is covered by this License, on a durable physical medium customarily used for software interchange, for a price no more than your reasonable cost of physically performing this conveying of source, or (2) access to copy the Corresponding Source from a network server at no charge. c) Convey individual copies of the object code with a copy of the written offer to provide the Corresponding Source. This alternative is allowed only occasionally and noncommercially, and only if you received the object code with such an offer, in accord with subsection 6b. d) Convey the object code by offering access from a designated place (gratis or for a charge), and offer equivalent access to the Corresponding Source in the same way through the same place at no further charge. You need not require recipients to copy the Corresponding Source along with the object code. If the place to copy the object code is a network server, the Corresponding Source may be on a different server (operated by you or a third party) that supports equivalent copying facilities, provided you maintain clear directions next to the object code saying where to find the Corresponding Source. Regardless of what server hosts the Corresponding Source, you remain obligated to ensure that it is available for as long as needed to satisfy these requirements. e) Convey the object code using peer-to-peer transmission, provided you inform other peers where the object code and Corresponding Source of the work are being offered to the general public at no charge under subsection 6d. A separable portion of the object code, whose source code is excluded from the Corresponding Source as a System Library, need not be included in conveying the object code work. A "User Product" is either (1) a "consumer product", which means any tangible personal property which is normally used for personal, family, or household purposes, or (2) anything designed or sold for incorporation into a dwelling. In determining whether a product is a consumer product, doubtful cases shall be resolved in favor of coverage. For a particular product received by a particular user, "normally used" refers to a typical or common use of that class of product, regardless of the status of the particular user or of the way in which the particular user actually uses, or expects or is expected to use, the product. A product is a consumer product regardless of whether the product has substantial commercial, industrial or non-consumer uses, unless such uses represent the only significant mode of use of the product. "Installation Information" for a User Product means any methods, procedures, authorization keys, or other information required to install and execute modified versions of a covered work in that User Product from a modified version of its Corresponding Source. The information must suffice to ensure that the continued functioning of the modified object code is in no case prevented or interfered with solely because modification has been made. If you convey an object code work under this section in, or with, or specifically for use in, a User Product, and the conveying occurs as part of a transaction in which the right of possession and use of the User Product is transferred to the recipient in perpetuity or for a fixed term (regardless of how the transaction is characterized), the Corresponding Source conveyed under this section must be accompanied by the Installation Information. But this requirement does not apply if neither you nor any third party retains the ability to install modified object code on the User Product (for example, the work has been installed in ROM). The requirement to provide Installation Information does not include a requirement to continue to provide support service, warranty, or updates for a work that has been modified or installed by the recipient, or for the User Product in which it has been modified or installed. Access to a network may be denied when the modification itself materially and adversely affects the operation of the network or violates the rules and protocols for communication across the network. Corresponding Source conveyed, and Installation Information provided, in accord with this section must be in a format that is publicly documented (and with an implementation available to the public in source code form), and must require no special password or key for unpacking, reading or copying. 7. Additional Terms. "Additional permissions" are terms that supplement the terms of this License by making exceptions from one or more of its conditions. Additional permissions that are applicable to the entire Program shall be treated as though they were included in this License, to the extent that they are valid under applicable law. If additional permissions apply only to part of the Program, that part may be used separately under those permissions, but the entire Program remains governed by this License without regard to the additional permissions. When you convey a copy of a covered work, you may at your option remove any additional permissions from that copy, or from any part of it. (Additional permissions may be written to require their own removal in certain cases when you modify the work.) You may place additional permissions on material, added by you to a covered work, for which you have or can give appropriate copyright permission. Notwithstanding any other provision of this License, for material you add to a covered work, you may (if authorized by the copyright holders of that material) supplement the terms of this License with terms: a) Disclaiming warranty or limiting liability differently from the terms of sections 15 and 16 of this License; or b) Requiring preservation of specified reasonable legal notices or author attributions in that material or in the Appropriate Legal Notices displayed by works containing it; or c) Prohibiting misrepresentation of the origin of that material, or requiring that modified versions of such material be marked in reasonable ways as different from the original version; or d) Limiting the use for publicity purposes of names of licensors or authors of the material; or e) Declining to grant rights under trademark law for use of some trade names, trademarks, or service marks; or f) Requiring indemnification of licensors and authors of that material by anyone who conveys the material (or modified versions of it) with contractual assumptions of liability to the recipient, for any liability that these contractual assumptions directly impose on those licensors and authors. All other non-permissive additional terms are considered "further restrictions" within the meaning of section 10. If the Program as you received it, or any part of it, contains a notice stating that it is governed by this License along with a term that is a further restriction, you may remove that term. If a license document contains a further restriction but permits relicensing or conveying under this License, you may add to a covered work material governed by the terms of that license document, provided that the further restriction does not survive such relicensing or conveying. If you add terms to a covered work in accord with this section, you must place, in the relevant source files, a statement of the additional terms that apply to those files, or a notice indicating where to find the applicable terms. Additional terms, permissive or non-permissive, may be stated in the form of a separately written license, or stated as exceptions; the above requirements apply either way. 8. Termination. You may not propagate or modify a covered work except as expressly provided under this License. Any attempt otherwise to propagate or modify it is void, and will automatically terminate your rights under this License (including any patent licenses granted under the third paragraph of section 11). However, if you cease all violation of this License, then your license from a particular copyright holder is reinstated (a) provisionally, unless and until the copyright holder explicitly and finally terminates your license, and (b) permanently, if the copyright holder fails to notify you of the violation by some reasonable means prior to 60 days after the cessation. Moreover, your license from a particular copyright holder is reinstated permanently if the copyright holder notifies you of the violation by some reasonable means, this is the first time you have received notice of violation of this License (for any work) from that copyright holder, and you cure the violation prior to 30 days after your receipt of the notice. Termination of your rights under this section does not terminate the licenses of parties who have received copies or rights from you under this License. If your rights have been terminated and not permanently reinstated, you do not qualify to receive new licenses for the same material under section 10. 9. Acceptance Not Required for Having Copies. You are not required to accept this License in order to receive or run a copy of the Program. Ancillary propagation of a covered work occurring solely as a consequence of using peer-to-peer transmission to receive a copy likewise does not require acceptance. However, nothing other than this License grants you permission to propagate or modify any covered work. These actions infringe copyright if you do not accept this License. Therefore, by modifying or propagating a covered work, you indicate your acceptance of this License to do so. 10. Automatic Licensing of Downstream Recipients. Each time you convey a covered work, the recipient automatically receives a license from the original licensors, to run, modify and propagate that work, subject to this License. You are not responsible for enforcing compliance by third parties with this License. An "entity transaction" is a transaction transferring control of an organization, or substantially all assets of one, or subdividing an organization, or merging organizations. If propagation of a covered work results from an entity transaction, each party to that transaction who receives a copy of the work also receives whatever licenses to the work the party's predecessor in interest had or could give under the previous paragraph, plus a right to possession of the Corresponding Source of the work from the predecessor in interest, if the predecessor has it or can get it with reasonable efforts. You may not impose any further restrictions on the exercise of the rights granted or affirmed under this License. For example, you may not impose a license fee, royalty, or other charge for exercise of rights granted under this License, and you may not initiate litigation (including a cross-claim or counterclaim in a lawsuit) alleging that any patent claim is infringed by making, using, selling, offering for sale, or importing the Program or any portion of it. 11. Patents. A "contributor" is a copyright holder who authorizes use under this License of the Program or a work on which the Program is based. The work thus licensed is called the contributor's "contributor version". A contributor's "essential patent claims" are all patent claims owned or controlled by the contributor, whether already acquired or hereafter acquired, that would be infringed by some manner, permitted by this License, of making, using, or selling its contributor version, but do not include claims that would be infringed only as a consequence of further modification of the contributor version. For purposes of this definition, "control" includes the right to grant patent sublicenses in a manner consistent with the requirements of this License. Each contributor grants you a non-exclusive, worldwide, royalty-free patent license under the contributor's essential patent claims, to make, use, sell, offer for sale, import and otherwise run, modify and propagate the contents of its contributor version. In the following three paragraphs, a "patent license" is any express agreement or commitment, however denominated, not to enforce a patent (such as an express permission to practice a patent or covenant not to sue for patent infringement). To "grant" such a patent license to a party means to make such an agreement or commitment not to enforce a patent against the party. If you convey a covered work, knowingly relying on a patent license, and the Corresponding Source of the work is not available for anyone to copy, free of charge and under the terms of this License, through a publicly available network server or other readily accessible means, then you must either (1) cause the Corresponding Source to be so available, or (2) arrange to deprive yourself of the benefit of the patent license for this particular work, or (3) arrange, in a manner consistent with the requirements of this License, to extend the patent license to downstream recipients. "Knowingly relying" means you have actual knowledge that, but for the patent license, your conveying the covered work in a country, or your recipient's use of the covered work in a country, would infringe one or more identifiable patents in that country that you have reason to believe are valid. If, pursuant to or in connection with a single transaction or arrangement, you convey, or propagate by procuring conveyance of, a covered work, and grant a patent license to some of the parties receiving the covered work authorizing them to use, propagate, modify or convey a specific copy of the covered work, then the patent license you grant is automatically extended to all recipients of the covered work and works based on it. A patent license is "discriminatory" if it does not include within the scope of its coverage, prohibits the exercise of, or is conditioned on the non-exercise of one or more of the rights that are specifically granted under this License. You may not convey a covered work if you are a party to an arrangement with a third party that is in the business of distributing software, under which you make payment to the third party based on the extent of your activity of conveying the work, and under which the third party grants, to any of the parties who would receive the covered work from you, a discriminatory patent license (a) in connection with copies of the covered work conveyed by you (or copies made from those copies), or (b) primarily for and in connection with specific products or compilations that contain the covered work, unless you entered into that arrangement, or that patent license was granted, prior to 28 March 2007. Nothing in this License shall be construed as excluding or limiting any implied license or other defenses to infringement that may otherwise be available to you under applicable patent law. 12. No Surrender of Others' Freedom. If 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 convey a covered work so as to satisfy simultaneously your obligations under this License and any other pertinent obligations, then as a consequence you may not convey it at all. For example, if you agree to terms that obligate you to collect a royalty for further conveying from those to whom you convey the Program, the only way you could satisfy both those terms and this License would be to refrain entirely from conveying the Program. 13. Use with the GNU Affero General Public License. Notwithstanding any other provision of this License, you have permission to link or combine any covered work with a work licensed under version 3 of the GNU Affero General Public License into a single combined work, and to convey the resulting work. The terms of this License will continue to apply to the part which is the covered work, but the special requirements of the GNU Affero General Public License, section 13, concerning interaction through a network will apply to the combination as such. 14. Revised Versions of this License. The Free Software Foundation may publish revised and/or new versions of the GNU 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 that a certain numbered version of the GNU General Public License "or any later version" applies to it, you have the option of following the terms and conditions either of that numbered version or of any later version published by the Free Software Foundation. If the Program does not specify a version number of the GNU General Public License, you may choose any version ever published by the Free Software Foundation. If the Program specifies that a proxy can decide which future versions of the GNU General Public License can be used, that proxy's public statement of acceptance of a version permanently authorizes you to choose that version for the Program. Later license versions may give you additional or different permissions. However, no additional obligations are imposed on any author or copyright holder as a result of your choosing to follow a later version. 15. Disclaimer of Warranty. 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. 16. Limitation of Liability. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS 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. 17. Interpretation of Sections 15 and 16. If the disclaimer of warranty and limitation of liability provided above cannot be given local legal effect according to their terms, reviewing courts shall apply local law that most closely approximates an absolute waiver of all civil liability in connection with the Program, unless a warranty or assumption of liability accompanies a copy of the Program in return for a fee. 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 state 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 3 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, see . Also add information on how to contact you by electronic and paper mail. If the program does terminal interaction, make it output a short notice like this when it starts in an interactive mode: Copyright (C) This program 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, your program's commands might be different; for a GUI interface, you would use an "about box". You should also get your employer (if you work as a programmer) or school, if any, to sign a "copyright disclaimer" for the program, if necessary. For more information on this, and how to apply and follow the GNU GPL, see . The GNU 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. But first, please read . lpctools-v1.07/Makefile000066400000000000000000000020261247765405200151420ustar00rootroot00000000000000# Makefile for Tools CC = $(CROSS_COMPILE)gcc CFLAGS += -Wall -Wextra -O2 all: lpcisp lpcprog lpc_binary_check OBJDIR = objs SRC = $(shell find . -name \*.c) OBJS = ${SRC:%.c=${OBJDIR}/%.o} LPCISP_OBJS = ${OBJDIR}/lpcisp.o \ ${OBJDIR}/isp_utils.o \ ${OBJDIR}/isp_commands.o \ ${OBJDIR}/isp_wrapper.o LPCPROG_OBJS = ${OBJDIR}/lpcprog.o \ ${OBJDIR}/isp_utils.o \ ${OBJDIR}/isp_commands.o \ ${OBJDIR}/prog_commands.o \ ${OBJDIR}/parts.o LPCCHECK_OBJS = ${OBJDIR}/check.o \ ${OBJDIR}/isp_utils.o lpcisp: $(LPCISP_OBJS) @echo "Linking $@ ..." @$(CC) $(LDFLAGS) $(LPCISP_OBJS) -o $@ @echo Done. lpcprog: $(LPCPROG_OBJS) @echo "Linking $@ ..." @$(CC) $(LDFLAGS) $(LPCPROG_OBJS) -o $@ @echo Done. lpc_binary_check: $(LPCCHECK_OBJS) @echo "Linking $@ ..." @$(CC) $(LDFLAGS) $(LPCCHECK_OBJS) -o $@ @echo Done. ${OBJDIR}/%.o: %.c @mkdir -p $(dir $@) @echo "-- compiling" $< @$(CC) -MMD -MP -MF ${OBJDIR}/$*.d $(CPPFLAGS) $(CFLAGS) $< -c -o $@ clean: rm -f ${OBJDIR}/* mrproper: clean rm -f lpcisp rm -f lpcprog lpctools-v1.07/README000066400000000000000000000024041247765405200143620ustar00rootroot00000000000000LPCTools is an interface to NXP LPC Microcontrollers ISP (In-System Programming) serial interface. It is split in two programs: lpcisp and lpcprog * lpcisp: This tool gives access to each of the useful isp commands on LPC devices. It does not provide wrappers for flashing a device. * lpcprog: This tool does not give access to each isp command, instead it provides wrappers for flashing a device. Both programs were originally written by Nathael Pajani because existing programs were published under non-free licences, did not allow comercial use, or did not provide source code. * lpc_binary_check: This third tool is an additionnal helper programm created to change the user code and check that the CRP protection is not enabled in a binary image so that it can be uploaded to a target with different tools. These programs are released under the terms of the GNU GPLv3 licence as can be found on the GNU website : or in the included LICENSE file. ******************** TODO : Allow flashing of parts with flash sectors of different sizes (LPC1764 has 16 small 4k sectors and two big 32k sectors). --> Need either to change parts definition format or to flash in two times. Add the definition of all LPC parts ? lpctools-v1.07/check.c000066400000000000000000000113611247765405200147250ustar00rootroot00000000000000/********************************************************************* * * ISP (In-System Programming) companion tool for NXP LPC Microcontrollers * * This tool allows user-code computation and CRP protection check for NXP LPC * microcontrollers. * * * Copyright (C) 2012 Nathael Pajani * * 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 3 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, see . * *********************************************************************/ #include /* malloc, free */ #include #include #include /* open, getopt, close, usleep */ #include #include #include #include #include #include /* for serial config */ #include #include /* strncmp, strlen, strdup */ #include "isp_utils.h" #define PROG_NAME "LPC binary check" #define VERSION "1.07" void help(char *prog_name) { fprintf(stderr, "---------------- "PROG_NAME" --------------------------------\n"); fprintf(stderr, "Usage: %s [options] [prog file name]\n" \ " Available options:\n" \ " \t -n | --no-user-code : do not compute a valid user code for exception vector 7\n" \ " \t -s | --skip-crp-verification : do not perform CRP code verification\n" \ " \t -h | --help : display this help\n" \ " \t -v | --version : display version information\n", prog_name); fprintf(stderr, "-----------------------------------------------------------------------\n"); } int trace_on = 0; static int calc_user_code = 1; /* User code is computed by default */ static int check_crp = 1; /* CRP is checked by default */ int main(int argc, char** argv) { char* filename = NULL; char* data = NULL; struct stat stat_buffer; int size = 0; uint32_t* v = NULL; /* Used for checksum computing */ uint32_t cksum = 0; uint32_t crp = 0; int ret = 0; /* parameter parsing */ while(1) { int option_index = 0; int c = 0; struct option long_options[] = { {"no-user-code", no_argument, 0, 'n'}, {"skip-crp-verification", no_argument, 0, 's'}, {"help", no_argument, 0, 'h'}, {"version", no_argument, 0, 'v'}, {0, 0, 0, 0} }; c = getopt_long(argc, argv, "nshv", long_options, &option_index); /* no more options to parse */ if (c == -1) break; switch (c) { /* n, user-code */ case 'n': calc_user_code = 0; break; /* s, skip CRP verification */ case 's': check_crp = 0; break; /* v, version */ case 'v': printf("%s Version %s\n", PROG_NAME, VERSION); return 0; break; /* h, help */ case 'h': default: help(argv[0]); return -1; } } /* Parse remaining command line arguments (not options). It should be the binary file name */ if ((optind >= argc) || ((argc - optind) != 1)) { printf("Need one (and only one) file name to check.\n"); return -1; } filename = argv[optind]; /* Get information on the file */ if (stat(filename, &stat_buffer) == -1) { perror("stat failed"); printf("Unable to get informations on file \"%s\"\n", filename); return -2; } /* Allocate a buffer big enougth to get the CRP in it */ data = malloc(stat_buffer.st_size); if (data == NULL) { printf("Unable to get a buffer to load the image!\n"); return -2; } size = isp_file_to_buff(data, stat_buffer.st_size, filename); if (size <= 0){ free(data); return -2; } /* And check checksum of first 7 vectors if asked, according to section 21.3.3 of * LPC11xx user's manual (UM10398) */ v = (uint32_t *)data; cksum = 0 - v[0] - v[1] - v[2] - v[3] - v[4] - v[5] - v[6]; if (calc_user_code == 1) { v[7] = cksum; isp_buff_to_file(data, size, filename); printf("Checksum check done. File updated.\n"); } else if (cksum != v[7]) { printf("Checksum is 0x%08x, should be 0x%08x\n", v[7], cksum); ret = -3; } if (check_crp == 1) { crp = v[(CRP_OFFSET / 4)]; printf("CRP : 0x%08x\n", crp); if ((crp == CRP_NO_ISP) || (crp == CRP_CRP1) || (crp == CRP_CRP2) || (crp == CRP_CRP3)) { printf("CRP : 0x%08x\n", crp); printf("The binary has CRP protection ativated, which violates GPLv3.\n"); printf("Check the licence for the software you are about to flash.\n"); ret = -4; } } free(data); return ret; } lpctools-v1.07/isp_commands.c000066400000000000000000000440651247765405200163330ustar00rootroot00000000000000/********************************************************************* * * LPC ISP Commands * * * Copyright (C) 2012 Nathael Pajani * * 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 3 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, see . * *********************************************************************/ #include /* strtoul */ #include #include #include #include /* strncmp */ #include #include #include /* for open, close */ #include #include #include #include "isp_utils.h" extern int trace_on; /* Max should be 1270 for read memory, a little bit more for writes */ #define SERIAL_BUFSIZE 1300 #define REP_BUFSIZE 100 #define SYNCHRO_START "?" #define SYNCHRO "Synchronized\r\n" #define SYNCHRO_OK "OK\r\n" #define DATA_BLOCK_OK "OK\r\n" #define DATA_BLOCK_RESEND "RESEND\r\n" #define SYNCHRO_ECHO_OFF "A 0\r\n" #define ISP_ABORT "" /* Not handled */ #define UNLOCK "U 23130\r\n" #define READ_UID "N\r\n" #define READ_PART_ID "J\r\n" #define READ_BOOT_VERSION "K\r\n" #define LINE_DATA_LENGTH 45 #define LINES_PER_BLOCK 20 #define MAX_DATA_BLOCK_SIZE (LINES_PER_BLOCK * LINE_DATA_LENGTH) /* 900 */ #define MAX_BYTES_PER_LINE (1 + ((LINE_DATA_LENGTH / 3) * 4) + 2) /* "line length" (1) + 60 + \r\n */ #if (SERIAL_BUFSIZE < (MAX_BYTES_PER_LINE * LINES_PER_BLOCK + 10 + 2)) /* uuencoded data + checksum + \r\n */ #error "SERIAL_BUFSIZE too small" #endif /* Order in this table is important */ char* error_codes[] = { "CMD_SUCCESS", "INVALID_COMMAND", "SRC_ADDR_ERROR", "DST_ADDR_ERROR", "SRC_ADDR_NOT_MAPPED", "DST_ADDR_NOT_MAPPED", "COUNT_ERROR", "INVALID_SECTOR", "SECTOR_NOT_BLANK", "SECTOR_NOT_PREPARED_FOR_WRITE_OPERATION", "COMPARE_ERROR", "BUSY", "PARAM_ERROR", "ADDR_ERROR", "ADDR_NOT_MAPPED", "CMD_LOCKED", "INVALID_CODE", "INVALID_BAUD_RATE", "INVALID_STOP_BIT", "CODE_READ_PROTECTION_ENABLED", }; static int isp_ret_code(char* buf, char** endptr, int quiet) { unsigned int ret = 0; ret = strtoul(buf, endptr, 10); /* FIXME : Find how return code are sent (binary or ASCII) */ if (quiet != 1) { if (ret > (sizeof(error_codes)/sizeof(char*))) { printf("Received unknown error code '%u' !\n", ret); } else if ((ret != 0) || trace_on) { printf("Received error code '%u': %s\n", ret, error_codes[ret]); } } return ret; } /* Connect or reconnect to the target. * crystal_freq is in KHz * Return positive or NULL value when connection is OK, or negative value otherwise. */ int isp_connect(unsigned int crystal_freq, int quiet) { char buf[REP_BUFSIZE]; char freq[10]; snprintf(freq, 8, "%d\r\n", crystal_freq); /* Send synchronize request */ if (isp_serial_write(SYNCHRO_START, strlen(SYNCHRO_START)) != strlen(SYNCHRO_START)) { printf("Unable to send synchronize request.\n"); return -5; } /* Wait for answer */ if (isp_serial_read(buf, REP_BUFSIZE, strlen(SYNCHRO)) < 0) { printf("Error reading synchronize answer.\n"); return -4; } /* Check answer, and acknowledge if OK */ if (strncmp(SYNCHRO, buf, strlen(SYNCHRO)) == 0) { isp_serial_write(SYNCHRO, strlen(SYNCHRO)); } else { if (quiet != 1) { printf("Unable to synchronize, no synchro received.\n"); } return -3; } /* Empty read buffer (echo is on) */ isp_serial_empty_buffer(); /* Read reply (OK) */ isp_serial_read(buf, REP_BUFSIZE, strlen(SYNCHRO_OK)); if (strncmp(SYNCHRO_OK, buf, strlen(SYNCHRO_OK)) != 0) { printf("Unable to synchronize, synchro not acknowledged.\n"); return -2; } /* Documentation says we should send crystal frequency .. sending anything is OK */ isp_serial_write(freq, strlen(freq)); /* Empty read buffer (echo is on) */ isp_serial_empty_buffer(); /* Read reply (OK) */ isp_serial_read(buf, REP_BUFSIZE, strlen(SYNCHRO_OK)); if (strncmp(SYNCHRO_OK, buf, strlen(SYNCHRO_OK)) != 0) { printf("Unable to synchronize, crystal frequency not acknowledged.\n"); return -2; } /* Turn off echo */ isp_serial_write(SYNCHRO_ECHO_OFF, strlen(SYNCHRO_ECHO_OFF)); /* Empty read buffer (echo still on) */ isp_serial_empty_buffer(); /* Read eror code for command */ isp_serial_read(buf, REP_BUFSIZE, 3); /* Leave it even in quiet mode, so the user knows something is going on */ printf("Device session openned.\n"); return 1; } int isp_send_cmd_no_args(char* cmd_name, char* cmd, int quiet) { char buf[5]; int ret = 0, len = 0; /* Send request */ if (isp_serial_write(cmd, strlen(cmd)) != (int)strlen(cmd)) { printf("Unable to send %s request.\n", cmd_name); return -5; } /* Wait for answer */ usleep( 5000 ); len = isp_serial_read(buf, 3, 3); if (len <= 0) { printf("Error reading %s acknowledge.\n", cmd_name); return -4; } ret = isp_ret_code(buf, NULL, quiet); return ret; } int isp_cmd_unlock(int quiet) { int ret = 0; ret = isp_send_cmd_no_args("unlock", UNLOCK, quiet); if (ret != 0) { printf("Unlock error.\n"); return -1; } if (quiet == 0) { printf("Device memory protection unlocked.\n"); } return 0; } int isp_cmd_read_uid(void) { char buf[REP_BUFSIZE]; char* tmp = NULL; int i = 0, ret = 0, len = 0; unsigned long int uid[4]; ret = isp_send_cmd_no_args("read-uid", READ_UID, 0); if (ret != 0) { printf("Read UID error.\n"); return ret; } len = isp_serial_read(buf, REP_BUFSIZE, 50); if (len <= 0) { printf("Error reading uid.\n"); return -2; } tmp = buf; for (i=0; i<4; i++) { static char* endptr = NULL; uid[i] = strtoul(tmp, &endptr, 10); tmp = endptr; } printf("UID: 0x%08lx - 0x%08lx - 0x%08lx - 0x%08lx\n", uid[0], uid[1], uid[2], uid[3]); return 0; } int isp_cmd_part_id(int quiet) { char buf[REP_BUFSIZE]; int ret = 0, len = 0; unsigned long int part_id = 0; ret = isp_send_cmd_no_args("read-part-id", READ_PART_ID, quiet); if (ret != 0) { if (quiet != 1) { printf("Read part ID error.\n"); } return ret; } len = isp_serial_read(buf, REP_BUFSIZE, 15); if (len <= 0) { printf("Error reading part ID.\n"); return -2; } /* FIXME : some part IDs are on two 32bits values */ part_id = strtoul(buf, NULL, 10); if (quiet == 0) { printf("Part ID is 0x%08lx\n", part_id); } return part_id; } int isp_cmd_boot_version(void) { char buf[REP_BUFSIZE]; int ret = 0, len = 0; char* tmp = NULL; unsigned int ver[2]; ret = isp_send_cmd_no_args("read-boot-version", READ_BOOT_VERSION, 0); if (ret != 0) { printf("Read boot version error.\n"); return ret; } len = isp_serial_read(buf, REP_BUFSIZE, 20); if (len <= 0) { printf("Error reading boot version.\n"); return -2; } ver[0] = strtoul(buf, &tmp, 10); ver[1] = strtoul(tmp, NULL, 10); printf("Boot code version is %u.%u\n", ver[1], ver[0]); return 0; } int isp_send_cmd_two_args(char* cmd_name, char cmd, unsigned int arg1, unsigned int arg2) { char buf[REP_BUFSIZE]; int ret = 0, len = 0; /* Create read-memory request */ len = snprintf(buf, REP_BUFSIZE, "%c %u %u\r\n", cmd, arg1, arg2); if (len > REP_BUFSIZE) { len = REP_BUFSIZE; /* This should not happen if REP_BUFSIZE is 32 or more ... */ } /* Send request */ if (isp_serial_write(buf, len) != len) { printf("Unable to send %s request.\n", cmd_name); return -5; } /* Wait for answer */ usleep( 5000 ); len = isp_serial_read(buf, 3, 3); if (len <= 0) { printf("Error reading %s acknowledge.\n", cmd_name); return -4; } ret = isp_ret_code(buf, NULL, 0); return ret; } static int get_remaining_blocksize(unsigned int count, unsigned int actual_count, char* dir, unsigned int i) { unsigned int blocksize = 0; unsigned int remain = count - actual_count; if (remain >= MAX_DATA_BLOCK_SIZE) { blocksize = (MAX_BYTES_PER_LINE * LINES_PER_BLOCK); if (trace_on) { printf("%s block %d (%d line(s)).\n", dir, i, LINES_PER_BLOCK); } } else { unsigned int nb_last_lines = (remain / LINE_DATA_LENGTH); if (remain % LINE_DATA_LENGTH) { nb_last_lines += 1; } /* 4 bytes transmitted for each 3 data bytes */ blocksize = ((remain / 3) * 4); if (remain % 3) { blocksize += 4; } /* Add one byte per line for the starting "length character" and two bytes per * line for the ending "\r\n" */ blocksize += (3 * nb_last_lines); if (trace_on) { printf("%s block %d of size %u (%d line(s)).\n", dir, i, blocksize, nb_last_lines); } } return blocksize; } /* Compute the number of blocks of the transmitted data. */ static int get_nb_blocks(unsigned int count, char* dir) { unsigned int lines = 0; unsigned int blocks = 0; /* See section 21.4.3 of LPC11xx user's manual (UM10398) */ lines = (count / LINE_DATA_LENGTH); if (count % LINE_DATA_LENGTH) { lines += 1; } blocks = (lines / LINES_PER_BLOCK); if (lines % LINES_PER_BLOCK) { blocks += 1; } if (trace_on) { printf("%s %d block(s) (%d line(s)).\n", dir, blocks, lines); } return blocks; } static unsigned int calc_checksum(unsigned char* data, unsigned int size) { unsigned int i = 0; unsigned int checksum = 0; for (i=0; i (int)blocksize) ? 0 : 3)); if (len < 0) { /* Length may be null, as we may already have received everything */ printf("Error reading memory (checksum part).\n"); ret = -5; break; } /* Decode. This must be done before sending acknowledge because we must * compute the checksum */ decoded_size = isp_uu_decode((data + total_bytes_received), buf, blocksize); received_checksum = strtoul((buf + blocksize), NULL, 10); computed_checksum = calc_checksum((unsigned char*)(data + total_bytes_received), decoded_size); if (trace_on) { printf("Decoded Data :\n"); isp_dump((unsigned char*)(data + total_bytes_received), decoded_size); } if (computed_checksum == received_checksum) { resend_request_for_block = 0; /* reset resend request counter */ if (trace_on) { printf("Reading of blocks %u OK, contained %u bytes\n", i, decoded_size); } /* Acknowledge data */ if (isp_serial_write(DATA_BLOCK_OK, strlen(DATA_BLOCK_OK)) != strlen(DATA_BLOCK_OK)) { printf("Unable to send acknowledge.\n"); ret = -4; break; } /* Add data length to sum of received data */ total_bytes_received += decoded_size; } else { resend_request_for_block++; if (trace_on) { printf("Checksum error for block %u (received %u, computed %u) error number: %d.\n", i, received_checksum, computed_checksum, resend_request_for_block); } if (resend_request_for_block > 3) { printf("Block %d still wrong after 3 attempts, aborting.\n", i); ret = -2; break; } /* Back to previous block */ i--; /* Ask for resend */ if (isp_serial_write(DATA_BLOCK_RESEND, strlen(DATA_BLOCK_RESEND)) != strlen(DATA_BLOCK_RESEND)) { printf("Unable to send resend request.\n"); ret = -1; break; } } } return total_bytes_received; } /* * perform write-to-ram operation * send 'count' bytes from 'data' to 'addr' in RAM */ int isp_send_buf_to_ram(char* data, unsigned long int addr, unsigned int count, unsigned int perform_uuencode) { /* Serial communication */ int ret = 0, len = 0; /* Reply handling */ unsigned int blocks = 0; unsigned int total_bytes_sent = 0; unsigned int i = 0; /* Send write-to-ram request */ ret = isp_send_cmd_two_args("write-to-ram", 'W', addr, count); if (ret != 0) { printf("Error when trying to start write procedure to address 0x%08lx.\n", addr); return -8; } /* First check if we must UU-encode data */ if (perform_uuencode == 0) { if (isp_serial_write(data, count) != (int)count) { printf("Error sending raw binary data.\n"); return -7; } /* No checks required, we are done */ return 0; } /* Now, find the number of blocks of data to send. */ blocks = get_nb_blocks(count, "Sending"); /* Encode and send the data */ for (i=0; i= MAX_DATA_BLOCK_SIZE) { datasize = MAX_DATA_BLOCK_SIZE; } /* uuencode data */ encoded_size = isp_uu_encode(buf, data + total_bytes_sent, datasize); /* Add checksum */ computed_checksum = calc_checksum((unsigned char*)(data + total_bytes_sent), datasize); encoded_size += snprintf((buf + encoded_size), 12, "%u\r\n", computed_checksum); if (trace_on) { printf("Encoded Data :\n"); isp_dump((unsigned char*)buf, encoded_size); } if (isp_serial_write(buf, encoded_size) != (int)encoded_size) { printf("Error sending uuencoded data.\n"); ret = -6; break; } usleep( 20000 ); len = isp_serial_read(repbuf, REP_BUFSIZE, 4); if (len <= 0) { printf("Error reading write acknowledge.\n"); return -5; } if (strncmp(DATA_BLOCK_OK, repbuf, strlen(DATA_BLOCK_OK)) == 0) { total_bytes_sent += datasize; resend_requested_for_block = 0; /* reset resend request counter */ if (trace_on) { printf("Block %d sent.\n", i); } } else { resend_requested_for_block++; if (trace_on) { printf("Checksum error for block %u, error number: %d.\n", i, resend_requested_for_block); } if (resend_requested_for_block >= 3) { printf("Block %d still wrong after 3 attempts, aborting.\n", i); ret = -2; break; } /* Back to previous block */ i--; } } return ret; } int isp_send_cmd_address(char cmd, uint32_t addr1, uint32_t addr2, uint32_t length, char* name) { char buf[SERIAL_BUFSIZE]; int ret = 0, len = 0; char* tmp = NULL; /* Create request */ len = snprintf(buf, SERIAL_BUFSIZE, "%c %u %u %u\r\n", cmd, addr1, addr2, length); if (len > SERIAL_BUFSIZE) { len = SERIAL_BUFSIZE; } /* Send request */ if (isp_serial_write(buf, len) != len) { printf("Unable to send %s request.\n", name); return -5; } /* Wait for answer */ usleep( 5000 ); len = isp_serial_read(buf, 3, 3); if (len <= 0) { printf("Error reading %s result.\n", name); return -4; } ret = isp_ret_code(buf, &tmp, 0); return ret; } int isp_send_cmd_go(uint32_t addr, char mode) { char buf[SERIAL_BUFSIZE]; int ret = 0, len = 0; if (addr < 0x200) { printf("Error: address must be 0x00000200 or greater for go command.\n"); return -6; } if ((mode != 'T') && (mode != 'A')) { printf("Error: mode must be 'thumb' (T) or 'arm' (A) for go command.\n"); return -5; } /* Create go request */ len = snprintf(buf, SERIAL_BUFSIZE, "G %u %c\r\n", addr, mode); if (len > SERIAL_BUFSIZE) { len = SERIAL_BUFSIZE; } /* Send go request */ if (isp_serial_write(buf, len) != len) { printf("Unable to send go request.\n"); return -4; } /* Wait for answer */ usleep( 5000 ); len = isp_serial_read(buf, SERIAL_BUFSIZE, 3); if (len <= 0) { printf("Error reading go result.\n"); return -3; } ret = isp_ret_code(buf, NULL, 0); if (ret != 0) { printf("Error when trying to execute program at 0x%08x in '%c' mode.\n", addr, mode); return -1; } return 0; } int isp_send_cmd_sectors(char* name, char cmd, int first_sector, int last_sector, int quiet) { char buf[SERIAL_BUFSIZE]; int ret = 0, len = 0; if (last_sector < first_sector) { printf("Last sector must be after (or equal to) first sector for %s command.\n", name); return -6; } /* Create request */ len = snprintf(buf, SERIAL_BUFSIZE, "%c %u %u\r\n", cmd, first_sector, last_sector); if (len > SERIAL_BUFSIZE) { len = SERIAL_BUFSIZE; } /* Send request */ if (isp_serial_write(buf, len) != len) { printf("Unable to send %s request.\n", name); return -5; } /* Wait for answer */ usleep( 5000 ); len = isp_serial_read(buf, 3, 3); /* Read at exactly 3 bytes, so caller can retreive info */ if (len <= 0) { printf("Error reading %s result.\n", name); return -4; } ret = isp_ret_code(buf, NULL, quiet); return ret; } lpctools-v1.07/isp_commands.h000066400000000000000000000067031247765405200163350ustar00rootroot00000000000000/********************************************************************* * * LPC ISP Commands * * * Copyright (C) 2012 Nathael Pajani * * 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 3 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, see . * *********************************************************************/ #ifndef ISP_COMMANDS_H #define ISP_COMMANDS_H extern char* error_codes[]; #define CMD_SUCCESS 0 #define INVALID_COMMAND 1 #define SRC_ADDR_ERROR 2 #define DST_ADDR_ERROR 3 #define SRC_ADDR_NOT_MAPPED 4 #define DST_ADDR_NOT_MAPPED 5 #define COUNT_ERROR 6 #define INVALID_SECTOR 7 #define SECTOR_NOT_BLANK 8 #define SECTOR_NOT_PREPARED_FOR_WRITE_OPERATION 9 #define COMPARE_ERROR 10 #define BUSY 11 #define PARAM_ERROR 12 #define ADDR_ERROR 13 #define ADDR_NOT_MAPPED 14 #define CMD_LOCKED 15 #define INVALID_CODE 16 #define INVALID_BAUD_RATE 17 #define INVALID_STOP_BIT 18 #define CODE_READ_PROTECTION_ENABLED 19 /* Connect or reconnect to the target. * Return positive or NULL value when connection is OK, or negative value otherwise. */ int isp_connect(unsigned int crystal_freq, int quiet); /* * Helper functions */ int isp_send_cmd_no_args(char* cmd_name, char* cmd, int quiet); int isp_send_cmd_two_args(char* cmd_name, char cmd, unsigned int arg1, unsigned int arg2); int isp_send_cmd_address(char cmd, uint32_t addr1, uint32_t addr2, uint32_t length, char* name); int isp_send_cmd_sectors(char* name, char cmd, int first_sector, int last_sector, int quiet); int isp_cmd_unlock(int quiet); int isp_cmd_read_uid(void); int isp_cmd_part_id(int quiet); int isp_cmd_boot_version(void); /* * read-memory * aruments : address count file * read 'count' bytes from 'address', store then in 'file' */ int isp_cmd_read_memory(int arg_count, char** args); /* * perform read-memory operation * read 'count' bytes from 'addr' to 'data' buffer */ int isp_read_memory(char* data, uint32_t addr, unsigned int count, unsigned int uuencoded); /* * write-to-ram * aruments : address file * send 'file' to 'address' in ram */ int isp_cmd_write_to_ram(int arg_count, char** args); /* * perform write-to-ram operation * send 'count' bytes from 'data' to 'addr' in RAM */ int isp_send_buf_to_ram(char* data, unsigned long int addr, unsigned int count, unsigned int perform_uuencode); int isp_cmd_compare(int arg_count, char** args); int isp_cmd_copy_ram_to_flash(int arg_count, char** args); /* * go * aruments : address mode * execute program at 'address' (> 0x200) in 'mode' ('arm' or 'thumb') */ int isp_cmd_go(int arg_count, char** args); /* * perform go operation * start user program at 'addr' in 'mode' * mode is 'T' for thumb or 'A' for arm. */ int isp_send_cmd_go(uint32_t addr, char mode); int isp_cmd_blank_check(int arg_count, char** args); int isp_cmd_prepare_for_write(int arg_count, char** args); int isp_cmd_erase(int arg_count, char** args); #endif /* ISP_COMMANDS_H */ lpctools-v1.07/isp_utils.c000066400000000000000000000237111247765405200156650ustar00rootroot00000000000000/********************************************************************* * * LPC ISP - Utility functions * * * Copyright (C) 2012 Nathael Pajani * * 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 3 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, see . * *********************************************************************/ #include #include #include #include /* for open, close */ #include #include #include #include #include /* memcpy */ #include /* serial */ #include #define FILE_CREATE_MODE (S_IRUSR | S_IWUSR | S_IRGRP) extern int trace_on; /* display data as in hexdump -C : 00000000 7f 45 4c 46 02 01 01 00 00 00 00 00 00 00 00 00 |.ELF............| */ void isp_dump(const unsigned char* buf, unsigned int buf_size) { unsigned int count = 0; char pass = 0; if (buf_size == 0) { return; } while ((count < buf_size) || (pass == 0)) { if (count == buf_size) { while (count & 0x0F) { printf(" "); count++; } pass = 1; count -= 0x10; } if ((count % 0x10) == 0) { if (pass == 0) { printf(" %08x ", count); } else { printf(" |"); } } if (pass == 0) { printf("%02x ", buf[count]); } else { if (isgraph((int)buf[count])) { printf("%c", buf[count]); } else { printf("."); } } count++; if ((count % 0x10) == 0) { if (pass == 0) { count -= 0x10; } else { if (count == buf_size) { break; /* prevent infinite loop */ } printf("|\n"); } pass = !pass; } } printf("|\n"); } /* ---- Serial utility functions ---------------------------------------------------*/ static int serial_fd = -1; /* Open the serial device and set it up. * Returns 0 on success, negativ value on error. * Actal setup is done according to LPC11xx user's manual. * Only baudrate can be changed using command line option. */ int isp_serial_open(int baudrate, char* serial_device) { struct termios tio; if (serial_device == NULL) { printf("No serial device given on command line\n"); return -2; } /* Open serial port */ serial_fd = open(serial_device, O_RDWR | O_NONBLOCK); if (serial_fd < 0) { perror("Unable to open serial_device"); printf("Tried to open \"%s\".\n", serial_device); return -1; } /* Setup serial port */ memset(&tio, 0, sizeof(tio)); tio.c_iflag = IXON | IXOFF; /* See section 21.4.4 of LPC11xx user's manual (UM10398) */ tio.c_cflag = CS8 | CREAD | CLOCAL; /* 8n1, see termios.h for more information */ tio.c_cc[VMIN] = 1; tio.c_cc[VTIME] = 5; cfsetospeed(&tio, baudrate); cfsetispeed(&tio, baudrate); tcsetattr(serial_fd, TCSANOW, &tio); return 0; } void isp_serial_close(void) { close(serial_fd); } /* Simple write() wrapper, with trace if enabled */ int isp_serial_write(const char* buf, unsigned int buf_size) { int nb = 0; if (trace_on) { printf("Sending %d octet(s) :\n", buf_size); isp_dump((unsigned char*)buf, buf_size); } nb = write(serial_fd, buf, buf_size); if (nb <= 0) { perror("Serial write error"); return -1; } return nb; } static char next_read_char = 0; void isp_serial_empty_buffer() { int nb = 0; char unused = 0; unsigned int loops = 0; /* Used to create a timeout */ do { nb = read(serial_fd, &unused, 1); if (nb < 0) { if ((errno == EAGAIN) || (errno == EWOULDBLOCK)) { if (loops++ > 100) { break; /* timeout at 500ms */ } usleep( 5000 ); continue; } perror("Serial read error"); return; } else if (nb == 0) { printf("serial_read: end of file !!!!\n"); return; } } while ((unused != '\r') && (unused != '\n')); /* This should be improved by reading ALL \r and \n */ if (unused == '\r') { nb = read(serial_fd, &unused, 1); } if (unused == '\n') { return; } next_read_char = unused; } /* Try to read at least "min_read" characters from the serial line. * Returns -1 on error, 0 on end of file, or read count otherwise. */ int isp_serial_read(char* buf, unsigned int buf_size, unsigned int min_read) { int nb = 0; unsigned int count = 0; unsigned int loops = 0; /* Used to create a timeout */ if (min_read > buf_size) { printf("serial_read: buffer too small for min read value.\n"); return -3; } if (next_read_char != 0) { buf[count++] = next_read_char; next_read_char = 0; } do { nb = read(serial_fd, &buf[count], (buf_size - count)); if (nb < 0) { if ((errno == EAGAIN) || (errno == EWOULDBLOCK)) { if (loops++ > 100) { break; /* timeout at 500ms */ } usleep( 5000 ); continue; } perror("Serial read error"); return -1; } else if (nb == 0) { printf("serial_read: end of file !!!!\n"); return 0; } if (trace_on == 2) { isp_dump((unsigned char*)(&buf[count]), nb); } count += nb; } while (count < min_read); if (trace_on) { printf("Received %d octet(s) :\n", count); isp_dump((unsigned char*)buf, count); } return count; } /* ---- UU_Encoding utility functions ----------------------------------------------*/ /* This might have been taken from a lib, but I hate lib dependencies, and installing * a full window manager (or MTA or MUA for instance) for two functions is not an * option */ #define UUENCODE_ADDED_VAL 32 #define LINE_DATA_LENGTH_MAX 45 #define LINE_LENGTH_NULL 96 int isp_uu_encode(char* dest, char* src, unsigned int orig_size) { unsigned int new_size = 0; unsigned int pos = 0; while (pos < orig_size) { unsigned int line_length = orig_size - pos; unsigned int i = 0; /* Start with line length */ if (line_length > LINE_DATA_LENGTH_MAX) { line_length = LINE_DATA_LENGTH_MAX; } dest[new_size] = line_length + UUENCODE_ADDED_VAL; new_size++; /* Encode line */ while (i < line_length) { uint32_t int_triplet = 0; int j = 0; /* Get original triplet (three bytes) */ for (j=0; j<3; j++) { int_triplet |= ((src[pos + i + j] & 0xFF) << (8 * (2 - j))); /* if not enougth data, leave it as nul */ if ((i + j) > line_length) { break; } } for (j=0; j<4; j++) { /* Store triplet in four bytes */ dest[new_size + j] = ((int_triplet >> (6 * (3 - j))) & 0x3F); /* Add offset */ dest[new_size + j] += UUENCODE_ADDED_VAL; } i += 3; new_size += 4; } pos += line_length; /* Add \r\n */ dest[new_size++] = '\r'; dest[new_size++] = '\n'; } return new_size; } int isp_uu_decode(char* dest, char* src, unsigned int orig_size) { unsigned int new_size = 0; unsigned int pos = 0; while (pos < orig_size) { unsigned int line_length = 0; unsigned int i = 0; int j = 0; /* Read line length */ line_length = src[pos] - UUENCODE_ADDED_VAL; if (src[pos] == LINE_LENGTH_NULL) { /* Empty line ? then we are done converting * (should not happen in communication with ISP) */ return new_size; } pos++; /* Decode line */ while (i < line_length) { char quartet[4]; uint32_t int_triplet = 0; /* copy data */ memcpy(quartet, &src[pos], 4); pos += 4; /* Get the original bits */ for (j=0; j<4; j++) { /* Remove the offset added by uuencoding */ quartet[j] -= UUENCODE_ADDED_VAL; int_triplet |= ((quartet[j] & 0x3F) << ((3 - j) * 6)); } /* And store them */ for (j=2; j>=0; j--) { dest[new_size++] = ((int_triplet >> (j * 8)) & 0xFF ); i++; if (i >= line_length) { break; } } } /* Find next line */ while ((src[pos] < UUENCODE_ADDED_VAL) && (pos < orig_size)) { pos++; } } return new_size; } /* ---- File utility functions ----------------------------------------------*/ int isp_buff_to_file(char* data, unsigned int len, char* filename) { int ret = 0; int out_fd = -1; /* Open file for writing if output is not stdout */ if (strncmp(filename, "-", strlen(filename)) == 0) { out_fd = STDOUT_FILENO; } else { out_fd = open(filename, (O_WRONLY | O_CREAT | O_TRUNC), FILE_CREATE_MODE); if (out_fd <= 0) { perror("Unable to open or create file for writing"); printf("Tried to open \"%s\".\n", filename); return -1; } } /* Write data to file */ ret = write(out_fd, data, len); if (ret != (int)len) { perror("File write error"); printf("Unable to write %u bytes of data to file \"%s\", returned %d !", len, filename, ret); ret = -2; } /* Close file */ if (out_fd != STDOUT_FILENO) { close(out_fd); } return ret; } int isp_file_to_buff(char* data, unsigned int len, char* filename) { int in_fd = -1; unsigned int bytes_read = 0; /* Open file for reading if input is not stdin */ if (strncmp(filename, "-", strlen(filename)) == 0) { /* FIXME : set as non blocking ? */ in_fd = STDIN_FILENO; } else { in_fd = open(filename, O_RDONLY | O_NONBLOCK); if (in_fd <= 0) { perror("Unable to open file for reading"); printf("Tried to open \"%s\".\n", filename); return -13; } } /* Read file content */ do { int nb = read(in_fd, &data[bytes_read], (len - bytes_read)); if (nb < 0) { if ((errno == EAGAIN) || (errno == EWOULDBLOCK)) { usleep( 50 ); continue; } perror("Input file read error"); if (in_fd != STDIN_FILENO) { close(in_fd); } return -12; } else if (nb == 0) { /* End of file */ break; } bytes_read += nb; } while (bytes_read < len); if (in_fd != STDIN_FILENO) { close(in_fd); } return bytes_read; } lpctools-v1.07/isp_utils.h000066400000000000000000000046621247765405200156760ustar00rootroot00000000000000/********************************************************************* * * LPC ISP - Utility functions * * * Copyright (C) 2012 Nathael Pajani * * 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 3 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, see . * *********************************************************************/ #ifndef ISP_UTILS_H #define ISP_UTILS_H /* ---- CRP Protection values ---------------------------------------------------*/ #define CRP_OFFSET 0x000002FC #define CRP_NO_ISP 0x4E697370 #define CRP_CRP1 0x12345678 #define CRP_CRP2 0x87654321 #define CRP_CRP3 0x43218765 void isp_dump(const unsigned char* buf, unsigned int buf_size); /* ---- Serial utility functions ---------------------------------------------------*/ /* Open the serial device and set it up. * Returns 0 on success, negativ value on error. * Actal setup is done according to LPC11xx user's manual. * Only baudrate can be changed using command line option. */ int isp_serial_open(int baudrate, char* serial_device); void isp_serial_close(void); /* Simple write() wrapper, with trace if enabled */ int isp_serial_write(const char* buf, unsigned int buf_size); void isp_serial_empty_buffer(); /* Try to read at least "min_read" characters from the serial line. * Returns -1 on error, 0 on end of file, or read count otherwise. */ int isp_serial_read(char* buf, unsigned int buf_size, unsigned int min_read); /* ---- UU_Encoding utility functions ----------------------------------------------*/ int isp_uu_encode(char* dest, char* src, unsigned int orig_size); int isp_uu_decode(char* dest, char* src, unsigned int orig_size); /* ---- File utility functions ----------------------------------------------*/ int isp_buff_to_file(char* data, unsigned int len, char* filename); int isp_file_to_buff(char* data, unsigned int len, char* filename); #endif /* ISP_UTILS_H */ lpctools-v1.07/isp_wrapper.c000066400000000000000000000223551247765405200162100ustar00rootroot00000000000000/********************************************************************* * * LPC ISP Commands * * * Copyright (C) 2012 Nathael Pajani * * 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 3 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, see . * *********************************************************************/ #include /* strtoul */ #include #include #include #include /* strncmp */ #include #include #include /* for open, close */ #include #include #include #include "isp_commands.h" #include "isp_utils.h" extern int trace_on; #define RAM_MAX_SIZE (8 * 1024) /* 8 KB */ #define REP_BUFSIZE 40 /* * read-memory * aruments : address count file uuencoded * read 'count' bytes from 'address', store then in 'file', and maybe decode uuencoded data. */ int isp_cmd_read_memory(int arg_count, char** args) { /* Arguments */ unsigned long int addr = 0, count = 0; char* out_file_name = NULL; /* Reply handling */ char* data; int ret = 0, len = 0; unsigned int uuencoded = 1; /* Check read-memory arguments */ if (arg_count < 3) { printf("read-memory command needs address and count. Both must be multiple of 4.\n"); return -12; } addr = strtoul(args[0], NULL, 0); count = strtoul(args[1], NULL, 0); if (trace_on) { printf("read-memory command called for 0x%08lx (%lu) bytes at address 0x%08lx.\n", count, count, addr); } if ((addr & 0x03) || (count & 0x03)) { printf("Address and count must be multiple of 4 for read-memory command.\n"); return -11; } out_file_name = args[2]; if (arg_count > 3) { uuencoded = strtoul(args[3], NULL, 0); } /* Allocate buffer */ data = malloc(count); if (data == NULL) { printf("Unable to allocate read buffer, asked %lu.\n", count); return -10; } /* Read data */ len = isp_read_memory(data, addr, count, uuencoded); if (len != (int)count) { printf("Read returned %d bytes instead of %lu.\n", len, count); } /* Write data to file */ ret = isp_buff_to_file(data, len, out_file_name); /* Free memory */ free(data); return ret; } /* * write-to-ram * aruments : address file do_not_perform_uuencode * send 'file' to 'address' in ram with or without uuencoding data */ int isp_cmd_write_to_ram(int arg_count, char** args) { /* Arguments */ unsigned long int addr = 0; char* in_file_name = NULL; char file_buff[RAM_MAX_SIZE]; unsigned int bytes_read = 0; int ret = 0; int uuencode = 1; /* Check write-to-ram arguments */ if (arg_count < 2) { printf("write-to-ram command needs ram address. Must be multiple of 4.\n"); return -15; } addr = strtoul(args[0], NULL, 0); if (trace_on) { printf("write-to-ram command called, destination address in ram is 0x%08lx.\n", addr); } if (addr & 0x03) { printf("Address must be multiple of 4 for write-to-ram command.\n"); return -14; } in_file_name = args[1]; if (arg_count > 2) { uuencode = strtoul(args[2], NULL, 0); } /* Read data */ bytes_read = isp_file_to_buff(file_buff, RAM_MAX_SIZE, in_file_name); if (bytes_read <= 0){ return -13; } if (trace_on) { printf("Read %d octet(s) from input file\n", bytes_read); } /* Pad data size to 4 */ if (bytes_read & 0x03) { bytes_read = (bytes_read & ~0x03) + 0x04; } /* And send to ram */ ret = isp_send_buf_to_ram(file_buff, addr, bytes_read, uuencode); return ret; } int isp_cmd_address_wrapper(int arg_count, char** args, char* name, char cmd) { int ret = 0, len = 0; /* Arguments */ unsigned long int addr1 = 0, addr2 = 0; unsigned long int length = 0; /* Check arguments */ if (arg_count != 3) { printf("%s command needs two addresses and byte count.\n", name); return -7; } addr1 = strtoul(args[0], NULL, 0); addr2 = strtoul(args[1], NULL, 0); length = strtoul(args[2], NULL, 0); if (trace_on) { printf("%s command called for %lu bytes between addresses %lu and %lu.\n", name, length, addr1, addr2); } switch (cmd) { case 'M': if ((addr1 & 0x03) || (addr2 & 0x03) || (length & 0x03)) { printf("Error: addresses and count must be multiples of 4 for %s command.\n", name); return -7; } break; case 'C': if ((addr1 & 0x00FF) || (addr2 & 0x03)) { printf("Error: flash address must be on 256 bytes boundary, ram address on 4 bytes boundary.\n"); return -8; } /* According to section 21.5.7 of LPC11xx user's manual (UM10398), number of bytes * written should be 256 | 512 | 1024 | 4096 */ len = ((length >> 8) & 0x01) + ((length >> 9) & 0x01); len += ((length >> 10) & 0x01) + ((length >> 12) & 0x01); if (len != 1) { printf("Error: bytes count must be one of 256 | 512 | 1024 | 4096\n"); return -7; } break; default: printf("Error: unsupported command code: '%c'\n", cmd); return -6; } ret = isp_send_cmd_address(cmd, addr1, addr2, length, name); return ret; } int isp_cmd_compare(int arg_count, char** args) { char buf[REP_BUFSIZE]; int ret = 0, len = 0; unsigned long int offset = 0; ret = isp_cmd_address_wrapper(arg_count, args, "compare", 'M'); switch (ret) { case CMD_SUCCESS: printf("Source and destination data are equal.\n"); break; case COMPARE_ERROR: /* read remaining data */ usleep( 2000 ); len = isp_serial_read(buf, REP_BUFSIZE, 3); if (len <= 0) { printf("Error reading blank-check result.\n"); return -3; } offset = strtoul(buf, NULL, 10); printf("First mismatch occured at offset 0x%08lx\n", offset); break; default : printf("Error for compare command.\n"); return -1; } return 0; } int isp_cmd_copy_ram_to_flash(int arg_count, char** args) { int ret = 0; ret = isp_cmd_address_wrapper(arg_count, args, "copy-ram-to-flash", 'C'); if (ret != 0) { printf("Error when trying to copy data from ram to flash.\n"); return ret; } printf("Data copied from ram to flash.\n"); return 0; } int isp_cmd_go(int arg_count, char** args) { int ret = 0; /* Arguments */ unsigned long int addr = 0; char* mode = NULL; /* Check go arguments */ if (arg_count != 2) { printf("go command needs address (> 0x200) and mode ('thumb' or 'arm').\n"); return -7; } addr = strtoul(args[0], NULL, 0); mode = args[1]; if (trace_on) { printf("go command called with address 0x%08lx and mode %s.\n", addr, mode); } if (addr < 0x200) { printf("Error: address must be 0x00000200 or greater for go command.\n"); return -6; } if (strncmp(mode, "thumb", strlen(mode)) == 0) { mode[0] = 'T'; } else if (strncmp(mode, "arm", strlen(mode)) == 0) { mode[0] = 'A'; } else { printf("Error: mode must be 'thumb' or 'arm' for go command.\n"); return -5; } ret = isp_send_cmd_go(addr, mode[0]); return ret; } int isp_cmd_sectors_skel(int arg_count, char** args, char* name, char cmd) { int ret = 0; /* Arguments */ unsigned long int first_sector = 0, last_sector = 0; /* Check arguments */ if (arg_count != 2) { printf("%s command needs first and last sectors (1 sector = 4KB of flash).\n", name); return -7; } first_sector = strtoul(args[0], NULL, 0); last_sector = strtoul(args[1], NULL, 0); if (trace_on) { printf("%s command called for sectors %lu to %lu.\n", name, first_sector, last_sector); } ret = isp_send_cmd_sectors(name, cmd, first_sector, last_sector, 0); return ret; } int isp_cmd_blank_check(int arg_count, char** args) { char* tmp = NULL; unsigned long int offset = 0, content = 0; char buf[REP_BUFSIZE]; int ret = 0, len = 0; ret = isp_cmd_sectors_skel(arg_count, args, "blank-check", 'I'); if (ret < 0) { return ret; } switch (ret) { case CMD_SUCCESS: printf("Specified sector(s) all blank(s).\n"); break; case SECTOR_NOT_BLANK: /* read remaining data */ usleep( 2000 ); len = isp_serial_read(buf, REP_BUFSIZE, 3); if (len <= 0) { printf("Error reading blank-check result.\n"); return -3; } offset = strtoul(buf, &tmp, 10); content = strtoul(tmp, NULL, 10); printf("First non blank word is at offset 0x%08lx and contains 0x%08lx\n", offset, content); break; case INVALID_SECTOR : printf("Invalid sector for blank-check command.\n"); return -2; case PARAM_ERROR: printf("Param error for blank-check command.\n"); return -1; } return 0; } int isp_cmd_prepare_for_write(int arg_count, char** args) { int ret = isp_cmd_sectors_skel(arg_count, args, "prepare-for-write", 'P'); if (ret != 0) { printf("Error when trying to prepare sectors for write operation.\n"); return ret; } printf("Sectors prepared for write operation.\n"); return 0; } int isp_cmd_erase(int arg_count, char** args) { int ret = isp_cmd_sectors_skel(arg_count, args, "erase", 'E'); if (ret != 0) { printf("Error when trying to erase sectors.\n"); return ret; } printf("Sectors erased.\n"); return 0; } lpctools-v1.07/lpcisp.c000066400000000000000000000230601247765405200151410ustar00rootroot00000000000000/********************************************************************* * * ISP (In-System Programming) tool for NXP LPC Microcontrollers * * This tool gives access to each of the useful isp commands on LPC * devices. It does not provide wrappers for flashing a device. * * Copyright (C) 2012 Nathael Pajani * * 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 3 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, see . * *********************************************************************/ #include /* malloc, free */ #include #include #include /* open, getopt, close, usleep */ #include #include #include #include #include #include /* for serial config */ #include #include /* strncmp, strlen */ #include "isp_utils.h" #include "isp_commands.h" #define PROG_NAME "LPC ISP" #define VERSION "1.07" /* short explanation on exit values: * * 0 is OK * 1 is arg error * */ void help(char *prog_name) { fprintf(stderr, "-----------------------------------------------------------------------\n"); fprintf(stderr, "Usage: %s [options] device [-s | --synchronize]\n" \ " %s [options] device command [command arguments]\n" \ " Default baudrate is B115200\n" \ " is the (host) serial line used to program the device\n" \ " is one of:\n" \ " \t unlock, write-to-ram, read-memory, prepare-for-write, copy-ram-to-flash, go, erase,\n" \ " \t blank-check, read-part-id, read-boot-version, compare and read-uid.\n" \ " command specific arguments are as follow:\n" \ " \t unlock \n" \ " \t write-to-ram address file uuencode : send 'file' to 'address' in ram with or without uuencoding\n" \ " \t read-memory address count file uuencoded : read 'count' bytes from 'address', store then in 'file' (maybe decode)\n" \ " \t prepare-for-write first last : prepare sectors from 'first' to 'last' for write operation\n" \ " \t copy-ram-to-flash flash_addr ram_addr count : copy count bytes (256, 512, 1024 or 4096)\n" \ " \t from 'ram_addr' to 'flash_addr'\n" \ " \t go address mode : execute program at 'address' (> 0x200) in 'mode' ('arm' or 'thumb')\n" \ " \t erase first last : erase flash starting from 'first' sector up to (including) 'last' sector \n" \ " \t blank-check first last : check flash starting from 'first' sector to 'last' sector is blank\n" \ " \t read-part-id \n" \ " \t read-boot-version \n" \ " \t compare address1 address2 count : compare count bytes between address1 and address2\n" \ " \t read-uid \n" \ " Notes:\n" \ " - Access to the ISP mode is done by calling this utility once with the synchronize\n" \ " option and no command. This starts a session. No command can be used before starting\n" \ " a session, and no other synchronize request must be done once the session is started\n" \ " unless the target is reseted, which closes the session.\n" \ " - Echo is turned OFF when starting a session and the command is not available.\n" \ " - The set-baud-rate command is not available. The SAME baudrate MUST be used for the\n" \ " whole session. It must be specified on each successive call if the default baudrate\n" \ " is not used.\n" \ " - User must issue an 'unlock' command before any of 'copy-ram-to-flash', 'erase',\n" \ " and 'go' commands.\n" \ " Available options:\n" \ " \t -s | --synchronize : Perform synchronization (open session)\n" \ " \t -b | --baudrate=N : Use this baudrate (does not issue the set-baud-rate command)\n" \ " \t -t | --trace : turn on trace output of serial communication\n" \ " \t -h | --help : display this help\n" \ " \t -v | --version : display version information\n", prog_name, prog_name); fprintf(stderr, "-----------------------------------------------------------------------\n"); } #define SERIAL_BAUD B115200 int trace_on = 0; int isp_handle_command(char* cmd, int arg_count, char** args); int main(int argc, char** argv) { int baudrate = SERIAL_BAUD; int crystal_freq = 10000; int synchronize = 0; char* isp_serial_device = NULL; /* For "command" handling */ char* command = NULL; char** cmd_args = NULL; int nb_cmd_args = 0; /* parameter parsing */ while(1) { int option_index = 0; int c = 0; struct option long_options[] = { {"synchronize", no_argument, 0, 's'}, {"baudrate", required_argument, 0, 'b'}, {"trace", no_argument, 0, 't'}, {"help", no_argument, 0, 'h'}, {"version", no_argument, 0, 'v'}, {0, 0, 0, 0} }; c = getopt_long(argc, argv, "sb:thv", long_options, &option_index); /* no more options to parse */ if (c == -1) break; switch (c) { /* b, baudrate */ case 'b': baudrate = atoi(optarg); /* FIXME: validate baudrate */ break; /* t, trace */ case 't': trace_on = 1; break; /* s, synchronize */ case 's': synchronize = 1; break; /* v, version */ case 'v': printf("%s Version %s\n", PROG_NAME, VERSION); return 0; break; /* h, help */ case 'h': default: help(argv[0]); return 0; } } /* Parse remaining command line arguments (not options). */ /* First one should (must) be serial device */ if (optind < argc) { isp_serial_device = argv[optind++]; if (trace_on) { printf("Serial device : %s\n", isp_serial_device); } } if (isp_serial_device == NULL) { printf("No serial device given, exiting\n"); help(argv[0]); return 0; } if (isp_serial_open(baudrate, isp_serial_device) != 0) { printf("Serial open failed, unable to initiate serial communication with target.\n"); return -1; } if (synchronize) { if (optind < argc) { /* no command can be specified when opening a session */ printf("No command can be specified with -s or --synchronize (Session opening)\n"); printf("NOT SYNCHRONIZED !\n"); return -1; } isp_connect(crystal_freq, 0); isp_serial_close(); return 0; } /* Next one should be "command" (if present) */ if (optind < argc) { command = argv[optind++]; if (trace_on) { printf("Command : %s\n", command); } } else { printf("No command given. use -h or --help for help on available commands.\n"); isp_serial_close(); return -1; } /* And then remaining ones (if any) are command arguments */ if (optind < argc) { nb_cmd_args = argc - optind; cmd_args = malloc(nb_cmd_args * sizeof(char *)); if (trace_on) { printf("Command arguments :\n"); } while (optind < argc) { static unsigned int idx = 0; cmd_args[idx++] = argv[optind++]; if (trace_on) { printf("%s\n", cmd_args[idx - 1]); } } } if (command != NULL) { int err = 0; err = isp_handle_command(command, nb_cmd_args, cmd_args); if (err >= 0) { if (trace_on) { printf("Command \"%s\" handled OK.\n", command); } } else { printf("Error handling command \"%s\" : %d\n", command, err); } } if (cmd_args != NULL) { free(cmd_args); } isp_serial_close(); return 0; } struct isp_command { int cmd_num; char* name; int nb_args; int (*handler)(int arg_count, char** args); }; static struct isp_command isp_cmds_list[] = { {0, "unlock", 0, NULL}, {1, "write-to-ram", 3, isp_cmd_write_to_ram}, {2, "read-memory", 3, isp_cmd_read_memory}, {3, "prepare-for-write", 2, isp_cmd_prepare_for_write}, {4, "copy-ram-to-flash", 3, isp_cmd_copy_ram_to_flash}, {5, "go", 2, isp_cmd_go}, {6, "erase", 2, isp_cmd_erase}, {7, "blank-check", 2, isp_cmd_blank_check}, {8, "read-part-id", 0, NULL}, {9, "read-boot-version", 0, NULL}, {10, "compare", 3, isp_cmd_compare}, {11, "read-uid", 0, NULL}, {-1, NULL, 0, NULL} }; void isp_warn_args(int cmd_num, int arg_count, char** args) { int i = 0; printf("command \"%s\" needs %d args, got %d.\n", isp_cmds_list[cmd_num].name, isp_cmds_list[cmd_num].nb_args, arg_count); for (i=0; i .br Software Git Repository: .SH COPYRIGHT Copyright \(co 2013 Techno-Innov License GPLv3+: GNU GPL version 3 or later . .br This is free software: you are free to change and redistribute it. There is NO WARRANTY, to the extent permitted by law. .SH "SEE ALSO" lpcisp(1) lpctools-v1.07/lpcprog.c000066400000000000000000000224541247765405200153230ustar00rootroot00000000000000/********************************************************************* * * ISP (In-System Programming) tool for NXP LPC Microcontrollers * * This tool does not give access to each isp command, instead it * provides wrappers for flashing a device. * * * Copyright (C) 2012 Nathael Pajani * * 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 3 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, see . * *********************************************************************/ #include /* malloc, free */ #include #include #include /* open, getopt, close, usleep */ #include #include #include #include #include #include /* for serial config */ #include #include /* strncmp, strlen, strdup */ #include "isp_utils.h" #include "isp_commands.h" #include "prog_commands.h" #include "parts.h" #define PROG_NAME "LPC ISP Prog tool" #define VERSION "1.07" void help(char *prog_name) { fprintf(stderr, "---------------- "PROG_NAME" --------------------------------\n"); fprintf(stderr, "Usage: %s -d -c [options] [dump/prog file name]\n" \ " Default parts description files are /etc/lpctools_parts.def or ./lpctools_parts.def\n" \ " Default baudrate is B115200\n" \ " Default oscilator frequency used is 10000 KHz\n" \ " is one of:\n" \ " \t dump, flash, id, blank, go\n" \ " \t dump file_name: dump flash content to 'file'\n" \ " \t flash file_name : put 'file' to flash, erasing requiered sectors\n" \ " \t blank : erase whole flash\n" \ " \t id : get all id information\n" \ " \t go : execute program from reset handler in thumb mode and open terminal\n" \ " Available options:\n" \ " \t -p | --parts=file : Parts description file (see defaults)\n" \ " \t -c | --command=cmd : \n" \ " \t -d | --device=dev_path : Host serial line used to program the device\n" \ " \t -b | --baudrate=N : Use this baudrate (Same baudrate must be used across whole session)\n" \ " \t -t | --trace : turn on trace output of serial communication\n" \ " \t -f | --freq=N : Oscilator frequency of target device\n" \ " \t -n | --no-user-code : do not compute a valid user code for exception vector 7\n" \ " \t -h | --help : display this help\n" \ " \t -v | --version : display version information\n", prog_name); fprintf(stderr, "-----------------------------------------------------------------------\n"); } #define SERIAL_BAUD B115200 int trace_on = 0; int quiet = 0; static int calc_user_code = 1; /* User code is computed by default */ char* parts_file_name = NULL; #define DEFAULT_PART_FILE_NAME_ETC "/etc/lpctools_parts.def" #define DEFAULT_PART_FILE_NAME_CURRENT "./lpctools_parts.def" static int prog_connect_and_id(int freq); static int prog_handle_command(char* cmd, int dev_id, int arg_count, char** args); int main(int argc, char** argv) { int baudrate = SERIAL_BAUD; int crystal_freq = 10000; char* isp_serial_device = NULL; int dev_id = 0; /* For "command" handling */ char* command = NULL; char** cmd_args = NULL; int nb_cmd_args = 0; /* parameter parsing */ while(1) { int option_index = 0; int c = 0; struct option long_options[] = { {"parts", required_argument, 0, 'p'}, {"command", required_argument, 0, 'c'}, {"device", required_argument, 0, 'd'}, {"baudrate", required_argument, 0, 'b'}, {"trace", no_argument, 0, 't'}, {"freq", required_argument, 0, 'f'}, {"no-user-code", no_argument, 0, 'n'}, {"help", no_argument, 0, 'h'}, {"version", no_argument, 0, 'v'}, {0, 0, 0, 0} }; c = getopt_long(argc, argv, "p:c:d:b:tf:nhv", long_options, &option_index); /* no more options to parse */ if (c == -1) break; switch (c) { /* p, parts description file */ case 'p': parts_file_name = strdup(optarg); break; /* c, command */ case 'c': command = strdup(optarg); break; /* d, device */ case 'd': isp_serial_device = strdup(optarg); break; /* b, baudrate */ case 'b': baudrate = atoi(optarg); /* FIXME: validate baudrate */ break; /* t, trace */ case 't': trace_on = 1; break; /* f, freq */ case 'f': crystal_freq = atoi(optarg); break; /* n, user-code */ case 'n': calc_user_code = 0; break; /* v, version */ case 'v': printf("%s Version %s\n", PROG_NAME, VERSION); return 0; break; /* h, help */ case 'h': default: help(argv[0]); return 0; } } /* Command is mandatory */ if (command == NULL) { printf("No command given. use -h or --help for help on available commands.\n"); return -1; } /* Check for default parts file availability if none given as argument */ if (parts_file_name == NULL) { FILE* parts_file = NULL; parts_file_name = DEFAULT_PART_FILE_NAME_CURRENT; parts_file = fopen(parts_file_name, "r"); if (parts_file == NULL) { parts_file_name = DEFAULT_PART_FILE_NAME_ETC; parts_file = fopen(parts_file_name, "r"); } if (parts_file == NULL) { printf("Unable to open default parts file to read LPC descriptions !\n"); printf("Tried : '%s' and '%s'\n", DEFAULT_PART_FILE_NAME_ETC, DEFAULT_PART_FILE_NAME_CURRENT); return -1; } fclose(parts_file); } /* Open serial device */ if (isp_serial_device == NULL) { printf("No serial device given, exiting\n"); help(argv[0]); return 0; } else if (isp_serial_open(baudrate, isp_serial_device) != 0) { printf("Serial open failed, unable to initiate serial communication with target.\n"); return -1; } if (trace_on) { printf("Serial device : %s\n", isp_serial_device); printf("Command : %s\n", command); } /* Parse remaining command line arguments (not options). They are command arguments */ if (optind < argc) { nb_cmd_args = argc - optind; cmd_args = malloc(nb_cmd_args * sizeof(char *)); if (trace_on) { printf("Command arguments :\n"); } while (optind < argc) { static unsigned int idx = 0; cmd_args[idx++] = argv[optind++]; if (trace_on) { printf("%s\n", cmd_args[idx - 1]); } } } /* First : sync with device */ dev_id = prog_connect_and_id(crystal_freq); if (dev_id < 0) { printf("Unable to connect to target, consider hard reset of target or link\n"); return -1; } if (command != NULL) { int err = 0; err = prog_handle_command(command, dev_id, nb_cmd_args, cmd_args); if (err >= 0) { if (trace_on) { printf("Command \"%s\" handled OK.\n", command); } } else { printf("Error handling command \"%s\" : %d\n", command, err); } } if (cmd_args != NULL) { free(cmd_args); } isp_serial_close(); return 0; } struct prog_command { int cmd_num; char* name; }; static struct prog_command prog_cmds_list[] = { {0, "dump"}, {1, "flash"}, {2, "id"}, {3, "blank"}, {4, "go"}, {5, NULL} }; /* * Try to connect to the target and identify the device. * First try sync, and if it fails, try to read id twice. if both fail, then we are not connected */ static int prog_connect_and_id(int freq) { int sync_ret = 0; /* Try to connect */ sync_ret = isp_connect(freq, 1); /* Synchro failed or already synchronised ? */ if (sync_ret < 0) { /* If already synchronized, then sync command and the next command fail. Sync failed, maybe we are already sync'ed, then send one command for nothing */ isp_cmd_part_id(1); } return isp_cmd_part_id(1); } static int prog_handle_command(char* cmd, int dev_id, int arg_count, char** args) { int cmd_found = -1; int ret = 0; int index = 0; struct part_desc* part = NULL; if (cmd == NULL) { printf("prog_handle_command called with no command !\n"); return -1; } part = find_part_in_file(dev_id, parts_file_name); if (part == NULL) { printf("Unknown part number : 0x%08x.\n", dev_id); return -2; } while ((cmd_found == -1) && (prog_cmds_list[index].name != NULL)) { if (strncmp(prog_cmds_list[index].name, cmd, strlen(prog_cmds_list[index].name)) == 0) { cmd_found = index; break; } index++; } if (cmd_found == -1) { printf("Unknown command \"%s\", use -h or --help for a list.\n", cmd); return -3; } switch (prog_cmds_list[cmd_found].cmd_num) { case 0: /* dump, need one arg : filename */ if (arg_count != 1) { printf("command dump needs one arg (filename), got %d.\n", arg_count); return -4; } ret = dump_to_file(part, args[0]); break; case 1: /* flash, need one arg : filename */ if (arg_count != 1) { printf("command flash needs one arg (filename), got %d.\n", arg_count); return -4; } ret = flash_target(part, args[0], calc_user_code); break; case 2: /* id : no args */ ret = get_ids(); break; case 3: /* blank : no args */ ret = erase_flash(part); break; case 4: /* go : no args */ ret = start_prog(part); break; } return ret; } lpctools-v1.07/lpctools_parts.def000066400000000000000000000027241247765405200172370ustar00rootroot00000000000000# Parts definition file # # Note that the version of this file found in /etc should not be modified to ease program update. # Instead place a copy in your project directory or specify an alternative using the appropriate option. # # Lines starting whith # are comments # All values but the last one MUST be immediately followed by a coma (',') and can be precedeed by # any number of whitespaces. # "part name" must be under 25 characters. # Line format : # part info | flash | reset | ram | ram | UU # | nb | vector | | buffer | encode # part_id part name | base addr size sect | offset | base addr size | off size | ? # LPC81X Familly 0x00008100, LPC810M021FN8, 0x00000000, 0x1000, 4, 0x04, 0x10000000, 0x0400, 0x300, 0x100, 0 0x00008122, LPC812M101JDH20, 0x00000000, 0x4000, 16, 0x04, 0x10000000, 0x1000, 0x800, 0x400, 0 # LPC11XX Familly 0x2540102B, LPC1114FHN33/302, 0x00000000, 0x8000, 8, 0x04, 0x10000000, 0x2000, 0x800, 0x400, 1 0x4d80002b, LPC11A04UK, 0x00000000, 0x8000, 8, 0x04, 0x10000000, 0x2000, 0x800, 0x400, 1 # LPC12XX Familly 0x3640C02B, LPC1224FBD48/101, 0x00000000, 0x8000, 8, 0x04, 0x10000000, 0x1000, 0x800, 0x400, 1 # LPC17XX Familly 0x26011922, LPC1764FBD100, 0x00000000, 0x10000, 16, 0x04, 0x10000000, 0x4000, 0x800, 0x800, 1 lpctools-v1.07/parts.c000066400000000000000000000072121247765405200150010ustar00rootroot00000000000000/********************************************************************* * * LPC Parts identification * * * Copyright (C) 2012 Nathael Pajani * * 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 3 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, see . * *********************************************************************/ #include /* uint64_t and uint32_t */ #include /* NULL */ #include /* For offsetof */ #include /* perror(), fopen(), fclose() */ #include #include "parts.h" #define CONF_READ_BUF_SIZE 250 /* When looking for parts description in a file ee do allocate (malloc) two memory * chunks which we will never free. * The user should free part_desc->name and part_desc when they are no more useful */ struct part_desc* find_part_in_file(uint64_t dev_id, char* parts_file_name) { FILE* parts_file = NULL; unsigned int i = 0; unsigned int line = 0; /* Store current line number when reading file */ struct part_desc* part = malloc(sizeof(struct part_desc)); parts_file = fopen(parts_file_name, "r"); if (parts_file == NULL) { perror("Unable to open file to read LPC descriptions !"); return NULL; } while (1) { char buf[CONF_READ_BUF_SIZE]; char* endp = NULL; uint32_t* part_values = NULL; unsigned int nval = 0; if (fgets(buf, CONF_READ_BUF_SIZE, parts_file) == NULL) { printf("Part not found before end of parts description file.\n"); goto out_err_1; } line++; /* Store current line number to help parts description file correction */ if (buf[0] == '#') { /* skip comments */ continue; } part->part_id = strtoul(buf, &endp, 0); if (part->part_id != dev_id) { continue; } printf("Part ID 0x%08x found on line %d\n", (unsigned int)part->part_id, line); /* We found the right part ID, get all the data */ part->name = malloc(PART_NAME_LENGTH); /* Part names must start with "LPC" */ while ((*endp != '\0') && (*endp != 'L')) { endp++; } /* Copy part name */ for (i = 0; i < PART_NAME_LENGTH; i++) { if (endp[i] == '\0') { /* Hey, we need the part description after the name */ printf("Malformed parts description file at line %d, nothing after part name.\n", line); goto out_err_2; } if (endp[i] == ',') { break; } part->name[i] = endp[i]; } if ((i == PART_NAME_LENGTH) && (endp[i] != ',')) { printf("Malformed parts description file at line %d, part name too long.\n", line); goto out_err_2; } endp += i; /* Get all the values */ nval = ((sizeof(struct part_desc) - offsetof(struct part_desc, flash_base)) / sizeof(uint32_t)); part_values = &(part->flash_base); /* Use a table to read the data, we do not care of what the data is */ for (i = 0; i < nval; i++) { part_values[i] = strtoul((endp + 1), &endp, 0); if ((part_values[i] == 0) && (errno == EINVAL)) { printf("Malformed parts description file at line %d, error reading value %d\n", line, i); goto out_err_2; } } /* Done, retrun the part ! */ return part; } out_err_2: free(part->name); out_err_1: free(part); fclose(parts_file); return NULL; } lpctools-v1.07/parts.h000066400000000000000000000035431247765405200150110ustar00rootroot00000000000000/********************************************************************* * * LPC Parts identification * * * Copyright (C) 2012 Nathael Pajani * * 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 3 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, see . * *********************************************************************/ #ifndef FIND_PART_H #define FIND_PART_H #include /* uint64_t and uint32_t */ #define PART_NAME_LENGTH 25 /* Part descriptor */ /* ALL data below name MUST be of type uint32_t */ struct part_desc { uint64_t part_id; char* name; /* Flash */ uint32_t flash_base; uint32_t flash_size; uint32_t flash_nb_sectors; uint32_t reset_vector_offset; /* RAM */ uint32_t ram_base; uint32_t ram_size; uint32_t ram_buff_offset; /* Used to transfer data for flashing */ uint32_t ram_buff_size; uint32_t uuencode; }; /* When looking for parts description in a file ee do allocate (malloc) two memory * chunks which we will never free. * The user should free part_desc->name and part_desc when they are no more useful */ struct part_desc* find_part_in_file(uint64_t dev_id, char* conf_file_name); /* Find a part in the internal parts table */ /* FIXME : To be inplemented ? */ struct part_desc* find_part_internal_tab(uint64_t dev_id); #endif /* FIND_PART_H */ lpctools-v1.07/prog_commands.c000066400000000000000000000212061247765405200164770ustar00rootroot00000000000000/********************************************************************* * * LPC ISP Commands for flash tool * * * Copyright (C) 2012 Nathael Pajani * * 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 3 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, see . * *********************************************************************/ #include /* strtoul */ #include /* printf, snprintf */ #include #include #include /* strncmp, memset */ #include #include #include /* for open, close */ #include #include #include #include "isp_utils.h" #include "isp_commands.h" #include "parts.h" #define REP_BUFSIZE 40 extern int trace_on; int get_ids(void) { int ret = 0; isp_cmd_part_id(0); isp_cmd_read_uid(); isp_cmd_boot_version(); return ret; } int dump_to_file(struct part_desc* part, char* filename) { int ret = 0, len = 0; char* data; /* Allocate buffer */ data = malloc(part->flash_size); if (data == NULL) { printf("Unable to allocate read buffer, asked %u.\n", part->flash_size); return -10; } /* Read data */ len = isp_read_memory(data, part->flash_base, part->flash_size, part->uuencode); if (len != (int)(part->flash_size)) { printf("Read returned %d bytes instead of %u.\n", len, part->flash_size); } /* Write data to file */ ret = isp_buff_to_file(data, part->flash_size, filename); /* Free memory */ free(data); return ret; } int erase_flash(struct part_desc* part) { int ret = 0; int i = 0; /* Unlock device */ ret = isp_cmd_unlock(1); if (ret != 0) { printf("Unable to unlock device, aborting.\n"); return -1; } for (i=0; i<(int)(part->flash_nb_sectors); i++) { ret = isp_send_cmd_sectors("blank-check", 'I', i, i, 1); if (ret == CMD_SUCCESS) { /* sector already blank, preserve the flash, skip to next one :) */ continue; } if (ret < 0) { /* Error ? */ printf("Initial blank check error (%d) at sector %d!\n", ret, i); return ret; } else { /* Controller replyed with first non blank offset and data, remove it from buffer */ char buf[REP_BUFSIZE]; usleep( 5000 ); /* Some devices are slow to scan flash, give them some time */ isp_serial_read(buf, REP_BUFSIZE, 3); } /* Sector not blank, perform erase */ ret = isp_send_cmd_sectors("prepare-for-write", 'P', i, i, 1); if (ret != 0) { printf("Error (%d) when trying to prepare sector %d for erase operation!\n", ret, i); return ret; } ret = isp_send_cmd_sectors("erase", 'E', i, i, 1); if (ret != 0) { printf("Error (%d) when trying to erase sector %d!\n", ret, i); return ret; } } printf("Flash now all blank.\n"); return 0; } int start_prog(struct part_desc* part) { int ret = 0, len = 0; uint32_t addr = 0; len = isp_read_memory((char*)&addr, (part->flash_base + part->reset_vector_offset), sizeof(addr), part->uuencode); if (len != sizeof(addr)) { printf("Unable to read reset address from flash.\n"); return len; } /* FIXME : the following value (0x200) may be LPC111x specific */ if (addr < 0x200) { printf("Actual reset address is 0x%08x, which is under the lowest allowed address of 0x200.\n", addr); } /* Unlock device */ ret = isp_cmd_unlock(1); if (ret != 0) { printf("Unable to unlock device, aborting.\n"); return -1; } /* Read address in thumb or arm mode ? */ if (addr & 0x01) { addr &= ~1UL; ret = isp_send_cmd_go(addr, 'T'); } else { ret = isp_send_cmd_go(addr, 'A'); } /* FIXME : start terminal */ return ret; } static unsigned int calc_write_size(unsigned int sector_size, unsigned int ram_buff_size) { unsigned int write_size = 0; write_size = ((sector_size < ram_buff_size) ? sector_size : ram_buff_size); /* According to section 21.5.7 of LPC11xx user's manual (UM10398), number of bytes * written should be 256 | 512 | 1024 | 4096 */ if (write_size >= 4096) { write_size = 4096; } else if (write_size >= 1024) { write_size = 1024; } else if (write_size >= 512) { write_size = 512; } else if (write_size >= 256) { write_size = 256; } else if (write_size >= 64) { write_size = 64; } else { write_size = 0; } return write_size; } int flash_target(struct part_desc* part, char* filename, int calc_user_code) { int ret = 0; char* data = NULL; int size = 0; int i = 0, blocks = 0; unsigned int write_size = 0; unsigned int sector_size = (part->flash_size / part->flash_nb_sectors); uint32_t ram_addr = (part->ram_base + part->ram_buff_offset); uint32_t uuencode = part->uuencode; uint32_t* v = NULL; /* Used for checksum computing */ uint32_t cksum = 0; uint32_t crp = 0; /** Sanity checks *********************************/ /* RAM buffer address within RAM */ if (ram_addr > (part->ram_base + part->ram_size)) { printf("Invalid configuration, asked to use buffer out of RAM, aborting.\n"); return -1; } /* Calc write block size */ write_size = calc_write_size(sector_size, part->ram_buff_size); if (write_size == 0) { printf("Config error, I cannot flash using blocks of nul size !\nAborted.\n"); return -2; } /* Just make sure flash is erased */ ret = erase_flash(part); if (ret != 0) { printf("Unable to erase device, aborting.\n"); return -3; } /* Allocate a buffer as big as the flash */ data = malloc(part->flash_size); if (data == NULL) { printf("Unable to get a buffer to load the image!"); return -4; } /* And fill the buffer with the image */ size = isp_file_to_buff(data, part->flash_size, filename); if (size <= 0){ free(data); return -5; } /* Fill unused buffer with 0's so we can flash blocks of data of "write_size" */ memset(&data[size], 0, (part->flash_size - size)); /* And check checksum of first 7 vectors if asked, according to section 21.3.3 of * LPC11xx user's manual (UM10398) */ v = (uint32_t *)data; cksum = 0 - v[0] - v[1] - v[2] - v[3] - v[4] - v[5] - v[6]; if (calc_user_code == 1) { v[7] = cksum; } else if (cksum != v[7]) { printf("Checksum is 0x%08x, should be 0x%08x\n", v[7], cksum); free(data); return -5; } printf("Checksum check OK\n"); crp = v[(CRP_OFFSET / 4)]; if ((crp == CRP_NO_ISP) || (crp == CRP_CRP1) || (crp == CRP_CRP2) || (crp == CRP_CRP3)) { printf("CRP : 0x%08x\n", crp); printf("The binary has CRP protection ativated, which violates GPLv3.\n"); printf("Check the licence for the software you are using, and if this is allowed,\n"); printf(" then modify this software to allow flashing of code with CRP protection\n"); printf(" activated. (Or use another software).\n"); return -6; } blocks = (size / write_size) + ((size % write_size) ? 1 : 0); /* Gonna write out of flash ? */ if ((blocks * write_size) > part->flash_size) { printf("Config error, I cannot flash beyond end of flash !\n"); printf("Flash size : %d, trying to flash %d blocks of %d bytes : %d\n", part->flash_size, blocks, write_size, (blocks * write_size)); free(data); return -7; } printf("Flash size : %d, trying to flash %d blocks of %d bytes : %d\n", part->flash_size, blocks, write_size, (blocks * write_size)); /* Now flash the device */ printf("Writing started, %d blocks of %d bytes ...\n", blocks, write_size); for (i=0; iflash_base + (i * write_size); /* Prepare sector for writting (must be done before each write) */ ret = isp_send_cmd_sectors("prepare-for-write", 'P', current_sector, current_sector, 1); if (ret != 0) { printf("Error (%d) when trying to prepare sector %d for erase operation!\n", ret, i); free(data); return ret; } /* Send data to RAM */ ret = isp_send_buf_to_ram(&data[i * write_size], ram_addr, write_size, uuencode); if (ret != 0) { printf("Unable to perform write-to-ram operation for block %d (block size: %d)\n", i, write_size); free(data); return ret; } /* Copy from RAM to FLASH */ ret = isp_send_cmd_address('C', flash_addr, ram_addr, write_size, "write_to_ram"); if (ret != 0) { printf("Unable to copy data to flash for block %d (block size: %d)\n", i, write_size); free(data); return ret; } } free(data); return ret; } lpctools-v1.07/prog_commands.h000066400000000000000000000023601247765405200165040ustar00rootroot00000000000000/********************************************************************* * * LPC ISP Commands for flash tool * * * Copyright (C) 2012 Nathael Pajani * * 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 3 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, see . * *********************************************************************/ #ifndef ISP_CMDS_FLASH_H #define ISP_CMDS_FLASH_H #include "parts.h" int dump_to_file(struct part_desc* part, char* filename); int erase_flash(struct part_desc* part); int flash_target(struct part_desc* part, char* filename, int check_user_code); int get_ids(void); int start_prog(struct part_desc* part); #endif /* ISP_CMDS_FLASH_H */ lpctools-v1.07/tests_non_regression000066400000000000000000000105031247765405200177000ustar00rootroot00000000000000./lpcisp /dev/ttyUSB0 -t -s read-uid sdfsdf ldsfgk ./lpcisp /dev/ttyUSB0 -t -s read-uid sdfsdf ldsfgk -h ./lpcisp /dev/ttyUSB0 -t -s ./lpcisp /dev/ttyUSB0 -t read-uid sdfsdf ldsfg UID: 0x0b0b134f - 0x53531b87 - 0x4d3ccb42 - 0xf5000000 ./lpcisp /dev/ttyUSB0 -t echo ./lpcisp /dev/ttyUSB0 -t erase ./lpcisp /dev/ttyUSB0 -t read-part-id Part ID is 0x2540102b ./lpcisp /dev/ttyUSB0 -t read-uid UID: 0x0b0b134f - 0x53531b87 - 0x4d3ccb42 - 0xf5000000 ./lpcisp /dev/ttyUSB0 -t read-boot-version Boot code version is 1.7 ./lpcisp /dev/ttyUSB0 read-boot-version ./lpcisp /dev/ttyUSB0 read-part-id ./lpcisp /dev/ttyUSB0 read-uid ./lpcisp /dev/ttyUSB0 -s ./lpcisp /dev/ttyUSB0 unlock ./lpcisp /dev/ttyUSB0 --synchronize ./lpcisp /dev/ttyUSB0 -t read-memory ./lpcisp /dev/ttyUSB0 -t read-memory 0x15 1548 ./lpcisp /dev/ttyUSB0 -t read-memory 0x1000 4 ./lpcisp /dev/ttyUSB0 -t read-memory 0x1000 40 ./lpcisp /dev/ttyUSB0 -t read-memory 0x1000 900 ./lpcisp /dev/ttyUSB0 -t read-memory 0x1000 150 ./lpcisp /dev/ttyUSB0 -t read-memory 0x1000 160 ./lpcisp /dev/ttyUSB0 -t read-memory 0x1000 4 - ./lpcisp /dev/ttyUSB0 -t read-memory 0x1000 800 - ./lpcisp /dev/ttyUSB0 read-memory 0x1000 800 - ./lpcisp /dev/ttyUSB0 -t read-memory 0x1000 800 dump01 ./lpcisp /dev/ttyUSB0 -t read-memory 0x1000 805 ../dump01 ./lpcisp /dev/ttyUSB0 -t read-memory 0x1000 804 ../dump01 ./lpcisp /dev/ttyUSB0 -t read-memory 0x1000 808 ../dump01 ./lpcisp /dev/ttyUSB0 -t read-memory 0x1000 1204 ../dump01 ./lpcisp /dev/ttyUSB0 -t read-memory 0x1000 900 ../dump01 ./lpcisp /dev/ttyUSB0 -t read-memory 0x1000 1840 ../dump01 ./lpcisp /dev/ttyUSB0 -t read-memory 0x1000 4 ../dump01 ./lpcisp /dev/ttyUSB0 read-memory 0x0000 1800 ../dump01 ./lpcisp /dev/ttyUSB0 read-memory 0x0000 600 ../dump01 ./lpcisp /dev/ttyUSB0 read-memory 0x0000 600 - ./lpcisp /dev/ttyUSB0 -t unlock ./lpcisp /dev/ttyUSB0 -t blank-check ./lpcisp /dev/ttyUSB0 -t blank-check 1 3 ./lpcisp /dev/ttyUSB0 -t blank-check 0 3 ./lpcisp /dev/ttyUSB0 -t blank-check 4 3 ./lpcisp /dev/ttyUSB0 -t blank-check 4 4 ./lpcisp /dev/ttyUSB0 -t blank-check 3 4 ./lpcisp /dev/ttyUSB0 -t blank-check 4 6 ./lpcisp /dev/ttyUSB0 read-memory 0x4000 12 - ./lpcisp /dev/ttyUSB0 -t blank-check 4 8 ./lpcisp /dev/ttyUSB0 -t blank-check 4 7 ./lpcisp /dev/ttyUSB0 blank-check 4 7 ./lpcisp /dev/ttyUSB0 blank-check 1 1 ./lpcisp /dev/ttyUSB0 read-memory 0x0000 0x4000 - > ../dump_full ./lpcisp /dev/ttyUSB0 -s ./lpcisp /dev/ttyUSB0 read-part-id ./lpcisp /dev/ttyUSB0 read-boot-version ./lpcisp /dev/ttyUSB0 compare 0x4000 0x5000 60 ./lpcisp /dev/ttyUSB0 compare 0x4000 0x5000 65 ./lpcisp /dev/ttyUSB0 compare 0x4000 0x5000 66 ./lpcisp /dev/ttyUSB0 compare 0x4000 0x5000 68 ./lpcisp /dev/ttyUSB0 compare 0x4000 0x5000 1000 ./lpcisp /dev/ttyUSB0 compare 0x1000 0x5000 1000 ./lpcisp /dev/ttyUSB0 compare 0x1000 0x5000 4 ./lpcisp /dev/ttyUSB0 compare 0x1BB0 0x1C80 4 ./lpcisp /dev/ttyUSB0 compare 0x1BB0 0x1BC8 4 ./lpcisp /dev/ttyUSB0 compare 0x1BB0 0x1C08 4 ./lpcisp /dev/ttyUSB0 go 512 toto ./lpcisp /dev/ttyUSB0 go 512 arm ./lpcisp /dev/ttyUSB0 go 512 thumb ./lpcisp /dev/ttyUSB0 unlock ./lpcisp /dev/ttyUSB0 go 512 thumb ./lpcisp /dev/ttyUSB0 read-boot-version ./lpcisp /dev/ttyUSB0 go 500 thumb ./lpcisp /dev/ttyUSB0 go 0x5000 thumb ./lpcisp /dev/ttyUSB0 -s ./lpcisp /dev/ttyUSB0 read-boot-version ./lpcisp /dev/ttyUSB0 read-boot-version ./lpcisp /dev/ttyUSB0 unlock ./lpcisp /dev/ttyUSB0 compare 0x1BB0 0x1C08 4 ./lpcisp /dev/ttyUSB0 prepare-for-write 6 6 ./lpcisp /dev/ttyUSB0 erase 6 6 ./lpcisp /dev/ttyUSB0 blank-check 6 6 ./lpcisp /dev/ttyUSB0 prepare-for-write 6 6 ./lpcisp /dev/ttyUSB0 copy-ram-to-flash 0x6000 0x10000800 0x400 ./lpcisp /dev/ttyUSB0 prepare-for-write 6 6 ./lpcisp /dev/ttyUSB0 copy-ram-to-flash 0x6400 0x10000C00 0x400 ./lpcisp /dev/ttyUSB0 read-memory 0x10000800 0x800 dumptest_ram ./lpcisp /dev/ttyUSB0 read-memory 0x6000 0x800 dumptest_flash diff dumptest_ram dumptest_flash ./lpcisp /dev/ttyUSB0 -s ./lpcisp /dev/ttyUSB0 unlock ./lpcisp /dev/ttyUSB0 prepare-for-write 6 6 ./lpcisp /dev/ttyUSB0 erase 6 6 ./lpcisp /dev/ttyUSB0 blank-check 6 6 dd if=isp of=src bs=1024 count=1 ./lpcisp /dev/ttyUSB0 write-to-ram 0x10000800 src ./lpcisp /dev/ttyUSB0 read-memory 0x10000800 0x400 reread diff src reread ./lpcisp /dev/ttyUSB0 prepare-for-write 6 6 ./lpcisp /dev/ttyUSB0 copy-ram-to-flash 0x6000 0x10000800 0x400 ./lpcisp /dev/ttyUSB0 read-memory 0x6000 0x400 reread_flash diff reread_flash src lpctools-v1.07/tests_non_regression.prog000066400000000000000000000005151247765405200206500ustar00rootroot00000000000000./lpcprog ./lpcprog id ./lpcprog -c id ./lpcprog -d /dev/ttyUSB0 -c id Part ID is 0x2540102b UID: 0x0b0b134f - 0x53531b87 - 0x4d3ccb42 - 0xf5000000 Boot code version is 1.7 ./lpcprog -d /dev/ttyUSB0 -c toto ./lpcprog -d /dev/ttyUSB0 -c dump test ./lpcprog -d /dev/ttyUSB0 -c flash ./lpcprog -d /dev/ttyUSB0 -c flash mod_gpio.bin