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 <keys/trusted-type.h>
21 enum tpm2_object_attributes {
22 TPM2_ATTR_USER_WITH_AUTH = BIT(6),
25 struct tpm2_startup_in {
29 struct tpm2_self_test_in {
33 struct tpm2_pcr_read_in {
34 __be32 pcr_selects_cnt;
37 u8 pcr_select[TPM2_PCR_SELECT_MIN];
40 struct tpm2_pcr_read_out {
42 __be32 pcr_selects_cnt;
45 u8 pcr_select[TPM2_PCR_SELECT_MIN];
48 u8 digest[TPM_DIGEST_SIZE];
51 struct tpm2_null_auth_area {
58 struct tpm2_pcr_extend_in {
60 __be32 auth_area_size;
61 struct tpm2_null_auth_area auth_area;
64 u8 digest[TPM_DIGEST_SIZE];
67 struct tpm2_get_tpm_pt_in {
73 struct tpm2_get_tpm_pt_out {
81 struct tpm2_get_random_in {
85 struct tpm2_get_random_out {
87 u8 buffer[TPM_MAX_RNG_DATA];
90 union tpm2_cmd_params {
91 struct tpm2_startup_in startup_in;
92 struct tpm2_self_test_in selftest_in;
93 struct tpm2_pcr_read_in pcrread_in;
94 struct tpm2_pcr_read_out pcrread_out;
95 struct tpm2_pcr_extend_in pcrextend_in;
96 struct tpm2_get_tpm_pt_in get_tpm_pt_in;
97 struct tpm2_get_tpm_pt_out get_tpm_pt_out;
98 struct tpm2_get_random_in getrandom_in;
99 struct tpm2_get_random_out getrandom_out;
103 tpm_cmd_header header;
104 union tpm2_cmd_params params;
108 * Array with one entry per ordinal defining the maximum amount
109 * of time the chip could take to return the result. The values
110 * of the SHORT, MEDIUM, and LONG durations are taken from the
111 * PC Client Profile (PTP) specification.
113 static const u8 tpm2_ordinal_duration[TPM2_CC_LAST - TPM2_CC_FIRST + 1] = {
114 TPM_UNDEFINED, /* 11F */
115 TPM_UNDEFINED, /* 120 */
117 TPM_UNDEFINED, /* 122 */
118 TPM_UNDEFINED, /* 123 */
119 TPM_UNDEFINED, /* 124 */
120 TPM_UNDEFINED, /* 125 */
121 TPM_UNDEFINED, /* 126 */
122 TPM_UNDEFINED, /* 127 */
123 TPM_UNDEFINED, /* 128 */
125 TPM_UNDEFINED, /* 12a */
126 TPM_UNDEFINED, /* 12b */
127 TPM_UNDEFINED, /* 12c */
128 TPM_UNDEFINED, /* 12d */
129 TPM_UNDEFINED, /* 12e */
130 TPM_UNDEFINED, /* 12f */
131 TPM_UNDEFINED, /* 130 */
132 TPM_UNDEFINED, /* 131 */
133 TPM_UNDEFINED, /* 132 */
134 TPM_UNDEFINED, /* 133 */
135 TPM_UNDEFINED, /* 134 */
136 TPM_UNDEFINED, /* 135 */
137 TPM_UNDEFINED, /* 136 */
138 TPM_UNDEFINED, /* 137 */
139 TPM_UNDEFINED, /* 138 */
140 TPM_UNDEFINED, /* 139 */
141 TPM_UNDEFINED, /* 13a */
142 TPM_UNDEFINED, /* 13b */
143 TPM_UNDEFINED, /* 13c */
144 TPM_UNDEFINED, /* 13d */
145 TPM_MEDIUM, /* 13e */
146 TPM_UNDEFINED, /* 13f */
147 TPM_UNDEFINED, /* 140 */
148 TPM_UNDEFINED, /* 141 */
149 TPM_UNDEFINED, /* 142 */
151 TPM_MEDIUM, /* 144 */
152 TPM_UNDEFINED, /* 145 */
153 TPM_UNDEFINED, /* 146 */
154 TPM_UNDEFINED, /* 147 */
155 TPM_UNDEFINED, /* 148 */
156 TPM_UNDEFINED, /* 149 */
157 TPM_UNDEFINED, /* 14a */
158 TPM_UNDEFINED, /* 14b */
159 TPM_UNDEFINED, /* 14c */
160 TPM_UNDEFINED, /* 14d */
162 TPM_UNDEFINED, /* 14f */
163 TPM_UNDEFINED, /* 150 */
164 TPM_UNDEFINED, /* 151 */
165 TPM_UNDEFINED, /* 152 */
166 TPM_UNDEFINED, /* 153 */
167 TPM_UNDEFINED, /* 154 */
168 TPM_UNDEFINED, /* 155 */
169 TPM_UNDEFINED, /* 156 */
170 TPM_UNDEFINED, /* 157 */
171 TPM_UNDEFINED, /* 158 */
172 TPM_UNDEFINED, /* 159 */
173 TPM_UNDEFINED, /* 15a */
174 TPM_UNDEFINED, /* 15b */
175 TPM_MEDIUM, /* 15c */
176 TPM_UNDEFINED, /* 15d */
177 TPM_UNDEFINED, /* 15e */
178 TPM_UNDEFINED, /* 15f */
179 TPM_UNDEFINED, /* 160 */
180 TPM_UNDEFINED, /* 161 */
181 TPM_UNDEFINED, /* 162 */
182 TPM_UNDEFINED, /* 163 */
183 TPM_UNDEFINED, /* 164 */
184 TPM_UNDEFINED, /* 165 */
185 TPM_UNDEFINED, /* 166 */
186 TPM_UNDEFINED, /* 167 */
187 TPM_UNDEFINED, /* 168 */
188 TPM_UNDEFINED, /* 169 */
189 TPM_UNDEFINED, /* 16a */
190 TPM_UNDEFINED, /* 16b */
191 TPM_UNDEFINED, /* 16c */
192 TPM_UNDEFINED, /* 16d */
193 TPM_UNDEFINED, /* 16e */
194 TPM_UNDEFINED, /* 16f */
195 TPM_UNDEFINED, /* 170 */
196 TPM_UNDEFINED, /* 171 */
197 TPM_UNDEFINED, /* 172 */
198 TPM_UNDEFINED, /* 173 */
199 TPM_UNDEFINED, /* 174 */
200 TPM_UNDEFINED, /* 175 */
201 TPM_UNDEFINED, /* 176 */
203 TPM_UNDEFINED, /* 178 */
204 TPM_UNDEFINED, /* 179 */
205 TPM_MEDIUM, /* 17a */
207 TPM_UNDEFINED, /* 17c */
208 TPM_UNDEFINED, /* 17d */
209 TPM_UNDEFINED, /* 17e */
210 TPM_UNDEFINED, /* 17f */
211 TPM_UNDEFINED, /* 180 */
212 TPM_UNDEFINED, /* 181 */
213 TPM_MEDIUM, /* 182 */
214 TPM_UNDEFINED, /* 183 */
215 TPM_UNDEFINED, /* 184 */
216 TPM_MEDIUM, /* 185 */
217 TPM_MEDIUM, /* 186 */
218 TPM_UNDEFINED, /* 187 */
219 TPM_UNDEFINED, /* 188 */
220 TPM_UNDEFINED, /* 189 */
221 TPM_UNDEFINED, /* 18a */
222 TPM_UNDEFINED, /* 18b */
223 TPM_UNDEFINED, /* 18c */
224 TPM_UNDEFINED, /* 18d */
225 TPM_UNDEFINED, /* 18e */
226 TPM_UNDEFINED /* 18f */
229 #define TPM2_PCR_READ_IN_SIZE \
230 (sizeof(struct tpm_input_header) + \
231 sizeof(struct tpm2_pcr_read_in))
233 static const struct tpm_input_header tpm2_pcrread_header = {
234 .tag = cpu_to_be16(TPM2_ST_NO_SESSIONS),
235 .length = cpu_to_be32(TPM2_PCR_READ_IN_SIZE),
236 .ordinal = cpu_to_be32(TPM2_CC_PCR_READ)
240 * tpm2_pcr_read() - read a PCR value
241 * @chip: TPM chip to use.
242 * @pcr_idx: index of the PCR to read.
243 * @ref_buf: buffer to store the resulting hash,
245 * 0 is returned when the operation is successful. If a negative number is
246 * returned it remarks a POSIX error code. If a positive number is returned
247 * it remarks a TPM error.
249 int tpm2_pcr_read(struct tpm_chip *chip, int pcr_idx, u8 *res_buf)
255 if (pcr_idx >= TPM2_PLATFORM_PCR)
258 cmd.header.in = tpm2_pcrread_header;
259 cmd.params.pcrread_in.pcr_selects_cnt = cpu_to_be32(1);
260 cmd.params.pcrread_in.hash_alg = cpu_to_be16(TPM2_ALG_SHA1);
261 cmd.params.pcrread_in.pcr_select_size = TPM2_PCR_SELECT_MIN;
263 memset(cmd.params.pcrread_in.pcr_select, 0,
264 sizeof(cmd.params.pcrread_in.pcr_select));
265 cmd.params.pcrread_in.pcr_select[pcr_idx >> 3] = 1 << (pcr_idx & 0x7);
267 rc = tpm_transmit_cmd(chip, &cmd, sizeof(cmd),
268 "attempting to read a pcr value");
270 buf = cmd.params.pcrread_out.digest;
271 memcpy(res_buf, buf, TPM_DIGEST_SIZE);
277 #define TPM2_GET_PCREXTEND_IN_SIZE \
278 (sizeof(struct tpm_input_header) + \
279 sizeof(struct tpm2_pcr_extend_in))
281 static const struct tpm_input_header tpm2_pcrextend_header = {
282 .tag = cpu_to_be16(TPM2_ST_SESSIONS),
283 .length = cpu_to_be32(TPM2_GET_PCREXTEND_IN_SIZE),
284 .ordinal = cpu_to_be32(TPM2_CC_PCR_EXTEND)
288 * tpm2_pcr_extend() - extend a PCR value
289 * @chip: TPM chip to use.
290 * @pcr_idx: index of the PCR.
291 * @hash: hash value to use for the extend operation.
293 * 0 is returned when the operation is successful. If a negative number is
294 * returned it remarks a POSIX error code. If a positive number is returned
295 * it remarks a TPM error.
297 int tpm2_pcr_extend(struct tpm_chip *chip, int pcr_idx, const u8 *hash)
302 cmd.header.in = tpm2_pcrextend_header;
303 cmd.params.pcrextend_in.pcr_idx = cpu_to_be32(pcr_idx);
304 cmd.params.pcrextend_in.auth_area_size =
305 cpu_to_be32(sizeof(struct tpm2_null_auth_area));
306 cmd.params.pcrextend_in.auth_area.handle =
307 cpu_to_be32(TPM2_RS_PW);
308 cmd.params.pcrextend_in.auth_area.nonce_size = 0;
309 cmd.params.pcrextend_in.auth_area.attributes = 0;
310 cmd.params.pcrextend_in.auth_area.auth_size = 0;
311 cmd.params.pcrextend_in.digest_cnt = cpu_to_be32(1);
312 cmd.params.pcrextend_in.hash_alg = cpu_to_be16(TPM2_ALG_SHA1);
313 memcpy(cmd.params.pcrextend_in.digest, hash, TPM_DIGEST_SIZE);
315 rc = tpm_transmit_cmd(chip, &cmd, sizeof(cmd),
316 "attempting extend a PCR value");
321 #define TPM2_GETRANDOM_IN_SIZE \
322 (sizeof(struct tpm_input_header) + \
323 sizeof(struct tpm2_get_random_in))
325 static const struct tpm_input_header tpm2_getrandom_header = {
326 .tag = cpu_to_be16(TPM2_ST_NO_SESSIONS),
327 .length = cpu_to_be32(TPM2_GETRANDOM_IN_SIZE),
328 .ordinal = cpu_to_be32(TPM2_CC_GET_RANDOM)
332 * tpm2_get_random() - get random bytes from the TPM RNG
333 * @chip: TPM chip to use
334 * @out: destination buffer for the random bytes
335 * @max: the max number of bytes to write to @out
337 * 0 is returned when the operation is successful. If a negative number is
338 * returned it remarks a POSIX error code. If a positive number is returned
339 * it remarks a TPM error.
341 int tpm2_get_random(struct tpm_chip *chip, u8 *out, size_t max)
351 num_bytes = min_t(u32, max, sizeof(cmd.params.getrandom_out.buffer));
353 if (!out || !num_bytes ||
354 max > sizeof(cmd.params.getrandom_out.buffer))
358 cmd.header.in = tpm2_getrandom_header;
359 cmd.params.getrandom_in.size = cpu_to_be16(num_bytes);
361 err = tpm_transmit_cmd(chip, &cmd, sizeof(cmd),
362 "attempting get random");
366 recd = min_t(u32, be16_to_cpu(cmd.params.getrandom_out.size),
368 memcpy(dest, cmd.params.getrandom_out.buffer, recd);
373 } while (retries-- && total < max);
375 return total ? total : -EIO;
378 #define TPM2_GET_TPM_PT_IN_SIZE \
379 (sizeof(struct tpm_input_header) + \
380 sizeof(struct tpm2_get_tpm_pt_in))
382 static const struct tpm_input_header tpm2_get_tpm_pt_header = {
383 .tag = cpu_to_be16(TPM2_ST_NO_SESSIONS),
384 .length = cpu_to_be32(TPM2_GET_TPM_PT_IN_SIZE),
385 .ordinal = cpu_to_be32(TPM2_CC_GET_CAPABILITY)
389 * Append TPMS_AUTH_COMMAND to the buffer. The buffer must be allocated with
392 * @param buf: an allocated tpm_buf instance
393 * @param nonce: the session nonce, may be NULL if not used
394 * @param nonce_len: the session nonce length, may be 0 if not used
395 * @param attributes: the session attributes
396 * @param hmac: the session HMAC or password, may be NULL if not used
397 * @param hmac_len: the session HMAC or password length, maybe 0 if not used
399 static void tpm2_buf_append_auth(struct tpm_buf *buf, u32 session_handle,
400 const u8 *nonce, u16 nonce_len,
402 const u8 *hmac, u16 hmac_len)
404 tpm_buf_append_u32(buf, 9 + nonce_len + hmac_len);
405 tpm_buf_append_u32(buf, session_handle);
406 tpm_buf_append_u16(buf, nonce_len);
408 if (nonce && nonce_len)
409 tpm_buf_append(buf, nonce, nonce_len);
411 tpm_buf_append_u8(buf, attributes);
412 tpm_buf_append_u16(buf, hmac_len);
414 if (hmac && hmac_len)
415 tpm_buf_append(buf, hmac, hmac_len);
419 * tpm2_seal_trusted() - seal a trusted key
420 * @chip_num: A specific chip number for the request or TPM_ANY_NUM
421 * @options: authentication values and other options
422 * @payload: the key data in clear and encrypted form
424 * Returns < 0 on error and 0 on success.
426 int tpm2_seal_trusted(struct tpm_chip *chip,
427 struct trusted_key_payload *payload,
428 struct trusted_key_options *options)
430 unsigned int blob_len;
434 rc = tpm_buf_init(&buf, TPM2_ST_SESSIONS, TPM2_CC_CREATE);
438 tpm_buf_append_u32(&buf, options->keyhandle);
439 tpm2_buf_append_auth(&buf, TPM2_RS_PW,
441 0 /* session_attributes */,
442 options->keyauth /* hmac */,
446 tpm_buf_append_u16(&buf, 4 + TPM_DIGEST_SIZE + payload->key_len);
448 tpm_buf_append_u16(&buf, TPM_DIGEST_SIZE);
449 tpm_buf_append(&buf, options->blobauth, TPM_DIGEST_SIZE);
450 tpm_buf_append_u16(&buf, payload->key_len);
451 tpm_buf_append(&buf, payload->key, payload->key_len);
454 tpm_buf_append_u16(&buf, 14);
456 tpm_buf_append_u16(&buf, TPM2_ALG_KEYEDHASH);
457 tpm_buf_append_u16(&buf, TPM2_ALG_SHA256);
458 tpm_buf_append_u32(&buf, TPM2_ATTR_USER_WITH_AUTH);
459 tpm_buf_append_u16(&buf, 0); /* policy digest size */
460 tpm_buf_append_u16(&buf, TPM2_ALG_NULL);
461 tpm_buf_append_u16(&buf, 0);
464 tpm_buf_append_u16(&buf, 0);
467 tpm_buf_append_u32(&buf, 0);
469 if (buf.flags & TPM_BUF_OVERFLOW) {
474 rc = tpm_transmit_cmd(chip, buf.data, PAGE_SIZE, "sealing data");
478 blob_len = be32_to_cpup((__be32 *) &buf.data[TPM_HEADER_SIZE]);
479 if (blob_len > MAX_BLOB_SIZE) {
484 memcpy(payload->blob, &buf.data[TPM_HEADER_SIZE + 4], blob_len);
485 payload->blob_len = blob_len;
488 tpm_buf_destroy(&buf);
496 static int tpm2_load(struct tpm_chip *chip,
497 struct trusted_key_payload *payload,
498 struct trusted_key_options *options,
502 unsigned int private_len;
503 unsigned int public_len;
504 unsigned int blob_len;
507 private_len = be16_to_cpup((__be16 *) &payload->blob[0]);
508 if (private_len > (payload->blob_len - 2))
511 public_len = be16_to_cpup((__be16 *) &payload->blob[2 + private_len]);
512 blob_len = private_len + public_len + 4;
513 if (blob_len > payload->blob_len)
516 rc = tpm_buf_init(&buf, TPM2_ST_SESSIONS, TPM2_CC_LOAD);
520 tpm_buf_append_u32(&buf, options->keyhandle);
521 tpm2_buf_append_auth(&buf, TPM2_RS_PW,
523 0 /* session_attributes */,
524 options->keyauth /* hmac */,
527 tpm_buf_append(&buf, payload->blob, blob_len);
529 if (buf.flags & TPM_BUF_OVERFLOW) {
534 rc = tpm_transmit_cmd(chip, buf.data, PAGE_SIZE, "loading blob");
536 *blob_handle = be32_to_cpup(
537 (__be32 *) &buf.data[TPM_HEADER_SIZE]);
540 tpm_buf_destroy(&buf);
548 static void tpm2_flush_context(struct tpm_chip *chip, u32 handle)
553 rc = tpm_buf_init(&buf, TPM2_ST_NO_SESSIONS, TPM2_CC_FLUSH_CONTEXT);
555 dev_warn(chip->pdev, "0x%08x was not flushed, out of memory\n",
560 tpm_buf_append_u32(&buf, handle);
562 rc = tpm_transmit_cmd(chip, buf.data, PAGE_SIZE, "flushing context");
564 dev_warn(chip->pdev, "0x%08x was not flushed, rc=%d\n", handle,
567 tpm_buf_destroy(&buf);
570 static int tpm2_unseal(struct tpm_chip *chip,
571 struct trusted_key_payload *payload,
572 struct trusted_key_options *options,
578 rc = tpm_buf_init(&buf, TPM2_ST_SESSIONS, TPM2_CC_UNSEAL);
582 tpm_buf_append_u32(&buf, blob_handle);
583 tpm2_buf_append_auth(&buf, TPM2_RS_PW,
585 0 /* session_attributes */,
586 options->blobauth /* hmac */,
589 rc = tpm_transmit_cmd(chip, buf.data, PAGE_SIZE, "unsealing");
594 payload->key_len = be16_to_cpup(
595 (__be16 *) &buf.data[TPM_HEADER_SIZE + 4]);
597 memcpy(payload->key, &buf.data[TPM_HEADER_SIZE + 6],
601 tpm_buf_destroy(&buf);
606 * tpm_unseal_trusted() - unseal a trusted key
607 * @chip_num: A specific chip number for the request or TPM_ANY_NUM
608 * @options: authentication values and other options
609 * @payload: the key data in clear and encrypted form
611 * Returns < 0 on error and 0 on success.
613 int tpm2_unseal_trusted(struct tpm_chip *chip,
614 struct trusted_key_payload *payload,
615 struct trusted_key_options *options)
620 rc = tpm2_load(chip, payload, options, &blob_handle);
624 rc = tpm2_unseal(chip, payload, options, blob_handle);
626 tpm2_flush_context(chip, blob_handle);
632 * tpm2_get_tpm_pt() - get value of a TPM_CAP_TPM_PROPERTIES type property
633 * @chip: TPM chip to use.
634 * @property_id: property ID.
635 * @value: output variable.
636 * @desc: passed to tpm_transmit_cmd()
638 * 0 is returned when the operation is successful. If a negative number is
639 * returned it remarks a POSIX error code. If a positive number is returned
640 * it remarks a TPM error.
642 ssize_t tpm2_get_tpm_pt(struct tpm_chip *chip, u32 property_id, u32 *value,
648 cmd.header.in = tpm2_get_tpm_pt_header;
649 cmd.params.get_tpm_pt_in.cap_id = cpu_to_be32(TPM2_CAP_TPM_PROPERTIES);
650 cmd.params.get_tpm_pt_in.property_id = cpu_to_be32(property_id);
651 cmd.params.get_tpm_pt_in.property_cnt = cpu_to_be32(1);
653 rc = tpm_transmit_cmd(chip, &cmd, sizeof(cmd), desc);
655 *value = cmd.params.get_tpm_pt_out.value;
660 #define TPM2_STARTUP_IN_SIZE \
661 (sizeof(struct tpm_input_header) + \
662 sizeof(struct tpm2_startup_in))
664 static const struct tpm_input_header tpm2_startup_header = {
665 .tag = cpu_to_be16(TPM2_ST_NO_SESSIONS),
666 .length = cpu_to_be32(TPM2_STARTUP_IN_SIZE),
667 .ordinal = cpu_to_be32(TPM2_CC_STARTUP)
671 * tpm2_startup() - send startup command to the TPM chip
672 * @chip: TPM chip to use.
673 * @startup_type startup type. The value is either
674 * TPM_SU_CLEAR or TPM_SU_STATE.
676 * 0 is returned when the operation is successful. If a negative number is
677 * returned it remarks a POSIX error code. If a positive number is returned
678 * it remarks a TPM error.
680 int tpm2_startup(struct tpm_chip *chip, u16 startup_type)
684 cmd.header.in = tpm2_startup_header;
686 cmd.params.startup_in.startup_type = cpu_to_be16(startup_type);
687 return tpm_transmit_cmd(chip, &cmd, sizeof(cmd),
688 "attempting to start the TPM");
690 EXPORT_SYMBOL_GPL(tpm2_startup);
692 #define TPM2_SHUTDOWN_IN_SIZE \
693 (sizeof(struct tpm_input_header) + \
694 sizeof(struct tpm2_startup_in))
696 static const struct tpm_input_header tpm2_shutdown_header = {
697 .tag = cpu_to_be16(TPM2_ST_NO_SESSIONS),
698 .length = cpu_to_be32(TPM2_SHUTDOWN_IN_SIZE),
699 .ordinal = cpu_to_be32(TPM2_CC_SHUTDOWN)
703 * tpm2_shutdown() - send shutdown command to the TPM chip
704 * @chip: TPM chip to use.
705 * @shutdown_type shutdown type. The value is either
706 * TPM_SU_CLEAR or TPM_SU_STATE.
708 void tpm2_shutdown(struct tpm_chip *chip, u16 shutdown_type)
713 cmd.header.in = tpm2_shutdown_header;
714 cmd.params.startup_in.startup_type = cpu_to_be16(shutdown_type);
716 rc = tpm_transmit_cmd(chip, &cmd, sizeof(cmd), "stopping the TPM");
718 /* In places where shutdown command is sent there's no much we can do
719 * except print the error code on a system failure.
722 dev_warn(chip->pdev, "transmit returned %d while stopping the TPM",
725 EXPORT_SYMBOL_GPL(tpm2_shutdown);
728 * tpm2_calc_ordinal_duration() - maximum duration for a command
729 * @chip: TPM chip to use.
730 * @ordinal: command code number.
732 * 0 is returned when the operation is successful. If a negative number is
733 * returned it remarks a POSIX error code. If a positive number is returned
734 * it remarks a TPM error.
736 unsigned long tpm2_calc_ordinal_duration(struct tpm_chip *chip, u32 ordinal)
738 int index = TPM_UNDEFINED;
741 if (ordinal >= TPM2_CC_FIRST && ordinal <= TPM2_CC_LAST)
742 index = tpm2_ordinal_duration[ordinal - TPM2_CC_FIRST];
744 if (index != TPM_UNDEFINED)
745 duration = chip->vendor.duration[index];
748 duration = 2 * 60 * HZ;
752 EXPORT_SYMBOL_GPL(tpm2_calc_ordinal_duration);
754 #define TPM2_SELF_TEST_IN_SIZE \
755 (sizeof(struct tpm_input_header) + \
756 sizeof(struct tpm2_self_test_in))
758 static const struct tpm_input_header tpm2_selftest_header = {
759 .tag = cpu_to_be16(TPM2_ST_NO_SESSIONS),
760 .length = cpu_to_be32(TPM2_SELF_TEST_IN_SIZE),
761 .ordinal = cpu_to_be32(TPM2_CC_SELF_TEST)
765 * tpm2_continue_selftest() - start a self test
766 * @chip: TPM chip to use
767 * @full: test all commands instead of testing only those that were not
770 * 0 is returned when the operation is successful. If a negative number is
771 * returned it remarks a POSIX error code. If a positive number is returned
772 * it remarks a TPM error.
774 static int tpm2_start_selftest(struct tpm_chip *chip, bool full)
779 cmd.header.in = tpm2_selftest_header;
780 cmd.params.selftest_in.full_test = full;
782 rc = tpm_transmit_cmd(chip, &cmd, TPM2_SELF_TEST_IN_SIZE,
783 "continue selftest");
785 /* At least some prototype chips seem to give RC_TESTING error
786 * immediately. This is a workaround for that.
788 if (rc == TPM2_RC_TESTING) {
789 dev_warn(chip->pdev, "Got RC_TESTING, ignoring\n");
797 * tpm2_do_selftest() - run a full self test
798 * @chip: TPM chip to use
800 * During the self test TPM2 commands return with the error code RC_TESTING.
801 * Waiting is done by issuing PCR read until it executes successfully.
803 * 0 is returned when the operation is successful. If a negative number is
804 * returned it remarks a POSIX error code. If a positive number is returned
805 * it remarks a TPM error.
807 int tpm2_do_selftest(struct tpm_chip *chip)
811 unsigned int delay_msec = 100;
812 unsigned long duration;
816 duration = tpm2_calc_ordinal_duration(chip, TPM2_CC_SELF_TEST);
818 loops = jiffies_to_msecs(duration) / delay_msec;
820 rc = tpm2_start_selftest(chip, true);
824 for (i = 0; i < loops; i++) {
825 /* Attempt to read a PCR value */
826 cmd.header.in = tpm2_pcrread_header;
827 cmd.params.pcrread_in.pcr_selects_cnt = cpu_to_be32(1);
828 cmd.params.pcrread_in.hash_alg = cpu_to_be16(TPM2_ALG_SHA1);
829 cmd.params.pcrread_in.pcr_select_size = TPM2_PCR_SELECT_MIN;
830 cmd.params.pcrread_in.pcr_select[0] = 0x01;
831 cmd.params.pcrread_in.pcr_select[1] = 0x00;
832 cmd.params.pcrread_in.pcr_select[2] = 0x00;
834 rc = tpm_transmit_cmd(chip, (u8 *) &cmd, sizeof(cmd), NULL);
838 rc = be32_to_cpu(cmd.header.out.return_code);
839 if (rc != TPM2_RC_TESTING)
847 EXPORT_SYMBOL_GPL(tpm2_do_selftest);
850 * tpm2_gen_interrupt() - generate an interrupt
851 * @chip: TPM chip to use
853 * 0 is returned when the operation is successful. If a negative number is
854 * returned it remarks a POSIX error code. If a positive number is returned
855 * it remarks a TPM error.
857 int tpm2_gen_interrupt(struct tpm_chip *chip)
861 return tpm2_get_tpm_pt(chip, 0x100, &dummy,
862 "attempting to generate an interrupt");
864 EXPORT_SYMBOL_GPL(tpm2_gen_interrupt);
867 * tpm2_probe() - probe TPM 2.0
868 * @chip: TPM chip to use
870 * Send idempotent TPM 2.0 command and see whether TPM 2.0 chip replied based on
873 int tpm2_probe(struct tpm_chip *chip)
878 cmd.header.in = tpm2_get_tpm_pt_header;
879 cmd.params.get_tpm_pt_in.cap_id = cpu_to_be32(TPM2_CAP_TPM_PROPERTIES);
880 cmd.params.get_tpm_pt_in.property_id = cpu_to_be32(0x100);
881 cmd.params.get_tpm_pt_in.property_cnt = cpu_to_be32(1);
883 rc = tpm_transmit(chip, (const char *) &cmd, sizeof(cmd));
886 else if (rc < TPM_HEADER_SIZE)
889 if (be16_to_cpu(cmd.header.out.tag) == TPM2_ST_NO_SESSIONS)
890 chip->flags |= TPM_CHIP_FLAG_TPM2;
894 EXPORT_SYMBOL_GPL(tpm2_probe);