blob: 6a5fe6ec5efeb6f033c538c9a5cf8c5f040c8dc5 [file] [log] [blame]
Leonardo Sandoval9dfdd1b2020-08-06 17:08:11 -05001#!/usr/bin/env bash
Fathi Boudra422bf772019-12-02 11:10:16 +02002#
Leonardo Sandoval579c7372020-10-23 15:23:32 -05003# Copyright (c) 2019-2020 Arm Limited. All rights reserved.
Fathi Boudra422bf772019-12-02 11:10:16 +02004#
5# SPDX-License-Identifier: BSD-3-Clause
6#
7
8# Builds a package with Trusted Firwmare and other payload binaries. The package
9# is meant to be executed by run_package.sh
10
11set -e
12
13ci_root="$(readlink -f "$(dirname "$0")/..")"
14source "$ci_root/utils.sh"
15
16if [ ! -d "$workspace" ]; then
17 die "Directory $workspace doesn't exist"
18fi
19
20# Directory to where the source code e.g. for Trusted Firmware is checked out.
Zelalem219df412020-05-17 19:21:20 -050021export tf_root="${tf_root:-$workspace/trusted_firmware}"
22export tftf_root="${tftf_root:-$workspace/trusted_firmware_tf}"
23export scp_root="${scp_root:-$workspace/scp}"
24scp_tools_root="${scp_tools_root:-$workspace/scp_tools}"
25cc_root="${cc_root:-$ccpathspec}"
26
27scp_tf_tools_root="$scp_tools_root/scp_tf_tools"
Fathi Boudra422bf772019-12-02 11:10:16 +020028
29# Refspecs
30tf_refspec="$TF_REFSPEC"
31tftf_refspec="$TFTF_REFSPEC"
32scp_refspec="$SCP_REFSPEC"
Zelalem219df412020-05-17 19:21:20 -050033scp_tools_commit="${SCP_TOOLS_COMMIT:-master}"
Fathi Boudra422bf772019-12-02 11:10:16 +020034
35test_config="${TEST_CONFIG:?}"
36test_group="${TEST_GROUP:?}"
37build_configs="${BUILD_CONFIG:?}"
38run_config="${RUN_CONFIG:?}"
Zelalem219df412020-05-17 19:21:20 -050039cc_config="${CC_ENABLE:-}"
Fathi Boudra422bf772019-12-02 11:10:16 +020040
41archive="$artefacts"
42build_log="$artefacts/build.log"
43fiptool="$tf_root/tools/fiptool/fiptool"
44cert_create="$tf_root/tools/cert_create/cert_create"
45
46# Validate $bin_mode
47case "$bin_mode" in
48 "" | debug | release)
49 ;;
50 *)
51 die "Invalid value for bin_mode: $bin_mode"
52 ;;
53esac
54
55# File to save any environem
56hook_env_file="$(mktempfile)"
57
58# Check if a config is valid
59config_valid() {
60 local config="${1?}"
61 if [ -z "$config" ] || [ "$(basename "$config")" = "nil" ]; then
62 return 1
63 fi
64
65 return 0
66}
67
68# Echo from a build wrapper. Print to descriptor 3 that's opened by the build
69# function.
70echo_w() {
71 echo $echo_flags "$@" >&3
72}
73
74# Print a separator to the log file. Intended to be used at the tail end of a pipe
75log_separator() {
76 {
77 echo
78 echo "----------"
79 } >> "$build_log"
80
81 tee -a "$build_log"
82
83 {
84 echo "----------"
85 echo
86 } >> "$build_log"
87}
88
89# Call function $1 if it's defined
90call_func() {
91 if type "${1:?}" &>/dev/null; then
92 echo
93 echo "> ${2:?}:$1()"
94 eval "$1"
95 echo "< $2:$1()"
96 fi
97}
98
99# Call hook $1 in all chosen fragments if it's defined. Hooks are invoked from
100# within a subshell, so any variables set within a hook are lost. Should a
101# variable needs to be set from within a hook, the function 'set_hook_var'
102# should be used
103call_hook() {
104 local func="$1"
105 local config_fragment
106
107 [ -z "$func" ] && return 0
108
109 : >"$hook_env_file"
110
111 if [ "$run_config_candiates" ]; then
112 for config_fragment in $run_config_candiates; do
113 (
114 source "$ci_root/run_config/$config_fragment"
115 call_func "$func" "$config_fragment"
116 )
117 done
118 fi
119
120 # Also source test config file
121 (
122 unset "$func"
123 source "$test_config_file"
124 call_func "$func" "$(basename $test_config_file)"
125 )
126
127 # Have any variables set take effect
128 source "$hook_env_file"
129}
130
131# Set a variable from within a hook
132set_hook_var() {
133 echo "export $1=\"${2?}\"" >> "$hook_env_file"
134}
135
136# Append to an array from within a hook
137append_hook_var() {
138 echo "export $1+=\"${2?}\"" >> "$hook_env_file"
139}
140
141# Have the main build script source a file
142source_later() {
143 echo "source ${1?}" >> "$hook_env_file"
144}
145
146# Setup TF build wrapper function by pointing to a script containing a function
147# that will be called with the TF build commands.
148setup_tf_build_wrapper() {
149 source_later "$ci_root/script/${wrapper?}_wrapper.sh"
150 set_hook_var "tf_build_wrapper" "${wrapper}_wrapper"
151 echo "Setup $wrapper build wrapper."
152}
153
154# Collect .bin files for archiving
155collect_build_artefacts() {
156 if [ ! -d "${from:?}" ]; then
157 return
158 fi
159
Javier Almansa Sobrinof98dbd82020-09-30 19:29:27 +0100160 if ! find "$from" \( -name "*.bin" -o -name '*.elf' -o -name '*.dtb' -o -name '*.axf' \) -exec cp -t "${to:?}" '{}' +; then
Fathi Boudra422bf772019-12-02 11:10:16 +0200161 echo "You probably are running local CI on local repositories."
162 echo "Did you set 'dont_clean' but forgot to run 'distclean'?"
163 die
164 fi
165}
166
167# SCP and MCP binaries are named firmware.{bin,elf}, and are placed under
168# scp/mcp_ramfw and scp/mcp_romfw directories, so can't be collected by
169# collect_build_artefacts function.
170collect_scp_artefacts() {
171 to="${to:?}" \
172 find "$scp_root" \( -name "*.bin" -o -name '*.elf' \) -exec bash -c '
173 for file; do
174 ext="$(echo $file | awk -F. "{print \$NF}")"
175 case $file in
176 */scp_ramfw/*)
177 cp $file $to/scp_ram.$ext
178 ;;
179 */scp_romfw/*)
180 cp $file $to/scp_rom.$ext
181 ;;
182 */mcp_ramfw/*)
183 cp $file $to/mcp_ram.$ext
184 ;;
185 */mcp_romfw/*)
186 cp $file $to/mcp_rom.$ext
187 ;;
Zelalem219df412020-05-17 19:21:20 -0500188 */scp_romfw_bypass/*)
189 cp $file $to/scp_rom_bypass.$ext
190 ;;
Fathi Boudra422bf772019-12-02 11:10:16 +0200191 *)
192 echo "Unknown SCP binary: $file" >&2
193 ;;
194 esac
195 done
196 ' bash '{}' +
197}
198
Javier Almansa Sobrino412d3612020-05-22 17:53:12 +0100199# Map the UART ID used for expect with the UART descriptor and port
200# used by the FPGA automation tools.
201map_uart() {
202 local port="${port:?}"
203 local descriptor="${descriptor:?}"
204 local baudrate="${baudrate:?}"
205 local run_root="${archive:?}/run"
206
207 local uart_dir="$run_root/uart${uart:?}"
208 mkdir -p "$uart_dir"
209
210 echo "$port" > "$uart_dir/port"
211 echo "$descriptor" > "$uart_dir/descriptor"
212 echo "$baudrate" > "$uart_dir/baudrate"
213
214 echo "UART${uart} mapped to port ${port} with descriptor ${descriptor} and baudrate ${baudrate}"
215}
216
Fathi Boudra422bf772019-12-02 11:10:16 +0200217# Arrange environment varibles to be set when expect scripts are launched
218set_expect_variable() {
219 local var="${1:?}"
220 local val="${2?}"
221
222 local run_root="${archive:?}/run"
223 local uart_dir="$run_root/uart${uart:?}"
224 mkdir -p "$uart_dir"
225
226 env_file="$uart_dir/env" quote="1" emit_env "$var" "$val"
227 echo "UART$uart: env has $@"
228}
229
230# Place the binary package a pointer to expect script, and its parameters
231track_expect() {
232 local file="${file:?}"
233 local timeout="${timeout-600}"
234 local run_root="${archive:?}/run"
235
236 local uart_dir="$run_root/uart${uart:?}"
237 mkdir -p "$uart_dir"
238
239 echo "$file" > "$uart_dir/expect"
240 echo "$timeout" > "$uart_dir/timeout"
241
242 echo "UART$uart to be tracked with $file; timeout ${timeout}s"
243
244 # The run script assumes UART0 to be primary. If we're asked to set any
245 # other UART to be primary, set a run environment variable to signal
246 # that to the run script
247 if upon "$set_primary"; then
248 echo "Primary UART set to UART$uart."
249 set_run_env "primary_uart" "$uart"
250 fi
251}
252
253# Extract a FIP in $1 using fiptool
254extract_fip() {
255 local fip="$1"
256
257 if is_url "$1"; then
258 url="$1" fetch_file
259 fip="$(basename "$1")"
260 fi
261
262 "$fiptool" unpack "$fip"
263 echo "Extracted FIP: $fip"
264}
265
266# Report build failure by printing a the tail end of build log. Archive the
267# build log for later inspection
268fail_build() {
269 local log_path
270
271 if upon "$jenkins_run"; then
272 log_path="$BUILD_URL/artifact/artefacts/build.log"
273 else
274 log_path="$build_log"
275 fi
276
277 echo
Leonardo Sandovalae97eda2020-11-12 10:07:03 -0600278 echo "Build failed! Full build log below:"
Fathi Boudra422bf772019-12-02 11:10:16 +0200279 echo "[...]"
280 echo
Leonardo Sandovalae97eda2020-11-12 10:07:03 -0600281 cat "$build_log"
Fathi Boudra422bf772019-12-02 11:10:16 +0200282 echo
283 echo "See $log_path for full output"
284 echo
285 cp -t "$archive" "$build_log"
286 exit 1;
287}
288
289# Build a FIP with supplied arguments
290build_fip() {
291 (
292 echo "Building FIP with arguments: $@"
293 local tf_env="$workspace/tf.env"
294
295 if [ -f "$tf_env" ]; then
296 set -a
297 source "$tf_env"
298 set +a
299 fi
300
301 make -C "$tf_root" $(cat "$tf_config_file") DEBUG="$DEBUG" V=1 "$@" \
302 ${fip_targets:-fip} &>>"$build_log" || fail_build
303 )
304}
305
306fip_update() {
307 # Before the update process, check if the given image is supported by
308 # the fiptool. It's assumed that both fiptool and cert_create move in
309 # tandem, and therfore, if one has support, the other has it too.
310 if ! "$fiptool" update 2>&1 | grep -qe "\s\+--${bin_name:?}"; then
311 return 1
312 fi
313
314 if not_upon "$(get_tf_opt TRUSTED_BOARD_BOOT)"; then
315 echo "Updating FIP image: $bin_name"
316 # Update HW config. Without TBBR, it's only a matter of using
317 # the update sub-command of fiptool
318 "$fiptool" update "--$bin_name" "${src:-}" \
319 "$archive/fip.bin"
320 else
321 echo "Updating FIP image (TBBR): $bin_name"
322 # With TBBR, we need to unpack, re-create certificates, and then
323 # recreate the FIP.
324 local fip_dir="$(mktempdir)"
325 local bin common_args stem
326 local rot_key="$(get_tf_opt ROT_KEY)"
327
328 rot_key="${rot_key:?}"
329 if ! is_abs "$rot_key"; then
330 rot_key="$tf_root/$rot_key"
331 fi
332
333 # Arguments only for cert_create
334 local cert_args="-n"
335 cert_args+=" --tfw-nvctr ${nvctr:-31}"
336 cert_args+=" --ntfw-nvctr ${nvctr:-223}"
337 cert_args+=" --key-alg ${KEY_ALG:-rsa}"
338 cert_args+=" --rot-key $rot_key"
339
340 local dyn_config_opts=(
Zelalem1af7a7b2020-08-04 17:34:32 -0500341 "fw-config"
Fathi Boudra422bf772019-12-02 11:10:16 +0200342 "hw-config"
343 "tb-fw-config"
344 "nt-fw-config"
345 "soc-fw-config"
346 "tos-fw-config"
347 )
348
349 # Binaries without key certificates
350 declare -A has_no_key_cert
351 for bin in "tb-fw" "${dyn_config_opts[@]}"; do
352 has_no_key_cert["$bin"]="1"
353 done
354
355 # Binaries without certificates
356 declare -A has_no_cert
357 for bin in "hw-config" "${dyn_config_opts[@]}"; do
358 has_no_cert["$bin"]="1"
359 done
360
361 pushd "$fip_dir"
362
363 # Unpack FIP
364 "$fiptool" unpack "$archive/fip.bin" &>>"$build_log"
365
366 # Remove all existing certificates
367 rm -f *-cert.bin
368
369 # Copy the binary to be updated
370 cp -f "$src" "${bin_name}.bin"
371
372 # FIP unpack dumps binaries with the same name as the option
373 # used to pack it; likewise for certificates. Reverse-engineer
374 # the command line from the binary output.
375 common_args="--trusted-key-cert trusted_key.crt"
376 for bin in *.bin; do
377 stem="${bin%%.bin}"
378 common_args+=" --$stem $bin"
379 if not_upon "${has_no_cert[$stem]}"; then
380 common_args+=" --$stem-cert $stem.crt"
381 fi
382 if not_upon "${has_no_key_cert[$stem]}"; then
383 common_args+=" --$stem-key-cert $stem-key.crt"
384 fi
385 done
386
387 # Create certificates
388 "$cert_create" $cert_args $common_args &>>"$build_log"
389
390 # Recreate and archive FIP
391 "$fiptool" create $common_args "fip.bin" &>>"$build_log"
392 archive_file "fip.bin"
393
394 popd
395 fi
396}
397
398# Update hw-config in FIP, and remove the original DTB afterwards.
399update_fip_hw_config() {
400 # The DTB needs to be loaded by the model (and not updated in the FIP)
401 # in configs where BL2 isn't present
402 case "1" in
403 "$(get_tf_opt RESET_TO_BL31)" | \
404 "$(get_tf_opt RESET_TO_SP_MIN)" | \
405 "$(get_tf_opt BL2_AT_EL3)")
406 return 0;;
407 esac
408
409 if bin_name="hw-config" src="$archive/dtb.bin" fip_update; then
410 # Remove the DTB so that model won't load it
411 rm -f "$archive/dtb.bin"
412 fi
413}
414
415get_scp_opt() {
416 (
417 name="${1:?}"
418 if config_valid "$scp_config_file"; then
419 source "$scp_config_file"
420 echo "${!name}"
421 fi
422 )
423}
424
425get_tftf_opt() {
426 (
427 name="${1:?}"
428 if config_valid "$tftf_config_file"; then
429 source "$tftf_config_file"
430 echo "${!name}"
431 fi
432 )
433}
434
435get_tf_opt() {
436 (
437 name="${1:?}"
438 if config_valid "$tf_config_file"; then
439 source "$tf_config_file"
440 echo "${!name}"
441 fi
442 )
443}
444
445build_tf() {
446 (
447 env_file="$workspace/tf.env"
448 config_file="${tf_build_config:-$tf_config_file}"
449
450 # Build fiptool and all targets by default
451 build_targets="${tf_build_targets:-fiptool all}"
452
453 source "$config_file"
454
455 # If it is a TBBR build, extract the MBED TLS library from archive
456 if [ "$(get_tf_opt TRUSTED_BOARD_BOOT)" = 1 ]; then
457 mbedtls_dir="$workspace/mbedtls"
458 if [ ! -d "$mbedtls_dir" ]; then
459 mbedtls_ar="$workspace/mbedtls.tar.gz"
460
461 url="$mbedtls_archive" saveas="$mbedtls_ar" fetch_file
462 mkdir "$mbedtls_dir"
Leonardo Sandovalec9e16c2020-09-09 14:32:40 -0500463 extract_tarball $mbedtls_ar $mbedtls_dir --strip-components=1
Fathi Boudra422bf772019-12-02 11:10:16 +0200464 fi
465
466 emit_env "MBEDTLS_DIR" "$mbedtls_dir"
467 fi
468
469 if [ -f "$env_file" ]; then
470 set -a
471 source "$env_file"
472 set +a
473 fi
474
475 cd "$tf_root"
476
477 # Always distclean when running on Jenkins. Skip distclean when running
478 # locally and explicitly requested.
479 if upon "$jenkins_run" || not_upon "$dont_clean"; then
480 make distclean &>>"$build_log" || fail_build
481 fi
482
483 # Log build command line. It is left unfolded on purpose to assist
484 # copying to clipboard.
485 cat <<EOF | log_separator >/dev/null
486
487Build command line:
488 $tf_build_wrapper make $make_j_opts $(cat "$config_file" | tr '\n' ' ') DEBUG=$DEBUG V=1 $build_targets
489
490EOF
491
492 # Build TF. Since build output is being directed to the build log, have
493 # descriptor 3 point to the current terminal for build wrappers to vent.
494 $tf_build_wrapper make $make_j_opts $(cat "$config_file") \
495 DEBUG="$DEBUG" V=1 \
496 $build_targets 3>&1 &>>"$build_log" || fail_build
497 )
498}
499
500build_tftf() {
501 (
502 config_file="${tftf_build_config:-$tftf_config_file}"
503
504 # Build tftf target by default
505 build_targets="${tftf_build_targets:-all}"
506
507 source "$config_file"
508
509 cd "$tftf_root"
510
511 # Always distclean when running on Jenkins. Skip distclean when running
512 # locally and explicitly requested.
513 if upon "$jenkins_run" || not_upon "$dont_clean"; then
514 make distclean &>>"$build_log" || fail_build
515 fi
516
517 # TFTF build system cannot reliably deal with -j option, so we avoid
518 # using that.
519
520 # Log build command line
521 cat <<EOF | log_separator >/dev/null
522
523Build command line:
524 make $(cat "$config_file" | tr '\n' ' ') DEBUG=$DEBUG V=1 $build_targets
525
526EOF
527
528 make $(cat "$config_file") DEBUG="$DEBUG" V=1 \
529 $build_targets &>>"$build_log" || fail_build
530 )
531}
532
533build_scp() {
534 (
535 config_file="${scp_build_config:-$scp_config_file}"
536
537 source "$config_file"
538
539 cd "$scp_root"
540
541 # Always distclean when running on Jenkins. Skip distclean when running
542 # locally and explicitly requested.
543 if upon "$jenkins_run" || not_upon "$dont_clean"; then
544 make clean &>>"$build_log" || fail_build
545 fi
546
547 # Log build command line. It is left unfolded on purpose to assist
548 # copying to clipboard.
549 cat <<EOF | log_separator >/dev/null
550
551SCP build command line:
552 make $(cat "$config_file" | tr '\n' ' ') MODE=$mode V=1
553
554EOF
555
556 # Build SCP
557 make $(cat "$config_file") MODE="$mode" V=1 &>>"$build_log" \
558 || fail_build
559 )
560}
561
Zelalem219df412020-05-17 19:21:20 -0500562clone_scp_tools() {
563
564 if [ ! -d "$scp_tools_root" ]; then
565 echo "Cloning SCP-tools ... $scp_tools_commit" |& log_separator
566
567 clone_url="${SCP_TOOLS_CHECKOUT_LOC:-$scp_tools_src_repo_url}" \
568 where="$scp_tools_root" \
569 refspec="${scp_tools_commit}"
570 clone_repo &>>"$build_log"
571 else
572 echo "Already cloned SCP-tools ..." |& log_separator
573 fi
574
575 show_head "$scp_tools_root"
576
577 cd "$scp_tools_root"
578
579 echo "Updating submodules"
580
581 git submodule init
582
583 git submodule update
584
585 cd "scmi"
586
587 git show --quiet --no-color | sed 's/^/ > /g'
588}
589
590clone_tf_for_scp_tools() {
591 scp_tools_arm_tf="$scp_tools_root/arm-tf"
592
593 if [ ! -d "$scp_tools_arm_tf" ]; then
594 echo "Cloning TF-4-SCP-tools ..." |& log_separator
595
596 clone_url="$tf_for_scp_tools_src_repo_url"
597 where="$scp_tools_arm_tf"
598
599 git clone "$clone_url" "$where"
600
601 cd "$scp_tools_arm_tf"
602
603 git checkout --track origin/dev/pedro/juno
604
605 git show --quiet --no-color | sed 's/^/ > /g'
606
607 else
608 echo "Already cloned TF-4-SCP-tools ..." |& log_separator
609 fi
610}
611
612build_scmi_lib_scp_tools() {
613 (
614 cd "$scp_tools_root"
615
616 cd "scmi"
617
618 scp_tools_arm_tf="$scp_tools_root/arm-tf"
619
Leonardo Sandovalfeae3a22020-11-17 16:53:59 -0600620 cross_compile="$(set_cross_compile_gcc_linaro_toolchain)"
Zelalem219df412020-05-17 19:21:20 -0500621
622 std_libs="-I$scp_tools_arm_tf/include/common"
623 std_libs="$std_libs -I$scp_tools_arm_tf/include/common/tbbr"
624 std_libs="$std_libs -I$scp_tools_arm_tf/include/drivers/arm"
625 std_libs="$std_libs -I$scp_tools_arm_tf/include/lib"
626 std_libs="$std_libs -I$scp_tools_arm_tf/include/lib/aarch64"
627 std_libs="$std_libs -I$scp_tools_arm_tf/include/lib/stdlib"
628 std_libs="$std_libs -I$scp_tools_arm_tf/include/lib/stdlib/sys"
629 std_libs="$std_libs -I$scp_tools_arm_tf/include/lib/xlat_tables"
630 std_libs="$std_libs -I$scp_tools_arm_tf/include/plat/common"
631 std_libs="$std_libs -I$scp_tools_arm_tf/include/plat/arm/common"
632 std_libs="$std_libs -I$scp_tools_arm_tf/include/plat/arm/css/common"
633 std_libs="$std_libs -I$scp_tools_arm_tf/include/plat/arm/board/common"
634 std_libs="$std_libs -I$scp_tools_arm_tf/include/plat/arm/soc/common"
635 std_libs="$std_libs -I$scp_tools_arm_tf/plat/arm/board/juno/include"
636
637 cflags="-Og -g"
638 cflags="$cflags -mgeneral-regs-only"
639 cflags="$cflags -mstrict-align"
640 cflags="$cflags -nostdinc"
641 cflags="$cflags -fno-inline"
642 cflags="$cflags -ffreestanding"
643 cflags="$cflags -ffunction-sections"
644 cflags="$cflags -fdata-sections"
645 cflags="$cflags -DAARCH64"
646 cflags="$cflags -DPRId32=\"ld\""
647
648 cflags="$cflags $std_libs"
649
650 protocols="power,system_power,performance,sensor"
651
652 echo "Building SCMI library (SCP-tools) ..."
653
654 make "CROSS_COMPILE=$cross_compile" \
655 "CFLAGS=$cflags" \
656 "PROTOCOLS=$protocols" \
657 "clean" \
658 "all"
659 )
660}
661
662build_tf_for_scp_tools() {
663
664 cd "$scp_tools_root/arm-tf"
665
Leonardo Sandovalfeae3a22020-11-17 16:53:59 -0600666 cross_compile="$(set_cross_compile_gcc_linaro_toolchain)"
Zelalem219df412020-05-17 19:21:20 -0500667
668 if [ "$1" = "release" ]; then
669 echo "Build TF-4-SCP-Tools rls..."
670 else
671 echo "Build TF-4-SCP-Tools dbg..."
672
673 make realclean
674
675 make "BM_TEST=scmi" \
676 "ARM_BOARD_OPTIMISE_MEM=1" \
677 "BM_CSS=juno" \
678 "CSS_USE_SCMI_SDS_DRIVER=1" \
679 "PLAT=juno" \
680 "DEBUG=1" \
681 "PLATFORM=juno" \
682 "CROSS_COMPILE=$cross_compile" \
683 "BM_WORKSPACE=$scp_tools_root/baremetal"
684
685 archive_file "build/juno/debug/bl1.bin"
686
687 archive_file "build/juno/debug/bl2.bin"
688
689 archive_file "build/juno/debug/bl31.bin"
690 fi
691}
692
693build_fip_for_scp_tools() {
694
695 cd "$scp_tools_root/arm-tf"
696
Leonardo Sandovalfeae3a22020-11-17 16:53:59 -0600697 cross_compile="$(set_cross_compile_gcc_linaro_toolchain)"
Zelalem219df412020-05-17 19:21:20 -0500698
699 if [ ! -d "$scp_root/build/product/juno/scp_ramfw/debug" ]; then
700 make fiptool
701 echo "Make FIP 4 SCP-Tools rls..."
702
703 else
704 make fiptool
705 echo "Make FIP 4 SCP-Tools dbg..."
706
707 make "PLAT=juno" \
708 "all" \
709 "fip" \
710 "DEBUG=1" \
711 "CROSS_COMPILE=$cross_compile" \
712 "BL31=$scp_tools_root/arm-tf/build/juno/debug/bl31.bin" \
713 "BL33=$scp_tools_root/baremetal/dummy_bl33" \
714 "SCP_BL2=$scp_root/build/product/juno/scp_ramfw/debug/bin/firmware.bin"
715
716 archive_file "$scp_tools_root/arm-tf/build/juno/debug/fip.bin"
717 fi
718}
719
720build_cc() {
721# Building code coverage plugin
722 ARM_DIR=/arm
723 pvlibversion=$(/arm/devsys-tools/abs/detag "SysGen:PVModelLib:$model_version::trunk")
724 PVLIB_HOME=$warehouse/SysGen/PVModelLib/$model_version/${pvlibversion}/external
725 if [ -n "$(find "$ARM_DIR" -maxdepth 0 -type d -empty 2>/dev/null)" ]; then
726 echo "Error: Arm warehouse not mounted. Please mount the Arm warehouse to your /arm local folder"
727 exit -1
728 fi # Error if arm warehouse not found
729 cd "$ccpathspec/scripts/tools/code_coverage/fastmodel_baremetal/bmcov"
730
731 make -C model-plugin PVLIB_HOME=$PVLIB_HOME &>>"$build_log"
732}
733
734
Fathi Boudra422bf772019-12-02 11:10:16 +0200735# Set metadata for the whole package so that it can be used by both Jenkins and
736# shell
737set_package_var() {
738 env_file="$artefacts/env" emit_env "$@"
739}
740
741set_tf_build_targets() {
742 echo "Set build target to '${targets:?}'"
743 set_hook_var "tf_build_targets" "$targets"
744}
745
746set_tftf_build_targets() {
747 echo "Set build target to '${targets:?}'"
748 set_hook_var "tftf_build_targets" "$targets"
749}
750
751set_scp_build_targets() {
752 echo "Set build target to '${targets:?}'"
753 set_hook_var "scp_build_targets" "$targets"
754}
755
756# Look under $archive directory for known files such as blX images, kernel, DTB,
757# initrd etc. For each known file foo, if foo.bin exists, then set variable
758# foo_bin to the path of the file. Make the path relative to the workspace so as
759# to remove any @ characters, which Jenkins inserts for parallel runs. If the
760# file doesn't exist, unset its path.
761set_default_bin_paths() {
762 local image image_name image_path path
763 local archive="${archive:?}"
764 local set_vars
765 local var
766
767 pushd "$archive"
768
769 for file in *.bin; do
770 # Get a shell variable from the file's stem
771 var_name="${file%%.*}_bin"
772 var_name="$(echo "$var_name" | sed -r 's/[^[:alnum:]]/_/g')"
773
774 # Skip setting the variable if it's already
775 if [ "${!var_name}" ]; then
776 echo "Note: not setting $var_name; already set to ${!var_name}"
777 continue
778 else
779 set_vars+="$var_name "
780 fi
781
782 eval "$var_name=$file"
783 done
784
785 echo "Binary paths set for: "
786 {
787 for var in $set_vars; do
788 echo -n "\$$var "
789 done
790 } | fmt -80 | sed 's/^/ /'
791 echo
792
793 popd
794}
795
796gen_model_params() {
797 local model_param_file="$archive/model_params"
798
799 set_default_bin_paths
800 echo "Generating model parameter for $model..."
801 source "$ci_root/model/${model:?}.sh"
802 archive_file "$model_param_file"
803}
804
805set_model_path() {
806 set_run_env "model_path" "${1:?}"
807}
808
Zelalem1af7a7b2020-08-04 17:34:32 -0500809set_model_env() {
810 local var="${1:?}"
811 local val="${2?}"
812 local run_root="${archive:?}/run"
813
814 mkdir -p "$run_root"
815 echo "export $var=$val" >> "$run_root/model_env"
816}
Fathi Boudra422bf772019-12-02 11:10:16 +0200817set_run_env() {
818 local var="${1:?}"
819 local val="${2?}"
820 local run_root="${archive:?}/run"
821
822 mkdir -p "$run_root"
823 env_file="$run_root/env" quote="1" emit_env "$var" "$val"
824}
825
826show_head() {
827 # Display HEAD descripton
828 pushd "$1"
829 git show --quiet --no-color | sed 's/^/ > /g'
830 echo
831 popd
832}
833
834# Choose debug binaries to run; by default, release binaries are chosen to run
835use_debug_bins() {
836 local run_root="${archive:?}/run"
837
838 echo "Choosing debug binaries for execution"
839 set_package_var "BIN_MODE" "debug"
840}
841
842assert_can_git_clone() {
843 local name="${1:?}"
844 local dir="${!name}"
845
846 # If it doesn't exist, it can be cloned into
847 if [ ! -e "$dir" ]; then
848 return 0
849 fi
850
851 # If it's a directory, it must be a Git clone already
852 if [ -d "$dir" ] && [ -d "$dir/.git" ]; then
853 # No need to clone again
854 echo "Using existing git clone for $name: $dir"
855 return 1
856 fi
857
858 die "Path $dir exists but is not a git clone"
859}
860
861clone_repo() {
862 if ! is_url "${clone_url?}"; then
863 # For --depth to take effect on local paths, it needs to use the
864 # file:// scheme.
865 clone_url="file://$clone_url"
866 fi
867
868 git clone -q --depth 1 "$clone_url" "${where?}"
869 if [ "$refspec" ]; then
870 pushd "$where"
871 git fetch -q --depth 1 origin "$refspec"
872 git checkout -q FETCH_HEAD
873 popd
874 fi
875}
876
877build_unstable() {
878 echo "--BUILD UNSTABLE--" | tee -a "$build_log"
879}
880
881undo_patch_record() {
882 if [ ! -f "${patch_record:?}" ]; then
883 return
884 fi
885
886 # Undo patches in reverse
887 echo
888 for patch_name in $(tac "$patch_record"); do
889 echo "Undoing $patch_name..."
890 if ! git apply -R "$ci_root/patch/$patch_name"; then
891 if upon "$local_ci"; then
892 echo
893 echo "Your local directory may have been dirtied."
894 echo
895 fi
896 fail_build
897 fi
898 done
899
900 rm -f "$patch_record"
901}
902
903undo_local_patches() {
904 pushd "$tf_root"
905 patch_record="$tf_patch_record" undo_patch_record
906 popd
907
908 if [ -d "$tftf_root" ]; then
909 pushd "$tftf_root"
910 patch_record="$tftf_patch_record" undo_patch_record
911 popd
912 fi
913}
914
915undo_tftf_patches() {
916 pushd "$tftf_root"
917 patch_record="$tftf_patch_record" undo_patch_record
918 popd
919}
920
921undo_tf_patches() {
922 pushd "$tf_root"
923 patch_record="$tf_patch_record" undo_patch_record
924 popd
925}
926
927apply_patch() {
928 # If skip_patches is set, the developer has applied required patches
929 # manually. They probably want to keep them applied for debugging
930 # purposes too. This means we don't have to apply/revert them as part of
931 # build process.
932 if upon "$skip_patches"; then
933 echo "Skipped applying ${1:?}..."
934 return 0
935 else
936 echo "Applying ${1:?}..."
937 fi
938
939 if git apply < "$ci_root/patch/$1"; then
940 echo "$1" >> "${patch_record:?}"
941 else
942 if upon "$local_ci"; then
943 undo_local_patches
944 fi
945 fail_build
946 fi
947}
948
949apply_tftf_patch() {
950 pushd "$tftf_root"
951 patch_record="$tftf_patch_record" apply_patch "$1"
952 popd
953}
954
955apply_tf_patch() {
956 pushd "$tf_root"
957 patch_record="$tf_patch_record" apply_patch "$1"
958 popd
959}
960
961# Clear workspace for a local run
962if not_upon "$jenkins_run"; then
963 rm -rf "$workspace"
964
965 # Clear residue from previous runs
966 rm -rf "$archive"
967fi
968
969mkdir -p "$workspace"
970mkdir -p "$archive"
971set_package_var "TEST_CONFIG" "$test_config"
972
973{
974echo
975echo "CONFIGURATION: $test_group/$test_config"
976echo
977} |& log_separator
978
979tf_config="$(echo "$build_configs" | awk -F, '{print $1}')"
980tftf_config="$(echo "$build_configs" | awk -F, '{print $2}')"
981scp_config="$(echo "$build_configs" | awk -F, '{print $3}')"
Zelalem219df412020-05-17 19:21:20 -0500982scp_tools_config="$(echo "$build_configs" | awk -F, '{print $4}')"
Fathi Boudra422bf772019-12-02 11:10:16 +0200983
984test_config_file="$ci_root/group/$test_group/$test_config"
985
986tf_config_file="$ci_root/tf_config/$tf_config"
987tftf_config_file="$ci_root/tftf_config/$tftf_config"
988scp_config_file="$ci_root/scp_config/$scp_config"
Zelalem219df412020-05-17 19:21:20 -0500989scp_tools_config_file="$ci_root/scp_tools_config/$scp_tools_config"
Fathi Boudra422bf772019-12-02 11:10:16 +0200990
991# File that keeps track of applied patches
992tf_patch_record="$workspace/tf_patches"
993tftf_patch_record="$workspace/tftf_patches"
994
995pushd "$workspace"
996
997if ! config_valid "$tf_config"; then
998 tf_config=
999else
1000 echo "Trusted Firmware config:"
1001 echo
1002 sort "$tf_config_file" | sed '/^\s*$/d;s/^/\t/'
1003 echo
1004fi
1005
1006if ! config_valid "$tftf_config"; then
1007 tftf_config=
1008else
1009 echo "Trusted Firmware TF config:"
1010 echo
1011 sort "$tftf_config_file" | sed '/^\s*$/d;s/^/\t/'
1012 echo
1013fi
1014
1015if ! config_valid "$scp_config"; then
1016 scp_config=
1017else
1018 echo "SCP firmware config:"
1019 echo
1020 sort "$scp_config_file" | sed '/^\s*$/d;s/^/\t/'
1021 echo
1022fi
1023
Zelalem219df412020-05-17 19:21:20 -05001024if ! config_valid "$scp_tools_config"; then
1025 scp_tools_config=
1026else
1027 echo "SCP Tools config:"
1028 echo
1029 sort "$scp_tools_config_file" | sed '/^\s*$/d;s/^/\t/'
1030 echo
1031fi
1032
Fathi Boudra422bf772019-12-02 11:10:16 +02001033if ! config_valid "$run_config"; then
1034 run_config=
1035fi
1036
1037if [ "$tf_config" ] && assert_can_git_clone "tf_root"; then
1038 # If the Trusted Firmware repository has already been checked out, use
1039 # that location. Otherwise, clone one ourselves.
1040 echo "Cloning Trusted Firmware..."
1041 clone_url="${TF_CHECKOUT_LOC:-$tf_src_repo_url}" where="$tf_root" \
1042 refspec="$TF_REFSPEC" clone_repo &>>"$build_log"
1043 show_head "$tf_root"
1044fi
1045
1046if [ "$tftf_config" ] && assert_can_git_clone "tftf_root"; then
1047 # If the Trusted Firmware TF repository has already been checked out,
1048 # use that location. Otherwise, clone one ourselves.
1049 echo "Cloning Trusted Firmware TF..."
1050 clone_url="${TFTF_CHECKOUT_LOC:-$tftf_src_repo_url}" where="$tftf_root" \
1051 refspec="$TFTF_REFSPEC" clone_repo &>>"$build_log"
1052 show_head "$tftf_root"
1053fi
1054
1055if [ "$scp_config" ] && assert_can_git_clone "scp_root"; then
1056 # If the SCP firmware repository has already been checked out,
1057 # use that location. Otherwise, clone one ourselves.
1058 echo "Cloning SCP Firmware..."
1059 clone_url="${SCP_CHECKOUT_LOC:-$scp_src_repo_url}" where="$scp_root" \
1060 refspec="${SCP_REFSPEC-master-upstream}" clone_repo &>>"$build_log"
1061
1062 pushd "$scp_root"
1063
1064 # Use filer submodule as a reference if it exists
1065 if [ -d "$SCP_CHECKOUT_LOC/cmsis" ]; then
1066 cmsis_reference="--reference $SCP_CHECKOUT_LOC/cmsis"
1067 fi
1068
1069 # If we don't have a reference yet, fall back to $cmsis_root if set, or
1070 # then to project filer if accessible.
1071 if [ -z "$cmsis_reference" ]; then
1072 cmsis_ref_repo="${cmsis_root:-$project_filer/ref-repos/cmsis}"
1073 if [ -d "$cmsis_ref_repo" ]; then
1074 cmsis_reference="--reference $cmsis_ref_repo"
1075 fi
1076 fi
1077
1078 git submodule -q update $cmsis_reference --init
1079
1080 popd
1081
1082 show_head "$scp_root"
1083fi
1084
Zelalem219df412020-05-17 19:21:20 -05001085if [ -n "$cc_config" ] ; then
1086 if [ "$cc_config" -eq 1 ] && assert_can_git_clone "cc_root"; then
1087 # Copy code coverage repository
1088 echo "Cloning Code Coverage..."
1089 git clone -q $cc_src_repo_url cc_plugin --depth 1 -b $cc_src_repo_tag > /dev/null
1090 show_head "$cc_root"
1091 fi
1092fi
1093
Fathi Boudra422bf772019-12-02 11:10:16 +02001094if [ "$run_config" ]; then
1095 # Get candidates for run config
1096 run_config_candiates="$("$ci_root/script/gen_run_config_candidates.py" \
1097 "$run_config")"
1098 if [ -z "$run_config_candiates" ]; then
1099 die "No run config candidates!"
1100 else
1101 echo
1102 echo "Chosen fragments:"
1103 echo
1104 echo "$run_config_candiates" | sed 's/^\|\n/\t/g'
1105 echo
1106 fi
1107fi
1108
1109call_hook "test_setup"
1110echo
1111
1112if upon "$local_ci"; then
1113 # For local runs, since each config is tried in sequence, it's
1114 # advantageous to run jobs in parallel
1115 if [ "$make_j" ]; then
1116 make_j_opts="-j $make_j"
1117 else
1118 n_cores="$(getconf _NPROCESSORS_ONLN)" 2>/dev/null || true
1119 if [ "$n_cores" ]; then
1120 make_j_opts="-j $n_cores"
1121 fi
1122 fi
1123fi
1124
1125modes="${bin_mode:-debug release}"
1126for mode in $modes; do
1127 # Build with a temporary archive
1128 build_archive="$archive/$mode"
1129 mkdir "$build_archive"
1130
1131 if [ "$mode" = "debug" ]; then
Zelalem219df412020-05-17 19:21:20 -05001132 export bin_mode="debug"
Fathi Boudra422bf772019-12-02 11:10:16 +02001133 DEBUG=1
1134 else
Zelalem219df412020-05-17 19:21:20 -05001135 export bin_mode="release"
Fathi Boudra422bf772019-12-02 11:10:16 +02001136 DEBUG=0
1137 fi
1138
1139 # Perform builds in a subshell so as not to pollute the current and
1140 # subsequent builds' environment
1141
Zelalem219df412020-05-17 19:21:20 -05001142 if config_valid "$cc_config"; then
1143 # Build code coverage plugin
1144 build_cc
1145 fi
1146
Fathi Boudra422bf772019-12-02 11:10:16 +02001147 # SCP build
1148 if config_valid "$scp_config"; then
1149 (
1150 echo "##########"
1151
1152 # Source platform-specific utilities
1153 plat="$(get_scp_opt PRODUCT)"
1154 plat_utils="$ci_root/${plat}_utils.sh"
1155 if [ -f "$plat_utils" ]; then
1156 source "$plat_utils"
1157 fi
1158
1159 archive="$build_archive"
1160 scp_build_root="$scp_root/build"
1161
1162 echo "Building SCP Firmware ($mode) ..." |& log_separator
1163
1164 build_scp
Fathi Boudra422bf772019-12-02 11:10:16 +02001165 to="$archive" collect_scp_artefacts
1166
1167 echo "##########"
1168 echo
1169 )
1170 fi
1171
Zelalem219df412020-05-17 19:21:20 -05001172 # SCP-tools build
1173 if config_valid "$scp_tools_config"; then
1174 (
1175 echo "##########"
1176
1177 archive="$build_archive"
1178 scp_tools_build_root="$scp_tools_root/build"
1179
1180 clone_scp_tools
1181
1182 echo "##########"
1183 echo
1184
1185 echo "##########"
1186 clone_tf_for_scp_tools
1187 echo "##########"
1188 echo
1189 )
1190 fi
1191
Fathi Boudra422bf772019-12-02 11:10:16 +02001192 # TFTF build
1193 if config_valid "$tftf_config"; then
1194 (
1195 echo "##########"
1196
Manish V Badarkhe3bd3fea2020-11-08 15:17:00 +00001197 plat_utils="$(get_tf_opt PLAT_UTILS)"
1198 if [ -z ${plat_utils} ]; then
1199 # Source platform-specific utilities.
1200 plat="$(get_tftf_opt PLAT)"
1201 plat_utils="$ci_root/${plat}_utils.sh"
1202 else
1203 # Source platform-specific utilities by
1204 # using plat_utils name.
1205 plat_utils="$ci_root/${plat_utils}.sh"
1206 fi
1207
Fathi Boudra422bf772019-12-02 11:10:16 +02001208 if [ -f "$plat_utils" ]; then
1209 source "$plat_utils"
1210 fi
1211
1212 archive="$build_archive"
1213 tftf_build_root="$tftf_root/build"
1214
1215 echo "Building Trusted Firmware TF ($mode) ..." |& log_separator
1216
1217 # Call pre-build hook
1218 call_hook pre_tftf_build
1219
1220 build_tftf
1221
1222 from="$tftf_build_root" to="$archive" collect_build_artefacts
1223
1224 # Clear any local changes made by applied patches
1225 undo_tftf_patches
1226
1227 echo "##########"
1228 echo
1229 )
1230 fi
1231
1232 # TF build
1233 if config_valid "$tf_config"; then
1234 (
1235 echo "##########"
1236
Manish V Badarkhe3bd3fea2020-11-08 15:17:00 +00001237 plat_utils="$(get_tf_opt PLAT_UTILS)"
1238 if [ -z ${plat_utils} ]; then
1239 # Source platform-specific utilities.
1240 plat="$(get_tf_opt PLAT)"
1241 plat_utils="$ci_root/${plat}_utils.sh"
1242 else
1243 # Source platform-specific utilities by
1244 # using plat_utils name.
1245 plat_utils="$ci_root/${plat_utils}.sh"
1246 fi
1247
Fathi Boudra422bf772019-12-02 11:10:16 +02001248 if [ -f "$plat_utils" ]; then
1249 source "$plat_utils"
1250 fi
1251
1252 archive="$build_archive"
1253 tf_build_root="$tf_root/build"
1254
1255 echo "Building Trusted Firmware ($mode) ..." |& log_separator
1256
1257 # Call pre-build hook
1258 call_hook pre_tf_build
1259
1260 build_tf
1261
1262 # Call post-build hook
1263 call_hook post_tf_build
1264
1265 # Pre-archive hook
1266 call_hook pre_tf_archive
1267
1268 from="$tf_build_root" to="$archive" collect_build_artefacts
1269
1270 # Post-archive hook
1271 call_hook post_tf_archive
1272
1273 call_hook fetch_tf_resource
1274 call_hook post_fetch_tf_resource
1275
1276 # Clear any local changes made by applied patches
1277 undo_tf_patches
1278
1279 echo "##########"
1280 )
1281 fi
1282
1283 echo
1284 echo
1285done
1286
1287call_hook pre_package
1288
1289call_hook post_package
1290
1291if upon "$jenkins_run" && upon "$artefacts_receiver" && [ -d "artefacts" ]; then
Zelalem219df412020-05-17 19:21:20 -05001292 source "$CI_ROOT/script/send_artefacts.sh" "artefacts"
Fathi Boudra422bf772019-12-02 11:10:16 +02001293fi
1294
1295echo
1296echo "Done"