215 lines · bash
1#!/bin/bash2# SPDX-License-Identifier: GPL-2.03# This validates the user-initiated fw upload mechanism of the firmware4# loader. It verifies that one or more firmware devices can be created5# for a device driver. It also verifies the data transfer, the6# cancellation support, and the error flows.7set -e8 9TEST_REQS_FW_UPLOAD="yes"10TEST_DIR=$(dirname $0)11 12progress_states="preparing transferring programming"13errors="hw-error14 timeout15 device-busy16 invalid-file-size17 read-write-error18 flash-wearout"19error_abort="user-abort"20fwname1=fw121fwname2=fw222fwname3=fw323 24source $TEST_DIR/fw_lib.sh25 26check_mods27check_setup28verify_reqs29 30trap "upload_finish" EXIT31 32upload_finish() {33 local fwdevs="$fwname1 $fwname2 $fwname3"34 35 for name in $fwdevs; do36 if [ -e "$DIR/$name" ]; then37 echo -n "$name" > "$DIR"/upload_unregister38 fi39 done40}41 42upload_fw() {43 local name="$1"44 local file="$2"45 46 echo 1 > "$DIR"/"$name"/loading47 cat "$file" > "$DIR"/"$name"/data48 echo 0 > "$DIR"/"$name"/loading49}50 51verify_fw() {52 local name="$1"53 local file="$2"54 55 echo -n "$name" > "$DIR"/config_upload_name56 if ! cmp "$file" "$DIR"/upload_read > /dev/null 2>&1; then57 echo "$0: firmware compare for $name did not match" >&258 exit 159 fi60 61 echo "$0: firmware upload for $name works" >&262 return 063}64 65inject_error() {66 local name="$1"67 local status="$2"68 local error="$3"69 70 echo 1 > "$DIR"/"$name"/loading71 echo -n "inject":"$status":"$error" > "$DIR"/"$name"/data72 echo 0 > "$DIR"/"$name"/loading73}74 75await_status() {76 local name="$1"77 local expected="$2"78 local status79 local i80 81 let i=082 while [ $i -lt 50 ]; do83 status=$(cat "$DIR"/"$name"/status)84 if [ "$status" = "$expected" ]; then85 return 0;86 fi87 sleep 1e-0388 let i=$i+189 done90 91 echo "$0: Invalid status: Expected $expected, Actual $status" >&292 return 1;93}94 95await_idle() {96 local name="$1"97 98 await_status "$name" "idle"99 return $?100}101 102expect_error() {103 local name="$1"104 local expected="$2"105 local error=$(cat "$DIR"/"$name"/error)106 107 if [ "$error" != "$expected" ]; then108 echo "Invalid error: Expected $expected, Actual $error" >&2109 return 1110 fi111 112 return 0113}114 115random_firmware() {116 local bs="$1"117 local count="$2"118 local file=$(mktemp -p /tmp uploadfwXXX.bin)119 120 dd if=/dev/urandom of="$file" bs="$bs" count="$count" > /dev/null 2>&1121 echo "$file"122}123 124test_upload_cancel() {125 local name="$1"126 local status127 128 for status in $progress_states; do129 inject_error $name $status $error_abort130 if ! await_status $name $status; then131 exit 1132 fi133 134 echo 1 > "$DIR"/"$name"/cancel135 136 if ! await_idle $name; then137 exit 1138 fi139 140 if ! expect_error $name "$status":"$error_abort"; then141 exit 1142 fi143 done144 145 echo "$0: firmware upload cancellation works"146 return 0147}148 149test_error_handling() {150 local name=$1151 local status152 local error153 154 for status in $progress_states; do155 for error in $errors; do156 inject_error $name $status $error157 158 if ! await_idle $name; then159 exit 1160 fi161 162 if ! expect_error $name "$status":"$error"; then163 exit 1164 fi165 166 done167 done168 echo "$0: firmware upload error handling works"169}170 171test_fw_too_big() {172 local name=$1173 local fw_too_big=`random_firmware 512 5`174 local expected="preparing:invalid-file-size"175 176 upload_fw $name $fw_too_big177 rm -f $fw_too_big178 179 if ! await_idle $name; then180 exit 1181 fi182 183 if ! expect_error $name $expected; then184 exit 1185 fi186 187 echo "$0: oversized firmware error handling works"188}189 190echo -n "$fwname1" > "$DIR"/upload_register191echo -n "$fwname2" > "$DIR"/upload_register192echo -n "$fwname3" > "$DIR"/upload_register193 194test_upload_cancel $fwname1195test_error_handling $fwname1196test_fw_too_big $fwname1197 198fw_file1=`random_firmware 512 4`199fw_file2=`random_firmware 512 3`200fw_file3=`random_firmware 512 2`201 202upload_fw $fwname1 $fw_file1203upload_fw $fwname2 $fw_file2204upload_fw $fwname3 $fw_file3205 206verify_fw ${fwname1} ${fw_file1}207verify_fw ${fwname2} ${fw_file2}208verify_fw ${fwname3} ${fw_file3}209 210echo -n "$fwname1" > "$DIR"/upload_unregister211echo -n "$fwname2" > "$DIR"/upload_unregister212echo -n "$fwname3" > "$DIR"/upload_unregister213 214exit 0215