Path: blob/master/tools/testing/selftests/firmware/fw_upload.sh
26285 views
#!/bin/bash1# SPDX-License-Identifier: GPL-2.02# This validates the user-initiated fw upload mechanism of the firmware3# loader. It verifies that one or more firmware devices can be created4# for a device driver. It also verifies the data transfer, the5# cancellation support, and the error flows.6set -e78TEST_REQS_FW_UPLOAD="yes"9TEST_DIR=$(dirname $0)1011progress_states="preparing transferring programming"12errors="hw-error13timeout14device-busy15invalid-file-size16read-write-error17flash-wearout"18error_abort="user-abort"19fwname1=fw120fwname2=fw221fwname3=fw32223source $TEST_DIR/fw_lib.sh2425check_mods26check_setup27verify_reqs2829trap "upload_finish" EXIT3031upload_finish() {32local fwdevs="$fwname1 $fwname2 $fwname3"3334for name in $fwdevs; do35if [ -e "$DIR/$name" ]; then36echo -n "$name" > "$DIR"/upload_unregister37fi38done39}4041upload_fw() {42local name="$1"43local file="$2"4445echo 1 > "$DIR"/"$name"/loading46cat "$file" > "$DIR"/"$name"/data47echo 0 > "$DIR"/"$name"/loading48}4950verify_fw() {51local name="$1"52local file="$2"5354echo -n "$name" > "$DIR"/config_upload_name55if ! cmp "$file" "$DIR"/upload_read > /dev/null 2>&1; then56echo "$0: firmware compare for $name did not match" >&257exit 158fi5960echo "$0: firmware upload for $name works" >&261return 062}6364inject_error() {65local name="$1"66local status="$2"67local error="$3"6869echo 1 > "$DIR"/"$name"/loading70echo -n "inject":"$status":"$error" > "$DIR"/"$name"/data71echo 0 > "$DIR"/"$name"/loading72}7374await_status() {75local name="$1"76local expected="$2"77local status78local i7980let i=081while [ $i -lt 50 ]; do82status=$(cat "$DIR"/"$name"/status)83if [ "$status" = "$expected" ]; then84return 0;85fi86sleep 1e-0387let i=$i+188done8990echo "$0: Invalid status: Expected $expected, Actual $status" >&291return 1;92}9394await_idle() {95local name="$1"9697await_status "$name" "idle"98return $?99}100101expect_error() {102local name="$1"103local expected="$2"104local error=$(cat "$DIR"/"$name"/error)105106if [ "$error" != "$expected" ]; then107echo "Invalid error: Expected $expected, Actual $error" >&2108return 1109fi110111return 0112}113114random_firmware() {115local bs="$1"116local count="$2"117local file=$(mktemp -p /tmp uploadfwXXX.bin)118119dd if=/dev/urandom of="$file" bs="$bs" count="$count" > /dev/null 2>&1120echo "$file"121}122123test_upload_cancel() {124local name="$1"125local status126127for status in $progress_states; do128inject_error $name $status $error_abort129if ! await_status $name $status; then130exit 1131fi132133echo 1 > "$DIR"/"$name"/cancel134135if ! await_idle $name; then136exit 1137fi138139if ! expect_error $name "$status":"$error_abort"; then140exit 1141fi142done143144echo "$0: firmware upload cancellation works"145return 0146}147148test_error_handling() {149local name=$1150local status151local error152153for status in $progress_states; do154for error in $errors; do155inject_error $name $status $error156157if ! await_idle $name; then158exit 1159fi160161if ! expect_error $name "$status":"$error"; then162exit 1163fi164165done166done167echo "$0: firmware upload error handling works"168}169170test_fw_too_big() {171local name=$1172local fw_too_big=`random_firmware 512 5`173local expected="preparing:invalid-file-size"174175upload_fw $name $fw_too_big176rm -f $fw_too_big177178if ! await_idle $name; then179exit 1180fi181182if ! expect_error $name $expected; then183exit 1184fi185186echo "$0: oversized firmware error handling works"187}188189echo -n "$fwname1" > "$DIR"/upload_register190echo -n "$fwname2" > "$DIR"/upload_register191echo -n "$fwname3" > "$DIR"/upload_register192193test_upload_cancel $fwname1194test_error_handling $fwname1195test_fw_too_big $fwname1196197fw_file1=`random_firmware 512 4`198fw_file2=`random_firmware 512 3`199fw_file3=`random_firmware 512 2`200201upload_fw $fwname1 $fw_file1202upload_fw $fwname2 $fw_file2203upload_fw $fwname3 $fw_file3204205verify_fw ${fwname1} ${fw_file1}206verify_fw ${fwname2} ${fw_file2}207verify_fw ${fwname3} ${fw_file3}208209echo -n "$fwname1" > "$DIR"/upload_unregister210echo -n "$fwname2" > "$DIR"/upload_unregister211echo -n "$fwname3" > "$DIR"/upload_unregister212213exit 0214215216