fw_fallback.sh 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. #!/bin/sh
  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.
  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. load_fw_cancel()
  50. {
  51. local name="$1"
  52. local file="$2"
  53. # This will block until our load (below) has finished.
  54. echo -n "$name" >"$DIR"/trigger_request 2>/dev/null &
  55. # Give kernel a chance to react.
  56. local timeout=10
  57. while [ ! -e "$DIR"/"$name"/loading ]; do
  58. sleep 0.1
  59. timeout=$(( $timeout - 1 ))
  60. if [ "$timeout" -eq 0 ]; then
  61. echo "$0: firmware interface never appeared" >&2
  62. exit 1
  63. fi
  64. done
  65. echo -1 >"$DIR"/"$name"/loading
  66. # Wait for request to finish.
  67. wait
  68. }
  69. load_fw_custom()
  70. {
  71. local name="$1"
  72. local file="$2"
  73. echo -n "$name" >"$DIR"/trigger_custom_fallback 2>/dev/null &
  74. # Give kernel a chance to react.
  75. local timeout=10
  76. while [ ! -e "$DIR"/"$name"/loading ]; do
  77. sleep 0.1
  78. timeout=$(( $timeout - 1 ))
  79. if [ "$timeout" -eq 0 ]; then
  80. echo "$0: firmware interface never appeared" >&2
  81. exit 1
  82. fi
  83. done
  84. echo 1 >"$DIR"/"$name"/loading
  85. cat "$file" >"$DIR"/"$name"/data
  86. echo 0 >"$DIR"/"$name"/loading
  87. # Wait for request to finish.
  88. wait
  89. }
  90. load_fw_custom_cancel()
  91. {
  92. local name="$1"
  93. local file="$2"
  94. echo -n "$name" >"$DIR"/trigger_custom_fallback 2>/dev/null &
  95. # Give kernel a chance to react.
  96. local timeout=10
  97. while [ ! -e "$DIR"/"$name"/loading ]; do
  98. sleep 0.1
  99. timeout=$(( $timeout - 1 ))
  100. if [ "$timeout" -eq 0 ]; then
  101. echo "$0: firmware interface never appeared" >&2
  102. exit 1
  103. fi
  104. done
  105. echo -1 >"$DIR"/"$name"/loading
  106. # Wait for request to finish.
  107. wait
  108. }
  109. trap "test_finish" EXIT
  110. # This is an unlikely real-world firmware content. :)
  111. echo "ABCD0123" >"$FW"
  112. NAME=$(basename "$FW")
  113. DEVPATH="$DIR"/"nope-$NAME"/loading
  114. # Test failure when doing nothing (timeout works).
  115. echo -n 2 >/sys/class/firmware/timeout
  116. echo -n "nope-$NAME" >"$DIR"/trigger_request 2>/dev/null &
  117. # Give the kernel some time to load the loading file, must be less
  118. # than the timeout above.
  119. sleep 1
  120. if [ ! -f $DEVPATH ]; then
  121. echo "$0: fallback mechanism immediately cancelled"
  122. echo ""
  123. echo "The file never appeared: $DEVPATH"
  124. echo ""
  125. echo "This might be a distribution udev rule setup by your distribution"
  126. echo "to immediately cancel all fallback requests, this must be"
  127. echo "removed before running these tests. To confirm look for"
  128. echo "a firmware rule like /lib/udev/rules.d/50-firmware.rules"
  129. echo "and see if you have something like this:"
  130. echo ""
  131. echo "SUBSYSTEM==\"firmware\", ACTION==\"add\", ATTR{loading}=\"-1\""
  132. echo ""
  133. echo "If you do remove this file or comment out this line before"
  134. echo "proceeding with these tests."
  135. exit 1
  136. fi
  137. if diff -q "$FW" /dev/test_firmware >/dev/null ; then
  138. echo "$0: firmware was not expected to match" >&2
  139. exit 1
  140. else
  141. echo "$0: timeout works"
  142. fi
  143. # Put timeout high enough for us to do work but not so long that failures
  144. # slow down this test too much.
  145. echo 4 >/sys/class/firmware/timeout
  146. # Load this script instead of the desired firmware.
  147. load_fw "$NAME" "$0"
  148. if diff -q "$FW" /dev/test_firmware >/dev/null ; then
  149. echo "$0: firmware was not expected to match" >&2
  150. exit 1
  151. else
  152. echo "$0: firmware comparison works"
  153. fi
  154. # Do a proper load, which should work correctly.
  155. load_fw "$NAME" "$FW"
  156. if ! diff -q "$FW" /dev/test_firmware >/dev/null ; then
  157. echo "$0: firmware was not loaded" >&2
  158. exit 1
  159. else
  160. echo "$0: fallback mechanism works"
  161. fi
  162. load_fw_cancel "nope-$NAME" "$FW"
  163. if diff -q "$FW" /dev/test_firmware >/dev/null ; then
  164. echo "$0: firmware was expected to be cancelled" >&2
  165. exit 1
  166. else
  167. echo "$0: cancelling fallback mechanism works"
  168. fi
  169. load_fw_custom "$NAME" "$FW"
  170. if ! diff -q "$FW" /dev/test_firmware >/dev/null ; then
  171. echo "$0: firmware was not loaded" >&2
  172. exit 1
  173. else
  174. echo "$0: custom fallback loading mechanism works"
  175. fi
  176. load_fw_custom_cancel "nope-$NAME" "$FW"
  177. if diff -q "$FW" /dev/test_firmware >/dev/null ; then
  178. echo "$0: firmware was expected to be cancelled" >&2
  179. exit 1
  180. else
  181. echo "$0: cancelling custom fallback mechanism works"
  182. fi
  183. exit 0