AK3: clean up and simplify Magisk patch logic

- only repatch when there is an Image* (i.e. don't attempt on ramdisk-only mods)
- make sure new ramdisk is already copied before trying to detect Magisk patch state in case it changed
- clean up Magisk patch state and exported variables immediately
This commit is contained in:
osm0sis committed 2020-01-16 01:00:32 -04:00
1 parent bd279e6cba
commit 21ee68f5a8
1 file changed
+38 -38
+38 -38
View File
@@ -305,48 +305,49 @@ flash_boot() {
test "$dt" && dt="--dt $dt";
$bin/mkbootimg --kernel $kernel --ramdisk $ramdisk --cmdline "$cmdline" --base $home --pagesize $pagesize --kernel_offset $kerneloff --ramdisk_offset $ramdiskoff --tags_offset "$tagsoff" $dt --output $home/boot-new.img;
else
if [ ! "$magisk_patched" ]; then
$bin/magiskboot cpio ramdisk.cpio test;
magisk_patched=$?;
test $((magisk_patched & 3)) -eq 1 && $bin/magiskboot cpio ramdisk.cpio "extract .backup/.magisk .magisk";
fi;
if [ "$kernel" ]; then
cp -f $kernel kernel;
if [ $((magisk_patched & 3)) -eq 1 ]; then
ui_print " " "Magisk detected! Patching kernel so reflashing Magisk is not necessary...";
comp=$($bin/magiskboot decompress kernel 2>&1 | grep -v 'raw' | sed -n 's;.*\[\(.*\)\];\1;p');
($bin/magiskboot split $kernel || $bin/magiskboot decompress $kernel kernel) 2>/dev/null;
if [ $? != 0 -a "$comp" ]; then
echo "Attempting kernel unpack with busybox $comp..." >&2;
$comp -dc $kernel > kernel;
fi;
$bin/magiskboot hexpatch kernel 736B69705F696E697472616D667300 77616E745F696E697472616D667300;
if [ "$comp" ]; then
$bin/magiskboot compress=$comp kernel kernel.$comp;
if [ $? != 0 ]; then
echo "Attempting kernel repack with busybox $comp..." >&2;
$comp -9c kernel > kernel.$comp;
fi;
mv -f kernel.$comp kernel;
fi;
else
case $kernel in
*-dtb) rm -f kernel_dtb;;
esac;
fi;
fi;
test "$kernel" && cp -f $kernel kernel;
test "$ramdisk" && cp -f $ramdisk ramdisk.cpio;
test "$dt" -a -f extra && cp -f $dt extra;
for i in dtb recovery_dtbo; do
test "$(eval echo \$$i)" -a -f $i && cp -f $(eval echo \$$i) $i;
done;
if [ $((magisk_patched & 3)) -eq 1 ]; then
export $(cat .magisk);
test $((magisk_patched & 8)) -ne 0 && export TWOSTAGEINIT=true;
for fdt in dtb extra kernel_dtb recovery_dtbo; do
test -f $fdt && $bin/magiskboot dtb $fdt patch;
done;
fi;
case $kernel in
*Image*)
if [ ! "$magisk_patched" ]; then
$bin/magiskboot cpio ramdisk.cpio test;
magisk_patched=$?;
fi;
if [ $((magisk_patched & 3)) -eq 1 ]; then
ui_print " " "Magisk detected! Patching kernel so reflashing Magisk is not necessary...";
comp=$($bin/magiskboot decompress kernel 2>&1 | grep -v 'raw' | sed -n 's;.*\[\(.*\)\];\1;p');
($bin/magiskboot split $kernel || $bin/magiskboot decompress $kernel kernel) 2>/dev/null;
if [ $? != 0 -a "$comp" ]; then
echo "Attempting kernel unpack with busybox $comp..." >&2;
$comp -dc $kernel > kernel;
fi;
$bin/magiskboot hexpatch kernel 736B69705F696E697472616D667300 77616E745F696E697472616D667300;
if [ "$comp" ]; then
$bin/magiskboot compress=$comp kernel kernel.$comp;
if [ $? != 0 ]; then
echo "Attempting kernel repack with busybox $comp..." >&2;
$comp -9c kernel > kernel.$comp;
fi;
mv -f kernel.$comp kernel;
fi;
test ! -f .magisk && $bin/magiskboot cpio ramdisk.cpio "extract .backup/.magisk .magisk";
export $(cat .magisk);
test $((magisk_patched & 8)) -ne 0 && export TWOSTAGEINIT=true;
for fdt in dtb extra kernel_dtb recovery_dtbo; do
test -f $fdt && $bin/magiskboot dtb $fdt patch;
done;
else
case $kernel in
*-dtb) rm -f kernel_dtb;;
esac;
fi;
unset magisk_patched KEEPFORCEENCRYPT KEEPVERITY SHA1 TWOSTAGEINIT;
;;
esac;
case $ramdisk_compression in
none|cpio) nocompflag="-n";;
esac;
@@ -650,7 +651,6 @@ reset_ak() {
else
rm -rf $patch $home/rdtmp;
fi;
unset magisk_patched KEEPFORCEENCRYPT KEEPVERITY SHA1 TWOSTAGEINIT;
setup_ak;
}