1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2014, 2015 Intel Corporation
6 * Jarkko Sakkinen <jarkko.sakkinen@linux.intel.com>
8 * Maintained by: <tpmdd-devel@lists.sourceforge.net>
10 * This file contains TPM2 protocol implementations of the commands
11 * used by the kernel internally.
15 #include <crypto/hash_info.h>
16 #include <keys/trusted-type.h>
18 enum tpm2_object_attributes {
19 TPM2_OA_USER_WITH_AUTH = BIT(6),
22 enum tpm2_session_attributes {
23 TPM2_SA_CONTINUE_SESSION = BIT(0),
27 unsigned int crypto_id;
31 static struct tpm2_hash tpm2_hash_map[] = {
32 {HASH_ALGO_SHA1, TPM_ALG_SHA1},
33 {HASH_ALGO_SHA256, TPM_ALG_SHA256},
34 {HASH_ALGO_SHA384, TPM_ALG_SHA384},
35 {HASH_ALGO_SHA512, TPM_ALG_SHA512},
36 {HASH_ALGO_SM3_256, TPM_ALG_SM3_256},
39 int tpm2_get_timeouts(struct tpm_chip *chip)
41 /* Fixed timeouts for TPM2 */
42 chip->timeout_a = msecs_to_jiffies(TPM2_TIMEOUT_A);
43 chip->timeout_b = msecs_to_jiffies(TPM2_TIMEOUT_B);
44 chip->timeout_c = msecs_to_jiffies(TPM2_TIMEOUT_C);
45 chip->timeout_d = msecs_to_jiffies(TPM2_TIMEOUT_D);
47 /* PTP spec timeouts */
48 chip->duration[TPM_SHORT] = msecs_to_jiffies(TPM2_DURATION_SHORT);
49 chip->duration[TPM_MEDIUM] = msecs_to_jiffies(TPM2_DURATION_MEDIUM);
50 chip->duration[TPM_LONG] = msecs_to_jiffies(TPM2_DURATION_LONG);
52 /* Key creation commands long timeouts */
53 chip->duration[TPM_LONG_LONG] =
54 msecs_to_jiffies(TPM2_DURATION_LONG_LONG);
56 chip->flags |= TPM_CHIP_FLAG_HAVE_TIMEOUTS;
62 * tpm2_ordinal_duration_index() - returns an index to the chip duration table
63 * @ordinal: TPM command ordinal.
65 * The function returns an index to the chip duration table
66 * (enum tpm_duration), that describes the maximum amount of
67 * time the chip could take to return the result for a particular ordinal.
69 * The values of the MEDIUM, and LONG durations are taken
70 * from the PC Client Profile (PTP) specification (750, 2000 msec)
72 * LONG_LONG is for commands that generates keys which empirically takes
73 * a longer time on some systems.
81 static u8 tpm2_ordinal_duration_index(u32 ordinal)
85 case TPM2_CC_STARTUP: /* 144 */
88 case TPM2_CC_SELF_TEST: /* 143 */
91 case TPM2_CC_GET_RANDOM: /* 17B */
94 case TPM2_CC_SEQUENCE_UPDATE: /* 15C */
96 case TPM2_CC_SEQUENCE_COMPLETE: /* 13E */
98 case TPM2_CC_EVENT_SEQUENCE_COMPLETE: /* 185 */
100 case TPM2_CC_HASH_SEQUENCE_START: /* 186 */
103 case TPM2_CC_VERIFY_SIGNATURE: /* 177 */
106 case TPM2_CC_PCR_EXTEND: /* 182 */
109 case TPM2_CC_HIERARCHY_CONTROL: /* 121 */
111 case TPM2_CC_HIERARCHY_CHANGE_AUTH: /* 129 */
114 case TPM2_CC_GET_CAPABILITY: /* 17A */
117 case TPM2_CC_NV_READ: /* 14E */
120 case TPM2_CC_CREATE_PRIMARY: /* 131 */
121 return TPM_LONG_LONG;
122 case TPM2_CC_CREATE: /* 153 */
123 return TPM_LONG_LONG;
124 case TPM2_CC_CREATE_LOADED: /* 191 */
125 return TPM_LONG_LONG;
128 return TPM_UNDEFINED;
133 * tpm2_calc_ordinal_duration() - calculate the maximum command duration
134 * @chip: TPM chip to use.
135 * @ordinal: TPM command ordinal.
137 * The function returns the maximum amount of time the chip could take
138 * to return the result for a particular ordinal in jiffies.
140 * Return: A maximal duration time for an ordinal in jiffies.
142 unsigned long tpm2_calc_ordinal_duration(struct tpm_chip *chip, u32 ordinal)
146 index = tpm2_ordinal_duration_index(ordinal);
148 if (index != TPM_UNDEFINED)
149 return chip->duration[index];
151 return msecs_to_jiffies(TPM2_DURATION_DEFAULT);
155 struct tpm2_pcr_read_out {
157 __be32 pcr_selects_cnt;
160 u8 pcr_select[TPM2_PCR_SELECT_MIN];
167 * tpm2_pcr_read() - read a PCR value
168 * @chip: TPM chip to use.
169 * @pcr_idx: index of the PCR to read.
170 * @digest: PCR bank and buffer current PCR value is written to.
171 * @digest_size_ptr: pointer to variable that stores the digest size.
173 * Return: Same as with tpm_transmit_cmd.
175 int tpm2_pcr_read(struct tpm_chip *chip, u32 pcr_idx,
176 struct tpm_digest *digest, u16 *digest_size_ptr)
181 struct tpm2_pcr_read_out *out;
182 u8 pcr_select[TPM2_PCR_SELECT_MIN] = {0};
184 u16 expected_digest_size = 0;
186 if (pcr_idx >= TPM2_PLATFORM_PCR)
189 if (!digest_size_ptr) {
190 for (i = 0; i < chip->nr_allocated_banks &&
191 chip->allocated_banks[i].alg_id != digest->alg_id; i++)
194 if (i == chip->nr_allocated_banks)
197 expected_digest_size = chip->allocated_banks[i].digest_size;
200 rc = tpm_buf_init(&buf, TPM2_ST_NO_SESSIONS, TPM2_CC_PCR_READ);
204 pcr_select[pcr_idx >> 3] = 1 << (pcr_idx & 0x7);
206 tpm_buf_append_u32(&buf, 1);
207 tpm_buf_append_u16(&buf, digest->alg_id);
208 tpm_buf_append_u8(&buf, TPM2_PCR_SELECT_MIN);
209 tpm_buf_append(&buf, (const unsigned char *)pcr_select,
212 rc = tpm_transmit_cmd(chip, &buf, 0, "attempting to read a pcr value");
216 out = (struct tpm2_pcr_read_out *)&buf.data[TPM_HEADER_SIZE];
217 digest_size = be16_to_cpu(out->digest_size);
218 if (digest_size > sizeof(digest->digest) ||
219 (!digest_size_ptr && digest_size != expected_digest_size)) {
225 *digest_size_ptr = digest_size;
227 memcpy(digest->digest, out->digest, digest_size);
229 tpm_buf_destroy(&buf);
233 struct tpm2_null_auth_area {
241 * tpm2_pcr_extend() - extend a PCR value
243 * @chip: TPM chip to use.
244 * @pcr_idx: index of the PCR.
245 * @digests: list of pcr banks and corresponding digest values to extend.
247 * Return: Same as with tpm_transmit_cmd.
249 int tpm2_pcr_extend(struct tpm_chip *chip, u32 pcr_idx,
250 struct tpm_digest *digests)
253 struct tpm2_null_auth_area auth_area;
257 rc = tpm_buf_init(&buf, TPM2_ST_SESSIONS, TPM2_CC_PCR_EXTEND);
261 tpm_buf_append_u32(&buf, pcr_idx);
263 auth_area.handle = cpu_to_be32(TPM2_RS_PW);
264 auth_area.nonce_size = 0;
265 auth_area.attributes = 0;
266 auth_area.auth_size = 0;
268 tpm_buf_append_u32(&buf, sizeof(struct tpm2_null_auth_area));
269 tpm_buf_append(&buf, (const unsigned char *)&auth_area,
271 tpm_buf_append_u32(&buf, chip->nr_allocated_banks);
273 for (i = 0; i < chip->nr_allocated_banks; i++) {
274 tpm_buf_append_u16(&buf, digests[i].alg_id);
275 tpm_buf_append(&buf, (const unsigned char *)&digests[i].digest,
276 chip->allocated_banks[i].digest_size);
279 rc = tpm_transmit_cmd(chip, &buf, 0, "attempting extend a PCR value");
281 tpm_buf_destroy(&buf);
286 struct tpm2_get_random_out {
288 u8 buffer[TPM_MAX_RNG_DATA];
292 * tpm2_get_random() - get random bytes from the TPM RNG
294 * @chip: a &tpm_chip instance
295 * @dest: destination buffer
296 * @max: the max number of random bytes to pull
299 * size of the buffer on success,
300 * -errno otherwise (positive TPM return codes are masked to -EIO)
302 int tpm2_get_random(struct tpm_chip *chip, u8 *dest, size_t max)
304 struct tpm2_get_random_out *out;
313 if (!num_bytes || max > TPM_MAX_RNG_DATA)
316 err = tpm_buf_init(&buf, 0, 0);
321 tpm_buf_reset(&buf, TPM2_ST_NO_SESSIONS, TPM2_CC_GET_RANDOM);
322 tpm_buf_append_u16(&buf, num_bytes);
323 err = tpm_transmit_cmd(chip, &buf,
324 offsetof(struct tpm2_get_random_out,
326 "attempting get random");
333 out = (struct tpm2_get_random_out *)
334 &buf.data[TPM_HEADER_SIZE];
335 recd = min_t(u32, be16_to_cpu(out->size), num_bytes);
336 if (tpm_buf_length(&buf) <
338 offsetof(struct tpm2_get_random_out, buffer) +
343 memcpy(dest_ptr, out->buffer, recd);
348 } while (retries-- && total < max);
350 tpm_buf_destroy(&buf);
351 return total ? total : -EIO;
353 tpm_buf_destroy(&buf);
358 * tpm2_flush_context() - execute a TPM2_FlushContext command
359 * @chip: TPM chip to use
360 * @handle: context handle
362 void tpm2_flush_context(struct tpm_chip *chip, u32 handle)
367 rc = tpm_buf_init(&buf, TPM2_ST_NO_SESSIONS, TPM2_CC_FLUSH_CONTEXT);
369 dev_warn(&chip->dev, "0x%08x was not flushed, out of memory\n",
374 tpm_buf_append_u32(&buf, handle);
376 tpm_transmit_cmd(chip, &buf, 0, "flushing context");
377 tpm_buf_destroy(&buf);
381 * tpm_buf_append_auth() - append TPMS_AUTH_COMMAND to the buffer.
383 * @buf: an allocated tpm_buf instance
384 * @session_handle: session handle
385 * @nonce: the session nonce, may be NULL if not used
386 * @nonce_len: the session nonce length, may be 0 if not used
387 * @attributes: the session attributes
388 * @hmac: the session HMAC or password, may be NULL if not used
389 * @hmac_len: the session HMAC or password length, maybe 0 if not used
391 static void tpm2_buf_append_auth(struct tpm_buf *buf, u32 session_handle,
392 const u8 *nonce, u16 nonce_len,
394 const u8 *hmac, u16 hmac_len)
396 tpm_buf_append_u32(buf, 9 + nonce_len + hmac_len);
397 tpm_buf_append_u32(buf, session_handle);
398 tpm_buf_append_u16(buf, nonce_len);
400 if (nonce && nonce_len)
401 tpm_buf_append(buf, nonce, nonce_len);
403 tpm_buf_append_u8(buf, attributes);
404 tpm_buf_append_u16(buf, hmac_len);
406 if (hmac && hmac_len)
407 tpm_buf_append(buf, hmac, hmac_len);
411 * tpm2_seal_trusted() - seal the payload of a trusted key
413 * @chip: TPM chip to use
414 * @payload: the key data in clear and encrypted form
415 * @options: authentication values and other options
417 * Return: < 0 on error and 0 on success.
419 int tpm2_seal_trusted(struct tpm_chip *chip,
420 struct trusted_key_payload *payload,
421 struct trusted_key_options *options)
423 unsigned int blob_len;
429 for (i = 0; i < ARRAY_SIZE(tpm2_hash_map); i++) {
430 if (options->hash == tpm2_hash_map[i].crypto_id) {
431 hash = tpm2_hash_map[i].tpm_id;
436 if (i == ARRAY_SIZE(tpm2_hash_map))
439 rc = tpm_buf_init(&buf, TPM2_ST_SESSIONS, TPM2_CC_CREATE);
443 tpm_buf_append_u32(&buf, options->keyhandle);
444 tpm2_buf_append_auth(&buf, TPM2_RS_PW,
446 0 /* session_attributes */,
447 options->keyauth /* hmac */,
451 tpm_buf_append_u16(&buf, 4 + TPM_DIGEST_SIZE + payload->key_len + 1);
453 tpm_buf_append_u16(&buf, TPM_DIGEST_SIZE);
454 tpm_buf_append(&buf, options->blobauth, TPM_DIGEST_SIZE);
455 tpm_buf_append_u16(&buf, payload->key_len + 1);
456 tpm_buf_append(&buf, payload->key, payload->key_len);
457 tpm_buf_append_u8(&buf, payload->migratable);
460 tpm_buf_append_u16(&buf, 14 + options->policydigest_len);
461 tpm_buf_append_u16(&buf, TPM_ALG_KEYEDHASH);
462 tpm_buf_append_u16(&buf, hash);
465 if (options->policydigest_len) {
466 tpm_buf_append_u32(&buf, 0);
467 tpm_buf_append_u16(&buf, options->policydigest_len);
468 tpm_buf_append(&buf, options->policydigest,
469 options->policydigest_len);
471 tpm_buf_append_u32(&buf, TPM2_OA_USER_WITH_AUTH);
472 tpm_buf_append_u16(&buf, 0);
475 /* public parameters */
476 tpm_buf_append_u16(&buf, TPM_ALG_NULL);
477 tpm_buf_append_u16(&buf, 0);
480 tpm_buf_append_u16(&buf, 0);
483 tpm_buf_append_u32(&buf, 0);
485 if (buf.flags & TPM_BUF_OVERFLOW) {
490 rc = tpm_transmit_cmd(chip, &buf, 4, "sealing data");
494 blob_len = be32_to_cpup((__be32 *) &buf.data[TPM_HEADER_SIZE]);
495 if (blob_len > MAX_BLOB_SIZE) {
499 if (tpm_buf_length(&buf) < TPM_HEADER_SIZE + 4 + blob_len) {
504 memcpy(payload->blob, &buf.data[TPM_HEADER_SIZE + 4], blob_len);
505 payload->blob_len = blob_len;
508 tpm_buf_destroy(&buf);
511 if (tpm2_rc_value(rc) == TPM2_RC_HASH)
521 * tpm2_load_cmd() - execute a TPM2_Load command
523 * @chip: TPM chip to use
524 * @payload: the key data in clear and encrypted form
525 * @options: authentication values and other options
526 * @blob_handle: returned blob handle
528 * Return: 0 on success.
529 * -E2BIG on wrong payload size.
530 * -EPERM on tpm error status.
531 * < 0 error from tpm_transmit_cmd.
533 static int tpm2_load_cmd(struct tpm_chip *chip,
534 struct trusted_key_payload *payload,
535 struct trusted_key_options *options,
539 unsigned int private_len;
540 unsigned int public_len;
541 unsigned int blob_len;
544 private_len = be16_to_cpup((__be16 *) &payload->blob[0]);
545 if (private_len > (payload->blob_len - 2))
548 public_len = be16_to_cpup((__be16 *) &payload->blob[2 + private_len]);
549 blob_len = private_len + public_len + 4;
550 if (blob_len > payload->blob_len)
553 rc = tpm_buf_init(&buf, TPM2_ST_SESSIONS, TPM2_CC_LOAD);
557 tpm_buf_append_u32(&buf, options->keyhandle);
558 tpm2_buf_append_auth(&buf, TPM2_RS_PW,
560 0 /* session_attributes */,
561 options->keyauth /* hmac */,
564 tpm_buf_append(&buf, payload->blob, blob_len);
566 if (buf.flags & TPM_BUF_OVERFLOW) {
571 rc = tpm_transmit_cmd(chip, &buf, 4, "loading blob");
573 *blob_handle = be32_to_cpup(
574 (__be32 *) &buf.data[TPM_HEADER_SIZE]);
577 tpm_buf_destroy(&buf);
586 * tpm2_unseal_cmd() - execute a TPM2_Unload command
588 * @chip: TPM chip to use
589 * @payload: the key data in clear and encrypted form
590 * @options: authentication values and other options
591 * @blob_handle: blob handle
593 * Return: 0 on success
594 * -EPERM on tpm error status
595 * < 0 error from tpm_transmit_cmd
597 static int tpm2_unseal_cmd(struct tpm_chip *chip,
598 struct trusted_key_payload *payload,
599 struct trusted_key_options *options,
607 rc = tpm_buf_init(&buf, TPM2_ST_SESSIONS, TPM2_CC_UNSEAL);
611 tpm_buf_append_u32(&buf, blob_handle);
612 tpm2_buf_append_auth(&buf,
613 options->policyhandle ?
614 options->policyhandle : TPM2_RS_PW,
616 TPM2_SA_CONTINUE_SESSION,
617 options->blobauth /* hmac */,
620 rc = tpm_transmit_cmd(chip, &buf, 6, "unsealing");
625 data_len = be16_to_cpup(
626 (__be16 *) &buf.data[TPM_HEADER_SIZE + 4]);
627 if (data_len < MIN_KEY_SIZE || data_len > MAX_KEY_SIZE + 1) {
632 if (tpm_buf_length(&buf) < TPM_HEADER_SIZE + 6 + data_len) {
636 data = &buf.data[TPM_HEADER_SIZE + 6];
638 memcpy(payload->key, data, data_len - 1);
639 payload->key_len = data_len - 1;
640 payload->migratable = data[data_len - 1];
644 tpm_buf_destroy(&buf);
649 * tpm2_unseal_trusted() - unseal the payload of a trusted key
651 * @chip: TPM chip to use
652 * @payload: the key data in clear and encrypted form
653 * @options: authentication values and other options
655 * Return: Same as with tpm_transmit_cmd.
657 int tpm2_unseal_trusted(struct tpm_chip *chip,
658 struct trusted_key_payload *payload,
659 struct trusted_key_options *options)
664 rc = tpm2_load_cmd(chip, payload, options, &blob_handle);
668 rc = tpm2_unseal_cmd(chip, payload, options, blob_handle);
669 tpm2_flush_context(chip, blob_handle);
673 struct tpm2_get_cap_out {
682 * tpm2_get_tpm_pt() - get value of a TPM_CAP_TPM_PROPERTIES type property
683 * @chip: a &tpm_chip instance
684 * @property_id: property ID.
685 * @value: output variable.
686 * @desc: passed to tpm_transmit_cmd()
690 * -errno or a TPM return code otherwise
692 ssize_t tpm2_get_tpm_pt(struct tpm_chip *chip, u32 property_id, u32 *value,
695 struct tpm2_get_cap_out *out;
699 rc = tpm_buf_init(&buf, TPM2_ST_NO_SESSIONS, TPM2_CC_GET_CAPABILITY);
702 tpm_buf_append_u32(&buf, TPM2_CAP_TPM_PROPERTIES);
703 tpm_buf_append_u32(&buf, property_id);
704 tpm_buf_append_u32(&buf, 1);
705 rc = tpm_transmit_cmd(chip, &buf, 0, NULL);
707 out = (struct tpm2_get_cap_out *)
708 &buf.data[TPM_HEADER_SIZE];
709 *value = be32_to_cpu(out->value);
711 tpm_buf_destroy(&buf);
714 EXPORT_SYMBOL_GPL(tpm2_get_tpm_pt);
717 * tpm2_shutdown() - send a TPM shutdown command
719 * Sends a TPM shutdown command. The shutdown command is used in call
720 * sites where the system is going down. If it fails, there is not much
721 * that can be done except print an error message.
723 * @chip: a &tpm_chip instance
724 * @shutdown_type: TPM_SU_CLEAR or TPM_SU_STATE.
726 void tpm2_shutdown(struct tpm_chip *chip, u16 shutdown_type)
731 rc = tpm_buf_init(&buf, TPM2_ST_NO_SESSIONS, TPM2_CC_SHUTDOWN);
734 tpm_buf_append_u16(&buf, shutdown_type);
735 tpm_transmit_cmd(chip, &buf, 0, "stopping the TPM");
736 tpm_buf_destroy(&buf);
740 * tpm2_do_selftest() - ensure that all self tests have passed
742 * @chip: TPM chip to use
744 * Return: Same as with tpm_transmit_cmd.
746 * The TPM can either run all self tests synchronously and then return
747 * RC_SUCCESS once all tests were successful. Or it can choose to run the tests
748 * asynchronously and return RC_TESTING immediately while the self tests still
749 * execute in the background. This function handles both cases and waits until
750 * all tests have completed.
752 static int tpm2_do_selftest(struct tpm_chip *chip)
758 for (full = 0; full < 2; full++) {
759 rc = tpm_buf_init(&buf, TPM2_ST_NO_SESSIONS, TPM2_CC_SELF_TEST);
763 tpm_buf_append_u8(&buf, full);
764 rc = tpm_transmit_cmd(chip, &buf, 0,
765 "attempting the self test");
766 tpm_buf_destroy(&buf);
768 if (rc == TPM2_RC_TESTING)
769 rc = TPM2_RC_SUCCESS;
770 if (rc == TPM2_RC_INITIALIZE || rc == TPM2_RC_SUCCESS)
778 * tpm2_probe() - probe for the TPM 2.0 protocol
779 * @chip: a &tpm_chip instance
781 * Send an idempotent TPM 2.0 command and see whether there is TPM2 chip in the
782 * other end based on the response tag. The flag TPM_CHIP_FLAG_TPM2 is set by
783 * this function if this is the case.
789 int tpm2_probe(struct tpm_chip *chip)
791 struct tpm_header *out;
795 rc = tpm_buf_init(&buf, TPM2_ST_NO_SESSIONS, TPM2_CC_GET_CAPABILITY);
798 tpm_buf_append_u32(&buf, TPM2_CAP_TPM_PROPERTIES);
799 tpm_buf_append_u32(&buf, TPM_PT_TOTAL_COMMANDS);
800 tpm_buf_append_u32(&buf, 1);
801 rc = tpm_transmit_cmd(chip, &buf, 0, NULL);
802 /* We ignore TPM return codes on purpose. */
804 out = (struct tpm_header *)buf.data;
805 if (be16_to_cpu(out->tag) == TPM2_ST_NO_SESSIONS)
806 chip->flags |= TPM_CHIP_FLAG_TPM2;
808 tpm_buf_destroy(&buf);
811 EXPORT_SYMBOL_GPL(tpm2_probe);
813 static int tpm2_init_bank_info(struct tpm_chip *chip, u32 bank_index)
815 struct tpm_bank_info *bank = chip->allocated_banks + bank_index;
816 struct tpm_digest digest = { .alg_id = bank->alg_id };
820 * Avoid unnecessary PCR read operations to reduce overhead
821 * and obtain identifiers of the crypto subsystem.
823 for (i = 0; i < ARRAY_SIZE(tpm2_hash_map); i++) {
824 enum hash_algo crypto_algo = tpm2_hash_map[i].crypto_id;
826 if (bank->alg_id != tpm2_hash_map[i].tpm_id)
829 bank->digest_size = hash_digest_size[crypto_algo];
830 bank->crypto_id = crypto_algo;
834 return tpm2_pcr_read(chip, 0, &digest, &bank->digest_size);
837 struct tpm2_pcr_selection {
843 ssize_t tpm2_get_pcr_allocation(struct tpm_chip *chip)
845 struct tpm2_pcr_selection pcr_selection;
849 void *pcr_select_offset;
850 u32 sizeof_pcr_selection;
851 u32 nr_possible_banks;
852 u32 nr_alloc_banks = 0;
858 rc = tpm_buf_init(&buf, TPM2_ST_NO_SESSIONS, TPM2_CC_GET_CAPABILITY);
862 tpm_buf_append_u32(&buf, TPM2_CAP_PCRS);
863 tpm_buf_append_u32(&buf, 0);
864 tpm_buf_append_u32(&buf, 1);
866 rc = tpm_transmit_cmd(chip, &buf, 9, "get tpm pcr allocation");
870 nr_possible_banks = be32_to_cpup(
871 (__be32 *)&buf.data[TPM_HEADER_SIZE + 5]);
873 chip->allocated_banks = kcalloc(nr_possible_banks,
874 sizeof(*chip->allocated_banks),
876 if (!chip->allocated_banks) {
881 marker = &buf.data[TPM_HEADER_SIZE + 9];
883 rsp_len = be32_to_cpup((__be32 *)&buf.data[2]);
884 end = &buf.data[rsp_len];
886 for (i = 0; i < nr_possible_banks; i++) {
887 pcr_select_offset = marker +
888 offsetof(struct tpm2_pcr_selection, size_of_select);
889 if (pcr_select_offset >= end) {
894 memcpy(&pcr_selection, marker, sizeof(pcr_selection));
895 hash_alg = be16_to_cpu(pcr_selection.hash_alg);
897 pcr_select_offset = memchr_inv(pcr_selection.pcr_select, 0,
898 pcr_selection.size_of_select);
899 if (pcr_select_offset) {
900 chip->allocated_banks[nr_alloc_banks].alg_id = hash_alg;
902 rc = tpm2_init_bank_info(chip, nr_alloc_banks);
909 sizeof_pcr_selection = sizeof(pcr_selection.hash_alg) +
910 sizeof(pcr_selection.size_of_select) +
911 pcr_selection.size_of_select;
912 marker = marker + sizeof_pcr_selection;
915 chip->nr_allocated_banks = nr_alloc_banks;
917 tpm_buf_destroy(&buf);
922 static int tpm2_get_cc_attrs_tbl(struct tpm_chip *chip)
931 rc = tpm2_get_tpm_pt(chip, TPM_PT_TOTAL_COMMANDS, &nr_commands, NULL);
935 if (nr_commands > 0xFFFFF) {
940 chip->cc_attrs_tbl = devm_kcalloc(&chip->dev, 4, nr_commands,
943 rc = tpm_buf_init(&buf, TPM2_ST_NO_SESSIONS, TPM2_CC_GET_CAPABILITY);
947 tpm_buf_append_u32(&buf, TPM2_CAP_COMMANDS);
948 tpm_buf_append_u32(&buf, TPM2_CC_FIRST);
949 tpm_buf_append_u32(&buf, nr_commands);
951 rc = tpm_transmit_cmd(chip, &buf, 9 + 4 * nr_commands, NULL);
953 tpm_buf_destroy(&buf);
958 be32_to_cpup((__be32 *)&buf.data[TPM_HEADER_SIZE + 5])) {
959 tpm_buf_destroy(&buf);
963 chip->nr_commands = nr_commands;
965 attrs = (__be32 *)&buf.data[TPM_HEADER_SIZE + 9];
966 for (i = 0; i < nr_commands; i++, attrs++) {
967 chip->cc_attrs_tbl[i] = be32_to_cpup(attrs);
968 cc = chip->cc_attrs_tbl[i] & 0xFFFF;
970 if (cc == TPM2_CC_CONTEXT_SAVE || cc == TPM2_CC_FLUSH_CONTEXT) {
971 chip->cc_attrs_tbl[i] &=
972 ~(GENMASK(2, 0) << TPM2_CC_ATTR_CHANDLES);
973 chip->cc_attrs_tbl[i] |= 1 << TPM2_CC_ATTR_CHANDLES;
977 tpm_buf_destroy(&buf);
986 * tpm2_startup - turn on the TPM
987 * @chip: TPM chip to use
989 * Normally the firmware should start the TPM. This function is provided as a
990 * workaround if this does not happen. A legal case for this could be for
991 * example when a TPM emulator is used.
993 * Return: same as tpm_transmit_cmd()
996 static int tpm2_startup(struct tpm_chip *chip)
1001 dev_info(&chip->dev, "starting up the TPM manually\n");
1003 rc = tpm_buf_init(&buf, TPM2_ST_NO_SESSIONS, TPM2_CC_STARTUP);
1007 tpm_buf_append_u16(&buf, TPM2_SU_CLEAR);
1008 rc = tpm_transmit_cmd(chip, &buf, 0, "attempting to start the TPM");
1009 tpm_buf_destroy(&buf);
1015 * tpm2_auto_startup - Perform the standard automatic TPM initialization
1017 * @chip: TPM chip to use
1019 * Returns 0 on success, < 0 in case of fatal error.
1021 int tpm2_auto_startup(struct tpm_chip *chip)
1025 rc = tpm2_get_timeouts(chip);
1029 rc = tpm2_do_selftest(chip);
1030 if (rc && rc != TPM2_RC_INITIALIZE)
1033 if (rc == TPM2_RC_INITIALIZE) {
1034 rc = tpm2_startup(chip);
1038 rc = tpm2_do_selftest(chip);
1043 rc = tpm2_get_cc_attrs_tbl(chip);
1051 int tpm2_find_cc(struct tpm_chip *chip, u32 cc)
1055 for (i = 0; i < chip->nr_commands; i++)
1056 if (cc == (chip->cc_attrs_tbl[i] & GENMASK(15, 0)))