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 # We need to load a different file to test request_firmware_into_buf
13 # I believe the issue is firmware loaded cached vs. non-cached
14 # with same filename is bungled.
15 # To reproduce rename this to test-firmware.bin
16 TEST_FIRMWARE_INTO_BUF_FILENAME=test-firmware-into-buf.bin
18 # Kselftest framework requirement - SKIP code is 4.
23 echo "You must have the following enabled in your kernel:" >&2
24 cat $TEST_DIR/config >&2
30 if [ ! -d $DIR ]; then
38 if [ $uid -ne 0 ]; then
39 echo "skip all tests: must be run as root" >&2
43 trap "test_modprobe" EXIT
44 if [ ! -d $DIR ]; then
45 modprobe test_firmware
47 if [ ! -f $PROC_CONFIG ]; then
48 if modprobe configs 2>/dev/null; then
49 echo "Loaded configs module"
50 if [ ! -f $PROC_CONFIG ]; then
51 echo "You must have the following enabled in your kernel:" >&2
52 cat $TEST_DIR/config >&2
53 echo "Resorting to old heuristics" >&2
56 echo "Failed to load configs module, using old heuristics" >&2
63 HAS_FW_LOADER_USER_HELPER="$(kconfig_has CONFIG_FW_LOADER_USER_HELPER=y)"
64 HAS_FW_LOADER_USER_HELPER_FALLBACK="$(kconfig_has CONFIG_FW_LOADER_USER_HELPER_FALLBACK=y)"
65 HAS_FW_LOADER_COMPRESS_XZ="$(kconfig_has CONFIG_FW_LOADER_COMPRESS_XZ=y)"
66 HAS_FW_LOADER_COMPRESS_ZSTD="$(kconfig_has CONFIG_FW_LOADER_COMPRESS_ZSTD=y)"
67 HAS_FW_UPLOAD="$(kconfig_has CONFIG_FW_UPLOAD=y)"
68 PROC_FW_IGNORE_SYSFS_FALLBACK="0"
69 PROC_FW_FORCE_SYSFS_FALLBACK="0"
71 if [ -z $PROC_SYS_DIR ]; then
72 PROC_SYS_DIR="/proc/sys/kernel"
75 FW_PROC="${PROC_SYS_DIR}/firmware_config"
76 FW_FORCE_SYSFS_FALLBACK="$FW_PROC/force_sysfs_fallback"
77 FW_IGNORE_SYSFS_FALLBACK="$FW_PROC/ignore_sysfs_fallback"
79 if [ -f $FW_FORCE_SYSFS_FALLBACK ]; then
80 PROC_FW_FORCE_SYSFS_FALLBACK="$(cat $FW_FORCE_SYSFS_FALLBACK)"
83 if [ -f $FW_IGNORE_SYSFS_FALLBACK ]; then
84 PROC_FW_IGNORE_SYSFS_FALLBACK="$(cat $FW_IGNORE_SYSFS_FALLBACK)"
87 if [ "$PROC_FW_FORCE_SYSFS_FALLBACK" = "1" ]; then
88 HAS_FW_LOADER_USER_HELPER="yes"
89 HAS_FW_LOADER_USER_HELPER_FALLBACK="yes"
92 if [ "$PROC_FW_IGNORE_SYSFS_FALLBACK" = "1" ]; then
93 HAS_FW_LOADER_USER_HELPER_FALLBACK="no"
94 HAS_FW_LOADER_USER_HELPER="no"
97 if [ "$HAS_FW_LOADER_USER_HELPER" = "yes" ]; then
98 OLD_TIMEOUT="$(cat /sys/class/firmware/timeout)"
101 OLD_FWPATH="$(cat /sys/module/firmware_class/parameters/path)"
103 if [ "$HAS_FW_LOADER_COMPRESS_XZ" = "yes" ]; then
104 if ! which xz 2> /dev/null > /dev/null; then
105 HAS_FW_LOADER_COMPRESS_XZ=""
108 if [ "$HAS_FW_LOADER_COMPRESS_ZSTD" = "yes" ]; then
109 if ! which zstd 2> /dev/null > /dev/null; then
110 HAS_FW_LOADER_COMPRESS_ZSTD=""
117 if [ "$TEST_REQS_FW_SYSFS_FALLBACK" = "yes" ]; then
118 if [ ! "$HAS_FW_LOADER_USER_HELPER" = "yes" ]; then
119 echo "usermode helper disabled so ignoring test"
123 if [ "$TEST_REQS_FW_UPLOAD" = "yes" ]; then
124 if [ ! "$HAS_FW_UPLOAD" = "yes" ]; then
125 echo "firmware upload disabled so ignoring test"
134 FW="$FWPATH/test-firmware.bin"
135 echo "ABCD0123" >"$FW"
136 FW_INTO_BUF="$FWPATH/$TEST_FIRMWARE_INTO_BUF_FILENAME"
137 echo "EFGH4567" >"$FW_INTO_BUF"
138 NAME=$(basename "$FW")
139 if [ "$TEST_REQS_FW_SET_CUSTOM_PATH" = "yes" ]; then
140 echo -n "$FWPATH" >/sys/module/firmware_class/parameters/path
144 __setup_random_file()
146 RANDOM_FILE_PATH="$(mktemp -p $FWPATH)"
147 # mktemp says dry-run -n is unsafe, so...
148 if [[ "$1" = "fake" ]]; then
149 rm -rf $RANDOM_FILE_PATH
152 echo "ABCD0123" >"$RANDOM_FILE_PATH"
154 echo $RANDOM_FILE_PATH
159 echo $(__setup_random_file)
162 setup_random_file_fake()
164 echo $(__setup_random_file fake)
167 proc_set_force_sysfs_fallback()
169 if [ -f $FW_FORCE_SYSFS_FALLBACK ]; then
170 echo -n $1 > $FW_FORCE_SYSFS_FALLBACK
175 proc_set_ignore_sysfs_fallback()
177 if [ -f $FW_IGNORE_SYSFS_FALLBACK ]; then
178 echo -n $1 > $FW_IGNORE_SYSFS_FALLBACK
183 proc_restore_defaults()
185 proc_set_force_sysfs_fallback 0
186 proc_set_ignore_sysfs_fallback 0
191 if [ "$HAS_FW_LOADER_USER_HELPER" = "yes" ]; then
192 echo "$OLD_TIMEOUT" >/sys/class/firmware/timeout
194 if [ "$TEST_REQS_FW_SET_CUSTOM_PATH" = "yes" ]; then
195 if [ "$OLD_FWPATH" = "" ]; then
196 # A zero-length write won't work; write a null byte
197 printf '\000' >/sys/module/firmware_class/parameters/path
199 echo -n "$OLD_FWPATH" >/sys/module/firmware_class/parameters/path
205 if [ -f $FW_INTO_BUF ]; then
208 if [ -d $FWPATH ]; then
211 proc_restore_defaults
216 if [ -f $PROC_CONFIG ]; then
217 if zgrep -q $1 $PROC_CONFIG 2>/dev/null; then
223 # We currently don't have easy heuristics to infer this
224 # so best we can do is just try to use the kernel assuming
225 # you had enabled it. This matches the old behaviour.
226 if [ "$1" = "CONFIG_FW_LOADER_USER_HELPER_FALLBACK=y" ]; then
228 elif [ "$1" = "CONFIG_FW_LOADER_USER_HELPER=y" ]; then
229 if [ -d /sys/class/firmware/ ]; then