Magisk/scripts/custom_ramdisk_patch.sh

75 lines
2.0 KiB
Bash
Raw Normal View History

2016-10-19 06:22:00 +08:00
#!/system/bin/sh
RAMDISK=$1
2017-02-01 06:02:43 +08:00
BINDIR=/data/magisk
[ ! -e $BINDIR ] && BINDIR=/cache/data_bin
[ ! -e $BINDIR ] && exit
2017-01-02 02:00:53 +08:00
SYSTEMLIB=/system/lib
[ -d /system/lib64 ] && SYSTEMLIB=/system/lib64
2016-10-19 06:22:00 +08:00
cpio_add() {
2017-01-02 02:00:53 +08:00
LD_LIBRARY_PATH=$SYSTEMLIB /su/bin/sukernel --cpio-add $RAMDISK $RAMDISK $2 $1 $1
2016-10-19 06:22:00 +08:00
}
cpio_extract() {
2017-01-02 02:00:53 +08:00
LD_LIBRARY_PATH=$SYSTEMLIB /su/bin/sukernel --cpio-extract $RAMDISK $1 $1
2016-10-19 06:22:00 +08:00
}
cpio_mkdir() {
2017-01-02 02:00:53 +08:00
LD_LIBRARY_PATH=$SYSTEMLIB /su/bin/sukernel --cpio-mkdir $RAMDISK $RAMDISK $2 $1
}
# Recursive
cpio_rm() {
if [ "$1" = "-r" ]; then
LD_LIBRARY_PATH=$SYSTEMLIB /su/bin/sukernel --cpio-ls $RAMDISK | grep "^$2/" | while read i ; do
LD_LIBRARY_PATH=$SYSTEMLIB /su/bin/sukernel --cpio-rm $RAMDISK $RAMDISK $i
done
LD_LIBRARY_PATH=$SYSTEMLIB /su/bin/sukernel --cpio-rmdir $RAMDISK $RAMDISK $2
else
LD_LIBRARY_PATH=$SYSTEMLIB /su/bin/sukernel --cpio-rm $RAMDISK $RAMDISK $1
fi
2016-10-19 06:22:00 +08:00
}
file_contain() {
grep "$1" "$2" >/dev/null 2>&1
return $?
}
2016-10-19 06:22:00 +08:00
rm -rf /tmp/magisk/ramdisk 2>/dev/null
mkdir -p /tmp/magisk/ramdisk
cd /tmp/magisk/ramdisk
cat $RAMDISK | cpio -i
2017-01-02 02:00:53 +08:00
# Cleanup SuperSU backups
cpio_rm -r .subackup
2016-10-19 06:22:00 +08:00
# Add magisk entrypoint
for RC in init*.rc; do
if file_contain "import /init.environ.rc" $RC && ! file_contain "import /init.magisk.rc" $RC; then
sed -i "/import \/init\.environ\.rc/iimport /init.magisk.rc" $RC
cpio_add $RC 750
fi
if file_contain "trigger load_persist_props_action" $RC && ! file_contain "trigger load_magisk_props_action" $RC; then
sed -i "/trigger load_persist_props_action/a\ \ \ \ trigger load_magisk_props_action" $RC
cpio_add $RC 750
fi
if file_contain "selinux.reload_policy"; then
sed -i "/selinux.reload_policy/d" $RC
cpio_add $RC 750
2016-10-19 06:22:00 +08:00
fi
done
# sepolicy patches
2017-02-04 18:54:04 +08:00
LD_LIBRARY_PATH=$SYSTEMLIB $BINDIR/sepolicy-inject --load sepolicy --save sepolicy --minimal
2016-10-19 06:22:00 +08:00
cpio_add sepolicy 644
# Add new items
cp -af $BINDIR/init.magisk.rc init.magisk.rc
cp -af $BINDIR/magic_mask.sh sbin/magic_mask.sh
cpio_mkdir magisk 755
cpio_add init.magisk.rc 750
cpio_add sbin/magic_mask.sh 750