1#!/bin/bash
2# SPDX-License-Identifier: GPL-2.0
3
4# Library of helpers for test scripts.
5set -e
6
7DIR=/sys/devices/virtual/misc/test_firmware
8
9PROC_CONFIG="/proc/config.gz"
10TEST_DIR=$(dirname $0)
11
12print_reqs_exit()
13{
14	echo "You must have the following enabled in your kernel:" >&2
15	cat $TEST_DIR/config >&2
16	exit 1
17}
18
19test_modprobe()
20{
21	if [ ! -d $DIR ]; then
22		print_reqs_exit
23	fi
24}
25
26check_mods()
27{
28	trap "test_modprobe" EXIT
29	if [ ! -d $DIR ]; then
30		modprobe test_firmware
31	fi
32	if [ ! -f $PROC_CONFIG ]; then
33		if modprobe configs 2>/dev/null; then
34			echo "Loaded configs module"
35			if [ ! -f $PROC_CONFIG ]; then
36				echo "You must have the following enabled in your kernel:" >&2
37				cat $TEST_DIR/config >&2
38				echo "Resorting to old heuristics" >&2
39			fi
40		else
41			echo "Failed to load configs module, using old heuristics" >&2
42		fi
43	fi
44}
45
46kconfig_has()
47{
48	if [ -f $PROC_CONFIG ]; then
49		if zgrep -q $1 $PROC_CONFIG 2>/dev/null; then
50			echo "yes"
51		else
52			echo "no"
53		fi
54	else
55		# We currently don't have easy heuristics to infer this
56		# so best we can do is just try to use the kernel assuming
57		# you had enabled it. This matches the old behaviour.
58		if [ "$1" = "CONFIG_FW_LOADER_USER_HELPER_FALLBACK=y" ]; then
59			echo "yes"
60		elif [ "$1" = "CONFIG_FW_LOADER_USER_HELPER=y" ]; then
61			if [ -d /sys/class/firmware/ ]; then
62				echo yes
63			else
64				echo no
65			fi
66		fi
67	fi
68}
69