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