blob: ece422df0c7dbf95e9c5ad4cc3d6e94cfed60ba6 [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 Maof69f2d72025-01-27 06:58:47 -0800233 TPM2_CC_PCR_ALLOCATE = 0x012B,
Raymond Mao5187a642025-01-27 06:58:46 -0800234 TPM2_CC_SHUTDOWN = 0x0145,
Miquel Raynalf3b43502018-05-15 11:57:08 +0200235};
236
237/**
238 * TPM2 return codes.
239 */
240enum tpm2_return_codes {
241 TPM2_RC_SUCCESS = 0x0000,
242 TPM2_RC_BAD_TAG = 0x001E,
243 TPM2_RC_FMT1 = 0x0080,
244 TPM2_RC_HASH = TPM2_RC_FMT1 + 0x0003,
245 TPM2_RC_VALUE = TPM2_RC_FMT1 + 0x0004,
246 TPM2_RC_SIZE = TPM2_RC_FMT1 + 0x0015,
247 TPM2_RC_BAD_AUTH = TPM2_RC_FMT1 + 0x0022,
248 TPM2_RC_HANDLE = TPM2_RC_FMT1 + 0x000B,
249 TPM2_RC_VER1 = 0x0100,
250 TPM2_RC_INITIALIZE = TPM2_RC_VER1 + 0x0000,
251 TPM2_RC_FAILURE = TPM2_RC_VER1 + 0x0001,
252 TPM2_RC_DISABLED = TPM2_RC_VER1 + 0x0020,
253 TPM2_RC_AUTH_MISSING = TPM2_RC_VER1 + 0x0025,
254 TPM2_RC_COMMAND_CODE = TPM2_RC_VER1 + 0x0043,
255 TPM2_RC_AUTHSIZE = TPM2_RC_VER1 + 0x0044,
256 TPM2_RC_AUTH_CONTEXT = TPM2_RC_VER1 + 0x0045,
Simon Glass77759db2021-02-06 14:23:42 -0700257 TPM2_RC_NV_DEFINED = TPM2_RC_VER1 + 0x004c,
Miquel Raynalf3b43502018-05-15 11:57:08 +0200258 TPM2_RC_NEEDS_TEST = TPM2_RC_VER1 + 0x0053,
259 TPM2_RC_WARN = 0x0900,
260 TPM2_RC_TESTING = TPM2_RC_WARN + 0x000A,
261 TPM2_RC_REFERENCE_H0 = TPM2_RC_WARN + 0x0010,
262 TPM2_RC_LOCKOUT = TPM2_RC_WARN + 0x0021,
263};
264
265/**
266 * TPM2 algorithms.
267 */
268enum tpm2_algorithms {
Ilias Apalodimasf4e05902020-11-11 11:18:10 +0200269 TPM2_ALG_SHA1 = 0x04,
Miquel Raynalf3b43502018-05-15 11:57:08 +0200270 TPM2_ALG_XOR = 0x0A,
271 TPM2_ALG_SHA256 = 0x0B,
272 TPM2_ALG_SHA384 = 0x0C,
273 TPM2_ALG_SHA512 = 0x0D,
274 TPM2_ALG_NULL = 0x10,
Ilias Apalodimasf4e05902020-11-11 11:18:10 +0200275 TPM2_ALG_SM3_256 = 0x12,
Miquel Raynalf3b43502018-05-15 11:57:08 +0200276};
277
Tim Harvey6ea1e052024-05-25 13:00:48 -0700278/**
279 * struct digest_info - details of supported digests
280 *
281 * @hash_name: hash name
282 * @hash_alg: hash algorithm id
283 * @hash_mask: hash registry mask
284 * @hash_len: hash digest length
285 */
286struct digest_info {
287 const char *hash_name;
288 u16 hash_alg;
289 u32 hash_mask;
290 u16 hash_len;
Raymond Mao43158122024-12-24 08:01:07 -0800291 bool supported;
Tim Harvey6ea1e052024-05-25 13:00:48 -0700292};
293
294/* Algorithm Registry */
295#define TCG2_BOOT_HASH_ALG_SHA1 0x00000001
296#define TCG2_BOOT_HASH_ALG_SHA256 0x00000002
297#define TCG2_BOOT_HASH_ALG_SHA384 0x00000004
298#define TCG2_BOOT_HASH_ALG_SHA512 0x00000008
299#define TCG2_BOOT_HASH_ALG_SM3_256 0x00000010
300
301static const struct digest_info hash_algo_list[] = {
302 {
303 "sha1",
304 TPM2_ALG_SHA1,
305 TCG2_BOOT_HASH_ALG_SHA1,
306 TPM2_SHA1_DIGEST_SIZE,
Raymond Mao43158122024-12-24 08:01:07 -0800307#if IS_ENABLED(CONFIG_SHA1)
308 true,
309#else
310 false,
Ilias Apalodimas1e665f92024-06-23 14:48:18 +0300311#endif
Raymond Mao43158122024-12-24 08:01:07 -0800312 },
Tim Harvey6ea1e052024-05-25 13:00:48 -0700313 {
314 "sha256",
315 TPM2_ALG_SHA256,
316 TCG2_BOOT_HASH_ALG_SHA256,
317 TPM2_SHA256_DIGEST_SIZE,
Raymond Mao43158122024-12-24 08:01:07 -0800318#if IS_ENABLED(CONFIG_SHA256)
319 true,
320#else
321 false,
Ilias Apalodimas1e665f92024-06-23 14:48:18 +0300322#endif
Raymond Mao43158122024-12-24 08:01:07 -0800323 },
Tim Harvey6ea1e052024-05-25 13:00:48 -0700324 {
325 "sha384",
326 TPM2_ALG_SHA384,
327 TCG2_BOOT_HASH_ALG_SHA384,
328 TPM2_SHA384_DIGEST_SIZE,
Raymond Mao43158122024-12-24 08:01:07 -0800329#if IS_ENABLED(CONFIG_SHA384)
330 true,
331#else
332 false,
Ilias Apalodimas1e665f92024-06-23 14:48:18 +0300333#endif
Raymond Mao43158122024-12-24 08:01:07 -0800334 },
Tim Harvey6ea1e052024-05-25 13:00:48 -0700335 {
336 "sha512",
337 TPM2_ALG_SHA512,
338 TCG2_BOOT_HASH_ALG_SHA512,
339 TPM2_SHA512_DIGEST_SIZE,
Raymond Mao43158122024-12-24 08:01:07 -0800340#if IS_ENABLED(CONFIG_SHA512)
341 true,
342#else
343 false,
Ilias Apalodimas1e665f92024-06-23 14:48:18 +0300344#endif
Raymond Mao43158122024-12-24 08:01:07 -0800345 },
Tim Harvey6ea1e052024-05-25 13:00:48 -0700346};
Eddie James8ed7bb32023-10-24 10:43:49 -0500347
Simon Glassb4ebd1f2018-11-23 21:29:34 -0700348/* NV index attributes */
349enum tpm_index_attrs {
350 TPMA_NV_PPWRITE = 1UL << 0,
351 TPMA_NV_OWNERWRITE = 1UL << 1,
352 TPMA_NV_AUTHWRITE = 1UL << 2,
353 TPMA_NV_POLICYWRITE = 1UL << 3,
354 TPMA_NV_COUNTER = 1UL << 4,
355 TPMA_NV_BITS = 1UL << 5,
356 TPMA_NV_EXTEND = 1UL << 6,
357 TPMA_NV_POLICY_DELETE = 1UL << 10,
358 TPMA_NV_WRITELOCKED = 1UL << 11,
359 TPMA_NV_WRITEALL = 1UL << 12,
360 TPMA_NV_WRITEDEFINE = 1UL << 13,
361 TPMA_NV_WRITE_STCLEAR = 1UL << 14,
362 TPMA_NV_GLOBALLOCK = 1UL << 15,
363 TPMA_NV_PPREAD = 1UL << 16,
364 TPMA_NV_OWNERREAD = 1UL << 17,
365 TPMA_NV_AUTHREAD = 1UL << 18,
366 TPMA_NV_POLICYREAD = 1UL << 19,
367 TPMA_NV_NO_DA = 1UL << 25,
368 TPMA_NV_ORDERLY = 1UL << 26,
369 TPMA_NV_CLEAR_STCLEAR = 1UL << 27,
370 TPMA_NV_READLOCKED = 1UL << 28,
371 TPMA_NV_WRITTEN = 1UL << 29,
372 TPMA_NV_PLATFORMCREATE = 1UL << 30,
373 TPMA_NV_READ_STCLEAR = 1UL << 31,
374
375 TPMA_NV_MASK_READ = TPMA_NV_PPREAD | TPMA_NV_OWNERREAD |
376 TPMA_NV_AUTHREAD | TPMA_NV_POLICYREAD,
377 TPMA_NV_MASK_WRITE = TPMA_NV_PPWRITE | TPMA_NV_OWNERWRITE |
378 TPMA_NV_AUTHWRITE | TPMA_NV_POLICYWRITE,
379};
380
Simon Glasse1ed0ec2020-02-06 09:55:03 -0700381enum {
382 TPM_ACCESS_VALID = 1 << 7,
383 TPM_ACCESS_ACTIVE_LOCALITY = 1 << 5,
384 TPM_ACCESS_REQUEST_PENDING = 1 << 2,
385 TPM_ACCESS_REQUEST_USE = 1 << 1,
386 TPM_ACCESS_ESTABLISHMENT = 1 << 0,
387};
388
389enum {
390 TPM_STS_FAMILY_SHIFT = 26,
391 TPM_STS_FAMILY_MASK = 0x3 << TPM_STS_FAMILY_SHIFT,
392 TPM_STS_FAMILY_TPM2 = 1 << TPM_STS_FAMILY_SHIFT,
393 TPM_STS_RESE_TESTABLISMENT_BIT = 1 << 25,
394 TPM_STS_COMMAND_CANCEL = 1 << 24,
395 TPM_STS_BURST_COUNT_SHIFT = 8,
396 TPM_STS_BURST_COUNT_MASK = 0xffff << TPM_STS_BURST_COUNT_SHIFT,
397 TPM_STS_VALID = 1 << 7,
398 TPM_STS_COMMAND_READY = 1 << 6,
399 TPM_STS_GO = 1 << 5,
400 TPM_STS_DATA_AVAIL = 1 << 4,
401 TPM_STS_DATA_EXPECT = 1 << 3,
402 TPM_STS_SELF_TEST_DONE = 1 << 2,
403 TPM_STS_RESPONSE_RETRY = 1 << 1,
Ilias Apalodimas97f5e2d2021-11-09 09:02:17 +0200404 TPM_STS_READ_ZERO = 0x23
Simon Glasse1ed0ec2020-02-06 09:55:03 -0700405};
406
407enum {
408 TPM_CMD_COUNT_OFFSET = 2,
409 TPM_CMD_ORDINAL_OFFSET = 6,
410 TPM_MAX_BUF_SIZE = 1260,
411};
412
Simon Glass3d930ed2021-02-06 14:23:40 -0700413enum {
414 /* Secure storage for firmware settings */
415 TPM_HT_PCR = 0,
416 TPM_HT_NV_INDEX,
417 TPM_HT_HMAC_SESSION,
418 TPM_HT_POLICY_SESSION,
419
420 HR_SHIFT = 24,
421 HR_PCR = TPM_HT_PCR << HR_SHIFT,
422 HR_HMAC_SESSION = TPM_HT_HMAC_SESSION << HR_SHIFT,
423 HR_POLICY_SESSION = TPM_HT_POLICY_SESSION << HR_SHIFT,
424 HR_NV_INDEX = TPM_HT_NV_INDEX << HR_SHIFT,
425};
426
Miquel Raynal65a1a6c2018-05-15 11:57:12 +0200427/**
428 * Issue a TPM2_Startup command.
429 *
Simon Glass8ceca1d2018-11-18 14:22:27 -0700430 * @dev TPM device
Miquel Raynal65a1a6c2018-05-15 11:57:12 +0200431 * @mode TPM startup mode
432 *
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100433 * Return: code of the operation
Miquel Raynal65a1a6c2018-05-15 11:57:12 +0200434 */
Raymond Mao5187a642025-01-27 06:58:46 -0800435u32 tpm2_startup(struct udevice *dev, bool onoff, enum tpm2_startup_types mode);
Miquel Raynal65a1a6c2018-05-15 11:57:12 +0200436
Miquel Raynal39c76082018-05-15 11:57:13 +0200437/**
438 * Issue a TPM2_SelfTest command.
439 *
Simon Glass8ceca1d2018-11-18 14:22:27 -0700440 * @dev TPM device
Miquel Raynal39c76082018-05-15 11:57:13 +0200441 * @full_test Asking to perform all tests or only the untested ones
442 *
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100443 * Return: code of the operation
Miquel Raynal39c76082018-05-15 11:57:13 +0200444 */
Simon Glass8ceca1d2018-11-18 14:22:27 -0700445u32 tpm2_self_test(struct udevice *dev, enum tpm2_yes_no full_test);
Miquel Raynal39c76082018-05-15 11:57:13 +0200446
Miquel Raynal8df6f8d2018-05-15 11:57:14 +0200447/**
448 * Issue a TPM2_Clear command.
449 *
Simon Glass8ceca1d2018-11-18 14:22:27 -0700450 * @dev TPM device
Miquel Raynal8df6f8d2018-05-15 11:57:14 +0200451 * @handle Handle
452 * @pw Password
453 * @pw_sz Length of the password
454 *
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100455 * Return: code of the operation
Miquel Raynal8df6f8d2018-05-15 11:57:14 +0200456 */
Simon Glass8ceca1d2018-11-18 14:22:27 -0700457u32 tpm2_clear(struct udevice *dev, u32 handle, const char *pw,
458 const ssize_t pw_sz);
Miquel Raynal8df6f8d2018-05-15 11:57:14 +0200459
Miquel Raynal14d72352018-05-15 11:57:15 +0200460/**
Simon Glass713c58a2021-02-06 14:23:39 -0700461 * Issue a TPM_NV_DefineSpace command
462 *
463 * This allows a space to be defined with given attributes and policy
464 *
465 * @dev TPM device
466 * @space_index index of the area
467 * @space_size size of area in bytes
468 * @nv_attributes TPM_NV_ATTRIBUTES of the area
469 * @nv_policy policy to use
470 * @nv_policy_size size of the policy
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100471 * Return: return code of the operation
Simon Glass713c58a2021-02-06 14:23:39 -0700472 */
473u32 tpm2_nv_define_space(struct udevice *dev, u32 space_index,
474 size_t space_size, u32 nv_attributes,
475 const u8 *nv_policy, size_t nv_policy_size);
476
477/**
Miquel Raynal14d72352018-05-15 11:57:15 +0200478 * Issue a TPM2_PCR_Extend command.
479 *
Simon Glass8ceca1d2018-11-18 14:22:27 -0700480 * @dev TPM device
Miquel Raynal14d72352018-05-15 11:57:15 +0200481 * @index Index of the PCR
Ilias Apalodimas7f59c712020-11-26 23:07:22 +0200482 * @algorithm Algorithm used, defined in 'enum tpm2_algorithms'
Miquel Raynal14d72352018-05-15 11:57:15 +0200483 * @digest Value representing the event to be recorded
Ilias Apalodimas7f59c712020-11-26 23:07:22 +0200484 * @digest_len len of the hash
Miquel Raynal14d72352018-05-15 11:57:15 +0200485 *
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100486 * Return: code of the operation
Miquel Raynal14d72352018-05-15 11:57:15 +0200487 */
Ilias Apalodimas7f59c712020-11-26 23:07:22 +0200488u32 tpm2_pcr_extend(struct udevice *dev, u32 index, u32 algorithm,
489 const u8 *digest, u32 digest_len);
Miquel Raynal14d72352018-05-15 11:57:15 +0200490
Miquel Raynal4c1a5852018-05-15 11:57:16 +0200491/**
Simon Glass3d930ed2021-02-06 14:23:40 -0700492 * Read data from the secure storage
493 *
494 * @dev TPM device
495 * @index Index of data to read
496 * @data Place to put data
497 * @count Number of bytes of data
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100498 * Return: code of the operation
Simon Glass3d930ed2021-02-06 14:23:40 -0700499 */
500u32 tpm2_nv_read_value(struct udevice *dev, u32 index, void *data, u32 count);
501
502/**
503 * Write data to the secure storage
504 *
505 * @dev TPM device
506 * @index Index of data to write
507 * @data Data to write
508 * @count Number of bytes of data
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100509 * Return: code of the operation
Simon Glass3d930ed2021-02-06 14:23:40 -0700510 */
511u32 tpm2_nv_write_value(struct udevice *dev, u32 index, const void *data,
512 u32 count);
513
514/**
Miquel Raynal4c1a5852018-05-15 11:57:16 +0200515 * Issue a TPM2_PCR_Read command.
516 *
Simon Glass8ceca1d2018-11-18 14:22:27 -0700517 * @dev TPM device
Miquel Raynal4c1a5852018-05-15 11:57:16 +0200518 * @idx Index of the PCR
519 * @idx_min_sz Minimum size in bytes of the pcrSelect array
Ruchika Gupta686bedb2021-11-29 13:09:45 +0530520 * @algorithm Algorithm used, defined in 'enum tpm2_algorithms'
Miquel Raynal4c1a5852018-05-15 11:57:16 +0200521 * @data Output buffer for contents of the named PCR
Ruchika Gupta686bedb2021-11-29 13:09:45 +0530522 * @digest_len len of the data
Miquel Raynal4c1a5852018-05-15 11:57:16 +0200523 * @updates Optional out parameter: number of updates for this PCR
524 *
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100525 * Return: code of the operation
Miquel Raynal4c1a5852018-05-15 11:57:16 +0200526 */
Simon Glass8ceca1d2018-11-18 14:22:27 -0700527u32 tpm2_pcr_read(struct udevice *dev, u32 idx, unsigned int idx_min_sz,
Ruchika Gupta686bedb2021-11-29 13:09:45 +0530528 u16 algorithm, void *data, u32 digest_len,
529 unsigned int *updates);
Miquel Raynal4c1a5852018-05-15 11:57:16 +0200530
Miquel Raynal2e52c062018-05-15 11:57:17 +0200531/**
532 * Issue a TPM2_GetCapability command. This implementation is limited
533 * to query property index that is 4-byte wide.
534 *
Simon Glass8ceca1d2018-11-18 14:22:27 -0700535 * @dev TPM device
Miquel Raynal2e52c062018-05-15 11:57:17 +0200536 * @capability Partition of capabilities
537 * @property Further definition of capability, limited to be 4 bytes wide
538 * @buf Output buffer for capability information
539 * @prop_count Size of output buffer
540 *
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100541 * Return: code of the operation
Miquel Raynal2e52c062018-05-15 11:57:17 +0200542 */
Simon Glass8ceca1d2018-11-18 14:22:27 -0700543u32 tpm2_get_capability(struct udevice *dev, u32 capability, u32 property,
544 void *buf, size_t prop_count);
Miquel Raynal2e52c062018-05-15 11:57:17 +0200545
Miquel Raynal228e9902018-05-15 11:57:18 +0200546/**
Eddie James8ed7bb32023-10-24 10:43:49 -0500547 * tpm2_get_pcr_info() - get the supported, active PCRs and number of banks
548 *
549 * @dev: TPM device
Ilias Apalodimascb356612024-06-23 14:48:17 +0300550 * @pcrs: struct tpml_pcr_selection of available PCRs
Eddie James8ed7bb32023-10-24 10:43:49 -0500551 *
552 * @return 0 on success, code of operation or negative errno on failure
553 */
Ilias Apalodimascb356612024-06-23 14:48:17 +0300554int tpm2_get_pcr_info(struct udevice *dev, struct tpml_pcr_selection *pcrs);
Eddie James8ed7bb32023-10-24 10:43:49 -0500555
556/**
Miquel Raynal228e9902018-05-15 11:57:18 +0200557 * Issue a TPM2_DictionaryAttackLockReset command.
558 *
Simon Glass8ceca1d2018-11-18 14:22:27 -0700559 * @dev TPM device
Miquel Raynal228e9902018-05-15 11:57:18 +0200560 * @pw Password
561 * @pw_sz Length of the password
562 *
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100563 * Return: code of the operation
Miquel Raynal228e9902018-05-15 11:57:18 +0200564 */
Simon Glass8ceca1d2018-11-18 14:22:27 -0700565u32 tpm2_dam_reset(struct udevice *dev, const char *pw, const ssize_t pw_sz);
Miquel Raynal228e9902018-05-15 11:57:18 +0200566
567/**
568 * Issue a TPM2_DictionaryAttackParameters command.
569 *
Simon Glass8ceca1d2018-11-18 14:22:27 -0700570 * @dev TPM device
Miquel Raynal228e9902018-05-15 11:57:18 +0200571 * @pw Password
572 * @pw_sz Length of the password
573 * @max_tries Count of authorizations before lockout
574 * @recovery_time Time before decrementation of the failure count
575 * @lockout_recovery Time to wait after a lockout
576 *
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100577 * Return: code of the operation
Miquel Raynal228e9902018-05-15 11:57:18 +0200578 */
Simon Glass8ceca1d2018-11-18 14:22:27 -0700579u32 tpm2_dam_parameters(struct udevice *dev, const char *pw,
580 const ssize_t pw_sz, unsigned int max_tries,
581 unsigned int recovery_time,
Miquel Raynal228e9902018-05-15 11:57:18 +0200582 unsigned int lockout_recovery);
583
Miquel Raynal05d7be32018-05-15 11:57:19 +0200584/**
585 * Issue a TPM2_HierarchyChangeAuth command.
586 *
Simon Glass8ceca1d2018-11-18 14:22:27 -0700587 * @dev TPM device
Miquel Raynal05d7be32018-05-15 11:57:19 +0200588 * @handle Handle
589 * @newpw New password
590 * @newpw_sz Length of the new password
591 * @oldpw Old password
592 * @oldpw_sz Length of the old password
593 *
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100594 * Return: code of the operation
Miquel Raynal05d7be32018-05-15 11:57:19 +0200595 */
Simon Glass8ceca1d2018-11-18 14:22:27 -0700596int tpm2_change_auth(struct udevice *dev, u32 handle, const char *newpw,
597 const ssize_t newpw_sz, const char *oldpw,
598 const ssize_t oldpw_sz);
Miquel Raynal05d7be32018-05-15 11:57:19 +0200599
Miquel Raynal0b864f62018-05-15 11:57:20 +0200600/**
601 * Issue a TPM_PCR_SetAuthPolicy command.
602 *
Simon Glass8ceca1d2018-11-18 14:22:27 -0700603 * @dev TPM device
Miquel Raynal0b864f62018-05-15 11:57:20 +0200604 * @pw Platform password
605 * @pw_sz Length of the password
606 * @index Index of the PCR
607 * @digest New key to access the PCR
608 *
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100609 * Return: code of the operation
Miquel Raynal0b864f62018-05-15 11:57:20 +0200610 */
Simon Glass8ceca1d2018-11-18 14:22:27 -0700611u32 tpm2_pcr_setauthpolicy(struct udevice *dev, const char *pw,
612 const ssize_t pw_sz, u32 index, const char *key);
Miquel Raynal0b864f62018-05-15 11:57:20 +0200613
614/**
615 * Issue a TPM_PCR_SetAuthValue command.
616 *
Simon Glass8ceca1d2018-11-18 14:22:27 -0700617 * @dev TPM device
Miquel Raynal0b864f62018-05-15 11:57:20 +0200618 * @pw Platform password
619 * @pw_sz Length of the password
620 * @index Index of the PCR
621 * @digest New key to access the PCR
622 * @key_sz Length of the new key
623 *
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100624 * Return: code of the operation
Miquel Raynal0b864f62018-05-15 11:57:20 +0200625 */
Simon Glass8ceca1d2018-11-18 14:22:27 -0700626u32 tpm2_pcr_setauthvalue(struct udevice *dev, const char *pw,
627 const ssize_t pw_sz, u32 index, const char *key,
628 const ssize_t key_sz);
Miquel Raynal0b864f62018-05-15 11:57:20 +0200629
Dhananjay Phadke7a2cf2e2020-06-04 16:43:59 -0700630/**
631 * Issue a TPM2_GetRandom command.
632 *
633 * @dev TPM device
634 * @param data output buffer for the random bytes
635 * @param count size of output buffer
636 *
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100637 * Return: return code of the operation
Dhananjay Phadke7a2cf2e2020-06-04 16:43:59 -0700638 */
639u32 tpm2_get_random(struct udevice *dev, void *data, u32 count);
640
Simon Glasse9d3d592021-02-06 14:23:41 -0700641/**
642 * Lock data in the TPM
643 *
644 * Once locked the data cannot be written until after a reboot
645 *
646 * @dev TPM device
647 * @index Index of data to lock
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100648 * Return: code of the operation
Simon Glasse9d3d592021-02-06 14:23:41 -0700649 */
650u32 tpm2_write_lock(struct udevice *dev, u32 index);
651
Simon Glass77759db2021-02-06 14:23:42 -0700652/**
653 * Disable access to any platform data
654 *
655 * This can be called to close off access to the firmware data in the data,
656 * before calling the kernel.
657 *
658 * @dev TPM device
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100659 * Return: code of the operation
Simon Glass77759db2021-02-06 14:23:42 -0700660 */
661u32 tpm2_disable_platform_hierarchy(struct udevice *dev);
662
Masahisa Kojima06ef6b62021-11-04 22:59:16 +0900663/**
664 * submit user specified data to the TPM and get response
665 *
666 * @dev TPM device
667 * @sendbuf: Buffer of the data to send
668 * @recvbuf: Buffer to save the response to
669 * @recv_size: Pointer to the size of the response buffer
670 *
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100671 * Return: code of the operation
Masahisa Kojima06ef6b62021-11-04 22:59:16 +0900672 */
673u32 tpm2_submit_command(struct udevice *dev, const u8 *sendbuf,
674 u8 *recvbuf, size_t *recv_size);
675
Simon Glass3f7a73a2022-08-30 21:05:37 -0600676/**
677 * tpm_cr50_report_state() - Report the Cr50 internal state
678 *
679 * @dev: TPM device
680 * @vendor_cmd: Vendor command number to send
681 * @vendor_subcmd: Vendor sub-command number to send
682 * @recvbuf: Buffer to save the response to
683 * @recv_size: Pointer to the size of the response buffer
684 * Return: result of the operation
685 */
686u32 tpm2_report_state(struct udevice *dev, uint vendor_cmd, uint vendor_subcmd,
687 u8 *recvbuf, size_t *recv_size);
688
Simon Glass3564b8e2022-08-30 21:05:38 -0600689/**
690 * tpm2_enable_nvcommits() - Tell TPM to commit NV data immediately
691 *
692 * For Chromium OS verified boot, we may reboot or reset at different times,
693 * possibly leaving non-volatile data unwritten by the TPM.
694 *
695 * This vendor command is used to indicate that non-volatile data should be
696 * written to its store immediately.
697 *
698 * @dev TPM device
699 * @vendor_cmd: Vendor command number to send
700 * @vendor_subcmd: Vendor sub-command number to send
701 * Return: result of the operation
702 */
703u32 tpm2_enable_nvcommits(struct udevice *dev, uint vendor_cmd,
704 uint vendor_subcmd);
705
Ilias Apalodimas42d7bdf2023-01-25 12:18:36 +0200706/**
Raymond Maof0c91252025-01-27 06:58:48 -0800707 * tpm2_scan_masks - Scan the bitmask of algorithms based on the
708 * active/supported banks and the one from eventlog.
709 *
710 * @dev TPM device
711 * @log_active Active algorithm bitmask
712 * @mask Bitmask to set
713 *
714 * Return: zero on success, negative errno otherwise
715 */
716int tpm2_scan_masks(struct udevice *dev, u32 log_active, u32 *mask);
717
718/**
Raymond Maof69f2d72025-01-27 06:58:47 -0800719 * tpm2_pcr_config_algo() - Allocate the active PCRs. Requires reboot
720 *
721 * @dev TPM device
722 * @algo_mask Mask of the algorithms
723 * @pcr PCR structure for allocation
724 * @pcr_len Actual PCR data length
725 *
726 * Return: code of the operation
727 */
728u32 tpm2_pcr_config_algo(struct udevice *dev, u32 algo_mask,
729 struct tpml_pcr_selection *pcr, u32 *pcr_len);
730
731/**
732 * tpm2_send_pcr_allocate() - Send PCR allocate command. Requires reboot
733 *
734 * @dev TPM device
735 * @pw Platform password
736 * @pw_sz Length of the password
737 * @pcr PCR structure for allocation
738 * @pcr_len Actual PCR data length
739 *
740 * Return: code of the operation
741 */
742u32 tpm2_send_pcr_allocate(struct udevice *dev, const char *pw,
743 const ssize_t pw_sz, struct tpml_pcr_selection *pcr,
744 u32 pcr_len);
Raymond Maof0c91252025-01-27 06:58:48 -0800745/**
746 * tpm2_activate_banks() - Activate PCR banks
747 *
748 * @param dev TPM device
749 * @log_active Bitmask of eventlog algorithms
750 *
751 * Return: code of the operation
752 */
753int tpm2_activate_banks(struct udevice *dev, u32 log_active);
Raymond Maof69f2d72025-01-27 06:58:47 -0800754
755/**
Ilias Apalodimas42d7bdf2023-01-25 12:18:36 +0200756 * tpm2_auto_start() - start up the TPM and perform selftests.
757 * If a testable function has not been tested and is
758 * requested the TPM2 will return TPM_RC_NEEDS_TEST.
759 *
760 * @param dev TPM device
761 * Return: TPM2_RC_TESTING, if TPM2 self-test is in progress.
762 * TPM2_RC_SUCCESS, if testing of all functions is complete without
763 * functional failures.
764 * TPM2_RC_FAILURE, if any test failed.
765 * TPM2_RC_INITIALIZE, if the TPM has not gone through the Startup
766 * sequence
767
768 */
769u32 tpm2_auto_start(struct udevice *dev);
770
Tim Harvey6ea1e052024-05-25 13:00:48 -0700771/**
772 * tpm2_name_to_algorithm() - Return an algorithm id given a supported
773 * algorithm name
774 *
775 * @name: algorithm name
776 * Return: enum tpm2_algorithms or -EINVAL
777 */
778enum tpm2_algorithms tpm2_name_to_algorithm(const char *name);
779
780/**
781 * tpm2_algorithm_name() - Return an algorithm name string for a
782 * supported algorithm id
783 *
784 * @algorithm_id: algorithm defined in enum tpm2_algorithms
785 * Return: algorithm name string or ""
786 */
787const char *tpm2_algorithm_name(enum tpm2_algorithms);
788
Ilias Apalodimascb356612024-06-23 14:48:17 +0300789/**
Raymond Mao43158122024-12-24 08:01:07 -0800790 * tpm2_algorithm_supported() - Check if the algorithm supported by U-Boot
791 *
792 * @algorithm_id: algorithm defined in enum tpm2_algorithms
793 * Return: true if supported, otherwise false
794 */
795bool tpm2_algorithm_supported(enum tpm2_algorithms algo);
796
797/**
Ilias Apalodimas1e665f92024-06-23 14:48:18 +0300798 * tpm2_algorithm_to_len() - Return an algorithm length for supported algorithm id
799 *
800 * @algorithm_id: algorithm defined in enum tpm2_algorithms
801 * Return: len or 0 if not supported
802 */
803u16 tpm2_algorithm_to_len(enum tpm2_algorithms algo);
804
805/*
806 * When measured boot is enabled via EFI or bootX commands all the algorithms
807 * above are selected by our Kconfigs. Due to U-Boots nature of being small there
808 * are cases where we need some functionality from the TPM -- e.g storage or RNG
809 * but we don't want to support measurements.
810 *
811 * The choice of hash algorithms are determined by the platform and the TPM
812 * configuration. Failing to cap a PCR in a bank which the platform left
813 * active is a security vulnerability. It permits the unsealing of secrets
814 * if an attacker can replay a good set of measurements into an unused bank.
815 *
816 * On top of that a previous stage bootloader (e.g TF-A), migh pass an eventlog
817 * since it doesn't have a TPM driver, which U-Boot needs to replace. The algorit h
818 * choice is a compile time option in that case and we need to make sure we conform.
819 *
820 * Add a variable here that sums the supported algorithms U-Boot was compiled
821 * with so we can refuse to do measurements if we don't support all of them
822 */
823
824/**
Ilias Apalodimasd788b062024-12-24 08:01:05 -0800825 * tpm2_check_active_banks() - Check if the active PCR banks are supported by
826 * our configuration
Ilias Apalodimas1e665f92024-06-23 14:48:18 +0300827 *
828 * @dev: TPM device
829 * Return: true if allowed
830 */
Ilias Apalodimasd788b062024-12-24 08:01:05 -0800831bool tpm2_check_active_banks(struct udevice *dev);
Ilias Apalodimas1e665f92024-06-23 14:48:18 +0300832
833/**
Ilias Apalodimas9465f7a2024-12-24 08:01:04 -0800834 * tpm2_is_active_bank() - check the pcr_select. If at least one of the PCRs
835 * supports the algorithm add it on the active ones
Ilias Apalodimascb356612024-06-23 14:48:17 +0300836 *
837 * @selection: PCR selection structure
838 * Return: True if the algorithm is active
839 */
Ilias Apalodimas9465f7a2024-12-24 08:01:04 -0800840bool tpm2_is_active_bank(struct tpms_pcr_selection *selection);
Ilias Apalodimascb356612024-06-23 14:48:17 +0300841
Ilias Apalodimas7b1e5222024-12-24 08:01:08 -0800842/**
843 * tpm2_print_active_banks() - Print the active TPM PCRs
844 *
845 * @dev: TPM device
846 */
847void tpm2_print_active_banks(struct udevice *dev);
848
Miquel Raynalf3b43502018-05-15 11:57:08 +0200849#endif /* __TPM_V2_H */