blob: 55a19916edba64d83f6fe1ea055c5ca291e94b71 [file] [log] [blame]
Javier Almansa Sobrino412d3612020-05-22 17:53:12 +01001#!/bin/bash
2#
3# Copyright (c) 2020, Arm Limited. All rights reserved.
4#
5# SPDX-License-Identifier: BSD-3-Clause
6#
7
8set -e
9
10# Enable job control to have background processes run in their own process
11# group. That way, we can kill a background process group in one go.
12set -m
13
14ci_root="$(readlink -f "$(dirname "$0")/..")"
15source "$ci_root/utils.sh"
16
17artefacts="${artefacts-$workspace/artefacts}"
18
19run_root="$workspace/run"
20pid_dir="$workspace/pids"
21
22mkdir -p "$pid_dir"
23mkdir -p "$run_root"
24
25archive="$artefacts"
Javier Almansa Sobrinof98dbd82020-09-30 19:29:27 +010026bootargs_file="bootargs_file"
Javier Almansa Sobrino412d3612020-05-22 17:53:12 +010027
28gen_fpga_params() {
29 local fpga_param_file="fpga_env.sh"
30
31 echo "Generating parameters for FPGA $fpga..."
32 echo
33
34 echo "baudrate=$uart_baudrate" > $fpga_param_file
35 echo "fpga=$fpga" >> $fpga_param_file
36 echo "fpga_bitfile=$fpga_bitfile" >> $fpga_param_file
Javier Almansa Sobrino412d3612020-05-22 17:53:12 +010037 echo "project_name=$project_name" >> $fpga_param_file
38 echo "port=$uart_port" >> $fpga_param_file
39 echo "uart=$uart_descriptor" >> $fpga_param_file
40
Javier Almansa Sobrinof98dbd82020-09-30 19:29:27 +010041 if [ -n "$bl33_img" ]; then
42 echo "bl33_img=$bl33_img" >> $fpga_param_file
43 echo "bl33_addr=$bl33_addr" >> $fpga_param_file
44 fi
45
46 if [ -n "$initrd_img" ]; then
47 echo "initrd_img=$initrd_img" >> $fpga_param_file
48 echo "initrd_addr=$initrd_addr" >> $fpga_param_file
49 fi
50
51 if [ -n "$bootargs" ]; then
52 echo "CMD:$bootargs" > $bootargs_file
53 archive_file "$bootargs_file"
54 echo "cmdline_file=$bootargs_file" >> $fpga_param_file
55 echo "cmdline_addr=$bootargs_addr" >> $fpga_param_file
56 fi
57
Javier Almansa Sobrino412d3612020-05-22 17:53:12 +010058 archive_file "$fpga_param_file"
59}
60
61kill_and_reap() {
62 local gid
63 # Kill an active process. Ignore errors
64 [ "$1" ] || return 0
65 kill -0 "$1" &>/dev/null || return 0
66
67 # Kill the children
68 kill -- "-$1" &>/dev/null || true
69 # Kill the group
70 { gid="$(awk '{print $5}' < /proc/$1/stat)";} 2>/dev/null || return
71 kill -SIGKILL -- "-$gid" &>/dev/null || true
72
73 wait "$gid" &>/dev/null || true
74}
75
76# Perform clean up and ignore errors
77cleanup() {
78 local pid
79
80 # Test success. Kill all background processes so far and wait for them
81 pushd "$pid_dir"
82 set +e
83 while read pid; do
84 pid="$(cat $pid)"
85 kill_and_reap "$pid"
86 done < <(find -name '*.pid')
87 popd
88}
89
90# Launch a program. Have its PID saved in a file with given name with .pid
91# suffix. When the program exits, create a file with .success suffix, or one
92# with .fail if it fails. This function blocks, so the caller must '&' this if
93# they want to continue. Call must wait for $pid_dir/$name.pid to be created
94# should it want to read it.
95launch() {
96 local pid
97
98 "$@" &
99 pid="$!"
100 echo "$pid" > "$pid_dir/${name:?}.pid"
101 if wait "$pid"; then
102 touch "$pid_dir/$name.success"
103 else
104 touch "$pid_dir/$name.fail"
105 fi
106}
107
108# Cleanup actions
109trap cleanup SIGINT SIGHUP SIGTERM EXIT
110
111# Source variables required for run
112source "$artefacts/env"
113
114echo
115echo "RUNNING: $TEST_CONFIG"
116echo
117
118# Accept BIN_MODE from environment, or default to release. If bin_mode is set
119# and non-empty (intended to be set from command line), that takes precedence.
120pkg_bin_mode="${BIN_MODE:-release}"
121bin_mode="${bin_mode:-$pkg_bin_mode}"
122
123artefacts_wd="$artefacts/$bin_mode"
124
125# Change directory so that all binaries can be accessed relative to where they
126# lie
127run_cwd="$artefacts/$bin_mode"
128cd "$run_cwd"
129
130# Source environment for run
131if [ -f "run/env" ]; then
132 source "run/env"
133fi
134
135# Whether to display primary UART progress live on the console
136primary_live="${primary_live-$PRIMARY_LIVE}"
137
Javier Almansa Sobrino412d3612020-05-22 17:53:12 +0100138# Assume 1 UARTs by default
Chris Kay03ffbe72022-11-17 18:53:50 +0000139num_uarts="$(get_num_uarts "${archive}" 1)"
Javier Almansa Sobrino412d3612020-05-22 17:53:12 +0100140
141# Generate the environment configuration file for the FPGA host.
Chris Kay03ffbe72022-11-17 18:53:50 +0000142for u in $(seq 0 $(( $(get_num_uarts "${archive}") - 1 )) | tac); do
Javier Almansa Sobrino412d3612020-05-22 17:53:12 +0100143 descriptor="run/uart$u/descriptor"
144 if [ -f "$descriptor" ]; then
145 uart_descriptor="$(cat "$descriptor")"
146 else
147 echo "Error: No descriptor specified for UART$u"
148 exit 1
149 fi
150
151 baudrate="run/uart$u/baudrate"
152 if [ -f "$baudrate" ]; then
153 uart_baudrate="$(cat "$baudrate")"
154 else
155 echo "Error: No baudrate specified for UART$u"
156 exit 1
157 fi
158
159 port="run/uart$u/port"
160 if [ -f "$port" ]; then
161 uart_port="$(cat "$port")"
162 else
163 echo "Error: No port specified for UART$u"
164 exit 1
165 fi
166
167 fpga="$fpga_cluster" gen_fpga_params
168done
169
170if [ -z "$fpga_user" ]; then
171 echo "FPGA user not configured!"
172 exit 1
173fi
174if [ -z "$fpga_host" ]; then
175 echo "FPGA host not configured!"
176 exit 1
177fi
178remote_user="$fpga_user"
179remote_host="$fpga_host"
180
181echo
182echo "Copying artefacts to $remote_host as user $remote_user"
183echo
184
185# Copy the image to the remote host.
Javier Almansa Sobrinof98dbd82020-09-30 19:29:27 +0100186if [ -n "$bl33_img" ]; then
187 scp "$artefacts_wd/$bl33_img" "$remote_user@$remote_host:." > /dev/null
188fi
189
190if [ -n "$initrd_img" ]; then
191 scp "$artefacts_wd/$initrd_img" "$remote_user@$remote_host:." > /dev/null
192fi
193
194if [ -n "$bootargs" ]; then
195 scp "$artefacts_wd/$bootargs_file" "$remote_user@$remote_host:." > /dev/null
196fi
197scp "$artefacts_wd/bl31.axf" "$remote_user@$remote_host:." > /dev/null
Javier Almansa Sobrino412d3612020-05-22 17:53:12 +0100198
199# Copy the env and run scripts to the remote host.
200scp "$artefacts_wd/fpga_env.sh" "$remote_user@$remote_host:." > /dev/null
201scp "$ci_root/script/$fpga_run_script" "$remote_user@$remote_host:." > /dev/null
202
203echo "FPGA configuration options:"
204echo
Javier Almansa Sobrinof98dbd82020-09-30 19:29:27 +0100205while read conf_option; do
206 echo -e "\t$conf_option"
207done <$artefacts/fpga_env.sh
208if [ -n "$bootargs" ]; then
209echo -e "\tKernel bootargs: $bootargs"
210fi
Javier Almansa Sobrino412d3612020-05-22 17:53:12 +0100211
212# For an automated run, export a known variable so that we can identify stale
213# processes spawned by Trusted Firmware CI by inspecting its environment.
214export TRUSTED_FIRMWARE_CI="1"
215
216echo
217echo "Executing on $remote_host as user $remote_user"
218echo
219
220# Run the FPGA from the remote host.
221name="fpga_run" launch ssh "$remote_user@$remote_host" "bash ./$fpga_run_script" > \
222 /dev/null 2>&1 &
223
224# Wait enough time for the UART to show up on the FPGA host so the connection
225# can be stablished.
Javier Almansa Sobrinof98dbd82020-09-30 19:29:27 +0100226sleep 65
Javier Almansa Sobrino412d3612020-05-22 17:53:12 +0100227
228# If it's a test run, skip all the hoops and start a telnet connection to the FPGA.
229if upon "$test_run"; then
Chris Kay03ffbe72022-11-17 18:53:50 +0000230 telnet "$remote_host" "$(cat "run/uart$(get_primary_uart "${archive}")/port")"
Javier Almansa Sobrino412d3612020-05-22 17:53:12 +0100231 exit 0
232fi
233
234# Launch expect scripts for all UARTs
Chris Kay03ffbe72022-11-17 18:53:50 +0000235for u in $(seq 0 $(( $(get_num_uarts "${archive}") - 1 )) | tac); do
Javier Almansa Sobrino412d3612020-05-22 17:53:12 +0100236 script="run/uart$u/expect"
237 if [ -f "$script" ]; then
238 script="$(cat "$script")"
239 else
240 script=
241 fi
242
243 # Primary UART must have a script
244 if [ -z "$script" ]; then
Chris Kay03ffbe72022-11-17 18:53:50 +0000245 if [ "$u" = "$(get_primary_uart "${archive}")" ]; then
Javier Almansa Sobrino412d3612020-05-22 17:53:12 +0100246 die "No primary UART script!"
247 else
248 echo "Ignoring UART$u (no expect script provided)."
249 continue
250 fi
251 fi
252
253 uart_descriptor="$(cat "run/uart$u/descriptor")"
254
255 timeout="run/uart$u/timeout"
256 uart_port="$(cat "run/uart$u/port")"
257
258 if [ -f "$timeout" ]; then
259 timeout="$(cat "$timeout")"
260 else
261 timeout=
262 fi
263 timeout="${timeout-600}"
264
265 full_log="$run_root/uart${u}_full.txt"
266
Chris Kay03ffbe72022-11-17 18:53:50 +0000267 if [ "$u" = "$(get_primary_uart "${archive}")" ]; then
Javier Almansa Sobrino412d3612020-05-22 17:53:12 +0100268 star="*"
269 uart_name="primary_uart"
270 else
271 star=" "
272 uart_name="uart$u"
273 fi
274
275 # Launch expect after exporting required variables
276 (
277 if [ -f "run/uart$u/env" ]; then
278 set -a
279 source "run/uart$u/env"
280 set +a
281 fi
282
Chris Kay03ffbe72022-11-17 18:53:50 +0000283 if [ "$u" = "$(get_primary_uart "${archive}")" ] && upon "$primary_live"; then
Javier Almansa Sobrino412d3612020-05-22 17:53:12 +0100284 uart_port="$uart_port" remote_host="$remote_host" timeout="$timeout" \
285 name="$uart_name" launch expect -f "$ci_root/expect/$script" | \
286 tee "$full_log"
287 echo
288 else
289 uart_port="$uart_port" remote_host="$remote_host" timeout="$timeout" \
290 name="$uart_name" launch expect -f "$ci_root/expect/$script" \
291 &>"$full_log"
292 fi
293
294 ) &
295
296 echo "Tracking UART$u$star ($uart_descriptor) with $script and timeout $timeout."
297done
298echo
299
300result=0
301
302set +e
303
304# Wait for all the children. Note that the wait below is *not* a timed wait.
305wait -n
306
307pushd "$pid_dir"
308# Wait for fpga_run to finish on the remote server.
309while :; do
310 if [ "$(wc -l < <(ls -l fpga_run.* 2> /dev/null))" -eq 2 ]; then
311 break
312 else
313 sleep 1
314 fi
315done
316
317# Check if there is any failure.
318while :; do
319 # Exit failure if we've any failures
320 if [ "$(wc -l < <(find -name '*.fail'))" -ne 0 ]; then
321 result=1
322 break
323 fi
324
325 # We're done if the primary UART exits success
326 if [ -f "$pid_dir/primary_uart.success" ]; then
327 break
328 fi
329done
330
Javier Almansa Sobrinof98dbd82020-09-30 19:29:27 +0100331ssh "$remote_user@$remote_host" "rm ./$fpga_run_script"
332
Javier Almansa Sobrino412d3612020-05-22 17:53:12 +0100333cleanup
334
335if [ "$result" -eq 0 ]; then
336 echo "Test success!"
337else
338 echo "Test failed!"
339fi
340
341if upon "$jenkins_run"; then
342 echo
343 echo "Artefacts location: $BUILD_URL."
344 echo
345fi
346
347if upon "$jenkins_run" && upon "$artefacts_receiver" && [ -d "$workspace/run" ]; then
348 source "$CI_ROOT/script/send_artefacts.sh" "run"
349fi
350
351exit "$result"
352# vim: set tw=80 sw=8 noet: