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 HAS_FW_LOADER_COMPRESS
="$(kconfig_has CONFIG_FW_LOADER_COMPRESS=y)"
54 PROC_FW_IGNORE_SYSFS_FALLBACK
="0"
55 PROC_FW_FORCE_SYSFS_FALLBACK
="0"
57 if [ -z $PROC_SYS_DIR ]; then
58 PROC_SYS_DIR
="/proc/sys/kernel"
61 FW_PROC
="${PROC_SYS_DIR}/firmware_config"
62 FW_FORCE_SYSFS_FALLBACK
="$FW_PROC/force_sysfs_fallback"
63 FW_IGNORE_SYSFS_FALLBACK
="$FW_PROC/ignore_sysfs_fallback"
65 if [ -f $FW_FORCE_SYSFS_FALLBACK ]; then
66 PROC_FW_FORCE_SYSFS_FALLBACK
="$(cat $FW_FORCE_SYSFS_FALLBACK)"
69 if [ -f $FW_IGNORE_SYSFS_FALLBACK ]; then
70 PROC_FW_IGNORE_SYSFS_FALLBACK
="$(cat $FW_IGNORE_SYSFS_FALLBACK)"
73 if [ "$PROC_FW_FORCE_SYSFS_FALLBACK" = "1" ]; then
74 HAS_FW_LOADER_USER_HELPER
="yes"
75 HAS_FW_LOADER_USER_HELPER_FALLBACK
="yes"
78 if [ "$PROC_FW_IGNORE_SYSFS_FALLBACK" = "1" ]; then
79 HAS_FW_LOADER_USER_HELPER_FALLBACK
="no"
80 HAS_FW_LOADER_USER_HELPER
="no"
83 if [ "$HAS_FW_LOADER_USER_HELPER" = "yes" ]; then
84 OLD_TIMEOUT
="$(cat /sys/class/firmware/timeout)"
87 OLD_FWPATH
="$(cat /sys/module/firmware_class/parameters/path)"
89 if [ "$HAS_FW_LOADER_COMPRESS" = "yes" ]; then
90 if ! which xz
2> /dev
/null
> /dev
/null
; then
91 HAS_FW_LOADER_COMPRESS
=""
98 if [ "$TEST_REQS_FW_SYSFS_FALLBACK" = "yes" ]; then
99 if [ ! "$HAS_FW_LOADER_USER_HELPER" = "yes" ]; then
100 echo "usermode helper disabled so ignoring test"
109 FW
="$FWPATH/test-firmware.bin"
110 echo "ABCD0123" >"$FW"
111 NAME
=$
(basename "$FW")
112 if [ "$TEST_REQS_FW_SET_CUSTOM_PATH" = "yes" ]; then
113 echo -n "$FWPATH" >/sys
/module
/firmware_class
/parameters
/path
117 __setup_random_file
()
119 RANDOM_FILE_PATH
="$(mktemp -p $FWPATH)"
120 # mktemp says dry-run -n is unsafe, so...
121 if [[ "$1" = "fake" ]]; then
122 rm -rf $RANDOM_FILE_PATH
125 echo "ABCD0123" >"$RANDOM_FILE_PATH"
127 echo $RANDOM_FILE_PATH
132 echo $
(__setup_random_file
)
135 setup_random_file_fake
()
137 echo $
(__setup_random_file fake
)
140 proc_set_force_sysfs_fallback
()
142 if [ -f $FW_FORCE_SYSFS_FALLBACK ]; then
143 echo -n $1 > $FW_FORCE_SYSFS_FALLBACK
148 proc_set_ignore_sysfs_fallback
()
150 if [ -f $FW_IGNORE_SYSFS_FALLBACK ]; then
151 echo -n $1 > $FW_IGNORE_SYSFS_FALLBACK
156 proc_restore_defaults
()
158 proc_set_force_sysfs_fallback
0
159 proc_set_ignore_sysfs_fallback
0
164 if [ "$HAS_FW_LOADER_USER_HELPER" = "yes" ]; then
165 echo "$OLD_TIMEOUT" >/sys
/class
/firmware
/timeout
167 if [ "$TEST_REQS_FW_SET_CUSTOM_PATH" = "yes" ]; then
168 if [ "$OLD_FWPATH" = "" ]; then
169 # A zero-length write won't work; write a null byte
170 printf '\000' >/sys
/module
/firmware_class
/parameters
/path
172 echo -n "$OLD_FWPATH" >/sys
/module
/firmware_class
/parameters
/path
178 if [ -d $FWPATH ]; then
181 proc_restore_defaults
186 if [ -f $PROC_CONFIG ]; then
187 if zgrep -q $1 $PROC_CONFIG 2>/dev
/null
; then
193 # We currently don't have easy heuristics to infer this
194 # so best we can do is just try to use the kernel assuming
195 # you had enabled it. This matches the old behaviour.
196 if [ "$1" = "CONFIG_FW_LOADER_USER_HELPER_FALLBACK=y" ]; then
198 elif [ "$1" = "CONFIG_FW_LOADER_USER_HELPER=y" ]; then
199 if [ -d /sys
/class
/firmware
/ ]; then