1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (c) 2018 Bootlin
4 * Author: Miquel Raynal <miquel.raynal@bootlin.com>
9 #include <tpm-common.h>
11 #include <linux/bitops.h>
12 #include "tpm-utils.h"
14 u32 tpm2_startup(struct udevice *dev, enum tpm2_startup_types mode)
16 const u8 command_v2[12] = {
17 tpm_u16(TPM2_ST_NO_SESSIONS),
19 tpm_u32(TPM2_CC_STARTUP),
25 * Note TPM2_Startup command will return RC_SUCCESS the first time,
26 * but will return RC_INITIALIZE otherwise.
28 ret = tpm_sendrecv_command(dev, command_v2, NULL, NULL);
29 if (ret && ret != TPM2_RC_INITIALIZE)
35 u32 tpm2_self_test(struct udevice *dev, enum tpm2_yes_no full_test)
37 const u8 command_v2[12] = {
38 tpm_u16(TPM2_ST_NO_SESSIONS),
40 tpm_u32(TPM2_CC_SELF_TEST),
44 return tpm_sendrecv_command(dev, command_v2, NULL, NULL);
47 u32 tpm2_clear(struct udevice *dev, u32 handle, const char *pw,
50 /* Length of the message header, up to start of password */
52 u8 command_v2[COMMAND_BUFFER_SIZE] = {
53 tpm_u16(TPM2_ST_SESSIONS), /* TAG */
54 tpm_u32(offset + pw_sz), /* Length */
55 tpm_u32(TPM2_CC_CLEAR), /* Command code */
58 tpm_u32(handle), /* TPM resource handle */
61 tpm_u32(9 + pw_sz), /* Authorization size */
62 tpm_u32(TPM2_RS_PW), /* Session handle */
63 tpm_u16(0), /* Size of <nonce> */
64 /* <nonce> (if any) */
65 0, /* Attributes: Cont/Excl/Rst */
66 tpm_u16(pw_sz), /* Size of <hmac/password> */
67 /* STRING(pw) <hmac/password> (if any) */
72 * Fill the command structure starting from the first buffer:
73 * - the password (if any)
75 ret = pack_byte_string(command_v2, sizeof(command_v2), "s",
81 return tpm_sendrecv_command(dev, command_v2, NULL, NULL);
84 u32 tpm2_nv_define_space(struct udevice *dev, u32 space_index,
85 size_t space_size, u32 nv_attributes,
86 const u8 *nv_policy, size_t nv_policy_size)
89 * Calculate the offset of the nv_policy piece by adding each of the
92 const int platform_len = sizeof(u32);
93 const int session_hdr_len = 13;
94 const int message_len = 14;
95 uint offset = TPM2_HDR_LEN + platform_len + session_hdr_len +
97 u8 command_v2[COMMAND_BUFFER_SIZE] = {
99 tpm_u16(TPM2_ST_SESSIONS), /* TAG */
100 tpm_u32(offset + nv_policy_size + 2),/* Length */
101 tpm_u32(TPM2_CC_NV_DEFINE_SPACE),/* Command code */
103 /* handles 4 bytes */
104 tpm_u32(TPM2_RH_PLATFORM), /* Primary platform seed */
106 /* session header 13 bytes */
107 tpm_u32(9), /* Header size */
108 tpm_u32(TPM2_RS_PW), /* Password authorisation */
109 tpm_u16(0), /* nonce_size */
110 0, /* session_attrs */
111 tpm_u16(0), /* auth_size */
113 /* message 14 bytes + policy */
114 tpm_u16(message_len + nv_policy_size), /* size */
115 tpm_u32(space_index),
116 tpm_u16(TPM2_ALG_SHA256),
117 tpm_u32(nv_attributes),
118 tpm_u16(nv_policy_size),
127 * Fill the command structure starting from the first buffer:
128 * - the password (if any)
130 ret = pack_byte_string(command_v2, sizeof(command_v2), "sw",
131 offset, nv_policy, nv_policy_size,
132 offset + nv_policy_size, space_size);
134 return TPM_LIB_ERROR;
136 return tpm_sendrecv_command(dev, command_v2, NULL, NULL);
139 u32 tpm2_pcr_extend(struct udevice *dev, u32 index, u32 algorithm,
140 const u8 *digest, u32 digest_len)
142 /* Length of the message header, up to start of digest */
144 u8 command_v2[COMMAND_BUFFER_SIZE] = {
145 tpm_u16(TPM2_ST_SESSIONS), /* TAG */
146 tpm_u32(offset + digest_len), /* Length */
147 tpm_u32(TPM2_CC_PCR_EXTEND), /* Command code */
150 tpm_u32(index), /* Handle (PCR Index) */
153 tpm_u32(9), /* Authorization size */
154 tpm_u32(TPM2_RS_PW), /* Session handle */
155 tpm_u16(0), /* Size of <nonce> */
156 /* <nonce> (if any) */
157 0, /* Attributes: Cont/Excl/Rst */
158 tpm_u16(0), /* Size of <hmac/password> */
159 /* <hmac/password> (if any) */
162 tpm_u32(1), /* Count (number of hashes) */
163 tpm_u16(algorithm), /* Algorithm of the hash */
164 /* STRING(digest) Digest */
171 * Fill the command structure starting from the first buffer:
174 ret = pack_byte_string(command_v2, sizeof(command_v2), "s",
175 offset, digest, digest_len);
177 return TPM_LIB_ERROR;
179 return tpm_sendrecv_command(dev, command_v2, NULL, NULL);
182 u32 tpm2_nv_read_value(struct udevice *dev, u32 index, void *data, u32 count)
184 u8 command_v2[COMMAND_BUFFER_SIZE] = {
185 /* header 10 bytes */
186 tpm_u16(TPM2_ST_SESSIONS), /* TAG */
187 tpm_u32(10 + 8 + 4 + 9 + 4), /* Length */
188 tpm_u32(TPM2_CC_NV_READ), /* Command code */
190 /* handles 8 bytes */
191 tpm_u32(TPM2_RH_PLATFORM), /* Primary platform seed */
192 tpm_u32(HR_NV_INDEX + index), /* Password authorisation */
195 tpm_u32(9), /* Authorization size */
196 tpm_u32(TPM2_RS_PW), /* Session handle */
197 tpm_u16(0), /* Size of <nonce> */
198 /* <nonce> (if any) */
199 0, /* Attributes: Cont/Excl/Rst */
200 tpm_u16(0), /* Size of <hmac/password> */
201 /* <hmac/password> (if any) */
203 tpm_u16(count), /* Number of bytes */
204 tpm_u16(0), /* Offset */
206 size_t response_len = COMMAND_BUFFER_SIZE;
207 u8 response[COMMAND_BUFFER_SIZE];
212 ret = tpm_sendrecv_command(dev, command_v2, response, &response_len);
214 return log_msg_ret("read", ret);
215 if (unpack_byte_string(response, response_len, "wdds",
216 0, &tag, 2, &size, 6, &code,
218 return TPM_LIB_ERROR;
223 u32 tpm2_nv_write_value(struct udevice *dev, u32 index, const void *data,
226 struct tpm_chip_priv *priv = dev_get_uclass_priv(dev);
227 uint offset = 10 + 8 + 4 + 9 + 2;
228 uint len = offset + count + 2;
229 /* Use empty password auth if platform hierarchy is disabled */
230 u32 auth = priv->plat_hier_disabled ? HR_NV_INDEX + index :
232 u8 command_v2[COMMAND_BUFFER_SIZE] = {
233 /* header 10 bytes */
234 tpm_u16(TPM2_ST_SESSIONS), /* TAG */
235 tpm_u32(len), /* Length */
236 tpm_u32(TPM2_CC_NV_WRITE), /* Command code */
238 /* handles 8 bytes */
239 tpm_u32(auth), /* Primary platform seed */
240 tpm_u32(HR_NV_INDEX + index), /* Password authorisation */
243 tpm_u32(9), /* Authorization size */
244 tpm_u32(TPM2_RS_PW), /* Session handle */
245 tpm_u16(0), /* Size of <nonce> */
246 /* <nonce> (if any) */
247 0, /* Attributes: Cont/Excl/Rst */
248 tpm_u16(0), /* Size of <hmac/password> */
249 /* <hmac/password> (if any) */
253 size_t response_len = COMMAND_BUFFER_SIZE;
254 u8 response[COMMAND_BUFFER_SIZE];
257 ret = pack_byte_string(command_v2, sizeof(command_v2), "sw",
261 return TPM_LIB_ERROR;
263 return tpm_sendrecv_command(dev, command_v2, response, &response_len);
266 u32 tpm2_pcr_read(struct udevice *dev, u32 idx, unsigned int idx_min_sz,
267 u16 algorithm, void *data, u32 digest_len,
268 unsigned int *updates)
270 u8 idx_array_sz = max(idx_min_sz, DIV_ROUND_UP(idx, 8));
271 u8 command_v2[COMMAND_BUFFER_SIZE] = {
272 tpm_u16(TPM2_ST_NO_SESSIONS), /* TAG */
273 tpm_u32(17 + idx_array_sz), /* Length */
274 tpm_u32(TPM2_CC_PCR_READ), /* Command code */
276 /* TPML_PCR_SELECTION */
277 tpm_u32(1), /* Number of selections */
278 tpm_u16(algorithm), /* Algorithm of the hash */
279 idx_array_sz, /* Array size for selection */
280 /* bitmap(idx) Selected PCR bitmap */
282 size_t response_len = COMMAND_BUFFER_SIZE;
283 u8 response[COMMAND_BUFFER_SIZE];
284 unsigned int pcr_sel_idx = idx / 8;
285 u8 pcr_sel_bit = BIT(idx % 8);
286 unsigned int counter = 0;
289 if (pack_byte_string(command_v2, COMMAND_BUFFER_SIZE, "b",
290 17 + pcr_sel_idx, pcr_sel_bit))
291 return TPM_LIB_ERROR;
293 ret = tpm_sendrecv_command(dev, command_v2, response, &response_len);
297 if (digest_len > response_len)
298 return TPM_LIB_ERROR;
300 if (unpack_byte_string(response, response_len, "ds",
302 response_len - digest_len, data,
304 return TPM_LIB_ERROR;
312 u32 tpm2_get_capability(struct udevice *dev, u32 capability, u32 property,
313 void *buf, size_t prop_count)
315 u8 command_v2[COMMAND_BUFFER_SIZE] = {
316 tpm_u16(TPM2_ST_NO_SESSIONS), /* TAG */
317 tpm_u32(22), /* Length */
318 tpm_u32(TPM2_CC_GET_CAPABILITY), /* Command code */
320 tpm_u32(capability), /* Capability */
321 tpm_u32(property), /* Property */
322 tpm_u32(prop_count), /* Property count */
324 u8 response[COMMAND_BUFFER_SIZE];
325 size_t response_len = COMMAND_BUFFER_SIZE;
326 unsigned int properties_off;
329 ret = tpm_sendrecv_command(dev, command_v2, response, &response_len);
334 * In the response buffer, the properties are located after the:
335 * tag (u16), response size (u32), response code (u32),
336 * YES/NO flag (u8), TPM_CAP (u32).
338 properties_off = sizeof(u16) + sizeof(u32) + sizeof(u32) +
339 sizeof(u8) + sizeof(u32);
340 memcpy(buf, &response[properties_off], response_len - properties_off);
345 u32 tpm2_dam_reset(struct udevice *dev, const char *pw, const ssize_t pw_sz)
347 u8 command_v2[COMMAND_BUFFER_SIZE] = {
348 tpm_u16(TPM2_ST_SESSIONS), /* TAG */
349 tpm_u32(27 + pw_sz), /* Length */
350 tpm_u32(TPM2_CC_DAM_RESET), /* Command code */
353 tpm_u32(TPM2_RH_LOCKOUT), /* TPM resource handle */
356 tpm_u32(9 + pw_sz), /* Authorization size */
357 tpm_u32(TPM2_RS_PW), /* Session handle */
358 tpm_u16(0), /* Size of <nonce> */
359 /* <nonce> (if any) */
360 0, /* Attributes: Cont/Excl/Rst */
361 tpm_u16(pw_sz), /* Size of <hmac/password> */
362 /* STRING(pw) <hmac/password> (if any) */
364 unsigned int offset = 27;
368 * Fill the command structure starting from the first buffer:
369 * - the password (if any)
371 ret = pack_byte_string(command_v2, sizeof(command_v2), "s",
375 return TPM_LIB_ERROR;
377 return tpm_sendrecv_command(dev, command_v2, NULL, NULL);
380 u32 tpm2_dam_parameters(struct udevice *dev, const char *pw,
381 const ssize_t pw_sz, unsigned int max_tries,
382 unsigned int recovery_time,
383 unsigned int lockout_recovery)
385 u8 command_v2[COMMAND_BUFFER_SIZE] = {
386 tpm_u16(TPM2_ST_SESSIONS), /* TAG */
387 tpm_u32(27 + pw_sz + 12), /* Length */
388 tpm_u32(TPM2_CC_DAM_PARAMETERS), /* Command code */
391 tpm_u32(TPM2_RH_LOCKOUT), /* TPM resource handle */
394 tpm_u32(9 + pw_sz), /* Authorization size */
395 tpm_u32(TPM2_RS_PW), /* Session handle */
396 tpm_u16(0), /* Size of <nonce> */
397 /* <nonce> (if any) */
398 0, /* Attributes: Cont/Excl/Rst */
399 tpm_u16(pw_sz), /* Size of <hmac/password> */
400 /* STRING(pw) <hmac/password> (if any) */
402 /* LOCKOUT PARAMETERS */
403 /* tpm_u32(max_tries) Max tries (0, always lock) */
404 /* tpm_u32(recovery_time) Recovery time (0, no lock) */
405 /* tpm_u32(lockout_recovery) Lockout recovery */
407 unsigned int offset = 27;
411 * Fill the command structure starting from the first buffer:
412 * - the password (if any)
417 ret = pack_byte_string(command_v2, sizeof(command_v2), "sddd",
419 offset + pw_sz, max_tries,
420 offset + pw_sz + 4, recovery_time,
421 offset + pw_sz + 8, lockout_recovery);
422 offset += pw_sz + 12;
424 return TPM_LIB_ERROR;
426 return tpm_sendrecv_command(dev, command_v2, NULL, NULL);
429 int tpm2_change_auth(struct udevice *dev, u32 handle, const char *newpw,
430 const ssize_t newpw_sz, const char *oldpw,
431 const ssize_t oldpw_sz)
433 unsigned int offset = 27;
434 u8 command_v2[COMMAND_BUFFER_SIZE] = {
435 tpm_u16(TPM2_ST_SESSIONS), /* TAG */
436 tpm_u32(offset + oldpw_sz + 2 + newpw_sz), /* Length */
437 tpm_u32(TPM2_CC_HIERCHANGEAUTH), /* Command code */
440 tpm_u32(handle), /* TPM resource handle */
443 tpm_u32(9 + oldpw_sz), /* Authorization size */
444 tpm_u32(TPM2_RS_PW), /* Session handle */
445 tpm_u16(0), /* Size of <nonce> */
446 /* <nonce> (if any) */
447 0, /* Attributes: Cont/Excl/Rst */
448 tpm_u16(oldpw_sz) /* Size of <hmac/password> */
449 /* STRING(oldpw) <hmac/password> (if any) */
451 /* TPM2B_AUTH (TPM2B_DIGEST) */
452 /* tpm_u16(newpw_sz) Digest size, new pw length */
453 /* STRING(newpw) Digest buffer, new pw */
458 * Fill the command structure starting from the first buffer:
459 * - the old password (if any)
460 * - size of the new password
463 ret = pack_byte_string(command_v2, sizeof(command_v2), "sws",
464 offset, oldpw, oldpw_sz,
465 offset + oldpw_sz, newpw_sz,
466 offset + oldpw_sz + 2, newpw, newpw_sz);
467 offset += oldpw_sz + 2 + newpw_sz;
469 return TPM_LIB_ERROR;
471 return tpm_sendrecv_command(dev, command_v2, NULL, NULL);
474 u32 tpm2_pcr_setauthpolicy(struct udevice *dev, const char *pw,
475 const ssize_t pw_sz, u32 index, const char *key)
477 u8 command_v2[COMMAND_BUFFER_SIZE] = {
478 tpm_u16(TPM2_ST_SESSIONS), /* TAG */
479 tpm_u32(35 + pw_sz + TPM2_DIGEST_LEN), /* Length */
480 tpm_u32(TPM2_CC_PCR_SETAUTHPOL), /* Command code */
483 tpm_u32(TPM2_RH_PLATFORM), /* TPM resource handle */
486 tpm_u32(9 + pw_sz), /* Authorization size */
487 tpm_u32(TPM2_RS_PW), /* session handle */
488 tpm_u16(0), /* Size of <nonce> */
489 /* <nonce> (if any) */
490 0, /* Attributes: Cont/Excl/Rst */
491 tpm_u16(pw_sz) /* Size of <hmac/password> */
492 /* STRING(pw) <hmac/password> (if any) */
494 /* TPM2B_AUTH (TPM2B_DIGEST) */
495 /* tpm_u16(TPM2_DIGEST_LEN) Digest size length */
496 /* STRING(key) Digest buffer (PCR key) */
499 /* tpm_u16(TPM2_ALG_SHA256) Algorithm of the hash */
502 /* tpm_u32(index), PCR Index */
504 unsigned int offset = 27;
508 * Fill the command structure starting from the first buffer:
509 * - the password (if any)
510 * - the PCR key length
512 * - the hash algorithm
515 ret = pack_byte_string(command_v2, sizeof(command_v2), "swswd",
517 offset + pw_sz, TPM2_DIGEST_LEN,
518 offset + pw_sz + 2, key, TPM2_DIGEST_LEN,
519 offset + pw_sz + 2 + TPM2_DIGEST_LEN,
521 offset + pw_sz + 4 + TPM2_DIGEST_LEN, index);
522 offset += pw_sz + 2 + TPM2_DIGEST_LEN + 2 + 4;
524 return TPM_LIB_ERROR;
526 return tpm_sendrecv_command(dev, command_v2, NULL, NULL);
529 u32 tpm2_pcr_setauthvalue(struct udevice *dev, const char *pw,
530 const ssize_t pw_sz, u32 index, const char *key,
531 const ssize_t key_sz)
533 u8 command_v2[COMMAND_BUFFER_SIZE] = {
534 tpm_u16(TPM2_ST_SESSIONS), /* TAG */
535 tpm_u32(33 + pw_sz + TPM2_DIGEST_LEN), /* Length */
536 tpm_u32(TPM2_CC_PCR_SETAUTHVAL), /* Command code */
539 tpm_u32(index), /* Handle (PCR Index) */
542 tpm_u32(9 + pw_sz), /* Authorization size */
543 tpm_u32(TPM2_RS_PW), /* session handle */
544 tpm_u16(0), /* Size of <nonce> */
545 /* <nonce> (if any) */
546 0, /* Attributes: Cont/Excl/Rst */
547 tpm_u16(pw_sz), /* Size of <hmac/password> */
548 /* STRING(pw) <hmac/password> (if any) */
551 /* tpm_u16(key_sz) Key length */
552 /* STRING(key) Key */
554 unsigned int offset = 27;
558 * Fill the command structure starting from the first buffer:
559 * - the password (if any)
560 * - the number of digests, 1 in our case
561 * - the algorithm, sha256 in our case
562 * - the digest (64 bytes)
564 ret = pack_byte_string(command_v2, sizeof(command_v2), "sws",
566 offset + pw_sz, key_sz,
567 offset + pw_sz + 2, key, key_sz);
568 offset += pw_sz + 2 + key_sz;
570 return TPM_LIB_ERROR;
572 return tpm_sendrecv_command(dev, command_v2, NULL, NULL);
575 u32 tpm2_get_random(struct udevice *dev, void *data, u32 count)
577 const u8 command_v2[10] = {
578 tpm_u16(TPM2_ST_NO_SESSIONS),
580 tpm_u32(TPM2_CC_GET_RANDOM),
582 u8 buf[COMMAND_BUFFER_SIZE], response[COMMAND_BUFFER_SIZE];
584 const size_t data_size_offset = 10;
585 const size_t data_offset = 12;
586 size_t response_length = sizeof(response);
591 u32 this_bytes = min((size_t)count,
592 sizeof(response) - data_offset);
595 if (pack_byte_string(buf, sizeof(buf), "sw",
596 0, command_v2, sizeof(command_v2),
597 sizeof(command_v2), this_bytes))
598 return TPM_LIB_ERROR;
599 err = tpm_sendrecv_command(dev, buf, response,
603 if (unpack_byte_string(response, response_length, "w",
604 data_size_offset, &data_size))
605 return TPM_LIB_ERROR;
606 if (data_size > this_bytes)
607 return TPM_LIB_ERROR;
608 if (unpack_byte_string(response, response_length, "s",
609 data_offset, out, data_size))
610 return TPM_LIB_ERROR;
619 u32 tpm2_write_lock(struct udevice *dev, u32 index)
621 u8 command_v2[COMMAND_BUFFER_SIZE] = {
622 /* header 10 bytes */
623 tpm_u16(TPM2_ST_SESSIONS), /* TAG */
624 tpm_u32(10 + 8 + 13), /* Length */
625 tpm_u32(TPM2_CC_NV_WRITELOCK), /* Command code */
627 /* handles 8 bytes */
628 tpm_u32(TPM2_RH_PLATFORM), /* Primary platform seed */
629 tpm_u32(HR_NV_INDEX + index), /* Password authorisation */
631 /* session header 9 bytes */
632 tpm_u32(9), /* Header size */
633 tpm_u32(TPM2_RS_PW), /* Password authorisation */
634 tpm_u16(0), /* nonce_size */
635 0, /* session_attrs */
636 tpm_u16(0), /* auth_size */
639 return tpm_sendrecv_command(dev, command_v2, NULL, NULL);
642 u32 tpm2_disable_platform_hierarchy(struct udevice *dev)
644 struct tpm_chip_priv *priv = dev_get_uclass_priv(dev);
645 u8 command_v2[COMMAND_BUFFER_SIZE] = {
646 /* header 10 bytes */
647 tpm_u16(TPM2_ST_SESSIONS), /* TAG */
648 tpm_u32(10 + 4 + 13 + 5), /* Length */
649 tpm_u32(TPM2_CC_HIER_CONTROL), /* Command code */
652 tpm_u32(TPM2_RH_PLATFORM), /* Primary platform seed */
654 /* session header 9 bytes */
655 tpm_u32(9), /* Header size */
656 tpm_u32(TPM2_RS_PW), /* Password authorisation */
657 tpm_u16(0), /* nonce_size */
658 0, /* session_attrs */
659 tpm_u16(0), /* auth_size */
661 /* payload 5 bytes */
662 tpm_u32(TPM2_RH_PLATFORM), /* Hierarchy to disable */
667 ret = tpm_sendrecv_command(dev, command_v2, NULL, NULL);
668 log_info("ret=%s, %x\n", dev->name, ret);
672 priv->plat_hier_disabled = true;
677 u32 tpm2_submit_command(struct udevice *dev, const u8 *sendbuf,
678 u8 *recvbuf, size_t *recv_size)
680 return tpm_sendrecv_command(dev, sendbuf, recvbuf, recv_size);
683 u32 tpm2_report_state(struct udevice *dev, uint vendor_cmd, uint vendor_subcmd,
684 u8 *recvbuf, size_t *recv_size)
686 u8 command_v2[COMMAND_BUFFER_SIZE] = {
687 /* header 10 bytes */
688 tpm_u16(TPM2_ST_NO_SESSIONS), /* TAG */
689 tpm_u32(10 + 2), /* Length */
690 tpm_u32(vendor_cmd), /* Command code */
692 tpm_u16(vendor_subcmd),
696 ret = tpm_sendrecv_command(dev, command_v2, recvbuf, recv_size);
697 log_debug("ret=%s, %x\n", dev->name, ret);
703 memcpy(recvbuf, recvbuf + 12, *recv_size);
708 u32 tpm2_enable_nvcommits(struct udevice *dev, uint vendor_cmd,
711 u8 command_v2[COMMAND_BUFFER_SIZE] = {
712 /* header 10 bytes */
713 tpm_u16(TPM2_ST_NO_SESSIONS), /* TAG */
714 tpm_u32(10 + 2), /* Length */
715 tpm_u32(vendor_cmd), /* Command code */
717 tpm_u16(vendor_subcmd),
721 ret = tpm_sendrecv_command(dev, command_v2, NULL, NULL);
722 log_debug("ret=%s, %x\n", dev->name, ret);