fw_filesystem.sh 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. 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. fi
  16. OLD_FWPATH=$(cat /sys/module/firmware_class/parameters/path)
  17. FWPATH=$(mktemp -d)
  18. FW="$FWPATH/test-firmware.bin"
  19. test_finish()
  20. {
  21. if [ "$HAS_FW_LOADER_USER_HELPER" = "yes" ]; then
  22. echo "$OLD_TIMEOUT" >/sys/class/firmware/timeout
  23. fi
  24. echo -n "$OLD_PATH" >/sys/module/firmware_class/parameters/path
  25. rm -f "$FW"
  26. rmdir "$FWPATH"
  27. }
  28. trap "test_finish" EXIT
  29. if [ "$HAS_FW_LOADER_USER_HELPER" = "yes" ]; then
  30. # Turn down the timeout so failures don't take so long.
  31. echo 1 >/sys/class/firmware/timeout
  32. fi
  33. # Set the kernel search path.
  34. echo -n "$FWPATH" >/sys/module/firmware_class/parameters/path
  35. # This is an unlikely real-world firmware content. :)
  36. echo "ABCD0123" >"$FW"
  37. NAME=$(basename "$FW")
  38. if printf '\000' >"$DIR"/trigger_request; then
  39. echo "$0: empty filename should not succeed" >&2
  40. exit 1
  41. fi
  42. if printf '\000' >"$DIR"/trigger_async_request; then
  43. echo "$0: empty filename should not succeed (async)" >&2
  44. exit 1
  45. fi
  46. # Request a firmware that doesn't exist, it should fail.
  47. if echo -n "nope-$NAME" >"$DIR"/trigger_request; then
  48. echo "$0: firmware shouldn't have loaded" >&2
  49. exit 1
  50. fi
  51. if diff -q "$FW" /dev/test_firmware >/dev/null ; then
  52. echo "$0: firmware was not expected to match" >&2
  53. exit 1
  54. else
  55. if [ "$HAS_FW_LOADER_USER_HELPER" = "yes" ]; then
  56. echo "$0: timeout works"
  57. fi
  58. fi
  59. # This should succeed via kernel load or will fail after 1 second after
  60. # being handed over to the user helper, which won't find the fw either.
  61. if ! echo -n "$NAME" >"$DIR"/trigger_request ; then
  62. echo "$0: could not trigger request" >&2
  63. exit 1
  64. fi
  65. # Verify the contents are what we expect.
  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: filesystem loading works"
  71. fi
  72. # Try the asynchronous version too
  73. if ! echo -n "$NAME" >"$DIR"/trigger_async_request ; then
  74. echo "$0: could not trigger async request" >&2
  75. exit 1
  76. fi
  77. # Verify the contents are what we expect.
  78. if ! diff -q "$FW" /dev/test_firmware >/dev/null ; then
  79. echo "$0: firmware was not loaded (async)" >&2
  80. exit 1
  81. else
  82. echo "$0: async filesystem loading works"
  83. fi
  84. exit 0