├── README ├── build └── update │ └── META-INF │ └── com │ └── google │ └── android │ └── update-binary ├── extract_initramfs.sh ├── repack-zImage.sh └── resources └── blankfile /README: -------------------------------------------------------------------------------- 1 | ############################################################################### 2 | # # 3 | # Initramfs utilities for Samsung Galaxy S based phones. 4 | # # 5 | # Released under the GPLv3 # 6 | # # 7 | # This program is free software: you can redistribute it and/or modify # 8 | # it under the terms of the GNU General Public License as published by # 9 | # the Free Software Foundation, either version 3 of the License, or # 10 | # (at your option) any later version. # 11 | # # 12 | # This program is distributed in the hope that it will be useful, # 13 | # but WITHOUT ANY WARRANTY; without even the implied warranty of # 14 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # 15 | # GNU General Public License for more details. # 16 | # # 17 | # You should have received a copy of the GNU General Public License # 18 | # along with this program. If not, see . # 19 | # # 20 | ############################################################################### 21 | -------------------------------------------------------------------------------- /build/update/META-INF/com/google/android/update-binary: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/atinm/initramfs-utils/4784be5fa292d4a0e1e1ea9abd5779e6bde3b495/build/update/META-INF/com/google/android/update-binary -------------------------------------------------------------------------------- /extract_initramfs.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | zImage=$1 3 | #======================================================== 4 | # find start of gziped kernel object in the zImage file: 5 | #======================================================== 6 | pos=`grep -P -a -b -m 1 --only-matching $'\x1F\x8B\x08' $zImage | cut -f 1 -d :` 7 | echo "-I- Extracting kernel image from $zImage (start = $pos)" 8 | 9 | #======================================================================== 10 | # the cpio archive might be compressed too, so two gunzips could be needed: 11 | #======================================================================== 12 | dd if=$zImage bs=1 skip=$pos | gunzip > /tmp/kernel.img 13 | pos=`grep -P -a -b -m 1 --only-matching $'\x1F\x8B\x08' /tmp/kernel.img | cut -f 1 -d :` 14 | #=========================================================================== 15 | # find start and end of the "cpio" initramfs image inside the kernel object: 16 | # ASCII cpio header starts with '070701' 17 | # The end of the cpio archive is marked with an empty file named TRAILER!!! 18 | #=========================================================================== 19 | if [ ! $pos = "" ]; then 20 | echo "-I- Extracting compressed cpio image from kernel image (start = $pos)" 21 | dd if=/tmp/kernel.img bs=1 skip=$pos | gunzip > /tmp/cpio.img 22 | start=`grep -a -b -m 1 --only-matching '070701' /tmp/cpio.img | head -1 | cut -f 1 -d :` 23 | end=`grep -a -b --only-matching 'TRAILER!!!' /tmp/cpio.img | tail -1 | cut -f 1 -d :` 24 | inputfile=/tmp/cpio.img 25 | else 26 | echo "-I- Already uncompressed cpio.img, not decompressing" 27 | start=`grep -a -b -m 1 --only-matching '070701' /tmp/kernel.img | head -1 | cut -f 1 -d :` 28 | end=`grep -a -b --only-matching 'TRAILER!!!' /tmp/kernel.img | tail -1 | cut -f 1 -d :` 29 | inputfile=/tmp/kernel.img 30 | fi 31 | 32 | end=$(($end+10)) 33 | count=$(($end-$start)) 34 | if (($count < 0)); then 35 | echo "-E- Couldn't match start/end of the initramfs image." 36 | exit 37 | fi 38 | echo "-I- Extracting initramfs image from $inputfile (start = $start, end = $end)" 39 | dd if=$inputfile bs=1 skip=$start count=$count > initramfs.cpio 40 | -------------------------------------------------------------------------------- /repack-zImage.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | ############################################################################## 3 | # usage : ./repack.sh [kernel] [initramfs_direcotry] [kernel source dir] "title of build" 4 | # example : ./repack.sh /data/android/initramfs-utils/zImage /data/android/captivate-initramfs \ 5 | # /data/android/linux-2.6.32 "title of build" 6 | # based on editor.sh from dkcldark @ xda 7 | ############################################################################## 8 | set -x 9 | # you should point where your cross-compiler is 10 | COMPILER_PATH="${HOME}/arm-none-eabi-4.3.4/bin" 11 | COMPILER="$COMPILER_PATH/arm-none-eabi" 12 | ############################################################################## 13 | 14 | zImage=$1 15 | new_ramdisk_dir=$2 16 | KSRC=$3 17 | TITLE=$4 18 | determiner=0 19 | Image_here="./out/Image" 20 | MKZIP='7z -mx9 -mmt=1 a "$OUTFILE" .' 21 | TARGET_DEVICE_NAME=SGH-I897 22 | DATE=`date` 23 | 24 | #load local definitions for unpack_hook, apply_hook, prepare_hook, post_hook if any 25 | test -f ./locals.sh && source ./locals.sh 26 | 27 | # you should point this where your AOSP root is in the environment 28 | [ -z "$AOSP" ] && AOSP="/data/android/cm" 29 | 30 | write_script() { 31 | test -n "$TARGET_DEVICE_NAME" && \ 32 | echo "assert(getprop(\"ro.product.device\") == \"$TARGET_DEVICE_NAME\" || getprop(\"ro.build.product\") == \"$TARGET_DEVICE_NAME\");" 33 | declare -f pre_hook >/dev/null 2>&1 && \ 34 | pre_hook 35 | title="ui_print(\"** $TITLE \");" 36 | echo $title 37 | echo 'ui_print("** atinm @ xda-developers");' 38 | date="ui_print(\"** Build date: $DATE \");" 39 | echo $date 40 | echo 'ui_print("-");' 41 | echo 'ui_print("-");' 42 | echo 'ui_print("Unpacking files...");' 43 | declare -f unpack_hook >/dev/null 2>&1 && \ 44 | unpack_hook 45 | echo 'package_extract_dir("tmp", "/tmp");' 46 | declare -f apply_hook >/dev/null 2>&1 && \ 47 | apply_hook 48 | echo 'ui_print("-");' 49 | echo 'ui_print("-");' 50 | declare -f post_hook >/dev/null 2>&1 && \ 51 | post_hook 52 | echo 'ui_print("** Done!");' 53 | return 0 54 | } 55 | 56 | prepare_update() { 57 | rm -r build/update/tmp 58 | mkdir -p build/update/tmp 59 | cp -a out/zImage build/update/tmp 60 | write_script >build/update/META-INF/com/google/android/updater-script 61 | declare -f prepare_hook >/dev/null 2>&1 && \ 62 | prepare_hook 63 | return 0 64 | } 65 | 66 | if [ -z $1 ]; then 67 | echo "##### You should point where the zImage file is in arg 1 #####" 68 | exit 69 | elif [ -z $2 ]; then 70 | echo "##### You should point where your new initramfs is in arg 2 #####" 71 | exit 72 | elif [ -z $3 ]; then 73 | echo "##### You should point where your kernel source is in arg 3 #####" 74 | exit 75 | elif [ -z "$4" ]; then 76 | echo "##### You should specify the title for this update in arg 4 #####" 77 | exit 78 | fi 79 | echo "##### My name is $0 #####" 80 | echo "##### The kernel is $1 #####" 81 | echo "##### The ramdisk is $2 #####" 82 | echo "##### The kernel source is $3 #####" 83 | echo "##### The title for this update is $4 #####" 84 | 85 | #======================================================= 86 | # find start of gziped kernel object in the zImage file: 87 | #======================================================= 88 | rm -rf out 89 | mkdir out 90 | 91 | pos=`grep -P -a -b -m 1 --only-matching '\x1F\x8B\x08' $zImage | cut -f 1 -d :` 92 | echo "##### 01. Extracting boot header from $zImage (start=0, count=$pos)" 93 | dd if=$zImage bs=1 count=$pos of=out/boot.img 94 | 95 | echo "##### 03. Extracting kernel from $zImage (start = $pos)" 96 | dd if=$zImage bs=1 skip=$pos | gunzip > $Image_here 97 | 98 | pos=`grep -P -a -b -m 1 --only-matching $'\x1F\x8B\x08' $Image_here | cut -f 1 -d :` 99 | if [ ! $pos = "" ]; then 100 | echo "##### ERROR: Cannot handle a compressed cpio image in the kernel image" 101 | exit 102 | fi 103 | 104 | #=========================================================================== 105 | # find start and end of the "cpio" initramfs inside the kernel object: 106 | # ASCII cpio header starts with '070701' 107 | # The end of the cpio archive is marked with an empty file named TRAILER!!! 108 | #=========================================================================== 109 | start=`grep -a -b -m 1 --only-matching '070701' $Image_here | head -1 | cut -f 1 -d :` 110 | end=`grep -a -b -m 1 --only-matching 'TRAILER!!!' $Image_here | head -1 | cut -f 1 -d :` 111 | end=$((end + 10)) 112 | count=$((end - start)) 113 | 114 | if [ $count -lt $determiner ]; then 115 | echo "##### ERROR : Couldn't match start/end of the initramfs ." 116 | exit 117 | fi 118 | 119 | # Check the Image's size 120 | filesize=`ls -l $Image_here | awk '{print $5}'` 121 | echo "##### 03. The size of the Image is $filesize" 122 | 123 | # Split the Image #1 -> head.img 124 | echo "##### 04. Making a head.img ( from 0 ~ $start )" 125 | dd if=$Image_here bs=1 count=$start of=out/head.img 126 | 127 | # Split the Image #2 -> tail.img 128 | echo "##### 05. Making a tail.img ( from $end ~ $filesize )" 129 | dd if=$Image_here bs=1 skip=$end of=out/tail.img 130 | 131 | # Create the cpio archive 132 | OUT=`pwd`/out 133 | pushd $new_ramdisk_dir 134 | find ./ | grep -v ".gitignore" | cpio -o -H newc > $OUT/new_ramdisk.cpio 135 | popd 136 | 137 | # Check the new ramdispk's size 138 | ramdsize=`ls -l out/new_ramdisk.cpio | awk '{print $5}'` 139 | echo "##### 06. The size of the new ramdisk is = $ramdsize" 140 | 141 | echo "##### 07. Checking if ramdsize is bigger than the stock one" 142 | if [ $ramdsize -gt $count ]; then 143 | echo "##### Your initramfs needs to be gzipped!! ###" 144 | cp out/new_ramdisk.cpio out/ramdisk.backup 145 | cat out/ramdisk.backup | gzip -f -9 > out/ramdisk.cpio 146 | else 147 | cp out/new_ramdisk.cpio out/ramdisk.cpio 148 | fi 149 | 150 | # FrankenStein is being made #1 151 | echo "##### 08. Merging head + ramdisk" 152 | cat out/head.img out/ramdisk.cpio > out/franken.img 153 | 154 | echo "##### 09. Checking the size of [head+ramdisk]" 155 | franksize=`ls -l out/franken.img | awk '{print $5}'` 156 | 157 | # FrankenStein is being made #2 158 | echo "##### 10. Merging [head+ramdisk] + padding + tail" 159 | if [ $franksize -le $end ]; then 160 | tempnum=$((end - franksize)) 161 | dd if=resources/blankfile bs=1 count=$tempnum of=out/padding 162 | cat out/padding out/tail.img > out/newtail.img 163 | cat out/franken.img out/newtail.img > out/new_Image 164 | else 165 | echo "##### ERROR : Your initramfs is still BIGGER than the stock initramfs $franksize > $end #####" 166 | exit 167 | fi 168 | 169 | #============================================ 170 | # rebuild zImage 171 | #============================================ 172 | echo "##### Now we are rebuilding the zImage #####" 173 | 174 | cp out/new_Image $KSRC/arch/arm/boot/Image 175 | pushd $KSRC 176 | 177 | #1. Image -> piggy.gz 178 | echo "##### 11. Image ---> piggy.gz" 179 | gzip -f -9 < arch/arm/boot/compressed/../Image > arch/arm/boot/compressed/piggy.gz 180 | 181 | #2. piggy.gz -> piggy.o 182 | echo "##### 12. piggy.gz ---> piggy.o" 183 | $COMPILER-gcc -Wp,-MD,arch/arm/boot/compressed/.piggy.o.d -nostdinc -isystem $COMPILER_PATH/../lib/gcc/arm-none-eabi/4.3.4/include -Dlinux -Iinclude -Iarch/arm/include -include include/linux/autoconf.h -D__KERNEL__ -mlittle-endian -Iarch/arm/mach-s5pc110/include -Iarch/arm/plat-s5pc11x/include -Iarch/arm/plat-s3c/include -D__ASSEMBLY__ -mabi=aapcs-linux -mno-thumb-interwork -D__LINUX_ARM_ARCH__=7 -mcpu=cortex-a8 -msoft-float -gdwarf-2 -Wa,-march=all -c -o arch/arm/boot/compressed/piggy.o arch/arm/boot/compressed/piggy.S 184 | 185 | #3. head.o 186 | echo "##### 13. Compiling head" 187 | $COMPILER-gcc -Wp,-MD,arch/arm/boot/compressed/.head.o.d -nostdinc -isystem $COMPILER_PATH/../lib/gcc/arm-none-eabi/4.3.4/include -Dlinux -Iinclude -Iarch/arm/include -include include/linux/autoconf.h -D__KERNEL__ -mlittle-endian -Iarch/arm/mach-s5pc110/include -Iarch/arm/plat-s5pc11x/include -Iarch/arm/plat-s3c/include -D__ASSEMBLY__ -mabi=aapcs-linux -mno-thumb-interwork -D__LINUX_ARM_ARCH__=7 -mcpu=cortex-a8 -msoft-float -gdwarf-2 -Wa,-march=all -c -o arch/arm/boot/compressed/head.o arch/arm/boot/compressed/head.S 188 | 189 | #4. misc.o 190 | echo "##### 14. Compiling misc" 191 | $COMPILER-gcc -Wp,-MD,arch/arm/boot/compressed/.misc.o.d -nostdinc -isystem $COMPILER_PATH/../lib/gcc/arm-none-eabi/4.3.4/include -Dlinux -Iinclude -Iarch/arm/include -include include/linux/autoconf.h -D__KERNEL__ -mlittle-endian -Iarch/arm/mach-s5pc110/include -Iarch/arm/plat-s5pc11x/include -Iarch/arm/plat-s3c/include -Wall -Wundef -Wstrict-prototypes -Wno-trigraphs -fno-strict-aliasing -fno-common -Werror-implicit-function-declaration -Os -marm -fno-omit-frame-pointer -mapcs -mno-sched-prolog -mabi=aapcs-linux -mno-thumb-interwork -D__LINUX_ARM_ARCH__=7 -mcpu=cortex-a8 -msoft-float -Uarm -fno-stack-protector -I/modules/include -fno-omit-frame-pointer -fno-optimize-sibling-calls -g -Wdeclaration-after-statement -Wno-pointer-sign -fwrapv -fpic -fno-builtin -Dstatic= -D"KBUILD_STR(s)=\#s" -D"KBUILD_BASENAME=KBUILD_STR(misc)" -D"KBUILD_MODNAME=KBUILD_STR(misc)" -c -o arch/arm/boot/compressed/misc.o arch/arm/boot/compressed/misc.c 192 | 193 | #5. head.o + misc.o + piggy.o --> vmlinux 194 | echo "##### 15. head.o + misc.o + piggy.o ---> vmlinux" 195 | $COMPILER-ld -EL --defsym zreladdr=0x30008000 --defsym params_phys=0x30000100 -p --no-undefined -X $COMPILER_PATH/../lib/gcc/arm-none-eabi/4.3.4/libgcc.a -T arch/arm/boot/compressed/vmlinux.lds arch/arm/boot/compressed/head.o arch/arm/boot/compressed/piggy.o arch/arm/boot/compressed/misc.o -o arch/arm/boot/compressed/vmlinux 196 | 197 | #6. vmlinux -> zImage 198 | echo "##### 16. vmlinux ---> zImage" 199 | $COMPILER-objcopy -O binary -R .note -R .note.gnu.build-id -R .comment -S arch/arm/boot/compressed/vmlinux arch/arm/boot/zImage 200 | 201 | popd 202 | mv $KSRC/arch/arm/boot/zImage out/zImage 203 | 204 | echo "##### 17. Creating zImage-inject.tar for Odin" 205 | tar c -C out zImage > zImage-inject.tar 206 | 207 | # Generate update.zip for flashing 208 | echo "18. Generating update.zip for flashing" 209 | rm -fr update.zip* 210 | prepare_update 211 | OUTFILE="$PWD/update.zip" 212 | pushd build/update 213 | 214 | FILES= 215 | SYMLINKS= 216 | 217 | for file in $(find .) 218 | do 219 | 220 | if [ -d $file ] 221 | then 222 | continue 223 | fi 224 | 225 | META_INF=$(echo $file | grep META-INF) 226 | if [ ! -z $META_INF ] 227 | then 228 | continue; 229 | fi 230 | 231 | if [ -h $file ] 232 | then 233 | SYMLINKS=$SYMLINKS' '$file 234 | elif [ -f $file ] 235 | then 236 | FILES=$FILES' '$file 237 | fi 238 | done 239 | 240 | echo "Zipping $OUTFILE..." 241 | zip -ry $OUTFILE-unsigned . -x $SYMLINKS '*\[*' '*\[\[*' 242 | echo "Signing $OUTFILE for flashing" 243 | java -jar $AOSP/out/host/linux-x86/framework/signapk.jar -w $AOSP/build/target/product/security/testkey.x509.pem $AOSP/build/target/product/security/testkey.pk8 $OUTFILE-unsigned $OUTFILE 244 | popd 245 | 246 | echo "##### 20. Done!" 247 | 248 | --------------------------------------------------------------------------------