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,
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");
330 out = (struct tpm2_get_random_out *)
331 &buf.data[TPM_HEADER_SIZE];
332 recd = min_t(u32, be16_to_cpu(out->size), num_bytes);
333 if (tpm_buf_length(&buf) <
335 offsetof(struct tpm2_get_random_out, buffer) +
340 memcpy(dest_ptr, out->buffer, recd);
345 } while (retries-- && total < max);
347 tpm_buf_destroy(&buf);
348 return total ? total : -EIO;
350 tpm_buf_destroy(&buf);
355 * tpm2_flush_context() - execute a TPM2_FlushContext command
356 * @chip: TPM chip to use
357 * @handle: context handle
359 void tpm2_flush_context(struct tpm_chip *chip, u32 handle)
364 rc = tpm_buf_init(&buf, TPM2_ST_NO_SESSIONS, TPM2_CC_FLUSH_CONTEXT);
366 dev_warn(&chip->dev, "0x%08x was not flushed, out of memory\n",
371 tpm_buf_append_u32(&buf, handle);
373 tpm_transmit_cmd(chip, &buf, 0, "flushing context");
374 tpm_buf_destroy(&buf);
378 * tpm_buf_append_auth() - append TPMS_AUTH_COMMAND to the buffer.
380 * @buf: an allocated tpm_buf instance
381 * @session_handle: session handle
382 * @nonce: the session nonce, may be NULL if not used
383 * @nonce_len: the session nonce length, may be 0 if not used
384 * @attributes: the session attributes
385 * @hmac: the session HMAC or password, may be NULL if not used
386 * @hmac_len: the session HMAC or password length, maybe 0 if not used
388 static void tpm2_buf_append_auth(struct tpm_buf *buf, u32 session_handle,
389 const u8 *nonce, u16 nonce_len,
391 const u8 *hmac, u16 hmac_len)
393 tpm_buf_append_u32(buf, 9 + nonce_len + hmac_len);
394 tpm_buf_append_u32(buf, session_handle);
395 tpm_buf_append_u16(buf, nonce_len);
397 if (nonce && nonce_len)
398 tpm_buf_append(buf, nonce, nonce_len);
400 tpm_buf_append_u8(buf, attributes);
401 tpm_buf_append_u16(buf, hmac_len);
403 if (hmac && hmac_len)
404 tpm_buf_append(buf, hmac, hmac_len);
408 * tpm2_seal_trusted() - seal the payload of a trusted key
410 * @chip: TPM chip to use
411 * @payload: the key data in clear and encrypted form
412 * @options: authentication values and other options
414 * Return: < 0 on error and 0 on success.
416 int tpm2_seal_trusted(struct tpm_chip *chip,
417 struct trusted_key_payload *payload,
418 struct trusted_key_options *options)
420 unsigned int blob_len;
426 for (i = 0; i < ARRAY_SIZE(tpm2_hash_map); i++) {
427 if (options->hash == tpm2_hash_map[i].crypto_id) {
428 hash = tpm2_hash_map[i].tpm_id;
433 if (i == ARRAY_SIZE(tpm2_hash_map))
436 rc = tpm_buf_init(&buf, TPM2_ST_SESSIONS, TPM2_CC_CREATE);
440 tpm_buf_append_u32(&buf, options->keyhandle);
441 tpm2_buf_append_auth(&buf, TPM2_RS_PW,
443 0 /* session_attributes */,
444 options->keyauth /* hmac */,
448 tpm_buf_append_u16(&buf, 4 + TPM_DIGEST_SIZE + payload->key_len + 1);
450 tpm_buf_append_u16(&buf, TPM_DIGEST_SIZE);
451 tpm_buf_append(&buf, options->blobauth, TPM_DIGEST_SIZE);
452 tpm_buf_append_u16(&buf, payload->key_len + 1);
453 tpm_buf_append(&buf, payload->key, payload->key_len);
454 tpm_buf_append_u8(&buf, payload->migratable);
457 tpm_buf_append_u16(&buf, 14 + options->policydigest_len);
458 tpm_buf_append_u16(&buf, TPM_ALG_KEYEDHASH);
459 tpm_buf_append_u16(&buf, hash);
462 if (options->policydigest_len) {
463 tpm_buf_append_u32(&buf, 0);
464 tpm_buf_append_u16(&buf, options->policydigest_len);
465 tpm_buf_append(&buf, options->policydigest,
466 options->policydigest_len);
468 tpm_buf_append_u32(&buf, TPM2_OA_USER_WITH_AUTH);
469 tpm_buf_append_u16(&buf, 0);
472 /* public parameters */
473 tpm_buf_append_u16(&buf, TPM_ALG_NULL);
474 tpm_buf_append_u16(&buf, 0);
477 tpm_buf_append_u16(&buf, 0);
480 tpm_buf_append_u32(&buf, 0);
482 if (buf.flags & TPM_BUF_OVERFLOW) {
487 rc = tpm_transmit_cmd(chip, &buf, 4, "sealing data");
491 blob_len = be32_to_cpup((__be32 *) &buf.data[TPM_HEADER_SIZE]);
492 if (blob_len > MAX_BLOB_SIZE) {
496 if (tpm_buf_length(&buf) < TPM_HEADER_SIZE + 4 + blob_len) {
501 memcpy(payload->blob, &buf.data[TPM_HEADER_SIZE + 4], blob_len);
502 payload->blob_len = blob_len;
505 tpm_buf_destroy(&buf);
508 if (tpm2_rc_value(rc) == TPM2_RC_HASH)
518 * tpm2_load_cmd() - execute a TPM2_Load command
520 * @chip: TPM chip to use
521 * @payload: the key data in clear and encrypted form
522 * @options: authentication values and other options
523 * @blob_handle: returned blob handle
525 * Return: 0 on success.
526 * -E2BIG on wrong payload size.
527 * -EPERM on tpm error status.
528 * < 0 error from tpm_transmit_cmd.
530 static int tpm2_load_cmd(struct tpm_chip *chip,
531 struct trusted_key_payload *payload,
532 struct trusted_key_options *options,
536 unsigned int private_len;
537 unsigned int public_len;
538 unsigned int blob_len;
541 private_len = be16_to_cpup((__be16 *) &payload->blob[0]);
542 if (private_len > (payload->blob_len - 2))
545 public_len = be16_to_cpup((__be16 *) &payload->blob[2 + private_len]);
546 blob_len = private_len + public_len + 4;
547 if (blob_len > payload->blob_len)
550 rc = tpm_buf_init(&buf, TPM2_ST_SESSIONS, TPM2_CC_LOAD);
554 tpm_buf_append_u32(&buf, options->keyhandle);
555 tpm2_buf_append_auth(&buf, TPM2_RS_PW,
557 0 /* session_attributes */,
558 options->keyauth /* hmac */,
561 tpm_buf_append(&buf, payload->blob, blob_len);
563 if (buf.flags & TPM_BUF_OVERFLOW) {
568 rc = tpm_transmit_cmd(chip, &buf, 4, "loading blob");
570 *blob_handle = be32_to_cpup(
571 (__be32 *) &buf.data[TPM_HEADER_SIZE]);
574 tpm_buf_destroy(&buf);
583 * tpm2_unseal_cmd() - execute a TPM2_Unload command
585 * @chip: TPM chip to use
586 * @payload: the key data in clear and encrypted form
587 * @options: authentication values and other options
588 * @blob_handle: blob handle
590 * Return: 0 on success
591 * -EPERM on tpm error status
592 * < 0 error from tpm_transmit_cmd
594 static int tpm2_unseal_cmd(struct tpm_chip *chip,
595 struct trusted_key_payload *payload,
596 struct trusted_key_options *options,
604 rc = tpm_buf_init(&buf, TPM2_ST_SESSIONS, TPM2_CC_UNSEAL);
608 tpm_buf_append_u32(&buf, blob_handle);
609 tpm2_buf_append_auth(&buf,
610 options->policyhandle ?
611 options->policyhandle : TPM2_RS_PW,
613 TPM2_SA_CONTINUE_SESSION,
614 options->blobauth /* hmac */,
617 rc = tpm_transmit_cmd(chip, &buf, 6, "unsealing");
622 data_len = be16_to_cpup(
623 (__be16 *) &buf.data[TPM_HEADER_SIZE + 4]);
624 if (data_len < MIN_KEY_SIZE || data_len > MAX_KEY_SIZE + 1) {
629 if (tpm_buf_length(&buf) < TPM_HEADER_SIZE + 6 + data_len) {
633 data = &buf.data[TPM_HEADER_SIZE + 6];
635 memcpy(payload->key, data, data_len - 1);
636 payload->key_len = data_len - 1;
637 payload->migratable = data[data_len - 1];
641 tpm_buf_destroy(&buf);
646 * tpm2_unseal_trusted() - unseal the payload of a trusted key
648 * @chip: TPM chip to use
649 * @payload: the key data in clear and encrypted form
650 * @options: authentication values and other options
652 * Return: Same as with tpm_transmit_cmd.
654 int tpm2_unseal_trusted(struct tpm_chip *chip,
655 struct trusted_key_payload *payload,
656 struct trusted_key_options *options)
661 rc = tpm2_load_cmd(chip, payload, options, &blob_handle);
665 rc = tpm2_unseal_cmd(chip, payload, options, blob_handle);
666 tpm2_flush_context(chip, blob_handle);
670 struct tpm2_get_cap_out {
679 * tpm2_get_tpm_pt() - get value of a TPM_CAP_TPM_PROPERTIES type property
680 * @chip: a &tpm_chip instance
681 * @property_id: property ID.
682 * @value: output variable.
683 * @desc: passed to tpm_transmit_cmd()
687 * -errno or a TPM return code otherwise
689 ssize_t tpm2_get_tpm_pt(struct tpm_chip *chip, u32 property_id, u32 *value,
692 struct tpm2_get_cap_out *out;
696 rc = tpm_buf_init(&buf, TPM2_ST_NO_SESSIONS, TPM2_CC_GET_CAPABILITY);
699 tpm_buf_append_u32(&buf, TPM2_CAP_TPM_PROPERTIES);
700 tpm_buf_append_u32(&buf, property_id);
701 tpm_buf_append_u32(&buf, 1);
702 rc = tpm_transmit_cmd(chip, &buf, 0, NULL);
704 out = (struct tpm2_get_cap_out *)
705 &buf.data[TPM_HEADER_SIZE];
706 *value = be32_to_cpu(out->value);
708 tpm_buf_destroy(&buf);
711 EXPORT_SYMBOL_GPL(tpm2_get_tpm_pt);
714 * tpm2_shutdown() - send a TPM shutdown command
716 * Sends a TPM shutdown command. The shutdown command is used in call
717 * sites where the system is going down. If it fails, there is not much
718 * that can be done except print an error message.
720 * @chip: a &tpm_chip instance
721 * @shutdown_type: TPM_SU_CLEAR or TPM_SU_STATE.
723 void tpm2_shutdown(struct tpm_chip *chip, u16 shutdown_type)
728 rc = tpm_buf_init(&buf, TPM2_ST_NO_SESSIONS, TPM2_CC_SHUTDOWN);
731 tpm_buf_append_u16(&buf, shutdown_type);
732 tpm_transmit_cmd(chip, &buf, 0, "stopping the TPM");
733 tpm_buf_destroy(&buf);
737 * tpm2_do_selftest() - ensure that all self tests have passed
739 * @chip: TPM chip to use
741 * Return: Same as with tpm_transmit_cmd.
743 * The TPM can either run all self tests synchronously and then return
744 * RC_SUCCESS once all tests were successful. Or it can choose to run the tests
745 * asynchronously and return RC_TESTING immediately while the self tests still
746 * execute in the background. This function handles both cases and waits until
747 * all tests have completed.
749 static int tpm2_do_selftest(struct tpm_chip *chip)
755 for (full = 0; full < 2; full++) {
756 rc = tpm_buf_init(&buf, TPM2_ST_NO_SESSIONS, TPM2_CC_SELF_TEST);
760 tpm_buf_append_u8(&buf, full);
761 rc = tpm_transmit_cmd(chip, &buf, 0,
762 "attempting the self test");
763 tpm_buf_destroy(&buf);
765 if (rc == TPM2_RC_TESTING)
766 rc = TPM2_RC_SUCCESS;
767 if (rc == TPM2_RC_INITIALIZE || rc == TPM2_RC_SUCCESS)
775 * tpm2_probe() - probe for the TPM 2.0 protocol
776 * @chip: a &tpm_chip instance
778 * Send an idempotent TPM 2.0 command and see whether there is TPM2 chip in the
779 * other end based on the response tag. The flag TPM_CHIP_FLAG_TPM2 is set by
780 * this function if this is the case.
786 int tpm2_probe(struct tpm_chip *chip)
788 struct tpm_header *out;
792 rc = tpm_buf_init(&buf, TPM2_ST_NO_SESSIONS, TPM2_CC_GET_CAPABILITY);
795 tpm_buf_append_u32(&buf, TPM2_CAP_TPM_PROPERTIES);
796 tpm_buf_append_u32(&buf, TPM_PT_TOTAL_COMMANDS);
797 tpm_buf_append_u32(&buf, 1);
798 rc = tpm_transmit_cmd(chip, &buf, 0, NULL);
799 /* We ignore TPM return codes on purpose. */
801 out = (struct tpm_header *)buf.data;
802 if (be16_to_cpu(out->tag) == TPM2_ST_NO_SESSIONS)
803 chip->flags |= TPM_CHIP_FLAG_TPM2;
805 tpm_buf_destroy(&buf);
808 EXPORT_SYMBOL_GPL(tpm2_probe);
810 static int tpm2_init_bank_info(struct tpm_chip *chip, u32 bank_index)
812 struct tpm_bank_info *bank = chip->allocated_banks + bank_index;
813 struct tpm_digest digest = { .alg_id = bank->alg_id };
817 * Avoid unnecessary PCR read operations to reduce overhead
818 * and obtain identifiers of the crypto subsystem.
820 for (i = 0; i < ARRAY_SIZE(tpm2_hash_map); i++) {
821 enum hash_algo crypto_algo = tpm2_hash_map[i].crypto_id;
823 if (bank->alg_id != tpm2_hash_map[i].tpm_id)
826 bank->digest_size = hash_digest_size[crypto_algo];
827 bank->crypto_id = crypto_algo;
831 return tpm2_pcr_read(chip, 0, &digest, &bank->digest_size);
834 struct tpm2_pcr_selection {
840 static ssize_t tpm2_get_pcr_allocation(struct tpm_chip *chip)
842 struct tpm2_pcr_selection pcr_selection;
846 void *pcr_select_offset;
847 u32 sizeof_pcr_selection;
848 u32 nr_possible_banks;
849 u32 nr_alloc_banks = 0;
855 rc = tpm_buf_init(&buf, TPM2_ST_NO_SESSIONS, TPM2_CC_GET_CAPABILITY);
859 tpm_buf_append_u32(&buf, TPM2_CAP_PCRS);
860 tpm_buf_append_u32(&buf, 0);
861 tpm_buf_append_u32(&buf, 1);
863 rc = tpm_transmit_cmd(chip, &buf, 9, "get tpm pcr allocation");
867 nr_possible_banks = be32_to_cpup(
868 (__be32 *)&buf.data[TPM_HEADER_SIZE + 5]);
870 chip->allocated_banks = kcalloc(nr_possible_banks,
871 sizeof(*chip->allocated_banks),
873 if (!chip->allocated_banks) {
878 marker = &buf.data[TPM_HEADER_SIZE + 9];
880 rsp_len = be32_to_cpup((__be32 *)&buf.data[2]);
881 end = &buf.data[rsp_len];
883 for (i = 0; i < nr_possible_banks; i++) {
884 pcr_select_offset = marker +
885 offsetof(struct tpm2_pcr_selection, size_of_select);
886 if (pcr_select_offset >= end) {
891 memcpy(&pcr_selection, marker, sizeof(pcr_selection));
892 hash_alg = be16_to_cpu(pcr_selection.hash_alg);
894 pcr_select_offset = memchr_inv(pcr_selection.pcr_select, 0,
895 pcr_selection.size_of_select);
896 if (pcr_select_offset) {
897 chip->allocated_banks[nr_alloc_banks].alg_id = hash_alg;
899 rc = tpm2_init_bank_info(chip, nr_alloc_banks);
906 sizeof_pcr_selection = sizeof(pcr_selection.hash_alg) +
907 sizeof(pcr_selection.size_of_select) +
908 pcr_selection.size_of_select;
909 marker = marker + sizeof_pcr_selection;
912 chip->nr_allocated_banks = nr_alloc_banks;
914 tpm_buf_destroy(&buf);
919 static int tpm2_get_cc_attrs_tbl(struct tpm_chip *chip)
928 rc = tpm2_get_tpm_pt(chip, TPM_PT_TOTAL_COMMANDS, &nr_commands, NULL);
932 if (nr_commands > 0xFFFFF) {
937 chip->cc_attrs_tbl = devm_kcalloc(&chip->dev, 4, nr_commands,
940 rc = tpm_buf_init(&buf, TPM2_ST_NO_SESSIONS, TPM2_CC_GET_CAPABILITY);
944 tpm_buf_append_u32(&buf, TPM2_CAP_COMMANDS);
945 tpm_buf_append_u32(&buf, TPM2_CC_FIRST);
946 tpm_buf_append_u32(&buf, nr_commands);
948 rc = tpm_transmit_cmd(chip, &buf, 9 + 4 * nr_commands, NULL);
950 tpm_buf_destroy(&buf);
955 be32_to_cpup((__be32 *)&buf.data[TPM_HEADER_SIZE + 5])) {
956 tpm_buf_destroy(&buf);
960 chip->nr_commands = nr_commands;
962 attrs = (__be32 *)&buf.data[TPM_HEADER_SIZE + 9];
963 for (i = 0; i < nr_commands; i++, attrs++) {
964 chip->cc_attrs_tbl[i] = be32_to_cpup(attrs);
965 cc = chip->cc_attrs_tbl[i] & 0xFFFF;
967 if (cc == TPM2_CC_CONTEXT_SAVE || cc == TPM2_CC_FLUSH_CONTEXT) {
968 chip->cc_attrs_tbl[i] &=
969 ~(GENMASK(2, 0) << TPM2_CC_ATTR_CHANDLES);
970 chip->cc_attrs_tbl[i] |= 1 << TPM2_CC_ATTR_CHANDLES;
974 tpm_buf_destroy(&buf);
983 * tpm2_startup - turn on the TPM
984 * @chip: TPM chip to use
986 * Normally the firmware should start the TPM. This function is provided as a
987 * workaround if this does not happen. A legal case for this could be for
988 * example when a TPM emulator is used.
990 * Return: same as tpm_transmit_cmd()
993 static int tpm2_startup(struct tpm_chip *chip)
998 dev_info(&chip->dev, "starting up the TPM manually\n");
1000 rc = tpm_buf_init(&buf, TPM2_ST_NO_SESSIONS, TPM2_CC_STARTUP);
1004 tpm_buf_append_u16(&buf, TPM2_SU_CLEAR);
1005 rc = tpm_transmit_cmd(chip, &buf, 0, "attempting to start the TPM");
1006 tpm_buf_destroy(&buf);
1012 * tpm2_auto_startup - Perform the standard automatic TPM initialization
1014 * @chip: TPM chip to use
1016 * Returns 0 on success, < 0 in case of fatal error.
1018 int tpm2_auto_startup(struct tpm_chip *chip)
1022 rc = tpm2_get_timeouts(chip);
1026 rc = tpm2_do_selftest(chip);
1027 if (rc && rc != TPM2_RC_INITIALIZE)
1030 if (rc == TPM2_RC_INITIALIZE) {
1031 rc = tpm2_startup(chip);
1035 rc = tpm2_do_selftest(chip);
1040 rc = tpm2_get_pcr_allocation(chip);
1044 rc = tpm2_get_cc_attrs_tbl(chip);
1052 int tpm2_find_cc(struct tpm_chip *chip, u32 cc)
1056 for (i = 0; i < chip->nr_commands; i++)
1057 if (cc == (chip->cc_attrs_tbl[i] & GENMASK(15, 0)))