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
31 trap "test_modprobe" EXIT
32 if [ ! -d $DIR ]; then
33 modprobe test_firmware
35 if [ ! -f $PROC_CONFIG ]; then
36 if modprobe configs
2>/dev
/null
; then
37 echo "Loaded configs module"
38 if [ ! -f $PROC_CONFIG ]; then
39 echo "You must have the following enabled in your kernel:" >&2
40 cat $TEST_DIR/config
>&2
41 echo "Resorting to old heuristics" >&2
44 echo "Failed to load configs module, using old heuristics" >&2
51 HAS_FW_LOADER_USER_HELPER
="$(kconfig_has CONFIG_FW_LOADER_USER_HELPER=y)"
52 HAS_FW_LOADER_USER_HELPER_FALLBACK
="$(kconfig_has CONFIG_FW_LOADER_USER_HELPER_FALLBACK=y)"
53 PROC_FW_IGNORE_SYSFS_FALLBACK
="0"
54 PROC_FW_FORCE_SYSFS_FALLBACK
="0"
56 if [ -z $PROC_SYS_DIR ]; then
57 PROC_SYS_DIR
="/proc/sys/kernel"
60 FW_PROC
="${PROC_SYS_DIR}/firmware_config"
61 FW_FORCE_SYSFS_FALLBACK
="$FW_PROC/force_sysfs_fallback"
62 FW_IGNORE_SYSFS_FALLBACK
="$FW_PROC/ignore_sysfs_fallback"
64 if [ -f $FW_FORCE_SYSFS_FALLBACK ]; then
65 PROC_FW_FORCE_SYSFS_FALLBACK
="$(cat $FW_FORCE_SYSFS_FALLBACK)"
68 if [ -f $FW_IGNORE_SYSFS_FALLBACK ]; then
69 PROC_FW_IGNORE_SYSFS_FALLBACK
="$(cat $FW_IGNORE_SYSFS_FALLBACK)"
72 if [ "$PROC_FW_FORCE_SYSFS_FALLBACK" = "1" ]; then
73 HAS_FW_LOADER_USER_HELPER
="yes"
74 HAS_FW_LOADER_USER_HELPER_FALLBACK
="yes"
77 if [ "$PROC_FW_IGNORE_SYSFS_FALLBACK" = "1" ]; then
78 HAS_FW_LOADER_USER_HELPER_FALLBACK
="no"
79 HAS_FW_LOADER_USER_HELPER
="no"
82 if [ "$HAS_FW_LOADER_USER_HELPER" = "yes" ]; then
83 OLD_TIMEOUT
="$(cat /sys/class/firmware/timeout)"
86 OLD_FWPATH
="$(cat /sys/module/firmware_class/parameters/path)"
91 if [ "$TEST_REQS_FW_SYSFS_FALLBACK" = "yes" ]; then
92 if [ ! "$HAS_FW_LOADER_USER_HELPER" = "yes" ]; then
93 echo "usermode helper disabled so ignoring test"
102 FW
="$FWPATH/test-firmware.bin"
103 echo "ABCD0123" >"$FW"
104 NAME
=$
(basename "$FW")
105 if [ "$TEST_REQS_FW_SET_CUSTOM_PATH" = "yes" ]; then
106 echo -n "$FWPATH" >/sys
/module
/firmware_class
/parameters
/path
110 __setup_random_file
()
112 RANDOM_FILE_PATH
="$(mktemp -p $FWPATH)"
113 # mktemp says dry-run -n is unsafe, so...
114 if [[ "$1" = "fake" ]]; then
115 rm -rf $RANDOM_FILE_PATH
118 echo "ABCD0123" >"$RANDOM_FILE_PATH"
120 echo $RANDOM_FILE_PATH
125 echo $
(__setup_random_file
)
128 setup_random_file_fake
()
130 echo $
(__setup_random_file fake
)
133 proc_set_force_sysfs_fallback
()
135 if [ -f $FW_FORCE_SYSFS_FALLBACK ]; then
136 echo -n $1 > $FW_FORCE_SYSFS_FALLBACK
141 proc_set_ignore_sysfs_fallback
()
143 if [ -f $FW_IGNORE_SYSFS_FALLBACK ]; then
144 echo -n $1 > $FW_IGNORE_SYSFS_FALLBACK
149 proc_restore_defaults
()
151 proc_set_force_sysfs_fallback
0
152 proc_set_ignore_sysfs_fallback
0
157 if [ "$HAS_FW_LOADER_USER_HELPER" = "yes" ]; then
158 echo "$OLD_TIMEOUT" >/sys
/class
/firmware
/timeout
160 if [ "$TEST_REQS_FW_SET_CUSTOM_PATH" = "yes" ]; then
161 if [ "$OLD_FWPATH" = "" ]; then
162 # A zero-length write won't work; write a null byte
163 printf '\000' >/sys
/module
/firmware_class
/parameters
/path
165 echo -n "$OLD_FWPATH" >/sys
/module
/firmware_class
/parameters
/path
171 if [ -d $FWPATH ]; then
174 proc_restore_defaults
179 if [ -f $PROC_CONFIG ]; then
180 if zgrep -q $1 $PROC_CONFIG 2>/dev
/null
; then
186 # We currently don't have easy heuristics to infer this
187 # so best we can do is just try to use the kernel assuming
188 # you had enabled it. This matches the old behaviour.
189 if [ "$1" = "CONFIG_FW_LOADER_USER_HELPER_FALLBACK=y" ]; then
191 elif [ "$1" = "CONFIG_FW_LOADER_USER_HELPER=y" ]; then
192 if [ -d /sys
/class
/firmware
/ ]; then