-
Notifications
You must be signed in to change notification settings - Fork 97
/
functions.sh
317 lines (292 loc) · 12.4 KB
/
functions.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
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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
#!/bin/bash
# Define color output function
error() {
if [ "$#" -eq 2 ]; then
if [[ "$LANG" == zh_CN* ]]; then
echo -e \[$(date +%m%d-%T)\] "\033[1;31m"$1"\033[0m"
elif [[ "$LANG" == en* ]]; then
echo -e \[$(date +%m%d-%T)\] "\033[1;31m"$2"\033[0m"
else
echo -e \[$(date +%m%d-%T)\] "\033[1;31m"$2"\033[0m"
fi
elif [ "$#" -eq 1 ]; then
echo -e \[$(date +%m%d-%T)\] "\033[1;31m"$1"\033[0m"
else
echo "Usage: error <Chinese> <English>"
fi
}
yellow() {
if [ "$#" -eq 2 ]; then
if [[ "$LANG" == zh_CN* ]]; then
echo -e \[$(date +%m%d-%T)\] "\033[1;33m"$1"\033[0m"
elif [[ "$LANG" == en* ]]; then
echo -e \[$(date +%m%d-%T)\] "\033[1;33m"$2"\033[0m"
else
echo -e \[$(date +%m%d-%T)\] "\033[1;33m"$2"\033[0m"
fi
elif [ "$#" -eq 1 ]; then
echo -e \[$(date +%m%d-%T)\] "\033[1;33m"$1"\033[0m"
else
echo "Usage: yellow <Chinese> <English>"
fi
}
blue() {
if [ "$#" -eq 2 ]; then
if [[ "$LANG" == zh_CN* ]]; then
echo -e \[$(date +%m%d-%T)\] "\033[1;34m"$1"\033[0m"
elif [[ "$LANG" == en* ]]; then
echo -e \[$(date +%m%d-%T)\] "\033[1;34m"$2"\033[0m"
else
echo -e \[$(date +%m%d-%T)\] "\033[1;34m"$2"\033[0m"
fi
elif [ "$#" -eq 1 ]; then
echo -e \[$(date +%m%d-%T)\] "\033[1;34m"$1"\033[0m"
else
echo "Usage: blue <Chinese> <English>"
fi
}
green() {
if [ "$#" -eq 2 ]; then
if [[ "$LANG" == zh_CN* ]]; then
echo -e \[$(date +%m%d-%T)\] "\033[1;32m"$1"\033[0m"
elif [[ "$LANG" == en* ]]; then
echo -e \[$(date +%m%d-%T)\] "\033[1;32m"$2"\033[0m"
else
echo -e \[$(date +%m%d-%T)\] "\033[1;32m"$2"\033[0m"
fi
elif [ "$#" -eq 1 ]; then
echo -e \[$(date +%m%d-%T)\] "\033[1;32m"$1"\033[0m"
else
echo "Usage: green <Chinese> <English>"
fi
}
#Check for the existence of the requirements command, proceed if it exists, or abort otherwise.
exists() {
command -v "$1" > /dev/null 2>&1
}
abort() {
error "--> Missing $1 abort! please run ./setup.sh first (sudo is required on Linux system)"
error "--> 命令 $1 缺失!请重新运行setup.sh (Linux系统sudo ./setup.sh)"
exit 1
}
check() {
for b in "$@"; do
exists "$b" || abort "$b"
done
}
# Replace Smali code in an APK or JAR file, without supporting resource patches.
# $1: Target APK/JAR file
# $2: Target Smali file (supports relative paths for Smali files)
# $3: Value to be replaced
# $4: Replacement value
patch_smali() {
if [[ $is_eu_rom == "true" ]]; then
SMALI_COMMAND="java -jar bin/apktool/smali-3.0.5.jar"
BAKSMALI_COMMAND="java -jar bin/apktool/baksmali-3.0.5.jar"
else
SMALI_COMMAND="java -jar bin/apktool/smali.jar"
BAKSMALI_COMMAND="java -jar bin/apktool/baksmali.jar"
fi
targetfilefullpath=$(find build/portrom/images -type f -name $1)
if [ -f $targetfilefullpath ];then
targetfilename=$(basename $targetfilefullpath)
yellow "正在修改 $targetfilename" "Modifying $targetfilename"
foldername=${targetfilename%.*}
rm -rf tmp/$foldername/
mkdir -p tmp/$foldername/
cp -rf $targetfilefullpath tmp/$foldername/
7z x -y tmp/$foldername/$targetfilename *.dex -otmp/$foldername >/dev/null
for dexfile in tmp/$foldername/*.dex;do
smalifname=${dexfile%.*}
smalifname=$(echo $smalifname | cut -d "/" -f 3)
${BAKSMALI_COMMAND} d --api ${port_android_sdk} ${dexfile} -o tmp/$foldername/$smalifname 2>&1 || error " Baksmaling 失败" "Baksmaling failed"
done
if [[ $2 == *"/"* ]];then
targetsmali=$(find tmp/$foldername/*/$(dirname $2) -type f -name $(basename $2))
else
targetsmali=$(find tmp/$foldername -type f -name $2)
fi
if [ -f $targetsmali ];then
smalidir=$(echo $targetsmali |cut -d "/" -f 3)
yellow "I: 开始patch目标 ${smalidir}" "Target ${smalidir} Found"
search_pattern=$3
repalcement_pattern=$4
if [[ $5 == 'regex' ]];then
sed -i "/${search_pattern}/c\\${repalcement_pattern}" $targetsmali
else
sed -i "s/$search_pattern/$repalcement_pattern/g" $targetsmali
fi
${SMALI_COMMAND} a --api ${port_android_sdk} tmp/$foldername/${smalidir} -o tmp/$foldername/${smalidir}.dex > /dev/null 2>&1 || error " Smaling 失败" "Smaling failed"
pushd tmp/$foldername/ >/dev/null || exit
7z a -y -mx0 -tzip $targetfilename ${smalidir}.dex > /dev/null 2>&1 || error "修改$targetfilename失败" "Failed to modify $targetfilename"
popd >/dev/null || exit
yellow "修补$targetfilename 完成" "Fix $targetfilename completed"
if [[ $targetfilename == *.apk ]]; then
yellow "检测到apk,进行zipalign处理。。" "APK file detected, initiating ZipAlign process..."
rm -rf ${targetfilefullpath}
# Align moddified APKs, to avoid error "Targeting R+ (version 30 and above) requires the resources.arsc of installed APKs to be stored uncompressed and aligned on a 4-byte boundary"
zipalign -p -f -v 4 tmp/$foldername/$targetfilename ${targetfilefullpath} > /dev/null 2>&1 || error "zipalign错误,请检查原因。" "zipalign error,please check for any issues"
yellow "apk zipalign处理完成" "APK ZipAlign process completed."
yellow "复制APK到目标位置:${targetfilefullpath}" "Copying APK to target ${targetfilefullpath}"
else
yellow "复制修改文件到目标位置:${targetfilefullpath}" "Copying file to target ${targetfilefullpath}"
cp -rf tmp/$foldername/$targetfilename ${targetfilefullpath}
fi
fi
else
error "Failed to find $1,please check it manually".
fi
}
unlock_device_feature() {
feature_type=$2
feature_name=$3
feature_value=$4
if [[ ! -z "$1" ]]; then
comment=$1
else
comment="Whether enable $feature feature"
fi
if [[ $feature_type == "bool" ]] && [[ $feature_value == "" ]];then
feature_value="true"
fi
if ! grep -q "$feature_name" build/portrom/images/product/etc/device_features/${base_rom_code}.xml;then
sed -i "/<features>/a\\\t<!-- ${comment} -->\n\t<${feature_type} name=\"${feature_name}\">${feature_value}</${feature_type}> " build/portrom/images/product/etc/device_features/${base_rom_code}.xml
else
sed -i "s/<${feature_type} name=\"${feature_name}\">.*<\/${feature_type}>/<${feature_type} name=\"$feature_name\">${feature_value}<\/${feature_type}>/" build/portrom/images/product/etc/device_features/${base_rom_code}.xml
fi
}
#check if a prperty is avaialble
is_property_exists () {
if [ $(grep -c "$1" "$2") -ne 0 ]; then
return 0
else
return 1
fi
}
# Function to update netlink in build.prop
update_netlink() {
local netlink_version=$1
local prop_file=$2
if grep -q "ro.millet.netlink" "$prop_file"; then
blue "找到ro.millet.netlink修改值为$netlink_version" "millet_netlink propery found, changing value to $netlink_version"
sed -i "s/ro.millet.netlink=.*/ro.millet.netlink=$netlink_version/" "$prop_file"
else
blue "PORTROM未找到ro.millet.netlink值,添加为$netlink_version" "millet_netlink not found in portrom, adding new value $netlink_version"
echo -e "ro.millet.netlink=$netlink_version\n" >> "$prop_file"
fi
}
disable_avb_verify() {
fstab=$1
blue "Disabling avb_verify: $fstab"
if [[ ! -f $fstab ]]; then
yellow "$fstab not found, please check it manually"
else
sed -i "s/,avb_keys=.*avbpubkey//g" $fstab
sed -i "s/,avb=vbmeta_system//g" $fstab
sed -i "s/,avb=vbmeta_vendor//g" $fstab
sed -i "s/,avb=vbmeta//g" $fstab
sed -i "s/,avb//g" $fstab
fi
}
extract_partition() {
part_img=$1
part_name=$(basename ${part_img})
target_dir=$2
if [[ -f ${part_img} ]];then
if [[ $($tools_dir/gettype -i ${part_img} ) == "ext" ]];then
blue "[ext] 正在分解${part_name}" "[ext] Extracing ${part_name} "
sudo python3 bin/imgextractor/imgextractor.py ${part_img} ${target_dir} >/dev/null 2>&1 || { error "分解 ${part_name} 失败" "Extracting ${part_name} failed."; exit 1; }
green "[ext]分解[${part_name}] 完成" "[ext] ${part_name} extracted."
rm -rf ${part_img}
elif [[ $($tools_dir/gettype -i ${part_img}) == "erofs" ]]; then
blue "[erofs] 正在分解${part_name} " "[erofs] Extracing ${part_name} "
extract.erofs -x -i ${part_img} -o $target_dir > /dev/null 2>&1 || { error "分解 ${part_name} 失败" "Extracting ${part_name} failed." ; exit 1; }
green "[erofs] 分解[${part_name}] 完成" "[erofs] ${part_name} extracted."
rm -rf ${part_img}
else
error "无法识别img文件类型,请检查" "Unable to handle img, exit."
exit 1
fi
fi
}
disable_avb_verify() {
fstab=$(find $1 -name "fstab*")
if [[ $fstab == "" ]];then
error "未找到 fstab 文件!" "No fstab found!"
sleep 5
else
blue "禁用 AVB 验证中..." "Disabling AVB verfication...."
for file in $fstab; do
sed -i 's/,avb.*system//g' $file
sed -i 's/,avb,/,/g' $file
sed -i 's/,avb=.*a,/,/g' $file
sed -i 's/,avb_keys.*key//g' $file
if [[ "${pack_type}" == "EXT" ]];then
sed -i "/erofs/d" $file
fi
done
blue "AVB 验证禁用完成" "AVB verification disabled successfully"
fi
}
patch_kernel_to_bootimg() {
kernel_file=$1
dtb_file=$2
bootimg_name=$3
mkdir -p ${work_dir}/tmp/boot
cd ${work_dir}/tmp/boot
bootimg=$(find ${work_dir}/build/baserom -name "boot.img")
cp $bootimg ${work_dir}/tmp/boot/boot.img
magiskboot unpack -h ${work_dir}/tmp/boot/boot.img > /dev/null 2>&1
if [ -f ramdisk.cpio ]; then
comp=$(magiskboot decompress ramdisk.cpio | grep -v 'raw' | sed -n 's;.*\[\(.*\)\];\1;p')
if [ "$comp" ]; then
mv -f ramdisk.cpio ramdisk.cpio.$comp
magiskboot decompress ramdisk.cpio.$comp ramdisk.cpio > /dev/null 2>&1
if [ $? != 0 ] && $comp --help; then
$comp -dc ramdisk.cpio.$comp >ramdisk.cpio
fi
fi
mkdir -p ramdisk
chmod 755 ramdisk
cd ramdisk
EXTRACT_UNSAFE_SYMLINKS=1 cpio -d -F ../ramdisk.cpio -i
disable_avb_verify ${work_dir}/tmp/boot/
#添加erofs文件系统fstab
if [[ ${pack_type} == "EROFS" ]];then
blue "检查 ramdisk fstab.qcom是否需要添加erofs挂载点" "Check if ramdisk fstab.qcom needs to add erofs mount point."
if ! grep -q "erofs" ${work_dir}/tmp/boot/ramdisk/fstab.qcom ; then
for pname in ${super_list}; do
sed -i "/\/${pname}[[:space:]]\+ext4/{p;s/ext4/erofs/;s/ro,barrier=1,discard/ro/;}" ${work_dir}/tmp/boot/ramdisk/fstab.qcom
added_line=$(sed -n "/\/${pname}[[:space:]]\+erofs/p" ${work_dir}/tmp/boot/ramdisk/fstab.qcom)
if [ -n "$added_line" ]; then
yellow "添加${pname}成功" "Adding erofs mount point [$pname]"
else
error "添加失败,请检查" "Adding faild, please check."
exit 1
fi
done
fi
fi
fi
sudo cp -f $kernel_file ${work_dir}/tmp/boot/kernel
sudo cp -f $dtb_file ${work_dir}/tmp/boot/dtb
cd ${work_dir}/tmp/boot/ramdisk/
find | sed 1d | cpio -H newc -R 0:0 -o -F ../ramdisk_new.cpio > /dev/null 2>&1
cd ..
if [ "$comp" ]; then
magiskboot compress=$comp ramdisk_new.cpio
if [ $? != 0 ] && $comp --help > /dev/null 2>&1; then
$comp -9c ramdisk_new.cpio >ramdisk.cpio.$comp
fi
fi
ramdisk=$(ls ramdisk_new.cpio* | tail -n1)
if [ "$ramdisk" ]; then
cp -f $ramdisk ramdisk.cpio
case $comp in
cpio) nocompflag="-n" ;;
esac
magiskboot repack $nocompflag ${work_dir}/tmp/boot/boot.img ${work_dir}/devices/$base_rom_code/${bootimg_name} >/dev/null 2>&1
fi
rm -rf ${work_dir}/tmp/boot
cd $work_dir
}