129a1c00cSLuis R. Rodriguez#!/bin/bash
229a1c00cSLuis R. Rodriguez# SPDX-License-Identifier: GPL-2.0
329a1c00cSLuis R. Rodriguez
429a1c00cSLuis R. Rodriguez# Library of helpers for test scripts.
529a1c00cSLuis R. Rodriguezset -e
629a1c00cSLuis R. Rodriguez
729a1c00cSLuis R. RodriguezDIR=/sys/devices/virtual/misc/test_firmware
829a1c00cSLuis R. Rodriguez
929a1c00cSLuis R. RodriguezPROC_CONFIG="/proc/config.gz"
1029a1c00cSLuis R. RodriguezTEST_DIR=$(dirname $0)
1129a1c00cSLuis R. Rodriguez
12a6a9be92SShuah Khan (Samsung OSG)# Kselftest framework requirement - SKIP code is 4.
13a6a9be92SShuah Khan (Samsung OSG)ksft_skip=4
14a6a9be92SShuah Khan (Samsung OSG)
1529a1c00cSLuis R. Rodriguezprint_reqs_exit()
1629a1c00cSLuis R. Rodriguez{
1729a1c00cSLuis R. Rodriguez	echo "You must have the following enabled in your kernel:" >&2
1829a1c00cSLuis R. Rodriguez	cat $TEST_DIR/config >&2
19a6a9be92SShuah Khan (Samsung OSG)	exit $ksft_skip
2029a1c00cSLuis R. Rodriguez}
2129a1c00cSLuis R. Rodriguez
2229a1c00cSLuis R. Rodrigueztest_modprobe()
2329a1c00cSLuis R. Rodriguez{
2429a1c00cSLuis R. Rodriguez	if [ ! -d $DIR ]; then
2529a1c00cSLuis R. Rodriguez		print_reqs_exit
2629a1c00cSLuis R. Rodriguez	fi
2729a1c00cSLuis R. Rodriguez}
2829a1c00cSLuis R. Rodriguez
2929a1c00cSLuis R. Rodriguezcheck_mods()
3029a1c00cSLuis R. Rodriguez{
3129a1c00cSLuis R. Rodriguez	trap "test_modprobe" EXIT
3229a1c00cSLuis R. Rodriguez	if [ ! -d $DIR ]; then
3329a1c00cSLuis R. Rodriguez		modprobe test_firmware
3429a1c00cSLuis R. Rodriguez	fi
3529a1c00cSLuis R. Rodriguez	if [ ! -f $PROC_CONFIG ]; then
3629a1c00cSLuis R. Rodriguez		if modprobe configs 2>/dev/null; then
3729a1c00cSLuis R. Rodriguez			echo "Loaded configs module"
3829a1c00cSLuis R. Rodriguez			if [ ! -f $PROC_CONFIG ]; then
3929a1c00cSLuis R. Rodriguez				echo "You must have the following enabled in your kernel:" >&2
4029a1c00cSLuis R. Rodriguez				cat $TEST_DIR/config >&2
4129a1c00cSLuis R. Rodriguez				echo "Resorting to old heuristics" >&2
4229a1c00cSLuis R. Rodriguez			fi
4329a1c00cSLuis R. Rodriguez		else
4429a1c00cSLuis R. Rodriguez			echo "Failed to load configs module, using old heuristics" >&2
4529a1c00cSLuis R. Rodriguez		fi
4629a1c00cSLuis R. Rodriguez	fi
4729a1c00cSLuis R. Rodriguez}
48ef557787SLuis R. Rodriguez
49f5a61451SLuis R. Rodriguezcheck_setup()
50f5a61451SLuis R. Rodriguez{
51f5a61451SLuis R. Rodriguez	HAS_FW_LOADER_USER_HELPER="$(kconfig_has CONFIG_FW_LOADER_USER_HELPER=y)"
52f5a61451SLuis R. Rodriguez	HAS_FW_LOADER_USER_HELPER_FALLBACK="$(kconfig_has CONFIG_FW_LOADER_USER_HELPER_FALLBACK=y)"
53108ae07cSTakashi Iwai	HAS_FW_LOADER_COMPRESS="$(kconfig_has CONFIG_FW_LOADER_COMPRESS=y)"
54b3cf21faSLuis R. Rodriguez	PROC_FW_IGNORE_SYSFS_FALLBACK="0"
55b3cf21faSLuis R. Rodriguez	PROC_FW_FORCE_SYSFS_FALLBACK="0"
56b3cf21faSLuis R. Rodriguez
57b3cf21faSLuis R. Rodriguez	if [ -z $PROC_SYS_DIR ]; then
58b3cf21faSLuis R. Rodriguez		PROC_SYS_DIR="/proc/sys/kernel"
59b3cf21faSLuis R. Rodriguez	fi
60b3cf21faSLuis R. Rodriguez
61b3cf21faSLuis R. Rodriguez	FW_PROC="${PROC_SYS_DIR}/firmware_config"
62b3cf21faSLuis R. Rodriguez	FW_FORCE_SYSFS_FALLBACK="$FW_PROC/force_sysfs_fallback"
63b3cf21faSLuis R. Rodriguez	FW_IGNORE_SYSFS_FALLBACK="$FW_PROC/ignore_sysfs_fallback"
64b3cf21faSLuis R. Rodriguez
65b3cf21faSLuis R. Rodriguez	if [ -f $FW_FORCE_SYSFS_FALLBACK ]; then
66b3cf21faSLuis R. Rodriguez		PROC_FW_FORCE_SYSFS_FALLBACK="$(cat $FW_FORCE_SYSFS_FALLBACK)"
67b3cf21faSLuis R. Rodriguez	fi
68b3cf21faSLuis R. Rodriguez
69b3cf21faSLuis R. Rodriguez	if [ -f $FW_IGNORE_SYSFS_FALLBACK ]; then
70b3cf21faSLuis R. Rodriguez		PROC_FW_IGNORE_SYSFS_FALLBACK="$(cat $FW_IGNORE_SYSFS_FALLBACK)"
71b3cf21faSLuis R. Rodriguez	fi
72b3cf21faSLuis R. Rodriguez
73b3cf21faSLuis R. Rodriguez	if [ "$PROC_FW_FORCE_SYSFS_FALLBACK" = "1" ]; then
74b3cf21faSLuis R. Rodriguez		HAS_FW_LOADER_USER_HELPER="yes"
75b3cf21faSLuis R. Rodriguez		HAS_FW_LOADER_USER_HELPER_FALLBACK="yes"
76b3cf21faSLuis R. Rodriguez	fi
77b3cf21faSLuis R. Rodriguez
78b3cf21faSLuis R. Rodriguez	if [ "$PROC_FW_IGNORE_SYSFS_FALLBACK" = "1" ]; then
79b3cf21faSLuis R. Rodriguez		HAS_FW_LOADER_USER_HELPER_FALLBACK="no"
80b3cf21faSLuis R. Rodriguez		HAS_FW_LOADER_USER_HELPER="no"
81b3cf21faSLuis R. Rodriguez	fi
82f5a61451SLuis R. Rodriguez
83f5a61451SLuis R. Rodriguez	if [ "$HAS_FW_LOADER_USER_HELPER" = "yes" ]; then
84f5a61451SLuis R. Rodriguez	       OLD_TIMEOUT="$(cat /sys/class/firmware/timeout)"
85f5a61451SLuis R. Rodriguez	fi
86f5a61451SLuis R. Rodriguez
87f5a61451SLuis R. Rodriguez	OLD_FWPATH="$(cat /sys/module/firmware_class/parameters/path)"
88108ae07cSTakashi Iwai
89108ae07cSTakashi Iwai	if [ "$HAS_FW_LOADER_COMPRESS" = "yes" ]; then
90108ae07cSTakashi Iwai		if ! which xz 2> /dev/null > /dev/null; then
91108ae07cSTakashi Iwai			HAS_FW_LOADER_COMPRESS=""
92108ae07cSTakashi Iwai		fi
93108ae07cSTakashi Iwai	fi
94f5a61451SLuis R. Rodriguez}
95f5a61451SLuis R. Rodriguez
96f5a61451SLuis R. Rodriguezverify_reqs()
97f5a61451SLuis R. Rodriguez{
98f5a61451SLuis R. Rodriguez	if [ "$TEST_REQS_FW_SYSFS_FALLBACK" = "yes" ]; then
99f5a61451SLuis R. Rodriguez		if [ ! "$HAS_FW_LOADER_USER_HELPER" = "yes" ]; then
100f5a61451SLuis R. Rodriguez			echo "usermode helper disabled so ignoring test"
101344c0152SLuis Chamberlain			exit 0
102f5a61451SLuis R. Rodriguez		fi
103f5a61451SLuis R. Rodriguez	fi
104f5a61451SLuis R. Rodriguez}
105f5a61451SLuis R. Rodriguez
106f5a61451SLuis R. Rodriguezsetup_tmp_file()
107f5a61451SLuis R. Rodriguez{
108f5a61451SLuis R. Rodriguez	FWPATH=$(mktemp -d)
109f5a61451SLuis R. Rodriguez	FW="$FWPATH/test-firmware.bin"
110f5a61451SLuis R. Rodriguez	echo "ABCD0123" >"$FW"
111f5a61451SLuis R. Rodriguez	NAME=$(basename "$FW")
112f5a61451SLuis R. Rodriguez	if [ "$TEST_REQS_FW_SET_CUSTOM_PATH" = "yes" ]; then
113f5a61451SLuis R. Rodriguez		echo -n "$FWPATH" >/sys/module/firmware_class/parameters/path
114f5a61451SLuis R. Rodriguez	fi
115f5a61451SLuis R. Rodriguez}
116f5a61451SLuis R. Rodriguez
1179952db75SLuis R. Rodriguez__setup_random_file()
1189952db75SLuis R. Rodriguez{
1199952db75SLuis R. Rodriguez	RANDOM_FILE_PATH="$(mktemp -p $FWPATH)"
1209952db75SLuis R. Rodriguez	# mktemp says dry-run -n is unsafe, so...
1219952db75SLuis R. Rodriguez	if [[ "$1" = "fake" ]]; then
1229952db75SLuis R. Rodriguez		rm -rf $RANDOM_FILE_PATH
1239952db75SLuis R. Rodriguez		sync
1249952db75SLuis R. Rodriguez	else
1259952db75SLuis R. Rodriguez		echo "ABCD0123" >"$RANDOM_FILE_PATH"
1269952db75SLuis R. Rodriguez	fi
1279952db75SLuis R. Rodriguez	echo $RANDOM_FILE_PATH
1289952db75SLuis R. Rodriguez}
1299952db75SLuis R. Rodriguez
1309952db75SLuis R. Rodriguezsetup_random_file()
1319952db75SLuis R. Rodriguez{
1329952db75SLuis R. Rodriguez	echo $(__setup_random_file)
1339952db75SLuis R. Rodriguez}
1349952db75SLuis R. Rodriguez
1359952db75SLuis R. Rodriguezsetup_random_file_fake()
1369952db75SLuis R. Rodriguez{
1379952db75SLuis R. Rodriguez	echo $(__setup_random_file fake)
1389952db75SLuis R. Rodriguez}
1399952db75SLuis R. Rodriguez
140b3cf21faSLuis R. Rodriguezproc_set_force_sysfs_fallback()
141b3cf21faSLuis R. Rodriguez{
142b3cf21faSLuis R. Rodriguez	if [ -f $FW_FORCE_SYSFS_FALLBACK ]; then
143b3cf21faSLuis R. Rodriguez		echo -n $1 > $FW_FORCE_SYSFS_FALLBACK
144b3cf21faSLuis R. Rodriguez		check_setup
145b3cf21faSLuis R. Rodriguez	fi
146b3cf21faSLuis R. Rodriguez}
147b3cf21faSLuis R. Rodriguez
148b3cf21faSLuis R. Rodriguezproc_set_ignore_sysfs_fallback()
149b3cf21faSLuis R. Rodriguez{
150b3cf21faSLuis R. Rodriguez	if [ -f $FW_IGNORE_SYSFS_FALLBACK ]; then
151b3cf21faSLuis R. Rodriguez		echo -n $1 > $FW_IGNORE_SYSFS_FALLBACK
152b3cf21faSLuis R. Rodriguez		check_setup
153b3cf21faSLuis R. Rodriguez	fi
154b3cf21faSLuis R. Rodriguez}
155b3cf21faSLuis R. Rodriguez
156b3cf21faSLuis R. Rodriguezproc_restore_defaults()
157b3cf21faSLuis R. Rodriguez{
158b3cf21faSLuis R. Rodriguez	proc_set_force_sysfs_fallback 0
159b3cf21faSLuis R. Rodriguez	proc_set_ignore_sysfs_fallback 0
160b3cf21faSLuis R. Rodriguez}
161b3cf21faSLuis R. Rodriguez
162f5a61451SLuis R. Rodrigueztest_finish()
163f5a61451SLuis R. Rodriguez{
164f5a61451SLuis R. Rodriguez	if [ "$HAS_FW_LOADER_USER_HELPER" = "yes" ]; then
165f5a61451SLuis R. Rodriguez		echo "$OLD_TIMEOUT" >/sys/class/firmware/timeout
166f5a61451SLuis R. Rodriguez	fi
167f5a61451SLuis R. Rodriguez	if [ "$TEST_REQS_FW_SET_CUSTOM_PATH" = "yes" ]; then
168e5384092SBen Hutchings		if [ "$OLD_FWPATH" = "" ]; then
169e5384092SBen Hutchings			# A zero-length write won't work; write a null byte
170e5384092SBen Hutchings			printf '\000' >/sys/module/firmware_class/parameters/path
171e5384092SBen Hutchings		else
172f5a61451SLuis R. Rodriguez			echo -n "$OLD_FWPATH" >/sys/module/firmware_class/parameters/path
173f5a61451SLuis R. Rodriguez		fi
174e5384092SBen Hutchings	fi
175f5a61451SLuis R. Rodriguez	if [ -f $FW ]; then
176f5a61451SLuis R. Rodriguez		rm -f "$FW"
177f5a61451SLuis R. Rodriguez	fi
178f5a61451SLuis R. Rodriguez	if [ -d $FWPATH ]; then
179f5a61451SLuis R. Rodriguez		rm -rf "$FWPATH"
180f5a61451SLuis R. Rodriguez	fi
181b3cf21faSLuis R. Rodriguez	proc_restore_defaults
182f5a61451SLuis R. Rodriguez}
183f5a61451SLuis R. Rodriguez
184ef557787SLuis R. Rodriguezkconfig_has()
185ef557787SLuis R. Rodriguez{
186ef557787SLuis R. Rodriguez	if [ -f $PROC_CONFIG ]; then
187ef557787SLuis R. Rodriguez		if zgrep -q $1 $PROC_CONFIG 2>/dev/null; then
188ef557787SLuis R. Rodriguez			echo "yes"
189ef557787SLuis R. Rodriguez		else
190ef557787SLuis R. Rodriguez			echo "no"
191ef557787SLuis R. Rodriguez		fi
192ef557787SLuis R. Rodriguez	else
193ef557787SLuis R. Rodriguez		# We currently don't have easy heuristics to infer this
194ef557787SLuis R. Rodriguez		# so best we can do is just try to use the kernel assuming
195ef557787SLuis R. Rodriguez		# you had enabled it. This matches the old behaviour.
196ef557787SLuis R. Rodriguez		if [ "$1" = "CONFIG_FW_LOADER_USER_HELPER_FALLBACK=y" ]; then
197ef557787SLuis R. Rodriguez			echo "yes"
198ef557787SLuis R. Rodriguez		elif [ "$1" = "CONFIG_FW_LOADER_USER_HELPER=y" ]; then
199ef557787SLuis R. Rodriguez			if [ -d /sys/class/firmware/ ]; then
200ef557787SLuis R. Rodriguez				echo yes
201ef557787SLuis R. Rodriguez			else
202ef557787SLuis R. Rodriguez				echo no
203ef557787SLuis R. Rodriguez			fi
204ef557787SLuis R. Rodriguez		fi
205ef557787SLuis R. Rodriguez	fi
206ef557787SLuis R. Rodriguez}
207