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