blob: 6e9bc794f9ecb7c8df677b46d18334d5dc0b9c73 [file] [log] [blame]
Miquel Raynalf3b43502018-05-15 11:57:08 +02001/* SPDX-License-Identifier: GPL-2.0+ */
2/*
Ilias Apalodimasf4e05902020-11-11 11:18:10 +02003 * Defines APIs and structures that allow software to interact with a
4 * TPM2 device
5 *
6 * Copyright (c) 2020 Linaro
Miquel Raynalf3b43502018-05-15 11:57:08 +02007 * Copyright (c) 2018 Bootlin
Ilias Apalodimasf4e05902020-11-11 11:18:10 +02008 *
9 * https://trustedcomputinggroup.org/resource/tss-overview-common-structures-specification/
10 *
Miquel Raynalf3b43502018-05-15 11:57:08 +020011 * Author: Miquel Raynal <miquel.raynal@bootlin.com>
12 */
13
14#ifndef __TPM_V2_H
15#define __TPM_V2_H
16
17#include <tpm-common.h>
18
Simon Glass3ba929a2020-10-30 21:38:53 -060019struct udevice;
20
Miquel Raynalf3b43502018-05-15 11:57:08 +020021#define TPM2_DIGEST_LEN 32
22
Ilias Apalodimascae28ef2020-11-30 11:47:39 +020023#define TPM2_SHA1_DIGEST_SIZE 20
24#define TPM2_SHA256_DIGEST_SIZE 32
25#define TPM2_SHA384_DIGEST_SIZE 48
26#define TPM2_SHA512_DIGEST_SIZE 64
27#define TPM2_SM3_256_DIGEST_SIZE 32
28
Ilias Apalodimasf4e05902020-11-11 11:18:10 +020029#define TPM2_MAX_PCRS 32
30#define TPM2_PCR_SELECT_MAX ((TPM2_MAX_PCRS + 7) / 8)
31#define TPM2_MAX_CAP_BUFFER 1024
32#define TPM2_MAX_TPM_PROPERTIES ((TPM2_MAX_CAP_BUFFER - sizeof(u32) /* TPM2_CAP */ - \
33 sizeof(u32)) / sizeof(struct tpms_tagged_property))
34
Simon Glassca31f072021-07-18 14:18:03 -060035#define TPM2_HDR_LEN 10
36
Ilias Apalodimasf4e05902020-11-11 11:18:10 +020037#define TPM2_CAP_PCRS 0x00000005U
38#define TPM2_CAP_TPM_PROPERTIES 0x00000006U
39
40/* Definition of (UINT32) TPM2_PT Constants */
41#define TPM2_PT_GROUP (u32)(0x00000100)
42#define TPM2_PT_FIXED (u32)(TPM2_PT_GROUP * 1)
43#define TPM2_PT_MANUFACTURER (u32)(TPM2_PT_FIXED + 5)
44#define TPM2_PT_PCR_COUNT (u32)(TPM2_PT_FIXED + 18)
45#define TPM2_PT_MAX_COMMAND_SIZE (u32)(TPM2_PT_FIXED + 30)
46#define TPM2_PT_MAX_RESPONSE_SIZE (u32)(TPM2_PT_FIXED + 31)
47
48/* TPMS_TAGGED_PROPERTY Structure */
49struct tpms_tagged_property {
50 u32 property;
51 u32 value;
52} __packed;
53
54/* TPMS_PCR_SELECTION Structure */
55struct tpms_pcr_selection {
56 u16 hash;
57 u8 size_of_select;
58 u8 pcr_select[TPM2_PCR_SELECT_MAX];
59} __packed;
60
61/* TPML_PCR_SELECTION Structure */
62struct tpml_pcr_selection {
63 u32 count;
64 struct tpms_pcr_selection selection[TPM2_NUM_PCR_BANKS];
65} __packed;
66
67/* TPML_TAGGED_TPM_PROPERTY Structure */
68struct tpml_tagged_tpm_property {
69 u32 count;
70 struct tpms_tagged_property tpm_property[TPM2_MAX_TPM_PROPERTIES];
71} __packed;
72
73/* TPMU_CAPABILITIES Union */
74union tpmu_capabilities {
75 /*
76 * Non exhaustive. Only added the structs needed for our
77 * current code
78 */
79 struct tpml_pcr_selection assigned_pcr;
80 struct tpml_tagged_tpm_property tpm_properties;
81} __packed;
82
83/* TPMS_CAPABILITY_DATA Structure */
84struct tpms_capability_data {
85 u32 capability;
86 union tpmu_capabilities data;
87} __packed;
88
Miquel Raynalf3b43502018-05-15 11:57:08 +020089/**
Ilias Apalodimascae28ef2020-11-30 11:47:39 +020090 * Definition of TPMU_HA Union
91 */
Eddie James90b6c862023-10-24 10:43:47 -050092union tpmu_ha {
Ilias Apalodimascae28ef2020-11-30 11:47:39 +020093 u8 sha1[TPM2_SHA1_DIGEST_SIZE];
94 u8 sha256[TPM2_SHA256_DIGEST_SIZE];
95 u8 sm3_256[TPM2_SM3_256_DIGEST_SIZE];
96 u8 sha384[TPM2_SHA384_DIGEST_SIZE];
97 u8 sha512[TPM2_SHA512_DIGEST_SIZE];
98} __packed;
99
100/**
101 * Definition of TPMT_HA Structure
102 *
103 * @hash_alg: Hash algorithm defined in enum tpm2_algorithms
104 * @digest: Digest value for a given algorithm
105 */
106struct tpmt_ha {
107 u16 hash_alg;
Eddie James90b6c862023-10-24 10:43:47 -0500108 union tpmu_ha digest;
Ilias Apalodimascae28ef2020-11-30 11:47:39 +0200109} __packed;
110
111/**
112 * Definition of TPML_DIGEST_VALUES Structure
113 *
114 * @count: Number of algorithms supported by hardware
115 * @digests: struct for algorithm id and hash value
116 */
117struct tpml_digest_values {
118 u32 count;
119 struct tpmt_ha digests[TPM2_NUM_PCR_BANKS];
120} __packed;
121
122/**
Miquel Raynalf3b43502018-05-15 11:57:08 +0200123 * TPM2 Structure Tags for command/response buffers.
124 *
125 * @TPM2_ST_NO_SESSIONS: the command does not need an authentication.
126 * @TPM2_ST_SESSIONS: the command needs an authentication.
127 */
128enum tpm2_structures {
129 TPM2_ST_NO_SESSIONS = 0x8001,
130 TPM2_ST_SESSIONS = 0x8002,
131};
132
133/**
134 * TPM2 type of boolean.
135 */
136enum tpm2_yes_no {
137 TPMI_YES = 1,
138 TPMI_NO = 0,
139};
140
141/**
142 * TPM2 startup values.
143 *
144 * @TPM2_SU_CLEAR: reset the internal state.
145 * @TPM2_SU_STATE: restore saved state (if any).
146 */
147enum tpm2_startup_types {
148 TPM2_SU_CLEAR = 0x0000,
149 TPM2_SU_STATE = 0x0001,
150};
151
152/**
153 * TPM2 permanent handles.
154 *
155 * @TPM2_RH_OWNER: refers to the 'owner' hierarchy.
156 * @TPM2_RS_PW: indicates a password.
157 * @TPM2_RH_LOCKOUT: refers to the 'lockout' hierarchy.
158 * @TPM2_RH_ENDORSEMENT: refers to the 'endorsement' hierarchy.
159 * @TPM2_RH_PLATFORM: refers to the 'platform' hierarchy.
160 */
161enum tpm2_handles {
162 TPM2_RH_OWNER = 0x40000001,
163 TPM2_RS_PW = 0x40000009,
164 TPM2_RH_LOCKOUT = 0x4000000A,
165 TPM2_RH_ENDORSEMENT = 0x4000000B,
166 TPM2_RH_PLATFORM = 0x4000000C,
167};
168
169/**
170 * TPM2 command codes used at the beginning of a buffer, gives the command.
171 *
172 * @TPM2_CC_STARTUP: TPM2_Startup().
173 * @TPM2_CC_SELF_TEST: TPM2_SelfTest().
174 * @TPM2_CC_CLEAR: TPM2_Clear().
175 * @TPM2_CC_CLEARCONTROL: TPM2_ClearControl().
176 * @TPM2_CC_HIERCHANGEAUTH: TPM2_HierarchyChangeAuth().
177 * @TPM2_CC_PCR_SETAUTHPOL: TPM2_PCR_SetAuthPolicy().
178 * @TPM2_CC_DAM_RESET: TPM2_DictionaryAttackLockReset().
179 * @TPM2_CC_DAM_PARAMETERS: TPM2_DictionaryAttackParameters().
180 * @TPM2_CC_GET_CAPABILITY: TPM2_GetCapibility().
Dhananjay Phadke7a2cf2e2020-06-04 16:43:59 -0700181 * @TPM2_CC_GET_RANDOM: TPM2_GetRandom().
Miquel Raynalf3b43502018-05-15 11:57:08 +0200182 * @TPM2_CC_PCR_READ: TPM2_PCR_Read().
183 * @TPM2_CC_PCR_EXTEND: TPM2_PCR_Extend().
184 * @TPM2_CC_PCR_SETAUTHVAL: TPM2_PCR_SetAuthValue().
185 */
186enum tpm2_command_codes {
187 TPM2_CC_STARTUP = 0x0144,
188 TPM2_CC_SELF_TEST = 0x0143,
Simon Glass77759db2021-02-06 14:23:42 -0700189 TPM2_CC_HIER_CONTROL = 0x0121,
Miquel Raynalf3b43502018-05-15 11:57:08 +0200190 TPM2_CC_CLEAR = 0x0126,
191 TPM2_CC_CLEARCONTROL = 0x0127,
192 TPM2_CC_HIERCHANGEAUTH = 0x0129,
Simon Glass713c58a2021-02-06 14:23:39 -0700193 TPM2_CC_NV_DEFINE_SPACE = 0x012a,
Miquel Raynal0b864f62018-05-15 11:57:20 +0200194 TPM2_CC_PCR_SETAUTHPOL = 0x012C,
Simon Glass3d930ed2021-02-06 14:23:40 -0700195 TPM2_CC_NV_WRITE = 0x0137,
Simon Glasse9d3d592021-02-06 14:23:41 -0700196 TPM2_CC_NV_WRITELOCK = 0x0138,
Miquel Raynalf3b43502018-05-15 11:57:08 +0200197 TPM2_CC_DAM_RESET = 0x0139,
198 TPM2_CC_DAM_PARAMETERS = 0x013A,
Simon Glass5ff3f162018-10-01 11:55:17 -0600199 TPM2_CC_NV_READ = 0x014E,
Miquel Raynalf3b43502018-05-15 11:57:08 +0200200 TPM2_CC_GET_CAPABILITY = 0x017A,
Dhananjay Phadke7a2cf2e2020-06-04 16:43:59 -0700201 TPM2_CC_GET_RANDOM = 0x017B,
Miquel Raynalf3b43502018-05-15 11:57:08 +0200202 TPM2_CC_PCR_READ = 0x017E,
203 TPM2_CC_PCR_EXTEND = 0x0182,
Miquel Raynal0b864f62018-05-15 11:57:20 +0200204 TPM2_CC_PCR_SETAUTHVAL = 0x0183,
Miquel Raynalf3b43502018-05-15 11:57:08 +0200205};
206
207/**
208 * TPM2 return codes.
209 */
210enum tpm2_return_codes {
211 TPM2_RC_SUCCESS = 0x0000,
212 TPM2_RC_BAD_TAG = 0x001E,
213 TPM2_RC_FMT1 = 0x0080,
214 TPM2_RC_HASH = TPM2_RC_FMT1 + 0x0003,
215 TPM2_RC_VALUE = TPM2_RC_FMT1 + 0x0004,
216 TPM2_RC_SIZE = TPM2_RC_FMT1 + 0x0015,
217 TPM2_RC_BAD_AUTH = TPM2_RC_FMT1 + 0x0022,
218 TPM2_RC_HANDLE = TPM2_RC_FMT1 + 0x000B,
219 TPM2_RC_VER1 = 0x0100,
220 TPM2_RC_INITIALIZE = TPM2_RC_VER1 + 0x0000,
221 TPM2_RC_FAILURE = TPM2_RC_VER1 + 0x0001,
222 TPM2_RC_DISABLED = TPM2_RC_VER1 + 0x0020,
223 TPM2_RC_AUTH_MISSING = TPM2_RC_VER1 + 0x0025,
224 TPM2_RC_COMMAND_CODE = TPM2_RC_VER1 + 0x0043,
225 TPM2_RC_AUTHSIZE = TPM2_RC_VER1 + 0x0044,
226 TPM2_RC_AUTH_CONTEXT = TPM2_RC_VER1 + 0x0045,
Simon Glass77759db2021-02-06 14:23:42 -0700227 TPM2_RC_NV_DEFINED = TPM2_RC_VER1 + 0x004c,
Miquel Raynalf3b43502018-05-15 11:57:08 +0200228 TPM2_RC_NEEDS_TEST = TPM2_RC_VER1 + 0x0053,
229 TPM2_RC_WARN = 0x0900,
230 TPM2_RC_TESTING = TPM2_RC_WARN + 0x000A,
231 TPM2_RC_REFERENCE_H0 = TPM2_RC_WARN + 0x0010,
232 TPM2_RC_LOCKOUT = TPM2_RC_WARN + 0x0021,
233};
234
235/**
236 * TPM2 algorithms.
237 */
238enum tpm2_algorithms {
Ilias Apalodimasf4e05902020-11-11 11:18:10 +0200239 TPM2_ALG_SHA1 = 0x04,
Miquel Raynalf3b43502018-05-15 11:57:08 +0200240 TPM2_ALG_XOR = 0x0A,
241 TPM2_ALG_SHA256 = 0x0B,
242 TPM2_ALG_SHA384 = 0x0C,
243 TPM2_ALG_SHA512 = 0x0D,
244 TPM2_ALG_NULL = 0x10,
Ilias Apalodimasf4e05902020-11-11 11:18:10 +0200245 TPM2_ALG_SM3_256 = 0x12,
Miquel Raynalf3b43502018-05-15 11:57:08 +0200246};
247
Tim Harvey6ea1e052024-05-25 13:00:48 -0700248/**
249 * struct digest_info - details of supported digests
250 *
251 * @hash_name: hash name
252 * @hash_alg: hash algorithm id
253 * @hash_mask: hash registry mask
254 * @hash_len: hash digest length
255 */
256struct digest_info {
257 const char *hash_name;
258 u16 hash_alg;
259 u32 hash_mask;
260 u16 hash_len;
Raymond Mao43158122024-12-24 08:01:07 -0800261 bool supported;
Tim Harvey6ea1e052024-05-25 13:00:48 -0700262};
263
264/* Algorithm Registry */
265#define TCG2_BOOT_HASH_ALG_SHA1 0x00000001
266#define TCG2_BOOT_HASH_ALG_SHA256 0x00000002
267#define TCG2_BOOT_HASH_ALG_SHA384 0x00000004
268#define TCG2_BOOT_HASH_ALG_SHA512 0x00000008
269#define TCG2_BOOT_HASH_ALG_SM3_256 0x00000010
270
271static const struct digest_info hash_algo_list[] = {
272 {
273 "sha1",
274 TPM2_ALG_SHA1,
275 TCG2_BOOT_HASH_ALG_SHA1,
276 TPM2_SHA1_DIGEST_SIZE,
Raymond Mao43158122024-12-24 08:01:07 -0800277#if IS_ENABLED(CONFIG_SHA1)
278 true,
279#else
280 false,
Ilias Apalodimas1e665f92024-06-23 14:48:18 +0300281#endif
Raymond Mao43158122024-12-24 08:01:07 -0800282 },
Tim Harvey6ea1e052024-05-25 13:00:48 -0700283 {
284 "sha256",
285 TPM2_ALG_SHA256,
286 TCG2_BOOT_HASH_ALG_SHA256,
287 TPM2_SHA256_DIGEST_SIZE,
Raymond Mao43158122024-12-24 08:01:07 -0800288#if IS_ENABLED(CONFIG_SHA256)
289 true,
290#else
291 false,
Ilias Apalodimas1e665f92024-06-23 14:48:18 +0300292#endif
Raymond Mao43158122024-12-24 08:01:07 -0800293 },
Tim Harvey6ea1e052024-05-25 13:00:48 -0700294 {
295 "sha384",
296 TPM2_ALG_SHA384,
297 TCG2_BOOT_HASH_ALG_SHA384,
298 TPM2_SHA384_DIGEST_SIZE,
Raymond Mao43158122024-12-24 08:01:07 -0800299#if IS_ENABLED(CONFIG_SHA384)
300 true,
301#else
302 false,
Ilias Apalodimas1e665f92024-06-23 14:48:18 +0300303#endif
Raymond Mao43158122024-12-24 08:01:07 -0800304 },
Tim Harvey6ea1e052024-05-25 13:00:48 -0700305 {
306 "sha512",
307 TPM2_ALG_SHA512,
308 TCG2_BOOT_HASH_ALG_SHA512,
309 TPM2_SHA512_DIGEST_SIZE,
Raymond Mao43158122024-12-24 08:01:07 -0800310#if IS_ENABLED(CONFIG_SHA512)
311 true,
312#else
313 false,
Ilias Apalodimas1e665f92024-06-23 14:48:18 +0300314#endif
Raymond Mao43158122024-12-24 08:01:07 -0800315 },
Tim Harvey6ea1e052024-05-25 13:00:48 -0700316};
Eddie James8ed7bb32023-10-24 10:43:49 -0500317
Simon Glassb4ebd1f2018-11-23 21:29:34 -0700318/* NV index attributes */
319enum tpm_index_attrs {
320 TPMA_NV_PPWRITE = 1UL << 0,
321 TPMA_NV_OWNERWRITE = 1UL << 1,
322 TPMA_NV_AUTHWRITE = 1UL << 2,
323 TPMA_NV_POLICYWRITE = 1UL << 3,
324 TPMA_NV_COUNTER = 1UL << 4,
325 TPMA_NV_BITS = 1UL << 5,
326 TPMA_NV_EXTEND = 1UL << 6,
327 TPMA_NV_POLICY_DELETE = 1UL << 10,
328 TPMA_NV_WRITELOCKED = 1UL << 11,
329 TPMA_NV_WRITEALL = 1UL << 12,
330 TPMA_NV_WRITEDEFINE = 1UL << 13,
331 TPMA_NV_WRITE_STCLEAR = 1UL << 14,
332 TPMA_NV_GLOBALLOCK = 1UL << 15,
333 TPMA_NV_PPREAD = 1UL << 16,
334 TPMA_NV_OWNERREAD = 1UL << 17,
335 TPMA_NV_AUTHREAD = 1UL << 18,
336 TPMA_NV_POLICYREAD = 1UL << 19,
337 TPMA_NV_NO_DA = 1UL << 25,
338 TPMA_NV_ORDERLY = 1UL << 26,
339 TPMA_NV_CLEAR_STCLEAR = 1UL << 27,
340 TPMA_NV_READLOCKED = 1UL << 28,
341 TPMA_NV_WRITTEN = 1UL << 29,
342 TPMA_NV_PLATFORMCREATE = 1UL << 30,
343 TPMA_NV_READ_STCLEAR = 1UL << 31,
344
345 TPMA_NV_MASK_READ = TPMA_NV_PPREAD | TPMA_NV_OWNERREAD |
346 TPMA_NV_AUTHREAD | TPMA_NV_POLICYREAD,
347 TPMA_NV_MASK_WRITE = TPMA_NV_PPWRITE | TPMA_NV_OWNERWRITE |
348 TPMA_NV_AUTHWRITE | TPMA_NV_POLICYWRITE,
349};
350
Simon Glasse1ed0ec2020-02-06 09:55:03 -0700351enum {
352 TPM_ACCESS_VALID = 1 << 7,
353 TPM_ACCESS_ACTIVE_LOCALITY = 1 << 5,
354 TPM_ACCESS_REQUEST_PENDING = 1 << 2,
355 TPM_ACCESS_REQUEST_USE = 1 << 1,
356 TPM_ACCESS_ESTABLISHMENT = 1 << 0,
357};
358
359enum {
360 TPM_STS_FAMILY_SHIFT = 26,
361 TPM_STS_FAMILY_MASK = 0x3 << TPM_STS_FAMILY_SHIFT,
362 TPM_STS_FAMILY_TPM2 = 1 << TPM_STS_FAMILY_SHIFT,
363 TPM_STS_RESE_TESTABLISMENT_BIT = 1 << 25,
364 TPM_STS_COMMAND_CANCEL = 1 << 24,
365 TPM_STS_BURST_COUNT_SHIFT = 8,
366 TPM_STS_BURST_COUNT_MASK = 0xffff << TPM_STS_BURST_COUNT_SHIFT,
367 TPM_STS_VALID = 1 << 7,
368 TPM_STS_COMMAND_READY = 1 << 6,
369 TPM_STS_GO = 1 << 5,
370 TPM_STS_DATA_AVAIL = 1 << 4,
371 TPM_STS_DATA_EXPECT = 1 << 3,
372 TPM_STS_SELF_TEST_DONE = 1 << 2,
373 TPM_STS_RESPONSE_RETRY = 1 << 1,
Ilias Apalodimas97f5e2d2021-11-09 09:02:17 +0200374 TPM_STS_READ_ZERO = 0x23
Simon Glasse1ed0ec2020-02-06 09:55:03 -0700375};
376
377enum {
378 TPM_CMD_COUNT_OFFSET = 2,
379 TPM_CMD_ORDINAL_OFFSET = 6,
380 TPM_MAX_BUF_SIZE = 1260,
381};
382
Simon Glass3d930ed2021-02-06 14:23:40 -0700383enum {
384 /* Secure storage for firmware settings */
385 TPM_HT_PCR = 0,
386 TPM_HT_NV_INDEX,
387 TPM_HT_HMAC_SESSION,
388 TPM_HT_POLICY_SESSION,
389
390 HR_SHIFT = 24,
391 HR_PCR = TPM_HT_PCR << HR_SHIFT,
392 HR_HMAC_SESSION = TPM_HT_HMAC_SESSION << HR_SHIFT,
393 HR_POLICY_SESSION = TPM_HT_POLICY_SESSION << HR_SHIFT,
394 HR_NV_INDEX = TPM_HT_NV_INDEX << HR_SHIFT,
395};
396
Miquel Raynal65a1a6c2018-05-15 11:57:12 +0200397/**
398 * Issue a TPM2_Startup command.
399 *
Simon Glass8ceca1d2018-11-18 14:22:27 -0700400 * @dev TPM device
Miquel Raynal65a1a6c2018-05-15 11:57:12 +0200401 * @mode TPM startup mode
402 *
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100403 * Return: code of the operation
Miquel Raynal65a1a6c2018-05-15 11:57:12 +0200404 */
Simon Glass8ceca1d2018-11-18 14:22:27 -0700405u32 tpm2_startup(struct udevice *dev, enum tpm2_startup_types mode);
Miquel Raynal65a1a6c2018-05-15 11:57:12 +0200406
Miquel Raynal39c76082018-05-15 11:57:13 +0200407/**
408 * Issue a TPM2_SelfTest command.
409 *
Simon Glass8ceca1d2018-11-18 14:22:27 -0700410 * @dev TPM device
Miquel Raynal39c76082018-05-15 11:57:13 +0200411 * @full_test Asking to perform all tests or only the untested ones
412 *
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100413 * Return: code of the operation
Miquel Raynal39c76082018-05-15 11:57:13 +0200414 */
Simon Glass8ceca1d2018-11-18 14:22:27 -0700415u32 tpm2_self_test(struct udevice *dev, enum tpm2_yes_no full_test);
Miquel Raynal39c76082018-05-15 11:57:13 +0200416
Miquel Raynal8df6f8d2018-05-15 11:57:14 +0200417/**
418 * Issue a TPM2_Clear command.
419 *
Simon Glass8ceca1d2018-11-18 14:22:27 -0700420 * @dev TPM device
Miquel Raynal8df6f8d2018-05-15 11:57:14 +0200421 * @handle Handle
422 * @pw Password
423 * @pw_sz Length of the password
424 *
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100425 * Return: code of the operation
Miquel Raynal8df6f8d2018-05-15 11:57:14 +0200426 */
Simon Glass8ceca1d2018-11-18 14:22:27 -0700427u32 tpm2_clear(struct udevice *dev, u32 handle, const char *pw,
428 const ssize_t pw_sz);
Miquel Raynal8df6f8d2018-05-15 11:57:14 +0200429
Miquel Raynal14d72352018-05-15 11:57:15 +0200430/**
Simon Glass713c58a2021-02-06 14:23:39 -0700431 * Issue a TPM_NV_DefineSpace command
432 *
433 * This allows a space to be defined with given attributes and policy
434 *
435 * @dev TPM device
436 * @space_index index of the area
437 * @space_size size of area in bytes
438 * @nv_attributes TPM_NV_ATTRIBUTES of the area
439 * @nv_policy policy to use
440 * @nv_policy_size size of the policy
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100441 * Return: return code of the operation
Simon Glass713c58a2021-02-06 14:23:39 -0700442 */
443u32 tpm2_nv_define_space(struct udevice *dev, u32 space_index,
444 size_t space_size, u32 nv_attributes,
445 const u8 *nv_policy, size_t nv_policy_size);
446
447/**
Miquel Raynal14d72352018-05-15 11:57:15 +0200448 * Issue a TPM2_PCR_Extend command.
449 *
Simon Glass8ceca1d2018-11-18 14:22:27 -0700450 * @dev TPM device
Miquel Raynal14d72352018-05-15 11:57:15 +0200451 * @index Index of the PCR
Ilias Apalodimas7f59c712020-11-26 23:07:22 +0200452 * @algorithm Algorithm used, defined in 'enum tpm2_algorithms'
Miquel Raynal14d72352018-05-15 11:57:15 +0200453 * @digest Value representing the event to be recorded
Ilias Apalodimas7f59c712020-11-26 23:07:22 +0200454 * @digest_len len of the hash
Miquel Raynal14d72352018-05-15 11:57:15 +0200455 *
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100456 * Return: code of the operation
Miquel Raynal14d72352018-05-15 11:57:15 +0200457 */
Ilias Apalodimas7f59c712020-11-26 23:07:22 +0200458u32 tpm2_pcr_extend(struct udevice *dev, u32 index, u32 algorithm,
459 const u8 *digest, u32 digest_len);
Miquel Raynal14d72352018-05-15 11:57:15 +0200460
Miquel Raynal4c1a5852018-05-15 11:57:16 +0200461/**
Simon Glass3d930ed2021-02-06 14:23:40 -0700462 * Read data from the secure storage
463 *
464 * @dev TPM device
465 * @index Index of data to read
466 * @data Place to put data
467 * @count Number of bytes of data
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100468 * Return: code of the operation
Simon Glass3d930ed2021-02-06 14:23:40 -0700469 */
470u32 tpm2_nv_read_value(struct udevice *dev, u32 index, void *data, u32 count);
471
472/**
473 * Write data to the secure storage
474 *
475 * @dev TPM device
476 * @index Index of data to write
477 * @data Data to write
478 * @count Number of bytes of data
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100479 * Return: code of the operation
Simon Glass3d930ed2021-02-06 14:23:40 -0700480 */
481u32 tpm2_nv_write_value(struct udevice *dev, u32 index, const void *data,
482 u32 count);
483
484/**
Miquel Raynal4c1a5852018-05-15 11:57:16 +0200485 * Issue a TPM2_PCR_Read command.
486 *
Simon Glass8ceca1d2018-11-18 14:22:27 -0700487 * @dev TPM device
Miquel Raynal4c1a5852018-05-15 11:57:16 +0200488 * @idx Index of the PCR
489 * @idx_min_sz Minimum size in bytes of the pcrSelect array
Ruchika Gupta686bedb2021-11-29 13:09:45 +0530490 * @algorithm Algorithm used, defined in 'enum tpm2_algorithms'
Miquel Raynal4c1a5852018-05-15 11:57:16 +0200491 * @data Output buffer for contents of the named PCR
Ruchika Gupta686bedb2021-11-29 13:09:45 +0530492 * @digest_len len of the data
Miquel Raynal4c1a5852018-05-15 11:57:16 +0200493 * @updates Optional out parameter: number of updates for this PCR
494 *
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100495 * Return: code of the operation
Miquel Raynal4c1a5852018-05-15 11:57:16 +0200496 */
Simon Glass8ceca1d2018-11-18 14:22:27 -0700497u32 tpm2_pcr_read(struct udevice *dev, u32 idx, unsigned int idx_min_sz,
Ruchika Gupta686bedb2021-11-29 13:09:45 +0530498 u16 algorithm, void *data, u32 digest_len,
499 unsigned int *updates);
Miquel Raynal4c1a5852018-05-15 11:57:16 +0200500
Miquel Raynal2e52c062018-05-15 11:57:17 +0200501/**
502 * Issue a TPM2_GetCapability command. This implementation is limited
503 * to query property index that is 4-byte wide.
504 *
Simon Glass8ceca1d2018-11-18 14:22:27 -0700505 * @dev TPM device
Miquel Raynal2e52c062018-05-15 11:57:17 +0200506 * @capability Partition of capabilities
507 * @property Further definition of capability, limited to be 4 bytes wide
508 * @buf Output buffer for capability information
509 * @prop_count Size of output buffer
510 *
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100511 * Return: code of the operation
Miquel Raynal2e52c062018-05-15 11:57:17 +0200512 */
Simon Glass8ceca1d2018-11-18 14:22:27 -0700513u32 tpm2_get_capability(struct udevice *dev, u32 capability, u32 property,
514 void *buf, size_t prop_count);
Miquel Raynal2e52c062018-05-15 11:57:17 +0200515
Miquel Raynal228e9902018-05-15 11:57:18 +0200516/**
Eddie James8ed7bb32023-10-24 10:43:49 -0500517 * tpm2_get_pcr_info() - get the supported, active PCRs and number of banks
518 *
519 * @dev: TPM device
Ilias Apalodimascb356612024-06-23 14:48:17 +0300520 * @pcrs: struct tpml_pcr_selection of available PCRs
Eddie James8ed7bb32023-10-24 10:43:49 -0500521 *
522 * @return 0 on success, code of operation or negative errno on failure
523 */
Ilias Apalodimascb356612024-06-23 14:48:17 +0300524int tpm2_get_pcr_info(struct udevice *dev, struct tpml_pcr_selection *pcrs);
Eddie James8ed7bb32023-10-24 10:43:49 -0500525
526/**
Miquel Raynal228e9902018-05-15 11:57:18 +0200527 * Issue a TPM2_DictionaryAttackLockReset command.
528 *
Simon Glass8ceca1d2018-11-18 14:22:27 -0700529 * @dev TPM device
Miquel Raynal228e9902018-05-15 11:57:18 +0200530 * @pw Password
531 * @pw_sz Length of the password
532 *
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100533 * Return: code of the operation
Miquel Raynal228e9902018-05-15 11:57:18 +0200534 */
Simon Glass8ceca1d2018-11-18 14:22:27 -0700535u32 tpm2_dam_reset(struct udevice *dev, const char *pw, const ssize_t pw_sz);
Miquel Raynal228e9902018-05-15 11:57:18 +0200536
537/**
538 * Issue a TPM2_DictionaryAttackParameters command.
539 *
Simon Glass8ceca1d2018-11-18 14:22:27 -0700540 * @dev TPM device
Miquel Raynal228e9902018-05-15 11:57:18 +0200541 * @pw Password
542 * @pw_sz Length of the password
543 * @max_tries Count of authorizations before lockout
544 * @recovery_time Time before decrementation of the failure count
545 * @lockout_recovery Time to wait after a lockout
546 *
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100547 * Return: code of the operation
Miquel Raynal228e9902018-05-15 11:57:18 +0200548 */
Simon Glass8ceca1d2018-11-18 14:22:27 -0700549u32 tpm2_dam_parameters(struct udevice *dev, const char *pw,
550 const ssize_t pw_sz, unsigned int max_tries,
551 unsigned int recovery_time,
Miquel Raynal228e9902018-05-15 11:57:18 +0200552 unsigned int lockout_recovery);
553
Miquel Raynal05d7be32018-05-15 11:57:19 +0200554/**
555 * Issue a TPM2_HierarchyChangeAuth command.
556 *
Simon Glass8ceca1d2018-11-18 14:22:27 -0700557 * @dev TPM device
Miquel Raynal05d7be32018-05-15 11:57:19 +0200558 * @handle Handle
559 * @newpw New password
560 * @newpw_sz Length of the new password
561 * @oldpw Old password
562 * @oldpw_sz Length of the old password
563 *
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100564 * Return: code of the operation
Miquel Raynal05d7be32018-05-15 11:57:19 +0200565 */
Simon Glass8ceca1d2018-11-18 14:22:27 -0700566int tpm2_change_auth(struct udevice *dev, u32 handle, const char *newpw,
567 const ssize_t newpw_sz, const char *oldpw,
568 const ssize_t oldpw_sz);
Miquel Raynal05d7be32018-05-15 11:57:19 +0200569
Miquel Raynal0b864f62018-05-15 11:57:20 +0200570/**
571 * Issue a TPM_PCR_SetAuthPolicy command.
572 *
Simon Glass8ceca1d2018-11-18 14:22:27 -0700573 * @dev TPM device
Miquel Raynal0b864f62018-05-15 11:57:20 +0200574 * @pw Platform password
575 * @pw_sz Length of the password
576 * @index Index of the PCR
577 * @digest New key to access the PCR
578 *
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100579 * Return: code of the operation
Miquel Raynal0b864f62018-05-15 11:57:20 +0200580 */
Simon Glass8ceca1d2018-11-18 14:22:27 -0700581u32 tpm2_pcr_setauthpolicy(struct udevice *dev, const char *pw,
582 const ssize_t pw_sz, u32 index, const char *key);
Miquel Raynal0b864f62018-05-15 11:57:20 +0200583
584/**
585 * Issue a TPM_PCR_SetAuthValue command.
586 *
Simon Glass8ceca1d2018-11-18 14:22:27 -0700587 * @dev TPM device
Miquel Raynal0b864f62018-05-15 11:57:20 +0200588 * @pw Platform password
589 * @pw_sz Length of the password
590 * @index Index of the PCR
591 * @digest New key to access the PCR
592 * @key_sz Length of the new key
593 *
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100594 * Return: code of the operation
Miquel Raynal0b864f62018-05-15 11:57:20 +0200595 */
Simon Glass8ceca1d2018-11-18 14:22:27 -0700596u32 tpm2_pcr_setauthvalue(struct udevice *dev, const char *pw,
597 const ssize_t pw_sz, u32 index, const char *key,
598 const ssize_t key_sz);
Miquel Raynal0b864f62018-05-15 11:57:20 +0200599
Dhananjay Phadke7a2cf2e2020-06-04 16:43:59 -0700600/**
601 * Issue a TPM2_GetRandom command.
602 *
603 * @dev TPM device
604 * @param data output buffer for the random bytes
605 * @param count size of output buffer
606 *
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100607 * Return: return code of the operation
Dhananjay Phadke7a2cf2e2020-06-04 16:43:59 -0700608 */
609u32 tpm2_get_random(struct udevice *dev, void *data, u32 count);
610
Simon Glasse9d3d592021-02-06 14:23:41 -0700611/**
612 * Lock data in the TPM
613 *
614 * Once locked the data cannot be written until after a reboot
615 *
616 * @dev TPM device
617 * @index Index of data to lock
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100618 * Return: code of the operation
Simon Glasse9d3d592021-02-06 14:23:41 -0700619 */
620u32 tpm2_write_lock(struct udevice *dev, u32 index);
621
Simon Glass77759db2021-02-06 14:23:42 -0700622/**
623 * Disable access to any platform data
624 *
625 * This can be called to close off access to the firmware data in the data,
626 * before calling the kernel.
627 *
628 * @dev TPM device
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100629 * Return: code of the operation
Simon Glass77759db2021-02-06 14:23:42 -0700630 */
631u32 tpm2_disable_platform_hierarchy(struct udevice *dev);
632
Masahisa Kojima06ef6b62021-11-04 22:59:16 +0900633/**
634 * submit user specified data to the TPM and get response
635 *
636 * @dev TPM device
637 * @sendbuf: Buffer of the data to send
638 * @recvbuf: Buffer to save the response to
639 * @recv_size: Pointer to the size of the response buffer
640 *
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100641 * Return: code of the operation
Masahisa Kojima06ef6b62021-11-04 22:59:16 +0900642 */
643u32 tpm2_submit_command(struct udevice *dev, const u8 *sendbuf,
644 u8 *recvbuf, size_t *recv_size);
645
Simon Glass3f7a73a2022-08-30 21:05:37 -0600646/**
647 * tpm_cr50_report_state() - Report the Cr50 internal state
648 *
649 * @dev: TPM device
650 * @vendor_cmd: Vendor command number to send
651 * @vendor_subcmd: Vendor sub-command number to send
652 * @recvbuf: Buffer to save the response to
653 * @recv_size: Pointer to the size of the response buffer
654 * Return: result of the operation
655 */
656u32 tpm2_report_state(struct udevice *dev, uint vendor_cmd, uint vendor_subcmd,
657 u8 *recvbuf, size_t *recv_size);
658
Simon Glass3564b8e2022-08-30 21:05:38 -0600659/**
660 * tpm2_enable_nvcommits() - Tell TPM to commit NV data immediately
661 *
662 * For Chromium OS verified boot, we may reboot or reset at different times,
663 * possibly leaving non-volatile data unwritten by the TPM.
664 *
665 * This vendor command is used to indicate that non-volatile data should be
666 * written to its store immediately.
667 *
668 * @dev TPM device
669 * @vendor_cmd: Vendor command number to send
670 * @vendor_subcmd: Vendor sub-command number to send
671 * Return: result of the operation
672 */
673u32 tpm2_enable_nvcommits(struct udevice *dev, uint vendor_cmd,
674 uint vendor_subcmd);
675
Ilias Apalodimas42d7bdf2023-01-25 12:18:36 +0200676/**
677 * tpm2_auto_start() - start up the TPM and perform selftests.
678 * If a testable function has not been tested and is
679 * requested the TPM2 will return TPM_RC_NEEDS_TEST.
680 *
681 * @param dev TPM device
682 * Return: TPM2_RC_TESTING, if TPM2 self-test is in progress.
683 * TPM2_RC_SUCCESS, if testing of all functions is complete without
684 * functional failures.
685 * TPM2_RC_FAILURE, if any test failed.
686 * TPM2_RC_INITIALIZE, if the TPM has not gone through the Startup
687 * sequence
688
689 */
690u32 tpm2_auto_start(struct udevice *dev);
691
Tim Harvey6ea1e052024-05-25 13:00:48 -0700692/**
693 * tpm2_name_to_algorithm() - Return an algorithm id given a supported
694 * algorithm name
695 *
696 * @name: algorithm name
697 * Return: enum tpm2_algorithms or -EINVAL
698 */
699enum tpm2_algorithms tpm2_name_to_algorithm(const char *name);
700
701/**
702 * tpm2_algorithm_name() - Return an algorithm name string for a
703 * supported algorithm id
704 *
705 * @algorithm_id: algorithm defined in enum tpm2_algorithms
706 * Return: algorithm name string or ""
707 */
708const char *tpm2_algorithm_name(enum tpm2_algorithms);
709
Ilias Apalodimascb356612024-06-23 14:48:17 +0300710/**
Raymond Mao43158122024-12-24 08:01:07 -0800711 * tpm2_algorithm_supported() - Check if the algorithm supported by U-Boot
712 *
713 * @algorithm_id: algorithm defined in enum tpm2_algorithms
714 * Return: true if supported, otherwise false
715 */
716bool tpm2_algorithm_supported(enum tpm2_algorithms algo);
717
718/**
Ilias Apalodimas1e665f92024-06-23 14:48:18 +0300719 * tpm2_algorithm_to_len() - Return an algorithm length for supported algorithm id
720 *
721 * @algorithm_id: algorithm defined in enum tpm2_algorithms
722 * Return: len or 0 if not supported
723 */
724u16 tpm2_algorithm_to_len(enum tpm2_algorithms algo);
725
726/*
727 * When measured boot is enabled via EFI or bootX commands all the algorithms
728 * above are selected by our Kconfigs. Due to U-Boots nature of being small there
729 * are cases where we need some functionality from the TPM -- e.g storage or RNG
730 * but we don't want to support measurements.
731 *
732 * The choice of hash algorithms are determined by the platform and the TPM
733 * configuration. Failing to cap a PCR in a bank which the platform left
734 * active is a security vulnerability. It permits the unsealing of secrets
735 * if an attacker can replay a good set of measurements into an unused bank.
736 *
737 * On top of that a previous stage bootloader (e.g TF-A), migh pass an eventlog
738 * since it doesn't have a TPM driver, which U-Boot needs to replace. The algorit h
739 * choice is a compile time option in that case and we need to make sure we conform.
740 *
741 * Add a variable here that sums the supported algorithms U-Boot was compiled
742 * with so we can refuse to do measurements if we don't support all of them
743 */
744
745/**
Ilias Apalodimasd788b062024-12-24 08:01:05 -0800746 * tpm2_check_active_banks() - Check if the active PCR banks are supported by
747 * our configuration
Ilias Apalodimas1e665f92024-06-23 14:48:18 +0300748 *
749 * @dev: TPM device
750 * Return: true if allowed
751 */
Ilias Apalodimasd788b062024-12-24 08:01:05 -0800752bool tpm2_check_active_banks(struct udevice *dev);
Ilias Apalodimas1e665f92024-06-23 14:48:18 +0300753
754/**
Ilias Apalodimas9465f7a2024-12-24 08:01:04 -0800755 * tpm2_is_active_bank() - check the pcr_select. If at least one of the PCRs
756 * supports the algorithm add it on the active ones
Ilias Apalodimascb356612024-06-23 14:48:17 +0300757 *
758 * @selection: PCR selection structure
759 * Return: True if the algorithm is active
760 */
Ilias Apalodimas9465f7a2024-12-24 08:01:04 -0800761bool tpm2_is_active_bank(struct tpms_pcr_selection *selection);
Ilias Apalodimascb356612024-06-23 14:48:17 +0300762
Ilias Apalodimas7b1e5222024-12-24 08:01:08 -0800763/**
764 * tpm2_print_active_banks() - Print the active TPM PCRs
765 *
766 * @dev: TPM device
767 */
768void tpm2_print_active_banks(struct udevice *dev);
769
Miquel Raynalf3b43502018-05-15 11:57:08 +0200770#endif /* __TPM_V2_H */