fw_userhelper.sh 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. #!/bin/sh
  2. # This validates that the kernel will fall back to using the user helper
  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.
  6. set -e
  7. modprobe test_firmware
  8. DIR=/sys/devices/virtual/misc/test_firmware
  9. OLD_TIMEOUT=$(cat /sys/class/firmware/timeout)
  10. FWPATH=$(mktemp -d)
  11. FW="$FWPATH/test-firmware.bin"
  12. test_finish()
  13. {
  14. echo "$OLD_TIMEOUT" >/sys/class/firmware/timeout
  15. rm -f "$FW"
  16. rmdir "$FWPATH"
  17. }
  18. load_fw()
  19. {
  20. local name="$1"
  21. local file="$2"
  22. # This will block until our load (below) has finished.
  23. echo -n "$name" >"$DIR"/trigger_request &
  24. # Give kernel a chance to react.
  25. local timeout=10
  26. while [ ! -e "$DIR"/"$name"/loading ]; do
  27. sleep 0.1
  28. timeout=$(( $timeout - 1 ))
  29. if [ "$timeout" -eq 0 ]; then
  30. echo "$0: firmware interface never appeared" >&2
  31. exit 1
  32. fi
  33. done
  34. echo 1 >"$DIR"/"$name"/loading
  35. cat "$file" >"$DIR"/"$name"/data
  36. echo 0 >"$DIR"/"$name"/loading
  37. # Wait for request to finish.
  38. wait
  39. }
  40. trap "test_finish" EXIT
  41. # This is an unlikely real-world firmware content. :)
  42. echo "ABCD0123" >"$FW"
  43. NAME=$(basename "$FW")
  44. # Test failure when doing nothing (timeout works).
  45. echo 1 >/sys/class/firmware/timeout
  46. echo -n "$NAME" >"$DIR"/trigger_request
  47. if diff -q "$FW" /dev/test_firmware >/dev/null ; then
  48. echo "$0: firmware was not expected to match" >&2
  49. exit 1
  50. else
  51. echo "$0: timeout works"
  52. fi
  53. # Put timeout high enough for us to do work but not so long that failures
  54. # slow down this test too much.
  55. echo 4 >/sys/class/firmware/timeout
  56. # Load this script instead of the desired firmware.
  57. load_fw "$NAME" "$0"
  58. if diff -q "$FW" /dev/test_firmware >/dev/null ; then
  59. echo "$0: firmware was not expected to match" >&2
  60. exit 1
  61. else
  62. echo "$0: firmware comparison works"
  63. fi
  64. # Do a proper load, which should work correctly.
  65. load_fw "$NAME" "$FW"
  66. if ! diff -q "$FW" /dev/test_firmware >/dev/null ; then
  67. echo "$0: firmware was not loaded" >&2
  68. exit 1
  69. else
  70. echo "$0: user helper firmware loading works"
  71. fi
  72. exit 0