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