forked from likeadragonmaid/Magisk_BootloopSaver
-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathcustomize.sh
More file actions
282 lines (257 loc) · 9.81 KB
/
customize.sh
File metadata and controls
282 lines (257 loc) · 9.81 KB
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
MAGISKTMP="$(magisk --path)"
get_flags
find_boot_image
exit_ui_print(){
ui_print "$1"
exit
}
test -z /data/adb/magisk/magiskboot && abort "- Hmmm... Where is magiskboot binary?"
check_ramdisk(){
ui_print "- Checking ramdisk status"
if [ -e ramdisk.cpio ]; then
/data/adb/magisk/magiskboot cpio ramdisk.cpio test
STATUS=$?
else
# Stock A only legacy SAR, or some Android 13 GKIs
STATUS=0
fi
case $((STATUS & 3)) in
0 ) # Stock boot
ui_print "- Stock boot image detected"
;;
1 ) # Magisk patched
ui_print "- Magisk patched boot image detected"
;;
2 ) # Unsupported
ui_print "! Boot image patched by unsupported programs"
exit_ui_print "! Please restore back to stock boot image"
;;
esac
}
uninstall_module(){
( if [ ! -z "$BOOTIMAGE" ]; then
ui_print "- Target boot image: $BOOTIMAGE"
rm -rf "$TMPDIR/boot"
mkdir -p "$TMPDIR/boot"
if [ -c "$BOOTIMAGE" ]; then
nanddump -f "$TMPDIR/boot/boot.img" "$BOOTIMAGE" || exit_ui_print "! Unable to dump boot image"
BOOTNAND="$BOOTIMAGE"
BOOTIMAGE="$TMPDIR/boot/boot.img"
else
dd if="$BOOTIMAGE" of="$TMPDIR/boot/boot.img" || exit_ui_print "! Unable to dump boot image"
fi
cd "$TMPDIR/boot" || exit 1
check_ramdisk
ui_print "- Revert patch from boot image"
/data/adb/magisk/magiskboot unpack boot.img
/data/adb/magisk/magiskboot cpio ramdisk.cpio \
"rm overlay.d/safemode.rc" \
"rm -r overlay.d/sbin/bootloopsaver" \
"rm overlay.d/sbin/safemode.sh"
/data/adb/magisk/magiskboot repack boot.img || abort "! Unable to repack boot image"
ui_print "- Flashing new boot image"
flash_image "$TMPDIR/boot/new-boot.img" "$BOOTIMAGE"
case $? in
1)
abort "! Insufficient partition size"
;;
2)
FILENAME="/sdcard/Download/bootloopsaver-patched-boot-$RANDOM.img"
cp "$TMPDIR/boot/new-boot.img" "$FILENAME"
ui_print "! $BOOTIMAGE is read-only"
ui_print "*****************************************"
ui_print " Oops! It seems your boot partition is read-only"
ui_print " I have saved your boot image to $FILENAME"
ui_print " Please try flashing this boot image from fastboot or recovery"
ui_print "*****************************************"
exit_ui_print "! Unable to flash boot image"
;;
esac
ui_print "- Module is completely remove from boot image!"
fi )
}
patch_bootimg(){
( if [ ! -z "$BOOTIMAGE" ]; then
ui_print "- Target boot image: $BOOTIMAGE"
[ "$RECOVERYMODE" == "true" ] && ui_print "- Recovery mode is present, the script might patch recovery image..."
mkdir "$TMPDIR/boot"
if [ -c "$BOOTIMAGE" ]; then
nanddump -f "$TMPDIR/boot/boot.img" "$BOOTIMAGE" || exit_ui_print "! Unable to dump boot image"
BOOTNAND="$BOOTIMAGE"
BOOTIMAGE="$TMPDIR/boot/boot.img"
else
dd if="$BOOTIMAGE" of="$TMPDIR/boot/boot.img" || exit_ui_print "! Unable to dump boot image"
fi
ui_print "- Unpack boot image"
cd "$TMPDIR/boot" || exit 1
/data/adb/magisk/magiskboot unpack boot.img
check_ramdisk
ui_print "- Add bootloop protector script"
cat <<EOF > safemode.rc
# safe mode trigger
on post-fs
exec u:r:magisk:s0 root root -- /system/bin/sh \${MAGISKTMP}/safemode.sh
on post-fs-data
exec u:r:magisk:s0 root root -- /system/bin/sh \${MAGISKTMP}/safemode.sh --post-fs-data
# if boot completed, remove file to tell script that previous boot is successful
on property:sys.boot_completed=1
rm /cache/.system_booting
rm /data/unencrypted/.system_booting
rm /metadata/.system_booting
rm /persist/.system_booting
rm /mnt/vendor/persist/.system_booting
EOF
cat <<EOF >safemode.sh
MAGISKTMP="\${0%/*}"
post_fs_dir(){
unset POSTFSDIR
if [ -d /data/unencrypted ] && ! grep ' /data ' /proc/mounts | grep -qE 'dm-|f2fs'; then
POSTFSDIR="/data/unencrypted/${MODPATH##*/}"
elif grep ' /cache ' /proc/mounts | grep -q 'ext4' ; then
POSTFSDIR="/cache/${MODPATH##*/}"
elif grep ' /metadata ' /proc/mounts | grep -q 'ext4' ; then
POSTFSDIR="/metadata/${MODPATH##*/}"
elif grep ' /persist ' /proc/mounts | grep -q 'ext4' ; then
POSTFSDIR="/persist/${MODPATH##*/}"
elif grep ' /mnt/vendor/persist ' /proc/mounts | grep -q 'ext4' ; then
POSTFSDIR="/mnt/vendor/persist/${MODPATH##*/}"
elif [ ! -z "\$MAGISKTMP" ]; then
POSTFSDIR="\$MAGISKTMP/.magisk/${MODPATH##*/}"
else
POSTFSDIR="/dev/${MODPATH##*/}"
fi
[ ! -z "\$POSTFSDIR" ] && mkdir -p "\$POSTFSDIR/magisk"
}
post_fs_dir
if [ "\$1" == "--post-fs-data" ]; then
cp -af /data/adb/magisk/magiskboot "\$POSTFSDIR/magisk/magiskboot"
cp -af /data/adb/magisk/util_functions.sh "\$POSTFSDIR/magisk/util_functions.sh"
mkdir -p "\$MAGISKTMP/.magisk/${MODPATH##*/}"
# do not disable bootloop protector
rm -rf "/data/adb/modules/${MODPATH##*/}/disable"
# always sync module
if [ -e "\$POSTFSDIR/${MODPATH##*/}/remove" ]; then
touch "/data/adb/modules/${MODPATH##*/}/remove"
else
mkdir -p "/data/adb/modules/${MODPATH##*/}"
cp -af "\$MAGISKTMP/bootloopsaver/"* "/data/adb/modules/${MODPATH##*/}"
cp -af "\$MAGISKTMP/bootloopsaver/"* "\$POSTFSDIR"
fi
else
for dir in /cache /data/unencrypted /metadata /mnt/vendor/persist; do
if [ -e "\$dir/disable_magisk" ]; then
DISABLE=true
rm -rf "\$dir/disable_magisk"
fi
# reboot while in boot animation will boot into safe mode
# this feature is only enabled when new_safemode is created
if [ -e "\$dir/new_safemode" ] && [ -e "\$dir/.system_booting" ]; then
# if we found ".system_booting" that's mean previous boot is not completed
DISABLE=true
rm -rf "\$dir/.system_booting"
fi
# tell that system is booting
touch "\$dir/.system_booting"
done
if [ ! -f "\$POSTFSDIR/count" ]; then
rm -rf "\$POSTFSDIR/count"
echo "0" >"\$POSTFSDIR/count"
fi
if [ "\$DISABLE" == "true" ]; then
echo "\$(( \$(cat "\$POSTFSDIR/count" | head -c4) + 1 ))" >"\$POSTFSDIR/count"
setprop persist.sys.safemode 1
[ ! -z "\$POSTFSDIR" ] && echo "I have triggered Safe Mode for \$(cat "\$POSTFSDIR/count" | head -c4) time(s)" >"\$POSTFSDIR/note.txt"
fi
fi
EOF
/data/adb/magisk/magiskboot cpio ramdisk.cpio \
"mkdir 0750 overlay.d" \
"mkdir 0750 overlay.d/sbin" \
"rm -r overlay.d/sbin/bootloopsaver" \
"mkdir 0750 overlay.d/sbin/bootloopsaver" \
"rm overlay.d/safemode.rc" \
"rm overlay.d/sbin/safemode.sh" \
"add 0750 overlay.d/safemode.rc safemode.rc" \
"add 0750 overlay.d/sbin/bootloopsaver/post-fs-data.sh $MODPATH/post-fs-data.sh" \
"add 0750 overlay.d/sbin/bootloopsaver/service.sh $MODPATH/service.sh" \
"add 0750 overlay.d/sbin/bootloopsaver/utils.sh $MODPATH/utils.sh" \
"add 0750 overlay.d/sbin/bootloopsaver/module.prop $MODPATH/module.prop" \
"add 0750 overlay.d/sbin/safemode.sh safemode.sh"
ui_print "- Repack boot image"
/data/adb/magisk/magiskboot repack boot.img || exit_ui_print "! Unable to repack boot image"
[ -e "$BOOTNAND" ] && BOOTIMAGE="$BOOTNAND"
ui_print "- Flashing new boot image"
flash_image "$TMPDIR/boot/new-boot.img" "$BOOTIMAGE"
case $? in
1)
exit_ui_print "! Insufficient partition size"
;;
2)
FILENAME="/sdcard/Download/bootloopsaver-patched-boot-$RANDOM.img"
cp "$TMPDIR/boot/new-boot.img" "$FILENAME"
ui_print "! $BOOTIMAGE is read-only"
ui_print "*****************************************"
ui_print " Oops! It seems your boot partition is read-only"
ui_print " I have saved your boot image to $FILENAME"
ui_print " Please try flashing this boot image from fastboot or recovery"
ui_print "*****************************************"
exit_ui_print "! Unable to flash boot image"
;;
esac
ui_print "- All done!"
ui_print "*****************************************"
ui_print " Remember to reinstall module"
ui_print " when you flash custom kernel/boot image"
ui_print "*****************************************"
else
ui_print "! Cannot detect target boot image"
fi ) }
. $MODPATH/addon/Volume-Key-Selector/install.sh
ui_print "- Do you want to patch boot image with this module?"
ui_print " 1. DON'T patch my boot image!"
ui_print " Basic function only: Auto mode and Disable from Recovery"
ui_print " 2. Yes, patch my boot image!"
ui_print " Basic function + New way to reboot to Safe Mode"
ui_print " Survive after wipe data or factory reset"
ui_print " 3. Completely remove this module"
ui_print ""
ui_print "Volume up (+) to change selection"
ui_print "Volume down (-) to decide"
sleep 0.5
A=1
while true; do
case $A in
1 ) TEXT="Don't patch my boot image";;
2 ) TEXT="Yes, patch my boot image!";;
3 ) TEXT="Completely remove this module";;
esac
ui_print "$A - $TEXT"
if $VKSEL; then
A=$((A + 1))
else
break
fi
if [ $A -gt 3 ]; then
A=1
fi
done
ui_print ""
ui_print "You have selected: $A - $TEXT"
case $A in
2 ) patch_bootimg;;
3 ) uninstall_module; touch "$MODPATH/remove"; ui_print "- Module will be removed in next boot";;
esac
sleep 1
if [ "$A" != 3 ]; then
( . "$MODPATH/utils.sh"
MODULEDIR="$MODPATH"
post_fs_dir
ui_print "- Module directory is $POSTFSDIR"
rm -rf "$POSTFSDIR/remove"
)
if [ ! -z "$MAGISKTMP" ]; then
cat "$MODPATH/module.prop" >"$MAGISKTMP/saver.prop"
sed -Ei "s/^description=(\[.*][[:space:]]*)?/description=[ ✔ Module has been updated ] /g" "$MAGISKTMP/saver.prop"
fi
fi
rm -rf "$MODPATH/addon"