forked from anht3889/kernel_lge_sm8150_new
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tuxera_update.sh
1437 lines (1167 loc) · 46.2 KB
/
tuxera_update.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
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
# vim: ft=bash sw=4 sts=4 et
# ================= NO CHANGE NEEDED BELOW! =====================
usage() {
usagestr=$(cat <<EOF
Usage: tuxera_update.sh [OPTION...]
This script only assembles the kernel headers package
(kheaders.tar.bz2) by default. Use -a to invoke Autobuild.
--output-dir OUTDIR
Specify kernel build output directory.
--no-excludes
Do not exclude *.c, *.o, *.S, arch/*/boot. Only use if the build
fails (some of the excluded files are needed). This significantly
grows the headers package size.
--user USER [--pass PASSWD] [--admin ADMIN]
If remote connectivity is needed (-a or --latest), username and
password are required. If missing, they will be read from stdin.
NOTE: Using --pass can be dangerous as local users see it in 'ps'.
The --admin option lets you log in using the password of the given
ADMIN user.
--use-cache [--cache-dir CACHEDIR] [--latest] [--max-cache-entries N]
Obtain modules from local cache if kernel dependencies are not
modified. You must provide --output-dir to
use the cache. Modules are relinked on cache hit, ensure the
toolchain is in PATH. If -a is specified, Autobuild is invoked
on a cache miss, and the cache is updated. Otherwise, kernel headers
assembly is performed for later, manual build.
You can specify a different cache directory with --cache-dir. The
default directory is \$PWD/.tuxera_update_cache
The --latest option will ensure that the release found in cache is
the latest version available on Autobuild servers. This needs remote
connectivity. If a new version is found and -a is specified,
Autobuild is invoked and the cache is updated.
The optional --max-cache-entries can be used to limit the maximum
cache size to N recently used entries. The default is 10.
-a [--target TARGET] [OPTIONS]
Start Autobuild against TARGET. If target was not specified, uses
the default target. Use '--target list' to show available targets.
The following extra options are supported:
--use-package PACKAGE
Autobuild starts with fresh .tar.bz2 assembly by default, but
you can use this option to start Autobuild by uploading a pre-built
.tar.bz2 file PACKAGE.
--ignore-cert
If up/download fails due to certificate issues, this option can
be used to disable verification.
--use-curl
--use-wget
Force the use of 'curl' or 'wget' for remote communication.
-u, --upgrade
Upgrade online to the latest script version, obtained from
$upgrade_url
-h, --help
Prints this help.
-v
Only print the version of this script and exit.
--verbose
Print more debug information.
Tuxera Autobuild Team
autobuild-support@tuxera.com
EOF
)
if [ -n "$long_help" ] ; then
echo "$usagestr"
echo
else
echo "For long help, use -h. Options:"
echo "$usagestr" | grep "^ *-"
fi
exit 1
}
archs="alpha arc arm arm64 avr32 blackfin c6x cris frv h8300 hexagon ia64 m32r \
m68k metag microblaze mips mn10300 openrisc parisc ppc s390 score \
superh sparc tile unicore32 x86 xtensa ubicom32 csky"
#
# Detect required architecture directories based on autoconf.h
#
detect_arch_dirs() {
# Simplify conditionals, if output_dir not set
dir1="$source_dir"
[ -n "$output_dir" ] && dir2="$output_dir" || dir2="$dir1"
# autoconf.h must be present
ac="${dir2}/include/generated/autoconf.h"
[ -e "$ac" ] || ac="${dir2}/include/linux/autoconf.h"
[ -e "$ac" ] || ac="${dir1}/include/generated/autoconf.h"
[ -e "$ac" ] || ac="${dir1}/include/linux/autoconf.h"
[ -e "$ac" ] || { echo "ERROR: Unable to detect kernel architecture: autoconf.h not found."; return 1; }
pattern=""
# Compute grep pattern
for arch in $archs; do
[ -n "$pattern" ] && separ="\|" || separ=""
pattern="${pattern}${separ}^#define CONFIG_$(echo $arch | tr '[:lower:]' '[:upper:]')[[:space:]]"
done
# Find architecture from autoconf.h
def=$(grep -e "$pattern" "$ac") || { echo "ERROR: Unable to detect kernel architecture: no macro found in autoconf.h."; return 1; }
archdirs=$(echo "$def" | head -n 1 | sed -n 's/#define CONFIG_\(.*\)[[:space:]].*/\1/p' | tr '[:upper:]' '[:lower:]')
# Fix some architecture names to get the correct directory
[ "$archdirs" == "ppc" ] && archdirs="powerpc"
[ "$archdirs" == "superh" ] && archdirs="sh"
if [ "$archdirs" == "x86" ] ; then
# Older kernels don't have x86 dir at all.
# Some x86 builds require 2 directories. This is very rare.
grep -q '^#define CONFIG_X86_32' "$ac" && archdirs="$archdirs i386"
grep -q '^#define CONFIG_X86_64' "$ac" && archdirs="$archdirs x86_64"
fi
if [ "$archdirs" == "arm64" ] ; then
# Nvidia Tegra 3.10.61
grep -q '^#define CONFIG_ARCH_TEGRA' "$ac" && archdirs="$archdirs arm"
fi
# Realtek RLX fix
[ "$archdirs" == "mips" ] && grep -q '^#define CONFIG_CPU_RLX' "$ac" && archdirs="rlx"
# Realtek mips-ori fix
[ "$archdirs" == "mips" ] && grep -q 'Linux/mips-ori' "$ac" && archdirs="$archdirs mips-ori"
# Only select existing directories.
existing=''
for d in $archdirs ; do
[ -e "$dir1/arch/$d" ] || [ -e "$dir2/arch/$d" ] && existing="$existing $d"
done
# Don't continue if there is no arch directory.
[ -z "$existing" ] && { echo "ERROR: No arch directories found. Cannot continue."; return 1; }
archdirs="$existing"
echo "Kernel architecture directories: $archdirs"
}
#
# Check KH against typical errors
#
check_kh() {
source_link="$1"
output_link="$2"
[ -e "${output_link}" ] || output_link="${source_link}"
if ! [ -e "${source_link}/Makefile" ]; then
echo " ERROR: Kernel source code directory is invalid (no Makefile found).";
echo " To fix it, set the --output-dir parameter correctly.";
return 1
fi
if grep -q "VERSION" ${source_link}/Makefile &&
grep -q "PATCHLEVEL" ${source_link}/Makefile &&
grep -q "SUBLEVEL" ${source_link}/Makefile
then
echo "Found valid Linux kernel Makefile at ${source_link}/Makefile"
else
echo " ERROR: Kernel source code directory is invalid.";
echo " Makefile doesn't have VERSION, PATCHLEVEL and SUBLEVEL."
echo " To fix it, set the --output-dir parameter correctly.";
return 1
fi
if ! [ -e "${output_link}/Module.symvers" ]; then
echo " ERROR: Invalid kernel configuration:";
echo " Module.symvers is missing.";
echo " Either the kernel was not compiled yet, or the kernel output directory is not correct.";
echo " Please make sure the kernel has been compiled, then use that directory for the --output-dir";
echo " argument where the Module.symvers file can be found."
return 1
fi
[ -n "${disable_kernel_headers_check}" ] && return 0
error_config_modules=" ERROR: Update kernel configuration:
Please enable loadable modules support in kernel: CONFIG_MODULES=y
(Enable loadable module support (CONFIG_MODULES)[Y/n/?] Y)
Then rebuild the kernel and recollect kernel headers.
"
error_config_cma=" ERROR: Fix kernel source code:
CONFIG_CMA is enabled but 'is_cma_pageblock' is not exported!
Please add EXPORT_SYMBOL(is_cma_pageblock) after the definition of this
function (usually in mm/page_alloc.c).
Then rebuild the kernel and recollect kernel headers.
"
error_config_lockdep=" ERROR: Update kernel configuration:
CONFIG_DEBUG_LOCK_ALLOC=y and/or CONFIG_LOCKDEP=y are enabled.
These options convert some symbols required by file systems into
GPL-only symbols, which legally cannot be used by our filesystem driver(s).
Please disable these options, rebuild the kernel and recollect kernel headers.
"
if ! [ -s "${output_link}/Module.symvers" ] &&
! grep -sxq 'CONFIG_MODULES=y' "${output_link}/.config"
then
errors+="${error_config_modules}"
config_modules_not_set="yes"
fi
if grep -sxq 'CONFIG_CMA=y' "${output_link}/.config" &&
grep -sq 'is_cma_pageblock' "${source_link}/include/linux/pagemap.h"
then
if [ -n "${config_modules_not_set}" ]; then
errors+="${error_config_cma}"
else
grep -wq 'is_cma_pageblock' "${output_link}/Module.symvers" || errors+="${error_config_cma}"
fi
fi
if grep -qx -e 'CONFIG_DEBUG_LOCK_ALLOC=y' -e 'CONFIG_LOCKDEP=y' "${output_link}/.config"
then
if [ -n "${config_modules_not_set}" ]; then
errors+="${error_config_lockdep}"
else
awk '$4 == "EXPORT_SYMBOL_GPL" && $2 ~ /(mutex_lock_nested|lockdep_init_map|debug_check_no_locks_held)/ { exit(1); }' \
"${output_link}/Module.symvers" ||
errors+="${error_config_lockdep}"
fi
fi
[ -n "${errors}" ] && { printf "${errors}"; return 1; }
return 0
}
#
# Assemble kernel headers package.
#
build_package() {
if [ -z "$source_dir" ] ; then
echo "You must specify --output-dir for headers package assembly."
usage
fi
# Temporary directory that will contain links to kernel and output directories.
LINK_DIR=$(mktemp -d)
if [ $? -ne 0 ] ; then
echo "mktemp failed. Unable to continue."
exit 1
fi
KERNEL_LINK="${LINK_DIR}"/kernel
OUTPUT_LINK="${LINK_DIR}"/output
MEDIATEK_LINK="${LINK_DIR}"/mediatek
MSTAR2_LINK="${LINK_DIR}"/mstar2
EXTRA_SEARCH_NEW_MEDIATEK="${KERNEL_LINK}"/drivers/misc/mediatek/mach
EXTRA_SEARCH_NEW_MEDIATEK_INCLUDE="${KERNEL_LINK}"/drivers/misc/mediatek/include
EXTRA_SEARCH_NEW_MEDIATEK_BASE="${KERNEL_LINK}"/drivers/misc/mediatek/base
EXTRA_SEARCH_AMLOGIC_DEBUG="${KERNEL_LINK}"/drivers/amlogic/debug
[ -d "$source_dir"/../mediatek ] && have_mediatek=1
[ -d "$(readlink -f "$source_dir"/../mstar2)" ] && have_mstar2=1
[ -d "$(readlink -f "$source_dir"/mstar2)" ] && have_mstar2_inkernel=1
# Create kernel and output links to LINK_DIR. Only create link to output
# dir if output directory was specified. Note that absolute paths
# must be used!
ln -sf "$(readlink -f "$source_dir")" "${KERNEL_LINK}" && \
if [ -n "$output_dir" ]; then ln -sf "$(readlink -f "$output_dir")" "${OUTPUT_LINK}"; fi && \
if [ -n "$have_mediatek" ] ; then ln -sf "$(readlink -f "$source_dir")"/../mediatek "${MEDIATEK_LINK}"; fi
if [ $? -ne 0 ] ; then
echo "Symlinking (ln -s) failed. Unable to continue."
rm -rf "${LINK_DIR}"
exit 1
fi
if [ -e "${OUTPUT_LINK}" ] ; then dot_config="${OUTPUT_LINK}/.config"; else dot_config="${KERNEL_LINK}/.config"; fi
if [ -n "$have_mstar2" ] ; then
ln -sf "$(readlink -f "$source_dir")"/../mstar2 "${MSTAR2_LINK}"
if [ -n "$have_mstar2_inkernel" ] ; then
EXTRA_SEARCH_MSTAR2_INKERNEL="${KERNEL_LINK}"/mstar2/hal
fi
elif [ -n "$have_mstar2_inkernel" ] ; then
ln -sf "$(readlink -f "$source_dir")"/mstar2 "${MSTAR2_LINK}"
fi
if [ -d "$(readlink -f "${MSTAR2_LINK}")" ] && grep -q 'CONFIG_MSTAR_CHIP' "$dot_config"; then
MSTAR2_CHIP_NAME=$(awk -F= '/^CONFIG_MSTAR_CHIP_NAME=/{gsub(/[[:space:],\"]/,"",$2); print $2}' "$dot_config")
[ ! -d "${MSTAR2_LINK}/hal/${MSTAR2_CHIP_NAME}" ] && MSTAR2_CHIP_NAME=""
fi
if [ "$source_dir" = "$output_dir" ] ; then
rm "$OUTPUT_LINK"
fi
if ! check_kh "${KERNEL_LINK}" "${OUTPUT_LINK}"; then
rm -rf "${LINK_DIR}"
exit 1
fi
echo "Generating list of files to include..."
INCLUDEFILE=$(mktemp)
echo "${script_version}" >> "${LINK_DIR}"/.version
echo "${LINK_DIR}"/.version >> "${INCLUDEFILE}"
if [ $? -ne 0 ] ; then
echo "mktemp failed. Unable to continue."
rm -rf "${LINK_DIR}"
exit 1
fi
if ! detect_arch_dirs; then
rm -rf "${LINK_DIR}"
exit 1
fi
SEARCHPATHS="$(for d in $archdirs; do echo "${KERNEL_LINK}/arch/$d"; done) ${KERNEL_LINK}/include ${KERNEL_LINK}/scripts ${KERNEL_LINK}/mediatek/Makefile ${KERNEL_LINK}/mediatek/platform ${MEDIATEK_LINK}/config ${MEDIATEK_LINK}/platform ${MEDIATEK_LINK}/build/libs ${MEDIATEK_LINK}/build/Makefile ${MEDIATEK_LINK}/build/kernel ${MEDIATEK_LINK}/kernel/include/linux ${MEDIATEK_LINK}/kernel/Makefile ${MSTAR2_LINK}/hal/${MSTAR2_CHIP_NAME} ${EXTRA_SEARCH_MSTAR2_INKERNEL} ${KERNEL_LINK}/KMC ${KERNEL_LINK}/init/secureboot ${KERNEL_LINK}/mvl-avb-version ${EXTRA_SEARCH_NEW_MEDIATEK} ${EXTRA_SEARCH_NEW_MEDIATEK_INCLUDE} ${EXTRA_SEARCH_NEW_MEDIATEK_BASE} ${EXTRA_SEARCH_AMLOGIC_DEBUG} ${KERNEL_LINK}/tools/gcc ${KERNEL_LINK}/tools/Makefile ${KERNEL_LINK}/tools/objtool ${KERNEL_LINK}/tools/scripts"
if [ -L "${OUTPUT_LINK}" ] ; then
SEARCHPATHS="${SEARCHPATHS} $(for d in $archdirs; do echo "${OUTPUT_LINK}/arch/$d"; done) ${OUTPUT_LINK}/include ${OUTPUT_LINK}/scripts ${OUTPUT_LINK}/KMC ${OUTPUT_LINK}/init/secureboot ${OUTPUT_LINK}/mvl-avb-version ${OUTPUT_LINK}/tools/gcc ${OUTPUT_LINK}/tools/Makefile ${OUTPUT_LINK}/tools/objtool ${OUTPUT_LINK}/tools/scripts"
fi
# Not all of the directories always exist, so check for them first to avoid an error message
# from 'find' if the directory is not found.
for P in ${SEARCHPATHS}; do
if [ ! -e "${P}" ]; then continue; fi
if [ -n "$no_excludes" ] ; then
find -L "${P}" ! -type l >> "${INCLUDEFILE}"
else
find -L "${P}" \
\( ! -type l -a ! -name \*.c -a ! -name \*.o -a ! -name \*.S -a ! -name \*.ko -a ! -name \*.dts -a ! -name \*.dtsi -a ! -path \*/scripts/dtc\* -a ! -path \*/arch/\*/boot/\* -a ! -path \*/.svn/\* -a ! -path \*/.git/\* ! -path \*mediatek/platform/Android.mk ! -path \*mediatek/platform/README ! -path \*mediatek/platform/common\* ! -path \*mediatek/platform/rules.mk ! -path \*mediatek/platform/\*/Trace\* ! -path \*mediatek/platform/\*/external\* ! -path \*mediatek/platform/\*/hardware\* ! -path \*mediatek/platform/\*/lk\* ! -path \*mediatek/platform/\*/preloader\* ! -path \*mediatek/platform/\*/uboot\* ! -path \*mediatek/platform/\*/kernel/core/Makefile ! -path \*mediatek/platform/\*/kernel/core/Makefile.boot ! -path \*mediatek/platform/\*/kernel/core/modules.builtin ! -path \*mediatek/platform/\*/kernel/drivers\* ! -path \*mediatek/platform/\*/kernel/Kconfig\* ! -path \*mediatek/config/a830\* ! -path \*mediatek/config/banyan_addon\* ! -path \*mediatek/config/out\* ! -path \*mediatek/config/prada\* ! -path \*mediatek/config/s820\* ! -path \*mediatek/config/seine\* \) \
>> ${INCLUDEFILE}
fi
done
echo ${KERNEL_LINK}/Makefile >> ${INCLUDEFILE}
if [ -e "${OUTPUT_LINK}/Makefile" ]; then echo "${OUTPUT_LINK}/Makefile" >> ${INCLUDEFILE}; fi
if [ -e "${KERNEL_LINK}/Module.symvers" ]; then echo "${KERNEL_LINK}/Module.symvers" >> ${INCLUDEFILE}; fi
if [ -e "${OUTPUT_LINK}/Module.symvers" ]; then echo "${OUTPUT_LINK}/Module.symvers" >> ${INCLUDEFILE}; fi
if [ -e "${KERNEL_LINK}/.config" ]; then echo "${KERNEL_LINK}/.config" >> ${INCLUDEFILE}; fi
if [ -e "${OUTPUT_LINK}/.config" ]; then echo "${OUTPUT_LINK}/.config" >> ${INCLUDEFILE}; fi
if [ -e "${OUTPUT_LINK}/arch/powerpc/lib/crtsavres.o" ]; then echo "${OUTPUT_LINK}/arch/powerpc/lib/crtsavres.o" >> ${INCLUDEFILE}; fi
if [ -e "${KERNEL_LINK}/arch/powerpc/lib/crtsavres.o" ]; then echo "${KERNEL_LINK}/arch/powerpc/lib/crtsavres.o" >> ${INCLUDEFILE}; fi
if [ -e "${OUTPUT_LINK}/scripts/recordmcount.c" ]; then echo "${OUTPUT_LINK}/scripts/recordmcount.c" >> ${INCLUDEFILE}; fi
if [ -e "${KERNEL_LINK}/scripts/recordmcount.c" ]; then echo "${KERNEL_LINK}/scripts/recordmcount.c" >> ${INCLUDEFILE}; fi
if [ -e "${OUTPUT_LINK}/scripts/recordmcount.h" ]; then echo "${OUTPUT_LINK}/scripts/recordmcount.h" >> ${INCLUDEFILE}; fi
if [ -e "${KERNEL_LINK}/scripts/recordmcount.h" ]; then echo "${KERNEL_LINK}/scripts/recordmcount.h" >> ${INCLUDEFILE}; fi
# Commit 338d4f49d6f7114a017d294ccf7374df4f998edc requires arm64 some kernel headers from arm
case "$archdirs" in *arm64*)
arm64_need_arm_files="arch/arm/include/asm/opcodes.h arch/arm/include/asm/xen/hypervisor.h arch/arm/include/asm/xen/interface.h include/asm/xen/interface.h"
for EF in ${arm64_need_arm_files}; do
if [ -e "${OUTPUT_LINK}/${EF}" ]; then echo "${OUTPUT_LINK}/${EF}" >> ${INCLUDEFILE}; fi
if [ -e "${KERNEL_LINK}/${EF}" ]; then echo "${KERNEL_LINK}/${EF}" >> ${INCLUDEFILE}; fi
done
;;
esac
echo "Packing kernel headers ..."
tar cjf "${1}" --dereference --no-recursion --files-from "${INCLUDEFILE}"
if [ $? -ne 0 ] ; then
echo "tar packaging failed. I will now exit."
rm -rf "${LINK_DIR}"
exit 1
fi
rm ${INCLUDEFILE}
rm -rf "${LINK_DIR}"
echo "Headers package assembly succeeded. You could now use --use-package ${1}."
}
#
# Retry network operations
#
do_retry() {
local result
local status
local delay=$conn_fail_retry_delay
local retr=1
while :
do
result=$("$@")
status=$?
if [ $status -eq 0 ] || [ $retr -gt $conn_fail_retry ] || [ -z "$retry_on_conn_fail" ] ; then
break
fi
echo "Trying to reconnect in "$delay" seconds..." >&2
sleep $delay
delay=$(($delay * 2))
retr=$(($retr + 1))
done
[ -n "$result" ] && echo "$result"
return $status
}
#
# Upload kernel headers package using curl.
#
upload_package_curl() {
local retr=1
local delay=$conn_fail_retry_delay
local execution_status
while :
do
execution_status=0
echo "Uploading the following package:"
ls -lh "${1}"
reply=$(do_retry $curl -F "file=@${1}" https://${server}/upload.php)
if [ $? -ne 0 ] ; then
echo "curl failed. Unable to continue. Check connectivity and username/password." >&2
execution_status=1
fi
if [ $execution_status -eq 0 ] ; then
status=$(echo "$reply" | head -n 1)
if [ "$status" != "OK" ] ; then
echo "$reply" >&2
echo "Upload failed. Unable to continue." >&2
execution_status=1
fi
fi
if [ $execution_status -ne 0 ] ; then
if [ $retr -gt $conn_fail_retry ] || [ -z "$retry_on_conn_fail" ] ; then
exit 1
else
echo "Will try one more time $retr of $conn_fail_retry in "$delay" seconds..." >&2
sleep $delay
retr=$(($retr + 1))
delay=$(($delay * 2))
continue
fi
fi
remote_package=$(echo "$reply" | head -n 2 | tail -n 1)
echo "Upload succeeded."
break
done
}
#
# Upload kernel headers package using wget.
# Package needs to be urlencoded.
#
upload_package_wget() {
encoded="$(mktemp)"
echo "urlencoding $1 to $encoded ..."
echo -n "file=" > "$encoded"
perl -pe 's/([^-_.~A-Za-z0-9])/sprintf("%%%02X", ord($1))/seg' < "$1" >> "$encoded"
if [ $? -ne 0 ] ; then
echo "urlencoding failed. Unable to continue."
exit 1
fi
local retr=1
local delay=$conn_fail_retry_delay
local execution_status
while :
do
execution_status=0
echo "Uploading package..."
reply=$(do_retry $wget --post-file="$encoded" -O - https://${server}/upload.php)
if [ $? -ne 0 ] ; then
echo "wget failed. Unable to continue. Check connectivity and username/password." >&2
execution_status=1
fi
if [ $execution_status -eq 0 ] ; then
status=$(echo "$reply" | head -n 1)
if [ "$status" != "OK" ] ; then
echo "$reply"
echo "Upload failed. Unable to continue." >&2
execution_status=1
fi
fi
if [ $execution_status -ne 0 ] ; then
if [ $retr -gt $conn_fail_retry ] || [ -z "$retry_on_conn_fail" ] ; then
rm "$encoded"
exit 1
else
echo "Will try one more time $retr of $conn_fail_retry in "$delay" seconds..." >&2
sleep $delay
retr=$(($retr + 1))
delay=$(($delay * 2))
continue
fi
fi
remote_package=$(echo "$reply" | head -n 2 | tail -n 1)
rm "$encoded"
echo "Upload succeeded."
break
done
}
#
# Find headers that were used during dependency module compilation from
# the *.i files. Compute their checksums.
#
calc_header_checksums() {
# Holds initial list of header files.
DEPENDENCY=$(mktemp)
if [ $? -ne 0 ] ; then
echo "mktemp failed. Unable to continue."
return 1
fi
# Find header files from the *.i files.
egrep '^#.*".*\.h".*' "$1"/*.i | awk -F '"' '{print $2}' | sort | uniq >> $DEPENDENCY
if [ $? -ne 0 ] ; then
echo "Failed to extract dependencies."
rm -f "${DEPENDENCY}"
return 1
fi
# Make sure the destination file doesn't exist.
rm -f "$2"
dirlen=$(readlink -f "${source_dir}" | wc -c)
# Loop through all headers.
while read line; do
echo "$line" | grep ^/ > /dev/null 2>&1
# Absolute path?
if [ $? -eq 0 ] ; then
echo "$line" | grep ^"$(readlink -f "${source_dir}")" > /dev/null 2>&1
# Common Headers
if [ $? -eq 0 ]; then
path_end=$(echo $line | tail -c +$dirlen)
sum=$(md5sum "${line}" | cut -d ' ' -f 1 2>/dev/null)
if [ $? -ne 0 ] ; then
echo "Failed to get checksum for file: ${linkfile}"
echo "Unable to continue."
rm -f "${DEPENDENCY}"
return 1
fi
echo "${sum} source${path_end}" >> "$2"
fi
# Generated Headers (not absolute path)
else
sum=$(md5sum "${kernel}/${line}" | cut -d ' ' -f 1 2>/dev/null)
if [ $? -ne 0 ] ; then
echo "Failed to get checksum for file: ${kernel}/${line}"
echo "Unable to continue."
rm -f "${DEPENDENCY}"
return 1
fi
echo "${sum} generated/${line}" >> "$2"
fi
done < ${DEPENDENCY}
rm "${DEPENDENCY}"
return 0
}
#
# Build dependency module and produce header checksums
# based on the build output .i file.
#
gen_header_checksums() {
if [ -f "${cache_dir}/pkgtmp/dependency_mod/env" ] ; then
. "${cache_dir}/pkgtmp/dependency_mod/env"
else
echo "No build environment found, unable to produce header checksums."
return 1
fi
if [ -z "${KERNEL_COMPILER}" ] ; then
KERNEL_COMPILER=${CROSS_COMPILE}gcc
fi
mute env
mute command -v ${KERNEL_COMPILER}
if [ $? -ne 0 ] ; then
echo "${KERNEL_COMPILER} not found. Do you have the compiler in PATH?"
return 1
fi
if [ -z "${KERNEL_LINKER}" ] ; then
KERNEL_LINKER=${CROSS_COMPILE}ld
fi
mute command -v ${KERNEL_LINKER}
if [ $? -ne 0 ] ; then
echo "${KERNEL_LINKER} not found. Do you have the linker in PATH?"
return 1
fi
make -C "$kernel" ${CACHE_MAKE_FLAGS} M="${cache_dir}/pkgtmp/dependency_mod" depmod.i > $dbgdev
if [ $? -ne 0 ] ; then
echo "Compilation failed. Unable to compute header dependency tree."
return 1
fi
# Compute checksums from headers present in the depmod.i file.
calc_header_checksums "${cache_dir}/pkgtmp/dependency_mod" "$1"
return $?
}
#
# Match kernel symbol CRCs.
#
check_symvers() {
searchdir="$1"
symvers="$2"
searchvers=$(mktemp)
symvers_parsed=$(mktemp)
sort $(find "$searchdir" -name \*.mod.c) | uniq | \
sed -rn 's/^[[:space:]]*\{[[:space:]]*0x([[:xdigit:]]{8}),.*["(](.*)[")].*\}.*/0x\1 \2/p' > $searchvers
awk '{print $1,$2}' "$symvers" > "$symvers_parsed"
sort "$symvers_parsed" "$searchvers" | uniq -d | diff "$searchvers" - > /dev/null
ret=$?
rm -f "$symvers_parsed" "$searchvers"
return $ret
}
#
# Remove files related to given cache entry.
#
destroy_cache_entry() {
echo "Destroying cache entry: ${1}"
rm "${cache_dir}/${pkg}".{pkg,md5sum,target,pkgname}
}
#
# Match cache entries against given --source-dir/--output-dir
#
lookup_cache() {
if [ ! -f "${kernel}/Module.symvers" ] ; then
echo "${kernel}/Module.symvers does not exist. Unable to lookup cache."
return 1
fi
# All cache entries. Prioritize recently used entries.
cachefiles=$(cd "${cache_dir}"; ls -t *.pkg 2>/dev/null)
if [ $? -ne 0 ] ; then
echo "Can't find any cache files."
return 1
fi
for pkg in ${cachefiles} ; do
pkg=$(basename "$pkg" .pkg)
echo -n "Cache lookup: ${pkg}.pkg ... "
if [ ! -f "${cache_dir}/${pkg}.target" -o $(cat "${cache_dir}/${pkg}.target") != "$target" ] ; then
echo "miss (different target)"
continue
fi
if [ ! -f "${cache_dir}/${pkg}.md5sum" -o ! -f "${cache_dir}/${pkg}.pkgname" ] ; then
echo "md5sum or pkgname file missing for ${cache_dir}/${pkg}, unable to validate"
continue
fi
pkgname=$(cat "${cache_dir}/${pkg}.pkgname")
if [ -n "$check_latest" -a "$pkgname" != "$latest_pkg" ] ; then
echo "Old cache entry: new version available"
[ -z "$autobuild" ] || destroy_cache_entry "old version"
continue
fi
# Prepare to match kernel symbol CRCs and header checksums.
rm -rf "${cache_dir}/pkgtmp"
mkdir "${cache_dir}/pkgtmp"
tar xf "${cache_dir}/${pkg}.pkg" --strip-components=1 -C "${cache_dir}/pkgtmp"
# Match symbol CRCs
check_symvers "${cache_dir}/pkgtmp" "${kernel}/Module.symvers"
if [ $? -ne 0 ] ; then
echo "miss (kernel symbol CRCs differ)"
rm -rf "${cache_dir}/pkgtmp"
continue
fi
# Build dependency module and compute header checksums.
if [ -z "${CACHE_MAKE_FLAGS}" ] ; then
CACHE_MAKE_FLAGS="ARCH=$ARCH CROSS_COMPILE=$CROSS_COMPILE $CUST_KENV"
fi
tmpsums=$(mktemp)
gen_header_checksums "$tmpsums"
if [ $? -ne 0 ] ; then
echo "checksum calculation failed"
rm -rf "${cache_dir}/pkgtmp"
rm -f "$tmpsums"
continue
fi
# Match header checksums.
diff "$tmpsums" "${cache_dir}/${pkg}.md5sum" > /dev/null
if [ $? -ne 0 ] ; then
rm -rf "${cache_dir}/pkgtmp"
rm -f "$tmpsums"
echo "miss (header checksums differ)"
continue
fi
rm -f "$tmpsums"
echo "Cache hit! Relinking modules..."
# Relink all kernel modules against currently used kernel.
if [ -z "$(find "${cache_dir}/pkgtmp/" -name \*.ko)" ] ; then
require_relink="yes"
fi
mute env
mute command -v ${KERNEL_COMPILER}
mute command -v ${KERNEL_LINKER}
for driver_obj in "${cache_dir}/pkgtmp/"*/objects ; do
driver=$(dirname "$driver_obj")
if [ ! -f "${driver}/objects/Makefile.autobuild" ] ; then
echo "Old cache entry: no Makefile.autobuild found"
destroy_cache_entry "created by an old version"
rm -rf "${cache_dir}/pkgtmp"
continue 2
fi
rm -f "${driver}/objects/Kbuild"
mv "${driver}/objects/Makefile.autobuild" "${driver}/objects/Makefile"
if [ "${verbose}" = "yes" ] ; then
CACHE_MAKE_FLAGS="${CACHE_MAKE_FLAGS} V=1"
fi
echo Relinking $(basename "${driver}") ...
mute make -C "$kernel" ${CACHE_MAKE_FLAGS} M="${driver}/objects" modules
if [ $? -ne 0 ] ; then
echo "Kernel module relinking failed - this usually shouldn't happen."
echo "Refusing to use this cache entry!"
rm -rf "${cache_dir}/pkgtmp"
continue 2
fi
echo "Stripping debug information..."
for mod in "${driver}"/objects/*.ko ; do
"${CROSS_COMPILE}strip" --strip-debug "${mod}" 1>/dev/null 2>/dev/null
cp "${mod}" "${driver}/kernel-module/"
done
relinked="yes"
done
if [ -n "$require_relink" -a -z "$relinked" ] ; then
echo "Old cache entry: no objects for relinking found"
destroy_cache_entry "created by an old version"
rm -rf "${cache_dir}/pkgtmp"
continue
fi
# Produce a package like the ones from the server.
echo "Packaging ${pkgname}..."
pkgcontents=$(tar tf "${cache_dir}/${pkg}.pkg")
origname=$(echo "${pkgcontents}" | head -n 1 | awk -F '/' '{print $1}')
rm -rf "${cache_dir}/${origname}"
mv "${cache_dir}/pkgtmp" "${cache_dir}/${origname}"
tar czf "$(pwd)/${pkgname}" -C "${cache_dir}" "${origname}/"
rm -rf "${cache_dir}/${origname}"
# Update cache entry timestamp.
touch "${cache_dir}/${pkg}.pkg"
return 0
done
echo "No cache hits."
return 1
}
#
# Start remote build. Headers package must be uploaded
# first.
#
do_remote_build() {
echo "Starting remote build against target ${target}..."
# Start build
if [ "$http_client" = "wget" ] ; then
reply=$(do_retry $wget --post-data="terminal=1&filename=${remote_package}&target-config=${target}&extraargs=${extraargs}&use-cache=${using_cache}&script-version=${script_version}&cache-lookup-time=${cache_lookup_time}&suid=${suid}&start-build=1" -O - https://${server})
else
reply=$(do_retry $curl -d terminal=1 -d filename="$remote_package" -d target-config="$target" -d extraargs="$extraargs" -d use-cache="$using_cache" -d script-version="$script_version" -d cache-lookup-time="$cache_lookup_time" -d suid="$suid" -d start-build=1 https://${server})
fi
if [ $? -ne 0 ] ; then
echo "${http_client} failed. Unable to start build."
echo "Check connectivity and username/password."
exit 1
fi
status=$(echo "$reply" | head -n 1)
# If status is OK, build ID should be included as well
if [ "$status" != "OK" ] ; then
echo "Starting the build failed. Unable to continue."
echo "The server reported:"
echo "$status"
exit 1
fi
# ID of this build is given in the reply
build_id=$(echo "$reply" | head -n 2 | tail -n 1)
echo "Build started, id ${build_id}"
echo "Polling for completion every 10 seconds..."
statusurl="https://${server}/builds/${build_id}/.status"
for i in `seq $max_polls`
do
if [ "$http_client" = "wget" ] ; then
reply=$($wget_quiet -O - "$statusurl")
else
reply=$($curl_quiet "$statusurl")
fi
if [ $? -ne 0 ] ; then
if [ $i -eq $max_polls ] ; then
echo "Maximum attempts exceeded. Build process"
echo "died or hung. Please notify Tuxera if the"
echo "problem persists."
exit 1
fi
echo "Not finished yet; waiting..."
sleep $poll_delay
continue
fi
break
done
echo "Build finished."
# Downloadable filename given in the reply
status=$(echo "$reply" | head -n 1)
if [ "$status" != "OK" ] ; then
echo "Build failed. Cannot download package."
echo "Tuxera has been notified of this failure."
exit 1
fi
filename=$(echo "$reply" | head -2 | tail -1)
fileurl="https://${server}/builds/${build_id}/${filename}"
echo "Downloading ${filename} ..."
if [ "$http_client" = "wget" ] ; then
reply=$(do_retry $wget -O "$filename" "$fileurl")
else
reply=$(do_retry $curl -o "$filename" "$fileurl")
fi
if [ $? -ne 0 ] ; then
echo "Failed. You can still try to download using the link in the e-mail that was sent."
exit 1
fi
echo "Download finished."
# Create cache entry.
if [ -n "$use_cache" ] ; then
echo "Updating cache..."
pkgprefix="$(date +%Y-%m-%d-%H-%M-%S)-$(head -c 8 /dev/urandom | md5sum | head -c 4)"
cp "$filename" "${cache_dir}/${pkgprefix}.pkg"
echo "$target" > "${cache_dir}/${pkgprefix}.target"
echo "$filename" > "${cache_dir}/${pkgprefix}.pkgname"
# Prepare to compute checksums.
mkdir "${cache_dir}/pkgtmp"
tar xf "${cache_dir}/${pkgprefix}.pkg" --strip-components=1 -C "${cache_dir}/pkgtmp"
# Build dependency module and compute checksums.
gen_header_checksums "${cache_dir}/${pkgprefix}.md5sum"
if [ $? -ne 0 ] ; then
echo "Updating cache failed."
rm -rf "${cache_dir}/${pkgprefix}.pkg"
rm -f "${cache_dir}/${pkgprefix}.target"
fi
rm -rf "${cache_dir}/pkgtmp"
# Purge cache entries if there are more than max_cache_entries.
cachefiles=$(cd "${cache_dir}"; ls -t *.pkg 2>/dev/null | tail -n +$((${max_cache_entries}+1)))
for pkg in ${cachefiles} ; do
pkg=$(basename "$pkg" .pkg)
destroy_cache_entry "enforcing cache size limit"
done
fi
}
#
# Request a list of available targets on the server.
#
list_targets() {
echo "Connecting..."
if [ "$http_client" = "wget" ] ; then
reply=$(do_retry $wget --post-data="suid=${suid}" -O - https://${server}/targets.php)
else
reply=$(do_retry $curl -d suid="$suid" https://${server}/targets.php)
fi
if [ $? -ne 0 ] ; then
echo "Unable to list targets. Check connectivity and username/password."
exit 1
fi
echo
echo "Available targets for this user:"
echo "$reply"
echo
}
#
# Request the latest package name from the server, for a given
# target. Used to validate cache entry freshness.
#
get_latest() {
echo "Checking for latest release..."
if [ "$http_client" = "wget" ] ; then
latest_pkg=$(do_retry $wget --post-data="target-config=${target}&suid=${suid}" -O - https://${server}/latest.php)
else
latest_pkg=$(do_retry $curl -d target-config="$target" https://${server}/latest.php)
fi
if [ $? -ne 0 ] ; then
echo "Unable to get latest release. Check connectivity and username/password."
exit 1
fi
if [ "$latest_pkg" = "FAIL" -o -z "$latest_pkg" ] ; then
echo "Unable to get latest release for this target."
echo "Use '-a --target list' to get valid targets."