proc: test /proc/thread-self symlink
[linux/fpc-iii.git] / tools / testing / selftests / firmware / fw_lib.sh
blob6c5f1b2ffb74598a79eaf0cc4316cee8b74a5ef9
1 #!/bin/bash
2 # SPDX-License-Identifier: GPL-2.0
4 # Library of helpers for test scripts.
5 set -e
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.
13 ksft_skip=4
15 print_reqs_exit()
17 echo "You must have the following enabled in your kernel:" >&2
18 cat $TEST_DIR/config >&2
19 exit $ksft_skip
22 test_modprobe()
24 if [ ! -d $DIR ]; then
25 print_reqs_exit
29 check_mods()
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
43 else
44 echo "Failed to load configs module, using old heuristics" >&2
49 check_setup()
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)"
89 verify_reqs()
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"
94 exit $ksft_skip
99 setup_tmp_file()
101 FWPATH=$(mktemp -d)
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
116 sync
117 else
118 echo "ABCD0123" >"$RANDOM_FILE_PATH"
120 echo $RANDOM_FILE_PATH
123 setup_random_file()
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
137 check_setup
141 proc_set_ignore_sysfs_fallback()
143 if [ -f $FW_IGNORE_SYSFS_FALLBACK ]; then
144 echo -n $1 > $FW_IGNORE_SYSFS_FALLBACK
145 check_setup
149 proc_restore_defaults()
151 proc_set_force_sysfs_fallback 0
152 proc_set_ignore_sysfs_fallback 0
155 test_finish()
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
164 else
165 echo -n "$OLD_FWPATH" >/sys/module/firmware_class/parameters/path
168 if [ -f $FW ]; then
169 rm -f "$FW"
171 if [ -d $FWPATH ]; then
172 rm -rf "$FWPATH"
174 proc_restore_defaults
177 kconfig_has()
179 if [ -f $PROC_CONFIG ]; then
180 if zgrep -q $1 $PROC_CONFIG 2>/dev/null; then
181 echo "yes"
182 else
183 echo "no"
185 else
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
190 echo "yes"
191 elif [ "$1" = "CONFIG_FW_LOADER_USER_HELPER=y" ]; then
192 if [ -d /sys/class/firmware/ ]; then
193 echo yes
194 else
195 echo no