├── README ├── .gitignore ├── unpack-bootimg.sh ├── Android.mk ├── CMakeLists.txt ├── Makefile ├── src ├── hdrboot.c ├── mincrypt │ ├── sha.h │ └── sha.c ├── bootunpack.c └── mkbootimg.c └── shared └── bootimg.h /README: -------------------------------------------------------------------------------- 1 | Tools for unpacking and repacking boot.img. 2 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | bootunpack 2 | hdrboot 3 | mkbootimg 4 | *.o 5 | -------------------------------------------------------------------------------- /unpack-bootimg.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | if [ "$1x" == x ]; 3 | then 4 | echo "Error"; 5 | exit 1; 6 | fi 7 | bootunpack "$1" 8 | mkdir -p "$1-ramdisk" 9 | CPIONAME=`basename $1-ramdisk.cpio.gz` 10 | cd "$1-ramdisk" 11 | gunzip -c "../$CPIONAME" | cpio -i 12 | -------------------------------------------------------------------------------- /Android.mk: -------------------------------------------------------------------------------- 1 | 2 | LOCAL_PATH := $(call my-dir) 3 | 4 | include $(CLEAR_VARS) 5 | 6 | LOCAL_MODULE := bootunpack 7 | LOCAL_MODULE_TAGS := optional 8 | 9 | LOCAL_C_INCLUDES := $(LOCAL_PATH)/shared \ 10 | 11 | LOCAL_SRC_FILES := src/bootunpack.c 12 | 13 | LOCAL_FORCE_STATIC_EXECUTABLE := true 14 | LOCAL_STATIC_LIBRARIES := libc libmincrypt 15 | 16 | include $(BUILD_EXECUTABLE) 17 | -------------------------------------------------------------------------------- /CMakeLists.txt: -------------------------------------------------------------------------------- 1 | project(AndroidTools) 2 | cmake_minimum_required(VERSION 2.6) 3 | SET(EXECUTABLE_OUTPUT_PATH ${CMAKE_BINARY_DIR}/bin) 4 | SET(LIBRARY_OUTPUT_PATH ${CMAKE_BINARY_DIR}/bin/lib) 5 | INCLUDE_DIRECTORIES("${CMAKE_SOURCE_DIR}" "${CMAKE_SOURCE_DIR}/shared" "${CMAKE_SOURCE_DIR}/src") 6 | SET(SHARED_SOURCES shared/bootimg.h) 7 | SET(HDRBOOT_SOURCES src/hdrboot.c ${SHARED_SOURCES}) 8 | SET(BOOTUNPACK_SOURCES src/bootunpack.c ${SHARED_SOURCES}) 9 | SET(MKBOOTIMG_SOURCES src/mkbootimg.c src/mincrypt/rsa.c src/mincrypt/rsa.h src/mincrypt/sha.c src/mincrypt/sha.h ${SHARED_SOURCES}) 10 | ADD_EXECUTABLE(hdrboot ${HDRBOOT_SOURCES}) 11 | ADD_EXECUTABLE(bootunpack ${BOOTUNPACK_SOURCES}) 12 | ADD_EXECUTABLE(mkbootimg ${MKBOOTIMG_SOURCES}) 13 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | AUTOTARGETS=hdrboot bootunpack 2 | TARGETS=$(AUTOTARGETS) mkbootimg 3 | DEPS = Makefile shared/bootimg.h 4 | COMMON= 5 | PREFIX ?= usr/local 6 | 7 | 8 | CC=gcc 9 | CFLAGS=-I. -Ishared -Isrc -Wall 10 | LDFLAGS= 11 | 12 | OBJS = $(COMMON) $(addsuffix .o, $(TARGETS)) 13 | 14 | all: $(TARGETS) 15 | 16 | $(AUTOTARGETS): %: %.o $(COMMON) $(DEPS) 17 | $(CC) $(CFLAGS) -o $@ $< $(COMMON) $(LDFLAGS) 18 | 19 | sha.o: src/mincrypt/sha.c src/mincrypt/sha.h 20 | $(CC) $(CFLAGS) -c -o sha.o src/mincrypt/sha.c 21 | 22 | mkbootimg: mkbootimg.o sha.o $(COMMON) $(DEPS) 23 | $(CC) $(CFLAGS) -o mkbootimg mkbootimg.o sha.o $(COMMON) $(LDFLAGS) 24 | 25 | install: $(TARGETS) 26 | install -d -m 0755 $(DESTDIR)/$(PREFIX)/bin 27 | install -D -m 0755 $(TARGETS) $(DESTDIR)/$(PREFIX)/bin 28 | 29 | $(OBJS): %.o: src/%.c $(DEPS) 30 | $(CC) -c -o $@ $< $(CFLAGS) 31 | 32 | .PHONY: clean 33 | 34 | clean: 35 | rm -f $(TARGETS) *.o 36 | -------------------------------------------------------------------------------- /src/hdrboot.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "bootimg.h" 4 | 5 | int main(int argc, char **argv) 6 | { 7 | if(argc < 2) 8 | { 9 | printf("Usage: %s \n", argv[0]); 10 | return 0; 11 | } 12 | 13 | FILE *fil = fopen(argv[1],"r"); 14 | boot_img_hdr hdr; 15 | fread(&hdr, sizeof(boot_img_hdr), 1, fil); 16 | fclose(fil); 17 | printf("Magic: %.*s\n", BOOT_MAGIC_SIZE, hdr.magic); 18 | printf("Kernel size: 0x%X (%d)\n",hdr.kernel_size, hdr.kernel_size); 19 | printf(" Aligned size: 0x%X\n", hdr.kernel_size + ((-hdr.kernel_size) & (hdr.page_size -1))); 20 | printf("Kernel addr: 0x%X\n",hdr.kernel_addr); 21 | printf("Ramdisk size: 0x%X (%d)\n",hdr.ramdisk_size, hdr.ramdisk_size); 22 | printf("Ramdisk addr: 0x%X\n",hdr.ramdisk_addr); 23 | 24 | printf("Second size: 0x%X (%d)\n",hdr.second_size, hdr.second_size); 25 | printf("Second addr: 0x%X\n",hdr.second_addr); 26 | 27 | printf("Tags addr: 0x%X\n",hdr.tags_addr); 28 | printf("Page size: 0x%X (%d)\n",hdr.page_size, hdr.page_size); 29 | 30 | printf("Name: %.*s\n", BOOT_NAME_SIZE, hdr.name); 31 | printf("Cmdline: %.*s\n", BOOT_ARGS_SIZE, hdr.cmdline); 32 | return 0; 33 | } 34 | -------------------------------------------------------------------------------- /src/mincrypt/sha.h: -------------------------------------------------------------------------------- 1 | /* sha.h 2 | ** 3 | ** Copyright 2008, The Android Open Source Project 4 | ** 5 | ** Redistribution and use in source and binary forms, with or without 6 | ** modification, are permitted provided that the following conditions are met: 7 | ** * Redistributions of source code must retain the above copyright 8 | ** notice, this list of conditions and the following disclaimer. 9 | ** * Redistributions in binary form must reproduce the above copyright 10 | ** notice, this list of conditions and the following disclaimer in the 11 | ** documentation and/or other materials provided with the distribution. 12 | ** * Neither the name of Google Inc. nor the names of its contributors may 13 | ** be used to endorse or promote products derived from this software 14 | ** without specific prior written permission. 15 | ** 16 | ** THIS SOFTWARE IS PROVIDED BY Google Inc. ``AS IS'' AND ANY EXPRESS OR 17 | ** IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 18 | ** MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO 19 | ** EVENT SHALL Google Inc. BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 20 | ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 21 | ** PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 22 | ** OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 23 | ** WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 24 | ** OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 25 | ** ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 | */ 27 | 28 | #ifndef _EMBEDDED_SHA_H_ 29 | #define _EMBEDDED_SHA_H_ 30 | 31 | #include 32 | 33 | #ifdef __cplusplus 34 | extern "C" { 35 | #endif 36 | 37 | typedef struct SHA_CTX { 38 | uint64_t count; 39 | uint8_t buf[64]; 40 | uint32_t state[5]; 41 | } SHA_CTX; 42 | 43 | void SHA_init(SHA_CTX *ctx); 44 | void SHA_update(SHA_CTX *ctx, const void* data, int len); 45 | const uint8_t* SHA_final(SHA_CTX *ctx); 46 | 47 | /* Convenience method. Returns digest parameter value. */ 48 | const uint8_t* SHA(const void *data, int len, uint8_t *digest); 49 | 50 | #define SHA_DIGEST_SIZE 20 51 | 52 | #ifdef __cplusplus 53 | } 54 | #endif 55 | 56 | #endif 57 | -------------------------------------------------------------------------------- /shared/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 | #define RESERVE_LOG_MAGIC 0x474F4C52 //ML,"RLOG" in ascii 28 | 29 | struct boot_img_hdr 30 | { 31 | unsigned char magic[BOOT_MAGIC_SIZE]; 32 | 33 | unsigned kernel_size; /* size in bytes */ 34 | unsigned kernel_addr; /* physical load addr */ 35 | 36 | unsigned ramdisk_size; /* size in bytes */ 37 | unsigned ramdisk_addr; /* physical load addr */ 38 | 39 | unsigned second_size; /* size in bytes */ 40 | unsigned second_addr; /* physical load addr */ 41 | 42 | unsigned tags_addr; /* physical addr for kernel tags */ 43 | unsigned page_size; /* flash page size we assume */ 44 | unsigned unused[2]; /* future expansion: should be 0 */ 45 | 46 | unsigned char name[BOOT_NAME_SIZE]; /* asciiz product name */ 47 | 48 | unsigned char cmdline[BOOT_ARGS_SIZE]; 49 | 50 | unsigned id[8]; /* timestamp / checksum / sha1 / etc */ 51 | unsigned log_buf_magic; /* physical log buf addr */ 52 | unsigned log_buf_addr; /* physical log buf addr */ 53 | }; 54 | 55 | /* 56 | ** +-----------------+ 57 | ** | boot header | 1 page 58 | ** +-----------------+ 59 | ** | kernel | n pages 60 | ** +-----------------+ 61 | ** | ramdisk | m pages 62 | ** +-----------------+ 63 | ** | second stage | o pages 64 | ** +-----------------+ 65 | ** 66 | ** n = (kernel_size + page_size - 1) / page_size 67 | ** m = (ramdisk_size + page_size - 1) / page_size 68 | ** o = (second_size + page_size - 1) / page_size 69 | ** 70 | ** 0. all entities are page_size aligned in flash 71 | ** 1. kernel and ramdisk are required (size != 0) 72 | ** 2. second is optional (second_size == 0 -> no second) 73 | ** 3. load each element (kernel, ramdisk, second) at 74 | ** the specified physical address (kernel_addr, etc) 75 | ** 4. prepare tags at tag_addr. kernel_args[] is 76 | ** appended to the kernel commandline in the tags. 77 | ** 5. r0 = 0, r1 = MACHINE_TYPE, r2 = tags_addr 78 | ** 6. if second_size != 0: jump to second_addr 79 | ** else: jump to kernel_addr 80 | */ 81 | 82 | #if 0 83 | typedef struct ptentry ptentry; 84 | 85 | struct ptentry { 86 | char name[16]; /* asciiz partition name */ 87 | unsigned start; /* starting block number */ 88 | unsigned length; /* length in blocks */ 89 | unsigned flags; /* set to zero */ 90 | }; 91 | 92 | /* MSM Partition Table ATAG 93 | ** 94 | ** length: 2 + 7 * n 95 | ** atag: 0x4d534d70 96 | ** x n 97 | */ 98 | #endif 99 | 100 | #endif 101 | -------------------------------------------------------------------------------- /src/bootunpack.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | #include "bootimg.h" 6 | #define FALSE 0 7 | #define TRUE 1 8 | #define MAX_PATH 1024 9 | #define BUFFER_SIZE 2048 10 | 11 | void write_file(char *filename, FILE* in_file, unsigned int filesize); 12 | void write_config(char *filename, boot_img_hdr* header); 13 | 14 | int main(int argc, char **argv) 15 | { 16 | if(argc< 2) 17 | { 18 | printf("Usage: %s \n", argv[0]); 19 | printf(" Extracts kernel and ramdisk from a boot.img file\n"); 20 | return -1; 21 | } 22 | 23 | FILE *fil = fopen(argv[1],"rb"); 24 | boot_img_hdr hdr; 25 | fread(&hdr, sizeof(boot_img_hdr), 1, fil); 26 | unsigned int header_padding = (-sizeof(boot_img_hdr) & (hdr.page_size -1)); 27 | if(strncmp((const char*)hdr.magic, BOOT_MAGIC, BOOT_MAGIC_SIZE) || hdr.kernel_size < 1 || hdr.ramdisk_size < 1) 28 | { 29 | fclose(fil); 30 | fprintf(stderr, "Not a valid boot.img\n"); 31 | return -2; 32 | }; 33 | fseek(fil, header_padding, SEEK_CUR); 34 | unsigned int kernel_padding = (-hdr.kernel_size) & (hdr.page_size -1); 35 | unsigned int ramdisk_padding = (-hdr.ramdisk_size) & (hdr.page_size -1); 36 | 37 | char filename[MAX_PATH]; 38 | 39 | // Write out kernel 40 | snprintf(filename, MAX_PATH, "%s-kernel.gz", argv[1]); 41 | printf("Writing kernel to %s\n", filename); 42 | write_file(filename, fil, hdr.kernel_size); 43 | fseek(fil, kernel_padding, SEEK_CUR); // Skip past kernel padding 44 | 45 | // Write out ramdisk 46 | snprintf(filename, MAX_PATH, "%s-ramdisk.cpio.gz", argv[1]); 47 | printf("Writing ramdisk to %s\n", filename); 48 | write_file(filename, fil, hdr.ramdisk_size); 49 | 50 | // Write out optional secondary bootloader 51 | if(hdr.second_size > 0) 52 | { 53 | fseek(fil, ramdisk_padding, SEEK_CUR); // Skip past ramdisk padding 54 | snprintf(filename, MAX_PATH, "%s-second.boot", argv[1]); 55 | printf("Writing secondary bootloader to %s\n", filename); 56 | write_file(filename, fil, hdr.second_size); 57 | } 58 | 59 | fclose(fil); 60 | snprintf(filename, MAX_PATH, "%s-config", argv[1]); 61 | printf("Writing boot.img config to %s\n",filename); 62 | write_config(filename, &hdr); 63 | return 0; 64 | } 65 | 66 | void write_file(char *filename, FILE* in_file, unsigned int filesize) 67 | { 68 | FILE *out_file; 69 | out_file = fopen(filename, "wb"); 70 | unsigned int dataleft = filesize; 71 | char buffer[BUFFER_SIZE]; 72 | while(dataleft > 0) 73 | { 74 | unsigned int fsize = BUFFER_SIZE < dataleft ? BUFFER_SIZE : dataleft; 75 | unsigned int read_data = fread(buffer, 1, fsize, in_file); 76 | unsigned int written_data = fwrite(buffer, 1, read_data, out_file); 77 | // TODO: make more robust, e.g. we can't always count on the 78 | // full amount of read data is actually written to disk 79 | // In practice this never happens as long as we are reading and writing to files! 80 | //FIFO's etc might cause issues. 81 | if(read_data != written_data) 82 | { 83 | printf("Did not write all data read. Corruption ohoy!\n"); 84 | fflush(stdout); 85 | abort(); 86 | } 87 | dataleft -= read_data; 88 | } 89 | fclose(out_file); 90 | }; 91 | 92 | void write_config(char *filename, boot_img_hdr* header) 93 | { 94 | FILE *out_file = fopen(filename, "w"); 95 | fprintf(out_file, "NAME=%s\n", header->name); 96 | fprintf(out_file, "CMDLINE=%s\n", header->cmdline); 97 | fclose(out_file); 98 | }; 99 | 100 | -------------------------------------------------------------------------------- /src/mincrypt/sha.c: -------------------------------------------------------------------------------- 1 | /* sha.c 2 | ** 3 | ** Copyright 2008, The Android Open Source Project 4 | ** 5 | ** Redistribution and use in source and binary forms, with or without 6 | ** modification, are permitted provided that the following conditions are met: 7 | ** * Redistributions of source code must retain the above copyright 8 | ** notice, this list of conditions and the following disclaimer. 9 | ** * Redistributions in binary form must reproduce the above copyright 10 | ** notice, this list of conditions and the following disclaimer in the 11 | ** documentation and/or other materials provided with the distribution. 12 | ** * Neither the name of Google Inc. nor the names of its contributors may 13 | ** be used to endorse or promote products derived from this software 14 | ** without specific prior written permission. 15 | ** 16 | ** THIS SOFTWARE IS PROVIDED BY Google Inc. ``AS IS'' AND ANY EXPRESS OR 17 | ** IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 18 | ** MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO 19 | ** EVENT SHALL Google Inc. BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 20 | ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 21 | ** PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 22 | ** OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 23 | ** WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 24 | ** OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 25 | ** ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 | */ 27 | 28 | #include "mincrypt/sha.h" 29 | 30 | #define rol(bits, value) (((value) << (bits)) | ((value) >> (32 - (bits)))) 31 | 32 | static void SHA1_transform(SHA_CTX *ctx) { 33 | uint32_t W[80]; 34 | uint32_t A, B, C, D, E; 35 | uint8_t *p = ctx->buf; 36 | int t; 37 | 38 | for(t = 0; t < 16; ++t) { 39 | uint32_t tmp = *p++ << 24; 40 | tmp |= *p++ << 16; 41 | tmp |= *p++ << 8; 42 | tmp |= *p++; 43 | W[t] = tmp; 44 | } 45 | 46 | for(; t < 80; t++) { 47 | W[t] = rol(1,W[t-3] ^ W[t-8] ^ W[t-14] ^ W[t-16]); 48 | } 49 | 50 | A = ctx->state[0]; 51 | B = ctx->state[1]; 52 | C = ctx->state[2]; 53 | D = ctx->state[3]; 54 | E = ctx->state[4]; 55 | 56 | for(t = 0; t < 80; t++) { 57 | uint32_t tmp = rol(5,A) + E + W[t]; 58 | 59 | if (t < 20) 60 | tmp += (D^(B&(C^D))) + 0x5A827999; 61 | else if ( t < 40) 62 | tmp += (B^C^D) + 0x6ED9EBA1; 63 | else if ( t < 60) 64 | tmp += ((B&C)|(D&(B|C))) + 0x8F1BBCDC; 65 | else 66 | tmp += (B^C^D) + 0xCA62C1D6; 67 | 68 | E = D; 69 | D = C; 70 | C = rol(30,B); 71 | B = A; 72 | A = tmp; 73 | } 74 | 75 | ctx->state[0] += A; 76 | ctx->state[1] += B; 77 | ctx->state[2] += C; 78 | ctx->state[3] += D; 79 | ctx->state[4] += E; 80 | } 81 | 82 | void SHA_init(SHA_CTX *ctx) { 83 | ctx->state[0] = 0x67452301; 84 | ctx->state[1] = 0xEFCDAB89; 85 | ctx->state[2] = 0x98BADCFE; 86 | ctx->state[3] = 0x10325476; 87 | ctx->state[4] = 0xC3D2E1F0; 88 | ctx->count = 0; 89 | } 90 | 91 | void SHA_update(SHA_CTX *ctx, const void *data, int len) { 92 | int i = ctx->count % sizeof(ctx->buf); 93 | const uint8_t* p = (const uint8_t*)data; 94 | 95 | ctx->count += len; 96 | 97 | while (len--) { 98 | ctx->buf[i++] = *p++; 99 | if (i == sizeof(ctx->buf)) { 100 | SHA1_transform(ctx); 101 | i = 0; 102 | } 103 | } 104 | } 105 | const uint8_t *SHA_final(SHA_CTX *ctx) { 106 | uint8_t *p = ctx->buf; 107 | uint64_t cnt = ctx->count * 8; 108 | int i; 109 | 110 | SHA_update(ctx, (uint8_t*)"\x80", 1); 111 | while ((ctx->count % sizeof(ctx->buf)) != (sizeof(ctx->buf) - 8)) { 112 | SHA_update(ctx, (uint8_t*)"\0", 1); 113 | } 114 | for (i = 0; i < 8; ++i) { 115 | uint8_t tmp = cnt >> ((7 - i) * 8); 116 | SHA_update(ctx, &tmp, 1); 117 | } 118 | 119 | for (i = 0; i < 5; i++) { 120 | uint32_t tmp = ctx->state[i]; 121 | *p++ = tmp >> 24; 122 | *p++ = tmp >> 16; 123 | *p++ = tmp >> 8; 124 | *p++ = tmp >> 0; 125 | } 126 | 127 | return ctx->buf; 128 | } 129 | 130 | /* Convenience function */ 131 | const uint8_t* SHA(const void *data, int len, uint8_t *digest) { 132 | const uint8_t *p; 133 | int i; 134 | SHA_CTX ctx; 135 | SHA_init(&ctx); 136 | SHA_update(&ctx, data, len); 137 | p = SHA_final(&ctx); 138 | for (i = 0; i < SHA_DIGEST_SIZE; ++i) { 139 | digest[i] = *p++; 140 | } 141 | return digest; 142 | } 143 | -------------------------------------------------------------------------------- /src/mkbootimg.c: -------------------------------------------------------------------------------- 1 | /* tools/mkbootimg/mkbootimg.c 2 | ** 3 | ** Copyright 2007, The Android Open Source Project 4 | ** Copyright (c) 2012, Code Aurora Forum. All rights reserved. 5 | ** 6 | ** Licensed under the Apache License, Version 2.0 (the "License"); 7 | ** you may not use this file except in compliance with the License. 8 | ** You may obtain a copy of the License at 9 | ** 10 | ** http://www.apache.org/licenses/LICENSE-2.0 11 | ** 12 | ** Unless required by applicable law or agreed to in writing, software 13 | ** distributed under the License is distributed on an "AS IS" BASIS, 14 | ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | ** See the License for the specific language governing permissions and 16 | ** limitations under the License. 17 | */ 18 | 19 | #include 20 | #include 21 | #include 22 | #include 23 | #include 24 | #include 25 | 26 | #include "mincrypt/sha.h" 27 | #include "bootimg.h" 28 | 29 | #define ROUND_TO_PAGE(x, y) (((x) + (y)) & (~(y))) 30 | 31 | static void *load_file(const char *fn, unsigned *_sz) 32 | { 33 | char *data; 34 | int sz; 35 | int fd; 36 | 37 | data = 0; 38 | fd = open(fn, O_RDONLY); 39 | if(fd < 0) return 0; 40 | 41 | sz = lseek(fd, 0, SEEK_END); 42 | if(sz < 0) goto oops; 43 | 44 | if(lseek(fd, 0, SEEK_SET) != 0) goto oops; 45 | 46 | data = (char*) malloc(sz); 47 | if(data == 0) goto oops; 48 | 49 | if(read(fd, data, sz) != sz) goto oops; 50 | close(fd); 51 | 52 | if(_sz) *_sz = sz; 53 | return data; 54 | 55 | oops: 56 | close(fd); 57 | if(data != 0) free(data); 58 | return 0; 59 | } 60 | 61 | int usage(void) 62 | { 63 | fprintf(stderr,"usage: mkbootimg\n" 64 | " --kernel \n" 65 | " --ramdisk \n" 66 | " [ --ramdisk_offset ]\n" 67 | " [ -z ] \n" 68 | " [ --second <2ndbootloader-filename> ]\n" 69 | " [ --cmdline ]\n" 70 | " [ --board ]\n" 71 | " [ --base
]\n" 72 | " [ --pagesize ]\n" 73 | " -o|--output \n" 74 | ); 75 | return 1; 76 | } 77 | 78 | 79 | 80 | static unsigned char padding[4096] = { 0, }; 81 | 82 | int write_padding(int fd, unsigned pagesize, unsigned itemsize) 83 | { 84 | unsigned pagemask = pagesize - 1; 85 | unsigned count; 86 | 87 | if((itemsize & pagemask) == 0) { 88 | return 0; 89 | } 90 | 91 | count = pagesize - (itemsize & pagemask); 92 | 93 | if(write(fd, padding, count) != count) { 94 | return -1; 95 | } else { 96 | return 0; 97 | } 98 | } 99 | 100 | int parse_log_addr(char *filename) 101 | { 102 | FILE* fp; 103 | char buffer[128]; 104 | char data[9]; 105 | char *p; 106 | int retValue = 0; 107 | int found =0; 108 | 109 | char *subs = "__log_buf"; 110 | fp=fopen(filename,"r"); 111 | 112 | if(fp==NULL) 113 | return -1; 114 | while ((fgets (buffer, 100, fp)) != NULL){ 115 | if( (p =strstr(buffer, subs))){ 116 | found = 1; 117 | break; 118 | } 119 | } 120 | fclose(fp); 121 | 122 | if(!found) 123 | return -1; 124 | 125 | memcpy(data,buffer,8); 126 | data[8] = 0; 127 | 128 | sscanf(data,"%x",&retValue); 129 | 130 | return (retValue - 0xc0000000 +0x200000); 131 | } 132 | 133 | 134 | int main(int argc, char **argv) 135 | { 136 | boot_img_hdr hdr; 137 | 138 | char *system_map = 0; 139 | char *kernel_fn = 0; 140 | void *kernel_data = 0; 141 | char *ramdisk_fn = 0; 142 | void *ramdisk_data = 0; 143 | char *second_fn = 0; 144 | void *second_data = 0; 145 | char *cmdline = ""; 146 | char *bootimg = 0; 147 | char *board = ""; 148 | unsigned pagesize = 2048; 149 | int fd; 150 | SHA_CTX ctx; 151 | const uint8_t* sha; 152 | unsigned base = 0x10000000; 153 | unsigned ramdisk_offset = 0x01300000; 154 | int compressed_kernel = 0; 155 | 156 | argc--; 157 | argv++; 158 | 159 | memset(&hdr, 0, sizeof(hdr)); 160 | 161 | while(argc > 0){ 162 | char *arg = argv[0]; 163 | char *val = argv[1]; 164 | if(argc < 2) { 165 | return usage(); 166 | } 167 | argc -= 2; 168 | argv += 2; 169 | if(!strcmp(arg, "--output") || !strcmp(arg, "-o")) { 170 | bootimg = val; 171 | } else if(!strcmp(arg, "--kernel")) { 172 | kernel_fn = val; 173 | } else if(!strcmp(arg, "--systemmap")) { 174 | system_map = val; 175 | } else if(!strcmp(arg, "--ramdisk")) { 176 | ramdisk_fn = val; 177 | } else if(!strcmp(arg, "--second")) { 178 | second_fn = val; 179 | } else if(!strcmp(arg, "--cmdline")) { 180 | cmdline = val; 181 | } else if(!strcmp(arg, "--base")) { 182 | base = strtoul(val, 0, 16); 183 | } else if(!strcmp(arg, "--board")) { 184 | board = val; 185 | } else if(!strcmp(arg,"--pagesize")) { 186 | pagesize = strtoul(val, 0, 10); 187 | if ((pagesize != 2048) && (pagesize != 4096)) { 188 | fprintf(stderr,"error: unsupported page size %d\n", pagesize); 189 | return -1; 190 | } 191 | } else if (!strcmp(arg, "--ramdisk_offset")) { 192 | ramdisk_offset = strtoul(val, 0, 16); 193 | } else if (!strcmp(arg, "-z")) { 194 | compressed_kernel = 1; 195 | argc++; 196 | argv--; 197 | } else { 198 | return usage(); 199 | } 200 | } 201 | hdr.page_size = pagesize; 202 | 203 | 204 | if(bootimg == 0) { 205 | fprintf(stderr,"error: no output filename specified\n"); 206 | return usage(); 207 | } 208 | 209 | if(kernel_fn == 0) { 210 | fprintf(stderr,"error: no kernel image specified\n"); 211 | return usage(); 212 | } 213 | 214 | if(ramdisk_fn == 0) { 215 | fprintf(stderr,"error: no ramdisk image specified\n"); 216 | return usage(); 217 | } 218 | 219 | if(strlen(board) >= BOOT_NAME_SIZE) { 220 | fprintf(stderr,"error: board name too large\n"); 221 | return usage(); 222 | } 223 | 224 | hdr.second_addr = base + 0x00F00000; 225 | hdr.tags_addr = base + 0x00000100; 226 | hdr.ramdisk_addr = base + 0x00008000 + ramdisk_offset; 227 | if(parse_log_addr(system_map) > 0) 228 | { 229 | hdr.log_buf_addr = parse_log_addr(system_map); 230 | hdr.log_buf_magic = RESERVE_LOG_MAGIC;//ML, 231 | } 232 | 233 | strcpy((char*)hdr.name, board); 234 | 235 | memcpy(hdr.magic, BOOT_MAGIC, BOOT_MAGIC_SIZE); 236 | 237 | if(strlen(cmdline) > (BOOT_ARGS_SIZE - 1)) { 238 | fprintf(stderr,"error: kernel commandline too large\n"); 239 | return 1; 240 | } 241 | strcpy((char*)hdr.cmdline, cmdline); 242 | 243 | kernel_data = load_file(kernel_fn, &hdr.kernel_size); 244 | if(kernel_data == 0) { 245 | fprintf(stderr,"error: could not load kernel '%s'\n", kernel_fn); 246 | return 1; 247 | } 248 | 249 | if(!strcmp(ramdisk_fn,"NONE")) { 250 | ramdisk_data = 0; 251 | hdr.ramdisk_size = 0; 252 | } else { 253 | ramdisk_data = load_file(ramdisk_fn, &hdr.ramdisk_size); 254 | if(ramdisk_data == 0) { 255 | fprintf(stderr,"error: could not load ramdisk '%s'\n", ramdisk_fn); 256 | return 1; 257 | } 258 | } 259 | 260 | if (compressed_kernel) { 261 | /* put the compressed image after the ramdisk so that the 262 | decompressor may run without having to relocate itself and 263 | the compressed image */ 264 | hdr.kernel_addr = hdr.ramdisk_addr + 265 | ROUND_TO_PAGE(hdr.ramdisk_size, (pagesize - 1)); 266 | hdr.kernel_addr = (hdr.kernel_addr + 4) & 0xfffffffc; 267 | } else 268 | hdr.kernel_addr = base + 0x00008000; 269 | 270 | if(second_fn) { 271 | second_data = load_file(second_fn, &hdr.second_size); 272 | if(second_data == 0) { 273 | fprintf(stderr,"error: could not load secondstage '%s'\n", second_fn); 274 | return 1; 275 | } 276 | } 277 | 278 | /* put a hash of the contents in the header so boot images can be 279 | * differentiated based on their first 2k. 280 | */ 281 | SHA_init(&ctx); 282 | SHA_update(&ctx, kernel_data, hdr.kernel_size); 283 | SHA_update(&ctx, &hdr.kernel_size, sizeof(hdr.kernel_size)); 284 | SHA_update(&ctx, ramdisk_data, hdr.ramdisk_size); 285 | SHA_update(&ctx, &hdr.ramdisk_size, sizeof(hdr.ramdisk_size)); 286 | SHA_update(&ctx, second_data, hdr.second_size); 287 | SHA_update(&ctx, &hdr.second_size, sizeof(hdr.second_size)); 288 | sha = SHA_final(&ctx); 289 | memcpy(hdr.id, sha, 290 | SHA_DIGEST_SIZE > sizeof(hdr.id) ? sizeof(hdr.id) : SHA_DIGEST_SIZE); 291 | 292 | fd = open(bootimg, O_CREAT | O_TRUNC | O_WRONLY, 0644); 293 | if(fd < 0) { 294 | fprintf(stderr,"error: could not create '%s'\n", bootimg); 295 | return 1; 296 | } 297 | 298 | if(write(fd, &hdr, sizeof(hdr)) != sizeof(hdr)) goto fail; 299 | if(write_padding(fd, pagesize, sizeof(hdr))) goto fail; 300 | 301 | if(write(fd, kernel_data, hdr.kernel_size) != hdr.kernel_size) goto fail; 302 | if(write_padding(fd, pagesize, hdr.kernel_size)) goto fail; 303 | 304 | if(write(fd, ramdisk_data, hdr.ramdisk_size) != hdr.ramdisk_size) goto fail; 305 | if(write_padding(fd, pagesize, hdr.ramdisk_size)) goto fail; 306 | 307 | if(second_data) { 308 | if(write(fd, second_data, hdr.second_size) != hdr.second_size) goto fail; 309 | if(write_padding(fd, pagesize, hdr.ramdisk_size)) goto fail; 310 | } 311 | 312 | return 0; 313 | 314 | fail: 315 | unlink(bootimg); 316 | close(fd); 317 | fprintf(stderr,"error: failed writing '%s': %s\n", bootimg, 318 | strerror(errno)); 319 | return 1; 320 | } 321 | --------------------------------------------------------------------------------