fw_lib.sh 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. #!/bin/bash
  2. # SPDX-License-Identifier: GPL-2.0
  3. # Library of helpers for test scripts.
  4. set -e
  5. DIR=/sys/devices/virtual/misc/test_firmware
  6. PROC_CONFIG="/proc/config.gz"
  7. TEST_DIR=$(dirname $0)
  8. print_reqs_exit()
  9. {
  10. echo "You must have the following enabled in your kernel:" >&2
  11. cat $TEST_DIR/config >&2
  12. exit 1
  13. }
  14. test_modprobe()
  15. {
  16. if [ ! -d $DIR ]; then
  17. print_reqs_exit
  18. fi
  19. }
  20. check_mods()
  21. {
  22. trap "test_modprobe" EXIT
  23. if [ ! -d $DIR ]; then
  24. modprobe test_firmware
  25. fi
  26. if [ ! -f $PROC_CONFIG ]; then
  27. if modprobe configs 2>/dev/null; then
  28. echo "Loaded configs module"
  29. if [ ! -f $PROC_CONFIG ]; then
  30. echo "You must have the following enabled in your kernel:" >&2
  31. cat $TEST_DIR/config >&2
  32. echo "Resorting to old heuristics" >&2
  33. fi
  34. else
  35. echo "Failed to load configs module, using old heuristics" >&2
  36. fi
  37. fi
  38. }
  39. check_setup()
  40. {
  41. HAS_FW_LOADER_USER_HELPER="$(kconfig_has CONFIG_FW_LOADER_USER_HELPER=y)"
  42. HAS_FW_LOADER_USER_HELPER_FALLBACK="$(kconfig_has CONFIG_FW_LOADER_USER_HELPER_FALLBACK=y)"
  43. PROC_FW_IGNORE_SYSFS_FALLBACK="0"
  44. PROC_FW_FORCE_SYSFS_FALLBACK="0"
  45. if [ -z $PROC_SYS_DIR ]; then
  46. PROC_SYS_DIR="/proc/sys/kernel"
  47. fi
  48. FW_PROC="${PROC_SYS_DIR}/firmware_config"
  49. FW_FORCE_SYSFS_FALLBACK="$FW_PROC/force_sysfs_fallback"
  50. FW_IGNORE_SYSFS_FALLBACK="$FW_PROC/ignore_sysfs_fallback"
  51. if [ -f $FW_FORCE_SYSFS_FALLBACK ]; then
  52. PROC_FW_FORCE_SYSFS_FALLBACK="$(cat $FW_FORCE_SYSFS_FALLBACK)"
  53. fi
  54. if [ -f $FW_IGNORE_SYSFS_FALLBACK ]; then
  55. PROC_FW_IGNORE_SYSFS_FALLBACK="$(cat $FW_IGNORE_SYSFS_FALLBACK)"
  56. fi
  57. if [ "$PROC_FW_FORCE_SYSFS_FALLBACK" = "1" ]; then
  58. HAS_FW_LOADER_USER_HELPER="yes"
  59. HAS_FW_LOADER_USER_HELPER_FALLBACK="yes"
  60. fi
  61. if [ "$PROC_FW_IGNORE_SYSFS_FALLBACK" = "1" ]; then
  62. HAS_FW_LOADER_USER_HELPER_FALLBACK="no"
  63. HAS_FW_LOADER_USER_HELPER="no"
  64. fi
  65. if [ "$HAS_FW_LOADER_USER_HELPER" = "yes" ]; then
  66. OLD_TIMEOUT="$(cat /sys/class/firmware/timeout)"
  67. fi
  68. OLD_FWPATH="$(cat /sys/module/firmware_class/parameters/path)"
  69. }
  70. verify_reqs()
  71. {
  72. if [ "$TEST_REQS_FW_SYSFS_FALLBACK" = "yes" ]; then
  73. if [ ! "$HAS_FW_LOADER_USER_HELPER" = "yes" ]; then
  74. echo "usermode helper disabled so ignoring test"
  75. exit 0
  76. fi
  77. fi
  78. }
  79. setup_tmp_file()
  80. {
  81. FWPATH=$(mktemp -d)
  82. FW="$FWPATH/test-firmware.bin"
  83. echo "ABCD0123" >"$FW"
  84. NAME=$(basename "$FW")
  85. if [ "$TEST_REQS_FW_SET_CUSTOM_PATH" = "yes" ]; then
  86. echo -n "$FWPATH" >/sys/module/firmware_class/parameters/path
  87. fi
  88. }
  89. __setup_random_file()
  90. {
  91. RANDOM_FILE_PATH="$(mktemp -p $FWPATH)"
  92. # mktemp says dry-run -n is unsafe, so...
  93. if [[ "$1" = "fake" ]]; then
  94. rm -rf $RANDOM_FILE_PATH
  95. sync
  96. else
  97. echo "ABCD0123" >"$RANDOM_FILE_PATH"
  98. fi
  99. echo $RANDOM_FILE_PATH
  100. }
  101. setup_random_file()
  102. {
  103. echo $(__setup_random_file)
  104. }
  105. setup_random_file_fake()
  106. {
  107. echo $(__setup_random_file fake)
  108. }
  109. proc_set_force_sysfs_fallback()
  110. {
  111. if [ -f $FW_FORCE_SYSFS_FALLBACK ]; then
  112. echo -n $1 > $FW_FORCE_SYSFS_FALLBACK
  113. check_setup
  114. fi
  115. }
  116. proc_set_ignore_sysfs_fallback()
  117. {
  118. if [ -f $FW_IGNORE_SYSFS_FALLBACK ]; then
  119. echo -n $1 > $FW_IGNORE_SYSFS_FALLBACK
  120. check_setup
  121. fi
  122. }
  123. proc_restore_defaults()
  124. {
  125. proc_set_force_sysfs_fallback 0
  126. proc_set_ignore_sysfs_fallback 0
  127. }
  128. test_finish()
  129. {
  130. if [ "$HAS_FW_LOADER_USER_HELPER" = "yes" ]; then
  131. echo "$OLD_TIMEOUT" >/sys/class/firmware/timeout
  132. fi
  133. if [ "$TEST_REQS_FW_SET_CUSTOM_PATH" = "yes" ]; then
  134. if [ "$OLD_FWPATH" = "" ]; then
  135. # A zero-length write won't work; write a null byte
  136. printf '\000' >/sys/module/firmware_class/parameters/path
  137. else
  138. echo -n "$OLD_FWPATH" >/sys/module/firmware_class/parameters/path
  139. fi
  140. fi
  141. if [ -f $FW ]; then
  142. rm -f "$FW"
  143. fi
  144. if [ -d $FWPATH ]; then
  145. rm -rf "$FWPATH"
  146. fi
  147. proc_restore_defaults
  148. }
  149. kconfig_has()
  150. {
  151. if [ -f $PROC_CONFIG ]; then
  152. if zgrep -q $1 $PROC_CONFIG 2>/dev/null; then
  153. echo "yes"
  154. else
  155. echo "no"
  156. fi
  157. else
  158. # We currently don't have easy heuristics to infer this
  159. # so best we can do is just try to use the kernel assuming
  160. # you had enabled it. This matches the old behaviour.
  161. if [ "$1" = "CONFIG_FW_LOADER_USER_HELPER_FALLBACK=y" ]; then
  162. echo "yes"
  163. elif [ "$1" = "CONFIG_FW_LOADER_USER_HELPER=y" ]; then
  164. if [ -d /sys/class/firmware/ ]; then
  165. echo yes
  166. else
  167. echo no
  168. fi
  169. fi
  170. fi
  171. }