Merge tag 'u-boot-nand-20221009' of https://source.denx.de/u-boot/custodians/u-boot...
[platform/kernel/u-boot.git] / lib / tpm_api.c
1 // 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
14 u32 tpm_startup(struct udevice *dev, enum tpm_startup_type mode)
15 {
16         if (tpm_is_v1(dev)) {
17                 return tpm1_startup(dev, mode);
18         } else if (tpm_is_v2(dev)) {
19                 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 {
34                 return -ENOSYS;
35         }
36 }
37
38 u32 tpm_resume(struct udevice *dev)
39 {
40         if (tpm_is_v1(dev))
41                 return tpm1_startup(dev, TPM_ST_STATE);
42         else if (tpm_is_v2(dev))
43                 return tpm2_startup(dev, TPM2_SU_STATE);
44         else
45                 return -ENOSYS;
46 }
47
48 u32 tpm_self_test_full(struct udevice *dev)
49 {
50         if (tpm_is_v1(dev))
51                 return tpm1_self_test_full(dev);
52         else if (tpm_is_v2(dev))
53                 return tpm2_self_test(dev, TPMI_YES);
54         else
55                 return -ENOSYS;
56 }
57
58 u32 tpm_continue_self_test(struct udevice *dev)
59 {
60         if (tpm_is_v1(dev))
61                 return tpm1_continue_self_test(dev);
62         else if (tpm_is_v2(dev))
63                 return tpm2_self_test(dev, TPMI_NO);
64         else
65                 return -ENOSYS;
66 }
67
68 u32 tpm_clear_and_reenable(struct udevice *dev)
69 {
70         u32 ret;
71
72         log_info("TPM: Clear and re-enable\n");
73         ret = tpm_force_clear(dev);
74         if (ret != TPM_SUCCESS) {
75                 log_err("Can't initiate a force clear\n");
76                 return ret;
77         }
78
79         if (tpm_is_v1(dev)) {
80                 ret = tpm1_physical_enable(dev);
81                 if (ret != TPM_SUCCESS) {
82                         log_err("TPM: Can't set enabled state\n");
83                         return ret;
84                 }
85
86                 ret = tpm1_physical_set_deactivated(dev, 0);
87                 if (ret != TPM_SUCCESS) {
88                         log_err("TPM: Can't set deactivated state\n");
89                         return ret;
90                 }
91         }
92
93         return TPM_SUCCESS;
94 }
95
96 u32 tpm_nv_enable_locking(struct udevice *dev)
97 {
98         if (tpm_is_v1(dev))
99                 return tpm1_nv_define_space(dev, TPM_NV_INDEX_LOCK, 0, 0);
100         else if (tpm_is_v2(dev))
101                 return -ENOSYS;
102         else
103                 return -ENOSYS;
104 }
105
106 u32 tpm_nv_read_value(struct udevice *dev, u32 index, void *data, u32 count)
107 {
108         if (tpm_is_v1(dev))
109                 return tpm1_nv_read_value(dev, index, data, count);
110         else if (tpm_is_v2(dev))
111                 return tpm2_nv_read_value(dev, index, data, count);
112         else
113                 return -ENOSYS;
114 }
115
116 u32 tpm_nv_write_value(struct udevice *dev, u32 index, const void *data,
117                        u32 count)
118 {
119         if (tpm_is_v1(dev))
120                 return tpm1_nv_write_value(dev, index, data, count);
121         else if (tpm_is_v2(dev))
122                 return tpm2_nv_write_value(dev, index, data, count);
123         else
124                 return -ENOSYS;
125 }
126
127 u32 tpm_set_global_lock(struct udevice *dev)
128 {
129         return tpm_nv_write_value(dev, TPM_NV_INDEX_0, NULL, 0);
130 }
131
132 u32 tpm_write_lock(struct udevice *dev, u32 index)
133 {
134         if (tpm_is_v1(dev))
135                 return -ENOSYS;
136         else if (tpm_is_v2(dev))
137                 return tpm2_write_lock(dev, index);
138         else
139                 return -ENOSYS;
140 }
141
142 u32 tpm_pcr_extend(struct udevice *dev, u32 index, const void *in_digest,
143                    uint size, void *out_digest, const char *name)
144 {
145         if (tpm_is_v1(dev)) {
146                 return tpm1_extend(dev, index, in_digest, out_digest);
147         } else if (tpm_is_v2(dev)) {
148                 return tpm2_pcr_extend(dev, index, TPM2_ALG_SHA256, in_digest,
149                                        TPM2_DIGEST_LEN);
150                 /* @name is ignored as we do not support the TPM log here */
151         } else {
152                 return -ENOSYS;
153         }
154 }
155
156 u32 tpm_pcr_read(struct udevice *dev, u32 index, void *data, size_t count)
157 {
158         if (tpm_is_v1(dev))
159                 return tpm1_pcr_read(dev, index, data, count);
160         else if (tpm_is_v2(dev))
161                 return -ENOSYS;
162         else
163                 return -ENOSYS;
164 }
165
166 u32 tpm_tsc_physical_presence(struct udevice *dev, u16 presence)
167 {
168         if (tpm_is_v1(dev))
169                 return tpm1_tsc_physical_presence(dev, presence);
170
171         /*
172          * Nothing to do on TPM2 for this; use platform hierarchy availability
173          * instead.
174          */
175         else if (tpm_is_v2(dev))
176                 return 0;
177         else
178                 return -ENOSYS;
179 }
180
181 u32 tpm_finalise_physical_presence(struct udevice *dev)
182 {
183         if (tpm_is_v1(dev))
184                 return tpm1_finalise_physical_presence(dev);
185
186         /* Nothing needs to be done with tpm2 */
187         else if (tpm_is_v2(dev))
188                 return 0;
189         else
190                 return -ENOSYS;
191 }
192
193 u32 tpm_read_pubek(struct udevice *dev, void *data, size_t count)
194 {
195         if (tpm_is_v1(dev))
196                 return tpm1_read_pubek(dev, data, count);
197         else if (tpm_is_v2(dev))
198                 return -ENOSYS; /* not implemented yet */
199         else
200                 return -ENOSYS;
201 }
202
203 u32 tpm_force_clear(struct udevice *dev)
204 {
205         if (tpm_is_v1(dev))
206                 return tpm1_force_clear(dev);
207         else if (tpm_is_v2(dev))
208                 return tpm2_clear(dev, TPM2_RH_PLATFORM, NULL, 0);
209         else
210                 return -ENOSYS;
211 }
212
213 u32 tpm_physical_enable(struct udevice *dev)
214 {
215         if (tpm_is_v1(dev))
216                 return tpm1_physical_enable(dev);
217
218         /* Nothing needs to be done with tpm2 */
219         else if (tpm_is_v2(dev))
220                 return 0;
221         else
222                 return -ENOSYS;
223 }
224
225 u32 tpm_physical_disable(struct udevice *dev)
226 {
227         if (tpm_is_v1(dev))
228                 return tpm1_physical_disable(dev);
229
230         /* Nothing needs to be done with tpm2 */
231         else if (tpm_is_v2(dev))
232                 return 0;
233         else
234                 return -ENOSYS;
235 }
236
237 u32 tpm_physical_set_deactivated(struct udevice *dev, u8 state)
238 {
239         if (tpm_is_v1(dev))
240                 return tpm1_physical_set_deactivated(dev, state);
241         /* Nothing needs to be done with tpm2 */
242         else if (tpm_is_v2(dev))
243                 return 0;
244         else
245                 return -ENOSYS;
246 }
247
248 u32 tpm_get_capability(struct udevice *dev, u32 cap_area, u32 sub_cap,
249                        void *cap, size_t count)
250 {
251         if (tpm_is_v1(dev))
252                 return tpm1_get_capability(dev, cap_area, sub_cap, cap, count);
253         else if (tpm_is_v2(dev))
254                 return tpm2_get_capability(dev, cap_area, sub_cap, cap, count);
255         else
256                 return -ENOSYS;
257 }
258
259 u32 tpm_get_permissions(struct udevice *dev, u32 index, u32 *perm)
260 {
261         if (tpm_is_v1(dev))
262                 return tpm1_get_permissions(dev, index, perm);
263         else if (tpm_is_v2(dev))
264                 return -ENOSYS; /* not implemented yet */
265         else
266                 return -ENOSYS;
267 }
268
269 u32 tpm_get_random(struct udevice *dev, void *data, u32 count)
270 {
271         if (tpm_is_v1(dev))
272                 return tpm1_get_random(dev, data, count);
273         else if (tpm_is_v2(dev))
274                 return tpm2_get_random(dev, data, count);
275
276         return -ENOSYS;
277 }