blob: bf025b8a848dcb784459706e3ef950f3b0e012ab [file] [log] [blame]
Leonardo Sandoval9dfdd1b2020-08-06 17:08:11 -05001#!/usr/bin/env bash
Fathi Boudra422bf772019-12-02 11:10:16 +02002#
Yann Gautier7e9d6cf2023-03-08 14:24:38 +01003# Copyright (c) 2019-2023 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}"
Zelalem219df412020-05-17 19:21:20 -050023cc_root="${cc_root:-$ccpathspec}"
Olivier Deprez0a9a3482019-12-16 14:10:31 +010024spm_root="${spm_root:-$workspace/spm}"
Zelalem219df412020-05-17 19:21:20 -050025
Fathi Boudra422bf772019-12-02 11:10:16 +020026# Refspecs
27tf_refspec="$TF_REFSPEC"
28tftf_refspec="$TFTF_REFSPEC"
Olivier Deprez0a9a3482019-12-16 14:10:31 +010029spm_refspec="$SPM_REFSPEC"
Fathi Boudra422bf772019-12-02 11:10:16 +020030
31test_config="${TEST_CONFIG:?}"
32test_group="${TEST_GROUP:?}"
33build_configs="${BUILD_CONFIG:?}"
34run_config="${RUN_CONFIG:?}"
Zelalem219df412020-05-17 19:21:20 -050035cc_config="${CC_ENABLE:-}"
Fathi Boudra422bf772019-12-02 11:10:16 +020036
37archive="$artefacts"
38build_log="$artefacts/build.log"
39fiptool="$tf_root/tools/fiptool/fiptool"
40cert_create="$tf_root/tools/cert_create/cert_create"
41
42# Validate $bin_mode
43case "$bin_mode" in
44 "" | debug | release)
45 ;;
46 *)
47 die "Invalid value for bin_mode: $bin_mode"
48 ;;
49esac
50
51# File to save any environem
52hook_env_file="$(mktempfile)"
53
54# Check if a config is valid
55config_valid() {
56 local config="${1?}"
57 if [ -z "$config" ] || [ "$(basename "$config")" = "nil" ]; then
58 return 1
59 fi
60
61 return 0
62}
63
64# Echo from a build wrapper. Print to descriptor 3 that's opened by the build
65# function.
66echo_w() {
67 echo $echo_flags "$@" >&3
68}
69
70# Print a separator to the log file. Intended to be used at the tail end of a pipe
71log_separator() {
72 {
73 echo
74 echo "----------"
75 } >> "$build_log"
76
77 tee -a "$build_log"
78
79 {
80 echo "----------"
81 echo
82 } >> "$build_log"
83}
84
85# Call function $1 if it's defined
86call_func() {
87 if type "${1:?}" &>/dev/null; then
88 echo
89 echo "> ${2:?}:$1()"
90 eval "$1"
91 echo "< $2:$1()"
92 fi
93}
94
Paul Sokolovsky0027af42024-08-15 21:54:00 +030095# Retry a command a number of times if it fails. Intended for I/O commands
96# in a CI environment which may be flaky.
97function retry() {
98 for i in $(seq 1 3); do
99 if "$@"; then
100 return 0
101 fi
102 sleep $(( i * 5 ))
103 done
104 return 1
105}
106
Fathi Boudra422bf772019-12-02 11:10:16 +0200107# Call hook $1 in all chosen fragments if it's defined. Hooks are invoked from
108# within a subshell, so any variables set within a hook are lost. Should a
109# variable needs to be set from within a hook, the function 'set_hook_var'
110# should be used
111call_hook() {
112 local func="$1"
113 local config_fragment
114
115 [ -z "$func" ] && return 0
116
Paul Sokolovskye9962cd2021-12-17 18:39:40 +0300117 echo "=== Calling hooks: $1 ==="
118
Fathi Boudra422bf772019-12-02 11:10:16 +0200119 : >"$hook_env_file"
120
121 if [ "$run_config_candiates" ]; then
122 for config_fragment in $run_config_candiates; do
123 (
124 source "$ci_root/run_config/$config_fragment"
125 call_func "$func" "$config_fragment"
126 )
127 done
128 fi
129
130 # Also source test config file
131 (
132 unset "$func"
133 source "$test_config_file"
134 call_func "$func" "$(basename $test_config_file)"
135 )
136
137 # Have any variables set take effect
138 source "$hook_env_file"
Paul Sokolovskye9962cd2021-12-17 18:39:40 +0300139
140 echo "=== End calling hooks: $1 ==="
Fathi Boudra422bf772019-12-02 11:10:16 +0200141}
142
143# Set a variable from within a hook
144set_hook_var() {
145 echo "export $1=\"${2?}\"" >> "$hook_env_file"
146}
147
148# Append to an array from within a hook
149append_hook_var() {
150 echo "export $1+=\"${2?}\"" >> "$hook_env_file"
151}
152
153# Have the main build script source a file
154source_later() {
155 echo "source ${1?}" >> "$hook_env_file"
156}
157
158# Setup TF build wrapper function by pointing to a script containing a function
159# that will be called with the TF build commands.
160setup_tf_build_wrapper() {
161 source_later "$ci_root/script/${wrapper?}_wrapper.sh"
162 set_hook_var "tf_build_wrapper" "${wrapper}_wrapper"
163 echo "Setup $wrapper build wrapper."
164}
165
166# Collect .bin files for archiving
167collect_build_artefacts() {
168 if [ ! -d "${from:?}" ]; then
169 return
170 fi
171
Yann Gautier7e9d6cf2023-03-08 14:24:38 +0100172 if ! find "$from" \( -name "*.bin" -o -name '*.elf' -o -name '*.dtb' -o -name '*.axf' -o -name '*.stm32' \) -exec cp -t "${to:?}" '{}' +; then
Fathi Boudra422bf772019-12-02 11:10:16 +0200173 echo "You probably are running local CI on local repositories."
174 echo "Did you set 'dont_clean' but forgot to run 'distclean'?"
175 die
176 fi
177}
178
Manish Pandey1e7be852020-11-09 16:04:48 +0000179# Collect SPM/hafnium artefacts with "secure_" appended to the files
180# generated for SPM(secure hafnium).
181collect_spm_artefacts() {
Madhukar Pappireddyc683cf62021-11-01 14:38:32 -0500182 if [ -d "${non_secure_from:?}" ]; then
183 find "$non_secure_from" \( -name "*.bin" -o -name '*.elf' \) -exec cp -t "${to:?}" '{}' +
Manish Pandey1e7be852020-11-09 16:04:48 +0000184 fi
185
Madhukar Pappireddyc683cf62021-11-01 14:38:32 -0500186 if [ -d "${secure_from:?}" ]; then
187 for f in $(find "$secure_from" \( -name "*.bin" -o -name '*.elf' \)); do cp -- "$f" "${to:?}"/secure_$(basename $f); done
188 fi
Manish Pandey1e7be852020-11-09 16:04:48 +0000189}
190
Javier Almansa Sobrino412d3612020-05-22 17:53:12 +0100191# Map the UART ID used for expect with the UART descriptor and port
192# used by the FPGA automation tools.
193map_uart() {
194 local port="${port:?}"
195 local descriptor="${descriptor:?}"
196 local baudrate="${baudrate:?}"
197 local run_root="${archive:?}/run"
198
199 local uart_dir="$run_root/uart${uart:?}"
200 mkdir -p "$uart_dir"
201
202 echo "$port" > "$uart_dir/port"
203 echo "$descriptor" > "$uart_dir/descriptor"
204 echo "$baudrate" > "$uart_dir/baudrate"
205
206 echo "UART${uart} mapped to port ${port} with descriptor ${descriptor} and baudrate ${baudrate}"
207}
208
Fathi Boudra422bf772019-12-02 11:10:16 +0200209# Arrange environment varibles to be set when expect scripts are launched
210set_expect_variable() {
211 local var="${1:?}"
212 local val="${2?}"
213
214 local run_root="${archive:?}/run"
215 local uart_dir="$run_root/uart${uart:?}"
216 mkdir -p "$uart_dir"
217
218 env_file="$uart_dir/env" quote="1" emit_env "$var" "$val"
219 echo "UART$uart: env has $@"
220}
221
222# Place the binary package a pointer to expect script, and its parameters
223track_expect() {
224 local file="${file:?}"
225 local timeout="${timeout-600}"
226 local run_root="${archive:?}/run"
227
228 local uart_dir="$run_root/uart${uart:?}"
229 mkdir -p "$uart_dir"
230
231 echo "$file" > "$uart_dir/expect"
232 echo "$timeout" > "$uart_dir/timeout"
Paul Sokolovskybfa8bab2023-01-25 19:34:51 +0700233 if [ -n "$lava_timeout" ]; then
234 set_run_env "lava_timeout" "$lava_timeout"
235 fi
Fathi Boudra422bf772019-12-02 11:10:16 +0200236
Paul Sokolovskybfa8bab2023-01-25 19:34:51 +0700237 echo "UART$uart to be tracked with $file; timeout ${timeout}s; lava_timeout ${lava_timeout:-N/A}s"
Fathi Boudra422bf772019-12-02 11:10:16 +0200238
Chris Kayfab6edc2022-11-17 19:18:32 +0000239 if [ ! -z "${port}" ]; then
240 echo "${port}" > "$uart_dir/port"
241 fi
242
Fathi Boudra422bf772019-12-02 11:10:16 +0200243 # The run script assumes UART0 to be primary. If we're asked to set any
244 # other UART to be primary, set a run environment variable to signal
245 # that to the run script
246 if upon "$set_primary"; then
247 echo "Primary UART set to UART$uart."
248 set_run_env "primary_uart" "$uart"
249 fi
Madhukar Pappireddy1e953722021-11-08 15:23:02 -0600250
251 # UART used by payload(such as tftf, Linux) may not be the same as the
252 # primary UART. Set a run environment variable to track the payload
253 # UART which is tracked to check if the test has finished sucessfully.
254 if upon "$set_payload_uart"; then
255 echo "Payload uses UART$uart."
256 set_run_env "payload_uart" "$uart"
257 fi
Fathi Boudra422bf772019-12-02 11:10:16 +0200258}
259
260# Extract a FIP in $1 using fiptool
261extract_fip() {
262 local fip="$1"
263
264 if is_url "$1"; then
265 url="$1" fetch_file
266 fip="$(basename "$1")"
267 fi
268
269 "$fiptool" unpack "$fip"
270 echo "Extracted FIP: $fip"
271}
272
273# Report build failure by printing a the tail end of build log. Archive the
274# build log for later inspection
275fail_build() {
276 local log_path
277
278 if upon "$jenkins_run"; then
279 log_path="$BUILD_URL/artifact/artefacts/build.log"
280 else
281 log_path="$build_log"
282 fi
283
284 echo
Leonardo Sandovalae97eda2020-11-12 10:07:03 -0600285 echo "Build failed! Full build log below:"
Fathi Boudra422bf772019-12-02 11:10:16 +0200286 echo "[...]"
287 echo
Leonardo Sandovalae97eda2020-11-12 10:07:03 -0600288 cat "$build_log"
Fathi Boudra422bf772019-12-02 11:10:16 +0200289 echo
290 echo "See $log_path for full output"
291 echo
292 cp -t "$archive" "$build_log"
293 exit 1;
294}
295
296# Build a FIP with supplied arguments
297build_fip() {
298 (
299 echo "Building FIP with arguments: $@"
300 local tf_env="$workspace/tf.env"
301
302 if [ -f "$tf_env" ]; then
303 set -a
304 source "$tf_env"
305 set +a
306 fi
307
308 make -C "$tf_root" $(cat "$tf_config_file") DEBUG="$DEBUG" V=1 "$@" \
309 ${fip_targets:-fip} &>>"$build_log" || fail_build
310 )
311}
312
Sandrine Bailleux189fdb32023-10-20 13:41:22 +0200313# Build any extra rule from TF-A makefile with supplied arguments.
314#
315# This is useful in case you need to build something else than firmware binaries
316# or the FIP.
317build_tf_extra() {
318 (
319 tf_extra_rules=${tf_extra_rules:?}
320 echo "Building extra TF rule(s): $tf_extra_rules"
321 echo " Arguments: $@"
322
323 local tf_env="$workspace/tf.env"
324
325 if [ -f "$tf_env" ]; then
326 set -a
327 source "$tf_env"
328 set +a
329 fi
330
331 make -C "$tf_root" $(cat "$tf_config_file") DEBUG="$DEBUG" V=1 "$@" \
332 ${tf_extra_rules} &>>"$build_log" || fail_build
333 )
334}
335
Fathi Boudra422bf772019-12-02 11:10:16 +0200336fip_update() {
337 # Before the update process, check if the given image is supported by
338 # the fiptool. It's assumed that both fiptool and cert_create move in
339 # tandem, and therfore, if one has support, the other has it too.
340 if ! "$fiptool" update 2>&1 | grep -qe "\s\+--${bin_name:?}"; then
341 return 1
342 fi
343
344 if not_upon "$(get_tf_opt TRUSTED_BOARD_BOOT)"; then
345 echo "Updating FIP image: $bin_name"
346 # Update HW config. Without TBBR, it's only a matter of using
347 # the update sub-command of fiptool
348 "$fiptool" update "--$bin_name" "${src:-}" \
349 "$archive/fip.bin"
350 else
351 echo "Updating FIP image (TBBR): $bin_name"
352 # With TBBR, we need to unpack, re-create certificates, and then
353 # recreate the FIP.
354 local fip_dir="$(mktempdir)"
355 local bin common_args stem
356 local rot_key="$(get_tf_opt ROT_KEY)"
357
358 rot_key="${rot_key:?}"
359 if ! is_abs "$rot_key"; then
360 rot_key="$tf_root/$rot_key"
361 fi
362
363 # Arguments only for cert_create
364 local cert_args="-n"
365 cert_args+=" --tfw-nvctr ${nvctr:-31}"
366 cert_args+=" --ntfw-nvctr ${nvctr:-223}"
367 cert_args+=" --key-alg ${KEY_ALG:-rsa}"
368 cert_args+=" --rot-key $rot_key"
369
370 local dyn_config_opts=(
Zelalem1af7a7b2020-08-04 17:34:32 -0500371 "fw-config"
Fathi Boudra422bf772019-12-02 11:10:16 +0200372 "hw-config"
373 "tb-fw-config"
374 "nt-fw-config"
375 "soc-fw-config"
376 "tos-fw-config"
377 )
378
379 # Binaries without key certificates
380 declare -A has_no_key_cert
381 for bin in "tb-fw" "${dyn_config_opts[@]}"; do
382 has_no_key_cert["$bin"]="1"
383 done
384
385 # Binaries without certificates
386 declare -A has_no_cert
387 for bin in "hw-config" "${dyn_config_opts[@]}"; do
388 has_no_cert["$bin"]="1"
389 done
390
391 pushd "$fip_dir"
392
393 # Unpack FIP
394 "$fiptool" unpack "$archive/fip.bin" &>>"$build_log"
395
396 # Remove all existing certificates
397 rm -f *-cert.bin
398
399 # Copy the binary to be updated
400 cp -f "$src" "${bin_name}.bin"
401
402 # FIP unpack dumps binaries with the same name as the option
403 # used to pack it; likewise for certificates. Reverse-engineer
404 # the command line from the binary output.
405 common_args="--trusted-key-cert trusted_key.crt"
406 for bin in *.bin; do
407 stem="${bin%%.bin}"
408 common_args+=" --$stem $bin"
409 if not_upon "${has_no_cert[$stem]}"; then
410 common_args+=" --$stem-cert $stem.crt"
411 fi
412 if not_upon "${has_no_key_cert[$stem]}"; then
413 common_args+=" --$stem-key-cert $stem-key.crt"
414 fi
415 done
416
417 # Create certificates
418 "$cert_create" $cert_args $common_args &>>"$build_log"
419
420 # Recreate and archive FIP
421 "$fiptool" create $common_args "fip.bin" &>>"$build_log"
422 archive_file "fip.bin"
423
424 popd
425 fi
426}
427
428# Update hw-config in FIP, and remove the original DTB afterwards.
429update_fip_hw_config() {
430 # The DTB needs to be loaded by the model (and not updated in the FIP)
Madhukar Pappireddy9062ebf2021-03-02 17:07:06 -0600431 # in configs:
432 # 1. Where BL2 isn't present
433 # 2. Where we boot to Linux directly as BL33
Fathi Boudra422bf772019-12-02 11:10:16 +0200434 case "1" in
435 "$(get_tf_opt RESET_TO_BL31)" | \
Madhukar Pappireddy9062ebf2021-03-02 17:07:06 -0600436 "$(get_tf_opt ARM_LINUX_KERNEL_AS_BL33)" | \
Fathi Boudra422bf772019-12-02 11:10:16 +0200437 "$(get_tf_opt RESET_TO_SP_MIN)" | \
Maksims Svecovs7a0da522023-03-06 16:28:27 +0000438 "$(get_tf_opt RESET_TO_BL2)")
Fathi Boudra422bf772019-12-02 11:10:16 +0200439 return 0;;
440 esac
441
442 if bin_name="hw-config" src="$archive/dtb.bin" fip_update; then
443 # Remove the DTB so that model won't load it
444 rm -f "$archive/dtb.bin"
445 fi
446}
447
Fathi Boudra422bf772019-12-02 11:10:16 +0200448get_tftf_opt() {
449 (
450 name="${1:?}"
451 if config_valid "$tftf_config_file"; then
452 source "$tftf_config_file"
453 echo "${!name}"
454 fi
455 )
456}
457
458get_tf_opt() {
459 (
460 name="${1:?}"
461 if config_valid "$tf_config_file"; then
462 source "$tf_config_file"
463 echo "${!name}"
464 fi
465 )
466}
467
468build_tf() {
469 (
470 env_file="$workspace/tf.env"
471 config_file="${tf_build_config:-$tf_config_file}"
472
473 # Build fiptool and all targets by default
Harrison Mutai32de9d02023-06-12 14:23:37 +0100474 build_targets="${tf_build_targets:-fiptool all}"
Fathi Boudra422bf772019-12-02 11:10:16 +0200475
476 source "$config_file"
477
478 # If it is a TBBR build, extract the MBED TLS library from archive
Manish V Badarkhe8f125012021-12-21 05:47:52 +0000479 if [ "$(get_tf_opt TRUSTED_BOARD_BOOT)" = 1 ] ||
Manish V Badarkhef43e3f52022-06-21 20:37:25 +0100480 [ "$(get_tf_opt MEASURED_BOOT)" = 1 ] ||
481 [ "$(get_tf_opt DRTM_SUPPORT)" = 1 ]; then
Fathi Boudra422bf772019-12-02 11:10:16 +0200482 mbedtls_dir="$workspace/mbedtls"
483 if [ ! -d "$mbedtls_dir" ]; then
484 mbedtls_ar="$workspace/mbedtls.tar.gz"
485
486 url="$mbedtls_archive" saveas="$mbedtls_ar" fetch_file
487 mkdir "$mbedtls_dir"
Leonardo Sandovalec9e16c2020-09-09 14:32:40 -0500488 extract_tarball $mbedtls_ar $mbedtls_dir --strip-components=1
Fathi Boudra422bf772019-12-02 11:10:16 +0200489 fi
490
491 emit_env "MBEDTLS_DIR" "$mbedtls_dir"
492 fi
Jimmy Brisson0d5e12c2023-05-16 14:51:51 -0500493 if [ "$(get_tf_opt PLATFORM_TEST)" = "tfm-testsuite" ] &&
494 not_upon "${TF_M_TESTS_PATH}"; then
495 emit_env "TF_M_TESTS_PATH" "$WORKSPACE/tf-m-tests"
496 fi
497 if [ "$(get_tf_opt PLATFORM_TEST)" = "tfm-testsuite" ] &&
498 not_upon "${TF_M_EXTRAS_PATH}"; then
499 emit_env "TF_M_EXTRAS_PATH" "$WORKSPACE/tf-m-extras"
500 fi
Fathi Boudra422bf772019-12-02 11:10:16 +0200501 if [ -f "$env_file" ]; then
502 set -a
503 source "$env_file"
504 set +a
505 fi
506
Harrison Mutai013f6332022-02-16 16:06:33 +0000507 if is_arm_jenkins_env || upon "$local_ci"; then
508 path_list=(
509 "$llvm_dir/bin"
510 )
511 extend_path "PATH" "path_list"
512 fi
513
Fathi Boudra422bf772019-12-02 11:10:16 +0200514 cd "$tf_root"
515
516 # Always distclean when running on Jenkins. Skip distclean when running
517 # locally and explicitly requested.
518 if upon "$jenkins_run" || not_upon "$dont_clean"; then
519 make distclean &>>"$build_log" || fail_build
520 fi
521
522 # Log build command line. It is left unfolded on purpose to assist
523 # copying to clipboard.
524 cat <<EOF | log_separator >/dev/null
525
526Build command line:
527 $tf_build_wrapper make $make_j_opts $(cat "$config_file" | tr '\n' ' ') DEBUG=$DEBUG V=1 $build_targets
528
Paul Sokolovsky7f71b072023-10-16 12:59:09 +0300529CC version:
530$(${CC-${CROSS_COMPILE}gcc} -v 2>&1)
Fathi Boudra422bf772019-12-02 11:10:16 +0200531EOF
532
Javier Almansa Sobrinoe8363182020-11-10 16:40:53 +0000533 if not_upon "$local_ci"; then
534 connect_debugger=0
535 fi
536
Fathi Boudra422bf772019-12-02 11:10:16 +0200537 # Build TF. Since build output is being directed to the build log, have
538 # descriptor 3 point to the current terminal for build wrappers to vent.
Harrison Mutai6361dbe2023-02-16 14:12:40 +0000539 $tf_build_wrapper poetry run make $make_j_opts $(cat "$config_file") \
Javier Almansa Sobrinoe8363182020-11-10 16:40:53 +0000540 DEBUG="$DEBUG" V=1 SPIN_ON_BL1_EXIT="$connect_debugger" \
Fathi Boudra422bf772019-12-02 11:10:16 +0200541 $build_targets 3>&1 &>>"$build_log" || fail_build
Harrison Mutai32de9d02023-06-12 14:23:37 +0100542
Harrison Mutai6dafb5f2023-07-18 15:21:48 +0100543 if [ "$build_targets" != "doc" ]; then
544 poetry run memory -sr "$tf_build_path" 2>&1 | tee -a "$build_log"
545 fi
Fathi Boudra422bf772019-12-02 11:10:16 +0200546 )
547}
548
549build_tftf() {
550 (
551 config_file="${tftf_build_config:-$tftf_config_file}"
552
553 # Build tftf target by default
554 build_targets="${tftf_build_targets:-all}"
555
556 source "$config_file"
557
558 cd "$tftf_root"
559
560 # Always distclean when running on Jenkins. Skip distclean when running
561 # locally and explicitly requested.
562 if upon "$jenkins_run" || not_upon "$dont_clean"; then
563 make distclean &>>"$build_log" || fail_build
564 fi
565
566 # TFTF build system cannot reliably deal with -j option, so we avoid
567 # using that.
568
569 # Log build command line
570 cat <<EOF | log_separator >/dev/null
571
572Build command line:
573 make $(cat "$config_file" | tr '\n' ' ') DEBUG=$DEBUG V=1 $build_targets
574
575EOF
576
577 make $(cat "$config_file") DEBUG="$DEBUG" V=1 \
578 $build_targets &>>"$build_log" || fail_build
579 )
580}
581
Zelalem219df412020-05-17 19:21:20 -0500582build_cc() {
583# Building code coverage plugin
584 ARM_DIR=/arm
585 pvlibversion=$(/arm/devsys-tools/abs/detag "SysGen:PVModelLib:$model_version::trunk")
586 PVLIB_HOME=$warehouse/SysGen/PVModelLib/$model_version/${pvlibversion}/external
587 if [ -n "$(find "$ARM_DIR" -maxdepth 0 -type d -empty 2>/dev/null)" ]; then
588 echo "Error: Arm warehouse not mounted. Please mount the Arm warehouse to your /arm local folder"
589 exit -1
590 fi # Error if arm warehouse not found
591 cd "$ccpathspec/scripts/tools/code_coverage/fastmodel_baremetal/bmcov"
592
593 make -C model-plugin PVLIB_HOME=$PVLIB_HOME &>>"$build_log"
594}
595
Olivier Deprez0a9a3482019-12-16 14:10:31 +0100596build_spm() {
597 (
598 env_file="$workspace/spm.env"
599 config_file="${spm_build_config:-$spm_config_file}"
600
601 source "$config_file"
602
603 if [ -f "$env_file" ]; then
604 set -a
605 source "$env_file"
606 set +a
607 fi
608
609 cd "$spm_root"
610
611 # Always clean when running on Jenkins. Skip clean when running
612 # locally and explicitly requested.
613 if upon "$jenkins_run" || not_upon "$dont_clean"; then
614 # make clean fails on a fresh repo where the project has not
615 # yet been built. Hence only clean if out/reference directory
616 # already exists.
617 if [ -d "out/reference" ]; then
618 make clean &>>"$build_log" || fail_build
619 fi
620 fi
621
622 # Log build command line. It is left unfolded on purpose to assist
623 # copying to clipboard.
624 cat <<EOF | log_separator >/dev/null
625
626Build command line:
627 make $make_j_opts $(cat "$config_file" | tr '\n' ' ')
628
629EOF
630
631 # Build SPM. Since build output is being directed to the build log, have
632 # descriptor 3 point to the current terminal for build wrappers to vent.
Olivier Deprezf34d1d32021-10-11 09:45:32 +0200633 export PATH=$PWD/prebuilts/linux-x64/clang/bin:$PWD/prebuilts/linux-x64/dtc:$PATH
634
Olivier Deprez0a9a3482019-12-16 14:10:31 +0100635 make $make_j_opts $(cat "$config_file") 3>&1 &>>"$build_log" \
636 || fail_build
637 )
638}
Zelalem219df412020-05-17 19:21:20 -0500639
Fathi Boudra422bf772019-12-02 11:10:16 +0200640# Set metadata for the whole package so that it can be used by both Jenkins and
641# shell
642set_package_var() {
643 env_file="$artefacts/env" emit_env "$@"
644}
645
646set_tf_build_targets() {
647 echo "Set build target to '${targets:?}'"
648 set_hook_var "tf_build_targets" "$targets"
649}
650
651set_tftf_build_targets() {
652 echo "Set build target to '${targets:?}'"
653 set_hook_var "tftf_build_targets" "$targets"
654}
655
Olivier Deprez0a9a3482019-12-16 14:10:31 +0100656set_spm_build_targets() {
657 echo "Set build target to '${targets:?}'"
658 set_hook_var "spm_build_targets" "$targets"
659}
660
Daniel Boulbyb8d2a462022-03-07 13:55:25 +0000661set_spm_out_dir() {
662 echo "Set SPMC binary build to '${out_dir:?}'"
663 set_hook_var "spm_secure_out_dir" "$out_dir"
664}
Fathi Boudra422bf772019-12-02 11:10:16 +0200665# Look under $archive directory for known files such as blX images, kernel, DTB,
666# initrd etc. For each known file foo, if foo.bin exists, then set variable
667# foo_bin to the path of the file. Make the path relative to the workspace so as
668# to remove any @ characters, which Jenkins inserts for parallel runs. If the
669# file doesn't exist, unset its path.
670set_default_bin_paths() {
671 local image image_name image_path path
672 local archive="${archive:?}"
673 local set_vars
674 local var
675
676 pushd "$archive"
677
678 for file in *.bin; do
679 # Get a shell variable from the file's stem
680 var_name="${file%%.*}_bin"
681 var_name="$(echo "$var_name" | sed -r 's/[^[:alnum:]]/_/g')"
682
683 # Skip setting the variable if it's already
684 if [ "${!var_name}" ]; then
685 echo "Note: not setting $var_name; already set to ${!var_name}"
686 continue
687 else
688 set_vars+="$var_name "
689 fi
690
691 eval "$var_name=$file"
692 done
693
694 echo "Binary paths set for: "
695 {
696 for var in $set_vars; do
697 echo -n "\$$var "
698 done
699 } | fmt -80 | sed 's/^/ /'
700 echo
701
702 popd
703}
704
705gen_model_params() {
706 local model_param_file="$archive/model_params"
Javier Almansa Sobrinoe8363182020-11-10 16:40:53 +0000707 [ "$connect_debugger" ] && [ "$connect_debugger" -eq 1 ] && wait_debugger=1
Fathi Boudra422bf772019-12-02 11:10:16 +0200708
709 set_default_bin_paths
710 echo "Generating model parameter for $model..."
711 source "$ci_root/model/${model:?}.sh"
712 archive_file "$model_param_file"
713}
714
715set_model_path() {
716 set_run_env "model_path" "${1:?}"
717}
718
Zelalem1af7a7b2020-08-04 17:34:32 -0500719set_model_env() {
720 local var="${1:?}"
721 local val="${2?}"
722 local run_root="${archive:?}/run"
723
724 mkdir -p "$run_root"
725 echo "export $var=$val" >> "$run_root/model_env"
726}
Fathi Boudra422bf772019-12-02 11:10:16 +0200727set_run_env() {
728 local var="${1:?}"
729 local val="${2?}"
730 local run_root="${archive:?}/run"
731
732 mkdir -p "$run_root"
733 env_file="$run_root/env" quote="1" emit_env "$var" "$val"
734}
735
736show_head() {
737 # Display HEAD descripton
738 pushd "$1"
739 git show --quiet --no-color | sed 's/^/ > /g'
740 echo
741 popd
742}
743
744# Choose debug binaries to run; by default, release binaries are chosen to run
745use_debug_bins() {
746 local run_root="${archive:?}/run"
747
748 echo "Choosing debug binaries for execution"
749 set_package_var "BIN_MODE" "debug"
750}
751
752assert_can_git_clone() {
753 local name="${1:?}"
754 local dir="${!name}"
755
756 # If it doesn't exist, it can be cloned into
757 if [ ! -e "$dir" ]; then
758 return 0
759 fi
760
761 # If it's a directory, it must be a Git clone already
762 if [ -d "$dir" ] && [ -d "$dir/.git" ]; then
763 # No need to clone again
764 echo "Using existing git clone for $name: $dir"
765 return 1
766 fi
767
768 die "Path $dir exists but is not a git clone"
769}
770
771clone_repo() {
772 if ! is_url "${clone_url?}"; then
773 # For --depth to take effect on local paths, it needs to use the
774 # file:// scheme.
775 clone_url="file://$clone_url"
776 fi
777
778 git clone -q --depth 1 "$clone_url" "${where?}"
779 if [ "$refspec" ]; then
780 pushd "$where"
781 git fetch -q --depth 1 origin "$refspec"
782 git checkout -q FETCH_HEAD
783 popd
784 fi
785}
786
787build_unstable() {
788 echo "--BUILD UNSTABLE--" | tee -a "$build_log"
789}
790
791undo_patch_record() {
792 if [ ! -f "${patch_record:?}" ]; then
793 return
794 fi
795
796 # Undo patches in reverse
797 echo
798 for patch_name in $(tac "$patch_record"); do
799 echo "Undoing $patch_name..."
800 if ! git apply -R "$ci_root/patch/$patch_name"; then
801 if upon "$local_ci"; then
802 echo
803 echo "Your local directory may have been dirtied."
804 echo
805 fi
806 fail_build
807 fi
808 done
809
810 rm -f "$patch_record"
811}
812
813undo_local_patches() {
814 pushd "$tf_root"
815 patch_record="$tf_patch_record" undo_patch_record
816 popd
817
818 if [ -d "$tftf_root" ]; then
819 pushd "$tftf_root"
820 patch_record="$tftf_patch_record" undo_patch_record
821 popd
822 fi
823}
824
825undo_tftf_patches() {
826 pushd "$tftf_root"
827 patch_record="$tftf_patch_record" undo_patch_record
828 popd
829}
830
831undo_tf_patches() {
832 pushd "$tf_root"
833 patch_record="$tf_patch_record" undo_patch_record
834 popd
835}
836
837apply_patch() {
838 # If skip_patches is set, the developer has applied required patches
839 # manually. They probably want to keep them applied for debugging
840 # purposes too. This means we don't have to apply/revert them as part of
841 # build process.
842 if upon "$skip_patches"; then
843 echo "Skipped applying ${1:?}..."
844 return 0
845 else
846 echo "Applying ${1:?}..."
847 fi
848
Sandrine Bailleux4cb8c222023-09-13 13:48:15 +0200849 if git apply --reverse --check < "$ci_root/patch/$1" 2> /dev/null; then
Jimmy Brissonf134e4c2023-03-22 13:20:20 -0500850 echo "Skipping already applied ${1:?}"
851 return 0
852 fi
853
Fathi Boudra422bf772019-12-02 11:10:16 +0200854 if git apply < "$ci_root/patch/$1"; then
855 echo "$1" >> "${patch_record:?}"
856 else
857 if upon "$local_ci"; then
858 undo_local_patches
859 fi
860 fail_build
861 fi
862}
863
864apply_tftf_patch() {
865 pushd "$tftf_root"
866 patch_record="$tftf_patch_record" apply_patch "$1"
867 popd
868}
869
870apply_tf_patch() {
871 pushd "$tf_root"
872 patch_record="$tf_patch_record" apply_patch "$1"
873 popd
874}
875
876# Clear workspace for a local run
877if not_upon "$jenkins_run"; then
878 rm -rf "$workspace"
879
880 # Clear residue from previous runs
881 rm -rf "$archive"
882fi
883
884mkdir -p "$workspace"
885mkdir -p "$archive"
886set_package_var "TEST_CONFIG" "$test_config"
887
888{
889echo
890echo "CONFIGURATION: $test_group/$test_config"
891echo
892} |& log_separator
893
894tf_config="$(echo "$build_configs" | awk -F, '{print $1}')"
895tftf_config="$(echo "$build_configs" | awk -F, '{print $2}')"
Chris Kay3a2adef2025-08-04 19:56:35 +0100896spm_config="$(echo "$build_configs" | awk -F, '{print $3}')"
Fathi Boudra422bf772019-12-02 11:10:16 +0200897
898test_config_file="$ci_root/group/$test_group/$test_config"
899
900tf_config_file="$ci_root/tf_config/$tf_config"
901tftf_config_file="$ci_root/tftf_config/$tftf_config"
Olivier Deprez0a9a3482019-12-16 14:10:31 +0100902spm_config_file="$ci_root/spm_config/$spm_config"
Fathi Boudra422bf772019-12-02 11:10:16 +0200903
904# File that keeps track of applied patches
905tf_patch_record="$workspace/tf_patches"
906tftf_patch_record="$workspace/tftf_patches"
907
908pushd "$workspace"
909
910if ! config_valid "$tf_config"; then
911 tf_config=
912else
913 echo "Trusted Firmware config:"
914 echo
915 sort "$tf_config_file" | sed '/^\s*$/d;s/^/\t/'
916 echo
917fi
918
919if ! config_valid "$tftf_config"; then
920 tftf_config=
921else
922 echo "Trusted Firmware TF config:"
923 echo
924 sort "$tftf_config_file" | sed '/^\s*$/d;s/^/\t/'
925 echo
926fi
927
Olivier Deprez0a9a3482019-12-16 14:10:31 +0100928if ! config_valid "$spm_config"; then
929 spm_config=
930else
931 echo "SPM config:"
932 echo
933 sort "$spm_config_file" | sed '/^\s*$/d;s/^/\t/'
Zelalem219df412020-05-17 19:21:20 -0500934 echo
935fi
936
Fathi Boudra422bf772019-12-02 11:10:16 +0200937if ! config_valid "$run_config"; then
938 run_config=
939fi
940
941if [ "$tf_config" ] && assert_can_git_clone "tf_root"; then
942 # If the Trusted Firmware repository has already been checked out, use
943 # that location. Otherwise, clone one ourselves.
944 echo "Cloning Trusted Firmware..."
945 clone_url="${TF_CHECKOUT_LOC:-$tf_src_repo_url}" where="$tf_root" \
946 refspec="$TF_REFSPEC" clone_repo &>>"$build_log"
947 show_head "$tf_root"
948fi
949
950if [ "$tftf_config" ] && assert_can_git_clone "tftf_root"; then
951 # If the Trusted Firmware TF repository has already been checked out,
952 # use that location. Otherwise, clone one ourselves.
953 echo "Cloning Trusted Firmware TF..."
954 clone_url="${TFTF_CHECKOUT_LOC:-$tftf_src_repo_url}" where="$tftf_root" \
955 refspec="$TFTF_REFSPEC" clone_repo &>>"$build_log"
956 show_head "$tftf_root"
957fi
958
Zelalem219df412020-05-17 19:21:20 -0500959if [ -n "$cc_config" ] ; then
960 if [ "$cc_config" -eq 1 ] && assert_can_git_clone "cc_root"; then
961 # Copy code coverage repository
962 echo "Cloning Code Coverage..."
963 git clone -q $cc_src_repo_url cc_plugin --depth 1 -b $cc_src_repo_tag > /dev/null
964 show_head "$cc_root"
965 fi
966fi
967
Daniel Boulby052f1bb2023-12-14 14:36:25 +0000968if [ "$spm_config" ] ; then
969 if assert_can_git_clone "spm_root"; then
970 # If the SPM repository has already been checked out, use
971 # that location. Otherwise, clone one ourselves.
972 echo "Cloning SPM..."
973 clone_url="${SPM_CHECKOUT_LOC:-$spm_src_repo_url}" \
974 where="$spm_root" refspec="$SPM_REFSPEC" \
975 clone_repo &>>"$build_log"
976 fi
Olivier Deprez0a9a3482019-12-16 14:10:31 +0100977
978 # Query git submodules
979 pushd "$spm_root"
Daniel Boulby052f1bb2023-12-14 14:36:25 +0000980 # Check if submodules need initialising
Paul Sokolovskyd9b23df2024-09-01 10:27:56 +0300981
982 # This handling is needed to reliably fetch submodules
983 # in CI environment.
984 for subm in $(git submodule status | awk '/^-/ {print $2}'); do
985 for i in $(seq 1 7); do
986 git submodule init $subm
987 if git submodule update $subm; then
988 break
989 fi
990 git submodule deinit --force $subm
991 echo "Retrying $subm"
992 sleep $((RANDOM % 10 + 5))
993 done
994 done
995
996 git submodule status
Olivier Deprez0a9a3482019-12-16 14:10:31 +0100997 popd
998
999 show_head "$spm_root"
1000fi
1001
Fathi Boudra422bf772019-12-02 11:10:16 +02001002if [ "$run_config" ]; then
1003 # Get candidates for run config
1004 run_config_candiates="$("$ci_root/script/gen_run_config_candidates.py" \
1005 "$run_config")"
1006 if [ -z "$run_config_candiates" ]; then
1007 die "No run config candidates!"
1008 else
1009 echo
1010 echo "Chosen fragments:"
1011 echo
1012 echo "$run_config_candiates" | sed 's/^\|\n/\t/g'
1013 echo
1014 fi
1015fi
1016
1017call_hook "test_setup"
1018echo
1019
1020if upon "$local_ci"; then
1021 # For local runs, since each config is tried in sequence, it's
1022 # advantageous to run jobs in parallel
1023 if [ "$make_j" ]; then
1024 make_j_opts="-j $make_j"
1025 else
1026 n_cores="$(getconf _NPROCESSORS_ONLN)" 2>/dev/null || true
1027 if [ "$n_cores" ]; then
1028 make_j_opts="-j $n_cores"
1029 fi
1030 fi
1031fi
1032
Harrison Mutai07043e92023-07-06 09:41:12 +01001033# Install python build dependencies
1034if is_arm_jenkins_env; then
1035 source "$ci_root/script/install_python_deps.sh"
1036fi
1037
Fathi Boudra422bf772019-12-02 11:10:16 +02001038modes="${bin_mode:-debug release}"
1039for mode in $modes; do
Paul Sokolovskye9962cd2021-12-17 18:39:40 +03001040 echo "===== Building package in mode: $mode ====="
Fathi Boudra422bf772019-12-02 11:10:16 +02001041 # Build with a temporary archive
1042 build_archive="$archive/$mode"
1043 mkdir "$build_archive"
1044
1045 if [ "$mode" = "debug" ]; then
Zelalem219df412020-05-17 19:21:20 -05001046 export bin_mode="debug"
Fathi Boudra422bf772019-12-02 11:10:16 +02001047 DEBUG=1
1048 else
Zelalem219df412020-05-17 19:21:20 -05001049 export bin_mode="release"
Fathi Boudra422bf772019-12-02 11:10:16 +02001050 DEBUG=0
1051 fi
1052
1053 # Perform builds in a subshell so as not to pollute the current and
1054 # subsequent builds' environment
1055
Zelalem219df412020-05-17 19:21:20 -05001056 if config_valid "$cc_config"; then
1057 # Build code coverage plugin
1058 build_cc
1059 fi
1060
Fathi Boudra422bf772019-12-02 11:10:16 +02001061 # TFTF build
1062 if config_valid "$tftf_config"; then
1063 (
1064 echo "##########"
1065
Manish V Badarkhe3bd3fea2020-11-08 15:17:00 +00001066 plat_utils="$(get_tf_opt PLAT_UTILS)"
1067 if [ -z ${plat_utils} ]; then
1068 # Source platform-specific utilities.
1069 plat="$(get_tftf_opt PLAT)"
1070 plat_utils="$ci_root/${plat}_utils.sh"
1071 else
1072 # Source platform-specific utilities by
1073 # using plat_utils name.
1074 plat_utils="$ci_root/${plat_utils}.sh"
1075 fi
1076
Fathi Boudra422bf772019-12-02 11:10:16 +02001077 if [ -f "$plat_utils" ]; then
1078 source "$plat_utils"
1079 fi
1080
1081 archive="$build_archive"
1082 tftf_build_root="$tftf_root/build"
1083
1084 echo "Building Trusted Firmware TF ($mode) ..." |& log_separator
1085
1086 # Call pre-build hook
1087 call_hook pre_tftf_build
1088
1089 build_tftf
1090
1091 from="$tftf_build_root" to="$archive" collect_build_artefacts
1092
1093 # Clear any local changes made by applied patches
1094 undo_tftf_patches
1095
1096 echo "##########"
1097 echo
1098 )
1099 fi
1100
Olivier Deprez0a9a3482019-12-16 14:10:31 +01001101 # SPM build
1102 if config_valid "$spm_config"; then
1103 (
1104 echo "##########"
1105
1106 # Get platform name from spm_config file
1107 plat="$(echo "$spm_config" | awk -F- '{print $1}')"
1108 plat_utils="$ci_root/${plat}_utils.sh"
1109 if [ -f "$plat_utils" ]; then
1110 source "$plat_utils"
1111 fi
1112
Daniel Boulbyb8d2a462022-03-07 13:55:25 +00001113 # Call pre-build hook
1114 call_hook pre_spm_build
1115
Manish Pandey1e7be852020-11-09 16:04:48 +00001116 # SPM build generates two sets of binaries, one for normal and other
1117 # for Secure world. We need both set of binaries for CI.
Olivier Deprez0a9a3482019-12-16 14:10:31 +01001118 archive="$build_archive"
Manish Pandey1e7be852020-11-09 16:04:48 +00001119 spm_build_root="$spm_root/out/reference/$spm_secure_out_dir"
1120 hafnium_build_root="$spm_root/out/reference/$spm_non_secure_out_dir"
Olivier Deprez0a9a3482019-12-16 14:10:31 +01001121
Daniel Boulbyb8d2a462022-03-07 13:55:25 +00001122 echo "spm_build_root is $spm_build_root"
Olivier Deprez0a9a3482019-12-16 14:10:31 +01001123 echo "Building SPM ($mode) ..." |& log_separator
1124
1125 # NOTE: mode has no effect on SPM build (for now), hence debug
1126 # mode is built but subsequent build using release mode just
1127 # goes through with "nothing to do".
1128 build_spm
1129
1130 # Show SPM/Hafnium binary details
Madhukar Pappireddyc683cf62021-11-01 14:38:32 -05001131 cksum $spm_build_root/hafnium.bin
1132
1133 # Some platforms only have secure configuration enabled. Hence,
1134 # non secure hanfnium binary might not be built.
1135 if [ -f $hafnium_build_root/hafnium.bin ]; then
1136 cksum $hafnium_build_root/hafnium.bin
1137 fi
Olivier Deprez0a9a3482019-12-16 14:10:31 +01001138
Manish Pandey1e7be852020-11-09 16:04:48 +00001139 secure_from="$spm_build_root" non_secure_from="$hafnium_build_root" to="$archive" collect_spm_artefacts
Olivier Deprez0a9a3482019-12-16 14:10:31 +01001140
1141 echo "##########"
1142 echo
1143 )
1144 fi
1145
Fathi Boudra422bf772019-12-02 11:10:16 +02001146 # TF build
1147 if config_valid "$tf_config"; then
1148 (
1149 echo "##########"
1150
Manish V Badarkhe3bd3fea2020-11-08 15:17:00 +00001151 plat_utils="$(get_tf_opt PLAT_UTILS)"
Madhukar Pappireddy2f284e12021-08-30 16:06:14 -05001152 export plat_variant="$(get_tf_opt TARGET_PLATFORM)"
1153
Manish V Badarkhe3bd3fea2020-11-08 15:17:00 +00001154 if [ -z ${plat_utils} ]; then
1155 # Source platform-specific utilities.
1156 plat="$(get_tf_opt PLAT)"
1157 plat_utils="$ci_root/${plat}_utils.sh"
1158 else
1159 # Source platform-specific utilities by
1160 # using plat_utils name.
1161 plat_utils="$ci_root/${plat_utils}.sh"
1162 fi
1163
Fathi Boudra422bf772019-12-02 11:10:16 +02001164 if [ -f "$plat_utils" ]; then
1165 source "$plat_utils"
1166 fi
1167
Chris Kaye5a486b2023-08-04 11:50:31 +00001168 fvp_tsram_size="$(get_tf_opt FVP_TRUSTED_SRAM_SIZE)"
1169 fvp_tsram_size="${fvp_tsram_size:-256}"
1170
Harrison Mutai6361dbe2023-02-16 14:12:40 +00001171 poetry -C "$tf_root" install --without doc
Chris Kayd0837902021-11-17 10:17:52 +00001172
Fathi Boudra422bf772019-12-02 11:10:16 +02001173 archive="$build_archive"
1174 tf_build_root="$tf_root/build"
1175
1176 echo "Building Trusted Firmware ($mode) ..." |& log_separator
1177
1178 # Call pre-build hook
1179 call_hook pre_tf_build
1180
1181 build_tf
1182
1183 # Call post-build hook
1184 call_hook post_tf_build
1185
1186 # Pre-archive hook
1187 call_hook pre_tf_archive
1188
1189 from="$tf_build_root" to="$archive" collect_build_artefacts
1190
1191 # Post-archive hook
1192 call_hook post_tf_archive
1193
1194 call_hook fetch_tf_resource
1195 call_hook post_fetch_tf_resource
1196
Chris Kay4e8aaf12022-09-01 15:21:55 +01001197 # Generate LAVA job files if necessary
1198 call_hook generate_lava_job_template
1199 call_hook generate_lava_job
1200
Fathi Boudra422bf772019-12-02 11:10:16 +02001201 # Clear any local changes made by applied patches
1202 undo_tf_patches
1203
1204 echo "##########"
1205 )
1206 fi
1207
1208 echo
1209 echo
1210done
1211
1212call_hook pre_package
1213
1214call_hook post_package
1215
1216if upon "$jenkins_run" && upon "$artefacts_receiver" && [ -d "artefacts" ]; then
Zelalem219df412020-05-17 19:21:20 -05001217 source "$CI_ROOT/script/send_artefacts.sh" "artefacts"
Fathi Boudra422bf772019-12-02 11:10:16 +02001218fi
1219
1220echo
1221echo "Done"