1b886d83cSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
27a1d7e6dSJarkko Sakkinen /*
3954650efSJarkko Sakkinen * Copyright (C) 2014, 2015 Intel Corporation
47a1d7e6dSJarkko Sakkinen *
57a1d7e6dSJarkko Sakkinen * Authors:
67a1d7e6dSJarkko Sakkinen * Jarkko Sakkinen <jarkko.sakkinen@linux.intel.com>
77a1d7e6dSJarkko Sakkinen *
87a1d7e6dSJarkko Sakkinen * Maintained by: <tpmdd-devel@lists.sourceforge.net>
97a1d7e6dSJarkko Sakkinen *
107a1d7e6dSJarkko Sakkinen * This file contains TPM2 protocol implementations of the commands
117a1d7e6dSJarkko Sakkinen * used by the kernel internally.
127a1d7e6dSJarkko Sakkinen */
137a1d7e6dSJarkko Sakkinen
147a1d7e6dSJarkko Sakkinen #include "tpm.h"
155ca4c20cSJarkko Sakkinen #include <crypto/hash_info.h>
165ca4c20cSJarkko Sakkinen
175ca4c20cSJarkko Sakkinen static struct tpm2_hash tpm2_hash_map[] = {
18aa042475SRoberto Sassu {HASH_ALGO_SHA1, TPM_ALG_SHA1},
19aa042475SRoberto Sassu {HASH_ALGO_SHA256, TPM_ALG_SHA256},
20aa042475SRoberto Sassu {HASH_ALGO_SHA384, TPM_ALG_SHA384},
21aa042475SRoberto Sassu {HASH_ALGO_SHA512, TPM_ALG_SHA512},
22aa042475SRoberto Sassu {HASH_ALGO_SM3_256, TPM_ALG_SM3_256},
235ca4c20cSJarkko Sakkinen };
245ca4c20cSJarkko Sakkinen
tpm2_get_timeouts(struct tpm_chip * chip)2570a3199aSTomas Winkler int tpm2_get_timeouts(struct tpm_chip *chip)
2670a3199aSTomas Winkler {
2770a3199aSTomas Winkler /* Fixed timeouts for TPM2 */
2870a3199aSTomas Winkler chip->timeout_a = msecs_to_jiffies(TPM2_TIMEOUT_A);
2970a3199aSTomas Winkler chip->timeout_b = msecs_to_jiffies(TPM2_TIMEOUT_B);
3070a3199aSTomas Winkler chip->timeout_c = msecs_to_jiffies(TPM2_TIMEOUT_C);
3170a3199aSTomas Winkler chip->timeout_d = msecs_to_jiffies(TPM2_TIMEOUT_D);
3270a3199aSTomas Winkler
3370a3199aSTomas Winkler /* PTP spec timeouts */
3470a3199aSTomas Winkler chip->duration[TPM_SHORT] = msecs_to_jiffies(TPM2_DURATION_SHORT);
3570a3199aSTomas Winkler chip->duration[TPM_MEDIUM] = msecs_to_jiffies(TPM2_DURATION_MEDIUM);
3670a3199aSTomas Winkler chip->duration[TPM_LONG] = msecs_to_jiffies(TPM2_DURATION_LONG);
3770a3199aSTomas Winkler
3870a3199aSTomas Winkler /* Key creation commands long timeouts */
3970a3199aSTomas Winkler chip->duration[TPM_LONG_LONG] =
4070a3199aSTomas Winkler msecs_to_jiffies(TPM2_DURATION_LONG_LONG);
4170a3199aSTomas Winkler
4270a3199aSTomas Winkler chip->flags |= TPM_CHIP_FLAG_HAVE_TIMEOUTS;
4370a3199aSTomas Winkler
4470a3199aSTomas Winkler return 0;
4570a3199aSTomas Winkler }
4670a3199aSTomas Winkler
47899102bcSTomas Winkler /**
48899102bcSTomas Winkler * tpm2_ordinal_duration_index() - returns an index to the chip duration table
49899102bcSTomas Winkler * @ordinal: TPM command ordinal.
50899102bcSTomas Winkler *
51899102bcSTomas Winkler * The function returns an index to the chip duration table
52899102bcSTomas Winkler * (enum tpm_duration), that describes the maximum amount of
53899102bcSTomas Winkler * time the chip could take to return the result for a particular ordinal.
54899102bcSTomas Winkler *
55899102bcSTomas Winkler * The values of the MEDIUM, and LONG durations are taken
56899102bcSTomas Winkler * from the PC Client Profile (PTP) specification (750, 2000 msec)
57899102bcSTomas Winkler *
58899102bcSTomas Winkler * LONG_LONG is for commands that generates keys which empirically takes
59899102bcSTomas Winkler * a longer time on some systems.
60899102bcSTomas Winkler *
61899102bcSTomas Winkler * Return:
62899102bcSTomas Winkler * * TPM_MEDIUM
63899102bcSTomas Winkler * * TPM_LONG
64899102bcSTomas Winkler * * TPM_LONG_LONG
65899102bcSTomas Winkler * * TPM_UNDEFINED
667a1d7e6dSJarkko Sakkinen */
tpm2_ordinal_duration_index(u32 ordinal)67899102bcSTomas Winkler static u8 tpm2_ordinal_duration_index(u32 ordinal)
68899102bcSTomas Winkler {
69899102bcSTomas Winkler switch (ordinal) {
70899102bcSTomas Winkler /* Startup */
71899102bcSTomas Winkler case TPM2_CC_STARTUP: /* 144 */
72899102bcSTomas Winkler return TPM_MEDIUM;
73899102bcSTomas Winkler
74899102bcSTomas Winkler case TPM2_CC_SELF_TEST: /* 143 */
75899102bcSTomas Winkler return TPM_LONG;
76899102bcSTomas Winkler
77899102bcSTomas Winkler case TPM2_CC_GET_RANDOM: /* 17B */
78899102bcSTomas Winkler return TPM_LONG;
79899102bcSTomas Winkler
80899102bcSTomas Winkler case TPM2_CC_SEQUENCE_UPDATE: /* 15C */
81899102bcSTomas Winkler return TPM_MEDIUM;
82899102bcSTomas Winkler case TPM2_CC_SEQUENCE_COMPLETE: /* 13E */
83899102bcSTomas Winkler return TPM_MEDIUM;
84899102bcSTomas Winkler case TPM2_CC_EVENT_SEQUENCE_COMPLETE: /* 185 */
85899102bcSTomas Winkler return TPM_MEDIUM;
86899102bcSTomas Winkler case TPM2_CC_HASH_SEQUENCE_START: /* 186 */
87899102bcSTomas Winkler return TPM_MEDIUM;
88899102bcSTomas Winkler
89899102bcSTomas Winkler case TPM2_CC_VERIFY_SIGNATURE: /* 177 */
905317677dSAmir Mizinski return TPM_LONG_LONG;
91899102bcSTomas Winkler
92899102bcSTomas Winkler case TPM2_CC_PCR_EXTEND: /* 182 */
93899102bcSTomas Winkler return TPM_MEDIUM;
94899102bcSTomas Winkler
95899102bcSTomas Winkler case TPM2_CC_HIERARCHY_CONTROL: /* 121 */
96899102bcSTomas Winkler return TPM_LONG;
97899102bcSTomas Winkler case TPM2_CC_HIERARCHY_CHANGE_AUTH: /* 129 */
98899102bcSTomas Winkler return TPM_LONG;
99899102bcSTomas Winkler
100899102bcSTomas Winkler case TPM2_CC_GET_CAPABILITY: /* 17A */
101899102bcSTomas Winkler return TPM_MEDIUM;
102899102bcSTomas Winkler
103899102bcSTomas Winkler case TPM2_CC_NV_READ: /* 14E */
104899102bcSTomas Winkler return TPM_LONG;
105899102bcSTomas Winkler
106899102bcSTomas Winkler case TPM2_CC_CREATE_PRIMARY: /* 131 */
107899102bcSTomas Winkler return TPM_LONG_LONG;
108899102bcSTomas Winkler case TPM2_CC_CREATE: /* 153 */
109899102bcSTomas Winkler return TPM_LONG_LONG;
110899102bcSTomas Winkler case TPM2_CC_CREATE_LOADED: /* 191 */
111899102bcSTomas Winkler return TPM_LONG_LONG;
112899102bcSTomas Winkler
113899102bcSTomas Winkler default:
114899102bcSTomas Winkler return TPM_UNDEFINED;
115899102bcSTomas Winkler }
116899102bcSTomas Winkler }
117899102bcSTomas Winkler
118899102bcSTomas Winkler /**
119899102bcSTomas Winkler * tpm2_calc_ordinal_duration() - calculate the maximum command duration
120899102bcSTomas Winkler * @chip: TPM chip to use.
121899102bcSTomas Winkler * @ordinal: TPM command ordinal.
122899102bcSTomas Winkler *
123899102bcSTomas Winkler * The function returns the maximum amount of time the chip could take
124899102bcSTomas Winkler * to return the result for a particular ordinal in jiffies.
125899102bcSTomas Winkler *
126899102bcSTomas Winkler * Return: A maximal duration time for an ordinal in jiffies.
127899102bcSTomas Winkler */
tpm2_calc_ordinal_duration(struct tpm_chip * chip,u32 ordinal)128899102bcSTomas Winkler unsigned long tpm2_calc_ordinal_duration(struct tpm_chip *chip, u32 ordinal)
129899102bcSTomas Winkler {
130899102bcSTomas Winkler unsigned int index;
131899102bcSTomas Winkler
132899102bcSTomas Winkler index = tpm2_ordinal_duration_index(ordinal);
133899102bcSTomas Winkler
134899102bcSTomas Winkler if (index != TPM_UNDEFINED)
135899102bcSTomas Winkler return chip->duration[index];
136899102bcSTomas Winkler else
137899102bcSTomas Winkler return msecs_to_jiffies(TPM2_DURATION_DEFAULT);
138899102bcSTomas Winkler }
139899102bcSTomas Winkler
1407a1d7e6dSJarkko Sakkinen
14191f7f3d7SRoberto Sassu struct tpm2_pcr_read_out {
14291f7f3d7SRoberto Sassu __be32 update_cnt;
14391f7f3d7SRoberto Sassu __be32 pcr_selects_cnt;
14491f7f3d7SRoberto Sassu __be16 hash_alg;
14591f7f3d7SRoberto Sassu u8 pcr_select_size;
14691f7f3d7SRoberto Sassu u8 pcr_select[TPM2_PCR_SELECT_MIN];
14791f7f3d7SRoberto Sassu __be32 digests_cnt;
14891f7f3d7SRoberto Sassu __be16 digest_size;
14991f7f3d7SRoberto Sassu u8 digest[];
15091f7f3d7SRoberto Sassu } __packed;
15191f7f3d7SRoberto Sassu
1527a1d7e6dSJarkko Sakkinen /**
1537a1d7e6dSJarkko Sakkinen * tpm2_pcr_read() - read a PCR value
1547a1d7e6dSJarkko Sakkinen * @chip: TPM chip to use.
1557a1d7e6dSJarkko Sakkinen * @pcr_idx: index of the PCR to read.
156879b5892SRoberto Sassu * @digest: PCR bank and buffer current PCR value is written to.
157879b5892SRoberto Sassu * @digest_size_ptr: pointer to variable that stores the digest size.
1587a1d7e6dSJarkko Sakkinen *
159794c6e10SWinkler, Tomas * Return: Same as with tpm_transmit_cmd.
1607a1d7e6dSJarkko Sakkinen */
tpm2_pcr_read(struct tpm_chip * chip,u32 pcr_idx,struct tpm_digest * digest,u16 * digest_size_ptr)161879b5892SRoberto Sassu int tpm2_pcr_read(struct tpm_chip *chip, u32 pcr_idx,
162879b5892SRoberto Sassu struct tpm_digest *digest, u16 *digest_size_ptr)
1637a1d7e6dSJarkko Sakkinen {
164879b5892SRoberto Sassu int i;
1657a1d7e6dSJarkko Sakkinen int rc;
16691f7f3d7SRoberto Sassu struct tpm_buf buf;
16791f7f3d7SRoberto Sassu struct tpm2_pcr_read_out *out;
16891f7f3d7SRoberto Sassu u8 pcr_select[TPM2_PCR_SELECT_MIN] = {0};
169879b5892SRoberto Sassu u16 digest_size;
170879b5892SRoberto Sassu u16 expected_digest_size = 0;
1717a1d7e6dSJarkko Sakkinen
1727a1d7e6dSJarkko Sakkinen if (pcr_idx >= TPM2_PLATFORM_PCR)
1737a1d7e6dSJarkko Sakkinen return -EINVAL;
1747a1d7e6dSJarkko Sakkinen
175879b5892SRoberto Sassu if (!digest_size_ptr) {
176879b5892SRoberto Sassu for (i = 0; i < chip->nr_allocated_banks &&
177879b5892SRoberto Sassu chip->allocated_banks[i].alg_id != digest->alg_id; i++)
178879b5892SRoberto Sassu ;
179879b5892SRoberto Sassu
180879b5892SRoberto Sassu if (i == chip->nr_allocated_banks)
181879b5892SRoberto Sassu return -EINVAL;
182879b5892SRoberto Sassu
183879b5892SRoberto Sassu expected_digest_size = chip->allocated_banks[i].digest_size;
184879b5892SRoberto Sassu }
185879b5892SRoberto Sassu
18691f7f3d7SRoberto Sassu rc = tpm_buf_init(&buf, TPM2_ST_NO_SESSIONS, TPM2_CC_PCR_READ);
18791f7f3d7SRoberto Sassu if (rc)
18891f7f3d7SRoberto Sassu return rc;
1897a1d7e6dSJarkko Sakkinen
19091f7f3d7SRoberto Sassu pcr_select[pcr_idx >> 3] = 1 << (pcr_idx & 0x7);
1917a1d7e6dSJarkko Sakkinen
19291f7f3d7SRoberto Sassu tpm_buf_append_u32(&buf, 1);
193879b5892SRoberto Sassu tpm_buf_append_u16(&buf, digest->alg_id);
19491f7f3d7SRoberto Sassu tpm_buf_append_u8(&buf, TPM2_PCR_SELECT_MIN);
19591f7f3d7SRoberto Sassu tpm_buf_append(&buf, (const unsigned char *)pcr_select,
19691f7f3d7SRoberto Sassu sizeof(pcr_select));
19791f7f3d7SRoberto Sassu
198879b5892SRoberto Sassu rc = tpm_transmit_cmd(chip, &buf, 0, "attempting to read a pcr value");
199879b5892SRoberto Sassu if (rc)
200879b5892SRoberto Sassu goto out;
201879b5892SRoberto Sassu
20291f7f3d7SRoberto Sassu out = (struct tpm2_pcr_read_out *)&buf.data[TPM_HEADER_SIZE];
203879b5892SRoberto Sassu digest_size = be16_to_cpu(out->digest_size);
204879b5892SRoberto Sassu if (digest_size > sizeof(digest->digest) ||
205879b5892SRoberto Sassu (!digest_size_ptr && digest_size != expected_digest_size)) {
206879b5892SRoberto Sassu rc = -EINVAL;
207879b5892SRoberto Sassu goto out;
2087a1d7e6dSJarkko Sakkinen }
2097a1d7e6dSJarkko Sakkinen
210879b5892SRoberto Sassu if (digest_size_ptr)
211879b5892SRoberto Sassu *digest_size_ptr = digest_size;
212879b5892SRoberto Sassu
213879b5892SRoberto Sassu memcpy(digest->digest, out->digest, digest_size);
214879b5892SRoberto Sassu out:
21591f7f3d7SRoberto Sassu tpm_buf_destroy(&buf);
2167a1d7e6dSJarkko Sakkinen return rc;
2177a1d7e6dSJarkko Sakkinen }
2187a1d7e6dSJarkko Sakkinen
219c1f92b4bSNayna Jain struct tpm2_null_auth_area {
220c1f92b4bSNayna Jain __be32 handle;
221c1f92b4bSNayna Jain __be16 nonce_size;
222c1f92b4bSNayna Jain u8 attributes;
223c1f92b4bSNayna Jain __be16 auth_size;
224c1f92b4bSNayna Jain } __packed;
2257a1d7e6dSJarkko Sakkinen
2267a1d7e6dSJarkko Sakkinen /**
2277a1d7e6dSJarkko Sakkinen * tpm2_pcr_extend() - extend a PCR value
228794c6e10SWinkler, Tomas *
2297a1d7e6dSJarkko Sakkinen * @chip: TPM chip to use.
2307a1d7e6dSJarkko Sakkinen * @pcr_idx: index of the PCR.
231c1f92b4bSNayna Jain * @digests: list of pcr banks and corresponding digest values to extend.
2327a1d7e6dSJarkko Sakkinen *
233794c6e10SWinkler, Tomas * Return: Same as with tpm_transmit_cmd.
2347a1d7e6dSJarkko Sakkinen */
tpm2_pcr_extend(struct tpm_chip * chip,u32 pcr_idx,struct tpm_digest * digests)2350b6cf6b9SRoberto Sassu int tpm2_pcr_extend(struct tpm_chip *chip, u32 pcr_idx,
236aa042475SRoberto Sassu struct tpm_digest *digests)
2377a1d7e6dSJarkko Sakkinen {
238c1f92b4bSNayna Jain struct tpm_buf buf;
239c1f92b4bSNayna Jain struct tpm2_null_auth_area auth_area;
2407a1d7e6dSJarkko Sakkinen int rc;
241c1f92b4bSNayna Jain int i;
2427a1d7e6dSJarkko Sakkinen
243c1f92b4bSNayna Jain rc = tpm_buf_init(&buf, TPM2_ST_SESSIONS, TPM2_CC_PCR_EXTEND);
244c1f92b4bSNayna Jain if (rc)
245c1f92b4bSNayna Jain return rc;
246c1f92b4bSNayna Jain
247c1f92b4bSNayna Jain tpm_buf_append_u32(&buf, pcr_idx);
248c1f92b4bSNayna Jain
249c1f92b4bSNayna Jain auth_area.handle = cpu_to_be32(TPM2_RS_PW);
250c1f92b4bSNayna Jain auth_area.nonce_size = 0;
251c1f92b4bSNayna Jain auth_area.attributes = 0;
252c1f92b4bSNayna Jain auth_area.auth_size = 0;
253c1f92b4bSNayna Jain
254c1f92b4bSNayna Jain tpm_buf_append_u32(&buf, sizeof(struct tpm2_null_auth_area));
255c1f92b4bSNayna Jain tpm_buf_append(&buf, (const unsigned char *)&auth_area,
256c1f92b4bSNayna Jain sizeof(auth_area));
2570b6cf6b9SRoberto Sassu tpm_buf_append_u32(&buf, chip->nr_allocated_banks);
258c1f92b4bSNayna Jain
2590b6cf6b9SRoberto Sassu for (i = 0; i < chip->nr_allocated_banks; i++) {
260c1f92b4bSNayna Jain tpm_buf_append_u16(&buf, digests[i].alg_id);
261879b5892SRoberto Sassu tpm_buf_append(&buf, (const unsigned char *)&digests[i].digest,
262879b5892SRoberto Sassu chip->allocated_banks[i].digest_size);
263c1f92b4bSNayna Jain }
264c1f92b4bSNayna Jain
26547a6c28bSJarkko Sakkinen rc = tpm_transmit_cmd(chip, &buf, 0, "attempting extend a PCR value");
2667a1d7e6dSJarkko Sakkinen
267c1f92b4bSNayna Jain tpm_buf_destroy(&buf);
268c1f92b4bSNayna Jain
2697a1d7e6dSJarkko Sakkinen return rc;
2707a1d7e6dSJarkko Sakkinen }
2717a1d7e6dSJarkko Sakkinen
272ce63c05bSJarkko Sakkinen struct tpm2_get_random_out {
273ce63c05bSJarkko Sakkinen __be16 size;
274ce63c05bSJarkko Sakkinen u8 buffer[TPM_MAX_RNG_DATA];
275ce63c05bSJarkko Sakkinen } __packed;
2767a1d7e6dSJarkko Sakkinen
2777a1d7e6dSJarkko Sakkinen /**
2787a1d7e6dSJarkko Sakkinen * tpm2_get_random() - get random bytes from the TPM RNG
279794c6e10SWinkler, Tomas *
280ce63c05bSJarkko Sakkinen * @chip: a &tpm_chip instance
281ce63c05bSJarkko Sakkinen * @dest: destination buffer
282ce63c05bSJarkko Sakkinen * @max: the max number of random bytes to pull
2837a1d7e6dSJarkko Sakkinen *
284794c6e10SWinkler, Tomas * Return:
285ce63c05bSJarkko Sakkinen * size of the buffer on success,
286782779b6SKees Cook * -errno otherwise (positive TPM return codes are masked to -EIO)
2877a1d7e6dSJarkko Sakkinen */
tpm2_get_random(struct tpm_chip * chip,u8 * dest,size_t max)288ce63c05bSJarkko Sakkinen int tpm2_get_random(struct tpm_chip *chip, u8 *dest, size_t max)
2897a1d7e6dSJarkko Sakkinen {
290ce63c05bSJarkko Sakkinen struct tpm2_get_random_out *out;
291ce63c05bSJarkko Sakkinen struct tpm_buf buf;
292ce63c05bSJarkko Sakkinen u32 recd;
293ce63c05bSJarkko Sakkinen u32 num_bytes = max;
2947a1d7e6dSJarkko Sakkinen int err;
2957a1d7e6dSJarkko Sakkinen int total = 0;
2967a1d7e6dSJarkko Sakkinen int retries = 5;
297ce63c05bSJarkko Sakkinen u8 *dest_ptr = dest;
2987a1d7e6dSJarkko Sakkinen
299ce63c05bSJarkko Sakkinen if (!num_bytes || max > TPM_MAX_RNG_DATA)
3007a1d7e6dSJarkko Sakkinen return -EINVAL;
3017a1d7e6dSJarkko Sakkinen
302ce63c05bSJarkko Sakkinen err = tpm_buf_init(&buf, 0, 0);
303ce63c05bSJarkko Sakkinen if (err)
304ce63c05bSJarkko Sakkinen return err;
3057a1d7e6dSJarkko Sakkinen
306ce63c05bSJarkko Sakkinen do {
307ce63c05bSJarkko Sakkinen tpm_buf_reset(&buf, TPM2_ST_NO_SESSIONS, TPM2_CC_GET_RANDOM);
308ce63c05bSJarkko Sakkinen tpm_buf_append_u16(&buf, num_bytes);
3095faafbabSJarkko Sakkinen err = tpm_transmit_cmd(chip, &buf,
310c659af78SStefan Berger offsetof(struct tpm2_get_random_out,
311c659af78SStefan Berger buffer),
31247a6c28bSJarkko Sakkinen "attempting get random");
313782779b6SKees Cook if (err) {
314782779b6SKees Cook if (err > 0)
315782779b6SKees Cook err = -EIO;
316ce63c05bSJarkko Sakkinen goto out;
317782779b6SKees Cook }
3187a1d7e6dSJarkko Sakkinen
319ce63c05bSJarkko Sakkinen out = (struct tpm2_get_random_out *)
320ce63c05bSJarkko Sakkinen &buf.data[TPM_HEADER_SIZE];
321ce63c05bSJarkko Sakkinen recd = min_t(u32, be16_to_cpu(out->size), num_bytes);
322ce63c05bSJarkko Sakkinen if (tpm_buf_length(&buf) <
32384b59f64SJarkko Sakkinen TPM_HEADER_SIZE +
32484b59f64SJarkko Sakkinen offsetof(struct tpm2_get_random_out, buffer) +
32584b59f64SJarkko Sakkinen recd) {
326ce63c05bSJarkko Sakkinen err = -EFAULT;
327ce63c05bSJarkko Sakkinen goto out;
328ce63c05bSJarkko Sakkinen }
329ce63c05bSJarkko Sakkinen memcpy(dest_ptr, out->buffer, recd);
3307a1d7e6dSJarkko Sakkinen
331ce63c05bSJarkko Sakkinen dest_ptr += recd;
3327a1d7e6dSJarkko Sakkinen total += recd;
3337a1d7e6dSJarkko Sakkinen num_bytes -= recd;
3347a1d7e6dSJarkko Sakkinen } while (retries-- && total < max);
3357a1d7e6dSJarkko Sakkinen
336ce63c05bSJarkko Sakkinen tpm_buf_destroy(&buf);
3377a1d7e6dSJarkko Sakkinen return total ? total : -EIO;
338ce63c05bSJarkko Sakkinen out:
339ce63c05bSJarkko Sakkinen tpm_buf_destroy(&buf);
340ce63c05bSJarkko Sakkinen return err;
3417a1d7e6dSJarkko Sakkinen }
3427a1d7e6dSJarkko Sakkinen
3437a1d7e6dSJarkko Sakkinen /**
34447a6c28bSJarkko Sakkinen * tpm2_flush_context() - execute a TPM2_FlushContext command
3459aa36b39SJarkko Sakkinen * @chip: TPM chip to use
3465122b5f0STomas Winkler * @handle: context handle
3479aa36b39SJarkko Sakkinen */
tpm2_flush_context(struct tpm_chip * chip,u32 handle)34847a6c28bSJarkko Sakkinen void tpm2_flush_context(struct tpm_chip *chip, u32 handle)
3499aa36b39SJarkko Sakkinen {
3509aa36b39SJarkko Sakkinen struct tpm_buf buf;
3519aa36b39SJarkko Sakkinen int rc;
3529aa36b39SJarkko Sakkinen
3539aa36b39SJarkko Sakkinen rc = tpm_buf_init(&buf, TPM2_ST_NO_SESSIONS, TPM2_CC_FLUSH_CONTEXT);
3549aa36b39SJarkko Sakkinen if (rc) {
3559aa36b39SJarkko Sakkinen dev_warn(&chip->dev, "0x%08x was not flushed, out of memory\n",
3569aa36b39SJarkko Sakkinen handle);
3579aa36b39SJarkko Sakkinen return;
3589aa36b39SJarkko Sakkinen }
3599aa36b39SJarkko Sakkinen
3609aa36b39SJarkko Sakkinen tpm_buf_append_u32(&buf, handle);
3619aa36b39SJarkko Sakkinen
36247a6c28bSJarkko Sakkinen tpm_transmit_cmd(chip, &buf, 0, "flushing context");
3639aa36b39SJarkko Sakkinen tpm_buf_destroy(&buf);
3649aa36b39SJarkko Sakkinen }
36545477b3fSJames Bottomley EXPORT_SYMBOL_GPL(tpm2_flush_context);
3669aa36b39SJarkko Sakkinen
3672ab32411SJarkko Sakkinen struct tpm2_get_cap_out {
3682ab32411SJarkko Sakkinen u8 more_data;
3692ab32411SJarkko Sakkinen __be32 subcap_id;
3702ab32411SJarkko Sakkinen __be32 property_cnt;
3712ab32411SJarkko Sakkinen __be32 property_id;
3722ab32411SJarkko Sakkinen __be32 value;
3732ab32411SJarkko Sakkinen } __packed;
3742ab32411SJarkko Sakkinen
375954650efSJarkko Sakkinen /**
3767a1d7e6dSJarkko Sakkinen * tpm2_get_tpm_pt() - get value of a TPM_CAP_TPM_PROPERTIES type property
3772ab32411SJarkko Sakkinen * @chip: a &tpm_chip instance
3787a1d7e6dSJarkko Sakkinen * @property_id: property ID.
3797a1d7e6dSJarkko Sakkinen * @value: output variable.
3807a1d7e6dSJarkko Sakkinen * @desc: passed to tpm_transmit_cmd()
3817a1d7e6dSJarkko Sakkinen *
3822ab32411SJarkko Sakkinen * Return:
3832ab32411SJarkko Sakkinen * 0 on success,
3842ab32411SJarkko Sakkinen * -errno or a TPM return code otherwise
3857a1d7e6dSJarkko Sakkinen */
tpm2_get_tpm_pt(struct tpm_chip * chip,u32 property_id,u32 * value,const char * desc)3867a1d7e6dSJarkko Sakkinen ssize_t tpm2_get_tpm_pt(struct tpm_chip *chip, u32 property_id, u32 *value,
3877a1d7e6dSJarkko Sakkinen const char *desc)
3887a1d7e6dSJarkko Sakkinen {
3892ab32411SJarkko Sakkinen struct tpm2_get_cap_out *out;
3902ab32411SJarkko Sakkinen struct tpm_buf buf;
3917a1d7e6dSJarkko Sakkinen int rc;
3927a1d7e6dSJarkko Sakkinen
3932ab32411SJarkko Sakkinen rc = tpm_buf_init(&buf, TPM2_ST_NO_SESSIONS, TPM2_CC_GET_CAPABILITY);
3942ab32411SJarkko Sakkinen if (rc)
3952ab32411SJarkko Sakkinen return rc;
3962ab32411SJarkko Sakkinen tpm_buf_append_u32(&buf, TPM2_CAP_TPM_PROPERTIES);
3972ab32411SJarkko Sakkinen tpm_buf_append_u32(&buf, property_id);
3982ab32411SJarkko Sakkinen tpm_buf_append_u32(&buf, 1);
39947a6c28bSJarkko Sakkinen rc = tpm_transmit_cmd(chip, &buf, 0, NULL);
4002ab32411SJarkko Sakkinen if (!rc) {
4012ab32411SJarkko Sakkinen out = (struct tpm2_get_cap_out *)
4022ab32411SJarkko Sakkinen &buf.data[TPM_HEADER_SIZE];
403e57b2523SStefan Mahnke-Hartmann /*
404e57b2523SStefan Mahnke-Hartmann * To prevent failing boot up of some systems, Infineon TPM2.0
405e57b2523SStefan Mahnke-Hartmann * returns SUCCESS on TPM2_Startup in field upgrade mode. Also
406e57b2523SStefan Mahnke-Hartmann * the TPM2_Getcapability command returns a zero length list
407e57b2523SStefan Mahnke-Hartmann * in field upgrade mode.
408e57b2523SStefan Mahnke-Hartmann */
409e57b2523SStefan Mahnke-Hartmann if (be32_to_cpu(out->property_cnt) > 0)
4102ab32411SJarkko Sakkinen *value = be32_to_cpu(out->value);
411e57b2523SStefan Mahnke-Hartmann else
412e57b2523SStefan Mahnke-Hartmann rc = -ENODATA;
4132ab32411SJarkko Sakkinen }
4142ab32411SJarkko Sakkinen tpm_buf_destroy(&buf);
4157a1d7e6dSJarkko Sakkinen return rc;
4167a1d7e6dSJarkko Sakkinen }
417eb5854e7SJarkko Sakkinen EXPORT_SYMBOL_GPL(tpm2_get_tpm_pt);
4187a1d7e6dSJarkko Sakkinen
4197a1d7e6dSJarkko Sakkinen /**
420cc26c6efSJarkko Sakkinen * tpm2_shutdown() - send a TPM shutdown command
421794c6e10SWinkler, Tomas *
422cc26c6efSJarkko Sakkinen * Sends a TPM shutdown command. The shutdown command is used in call
423cc26c6efSJarkko Sakkinen * sites where the system is going down. If it fails, there is not much
424cc26c6efSJarkko Sakkinen * that can be done except print an error message.
425cc26c6efSJarkko Sakkinen *
426cc26c6efSJarkko Sakkinen * @chip: a &tpm_chip instance
427cc26c6efSJarkko Sakkinen * @shutdown_type: TPM_SU_CLEAR or TPM_SU_STATE.
4287a1d7e6dSJarkko Sakkinen */
tpm2_shutdown(struct tpm_chip * chip,u16 shutdown_type)42974d6b3ceSJarkko Sakkinen void tpm2_shutdown(struct tpm_chip *chip, u16 shutdown_type)
4307a1d7e6dSJarkko Sakkinen {
431cc26c6efSJarkko Sakkinen struct tpm_buf buf;
43274d6b3ceSJarkko Sakkinen int rc;
4337a1d7e6dSJarkko Sakkinen
434cc26c6efSJarkko Sakkinen rc = tpm_buf_init(&buf, TPM2_ST_NO_SESSIONS, TPM2_CC_SHUTDOWN);
435cc26c6efSJarkko Sakkinen if (rc)
436cc26c6efSJarkko Sakkinen return;
437cc26c6efSJarkko Sakkinen tpm_buf_append_u16(&buf, shutdown_type);
43847a6c28bSJarkko Sakkinen tpm_transmit_cmd(chip, &buf, 0, "stopping the TPM");
439cc26c6efSJarkko Sakkinen tpm_buf_destroy(&buf);
4407a1d7e6dSJarkko Sakkinen }
4417a1d7e6dSJarkko Sakkinen
4427a1d7e6dSJarkko Sakkinen /**
4432482b1bbSAlexander Steffen * tpm2_do_selftest() - ensure that all self tests have passed
444794c6e10SWinkler, Tomas *
4457a1d7e6dSJarkko Sakkinen * @chip: TPM chip to use
4467a1d7e6dSJarkko Sakkinen *
447794c6e10SWinkler, Tomas * Return: Same as with tpm_transmit_cmd.
448794c6e10SWinkler, Tomas *
449125a2210SAlexander Steffen * The TPM can either run all self tests synchronously and then return
450125a2210SAlexander Steffen * RC_SUCCESS once all tests were successful. Or it can choose to run the tests
451125a2210SAlexander Steffen * asynchronously and return RC_TESTING immediately while the self tests still
452125a2210SAlexander Steffen * execute in the background. This function handles both cases and waits until
453125a2210SAlexander Steffen * all tests have completed.
4547a1d7e6dSJarkko Sakkinen */
tpm2_do_selftest(struct tpm_chip * chip)455cae8b441SJason Gunthorpe static int tpm2_do_selftest(struct tpm_chip *chip)
4567a1d7e6dSJarkko Sakkinen {
4572be8ffedSJames Bottomley struct tpm_buf buf;
4582be8ffedSJames Bottomley int full;
4597a1d7e6dSJarkko Sakkinen int rc;
4607a1d7e6dSJarkko Sakkinen
4612be8ffedSJames Bottomley for (full = 0; full < 2; full++) {
4622be8ffedSJames Bottomley rc = tpm_buf_init(&buf, TPM2_ST_NO_SESSIONS, TPM2_CC_SELF_TEST);
4632be8ffedSJames Bottomley if (rc)
4642be8ffedSJames Bottomley return rc;
4657a1d7e6dSJarkko Sakkinen
4662be8ffedSJames Bottomley tpm_buf_append_u8(&buf, full);
46747a6c28bSJarkko Sakkinen rc = tpm_transmit_cmd(chip, &buf, 0,
4682be8ffedSJames Bottomley "attempting the self test");
4692be8ffedSJames Bottomley tpm_buf_destroy(&buf);
470125a2210SAlexander Steffen
4712be8ffedSJames Bottomley if (rc == TPM2_RC_TESTING)
4722be8ffedSJames Bottomley rc = TPM2_RC_SUCCESS;
4732be8ffedSJames Bottomley if (rc == TPM2_RC_INITIALIZE || rc == TPM2_RC_SUCCESS)
4742be8ffedSJames Bottomley return rc;
4757a1d7e6dSJarkko Sakkinen }
4767a1d7e6dSJarkko Sakkinen
4777a1d7e6dSJarkko Sakkinen return rc;
4787a1d7e6dSJarkko Sakkinen }
4797a1d7e6dSJarkko Sakkinen
4807a1d7e6dSJarkko Sakkinen /**
48194e266baSJarkko Sakkinen * tpm2_probe() - probe for the TPM 2.0 protocol
48294e266baSJarkko Sakkinen * @chip: a &tpm_chip instance
4834d5f2051SJarkko Sakkinen *
48494e266baSJarkko Sakkinen * Send an idempotent TPM 2.0 command and see whether there is TPM2 chip in the
48594e266baSJarkko Sakkinen * other end based on the response tag. The flag TPM_CHIP_FLAG_TPM2 is set by
48694e266baSJarkko Sakkinen * this function if this is the case.
487794c6e10SWinkler, Tomas *
48894e266baSJarkko Sakkinen * Return:
48994e266baSJarkko Sakkinen * 0 on success,
49094e266baSJarkko Sakkinen * -errno otherwise
4914d5f2051SJarkko Sakkinen */
tpm2_probe(struct tpm_chip * chip)4924d5f2051SJarkko Sakkinen int tpm2_probe(struct tpm_chip *chip)
4934d5f2051SJarkko Sakkinen {
494b34b77a9SJarkko Sakkinen struct tpm_header *out;
49594e266baSJarkko Sakkinen struct tpm_buf buf;
4964d5f2051SJarkko Sakkinen int rc;
4974d5f2051SJarkko Sakkinen
49894e266baSJarkko Sakkinen rc = tpm_buf_init(&buf, TPM2_ST_NO_SESSIONS, TPM2_CC_GET_CAPABILITY);
49994e266baSJarkko Sakkinen if (rc)
5004d5f2051SJarkko Sakkinen return rc;
50194e266baSJarkko Sakkinen tpm_buf_append_u32(&buf, TPM2_CAP_TPM_PROPERTIES);
50294e266baSJarkko Sakkinen tpm_buf_append_u32(&buf, TPM_PT_TOTAL_COMMANDS);
50394e266baSJarkko Sakkinen tpm_buf_append_u32(&buf, 1);
50447a6c28bSJarkko Sakkinen rc = tpm_transmit_cmd(chip, &buf, 0, NULL);
50594e266baSJarkko Sakkinen /* We ignore TPM return codes on purpose. */
50694e266baSJarkko Sakkinen if (rc >= 0) {
507b34b77a9SJarkko Sakkinen out = (struct tpm_header *)buf.data;
50894e266baSJarkko Sakkinen if (be16_to_cpu(out->tag) == TPM2_ST_NO_SESSIONS)
5094d5f2051SJarkko Sakkinen chip->flags |= TPM_CHIP_FLAG_TPM2;
51094e266baSJarkko Sakkinen }
51194e266baSJarkko Sakkinen tpm_buf_destroy(&buf);
5124d5f2051SJarkko Sakkinen return 0;
5134d5f2051SJarkko Sakkinen }
5144d5f2051SJarkko Sakkinen EXPORT_SYMBOL_GPL(tpm2_probe);
515cae8b441SJason Gunthorpe
tpm2_init_bank_info(struct tpm_chip * chip,u32 bank_index)516879b5892SRoberto Sassu static int tpm2_init_bank_info(struct tpm_chip *chip, u32 bank_index)
517879b5892SRoberto Sassu {
518879b5892SRoberto Sassu struct tpm_bank_info *bank = chip->allocated_banks + bank_index;
519879b5892SRoberto Sassu struct tpm_digest digest = { .alg_id = bank->alg_id };
520879b5892SRoberto Sassu int i;
521879b5892SRoberto Sassu
522879b5892SRoberto Sassu /*
523879b5892SRoberto Sassu * Avoid unnecessary PCR read operations to reduce overhead
524879b5892SRoberto Sassu * and obtain identifiers of the crypto subsystem.
525879b5892SRoberto Sassu */
526879b5892SRoberto Sassu for (i = 0; i < ARRAY_SIZE(tpm2_hash_map); i++) {
527879b5892SRoberto Sassu enum hash_algo crypto_algo = tpm2_hash_map[i].crypto_id;
528879b5892SRoberto Sassu
529879b5892SRoberto Sassu if (bank->alg_id != tpm2_hash_map[i].tpm_id)
530879b5892SRoberto Sassu continue;
531879b5892SRoberto Sassu
532879b5892SRoberto Sassu bank->digest_size = hash_digest_size[crypto_algo];
533879b5892SRoberto Sassu bank->crypto_id = crypto_algo;
534879b5892SRoberto Sassu return 0;
535879b5892SRoberto Sassu }
536879b5892SRoberto Sassu
537dc10e418SRoberto Sassu bank->crypto_id = HASH_ALGO__LAST;
538dc10e418SRoberto Sassu
539879b5892SRoberto Sassu return tpm2_pcr_read(chip, 0, &digest, &bank->digest_size);
540879b5892SRoberto Sassu }
541879b5892SRoberto Sassu
5421db15344SNayna Jain struct tpm2_pcr_selection {
5431db15344SNayna Jain __be16 hash_alg;
5441db15344SNayna Jain u8 size_of_select;
5451db15344SNayna Jain u8 pcr_select[3];
5461db15344SNayna Jain } __packed;
5471db15344SNayna Jain
tpm2_get_pcr_allocation(struct tpm_chip * chip)548fa4f99c0SNayna Jain ssize_t tpm2_get_pcr_allocation(struct tpm_chip *chip)
5491db15344SNayna Jain {
5501db15344SNayna Jain struct tpm2_pcr_selection pcr_selection;
5511db15344SNayna Jain struct tpm_buf buf;
5521db15344SNayna Jain void *marker;
5531db15344SNayna Jain void *end;
5541db15344SNayna Jain void *pcr_select_offset;
5551db15344SNayna Jain u32 sizeof_pcr_selection;
556bcfff838SRoberto Sassu u32 nr_possible_banks;
557bcfff838SRoberto Sassu u32 nr_alloc_banks = 0;
558bcfff838SRoberto Sassu u16 hash_alg;
5591db15344SNayna Jain u32 rsp_len;
5601db15344SNayna Jain int rc;
5611db15344SNayna Jain int i = 0;
5621db15344SNayna Jain
5631db15344SNayna Jain rc = tpm_buf_init(&buf, TPM2_ST_NO_SESSIONS, TPM2_CC_GET_CAPABILITY);
5641db15344SNayna Jain if (rc)
5651db15344SNayna Jain return rc;
5661db15344SNayna Jain
5671db15344SNayna Jain tpm_buf_append_u32(&buf, TPM2_CAP_PCRS);
5681db15344SNayna Jain tpm_buf_append_u32(&buf, 0);
5691db15344SNayna Jain tpm_buf_append_u32(&buf, 1);
5701db15344SNayna Jain
57147a6c28bSJarkko Sakkinen rc = tpm_transmit_cmd(chip, &buf, 9, "get tpm pcr allocation");
5721db15344SNayna Jain if (rc)
5731db15344SNayna Jain goto out;
5741db15344SNayna Jain
575bcfff838SRoberto Sassu nr_possible_banks = be32_to_cpup(
5761db15344SNayna Jain (__be32 *)&buf.data[TPM_HEADER_SIZE + 5]);
5771db15344SNayna Jain
578bcfff838SRoberto Sassu chip->allocated_banks = kcalloc(nr_possible_banks,
579bcfff838SRoberto Sassu sizeof(*chip->allocated_banks),
580bcfff838SRoberto Sassu GFP_KERNEL);
581bcfff838SRoberto Sassu if (!chip->allocated_banks) {
582bcfff838SRoberto Sassu rc = -ENOMEM;
5831db15344SNayna Jain goto out;
5841db15344SNayna Jain }
5851db15344SNayna Jain
5861db15344SNayna Jain marker = &buf.data[TPM_HEADER_SIZE + 9];
5871db15344SNayna Jain
5881db15344SNayna Jain rsp_len = be32_to_cpup((__be32 *)&buf.data[2]);
5891db15344SNayna Jain end = &buf.data[rsp_len];
5901db15344SNayna Jain
591bcfff838SRoberto Sassu for (i = 0; i < nr_possible_banks; i++) {
5921db15344SNayna Jain pcr_select_offset = marker +
5931db15344SNayna Jain offsetof(struct tpm2_pcr_selection, size_of_select);
5941db15344SNayna Jain if (pcr_select_offset >= end) {
5951db15344SNayna Jain rc = -EFAULT;
5961db15344SNayna Jain break;
5971db15344SNayna Jain }
5981db15344SNayna Jain
5991db15344SNayna Jain memcpy(&pcr_selection, marker, sizeof(pcr_selection));
600bcfff838SRoberto Sassu hash_alg = be16_to_cpu(pcr_selection.hash_alg);
601bcfff838SRoberto Sassu
602bcfff838SRoberto Sassu pcr_select_offset = memchr_inv(pcr_selection.pcr_select, 0,
603bcfff838SRoberto Sassu pcr_selection.size_of_select);
604bcfff838SRoberto Sassu if (pcr_select_offset) {
605879b5892SRoberto Sassu chip->allocated_banks[nr_alloc_banks].alg_id = hash_alg;
606879b5892SRoberto Sassu
607879b5892SRoberto Sassu rc = tpm2_init_bank_info(chip, nr_alloc_banks);
608879b5892SRoberto Sassu if (rc < 0)
609879b5892SRoberto Sassu break;
610879b5892SRoberto Sassu
611bcfff838SRoberto Sassu nr_alloc_banks++;
612bcfff838SRoberto Sassu }
613bcfff838SRoberto Sassu
6141db15344SNayna Jain sizeof_pcr_selection = sizeof(pcr_selection.hash_alg) +
6151db15344SNayna Jain sizeof(pcr_selection.size_of_select) +
6161db15344SNayna Jain pcr_selection.size_of_select;
6171db15344SNayna Jain marker = marker + sizeof_pcr_selection;
6181db15344SNayna Jain }
6191db15344SNayna Jain
620bcfff838SRoberto Sassu chip->nr_allocated_banks = nr_alloc_banks;
6211db15344SNayna Jain out:
6221db15344SNayna Jain tpm_buf_destroy(&buf);
6231db15344SNayna Jain
6241db15344SNayna Jain return rc;
6251db15344SNayna Jain }
62661841be6SJarkko Sakkinen
tpm2_get_cc_attrs_tbl(struct tpm_chip * chip)62718b3670dSStefan Berger int tpm2_get_cc_attrs_tbl(struct tpm_chip *chip)
62858472f5cSJarkko Sakkinen {
62958472f5cSJarkko Sakkinen struct tpm_buf buf;
63058472f5cSJarkko Sakkinen u32 nr_commands;
631171360d7SJarkko Sakkinen __be32 *attrs;
63258472f5cSJarkko Sakkinen u32 cc;
63358472f5cSJarkko Sakkinen int i;
63458472f5cSJarkko Sakkinen int rc;
63558472f5cSJarkko Sakkinen
63658472f5cSJarkko Sakkinen rc = tpm2_get_tpm_pt(chip, TPM_PT_TOTAL_COMMANDS, &nr_commands, NULL);
63758472f5cSJarkko Sakkinen if (rc)
63858472f5cSJarkko Sakkinen goto out;
63958472f5cSJarkko Sakkinen
64058472f5cSJarkko Sakkinen if (nr_commands > 0xFFFFF) {
64158472f5cSJarkko Sakkinen rc = -EFAULT;
64258472f5cSJarkko Sakkinen goto out;
64358472f5cSJarkko Sakkinen }
64458472f5cSJarkko Sakkinen
645a86854d0SKees Cook chip->cc_attrs_tbl = devm_kcalloc(&chip->dev, 4, nr_commands,
64658472f5cSJarkko Sakkinen GFP_KERNEL);
647f1689114STadeusz Struk if (!chip->cc_attrs_tbl) {
648f1689114STadeusz Struk rc = -ENOMEM;
649f1689114STadeusz Struk goto out;
650f1689114STadeusz Struk }
65158472f5cSJarkko Sakkinen
65258472f5cSJarkko Sakkinen rc = tpm_buf_init(&buf, TPM2_ST_NO_SESSIONS, TPM2_CC_GET_CAPABILITY);
65358472f5cSJarkko Sakkinen if (rc)
65458472f5cSJarkko Sakkinen goto out;
65558472f5cSJarkko Sakkinen
65658472f5cSJarkko Sakkinen tpm_buf_append_u32(&buf, TPM2_CAP_COMMANDS);
65758472f5cSJarkko Sakkinen tpm_buf_append_u32(&buf, TPM2_CC_FIRST);
65858472f5cSJarkko Sakkinen tpm_buf_append_u32(&buf, nr_commands);
65958472f5cSJarkko Sakkinen
66047a6c28bSJarkko Sakkinen rc = tpm_transmit_cmd(chip, &buf, 9 + 4 * nr_commands, NULL);
66158472f5cSJarkko Sakkinen if (rc) {
66258472f5cSJarkko Sakkinen tpm_buf_destroy(&buf);
66358472f5cSJarkko Sakkinen goto out;
66458472f5cSJarkko Sakkinen }
66558472f5cSJarkko Sakkinen
66658472f5cSJarkko Sakkinen if (nr_commands !=
66758472f5cSJarkko Sakkinen be32_to_cpup((__be32 *)&buf.data[TPM_HEADER_SIZE + 5])) {
6681df83992SZhen Lei rc = -EFAULT;
66958472f5cSJarkko Sakkinen tpm_buf_destroy(&buf);
67058472f5cSJarkko Sakkinen goto out;
67158472f5cSJarkko Sakkinen }
67258472f5cSJarkko Sakkinen
67358472f5cSJarkko Sakkinen chip->nr_commands = nr_commands;
67458472f5cSJarkko Sakkinen
675171360d7SJarkko Sakkinen attrs = (__be32 *)&buf.data[TPM_HEADER_SIZE + 9];
67658472f5cSJarkko Sakkinen for (i = 0; i < nr_commands; i++, attrs++) {
67758472f5cSJarkko Sakkinen chip->cc_attrs_tbl[i] = be32_to_cpup(attrs);
67858472f5cSJarkko Sakkinen cc = chip->cc_attrs_tbl[i] & 0xFFFF;
67958472f5cSJarkko Sakkinen
68058472f5cSJarkko Sakkinen if (cc == TPM2_CC_CONTEXT_SAVE || cc == TPM2_CC_FLUSH_CONTEXT) {
68158472f5cSJarkko Sakkinen chip->cc_attrs_tbl[i] &=
68258472f5cSJarkko Sakkinen ~(GENMASK(2, 0) << TPM2_CC_ATTR_CHANDLES);
68358472f5cSJarkko Sakkinen chip->cc_attrs_tbl[i] |= 1 << TPM2_CC_ATTR_CHANDLES;
68458472f5cSJarkko Sakkinen }
68558472f5cSJarkko Sakkinen }
68658472f5cSJarkko Sakkinen
68758472f5cSJarkko Sakkinen tpm_buf_destroy(&buf);
68858472f5cSJarkko Sakkinen
68958472f5cSJarkko Sakkinen out:
69058472f5cSJarkko Sakkinen if (rc > 0)
69158472f5cSJarkko Sakkinen rc = -ENODEV;
69258472f5cSJarkko Sakkinen return rc;
69358472f5cSJarkko Sakkinen }
694684c6bd8SStefan Berger EXPORT_SYMBOL_GPL(tpm2_get_cc_attrs_tbl);
69558472f5cSJarkko Sakkinen
69661841be6SJarkko Sakkinen /**
6979db7fe18STomas Winkler * tpm2_startup - turn on the TPM
6989db7fe18STomas Winkler * @chip: TPM chip to use
6999db7fe18STomas Winkler *
7009db7fe18STomas Winkler * Normally the firmware should start the TPM. This function is provided as a
7019db7fe18STomas Winkler * workaround if this does not happen. A legal case for this could be for
7029db7fe18STomas Winkler * example when a TPM emulator is used.
7039db7fe18STomas Winkler *
7049db7fe18STomas Winkler * Return: same as tpm_transmit_cmd()
7059db7fe18STomas Winkler */
7069db7fe18STomas Winkler
tpm2_startup(struct tpm_chip * chip)7079db7fe18STomas Winkler static int tpm2_startup(struct tpm_chip *chip)
7089db7fe18STomas Winkler {
7099db7fe18STomas Winkler struct tpm_buf buf;
7109db7fe18STomas Winkler int rc;
7119db7fe18STomas Winkler
7129db7fe18STomas Winkler dev_info(&chip->dev, "starting up the TPM manually\n");
7139db7fe18STomas Winkler
7149db7fe18STomas Winkler rc = tpm_buf_init(&buf, TPM2_ST_NO_SESSIONS, TPM2_CC_STARTUP);
7159db7fe18STomas Winkler if (rc < 0)
7169db7fe18STomas Winkler return rc;
7179db7fe18STomas Winkler
7189db7fe18STomas Winkler tpm_buf_append_u16(&buf, TPM2_SU_CLEAR);
71947a6c28bSJarkko Sakkinen rc = tpm_transmit_cmd(chip, &buf, 0, "attempting to start the TPM");
7209db7fe18STomas Winkler tpm_buf_destroy(&buf);
7219db7fe18STomas Winkler
7229db7fe18STomas Winkler return rc;
7239db7fe18STomas Winkler }
7249db7fe18STomas Winkler
7259db7fe18STomas Winkler /**
72661841be6SJarkko Sakkinen * tpm2_auto_startup - Perform the standard automatic TPM initialization
72761841be6SJarkko Sakkinen * sequence
72861841be6SJarkko Sakkinen * @chip: TPM chip to use
72961841be6SJarkko Sakkinen *
73058472f5cSJarkko Sakkinen * Returns 0 on success, < 0 in case of fatal error.
73161841be6SJarkko Sakkinen */
tpm2_auto_startup(struct tpm_chip * chip)73261841be6SJarkko Sakkinen int tpm2_auto_startup(struct tpm_chip *chip)
73361841be6SJarkko Sakkinen {
73461841be6SJarkko Sakkinen int rc;
73561841be6SJarkko Sakkinen
7369db7fe18STomas Winkler rc = tpm2_get_timeouts(chip);
73761841be6SJarkko Sakkinen if (rc)
73861841be6SJarkko Sakkinen goto out;
73961841be6SJarkko Sakkinen
74061841be6SJarkko Sakkinen rc = tpm2_do_selftest(chip);
7412be8ffedSJames Bottomley if (rc && rc != TPM2_RC_INITIALIZE)
74261841be6SJarkko Sakkinen goto out;
74361841be6SJarkko Sakkinen
74461841be6SJarkko Sakkinen if (rc == TPM2_RC_INITIALIZE) {
7459db7fe18STomas Winkler rc = tpm2_startup(chip);
74661841be6SJarkko Sakkinen if (rc)
74761841be6SJarkko Sakkinen goto out;
74861841be6SJarkko Sakkinen
74961841be6SJarkko Sakkinen rc = tpm2_do_selftest(chip);
7502be8ffedSJames Bottomley if (rc)
75161841be6SJarkko Sakkinen goto out;
75261841be6SJarkko Sakkinen }
75361841be6SJarkko Sakkinen
75458472f5cSJarkko Sakkinen rc = tpm2_get_cc_attrs_tbl(chip);
755863ed94cSMårten Lindahl if (rc == TPM2_RC_FAILURE || (rc < 0 && rc != -ENOMEM)) {
756863ed94cSMårten Lindahl dev_info(&chip->dev,
757863ed94cSMårten Lindahl "TPM in field failure mode, requires firmware upgrade\n");
758863ed94cSMårten Lindahl chip->flags |= TPM_CHIP_FLAG_FIRMWARE_UPGRADE;
759863ed94cSMårten Lindahl rc = 0;
760863ed94cSMårten Lindahl }
76161841be6SJarkko Sakkinen
76261841be6SJarkko Sakkinen out:
763af402ee3SStefan Mahnke-Hartmann /*
764af402ee3SStefan Mahnke-Hartmann * Infineon TPM in field upgrade mode will return no data for the number
765af402ee3SStefan Mahnke-Hartmann * of supported commands.
766af402ee3SStefan Mahnke-Hartmann */
767af402ee3SStefan Mahnke-Hartmann if (rc == TPM2_RC_UPGRADE || rc == -ENODATA) {
7680aa69878Saxelj dev_info(&chip->dev, "TPM in field upgrade mode, requires firmware upgrade\n");
7690aa69878Saxelj chip->flags |= TPM_CHIP_FLAG_FIRMWARE_UPGRADE;
7700aa69878Saxelj rc = 0;
7710aa69878Saxelj }
7720aa69878Saxelj
77361841be6SJarkko Sakkinen if (rc > 0)
77461841be6SJarkko Sakkinen rc = -ENODEV;
77561841be6SJarkko Sakkinen return rc;
77661841be6SJarkko Sakkinen }
77758472f5cSJarkko Sakkinen
tpm2_find_cc(struct tpm_chip * chip,u32 cc)77858472f5cSJarkko Sakkinen int tpm2_find_cc(struct tpm_chip *chip, u32 cc)
77958472f5cSJarkko Sakkinen {
780*85b93bbdSJulien Gomes u32 cc_mask;
78158472f5cSJarkko Sakkinen int i;
78258472f5cSJarkko Sakkinen
783*85b93bbdSJulien Gomes cc_mask = 1 << TPM2_CC_ATTR_VENDOR | GENMASK(15, 0);
78458472f5cSJarkko Sakkinen for (i = 0; i < chip->nr_commands; i++)
785*85b93bbdSJulien Gomes if (cc == (chip->cc_attrs_tbl[i] & cc_mask))
78658472f5cSJarkko Sakkinen return i;
78758472f5cSJarkko Sakkinen
78858472f5cSJarkko Sakkinen return -1;
78958472f5cSJarkko Sakkinen }
790