blob: 65681464b373b2af2013ea79e08ccbd09bf964da [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,
Miquel Raynalf3b43502018-05-15 11:57:08 +0200233};
234
235/**
236 * TPM2 return codes.
237 */
238enum tpm2_return_codes {
239 TPM2_RC_SUCCESS = 0x0000,
240 TPM2_RC_BAD_TAG = 0x001E,
241 TPM2_RC_FMT1 = 0x0080,
242 TPM2_RC_HASH = TPM2_RC_FMT1 + 0x0003,
243 TPM2_RC_VALUE = TPM2_RC_FMT1 + 0x0004,
244 TPM2_RC_SIZE = TPM2_RC_FMT1 + 0x0015,
245 TPM2_RC_BAD_AUTH = TPM2_RC_FMT1 + 0x0022,
246 TPM2_RC_HANDLE = TPM2_RC_FMT1 + 0x000B,
247 TPM2_RC_VER1 = 0x0100,
248 TPM2_RC_INITIALIZE = TPM2_RC_VER1 + 0x0000,
249 TPM2_RC_FAILURE = TPM2_RC_VER1 + 0x0001,
250 TPM2_RC_DISABLED = TPM2_RC_VER1 + 0x0020,
251 TPM2_RC_AUTH_MISSING = TPM2_RC_VER1 + 0x0025,
252 TPM2_RC_COMMAND_CODE = TPM2_RC_VER1 + 0x0043,
253 TPM2_RC_AUTHSIZE = TPM2_RC_VER1 + 0x0044,
254 TPM2_RC_AUTH_CONTEXT = TPM2_RC_VER1 + 0x0045,
Simon Glass77759db2021-02-06 14:23:42 -0700255 TPM2_RC_NV_DEFINED = TPM2_RC_VER1 + 0x004c,
Miquel Raynalf3b43502018-05-15 11:57:08 +0200256 TPM2_RC_NEEDS_TEST = TPM2_RC_VER1 + 0x0053,
257 TPM2_RC_WARN = 0x0900,
258 TPM2_RC_TESTING = TPM2_RC_WARN + 0x000A,
259 TPM2_RC_REFERENCE_H0 = TPM2_RC_WARN + 0x0010,
260 TPM2_RC_LOCKOUT = TPM2_RC_WARN + 0x0021,
261};
262
263/**
264 * TPM2 algorithms.
265 */
266enum tpm2_algorithms {
Ilias Apalodimasf4e05902020-11-11 11:18:10 +0200267 TPM2_ALG_SHA1 = 0x04,
Miquel Raynalf3b43502018-05-15 11:57:08 +0200268 TPM2_ALG_XOR = 0x0A,
269 TPM2_ALG_SHA256 = 0x0B,
270 TPM2_ALG_SHA384 = 0x0C,
271 TPM2_ALG_SHA512 = 0x0D,
272 TPM2_ALG_NULL = 0x10,
Ilias Apalodimasf4e05902020-11-11 11:18:10 +0200273 TPM2_ALG_SM3_256 = 0x12,
Miquel Raynalf3b43502018-05-15 11:57:08 +0200274};
275
Tim Harvey6ea1e052024-05-25 13:00:48 -0700276/**
277 * struct digest_info - details of supported digests
278 *
279 * @hash_name: hash name
280 * @hash_alg: hash algorithm id
281 * @hash_mask: hash registry mask
282 * @hash_len: hash digest length
283 */
284struct digest_info {
285 const char *hash_name;
286 u16 hash_alg;
287 u32 hash_mask;
288 u16 hash_len;
Raymond Mao43158122024-12-24 08:01:07 -0800289 bool supported;
Tim Harvey6ea1e052024-05-25 13:00:48 -0700290};
291
292/* Algorithm Registry */
293#define TCG2_BOOT_HASH_ALG_SHA1 0x00000001
294#define TCG2_BOOT_HASH_ALG_SHA256 0x00000002
295#define TCG2_BOOT_HASH_ALG_SHA384 0x00000004
296#define TCG2_BOOT_HASH_ALG_SHA512 0x00000008
297#define TCG2_BOOT_HASH_ALG_SM3_256 0x00000010
298
299static const struct digest_info hash_algo_list[] = {
300 {
301 "sha1",
302 TPM2_ALG_SHA1,
303 TCG2_BOOT_HASH_ALG_SHA1,
304 TPM2_SHA1_DIGEST_SIZE,
Raymond Mao43158122024-12-24 08:01:07 -0800305#if IS_ENABLED(CONFIG_SHA1)
306 true,
307#else
308 false,
Ilias Apalodimas1e665f92024-06-23 14:48:18 +0300309#endif
Raymond Mao43158122024-12-24 08:01:07 -0800310 },
Tim Harvey6ea1e052024-05-25 13:00:48 -0700311 {
312 "sha256",
313 TPM2_ALG_SHA256,
314 TCG2_BOOT_HASH_ALG_SHA256,
315 TPM2_SHA256_DIGEST_SIZE,
Raymond Mao43158122024-12-24 08:01:07 -0800316#if IS_ENABLED(CONFIG_SHA256)
317 true,
318#else
319 false,
Ilias Apalodimas1e665f92024-06-23 14:48:18 +0300320#endif
Raymond Mao43158122024-12-24 08:01:07 -0800321 },
Tim Harvey6ea1e052024-05-25 13:00:48 -0700322 {
323 "sha384",
324 TPM2_ALG_SHA384,
325 TCG2_BOOT_HASH_ALG_SHA384,
326 TPM2_SHA384_DIGEST_SIZE,
Raymond Mao43158122024-12-24 08:01:07 -0800327#if IS_ENABLED(CONFIG_SHA384)
328 true,
329#else
330 false,
Ilias Apalodimas1e665f92024-06-23 14:48:18 +0300331#endif
Raymond Mao43158122024-12-24 08:01:07 -0800332 },
Tim Harvey6ea1e052024-05-25 13:00:48 -0700333 {
334 "sha512",
335 TPM2_ALG_SHA512,
336 TCG2_BOOT_HASH_ALG_SHA512,
337 TPM2_SHA512_DIGEST_SIZE,
Raymond Mao43158122024-12-24 08:01:07 -0800338#if IS_ENABLED(CONFIG_SHA512)
339 true,
340#else
341 false,
Ilias Apalodimas1e665f92024-06-23 14:48:18 +0300342#endif
Raymond Mao43158122024-12-24 08:01:07 -0800343 },
Tim Harvey6ea1e052024-05-25 13:00:48 -0700344};
Eddie James8ed7bb32023-10-24 10:43:49 -0500345
Simon Glassb4ebd1f2018-11-23 21:29:34 -0700346/* NV index attributes */
347enum tpm_index_attrs {
348 TPMA_NV_PPWRITE = 1UL << 0,
349 TPMA_NV_OWNERWRITE = 1UL << 1,
350 TPMA_NV_AUTHWRITE = 1UL << 2,
351 TPMA_NV_POLICYWRITE = 1UL << 3,
352 TPMA_NV_COUNTER = 1UL << 4,
353 TPMA_NV_BITS = 1UL << 5,
354 TPMA_NV_EXTEND = 1UL << 6,
355 TPMA_NV_POLICY_DELETE = 1UL << 10,
356 TPMA_NV_WRITELOCKED = 1UL << 11,
357 TPMA_NV_WRITEALL = 1UL << 12,
358 TPMA_NV_WRITEDEFINE = 1UL << 13,
359 TPMA_NV_WRITE_STCLEAR = 1UL << 14,
360 TPMA_NV_GLOBALLOCK = 1UL << 15,
361 TPMA_NV_PPREAD = 1UL << 16,
362 TPMA_NV_OWNERREAD = 1UL << 17,
363 TPMA_NV_AUTHREAD = 1UL << 18,
364 TPMA_NV_POLICYREAD = 1UL << 19,
365 TPMA_NV_NO_DA = 1UL << 25,
366 TPMA_NV_ORDERLY = 1UL << 26,
367 TPMA_NV_CLEAR_STCLEAR = 1UL << 27,
368 TPMA_NV_READLOCKED = 1UL << 28,
369 TPMA_NV_WRITTEN = 1UL << 29,
370 TPMA_NV_PLATFORMCREATE = 1UL << 30,
371 TPMA_NV_READ_STCLEAR = 1UL << 31,
372
373 TPMA_NV_MASK_READ = TPMA_NV_PPREAD | TPMA_NV_OWNERREAD |
374 TPMA_NV_AUTHREAD | TPMA_NV_POLICYREAD,
375 TPMA_NV_MASK_WRITE = TPMA_NV_PPWRITE | TPMA_NV_OWNERWRITE |
376 TPMA_NV_AUTHWRITE | TPMA_NV_POLICYWRITE,
377};
378
Simon Glasse1ed0ec2020-02-06 09:55:03 -0700379enum {
380 TPM_ACCESS_VALID = 1 << 7,
381 TPM_ACCESS_ACTIVE_LOCALITY = 1 << 5,
382 TPM_ACCESS_REQUEST_PENDING = 1 << 2,
383 TPM_ACCESS_REQUEST_USE = 1 << 1,
384 TPM_ACCESS_ESTABLISHMENT = 1 << 0,
385};
386
387enum {
388 TPM_STS_FAMILY_SHIFT = 26,
389 TPM_STS_FAMILY_MASK = 0x3 << TPM_STS_FAMILY_SHIFT,
390 TPM_STS_FAMILY_TPM2 = 1 << TPM_STS_FAMILY_SHIFT,
391 TPM_STS_RESE_TESTABLISMENT_BIT = 1 << 25,
392 TPM_STS_COMMAND_CANCEL = 1 << 24,
393 TPM_STS_BURST_COUNT_SHIFT = 8,
394 TPM_STS_BURST_COUNT_MASK = 0xffff << TPM_STS_BURST_COUNT_SHIFT,
395 TPM_STS_VALID = 1 << 7,
396 TPM_STS_COMMAND_READY = 1 << 6,
397 TPM_STS_GO = 1 << 5,
398 TPM_STS_DATA_AVAIL = 1 << 4,
399 TPM_STS_DATA_EXPECT = 1 << 3,
400 TPM_STS_SELF_TEST_DONE = 1 << 2,
401 TPM_STS_RESPONSE_RETRY = 1 << 1,
Ilias Apalodimas97f5e2d2021-11-09 09:02:17 +0200402 TPM_STS_READ_ZERO = 0x23
Simon Glasse1ed0ec2020-02-06 09:55:03 -0700403};
404
405enum {
406 TPM_CMD_COUNT_OFFSET = 2,
407 TPM_CMD_ORDINAL_OFFSET = 6,
408 TPM_MAX_BUF_SIZE = 1260,
409};
410
Simon Glass3d930ed2021-02-06 14:23:40 -0700411enum {
412 /* Secure storage for firmware settings */
413 TPM_HT_PCR = 0,
414 TPM_HT_NV_INDEX,
415 TPM_HT_HMAC_SESSION,
416 TPM_HT_POLICY_SESSION,
417
418 HR_SHIFT = 24,
419 HR_PCR = TPM_HT_PCR << HR_SHIFT,
420 HR_HMAC_SESSION = TPM_HT_HMAC_SESSION << HR_SHIFT,
421 HR_POLICY_SESSION = TPM_HT_POLICY_SESSION << HR_SHIFT,
422 HR_NV_INDEX = TPM_HT_NV_INDEX << HR_SHIFT,
423};
424
Miquel Raynal65a1a6c2018-05-15 11:57:12 +0200425/**
426 * Issue a TPM2_Startup command.
427 *
Simon Glass8ceca1d2018-11-18 14:22:27 -0700428 * @dev TPM device
Miquel Raynal65a1a6c2018-05-15 11:57:12 +0200429 * @mode TPM startup mode
430 *
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100431 * Return: code of the operation
Miquel Raynal65a1a6c2018-05-15 11:57:12 +0200432 */
Simon Glass8ceca1d2018-11-18 14:22:27 -0700433u32 tpm2_startup(struct udevice *dev, enum tpm2_startup_types mode);
Miquel Raynal65a1a6c2018-05-15 11:57:12 +0200434
Miquel Raynal39c76082018-05-15 11:57:13 +0200435/**
436 * Issue a TPM2_SelfTest command.
437 *
Simon Glass8ceca1d2018-11-18 14:22:27 -0700438 * @dev TPM device
Miquel Raynal39c76082018-05-15 11:57:13 +0200439 * @full_test Asking to perform all tests or only the untested ones
440 *
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100441 * Return: code of the operation
Miquel Raynal39c76082018-05-15 11:57:13 +0200442 */
Simon Glass8ceca1d2018-11-18 14:22:27 -0700443u32 tpm2_self_test(struct udevice *dev, enum tpm2_yes_no full_test);
Miquel Raynal39c76082018-05-15 11:57:13 +0200444
Miquel Raynal8df6f8d2018-05-15 11:57:14 +0200445/**
446 * Issue a TPM2_Clear command.
447 *
Simon Glass8ceca1d2018-11-18 14:22:27 -0700448 * @dev TPM device
Miquel Raynal8df6f8d2018-05-15 11:57:14 +0200449 * @handle Handle
450 * @pw Password
451 * @pw_sz Length of the password
452 *
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100453 * Return: code of the operation
Miquel Raynal8df6f8d2018-05-15 11:57:14 +0200454 */
Simon Glass8ceca1d2018-11-18 14:22:27 -0700455u32 tpm2_clear(struct udevice *dev, u32 handle, const char *pw,
456 const ssize_t pw_sz);
Miquel Raynal8df6f8d2018-05-15 11:57:14 +0200457
Miquel Raynal14d72352018-05-15 11:57:15 +0200458/**
Simon Glass713c58a2021-02-06 14:23:39 -0700459 * Issue a TPM_NV_DefineSpace command
460 *
461 * This allows a space to be defined with given attributes and policy
462 *
463 * @dev TPM device
464 * @space_index index of the area
465 * @space_size size of area in bytes
466 * @nv_attributes TPM_NV_ATTRIBUTES of the area
467 * @nv_policy policy to use
468 * @nv_policy_size size of the policy
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100469 * Return: return code of the operation
Simon Glass713c58a2021-02-06 14:23:39 -0700470 */
471u32 tpm2_nv_define_space(struct udevice *dev, u32 space_index,
472 size_t space_size, u32 nv_attributes,
473 const u8 *nv_policy, size_t nv_policy_size);
474
475/**
Miquel Raynal14d72352018-05-15 11:57:15 +0200476 * Issue a TPM2_PCR_Extend command.
477 *
Simon Glass8ceca1d2018-11-18 14:22:27 -0700478 * @dev TPM device
Miquel Raynal14d72352018-05-15 11:57:15 +0200479 * @index Index of the PCR
Ilias Apalodimas7f59c712020-11-26 23:07:22 +0200480 * @algorithm Algorithm used, defined in 'enum tpm2_algorithms'
Miquel Raynal14d72352018-05-15 11:57:15 +0200481 * @digest Value representing the event to be recorded
Ilias Apalodimas7f59c712020-11-26 23:07:22 +0200482 * @digest_len len of the hash
Miquel Raynal14d72352018-05-15 11:57:15 +0200483 *
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100484 * Return: code of the operation
Miquel Raynal14d72352018-05-15 11:57:15 +0200485 */
Ilias Apalodimas7f59c712020-11-26 23:07:22 +0200486u32 tpm2_pcr_extend(struct udevice *dev, u32 index, u32 algorithm,
487 const u8 *digest, u32 digest_len);
Miquel Raynal14d72352018-05-15 11:57:15 +0200488
Miquel Raynal4c1a5852018-05-15 11:57:16 +0200489/**
Simon Glass3d930ed2021-02-06 14:23:40 -0700490 * Read data from the secure storage
491 *
492 * @dev TPM device
493 * @index Index of data to read
494 * @data Place to put data
495 * @count Number of bytes of data
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100496 * Return: code of the operation
Simon Glass3d930ed2021-02-06 14:23:40 -0700497 */
498u32 tpm2_nv_read_value(struct udevice *dev, u32 index, void *data, u32 count);
499
500/**
501 * Write data to the secure storage
502 *
503 * @dev TPM device
504 * @index Index of data to write
505 * @data Data to write
506 * @count Number of bytes of data
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100507 * Return: code of the operation
Simon Glass3d930ed2021-02-06 14:23:40 -0700508 */
509u32 tpm2_nv_write_value(struct udevice *dev, u32 index, const void *data,
510 u32 count);
511
512/**
Miquel Raynal4c1a5852018-05-15 11:57:16 +0200513 * Issue a TPM2_PCR_Read command.
514 *
Simon Glass8ceca1d2018-11-18 14:22:27 -0700515 * @dev TPM device
Miquel Raynal4c1a5852018-05-15 11:57:16 +0200516 * @idx Index of the PCR
517 * @idx_min_sz Minimum size in bytes of the pcrSelect array
Ruchika Gupta686bedb2021-11-29 13:09:45 +0530518 * @algorithm Algorithm used, defined in 'enum tpm2_algorithms'
Miquel Raynal4c1a5852018-05-15 11:57:16 +0200519 * @data Output buffer for contents of the named PCR
Ruchika Gupta686bedb2021-11-29 13:09:45 +0530520 * @digest_len len of the data
Miquel Raynal4c1a5852018-05-15 11:57:16 +0200521 * @updates Optional out parameter: number of updates for this PCR
522 *
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100523 * Return: code of the operation
Miquel Raynal4c1a5852018-05-15 11:57:16 +0200524 */
Simon Glass8ceca1d2018-11-18 14:22:27 -0700525u32 tpm2_pcr_read(struct udevice *dev, u32 idx, unsigned int idx_min_sz,
Ruchika Gupta686bedb2021-11-29 13:09:45 +0530526 u16 algorithm, void *data, u32 digest_len,
527 unsigned int *updates);
Miquel Raynal4c1a5852018-05-15 11:57:16 +0200528
Miquel Raynal2e52c062018-05-15 11:57:17 +0200529/**
530 * Issue a TPM2_GetCapability command. This implementation is limited
531 * to query property index that is 4-byte wide.
532 *
Simon Glass8ceca1d2018-11-18 14:22:27 -0700533 * @dev TPM device
Miquel Raynal2e52c062018-05-15 11:57:17 +0200534 * @capability Partition of capabilities
535 * @property Further definition of capability, limited to be 4 bytes wide
536 * @buf Output buffer for capability information
537 * @prop_count Size of output buffer
538 *
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100539 * Return: code of the operation
Miquel Raynal2e52c062018-05-15 11:57:17 +0200540 */
Simon Glass8ceca1d2018-11-18 14:22:27 -0700541u32 tpm2_get_capability(struct udevice *dev, u32 capability, u32 property,
542 void *buf, size_t prop_count);
Miquel Raynal2e52c062018-05-15 11:57:17 +0200543
Miquel Raynal228e9902018-05-15 11:57:18 +0200544/**
Eddie James8ed7bb32023-10-24 10:43:49 -0500545 * tpm2_get_pcr_info() - get the supported, active PCRs and number of banks
546 *
547 * @dev: TPM device
Ilias Apalodimascb356612024-06-23 14:48:17 +0300548 * @pcrs: struct tpml_pcr_selection of available PCRs
Eddie James8ed7bb32023-10-24 10:43:49 -0500549 *
550 * @return 0 on success, code of operation or negative errno on failure
551 */
Ilias Apalodimascb356612024-06-23 14:48:17 +0300552int tpm2_get_pcr_info(struct udevice *dev, struct tpml_pcr_selection *pcrs);
Eddie James8ed7bb32023-10-24 10:43:49 -0500553
554/**
Miquel Raynal228e9902018-05-15 11:57:18 +0200555 * Issue a TPM2_DictionaryAttackLockReset command.
556 *
Simon Glass8ceca1d2018-11-18 14:22:27 -0700557 * @dev TPM device
Miquel Raynal228e9902018-05-15 11:57:18 +0200558 * @pw Password
559 * @pw_sz Length of the password
560 *
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100561 * Return: code of the operation
Miquel Raynal228e9902018-05-15 11:57:18 +0200562 */
Simon Glass8ceca1d2018-11-18 14:22:27 -0700563u32 tpm2_dam_reset(struct udevice *dev, const char *pw, const ssize_t pw_sz);
Miquel Raynal228e9902018-05-15 11:57:18 +0200564
565/**
566 * Issue a TPM2_DictionaryAttackParameters command.
567 *
Simon Glass8ceca1d2018-11-18 14:22:27 -0700568 * @dev TPM device
Miquel Raynal228e9902018-05-15 11:57:18 +0200569 * @pw Password
570 * @pw_sz Length of the password
571 * @max_tries Count of authorizations before lockout
572 * @recovery_time Time before decrementation of the failure count
573 * @lockout_recovery Time to wait after a lockout
574 *
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100575 * Return: code of the operation
Miquel Raynal228e9902018-05-15 11:57:18 +0200576 */
Simon Glass8ceca1d2018-11-18 14:22:27 -0700577u32 tpm2_dam_parameters(struct udevice *dev, const char *pw,
578 const ssize_t pw_sz, unsigned int max_tries,
579 unsigned int recovery_time,
Miquel Raynal228e9902018-05-15 11:57:18 +0200580 unsigned int lockout_recovery);
581
Miquel Raynal05d7be32018-05-15 11:57:19 +0200582/**
583 * Issue a TPM2_HierarchyChangeAuth command.
584 *
Simon Glass8ceca1d2018-11-18 14:22:27 -0700585 * @dev TPM device
Miquel Raynal05d7be32018-05-15 11:57:19 +0200586 * @handle Handle
587 * @newpw New password
588 * @newpw_sz Length of the new password
589 * @oldpw Old password
590 * @oldpw_sz Length of the old password
591 *
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100592 * Return: code of the operation
Miquel Raynal05d7be32018-05-15 11:57:19 +0200593 */
Simon Glass8ceca1d2018-11-18 14:22:27 -0700594int tpm2_change_auth(struct udevice *dev, u32 handle, const char *newpw,
595 const ssize_t newpw_sz, const char *oldpw,
596 const ssize_t oldpw_sz);
Miquel Raynal05d7be32018-05-15 11:57:19 +0200597
Miquel Raynal0b864f62018-05-15 11:57:20 +0200598/**
599 * Issue a TPM_PCR_SetAuthPolicy command.
600 *
Simon Glass8ceca1d2018-11-18 14:22:27 -0700601 * @dev TPM device
Miquel Raynal0b864f62018-05-15 11:57:20 +0200602 * @pw Platform password
603 * @pw_sz Length of the password
604 * @index Index of the PCR
605 * @digest New key to access the PCR
606 *
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100607 * Return: code of the operation
Miquel Raynal0b864f62018-05-15 11:57:20 +0200608 */
Simon Glass8ceca1d2018-11-18 14:22:27 -0700609u32 tpm2_pcr_setauthpolicy(struct udevice *dev, const char *pw,
610 const ssize_t pw_sz, u32 index, const char *key);
Miquel Raynal0b864f62018-05-15 11:57:20 +0200611
612/**
613 * Issue a TPM_PCR_SetAuthValue command.
614 *
Simon Glass8ceca1d2018-11-18 14:22:27 -0700615 * @dev TPM device
Miquel Raynal0b864f62018-05-15 11:57:20 +0200616 * @pw Platform password
617 * @pw_sz Length of the password
618 * @index Index of the PCR
619 * @digest New key to access the PCR
620 * @key_sz Length of the new key
621 *
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100622 * Return: code of the operation
Miquel Raynal0b864f62018-05-15 11:57:20 +0200623 */
Simon Glass8ceca1d2018-11-18 14:22:27 -0700624u32 tpm2_pcr_setauthvalue(struct udevice *dev, const char *pw,
625 const ssize_t pw_sz, u32 index, const char *key,
626 const ssize_t key_sz);
Miquel Raynal0b864f62018-05-15 11:57:20 +0200627
Dhananjay Phadke7a2cf2e2020-06-04 16:43:59 -0700628/**
629 * Issue a TPM2_GetRandom command.
630 *
631 * @dev TPM device
632 * @param data output buffer for the random bytes
633 * @param count size of output buffer
634 *
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100635 * Return: return code of the operation
Dhananjay Phadke7a2cf2e2020-06-04 16:43:59 -0700636 */
637u32 tpm2_get_random(struct udevice *dev, void *data, u32 count);
638
Simon Glasse9d3d592021-02-06 14:23:41 -0700639/**
640 * Lock data in the TPM
641 *
642 * Once locked the data cannot be written until after a reboot
643 *
644 * @dev TPM device
645 * @index Index of data to lock
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100646 * Return: code of the operation
Simon Glasse9d3d592021-02-06 14:23:41 -0700647 */
648u32 tpm2_write_lock(struct udevice *dev, u32 index);
649
Simon Glass77759db2021-02-06 14:23:42 -0700650/**
651 * Disable access to any platform data
652 *
653 * This can be called to close off access to the firmware data in the data,
654 * before calling the kernel.
655 *
656 * @dev TPM device
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100657 * Return: code of the operation
Simon Glass77759db2021-02-06 14:23:42 -0700658 */
659u32 tpm2_disable_platform_hierarchy(struct udevice *dev);
660
Masahisa Kojima06ef6b62021-11-04 22:59:16 +0900661/**
662 * submit user specified data to the TPM and get response
663 *
664 * @dev TPM device
665 * @sendbuf: Buffer of the data to send
666 * @recvbuf: Buffer to save the response to
667 * @recv_size: Pointer to the size of the response buffer
668 *
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100669 * Return: code of the operation
Masahisa Kojima06ef6b62021-11-04 22:59:16 +0900670 */
671u32 tpm2_submit_command(struct udevice *dev, const u8 *sendbuf,
672 u8 *recvbuf, size_t *recv_size);
673
Simon Glass3f7a73a2022-08-30 21:05:37 -0600674/**
675 * tpm_cr50_report_state() - Report the Cr50 internal state
676 *
677 * @dev: TPM device
678 * @vendor_cmd: Vendor command number to send
679 * @vendor_subcmd: Vendor sub-command number to send
680 * @recvbuf: Buffer to save the response to
681 * @recv_size: Pointer to the size of the response buffer
682 * Return: result of the operation
683 */
684u32 tpm2_report_state(struct udevice *dev, uint vendor_cmd, uint vendor_subcmd,
685 u8 *recvbuf, size_t *recv_size);
686
Simon Glass3564b8e2022-08-30 21:05:38 -0600687/**
688 * tpm2_enable_nvcommits() - Tell TPM to commit NV data immediately
689 *
690 * For Chromium OS verified boot, we may reboot or reset at different times,
691 * possibly leaving non-volatile data unwritten by the TPM.
692 *
693 * This vendor command is used to indicate that non-volatile data should be
694 * written to its store immediately.
695 *
696 * @dev TPM device
697 * @vendor_cmd: Vendor command number to send
698 * @vendor_subcmd: Vendor sub-command number to send
699 * Return: result of the operation
700 */
701u32 tpm2_enable_nvcommits(struct udevice *dev, uint vendor_cmd,
702 uint vendor_subcmd);
703
Ilias Apalodimas42d7bdf2023-01-25 12:18:36 +0200704/**
705 * tpm2_auto_start() - start up the TPM and perform selftests.
706 * If a testable function has not been tested and is
707 * requested the TPM2 will return TPM_RC_NEEDS_TEST.
708 *
709 * @param dev TPM device
710 * Return: TPM2_RC_TESTING, if TPM2 self-test is in progress.
711 * TPM2_RC_SUCCESS, if testing of all functions is complete without
712 * functional failures.
713 * TPM2_RC_FAILURE, if any test failed.
714 * TPM2_RC_INITIALIZE, if the TPM has not gone through the Startup
715 * sequence
716
717 */
718u32 tpm2_auto_start(struct udevice *dev);
719
Tim Harvey6ea1e052024-05-25 13:00:48 -0700720/**
721 * tpm2_name_to_algorithm() - Return an algorithm id given a supported
722 * algorithm name
723 *
724 * @name: algorithm name
725 * Return: enum tpm2_algorithms or -EINVAL
726 */
727enum tpm2_algorithms tpm2_name_to_algorithm(const char *name);
728
729/**
730 * tpm2_algorithm_name() - Return an algorithm name string for a
731 * supported algorithm id
732 *
733 * @algorithm_id: algorithm defined in enum tpm2_algorithms
734 * Return: algorithm name string or ""
735 */
736const char *tpm2_algorithm_name(enum tpm2_algorithms);
737
Ilias Apalodimascb356612024-06-23 14:48:17 +0300738/**
Raymond Mao43158122024-12-24 08:01:07 -0800739 * tpm2_algorithm_supported() - Check if the algorithm supported by U-Boot
740 *
741 * @algorithm_id: algorithm defined in enum tpm2_algorithms
742 * Return: true if supported, otherwise false
743 */
744bool tpm2_algorithm_supported(enum tpm2_algorithms algo);
745
746/**
Ilias Apalodimas1e665f92024-06-23 14:48:18 +0300747 * tpm2_algorithm_to_len() - Return an algorithm length for supported algorithm id
748 *
749 * @algorithm_id: algorithm defined in enum tpm2_algorithms
750 * Return: len or 0 if not supported
751 */
752u16 tpm2_algorithm_to_len(enum tpm2_algorithms algo);
753
754/*
755 * When measured boot is enabled via EFI or bootX commands all the algorithms
756 * above are selected by our Kconfigs. Due to U-Boots nature of being small there
757 * are cases where we need some functionality from the TPM -- e.g storage or RNG
758 * but we don't want to support measurements.
759 *
760 * The choice of hash algorithms are determined by the platform and the TPM
761 * configuration. Failing to cap a PCR in a bank which the platform left
762 * active is a security vulnerability. It permits the unsealing of secrets
763 * if an attacker can replay a good set of measurements into an unused bank.
764 *
765 * On top of that a previous stage bootloader (e.g TF-A), migh pass an eventlog
766 * since it doesn't have a TPM driver, which U-Boot needs to replace. The algorit h
767 * choice is a compile time option in that case and we need to make sure we conform.
768 *
769 * Add a variable here that sums the supported algorithms U-Boot was compiled
770 * with so we can refuse to do measurements if we don't support all of them
771 */
772
773/**
Ilias Apalodimasd788b062024-12-24 08:01:05 -0800774 * tpm2_check_active_banks() - Check if the active PCR banks are supported by
775 * our configuration
Ilias Apalodimas1e665f92024-06-23 14:48:18 +0300776 *
777 * @dev: TPM device
778 * Return: true if allowed
779 */
Ilias Apalodimasd788b062024-12-24 08:01:05 -0800780bool tpm2_check_active_banks(struct udevice *dev);
Ilias Apalodimas1e665f92024-06-23 14:48:18 +0300781
782/**
Ilias Apalodimas9465f7a2024-12-24 08:01:04 -0800783 * tpm2_is_active_bank() - check the pcr_select. If at least one of the PCRs
784 * supports the algorithm add it on the active ones
Ilias Apalodimascb356612024-06-23 14:48:17 +0300785 *
786 * @selection: PCR selection structure
787 * Return: True if the algorithm is active
788 */
Ilias Apalodimas9465f7a2024-12-24 08:01:04 -0800789bool tpm2_is_active_bank(struct tpms_pcr_selection *selection);
Ilias Apalodimascb356612024-06-23 14:48:17 +0300790
Ilias Apalodimas7b1e5222024-12-24 08:01:08 -0800791/**
792 * tpm2_print_active_banks() - Print the active TPM PCRs
793 *
794 * @dev: TPM device
795 */
796void tpm2_print_active_banks(struct udevice *dev);
797
Miquel Raynalf3b43502018-05-15 11:57:08 +0200798#endif /* __TPM_V2_H */