547 lines · bash
1#!/bin/bash2# SPDX-License-Identifier: GPL-2.03# This validates that the kernel will load firmware out of its list of4# firmware locations on disk. Since the user helper does similar work,5# we reset the custom load directory to a location the user helper doesn't6# know so we can be sure we're not accidentally testing the user helper.7set -e8 9TEST_REQS_FW_SYSFS_FALLBACK="no"10TEST_REQS_FW_SET_CUSTOM_PATH="yes"11TEST_DIR=$(dirname $0)12source $TEST_DIR/fw_lib.sh13 14RUN_XZ="xz -C crc32 --lzma2=dict=2MiB"15RUN_ZSTD="zstd -q"16 17check_mods18check_setup19verify_reqs20setup_tmp_file21 22trap "test_finish" EXIT23 24if [ "$HAS_FW_LOADER_USER_HELPER" = "yes" ]; then25 # Turn down the timeout so failures don't take so long.26 echo 1 >/sys/class/firmware/timeout27fi28 29if printf '\000' >"$DIR"/trigger_request 2> /dev/null; then30 echo "$0: empty filename should not succeed" >&231 exit 132fi33 34if [ ! -e "$DIR"/trigger_async_request ]; then35 echo "$0: empty filename: async trigger not present, ignoring test" >&236 exit $ksft_skip37else38 if printf '\000' >"$DIR"/trigger_async_request 2> /dev/null; then39 echo "$0: empty filename should not succeed (async)" >&240 exit 141 fi42fi43 44# Request a firmware that doesn't exist, it should fail.45if echo -n "nope-$NAME" >"$DIR"/trigger_request 2> /dev/null; then46 echo "$0: firmware shouldn't have loaded" >&247 exit 148fi49if diff -q "$FW" /dev/test_firmware >/dev/null ; then50 echo "$0: firmware was not expected to match" >&251 exit 152else53 if [ "$HAS_FW_LOADER_USER_HELPER" = "yes" ]; then54 echo "$0: timeout works"55 fi56fi57 58# This should succeed via kernel load or will fail after 1 second after59# being handed over to the user helper, which won't find the fw either.60if ! echo -n "$NAME" >"$DIR"/trigger_request ; then61 echo "$0: could not trigger request" >&262 exit 163fi64 65# Verify the contents are what we expect.66if ! diff -q "$FW" /dev/test_firmware >/dev/null ; then67 echo "$0: firmware was not loaded" >&268 exit 169else70 echo "$0: filesystem loading works"71fi72 73# Try the asynchronous version too74if [ ! -e "$DIR"/trigger_async_request ]; then75 echo "$0: firmware loading: async trigger not present, ignoring test" >&276 exit $ksft_skip77else78 if ! echo -n "$NAME" >"$DIR"/trigger_async_request ; then79 echo "$0: could not trigger async request" >&280 exit 181 fi82 83 # Verify the contents are what we expect.84 if ! diff -q "$FW" /dev/test_firmware >/dev/null ; then85 echo "$0: firmware was not loaded (async)" >&286 exit 187 else88 echo "$0: async filesystem loading works"89 fi90fi91 92# Try platform (EFI embedded fw) loading too93if [ ! -e "$DIR"/trigger_request_platform ]; then94 echo "$0: firmware loading: platform trigger not present, ignoring test" >&295else96 if printf '\000' >"$DIR"/trigger_request_platform 2> /dev/null; then97 echo "$0: empty filename should not succeed (platform)" >&298 exit 199 fi100 101 # Note we echo a non-existing name, since files on the file-system102 # are preferred over firmware embedded inside the platform's firmware103 # The test adds a fake entry with the requested name to the platform's104 # fw list, so the name does not matter as long as it does not exist105 if ! echo -n "nope-$NAME" >"$DIR"/trigger_request_platform ; then106 echo "$0: could not trigger request platform" >&2107 exit 1108 fi109 110 # The test verifies itself that the loaded firmware contents matches111 # the contents for the fake platform fw entry it added.112 echo "$0: platform loading works"113fi114 115### Batched requests tests116test_config_present()117{118 if [ ! -f $DIR/reset ]; then119 echo "Configuration triggers not present, ignoring test"120 exit $ksft_skip121 fi122}123 124# Defaults :125#126# send_uevent: 1127# sync_direct: 0128# name: test-firmware.bin129# num_requests: 4130config_reset()131{132 echo 1 > $DIR/reset133}134 135release_all_firmware()136{137 echo 1 > $DIR/release_all_firmware138}139 140config_set_name()141{142 echo -n $1 > $DIR/config_name143}144 145config_set_into_buf()146{147 echo 1 > $DIR/config_into_buf148}149 150config_unset_into_buf()151{152 echo 0 > $DIR/config_into_buf153}154 155config_set_buf_size()156{157 echo $1 > $DIR/config_buf_size158}159 160config_set_file_offset()161{162 echo $1 > $DIR/config_file_offset163}164 165config_set_partial()166{167 echo 1 > $DIR/config_partial168}169 170config_unset_partial()171{172 echo 0 > $DIR/config_partial173}174 175config_set_sync_direct()176{177 echo 1 > $DIR/config_sync_direct178}179 180config_unset_sync_direct()181{182 echo 0 > $DIR/config_sync_direct183}184 185config_set_uevent()186{187 echo 1 > $DIR/config_send_uevent188}189 190config_unset_uevent()191{192 echo 0 > $DIR/config_send_uevent193}194 195config_trigger_sync()196{197 echo -n 1 > $DIR/trigger_batched_requests 2>/dev/null198}199 200config_trigger_async()201{202 echo -n 1 > $DIR/trigger_batched_requests_async 2> /dev/null203}204 205config_set_read_fw_idx()206{207 echo -n $1 > $DIR/config_read_fw_idx 2> /dev/null208}209 210read_firmwares()211{212 if [ "$(cat $DIR/config_into_buf)" == "1" ]; then213 fwfile="$FW_INTO_BUF"214 else215 fwfile="$FW"216 fi217 if [ "$1" = "componly" ]; then218 fwfile="${fwfile}-orig"219 fi220 for i in $(seq 0 3); do221 config_set_read_fw_idx $i222 # Verify the contents are what we expect.223 # -Z required for now -- check for yourself, md5sum224 # on $FW and DIR/read_firmware will yield the same. Even225 # cmp agrees, so something is off.226 if ! diff -q -Z "$fwfile" $DIR/read_firmware 2>/dev/null ; then227 echo "request #$i: firmware was not loaded" >&2228 exit 1229 fi230 done231}232 233read_partial_firmwares()234{235 if [ "$(cat $DIR/config_into_buf)" == "1" ]; then236 fwfile="${FW_INTO_BUF}"237 else238 fwfile="${FW}"239 fi240 241 if [ "$1" = "componly" ]; then242 fwfile="${fwfile}-orig"243 fi244 245 # Strip fwfile down to match partial offset and length246 partial_data="$(cat $fwfile)"247 partial_data="${partial_data:$2:$3}"248 249 for i in $(seq 0 3); do250 config_set_read_fw_idx $i251 252 read_firmware="$(cat $DIR/read_firmware)"253 254 # Verify the contents are what we expect.255 if [ $read_firmware != $partial_data ]; then256 echo "request #$i: partial firmware was not loaded" >&2257 exit 1258 fi259 done260}261 262read_firmwares_expect_nofile()263{264 for i in $(seq 0 3); do265 config_set_read_fw_idx $i266 # Ensures contents differ267 if diff -q -Z "$FW" $DIR/read_firmware 2>/dev/null ; then268 echo "request $i: file was not expected to match" >&2269 exit 1270 fi271 done272}273 274test_batched_request_firmware_nofile()275{276 echo -n "Batched request_firmware() nofile try #$1: "277 config_reset278 config_set_name nope-test-firmware.bin279 config_trigger_sync280 read_firmwares_expect_nofile281 release_all_firmware282 echo "OK"283}284 285test_batched_request_firmware_into_buf_nofile()286{287 echo -n "Batched request_firmware_into_buf() nofile try #$1: "288 config_reset289 config_set_name nope-test-firmware.bin290 config_set_into_buf291 config_trigger_sync292 read_firmwares_expect_nofile293 release_all_firmware294 echo "OK"295}296 297test_request_partial_firmware_into_buf_nofile()298{299 echo -n "Test request_partial_firmware_into_buf() off=$1 size=$2 nofile: "300 config_reset301 config_set_name nope-test-firmware.bin302 config_set_into_buf303 config_set_partial304 config_set_buf_size $2305 config_set_file_offset $1306 config_trigger_sync307 read_firmwares_expect_nofile308 release_all_firmware309 echo "OK"310}311 312test_batched_request_firmware_direct_nofile()313{314 echo -n "Batched request_firmware_direct() nofile try #$1: "315 config_reset316 config_set_name nope-test-firmware.bin317 config_set_sync_direct318 config_trigger_sync319 release_all_firmware320 echo "OK"321}322 323test_request_firmware_nowait_uevent_nofile()324{325 echo -n "Batched request_firmware_nowait(uevent=true) nofile try #$1: "326 config_reset327 config_set_name nope-test-firmware.bin328 config_trigger_async329 release_all_firmware330 echo "OK"331}332 333test_wait_and_cancel_custom_load()334{335 if [ "$HAS_FW_LOADER_USER_HELPER" != "yes" ]; then336 return337 fi338 local timeout=10339 name=$1340 while [ ! -e "$DIR"/"$name"/loading ]; do341 sleep 0.1342 timeout=$(( $timeout - 1 ))343 if [ "$timeout" -eq 0 ]; then344 echo "firmware interface never appeared:" >&2345 echo "$DIR/$name/loading" >&2346 exit 1347 fi348 done349 echo -1 >"$DIR"/"$name"/loading350}351 352test_request_firmware_nowait_custom_nofile()353{354 echo -n "Batched request_firmware_nowait(uevent=false) nofile try #$1: "355 config_reset356 config_unset_uevent357 RANDOM_FILE_PATH=$(setup_random_file_fake)358 RANDOM_FILE="$(basename $RANDOM_FILE_PATH)"359 config_set_name $RANDOM_FILE360 config_trigger_async &361 test_wait_and_cancel_custom_load $RANDOM_FILE362 wait363 release_all_firmware364 echo "OK"365}366 367test_batched_request_firmware()368{369 echo -n "Batched request_firmware() $2 try #$1: "370 config_reset371 config_trigger_sync372 read_firmwares $2373 release_all_firmware374 echo "OK"375}376 377test_batched_request_firmware_into_buf()378{379 echo -n "Batched request_firmware_into_buf() $2 try #$1: "380 config_reset381 config_set_name $TEST_FIRMWARE_INTO_BUF_FILENAME382 config_set_into_buf383 config_trigger_sync384 read_firmwares $2385 release_all_firmware386 echo "OK"387}388 389test_batched_request_firmware_direct()390{391 echo -n "Batched request_firmware_direct() $2 try #$1: "392 config_reset393 config_set_sync_direct394 config_trigger_sync395 release_all_firmware396 echo "OK"397}398 399test_request_firmware_nowait_uevent()400{401 echo -n "Batched request_firmware_nowait(uevent=true) $2 try #$1: "402 config_reset403 config_trigger_async404 release_all_firmware405 echo "OK"406}407 408test_request_firmware_nowait_custom()409{410 echo -n "Batched request_firmware_nowait(uevent=false) $2 try #$1: "411 config_reset412 config_unset_uevent413 RANDOM_FILE_PATH=$(setup_random_file)414 RANDOM_FILE="$(basename $RANDOM_FILE_PATH)"415 if [ -n "$2" -a "$2" != "normal" ]; then416 compress_"$2"_"$COMPRESS_FORMAT" $RANDOM_FILE_PATH417 fi418 config_set_name $RANDOM_FILE419 config_trigger_async420 release_all_firmware421 echo "OK"422}423 424test_request_partial_firmware_into_buf()425{426 echo -n "Test request_partial_firmware_into_buf() off=$1 size=$2: "427 config_reset428 config_set_name $TEST_FIRMWARE_INTO_BUF_FILENAME429 config_set_into_buf430 config_set_partial431 config_set_buf_size $2432 config_set_file_offset $1433 config_trigger_sync434 read_partial_firmwares normal $1 $2435 release_all_firmware436 echo "OK"437}438 439do_tests ()440{441 mode="$1"442 suffix="$2"443 444 for i in $(seq 1 5); do445 test_batched_request_firmware$suffix $i $mode446 done447 448 for i in $(seq 1 5); do449 test_batched_request_firmware_into_buf$suffix $i $mode450 done451 452 for i in $(seq 1 5); do453 test_batched_request_firmware_direct$suffix $i $mode454 done455 456 for i in $(seq 1 5); do457 test_request_firmware_nowait_uevent$suffix $i $mode458 done459 460 for i in $(seq 1 5); do461 test_request_firmware_nowait_custom$suffix $i $mode462 done463}464 465# Only continue if batched request triggers are present on the466# test-firmware driver467test_config_present468 469# test with the file present470echo471echo "Testing with the file present..."472do_tests normal473 474# Partial loads cannot use fallback, so do not repeat tests.475test_request_partial_firmware_into_buf 0 10476test_request_partial_firmware_into_buf 0 5477test_request_partial_firmware_into_buf 1 6478test_request_partial_firmware_into_buf 2 10479 480# Test for file not found, errors are expected, the failure would be481# a hung task, which would require a hard reset.482echo483echo "Testing with the file missing..."484do_tests nofile _nofile485 486# Partial loads cannot use fallback, so do not repeat tests.487test_request_partial_firmware_into_buf_nofile 0 10488test_request_partial_firmware_into_buf_nofile 0 5489test_request_partial_firmware_into_buf_nofile 1 6490test_request_partial_firmware_into_buf_nofile 2 10491 492test_request_firmware_compressed ()493{494 export COMPRESS_FORMAT="$1"495 496 # test with both files present497 compress_both_"$COMPRESS_FORMAT" $FW498 compress_both_"$COMPRESS_FORMAT" $FW_INTO_BUF499 500 config_set_name $NAME501 echo502 echo "Testing with both plain and $COMPRESS_FORMAT files present..."503 do_tests both504 505 # test with only compressed file present506 mv "$FW" "${FW}-orig"507 mv "$FW_INTO_BUF" "${FW_INTO_BUF}-orig"508 509 config_set_name $NAME510 echo511 echo "Testing with only $COMPRESS_FORMAT file present..."512 do_tests componly513 514 mv "${FW}-orig" "$FW"515 mv "${FW_INTO_BUF}-orig" "$FW_INTO_BUF"516}517 518compress_both_XZ ()519{520 $RUN_XZ -k "$@"521}522 523compress_componly_XZ ()524{525 $RUN_XZ "$@"526}527 528compress_both_ZSTD ()529{530 $RUN_ZSTD -k "$@"531}532 533compress_componly_ZSTD ()534{535 $RUN_ZSTD --rm "$@"536}537 538if test "$HAS_FW_LOADER_COMPRESS_XZ" = "yes"; then539 test_request_firmware_compressed XZ540fi541 542if test "$HAS_FW_LOADER_COMPRESS_ZSTD" = "yes"; then543 test_request_firmware_compressed ZSTD544fi545 546exit 0547