2018-03-10 06:14:42 -08:00
#!/bin/bash
# SPDX-License-Identifier: GPL-2.0
# Library of helpers for test scripts.
set -e
DIR = /sys/devices/virtual/misc/test_firmware
PROC_CONFIG = "/proc/config.gz"
TEST_DIR = $( dirname $0 )
2019-08-22 11:40:05 -07:00
# We need to load a different file to test request_firmware_into_buf
# I believe the issue is firmware loaded cached vs. non-cached
# with same filename is bungled.
# To reproduce rename this to test-firmware.bin
TEST_FIRMWARE_INTO_BUF_FILENAME = test-firmware-into-buf.bin
2018-05-03 16:34:22 -06:00
# Kselftest framework requirement - SKIP code is 4.
ksft_skip = 4
2018-03-10 06:14:42 -08:00
print_reqs_exit( )
{
echo "You must have the following enabled in your kernel:" >& 2
cat $TEST_DIR /config >& 2
2018-05-03 16:34:22 -06:00
exit $ksft_skip
2018-03-10 06:14:42 -08:00
}
test_modprobe( )
{
if [ ! -d $DIR ] ; then
print_reqs_exit
fi
}
check_mods( )
{
2019-12-12 18:56:06 -07:00
local uid = $( id -u)
if [ $uid -ne 0 ] ; then
echo "skip all tests: must be run as root" >& 2
exit $ksft_skip
fi
2018-03-10 06:14:42 -08:00
trap "test_modprobe" EXIT
if [ ! -d $DIR ] ; then
modprobe test_firmware
fi
if [ ! -f $PROC_CONFIG ] ; then
if modprobe configs 2>/dev/null; then
echo "Loaded configs module"
if [ ! -f $PROC_CONFIG ] ; then
echo "You must have the following enabled in your kernel:" >& 2
cat $TEST_DIR /config >& 2
echo "Resorting to old heuristics" >& 2
fi
else
echo "Failed to load configs module, using old heuristics" >& 2
fi
fi
}
2018-03-10 06:14:43 -08:00
2018-03-10 06:14:53 -08:00
check_setup( )
{
HAS_FW_LOADER_USER_HELPER = " $( kconfig_has CONFIG_FW_LOADER_USER_HELPER = y) "
HAS_FW_LOADER_USER_HELPER_FALLBACK = " $( kconfig_has CONFIG_FW_LOADER_USER_HELPER_FALLBACK = y) "
2019-06-11 14:26:26 +02:00
HAS_FW_LOADER_COMPRESS = " $( kconfig_has CONFIG_FW_LOADER_COMPRESS = y) "
2018-03-10 06:14:54 -08:00
PROC_FW_IGNORE_SYSFS_FALLBACK = "0"
PROC_FW_FORCE_SYSFS_FALLBACK = "0"
if [ -z $PROC_SYS_DIR ] ; then
PROC_SYS_DIR = "/proc/sys/kernel"
fi
FW_PROC = " ${ PROC_SYS_DIR } /firmware_config "
FW_FORCE_SYSFS_FALLBACK = " $FW_PROC /force_sysfs_fallback "
FW_IGNORE_SYSFS_FALLBACK = " $FW_PROC /ignore_sysfs_fallback "
if [ -f $FW_FORCE_SYSFS_FALLBACK ] ; then
PROC_FW_FORCE_SYSFS_FALLBACK = " $( cat $FW_FORCE_SYSFS_FALLBACK ) "
fi
if [ -f $FW_IGNORE_SYSFS_FALLBACK ] ; then
PROC_FW_IGNORE_SYSFS_FALLBACK = " $( cat $FW_IGNORE_SYSFS_FALLBACK ) "
fi
if [ " $PROC_FW_FORCE_SYSFS_FALLBACK " = "1" ] ; then
HAS_FW_LOADER_USER_HELPER = "yes"
HAS_FW_LOADER_USER_HELPER_FALLBACK = "yes"
fi
if [ " $PROC_FW_IGNORE_SYSFS_FALLBACK " = "1" ] ; then
HAS_FW_LOADER_USER_HELPER_FALLBACK = "no"
HAS_FW_LOADER_USER_HELPER = "no"
fi
2018-03-10 06:14:53 -08:00
if [ " $HAS_FW_LOADER_USER_HELPER " = "yes" ] ; then
OLD_TIMEOUT = " $( cat /sys/class/firmware/timeout) "
fi
OLD_FWPATH = " $( cat /sys/module/firmware_class/parameters/path) "
2019-06-11 14:26:26 +02:00
if [ " $HAS_FW_LOADER_COMPRESS " = "yes" ] ; then
if ! which xz 2> /dev/null > /dev/null; then
HAS_FW_LOADER_COMPRESS = ""
fi
fi
2018-03-10 06:14:53 -08:00
}
verify_reqs( )
{
if [ " $TEST_REQS_FW_SYSFS_FALLBACK " = "yes" ] ; then
if [ ! " $HAS_FW_LOADER_USER_HELPER " = "yes" ] ; then
echo "usermode helper disabled so ignoring test"
2019-02-07 11:06:02 -08:00
exit 0
2018-03-10 06:14:53 -08:00
fi
fi
}
setup_tmp_file( )
{
FWPATH = $( mktemp -d)
FW = " $FWPATH /test-firmware.bin "
echo "ABCD0123" >" $FW "
2019-08-22 11:40:05 -07:00
FW_INTO_BUF = " $FWPATH / $TEST_FIRMWARE_INTO_BUF_FILENAME "
echo "EFGH4567" >" $FW_INTO_BUF "
2018-03-10 06:14:53 -08:00
NAME = $( basename " $FW " )
if [ " $TEST_REQS_FW_SET_CUSTOM_PATH " = "yes" ] ; then
echo -n " $FWPATH " >/sys/module/firmware_class/parameters/path
fi
}
2018-03-10 06:14:58 -08:00
__setup_random_file( )
{
RANDOM_FILE_PATH = " $( mktemp -p $FWPATH ) "
# mktemp says dry-run -n is unsafe, so...
if [ [ " $1 " = "fake" ] ] ; then
rm -rf $RANDOM_FILE_PATH
sync
else
echo "ABCD0123" >" $RANDOM_FILE_PATH "
fi
echo $RANDOM_FILE_PATH
}
setup_random_file( )
{
echo $( __setup_random_file)
}
setup_random_file_fake( )
{
echo $( __setup_random_file fake)
}
2018-03-10 06:14:54 -08:00
proc_set_force_sysfs_fallback( )
{
if [ -f $FW_FORCE_SYSFS_FALLBACK ] ; then
echo -n $1 > $FW_FORCE_SYSFS_FALLBACK
check_setup
fi
}
proc_set_ignore_sysfs_fallback( )
{
if [ -f $FW_IGNORE_SYSFS_FALLBACK ] ; then
echo -n $1 > $FW_IGNORE_SYSFS_FALLBACK
check_setup
fi
}
proc_restore_defaults( )
{
proc_set_force_sysfs_fallback 0
proc_set_ignore_sysfs_fallback 0
}
2018-03-10 06:14:53 -08:00
test_finish( )
{
if [ " $HAS_FW_LOADER_USER_HELPER " = "yes" ] ; then
echo " $OLD_TIMEOUT " >/sys/class/firmware/timeout
fi
if [ " $TEST_REQS_FW_SET_CUSTOM_PATH " = "yes" ] ; then
2018-04-04 22:38:49 +02:00
if [ " $OLD_FWPATH " = "" ] ; then
# A zero-length write won't work; write a null byte
printf '\000' >/sys/module/firmware_class/parameters/path
else
echo -n " $OLD_FWPATH " >/sys/module/firmware_class/parameters/path
fi
2018-03-10 06:14:53 -08:00
fi
if [ -f $FW ] ; then
rm -f " $FW "
fi
2019-08-22 11:40:05 -07:00
if [ -f $FW_INTO_BUF ] ; then
rm -f " $FW_INTO_BUF "
fi
2018-03-10 06:14:53 -08:00
if [ -d $FWPATH ] ; then
rm -rf " $FWPATH "
fi
2018-03-10 06:14:54 -08:00
proc_restore_defaults
2018-03-10 06:14:53 -08:00
}
2018-03-10 06:14:43 -08:00
kconfig_has( )
{
if [ -f $PROC_CONFIG ] ; then
if zgrep -q $1 $PROC_CONFIG 2>/dev/null; then
echo "yes"
else
echo "no"
fi
else
# We currently don't have easy heuristics to infer this
# so best we can do is just try to use the kernel assuming
# you had enabled it. This matches the old behaviour.
if [ " $1 " = "CONFIG_FW_LOADER_USER_HELPER_FALLBACK=y" ] ; then
echo "yes"
elif [ " $1 " = "CONFIG_FW_LOADER_USER_HELPER=y" ] ; then
if [ -d /sys/class/firmware/ ] ; then
echo yes
else
echo no
fi
fi
fi
}