### AnyKernel methods (DO NOT CHANGE)
## osm0sis @ xda-developers
[ "$OUTFD" ] || OUTFD=$1;
# set up working directory variables
[ "$AKHOME" ] && home=$AKHOME;
[ "$home" ] || home=$PWD;
bootimg=$home/boot.img;
bin=$home/tools;
patch=$home/patch;
ramdisk=$home/ramdisk;
split_img=$home/split_img;
### output/testing functions:
# ui_print "<text>" [...]
ui_print() {
until [ ! "$1" ]; do
echo "ui_print $1
ui_print" >> /proc/self/fd/$OUTFD;
shift;
done;
}
# abort ["<text>" [...]]
abort() {
ui_print " " "$@";
exit 1;
}
# contains <string> <substring>
contains() {
[ "${1#*$2}" != "$1" ];
}
# file_getprop <file> <property>
file_getprop() {
grep "^$2=" "$1" | tail -n1 | cut -d= -f2-;
}
###
### file/directory attributes functions:
# set_perm <owner> <group> <mode> <file> [<file2> ...]
set_perm() {
local uid gid mod;
uid=$1; gid=$2; mod=$3;
shift 3;
chown $uid:$gid "$@" || chown $uid.$gid "$@";
chmod $mod "$@";
}
# set_perm_recursive <owner> <group> <dir_mode> <file_mode> <dir> [<dir2> ...]
set_perm_recursive() {
local uid gid dmod fmod;
uid=$1; gid=$2; dmod=$3; fmod=$4;
shift 4;
while [ "$1" ]; do
chown -R $uid:$gid "$1" || chown -R $uid.$gid "$1";
find "$1" -type d -exec chmod $dmod {} +;
find "$1" -type f -exec chmod $fmod {} +;
shift;
done;
}
###
### dump_boot functions:
# split_boot (dump and split image only)
split_boot() {
local dumpfail;
if [ ! -e "$(echo $block | cut -d\ -f1)" ]; then
abort "Invalid partition. Aborting...";
fi;
if [ "$(echo $block | grep ' ')" ]; then
block=$(echo $block | cut -d\ -f1);
customdd=$(echo $block | cut -d\ -f2-);
elif [ ! "$customdd" ]; then
local customdd="bs=1048576";
fi;
if [ -f "$bin/nanddump" ]; then
$bin/nanddump -f $bootimg $block;
else
dd if=$block of=$bootimg $customdd;
fi;
[ $? != 0 ] && dumpfail=1;
mkdir -p $split_img;
cd $split_img;
if [ -f "$bin/unpackelf" ] && $bin/unpackelf -i $bootimg -h -q 2>/dev/null; then
if [ -f "$bin/elftool" ]; then
mkdir elftool_out;
$bin/elftool unpack -i $bootimg -o elftool_out;
fi;
$bin/unpackelf -i $bootimg;
[ $? != 0 ] && dumpfail=1;
mv -f boot.img-kernel kernel.gz;
mv -f boot.img-ramdisk ramdisk.cpio.gz;
mv -f boot.img-cmdline cmdline.txt 2>/dev/null;
if [ -f boot.img-dt -a ! -f "$bin/elftool" ]; then
case $(od -ta -An -N4 boot.img-dt | sed -e 's/ del//' -e 's/ //g') in
QCDT|ELF) mv -f boot.img-dt dt;;
*)
gzip -c kernel.gz > kernel.gz-dtb;
cat boot.img-dt >> kernel.gz-dtb;
rm -f boot.img-dt kernel.gz;
;;
esac;
fi;
elif [ -f "$bin/mboot" ]; then
$bin/mboot -u -f $bootimg;
elif [ -f "$bin/dumpimage" ]; then
dd bs=$(($(printf '%d\n' 0x$(hexdump -n 4 -s 12 -e '16/1 "%02x""\n"' $bootimg)) + 64)) count=1 conv=notrunc if=$bootimg of=boot-trimmed.img;
$bin/dumpimage -l boot-trimmed.img > header;
grep "Name:" header | cut -c15- > boot.img-name;
grep "Type:" header | cut -c15- | cut -d\ -f1 > boot.img-arch;
grep "Type:" header | cut -c15- | cut -d\ -f2 > boot.img-os;
grep "Type:" header | cut -c15- | cut -d\ -f3 | cut -d- -f1 > boot.img-type;
grep "Type:" header | cut -d\( -f2 | cut -d\) -f1 | cut -d\ -f1 | cut -d- -f1 > boot.img-comp;
grep "Address:" header | cut -c15- > boot.img-addr;
grep "Point:" header | cut -c15- > boot.img-ep;
$bin/dumpimage -p 0 -o kernel.gz boot-trimmed.img;
[ $? != 0 ] && dumpfail=1;
case $(cat boot.img-type) in
Multi) $bin/dumpimage -p 1 -o ramdisk.cpio.gz boot-trimmed.img;;
RAMDisk) mv -f kernel.gz ramdisk.cpio.gz;;
esac;
elif [ -f "$bin/rkcrc" ]; then
dd bs=4096 skip=8 iflag=skip_bytes conv=notrunc if=$bootimg of=ramdisk.cpio.gz;
else
$bin/magiskboot unpack -h $bootimg;
case $? in
1) dumpfail=1;;
2) touch chromeos;;
esac;
fi;
if [ $? != 0 -o "$dumpfail" ]; then
abort "Dumping/splitting image failed. Aborting...";
fi;
cd $home;
}
# unpack_ramdisk (extract ramdisk only)
unpack_ramdisk() {
local comp;
cd $split_img;
if [ -f ramdisk.cpio.gz ]; then
if [ -f "$bin/mkmtkhdr" ]; then
mv -f ramdisk.cpio.gz ramdisk.cpio.gz-mtk;
dd bs=512 skip=1 conv=notrunc if=ramdisk.cpio.gz-mtk of=ramdisk.cpio.gz;
fi;
mv -f ramdisk.cpio.gz ramdisk.cpio;
fi;
if [ -f ramdisk.cpio ]; then
comp=$($bin/magiskboot decompress ramdisk.cpio 2>&1 | grep -v 'raw' | sed -n 's;.*\[\(.*\)\];\1;p');
else
abort "No ramdisk found to unpack. Aborting...";
fi;
if [ "$comp" ]; then
mv -f ramdisk.cpio ramdisk.cpio.$comp;
$bin/magiskboot decompress ramdisk.cpio.$comp ramdisk.cpio;
if [ $? != 0 ] && $comp --help 2>/dev/null; then
echo "Attempting ramdisk unpack with busybox $comp..." >&2;
$comp -dc ramdisk.cpio.$comp > ramdisk.cpio;
fi;
fi;
[ -d $ramdisk ] && mv -f $ramdisk $home/rdtmp;
mkdir -p $ramdisk;
chmod 755 $ramdisk;
cd $ramdisk;
EXTRACT_UNSAFE_SYMLINKS=1 cpio -d -F $split_img/ramdisk.cpio -i;
if [ $? != 0 -o ! "$(ls)" ]; then
abort "Unpacking ramdisk failed. Aborting...";
fi;
if [ -d "$home/rdtmp" ]; then
cp -af $home/rdtmp/* .;
fi;
}
### dump_boot (dump and split image, then extract ramdisk)
dump_boot() {
split_boot;
unpack_ramdisk;
}
###
### write_boot functions:
# repack_ramdisk (repack ramdisk only)
repack_ramdisk() {
local comp packfail mtktype;
cd $home;
case $ramdisk_compression in
auto|"") comp=$(ls $split_img/ramdisk.cpio.* 2>/dev/null | grep -v 'mtk' | rev | cut -d. -f1 | rev);;
none|cpio) comp="";;
gz) comp=gzip;;
lzo) comp=lzop;;
bz2) comp=bzip2;;
lz4-l) comp=lz4_legacy;;
*) comp=$ramdisk_compression;;
esac;
if [ -f "$bin/mkbootfs" ]; then
$bin/mkbootfs $ramdisk > ramdisk-new.cpio;
else
cd $ramdisk;
find . | cpio -H newc -o > $home/ramdisk-new.cpio;
fi;
[ $? != 0 ] && packfail=1;
cd $home;
$bin/magiskboot cpio ramdisk-new.cpio test;
magisk_patched=$?;
[ $((magisk_patched & 3)) -eq 1 ] && $bin/magiskboot cpio ramdisk-new.cpio "extract .backup/.magisk $split_img/.magisk";
if [ "$comp" ]; then
$bin/magiskboot compress=$comp ramdisk-new.cpio;
if [ $? != 0 ] && $comp --help 2>/dev/null; then
echo "Attempting ramdisk repack with busybox $comp..." >&2;
$comp -9c ramdisk-new.cpio > ramdisk-new.cpio.$comp;
[ $? != 0 ] && packfail=1;
rm -f ramdisk-new.cpio;
fi;
fi;
if [ "$packfail" ]; then
abort "Repacking ramdisk failed. Aborting...";
fi;
if [ -f "$bin/mkmtkhdr" -a -f "$split_img/boot.img-base" ]; then
mtktype=$(od -ta -An -N8 -j8 $split_img/ramdisk.cpio.gz-mtk | sed -e 's/ nul//g' -e 's/ //g' | tr '[:upper:]' '[:lower:]');
case $mtktype in
rootfs|recovery) $bin/mkmtkhdr --$mtktype ramdisk-new.cpio*;;
esac;
fi;
}
# flash_boot (build, sign and write image only)
flash_boot() {
local varlist i kernel ramdisk fdt cmdline comp part0 part1 nocompflag signfail pk8 cert avbtype;
cd $split_img;
if [ -f "$bin/mkimage" ]; then
varlist="name arch os type comp addr ep";
elif [ -f "$bin/mkbootimg" -a -f "$bin/unpackelf" -a -f boot.img-base ]; then
mv -f cmdline.txt boot.img-cmdline 2>/dev/null;
varlist="cmdline base pagesize kernel_offset ramdisk_offset tags_offset";
fi;
for i in $varlist; do
if [ -f boot.img-$i ]; then
eval local $i=\"$(cat boot.img-$i)\";
fi;
done;
cd $home;
for i in zImage zImage-dtb Image Image-dtb Image.gz Image.gz-dtb Image.bz2 Image.bz2-dtb Image.lzo Image.lzo-dtb Image.lzma Image.lzma-dtb Image.xz Image.xz-dtb Image.lz4 Image.lz4-dtb Image.fit; do
if [ -f $i ]; then
kernel=$home/$i;
break;
fi;
done;
if [ "$kernel" ]; then
if [ -f "$bin/mkmtkhdr" -a -f "$split_img/boot.img-base" ]; then
$bin/mkmtkhdr --kernel $kernel;
kernel=$kernel-mtk;
fi;
elif [ "$(ls $split_img/kernel* 2>/dev/null)"