1#!/bin/bash
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.
7set -e
8
9TEST_REQS_FW_SYSFS_FALLBACK="no"
10TEST_REQS_FW_SET_CUSTOM_PATH="yes"
11TEST_DIR=$(dirname $0)
12source $TEST_DIR/fw_lib.sh
13
14check_mods
15check_setup
16verify_reqs
17setup_tmp_file
18
19trap "test_finish" EXIT
20
21if [ "$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
24fi
25
26if printf '\000' >"$DIR"/trigger_request 2> /dev/null; then
27	echo "$0: empty filename should not succeed" >&2
28	exit 1
29fi
30
31if [ ! -e "$DIR"/trigger_async_request ]; then
32	echo "$0: empty filename: async trigger not present, ignoring test" >&2
33else
34	if printf '\000' >"$DIR"/trigger_async_request 2> /dev/null; then
35		echo "$0: empty filename should not succeed (async)" >&2
36		exit 1
37	fi
38fi
39
40# Request a firmware that doesn't exist, it should fail.
41if echo -n "nope-$NAME" >"$DIR"/trigger_request 2> /dev/null; then
42	echo "$0: firmware shouldn't have loaded" >&2
43	exit 1
44fi
45if diff -q "$FW" /dev/test_firmware >/dev/null ; then
46	echo "$0: firmware was not expected to match" >&2
47	exit 1
48else
49	if [ "$HAS_FW_LOADER_USER_HELPER" = "yes" ]; then
50		echo "$0: timeout works"
51	fi
52fi
53
54# This should succeed via kernel load or will fail after 1 second after
55# being handed over to the user helper, which won't find the fw either.
56if ! echo -n "$NAME" >"$DIR"/trigger_request ; then
57	echo "$0: could not trigger request" >&2
58	exit 1
59fi
60
61# Verify the contents are what we expect.
62if ! diff -q "$FW" /dev/test_firmware >/dev/null ; then
63	echo "$0: firmware was not loaded" >&2
64	exit 1
65else
66	echo "$0: filesystem loading works"
67fi
68
69# Try the asynchronous version too
70if [ ! -e "$DIR"/trigger_async_request ]; then
71	echo "$0: firmware loading: async trigger not present, ignoring test" >&2
72else
73	if ! echo -n "$NAME" >"$DIR"/trigger_async_request ; then
74		echo "$0: could not trigger async request" >&2
75		exit 1
76	fi
77
78	# Verify the contents are what we expect.
79	if ! diff -q "$FW" /dev/test_firmware >/dev/null ; then
80		echo "$0: firmware was not loaded (async)" >&2
81		exit 1
82	else
83		echo "$0: async filesystem loading works"
84	fi
85fi
86
87### Batched requests tests
88test_config_present()
89{
90	if [ ! -f $DIR/reset ]; then
91		echo "Configuration triggers not present, ignoring test"
92		exit 0
93	fi
94}
95
96# Defaults :
97#
98# send_uevent: 1
99# sync_direct: 0
100# name: test-firmware.bin
101# num_requests: 4
102config_reset()
103{
104	echo 1 >  $DIR/reset
105}
106
107release_all_firmware()
108{
109	echo 1 >  $DIR/release_all_firmware
110}
111
112config_set_name()
113{
114	echo -n $1 >  $DIR/config_name
115}
116
117config_set_sync_direct()
118{
119	echo 1 >  $DIR/config_sync_direct
120}
121
122config_unset_sync_direct()
123{
124	echo 0 >  $DIR/config_sync_direct
125}
126
127config_set_uevent()
128{
129	echo 1 >  $DIR/config_send_uevent
130}
131
132config_unset_uevent()
133{
134	echo 0 >  $DIR/config_send_uevent
135}
136
137config_trigger_sync()
138{
139	echo -n 1 > $DIR/trigger_batched_requests 2>/dev/null
140}
141
142config_trigger_async()
143{
144	echo -n 1 > $DIR/trigger_batched_requests_async 2> /dev/null
145}
146
147config_set_read_fw_idx()
148{
149	echo -n $1 > $DIR/config_read_fw_idx 2> /dev/null
150}
151
152read_firmwares()
153{
154	for i in $(seq 0 3); do
155		config_set_read_fw_idx $i
156		# Verify the contents are what we expect.
157		# -Z required for now -- check for yourself, md5sum
158		# on $FW and DIR/read_firmware will yield the same. Even
159		# cmp agrees, so something is off.
160		if ! diff -q -Z "$FW" $DIR/read_firmware 2>/dev/null ; then
161			echo "request #$i: firmware was not loaded" >&2
162			exit 1
163		fi
164	done
165}
166
167read_firmwares_expect_nofile()
168{
169	for i in $(seq 0 3); do
170		config_set_read_fw_idx $i
171		# Ensures contents differ
172		if diff -q -Z "$FW" $DIR/read_firmware 2>/dev/null ; then
173			echo "request $i: file was not expected to match" >&2
174			exit 1
175		fi
176	done
177}
178
179test_batched_request_firmware_nofile()
180{
181	echo -n "Batched request_firmware() nofile try #$1: "
182	config_reset
183	config_set_name nope-test-firmware.bin
184	config_trigger_sync
185	read_firmwares_expect_nofile
186	release_all_firmware
187	echo "OK"
188}
189
190test_batched_request_firmware_direct_nofile()
191{
192	echo -n "Batched request_firmware_direct() nofile try #$1: "
193	config_reset
194	config_set_name nope-test-firmware.bin
195	config_set_sync_direct
196	config_trigger_sync
197	release_all_firmware
198	echo "OK"
199}
200
201test_request_firmware_nowait_uevent_nofile()
202{
203	echo -n "Batched request_firmware_nowait(uevent=true) nofile try #$1: "
204	config_reset
205	config_set_name nope-test-firmware.bin
206	config_trigger_async
207	release_all_firmware
208	echo "OK"
209}
210
211test_wait_and_cancel_custom_load()
212{
213	if [ "$HAS_FW_LOADER_USER_HELPER" != "yes" ]; then
214		return
215	fi
216	local timeout=10
217	name=$1
218	while [ ! -e "$DIR"/"$name"/loading ]; do
219		sleep 0.1
220		timeout=$(( $timeout - 1 ))
221		if [ "$timeout" -eq 0 ]; then
222			echo "firmware interface never appeared:" >&2
223			echo "$DIR/$name/loading" >&2
224			exit 1
225		fi
226	done
227	echo -1 >"$DIR"/"$name"/loading
228}
229
230test_request_firmware_nowait_custom_nofile()
231{
232	echo -n "Batched request_firmware_nowait(uevent=false) nofile try #$1: "
233	config_unset_uevent
234	config_set_name nope-test-firmware.bin
235	config_trigger_async &
236	test_wait_and_cancel_custom_load nope-test-firmware.bin
237	wait
238	release_all_firmware
239	echo "OK"
240}
241
242test_batched_request_firmware()
243{
244	echo -n "Batched request_firmware() try #$1: "
245	config_reset
246	config_trigger_sync
247	read_firmwares
248	release_all_firmware
249	echo "OK"
250}
251
252test_batched_request_firmware_direct()
253{
254	echo -n "Batched request_firmware_direct() try #$1: "
255	config_reset
256	config_set_sync_direct
257	config_trigger_sync
258	release_all_firmware
259	echo "OK"
260}
261
262test_request_firmware_nowait_uevent()
263{
264	echo -n "Batched request_firmware_nowait(uevent=true) try #$1: "
265	config_reset
266	config_trigger_async
267	release_all_firmware
268	echo "OK"
269}
270
271test_request_firmware_nowait_custom()
272{
273	echo -n "Batched request_firmware_nowait(uevent=false) try #$1: "
274	config_unset_uevent
275	config_trigger_async
276	release_all_firmware
277	echo "OK"
278}
279
280# Only continue if batched request triggers are present on the
281# test-firmware driver
282test_config_present
283
284# test with the file present
285echo
286echo "Testing with the file present..."
287for i in $(seq 1 5); do
288	test_batched_request_firmware $i
289done
290
291for i in $(seq 1 5); do
292	test_batched_request_firmware_direct $i
293done
294
295for i in $(seq 1 5); do
296	test_request_firmware_nowait_uevent $i
297done
298
299for i in $(seq 1 5); do
300	test_request_firmware_nowait_custom $i
301done
302
303# Test for file not found, errors are expected, the failure would be
304# a hung task, which would require a hard reset.
305echo
306echo "Testing with the file missing..."
307for i in $(seq 1 5); do
308	test_batched_request_firmware_nofile $i
309done
310
311for i in $(seq 1 5); do
312	test_batched_request_firmware_direct_nofile $i
313done
314
315for i in $(seq 1 5); do
316	test_request_firmware_nowait_uevent_nofile $i
317done
318
319for i in $(seq 1 5); do
320	test_request_firmware_nowait_custom_nofile $i
321done
322
323exit 0
324