fw_userhelper.sh 2.5 KB

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