]> Git Repo - u-boot.git/blob - test/dm/tpm.c
Merge branch 'master' of https://source.denx.de/u-boot/custodians/u-boot-sh
[u-boot.git] / test / dm / tpm.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright 2022 Google LLC
4  * Written by Simon Glass <[email protected]>
5  */
6
7 #include <dm.h>
8 #include <tpm_api.h>
9 #include <dm/test.h>
10 #include <test/test.h>
11 #include <test/ut.h>
12
13 /*
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  */
20 static 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 */
40 static 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
52 static int dm_test_tpm(struct unit_test_state *uts)
53 {
54         ut_assertok(test_tpm_init(uts, TPM_V1));
55         ut_assertok(test_tpm_init(uts, TPM_V2));
56
57         return 0;
58 }
59 DM_TEST(dm_test_tpm, UTF_SCAN_FDT);
60
61 /* Test report_state */
62 static int dm_test_tpm_report_state(struct unit_test_state *uts)
63 {
64         struct udevice *dev;
65         char buf[50];
66
67         /* check probe success */
68         ut_assertok(get_tpm_version(TPM_V2, &dev));
69
70         ut_assert(tpm_report_state(dev, buf, sizeof(buf)));
71         ut_asserteq_str("init_done=0", buf);
72
73         ut_assertok(tpm_auto_start(dev));
74
75         ut_assert(tpm_report_state(dev, buf, sizeof(buf)));
76         ut_asserteq_str("init_done=1", buf);
77
78         return 0;
79 }
80 DM_TEST(dm_test_tpm_report_state, UTF_SCAN_FDT);
81
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  */
90 static 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));
100
101         /*
102          * tpm_auto_start will rerun tpm_init() if reinit, but handles the
103          * -EBUSY return code internally.
104          */
105         ut_assertok(tpm_auto_start(dev));
106
107         return 0;
108 }
109
110 static 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 }
117 DM_TEST(dm_test_tpm_autostart, UTF_SCAN_FDT);
118
119 static 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 }
126 DM_TEST(dm_test_tpm_autostart_reinit, UTF_SCAN_FDT);
This page took 0.036379 seconds and 4 git commands to generate.