├── .gitignore ├── LICENSE ├── README.md ├── binaries ├── mgos512k-0x1000.bin └── mgos512k-0x7000.bin ├── fs └── hwinfo_struct.json ├── mos.yml ├── patch ├── Makefile.build ├── core │ └── mos.yml └── rboot │ ├── license.txt │ ├── rboot.c │ └── rboot.h └── src └── main.c /.gitignore: -------------------------------------------------------------------------------- 1 | # See http://help.github.com/ignore-files/ for more about ignoring files. 2 | 3 | # mgos 4 | build/ 5 | deps/ 6 | 7 | # IDEs and editors 8 | .on-save.json 9 | auto_compile.log 10 | 11 | # System Files 12 | .DS_Store 13 | Thumbs.db 14 | test/ 15 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 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 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | > ⚠ Unfortunately, due to missing time, I will no longer be able to maintain this repository. If you are interested, I encourage you to fork the project and continue development. 2 | 3 | # Convert to Mongoose OS 4 | 5 | A minimal firmware for OTA (over the air) flashing Mongoose OS from Tasmota 6 | or compatible firmware types. 7 | 8 | ## Overview 9 | 10 | This is a first working draft for an intermediate firmware, which can be used to 11 | wirelessly restore [Mongoose OS](https://mongoose-os.com/docs/mongoose-os/quickstart/setup.md) 12 | on esp8266 based IoT devices initially shipped with a Mongoose OS based 13 | firmware. This firmware will install a minimal version of Mongoose OS, and you 14 | can continue from there to your favourite target release, such as e.g. the 15 | Shelly stock firmware. 16 | 17 | ## Supported Devices 18 | 19 | Any esp8266 device with a physical flash size of 2 or 4 MB, which was initially 20 | shipped with a Mongoose OS based firmware. 21 | 22 | ## Install 23 | 24 | ⚠ Overwriting a device's boot loader via OTA update is a risky operation. If 25 | something unexpected fails, your device may be bricked, unless you know how to 26 | flash a new firmware over a wired connection. 27 | 28 | ⚠ Specifically, you currently should't use this for Shelly PlugS (due to this [known issue](https://github.com/yaourdt/tasmota-to-mgos/issues/3)). 29 | 30 | There are two versions with different bootloader configuration locations. The 31 | default location in Mongoose OS is `0x7000`, but some devices use `0x1000` 32 | instead. Be careful to pick the appropriate version, or the device will be 33 | bricked during the first boot of the target firmware! 34 | 35 | For Shelly 1, Shelly 1L, Shelly 1PM, Shelly 2, Shelly 2.5, Shelly Uni, 36 | Shelly i3, and Shelly EM use __0x1000__, for Shelly Plug S, Shelly RGBW2, 37 | Shelly Dimmer 1, Shelly Dimmer 2, Shelly Bulb, Shelly Vintage, Shelly Plug US, 38 | Shelly H&T, Shelly Duo, Shelly Plug 2 use __0x7000__. 39 | 40 | Available versions: 41 | * [0x1000](https://dl.dasker.eu/firmware/mgos512k-0x1000.bin) 42 | * [0x7000](https://dl.dasker.eu/firmware/mgos512k-0x7000.bin) 43 | 44 | **Step 1**: Use your current firmware's update mechanism to apply that intermediate firmware 45 | as you would for any other update. If the process succeeds, the device will reboot 46 | and you will see a new WiFi network labeled _mg-????_. Be patient, this process can take up to two 47 | minutes. 48 | 49 | **Step 2**: Obtain a target firmware file. If you own a 50 | Shelly device and want to revert to stock firmware, you can find the matching 51 | files [here](https://api.shelly.cloud/files/firmware). 52 | 53 | **Step 3**: As soon as you have joined the device's new WiFi network, you can install the desired 54 | version of a Mongoose OS based firmware by issuing this command from your computer: 55 | 56 | ``` 57 | curl -i -F filedata=@./fw.zip http://10.42.42.44/update 58 | ``` 59 | 60 | Replace `./fw.zip` with the path to the target firmware ZIP-file you obtained previously. 61 | 62 | ## Trouble shooting 63 | 64 | ### Tasmota: Upload Failed - Not compatible 65 | If you cannot upload the intermediate firmware to Tasmota due to a _not 66 | compatible_ error, run `Setoption78 1` in the Tasmota console to disable 67 | the compatibility check and allow for the update to be applied. See 68 | [this github issue](https://github.com/esphome/issues/issues/955). 69 | 70 | ### Tasmota: Upload Failed - Upload buffer miscompare 71 | There is not enough space for Tasmota to save the update file. Switch to 72 | **tasmota-minimal.bin** before trying again. 73 | 74 | ## Build the firmware yourself 75 | 76 | You can compile a binary version of this firmware using [mos tools](https://mongoose-os.com/docs/mongoose-os/quickstart/setup.md#1-download-and-install-mos-tool). Once installed, `git clone` this repository and 77 | run 78 | 79 | ``` 80 | mos build --build-var BOOT_CONFIG_ADDR=0x7000 --local 81 | ``` 82 | 83 | to copy all necessary libraries to `deps/...`. It does not matter if the initial 84 | build fails, as long as the libraries have been installed. Now patch some files 85 | with those included in this repository: 86 | 87 | ``` 88 | cp patch/Makefile.build deps/modules/mongoose-os/platforms/esp8266/Makefile.build 89 | cp patch/rboot/rboot.c deps/modules/mongoose-os/platforms/esp8266/rboot/rboot/rboot.c 90 | cp patch/rboot/rboot.h deps/modules/mongoose-os/platforms/esp8266/rboot/rboot/rboot.h 91 | cp patch/core/mos.yml deps/core/mos.yml 92 | ``` 93 | 94 | and build again with 95 | 96 | ``` 97 | mos build --build-var BOOT_CONFIG_ADDR=0x7000 --local 98 | ``` 99 | 100 | for the Mongoose OS bootloader config default location. Alternatively, you may 101 | build using `BOOT_CONFIG_ADDR=0x1000`. 102 | 103 | The generated firmware can be found in `build/fw.zip` and flashed using the 104 | command `mos flash --esp-erase-chip --esp-flash-params 'dout,4m,80m'`. 105 | 106 | If you want to read it back without it changing anything, pull pin 14 low during 107 | boot. 108 | 109 | ## Acknowledgments 110 | Thanks to [rojer](https://github.com/rojer) for helping me with the debugging of 111 | the initial code. 112 | 113 | This firmware is build using a fork of [Mongoose OS docker action](https://github.com/dea82/mongoose-os-action) 114 | which can be found [here](https://github.com/yaourdt/mongoose-os-action). 115 | 116 | ## License 117 | Copyright (C) 2020, Mark Dornbach 118 | 119 | This program is free software: you can redistribute it and/or modify 120 | it under the terms of the GNU General Public License as published by 121 | the Free Software Foundation, either version 3 of the License, or 122 | (at your option) any later version. 123 | 124 | This program is distributed in the hope that it will be useful, 125 | but WITHOUT ANY WARRANTY; without even the implied warranty of 126 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 127 | GNU General Public License for more details. 128 | 129 | You should have received a copy of the GNU General Public License 130 | along with this program. If not, see https://www.gnu.org/licenses/. 131 | -------------------------------------------------------------------------------- /binaries/mgos512k-0x1000.bin: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yaourdt/tasmota-to-mgos/9a2d5844cb4c8713e47f2830abfc46c2914f40e4/binaries/mgos512k-0x1000.bin -------------------------------------------------------------------------------- /binaries/mgos512k-0x7000.bin: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yaourdt/tasmota-to-mgos/9a2d5844cb4c8713e47f2830abfc46c2914f40e4/binaries/mgos512k-0x7000.bin -------------------------------------------------------------------------------- /fs/hwinfo_struct.json: -------------------------------------------------------------------------------- 1 | {"batch_id": 2, "model": "unknown", "manufacturer": "Allterco", "hw_revision": "prod-2018-08", "hwinfo_ver": 1} 2 | -------------------------------------------------------------------------------- /mos.yml: -------------------------------------------------------------------------------- 1 | author: mark dornbach 2 | description: a minimal firmware for ota flashing mongoose os from tasmota or tuya stock firmware 3 | platform: esp8266 4 | version: 0.1.2 5 | 6 | libs_version: 2.19.0 7 | modules_version: 2.19.0 8 | mongoose_os_version: 2.19.0 9 | manifest_version: 2020-01-29 10 | sources: [src] 11 | filesystem: [fs] 12 | tags: [c, tuya, tasmota, mongoose] 13 | 14 | # Custom configuration 15 | config_schema: 16 | #### sys #### 17 | - ["debug.level", 2] # 0 = ERROR, 1 = WARN, 2 = INFO, 3+ = DEBUG 18 | #### wifi #### 19 | - ["wifi.sta.enable", true ] # join wifi as client 20 | - ["wifi.sta.ssid", "vtrust-flash" ] # SSID 21 | - ["wifi.sta.pass", "" ] # password 22 | - ["wifi.sta.ip", "10.42.42.44" ] # device static IP 23 | - ["wifi.sta.netmask", "255.255.255.0" ] # netmask 24 | - ["wifi.sta.gw", "10.42.42.1" ] # default gateway 25 | - ["wifi.sta.nameserver", "10.42.42.1" ] # DNS nameserver 26 | - ["wifi.ap.enable", true ] # enable Access Point mode 27 | - ["wifi.ap.ssid", "mg-??????" ] # SSID to use. ?? symbols are substituted by MAC address 28 | - ["wifi.ap.pass", "" ] # password 29 | - ["wifi.ap.hidden", false ] # hide WiFi network 30 | - ["wifi.ap.ip", "10.42.42.44" ] # device static IP 31 | - ["wifi.ap.netmask", "255.255.255.0" ] # netmask 32 | - ["wifi.ap.gw", "10.42.42.1" ] # default gateway 33 | - ["wifi.ap.nameserver", "10.42.42.1" ] # DNS nameserver 34 | - ["wifi.ap.dhcp_start", "10.42.42.50" ] # DHCP start Address 35 | - ["wifi.ap.dhcp_end", "10.42.42.60" ] # DHCP end Address 36 | 37 | libs: 38 | - origin: https://github.com/mongoose-os-libs/wifi 39 | - origin: https://github.com/mongoose-os-libs/mbedtls # remove support for crypto chip to reduce footprint 40 | variant: esp8266-noatca 41 | - origin: https://github.com/mongoose-os-libs/mongoose # remove SSL support to reduce footprint 42 | variant: esp8266-nossl 43 | - origin: https://github.com/mongoose-os-libs/ota-http-server # local OTA update via HTTP POST 44 | 45 | build_vars: 46 | FLASH_SIZE: 524288 47 | MGOS_MBEDTLS_ENABLE_ATCA: 0 48 | 49 | cdefs: 50 | MG_ENABLE_SSL: 0 51 | -------------------------------------------------------------------------------- /patch/Makefile.build: -------------------------------------------------------------------------------- 1 | MAKEFLAGS += --warn-undefined-variables 2 | .DEFAULT_GOAL := all 3 | 4 | ifdef MGOS_VARS_FILE 5 | include $(MGOS_VARS_FILE) 6 | endif 7 | 8 | APP ?= mongoose-os 9 | APP_PLATFORM = esp8266 10 | APP_CFLAGS ?= 11 | APP_CXXFLAGS ?= 12 | APP_EXTRA_SRCS ?= 13 | FFI_SYMBOLS ?= 14 | # List of app source files, globs are supported 15 | APP_SOURCES ?= 16 | # List of include dirs, in addition to dirs from APP_SOURCES 17 | APP_INCLUDES ?= 18 | # List of app files to put into the device's filesystem, globs are supported 19 | APP_FS_FILES ?= 20 | # List of static libs (.a) which are parts of the app 21 | APP_BIN_LIBS ?= 22 | # Extra parts to include in firmware 23 | APP_EXTRA_FW_PARTS ?= 24 | 25 | BUILD_LIB_NAME ?= lib.a 26 | 27 | # Get list of dirs which contain sources and filesystem files 28 | APP_SOURCE_DIRS = $(sort $(dir $(APP_SOURCES))) 29 | APP_FS_DIRS = $(sort $(dir $(APP_FS_FILES))) 30 | 31 | MGOS_DEBUG_UART ?= 0 32 | 33 | APP_PATH := $(CURDIR) 34 | 35 | MGOS_PATH ?= ../.. 36 | include $(MGOS_PATH)/tools/mk/mgos_common.mk 37 | 38 | MGOS_ESP8266_PATH ?= $(MGOS_PATH)/platforms/esp8266 39 | BOOTLOADER_PATH ?= $(MGOS_ESP8266_PATH)/rboot 40 | ESPTOOL2 = $(BUILD_DIR)/esptool2 41 | # Enable heap allocation logging - every malloc/free is logged. 42 | MGOS_ENABLE_HEAP_LOG ?= 0 43 | # In addition to logging allocations, print call traces for them. 44 | # This instruments every function and increases code size significantly. 45 | MGOS_ENABLE_CALL_TRACE ?= 0 46 | MGOS_ESP8266_RTOS ?= 0 47 | 48 | # Normally boot loader is not updated during OTA update. 49 | # The firmware built with this flag set to true will, when used for OTA, 50 | # will reflash the boot loader as well. 51 | MGOS_UPDATE_BOOT_LOADER ?= false 52 | 53 | FLASH_SIZE ?= 4194304 54 | 55 | RF_CAL_DATA_SIZE = 0x1000 56 | SYS_PARAMS_SIZE = 0x4000 57 | 58 | BUILD_DIR ?= $(CURDIR)/.build 59 | RBOOT_BUILD_DIR ?= $(BUILD_DIR)/rboot 60 | FW_STAGING_DIR ?= $(BUILD_DIR)/fw_temp 61 | FW_DIR ?= $(CURDIR)/firmware 62 | GEN_DIR ?= $(BUILD_DIR)/gen 63 | 64 | # Absolutize all paths which can be given by the caller 65 | override BUILD_DIR := $(abspath $(BUILD_DIR)) 66 | override FW_STAGING_DIR := $(abspath $(FW_STAGING_DIR)) 67 | override FW_DIR := $(abspath $(FW_DIR)) 68 | override GEN_DIR := $(abspath $(GEN_DIR)) 69 | 70 | LWIP_BUILD_DIR = $(BUILD_DIR)/lwip 71 | 72 | FS_STAGING_DIR = $(BUILD_DIR)/fs 73 | FW_SECTS = .text .data .rodata 74 | FW_USER_ARGS = -quiet -bin -boot2 75 | 76 | include $(MGOS_ESP8266_PATH)/common.mk 77 | 78 | MGOS_ESP_SRC_PATH = $(MGOS_ESP8266_PATH)/src 79 | 80 | MGOS_SRCS += cs_file.c cs_hex.c cs_rbuf.c \ 81 | mgos_config_util.c \ 82 | mgos_core_dump.c \ 83 | mgos_dlsym.c \ 84 | mgos_event.c \ 85 | mgos_file_utils.c \ 86 | mgos_gpio.c \ 87 | mgos_hw_timers.c \ 88 | mgos_init.c \ 89 | mgos_json_utils.cpp \ 90 | mgos_time.c \ 91 | mgos_timers.c mgos_timers.cpp \ 92 | mgos_mmap_esp.c \ 93 | mgos_sys_config.c $(notdir $(MGOS_CONFIG_C)) $(notdir $(MGOS_RO_VARS_C)) \ 94 | mgos_system.c mgos_system.cpp \ 95 | mgos_uart.c \ 96 | mgos_utils.c mgos_utils.cpp \ 97 | cs_crc32.c \ 98 | rboot-bigflash.c rboot-api.c \ 99 | json_utils.c \ 100 | umm_malloc.c \ 101 | frozen.c \ 102 | error_codes.cpp status.cpp 103 | 104 | MGOS_SRCS += esp_config.c \ 105 | esp_coredump.c \ 106 | esp_crypto.c \ 107 | esp_debug.c \ 108 | esp_exc.c \ 109 | esp_flash_writer.c \ 110 | esp_gpio.c \ 111 | esp_hal.c \ 112 | esp_heap_trace.c \ 113 | esp_hw_timers.c \ 114 | esp_hw_wdt.c \ 115 | esp_libc.c \ 116 | esp_main.c \ 117 | esp_periph.c \ 118 | esp_uart.c \ 119 | esp_umm_malloc.c 120 | 121 | APP_SRCS := $(notdir $(foreach m,$(APP_SOURCES),$(wildcard $(m)))) $(APP_EXTRA_SRCS) 122 | APP_BIN_LIB_FILES := $(foreach m,$(APP_BIN_LIBS),$(wildcard $(m))) 123 | 124 | MGOS_FEATURES ?= 125 | 126 | VPATH = $(MGOS_VPATH) $(APP_SOURCE_DIRS) $(MGOS_SRC_PATH) \ 127 | $(BOOTLOADER_PATH)/rboot/appcode \ 128 | $(MGOS_ESP_SRC_PATH) 129 | IPATH = $(MGOS_IPATH) $(sort $(APP_SOURCE_DIRS) $(APP_INCLUDES)) \ 130 | $(MGOS_ESP8266_PATH)/include $(MGOS_ESP8266_PATH)/include/spiffs \ 131 | $(BOOTLOADER_PATH)/rboot $(SDK_PATH)/include/lwip \ 132 | $(SDK_PATH) $(SDK_PATH)/include \ 133 | $(MGOS_ESP_SRC_PATH) 134 | LPATH = $(SDK_PATH)/lib $(BUILD_DIR)/lwip 135 | 136 | include $(MGOS_PATH)/tools/mk/mgos_features.mk 137 | 138 | # libraries used in this project, mainly provided by the SDK 139 | SDK_LIBS = c crypto espnow gcc m main net80211 pp phy stdc++ wpa wpa2 140 | LIBS = $(addprefix -l,$(SDK_LIBS)) 141 | 142 | SHIMS = -DNDEBUG 143 | 144 | MGOS_ESP_FEATURES = '-DUMM_ONFREE(ptr, size)=memset(ptr, 0xfa, size)' 145 | 146 | MG_FEATURES ?= $(MG_FEATURES_TINY) -DMG_ESP8266 \ 147 | -DMG_ENABLE_FILESYSTEM -DMG_ENABLE_DIRECTORY_LISTING \ 148 | -DMG_NET_IF=MG_NET_IF_LWIP_LOW_LEVEL \ 149 | -DCS_DISABLE_MD5 -DMG_EXT_MD5 \ 150 | -DMG_EXT_SHA1 151 | 152 | FEATURES_EXTRA ?= 153 | FEATURES = $(MG_FEATURES) $(MGOS_FEATURES) $(MGOS_ESP_FEATURES) \ 154 | $(FEATURES_EXTRA) \ 155 | -DCS_ENABLE_SPIFFS \ 156 | -DBOOT_BIG_FLASH -DESP_UMM_ENABLE \ 157 | -DMGOS_VFS_DEFINE_LIBC_REENT_API \ 158 | -DMGOS_VFS_DEFINE_LIBC_DIR_API \ 159 | -DMGOS_VFS_DEFINE_DIRENT \ 160 | -DMG_LOCALS 161 | 162 | MEMORY_FLAGS = -DFS_MAX_OPEN_FILES=5 163 | 164 | .PHONY: all clean 165 | 166 | # NB: These setting must match ones used during build of the LwIP library during Docker image build. 167 | LWIP_CFLAGS = -DTCP_MSS=536 -DTCP_MSL=5000 -DLWIP_FEATURES=1 -DLWIP_IPV6=0 -DLWIP_COMPAT_SOCKETS=0 \ 168 | -I$(MGOS_PATH)/include 169 | LIBS += -llwip2-536-feat 170 | IPATH += /opt/Espressif/xtensa-lx106-elf/xtensa-lx106-elf/sysroot/usr/include/lwip2 171 | 172 | MGOS_CFLAGS = -DMGOS_APP=\"$(APP)\" \ 173 | -DMGOS_MAX_NUM_UARTS=2 \ 174 | -DC_DISABLE_BUILTIN_SNPRINTF \ 175 | -DMGOS_NUM_HW_TIMERS=1 \ 176 | -DMGOS_ROOT_FS_TYPE='$(MGOS_ROOT_FS_TYPE)' \ 177 | -DMGOS_ROOT_FS_OPTS='$(MGOS_ROOT_FS_OPTS)' \ 178 | $(LWIP_CFLAGS) 179 | 180 | MGOS_LIB = $(BUILD_DIR)/mongoose-os.a 181 | MGOS_CONFIG_C = $(GEN_DIR)/mgos_config.c 182 | MGOS_RO_VARS_C = $(GEN_DIR)/mgos_ro_vars.c 183 | BUILD_INFO_C = $(GEN_DIR)/build_info.c 184 | BUILD_INFO_O = $(BUILD_DIR)/build_info.c.o 185 | BUILD_INFO_JSON = $(GEN_DIR)/build_info.json 186 | MG_BUILD_INFO_C = $(GEN_DIR)/mg_build_info.c 187 | MG_BUILD_INFO_O = $(BUILD_DIR)/mg_build_info.c.o 188 | FFI_EXPORTS_C = $(GEN_DIR)/ffi_exports.c 189 | FFI_EXPORTS_O = $(BUILD_DIR)/ffi_exports.c.o 190 | GENFILES_FLAG = $(GEN_DIR)/genfiles.done 191 | GENFILES_LIST = $(MGOS_CONFIG_C) $(MGOS_RO_VARS_C) 192 | IMAGE_HEADER_SIZE = 0 193 | MAP_FILE = $(BUILD_DIR)/$(APP).elf.map 194 | FS_IMG = $(FW_STAGING_DIR)/fs.bin 195 | 196 | SDK_PATH = /opt/Espressif/ESP8266_NONOS_SDK 197 | MGOS_SRCS += esp_exc_non_os.c 198 | MGOS_CFLAGS += -DLWIP_OPEN_SRC \ 199 | -DCS_MMAP -DSPIFFS_ON_PAGE_MOVE_HOOK=mgos_vfs_mmap_spiffs_on_page_move_hook 200 | LD_SCRIPT_TEMPL = $(MGOS_ESP8266_PATH)/ld/non_os.ld 201 | 202 | MGOS_OBJS = $(addprefix $(BUILD_DIR)/,$(MGOS_SRCS:=.o)) \ 203 | $(BUILD_DIR)/esp_exc_vectors.S.o \ 204 | $(BUILD_DIR)/esp_nsleep100.S.o 205 | APP_OBJS = $(addprefix $(BUILD_DIR)/,$(APP_SRCS:=.o)) 206 | 207 | BOOT_LOADER_BIN = $(RBOOT_BUILD_DIR)/rboot.bin 208 | APP_BIN = $(BUILD_DIR)/$(APP).bin 209 | APP_ELF = $(BUILD_DIR)/$(APP).elf 210 | LD_SCRIPT = $(GEN_DIR)/$(APP0_ADDR).ld 211 | LD_WRAPPERS = 212 | HEAP_LOG_FLAGS = 213 | 214 | ifneq "${MGOS_ENABLE_HEAP_LOG}${MGOS_ENABLE_CALL_TRACE}" "00" 215 | HEAP_LOG_FLAGS += -DMGOS_ENABLE_HEAP_LOG 216 | LD_WRAPPERS += -Wl,--wrap=umm_calloc \ 217 | -Wl,--wrap=umm_malloc \ 218 | -Wl,--wrap=umm_realloc \ 219 | -Wl,--wrap=umm_free 220 | endif 221 | ifeq "${MGOS_ENABLE_CALL_TRACE}" "1" 222 | MGOS_SRCS += cs_heap_trace.c 223 | HEAP_LOG_FLAGS += -finstrument-functions -DCALL_TRACE_SIZE=33 -DMGOS_ENABLE_CALL_TRACE 224 | endif 225 | 226 | IMAGE_HEADER_SIZE = 0x10 227 | 228 | # Flash layout configuration. 229 | BOOT_LOADER_ADDR ?= 0x0 230 | BOOT_CONFIG_ADDR ?= 0x7000 231 | BOOT_CONFIG_WRITE_ADDR ?= $(BOOT_CONFIG_ADDR) 232 | # Sys params must be at the end of flash, this is where SDK looks for (and writes!) them and it's not configurable. 233 | SYS_PARAMS_ADDR = $(shell printf "0x%05x" "$$(($(FLASH_SIZE) - $(SYS_PARAMS_SIZE)))") 234 | # RF calibration data sector location is configurable, but we also keep it at the end, just above sys params. 235 | FW_RF_CAL_DATA_ADDR = $(shell printf "0x%05x" "$$(($(FLASH_SIZE) - $(SYS_PARAMS_SIZE) - $(RF_CAL_DATA_SIZE)))") 236 | MGOS_ROOT_FS_TYPE ?= SPIFFS 237 | ifdef FS_SIZE 238 | MGOS_ROOT_FS_SIZE = $(FS_SIZE) # For backward comaptibility 239 | endif 240 | 241 | ifeq ("$(FLASH_SIZE)", "524288") 242 | # 243 | # 0.5MB (4Mb) nano layout: 244 | # 245 | # 0x000000 - rBoot | 246 | # 0x007000 - rBoot cfg | 247 | # 0x008000 - app (444K) | 248 | # 0x077000 - fs (16K) | 249 | # 0x07b000 - rf cal | 250 | # 0x07c000 - sys params | 251 | MGOS_ROOT_FS_SIZE ?= 16384 252 | ROM_SIZE = 0x6F000 253 | APP0_ADDR = 0x08000 254 | FS0_ADDR = 0x77000 255 | APP1_ADDR = 0x108000 256 | FS1_ADDR = 0x1db000 257 | else 258 | ifeq ("$(FLASH_SIZE)", "1048576") 259 | # 260 | # 1MB (8Mb) layout: 261 | # 262 | # 0x000000 - rBoot | 263 | # 0x007000 - rBoot cfg | 264 | # 0x008000 - app (844K) | 265 | # 0xdb0000 - fs (128K) | 266 | # 0x0fb000 - rf cal | 267 | # 0x0fc000 - sys params | 268 | MGOS_ROOT_FS_SIZE ?= 131072 269 | ROM_SIZE = $(shell printf "0x%05x" "$$((0x100000 - $(APP0_ADDR) - $(MGOS_ROOT_FS_SIZE) - $(RF_CAL_DATA_SIZE) - $(SYS_PARAMS_SIZE) - $(IMAGE_HEADER_SIZE)))") 270 | APP0_ADDR = 0x8000 271 | FS0_ADDR = $(shell printf "0x%05x" "$$((0x100000 - $(MGOS_ROOT_FS_SIZE) - $(RF_CAL_DATA_SIZE) - $(SYS_PARAMS_SIZE)))") 272 | APP1_ADDR = 0 273 | FS1_ADDR = 0 274 | else 275 | ifeq ("$(FLASH_SIZE)", "2097152") 276 | # 277 | # 2MB (16Mb) layout 278 | # 279 | # 0x000000 - rBoot | 0x100000 - free (32K) | 280 | # 0x007000 - rBoot cfg | | 281 | # 0x008000 - app0 (844K) | 0x108000 - app1 (844K) | 282 | # 0x0db000 - fs0 (128K) | 0x1db000 - fs1 (128K) | 283 | # 0x0fb000 - free (20K) | 0x1fb000 - rf cal | 284 | # | 0x1fc000 - sys params | 285 | MGOS_ROOT_FS_SIZE ?= 131072 286 | ROM_SIZE = $(shell printf "0x%05x" "$$((0x100000 - ($(APP0_ADDR) % 0x100000) - $(MGOS_ROOT_FS_SIZE) - $(RF_CAL_DATA_SIZE) - $(SYS_PARAMS_SIZE) - $(IMAGE_HEADER_SIZE)))") 287 | APP0_ADDR = 0x8000 288 | FS0_ADDR = $(shell printf "0x%05x" "$$((0x100000 - $(MGOS_ROOT_FS_SIZE) - $(RF_CAL_DATA_SIZE) - $(SYS_PARAMS_SIZE)))") 289 | APP1_ADDR = 0x108000 290 | FS1_ADDR = $(shell printf "0x%05x" "$$((0x200000 - $(MGOS_ROOT_FS_SIZE) - $(RF_CAL_DATA_SIZE) - $(SYS_PARAMS_SIZE)))") 291 | else 292 | # 293 | # 4MB (32Mb) or larger layout 294 | # 295 | # 0x000000 - rBoot | 0x100000 - app0 (1024K) | 0x200000 - app1 (1024K) | 0x300000 - fs1 (256K) | 296 | # 0x007000 - rBoot cfg | | | 0x340000 - free(748K) | 297 | # 0x008000 - fs0 (256K) | | | | 298 | # 0x048000 - free (736K) | | | | 299 | # | | | 0x3fb000 - rf cal | 300 | # | | | 0x3fc000 - sys params | 301 | MGOS_ROOT_FS_SIZE ?= 262144 302 | ROM_SIZE = $(shell printf "0x%05x" "$$((0x100000 - $(IMAGE_HEADER_SIZE)))") 303 | FS0_ADDR = 0x8000 304 | FS1_ADDR = 0x300000 305 | APP0_ADDR = 0x100000 306 | APP1_ADDR = 0x200000 307 | endif 308 | endif 309 | endif 310 | 311 | RF_CAL_DATA_INIT = $(FW_STAGING_DIR)/$(FW_RF_CAL_DATA_ADDR).bin 312 | BOOTLOADER_FLAGS = -DFW1_ADDR=$(APP0_ADDR) \ 313 | -DFW2_ADDR=$(APP1_ADDR) \ 314 | -DFW1_FS_ADDR=$(FS0_ADDR) \ 315 | -DFW2_FS_ADDR=$(FS1_ADDR) \ 316 | -DFS_SIZE=$(MGOS_ROOT_FS_SIZE) \ 317 | -DFW_SIZE=$(ROM_SIZE) \ 318 | -DBOOT_CONFIG_ADDR=$(BOOT_CONFIG_ADDR) 319 | 320 | FW_MANIFEST = $(FW_STAGING_DIR)/manifest.json 321 | 322 | INCDIRS = $(addprefix -I,$(IPATH)) -I$(GEN_DIR) 323 | LIBDIRS = $(addprefix -L,$(LPATH)) 324 | 325 | C_CXX_FLAGS += $(MEMORY_FLAGS) $(SHIMS) $(FEATURES) \ 326 | -DFW_SYS_PARAMS_ADDR=$(SYS_PARAMS_ADDR) \ 327 | -DFW_RF_CAL_DATA_ADDR=$(FW_RF_CAL_DATA_ADDR) \ 328 | -DFW_ARCHITECTURE=$(APP_PLATFORM) \ 329 | $(MGOS_CFLAGS) $(CFLAGS_EXTRA) \ 330 | $(HEAP_LOG_FLAGS) $(BOOTLOADER_FLAGS) 331 | 332 | CFLAGS += $(APP_CFLAGS) 333 | CXXFLAGS += $(APP_CXXFLAGS) 334 | 335 | $(BUILD_DIR) $(FW_DIR) $(FW_STAGING_DIR) $(GEN_DIR): 336 | $(vecho) "MKDIR $@" 337 | $(Q) mkdir -p $@ 338 | 339 | MGOS_ADC_MODE_VDD ?= 0 340 | # If MGOS_ADC_MODE_VDD == 1, we want to measure VDD33 internal (system_get_vdd33). 341 | # Then the 107th byte in esp_init_data_default.bin (named as vdd33_const) 342 | # must be set to 255 (0xFF). 343 | # So we change the path to the esp_init_data_default.bin file 344 | ifeq "$(MGOS_ADC_MODE_VDD)" "1" 345 | SDK_INIT_DATA_PATH = $(MGOS_ESP8266_PATH) 346 | else 347 | SDK_INIT_DATA_PATH = $(SDK_PATH) 348 | endif 349 | CFLAGS += -DMGOS_ADC_MODE_VDD=$(MGOS_ADC_MODE_VDD) 350 | 351 | # Main output product: firmware files. 352 | FW_PARTS = boot:addr=$(BOOT_LOADER_ADDR),src=$(BOOT_LOADER_BIN),update=$(MGOS_UPDATE_BOOT_LOADER) \ 353 | boot_cfg:addr=$(BOOT_CONFIG_WRITE_ADDR),size=0x1000,fill=0xff \ 354 | fw:addr=$(APP0_ADDR),addr1=$(APP1_ADDR),src=$(APP_BIN) \ 355 | fs:src=$(FS_IMG),type=fs,addr=$(FS0_ADDR),addr1=$(FS1_ADDR),fs_size=$(MGOS_ROOT_FS_SIZE) \ 356 | rf_cal_data:addr=$(FW_RF_CAL_DATA_ADDR),size=$(RF_CAL_DATA_SIZE),fill=0xff \ 357 | sys_params:type=sys_params3,src=$(SDK_INIT_DATA_PATH)/bin/esp_init_data_default_v08.bin,addr=$(SYS_PARAMS_ADDR) \ 358 | $(APP_EXTRA_FW_PARTS) 359 | 360 | include $(MGOS_PATH)/tools/mk/mgos_fw_meta.mk 361 | 362 | all: $(FW_DIR) $(BUILD_DIR) $(GEN_DIR) $(FW_STAGING_DIR) $(FW_ZIP) 363 | 364 | $(FW_MANIFEST): $(BOOT_LOADER_BIN) $(APP_BIN) $(FS_IMG) $(FS_FILES) 365 | 366 | $(APP_BIN): $(APP_ELF) $(BUILD_DIR)/esptool2 367 | $(vecho) "FW $< -> $@" 368 | $(Q) $(ESPTOOL2) $(FW_USER_ARGS) $< $@ $(FW_SECTS) 369 | 370 | # -- Linking. 371 | 372 | # Link the main ELF output file. 373 | BIN_PARTS = $(BUILD_DIR)/esp_cache.c.o $(APP_OBJS) $(FFI_EXPORTS_O) $(BUILD_INFO_O) $(MG_BUILD_INFO_O) $(MGOS_LIB) $(APP_BIN_LIB_FILES) 374 | $(APP_ELF): $(BIN_PARTS) $(LD_SCRIPT) 375 | ifeq "$(MGOS_ESP8266_RTOS)" "1" 376 | $(OBJCOPY) --weaken-symbol printf --weaken-symbol puts /opt/Espressif/esp-open-sdk/xtensa-lx106-elf/xtensa-lx106-elf/lib/libc.a 377 | endif 378 | $(call link,$(BIN_PARTS) $(LIBS) $(LD_WRAPPERS)) 379 | 380 | $(BUILD_DIR)/dummy_main.c: $(APP_OBJS) $(MGOS_ESP8266_PATH)/Makefile.build 381 | $(vecho) "GEN $@" 382 | $(Q) echo "void user_init(void){}; " \ 383 | "void system_restart_local(){};" \ 384 | "void __wrap_user_fatal_exception_handler(){}; " \ 385 | "void __cyg_profile_func_enter(void *this_fn, void *call_site){(void) this_fn; (void) call_site;}" \ 386 | "void __cyg_profile_func_exit(void *this_fn, void *call_site){(void) this_fn; (void) call_site;}" > $@ 387 | 388 | $(MGOS_LIB): $(MGOS_OBJS) 389 | $(vecho) "AR $@" 390 | $(Q) $(AR) cru $@ $^ 391 | 392 | # lib.a, used during lib release build. 393 | $(BUILD_DIR)/$(BUILD_LIB_NAME): $(BUILD_DIR) $(APP_OBJS) 394 | $(vecho) "AR $@" 395 | $(AR) cru $@ $(APP_OBJS) 396 | 397 | # This rule is for normal (pre-existing) C sources from VPATH. 398 | $(BUILD_DIR)/%.c.o: %.c $(GENFILES_FLAG) 399 | $(compile) 400 | 401 | # This rule is for normal (pre-existing) C++ sources from VPATH. 402 | $(BUILD_DIR)/%.cpp.o: %.cpp $(GENFILES_FLAG) 403 | $(compile_cxx) 404 | 405 | # Assembly sources. 406 | $(BUILD_DIR)/%.S.o: %.S $(GENFILES_FLAG) 407 | $(vecho) "AS $< -> $@" 408 | $(Q) $(CC) -c -mtext-section-literals $(MGOS_CFLAGS) -o $@ $< 409 | 410 | # This one is for generated sources in build/gen directory. 411 | $(BUILD_DIR)/%.c.o: $(GEN_DIR)/%.c $(GENFILES_FLAG) 412 | $(compile) 413 | 414 | # Common gathering point for all generated files. 415 | # Except build info, which is special because it depends on objects. 416 | $(GENFILES_FLAG): $(GENFILES_LIST) 417 | $(Q) touch $@ 418 | 419 | # Generate linker script. 420 | $(LD_SCRIPT): $(LD_SCRIPT_TEMPL) 421 | $(eval _IROM0ADDR=$(shell printf "0x%08x" $$((0x40200000 + ($(basename $(notdir $@)) % 0x100000) + $(IMAGE_HEADER_SIZE))))) 422 | $(eval _IROM0SIZE=$(ROM_SIZE)) 423 | $(vecho) "GEN $^ ($(_IROM0SIZE) @ $(_IROM0ADDR)) -> $@" 424 | $(Q) mkdir -p $(GEN_DIR) 425 | $(Q) sed -e 's/_IROM0ADDR/$(_IROM0ADDR)/; s/_IROM0SIZE/$(_IROM0SIZE)/' $^ > $@ 426 | 427 | include $(MGOS_PATH)/tools/mk/mgos_build_info.mk 428 | include $(MGOS_PATH)/tools/mk/mgos_ffi_exports.mk 429 | include $(MGOS_PATH)/tools/mk/mgos_config.mk 430 | include $(MGOS_PATH)/tools/mk/mgos_mkfs.mk 431 | 432 | $(MGOS_CONFIG_C): $(MANIFEST_FINAL) 433 | 434 | $(FFI_EXPORTS_C): $(APP_FS_FILES) 435 | $(call gen_ffi_exports,$@,$(FFI_SYMBOLS),$(filter %.js,$(FS_FILES))) 436 | 437 | # In ffi exports file we use fake signatures: void func(void), and it conflicts 438 | # with the builtin functions like fopen, etc. 439 | $(FFI_EXPORTS_O): CFLAGS += -fno-builtin 440 | 441 | # Regenerate build info if there are changes in objects. 442 | $(BUILD_INFO_C) $(BUILD_INFO_JSON): $(APP_OBJS) $(MGOS_OBJS) 443 | $(call gen_build_info,$@,$(APP_PATH),$(APP_BUILD_ID),$(APP_VERSION),,$(BUILD_INFO_C),$(BUILD_INFO_JSON)) 444 | 445 | $(MG_BUILD_INFO_C): $(MGOS_OBJS) 446 | $(call gen_build_info,$@,$(MGOS_PATH)/fw,,,mg_,$(MG_BUILD_INFO_C),) 447 | 448 | # Pull in auto-generated .c dependencies (if any). 449 | -include $(wildcard $(BUILD_DIR)/*.d) 450 | 451 | # -- Filesystem. 452 | 453 | $(FS_IMG): $(MKFS) $(APP_FS_DIRS) $(FS_FILES) 454 | $(call mkfs,$(MGOS_ROOT_FS_SIZE),$(MGOS_ROOT_FS_OPTS)) 455 | 456 | # 457 | # Auxiliary targets. 458 | # 459 | 460 | $(ESPTOOL2): 461 | $(Q) $(MAKE) --no-print-directory -C $(BOOTLOADER_PATH)/esptool2 BUILD_DIR=$(abspath $(BUILD_DIR)) 462 | 463 | $(BOOT_LOADER_BIN): $(ESPTOOL2) $(GENFILES_LIST) 464 | $(Q) rm -rf $(RBOOT_BUILD_DIR) && mkdir -p $(RBOOT_BUILD_DIR) 465 | $(Q) $(MAKE) --no-print-directory -C $(BOOTLOADER_PATH)/rboot -f rboot.mk \ 466 | RBOOT_BUILD_BASE=$(abspath $(RBOOT_BUILD_DIR)) \ 467 | RBOOT_GEN_BASE=$(abspath $(GEN_DIR)) \ 468 | ESPTOOL2=$(abspath $(ESPTOOL2)) SDK_BASE=$(SDK_PATH) \ 469 | RBOOT_BIG_FLASH=1 CFLAGS_EXTRA="$(BOOTLOADER_FLAGS)" 470 | 471 | clean: 472 | $(Q) rm -rf $(BUILD_DIR) $(FW_DIR) 473 | -------------------------------------------------------------------------------- /patch/core/mos.yml: -------------------------------------------------------------------------------- 1 | author: mongoose-os 2 | description: Core meta-lib 3 | type: lib 4 | version: 1.0 5 | 6 | sources: 7 | - src 8 | - src/${platform} 9 | 10 | includes: 11 | - include 12 | - include/${platform} 13 | 14 | libs: 15 | - origin: https://github.com/mongoose-os-libs/mongoose 16 | 17 | init_after: 18 | - lwip 19 | - ota-common 20 | - vfs-* 21 | 22 | tags: 23 | - c 24 | - core 25 | - docs:core:Core meta-library 26 | 27 | config_schema: 28 | - ["device", "o", {title: "Device settings"}] 29 | - ["device.id", "s", {title: "Device ID"}] # Note: Each arch defines a default 30 | - ["device.license", "s", {title: "License code"}] 31 | - ["device.mac", "s", "", {title: "user/custom defined mac address"}] 32 | - ["device.public_key", "s", "", {title: "Key for the restricted access"}] 33 | - ["device.sn", "s", "", {title: "Serial number"}] 34 | 35 | - ["debug", "o", {title: "Debug Settings"}] 36 | - ["debug.level", "i", 2, {title: "Debug Level"}] 37 | - ["debug.file_level", "s", "", {title: "Log file level specification: file=level,file=level,...; see cs_log_set_file_level()"}] 38 | - ["debug.event_level", "i", 2, {title: "Max level for which a MGOS_EVENT_LOG is raised"}] 39 | - ["debug.stdout_uart", "i", 0, {title: "STDOUT UART (-1 to disable)"}] 40 | - ["debug.stderr_uart", "i", 0, {title: "STDERR UART (-1 to disable)"}] 41 | - ["debug.factory_reset_gpio", "i", -1, {title: "Factory reset GPIO (low on boot)"}] 42 | - ["debug.mg_mgr_hexdump_file", "s", {title: "File name to hexdump network traffic to. Use '-' for stdout, '--' for stderr."}] 43 | 44 | - ["sys", "o", {title: "System settings"}] 45 | # https://users.pja.edu.pl/~jms/qnx/help/watcom/clibref/global_data.html 46 | # https://github.com/G6EJD/ESP32-Time-Services-and-SETENV-variable/blob/master/ESP32_Time_and_SETENV.ino 47 | # Example: EST5EDT4,M4.1.0/02:00:00,M10.5.0/02:00:00 48 | # Eastern Standard Time is 5 hours earlier than Coordinated Universal Time (UTC). 49 | # Standard time and daylight savings time both apply to this locale. 50 | # Eastern Daylight Time (EDT) is one hour ahead of standard time. 51 | # Daylight saving time starts on the first (1) Sunday (0) of April (4) at 2:00 A.M., and ends on the last (5) Sunday (0) of October (10) at 2:00 A.M. 52 | - ["sys.tz_spec", "s", "", {title: "See formats for the TZ env var: \"man tzset\". Formats like \":/path/to/file\" are not supported"}] 53 | 54 | - ["sys.wdt_timeout", "i", 30, {title: "Watchdog timeout (seconds)"}] 55 | - ["sys.pref_ota_lib", "s", {title: "Preferred ota lib, e.g. dash, ota-http-client"}] 56 | 57 | - ["conf_acl", "s", "*", {title: "Conf ACL"}] 58 | 59 | conds: 60 | - when: mos.platform == "cc3200" 61 | apply: 62 | libs: 63 | - origin: https://github.com/mongoose-os-libs/freertos 64 | - origin: https://github.com/mongoose-os-libs/vfs-fs-spiffs 65 | build_vars: 66 | FREERTOS_PORT: CCS/ARM_CM3 67 | 68 | - when: mos.platform == "cc3220" 69 | apply: 70 | libs: 71 | - origin: https://github.com/mongoose-os-libs/freertos 72 | - origin: https://github.com/mongoose-os-libs/vfs-fs-spiffs 73 | build_vars: 74 | FREERTOS_PORT: CCS/ARM_CM3 75 | 76 | - when: mos.platform == "esp32" 77 | apply: 78 | libs: 79 | - origin: https://github.com/mongoose-os-libs/freertos 80 | - origin: https://github.com/mongoose-os-libs/vfs-fs-lfs 81 | - origin: https://github.com/mongoose-os-libs/vfs-fs-spiffs 82 | build_vars: 83 | # FreeRTOS option to enable statically allocated task stacks. 84 | ESP_IDF_SDKCONFIG_OPTS: > 85 | ${build_vars.ESP_IDF_SDKCONFIG_OPTS} 86 | CONFIG_FREERTOS_SUPPORT_STATIC_ALLOCATION=y 87 | 88 | - when: mos.platform == "esp8266" 89 | apply: 90 | libs: 91 | - origin: https://github.com/mongoose-os-libs/vfs-dev-part 92 | #- origin: https://github.com/mongoose-os-libs/vfs-fs-lfs 93 | - origin: https://github.com/mongoose-os-libs/vfs-fs-spiffs 94 | config_schema: 95 | - ["device.id", "esp8266_??????"] 96 | 97 | - when: mos.platform == "rs14100" 98 | apply: 99 | libs: 100 | - origin: https://github.com/mongoose-os-libs/freertos 101 | - origin: https://github.com/mongoose-os-libs/vfs-dev-part 102 | - origin: https://github.com/mongoose-os-libs/vfs-fs-lfs 103 | - origin: https://github.com/mongoose-os-libs/vfs-fs-spiffs 104 | config_schema: 105 | - ["device.id", "rs14100_??????"] 106 | - ["debug.stdout_uart", 1] 107 | - ["debug.stderr_uart", 1] 108 | - ["rpc.uart.uart_no", 1] 109 | build_vars: 110 | FREERTOS_PORT: GCC/ARM_CM4F 111 | 112 | - when: mos.platform == "stm32" 113 | apply: 114 | libs: 115 | - origin: https://github.com/mongoose-os-libs/freertos 116 | config_schema: 117 | - ["device.id", "stm32_??????"] 118 | 119 | manifest_version: 2018-06-12 120 | -------------------------------------------------------------------------------- /patch/rboot/license.txt: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015 Richard A Burton (richardaburton@gmail.com) 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in 13 | all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | THE SOFTWARE. 22 | -------------------------------------------------------------------------------- /patch/rboot/rboot.c: -------------------------------------------------------------------------------- 1 | ////////////////////////////////////////////////// 2 | // rBoot open source boot loader for ESP8266. 3 | // Copyright 2015 Richard A Burton 4 | // richardaburton@gmail.com 5 | // See license.txt for license terms. 6 | ////////////////////////////////////////////////// 7 | /* clang-format off */ 8 | 9 | #ifdef RBOOT_INTEGRATION 10 | #include 11 | #endif 12 | 13 | #include "rboot-private.h" 14 | #include 15 | 16 | static uint32 check_image(uint32 readpos) { 17 | 18 | uint8 buffer[BUFFER_SIZE]; 19 | uint8 sectcount; 20 | uint8 sectcurrent; 21 | uint8 *writepos; 22 | uint8 chksum = CHKSUM_INIT; 23 | uint32 loop; 24 | uint32 remaining; 25 | uint32 romaddr; 26 | 27 | rom_header_new *header = (rom_header_new*)buffer; 28 | section_header *section = (section_header*)buffer; 29 | 30 | if (readpos == 0 || readpos == 0xffffffff) { 31 | return 0; 32 | } 33 | 34 | // read rom header 35 | if (SPIRead(readpos, header, sizeof(rom_header_new)) != 0) { 36 | return 0; 37 | } 38 | 39 | // check header type 40 | if (header->magic == ROM_MAGIC) { 41 | // old type, no extra header or irom section to skip over 42 | romaddr = readpos; 43 | readpos += sizeof(rom_header); 44 | sectcount = header->count; 45 | } else if (header->magic == ROM_MAGIC_NEW1 && header->count == ROM_MAGIC_NEW2) { 46 | // new type, has extra header and irom section first 47 | romaddr = readpos + header->len + sizeof(rom_header_new); 48 | #ifdef BOOT_IROM_CHKSUM 49 | // we will set the real section count later, when we read the header 50 | sectcount = 0xff; 51 | // just skip the first part of the header 52 | // rest is processed for the chksum 53 | readpos += sizeof(rom_header); 54 | #else 55 | // skip the extra header and irom section 56 | readpos = romaddr; 57 | // read the normal header that follows 58 | if (SPIRead(readpos, header, sizeof(rom_header)) != 0) { 59 | return 0; 60 | } 61 | sectcount = header->count; 62 | readpos += sizeof(rom_header); 63 | #endif 64 | } else { 65 | return 0; 66 | } 67 | 68 | // test each section 69 | for (sectcurrent = 0; sectcurrent < sectcount; sectcurrent++) { 70 | 71 | // read section header 72 | if (SPIRead(readpos, section, sizeof(section_header)) != 0) { 73 | return 0; 74 | } 75 | readpos += sizeof(section_header); 76 | 77 | // get section address and length 78 | writepos = section->address; 79 | remaining = section->length; 80 | 81 | while (remaining > 0) { 82 | // work out how much to read, up to BUFFER_SIZE 83 | uint32 readlen = (remaining < BUFFER_SIZE) ? remaining : BUFFER_SIZE; 84 | // read the block 85 | if (SPIRead(readpos, buffer, readlen) != 0) { 86 | return 0; 87 | } 88 | // increment next read and write positions 89 | readpos += readlen; 90 | writepos += readlen; 91 | // decrement remaining count 92 | remaining -= readlen; 93 | // add to chksum 94 | for (loop = 0; loop < readlen; loop++) { 95 | chksum ^= buffer[loop]; 96 | } 97 | } 98 | 99 | #ifdef BOOT_IROM_CHKSUM 100 | if (sectcount == 0xff) { 101 | // just processed the irom section, now 102 | // read the normal header that follows 103 | if (SPIRead(readpos, header, sizeof(rom_header)) != 0) { 104 | return 0; 105 | } 106 | sectcount = header->count + 1; 107 | readpos += sizeof(rom_header); 108 | } 109 | #endif 110 | } 111 | 112 | // round up to next 16 and get checksum 113 | readpos = readpos | 0x0f; 114 | if (SPIRead(readpos, buffer, 1) != 0) { 115 | return 0; 116 | } 117 | 118 | // compare calculated and stored checksums 119 | if (buffer[0] != chksum) { 120 | return 0; 121 | } 122 | 123 | return romaddr; 124 | } 125 | 126 | #define ETS_UNCACHED_ADDR(addr) (addr) 127 | #define READ_PERI_REG(addr) (*((volatile uint32 *)ETS_UNCACHED_ADDR(addr))) 128 | #define WRITE_PERI_REG(addr, val) (*((volatile uint32 *)ETS_UNCACHED_ADDR(addr))) = (uint32)(val) 129 | #define PERIPHS_RTC_BASEADDR 0x60000700 130 | #define REG_RTC_BASE PERIPHS_RTC_BASEADDR 131 | #define RTC_GPIO_OUT (REG_RTC_BASE + 0x068) 132 | #define RTC_GPIO_ENABLE (REG_RTC_BASE + 0x074) 133 | #define RTC_GPIO_IN_DATA (REG_RTC_BASE + 0x08C) 134 | #define RTC_GPIO_CONF (REG_RTC_BASE + 0x090) 135 | #define PAD_XPD_DCDC_CONF (REG_RTC_BASE + 0x0A0) 136 | static uint32 get_gpio16(void) { 137 | // set output level to 1 138 | WRITE_PERI_REG(RTC_GPIO_OUT, (READ_PERI_REG(RTC_GPIO_OUT) & (uint32)0xfffffffe) | (uint32)(1)); 139 | 140 | // read level 141 | WRITE_PERI_REG(PAD_XPD_DCDC_CONF, (READ_PERI_REG(PAD_XPD_DCDC_CONF) & 0xffffffbc) | (uint32)0x1); // mux configuration for XPD_DCDC and rtc_gpio0 connection 142 | WRITE_PERI_REG(RTC_GPIO_CONF, (READ_PERI_REG(RTC_GPIO_CONF) & (uint32)0xfffffffe) | (uint32)0x0); //mux configuration for out enable 143 | WRITE_PERI_REG(RTC_GPIO_ENABLE, READ_PERI_REG(RTC_GPIO_ENABLE) & (uint32)0xfffffffe); //out disable 144 | 145 | uint32 x = (READ_PERI_REG(RTC_GPIO_IN_DATA) & 1); 146 | 147 | return x; 148 | } 149 | 150 | #ifdef BOOT_CONFIG_CHKSUM 151 | // calculate checksum for block of data 152 | // from start up to (but excluding) end 153 | static uint8 calc_chksum(uint8 *start, uint8 *end) { 154 | uint8 chksum = CHKSUM_INIT; 155 | while(start < end) { 156 | chksum ^= *start; 157 | start++; 158 | } 159 | return chksum; 160 | } 161 | #endif 162 | 163 | #define UART_CLKDIV_80MHZ(B) (80000000 + B / 2) / B 164 | #define UART_CLKDIV_52MHZ(B) (52000000 + B / 2) / B 165 | 166 | // prevent this function being placed inline with main 167 | // to keep main's stack size as small as possible 168 | // don't mark as static or it'll be optimised out when 169 | // using the assembler stub 170 | uint32 NOINLINE find_image(void) { 171 | 172 | uint8 flag; 173 | uint32 runAddr; 174 | uint32 flashsize; 175 | int32 romToBoot; 176 | uint8 gpio_boot = FALSE; 177 | uint8 updateConfig = FALSE; 178 | uint8 buffer[SECTOR_SIZE]; 179 | 180 | rboot_config *romconf = (rboot_config*)buffer; 181 | rom_header *header = (rom_header*)buffer; 182 | 183 | // delay to slow boot (help see messages when debugging) 184 | //ets_delay_us(2000000); 185 | 186 | /* 187 | * UART divider depends on the APB frequency. Cold boot starts with 52MHz APB, 188 | * SDK configures PLL, sets it to 80 and it persists across soft reset 189 | * so on soft reset a different divider should be used. What we really want to 190 | * know is if the PLL is running but since that is completely undocumented 191 | * we use CPU frequency bit as a workaround: mos sets CPU to 160 MHz on startup 192 | * so we assume that if frequency is preserved, then it's soft reset, PLL is 193 | * running and APB is at 80 MHz. 194 | */ 195 | if (READ_PERI_REG(0x3ff00014) & 1) { 196 | uart_div_modify(0, UART_CLKDIV_80MHZ(115200)); 197 | } else { 198 | uart_div_modify(0, UART_CLKDIV_52MHZ(115200)); 199 | } 200 | ets_delay_us(1000); 201 | 202 | ets_printf("\r\nrBoot v1.2.1-cesanta1-yaourdt - richardaburton@gmail.com\r\n"); 203 | 204 | // read rom header 205 | SPIRead(0, header, sizeof(rom_header)); 206 | 207 | // print and get flash size 208 | ets_printf("Flash Size: "); 209 | flag = header->flags2 >> 4; 210 | if (flag == 0) { 211 | ets_printf("4 Mbit\r\n"); 212 | flashsize = 0x80000; 213 | } else if (flag == 1) { 214 | ets_printf("2 Mbit\r\n"); 215 | flashsize = 0x40000; 216 | } else if (flag == 2) { 217 | ets_printf("8 Mbit\r\n"); 218 | flashsize = 0x100000; 219 | } else { 220 | #ifdef BOOT_BIG_FLASH 221 | if (flag == 3) { 222 | ets_printf("16 Mbit\r\n"); 223 | flashsize = 0x200000; 224 | } else if (flag == 4) { 225 | ets_printf("32 Mbit\r\n"); 226 | flashsize = 0x400000; 227 | } else if (flag == 8) { 228 | ets_printf("64 Mbit\r\n"); 229 | flashsize = 0x800000; 230 | } else if (flag == 9) { 231 | ets_printf("128 Mbit\r\n"); 232 | flashsize = 0x1000000; 233 | } else { 234 | ets_printf("unknown\r\n"); 235 | flashsize = 0x100000; // assume 8Mbit 236 | } 237 | #else 238 | ets_printf("8 Mbit\r\n"); 239 | flashsize = 0x100000; // limit to 8Mbit 240 | #endif 241 | } 242 | 243 | // print spi mode 244 | ets_printf("Flash Mode: "); 245 | if (header->flags1 == 0) { 246 | ets_printf("QIO\r\n"); 247 | } else if (header->flags1 == 1) { 248 | ets_printf("QOUT\r\n"); 249 | } else if (header->flags1 == 2) { 250 | ets_printf("DIO\r\n"); 251 | } else if (header->flags1 == 3) { 252 | ets_printf("DOUT\r\n"); 253 | } else { 254 | ets_printf("unknown\r\n"); 255 | } 256 | 257 | // print spi speed 258 | ets_printf("Flash Speed: "); 259 | flag = header->flags2 & 0x0f; 260 | if (flag == 0) ets_printf("40 MHz\r\n"); 261 | else if (flag == 1) ets_printf("26.7 MHz\r\n"); 262 | else if (flag == 2) ets_printf("20 MHz\r\n"); 263 | else if (flag == 0x0f) ets_printf("80 MHz\r\n"); 264 | else ets_printf("unknown\r\n"); 265 | 266 | // print enabled options 267 | #ifdef BOOT_BIG_FLASH 268 | ets_printf("rBoot Option: Big flash\r\n"); 269 | #endif 270 | #ifdef BOOT_CONFIG_CHKSUM 271 | ets_printf("rBoot Option: Config chksum\r\n"); 272 | #endif 273 | #ifdef BOOT_IROM_CHKSUM 274 | ets_printf("rBoot Option: irom chksum\r\n"); 275 | #endif 276 | #ifdef RBOOT_MV_SDKCONFIG_ENABLED 277 | ets_printf("rBoot Option: move esp init data\r\n"); 278 | #endif 279 | 280 | ets_printf("\r\n"); 281 | 282 | // read boot config 283 | SPIRead(BOOT_CONFIG_SECTOR * SECTOR_SIZE, buffer, SECTOR_SIZE); 284 | // fresh install or old version? 285 | if (romconf->magic != BOOT_CONFIG_MAGIC || romconf->version != BOOT_CONFIG_VERSION 286 | #ifdef BOOT_CONFIG_CHKSUM 287 | || romconf->chksum != calc_chksum((uint8*)romconf, (uint8*)&romconf->chksum) 288 | #endif 289 | ) { 290 | /* Modified by Cesanta */ 291 | ets_printf("Writing default boot config @ 0x%x.\r\n", BOOT_CONFIG_SECTOR * SECTOR_SIZE); 292 | ets_memset(romconf, 0x00, sizeof(rboot_config)); 293 | romconf->magic = BOOT_CONFIG_MAGIC; 294 | romconf->version = BOOT_CONFIG_VERSION; 295 | romconf->count = 2; 296 | romconf->mode = MODE_STANDARD; 297 | /* FWx_ADDR, FWx_FS_ADDR and FS_SIZE, FW_SIZE must be defined by -D */ 298 | romconf->roms[0] = FW1_ADDR; 299 | romconf->roms[1] = FW2_ADDR; 300 | romconf->fs_addresses[0] = FW1_FS_ADDR; 301 | romconf->fs_addresses[1] = FW2_FS_ADDR; 302 | romconf->fs_sizes[0] = romconf->fs_sizes[1] = FS_SIZE; 303 | romconf->roms_sizes[0] = romconf->roms_sizes[1] = FW_SIZE; 304 | #ifdef BOOT_CONFIG_CHKSUM 305 | romconf->chksum = calc_chksum((uint8*)romconf, (uint8*)&romconf->chksum); 306 | #endif 307 | #ifdef RBOOT_MV_SDKCONFIG_ENABLED 308 | romconf->sdk_config_source = flashsize; 309 | #endif 310 | // write new config sector 311 | SPIEraseSector(BOOT_CONFIG_SECTOR); 312 | SPIWrite(BOOT_CONFIG_SECTOR * SECTOR_SIZE, buffer, SECTOR_SIZE); 313 | } 314 | 315 | #ifdef RBOOT_MV_SDKCONFIG_ENABLED 316 | if (romconf->sdk_config_source != flashsize) { 317 | ets_printf("Moving SDK config sectors before booting.\r\n"); 318 | uint8 data[SECTOR_SIZE]; 319 | for (int sec = 1; sec < 5; sec++) { 320 | if ( SPIRead(romconf->sdk_config_source - (SECTOR_SIZE * sec), data, SECTOR_SIZE) != 0 ) { 321 | break; 322 | } 323 | SPIEraseSector((flashsize / SECTOR_SIZE) - sec); 324 | SPIWrite(flashsize - (SECTOR_SIZE * sec), data, SECTOR_SIZE); 325 | } 326 | romconf->sdk_config_source = flashsize; 327 | } 328 | #endif 329 | 330 | // if gpio mode enabled check status of the gpio 331 | if ((romconf->mode & MODE_GPIO_ROM) && (get_gpio16() == 0)) { 332 | ets_printf("Booting GPIO-selected.\r\n"); 333 | romToBoot = romconf->previous_rom; 334 | /* 335 | * Modified by Cesanta 336 | * Make FD current 337 | */ 338 | updateConfig = TRUE; 339 | romconf->fw_updated = 0; 340 | romconf->is_first_boot = 0; 341 | gpio_boot = TRUE; 342 | } else if (romconf->current_rom >= romconf->count) { 343 | // if invalid rom selected try rom 0 344 | ets_printf("Invalid rom selected, defaulting.\r\n"); 345 | romToBoot = 0; 346 | romconf->current_rom = 0; 347 | romconf->fw_updated = 0; 348 | romconf->is_first_boot = 0; 349 | updateConfig = TRUE; 350 | } else { 351 | /* Modified by Cesanta */ 352 | if (romconf->is_first_boot != 0) { 353 | ets_printf("First boot, attempt %d\n", romconf->boot_attempts); 354 | /* boot is unconfirmed */ 355 | if (romconf->boot_attempts == 0) { 356 | /* haven't try to load yes */ 357 | ets_printf("Boot is unconfirmed\r\n"); 358 | romconf->boot_attempts++; 359 | } else { 360 | ets_printf("Boot failed, fallback to fw #%d\r\n", 361 | romconf->previous_rom); 362 | romconf->current_rom = romconf->previous_rom; 363 | /* clear fw update flag, to avoid post-update acttions */ 364 | romconf->fw_updated = 0; 365 | romconf->boot_attempts = 0; 366 | } 367 | 368 | updateConfig = TRUE; 369 | } 370 | /* End of Cesanta modifications */ 371 | // try rom selected in the config 372 | romToBoot = romconf->current_rom; 373 | } 374 | 375 | // try to find a good rom 376 | do { 377 | runAddr = check_image(romconf->roms[romToBoot]); 378 | if (runAddr == 0) { 379 | ets_printf("Rom %d is bad.\r\n", romToBoot); 380 | if (gpio_boot) { 381 | // don't switch to backup for gpio-selected rom 382 | ets_printf("GPIO boot failed.\r\n"); 383 | return 0; 384 | } else { 385 | // for normal mode try each previous rom 386 | // until we find a good one or run out 387 | updateConfig = TRUE; 388 | romToBoot--; 389 | if (romToBoot < 0) romToBoot = romconf->count - 1; 390 | if (romToBoot == romconf->current_rom) { 391 | // tried them all and all are bad! 392 | ets_printf("No good rom available.\r\n"); 393 | return 0; 394 | } 395 | } 396 | } 397 | } while (runAddr == 0); 398 | 399 | // re-write config, if required 400 | if (updateConfig) { 401 | romconf->current_rom = romToBoot; 402 | #ifdef BOOT_CONFIG_CHKSUM 403 | romconf->chksum = calc_chksum((uint8*)romconf, (uint8*)&romconf->chksum); 404 | #endif 405 | SPIEraseSector(BOOT_CONFIG_SECTOR); 406 | SPIWrite(BOOT_CONFIG_SECTOR * SECTOR_SIZE, buffer, SECTOR_SIZE); 407 | } 408 | 409 | ets_printf("Booting rom %d (0x%x).\r\n", romToBoot, romconf->roms[romToBoot]); 410 | // copy the loader to top of iram 411 | ets_memcpy((void*)_text_addr, _text_data, _text_len); 412 | // return address to load from 413 | return runAddr; 414 | 415 | } 416 | 417 | #ifdef BOOT_NO_ASM 418 | 419 | // small stub method to ensure minimum stack space used 420 | void call_user_start(void) { 421 | uint32 addr; 422 | stage2a *loader; 423 | 424 | addr = find_image(); 425 | if (addr != 0) { 426 | loader = (stage2a*)entry_addr; 427 | loader(addr); 428 | } 429 | } 430 | 431 | #else 432 | 433 | // assembler stub uses no stack space 434 | // works with gcc 435 | void call_user_start(void) { 436 | __asm volatile ( 437 | "mov a15, a0\n" // store return addr, hope nobody wanted a15! 438 | "call0 find_image\n" // find a good rom to boot 439 | "mov a0, a15\n" // restore return addr 440 | "bnez a2, 1f\n" // ?success 441 | "ret\n" // no, return 442 | "1:\n" // yes... 443 | "movi a3, entry_addr\n" // actually gives us a pointer to entry_addr 444 | "l32i a3, a3, 0\n" // now really load entry_addr 445 | "jx a3\n" // now jump to it 446 | ); 447 | } 448 | 449 | #endif 450 | -------------------------------------------------------------------------------- /patch/rboot/rboot.h: -------------------------------------------------------------------------------- 1 | #ifndef CS_COMMON_PLATFORMS_ESP8266_RBOOT_RBOOT_RBOOT_H_ 2 | #define CS_COMMON_PLATFORMS_ESP8266_RBOOT_RBOOT_RBOOT_H_ 3 | 4 | ////////////////////////////////////////////////// 5 | // rBoot open source boot loader for ESP8266. 6 | // Copyright 2015 Richard A Burton 7 | // richardaburton@gmail.com 8 | // See license.txt for license terms. 9 | ////////////////////////////////////////////////// 10 | 11 | #ifdef __cplusplus 12 | extern "C" { 13 | #endif 14 | 15 | // uncomment to use only c code 16 | // if you aren't using gcc you may need to do this 17 | //#define BOOT_NO_ASM 18 | 19 | // uncomment to have a checksum on the boot config 20 | //#define BOOT_CONFIG_CHKSUM 21 | 22 | // uncomment to enable big flash support (>1MB) 23 | //#define BOOT_BIG_FLASH 24 | 25 | // uncomment to include .irom0.text section in the checksum 26 | // roms must be built with esptool2 using -iromchksum option 27 | //#define BOOT_IROM_CHKSUM 28 | 29 | // increase if required 30 | #define MAX_ROMS 4 31 | 32 | #define CHKSUM_INIT 0xef 33 | 34 | #define SECTOR_SIZE 0x1000 35 | 36 | #ifndef BOOT_CONFIG_ADDR 37 | #define BOOT_CONFIG_ADDR 0x1000 38 | #endif 39 | 40 | #define BOOT_CONFIG_SECTOR (BOOT_CONFIG_ADDR / SECTOR_SIZE) 41 | 42 | #define BOOT_CONFIG_MAGIC 0xe1 43 | #define BOOT_CONFIG_VERSION 0x01 44 | 45 | #define MODE_STANDARD 0x00 46 | #define MODE_GPIO_ROM 0x01 47 | 48 | #define RBOOT_MV_SDKCONFIG_ENABLED 1 49 | // boot config structure 50 | // rom addresses must be multiples of 0x1000 (flash sector aligned) 51 | // without BOOT_BIG_FLASH only the first 8Mbit of the chip will be memory mapped 52 | // so rom slots containing .irom0.text sections must remain below 0x100000 53 | // slots beyond this will only be accessible via spi read calls, so 54 | // use these for stored resources, not code 55 | // with BOOT_BIG_FLASH the flash will be mapped in chunks of 8MBit, so roms can 56 | // be anywhere, but must not straddle two 8MBit blocks 57 | typedef struct { 58 | uint8 magic; // our magic 59 | uint8 version; // config struct version 60 | uint8 mode; // boot loader mode 61 | uint8 current_rom; // currently selected rom 62 | uint8 gpio_rom; // rom to use for gpio boot 63 | uint8 count; // number of roms in use 64 | uint8 previous_rom; // previously selected rom 65 | uint8 is_first_boot; 66 | uint8 boot_attempts; 67 | uint8 fw_updated; 68 | uint8 padding[2]; 69 | uint32 roms[MAX_ROMS]; // flash addresses of the roms 70 | uint32 roms_sizes[MAX_ROMS]; // sizes of the roms 71 | uint32 fs_addresses[MAX_ROMS]; // file system addresses 72 | uint32 fs_sizes[MAX_ROMS]; // file system sizes 73 | uint32 user_flags; 74 | #ifdef BOOT_CONFIG_CHKSUM 75 | uint8 chksum; // config chksum 76 | #endif 77 | #ifdef RBOOT_MV_SDKCONFIG_ENABLED 78 | uint32 sdk_config_source; // move esp init data from this address 79 | #endif 80 | } rboot_config; 81 | 82 | #ifdef __cplusplus 83 | } 84 | #endif 85 | 86 | #endif /* CS_COMMON_PLATFORMS_ESP8266_RBOOT_RBOOT_RBOOT_H_ */ 87 | -------------------------------------------------------------------------------- /src/main.c: -------------------------------------------------------------------------------- 1 | #include "mgos.h" 2 | #include "spi_flash.h" 3 | #include "esp_rboot.h" 4 | #define BUTTON_PIN 14 5 | 6 | enum mgos_app_init_result mgos_app_init(void) { 7 | // if skip button is pressed, do not do anything. 8 | // this way the firmware image can be read from the deivce 9 | mgos_gpio_setup_input(BUTTON_PIN, MGOS_GPIO_PULL_UP); 10 | if ( !mgos_gpio_read(BUTTON_PIN) ) { 11 | LOG(LL_INFO, ("Skip button pressed. Will not alter bootloader.")); 12 | return MGOS_APP_INIT_SUCCESS; 13 | } 14 | 15 | // detect devices flash size and adapt image to it 16 | unsigned char *buffer = NULL; 17 | buffer = (unsigned char*) malloc(SECTOR_SIZE); 18 | if ( spi_flash_read(0, (uint32 *) buffer, SECTOR_SIZE) != 0 ) { 19 | LOG(LL_ERROR, ("flash read error! abort.")); 20 | return MGOS_APP_INIT_SUCCESS; 21 | } 22 | 23 | unsigned char *loaded_flash_size = buffer+3; 24 | uint8 true_flash_size = 0; 25 | switch( (1 << ((spi_flash_get_id() >> 16) & 0xFF)) ) { 26 | case 2097152: 27 | true_flash_size = 0x3f; // 0x0f = 512KB; 0x2f = 1MB; 0x3f = 2MB; 0x4f = 4MB 28 | break; 29 | case 4194304: 30 | true_flash_size = 0x4f; 31 | break; 32 | default: 33 | LOG(LL_ERROR, ("flash size %i not supported! abort.", (1 << ((spi_flash_get_id() >> 16) & 0xFF)) )); 34 | return MGOS_APP_INIT_SUCCESS; 35 | } 36 | if (*loaded_flash_size == true_flash_size) { 37 | LOG(LL_INFO, ("All done. Waiting for updates.")); 38 | return MGOS_APP_INIT_SUCCESS; 39 | } 40 | 41 | LOG(LL_INFO, ("Change flash size from %02x to %02x and reboot", *loaded_flash_size, true_flash_size )); 42 | *loaded_flash_size = true_flash_size; 43 | spi_flash_erase_sector(0); 44 | spi_flash_write(0, (uint32 *) buffer, SECTOR_SIZE); 45 | free(buffer); 46 | 47 | // tell rboot to move esp system config at next boot 48 | rboot_config *rboot_cfg; 49 | rboot_cfg = get_rboot_config(); 50 | (*rboot_cfg).sdk_config_source = 524288; 51 | rboot_set_config(rboot_cfg); 52 | 53 | mgos_system_restart_after(200); 54 | return MGOS_APP_INIT_SUCCESS; 55 | } 56 | --------------------------------------------------------------------------------