fw_filesystem.sh 3.0 KB

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