blob: 0e413c0eedd025db9a076d293fe01a757f16eef6 [file] [log] [blame]
Simon Glass11a075c2022-08-30 21:05:36 -06001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * Copyright 2022 Google LLC
4 * Written by Simon Glass <sjg@chromium.org>
5 */
6
Simon Glass11a075c2022-08-30 21:05:36 -06007#include <dm.h>
8#include <tpm_api.h>
9#include <dm/test.h>
10#include <test/test.h>
11#include <test/ut.h>
12
Simon Glasseef107e2023-02-21 06:24:51 -070013/*
14 * get_tpm_version() - Get a TPM of the given version
15 *
16 * @version: Version to get
17 * @devp: Returns the TPM device
18 * Returns: 0 if OK, -ENODEV if not found
19 */
20static int get_tpm_version(enum tpm_version version, struct udevice **devp)
21{
22 struct udevice *dev;
23
24 /*
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
28 */
29 uclass_foreach_dev_probe(UCLASS_TPM, dev) {
30 if (tpm_get_version(dev) == version) {
31 *devp = dev;
32 return 0;
33 }
34 }
35
36 return -ENODEV;
37}
38
39/* Basic test of initing a TPM */
40static int test_tpm_init(struct unit_test_state *uts, enum tpm_version version)
41{
42 struct udevice *dev;
43
44 /* check probe success */
45 ut_assertok(get_tpm_version(version, &dev));
46
47 ut_assertok(tpm_init(dev));
48
49 return 0;
50}
51
Simon Glass11a075c2022-08-30 21:05:36 -060052static int dm_test_tpm(struct unit_test_state *uts)
53{
Simon Glasseef107e2023-02-21 06:24:51 -070054 ut_assertok(test_tpm_init(uts, TPM_V1));
55 ut_assertok(test_tpm_init(uts, TPM_V2));
56
57 return 0;
58}
59DM_TEST(dm_test_tpm, UT_TESTF_SCAN_FDT);
60
61/* Test report_state */
62static int dm_test_tpm_report_state(struct unit_test_state *uts)
63{
Simon Glass11a075c2022-08-30 21:05:36 -060064 struct udevice *dev;
65 char buf[50];
66
67 /* check probe success */
Simon Glasseef107e2023-02-21 06:24:51 -070068 ut_assertok(get_tpm_version(TPM_V2, &dev));
Simon Glass11a075c2022-08-30 21:05:36 -060069
70 ut_assert(tpm_report_state(dev, buf, sizeof(buf)));
71 ut_asserteq_str("init_done=0", buf);
72
Ilias Apalodimasb6a2fae2023-02-18 17:21:22 +020073 ut_assertok(tpm_auto_start(dev));
Simon Glass11a075c2022-08-30 21:05:36 -060074
75 ut_assert(tpm_report_state(dev, buf, sizeof(buf)));
76 ut_asserteq_str("init_done=1", buf);
77
78 return 0;
79}
Simon Glasseef107e2023-02-21 06:24:51 -070080DM_TEST(dm_test_tpm_report_state, UT_TESTF_SCAN_FDT);
Simon Glass3467ed22023-02-21 06:24:52 -070081
82/**
83 * test_tpm_autostart() - check the tpm_auto_start() call
84 *
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
89 */
90static int test_tpm_autostart(struct unit_test_state *uts,
91 enum tpm_version version, bool reinit)
92{
93 struct udevice *dev;
94
95 /* check probe success */
96 ut_assertok(get_tpm_version(version, &dev));
97
98 if (reinit)
99 ut_assertok(tpm_init(dev));
Ilias Apalodimas83e0d212023-06-01 09:18:44 +0300100
101 /*
102 * tpm_auto_start will rerun tpm_init() if reinit, but handles the
103 * -EBUSY return code internally.
104 */
Simon Glass3467ed22023-02-21 06:24:52 -0700105 ut_assertok(tpm_auto_start(dev));
106
107 return 0;
108}
109
110static int dm_test_tpm_autostart(struct unit_test_state *uts)
111{
112 ut_assertok(test_tpm_autostart(uts, TPM_V1, false));
113 ut_assertok(test_tpm_autostart(uts, TPM_V2, false));
114
115 return 0;
116}
117DM_TEST(dm_test_tpm_autostart, UT_TESTF_SCAN_FDT);
118
119static int dm_test_tpm_autostart_reinit(struct unit_test_state *uts)
120{
121 ut_assertok(test_tpm_autostart(uts, TPM_V1, true));
122 ut_assertok(test_tpm_autostart(uts, TPM_V2, true));
123
124 return 0;
125}
126DM_TEST(dm_test_tpm_autostart_reinit, UT_TESTF_SCAN_FDT);