#!/sbin/sh
# arg 1 is recovery api version, generally 3.
# arg 2 is the pipe fd, to the recovery binary.
# communicate with it using the recovery api.
# arg 3 is the zip file
#Set these per device, /proc/emmc is unreliable now
BOOTBLK="/dev/block/mmcblk0p20"
echo -n -e 'ui_print Installing Kernel...\n' > /proc/self/fd/$2
echo -n -e 'ui_print\n' > /proc/self/fd/$2
cd /tmp
unzip -o "$3"
tools=/tmp/tools;
binaries=/tmp/binaries;
working=/tmp/prebuilt/working_folder;
prebuilt=/tmp/prebuilt/kernel;
modules=/tmp/modules/lib/modules;
#Give permission to use tools
chmod 755 $binaries/mkbootimg
chmod 755 $binaries/unmkbootimg
chmod 755 $tools/busybox
chmod 755 $tools/dump_image
#Pull boot.img
dd if=$BOOTBLK of=$working/boot.img
#$tools/dump_image boot $working/boot.img
#Check if kernel is signed
echo -n -e 'ui_print Checking old kernel for signature...\n' > /proc/self/fd/$2
#Boot.img uses a magic header of ANDROID!
#
# Check for strange boot.img header
#
check_header=`$tools/busybox od -A n -h -j 0 -N 8 $working/boot.img | sed 's/ //g'`
if [ "$check_header" != "4e415244494f2144" ]
then
echo -n -e 'ui_print\n' > /proc/self/fd/$2
echo -n -e 'ui_print Android magic header not found at start of boot.img\n' > /proc/self/fd/$2
echo -n -e 'ui_print Checking if it exists elsewhere ...\n' > /proc/self/fd/$2
# Look for Header
hex_offset=`$tools/busybox od -x -A x $working/boot.img | grep -m 1 "4e41 [ ]*5244 [ ]*494f [ ]*2144" | sed -e 's/ .*//'`
if [ "$hex_offset" != "" ]
then
#
# Remove leading bytes before the Android header
#
dec_offset=`printf "%d" 0x$hex_offset`
echo -n -e 'ui_print Android header found at offset $dec_offset\n' > /proc/self/fd/$2
echo -n -e 'ui_print Removing extra stuff before it so boot.img can be read properly ...\n' > /proc/self/fd/$2
dd if=$working/boot.img of=$working/modboot.img bs=1 skip=$dec_offset
rm -f $working/boot.img
mv $working/modboot.img /tmp/newboot.img
echo -n -e 'ui_print\n' > /proc/self/fd/$2
#Kernel info is given by unmkbootimg
dump_boot() {
mv $binaries/unmkbootimg /tmp/unmkbootimg
mv $binaries/mkbootimg /tmp/mkbootimg
KERNEL_INFO_RAW=$(./unmkbootimg -i newboot.img)
KERNEL_INFO=$(echo $KERNEL_INFO_RAW | grep mkbootimg)
KERNEL_BASE=$(echo $KERNEL_INFO | cut -d " " -f 3)
KERNEL_PAGESIZE=$(echo $KERNEL_INFO | cut -d " " -f 5)
KERNEL_OFFSET=$(echo $KERNEL_INFO | cut -d " " -f 7)
RAMDISK_OFFSET=$(echo $KERNEL_INFO | cut -d " " -f 9)
TAGS_OFFSET=$(echo $KERNEL_INFO | cut -d " " -f 13)
KERNEL_CMDLINE=$(echo $KERNEL_INFO | cut -d "'" -f 2)
RAMDISK_NAME=$(echo $KERNEL_INFO | cut -d " " -f 20)
}
#Make the boot.img
write_boot() {
echo -n -e 'ui_print\n' > /proc/self/fd/$2
echo -n -e 'ui_print Make new boot.img\n' > /proc/self/fd/$2
echo -n -e 'ui_print\n' > /proc/self/fd/$2
./mkbootimg --base $KERNEL_BASE --pagesize $KERNEL_PAGESIZE --kernel_offset $KERNEL_OFFSET --ramdisk_offset $RAMDISK_OFFSET --tags_offset $TAGS_OFFSET --cmdline "$KERNEL_CMDLINE" --kernel $working/kernel --ramdisk "$RAMDISK_NAME" -o modboot.img
}
#Flash the boot.img
flash_boot() {
echo -n -e 'ui_print Flash boot.img\n' > /proc/self/fd/$2
dd if=/tmp/modboot.img of=$BOOTBLK
busybox mount /system
echo -n -e 'ui_print Flashing modules...\n' > /proc/self/fd/$2
rm -rf /system/lib/modules/
cp -r $modules/ /system/lib/modules/
busybox umount /system
echo -n -e 'ui_print Boot.img flashed, you may now reboot...\n' > /proc/self/fd/$2
}
dump_boot;
write_boot;
flash_boot;
exit 0
else
echo -n -e 'ui_print Warning: Android header not found in boot.img (unsupported format)\n' > /proc/self/fd/$2
return $?
exit 1
fi
fi
#Kernel info is given by unmkbootimg
mv $working/boot.img /tmp/newboot.img
dump_boot() {
mv $binaries/unmkbootimg /tmp/unmkbootimg
mv $binaries/mkbootimg /tmp/mkbootimg
KERNEL_INFO_RAW=$(./unmkbootimg -i boot.img)
KERNEL_INFO=$(echo $KERNEL_INFO_RAW | grep mkbootimg)
KERNEL_BASE=$(echo $KERNEL_INFO | cut -d " " -f 3)
KERNEL_PAGESIZE=$(echo $KERNEL_INFO | cut -d " " -f 5)
KERNEL_OFFSET=$(echo $KERNEL_INFO | cut -d " " -f 7)
RAMDISK_OFFSET=$(echo $KERNEL_INFO | cut -d " " -f 9)
TAGS_OFFSET=$(echo $KERNEL_INFO | cut -d " " -f 13)
KERNEL_CMDLINE=$(echo $KERNEL_INFO | cut -d "'" -f 2)
RAMDISK_NAME=$(echo $KERNEL_INFO | cut -d " " -f 20)
}
#Make the boot.img
write_boot() {
echo -n -e 'ui_print\n' > /proc/self/fd/$2
echo -n -e 'ui_print Make new boot.img\n' > /proc/self/fd/$2
echo -n -e 'ui_print\n' > /proc/self/fd/$2
./mkbootimg --base $KERNEL_BASE --pagesize $KERNEL_PAGESIZE --kernel_offset $KERNEL_OFFSET --ramdisk_offset $RAMDISK_OFFSET --tags_offset $TAGS_OFFSET --cmdline "$KERNEL_CMDLINE" --kernel $working/kernel --ramdisk "$RAMDISK_NAME" -o modboot.img
}
#Flash the boot.img
flash_boot() {
echo -n -e 'ui_print Flash boot.img\n' > /proc/self/fd/$2
dd if=/tmp/modboot.img of=$BOOTBLK
busybox mount /system
echo -n -e 'ui_print Flashing modules...\n' > /proc/self/fd/$2
rm -rf /system/lib/modules/
cp -r $modules/ /system/lib/modules/
busybox umount /system
echo -n -e 'ui_print Boot.img flashed, you may now reboot...\n' > /proc/self/fd/$2
}
dump_boot;
write_boot;
flash_boot;
exit 0