-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmagisk-boot-mod.sh
121 lines (103 loc) · 2.74 KB
/
magisk-boot-mod.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#!/system/bin/sh
HOMEDIR=/data/data/com.arlosoft.macrodroid/files/bin
SRCDIR=/storage/emulated/0/init.d
SU_MINISCRIPT='
# Magisk function to find boot partition and prevent the installer from finding
# it again
find_block() {
for BLOCK in "$@"; do
DEVICES=$(find /dev/block -type l -iname $BLOCK) 2>/dev/null
for DEVICE in $DEVICES; do
cd ${DEVICE%/*}
local BASENAME="${DEVICE##*/}"
mv "$BASENAME" ".$BASENAME"
cd -
done
done
# Fallback by parsing sysfs uevents
typeset -l PARTNAME BLOCK
local FILELIST=$(grep -s PARTNAME= /sys/dev/block/*/uevent) 2>/dev/null
for uevent in $FILELIST; do
local PARTNAME=${uevent##*PARTNAME=}
for BLOCK in "$@"; do
if [ "$BLOCK" = "$PARTNAME" ]; then
local FNAME=${uevent%:*}
chmod 0 $FNAME
fi
done
done
return 0
}
# Root only at this point; hoping selinux is permissive
if [ $(id -u) != 0 ] || [ "$(getenforce)" != "Permissive" ]; then
echo "Root user only" >&2
exit 1
fi
# Disaster prevention
SLOT=$(getprop ro.boot.slot_suffix)
find_block boot$SLOT
cd $HOMEDIR || { setenforce 1; exit 1; }
# Patch selinux policy
./magiskpolicy --live --magisk "allow magisk * * *"
if [ ! -f /sbin/.init-stamp ]; then
# Set up /root links to /sbin files
mount | grep -qF rootfs
have_rootfs=$?
if [ $have_rootfs -eq 0 ]; then
mount -o rw,remount /
mkdir -p /root
chmod 750 /root
if ! ln /sbin/* /root; then
echo "Error making /sbin hardlinks" >&2
setenforce 1
exit 1
fi
mount -o ro,remount /
fi
# Create tmpfs /sbin overlay
# This may crash on system-as-root with no /root directory
./magisk -c >&2
touch /sbin/.init-stamp
if [ ! -f /sbin/magiskinit ] || [ ! -f /sbin/magisk ]; then
echo "Bad /sbin mount?" >&2
setenforce 1
exit 1
fi
# Copy binaries
cp magiskinit /sbin/
if [ $have_rootfs -ne 0 ]; then
mkdir /sbin/.magisk/mirror/system_root
block=$(mount | grep " / " | cut -d\ -f1)
[ $block = "/dev/root" ] && block=/dev/block/dm-0
mount -o ro $block /sbin/.magisk/mirror/system_root
for file in /sbin/.magisk/mirror/system_root/sbin/*; do
if [ -L $file ]; then
cp -a $file /sbin/
else
cp -ps $file /sbin/${file##*/}
fi
done
fi
export PATH=/sbin:$PATH
# Finish startup calls
magisk --post-fs-data
sleep 1 # hack to prevent race with later service calls
magisk --service
magisk --boot-complete
fi
setenforce 1
'
mkdir -p $HOMEDIR
cd $HOMEDIR || exit 1
if ! cmp -s $SRCDIR/bin/magiskinit magiskinit; then
cp $SRCDIR/bin/magiskinit ./
chmod 700 magiskinit
ln -fs magiskinit magiskpolicy
ln -fs magiskinit magisk
fi
# strip ':c512...' tail from selinux context
ctx=$(cat /proc/$$/attr/current)
newctx=${ctx/%:s0:*/:s0}
# start SU daemon
export HOMEDIR
echo "$SU_MINISCRIPT" | runcon $newctx sh