Magisk/scripts/addon.d.sh
osm0sis f5c099e9a7 scripts: fix addon.d after merge to trampoline
- pass addon.d arguments through trampoline or nothing will happen
- exit immediately after handing over from trampoline
- better grep for recovery OUTFD which should work in all cases
- output to logcat when booted and no binaries are found
- use /postinstall/tmp path to call functions from addon.d-v2 in progress
- remove unnecessary check for $MAGISKBIN since we're already executing from within it
- make sure we're not in $TMPDIR again before we delete it
- use $MAGISKBIN wherever possible in case it ever needs to be changed
2020-03-08 22:33:19 -07:00

139 lines
3.2 KiB
Bash

#!/sbin/sh
# ADDOND_VERSION=2
########################################################
#
# Magisk Survival Script for ROMs with addon.d support
# by topjohnwu and osm0sis
#
########################################################
trampoline() {
mount /data 2>/dev/null
if [ -f $MAGISKBIN/addon.d.sh ]; then
exec sh $MAGISKBIN/addon.d.sh "$@"
exit $?
else
ps | grep zygote | grep -v grep >/dev/null && BOOTMODE=true || BOOTMODE=false
$BOOTMODE || ps -A 2>/dev/null | grep zygote | grep -v grep >/dev/null && BOOTMODE=true
if ! $BOOTMODE; then
# update-binary|updater <RECOVERY_API_VERSION> <OUTFD> <ZIPFILE>
OUTFD=$(ps | grep -v 'grep' | grep -oE 'update(.*) 3 [0-9]+' | cut -d" " -f3)
[ -z $OUTFD ] && OUTFD=$(ps -Af | grep -v 'grep' | grep -oE 'update(.*) 3 [0-9]+' | cut -d" " -f3)
# update_engine_sideload --payload=file://<ZIPFILE> --offset=<OFFSET> --headers=<HEADERS> --status_fd=<OUTFD>
[ -z $OUTFD ] && OUTFD=$(ps | grep -v 'grep' | grep -oE 'status_fd=[0-9]+' | cut -d= -f2)
[ -z $OUTFD ] && OUTFD=$(ps -Af | grep -v 'grep' | grep -oE 'status_fd=[0-9]+' | cut -d= -f2)
fi
ui_print() { $BOOTMODE && log -t Magisk -- "$1" || echo -e "ui_print $1\nui_print" >> /proc/self/fd/$OUTFD; }
ui_print "************************"
ui_print "* Magisk addon.d failed"
ui_print "************************"
ui_print "! Cannot find Magisk binaries - was data wiped or not decrypted?"
ui_print "! Reflash OTA from decrypted recovery or reflash Magisk"
exit 1
fi
}
# Always use the script in /data
MAGISKBIN=/data/adb/magisk
[ "$0" = $MAGISKBIN/addon.d.sh ] || trampoline "$@"
V1_FUNCS=/tmp/backuptool.functions
V2_FUNCS=/postinstall/tmp/backuptool.functions
if [ -f $V1_FUNCS ]; then
. $V1_FUNCS
backuptool_ab=false
elif [ -f $V2_FUNCS ]; then
. $V2_FUNCS
else
return 1
fi
initialize() {
# Load utility functions
. $MAGISKBIN/util_functions.sh
if $BOOTMODE; then
# Override ui_print when booted
ui_print() { log -t Magisk -- "$1"; }
fi
OUTFD=
setup_flashable
}
main() {
if ! $backuptool_ab; then
# Wait for post addon.d-v1 processes to finish
sleep 5
fi
# Ensure we aren't in /tmp/addon.d anymore (since it's been deleted by addon.d)
cd $TMPDIR
$BOOTMODE || recovery_actions
PRETTY_VER=$MAGISK_VER
echo $PRETTY_VER | grep -q '\.' && PRETTY_VER=v$PRETTY_VER
print_title "Magisk $PRETTY_VER addon.d"
mount_partitions
check_data
get_flags
if $backuptool_ab; then
# Swap the slot for addon.d-v2
if [ ! -z $SLOT ]; then [ $SLOT = _a ] && SLOT=_b || SLOT=_a; fi
fi
find_boot_image
[ -z $BOOTIMAGE ] && abort "! Unable to detect target image"
ui_print "- Target image: $BOOTIMAGE"
remove_system_su
find_manager_apk
install_magisk
# Cleanups
cd /
$BOOTMODE || recovery_cleanup
rm -rf $TMPDIR
ui_print "- Done"
exit 0
}
case "$1" in
backup)
# Stub
;;
restore)
# Stub
;;
pre-backup)
# Stub
;;
post-backup)
# Stub
;;
pre-restore)
# Stub
;;
post-restore)
initialize
if $backuptool_ab; then
$BOOTMODE && su=su || su=sh
exec $su -c "sh $0 addond-v2"
else
# Run in background, hack for addon.d-v1
(main) &
fi
;;
addond-v2)
initialize
main
;;
esac