fw_filesystem.sh 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. #!/bin/sh
  2. # This validates that the kernel will load firmware out of its list of
  3. # firmware locations on disk. Since the user helper does similar work,
  4. # we reset the custom load directory to a location the user helper doesn't
  5. # know so we can be sure we're not accidentally testing the user helper.
  6. set -e
  7. DIR=/sys/devices/virtual/misc/test_firmware
  8. TEST_DIR=$(dirname $0)
  9. test_modprobe()
  10. {
  11. if [ ! -d $DIR ]; then
  12. echo "$0: $DIR not present"
  13. echo "You must have the following enabled in your kernel:"
  14. cat $TEST_DIR/config
  15. exit 1
  16. fi
  17. }
  18. trap "test_modprobe" EXIT
  19. if [ ! -d $DIR ]; then
  20. modprobe test_firmware
  21. fi
  22. # CONFIG_FW_LOADER_USER_HELPER has a sysfs class under /sys/class/firmware/
  23. # These days no one enables CONFIG_FW_LOADER_USER_HELPER so check for that
  24. # as an indicator for CONFIG_FW_LOADER_USER_HELPER.
  25. HAS_FW_LOADER_USER_HELPER=$(if [ -d /sys/class/firmware/ ]; then echo yes; else echo no; fi)
  26. if [ "$HAS_FW_LOADER_USER_HELPER" = "yes" ]; then
  27. OLD_TIMEOUT=$(cat /sys/class/firmware/timeout)
  28. fi
  29. OLD_FWPATH=$(cat /sys/module/firmware_class/parameters/path)
  30. FWPATH=$(mktemp -d)
  31. FW="$FWPATH/test-firmware.bin"
  32. test_finish()
  33. {
  34. if [ "$HAS_FW_LOADER_USER_HELPER" = "yes" ]; then
  35. echo "$OLD_TIMEOUT" >/sys/class/firmware/timeout
  36. fi
  37. echo -n "$OLD_PATH" >/sys/module/firmware_class/parameters/path
  38. rm -f "$FW"
  39. rmdir "$FWPATH"
  40. }
  41. trap "test_finish" EXIT
  42. if [ "$HAS_FW_LOADER_USER_HELPER" = "yes" ]; then
  43. # Turn down the timeout so failures don't take so long.
  44. echo 1 >/sys/class/firmware/timeout
  45. fi
  46. # Set the kernel search path.
  47. echo -n "$FWPATH" >/sys/module/firmware_class/parameters/path
  48. # This is an unlikely real-world firmware content. :)
  49. echo "ABCD0123" >"$FW"
  50. NAME=$(basename "$FW")
  51. if printf '\000' >"$DIR"/trigger_request 2> /dev/null; then
  52. echo "$0: empty filename should not succeed" >&2
  53. exit 1
  54. fi
  55. if printf '\000' >"$DIR"/trigger_async_request 2> /dev/null; then
  56. echo "$0: empty filename should not succeed (async)" >&2
  57. exit 1
  58. fi
  59. # Request a firmware that doesn't exist, it should fail.
  60. if echo -n "nope-$NAME" >"$DIR"/trigger_request 2> /dev/null; then
  61. echo "$0: firmware shouldn't have loaded" >&2
  62. exit 1
  63. fi
  64. if diff -q "$FW" /dev/test_firmware >/dev/null ; then
  65. echo "$0: firmware was not expected to match" >&2
  66. exit 1
  67. else
  68. if [ "$HAS_FW_LOADER_USER_HELPER" = "yes" ]; then
  69. echo "$0: timeout works"
  70. fi
  71. fi
  72. # This should succeed via kernel load or will fail after 1 second after
  73. # being handed over to the user helper, which won't find the fw either.
  74. if ! echo -n "$NAME" >"$DIR"/trigger_request ; then
  75. echo "$0: could not trigger request" >&2
  76. exit 1
  77. fi
  78. # Verify the contents are what we expect.
  79. if ! diff -q "$FW" /dev/test_firmware >/dev/null ; then
  80. echo "$0: firmware was not loaded" >&2
  81. exit 1
  82. else
  83. echo "$0: filesystem loading works"
  84. fi
  85. # Try the asynchronous version too
  86. if ! echo -n "$NAME" >"$DIR"/trigger_async_request ; then
  87. echo "$0: could not trigger async request" >&2
  88. exit 1
  89. fi
  90. # Verify the contents are what we expect.
  91. if ! diff -q "$FW" /dev/test_firmware >/dev/null ; then
  92. echo "$0: firmware was not loaded (async)" >&2
  93. exit 1
  94. else
  95. echo "$0: async filesystem loading works"
  96. fi
  97. exit 0