Introduce MagiskSU

This commit is contained in:
topjohnwu
2017-02-01 06:02:43 +08:00
parent d4149d4b7a
commit 298f09402f
10 changed files with 82 additions and 106 deletions

View File

@ -1,8 +1,9 @@
#!/system/bin/sh
RAMDISK=$1
BINDIR=$2
[ -z $BINDIR ] && BINDIR=/data/magisk
BINDIR=/data/magisk
[ ! -e $BINDIR ] && BINDIR=/cache/data_bin
[ ! -e $BINDIR ] && exit
SYSTEMLIB=/system/lib
[ -d /system/lib64 ] && SYSTEMLIB=/system/lib64

View File

@ -321,25 +321,25 @@ ui_print "- Constructing environment"
if (is_mounted /data); then
rm -rf /data/busybox /data/magisk 2>/dev/null
mkdir -p /data/busybox
cp -af $BINDIR /data/magisk
cp -af $INSTALLER/common/init.magisk.rc $INSTALLER/common/magic_mask.sh /data/magisk
mkdir -p /data/busybox /data/magisk
cp -af $BINDIR/busybox $BINDIR/sepolicy-inject $BINDIR/resetprop \
$INSTALLER/common/init.magisk.rc $INSTALLER/common/magic_mask.sh /data/magisk
cp -af $INSTALLER/common/magisk.apk /data/magisk.apk
/data/magisk/busybox --install -s /data/busybox
ln -s /data/magisk/busybox /data/busybox/busybox
# Prevent issues
rm -f /data/busybox/su /data/busybox/sh /data/busybox/reboot
chcon -hR "u:object_r:system_file:s0" /data/magisk /data/busybox
chcon -hR u:object_r:system_file:s0 /data/magisk /data/busybox
chmod -R 755 /data/magisk /data/busybox
PATH=/data/busybox:$PATH
BINDIR=/data/magisk
# PATH=/data/busybox:$PATH
else
rm -rf /cache/data_bin 2>/dev/null
cp -af $BINDIR /cache/data_bin
cp -af $INSTALLER/common/init.magisk.rc $INSTALLER/common/magic_mask.sh /cache/data_bin
mkdir -p /cache/data_bin
cp -af $BINDIR/busybox $BINDIR/sepolicy-inject $BINDIR/resetprop \
$INSTALLER/common/custom_ramdisk_patch.sh $INSTALLER/common/init.magisk.rc \
$INSTALLER/common/magic_mask.sh /cache/data_bin
cp -af $INSTALLER/common/magisk.apk /cache/magisk.apk
chmod -R 755 /cache/data_bin
BINDIR=/cache/data_bin
fi
##########################################################################################
@ -370,8 +370,11 @@ if (! is_mounted /magisk); then
fi
MAGISKLOOP=$LOOPDEVICE
mkdir -p /magisk/.core/magiskhide 2>/dev/null
cp -af $INSTALLER/common/magiskhide/. /magisk/.core/magiskhide
# Core folders and scripts
mkdir -p $COREDIR/magiskhide $COREDIR/postfsdata.d $COREDIR/service.d 2>/dev/null
cp -af $INSTALLER/common/magiskhide/. $BINDIR/magiskhide $COREDIR/magiskhide
chmod -R 755 $COREDIR/magiskhide $COREDIR/postfsdata.d $COREDIR/service.d
chown -R 0.0 $COREDIR/magiskhide $COREDIR/postfsdata.d $COREDIR/service.d
##########################################################################################
# Boot image patch
@ -434,7 +437,7 @@ if ($SUPERSU); then
fi
rm -f $TMPDIR/boottmp/stock_boot.img $UNPACKDIR/ramdisk.orig.gz $UNPACKDIR/ramdisk.gz 2>/dev/null
ui_print "- Patching ramdisk with sukernel"
sh /data/custom_ramdisk_patch.sh $UNPACKDIR/ramdisk $BINDIR
sh /data/custom_ramdisk_patch.sh $UNPACKDIR/ramdisk
LD_LIBRARY_PATH=$SYSTEMLIB /su/bin/sukernel --cpio-backup $UNPACKDIR/ramdisk.orig $UNPACKDIR/ramdisk $UNPACKDIR/ramdisk
gzip -9 < $UNPACKDIR/ramdisk > $UNPACKDIR/ramdisk.gz
rm -f $UNPACKDIR/ramdisk $UNPACKDIR/ramdisk.orig
@ -481,24 +484,13 @@ else
fi
fi
# Root
ROOT=false
if [ ! -d /magisk/phh ]; then
ui_print "- Installing phh's SuperUser"
ROOT=true
elif [ `grep_prop versionCode /magisk/phh/module.prop` -lt `grep_prop versionCode $INSTALLER/common/phh/module.prop` ]; then
ui_print "- Upgrading phh's SuperUser"
ROOT=true
fi
if ($ROOT); then
mkdir -p /magisk/phh/bin 2>/dev/null
mkdir -p /magisk/phh/su.d 2>/dev/null
cp -af $INSTALLER/common/phh/. /magisk/phh
cp -af $BINDIR/su $BINDIR/sepolicy-inject /magisk/phh/bin
chmod -R 755 /magisk/phh
chown -R 0.0 /magisk/phh
fi
# MagiskSU
ui_print "- Installing MagiskSU"
rm -rf $COREDIR/su 2>/dev/null
mkdir -p $COREDIR/su
cp -af $BINDIR/su $INSTALLER/common/magisksu.sh $COREDIR/su
chmod -R 755 $COREDIR/su
chown -R 0.0 $COREDIR/su
# Patch ramdisk
ui_print "- Patching ramdisk"
@ -544,6 +536,7 @@ else
chmod 0755 magisk
chmod 0750 init.magisk.rc sbin/magic_mask.sh
chown 0.0 magisk init.magisk.rc sbin/magic_mask.sh
fi
ui_print "- Repacking boot image"

46
scripts/init.magisk.rc Normal file
View File

@ -0,0 +1,46 @@
# Triggers
on post-fs
start magisk_pfs
wait /dev/.magisk.unblock 20
rm /dev/.magisk.unblock
on post-fs-data
start magisk_pfsd
wait /dev/.magisk.unblock 60
rm /dev/.magisk.unblock
on load_magisk_props_action
start magisk_props
wait /dev/.magisk.unblock 5
rm /dev/.magisk.unblock
on property:magisk.restart_pfsd=1
trigger post-fs-data
# Services
# launch post-fs script
service magisk_pfs /sbin/magic_mask.sh post-fs
user root
seclabel u:r:su:s0
oneshot
# launch post-fs-data script
service magisk_pfsd /sbin/magic_mask.sh post-fs-data
user root
seclabel u:r:su:s0
oneshot
# Load module props
service magisk_props /sbin/magic_mask.sh load_magisk_props
user root
seclabel u:r:su:s0
oneshot
# launch late_start script
service magisk_service /sbin/magic_mask.sh service
class late_start
user root
seclabel u:r:su:s0
oneshot

View File

@ -266,12 +266,13 @@ case $1 in
# Don't run twice
if [ "`getprop magisk.restart_pfsd`" != "1" ]; then
export OLDPATH=$PATH
export PATH=$TOOLPATH:$OLDPATH
log_print "** Magisk post-fs-data mode running..."
# Cache support
mv /cache/stock_boot.img /data/stock_boot.img 2>/dev/null
mv /cache/magisk.apk /data/magisk.apk 2>/dev/null
mv /cache/custom_ramdisk_patch.sh /data/custom_ramdisk_patch.sh 2>/dev/null
if [ -d "/cache/data_bin" ]; then
rm -rf $BINPATH $TOOLPATH
mkdir -p $TOOLPATH
@ -282,17 +283,16 @@ case $1 in
rm -f $TOOLPATH/su $TOOLPATH/sh $TOOLPATH/reboot
fi
mv /cache/stock_boot.img /data/stock_boot.img 2>/dev/null
mv /cache/magisk.apk /data/magisk.apk 2>/dev/null
# Live patch sepolicy
$BINPATH/sepolicy-inject --live
# Set up environment
export OLDPATH=$PATH
export PATH=$TOOLPATH:$OLDPATH
chmod -R 755 $BINPATH $TOOLPATH
chown -R 0.0 $BINPATH $TOOLPATH
find $BINPATH $TOOLPATH -exec chcon -h u:object_r:system_file:s0 {} \;
# Live patch sepolicy
$BINPATH/sepolicy-inject --live -s su
# Multirom functions should go here, not available right now
MULTIROM=false
@ -314,9 +314,9 @@ case $1 in
# Remove empty directories, legacy paths, symlinks, old temporary images
find $MOUNTPOINT -type d -depth ! -path "*core*" -exec rmdir {} \; 2>/dev/null
rm -rf $MOUNTPOINT/zzsupersu $COREDIR/bin $COREDIR/dummy $COREDIR/mirror /data/magisk/*.img 2>/dev/null
rm -rf $MOUNTPOINT/zzsupersu $MOUNTPOINT/phh $COREDIR/bin $COREDIR/dummy $COREDIR/mirror /data/magisk/*.img 2>/dev/null
# Remove modules that is labeled to be removed
# Remove modules that are labeled to be removed
for MOD in $MOUNTPOINT/* ; do
rm -f $MOD/system/placeholder 2>/dev/null
if [ -f $MOD/remove ]; then
@ -342,13 +342,10 @@ case $1 in
fi
fi
log_print "* Preparing modules"
# Start MagiskSU if no SuperSU
[ ! -f /sbin/launch_daemonsu.sh ] && sh $COREDIR/su/magisksu.sh
# Disable phh and Magisk Hide for SuperSU
if [ -f /sbin/launch_daemonsu.sh ]; then
touch /magisk/phh/disable 2>/dev/null
rm -f $COREDIR/magiskhide/enable 2>/dev/null
fi
log_print "* Preparing modules"
mkdir -p $DUMMDIR
mkdir -p $MIRRDIR/system

28
scripts/magisksu.sh Normal file
View File

@ -0,0 +1,28 @@
#!/system/bin/sh
MODDIR=${0%/*}
LOGFILE=/cache/magisk.log
PATH=$OLDPATH
log_print() {
echo "MagiskSU: $1"
echo "MagiskSU: $1" >> $LOGFILE
log -p i -t Magisk "MagiskSU: $1"
}
log_print "Moving and linking /sbin binaries"
mount -o rw,remount rootfs /
/data/busybox/cp -afc /sbin /sbin_orig
mount -o ro,remount rootfs /
log_print "Exposing su binary"
rm -rf /magisk/.core/bin $MODDIR/sbin_bind
mkdir -p $MODDIR/sbin_bind
ln -s /sbin_orig/* $MODDIR/sbin_bind
chcon -h u:object_r:rootfs:s0 $MODDIR/sbin_bind/*
chmod 755 $MODDIR/sbin_bind
ln -s $MODDIR/su $MODDIR/sbin_bind/su
mount -o bind $MODDIR/sbin_bind /sbin
log_print "Starting su daemon"
/sbin/su --daemon