-
Notifications
You must be signed in to change notification settings - Fork 0
/
patch_boot.sh
executable file
·176 lines (141 loc) · 5.55 KB
/
patch_boot.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
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
#!/bin/bash
# Argument check
if [ $# == 0 ]; then
echo "Usage: $0 [<path_to_zip_package> ...]"
exit 1
fi
# Root check
if [ "$(whoami)" != root ]; then
echo "Please run this script using sudo."
exit 1
fi
if [ "$1" = "-c" ]; then
variant="canary"
else
variant="magisk"
fi
script_path="$(dirname "$(realpath "${BASH_SOURCE[0]}")")"
#programs=("adb" "brotli" "curl" "dos2unix" "ed" "fastboot" "file" "jq" "python3" "unzip")
programs=()
# Check if all required programs are installed
for program in "${programs[@]}"; do
if ! command -v "$program" &>/dev/null; then
echo "$program is not installed. Please install it and run this script again."
exit 1
fi
done
setup_env() {
[ -z $KEEPVERITY ] && export KEEPVERITY=true
[ -z $KEEPFORCEENCRYPT ] && export KEEPFORCEENCRYPT=true
if grep -q "vbmeta.img" "${script_path}/magisk_files/updater-script"; then
export PATCHVBMETAFLAG=false
else
export PATCHVBMETAFLAG=true
fi
}
patch_scripts() {
local util_script="${script_path}/magisk_files/util_functions.sh"
# Get line number
local line=$(grep -n '/proc/self/fd/$OUTFD' "$util_script" | cut -d: -f1)
# Add echo "$1" and delete the line
(
echo "$line"
echo 'd'
echo "$line-1"
echo a
echo ' echo "$1"'
echo .
echo wq
) | ed "$util_script" > /dev/null 2>&1
# Replace build.prop path
sed -i 's/\/system\/build.prop/.\/build.prop/g' "$util_script"
}
get_magisk_files() {
local temp_dir="$(mktemp -d)"
# Download and extract Magisk
if [ "$variant" = "canary" ]; then
local magisk_url=$(curl -s https://api.github.com/repos/topjohnwu/Magisk/releases | grep 'browser_download_url' | grep 'canary' | grep 'app-release.apk' | head -n 1 | cut -d \" -f 4)
else
local magisk_url=$(curl -s https://api.github.com/repos/topjohnwu/Magisk/releases/latest | grep 'browser_download_url' | cut -d \" -f 4)
fi
echo "Downloading ${variant} from ${magisk_url}..."
wget "$magisk_url" -O "$temp_dir/Magisk.apk" &>/dev/null
if [ $? -ne 0 ]; then
echo "Failed to download Magisk. Please check your internet connection and try again."
rm -rf "$temp_dir"
exit 1
fi
unzip "$temp_dir/Magisk.apk" -d "$temp_dir" &>/dev/null
# Copy needed files
mkdir -p "$script_path/magisk_files/"
cp "$temp_dir/assets/boot_patch.sh" "$script_path/magisk_files/"
cp "$temp_dir/assets/util_functions.sh" "$script_path/magisk_files/"
cp "$temp_dir/assets/stub.apk" "$script_path/magisk_files/"
cp "$temp_dir/lib/x86_64/libmagiskboot.so" "$script_path/magisk_files/magiskboot"
cp "$temp_dir/lib/armeabi-v7a/libmagisk.so" "$script_path/magisk_files/magisk32"
cp "$temp_dir/lib/arm64-v8a/libmagisk.so" "$script_path/magisk_files/magisk64"
cp "$temp_dir/lib/arm64-v8a/libmagiskinit.so" "$script_path/magisk_files/magiskinit"
rm -rf "$temp_dir"
}
get_rom_files() {
local temp_dir="$(mktemp -d)"
# Extract boot.img from ROM zip package
unzip "$1" -d "$temp_dir" &>/dev/null
cp "$temp_dir/boot.img" "$script_path/magisk_files/"
# Extract build.prop
if [[ -f "$temp_dir/system.new.dat.br" ]]; then
brotli --decompress "$temp_dir/system.new.dat.br" -o "$temp_dir/system.new.dat" &>/dev/null
fi
python3 "$script_path/sdat2img/sdat2img.py" "$temp_dir/system.transfer.list" "$temp_dir/system.new.dat" "$temp_dir/system.img" &>/dev/null
if [[ $(file "$temp_dir/system.img") == *EROFS* ]]; then
local temp_mount_dir=$(mktemp -d)
echo "EROFS system detected, mounting system.img to extract build.prop"
mount -t erofs "$temp_dir/system.img" "$temp_mount_dir"
cp "$temp_mount_dir/system/build.prop" "$script_path/magisk_files/build.prop"
umount "$temp_mount_dir"
rm -rf "$temp_mount_dir"
else
debugfs -R "dump system/build.prop $script_path/magisk_files/build.prop" "$temp_dir/system.img" &>/dev/null
fi
# Extract updater-script
cp "$temp_dir/META-INF/com/google/android/updater-script" "$script_path/magisk_files/"
rm -rf "$temp_dir"
}
get_props() {
local REGEX="s/^$1=//p"
local FILE="$script_path/magisk_files/build.prop"
cat $FILE 2>/dev/null | dos2unix | sed -n "$REGEX" | head -n 1
}
move_patched() {
patched_name="$(get_props "ro.build.product")_$(tr -dc A-Za-z0-9 < /dev/urandom | head -c 5).img"
# Move patched boot.img to output folder
mv "$script_path/magisk_files/new-boot.img" "$script_path/out/${variant}_${patched_name}"
mv "$script_path/magisk_files/boot.img" "$script_path/out/stock_$patched_name"
}
patch_boot() {
echo "Getting needed files from ${variant}..."
get_magisk_files
echo "Patching ${variant} util scripts..."
patch_scripts
for zip_package in "$@"; do
if [[ $zip_package == "-k" || "$zip_package" == "-c" ]]; then
continue
fi
echo -e "\nProcessing $zip_package..."
echo "---------------------------------------------"
if [[ "$zip_package" != *.zip ]]; then
echo "$zip_package is not a zip file."
else
echo "Getting needed files from rom zip package..."
get_rom_files "$zip_package"
echo "Patching boot.img..."
setup_env
sh "$script_path/magisk_files/boot_patch.sh" "$script_path/magisk_files/boot.img" &>/dev/null
move_patched
echo "Done! Patched boot.img can be found at out/${variant}_${patched_name}"
fi
done
echo -e "\nCleaning up..."
rm -rf "$script_path/magisk_files/"
}
patch_boot "$@"