-
Notifications
You must be signed in to change notification settings - Fork 60
/
CommonInstaller
2385 lines (1919 loc) · 74.7 KB
/
CommonInstaller
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
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/sbin/sh
VERSION=23.2.99999999
NANODROID_UPGRADE=0
INSTALL_SUCCESS=TRUE
NANODROID_LIST=/system/addon.d/NanoDroid_FileList
NANODROID_LIST_OLD=/data/adb/NanoDroid_FileList
STORAGE=/data/media/0
curdate=$(date +%Y%m%d_%H.%M.%S)
nanodroid_logfile="${STORAGE}/nanodroid_logs/${MODID}_${VERSION}_log_${curdate}.log"
config_locations="${STORAGE} /external_sd /sdcard /sdcard1 /data /system/addon.d /tmp"
nanodroid_all_init='"10_sqlite 20_fstrim 30_logcat 40_external_sd 50_logscleaner"'
##########################################################################################
# NanoDroid Installer Setup Function
##########################################################################################
setup_installer () {
detect_bootmode
detect_outfd
! ${BOOTMODE} && [ "${MODID}" = "NanoDroid" ] && check_space
show_progress 1.0 0
show_banner
set_progress 0.1
unpack_zip
setup_busybox
mount_partitions
detect_arch
set_progress 0.2
if ${BOOTMODE}; then
media_rw=$(grep "^/dev/block.*/mnt/media_rw" /proc/mounts \
| cut -d " " -f 2 \
| cut -d "/" -f 4)
ext_storage="/storage/${media_rw}"
[ -r "${ext_storage}" ] && config_locations="${ext_storage} ${config_locations}"
fi
[ -n "${1}" ] && get_cfg_setup
[ -n "${2}" ] && get_cfg_overlay
[ -n "${3}" ] && get_cfg_apps
detect_mode
set_progress 0.3
}
##########################################################################################
# Misc. Functions
##########################################################################################
##########################################################################################
# Debug Echo: only echo in TWRP, in Magisk Manager print to logfile
##########################################################################################
decho () {
mkdir -p "${STORAGE}/nanodroid_logs"
if ${BOOTMODE}; then
echo "$@" >> "${nanodroid_logfile}"
else
echo "$@"
fi
}
##########################################################################################
# use print_info() instead of ui_print() to avoid spamming Magisk Manager output
##########################################################################################
print_info () {
if ${BOOTMODE}; then
echo "${@}"
decho "${@}"
else
echo -e "ui_print ${@}" >> /proc/self/fd/${OUTFD}
echo -e "ui_print" >> /proc/self/fd/${OUTFD}
fi
}
show_progress () {
${BOOTMODE} || echo "progress ${1} ${2}" >> /proc/self/fd/${OUTFD}
}
set_progress () {
${BOOTMODE} || echo "set_progress ${1}" >> /proc/self/fd/${OUTFD}
}
##########################################################################################
# Error out with proper cleanup
##########################################################################################
error () {
print_info " !!"
print_info "${@}"
print_info " !!"
if is_mounted /data; then
[ "${MODE}" = "MAGISK" ] && rm -rf "${MODPATH}"
installer_cleanup_env
INSTALL_SUCCESS=FALSE
nanodroid_storelogs
fi
${BOOTMODE} || umount_partitions
exit 1
}
##########################################################################################
# Taken from Magisk, check where to print to
##########################################################################################
detect_outfd () {
if [ -z $OUTFD ] || readlink /proc/$$/fd/$OUTFD | grep -q /tmp; then
# We will have to manually find out OUTFD
for FD in `ls /proc/$$/fd`; do
if readlink /proc/$$/fd/$FD | grep -q pipe; then
if ps | grep -v grep | grep -q " 3 $FD "; then
OUTFD=$FD
break
fi
fi
done
fi
}
##########################################################################################
# XXX - unused - Check if enough free space in /dev/tmp
##########################################################################################
check_space () {
space_required=524288
space_available=$(df /dev 2>/dev/null | awk '/tmpfs/{print $4}')
decho " required space : ${space_required}"
decho " available space: ${space_available}"
if [ ${space_available} -lt ${space_required} ]; then
error "Less than 512 MB free space availabe from TWRP!"
fi
}
##########################################################################################
# Taken from Magisk, check whether we're in Magisk Manager or TWRP
##########################################################################################
detect_bootmode () {
[ -z ${BOOTMODE} ] && ps | grep zygote | grep -qv grep && BOOTMODE=true
[ -z ${BOOTMODE} ] && ps -A 2>/dev/null | grep zygote | grep -qv grep && BOOTMODE=true
[ -z ${BOOTMODE} ] && BOOTMODE=false
}
##########################################################################################
# Taken from Magisk, grep build_props / /proc/cmdline for value
##########################################################################################
grep_prop () {
sed -n "s/^${1}=//p" /system/build.prop ${2} | head -n 1
}
grep_cmdline () {
local REGEX="s/^${1}=//p"
sed -E 's/ +/\n/g' /proc/cmdline | \
sed -n "${REGEX}" 2>/dev/null
}
##########################################################################################
# Set permissions for installed files
##########################################################################################
set_perm () {
chown ${2}:${3} ${1} || error "failed change owner for ${1}"
chmod ${4} ${1} || error "failed to change mode for ${1}"
if [ -n "${5}" ]; then
chcon ${5} ${1} 2>/dev/null
else chcon 'u:object_r:system_file:s0' ${1} 2>/dev/null
fi
}
set_perm_recursive () {
find ${1} -type d 2>/dev/null | while read dir; do
set_perm ${dir} ${2} ${3} ${4} ${6}
done
find ${1} -type f 2>/dev/null | while read file; do
set_perm ${file} ${2} ${3} ${5} ${6}
done
}
set_perm_data () {
if [ "${1}" = "-r" ]; then
decho " perm: data [recursive] {${2}}"
set_perm_recursive ${2} 0 0 0755 0644
else
decho " perm: data [single] {${1}}"
set_perm ${1} 0 0 0644
fi
}
set_perm_bin () {
if [ "${1}" = "-r" ]; then
decho " perm: exec [recursive] {${2}}"
set_perm_recursive ${2} 0 0 0755 0755
else
decho " perm: exec [single] {${1}}"
set_perm ${1} 0 0 0755
fi
}
##########################################################################################
# Taken from Magisk, mount functions
# Modified for NanoDroid
##########################################################################################
is_mounted () {
grep -q " $(readlink -f ${1}) " /proc/mounts 2>/dev/null
return $?
}
##########################################################################################
# taken from Magisk, with minor modifications for NanoDroid
# mount partitions
##########################################################################################
toupper() {
echo "$@" | tr '[:lower:]' '[:upper:]'
}
find_block () {
local block partname devname device
for block in "${@}"; do
block=${block}${SLOT}
device=$(find /dev/block \( -type b -o -type c -o -type l \) -name ${block} | head -n 1)
if [ -n "$device" ]; then
readlink -f "$device"
return 0
fi
done
for block in "${@}"; do
block=${block}${SLOT}
for uevent in /sys/dev/block/*/uevent; do
partname=$(awk -F= '/PARTNAME/{print $2}' ${uevent})
devname=$(awk -F= '/DEVNAME/{print $2}' ${uevent})
if [ "$(toupper ${block})" = "$(toupper ${partname})" ]; then
echo /dev/block/${devname}
return 0
fi
done
done
for block in "${@}"; do
block=${block}${SLOT}
device=$(find /dev \( -type b -o -type c -o -type l \) -maxdepth 1 -iname ${block} | head -n 1)
if [ -n "$device" ]; then
readlink -f "$device"
return 0
fi
done
return 1
}
mount_partitions () {
if ! ${BOOTMODE}; then
VENDOR_COMPAT=FALSE
SYSTEM_AS_ROOT=FALSE
SLOT=$(grep_cmdline androidboot.slot_suffix)
if [ -z ${SLOT} ]; then
SLOT=$(grep_cmdline androidboot.slot)
[ -z ${SLOT} ] || SLOT=_${SLOT}
fi
[ -z ${SLOT} ] && DEVICE_AB=FALSE || DEVICE_AB=TRUE
decho " INFO: #1 [SLOT] ${SLOT}"
is_mounted /data || mount /data || decho "failed to mount /data!"
mount -o bind /dev/urandom /dev/random
SYSTEM_BLOCK=$(find_block system app)
decho " INFO: #5 [SYSTEM_BLOCK] ${SYSTEM_BLOCK}"
[ -z "${SYSTEM_BLOCK}" ] && error "failed to detect block device for /system"
if is_mounted /system_root; then
umount /system 2&>/dev/null
umount /system_root 2&>/dev/null
fi
mkdir -p /system /system_root
mount -o rw ${SYSTEM_BLOCK} /system
if [ -f /system/build.prop ]; then
SYSTEM=/system
elif [ -f /system/system/build.prop -o -f /system/init -o -L /system/init ]; then
SYSTEM_AS_ROOT=true
SYSTEM=/system/system
if ! mount --move /system /system_root; then
umount /system
umount -l /system
mount -o rw ${SYSTEM_BLOCK} /system_root
fi
mount -o bind /system_root/system /system
fi
VENDOR_BLOCK=$(find_block vendor vnr)
decho " INFO: #7 [VENDOR_BLOCK] ${VENDOR_BLOCK}"
[ -z "${VENDOR_BLOCK}" ] && error "failed to detect block device for /vendor"
! is_mounted /vendor && mount -o ro /vendor
! is_mounted /vendor && mount -o ro ${VENDOR_BLOCK} /vendor
if [[ ! $(is_mounted /vendor) && -d /system/vendor ]]; then
### XXX work-around required for some devices
VENDOR_COMPAT=TRUE
ln -sf /system/vendor /vendor >/dev/null
fi
decho " INFO: #8 [prop]"
ls -l /system/*.prop
[ -d /system/apex ] && mount_apex
fi
[ ! -f /system/build.prop ] && error "failed to mount /system (unsupported A/B device?)"
export ANDROID_RUNTIME_ROOT=/apex/com.android.runtime
export ANDROID_TZDATA_ROOT=/apex/com.android.tzdata
export ANDROID_ART_ROOT=/apex/com.android.art
export ANDROID_I18N_ROOT=/apex/com.android.i18n
}
##########################################################################################
# taken from Magisk, with minor modifications for NanoDroid
# mount APEX directories or images
##########################################################################################
mount_apex () {
mkdir -p /apex
local pattern='s/.*"name":[^"]*"\([^"]*\).*/\1/p'
mount -t tmpfs tmpfs /apex -o mode=755
for apex in /system/apex/*; do
apex_loop="/dev/loop_apex_$(basename ${apex} .apex)"
if [ -f "${apex}" ]; then
unzip -oq "${apex}" apex_payload.img -d /apex
apex_mount=$(unzip -qp ${apex} apex_manifest.pb | strings | head -n 1)
[ -z ${apex_mount} ] && apex_mount=$(unzip -qp ${apex} apex_manifest.json | sed -n ${pattern})
[ -z ${apex_mount} ] && continue
mkdir -p "${apex_mount}"
mount_apex_loop "${apex_mount}" || error "APEX loop setup failed!"
elif [ -d "${apex}" ]; then
if [ -f ${apex}/apex_manifest.json ]; then
apex_mount=/apex/$(sed -n ${pattern} ${apex}/apex_manifest.json)
elif [ -f ${apex}/apex_manifest.pb ]; then
apex_mount=/apex/$(strings ${apex}/apex_manifest.pb | head -n 1)
else
continue
fi
mkdir -p "${apex_mount}"
mount -o bind "${apex}" "${apex_mount}"
fi
done
echo " INFO: #10 [APEX [ALL]] $(ls /system/apex/*)"
}
##########################################################################################
# taken from Magisk, with minor modifications for NanoDroid
# helper function for mounting APEX directories or images
##########################################################################################
mount_apex_loop () {
local number=0
local minorx=1
local loop
[ -e /dev/block/loop1 ] && minorx=$(stat -Lc '%T' /dev/block/loop1)
apex_mount="${1}"
decho " *** mount_apex_loop [apex_mount]: ${apex_mount}"
while [ ${number} -lt 64 ]; do
loop=/dev/block/loop${number}
[ -e ${loop} ] || mknod ${loop} b 7 $((number * minorx))
if losetup "${loop}" /apex/apex_payload.img 2>/dev/null; then
decho " *** mount_apex_loop [loop]: ${loop}"
if mount -text4 -oro,noatime "${loop}" "${apex_mount}"; then
rm -f /apex/apex_payload.img
break
fi
fi
number=$((number + 1))
done
}
##########################################################################################
# unmount partitions
##########################################################################################
umount_partitions () {
umount -l /system_root 2>/dev/null
umount -l /system 2>/dev/null
umount -l /system/vendor 2>/dev/null
umount -l /vendor 2>/dev/null
umount -l /dev/random 2>/dev/null
mount | awk '/ \/apex/{print $1 " " $3}' | while read apex_loop apex_mount; do
umount -l "${apex_mount}" 2>/dev/null
losetup -d "${apex_loop}" 2>/dev/null
done
rm -rf /apex
unset ANDROID_RUNTIME_ROOT
unset ANDROID_TZDATA_ROOT
unset ANDROID_ART_ROOT
unset ANDROID_I18N_ROOT
}
##########################################################################################
# migrate from Nanolx to Official APKs and vice-versa
##########################################################################################
detect_migrate_apk () {
local app_id="${1}"
local app_name="${2}"
USER_PATH=$(find /data/app -type d -name "${app_id}-*" &>/dev/null)
if [ -n ${USER_PATH} ]; then
if ${UNZIP} -l "${MODPATH}/system/priv-app/${app_name}/${app_name}.apk" | grep META-INF/NANOLX.RSA &>/dev/null; then
BUILD_INST=Nanolx
else BUILD_INST=Official
fi
if ${UNZIP} -l "${USER_PATH}/base.apk" | grep META-INF/NANOLX.RSA &>/dev/null; then
BUILD_USER=Nanolx
else BUILD_USER=Official
fi
if [ "${BUILD_INST}" != "${BUILD_USER}" ]; then
decho " + Removing ${BUILD_USER} ${app_name} in favor of ${BUILD_INST} ${app_name}"
rm -rf "${USER_PATH}"
if [ "${app_id}" = "com.google.android.gms" ]; then
decho " + Resetting GCM/FCM connection of all apps to allow re-registration"
find /data/data/*/shared_prefs -name com.google.android.gms.*.xml -delete
fi
fi
fi
}
##########################################################################################
# check if ROM has native fake signature spoofing support
# origingally by @ale5000 - revised by me
##########################################################################################
check_fake_package_signature () {
PERMISSION=android.permission.FAKE_PACKAGE_SIGNATURE
PERMISSION_OD=$(echo -n "${PERMISSION}" | od -A n -t x1 | tr -d '\n' | sed -e 's/^ //g;s/ /00/g')
PATCH_TYPE=""
FRAMEWORKRES_PATCH=false
SERVICESJAR_PATCH=false
mkdir -p ${TMPDIR}/sigcheck
# natively patched ROM: only framework-res.apk patched (old)
# natively patched ROM: both framework-res.apk and services.jar patched (new)
# self patched ROM: only services.jar patched
# check framework-res.apk for the patch
unzip -oq /system/framework/framework-res.apk -d "${TMPDIR}/sigcheck" \
|| error "failed to unpack framework-res.apk"
grep -qF "${PERMISSION}" "${TMPDIR}"/sigcheck/AndroidManifest.xml && FRAMEWORKRES_PATCH=true
od -A n -t x1 "${TMPDIR}"/sigcheck/AndroidManifest.xml | tr -d ' \n' | grep -qF "${PERMISSION_OD}" && FRAMEWORKRES_PATCH=true
# check services.jar for the patch
unzip -oq /system/framework/services.jar -d "${TMPDIR}/sigcheck" \
|| error "failed to unpack services.jar"
grep -qF "${PERMISSION}" "${TMPDIR}"/sigcheck/*.dex && SERVICESJAR_PATCH=true
od -A n -t x1 "${TMPDIR}"/sigcheck/*.dex | tr -d ' \n' | grep -qF "${PERMISSION_OD}" && SERVICESJAR_PATCH=true
# we don't use this anywhere (except in SysTest log),
# but may still come in hand in the future
if ${FRAMEWORKRES_PATCH} && ! ${SERVICESJAR_PATCH}; then
PATCH_TYPE="native_old"
elif ${FRAMEWORKRES_PATCH} && ${SERVICESJAR_PATCH}; then
PATCH_TYPE="native_new"
elif ! ${FRAMEWORKRES_PATCH} && ${SERVICESJAR_PATCH}; then
PATCH_TYPE="self_patched"
fi
[ -n "${PATCH_TYPE}" ] && return 0 || return 1
}
##########################################################################################
# parse output from "aapt dump xmltree" back to xml
##########################################################################################
parse_aapt_dump_xmltree(){
local i step indent prev_indent cur_type prev_type entity_stack key value tmp
while IFS=$'\n' read -r i; do
cur_type="$(echo "${i}" | awk '{print $1}')"
case "${cur_type}" in
E:) # entity
indent="$(echo "${i}" | awk -F'[^ ]' '{print length($1)}')"
[ -z "${prev_type}" ] || [ "${prev_type}" = "C:" ] || echo -n ">"
[ -z "${prev_type}" ] || [ "$((${prev_indent:=0}-${indent}))" -lt 0 ] || for j in $(seq "$((${prev_indent:=0}-${indent}))" "-${step:=2}" "$((${indent}-${step}))"); do
echo -ne "\n</${entity_stack##* }>"
entity_stack="${entity_stack% *}"
done
echo -ne "\n<$(echo -n "${i}" | awk '{print $2}')"
entity_stack="${entity_stack} $(echo "${i}" | awk '{print $2}')"
prev_indent="${indent}"
;;
A:) # attribute
key="$(echo "${i}" | awk '{print $2}' | awk -F= '{print $1}')"
value="${i#*=}"
case "${value}" in
"(type 0x12)"*) [ "${value#*)}" = "0x0" ] && value='"false"' || value='"true"';;
"(type 0x10)"*) value="$(printf '%d' "${value#*)}")";;
*) value="${value% \(Raw:*}"
esac
echo -n " ${key}=${value}"
unset key value
;;
C:) # content
tmp="${i#*C: \"}"
echo -n ">${tmp%*\"}"
unset tmp
;;
esac
prev_type="${cur_type}"
done <&0
[ "${prev_type}" = "C:" ] || echo -n ">"
until [ -z "${entity_stack}" ]; do
echo -ne "\n</${entity_stack##* }>"
entity_stack="${entity_stack% *}"
done
}
##########################################################################################
# sign package with test keys
##########################################################################################
byte_split(){ local i; for i in $(seq 0 2 $((${#1}-1))); do echo "${1:${i}:2}"; done; }
hex2bin(){ local i; for i in $(seq 0 2 $((${#1}-1))); do printf "\x${1:${i}:2}"; done; }
jar_sign(){
local tmpdir="${TMPDIR}/$(tr -cd '0-9a-z' 2>/dev/null </dev/urandom | head -c8)" apk="${1}" i
mkdir -p "${tmpdir}/META-INF"
cd "${tmpdir}"; echo A | unzip -qo "${apk}"
cat <<EOF >"${tmpdir}/META-INF/MANIFEST.MF"
Manifest-Version: 1.0
Built-By: NanoDroid
Created-By: 1.0 (Android)
EOF
for i in $(find "${tmpdir}" -type f); do
echo "${i}" | grep -qvE "^${tmpdir}/META-INF/" || continue
cat <<EOF >>"${tmpdir}/META-INF/MANIFEST.MF"
Name: ${i#${tmpdir}/*}
SHA-256-Digest: $(hex2bin "$(sha256sum "${i}" | awk '{print $1}')" | base64 | tr -d '[:space:]')
EOF
done
cat <<EOF >"${tmpdir}/META-INF/CERT.SF"
Signature-Version: 1.0
Created-By: 1.0 (Android)
SHA-256-Digest-Manifest: $(hex2bin "$(sha256sum "${tmpdir}/META-INF/MANIFEST.MF" | awk '{print $1}')" | base64 | tr -d '[:space:]')
X-Android-APK-Signed: 2, 3
EOF
for i in $(find "${tmpdir}" -type f); do
echo "${i}" | grep -qvE "^${tmpdir}/META-INF/" || continue
cat <<EOF >>"${tmpdir}/META-INF/CERT.SF"
Name: ${i#${tmpdir}/*}
SHA-256-Digest: $(hex2bin "$(sha256sum "${i}" | awk '{print $1}')" | base64 | tr -d '[:space:]')
EOF
done
"${OPENSSL}" smime -sign -binary -noattr -in "${tmpdir}/META-INF/CERT.SF" -outform der -out "${tmpdir}/META-INF/CERT.RSA" -signer "${INSTALLER}/testkey.crt" -md sha256 -inkey "${INSTALLER}/testkey.key"
"${ZIP}" -r "${apk}" ./META-INF
}
generate_signed_data(){
local idx=0 apk="${1}" segment_sz=1048576 tmpfile="${INSTALLER}/tmpfile"
local apk_content="$(od -v -tx1 -An "${apk}" | tr -dc '[0-9a-fA-F]')"
local eocd_base="$(($(echo -n "${apk_content%504b0506*}" | wc -c)/2))"; unset apk_content
local cd_base="$(printf '%d' "0x$(od -v -tx1 -An -j$((${eocd_base}+16)) -N4 "${apk}" | tr -c '[0-9a-fA-F]' '\n' | tac | tr -dc '[0-9a-fA-F]')")"
local cd_size="$(printf '%d' "0x$(od -v -tx1 -An -j$((${eocd_base}+12)) -N4 "${apk}" | tr -c '[0-9a-fA-F]' '\n' | tac | tr -dc '[0-9a-fA-F]')")"
dd bs=1 count="${cd_base}" if="${apk}" of="${INSTALLER}/contents"
dd bs=1 skip="${cd_base}" count="${cd_size}" if="${apk}" of="${INSTALLER}/cd"
dd bs=1 skip="${eocd_base}" if="${apk}" of="${INSTALLER}/eocd"
echo -n >"${tmpfile}"; segment_sz=1048576; while [ $((${idx}*1048576)) -lt ${cd_base} ]; do
[ $(($((${idx}+1))*1048576)) -lt ${cd_base} ] || segment_sz=$((${cd_base}-$((${idx}*1048576))))
hex2bin "a5$(byte_split $(printf '%08x' ${segment_sz}) | tac | tr -dc '[0-9a-fA-F]')$(dd bs=1 skip="$((${idx}*1048576))" count="${segment_sz}" if="${INSTALLER}/contents" | sha512sum | awk '{print $1}')" >> "${tmpfile}"
idx=$((${idx}+1))
done
local cd_idx=0 segment_sz=1048576; while [ $((${cd_idx}*1048576)) -lt ${cd_size} ]; do
[ $(($((${cd_idx}+1))*1048576)) -lt ${cd_size} ] || segment_sz=$((${cd_size}-$((${cd_idx}*1048576))))
hex2bin "a5$(byte_split $(printf '%08x' ${segment_sz}) | tac | tr -dc '[0-9a-fA-F]')$(dd bs=1 skip="$((${cd_idx}*1048576))" count="${segment_sz}" if="${INSTALLER}/cd" | sha512sum | awk '{print $1}')" >> "${tmpfile}"
cd_idx=$((${cd_idx}+1))
done
local eocd_idx=0 eocd_sz="$(stat -c '%s' "${INSTALLER}/eocd")" segment_sz=1048576
while [ $((${eocd_idx}*1048576)) -lt ${eocd_sz} ]; do
[ $(($((${eocd_idx}+1))*1048576)) -lt ${eocd_sz} ] || segment_sz=$((${eocd_sz}-$((${eocd_idx}*1048576))))
hex2bin "a5$(byte_split $(printf '%08x' ${segment_sz}) | tac | tr -dc '[0-9a-fA-F]')$(dd bs=1 skip="$((${eocd_idx}*1048576))" count="${segment_sz}" if="${INSTALLER}/eocd" | sha512sum | awk '{print $1}')" >> "${tmpfile}"
eocd_idx=$((${eocd_idx}+1))
done
local digest_le="$(hex2bin "5a$(byte_split $(printf '%08x' $((${idx}+${cd_idx}+${eocd_idx}))) | tac | tr -dc '[0-9a-fA-F]')$(od -v -An -tx1 "${tmpfile}" | tr -dc '[0-9a-fA-F]')" | "${OPENSSL}" dgst -hex -c -sha512 | awk '{print $NF}' | tr ':' '\n' | tac | tr -dc '[0-9a-fA-F]')"
local digest_field="$(byte_split 00000104 | tac | tr -dc '[0-9a-fA-F]')$(byte_split $(printf '%08x' $((${#digest_le}/2))) | tac | tr -dc '[0-9a-fA-F]')${digest_le}"
local digest_record="$(byte_split $(printf '%08x' $((${#digest_field}/2))) | tac | tr -dc '[0-9a-fA-F]')${digest_field}"
local digest_seq="$(byte_split $(printf '%08x' $((${#digest_record}/2))) | tac | tr -dc '[0-9a-fA-F]')${digest_record}"
local cert_len="$("${OPENSSL}" x509 -in "${INSTALLER}/testkey.crt" -outform DER | wc -c)"
local cert_head="$(byte_split $(printf '%08x' ${cert_len}) | tac | tr -dc '[0-9a-fA-F]')"
local cert_seq_head="$(byte_split $(printf '%08x' $((${cert_len}+4))) | tac | tr -dc '[0-9a-fA-F]')"
hex2bin "${digest_seq}${cert_seq_head}${cert_head}" > "${INSTALLER}/signed_data"
"${OPENSSL}" x509 -in "${INSTALLER}/testkey.crt" -outform DER >> "${INSTALLER}/signed_data"
}
apk_sign(){
local apk="${1}" min_sdk="$(byte_split $(printf '%08x' 23) | tac | tr -dc '[0-9a-fA-F]')" max_sdk="ffffff7f" attrib_seq="00000000" sdk_spec i block_id
generate_signed_data "${apk}"
"${OPENSSL}" x509 -in "${INSTALLER}/testkey.crt" -pubkey -noout | tail -n+2 | head -n-1 | base64 -d >"${INSTALLER}/testkey.pub"
local pub_footer="$(byte_split $(printf '%08x' $(stat -c '%s' "${INSTALLER}/testkey.pub")) | tac | tr -dc '[0-9a-fA-F]')$(od -v -An -tx1 "${INSTALLER}/testkey.pub" | tr -dc '[0-9a-fA-F]')"
rm "${INSTALLER}/testkey.pub"
echo -n >"${INSTALLER}/sigblock"
for i in $(seq 2 3); do
case "${i}" in
2)
block_id="$(byte_split 7109871a | tac | tr -dc '[0-9a-fA-F]')"
;;
3)
block_id="$(byte_split f05368c0 | tac | tr -dc '[0-9a-fA-F]')"
sdk_spec="${min_sdk}${max_sdk}"
;;
esac
cat "${INSTALLER}/signed_data" > "${INSTALLER}/signed_data.v${i}"
hex2bin "${sdk_spec}${attrib_seq}" >> "${INSTALLER}/signed_data.v${i}"
local signed_data="$(byte_split "$(printf '%08x' "$(stat -c '%s' "${INSTALLER}/signed_data.v${i}")")" | tac | tr -dc '[0-9a-fA-F]')$(od -v -An -tx1 "${INSTALLER}/signed_data.v${i}" | tr -dc '[0-9a-fA-F]')"
"${OPENSSL}" dgst -sha512 -sign "${INSTALLER}/testkey.key" "${INSTALLER}/signed_data.v${i}" > "${INSTALLER}/signed_data.v${i}.sig"
local sig_len="$(stat -c '%s' "${INSTALLER}/signed_data.v${i}.sig")"
local sig_seq="${sdk_spec}$(byte_split $(printf '%08x' $((${sig_len}+12))) | tac | tr -dc '[0-9a-fA-F]')$(byte_split $(printf '%08x' $((${sig_len}+8))) | tac | tr -dc '[0-9a-fA-F]')$(byte_split 00000104 | tac | tr -dc '[0-9a-fA-F]')$(byte_split $(printf '%08x' ${sig_len}) | tac | tr -dc '[0-9a-fA-F]')$(od -v -An -tx1 "${INSTALLER}/signed_data.v${i}.sig" | tr -dc '[0-9a-fA-F]')"
rm "${INSTALLER}/signed_data.v${i}" "${INSTALLER}/signed_data.v${i}.sig"
local signer="${signed_data}${sig_seq}${pub_footer}"
local sigblock="${block_id}$(byte_split $(printf '%08x' $((${#signer}/2+4))) | tac | tr -dc '[0-9a-fA-F]')$(byte_split $(printf '%08x' $((${#signer}/2))) | tac | tr -dc '[0-9a-fA-F]')${signer}"
hex2bin "$(byte_split $(printf '%016x' $((${#sigblock}/2))) | tac | tr -dc '[0-9a-fA-F]')${sigblock}" >> "${INSTALLER}/sigblock"
done
rm "${INSTALLER}/signed_data"
local sigblock_len="$(byte_split $(printf '%016x' "$(($(stat -c '%s' "${INSTALLER}/sigblock")+24))") | tac | tr -dc '[0-9a-fA-F]')"
cat "${INSTALLER}/contents" >"${apk}.tmp"
hex2bin "${sigblock_len}" >>"${apk}.tmp"
cat "${INSTALLER}/sigblock" >>"${apk}.tmp"
hex2bin "${sigblock_len}" >>"${apk}.tmp"
echo -n "APK Sig Block 42" >>"${apk}.tmp"
sync
local new_cd_base="$(byte_split $(printf '%08x' "$(stat -c '%s' "${apk}.tmp")") | tac | tr -dc '[0-9a-fA-F]')"
cat "${INSTALLER}/cd" >>"${apk}.tmp"
dd bs=1 count=16 if="${INSTALLER}/eocd" >>"${apk}.tmp"
hex2bin "${new_cd_base}" >>"${apk}.tmp"
dd bs=1 skip=20 if="${INSTALLER}/eocd" >> "${apk}.tmp"
mv "${apk}.tmp" "${apk}"
}
##########################################################################################
# privapp permission whitelist generation
##########################################################################################
dump_system_privapp_permissions () {
local sys_list="${TMPDIR}/privapp-permissions.list"
local tmp_list="${TMPDIR}/privapp-permissions.dump"
rm -f "${sys_list}"
"${AAPT}" dump xmltree /system/framework/framework-res.apk AndroidManifest.xml | \
tr -d '\n' | sed -e 's/E:/\n/g' > "${tmp_list}"
for pattern in "(type 0x11)0x[1357]2" "(type 0x11)0xc212" \
"(type 0x11)0x[02]" "(type 0x11)0x[13]02" \
"(type 0x11)0x1[01]2" "(type 0x11)0x1001" \
"(type 0x11)0x[15]2" "(type 0x11)0x1000072"; do
grep "${pattern}" "${tmp_list}" | \
awk -F\" '{print $2}' >> "${sys_list}"
done
# XXX pre-patched custom ROM
check_fake_package_signature && \
echo "android.permission.FAKE_PACKAGE_SIGNATURE" >> "${sys_list}"
}
create_privapp_permissions_whitelist () {
local apk="${MODPATH}/system/priv-app/${1}/${1}.apk"
local apk_name="$("${AAPT}" dump badging "${apk}" | awk -F \' '/^package: name/{print $2}')"
local apk_list="${TMPDIR}/${apk_name}.list"
local sys_list="${TMPDIR}/privapp-permissions.list"
local app_whitelist="${TMPDIR}/${apk_name}.xml"
local inst_whitelist="/system/etc/permissions/${apk_name}.xml"
[ ! -f "${sys_list}" ] && dump_system_privapp_permissions
"${AAPT}" dump permissions "${apk}" | \
awk -F \' '/^uses-permission:/{print $2}' > "${apk_list}"
echo '<?xml version="1.0" encoding="utf-8"?>
<permissions>
<privapp-permissions package="'${apk_name}'">' > "${app_whitelist}"
cat "${apk_list}" | while read perm; do
if grep -q "${perm}" "${sys_list}"; then
decho " ++ package ${apk_Name} needs privapp-whitelist ${perm}"
echo ' <permission name="'${perm}'" />' >> "${app_whitelist}"
fi
done
echo ' </privapp-permissions>
</permissions>' >> "${app_whitelist}"
mkdir -p "${MODPATH}/system/etc/permissions/"
cp "${app_whitelist}" "${MODPATH}/${inst_whitelist}"
set_perm_data "${MODPATH}/${inst_whitelist}"
installinfo_add "${inst_whitelist}"
}
##########################################################################################
# Reset System Runtime permissions
##########################################################################################
reset_runtime_permissions () {
# required on Android 10, else all apps bundled with NanoDroid will not
# show a permission prompt for android.permission.WRITE_EXTERNAL_STORAGE
if [ "${SDK_VERSION}" -ge 29 ]; then
if ${BOOTMODE}; then
pm reset-permissions
else
find /data/system/users -type f -name 'runtime-permissions.xml' 2>/dev/null | while read file; do
rm -f "${file}"
decho " ++ resetting permissions: ${file}"
done
fi
fi
}
##########################################################################################
# Unpack NanoDroid zip
##########################################################################################
unpack_zip () {
TMPDIR=/dev/tmp
TMP_LIBDIR=${TMPDIR}/nanodroid-lib
INSTALLER=${TMPDIR}/install
rm -rf ${INSTALLER}
mkdir -p ${INSTALLER} || error "failed to prepare environment!"
print_info " > prepare installation"
unzip -oq "${ZIP}" -d "${INSTALLER}" || \
error "failed to prepare environment!"
rm -rf ${TMPDIR}/framework-res
rm -f ${TMPDIR}/*.xml
rm -f ${TMPDIR}/*.list
chmod 0755 ${INSTALLER}/*.*
}
##########################################################################################
# Print NanoDroid banner
##########################################################################################
show_banner () {
print_info " "
print_info "*****************************"
print_info " NanoDroid "
case ${MODID} in
NanoDroid )
print_info " > Full package "
;;
NanoDroid_microG )
print_info " > microG package "
;;
NanoDroid_FDroid )
print_info " > F-Droid package "
;;
NanoDroid_BromiteWebView )
print_info " > Bromite WebView package "
;;
NanoDroid_OsmAnd )
print_info " > OsmAnd package "
;;
NanoDroid_Google )
print_info " > Google package "
;;
esac
if [ "${ZIP}" == *${VERSION}* ]; then
print_info " ${VERSION} "
else
print_info " ${VERSION} (snapshot) "
fi
print_info "*****************************"
print_info " "
}
##########################################################################################
# Architecture detection
##########################################################################################
detect_arch () {
SDK_VERSION=$(grep_prop ro.build.version.sdk)
ABI=$(grep_prop ro.product.cpu.abi | cut -c-3)
ABI2=$(grep_prop ro.product.cpu.abi2 | cut -c-3)
ABILONG=$(grep_prop ro.product.cpu.abi)
ARCH=arm
[ "$ABI" = "x86" ] && ARCH=x86
[ "$ABI2" = "x86" ] && ARCH=x86
[ "$ABILONG" = "arm64-v8a" ] && ARCH=arm64
[ "$ABILONG" = "x86_64" ] && ARCH=x86_64
case ${ARCH} in
arm ) BIN_ARCH="arm"
LIB_ARCHES="armeabi-v7a armeabi"
SWIPE_LIBDIR=lib ;;
arm64 ) BIN_ARCH="arm"
LIB_ARCHES="arm64-v8a armeabi-v7a armeabi"
SWIPE_LIBDIR=lib64 ;;
x86 ) BIN_ARCH="x86"
LIB_ARCHES="x86 armeabi-v7a armeabi"
SWIPE_LIBDIR=lib ;;
x86_64 ) BIN_ARCH="x86"
LIB_ARCHES="x86_64 x86 armeabi-v7a armeabi"
SWIPE_LIBDIR=lib64 ;;
esac
UNZIP="${INSTALLER}/unzip.${BIN_ARCH}"
AAPT="${INSTALLER}/aapt.${BIN_ARCH}"
ZSTD="${INSTALLER}/zstd.${BIN_ARCH}"
OPENSSL="${INSTALLER}/openssl.${BIN_ARCH}"
ZIP="${INSTALLER}/zip.${BIN_ARCH}"
ZIPALIGN="${INSTALLER}/zipalign.${BIN_ARCH}"
chmod +x "${UNZIP}" "${AAPT}" "${ZSTD}"
case ${SDK_VERSION} in
19 ) GSYNC_VER=K ;;
21 | 22 ) GSYNC_VER=L ;;
23 ) GSYNC_VER=M ;;
24 | 25 ) GSYNC_VER=N ;;
26 | 27 ) GSYNC_VER=O ;;
28 ) GSYNC_VER=P ;;
29 ) GSYNC_VER=Q ;;
30 ) GSYNC_VER=R ;;
* ) GSYNC_UNSUPPORTED=1 ;;
esac
case ${ARCH} in
arm )
case ${SDK_VERSION} in
29 | 30 )
LIBJNI_IME=libjni_latinimegoogle.so_28
;;
19 | 21 | 22 | 23 | 27 | 28 )
LIBJNI_IME=libjni_latinimegoogle.so_${SDK_VERSION}
;;
24 | 25 | 26 )
LIBJNI_IME=libjni_latinimegoogle.so_23
;;
* )
SWIPE_UNSUPPORTED=1
;;
esac
;;
arm64 )
case ${SDK_VERSION} in
29 | 30 )
LIBJNI_IME=libjni_latinimegoogle.so_28
;;
21 | 22 | 23 | 27 | 28 )
LIBJNI_IME=libjni_latinimegoogle.so_${SDK_VERSION}
;;
24 | 25 | 26 )
LIBJNI_IME=libjni_latinimegoogle.so_23
;;
* )
SWIPE_UNSUPPORTED=1
;;
esac
;;
x86 | x86_64 )
case ${SDK_VERSION} in
19 | 21 | 22 )
SWIPE_UNSUPPORTED=1
;;
23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 )
LIBJNI_IME=libjni_latinimegoogle.so
;;
* )
SWIPE_UNSUPPORTED=1
;;
esac
;;
esac
if [[ "${SDK_VERSION}" -lt 21 ]]; then
UNFOLD_APP_DIR=1
else UNFOLD_APP_DIR=0
fi
if [[ "${SDK_VERSION}" -lt 19 ]]; then
print_info " "
print_info " ++ Installing on pre-KitKat ROM, full"
print_info " ++ compatibility is not guaranteed!"
print_info " "
fi
[ ! -d /data/adb ] && mkdir /data/adb
}
##########################################################################################
# Detect and setup installation mode
##########################################################################################
detect_mode () {
[ -f "${NANODROID_LIST_OLD}" ] && mv "${NANODROID_LIST_OLD}" "${NANODROID_LIST}"
case "${nanodroid_forcesystem}" in
1 )
if ${BOOTMODE}; then
print_info "nanodroid_forcesystem set to true"
error "can't install to /system from Magisk Manager"
fi
MODE_DETECT=forced
MODE=SYSTEM
;;
0 | *)
MODE_DETECT=detected
MODE=SYSTEM
MAGISK_VER_CODE=0
if [ ! -f "${NANODROID_LIST}" ]; then
if [ -f /data/adb/magisk/util_functions.sh ]; then
. /data/adb/magisk/util_functions.sh
elif [ -f /data/magisk/util_functions.sh ]; then
. /data/magisk/util_functions.sh
fi
if [ ${MAGISK_VER_CODE} -ge 19000 ]; then
MODE=MAGISK
else
MODE=SYSTEM
fi
fi
;;
esac