2 # SPDX-License-Identifier: GPL-2.0
4 # Library of helpers for test scripts.
7 DIR
=/sys
/devices
/virtual
/misc
/test_firmware
9 PROC_CONFIG
="/proc/config.gz"
10 TEST_DIR
=$
(dirname $0)
12 # Kselftest framework requirement - SKIP code is 4.
17 echo "You must have the following enabled in your kernel:" >&2
18 cat $TEST_DIR/config
>&2
24 if [ ! -d $DIR ]; then
32 if [ $uid -ne 0 ]; then
33 echo "skip all tests: must be run as root" >&2
37 trap "test_modprobe" EXIT
38 if [ ! -d $DIR ]; then
39 modprobe test_firmware
41 if [ ! -f $PROC_CONFIG ]; then
42 if modprobe configs
2>/dev
/null
; then
43 echo "Loaded configs module"
44 if [ ! -f $PROC_CONFIG ]; then
45 echo "You must have the following enabled in your kernel:" >&2
46 cat $TEST_DIR/config
>&2
47 echo "Resorting to old heuristics" >&2
50 echo "Failed to load configs module, using old heuristics" >&2
57 HAS_FW_LOADER_USER_HELPER
="$(kconfig_has CONFIG_FW_LOADER_USER_HELPER=y)"
58 HAS_FW_LOADER_USER_HELPER_FALLBACK
="$(kconfig_has CONFIG_FW_LOADER_USER_HELPER_FALLBACK=y)"
59 PROC_FW_IGNORE_SYSFS_FALLBACK
="0"
60 PROC_FW_FORCE_SYSFS_FALLBACK
="0"
62 if [ -z $PROC_SYS_DIR ]; then
63 PROC_SYS_DIR
="/proc/sys/kernel"
66 FW_PROC
="${PROC_SYS_DIR}/firmware_config"
67 FW_FORCE_SYSFS_FALLBACK
="$FW_PROC/force_sysfs_fallback"
68 FW_IGNORE_SYSFS_FALLBACK
="$FW_PROC/ignore_sysfs_fallback"
70 if [ -f $FW_FORCE_SYSFS_FALLBACK ]; then
71 PROC_FW_FORCE_SYSFS_FALLBACK
="$(cat $FW_FORCE_SYSFS_FALLBACK)"
74 if [ -f $FW_IGNORE_SYSFS_FALLBACK ]; then
75 PROC_FW_IGNORE_SYSFS_FALLBACK
="$(cat $FW_IGNORE_SYSFS_FALLBACK)"
78 if [ "$PROC_FW_FORCE_SYSFS_FALLBACK" = "1" ]; then
79 HAS_FW_LOADER_USER_HELPER
="yes"
80 HAS_FW_LOADER_USER_HELPER_FALLBACK
="yes"
83 if [ "$PROC_FW_IGNORE_SYSFS_FALLBACK" = "1" ]; then
84 HAS_FW_LOADER_USER_HELPER_FALLBACK
="no"
85 HAS_FW_LOADER_USER_HELPER
="no"
88 if [ "$HAS_FW_LOADER_USER_HELPER" = "yes" ]; then
89 OLD_TIMEOUT
="$(cat /sys/class/firmware/timeout)"
92 OLD_FWPATH
="$(cat /sys/module/firmware_class/parameters/path)"
97 if [ "$TEST_REQS_FW_SYSFS_FALLBACK" = "yes" ]; then
98 if [ ! "$HAS_FW_LOADER_USER_HELPER" = "yes" ]; then
99 echo "usermode helper disabled so ignoring test"
108 FW
="$FWPATH/test-firmware.bin"
109 echo "ABCD0123" >"$FW"
110 NAME
=$
(basename "$FW")
111 if [ "$TEST_REQS_FW_SET_CUSTOM_PATH" = "yes" ]; then
112 echo -n "$FWPATH" >/sys
/module
/firmware_class
/parameters
/path
116 __setup_random_file
()
118 RANDOM_FILE_PATH
="$(mktemp -p $FWPATH)"
119 # mktemp says dry-run -n is unsafe, so...
120 if [[ "$1" = "fake" ]]; then
121 rm -rf $RANDOM_FILE_PATH
124 echo "ABCD0123" >"$RANDOM_FILE_PATH"
126 echo $RANDOM_FILE_PATH
131 echo $
(__setup_random_file
)
134 setup_random_file_fake
()
136 echo $
(__setup_random_file fake
)
139 proc_set_force_sysfs_fallback
()
141 if [ -f $FW_FORCE_SYSFS_FALLBACK ]; then
142 echo -n $1 > $FW_FORCE_SYSFS_FALLBACK
147 proc_set_ignore_sysfs_fallback
()
149 if [ -f $FW_IGNORE_SYSFS_FALLBACK ]; then
150 echo -n $1 > $FW_IGNORE_SYSFS_FALLBACK
155 proc_restore_defaults
()
157 proc_set_force_sysfs_fallback
0
158 proc_set_ignore_sysfs_fallback
0
163 if [ "$HAS_FW_LOADER_USER_HELPER" = "yes" ]; then
164 echo "$OLD_TIMEOUT" >/sys
/class
/firmware
/timeout
166 if [ "$TEST_REQS_FW_SET_CUSTOM_PATH" = "yes" ]; then
167 if [ "$OLD_FWPATH" = "" ]; then
168 # A zero-length write won't work; write a null byte
169 printf '\000' >/sys
/module
/firmware_class
/parameters
/path
171 echo -n "$OLD_FWPATH" >/sys
/module
/firmware_class
/parameters
/path
177 if [ -d $FWPATH ]; then
180 proc_restore_defaults
185 if [ -f $PROC_CONFIG ]; then
186 if zgrep -q $1 $PROC_CONFIG 2>/dev
/null
; then
192 # We currently don't have easy heuristics to infer this
193 # so best we can do is just try to use the kernel assuming
194 # you had enabled it. This matches the old behaviour.
195 if [ "$1" = "CONFIG_FW_LOADER_USER_HELPER_FALLBACK=y" ]; then
197 elif [ "$1" = "CONFIG_FW_LOADER_USER_HELPER=y" ]; then
198 if [ -d /sys
/class
/firmware
/ ]; then