blob: e7e1fe71cd83987b6c8b110d826dd6e25d790873 [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
Javier Almansa Sobrino412d3612020-05-22 17:53:12 +0100214# Map the UART ID used for expect with the UART descriptor and port
215# used by the FPGA automation tools.
216map_uart() {
217 local port="${port:?}"
218 local descriptor="${descriptor:?}"
219 local baudrate="${baudrate:?}"
220 local run_root="${archive:?}/run"
221
222 local uart_dir="$run_root/uart${uart:?}"
223 mkdir -p "$uart_dir"
224
225 echo "$port" > "$uart_dir/port"
226 echo "$descriptor" > "$uart_dir/descriptor"
227 echo "$baudrate" > "$uart_dir/baudrate"
228
229 echo "UART${uart} mapped to port ${port} with descriptor ${descriptor} and baudrate ${baudrate}"
230}
231
Fathi Boudra422bf772019-12-02 11:10:16 +0200232# Arrange environment varibles to be set when expect scripts are launched
233set_expect_variable() {
234 local var="${1:?}"
235 local val="${2?}"
236
237 local run_root="${archive:?}/run"
238 local uart_dir="$run_root/uart${uart:?}"
239 mkdir -p "$uart_dir"
240
241 env_file="$uart_dir/env" quote="1" emit_env "$var" "$val"
242 echo "UART$uart: env has $@"
243}
244
245# Place the binary package a pointer to expect script, and its parameters
246track_expect() {
247 local file="${file:?}"
248 local timeout="${timeout-600}"
249 local run_root="${archive:?}/run"
250
251 local uart_dir="$run_root/uart${uart:?}"
252 mkdir -p "$uart_dir"
253
254 echo "$file" > "$uart_dir/expect"
255 echo "$timeout" > "$uart_dir/timeout"
Paul Sokolovskybfa8bab2023-01-25 19:34:51 +0700256 if [ -n "$lava_timeout" ]; then
257 set_run_env "lava_timeout" "$lava_timeout"
258 fi
Fathi Boudra422bf772019-12-02 11:10:16 +0200259
Paul Sokolovskybfa8bab2023-01-25 19:34:51 +0700260 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 +0200261
Chris Kayfab6edc2022-11-17 19:18:32 +0000262 if [ ! -z "${port}" ]; then
263 echo "${port}" > "$uart_dir/port"
264 fi
265
Fathi Boudra422bf772019-12-02 11:10:16 +0200266 # The run script assumes UART0 to be primary. If we're asked to set any
267 # other UART to be primary, set a run environment variable to signal
268 # that to the run script
269 if upon "$set_primary"; then
270 echo "Primary UART set to UART$uart."
271 set_run_env "primary_uart" "$uart"
272 fi
Madhukar Pappireddy1e953722021-11-08 15:23:02 -0600273
274 # UART used by payload(such as tftf, Linux) may not be the same as the
275 # primary UART. Set a run environment variable to track the payload
276 # UART which is tracked to check if the test has finished sucessfully.
277 if upon "$set_payload_uart"; then
278 echo "Payload uses UART$uart."
279 set_run_env "payload_uart" "$uart"
280 fi
Fathi Boudra422bf772019-12-02 11:10:16 +0200281}
282
283# Extract a FIP in $1 using fiptool
284extract_fip() {
285 local fip="$1"
286
287 if is_url "$1"; then
288 url="$1" fetch_file
289 fip="$(basename "$1")"
290 fi
291
Boyan Karatotevde2cd442025-07-28 09:50:29 +0100292 fiptool=$(fiptool_path)
Fathi Boudra422bf772019-12-02 11:10:16 +0200293 "$fiptool" unpack "$fip"
294 echo "Extracted FIP: $fip"
295}
296
297# Report build failure by printing a the tail end of build log. Archive the
298# build log for later inspection
299fail_build() {
300 local log_path
301
302 if upon "$jenkins_run"; then
303 log_path="$BUILD_URL/artifact/artefacts/build.log"
304 else
305 log_path="$build_log"
306 fi
307
308 echo
Leonardo Sandovalae97eda2020-11-12 10:07:03 -0600309 echo "Build failed! Full build log below:"
Fathi Boudra422bf772019-12-02 11:10:16 +0200310 echo "[...]"
311 echo
Leonardo Sandovalae97eda2020-11-12 10:07:03 -0600312 cat "$build_log"
Fathi Boudra422bf772019-12-02 11:10:16 +0200313 echo
314 echo "See $log_path for full output"
315 echo
316 cp -t "$archive" "$build_log"
317 exit 1;
318}
319
320# Build a FIP with supplied arguments
321build_fip() {
322 (
323 echo "Building FIP with arguments: $@"
324 local tf_env="$workspace/tf.env"
325
326 if [ -f "$tf_env" ]; then
327 set -a
328 source "$tf_env"
329 set +a
330 fi
331
Slava Andrianov192ee172025-06-11 15:40:43 -0500332 if [ "$(get_tf_opt MEASURED_BOOT)" = 1 ]; then
333 # These are needed for accurate hash verification
334 local build_args_path="${workspace}/fip_build_args"
335 echo $@ > $build_args_path
336 archive_file $build_args_path
337 fi
338
Boyan Karatotev99e12312025-05-02 15:00:24 +0100339 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 +0200340 ${fip_targets:-fip} &>>"$build_log" || fail_build
341 )
342}
343
Sandrine Bailleux189fdb32023-10-20 13:41:22 +0200344# Build any extra rule from TF-A makefile with supplied arguments.
345#
346# This is useful in case you need to build something else than firmware binaries
347# or the FIP.
348build_tf_extra() {
349 (
350 tf_extra_rules=${tf_extra_rules:?}
351 echo "Building extra TF rule(s): $tf_extra_rules"
352 echo " Arguments: $@"
353
354 local tf_env="$workspace/tf.env"
355
356 if [ -f "$tf_env" ]; then
357 set -a
358 source "$tf_env"
359 set +a
360 fi
361
Boyan Karatotev99e12312025-05-02 15:00:24 +0100362 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 +0200363 ${tf_extra_rules} &>>"$build_log" || fail_build
364 )
365}
366
Fathi Boudra422bf772019-12-02 11:10:16 +0200367fip_update() {
Boyan Karatotevde2cd442025-07-28 09:50:29 +0100368 fiptool=$(fiptool_path)
Fathi Boudra422bf772019-12-02 11:10:16 +0200369 # Before the update process, check if the given image is supported by
370 # the fiptool. It's assumed that both fiptool and cert_create move in
Chris Kay197b1022023-08-16 21:31:41 +0100371 # tandem, and therefore, if one has support, the other has it too.
372 if ! ("$fiptool" update 2>&1 || true) | grep -qe "\s\+--${bin_name:?}"; then
Fathi Boudra422bf772019-12-02 11:10:16 +0200373 return 1
374 fi
375
376 if not_upon "$(get_tf_opt TRUSTED_BOARD_BOOT)"; then
377 echo "Updating FIP image: $bin_name"
378 # Update HW config. Without TBBR, it's only a matter of using
379 # the update sub-command of fiptool
380 "$fiptool" update "--$bin_name" "${src:-}" \
381 "$archive/fip.bin"
382 else
383 echo "Updating FIP image (TBBR): $bin_name"
384 # With TBBR, we need to unpack, re-create certificates, and then
385 # recreate the FIP.
386 local fip_dir="$(mktempdir)"
387 local bin common_args stem
388 local rot_key="$(get_tf_opt ROT_KEY)"
389
390 rot_key="${rot_key:?}"
391 if ! is_abs "$rot_key"; then
392 rot_key="$tf_root/$rot_key"
393 fi
394
395 # Arguments only for cert_create
396 local cert_args="-n"
397 cert_args+=" --tfw-nvctr ${nvctr:-31}"
398 cert_args+=" --ntfw-nvctr ${nvctr:-223}"
399 cert_args+=" --key-alg ${KEY_ALG:-rsa}"
400 cert_args+=" --rot-key $rot_key"
401
402 local dyn_config_opts=(
Zelalem1af7a7b2020-08-04 17:34:32 -0500403 "fw-config"
Fathi Boudra422bf772019-12-02 11:10:16 +0200404 "hw-config"
405 "tb-fw-config"
406 "nt-fw-config"
407 "soc-fw-config"
408 "tos-fw-config"
409 )
410
411 # Binaries without key certificates
412 declare -A has_no_key_cert
413 for bin in "tb-fw" "${dyn_config_opts[@]}"; do
414 has_no_key_cert["$bin"]="1"
415 done
416
417 # Binaries without certificates
418 declare -A has_no_cert
419 for bin in "hw-config" "${dyn_config_opts[@]}"; do
420 has_no_cert["$bin"]="1"
421 done
422
423 pushd "$fip_dir"
424
425 # Unpack FIP
426 "$fiptool" unpack "$archive/fip.bin" &>>"$build_log"
427
428 # Remove all existing certificates
429 rm -f *-cert.bin
430
431 # Copy the binary to be updated
432 cp -f "$src" "${bin_name}.bin"
433
434 # FIP unpack dumps binaries with the same name as the option
435 # used to pack it; likewise for certificates. Reverse-engineer
436 # the command line from the binary output.
437 common_args="--trusted-key-cert trusted_key.crt"
438 for bin in *.bin; do
439 stem="${bin%%.bin}"
440 common_args+=" --$stem $bin"
441 if not_upon "${has_no_cert[$stem]}"; then
442 common_args+=" --$stem-cert $stem.crt"
443 fi
444 if not_upon "${has_no_key_cert[$stem]}"; then
445 common_args+=" --$stem-key-cert $stem-key.crt"
446 fi
447 done
448
449 # Create certificates
Boyan Karatotevde2cd442025-07-28 09:50:29 +0100450 cert_create=$(cert_create_path)
Fathi Boudra422bf772019-12-02 11:10:16 +0200451 "$cert_create" $cert_args $common_args &>>"$build_log"
452
453 # Recreate and archive FIP
454 "$fiptool" create $common_args "fip.bin" &>>"$build_log"
455 archive_file "fip.bin"
456
457 popd
458 fi
459}
460
461# Update hw-config in FIP, and remove the original DTB afterwards.
462update_fip_hw_config() {
463 # The DTB needs to be loaded by the model (and not updated in the FIP)
Madhukar Pappireddy9062ebf2021-03-02 17:07:06 -0600464 # in configs:
465 # 1. Where BL2 isn't present
466 # 2. Where we boot to Linux directly as BL33
Fathi Boudra422bf772019-12-02 11:10:16 +0200467 case "1" in
468 "$(get_tf_opt RESET_TO_BL31)" | \
Madhukar Pappireddy9062ebf2021-03-02 17:07:06 -0600469 "$(get_tf_opt ARM_LINUX_KERNEL_AS_BL33)" | \
Fathi Boudra422bf772019-12-02 11:10:16 +0200470 "$(get_tf_opt RESET_TO_SP_MIN)" | \
Maksims Svecovs7a0da522023-03-06 16:28:27 +0000471 "$(get_tf_opt RESET_TO_BL2)")
Fathi Boudra422bf772019-12-02 11:10:16 +0200472 return 0;;
473 esac
474
475 if bin_name="hw-config" src="$archive/dtb.bin" fip_update; then
476 # Remove the DTB so that model won't load it
477 rm -f "$archive/dtb.bin"
478 fi
479}
480
Fathi Boudra422bf772019-12-02 11:10:16 +0200481get_tftf_opt() {
482 (
483 name="${1:?}"
484 if config_valid "$tftf_config_file"; then
485 source "$tftf_config_file"
486 echo "${!name}"
487 fi
488 )
489}
490
491get_tf_opt() {
492 (
493 name="${1:?}"
494 if config_valid "$tf_config_file"; then
495 source "$tf_config_file"
496 echo "${!name}"
497 fi
498 )
499}
500
Manish V Badarkhed62aa5f2025-03-18 21:18:14 +0000501get_rmm_opt() {
502 (
503 name="${1:?}"
Manish V Badarkhea3505272025-04-17 14:20:42 +0100504 default="$2"
Manish V Badarkhed62aa5f2025-03-18 21:18:14 +0000505 if config_valid "$rmm_config_file"; then
506 source "$rmm_config_file"
Manish V Badarkhea3505272025-04-17 14:20:42 +0100507 # If !name is not defined, go with the default
508 # value (if defined)
509 if [ -z "${!name}" ]; then
510 echo "$default"
511 else
512 echo "${!name}"
513 fi
Manish V Badarkhed62aa5f2025-03-18 21:18:14 +0000514 fi
515 )
516}
517
Fathi Boudra422bf772019-12-02 11:10:16 +0200518build_tf() {
519 (
520 env_file="$workspace/tf.env"
521 config_file="${tf_build_config:-$tf_config_file}"
522
523 # Build fiptool and all targets by default
Harrison Mutai32de9d02023-06-12 14:23:37 +0100524 build_targets="${tf_build_targets:-fiptool all}"
Fathi Boudra422bf772019-12-02 11:10:16 +0200525
526 source "$config_file"
527
528 # If it is a TBBR build, extract the MBED TLS library from archive
Manish V Badarkhe8f125012021-12-21 05:47:52 +0000529 if [ "$(get_tf_opt TRUSTED_BOARD_BOOT)" = 1 ] ||
Manish V Badarkhef43e3f52022-06-21 20:37:25 +0100530 [ "$(get_tf_opt MEASURED_BOOT)" = 1 ] ||
531 [ "$(get_tf_opt DRTM_SUPPORT)" = 1 ]; then
Fathi Boudra422bf772019-12-02 11:10:16 +0200532 mbedtls_dir="$workspace/mbedtls"
533 if [ ! -d "$mbedtls_dir" ]; then
534 mbedtls_ar="$workspace/mbedtls.tar.gz"
535
536 url="$mbedtls_archive" saveas="$mbedtls_ar" fetch_file
537 mkdir "$mbedtls_dir"
Leonardo Sandovalec9e16c2020-09-09 14:32:40 -0500538 extract_tarball $mbedtls_ar $mbedtls_dir --strip-components=1
Fathi Boudra422bf772019-12-02 11:10:16 +0200539 fi
540
541 emit_env "MBEDTLS_DIR" "$mbedtls_dir"
542 fi
Jimmy Brisson0d5e12c2023-05-16 14:51:51 -0500543 if [ "$(get_tf_opt PLATFORM_TEST)" = "tfm-testsuite" ] &&
544 not_upon "${TF_M_TESTS_PATH}"; then
545 emit_env "TF_M_TESTS_PATH" "$WORKSPACE/tf-m-tests"
546 fi
547 if [ "$(get_tf_opt PLATFORM_TEST)" = "tfm-testsuite" ] &&
548 not_upon "${TF_M_EXTRAS_PATH}"; then
549 emit_env "TF_M_EXTRAS_PATH" "$WORKSPACE/tf-m-extras"
550 fi
David Vincze82db6932024-02-21 12:05:50 +0100551 if [ "$(get_tf_opt DICE_PROTECTION_ENVIRONMENT)" = 1 ] &&
552 not_upon "${QCBOR_DIR}"; then
553 emit_env "QCBOR_DIR" "$WORKSPACE/qcbor"
554 fi
Slava Andrianov192ee172025-06-11 15:40:43 -0500555
556 # Hash verification only occurs if there is a sufficient amount of
557 # information in the event log, which is as long as EVENT_LOG_LEVEL
558 # is set to at least 20 or if it is a debug build
559 if [[ ("$(get_tf_opt MEASURED_BOOT)" -eq 1) &&
560 (($bin_mode == "debug") || ("$(get_tf_opt EVENT_LOG_LEVEL)" -ge 20)) ]]; then
561 # This variable is later exported to the expect scripts so
562 # the hashes in the TF-A event log can be verified
563 set_run_env "verify_hashes" "1"
564 fi
Fathi Boudra422bf772019-12-02 11:10:16 +0200565 if [ -f "$env_file" ]; then
566 set -a
567 source "$env_file"
568 set +a
569 fi
570
Harrison Mutai013f6332022-02-16 16:06:33 +0000571 if is_arm_jenkins_env || upon "$local_ci"; then
572 path_list=(
573 "$llvm_dir/bin"
574 )
575 extend_path "PATH" "path_list"
576 fi
577
Boyan Karatotev97de8d82025-03-06 15:22:21 +0000578 pushd "$tf_root"
Fathi Boudra422bf772019-12-02 11:10:16 +0200579
580 # Always distclean when running on Jenkins. Skip distclean when running
581 # locally and explicitly requested.
582 if upon "$jenkins_run" || not_upon "$dont_clean"; then
Boyan Karatotev97de8d82025-03-06 15:22:21 +0000583 make distclean BUILD_BASE=$tf_build_root &>>"$build_log" || fail_build
Fathi Boudra422bf772019-12-02 11:10:16 +0200584 fi
585
586 # Log build command line. It is left unfolded on purpose to assist
587 # copying to clipboard.
588 cat <<EOF | log_separator >/dev/null
589
590Build command line:
Boyan Karatotev97de8d82025-03-06 15:22:21 +0000591 $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 +0200592
Paul Sokolovsky7f71b072023-10-16 12:59:09 +0300593CC version:
594$(${CC-${CROSS_COMPILE}gcc} -v 2>&1)
Fathi Boudra422bf772019-12-02 11:10:16 +0200595EOF
596
Javier Almansa Sobrinoe8363182020-11-10 16:40:53 +0000597 if not_upon "$local_ci"; then
598 connect_debugger=0
599 fi
600
Fathi Boudra422bf772019-12-02 11:10:16 +0200601 # Build TF. Since build output is being directed to the build log, have
602 # descriptor 3 point to the current terminal for build wrappers to vent.
Harrison Mutai6361dbe2023-02-16 14:12:40 +0000603 $tf_build_wrapper poetry run make $make_j_opts $(cat "$config_file") \
Boyan Karatotev97de8d82025-03-06 15:22:21 +0000604 DEBUG="$DEBUG" V=1 BUILD_BASE="$tf_build_root" SPIN_ON_BL1_EXIT="$connect_debugger" \
Fathi Boudra422bf772019-12-02 11:10:16 +0200605 $build_targets 3>&1 &>>"$build_log" || fail_build
Harrison Mutai32de9d02023-06-12 14:23:37 +0100606
Harrison Mutai6dafb5f2023-07-18 15:21:48 +0100607 if [ "$build_targets" != "doc" ]; then
Chris Kay9ab2d952025-05-29 13:46:24 +0100608 (poetry run memory --root "$tf_build_root" symbols 2>&1 || true) | tee -a "${build_log}"
609
610 for map in $(find "${tf_build_root}" -name '*.map'); do
611 (poetry run memory --root "${tf_build_root}" summary "${map}" 2>&1 || true) | tee -a "${build_log}"
612 done
Harrison Mutai6dafb5f2023-07-18 15:21:48 +0100613 fi
Boyan Karatotev97de8d82025-03-06 15:22:21 +0000614 popd
Fathi Boudra422bf772019-12-02 11:10:16 +0200615 )
616}
617
618build_tftf() {
619 (
620 config_file="${tftf_build_config:-$tftf_config_file}"
621
622 # Build tftf target by default
623 build_targets="${tftf_build_targets:-all}"
624
625 source "$config_file"
626
627 cd "$tftf_root"
628
629 # Always distclean when running on Jenkins. Skip distclean when running
630 # locally and explicitly requested.
631 if upon "$jenkins_run" || not_upon "$dont_clean"; then
Boyan Karatotev97de8d82025-03-06 15:22:21 +0000632 make distclean BUILD_BASE="$tftf_build_root" &>>"$build_log" || fail_build
Fathi Boudra422bf772019-12-02 11:10:16 +0200633 fi
634
635 # TFTF build system cannot reliably deal with -j option, so we avoid
636 # using that.
637
638 # Log build command line
639 cat <<EOF | log_separator >/dev/null
640
641Build command line:
Boyan Karatotev97de8d82025-03-06 15:22:21 +0000642 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 +0200643
644EOF
645
Boyan Karatotev97de8d82025-03-06 15:22:21 +0000646 make $make_j_opts $(cat "$config_file") DEBUG="$DEBUG" V=1 BUILD_BASE="$tftf_build_root" \
Fathi Boudra422bf772019-12-02 11:10:16 +0200647 $build_targets &>>"$build_log" || fail_build
648 )
649}
650
Zelalem219df412020-05-17 19:21:20 -0500651build_cc() {
652# Building code coverage plugin
653 ARM_DIR=/arm
654 pvlibversion=$(/arm/devsys-tools/abs/detag "SysGen:PVModelLib:$model_version::trunk")
655 PVLIB_HOME=$warehouse/SysGen/PVModelLib/$model_version/${pvlibversion}/external
656 if [ -n "$(find "$ARM_DIR" -maxdepth 0 -type d -empty 2>/dev/null)" ]; then
657 echo "Error: Arm warehouse not mounted. Please mount the Arm warehouse to your /arm local folder"
658 exit -1
659 fi # Error if arm warehouse not found
660 cd "$ccpathspec/scripts/tools/code_coverage/fastmodel_baremetal/bmcov"
661
662 make -C model-plugin PVLIB_HOME=$PVLIB_HOME &>>"$build_log"
663}
664
Olivier Deprez0a9a3482019-12-16 14:10:31 +0100665build_spm() {
666 (
667 env_file="$workspace/spm.env"
668 config_file="${spm_build_config:-$spm_config_file}"
669
670 source "$config_file"
671
672 if [ -f "$env_file" ]; then
673 set -a
674 source "$env_file"
675 set +a
676 fi
677
678 cd "$spm_root"
679
680 # Always clean when running on Jenkins. Skip clean when running
681 # locally and explicitly requested.
682 if upon "$jenkins_run" || not_upon "$dont_clean"; then
683 # make clean fails on a fresh repo where the project has not
684 # yet been built. Hence only clean if out/reference directory
685 # already exists.
686 if [ -d "out/reference" ]; then
687 make clean &>>"$build_log" || fail_build
688 fi
689 fi
690
691 # Log build command line. It is left unfolded on purpose to assist
692 # copying to clipboard.
693 cat <<EOF | log_separator >/dev/null
694
695Build command line:
Boyan Karatotev27057342025-07-28 09:56:23 +0100696 make $make_j_opts OUT=$spm_build_root $(cat "$config_file" | tr '\n' ' ')
Olivier Deprez0a9a3482019-12-16 14:10:31 +0100697
698EOF
699
700 # Build SPM. Since build output is being directed to the build log, have
701 # descriptor 3 point to the current terminal for build wrappers to vent.
Boyan Karatotev27057342025-07-28 09:56:23 +0100702 make $make_j_opts OUT=$spm_build_root $(cat "$config_file") 3>&1 &>>"$build_log" \
Olivier Deprez0a9a3482019-12-16 14:10:31 +0100703 || fail_build
704 )
705}
Zelalem219df412020-05-17 19:21:20 -0500706
Manish V Badarkhed62aa5f2025-03-18 21:18:14 +0000707build_rmm() {
708 (
709 env_file="$workspace/rmm.env"
710 config_file="${rmm_build_config:-$rmm_config_file}"
711
712 # Build fiptool and all targets by default
Manish V Badarkhe8cbbabf2025-08-20 11:27:12 +0100713 export CROSS_COMPILE="aarch64-none-elf-"
Manish V Badarkhed62aa5f2025-03-18 21:18:14 +0000714
715 source "$config_file"
716
717 if [ -f "$env_file" ]; then
718 set -a
719 source "$env_file"
720 set +a
721 fi
722
723 cd "$rmm_root"
724
725 if [ -f "$rmm_root/requirements.txt" ]; then
726 export PATH="$HOME/.local/bin:$PATH"
727 python3 -m pip install --upgrade pip
728 python3 -m pip install -r "$rmm_root/requirements.txt"
729 fi
730
731 # Always distclean when running on Jenkins. Skip distclean when running
732 # locally and explicitly requested.
733 if upon "$jenkins_run" || not_upon "$dont_clean"; then
734 # Remove 'rmm\build' folder
735 echo "Removing $rmm_build_root..."
736 rm -rf $rmm_build_root
737 fi
738
Manish V Badarkhea3505272025-04-17 14:20:42 +0100739 if not_upon "$local_ci"; then
740 connect_debugger=0
741 fi
742
Manish V Badarkhed62aa5f2025-03-18 21:18:14 +0000743 # Log build command line. It is left unfolded on purpose to assist
744 # copying to clipboard.
745 cat <<EOF | log_separator >/dev/null
746
747Build command line:
Manish V Badarkhea3505272025-04-17 14:20:42 +0100748 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}
749 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 +0000750
Manish V Badarkhea3505272025-04-17 14:20:42 +0100751EOF
752 cmake \
753 -DRMM_CONFIG=${plat}_defcfg $cmake_gen \
754 -S $rmm_root -B $rmm_build_root \
755 -DRMM_TOOLCHAIN=$rmm_toolchain \
756 -DRMM_FPU_USE_AT_REL2=$rmm_fpu_use_at_rel2 \
757 -DATTEST_EL3_TOKEN_SIGN=$rmm_attest_el3_token_sign \
758 -DRMM_V1_1=$rmm_v1_1 \
759 ${extra_options}
760 cmake --build $rmm_build_root --config $cmake_build_type $make_j_opts -v ${extra_targets+-- $extra_targets} 3>&1 &>>"$build_log" || fail_build
761 )
Manish V Badarkhed62aa5f2025-03-18 21:18:14 +0000762}
763
Juan Pablo Conde3feb7f12023-10-21 13:14:47 -0500764build_tfut() {
765 (
766 config_file="${tfut_build_config:-$tfut_config_file}"
767
768 # Build tfut target by default
769 build_targets="${tfut_build_targets:-all}"
770
771 source "$config_file"
772
773 mkdir -p "$tfut_root/build"
774 cd "$tfut_root/build"
775
776 # Always distclean when running on Jenkins. Skip distclean when running
777 # locally and explicitly requested.
778 if upon "$jenkins_run" || not_upon "$dont_clean"; then
779 #make clean &>>"$build_log" || fail_build
780 rm -Rf * || fail_build
781 fi
782
783 #Override build targets only if the run config did not set them.
784 if [ $build_targets == "all" ]; then
785 tests_line=$(cat "$config_file" | { grep "tests=" || :; })
786 if [ -z "$tests_line" ]; then
787 build_targets=$(echo "$tests_line" | awk -F= '{ print $NF }')
788 fi
789 fi
790
Juan Pablo Conde2ecf1862024-01-16 20:12:43 -0600791 #TODO: extract vars from env to use them for cmake
792
793 test -f "$config_file"
794
795 config=$(cat "$config_file" | grep -v "tests=") \
796 && cmake_config=$(echo "$config" | sed -e 's/^/\-D/')
Juan Pablo Conde3feb7f12023-10-21 13:14:47 -0500797
798 # Check if cmake is installed
799 if ! command -v cmake &> /dev/null
800 then
801 echo "cmake could not be found"
802 exit 1
803 fi
804
805 # Log build command line
806 cat <<EOF | log_separator >/dev/null
807
808Build command line:
809 cmake $(echo "$cmake_config") -G"Unix Makefiles" --debug-output -DCMAKE_VERBOSE_MAKEFILE -DUNIT_TEST_PROJECT_PATH="$tf_root" ..
810 make $(echo "$config" | tr '\n' ' ') DEBUG=$DEBUG V=1 $build_targets
811
812EOF
813 cmake $(echo "$cmake_config") -G"Unix Makefiles" --debug-output \
814 -DCMAKE_VERBOSE_MAKEFILE=ON \
815 -DUNIT_TEST_PROJECT_PATH="$tf_root" \
816 .. &>> "$build_log" || fail_build
817 echo "Done with cmake" >> "$build_log"
818 make $(echo "$config") VERBOSE=1 \
819 $build_targets &>> "$build_log" || fail_build
820 )
821
822}
823
Fathi Boudra422bf772019-12-02 11:10:16 +0200824# Set metadata for the whole package so that it can be used by both Jenkins and
825# shell
826set_package_var() {
827 env_file="$artefacts/env" emit_env "$@"
828}
829
830set_tf_build_targets() {
831 echo "Set build target to '${targets:?}'"
832 set_hook_var "tf_build_targets" "$targets"
833}
834
835set_tftf_build_targets() {
836 echo "Set build target to '${targets:?}'"
837 set_hook_var "tftf_build_targets" "$targets"
838}
839
Olivier Deprez0a9a3482019-12-16 14:10:31 +0100840set_spm_build_targets() {
841 echo "Set build target to '${targets:?}'"
842 set_hook_var "spm_build_targets" "$targets"
843}
844
Juan Pablo Conde2ecf1862024-01-16 20:12:43 -0600845add_tfut_build_targets() {
846 echo "Add TFUT build targets '${targets:?}'"
847 append_hook_var "tfut_build_targets" "$targets "
Juan Pablo Conde3feb7f12023-10-21 13:14:47 -0500848}
849
Daniel Boulbyb8d2a462022-03-07 13:55:25 +0000850set_spm_out_dir() {
851 echo "Set SPMC binary build to '${out_dir:?}'"
852 set_hook_var "spm_secure_out_dir" "$out_dir"
853}
Fathi Boudra422bf772019-12-02 11:10:16 +0200854# Look under $archive directory for known files such as blX images, kernel, DTB,
855# initrd etc. For each known file foo, if foo.bin exists, then set variable
856# foo_bin to the path of the file. Make the path relative to the workspace so as
857# to remove any @ characters, which Jenkins inserts for parallel runs. If the
858# file doesn't exist, unset its path.
859set_default_bin_paths() {
860 local image image_name image_path path
861 local archive="${archive:?}"
862 local set_vars
863 local var
864
865 pushd "$archive"
866
867 for file in *.bin; do
868 # Get a shell variable from the file's stem
869 var_name="${file%%.*}_bin"
870 var_name="$(echo "$var_name" | sed -r 's/[^[:alnum:]]/_/g')"
871
872 # Skip setting the variable if it's already
873 if [ "${!var_name}" ]; then
874 echo "Note: not setting $var_name; already set to ${!var_name}"
875 continue
876 else
877 set_vars+="$var_name "
878 fi
879
880 eval "$var_name=$file"
881 done
882
883 echo "Binary paths set for: "
884 {
885 for var in $set_vars; do
886 echo -n "\$$var "
887 done
888 } | fmt -80 | sed 's/^/ /'
889 echo
890
891 popd
892}
893
894gen_model_params() {
895 local model_param_file="$archive/model_params"
Javier Almansa Sobrinoe8363182020-11-10 16:40:53 +0000896 [ "$connect_debugger" ] && [ "$connect_debugger" -eq 1 ] && wait_debugger=1
Fathi Boudra422bf772019-12-02 11:10:16 +0200897
898 set_default_bin_paths
899 echo "Generating model parameter for $model..."
900 source "$ci_root/model/${model:?}.sh"
901 archive_file "$model_param_file"
902}
903
904set_model_path() {
905 set_run_env "model_path" "${1:?}"
906}
907
Zelalem1af7a7b2020-08-04 17:34:32 -0500908set_model_env() {
909 local var="${1:?}"
910 local val="${2?}"
911 local run_root="${archive:?}/run"
912
913 mkdir -p "$run_root"
914 echo "export $var=$val" >> "$run_root/model_env"
915}
Fathi Boudra422bf772019-12-02 11:10:16 +0200916set_run_env() {
917 local var="${1:?}"
918 local val="${2?}"
919 local run_root="${archive:?}/run"
920
921 mkdir -p "$run_root"
922 env_file="$run_root/env" quote="1" emit_env "$var" "$val"
923}
924
925show_head() {
926 # Display HEAD descripton
927 pushd "$1"
928 git show --quiet --no-color | sed 's/^/ > /g'
929 echo
930 popd
931}
932
933# Choose debug binaries to run; by default, release binaries are chosen to run
934use_debug_bins() {
935 local run_root="${archive:?}/run"
936
937 echo "Choosing debug binaries for execution"
938 set_package_var "BIN_MODE" "debug"
939}
940
941assert_can_git_clone() {
942 local name="${1:?}"
943 local dir="${!name}"
944
945 # If it doesn't exist, it can be cloned into
946 if [ ! -e "$dir" ]; then
947 return 0
948 fi
949
950 # If it's a directory, it must be a Git clone already
951 if [ -d "$dir" ] && [ -d "$dir/.git" ]; then
952 # No need to clone again
953 echo "Using existing git clone for $name: $dir"
954 return 1
955 fi
956
957 die "Path $dir exists but is not a git clone"
958}
959
960clone_repo() {
961 if ! is_url "${clone_url?}"; then
962 # For --depth to take effect on local paths, it needs to use the
963 # file:// scheme.
964 clone_url="file://$clone_url"
965 fi
966
967 git clone -q --depth 1 "$clone_url" "${where?}"
968 if [ "$refspec" ]; then
969 pushd "$where"
970 git fetch -q --depth 1 origin "$refspec"
971 git checkout -q FETCH_HEAD
972 popd
973 fi
974}
975
976build_unstable() {
977 echo "--BUILD UNSTABLE--" | tee -a "$build_log"
978}
979
980undo_patch_record() {
981 if [ ! -f "${patch_record:?}" ]; then
982 return
983 fi
984
985 # Undo patches in reverse
986 echo
987 for patch_name in $(tac "$patch_record"); do
988 echo "Undoing $patch_name..."
989 if ! git apply -R "$ci_root/patch/$patch_name"; then
990 if upon "$local_ci"; then
991 echo
992 echo "Your local directory may have been dirtied."
993 echo
994 fi
995 fail_build
996 fi
997 done
998
999 rm -f "$patch_record"
1000}
1001
1002undo_local_patches() {
1003 pushd "$tf_root"
1004 patch_record="$tf_patch_record" undo_patch_record
1005 popd
1006
1007 if [ -d "$tftf_root" ]; then
1008 pushd "$tftf_root"
1009 patch_record="$tftf_patch_record" undo_patch_record
1010 popd
1011 fi
1012}
1013
1014undo_tftf_patches() {
1015 pushd "$tftf_root"
1016 patch_record="$tftf_patch_record" undo_patch_record
1017 popd
1018}
1019
1020undo_tf_patches() {
1021 pushd "$tf_root"
1022 patch_record="$tf_patch_record" undo_patch_record
1023 popd
1024}
1025
1026apply_patch() {
1027 # If skip_patches is set, the developer has applied required patches
1028 # manually. They probably want to keep them applied for debugging
1029 # purposes too. This means we don't have to apply/revert them as part of
1030 # build process.
1031 if upon "$skip_patches"; then
1032 echo "Skipped applying ${1:?}..."
1033 return 0
1034 else
1035 echo "Applying ${1:?}..."
1036 fi
1037
Sandrine Bailleux4cb8c222023-09-13 13:48:15 +02001038 if git apply --reverse --check < "$ci_root/patch/$1" 2> /dev/null; then
Jimmy Brissonf134e4c2023-03-22 13:20:20 -05001039 echo "Skipping already applied ${1:?}"
1040 return 0
1041 fi
1042
Fathi Boudra422bf772019-12-02 11:10:16 +02001043 if git apply < "$ci_root/patch/$1"; then
1044 echo "$1" >> "${patch_record:?}"
1045 else
1046 if upon "$local_ci"; then
1047 undo_local_patches
1048 fi
1049 fail_build
1050 fi
1051}
1052
Fathi Boudra422bf772019-12-02 11:10:16 +02001053apply_tf_patch() {
Boyan Karatotevfaf9a9d2025-07-28 09:52:05 +01001054 root="$tf_root"
1055 new_root="$archive/tfa_mirror"
1056
1057 # paralell builds are only used locally. Don't do for CI since this will
1058 # have a speed penalty. Also skip if this was already done as a single
1059 # job may apply many patches.
1060 if upon "$local_ci" && [[ ! -d $new_root ]]; then
1061 root=$new_root
1062 diff=$(mktempfile)
1063
1064 # get anything still uncommitted
1065 pushd $tf_root
1066 git diff HEAD > $diff
1067 popd
1068
1069 # git will hard link when cloning locally, no need for --depth=1
1070 git clone "$tf_root" $root --shallow-submodules
1071
1072 tf_root=$root # next apply_tf_patch will run in the same hook
1073 set_hook_var "tf_root" "$root" # for anyone outside the hook
1074
1075 # apply uncommited changes so they are picked up in the build
1076 pushd $tf_root
1077 git apply $diff &> /dev/null || true
1078 popd
1079
1080 fi
1081
1082 pushd "$root"
Fathi Boudra422bf772019-12-02 11:10:16 +02001083 patch_record="$tf_patch_record" apply_patch "$1"
1084 popd
1085}
1086
Fathi Boudra422bf772019-12-02 11:10:16 +02001087mkdir -p "$workspace"
1088mkdir -p "$archive"
1089set_package_var "TEST_CONFIG" "$test_config"
1090
1091{
1092echo
1093echo "CONFIGURATION: $test_group/$test_config"
1094echo
1095} |& log_separator
1096
1097tf_config="$(echo "$build_configs" | awk -F, '{print $1}')"
1098tftf_config="$(echo "$build_configs" | awk -F, '{print $2}')"
Chris Kay4f7846a2025-08-04 19:56:35 +01001099spm_config="$(echo "$build_configs" | awk -F, '{print $3}')"
1100rmm_config="$(echo "$build_configs" | awk -F, '{print $4}')"
Juan Pablo Conde3feb7f12023-10-21 13:14:47 -05001101tfut_config="$(echo "$build_configs" | awk -F, '{print $5}')"
Fathi Boudra422bf772019-12-02 11:10:16 +02001102
1103test_config_file="$ci_root/group/$test_group/$test_config"
1104
1105tf_config_file="$ci_root/tf_config/$tf_config"
1106tftf_config_file="$ci_root/tftf_config/$tftf_config"
Olivier Deprez0a9a3482019-12-16 14:10:31 +01001107spm_config_file="$ci_root/spm_config/$spm_config"
Manish V Badarkhed62aa5f2025-03-18 21:18:14 +00001108rmm_config_file="$ci_root/rmm_config/$rmm_config"
Juan Pablo Conde3feb7f12023-10-21 13:14:47 -05001109tfut_config_file="$ci_root/tfut_config/$tfut_config"
Fathi Boudra422bf772019-12-02 11:10:16 +02001110
1111# File that keeps track of applied patches
1112tf_patch_record="$workspace/tf_patches"
1113tftf_patch_record="$workspace/tftf_patches"
1114
Juan Pablo Conde84bf39f2024-01-12 22:09:49 -06001115# Split run config into TF and TFUT components
1116run_config_tfa="$(echo "$run_config" | awk -F, '{print $1}')"
1117run_config_tfut="$(echo "$run_config" | awk -F, '{print $2}')"
1118
Fathi Boudra422bf772019-12-02 11:10:16 +02001119pushd "$workspace"
1120
1121if ! config_valid "$tf_config"; then
1122 tf_config=
1123else
1124 echo "Trusted Firmware config:"
1125 echo
1126 sort "$tf_config_file" | sed '/^\s*$/d;s/^/\t/'
1127 echo
1128fi
1129
1130if ! config_valid "$tftf_config"; then
1131 tftf_config=
1132else
1133 echo "Trusted Firmware TF config:"
1134 echo
1135 sort "$tftf_config_file" | sed '/^\s*$/d;s/^/\t/'
1136 echo
1137fi
1138
Olivier Deprez0a9a3482019-12-16 14:10:31 +01001139if ! config_valid "$spm_config"; then
1140 spm_config=
1141else
1142 echo "SPM config:"
1143 echo
1144 sort "$spm_config_file" | sed '/^\s*$/d;s/^/\t/'
Zelalem219df412020-05-17 19:21:20 -05001145 echo
1146fi
1147
Manish V Badarkhed62aa5f2025-03-18 21:18:14 +00001148# File that keeps track of applied patches
1149rmm_patch_record="$workspace/rmm_patches"
1150
1151if ! config_valid "$rmm_config"; then
1152 rmm_config=
1153else
1154 echo "Trusted Firmware RMM config:"
1155 echo
1156 sort "$rmm_config_file" | sed '/^\s*$/d;s/^/\t/'
1157 echo
1158fi
1159
Juan Pablo Conde3feb7f12023-10-21 13:14:47 -05001160if ! config_valid "$tfut_config"; then
1161 tfut_config=
1162else
1163 echo "TFUT config:"
1164 echo
1165 sort "$tfut_config_file" | sed '/^\s*$/d;s/^/\t/'
1166 echo
1167fi
1168
Juan Pablo Conde84bf39f2024-01-12 22:09:49 -06001169if ! config_valid "$run_config_tfa"; then
1170 run_config_tfa=
Fathi Boudra422bf772019-12-02 11:10:16 +02001171fi
1172
Juan Pablo Conde3feb7f12023-10-21 13:14:47 -05001173if { [ "$tf_config" ] || [ "$tfut_config" ]; } && assert_can_git_clone "tf_root"; then
Fathi Boudra422bf772019-12-02 11:10:16 +02001174 # If the Trusted Firmware repository has already been checked out, use
1175 # that location. Otherwise, clone one ourselves.
1176 echo "Cloning Trusted Firmware..."
1177 clone_url="${TF_CHECKOUT_LOC:-$tf_src_repo_url}" where="$tf_root" \
1178 refspec="$TF_REFSPEC" clone_repo &>>"$build_log"
1179 show_head "$tf_root"
1180fi
1181
1182if [ "$tftf_config" ] && assert_can_git_clone "tftf_root"; then
1183 # If the Trusted Firmware TF repository has already been checked out,
1184 # use that location. Otherwise, clone one ourselves.
1185 echo "Cloning Trusted Firmware TF..."
1186 clone_url="${TFTF_CHECKOUT_LOC:-$tftf_src_repo_url}" where="$tftf_root" \
1187 refspec="$TFTF_REFSPEC" clone_repo &>>"$build_log"
1188 show_head "$tftf_root"
1189fi
1190
Zelalem219df412020-05-17 19:21:20 -05001191if [ -n "$cc_config" ] ; then
1192 if [ "$cc_config" -eq 1 ] && assert_can_git_clone "cc_root"; then
1193 # Copy code coverage repository
1194 echo "Cloning Code Coverage..."
1195 git clone -q $cc_src_repo_url cc_plugin --depth 1 -b $cc_src_repo_tag > /dev/null
1196 show_head "$cc_root"
1197 fi
1198fi
1199
Daniel Boulby25385ab2023-12-14 14:36:25 +00001200if [ "$spm_config" ] ; then
1201 if assert_can_git_clone "spm_root"; then
1202 # If the SPM repository has already been checked out, use
1203 # that location. Otherwise, clone one ourselves.
1204 echo "Cloning SPM..."
1205 clone_url="${SPM_CHECKOUT_LOC:-$spm_src_repo_url}" \
1206 where="$spm_root" refspec="$SPM_REFSPEC" \
1207 clone_repo &>>"$build_log"
1208 fi
Olivier Deprez0a9a3482019-12-16 14:10:31 +01001209
1210 # Query git submodules
1211 pushd "$spm_root"
Daniel Boulby25385ab2023-12-14 14:36:25 +00001212 # Check if submodules need initialising
Paul Sokolovskyad274422024-09-01 10:27:56 +03001213
1214 # This handling is needed to reliably fetch submodules
1215 # in CI environment.
1216 for subm in $(git submodule status | awk '/^-/ {print $2}'); do
1217 for i in $(seq 1 7); do
1218 git submodule init $subm
1219 if git submodule update $subm; then
1220 break
1221 fi
1222 git submodule deinit --force $subm
1223 echo "Retrying $subm"
1224 sleep $((RANDOM % 10 + 5))
1225 done
1226 done
1227
1228 git submodule status
Olivier Deprez0a9a3482019-12-16 14:10:31 +01001229 popd
1230
1231 show_head "$spm_root"
1232fi
1233
Manish V Badarkhed62aa5f2025-03-18 21:18:14 +00001234if [ "$rmm_config" ] && assert_can_git_clone "rmm_root"; then
Manish V Badarkhe41909452025-04-11 12:06:45 +01001235 # If the RMM repository has already been checked out,
Manish V Badarkhed62aa5f2025-03-18 21:18:14 +00001236 # use that location. Otherwise, clone one ourselves.
1237 echo "Cloning TF-RMM..."
1238 clone_url="${RMM_CHECKOUT_LOC:-$rmm_src_repo_url}" where="$rmm_root" \
1239 refspec="$RMM_REFSPEC" clone_repo &>>"$build_log"
1240 show_head "$rmm_root"
1241fi
1242
Juan Pablo Conde3feb7f12023-10-21 13:14:47 -05001243if [ "$tfut_config" ] && assert_can_git_clone "tfut_root"; then
1244 # If the Trusted Firmware UT repository has already been checked out,
1245 # use that location. Otherwise, clone one ourselves.
1246 echo "Cloning Trusted Firmware UT..."
1247 clone_url="${TFUT_CHECKOUT_LOC:-$tfut_src_repo_url}" where="$tfut_root" \
1248 refspec="$TFUT_GERRIT_REFSPEC" clone_repo &>>"$build_log"
1249 show_head "$tfut_root"
1250fi
1251
Juan Pablo Conde84bf39f2024-01-12 22:09:49 -06001252if [ "$run_config_tfa" ]; then
1253 # Get candidates for TF-A run config
Nicola Mazzucato7fc5abd2024-02-23 21:32:48 +00001254 run_config_candidates="$("$ci_root/script/gen_run_config_candidates.py" \
Juan Pablo Conde84bf39f2024-01-12 22:09:49 -06001255 "$run_config_tfa")"
Nicola Mazzucato7fc5abd2024-02-23 21:32:48 +00001256 if [ -z "$run_config_candidates" ]; then
Fathi Boudra422bf772019-12-02 11:10:16 +02001257 die "No run config candidates!"
1258 else
1259 echo
1260 echo "Chosen fragments:"
1261 echo
Nicola Mazzucato7fc5abd2024-02-23 21:32:48 +00001262 echo "$run_config_candidates" | sed 's/^\|\n/\t/g'
Fathi Boudra422bf772019-12-02 11:10:16 +02001263 echo
Harrison Mutai4dfe1192024-07-03 12:35:38 +00001264
1265 if [ ! -n "$bin_mode" ]; then
1266 if echo $run_config_candidates | grep -wq "debug"; then
1267 bin_mode="debug"
1268 else
1269 bin_mode="release"
1270 fi
1271 fi
Fathi Boudra422bf772019-12-02 11:10:16 +02001272 fi
1273fi
1274
Juan Pablo Conde2ecf1862024-01-16 20:12:43 -06001275if [ "$run_config_tfut" ]; then
1276 # Get candidates for run TFUT config
1277 run_config_tfut_candidates="$("$ci_root/script/gen_run_config_candidates.py" \
1278 "--unit-testing" "$run_config_tfut")"
1279 if [ -z "$run_config_tfut_candidates" ]; then
1280 die "No run TFUT config candidates!"
1281 else
1282 echo
1283 echo "Chosen fragments:"
1284 echo
1285 echo "$run_config_tfut_candidates" | sed 's/^\|\n/\t/g'
1286 fi
1287fi
1288
Fathi Boudra422bf772019-12-02 11:10:16 +02001289call_hook "test_setup"
1290echo
1291
1292if upon "$local_ci"; then
1293 # For local runs, since each config is tried in sequence, it's
1294 # advantageous to run jobs in parallel
1295 if [ "$make_j" ]; then
1296 make_j_opts="-j $make_j"
1297 else
1298 n_cores="$(getconf _NPROCESSORS_ONLN)" 2>/dev/null || true
1299 if [ "$n_cores" ]; then
1300 make_j_opts="-j $n_cores"
1301 fi
1302 fi
1303fi
1304
Harrison Mutai07043e92023-07-06 09:41:12 +01001305# Install python build dependencies
1306if is_arm_jenkins_env; then
1307 source "$ci_root/script/install_python_deps.sh"
1308fi
1309
Juan Pablo Conde3feb7f12023-10-21 13:14:47 -05001310# Install c-picker dependency
1311if config_valid "$tfut_config"; then
1312 echo "started building"
1313 python3 -m venv .venv
1314 source .venv/bin/activate
1315
1316 if ! python3 -m pip show c-picker &> /dev/null; then
1317 echo "Installing c-picker"
1318 pip install git+https://git.trustedfirmware.org/TS/trusted-services.git@topics/c-picker || {
1319 echo "c-picker was not installed!"
1320 exit 1
1321 }
1322 echo "c-picker was installed"
1323 else
1324 echo "c-picker is already installed"
1325 fi
1326fi
1327
Manish V Badarkhed62aa5f2025-03-18 21:18:14 +00001328# Print CMake version
1329cmake_ver=$(echo `cmake --version | sed -n '1p'`)
1330echo "Using $cmake_ver"
1331
1332# Check for Ninja
1333if [ -x "$(command -v ninja)" ]; then
1334 # Print Ninja version
1335 ninja_ver=$(echo `ninja --version | sed -n '1p'`)
1336 echo "Using ninja $ninja_ver"
1337 export cmake_gen="-G Ninja"
1338else
1339 echo 'Ninja is not installed'
1340 export cmake_gen=""
1341fi
1342
1343undo_rmm_patches() {
1344 pushd "$rmm_root"
1345 patch_record="$rmm_patch_record" undo_patch_record
1346 popd
1347}
1348
Fathi Boudra422bf772019-12-02 11:10:16 +02001349modes="${bin_mode:-debug release}"
1350for mode in $modes; do
Paul Sokolovskye9962cd2021-12-17 18:39:40 +03001351 echo "===== Building package in mode: $mode ====="
Fathi Boudra422bf772019-12-02 11:10:16 +02001352 # Build with a temporary archive
1353 build_archive="$archive/$mode"
1354 mkdir "$build_archive"
1355
1356 if [ "$mode" = "debug" ]; then
Zelalem219df412020-05-17 19:21:20 -05001357 export bin_mode="debug"
Manish V Badarkhed62aa5f2025-03-18 21:18:14 +00001358 cmake_build_type="Debug"
Fathi Boudra422bf772019-12-02 11:10:16 +02001359 DEBUG=1
1360 else
Zelalem219df412020-05-17 19:21:20 -05001361 export bin_mode="release"
Manish V Badarkhed62aa5f2025-03-18 21:18:14 +00001362 cmake_build_type="Release"
Fathi Boudra422bf772019-12-02 11:10:16 +02001363 DEBUG=0
1364 fi
1365
1366 # Perform builds in a subshell so as not to pollute the current and
1367 # subsequent builds' environment
1368
Zelalem219df412020-05-17 19:21:20 -05001369 if config_valid "$cc_config"; then
1370 # Build code coverage plugin
1371 build_cc
1372 fi
1373
Fathi Boudra422bf772019-12-02 11:10:16 +02001374 # TFTF build
1375 if config_valid "$tftf_config"; then
1376 (
1377 echo "##########"
1378
Manish V Badarkhe3bd3fea2020-11-08 15:17:00 +00001379 plat_utils="$(get_tf_opt PLAT_UTILS)"
1380 if [ -z ${plat_utils} ]; then
1381 # Source platform-specific utilities.
1382 plat="$(get_tftf_opt PLAT)"
1383 plat_utils="$ci_root/${plat}_utils.sh"
1384 else
1385 # Source platform-specific utilities by
1386 # using plat_utils name.
1387 plat_utils="$ci_root/${plat_utils}.sh"
1388 fi
1389
Fathi Boudra422bf772019-12-02 11:10:16 +02001390 if [ -f "$plat_utils" ]; then
1391 source "$plat_utils"
1392 fi
1393
1394 archive="$build_archive"
Boyan Karatotev97de8d82025-03-06 15:22:21 +00001395 tftf_build_root="$archive/build/tftf"
1396 mkdir -p ${tftf_build_root}
Fathi Boudra422bf772019-12-02 11:10:16 +02001397
1398 echo "Building Trusted Firmware TF ($mode) ..." |& log_separator
1399
1400 # Call pre-build hook
1401 call_hook pre_tftf_build
1402
1403 build_tftf
1404
1405 from="$tftf_build_root" to="$archive" collect_build_artefacts
1406
1407 # Clear any local changes made by applied patches
1408 undo_tftf_patches
1409
1410 echo "##########"
1411 echo
1412 )
1413 fi
1414
Olivier Deprez0a9a3482019-12-16 14:10:31 +01001415 # SPM build
1416 if config_valid "$spm_config"; then
1417 (
1418 echo "##########"
1419
1420 # Get platform name from spm_config file
1421 plat="$(echo "$spm_config" | awk -F- '{print $1}')"
1422 plat_utils="$ci_root/${plat}_utils.sh"
1423 if [ -f "$plat_utils" ]; then
1424 source "$plat_utils"
1425 fi
1426
Daniel Boulbyb8d2a462022-03-07 13:55:25 +00001427 # Call pre-build hook
1428 call_hook pre_spm_build
1429
Manish Pandey1e7be852020-11-09 16:04:48 +00001430 # SPM build generates two sets of binaries, one for normal and other
1431 # for Secure world. We need both set of binaries for CI.
Olivier Deprez0a9a3482019-12-16 14:10:31 +01001432 archive="$build_archive"
Boyan Karatotev27057342025-07-28 09:56:23 +01001433 spm_build_root="$archive/build/spm"
1434
1435 spm_secure_build_root="$spm_build_root/$spm_secure_out_dir"
1436 spm_ns_build_root="$spm_build_root/$spm_non_secure_out_dir"
Olivier Deprez0a9a3482019-12-16 14:10:31 +01001437
Daniel Boulbyb8d2a462022-03-07 13:55:25 +00001438 echo "spm_build_root is $spm_build_root"
Olivier Deprez0a9a3482019-12-16 14:10:31 +01001439 echo "Building SPM ($mode) ..." |& log_separator
1440
1441 # NOTE: mode has no effect on SPM build (for now), hence debug
1442 # mode is built but subsequent build using release mode just
1443 # goes through with "nothing to do".
1444 build_spm
1445
1446 # Show SPM/Hafnium binary details
Boyan Karatotev27057342025-07-28 09:56:23 +01001447 cksum $spm_secure_build_root/hafnium.bin
Madhukar Pappireddyc683cf62021-11-01 14:38:32 -05001448
1449 # Some platforms only have secure configuration enabled. Hence,
1450 # non secure hanfnium binary might not be built.
Boyan Karatotev27057342025-07-28 09:56:23 +01001451 if [ -f $spm_ns_build_root/hafnium.bin ]; then
1452 cksum $spm_ns_build_root/hafnium.bin
Madhukar Pappireddyc683cf62021-11-01 14:38:32 -05001453 fi
Olivier Deprez0a9a3482019-12-16 14:10:31 +01001454
Boyan Karatotev27057342025-07-28 09:56:23 +01001455 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 +01001456
1457 echo "##########"
1458 echo
1459 )
1460 fi
1461
Manish V Badarkhed62aa5f2025-03-18 21:18:14 +00001462 # TF RMM build
1463 if config_valid "$rmm_config"; then
1464 (
1465 echo "##########"
1466
1467 plat_utils="$(get_rmm_opt PLAT_UTILS)"
1468 if [ -z ${plat_utils} ]; then
1469 # Source platform-specific utilities.
1470 plat="$(get_rmm_opt PLAT)"
Manish V Badarkhea3505272025-04-17 14:20:42 +01001471 extra_options="$(get_rmm_opt EXTRA_OPTIONS)"
1472 extra_targets="$(get_rmm_opt EXTRA_TARGETS "")"
1473 rmm_toolchain="$(get_rmm_opt TOOLCHAIN gnu)"
1474 rmm_fpu_use_at_rel2="$(get_rmm_opt RMM_FPU_USE_AT_REL2 OFF)"
1475 rmm_attest_el3_token_sign="$(get_rmm_opt ATTEST_EL3_TOKEN_SIGN OFF)"
1476 rmm_v1_1="$(get_rmm_opt RMM_V1_1 ON)"
Manish V Badarkhed62aa5f2025-03-18 21:18:14 +00001477 plat_utils="$ci_root/${plat}_utils.sh"
1478 else
1479 # Source platform-specific utilities by
1480 # using plat_utils name.
1481 plat_utils="$ci_root/${plat_utils}.sh"
1482 fi
1483
1484 if [ -f "$plat_utils" ]; then
1485 source "$plat_utils"
1486 fi
1487
1488 archive="$build_archive"
1489 rmm_build_root="$rmm_root/build"
1490
1491 echo "Building Trusted Firmware RMM ($mode) ..." |& log_separator
1492
1493 #call_hook pre_rmm_build
1494 build_rmm
1495
1496 # Collect all rmm.* files: rmm.img, rmm.elf, rmm.dump, rmm.map
1497 from="$rmm_build_root" to="$archive" collect_build_artefacts
1498
1499 # Clear any local changes made by applied patches
1500 undo_rmm_patches
1501
1502 echo "##########"
1503 )
1504 fi
1505
Fathi Boudra422bf772019-12-02 11:10:16 +02001506 # TF build
1507 if config_valid "$tf_config"; then
1508 (
1509 echo "##########"
1510
Manish V Badarkhe3bd3fea2020-11-08 15:17:00 +00001511 plat_utils="$(get_tf_opt PLAT_UTILS)"
Madhukar Pappireddy2f284e12021-08-30 16:06:14 -05001512 export plat_variant="$(get_tf_opt TARGET_PLATFORM)"
1513
Manish V Badarkhe3bd3fea2020-11-08 15:17:00 +00001514 if [ -z ${plat_utils} ]; then
1515 # Source platform-specific utilities.
1516 plat="$(get_tf_opt PLAT)"
1517 plat_utils="$ci_root/${plat}_utils.sh"
1518 else
1519 # Source platform-specific utilities by
1520 # using plat_utils name.
1521 plat_utils="$ci_root/${plat_utils}.sh"
1522 fi
1523
Fathi Boudra422bf772019-12-02 11:10:16 +02001524 if [ -f "$plat_utils" ]; then
1525 source "$plat_utils"
1526 fi
1527
Chris Kaye5a486b2023-08-04 11:50:31 +00001528 fvp_tsram_size="$(get_tf_opt FVP_TRUSTED_SRAM_SIZE)"
1529 fvp_tsram_size="${fvp_tsram_size:-256}"
1530
Harrison Mutaidc703402024-08-02 14:40:16 +00001531 poetry -C "$tf_root" install --without docs
Chris Kayd0837902021-11-17 10:17:52 +00001532
Fathi Boudra422bf772019-12-02 11:10:16 +02001533 archive="$build_archive"
Boyan Karatotev97de8d82025-03-06 15:22:21 +00001534 tf_build_root="$archive/build/tfa"
1535 mkdir -p ${tf_build_root}
Fathi Boudra422bf772019-12-02 11:10:16 +02001536
1537 echo "Building Trusted Firmware ($mode) ..." |& log_separator
1538
1539 # Call pre-build hook
1540 call_hook pre_tf_build
1541
1542 build_tf
1543
1544 # Call post-build hook
1545 call_hook post_tf_build
1546
1547 # Pre-archive hook
1548 call_hook pre_tf_archive
1549
1550 from="$tf_build_root" to="$archive" collect_build_artefacts
1551
1552 # Post-archive hook
1553 call_hook post_tf_archive
1554
1555 call_hook fetch_tf_resource
1556 call_hook post_fetch_tf_resource
1557
Chris Kay4e8aaf12022-09-01 15:21:55 +01001558 # Generate LAVA job files if necessary
1559 call_hook generate_lava_job_template
1560 call_hook generate_lava_job
1561
Fathi Boudra422bf772019-12-02 11:10:16 +02001562 # Clear any local changes made by applied patches
1563 undo_tf_patches
1564
1565 echo "##########"
1566 )
1567 fi
1568
Juan Pablo Conde3feb7f12023-10-21 13:14:47 -05001569 # TFUT build
1570 if config_valid "$tfut_config"; then
1571 (
1572 echo "##########"
1573
1574 archive="$build_archive"
1575 tfut_build_root="$tfut_root/build"
1576
1577 echo "Building Trusted Firmware UT ($mode) ..." |& log_separator
1578
Juan Pablo Conde2ecf1862024-01-16 20:12:43 -06001579 # Clean TFUT build targets
1580 set_hook_var "tfut_build_targets" ""
1581
Juan Pablo Conde3feb7f12023-10-21 13:14:47 -05001582 # Call pre-build hook
1583 call_hook pre_tfut_build
1584
1585 build_tfut
1586
1587 from="$tfut_build_root" to="$archive" collect_tfut_artefacts
1588
1589 echo "##########"
1590 echo
1591 )
1592 fi
Fathi Boudra422bf772019-12-02 11:10:16 +02001593 echo
1594 echo
1595done
1596
Juan Pablo Conde3feb7f12023-10-21 13:14:47 -05001597if config_valid "$tfut_config"; then
1598 deactivate
1599fi
1600
Fathi Boudra422bf772019-12-02 11:10:16 +02001601call_hook pre_package
1602
1603call_hook post_package
1604
1605if upon "$jenkins_run" && upon "$artefacts_receiver" && [ -d "artefacts" ]; then
Zelalem219df412020-05-17 19:21:20 -05001606 source "$CI_ROOT/script/send_artefacts.sh" "artefacts"
Fathi Boudra422bf772019-12-02 11:10:16 +02001607fi
1608
1609echo
1610echo "Done"