summaryrefslogtreecommitdiffstats
path: root/src/initramfs/tpm/bin/detect-tpm.sh
blob: 48bc92f74a1e52a34a7b4d730ba1fffa88b23bda (plain) (blame)
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
#!/bin/sh
#

SYS_PATH="/sys/class/misc/tpm0/device/"
MODULES="atmel tis nsc infineon"
MODULES_FORCE="tis"
FLAGS=""
FLAGS_FORCE="force=1"

test_tpm() {
	if [ ! -d "$SYS_PATH" ] ; then
		return 1
	fi

# tpm_tis creates "active" and "enabled" files
# _atmel and _nsc only create the "caps"
	ACTIVE="$(cat $SYS_PATH/active 2>/dev/null)"
	ENABLED="$(cat $SYS_PATH/enabled 2>/dev/null)"
	CAPS="$(cat $SYS_PATH/caps 2>/dev/null)"
	if [ -n "$ACTIVE" -o -n "$ENABLED" -o -n "$CAPS" ] ; then
		echo
		echo "successfully detected TPM chip!"
		echo
		echo "$CAPS"
		echo
	else
		return 2
	fi
}

try_modules() {
	if [ "$1" == "force" ] ; then
		MODULES=$MODULES_FORCE
		FLAGS=$FLAGS_FORCE
		echo "using flags '$FLAGS'"
	fi
	echo -n "trying modules:"
	for module in $MODULES ; do
		echo -n " $module"
		modprobe tpm_${module} $FLAGS 2>/dev/null
		if test_tpm ; then
			return 0
		fi
		# clean up since e.g. infineon always loads w/o error...
		modprobe -r tpm_${module} 2>/dev/null
	done
	echo
	return 1
}

# create device-node
test -c /dev/tpm0 || mknod /dev/tpm0 c 10 224

if try_modules ; then
	exit 0  # success
fi
if ! try_modules force ; then
	echo "Warning: no TPM chip found!"
	exit 1
fi