-
Notifications
You must be signed in to change notification settings - Fork 16
/
reinstall-magisk-on-lineageos
executable file
·130 lines (98 loc) · 3.59 KB
/
reinstall-magisk-on-lineageos
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
#! /bin/bash
set -e
PHONE_ADB_SERIAL_NUMBER=paste_yours_here # The adb serial number can be found by running `adb devices`.
print_message() {
printf "$1\n"
}
check_phone_is_connected() {
if ! adb devices | grep --silent "$PHONE_ADB_SERIAL_NUMBER"; then
print_message "ERROR: phone '$PHONE_ADB_SERIAL_NUMBER' is not connected"
adb devices -l
exit 1
fi
print_message "Operations will be performed on:"
adb devices -l | grep $PHONE_ADB_SERIAL_NUMBER
}
check_phone_is_in_fastboot_mode() {
fastboot devices | grep --silent "$PHONE_ADB_SERIAL_NUMBER"
}
get_lineage_version() {
adb -s "$PHONE_ADB_SERIAL_NUMBER" shell getprop ro.lineage.version | tr -d '\n'
}
get_device_name() {
adb -s "$PHONE_ADB_SERIAL_NUMBER" shell getprop ro.lineage.device | tr -d '\n'
}
get_device_downloads_json() {
echo "https://download.lineageos.org/api/v2/devices/$(get_device_name)/builds"
}
get_boot_img_url() {
curl --no-progress-meter "$(get_device_downloads_json)" \
| jq -r 'max_by(.date) | .files[] | select(.filename == "boot.img") | .url'
}
download_boot_img() {
# --location tells curl to follow the redirection.
curl --location "$(get_boot_img_url)" --output /tmp/boot.img
}
transfer_unpatched_boot_image_to_phone() {
adb -s "$PHONE_ADB_SERIAL_NUMBER" push /tmp/boot.img /sdcard/Download/boot.img
}
patch_boot_image_on_phone() {
adb -s "$PHONE_ADB_SERIAL_NUMBER" shell /data/adb/magisk/boot_patch.sh /sdcard/Download/boot.img
adb -s "$PHONE_ADB_SERIAL_NUMBER" shell mv /data/adb/magisk/new-boot.img /sdcard/Download/patched-boot.img
}
transfer_patched_boot_image_to_pc() {
adb -s "$PHONE_ADB_SERIAL_NUMBER" pull /sdcard/Download/patched-boot.img /tmp
}
reboot_to_bootloader() {
adb -s "$PHONE_ADB_SERIAL_NUMBER" reboot bootloader
}
wait_for_phone_to_be_in_fastboot() {
SECONDS=0
until check_phone_is_in_fastboot_mode; do
if ((SECONDS > 60)); then
echo "Giving up..."
exit 2
fi
echo "Phone is not in fastboot mode yet. Waiting..."
sleep 5
done
}
flash_patched_boot_image() {
fastboot -s "$PHONE_ADB_SERIAL_NUMBER" flash boot /tmp/patched-boot.img
}
reboot_phone() {
fastboot -s "$PHONE_ADB_SERIAL_NUMBER" reboot
}
check_magisk_folder() {
adb -s "$PHONE_ADB_SERIAL_NUMBER" shell ls /data/adb/magisk/
}
is_rooted_debugging_enabled() {
adb -s "$PHONE_ADB_SERIAL_NUMBER" root
}
main() {
print_message "Looking for phone '$PHONE_ADB_SERIAL_NUMBER'"
check_phone_is_connected
print_message "Checking on phone if rooted debugging is enabled"
is_rooted_debugging_enabled
print_message "Checking on phone if Magisk folder is present listing /data/adb/magisk/"
check_magisk_folder
print_message "Downloading boot.img from $(get_boot_img_url)"
download_boot_img
print_message "Copying from PC to phone the boot image in /sdcard/Download/boot.img"
transfer_unpatched_boot_image_to_phone
print_message "Patching boot image with Magisk script and moving it in /sdcard/Download/patched-boot.img"
patch_boot_image_on_phone
print_message "Copying patched boot image from phone to PC in /tmp/patched-boot.img"
transfer_patched_boot_image_to_pc
print_message "Rebooting phone in fastboot mode"
reboot_to_bootloader
wait_for_phone_to_be_in_fastboot
print_message "Flashing patched boot image on phone"
flash_patched_boot_image
print_message "Rebooting phone"
reboot_phone
}
# Run the main only when this file is executed as script, to help with testing.
if [[ ${BASH_SOURCE[0]} == "${0}" ]]; then
main "$@"
fi