WIP FPC-III support
[linux/fpc-iii.git] / tools / testing / selftests / firmware / fw_lib.sh
blob5b8c0fedee7613b77456409517bcdea1b76707c3
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 # 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.
19 ksft_skip=4
21 print_reqs_exit()
23 echo "You must have the following enabled in your kernel:" >&2
24 cat $TEST_DIR/config >&2
25 exit $ksft_skip
28 test_modprobe()
30 if [ ! -d $DIR ]; then
31 print_reqs_exit
35 check_mods()
37 local uid=$(id -u)
38 if [ $uid -ne 0 ]; then
39 echo "skip all tests: must be run as root" >&2
40 exit $ksft_skip
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
55 else
56 echo "Failed to load configs module, using old heuristics" >&2
61 check_setup()
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="$(kconfig_has CONFIG_FW_LOADER_COMPRESS=y)"
66 PROC_FW_IGNORE_SYSFS_FALLBACK="0"
67 PROC_FW_FORCE_SYSFS_FALLBACK="0"
69 if [ -z $PROC_SYS_DIR ]; then
70 PROC_SYS_DIR="/proc/sys/kernel"
73 FW_PROC="${PROC_SYS_DIR}/firmware_config"
74 FW_FORCE_SYSFS_FALLBACK="$FW_PROC/force_sysfs_fallback"
75 FW_IGNORE_SYSFS_FALLBACK="$FW_PROC/ignore_sysfs_fallback"
77 if [ -f $FW_FORCE_SYSFS_FALLBACK ]; then
78 PROC_FW_FORCE_SYSFS_FALLBACK="$(cat $FW_FORCE_SYSFS_FALLBACK)"
81 if [ -f $FW_IGNORE_SYSFS_FALLBACK ]; then
82 PROC_FW_IGNORE_SYSFS_FALLBACK="$(cat $FW_IGNORE_SYSFS_FALLBACK)"
85 if [ "$PROC_FW_FORCE_SYSFS_FALLBACK" = "1" ]; then
86 HAS_FW_LOADER_USER_HELPER="yes"
87 HAS_FW_LOADER_USER_HELPER_FALLBACK="yes"
90 if [ "$PROC_FW_IGNORE_SYSFS_FALLBACK" = "1" ]; then
91 HAS_FW_LOADER_USER_HELPER_FALLBACK="no"
92 HAS_FW_LOADER_USER_HELPER="no"
95 if [ "$HAS_FW_LOADER_USER_HELPER" = "yes" ]; then
96 OLD_TIMEOUT="$(cat /sys/class/firmware/timeout)"
99 OLD_FWPATH="$(cat /sys/module/firmware_class/parameters/path)"
101 if [ "$HAS_FW_LOADER_COMPRESS" = "yes" ]; then
102 if ! which xz 2> /dev/null > /dev/null; then
103 HAS_FW_LOADER_COMPRESS=""
108 verify_reqs()
110 if [ "$TEST_REQS_FW_SYSFS_FALLBACK" = "yes" ]; then
111 if [ ! "$HAS_FW_LOADER_USER_HELPER" = "yes" ]; then
112 echo "usermode helper disabled so ignoring test"
113 exit 0
118 setup_tmp_file()
120 FWPATH=$(mktemp -d)
121 FW="$FWPATH/test-firmware.bin"
122 echo "ABCD0123" >"$FW"
123 FW_INTO_BUF="$FWPATH/$TEST_FIRMWARE_INTO_BUF_FILENAME"
124 echo "EFGH4567" >"$FW_INTO_BUF"
125 NAME=$(basename "$FW")
126 if [ "$TEST_REQS_FW_SET_CUSTOM_PATH" = "yes" ]; then
127 echo -n "$FWPATH" >/sys/module/firmware_class/parameters/path
131 __setup_random_file()
133 RANDOM_FILE_PATH="$(mktemp -p $FWPATH)"
134 # mktemp says dry-run -n is unsafe, so...
135 if [[ "$1" = "fake" ]]; then
136 rm -rf $RANDOM_FILE_PATH
137 sync
138 else
139 echo "ABCD0123" >"$RANDOM_FILE_PATH"
141 echo $RANDOM_FILE_PATH
144 setup_random_file()
146 echo $(__setup_random_file)
149 setup_random_file_fake()
151 echo $(__setup_random_file fake)
154 proc_set_force_sysfs_fallback()
156 if [ -f $FW_FORCE_SYSFS_FALLBACK ]; then
157 echo -n $1 > $FW_FORCE_SYSFS_FALLBACK
158 check_setup
162 proc_set_ignore_sysfs_fallback()
164 if [ -f $FW_IGNORE_SYSFS_FALLBACK ]; then
165 echo -n $1 > $FW_IGNORE_SYSFS_FALLBACK
166 check_setup
170 proc_restore_defaults()
172 proc_set_force_sysfs_fallback 0
173 proc_set_ignore_sysfs_fallback 0
176 test_finish()
178 if [ "$HAS_FW_LOADER_USER_HELPER" = "yes" ]; then
179 echo "$OLD_TIMEOUT" >/sys/class/firmware/timeout
181 if [ "$TEST_REQS_FW_SET_CUSTOM_PATH" = "yes" ]; then
182 if [ "$OLD_FWPATH" = "" ]; then
183 # A zero-length write won't work; write a null byte
184 printf '\000' >/sys/module/firmware_class/parameters/path
185 else
186 echo -n "$OLD_FWPATH" >/sys/module/firmware_class/parameters/path
189 if [ -f $FW ]; then
190 rm -f "$FW"
192 if [ -f $FW_INTO_BUF ]; then
193 rm -f "$FW_INTO_BUF"
195 if [ -d $FWPATH ]; then
196 rm -rf "$FWPATH"
198 proc_restore_defaults
201 kconfig_has()
203 if [ -f $PROC_CONFIG ]; then
204 if zgrep -q $1 $PROC_CONFIG 2>/dev/null; then
205 echo "yes"
206 else
207 echo "no"
209 else
210 # We currently don't have easy heuristics to infer this
211 # so best we can do is just try to use the kernel assuming
212 # you had enabled it. This matches the old behaviour.
213 if [ "$1" = "CONFIG_FW_LOADER_USER_HELPER_FALLBACK=y" ]; then
214 echo "yes"
215 elif [ "$1" = "CONFIG_FW_LOADER_USER_HELPER=y" ]; then
216 if [ -d /sys/class/firmware/ ]; then
217 echo yes
218 else
219 echo no