2 # This validates that the kernel will fall back to using the fallback mechanism
3 # to load firmware it can't find on disk itself. We must request a firmware
4 # that the kernel won't find, and any installed helper (e.g. udev) also
5 # won't find so that we can do the load ourself manually.
10 DIR
=/sys
/devices
/virtual
/misc
/test_firmware
12 # CONFIG_FW_LOADER_USER_HELPER has a sysfs class under /sys/class/firmware/
13 # These days no one enables CONFIG_FW_LOADER_USER_HELPER so check for that
14 # as an indicator for CONFIG_FW_LOADER_USER_HELPER.
15 HAS_FW_LOADER_USER_HELPER
=$
(if [ -d /sys
/class
/firmware
/ ]; then echo yes; else echo no
; fi)
17 if [ "$HAS_FW_LOADER_USER_HELPER" = "yes" ]; then
18 OLD_TIMEOUT
=$
(cat /sys
/class
/firmware
/timeout
)
20 echo "usermode helper disabled so ignoring test"
25 FW
="$FWPATH/test-firmware.bin"
29 echo "$OLD_TIMEOUT" >/sys
/class
/firmware
/timeout
39 # This will block until our load (below) has finished.
40 echo -n "$name" >"$DIR"/trigger_request
&
42 # Give kernel a chance to react.
44 while [ ! -e "$DIR"/"$name"/loading
]; do
46 timeout
=$
(( $timeout - 1 ))
47 if [ "$timeout" -eq 0 ]; then
48 echo "$0: firmware interface never appeared" >&2
53 echo 1 >"$DIR"/"$name"/loading
54 cat "$file" >"$DIR"/"$name"/data
55 echo 0 >"$DIR"/"$name"/loading
57 # Wait for request to finish.
66 # This will block until our load (below) has finished.
67 echo -n "$name" >"$DIR"/trigger_request
2>/dev
/null
&
69 # Give kernel a chance to react.
71 while [ ! -e "$DIR"/"$name"/loading
]; do
73 timeout
=$
(( $timeout - 1 ))
74 if [ "$timeout" -eq 0 ]; then
75 echo "$0: firmware interface never appeared" >&2
80 echo -1 >"$DIR"/"$name"/loading
82 # Wait for request to finish.
91 echo -n "$name" >"$DIR"/trigger_custom_fallback
2>/dev
/null
&
93 # Give kernel a chance to react.
95 while [ ! -e "$DIR"/"$name"/loading
]; do
97 timeout
=$
(( $timeout - 1 ))
98 if [ "$timeout" -eq 0 ]; then
99 echo "$0: firmware interface never appeared" >&2
104 echo 1 >"$DIR"/"$name"/loading
105 cat "$file" >"$DIR"/"$name"/data
106 echo 0 >"$DIR"/"$name"/loading
108 # Wait for request to finish.
113 load_fw_custom_cancel
()
118 echo -n "$name" >"$DIR"/trigger_custom_fallback
2>/dev
/null
&
120 # Give kernel a chance to react.
122 while [ ! -e "$DIR"/"$name"/loading
]; do
124 timeout
=$
(( $timeout - 1 ))
125 if [ "$timeout" -eq 0 ]; then
126 echo "$0: firmware interface never appeared" >&2
131 echo -1 >"$DIR"/"$name"/loading
133 # Wait for request to finish.
137 load_fw_fallback_with_child
()
142 # This is the value already set but we want to be explicit
143 echo 4 >/sys
/class
/firmware
/timeout
146 SECONDS_BEFORE
=$
(date +%s
)
147 echo -n "$name" >"$DIR"/trigger_request
2>/dev
/null
148 SECONDS_AFTER
=$
(date +%s
)
149 SECONDS_DELTA
=$
(($SECONDS_AFTER - $SECONDS_BEFORE))
150 if [ "$SECONDS_DELTA" -lt 4 ]; then
159 trap "test_finish" EXIT
161 # This is an unlikely real-world firmware content. :)
162 echo "ABCD0123" >"$FW"
163 NAME
=$
(basename "$FW")
165 DEVPATH
="$DIR"/"nope-$NAME"/loading
167 # Test failure when doing nothing (timeout works).
168 echo -n 2 >/sys
/class
/firmware
/timeout
169 echo -n "nope-$NAME" >"$DIR"/trigger_request
2>/dev
/null
&
171 # Give the kernel some time to load the loading file, must be less
172 # than the timeout above.
174 if [ ! -f $DEVPATH ]; then
175 echo "$0: fallback mechanism immediately cancelled"
177 echo "The file never appeared: $DEVPATH"
179 echo "This might be a distribution udev rule setup by your distribution"
180 echo "to immediately cancel all fallback requests, this must be"
181 echo "removed before running these tests. To confirm look for"
182 echo "a firmware rule like /lib/udev/rules.d/50-firmware.rules"
183 echo "and see if you have something like this:"
185 echo "SUBSYSTEM==\"firmware\", ACTION==\"add\", ATTR{loading}=\"-1\""
187 echo "If you do remove this file or comment out this line before"
188 echo "proceeding with these tests."
192 if diff -q "$FW" /dev
/test_firmware
>/dev
/null
; then
193 echo "$0: firmware was not expected to match" >&2
196 echo "$0: timeout works"
199 # Put timeout high enough for us to do work but not so long that failures
200 # slow down this test too much.
201 echo 4 >/sys
/class
/firmware
/timeout
203 # Load this script instead of the desired firmware.
205 if diff -q "$FW" /dev
/test_firmware
>/dev
/null
; then
206 echo "$0: firmware was not expected to match" >&2
209 echo "$0: firmware comparison works"
212 # Do a proper load, which should work correctly.
213 load_fw
"$NAME" "$FW"
214 if ! diff -q "$FW" /dev
/test_firmware
>/dev
/null
; then
215 echo "$0: firmware was not loaded" >&2
218 echo "$0: fallback mechanism works"
221 load_fw_cancel
"nope-$NAME" "$FW"
222 if diff -q "$FW" /dev
/test_firmware
>/dev
/null
; then
223 echo "$0: firmware was expected to be cancelled" >&2
226 echo "$0: cancelling fallback mechanism works"
229 load_fw_custom
"$NAME" "$FW"
230 if ! diff -q "$FW" /dev
/test_firmware
>/dev
/null
; then
231 echo "$0: firmware was not loaded" >&2
234 echo "$0: custom fallback loading mechanism works"
237 load_fw_custom_cancel
"nope-$NAME" "$FW"
238 if diff -q "$FW" /dev
/test_firmware
>/dev
/null
; then
239 echo "$0: firmware was expected to be cancelled" >&2
242 echo "$0: cancelling custom fallback mechanism works"
246 load_fw_fallback_with_child
"nope-signal-$NAME" "$FW"
247 if [ "$?" -eq 0 ]; then
248 echo "$0: SIGCHLD on sync ignored as expected" >&2
250 echo "$0: error - sync firmware request cancelled due to SIGCHLD" >&2