fw_userhelper.sh 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. DEVPATH="$DIR"/"nope-$NAME"/loading
  54. # Test failure when doing nothing (timeout works).
  55. echo -n 2 >/sys/class/firmware/timeout
  56. echo -n "nope-$NAME" >"$DIR"/trigger_request 2>/dev/null &
  57. # Give the kernel some time to load the loading file, must be less
  58. # than the timeout above.
  59. sleep 1
  60. if [ ! -f $DEVPATH ]; then
  61. echo "$0: fallback mechanism immediately cancelled"
  62. echo ""
  63. echo "The file never appeared: $DEVPATH"
  64. echo ""
  65. echo "This might be a distribution udev rule setup by your distribution"
  66. echo "to immediately cancel all fallback requests, this must be"
  67. echo "removed before running these tests. To confirm look for"
  68. echo "a firmware rule like /lib/udev/rules.d/50-firmware.rules"
  69. echo "and see if you have something like this:"
  70. echo ""
  71. echo "SUBSYSTEM==\"firmware\", ACTION==\"add\", ATTR{loading}=\"-1\""
  72. echo ""
  73. echo "If you do remove this file or comment out this line before"
  74. echo "proceeding with these tests."
  75. exit 1
  76. fi
  77. if diff -q "$FW" /dev/test_firmware >/dev/null ; then
  78. echo "$0: firmware was not expected to match" >&2
  79. exit 1
  80. else
  81. echo "$0: timeout works"
  82. fi
  83. # Put timeout high enough for us to do work but not so long that failures
  84. # slow down this test too much.
  85. echo 4 >/sys/class/firmware/timeout
  86. # Load this script instead of the desired firmware.
  87. load_fw "$NAME" "$0"
  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. echo "$0: firmware comparison works"
  93. fi
  94. # Do a proper load, which should work correctly.
  95. load_fw "$NAME" "$FW"
  96. if ! diff -q "$FW" /dev/test_firmware >/dev/null ; then
  97. echo "$0: firmware was not loaded" >&2
  98. exit 1
  99. else
  100. echo "$0: user helper firmware loading works"
  101. fi
  102. exit 0