blob: 6e908d8b9c8550ccc3fc2687b42358240efc8467 [file] [log] [blame]
Leonardo Sandoval9dfdd1b2020-08-06 17:08:11 -05001#!/usr/bin/env bash
Fathi Boudra422bf772019-12-02 11:10:16 +02002#
Boyan Karatotev27057342025-07-28 09:56:23 +01003# Copyright (c) 2019-2025 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}"
Juan Pablo Conde3feb7f12023-10-21 13:14:47 -050023export tfut_root="${tfut_root:-$workspace/tfut}"
Zelalem219df412020-05-17 19:21:20 -050024cc_root="${cc_root:-$ccpathspec}"
Olivier Deprez0a9a3482019-12-16 14:10:31 +010025spm_root="${spm_root:-$workspace/spm}"
Manish V Badarkhed62aa5f2025-03-18 21:18:14 +000026rmm_root="${rmm_root:-$workspace/tf-rmm}"
Zelalem219df412020-05-17 19:21:20 -050027
Fathi Boudra422bf772019-12-02 11:10:16 +020028# Refspecs
29tf_refspec="$TF_REFSPEC"
30tftf_refspec="$TFTF_REFSPEC"
Olivier Deprez0a9a3482019-12-16 14:10:31 +010031spm_refspec="$SPM_REFSPEC"
Manish V Badarkhed62aa5f2025-03-18 21:18:14 +000032rmm_refspec="$RMM_REFSPEC"
Juan Pablo Conde3feb7f12023-10-21 13:14:47 -050033tfut_gerrit_refspec="$TFUT_GERRIT_REFSPEC"
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
Boyan Karatotev97de8d82025-03-06 15:22:21 +000041export archive="$artefacts"
Fathi Boudra422bf772019-12-02 11:10:16 +020042build_log="$artefacts/build.log"
Boyan Karatotevde2cd442025-07-28 09:50:29 +010043
44fiptool_path() {
45 echo $tf_build_root/$(get_tf_opt PLAT)/${bin_mode}/tools/fiptool/fiptool
46}
47
48cert_create_path() {
49 echo $tf_build_root/$(get_tf_opt PLAT)/${bin_mode}/tools/cert_create/cert_create
50}
Fathi Boudra422bf772019-12-02 11:10:16 +020051
52# Validate $bin_mode
53case "$bin_mode" in
54 "" | debug | release)
55 ;;
56 *)
57 die "Invalid value for bin_mode: $bin_mode"
58 ;;
59esac
60
61# File to save any environem
62hook_env_file="$(mktempfile)"
63
Fathi Boudra422bf772019-12-02 11:10:16 +020064# 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 Sokolovskybe6510c2024-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
Nicola Mazzucato7fc5abd2024-02-23 21:32:48 +0000121 if [ "$run_config_candidates" ]; then
122 for config_fragment in $run_config_candidates; do
Fathi Boudra422bf772019-12-02 11:10:16 +0200123 (
124 source "$ci_root/run_config/$config_fragment"
125 call_func "$func" "$config_fragment"
126 )
127 done
128 fi
129
Juan Pablo Conde2ecf1862024-01-16 20:12:43 -0600130 if [ "$run_config_tfut_candidates" ]; then
131 for config_fragment in $run_config_tfut_candidates; do
132 (
133 source "$ci_root/run_config_tfut/$config_fragment"
134 call_func "$func" "$config_fragment"
135 )
136 done
137 fi
138
Fathi Boudra422bf772019-12-02 11:10:16 +0200139 # Also source test config file
140 (
141 unset "$func"
142 source "$test_config_file"
143 call_func "$func" "$(basename $test_config_file)"
144 )
145
146 # Have any variables set take effect
147 source "$hook_env_file"
Paul Sokolovskye9962cd2021-12-17 18:39:40 +0300148
149 echo "=== End calling hooks: $1 ==="
Fathi Boudra422bf772019-12-02 11:10:16 +0200150}
151
152# Set a variable from within a hook
153set_hook_var() {
154 echo "export $1=\"${2?}\"" >> "$hook_env_file"
155}
156
157# Append to an array from within a hook
158append_hook_var() {
159 echo "export $1+=\"${2?}\"" >> "$hook_env_file"
160}
161
162# Have the main build script source a file
163source_later() {
164 echo "source ${1?}" >> "$hook_env_file"
165}
166
167# Setup TF build wrapper function by pointing to a script containing a function
168# that will be called with the TF build commands.
169setup_tf_build_wrapper() {
170 source_later "$ci_root/script/${wrapper?}_wrapper.sh"
171 set_hook_var "tf_build_wrapper" "${wrapper}_wrapper"
172 echo "Setup $wrapper build wrapper."
173}
174
175# Collect .bin files for archiving
176collect_build_artefacts() {
177 if [ ! -d "${from:?}" ]; then
178 return
179 fi
180
Manish V Badarkhe84c3a482025-04-16 08:15:38 +0100181 if ! find "$from" \( -name "*.bin" -o -name '*.elf' -o -name '*.dtb' -o -name '*.axf' -o -name '*.stm32' -o -name '*.img' \) -exec cp -t "${to:?}" '{}' +; then
Fathi Boudra422bf772019-12-02 11:10:16 +0200182 echo "You probably are running local CI on local repositories."
183 echo "Did you set 'dont_clean' but forgot to run 'distclean'?"
184 die
185 fi
186}
187
Manish Pandey1e7be852020-11-09 16:04:48 +0000188# Collect SPM/hafnium artefacts with "secure_" appended to the files
189# generated for SPM(secure hafnium).
190collect_spm_artefacts() {
Madhukar Pappireddyc683cf62021-11-01 14:38:32 -0500191 if [ -d "${non_secure_from:?}" ]; then
192 find "$non_secure_from" \( -name "*.bin" -o -name '*.elf' \) -exec cp -t "${to:?}" '{}' +
Manish Pandey1e7be852020-11-09 16:04:48 +0000193 fi
194
Madhukar Pappireddyc683cf62021-11-01 14:38:32 -0500195 if [ -d "${secure_from:?}" ]; then
196 for f in $(find "$secure_from" \( -name "*.bin" -o -name '*.elf' \)); do cp -- "$f" "${to:?}"/secure_$(basename $f); done
197 fi
Manish Pandey1e7be852020-11-09 16:04:48 +0000198}
199
Juan Pablo Conde3feb7f12023-10-21 13:14:47 -0500200collect_tfut_artefacts() {
201 if [ ! -d "${from:?}" ]; then
202 return
203 fi
204
205 pushd "$tfut_root/build"
206 artefact_list=$(python3 "$ci_root/script/get_ut_test_list.py")
207 for artefact in $artefact_list; do
208 cp -t "${to:?}" "$from/$artefact"
209 done
210 echo "$artefact_list" | tr ' ' '\n' > "${to:?}/tfut_artefacts.txt"
211 popd
212}
213
Edward Potapovdb31e702025-06-30 16:28:40 -0500214collect_tfut_coverage() {
215 if [ "$coverage" != "ON" ]; then
216 return
217 fi
218
219 pushd "$tfut_root/build"
220 touch "${to:?}/tfut_coverage.txt"
221 popd
222}
223
Javier Almansa Sobrino412d3612020-05-22 17:53:12 +0100224# Map the UART ID used for expect with the UART descriptor and port
225# used by the FPGA automation tools.
226map_uart() {
227 local port="${port:?}"
228 local descriptor="${descriptor:?}"
229 local baudrate="${baudrate:?}"
230 local run_root="${archive:?}/run"
231
232 local uart_dir="$run_root/uart${uart:?}"
233 mkdir -p "$uart_dir"
234
235 echo "$port" > "$uart_dir/port"
236 echo "$descriptor" > "$uart_dir/descriptor"
237 echo "$baudrate" > "$uart_dir/baudrate"
238
239 echo "UART${uart} mapped to port ${port} with descriptor ${descriptor} and baudrate ${baudrate}"
240}
241
Fathi Boudra422bf772019-12-02 11:10:16 +0200242# Arrange environment varibles to be set when expect scripts are launched
243set_expect_variable() {
244 local var="${1:?}"
245 local val="${2?}"
246
247 local run_root="${archive:?}/run"
248 local uart_dir="$run_root/uart${uart:?}"
249 mkdir -p "$uart_dir"
250
251 env_file="$uart_dir/env" quote="1" emit_env "$var" "$val"
252 echo "UART$uart: env has $@"
253}
254
255# Place the binary package a pointer to expect script, and its parameters
256track_expect() {
257 local file="${file:?}"
258 local timeout="${timeout-600}"
259 local run_root="${archive:?}/run"
260
261 local uart_dir="$run_root/uart${uart:?}"
262 mkdir -p "$uart_dir"
263
264 echo "$file" > "$uart_dir/expect"
265 echo "$timeout" > "$uart_dir/timeout"
Paul Sokolovskybfa8bab2023-01-25 19:34:51 +0700266 if [ -n "$lava_timeout" ]; then
267 set_run_env "lava_timeout" "$lava_timeout"
268 fi
Fathi Boudra422bf772019-12-02 11:10:16 +0200269
Paul Sokolovskybfa8bab2023-01-25 19:34:51 +0700270 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 +0200271
Chris Kayfab6edc2022-11-17 19:18:32 +0000272 if [ ! -z "${port}" ]; then
273 echo "${port}" > "$uart_dir/port"
274 fi
275
Fathi Boudra422bf772019-12-02 11:10:16 +0200276 # The run script assumes UART0 to be primary. If we're asked to set any
277 # other UART to be primary, set a run environment variable to signal
278 # that to the run script
279 if upon "$set_primary"; then
280 echo "Primary UART set to UART$uart."
281 set_run_env "primary_uart" "$uart"
282 fi
Madhukar Pappireddy1e953722021-11-08 15:23:02 -0600283
284 # UART used by payload(such as tftf, Linux) may not be the same as the
285 # primary UART. Set a run environment variable to track the payload
286 # UART which is tracked to check if the test has finished sucessfully.
287 if upon "$set_payload_uart"; then
288 echo "Payload uses UART$uart."
289 set_run_env "payload_uart" "$uart"
290 fi
Fathi Boudra422bf772019-12-02 11:10:16 +0200291}
292
293# Extract a FIP in $1 using fiptool
294extract_fip() {
295 local fip="$1"
296
297 if is_url "$1"; then
298 url="$1" fetch_file
299 fip="$(basename "$1")"
300 fi
301
Boyan Karatotevde2cd442025-07-28 09:50:29 +0100302 fiptool=$(fiptool_path)
Fathi Boudra422bf772019-12-02 11:10:16 +0200303 "$fiptool" unpack "$fip"
304 echo "Extracted FIP: $fip"
305}
306
307# Report build failure by printing a the tail end of build log. Archive the
308# build log for later inspection
309fail_build() {
310 local log_path
311
312 if upon "$jenkins_run"; then
313 log_path="$BUILD_URL/artifact/artefacts/build.log"
314 else
315 log_path="$build_log"
316 fi
317
318 echo
Leonardo Sandovalae97eda2020-11-12 10:07:03 -0600319 echo "Build failed! Full build log below:"
Fathi Boudra422bf772019-12-02 11:10:16 +0200320 echo "[...]"
321 echo
Leonardo Sandovalae97eda2020-11-12 10:07:03 -0600322 cat "$build_log"
Fathi Boudra422bf772019-12-02 11:10:16 +0200323 echo
324 echo "See $log_path for full output"
325 echo
326 cp -t "$archive" "$build_log"
327 exit 1;
328}
329
330# Build a FIP with supplied arguments
331build_fip() {
332 (
333 echo "Building FIP with arguments: $@"
334 local tf_env="$workspace/tf.env"
335
336 if [ -f "$tf_env" ]; then
337 set -a
338 source "$tf_env"
339 set +a
340 fi
341
Slava Andrianov192ee172025-06-11 15:40:43 -0500342 if [ "$(get_tf_opt MEASURED_BOOT)" = 1 ]; then
343 # These are needed for accurate hash verification
344 local build_args_path="${workspace}/fip_build_args"
345 echo $@ > $build_args_path
346 archive_file $build_args_path
347 fi
348
Boyan Karatotev99e12312025-05-02 15:00:24 +0100349 make -C "$tf_root" $make_j_opts $(cat "$tf_config_file") DEBUG="$DEBUG" BUILD_BASE=$tf_build_root V=1 "$@" \
Fathi Boudra422bf772019-12-02 11:10:16 +0200350 ${fip_targets:-fip} &>>"$build_log" || fail_build
351 )
352}
353
Sandrine Bailleux189fdb32023-10-20 13:41:22 +0200354# Build any extra rule from TF-A makefile with supplied arguments.
355#
356# This is useful in case you need to build something else than firmware binaries
357# or the FIP.
358build_tf_extra() {
359 (
360 tf_extra_rules=${tf_extra_rules:?}
361 echo "Building extra TF rule(s): $tf_extra_rules"
362 echo " Arguments: $@"
363
364 local tf_env="$workspace/tf.env"
365
366 if [ -f "$tf_env" ]; then
367 set -a
368 source "$tf_env"
369 set +a
370 fi
371
Boyan Karatotev99e12312025-05-02 15:00:24 +0100372 make -C "$tf_root" $make_j_opts $(cat "$tf_config_file") DEBUG="$DEBUG" V=1 BUILD_BASE=$tf_build_root "$@" \
Sandrine Bailleux189fdb32023-10-20 13:41:22 +0200373 ${tf_extra_rules} &>>"$build_log" || fail_build
374 )
375}
376
Fathi Boudra422bf772019-12-02 11:10:16 +0200377fip_update() {
Boyan Karatotevde2cd442025-07-28 09:50:29 +0100378 fiptool=$(fiptool_path)
Fathi Boudra422bf772019-12-02 11:10:16 +0200379 # Before the update process, check if the given image is supported by
380 # the fiptool. It's assumed that both fiptool and cert_create move in
Chris Kay197b1022023-08-16 21:31:41 +0100381 # tandem, and therefore, if one has support, the other has it too.
382 if ! ("$fiptool" update 2>&1 || true) | grep -qe "\s\+--${bin_name:?}"; then
Fathi Boudra422bf772019-12-02 11:10:16 +0200383 return 1
384 fi
385
386 if not_upon "$(get_tf_opt TRUSTED_BOARD_BOOT)"; then
387 echo "Updating FIP image: $bin_name"
388 # Update HW config. Without TBBR, it's only a matter of using
389 # the update sub-command of fiptool
390 "$fiptool" update "--$bin_name" "${src:-}" \
391 "$archive/fip.bin"
392 else
393 echo "Updating FIP image (TBBR): $bin_name"
394 # With TBBR, we need to unpack, re-create certificates, and then
395 # recreate the FIP.
396 local fip_dir="$(mktempdir)"
397 local bin common_args stem
398 local rot_key="$(get_tf_opt ROT_KEY)"
399
400 rot_key="${rot_key:?}"
401 if ! is_abs "$rot_key"; then
402 rot_key="$tf_root/$rot_key"
403 fi
404
405 # Arguments only for cert_create
406 local cert_args="-n"
407 cert_args+=" --tfw-nvctr ${nvctr:-31}"
408 cert_args+=" --ntfw-nvctr ${nvctr:-223}"
409 cert_args+=" --key-alg ${KEY_ALG:-rsa}"
410 cert_args+=" --rot-key $rot_key"
411
412 local dyn_config_opts=(
Zelalem1af7a7b2020-08-04 17:34:32 -0500413 "fw-config"
Fathi Boudra422bf772019-12-02 11:10:16 +0200414 "hw-config"
415 "tb-fw-config"
416 "nt-fw-config"
417 "soc-fw-config"
418 "tos-fw-config"
419 )
420
421 # Binaries without key certificates
422 declare -A has_no_key_cert
423 for bin in "tb-fw" "${dyn_config_opts[@]}"; do
424 has_no_key_cert["$bin"]="1"
425 done
426
427 # Binaries without certificates
428 declare -A has_no_cert
429 for bin in "hw-config" "${dyn_config_opts[@]}"; do
430 has_no_cert["$bin"]="1"
431 done
432
433 pushd "$fip_dir"
434
435 # Unpack FIP
436 "$fiptool" unpack "$archive/fip.bin" &>>"$build_log"
437
438 # Remove all existing certificates
439 rm -f *-cert.bin
440
441 # Copy the binary to be updated
442 cp -f "$src" "${bin_name}.bin"
443
444 # FIP unpack dumps binaries with the same name as the option
445 # used to pack it; likewise for certificates. Reverse-engineer
446 # the command line from the binary output.
447 common_args="--trusted-key-cert trusted_key.crt"
448 for bin in *.bin; do
449 stem="${bin%%.bin}"
450 common_args+=" --$stem $bin"
451 if not_upon "${has_no_cert[$stem]}"; then
452 common_args+=" --$stem-cert $stem.crt"
453 fi
454 if not_upon "${has_no_key_cert[$stem]}"; then
455 common_args+=" --$stem-key-cert $stem-key.crt"
456 fi
457 done
458
459 # Create certificates
Boyan Karatotevde2cd442025-07-28 09:50:29 +0100460 cert_create=$(cert_create_path)
Fathi Boudra422bf772019-12-02 11:10:16 +0200461 "$cert_create" $cert_args $common_args &>>"$build_log"
462
463 # Recreate and archive FIP
464 "$fiptool" create $common_args "fip.bin" &>>"$build_log"
465 archive_file "fip.bin"
466
467 popd
468 fi
469}
470
471# Update hw-config in FIP, and remove the original DTB afterwards.
472update_fip_hw_config() {
473 # The DTB needs to be loaded by the model (and not updated in the FIP)
Madhukar Pappireddy9062ebf2021-03-02 17:07:06 -0600474 # in configs:
475 # 1. Where BL2 isn't present
476 # 2. Where we boot to Linux directly as BL33
Fathi Boudra422bf772019-12-02 11:10:16 +0200477 case "1" in
478 "$(get_tf_opt RESET_TO_BL31)" | \
Madhukar Pappireddy9062ebf2021-03-02 17:07:06 -0600479 "$(get_tf_opt ARM_LINUX_KERNEL_AS_BL33)" | \
Fathi Boudra422bf772019-12-02 11:10:16 +0200480 "$(get_tf_opt RESET_TO_SP_MIN)" | \
Maksims Svecovs7a0da522023-03-06 16:28:27 +0000481 "$(get_tf_opt RESET_TO_BL2)")
Fathi Boudra422bf772019-12-02 11:10:16 +0200482 return 0;;
483 esac
484
485 if bin_name="hw-config" src="$archive/dtb.bin" fip_update; then
486 # Remove the DTB so that model won't load it
487 rm -f "$archive/dtb.bin"
488 fi
489}
490
Fathi Boudra422bf772019-12-02 11:10:16 +0200491get_tftf_opt() {
492 (
493 name="${1:?}"
494 if config_valid "$tftf_config_file"; then
495 source "$tftf_config_file"
496 echo "${!name}"
497 fi
498 )
499}
500
501get_tf_opt() {
502 (
503 name="${1:?}"
504 if config_valid "$tf_config_file"; then
505 source "$tf_config_file"
506 echo "${!name}"
507 fi
508 )
509}
510
Manish V Badarkhed62aa5f2025-03-18 21:18:14 +0000511get_rmm_opt() {
512 (
513 name="${1:?}"
Manish V Badarkhea3505272025-04-17 14:20:42 +0100514 default="$2"
Manish V Badarkhed62aa5f2025-03-18 21:18:14 +0000515 if config_valid "$rmm_config_file"; then
516 source "$rmm_config_file"
Manish V Badarkhea3505272025-04-17 14:20:42 +0100517 # If !name is not defined, go with the default
518 # value (if defined)
519 if [ -z "${!name}" ]; then
520 echo "$default"
521 else
522 echo "${!name}"
523 fi
Manish V Badarkhed62aa5f2025-03-18 21:18:14 +0000524 fi
525 )
526}
527
Fathi Boudra422bf772019-12-02 11:10:16 +0200528build_tf() {
529 (
530 env_file="$workspace/tf.env"
531 config_file="${tf_build_config:-$tf_config_file}"
532
533 # Build fiptool and all targets by default
Harrison Mutai32de9d02023-06-12 14:23:37 +0100534 build_targets="${tf_build_targets:-fiptool all}"
Fathi Boudra422bf772019-12-02 11:10:16 +0200535
536 source "$config_file"
537
538 # If it is a TBBR build, extract the MBED TLS library from archive
Manish V Badarkhe8f125012021-12-21 05:47:52 +0000539 if [ "$(get_tf_opt TRUSTED_BOARD_BOOT)" = 1 ] ||
Manish V Badarkhef43e3f52022-06-21 20:37:25 +0100540 [ "$(get_tf_opt MEASURED_BOOT)" = 1 ] ||
541 [ "$(get_tf_opt DRTM_SUPPORT)" = 1 ]; then
Fathi Boudra422bf772019-12-02 11:10:16 +0200542 mbedtls_dir="$workspace/mbedtls"
543 if [ ! -d "$mbedtls_dir" ]; then
544 mbedtls_ar="$workspace/mbedtls.tar.gz"
545
546 url="$mbedtls_archive" saveas="$mbedtls_ar" fetch_file
547 mkdir "$mbedtls_dir"
Leonardo Sandovalec9e16c2020-09-09 14:32:40 -0500548 extract_tarball $mbedtls_ar $mbedtls_dir --strip-components=1
Fathi Boudra422bf772019-12-02 11:10:16 +0200549 fi
550
551 emit_env "MBEDTLS_DIR" "$mbedtls_dir"
552 fi
Jimmy Brisson0d5e12c2023-05-16 14:51:51 -0500553 if [ "$(get_tf_opt PLATFORM_TEST)" = "tfm-testsuite" ] &&
554 not_upon "${TF_M_TESTS_PATH}"; then
555 emit_env "TF_M_TESTS_PATH" "$WORKSPACE/tf-m-tests"
556 fi
557 if [ "$(get_tf_opt PLATFORM_TEST)" = "tfm-testsuite" ] &&
558 not_upon "${TF_M_EXTRAS_PATH}"; then
559 emit_env "TF_M_EXTRAS_PATH" "$WORKSPACE/tf-m-extras"
560 fi
David Vincze82db6932024-02-21 12:05:50 +0100561 if [ "$(get_tf_opt DICE_PROTECTION_ENVIRONMENT)" = 1 ] &&
562 not_upon "${QCBOR_DIR}"; then
563 emit_env "QCBOR_DIR" "$WORKSPACE/qcbor"
564 fi
Slava Andrianov192ee172025-06-11 15:40:43 -0500565
566 # Hash verification only occurs if there is a sufficient amount of
567 # information in the event log, which is as long as EVENT_LOG_LEVEL
568 # is set to at least 20 or if it is a debug build
569 if [[ ("$(get_tf_opt MEASURED_BOOT)" -eq 1) &&
570 (($bin_mode == "debug") || ("$(get_tf_opt EVENT_LOG_LEVEL)" -ge 20)) ]]; then
571 # This variable is later exported to the expect scripts so
572 # the hashes in the TF-A event log can be verified
573 set_run_env "verify_hashes" "1"
574 fi
Fathi Boudra422bf772019-12-02 11:10:16 +0200575 if [ -f "$env_file" ]; then
576 set -a
577 source "$env_file"
578 set +a
579 fi
580
Harrison Mutai013f6332022-02-16 16:06:33 +0000581 if is_arm_jenkins_env || upon "$local_ci"; then
582 path_list=(
583 "$llvm_dir/bin"
584 )
585 extend_path "PATH" "path_list"
586 fi
587
Boyan Karatotev97de8d82025-03-06 15:22:21 +0000588 pushd "$tf_root"
Fathi Boudra422bf772019-12-02 11:10:16 +0200589
590 # Always distclean when running on Jenkins. Skip distclean when running
591 # locally and explicitly requested.
592 if upon "$jenkins_run" || not_upon "$dont_clean"; then
Boyan Karatotev97de8d82025-03-06 15:22:21 +0000593 make distclean BUILD_BASE=$tf_build_root &>>"$build_log" || fail_build
Fathi Boudra422bf772019-12-02 11:10:16 +0200594 fi
595
596 # Log build command line. It is left unfolded on purpose to assist
597 # copying to clipboard.
598 cat <<EOF | log_separator >/dev/null
599
600Build command line:
Boyan Karatotev97de8d82025-03-06 15:22:21 +0000601 $tf_build_wrapper make $make_j_opts $(cat "$config_file" | tr '\n' ' ') DEBUG=$DEBUG V=1 BUILD_BASE=$tf_build_root $build_targets
Fathi Boudra422bf772019-12-02 11:10:16 +0200602
Paul Sokolovsky7f71b072023-10-16 12:59:09 +0300603CC version:
604$(${CC-${CROSS_COMPILE}gcc} -v 2>&1)
Fathi Boudra422bf772019-12-02 11:10:16 +0200605EOF
606
Javier Almansa Sobrinoe8363182020-11-10 16:40:53 +0000607 if not_upon "$local_ci"; then
608 connect_debugger=0
609 fi
610
Fathi Boudra422bf772019-12-02 11:10:16 +0200611 # Build TF. Since build output is being directed to the build log, have
612 # descriptor 3 point to the current terminal for build wrappers to vent.
Harrison Mutai6361dbe2023-02-16 14:12:40 +0000613 $tf_build_wrapper poetry run make $make_j_opts $(cat "$config_file") \
Boyan Karatotev97de8d82025-03-06 15:22:21 +0000614 DEBUG="$DEBUG" V=1 BUILD_BASE="$tf_build_root" SPIN_ON_BL1_EXIT="$connect_debugger" \
Fathi Boudra422bf772019-12-02 11:10:16 +0200615 $build_targets 3>&1 &>>"$build_log" || fail_build
Harrison Mutai32de9d02023-06-12 14:23:37 +0100616
Harrison Mutai6dafb5f2023-07-18 15:21:48 +0100617 if [ "$build_targets" != "doc" ]; then
Chris Kay9ab2d952025-05-29 13:46:24 +0100618 (poetry run memory --root "$tf_build_root" symbols 2>&1 || true) | tee -a "${build_log}"
619
620 for map in $(find "${tf_build_root}" -name '*.map'); do
621 (poetry run memory --root "${tf_build_root}" summary "${map}" 2>&1 || true) | tee -a "${build_log}"
622 done
Harrison Mutai6dafb5f2023-07-18 15:21:48 +0100623 fi
Boyan Karatotev97de8d82025-03-06 15:22:21 +0000624 popd
Fathi Boudra422bf772019-12-02 11:10:16 +0200625 )
626}
627
628build_tftf() {
629 (
630 config_file="${tftf_build_config:-$tftf_config_file}"
631
632 # Build tftf target by default
633 build_targets="${tftf_build_targets:-all}"
634
635 source "$config_file"
636
637 cd "$tftf_root"
638
639 # Always distclean when running on Jenkins. Skip distclean when running
640 # locally and explicitly requested.
641 if upon "$jenkins_run" || not_upon "$dont_clean"; then
Boyan Karatotev97de8d82025-03-06 15:22:21 +0000642 make distclean BUILD_BASE="$tftf_build_root" &>>"$build_log" || fail_build
Fathi Boudra422bf772019-12-02 11:10:16 +0200643 fi
644
645 # TFTF build system cannot reliably deal with -j option, so we avoid
646 # using that.
647
648 # Log build command line
649 cat <<EOF | log_separator >/dev/null
650
651Build command line:
Boyan Karatotev97de8d82025-03-06 15:22:21 +0000652 make $make_j_opts $(cat "$config_file" | tr '\n' ' ') DEBUG=$DEBUG V=1 BUILD_BASE="$tftf_build_root" $build_targets
Fathi Boudra422bf772019-12-02 11:10:16 +0200653
654EOF
655
Boyan Karatotev97de8d82025-03-06 15:22:21 +0000656 make $make_j_opts $(cat "$config_file") DEBUG="$DEBUG" V=1 BUILD_BASE="$tftf_build_root" \
Fathi Boudra422bf772019-12-02 11:10:16 +0200657 $build_targets &>>"$build_log" || fail_build
658 )
659}
660
Zelalem219df412020-05-17 19:21:20 -0500661build_cc() {
662# Building code coverage plugin
663 ARM_DIR=/arm
664 pvlibversion=$(/arm/devsys-tools/abs/detag "SysGen:PVModelLib:$model_version::trunk")
665 PVLIB_HOME=$warehouse/SysGen/PVModelLib/$model_version/${pvlibversion}/external
666 if [ -n "$(find "$ARM_DIR" -maxdepth 0 -type d -empty 2>/dev/null)" ]; then
667 echo "Error: Arm warehouse not mounted. Please mount the Arm warehouse to your /arm local folder"
668 exit -1
669 fi # Error if arm warehouse not found
670 cd "$ccpathspec/scripts/tools/code_coverage/fastmodel_baremetal/bmcov"
671
672 make -C model-plugin PVLIB_HOME=$PVLIB_HOME &>>"$build_log"
673}
674
Olivier Deprez0a9a3482019-12-16 14:10:31 +0100675build_spm() {
676 (
677 env_file="$workspace/spm.env"
678 config_file="${spm_build_config:-$spm_config_file}"
679
680 source "$config_file"
681
682 if [ -f "$env_file" ]; then
683 set -a
684 source "$env_file"
685 set +a
686 fi
687
688 cd "$spm_root"
689
690 # Always clean when running on Jenkins. Skip clean when running
691 # locally and explicitly requested.
692 if upon "$jenkins_run" || not_upon "$dont_clean"; then
693 # make clean fails on a fresh repo where the project has not
694 # yet been built. Hence only clean if out/reference directory
695 # already exists.
696 if [ -d "out/reference" ]; then
697 make clean &>>"$build_log" || fail_build
698 fi
699 fi
700
701 # Log build command line. It is left unfolded on purpose to assist
702 # copying to clipboard.
703 cat <<EOF | log_separator >/dev/null
704
705Build command line:
Boyan Karatotev27057342025-07-28 09:56:23 +0100706 make $make_j_opts OUT=$spm_build_root $(cat "$config_file" | tr '\n' ' ')
Olivier Deprez0a9a3482019-12-16 14:10:31 +0100707
708EOF
709
710 # Build SPM. Since build output is being directed to the build log, have
711 # descriptor 3 point to the current terminal for build wrappers to vent.
Boyan Karatotev27057342025-07-28 09:56:23 +0100712 make $make_j_opts OUT=$spm_build_root $(cat "$config_file") 3>&1 &>>"$build_log" \
Olivier Deprez0a9a3482019-12-16 14:10:31 +0100713 || fail_build
714 )
715}
Zelalem219df412020-05-17 19:21:20 -0500716
Manish V Badarkhed62aa5f2025-03-18 21:18:14 +0000717build_rmm() {
718 (
719 env_file="$workspace/rmm.env"
720 config_file="${rmm_build_config:-$rmm_config_file}"
721
722 # Build fiptool and all targets by default
Manish V Badarkhe8cbbabf2025-08-20 11:27:12 +0100723 export CROSS_COMPILE="aarch64-none-elf-"
Manish V Badarkhed62aa5f2025-03-18 21:18:14 +0000724
725 source "$config_file"
726
727 if [ -f "$env_file" ]; then
728 set -a
729 source "$env_file"
730 set +a
731 fi
732
733 cd "$rmm_root"
734
735 if [ -f "$rmm_root/requirements.txt" ]; then
736 export PATH="$HOME/.local/bin:$PATH"
737 python3 -m pip install --upgrade pip
738 python3 -m pip install -r "$rmm_root/requirements.txt"
739 fi
740
741 # Always distclean when running on Jenkins. Skip distclean when running
742 # locally and explicitly requested.
743 if upon "$jenkins_run" || not_upon "$dont_clean"; then
744 # Remove 'rmm\build' folder
745 echo "Removing $rmm_build_root..."
746 rm -rf $rmm_build_root
747 fi
748
Manish V Badarkhea3505272025-04-17 14:20:42 +0100749 if not_upon "$local_ci"; then
750 connect_debugger=0
751 fi
752
Manish V Badarkhed62aa5f2025-03-18 21:18:14 +0000753 # Log build command line. It is left unfolded on purpose to assist
754 # copying to clipboard.
755 cat <<EOF | log_separator >/dev/null
756
757Build command line:
Manish V Badarkhea3505272025-04-17 14:20:42 +0100758 cmake -DRMM_CONFIG=${plat}_defcfg "$cmake_gen" -S $rmm_root -B $rmm_build_root -DRMM_TOOLCHAIN=$rmm_toolchain -DRMM_FPU_USE_AT_REL2=$rmm_fpu_use_at_rel2 -DATTEST_EL3_TOKEN_SIGN=$rmm_attest_el3_token_sign -DRMM_V1_1=$rmm_v1_1 ${extra_options}
759 cmake --build $rmm_build_root --config $cmake_build_type $make_j_opts -v ${extra_targets+-- $extra_targets}
Manish V Badarkhed62aa5f2025-03-18 21:18:14 +0000760
Manish V Badarkhea3505272025-04-17 14:20:42 +0100761EOF
762 cmake \
763 -DRMM_CONFIG=${plat}_defcfg $cmake_gen \
764 -S $rmm_root -B $rmm_build_root \
765 -DRMM_TOOLCHAIN=$rmm_toolchain \
766 -DRMM_FPU_USE_AT_REL2=$rmm_fpu_use_at_rel2 \
767 -DATTEST_EL3_TOKEN_SIGN=$rmm_attest_el3_token_sign \
768 -DRMM_V1_1=$rmm_v1_1 \
769 ${extra_options}
770 cmake --build $rmm_build_root --config $cmake_build_type $make_j_opts -v ${extra_targets+-- $extra_targets} 3>&1 &>>"$build_log" || fail_build
771 )
Manish V Badarkhed62aa5f2025-03-18 21:18:14 +0000772}
773
Juan Pablo Conde3feb7f12023-10-21 13:14:47 -0500774build_tfut() {
775 (
776 config_file="${tfut_build_config:-$tfut_config_file}"
777
778 # Build tfut target by default
779 build_targets="${tfut_build_targets:-all}"
780
781 source "$config_file"
782
783 mkdir -p "$tfut_root/build"
784 cd "$tfut_root/build"
785
786 # Always distclean when running on Jenkins. Skip distclean when running
787 # locally and explicitly requested.
788 if upon "$jenkins_run" || not_upon "$dont_clean"; then
789 #make clean &>>"$build_log" || fail_build
790 rm -Rf * || fail_build
791 fi
792
793 #Override build targets only if the run config did not set them.
794 if [ $build_targets == "all" ]; then
795 tests_line=$(cat "$config_file" | { grep "tests=" || :; })
796 if [ -z "$tests_line" ]; then
797 build_targets=$(echo "$tests_line" | awk -F= '{ print $NF }')
798 fi
799 fi
800
Juan Pablo Conde2ecf1862024-01-16 20:12:43 -0600801 #TODO: extract vars from env to use them for cmake
802
803 test -f "$config_file"
804
805 config=$(cat "$config_file" | grep -v "tests=") \
806 && cmake_config=$(echo "$config" | sed -e 's/^/\-D/')
Juan Pablo Conde3feb7f12023-10-21 13:14:47 -0500807
808 # Check if cmake is installed
809 if ! command -v cmake &> /dev/null
810 then
811 echo "cmake could not be found"
812 exit 1
813 fi
814
815 # Log build command line
816 cat <<EOF | log_separator >/dev/null
817
818Build command line:
Edward Potapovdb31e702025-06-30 16:28:40 -0500819cmake $(echo "$cmake_config") -G"Unix Makefiles" --debug-output -DCMAKE_VERBOSE_MAKEFILE -DCOVERAGE="$COVERAGE" -DUNIT_TEST_PROJECT_PATH="$tf_root" ..
Juan Pablo Conde3feb7f12023-10-21 13:14:47 -0500820 make $(echo "$config" | tr '\n' ' ') DEBUG=$DEBUG V=1 $build_targets
821
822EOF
823 cmake $(echo "$cmake_config") -G"Unix Makefiles" --debug-output \
824 -DCMAKE_VERBOSE_MAKEFILE=ON \
Edward Potapovdb31e702025-06-30 16:28:40 -0500825 -DCOVERAGE="$COVERAGE" \
Juan Pablo Conde3feb7f12023-10-21 13:14:47 -0500826 -DUNIT_TEST_PROJECT_PATH="$tf_root" \
827 .. &>> "$build_log" || fail_build
828 echo "Done with cmake" >> "$build_log"
829 make $(echo "$config") VERBOSE=1 \
830 $build_targets &>> "$build_log" || fail_build
831 )
832
833}
834
Fathi Boudra422bf772019-12-02 11:10:16 +0200835# Set metadata for the whole package so that it can be used by both Jenkins and
836# shell
837set_package_var() {
838 env_file="$artefacts/env" emit_env "$@"
839}
840
841set_tf_build_targets() {
842 echo "Set build target to '${targets:?}'"
843 set_hook_var "tf_build_targets" "$targets"
844}
845
846set_tftf_build_targets() {
847 echo "Set build target to '${targets:?}'"
848 set_hook_var "tftf_build_targets" "$targets"
849}
850
Olivier Deprez0a9a3482019-12-16 14:10:31 +0100851set_spm_build_targets() {
852 echo "Set build target to '${targets:?}'"
853 set_hook_var "spm_build_targets" "$targets"
854}
855
Juan Pablo Conde2ecf1862024-01-16 20:12:43 -0600856add_tfut_build_targets() {
857 echo "Add TFUT build targets '${targets:?}'"
858 append_hook_var "tfut_build_targets" "$targets "
Juan Pablo Conde3feb7f12023-10-21 13:14:47 -0500859}
860
Daniel Boulbyb8d2a462022-03-07 13:55:25 +0000861set_spm_out_dir() {
862 echo "Set SPMC binary build to '${out_dir:?}'"
863 set_hook_var "spm_secure_out_dir" "$out_dir"
864}
Fathi Boudra422bf772019-12-02 11:10:16 +0200865# Look under $archive directory for known files such as blX images, kernel, DTB,
866# initrd etc. For each known file foo, if foo.bin exists, then set variable
867# foo_bin to the path of the file. Make the path relative to the workspace so as
868# to remove any @ characters, which Jenkins inserts for parallel runs. If the
869# file doesn't exist, unset its path.
870set_default_bin_paths() {
871 local image image_name image_path path
872 local archive="${archive:?}"
873 local set_vars
874 local var
875
876 pushd "$archive"
877
878 for file in *.bin; do
879 # Get a shell variable from the file's stem
880 var_name="${file%%.*}_bin"
881 var_name="$(echo "$var_name" | sed -r 's/[^[:alnum:]]/_/g')"
882
883 # Skip setting the variable if it's already
884 if [ "${!var_name}" ]; then
885 echo "Note: not setting $var_name; already set to ${!var_name}"
886 continue
887 else
888 set_vars+="$var_name "
889 fi
890
891 eval "$var_name=$file"
892 done
893
894 echo "Binary paths set for: "
895 {
896 for var in $set_vars; do
897 echo -n "\$$var "
898 done
899 } | fmt -80 | sed 's/^/ /'
900 echo
901
902 popd
903}
904
905gen_model_params() {
906 local model_param_file="$archive/model_params"
Javier Almansa Sobrinoe8363182020-11-10 16:40:53 +0000907 [ "$connect_debugger" ] && [ "$connect_debugger" -eq 1 ] && wait_debugger=1
Fathi Boudra422bf772019-12-02 11:10:16 +0200908
909 set_default_bin_paths
910 echo "Generating model parameter for $model..."
911 source "$ci_root/model/${model:?}.sh"
912 archive_file "$model_param_file"
913}
914
915set_model_path() {
916 set_run_env "model_path" "${1:?}"
917}
918
Zelalem1af7a7b2020-08-04 17:34:32 -0500919set_model_env() {
920 local var="${1:?}"
921 local val="${2?}"
922 local run_root="${archive:?}/run"
923
924 mkdir -p "$run_root"
925 echo "export $var=$val" >> "$run_root/model_env"
926}
Fathi Boudra422bf772019-12-02 11:10:16 +0200927set_run_env() {
928 local var="${1:?}"
929 local val="${2?}"
930 local run_root="${archive:?}/run"
931
932 mkdir -p "$run_root"
933 env_file="$run_root/env" quote="1" emit_env "$var" "$val"
934}
935
936show_head() {
937 # Display HEAD descripton
938 pushd "$1"
939 git show --quiet --no-color | sed 's/^/ > /g'
940 echo
941 popd
942}
943
944# Choose debug binaries to run; by default, release binaries are chosen to run
945use_debug_bins() {
946 local run_root="${archive:?}/run"
947
948 echo "Choosing debug binaries for execution"
949 set_package_var "BIN_MODE" "debug"
950}
951
952assert_can_git_clone() {
953 local name="${1:?}"
954 local dir="${!name}"
955
956 # If it doesn't exist, it can be cloned into
957 if [ ! -e "$dir" ]; then
958 return 0
959 fi
960
961 # If it's a directory, it must be a Git clone already
962 if [ -d "$dir" ] && [ -d "$dir/.git" ]; then
963 # No need to clone again
964 echo "Using existing git clone for $name: $dir"
965 return 1
966 fi
967
968 die "Path $dir exists but is not a git clone"
969}
970
971clone_repo() {
972 if ! is_url "${clone_url?}"; then
973 # For --depth to take effect on local paths, it needs to use the
974 # file:// scheme.
975 clone_url="file://$clone_url"
976 fi
977
978 git clone -q --depth 1 "$clone_url" "${where?}"
979 if [ "$refspec" ]; then
980 pushd "$where"
981 git fetch -q --depth 1 origin "$refspec"
982 git checkout -q FETCH_HEAD
983 popd
984 fi
985}
986
987build_unstable() {
988 echo "--BUILD UNSTABLE--" | tee -a "$build_log"
989}
990
991undo_patch_record() {
992 if [ ! -f "${patch_record:?}" ]; then
993 return
994 fi
995
996 # Undo patches in reverse
997 echo
998 for patch_name in $(tac "$patch_record"); do
999 echo "Undoing $patch_name..."
1000 if ! git apply -R "$ci_root/patch/$patch_name"; then
1001 if upon "$local_ci"; then
1002 echo
1003 echo "Your local directory may have been dirtied."
1004 echo
1005 fi
1006 fail_build
1007 fi
1008 done
1009
1010 rm -f "$patch_record"
1011}
1012
1013undo_local_patches() {
1014 pushd "$tf_root"
1015 patch_record="$tf_patch_record" undo_patch_record
1016 popd
1017
1018 if [ -d "$tftf_root" ]; then
1019 pushd "$tftf_root"
1020 patch_record="$tftf_patch_record" undo_patch_record
1021 popd
1022 fi
1023}
1024
1025undo_tftf_patches() {
1026 pushd "$tftf_root"
1027 patch_record="$tftf_patch_record" undo_patch_record
1028 popd
1029}
1030
1031undo_tf_patches() {
1032 pushd "$tf_root"
1033 patch_record="$tf_patch_record" undo_patch_record
1034 popd
1035}
1036
1037apply_patch() {
1038 # If skip_patches is set, the developer has applied required patches
1039 # manually. They probably want to keep them applied for debugging
1040 # purposes too. This means we don't have to apply/revert them as part of
1041 # build process.
1042 if upon "$skip_patches"; then
1043 echo "Skipped applying ${1:?}..."
1044 return 0
1045 else
1046 echo "Applying ${1:?}..."
1047 fi
1048
Sandrine Bailleux4cb8c222023-09-13 13:48:15 +02001049 if git apply --reverse --check < "$ci_root/patch/$1" 2> /dev/null; then
Jimmy Brissonf134e4c2023-03-22 13:20:20 -05001050 echo "Skipping already applied ${1:?}"
1051 return 0
1052 fi
1053
Fathi Boudra422bf772019-12-02 11:10:16 +02001054 if git apply < "$ci_root/patch/$1"; then
1055 echo "$1" >> "${patch_record:?}"
1056 else
1057 if upon "$local_ci"; then
1058 undo_local_patches
1059 fi
1060 fail_build
1061 fi
1062}
1063
Fathi Boudra422bf772019-12-02 11:10:16 +02001064apply_tf_patch() {
Boyan Karatotevfaf9a9d2025-07-28 09:52:05 +01001065 root="$tf_root"
1066 new_root="$archive/tfa_mirror"
1067
1068 # paralell builds are only used locally. Don't do for CI since this will
1069 # have a speed penalty. Also skip if this was already done as a single
1070 # job may apply many patches.
1071 if upon "$local_ci" && [[ ! -d $new_root ]]; then
1072 root=$new_root
1073 diff=$(mktempfile)
1074
1075 # get anything still uncommitted
1076 pushd $tf_root
1077 git diff HEAD > $diff
1078 popd
1079
1080 # git will hard link when cloning locally, no need for --depth=1
1081 git clone "$tf_root" $root --shallow-submodules
1082
1083 tf_root=$root # next apply_tf_patch will run in the same hook
1084 set_hook_var "tf_root" "$root" # for anyone outside the hook
1085
1086 # apply uncommited changes so they are picked up in the build
1087 pushd $tf_root
1088 git apply $diff &> /dev/null || true
1089 popd
1090
1091 fi
1092
1093 pushd "$root"
Fathi Boudra422bf772019-12-02 11:10:16 +02001094 patch_record="$tf_patch_record" apply_patch "$1"
1095 popd
1096}
1097
Fathi Boudra422bf772019-12-02 11:10:16 +02001098mkdir -p "$workspace"
1099mkdir -p "$archive"
1100set_package_var "TEST_CONFIG" "$test_config"
1101
1102{
1103echo
1104echo "CONFIGURATION: $test_group/$test_config"
1105echo
1106} |& log_separator
1107
1108tf_config="$(echo "$build_configs" | awk -F, '{print $1}')"
1109tftf_config="$(echo "$build_configs" | awk -F, '{print $2}')"
Chris Kay4f7846a2025-08-04 19:56:35 +01001110spm_config="$(echo "$build_configs" | awk -F, '{print $3}')"
1111rmm_config="$(echo "$build_configs" | awk -F, '{print $4}')"
Juan Pablo Conde3feb7f12023-10-21 13:14:47 -05001112tfut_config="$(echo "$build_configs" | awk -F, '{print $5}')"
Fathi Boudra422bf772019-12-02 11:10:16 +02001113
1114test_config_file="$ci_root/group/$test_group/$test_config"
1115
1116tf_config_file="$ci_root/tf_config/$tf_config"
1117tftf_config_file="$ci_root/tftf_config/$tftf_config"
Olivier Deprez0a9a3482019-12-16 14:10:31 +01001118spm_config_file="$ci_root/spm_config/$spm_config"
Manish V Badarkhed62aa5f2025-03-18 21:18:14 +00001119rmm_config_file="$ci_root/rmm_config/$rmm_config"
Juan Pablo Conde3feb7f12023-10-21 13:14:47 -05001120tfut_config_file="$ci_root/tfut_config/$tfut_config"
Fathi Boudra422bf772019-12-02 11:10:16 +02001121
1122# File that keeps track of applied patches
1123tf_patch_record="$workspace/tf_patches"
1124tftf_patch_record="$workspace/tftf_patches"
1125
Juan Pablo Conde84bf39f2024-01-12 22:09:49 -06001126# Split run config into TF and TFUT components
1127run_config_tfa="$(echo "$run_config" | awk -F, '{print $1}')"
1128run_config_tfut="$(echo "$run_config" | awk -F, '{print $2}')"
1129
Fathi Boudra422bf772019-12-02 11:10:16 +02001130pushd "$workspace"
1131
1132if ! config_valid "$tf_config"; then
1133 tf_config=
1134else
1135 echo "Trusted Firmware config:"
1136 echo
1137 sort "$tf_config_file" | sed '/^\s*$/d;s/^/\t/'
1138 echo
1139fi
1140
1141if ! config_valid "$tftf_config"; then
1142 tftf_config=
1143else
1144 echo "Trusted Firmware TF config:"
1145 echo
1146 sort "$tftf_config_file" | sed '/^\s*$/d;s/^/\t/'
1147 echo
1148fi
1149
Olivier Deprez0a9a3482019-12-16 14:10:31 +01001150if ! config_valid "$spm_config"; then
1151 spm_config=
1152else
1153 echo "SPM config:"
1154 echo
1155 sort "$spm_config_file" | sed '/^\s*$/d;s/^/\t/'
Zelalem219df412020-05-17 19:21:20 -05001156 echo
1157fi
1158
Manish V Badarkhed62aa5f2025-03-18 21:18:14 +00001159# File that keeps track of applied patches
1160rmm_patch_record="$workspace/rmm_patches"
1161
1162if ! config_valid "$rmm_config"; then
1163 rmm_config=
1164else
1165 echo "Trusted Firmware RMM config:"
1166 echo
1167 sort "$rmm_config_file" | sed '/^\s*$/d;s/^/\t/'
1168 echo
1169fi
1170
Juan Pablo Conde3feb7f12023-10-21 13:14:47 -05001171if ! config_valid "$tfut_config"; then
1172 tfut_config=
1173else
1174 echo "TFUT config:"
1175 echo
1176 sort "$tfut_config_file" | sed '/^\s*$/d;s/^/\t/'
1177 echo
1178fi
1179
Juan Pablo Conde84bf39f2024-01-12 22:09:49 -06001180if ! config_valid "$run_config_tfa"; then
1181 run_config_tfa=
Fathi Boudra422bf772019-12-02 11:10:16 +02001182fi
1183
Juan Pablo Conde3feb7f12023-10-21 13:14:47 -05001184if { [ "$tf_config" ] || [ "$tfut_config" ]; } && assert_can_git_clone "tf_root"; then
Fathi Boudra422bf772019-12-02 11:10:16 +02001185 # If the Trusted Firmware repository has already been checked out, use
1186 # that location. Otherwise, clone one ourselves.
1187 echo "Cloning Trusted Firmware..."
1188 clone_url="${TF_CHECKOUT_LOC:-$tf_src_repo_url}" where="$tf_root" \
1189 refspec="$TF_REFSPEC" clone_repo &>>"$build_log"
1190 show_head "$tf_root"
1191fi
1192
1193if [ "$tftf_config" ] && assert_can_git_clone "tftf_root"; then
1194 # If the Trusted Firmware TF repository has already been checked out,
1195 # use that location. Otherwise, clone one ourselves.
1196 echo "Cloning Trusted Firmware TF..."
1197 clone_url="${TFTF_CHECKOUT_LOC:-$tftf_src_repo_url}" where="$tftf_root" \
1198 refspec="$TFTF_REFSPEC" clone_repo &>>"$build_log"
1199 show_head "$tftf_root"
1200fi
1201
Zelalem219df412020-05-17 19:21:20 -05001202if [ -n "$cc_config" ] ; then
1203 if [ "$cc_config" -eq 1 ] && assert_can_git_clone "cc_root"; then
1204 # Copy code coverage repository
1205 echo "Cloning Code Coverage..."
1206 git clone -q $cc_src_repo_url cc_plugin --depth 1 -b $cc_src_repo_tag > /dev/null
1207 show_head "$cc_root"
1208 fi
1209fi
1210
Daniel Boulby25385ab2023-12-14 14:36:25 +00001211if [ "$spm_config" ] ; then
1212 if assert_can_git_clone "spm_root"; then
1213 # If the SPM repository has already been checked out, use
1214 # that location. Otherwise, clone one ourselves.
1215 echo "Cloning SPM..."
1216 clone_url="${SPM_CHECKOUT_LOC:-$spm_src_repo_url}" \
1217 where="$spm_root" refspec="$SPM_REFSPEC" \
1218 clone_repo &>>"$build_log"
1219 fi
Olivier Deprez0a9a3482019-12-16 14:10:31 +01001220
1221 # Query git submodules
1222 pushd "$spm_root"
Daniel Boulby25385ab2023-12-14 14:36:25 +00001223 # Check if submodules need initialising
Paul Sokolovskyad274422024-09-01 10:27:56 +03001224
1225 # This handling is needed to reliably fetch submodules
1226 # in CI environment.
1227 for subm in $(git submodule status | awk '/^-/ {print $2}'); do
1228 for i in $(seq 1 7); do
1229 git submodule init $subm
1230 if git submodule update $subm; then
1231 break
1232 fi
1233 git submodule deinit --force $subm
1234 echo "Retrying $subm"
1235 sleep $((RANDOM % 10 + 5))
1236 done
1237 done
1238
1239 git submodule status
Olivier Deprez0a9a3482019-12-16 14:10:31 +01001240 popd
1241
1242 show_head "$spm_root"
1243fi
1244
Manish V Badarkhed62aa5f2025-03-18 21:18:14 +00001245if [ "$rmm_config" ] && assert_can_git_clone "rmm_root"; then
Manish V Badarkhe41909452025-04-11 12:06:45 +01001246 # If the RMM repository has already been checked out,
Manish V Badarkhed62aa5f2025-03-18 21:18:14 +00001247 # use that location. Otherwise, clone one ourselves.
1248 echo "Cloning TF-RMM..."
1249 clone_url="${RMM_CHECKOUT_LOC:-$rmm_src_repo_url}" where="$rmm_root" \
1250 refspec="$RMM_REFSPEC" clone_repo &>>"$build_log"
1251 show_head "$rmm_root"
1252fi
1253
Juan Pablo Conde3feb7f12023-10-21 13:14:47 -05001254if [ "$tfut_config" ] && assert_can_git_clone "tfut_root"; then
1255 # If the Trusted Firmware UT repository has already been checked out,
1256 # use that location. Otherwise, clone one ourselves.
1257 echo "Cloning Trusted Firmware UT..."
1258 clone_url="${TFUT_CHECKOUT_LOC:-$tfut_src_repo_url}" where="$tfut_root" \
1259 refspec="$TFUT_GERRIT_REFSPEC" clone_repo &>>"$build_log"
1260 show_head "$tfut_root"
1261fi
1262
Juan Pablo Conde84bf39f2024-01-12 22:09:49 -06001263if [ "$run_config_tfa" ]; then
1264 # Get candidates for TF-A run config
Nicola Mazzucato7fc5abd2024-02-23 21:32:48 +00001265 run_config_candidates="$("$ci_root/script/gen_run_config_candidates.py" \
Juan Pablo Conde84bf39f2024-01-12 22:09:49 -06001266 "$run_config_tfa")"
Nicola Mazzucato7fc5abd2024-02-23 21:32:48 +00001267 if [ -z "$run_config_candidates" ]; then
Fathi Boudra422bf772019-12-02 11:10:16 +02001268 die "No run config candidates!"
1269 else
1270 echo
1271 echo "Chosen fragments:"
1272 echo
Nicola Mazzucato7fc5abd2024-02-23 21:32:48 +00001273 echo "$run_config_candidates" | sed 's/^\|\n/\t/g'
Fathi Boudra422bf772019-12-02 11:10:16 +02001274 echo
Harrison Mutai4dfe1192024-07-03 12:35:38 +00001275
1276 if [ ! -n "$bin_mode" ]; then
1277 if echo $run_config_candidates | grep -wq "debug"; then
1278 bin_mode="debug"
1279 else
1280 bin_mode="release"
1281 fi
1282 fi
Fathi Boudra422bf772019-12-02 11:10:16 +02001283 fi
1284fi
1285
Juan Pablo Conde2ecf1862024-01-16 20:12:43 -06001286if [ "$run_config_tfut" ]; then
1287 # Get candidates for run TFUT config
1288 run_config_tfut_candidates="$("$ci_root/script/gen_run_config_candidates.py" \
1289 "--unit-testing" "$run_config_tfut")"
1290 if [ -z "$run_config_tfut_candidates" ]; then
1291 die "No run TFUT config candidates!"
1292 else
1293 echo
1294 echo "Chosen fragments:"
1295 echo
1296 echo "$run_config_tfut_candidates" | sed 's/^\|\n/\t/g'
1297 fi
1298fi
1299
Fathi Boudra422bf772019-12-02 11:10:16 +02001300call_hook "test_setup"
1301echo
1302
1303if upon "$local_ci"; then
1304 # For local runs, since each config is tried in sequence, it's
1305 # advantageous to run jobs in parallel
1306 if [ "$make_j" ]; then
1307 make_j_opts="-j $make_j"
1308 else
1309 n_cores="$(getconf _NPROCESSORS_ONLN)" 2>/dev/null || true
1310 if [ "$n_cores" ]; then
1311 make_j_opts="-j $n_cores"
1312 fi
1313 fi
1314fi
1315
Harrison Mutai07043e92023-07-06 09:41:12 +01001316# Install python build dependencies
1317if is_arm_jenkins_env; then
1318 source "$ci_root/script/install_python_deps.sh"
1319fi
1320
Juan Pablo Conde3feb7f12023-10-21 13:14:47 -05001321# Install c-picker dependency
1322if config_valid "$tfut_config"; then
1323 echo "started building"
1324 python3 -m venv .venv
1325 source .venv/bin/activate
1326
1327 if ! python3 -m pip show c-picker &> /dev/null; then
1328 echo "Installing c-picker"
1329 pip install git+https://git.trustedfirmware.org/TS/trusted-services.git@topics/c-picker || {
1330 echo "c-picker was not installed!"
1331 exit 1
1332 }
1333 echo "c-picker was installed"
1334 else
1335 echo "c-picker is already installed"
1336 fi
1337fi
1338
Manish V Badarkhed62aa5f2025-03-18 21:18:14 +00001339# Print CMake version
1340cmake_ver=$(echo `cmake --version | sed -n '1p'`)
1341echo "Using $cmake_ver"
1342
1343# Check for Ninja
1344if [ -x "$(command -v ninja)" ]; then
1345 # Print Ninja version
1346 ninja_ver=$(echo `ninja --version | sed -n '1p'`)
1347 echo "Using ninja $ninja_ver"
1348 export cmake_gen="-G Ninja"
1349else
1350 echo 'Ninja is not installed'
1351 export cmake_gen=""
1352fi
1353
1354undo_rmm_patches() {
1355 pushd "$rmm_root"
1356 patch_record="$rmm_patch_record" undo_patch_record
1357 popd
1358}
1359
Fathi Boudra422bf772019-12-02 11:10:16 +02001360modes="${bin_mode:-debug release}"
1361for mode in $modes; do
Paul Sokolovskye9962cd2021-12-17 18:39:40 +03001362 echo "===== Building package in mode: $mode ====="
Fathi Boudra422bf772019-12-02 11:10:16 +02001363 # Build with a temporary archive
1364 build_archive="$archive/$mode"
1365 mkdir "$build_archive"
1366
1367 if [ "$mode" = "debug" ]; then
Zelalem219df412020-05-17 19:21:20 -05001368 export bin_mode="debug"
Manish V Badarkhed62aa5f2025-03-18 21:18:14 +00001369 cmake_build_type="Debug"
Fathi Boudra422bf772019-12-02 11:10:16 +02001370 DEBUG=1
1371 else
Zelalem219df412020-05-17 19:21:20 -05001372 export bin_mode="release"
Manish V Badarkhed62aa5f2025-03-18 21:18:14 +00001373 cmake_build_type="Release"
Fathi Boudra422bf772019-12-02 11:10:16 +02001374 DEBUG=0
1375 fi
1376
1377 # Perform builds in a subshell so as not to pollute the current and
1378 # subsequent builds' environment
1379
Zelalem219df412020-05-17 19:21:20 -05001380 if config_valid "$cc_config"; then
1381 # Build code coverage plugin
1382 build_cc
1383 fi
1384
Fathi Boudra422bf772019-12-02 11:10:16 +02001385 # TFTF build
1386 if config_valid "$tftf_config"; then
1387 (
1388 echo "##########"
1389
Manish V Badarkhe3bd3fea2020-11-08 15:17:00 +00001390 plat_utils="$(get_tf_opt PLAT_UTILS)"
1391 if [ -z ${plat_utils} ]; then
1392 # Source platform-specific utilities.
1393 plat="$(get_tftf_opt PLAT)"
1394 plat_utils="$ci_root/${plat}_utils.sh"
1395 else
1396 # Source platform-specific utilities by
1397 # using plat_utils name.
1398 plat_utils="$ci_root/${plat_utils}.sh"
1399 fi
1400
Fathi Boudra422bf772019-12-02 11:10:16 +02001401 if [ -f "$plat_utils" ]; then
1402 source "$plat_utils"
1403 fi
1404
1405 archive="$build_archive"
Boyan Karatotev97de8d82025-03-06 15:22:21 +00001406 tftf_build_root="$archive/build/tftf"
1407 mkdir -p ${tftf_build_root}
Fathi Boudra422bf772019-12-02 11:10:16 +02001408
1409 echo "Building Trusted Firmware TF ($mode) ..." |& log_separator
1410
1411 # Call pre-build hook
1412 call_hook pre_tftf_build
1413
1414 build_tftf
1415
1416 from="$tftf_build_root" to="$archive" collect_build_artefacts
1417
1418 # Clear any local changes made by applied patches
1419 undo_tftf_patches
1420
1421 echo "##########"
1422 echo
1423 )
1424 fi
1425
Olivier Deprez0a9a3482019-12-16 14:10:31 +01001426 # SPM build
1427 if config_valid "$spm_config"; then
1428 (
1429 echo "##########"
1430
1431 # Get platform name from spm_config file
1432 plat="$(echo "$spm_config" | awk -F- '{print $1}')"
1433 plat_utils="$ci_root/${plat}_utils.sh"
1434 if [ -f "$plat_utils" ]; then
1435 source "$plat_utils"
1436 fi
1437
Daniel Boulbyb8d2a462022-03-07 13:55:25 +00001438 # Call pre-build hook
1439 call_hook pre_spm_build
1440
Manish Pandey1e7be852020-11-09 16:04:48 +00001441 # SPM build generates two sets of binaries, one for normal and other
1442 # for Secure world. We need both set of binaries for CI.
Olivier Deprez0a9a3482019-12-16 14:10:31 +01001443 archive="$build_archive"
Boyan Karatotev27057342025-07-28 09:56:23 +01001444 spm_build_root="$archive/build/spm"
1445
1446 spm_secure_build_root="$spm_build_root/$spm_secure_out_dir"
1447 spm_ns_build_root="$spm_build_root/$spm_non_secure_out_dir"
Olivier Deprez0a9a3482019-12-16 14:10:31 +01001448
Daniel Boulbyb8d2a462022-03-07 13:55:25 +00001449 echo "spm_build_root is $spm_build_root"
Olivier Deprez0a9a3482019-12-16 14:10:31 +01001450 echo "Building SPM ($mode) ..." |& log_separator
1451
1452 # NOTE: mode has no effect on SPM build (for now), hence debug
1453 # mode is built but subsequent build using release mode just
1454 # goes through with "nothing to do".
1455 build_spm
1456
1457 # Show SPM/Hafnium binary details
Boyan Karatotev27057342025-07-28 09:56:23 +01001458 cksum $spm_secure_build_root/hafnium.bin
Madhukar Pappireddyc683cf62021-11-01 14:38:32 -05001459
1460 # Some platforms only have secure configuration enabled. Hence,
1461 # non secure hanfnium binary might not be built.
Boyan Karatotev27057342025-07-28 09:56:23 +01001462 if [ -f $spm_ns_build_root/hafnium.bin ]; then
1463 cksum $spm_ns_build_root/hafnium.bin
Madhukar Pappireddyc683cf62021-11-01 14:38:32 -05001464 fi
Olivier Deprez0a9a3482019-12-16 14:10:31 +01001465
Boyan Karatotev27057342025-07-28 09:56:23 +01001466 secure_from="$spm_secure_build_root" non_secure_from="$spm_ns_build_root" to="$archive" collect_spm_artefacts
Olivier Deprez0a9a3482019-12-16 14:10:31 +01001467
1468 echo "##########"
1469 echo
1470 )
1471 fi
1472
Manish V Badarkhed62aa5f2025-03-18 21:18:14 +00001473 # TF RMM build
1474 if config_valid "$rmm_config"; then
1475 (
1476 echo "##########"
1477
1478 plat_utils="$(get_rmm_opt PLAT_UTILS)"
1479 if [ -z ${plat_utils} ]; then
1480 # Source platform-specific utilities.
1481 plat="$(get_rmm_opt PLAT)"
Manish V Badarkhea3505272025-04-17 14:20:42 +01001482 extra_options="$(get_rmm_opt EXTRA_OPTIONS)"
1483 extra_targets="$(get_rmm_opt EXTRA_TARGETS "")"
1484 rmm_toolchain="$(get_rmm_opt TOOLCHAIN gnu)"
1485 rmm_fpu_use_at_rel2="$(get_rmm_opt RMM_FPU_USE_AT_REL2 OFF)"
1486 rmm_attest_el3_token_sign="$(get_rmm_opt ATTEST_EL3_TOKEN_SIGN OFF)"
1487 rmm_v1_1="$(get_rmm_opt RMM_V1_1 ON)"
Manish V Badarkhed62aa5f2025-03-18 21:18:14 +00001488 plat_utils="$ci_root/${plat}_utils.sh"
1489 else
1490 # Source platform-specific utilities by
1491 # using plat_utils name.
1492 plat_utils="$ci_root/${plat_utils}.sh"
1493 fi
1494
1495 if [ -f "$plat_utils" ]; then
1496 source "$plat_utils"
1497 fi
1498
1499 archive="$build_archive"
1500 rmm_build_root="$rmm_root/build"
1501
1502 echo "Building Trusted Firmware RMM ($mode) ..." |& log_separator
1503
1504 #call_hook pre_rmm_build
1505 build_rmm
1506
1507 # Collect all rmm.* files: rmm.img, rmm.elf, rmm.dump, rmm.map
1508 from="$rmm_build_root" to="$archive" collect_build_artefacts
1509
1510 # Clear any local changes made by applied patches
1511 undo_rmm_patches
1512
1513 echo "##########"
1514 )
1515 fi
1516
Fathi Boudra422bf772019-12-02 11:10:16 +02001517 # TF build
1518 if config_valid "$tf_config"; then
1519 (
1520 echo "##########"
1521
Manish V Badarkhe3bd3fea2020-11-08 15:17:00 +00001522 plat_utils="$(get_tf_opt PLAT_UTILS)"
Madhukar Pappireddy2f284e12021-08-30 16:06:14 -05001523 export plat_variant="$(get_tf_opt TARGET_PLATFORM)"
1524
Manish V Badarkhe3bd3fea2020-11-08 15:17:00 +00001525 if [ -z ${plat_utils} ]; then
1526 # Source platform-specific utilities.
1527 plat="$(get_tf_opt PLAT)"
1528 plat_utils="$ci_root/${plat}_utils.sh"
1529 else
1530 # Source platform-specific utilities by
1531 # using plat_utils name.
1532 plat_utils="$ci_root/${plat_utils}.sh"
1533 fi
1534
Fathi Boudra422bf772019-12-02 11:10:16 +02001535 if [ -f "$plat_utils" ]; then
1536 source "$plat_utils"
1537 fi
1538
Chris Kaye5a486b2023-08-04 11:50:31 +00001539 fvp_tsram_size="$(get_tf_opt FVP_TRUSTED_SRAM_SIZE)"
1540 fvp_tsram_size="${fvp_tsram_size:-256}"
1541
Harrison Mutaidc703402024-08-02 14:40:16 +00001542 poetry -C "$tf_root" install --without docs
Chris Kayd0837902021-11-17 10:17:52 +00001543
Fathi Boudra422bf772019-12-02 11:10:16 +02001544 archive="$build_archive"
Boyan Karatotev97de8d82025-03-06 15:22:21 +00001545 tf_build_root="$archive/build/tfa"
1546 mkdir -p ${tf_build_root}
Fathi Boudra422bf772019-12-02 11:10:16 +02001547
1548 echo "Building Trusted Firmware ($mode) ..." |& log_separator
1549
1550 # Call pre-build hook
1551 call_hook pre_tf_build
1552
1553 build_tf
1554
1555 # Call post-build hook
1556 call_hook post_tf_build
1557
1558 # Pre-archive hook
1559 call_hook pre_tf_archive
1560
1561 from="$tf_build_root" to="$archive" collect_build_artefacts
1562
1563 # Post-archive hook
1564 call_hook post_tf_archive
1565
1566 call_hook fetch_tf_resource
1567 call_hook post_fetch_tf_resource
1568
Chris Kay4e8aaf12022-09-01 15:21:55 +01001569 # Generate LAVA job files if necessary
1570 call_hook generate_lava_job_template
1571 call_hook generate_lava_job
1572
Fathi Boudra422bf772019-12-02 11:10:16 +02001573 # Clear any local changes made by applied patches
1574 undo_tf_patches
1575
1576 echo "##########"
1577 )
1578 fi
1579
Juan Pablo Conde3feb7f12023-10-21 13:14:47 -05001580 # TFUT build
1581 if config_valid "$tfut_config"; then
1582 (
1583 echo "##########"
1584
1585 archive="$build_archive"
1586 tfut_build_root="$tfut_root/build"
1587
1588 echo "Building Trusted Firmware UT ($mode) ..." |& log_separator
1589
Juan Pablo Conde2ecf1862024-01-16 20:12:43 -06001590 # Clean TFUT build targets
1591 set_hook_var "tfut_build_targets" ""
1592
Juan Pablo Conde3feb7f12023-10-21 13:14:47 -05001593 # Call pre-build hook
1594 call_hook pre_tfut_build
1595
1596 build_tfut
1597
1598 from="$tfut_build_root" to="$archive" collect_tfut_artefacts
1599
Edward Potapovdb31e702025-06-30 16:28:40 -05001600 to="$archive" coverage="$COVERAGE" collect_tfut_coverage
1601
Juan Pablo Conde3feb7f12023-10-21 13:14:47 -05001602 echo "##########"
1603 echo
1604 )
1605 fi
Fathi Boudra422bf772019-12-02 11:10:16 +02001606 echo
1607 echo
1608done
1609
Juan Pablo Conde3feb7f12023-10-21 13:14:47 -05001610if config_valid "$tfut_config"; then
1611 deactivate
1612fi
1613
Fathi Boudra422bf772019-12-02 11:10:16 +02001614call_hook pre_package
1615
1616call_hook post_package
1617
1618if upon "$jenkins_run" && upon "$artefacts_receiver" && [ -d "artefacts" ]; then
Zelalem219df412020-05-17 19:21:20 -05001619 source "$CI_ROOT/script/send_artefacts.sh" "artefacts"
Fathi Boudra422bf772019-12-02 11:10:16 +02001620fi
1621
1622echo
1623echo "Done"