1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright 2022 Google LLC
10 #include <test/test.h>
14 * get_tpm_version() - Get a TPM of the given version
16 * @version: Version to get
17 * @devp: Returns the TPM device
18 * Returns: 0 if OK, -ENODEV if not found
20 static int get_tpm_version(enum tpm_version version, struct udevice **devp)
25 * For now we have to probe each TPM, since the version is set up in
26 * of_to_plat(). We could require TPMs to declare their version when
27 * probed, to avoid this
29 uclass_foreach_dev_probe(UCLASS_TPM, dev) {
30 if (tpm_get_version(dev) == version) {
39 /* Basic test of initing a TPM */
40 static int test_tpm_init(struct unit_test_state *uts, enum tpm_version version)
44 /* check probe success */
45 ut_assertok(get_tpm_version(version, &dev));
47 ut_assertok(tpm_init(dev));
52 static int dm_test_tpm(struct unit_test_state *uts)
54 ut_assertok(test_tpm_init(uts, TPM_V1));
55 ut_assertok(test_tpm_init(uts, TPM_V2));
59 DM_TEST(dm_test_tpm, UTF_SCAN_FDT);
61 /* Test report_state */
62 static int dm_test_tpm_report_state(struct unit_test_state *uts)
67 /* check probe success */
68 ut_assertok(get_tpm_version(TPM_V2, &dev));
70 ut_assert(tpm_report_state(dev, buf, sizeof(buf)));
71 ut_asserteq_str("init_done=0", buf);
73 ut_assertok(tpm_auto_start(dev));
75 ut_assert(tpm_report_state(dev, buf, sizeof(buf)));
76 ut_asserteq_str("init_done=1", buf);
80 DM_TEST(dm_test_tpm_report_state, UTF_SCAN_FDT);
83 * test_tpm_autostart() - check the tpm_auto_start() call
85 * @uts: Unit test state
86 * @version: TPM version to use
87 * @reinit: true to call tpm_init() first
88 * Returns 0 if OK, non-zero on failure
90 static int test_tpm_autostart(struct unit_test_state *uts,
91 enum tpm_version version, bool reinit)
95 /* check probe success */
96 ut_assertok(get_tpm_version(version, &dev));
99 ut_assertok(tpm_init(dev));
102 * tpm_auto_start will rerun tpm_init() if reinit, but handles the
103 * -EBUSY return code internally.
105 ut_assertok(tpm_auto_start(dev));
110 static int dm_test_tpm_autostart(struct unit_test_state *uts)
112 ut_assertok(test_tpm_autostart(uts, TPM_V1, false));
113 ut_assertok(test_tpm_autostart(uts, TPM_V2, false));
117 DM_TEST(dm_test_tpm_autostart, UTF_SCAN_FDT);
119 static int dm_test_tpm_autostart_reinit(struct unit_test_state *uts)
121 ut_assertok(test_tpm_autostart(uts, TPM_V1, true));
122 ut_assertok(test_tpm_autostart(uts, TPM_V2, true));
126 DM_TEST(dm_test_tpm_autostart_reinit, UTF_SCAN_FDT);