├── .gitignore ├── COPYING ├── Makefile ├── README.md ├── aml.h ├── amlbootenc-gxl.c ├── amlbootimg-gxl.c ├── amlbootsig-gxl.c ├── amlbootsig.c ├── amlinfo.c ├── fip.h ├── meson.h └── unamlbootsig.c /.gitignore: -------------------------------------------------------------------------------- 1 | amlbootenc-gxl 2 | amlbootimg-gxl 3 | amlbootsig 4 | amlbootsig-gxl 5 | amlinfo 6 | test 7 | unamlbootsig 8 | *.o 9 | -------------------------------------------------------------------------------- /COPYING: -------------------------------------------------------------------------------- 1 | GNU GENERAL PUBLIC LICENSE 2 | Version 2, June 1991 3 | 4 | Copyright (C) 1989, 1991 Free Software Foundation, Inc., 5 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 6 | Everyone is permitted to copy and distribute verbatim copies 7 | of this license document, but changing it is not allowed. 8 | 9 | Preamble 10 | 11 | The licenses for most software are designed to take away your 12 | freedom to share and change it. By contrast, the GNU General Public 13 | License is intended to guarantee your freedom to share and change free 14 | software--to make sure the software is free for all its users. This 15 | General Public License applies to most of the Free Software 16 | Foundation's software and to any other program whose authors commit to 17 | using it. (Some other Free Software Foundation software is covered by 18 | the GNU Lesser General Public License instead.) You can apply it to 19 | your programs, too. 20 | 21 | When we speak of free software, we are referring to freedom, not 22 | price. Our General Public Licenses are designed to make sure that you 23 | have the freedom to distribute copies of free software (and charge for 24 | this service if you wish), that you receive source code or can get it 25 | if you want it, that you can change the software or use pieces of it 26 | in new free programs; and that you know you can do these things. 27 | 28 | To protect your rights, we need to make restrictions that forbid 29 | anyone to deny you these rights or to ask you to surrender the rights. 30 | These restrictions translate to certain responsibilities for you if you 31 | distribute copies of the software, or if you modify it. 32 | 33 | For example, if you distribute copies of such a program, whether 34 | gratis or for a fee, you must give the recipients all the rights that 35 | you have. You must make sure that they, too, receive or can get the 36 | source code. And you must show them these terms so they know their 37 | rights. 38 | 39 | We protect your rights with two steps: (1) copyright the software, and 40 | (2) offer you this license which gives you legal permission to copy, 41 | distribute and/or modify the software. 42 | 43 | Also, for each author's protection and ours, we want to make certain 44 | that everyone understands that there is no warranty for this free 45 | software. If the software is modified by someone else and passed on, we 46 | want its recipients to know that what they have is not the original, so 47 | that any problems introduced by others will not reflect on the original 48 | authors' reputations. 49 | 50 | Finally, any free program is threatened constantly by software 51 | patents. We wish to avoid the danger that redistributors of a free 52 | program will individually obtain patent licenses, in effect making the 53 | program proprietary. To prevent this, we have made it clear that any 54 | patent must be licensed for everyone's free use or not licensed at all. 55 | 56 | The precise terms and conditions for copying, distribution and 57 | modification follow. 58 | 59 | GNU GENERAL PUBLIC LICENSE 60 | TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION 61 | 62 | 0. This License applies to any program or other work which contains 63 | a notice placed by the copyright holder saying it may be distributed 64 | under the terms of this General Public License. The "Program", below, 65 | refers to any such program or work, and a "work based on the Program" 66 | means either the Program or any derivative work under copyright law: 67 | that is to say, a work containing the Program or a portion of it, 68 | either verbatim or with modifications and/or translated into another 69 | language. (Hereinafter, translation is included without limitation in 70 | the term "modification".) Each licensee is addressed as "you". 71 | 72 | Activities other than copying, distribution and modification are not 73 | covered by this License; they are outside its scope. The act of 74 | running the Program is not restricted, and the output from the Program 75 | is covered only if its contents constitute a work based on the 76 | Program (independent of having been made by running the Program). 77 | Whether that is true depends on what the Program does. 78 | 79 | 1. You may copy and distribute verbatim copies of the Program's 80 | source code as you receive it, in any medium, provided that you 81 | conspicuously and appropriately publish on each copy an appropriate 82 | copyright notice and disclaimer of warranty; keep intact all the 83 | notices that refer to this License and to the absence of any warranty; 84 | and give any other recipients of the Program a copy of this License 85 | along with the Program. 86 | 87 | You may charge a fee for the physical act of transferring a copy, and 88 | you may at your option offer warranty protection in exchange for a fee. 89 | 90 | 2. You may modify your copy or copies of the Program or any portion 91 | of it, thus forming a work based on the Program, and copy and 92 | distribute such modifications or work under the terms of Section 1 93 | above, provided that you also meet all of these conditions: 94 | 95 | a) You must cause the modified files to carry prominent notices 96 | stating that you changed the files and the date of any change. 97 | 98 | b) You must cause any work that you distribute or publish, that in 99 | whole or in part contains or is derived from the Program or any 100 | part thereof, to be licensed as a whole at no charge to all third 101 | parties under the terms of this License. 102 | 103 | c) If the modified program normally reads commands interactively 104 | when run, you must cause it, when started running for such 105 | interactive use in the most ordinary way, to print or display an 106 | announcement including an appropriate copyright notice and a 107 | notice that there is no warranty (or else, saying that you provide 108 | a warranty) and that users may redistribute the program under 109 | these conditions, and telling the user how to view a copy of this 110 | License. (Exception: if the Program itself is interactive but 111 | does not normally print such an announcement, your work based on 112 | the Program is not required to print an announcement.) 113 | 114 | These requirements apply to the modified work as a whole. If 115 | identifiable sections of that work are not derived from the Program, 116 | and can be reasonably considered independent and separate works in 117 | themselves, then this License, and its terms, do not apply to those 118 | sections when you distribute them as separate works. But when you 119 | distribute the same sections as part of a whole which is a work based 120 | on the Program, the distribution of the whole must be on the terms of 121 | this License, whose permissions for other licensees extend to the 122 | entire whole, and thus to each and every part regardless of who wrote it. 123 | 124 | Thus, it is not the intent of this section to claim rights or contest 125 | your rights to work written entirely by you; rather, the intent is to 126 | exercise the right to control the distribution of derivative or 127 | collective works based on the Program. 128 | 129 | In addition, mere aggregation of another work not based on the Program 130 | with the Program (or with a work based on the Program) on a volume of 131 | a storage or distribution medium does not bring the other work under 132 | the scope of this License. 133 | 134 | 3. You may copy and distribute the Program (or a work based on it, 135 | under Section 2) in object code or executable form under the terms of 136 | Sections 1 and 2 above provided that you also do one of the following: 137 | 138 | a) Accompany it with the complete corresponding machine-readable 139 | source code, which must be distributed under the terms of Sections 140 | 1 and 2 above on a medium customarily used for software interchange; or, 141 | 142 | b) Accompany it with a written offer, valid for at least three 143 | years, to give any third party, for a charge no more than your 144 | cost of physically performing source distribution, a complete 145 | machine-readable copy of the corresponding source code, to be 146 | distributed under the terms of Sections 1 and 2 above on a medium 147 | customarily used for software interchange; or, 148 | 149 | c) Accompany it with the information you received as to the offer 150 | to distribute corresponding source code. (This alternative is 151 | allowed only for noncommercial distribution and only if you 152 | received the program in object code or executable form with such 153 | an offer, in accord with Subsection b above.) 154 | 155 | The source code for a work means the preferred form of the work for 156 | making modifications to it. For an executable work, complete source 157 | code means all the source code for all modules it contains, plus any 158 | associated interface definition files, plus the scripts used to 159 | control compilation and installation of the executable. However, as a 160 | special exception, the source code distributed need not include 161 | anything that is normally distributed (in either source or binary 162 | form) with the major components (compiler, kernel, and so on) of the 163 | operating system on which the executable runs, unless that component 164 | itself accompanies the executable. 165 | 166 | If distribution of executable or object code is made by offering 167 | access to copy from a designated place, then offering equivalent 168 | access to copy the source code from the same place counts as 169 | distribution of the source code, even though third parties are not 170 | compelled to copy the source along with the object code. 171 | 172 | 4. You may not copy, modify, sublicense, or distribute the Program 173 | except as expressly provided under this License. Any attempt 174 | otherwise to copy, modify, sublicense or distribute the Program is 175 | void, and will automatically terminate your rights under this License. 176 | However, parties who have received copies, or rights, from you under 177 | this License will not have their licenses terminated so long as such 178 | parties remain in full compliance. 179 | 180 | 5. You are not required to accept this License, since you have not 181 | signed it. However, nothing else grants you permission to modify or 182 | distribute the Program or its derivative works. These actions are 183 | prohibited by law if you do not accept this License. Therefore, by 184 | modifying or distributing the Program (or any work based on the 185 | Program), you indicate your acceptance of this License to do so, and 186 | all its terms and conditions for copying, distributing or modifying 187 | the Program or works based on it. 188 | 189 | 6. Each time you redistribute the Program (or any work based on the 190 | Program), the recipient automatically receives a license from the 191 | original licensor to copy, distribute or modify the Program subject to 192 | these terms and conditions. You may not impose any further 193 | restrictions on the recipients' exercise of the rights granted herein. 194 | You are not responsible for enforcing compliance by third parties to 195 | this License. 196 | 197 | 7. If, as a consequence of a court judgment or allegation of patent 198 | infringement or for any other reason (not limited to patent issues), 199 | conditions are imposed on you (whether by court order, agreement or 200 | otherwise) that contradict the conditions of this License, they do not 201 | excuse you from the conditions of this License. If you cannot 202 | distribute so as to satisfy simultaneously your obligations under this 203 | License and any other pertinent obligations, then as a consequence you 204 | may not distribute the Program at all. For example, if a patent 205 | license would not permit royalty-free redistribution of the Program by 206 | all those who receive copies directly or indirectly through you, then 207 | the only way you could satisfy both it and this License would be to 208 | refrain entirely from distribution of the Program. 209 | 210 | If any portion of this section is held invalid or unenforceable under 211 | any particular circumstance, the balance of the section is intended to 212 | apply and the section as a whole is intended to apply in other 213 | circumstances. 214 | 215 | It is not the purpose of this section to induce you to infringe any 216 | patents or other property right claims or to contest validity of any 217 | such claims; this section has the sole purpose of protecting the 218 | integrity of the free software distribution system, which is 219 | implemented by public license practices. Many people have made 220 | generous contributions to the wide range of software distributed 221 | through that system in reliance on consistent application of that 222 | system; it is up to the author/donor to decide if he or she is willing 223 | to distribute software through any other system and a licensee cannot 224 | impose that choice. 225 | 226 | This section is intended to make thoroughly clear what is believed to 227 | be a consequence of the rest of this License. 228 | 229 | 8. If the distribution and/or use of the Program is restricted in 230 | certain countries either by patents or by copyrighted interfaces, the 231 | original copyright holder who places the Program under this License 232 | may add an explicit geographical distribution limitation excluding 233 | those countries, so that distribution is permitted only in or among 234 | countries not thus excluded. In such case, this License incorporates 235 | the limitation as if written in the body of this License. 236 | 237 | 9. The Free Software Foundation may publish revised and/or new versions 238 | of the General Public License from time to time. Such new versions will 239 | be similar in spirit to the present version, but may differ in detail to 240 | address new problems or concerns. 241 | 242 | Each version is given a distinguishing version number. If the Program 243 | specifies a version number of this License which applies to it and "any 244 | later version", you have the option of following the terms and conditions 245 | either of that version or of any later version published by the Free 246 | Software Foundation. If the Program does not specify a version number of 247 | this License, you may choose any version ever published by the Free Software 248 | Foundation. 249 | 250 | 10. If you wish to incorporate parts of the Program into other free 251 | programs whose distribution conditions are different, write to the author 252 | to ask for permission. For software which is copyrighted by the Free 253 | Software Foundation, write to the Free Software Foundation; we sometimes 254 | make exceptions for this. Our decision will be guided by the two goals 255 | of preserving the free status of all derivatives of our free software and 256 | of promoting the sharing and reuse of software generally. 257 | 258 | NO WARRANTY 259 | 260 | 11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY 261 | FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN 262 | OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES 263 | PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED 264 | OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 265 | MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS 266 | TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE 267 | PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, 268 | REPAIR OR CORRECTION. 269 | 270 | 12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 271 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR 272 | REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, 273 | INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING 274 | OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED 275 | TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY 276 | YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER 277 | PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE 278 | POSSIBILITY OF SUCH DAMAGES. 279 | 280 | END OF TERMS AND CONDITIONS 281 | 282 | How to Apply These Terms to Your New Programs 283 | 284 | If you develop a new program, and you want it to be of the greatest 285 | possible use to the public, the best way to achieve this is to make it 286 | free software which everyone can redistribute and change under these terms. 287 | 288 | To do so, attach the following notices to the program. It is safest 289 | to attach them to the start of each source file to most effectively 290 | convey the exclusion of warranty; and each file should have at least 291 | the "copyright" line and a pointer to where the full notice is found. 292 | 293 | 294 | Copyright (C) 295 | 296 | This program is free software; you can redistribute it and/or modify 297 | it under the terms of the GNU General Public License as published by 298 | the Free Software Foundation; either version 2 of the License, or 299 | (at your option) any later version. 300 | 301 | This program is distributed in the hope that it will be useful, 302 | but WITHOUT ANY WARRANTY; without even the implied warranty of 303 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 304 | GNU General Public License for more details. 305 | 306 | You should have received a copy of the GNU General Public License along 307 | with this program; if not, write to the Free Software Foundation, Inc., 308 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 309 | 310 | Also add information on how to contact you by electronic and paper mail. 311 | 312 | If the program is interactive, make it output a short notice like this 313 | when it starts in an interactive mode: 314 | 315 | Gnomovision version 69, Copyright (C) year name of author 316 | Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'. 317 | This is free software, and you are welcome to redistribute it 318 | under certain conditions; type `show c' for details. 319 | 320 | The hypothetical commands `show w' and `show c' should show the appropriate 321 | parts of the General Public License. Of course, the commands you use may 322 | be called something other than `show w' and `show c'; they could even be 323 | mouse-clicks or menu items--whatever suits your program. 324 | 325 | You should also get your employer (if you work as a programmer) or your 326 | school, if any, to sign a "copyright disclaimer" for the program, if 327 | necessary. Here is a sample; alter the names: 328 | 329 | Yoyodyne, Inc., hereby disclaims all copyright interest in the program 330 | `Gnomovision' (which makes passes at compilers) written by James Hacker. 331 | 332 | , 1 April 1989 333 | Ty Coon, President of Vice 334 | 335 | This General Public License does not permit incorporating your program into 336 | proprietary programs. If your program is a subroutine library, you may 337 | consider it more useful to permit linking proprietary applications with the 338 | library. If this is what you want to do, use the GNU Lesser General 339 | Public License instead of this License. 340 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | all: amlbootsig unamlbootsig amlbootsig-gxl amlbootenc-gxl amlbootimg-gxl amlinfo 2 | 3 | CFLAGS = -g 4 | LDFLAGS = -lcrypto 5 | 6 | amlbootsig.o unamlbootsig.o amlinfo.o: meson.h aml.h fip.h 7 | amlbootsig-gxl.o amlbootenc-gxl.o amlbootimg-gxl.o: meson.h aml.h 8 | 9 | %.o: %.c 10 | $(CC) $(CFLAGS) -o $@ -c $< 11 | 12 | amlbootsig: amlbootsig.o 13 | $(CC) $(CFLAGS) -o $@ $^ $(LDFLAGS) 14 | 15 | unamlbootsig: unamlbootsig.o 16 | $(CC) $(CFLAGS) -o $@ $^ $(LDFLAGS) 17 | 18 | amlbootsig-gxl: amlbootsig-gxl.o 19 | $(CC) $(CFLAGS) -o $@ $^ $(LDFLAGS) 20 | 21 | amlbootenc-gxl: amlbootenc-gxl.o 22 | $(CC) $(CFLAGS) -o $@ $^ $(LDFLAGS) 23 | 24 | amlbootimg-gxl: amlbootimg-gxl.o 25 | $(CC) $(CFLAGS) -o $@ $^ $(LDFLAGS) 26 | 27 | amlinfo: amlinfo.o 28 | $(CC) $(CFLAGS) -o $@ $^ $(LDFLAGS) 29 | 30 | clean: 31 | -rm -f amlbootsig unamlbootsig amlbootsig-gxl amlinfo test *.o 32 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # meson-tools 2 | 3 | meson-tools is a collection of tools for use with the Amlogic Meson family of ARM based SoCs. 4 | 5 | Name-wise it was inspired by [sunxi-tools](https://github.com/linux-sunxi/sunxi-tools/). 6 | 7 | ## amlbootsig 8 | 9 | Usage: 10 | ``` 11 | amlbootsig boot_new.bin u-boot.img 12 | ``` 13 | 14 | This tool is supposed to provide equivalent output to: 15 | ``` 16 | aml_encrypt_gxb --bootsig --input boot_new.bin --output u-boot.img 17 | ``` 18 | with the tool version distributed by Hardkernel for their Odroid-C2 board (S905 / Meson GXBaby). 19 | 20 | ### How to compare output 21 | 22 | ``` 23 | hexdump -C a/u-boot.img > a/u-boot.img.hex 24 | hexdump -C b/u-boot.img > b/u-boot.img.hex 25 | diff -u a/u-boot.img.hex b/u-boot.img.hex | less 26 | ``` 27 | 28 | This should result in a diff with only the following differences: 29 | 30 | * First 16 bytes will be random in original. 31 | * The header for each FIP TOC entry will contain 4 bytes of random data. 32 | * Due to FIP TOC entry headers differing, the SHA256 hash in the header will differ as well. 33 | 34 | For testing identity of output files, modify the code to use the random bytes from the file you are testing against. 35 | 36 | ### Known limitations 37 | 38 | * Correctness: Since amlbootsig was designed to binary-match previously created output files, it may still have some numbers hard-coded or may do calculations in a way that happened to match tested input/output but may break for other input files. Please report such cases as GitHub issues. 39 | * Endianness: Only Little Endian byte order has been considered. Running it on Big Endian hosts may misinterpret file input and/or result in wrong output and/or other misbehavior. The solution would be byte swaps, but those depend on the field width, which is guessed only and thus may change as the format is being better understood. 40 | * Security: The tool was not designed to defend against invalid or evil file input. 41 | 42 | ## unamlbootsig 43 | 44 | Usage: 45 | ``` 46 | unamlbootsig u-boot.img boot_new.bin 47 | ``` 48 | 49 | This tool is supposed to do the reverse of `amlbootsig`, i.e. drop the boot signature. 50 | 51 | The output may differ from the original `aml_encrypt_gxb --bootsig`/`amlbootsig` input from 0xb000 to 0xbfff as well as in FIP TOC entry size 16-byte alignment. 52 | 53 | ### Known limitations 54 | 55 | See `amlbootsig`. 56 | 57 | ## amlbootsig-gxl 58 | 59 | Usage: 60 | ``` 61 | amlbootsig-gxl bl2_new.bin bl2_new.bin.sig 62 | ``` 63 | 64 | This tool is supposed to provide equivalent output to: 65 | ``` 66 | aml_encrypt_gxl --bl2sig --input bl2_new.bin --output bl2_new.bin.sig 67 | ``` 68 | with the tool version distributed by Amlogic in their 2018-07-06 Buildroot tarball. 69 | 70 | ### How to compare output 71 | 72 | ``` 73 | hexdump -C a/bl2_new.bin.sig > a/bl2_new.bin.sig.hex 74 | hexdump -C b/bl2_new.bin.sig > b/bl2_new.bin.sig.hex 75 | diff -u a/bl2_new.bin.sig.hex b/bl2_new.bin.sig.hex | less 76 | ``` 77 | 78 | This should result in a diff with only the following differences: 79 | 80 | * First 16 bytes will be random in original. 81 | * Due to padding contents differing, the SHA256 hash in the header will differ as well. 82 | 83 | For testing identity of output files, modify the code to use the random bytes from the file you are testing against. 84 | 85 | ### Known limitations 86 | 87 | Specifically tailored to BL2. 88 | 89 | Otherwise see `amlbootsig`. 90 | 91 | ## amlbootenc-gxl 92 | 93 | Usage: 94 | ``` 95 | amlbootenc-gxl u-boot.bin u-boot.bin.enc 96 | ``` 97 | 98 | This tool is supposed to provide equivalent output to: 99 | ``` 100 | aml_encrypt_gxl --bl3enc --input u-boot.bin --output u-boot.bin.enc 101 | ``` 102 | with the tool versions distributed by Amlogic in their 2018-07-06 Buildroot tarball. 103 | 104 | ### How to compare output 105 | 106 | ``` 107 | hexdump -C a/u-boot.bin.enc > a/u-boot.bin.enc.hex 108 | hexdump -C b/u-boot.bin.enc > b/u-boot.bin.enc.hex 109 | diff -u a/u-boot.bin.enc.hex b/u-boot.bin.enc.hex | less 110 | ``` 111 | 112 | This should result in a diff with the following differences: 113 | 114 | * Symmetric AES CBC encryption key (32 bytes) and initialization vector (16 bytes) are random. 115 | * Therefore the whole encrypted contents will differ. 116 | * Due to differing encryption the SHA256 hash (32 bytes) in the header will differ as well. 117 | 118 | For testing identity of output files, modify the code to use the key/IV bytes from the file you are testing against. 119 | 120 | ### Known limitations 121 | 122 | Specifically tailored to BL3x. 123 | 124 | Otherwise see `amlbootsig`. 125 | 126 | ## amlbootimg-gxl 127 | 128 | Usage: 129 | ``` 130 | fiptool --align 0x4000 --scp-fw bl30_new.bin.enc --soc-fw bl31.img.enc --nt-fw u-boot.bin.enc fip.bin 131 | amlbootenc-gxl fip.bin fip.bin.enc 132 | dd if=fip.bin.enc of=fip.enc.bin bs=512 count=31 133 | amlbootimg-gxl bl2_new.bin.sig fip.enc.bin bl30_new.bin.enc bl31.img.enc u-boot.bin.enc u-boot.img 134 | ``` 135 | 136 | This tool is supposed to provide equivalent output to: 137 | ``` 138 | aml_encrypt_gxl --bootmk --bl2 bl2_new.bin.sig --bl30 bl30_new.bin.enc --bl31 bl31.img.enc --bl33 u-boot.bin.enc --output u-boot.img 139 | dd if=u-boot.img.sd.bin of=/dev/XXX bs=512 skip=1 seek=1 140 | ``` 141 | with the tool versions distributed by Amlogic in their 2018-07-06 Buildroot tarball. 142 | 143 | To deploy the file to SD card: 144 | ``` 145 | dd if=u-boot.img of=/dev/XXX bs=512 seek=1 146 | ``` 147 | 148 | ### How to compare output 149 | 150 | ``` 151 | hexdump -C a/u-boot.img > a/u-boot.img.hex 152 | hexdump -C b/u-boot.img > b/u-boot.img.hex 153 | diff -u a/u-boot.img.hex b/u-boot.img.hex | less 154 | ``` 155 | 156 | This should result in a diff with the following differences: 157 | 158 | * Symmetric AES CBC encryption key (32 bytes) and initialization vector (16 bytes) are random. 159 | * Therefore the whole encrypted contents will differ. 160 | * Due to differing encryption the SHA256 hash (32 bytes) in the header will differ as well. 161 | * Random padding bytes between the FIP images. 162 | 163 | ### Known limitations 164 | 165 | * Hardcodes file offsets matching the current binary blobs. May break with upstream BL31 or newer/older blobs. 166 | 167 | Otherwise see `amlbootsig`. 168 | 169 | ## amlinfo 170 | 171 | Usage: 172 | ``` 173 | amlinfo u-boot.img 174 | ``` 175 | 176 | This tool dumps info about an existing image file. 177 | 178 | It supports the following inputs: 179 | * `amlbootsig` output 180 | * `aml_encrypt_gxb --bootsig` output, except when using `--amluserkey` or `--efuse` 181 | * `dd if=u-boot.img bs=512 skip=96` output for either of them (FIP TOC with headered entries) 182 | * `dd if=u-boot.img bs=1 skip=16` output, i.e. raw `@AML` header, of at least 64 bytes 183 | * `amlbootsig-gxl` output 184 | * `aml_encrypt_gxl --bl2sig` output 185 | 186 | ### Known limitations 187 | 188 | * Correctness: Since there seems to be no public documentation for the @AML headers, any field names printed are guesses and may need to be revised once their meaning becomes clearer. 189 | * Endianness: Only Little Endian byte order has been considered, see above. 190 | * Security: See above. 191 | -------------------------------------------------------------------------------- /aml.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2017, 2019 Andreas Färber 3 | * 4 | * SPDX-License-Identifier: GPL-2.0+ OR MIT 5 | */ 6 | 7 | #ifndef AML_H 8 | #define AML_H 9 | 10 | #include 11 | 12 | #define AMLOGIC_SIGNATURE "@AML" 13 | 14 | struct __attribute((__packed__)) AmlogicHeader { 15 | char sig[4]; 16 | uint32_t size; 17 | uint16_t header_size; 18 | uint16_t header_version; 19 | uint32_t id; 20 | uint32_t encrypted; 21 | uint32_t digest_offset; 22 | uint32_t digest_size; 23 | uint32_t data_offset; 24 | uint32_t data_size; 25 | uint32_t padding_offset; 26 | uint32_t padding_size; 27 | uint32_t _offset2; 28 | uint32_t pad2; 29 | uint32_t payload_offset; 30 | uint32_t payload_size; 31 | uint32_t unknown; 32 | }; 33 | 34 | #define AMLOGIC_C_SIGNATURE "AMLC" 35 | 36 | struct __attribute((__packed__)) AmlogicCryptoHeader { 37 | uint16_t _unknown; 38 | uint16_t block_size; 39 | uint32_t encrypted; 40 | uint32_t _unknown2; 41 | char sig[4]; 42 | uint32_t first_offset; 43 | uint32_t data_offset; 44 | uint32_t encrypted_size; 45 | uint32_t payload_size; 46 | uint8_t digest[32]; 47 | uint8_t key[32]; 48 | uint8_t iv[16]; 49 | uint8_t _pad[24]; 50 | char cipher[7]; 51 | char date[20]; 52 | uint8_t _pad2[69]; 53 | uint8_t _pad3[16]; 54 | uint32_t _unknown3; 55 | char sig2[4]; 56 | }; 57 | 58 | #endif 59 | -------------------------------------------------------------------------------- /amlbootenc-gxl.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2019 Andreas Färber 3 | * 4 | * SPDX-License-Identifier: GPL-2.0-or-later WITH openvpn-openssl-exception 5 | */ 6 | 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include 15 | 16 | #include 17 | #include 18 | 19 | #include "meson.h" 20 | 21 | /* 22 | * COMPARE_MODE: Define this and change values below to match files 23 | * generated by aml_encrypt_gxl. 24 | */ 25 | #undef COMPARE_MODE 26 | 27 | #ifndef COMPARE_MODE 28 | /* 29 | * REPRODUCIBLE_OUTPUT: For the benefit of generating reproducible packages, 30 | * deviate from aml_encrypt_gxl in not using random / time-based values. 31 | */ 32 | #define REPRODUCIBLE_OUTPUT 33 | #endif 34 | 35 | static int boot_enc(const char *input, const char *output) 36 | { 37 | FILE *fin, *fout; 38 | long file_size; 39 | uint8_t *src_buf, *buf; 40 | bool is_bl31 = false; 41 | time_t t; 42 | struct tm *tm; 43 | AES_KEY aes_key; 44 | SHA256_CTX sha256_ctx; 45 | uint8_t sha256_digest[SHA256_DIGEST_LENGTH], iv[16]; 46 | struct AmlogicCryptoHeader hdr = { 47 | .sig = AMLOGIC_C_SIGNATURE, 48 | .block_size = 0x200, 49 | .encrypted = 0x10001, 50 | .cipher = "AES-CBC", 51 | ._unknown3 = 0x02000000, 52 | .sig2 = AMLOGIC_C_SIGNATURE, 53 | }; 54 | 55 | assert(sizeof(hdr) == 0x100); 56 | 57 | fin = fopen(input, "rb"); 58 | if (fin == NULL) { 59 | perror(input); 60 | return 1; 61 | } 62 | 63 | fseek(fin, 0, SEEK_END); 64 | file_size = ftell(fin); 65 | fseek(fin, 0, SEEK_SET); 66 | 67 | hdr.data_offset = hdr.block_size; 68 | hdr.payload_size = ROUND_UP(file_size, hdr.block_size); 69 | 70 | src_buf = malloc(hdr.block_size); 71 | if (src_buf == NULL) 72 | return 1; 73 | 74 | memset(src_buf, 0, hdr.block_size); 75 | fread(src_buf, 1, hdr.block_size, fin); 76 | 77 | if (*(uint32_t*)src_buf == 0x12348765) { 78 | is_bl31 = true; 79 | } 80 | 81 | if (is_bl31) 82 | hdr.payload_size -= hdr.block_size; 83 | hdr.encrypted_size = hdr.payload_size; 84 | hdr.first_offset = hdr.payload_size; 85 | 86 | buf = malloc(hdr.data_offset + hdr.payload_size); 87 | if (buf == NULL) { 88 | perror("malloc"); 89 | return 1; 90 | } 91 | 92 | memset(buf, 0, hdr.data_offset + hdr.payload_size); 93 | 94 | if (is_bl31) { 95 | memcpy(buf + 0x100, src_buf, 0x100); 96 | memset(src_buf, 0, hdr.block_size); 97 | fread(src_buf, 1, hdr.block_size, fin); 98 | } 99 | 100 | #ifdef COMPARE_MODE 101 | tm = malloc(sizeof(*tm)); 102 | if (tm == NULL) 103 | return 1; 104 | tm->tm_year = 2019 - 1900; 105 | tm->tm_mon = 6 - 1; 106 | tm->tm_mday = 30; 107 | if (is_bl31) { 108 | tm->tm_hour = 18; 109 | tm->tm_min = 33; 110 | tm->tm_sec = 21; 111 | } else { 112 | tm->tm_hour = 2; 113 | tm->tm_min = 52; 114 | tm->tm_sec = 16; 115 | } 116 | #else 117 | t = time(NULL); 118 | tm = localtime(&t); 119 | #endif 120 | strftime(hdr.date, sizeof(hdr.date), "%Y/%m/%d %H:%M:%S", tm); 121 | 122 | #ifdef COMPARE_MODE 123 | if (is_bl31) { 124 | memcpy(hdr.key, (uint8_t[]){ 0x3e, 0xf9, 0x5c, 0xd7, 0x84, 0x4f, 0xa1, 0xee, 0xb1, 0x6e, 0x66, 0x4d, 0x87, 0xa2, 0x9f, 0x01, 125 | 0xfb, 0xc4, 0x83, 0x83, 0x75, 0x86, 0x0e, 0xa9, 0x84, 0x30, 0x6d, 0x7d, 0xf0, 0xe1, 0x5b, 0x2f }, 32); 126 | memcpy(hdr.iv, (uint8_t[]){ 0xdb, 0xb8, 0x06, 0x5f, 0x07, 0xa8, 0x4e, 0xb8, 0x16, 0xb4, 0x06, 0x9d, 0x56, 0xa5, 0x9e, 0x51 }, 16); 127 | } else { 128 | memcpy(hdr.key, (uint8_t[]){ 0x42, 0x16, 0xfc, 0x04, 0x07, 0x8d, 0x65, 0x2e, 0xfd, 0xce, 0xc9, 0xfd, 0x0a, 0x02, 0x45, 0x83, 129 | 0x21, 0xba, 0xf2, 0xf1, 0x0f, 0xf3, 0xdf, 0x2c, 0x94, 0x66, 0x30, 0xab, 0x15, 0xfe, 0x86, 0x57 }, 32); 130 | memcpy(hdr.iv, (uint8_t[]){ 0x14, 0x82, 0x5b, 0x1b, 0x10, 0xc1, 0x49, 0x0d, 0x8f, 0x13, 0x0a, 0x99, 0x15, 0x50, 0x1d, 0x37 }, 16); 131 | } 132 | #else 133 | for (int i = 0; i < sizeof(hdr.key); i++) { 134 | hdr.key[i] = rand(); 135 | } 136 | for (int i = 0; i < sizeof(hdr.iv); i++) { 137 | hdr.iv[i] = rand(); 138 | } 139 | #endif 140 | 141 | fout = fopen(output, "wb"); 142 | if (fout == NULL) { 143 | perror(output); 144 | return 1; 145 | } 146 | 147 | SHA256_Init(&sha256_ctx); 148 | AES_set_encrypt_key(hdr.key, sizeof(hdr.key) * 8, &aes_key); 149 | memcpy(iv, hdr.iv, 16); 150 | 151 | AES_cbc_encrypt(src_buf, buf + hdr.first_offset, hdr.block_size, &aes_key, iv, AES_ENCRYPT); 152 | SHA256_Update(&sha256_ctx, buf + hdr.first_offset, hdr.block_size); 153 | 154 | for (int i = 1; i < hdr.payload_size / hdr.block_size; i++) { 155 | memset(src_buf, 0, hdr.block_size); 156 | fread(src_buf, 1, hdr.block_size, fin); 157 | AES_cbc_encrypt(src_buf, buf + hdr.data_offset + (i - 1) * hdr.block_size, hdr.block_size, &aes_key, iv, AES_ENCRYPT); 158 | SHA256_Update(&sha256_ctx, buf + hdr.data_offset + (i - 1) * hdr.block_size, hdr.block_size); 159 | } 160 | 161 | memset(sha256_digest, 0, sizeof(sha256_digest)); 162 | SHA256_Final(sha256_digest, &sha256_ctx); 163 | memcpy(hdr.digest, sha256_digest, sizeof(sha256_digest)); 164 | 165 | memcpy(buf, &hdr, sizeof(hdr)); 166 | 167 | fwrite(buf, 1, hdr.data_offset + hdr.payload_size, fout); 168 | 169 | fclose(fout); 170 | fclose(fin); 171 | 172 | return 0; 173 | } 174 | 175 | int main(int argc, char **argv) 176 | { 177 | if (argc < 3) { 178 | fprintf(stderr, "Usage: %s input output\n", argv[0]); 179 | return 1; 180 | } 181 | 182 | return boot_enc(argv[1], argv[2]); 183 | } 184 | -------------------------------------------------------------------------------- /amlbootimg-gxl.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2019 Andreas Färber 3 | * 4 | * SPDX-License-Identifier: GPL-2.0-or-later WITH openvpn-openssl-exception 5 | */ 6 | 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | 15 | #include 16 | 17 | #include "meson.h" 18 | 19 | /* 20 | * COMPARE_MODE: Define this and change values below to match files 21 | * generated by aml_encrypt_gxl. 22 | */ 23 | #undef COMPARE_MODE 24 | 25 | #ifndef COMPARE_MODE 26 | /* 27 | * REPRODUCIBLE_OUTPUT: For the benefit of generating reproducible packages, 28 | * deviate from aml_encrypt_gxl in not using random / time-based values. 29 | */ 30 | #define REPRODUCIBLE_OUTPUT 31 | #endif 32 | 33 | static int append_file(const char *input, FILE *fout) 34 | { 35 | FILE *fin; 36 | long len; 37 | uint8_t *buf; 38 | int ret = 0; 39 | 40 | fin = fopen(input, "rb"); 41 | if (fin == NULL) { 42 | perror(input); 43 | return 1; 44 | } 45 | 46 | fseek(fin, 0, SEEK_END); 47 | len = ftell(fin); 48 | fseek(fin, 0, SEEK_SET); 49 | 50 | buf = malloc(len); 51 | if (buf == NULL) { 52 | ret = 1; 53 | goto err_malloc; 54 | } 55 | 56 | fread(buf, 1, len, fin); 57 | 58 | fwrite(buf, 1, len, fout); 59 | 60 | free(buf); 61 | err_malloc: 62 | fclose(fin); 63 | return ret; 64 | } 65 | 66 | static int boot_img(const char *in_bl2, const char *in_fip, 67 | const char *in_bl30, const char *in_bl31, const char *in_bl33, const char *output) 68 | { 69 | FILE *fin, *fout; 70 | uint8_t random[16]; 71 | uint8_t *src_buf, *buf; 72 | struct AmlogicHeader hdr = { 73 | .sig = AMLOGIC_SIGNATURE, 74 | .size = 64, 75 | .header_size = 64, 76 | .header_version = 0x101, 77 | .encrypted = 0, 78 | .digest_offset = 64, 79 | .digest_size = 512, 80 | }; 81 | SHA256_CTX sha256_ctx; 82 | uint8_t sha256_digest[SHA256_DIGEST_LENGTH]; 83 | int ret; 84 | #ifndef COMPARE_MODE 85 | int i; 86 | #endif 87 | 88 | assert(sizeof(struct AmlogicHeader) == 64); 89 | 90 | src_buf = malloc(0xb000); 91 | if (src_buf == NULL) 92 | return 1; 93 | 94 | fout = fopen(output, "wb"); 95 | if (fout == NULL) { 96 | perror(output); 97 | return 1; 98 | } 99 | 100 | #ifdef COMPARE_MODE 101 | memcpy(random, (uint8_t[]){ 0x70, 0x8c, 0x67, 0x1e, 0x91, 0x7c, 0xdd, 0x79, 0x1e, 0xbf, 0xb2, 0xb8, 0xc7, 0xd8, 0x66, 0x55 }, 16); 102 | #else 103 | for (i = 0; i < 16; i++) 104 | #ifdef REPRODUCIBLE_OUTPUT 105 | random[i] = 0x42; 106 | #else 107 | random[i] = rand(); 108 | #endif 109 | #endif 110 | 111 | fwrite(random, 1, 16, fout); 112 | 113 | fin = fopen(in_bl2, "rb"); 114 | if (fin == NULL) { 115 | perror(in_bl2); 116 | return 1; 117 | } 118 | 119 | fseek(fin, 0x1000, SEEK_SET); 120 | 121 | fread(src_buf, 1, 0xb000, fin); 122 | 123 | fclose(fin); 124 | 125 | hdr.size += hdr.digest_size; 126 | hdr.size += 0xdb0; 127 | hdr.size += 0xb000; 128 | hdr.digest_offset = hdr.header_size; 129 | hdr.data_offset = hdr.header_size + SHA256_DIGEST_LENGTH; 130 | hdr.padding_offset = hdr.digest_offset + 512; 131 | hdr.padding_size = 3504; 132 | hdr._offset2 = hdr.size - hdr.data_offset; 133 | hdr.payload_offset = hdr.padding_offset + hdr.padding_size; 134 | hdr.payload_size = 0xb000; 135 | 136 | buf = malloc(hdr.size); 137 | if (buf == NULL) { 138 | perror("malloc"); 139 | return 1; 140 | } 141 | 142 | memset(buf, 0, hdr.size); 143 | memcpy(buf, &hdr, sizeof(struct AmlogicHeader)); 144 | memcpy(buf + hdr.padding_offset + hdr.padding_size, src_buf, 0xb000); 145 | 146 | #ifdef COMPARE_MODE 147 | memcpy(buf + 0x258, (uint8_t[]){ 0x98, 0x02 }, 2); 148 | memcpy(buf + 0x8ec, (uint8_t[]){ 0x40, 0x02 }, 2); 149 | memcpy(buf + 0xb20, (uint8_t[]){ 0x98, 0x02 }, 2); 150 | #endif 151 | 152 | SHA256_Init(&sha256_ctx); 153 | SHA256_Update(&sha256_ctx, buf, hdr.header_size); 154 | SHA256_Update(&sha256_ctx, buf + hdr.data_offset, hdr._offset2); 155 | memset(sha256_digest, 0, sizeof(sha256_digest)); 156 | SHA256_Final(sha256_digest, &sha256_ctx); 157 | memcpy(buf + hdr.digest_offset, sha256_digest, sizeof(sha256_digest)); 158 | 159 | fwrite(buf, 1, hdr.size, fout); 160 | 161 | 162 | fseek(fout, 0xc000, SEEK_SET); 163 | 164 | ret = append_file(in_fip, fout); 165 | if (ret) 166 | return ret; 167 | 168 | 169 | fseek(fout, 0x10000, SEEK_SET); 170 | 171 | ret = append_file(in_bl30, fout); 172 | if (ret) 173 | return ret; 174 | 175 | fseek(fout, 0x20000, SEEK_SET); 176 | 177 | ret = append_file(in_bl31, fout); 178 | if (ret) 179 | return ret; 180 | 181 | fseek(fout, 0x3c000, SEEK_SET); 182 | 183 | ret = append_file(in_bl33, fout); 184 | if (ret) 185 | return ret; 186 | 187 | fclose(fout); 188 | 189 | free(src_buf); 190 | 191 | return 0; 192 | } 193 | 194 | int main(int argc, char **argv) 195 | { 196 | if (argc < 7) { 197 | fprintf(stderr, "Usage: %s bl2 fip bl30 bl31 bl33 output\n", argv[0]); 198 | return 1; 199 | } 200 | 201 | return boot_img(argv[1], argv[2], argv[3], argv[4], argv[5], argv[6]); 202 | } 203 | -------------------------------------------------------------------------------- /amlbootsig-gxl.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2017, 2019 Andreas Färber 3 | * 4 | * SPDX-License-Identifier: GPL-2.0-or-later WITH openvpn-openssl-exception 5 | */ 6 | 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | 15 | #include 16 | 17 | #include "meson.h" 18 | 19 | /* 20 | * COMPARE_MODE: Define this and change values below to match files 21 | * generated by aml_encrypt_gxb. 22 | */ 23 | #undef COMPARE_MODE 24 | 25 | #ifndef COMPARE_MODE 26 | /* 27 | * REPRODUCIBLE_OUTPUT: For the benefit of generating reproducible packages, 28 | * deviate from aml_encrypt_gxl in not using random / time-based values. 29 | */ 30 | #define REPRODUCIBLE_OUTPUT 31 | #endif 32 | 33 | static int boot_sig(const char *input, const char *output) 34 | { 35 | FILE *fin, *fout; 36 | uint8_t random[16]; 37 | uint8_t *src_buf, *buf; 38 | struct AmlogicHeader hdr = { 39 | .sig = AMLOGIC_SIGNATURE, 40 | .size = 64, 41 | .header_size = 64, 42 | .header_version = 0x0101, 43 | .encrypted = 0, 44 | .digest_offset = 64, 45 | .digest_size = 512, 46 | }; 47 | SHA256_CTX sha256_ctx; 48 | uint8_t sha256_digest[SHA256_DIGEST_LENGTH]; 49 | #ifndef COMPARE_MODE 50 | int i; 51 | #endif 52 | 53 | assert(sizeof(struct AmlogicHeader) == 64); 54 | 55 | src_buf = malloc(0xc000); 56 | if (src_buf == NULL) 57 | return 1; 58 | 59 | fin = fopen(input, "rb"); 60 | if (fin == NULL) { 61 | perror(input); 62 | return 1; 63 | } 64 | 65 | fout = fopen(output, "wb"); 66 | if (fout == NULL) { 67 | perror(output); 68 | return 1; 69 | } 70 | 71 | #ifdef COMPARE_MODE 72 | memcpy(random, (uint8_t[]){ 0x25, 0x66, 0x10, 0x39, 0x53, 0x45, 0x43, 0x2b, 0x10, 0xd7, 0x96, 0xe2, 0x68, 0xc2, 0x7a, 0x6a }, 16); 73 | #else 74 | for (i = 0; i < 16; i++) 75 | #ifdef REPRODUCIBLE_OUTPUT 76 | random[i] = 0x42; 77 | #else 78 | random[i] = rand(); 79 | #endif 80 | #endif 81 | 82 | fwrite(random, 1, 16, fout); 83 | 84 | fread(src_buf, 1, 0xc000, fin); 85 | 86 | if (strncmp(src_buf + 16, "@AML", 4) == 0) { 87 | fprintf(stderr, "@AML discovered in input!\n"); 88 | return 1; 89 | } 90 | 91 | hdr.size += hdr.digest_size; 92 | hdr.size += 0xdb0; 93 | hdr.size += 0xb000; 94 | hdr.digest_offset = hdr.header_size; 95 | hdr.data_offset = hdr.header_size + SHA256_DIGEST_LENGTH; 96 | hdr.padding_offset = hdr.digest_offset + 512; 97 | hdr.padding_size = 3504; 98 | hdr._offset2 = hdr.size - hdr.data_offset; 99 | hdr.payload_offset = hdr.padding_offset + hdr.padding_size; 100 | hdr.payload_size = 0xb000; 101 | 102 | buf = malloc(hdr.size); 103 | if (buf == NULL) { 104 | perror("malloc"); 105 | return 1; 106 | } 107 | 108 | memset(buf, 0, hdr.size); 109 | memcpy(buf, &hdr, sizeof(struct AmlogicHeader)); 110 | memcpy(buf + hdr.padding_offset + hdr.padding_size, src_buf, 0xb000); 111 | 112 | #ifdef COMPARE_MODE 113 | memcpy(buf + 0x258, (uint8_t[]){ 0x98, 0x02 }, 2); 114 | memcpy(buf + 0x8ec, (uint8_t[]){ 0x40, 0x02 }, 2); 115 | memcpy(buf + 0xb20, (uint8_t[]){ 0x98, 0x02 }, 2); 116 | #endif 117 | 118 | SHA256_Init(&sha256_ctx); 119 | SHA256_Update(&sha256_ctx, buf, hdr.header_size); 120 | SHA256_Update(&sha256_ctx, buf + hdr.data_offset, hdr._offset2); 121 | memset(sha256_digest, 0, sizeof(sha256_digest)); 122 | SHA256_Final(sha256_digest, &sha256_ctx); 123 | memcpy(buf + hdr.digest_offset, sha256_digest, sizeof(sha256_digest)); 124 | 125 | fwrite(buf, 1, hdr.size, fout); 126 | 127 | fclose(fout); 128 | fclose(fin); 129 | 130 | free(src_buf); 131 | 132 | return 0; 133 | } 134 | 135 | int main(int argc, char **argv) 136 | { 137 | if (argc < 3) { 138 | fprintf(stderr, "Usage: %s input output\n", argv[0]); 139 | return 1; 140 | } 141 | 142 | return boot_sig(argv[1], argv[2]); 143 | } 144 | -------------------------------------------------------------------------------- /amlbootsig.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2017 Andreas Färber 3 | * 4 | * SPDX-License-Identifier: GPL-2.0-or-later WITH openvpn-openssl-exception 5 | */ 6 | 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | 15 | #include 16 | 17 | #include "meson.h" 18 | 19 | /* 20 | * COMPARE_MODE: Define this and change values below to match files 21 | * generated by aml_encrypt_gxb. 22 | */ 23 | #undef COMPARE_MODE 24 | 25 | #ifndef COMPARE_MODE 26 | /* 27 | * REPRODUCIBLE_OUTPUT: For the benefit of generating reproducible packages, 28 | * deviate from aml_encrypt_gxb in not using random / time-based values. 29 | */ 30 | #define REPRODUCIBLE_OUTPUT 31 | #endif 32 | 33 | static int do_fip(FILE *fout, FILE *fin) 34 | { 35 | uint8_t *toc_buf, *buf; 36 | struct FipHeader *ptoc; 37 | int i, n; 38 | long toc_pos; 39 | 40 | assert(sizeof(struct FipEntry) == 40); 41 | 42 | toc_buf = malloc(0x4000); 43 | if (toc_buf == NULL) { 44 | perror("malloc"); 45 | return 1; 46 | } 47 | 48 | ptoc = (struct FipHeader *)toc_buf; 49 | toc_pos = ftell(fout); 50 | fread(toc_buf, 1, 0x4000, fin); 51 | n = 0; 52 | while (ptoc->entries[n].size != 0) { 53 | n++; 54 | } 55 | fwrite(toc_buf, 1, 0x4000, fout); 56 | 57 | buf = malloc(0x10000); 58 | if (buf == NULL) { 59 | perror("malloc"); 60 | return 1; 61 | } 62 | 63 | for (i = 0; i < n; i++) { 64 | struct AmlogicHeader fip_hdr = { 65 | .sig = AMLOGIC_SIGNATURE, 66 | .size = 64, 67 | .header_size = sizeof(struct AmlogicHeader), 68 | .header_version = 1, 69 | .digest_size = SHA256_DIGEST_LENGTH, 70 | }; 71 | SHA256_CTX sha256_ctx; 72 | uint8_t sha256_digest[SHA256_DIGEST_LENGTH]; 73 | size_t len; 74 | long pos; 75 | long remaining; 76 | int padlen; 77 | 78 | pos = ftell(fout); 79 | 80 | printf("FIP TOC entry offset_address: %" PRIx64 "\n", ptoc->entries[i].offset_address); 81 | printf("FIP TOC entry size: %" PRIx64 "\n", ptoc->entries[i].size); 82 | 83 | fseek(fin, toc_pos + ptoc->entries[i].offset_address, SEEK_SET); 84 | printf("Input at %lx\n", ftell(fin)); 85 | 86 | len = ptoc->entries[i].size & (16 - 1); 87 | padlen = (len != 0) ? (16 - len) : 0; 88 | 89 | memset(buf, 0, 0x10000); 90 | fip_hdr.size += fip_hdr.digest_size + ptoc->entries[i].size + padlen; 91 | #ifdef COMPARE_MODE 92 | fip_hdr.id = 0x4ee06c7b; 93 | #else 94 | #ifdef REPRODUCIBLE_OUTPUT 95 | fip_hdr.id = 0x42424200 + i; 96 | #else 97 | #error Non-reproducible FIP image IDs not implemented. 98 | #endif 99 | #endif 100 | fip_hdr.digest_offset = fip_hdr.header_size; 101 | fip_hdr.data_offset = fip_hdr.digest_offset + fip_hdr.digest_size; 102 | fip_hdr.padding_offset = fip_hdr.data_offset; 103 | fip_hdr._offset2 = fip_hdr.size - 0x60; 104 | fip_hdr.payload_size = fip_hdr.size - 0x60; 105 | fip_hdr.payload_offset = fip_hdr.header_size + SHA256_DIGEST_LENGTH; 106 | memcpy(buf, &fip_hdr, sizeof(fip_hdr)); 107 | 108 | fwrite(buf, 1, fip_hdr.header_size + fip_hdr.digest_size, fout); 109 | 110 | SHA256_Init(&sha256_ctx); 111 | SHA256_Update(&sha256_ctx, buf, fip_hdr.header_size); 112 | SHA256_Update(&sha256_ctx, buf + fip_hdr.data_offset, 0); 113 | remaining = ptoc->entries[i].size; 114 | len = fread(buf, 1, fip_hdr.header_size + fip_hdr.digest_size, fin); 115 | if (strncmp(buf + 16, "@AML", 4) == 0) { 116 | fprintf(stderr, "@AML discovered in input FIP section %i!\n", i); 117 | return 1; 118 | } 119 | SHA256_Update(&sha256_ctx, buf, len); 120 | remaining -= len; 121 | while (remaining > 0) { 122 | len = fread(buf, 1, MIN(remaining, 0x4000), fin); 123 | remaining -= len; 124 | memset(buf + len, 0, len & 0xf); 125 | SHA256_Update(&sha256_ctx, buf, ROUND_UP(len, 16)); 126 | fwrite(buf, 1, ROUND_UP(len, 16), fout); 127 | } 128 | SHA256_Final(sha256_digest, &sha256_ctx); 129 | fseek(fout, pos + fip_hdr.digest_offset, SEEK_SET); 130 | fwrite(sha256_digest, 1, sizeof(sha256_digest), fout); 131 | fseek(fout, 0, SEEK_END); 132 | 133 | fseek(fin, toc_pos + ptoc->entries[i].offset_address, SEEK_SET); 134 | len = fread(buf, 1, fip_hdr.header_size + fip_hdr.digest_size, fin); 135 | fwrite(buf, 1, len, fout); 136 | 137 | ptoc->entries[i].size += fip_hdr.header_size + fip_hdr.digest_size + padlen; 138 | printf("FIP TOC entry size: %" PRIx64 "\n", ptoc->entries[i].size); 139 | len = ptoc->entries[i].size & (0x4000 - 1); 140 | if (len != 0) { 141 | len = 0x4000 - len; 142 | memset(buf, 0, len); 143 | fwrite(buf, 1, len, fout); 144 | } 145 | } 146 | free(buf); 147 | fseek(fout, toc_pos, SEEK_SET); 148 | fwrite(toc_buf, 1, 0x4000, fout); 149 | fseek(fout, 0, SEEK_END); 150 | free(toc_buf); 151 | return 0; 152 | } 153 | 154 | static int boot_sig(const char *input, const char *output) 155 | { 156 | FILE *fin, *fout; 157 | uint8_t random[16]; 158 | uint8_t *src_buf, *buf, *fip_buf; 159 | struct AmlogicHeader hdr = { 160 | .sig = AMLOGIC_SIGNATURE, 161 | .size = 64, 162 | .header_size = 64, 163 | .header_version = 1, 164 | .encrypted = 0, 165 | .digest_offset = 64, 166 | .digest_size = 512, 167 | }; 168 | SHA256_CTX sha256_ctx; 169 | uint8_t sha256_digest[SHA256_DIGEST_LENGTH]; 170 | #ifndef COMPARE_MODE 171 | int i; 172 | #endif 173 | 174 | assert(sizeof(struct AmlogicHeader) == 64); 175 | 176 | src_buf = malloc(0xb000); 177 | if (src_buf == NULL) 178 | return 1; 179 | 180 | fin = fopen(input, "rb"); 181 | if (fin == NULL) { 182 | perror(input); 183 | return 1; 184 | } 185 | 186 | fout = fopen(output, "wb"); 187 | if (fout == NULL) { 188 | perror(output); 189 | return 1; 190 | } 191 | 192 | #ifdef COMPARE_MODE 193 | memcpy(random, (uint8_t[]){ 0xde, 0x49, 0x4c, 0x47, 0xe1, 0xd4, 0x86, 0xca, 0xb7, 0x4f, 0x10, 0xa4, 0x29, 0x3f, 0x28, 0x00 }, 16); 194 | #else 195 | for (i = 0; i < 16; i++) 196 | #ifdef REPRODUCIBLE_OUTPUT 197 | random[i] = 0x42; 198 | #else 199 | random[i] = rand(); 200 | #endif 201 | #endif 202 | 203 | fwrite(random, 1, 16, fout); 204 | 205 | fread(src_buf, 1, 0xb000, fin); 206 | 207 | if (strncmp(src_buf + 16, "@AML", 4) == 0) { 208 | fprintf(stderr, "@AML discovered in input!\n"); 209 | return 1; 210 | } 211 | 212 | fseek(fin, 0xc000, SEEK_SET); 213 | 214 | hdr.size += hdr.digest_size; 215 | hdr.size += 0xdb0; 216 | hdr.size += 0xb000; 217 | hdr.digest_offset = hdr.header_size; 218 | hdr.data_offset = hdr.header_size + SHA256_DIGEST_LENGTH; 219 | hdr.padding_offset = hdr.digest_offset + 512; 220 | hdr.padding_size = 3504; 221 | hdr._offset2 = hdr.size - hdr.data_offset; 222 | hdr.payload_offset = hdr.padding_offset + hdr.padding_size; 223 | hdr.payload_size = 0xb000; 224 | 225 | buf = malloc(hdr.size); 226 | if (buf == NULL) { 227 | perror("malloc"); 228 | return 1; 229 | } 230 | 231 | memset(buf, 0, hdr.size); 232 | memcpy(buf, &hdr, sizeof(struct AmlogicHeader)); 233 | memcpy(buf + hdr.padding_offset + hdr.padding_size, src_buf, 0xb000); 234 | 235 | SHA256_Init(&sha256_ctx); 236 | SHA256_Update(&sha256_ctx, buf, hdr.header_size); 237 | SHA256_Update(&sha256_ctx, buf + hdr.data_offset, hdr._offset2); 238 | memset(sha256_digest, 0, sizeof(sha256_digest)); 239 | SHA256_Final(sha256_digest, &sha256_ctx); 240 | memcpy(buf + hdr.digest_offset, sha256_digest, sizeof(sha256_digest)); 241 | 242 | fwrite(buf, 1, hdr.size, fout); 243 | 244 | if (do_fip(fout, fin) != 0) 245 | return 1; 246 | 247 | fclose(fout); 248 | fclose(fin); 249 | 250 | free(src_buf); 251 | 252 | return 0; 253 | } 254 | 255 | int main(int argc, char **argv) 256 | { 257 | if (argc < 3) { 258 | fprintf(stderr, "Usage: %s input output\n", argv[0]); 259 | return 1; 260 | } 261 | 262 | return boot_sig(argv[1], argv[2]); 263 | } 264 | -------------------------------------------------------------------------------- /amlinfo.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2017 Andreas Färber 3 | * 4 | * SPDX-License-Identifier: GPL-2.0+ 5 | */ 6 | 7 | #include 8 | #include 9 | #include 10 | #include 11 | 12 | #include "meson.h" 13 | 14 | static void print_aml_header(const struct AmlogicHeader *hdr) 15 | { 16 | printf("Size: 0x%" PRIx32 "\n", hdr->size); 17 | printf("Header size: 0x%" PRIx16 "\n", hdr->header_size); 18 | printf("Header version: %" PRIu16 "\n", hdr->header_version); 19 | printf("ID: 0x%" PRIx32 "\n", hdr->id); 20 | printf("Encrypted? %" PRIu32 "\n", hdr->encrypted); 21 | printf("Digest @ 0x%" PRIx32 ", len %" PRIu32 "\n", hdr->digest_offset, hdr->digest_size); 22 | printf("Data @ 0x%" PRIx32 ", len %" PRIu32 "\n", hdr->data_offset, hdr->data_size); 23 | printf("Padding @ 0x%" PRIx32 ", len %" PRIu32 "\n", hdr->padding_offset, hdr->padding_size); 24 | printf("_offset2: 0x%" PRIx32 "\n", hdr->_offset2); 25 | printf("pad2: 0x%" PRIx32 "\n", hdr->pad2); 26 | printf("Payload @ 0x%" PRIx32 ", len %" PRIu32 "\n", hdr->payload_offset, hdr->payload_size); 27 | printf("unknown: 0x%" PRIx32 "\n", hdr->unknown); 28 | printf("\n"); 29 | } 30 | 31 | static int info(char *filename) 32 | { 33 | uint8_t buf[16 + 64]; 34 | struct AmlogicHeader *hdr; 35 | FILE *f; 36 | long pos; 37 | int len; 38 | 39 | f = fopen(filename, "rb"); 40 | if (f == NULL) 41 | return 1; 42 | 43 | pos = 0; 44 | do { 45 | len = fread(buf, 1, 16 + 64, f); 46 | if (len >= 4 && strncmp(buf, AMLOGIC_SIGNATURE, 4) == 0) { 47 | hdr = (struct AmlogicHeader *)buf; 48 | pos += 0; 49 | } else if (len >= 16 + 4 && strncmp(buf + 16, AMLOGIC_SIGNATURE, 4) == 0) { 50 | hdr = (struct AmlogicHeader *)(buf + 16); 51 | pos += 16; 52 | } else if (len >= 4 && ((struct FipHeader *)buf)->name == FIP_SIGNATURE) { 53 | struct FipHeader *fip_hdr; 54 | long toc_pos = pos; 55 | int i = 0; 56 | 57 | fip_hdr = malloc(sizeof(struct FipHeader) + sizeof(struct FipEntry)); 58 | if (fip_hdr == NULL) 59 | return 1; 60 | fseek(f, toc_pos, SEEK_SET); 61 | len = fread(fip_hdr, 1, sizeof(struct FipHeader) + sizeof(struct FipEntry), f); 62 | printf("FIP header @ 0x%" PRIx64 " (flags 0x%" PRIx64 ")\n", pos, fip_hdr->flags); 63 | printf("\n"); 64 | while (fip_hdr->entries[0].size > 0) { 65 | printf("FIP TOC entry %u (flags 0x%" PRIx64 ")\n", i, fip_hdr->entries[0].flags); 66 | fseek(f, toc_pos + fip_hdr->entries[0].offset_address, SEEK_SET); 67 | len = fread(buf, 1, 64, f); 68 | if (len >= 4 && strncmp(buf, AMLOGIC_SIGNATURE, 4) == 0) { 69 | printf("@AML header @ 0x%" PRIx64 "\n", toc_pos + fip_hdr->entries[0].offset_address); 70 | print_aml_header((struct AmlogicHeader *)buf); 71 | } else { 72 | fprintf(stderr, "Unexpected FIP TOC entry contents\n"); 73 | } 74 | 75 | i++; 76 | fseek(f, toc_pos + sizeof(struct FipHeader) + i * sizeof(struct FipEntry), SEEK_SET); 77 | fread((void*)fip_hdr + sizeof(struct FipHeader), 1, sizeof(struct FipEntry), f); 78 | } 79 | free(fip_hdr); 80 | break; 81 | } else { 82 | fclose(f); 83 | return 1; 84 | } 85 | 86 | printf("@AML header @ 0x%" PRIx64 "\n", pos); 87 | print_aml_header(hdr); 88 | 89 | fseek(f, pos + hdr->size, SEEK_SET); 90 | } while ((pos = ftell(f)) > 0); 91 | 92 | fclose(f); 93 | return 0; 94 | } 95 | 96 | int main(int argc, char **argv) 97 | { 98 | if (argc < 2) { 99 | fprintf(stderr, "Usage: %s filename\n", argv[0]); 100 | return 1; 101 | } 102 | return info(argv[1]); 103 | } 104 | -------------------------------------------------------------------------------- /fip.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2017 Andreas Färber 3 | * 4 | * SPDX-License-Identifier: GPL-2.0+ OR MIT 5 | */ 6 | 7 | #ifndef MESON_FIP_H 8 | #define MESON_FIP_H 9 | 10 | #include 11 | 12 | #define FIP_SIGNATURE 0xaa640001 13 | 14 | struct __attribute((__packed__)) FipEntry { 15 | uint64_t uuid[2]; 16 | uint64_t offset_address; 17 | uint64_t size; 18 | uint64_t flags; 19 | }; 20 | 21 | struct __attribute((__packed__)) FipHeader { 22 | uint32_t name; 23 | uint32_t serial_number; 24 | uint64_t flags; 25 | struct FipEntry entries[0]; 26 | }; 27 | 28 | #endif 29 | -------------------------------------------------------------------------------- /meson.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2017 Andreas Färber 3 | * 4 | * SPDX-License-Identifier: GPL-2.0+ 5 | */ 6 | 7 | #ifndef MESON_COMMON_H 8 | #define MESON_COMMON_H 9 | 10 | #include "fip.h" 11 | #include "aml.h" 12 | 13 | #ifndef MIN 14 | #define MIN(a, b) (((a) < (b)) ? (a) : (b)) 15 | #endif 16 | 17 | #ifndef ROUND_UP 18 | #define ROUND_UP(x, a) (((x) + ((a) - 1)) & ~((a) - 1)) 19 | #endif 20 | 21 | #endif 22 | -------------------------------------------------------------------------------- /unamlbootsig.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2017 Andreas Färber 3 | * 4 | * SPDX-License-Identifier: GPL-2.0-or-later WITH openvpn-openssl-exception 5 | */ 6 | 7 | #include 8 | #include 9 | #include 10 | #include 11 | 12 | #include "meson.h" 13 | 14 | static int extract(const char *input, const char *output) 15 | { 16 | uint8_t buf[16 + sizeof(struct AmlogicHeader)]; 17 | struct AmlogicHeader *hdr; 18 | FILE *fin, *fout; 19 | long pos; 20 | int len; 21 | 22 | fin = fopen(input, "rb"); 23 | if (fin == NULL) 24 | return 1; 25 | 26 | fout = fopen(output, "wb"); 27 | if (fout == NULL) 28 | return 1; 29 | 30 | pos = 0; 31 | do { 32 | len = fread(buf, 1, 16 + sizeof(struct AmlogicHeader), fin); 33 | if (len >= 4 && strncmp(buf, AMLOGIC_SIGNATURE, 4) == 0) { 34 | hdr = (struct AmlogicHeader *)buf; 35 | } else if (len >= 16 + 4 && strncmp(buf + 16, AMLOGIC_SIGNATURE, 4) == 0) { 36 | hdr = (struct AmlogicHeader *)(buf + 16); 37 | pos += 16; 38 | } else if (len >= 4 && ((struct FipHeader *)buf)->name == FIP_SIGNATURE) { 39 | struct FipHeader *fip_hdr; 40 | uint8_t *src_buf; 41 | long toc_pos = pos; 42 | int i, n; 43 | 44 | fip_hdr = malloc(sizeof(struct FipHeader) + sizeof(struct FipEntry)); 45 | if (fip_hdr == NULL) 46 | return 1; 47 | fseek(fin, toc_pos, SEEK_SET); 48 | len = fread(fip_hdr, 1, sizeof(struct FipHeader) + sizeof(struct FipEntry), fin); 49 | printf("FIP header @ 0x%" PRIx64 " (flags 0x%" PRIx64 ")\n", pos, fip_hdr->flags); 50 | fwrite(fip_hdr, 1, sizeof(struct FipHeader), fout); 51 | n = 0; 52 | while (fip_hdr->entries[0].size > 0) { 53 | n++; 54 | fread((void*)fip_hdr + sizeof(struct FipHeader), 1, sizeof(struct FipEntry), fin); 55 | } 56 | fip_hdr = realloc(fip_hdr, sizeof(struct FipHeader) + (n + 1) * sizeof(struct FipEntry)); 57 | if (fip_hdr == NULL) 58 | return 1; 59 | 60 | fseek(fin, toc_pos + sizeof(struct FipHeader), SEEK_SET); 61 | fread((void*)fip_hdr + sizeof(struct FipHeader), 1, (n + 1) * sizeof(struct FipEntry), fin); 62 | fwrite((void *)fip_hdr + sizeof(struct FipHeader), 1, (n + 1) * sizeof(struct FipEntry), fout); 63 | src_buf = malloc(0x4000); 64 | if (src_buf == NULL) 65 | return 1; 66 | 67 | for (i = 0; i < n; i++) { 68 | printf("FIP TOC entry %u (flags 0x%" PRIx64 ")\n", i, fip_hdr->entries[i].flags); 69 | 70 | fseek(fin, toc_pos + fip_hdr->entries[i].offset_address, SEEK_SET); 71 | len = fread(buf, 1, sizeof(struct AmlogicHeader), fin); 72 | if (len >= 4 && strncmp(buf, AMLOGIC_SIGNATURE, 4) == 0) { 73 | long remaining; 74 | 75 | printf("@AML header @ 0x%" PRIx64 "\n", toc_pos + fip_hdr->entries[i].offset_address); 76 | hdr = (struct AmlogicHeader *)buf; 77 | fip_hdr->entries[i].size -= hdr->header_size + hdr->digest_size; 78 | pos = ftell(fout); 79 | pos = ROUND_UP(pos, 0x4000); 80 | fseek(fout, pos, SEEK_SET); 81 | fseek(fin, toc_pos + fip_hdr->entries[i].offset_address + fip_hdr->entries[i].size, SEEK_SET); 82 | len = fread(src_buf, 1, hdr->header_size + hdr->digest_size, fin); 83 | fwrite(src_buf, 1, len, fout); 84 | 85 | fseek(fin, toc_pos + fip_hdr->entries[i].offset_address + hdr->header_size + hdr->digest_size, SEEK_SET); 86 | remaining = fip_hdr->entries[i].size - hdr->header_size - hdr->digest_size; 87 | while (remaining > 0) { 88 | len = fread(src_buf, 1, MIN(remaining, 0x4000), fin); 89 | remaining -= len; 90 | fwrite(src_buf, 1, len, fout); 91 | } 92 | } else { 93 | fprintf(stderr, "Unexpected FIP TOC entry contents\n"); 94 | } 95 | } 96 | fseek(fout, 0xc000, SEEK_SET); 97 | fwrite(fip_hdr, 1, sizeof(struct FipHeader) + (n + 1) * sizeof(struct FipEntry), fout); 98 | fseek(fout, 0, SEEK_END); 99 | pos = ftell(fout); 100 | pos = ROUND_UP(pos, 0x4000); 101 | fseek(fout, pos - 1, SEEK_SET); 102 | buf[0] = 0; 103 | fwrite(buf, 1, 1, fout); 104 | free(fip_hdr); 105 | break; 106 | } else { 107 | fclose(fout); 108 | fclose(fin); 109 | return 1; 110 | } 111 | 112 | printf("@AML header @ 0x%" PRIx64 "\n", pos); 113 | 114 | fseek(fin, pos + hdr->payload_offset, SEEK_SET); 115 | uint8_t *src_buf = malloc(hdr->payload_size); 116 | if (src_buf == NULL) { 117 | fclose(fout); 118 | fclose(fin); 119 | return 1; 120 | } 121 | len = fread(src_buf, 1, hdr->payload_size, fin); 122 | fwrite(src_buf, 1, len, fout); 123 | free(src_buf); 124 | 125 | fseek(fin, pos + hdr->size, SEEK_SET); 126 | fseek(fout, 0xc000, SEEK_SET); 127 | } while ((pos = ftell(fin)) > 0); 128 | 129 | fclose(fout); 130 | fclose(fin); 131 | return 0; 132 | } 133 | 134 | int main(int argc, char **argv) 135 | { 136 | if (argc < 3) { 137 | fprintf(stderr, "Usage: %s input output\n", argv[0]); 138 | return 1; 139 | } 140 | return extract(argv[1], argv[2]); 141 | } 142 | --------------------------------------------------------------------------------