├── .gitignore ├── COPYING ├── Makefile.am ├── README.md ├── configure.ac ├── m4 └── check_libusb.m4 ├── ols-fwloader.cbp └── src ├── Makefile.am ├── boot_if.h ├── data_file.c ├── data_file.h ├── main.c ├── ols-boot.c ├── ols-boot.h ├── ols.c ├── ols.h ├── serial.c └── serial.h /.gitignore: -------------------------------------------------------------------------------- 1 | *.bin 2 | *.hex 3 | *.o 4 | ols-fwloader* -------------------------------------------------------------------------------- /COPYING: -------------------------------------------------------------------------------- 1 | GNU GENERAL PUBLIC LICENSE 2 | Version 3, 29 June 2007 3 | 4 | Copyright (C) 2007 Free Software Foundation, Inc. 5 | Everyone is permitted to copy and distribute verbatim copies 6 | of this license document, but changing it is not allowed. 7 | 8 | Preamble 9 | 10 | The GNU General Public License is a free, copyleft license for 11 | software and other kinds of works. 12 | 13 | The licenses for most software and other practical works are designed 14 | to take away your freedom to share and change the works. By contrast, 15 | the GNU General Public License is intended to guarantee your freedom to 16 | share and change all versions of a program--to make sure it remains free 17 | software for all its users. We, the Free Software Foundation, use the 18 | GNU General Public License for most of our software; it applies also to 19 | any other work released this way by its authors. You can apply it to 20 | your programs, too. 21 | 22 | When we speak of free software, we are referring to freedom, not 23 | price. Our General Public Licenses are designed to make sure that you 24 | have the freedom to distribute copies of free software (and charge for 25 | them if you wish), that you receive source code or can get it if you 26 | want it, that you can change the software or use pieces of it in new 27 | free programs, and that you know you can do these things. 28 | 29 | To protect your rights, we need to prevent others from denying you 30 | these rights or asking you to surrender the rights. Therefore, you have 31 | certain responsibilities if you distribute copies of the software, or if 32 | you modify it: responsibilities to respect the freedom of others. 33 | 34 | For example, if you distribute copies of such a program, whether 35 | gratis or for a fee, you must pass on to the recipients the same 36 | freedoms that you received. You must make sure that they, too, receive 37 | or can get the source code. And you must show them these terms so they 38 | know their rights. 39 | 40 | Developers that use the GNU GPL protect your rights with two steps: 41 | (1) assert copyright on the software, and (2) offer you this License 42 | giving you legal permission to copy, distribute and/or modify it. 43 | 44 | For the developers' and authors' protection, the GPL clearly explains 45 | that there is no warranty for this free software. For both users' and 46 | authors' sake, the GPL requires that modified versions be marked as 47 | changed, so that their problems will not be attributed erroneously to 48 | authors of previous versions. 49 | 50 | Some devices are designed to deny users access to install or run 51 | modified versions of the software inside them, although the manufacturer 52 | can do so. This is fundamentally incompatible with the aim of 53 | protecting users' freedom to change the software. The systematic 54 | pattern of such abuse occurs in the area of products for individuals to 55 | use, which is precisely where it is most unacceptable. Therefore, we 56 | have designed this version of the GPL to prohibit the practice for those 57 | products. If such problems arise substantially in other domains, we 58 | stand ready to extend this provision to those domains in future versions 59 | of the GPL, as needed to protect the freedom of users. 60 | 61 | Finally, every program is threatened constantly by software patents. 62 | States should not allow patents to restrict development and use of 63 | software on general-purpose computers, but in those that do, we wish to 64 | avoid the special danger that patents applied to a free program could 65 | make it effectively proprietary. To prevent this, the GPL assures that 66 | patents cannot be used to render the program non-free. 67 | 68 | The precise terms and conditions for copying, distribution and 69 | modification follow. 70 | 71 | TERMS AND CONDITIONS 72 | 73 | 0. Definitions. 74 | 75 | "This License" refers to version 3 of the GNU General Public License. 76 | 77 | "Copyright" also means copyright-like laws that apply to other kinds of 78 | works, such as semiconductor masks. 79 | 80 | "The Program" refers to any copyrightable work licensed under this 81 | License. Each licensee is addressed as "you". "Licensees" and 82 | "recipients" may be individuals or organizations. 83 | 84 | To "modify" a work means to copy from or adapt all or part of the work 85 | in a fashion requiring copyright permission, other than the making of an 86 | exact copy. The resulting work is called a "modified version" of the 87 | earlier work or a work "based on" the earlier work. 88 | 89 | A "covered work" means either the unmodified Program or a work based 90 | on the Program. 91 | 92 | To "propagate" a work means to do anything with it that, without 93 | permission, would make you directly or secondarily liable for 94 | infringement under applicable copyright law, except executing it on a 95 | computer or modifying a private copy. Propagation includes copying, 96 | distribution (with or without modification), making available to the 97 | public, and in some countries other activities as well. 98 | 99 | To "convey" a work means any kind of propagation that enables other 100 | parties to make or receive copies. Mere interaction with a user through 101 | a computer network, with no transfer of a copy, is not conveying. 102 | 103 | An interactive user interface displays "Appropriate Legal Notices" 104 | to the extent that it includes a convenient and prominently visible 105 | feature that (1) displays an appropriate copyright notice, and (2) 106 | tells the user that there is no warranty for the work (except to the 107 | extent that warranties are provided), that licensees may convey the 108 | work under this License, and how to view a copy of this License. If 109 | the interface presents a list of user commands or options, such as a 110 | menu, a prominent item in the list meets this criterion. 111 | 112 | 1. Source Code. 113 | 114 | The "source code" for a work means the preferred form of the work 115 | for making modifications to it. "Object code" means any non-source 116 | form of a work. 117 | 118 | A "Standard Interface" means an interface that either is an official 119 | standard defined by a recognized standards body, or, in the case of 120 | interfaces specified for a particular programming language, one that 121 | is widely used among developers working in that language. 122 | 123 | The "System Libraries" of an executable work include anything, other 124 | than the work as a whole, that (a) is included in the normal form of 125 | packaging a Major Component, but which is not part of that Major 126 | Component, and (b) serves only to enable use of the work with that 127 | Major Component, or to implement a Standard Interface for which an 128 | implementation is available to the public in source code form. A 129 | "Major Component", in this context, means a major essential component 130 | (kernel, window system, and so on) of the specific operating system 131 | (if any) on which the executable work runs, or a compiler used to 132 | produce the work, or an object code interpreter used to run it. 133 | 134 | The "Corresponding Source" for a work in object code form means all 135 | the source code needed to generate, install, and (for an executable 136 | work) run the object code and to modify the work, including scripts to 137 | control those activities. However, it does not include the work's 138 | System Libraries, or general-purpose tools or generally available free 139 | programs which are used unmodified in performing those activities but 140 | which are not part of the work. For example, Corresponding Source 141 | includes interface definition files associated with source files for 142 | the work, and the source code for shared libraries and dynamically 143 | linked subprograms that the work is specifically designed to require, 144 | such as by intimate data communication or control flow between those 145 | subprograms and other parts of the work. 146 | 147 | The Corresponding Source need not include anything that users 148 | can regenerate automatically from other parts of the Corresponding 149 | Source. 150 | 151 | The Corresponding Source for a work in source code form is that 152 | same work. 153 | 154 | 2. Basic Permissions. 155 | 156 | All rights granted under this License are granted for the term of 157 | copyright on the Program, and are irrevocable provided the stated 158 | conditions are met. This License explicitly affirms your unlimited 159 | permission to run the unmodified Program. The output from running a 160 | covered work is covered by this License only if the output, given its 161 | content, constitutes a covered work. This License acknowledges your 162 | rights of fair use or other equivalent, as provided by copyright law. 163 | 164 | You may make, run and propagate covered works that you do not 165 | convey, without conditions so long as your license otherwise remains 166 | in force. You may convey covered works to others for the sole purpose 167 | of having them make modifications exclusively for you, or provide you 168 | with facilities for running those works, provided that you comply with 169 | the terms of this License in conveying all material for which you do 170 | not control copyright. Those thus making or running the covered works 171 | for you must do so exclusively on your behalf, under your direction 172 | and control, on terms that prohibit them from making any copies of 173 | your copyrighted material outside their relationship with you. 174 | 175 | Conveying under any other circumstances is permitted solely under 176 | the conditions stated below. Sublicensing is not allowed; section 10 177 | makes it unnecessary. 178 | 179 | 3. Protecting Users' Legal Rights From Anti-Circumvention Law. 180 | 181 | No covered work shall be deemed part of an effective technological 182 | measure under any applicable law fulfilling obligations under article 183 | 11 of the WIPO copyright treaty adopted on 20 December 1996, or 184 | similar laws prohibiting or restricting circumvention of such 185 | measures. 186 | 187 | When you convey a covered work, you waive any legal power to forbid 188 | circumvention of technological measures to the extent such circumvention 189 | is effected by exercising rights under this License with respect to 190 | the covered work, and you disclaim any intention to limit operation or 191 | modification of the work as a means of enforcing, against the work's 192 | users, your or third parties' legal rights to forbid circumvention of 193 | technological measures. 194 | 195 | 4. Conveying Verbatim Copies. 196 | 197 | You may convey verbatim copies of the Program's source code as you 198 | receive it, in any medium, provided that you conspicuously and 199 | appropriately publish on each copy an appropriate copyright notice; 200 | keep intact all notices stating that this License and any 201 | non-permissive terms added in accord with section 7 apply to the code; 202 | keep intact all notices of the absence of any warranty; and give all 203 | recipients a copy of this License along with the Program. 204 | 205 | You may charge any price or no price for each copy that you convey, 206 | and you may offer support or warranty protection for a fee. 207 | 208 | 5. Conveying Modified Source Versions. 209 | 210 | You may convey a work based on the Program, or the modifications to 211 | produce it from the Program, in the form of source code under the 212 | terms of section 4, provided that you also meet all of these conditions: 213 | 214 | a) The work must carry prominent notices stating that you modified 215 | it, and giving a relevant date. 216 | 217 | b) The work must carry prominent notices stating that it is 218 | released under this License and any conditions added under section 219 | 7. This requirement modifies the requirement in section 4 to 220 | "keep intact all notices". 221 | 222 | c) You must license the entire work, as a whole, under this 223 | License to anyone who comes into possession of a copy. This 224 | License will therefore apply, along with any applicable section 7 225 | additional terms, to the whole of the work, and all its parts, 226 | regardless of how they are packaged. This License gives no 227 | permission to license the work in any other way, but it does not 228 | invalidate such permission if you have separately received it. 229 | 230 | d) If the work has interactive user interfaces, each must display 231 | Appropriate Legal Notices; however, if the Program has interactive 232 | interfaces that do not display Appropriate Legal Notices, your 233 | work need not make them do so. 234 | 235 | A compilation of a covered work with other separate and independent 236 | works, which are not by their nature extensions of the covered work, 237 | and which are not combined with it such as to form a larger program, 238 | in or on a volume of a storage or distribution medium, is called an 239 | "aggregate" if the compilation and its resulting copyright are not 240 | used to limit the access or legal rights of the compilation's users 241 | beyond what the individual works permit. Inclusion of a covered work 242 | in an aggregate does not cause this License to apply to the other 243 | parts of the aggregate. 244 | 245 | 6. Conveying Non-Source Forms. 246 | 247 | You may convey a covered work in object code form under the terms 248 | of sections 4 and 5, provided that you also convey the 249 | machine-readable Corresponding Source under the terms of this License, 250 | in one of these ways: 251 | 252 | a) Convey the object code in, or embodied in, a physical product 253 | (including a physical distribution medium), accompanied by the 254 | Corresponding Source fixed on a durable physical medium 255 | customarily used for software interchange. 256 | 257 | b) Convey the object code in, or embodied in, a physical product 258 | (including a physical distribution medium), accompanied by a 259 | written offer, valid for at least three years and valid for as 260 | long as you offer spare parts or customer support for that product 261 | model, to give anyone who possesses the object code either (1) a 262 | copy of the Corresponding Source for all the software in the 263 | product that is covered by this License, on a durable physical 264 | medium customarily used for software interchange, for a price no 265 | more than your reasonable cost of physically performing this 266 | conveying of source, or (2) access to copy the 267 | Corresponding Source from a network server at no charge. 268 | 269 | c) Convey individual copies of the object code with a copy of the 270 | written offer to provide the Corresponding Source. This 271 | alternative is allowed only occasionally and noncommercially, and 272 | only if you received the object code with such an offer, in accord 273 | with subsection 6b. 274 | 275 | d) Convey the object code by offering access from a designated 276 | place (gratis or for a charge), and offer equivalent access to the 277 | Corresponding Source in the same way through the same place at no 278 | further charge. You need not require recipients to copy the 279 | Corresponding Source along with the object code. If the place to 280 | copy the object code is a network server, the Corresponding Source 281 | may be on a different server (operated by you or a third party) 282 | that supports equivalent copying facilities, provided you maintain 283 | clear directions next to the object code saying where to find the 284 | Corresponding Source. Regardless of what server hosts the 285 | Corresponding Source, you remain obligated to ensure that it is 286 | available for as long as needed to satisfy these requirements. 287 | 288 | e) Convey the object code using peer-to-peer transmission, provided 289 | you inform other peers where the object code and Corresponding 290 | Source of the work are being offered to the general public at no 291 | charge under subsection 6d. 292 | 293 | A separable portion of the object code, whose source code is excluded 294 | from the Corresponding Source as a System Library, need not be 295 | included in conveying the object code work. 296 | 297 | A "User Product" is either (1) a "consumer product", which means any 298 | tangible personal property which is normally used for personal, family, 299 | or household purposes, or (2) anything designed or sold for incorporation 300 | into a dwelling. In determining whether a product is a consumer product, 301 | doubtful cases shall be resolved in favor of coverage. For a particular 302 | product received by a particular user, "normally used" refers to a 303 | typical or common use of that class of product, regardless of the status 304 | of the particular user or of the way in which the particular user 305 | actually uses, or expects or is expected to use, the product. A product 306 | is a consumer product regardless of whether the product has substantial 307 | commercial, industrial or non-consumer uses, unless such uses represent 308 | the only significant mode of use of the product. 309 | 310 | "Installation Information" for a User Product means any methods, 311 | procedures, authorization keys, or other information required to install 312 | and execute modified versions of a covered work in that User Product from 313 | a modified version of its Corresponding Source. The information must 314 | suffice to ensure that the continued functioning of the modified object 315 | code is in no case prevented or interfered with solely because 316 | modification has been made. 317 | 318 | If you convey an object code work under this section in, or with, or 319 | specifically for use in, a User Product, and the conveying occurs as 320 | part of a transaction in which the right of possession and use of the 321 | User Product is transferred to the recipient in perpetuity or for a 322 | fixed term (regardless of how the transaction is characterized), the 323 | Corresponding Source conveyed under this section must be accompanied 324 | by the Installation Information. But this requirement does not apply 325 | if neither you nor any third party retains the ability to install 326 | modified object code on the User Product (for example, the work has 327 | been installed in ROM). 328 | 329 | The requirement to provide Installation Information does not include a 330 | requirement to continue to provide support service, warranty, or updates 331 | for a work that has been modified or installed by the recipient, or for 332 | the User Product in which it has been modified or installed. Access to a 333 | network may be denied when the modification itself materially and 334 | adversely affects the operation of the network or violates the rules and 335 | protocols for communication across the network. 336 | 337 | Corresponding Source conveyed, and Installation Information provided, 338 | in accord with this section must be in a format that is publicly 339 | documented (and with an implementation available to the public in 340 | source code form), and must require no special password or key for 341 | unpacking, reading or copying. 342 | 343 | 7. Additional Terms. 344 | 345 | "Additional permissions" are terms that supplement the terms of this 346 | License by making exceptions from one or more of its conditions. 347 | Additional permissions that are applicable to the entire Program shall 348 | be treated as though they were included in this License, to the extent 349 | that they are valid under applicable law. If additional permissions 350 | apply only to part of the Program, that part may be used separately 351 | under those permissions, but the entire Program remains governed by 352 | this License without regard to the additional permissions. 353 | 354 | When you convey a copy of a covered work, you may at your option 355 | remove any additional permissions from that copy, or from any part of 356 | it. (Additional permissions may be written to require their own 357 | removal in certain cases when you modify the work.) You may place 358 | additional permissions on material, added by you to a covered work, 359 | for which you have or can give appropriate copyright permission. 360 | 361 | Notwithstanding any other provision of this License, for material you 362 | add to a covered work, you may (if authorized by the copyright holders of 363 | that material) supplement the terms of this License with terms: 364 | 365 | a) Disclaiming warranty or limiting liability differently from the 366 | terms of sections 15 and 16 of this License; or 367 | 368 | b) Requiring preservation of specified reasonable legal notices or 369 | author attributions in that material or in the Appropriate Legal 370 | Notices displayed by works containing it; or 371 | 372 | c) Prohibiting misrepresentation of the origin of that material, or 373 | requiring that modified versions of such material be marked in 374 | reasonable ways as different from the original version; or 375 | 376 | d) Limiting the use for publicity purposes of names of licensors or 377 | authors of the material; or 378 | 379 | e) Declining to grant rights under trademark law for use of some 380 | trade names, trademarks, or service marks; or 381 | 382 | f) Requiring indemnification of licensors and authors of that 383 | material by anyone who conveys the material (or modified versions of 384 | it) with contractual assumptions of liability to the recipient, for 385 | any liability that these contractual assumptions directly impose on 386 | those licensors and authors. 387 | 388 | All other non-permissive additional terms are considered "further 389 | restrictions" within the meaning of section 10. If the Program as you 390 | received it, or any part of it, contains a notice stating that it is 391 | governed by this License along with a term that is a further 392 | restriction, you may remove that term. If a license document contains 393 | a further restriction but permits relicensing or conveying under this 394 | License, you may add to a covered work material governed by the terms 395 | of that license document, provided that the further restriction does 396 | not survive such relicensing or conveying. 397 | 398 | If you add terms to a covered work in accord with this section, you 399 | must place, in the relevant source files, a statement of the 400 | additional terms that apply to those files, or a notice indicating 401 | where to find the applicable terms. 402 | 403 | Additional terms, permissive or non-permissive, may be stated in the 404 | form of a separately written license, or stated as exceptions; 405 | the above requirements apply either way. 406 | 407 | 8. Termination. 408 | 409 | You may not propagate or modify a covered work except as expressly 410 | provided under this License. Any attempt otherwise to propagate or 411 | modify it is void, and will automatically terminate your rights under 412 | this License (including any patent licenses granted under the third 413 | paragraph of section 11). 414 | 415 | However, if you cease all violation of this License, then your 416 | license from a particular copyright holder is reinstated (a) 417 | provisionally, unless and until the copyright holder explicitly and 418 | finally terminates your license, and (b) permanently, if the copyright 419 | holder fails to notify you of the violation by some reasonable means 420 | prior to 60 days after the cessation. 421 | 422 | Moreover, your license from a particular copyright holder is 423 | reinstated permanently if the copyright holder notifies you of the 424 | violation by some reasonable means, this is the first time you have 425 | received notice of violation of this License (for any work) from that 426 | copyright holder, and you cure the violation prior to 30 days after 427 | your receipt of the notice. 428 | 429 | Termination of your rights under this section does not terminate the 430 | licenses of parties who have received copies or rights from you under 431 | this License. If your rights have been terminated and not permanently 432 | reinstated, you do not qualify to receive new licenses for the same 433 | material under section 10. 434 | 435 | 9. Acceptance Not Required for Having Copies. 436 | 437 | You are not required to accept this License in order to receive or 438 | run a copy of the Program. Ancillary propagation of a covered work 439 | occurring solely as a consequence of using peer-to-peer transmission 440 | to receive a copy likewise does not require acceptance. However, 441 | nothing other than this License grants you permission to propagate or 442 | modify any covered work. These actions infringe copyright if you do 443 | not accept this License. Therefore, by modifying or propagating a 444 | covered work, you indicate your acceptance of this License to do so. 445 | 446 | 10. Automatic Licensing of Downstream Recipients. 447 | 448 | Each time you convey a covered work, the recipient automatically 449 | receives a license from the original licensors, to run, modify and 450 | propagate that work, subject to this License. You are not responsible 451 | for enforcing compliance by third parties with this License. 452 | 453 | An "entity transaction" is a transaction transferring control of an 454 | organization, or substantially all assets of one, or subdividing an 455 | organization, or merging organizations. If propagation of a covered 456 | work results from an entity transaction, each party to that 457 | transaction who receives a copy of the work also receives whatever 458 | licenses to the work the party's predecessor in interest had or could 459 | give under the previous paragraph, plus a right to possession of the 460 | Corresponding Source of the work from the predecessor in interest, if 461 | the predecessor has it or can get it with reasonable efforts. 462 | 463 | You may not impose any further restrictions on the exercise of the 464 | rights granted or affirmed under this License. For example, you may 465 | not impose a license fee, royalty, or other charge for exercise of 466 | rights granted under this License, and you may not initiate litigation 467 | (including a cross-claim or counterclaim in a lawsuit) alleging that 468 | any patent claim is infringed by making, using, selling, offering for 469 | sale, or importing the Program or any portion of it. 470 | 471 | 11. Patents. 472 | 473 | A "contributor" is a copyright holder who authorizes use under this 474 | License of the Program or a work on which the Program is based. The 475 | work thus licensed is called the contributor's "contributor version". 476 | 477 | A contributor's "essential patent claims" are all patent claims 478 | owned or controlled by the contributor, whether already acquired or 479 | hereafter acquired, that would be infringed by some manner, permitted 480 | by this License, of making, using, or selling its contributor version, 481 | but do not include claims that would be infringed only as a 482 | consequence of further modification of the contributor version. For 483 | purposes of this definition, "control" includes the right to grant 484 | patent sublicenses in a manner consistent with the requirements of 485 | this License. 486 | 487 | Each contributor grants you a non-exclusive, worldwide, royalty-free 488 | patent license under the contributor's essential patent claims, to 489 | make, use, sell, offer for sale, import and otherwise run, modify and 490 | propagate the contents of its contributor version. 491 | 492 | In the following three paragraphs, a "patent license" is any express 493 | agreement or commitment, however denominated, not to enforce a patent 494 | (such as an express permission to practice a patent or covenant not to 495 | sue for patent infringement). To "grant" such a patent license to a 496 | party means to make such an agreement or commitment not to enforce a 497 | patent against the party. 498 | 499 | If you convey a covered work, knowingly relying on a patent license, 500 | and the Corresponding Source of the work is not available for anyone 501 | to copy, free of charge and under the terms of this License, through a 502 | publicly available network server or other readily accessible means, 503 | then you must either (1) cause the Corresponding Source to be so 504 | available, or (2) arrange to deprive yourself of the benefit of the 505 | patent license for this particular work, or (3) arrange, in a manner 506 | consistent with the requirements of this License, to extend the patent 507 | license to downstream recipients. "Knowingly relying" means you have 508 | actual knowledge that, but for the patent license, your conveying the 509 | covered work in a country, or your recipient's use of the covered work 510 | in a country, would infringe one or more identifiable patents in that 511 | country that you have reason to believe are valid. 512 | 513 | If, pursuant to or in connection with a single transaction or 514 | arrangement, you convey, or propagate by procuring conveyance of, a 515 | covered work, and grant a patent license to some of the parties 516 | receiving the covered work authorizing them to use, propagate, modify 517 | or convey a specific copy of the covered work, then the patent license 518 | you grant is automatically extended to all recipients of the covered 519 | work and works based on it. 520 | 521 | A patent license is "discriminatory" if it does not include within 522 | the scope of its coverage, prohibits the exercise of, or is 523 | conditioned on the non-exercise of one or more of the rights that are 524 | specifically granted under this License. You may not convey a covered 525 | work if you are a party to an arrangement with a third party that is 526 | in the business of distributing software, under which you make payment 527 | to the third party based on the extent of your activity of conveying 528 | the work, and under which the third party grants, to any of the 529 | parties who would receive the covered work from you, a discriminatory 530 | patent license (a) in connection with copies of the covered work 531 | conveyed by you (or copies made from those copies), or (b) primarily 532 | for and in connection with specific products or compilations that 533 | contain the covered work, unless you entered into that arrangement, 534 | or that patent license was granted, prior to 28 March 2007. 535 | 536 | Nothing in this License shall be construed as excluding or limiting 537 | any implied license or other defenses to infringement that may 538 | otherwise be available to you under applicable patent law. 539 | 540 | 12. No Surrender of Others' Freedom. 541 | 542 | If conditions are imposed on you (whether by court order, agreement or 543 | otherwise) that contradict the conditions of this License, they do not 544 | excuse you from the conditions of this License. If you cannot convey a 545 | covered work so as to satisfy simultaneously your obligations under this 546 | License and any other pertinent obligations, then as a consequence you may 547 | not convey it at all. For example, if you agree to terms that obligate you 548 | to collect a royalty for further conveying from those to whom you convey 549 | the Program, the only way you could satisfy both those terms and this 550 | License would be to refrain entirely from conveying the Program. 551 | 552 | 13. Use with the GNU Affero General Public License. 553 | 554 | Notwithstanding any other provision of this License, you have 555 | permission to link or combine any covered work with a work licensed 556 | under version 3 of the GNU Affero General Public License into a single 557 | combined work, and to convey the resulting work. The terms of this 558 | License will continue to apply to the part which is the covered work, 559 | but the special requirements of the GNU Affero General Public License, 560 | section 13, concerning interaction through a network will apply to the 561 | combination as such. 562 | 563 | 14. Revised Versions of this License. 564 | 565 | The Free Software Foundation may publish revised and/or new versions of 566 | the GNU General Public License from time to time. Such new versions will 567 | be similar in spirit to the present version, but may differ in detail to 568 | address new problems or concerns. 569 | 570 | Each version is given a distinguishing version number. If the 571 | Program specifies that a certain numbered version of the GNU General 572 | Public License "or any later version" applies to it, you have the 573 | option of following the terms and conditions either of that numbered 574 | version or of any later version published by the Free Software 575 | Foundation. If the Program does not specify a version number of the 576 | GNU General Public License, you may choose any version ever published 577 | by the Free Software Foundation. 578 | 579 | If the Program specifies that a proxy can decide which future 580 | versions of the GNU General Public License can be used, that proxy's 581 | public statement of acceptance of a version permanently authorizes you 582 | to choose that version for the Program. 583 | 584 | Later license versions may give you additional or different 585 | permissions. However, no additional obligations are imposed on any 586 | author or copyright holder as a result of your choosing to follow a 587 | later version. 588 | 589 | 15. Disclaimer of Warranty. 590 | 591 | THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY 592 | APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT 593 | HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY 594 | OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, 595 | THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 596 | PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM 597 | IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF 598 | ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 599 | 600 | 16. Limitation of Liability. 601 | 602 | IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 603 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS 604 | THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY 605 | GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE 606 | USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF 607 | DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD 608 | PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), 609 | EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF 610 | SUCH DAMAGES. 611 | 612 | 17. Interpretation of Sections 15 and 16. 613 | 614 | If the disclaimer of warranty and limitation of liability provided 615 | above cannot be given local legal effect according to their terms, 616 | reviewing courts shall apply local law that most closely approximates 617 | an absolute waiver of all civil liability in connection with the 618 | Program, unless a warranty or assumption of liability accompanies a 619 | copy of the Program in return for a fee. 620 | 621 | END OF TERMS AND CONDITIONS 622 | 623 | How to Apply These Terms to Your New Programs 624 | 625 | If you develop a new program, and you want it to be of the greatest 626 | possible use to the public, the best way to achieve this is to make it 627 | free software which everyone can redistribute and change under these terms. 628 | 629 | To do so, attach the following notices to the program. It is safest 630 | to attach them to the start of each source file to most effectively 631 | state the exclusion of warranty; and each file should have at least 632 | the "copyright" line and a pointer to where the full notice is found. 633 | 634 | 635 | Copyright (C) 636 | 637 | This program is free software: you can redistribute it and/or modify 638 | it under the terms of the GNU General Public License as published by 639 | the Free Software Foundation, either version 3 of the License, or 640 | (at your option) any later version. 641 | 642 | This program is distributed in the hope that it will be useful, 643 | but WITHOUT ANY WARRANTY; without even the implied warranty of 644 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 645 | GNU General Public License for more details. 646 | 647 | You should have received a copy of the GNU General Public License 648 | along with this program. If not, see . 649 | 650 | Also add information on how to contact you by electronic and paper mail. 651 | 652 | If the program does terminal interaction, make it output a short 653 | notice like this when it starts in an interactive mode: 654 | 655 | Copyright (C) 656 | This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'. 657 | This is free software, and you are welcome to redistribute it 658 | under certain conditions; type `show c' for details. 659 | 660 | The hypothetical commands `show w' and `show c' should show the appropriate 661 | parts of the General Public License. Of course, your program's commands 662 | might be different; for a GUI interface, you would use an "about box". 663 | 664 | You should also get your employer (if you work as a programmer) or school, 665 | if any, to sign a "copyright disclaimer" for the program, if necessary. 666 | For more information on this, and how to apply and follow the GNU GPL, see 667 | . 668 | 669 | The GNU General Public License does not permit incorporating your program 670 | into proprietary programs. If your program is a subroutine library, you 671 | may consider it more useful to permit linking proprietary applications with 672 | the library. If this is what you want to do, use the GNU Lesser General 673 | Public License instead of this License. But first, please read 674 | . 675 | -------------------------------------------------------------------------------- /Makefile.am: -------------------------------------------------------------------------------- 1 | ACLOCAL_AMFLAGS = -I m4 2 | 3 | SUBDIRS = src -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # OLS-fwloader 2 | 3 | This is commandline update utility for OLS - OpenBench Logic Sniffer. Logic Analyzer created by Dangerous Prototypes (http://dangerousprototypes.com). 4 | 5 | This tool uses parts from Diolan's fw_update tool to talk to the bootloader. Serial port routines first written by Piotr Pawluczuk for pirate-loader, adapted and improved to better fit this project. 6 | 7 | So far supported input file types are: 8 | * Binary 9 | * Intel Hex 10 | * Xilinx bit file 11 | 12 | All input files are autodetected. 13 | 14 | ## Requirements: 15 | 16 | * linux: 17 | * cdc_acm support in kernel 18 | * libusb 1.0 (install libusb-1.0.0-dev) 19 | * autoconf (to build from the GIT repository) 20 | * Windows: 21 | * cdc acm driver 22 | 23 | ## Building 24 | 25 | ### From a git repository 26 | 27 | At first do this, then follow the tarball instructions 28 | 29 | ``` 30 | $ autoreconf --install 31 | ``` 32 | 33 | ### From a tarball 34 | 35 | ``` 36 | $ ./configure 37 | $ make 38 | $ make install 39 | ``` 40 | 41 | I suggest to use `--prefix` parameter for configure. 42 | 43 | ## Usage Examples 44 | 45 | To use BOOT mode (reading/writing the PIC), you will first need to press RESET while the PGC and PGD terminals are jumpered. The ACT and PWR LEDs will be lit. Note that this is different from resetting while pressing the UPDATE button, which is for FPGA firmware update. 46 | 47 | In BOOT mode, the device will identify itself (see it with `lsusb` on linux) as 04d8:fc90 whereas in warm (logic analyser operating) mode, the device will be 04d8:fc92. 48 | 49 | You may need to run ols-fwloader as root (`sudo ols-fwloader ...`) to use the USB device directly in BOOT mode. 50 | 51 | Read current OLS PIC firmware into a file: 52 | 53 | ``` 54 | ols-fwloader -f BOOT -t HEX -R -r current-pic-firmware.hex 55 | ``` 56 | 57 | Write PIC firmware with verification, enter bootloader first: 58 | 59 | ``` 60 | ols-fwloader -f BOOT -n -P /dev/ttyACM0 -V -W -w new-firmware.hex 61 | ``` 62 | 63 | Write FPGA bitstream (HEX): 64 | 65 | ``` 66 | ols-fwloader -f APP -P /dev/ttyACM0 -W -w bitstream.mcs 67 | ``` 68 | 69 | Write FPGA bitstream (BIN): 70 | 71 | ``` 72 | ols-fwloader -f APP -P /dev/ttyACM0 -W -w bitstream.bit -t BIN 73 | ``` 74 | 75 | # Contributions 76 | 77 | Git repository can be found here: 78 | 79 | https://github.com/robots/ols-fwloader 80 | 81 | This tool is created and maintained by Michal Demin 82 | Feel free to send suggestions, improvements, patches. 83 | -------------------------------------------------------------------------------- /configure.ac: -------------------------------------------------------------------------------- 1 | AC_INIT([ols-fwloader],[0.2],[michal.demin@gmail.com]) 2 | AM_INIT_AUTOMAKE([foreign -Wall -Werror]) 3 | 4 | AC_CONFIG_MACRO_DIR([m4]) 5 | 6 | AC_CONFIG_HEADERS([config.h]) 7 | 8 | AM_INIT_AUTOMAKE 9 | 10 | 11 | AC_LANG_C 12 | AC_PROG_CC 13 | AM_PROG_CC_C_O 14 | # Libtool 15 | LT_INIT 16 | 17 | is_win32=no 18 | is_mingw=no 19 | is_darwin=no 20 | 21 | case "$host" in 22 | *-mingw32*) 23 | is_win32=yes 24 | is_mingw=yes 25 | AC_DEFINE(IS_MINGW, 1, [1 if building for MinGW.]) 26 | AC_DEFINE(IS_DARWIN, 0, [0 if not building for Darwin.]) 27 | AC_DEFINE(IS_WIN32, 1, [1 if building for Win32.]) 28 | ;; 29 | *darwin*) 30 | is_darwin=yes 31 | AC_DEFINE(IS_MINGW, 0, [0 if not building for MinGW.]) 32 | AC_DEFINE(IS_DARWIN, 1, [1 if building for Darwin.]) 33 | AC_DEFINE(IS_WIN32, 0, [0 if not building for Win32.]) 34 | ;; 35 | *) 36 | AC_DEFINE(IS_MINGW, 0, [0 if not building for MinGW.]) 37 | AC_DEFINE(IS_DARWIN, 0, [0 if not building for Darwin.]) 38 | AC_DEFINE(IS_WIN32, 0, [0 if not building for Win32.]) 39 | ;; 40 | esac 41 | 42 | # options 43 | AC_ARG_ENABLE([debug],AS_HELP_STRING([--enable-debug],[Enable debugging output]),[enable_debug=$enableval],[enable_debug="no"]) 44 | 45 | if test x"$enable_debug" = "xyes" 46 | then 47 | CFLAGS="$CFLAGS -Wall DDEBUG -g" 48 | fi 49 | 50 | win32_LIBS="" 51 | # for nonwin we need libusb 52 | if test $is_win32 = no; then 53 | # check dependencies 54 | AC_PATH_PROG(PKG_CONFIG, pkg-config) 55 | if test x"$PKG_CONFIG" = x""; then 56 | AC_MSG_ERROR([pkg-config not found.]) 57 | fi 58 | 59 | PKG_CONFIG_REQUIRES="" 60 | OLS_CHECK_LIBUSB 61 | AC_SUBST(PKG_CONFIG_REQUIRES) 62 | else 63 | win32_LIBS="-lhid -lsetupapi" 64 | fi 65 | AC_SUBST(win32_LIBS) 66 | 67 | AM_CONDITIONAL(IS_WIN32, test $is_win32 = yes) 68 | AM_CONDITIONAL(IS_MINGW, test $is_mingw = yes) 69 | AM_CONDITIONAL(IS_DARWIN, test $is_mingw = yes) 70 | 71 | AC_CONFIG_FILES([Makefile src/Makefile]) 72 | AC_OUTPUT 73 | 74 | -------------------------------------------------------------------------------- /m4/check_libusb.m4: -------------------------------------------------------------------------------- 1 | dnl Copied from Libnfc project, modified for libusb-1.0 2 | dnl 3 | dnl Check for LIBUSB-1.0 4 | dnl On success, HAVE_LIBUSB is set to 1 and PKG_CONFIG_REQUIRES is filled when 5 | dnl libusb is found using pkg-config 6 | 7 | AC_DEFUN([OLS_CHECK_LIBUSB], 8 | [ 9 | HAVE_LIBUSB=0 10 | AC_ARG_WITH([libusb], 11 | [AS_HELP_STRING([--with-libusb], [use libusb from the following location])], 12 | [LIBUSB_DIR=$withval], 13 | [LIBUSB_DIR=""]) 14 | 15 | # --with-libusb directory have been set 16 | if test "x$LIBUSB_DIR" != "x"; then 17 | AC_MSG_NOTICE(["use libusb from $LIBUSB_DIR"]) 18 | libusb_CFLAGS="-I$LIBUSB_DIR/include" 19 | libusb_LIBS="-L$LIBUSB_DIR/lib/gcc -lusb-1.0" 20 | HAVE_LIBUSB=1 21 | fi 22 | 23 | # Search using pkg-config 24 | if test x"$HAVE_LIBUSB" = "x0"; then 25 | if test x"$PKG_CONFIG" != "x"; then 26 | PKG_CHECK_MODULES([libusb], [libusb-1.0], [HAVE_LIBUSB=1], [HAVE_LIBUSB=0]) 27 | if test x"$HAVE_LIBUSB" = "x1"; then 28 | if test x"$PKG_CONFIG_REQUIRES" != x""; then 29 | PKG_CONFIG_REQUIRES="$PKG_CONFIG_REQUIRES," 30 | fi 31 | PKG_CONFIG_REQUIRES="$PKG_CONFIG_REQUIRES libusb" 32 | fi 33 | fi 34 | fi 35 | 36 | # Search using libusb-config 37 | if test x"$HAVE_LIBUSB" = "x0"; then 38 | AC_PATH_PROG(libusb_CONFIG,libusb-config) 39 | if test x"$libusb_CONFIG" != "x" ; then 40 | libusb_CFLAGS=`$libusb_CONFIG --cflags` 41 | libusb_LIBS=`$libusb_CONFIG --libs` 42 | 43 | HAVE_LIBUSB=1 44 | fi 45 | fi 46 | 47 | # Search the library and headers directly (last chance) 48 | if test x"$HAVE_LIBUSB" = "x0"; then 49 | AC_CHECK_HEADER(libusb.h, [], [AC_MSG_ERROR([The libusb headers are missing])]) 50 | AC_CHECK_LIB(usb-1.0, libusb_init, [], [AC_MSG_ERROR([The libusb library is missing])]) 51 | 52 | libusb_LIBS="-lusb-1.0" 53 | HAVE_LIBUSB=1 54 | fi 55 | 56 | if test x"$HAVE_LIBUSB" = "x0"; then 57 | AC_MSG_ERROR([libusb-1.0 is mandatory.]) 58 | fi 59 | 60 | AC_SUBST(libusb_LIBS) 61 | AC_SUBST(libusb_CFLAGS) 62 | ]) 63 | -------------------------------------------------------------------------------- /ols-fwloader.cbp: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 64 | 65 | -------------------------------------------------------------------------------- /src/Makefile.am: -------------------------------------------------------------------------------- 1 | bin_PROGRAMS = ols_fwloader 2 | 3 | ols_fwloader_SOURCES = boot_if.h data_file.c data_file.h main.c ols-boot.c ols-boot.h ols.c ols.h serial.c serial.h 4 | 5 | ols_fwloader_CFLAGS = @libusb_CFLAGS@ 6 | ols_fwloader_LDADD = @libusb_LIBS@ @win32_LIBS@ 7 | 8 | -------------------------------------------------------------------------------- /src/boot_if.h: -------------------------------------------------------------------------------- 1 | /*************************************************************************** 2 | * Copyright (C) 2007 by Diolan * 3 | * www.diolan.com * 4 | * * 5 | * This program is free software; you can redistribute it and/or modify * 6 | * it under the terms of the GNU General Public License as published by * 7 | * the Free Software Foundation; either version 2 of the License, or * 8 | * (at your option) any later version. * 9 | * * 10 | * This program is distributed in the hope that it will be useful, * 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * 13 | * GNU General Public License for more details. * 14 | * * 15 | * You should have received a copy of the GNU General Public License * 16 | * along with this program; if not, write to the * 17 | * Free Software Foundation, Inc., * 18 | * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * 19 | ***************************************************************************/ 20 | 21 | #ifndef BOOT_IF_H_INCLUDED 22 | #define BOOT_IF_H_INCLUDED 23 | 24 | /* 25 | *----------------------------------------------------------------------------- 26 | * Constants 27 | *----------------------------------------------------------------------------- 28 | */ 29 | #define BOOT_CMD_SIZE 64 //!!!18f24j50 change 30 | #if( defined HID_OUT_EP_SIZE && (HID_OUT_EP_SIZE 5 | * 6 | * This program is free software: you can redistribute it and/or modify 7 | * it under the terms of the GNU General Public License as published by 8 | * the Free Software Foundation, either version 3 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * This program is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU General Public License 17 | * along with this program. If not, see . 18 | */ 19 | 20 | #include 21 | #include 22 | #include 23 | 24 | #include "data_file.h" 25 | 26 | static uint32_t HEX_ReadFile(const char *file, uint8_t *out_buf, uint32_t out_buf_size); 27 | static int HEX_WriteFile(const char *file, uint8_t *in_buf, uint32_t in_buf_size); 28 | static int HEX_CheckType(const char *); 29 | static uint32_t BIN_ReadFile(const char *file, uint8_t *out_buf, uint32_t out_buf_size); 30 | static int BIN_WriteFile(const char *file, uint8_t *in_buf, uint32_t in_buf_size); 31 | static int BIN_CheckType(const char *); 32 | 33 | // TODO: implement BIT file reading/ writing? 34 | 35 | #define FILE_OPS_CNT (sizeof(file_ops)/sizeof(struct file_ops_t)) 36 | const struct file_ops_t file_ops[] = { 37 | { 38 | .name = "HEX", 39 | .ReadFile = HEX_ReadFile, 40 | .WriteFile = HEX_WriteFile, 41 | .CheckType = HEX_CheckType, 42 | }, 43 | /* { 44 | .name = "BIT", 45 | .ReadFile = BIT_ReadFile, 46 | .WriteFile = BIT_WriteFile, 47 | .CheckType = BIT_CheckType, 48 | },*/ 49 | { 50 | .name = "BIN", 51 | .ReadFile = BIN_ReadFile, 52 | .WriteFile = BIN_WriteFile, 53 | .CheckType = BIN_CheckType, 54 | } 55 | }; 56 | 57 | struct file_ops_t *GetFileOps(char *name) 58 | { 59 | int i; 60 | 61 | for (i = 0; i < FILE_OPS_CNT; i++) { 62 | if (strcasecmp(name, file_ops[i].name) == 0) { 63 | return (struct file_ops_t *)&file_ops[i]; 64 | } 65 | } 66 | 67 | return NULL; 68 | } 69 | 70 | /* 71 | * returns checksum of input buffer 72 | */ 73 | uint8_t Data_Checksum(uint8_t *buf, uint16_t size) 74 | { 75 | uint16_t i; 76 | uint8_t sum = 0; 77 | 78 | for (i=0; i> 8) & 0xff; 224 | bin_line[2] = addr & 0xff; 225 | bin_line[3] = rec_id; 226 | 227 | // copy data 228 | for (i=0; i 0x10) { 271 | byte_count = 0x10; 272 | } else { 273 | byte_count = in_buf_size - written; 274 | } 275 | 276 | // write data record 277 | HEX_WriteRec(fp, 0x00, byte_count, addr, &in_buf[written]); 278 | written += byte_count; 279 | addr += byte_count; 280 | 281 | // write ext addr record 282 | if (addr & 0x10000) { 283 | uint8_t tmp[2]; 284 | 285 | base ++; 286 | 287 | tmp[0] = (base >> 8) & 0xff; 288 | tmp[1] = (base & 0xff); 289 | 290 | HEX_WriteRec(fp, 0x04, 2, 0x0000, tmp); 291 | 292 | addr -= 0x10000; 293 | } 294 | } 295 | 296 | // end record 297 | HEX_WriteRec(fp, 0x01, 0x00, 0x0000, NULL); 298 | 299 | fclose(fp); 300 | return 0; 301 | } 302 | 303 | static int HEX_CheckType(const char *dummy) 304 | { 305 | return 0 ;; //TODO: finish 306 | } 307 | 308 | /* 309 | * reads bin file 310 | * file - name of hexfile 311 | * buf - buffer where the data should be written to 312 | * size - size of buffer, returns actual size read 313 | */ 314 | static uint32_t BIN_ReadFile(const char *file, uint8_t *out_buf, uint32_t out_buf_size) 315 | { 316 | int res; 317 | long fsize; 318 | FILE *fp; 319 | 320 | fp = fopen(file, "rb"); 321 | if (fp == NULL) { 322 | return 0; 323 | } 324 | 325 | fseek(fp, 0, SEEK_END); 326 | fsize = ftell(fp); 327 | fseek(fp, 0, SEEK_SET); 328 | 329 | if (fsize > out_buf_size) { 330 | printf("file won't fit into buffer :(\n"); 331 | return 0; 332 | } 333 | 334 | res = fread(out_buf, sizeof(uint8_t), fsize, fp); 335 | if (res <= 0) { 336 | printf("error reading file %s \n", file); 337 | } 338 | 339 | fclose(fp); 340 | return (res < 0) ? 0 : res; 341 | } 342 | 343 | /* 344 | * writes bin file 345 | * file - name of hexfile 346 | * buf - buffer which contains the data 347 | * size - size of buffer 348 | */ 349 | static int BIN_WriteFile(const char *file, uint8_t *out_buf, uint32_t out_buf_size) 350 | { 351 | FILE *fp; 352 | int res; 353 | 354 | fp = fopen(file, "wb"); 355 | if (fp == NULL) { 356 | return -1; 357 | } 358 | res = fwrite(out_buf, sizeof(uint8_t), out_buf_size, fp); 359 | if (res != out_buf_size) { 360 | printf("error writing file %s\n", file); 361 | } 362 | fclose(fp); 363 | 364 | return 0; 365 | } 366 | 367 | static int BIN_CheckType(const char *dummy) 368 | { 369 | /* always binary */ 370 | return 1; 371 | } 372 | -------------------------------------------------------------------------------- /src/data_file.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Part of ols-fwloader - data file parsing/generaion 3 | * 4 | * Copyright (C) 2011 Michal Demin 5 | * 6 | * This program is free software: you can redistribute it and/or modify 7 | * it under the terms of the GNU General Public License as published by 8 | * the Free Software Foundation, either version 3 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * This program is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU General Public License 17 | * along with this program. If not, see . 18 | */ 19 | 20 | #ifndef DATA_FILE_H_ 21 | #define DATA_FILE_H_ 22 | 23 | #include 24 | struct file_ops_t { 25 | char *name; 26 | 27 | uint32_t (*ReadFile)(const char *, uint8_t *, uint32_t); 28 | int (*WriteFile)(const char *, uint8_t *, uint32_t); 29 | int (*CheckType)(const char *); 30 | }; 31 | 32 | uint8_t Data_Checksum(uint8_t *buf, uint16_t size); 33 | struct file_ops_t *GetFileOps(char *); 34 | 35 | #endif 36 | 37 | -------------------------------------------------------------------------------- /src/main.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Part of ols-fwloader - Main program 3 | * 4 | * Copyright (C) 2011 Michal Demin 5 | * 6 | * This program is free software: you can redistribute it and/or modify 7 | * it under the terms of the GNU General Public License as published by 8 | * the Free Software Foundation, either version 3 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * This program is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU General Public License 17 | * along with this program. If not, see . 18 | */ 19 | 20 | #include 21 | #include 22 | #include 23 | #include 24 | #include 25 | 26 | #include "ols-boot.h" 27 | #include "ols.h" 28 | #include "data_file.h" 29 | 30 | #if IS_WIN32 31 | #define sleep(n) Sleep((1000 * (n))) 32 | #endif 33 | 34 | #define DEFAULT_TYPE "HEX" 35 | enum { 36 | CMD_READ = 1, 37 | CMD_WRITE = 2, 38 | CMD_VERIFY = 4, 39 | CMD_ERASE = 8, 40 | CMD_RESET = 16, 41 | CMD_SELFTEST = 32, 42 | }; 43 | 44 | enum { 45 | DEV_BOOT = 1, 46 | DEV_APP = 2, 47 | DEV_SWITCH = 4, 48 | }; 49 | 50 | static int memverify(uint8_t *ref, uint8_t *mem, uint32_t len, uint32_t offset, int verbose); 51 | 52 | static void usage() 53 | { 54 | printf(PACKAGE_STRING " Copyright (C) 2011 Michal Demin\n"); 55 | printf("This program comes with ABSOLUTELY NO WARRANTY;\n"); 56 | printf("This is free software, and you are welcome to redistribute it\n"); 57 | printf("under certain conditions. See COPYING\n\n"); 58 | 59 | printf("ols-fwloader [-d] [-V] [-W] [-R] [-E] [-r rfile] [-w wfile] [-t type] [-v vid] [-p pid]\n\n"); 60 | printf(" -f dev - select which device to work with (BOOT or APP)\n"); 61 | printf(" -V - verify flash against wfile\n"); 62 | printf(" -E - erase flash\n"); 63 | printf(" -W - erase and write flash from wfile\n"); 64 | printf(" -R - read flash to rfile\n"); 65 | printf(" -T - reset device at the end\n\n"); 66 | printf(" -t type - File type (BIN/HEX) (default: " DEFAULT_TYPE ")\n"); 67 | printf(" -w file - file to be read and written to flash\n"); 68 | printf(" -r file - file where the flash content should be written to\n"); 69 | printf(" -d - be verbose\n"); 70 | 71 | printf("BOOT only options: \n"); 72 | printf(" -p pid - Set usb PID (default: 0x%04x)\n", OLS_PID); 73 | printf(" -v vid - Set usb VID (default: 0x%04x)\n", OLS_VID); 74 | printf(" -n - enter bootloader first\n"); 75 | 76 | printf("APP only options: \n"); 77 | printf(" -P port - Serial port device\n"); 78 | printf(" -l num - Limit number of read/written pages to num\n"); 79 | printf(" -S - run selftest\n"); 80 | printf("\n"); 81 | } 82 | 83 | int main(int argc, char** argv) 84 | { 85 | struct ols_boot_t *ob; 86 | struct ols_t *ols; 87 | 88 | uint8_t *bin_buf; 89 | uint8_t *bin_buf_tmp; 90 | uint32_t bin_buf_size; 91 | 92 | uint16_t vid = OLS_VID; 93 | uint16_t pid = OLS_PID; 94 | 95 | int error = 0; 96 | int ret; 97 | int i; 98 | uint16_t pages; 99 | int debug = 0; 100 | 101 | // aguments 102 | struct file_ops_t *fo; 103 | char *file_write = NULL; 104 | char *file_read = NULL; 105 | uint8_t cmd = 0; 106 | char *port = NULL; 107 | uint8_t device = 0; 108 | uint16_t page_limit = 0; 109 | uint32_t max_addr = 0; 110 | 111 | // getopt 112 | int opt; 113 | 114 | fo = GetFileOps("HEX"); 115 | 116 | // parse args 117 | while ((opt = getopt(argc, argv, "WRVETSnr:w:v:p:t:P:f:hd")) != -1) { 118 | switch (opt) { 119 | case 'd': 120 | debug = 1; 121 | break; 122 | case 'h': 123 | usage(); 124 | exit(1); 125 | break; 126 | case 'R': 127 | cmd |= CMD_READ; 128 | break; 129 | case 'W': 130 | cmd |= CMD_WRITE; 131 | break; 132 | case 'E': 133 | cmd |= CMD_ERASE; 134 | break; 135 | case 'V': 136 | cmd |= CMD_VERIFY; 137 | break; 138 | case 'T': 139 | cmd |= CMD_RESET; 140 | break; 141 | case 'S': 142 | cmd |= CMD_SELFTEST; 143 | break; 144 | case 'l': 145 | page_limit = atoi(optarg); 146 | break; 147 | case 'v': // vid 148 | vid = (uint16_t)strtol(optarg, NULL, 0); 149 | break; 150 | case 'p': // pid 151 | pid = (uint16_t)strtol(optarg, NULL, 0); 152 | break; 153 | case 'P': 154 | if (port != NULL) { 155 | fprintf(stderr, "Two ports ?!\n"); 156 | exit(-1); 157 | } 158 | port = strdup(optarg); 159 | break; 160 | case 'r': // readfile 161 | if (file_read != NULL) { 162 | fprintf(stderr, "Two files?\n"); 163 | exit(-1); 164 | } 165 | file_read = strdup(optarg); 166 | break; 167 | case 'w': // readfile 168 | if (file_write != NULL) { 169 | fprintf(stderr, "Two files?\n"); 170 | exit(-1); 171 | } 172 | file_write = strdup(optarg); 173 | break; 174 | case 't': 175 | fo = GetFileOps(optarg); 176 | if (fo == NULL) { 177 | fprintf(stderr, "Unknown type \n"); 178 | exit(-1); 179 | } 180 | break; 181 | case 'n': 182 | device |= DEV_SWITCH; 183 | break; 184 | case 'f': 185 | if (device & (DEV_APP | DEV_BOOT)) { 186 | fprintf(stderr, "Two devices ??\n"); 187 | exit(-1); 188 | } 189 | if (strncasecmp(optarg, "APP", 3) == 0) { 190 | device |= DEV_APP; 191 | } else if (strncasecmp(optarg, "BOOT", 4) == 0) { 192 | device |= DEV_BOOT; 193 | } else { 194 | fprintf(stderr, "Unknown device specified\n"); 195 | exit(-1); 196 | } 197 | break; 198 | } 199 | } 200 | 201 | // check parameters 202 | if ((cmd & CMD_READ) && (file_read == NULL)) { 203 | fprintf(stderr, "Read command but no file ? \n"); 204 | error = 1; 205 | } 206 | 207 | if ((cmd & CMD_WRITE) && (file_write == NULL)) { 208 | fprintf(stderr, "Write command but no file ? \n"); 209 | error = 1; 210 | } 211 | 212 | if ((cmd & CMD_VERIFY) && (file_write == NULL)) { 213 | fprintf(stderr, "Verify command but no file ? \n"); 214 | error = 1; 215 | } 216 | 217 | if ((device & DEV_APP) || device & DEV_SWITCH) { 218 | if (port == NULL) { 219 | fprintf(stderr, "Missing serial port \n"); 220 | error = 1; 221 | } 222 | } 223 | 224 | if ((device & 3) == 0) { 225 | fprintf(stderr, "No device specified\n"); 226 | error = 1; 227 | } 228 | 229 | /* 230 | if (cmd == 0) { 231 | fprintf(stderr, "Missing command\n"); 232 | error = 1; 233 | } 234 | */ 235 | 236 | if ((error)) { 237 | usage(); 238 | exit(1); 239 | } 240 | 241 | // execute commands 242 | // Working with APP or switch to bootloader first 243 | if ((device & DEV_APP) || (device & DEV_SWITCH)) { 244 | ols = OLS_Init(port, 921600); 245 | 246 | if (ols == NULL) { 247 | fprintf(stderr, "Unable to initialise OLS\n"); 248 | exit(-1); 249 | } 250 | 251 | if (debug) { 252 | ols->verbose = 1; 253 | } 254 | 255 | bin_buf_size = ols->flash->pages * ols->flash->page_size; 256 | if (device & DEV_SWITCH) { 257 | if (device & DEV_BOOT) { 258 | OLS_EnterBootloader(ols); 259 | OLS_Deinit(ols); 260 | 261 | // wait for device to appear 262 | sleep(2); 263 | } else { 264 | fprintf(stderr, "Not switching to bootloader.\n"); 265 | device &= ~DEV_SWITCH; 266 | } 267 | } 268 | } 269 | 270 | // Initialize bootloader 271 | if (device & DEV_BOOT) { 272 | ob = BOOT_Init(vid, pid, debug); 273 | if (ob == NULL) { 274 | exit(1); 275 | } 276 | 277 | // 2 times, first might fail for reason unknown 278 | for (i = 0; i < 2; i++) { 279 | ret = BOOT_Version(ob); 280 | if (ret) { 281 | exit(1); 282 | } 283 | } 284 | 285 | bin_buf_size = OLS_FLASH_TOTSIZE; 286 | } 287 | 288 | // allocate buffers 289 | bin_buf = malloc(bin_buf_size); 290 | bin_buf_tmp = malloc(bin_buf_size); 291 | if ((bin_buf == NULL) || (bin_buf_tmp == NULL)) { 292 | fprintf(stderr, "Error allocating memory \n"); 293 | exit(1); 294 | } 295 | 296 | if (cmd & CMD_SELFTEST) { 297 | if (device & DEV_APP) { 298 | ret = OLS_RunSelftest(ols); 299 | if (ret) { 300 | exit(1); 301 | } 302 | } 303 | } 304 | 305 | if (cmd & CMD_READ) { 306 | printf("Reading flash \n"); 307 | memset(bin_buf, 0xff, bin_buf_size); 308 | 309 | if (device & DEV_APP) { 310 | int i; 311 | if (page_limit != 0) { 312 | pages = page_limit; 313 | } else { 314 | pages = ols->flash->pages; 315 | } 316 | pages = (pages > ols->flash->pages) ? ols->flash->pages : pages; 317 | for (i = 0; i < pages; i ++) { 318 | // read i-th page into buffer 319 | ret = OLS_FlashRead(ols, i, bin_buf + (ols->flash->page_size * i)); 320 | if (ret) { 321 | exit(1); 322 | } 323 | if ((i % 32) == 0) { 324 | printf("."); 325 | fflush(stdout); 326 | } 327 | } 328 | printf("\n"); 329 | } else { 330 | // reads whole flash (inc bootloader) 331 | ret = BOOT_Read(ob, 0x0000, bin_buf, bin_buf_size); 332 | if (ret) { 333 | exit(1); 334 | } 335 | } 336 | printf("Writing file '%s'\n", file_read); 337 | fo->WriteFile(file_read, bin_buf, bin_buf_size); 338 | } 339 | 340 | // JaWi: first read the entire data file before going to erase/write stuff. 341 | // This way, we're fairly sure we can leave the device in a workable state 342 | if (cmd & CMD_WRITE) { 343 | printf("Reading file '%s'\n", file_write); 344 | memset(bin_buf_tmp, 0xff, bin_buf_size); 345 | 346 | max_addr = fo->ReadFile(file_write, bin_buf, bin_buf_size); 347 | if (max_addr == 0) { 348 | // error reading 349 | fprintf(stderr, "Error reading file - skipping write\n"); 350 | exit(1); 351 | } 352 | } 353 | 354 | // writing implies erase 355 | if ((cmd & CMD_ERASE) || (cmd & CMD_WRITE)) { 356 | printf("Erasing flash ...\n"); 357 | if (device & DEV_APP) { 358 | // erase spi flash, bulk erase 359 | ret = OLS_FlashErase(ols); 360 | if (ret) { 361 | exit(1); 362 | } 363 | } else { 364 | // erases flash (this is done internally by bootloader) 365 | ret = BOOT_Erase(ob); 366 | if (ret) { 367 | exit(1); 368 | } 369 | } 370 | } 371 | 372 | if (cmd & CMD_WRITE) { 373 | if ((max_addr != 0) && (device & DEV_APP)) { 374 | int i; 375 | // determine number of pages (round up) 376 | if (page_limit != 0) { 377 | pages = page_limit; 378 | } else { 379 | pages = (max_addr + ols->flash->page_size - 1) / ols->flash->page_size; 380 | } 381 | pages = (pages > ols->flash->pages) ? ols->flash->pages : pages; 382 | printf("Will write %d pages \n", pages); 383 | for (i = 0; i < pages; i ++) { 384 | ret = OLS_FlashWrite(ols, i, bin_buf + (ols->flash->page_size * i)); 385 | if (ret) { 386 | exit(1); 387 | } 388 | if ((i % 32) == 0) { 389 | printf("."); 390 | fflush(stdout); 391 | } 392 | } 393 | printf("\n"); 394 | } else if ((max_addr != 0) && (device & DEV_BOOT)) { 395 | int size; 396 | // we write only application 397 | size = ((max_addr - OLS_FLASH_ADDR) > OLS_FLASH_SIZE)? OLS_FLASH_SIZE : max_addr - OLS_FLASH_ADDR; 398 | printf("Writing flash ... (0x%04x - 0x%04x) \n", OLS_FLASH_ADDR, size + OLS_FLASH_ADDR); 399 | ret = BOOT_Write(ob, OLS_FLASH_ADDR, &bin_buf[OLS_FLASH_ADDR], size); 400 | if (ret) { 401 | exit(1); 402 | } 403 | } 404 | } 405 | 406 | if (cmd & CMD_VERIFY) { 407 | // read the whole flash 408 | memset(bin_buf_tmp, 0xff, bin_buf_size); 409 | memset(bin_buf, 0xff, bin_buf_size); 410 | 411 | max_addr = fo->ReadFile(file_write, bin_buf_tmp, bin_buf_size); 412 | if (max_addr == 0) { 413 | // error reading 414 | fprintf(stderr, "Error reading file - skipping verify\n"); 415 | } 416 | 417 | if ((max_addr != 0) && (device & DEV_APP)) { 418 | int i; 419 | 420 | printf("Checking flash ...\n"); 421 | 422 | if (page_limit != 0) { 423 | pages = page_limit; 424 | } else { 425 | pages = (max_addr + ols->flash->page_size - 1) / ols->flash->page_size; 426 | } 427 | pages = (pages > ols->flash->pages) ? ols->flash->pages : pages; 428 | for (i = 0; i < pages; i ++) { 429 | uint32_t off; 430 | off = ols->flash->page_size * i; 431 | // read i-th page into buffer 432 | ret = OLS_FlashRead(ols, i, bin_buf + off); 433 | if (ret) { 434 | exit(1); 435 | } 436 | if ((i % 32) == 0) { 437 | printf("."); 438 | fflush(stdout); 439 | } 440 | } 441 | printf("\n"); 442 | // compare page by page 443 | if (memverify(bin_buf_tmp, bin_buf, max_addr, 0, debug)) { 444 | printf("Verify error\n"); 445 | } else { 446 | printf("Verify OK\n"); 447 | } 448 | } else if ((max_addr != 0) && (device & DEV_BOOT)) { 449 | int size; 450 | ret = BOOT_Read(ob, 0x0000, bin_buf, OLS_FLASH_TOTSIZE); 451 | if (ret) { 452 | exit(1); 453 | } 454 | // compare only application 455 | size = ((max_addr - OLS_FLASH_ADDR) > OLS_FLASH_SIZE)? OLS_FLASH_SIZE : max_addr - OLS_FLASH_ADDR; 456 | printf("Checking flash ... (0x%04x - 0x%04x)\n", OLS_FLASH_ADDR, size + OLS_FLASH_ADDR); 457 | 458 | if (memverify(&bin_buf_tmp[OLS_FLASH_ADDR], &bin_buf[OLS_FLASH_ADDR], size, OLS_FLASH_ADDR, debug) == 0) { 459 | printf("Verified OK! :)\n"); 460 | } else { 461 | printf("Verify failed :(\n"); 462 | } 463 | } 464 | } 465 | 466 | if (cmd & CMD_RESET) { 467 | if (device & DEV_APP) { 468 | printf("Reseting to normal mode \n"); 469 | OLS_EnterRunMode(ols); 470 | } else { 471 | printf("Reseting device \n"); 472 | BOOT_Reset(ob); 473 | } 474 | } 475 | 476 | if (device & DEV_APP) { 477 | OLS_Deinit(ols); 478 | } else { 479 | BOOT_Deinit(ob); 480 | } 481 | 482 | // free allocated memory 483 | free(bin_buf_tmp); 484 | free(bin_buf); 485 | 486 | return 0; 487 | } 488 | 489 | static int memverify(uint8_t *ref, uint8_t *mem, uint32_t len, uint32_t offset, int verbose) 490 | { 491 | int ret = 0; 492 | uint32_t i; 493 | 494 | for (i = 0; i < len; i++) { 495 | if (ref[i] != mem[i]) { 496 | if (verbose) 497 | printf("Diff @0x%04x (Is 0x%02x should be 0x%02x)\n", offset + i, mem[i], ref[i]); 498 | ret = 1; 499 | } 500 | } 501 | 502 | return ret; 503 | } 504 | 505 | -------------------------------------------------------------------------------- /src/ols-boot.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Part of ols-fwloader - Diolan bootloader interface 3 | * 4 | * Copyright (C) 2011 Michal Demin 5 | * 6 | * This program is free software: you can redistribute it and/or modify 7 | * it under the terms of the GNU General Public License as published by 8 | * the Free Software Foundation, either version 3 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * This program is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU General Public License 17 | * along with this program. If not, see . 18 | */ 19 | 20 | #include 21 | #include 22 | #include 23 | #include 24 | #include 25 | 26 | #include "boot_if.h" 27 | #include "ols-boot.h" 28 | 29 | struct ols_boot_t *BOOT_Init(uint16_t vid, uint16_t pid, int debug) 30 | { 31 | #if IS_WIN32 32 | GUID HidGuid; 33 | HDEVINFO hDevInfo; 34 | SP_DEVICE_INTERFACE_DATA DevInterfaceData; 35 | unsigned long DevIndex = 0; 36 | unsigned long DetailsSize; 37 | PSP_INTERFACE_DEVICE_DETAIL_DATA pDetails; 38 | HANDLE hHidDevice; 39 | HIDD_ATTRIBUTES Attr; 40 | #endif 41 | 42 | struct ols_boot_t *ob; 43 | int ret; 44 | 45 | ob = malloc(sizeof(struct ols_boot_t)); 46 | if (ob == NULL) { 47 | fprintf(stderr, "Not enough memory \n"); 48 | return NULL; 49 | } 50 | memset(ob, 0, sizeof(struct ols_boot_t)); 51 | 52 | #if IS_WIN32 53 | HidD_GetHidGuid( &HidGuid); 54 | hDevInfo = SetupDiGetClassDevs(&HidGuid, NULL, NULL, DIGCF_DEVICEINTERFACE | DIGCF_PRESENT); 55 | if (hDevInfo == INVALID_HANDLE_VALUE) 56 | { 57 | fprintf(stderr, "INVALID_HANDLE_VALUE\n"); 58 | return NULL; 59 | } 60 | DevInterfaceData.cbSize = sizeof(SP_DEVICE_INTERFACE_DATA); 61 | 62 | // iterate thgough all HID devices 63 | // this part is inspirated by Diolan's fw_update 64 | while(1) 65 | { 66 | int bad = 0; 67 | 68 | if (!SetupDiEnumDeviceInterfaces(hDevInfo, NULL, &HidGuid, DevIndex, &DevInterfaceData)) { 69 | fprintf(stderr, "Device does not exist\n"); 70 | return NULL; 71 | } 72 | 73 | SetupDiGetDeviceInterfaceDetail(hDevInfo, &DevInterfaceData, NULL, 0, &DetailsSize, NULL); 74 | pDetails = malloc(DetailsSize); 75 | if (pDetails == NULL) 76 | { 77 | SetupDiDestroyDeviceInfoList(hDevInfo); 78 | fprintf(stderr, "Not enough memory \n"); 79 | return NULL; 80 | } 81 | 82 | pDetails->cbSize = sizeof(SP_INTERFACE_DEVICE_DETAIL_DATA); 83 | if (!SetupDiGetDeviceInterfaceDetail(hDevInfo, &DevInterfaceData, pDetails, DetailsSize, NULL, NULL)) 84 | { 85 | free(pDetails); 86 | SetupDiDestroyDeviceInfoList(hDevInfo); 87 | fprintf(stderr, "SetupDiGetDeviceInterfaceDetail failed\n"); 88 | return NULL; 89 | } 90 | 91 | hHidDevice = CreateFile(pDetails->DevicePath, GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, 92 | NULL, OPEN_EXISTING, FILE_FLAG_OVERLAPPED, NULL); 93 | 94 | if (hHidDevice == INVALID_HANDLE_VALUE) { 95 | bad = 1; 96 | } 97 | 98 | if ((bad == 0) && (!HidD_GetAttributes (hHidDevice, &Attr))) { 99 | bad = 2; 100 | } 101 | 102 | if ((bad == 0) && (Attr.VendorID == vid) && (Attr.ProductID == pid)) { 103 | ob->hDevice = hHidDevice; 104 | free(pDetails); 105 | break; 106 | } 107 | 108 | if (bad > 1) { 109 | CloseHandle(hHidDevice); 110 | } 111 | 112 | free(pDetails); 113 | DevIndex++; 114 | } 115 | SetupDiDestroyDeviceInfoList(hDevInfo); 116 | #else 117 | ret = libusb_init(&ob->ctx); 118 | if (ret != 0) { 119 | fprintf(stderr, "libusb_init problem\n"); 120 | } 121 | 122 | if (debug) { 123 | libusb_set_debug(ob->ctx, 4); 124 | } 125 | 126 | ob->dev = libusb_open_device_with_vid_pid(ob->ctx, vid, pid); 127 | if (ob->dev == NULL) { 128 | fprintf(stderr, "USB Device (%04x:%04x) not found, is OLS in bootloader mode ?\n", vid, pid); 129 | free(ob); 130 | return NULL; 131 | } 132 | 133 | if (libusb_kernel_driver_active(ob->dev, 0)) { 134 | // reattach later 135 | ob->attach = 1; 136 | if (libusb_detach_kernel_driver(ob->dev, 0)) { 137 | fprintf(stderr, "Error detaching kernel driver \n"); 138 | free(ob); 139 | return NULL; 140 | } 141 | } 142 | 143 | ret = libusb_claim_interface(ob->dev, 0); 144 | if (ret != 0) { 145 | fprintf(stderr, "Cannot claim USB device\n"); 146 | } 147 | 148 | ret = libusb_set_interface_alt_setting(ob->dev, 0, 0); 149 | if (ret != 0) { 150 | fprintf(stderr, "Unable to set alternative interface \n"); 151 | } 152 | #endif 153 | 154 | return ob; 155 | } 156 | 157 | static uint8_t BOOT_Recv(struct ols_boot_t *ob, boot_rsp *rsp) 158 | { 159 | #if IS_WIN32 160 | OVERLAPPED read_over; 161 | unsigned long readed; 162 | unsigned char read_buf[sizeof(boot_rsp)+1]; 163 | unsigned long res; 164 | 165 | memset(&read_over, 0, sizeof(OVERLAPPED)); 166 | 167 | read_over.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL); 168 | 169 | if ((!ReadFile(ob->hDevice, read_buf, sizeof(boot_rsp)+1, &readed, &read_over)) && (GetLastError() != ERROR_IO_PENDING)) { 170 | fprintf(stderr, "LastError = %d\n", GetLastError()); 171 | return 3; 172 | } 173 | 174 | if (WAIT_TIMEOUT == (res = WaitForSingleObject(read_over.hEvent, 5000))) 175 | { 176 | fprintf(stderr, "Write timeout\n"); 177 | return 2; 178 | } 179 | 180 | if ((!GetOverlappedResult(ob->hDevice, &read_over, &readed, FALSE)) || (readed != sizeof(boot_rsp) + 1)) { 181 | fprintf(stderr, "LastError = %d, readed = %d\n", GetLastError(), readed); 182 | return 1; 183 | } 184 | 185 | memcpy(rsp, read_buf + 1, sizeof(boot_rsp)); 186 | CloseHandle(read_over.hEvent); 187 | 188 | return 0; 189 | #else 190 | int ret; 191 | int len; 192 | 193 | memset (rsp, 0, sizeof(boot_rsp)); 194 | ret = libusb_interrupt_transfer(ob->dev, 0x81, (uint8_t *)rsp, sizeof(boot_rsp), &len, OLS_TIMEOUT); 195 | if ((ret == 0) && (len == sizeof(boot_rsp))) { 196 | return 0; 197 | } else if ((ret == 0) && (len != sizeof(boot_rsp))) { 198 | fprintf(stderr, "Transfered too little (%d)\n", len); 199 | return 1; 200 | } else if (ret == LIBUSB_ERROR_TIMEOUT) { 201 | fprintf(stderr, "Com timeout\n"); 202 | return 2; 203 | } else if (ret == LIBUSB_ERROR_PIPE) { 204 | fprintf(stderr, "Error sending, not ols ?\n"); 205 | return 3; 206 | } else if (ret == LIBUSB_ERROR_NO_DEVICE) { 207 | fprintf(stderr, "Device disconnected \n"); 208 | return 4; 209 | } 210 | fprintf(stderr, "Other error - recv \n"); 211 | return 5; 212 | #endif 213 | } 214 | 215 | static uint8_t BOOT_Send(struct ols_boot_t *ob, boot_cmd *cmd) 216 | { 217 | #if IS_WIN32 218 | unsigned char write_buf[sizeof(boot_cmd)+1]; 219 | unsigned long written; 220 | OVERLAPPED write_over; 221 | BOOL write_res; 222 | 223 | memset(&write_over, 0, sizeof(OVERLAPPED)); 224 | write_over.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL); 225 | 226 | write_buf[0] = 0; 227 | memcpy(write_buf + 1, cmd, sizeof(boot_cmd)); 228 | 229 | write_res = WriteFile(ob->hDevice, write_buf, sizeof(boot_cmd) + 1, &written, &write_over); 230 | if ((!write_res) && (GetLastError() != ERROR_IO_PENDING)) { 231 | fprintf(stderr, "LastError = %d\n", GetLastError()); 232 | return 2; 233 | } 234 | 235 | if (WaitForSingleObject(write_over.hEvent, OLS_TIMEOUT) == WAIT_TIMEOUT) { 236 | fprintf(stderr, "Write timeout"); 237 | return 1; 238 | } 239 | 240 | CloseHandle(write_over.hEvent); 241 | 242 | return 0; 243 | #else 244 | int ret; 245 | 246 | ret = libusb_control_transfer(ob->dev, 0x21, 0x09, 0x0000, 0x0000, (uint8_t *)cmd, sizeof(boot_cmd), OLS_TIMEOUT); 247 | if (ret == sizeof(boot_cmd)) { 248 | return 0; 249 | } else if (ret == LIBUSB_ERROR_TIMEOUT) { 250 | fprintf(stderr, "Com timeout\n"); 251 | return 2; 252 | } else if (ret == LIBUSB_ERROR_PIPE) { 253 | fprintf(stderr, "Error sending, not ols ?\n"); 254 | return 3; 255 | } else if (ret == LIBUSB_ERROR_NO_DEVICE) { 256 | fprintf(stderr, "Device disconnected \n"); 257 | return 4; 258 | } 259 | 260 | fprintf(stderr, "Other error \n"); 261 | 262 | return 5; 263 | #endif 264 | } 265 | 266 | static uint8_t BOOT_SendRecv(struct ols_boot_t *ob, boot_cmd *cmd, boot_rsp *rsp) 267 | { 268 | int ret; 269 | 270 | ret = BOOT_Send(ob, cmd); 271 | 272 | if (ret != 0) 273 | return ret; 274 | 275 | ret = BOOT_Recv(ob, rsp); 276 | 277 | if (ret != 0) 278 | return ret; 279 | 280 | // check echo byte 281 | if (cmd->header.echo != rsp->header.echo) { 282 | fprintf(stderr, "Id doesn't match. Bootloader error\n"); 283 | return 1; 284 | } 285 | return 0; 286 | } 287 | 288 | uint8_t BOOT_Version(struct ols_boot_t *ob) 289 | { 290 | boot_cmd cmd; 291 | boot_rsp rsp; 292 | int ret; 293 | 294 | memset(&cmd, 0, sizeof(cmd)); 295 | 296 | cmd.header.cmd = BOOT_GET_FW_VER; 297 | 298 | ret = BOOT_SendRecv(ob, &cmd, &rsp); 299 | 300 | if (ret) { 301 | return 1; 302 | } 303 | 304 | printf("Bootloader version %d.%d.%d\n", rsp.get_fw_ver.major, 305 | rsp.get_fw_ver.minor, rsp.get_fw_ver.sub_minor); 306 | 307 | return 0; 308 | } 309 | 310 | uint8_t BOOT_Read(struct ols_boot_t *ob, uint16_t addr, uint8_t *buf, uint16_t size) 311 | { 312 | // todo loop 313 | boot_cmd cmd; 314 | boot_rsp rsp; 315 | uint16_t address = 0; 316 | uint16_t len; 317 | int ret; 318 | 319 | memset(&cmd, 0, sizeof(cmd)); 320 | 321 | while (size > 0) { 322 | len = (size > OLS_READ_SIZE) ? OLS_READ_SIZE : size; 323 | cmd.header.cmd = BOOT_READ_FLASH; 324 | cmd.header.echo = ob->cmd_id ++; 325 | 326 | cmd.read_flash.addr_hi = (address >> 8) & 0xff; 327 | cmd.read_flash.addr_lo = address & 0xff; 328 | cmd.read_flash.size8 = (len%2)?len+1:len; // round if odd 329 | 330 | ret = BOOT_SendRecv(ob, &cmd, &rsp); 331 | if (ret != 0) { 332 | fprintf(stderr, "Error reading memory\n"); 333 | return 1; 334 | } 335 | memcpy(buf, rsp.read_flash.data, len); 336 | buf += len; 337 | size -= len; 338 | address += len; 339 | } 340 | return 0; 341 | } 342 | 343 | uint8_t BOOT_Write(struct ols_boot_t *ob, uint16_t addr, uint8_t *buf, uint16_t size_in) 344 | { 345 | // todo loop 346 | boot_cmd cmd; 347 | boot_rsp rsp; 348 | uint16_t address = 0; 349 | uint16_t len; 350 | int32_t size = size_in; 351 | int flush = 1; 352 | int ret; 353 | 354 | address = addr; 355 | 356 | while (size > 0) { 357 | memset(&cmd, 0, sizeof(cmd)); 358 | #if OLS_PAGE_SIZE == 2 359 | len = (size > OLS_PAGE_SIZE) ? OLS_PAGE_SIZE : size; 360 | len = (len % 2) ? len + 1: len; // round odd 361 | 362 | memcpy(cmd.write_flash.data, buf, len); 363 | 364 | // command bootloader to only write 2 bytes 365 | cmd.write_flash.flush = OLS_WRITE_2BYTE | OLS_WRITE_FLUSH; 366 | #elif OLS_PAGE_SIZE == 64 367 | flush ^= 1; // toggle flush 368 | 369 | len = (size > (OLS_PAGE_SIZE/2)) ? OLS_PAGE_SIZE/2 : size; 370 | 371 | memset(cmd.write_flash.data, 0xff, sizeof(cmd.write_flash.data)); 372 | memcpy(cmd.write_flash.data, buf, len); 373 | 374 | // in this mode we always write 32 bytes 375 | // rest is padded with 0xff 376 | len = OLS_PAGE_SIZE/2; 377 | 378 | // command bootloader to write 64bytes 379 | cmd.write_flash.flush = flush & OLS_WRITE_FLUSH; 380 | #else 381 | #error "Unsupported page size" 382 | #endif 383 | 384 | cmd.header.cmd = BOOT_WRITE_FLASH; 385 | cmd.header.echo = ob->cmd_id ++; 386 | 387 | cmd.write_flash.addr_hi = (address >> 8) & 0xff; 388 | cmd.write_flash.addr_lo = address & 0xff; 389 | cmd.write_flash.size8 = len; 390 | 391 | if (address < OLS_FLASH_ADDR) { 392 | fprintf(stderr, "Protecting bootloader - skip @0x%04x\n", address); 393 | } if (address + len > OLS_FLASH_ADDR + OLS_FLASH_SIZE) { 394 | fprintf(stderr, "Protecting bootloader - skip @0x%04x\n", address); 395 | // we end 396 | break; 397 | } else { 398 | ret = BOOT_SendRecv(ob, &cmd, &rsp); 399 | } 400 | if (ret != 0) { 401 | fprintf(stderr, "Error writing memory\n"); 402 | return 1; 403 | } 404 | 405 | buf += len; 406 | size -= len; 407 | address += len; 408 | } 409 | return 0; 410 | } 411 | 412 | uint8_t BOOT_Erase(struct ols_boot_t *ob) 413 | { 414 | boot_cmd cmd; 415 | boot_rsp rsp; 416 | uint16_t address = 0; 417 | int ret; 418 | 419 | memset(&cmd, 0, sizeof(cmd)); 420 | 421 | cmd.header.cmd = BOOT_ERASE_FLASH; 422 | cmd.header.echo = ob->cmd_id ++; 423 | 424 | cmd.erase_flash.addr_hi = 0x08;//(address >> 8) & 0xff; 425 | cmd.erase_flash.addr_lo = 0x00;//address & 0xff; 426 | cmd.erase_flash.size_x64 = 0x0d;//64; 427 | 428 | ret = BOOT_SendRecv(ob, &cmd, &rsp); 429 | if (ret != 0) { 430 | fprintf(stderr, "Error erasing memory\n"); 431 | } 432 | return ret; 433 | } 434 | 435 | uint8_t BOOT_Reset(struct ols_boot_t *ob) 436 | { 437 | boot_cmd cmd; 438 | 439 | memset(&cmd, 0, sizeof(cmd)); 440 | 441 | cmd.header.cmd = BOOT_RESET; 442 | BOOT_Send(ob, &cmd); 443 | 444 | // device wont exist after reset 445 | ob->attach = 0; 446 | } 447 | 448 | 449 | void BOOT_Deinit(struct ols_boot_t *ob) 450 | { 451 | #if IS_WIN32 452 | CloseHandle(ob->hDevice); 453 | ob->hDevice = INVALID_HANDLE_VALUE; 454 | #else 455 | libusb_release_interface(ob->dev, 0); 456 | 457 | if (ob->attach) { 458 | if (libusb_attach_kernel_driver(ob->dev, 0)) { 459 | fprintf(stderr, "Unable to reattach kernel driver\n"); 460 | } 461 | } 462 | 463 | libusb_close(ob->dev); 464 | libusb_exit(ob->ctx); 465 | 466 | ob->dev = NULL; 467 | ob->ctx = NULL; 468 | #endif 469 | } 470 | 471 | -------------------------------------------------------------------------------- /src/ols-boot.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Part of ols-fwloader - Diolan bootloader interface 3 | * 4 | * Copyright (C) 2011 Michal Demin 5 | * 6 | * This program is free software: you can redistribute it and/or modify 7 | * it under the terms of the GNU General Public License as published by 8 | * the Free Software Foundation, either version 3 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * This program is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU General Public License 17 | * along with this program. If not, see . 18 | */ 19 | 20 | #ifndef BOOT_h_ 21 | #define BOOT_h_ 22 | 23 | #include 24 | #include 25 | #if IS_WIN32 26 | #include 27 | #include 28 | #include 29 | #include 30 | #else 31 | #include 32 | #endif 33 | 34 | #define OLS_VID 0x04d8 35 | #define OLS_PID 0xfc90 36 | 37 | #define OLS_TIMEOUT 1000 38 | #define OLS_PAGE_SIZE 64 39 | #define OLS_READ_SIZE (sizeof(rsp.read_flash.data)) 40 | 41 | #define OLS_FLASH_SIZE 0x3400 // 16*0x400 - 3*0x400 42 | #define OLS_FLASH_ADDR 0x0800 // protect bootloader 43 | 44 | #define OLS_FLASH_TOTSIZE 0x4000 45 | 46 | enum { 47 | OLS_WRITE_FLUSH = 1, 48 | OLS_WRITE_2BYTE = 2, 49 | }; 50 | 51 | struct ols_boot_t { 52 | #if IS_WIN32 53 | HANDLE hDevice; 54 | #else 55 | libusb_context *ctx; 56 | libusb_device_handle *dev; 57 | #endif 58 | int attach; 59 | 60 | uint8_t cmd_id; 61 | }; 62 | 63 | struct ols_boot_t *BOOT_Init(uint16_t vid, uint16_t pid, int debug); 64 | uint8_t BOOT_Version(struct ols_boot_t *ob); 65 | uint8_t BOOT_Read(struct ols_boot_t *ob, uint16_t addr, uint8_t *buf, uint16_t size); 66 | uint8_t BOOT_Write(struct ols_boot_t *ob, uint16_t addr, uint8_t *buf, uint16_t size); 67 | uint8_t BOOT_Erase(struct ols_boot_t *ob); 68 | uint8_t BOOT_Reset(struct ols_boot_t *ob); 69 | void BOOT_Deinit(struct ols_boot_t *ob); 70 | #endif 71 | 72 | -------------------------------------------------------------------------------- /src/ols.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Part of ols-fwloader - Open Bench Logic Sniffer update interface 3 | * 4 | * Copyright (C) 2011 Michal Demin 5 | * 6 | * This program is free software: you can redistribute it and/or modify 7 | * it under the terms of the GNU General Public License as published by 8 | * the Free Software Foundation, either version 3 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * This program is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU General Public License 17 | * along with this program. If not, see . 18 | */ 19 | 20 | #include 21 | #include 22 | #include 23 | #include 24 | 25 | #include "data_file.h" 26 | #include "serial.h" 27 | #include "ols.h" 28 | 29 | const struct ols_flash_t OLS_Flash[] = { 30 | { 31 | "\x1f\x24\x00\x00", 32 | 264, // size of page 33 | 2048, // number of pages 34 | "ATMEL AT45DB041D" 35 | }, 36 | { 37 | "\x1f\x23\x00\x00", 38 | 264, // size of page 39 | 1024, // number of pages 40 | "ATMEL AT45DB021D" 41 | }, 42 | { 43 | "\xef\x30\x12\x00", 44 | 256, // size of page 45 | 1024, // number of pages 46 | "WINBOND W25X20" 47 | }, 48 | { 49 | "\xef\x30\x13\x00", 50 | 256, // size of page 51 | 2048, // number of pages 52 | "WINBOND W25X40" 53 | }, 54 | { 55 | "\xef\x30\x14\x00", 56 | 256, // size of page 57 | 4096, // number of pages 58 | "WINBOND W25X80" 59 | }, 60 | { 61 | "\xef\x40\x14\x00", 62 | 256, // size of page 63 | 4096, // number of pages 64 | "WINBOND W25Q80" 65 | }, 66 | }; 67 | 68 | #define OLS_FLASH_NUM (sizeof(OLS_Flash)/sizeof(struct ols_flash_t)) 69 | 70 | struct ols_t *OLS_Init(char *port, unsigned long speed) 71 | { 72 | int fd; 73 | int ret; 74 | struct ols_t *ols; 75 | 76 | fd = serial_open(port); 77 | if (fd < 0) { 78 | fprintf(stderr, "Unable to open port '%s' \n", port); 79 | return NULL; 80 | } 81 | 82 | ret = serial_setup(fd, speed); 83 | if (ret) { 84 | fprintf(stderr, "Unable to set serial port parameters \n"); 85 | return NULL; 86 | } 87 | 88 | ols = malloc(sizeof(struct ols_t)); 89 | if (ols == NULL) { 90 | fprintf(stderr, "Error allocating memory \n"); 91 | return NULL; 92 | } 93 | 94 | ols->fd = fd; 95 | ols->verbose = 0; 96 | ols->flash = NULL; 97 | 98 | ret = OLS_GetID(ols); 99 | if (ret) { 100 | fprintf(stderr, "Unable to read ID \n"); 101 | free(ols); 102 | return NULL; 103 | } 104 | 105 | ret = OLS_GetFlashID(ols); 106 | if (ret) { 107 | fprintf(stderr, "Unable to read Flash ID \n"); 108 | free(ols); 109 | return NULL; 110 | } 111 | 112 | return ols; 113 | } 114 | 115 | int OLS_Deinit(struct ols_t *ols) 116 | { 117 | serial_close(ols->fd); 118 | free(ols); 119 | 120 | return 0; 121 | } 122 | 123 | /* 124 | * Does OLS self-test 125 | * ols->fd - fd of OLS com port 126 | */ 127 | int OLS_RunSelftest(struct ols_t *ols) 128 | { 129 | uint8_t cmd[4] = {0x07, 0x00, 0x00, 0x00}; 130 | uint8_t status; 131 | int res, retry; 132 | 133 | res = serial_write(ols->fd, cmd, 4); 134 | if (res != 4) { 135 | printf("Error writing to OLS\n"); 136 | return -2; 137 | } 138 | 139 | retry=0; 140 | while (1) { 141 | res = serial_read(ols->fd, &status, 1, 100); 142 | 143 | if (res < 1) { 144 | retry ++; 145 | } 146 | 147 | if (res == 1) { 148 | printf("done...\n"); 149 | break; 150 | } 151 | 152 | // 20 second timenout 153 | if (retry > 60) { 154 | printf("failed :( - timeout\n"); 155 | return -1; 156 | } 157 | } 158 | 159 | if(status == 0x00){ 160 | printf("Passed self-test :) \n"); 161 | }else{ 162 | printf("Failed :( - '%x'\n", status); 163 | if(status & 0x01 /* 0b1 */) 164 | printf("ERROR: 1V2 supply failed self-test :( \n"); 165 | if(status & 0x02 /* 0b10 */) 166 | printf("ERROR: 2V5 supply failed self-test :( \n"); 167 | if(status & 0x04 /* 0b100 */) 168 | printf("ERROR: PROG_B pull-up failed self-test :( \n"); 169 | if(status & 0x08 /* 0b1000 */) 170 | printf("ERROR: DONE pull-up failed self-test :( \n"); 171 | if(status & 0x10 /* 0b10000 */) 172 | printf("ERROR: unknown ROM JEDEC ID (this could be ok...) :( \n"); 173 | if(status & 0x20 /* 0b100000 */) 174 | printf("ERROR: UPDATE button pull-up failed self-test :( \n"); 175 | } 176 | 177 | return 0; 178 | } 179 | 180 | 181 | /* 182 | * Reads OLS status 183 | * ols->fd - fd of ols com port 184 | */ 185 | int OLS_GetStatus(struct ols_t *ols) 186 | { 187 | uint8_t cmd[4] = {0x05, 0x00, 0x00, 0x00}; 188 | uint8_t status; 189 | int res; 190 | 191 | res = serial_write(ols->fd, cmd, 4); 192 | if (res != 4) { 193 | printf("Error writing to OLS\n"); 194 | return -2; 195 | } 196 | 197 | res = serial_read(ols->fd, &status, 1, 100); 198 | 199 | if (res != 1) { 200 | printf("Error reading OLS status\n"); 201 | return -1; 202 | } 203 | 204 | printf("OLS status: %02x\n", status); 205 | return 0; 206 | } 207 | 208 | /* 209 | * Reads OLS version 210 | * ols->fd - fd of ols com port 211 | */ 212 | int OLS_GetID(struct ols_t *ols) 213 | { 214 | uint8_t cmd[4] = {0x00, 0x00, 0x00, 0x00}; 215 | uint8_t ret[7]; 216 | int res; 217 | int i; 218 | 219 | ret[0] = 0x00; 220 | 221 | for (i = 0; i < 7; i++) { 222 | /* Write a single 0x00 until we get a response */ 223 | res = serial_write(ols->fd, cmd, 1); 224 | 225 | if (res != 1) { 226 | printf("Error writing to OLS\n"); 227 | return -2; 228 | } 229 | 230 | res = serial_read(ols->fd, ret, 1, 1); 231 | if (res == 1) { 232 | if (ret[0] == 'H') { 233 | /* Found response */ 234 | break; 235 | } 236 | /* Ignore anything else */ 237 | } 238 | } 239 | 240 | if (ret[0] != 'H') { 241 | printf("Error reading OLS version id\n"); 242 | return -1; 243 | } 244 | 245 | /* Read the following 6 response bytes */ 246 | 247 | res = serial_read(ols->fd, ret + 1, 6, 10); 248 | if (res != 6) { 249 | printf("Error reading OLS version id\n"); 250 | return -1; 251 | } 252 | 253 | /* Now the sender and receiver are in sync */ 254 | 255 | if (ret[0] != 'H' || ret[2] != 'F' || ret[5] != 'B') { 256 | printf("Error reading OLS id - invalid data returned\n"); 257 | return -1; 258 | } 259 | 260 | printf("Found OLS HW: %d, FW: %d.%d, Boot: %d\n", ret[1], ret[3], ret[4], ret[6]); 261 | return 0; 262 | } 263 | 264 | /* 265 | * commands OLS to enter bootloader mode 266 | * ols->fd - fd of ols com port 267 | */ 268 | int OLS_EnterBootloader(struct ols_t *ols) 269 | { 270 | uint8_t cmd[4] = {0x24, 0x24, 0x24, 0x24}; 271 | int res; 272 | 273 | res = serial_write(ols->fd, cmd, 4); 274 | if (res != 4) { 275 | printf("Error writing to OLS\n"); 276 | return -2; 277 | } 278 | 279 | printf("OLS switched to bootloader mode\n"); 280 | return 0; 281 | } 282 | 283 | /* 284 | * Switch the OLS to run mode 285 | * ols->fd - fd of ols com port 286 | */ 287 | int OLS_EnterRunMode(struct ols_t *ols) 288 | { 289 | uint8_t cmd[4] = {0xFF, 0xFF, 0xFF, 0xFF}; 290 | int res; 291 | 292 | res = serial_write(ols->fd, cmd, 4); 293 | if (res != 4) { 294 | printf("Error writing to OLS\n"); 295 | return -2; 296 | } 297 | 298 | printf("OLS switched to RUN mode\n"); 299 | return 0; 300 | } 301 | 302 | /* 303 | * ask the OLS for JEDEC id 304 | * ols->fd - fd of ols com port 305 | */ 306 | int OLS_GetFlashID(struct ols_t *ols) { 307 | uint8_t cmd[4] = {0x01, 0x00, 0x00, 0x00}; 308 | uint8_t ret[4]; 309 | int res; 310 | int i; 311 | 312 | res = serial_write(ols->fd, cmd, 4); 313 | if (res != 4) { 314 | printf("Error writing to OLS\n"); 315 | return -2; 316 | } 317 | 318 | res = serial_read(ols->fd, ret, 4, 10); 319 | if (res != 4) { 320 | printf("Error reading JEDEC ID\n"); 321 | return -1; 322 | } 323 | 324 | if (ret[0] == 'H' && ret[2] == 'F') { 325 | printf("Error reading flash id - OLS id was returned instead\n"); 326 | return -1; 327 | } 328 | 329 | for (i = 0; i < OLS_FLASH_NUM; i++) { 330 | if (memcmp(ret, OLS_Flash[i].jedec_id, 4) == 0) { 331 | ols->flash = (struct ols_flash_t *)&OLS_Flash[i]; 332 | printf("Found flash: %s \n", OLS_Flash[i].name); 333 | break; 334 | } 335 | } 336 | 337 | if(ols->flash == NULL) { 338 | printf("Error - unknown flash type (%02x %02x %02x %02x)\n", ret[0], ret[1], ret[2], ret[3]); 339 | printf("Is OLS in update mode ??\n"); 340 | return -1; 341 | } 342 | 343 | return 0; 344 | } 345 | 346 | /* 347 | * erases OLS flash 348 | * ols->fd - fd of ols com port 349 | */ 350 | int OLS_FlashErase(struct ols_t *ols) 351 | { 352 | uint8_t cmd[4] = {0x04, 0x00, 0x00, 0x00}; 353 | uint8_t status; 354 | int res; 355 | int retry = 0; 356 | 357 | if (ols->flash == NULL) { 358 | printf("Cannot erase unknown flash\n"); 359 | return -3; 360 | } 361 | 362 | res = serial_write(ols->fd, cmd, 4); 363 | if (res != 4) { 364 | printf("Error writing to OLS\n"); 365 | return -2; 366 | } 367 | 368 | printf("Chip erase ... "); 369 | 370 | while (1) { 371 | res = serial_read(ols->fd, &status, 1, 100); 372 | 373 | if (res <1) { 374 | retry ++; 375 | } 376 | 377 | if (res == 1) { 378 | if (status == 0x01) { 379 | printf("done :)\n"); 380 | return 0; 381 | } 382 | printf("failed :( - bad reply '%x' Number of reply bytes: %x \n", status, res); 383 | return -1; 384 | } 385 | 386 | // 20 second timenout 387 | if (retry > 60) { 388 | printf("failed :( - timeout\n"); 389 | return -1; 390 | } 391 | } 392 | 393 | return 0; 394 | } 395 | 396 | /* 397 | * Reads data from flash 398 | * ols->fd - fd of ols com port 399 | * page - which page should be read 400 | * buf - buffer where the data will be stored 401 | */ 402 | int OLS_FlashRead(struct ols_t *ols, uint16_t page, uint8_t *buf) 403 | { 404 | uint8_t cmd[4] = {0x03, 0x00, 0x00, 0x00}; 405 | int res; 406 | 407 | if (ols->flash == NULL) { 408 | printf("Cannot READ unknown flash\n"); 409 | return -3; 410 | } 411 | 412 | if (page > ols->flash->pages) { 413 | printf("You are trying to read page %d, but we have only %d !\n", page, ols->flash->pages); 414 | return -2; 415 | } 416 | 417 | if(ols->flash->page_size == 264){//ATMEL ROM with 264 byte pages 418 | cmd[1] = (page >> 7) & 0xff; 419 | cmd[2] = (page << 1) & 0xff; 420 | } else { //most 256 byte page roms 421 | cmd[1] = (page >> 8) & 0xff; 422 | cmd[2] = (page) & 0xff; 423 | } 424 | 425 | if (ols->verbose) 426 | printf("Page 0x%04x read ... ", page); 427 | 428 | res = serial_write(ols->fd, cmd, 4); 429 | if (res != 4) { 430 | printf("Error writing CMD to OLS\n"); 431 | return -2; 432 | } 433 | 434 | res = serial_read(ols->fd, buf, ols->flash->page_size, 100); 435 | 436 | if (res == ols->flash->page_size) { 437 | if (ols->verbose) 438 | printf("OK\n"); 439 | return 0; 440 | } 441 | 442 | printf("Failed :(\n"); 443 | return -1; 444 | } 445 | 446 | /* 447 | * writes data to flash 448 | * ols->fd - fd of ols com port 449 | * page - where the data should be written to 450 | * buf - data to be written 451 | */ 452 | int OLS_FlashWrite(struct ols_t *ols, uint16_t page, uint8_t *buf) 453 | { 454 | uint8_t cmd[4] = {0x02, 0x00, 0x00, 0x00}; 455 | uint8_t status; 456 | uint8_t chksum; 457 | int res; 458 | 459 | if (ols->flash == NULL) { 460 | printf("Cannot Write unknown flash\n"); 461 | return -3; 462 | } 463 | 464 | if (page > ols->flash->pages) { 465 | printf("You are trying to Write page %d, but we have only %d !\n", page, ols->flash->pages); 466 | return -2; 467 | } 468 | 469 | if(ols->flash->page_size == 264){//ATMEL ROM with 264 byte pages 470 | cmd[1] = (page >> 7) & 0xff; 471 | cmd[2] = (page << 1) & 0xff; 472 | } else { //most 256 byte page roms 473 | cmd[1] = (page >> 8) & 0xff; 474 | cmd[2] = (page) & 0xff; 475 | } 476 | 477 | chksum = Data_Checksum(buf, ols->flash->page_size); 478 | 479 | if (ols->verbose) 480 | printf("Page 0x%04x write ... (0x%04x 0x%04x)", page, cmd[1], cmd[2]); 481 | 482 | res = serial_write(ols->fd, cmd, 4); 483 | res += serial_write(ols->fd, buf, ols->flash->page_size); 484 | res += serial_write(ols->fd, &chksum, 1); 485 | if (res != 4 + 1 + ols->flash->page_size) { 486 | printf("Error writing CMD to OLS\n"); 487 | return -2; 488 | } 489 | 490 | res = serial_read(ols->fd, &status, 1, 100); 491 | 492 | if (res != 1) { 493 | printf("Page writing timeout\n"); 494 | return -1; 495 | } 496 | 497 | if (status == 0x01) { 498 | if (ols->verbose) 499 | printf("OK\n"); 500 | return 0; 501 | } 502 | 503 | printf("Checksum error :(\n"); 504 | return -1; 505 | } 506 | 507 | -------------------------------------------------------------------------------- /src/ols.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Part of ols-fwloader - Open Bench Logic Sniffer update interface 3 | * 4 | * Copyright (C) 2011 Michal Demin 5 | * 6 | * This program is free software: you can redistribute it and/or modify 7 | * it under the terms of the GNU General Public License as published by 8 | * the Free Software Foundation, either version 3 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * This program is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU General Public License 17 | * along with this program. If not, see . 18 | */ 19 | 20 | #ifndef OLS_H_ 21 | #define OLS_H_ 22 | 23 | #include 24 | 25 | struct ols_flash_t { 26 | const char *jedec_id; 27 | uint16_t page_size; 28 | uint16_t pages; 29 | const char *name; 30 | }; 31 | 32 | struct ols_t { 33 | int fd; 34 | struct ols_flash_t *flash; 35 | int verbose; 36 | }; 37 | 38 | 39 | struct ols_t *OLS_Init(char *, unsigned long); 40 | int OLS_Deinit(struct ols_t *); 41 | int OLS_RunSelftest(struct ols_t *); 42 | int OLS_GetStatus(struct ols_t *); 43 | int OLS_GetID(struct ols_t *); 44 | int OLS_EnterBootloader(struct ols_t *); 45 | int OLS_EnterRunMode(struct ols_t *); 46 | int OLS_GetFlashID(struct ols_t *); 47 | struct ols_flash_t *OLS_GetFlash(struct ols_t *); 48 | int OLS_FlashErase(struct ols_t *); 49 | int OLS_FlashRead(struct ols_t *, uint16_t page, uint8_t *buf); 50 | int OLS_FlashWrite(struct ols_t *, uint16_t page, uint8_t *buf); 51 | 52 | #endif 53 | 54 | -------------------------------------------------------------------------------- /src/serial.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Part of ols-fwloader - Serial port routines 3 | * Inspired by pirate-loader, written by Piotr Pawluczuk 4 | * http://the-bus-pirate.googlecode.com/svn/trunk/bootloader-v4/pirate-loader/source/pirate-loader.c 5 | * 6 | * Copyright (C) 2010 Piotr Pawluczuk 7 | * Copyright (C) 2011 Michal Demin 8 | * 9 | * This program is free software: you can redistribute it and/or modify 10 | * it under the terms of the GNU General Public License as published by 11 | * the Free Software Foundation, either version 3 of the License, or 12 | * (at your option) any later version. 13 | * 14 | * This program is distributed in the hope that it will be useful, 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 17 | * GNU General Public License for more details. 18 | * 19 | * You should have received a copy of the GNU General Public License 20 | * along with this program. If not, see . 21 | */ 22 | 23 | #include 24 | 25 | #include 26 | #include 27 | #include 28 | 29 | #include 30 | 31 | #include "serial.h" 32 | 33 | int serial_setup(int fd, unsigned long speed) 34 | { 35 | #if IS_WIN32 36 | COMMTIMEOUTS timeouts; 37 | DCB dcb = {0}; 38 | HANDLE hCom = (HANDLE)fd; 39 | 40 | dcb.DCBlength = sizeof(dcb); 41 | 42 | dcb.BaudRate = speed; 43 | dcb.ByteSize = 8; 44 | dcb.Parity = NOPARITY; 45 | dcb.StopBits = ONESTOPBIT; 46 | 47 | if( !SetCommState(hCom, &dcb) ){ 48 | return -1; 49 | } 50 | 51 | timeouts.ReadIntervalTimeout = 100; 52 | timeouts.ReadTotalTimeoutMultiplier = 10; 53 | timeouts.ReadTotalTimeoutConstant = 100; 54 | timeouts.WriteTotalTimeoutMultiplier = 10; 55 | timeouts.WriteTotalTimeoutConstant = 100; 56 | 57 | if (!SetCommTimeouts(hCom, &timeouts)) { 58 | return -1; 59 | } 60 | 61 | return 0; 62 | #else 63 | struct termios t_opt; 64 | speed_t baud; 65 | 66 | switch (speed) { 67 | case 921600: 68 | baud = B921600; 69 | break; 70 | case 115200: 71 | baud = B115200; 72 | break; 73 | case 1000000: 74 | baud = B1000000; 75 | break; 76 | case 1500000: 77 | baud = B1500000; 78 | break; 79 | default: 80 | printf("unknown speed setting \n"); 81 | return -1; 82 | break; 83 | } 84 | 85 | /* set the serial port parameters */ 86 | fcntl(fd, F_SETFL, 0); 87 | tcgetattr(fd, &t_opt); 88 | cfsetispeed(&t_opt, baud); 89 | cfsetospeed(&t_opt, baud); 90 | t_opt.c_cflag |= (CLOCAL | CREAD); 91 | t_opt.c_cflag &= ~PARENB; 92 | t_opt.c_cflag &= ~CSTOPB; 93 | t_opt.c_cflag &= ~CSIZE; 94 | t_opt.c_cflag |= CS8; 95 | t_opt.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG); 96 | t_opt.c_iflag &= ~(IXON | IXOFF | IXANY); 97 | t_opt.c_iflag &= ~(ICRNL | INLCR); 98 | t_opt.c_oflag &= ~(OCRNL | ONLCR); 99 | t_opt.c_oflag &= ~OPOST; 100 | t_opt.c_cc[VMIN] = 0; 101 | t_opt.c_cc[VTIME] = 1; 102 | 103 | #if IS_DARWIN 104 | if( tcsetattr(fd, TCSANOW, &t_opt) < 0 ) { 105 | return -1; 106 | } 107 | 108 | return ioctl( fd, IOSSIOSPEED, &baud ); 109 | #else 110 | tcflush(fd, TCIOFLUSH); 111 | 112 | return tcsetattr(fd, TCSANOW, &t_opt); 113 | #endif 114 | #endif 115 | } 116 | 117 | int serial_write(int fd, const char *buf, int size) 118 | { 119 | int ret = 0; 120 | #if IS_WIN32 121 | HANDLE hCom = (HANDLE)fd; 122 | int res = 0; 123 | unsigned long bwritten = 0; 124 | 125 | res = WriteFile(hCom, buf, size, &bwritten, NULL); 126 | 127 | if( res == FALSE ) { 128 | ret = -1; 129 | } else { 130 | ret = bwritten; 131 | } 132 | #else 133 | ret = write(fd, buf, size); 134 | #endif 135 | 136 | #if DEBUG 137 | if (ret != size) 138 | fprintf(stderr, "Error sending data (written %d should have written %d)\n", ret, size); 139 | #endif 140 | 141 | return ret; 142 | } 143 | 144 | int serial_read(int fd, char *buf, int size, int timeout) 145 | { 146 | int len = 0; 147 | int ret = 0; 148 | 149 | #if IS_WIN32 150 | HANDLE hCom = (HANDLE)fd; 151 | unsigned long bread = 0; 152 | 153 | ret = ReadFile(hCom, buf, size, &bread, NULL); 154 | 155 | if( ret == FALSE || ret==-1 ) { 156 | len = -1; 157 | } else { 158 | len = bread; 159 | } 160 | 161 | #else 162 | while (len < size) { 163 | ret = read(fd, buf+len, size-len); 164 | if (ret == -1){ 165 | //printf("ret -1"); 166 | return -1; 167 | } 168 | 169 | if (ret == 0) { 170 | timeout--; 171 | 172 | if (timeout <= 0) 173 | break; 174 | 175 | continue; 176 | } 177 | 178 | len += ret; 179 | } 180 | #endif 181 | 182 | #if DEBUG 183 | if (len != size) 184 | fprintf(stderr, "Error receiving data (read %d should have read %d)\n", len, size); 185 | #endif 186 | 187 | return len; 188 | } 189 | 190 | int serial_open(const char *port) 191 | { 192 | int fd; 193 | #if IS_WIN32 194 | static char full_path[32] = {0}; 195 | 196 | HANDLE hCom = NULL; 197 | 198 | if( port[0] != '\\' ) { 199 | _snprintf(full_path, sizeof(full_path) - 1, "\\\\.\\%s", port); 200 | port = full_path; 201 | } 202 | 203 | hCom = CreateFileA(port, GENERIC_WRITE | GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); 204 | 205 | if( !hCom || hCom == INVALID_HANDLE_VALUE ) { 206 | fd = -1; 207 | } else { 208 | fd = (int)hCom; 209 | } 210 | #else 211 | fd = open(port, O_RDWR | O_NOCTTY | O_NDELAY); 212 | if (fd == -1) { 213 | //fprintf(stderr, "Could not open serial port.\n"); 214 | return -1; 215 | } 216 | #endif 217 | return fd; 218 | } 219 | 220 | int serial_close(int fd) 221 | { 222 | #if IS_WIN32 223 | HANDLE hCom = (HANDLE)fd; 224 | 225 | CloseHandle(hCom); 226 | #else 227 | close(fd); 228 | #endif 229 | return 0; 230 | } 231 | 232 | -------------------------------------------------------------------------------- /src/serial.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Part of ols-fwloader - Serial port routines 3 | * Inspired by pirate-loader, written by Piotr Pawluczuk 4 | * http://the-bus-pirate.googlecode.com/svn/trunk/bootloader-v4/pirate-loader/source/pirate-loader.c 5 | * 6 | * Copyright (C) 2010 Piotr Pawluczuk 7 | * Copyright (C) 2011 Michal Demin 8 | * 9 | * This program is free software: you can redistribute it and/or modify 10 | * it under the terms of the GNU General Public License as published by 11 | * the Free Software Foundation, either version 3 of the License, or 12 | * (at your option) any later version. 13 | * 14 | * This program is distributed in the hope that it will be useful, 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 17 | * GNU General Public License for more details. 18 | * 19 | * You should have received a copy of the GNU General Public License 20 | * along with this program. If not, see . 21 | */ 22 | 23 | #ifndef MYSERIAL_H_ 24 | #define MYSERIAL_H_ 25 | 26 | #include 27 | #include 28 | 29 | #if IS_WIN32 30 | #include 31 | #include 32 | 33 | #define B115200 115200 34 | #define B921600 921600 35 | 36 | typedef long speed_t; 37 | #else // WIN32 38 | 39 | #include 40 | #include 41 | #include 42 | #include 43 | #include 44 | 45 | #endif 46 | 47 | #if IS_DARWIN 48 | #include 49 | #include 50 | 51 | #define B1500000 1500000 52 | #define B1000000 1000000 53 | #define B921600 921600 54 | #endif 55 | 56 | int serial_setup(int fd, unsigned long speed); 57 | int serial_write(int fd, const char *buf, int size); 58 | int serial_read(int fd, char *buf, int size, int timeout); 59 | int serial_open(const char *port); 60 | int serial_close(int fd); 61 | 62 | 63 | #endif 64 | 65 | --------------------------------------------------------------------------------