blob: f66a8e1bf41f44c84331db3758a468a630c100f2 [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 *
Heinrich Schuchardt4fc65092024-12-27 14:25:41 +02009 * The structures are described in
10 * Trusted Platform Module Library Part 2: Structures
11 * http://tcg.tjn.chef.causewaynow.com/resource/tpm-library-specification/
12 *
13 * C header files are listed in
Ilias Apalodimasf4e05902020-11-11 11:18:10 +020014 * https://trustedcomputinggroup.org/resource/tss-overview-common-structures-specification/
15 *
Miquel Raynalf3b43502018-05-15 11:57:08 +020016 * Author: Miquel Raynal <miquel.raynal@bootlin.com>
17 */
18
19#ifndef __TPM_V2_H
20#define __TPM_V2_H
21
22#include <tpm-common.h>
23
Simon Glass3ba929a2020-10-30 21:38:53 -060024struct udevice;
25
Miquel Raynalf3b43502018-05-15 11:57:08 +020026#define TPM2_DIGEST_LEN 32
27
Ilias Apalodimascae28ef2020-11-30 11:47:39 +020028#define TPM2_SHA1_DIGEST_SIZE 20
29#define TPM2_SHA256_DIGEST_SIZE 32
30#define TPM2_SHA384_DIGEST_SIZE 48
31#define TPM2_SHA512_DIGEST_SIZE 64
32#define TPM2_SM3_256_DIGEST_SIZE 32
33
Ilias Apalodimasf4e05902020-11-11 11:18:10 +020034#define TPM2_MAX_PCRS 32
35#define TPM2_PCR_SELECT_MAX ((TPM2_MAX_PCRS + 7) / 8)
36#define TPM2_MAX_CAP_BUFFER 1024
37#define TPM2_MAX_TPM_PROPERTIES ((TPM2_MAX_CAP_BUFFER - sizeof(u32) /* TPM2_CAP */ - \
38 sizeof(u32)) / sizeof(struct tpms_tagged_property))
39
Simon Glassca31f072021-07-18 14:18:03 -060040#define TPM2_HDR_LEN 10
41
Ilias Apalodimasf4e05902020-11-11 11:18:10 +020042#define TPM2_CAP_PCRS 0x00000005U
43#define TPM2_CAP_TPM_PROPERTIES 0x00000006U
44
45/* Definition of (UINT32) TPM2_PT Constants */
46#define TPM2_PT_GROUP (u32)(0x00000100)
47#define TPM2_PT_FIXED (u32)(TPM2_PT_GROUP * 1)
48#define TPM2_PT_MANUFACTURER (u32)(TPM2_PT_FIXED + 5)
49#define TPM2_PT_PCR_COUNT (u32)(TPM2_PT_FIXED + 18)
50#define TPM2_PT_MAX_COMMAND_SIZE (u32)(TPM2_PT_FIXED + 30)
51#define TPM2_PT_MAX_RESPONSE_SIZE (u32)(TPM2_PT_FIXED + 31)
52
Heinrich Schuchardt4fc65092024-12-27 14:25:41 +020053/**
54 * struct tpms_tagged_property - TPMS_TAGGED_PROPERTY structure
55 *
56 * This structure is returned by TPM2_GetCapability() to report
57 * a u32 property value.
58 *
59 * @property: property identifier
60 * @value: value of the property
61 */
Ilias Apalodimasf4e05902020-11-11 11:18:10 +020062struct tpms_tagged_property {
63 u32 property;
64 u32 value;
65} __packed;
66
Heinrich Schuchardt4fc65092024-12-27 14:25:41 +020067/**
68 * struct tpms_pcr_selection - TPMS_PCR_SELECTION structure
69 *
70 * This structure allows to specify a hash algorithm and a list of
71 * selected PCRs. A PCR is selected by setting the related bit in
72 * @pcr_select to 1.
73 *
74 * @hash: hash algorithm associated with the selection
75 * @size_of_select: size in bytes of the @pcr_select array
76 * @pcr_select: bit map of selected PCRs
77 */
Ilias Apalodimasf4e05902020-11-11 11:18:10 +020078struct tpms_pcr_selection {
79 u16 hash;
80 u8 size_of_select;
81 u8 pcr_select[TPM2_PCR_SELECT_MAX];
82} __packed;
83
Heinrich Schuchardt4fc65092024-12-27 14:25:41 +020084/**
85 * struct tpml_pcr_selection - TPML_PCR_SELECTION structure
86 *
87 * @count: number of selection structures, may be zero
88 * @selection: list of selections
89 */
Ilias Apalodimasf4e05902020-11-11 11:18:10 +020090struct tpml_pcr_selection {
91 u32 count;
92 struct tpms_pcr_selection selection[TPM2_NUM_PCR_BANKS];
93} __packed;
94
95/* TPML_TAGGED_TPM_PROPERTY Structure */
96struct tpml_tagged_tpm_property {
97 u32 count;
98 struct tpms_tagged_property tpm_property[TPM2_MAX_TPM_PROPERTIES];
99} __packed;
100
101/* TPMU_CAPABILITIES Union */
102union tpmu_capabilities {
103 /*
104 * Non exhaustive. Only added the structs needed for our
105 * current code
106 */
107 struct tpml_pcr_selection assigned_pcr;
108 struct tpml_tagged_tpm_property tpm_properties;
109} __packed;
110
111/* TPMS_CAPABILITY_DATA Structure */
112struct tpms_capability_data {
113 u32 capability;
114 union tpmu_capabilities data;
115} __packed;
116
Miquel Raynalf3b43502018-05-15 11:57:08 +0200117/**
Ilias Apalodimascae28ef2020-11-30 11:47:39 +0200118 * Definition of TPMU_HA Union
119 */
Eddie James90b6c862023-10-24 10:43:47 -0500120union tpmu_ha {
Ilias Apalodimascae28ef2020-11-30 11:47:39 +0200121 u8 sha1[TPM2_SHA1_DIGEST_SIZE];
122 u8 sha256[TPM2_SHA256_DIGEST_SIZE];
123 u8 sm3_256[TPM2_SM3_256_DIGEST_SIZE];
124 u8 sha384[TPM2_SHA384_DIGEST_SIZE];
125 u8 sha512[TPM2_SHA512_DIGEST_SIZE];
126} __packed;
127
128/**
129 * Definition of TPMT_HA Structure
130 *
131 * @hash_alg: Hash algorithm defined in enum tpm2_algorithms
132 * @digest: Digest value for a given algorithm
133 */
134struct tpmt_ha {
135 u16 hash_alg;
Eddie James90b6c862023-10-24 10:43:47 -0500136 union tpmu_ha digest;
Ilias Apalodimascae28ef2020-11-30 11:47:39 +0200137} __packed;
138
139/**
140 * Definition of TPML_DIGEST_VALUES Structure
141 *
142 * @count: Number of algorithms supported by hardware
143 * @digests: struct for algorithm id and hash value
144 */
145struct tpml_digest_values {
146 u32 count;
147 struct tpmt_ha digests[TPM2_NUM_PCR_BANKS];
148} __packed;
149
150/**
Miquel Raynalf3b43502018-05-15 11:57:08 +0200151 * TPM2 Structure Tags for command/response buffers.
152 *
153 * @TPM2_ST_NO_SESSIONS: the command does not need an authentication.
154 * @TPM2_ST_SESSIONS: the command needs an authentication.
155 */
156enum tpm2_structures {
157 TPM2_ST_NO_SESSIONS = 0x8001,
158 TPM2_ST_SESSIONS = 0x8002,
159};
160
161/**
162 * TPM2 type of boolean.
163 */
164enum tpm2_yes_no {
165 TPMI_YES = 1,
166 TPMI_NO = 0,
167};
168
169/**
170 * TPM2 startup values.
171 *
172 * @TPM2_SU_CLEAR: reset the internal state.
173 * @TPM2_SU_STATE: restore saved state (if any).
174 */
175enum tpm2_startup_types {
176 TPM2_SU_CLEAR = 0x0000,
177 TPM2_SU_STATE = 0x0001,
178};
179
180/**
181 * TPM2 permanent handles.
182 *
183 * @TPM2_RH_OWNER: refers to the 'owner' hierarchy.
184 * @TPM2_RS_PW: indicates a password.
185 * @TPM2_RH_LOCKOUT: refers to the 'lockout' hierarchy.
186 * @TPM2_RH_ENDORSEMENT: refers to the 'endorsement' hierarchy.
187 * @TPM2_RH_PLATFORM: refers to the 'platform' hierarchy.
188 */
189enum tpm2_handles {
190 TPM2_RH_OWNER = 0x40000001,
191 TPM2_RS_PW = 0x40000009,
192 TPM2_RH_LOCKOUT = 0x4000000A,
193 TPM2_RH_ENDORSEMENT = 0x4000000B,
194 TPM2_RH_PLATFORM = 0x4000000C,
195};
196
197/**
198 * TPM2 command codes used at the beginning of a buffer, gives the command.
199 *
200 * @TPM2_CC_STARTUP: TPM2_Startup().
201 * @TPM2_CC_SELF_TEST: TPM2_SelfTest().
202 * @TPM2_CC_CLEAR: TPM2_Clear().
203 * @TPM2_CC_CLEARCONTROL: TPM2_ClearControl().
204 * @TPM2_CC_HIERCHANGEAUTH: TPM2_HierarchyChangeAuth().
205 * @TPM2_CC_PCR_SETAUTHPOL: TPM2_PCR_SetAuthPolicy().
206 * @TPM2_CC_DAM_RESET: TPM2_DictionaryAttackLockReset().
207 * @TPM2_CC_DAM_PARAMETERS: TPM2_DictionaryAttackParameters().
208 * @TPM2_CC_GET_CAPABILITY: TPM2_GetCapibility().
Dhananjay Phadke7a2cf2e2020-06-04 16:43:59 -0700209 * @TPM2_CC_GET_RANDOM: TPM2_GetRandom().
Miquel Raynalf3b43502018-05-15 11:57:08 +0200210 * @TPM2_CC_PCR_READ: TPM2_PCR_Read().
211 * @TPM2_CC_PCR_EXTEND: TPM2_PCR_Extend().
212 * @TPM2_CC_PCR_SETAUTHVAL: TPM2_PCR_SetAuthValue().
213 */
214enum tpm2_command_codes {
215 TPM2_CC_STARTUP = 0x0144,
216 TPM2_CC_SELF_TEST = 0x0143,
Simon Glass77759db2021-02-06 14:23:42 -0700217 TPM2_CC_HIER_CONTROL = 0x0121,
Miquel Raynalf3b43502018-05-15 11:57:08 +0200218 TPM2_CC_CLEAR = 0x0126,
219 TPM2_CC_CLEARCONTROL = 0x0127,
220 TPM2_CC_HIERCHANGEAUTH = 0x0129,
Simon Glass713c58a2021-02-06 14:23:39 -0700221 TPM2_CC_NV_DEFINE_SPACE = 0x012a,
Miquel Raynal0b864f62018-05-15 11:57:20 +0200222 TPM2_CC_PCR_SETAUTHPOL = 0x012C,
Simon Glass3d930ed2021-02-06 14:23:40 -0700223 TPM2_CC_NV_WRITE = 0x0137,
Simon Glasse9d3d592021-02-06 14:23:41 -0700224 TPM2_CC_NV_WRITELOCK = 0x0138,
Miquel Raynalf3b43502018-05-15 11:57:08 +0200225 TPM2_CC_DAM_RESET = 0x0139,
226 TPM2_CC_DAM_PARAMETERS = 0x013A,
Simon Glass5ff3f162018-10-01 11:55:17 -0600227 TPM2_CC_NV_READ = 0x014E,
Miquel Raynalf3b43502018-05-15 11:57:08 +0200228 TPM2_CC_GET_CAPABILITY = 0x017A,
Dhananjay Phadke7a2cf2e2020-06-04 16:43:59 -0700229 TPM2_CC_GET_RANDOM = 0x017B,
Miquel Raynalf3b43502018-05-15 11:57:08 +0200230 TPM2_CC_PCR_READ = 0x017E,
231 TPM2_CC_PCR_EXTEND = 0x0182,
Miquel Raynal0b864f62018-05-15 11:57:20 +0200232 TPM2_CC_PCR_SETAUTHVAL = 0x0183,
Raymond Mao5187a642025-01-27 06:58:46 -0800233 TPM2_CC_SHUTDOWN = 0x0145,
Miquel Raynalf3b43502018-05-15 11:57:08 +0200234};
235
236/**
237 * TPM2 return codes.
238 */
239enum tpm2_return_codes {
240 TPM2_RC_SUCCESS = 0x0000,
241 TPM2_RC_BAD_TAG = 0x001E,
242 TPM2_RC_FMT1 = 0x0080,
243 TPM2_RC_HASH = TPM2_RC_FMT1 + 0x0003,
244 TPM2_RC_VALUE = TPM2_RC_FMT1 + 0x0004,
245 TPM2_RC_SIZE = TPM2_RC_FMT1 + 0x0015,
246 TPM2_RC_BAD_AUTH = TPM2_RC_FMT1 + 0x0022,
247 TPM2_RC_HANDLE = TPM2_RC_FMT1 + 0x000B,
248 TPM2_RC_VER1 = 0x0100,
249 TPM2_RC_INITIALIZE = TPM2_RC_VER1 + 0x0000,
250 TPM2_RC_FAILURE = TPM2_RC_VER1 + 0x0001,
251 TPM2_RC_DISABLED = TPM2_RC_VER1 + 0x0020,
252 TPM2_RC_AUTH_MISSING = TPM2_RC_VER1 + 0x0025,
253 TPM2_RC_COMMAND_CODE = TPM2_RC_VER1 + 0x0043,
254 TPM2_RC_AUTHSIZE = TPM2_RC_VER1 + 0x0044,
255 TPM2_RC_AUTH_CONTEXT = TPM2_RC_VER1 + 0x0045,
Simon Glass77759db2021-02-06 14:23:42 -0700256 TPM2_RC_NV_DEFINED = TPM2_RC_VER1 + 0x004c,
Miquel Raynalf3b43502018-05-15 11:57:08 +0200257 TPM2_RC_NEEDS_TEST = TPM2_RC_VER1 + 0x0053,
258 TPM2_RC_WARN = 0x0900,
259 TPM2_RC_TESTING = TPM2_RC_WARN + 0x000A,
260 TPM2_RC_REFERENCE_H0 = TPM2_RC_WARN + 0x0010,
261 TPM2_RC_LOCKOUT = TPM2_RC_WARN + 0x0021,
262};
263
264/**
265 * TPM2 algorithms.
266 */
267enum tpm2_algorithms {
Ilias Apalodimasf4e05902020-11-11 11:18:10 +0200268 TPM2_ALG_SHA1 = 0x04,
Miquel Raynalf3b43502018-05-15 11:57:08 +0200269 TPM2_ALG_XOR = 0x0A,
270 TPM2_ALG_SHA256 = 0x0B,
271 TPM2_ALG_SHA384 = 0x0C,
272 TPM2_ALG_SHA512 = 0x0D,
273 TPM2_ALG_NULL = 0x10,
Ilias Apalodimasf4e05902020-11-11 11:18:10 +0200274 TPM2_ALG_SM3_256 = 0x12,
Miquel Raynalf3b43502018-05-15 11:57:08 +0200275};
276
Tim Harvey6ea1e052024-05-25 13:00:48 -0700277/**
278 * struct digest_info - details of supported digests
279 *
280 * @hash_name: hash name
281 * @hash_alg: hash algorithm id
282 * @hash_mask: hash registry mask
283 * @hash_len: hash digest length
284 */
285struct digest_info {
286 const char *hash_name;
287 u16 hash_alg;
288 u32 hash_mask;
289 u16 hash_len;
Raymond Mao43158122024-12-24 08:01:07 -0800290 bool supported;
Tim Harvey6ea1e052024-05-25 13:00:48 -0700291};
292
293/* Algorithm Registry */
294#define TCG2_BOOT_HASH_ALG_SHA1 0x00000001
295#define TCG2_BOOT_HASH_ALG_SHA256 0x00000002
296#define TCG2_BOOT_HASH_ALG_SHA384 0x00000004
297#define TCG2_BOOT_HASH_ALG_SHA512 0x00000008
298#define TCG2_BOOT_HASH_ALG_SM3_256 0x00000010
299
300static const struct digest_info hash_algo_list[] = {
301 {
302 "sha1",
303 TPM2_ALG_SHA1,
304 TCG2_BOOT_HASH_ALG_SHA1,
305 TPM2_SHA1_DIGEST_SIZE,
Raymond Mao43158122024-12-24 08:01:07 -0800306#if IS_ENABLED(CONFIG_SHA1)
307 true,
308#else
309 false,
Ilias Apalodimas1e665f92024-06-23 14:48:18 +0300310#endif
Raymond Mao43158122024-12-24 08:01:07 -0800311 },
Tim Harvey6ea1e052024-05-25 13:00:48 -0700312 {
313 "sha256",
314 TPM2_ALG_SHA256,
315 TCG2_BOOT_HASH_ALG_SHA256,
316 TPM2_SHA256_DIGEST_SIZE,
Raymond Mao43158122024-12-24 08:01:07 -0800317#if IS_ENABLED(CONFIG_SHA256)
318 true,
319#else
320 false,
Ilias Apalodimas1e665f92024-06-23 14:48:18 +0300321#endif
Raymond Mao43158122024-12-24 08:01:07 -0800322 },
Tim Harvey6ea1e052024-05-25 13:00:48 -0700323 {
324 "sha384",
325 TPM2_ALG_SHA384,
326 TCG2_BOOT_HASH_ALG_SHA384,
327 TPM2_SHA384_DIGEST_SIZE,
Raymond Mao43158122024-12-24 08:01:07 -0800328#if IS_ENABLED(CONFIG_SHA384)
329 true,
330#else
331 false,
Ilias Apalodimas1e665f92024-06-23 14:48:18 +0300332#endif
Raymond Mao43158122024-12-24 08:01:07 -0800333 },
Tim Harvey6ea1e052024-05-25 13:00:48 -0700334 {
335 "sha512",
336 TPM2_ALG_SHA512,
337 TCG2_BOOT_HASH_ALG_SHA512,
338 TPM2_SHA512_DIGEST_SIZE,
Raymond Mao43158122024-12-24 08:01:07 -0800339#if IS_ENABLED(CONFIG_SHA512)
340 true,
341#else
342 false,
Ilias Apalodimas1e665f92024-06-23 14:48:18 +0300343#endif
Raymond Mao43158122024-12-24 08:01:07 -0800344 },
Tim Harvey6ea1e052024-05-25 13:00:48 -0700345};
Eddie James8ed7bb32023-10-24 10:43:49 -0500346
Simon Glassb4ebd1f2018-11-23 21:29:34 -0700347/* NV index attributes */
348enum tpm_index_attrs {
349 TPMA_NV_PPWRITE = 1UL << 0,
350 TPMA_NV_OWNERWRITE = 1UL << 1,
351 TPMA_NV_AUTHWRITE = 1UL << 2,
352 TPMA_NV_POLICYWRITE = 1UL << 3,
353 TPMA_NV_COUNTER = 1UL << 4,
354 TPMA_NV_BITS = 1UL << 5,
355 TPMA_NV_EXTEND = 1UL << 6,
356 TPMA_NV_POLICY_DELETE = 1UL << 10,
357 TPMA_NV_WRITELOCKED = 1UL << 11,
358 TPMA_NV_WRITEALL = 1UL << 12,
359 TPMA_NV_WRITEDEFINE = 1UL << 13,
360 TPMA_NV_WRITE_STCLEAR = 1UL << 14,
361 TPMA_NV_GLOBALLOCK = 1UL << 15,
362 TPMA_NV_PPREAD = 1UL << 16,
363 TPMA_NV_OWNERREAD = 1UL << 17,
364 TPMA_NV_AUTHREAD = 1UL << 18,
365 TPMA_NV_POLICYREAD = 1UL << 19,
366 TPMA_NV_NO_DA = 1UL << 25,
367 TPMA_NV_ORDERLY = 1UL << 26,
368 TPMA_NV_CLEAR_STCLEAR = 1UL << 27,
369 TPMA_NV_READLOCKED = 1UL << 28,
370 TPMA_NV_WRITTEN = 1UL << 29,
371 TPMA_NV_PLATFORMCREATE = 1UL << 30,
372 TPMA_NV_READ_STCLEAR = 1UL << 31,
373
374 TPMA_NV_MASK_READ = TPMA_NV_PPREAD | TPMA_NV_OWNERREAD |
375 TPMA_NV_AUTHREAD | TPMA_NV_POLICYREAD,
376 TPMA_NV_MASK_WRITE = TPMA_NV_PPWRITE | TPMA_NV_OWNERWRITE |
377 TPMA_NV_AUTHWRITE | TPMA_NV_POLICYWRITE,
378};
379
Simon Glasse1ed0ec2020-02-06 09:55:03 -0700380enum {
381 TPM_ACCESS_VALID = 1 << 7,
382 TPM_ACCESS_ACTIVE_LOCALITY = 1 << 5,
383 TPM_ACCESS_REQUEST_PENDING = 1 << 2,
384 TPM_ACCESS_REQUEST_USE = 1 << 1,
385 TPM_ACCESS_ESTABLISHMENT = 1 << 0,
386};
387
388enum {
389 TPM_STS_FAMILY_SHIFT = 26,
390 TPM_STS_FAMILY_MASK = 0x3 << TPM_STS_FAMILY_SHIFT,
391 TPM_STS_FAMILY_TPM2 = 1 << TPM_STS_FAMILY_SHIFT,
392 TPM_STS_RESE_TESTABLISMENT_BIT = 1 << 25,
393 TPM_STS_COMMAND_CANCEL = 1 << 24,
394 TPM_STS_BURST_COUNT_SHIFT = 8,
395 TPM_STS_BURST_COUNT_MASK = 0xffff << TPM_STS_BURST_COUNT_SHIFT,
396 TPM_STS_VALID = 1 << 7,
397 TPM_STS_COMMAND_READY = 1 << 6,
398 TPM_STS_GO = 1 << 5,
399 TPM_STS_DATA_AVAIL = 1 << 4,
400 TPM_STS_DATA_EXPECT = 1 << 3,
401 TPM_STS_SELF_TEST_DONE = 1 << 2,
402 TPM_STS_RESPONSE_RETRY = 1 << 1,
Ilias Apalodimas97f5e2d2021-11-09 09:02:17 +0200403 TPM_STS_READ_ZERO = 0x23
Simon Glasse1ed0ec2020-02-06 09:55:03 -0700404};
405
406enum {
407 TPM_CMD_COUNT_OFFSET = 2,
408 TPM_CMD_ORDINAL_OFFSET = 6,
409 TPM_MAX_BUF_SIZE = 1260,
410};
411
Simon Glass3d930ed2021-02-06 14:23:40 -0700412enum {
413 /* Secure storage for firmware settings */
414 TPM_HT_PCR = 0,
415 TPM_HT_NV_INDEX,
416 TPM_HT_HMAC_SESSION,
417 TPM_HT_POLICY_SESSION,
418
419 HR_SHIFT = 24,
420 HR_PCR = TPM_HT_PCR << HR_SHIFT,
421 HR_HMAC_SESSION = TPM_HT_HMAC_SESSION << HR_SHIFT,
422 HR_POLICY_SESSION = TPM_HT_POLICY_SESSION << HR_SHIFT,
423 HR_NV_INDEX = TPM_HT_NV_INDEX << HR_SHIFT,
424};
425
Miquel Raynal65a1a6c2018-05-15 11:57:12 +0200426/**
427 * Issue a TPM2_Startup command.
428 *
Simon Glass8ceca1d2018-11-18 14:22:27 -0700429 * @dev TPM device
Miquel Raynal65a1a6c2018-05-15 11:57:12 +0200430 * @mode TPM startup mode
431 *
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100432 * Return: code of the operation
Miquel Raynal65a1a6c2018-05-15 11:57:12 +0200433 */
Raymond Mao5187a642025-01-27 06:58:46 -0800434u32 tpm2_startup(struct udevice *dev, bool onoff, enum tpm2_startup_types mode);
Miquel Raynal65a1a6c2018-05-15 11:57:12 +0200435
Miquel Raynal39c76082018-05-15 11:57:13 +0200436/**
437 * Issue a TPM2_SelfTest command.
438 *
Simon Glass8ceca1d2018-11-18 14:22:27 -0700439 * @dev TPM device
Miquel Raynal39c76082018-05-15 11:57:13 +0200440 * @full_test Asking to perform all tests or only the untested ones
441 *
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100442 * Return: code of the operation
Miquel Raynal39c76082018-05-15 11:57:13 +0200443 */
Simon Glass8ceca1d2018-11-18 14:22:27 -0700444u32 tpm2_self_test(struct udevice *dev, enum tpm2_yes_no full_test);
Miquel Raynal39c76082018-05-15 11:57:13 +0200445
Miquel Raynal8df6f8d2018-05-15 11:57:14 +0200446/**
447 * Issue a TPM2_Clear command.
448 *
Simon Glass8ceca1d2018-11-18 14:22:27 -0700449 * @dev TPM device
Miquel Raynal8df6f8d2018-05-15 11:57:14 +0200450 * @handle Handle
451 * @pw Password
452 * @pw_sz Length of the password
453 *
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100454 * Return: code of the operation
Miquel Raynal8df6f8d2018-05-15 11:57:14 +0200455 */
Simon Glass8ceca1d2018-11-18 14:22:27 -0700456u32 tpm2_clear(struct udevice *dev, u32 handle, const char *pw,
457 const ssize_t pw_sz);
Miquel Raynal8df6f8d2018-05-15 11:57:14 +0200458
Miquel Raynal14d72352018-05-15 11:57:15 +0200459/**
Simon Glass713c58a2021-02-06 14:23:39 -0700460 * Issue a TPM_NV_DefineSpace command
461 *
462 * This allows a space to be defined with given attributes and policy
463 *
464 * @dev TPM device
465 * @space_index index of the area
466 * @space_size size of area in bytes
467 * @nv_attributes TPM_NV_ATTRIBUTES of the area
468 * @nv_policy policy to use
469 * @nv_policy_size size of the policy
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100470 * Return: return code of the operation
Simon Glass713c58a2021-02-06 14:23:39 -0700471 */
472u32 tpm2_nv_define_space(struct udevice *dev, u32 space_index,
473 size_t space_size, u32 nv_attributes,
474 const u8 *nv_policy, size_t nv_policy_size);
475
476/**
Miquel Raynal14d72352018-05-15 11:57:15 +0200477 * Issue a TPM2_PCR_Extend command.
478 *
Simon Glass8ceca1d2018-11-18 14:22:27 -0700479 * @dev TPM device
Miquel Raynal14d72352018-05-15 11:57:15 +0200480 * @index Index of the PCR
Ilias Apalodimas7f59c712020-11-26 23:07:22 +0200481 * @algorithm Algorithm used, defined in 'enum tpm2_algorithms'
Miquel Raynal14d72352018-05-15 11:57:15 +0200482 * @digest Value representing the event to be recorded
Ilias Apalodimas7f59c712020-11-26 23:07:22 +0200483 * @digest_len len of the hash
Miquel Raynal14d72352018-05-15 11:57:15 +0200484 *
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100485 * Return: code of the operation
Miquel Raynal14d72352018-05-15 11:57:15 +0200486 */
Ilias Apalodimas7f59c712020-11-26 23:07:22 +0200487u32 tpm2_pcr_extend(struct udevice *dev, u32 index, u32 algorithm,
488 const u8 *digest, u32 digest_len);
Miquel Raynal14d72352018-05-15 11:57:15 +0200489
Miquel Raynal4c1a5852018-05-15 11:57:16 +0200490/**
Simon Glass3d930ed2021-02-06 14:23:40 -0700491 * Read data from the secure storage
492 *
493 * @dev TPM device
494 * @index Index of data to read
495 * @data Place to put data
496 * @count Number of bytes of data
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100497 * Return: code of the operation
Simon Glass3d930ed2021-02-06 14:23:40 -0700498 */
499u32 tpm2_nv_read_value(struct udevice *dev, u32 index, void *data, u32 count);
500
501/**
502 * Write data to the secure storage
503 *
504 * @dev TPM device
505 * @index Index of data to write
506 * @data Data to write
507 * @count Number of bytes of data
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100508 * Return: code of the operation
Simon Glass3d930ed2021-02-06 14:23:40 -0700509 */
510u32 tpm2_nv_write_value(struct udevice *dev, u32 index, const void *data,
511 u32 count);
512
513/**
Miquel Raynal4c1a5852018-05-15 11:57:16 +0200514 * Issue a TPM2_PCR_Read command.
515 *
Simon Glass8ceca1d2018-11-18 14:22:27 -0700516 * @dev TPM device
Miquel Raynal4c1a5852018-05-15 11:57:16 +0200517 * @idx Index of the PCR
518 * @idx_min_sz Minimum size in bytes of the pcrSelect array
Ruchika Gupta686bedb2021-11-29 13:09:45 +0530519 * @algorithm Algorithm used, defined in 'enum tpm2_algorithms'
Miquel Raynal4c1a5852018-05-15 11:57:16 +0200520 * @data Output buffer for contents of the named PCR
Ruchika Gupta686bedb2021-11-29 13:09:45 +0530521 * @digest_len len of the data
Miquel Raynal4c1a5852018-05-15 11:57:16 +0200522 * @updates Optional out parameter: number of updates for this PCR
523 *
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100524 * Return: code of the operation
Miquel Raynal4c1a5852018-05-15 11:57:16 +0200525 */
Simon Glass8ceca1d2018-11-18 14:22:27 -0700526u32 tpm2_pcr_read(struct udevice *dev, u32 idx, unsigned int idx_min_sz,
Ruchika Gupta686bedb2021-11-29 13:09:45 +0530527 u16 algorithm, void *data, u32 digest_len,
528 unsigned int *updates);
Miquel Raynal4c1a5852018-05-15 11:57:16 +0200529
Miquel Raynal2e52c062018-05-15 11:57:17 +0200530/**
531 * Issue a TPM2_GetCapability command. This implementation is limited
532 * to query property index that is 4-byte wide.
533 *
Simon Glass8ceca1d2018-11-18 14:22:27 -0700534 * @dev TPM device
Miquel Raynal2e52c062018-05-15 11:57:17 +0200535 * @capability Partition of capabilities
536 * @property Further definition of capability, limited to be 4 bytes wide
537 * @buf Output buffer for capability information
538 * @prop_count Size of output buffer
539 *
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100540 * Return: code of the operation
Miquel Raynal2e52c062018-05-15 11:57:17 +0200541 */
Simon Glass8ceca1d2018-11-18 14:22:27 -0700542u32 tpm2_get_capability(struct udevice *dev, u32 capability, u32 property,
543 void *buf, size_t prop_count);
Miquel Raynal2e52c062018-05-15 11:57:17 +0200544
Miquel Raynal228e9902018-05-15 11:57:18 +0200545/**
Eddie James8ed7bb32023-10-24 10:43:49 -0500546 * tpm2_get_pcr_info() - get the supported, active PCRs and number of banks
547 *
548 * @dev: TPM device
Ilias Apalodimascb356612024-06-23 14:48:17 +0300549 * @pcrs: struct tpml_pcr_selection of available PCRs
Eddie James8ed7bb32023-10-24 10:43:49 -0500550 *
551 * @return 0 on success, code of operation or negative errno on failure
552 */
Ilias Apalodimascb356612024-06-23 14:48:17 +0300553int tpm2_get_pcr_info(struct udevice *dev, struct tpml_pcr_selection *pcrs);
Eddie James8ed7bb32023-10-24 10:43:49 -0500554
555/**
Miquel Raynal228e9902018-05-15 11:57:18 +0200556 * Issue a TPM2_DictionaryAttackLockReset command.
557 *
Simon Glass8ceca1d2018-11-18 14:22:27 -0700558 * @dev TPM device
Miquel Raynal228e9902018-05-15 11:57:18 +0200559 * @pw Password
560 * @pw_sz Length of the password
561 *
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100562 * Return: code of the operation
Miquel Raynal228e9902018-05-15 11:57:18 +0200563 */
Simon Glass8ceca1d2018-11-18 14:22:27 -0700564u32 tpm2_dam_reset(struct udevice *dev, const char *pw, const ssize_t pw_sz);
Miquel Raynal228e9902018-05-15 11:57:18 +0200565
566/**
567 * Issue a TPM2_DictionaryAttackParameters command.
568 *
Simon Glass8ceca1d2018-11-18 14:22:27 -0700569 * @dev TPM device
Miquel Raynal228e9902018-05-15 11:57:18 +0200570 * @pw Password
571 * @pw_sz Length of the password
572 * @max_tries Count of authorizations before lockout
573 * @recovery_time Time before decrementation of the failure count
574 * @lockout_recovery Time to wait after a lockout
575 *
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100576 * Return: code of the operation
Miquel Raynal228e9902018-05-15 11:57:18 +0200577 */
Simon Glass8ceca1d2018-11-18 14:22:27 -0700578u32 tpm2_dam_parameters(struct udevice *dev, const char *pw,
579 const ssize_t pw_sz, unsigned int max_tries,
580 unsigned int recovery_time,
Miquel Raynal228e9902018-05-15 11:57:18 +0200581 unsigned int lockout_recovery);
582
Miquel Raynal05d7be32018-05-15 11:57:19 +0200583/**
584 * Issue a TPM2_HierarchyChangeAuth command.
585 *
Simon Glass8ceca1d2018-11-18 14:22:27 -0700586 * @dev TPM device
Miquel Raynal05d7be32018-05-15 11:57:19 +0200587 * @handle Handle
588 * @newpw New password
589 * @newpw_sz Length of the new password
590 * @oldpw Old password
591 * @oldpw_sz Length of the old password
592 *
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100593 * Return: code of the operation
Miquel Raynal05d7be32018-05-15 11:57:19 +0200594 */
Simon Glass8ceca1d2018-11-18 14:22:27 -0700595int tpm2_change_auth(struct udevice *dev, u32 handle, const char *newpw,
596 const ssize_t newpw_sz, const char *oldpw,
597 const ssize_t oldpw_sz);
Miquel Raynal05d7be32018-05-15 11:57:19 +0200598
Miquel Raynal0b864f62018-05-15 11:57:20 +0200599/**
600 * Issue a TPM_PCR_SetAuthPolicy command.
601 *
Simon Glass8ceca1d2018-11-18 14:22:27 -0700602 * @dev TPM device
Miquel Raynal0b864f62018-05-15 11:57:20 +0200603 * @pw Platform password
604 * @pw_sz Length of the password
605 * @index Index of the PCR
606 * @digest New key to access the PCR
607 *
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100608 * Return: code of the operation
Miquel Raynal0b864f62018-05-15 11:57:20 +0200609 */
Simon Glass8ceca1d2018-11-18 14:22:27 -0700610u32 tpm2_pcr_setauthpolicy(struct udevice *dev, const char *pw,
611 const ssize_t pw_sz, u32 index, const char *key);
Miquel Raynal0b864f62018-05-15 11:57:20 +0200612
613/**
614 * Issue a TPM_PCR_SetAuthValue command.
615 *
Simon Glass8ceca1d2018-11-18 14:22:27 -0700616 * @dev TPM device
Miquel Raynal0b864f62018-05-15 11:57:20 +0200617 * @pw Platform password
618 * @pw_sz Length of the password
619 * @index Index of the PCR
620 * @digest New key to access the PCR
621 * @key_sz Length of the new key
622 *
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100623 * Return: code of the operation
Miquel Raynal0b864f62018-05-15 11:57:20 +0200624 */
Simon Glass8ceca1d2018-11-18 14:22:27 -0700625u32 tpm2_pcr_setauthvalue(struct udevice *dev, const char *pw,
626 const ssize_t pw_sz, u32 index, const char *key,
627 const ssize_t key_sz);
Miquel Raynal0b864f62018-05-15 11:57:20 +0200628
Dhananjay Phadke7a2cf2e2020-06-04 16:43:59 -0700629/**
630 * Issue a TPM2_GetRandom command.
631 *
632 * @dev TPM device
633 * @param data output buffer for the random bytes
634 * @param count size of output buffer
635 *
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100636 * Return: return code of the operation
Dhananjay Phadke7a2cf2e2020-06-04 16:43:59 -0700637 */
638u32 tpm2_get_random(struct udevice *dev, void *data, u32 count);
639
Simon Glasse9d3d592021-02-06 14:23:41 -0700640/**
641 * Lock data in the TPM
642 *
643 * Once locked the data cannot be written until after a reboot
644 *
645 * @dev TPM device
646 * @index Index of data to lock
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100647 * Return: code of the operation
Simon Glasse9d3d592021-02-06 14:23:41 -0700648 */
649u32 tpm2_write_lock(struct udevice *dev, u32 index);
650
Simon Glass77759db2021-02-06 14:23:42 -0700651/**
652 * Disable access to any platform data
653 *
654 * This can be called to close off access to the firmware data in the data,
655 * before calling the kernel.
656 *
657 * @dev TPM device
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100658 * Return: code of the operation
Simon Glass77759db2021-02-06 14:23:42 -0700659 */
660u32 tpm2_disable_platform_hierarchy(struct udevice *dev);
661
Masahisa Kojima06ef6b62021-11-04 22:59:16 +0900662/**
663 * submit user specified data to the TPM and get response
664 *
665 * @dev TPM device
666 * @sendbuf: Buffer of the data to send
667 * @recvbuf: Buffer to save the response to
668 * @recv_size: Pointer to the size of the response buffer
669 *
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100670 * Return: code of the operation
Masahisa Kojima06ef6b62021-11-04 22:59:16 +0900671 */
672u32 tpm2_submit_command(struct udevice *dev, const u8 *sendbuf,
673 u8 *recvbuf, size_t *recv_size);
674
Simon Glass3f7a73a2022-08-30 21:05:37 -0600675/**
676 * tpm_cr50_report_state() - Report the Cr50 internal state
677 *
678 * @dev: TPM device
679 * @vendor_cmd: Vendor command number to send
680 * @vendor_subcmd: Vendor sub-command number to send
681 * @recvbuf: Buffer to save the response to
682 * @recv_size: Pointer to the size of the response buffer
683 * Return: result of the operation
684 */
685u32 tpm2_report_state(struct udevice *dev, uint vendor_cmd, uint vendor_subcmd,
686 u8 *recvbuf, size_t *recv_size);
687
Simon Glass3564b8e2022-08-30 21:05:38 -0600688/**
689 * tpm2_enable_nvcommits() - Tell TPM to commit NV data immediately
690 *
691 * For Chromium OS verified boot, we may reboot or reset at different times,
692 * possibly leaving non-volatile data unwritten by the TPM.
693 *
694 * This vendor command is used to indicate that non-volatile data should be
695 * written to its store immediately.
696 *
697 * @dev TPM device
698 * @vendor_cmd: Vendor command number to send
699 * @vendor_subcmd: Vendor sub-command number to send
700 * Return: result of the operation
701 */
702u32 tpm2_enable_nvcommits(struct udevice *dev, uint vendor_cmd,
703 uint vendor_subcmd);
704
Ilias Apalodimas42d7bdf2023-01-25 12:18:36 +0200705/**
706 * tpm2_auto_start() - start up the TPM and perform selftests.
707 * If a testable function has not been tested and is
708 * requested the TPM2 will return TPM_RC_NEEDS_TEST.
709 *
710 * @param dev TPM device
711 * Return: TPM2_RC_TESTING, if TPM2 self-test is in progress.
712 * TPM2_RC_SUCCESS, if testing of all functions is complete without
713 * functional failures.
714 * TPM2_RC_FAILURE, if any test failed.
715 * TPM2_RC_INITIALIZE, if the TPM has not gone through the Startup
716 * sequence
717
718 */
719u32 tpm2_auto_start(struct udevice *dev);
720
Tim Harvey6ea1e052024-05-25 13:00:48 -0700721/**
722 * tpm2_name_to_algorithm() - Return an algorithm id given a supported
723 * algorithm name
724 *
725 * @name: algorithm name
726 * Return: enum tpm2_algorithms or -EINVAL
727 */
728enum tpm2_algorithms tpm2_name_to_algorithm(const char *name);
729
730/**
731 * tpm2_algorithm_name() - Return an algorithm name string for a
732 * supported algorithm id
733 *
734 * @algorithm_id: algorithm defined in enum tpm2_algorithms
735 * Return: algorithm name string or ""
736 */
737const char *tpm2_algorithm_name(enum tpm2_algorithms);
738
Ilias Apalodimascb356612024-06-23 14:48:17 +0300739/**
Raymond Mao43158122024-12-24 08:01:07 -0800740 * tpm2_algorithm_supported() - Check if the algorithm supported by U-Boot
741 *
742 * @algorithm_id: algorithm defined in enum tpm2_algorithms
743 * Return: true if supported, otherwise false
744 */
745bool tpm2_algorithm_supported(enum tpm2_algorithms algo);
746
747/**
Ilias Apalodimas1e665f92024-06-23 14:48:18 +0300748 * tpm2_algorithm_to_len() - Return an algorithm length for supported algorithm id
749 *
750 * @algorithm_id: algorithm defined in enum tpm2_algorithms
751 * Return: len or 0 if not supported
752 */
753u16 tpm2_algorithm_to_len(enum tpm2_algorithms algo);
754
755/*
756 * When measured boot is enabled via EFI or bootX commands all the algorithms
757 * above are selected by our Kconfigs. Due to U-Boots nature of being small there
758 * are cases where we need some functionality from the TPM -- e.g storage or RNG
759 * but we don't want to support measurements.
760 *
761 * The choice of hash algorithms are determined by the platform and the TPM
762 * configuration. Failing to cap a PCR in a bank which the platform left
763 * active is a security vulnerability. It permits the unsealing of secrets
764 * if an attacker can replay a good set of measurements into an unused bank.
765 *
766 * On top of that a previous stage bootloader (e.g TF-A), migh pass an eventlog
767 * since it doesn't have a TPM driver, which U-Boot needs to replace. The algorit h
768 * choice is a compile time option in that case and we need to make sure we conform.
769 *
770 * Add a variable here that sums the supported algorithms U-Boot was compiled
771 * with so we can refuse to do measurements if we don't support all of them
772 */
773
774/**
Ilias Apalodimasd788b062024-12-24 08:01:05 -0800775 * tpm2_check_active_banks() - Check if the active PCR banks are supported by
776 * our configuration
Ilias Apalodimas1e665f92024-06-23 14:48:18 +0300777 *
778 * @dev: TPM device
779 * Return: true if allowed
780 */
Ilias Apalodimasd788b062024-12-24 08:01:05 -0800781bool tpm2_check_active_banks(struct udevice *dev);
Ilias Apalodimas1e665f92024-06-23 14:48:18 +0300782
783/**
Ilias Apalodimas9465f7a2024-12-24 08:01:04 -0800784 * tpm2_is_active_bank() - check the pcr_select. If at least one of the PCRs
785 * supports the algorithm add it on the active ones
Ilias Apalodimascb356612024-06-23 14:48:17 +0300786 *
787 * @selection: PCR selection structure
788 * Return: True if the algorithm is active
789 */
Ilias Apalodimas9465f7a2024-12-24 08:01:04 -0800790bool tpm2_is_active_bank(struct tpms_pcr_selection *selection);
Ilias Apalodimascb356612024-06-23 14:48:17 +0300791
Ilias Apalodimas7b1e5222024-12-24 08:01:08 -0800792/**
793 * tpm2_print_active_banks() - Print the active TPM PCRs
794 *
795 * @dev: TPM device
796 */
797void tpm2_print_active_banks(struct udevice *dev);
798
Miquel Raynalf3b43502018-05-15 11:57:08 +0200799#endif /* __TPM_V2_H */