aboutsummaryrefslogtreecommitdiffstats
path: root/tools/testing/selftests/firmware/fw_lib.sh
blob: 8a853ace55a24e3710731d3af1ed9e74f0a492b5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
#!/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)

# Kselftest framework requirement - SKIP code is 4.
ksft_skip=4

print_reqs_exit()
{
	echo "You must have the following enabled in your kernel:" >&2
	cat $TEST_DIR/config >&2
	exit $ksft_skip
}

test_modprobe()
{
	if [ ! -d $DIR ]; then
		print_reqs_exit
	fi
}

check_mods()
{
	local uid=$(id -u)
	if [ $uid -ne 0 ]; then
		echo "skip all tests: must be run as root" >&2
		exit $ksft_skip
	fi

	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
}

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)"
	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

	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)"
}

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"
			exit 0
		fi
	fi
}

setup_tmp_file()
{
	FWPATH=$(mktemp -d)
	FW="$FWPATH/test-firmware.bin"
	echo "ABCD0123" >"$FW"
	NAME=$(basename "$FW")
	if [ "$TEST_REQS_FW_SET_CUSTOM_PATH" = "yes" ]; then
		echo -n "$FWPATH" >/sys/module/firmware_class/parameters/path
	fi
}

__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)
}

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
}

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
		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
	fi
	if [ -f $FW ]; then
		rm -f "$FW"
	fi
	if [ -d $FWPATH ]; then
		rm -rf "$FWPATH"
	fi
	proc_restore_defaults
}

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
}