├── .gitignore ├── README.md └── bin ├── aml-linux-usb-burn ├── README.md ├── aml-flash └── tools │ ├── aml_image_v2_packer │ ├── decompressPara_4M.dump │ ├── update │ ├── usbbl2runpara_ddrinit.bin │ └── usbbl2runpara_runfipimg.bin ├── aml_image_v2_packer ├── build ├── cleanup ├── extract_initrd ├── flash ├── lib64 └── libc++.so ├── logo_img_packer ├── recreate ├── recreate_initrd ├── remount ├── repack ├── src ├── abootimg │ ├── .gitignore │ ├── Changelog │ ├── LICENSE │ ├── Makefile │ ├── README │ ├── abootimg-pack-initrd │ ├── abootimg-unpack-initrd │ ├── abootimg.c │ ├── bootimg.h │ └── debian │ │ ├── abootimg.1 │ │ ├── changelog │ │ ├── compat │ │ ├── control │ │ ├── copyright │ │ ├── dirs │ │ ├── docs │ │ ├── gbp.conf │ │ └── rules ├── aml_image_extractor.c └── simg2img │ ├── .gitignore │ ├── LICENSE │ ├── Makefile │ ├── README.md │ ├── append2simg.c │ ├── backed_block.c │ ├── backed_block.h │ ├── defs.h │ ├── img2simg.c │ ├── include │ └── sparse │ │ └── sparse.h │ ├── output_file.c │ ├── output_file.h │ ├── simg2img.c │ ├── simg2simg.c │ ├── simg_dump.py │ ├── sparse.c │ ├── sparse_crc32.c │ ├── sparse_crc32.h │ ├── sparse_defs.h │ ├── sparse_err.c │ ├── sparse_file.h │ ├── sparse_format.h │ └── sparse_read.c ├── unmount └── unpack /.gitignore: -------------------------------------------------------------------------------- 1 | *.img 2 | *.apk 3 | output 4 | bin/abootimg 5 | bin/simg2img 6 | bin/img2simg 7 | bin/aml_image_extractor 8 | tmp 9 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Linux AMLogic Toolkit 2 | 3 | Allows to unpack and repack AMLogic Android images on Linux systems without using the Customization Tool - works for Android 7. 4 | 5 | # Features 6 | * Unpack and repack any image 7 | * Mount and edit `system` partition 8 | * Unpack and repack `logo` partition (for bootup and upgrading logos) 9 | * Unpack and repack `boot` image and `initrd` ramdisk 10 | * Flash the image directly to a device without repacking it (faster than using the USB Burning Tool) 11 | * Works for Android 7 12 | * No need to unpack the image each time you want to use the tool 13 | 14 | # What it cannot do (yet) 15 | * Edit other partitions of the image such as `recovery` (you can still replace the `PARTITION` files by hand) 16 | 17 | # Dependencies 18 | * `zlib1g-dev` for `simg2img` and `img2simg` 19 | * `libblkid-dev` for `abootimg` (unpacking and repacking boot image) 20 | * the `i386` packages if needed (for the logo unpacking / repacking binary) 21 | 22 | # How to use it 23 | * Clone or download this repository 24 | * Install the dependencies 25 | * Move to the directory of the repository, and **stay there** 26 | * *(first time, or after a cleanup)* Run `./bin/build` to build the required tools 27 | * *(when editing a new image file)* Run `./bin/unpack input.img` to unpack `input.img` 28 | * The result is : 29 | * `output/image` : raw image files (`PARTITION` files) 30 | * `output/system` : system partition files 31 | * `output/logo` : logo partition files 32 | * `output/boot` : boot partition files 33 | * From now on you can edit the files of the `output` directory 34 | * Note that those files will be overwritten when repacking : 35 | * `output/image/system.PARTITION` 36 | * `output/image/boot.PARTITION` 37 | * `output/image/logo.PARTITION` 38 | * `output/boot/initrd.img` if using `./bin/extract_initrd` 39 | * If you happen to loose the `output/system` mounting point (after a reboot for instance), just run `./bin/remount` to mount it again 40 | * On the other hand, you can unmount the system partition using `./bin/unmount` 41 | * If you want to extract the `initrd` ramdisk, use the `./bin/extract_initrd` and `./bin/recreate_initrd` scripts (output in `output/initrd`) 42 | * **Be careful :** 43 | * Don't break everything by chmod'ing the whole `output/system` folder, because it will be replicated in the image and it won't boot ! 44 | * Don't rename the files in `output/boot.img` 45 | * If you extract and recreate the `initrd` ramdisk, its size will change and it will most likely break the boot image. To fix this, edit the `bootimg.cfg` file in `output/boot` to replicate the change in size (you can repack the image, let it fail and read the logs to see the new size). 46 | * When you have finished editing the files, run `./bin/repack output.img` to repack the image to `output.img` 47 | * Additionnaly, you can use `./bin/flash` to flash the image to a device through USB (you will need the udev rule, see https://github.com/Stane1983/aml-linux-usb-burn) 48 | * The device type (`gxl`) is hardcoded into the flashing script, edit it if you're not using S905, S905X or S919 49 | * Done ! 50 | 51 | # Troubleshooting 52 | * If you have a `file not found` error when trying to unpack and repack the logo partition, install the `i386` libraries by following the accepted answer of this post : https://unix.stackexchange.com/questions/13391/getting-not-found-message-when-running-a-32-bit-binary-on-a-64-bit-system 53 | 54 | # Credits 55 | 56 | * Thanks to Magendanz and adg for the unpacking and repacking method 57 | * https://github.com/khadas/utils for the `aml_image_v2_packer` and `logo_img_packer` binaries 58 | * https://github.com/anestisb/android-simg2img for the `simg2img` tool 59 | * https://github.com/ggrandou/abootimg for the `abootimg` tool 60 | * https://github.com/Stane1983/aml-linux-usb-burn for the flashing tool 61 | 62 | 63 | 64 | -------------------------------------------------------------------------------- /bin/aml-linux-usb-burn/README.md: -------------------------------------------------------------------------------- 1 | # Linux version of Amlogic USB Burning Tool 2 | 3 | This is just "copy" of files that can be found on Amlogic Openlinux [website][amlTools]. 4 | 5 | # Installation 6 | Create new u-dev rule for Amlogic devices (in /etc/udev/rules.d). My file is called 70-persistent-usb-amlogic.rules 7 | Content of file is: 8 | ``` 9 | SUBSYSTEM=="usb", ENV{DEVTYPE}=="usb_device", ATTR{idVendor}=="1b8e", ATTR{idProduct}=="c003", MODE:="0666", SYMLINK+="worldcup" 10 | ``` 11 | 12 | When you created your rule either reload udev rules or reboot your machine. Make sure that root folder of this repository is in your PATH variable (I simply put it inside my ~/bin folder) so I can call it from anywhere without specifying path. 13 | 14 | And this tool depends on `libusb` and `libusb-compat`, you must install those tools first. 15 | 16 | # How to use 17 | Connect your device and put it to USB burning mode, open terminal and navigate to folder where your aml_upgrade_package.img is and issue command: 18 | ``` 19 | aml-flash --img=aml_upgrade_package.img --soc=gxl --wipe --reset=n --parts=all 20 | ``` 21 | For more options, just issue aml-flash command. 22 | __soc__ paremeter can be gxl (S905, S905X and S912), axg (A113 audio SoC), txlx (TV SoC - T962), m8 (S802, S805 and S812) 23 | I tested this tool with sucess on S812, S905, S905X and S912. 24 | 25 | # Note 26 | This tool is for x64 Linux only. File aml_image_v2_packer is 32bit but update binary (inside tools folder) is 64bit so... 27 | 28 | [amlTools]: 29 | 30 | -------------------------------------------------------------------------------- /bin/aml-linux-usb-burn/tools/aml_image_v2_packer: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/natinusala/linux-amlogic-toolkit/5f05a88dea7b0bb321a2c84fe53d9a5b37c2c19a/bin/aml-linux-usb-burn/tools/aml_image_v2_packer -------------------------------------------------------------------------------- /bin/aml-linux-usb-burn/tools/decompressPara_4M.dump: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/natinusala/linux-amlogic-toolkit/5f05a88dea7b0bb321a2c84fe53d9a5b37c2c19a/bin/aml-linux-usb-burn/tools/decompressPara_4M.dump -------------------------------------------------------------------------------- /bin/aml-linux-usb-burn/tools/update: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/natinusala/linux-amlogic-toolkit/5f05a88dea7b0bb321a2c84fe53d9a5b37c2c19a/bin/aml-linux-usb-burn/tools/update -------------------------------------------------------------------------------- /bin/aml-linux-usb-burn/tools/usbbl2runpara_ddrinit.bin: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/natinusala/linux-amlogic-toolkit/5f05a88dea7b0bb321a2c84fe53d9a5b37c2c19a/bin/aml-linux-usb-burn/tools/usbbl2runpara_ddrinit.bin -------------------------------------------------------------------------------- /bin/aml-linux-usb-burn/tools/usbbl2runpara_runfipimg.bin: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/natinusala/linux-amlogic-toolkit/5f05a88dea7b0bb321a2c84fe53d9a5b37c2c19a/bin/aml-linux-usb-burn/tools/usbbl2runpara_runfipimg.bin -------------------------------------------------------------------------------- /bin/aml_image_v2_packer: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/natinusala/linux-amlogic-toolkit/5f05a88dea7b0bb321a2c84fe53d9a5b37c2c19a/bin/aml_image_v2_packer -------------------------------------------------------------------------------- /bin/build: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | ./bin/cleanup 4 | 5 | mkdir -p output/system 6 | mkdir -p output/image 7 | mkdir -p output/logo 8 | 9 | make -C bin/src/simg2img/ 10 | gcc bin/src/aml_image_extractor.c -o bin/aml_image_extractor 11 | 12 | cp bin/src/simg2img/simg2img bin/ 13 | cp bin/src/simg2img/img2simg bin/ 14 | 15 | make -C bin/src/abootimg/ 16 | cp bin/src/abootimg/abootimg bin/ 17 | 18 | echo "Build done" 19 | -------------------------------------------------------------------------------- /bin/cleanup: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | sudo umount output/system 4 | 5 | rm -rf output 6 | rm -rf tmp 7 | 8 | rm -f bin/simg2img 9 | rm -f bin/img2simg 10 | rm -f bin/aml_image_extractor 11 | rm -f bin/abootimg 12 | 13 | make -C bin/src/simg2img/ clean 14 | make -C bin/src/abootimg/ clean 15 | 16 | echo "Cleanup done" 17 | -------------------------------------------------------------------------------- /bin/extract_initrd: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | if [ -e output/image/system.img ] 3 | then 4 | rm -rf output/initrd 5 | mkdir -p output/initrd 6 | 7 | echo "Extracting initrd ramdisk..." 8 | cd output/initrd 9 | cat ../boot/initrd.img | gunzip | cpio -vid 10 | cd ../.. 11 | 12 | echo "Done" 13 | echo "Please note that the boot image size may have changed ; if repacking the image fails, look at the logs and edit the bootimg.cfg file to reflect the size change" 14 | else 15 | echo "Please unpack an image before trying to extract initrd" 16 | fi 17 | -------------------------------------------------------------------------------- /bin/flash: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | if [ -e output/image/system.img ] 4 | then 5 | sync 6 | 7 | bin/recreate 8 | 9 | echo "Flashing image to gxl device..." 10 | bin/aml-linux-usb-burn/aml-flash --soc=gxl --wipe --reset=n --parts=all 11 | 12 | sync 13 | 14 | echo "Done" 15 | else 16 | echo "Please unpack an image before trying to repack it" 17 | fi 18 | 19 | -------------------------------------------------------------------------------- /bin/lib64/libc++.so: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/natinusala/linux-amlogic-toolkit/5f05a88dea7b0bb321a2c84fe53d9a5b37c2c19a/bin/lib64/libc++.so -------------------------------------------------------------------------------- /bin/logo_img_packer: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/natinusala/linux-amlogic-toolkit/5f05a88dea7b0bb321a2c84fe53d9a5b37c2c19a/bin/logo_img_packer -------------------------------------------------------------------------------- /bin/recreate: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | if [ -e output/image/system.img ] 4 | then 5 | sync 6 | 7 | echo "Repacking logo..." 8 | rm -f output/image/logo.PARTITION 9 | bin/logo_img_packer -r output/logo output/image/logo.PARTITION 10 | 11 | echo "Converting back system.img to system.PARTITION..." 12 | rm -f output/image/system.PARTITION 13 | bin/img2simg output/image/system.img output/image/system.PARTITION 14 | 15 | echo "Repacking boot..." 16 | rm -f output/image/boot.PARTITION 17 | bin/abootimg --create output/image/boot.PARTITION -f output/boot/bootimg.cfg -k output/boot/zImage -r output/boot/initrd.img 18 | 19 | sync 20 | 21 | echo "Done" 22 | else 23 | echo "Please unpack an image before trying to repack it" 24 | fi 25 | 26 | -------------------------------------------------------------------------------- /bin/recreate_initrd: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | if [ -e output/initrd ] 4 | then 5 | echo "Recreating initrd ramdisk..." 6 | rm -f output/boot/initrd.img 7 | cd output/initrd 8 | find . | cpio --create --format='newc' | gzip > ../boot/initrd.img 9 | cd ../.. 10 | 11 | echo "Done" 12 | else 13 | echo "Please extract initrd before trying to recreate it" 14 | fi 15 | -------------------------------------------------------------------------------- /bin/remount: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | echo "Unmounting previous image..." 4 | sync 5 | sudo umount output/system 6 | 7 | echo "Remounting system image..." 8 | sudo mount -t ext4 -o loop,rw output/image/system.img output/system 9 | 10 | echo "Done" 11 | 12 | -------------------------------------------------------------------------------- /bin/repack: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | if [ -e output/image/system.img ] 4 | then 5 | if [ $# -eq 1 ] 6 | then 7 | sync 8 | 9 | bin/recreate 10 | 11 | echo "Packing image to $1..." 12 | bin/aml_image_v2_packer -r output/image/image.cfg output/image $1 13 | 14 | sync 15 | 16 | echo "Done" 17 | else 18 | echo "Usage: repack [output image]" 19 | fi 20 | else 21 | echo "Please unpack an image before trying to repack it" 22 | fi 23 | 24 | -------------------------------------------------------------------------------- /bin/src/abootimg/.gitignore: -------------------------------------------------------------------------------- 1 | abootimg 2 | version.h 3 | *.o 4 | *.swp 5 | -------------------------------------------------------------------------------- /bin/src/abootimg/Changelog: -------------------------------------------------------------------------------- 1 | 2011/07/14 - v0.6 2 | 3 | * fixes block device support, broken in v0.5 4 | * fixes crash if ramdisk is bigger than kernel 5 | * version.h no longer relies on git 6 | * build fix on GNU Hurd 7 | 8 | 2011/05/18 - v0.5 9 | 10 | * add support for freebsd, cygwin, and osx 11 | * explains why sanity check has failed 12 | * print version number in -help 13 | * pack-initrd -f support 14 | 15 | 2011/02/17 - v0.4 16 | 17 | * fix when page_size is null 18 | 19 | 2010/12/08 - v0.3 20 | 21 | * fix when updating kernel without ramdisk 22 | 23 | 2010/11/28 - v0.2 24 | 25 | * fix when updating ramdisk without the kernel 26 | * fix --create on block device 27 | * added pack-initrd and unpack-initrd helper tools 28 | 29 | 2010/11/25 - v0.1 30 | 31 | Initial release 32 | 33 | -------------------------------------------------------------------------------- /bin/src/abootimg/LICENSE: -------------------------------------------------------------------------------- 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 | -------------------------------------------------------------------------------- /bin/src/abootimg/Makefile: -------------------------------------------------------------------------------- 1 | 2 | CPPFLAGS=-DHAS_BLKID 3 | CFLAGS=-O3 -Wall 4 | LDLIBS=-lblkid 5 | 6 | all: abootimg 7 | 8 | version.h: 9 | if [ ! -f version.h ]; then \ 10 | if [ -d .git ]; then \ 11 | echo '#define VERSION_STR "$(shell git describe --tags --abbrev=0)"' > version.h; \ 12 | else \ 13 | echo '#define VERSION_STR ""' > version.h; \ 14 | fi \ 15 | fi 16 | 17 | abootimg.o: bootimg.h version.h 18 | 19 | clean: 20 | rm -f abootimg *.o version.h 21 | 22 | .PHONY: clean all 23 | 24 | -------------------------------------------------------------------------------- /bin/src/abootimg/README: -------------------------------------------------------------------------------- 1 | abootimg - manipulate Android Boot Images. 2 | ------------------------------------------ 3 | 4 | (c) 2010 Gilles Grandou 5 | 6 | 7 | 8 | * Android Boot Images 9 | --------------------- 10 | 11 | 12 | It a special partition format defined by the Android Open Source Porject. 13 | See bootimg.h in the source tree for more information about the structure. 14 | 15 | It's used by Android Bootloaders to boot the OS. 16 | 17 | Boot images mainly convey: 18 | - a kernel image 19 | - a ramdisk image 20 | - optionaly, a 2nd stage bootloader 21 | - the cmdline passed to the kernel when booting. 22 | 23 | The official tool used to create boot images is part of the Android Project, 24 | available here: 25 | 26 | http://android.git.kernel.org/?p=platform/system/core.git;a=tree;f=mkbootimg 27 | 28 | 29 | abootimg can work directly on block devices, or, the safest way, on a file image. 30 | File images can be read/written with dd: 31 | 32 | $ dd if=/dev/mmcblk0p2 of=boot.img 33 | $ dd if=boot.img of=/dev/mmcblk0p2 34 | 35 | You obviously need to have right access to the block device (using su, sudo, 36 | ...). 37 | 38 | 39 | Android Boot Image contains an 32 bytes Id. The specification does not actually 40 | mandates any specific implementation of this Id (it can be a timestamp, a CRC 41 | checksum, a SHA hash, ...). Bootloader appears to do nothing of this Id, it's 42 | solely here for tracking purpose. Currently abootimg does nothing with it, it's 43 | never touched/modified. 44 | 45 | 46 | 47 | * Building abootimg 48 | ------------------- 49 | 50 | On a linux system, it's simply as: 51 | 52 | $ make 53 | 54 | blkid library is needed to perform some sanity checks when writing boot image 55 | directly on a block device (to avoid writing a valid existing filesystem). 56 | 57 | 58 | 59 | * Looking at an Android Boot Image 60 | ---------------------------------- 61 | 62 | Basic Information can be extracted from a boot image, using: 63 | 64 | 65 | $ abootimg -i 66 | 67 | Here is an example: 68 | 69 | 70 | $ ./abootimg -i boot.img 71 | 72 | Android Boot Image Info: 73 | 74 | * file name = boot.img 75 | 76 | * image size = 8388608 bytes (8.00 MB) 77 | page size = 2048 bytes 78 | 79 | * Boot Name = "" 80 | 81 | * kernel size = 3002744 bytes (2.86 MB) 82 | ramdisk size = 1639626 bytes (1.56 MB) 83 | 84 | * load addresses: 85 | kernel: 0x10008000 86 | ramdisk: 0x11000000 87 | tags: 0x10000100 88 | 89 | * cmdline = mem=448M@0M nvmem=64M@448M vmalloc=320M video=tegrafb console=tty0 usbcore.old_scheme_first=1 quiet splash elevator=noop tegraboot=sdmmc cmdpart=1:7168:10240,2:17408:16384,3:35840:614400,4:4004864:27096064 90 | 91 | * id = 0x07571070 0x13950a6a 0x185c996f 0x9ab7b64d 0xcccd09bd 0x00000000 0x00000000 0x00000000 92 | 93 | 94 | 95 | * Extracting elements from an Android Boot Image 96 | ------------------------------------------------ 97 | 98 | All parts of boot image can be extracted with: 99 | 100 | $ abootimg -x [ [ [ []]]] 101 | 102 | Parts name are optional. Default ones are used if none are given: 103 | 104 | * bootimg.cfg for the configuration file 105 | * zImage for the Kernel image 106 | * initrd.img for the Ramdisk 107 | * stage2.img for the Second Stage image 108 | 109 | Here is an example: 110 | 111 | $ abootimg -x boot.img 112 | writing boot image config in bootimg.cfg 113 | extracting kernel in zImage 114 | extracting ramdisk in initrd.img 115 | 116 | 117 | 118 | * Boot Configuration file 119 | ------------------------- 120 | 121 | It's an editable ascii file which is basically a dump of the header content, 122 | to be able to rebuild a compatible image later. 123 | 124 | Each entry takes one line and is in the form of: 125 | 126 | = 127 | 128 | You can put any number of spaces/tab before/after the = 129 | is evalued starting from the fisrt non space character following the = 130 | until the end of line 131 | 132 | Numerical values can be given in decimal (12345) or hexadecimal (0x1234abcd). 133 | 134 | Known configuration entries are: 135 | 136 | * bootsize 137 | 138 | Indicate the size of the boot image to produce 139 | 140 | 141 | * pagesize 142 | 143 | All sizes have to be a multiple of the page size. 144 | Standard page size is 2048 bytes. I don't know if other page size 145 | are supported by Android bootloader 146 | 147 | * kerneladdr, ramdiskaddr, secondaddr, tagsaddr 148 | 149 | Address in RAM used to load the kernel, ramdisk, 2nd stage 150 | bootloader, and tags table. 151 | 152 | * name 153 | 154 | name given to the boot image. 155 | not really used by bootloader, but it's can be usefull to keep 156 | track of what the image actually contains 157 | 158 | * cmdline 159 | 160 | contains the command line passed to the kernel when booting 161 | 162 | 163 | 164 | * Updating an existing Android Boot Image 165 | ----------------------------------------- 166 | 167 | 168 | An existing valid Boot Image can be updated with: 169 | 170 | 171 | $ abootimg -u [-c "param=value"] [-f ] [-k ] [-r ] [-s ] 172 | 173 | Any part of the image can be individully updated. As an example: 174 | 175 | 176 | $ abootimg -u boot.img -k zImage.new 177 | 178 | will update the kernel 179 | 180 | $ abootimg -u boot.img -r initrd.new.img 181 | 182 | will update the image 183 | 184 | $ abootimg -u boot.img -k zImage.new -r initrd.new.img 185 | 186 | will update both 187 | 188 | 189 | Image configuration can be updated either by giving a configuration file 190 | (-f option) or by giving individual config entries on the command line (-c). 191 | Severial config entries (-c) can be given on comamd line. 192 | 193 | As an example: 194 | 195 | $ abootimg -u boot.img -f bootimg.new.cfg 196 | 197 | will update the configuration without touching the kernel 198 | nor the ramdisk 199 | 200 | $ abootimg -u boot.img -c "cmdline = mem=448M@0M nvmem=64M@448M vmalloc=320M \ 201 | video=tegrafb console=tty0 usbcore.old_scheme_first=1 quiet splash elevator=noop \ 202 | tegraboot=sdmmc tegrapart=recovery:700:a00:800,boot:1100:1000:800,mbr:2100:200:800,\ 203 | system:2300:25800:800,cache:27b00:32000:800,misc:59b00:400:800,userdata:5a000:9a600:800" 204 | 205 | update the boot conmand line 206 | 207 | $ abootimg -u boot.img -c "bootsize=0x500000" 208 | 209 | update the boot image size to 5MB. 210 | It's usefull if you want to shrink an existing image to make it 211 | fit in another smaller boot partition. 212 | (On Toshiba AC100, it allows you to take a part06.img and 213 | transform it to fit inside part05) 214 | 215 | 216 | The original boot image has to be valid, otherwise abootimg will refuse to 217 | update it. 218 | 219 | 220 | 221 | 222 | * Creating a new Android Boot Image from scratch 223 | ------------------------------------------------ 224 | 225 | 226 | A new boot image can be create with: 227 | 228 | $ abootimg --create [-c "param=value"] [-f ] -k -r [-s ] 229 | 230 | Parameters are the same than above for update. The only difference is that 231 | kernel and ramdisk are mandatory. 232 | 233 | 234 | 235 | * Working directly of Block Devices 236 | ----------------------------------- 237 | 238 | 239 | Instead of manipulating Boot Image regular file, you can work directly on boot 240 | block device. 241 | 242 | Note that, on AC100, the current kernel needs to be patched in order to have 243 | direct access to boot partitions (partitions 5 and 6). 244 | 245 | Some examples: 246 | 247 | $ sudo abootimg -i /dev/mmcblk0p2 248 | 249 | read the current boot partition 250 | 251 | $ sudo abootimg -u /dev/mmcblk0p2 -k arch/arm/boot/zImage 252 | 253 | update the boot partition with the kernel you have just built 254 | 255 | $ sudo abootimg -u /dev/mmcblk0p2 -c "cmdline=..." 256 | 257 | update the boot partition with a new boot cmdline 258 | 259 | $ sudo abootimg --create /dev/mmcblk0p2 -f boot.cfg -k zImage -r initrd.img 260 | 261 | overwrite the boot partition (which can be damaged) with a 262 | brand new image 263 | 264 | If abootimg has to write to a block device (-u and --create), some sanity 265 | check are performed: 266 | 267 | * you oviously need read/write access to the block device (so use su, 268 | sudo, ...) 269 | 270 | * the actual partition must not be identified as containing a valid 271 | filesystem as recognised by blkid library. Specifically, it cannot 272 | contains a ext2/3 filesystem (this avoids you to ovewrite your root 273 | filesystem by mistake) 274 | 275 | * the updated/created boot image has to be same size than the block 276 | device you try to write on. 277 | 278 | * in case of update, the current boot partition has to contain a valid 279 | Android Boot Image. 280 | 281 | Failing any of these tests will abort the operation on block device. 282 | 283 | It's by definition more risky to manipulate block device, as a bad 284 | manipulation can as bad manipulation can make your system unbootable if you 285 | don't fix it before the next reboot. 286 | 287 | On the other hand, manipulating the block device allows abootimg to prevent 288 | most of the stupid mistakes which can be made when writing boot image with dd 289 | (overwriting another filesystem, writing a boot image bigger than the 290 | partition, writing the wrong file or an invalid partition, ...) 291 | 292 | 293 | -------------------------------------------------------------------------------- /bin/src/abootimg/abootimg-pack-initrd: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | # 3 | 4 | if [ "$1" = "-f" ]; then 5 | forcewrite=yes 6 | shift 7 | fi 8 | 9 | initrd=${1:-initrd.img} 10 | ramdisk=${2:-ramdisk} 11 | 12 | if [ ! -d $ramdisk ]; then 13 | echo "$ramdisk does not exist." 14 | exit 1 15 | fi 16 | 17 | if [ -f $initrd -a -z "$forcewrite" ]; then 18 | echo "$initrd already exist." 19 | exit 1 20 | fi 21 | 22 | ( cd $ramdisk; find | sort | cpio --quiet -o -H newc ) | gzip > $initrd 23 | 24 | -------------------------------------------------------------------------------- /bin/src/abootimg/abootimg-unpack-initrd: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | # 3 | 4 | initrd=${1:-initrd.img} 5 | ramdisk=${2:-ramdisk} 6 | 7 | if [ ! -f $initrd ]; then 8 | echo "$initrd does not exist." 9 | exit 1 10 | fi 11 | 12 | if [ -d $ramdisk ]; then 13 | echo "$ramdisk already exists." 14 | exit 1 15 | fi 16 | 17 | mkdir -p $ramdisk 18 | 19 | zcat $initrd | ( cd $ramdisk; cpio -i ) 20 | 21 | -------------------------------------------------------------------------------- /bin/src/abootimg/abootimg.c: -------------------------------------------------------------------------------- 1 | /* abootimg - Manipulate (read, modify, create) Android Boot Images 2 | * Copyright (c) 2010-2011 Gilles Grandou 3 | * 4 | * This program is free software; you can redistribute it and/or 5 | * modify it under the terms of the GNU General Public License 6 | * as published by the Free Software Foundation; either version 2 7 | * of the License, or (at your option) any later version. 8 | * 9 | * This program is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU General Public License 15 | * along with this program; if not, write to the Free Software 16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. 17 | */ 18 | 19 | 20 | #include 21 | #include 22 | #include 23 | #include 24 | #include 25 | #include 26 | #include 27 | #include 28 | 29 | 30 | #ifdef __linux__ 31 | #include 32 | #include /* BLKGETSIZE64 */ 33 | #endif 34 | 35 | #ifdef __CYGWIN__ 36 | #include 37 | #include /* BLKGETSIZE64 */ 38 | #endif 39 | 40 | #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) 41 | #include /* DIOCGMEDIASIZE */ 42 | #include 43 | #endif 44 | 45 | #if defined(__APPLE__) 46 | # include /* DKIOCGETBLOCKCOUNT */ 47 | #endif 48 | 49 | 50 | #ifdef HAS_BLKID 51 | #include 52 | #endif 53 | 54 | #include "version.h" 55 | #include "bootimg.h" 56 | 57 | 58 | enum command { 59 | none, 60 | help, 61 | info, 62 | extract, 63 | update, 64 | create 65 | }; 66 | 67 | 68 | typedef struct 69 | { 70 | unsigned size; 71 | int is_blkdev; 72 | 73 | char* fname; 74 | char* config_fname; 75 | char* kernel_fname; 76 | char* ramdisk_fname; 77 | char* second_fname; 78 | 79 | FILE* stream; 80 | 81 | boot_img_hdr header; 82 | 83 | char* kernel; 84 | char* ramdisk; 85 | char* second; 86 | } t_abootimg; 87 | 88 | 89 | #define MAX_CONF_LEN 4096 90 | char config_args[MAX_CONF_LEN] = ""; 91 | 92 | 93 | 94 | void abort_perror(char* str) 95 | { 96 | perror(str); 97 | exit(errno); 98 | } 99 | 100 | void abort_printf(char *fmt, ...) 101 | { 102 | va_list args; 103 | va_start(args, fmt); 104 | vfprintf(stderr, fmt, args); 105 | va_end(args); 106 | fprintf(stderr, "\n"); 107 | exit(1); 108 | } 109 | 110 | 111 | int blkgetsize(int fd, unsigned long long *pbsize) 112 | { 113 | # if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) 114 | return ioctl(fd, DIOCGMEDIASIZE, pbsize); 115 | # elif defined(__APPLE__) 116 | return ioctl(fd, DKIOCGETBLOCKCOUNT, pbsize); 117 | # elif defined(__NetBSD__) 118 | // does a suitable ioctl exist? 119 | // return (ioctl(fd, DIOCGDINFO, &label) == -1); 120 | return 1; 121 | # elif defined(__linux__) || defined(__CYGWIN__) 122 | return ioctl(fd, BLKGETSIZE64, pbsize); 123 | # elif defined(__GNU__) 124 | // does a suitable ioctl for HURD exist? 125 | return 1; 126 | # else 127 | return 1; 128 | # endif 129 | 130 | } 131 | 132 | void print_usage(void) 133 | { 134 | printf ( 135 | " abootimg - manipulate Android Boot Images.\n" 136 | " (c) 2010-2011 Gilles Grandou \n" 137 | " " VERSION_STR "\n" 138 | "\n" 139 | " abootimg [-h]\n" 140 | "\n" 141 | " print usage\n" 142 | "\n" 143 | " abootimg -i \n" 144 | "\n" 145 | " print boot image information\n" 146 | "\n" 147 | " abootimg -x [ [ [ []]]]\n" 148 | "\n" 149 | " extract objects from boot image:\n" 150 | " - config file (default name bootimg.cfg)\n" 151 | " - kernel image (default name zImage)\n" 152 | " - ramdisk image (default name initrd.img)\n" 153 | " - second stage image (default name stage2.img)\n" 154 | "\n" 155 | " abootimg -u [-c \"param=value\"] [-f ] [-k ] [-r ] [-s ]\n" 156 | "\n" 157 | " update a current boot image with objects given in command line\n" 158 | " - header informations given in arguments (several can be provided)\n" 159 | " - header informations given in config file\n" 160 | " - kernel image\n" 161 | " - ramdisk image\n" 162 | " - second stage image\n" 163 | "\n" 164 | " bootimg has to be valid Android Boot Image, or the update will abort.\n" 165 | "\n" 166 | " abootimg --create [-c \"param=value\"] [-f ] -k -r [-s ]\n" 167 | "\n" 168 | " create a new image from scratch.\n" 169 | " if the boot image file is a block device, sanity check will be performed to avoid overwriting a existing\n" 170 | " filesystem.\n" 171 | "\n" 172 | " argurments are the same than for -u.\n" 173 | " kernel and ramdisk are mandatory.\n" 174 | "\n" 175 | ); 176 | } 177 | 178 | 179 | enum command parse_args(int argc, char** argv, t_abootimg* img) 180 | { 181 | enum command cmd = none; 182 | int i; 183 | 184 | if (argc<2) 185 | return none; 186 | 187 | if (!strcmp(argv[1], "-h")) { 188 | return help; 189 | } 190 | else if (!strcmp(argv[1], "-i")) { 191 | cmd=info; 192 | } 193 | else if (!strcmp(argv[1], "-x")) { 194 | cmd=extract; 195 | } 196 | else if (!strcmp(argv[1], "-u")) { 197 | cmd=update; 198 | } 199 | else if (!strcmp(argv[1], "--create")) { 200 | cmd=create; 201 | } 202 | else 203 | return none; 204 | 205 | switch(cmd) { 206 | case none: 207 | case help: 208 | break; 209 | 210 | case info: 211 | if (argc != 3) 212 | return none; 213 | img->fname = argv[2]; 214 | break; 215 | 216 | case extract: 217 | if ((argc < 3) || (argc > 7)) 218 | return none; 219 | img->fname = argv[2]; 220 | if (argc >= 4) 221 | img->config_fname = argv[3]; 222 | if (argc >= 5) 223 | img->kernel_fname = argv[4]; 224 | if (argc >= 6) 225 | img->ramdisk_fname = argv[5]; 226 | if (argc >= 7) 227 | img->second_fname = argv[6]; 228 | break; 229 | 230 | case update: 231 | case create: 232 | if (argc < 3) 233 | return none; 234 | img->fname = argv[2]; 235 | img->config_fname = NULL; 236 | img->kernel_fname = NULL; 237 | img->ramdisk_fname = NULL; 238 | img->second_fname = NULL; 239 | for(i=3; i= argc) 242 | return none; 243 | unsigned len = strlen(argv[i]); 244 | if (strlen(config_args)+len+1 >= MAX_CONF_LEN) 245 | abort_printf("too many config parameters.\n"); 246 | strcat(config_args, argv[i]); 247 | strcat(config_args, "\n"); 248 | } 249 | else if (!strcmp(argv[i], "-f")) { 250 | if (++i >= argc) 251 | return none; 252 | img->config_fname = argv[i]; 253 | } 254 | else if (!strcmp(argv[i], "-k")) { 255 | if (++i >= argc) 256 | return none; 257 | img->kernel_fname = argv[i]; 258 | } 259 | else if (!strcmp(argv[i], "-r")) { 260 | if (++i >= argc) 261 | return none; 262 | img->ramdisk_fname = argv[i]; 263 | } 264 | else if (!strcmp(argv[i], "-s")) { 265 | if (++i >= argc) 266 | return none; 267 | img->second_fname = argv[i]; 268 | } 269 | else 270 | return none; 271 | } 272 | break; 273 | } 274 | 275 | return cmd; 276 | } 277 | 278 | 279 | 280 | int check_boot_img_header(t_abootimg* img) 281 | { 282 | if (strncmp((char*)(img->header.magic), BOOT_MAGIC, BOOT_MAGIC_SIZE)) { 283 | fprintf(stderr, "%s: no Android Magic Value\n", img->fname); 284 | return 1; 285 | } 286 | 287 | if (!(img->header.kernel_size)) { 288 | fprintf(stderr, "%s: kernel size is null\n", img->fname); 289 | return 1; 290 | } 291 | 292 | if (!(img->header.ramdisk_size)) { 293 | fprintf(stderr, "%s: ramdisk size is null\n", img->fname); 294 | return 1; 295 | } 296 | 297 | unsigned page_size = img->header.page_size; 298 | if (!page_size) { 299 | fprintf(stderr, "%s: Image page size is null\n", img->fname); 300 | return 1; 301 | } 302 | 303 | unsigned n = (img->header.kernel_size + page_size - 1) / page_size; 304 | unsigned m = (img->header.ramdisk_size + page_size - 1) / page_size; 305 | unsigned o = (img->header.second_size + page_size - 1) / page_size; 306 | 307 | unsigned total_size = (1+n+m+o)*page_size; 308 | 309 | if (total_size > img->size) { 310 | fprintf(stderr, "%s: sizes mismatches in boot image\n", img->fname); 311 | return 1; 312 | } 313 | 314 | return 0; 315 | } 316 | 317 | 318 | 319 | void check_if_block_device(t_abootimg* img) 320 | { 321 | struct stat st; 322 | 323 | if (stat(img->fname, &st)) 324 | if (errno != ENOENT) { 325 | printf("errno=%d\n", errno); 326 | abort_perror(img->fname); 327 | } 328 | 329 | #ifdef HAS_BLKID 330 | if (S_ISBLK(st.st_mode)) { 331 | img->is_blkdev = 1; 332 | 333 | char* type = blkid_get_tag_value(NULL, "TYPE", img->fname); 334 | if (type) 335 | abort_printf("%s: refuse to write on a valid partition type (%s)\n", img->fname, type); 336 | 337 | int fd = open(img->fname, O_RDONLY); 338 | if (fd == -1) 339 | abort_perror(img->fname); 340 | 341 | unsigned long long bsize = 0; 342 | if (blkgetsize(fd, &bsize)) 343 | abort_perror(img->fname); 344 | img->size = bsize; 345 | 346 | close(fd); 347 | } 348 | #endif 349 | } 350 | 351 | 352 | 353 | void open_bootimg(t_abootimg* img, char* mode) 354 | { 355 | img->stream = fopen(img->fname, mode); 356 | if (!img->stream) 357 | abort_perror(img->fname); 358 | } 359 | 360 | 361 | 362 | void read_header(t_abootimg* img) 363 | { 364 | size_t rb = fread(&img->header, sizeof(boot_img_hdr), 1, img->stream); 365 | if ((rb!=1) || ferror(img->stream)) 366 | abort_perror(img->fname); 367 | else if (feof(img->stream)) 368 | abort_printf("%s: cannot read image header\n", img->fname); 369 | 370 | struct stat s; 371 | int fd = fileno(img->stream); 372 | if (fstat(fd, &s)) 373 | abort_perror(img->fname); 374 | 375 | if (S_ISBLK(s.st_mode)) { 376 | unsigned long long bsize = 0; 377 | 378 | if (blkgetsize(fd, &bsize)) 379 | abort_perror(img->fname); 380 | img->size = bsize; 381 | img->is_blkdev = 1; 382 | } 383 | else { 384 | img->size = s.st_size; 385 | img->is_blkdev = 0; 386 | } 387 | 388 | if (check_boot_img_header(img)) 389 | abort_printf("%s: not a valid Android Boot Image.\n", img->fname); 390 | } 391 | 392 | 393 | 394 | void update_header_entry(t_abootimg* img, char* cmd) 395 | { 396 | char *p; 397 | char *token; 398 | char *endtoken; 399 | char *value; 400 | 401 | p = strchr(cmd, '\n'); 402 | if (p) 403 | *p = '\0'; 404 | 405 | p = cmd; 406 | p += strspn(p, " \t"); 407 | token = p; 408 | 409 | p += strcspn(p, " =\t"); 410 | endtoken = p; 411 | p += strspn(p, " \t"); 412 | 413 | if (*p++ != '=') 414 | goto err; 415 | 416 | p += strspn(p, " \t"); 417 | value = p; 418 | 419 | *endtoken = '\0'; 420 | 421 | unsigned valuenum = strtoul(value, NULL, 0); 422 | 423 | if (!strcmp(token, "cmdline")) { 424 | unsigned len = strlen(value); 425 | if (len >= BOOT_ARGS_SIZE) 426 | abort_printf("cmdline length (%d) is too long (max %d)", len, BOOT_ARGS_SIZE-1); 427 | memset(img->header.cmdline, 0, BOOT_ARGS_SIZE); 428 | strcpy((char*)(img->header.cmdline), value); 429 | } 430 | else if (!strncmp(token, "name", 4)) { 431 | strncpy((char*)(img->header.name), value, BOOT_NAME_SIZE); 432 | img->header.name[BOOT_NAME_SIZE-1] = '\0'; 433 | } 434 | else if (!strncmp(token, "bootsize", 8)) { 435 | if (img->is_blkdev && (img->size != valuenum)) 436 | abort_printf("%s: cannot change Boot Image size for a block device\n", img->fname); 437 | img->size = valuenum; 438 | } 439 | else if (!strncmp(token, "pagesize", 8)) { 440 | img->header.page_size = valuenum; 441 | } 442 | else if (!strncmp(token, "kerneladdr", 10)) { 443 | img->header.kernel_addr = valuenum; 444 | } 445 | else if (!strncmp(token, "ramdiskaddr", 11)) { 446 | img->header.ramdisk_addr = valuenum; 447 | } 448 | else if (!strncmp(token, "secondaddr", 10)) { 449 | img->header.second_addr = valuenum; 450 | } 451 | else if (!strncmp(token, "tagsaddr", 8)) { 452 | img->header.tags_addr = valuenum; 453 | } 454 | else 455 | goto err; 456 | return; 457 | 458 | err: 459 | abort_printf("%s: bad config entry\n", token); 460 | } 461 | 462 | 463 | void update_header(t_abootimg* img) 464 | { 465 | if (img->config_fname) { 466 | FILE* config_file = fopen(img->config_fname, "r"); 467 | if (!config_file) 468 | abort_perror(img->config_fname); 469 | 470 | printf("reading config file %s\n", img->config_fname); 471 | 472 | char* line = NULL; 473 | size_t len = 0; 474 | int read; 475 | 476 | while ((read = getline(&line, &len, config_file)) != -1) { 477 | update_header_entry(img, line); 478 | free(line); 479 | line = NULL; 480 | } 481 | if (ferror(config_file)) 482 | abort_perror(img->config_fname); 483 | } 484 | 485 | unsigned len = strlen(config_args); 486 | if (len) { 487 | FILE* config_file = fmemopen(config_args, len, "r"); 488 | if (!config_file) 489 | abort_perror("-c args"); 490 | 491 | printf("reading config args\n"); 492 | 493 | char* line = NULL; 494 | size_t len = 0; 495 | int read; 496 | 497 | while ((read = getline(&line, &len, config_file)) != -1) { 498 | update_header_entry(img, line); 499 | free(line); 500 | line = NULL; 501 | } 502 | if (ferror(config_file)) 503 | abort_perror("-c args"); 504 | } 505 | } 506 | 507 | 508 | 509 | void update_images(t_abootimg *img) 510 | { 511 | unsigned page_size = img->header.page_size; 512 | unsigned ksize = img->header.kernel_size; 513 | unsigned rsize = img->header.ramdisk_size; 514 | unsigned ssize = img->header.second_size; 515 | 516 | if (!page_size) 517 | abort_printf("%s: Image page size is null\n", img->fname); 518 | 519 | unsigned n = (ksize + page_size - 1) / page_size; 520 | unsigned m = (rsize + page_size - 1) / page_size; 521 | unsigned o = (ssize + page_size - 1) / page_size; 522 | 523 | unsigned roffset = (1+n)*page_size; 524 | unsigned soffset = (1+n+m)*page_size; 525 | 526 | if (img->kernel_fname) { 527 | printf("reading kernel from %s\n", img->kernel_fname); 528 | FILE* stream = fopen(img->kernel_fname, "r"); 529 | if (!stream) 530 | abort_perror(img->kernel_fname); 531 | struct stat st; 532 | if (fstat(fileno(stream), &st)) 533 | abort_perror(img->kernel_fname); 534 | ksize = st.st_size; 535 | char* k = malloc(ksize); 536 | if (!k) 537 | abort_perror(""); 538 | size_t rb = fread(k, ksize, 1, stream); 539 | if ((rb!=1) || ferror(stream)) 540 | abort_perror(img->kernel_fname); 541 | else if (feof(stream)) 542 | abort_printf("%s: cannot read kernel\n", img->kernel_fname); 543 | fclose(stream); 544 | img->header.kernel_size = ksize; 545 | img->kernel = k; 546 | } 547 | 548 | if (img->ramdisk_fname) { 549 | printf("reading ramdisk from %s\n", img->ramdisk_fname); 550 | FILE* stream = fopen(img->ramdisk_fname, "r"); 551 | if (!stream) 552 | abort_perror(img->ramdisk_fname); 553 | struct stat st; 554 | if (fstat(fileno(stream), &st)) 555 | abort_perror(img->ramdisk_fname); 556 | rsize = st.st_size; 557 | char* r = malloc(rsize); 558 | if (!r) 559 | abort_perror(""); 560 | size_t rb = fread(r, rsize, 1, stream); 561 | if ((rb!=1) || ferror(stream)) 562 | abort_perror(img->ramdisk_fname); 563 | else if (feof(stream)) 564 | abort_printf("%s: cannot read ramdisk\n", img->ramdisk_fname); 565 | fclose(stream); 566 | img->header.ramdisk_size = rsize; 567 | img->ramdisk = r; 568 | } 569 | else if (img->kernel) { 570 | // if kernel is updated, copy the ramdisk from original image 571 | char* r = malloc(rsize); 572 | if (!r) 573 | abort_perror(""); 574 | if (fseek(img->stream, roffset, SEEK_SET)) 575 | abort_perror(img->fname); 576 | size_t rb = fread(r, rsize, 1, img->stream); 577 | if ((rb!=1) || ferror(img->stream)) 578 | abort_perror(img->fname); 579 | else if (feof(img->stream)) 580 | abort_printf("%s: cannot read ramdisk\n", img->fname); 581 | img->ramdisk = r; 582 | } 583 | 584 | if (img->second_fname) { 585 | printf("reading second stage from %s\n", img->second_fname); 586 | FILE* stream = fopen(img->second_fname, "r"); 587 | if (!stream) 588 | abort_perror(img->second_fname); 589 | struct stat st; 590 | if (fstat(fileno(stream), &st)) 591 | abort_perror(img->second_fname); 592 | ssize = st.st_size; 593 | char* s = malloc(ssize); 594 | if (!s) 595 | abort_perror(""); 596 | size_t rb = fread(s, ssize, 1, stream); 597 | if ((rb!=1) || ferror(stream)) 598 | abort_perror(img->second_fname); 599 | else if (feof(stream)) 600 | abort_printf("%s: cannot read second stage\n", img->second_fname); 601 | fclose(stream); 602 | img->header.second_size = ssize; 603 | img->second = s; 604 | } 605 | else if (img->ramdisk && img->header.second_size) { 606 | // if ramdisk is updated, copy the second stage from original image 607 | char* s = malloc(ssize); 608 | if (!s) 609 | abort_perror(""); 610 | if (fseek(img->stream, soffset, SEEK_SET)) 611 | abort_perror(img->fname); 612 | size_t rb = fread(s, ssize, 1, img->stream); 613 | if ((rb!=1) || ferror(img->stream)) 614 | abort_perror(img->fname); 615 | else if (feof(img->stream)) 616 | abort_printf("%s: cannot read second stage\n", img->fname); 617 | img->second = s; 618 | } 619 | 620 | n = (img->header.kernel_size + page_size - 1) / page_size; 621 | m = (img->header.ramdisk_size + page_size - 1) / page_size; 622 | o = (img->header.second_size + page_size - 1) / page_size; 623 | unsigned total_size = (1+n+m+o)*page_size; 624 | 625 | if (!img->size) 626 | img->size = total_size; 627 | else if (total_size > img->size) 628 | abort_printf("%s: updated is too big for the Boot Image (%u vs %u bytes)\n", img->fname, total_size, img->size); 629 | } 630 | 631 | 632 | 633 | void write_bootimg(t_abootimg* img) 634 | { 635 | unsigned psize; 636 | char* padding; 637 | 638 | printf ("Writing Boot Image %s\n", img->fname); 639 | 640 | psize = img->header.page_size; 641 | padding = calloc(psize, 1); 642 | if (!padding) 643 | abort_perror(""); 644 | 645 | unsigned n = (img->header.kernel_size + psize - 1) / psize; 646 | unsigned m = (img->header.ramdisk_size + psize - 1) / psize; 647 | //unsigned o = (img->header.second_size + psize - 1) / psize; 648 | 649 | if (fseek(img->stream, 0, SEEK_SET)) 650 | abort_perror(img->fname); 651 | 652 | fwrite(&img->header, sizeof(img->header), 1, img->stream); 653 | if (ferror(img->stream)) 654 | abort_perror(img->fname); 655 | 656 | fwrite(padding, psize - sizeof(img->header), 1, img->stream); 657 | if (ferror(img->stream)) 658 | abort_perror(img->fname); 659 | 660 | if (img->kernel) { 661 | fwrite(img->kernel, img->header.kernel_size, 1, img->stream); 662 | if (ferror(img->stream)) 663 | abort_perror(img->fname); 664 | 665 | fwrite(padding, psize - (img->header.kernel_size % psize), 1, img->stream); 666 | if (ferror(img->stream)) 667 | abort_perror(img->fname); 668 | } 669 | 670 | if (img->ramdisk) { 671 | if (fseek(img->stream, (1+n)*psize, SEEK_SET)) 672 | abort_perror(img->fname); 673 | 674 | fwrite(img->ramdisk, img->header.ramdisk_size, 1, img->stream); 675 | if (ferror(img->stream)) 676 | abort_perror(img->fname); 677 | 678 | fwrite(padding, psize - (img->header.ramdisk_size % psize), 1, img->stream); 679 | if (ferror(img->stream)) 680 | abort_perror(img->fname); 681 | } 682 | 683 | if (img->header.second_size) { 684 | if (fseek(img->stream, (1+n+m)*psize, SEEK_SET)) 685 | abort_perror(img->fname); 686 | 687 | fwrite(img->second, img->header.second_size, 1, img->stream); 688 | if (ferror(img->stream)) 689 | abort_perror(img->fname); 690 | 691 | fwrite(padding, psize - (img->header.second_size % psize), 1, img->stream); 692 | if (ferror(img->stream)) 693 | abort_perror(img->fname); 694 | } 695 | 696 | ftruncate (fileno(img->stream), img->size); 697 | 698 | free(padding); 699 | } 700 | 701 | 702 | 703 | void print_bootimg_info(t_abootimg* img) 704 | { 705 | printf ("\nAndroid Boot Image Info:\n\n"); 706 | 707 | printf ("* file name = %s %s\n\n", img->fname, img->is_blkdev ? "[block device]":""); 708 | 709 | printf ("* image size = %u bytes (%.2f MB)\n", img->size, (double)img->size/0x100000); 710 | printf (" page size = %u bytes\n\n", img->header.page_size); 711 | 712 | printf ("* Boot Name = \"%s\"\n\n", img->header.name); 713 | 714 | unsigned kernel_size = img->header.kernel_size; 715 | unsigned ramdisk_size = img->header.ramdisk_size; 716 | unsigned second_size = img->header.second_size; 717 | 718 | printf ("* kernel size = %u bytes (%.2f MB)\n", kernel_size, (double)kernel_size/0x100000); 719 | printf (" ramdisk size = %u bytes (%.2f MB)\n", ramdisk_size, (double)ramdisk_size/0x100000); 720 | if (second_size) 721 | printf (" second stage size = %u bytes (%.2f MB)\n", second_size, (double)second_size/0x100000); 722 | 723 | printf ("\n* load addresses:\n"); 724 | printf (" kernel: 0x%08x\n", img->header.kernel_addr); 725 | printf (" ramdisk: 0x%08x\n", img->header.ramdisk_addr); 726 | if (second_size) 727 | printf (" second stage: 0x%08x\n", img->header.second_addr); 728 | printf (" tags: 0x%08x\n\n", img->header.tags_addr); 729 | 730 | if (img->header.cmdline[0]) 731 | printf ("* cmdline = %s\n\n", img->header.cmdline); 732 | else 733 | printf ("* empty cmdline\n"); 734 | 735 | printf ("* id = "); 736 | int i; 737 | for (i=0; i<8; i++) 738 | printf ("0x%08x ", img->header.id[i]); 739 | printf ("\n\n"); 740 | } 741 | 742 | 743 | 744 | void write_bootimg_config(t_abootimg* img) 745 | { 746 | printf ("writing boot image config in %s\n", img->config_fname); 747 | 748 | FILE* config_file = fopen(img->config_fname, "w"); 749 | if (!config_file) 750 | abort_perror(img->config_fname); 751 | 752 | fprintf(config_file, "bootsize = 0x%x\n", img->size); 753 | fprintf(config_file, "pagesize = 0x%x\n", img->header.page_size); 754 | 755 | fprintf(config_file, "kerneladdr = 0x%x\n", img->header.kernel_addr); 756 | fprintf(config_file, "ramdiskaddr = 0x%x\n", img->header.ramdisk_addr); 757 | fprintf(config_file, "secondaddr = 0x%x\n", img->header.second_addr); 758 | fprintf(config_file, "tagsaddr = 0x%x\n", img->header.tags_addr); 759 | 760 | fprintf(config_file, "name = %s\n", img->header.name); 761 | fprintf(config_file, "cmdline = %s\n", img->header.cmdline); 762 | 763 | fclose(config_file); 764 | } 765 | 766 | 767 | 768 | void extract_kernel(t_abootimg* img) 769 | { 770 | unsigned psize = img->header.page_size; 771 | unsigned ksize = img->header.kernel_size; 772 | 773 | printf ("extracting kernel in %s\n", img->kernel_fname); 774 | 775 | void* k = malloc(ksize); 776 | if (!k) 777 | abort_perror(NULL); 778 | 779 | if (fseek(img->stream, psize, SEEK_SET)) 780 | abort_perror(img->fname); 781 | 782 | size_t rb = fread(k, ksize, 1, img->stream); 783 | if ((rb!=1) || ferror(img->stream)) 784 | abort_perror(img->fname); 785 | 786 | FILE* kernel_file = fopen(img->kernel_fname, "w"); 787 | if (!kernel_file) 788 | abort_perror(img->kernel_fname); 789 | 790 | fwrite(k, ksize, 1, kernel_file); 791 | if (ferror(kernel_file)) 792 | abort_perror(img->kernel_fname); 793 | 794 | fclose(kernel_file); 795 | free(k); 796 | } 797 | 798 | 799 | 800 | void extract_ramdisk(t_abootimg* img) 801 | { 802 | unsigned psize = img->header.page_size; 803 | unsigned ksize = img->header.kernel_size; 804 | unsigned rsize = img->header.ramdisk_size; 805 | 806 | unsigned n = (ksize + psize - 1) / psize; 807 | unsigned roffset = (1+n)*psize; 808 | 809 | printf ("extracting ramdisk in %s\n", img->ramdisk_fname); 810 | 811 | void* r = malloc(rsize); 812 | if (!r) 813 | abort_perror(NULL); 814 | 815 | if (fseek(img->stream, roffset, SEEK_SET)) 816 | abort_perror(img->fname); 817 | 818 | size_t rb = fread(r, rsize, 1, img->stream); 819 | if ((rb!=1) || ferror(img->stream)) 820 | abort_perror(img->fname); 821 | 822 | FILE* ramdisk_file = fopen(img->ramdisk_fname, "w"); 823 | if (!ramdisk_file) 824 | abort_perror(img->ramdisk_fname); 825 | 826 | fwrite(r, rsize, 1, ramdisk_file); 827 | if (ferror(ramdisk_file)) 828 | abort_perror(img->ramdisk_fname); 829 | 830 | fclose(ramdisk_file); 831 | free(r); 832 | } 833 | 834 | 835 | 836 | void extract_second(t_abootimg* img) 837 | { 838 | unsigned psize = img->header.page_size; 839 | unsigned ksize = img->header.kernel_size; 840 | unsigned rsize = img->header.ramdisk_size; 841 | unsigned ssize = img->header.second_size; 842 | 843 | if (!ssize) // Second Stage not present 844 | return; 845 | 846 | unsigned n = (rsize + psize - 1)/psize + (ksize + psize - 1)/psize; 847 | unsigned soffset = (1+n)*psize; 848 | 849 | printf ("extracting second stage image in %s\n", img->second_fname); 850 | 851 | void* s = malloc(ksize); 852 | if (!s) 853 | abort_perror(NULL); 854 | 855 | if (fseek(img->stream, soffset, SEEK_SET)) 856 | abort_perror(img->fname); 857 | 858 | size_t rb = fread(s, ssize, 1, img->stream); 859 | if ((rb!=1) || ferror(img->stream)) 860 | abort_perror(img->fname); 861 | 862 | FILE* second_file = fopen(img->second_fname, "w"); 863 | if (!second_file) 864 | abort_perror(img->second_fname); 865 | 866 | fwrite(s, ssize, 1, second_file); 867 | if (ferror(second_file)) 868 | abort_perror(img->second_fname); 869 | 870 | fclose(second_file); 871 | free(s); 872 | } 873 | 874 | 875 | 876 | t_abootimg* new_bootimg() 877 | { 878 | t_abootimg* img; 879 | 880 | img = calloc(sizeof(t_abootimg), 1); 881 | if (!img) 882 | abort_perror(NULL); 883 | 884 | img->config_fname = "bootimg.cfg"; 885 | img->kernel_fname = "zImage"; 886 | img->ramdisk_fname = "initrd.img"; 887 | img->second_fname = "stage2.img"; 888 | 889 | memcpy(img->header.magic, BOOT_MAGIC, BOOT_MAGIC_SIZE); 890 | img->header.page_size = 2048; // a sensible default page size 891 | 892 | return img; 893 | } 894 | 895 | 896 | int main(int argc, char** argv) 897 | { 898 | t_abootimg* bootimg = new_bootimg(); 899 | 900 | switch(parse_args(argc, argv, bootimg)) 901 | { 902 | case none: 903 | printf("error - bad arguments\n\n"); 904 | print_usage(); 905 | break; 906 | 907 | case help: 908 | print_usage(); 909 | break; 910 | 911 | case info: 912 | open_bootimg(bootimg, "r"); 913 | read_header(bootimg); 914 | print_bootimg_info(bootimg); 915 | break; 916 | 917 | case extract: 918 | open_bootimg(bootimg, "r"); 919 | read_header(bootimg); 920 | write_bootimg_config(bootimg); 921 | extract_kernel(bootimg); 922 | extract_ramdisk(bootimg); 923 | extract_second(bootimg); 924 | break; 925 | 926 | case update: 927 | open_bootimg(bootimg, "r+"); 928 | read_header(bootimg); 929 | update_header(bootimg); 930 | update_images(bootimg); 931 | write_bootimg(bootimg); 932 | break; 933 | 934 | case create: 935 | if (!bootimg->kernel_fname || !bootimg->ramdisk_fname) { 936 | print_usage(); 937 | break; 938 | } 939 | check_if_block_device(bootimg); 940 | open_bootimg(bootimg, "w"); 941 | update_header(bootimg); 942 | update_images(bootimg); 943 | if (check_boot_img_header(bootimg)) 944 | abort_printf("%s: Sanity cheks failed", bootimg->fname); 945 | write_bootimg(bootimg); 946 | break; 947 | } 948 | 949 | return 0; 950 | } 951 | 952 | 953 | -------------------------------------------------------------------------------- /bin/src/abootimg/bootimg.h: -------------------------------------------------------------------------------- 1 | /* tools/mkbootimg/bootimg.h 2 | ** 3 | ** Copyright 2007, The Android Open Source Project 4 | ** 5 | ** Licensed under the Apache License, Version 2.0 (the "License"); 6 | ** you may not use this file except in compliance with the License. 7 | ** You may obtain a copy of the License at 8 | ** 9 | ** http://www.apache.org/licenses/LICENSE-2.0 10 | ** 11 | ** Unless required by applicable law or agreed to in writing, software 12 | ** distributed under the License is distributed on an "AS IS" BASIS, 13 | ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | ** See the License for the specific language governing permissions and 15 | ** limitations under the License. 16 | */ 17 | 18 | #ifndef _BOOT_IMAGE_H_ 19 | #define _BOOT_IMAGE_H_ 20 | 21 | typedef struct boot_img_hdr boot_img_hdr; 22 | 23 | #define BOOT_MAGIC "ANDROID!" 24 | #define BOOT_MAGIC_SIZE 8 25 | #define BOOT_NAME_SIZE 16 26 | #define BOOT_ARGS_SIZE 512 27 | 28 | struct boot_img_hdr 29 | { 30 | unsigned char magic[BOOT_MAGIC_SIZE]; 31 | 32 | unsigned kernel_size; /* size in bytes */ 33 | unsigned kernel_addr; /* physical load addr */ 34 | 35 | unsigned ramdisk_size; /* size in bytes */ 36 | unsigned ramdisk_addr; /* physical load addr */ 37 | 38 | unsigned second_size; /* size in bytes */ 39 | unsigned second_addr; /* physical load addr */ 40 | 41 | unsigned tags_addr; /* physical addr for kernel tags */ 42 | unsigned page_size; /* flash page size we assume */ 43 | unsigned unused[2]; /* future expansion: should be 0 */ 44 | 45 | unsigned char name[BOOT_NAME_SIZE]; /* asciiz product name */ 46 | 47 | unsigned char cmdline[BOOT_ARGS_SIZE]; 48 | 49 | unsigned id[8]; /* timestamp / checksum / sha1 / etc */ 50 | }; 51 | 52 | /* 53 | ** +-----------------+ 54 | ** | boot header | 1 page 55 | ** +-----------------+ 56 | ** | kernel | n pages 57 | ** +-----------------+ 58 | ** | ramdisk | m pages 59 | ** +-----------------+ 60 | ** | second stage | o pages 61 | ** +-----------------+ 62 | ** 63 | ** n = (kernel_size + page_size - 1) / page_size 64 | ** m = (ramdisk_size + page_size - 1) / page_size 65 | ** o = (second_size + page_size - 1) / page_size 66 | ** 67 | ** 0. all entities are page_size aligned in flash 68 | ** 1. kernel and ramdisk are required (size != 0) 69 | ** 2. second is optional (second_size == 0 -> no second) 70 | ** 3. load each element (kernel, ramdisk, second) at 71 | ** the specified physical address (kernel_addr, etc) 72 | ** 4. prepare tags at tag_addr. kernel_args[] is 73 | ** appended to the kernel commandline in the tags. 74 | ** 5. r0 = 0, r1 = MACHINE_TYPE, r2 = tags_addr 75 | ** 6. if second_size != 0: jump to second_addr 76 | ** else: jump to kernel_addr 77 | */ 78 | 79 | #if 0 80 | typedef struct ptentry ptentry; 81 | 82 | struct ptentry { 83 | char name[16]; /* asciiz partition name */ 84 | unsigned start; /* starting block number */ 85 | unsigned length; /* length in blocks */ 86 | unsigned flags; /* set to zero */ 87 | }; 88 | 89 | /* MSM Partition Table ATAG 90 | ** 91 | ** length: 2 + 7 * n 92 | ** atag: 0x4d534d70 93 | ** x n 94 | */ 95 | #endif 96 | 97 | #endif 98 | -------------------------------------------------------------------------------- /bin/src/abootimg/debian/abootimg.1: -------------------------------------------------------------------------------- 1 | .TH ABOOTIMG 1 2 | .SH NAME 3 | abootimg \- manipulate Android Boot Images. 4 | .SH DESCRIPTION 5 | Tool to read/write/update android boot images 6 | 7 | .SH SYNOPSIS 8 | .B abootimg 9 | \-i 10 | .br 11 | .B abootimg 12 | \-x [ [ [ []]]] 13 | .br 14 | .B abootimg 15 | \-u [\-c "param=value"] [\-f ] [\-k ] [\-r ] [\-s ] 16 | .br 17 | .B abootimg 18 | \-\-create [\-c "param=value"] [\-f ] \-k \-r [\-s ] 19 | 20 | .SH OPTIONS 21 | .TP 22 | .B \-i 23 | print boot image information 24 | .TP 25 | .B \-x 26 | Extract a boot image 27 | .TP 28 | .B \-u 29 | Update a boot image 30 | .TP 31 | .B \-\-create 32 | Create a boot image 33 | 34 | .SS "Options for extracting boot images" 35 | .TP 36 | .B bootimg 37 | Existing boot image to use 38 | .TP 39 | .B bootimg.cfg 40 | Name for the bootimg.cfg file, defaults to bootimg.cfg 41 | .TP 42 | .B kernel 43 | Name for the kernel image, defaults to zImage 44 | .TP 45 | .B ramdisk 46 | Name for the ramdisk image, defaults to initrd.img 47 | .TP 48 | .B secondstage 49 | Name for the second-stage image, defaults to stage2.img 50 | 51 | .SS "Options for updating and creating boot images" 52 | .TP 53 | .B bootimg 54 | Existing boot image to use 55 | .TP 56 | .B \-c "param=value" 57 | Existing boot image to use 58 | .TP 59 | .B \-f 60 | Update bootimg.cfg with the named file 61 | .TP 62 | .B \-k 63 | Update kernel with the named file 64 | .TP 65 | .B \-r 66 | Update ramdisk with the named file 67 | .TP 68 | .B \-s 69 | Update secondstage image with the named file 70 | -------------------------------------------------------------------------------- /bin/src/abootimg/debian/changelog: -------------------------------------------------------------------------------- 1 | abootimg (0.6-1) unstable; urgency=low 2 | 3 | * New upstream release 4 | 5 | -- Heiko Stuebner Thu, 14 Jul 2011 20:54:33 +0200 6 | 7 | abootimg (0.5+git20110704-1) unstable; urgency=low 8 | 9 | * New upstream snapshot 10 | * Fix build on GNU-Hurd 11 | * Bump standards to 3.9.2 12 | 13 | -- Heiko Stuebner Mon, 04 Jul 2011 18:34:17 +0200 14 | 15 | abootimg (0.5+git20110609-1) unstable; urgency=low 16 | 17 | * New upstream release (Closes: #631719) 18 | * Relax packaging license (Closes: #631720) 19 | * Install helper scripts for initramfs handling 20 | 21 | -- Heiko Stuebner Sat, 02 Jul 2011 12:46:35 +0200 22 | 23 | abootimg (0.3-1) unstable; urgency=low 24 | 25 | * Initial release (Closes: #612096) 26 | 27 | -- Heiko Stuebner Sat, 12 Feb 2011 12:04:10 +0100 28 | -------------------------------------------------------------------------------- /bin/src/abootimg/debian/compat: -------------------------------------------------------------------------------- 1 | 7 2 | -------------------------------------------------------------------------------- /bin/src/abootimg/debian/control: -------------------------------------------------------------------------------- 1 | Source: abootimg 2 | Section: admin 3 | Priority: extra 4 | Maintainer: Heiko Stuebner 5 | Build-Depends: debhelper (>= 7), cdbs (>= 0.4.49), libblkid-dev 6 | Standards-Version: 3.9.2 7 | Homepage: http://gitorious.org/ac100/abootimg 8 | 9 | Package: abootimg 10 | Architecture: any 11 | Depends: ${shlibs:Depends}, ${misc:Depends} 12 | Description: Tool to read/write/update android boot images 13 | Android devices use a special partition format to boot any 14 | operating system on the devices. These boot-images contain 15 | a kernel image, a ramdisk, optionally a 2nd stage boot loader 16 | and the commandline passed to the kernel when booting. 17 | The original mkbootimg from Android can only create these images 18 | where abootimg can also extract and modify them. 19 | Handling android boot images is necessary when bringing other 20 | operating systems to android devices. -------------------------------------------------------------------------------- /bin/src/abootimg/debian/copyright: -------------------------------------------------------------------------------- 1 | This package was debianized by Heiko Stuebner on 2 | Sat, 12 Feb 2011 10:44:12 +0100. 3 | 4 | It was downloaded from http://gitorious.org/ac100/abootimg 5 | 6 | Upstream Author: 7 | 8 | Gilles Grandou 9 | 10 | Copyright: 11 | 12 | Copyright (c) 2010 Gilles Grandou 13 | 14 | License: 15 | 16 | This program is free software; you can redistribute it and/or 17 | modify it under the terms of the GNU General Public License 18 | as published by the Free Software Foundation; either version 2 19 | of the License, or (at your option) any later version. 20 | 21 | This program is distributed in the hope that it will be useful, 22 | but WITHOUT ANY WARRANTY; without even the implied warranty of 23 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 24 | GNU General Public License for more details. 25 | 26 | You should have received a copy of the GNU General Public License 27 | along with this program; if not, write to the Free Software 28 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. 29 | 30 | On Debian systems, the complete text of the GNU Library 31 | General Public License can be found in `/usr/share/common-licenses/GPL-2'. 32 | 33 | except bootimg.h 34 | 35 | Copyright 2007, The Android Open Source Project 36 | 37 | Licensed under the Apache License, Version 2.0 (the "License"); 38 | you may not use this file except in compliance with the License. 39 | You may obtain a copy of the License at 40 | 41 | On Debian systems, the complete text of the Apache License Version 2.0 42 | can be found in `/usr/share/common-licenses/Apache-2.0'. 43 | 44 | 45 | The Debian packaging is copyright 2011, Heiko Stuebner and 46 | is licensed under the GPL version 2, or (at your option) any later version, 47 | see `/usr/share/common-licenses/GPL-2'. 48 | -------------------------------------------------------------------------------- /bin/src/abootimg/debian/dirs: -------------------------------------------------------------------------------- 1 | usr/bin 2 | usr/share/doc/abootimg 3 | -------------------------------------------------------------------------------- /bin/src/abootimg/debian/docs: -------------------------------------------------------------------------------- 1 | README 2 | -------------------------------------------------------------------------------- /bin/src/abootimg/debian/gbp.conf: -------------------------------------------------------------------------------- 1 | # Configuration file for git-buildpackage and friends 2 | 3 | [DEFAULT] 4 | # the default build command: 5 | #builder = debuild -i -I 6 | # the default clean command: 7 | #cleaner = debuild clean 8 | # the default branch for upstream sources: 9 | #upstream-branch = upstream 10 | # the default branch for the debian patch: 11 | #debian-branch = master 12 | # the default tag formats used: 13 | #upstream-tag = upstream/%(version)s 14 | #debian-tag = debian/%(version)s 15 | # use pristine-tar: 16 | pristine-tar = True 17 | -------------------------------------------------------------------------------- /bin/src/abootimg/debian/rules: -------------------------------------------------------------------------------- 1 | #!/usr/bin/make -f 2 | 3 | include /usr/share/cdbs/1/rules/debhelper.mk 4 | include /usr/share/cdbs/1/class/makefile.mk 5 | 6 | DEBVERS := $(shell dpkg-parsechangelog | sed -n -e 's/^Version: //p') 7 | VERSION := $(shell echo '$(DEBVERS)' | sed -e 's/^[[:digit:]]*://' -e 's/[~-].*//') 8 | 9 | configure/abootimg:: 10 | echo '#define VERSION_STR "$(VERSION)"' > version.h 11 | 12 | binary-install/abootimg:: 13 | dh_installman $(CURDIR)/debian/abootimg.1 14 | install $(CURDIR)/abootimg $(CURDIR)/debian/abootimg/usr/bin 15 | install -T $(CURDIR)/abootimg-pack-initrd $(CURDIR)/debian/abootimg/usr/bin 16 | install -T $(CURDIR)/abootimg-unpack-initrd $(CURDIR)/debian/abootimg/usr/bin 17 | 18 | clean:: 19 | rm -rf files/agtl.egg-info 20 | rm -rf tmp 21 | -------------------------------------------------------------------------------- /bin/src/aml_image_extractor.c: -------------------------------------------------------------------------------- 1 | //From https://www.cnx-software.com/2016/11/19/how-to-create-a-bootable-recovery-sd-card-for-amlogic-tv-boxes/ 2 | 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | 10 | uint32_t convert(uint8_t *test, uint64_t loc) { 11 | return ntohl((test[loc] << 24) | (test[loc+1] << 16) | (test[loc+2] << 8) | test[loc+3]); 12 | } 13 | 14 | void main (int argc, char **argv) { 15 | FILE *fileptr; 16 | uint8_t *buffer; 17 | long filelen; 18 | 19 | FILE *f; 20 | char *filename; 21 | uint64_t record; 22 | uint64_t record_loc; 23 | uint64_t file_loc; 24 | uint64_t file_size; 25 | 26 | if (argc <= 1) { 27 | printf("Usage: %s [firmware-file-name]\n", argv[0]); 28 | exit (0); 29 | } 30 | 31 | fileptr = fopen(argv[1], "rb"); 32 | fseek(fileptr, 0, SEEK_END); 33 | filelen = ftell(fileptr); 34 | rewind(fileptr); 35 | 36 | buffer = (uint8_t *)malloc((filelen+1)*sizeof(uint8_t)); 37 | fread(buffer, filelen, 1, fileptr); 38 | fclose(fileptr); 39 | 40 | for (record = 0; record < (uint8_t)buffer[0x18]; record = record + 1){ 41 | record_loc = 0x40 + (record * 0x240); 42 | 43 | filename = (malloc(32)); 44 | sprintf(filename,"tmp/%s.%s",(char *)&buffer[record_loc+0x120], (char *)&buffer[record_loc+0x20]); 45 | 46 | printf(" Extracting %s\n", filename); 47 | 48 | file_loc = convert(buffer,record_loc+0x10); 49 | file_size = convert(buffer,record_loc+0x18); 50 | 51 | f = fopen(filename, "wb"); 52 | if (f == NULL) { 53 | printf("ERROR: could not open output\n"); 54 | printf("the error was: %s\n",strerror(errno)); 55 | free(filename); 56 | continue; 57 | } 58 | fwrite(&(buffer[file_loc]), sizeof(uint8_t), (size_t)file_size, f); 59 | fclose(f); 60 | free(filename); 61 | } 62 | free(buffer); 63 | } 64 | -------------------------------------------------------------------------------- /bin/src/simg2img/.gitignore: -------------------------------------------------------------------------------- 1 | *.a 2 | *.o 3 | .depend 4 | simg2img 5 | simg2simg 6 | img2simg 7 | append2simg 8 | -------------------------------------------------------------------------------- /bin/src/simg2img/LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (C) 2010 The Android Open Source Project 2 | Modifications copyright (C) 2014 Anestis Bechtsoudis 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | 10 | Unless required by applicable law or agreed to in writing, software 11 | distributed under the License is distributed on an "AS IS" BASIS, 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | See the License for the specific language governing permissions and 14 | limitations under the License. 15 | -------------------------------------------------------------------------------- /bin/src/simg2img/Makefile: -------------------------------------------------------------------------------- 1 | # 2 | # Copyright (C) 2014 Anestis Bechtsoudis 3 | # 4 | # Licensed under the Apache License, Version 2.0 (the "License"); 5 | # you may not use this file except in compliance with the License. 6 | # You may obtain a copy of the License at 7 | # 8 | # http://www.apache.org/licenses/LICENSE-2.0 9 | # 10 | # Unless required by applicable law or agreed to in writing, software 11 | # distributed under the License is distributed on an "AS IS" BASIS, 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | # See the License for the specific language governing permissions and 14 | # limitations under the License. 15 | # 16 | PREFIX ?= /usr/local 17 | 18 | CC ?= gcc 19 | LD ?= gcc 20 | DEP_CC ?= gcc 21 | AR ?= ar 22 | RANLIB ?= ranlib 23 | STRIP ?= strip 24 | CFLAGS += -O2 -Wall -D_FILE_OFFSET_BITS=64 -D_LARGEFILE64_SOURCE=1 25 | 26 | # libsparse 27 | LIB_NAME = sparse 28 | SLIB = lib$(LIB_NAME).a 29 | LIB_SRCS = \ 30 | backed_block.c \ 31 | output_file.c \ 32 | sparse.c \ 33 | sparse_crc32.c \ 34 | sparse_err.c \ 35 | sparse_read.c 36 | LIB_OBJS = $(LIB_SRCS:%.c=%.o) 37 | LIB_INCS = -Iinclude 38 | 39 | LDFLAGS += -L. -l$(LIB_NAME) -lm -lz 40 | 41 | BINS = simg2img simg2simg img2simg append2simg 42 | HEADERS = include/sparse/sparse.h 43 | 44 | # simg2img 45 | SIMG2IMG_SRCS = simg2img.c 46 | SIMG2IMG_OBJS = $(SIMG2IMG_SRCS:%.c=%.o) 47 | 48 | # simg2simg 49 | SIMG2SIMG_SRCS = simg2simg.c 50 | SIMG2SIMG_OBJS = $(SIMG2SIMG_SRCS:%.c=%.o) 51 | 52 | # img2simg 53 | IMG2SIMG_SRCS = $(LIBSPARSE_SRCS) img2simg.c 54 | IMG2SIMG_OBJS = $(IMG2SIMG_SRCS:%.c=%.o) 55 | 56 | # append2simg 57 | APPEND2SIMG_SRCS = $(LIBSPARSE_SRCS) append2simg.c 58 | APPEND2SIMG_OBJS = $(APPEND2SIMG_SRCS:%.c=%.o) 59 | 60 | SRCS = \ 61 | $(SIMG2IMG_SRCS) \ 62 | $(SIMG2SIMG_SRCS) \ 63 | $(IMG2SIMG_SRCS) \ 64 | $(APPEND2SIMG_SRCS) \ 65 | $(LIB_SRCS) 66 | 67 | .PHONY: default all clean install 68 | 69 | default: all 70 | all: $(LIB_NAME) simg2img simg2simg img2simg append2simg 71 | 72 | install: all 73 | install -d $(PREFIX)/bin $(PREFIX)/lib $(PREFIX)/include/sparse 74 | install -m 0755 $(BINS) $(PREFIX)/bin 75 | install -m 0755 $(SLIB) $(PREFIX)/lib 76 | install -m 0644 $(HEADERS) $(PREFIX)/include/sparse 77 | 78 | $(LIB_NAME): $(LIB_OBJS) 79 | $(AR) rc $(SLIB) $(LIB_OBJS) 80 | $(RANLIB) $(SLIB) 81 | 82 | simg2img: $(SIMG2IMG_SRCS) $(LIB_NAME) 83 | $(CC) $(CFLAGS) $(LIB_INCS) -o simg2img $< $(LDFLAGS) 84 | 85 | simg2simg: $(SIMG2SIMG_SRCS) $(LIB_NAME) 86 | $(CC) $(CFLAGS) $(LIB_INCS) -o simg2simg $< $(LDFLAGS) 87 | 88 | img2simg: $(IMG2SIMG_SRCS) $(LIB_NAME) 89 | $(CC) $(CFLAGS) $(LIB_INCS) -o img2simg $< $(LDFLAGS) 90 | 91 | append2simg: $(APPEND2SIMG_SRCS) $(LIB_NAME) 92 | $(CC) $(CFLAGS) $(LIB_INCS) -o append2simg $< $(LDFLAGS) 93 | 94 | %.o: %.c .depend 95 | $(CC) -c $(CFLAGS) $(LIB_INCS) $< -o $@ 96 | 97 | clean: 98 | $(RM) -f *.o *.a simg2img simg2simg img2simg append2simg .depend 99 | 100 | ifneq ($(wildcard .depend),) 101 | include .depend 102 | endif 103 | 104 | .depend: 105 | @$(RM) .depend 106 | @$(foreach SRC, $(SRCS), $(DEP_CC) $(LIB_INCS) $(SRC) $(CFLAGS) -MT $(SRC:%.c=%.o) -MM >> .depend;) 107 | 108 | indent: 109 | indent -linux -l100 -lc100 -nut -i4 *.c *.h; rm -f *~ 110 | -------------------------------------------------------------------------------- /bin/src/simg2img/README.md: -------------------------------------------------------------------------------- 1 | simg2img 2 | ========= 3 | 4 | Tool to convert Android sparse images to raw images. 5 | 6 | Since image tools are not part of Android SDK, this standalone port of AOSP libsparse aims to avoid complex building chains. 7 | 8 | Usage 9 | ----- 10 | 11 | ``` 12 | $ make 13 | $ simg2img /path/to/Android/images/system.img /output/path/system.raw.img 14 | $ file /path/to/Android/images/system.img 15 | system.img: Android sparse image, version: 1.0, Total of 262144 4096-byte output blocks in 1620 input chunks. 16 | $ file /output/path/system.raw.img 17 | system.raw.img: Linux rev 1.0 ext4 filesystem data, UUID=57f8f4bc-abf4-655f-bf67-946fc0f9f25b (extents) (large files) 18 | ``` 19 | 20 | Windows 21 | ------- 22 | 23 | If you want to build simg2img on Windows you'll need to [install MinGW](http://www.mingw.org/wiki/howto_install_the_mingw_gcc_compiler_suite) 24 | and also zlib and libasprintf (go to MinGW Libraries in the installer and check `mingw32-libz` and `mingw32-libasprintf`). 25 | Once you've done that run the following command to build simg2img: 26 | 27 | ``` 28 | CFLAGS=-DUSE_MINGW LDFLAGS=-lasprintf mingw32-make 29 | ``` 30 | -------------------------------------------------------------------------------- /bin/src/simg2img/append2simg.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2013 The Android Open Source Project 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | #define _FILE_OFFSET_BITS 64 18 | #define _LARGEFILE64_SOURCE 1 19 | #define _GNU_SOURCE 20 | 21 | #include 22 | #include 23 | #include 24 | #include 25 | #include 26 | #include 27 | 28 | #include 29 | #include "sparse_file.h" 30 | #include "backed_block.h" 31 | 32 | #ifndef O_BINARY 33 | #define O_BINARY 0 34 | #endif 35 | 36 | #if defined(__APPLE__) && defined(__MACH__) 37 | #define lseek64 lseek 38 | #endif 39 | #if defined(__APPLE__) && defined(__MACH__) 40 | #define lseek64 lseek 41 | #define off64_t off_t 42 | #endif 43 | 44 | void usage() 45 | { 46 | fprintf(stderr, "Usage: append2simg \n"); 47 | } 48 | 49 | int main(int argc, char *argv[]) 50 | { 51 | int output; 52 | int output_block; 53 | char *output_path; 54 | struct sparse_file *sparse_output; 55 | 56 | int input; 57 | char *input_path; 58 | off64_t input_len; 59 | 60 | int tmp_fd; 61 | char *tmp_path; 62 | 63 | int ret; 64 | 65 | if (argc == 3) { 66 | output_path = argv[1]; 67 | input_path = argv[2]; 68 | } else { 69 | usage(); 70 | exit(-1); 71 | } 72 | 73 | ret = asprintf(&tmp_path, "%s.append2simg", output_path); 74 | if (ret < 0) { 75 | fprintf(stderr, "Couldn't allocate filename\n"); 76 | exit(-1); 77 | } 78 | 79 | output = open(output_path, O_RDWR | O_BINARY); 80 | if (output < 0) { 81 | fprintf(stderr, "Couldn't open output file (%s)\n", strerror(errno)); 82 | exit(-1); 83 | } 84 | 85 | sparse_output = sparse_file_import_auto(output, false, true); 86 | if (!sparse_output) { 87 | fprintf(stderr, "Couldn't import output file\n"); 88 | exit(-1); 89 | } 90 | 91 | input = open(input_path, O_RDONLY | O_BINARY); 92 | if (input < 0) { 93 | fprintf(stderr, "Couldn't open input file (%s)\n", strerror(errno)); 94 | exit(-1); 95 | } 96 | 97 | input_len = lseek64(input, 0, SEEK_END); 98 | if (input_len < 0) { 99 | fprintf(stderr, "Couldn't get input file length (%s)\n", strerror(errno)); 100 | exit(-1); 101 | } else if (input_len % sparse_output->block_size) { 102 | fprintf(stderr, "Input file is not a multiple of the output file's block size"); 103 | exit(-1); 104 | } 105 | lseek64(input, 0, SEEK_SET); 106 | 107 | output_block = sparse_output->len / sparse_output->block_size; 108 | if (sparse_file_add_fd(sparse_output, input, 0, input_len, output_block) < 0) { 109 | fprintf(stderr, "Couldn't add input file\n"); 110 | exit(-1); 111 | } 112 | sparse_output->len += input_len; 113 | 114 | tmp_fd = open(tmp_path, O_WRONLY | O_CREAT | O_BINARY, 0664); 115 | if (tmp_fd < 0) { 116 | fprintf(stderr, "Couldn't open temporary file (%s)\n", strerror(errno)); 117 | exit(-1); 118 | } 119 | 120 | lseek64(output, 0, SEEK_SET); 121 | if (sparse_file_write(sparse_output, tmp_fd, false, true, false) < 0) { 122 | fprintf(stderr, "Failed to write sparse file\n"); 123 | exit(-1); 124 | } 125 | 126 | sparse_file_destroy(sparse_output); 127 | close(tmp_fd); 128 | close(output); 129 | close(input); 130 | 131 | ret = rename(tmp_path, output_path); 132 | if (ret < 0) { 133 | fprintf(stderr, "Failed to rename temporary file (%s)\n", strerror(errno)); 134 | exit(-1); 135 | } 136 | 137 | free(tmp_path); 138 | 139 | exit(0); 140 | } 141 | -------------------------------------------------------------------------------- /bin/src/simg2img/backed_block.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2010 The Android Open Source Project 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | #include 18 | #include 19 | #include 20 | #include 21 | #include 22 | 23 | #include "backed_block.h" 24 | #include "sparse_defs.h" 25 | 26 | struct backed_block { 27 | unsigned int block; 28 | unsigned int len; 29 | enum backed_block_type type; 30 | union { 31 | struct { 32 | void *data; 33 | } data; 34 | struct { 35 | char *filename; 36 | int64_t offset; 37 | } file; 38 | struct { 39 | int fd; 40 | int64_t offset; 41 | } fd; 42 | struct { 43 | uint32_t val; 44 | } fill; 45 | }; 46 | struct backed_block *next; 47 | }; 48 | 49 | struct backed_block_list { 50 | struct backed_block *data_blocks; 51 | struct backed_block *last_used; 52 | unsigned int block_size; 53 | }; 54 | 55 | struct backed_block *backed_block_iter_new(struct backed_block_list *bbl) 56 | { 57 | return bbl->data_blocks; 58 | } 59 | 60 | struct backed_block *backed_block_iter_next(struct backed_block *bb) 61 | { 62 | return bb->next; 63 | } 64 | 65 | unsigned int backed_block_len(struct backed_block *bb) 66 | { 67 | return bb->len; 68 | } 69 | 70 | unsigned int backed_block_block(struct backed_block *bb) 71 | { 72 | return bb->block; 73 | } 74 | 75 | void *backed_block_data(struct backed_block *bb) 76 | { 77 | assert(bb->type == BACKED_BLOCK_DATA); 78 | return bb->data.data; 79 | } 80 | 81 | const char *backed_block_filename(struct backed_block *bb) 82 | { 83 | assert(bb->type == BACKED_BLOCK_FILE); 84 | return bb->file.filename; 85 | } 86 | 87 | int backed_block_fd(struct backed_block *bb) 88 | { 89 | assert(bb->type == BACKED_BLOCK_FD); 90 | return bb->fd.fd; 91 | } 92 | 93 | int64_t backed_block_file_offset(struct backed_block * bb) 94 | { 95 | assert(bb->type == BACKED_BLOCK_FILE || bb->type == BACKED_BLOCK_FD); 96 | if (bb->type == BACKED_BLOCK_FILE) { 97 | return bb->file.offset; 98 | } else { /* bb->type == BACKED_BLOCK_FD */ 99 | return bb->fd.offset; 100 | } 101 | } 102 | 103 | uint32_t backed_block_fill_val(struct backed_block * bb) 104 | { 105 | assert(bb->type == BACKED_BLOCK_FILL); 106 | return bb->fill.val; 107 | } 108 | 109 | enum backed_block_type backed_block_type(struct backed_block *bb) 110 | { 111 | return bb->type; 112 | } 113 | 114 | void backed_block_destroy(struct backed_block *bb) 115 | { 116 | if (bb->type == BACKED_BLOCK_FILE) { 117 | free(bb->file.filename); 118 | } 119 | 120 | free(bb); 121 | } 122 | 123 | struct backed_block_list *backed_block_list_new(unsigned int block_size) 124 | { 125 | struct backed_block_list *b = calloc(sizeof(struct backed_block_list), 1); 126 | b->block_size = block_size; 127 | return b; 128 | } 129 | 130 | void backed_block_list_destroy(struct backed_block_list *bbl) 131 | { 132 | if (bbl->data_blocks) { 133 | struct backed_block *bb = bbl->data_blocks; 134 | while (bb) { 135 | struct backed_block *next = bb->next; 136 | backed_block_destroy(bb); 137 | bb = next; 138 | } 139 | } 140 | 141 | free(bbl); 142 | } 143 | 144 | void backed_block_list_move(struct backed_block_list *from, 145 | struct backed_block_list *to, struct backed_block *start, 146 | struct backed_block *end) 147 | { 148 | struct backed_block *bb; 149 | 150 | if (start == NULL) { 151 | start = from->data_blocks; 152 | } 153 | 154 | if (!end) { 155 | for (end = start; end && end->next; end = end->next) ; 156 | } 157 | 158 | if (start == NULL || end == NULL) { 159 | return; 160 | } 161 | 162 | from->last_used = NULL; 163 | to->last_used = NULL; 164 | if (from->data_blocks == start) { 165 | from->data_blocks = end->next; 166 | } else { 167 | for (bb = from->data_blocks; bb; bb = bb->next) { 168 | if (bb->next == start) { 169 | bb->next = end->next; 170 | break; 171 | } 172 | } 173 | } 174 | 175 | if (!to->data_blocks) { 176 | to->data_blocks = start; 177 | end->next = NULL; 178 | } else { 179 | for (bb = to->data_blocks; bb; bb = bb->next) { 180 | if (!bb->next || bb->next->block > start->block) { 181 | end->next = bb->next; 182 | bb->next = start; 183 | break; 184 | } 185 | } 186 | } 187 | } 188 | 189 | /* may free b */ 190 | static int merge_bb(struct backed_block_list *bbl, struct backed_block *a, struct backed_block *b) 191 | { 192 | unsigned int block_len; 193 | 194 | /* Block doesn't exist (possible if one block is the last block) */ 195 | if (!a || !b) { 196 | return -EINVAL; 197 | } 198 | 199 | assert(a->block < b->block); 200 | 201 | /* Blocks are of different types */ 202 | if (a->type != b->type) { 203 | return -EINVAL; 204 | } 205 | 206 | /* Blocks are not adjacent */ 207 | block_len = a->len / bbl->block_size; /* rounds down */ 208 | if (a->block + block_len != b->block) { 209 | return -EINVAL; 210 | } 211 | 212 | switch (a->type) { 213 | case BACKED_BLOCK_DATA: 214 | /* Don't support merging data for now */ 215 | return -EINVAL; 216 | case BACKED_BLOCK_FILL: 217 | if (a->fill.val != b->fill.val) { 218 | return -EINVAL; 219 | } 220 | break; 221 | case BACKED_BLOCK_FILE: 222 | /* Already make sure b->type is BACKED_BLOCK_FILE */ 223 | if (strcmp(a->file.filename, b->file.filename) || a->file.offset + a->len != b->file.offset) { 224 | return -EINVAL; 225 | } 226 | break; 227 | case BACKED_BLOCK_FD: 228 | if (a->fd.fd != b->fd.fd || a->fd.offset + a->len != b->fd.offset) { 229 | return -EINVAL; 230 | } 231 | break; 232 | } 233 | 234 | /* Blocks are compatible and adjacent, with a before b. Merge b into a, 235 | * and free b */ 236 | a->len += b->len; 237 | a->next = b->next; 238 | 239 | backed_block_destroy(b); 240 | 241 | return 0; 242 | } 243 | 244 | static int queue_bb(struct backed_block_list *bbl, struct backed_block *new_bb) 245 | { 246 | struct backed_block *bb; 247 | 248 | if (bbl->data_blocks == NULL) { 249 | bbl->data_blocks = new_bb; 250 | return 0; 251 | } 252 | 253 | if (bbl->data_blocks->block > new_bb->block) { 254 | new_bb->next = bbl->data_blocks; 255 | bbl->data_blocks = new_bb; 256 | return 0; 257 | } 258 | 259 | /* Optimization: blocks are mostly queued in sequence, so save the 260 | pointer to the last bb that was added, and start searching from 261 | there if the next block number is higher */ 262 | if (bbl->last_used && new_bb->block > bbl->last_used->block) 263 | bb = bbl->last_used; 264 | else 265 | bb = bbl->data_blocks; 266 | bbl->last_used = new_bb; 267 | 268 | for (; bb->next && bb->next->block < new_bb->block; bb = bb->next) ; 269 | 270 | if (bb->next == NULL) { 271 | bb->next = new_bb; 272 | } else { 273 | new_bb->next = bb->next; 274 | bb->next = new_bb; 275 | } 276 | 277 | merge_bb(bbl, new_bb, new_bb->next); 278 | if (!merge_bb(bbl, bb, new_bb)) { 279 | /* new_bb destroyed, point to retained as last_used */ 280 | bbl->last_used = bb; 281 | } 282 | 283 | return 0; 284 | } 285 | 286 | /* Queues a fill block of memory to be written to the specified data blocks */ 287 | int backed_block_add_fill(struct backed_block_list *bbl, unsigned int fill_val, 288 | unsigned int len, unsigned int block) 289 | { 290 | struct backed_block *bb = calloc(1, sizeof(struct backed_block)); 291 | if (bb == NULL) { 292 | return -ENOMEM; 293 | } 294 | 295 | bb->block = block; 296 | bb->len = len; 297 | bb->type = BACKED_BLOCK_FILL; 298 | bb->fill.val = fill_val; 299 | bb->next = NULL; 300 | 301 | return queue_bb(bbl, bb); 302 | } 303 | 304 | /* Queues a block of memory to be written to the specified data blocks */ 305 | int backed_block_add_data(struct backed_block_list *bbl, void *data, 306 | unsigned int len, unsigned int block) 307 | { 308 | struct backed_block *bb = calloc(1, sizeof(struct backed_block)); 309 | if (bb == NULL) { 310 | return -ENOMEM; 311 | } 312 | 313 | bb->block = block; 314 | bb->len = len; 315 | bb->type = BACKED_BLOCK_DATA; 316 | bb->data.data = data; 317 | bb->next = NULL; 318 | 319 | return queue_bb(bbl, bb); 320 | } 321 | 322 | /* Queues a chunk of a file on disk to be written to the specified data blocks */ 323 | int backed_block_add_file(struct backed_block_list *bbl, const char *filename, 324 | int64_t offset, unsigned int len, unsigned int block) 325 | { 326 | struct backed_block *bb = calloc(1, sizeof(struct backed_block)); 327 | if (bb == NULL) { 328 | return -ENOMEM; 329 | } 330 | 331 | bb->block = block; 332 | bb->len = len; 333 | bb->type = BACKED_BLOCK_FILE; 334 | bb->file.filename = strdup(filename); 335 | bb->file.offset = offset; 336 | bb->next = NULL; 337 | 338 | return queue_bb(bbl, bb); 339 | } 340 | 341 | /* Queues a chunk of a fd to be written to the specified data blocks */ 342 | int backed_block_add_fd(struct backed_block_list *bbl, int fd, int64_t offset, 343 | unsigned int len, unsigned int block) 344 | { 345 | struct backed_block *bb = calloc(1, sizeof(struct backed_block)); 346 | if (bb == NULL) { 347 | return -ENOMEM; 348 | } 349 | 350 | bb->block = block; 351 | bb->len = len; 352 | bb->type = BACKED_BLOCK_FD; 353 | bb->fd.fd = fd; 354 | bb->fd.offset = offset; 355 | bb->next = NULL; 356 | 357 | return queue_bb(bbl, bb); 358 | } 359 | 360 | int backed_block_split(struct backed_block_list *bbl, struct backed_block *bb, unsigned int max_len) 361 | { 362 | struct backed_block *new_bb; 363 | 364 | max_len = ALIGN_DOWN(max_len, bbl->block_size); 365 | 366 | if (bb->len <= max_len) { 367 | return 0; 368 | } 369 | 370 | new_bb = malloc(sizeof(struct backed_block)); 371 | if (new_bb == NULL) { 372 | return -ENOMEM; 373 | } 374 | 375 | *new_bb = *bb; 376 | 377 | new_bb->len = bb->len - max_len; 378 | new_bb->block = bb->block + max_len / bbl->block_size; 379 | new_bb->next = bb->next; 380 | bb->next = new_bb; 381 | bb->len = max_len; 382 | 383 | switch (bb->type) { 384 | case BACKED_BLOCK_DATA: 385 | new_bb->data.data = (char *)bb->data.data + max_len; 386 | break; 387 | case BACKED_BLOCK_FILE: 388 | new_bb->file.offset += max_len; 389 | break; 390 | case BACKED_BLOCK_FD: 391 | new_bb->fd.offset += max_len; 392 | break; 393 | case BACKED_BLOCK_FILL: 394 | break; 395 | } 396 | 397 | return 0; 398 | } 399 | -------------------------------------------------------------------------------- /bin/src/simg2img/backed_block.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2010 The Android Open Source Project 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | #ifndef _BACKED_BLOCK_H_ 18 | #define _BACKED_BLOCK_H_ 19 | 20 | #include 21 | 22 | struct backed_block_list; 23 | struct backed_block; 24 | 25 | enum backed_block_type { 26 | BACKED_BLOCK_DATA, 27 | BACKED_BLOCK_FILE, 28 | BACKED_BLOCK_FD, 29 | BACKED_BLOCK_FILL, 30 | }; 31 | 32 | int backed_block_add_data(struct backed_block_list *bbl, void *data, 33 | unsigned int len, unsigned int block); 34 | int backed_block_add_fill(struct backed_block_list *bbl, unsigned int fill_val, 35 | unsigned int len, unsigned int block); 36 | int backed_block_add_file(struct backed_block_list *bbl, const char *filename, 37 | int64_t offset, unsigned int len, unsigned int block); 38 | int backed_block_add_fd(struct backed_block_list *bbl, int fd, 39 | int64_t offset, unsigned int len, unsigned int block); 40 | 41 | struct backed_block *backed_block_iter_new(struct backed_block_list *bbl); 42 | struct backed_block *backed_block_iter_next(struct backed_block *bb); 43 | unsigned int backed_block_len(struct backed_block *bb); 44 | unsigned int backed_block_block(struct backed_block *bb); 45 | void *backed_block_data(struct backed_block *bb); 46 | const char *backed_block_filename(struct backed_block *bb); 47 | int backed_block_fd(struct backed_block *bb); 48 | int64_t backed_block_file_offset(struct backed_block *bb); 49 | uint32_t backed_block_fill_val(struct backed_block *bb); 50 | enum backed_block_type backed_block_type(struct backed_block *bb); 51 | int backed_block_split(struct backed_block_list *bbl, struct backed_block *bb, 52 | unsigned int max_len); 53 | 54 | struct backed_block *backed_block_iter_new(struct backed_block_list *bbl); 55 | struct backed_block *backed_block_iter_next(struct backed_block *bb); 56 | 57 | struct backed_block_list *backed_block_list_new(unsigned int block_size); 58 | void backed_block_list_destroy(struct backed_block_list *bbl); 59 | 60 | void backed_block_list_move(struct backed_block_list *from, 61 | struct backed_block_list *to, struct backed_block *start, 62 | struct backed_block *end); 63 | 64 | #endif 65 | -------------------------------------------------------------------------------- /bin/src/simg2img/defs.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014 The Android Open Source Project 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | #ifndef _LIBSPARSE_DEFS_H_ 18 | 19 | #ifndef __unused 20 | #define __unused __attribute__((__unused__)) 21 | #endif 22 | 23 | #endif /* _LIBSPARSE_DEFS_H_ */ 24 | -------------------------------------------------------------------------------- /bin/src/simg2img/img2simg.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2012 The Android Open Source Project 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | #define _FILE_OFFSET_BITS 64 18 | #define _LARGEFILE64_SOURCE 1 19 | 20 | #include 21 | #include 22 | #include 23 | #include 24 | #include 25 | #include 26 | #include 27 | #include 28 | #include 29 | 30 | #include 31 | 32 | #ifndef O_BINARY 33 | #define O_BINARY 0 34 | #endif 35 | 36 | #if defined(__APPLE__) && defined(__MACH__) 37 | #define lseek64 lseek 38 | #define off64_t off_t 39 | #endif 40 | 41 | void usage() 42 | { 43 | fprintf(stderr, "Usage: img2simg []\n"); 44 | } 45 | 46 | int main(int argc, char *argv[]) 47 | { 48 | int in; 49 | int out; 50 | int ret; 51 | struct sparse_file *s; 52 | unsigned int block_size = 4096; 53 | off64_t len; 54 | 55 | if (argc < 3 || argc > 4) { 56 | usage(); 57 | exit(-1); 58 | } 59 | 60 | if (argc == 4) { 61 | block_size = atoi(argv[3]); 62 | } 63 | 64 | if (block_size < 1024 || block_size % 4 != 0) { 65 | usage(); 66 | exit(-1); 67 | } 68 | 69 | if (strcmp(argv[1], "-") == 0) { 70 | in = STDIN_FILENO; 71 | } else { 72 | in = open(argv[1], O_RDONLY | O_BINARY); 73 | if (in < 0) { 74 | fprintf(stderr, "Cannot open input file %s\n", argv[1]); 75 | exit(-1); 76 | } 77 | } 78 | 79 | if (strcmp(argv[2], "-") == 0) { 80 | out = STDOUT_FILENO; 81 | } else { 82 | out = open(argv[2], O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, 0664); 83 | if (out < 0) { 84 | fprintf(stderr, "Cannot open output file %s\n", argv[2]); 85 | exit(-1); 86 | } 87 | } 88 | 89 | len = lseek64(in, 0, SEEK_END); 90 | lseek64(in, 0, SEEK_SET); 91 | 92 | s = sparse_file_new(block_size, len); 93 | if (!s) { 94 | fprintf(stderr, "Failed to create sparse file\n"); 95 | exit(-1); 96 | } 97 | 98 | sparse_file_verbose(s); 99 | ret = sparse_file_read(s, in, false, false); 100 | if (ret) { 101 | fprintf(stderr, "Failed to read file\n"); 102 | exit(-1); 103 | } 104 | 105 | ret = sparse_file_write(s, out, false, true, false); 106 | if (ret) { 107 | fprintf(stderr, "Failed to write sparse file\n"); 108 | exit(-1); 109 | } 110 | 111 | close(in); 112 | close(out); 113 | 114 | exit(0); 115 | } 116 | -------------------------------------------------------------------------------- /bin/src/simg2img/include/sparse/sparse.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2012 The Android Open Source Project 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | #ifndef _LIBSPARSE_SPARSE_H_ 18 | #define _LIBSPARSE_SPARSE_H_ 19 | 20 | #include 21 | #include 22 | 23 | #ifdef __cplusplus 24 | extern "C" { 25 | #endif 26 | 27 | struct sparse_file; 28 | 29 | /** 30 | * sparse_file_new - create a new sparse file cookie 31 | * 32 | * @block_size - minimum size of a chunk 33 | * @len - size of the expanded sparse file. 34 | * 35 | * Creates a new sparse_file cookie that can be used to associate data 36 | * blocks. Can later be written to a file with a variety of options. 37 | * block_size specifies the minimum size of a chunk in the file. The maximum 38 | * size of the file is 2**32 * block_size (16TB for 4k block size). 39 | * 40 | * Returns the sparse file cookie, or NULL on error. 41 | */ 42 | struct sparse_file *sparse_file_new(unsigned int block_size, int64_t len); 43 | 44 | /** 45 | * sparse_file_destroy - destroy a sparse file cookie 46 | * 47 | * @s - sparse file cookie 48 | * 49 | * Destroys a sparse file cookie. After destroy, all memory passed in to 50 | * sparse_file_add_data can be freed by the caller 51 | */ 52 | void sparse_file_destroy(struct sparse_file *s); 53 | 54 | /** 55 | * sparse_file_add_data - associate a data chunk with a sparse file 56 | * 57 | * @s - sparse file cookie 58 | * @data - pointer to data block 59 | * @len - length of the data block 60 | * @block - offset in blocks into the sparse file to place the data chunk 61 | * 62 | * Associates a data chunk with a sparse file cookie. The region 63 | * [block * block_size : block * block_size + len) must not already be used in 64 | * the sparse file. If len is not a multiple of the block size the data 65 | * will be padded with zeros. 66 | * 67 | * The data pointer must remain valid until the sparse file is closed or the 68 | * data block is removed from the sparse file. 69 | * 70 | * Returns 0 on success, negative errno on error. 71 | */ 72 | int sparse_file_add_data(struct sparse_file *s, 73 | void *data, unsigned int len, unsigned int block); 74 | 75 | /** 76 | * sparse_file_add_fill - associate a fill chunk with a sparse file 77 | * 78 | * @s - sparse file cookie 79 | * @fill_val - 32 bit fill data 80 | * @len - length of the fill block 81 | * @block - offset in blocks into the sparse file to place the fill chunk 82 | * 83 | * Associates a chunk filled with fill_val with a sparse file cookie. 84 | * The region [block * block_size : block * block_size + len) must not already 85 | * be used in the sparse file. If len is not a multiple of the block size the 86 | * data will be padded with zeros. 87 | * 88 | * Returns 0 on success, negative errno on error. 89 | */ 90 | int sparse_file_add_fill(struct sparse_file *s, 91 | uint32_t fill_val, unsigned int len, unsigned int block); 92 | 93 | /** 94 | * sparse_file_add_file - associate a chunk of a file with a sparse file 95 | * 96 | * @s - sparse file cookie 97 | * @filename - filename of the file to be copied 98 | * @file_offset - offset into the copied file 99 | * @len - length of the copied block 100 | * @block - offset in blocks into the sparse file to place the file chunk 101 | * 102 | * Associates a chunk of an existing file with a sparse file cookie. 103 | * The region [block * block_size : block * block_size + len) must not already 104 | * be used in the sparse file. If len is not a multiple of the block size the 105 | * data will be padded with zeros. 106 | * 107 | * Allows adding large amounts of data to a sparse file without needing to keep 108 | * it all mapped. File size is limited by available virtual address space, 109 | * exceptionally large files may need to be added in multiple chunks. 110 | * 111 | * Returns 0 on success, negative errno on error. 112 | */ 113 | int sparse_file_add_file(struct sparse_file *s, 114 | const char *filename, int64_t file_offset, unsigned int len, 115 | unsigned int block); 116 | 117 | /** 118 | * sparse_file_add_file - associate a chunk of a file with a sparse file 119 | * 120 | * @s - sparse file cookie 121 | * @filename - filename of the file to be copied 122 | * @file_offset - offset into the copied file 123 | * @len - length of the copied block 124 | * @block - offset in blocks into the sparse file to place the file chunk 125 | * 126 | * Associates a chunk of an existing fd with a sparse file cookie. 127 | * The region [block * block_size : block * block_size + len) must not already 128 | * be used in the sparse file. If len is not a multiple of the block size the 129 | * data will be padded with zeros. 130 | * 131 | * Allows adding large amounts of data to a sparse file without needing to keep 132 | * it all mapped. File size is limited by available virtual address space, 133 | * exceptionally large files may need to be added in multiple chunks. 134 | * 135 | * The fd must remain open until the sparse file is closed or the fd block is 136 | * removed from the sparse file. 137 | * 138 | * Returns 0 on success, negative errno on error. 139 | */ 140 | int sparse_file_add_fd(struct sparse_file *s, 141 | int fd, int64_t file_offset, unsigned int len, unsigned int block); 142 | 143 | /** 144 | * sparse_file_write - write a sparse file to a file 145 | * 146 | * @s - sparse file cookie 147 | * @fd - file descriptor to write to 148 | * @gz - write a gzipped file 149 | * @sparse - write in the Android sparse file format 150 | * @crc - append a crc chunk 151 | * 152 | * Writes a sparse file to a file. If gz is true, the data will be passed 153 | * through zlib. If sparse is true, the file will be written in the Android 154 | * sparse file format. If sparse is false, the file will be written by seeking 155 | * over unused chunks, producing a smaller file if the filesystem supports 156 | * sparse files. If crc is true, the crc of the expanded data will be 157 | * calculated and appended in a crc chunk. 158 | * 159 | * Returns 0 on success, negative errno on error. 160 | */ 161 | int sparse_file_write(struct sparse_file *s, int fd, bool gz, bool sparse, 162 | bool crc); 163 | 164 | /** 165 | * sparse_file_len - return the length of a sparse file if written to disk 166 | * 167 | * @s - sparse file cookie 168 | * @sparse - write in the Android sparse file format 169 | * @crc - append a crc chunk 170 | * 171 | * Returns the size a sparse file would be on disk if it were written in the 172 | * specified format. If sparse is true, this is the size of the data in the 173 | * sparse format. If sparse is false, this is the size of the normal 174 | * non-sparse file. 175 | */ 176 | int64_t sparse_file_len(struct sparse_file *s, bool sparse, bool crc); 177 | 178 | /** 179 | * sparse_file_block_size 180 | * 181 | * @s - sparse file cookie 182 | */ 183 | unsigned int sparse_file_block_size(struct sparse_file *s); 184 | 185 | /** 186 | * sparse_file_callback - call a callback for blocks in sparse file 187 | * 188 | * @s - sparse file cookie 189 | * @sparse - write in the Android sparse file format 190 | * @crc - append a crc chunk 191 | * @write - function to call for each block 192 | * @priv - value that will be passed as the first argument to write 193 | * 194 | * Writes a sparse file by calling a callback function. If sparse is true, the 195 | * file will be written in the Android sparse file format. If crc is true, the 196 | * crc of the expanded data will be calculated and appended in a crc chunk. 197 | * The callback 'write' will be called with data and length for each data, 198 | * and with data==NULL to skip over a region (only used for non-sparse format). 199 | * The callback should return negative on error, 0 on success. 200 | * 201 | * Returns 0 on success, negative errno on error. 202 | */ 203 | int sparse_file_callback(struct sparse_file *s, bool sparse, bool crc, 204 | int (*write)(void *priv, const void *data, int len), void *priv); 205 | 206 | /** 207 | * sparse_file_foreach_chunk - call a callback for data blocks in sparse file 208 | * 209 | * @s - sparse file cookie 210 | * @sparse - write in the Android sparse file format 211 | * @crc - append a crc chunk 212 | * @write - function to call for each block 213 | * @priv - value that will be passed as the first argument to write 214 | * 215 | * The function has the same behavior as 'sparse_file_callback', except it only 216 | * iterates on blocks that contain data. 217 | * 218 | * Returns 0 on success, negative errno on error. 219 | */ 220 | int sparse_file_foreach_chunk(struct sparse_file *s, bool sparse, bool crc, 221 | int (*write)(void *priv, const void *data, int len, unsigned int block, 222 | unsigned int nr_blocks), 223 | void *priv); 224 | /** 225 | * sparse_file_read - read a file into a sparse file cookie 226 | * 227 | * @s - sparse file cookie 228 | * @fd - file descriptor to read from 229 | * @sparse - read a file in the Android sparse file format 230 | * @crc - verify the crc of a file in the Android sparse file format 231 | * 232 | * Reads a file into a sparse file cookie. If sparse is true, the file is 233 | * assumed to be in the Android sparse file format. If sparse is false, the 234 | * file will be sparsed by looking for block aligned chunks of all zeros or 235 | * another 32 bit value. If crc is true, the crc of the sparse file will be 236 | * verified. 237 | * 238 | * Returns 0 on success, negative errno on error. 239 | */ 240 | int sparse_file_read(struct sparse_file *s, int fd, bool sparse, bool crc); 241 | 242 | /** 243 | * sparse_file_import - import an existing sparse file 244 | * 245 | * @s - sparse file cookie 246 | * @verbose - print verbose errors while reading the sparse file 247 | * @crc - verify the crc of a file in the Android sparse file format 248 | * 249 | * Reads an existing sparse file into a sparse file cookie, recreating the same 250 | * sparse cookie that was used to write it. If verbose is true, prints verbose 251 | * errors when the sparse file is formatted incorrectly. 252 | * 253 | * Returns a new sparse file cookie on success, NULL on error. 254 | */ 255 | struct sparse_file *sparse_file_import(int fd, bool verbose, bool crc); 256 | 257 | /** 258 | * sparse_file_import_auto - import an existing sparse or normal file 259 | * 260 | * @fd - file descriptor to read from 261 | * @crc - verify the crc of a file in the Android sparse file format 262 | * @verbose - whether to use verbose logging 263 | * 264 | * Reads an existing sparse or normal file into a sparse file cookie. 265 | * Attempts to determine if the file is sparse or not by looking for the sparse 266 | * file magic number in the first 4 bytes. If the file is not sparse, the file 267 | * will be sparsed by looking for block aligned chunks of all zeros or another 268 | * 32 bit value. If crc is true, the crc of the sparse file will be verified. 269 | * 270 | * Returns a new sparse file cookie on success, NULL on error. 271 | */ 272 | struct sparse_file *sparse_file_import_auto(int fd, bool crc, bool verbose); 273 | 274 | /** sparse_file_resparse - rechunk an existing sparse file into smaller files 275 | * 276 | * @in_s - sparse file cookie of the existing sparse file 277 | * @max_len - maximum file size 278 | * @out_s - array of sparse file cookies 279 | * @out_s_count - size of out_s array 280 | * 281 | * Splits chunks of an existing sparse file into smaller sparse files such that 282 | * each sparse file is less than max_len. Returns the number of sparse_files 283 | * that would have been written to out_s if out_s were big enough. 284 | */ 285 | int sparse_file_resparse(struct sparse_file *in_s, unsigned int max_len, 286 | struct sparse_file **out_s, int out_s_count); 287 | 288 | /** 289 | * sparse_file_verbose - set a sparse file cookie to print verbose errors 290 | * 291 | * @s - sparse file cookie 292 | * 293 | * Print verbose sparse file errors whenever using the sparse file cookie. 294 | */ 295 | void sparse_file_verbose(struct sparse_file *s); 296 | 297 | /** 298 | * sparse_print_verbose - function called to print verbose errors 299 | * 300 | * By default, verbose errors will print to standard error. 301 | * sparse_print_verbose may be overridden to log verbose errors somewhere else. 302 | * 303 | */ 304 | extern void (*sparse_print_verbose)(const char *fmt, ...); 305 | 306 | #ifdef __cplusplus 307 | } 308 | #endif 309 | 310 | #endif 311 | -------------------------------------------------------------------------------- /bin/src/simg2img/output_file.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2010 The Android Open Source Project 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | #define _FILE_OFFSET_BITS 64 18 | #define _LARGEFILE64_SOURCE 1 19 | 20 | #include 21 | #include 22 | #include 23 | #include 24 | #include 25 | #include 26 | #include 27 | #include 28 | #include 29 | #include 30 | #include 31 | 32 | #include "defs.h" 33 | #include "output_file.h" 34 | #include "sparse_crc32.h" 35 | #include "sparse_format.h" 36 | 37 | #ifndef USE_MINGW 38 | #include 39 | #define O_BINARY 0 40 | #else 41 | #define ftruncate64 ftruncate 42 | #endif 43 | 44 | #if defined(__APPLE__) && defined(__MACH__) 45 | #define lseek64 lseek 46 | #define ftruncate64 ftruncate 47 | #define mmap64 mmap 48 | #define off64_t off_t 49 | #endif 50 | 51 | #define min(a, b) \ 52 | ({ typeof(a) _a = (a); typeof(b) _b = (b); (_a < _b) ? _a : _b; }) 53 | 54 | #define SPARSE_HEADER_MAJOR_VER 1 55 | #define SPARSE_HEADER_MINOR_VER 0 56 | #define SPARSE_HEADER_LEN (sizeof(sparse_header_t)) 57 | #define CHUNK_HEADER_LEN (sizeof(chunk_header_t)) 58 | 59 | #define container_of(inner, outer_t, elem) \ 60 | ((outer_t *)((char *)(inner) - offsetof(outer_t, elem))) 61 | 62 | struct output_file_ops { 63 | int (*open) (struct output_file *, int fd); 64 | int (*skip) (struct output_file *, int64_t); 65 | int (*pad) (struct output_file *, int64_t); 66 | int (*write) (struct output_file *, void *, size_t); 67 | void (*close) (struct output_file *); 68 | }; 69 | 70 | struct sparse_file_ops { 71 | int (*write_data_chunk) (struct output_file * out, unsigned int len, void *data); 72 | int (*write_fill_chunk) (struct output_file * out, unsigned int len, uint32_t fill_val); 73 | int (*write_skip_chunk) (struct output_file * out, int64_t len); 74 | int (*write_end_chunk) (struct output_file * out); 75 | }; 76 | 77 | struct output_file { 78 | int64_t cur_out_ptr; 79 | unsigned int chunk_cnt; 80 | uint32_t crc32; 81 | struct output_file_ops *ops; 82 | struct sparse_file_ops *sparse_ops; 83 | int use_crc; 84 | unsigned int block_size; 85 | int64_t len; 86 | char *zero_buf; 87 | uint32_t *fill_buf; 88 | char *buf; 89 | }; 90 | 91 | struct output_file_gz { 92 | struct output_file out; 93 | gzFile gz_fd; 94 | }; 95 | 96 | #define to_output_file_gz(_o) \ 97 | container_of((_o), struct output_file_gz, out) 98 | 99 | struct output_file_normal { 100 | struct output_file out; 101 | int fd; 102 | }; 103 | 104 | #define to_output_file_normal(_o) \ 105 | container_of((_o), struct output_file_normal, out) 106 | 107 | struct output_file_callback { 108 | struct output_file out; 109 | void *priv; 110 | int (*write) (void *priv, const void *buf, int len); 111 | }; 112 | 113 | #define to_output_file_callback(_o) \ 114 | container_of((_o), struct output_file_callback, out) 115 | 116 | static int file_open(struct output_file *out, int fd) 117 | { 118 | struct output_file_normal *outn = to_output_file_normal(out); 119 | 120 | outn->fd = fd; 121 | return 0; 122 | } 123 | 124 | static int file_skip(struct output_file *out, int64_t cnt) 125 | { 126 | off64_t ret; 127 | struct output_file_normal *outn = to_output_file_normal(out); 128 | 129 | ret = lseek64(outn->fd, cnt, SEEK_CUR); 130 | if (ret < 0) { 131 | error_errno("lseek64"); 132 | return -1; 133 | } 134 | return 0; 135 | } 136 | 137 | static int file_pad(struct output_file *out, int64_t len) 138 | { 139 | int ret; 140 | struct output_file_normal *outn = to_output_file_normal(out); 141 | 142 | ret = ftruncate64(outn->fd, len); 143 | if (ret < 0) { 144 | return -errno; 145 | } 146 | 147 | return 0; 148 | } 149 | 150 | static int file_write(struct output_file *out, void *data, size_t len) 151 | { 152 | ssize_t ret; 153 | struct output_file_normal *outn = to_output_file_normal(out); 154 | 155 | while (len > 0) { 156 | ret = write(outn->fd, data, len); 157 | if (ret < 0) { 158 | if (errno == EINTR) { 159 | continue; 160 | } 161 | error_errno("write"); 162 | return -1; 163 | } 164 | 165 | data = (char *)data + ret; 166 | len -= ret; 167 | } 168 | 169 | return 0; 170 | } 171 | 172 | static void file_close(struct output_file *out) 173 | { 174 | struct output_file_normal *outn = to_output_file_normal(out); 175 | 176 | free(outn); 177 | } 178 | 179 | static struct output_file_ops file_ops = { 180 | .open = file_open, 181 | .skip = file_skip, 182 | .pad = file_pad, 183 | .write = file_write, 184 | .close = file_close, 185 | }; 186 | 187 | static int gz_file_open(struct output_file *out, int fd) 188 | { 189 | struct output_file_gz *outgz = to_output_file_gz(out); 190 | 191 | outgz->gz_fd = gzdopen(fd, "wb9"); 192 | if (!outgz->gz_fd) { 193 | error_errno("gzopen"); 194 | return -errno; 195 | } 196 | 197 | return 0; 198 | } 199 | 200 | static int gz_file_skip(struct output_file *out, int64_t cnt) 201 | { 202 | off64_t ret; 203 | struct output_file_gz *outgz = to_output_file_gz(out); 204 | 205 | ret = gzseek(outgz->gz_fd, cnt, SEEK_CUR); 206 | if (ret < 0) { 207 | error_errno("gzseek"); 208 | return -1; 209 | } 210 | return 0; 211 | } 212 | 213 | static int gz_file_pad(struct output_file *out, int64_t len) 214 | { 215 | off64_t ret; 216 | struct output_file_gz *outgz = to_output_file_gz(out); 217 | 218 | ret = gztell(outgz->gz_fd); 219 | if (ret < 0) { 220 | return -1; 221 | } 222 | 223 | if (ret >= len) { 224 | return 0; 225 | } 226 | 227 | ret = gzseek(outgz->gz_fd, len - 1, SEEK_SET); 228 | if (ret < 0) { 229 | return -1; 230 | } 231 | 232 | gzwrite(outgz->gz_fd, "", 1); 233 | 234 | return 0; 235 | } 236 | 237 | static int gz_file_write(struct output_file *out, void *data, size_t len) 238 | { 239 | int ret; 240 | struct output_file_gz *outgz = to_output_file_gz(out); 241 | 242 | while (len > 0) { 243 | ret = gzwrite(outgz->gz_fd, data, 244 | min(len, (unsigned int)INT_MAX)); 245 | if (ret == 0) { 246 | error("gzwrite %s", gzerror(outgz->gz_fd, NULL)); 247 | return -1; 248 | } 249 | len -= ret; 250 | data = (char *)data + ret; 251 | } 252 | 253 | return 0; 254 | } 255 | 256 | static void gz_file_close(struct output_file *out) 257 | { 258 | struct output_file_gz *outgz = to_output_file_gz(out); 259 | 260 | gzclose(outgz->gz_fd); 261 | free(outgz); 262 | } 263 | 264 | static struct output_file_ops gz_file_ops = { 265 | .open = gz_file_open, 266 | .skip = gz_file_skip, 267 | .pad = gz_file_pad, 268 | .write = gz_file_write, 269 | .close = gz_file_close, 270 | }; 271 | 272 | static int callback_file_open(struct output_file *out __unused, int fd __unused) 273 | { 274 | return 0; 275 | } 276 | 277 | static int callback_file_skip(struct output_file *out, int64_t off) 278 | { 279 | struct output_file_callback *outc = to_output_file_callback(out); 280 | int to_write; 281 | int ret; 282 | 283 | while (off > 0) { 284 | to_write = min(off, (int64_t) INT_MAX); 285 | ret = outc->write(outc->priv, NULL, to_write); 286 | if (ret < 0) { 287 | return ret; 288 | } 289 | off -= to_write; 290 | } 291 | 292 | return 0; 293 | } 294 | 295 | static int callback_file_pad(struct output_file *out __unused, int64_t len __unused) 296 | { 297 | return -1; 298 | } 299 | 300 | static int callback_file_write(struct output_file *out, void *data, size_t len) 301 | { 302 | struct output_file_callback *outc = to_output_file_callback(out); 303 | 304 | return outc->write(outc->priv, data, len); 305 | } 306 | 307 | static void callback_file_close(struct output_file *out) 308 | { 309 | struct output_file_callback *outc = to_output_file_callback(out); 310 | 311 | free(outc); 312 | } 313 | 314 | static struct output_file_ops callback_file_ops = { 315 | .open = callback_file_open, 316 | .skip = callback_file_skip, 317 | .pad = callback_file_pad, 318 | .write = callback_file_write, 319 | .close = callback_file_close, 320 | }; 321 | 322 | int read_all(int fd, void *buf, size_t len) 323 | { 324 | size_t total = 0; 325 | int ret; 326 | char *ptr = buf; 327 | 328 | while (total < len) { 329 | ret = read(fd, ptr, len - total); 330 | 331 | if (ret < 0) 332 | return -errno; 333 | 334 | if (ret == 0) 335 | return -EINVAL; 336 | 337 | ptr += ret; 338 | total += ret; 339 | } 340 | 341 | return 0; 342 | } 343 | 344 | static int write_sparse_skip_chunk(struct output_file *out, int64_t skip_len) 345 | { 346 | chunk_header_t chunk_header; 347 | int ret; 348 | 349 | if (skip_len % out->block_size) { 350 | error("don't care size %" PRIi64 " is not a multiple of the block size %u", 351 | skip_len, out->block_size); 352 | return -1; 353 | } 354 | 355 | /* We are skipping data, so emit a don't care chunk. */ 356 | chunk_header.chunk_type = CHUNK_TYPE_DONT_CARE; 357 | chunk_header.reserved1 = 0; 358 | chunk_header.chunk_sz = skip_len / out->block_size; 359 | chunk_header.total_sz = CHUNK_HEADER_LEN; 360 | ret = out->ops->write(out, &chunk_header, sizeof(chunk_header)); 361 | if (ret < 0) 362 | return -1; 363 | 364 | out->cur_out_ptr += skip_len; 365 | out->chunk_cnt++; 366 | 367 | return 0; 368 | } 369 | 370 | static int write_sparse_fill_chunk(struct output_file *out, unsigned int len, uint32_t fill_val) 371 | { 372 | chunk_header_t chunk_header; 373 | int rnd_up_len, count; 374 | int ret; 375 | 376 | /* Round up the fill length to a multiple of the block size */ 377 | rnd_up_len = ALIGN(len, out->block_size); 378 | 379 | /* Finally we can safely emit a chunk of data */ 380 | chunk_header.chunk_type = CHUNK_TYPE_FILL; 381 | chunk_header.reserved1 = 0; 382 | chunk_header.chunk_sz = rnd_up_len / out->block_size; 383 | chunk_header.total_sz = CHUNK_HEADER_LEN + sizeof(fill_val); 384 | ret = out->ops->write(out, &chunk_header, sizeof(chunk_header)); 385 | 386 | if (ret < 0) 387 | return -1; 388 | ret = out->ops->write(out, &fill_val, sizeof(fill_val)); 389 | if (ret < 0) 390 | return -1; 391 | 392 | if (out->use_crc) { 393 | count = out->block_size / sizeof(uint32_t); 394 | while (count--) 395 | out->crc32 = sparse_crc32(out->crc32, &fill_val, sizeof(uint32_t)); 396 | } 397 | 398 | out->cur_out_ptr += rnd_up_len; 399 | out->chunk_cnt++; 400 | 401 | return 0; 402 | } 403 | 404 | static int write_sparse_data_chunk(struct output_file *out, unsigned int len, void *data) 405 | { 406 | chunk_header_t chunk_header; 407 | int rnd_up_len, zero_len; 408 | int ret; 409 | 410 | /* Round up the data length to a multiple of the block size */ 411 | rnd_up_len = ALIGN(len, out->block_size); 412 | zero_len = rnd_up_len - len; 413 | 414 | /* Finally we can safely emit a chunk of data */ 415 | chunk_header.chunk_type = CHUNK_TYPE_RAW; 416 | chunk_header.reserved1 = 0; 417 | chunk_header.chunk_sz = rnd_up_len / out->block_size; 418 | chunk_header.total_sz = CHUNK_HEADER_LEN + rnd_up_len; 419 | ret = out->ops->write(out, &chunk_header, sizeof(chunk_header)); 420 | 421 | if (ret < 0) 422 | return -1; 423 | ret = out->ops->write(out, data, len); 424 | if (ret < 0) 425 | return -1; 426 | if (zero_len) { 427 | ret = out->ops->write(out, out->zero_buf, zero_len); 428 | if (ret < 0) 429 | return -1; 430 | } 431 | 432 | if (out->use_crc) { 433 | out->crc32 = sparse_crc32(out->crc32, data, len); 434 | if (zero_len) 435 | out->crc32 = sparse_crc32(out->crc32, out->zero_buf, zero_len); 436 | } 437 | 438 | out->cur_out_ptr += rnd_up_len; 439 | out->chunk_cnt++; 440 | 441 | return 0; 442 | } 443 | 444 | int write_sparse_end_chunk(struct output_file *out) 445 | { 446 | chunk_header_t chunk_header; 447 | int ret; 448 | 449 | if (out->use_crc) { 450 | chunk_header.chunk_type = CHUNK_TYPE_CRC32; 451 | chunk_header.reserved1 = 0; 452 | chunk_header.chunk_sz = 0; 453 | chunk_header.total_sz = CHUNK_HEADER_LEN + 4; 454 | 455 | ret = out->ops->write(out, &chunk_header, sizeof(chunk_header)); 456 | if (ret < 0) { 457 | return ret; 458 | } 459 | out->ops->write(out, &out->crc32, 4); 460 | if (ret < 0) { 461 | return ret; 462 | } 463 | 464 | out->chunk_cnt++; 465 | } 466 | 467 | return 0; 468 | } 469 | 470 | static struct sparse_file_ops sparse_file_ops = { 471 | .write_data_chunk = write_sparse_data_chunk, 472 | .write_fill_chunk = write_sparse_fill_chunk, 473 | .write_skip_chunk = write_sparse_skip_chunk, 474 | .write_end_chunk = write_sparse_end_chunk, 475 | }; 476 | 477 | static int write_normal_data_chunk(struct output_file *out, unsigned int len, void *data) 478 | { 479 | int ret; 480 | unsigned int rnd_up_len = ALIGN(len, out->block_size); 481 | 482 | ret = out->ops->write(out, data, len); 483 | if (ret < 0) { 484 | return ret; 485 | } 486 | 487 | if (rnd_up_len > len) { 488 | ret = out->ops->skip(out, rnd_up_len - len); 489 | } 490 | 491 | return ret; 492 | } 493 | 494 | static int write_normal_fill_chunk(struct output_file *out, unsigned int len, uint32_t fill_val) 495 | { 496 | int ret; 497 | unsigned int i; 498 | unsigned int write_len; 499 | 500 | /* Initialize fill_buf with the fill_val */ 501 | for (i = 0; i < out->block_size / sizeof(uint32_t); i++) { 502 | out->fill_buf[i] = fill_val; 503 | } 504 | 505 | while (len) { 506 | write_len = min(len, out->block_size); 507 | ret = out->ops->write(out, out->fill_buf, write_len); 508 | if (ret < 0) { 509 | return ret; 510 | } 511 | 512 | len -= write_len; 513 | } 514 | 515 | return 0; 516 | } 517 | 518 | static int write_normal_skip_chunk(struct output_file *out, int64_t len) 519 | { 520 | return out->ops->skip(out, len); 521 | } 522 | 523 | int write_normal_end_chunk(struct output_file *out) 524 | { 525 | return out->ops->pad(out, out->len); 526 | } 527 | 528 | static struct sparse_file_ops normal_file_ops = { 529 | .write_data_chunk = write_normal_data_chunk, 530 | .write_fill_chunk = write_normal_fill_chunk, 531 | .write_skip_chunk = write_normal_skip_chunk, 532 | .write_end_chunk = write_normal_end_chunk, 533 | }; 534 | 535 | void output_file_close(struct output_file *out) 536 | { 537 | out->sparse_ops->write_end_chunk(out); 538 | out->ops->close(out); 539 | } 540 | 541 | static int output_file_init(struct output_file *out, int block_size, 542 | int64_t len, bool sparse, int chunks, bool crc) 543 | { 544 | int ret; 545 | 546 | out->len = len; 547 | out->block_size = block_size; 548 | out->cur_out_ptr = 0ll; 549 | out->chunk_cnt = 0; 550 | out->crc32 = 0; 551 | out->use_crc = crc; 552 | 553 | out->zero_buf = calloc(block_size, 1); 554 | if (!out->zero_buf) { 555 | error_errno("malloc zero_buf"); 556 | return -ENOMEM; 557 | } 558 | 559 | out->fill_buf = calloc(block_size, 1); 560 | if (!out->fill_buf) { 561 | error_errno("malloc fill_buf"); 562 | ret = -ENOMEM; 563 | goto err_fill_buf; 564 | } 565 | 566 | if (sparse) { 567 | out->sparse_ops = &sparse_file_ops; 568 | } else { 569 | out->sparse_ops = &normal_file_ops; 570 | } 571 | 572 | if (sparse) { 573 | sparse_header_t sparse_header = { 574 | .magic = SPARSE_HEADER_MAGIC, 575 | .major_version = SPARSE_HEADER_MAJOR_VER, 576 | .minor_version = SPARSE_HEADER_MINOR_VER, 577 | .file_hdr_sz = SPARSE_HEADER_LEN, 578 | .chunk_hdr_sz = CHUNK_HEADER_LEN, 579 | .blk_sz = out->block_size, 580 | .total_blks = out->len / out->block_size, 581 | .total_chunks = chunks, 582 | .image_checksum = 0 583 | }; 584 | 585 | if (out->use_crc) { 586 | sparse_header.total_chunks++; 587 | } 588 | 589 | ret = out->ops->write(out, &sparse_header, sizeof(sparse_header)); 590 | if (ret < 0) { 591 | goto err_write; 592 | } 593 | } 594 | 595 | return 0; 596 | 597 | err_write: 598 | free(out->fill_buf); 599 | err_fill_buf: 600 | free(out->zero_buf); 601 | return ret; 602 | } 603 | 604 | static struct output_file *output_file_new_gz(void) 605 | { 606 | struct output_file_gz *outgz = calloc(1, sizeof(struct output_file_gz)); 607 | if (!outgz) { 608 | error_errno("malloc struct outgz"); 609 | return NULL; 610 | } 611 | 612 | outgz->out.ops = &gz_file_ops; 613 | 614 | return &outgz->out; 615 | } 616 | 617 | static struct output_file *output_file_new_normal(void) 618 | { 619 | struct output_file_normal *outn = calloc(1, sizeof(struct output_file_normal)); 620 | if (!outn) { 621 | error_errno("malloc struct outn"); 622 | return NULL; 623 | } 624 | 625 | outn->out.ops = &file_ops; 626 | 627 | return &outn->out; 628 | } 629 | 630 | struct output_file *output_file_open_callback(int (*write) (void *, const void *, int), 631 | void *priv, unsigned int block_size, int64_t len, 632 | int gz __unused, int sparse, int chunks, int crc) 633 | { 634 | int ret; 635 | struct output_file_callback *outc; 636 | 637 | outc = calloc(1, sizeof(struct output_file_callback)); 638 | if (!outc) { 639 | error_errno("malloc struct outc"); 640 | return NULL; 641 | } 642 | 643 | outc->out.ops = &callback_file_ops; 644 | outc->priv = priv; 645 | outc->write = write; 646 | 647 | ret = output_file_init(&outc->out, block_size, len, sparse, chunks, crc); 648 | if (ret < 0) { 649 | free(outc); 650 | return NULL; 651 | } 652 | 653 | return &outc->out; 654 | } 655 | 656 | struct output_file *output_file_open_fd(int fd, unsigned int block_size, int64_t len, 657 | int gz, int sparse, int chunks, int crc) 658 | { 659 | int ret; 660 | struct output_file *out; 661 | 662 | if (gz) { 663 | out = output_file_new_gz(); 664 | } else { 665 | out = output_file_new_normal(); 666 | } 667 | if (!out) { 668 | return NULL; 669 | } 670 | 671 | out->ops->open(out, fd); 672 | 673 | ret = output_file_init(out, block_size, len, sparse, chunks, crc); 674 | if (ret < 0) { 675 | free(out); 676 | return NULL; 677 | } 678 | 679 | return out; 680 | } 681 | 682 | /* Write a contiguous region of data blocks from a memory buffer */ 683 | int write_data_chunk(struct output_file *out, unsigned int len, void *data) 684 | { 685 | return out->sparse_ops->write_data_chunk(out, len, data); 686 | } 687 | 688 | /* Write a contiguous region of data blocks with a fill value */ 689 | int write_fill_chunk(struct output_file *out, unsigned int len, uint32_t fill_val) 690 | { 691 | return out->sparse_ops->write_fill_chunk(out, len, fill_val); 692 | } 693 | 694 | int write_fd_chunk(struct output_file *out, unsigned int len, int fd, int64_t offset) 695 | { 696 | int ret; 697 | int64_t aligned_offset; 698 | int aligned_diff; 699 | uint64_t buffer_size; 700 | char *ptr; 701 | 702 | aligned_offset = offset & ~(4096 - 1); 703 | aligned_diff = offset - aligned_offset; 704 | buffer_size = (uint64_t)len + (uint64_t)aligned_diff; 705 | 706 | #ifndef USE_MINGW 707 | if (buffer_size > SIZE_MAX) 708 | return -E2BIG; 709 | char *data = mmap64(NULL, buffer_size, PROT_READ, MAP_SHARED, fd, 710 | aligned_offset); 711 | if (data == MAP_FAILED) { 712 | return -errno; 713 | } 714 | ptr = data + aligned_diff; 715 | #else 716 | off64_t pos; 717 | char *data = malloc(len); 718 | if (!data) { 719 | return -errno; 720 | } 721 | pos = lseek64(fd, offset, SEEK_SET); 722 | if (pos < 0) { 723 | free(data); 724 | return -errno; 725 | } 726 | ret = read_all(fd, data, len); 727 | if (ret < 0) { 728 | free(data); 729 | return ret; 730 | } 731 | ptr = data; 732 | #endif 733 | 734 | ret = out->sparse_ops->write_data_chunk(out, len, ptr); 735 | 736 | #ifndef USE_MINGW 737 | munmap(data, buffer_size); 738 | #else 739 | free(data); 740 | #endif 741 | 742 | return ret; 743 | } 744 | 745 | /* Write a contiguous region of data blocks from a file */ 746 | int write_file_chunk(struct output_file *out, unsigned int len, const char *file, int64_t offset) 747 | { 748 | int ret; 749 | 750 | int file_fd = open(file, O_RDONLY | O_BINARY); 751 | if (file_fd < 0) { 752 | return -errno; 753 | } 754 | 755 | ret = write_fd_chunk(out, len, file_fd, offset); 756 | 757 | close(file_fd); 758 | 759 | return ret; 760 | } 761 | 762 | int write_skip_chunk(struct output_file *out, int64_t len) 763 | { 764 | return out->sparse_ops->write_skip_chunk(out, len); 765 | } 766 | -------------------------------------------------------------------------------- /bin/src/simg2img/output_file.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2010 The Android Open Source Project 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | #ifndef _OUTPUT_FILE_H_ 18 | #define _OUTPUT_FILE_H_ 19 | 20 | #include 21 | 22 | struct output_file; 23 | 24 | struct output_file *output_file_open_fd(int fd, unsigned int block_size, int64_t len, 25 | int gz, int sparse, int chunks, int crc); 26 | struct output_file *output_file_open_callback(int (*write) (void *, const void *, int), 27 | void *priv, unsigned int block_size, int64_t len, 28 | int gz, int sparse, int chunks, int crc); 29 | int write_data_chunk(struct output_file *out, unsigned int len, void *data); 30 | int write_fill_chunk(struct output_file *out, unsigned int len, uint32_t fill_val); 31 | int write_file_chunk(struct output_file *out, unsigned int len, const char *file, int64_t offset); 32 | int write_fd_chunk(struct output_file *out, unsigned int len, int fd, int64_t offset); 33 | int write_skip_chunk(struct output_file *out, int64_t len); 34 | void output_file_close(struct output_file *out); 35 | 36 | int read_all(int fd, void *buf, size_t len); 37 | 38 | #endif 39 | -------------------------------------------------------------------------------- /bin/src/simg2img/simg2img.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2010 The Android Open Source Project 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | #include 18 | 19 | #include 20 | #include 21 | #include 22 | #include 23 | #include 24 | #include 25 | #include 26 | #include 27 | #include 28 | 29 | #ifndef O_BINARY 30 | #define O_BINARY 0 31 | #endif 32 | 33 | void usage() 34 | { 35 | fprintf(stderr, "Usage: simg2img \n"); 36 | } 37 | 38 | int main(int argc, char *argv[]) 39 | { 40 | int in; 41 | int out; 42 | int i; 43 | struct sparse_file *s; 44 | 45 | if (argc < 3) { 46 | usage(); 47 | exit(-1); 48 | } 49 | 50 | out = open(argv[argc - 1], O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, 0664); 51 | if (out < 0) { 52 | fprintf(stderr, "Cannot open output file %s\n", argv[argc - 1]); 53 | exit(-1); 54 | } 55 | 56 | for (i = 1; i < argc - 1; i++) { 57 | if (strcmp(argv[i], "-") == 0) { 58 | in = STDIN_FILENO; 59 | } else { 60 | in = open(argv[i], O_RDONLY | O_BINARY); 61 | if (in < 0) { 62 | fprintf(stderr, "Cannot open input file %s\n", argv[i]); 63 | exit(-1); 64 | } 65 | } 66 | 67 | s = sparse_file_import(in, true, false); 68 | if (!s) { 69 | fprintf(stderr, "Failed to read sparse file\n"); 70 | exit(-1); 71 | } 72 | 73 | if (lseek(out, 0, SEEK_SET) == -1) { 74 | perror("lseek failed"); 75 | exit(EXIT_FAILURE); 76 | } 77 | 78 | if (sparse_file_write(s, out, false, false, false) < 0) { 79 | fprintf(stderr, "Cannot write output file\n"); 80 | exit(-1); 81 | } 82 | sparse_file_destroy(s); 83 | close(in); 84 | } 85 | 86 | close(out); 87 | 88 | exit(0); 89 | } 90 | -------------------------------------------------------------------------------- /bin/src/simg2img/simg2simg.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2012 The Android Open Source Project 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | #define _FILE_OFFSET_BITS 64 18 | #define _LARGEFILE64_SOURCE 1 19 | #define _GNU_SOURCE 20 | 21 | #include 22 | #include 23 | #include 24 | #include 25 | #include 26 | #include 27 | #include 28 | #include 29 | #include 30 | 31 | #include 32 | 33 | #ifndef O_BINARY 34 | #define O_BINARY 0 35 | #endif 36 | 37 | void usage() 38 | { 39 | fprintf(stderr, "Usage: simg2simg \n"); 40 | } 41 | 42 | int main(int argc, char *argv[]) 43 | { 44 | int in; 45 | int out; 46 | int i; 47 | int ret; 48 | struct sparse_file *s; 49 | int64_t max_size; 50 | struct sparse_file **out_s; 51 | int files; 52 | char filename[4096]; 53 | 54 | if (argc != 4) { 55 | usage(); 56 | exit(-1); 57 | } 58 | 59 | max_size = atoll(argv[3]); 60 | 61 | in = open(argv[1], O_RDONLY | O_BINARY); 62 | if (in < 0) { 63 | fprintf(stderr, "Cannot open input file %s\n", argv[1]); 64 | exit(-1); 65 | } 66 | 67 | s = sparse_file_import(in, true, false); 68 | if (!s) { 69 | fprintf(stderr, "Failed to import sparse file\n"); 70 | exit(-1); 71 | } 72 | 73 | files = sparse_file_resparse(s, max_size, NULL, 0); 74 | if (files < 0) { 75 | fprintf(stderr, "Failed to resparse\n"); 76 | exit(-1); 77 | } 78 | 79 | out_s = calloc(sizeof(struct sparse_file *), files); 80 | if (!out_s) { 81 | fprintf(stderr, "Failed to allocate sparse file array\n"); 82 | exit(-1); 83 | } 84 | 85 | files = sparse_file_resparse(s, max_size, out_s, files); 86 | if (files < 0) { 87 | fprintf(stderr, "Failed to resparse\n"); 88 | exit(-1); 89 | } 90 | 91 | for (i = 0; i < files; i++) { 92 | ret = snprintf(filename, sizeof(filename), "%s.%d", argv[2], i); 93 | if (ret >= (int)sizeof(filename)) { 94 | fprintf(stderr, "Filename too long\n"); 95 | exit(-1); 96 | } 97 | 98 | out = open(filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, 0664); 99 | if (out < 0) { 100 | fprintf(stderr, "Cannot open output file %s\n", argv[2]); 101 | exit(-1); 102 | } 103 | 104 | ret = sparse_file_write(out_s[i], out, false, true, false); 105 | if (ret) { 106 | fprintf(stderr, "Failed to write sparse file\n"); 107 | exit(-1); 108 | } 109 | close(out); 110 | } 111 | 112 | close(in); 113 | 114 | exit(0); 115 | } 116 | -------------------------------------------------------------------------------- /bin/src/simg2img/simg_dump.py: -------------------------------------------------------------------------------- 1 | #! /usr/bin/env python 2 | 3 | # Copyright (C) 2012 The Android Open Source Project 4 | # 5 | # Licensed under the Apache License, Version 2.0 (the "License"); 6 | # you may not use this file except in compliance with the License. 7 | # You may obtain a copy of the License at 8 | # 9 | # http://www.apache.org/licenses/LICENSE-2.0 10 | # 11 | # Unless required by applicable law or agreed to in writing, software 12 | # distributed under the License is distributed on an "AS IS" BASIS, 13 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | # See the License for the specific language governing permissions and 15 | # limitations under the License. 16 | 17 | from __future__ import print_function 18 | import csv 19 | import getopt 20 | import hashlib 21 | import posixpath 22 | import signal 23 | import struct 24 | import sys 25 | 26 | 27 | def usage(argv0): 28 | print(""" 29 | Usage: %s [-v] [-s] [-c ] sparse_image_file ... 30 | -v verbose output 31 | -s show sha1sum of data blocks 32 | -c save .csv file of blocks 33 | """ % (argv0)) 34 | sys.exit(2) 35 | 36 | 37 | def main(): 38 | signal.signal(signal.SIGPIPE, signal.SIG_DFL) 39 | 40 | me = posixpath.basename(sys.argv[0]) 41 | 42 | # Parse the command line 43 | verbose = 0 # -v 44 | showhash = 0 # -s 45 | csvfilename = None # -c 46 | try: 47 | opts, args = getopt.getopt(sys.argv[1:], 48 | "vsc:", 49 | ["verbose", "showhash", "csvfile"]) 50 | except getopt.GetoptError, e: 51 | print(e) 52 | usage(me) 53 | for o, a in opts: 54 | if o in ("-v", "--verbose"): 55 | verbose += 1 56 | elif o in ("-s", "--showhash"): 57 | showhash = True 58 | elif o in ("-c", "--csvfile"): 59 | csvfilename = a 60 | else: 61 | print("Unrecognized option \"%s\"" % (o)) 62 | usage(me) 63 | 64 | if not args: 65 | print("No sparse_image_file specified") 66 | usage(me) 67 | 68 | if csvfilename: 69 | csvfile = open(csvfilename, "wb") 70 | csvwriter = csv.writer(csvfile) 71 | 72 | output = verbose or csvfilename or showhash 73 | 74 | for path in args: 75 | FH = open(path, "rb") 76 | header_bin = FH.read(28) 77 | header = struct.unpack(" 0: 116 | print(" input_bytes output_blocks") 117 | print("chunk offset number offset number") 118 | 119 | if csvfilename: 120 | csvwriter.writerow(["chunk", "input offset", "input bytes", 121 | "output offset", "output blocks", "type", "hash"]) 122 | 123 | offset = 0 124 | for i in xrange(1, total_chunks + 1): 125 | header_bin = FH.read(12) 126 | header = struct.unpack("<2H2I", header_bin) 127 | chunk_type = header[0] 128 | chunk_sz = header[2] 129 | total_sz = header[3] 130 | data_sz = total_sz - 12 131 | curhash = "" 132 | curtype = "" 133 | curpos = FH.tell() 134 | 135 | if verbose > 0: 136 | print("%4u %10u %10u %7u %7u" % (i, curpos, data_sz, offset, chunk_sz), 137 | end=" ") 138 | 139 | if chunk_type == 0xCAC1: 140 | if data_sz != (chunk_sz * blk_sz): 141 | print("Raw chunk input size (%u) does not match output size (%u)" 142 | % (data_sz, chunk_sz * blk_sz)) 143 | break 144 | else: 145 | curtype = "Raw data" 146 | data = FH.read(data_sz) 147 | if showhash: 148 | h = hashlib.sha1() 149 | h.update(data) 150 | curhash = h.hexdigest() 151 | elif chunk_type == 0xCAC2: 152 | if data_sz != 4: 153 | print("Fill chunk should have 4 bytes of fill, but this has %u" 154 | % (data_sz)) 155 | break 156 | else: 157 | fill_bin = FH.read(4) 158 | fill = struct.unpack(" 0: 186 | print("%-18s" % (curtype), end=" ") 187 | 188 | if verbose > 1: 189 | header = struct.unpack("<12B", header_bin) 190 | print(" (%02X%02X %02X%02X %02X%02X%02X%02X %02X%02X%02X%02X)" 191 | % (header[0], header[1], header[2], header[3], 192 | header[4], header[5], header[6], header[7], 193 | header[8], header[9], header[10], header[11]), end=" ") 194 | 195 | print(curhash) 196 | 197 | if csvfilename: 198 | csvwriter.writerow([i, curpos, data_sz, offset, chunk_sz, curtype, 199 | curhash]) 200 | 201 | offset += chunk_sz 202 | 203 | if verbose > 0: 204 | print(" %10u %7u End" % (FH.tell(), offset)) 205 | 206 | if total_blks != offset: 207 | print("The header said we should have %u output blocks, but we saw %u" 208 | % (total_blks, offset)) 209 | 210 | junk_len = len(FH.read()) 211 | if junk_len: 212 | print("There were %u bytes of extra data at the end of the file." 213 | % (junk_len)) 214 | 215 | if csvfilename: 216 | csvfile.close() 217 | 218 | sys.exit(0) 219 | 220 | if __name__ == "__main__": 221 | main() 222 | -------------------------------------------------------------------------------- /bin/src/simg2img/sparse.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2012 The Android Open Source Project 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | #include 18 | #include 19 | 20 | #include 21 | 22 | #include "defs.h" 23 | #include "sparse_file.h" 24 | 25 | #include "output_file.h" 26 | #include "backed_block.h" 27 | #include "sparse_defs.h" 28 | #include "sparse_format.h" 29 | 30 | struct sparse_file *sparse_file_new(unsigned int block_size, int64_t len) 31 | { 32 | struct sparse_file *s = calloc(sizeof(struct sparse_file), 1); 33 | if (!s) { 34 | return NULL; 35 | } 36 | 37 | s->backed_block_list = backed_block_list_new(block_size); 38 | if (!s->backed_block_list) { 39 | free(s); 40 | return NULL; 41 | } 42 | 43 | s->block_size = block_size; 44 | s->len = len; 45 | 46 | return s; 47 | } 48 | 49 | void sparse_file_destroy(struct sparse_file *s) 50 | { 51 | backed_block_list_destroy(s->backed_block_list); 52 | free(s); 53 | } 54 | 55 | int sparse_file_add_data(struct sparse_file *s, void *data, unsigned int len, unsigned int block) 56 | { 57 | return backed_block_add_data(s->backed_block_list, data, len, block); 58 | } 59 | 60 | int sparse_file_add_fill(struct sparse_file *s, 61 | uint32_t fill_val, unsigned int len, unsigned int block) 62 | { 63 | return backed_block_add_fill(s->backed_block_list, fill_val, len, block); 64 | } 65 | 66 | int sparse_file_add_file(struct sparse_file *s, 67 | const char *filename, int64_t file_offset, unsigned int len, 68 | unsigned int block) 69 | { 70 | return backed_block_add_file(s->backed_block_list, filename, file_offset, len, block); 71 | } 72 | 73 | int sparse_file_add_fd(struct sparse_file *s, 74 | int fd, int64_t file_offset, unsigned int len, unsigned int block) 75 | { 76 | return backed_block_add_fd(s->backed_block_list, fd, file_offset, len, block); 77 | } 78 | 79 | unsigned int sparse_count_chunks(struct sparse_file *s) 80 | { 81 | struct backed_block *bb; 82 | unsigned int last_block = 0; 83 | unsigned int chunks = 0; 84 | 85 | for (bb = backed_block_iter_new(s->backed_block_list); bb; bb = backed_block_iter_next(bb)) { 86 | if (backed_block_block(bb) > last_block) { 87 | /* If there is a gap between chunks, add a skip chunk */ 88 | chunks++; 89 | } 90 | chunks++; 91 | last_block = backed_block_block(bb) + DIV_ROUND_UP(backed_block_len(bb), s->block_size); 92 | } 93 | if (last_block < DIV_ROUND_UP(s->len, s->block_size)) { 94 | chunks++; 95 | } 96 | 97 | return chunks; 98 | } 99 | 100 | static int sparse_file_write_block(struct output_file *out, struct backed_block *bb) 101 | { 102 | int ret = -EINVAL; 103 | 104 | switch (backed_block_type(bb)) { 105 | case BACKED_BLOCK_DATA: 106 | ret = write_data_chunk(out, backed_block_len(bb), backed_block_data(bb)); 107 | break; 108 | case BACKED_BLOCK_FILE: 109 | ret = write_file_chunk(out, backed_block_len(bb), 110 | backed_block_filename(bb), backed_block_file_offset(bb)); 111 | break; 112 | case BACKED_BLOCK_FD: 113 | ret = write_fd_chunk(out, backed_block_len(bb), 114 | backed_block_fd(bb), backed_block_file_offset(bb)); 115 | break; 116 | case BACKED_BLOCK_FILL: 117 | ret = write_fill_chunk(out, backed_block_len(bb), backed_block_fill_val(bb)); 118 | break; 119 | } 120 | 121 | return ret; 122 | } 123 | 124 | static int write_all_blocks(struct sparse_file *s, struct output_file *out) 125 | { 126 | struct backed_block *bb; 127 | unsigned int last_block = 0; 128 | int64_t pad; 129 | int ret = 0; 130 | 131 | for (bb = backed_block_iter_new(s->backed_block_list); bb; bb = backed_block_iter_next(bb)) { 132 | if (backed_block_block(bb) > last_block) { 133 | unsigned int blocks = backed_block_block(bb) - last_block; 134 | write_skip_chunk(out, (int64_t) blocks * s->block_size); 135 | } 136 | ret = sparse_file_write_block(out, bb); 137 | if (ret) 138 | return ret; 139 | last_block = backed_block_block(bb) + DIV_ROUND_UP(backed_block_len(bb), s->block_size); 140 | } 141 | 142 | pad = s->len - (int64_t) last_block *s->block_size; 143 | assert(pad >= 0); 144 | if (pad > 0) { 145 | write_skip_chunk(out, pad); 146 | } 147 | 148 | return 0; 149 | } 150 | 151 | int sparse_file_write(struct sparse_file *s, int fd, bool gz, bool sparse, bool crc) 152 | { 153 | int ret; 154 | int chunks; 155 | struct output_file *out; 156 | 157 | chunks = sparse_count_chunks(s); 158 | out = output_file_open_fd(fd, s->block_size, s->len, gz, sparse, chunks, crc); 159 | 160 | if (!out) 161 | return -ENOMEM; 162 | 163 | ret = write_all_blocks(s, out); 164 | 165 | output_file_close(out); 166 | 167 | return ret; 168 | } 169 | 170 | int sparse_file_callback(struct sparse_file *s, bool sparse, bool crc, 171 | int (*write) (void *priv, const void *data, int len), void *priv) 172 | { 173 | int ret; 174 | int chunks; 175 | struct output_file *out; 176 | 177 | chunks = sparse_count_chunks(s); 178 | out = output_file_open_callback(write, priv, s->block_size, s->len, false, sparse, chunks, crc); 179 | 180 | if (!out) 181 | return -ENOMEM; 182 | 183 | ret = write_all_blocks(s, out); 184 | 185 | output_file_close(out); 186 | 187 | return ret; 188 | } 189 | 190 | struct chunk_data { 191 | void *priv; 192 | unsigned int block; 193 | unsigned int nr_blocks; 194 | int (*write)(void *priv, const void *data, int len, unsigned int block, 195 | unsigned int nr_blocks); 196 | }; 197 | 198 | static int foreach_chunk_write(void *priv, const void *data, int len) 199 | { 200 | struct chunk_data *chk = priv; 201 | 202 | return chk->write(chk->priv, data, len, chk->block, chk->nr_blocks); 203 | } 204 | 205 | int sparse_file_foreach_chunk(struct sparse_file *s, bool sparse, bool crc, 206 | int (*write)(void *priv, const void *data, int len, unsigned int block, 207 | unsigned int nr_blocks), 208 | void *priv) 209 | { 210 | int ret; 211 | int chunks; 212 | struct chunk_data chk; 213 | struct output_file *out; 214 | struct backed_block *bb; 215 | 216 | chk.priv = priv; 217 | chk.write = write; 218 | chk.block = chk.nr_blocks = 0; 219 | chunks = sparse_count_chunks(s); 220 | out = output_file_open_callback(foreach_chunk_write, &chk, 221 | s->block_size, s->len, false, sparse, 222 | chunks, crc); 223 | 224 | if (!out) 225 | return -ENOMEM; 226 | 227 | for (bb = backed_block_iter_new(s->backed_block_list); bb; 228 | bb = backed_block_iter_next(bb)) { 229 | chk.block = backed_block_block(bb); 230 | chk.nr_blocks = (backed_block_len(bb) - 1) / s->block_size + 1; 231 | ret = sparse_file_write_block(out, bb); 232 | if (ret) 233 | return ret; 234 | } 235 | 236 | output_file_close(out); 237 | 238 | return ret; 239 | } 240 | 241 | static int out_counter_write(void *priv, const void *data __unused, int len) 242 | { 243 | int64_t *count = priv; 244 | *count += len; 245 | return 0; 246 | } 247 | 248 | int64_t sparse_file_len(struct sparse_file * s, bool sparse, bool crc) 249 | { 250 | int ret; 251 | int chunks = sparse_count_chunks(s); 252 | int64_t count = 0; 253 | struct output_file *out; 254 | 255 | out = output_file_open_callback(out_counter_write, &count, 256 | s->block_size, s->len, false, sparse, chunks, crc); 257 | if (!out) { 258 | return -1; 259 | } 260 | 261 | ret = write_all_blocks(s, out); 262 | 263 | output_file_close(out); 264 | 265 | if (ret < 0) { 266 | return -1; 267 | } 268 | 269 | return count; 270 | } 271 | 272 | unsigned int sparse_file_block_size(struct sparse_file *s) 273 | { 274 | return s->block_size; 275 | } 276 | 277 | static struct backed_block *move_chunks_up_to_len(struct sparse_file *from, 278 | struct sparse_file *to, unsigned int len) 279 | { 280 | int64_t count = 0; 281 | struct output_file *out_counter; 282 | struct backed_block *last_bb = NULL; 283 | struct backed_block *bb; 284 | struct backed_block *start; 285 | unsigned int last_block = 0; 286 | int64_t file_len = 0; 287 | int ret; 288 | 289 | /* 290 | * overhead is sparse file header, the potential end skip 291 | * chunk and crc chunk. 292 | */ 293 | int overhead = sizeof(sparse_header_t) + 2 * sizeof(chunk_header_t) + sizeof(uint32_t); 294 | len -= overhead; 295 | 296 | start = backed_block_iter_new(from->backed_block_list); 297 | out_counter = output_file_open_callback(out_counter_write, &count, 298 | to->block_size, to->len, false, true, 0, false); 299 | if (!out_counter) { 300 | return NULL; 301 | } 302 | 303 | for (bb = start; bb; bb = backed_block_iter_next(bb)) { 304 | count = 0; 305 | if (backed_block_block(bb) > last_block) 306 | count += sizeof(chunk_header_t); 307 | last_block = backed_block_block(bb) + DIV_ROUND_UP(backed_block_len(bb), to->block_size); 308 | 309 | /* will call out_counter_write to update count */ 310 | ret = sparse_file_write_block(out_counter, bb); 311 | if (ret) { 312 | bb = NULL; 313 | goto out; 314 | } 315 | if (file_len + count > len) { 316 | /* 317 | * If the remaining available size is more than 1/8th of the 318 | * requested size, split the chunk. Results in sparse files that 319 | * are at least 7/8ths of the requested size 320 | */ 321 | file_len += sizeof(chunk_header_t); 322 | if (!last_bb || (len - file_len > (len / 8))) { 323 | backed_block_split(from->backed_block_list, bb, len - file_len); 324 | last_bb = bb; 325 | } 326 | goto move; 327 | } 328 | file_len += count; 329 | last_bb = bb; 330 | } 331 | 332 | move: 333 | backed_block_list_move(from->backed_block_list, to->backed_block_list, start, last_bb); 334 | 335 | out: 336 | output_file_close(out_counter); 337 | 338 | return bb; 339 | } 340 | 341 | int sparse_file_resparse(struct sparse_file *in_s, unsigned int max_len, 342 | struct sparse_file **out_s, int out_s_count) 343 | { 344 | struct backed_block *bb; 345 | struct sparse_file *s; 346 | struct sparse_file *tmp; 347 | int c = 0; 348 | 349 | tmp = sparse_file_new(in_s->block_size, in_s->len); 350 | if (!tmp) { 351 | return -ENOMEM; 352 | } 353 | 354 | do { 355 | s = sparse_file_new(in_s->block_size, in_s->len); 356 | 357 | bb = move_chunks_up_to_len(in_s, s, max_len); 358 | 359 | if (c < out_s_count) { 360 | out_s[c] = s; 361 | } else { 362 | backed_block_list_move(s->backed_block_list, tmp->backed_block_list, NULL, NULL); 363 | sparse_file_destroy(s); 364 | } 365 | c++; 366 | } while (bb); 367 | 368 | backed_block_list_move(tmp->backed_block_list, in_s->backed_block_list, NULL, NULL); 369 | 370 | sparse_file_destroy(tmp); 371 | 372 | return c; 373 | } 374 | 375 | void sparse_file_verbose(struct sparse_file *s) 376 | { 377 | s->verbose = true; 378 | } 379 | -------------------------------------------------------------------------------- /bin/src/simg2img/sparse_crc32.c: -------------------------------------------------------------------------------- 1 | /*- 2 | * COPYRIGHT (C) 1986 Gary S. Brown. You may use this program, or 3 | * code or tables extracted from it, as desired without restriction. 4 | */ 5 | 6 | /* 7 | * First, the polynomial itself and its table of feedback terms. The 8 | * polynomial is 9 | * X^32+X^26+X^23+X^22+X^16+X^12+X^11+X^10+X^8+X^7+X^5+X^4+X^2+X^1+X^0 10 | * 11 | * Note that we take it "backwards" and put the highest-order term in 12 | * the lowest-order bit. The X^32 term is "implied"; the LSB is the 13 | * X^31 term, etc. The X^0 term (usually shown as "+1") results in 14 | * the MSB being 1 15 | * 16 | * Note that the usual hardware shift register implementation, which 17 | * is what we're using (we're merely optimizing it by doing eight-bit 18 | * chunks at a time) shifts bits into the lowest-order term. In our 19 | * implementation, that means shifting towards the right. Why do we 20 | * do it this way? Because the calculated CRC must be transmitted in 21 | * order from highest-order term to lowest-order term. UARTs transmit 22 | * characters in order from LSB to MSB. By storing the CRC this way 23 | * we hand it to the UART in the order low-byte to high-byte; the UART 24 | * sends each low-bit to hight-bit; and the result is transmission bit 25 | * by bit from highest- to lowest-order term without requiring any bit 26 | * shuffling on our part. Reception works similarly 27 | * 28 | * The feedback terms table consists of 256, 32-bit entries. Notes 29 | * 30 | * The table can be generated at runtime if desired; code to do so 31 | * is shown later. It might not be obvious, but the feedback 32 | * terms simply represent the results of eight shift/xor opera 33 | * tions for all combinations of data and CRC register values 34 | * 35 | * The values must be right-shifted by eight bits by the "updcrc 36 | * logic; the shift must be unsigned (bring in zeroes). On some 37 | * hardware you could probably optimize the shift in assembler by 38 | * using byte-swap instructions 39 | * polynomial $edb88320 40 | * 41 | * 42 | * CRC32 code derived from work by Gary S. Brown. 43 | */ 44 | 45 | /* Code taken from FreeBSD 8 */ 46 | #include 47 | 48 | static uint32_t crc32_tab[] = { 49 | 0x00000000, 0x77073096, 0xee0e612c, 0x990951ba, 0x076dc419, 0x706af48f, 50 | 0xe963a535, 0x9e6495a3, 0x0edb8832, 0x79dcb8a4, 0xe0d5e91e, 0x97d2d988, 51 | 0x09b64c2b, 0x7eb17cbd, 0xe7b82d07, 0x90bf1d91, 0x1db71064, 0x6ab020f2, 52 | 0xf3b97148, 0x84be41de, 0x1adad47d, 0x6ddde4eb, 0xf4d4b551, 0x83d385c7, 53 | 0x136c9856, 0x646ba8c0, 0xfd62f97a, 0x8a65c9ec, 0x14015c4f, 0x63066cd9, 54 | 0xfa0f3d63, 0x8d080df5, 0x3b6e20c8, 0x4c69105e, 0xd56041e4, 0xa2677172, 55 | 0x3c03e4d1, 0x4b04d447, 0xd20d85fd, 0xa50ab56b, 0x35b5a8fa, 0x42b2986c, 56 | 0xdbbbc9d6, 0xacbcf940, 0x32d86ce3, 0x45df5c75, 0xdcd60dcf, 0xabd13d59, 57 | 0x26d930ac, 0x51de003a, 0xc8d75180, 0xbfd06116, 0x21b4f4b5, 0x56b3c423, 58 | 0xcfba9599, 0xb8bda50f, 0x2802b89e, 0x5f058808, 0xc60cd9b2, 0xb10be924, 59 | 0x2f6f7c87, 0x58684c11, 0xc1611dab, 0xb6662d3d, 0x76dc4190, 0x01db7106, 60 | 0x98d220bc, 0xefd5102a, 0x71b18589, 0x06b6b51f, 0x9fbfe4a5, 0xe8b8d433, 61 | 0x7807c9a2, 0x0f00f934, 0x9609a88e, 0xe10e9818, 0x7f6a0dbb, 0x086d3d2d, 62 | 0x91646c97, 0xe6635c01, 0x6b6b51f4, 0x1c6c6162, 0x856530d8, 0xf262004e, 63 | 0x6c0695ed, 0x1b01a57b, 0x8208f4c1, 0xf50fc457, 0x65b0d9c6, 0x12b7e950, 64 | 0x8bbeb8ea, 0xfcb9887c, 0x62dd1ddf, 0x15da2d49, 0x8cd37cf3, 0xfbd44c65, 65 | 0x4db26158, 0x3ab551ce, 0xa3bc0074, 0xd4bb30e2, 0x4adfa541, 0x3dd895d7, 66 | 0xa4d1c46d, 0xd3d6f4fb, 0x4369e96a, 0x346ed9fc, 0xad678846, 0xda60b8d0, 67 | 0x44042d73, 0x33031de5, 0xaa0a4c5f, 0xdd0d7cc9, 0x5005713c, 0x270241aa, 68 | 0xbe0b1010, 0xc90c2086, 0x5768b525, 0x206f85b3, 0xb966d409, 0xce61e49f, 69 | 0x5edef90e, 0x29d9c998, 0xb0d09822, 0xc7d7a8b4, 0x59b33d17, 0x2eb40d81, 70 | 0xb7bd5c3b, 0xc0ba6cad, 0xedb88320, 0x9abfb3b6, 0x03b6e20c, 0x74b1d29a, 71 | 0xead54739, 0x9dd277af, 0x04db2615, 0x73dc1683, 0xe3630b12, 0x94643b84, 72 | 0x0d6d6a3e, 0x7a6a5aa8, 0xe40ecf0b, 0x9309ff9d, 0x0a00ae27, 0x7d079eb1, 73 | 0xf00f9344, 0x8708a3d2, 0x1e01f268, 0x6906c2fe, 0xf762575d, 0x806567cb, 74 | 0x196c3671, 0x6e6b06e7, 0xfed41b76, 0x89d32be0, 0x10da7a5a, 0x67dd4acc, 75 | 0xf9b9df6f, 0x8ebeeff9, 0x17b7be43, 0x60b08ed5, 0xd6d6a3e8, 0xa1d1937e, 76 | 0x38d8c2c4, 0x4fdff252, 0xd1bb67f1, 0xa6bc5767, 0x3fb506dd, 0x48b2364b, 77 | 0xd80d2bda, 0xaf0a1b4c, 0x36034af6, 0x41047a60, 0xdf60efc3, 0xa867df55, 78 | 0x316e8eef, 0x4669be79, 0xcb61b38c, 0xbc66831a, 0x256fd2a0, 0x5268e236, 79 | 0xcc0c7795, 0xbb0b4703, 0x220216b9, 0x5505262f, 0xc5ba3bbe, 0xb2bd0b28, 80 | 0x2bb45a92, 0x5cb36a04, 0xc2d7ffa7, 0xb5d0cf31, 0x2cd99e8b, 0x5bdeae1d, 81 | 0x9b64c2b0, 0xec63f226, 0x756aa39c, 0x026d930a, 0x9c0906a9, 0xeb0e363f, 82 | 0x72076785, 0x05005713, 0x95bf4a82, 0xe2b87a14, 0x7bb12bae, 0x0cb61b38, 83 | 0x92d28e9b, 0xe5d5be0d, 0x7cdcefb7, 0x0bdbdf21, 0x86d3d2d4, 0xf1d4e242, 84 | 0x68ddb3f8, 0x1fda836e, 0x81be16cd, 0xf6b9265b, 0x6fb077e1, 0x18b74777, 85 | 0x88085ae6, 0xff0f6a70, 0x66063bca, 0x11010b5c, 0x8f659eff, 0xf862ae69, 86 | 0x616bffd3, 0x166ccf45, 0xa00ae278, 0xd70dd2ee, 0x4e048354, 0x3903b3c2, 87 | 0xa7672661, 0xd06016f7, 0x4969474d, 0x3e6e77db, 0xaed16a4a, 0xd9d65adc, 88 | 0x40df0b66, 0x37d83bf0, 0xa9bcae53, 0xdebb9ec5, 0x47b2cf7f, 0x30b5ffe9, 89 | 0xbdbdf21c, 0xcabac28a, 0x53b39330, 0x24b4a3a6, 0xbad03605, 0xcdd70693, 90 | 0x54de5729, 0x23d967bf, 0xb3667a2e, 0xc4614ab8, 0x5d681b02, 0x2a6f2b94, 91 | 0xb40bbe37, 0xc30c8ea1, 0x5a05df1b, 0x2d02ef8d 92 | }; 93 | 94 | /* 95 | * A function that calculates the CRC-32 based on the table above is 96 | * given below for documentation purposes. An equivalent implementation 97 | * of this function that's actually used in the kernel can be found 98 | * in sys/libkern.h, where it can be inlined. 99 | */ 100 | 101 | uint32_t sparse_crc32(uint32_t crc_in, const void *buf, int size) 102 | { 103 | const uint8_t *p = buf; 104 | uint32_t crc; 105 | 106 | crc = crc_in ^ ~0U; 107 | while (size--) 108 | crc = crc32_tab[(crc ^ *p++) & 0xFF] ^ (crc >> 8); 109 | return crc ^ ~0U; 110 | } 111 | -------------------------------------------------------------------------------- /bin/src/simg2img/sparse_crc32.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2010 The Android Open Source Project 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | #ifndef _LIBSPARSE_SPARSE_CRC32_H_ 18 | #define _LIBSPARSE_SPARSE_CRC32_H_ 19 | 20 | #include 21 | 22 | #ifdef __cplusplus 23 | extern "C" { 24 | #endif 25 | 26 | uint32_t sparse_crc32(uint32_t crc, const void *buf, size_t size); 27 | 28 | #ifdef __cplusplus 29 | } 30 | #endif 31 | #endif 32 | -------------------------------------------------------------------------------- /bin/src/simg2img/sparse_defs.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2010 The Android Open Source Project 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | #ifndef _LIBSPARSE_SPARSE_DEFS_ 18 | #define _LIBSPARSE_SPARSE_DEFS_ 19 | 20 | #include 21 | #include 22 | 23 | #define __le64 u64 24 | #define __le32 u32 25 | #define __le16 u16 26 | 27 | #define __be64 u64 28 | #define __be32 u32 29 | #define __be16 u16 30 | 31 | #define __u64 u64 32 | #define __u32 u32 33 | #define __u16 u16 34 | #define __u8 u8 35 | 36 | typedef unsigned long long u64; 37 | typedef signed long long s64; 38 | typedef unsigned int u32; 39 | typedef unsigned short int u16; 40 | typedef unsigned char u8; 41 | 42 | #define DIV_ROUND_UP(x, y) (((x) + (y) - 1)/(y)) 43 | #define ALIGN(x, y) ((y) * DIV_ROUND_UP((x), (y))) 44 | #define ALIGN_DOWN(x, y) ((y) * ((x) / (y))) 45 | 46 | #define error(fmt, args...) do { fprintf(stderr, "error: %s: " fmt "\n", __func__, ## args); } while (0) 47 | #define error_errno(s, args...) error(s ": %s", ##args, strerror(errno)) 48 | 49 | #endif 50 | -------------------------------------------------------------------------------- /bin/src/simg2img/sparse_err.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2012 The Android Open Source Project 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | #include 18 | 19 | #include 20 | #include 21 | #include 22 | 23 | void sparse_default_print(const char *fmt, ...) 24 | { 25 | va_list argp; 26 | 27 | va_start(argp, fmt); 28 | vfprintf(stderr, fmt, argp); 29 | va_end(argp); 30 | } 31 | 32 | void (*sparse_print_error) (const char *fmt, ...) = sparse_default_print; 33 | void (*sparse_print_verbose) (const char *fmt, ...) = sparse_default_print; 34 | -------------------------------------------------------------------------------- /bin/src/simg2img/sparse_file.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2012 The Android Open Source Project 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | #ifndef _LIBSPARSE_SPARSE_FILE_H_ 18 | #define _LIBSPARSE_SPARSE_FILE_H_ 19 | 20 | #include 21 | 22 | struct sparse_file { 23 | unsigned int block_size; 24 | int64_t len; 25 | bool verbose; 26 | 27 | struct backed_block_list *backed_block_list; 28 | struct output_file *out; 29 | }; 30 | 31 | #endif /* _LIBSPARSE_SPARSE_FILE_H_ */ 32 | -------------------------------------------------------------------------------- /bin/src/simg2img/sparse_format.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2010 The Android Open Source Project 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | #ifndef _LIBSPARSE_SPARSE_FORMAT_H_ 18 | #define _LIBSPARSE_SPARSE_FORMAT_H_ 19 | #include "sparse_defs.h" 20 | 21 | typedef struct sparse_header { 22 | __le32 magic; /* 0xed26ff3a */ 23 | __le16 major_version; /* (0x1) - reject images with higher major versions */ 24 | __le16 minor_version; /* (0x0) - allow images with higer minor versions */ 25 | __le16 file_hdr_sz; /* 28 bytes for first revision of the file format */ 26 | __le16 chunk_hdr_sz; /* 12 bytes for first revision of the file format */ 27 | __le32 blk_sz; /* block size in bytes, must be a multiple of 4 (4096) */ 28 | __le32 total_blks; /* total blocks in the non-sparse output image */ 29 | __le32 total_chunks; /* total chunks in the sparse input image */ 30 | __le32 image_checksum; /* CRC32 checksum of the original data, counting "don't care" */ 31 | /* as 0. Standard 802.3 polynomial, use a Public Domain */ 32 | /* table implementation */ 33 | } sparse_header_t; 34 | 35 | #define SPARSE_HEADER_MAGIC 0xed26ff3a 36 | 37 | #define CHUNK_TYPE_RAW 0xCAC1 38 | #define CHUNK_TYPE_FILL 0xCAC2 39 | #define CHUNK_TYPE_DONT_CARE 0xCAC3 40 | #define CHUNK_TYPE_CRC32 0xCAC4 41 | 42 | typedef struct chunk_header { 43 | __le16 chunk_type; /* 0xCAC1 -> raw; 0xCAC2 -> fill; 0xCAC3 -> don't care */ 44 | __le16 reserved1; 45 | __le32 chunk_sz; /* in blocks in output image */ 46 | __le32 total_sz; /* in bytes of chunk input file including chunk header and data */ 47 | } chunk_header_t; 48 | 49 | /* Following a Raw or Fill or CRC32 chunk is data. 50 | * For a Raw chunk, it's the data in chunk_sz * blk_sz. 51 | * For a Fill chunk, it's 4 bytes of the fill data. 52 | * For a CRC32 chunk, it's 4 bytes of CRC32 53 | */ 54 | 55 | #endif 56 | -------------------------------------------------------------------------------- /bin/src/simg2img/sparse_read.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2012 The Android Open Source Project 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | #define _GNU_SOURCE 18 | #define _FILE_OFFSET_BITS 64 19 | #define _LARGEFILE64_SOURCE 1 20 | 21 | #include 22 | #include 23 | #include 24 | #include 25 | #include 26 | #include 27 | #include 28 | #include 29 | #include 30 | 31 | #include 32 | 33 | #include "defs.h" 34 | #include "output_file.h" 35 | #include "sparse_crc32.h" 36 | #include "sparse_file.h" 37 | #include "sparse_format.h" 38 | 39 | #if defined(__APPLE__) && defined(__MACH__) 40 | #define lseek64 lseek 41 | #define off64_t off_t 42 | #endif 43 | 44 | #define SPARSE_HEADER_MAJOR_VER 1 45 | #define SPARSE_HEADER_LEN (sizeof(sparse_header_t)) 46 | #define CHUNK_HEADER_LEN (sizeof(chunk_header_t)) 47 | 48 | #define COPY_BUF_SIZE (1024U*1024U) 49 | static char *copybuf; 50 | 51 | #define min(a, b) \ 52 | ({ typeof(a) _a = (a); typeof(b) _b = (b); (_a < _b) ? _a : _b; }) 53 | 54 | static void verbose_error(bool verbose, int err, const char *fmt, ...) 55 | { 56 | char *s = ""; 57 | char *at = ""; 58 | if (fmt) { 59 | va_list argp; 60 | int size; 61 | 62 | va_start(argp, fmt); 63 | size = vsnprintf(NULL, 0, fmt, argp); 64 | va_end(argp); 65 | 66 | if (size < 0) { 67 | return; 68 | } 69 | 70 | at = malloc(size + 1); 71 | if (at == NULL) { 72 | return; 73 | } 74 | 75 | va_start(argp, fmt); 76 | vsnprintf(at, size, fmt, argp); 77 | va_end(argp); 78 | at[size] = 0; 79 | s = " at "; 80 | } 81 | if (verbose) { 82 | #ifndef USE_MINGW 83 | if (err == -EOVERFLOW) { 84 | sparse_print_verbose("EOF while reading file%s%s\n", s, at); 85 | } else 86 | #endif 87 | if (err == -EINVAL) { 88 | sparse_print_verbose("Invalid sparse file format%s%s\n", s, at); 89 | } else if (err == -ENOMEM) { 90 | sparse_print_verbose("Failed allocation while reading file%s%s\n", s, at); 91 | } else { 92 | sparse_print_verbose("Unknown error %d%s%s\n", err, s, at); 93 | } 94 | } 95 | if (fmt) { 96 | free(at); 97 | } 98 | } 99 | 100 | static int process_raw_chunk(struct sparse_file *s, unsigned int chunk_size, 101 | int fd, int64_t offset, unsigned int blocks, unsigned int block, 102 | uint32_t * crc32) 103 | { 104 | int ret; 105 | int chunk; 106 | unsigned int len = blocks * s->block_size; 107 | 108 | if (chunk_size % s->block_size != 0) { 109 | return -EINVAL; 110 | } 111 | 112 | if (chunk_size / s->block_size != blocks) { 113 | return -EINVAL; 114 | } 115 | 116 | ret = sparse_file_add_fd(s, fd, offset, len, block); 117 | if (ret < 0) { 118 | return ret; 119 | } 120 | 121 | if (crc32) { 122 | while (len) { 123 | chunk = min(len, COPY_BUF_SIZE); 124 | ret = read_all(fd, copybuf, chunk); 125 | if (ret < 0) { 126 | return ret; 127 | } 128 | *crc32 = sparse_crc32(*crc32, copybuf, chunk); 129 | len -= chunk; 130 | } 131 | } else { 132 | lseek64(fd, len, SEEK_CUR); 133 | } 134 | 135 | return 0; 136 | } 137 | 138 | static int process_fill_chunk(struct sparse_file *s, unsigned int chunk_size, 139 | int fd, unsigned int blocks, unsigned int block, uint32_t * crc32) 140 | { 141 | int ret; 142 | int chunk; 143 | int64_t len = (int64_t) blocks * s->block_size; 144 | uint32_t fill_val; 145 | uint32_t *fillbuf; 146 | unsigned int i; 147 | 148 | if (chunk_size != sizeof(fill_val)) { 149 | return -EINVAL; 150 | } 151 | 152 | ret = read_all(fd, &fill_val, sizeof(fill_val)); 153 | if (ret < 0) { 154 | return ret; 155 | } 156 | 157 | ret = sparse_file_add_fill(s, fill_val, len, block); 158 | if (ret < 0) { 159 | return ret; 160 | } 161 | 162 | if (crc32) { 163 | /* Fill copy_buf with the fill value */ 164 | fillbuf = (uint32_t *) copybuf; 165 | for (i = 0; i < (COPY_BUF_SIZE / sizeof(fill_val)); i++) { 166 | fillbuf[i] = fill_val; 167 | } 168 | 169 | while (len) { 170 | chunk = min(len, COPY_BUF_SIZE); 171 | *crc32 = sparse_crc32(*crc32, copybuf, chunk); 172 | len -= chunk; 173 | } 174 | } 175 | 176 | return 0; 177 | } 178 | 179 | static int process_skip_chunk(struct sparse_file *s, unsigned int chunk_size, 180 | int fd __unused, unsigned int blocks, 181 | unsigned int block __unused, uint32_t * crc32) 182 | { 183 | if (chunk_size != 0) { 184 | return -EINVAL; 185 | } 186 | 187 | if (crc32) { 188 | int64_t len = (int64_t) blocks * s->block_size; 189 | memset(copybuf, 0, COPY_BUF_SIZE); 190 | 191 | while (len) { 192 | int chunk = min(len, COPY_BUF_SIZE); 193 | *crc32 = sparse_crc32(*crc32, copybuf, chunk); 194 | len -= chunk; 195 | } 196 | } 197 | 198 | return 0; 199 | } 200 | 201 | static int process_crc32_chunk(int fd, unsigned int chunk_size, uint32_t * crc32) 202 | { 203 | uint32_t file_crc32; 204 | int ret; 205 | 206 | if (chunk_size != sizeof(file_crc32)) { 207 | return -EINVAL; 208 | } 209 | 210 | ret = read_all(fd, &file_crc32, sizeof(file_crc32)); 211 | if (ret < 0) { 212 | return ret; 213 | } 214 | 215 | if (crc32 != NULL && file_crc32 != *crc32) { 216 | return -EINVAL; 217 | } 218 | 219 | return 0; 220 | } 221 | 222 | static int process_chunk(struct sparse_file *s, int fd, off64_t offset, 223 | unsigned int chunk_hdr_sz, chunk_header_t * chunk_header, 224 | unsigned int cur_block, uint32_t * crc_ptr) 225 | { 226 | int ret; 227 | unsigned int chunk_data_size; 228 | 229 | chunk_data_size = chunk_header->total_sz - chunk_hdr_sz; 230 | 231 | switch (chunk_header->chunk_type) { 232 | case CHUNK_TYPE_RAW: 233 | ret = process_raw_chunk(s, chunk_data_size, fd, offset, 234 | chunk_header->chunk_sz, cur_block, crc_ptr); 235 | if (ret < 0) { 236 | verbose_error(s->verbose, ret, "data block at %" PRId64, offset); 237 | return ret; 238 | } 239 | return chunk_header->chunk_sz; 240 | case CHUNK_TYPE_FILL: 241 | ret = process_fill_chunk(s, chunk_data_size, fd, 242 | chunk_header->chunk_sz, cur_block, crc_ptr); 243 | if (ret < 0) { 244 | verbose_error(s->verbose, ret, "fill block at %" PRId64, offset); 245 | return ret; 246 | } 247 | return chunk_header->chunk_sz; 248 | case CHUNK_TYPE_DONT_CARE: 249 | ret = process_skip_chunk(s, chunk_data_size, fd, 250 | chunk_header->chunk_sz, cur_block, crc_ptr); 251 | if (chunk_data_size != 0) { 252 | if (ret < 0) { 253 | verbose_error(s->verbose, ret, "skip block at %" PRId64, offset); 254 | return ret; 255 | } 256 | } 257 | return chunk_header->chunk_sz; 258 | case CHUNK_TYPE_CRC32: 259 | ret = process_crc32_chunk(fd, chunk_data_size, crc_ptr); 260 | if (ret < 0) { 261 | verbose_error(s->verbose, -EINVAL, "crc block at %" PRId64, offset); 262 | return ret; 263 | } 264 | return 0; 265 | default: 266 | verbose_error(s->verbose, -EINVAL, "unknown block %04X at %" PRId64, 267 | chunk_header->chunk_type, offset); 268 | } 269 | 270 | return 0; 271 | } 272 | 273 | static int sparse_file_read_sparse(struct sparse_file *s, int fd, bool crc) 274 | { 275 | int ret; 276 | unsigned int i; 277 | sparse_header_t sparse_header; 278 | chunk_header_t chunk_header; 279 | uint32_t crc32 = 0; 280 | uint32_t *crc_ptr = 0; 281 | unsigned int cur_block = 0; 282 | off64_t offset; 283 | 284 | if (!copybuf) { 285 | copybuf = malloc(COPY_BUF_SIZE); 286 | } 287 | 288 | if (!copybuf) { 289 | return -ENOMEM; 290 | } 291 | 292 | if (crc) { 293 | crc_ptr = &crc32; 294 | } 295 | 296 | ret = read_all(fd, &sparse_header, sizeof(sparse_header)); 297 | if (ret < 0) { 298 | return ret; 299 | } 300 | 301 | if (sparse_header.magic != SPARSE_HEADER_MAGIC) { 302 | return -EINVAL; 303 | } 304 | 305 | if (sparse_header.major_version != SPARSE_HEADER_MAJOR_VER) { 306 | return -EINVAL; 307 | } 308 | 309 | if (sparse_header.file_hdr_sz < SPARSE_HEADER_LEN) { 310 | return -EINVAL; 311 | } 312 | 313 | if (sparse_header.chunk_hdr_sz < sizeof(chunk_header)) { 314 | return -EINVAL; 315 | } 316 | 317 | if (sparse_header.file_hdr_sz > SPARSE_HEADER_LEN) { 318 | /* Skip the remaining bytes in a header that is longer than 319 | * we expected. 320 | */ 321 | lseek64(fd, sparse_header.file_hdr_sz - SPARSE_HEADER_LEN, SEEK_CUR); 322 | } 323 | 324 | for (i = 0; i < sparse_header.total_chunks; i++) { 325 | ret = read_all(fd, &chunk_header, sizeof(chunk_header)); 326 | if (ret < 0) { 327 | return ret; 328 | } 329 | 330 | if (sparse_header.chunk_hdr_sz > CHUNK_HEADER_LEN) { 331 | /* Skip the remaining bytes in a header that is longer than 332 | * we expected. 333 | */ 334 | lseek64(fd, sparse_header.chunk_hdr_sz - CHUNK_HEADER_LEN, SEEK_CUR); 335 | } 336 | 337 | offset = lseek64(fd, 0, SEEK_CUR); 338 | 339 | ret = process_chunk(s, fd, offset, sparse_header.chunk_hdr_sz, &chunk_header, 340 | cur_block, crc_ptr); 341 | if (ret < 0) { 342 | return ret; 343 | } 344 | 345 | cur_block += ret; 346 | } 347 | 348 | if (sparse_header.total_blks != cur_block) { 349 | return -EINVAL; 350 | } 351 | 352 | return 0; 353 | } 354 | 355 | static int sparse_file_read_normal(struct sparse_file *s, int fd) 356 | { 357 | int ret; 358 | uint32_t *buf = malloc(s->block_size); 359 | unsigned int block = 0; 360 | int64_t remain = s->len; 361 | int64_t offset = 0; 362 | unsigned int to_read; 363 | unsigned int i; 364 | bool sparse_block; 365 | 366 | if (!buf) { 367 | return -ENOMEM; 368 | } 369 | 370 | while (remain > 0) { 371 | to_read = min(remain, s->block_size); 372 | ret = read_all(fd, buf, to_read); 373 | if (ret < 0) { 374 | error("failed to read sparse file"); 375 | free(buf); 376 | return ret; 377 | } 378 | 379 | if (to_read == s->block_size) { 380 | sparse_block = true; 381 | for (i = 1; i < s->block_size / sizeof(uint32_t); i++) { 382 | if (buf[0] != buf[i]) { 383 | sparse_block = false; 384 | break; 385 | } 386 | } 387 | } else { 388 | sparse_block = false; 389 | } 390 | 391 | if (sparse_block) { 392 | /* TODO: add flag to use skip instead of fill for buf[0] == 0 */ 393 | sparse_file_add_fill(s, buf[0], to_read, block); 394 | } else { 395 | sparse_file_add_fd(s, fd, offset, to_read, block); 396 | } 397 | 398 | remain -= to_read; 399 | offset += to_read; 400 | block++; 401 | } 402 | 403 | free(buf); 404 | return 0; 405 | } 406 | 407 | int sparse_file_read(struct sparse_file *s, int fd, bool sparse, bool crc) 408 | { 409 | if (crc && !sparse) { 410 | return -EINVAL; 411 | } 412 | 413 | if (sparse) { 414 | return sparse_file_read_sparse(s, fd, crc); 415 | } else { 416 | return sparse_file_read_normal(s, fd); 417 | } 418 | } 419 | 420 | struct sparse_file *sparse_file_import(int fd, bool verbose, bool crc) 421 | { 422 | int ret; 423 | sparse_header_t sparse_header; 424 | int64_t len; 425 | struct sparse_file *s; 426 | 427 | ret = read_all(fd, &sparse_header, sizeof(sparse_header)); 428 | if (ret < 0) { 429 | verbose_error(verbose, ret, "header"); 430 | return NULL; 431 | } 432 | 433 | if (sparse_header.magic != SPARSE_HEADER_MAGIC) { 434 | verbose_error(verbose, -EINVAL, "header magic"); 435 | return NULL; 436 | } 437 | 438 | if (sparse_header.major_version != SPARSE_HEADER_MAJOR_VER) { 439 | verbose_error(verbose, -EINVAL, "header major version"); 440 | return NULL; 441 | } 442 | 443 | if (sparse_header.file_hdr_sz < SPARSE_HEADER_LEN) { 444 | return NULL; 445 | } 446 | 447 | if (sparse_header.chunk_hdr_sz < sizeof(chunk_header_t)) { 448 | return NULL; 449 | } 450 | 451 | len = (int64_t) sparse_header.total_blks * sparse_header.blk_sz; 452 | s = sparse_file_new(sparse_header.blk_sz, len); 453 | if (!s) { 454 | verbose_error(verbose, -EINVAL, NULL); 455 | return NULL; 456 | } 457 | 458 | ret = lseek64(fd, 0, SEEK_SET); 459 | if (ret < 0) { 460 | verbose_error(verbose, ret, "seeking"); 461 | sparse_file_destroy(s); 462 | return NULL; 463 | } 464 | 465 | s->verbose = verbose; 466 | 467 | ret = sparse_file_read(s, fd, true, crc); 468 | if (ret < 0) { 469 | sparse_file_destroy(s); 470 | return NULL; 471 | } 472 | 473 | return s; 474 | } 475 | 476 | struct sparse_file *sparse_file_import_auto(int fd, bool crc, bool verbose) 477 | { 478 | struct sparse_file *s; 479 | int64_t len; 480 | int ret; 481 | 482 | s = sparse_file_import(fd, verbose, crc); 483 | if (s) { 484 | return s; 485 | } 486 | 487 | len = lseek64(fd, 0, SEEK_END); 488 | if (len < 0) { 489 | return NULL; 490 | } 491 | 492 | lseek64(fd, 0, SEEK_SET); 493 | 494 | s = sparse_file_new(4096, len); 495 | if (!s) { 496 | return NULL; 497 | } 498 | 499 | ret = sparse_file_read_normal(s, fd); 500 | if (ret < 0) { 501 | sparse_file_destroy(s); 502 | return NULL; 503 | } 504 | 505 | return s; 506 | } 507 | -------------------------------------------------------------------------------- /bin/unmount: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | echo "Unmounting system image..." 4 | sync 5 | sudo umount output/system 6 | 7 | echo "Done" 8 | -------------------------------------------------------------------------------- /bin/unpack: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | if [ -e bin/simg2img ] 4 | then 5 | if [ $# -eq 1 ] 6 | then 7 | if [ -e $1 ] 8 | then 9 | echo "Cleaning up..." 10 | sudo umount output/system 11 | rm -rf output 12 | mkdir -p output/system 13 | mkdir -p output/image 14 | mkdir -p output/logo 15 | mkdir -p output/boot 16 | 17 | echo "Unpacking image $1..." 18 | bin/aml_image_v2_packer -d $1 output/image 19 | 20 | echo "Converting system.PARTITION to system.img..." 21 | bin/simg2img output/image/system.PARTITION output/image/system.img 22 | 23 | echo "Mounting system image..." 24 | sudo mount -t ext4 -o loop,rw output/image/system.img output/system 25 | 26 | echo "Unpacking logo..." 27 | bin/logo_img_packer -d output/image/logo.PARTITION output/logo 28 | 29 | echo "Unpacking boot..." 30 | cp output/image/boot.PARTITION output/boot/boot.img 31 | cd output/boot 32 | ../../bin/abootimg -x boot.img 33 | cd ../.. 34 | rm -f output/boot/boot.img 35 | 36 | echo "Done" 37 | else 38 | echo "File not found: $1" 39 | fi 40 | else 41 | echo "Usage: unpack [input image]" 42 | fi 43 | else 44 | echo "Please run the build script before using this tool" 45 | fi 46 | --------------------------------------------------------------------------------