├── ARM ├── README.md ├── bash ├── cpio ├── file ├── grep ├── gzip ├── lz4 ├── lzma ├── lzop ├── magic.mgc ├── mkboot ├── mkbootfs ├── mkbootimg ├── od ├── wrapper └── xz ├── README.md ├── dtbTool ├── dtbToolCM ├── dtbtool.txt ├── dtc ├── lz4 ├── mkboot ├── mkbootfs └── mkbootimg /ARM/README.md: -------------------------------------------------------------------------------- 1 | 1. Built to be used on Arm devices. 2 | 3 | 2. All binaries are statically compiled using the arm-linux-androideabi and arm-linux-gnueabi toolchains. 4 | 5 | 3. Run the mkboot script and have fun (mkboot script is a shebang manipulator) 6 | 7 | NOTE: This project (ARM) is designed to work on Android (ARM) devices, however, it will also work on Linux as well. Tested and confirmed working on Ubuntu 15.04. 8 | 9 | ### Unpack Boot.img or Recovery.img: 10 | root@android:/data/local/tmp/mkbootimg_tool/ARM # ./mkboot boot.img bootfolder 11 | 12 | Unpack & decompress boot.img to bootfolder 13 | 14 | ****** WARNING ******* WARNING ******* WARNING ****** 15 | 16 | This image is built using NON-standard mkbootimg! 17 | 18 | BASE is 0x80400000 19 | RAMDISK_OFFSET is 0x01408000 20 | 21 | You can modify mkbootimg.c with the above value(s) 22 | 23 | ****** WARNING ******* WARNING ******* WARNING ****** 24 | 25 | kernel : zImage 26 | ramdisk : ramdisk 27 | page size : 2048 28 | kernel size : 5690888 29 | ramdisk size : 520206 30 | base : 0x80400000 (Non Standard) 31 | kernel offset : 0x00008000 32 | ramdisk offset : 0x01408000 (Non Standard) 33 | second offset : 0x00f00000 34 | tags offset : 0x00000100 35 | cmd line : console=ttyHSL0,115200,n8 user_debug=31 36 | 37 | Ramdisk is gzip format. 38 | 1851 blocks 39 | Unpack completed. 40 | 41 | root@android:/data/local/tmp/mkbootimg_tools-master/ARM # 42 | 43 | ### Repack Boot.img or Recovery.img: 44 | root@android:/data/local/tmp/mkbootimg_tools-master/ARM # ./mkboot bootfolder boot.img 45 | 46 | mkbootimg from bootfolder/img_info. 47 | 48 | kernel : zImage 49 | ramdisk : new_ramdisk.gzip 50 | page size : 2048 51 | kernel size : 5690888 52 | ramdisk size : 521739 53 | base : 0x80400000 54 | kernel offset : 0x00008000 55 | ramdisk offset : 0x01408000 56 | second offset : 0x00f00000 57 | tags offset : 0x00000100 58 | cmd line : console=ttyHSL0,115200,n8 user_debug=31 59 | 60 | Kernel size: 5690888, new ramdisk size: 521739, boot.img: 6215680. 61 | 62 | boot.img has been created. 63 | 64 | root@android:/data/local/tmp/mkbootimg_tools-master/ARM # 65 | 66 | ### Repack Boot.img or Recovery.img with larger build than original: 67 | root@android:/data/local/tmp/mkbootimg_tools-master/ARM # ./mkboot bootfolder boot.img 68 | 69 | mkbootimg from bootfolder/img_info. 70 | 71 | kernel : zImage 72 | ramdisk : new_ramdisk.gzip 73 | page size : 2048 74 | kernel size : 5690888 75 | ramdisk size : 11233890 76 | base : 0x80400000 77 | kernel offset : 0x00008000 78 | ramdisk offset : 0x01408000 79 | second offset : 0x00f00000 80 | tags offset : 0x00000100 81 | cmd line : console=ttyHSL0,115200,n8 user_debug=31 82 | 83 | Kernel size: 5690888, new ramdisk size: 11233890, boot.img: 16928768. 84 | 85 | boot.img has been created. 86 | 87 | 88 | ****** CAUTION ******* CAUTION ******* CAUTION ****** 89 | 90 | boot.img is 151552 bytes larger than 91 | the original build! Make sure this new 92 | size is not larger than the actual partition! 93 | 94 | ****** CAUTION ******* CAUTION ******* CAUTION ****** 95 | 96 | root@android:/data/local/tmp/mkbootimg_tools-master/ARM # 97 | -------------------------------------------------------------------------------- /ARM/bash: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/khanfrd/mkbootimg_tools/2d8905eb65b3b88490325d2afacd458d859006f6/ARM/bash -------------------------------------------------------------------------------- /ARM/cpio: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/khanfrd/mkbootimg_tools/2d8905eb65b3b88490325d2afacd458d859006f6/ARM/cpio -------------------------------------------------------------------------------- /ARM/file: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/khanfrd/mkbootimg_tools/2d8905eb65b3b88490325d2afacd458d859006f6/ARM/file -------------------------------------------------------------------------------- /ARM/grep: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/khanfrd/mkbootimg_tools/2d8905eb65b3b88490325d2afacd458d859006f6/ARM/grep -------------------------------------------------------------------------------- /ARM/gzip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/khanfrd/mkbootimg_tools/2d8905eb65b3b88490325d2afacd458d859006f6/ARM/gzip -------------------------------------------------------------------------------- /ARM/lz4: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/khanfrd/mkbootimg_tools/2d8905eb65b3b88490325d2afacd458d859006f6/ARM/lz4 -------------------------------------------------------------------------------- /ARM/lzma: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/khanfrd/mkbootimg_tools/2d8905eb65b3b88490325d2afacd458d859006f6/ARM/lzma -------------------------------------------------------------------------------- /ARM/lzop: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/khanfrd/mkbootimg_tools/2d8905eb65b3b88490325d2afacd458d859006f6/ARM/lzop -------------------------------------------------------------------------------- /ARM/magic.mgc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/khanfrd/mkbootimg_tools/2d8905eb65b3b88490325d2afacd458d859006f6/ARM/magic.mgc -------------------------------------------------------------------------------- /ARM/mkboot: -------------------------------------------------------------------------------- 1 | # This is a wrapper script which basically manipulates the boundaries of an already limited shebang. 2 | # This allows the execution of the real mkboot (aka wrapper) script using bash from the project directory whether it be on Linux or an ARM device. 3 | 4 | target=$(pwd) 5 | script="${target}/wrapper" 6 | shebang=$(head -1 "$script") 7 | buildit="$@" 8 | 9 | # Use an array in case a argument is there too 10 | interp=( ${shebang#\#!} ) 11 | 12 | # Now run it, passing in the remaining command line arguments 13 | # EXAMPLE 1: Unpacking: ./mkboot recovery.img recoveryfolder 14 | # EXAMPLE 2: Packing: ./mkboot recoveryfolder recovery.img 15 | shift 1 16 | exec "${target}/${interp[@]}" "$script" ${buildit} 17 | -------------------------------------------------------------------------------- /ARM/mkbootfs: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/khanfrd/mkbootimg_tools/2d8905eb65b3b88490325d2afacd458d859006f6/ARM/mkbootfs -------------------------------------------------------------------------------- /ARM/mkbootimg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/khanfrd/mkbootimg_tools/2d8905eb65b3b88490325d2afacd458d859006f6/ARM/mkbootimg -------------------------------------------------------------------------------- /ARM/od: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/khanfrd/mkbootimg_tools/2d8905eb65b3b88490325d2afacd458d859006f6/ARM/od -------------------------------------------------------------------------------- /ARM/wrapper: -------------------------------------------------------------------------------- 1 | #!bash 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | # 15 | #boot.img tool 16 | #original author: xiaolu 17 | #heavily modified by: Modding.MyMind 18 | 19 | #set -x # for debugging 20 | 21 | trap "clean" 2 3 4 22 | workdir=$(pwd) 23 | toolpath=$(readlink -f $0) 24 | tooldir=$(dirname $toolpath) 25 | mkbootimg=$tooldir/mkbootimg 26 | mkbootfs=$tooldir/mkbootfs 27 | busybox=$(command -v busybox) 28 | od=$tooldir/od 29 | gzip=$tooldir/gzip 30 | lz4=$tooldir/lz4 31 | lzop=$tooldir/lzop 32 | lzma=$tooldir/lzma 33 | xz=$tooldir/xz # Also used for lzma compression 34 | grep=$tooldir/grep 35 | cpio=$tooldir/cpio 36 | magic=$tooldir/magic.mgc 37 | file=$tooldir/file 38 | old_bootimg=true 39 | C_OUT="\033[0;1m" 40 | C_ERR="\033[31;1m" 41 | C_CAUT="\033[33;1m" 42 | C_CLEAR="\033[0;0m" 43 | 44 | pout() { 45 | $busybox printf "${C_OUT}${*}${C_CLEAR}\n" 46 | } 47 | perr() { 48 | $busybox printf "${C_ERR}${*}${C_CLEAR}\n" 49 | } 50 | pcaut() { 51 | $busybox printf "${C_CAUT}${*}${C_CLEAR}\n" 52 | } 53 | clean() 54 | { 55 | $busybox rm -rf /tmp/mkboot.* 56 | #pout "..." 57 | exit 58 | } 59 | 60 | # Check for busybox 61 | if [ -z $busybox ]; then 62 | clear; pcaut "Busybox is NOT installed!\nThis may cause issues with the script!\nUse at your own peril or install busybox!"; sleep 2 63 | fi 64 | 65 | usage() { 66 | pcaut "\n" 67 | perr "Not enough parameters or parameter error!\n" 68 | pout "Unpack Image & Decompress Ramdisk : \n $($busybox basename $0) [image] [output dir]" 69 | pout " $($busybox basename $0) boot.img project_folder\n" 70 | pout "Repack Image & Compress Ramdisk : \n $($busybox basename $0) [unpacked dir] [new image]" 71 | pout " $($busybox basename $0) project_folder newboot.img\n" 72 | clean 73 | } 74 | 75 | print_info() { 76 | [[ $boot_magic_addr -gt 0 ]] && pout " boot magic : ANDROID!" 77 | [[ $boot_magic_addr -gt 0 ]] && pout " magic address : $boot_magic ($boot_magic_addr)" 78 | [ ! -z "$board" ] && pout " board : $board" 79 | pout " kernel : $kernel" 80 | pout " ramdisk : $ramdisk" 81 | pout " page size : $page_size" 82 | pout " kernel size : $kernel_size" 83 | pout " ramdisk size : $ramdisk_size" 84 | [ ! -z $second_size ] && [ $second_size -gt 0 ] && \ 85 | pout " second size : $second_size" 86 | [ $dtb_size -gt 0 ] && pout " dtb size : $dtb_size" 87 | if [ ! -z $base_warning ]; then 88 | pcaut " base : $base_addr (Non Standard)" 89 | else 90 | pout " base : $base_addr" 91 | fi 92 | if [ ! -z $kernel_offset_warning ]; then 93 | pcaut " kernel offset : $kernel_offset (Non Standard)" 94 | else 95 | pout " kernel offset : $kernel_offset" 96 | fi 97 | if [ ! -z $ramdisk_offset_warning ]; then 98 | pcaut " ramdisk offset : $ramdisk_offset (Non Standard)" 99 | else 100 | pout " ramdisk offset : $ramdisk_offset" 101 | fi 102 | if [ ! -z $second_offset_warning ]; then 103 | [ -z $second_offset ] || pcaut " second offset : $second_offset (Non Standard)" 104 | else 105 | [ -z $second_offset ] || pout " second offset : $second_offset" 106 | fi 107 | if [ ! -z $tags_offset_warning ]; then 108 | pcaut " tags offset : $tags_offset (Non Standard)" 109 | else 110 | pout " tags offset : $tags_offset" 111 | fi 112 | [ $dtb_size -gt 0 ] && pout " dtb offset : $qcdt_offset" 113 | [ $dtb_size -gt 0 ] && pout " dtb img : $dt" 114 | [ $second_size -gt 0 ] && pout " second img : $second" 115 | pout " cmd line : $cmd_line" 116 | } 117 | 118 | mkboot_img() { 119 | error=0 120 | [ $second_size -gt 0 ] && second="--second ${second}" 121 | [ $dtb_size -gt 0 ] && dtb="--dt ${dt}" 122 | [ ! -z $second_offset ] && second_offset="--second_offset ${second_offset}" 123 | [ ! -z "$board" ] && board="--board $board" 124 | 125 | $mkbootimg --kernel $kernel --ramdisk $ramdisk $board \ 126 | --base $base_addr --ramdisk_offset $ramdisk_offset \ 127 | --kernel_offset $kernel_offset $second_offset \ 128 | --tags_offset $tags_offset --cmdline "$cmd_line" \ 129 | --pagesize $page_size $second $dtb -o $new_img 2>/dev/null || error=1 130 | 131 | [ $error -eq 1 ] && return $error 132 | ramdisk_size=$($busybox stat -c "%s" $ramdisk) 133 | boot_size=$($busybox stat -c "%s" $new_img) 134 | pout "\nKernel size: $kernel_size, new ramdisk size: $ramdisk_size, $($busybox basename $new_img): $boot_size." 135 | pout "\n$($busybox basename $new_img) has been created.\n" 136 | 137 | # Check if new build is larger than original 138 | # Give caution if it is to insure size is not larger than the partition 139 | # A courtesy warning 140 | if [[ "$image_size" -lt "$boot_size" ]]; then 141 | beefed_up=$(( $boot_size - $image_size )) 142 | pcaut "\n****** CAUTION ******* CAUTION ******* CAUTION ******" 143 | pout "\n$($busybox basename $new_img) is $beefed_up bytes larger than" 144 | pout "the original build! Make sure this new" 145 | pout "size is not larger than the actual partition!" 146 | pcaut "\n****** CAUTION ******* CAUTION ******* CAUTION ******\n" 147 | fi 148 | } 149 | 150 | #decide action 151 | [ $# -lt 2 ] || [ $# -gt 3 ] && usage 152 | if [ $# -eq 2 ] && [ -d $1 ]; then 153 | mkboot_from_dir=1 154 | elif [ $# -eq 2 ] && [ -s $1 ]; then 155 | split_boot_to_dir=1 156 | else 157 | usage 158 | fi 159 | 160 | #mkboot_from_dir, img_info 161 | if [ ! -z $mkboot_from_dir ]; then 162 | pout "\nmkbootimg from $1/img_info.\n" 163 | unpacked_dir=$1 164 | new_img=$2 165 | cd $unpacked_dir 166 | if [ ! -s img_info ]; then 167 | perr "Missing img_info file! Can't rebuild $2." 168 | clean 169 | fi 170 | eval $(cat img_info) 171 | 172 | if [ -z $kernel ] || [ -z $ramdisk ] || [ -z $base_addr ]; then 173 | perr "Lacking parameters in img_info." 174 | clean 175 | fi 176 | [ -z $second_size ] && second_size=0 177 | [ -z $dtb_size ] && dtb_size=0 178 | 179 | if [ -d $ramdisk ]; then 180 | compression_type=$($file -m $magic ./ramdisk.* | $busybox cut -d: -f2 | $busybox cut -d" " -f2) 181 | case $compression_type in 182 | gzip) compression_warning=$compression_type; compression_ext=gzip; compression_repack=$gzip;; 183 | XZ) compression_warning=$compression_type; compression_ext=xz; compression_repack="$xz -1 --check=crc32";; 184 | LZMA) compression_warning=$compression_type; compression_ext=lzma; compression_repack="$xz --format=lzma";; 185 | LZ4) compression_warning=$compression_type; compression_ext=lz4; compression_repack="$lz4 -l -9";; 186 | lzop) compression_warning=$compression_type; compression_ext=lzop; compression_repack="$lzop -f -9";; 187 | esac; 188 | if [ -z $compression_warning ]; then 189 | perr "\n****** HAZARD ******* HAZARD ******* HAZARD ******" 190 | pout "\nRamdisk is $compression_type format. Can't repack ramdisk." 191 | pout "This tool currently does not support $compression_type." 192 | perr "\n****** HAZARD ******* HAZARD ******* HAZARD ******\n" 193 | exit 194 | fi 195 | if [ $compression_type != "gzip" ] && [ $compression_type != "LZMA" ] && [ $compression_type != "LZ4" ] && [ $compression_type != "lzop" ] && [ $compression_type != "XZ" ]; then 196 | perr "\nRamdisk is unknown format. Can't repack ramdisk." 197 | exit 0 198 | else 199 | # XZ GZIP LZMA LZ4 LZOP 200 | $mkbootfs $ramdisk | $compression_repack > new_ramdisk.$compression_ext 201 | ramdisk=new_ramdisk.$compression_ext 202 | ramdisk_size=$($busybox stat -c "%s" $ramdisk) 203 | fi 204 | fi 205 | #cd $unpacked_dir 206 | print_info 207 | $busybox rm -f $new_img 208 | mkboot_img $new_img || perr "Make $new_img Error! pls check img_info file." 209 | #pout "Add SEANDROIDENFORCE tag." 210 | #printf SEANDROIDENFORCE >> $new_img 211 | $busybox rm -f new_ramdisk.gz 212 | clean 213 | fi 214 | 215 | #split boot.img to dir. 216 | if [ -e $2 ]; then 217 | read -p "$2 exists, delete?(N/y)" reply 218 | case $reply in 219 | y | Y) 220 | $busybox rm -rf $2 221 | ;; 222 | *) 223 | exit 224 | ;; 225 | esac 226 | fi 227 | tempdir=$2 228 | $busybox mkdir -p "$tempdir" 229 | pout "\nUnpack & decompress $1 to $2\n" 230 | 231 | #get boot.img info 232 | cp -f $1 $tempdir/ 233 | cd $tempdir 234 | bootimg=$($busybox basename $1) 235 | # Find BOOT_MAGIC address in dec and hex 236 | boot_magic_addr=$($grep -abo ANDROID! $bootimg | $busybox cut -f 1 -d : | head -1) 237 | boot_magic=`printf 0x%08x $boot_magic_addr` 238 | # Find standard QCDT address in hex 239 | qcdt_addr=$($grep -abo QCDT $bootimg | $busybox cut -f 1 -d : | head -1) 240 | if [ ! -z $qcdt_addr ]; then 241 | qcdt_addr=`printf 0x%x $qcdt_addr` 242 | fi 243 | [ -z $boot_magic_addr ] && clean 244 | if [ $boot_magic_addr -gt 0 ]; then 245 | $busybox dd if=$bootimg of=bootimg bs=$boot_magic_addr skip=1 2>/dev/null 246 | bootimg=bootimg 247 | fi 248 | 249 | kernel_addr=0x$($od -A n -X -j 12 -N 4 $bootimg | $busybox sed 's/ //g' | $busybox sed 's/^0*//g') 250 | ramdisk_addr=0x$($od -A n -X -j 20 -N 4 $bootimg | $busybox sed 's/ //g' | $busybox sed 's/^0*//g') 251 | second_addr=0x$($od -A n -X -j 28 -N 4 $bootimg | $busybox sed 's/ //g' | $busybox sed 's/^0*//g') 252 | tags_addr=0x$($od -A n -X -j 32 -N 4 $bootimg | $busybox sed 's/ //g' | $busybox sed 's/^0*//g') 253 | 254 | kernel_size=$($od -A n -D -j 8 -N 4 $bootimg | $busybox sed 's/ //g') 255 | #base_addr=0x$($od -A n -x -j 14 -N 2 $bootimg | $busybox sed 's/ //g')0000 256 | ramdisk_size=$($od -A n -D -j 16 -N 4 $bootimg | $busybox sed 's/ //g') 257 | second_size=$($od -A n -D -j 24 -N 4 $bootimg | $busybox sed 's/ //g') 258 | page_size=$($od -A n -D -j 36 -N 4 $bootimg | $busybox sed 's/ //g') 259 | dtb_size=$($od -A n -D -j 40 -N 4 $bootimg | $busybox sed 's/ //g') 260 | #cmd_line=$($od -A n --strings -j 64 -N 512 $bootimg) 261 | #board=$($od -A n --strings -j 48 -N 16 $bootimg) 262 | cmd_line=$($od -A n -S1 -j 64 -N 512 $bootimg) 263 | board=$($od -A n -S1 -j 48 -N 16 $bootimg) 264 | 265 | base_addr=$((kernel_addr-0x00008000)) 266 | kernel_offset=$((kernel_addr-base_addr)) 267 | ramdisk_offset=$((ramdisk_addr-base_addr)) 268 | second_offset=$((second_addr-base_addr)) 269 | tags_offset=$((tags_addr-base_addr)) 270 | qcdt_offset=$((qcdt_addr-base_addr)) 271 | 272 | base_addr=$(printf "%08x" $base_addr) 273 | kernel_offset=$(printf "%08x" $kernel_offset) 274 | ramdisk_offset=$(printf "%08x" $ramdisk_offset) 275 | second_offset=$(printf "%08x" $second_offset) 276 | tags_offset=$(printf "%08x" $tags_offset) 277 | qcdt_offset=$(printf "%08x" $qcdt_offset) 278 | 279 | base_addr=0x${base_addr:0-8} 280 | kernel_offset=0x${kernel_offset:0-8} 281 | ramdisk_offset=0x${ramdisk_offset:0-8} 282 | second_offset=0x${second_offset:0-8} 283 | tags_offset=0x${tags_offset:0-8} 284 | qcdt_offset=0x${qcdt_offset:0-8} 285 | 286 | ######################################################### 287 | # BELOW SECTION HANDLES NON STANDARD IMAGES 288 | 289 | if [ $base_addr != 0x10000000 ]; then 290 | base_warning=$base_addr 291 | fi 292 | 293 | if [ $kernel_offset != 0x00008000 ]; then 294 | kernel_offset_warning=$kernel_offset 295 | fi 296 | 297 | if [ $ramdisk_offset != 0x01000000 ]; then 298 | ramdisk_offset_warning=$ramdisk_offset 299 | fi 300 | 301 | if [ $second_offset != 0x00f00000 ]; then 302 | second_offset_warning=$second_offset 303 | fi 304 | 305 | if [ $tags_offset != 0x00000100 ]; then 306 | tags_offset_warning=$tags_offset 307 | fi 308 | 309 | # Below are the known offsets for non standard mkbootimg.c 310 | if [[ ! -z $base_warning ]] || [[ ! -z $kernel_offset_warning ]] || [[ ! -z $ramdisk_offset_warning ]] || [[ ! -z $second_offset_warning ]] || [[ ! -z $tags_offset_warning ]]; then 311 | perr "****** WARNING ******* WARNING ******* WARNING ******\n" 312 | pout "This image is built using NON-standard mkbootimg!\n" 313 | fi 314 | if [ ! -z $base_warning ]; then 315 | pout "BASE is $base_warning" 316 | fi 317 | if [ ! -z $kernel_offset_warning ]; then 318 | pout "KERNEL_OFFSET is $kernel_offset_warning" 319 | fi 320 | if [ ! -z $ramdisk_offset_warning ]; then 321 | pout "RAMDISK_OFFSET is $ramdisk_offset_warning" 322 | fi 323 | if [ ! -z $second_offset_warning ]; then 324 | pout "SECOND_OFFSET is $second_offset_warning" 325 | fi 326 | if [ ! -z $tags_offset_warning ]; then 327 | pout "TAGS_OFFSET is $tags_offset_warning" 328 | fi 329 | if [[ ! -z $base_warning ]] || [[ ! -z $kernel_offset_warning ]] || [[ ! -z $ramdisk_offset_warning ]] || [[ ! -z $second_offset_warning ]] || [[ ! -z $tags_offset_warning ]]; then 330 | pout "\nYou can modify mkbootimg.c with the above value(s)" 331 | perr "\n****** WARNING ******* WARNING ******* WARNING ******\n" 332 | fi 333 | 334 | # ABOVE SECTION HANDLES NON STANDARD IMAGES 335 | ######################################################### 336 | 337 | k_count=$(((kernel_size+page_size-1)/page_size)) 338 | r_count=$(((ramdisk_size+page_size-1)/page_size)) 339 | s_count=$(((second_size+page_size-1)/page_size)) 340 | d_count=$(((dtb_size+page_size-1)/page_size)) 341 | k_offset=1 342 | r_offset=$((k_offset+k_count)) 343 | s_offset=$((r_offset+r_count)) 344 | d_offset=$((s_offset+s_count)) 345 | 346 | #zImage 347 | $busybox dd if=$bootimg of=zImage_tmp bs=$page_size skip=$k_offset count=$k_count 2>/dev/null 348 | $busybox dd if=zImage_tmp of=zImage bs=$kernel_size count=1 2>/dev/null 349 | 350 | #ramdisk.gz 351 | $busybox dd if=$bootimg of=ramdisk_tmp bs=$page_size skip=$r_offset count=$r_count 2>/dev/null 352 | $busybox dd if=ramdisk_tmp of=ramdisk.gz bs=$ramdisk_size count=1 2>/dev/null 353 | 354 | #second image 355 | if [ $second_size -gt 0 ]; then 356 | $busybox dd if=$bootimg of=second.img_tmp bs=$page_size skip=$s_offset count=$s_count 2>/dev/null 357 | $busybox dd if=second.img_tmp of=second.img bs=$second_size count=1 2>/dev/null 358 | second="$tempdir/second.img" 359 | second=$($busybox basename $second) 360 | secondb_name="second=$second" 361 | secondb_size="second_size=$second_size" 362 | fi 363 | 364 | #dtb 365 | if [ $dtb_size -gt 0 ]; then 366 | $busybox dd if=$bootimg of=dt.img_tmp bs=$page_size skip=$d_offset count=$d_count 2>/dev/null 367 | $busybox dd if=dt.img_tmp of=dt.img bs=$dtb_size count=1 2>/dev/null 368 | dt="$tempdir/dt.img" 369 | dt=$($busybox basename $dt) 370 | dt_name="dt=$dt" 371 | dt_size="dtb_size=$dtb_size" 372 | fi 373 | $busybox rm -f *_tmp $($busybox basename $1) $bootimg 374 | 375 | kernel=zImage 376 | ramdisk=ramdisk 377 | [ ! -s $kernel ] && clean 378 | #print boot.img info 379 | print_info 380 | 381 | # Properly escape double quotes 382 | # Keep double quotes intact 383 | cmd_line=$(echo $cmd_line | sed -e "s/'/'\\\\''/g; 1s/^/'/; \$s/\$/'/") 384 | 385 | #write info to img_info,decompression ramdisk.gz 386 | 387 | [ ! -z "$board" ] && liveboard="board=\"$board\"" 388 | 389 | $busybox printf "kernel=zImage\nramdisk=ramdisk\n${secondb_name}\n${dt_name}\npage_size=$page_size\n\ 390 | kernel_size=$kernel_size\nramdisk_size=$ramdisk_size\n${secondb_size}\n${dt_size}\nbase_addr=$base_addr\nkernel_offset=$kernel_offset\n\ 391 | ramdisk_offset=$ramdisk_offset\nsecond_offset=$second_offset\ntags_offset=$tags_offset\nqcdt_offset=$qcdt_offset\ncmd_line=$cmd_line\n$liveboard\n" > img_info 392 | 393 | # Include original image size in bytes to img_info 394 | # Allow script to read second command argument despite the path 395 | # This should help the script be allow to read images from another directory such as /sdcard/recovery.img, ~/sdcard/recovery.img or simply recovery.img if in the project directory itself 396 | if [ -f $1 ]; then 397 | # Path to file exists outside of project directory 398 | image_size=$($busybox stat -c "%s" $1) 399 | else 400 | # Path to file exists in the project directory 401 | image_size=$($busybox stat -c "%s" '../'$1) 402 | fi 403 | $busybox printf "image_size=$image_size" >> img_info 404 | 405 | $busybox mkdir ramdisk 406 | cd ramdisk 407 | 408 | compression_type=$($file -m $magic ../ramdisk.gz | $busybox cut -d: -f2 | $busybox cut -d" " -f2) 409 | compression_warning=$compression_type 410 | 411 | case $compression_type in 412 | gzip) compression_type=$gzip; compression_ext=gz;; 413 | XZ) compression_type=$xz; compression_ext=xz;; 414 | LZMA) compression_type=$lzma; compression_ext=lzma;; 415 | LZ4) compression_type=$lz4; compression_ext=lz4;; 416 | lzop) compression_type=$lzop; compression_ext=lzop;; 417 | esac; 418 | 419 | decomp_ramdisk="$compression_type -d -c" 420 | decomp_ramdisk2="$cpio -i -d -m --no-absolute-filenames" 421 | 422 | if [ -z $compression_ext ]; then 423 | perr "\n****** HAZARD ******* HAZARD ******* HAZARD ******" 424 | pout "\nRamdisk is $compression_warning format. Can't unpack ramdisk." 425 | pout "This tool currently does not support $compression_warning." 426 | perr "\n****** HAZARD ******* HAZARD ******* HAZARD ******\n" 427 | exit 428 | fi 429 | $busybox mv ../ramdisk.gz ../ramdisk.$compression_ext # This is simply to remind the user if they view the folder afterwards 430 | pout "\nRamdisk is $compression_warning format." 431 | $decomp_ramdisk "../ramdisk.$compression_ext" | $decomp_ramdisk2 $extra 432 | 433 | #Unpack Finish to exit. 434 | pout "Unpack completed.\n" 435 | exit 436 | -------------------------------------------------------------------------------- /ARM/xz: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/khanfrd/mkbootimg_tools/2d8905eb65b3b88490325d2afacd458d859006f6/ARM/xz -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | mkbootimg_tools 2 | =============== 3 | 4 | HOW TO USE: 5 | ----------- 6 | 7 | ### Unpack boot/recovery(.img) support dtb(dt.img): 8 | ./mkboot name.img namefolderout 9 | 10 | EXAMPLE 11 | ./mkboot recoveryksuamg5.img ksuamg 12 | Unpack & decompress recoveryksuamg5.img to ksuamg 13 | kernel : /home/xiaolu/work/initramfs/s4/e330s/ksuamg5/zImage 14 | ramdisk : /home/xiaolu/work/initramfs/s4/e330s/ksuamg5/ramdisk.gz 15 | page_size : 2048 16 | base_addr : 0x00000000 17 | kernel size : 6911360 18 | kernel_addr : 0x00008000 19 | ramdisk_size : 2685222 20 | ramdisk_addr : 0x02000000 21 | second_size : 0 22 | second_addr : 0x00f00000 23 | dtb_size : 1427456 24 | tags_addr : 0x01e00000 25 | cmdline : console=null androidboot.hardware=qcom user_debug=31 maxcpus=2 msm_rtb.filter=0x3F 26 | Unpack completed. 27 | 28 | ### Repack boot/recovery(.img) support dtb(dt.img): 29 | ./mkboot namefolderout newimgname.img 30 | 31 | EXAMPLE 32 | ./mkboot ksuamg5 recovery.img 33 | mkbootimg from ksuamg5/img_info. 34 | kernel : /home/xiaolu/work/initramfs/s4/e330s/ksuamg5/zImage 35 | ramdisk : /home/xiaolu/work/initramfs/s4/e330s/ksuamg5/new_ramdisk.gz 36 | page_size : 37 | base_addr : 0x00000000 38 | kernel size : 6911360 39 | kernel_addr : 0x00008000 40 | ramdisk_size : 2685222 41 | ramdisk_addr : 0x02000000 42 | second_size : 43 | second_addr : 44 | dtb_size : 1427456 45 | dtb_img : dt.img 46 | tags_addr : 0x01e00000 47 | cmdline : console=null androidboot.hardware=qcom user_debug=31 maxcpus=2 msm_rtb.filter=0x3F 48 | Kernel size: 6911360, new ramdisk size: 3416778, recovery.img: 11759616. 49 | recovery.img has been created. 50 | ... 51 | 52 | ### Create a dt.img: 53 | yourkernelsources/scripts/dtbTool -s 2048 -o arch/arm/boot/dt.img -p scripts/dtc/ arch/arm/boot/ 54 | 55 | EXAMPLE 56 | SHV-E330S_JB_Opensource/Kernel$ scripts/dtbTool -s 2048 -o arch/arm/boot/dt.img -p scripts/dtc/ arch/arm/boot/ 57 | DTB combiner: 58 | Input directory: '/media/diskd/kernel/SHV-E330S_JB_Opensource/Kernel/arch/arm/boot/' 59 | Output file: '/media/diskd/kernel/SHV-E330S_JB_Opensource/Kernel/arch/arm/boot/dt.img' 60 | Found file: msm8974-sec-ks01-r03.dtb ... chipset: 2114015745, platform: 3, rev: 0 61 | Found file: msm8974-sec-ks01-r07.dtb ... chipset: 2114015745, platform: 7, rev: 0 62 | Found file: msm8974-sec-ks01-r06.dtb ... chipset: 2114015745, platform: 6, rev: 0 63 | Found file: msm8974-sec-ks01-r04.dtb ... chipset: 2114015745, platform: 4, rev: 0 64 | Found file: msm8974-sec-ks01-r11.dtb ... chipset: 2114015745, platform: 11, rev: 0 65 | Found file: msm8974-sec-ks01-r02.dtb ... chipset: 2114015745, platform: 2, rev: 0 66 | Found file: msm8974-sec-ks01-r00.dtb ... chipset: 2114015745, platform: 0, rev: 0 67 | Found file: msm8974-sec-ks01-r05.dtb ... chipset: 2114015745, platform: 5, rev: 0 68 | Found file: msm8974-sec-ks01-r01.dtb ... chipset: 2114015745, platform: 1, rev: 0 69 | => Found 9 unique DTB(s) 70 | 71 | Generating master DTB... completed 72 | 73 | 74 | ### dtbToolCM support dt-tag & dtb v2/3(https://github.com/CyanogenMod/android_device_qcom_common/tree/cm-13.0/dtbtool): 75 | 76 | dtbToolCM -s 2048 -d "htc,project-id = <" -o arch/arm/boot/dt.img -p scripts/dtc/ arch/arm/boot/ 77 | 78 | -------------------------------------------------------------------------------- /dtbTool: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/khanfrd/mkbootimg_tools/2d8905eb65b3b88490325d2afacd458d859006f6/dtbTool -------------------------------------------------------------------------------- /dtbToolCM: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/khanfrd/mkbootimg_tools/2d8905eb65b3b88490325d2afacd458d859006f6/dtbToolCM -------------------------------------------------------------------------------- /dtbtool.txt: -------------------------------------------------------------------------------- 1 | Copyright (c) 2012-2014, The Linux Foundation. All rights reserved. 2 | 3 | Redistribution and use in source form and compiled forms (SGML, HTML, 4 | PDF, PostScript, RTF and so forth) with or without modification, are 5 | permitted provided that the following conditions are met: 6 | 7 | Redistributions in source form must retain the above copyright 8 | notice, this list of conditions and the following disclaimer as the 9 | first lines of this file unmodified. 10 | 11 | Redistributions in compiled form (transformed to other DTDs, 12 | converted to PDF, PostScript, RTF and other formats) must reproduce 13 | the above copyright notice, this list of conditions and the following 14 | disclaimer in the documentation and/or other materials provided with 15 | the distribution. 16 | 17 | THIS DOCUMENTATION IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED 18 | WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 19 | MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE AND 20 | NON-INFRINGEMENT ARE DISCLAIMED. IN NO EVENT SHALL THE FREEBSD 21 | DOCUMENTATION PROJECT BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 22 | SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 23 | LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 24 | DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 25 | THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 26 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 27 | OF THIS DOCUMENTATION, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH 28 | DAMAGE. 29 | 30 | 31 | Android - Table of Device Tree 32 | ============================== 33 | 34 | 0) Document revision 35 | v1.0 - Initial version (dng) 36 | v1.1 - Add v2 format to allow subtype (dng) 37 | 38 | 1) Android boot image: 39 | ---------------------- 40 | 1.1) Header: 41 | 1) Magic (8B) 42 | 2) kernel size (4B) 43 | 3) kernel addr (4B) 44 | 4) ramdisk size (4B) 45 | 5) ramdisk addr (4B) 46 | 6) 2ndary size (4B) 47 | 7) 2ndary addr (4B) 48 | 8) tags addr (4B) 49 | 9) page size (4B) 50 | 10) unused #1 (4B) (zero in standard Android) 51 | 11) unused #2 (4B) (zero in standard Android) 52 | 12) product name (16B) 53 | 13) kernel cmdline (512B) 54 | 14) id (8B) 55 | 56 | 1.2) Layout: 57 | A) header (as above - 1 page) 58 | B) kernel (n pages) 59 | C) ramdisk (m pages) 60 | D) second stage (o pages) 61 | 62 | 2) QC table of device tree 63 | -------------------------- 64 | 2.1) Changes: 65 | i) use "unused #1, #2" members in existing boot image 66 | header to point to new table of device tree 67 | (#1 - size of QC table of DT) 68 | ii) append table of device tree (described later) 69 | after "D) second stage" 70 | 71 | 2.2) Format: 72 | size 73 | x +------------------+ 74 | | | MAGIC ("QCDT") | 4B 75 | | +------------------+ 76 | header | VERSION | uint32 (version 3) 77 | | +------------------+ 78 | | | num of DTBs | uint32 (number of DTB entries) 79 | x +------------------+ 80 | | | platform id #1 | uint32 (e.g. ID for MSM8974) 81 | | +------------------+ 82 | | | variant id #1 | uint32 (e.g. ID for CDP, MTP) 83 | | +------------------+ 84 | | | subtype id #1 | uint32 (e.g. ID for subtype) (QCDT v2) 85 | device +------------------+ 86 | #1 | soc rev #1 | uint32 (e.g. MSM8974 v2) 87 | entry +------------------+ 88 | | | pmic0 #1 | uint32 (pmic0-> first smallest SID of existing pmic) 89 | | +------------------+ 90 | | | pmic1 #1 | uint32 (pmic1-> secondary smallest SID of existing pmic) 91 | | +------------------+ 92 | | | pmic2 #1 | uint32 (pmic2-> third smallest SID of existing pmic) 93 | | +------------------+ 94 | | | pmic3 #1 | uint32 (pmic3-> fourth smallest SID of existing pmic) 95 | | +------------------+ 96 | | | offset #1 | uint32 (byte offset from start/before MAGIC 97 | | +------------------+ to DTB entry) 98 | | | size #1 | uint32 (size in bytes of DTB blob) 99 | x +------------------+ 100 | . . 101 | . . (repeat) 102 | . . 103 | 104 | x +------------------+ 105 | | | platform id #Z | uint32 (e.g. ID for MSM8974) 106 | | +------------------+ 107 | device | variant id #Z | uint32 (e.g. ID for CDP, MTP) 108 | #Z +------------------+ 109 | entry | subtype id #Z | uint32 (e.g. ID for subtype) (QCDT v2) 110 | (last) +------------------+ 111 | | | soc rev #Z | uint32 (e.g. MSM8974 v2) 112 | | +------------------+ 113 | | | pmic0 #1 | uint32 (pmic0-> first smallest SID of existing pmic) 114 | | +------------------+ 115 | | | pmic1 #1 | uint32 (pmic1-> secondary smallest SID of existing pmic) 116 | | +------------------+ 117 | | | pmic2 #1 | uint32 (pmic2-> third smallest SID of existing pmic) 118 | | +------------------+ 119 | | | pmic3 #1 | uint32 (pmic3-> fourth smallest SID of existing pmic) 120 | | +------------------+ 121 | | | offset #Z | uint32 (byte offset from start/before MAGIC 122 | x +------------------+ to DTB entry) 123 | | 0 ("zero") | uint32 (end of list delimiter) 124 | +------------------+ to DTB entry) 125 | | padding | variable length for next DTB to start on 126 | +------------------+ page boundary 127 | | DTB #1 | variable (start is page aligned) 128 | | | 129 | | | 130 | +------------------+ 131 | | padding | variable length for next DTB to start on 132 | +------------------+ page boundary 133 | . 134 | . 135 | . 136 | 137 | +------------------+ 138 | | DTB #Z (last) | variable (start is page aligned) 139 | | | 140 | | | 141 | +------------------+ 142 | 143 | 3) Operations 144 | ------------- 145 | 3.1) Build-time: 146 | 1) Each DTS per device will add a "qcom,msm-id" entry 147 | e.g. for msm8974-sim.dts, add 148 | qcom,msm-id = ; 149 | or 150 | qcom,msm-id = ; 151 | qcom,board-id = ; 152 | or 153 | qcom,msm-id = ; 154 | qcom,board-id = ; 155 | qcom,pmic-id = ; 156 | x = ID for msm8974 157 | y = ID for CDP, MTP, etc. 158 | y' = ID for subtype (assumed zero if absent) 159 | z = ID for soc revision 160 | a = pmic0 161 | b = pmic1 162 | c = pmic2 163 | d = pmic3 164 | SBL populates the pmic entries always in ascending order of SID, so 165 | pmic0-> SID0, pmic1-> SID1, pmic2-> SID2, pmic3-> SID3. 166 | e.g. for qcom,pmic-id = 167 | Board X = MSM8994 + PM8994 + PMI8994 (Existing boards [ROW]) 168 | Board Y = MSM8994 + PM8994 + PMI8994 + PM8004 (Internal SS board variant) 169 | Board Z = MSM8994 + PM8994 + PM8004 (Boards that SS will be making) 170 | 171 | For all boards X, Y, and Z, PMICs have the following SIDs and REVID SUBTYPEs 172 | (i.e. PMIC Model): 173 | PM8994 - SID 0 and 1; subtype = 9 174 | PMI8994 - SID 2 and 3; subtype = 10 175 | PM8004 - SID 4 and 5; subtype = 12 176 | 177 | LK using SMEM PMIC info(1 as major and 0 as minor version for example): 178 | Board X: qcom,pmic-id = <0x0109 0x010A 0x0 0x0>; 179 | Board Y: qcom,pmic-id = <0x0109 0x010A 0x010C 0x0>; 180 | Board Z: qcom,pmic-id = <0x0109 0x010C 0x0 0x0>; 181 | 182 | The entry can optionally be an array: 183 | qcom,msm-id = , , ...; 184 | or 185 | qcom,msm-id = , , ...; 186 | qcom,board-id = , ...; 187 | or 188 | qcom,msm-id = , , ...; 189 | qcom,board-id = , ...; 190 | qcom,pmic-id = , ...; 191 | Note that qcom,msm-id, qcom,board-id and qcom,pmic-id are not matched pairs. 192 | 2) Kernel compile will generate the DTB 193 | 3) Android build will run a new tool (dtbTool) 194 | a) scan the DTB output directory for all compiled DTB 195 | b) decompile the DTB for "qcom,msm-id"/"qcom,board-id"/"qcom,pmic-id" 196 | c) generate the QC table of device tree in sorted 197 | order (platform, variant, subtype, soc rev, pmic0, pmic1, pmic2, pmic3) 198 | d) modified mkbootimg will merge new table of DT 199 | 200 | 3.2) Run-time: 201 | 1) LK bootloader will obtain platform id/variant/subtype/soc rev/major ver/minor ver 202 | /pmic0/pmic1/pmic2/pmic3 info either from early bootloaders or via other means 203 | 2) LK bootloader will check entries #10 for non-zero 204 | value (set to zero for standard boot.img). If the 205 | value is non-zero, refer to page section after 206 | the "second stage" in the boot.img layout 207 | 3) Check QCDT magic 208 | 4) Check QCDT version (optional LK to handle multiple 209 | QCDT version) 210 | 5) LK scans through the QCDT table to look for matching 211 | entry. Search order is: 212 | 1) msm ID exact match 213 | 2) Platform type exact match 214 | 3) subtype ID exact match 215 | 4) HLOS subtype exact match 216 | 5) Pmic0 model ID exact match 217 | 6) Pmic1 model ID exact match 218 | 7) Pmic2 model ID exact match 219 | 8) Pmic3 model ID exact match 220 | 9) foundry ID, look for exact match, if not found choose 221 | device tree with foundry-id(0x0) 222 | 10) select the highest soc rev in QCDT that is 223 | equal to or lower than the runtime detected soc rev 224 | 11) select the highest major&minor ver in QCDT that is 225 | equal to or lower than the runtime detected major ver 226 | 12) select the highest pmic0 major&minor in QCDT that is 227 | equal to or lower than the runtime detected pmic0 228 | 13) select the highest pmic1 major&minor in QCDT that is 229 | equal to or lower than the runtime detected pmic1 230 | 14) select the highest pmic2 major&minor in QCDT that is 231 | equal to or lower than the runtime detected pmic2 232 | 15) select the highest pmic3 major&minor in QCDT that is 233 | equal to or lower than the runtime detected pmic3 234 | 6) Load the matching DTB blob to the tags addr 235 | 7) LK pass the correct DTB to the kernel 236 | -------------------------------------------------------------------------------- /dtc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/khanfrd/mkbootimg_tools/2d8905eb65b3b88490325d2afacd458d859006f6/dtc -------------------------------------------------------------------------------- /lz4: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/khanfrd/mkbootimg_tools/2d8905eb65b3b88490325d2afacd458d859006f6/lz4 -------------------------------------------------------------------------------- /mkboot: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | # 15 | #boot.img tool 16 | #by xiaolu 17 | trap "clean" 2 3 4 18 | workdir=$(pwd) 19 | toolpath=$(readlink -f $0) 20 | tooldir=$(dirname $toolpath) 21 | mkbootimg=$tooldir/mkbootimg 22 | mkbootfs=$tooldir/mkbootfs 23 | C_OUT="\033[0;1m" 24 | C_ERR="\033[31;1m" 25 | C_CLEAR="\033[0;0m" 26 | 27 | pout() { 28 | printf "${C_OUT}${*}${C_CLEAR}\n" 29 | } 30 | perr() { 31 | printf "${C_ERR}${*}${C_CLEAR}\n" 32 | } 33 | clean() 34 | { 35 | rm -rf /tmp/mkboot.* 36 | pout "..." 37 | exit 38 | } 39 | 40 | unpack_complete() 41 | { 42 | [ ! -z $format ] && echo format=$format >> ../img_info 43 | pout "Unpack completed." 44 | exit 45 | } 46 | 47 | zip_command() 48 | { 49 | if [ "$1" == lzop ]; then 50 | zcommand="lzop -n -f -9" 51 | elif [ "$1" == lz4 ]; then 52 | zcommand="lz4 -f -9" 53 | elif [ "$1" == lzma ]; then 54 | zcommand="lzma -f -c" 55 | elif [ "$1" == xz ]; then 56 | zcommand="xz -f -c" 57 | else 58 | zcommand="gzip -n -f" 59 | fi 60 | } 61 | 62 | usage() 63 | { 64 | pout "" 65 | pout "----------------------------------------------------------------------" 66 | pout "Not enough parameters or parameter error!" 67 | pout "unpack boot.img & decompress ramdisk:\n $(basename $0) [img] [output dir]" 68 | pout " $(basename $0) boot.img boot20130905" 69 | pout "Use the unpacked directory repack boot.img(img_info):\n $(basename $0) [unpacked dir] [newbootfile]" 70 | pout " $(basename $0) boot20130905 newboot.img" 71 | clean 72 | } 73 | 74 | print_info() 75 | { 76 | [ ! -z "$board" ] && pout " board : $board" 77 | pout " kernel : $kernel" 78 | pout " ramdisk : $ramdisk" 79 | pout " page size : $page_size" 80 | pout " kernel size : $kernel_size" 81 | pout " ramdisk size : $ramdisk_size" 82 | [ ! -z $second_size ] && [ $second_size -gt 0 ] && \ 83 | pout " second_size : $second_size" 84 | [ $dtb_size -gt 0 ] && pout " dtb size : $dtb_size" 85 | pout " base : $base_addr" 86 | pout " kernel offset : $kernel_offset" 87 | pout " ramdisk offset : $ramdisk_offset" 88 | [ ! -z $second_size ] && [ $second_size -gt 0 ] && \ 89 | pout " second_offset : $second_offset" 90 | pout " tags offset : $tags_offset" 91 | [ $dtb_size -gt 0 ] && pout " dtb img : $dt" 92 | pout " cmd line : $cmd_line" 93 | } 94 | 95 | mkboot_img() 96 | { 97 | error=0 98 | [ $second_size -gt 0 ] && second="--second ${second}" 99 | [ $dtb_size -gt 0 ] && dtb="--dt ${dt}" 100 | 101 | $mkbootimg --kernel $kernel --ramdisk $ramdisk --board "$board" \ 102 | --base $base_addr --ramdisk_offset $ramdisk_offset \ 103 | --tags_offset $tags_offset --cmdline "$cmd_line" \ 104 | --pagesize $page_size $second $dtb -o $1 || error=1 105 | 106 | [ $error -eq 1 ] && return $error 107 | ramdisk_size=$(stat -c "%s" $ramdisk) 108 | boot_size=$(stat -c "%s" $1) 109 | pout "Kernel size: $kernel_size, new ramdisk size: $ramdisk_size, $(basename $1): $boot_size." 110 | pout "$(basename $1) has been created." 111 | } 112 | 113 | #decide action 114 | [ $# -lt 2 ] || [ $# -gt 3 ] && usage 115 | if [ $# -eq 2 ] && [ -d $1 ]; then 116 | mkboot_from_dir=1 117 | elif [ $# -eq 2 ] && [ -s $1 ]; then 118 | split_boot_to_dir=1 119 | else 120 | usage 121 | fi 122 | 123 | #mkboot_from_dir, img_info 124 | if [ ! -z $mkboot_from_dir ]; then 125 | pout "mkbootimg from $1/img_info." 126 | unpacked_dir=$(readlink -f $1) 127 | new_img=$(readlink -f $2) 128 | cd $unpacked_dir 129 | if [ ! -s img_info ]; then 130 | pout "not found img_info file! can't rebuild img." 131 | clean 132 | fi 133 | eval $(cat img_info) 134 | if [ -z $kernel ] || [ -z $ramdisk ] || [ -z $base_addr ]; then 135 | pout "img_info file have not enough parameters." 136 | clean 137 | fi 138 | [ -z $second_size ] && second_size=0 139 | [ -z $dtb_size ] && dtb_size=0 140 | if [ -d $ramdisk ]; then 141 | [ -z $format ] && format=gzip 142 | zip_command $format 143 | #cd $ramdisk; find . | fakeroot cpio -R 0:0 -H newc -o 2>/dev/null \ 144 | # | $zcommand > $unpacked_dir/new_ramdisk; cd $unpacked_dir 145 | $mkbootfs $ramdisk | $zcommand > new_ramdisk 146 | ramdisk=new_ramdisk 147 | ramdisk_size=$(stat -c "%s" $ramdisk) 148 | fi 149 | print_info 150 | pout "ramdisk is $format format." 151 | rm -f $new_img 152 | mkboot_img $new_img || perr "Make boot.img Error! pls check img_info file." 153 | #pout "Add SEANDROIDENFORCE tag." 154 | #printf SEANDROIDENFORCE >> $new_img 155 | rm -f new_ramdisk 156 | clean 157 | fi 158 | 159 | #split boot.img to dir. 160 | if [ -e $2 ]; then 161 | read -p "$2 exists, delete?(N/y)" reply 162 | case $reply in 163 | y | Y) 164 | rm -rf $2 165 | ;; 166 | *) 167 | exit 168 | ;; 169 | esac 170 | fi 171 | tempdir="$(readlink -f $2)" 172 | mkdir -p $tempdir 173 | pout "Unpack & decompress $1 to $2" 174 | 175 | #get boot.img info 176 | cp -f $1 $tempdir/ 177 | cd $tempdir 178 | bootimg=$(basename $1) 179 | offset=$(grep -abo ANDROID! $bootimg | cut -f 1 -d :) 180 | [ -z $offset ] && clean 181 | if [ $offset -gt 0 ]; then 182 | dd if=$bootimg of=bootimg bs=$offset skip=1 2>/dev/null 183 | bootimg=bootimg 184 | fi 185 | 186 | kernel_addr=0x$(od -A n -X -j 12 -N 4 $bootimg | sed 's/ //g' | sed 's/^0*//g') 187 | ramdisk_addr=0x$(od -A n -X -j 20 -N 4 $bootimg | sed 's/ //g' | sed 's/^0*//g') 188 | second_addr=0x$(od -A n -X -j 28 -N 4 $bootimg | sed 's/ //g' | sed 's/^0*//g') 189 | tags_addr=0x$(od -A n -X -j 32 -N 4 $bootimg | sed 's/ //g' | sed 's/^0*//g') 190 | 191 | kernel_size=$(od -A n -D -j 8 -N 4 $bootimg | sed 's/ //g') 192 | #base_addr=0x$(od -A n -x -j 14 -N 2 $bootimg | sed 's/ //g')0000 193 | ramdisk_size=$(od -A n -D -j 16 -N 4 $bootimg | sed 's/ //g') 194 | second_size=$(od -A n -D -j 24 -N 4 $bootimg | sed 's/ //g') 195 | page_size=$(od -A n -D -j 36 -N 4 $bootimg | sed 's/ //g') 196 | dtb_size=$(od -A n -D -j 40 -N 4 $bootimg | sed 's/ //g') 197 | #cmd_line=$(od -A n --strings -j 64 -N 512 $bootimg) 198 | #board=$(od -A n --strings -j 48 -N 16 $bootimg) 199 | cmd_line=$(od -A n -S1 -j 64 -N 512 $bootimg) 200 | board=$(od -A n -S1 -j 48 -N 16 $bootimg) 201 | 202 | base_addr=$((kernel_addr-0x00008000)) 203 | kernel_offset=$((kernel_addr-base_addr)) 204 | ramdisk_offset=$((ramdisk_addr-base_addr)) 205 | second_offset=$((second_addr-base_addr)) 206 | tags_offset=$((tags_addr-base_addr)) 207 | 208 | base_addr=$(printf "%08x" $base_addr) 209 | kernel_offset=$(printf "%08x" $kernel_offset) 210 | ramdisk_offset=$(printf "%08x" $ramdisk_offset) 211 | second_offset=$(printf "%08x" $second_offset) 212 | tags_offset=$(printf "%08x" $tags_offset) 213 | 214 | base_addr=0x${base_addr:0-8} 215 | kernel_offset=0x${kernel_offset:0-8} 216 | ramdisk_offset=0x${ramdisk_offset:0-8} 217 | second_offset=0x${second_offset:0-8} 218 | tags_offset=0x${tags_offset:0-8} 219 | 220 | k_count=$(((kernel_size+page_size-1)/page_size)) 221 | r_count=$(((ramdisk_size+page_size-1)/page_size)) 222 | s_count=$(((second_size+page_size-1)/page_size)) 223 | d_count=$(((dtb_size+page_size-1)/page_size)) 224 | k_offset=1 225 | r_offset=$((k_offset+k_count)) 226 | s_offset=$((r_offset+r_count)) 227 | d_offset=$((s_offset+s_count)) 228 | 229 | #kernel 230 | dd if=$bootimg of=kernel_tmp bs=$page_size skip=$k_offset count=$k_count 2>/dev/null 231 | dd if=kernel_tmp of=kernel bs=$kernel_size count=1 2>/dev/null 232 | #ramdisk.packed 233 | dd if=$bootimg of=ramdisk_tmp bs=$page_size skip=$r_offset count=$r_count 2>/dev/null 234 | dd if=ramdisk_tmp of=ramdisk.packed bs=$ramdisk_size count=1 2>/dev/null 235 | #second 236 | if [ $second_size -gt 0 ]; then 237 | dd if=$bootimg of=second.img.tmp bs=$page_size skip=$s_offset count=$s_count 2>/dev/null 238 | dd if=second.img.tmp of=second.img bs=$second_size count=1 2>/dev/null 239 | s_name="second=second.img\n" 240 | s_size="second_size=$second_size\n" 241 | fi 242 | #dtb 243 | if [ $dtb_size -gt 0 ]; then 244 | dd if=$bootimg of=dt.img_tmp bs=$page_size skip=$d_offset count=$d_count 2>/dev/null 245 | dd if=dt.img_tmp of=dt.img bs=$dtb_size count=1 2>/dev/null 246 | dt="$tempdir/dt.img" 247 | dt=$(basename $dt) 248 | dt_name="dt=$dt\n" 249 | dt_size="dtb_size=$dtb_size\n" 250 | fi 251 | rm -f *_tmp $(basename $1) $bootimg 252 | 253 | kernel=kernel 254 | ramdisk=ramdisk 255 | [ ! -s $kernel ] && clean 256 | #print boot.img info 257 | print_info 258 | 259 | esq="'\"'\"'" 260 | escaped_cmd_line=`echo $cmd_line | sed "s/'/$esq/g"` 261 | 262 | #write info to img_info,decompression ramdisk.packed 263 | printf "kernel=kernel\nramdisk=ramdisk\n${s_name}${dt_name}page_size=$page_size\n\ 264 | kernel_size=$kernel_size\nramdisk_size=$ramdisk_size\n${s_size}${dt_size}base_addr=$base_addr\nkernel_offset=$kernel_offset\n\ 265 | ramdisk_offset=$ramdisk_offset\ntags_offset=$tags_offset\ncmd_line=\'$escaped_cmd_line\'\nboard=\"$board\"\n" > img_info 266 | mkdir ramdisk 267 | cd ramdisk 268 | 269 | gzip -t ../ramdisk.packed 2>/dev/null 270 | if [ $? -eq 0 ]; then 271 | pout "ramdisk is gzip format." 272 | format=gzip 273 | gzip -d -c ../ramdisk.packed | cpio -i -d -m --no-absolute-filenames 2>/dev/null 274 | unpack_complete 275 | fi 276 | lzma -t ../ramdisk.packed 2>/dev/null 277 | if [ $? -eq 0 ]; then 278 | pout "ramdisk is lzma format." 279 | format=lzma 280 | lzma -d -c ../ramdisk.packed | cpio -i -d -m --no-absolute-filenames 2>/dev/null 281 | unpack_complete 282 | fi 283 | xz -t ../ramdisk.packed 2>/dev/null 284 | if [ $? -eq 0 ]; then 285 | pout "ramdisk is xz format." 286 | format=xz 287 | xz -d -c ../ramdisk.packed | cpio -i -d -m --no-absolute-filenames 2>/dev/null 288 | unpack_complete 289 | fi 290 | lzop -t ../ramdisk.packed 2>/dev/null 291 | if [ $? -eq 0 ]; then 292 | pout "ramdisk is lzo format." 293 | format=lzop 294 | lzop -d -c ../ramdisk.packed | cpio -i -d -m --no-absolute-filenames 2>/dev/null 295 | unpack_complete 296 | fi 297 | $tooldir/lz4 -d ../ramdisk.packed 2>/dev/null | cpio -i -d -m --no-absolute-filenames 2>/dev/null 298 | if [ $? -eq 0 ]; then 299 | pout "ramdisk is lz4 format." 300 | format=lz4 301 | else 302 | pout "ramdisk is unknown format,can't unpack ramdisk" 303 | fi 304 | unpack_complete 305 | 306 | -------------------------------------------------------------------------------- /mkbootfs: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/khanfrd/mkbootimg_tools/2d8905eb65b3b88490325d2afacd458d859006f6/mkbootfs -------------------------------------------------------------------------------- /mkbootimg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/khanfrd/mkbootimg_tools/2d8905eb65b3b88490325d2afacd458d859006f6/mkbootimg --------------------------------------------------------------------------------