2 * Copyright (C) 2014, 2015 Intel Corporation
5 * Jarkko Sakkinen <jarkko.sakkinen@linux.intel.com>
7 * Maintained by: <tpmdd-devel@lists.sourceforge.net>
9 * This file contains TPM2 protocol implementations of the commands
10 * used by the kernel internally.
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; version 2
19 #include <crypto/hash_info.h>
20 #include <keys/trusted-type.h>
22 enum tpm2_object_attributes
{
23 TPM2_ATTR_USER_WITH_AUTH
= BIT(6),
26 struct tpm2_startup_in
{
30 struct tpm2_self_test_in
{
34 struct tpm2_pcr_read_in
{
35 __be32 pcr_selects_cnt
;
38 u8 pcr_select
[TPM2_PCR_SELECT_MIN
];
41 struct tpm2_pcr_read_out
{
43 __be32 pcr_selects_cnt
;
46 u8 pcr_select
[TPM2_PCR_SELECT_MIN
];
49 u8 digest
[TPM_DIGEST_SIZE
];
52 struct tpm2_null_auth_area
{
59 struct tpm2_pcr_extend_in
{
61 __be32 auth_area_size
;
62 struct tpm2_null_auth_area auth_area
;
65 u8 digest
[TPM_DIGEST_SIZE
];
68 struct tpm2_get_tpm_pt_in
{
74 struct tpm2_get_tpm_pt_out
{
82 struct tpm2_get_random_in
{
86 struct tpm2_get_random_out
{
88 u8 buffer
[TPM_MAX_RNG_DATA
];
91 union tpm2_cmd_params
{
92 struct tpm2_startup_in startup_in
;
93 struct tpm2_self_test_in selftest_in
;
94 struct tpm2_pcr_read_in pcrread_in
;
95 struct tpm2_pcr_read_out pcrread_out
;
96 struct tpm2_pcr_extend_in pcrextend_in
;
97 struct tpm2_get_tpm_pt_in get_tpm_pt_in
;
98 struct tpm2_get_tpm_pt_out get_tpm_pt_out
;
99 struct tpm2_get_random_in getrandom_in
;
100 struct tpm2_get_random_out getrandom_out
;
104 tpm_cmd_header header
;
105 union tpm2_cmd_params params
;
109 unsigned int crypto_id
;
113 static struct tpm2_hash tpm2_hash_map
[] = {
114 {HASH_ALGO_SHA1
, TPM2_ALG_SHA1
},
115 {HASH_ALGO_SHA256
, TPM2_ALG_SHA256
},
116 {HASH_ALGO_SHA384
, TPM2_ALG_SHA384
},
117 {HASH_ALGO_SHA512
, TPM2_ALG_SHA512
},
118 {HASH_ALGO_SM3_256
, TPM2_ALG_SM3_256
},
122 * Array with one entry per ordinal defining the maximum amount
123 * of time the chip could take to return the result. The values
124 * of the SHORT, MEDIUM, and LONG durations are taken from the
125 * PC Client Profile (PTP) specification.
127 static const u8 tpm2_ordinal_duration
[TPM2_CC_LAST
- TPM2_CC_FIRST
+ 1] = {
128 TPM_UNDEFINED
, /* 11F */
129 TPM_UNDEFINED
, /* 120 */
131 TPM_UNDEFINED
, /* 122 */
132 TPM_UNDEFINED
, /* 123 */
133 TPM_UNDEFINED
, /* 124 */
134 TPM_UNDEFINED
, /* 125 */
135 TPM_UNDEFINED
, /* 126 */
136 TPM_UNDEFINED
, /* 127 */
137 TPM_UNDEFINED
, /* 128 */
139 TPM_UNDEFINED
, /* 12a */
140 TPM_UNDEFINED
, /* 12b */
141 TPM_UNDEFINED
, /* 12c */
142 TPM_UNDEFINED
, /* 12d */
143 TPM_UNDEFINED
, /* 12e */
144 TPM_UNDEFINED
, /* 12f */
145 TPM_UNDEFINED
, /* 130 */
146 TPM_UNDEFINED
, /* 131 */
147 TPM_UNDEFINED
, /* 132 */
148 TPM_UNDEFINED
, /* 133 */
149 TPM_UNDEFINED
, /* 134 */
150 TPM_UNDEFINED
, /* 135 */
151 TPM_UNDEFINED
, /* 136 */
152 TPM_UNDEFINED
, /* 137 */
153 TPM_UNDEFINED
, /* 138 */
154 TPM_UNDEFINED
, /* 139 */
155 TPM_UNDEFINED
, /* 13a */
156 TPM_UNDEFINED
, /* 13b */
157 TPM_UNDEFINED
, /* 13c */
158 TPM_UNDEFINED
, /* 13d */
159 TPM_MEDIUM
, /* 13e */
160 TPM_UNDEFINED
, /* 13f */
161 TPM_UNDEFINED
, /* 140 */
162 TPM_UNDEFINED
, /* 141 */
163 TPM_UNDEFINED
, /* 142 */
165 TPM_MEDIUM
, /* 144 */
166 TPM_UNDEFINED
, /* 145 */
167 TPM_UNDEFINED
, /* 146 */
168 TPM_UNDEFINED
, /* 147 */
169 TPM_UNDEFINED
, /* 148 */
170 TPM_UNDEFINED
, /* 149 */
171 TPM_UNDEFINED
, /* 14a */
172 TPM_UNDEFINED
, /* 14b */
173 TPM_UNDEFINED
, /* 14c */
174 TPM_UNDEFINED
, /* 14d */
176 TPM_UNDEFINED
, /* 14f */
177 TPM_UNDEFINED
, /* 150 */
178 TPM_UNDEFINED
, /* 151 */
179 TPM_UNDEFINED
, /* 152 */
180 TPM_UNDEFINED
, /* 153 */
181 TPM_UNDEFINED
, /* 154 */
182 TPM_UNDEFINED
, /* 155 */
183 TPM_UNDEFINED
, /* 156 */
184 TPM_UNDEFINED
, /* 157 */
185 TPM_UNDEFINED
, /* 158 */
186 TPM_UNDEFINED
, /* 159 */
187 TPM_UNDEFINED
, /* 15a */
188 TPM_UNDEFINED
, /* 15b */
189 TPM_MEDIUM
, /* 15c */
190 TPM_UNDEFINED
, /* 15d */
191 TPM_UNDEFINED
, /* 15e */
192 TPM_UNDEFINED
, /* 15f */
193 TPM_UNDEFINED
, /* 160 */
194 TPM_UNDEFINED
, /* 161 */
195 TPM_UNDEFINED
, /* 162 */
196 TPM_UNDEFINED
, /* 163 */
197 TPM_UNDEFINED
, /* 164 */
198 TPM_UNDEFINED
, /* 165 */
199 TPM_UNDEFINED
, /* 166 */
200 TPM_UNDEFINED
, /* 167 */
201 TPM_UNDEFINED
, /* 168 */
202 TPM_UNDEFINED
, /* 169 */
203 TPM_UNDEFINED
, /* 16a */
204 TPM_UNDEFINED
, /* 16b */
205 TPM_UNDEFINED
, /* 16c */
206 TPM_UNDEFINED
, /* 16d */
207 TPM_UNDEFINED
, /* 16e */
208 TPM_UNDEFINED
, /* 16f */
209 TPM_UNDEFINED
, /* 170 */
210 TPM_UNDEFINED
, /* 171 */
211 TPM_UNDEFINED
, /* 172 */
212 TPM_UNDEFINED
, /* 173 */
213 TPM_UNDEFINED
, /* 174 */
214 TPM_UNDEFINED
, /* 175 */
215 TPM_UNDEFINED
, /* 176 */
217 TPM_UNDEFINED
, /* 178 */
218 TPM_UNDEFINED
, /* 179 */
219 TPM_MEDIUM
, /* 17a */
221 TPM_UNDEFINED
, /* 17c */
222 TPM_UNDEFINED
, /* 17d */
223 TPM_UNDEFINED
, /* 17e */
224 TPM_UNDEFINED
, /* 17f */
225 TPM_UNDEFINED
, /* 180 */
226 TPM_UNDEFINED
, /* 181 */
227 TPM_MEDIUM
, /* 182 */
228 TPM_UNDEFINED
, /* 183 */
229 TPM_UNDEFINED
, /* 184 */
230 TPM_MEDIUM
, /* 185 */
231 TPM_MEDIUM
, /* 186 */
232 TPM_UNDEFINED
, /* 187 */
233 TPM_UNDEFINED
, /* 188 */
234 TPM_UNDEFINED
, /* 189 */
235 TPM_UNDEFINED
, /* 18a */
236 TPM_UNDEFINED
, /* 18b */
237 TPM_UNDEFINED
, /* 18c */
238 TPM_UNDEFINED
, /* 18d */
239 TPM_UNDEFINED
, /* 18e */
240 TPM_UNDEFINED
/* 18f */
243 #define TPM2_PCR_READ_IN_SIZE \
244 (sizeof(struct tpm_input_header) + \
245 sizeof(struct tpm2_pcr_read_in))
247 static const struct tpm_input_header tpm2_pcrread_header
= {
248 .tag
= cpu_to_be16(TPM2_ST_NO_SESSIONS
),
249 .length
= cpu_to_be32(TPM2_PCR_READ_IN_SIZE
),
250 .ordinal
= cpu_to_be32(TPM2_CC_PCR_READ
)
254 * tpm2_pcr_read() - read a PCR value
255 * @chip: TPM chip to use.
256 * @pcr_idx: index of the PCR to read.
257 * @ref_buf: buffer to store the resulting hash,
259 * 0 is returned when the operation is successful. If a negative number is
260 * returned it remarks a POSIX error code. If a positive number is returned
261 * it remarks a TPM error.
263 int tpm2_pcr_read(struct tpm_chip
*chip
, int pcr_idx
, u8
*res_buf
)
269 if (pcr_idx
>= TPM2_PLATFORM_PCR
)
272 cmd
.header
.in
= tpm2_pcrread_header
;
273 cmd
.params
.pcrread_in
.pcr_selects_cnt
= cpu_to_be32(1);
274 cmd
.params
.pcrread_in
.hash_alg
= cpu_to_be16(TPM2_ALG_SHA1
);
275 cmd
.params
.pcrread_in
.pcr_select_size
= TPM2_PCR_SELECT_MIN
;
277 memset(cmd
.params
.pcrread_in
.pcr_select
, 0,
278 sizeof(cmd
.params
.pcrread_in
.pcr_select
));
279 cmd
.params
.pcrread_in
.pcr_select
[pcr_idx
>> 3] = 1 << (pcr_idx
& 0x7);
281 rc
= tpm_transmit_cmd(chip
, &cmd
, sizeof(cmd
),
282 "attempting to read a pcr value");
284 buf
= cmd
.params
.pcrread_out
.digest
;
285 memcpy(res_buf
, buf
, TPM_DIGEST_SIZE
);
291 #define TPM2_GET_PCREXTEND_IN_SIZE \
292 (sizeof(struct tpm_input_header) + \
293 sizeof(struct tpm2_pcr_extend_in))
295 static const struct tpm_input_header tpm2_pcrextend_header
= {
296 .tag
= cpu_to_be16(TPM2_ST_SESSIONS
),
297 .length
= cpu_to_be32(TPM2_GET_PCREXTEND_IN_SIZE
),
298 .ordinal
= cpu_to_be32(TPM2_CC_PCR_EXTEND
)
302 * tpm2_pcr_extend() - extend a PCR value
303 * @chip: TPM chip to use.
304 * @pcr_idx: index of the PCR.
305 * @hash: hash value to use for the extend operation.
307 * 0 is returned when the operation is successful. If a negative number is
308 * returned it remarks a POSIX error code. If a positive number is returned
309 * it remarks a TPM error.
311 int tpm2_pcr_extend(struct tpm_chip
*chip
, int pcr_idx
, const u8
*hash
)
316 cmd
.header
.in
= tpm2_pcrextend_header
;
317 cmd
.params
.pcrextend_in
.pcr_idx
= cpu_to_be32(pcr_idx
);
318 cmd
.params
.pcrextend_in
.auth_area_size
=
319 cpu_to_be32(sizeof(struct tpm2_null_auth_area
));
320 cmd
.params
.pcrextend_in
.auth_area
.handle
=
321 cpu_to_be32(TPM2_RS_PW
);
322 cmd
.params
.pcrextend_in
.auth_area
.nonce_size
= 0;
323 cmd
.params
.pcrextend_in
.auth_area
.attributes
= 0;
324 cmd
.params
.pcrextend_in
.auth_area
.auth_size
= 0;
325 cmd
.params
.pcrextend_in
.digest_cnt
= cpu_to_be32(1);
326 cmd
.params
.pcrextend_in
.hash_alg
= cpu_to_be16(TPM2_ALG_SHA1
);
327 memcpy(cmd
.params
.pcrextend_in
.digest
, hash
, TPM_DIGEST_SIZE
);
329 rc
= tpm_transmit_cmd(chip
, &cmd
, sizeof(cmd
),
330 "attempting extend a PCR value");
335 #define TPM2_GETRANDOM_IN_SIZE \
336 (sizeof(struct tpm_input_header) + \
337 sizeof(struct tpm2_get_random_in))
339 static const struct tpm_input_header tpm2_getrandom_header
= {
340 .tag
= cpu_to_be16(TPM2_ST_NO_SESSIONS
),
341 .length
= cpu_to_be32(TPM2_GETRANDOM_IN_SIZE
),
342 .ordinal
= cpu_to_be32(TPM2_CC_GET_RANDOM
)
346 * tpm2_get_random() - get random bytes from the TPM RNG
347 * @chip: TPM chip to use
348 * @out: destination buffer for the random bytes
349 * @max: the max number of bytes to write to @out
351 * 0 is returned when the operation is successful. If a negative number is
352 * returned it remarks a POSIX error code. If a positive number is returned
353 * it remarks a TPM error.
355 int tpm2_get_random(struct tpm_chip
*chip
, u8
*out
, size_t max
)
365 num_bytes
= min_t(u32
, max
, sizeof(cmd
.params
.getrandom_out
.buffer
));
367 if (!out
|| !num_bytes
||
368 max
> sizeof(cmd
.params
.getrandom_out
.buffer
))
372 cmd
.header
.in
= tpm2_getrandom_header
;
373 cmd
.params
.getrandom_in
.size
= cpu_to_be16(num_bytes
);
375 err
= tpm_transmit_cmd(chip
, &cmd
, sizeof(cmd
),
376 "attempting get random");
380 recd
= min_t(u32
, be16_to_cpu(cmd
.params
.getrandom_out
.size
),
382 memcpy(dest
, cmd
.params
.getrandom_out
.buffer
, recd
);
387 } while (retries
-- && total
< max
);
389 return total
? total
: -EIO
;
392 #define TPM2_GET_TPM_PT_IN_SIZE \
393 (sizeof(struct tpm_input_header) + \
394 sizeof(struct tpm2_get_tpm_pt_in))
396 static const struct tpm_input_header tpm2_get_tpm_pt_header
= {
397 .tag
= cpu_to_be16(TPM2_ST_NO_SESSIONS
),
398 .length
= cpu_to_be32(TPM2_GET_TPM_PT_IN_SIZE
),
399 .ordinal
= cpu_to_be32(TPM2_CC_GET_CAPABILITY
)
403 * Append TPMS_AUTH_COMMAND to the buffer. The buffer must be allocated with
406 * @param buf: an allocated tpm_buf instance
407 * @param nonce: the session nonce, may be NULL if not used
408 * @param nonce_len: the session nonce length, may be 0 if not used
409 * @param attributes: the session attributes
410 * @param hmac: the session HMAC or password, may be NULL if not used
411 * @param hmac_len: the session HMAC or password length, maybe 0 if not used
413 static void tpm2_buf_append_auth(struct tpm_buf
*buf
, u32 session_handle
,
414 const u8
*nonce
, u16 nonce_len
,
416 const u8
*hmac
, u16 hmac_len
)
418 tpm_buf_append_u32(buf
, 9 + nonce_len
+ hmac_len
);
419 tpm_buf_append_u32(buf
, session_handle
);
420 tpm_buf_append_u16(buf
, nonce_len
);
422 if (nonce
&& nonce_len
)
423 tpm_buf_append(buf
, nonce
, nonce_len
);
425 tpm_buf_append_u8(buf
, attributes
);
426 tpm_buf_append_u16(buf
, hmac_len
);
428 if (hmac
&& hmac_len
)
429 tpm_buf_append(buf
, hmac
, hmac_len
);
433 * tpm2_seal_trusted() - seal a trusted key
434 * @chip_num: A specific chip number for the request or TPM_ANY_NUM
435 * @options: authentication values and other options
436 * @payload: the key data in clear and encrypted form
438 * Returns < 0 on error and 0 on success.
440 int tpm2_seal_trusted(struct tpm_chip
*chip
,
441 struct trusted_key_payload
*payload
,
442 struct trusted_key_options
*options
)
444 unsigned int blob_len
;
450 for (i
= 0; i
< ARRAY_SIZE(tpm2_hash_map
); i
++) {
451 if (options
->hash
== tpm2_hash_map
[i
].crypto_id
) {
452 hash
= tpm2_hash_map
[i
].tpm_id
;
457 if (i
== ARRAY_SIZE(tpm2_hash_map
))
460 rc
= tpm_buf_init(&buf
, TPM2_ST_SESSIONS
, TPM2_CC_CREATE
);
464 tpm_buf_append_u32(&buf
, options
->keyhandle
);
465 tpm2_buf_append_auth(&buf
, TPM2_RS_PW
,
467 0 /* session_attributes */,
468 options
->keyauth
/* hmac */,
472 tpm_buf_append_u16(&buf
, 4 + TPM_DIGEST_SIZE
+ payload
->key_len
+ 1);
474 tpm_buf_append_u16(&buf
, TPM_DIGEST_SIZE
);
475 tpm_buf_append(&buf
, options
->blobauth
, TPM_DIGEST_SIZE
);
476 tpm_buf_append_u16(&buf
, payload
->key_len
+ 1);
477 tpm_buf_append(&buf
, payload
->key
, payload
->key_len
);
478 tpm_buf_append_u8(&buf
, payload
->migratable
);
481 tpm_buf_append_u16(&buf
, 14);
483 tpm_buf_append_u16(&buf
, TPM2_ALG_KEYEDHASH
);
484 tpm_buf_append_u16(&buf
, hash
);
485 tpm_buf_append_u32(&buf
, TPM2_ATTR_USER_WITH_AUTH
);
486 tpm_buf_append_u16(&buf
, 0); /* policy digest size */
487 tpm_buf_append_u16(&buf
, TPM2_ALG_NULL
);
488 tpm_buf_append_u16(&buf
, 0);
491 tpm_buf_append_u16(&buf
, 0);
494 tpm_buf_append_u32(&buf
, 0);
496 if (buf
.flags
& TPM_BUF_OVERFLOW
) {
501 rc
= tpm_transmit_cmd(chip
, buf
.data
, PAGE_SIZE
, "sealing data");
505 blob_len
= be32_to_cpup((__be32
*) &buf
.data
[TPM_HEADER_SIZE
]);
506 if (blob_len
> MAX_BLOB_SIZE
) {
511 memcpy(payload
->blob
, &buf
.data
[TPM_HEADER_SIZE
+ 4], blob_len
);
512 payload
->blob_len
= blob_len
;
515 tpm_buf_destroy(&buf
);
518 if ((rc
& TPM2_RC_HASH
) == TPM2_RC_HASH
)
527 static int tpm2_load(struct tpm_chip
*chip
,
528 struct trusted_key_payload
*payload
,
529 struct trusted_key_options
*options
,
533 unsigned int private_len
;
534 unsigned int public_len
;
535 unsigned int blob_len
;
538 private_len
= be16_to_cpup((__be16
*) &payload
->blob
[0]);
539 if (private_len
> (payload
->blob_len
- 2))
542 public_len
= be16_to_cpup((__be16
*) &payload
->blob
[2 + private_len
]);
543 blob_len
= private_len
+ public_len
+ 4;
544 if (blob_len
> payload
->blob_len
)
547 rc
= tpm_buf_init(&buf
, TPM2_ST_SESSIONS
, TPM2_CC_LOAD
);
551 tpm_buf_append_u32(&buf
, options
->keyhandle
);
552 tpm2_buf_append_auth(&buf
, TPM2_RS_PW
,
554 0 /* session_attributes */,
555 options
->keyauth
/* hmac */,
558 tpm_buf_append(&buf
, payload
->blob
, blob_len
);
560 if (buf
.flags
& TPM_BUF_OVERFLOW
) {
565 rc
= tpm_transmit_cmd(chip
, buf
.data
, PAGE_SIZE
, "loading blob");
567 *blob_handle
= be32_to_cpup(
568 (__be32
*) &buf
.data
[TPM_HEADER_SIZE
]);
571 tpm_buf_destroy(&buf
);
579 static void tpm2_flush_context(struct tpm_chip
*chip
, u32 handle
)
584 rc
= tpm_buf_init(&buf
, TPM2_ST_NO_SESSIONS
, TPM2_CC_FLUSH_CONTEXT
);
586 dev_warn(chip
->pdev
, "0x%08x was not flushed, out of memory\n",
591 tpm_buf_append_u32(&buf
, handle
);
593 rc
= tpm_transmit_cmd(chip
, buf
.data
, PAGE_SIZE
, "flushing context");
595 dev_warn(chip
->pdev
, "0x%08x was not flushed, rc=%d\n", handle
,
598 tpm_buf_destroy(&buf
);
601 static int tpm2_unseal(struct tpm_chip
*chip
,
602 struct trusted_key_payload
*payload
,
603 struct trusted_key_options
*options
,
611 rc
= tpm_buf_init(&buf
, TPM2_ST_SESSIONS
, TPM2_CC_UNSEAL
);
615 tpm_buf_append_u32(&buf
, blob_handle
);
616 tpm2_buf_append_auth(&buf
, TPM2_RS_PW
,
618 0 /* session_attributes */,
619 options
->blobauth
/* hmac */,
622 rc
= tpm_transmit_cmd(chip
, buf
.data
, PAGE_SIZE
, "unsealing");
627 data_len
= be16_to_cpup(
628 (__be16
*) &buf
.data
[TPM_HEADER_SIZE
+ 4]);
629 data
= &buf
.data
[TPM_HEADER_SIZE
+ 6];
631 memcpy(payload
->key
, data
, data_len
- 1);
632 payload
->key_len
= data_len
- 1;
633 payload
->migratable
= data
[data_len
- 1];
636 tpm_buf_destroy(&buf
);
641 * tpm_unseal_trusted() - unseal a trusted key
642 * @chip_num: A specific chip number for the request or TPM_ANY_NUM
643 * @options: authentication values and other options
644 * @payload: the key data in clear and encrypted form
646 * Returns < 0 on error and 0 on success.
648 int tpm2_unseal_trusted(struct tpm_chip
*chip
,
649 struct trusted_key_payload
*payload
,
650 struct trusted_key_options
*options
)
655 rc
= tpm2_load(chip
, payload
, options
, &blob_handle
);
659 rc
= tpm2_unseal(chip
, payload
, options
, blob_handle
);
661 tpm2_flush_context(chip
, blob_handle
);
667 * tpm2_get_tpm_pt() - get value of a TPM_CAP_TPM_PROPERTIES type property
668 * @chip: TPM chip to use.
669 * @property_id: property ID.
670 * @value: output variable.
671 * @desc: passed to tpm_transmit_cmd()
673 * 0 is returned when the operation is successful. If a negative number is
674 * returned it remarks a POSIX error code. If a positive number is returned
675 * it remarks a TPM error.
677 ssize_t
tpm2_get_tpm_pt(struct tpm_chip
*chip
, u32 property_id
, u32
*value
,
683 cmd
.header
.in
= tpm2_get_tpm_pt_header
;
684 cmd
.params
.get_tpm_pt_in
.cap_id
= cpu_to_be32(TPM2_CAP_TPM_PROPERTIES
);
685 cmd
.params
.get_tpm_pt_in
.property_id
= cpu_to_be32(property_id
);
686 cmd
.params
.get_tpm_pt_in
.property_cnt
= cpu_to_be32(1);
688 rc
= tpm_transmit_cmd(chip
, &cmd
, sizeof(cmd
), desc
);
690 *value
= cmd
.params
.get_tpm_pt_out
.value
;
695 #define TPM2_STARTUP_IN_SIZE \
696 (sizeof(struct tpm_input_header) + \
697 sizeof(struct tpm2_startup_in))
699 static const struct tpm_input_header tpm2_startup_header
= {
700 .tag
= cpu_to_be16(TPM2_ST_NO_SESSIONS
),
701 .length
= cpu_to_be32(TPM2_STARTUP_IN_SIZE
),
702 .ordinal
= cpu_to_be32(TPM2_CC_STARTUP
)
706 * tpm2_startup() - send startup command to the TPM chip
707 * @chip: TPM chip to use.
708 * @startup_type startup type. The value is either
709 * TPM_SU_CLEAR or TPM_SU_STATE.
711 * 0 is returned when the operation is successful. If a negative number is
712 * returned it remarks a POSIX error code. If a positive number is returned
713 * it remarks a TPM error.
715 int tpm2_startup(struct tpm_chip
*chip
, u16 startup_type
)
719 cmd
.header
.in
= tpm2_startup_header
;
721 cmd
.params
.startup_in
.startup_type
= cpu_to_be16(startup_type
);
722 return tpm_transmit_cmd(chip
, &cmd
, sizeof(cmd
),
723 "attempting to start the TPM");
725 EXPORT_SYMBOL_GPL(tpm2_startup
);
727 #define TPM2_SHUTDOWN_IN_SIZE \
728 (sizeof(struct tpm_input_header) + \
729 sizeof(struct tpm2_startup_in))
731 static const struct tpm_input_header tpm2_shutdown_header
= {
732 .tag
= cpu_to_be16(TPM2_ST_NO_SESSIONS
),
733 .length
= cpu_to_be32(TPM2_SHUTDOWN_IN_SIZE
),
734 .ordinal
= cpu_to_be32(TPM2_CC_SHUTDOWN
)
738 * tpm2_shutdown() - send shutdown command to the TPM chip
739 * @chip: TPM chip to use.
740 * @shutdown_type shutdown type. The value is either
741 * TPM_SU_CLEAR or TPM_SU_STATE.
743 void tpm2_shutdown(struct tpm_chip
*chip
, u16 shutdown_type
)
748 cmd
.header
.in
= tpm2_shutdown_header
;
749 cmd
.params
.startup_in
.startup_type
= cpu_to_be16(shutdown_type
);
751 rc
= tpm_transmit_cmd(chip
, &cmd
, sizeof(cmd
), "stopping the TPM");
753 /* In places where shutdown command is sent there's no much we can do
754 * except print the error code on a system failure.
757 dev_warn(chip
->pdev
, "transmit returned %d while stopping the TPM",
760 EXPORT_SYMBOL_GPL(tpm2_shutdown
);
763 * tpm2_calc_ordinal_duration() - maximum duration for a command
764 * @chip: TPM chip to use.
765 * @ordinal: command code number.
767 * 0 is returned when the operation is successful. If a negative number is
768 * returned it remarks a POSIX error code. If a positive number is returned
769 * it remarks a TPM error.
771 unsigned long tpm2_calc_ordinal_duration(struct tpm_chip
*chip
, u32 ordinal
)
773 int index
= TPM_UNDEFINED
;
776 if (ordinal
>= TPM2_CC_FIRST
&& ordinal
<= TPM2_CC_LAST
)
777 index
= tpm2_ordinal_duration
[ordinal
- TPM2_CC_FIRST
];
779 if (index
!= TPM_UNDEFINED
)
780 duration
= chip
->vendor
.duration
[index
];
783 duration
= 2 * 60 * HZ
;
787 EXPORT_SYMBOL_GPL(tpm2_calc_ordinal_duration
);
789 #define TPM2_SELF_TEST_IN_SIZE \
790 (sizeof(struct tpm_input_header) + \
791 sizeof(struct tpm2_self_test_in))
793 static const struct tpm_input_header tpm2_selftest_header
= {
794 .tag
= cpu_to_be16(TPM2_ST_NO_SESSIONS
),
795 .length
= cpu_to_be32(TPM2_SELF_TEST_IN_SIZE
),
796 .ordinal
= cpu_to_be32(TPM2_CC_SELF_TEST
)
800 * tpm2_continue_selftest() - start a self test
801 * @chip: TPM chip to use
802 * @full: test all commands instead of testing only those that were not
805 * 0 is returned when the operation is successful. If a negative number is
806 * returned it remarks a POSIX error code. If a positive number is returned
807 * it remarks a TPM error.
809 static int tpm2_start_selftest(struct tpm_chip
*chip
, bool full
)
814 cmd
.header
.in
= tpm2_selftest_header
;
815 cmd
.params
.selftest_in
.full_test
= full
;
817 rc
= tpm_transmit_cmd(chip
, &cmd
, TPM2_SELF_TEST_IN_SIZE
,
818 "continue selftest");
820 /* At least some prototype chips seem to give RC_TESTING error
821 * immediately. This is a workaround for that.
823 if (rc
== TPM2_RC_TESTING
) {
824 dev_warn(chip
->pdev
, "Got RC_TESTING, ignoring\n");
832 * tpm2_do_selftest() - run a full self test
833 * @chip: TPM chip to use
835 * During the self test TPM2 commands return with the error code RC_TESTING.
836 * Waiting is done by issuing PCR read until it executes successfully.
838 * 0 is returned when the operation is successful. If a negative number is
839 * returned it remarks a POSIX error code. If a positive number is returned
840 * it remarks a TPM error.
842 int tpm2_do_selftest(struct tpm_chip
*chip
)
846 unsigned int delay_msec
= 100;
847 unsigned long duration
;
851 duration
= tpm2_calc_ordinal_duration(chip
, TPM2_CC_SELF_TEST
);
853 loops
= jiffies_to_msecs(duration
) / delay_msec
;
855 rc
= tpm2_start_selftest(chip
, true);
859 for (i
= 0; i
< loops
; i
++) {
860 /* Attempt to read a PCR value */
861 cmd
.header
.in
= tpm2_pcrread_header
;
862 cmd
.params
.pcrread_in
.pcr_selects_cnt
= cpu_to_be32(1);
863 cmd
.params
.pcrread_in
.hash_alg
= cpu_to_be16(TPM2_ALG_SHA1
);
864 cmd
.params
.pcrread_in
.pcr_select_size
= TPM2_PCR_SELECT_MIN
;
865 cmd
.params
.pcrread_in
.pcr_select
[0] = 0x01;
866 cmd
.params
.pcrread_in
.pcr_select
[1] = 0x00;
867 cmd
.params
.pcrread_in
.pcr_select
[2] = 0x00;
869 rc
= tpm_transmit_cmd(chip
, (u8
*) &cmd
, sizeof(cmd
), NULL
);
873 rc
= be32_to_cpu(cmd
.header
.out
.return_code
);
874 if (rc
!= TPM2_RC_TESTING
)
882 EXPORT_SYMBOL_GPL(tpm2_do_selftest
);
885 * tpm2_gen_interrupt() - generate an interrupt
886 * @chip: TPM chip to use
888 * 0 is returned when the operation is successful. If a negative number is
889 * returned it remarks a POSIX error code. If a positive number is returned
890 * it remarks a TPM error.
892 int tpm2_gen_interrupt(struct tpm_chip
*chip
)
896 return tpm2_get_tpm_pt(chip
, 0x100, &dummy
,
897 "attempting to generate an interrupt");
899 EXPORT_SYMBOL_GPL(tpm2_gen_interrupt
);
902 * tpm2_probe() - probe TPM 2.0
903 * @chip: TPM chip to use
905 * Send idempotent TPM 2.0 command and see whether TPM 2.0 chip replied based on
908 int tpm2_probe(struct tpm_chip
*chip
)
913 cmd
.header
.in
= tpm2_get_tpm_pt_header
;
914 cmd
.params
.get_tpm_pt_in
.cap_id
= cpu_to_be32(TPM2_CAP_TPM_PROPERTIES
);
915 cmd
.params
.get_tpm_pt_in
.property_id
= cpu_to_be32(0x100);
916 cmd
.params
.get_tpm_pt_in
.property_cnt
= cpu_to_be32(1);
918 rc
= tpm_transmit(chip
, (const char *) &cmd
, sizeof(cmd
));
921 else if (rc
< TPM_HEADER_SIZE
)
924 if (be16_to_cpu(cmd
.header
.out
.tag
) == TPM2_ST_NO_SESSIONS
)
925 chip
->flags
|= TPM_CHIP_FLAG_TPM2
;
929 EXPORT_SYMBOL_GPL(tpm2_probe
);