pax_global_header00006660000000000000000000000064116076404300014513gustar00rootroot0000000000000052 comment=2f2daded1da393bf58db4ccf5aa2fc82c01b4016 abootimg-0.6/000077500000000000000000000000001160764043000131615ustar00rootroot00000000000000abootimg-0.6/.gitignore000066400000000000000000000000351160764043000151470ustar00rootroot00000000000000abootimg version.h *.o *.swp abootimg-0.6/Changelog000066400000000000000000000012021160764043000147660ustar00rootroot000000000000002011/07/14 - v0.6 * fixes block device support, broken in v0.5 * fixes crash if ramdisk is bigger than kernel * version.h no longer relies on git * build fix on GNU Hurd 2011/05/18 - v0.5 * add support for freebsd, cygwin, and osx * explains why sanity check has failed * print version number in -help * pack-initrd -f support 2011/02/17 - v0.4 * fix when page_size is null 2010/12/08 - v0.3 * fix when updating kernel without ramdisk 2010/11/28 - v0.2 * fix when updating ramdisk without the kernel * fix --create on block device * added pack-initrd and unpack-initrd helper tools 2010/11/25 - v0.1 Initial release abootimg-0.6/LICENSE000066400000000000000000000432541160764043000141760ustar00rootroot00000000000000 GNU GENERAL PUBLIC LICENSE Version 2, June 1991 Copyright (C) 1989, 1991 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA Everyone is permitted to copy and distribute verbatim copies of this license document, but changing it is not allowed. Preamble The licenses for most software are designed to take away your freedom to share and change it. By contrast, the GNU General Public License is intended to guarantee your freedom to share and change free software--to make sure the software is free for all its users. This General Public License applies to most of the Free Software Foundation's software and to any other program whose authors commit to using it. (Some other Free Software Foundation software is covered by the GNU Lesser General Public License instead.) You can apply it to your programs, too. When we speak of free software, we are referring to freedom, not price. Our General Public Licenses are designed to make sure that you have the freedom to distribute copies of free software (and charge for this service if you wish), that you receive source code or can get it if you want it, that you can change the software or use pieces of it in new free programs; and that you know you can do these things. To protect your rights, we need to make restrictions that forbid anyone to deny you these rights or to ask you to surrender the rights. These restrictions translate to certain responsibilities for you if you distribute copies of the software, or if you modify it. For example, if you distribute copies of such a program, whether gratis or for a fee, you must give the recipients all the rights that you have. You must make sure that they, too, receive or can get the source code. And you must show them these terms so they know their rights. We protect your rights with two steps: (1) copyright the software, and (2) offer you this license which gives you legal permission to copy, distribute and/or modify the software. Also, for each author's protection and ours, we want to make certain that everyone understands that there is no warranty for this free software. If the software is modified by someone else and passed on, we want its recipients to know that what they have is not the original, so that any problems introduced by others will not reflect on the original authors' reputations. Finally, any free program is threatened constantly by software patents. We wish to avoid the danger that redistributors of a free program will individually obtain patent licenses, in effect making the program proprietary. To prevent this, we have made it clear that any patent must be licensed for everyone's free use or not licensed at all. The precise terms and conditions for copying, distribution and modification follow. GNU GENERAL PUBLIC LICENSE TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION 0. This License applies to any program or other work which contains a notice placed by the copyright holder saying it may be distributed under the terms of this General Public License. The "Program", below, refers to any such program or work, and a "work based on the Program" means either the Program or any derivative work under copyright law: that is to say, a work containing the Program or a portion of it, either verbatim or with modifications and/or translated into another language. (Hereinafter, translation is included without limitation in the term "modification".) Each licensee is addressed as "you". Activities other than copying, distribution and modification are not covered by this License; they are outside its scope. The act of running the Program is not restricted, and the output from the Program is covered only if its contents constitute a work based on the Program (independent of having been made by running the Program). Whether that is true depends on what the Program does. 1. You may copy and distribute verbatim copies of the Program's source code as you receive it, in any medium, provided that you conspicuously and appropriately publish on each copy an appropriate copyright notice and disclaimer of warranty; keep intact all the notices that refer to this License and to the absence of any warranty; and give any other recipients of the Program a copy of this License along with the Program. You may charge a fee for the physical act of transferring a copy, and you may at your option offer warranty protection in exchange for a fee. 2. You may modify your copy or copies of the Program or any portion of it, thus forming a work based on the Program, and copy and distribute such modifications or work under the terms of Section 1 above, provided that you also meet all of these conditions: a) You must cause the modified files to carry prominent notices stating that you changed the files and the date of any change. b) You must cause any work that you distribute or publish, that in whole or in part contains or is derived from the Program or any part thereof, to be licensed as a whole at no charge to all third parties under the terms of this License. c) If the modified program normally reads commands interactively when run, you must cause it, when started running for such interactive use in the most ordinary way, to print or display an announcement including an appropriate copyright notice and a notice that there is no warranty (or else, saying that you provide a warranty) and that users may redistribute the program under these conditions, and telling the user how to view a copy of this License. (Exception: if the Program itself is interactive but does not normally print such an announcement, your work based on the Program is not required to print an announcement.) These requirements apply to the modified work as a whole. If identifiable sections of that work are not derived from the Program, and can be reasonably considered independent and separate works in themselves, then this License, and its terms, do not apply to those sections when you distribute them as separate works. But when you distribute the same sections as part of a whole which is a work based on the Program, the distribution of the whole must be on the terms of this License, whose permissions for other licensees extend to the entire whole, and thus to each and every part regardless of who wrote it. Thus, it is not the intent of this section to claim rights or contest your rights to work written entirely by you; rather, the intent is to exercise the right to control the distribution of derivative or collective works based on the Program. In addition, mere aggregation of another work not based on the Program with the Program (or with a work based on the Program) on a volume of a storage or distribution medium does not bring the other work under the scope of this License. 3. You may copy and distribute the Program (or a work based on it, under Section 2) in object code or executable form under the terms of Sections 1 and 2 above provided that you also do one of the following: a) Accompany it with the complete corresponding machine-readable source code, which must be distributed under the terms of Sections 1 and 2 above on a medium customarily used for software interchange; or, b) Accompany it with a written offer, valid for at least three years, to give any third party, for a charge no more than your cost of physically performing source distribution, a complete machine-readable copy of the corresponding source code, to be distributed under the terms of Sections 1 and 2 above on a medium customarily used for software interchange; or, c) Accompany it with the information you received as to the offer to distribute corresponding source code. (This alternative is allowed only for noncommercial distribution and only if you received the program in object code or executable form with such an offer, in accord with Subsection b above.) The source code for a work means the preferred form of the work for making modifications to it. For an executable work, complete source code means all the source code for all modules it contains, plus any associated interface definition files, plus the scripts used to control compilation and installation of the executable. However, as a special exception, the source code distributed need not include anything that is normally distributed (in either source or binary form) with the major components (compiler, kernel, and so on) of the operating system on which the executable runs, unless that component itself accompanies the executable. If distribution of executable or object code is made by offering access to copy from a designated place, then offering equivalent access to copy the source code from the same place counts as distribution of the source code, even though third parties are not compelled to copy the source along with the object code. 4. You may not copy, modify, sublicense, or distribute the Program except as expressly provided under this License. Any attempt otherwise to copy, modify, sublicense or distribute the Program is void, and will automatically terminate your rights under this License. However, parties who have received copies, or rights, from you under this License will not have their licenses terminated so long as such parties remain in full compliance. 5. You are not required to accept this License, since you have not signed it. However, nothing else grants you permission to modify or distribute the Program or its derivative works. These actions are prohibited by law if you do not accept this License. Therefore, by modifying or distributing the Program (or any work based on the Program), you indicate your acceptance of this License to do so, and all its terms and conditions for copying, distributing or modifying the Program or works based on it. 6. Each time you redistribute the Program (or any work based on the Program), the recipient automatically receives a license from the original licensor to copy, distribute or modify the Program subject to these terms and conditions. You may not impose any further restrictions on the recipients' exercise of the rights granted herein. You are not responsible for enforcing compliance by third parties to this License. 7. If, as a consequence of a court judgment or allegation of patent infringement or for any other reason (not limited to patent issues), conditions are imposed on you (whether by court order, agreement or otherwise) that contradict the conditions of this License, they do not excuse you from the conditions of this License. If you cannot distribute so as to satisfy simultaneously your obligations under this License and any other pertinent obligations, then as a consequence you may not distribute the Program at all. For example, if a patent license would not permit royalty-free redistribution of the Program by all those who receive copies directly or indirectly through you, then the only way you could satisfy both it and this License would be to refrain entirely from distribution of the Program. If any portion of this section is held invalid or unenforceable under any particular circumstance, the balance of the section is intended to apply and the section as a whole is intended to apply in other circumstances. It is not the purpose of this section to induce you to infringe any patents or other property right claims or to contest validity of any such claims; this section has the sole purpose of protecting the integrity of the free software distribution system, which is implemented by public license practices. Many people have made generous contributions to the wide range of software distributed through that system in reliance on consistent application of that system; it is up to the author/donor to decide if he or she is willing to distribute software through any other system and a licensee cannot impose that choice. This section is intended to make thoroughly clear what is believed to be a consequence of the rest of this License. 8. If the distribution and/or use of the Program is restricted in certain countries either by patents or by copyrighted interfaces, the original copyright holder who places the Program under this License may add an explicit geographical distribution limitation excluding those countries, so that distribution is permitted only in or among countries not thus excluded. In such case, this License incorporates the limitation as if written in the body of this License. 9. The Free Software Foundation may publish revised and/or new versions of the General Public License from time to time. Such new versions will be similar in spirit to the present version, but may differ in detail to address new problems or concerns. Each version is given a distinguishing version number. If the Program specifies a version number of this License which applies to it and "any later version", you have the option of following the terms and conditions either of that version or of any later version published by the Free Software Foundation. If the Program does not specify a version number of this License, you may choose any version ever published by the Free Software Foundation. 10. If you wish to incorporate parts of the Program into other free programs whose distribution conditions are different, write to the author to ask for permission. For software which is copyrighted by the Free Software Foundation, write to the Free Software Foundation; we sometimes make exceptions for this. Our decision will be guided by the two goals of preserving the free status of all derivatives of our free software and of promoting the sharing and reuse of software generally. NO WARRANTY 11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. END OF TERMS AND CONDITIONS How to Apply These Terms to Your New Programs If you develop a new program, and you want it to be of the greatest possible use to the public, the best way to achieve this is to make it free software which everyone can redistribute and change under these terms. To do so, attach the following notices to the program. It is safest to attach them to the start of each source file to most effectively convey the exclusion of warranty; and each file should have at least the "copyright" line and a pointer to where the full notice is found. Copyright (C) This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. Also add information on how to contact you by electronic and paper mail. If the program is interactive, make it output a short notice like this when it starts in an interactive mode: Gnomovision version 69, Copyright (C) year name of author Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'. This is free software, and you are welcome to redistribute it under certain conditions; type `show c' for details. The hypothetical commands `show w' and `show c' should show the appropriate parts of the General Public License. Of course, the commands you use may be called something other than `show w' and `show c'; they could even be mouse-clicks or menu items--whatever suits your program. You should also get your employer (if you work as a programmer) or your school, if any, to sign a "copyright disclaimer" for the program, if necessary. Here is a sample; alter the names: Yoyodyne, Inc., hereby disclaims all copyright interest in the program `Gnomovision' (which makes passes at compilers) written by James Hacker. , 1 April 1989 Ty Coon, President of Vice This General Public License does not permit incorporating your program into proprietary programs. If your program is a subroutine library, you may consider it more useful to permit linking proprietary applications with the library. If this is what you want to do, use the GNU Lesser General Public License instead of this License. abootimg-0.6/Makefile000066400000000000000000000005701160764043000146230ustar00rootroot00000000000000 CPPFLAGS=-DHAS_BLKID CFLAGS=-O3 -Wall LDLIBS=-lblkid all: abootimg version.h: if [ ! -f version.h ]; then \ if [ -d .git ]; then \ echo '#define VERSION_STR "$(shell git describe --tags --abbrev=0)"' > version.h; \ else \ echo '#define VERSION_STR ""' > version.h; \ fi \ fi abootimg.o: bootimg.h version.h clean: rm -f abootimg *.o version.h .PHONY: clean all abootimg-0.6/README000066400000000000000000000177201160764043000140500ustar00rootroot00000000000000 abootimg - manipulate Android Boot Images. ------------------------------------------ (c) 2010 Gilles Grandou * Android Boot Images --------------------- It a special partition format defined by the Android Open Source Porject. See bootimg.h in the source tree for more information about the structure. It's used by Android Bootloaders to boot the OS. Boot images mainly convey: - a kernel image - a ramdisk image - optionaly, a 2nd stage bootloader - the cmdline passed to the kernel when booting. The official tool used to create boot images is part of the Android Project, available here: http://android.git.kernel.org/?p=platform/system/core.git;a=tree;f=mkbootimg abootimg can work directly on block devices, or, the safest way, on a file image. File images can be read/written with dd: $ dd if=/dev/mmcblk0p2 of=boot.img $ dd if=boot.img of=/dev/mmcblk0p2 You obviously need to have right access to the block device (using su, sudo, ...). Android Boot Image contains an 32 bytes Id. The specification does not actually mandates any specific implementation of this Id (it can be a timestamp, a CRC checksum, a SHA hash, ...). Bootloader appears to do nothing of this Id, it's solely here for tracking purpose. Currently abootimg does nothing with it, it's never touched/modified. * Building abootimg ------------------- On a linux system, it's simply as: $ make blkid library is needed to perform some sanity checks when writing boot image directly on a block device (to avoid writing a valid existing filesystem). * Looking at an Android Boot Image ---------------------------------- Basic Information can be extracted from a boot image, using: $ abootimg -i Here is an example: $ ./abootimg -i boot.img Android Boot Image Info: * file name = boot.img * image size = 8388608 bytes (8.00 MB) page size = 2048 bytes * Boot Name = "" * kernel size = 3002744 bytes (2.86 MB) ramdisk size = 1639626 bytes (1.56 MB) * load addresses: kernel: 0x10008000 ramdisk: 0x11000000 tags: 0x10000100 * 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 * id = 0x07571070 0x13950a6a 0x185c996f 0x9ab7b64d 0xcccd09bd 0x00000000 0x00000000 0x00000000 * Extracting elements from an Android Boot Image ------------------------------------------------ All parts of boot image can be extracted with: $ abootimg -x [ [ [ []]]] Parts name are optional. Default ones are used if none are given: * bootimg.cfg for the configuration file * zImage for the Kernel image * initrd.img for the Ramdisk * stage2.img for the Second Stage image Here is an example: $ abootimg -x boot.img writing boot image config in bootimg.cfg extracting kernel in zImage extracting ramdisk in initrd.img * Boot Configuration file ------------------------- It's an editable ascii file which is basically a dump of the header content, to be able to rebuild a compatible image later. Each entry takes one line and is in the form of: = You can put any number of spaces/tab before/after the = is evalued starting from the fisrt non space character following the = until the end of line Numerical values can be given in decimal (12345) or hexadecimal (0x1234abcd). Known configuration entries are: * bootsize Indicate the size of the boot image to produce * pagesize All sizes have to be a multiple of the page size. Standard page size is 2048 bytes. I don't know if other page size are supported by Android bootloader * kerneladdr, ramdiskaddr, secondaddr, tagsaddr Address in RAM used to load the kernel, ramdisk, 2nd stage bootloader, and tags table. * name name given to the boot image. not really used by bootloader, but it's can be usefull to keep track of what the image actually contains * cmdline contains the command line passed to the kernel when booting * Updating an existing Android Boot Image ----------------------------------------- An existing valid Boot Image can be updated with: $ abootimg -u [-c "param=value"] [-f ] [-k ] [-r ] [-s ] Any part of the image can be individully updated. As an example: $ abootimg -u boot.img -k zImage.new will update the kernel $ abootimg -u boot.img -r initrd.new.img will update the image $ abootimg -u boot.img -k zImage.new -r initrd.new.img will update both Image configuration can be updated either by giving a configuration file (-f option) or by giving individual config entries on the command line (-c). Severial config entries (-c) can be given on comamd line. As an example: $ abootimg -u boot.img -f bootimg.new.cfg will update the configuration without touching the kernel nor the ramdisk $ abootimg -u boot.img -c "cmdline = mem=448M@0M nvmem=64M@448M vmalloc=320M \ video=tegrafb console=tty0 usbcore.old_scheme_first=1 quiet splash elevator=noop \ tegraboot=sdmmc tegrapart=recovery:700:a00:800,boot:1100:1000:800,mbr:2100:200:800,\ system:2300:25800:800,cache:27b00:32000:800,misc:59b00:400:800,userdata:5a000:9a600:800" update the boot conmand line $ abootimg -u boot.img -c "bootsize=0x500000" update the boot image size to 5MB. It's usefull if you want to shrink an existing image to make it fit in another smaller boot partition. (On Toshiba AC100, it allows you to take a part06.img and transform it to fit inside part05) The original boot image has to be valid, otherwise abootimg will refuse to update it. * Creating a new Android Boot Image from scratch ------------------------------------------------ A new boot image can be create with: $ abootimg --create [-c "param=value"] [-f ] -k -r [-s ] Parameters are the same than above for update. The only difference is that kernel and ramdisk are mandatory. * Working directly of Block Devices ----------------------------------- Instead of manipulating Boot Image regular file, you can work directly on boot block device. Note that, on AC100, the current kernel needs to be patched in order to have direct access to boot partitions (partitions 5 and 6). Some examples: $ sudo abootimg -i /dev/mmcblk0p2 read the current boot partition $ sudo abootimg -u /dev/mmcblk0p2 -k arch/arm/boot/zImage update the boot partition with the kernel you have just built $ sudo abootimg -u /dev/mmcblk0p2 -c "cmdline=..." update the boot partition with a new boot cmdline $ sudo abootimg --create /dev/mmcblk0p2 -f boot.cfg -k zImage -r initrd.img overwrite the boot partition (which can be damaged) with a brand new image If abootimg has to write to a block device (-u and --create), some sanity check are performed: * you oviously need read/write access to the block device (so use su, sudo, ...) * the actual partition must not be identified as containing a valid filesystem as recognised by blkid library. Specifically, it cannot contains a ext2/3 filesystem (this avoids you to ovewrite your root filesystem by mistake) * the updated/created boot image has to be same size than the block device you try to write on. * in case of update, the current boot partition has to contain a valid Android Boot Image. Failing any of these tests will abort the operation on block device. It's by definition more risky to manipulate block device, as a bad manipulation can as bad manipulation can make your system unbootable if you don't fix it before the next reboot. On the other hand, manipulating the block device allows abootimg to prevent most of the stupid mistakes which can be made when writing boot image with dd (overwriting another filesystem, writing a boot image bigger than the partition, writing the wrong file or an invalid partition, ...) abootimg-0.6/abootimg.c000066400000000000000000000565641160764043000151460ustar00rootroot00000000000000/* abootimg - Manipulate (read, modify, create) Android Boot Images * Copyright (c) 2010-2011 Gilles Grandou * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation; either version 2 * of the License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ #include #include #include #include #include #include #include #include #ifdef __linux__ #include #include /* BLKGETSIZE64 */ #endif #ifdef __CYGWIN__ #include #include /* BLKGETSIZE64 */ #endif #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) #include /* DIOCGMEDIASIZE */ #include #endif #if defined(__APPLE__) # include /* DKIOCGETBLOCKCOUNT */ #endif #ifdef HAS_BLKID #include #endif #include "version.h" #include "bootimg.h" enum command { none, help, info, extract, update, create }; typedef struct { unsigned size; int is_blkdev; char* fname; char* config_fname; char* kernel_fname; char* ramdisk_fname; char* second_fname; FILE* stream; boot_img_hdr header; char* kernel; char* ramdisk; char* second; } t_abootimg; #define MAX_CONF_LEN 4096 char config_args[MAX_CONF_LEN] = ""; void abort_perror(char* str) { perror(str); exit(errno); } void abort_printf(char *fmt, ...) { va_list args; va_start(args, fmt); vfprintf(stderr, fmt, args); va_end(args); fprintf(stderr, "\n"); exit(1); } int blkgetsize(int fd, unsigned long long *pbsize) { # if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) return ioctl(fd, DIOCGMEDIASIZE, pbsize); # elif defined(__APPLE__) return ioctl(fd, DKIOCGETBLOCKCOUNT, pbsize); # elif defined(__NetBSD__) // does a suitable ioctl exist? // return (ioctl(fd, DIOCGDINFO, &label) == -1); return 1; # elif defined(__linux__) || defined(__CYGWIN__) return ioctl(fd, BLKGETSIZE64, pbsize); # elif defined(__GNU__) // does a suitable ioctl for HURD exist? return 1; # else return 1; # endif } void print_usage(void) { printf ( " abootimg - manipulate Android Boot Images.\n" " (c) 2010-2011 Gilles Grandou \n" " " VERSION_STR "\n" "\n" " abootimg [-h]\n" "\n" " print usage\n" "\n" " abootimg -i \n" "\n" " print boot image information\n" "\n" " abootimg -x [ [ [ []]]]\n" "\n" " extract objects from boot image:\n" " - config file (default name bootimg.cfg)\n" " - kernel image (default name zImage)\n" " - ramdisk image (default name initrd.img)\n" " - second stage image (default name stage2.img)\n" "\n" " abootimg -u [-c \"param=value\"] [-f ] [-k ] [-r ] [-s ]\n" "\n" " update a current boot image with objects given in command line\n" " - header informations given in arguments (several can be provided)\n" " - header informations given in config file\n" " - kernel image\n" " - ramdisk image\n" " - second stage image\n" "\n" " bootimg has to be valid Android Boot Image, or the update will abort.\n" "\n" " abootimg --create [-c \"param=value\"] [-f ] -k -r [-s ]\n" "\n" " create a new image from scratch.\n" " if the boot image file is a block device, sanity check will be performed to avoid overwriting a existing\n" " filesystem.\n" "\n" " argurments are the same than for -u.\n" " kernel and ramdisk are mandatory.\n" "\n" ); } enum command parse_args(int argc, char** argv, t_abootimg* img) { enum command cmd = none; int i; if (argc<2) return none; if (!strcmp(argv[1], "-h")) { return help; } else if (!strcmp(argv[1], "-i")) { cmd=info; } else if (!strcmp(argv[1], "-x")) { cmd=extract; } else if (!strcmp(argv[1], "-u")) { cmd=update; } else if (!strcmp(argv[1], "--create")) { cmd=create; } else return none; switch(cmd) { case info: if (argc != 3) return none; img->fname = argv[2]; break; case extract: if ((argc < 3) || (argc > 7)) return none; img->fname = argv[2]; if (argc >= 4) img->config_fname = argv[3]; if (argc >= 5) img->kernel_fname = argv[4]; if (argc >= 6) img->ramdisk_fname = argv[5]; if (argc >= 7) img->second_fname = argv[6]; break; case update: case create: if (argc < 3) return none; img->fname = argv[2]; img->config_fname = NULL; img->kernel_fname = NULL; img->ramdisk_fname = NULL; img->second_fname = NULL; for(i=3; i= argc) return none; unsigned len = strlen(argv[i]); if (strlen(config_args)+len+1 >= MAX_CONF_LEN) abort_printf("too many config parameters.\n"); strcat(config_args, argv[i]); strcat(config_args, "\n"); } else if (!strcmp(argv[i], "-f")) { if (++i >= argc) return none; img->config_fname = argv[i]; } else if (!strcmp(argv[i], "-k")) { if (++i >= argc) return none; img->kernel_fname = argv[i]; } else if (!strcmp(argv[i], "-r")) { if (++i >= argc) return none; img->ramdisk_fname = argv[i]; } else if (!strcmp(argv[i], "-s")) { if (++i >= argc) return none; img->second_fname = argv[i]; } else return none; } break; } return cmd; } int check_boot_img_header(t_abootimg* img) { if (strncmp(img->header.magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) { fprintf(stderr, "%s: no Android Magic Value\n", img->fname); return 1; } if (!(img->header.kernel_size)) { fprintf(stderr, "%s: kernel size is null\n", img->fname); return 1; } if (!(img->header.ramdisk_size)) { fprintf(stderr, "%s: ramdisk size is null\n", img->fname); return 1; } unsigned page_size = img->header.page_size; if (!page_size) { fprintf(stderr, "%s: Image page size is null\n", img->fname); return 1; } unsigned n = (img->header.kernel_size + page_size - 1) / page_size; unsigned m = (img->header.ramdisk_size + page_size - 1) / page_size; unsigned o = (img->header.second_size + page_size - 1) / page_size; unsigned total_size = (1+n+m+o)*page_size; if (total_size > img->size) { fprintf(stderr, "%s: sizes mismatches in boot image\n", img->fname); return 1; } return 0; } void check_if_block_device(t_abootimg* img) { struct stat st; if (stat(img->fname, &st)) if (errno != ENOENT) { printf("errno=%d\n", errno); abort_perror(img->fname); } #ifdef HAS_BLKID if (S_ISBLK(st.st_mode)) { img->is_blkdev = 1; char* type = blkid_get_tag_value(NULL, "TYPE", img->fname); if (type) abort_printf("%s: refuse to write on a valid partition type (%s)\n", img->fname, type); int fd = open(img->fname, O_RDONLY); if (fd == -1) abort_perror(img->fname); unsigned long long bsize = 0; if (blkgetsize(fd, &bsize)) abort_perror(img->fname); img->size = bsize; close(fd); } #endif } void open_bootimg(t_abootimg* img, char* mode) { img->stream = fopen(img->fname, mode); if (!img->stream) abort_perror(img->fname); } void read_header(t_abootimg* img) { size_t rb = fread(&img->header, sizeof(boot_img_hdr), 1, img->stream); if ((rb!=1) || ferror(img->stream)) abort_perror(img->fname); else if (feof(img->stream)) abort_printf("%s: cannot read image header\n", img->fname); struct stat s; int fd = fileno(img->stream); if (fstat(fd, &s)) abort_perror(img->fname); if (S_ISBLK(s.st_mode)) { unsigned long long bsize = 0; if (blkgetsize(fd, &bsize)) abort_perror(img->fname); img->size = bsize; img->is_blkdev = 1; } else { img->size = s.st_size; img->is_blkdev = 0; } if (check_boot_img_header(img)) abort_printf("%s: not a valid Android Boot Image.\n", img->fname); } void update_header_entry(t_abootimg* img, char* cmd) { char *p; char *token; char *endtoken; char *value; p = strchr(cmd, '\n'); if (p) *p = '\0'; p = cmd; p += strspn(p, " \t"); token = p; p += strcspn(p, " =\t"); endtoken = p; p += strspn(p, " \t"); if (*p++ != '=') goto err; p += strspn(p, " \t"); value = p; *endtoken = '\0'; unsigned valuenum = strtoul(value, NULL, 0); if (!strcmp(token, "cmdline")) { unsigned len = strlen(value); if (len >= BOOT_ARGS_SIZE) abort_printf("cmdline length (%d) is too long (max %d)", len, BOOT_ARGS_SIZE-1); memset(img->header.cmdline, 0, BOOT_ARGS_SIZE); strcpy(img->header.cmdline, value); } else if (!strncmp(token, "name", 4)) { strncpy(img->header.name, value, BOOT_NAME_SIZE); img->header.name[BOOT_NAME_SIZE-1] = '\0'; } else if (!strncmp(token, "bootsize", 8)) { if (img->is_blkdev && (img->size != valuenum)) abort_printf("%s: cannot change Boot Image size for a block device\n", img->fname); img->size = valuenum; } else if (!strncmp(token, "pagesize", 8)) { img->header.page_size = valuenum; } else if (!strncmp(token, "kerneladdr", 10)) { img->header.kernel_addr = valuenum; } else if (!strncmp(token, "ramdiskaddr", 11)) { img->header.ramdisk_addr = valuenum; } else if (!strncmp(token, "secondaddr", 10)) { img->header.second_addr = valuenum; } else if (!strncmp(token, "tagsaddr", 8)) { img->header.tags_addr = valuenum; } else goto err; return; err: abort_printf("%s: bad config entry\n", token); } void update_header(t_abootimg* img) { if (img->config_fname) { FILE* config_file = fopen(img->config_fname, "r"); if (!config_file) abort_perror(img->config_fname); printf("reading config file %s\n", img->config_fname); char* line = NULL; size_t len = 0; int read; while ((read = getline(&line, &len, config_file)) != -1) { update_header_entry(img, line); free(line); line = NULL; } if (ferror(config_file)) abort_perror(img->config_fname); } unsigned len = strlen(config_args); if (len) { FILE* config_file = fmemopen(config_args, len, "r"); if (!config_file) abort_perror("-c args"); printf("reading config args\n"); char* line = NULL; size_t len = 0; int read; while ((read = getline(&line, &len, config_file)) != -1) { update_header_entry(img, line); free(line); line = NULL; } if (ferror(config_file)) abort_perror("-c args"); } } void update_images(t_abootimg *img) { unsigned page_size = img->header.page_size; unsigned ksize = img->header.kernel_size; unsigned rsize = img->header.ramdisk_size; unsigned ssize = img->header.second_size; if (!page_size) abort_printf("%s: Image page size is null\n", img->fname); unsigned n = (ksize + page_size - 1) / page_size; unsigned m = (rsize + page_size - 1) / page_size; unsigned o = (ssize + page_size - 1) / page_size; unsigned roffset = (1+n)*page_size; unsigned soffset = (1+n+m)*page_size; if (img->kernel_fname) { printf("reading kernel from %s\n", img->kernel_fname); FILE* stream = fopen(img->kernel_fname, "r"); if (!stream) abort_perror(img->kernel_fname); struct stat st; if (fstat(fileno(stream), &st)) abort_perror(img->kernel_fname); ksize = st.st_size; char* k = malloc(ksize); if (!k) abort_perror(""); size_t rb = fread(k, ksize, 1, stream); if ((rb!=1) || ferror(stream)) abort_perror(img->kernel_fname); else if (feof(stream)) abort_printf("%s: cannot read kernel\n", img->kernel_fname); fclose(stream); img->header.kernel_size = ksize; img->kernel = k; } if (img->ramdisk_fname) { printf("reading ramdisk from %s\n", img->ramdisk_fname); FILE* stream = fopen(img->ramdisk_fname, "r"); if (!stream) abort_perror(img->ramdisk_fname); struct stat st; if (fstat(fileno(stream), &st)) abort_perror(img->ramdisk_fname); rsize = st.st_size; char* r = malloc(rsize); if (!r) abort_perror(""); size_t rb = fread(r, rsize, 1, stream); if ((rb!=1) || ferror(stream)) abort_perror(img->ramdisk_fname); else if (feof(stream)) abort_printf("%s: cannot read ramdisk\n", img->ramdisk_fname); fclose(stream); img->header.ramdisk_size = rsize; img->ramdisk = r; } else if (img->kernel) { // if kernel is updated, copy the ramdisk from original image char* r = malloc(rsize); if (!r) abort_perror(""); if (fseek(img->stream, roffset, SEEK_SET)) abort_perror(img->fname); size_t rb = fread(r, rsize, 1, img->stream); if ((rb!=1) || ferror(img->stream)) abort_perror(img->fname); else if (feof(img->stream)) abort_printf("%s: cannot read ramdisk\n", img->fname); img->ramdisk = r; } if (img->second_fname) { printf("reading second stage from %s\n", img->second_fname); FILE* stream = fopen(img->second_fname, "r"); if (!stream) abort_perror(img->second_fname); struct stat st; if (fstat(fileno(stream), &st)) abort_perror(img->second_fname); ssize = st.st_size; char* s = malloc(ssize); if (!s) abort_perror(""); size_t rb = fread(s, ssize, 1, stream); if ((rb!=1) || ferror(stream)) abort_perror(img->second_fname); else if (feof(stream)) abort_printf("%s: cannot read second stage\n", img->second_fname); fclose(stream); img->header.second_size = ssize; img->second = s; } else if (img->ramdisk && img->header.second_size) { // if ramdisk is updated, copy the second stage from original image char* s = malloc(ssize); if (!s) abort_perror(""); if (fseek(img->stream, soffset, SEEK_SET)) abort_perror(img->fname); size_t rb = fread(s, ssize, 1, img->stream); if ((rb!=1) || ferror(img->stream)) abort_perror(img->fname); else if (feof(img->stream)) abort_printf("%s: cannot read second stage\n", img->fname); img->second = s; } n = (img->header.kernel_size + page_size - 1) / page_size; m = (img->header.ramdisk_size + page_size - 1) / page_size; o = (img->header.second_size + page_size - 1) / page_size; unsigned total_size = (1+n+m+o)*page_size; if (!img->size) img->size = total_size; else if (total_size > img->size) abort_printf("%s: updated is too big for the Boot Image (%u vs %u bytes)\n", img->fname, total_size, img->size); } void write_bootimg(t_abootimg* img) { unsigned psize; char* padding; printf ("Writing Boot Image %s\n", img->fname); psize = img->header.page_size; padding = calloc(psize, 1); if (!padding) abort_perror(""); unsigned n = (img->header.kernel_size + psize - 1) / psize; unsigned m = (img->header.ramdisk_size + psize - 1) / psize; unsigned o = (img->header.second_size + psize - 1) / psize; unsigned total_pages = 1+n+m+o; unsigned bootimg_pages = img->size/psize; if (fseek(img->stream, 0, SEEK_SET)) abort_perror(img->fname); fwrite(&img->header, sizeof(img->header), 1, img->stream); if (ferror(img->stream)) abort_perror(img->fname); fwrite(padding, psize - sizeof(img->header), 1, img->stream); if (ferror(img->stream)) abort_perror(img->fname); if (img->kernel) { fwrite(img->kernel, img->header.kernel_size, 1, img->stream); if (ferror(img->stream)) abort_perror(img->fname); fwrite(padding, psize - (img->header.kernel_size % psize), 1, img->stream); if (ferror(img->stream)) abort_perror(img->fname); } if (img->ramdisk) { if (fseek(img->stream, (1+n)*psize, SEEK_SET)) abort_perror(img->fname); fwrite(img->ramdisk, img->header.ramdisk_size, 1, img->stream); if (ferror(img->stream)) abort_perror(img->fname); fwrite(padding, psize - (img->header.ramdisk_size % psize), 1, img->stream); if (ferror(img->stream)) abort_perror(img->fname); } if (img->header.second_size) { if (fseek(img->stream, (1+n+m)*psize, SEEK_SET)) abort_perror(img->fname); fwrite(img->second, img->header.second_size, 1, img->stream); if (ferror(img->stream)) abort_perror(img->fname); fwrite(padding, psize - (img->header.second_size % psize), 1, img->stream); if (ferror(img->stream)) abort_perror(img->fname); } ftruncate (fileno(img->stream), img->size); free(padding); } void print_bootimg_info(t_abootimg* img) { printf ("\nAndroid Boot Image Info:\n\n"); printf ("* file name = %s %s\n\n", img->fname, img->is_blkdev ? "[block device]":""); printf ("* image size = %u bytes (%.2f MB)\n", img->size, (double)img->size/0x100000); printf (" page size = %u bytes\n\n", img->header.page_size); printf ("* Boot Name = \"%s\"\n\n", img->header.name); unsigned kernel_size = img->header.kernel_size; unsigned ramdisk_size = img->header.ramdisk_size; unsigned second_size = img->header.second_size; printf ("* kernel size = %u bytes (%.2f MB)\n", kernel_size, (double)kernel_size/0x100000); printf (" ramdisk size = %u bytes (%.2f MB)\n", ramdisk_size, (double)ramdisk_size/0x100000); if (second_size) printf (" second stage size = %u bytes (%.2f MB)\n", ramdisk_size, (double)ramdisk_size/0x100000); printf ("\n* load addresses:\n"); printf (" kernel: 0x%08x\n", img->header.kernel_addr); printf (" ramdisk: 0x%08x\n", img->header.ramdisk_addr); if (second_size) printf (" second stage: 0x%08x\n", img->header.second_addr); printf (" tags: 0x%08x\n\n", img->header.tags_addr); if (img->header.cmdline[0]) printf ("* cmdline = %s\n\n", img->header.cmdline); else printf ("* empty cmdline\n"); printf ("* id = "); int i; for (i=0; i<8; i++) printf ("0x%08x ", img->header.id[i]); printf ("\n\n"); } void write_bootimg_config(t_abootimg* img) { printf ("writing boot image config in %s\n", img->config_fname); FILE* config_file = fopen(img->config_fname, "w"); if (!config_file) abort_perror(img->config_fname); fprintf(config_file, "bootsize = 0x%x\n", img->size); fprintf(config_file, "pagesize = 0x%x\n", img->header.page_size); fprintf(config_file, "kerneladdr = 0x%x\n", img->header.kernel_addr); fprintf(config_file, "ramdiskaddr = 0x%x\n", img->header.ramdisk_addr); fprintf(config_file, "secondaddr = 0x%x\n", img->header.second_addr); fprintf(config_file, "tagsaddr = 0x%x\n", img->header.tags_addr); fprintf(config_file, "name = %s\n", img->header.name); fprintf(config_file, "cmdline = %s\n", img->header.cmdline); fclose(config_file); } void extract_kernel(t_abootimg* img) { unsigned psize = img->header.page_size; unsigned ksize = img->header.kernel_size; printf ("extracting kernel in %s\n", img->kernel_fname); void* k = malloc(ksize); if (!k) abort_perror(NULL); if (fseek(img->stream, psize, SEEK_SET)) abort_perror(img->fname); size_t rb = fread(k, ksize, 1, img->stream); if ((rb!=1) || ferror(img->stream)) abort_perror(img->fname); FILE* kernel_file = fopen(img->kernel_fname, "w"); if (!kernel_file) abort_perror(img->kernel_fname); fwrite(k, ksize, 1, kernel_file); if (ferror(kernel_file)) abort_perror(img->kernel_fname); fclose(kernel_file); free(k); } void extract_ramdisk(t_abootimg* img) { unsigned psize = img->header.page_size; unsigned ksize = img->header.kernel_size; unsigned rsize = img->header.ramdisk_size; unsigned n = (ksize + psize - 1) / psize; unsigned roffset = (1+n)*psize; printf ("extracting ramdisk in %s\n", img->ramdisk_fname); void* r = malloc(rsize); if (!r) abort_perror(NULL); if (fseek(img->stream, roffset, SEEK_SET)) abort_perror(img->fname); size_t rb = fread(r, rsize, 1, img->stream); if ((rb!=1) || ferror(img->stream)) abort_perror(img->fname); FILE* ramdisk_file = fopen(img->ramdisk_fname, "w"); if (!ramdisk_file) abort_perror(img->ramdisk_fname); fwrite(r, rsize, 1, ramdisk_file); if (ferror(ramdisk_file)) abort_perror(img->ramdisk_fname); fclose(ramdisk_file); free(r); } void extract_second(t_abootimg* img) { unsigned psize = img->header.page_size; unsigned ksize = img->header.kernel_size; unsigned rsize = img->header.ramdisk_size; unsigned ssize = img->header.second_size; if (!ssize) // Second Stage not present return; unsigned n = (rsize + ksize + psize - 1) / psize; unsigned soffset = (1+n)*psize; printf ("extracting second stage image in %s\n", img->second_fname); void* s = malloc(ksize); if (!s) abort_perror(NULL); if (fseek(img->stream, soffset, SEEK_SET)) abort_perror(img->fname); size_t rb = fread(s, ssize, 1, img->stream); if ((rb!=1) || ferror(img->stream)) abort_perror(img->fname); FILE* second_file = fopen(img->second_fname, "w"); if (!second_file) abort_perror(img->second_fname); fwrite(s, ssize, 1, second_file); if (ferror(second_file)) abort_perror(img->second_fname); fclose(second_file); free(s); } t_abootimg* new_bootimg() { t_abootimg* img; img = calloc(sizeof(t_abootimg), 1); if (!img) abort_perror(NULL); img->config_fname = "bootimg.cfg"; img->kernel_fname = "zImage"; img->ramdisk_fname = "initrd.img"; img->second_fname = "stage2.img"; memcpy(img->header.magic, BOOT_MAGIC, BOOT_MAGIC_SIZE); img->header.page_size = 2048; // a sensible default page size return img; } int main(int argc, char** argv) { t_abootimg* bootimg = new_bootimg(); switch(parse_args(argc, argv, bootimg)) { case none: printf("error - bad arguments\n\n"); print_usage(); break; case help: print_usage(); break; case info: open_bootimg(bootimg, "r"); read_header(bootimg); print_bootimg_info(bootimg); break; case extract: open_bootimg(bootimg, "r"); read_header(bootimg); write_bootimg_config(bootimg); extract_kernel(bootimg); extract_ramdisk(bootimg); extract_second(bootimg); break; case update: open_bootimg(bootimg, "r+"); read_header(bootimg); update_header(bootimg); update_images(bootimg); write_bootimg(bootimg); break; case create: if (!bootimg->kernel_fname || !bootimg->ramdisk_fname) { print_usage(); break; } check_if_block_device(bootimg); open_bootimg(bootimg, "w"); update_header(bootimg); update_images(bootimg); if (check_boot_img_header(bootimg)) abort_printf("%s: Sanity cheks failed", bootimg->fname); write_bootimg(bootimg); break; } return 0; } abootimg-0.6/bootimg.h000066400000000000000000000055011160764043000147730ustar00rootroot00000000000000/* tools/mkbootimg/bootimg.h ** ** Copyright 2007, The Android Open Source Project ** ** Licensed under the Apache License, Version 2.0 (the "License"); ** you may not use this file except in compliance with the License. ** You may obtain a copy of the License at ** ** http://www.apache.org/licenses/LICENSE-2.0 ** ** Unless required by applicable law or agreed to in writing, software ** distributed under the License is distributed on an "AS IS" BASIS, ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. ** See the License for the specific language governing permissions and ** limitations under the License. */ #ifndef _BOOT_IMAGE_H_ #define _BOOT_IMAGE_H_ typedef struct boot_img_hdr boot_img_hdr; #define BOOT_MAGIC "ANDROID!" #define BOOT_MAGIC_SIZE 8 #define BOOT_NAME_SIZE 16 #define BOOT_ARGS_SIZE 512 struct boot_img_hdr { unsigned char magic[BOOT_MAGIC_SIZE]; unsigned kernel_size; /* size in bytes */ unsigned kernel_addr; /* physical load addr */ unsigned ramdisk_size; /* size in bytes */ unsigned ramdisk_addr; /* physical load addr */ unsigned second_size; /* size in bytes */ unsigned second_addr; /* physical load addr */ unsigned tags_addr; /* physical addr for kernel tags */ unsigned page_size; /* flash page size we assume */ unsigned unused[2]; /* future expansion: should be 0 */ unsigned char name[BOOT_NAME_SIZE]; /* asciiz product name */ unsigned char cmdline[BOOT_ARGS_SIZE]; unsigned id[8]; /* timestamp / checksum / sha1 / etc */ }; /* ** +-----------------+ ** | boot header | 1 page ** +-----------------+ ** | kernel | n pages ** +-----------------+ ** | ramdisk | m pages ** +-----------------+ ** | second stage | o pages ** +-----------------+ ** ** n = (kernel_size + page_size - 1) / page_size ** m = (ramdisk_size + page_size - 1) / page_size ** o = (second_size + page_size - 1) / page_size ** ** 0. all entities are page_size aligned in flash ** 1. kernel and ramdisk are required (size != 0) ** 2. second is optional (second_size == 0 -> no second) ** 3. load each element (kernel, ramdisk, second) at ** the specified physical address (kernel_addr, etc) ** 4. prepare tags at tag_addr. kernel_args[] is ** appended to the kernel commandline in the tags. ** 5. r0 = 0, r1 = MACHINE_TYPE, r2 = tags_addr ** 6. if second_size != 0: jump to second_addr ** else: jump to kernel_addr */ #if 0 typedef struct ptentry ptentry; struct ptentry { char name[16]; /* asciiz partition name */ unsigned start; /* starting block number */ unsigned length; /* length in blocks */ unsigned flags; /* set to zero */ }; /* MSM Partition Table ATAG ** ** length: 2 + 7 * n ** atag: 0x4d534d70 ** x n */ #endif #endif abootimg-0.6/pack-initrd000077500000000000000000000005521160764043000153160ustar00rootroot00000000000000#!/bin/sh # if [ "$1" = "-f" ]; then forcewrite=yes shift fi initrd=${1:-initrd.img} ramdisk=${2:-ramdisk} if [ ! -d $ramdisk ]; then echo "$ramdisk does not exist." exit 1 fi if [ -f $initrd -a -z "$forcewrite" ]; then echo "$initrd already exist." exit 1 fi ( cd $ramdisk; find | sort | cpio --quiet -o -H newc ) | gzip > $initrd abootimg-0.6/unpack-initrd000077500000000000000000000004201160764043000156530ustar00rootroot00000000000000#!/bin/sh # initrd=${1:-initrd.img} ramdisk=${2:-ramdisk} if [ ! -f $initrd ]; then echo "$initrd does not exist." exit 1 fi if [ -d $ramdisk ]; then echo "$ramdisk already exists." exit 1 fi mkdir -p $ramdisk zcat $initrd | ( cd $ramdisk; cpio -i )