Skip to content

Commit

Permalink
Introduce MagiskSU
Browse files Browse the repository at this point in the history
  • Loading branch information
topjohnwu committed Jan 31, 2017
1 parent d4149d4 commit 298f094
Show file tree
Hide file tree
Showing 10 changed files with 82 additions and 106 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ ziptools/zipadjust

# Generated scripts
zip_static/common/magic_mask.sh
zip_static/common/magisksu.sh
zip_static/common/init.magisk.rc
zip_static/common/custom_ramdisk_patch.sh
zip_static/META-INF/com/google/android/update-binary

# Leave all busybox!
Expand Down
8 changes: 6 additions & 2 deletions build.cmd
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,11 @@ EXIT /B %ERRORLEVEL%
ECHO ************************
ECHO * Copying binaries
ECHO ************************
COPY /Y libs\armeabi\* zip_static\arm
COPY /Y libs\armeabi-v7a\* zip_static\arm
COPY /Y libs\arm64-v8a\* zip_static\arm64
COPY /Y libs\x86\* zip_static\x86
COPY /Y libs\x86_64\* zip_static\x64
CALL :mkcp libs\armeabi\bootimgtools uninstaller\arm
CALL :mkcp libs\armeabi-v7a\bootimgtools uninstaller\arm
CALL :mkcp libs\arm64-v8a\bootimgtools uninstaller\arm64
CALL :mkcp libs\x86\bootimgtools uninstaller\x86
CALL :mkcp libs\x86_64\bootimgtools uninstaller\x64
Expand All @@ -75,6 +75,8 @@ EXIT /B %ERRORLEVEL%
forfiles /P zip_static\x64 /C "cmd /C IF NOT @file == \"busybox\" DEL @file"
2>NUL DEL zip_static\META-INF\com\google\android\update-binary
2>NUL DEL zip_static\common\custom_ramdisk_patch.sh
2>NUL DEL zip_static\common\magisksu.sh
2>NUL DEL zip_static\common\init.magisk.rc
2>NUL DEL zip_static\common\magic_mask.sh
2>NUL RMDIR /S /Q uninstaller\arm
2>NUL RMDIR /S /Q uninstaller\arm64
Expand All @@ -100,6 +102,8 @@ EXIT /B %ERRORLEVEL%
ECHO * Zipping Magisk v%~1
ECHO ************************
COPY /Y scripts\custom_ramdisk_patch.sh zip_static\common\custom_ramdisk_patch.sh
COPY /Y scripts\magisksu.sh zip_static\common\magisksu.sh
COPY /Y scripts\init.magisk.rc zip_static\common\init.magisk.rc
CD zip_static
2>NUL DEL "..\Magisk-v%~1.zip"
..\ziptools\win_bin\zip "..\Magisk-v%~1.zip" -r .
Expand Down
8 changes: 6 additions & 2 deletions build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ cleanup() {
ls zip_static/x64/* | grep -v "busybox" | xargs rm -rfv
rm -rfv zip_static/META-INF/com/google/android/update-binary
rm -rfv zip_static/common/custom_ramdisk_patch.sh
rm -rfv zip_static/common/magisksu.sh
rm -rfv zip_static/common/init.magisk.rc
rm -rfv zip_static/common/magic_mask.sh
rm -rfv uninstaller/arm
rm -rfv uninstaller/arm64
Expand All @@ -52,8 +54,8 @@ build_bin() {
echo "************************"
echo "* Copying binaries"
echo "************************"
mkcp "libs/armeabi/*" zip_static/arm
mkcp libs/armeabi/bootimgtools uninstaller/arm
mkcp "libs/armeabi-v7a/*" zip_static/arm
mkcp libs/armeabi-v7a/bootimgtools uninstaller/arm
mkcp "libs/arm64-v8a/*" zip_static/arm64
mkcp libs/arm64-v8a/bootimgtools uninstaller/arm64
mkcp "libs/x86/*" zip_static/x86
Expand All @@ -73,6 +75,8 @@ zip_package() {
echo "* Zipping Magisk v$1"
echo "************************"
cp -afv scripts/custom_ramdisk_patch.sh zip_static/common/custom_ramdisk_patch.sh
cp -afv scripts/magisksu.sh zip_static/common/magisksu.sh
cp -afv scripts/init.magisk.rc zip_static/common/init.magisk.rc
cd zip_static
find . -type f -exec chmod 644 {} \;
find . -type d -exec chmod 755 {} \;
Expand Down
5 changes: 3 additions & 2 deletions scripts/custom_ramdisk_patch.sh
Original file line number Diff line number Diff line change
@@ -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

Expand Down
53 changes: 23 additions & 30 deletions scripts/flash_script.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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

##########################################################################################
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand Down
File renamed without changes.
29 changes: 13 additions & 16 deletions scripts/magic_mask.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

Expand All @@ -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
Expand All @@ -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
Expand Down
28 changes: 28 additions & 0 deletions scripts/magisksu.sh
Original file line number Diff line number Diff line change
@@ -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
8 changes: 0 additions & 8 deletions zip_static/common/phh/module.prop

This file was deleted.

46 changes: 0 additions & 46 deletions zip_static/common/phh/service.sh

This file was deleted.

0 comments on commit 298f094

Please sign in to comment.