2016-09-14 04:31:13 +02:00
|
|
|
#!/system/bin/sh
|
|
|
|
|
|
|
|
LOGFILE=/cache/magisk.log
|
2017-02-05 16:40:50 +01:00
|
|
|
DISABLEFILE=/cache/.disable_magisk
|
2017-02-05 20:22:37 +01:00
|
|
|
UNINSTALLER=/cache/magisk_uninstaller.sh
|
2016-09-14 04:31:13 +02:00
|
|
|
IMG=/data/magisk.img
|
2017-01-03 02:37:03 +01:00
|
|
|
WHITELIST="/system/bin"
|
2016-09-14 04:31:13 +02:00
|
|
|
|
2016-10-30 00:49:29 +02:00
|
|
|
MOUNTPOINT=/magisk
|
2016-10-06 11:53:52 +02:00
|
|
|
COREDIR=$MOUNTPOINT/.core
|
2016-09-14 04:31:13 +02:00
|
|
|
|
2016-10-30 00:49:29 +02:00
|
|
|
TMPDIR=/dev/magisk
|
|
|
|
DUMMDIR=$TMPDIR/dummy
|
|
|
|
MIRRDIR=$TMPDIR/mirror
|
|
|
|
MOUNTINFO=$TMPDIR/mnt
|
2016-09-14 04:31:13 +02:00
|
|
|
|
2016-10-30 00:49:29 +02:00
|
|
|
# Use the included busybox for maximum compatibility and reliable results
|
|
|
|
# e.g. we rely on the option "-c" for cp (reserve contexts), and -exec for find
|
2017-02-04 11:44:07 +01:00
|
|
|
TOOLPATH=/dev/busybox
|
2017-03-27 01:23:20 +02:00
|
|
|
DATABIN=/data/magisk
|
|
|
|
MAGISKBIN=$COREDIR/bin
|
2017-02-04 11:44:07 +01:00
|
|
|
OLDPATH=$PATH
|
|
|
|
export PATH=$TOOLPATH:$OLDPATH
|
|
|
|
|
|
|
|
APPDIR=/data/data/com.topjohnwu.magisk/files
|
2016-11-28 21:16:01 +01:00
|
|
|
|
2016-11-13 09:58:43 +01:00
|
|
|
# Default permissions
|
|
|
|
umask 022
|
|
|
|
|
2016-09-14 04:31:13 +02:00
|
|
|
log_print() {
|
2016-10-31 21:21:43 +01:00
|
|
|
echo "$1"
|
|
|
|
echo "$1" >> $LOGFILE
|
2016-09-14 04:31:13 +02:00
|
|
|
log -p i -t Magisk "$1"
|
|
|
|
}
|
|
|
|
|
|
|
|
mktouch() {
|
2017-01-06 04:20:16 +01:00
|
|
|
mkdir -p "${1%/*}" 2>/dev/null
|
2016-09-14 04:31:13 +02:00
|
|
|
if [ -z "$2" ]; then
|
2016-11-13 21:30:05 +01:00
|
|
|
touch "$1" 2>/dev/null
|
2016-09-14 04:31:13 +02:00
|
|
|
else
|
2016-11-13 21:30:05 +01:00
|
|
|
echo "$2" > "$1" 2>/dev/null
|
2016-09-14 04:31:13 +02:00
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2017-01-01 12:45:06 +01:00
|
|
|
in_list() {
|
|
|
|
for i in $2; do
|
|
|
|
[ "$1" = "$i" ] && return 0
|
|
|
|
done
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
2016-09-14 04:31:13 +02:00
|
|
|
unblock() {
|
2016-10-02 22:34:50 +02:00
|
|
|
touch /dev/.magisk.unblock
|
2016-09-14 04:31:13 +02:00
|
|
|
exit
|
|
|
|
}
|
|
|
|
|
2017-03-14 20:43:28 +01:00
|
|
|
bind_mount() {
|
|
|
|
if [ -e "$1" -a -e "$2" ]; then
|
|
|
|
mount -o bind "$1" "$2" || log_print "Mount Fail: $1 -> $2"
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
loopsetup() {
|
|
|
|
LOOPDEVICE=
|
|
|
|
for DEV in `ls /dev/block/loop*`; do
|
|
|
|
if losetup $DEV $1; then
|
|
|
|
LOOPDEVICE=$DEV
|
|
|
|
break
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
}
|
|
|
|
|
2017-03-18 09:57:04 +01:00
|
|
|
image_size_check() {
|
|
|
|
e2fsck -yf $1
|
2017-03-20 21:25:49 +01:00
|
|
|
curBlocks=`e2fsck -n $1 2>/dev/null | grep $1 | cut -d, -f3 | cut -d\ -f2`;
|
2017-03-18 09:57:04 +01:00
|
|
|
curUsedM=`echo "$curBlocks" | cut -d/ -f1`
|
|
|
|
curSizeM=`echo "$curBlocks" | cut -d/ -f1`
|
|
|
|
curFreeM=$(((curSizeM - curUsedM) * 4 / 1024))
|
|
|
|
curUsedM=$((curUsedM * 4 / 1024 + 1))
|
|
|
|
curSizeM=$((curSizeM * 4 / 1024))
|
2017-03-14 20:43:28 +01:00
|
|
|
}
|
|
|
|
|
2017-03-29 20:29:10 +02:00
|
|
|
module_scripts() {
|
2016-10-06 11:53:52 +02:00
|
|
|
BASE=$MOUNTPOINT
|
2016-09-14 04:31:13 +02:00
|
|
|
for MOD in $BASE/* ; do
|
2017-03-29 20:29:10 +02:00
|
|
|
if [ ! -f $MOD/disable -a -f $MOD/$1.sh ]; then
|
|
|
|
chmod 755 $MOD/$1.sh
|
|
|
|
chcon u:object_r:system_file:s0 $MOD/$1.sh
|
|
|
|
log_print "$1: $MOD/$1.sh"
|
|
|
|
sh $MOD/$1.sh
|
2016-09-14 04:31:13 +02:00
|
|
|
fi
|
|
|
|
done
|
2017-03-29 20:29:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
general_scripts() {
|
2017-02-05 17:13:25 +01:00
|
|
|
for SCRIPT in $COREDIR/${1}.d/* ; do
|
|
|
|
if [ -f "$SCRIPT" ]; then
|
|
|
|
chmod 755 $SCRIPT
|
|
|
|
chcon u:object_r:system_file:s0 $SCRIPT
|
|
|
|
log_print "${1}.d: $SCRIPT"
|
|
|
|
sh $SCRIPT
|
|
|
|
fi
|
|
|
|
done
|
2016-09-14 04:31:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
travel() {
|
2017-01-06 04:20:16 +01:00
|
|
|
cd "$TRAVEL_ROOT/$1"
|
2016-12-08 09:50:52 +01:00
|
|
|
if [ -f .replace ]; then
|
2017-03-14 20:43:28 +01:00
|
|
|
log_print "Replace: /$1"
|
2017-01-06 04:20:16 +01:00
|
|
|
rm -rf "$MOUNTINFO/$1"
|
|
|
|
mktouch "$MOUNTINFO/$1" "$TRAVEL_ROOT"
|
2016-09-14 04:31:13 +02:00
|
|
|
else
|
|
|
|
for ITEM in * ; do
|
2017-03-14 20:43:28 +01:00
|
|
|
# This means it's an empty folder (shouldn't happen, but better to be safe)
|
|
|
|
[ "$ITEM" = "*" ] && return
|
|
|
|
# Ignore /system/vendor since we will handle it differently
|
|
|
|
[ "$1" = "system" -a "$ITEM" = "vendor" ] && continue
|
|
|
|
|
2017-02-04 18:36:07 +01:00
|
|
|
# Target not found or target/file is a symlink
|
|
|
|
if [ ! -e "/$1/$ITEM" -o -L "/$1/$ITEM" -o -L "$ITEM" ]; then
|
2016-12-08 09:50:52 +01:00
|
|
|
# If we are in a higher level, delete the lower levels
|
2017-02-04 18:36:07 +01:00
|
|
|
rm -rf "$MOUNTINFO/dummy/$1" 2>/dev/null
|
2016-12-08 09:50:52 +01:00
|
|
|
# Mount the dummy parent
|
2017-03-14 20:43:28 +01:00
|
|
|
log_print "Replace with dummy: /$1"
|
2017-01-06 04:20:16 +01:00
|
|
|
mktouch "$MOUNTINFO/dummy/$1"
|
2016-12-08 09:50:52 +01:00
|
|
|
|
2017-03-14 20:43:28 +01:00
|
|
|
if [ -L "$ITEM" ]; then
|
2017-02-04 18:36:07 +01:00
|
|
|
# Copy symlinks
|
2017-03-14 20:43:28 +01:00
|
|
|
log_print "Symlink: /$1/$ITEM"
|
2017-01-06 04:20:16 +01:00
|
|
|
mkdir -p "$DUMMDIR/$1" 2>/dev/null
|
2017-03-20 22:15:13 +01:00
|
|
|
cp -afc "$ITEM" "$DUMMDIR/$1/$ITEM"
|
2017-03-14 20:43:28 +01:00
|
|
|
elif [ -d "$ITEM" ]; then
|
|
|
|
# Create new dummy directory and mount it
|
|
|
|
log_print "New directory: /$1/$ITEM"
|
|
|
|
mkdir -p "$DUMMDIR/$1/$ITEM"
|
|
|
|
mktouch "$MOUNTINFO/$1/$ITEM" "$TRAVEL_ROOT"
|
2016-09-14 04:31:13 +02:00
|
|
|
else
|
2016-12-08 09:50:52 +01:00
|
|
|
# Create new dummy file and mount it
|
2017-03-14 20:43:28 +01:00
|
|
|
log_print "New file: /$1/$ITEM"
|
2017-01-06 04:20:16 +01:00
|
|
|
mktouch "$DUMMDIR/$1/$ITEM"
|
|
|
|
mktouch "$MOUNTINFO/$1/$ITEM" "$TRAVEL_ROOT"
|
2016-09-14 04:31:13 +02:00
|
|
|
fi
|
2016-11-05 21:47:54 +01:00
|
|
|
else
|
2017-01-06 04:20:16 +01:00
|
|
|
if [ -d "$ITEM" ]; then
|
2016-11-05 21:47:54 +01:00
|
|
|
# It's an directory, travel deeper
|
2017-01-06 04:20:16 +01:00
|
|
|
(travel "$1/$ITEM")
|
|
|
|
elif [ ! -L "$ITEM" ]; then
|
2016-11-05 21:47:54 +01:00
|
|
|
# Mount this file
|
2017-03-14 20:43:28 +01:00
|
|
|
log_print "Replace: /$1/$ITEM"
|
2017-01-06 04:20:16 +01:00
|
|
|
mktouch "$MOUNTINFO/$1/$ITEM" "$TRAVEL_ROOT"
|
2016-11-05 21:47:54 +01:00
|
|
|
fi
|
2016-09-14 04:31:13 +02:00
|
|
|
fi
|
2016-11-05 21:47:54 +01:00
|
|
|
done
|
|
|
|
fi
|
|
|
|
}
|
2016-09-14 04:31:13 +02:00
|
|
|
|
2016-11-05 21:47:54 +01:00
|
|
|
clone_dummy() {
|
2017-01-02 10:48:34 +01:00
|
|
|
LINK=false
|
2017-01-06 04:20:16 +01:00
|
|
|
in_list "$1" "$WHITELIST" && LINK=true
|
2017-01-01 12:45:06 +01:00
|
|
|
|
2016-12-08 09:50:52 +01:00
|
|
|
for ITEM in $MIRRDIR$1/* ; do
|
2017-01-06 04:20:16 +01:00
|
|
|
REAL="${ITEM#$MIRRDIR}"
|
|
|
|
if [ -d "$MOUNTINFO$REAL" ]; then
|
2016-12-08 14:31:34 +01:00
|
|
|
# Need to clone deeper
|
2017-01-06 04:20:16 +01:00
|
|
|
mkdir -p "$DUMMDIR$REAL"
|
|
|
|
(clone_dummy "$REAL")
|
2017-02-04 18:36:07 +01:00
|
|
|
elif [ ! -f "$DUMMDIR$REAL" ]; then
|
|
|
|
# It's not the file to be added/replaced, clone it
|
2017-01-06 04:20:16 +01:00
|
|
|
if [ -L "$ITEM" ]; then
|
2016-12-08 09:50:52 +01:00
|
|
|
# Copy original symlink
|
2017-01-06 04:20:16 +01:00
|
|
|
cp -afc "$ITEM" "$DUMMDIR$REAL"
|
2016-12-08 09:50:52 +01:00
|
|
|
else
|
2017-01-06 04:20:16 +01:00
|
|
|
if $LINK && [ ! -e "$MOUNTINFO$REAL" ]; then
|
2017-03-18 22:00:12 +01:00
|
|
|
ln -sf "$MIRRDIR$REAL" "$DUMMDIR$REAL"
|
2016-11-28 21:16:01 +01:00
|
|
|
else
|
2017-01-06 04:20:16 +01:00
|
|
|
if [ -d "$ITEM" ]; then
|
|
|
|
mkdir -p "$DUMMDIR$REAL"
|
2017-01-01 12:45:06 +01:00
|
|
|
else
|
2017-01-06 04:20:16 +01:00
|
|
|
mktouch "$DUMMDIR$REAL"
|
2017-01-01 12:45:06 +01:00
|
|
|
fi
|
2017-03-14 20:43:28 +01:00
|
|
|
if [ ! -e "$MOUNTINFO$REAL" ]; then
|
|
|
|
log_print "Clone skeleton: $REAL"
|
|
|
|
mktouch "$MOUNTINFO/mirror$REAL"
|
|
|
|
fi
|
2016-11-28 21:16:01 +01:00
|
|
|
fi
|
2016-09-14 04:31:13 +02:00
|
|
|
fi
|
2016-11-05 21:47:54 +01:00
|
|
|
fi
|
|
|
|
done
|
2016-09-14 04:31:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
merge_image() {
|
2016-12-08 09:50:52 +01:00
|
|
|
if [ -f $1 ]; then
|
2016-09-14 04:31:13 +02:00
|
|
|
log_print "$1 found"
|
2016-12-08 09:50:52 +01:00
|
|
|
if [ -f $IMG ]; then
|
2016-09-14 04:31:13 +02:00
|
|
|
log_print "$IMG found, attempt to merge"
|
|
|
|
|
|
|
|
# Handle large images
|
2017-03-18 09:57:04 +01:00
|
|
|
image_size_check $1
|
|
|
|
mergeUsedM=$curUsedM
|
|
|
|
image_size_check $IMG
|
|
|
|
if [ "$mergeUsedM" -gt "$curFreeM" ]; then
|
|
|
|
NEWDATASIZE=$(((mergeUsedM + curUsedM) / 32 * 32 + 32))
|
2016-09-14 04:31:13 +02:00
|
|
|
log_print "Expanding $IMG to ${NEWDATASIZE}M..."
|
|
|
|
resize2fs $IMG ${NEWDATASIZE}M
|
|
|
|
fi
|
|
|
|
|
|
|
|
# Start merging
|
|
|
|
mkdir /cache/data_img
|
|
|
|
mkdir /cache/merge_img
|
|
|
|
|
|
|
|
# setup loop devices
|
|
|
|
loopsetup $IMG
|
|
|
|
LOOPDATA=$LOOPDEVICE
|
|
|
|
log_print "$LOOPDATA $IMG"
|
|
|
|
|
|
|
|
loopsetup $1
|
|
|
|
LOOPMERGE=$LOOPDEVICE
|
|
|
|
log_print "$LOOPMERGE $1"
|
|
|
|
|
2016-12-08 09:50:52 +01:00
|
|
|
if [ ! -z $LOOPDATA -a ! -z $LOOPMERGE ]; then
|
|
|
|
# if loop devices have been setup, mount images
|
|
|
|
OK=false
|
|
|
|
mount -t ext4 -o rw,noatime $LOOPDATA /cache/data_img && \
|
|
|
|
mount -t ext4 -o rw,noatime $LOOPMERGE /cache/merge_img && \
|
|
|
|
OK=true
|
|
|
|
|
|
|
|
if $OK; then
|
|
|
|
# Merge (will reserve selinux contexts)
|
|
|
|
cd /cache/merge_img
|
|
|
|
for MOD in *; do
|
|
|
|
if [ "$MOD" != "lost+found" ]; then
|
|
|
|
log_print "Merging: $MOD"
|
|
|
|
rm -rf /cache/data_img/$MOD
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
cp -afc . /cache/data_img
|
|
|
|
log_print "Merge complete"
|
|
|
|
cd /
|
2016-09-14 04:31:13 +02:00
|
|
|
fi
|
2016-12-08 09:50:52 +01:00
|
|
|
|
|
|
|
umount /cache/data_img
|
|
|
|
umount /cache/merge_img
|
2016-09-14 04:31:13 +02:00
|
|
|
fi
|
|
|
|
|
2016-11-13 21:30:05 +01:00
|
|
|
losetup -d $LOOPDATA
|
|
|
|
losetup -d $LOOPMERGE
|
2016-09-14 04:31:13 +02:00
|
|
|
|
|
|
|
rmdir /cache/data_img
|
|
|
|
rmdir /cache/merge_img
|
|
|
|
else
|
|
|
|
log_print "Moving $1 to $IMG "
|
|
|
|
mv $1 $IMG
|
|
|
|
fi
|
|
|
|
rm -f $1
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
case $1 in
|
|
|
|
post-fs )
|
|
|
|
mv $LOGFILE /cache/last_magisk.log
|
|
|
|
touch $LOGFILE
|
|
|
|
chmod 644 $LOGFILE
|
|
|
|
|
2016-10-30 00:49:29 +02:00
|
|
|
# No more cache mods!
|
|
|
|
# Only for multirom!
|
2016-09-14 04:31:13 +02:00
|
|
|
|
2016-11-08 22:17:14 +01:00
|
|
|
log_print "** Magisk post-fs mode running..."
|
|
|
|
|
2016-12-08 09:50:52 +01:00
|
|
|
# Cleanup legacy stuffs...
|
2016-11-07 16:57:21 +01:00
|
|
|
rm -rf /cache/magisk /cache/magisk_merge /cache/magiskhide.log
|
2016-11-04 19:38:02 +01:00
|
|
|
|
2017-02-05 20:22:37 +01:00
|
|
|
[ -f $DISABLEFILE -o -f $UNINSTALLER ] && unblock
|
2017-02-05 16:40:50 +01:00
|
|
|
|
2016-12-08 09:50:52 +01:00
|
|
|
if [ -d /cache/magisk_mount ]; then
|
2016-12-06 18:45:09 +01:00
|
|
|
log_print "* Mounting cache files"
|
2016-11-13 14:02:35 +01:00
|
|
|
find /cache/magisk_mount -type f 2>/dev/null | while read ITEM ; do
|
2017-01-06 04:20:16 +01:00
|
|
|
chmod 644 "$ITEM"
|
|
|
|
chcon u:object_r:system_file:s0 "$ITEM"
|
|
|
|
TARGET="${ITEM#/cache/magisk_mount}"
|
|
|
|
bind_mount "$ITEM" "$TARGET"
|
2016-11-08 22:17:14 +01:00
|
|
|
done
|
|
|
|
fi
|
|
|
|
|
2016-09-14 04:31:13 +02:00
|
|
|
unblock
|
|
|
|
;;
|
|
|
|
|
|
|
|
post-fs-data )
|
2016-12-08 09:50:52 +01:00
|
|
|
# /data not mounted yet
|
2017-01-01 12:45:06 +01:00
|
|
|
! mount | grep " /data " >/dev/null && unblock
|
|
|
|
mount | grep " /data " | grep "tmpfs" >/dev/null && unblock
|
2016-09-14 04:31:13 +02:00
|
|
|
|
2016-10-30 00:49:29 +02:00
|
|
|
# Don't run twice
|
2016-11-04 19:38:02 +01:00
|
|
|
if [ "`getprop magisk.restart_pfsd`" != "1" ]; then
|
2016-09-14 04:31:13 +02:00
|
|
|
|
2016-10-31 21:21:43 +01:00
|
|
|
log_print "** Magisk post-fs-data mode running..."
|
2016-10-02 22:34:50 +02:00
|
|
|
|
2016-10-30 00:49:29 +02:00
|
|
|
# Cache support
|
2017-03-15 12:32:25 +01:00
|
|
|
mv /cache/stock_boot* /data 2>/dev/null
|
2017-02-04 11:44:07 +01:00
|
|
|
if [ -d /cache/data_bin ]; then
|
2017-03-27 01:23:20 +02:00
|
|
|
rm -rf $DATABIN
|
|
|
|
mv /cache/data_bin $DATABIN
|
|
|
|
chmod -R 755 $DATABIN
|
|
|
|
chown -R 0.0 $DATABIN
|
2016-09-14 04:31:13 +02:00
|
|
|
fi
|
|
|
|
|
2017-01-06 04:20:16 +01:00
|
|
|
# Set up environment
|
2017-02-04 11:44:07 +01:00
|
|
|
mkdir -p $TOOLPATH
|
2017-03-27 01:23:20 +02:00
|
|
|
$DATABIN/busybox --install -s $TOOLPATH
|
|
|
|
ln -sf $DATABIN/busybox $TOOLPATH/busybox
|
2017-02-04 11:44:07 +01:00
|
|
|
# Prevent issues
|
|
|
|
rm -f $TOOLPATH/su $TOOLPATH/sh $TOOLPATH/reboot
|
2017-03-27 01:23:20 +02:00
|
|
|
find $DATABIN $TOOLPATH -exec chcon -h u:object_r:system_file:s0 {} \;
|
2016-09-14 04:31:13 +02:00
|
|
|
|
2017-03-27 01:23:20 +02:00
|
|
|
if [ -f $UNINSTALLER ]; then
|
|
|
|
touch /dev/.magisk.unblock
|
2017-03-29 20:29:10 +02:00
|
|
|
(BOOTMODE=true sh $UNINSTALLER) &
|
2017-03-27 01:23:20 +02:00
|
|
|
exit
|
2017-03-20 21:25:49 +01:00
|
|
|
fi
|
2016-11-28 21:16:01 +01:00
|
|
|
|
2017-03-30 17:31:37 +02:00
|
|
|
if [ -f $DATABIN/magisk.apk ]; then
|
|
|
|
if ! ls /data/app | grep com.topjohnwu.magisk; then
|
|
|
|
mkdir /data/app/com.topjohnwu.magisk-1
|
|
|
|
cp $DATABIN/magisk.apk /data/app/com.topjohnwu.magisk-1/base.apk
|
|
|
|
chown 1000.1000 /data/app/com.topjohnwu.magisk-1
|
|
|
|
chown 1000.1000 /data/app/com.topjohnwu.magisk-1/base.apk
|
|
|
|
chmod 755 /data/app/com.topjohnwu.magisk-1
|
|
|
|
chmod 644 /data/app/com.topjohnwu.magisk-1/base.apk
|
|
|
|
chcon u:object_r:apk_data_file:s0 /data/app/com.topjohnwu.magisk-1
|
|
|
|
chcon u:object_r:apk_data_file:s0 /data/app/com.topjohnwu.magisk-1/base.apk
|
|
|
|
fi
|
|
|
|
rm -f $DATABIN/magisk.apk 2>/dev/null
|
|
|
|
fi
|
|
|
|
|
2016-10-30 00:49:29 +02:00
|
|
|
# Image merging
|
|
|
|
chmod 644 $IMG /cache/magisk.img /data/magisk_merge.img 2>/dev/null
|
|
|
|
merge_image /cache/magisk.img
|
|
|
|
merge_image /data/magisk_merge.img
|
2016-09-14 04:31:13 +02:00
|
|
|
|
2016-10-30 00:49:29 +02:00
|
|
|
# Mount magisk.img
|
2016-12-06 18:45:09 +01:00
|
|
|
[ ! -d $MOUNTPOINT ] && mkdir -p $MOUNTPOINT
|
2016-12-08 09:50:52 +01:00
|
|
|
if ! mount | grep $MOUNTPOINT; then
|
2016-10-30 00:49:29 +02:00
|
|
|
loopsetup $IMG
|
2016-12-08 09:50:52 +01:00
|
|
|
[ ! -z $LOOPDEVICE ] && mount -t ext4 -o rw,noatime $LOOPDEVICE $MOUNTPOINT
|
|
|
|
if [ $? -ne 0 ]; then
|
|
|
|
log_print "magisk.img mount failed, nothing to do :("
|
|
|
|
unblock
|
2016-10-30 00:49:29 +02:00
|
|
|
fi
|
2016-09-14 04:31:13 +02:00
|
|
|
fi
|
2016-10-30 00:49:29 +02:00
|
|
|
|
2016-12-06 18:45:09 +01:00
|
|
|
# Remove empty directories, legacy paths, symlinks, old temporary images
|
2016-11-13 21:30:05 +01:00
|
|
|
find $MOUNTPOINT -type d -depth ! -path "*core*" -exec rmdir {} \; 2>/dev/null
|
2017-03-27 01:23:20 +02:00
|
|
|
rm -rf $MOUNTPOINT/zzsupersu $MOUNTPOINT/phh $COREDIR/dummy $COREDIR/mirror \
|
2017-03-18 22:00:12 +01:00
|
|
|
$COREDIR/busybox $COREDIR/su /data/magisk/*.img /data/busybox 2>/dev/null
|
2016-09-14 04:31:13 +02:00
|
|
|
|
2017-01-31 23:02:43 +01:00
|
|
|
# Remove modules that are labeled to be removed
|
2016-10-30 00:49:29 +02:00
|
|
|
for MOD in $MOUNTPOINT/* ; do
|
2017-01-01 12:45:06 +01:00
|
|
|
rm -f $MOD/system/placeholder 2>/dev/null
|
2017-01-06 04:20:16 +01:00
|
|
|
if [ -f $MOD/remove ]; then
|
2016-10-30 00:49:29 +02:00
|
|
|
log_print "Remove module: $MOD"
|
|
|
|
rm -rf $MOD
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
|
|
|
|
# Unmount, shrink, remount
|
2016-12-08 09:50:52 +01:00
|
|
|
if umount $MOUNTPOINT; then
|
2017-01-06 04:20:16 +01:00
|
|
|
losetup -d $LOOPDEVICE 2>/dev/null
|
2017-03-18 09:57:04 +01:00
|
|
|
image_size_check $IMG
|
|
|
|
NEWDATASIZE=$((curUsedM / 32 * 32 + 32))
|
2016-10-30 00:49:29 +02:00
|
|
|
if [ "$curSizeM" -gt "$NEWDATASIZE" ]; then
|
|
|
|
log_print "Shrinking $IMG to ${NEWDATASIZE}M..."
|
|
|
|
resize2fs $IMG ${NEWDATASIZE}M
|
|
|
|
fi
|
|
|
|
loopsetup $IMG
|
2016-12-08 09:50:52 +01:00
|
|
|
[ ! -z $LOOPDEVICE ] && mount -t ext4 -o rw,noatime $LOOPDEVICE $MOUNTPOINT
|
|
|
|
if [ $? -ne 0 ]; then
|
2016-10-30 00:49:29 +02:00
|
|
|
log_print "magisk.img mount failed, nothing to do :("
|
|
|
|
unblock
|
|
|
|
fi
|
|
|
|
fi
|
2016-09-14 04:31:13 +02:00
|
|
|
|
2017-03-29 20:29:10 +02:00
|
|
|
log_print "* Running post-fs-data.d"
|
|
|
|
general_scripts post-fs-data
|
2017-03-27 01:23:20 +02:00
|
|
|
|
2017-03-29 20:47:40 +02:00
|
|
|
log_print "* Loading core props"
|
|
|
|
for PROP in $COREDIR/props/* ; do
|
|
|
|
if [ -f $PROP ]; then
|
|
|
|
log_print "Load prop: $PROP"
|
|
|
|
$MAGISKBIN/resetprop --file $PROP
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
|
2017-03-27 01:23:20 +02:00
|
|
|
# Exit if disabled
|
|
|
|
[ -f $DISABLEFILE ] && unblock
|
2017-03-29 20:29:10 +02:00
|
|
|
|
|
|
|
######################
|
|
|
|
# Core features done #
|
|
|
|
######################
|
2017-03-27 01:23:20 +02:00
|
|
|
|
|
|
|
# Multirom functions should go here, not available right now
|
|
|
|
MULTIROM=false
|
|
|
|
|
2017-01-31 23:02:43 +01:00
|
|
|
log_print "* Preparing modules"
|
2017-01-01 19:00:53 +01:00
|
|
|
|
2017-01-01 12:45:06 +01:00
|
|
|
# Remove crap folder
|
|
|
|
rm -rf $MOUNTPOINT/lost+found
|
|
|
|
|
2017-01-02 10:48:34 +01:00
|
|
|
# Link vendor if not exist
|
|
|
|
if [ ! -e /vendor ]; then
|
|
|
|
mount -o rw,remount rootfs /
|
2017-03-18 22:00:12 +01:00
|
|
|
ln -sf /system/vendor /vendor
|
2017-01-02 10:48:34 +01:00
|
|
|
mount -o ro,remount rootfs /
|
|
|
|
fi
|
|
|
|
|
2016-10-30 00:49:29 +02:00
|
|
|
for MOD in $MOUNTPOINT/* ; do
|
2017-03-18 09:57:04 +01:00
|
|
|
if [ ! -f $MOD/disable ]; then
|
|
|
|
# Travel through all mods
|
|
|
|
if [ -f $MOD/auto_mount -a -d $MOD/system ]; then
|
|
|
|
log_print "Analyzing module: $MOD"
|
|
|
|
TRAVEL_ROOT=$MOD
|
|
|
|
(travel system)
|
|
|
|
rm -f $MOD/vendor 2>/dev/null
|
|
|
|
if [ -d $MOD/system/vendor ]; then
|
2017-03-18 22:00:12 +01:00
|
|
|
ln -sf $MOD/system/vendor $MOD/vendor
|
2017-03-18 09:57:04 +01:00
|
|
|
(travel vendor)
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
# Read in defined system props
|
|
|
|
if [ -f $MOD/system.prop ]; then
|
|
|
|
log_print "* Reading props from $MOD/system.prop"
|
2017-03-27 01:23:20 +02:00
|
|
|
$MAGISKBIN/resetprop --file $MOD/system.prop
|
2017-01-02 10:48:34 +01:00
|
|
|
fi
|
2016-10-30 00:49:29 +02:00
|
|
|
fi
|
|
|
|
done
|
|
|
|
|
2016-11-13 21:30:05 +01:00
|
|
|
# Proper permissions for generated items
|
2017-01-06 04:20:16 +01:00
|
|
|
find $TMPDIR -exec chcon -h u:object_r:system_file:s0 {} \;
|
2016-11-13 21:30:05 +01:00
|
|
|
|
2016-12-06 18:45:09 +01:00
|
|
|
# linker(64), t*box required for bin
|
2016-12-08 09:50:52 +01:00
|
|
|
if [ -f $MOUNTINFO/dummy/system/bin ]; then
|
2016-12-06 18:45:09 +01:00
|
|
|
cp -afc /system/bin/linker* /system/bin/t*box $DUMMDIR/system/bin/
|
2016-11-13 21:30:05 +01:00
|
|
|
fi
|
|
|
|
|
2016-10-30 00:49:29 +02:00
|
|
|
# Start doing tasks
|
2016-12-08 09:50:52 +01:00
|
|
|
|
2016-10-30 00:49:29 +02:00
|
|
|
# Stage 1
|
2016-12-08 09:50:52 +01:00
|
|
|
log_print "* Stage 1: Mount system and vendor mirrors"
|
|
|
|
SYSTEMBLOCK=`mount | grep " /system " | awk '{print $1}'`
|
|
|
|
mkdir -p $MIRRDIR/system
|
|
|
|
mount -o ro $SYSTEMBLOCK $MIRRDIR/system
|
|
|
|
if [ `mount | grep -c " /vendor "` -ne 0 ]; then
|
|
|
|
VENDORBLOCK=`mount | grep " /vendor " | awk '{print $1}'`
|
|
|
|
mkdir -p $MIRRDIR/vendor
|
|
|
|
mount -o ro $VENDORBLOCK $MIRRDIR/vendor
|
|
|
|
else
|
2017-03-18 22:00:12 +01:00
|
|
|
ln -sf $MIRRDIR/system/vendor $MIRRDIR/vendor
|
2016-12-08 09:50:52 +01:00
|
|
|
fi
|
2017-01-01 12:45:06 +01:00
|
|
|
|
2017-01-02 10:48:34 +01:00
|
|
|
# Since mirrors always exist, we load libraries and binaries from mirrors
|
2017-01-01 12:45:06 +01:00
|
|
|
export LD_LIBRARY_PATH=$MIRRDIR/system/lib:$MIRRDIR/vendor/lib
|
2017-01-02 10:48:34 +01:00
|
|
|
[ -d $MIRRDIR/system/lib64 ] && export LD_LIBRARY_PATH=$MIRRDIR/system/lib64:$MIRRDIR/vendor/lib64
|
2016-12-08 09:50:52 +01:00
|
|
|
|
|
|
|
# Stage 2
|
|
|
|
log_print "* Stage 2: Mount dummy skeletons"
|
2017-01-02 10:48:34 +01:00
|
|
|
# Move /system/vendor to /vendor for consistency
|
2016-12-08 09:50:52 +01:00
|
|
|
mv -f $MOUNTINFO/dummy/system/vendor $MOUNTINFO/dummy/vendor 2>/dev/null
|
|
|
|
mv -f $DUMMDIR/system/vendor $DUMMDIR/vendor 2>/dev/null
|
2016-11-13 21:30:05 +01:00
|
|
|
find $MOUNTINFO/dummy -type f 2>/dev/null | while read ITEM ; do
|
2017-01-06 04:20:16 +01:00
|
|
|
TARGET="${ITEM#$MOUNTINFO/dummy}"
|
|
|
|
ORIG="$DUMMDIR$TARGET"
|
|
|
|
(clone_dummy "$TARGET")
|
|
|
|
bind_mount "$ORIG" "$TARGET"
|
2016-10-30 00:49:29 +02:00
|
|
|
done
|
|
|
|
|
2017-01-02 10:48:34 +01:00
|
|
|
# Check if the dummy /system/bin is empty, it shouldn't
|
2017-01-03 02:37:03 +01:00
|
|
|
[ -e $DUMMDIR/system/bin -a ! -e $DUMMDIR/system/bin/sh ] && clone_dummy /system/bin
|
2017-01-02 10:48:34 +01:00
|
|
|
|
2016-12-08 09:50:52 +01:00
|
|
|
# Stage 3
|
|
|
|
log_print "* Stage 3: Mount module items"
|
2017-01-06 04:20:16 +01:00
|
|
|
find $MOUNTINFO/system $MOUNTINFO/vendor -type f 2>/dev/null | while read ITEM ; do
|
|
|
|
TARGET="${ITEM#$MOUNTINFO}"
|
|
|
|
ORIG="`cat "$ITEM"`$TARGET"
|
|
|
|
bind_mount "$ORIG" "$TARGET"
|
2017-01-02 10:48:34 +01:00
|
|
|
done
|
2016-10-30 00:49:29 +02:00
|
|
|
|
2016-12-08 09:50:52 +01:00
|
|
|
# Stage 4
|
2017-03-29 20:29:10 +02:00
|
|
|
log_print "* Stage 4: Execute module scripts"
|
|
|
|
module_scripts post-fs-data
|
2016-10-30 00:49:29 +02:00
|
|
|
|
2016-12-08 09:50:52 +01:00
|
|
|
# Stage 5
|
|
|
|
log_print "* Stage 5: Mount mirrored items back to dummy"
|
|
|
|
find $MOUNTINFO/mirror -type f 2>/dev/null | while read ITEM ; do
|
2017-01-06 04:20:16 +01:00
|
|
|
TARGET="${ITEM#$MOUNTINFO/mirror}"
|
|
|
|
ORIG="$MIRRDIR$TARGET"
|
|
|
|
bind_mount "$ORIG" "$TARGET"
|
2016-12-08 09:50:52 +01:00
|
|
|
done
|
|
|
|
|
2016-11-04 19:38:02 +01:00
|
|
|
# Restart post-fs-data if necessary (multirom)
|
2016-12-06 18:45:09 +01:00
|
|
|
$MULTIROM && setprop magisk.restart_pfsd 1
|
2016-10-30 00:49:29 +02:00
|
|
|
|
|
|
|
fi
|
2016-09-14 04:31:13 +02:00
|
|
|
unblock
|
|
|
|
;;
|
|
|
|
|
2017-02-04 11:44:07 +01:00
|
|
|
mount_busybox )
|
|
|
|
log_print "* Enabling BusyBox"
|
|
|
|
cp -afc /system/xbin/. $TOOLPATH
|
|
|
|
umount /system/xbin 2>/dev/null
|
|
|
|
bind_mount $TOOLPATH /system/xbin
|
|
|
|
;;
|
|
|
|
|
2016-09-14 04:31:13 +02:00
|
|
|
service )
|
2016-10-02 22:34:50 +02:00
|
|
|
# Version info
|
2016-10-30 12:11:26 +01:00
|
|
|
MAGISK_VERSION_STUB
|
2016-10-31 21:21:43 +01:00
|
|
|
log_print "** Magisk late_start service mode running..."
|
2017-03-26 15:47:54 +02:00
|
|
|
|
2017-03-30 17:31:37 +02:00
|
|
|
# Bind hosts for Adblock apps
|
|
|
|
if [ -f $COREDIR/hosts ]; then
|
|
|
|
log_print "* Enabling systemless hosts file support"
|
|
|
|
bind_mount $COREDIR/hosts /system/etc/hosts
|
|
|
|
fi
|
|
|
|
# Expose busybox
|
|
|
|
[ "`getprop persist.magisk.busybox`" = "1" ] && sh /sbin/magic_mask.sh mount_busybox
|
|
|
|
|
2017-03-29 20:29:10 +02:00
|
|
|
# Live patch sepolicy
|
|
|
|
$MAGISKBIN/magiskpolicy --live --magisk
|
|
|
|
|
|
|
|
log_print "* Linking binaries to /sbin"
|
|
|
|
mount -o rw,remount rootfs /
|
|
|
|
chmod 755 /sbin
|
|
|
|
ln -sf $MAGISKBIN/magiskpolicy /sbin/magiskpolicy
|
|
|
|
ln -sf $MAGISKBIN/magiskpolicy /sbin/sepolicy-inject
|
|
|
|
ln -sf $MAGISKBIN/resetprop /sbin/resetprop
|
|
|
|
if [ ! -f /sbin/launch_daemonsu.sh ]; then
|
|
|
|
log_print "* Starting MagiskSU"
|
|
|
|
export PATH=$OLDPATH
|
|
|
|
ln -sf $MAGISKBIN/su /sbin/su
|
|
|
|
ln -sf $MAGISKBIN/magiskpolicy /sbin/supolicy
|
|
|
|
/sbin/su --daemon
|
|
|
|
export PATH=$TOOLPATH:$OLDPATH
|
|
|
|
fi
|
|
|
|
mount -o ro,remount rootfs /
|
|
|
|
|
|
|
|
log_print "* Running service.d"
|
|
|
|
general_scripts service
|
|
|
|
|
2017-03-26 15:47:54 +02:00
|
|
|
# Start MagiskHide
|
2017-03-30 17:31:37 +02:00
|
|
|
if [ "`getprop persist.magisk.hide`" = "1" ]; then
|
|
|
|
log_print "* Starting MagiskHide"
|
|
|
|
sh $COREDIR/magiskhide/enable
|
|
|
|
fi
|
2017-03-26 15:47:54 +02:00
|
|
|
|
2017-02-05 16:40:50 +01:00
|
|
|
if [ -f $DISABLEFILE ]; then
|
2017-03-26 15:47:54 +02:00
|
|
|
# Let MagiskManager know
|
2017-02-05 16:40:50 +01:00
|
|
|
setprop ro.magisk.disable 1
|
|
|
|
exit
|
|
|
|
fi
|
2017-02-07 00:50:55 +01:00
|
|
|
|
2017-03-29 20:29:10 +02:00
|
|
|
module_scripts service
|
2016-09-14 04:31:13 +02:00
|
|
|
;;
|
|
|
|
|
|
|
|
esac
|