blob: 3ef5e811794faf5762c0334c3b66594bc834feb7 [file] [log] [blame]
Simon Glassbfe8fa22021-02-06 14:23:35 -07001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * Copyright 2019 Google LLC
4 */
5
6#include <common.h>
7#include <dm.h>
8#include <log.h>
9#include <tpm_api.h>
10#include <tpm-v1.h>
11#include <tpm-v2.h>
12#include <tpm_api.h>
13
Simon Glassbfe8fa22021-02-06 14:23:35 -070014u32 tpm_startup(struct udevice *dev, enum tpm_startup_type mode)
15{
Simon Glass5e593782022-07-22 21:32:02 +053016 if (tpm_is_v1(dev)) {
Simon Glassbfe8fa22021-02-06 14:23:35 -070017 return tpm1_startup(dev, mode);
Simon Glass5e593782022-07-22 21:32:02 +053018 } else if (tpm_is_v2(dev)) {
Simon Glassb8eb9212021-02-06 14:23:37 -070019 enum tpm2_startup_types type;
20
21 switch (mode) {
22 case TPM_ST_CLEAR:
23 type = TPM2_SU_CLEAR;
24 break;
25 case TPM_ST_STATE:
26 type = TPM2_SU_STATE;
27 break;
28 default:
29 case TPM_ST_DEACTIVATED:
30 return -EINVAL;
31 }
32 return tpm2_startup(dev, type);
33 } else {
Simon Glassbfe8fa22021-02-06 14:23:35 -070034 return -ENOSYS;
Simon Glassb8eb9212021-02-06 14:23:37 -070035 }
Simon Glassbfe8fa22021-02-06 14:23:35 -070036}
37
Ilias Apalodimasa595be32023-01-25 12:18:36 +020038u32 tpm_auto_start(struct udevice *dev)
39{
Simon Glassa11be4c2023-02-21 06:24:52 -070040 u32 rc;
Ilias Apalodimasa595be32023-01-25 12:18:36 +020041
Simon Glassa11be4c2023-02-21 06:24:52 -070042 /*
43 * the tpm_init() will return -EBUSY if the init has already happened
44 * The selftest and startup code can run multiple times with no side
45 * effects
46 */
47 rc = tpm_init(dev);
48 if (rc && rc != -EBUSY)
49 return rc;
50
51 if (tpm_is_v1(dev))
52 return tpm1_auto_start(dev);
53 else if (tpm_is_v2(dev))
54 return tpm2_auto_start(dev);
55 else
56 return -ENOSYS;
Ilias Apalodimasa595be32023-01-25 12:18:36 +020057}
58
Simon Glassbfe8fa22021-02-06 14:23:35 -070059u32 tpm_resume(struct udevice *dev)
60{
Simon Glass5e593782022-07-22 21:32:02 +053061 if (tpm_is_v1(dev))
Simon Glassbfe8fa22021-02-06 14:23:35 -070062 return tpm1_startup(dev, TPM_ST_STATE);
Simon Glass5e593782022-07-22 21:32:02 +053063 else if (tpm_is_v2(dev))
Simon Glassb8eb9212021-02-06 14:23:37 -070064 return tpm2_startup(dev, TPM2_SU_STATE);
Simon Glassbfe8fa22021-02-06 14:23:35 -070065 else
66 return -ENOSYS;
67}
68
69u32 tpm_self_test_full(struct udevice *dev)
70{
Simon Glass5e593782022-07-22 21:32:02 +053071 if (tpm_is_v1(dev))
Simon Glassbfe8fa22021-02-06 14:23:35 -070072 return tpm1_self_test_full(dev);
Simon Glass5e593782022-07-22 21:32:02 +053073 else if (tpm_is_v2(dev))
Simon Glassb8eb9212021-02-06 14:23:37 -070074 return tpm2_self_test(dev, TPMI_YES);
Simon Glassbfe8fa22021-02-06 14:23:35 -070075 else
76 return -ENOSYS;
77}
78
79u32 tpm_continue_self_test(struct udevice *dev)
80{
Simon Glass5e593782022-07-22 21:32:02 +053081 if (tpm_is_v1(dev))
Simon Glassbfe8fa22021-02-06 14:23:35 -070082 return tpm1_continue_self_test(dev);
Simon Glass5e593782022-07-22 21:32:02 +053083 else if (tpm_is_v2(dev))
Simon Glassb8eb9212021-02-06 14:23:37 -070084 return tpm2_self_test(dev, TPMI_NO);
Simon Glassbfe8fa22021-02-06 14:23:35 -070085 else
86 return -ENOSYS;
87}
88
89u32 tpm_clear_and_reenable(struct udevice *dev)
90{
91 u32 ret;
92
93 log_info("TPM: Clear and re-enable\n");
94 ret = tpm_force_clear(dev);
95 if (ret != TPM_SUCCESS) {
96 log_err("Can't initiate a force clear\n");
97 return ret;
98 }
99
Simon Glass5e593782022-07-22 21:32:02 +0530100 if (tpm_is_v1(dev)) {
Simon Glassbfe8fa22021-02-06 14:23:35 -0700101 ret = tpm1_physical_enable(dev);
102 if (ret != TPM_SUCCESS) {
103 log_err("TPM: Can't set enabled state\n");
104 return ret;
105 }
106
107 ret = tpm1_physical_set_deactivated(dev, 0);
108 if (ret != TPM_SUCCESS) {
109 log_err("TPM: Can't set deactivated state\n");
110 return ret;
111 }
Simon Glassbfe8fa22021-02-06 14:23:35 -0700112 }
113
114 return TPM_SUCCESS;
115}
116
117u32 tpm_nv_enable_locking(struct udevice *dev)
118{
Simon Glass5e593782022-07-22 21:32:02 +0530119 if (tpm_is_v1(dev))
Simon Glassbfe8fa22021-02-06 14:23:35 -0700120 return tpm1_nv_define_space(dev, TPM_NV_INDEX_LOCK, 0, 0);
Simon Glass5e593782022-07-22 21:32:02 +0530121 else if (tpm_is_v2(dev))
Simon Glassb8eb9212021-02-06 14:23:37 -0700122 return -ENOSYS;
Simon Glassbfe8fa22021-02-06 14:23:35 -0700123 else
124 return -ENOSYS;
125}
126
127u32 tpm_nv_read_value(struct udevice *dev, u32 index, void *data, u32 count)
128{
Simon Glass5e593782022-07-22 21:32:02 +0530129 if (tpm_is_v1(dev))
Simon Glassbfe8fa22021-02-06 14:23:35 -0700130 return tpm1_nv_read_value(dev, index, data, count);
Simon Glass5e593782022-07-22 21:32:02 +0530131 else if (tpm_is_v2(dev))
Simon Glass6719cbe2021-02-06 14:23:40 -0700132 return tpm2_nv_read_value(dev, index, data, count);
Simon Glassbfe8fa22021-02-06 14:23:35 -0700133 else
134 return -ENOSYS;
135}
136
137u32 tpm_nv_write_value(struct udevice *dev, u32 index, const void *data,
138 u32 count)
139{
Simon Glass5e593782022-07-22 21:32:02 +0530140 if (tpm_is_v1(dev))
Simon Glassbfe8fa22021-02-06 14:23:35 -0700141 return tpm1_nv_write_value(dev, index, data, count);
Simon Glass5e593782022-07-22 21:32:02 +0530142 else if (tpm_is_v2(dev))
Simon Glass6719cbe2021-02-06 14:23:40 -0700143 return tpm2_nv_write_value(dev, index, data, count);
Simon Glassbfe8fa22021-02-06 14:23:35 -0700144 else
145 return -ENOSYS;
146}
147
148u32 tpm_set_global_lock(struct udevice *dev)
149{
150 return tpm_nv_write_value(dev, TPM_NV_INDEX_0, NULL, 0);
151}
152
153u32 tpm_write_lock(struct udevice *dev, u32 index)
154{
Simon Glass5e593782022-07-22 21:32:02 +0530155 if (tpm_is_v1(dev))
Simon Glassbfe8fa22021-02-06 14:23:35 -0700156 return -ENOSYS;
Simon Glass5e593782022-07-22 21:32:02 +0530157 else if (tpm_is_v2(dev))
Simon Glass7785bc12021-02-06 14:23:41 -0700158 return tpm2_write_lock(dev, index);
Simon Glassbfe8fa22021-02-06 14:23:35 -0700159 else
160 return -ENOSYS;
161}
162
163u32 tpm_pcr_extend(struct udevice *dev, u32 index, const void *in_digest,
Simon Glassa557d252022-08-30 21:05:32 -0600164 uint size, void *out_digest, const char *name)
Simon Glassbfe8fa22021-02-06 14:23:35 -0700165{
Simon Glassa557d252022-08-30 21:05:32 -0600166 if (tpm_is_v1(dev)) {
Simon Glassbfe8fa22021-02-06 14:23:35 -0700167 return tpm1_extend(dev, index, in_digest, out_digest);
Simon Glassa557d252022-08-30 21:05:32 -0600168 } else if (tpm_is_v2(dev)) {
Simon Glassb8eb9212021-02-06 14:23:37 -0700169 return tpm2_pcr_extend(dev, index, TPM2_ALG_SHA256, in_digest,
170 TPM2_DIGEST_LEN);
Simon Glassa557d252022-08-30 21:05:32 -0600171 /* @name is ignored as we do not support the TPM log here */
172 } else {
Simon Glassbfe8fa22021-02-06 14:23:35 -0700173 return -ENOSYS;
Simon Glassa557d252022-08-30 21:05:32 -0600174 }
Simon Glassbfe8fa22021-02-06 14:23:35 -0700175}
176
177u32 tpm_pcr_read(struct udevice *dev, u32 index, void *data, size_t count)
178{
Simon Glass5e593782022-07-22 21:32:02 +0530179 if (tpm_is_v1(dev))
Simon Glassbfe8fa22021-02-06 14:23:35 -0700180 return tpm1_pcr_read(dev, index, data, count);
Simon Glass5e593782022-07-22 21:32:02 +0530181 else if (tpm_is_v2(dev))
Simon Glassb8eb9212021-02-06 14:23:37 -0700182 return -ENOSYS;
Simon Glassbfe8fa22021-02-06 14:23:35 -0700183 else
184 return -ENOSYS;
185}
186
187u32 tpm_tsc_physical_presence(struct udevice *dev, u16 presence)
188{
Simon Glass5e593782022-07-22 21:32:02 +0530189 if (tpm_is_v1(dev))
Simon Glassbfe8fa22021-02-06 14:23:35 -0700190 return tpm1_tsc_physical_presence(dev, presence);
Simon Glassb8eb9212021-02-06 14:23:37 -0700191
192 /*
193 * Nothing to do on TPM2 for this; use platform hierarchy availability
194 * instead.
195 */
Simon Glass5e593782022-07-22 21:32:02 +0530196 else if (tpm_is_v2(dev))
Simon Glassb8eb9212021-02-06 14:23:37 -0700197 return 0;
Simon Glassbfe8fa22021-02-06 14:23:35 -0700198 else
199 return -ENOSYS;
200}
201
202u32 tpm_finalise_physical_presence(struct udevice *dev)
203{
Simon Glass5e593782022-07-22 21:32:02 +0530204 if (tpm_is_v1(dev))
Simon Glassbfe8fa22021-02-06 14:23:35 -0700205 return tpm1_finalise_physical_presence(dev);
Simon Glassb8eb9212021-02-06 14:23:37 -0700206
207 /* Nothing needs to be done with tpm2 */
Simon Glass5e593782022-07-22 21:32:02 +0530208 else if (tpm_is_v2(dev))
Simon Glassb8eb9212021-02-06 14:23:37 -0700209 return 0;
Simon Glassbfe8fa22021-02-06 14:23:35 -0700210 else
211 return -ENOSYS;
212}
213
214u32 tpm_read_pubek(struct udevice *dev, void *data, size_t count)
215{
Simon Glass5e593782022-07-22 21:32:02 +0530216 if (tpm_is_v1(dev))
Simon Glassbfe8fa22021-02-06 14:23:35 -0700217 return tpm1_read_pubek(dev, data, count);
Simon Glass5e593782022-07-22 21:32:02 +0530218 else if (tpm_is_v2(dev))
Simon Glassbfe8fa22021-02-06 14:23:35 -0700219 return -ENOSYS; /* not implemented yet */
Simon Glassb8eb9212021-02-06 14:23:37 -0700220 else
221 return -ENOSYS;
Simon Glassbfe8fa22021-02-06 14:23:35 -0700222}
223
224u32 tpm_force_clear(struct udevice *dev)
225{
Simon Glass5e593782022-07-22 21:32:02 +0530226 if (tpm_is_v1(dev))
Simon Glassbfe8fa22021-02-06 14:23:35 -0700227 return tpm1_force_clear(dev);
Simon Glass5e593782022-07-22 21:32:02 +0530228 else if (tpm_is_v2(dev))
Simon Glassb8eb9212021-02-06 14:23:37 -0700229 return tpm2_clear(dev, TPM2_RH_PLATFORM, NULL, 0);
Simon Glassbfe8fa22021-02-06 14:23:35 -0700230 else
231 return -ENOSYS;
232}
233
234u32 tpm_physical_enable(struct udevice *dev)
235{
Simon Glass5e593782022-07-22 21:32:02 +0530236 if (tpm_is_v1(dev))
Simon Glassbfe8fa22021-02-06 14:23:35 -0700237 return tpm1_physical_enable(dev);
Simon Glassb8eb9212021-02-06 14:23:37 -0700238
239 /* Nothing needs to be done with tpm2 */
Simon Glass5e593782022-07-22 21:32:02 +0530240 else if (tpm_is_v2(dev))
Simon Glassb8eb9212021-02-06 14:23:37 -0700241 return 0;
Simon Glassbfe8fa22021-02-06 14:23:35 -0700242 else
243 return -ENOSYS;
244}
245
246u32 tpm_physical_disable(struct udevice *dev)
247{
Simon Glass5e593782022-07-22 21:32:02 +0530248 if (tpm_is_v1(dev))
Simon Glassbfe8fa22021-02-06 14:23:35 -0700249 return tpm1_physical_disable(dev);
Simon Glassb8eb9212021-02-06 14:23:37 -0700250
251 /* Nothing needs to be done with tpm2 */
Simon Glass5e593782022-07-22 21:32:02 +0530252 else if (tpm_is_v2(dev))
Simon Glassb8eb9212021-02-06 14:23:37 -0700253 return 0;
Simon Glassbfe8fa22021-02-06 14:23:35 -0700254 else
255 return -ENOSYS;
256}
257
258u32 tpm_physical_set_deactivated(struct udevice *dev, u8 state)
259{
Simon Glass5e593782022-07-22 21:32:02 +0530260 if (tpm_is_v1(dev))
Simon Glassbfe8fa22021-02-06 14:23:35 -0700261 return tpm1_physical_set_deactivated(dev, state);
Simon Glassb8eb9212021-02-06 14:23:37 -0700262 /* Nothing needs to be done with tpm2 */
Simon Glass5e593782022-07-22 21:32:02 +0530263 else if (tpm_is_v2(dev))
Simon Glassb8eb9212021-02-06 14:23:37 -0700264 return 0;
Simon Glassbfe8fa22021-02-06 14:23:35 -0700265 else
266 return -ENOSYS;
267}
268
269u32 tpm_get_capability(struct udevice *dev, u32 cap_area, u32 sub_cap,
270 void *cap, size_t count)
271{
Simon Glass5e593782022-07-22 21:32:02 +0530272 if (tpm_is_v1(dev))
Simon Glassbfe8fa22021-02-06 14:23:35 -0700273 return tpm1_get_capability(dev, cap_area, sub_cap, cap, count);
Simon Glass5e593782022-07-22 21:32:02 +0530274 else if (tpm_is_v2(dev))
Simon Glassb8eb9212021-02-06 14:23:37 -0700275 return tpm2_get_capability(dev, cap_area, sub_cap, cap, count);
Simon Glassbfe8fa22021-02-06 14:23:35 -0700276 else
277 return -ENOSYS;
278}
279
280u32 tpm_get_permissions(struct udevice *dev, u32 index, u32 *perm)
281{
Simon Glass5e593782022-07-22 21:32:02 +0530282 if (tpm_is_v1(dev))
Simon Glassbfe8fa22021-02-06 14:23:35 -0700283 return tpm1_get_permissions(dev, index, perm);
Simon Glass5e593782022-07-22 21:32:02 +0530284 else if (tpm_is_v2(dev))
Simon Glassbfe8fa22021-02-06 14:23:35 -0700285 return -ENOSYS; /* not implemented yet */
Simon Glassb8eb9212021-02-06 14:23:37 -0700286 else
287 return -ENOSYS;
Simon Glassbfe8fa22021-02-06 14:23:35 -0700288}
289
290u32 tpm_get_random(struct udevice *dev, void *data, u32 count)
291{
Simon Glass5e593782022-07-22 21:32:02 +0530292 if (tpm_is_v1(dev))
Simon Glassbfe8fa22021-02-06 14:23:35 -0700293 return tpm1_get_random(dev, data, count);
Simon Glass5e593782022-07-22 21:32:02 +0530294 else if (tpm_is_v2(dev))
Sughosh Ganue67ffb52022-07-22 21:32:04 +0530295 return tpm2_get_random(dev, data, count);
296
297 return -ENOSYS;
Simon Glassbfe8fa22021-02-06 14:23:35 -0700298}