This repository has been archived by the owner on Jul 5, 2021. It is now read-only.
forked from linusyang92/mipay-extract
-
Notifications
You must be signed in to change notification settings - Fork 3
/
cleaner-fix.sh
executable file
·353 lines (321 loc) · 10.3 KB
/
cleaner-fix.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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
#!/bin/bash
cd "$(dirname "$0")"
darr=()
while [[ $# -gt 0 ]]; do
key="$1"
case $key in
--trafficfix)
EXTRA_PRIV="framework/services.jar $EXTRA_PRIV"
echo "--> Increase threshold (50M) to prevent high cpu of traffic monitoring"
shift
;;
--nofbe)
NO_EXTRA_FBE="yes"
shift
;;
*)
darr+=("$1")
shift
;;
esac
done
mipay_apps="Calendar SecurityCenter Weather"
EXTRA_PRIV="app/DeskClock $EXTRA_PRIV"
private_apps=""
[ -z "$EXTRA_PRIV" ] || private_apps="$private_apps $EXTRA_PRIV"
base_dir=$PWD
tool_dir=$base_dir/tools
sdat2img="python2.7 $tool_dir/sdat2img.py"
patchmethod="python2.7 $tool_dir/patchmethod.py"
heapsize=1024
smali="java -Xmx${heapsize}m -jar $tool_dir/smali-2.2.5.jar"
baksmali="java -Xmx${heapsize}m -jar $tool_dir/baksmali-2.2.5.jar"
keypass="--ks-pass pass:testkey --key-pass pass:testkey"
sign="java -Xmx${heapsize}m -jar $tool_dir/apksigner.jar sign \
--ks $tool_dir/testkey.jks $keypass"
aria2c_opts="--check-certificate=false --file-allocation=trunc -s10 -x10 -j10 -c"
aria2c="aria2c $aria2c_opts"
sed="sed"
exists() {
command -v "$1" >/dev/null 2>&1
}
abort() {
echo "--> $1"
echo "--> abort"
exit 1
}
check() {
for b in $@; do
exists $b || abort "Missing $b"
done
}
check java python2.7
if [[ "$OSTYPE" == "darwin"* ]]; then
aapt="$tool_dir/darwin/aapt"
zipalign="$tool_dir/darwin/zipalign"
sevenzip="$tool_dir/darwin/7za"
aria2c="$tool_dir/darwin/aria2c $aria2c_opts"
sed="$tool_dir/darwin/gsed"
brotli="$tool_dir/darwin/brotli"
else
exists aapt && aapt="aapt" || aapt="$tool_dir/aapt"
exists zipalign && zipalign="zipalign" || zipalign="$tool_dir/zipalign"
exists 7z && sevenzip="7z" || sevenzip="$tool_dir/7za"
exists aria2c || aria2c="$tool_dir/aria2c $aria2c_opts"
exists brotli && brotli="brotli" || brotli="$tool_dir/brotli"
if [[ "$OSTYPE" == "cygwin"* ]]; then
sdat2img="python2.7 ../tools/sdat2img.py"
patchmethod="python2.7 ../../tools/patchmethod.py"
smali="java -Xmx${heapsize}m -jar ../../tools/smali-2.2.5.jar"
baksmali="java -Xmx${heapsize}m -jar ../../tools/baksmali-2.2.5.jar"
sign="java -Xmx${heapsize}m -jar ../../tools/apksigner.jar sign \
--ks ../../tools/testkey.jks $keypass"
fi
fi
clean() {
[ -e "$1" ] && rm -R "$1"
echo "--> abort"
echo "--> clean $(basename $1)"
exit 1
}
pushd() {
command pushd "$@" > /dev/null
}
popd() {
command popd "$@" > /dev/null
}
update_international_build_flag() {
path=$1
pattern="Lmiui/os/Build;->IS_INTERNATIONAL_BUILD"
if [ -d $path ]; then
found=()
if [[ "$OSTYPE" == "cygwin"* ]]; then
pushd "$path"
cmdret="$(findstr /sm /c:${pattern} '*.*' | tr -d '\015')"
popd
result="${cmdret//\\//}"
while read i; do
found+=("${path}/$i")
done <<< "$result"
else
files="$(find $path -type f -iname "*.smali")"
while read i; do
if grep -q -F "$pattern" $i; then
found+=("$i")
fi
done <<< "$files"
fi
fi
if [ -f $path ]; then
found=($path)
fi
for i in "${found[@]}"; do
$sed -i 's|sget-boolean \([a-z]\)\([0-9]\+\), Lmiui/os/Build;->IS_INTERNATIONAL_BUILD:Z|const/4 \1\2, 0x0|g' "$i" \
|| return 1
if grep -q -F "$pattern" $i; then
echo "----> ! failed to patch: $(basename $i)"
else
echo "----> patched smali: $(basename $i)"
fi
done
}
deodex() {
app=$2
base_dir="$1"
arch=$3
deoappdir=system/$4
deoarch=oat/$arch
framedir=system/framework
pushd "$base_dir"
api=$(grep "ro.build.version.sdk" system/build.prop | cut -d"=" -f2)
if [ -z "$api" ]; then
api=25
fi
apkdir=$deoappdir/$app
apkfile=$apkdir/$app.apk
if [[ "$app" == *".jar" ]]; then
apkdir=$deoappdir
apkfile=$apkdir/$app
fi
file_list="$($sevenzip l "$apkfile")"
if [[ "$file_list" == *"classes.dex"* ]]; then
echo "--> decompiling $app..."
dexclass="classes.dex"
$baksmali d $apkfile -o $apkdir/smali || return 1
if [[ "$app" == "Calendar" ]]; then
$patchmethod $apkdir/smali/com/miui/calendar/util/LocalizationUtils.smali \
showsDayDiff showsLunarDate showsWidgetHoliday -showsWorkFreeDay \
-isMainlandChina -isGreaterChina || return 1
fi
if [[ "$app" == "Weather" ]]; then
echo "----> searching smali..."
update_international_build_flag "$apkdir/smali/com/miui/weather2"
i="$apkdir/smali/com/miui/weather2/tools/ToolUtils.smali"
if [ -f "$i" ]; then
$patchmethod "$i" -canRequestCommercial -canRequestCommercialInfo || return 1
fi
fi
if [[ "$app" == "SecurityCenter" ]]; then
update_international_build_flag "$apkdir/smali/com/miui/antivirus/activity/SettingsActivity.smali"
fi
if [[ "$app" == "DeskClock" ]]; then
echo "----> searching smali..."
update_international_build_flag "$apkdir/smali/"
fi
if [[ "$app" == "services.jar" ]]; then
i="$apkdir/smali/com/android/server/net/NetworkStatsService.smali"
$sed -i 's|, 0x200000$|, 0x5000000|g' "$i" || return 1
$sed -i 's|, 0x20000$|, 0x1000000|g' "$i" || return 1
if grep -q -F ', 0x20000' $i; then
echo "----> ! failed to patch: $(basename $i)"
else
echo "----> patched smali: $(basename $i)"
fi
fi
$smali assemble -a $api $apkdir/smali -o $apkdir/$dexclass || return 1
rm -rf $apkdir/smali
if [[ ! -f $apkdir/$dexclass ]]; then
echo "----> failed to baksmali: $apkdir/$dexclass"
continue
fi
$sevenzip d "$apkfile" $dexclass >/dev/null
pushd $apkdir
adderror=false
$aapt add -fk "$(basename $apkfile)" classes.dex || adderror=true
popd
if $adderror; then
return 1
fi
rm -f $apkdir/classes.dex
$zipalign -f 4 $apkfile $apkfile-2 >/dev/null 2>&1
mv $apkfile-2 $apkfile
if [[ "$deoappdir" == "system/data-app" ]]; then
if $sign $apkfile; then
echo "----> signed: $app.apk"
else
echo "----> cannot sign $app.apk"
return 1
fi
fi
if ! [ -d $apkdir/lib ]; then
$sevenzip x -o$apkdir $apkfile lib >/dev/null
if [ -d $apkdir/lib ]; then
pushd $apkdir/lib
[ -d armeabi-v7a ] && mv armeabi-v7a arm
[ -d arm64-v8a ] && mv arm64-v8a arm64
popd
fi
fi
fi
popd
return 0
}
extract() {
model=$1
ver=$2
file=$3
apps=$4
priv_apps=$5
dir=miuieu-$model-$ver
img=$dir-system.img
echo "--> rom: $model v$ver"
[ -d $dir ] || mkdir $dir
pushd $dir
if ! [ -f $img ]; then
trap "clean \"$PWD/system.new.dat\"" INT
if ! [ -f system.new.dat ]; then
filelist="$($sevenzip l ../"$file")"
if [[ "$filelist" == *system.new.dat.br* ]]; then
$sevenzip x ../$file "system.new.dat.br" "system.transfer.list" \
|| clean system.new.dat.br
$brotli -d system.new.dat.br && rm -f system.new.dat.br
else
$sevenzip x ../$file "system.new.dat" "system.transfer.list" \
|| clean system.new.dat
fi
fi
fi
trap "clean \"$PWD/$img\"" INT
if ! [ -f $img ]; then
$sdat2img system.transfer.list system.new.dat $img 2>/dev/null \
&& rm -f "system.new.dat" "system.transfer.list" \
|| clean $img
fi
echo "--> image extracted: $img"
work_dir="$PWD/deodex"
trap "clean \"$work_dir\"" INT
rm -Rf deodex
if [[ $($sevenzip l "$img" "system/build.prop" | grep build.prop) ]]; then
mkdir -p deodex/
system="system/"
deodex_system="deodex/"
else
mkdir -p deodex/system
system=""
deodex_system=deodex/system/
fi
echo "--> copying apps"
$sevenzip x -o${deodex_system} "$img" ${system}build.prop >/dev/null || clean "$work_dir"
for f in $apps; do
echo "----> copying $f..."
$sevenzip x -o${deodex_system} "$img" ${system}priv-app/$f >/dev/null || clean "$work_dir"
done
for f in $priv_apps; do
echo "----> copying $f..."
$sevenzip x -o${deodex_system} "$img" ${system}$f >/dev/null || clean "$work_dir"
done
arch="arm64"
for f in $apps; do
deodex "$work_dir" "$f" "$arch" priv-app || clean "$work_dir"
done
for f in $priv_apps; do
deodex "$work_dir" "$(basename $f)" "$arch" "$(dirname $f)" || clean "$work_dir"
done
echo "--> packaging flashable zip"
pushd deodex
ubin=META-INF/com/google/android/update-binary
mkdir -p $(dirname $ubin)
cp "$tool_dir/update-binary-cleaner" $ubin
$sed -i "s/ver=.*/ver=$model-$ver/" $ubin
rm -f ../../eufix-$model-$ver.zip system/build.prop
$sevenzip a -tzip ../../eufix-$model-$ver.zip . >/dev/null
if [ -z "$NO_EXTRA_FBE" ]; then
cp "$tool_dir/update-binary-fbe" $ubin
rm -f eufix-force-fbe-oreo.zip
$sevenzip a -tzip -x!system ../../eufix-force-fbe-oreo.zip . >/dev/null
fi
trap - INT
popd
echo "--> done"
popd
}
trap "echo '--> abort'; exit 1" INT
for i in "${darr[@]}"; do
f="$(basename $i)"
if [ -f "$f" ] && ! [ -f "$f".aria2 ]; then
continue
fi
echo "--> Downloading $f"
$aria2c $i || exit 1
done
trap - INT
hasfile=false
for f in *.zip; do
arr=(${f//_/ })
if [[ "${arr[0]}" != "xiaomi.eu" ]]; then
continue
fi
if [ -f $f.aria2 ]; then
echo "--> skip incomplete file: $f"
continue
fi
model=${arr[2]}
ver=${arr[3]}
extract $model $ver $f "$mipay_apps" "$private_apps"
hasfile=true
done
if $hasfile; then
echo "--> all done"
else
echo "--> Error: no eu rom detected"
fi