Javier Almansa Sobrino | 412d361 | 2020-05-22 17:53:12 +0100 | [diff] [blame] | 1 | #!/bin/bash |
| 2 | # |
| 3 | # Copyright (c) 2020, Arm Limited. All rights reserved. |
| 4 | # |
| 5 | # SPDX-License-Identifier: BSD-3-Clause |
| 6 | # |
| 7 | |
| 8 | set -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. |
| 12 | set -m |
| 13 | |
| 14 | ci_root="$(readlink -f "$(dirname "$0")/..")" |
| 15 | source "$ci_root/utils.sh" |
| 16 | |
| 17 | artefacts="${artefacts-$workspace/artefacts}" |
| 18 | |
| 19 | run_root="$workspace/run" |
| 20 | pid_dir="$workspace/pids" |
| 21 | |
| 22 | mkdir -p "$pid_dir" |
| 23 | mkdir -p "$run_root" |
| 24 | |
| 25 | archive="$artefacts" |
Javier Almansa Sobrino | f98dbd8 | 2020-09-30 19:29:27 +0100 | [diff] [blame] | 26 | bootargs_file="bootargs_file" |
Javier Almansa Sobrino | 412d361 | 2020-05-22 17:53:12 +0100 | [diff] [blame] | 27 | |
| 28 | gen_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 Sobrino | 412d361 | 2020-05-22 17:53:12 +0100 | [diff] [blame] | 37 | 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 Sobrino | f98dbd8 | 2020-09-30 19:29:27 +0100 | [diff] [blame] | 41 | 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 Sobrino | 412d361 | 2020-05-22 17:53:12 +0100 | [diff] [blame] | 58 | archive_file "$fpga_param_file" |
| 59 | } |
| 60 | |
| 61 | kill_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 |
| 77 | cleanup() { |
| 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. |
| 95 | launch() { |
| 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 |
| 109 | trap cleanup SIGINT SIGHUP SIGTERM EXIT |
| 110 | |
| 111 | # Source variables required for run |
| 112 | source "$artefacts/env" |
| 113 | |
| 114 | echo |
| 115 | echo "RUNNING: $TEST_CONFIG" |
| 116 | echo |
| 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. |
| 120 | pkg_bin_mode="${BIN_MODE:-release}" |
| 121 | bin_mode="${bin_mode:-$pkg_bin_mode}" |
| 122 | |
| 123 | artefacts_wd="$artefacts/$bin_mode" |
| 124 | |
| 125 | # Change directory so that all binaries can be accessed relative to where they |
| 126 | # lie |
| 127 | run_cwd="$artefacts/$bin_mode" |
| 128 | cd "$run_cwd" |
| 129 | |
| 130 | # Source environment for run |
| 131 | if [ -f "run/env" ]; then |
| 132 | source "run/env" |
| 133 | fi |
| 134 | |
| 135 | # Whether to display primary UART progress live on the console |
| 136 | primary_live="${primary_live-$PRIMARY_LIVE}" |
| 137 | |
Javier Almansa Sobrino | 412d361 | 2020-05-22 17:53:12 +0100 | [diff] [blame] | 138 | # Assume 1 UARTs by default |
Chris Kay | 03ffbe7 | 2022-11-17 18:53:50 +0000 | [diff] [blame] | 139 | num_uarts="$(get_num_uarts "${archive}" 1)" |
Javier Almansa Sobrino | 412d361 | 2020-05-22 17:53:12 +0100 | [diff] [blame] | 140 | |
| 141 | # Generate the environment configuration file for the FPGA host. |
Chris Kay | 03ffbe7 | 2022-11-17 18:53:50 +0000 | [diff] [blame] | 142 | for u in $(seq 0 $(( $(get_num_uarts "${archive}") - 1 )) | tac); do |
Javier Almansa Sobrino | 412d361 | 2020-05-22 17:53:12 +0100 | [diff] [blame] | 143 | 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 |
| 168 | done |
| 169 | |
| 170 | if [ -z "$fpga_user" ]; then |
| 171 | echo "FPGA user not configured!" |
| 172 | exit 1 |
| 173 | fi |
| 174 | if [ -z "$fpga_host" ]; then |
| 175 | echo "FPGA host not configured!" |
| 176 | exit 1 |
| 177 | fi |
| 178 | remote_user="$fpga_user" |
| 179 | remote_host="$fpga_host" |
| 180 | |
| 181 | echo |
| 182 | echo "Copying artefacts to $remote_host as user $remote_user" |
| 183 | echo |
| 184 | |
| 185 | # Copy the image to the remote host. |
Javier Almansa Sobrino | f98dbd8 | 2020-09-30 19:29:27 +0100 | [diff] [blame] | 186 | if [ -n "$bl33_img" ]; then |
| 187 | scp "$artefacts_wd/$bl33_img" "$remote_user@$remote_host:." > /dev/null |
| 188 | fi |
| 189 | |
| 190 | if [ -n "$initrd_img" ]; then |
| 191 | scp "$artefacts_wd/$initrd_img" "$remote_user@$remote_host:." > /dev/null |
| 192 | fi |
| 193 | |
| 194 | if [ -n "$bootargs" ]; then |
| 195 | scp "$artefacts_wd/$bootargs_file" "$remote_user@$remote_host:." > /dev/null |
| 196 | fi |
| 197 | scp "$artefacts_wd/bl31.axf" "$remote_user@$remote_host:." > /dev/null |
Javier Almansa Sobrino | 412d361 | 2020-05-22 17:53:12 +0100 | [diff] [blame] | 198 | |
| 199 | # Copy the env and run scripts to the remote host. |
| 200 | scp "$artefacts_wd/fpga_env.sh" "$remote_user@$remote_host:." > /dev/null |
| 201 | scp "$ci_root/script/$fpga_run_script" "$remote_user@$remote_host:." > /dev/null |
| 202 | |
| 203 | echo "FPGA configuration options:" |
| 204 | echo |
Javier Almansa Sobrino | f98dbd8 | 2020-09-30 19:29:27 +0100 | [diff] [blame] | 205 | while read conf_option; do |
| 206 | echo -e "\t$conf_option" |
| 207 | done <$artefacts/fpga_env.sh |
| 208 | if [ -n "$bootargs" ]; then |
| 209 | echo -e "\tKernel bootargs: $bootargs" |
| 210 | fi |
Javier Almansa Sobrino | 412d361 | 2020-05-22 17:53:12 +0100 | [diff] [blame] | 211 | |
| 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. |
| 214 | export TRUSTED_FIRMWARE_CI="1" |
| 215 | |
| 216 | echo |
| 217 | echo "Executing on $remote_host as user $remote_user" |
| 218 | echo |
| 219 | |
| 220 | # Run the FPGA from the remote host. |
| 221 | name="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 Sobrino | f98dbd8 | 2020-09-30 19:29:27 +0100 | [diff] [blame] | 226 | sleep 65 |
Javier Almansa Sobrino | 412d361 | 2020-05-22 17:53:12 +0100 | [diff] [blame] | 227 | |
| 228 | # If it's a test run, skip all the hoops and start a telnet connection to the FPGA. |
| 229 | if upon "$test_run"; then |
Chris Kay | 03ffbe7 | 2022-11-17 18:53:50 +0000 | [diff] [blame] | 230 | telnet "$remote_host" "$(cat "run/uart$(get_primary_uart "${archive}")/port")" |
Javier Almansa Sobrino | 412d361 | 2020-05-22 17:53:12 +0100 | [diff] [blame] | 231 | exit 0 |
| 232 | fi |
| 233 | |
| 234 | # Launch expect scripts for all UARTs |
Chris Kay | 03ffbe7 | 2022-11-17 18:53:50 +0000 | [diff] [blame] | 235 | for u in $(seq 0 $(( $(get_num_uarts "${archive}") - 1 )) | tac); do |
Javier Almansa Sobrino | 412d361 | 2020-05-22 17:53:12 +0100 | [diff] [blame] | 236 | 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 Kay | 03ffbe7 | 2022-11-17 18:53:50 +0000 | [diff] [blame] | 245 | if [ "$u" = "$(get_primary_uart "${archive}")" ]; then |
Javier Almansa Sobrino | 412d361 | 2020-05-22 17:53:12 +0100 | [diff] [blame] | 246 | 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 Kay | 03ffbe7 | 2022-11-17 18:53:50 +0000 | [diff] [blame] | 267 | if [ "$u" = "$(get_primary_uart "${archive}")" ]; then |
Javier Almansa Sobrino | 412d361 | 2020-05-22 17:53:12 +0100 | [diff] [blame] | 268 | 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 Kay | 03ffbe7 | 2022-11-17 18:53:50 +0000 | [diff] [blame] | 283 | if [ "$u" = "$(get_primary_uart "${archive}")" ] && upon "$primary_live"; then |
Javier Almansa Sobrino | 412d361 | 2020-05-22 17:53:12 +0100 | [diff] [blame] | 284 | 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." |
| 297 | done |
| 298 | echo |
| 299 | |
| 300 | result=0 |
| 301 | |
| 302 | set +e |
| 303 | |
| 304 | # Wait for all the children. Note that the wait below is *not* a timed wait. |
| 305 | wait -n |
| 306 | |
| 307 | pushd "$pid_dir" |
| 308 | # Wait for fpga_run to finish on the remote server. |
| 309 | while :; do |
| 310 | if [ "$(wc -l < <(ls -l fpga_run.* 2> /dev/null))" -eq 2 ]; then |
| 311 | break |
| 312 | else |
| 313 | sleep 1 |
| 314 | fi |
| 315 | done |
| 316 | |
| 317 | # Check if there is any failure. |
| 318 | while :; 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 |
| 329 | done |
| 330 | |
Javier Almansa Sobrino | f98dbd8 | 2020-09-30 19:29:27 +0100 | [diff] [blame] | 331 | ssh "$remote_user@$remote_host" "rm ./$fpga_run_script" |
| 332 | |
Javier Almansa Sobrino | 412d361 | 2020-05-22 17:53:12 +0100 | [diff] [blame] | 333 | cleanup |
| 334 | |
| 335 | if [ "$result" -eq 0 ]; then |
| 336 | echo "Test success!" |
| 337 | else |
| 338 | echo "Test failed!" |
| 339 | fi |
| 340 | |
| 341 | if upon "$jenkins_run"; then |
| 342 | echo |
| 343 | echo "Artefacts location: $BUILD_URL." |
| 344 | echo |
| 345 | fi |
| 346 | |
| 347 | if upon "$jenkins_run" && upon "$artefacts_receiver" && [ -d "$workspace/run" ]; then |
| 348 | source "$CI_ROOT/script/send_artefacts.sh" "run" |
| 349 | fi |
| 350 | |
| 351 | exit "$result" |
| 352 | # vim: set tw=80 sw=8 noet: |