forked from AdrianDC/advanced_development_shell_tools
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtools.rc
More file actions
1068 lines (892 loc) · 28.6 KB
/
Copy pathtools.rc
File metadata and controls
1068 lines (892 loc) · 28.6 KB
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
#!/bin/bash
#
# Copyright 2015-2018 Adrian DC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# source <(curl -Ls https://github.com/AdrianDC/android_development_shell_tools/raw/master/sources/android_adb/tools.rc)
# source <(curl -Ls https://github.com/AdrianDC/android_development_shell_tools/raw/master/sources/android_build/helpers.rc)
# source <(curl -Ls https://github.com/AdrianDC/android_development_shell_tools/raw/master/sources/android_devices/target.rc)
# source <(curl -Ls https://github.com/AdrianDC/android_development_shell_tools/raw/master/sources/host/common.rc)
# source <(curl -Ls https://github.com/AdrianDC/android_development_shell_tools/raw/master/sources/android_release/cleaners.rc)
# source <(curl -Ls https://github.com/AdrianDC/android_development_shell_tools/raw/master/sources/android_kernel/tools.rc)
# === Build paths ===
export ANDROID_DEVELOPMENT_SHELL_TOOLS_BBOOTIMG=${ANDROID_DEVELOPMENT_SHELL_TOOLS_DIR}/addons/bbootimg/bbootimg;
export ANDROID_DEVELOPMENT_SHELL_TOOLS_BBOOTIMG_HOST=${ANDROID_DEVELOPMENT_SHELL_TOOLS_DIR}/addons/bbootimg/bbootimg_host;
# === Fast Bootimage Builder ===
function fboota()
{
# Usage
if [ ! "$(type -t hmm 2> /dev/null)" = 'function' ] ||
[ -z "${TARGET_PRODUCT}" ] || [ "${1}" = '-h' ]; then
echo '';
echo ' Usage: fboota [fastupl,flash,full,inject,push,recovery,sep,unsecure,zip] (Advanced bootimage builder)';
echo ' Details: fboota needs the envbuild and lunch variables';
echo '';
return;
fi;
# Variables
local adb_cmd;
local bootimg;
local cwd;
local device;
local device_common=;
local image_file;
local image_name;
local modules;
local module_file;
local params=${1};
local tmpfile;
# Initialize variables
cwd=$(pwd);
device=$(repogetdevice);
tmpfile=$(mktemp);
# If adb required, initialize adb
if [[ "${params}" == *'full'* ]] || [[ "${params}" == *'inject'* ]] || [[ "${params}" == *'push'* ]]; then
adbwait;
adbro;
fi;
# Build environment
croot;
export USE_NINJA=false;
export USE_SOONG_UI=false;
# Header
echo '';
echo -e ' \e[1;37m[ Fast Kernel Builder by Adrian DC - 2016-2018 ]\e[0m';
echo '';
# Root holder for fastboot flash
if [[ "${params}" == *'flash'* ]]; then
sudo echo -n '';
fi;
# Output cleaner
if [[ "${params}" == *'sep'* ]]; then
outsepdevcl "${device}";
else
outbootdevcl "${device}";
fi;
echo '';
# Unsecure adb changes
if [[ "${params}" == *'unsecure'* ]]; then
bootimagedebuggable "${device}" true;
echo '';
fi;
# Disable build overlays for fast builds
export USE_NINJA=false;
export USE_SOONG_UI=false;
# Ramdisk common files builder
if [[ "${params}" == *'full'* ]]; then
device_common=$(cat './device/'*"/${device}/device.mk" \
| grep 'inherit-product' \
| grep 'device' \
| grep -v 'gps' \
| sed 's/.*\(device.*\)\/[^\/]*)/\1/');
if [ ! -z "${device_common}" ]; then
mmm "./${device_common}/rootdir/";
fi;
fi;
# Recoveryimage builder
if [[ "${params}" == *'recovery'* ]]; then
image_file=recovery;
image_name=recoveryimage;
# Bootimage builder
else
image_file=boot;
image_name=bootimage;
fi
# Image fast builder
if [[ ! "${params}" == *'full'* ]] && mms -v > /dev/null 2>&1; then
mms "${image_name}" | tee "${tmpfile}";
# Image full builder
else
makes "${image_name}" | tee "${tmpfile}";
fi;
# Unsecure adb changes
if [[ "${params}" == *'unsecure'* ]]; then
bootimagedebuggable "${device}" false;
fi;
# Bootimage file path
bootimg=$(grep -a ".*image.*${image_file}\.img" "${tmpfile}" \
| head -1 \
| sed "s/.*: \(.*${image_file}\.img\).*/\1/g");
# Detect failed build
if [ -z "${bootimg}" ] || [ ! -f "${bootimg}" ]; then
echo '';
echo ' fboota: Bootimage build failed';
echo '';
# Inject kernel inside bootimage
elif [[ "${params}" == *'inject'* ]]; then
# Use the fbootk function
fbootk "$(dirname "${bootimg}")/kernel";
# Zip the bootimage
elif [[ "${params}" == *'zip'* ]]; then
# Push zip to device
if [[ "${params}" == *'push'* ]]; then
bootzip "${device}" "${bootimg}" 'true';
else
bootzip "${device}" "${bootimg}";
fi;
# Upload the bootimage
elif [[ "${params}" == *'fastupl'* ]]; then
fastupl "${bootimg}" 'bootimage';
# Flash the bootimage to the device
elif [[ "${params}" == *'flash'* ]]; then
# Detect modules
modules=$(find "./out/target/product/${device}/system/lib/modules/" -type f);
# Handle kernel modules
if [ ! -z "${modules}" ]; then
echo '';
echo -e ' \e[1;37m[ fboota: Kernel modules... ]\e[0m';
echo '';
# Give adb root permissions
adbwait;
adbro;
adb_cmd=$(adbcmd);
# Push the kernel modules
adbsur "rm -rf '/system/lib/modules/'";
for module in ${modules[*]}; do
module_file=$(basename "${module}");
${adb_cmd} push "${module}" "/system/lib/modules/${module_file}";
done;
fi;
# Flash new bootimage
echo '';
echo -e " \e[1;37m[ fboota: Bootloader ${bootimg}... ]\e[0m";
echo '';
# Export to external usage
export PACKAGE_RESULT=${bootimg};
# Give adb root permissions
adbwait;
adbro;
adb_cmd=$(adbcmd);
# Reboot to bootloader
${adb_cmd} reboot bootloader;
# Flash new boot
timeout -k 20 20 sudo fastboot flash boot "${bootimg}";
timeout -k 5 5 sudo fastboot reboot;
echo '';
fi;
# End of process
export USE_NINJA=;
export USE_SOONG_UI=;
rm -f "${tmpfile}";
cd "${cwd}";
echo '';
}
# === Kernel Flasher ===
function fboot()
{
# Variables
local bootimage;
# Initialize variables
bootimage=${1:-boot.img};
bootimage=${bootimage#file:/};
# Usage
if [ ! -f "${bootimage}" ]; then
echo '';
echo ' Usage: fboot <bootimage> (fastboot bootimage flashed)';
echo '';
return;
fi;
# Reboot to bootloader
sudo echo '';
if timeout -k 3 3 sh -c "$(adbcmd) devices | grep -q 'recovery$\|device$'" 2> /dev/null; then
adbro;
adbwait;
$(adbcmd) reboot bootloader;
fi;
# Flash bootimage and reboot
sudo fastboot flash boot "${bootimage}";
sudo fastboot reboot;
}
# === System Flasher ===
function fboots()
{
# Usage: fboots <system_img> (fastboot systemimage flashed)
# Variables
local adb;
local file;
# Initialize variables
adb=$(adbcmd);
# Reboot to bootloader
adbwait;
${adb} reboot bootloader;
# Flash system
if [ ! -z "${1}" ]; then
file=$(echo "${1}" | sed 's/file:\/\/\(.*\)/\1/');
sudo fastboot flash system "${file}";
else
sudo fastboot flash system system.img;
fi;
}
# === Fastboot Reboot ===
function fbootr()
{
# Usage: fbootr (Fastboot reboot)
# Fastboot reboot
sudo fastboot reboot;
}
# === Host Kernel Informations ===
function bootinfo()
{
# Usage
if [ ! -f "${1}" ]; then
echo '';
echo ' Usage: bootinfo <boot_img_file> (Bootimage structure and information parser)';
echo '';
return;
fi;
# Run bbootimg on the kernel input
"${ANDROID_DEVELOPMENT_SHELL_TOOLS_BBOOTIMG_HOST}" -i "${1}";
}
# === Kernel Informations ===
function adbbootinfo()
{
# Usage: adbbootinfo (Kernel bbootimg analyzer)
# Variables
local adb;
local adb_tmpdir;
local partitiontarget;
# Give adb root permissions
adbwait;
adbro;
# Initialize variables
androiddevicestarget init;
adb=$(adbcmd);
partitiontarget=$(androiddevicestarget boot);
# Check if '/tmp' exists
if [ ! -z "$(${adb} shell 'if [ -d /tmp ]; then echo true; fi;')" ]; then
adb_tmpdir=/tmp;
# Check if '/data/local/tmp' exists
elif [ ! -z "$(${adb} shell 'if [ -d /data/local/tmp ]; then echo true; fi;')" ]; then
adb_tmpdir=/data/local/tmp;
else
echo '';
echo ' adbbootinfo: No /tmp folder found, exiting...';
echo '';
return;
fi;
# Push and fix permissions of 'bbootimg' then kernel informations and cleanup
${adb} push "${ANDROID_DEVELOPMENT_SHELL_TOOLS_BBOOTIMG}" "${adb_tmpdir}/bbootimg";
adbsu "chmod 755 ${adb_tmpdir}/bbootimg;
${adb_tmpdir}/bbootimg -i ${partitiontarget};
rm -f ${adb_tmpdir}/bbootimg";
}
# === FOTA Informations ===
function adbfotainfo()
{
# Usage: adbfotainfo (FOTA bbootimg analyzer)
# Variables
local adb;
local adb_tmpdir;
local partitiontarget;
# Give adb root permissions
adbwait;
adbro;
# Initialize variables
androiddevicestarget init;
adb=$(adbcmd);
partitiontarget=$(androiddevicestarget fota);
# Check if '/tmp' exists
if [ ! -z "$(${adb} shell 'if [ -d /tmp ]; then echo true; fi;')" ]; then
adb_tmpdir=/tmp;
# Check if '/data/local/tmp' exists
elif [ ! -z "$(${adb} shell 'if [ -d /data/local/tmp ]; then echo true; fi;')" ]; then
adb_tmpdir=/data/local/tmp;
else
echo '';
echo ' adbfotainfo: No /tmp folder found, exiting...';
echo '';
return;
fi;
# Push, fix permissions of 'bbootimg' then get FOTA informations
${adb} push "${ANDROID_DEVELOPMENT_SHELL_TOOLS_BBOOTIMG}" "${adb_tmpdir}/bbootimg";
adbsu "chmod 755 ${adb_tmpdir}/bbootimg;
${adb_tmpdir}/bbootimg -i ${partitiontarget};
rm -f ${adb_tmpdir}/bbootimg";
}
# === Kernel Injector ===
function fbootk()
{
# Variables
local adb;
local adb_tmpdir;
local filepath=${1};
local fota=${2};
local dtimg='';
# Detect zImage file
if [ -z "${filepath}" ]; then
if [ -f '.build/arch/arm64/boot/zImage' ]; then
filepath=.build/arch/arm64/boot/zImage;
elif [ -f '.build/arch/arm/boot/zImage' ]; then
filepath=.build/arch/arm/boot/zImage;
elif [ -f 'arch/arm64/boot/zImage' ]; then
filepath=arch/arm64/boot/zImage;
elif [ -f 'arch/arm/boot/zImage' ]; then
filepath=arch/arm/boot/zImage;
fi;
fi;
# Check if file exists
filepath=$(readlink -e "${filepath}");
# Usage
if [ -z "${filepath}" ]; then
echo '';
echo ' Usage: fbootk <kernelpath> [bool_fota] (Kernel image to boot partition injector)';
echo '';
return;
fi;
# Variables
local partitiontarget;
# Initialize partition path
androiddevicestarget init;
if [ ! -z "${fota}" ]; then
partitiontarget=$(androiddevicestarget fota);
else
partitiontarget=$(androiddevicestarget boot);
fi;
# Give adb root permissions
adbwait;
adbro;
# Initialize variables
adb=$(adbcmd);
# Check if '/tmp' exists
if [ ! -z "$(${adb} shell 'if [ -d /tmp ]; then echo true; fi;')" ]; then
adb_tmpdir=/tmp;
# Check if '/data/local/tmp' exists
elif [ ! -z "$(${adb} shell 'if [ -d /data/local/tmp ]; then echo true; fi;')" ]; then
adb_tmpdir=/data/local/tmp;
else
echo '';
echo ' fbootk: No /tmp folder found, exiting...';
echo '';
return;
fi;
# Push kernel to target
${adb} push "${filepath}" "${adb_tmpdir}/tmpkernel";
# Push DT image to target (fboota extension)
if [ -f "${filepath%/kernel}/dt.img" ]; then
${adb} push "${filepath%/kernel}/dt.img" "${adb_tmpdir}/dt.img";
dtimg="-d ${adb_tmpdir}/dt.img";
fi
# Push and fix permissions of 'bbootimg'
${adb} push "${ANDROID_DEVELOPMENT_SHELL_TOOLS_BBOOTIMG}" "${adb_tmpdir}/bbootimg";
adbsu "chmod 755 ${adb_tmpdir}/bbootimg";
# Kernel informations and injection, then clenaup
adbsu "${adb_tmpdir}/bbootimg -i ${partitiontarget};
${adb_tmpdir}/bbootimg -u ${partitiontarget} -k ${adb_tmpdir}/tmpkernel ${dtimg};
rm -f ${adb_tmpdir}/bbootimg;
rm -f ${adb_tmpdir}/tmpkernel;
rm -f ${adb_tmpdir}/dt.img";
# Reboot the device
if [ -z "${fota}" ]; then
${adb} reboot;
else
${adb} reboot recovery;
fi;
echo '';
}
# === Recovery Flasher ===
function frecovery()
{
# Usage
if [ -z "${1}" ]; then
echo '';
echo ' Usage: frecovery <image> (Flash recovery with fastboot)';
echo '';
return;
fi;
# Variables
local adb;
# Initialize variables
adb=$(adbcmd);
# Reboot to bootloader
adbwait;
${adb} reboot bootloader;
# Flash recovery with fastboot
if [ ! -z "${1}" ]; then
sudo fastboot flash recovery "${1}";
else
sudo fastboot flash recovery boot.img;
fi;
sudo fastboot reboot;
}
# === ADB Bootimage Pusher ===
function adbbootpush()
{
# Usage
if [ -z "${1}" ]; then
echo '';
echo ' Usage: adbbootpush <image> (Inject bootimage to partition)';
echo '';
return;
fi;
# Variables
local adb;
local boot;
# Acquire root access if needed
adbro;
# Initialize variables
androiddevicestarget init;
adb=$(adbcmd);
boot=$(androiddevicestarget boot);
# Installer
${adb} push "${1}" /sdcard/boot.img;
adbsu "dd if=/dev/zero of=${boot};
dd if=/sdcard/boot.img of=${boot};
rm -f /sdcard/boot.img" \
| grep -v 'No space left on device';
}
# === ADB FOTA Pusher ===
function adbfotapush()
{
# Usage
if [ -z "${1}" ]; then
echo '';
echo ' Usage: adbfotapush <image> (Inject FOTA to partition)';
echo '';
return;
fi;
# Variables
local adb;
local fota;
# Initialize variables
androiddevicestarget init;
adb=$(adbcmd);
fota=$(androiddevicestarget fota);
# Installer
${adb} push "${1}" /sdcard/fota.img;
adbsu "dd if=/dev/zero of=${fota};
dd if=/sdcard/fota.img of=${fota};
rm -f /sdcard/fota.img" \
| grep -v 'No space left on device';
}
# === ADB Recovery Pusher ===
function adbrecoverypush()
{
# Usage
if [ -z "${1}" ]; then
echo '';
echo ' Usage: adbrecoverypush <image> (Inject recovery to partition)';
echo '';
return;
fi;
# Variables
local adb;
local recovery;
# Initialize variables
androiddevicestarget init;
adb=$(adbcmd);
recovery=$(androiddevicestarget recovery);
# Installer
${adb} push "${1}" /sdcard/recovery.img;
adbsu "dd if=/dev/zero of=${recovery};
dd if=/sdcard/recovery.img of=${recovery};
rm -f /sdcard/recovery.img" \
| grep -v 'No space left on device';
}
# === Kernel Injector Zip ===
function kernelinjectorzip()
{
# Usage
if [ -z "${2}" ] || [ ! -f "${2}" ]; then
echo '';
echo ' Usage: kernelinjectorzip <device> <kernel_file_path> [kernel_sources_for_modules] (Kernel to injector zip packager)';
echo '';
return;
fi;
# Variables
local PhoneName=${1};
local BootPartition;
local CurDir;
local KernelFile;
local KernelSources;
local OutFile;
local SystemPartition;
local TargetFile;
local TmpDir;
local tmp_list;
# Initialize variables
CurDir=$(pwd);
KernelFile=$(readlink -f "${2}");
KernelSources=$(readlink -f "${3}");
OutFile=kernel-injector-${PhoneName}-$(date +'%Y%m%d');
TargetFile="$(desktoppath)/${OutFile}";
TmpDir=$(mktemp -d);
# Partitions
androiddevicestarget init;
BootPartition=$(androiddevicestarget boot "${PhoneName}");
SystemPartition=$(androiddevicestarget system "${PhoneName}");
# Generate zip and updater-script files
mkdir -p "${TmpDir}/META-INF/com/google/android/";
mkdir -p "${TmpDir}/scripts/";
# Copy base zip and 'kernel'
cp "${ANDROID_DEVELOPMENT_SHELL_TOOLS_FLASHABLE_ANDROID_BASE}" "${TmpDir}/${OutFile}.unsigned.zip";
cp "${KernelFile}" "${TmpDir}/scripts/kernel";
# Write device specific changes
cd "${TmpDir}/";
cp "${ANDROID_DEVELOPMENT_SHELL_TOOLS_UPDATER_KERNEL_INJECTOR}" './META-INF/com/google/android/updater-script';
sed -i "s/ANDROID_SYSTEM_PARTITION/${SystemPartition//\//\\\/}/" './META-INF/com/google/android/updater-script';
cp -fv "${ANDROID_DEVELOPMENT_SHELL_TOOLS_SCRIPTS_KERNEL_INJECTOR}" './scripts/';
cp -fv "${ANDROID_DEVELOPMENT_SHELL_TOOLS_SCRIPTS_KERNEL_INJECTOR_SH}" './scripts/';
sed -i "s/ANDROID_BOOT_PARTITION/${BootPartition//\//\\\/}/" './scripts/kernel_inject.sh';
# Update 'updater-script', 'scripts' and 'kernel'
zip -g "./${OutFile}.unsigned.zip" './META-INF/com/google/android/updater-script';
zip -g "./${OutFile}.unsigned.zip" './scripts/'*;
zip -g "./${OutFile}.unsigned.zip" './scripts/kernel';
# Find and pack modules
if [ ! -z "${KernelSources}" ]; then
mkdir -p "./system/lib/modules/";
tmp_list=$(mktemp);
find "${KernelSources}" -name '*.ko' > "${tmp_list}";
while IFS= read -r -d '' module_file; do
cp "${module_file}" "./system/lib/modules/";
done < "${tmp_list}";
rm -f "${tmp_list}";
zip -g "./${OutFile}.unsigned.zip" "./system/lib/modules/"*;
fi;
# Copy zip unsigned file to desktop
cp "./${OutFile}.unsigned.zip" "${TargetFile}.unsigned.zip";
# Zip signature
signzip "./${OutFile}.unsigned.zip" "${TargetFile}.zip";
# Check if zip got signed
if [ -f "${TargetFile}.zip" ]; then
rm "${TargetFile}.unsigned.zip";
TargetFile=${TargetFile}.zip;
# Fallback to unsigned zip
else
echo '';
echo ' kernelinjectorzip: The zip signing failed. Falling back to the unsigned zip';
TargetFile=${TargetFile}.unsigned.zip;
fi;
# End of work
rm -rf "${TmpDir}";
cd "${CurDir}";
# Result
echo '';
echo -e " \e[1;37mkernelinjectorzip: Package File:\e[0m ${TargetFile}";
echo '';
}
# === Boot Zip Packer ===
function bootzip()
{
# Usage
if [ -z "${2}" ] || [ ! -f "${2}" ]; then
echo '';
echo ' Usage: bootzip <device> <boot_img_path> [bool_push_to_device] (Bootimage to zip packager)';
echo '';
return;
fi;
# Variables
local PhoneName=${1};
local PushToDevice=${3};
local adb;
local adb_targetdir;
local BootPartition;
local CurDir;
local BootFile;
local OutFile;
local TargetFile;
local TmpDir;
# Initialize variables
adb=$(adbcmd);
BootFile=$(readlink -f "${2}");
CurDir=$(pwd);
OutFile=boot-${PhoneName}-$(date +'%Y%m%d');
TargetFile="$(desktoppath)/${OutFile}";
TmpDir=$(mktemp -d);
# Partitions
androiddevicestarget init;
BootPartition=$(androiddevicestarget boot "${PhoneName}");
# Copy 'boot.img' and base zip
cp "${BootFile}" "${TmpDir}/boot.img";
cp "${ANDROID_DEVELOPMENT_SHELL_TOOLS_FLASHABLE_ANDROID_BASE}" "${TmpDir}/${OutFile}.unsigned.zip";
# Write device specific changes
cd "${TmpDir}/";
mkdir -p './META-INF/com/google/android/';
cp "${ANDROID_DEVELOPMENT_SHELL_TOOLS_UPDATER_FLASH_BOOT}" './META-INF/com/google/android/updater-script';
sed -i "s/ANDROID_BOOT_PARTITION/${BootPartition//\//\\\/}/" './META-INF/com/google/android/updater-script';
# Update 'updater-script' and 'boot.img'
zip -g "./${OutFile}.unsigned.zip" './META-INF/com/google/android/updater-script';
zip -g "./${OutFile}.unsigned.zip" './boot.img';
# Copy zip unsigned file to desktop
cp "./${OutFile}.unsigned.zip" "${TargetFile}.unsigned.zip";
# Zip signature
signzip "./${OutFile}.unsigned.zip" "${TargetFile}.zip";
# Check if zip got signed
if [ -f "${TargetFile}.zip" ]; then
rm "${TargetFile}.unsigned.zip";
OutFile=${OutFile}.zip;
TargetFile=${TargetFile}.zip;
# Fallback to unsigned zip
else
echo '';
echo ' bootzip: The zip signing failed. Falling back to the unsigned zip';
echo '';
OutFile=${OutFile}.unsigned.zip;
TargetFile=${TargetFile}.unsigned.zip;
fi;
# Push zip to the device
if [ "${PushToDevice}" = 'true' ]; then
# Wait for device
echo '';
adbwait;
# Check if '/sdcard' exists
if [ ! -z "$(${adb} shell 'ls /sdcard/ 2> /dev/null')" ]; then
adb_targetdir=/sdcard;
# Check if '/data/media/0' exists
elif [ ! -z "$(${adb} shell 'ls /data/media/0/ 2> /dev/null')" ]; then
adb_targetdir=/data/media/0;
fi;
# Push zip to device target
if [ -z "${adb_targetdir}" ]; then
echo ' bootzip: Failed pushing to device';
else
echo " bootzip: Pushing to device ${adb_targetdir}/${OutFile}";
${adb} push "${TargetFile}" "${adb_targetdir}/";
fi;
fi;
# End of work
rm -rf "${TmpDir}";
cd "${CurDir}";
# Result
echo '';
echo -e " \e[1;36mbootzip: Package File:\e[0m ${TargetFile}";
export PACKAGE_RESULT=${TargetFile};
echo '';
}
# === Make Sepolicy ===
function makesep()
{
# Usage: makesep [bool_inject] (Compile sepolicies clean)
# Variables
local cwd;
local device;
local out_device_path;
# Access repo root
cwd=$(pwd);
croot;
# Prepare optional sepolicies injection
if [ ! -z "${1}" ]; then
adbro;
fi;
# Initialize variables
device=$(repogetdevice);
out_device_path=./out/target/product/${device};
# Cleanup ramdisk files
outsepdevcl "${device}";
# Disable build environment
export USE_NINJA=false;
export USE_SOONG_UI=false;
# Start sepolicies full compilations
if [ -d ./system/sepolicy ]; then
mmm -j"$(grep -c ^processor /proc/cpuinfo)" ./system/sepolicy/;
else
mmm -j"$(grep -c ^processor /proc/cpuinfo)" ./external/sepolicy/;
fi;
# Optionally inject sepolicies to device
if [ ! -z "${1}" ]; then
sepinject "${out_device_path}/root";
fi;
# Restore build environment and path
export USE_NINJA=;
export USE_SOONG_UI=;
cd "${cwd}/";
}
# === Sepolicies Injector ===
function sepinject()
{
# Variables
local device;
local root_path=${1};
# Get device name
device=$(repogetdevice);
# Detect root files
if [ -z "${root_path}" ]; then
if [ -f "./out/target/product/${device}/root/sepolicy" ]; then
root_path=./out/target/product/${device}/root;
fi;
fi;
# Check if files exists
root_path=$(readlink -e "${root_path}");
# Usage
if [ -z "${root_path}" ] || [ ! -f "${root_path}/sepolicy" ]; then
echo '';
echo ' Usage: sepinject <root_path> (Sepolicies files to boot partition injector)';
echo '';
return;
fi;
# Injector ramdisk files
adbramdiskinject "${root_path}/file_contexts"* "${root_path}/"*'_contexts' "${root_path}/sepolicy";
}
# === Ramdisk Files Injector ===
function adbramdiskinject()
{
# Variables
local adb_cmd;
local adb_tmpdir;
local file_name;
local file_path;
local files_paths=("${@}");
# Usage
if [ -z "${files_paths[*]}" ]; then
echo '';
echo ' Usage: adbramdiskinject <files_paths> (Ramdisk files to boot partition injector)';
echo '';
return;
fi;
# Variables
local partitiontarget;
# Get partition path
androiddevicestarget init;
partitiontarget=$(androiddevicestarget boot);
# Give adb root permissions
adbwait;
adbro;
# Initialize variables
adb_cmd=$(adbcmd);
# Check if '/tmp' exists
if [ ! -z "$(${adb_cmd} shell 'if [ -d /tmp ]; then echo true; fi;')" ]; then
adb_tmpdir=/tmp;
# Check if '/data/local/tmp' exists
elif [ ! -z "$(${adb_cmd} shell 'if [ -d /data/local/tmp ]; then echo true; fi;')" ]; then
adb_tmpdir=/data/local/tmp;
else
echo '';
echo ' adbramdiskinject: No /tmp folder found, exiting...';
echo '';
return;
fi;
# Prepare temporary folder
adbsu "rm -rf ${adb_tmpdir}/inject;
mkdir -p '${adb_tmpdir}/inject'";
# Push and fix permissions of 'bbootimg'
${adb_cmd} push "${ANDROID_DEVELOPMENT_SHELL_TOOLS_BBOOTIMG}" "${adb_tmpdir}/inject/bbootimg";
adbsu "chmod 755 ${adb_tmpdir}/inject/bbootimg";
# Bootimage extraction, then unpack gzip and cpio ramdisk
adbsu "cd ${adb_tmpdir}/inject/;
${adb_tmpdir}/inject/bbootimg -x ${partitiontarget};
mv ${adb_tmpdir}/inject/initrd.img ${adb_tmpdir}/inject/initrd.cpio.gz;
gzip -d ${adb_tmpdir}/inject/initrd.cpio.gz;
mkdir -p ${adb_tmpdir}/inject/ramdisk;
cd ${adb_tmpdir}/inject/ramdisk/;
cpio -i -F ../initrd.cpio;
rm -f ../initrd.cpio";
# Push files to target
echo '';
for file_path in "${files_paths[@]}"; do
file_name=$(basename "${file_path}");
adbpushfile "${file_path}" "${adb_tmpdir}/inject/ramdisk/${file_name}";
done;
# Repack cpio and gzip ramdisk, then bootimage information and injection
adbsu "cd ${adb_tmpdir}/inject/ramdisk/;
find . | cpio -o -H newc -F ../initrd.cpio;
gzip -9 ${adb_tmpdir}/inject/initrd.cpio;
mv ${adb_tmpdir}/inject/initrd.cpio.gz ${adb_tmpdir}/inject/initrd.img;
${adb_tmpdir}/inject/bbootimg -i ${partitiontarget};
${adb_tmpdir}/inject/bbootimg -u ${partitiontarget} -r ${adb_tmpdir}/inject/initrd.img;
rm -rf ${adb_tmpdir}/inject";
# Reboot the device
${adb_cmd} reboot;
echo '';
}
# === Bootimage Debuggable ===
function bootimagedebuggable()
{
# Usage
if [ -z "${2}" ]; then
echo '';
echo ' Usage: bootimagedebuggable <device_product> <true/false> (Bootimage build unsecured patcher)';
echo '';
return;
fi;
# Variables
local cwd;
local file_path;
# Access repo root
cwd=$(pwd);
croot;
# Apply unsecure changes
if [ "${2}" = 'true' ]; then
# Apply changes build/core/main.mk
file_path=./build/core/main.mk;
echo " bootimagedebuggable: Apply debug unsecure in ${file_path}";
sed -i 's/ADDITIONAL_DEFAULT_PROPERTIES += ro.secure=1/ADDITIONAL_DEFAULT_PROPERTIES += ro.secure=0/' "${file_path}";
sed -i 's/ro.adb.secure=1/ro.adb.secure=0/' "${file_path}";
# Apply changes build/tools/post_process_props.py
file_path=./build/tools/post_process_props.py;
echo " bootimagedebuggable: Apply debug unsecure in ${file_path}";
sed -i 's/val = "adb"/val = "mtp,adb"/' "${file_path}";
sed -i 's/val = val + ",adb"/val = val + ",mtp,adb"/' "${file_path}";
sed -i 's/"persist.sys.usb.config", "none"/"persist.sys.usb.config", "mtp,adb"/' "${file_path}";
# Apply changes vendor/{cm|lineage}/config/common.mk
for file_path in \
./vendor/cm/config/common.mk \