blob: cde933ab28489467db46b55eb006d1f3f31e6760 [file] [log] [blame]
Simon Glass3bb4db42022-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
7#include <common.h>
8#include <dm.h>
9#include <tpm_api.h>
10#include <dm/test.h>
11#include <test/test.h>
12#include <test/ut.h>
13
Simon Glass4fef6572023-02-21 06:24:51 -070014/*
15 * get_tpm_version() - Get a TPM of the given version
16 *
17 * @version: Version to get
18 * @devp: Returns the TPM device
19 * Returns: 0 if OK, -ENODEV if not found
20 */
21static int get_tpm_version(enum tpm_version version, struct udevice **devp)
22{
23 struct udevice *dev;
24
25 /*
26 * For now we have to probe each TPM, since the version is set up in
27 * of_to_plat(). We could require TPMs to declare their version when
28 * probed, to avoid this
29 */
30 uclass_foreach_dev_probe(UCLASS_TPM, dev) {
31 if (tpm_get_version(dev) == version) {
32 *devp = dev;
33 return 0;
34 }
35 }
36
37 return -ENODEV;
38}
39
40/* Basic test of initing a TPM */
41static int test_tpm_init(struct unit_test_state *uts, enum tpm_version version)
42{
43 struct udevice *dev;
44
45 /* check probe success */
46 ut_assertok(get_tpm_version(version, &dev));
47
48 ut_assertok(tpm_init(dev));
49
50 return 0;
51}
52
Simon Glass3bb4db42022-08-30 21:05:36 -060053static int dm_test_tpm(struct unit_test_state *uts)
54{
Simon Glass4fef6572023-02-21 06:24:51 -070055 ut_assertok(test_tpm_init(uts, TPM_V1));
56 ut_assertok(test_tpm_init(uts, TPM_V2));
57
58 return 0;
59}
60DM_TEST(dm_test_tpm, UT_TESTF_SCAN_FDT);
61
62/* Test report_state */
63static int dm_test_tpm_report_state(struct unit_test_state *uts)
64{
Simon Glass3bb4db42022-08-30 21:05:36 -060065 struct udevice *dev;
66 char buf[50];
67
68 /* check probe success */
Simon Glass4fef6572023-02-21 06:24:51 -070069 ut_assertok(get_tpm_version(TPM_V2, &dev));
Simon Glass3bb4db42022-08-30 21:05:36 -060070
71 ut_assert(tpm_report_state(dev, buf, sizeof(buf)));
72 ut_asserteq_str("init_done=0", buf);
73
Ilias Apalodimas1b11de72023-02-18 17:21:22 +020074 ut_assertok(tpm_auto_start(dev));
Simon Glass3bb4db42022-08-30 21:05:36 -060075
76 ut_assert(tpm_report_state(dev, buf, sizeof(buf)));
77 ut_asserteq_str("init_done=1", buf);
78
79 return 0;
80}
Simon Glass4fef6572023-02-21 06:24:51 -070081DM_TEST(dm_test_tpm_report_state, UT_TESTF_SCAN_FDT);
Simon Glassa11be4c2023-02-21 06:24:52 -070082
83/**
84 * test_tpm_autostart() - check the tpm_auto_start() call
85 *
86 * @uts: Unit test state
87 * @version: TPM version to use
88 * @reinit: true to call tpm_init() first
89 * Returns 0 if OK, non-zero on failure
90 */
91static int test_tpm_autostart(struct unit_test_state *uts,
92 enum tpm_version version, bool reinit)
93{
94 struct udevice *dev;
95
96 /* check probe success */
97 ut_assertok(get_tpm_version(version, &dev));
98
99 if (reinit)
100 ut_assertok(tpm_init(dev));
Ilias Apalodimasaee56c02023-06-01 09:18:44 +0300101
102 /*
103 * tpm_auto_start will rerun tpm_init() if reinit, but handles the
104 * -EBUSY return code internally.
105 */
Simon Glassa11be4c2023-02-21 06:24:52 -0700106 ut_assertok(tpm_auto_start(dev));
107
108 return 0;
109}
110
111static int dm_test_tpm_autostart(struct unit_test_state *uts)
112{
113 ut_assertok(test_tpm_autostart(uts, TPM_V1, false));
114 ut_assertok(test_tpm_autostart(uts, TPM_V2, false));
115
116 return 0;
117}
118DM_TEST(dm_test_tpm_autostart, UT_TESTF_SCAN_FDT);
119
120static int dm_test_tpm_autostart_reinit(struct unit_test_state *uts)
121{
122 ut_assertok(test_tpm_autostart(uts, TPM_V1, true));
123 ut_assertok(test_tpm_autostart(uts, TPM_V2, true));
124
125 return 0;
126}
127DM_TEST(dm_test_tpm_autostart_reinit, UT_TESTF_SCAN_FDT);