2 # SPDX-License-Identifier: GPL-2.0
3 # This validates that the kernel will load firmware out of its list of
4 # 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't
6 # know so we can be sure we're not accidentally testing the user helper.
9 TEST_REQS_FW_SYSFS_FALLBACK="no"
10 TEST_REQS_FW_SET_CUSTOM_PATH="yes"
11 TEST_DIR=$(dirname $0)
12 source $TEST_DIR/fw_lib.sh
19 trap "test_finish" EXIT
21 if [ "$HAS_FW_LOADER_USER_HELPER" = "yes" ]; then
22 # Turn down the timeout so failures don't take so long.
23 echo 1 >/sys/class/firmware/timeout
26 if printf '\000' >"$DIR"/trigger_request 2> /dev/null; then
27 echo "$0: empty filename should not succeed" >&2
31 if [ ! -e "$DIR"/trigger_async_request ]; then
32 echo "$0: empty filename: async trigger not present, ignoring test" >&2
35 if printf '\000' >"$DIR"/trigger_async_request 2> /dev/null; then
36 echo "$0: empty filename should not succeed (async)" >&2
41 # Request a firmware that doesn't exist, it should fail.
42 if echo -n "nope-$NAME" >"$DIR"/trigger_request 2> /dev/null; then
43 echo "$0: firmware shouldn't have loaded" >&2
46 if diff -q "$FW" /dev/test_firmware >/dev/null ; then
47 echo "$0: firmware was not expected to match" >&2
50 if [ "$HAS_FW_LOADER_USER_HELPER" = "yes" ]; then
51 echo "$0: timeout works"
55 # This should succeed via kernel load or will fail after 1 second after
56 # being handed over to the user helper, which won't find the fw either.
57 if ! echo -n "$NAME" >"$DIR"/trigger_request ; then
58 echo "$0: could not trigger request" >&2
62 # Verify the contents are what we expect.
63 if ! diff -q "$FW" /dev/test_firmware >/dev/null ; then
64 echo "$0: firmware was not loaded" >&2
67 echo "$0: filesystem loading works"
70 # Try the asynchronous version too
71 if [ ! -e "$DIR"/trigger_async_request ]; then
72 echo "$0: firmware loading: async trigger not present, ignoring test" >&2
75 if ! echo -n "$NAME" >"$DIR"/trigger_async_request ; then
76 echo "$0: could not trigger async request" >&2
80 # Verify the contents are what we expect.
81 if ! diff -q "$FW" /dev/test_firmware >/dev/null ; then
82 echo "$0: firmware was not loaded (async)" >&2
85 echo "$0: async filesystem loading works"
89 # Try platform (EFI embedded fw) loading too
90 if [ ! -e "$DIR"/trigger_request_platform ]; then
91 echo "$0: firmware loading: platform trigger not present, ignoring test" >&2
93 if printf '\000' >"$DIR"/trigger_request_platform 2> /dev/null; then
94 echo "$0: empty filename should not succeed (platform)" >&2
98 # Note we echo a non-existing name, since files on the file-system
99 # are preferred over firmware embedded inside the platform's firmware
100 # The test adds a fake entry with the requested name to the platform's
101 # fw list, so the name does not matter as long as it does not exist
102 if ! echo -n "nope-$NAME" >"$DIR"/trigger_request_platform ; then
103 echo "$0: could not trigger request platform" >&2
107 # The test verifies itself that the loaded firmware contents matches
108 # the contents for the fake platform fw entry it added.
109 echo "$0: platform loading works"
112 ### Batched requests tests
113 test_config_present()
115 if [ ! -f $DIR/reset ]; then
116 echo "Configuration triggers not present, ignoring test"
125 # name: test-firmware.bin
132 release_all_firmware()
134 echo 1 > $DIR/release_all_firmware
139 echo -n $1 > $DIR/config_name
142 config_set_into_buf()
144 echo 1 > $DIR/config_into_buf
147 config_unset_into_buf()
149 echo 0 > $DIR/config_into_buf
152 config_set_buf_size()
154 echo $1 > $DIR/config_buf_size
157 config_set_file_offset()
159 echo $1 > $DIR/config_file_offset
164 echo 1 > $DIR/config_partial
167 config_unset_partial()
169 echo 0 > $DIR/config_partial
172 config_set_sync_direct()
174 echo 1 > $DIR/config_sync_direct
177 config_unset_sync_direct()
179 echo 0 > $DIR/config_sync_direct
184 echo 1 > $DIR/config_send_uevent
187 config_unset_uevent()
189 echo 0 > $DIR/config_send_uevent
192 config_trigger_sync()
194 echo -n 1 > $DIR/trigger_batched_requests 2>/dev/null
197 config_trigger_async()
199 echo -n 1 > $DIR/trigger_batched_requests_async 2> /dev/null
202 config_set_read_fw_idx()
204 echo -n $1 > $DIR/config_read_fw_idx 2> /dev/null
209 if [ "$(cat $DIR/config_into_buf)" == "1" ]; then
210 fwfile="$FW_INTO_BUF"
214 if [ "$1" = "xzonly" ]; then
215 fwfile="${fwfile}-orig"
217 for i in $(seq 0 3); do
218 config_set_read_fw_idx $i
219 # Verify the contents are what we expect.
220 # -Z required for now -- check for yourself, md5sum
221 # on $FW and DIR/read_firmware will yield the same. Even
222 # cmp agrees, so something is off.
223 if ! diff -q -Z "$fwfile" $DIR/read_firmware 2>/dev/null ; then
224 echo "request #$i: firmware was not loaded" >&2
230 read_partial_firmwares()
232 if [ "$(cat $DIR/config_into_buf)" == "1" ]; then
233 fwfile="${FW_INTO_BUF}"
238 if [ "$1" = "xzonly" ]; then
239 fwfile="${fwfile}-orig"
242 # Strip fwfile down to match partial offset and length
243 partial_data="$(cat $fwfile)"
244 partial_data="${partial_data:$2:$3}"
246 for i in $(seq 0 3); do
247 config_set_read_fw_idx $i
249 read_firmware="$(cat $DIR/read_firmware)"
251 # Verify the contents are what we expect.
252 if [ $read_firmware != $partial_data ]; then
253 echo "request #$i: partial firmware was not loaded" >&2
259 read_firmwares_expect_nofile()
261 for i in $(seq 0 3); do
262 config_set_read_fw_idx $i
263 # Ensures contents differ
264 if diff -q -Z "$FW" $DIR/read_firmware 2>/dev/null ; then
265 echo "request $i: file was not expected to match" >&2
271 test_batched_request_firmware_nofile()
273 echo -n "Batched request_firmware() nofile try #$1: "
275 config_set_name nope-test-firmware.bin
277 read_firmwares_expect_nofile
282 test_batched_request_firmware_into_buf_nofile()
284 echo -n "Batched request_firmware_into_buf() nofile try #$1: "
286 config_set_name nope-test-firmware.bin
289 read_firmwares_expect_nofile
294 test_request_partial_firmware_into_buf_nofile()
296 echo -n "Test request_partial_firmware_into_buf() off=$1 size=$2 nofile: "
298 config_set_name nope-test-firmware.bin
301 config_set_buf_size $2
302 config_set_file_offset $1
304 read_firmwares_expect_nofile
309 test_batched_request_firmware_direct_nofile()
311 echo -n "Batched request_firmware_direct() nofile try #$1: "
313 config_set_name nope-test-firmware.bin
314 config_set_sync_direct
320 test_request_firmware_nowait_uevent_nofile()
322 echo -n "Batched request_firmware_nowait(uevent=true) nofile try #$1: "
324 config_set_name nope-test-firmware.bin
330 test_wait_and_cancel_custom_load()
332 if [ "$HAS_FW_LOADER_USER_HELPER" != "yes" ]; then
337 while [ ! -e "$DIR"/"$name"/loading ]; do
339 timeout=$(( $timeout - 1 ))
340 if [ "$timeout" -eq 0 ]; then
341 echo "firmware interface never appeared:" >&2
342 echo "$DIR/$name/loading" >&2
346 echo -1 >"$DIR"/"$name"/loading
349 test_request_firmware_nowait_custom_nofile()
351 echo -n "Batched request_firmware_nowait(uevent=false) nofile try #$1: "
354 RANDOM_FILE_PATH=$(setup_random_file_fake)
355 RANDOM_FILE="$(basename $RANDOM_FILE_PATH)"
356 config_set_name $RANDOM_FILE
357 config_trigger_async &
358 test_wait_and_cancel_custom_load $RANDOM_FILE
364 test_batched_request_firmware()
366 echo -n "Batched request_firmware() $2 try #$1: "
374 test_batched_request_firmware_into_buf()
376 echo -n "Batched request_firmware_into_buf() $2 try #$1: "
378 config_set_name $TEST_FIRMWARE_INTO_BUF_FILENAME
386 test_batched_request_firmware_direct()
388 echo -n "Batched request_firmware_direct() $2 try #$1: "
390 config_set_sync_direct
396 test_request_firmware_nowait_uevent()
398 echo -n "Batched request_firmware_nowait(uevent=true) $2 try #$1: "
405 test_request_firmware_nowait_custom()
407 echo -n "Batched request_firmware_nowait(uevent=false) $2 try #$1: "
410 RANDOM_FILE_PATH=$(setup_random_file)
411 RANDOM_FILE="$(basename $RANDOM_FILE_PATH)"
412 if [ "$2" = "both" ]; then
413 xz -9 -C crc32 -k $RANDOM_FILE_PATH
414 elif [ "$2" = "xzonly" ]; then
415 xz -9 -C crc32 $RANDOM_FILE_PATH
417 config_set_name $RANDOM_FILE
423 test_request_partial_firmware_into_buf()
425 echo -n "Test request_partial_firmware_into_buf() off=$1 size=$2: "
427 config_set_name $TEST_FIRMWARE_INTO_BUF_FILENAME
430 config_set_buf_size $2
431 config_set_file_offset $1
433 read_partial_firmwares normal $1 $2
438 # Only continue if batched request triggers are present on the
439 # test-firmware driver
442 # test with the file present
444 echo "Testing with the file present..."
445 for i in $(seq 1 5); do
446 test_batched_request_firmware $i normal
449 for i in $(seq 1 5); do
450 test_batched_request_firmware_into_buf $i normal
453 for i in $(seq 1 5); do
454 test_batched_request_firmware_direct $i normal
457 for i in $(seq 1 5); do
458 test_request_firmware_nowait_uevent $i normal
461 for i in $(seq 1 5); do
462 test_request_firmware_nowait_custom $i normal
465 # Partial loads cannot use fallback, so do not repeat tests.
466 test_request_partial_firmware_into_buf 0 10
467 test_request_partial_firmware_into_buf 0 5
468 test_request_partial_firmware_into_buf 1 6
469 test_request_partial_firmware_into_buf 2 10
471 # Test for file not found, errors are expected, the failure would be
472 # a hung task, which would require a hard reset.
474 echo "Testing with the file missing..."
475 for i in $(seq 1 5); do
476 test_batched_request_firmware_nofile $i
479 for i in $(seq 1 5); do
480 test_batched_request_firmware_into_buf_nofile $i
483 for i in $(seq 1 5); do
484 test_batched_request_firmware_direct_nofile $i
487 for i in $(seq 1 5); do
488 test_request_firmware_nowait_uevent_nofile $i
491 for i in $(seq 1 5); do
492 test_request_firmware_nowait_custom_nofile $i
495 # Partial loads cannot use fallback, so do not repeat tests.
496 test_request_partial_firmware_into_buf_nofile 0 10
497 test_request_partial_firmware_into_buf_nofile 0 5
498 test_request_partial_firmware_into_buf_nofile 1 6
499 test_request_partial_firmware_into_buf_nofile 2 10
501 test "$HAS_FW_LOADER_COMPRESS" != "yes" && exit 0
503 # test with both files present
504 xz -9 -C crc32 -k $FW
505 config_set_name $NAME
507 echo "Testing with both plain and xz files present..."
508 for i in $(seq 1 5); do
509 test_batched_request_firmware $i both
512 for i in $(seq 1 5); do
513 test_batched_request_firmware_into_buf $i both
516 for i in $(seq 1 5); do
517 test_batched_request_firmware_direct $i both
520 for i in $(seq 1 5); do
521 test_request_firmware_nowait_uevent $i both
524 for i in $(seq 1 5); do
525 test_request_firmware_nowait_custom $i both
528 # test with only xz file present
529 mv "$FW" "${FW}-orig"
531 echo "Testing with only xz file present..."
532 for i in $(seq 1 5); do
533 test_batched_request_firmware $i xzonly
536 for i in $(seq 1 5); do
537 test_batched_request_firmware_into_buf $i xzonly
540 for i in $(seq 1 5); do
541 test_batched_request_firmware_direct $i xzonly
544 for i in $(seq 1 5); do
545 test_request_firmware_nowait_uevent $i xzonly
548 for i in $(seq 1 5); do
549 test_request_firmware_nowait_custom $i xzonly