2 * Copyright (C) 2004 IBM Corporation
3 * Copyright (C) 2014 Intel Corporation
6 * Leendert van Doorn <leendert@watson.ibm.com>
7 * Dave Safford <safford@watson.ibm.com>
8 * Reiner Sailer <sailer@watson.ibm.com>
9 * Kylene Hall <kjhall@us.ibm.com>
11 * Maintained by: <tpmdd-devel@lists.sourceforge.net>
13 * Device driver for TCG/TCPA TPM (trusted platform module).
14 * Specifications at www.trustedcomputinggroup.org
16 * This program is free software; you can redistribute it and/or
17 * modify it under the terms of the GNU General Public License as
18 * published by the Free Software Foundation, version 2 of the
21 * Note, the TPM chip is not interrupt driven (only polling)
22 * and can have very long timeouts (minutes!). Hence the unusual
27 #include <linux/poll.h>
28 #include <linux/slab.h>
29 #include <linux/mutex.h>
30 #include <linux/spinlock.h>
31 #include <linux/freezer.h>
32 #include <linux/tpm_eventlog.h>
37 * Bug workaround - some TPM's don't flush the most
38 * recently changed pcr on suspend, so force the flush
39 * with an extend to the selected _unused_ non-volatile pcr.
41 static u32 tpm_suspend_pcr;
42 module_param_named(suspend_pcr, tpm_suspend_pcr, uint, 0644);
43 MODULE_PARM_DESC(suspend_pcr,
44 "PCR to use for dummy writes to facilitate flush on suspend.");
47 * tpm_calc_ordinal_duration() - calculate the maximum command duration
48 * @chip: TPM chip to use.
49 * @ordinal: TPM command ordinal.
51 * The function returns the maximum amount of time the chip could take
52 * to return the result for a particular ordinal in jiffies.
54 * Return: A maximal duration time for an ordinal in jiffies.
56 unsigned long tpm_calc_ordinal_duration(struct tpm_chip *chip, u32 ordinal)
58 if (chip->flags & TPM_CHIP_FLAG_TPM2)
59 return tpm2_calc_ordinal_duration(chip, ordinal);
61 return tpm1_calc_ordinal_duration(chip, ordinal);
63 EXPORT_SYMBOL_GPL(tpm_calc_ordinal_duration);
65 static int tpm_validate_command(struct tpm_chip *chip,
66 struct tpm_space *space,
70 const struct tpm_input_header *header = (const void *)cmd;
74 unsigned int nr_handles;
76 if (len < TPM_HEADER_SIZE)
82 if (chip->flags & TPM_CHIP_FLAG_TPM2 && chip->nr_commands) {
83 cc = be32_to_cpu(header->ordinal);
85 i = tpm2_find_cc(chip, cc);
87 dev_dbg(&chip->dev, "0x%04X is an invalid command\n",
92 attrs = chip->cc_attrs_tbl[i];
94 4 * ((attrs >> TPM2_CC_ATTR_CHANDLES) & GENMASK(2, 0));
95 if (len < TPM_HEADER_SIZE + 4 * nr_handles)
102 "%s: insufficient command length %zu", __func__, len);
106 static int tpm_request_locality(struct tpm_chip *chip, unsigned int flags)
110 if (flags & TPM_TRANSMIT_NESTED)
113 if (!chip->ops->request_locality)
116 rc = chip->ops->request_locality(chip, 0);
125 static void tpm_relinquish_locality(struct tpm_chip *chip, unsigned int flags)
129 if (flags & TPM_TRANSMIT_NESTED)
132 if (!chip->ops->relinquish_locality)
135 rc = chip->ops->relinquish_locality(chip, chip->locality);
137 dev_err(&chip->dev, "%s: : error %d\n", __func__, rc);
142 static int tpm_cmd_ready(struct tpm_chip *chip, unsigned int flags)
144 if (flags & TPM_TRANSMIT_NESTED)
147 if (!chip->ops->cmd_ready)
150 return chip->ops->cmd_ready(chip);
153 static int tpm_go_idle(struct tpm_chip *chip, unsigned int flags)
155 if (flags & TPM_TRANSMIT_NESTED)
158 if (!chip->ops->go_idle)
161 return chip->ops->go_idle(chip);
164 static ssize_t tpm_try_transmit(struct tpm_chip *chip,
165 struct tpm_space *space,
166 u8 *buf, size_t bufsiz,
169 struct tpm_output_header *header = (void *)buf;
176 rc = tpm_validate_command(chip, space, buf, bufsiz);
180 * If the command is not implemented by the TPM, synthesize a
181 * response with a TPM2_RC_COMMAND_CODE return for user-space.
183 if (rc == -EOPNOTSUPP) {
184 header->length = cpu_to_be32(sizeof(*header));
185 header->tag = cpu_to_be16(TPM2_ST_NO_SESSIONS);
186 header->return_code = cpu_to_be32(TPM2_RC_COMMAND_CODE |
187 TSS2_RESMGR_TPM_RC_LAYER);
188 return sizeof(*header);
191 if (bufsiz > TPM_BUFSIZE)
192 bufsiz = TPM_BUFSIZE;
194 count = be32_to_cpu(*((__be32 *) (buf + 2)));
195 ordinal = be32_to_cpu(*((__be32 *) (buf + 6)));
198 if (count > bufsiz) {
200 "invalid count value %x %zx\n", count, bufsiz);
204 if (!(flags & TPM_TRANSMIT_UNLOCKED) && !(flags & TPM_TRANSMIT_NESTED))
205 mutex_lock(&chip->tpm_mutex);
207 if (chip->ops->clk_enable != NULL)
208 chip->ops->clk_enable(chip, true);
210 /* Store the decision as chip->locality will be changed. */
211 need_locality = chip->locality == -1;
214 rc = tpm_request_locality(chip, flags);
216 need_locality = false;
221 rc = tpm_cmd_ready(chip, flags);
225 rc = tpm2_prepare_space(chip, space, ordinal, buf);
229 rc = chip->ops->send(chip, buf, count);
233 "%s: send(): error %d\n", __func__, rc);
237 /* A sanity check. send() should just return zero on success e.g.
238 * not the command length.
242 "%s: send(): invalid value %d\n", __func__, rc);
246 if (chip->flags & TPM_CHIP_FLAG_IRQ)
249 stop = jiffies + tpm_calc_ordinal_duration(chip, ordinal);
251 u8 status = chip->ops->status(chip);
252 if ((status & chip->ops->req_complete_mask) ==
253 chip->ops->req_complete_val)
256 if (chip->ops->req_canceled(chip, status)) {
257 dev_err(&chip->dev, "Operation Canceled\n");
262 tpm_msleep(TPM_TIMEOUT_POLL);
264 } while (time_before(jiffies, stop));
266 chip->ops->cancel(chip);
267 dev_err(&chip->dev, "Operation Timed out\n");
272 len = chip->ops->recv(chip, buf, bufsiz);
276 "tpm_transmit: tpm_recv: error %d\n", rc);
278 } else if (len < TPM_HEADER_SIZE) {
283 if (len != be32_to_cpu(header->length)) {
288 rc = tpm2_commit_space(chip, space, ordinal, buf, &len);
290 dev_err(&chip->dev, "tpm2_commit_space: error %d\n", rc);
293 /* may fail but do not override previous error value in rc */
294 tpm_go_idle(chip, flags);
298 tpm_relinquish_locality(chip, flags);
300 if (chip->ops->clk_enable != NULL)
301 chip->ops->clk_enable(chip, false);
303 if (!(flags & TPM_TRANSMIT_UNLOCKED) && !(flags & TPM_TRANSMIT_NESTED))
304 mutex_unlock(&chip->tpm_mutex);
305 return rc ? rc : len;
309 * tpm_transmit - Internal kernel interface to transmit TPM commands.
310 * @chip: a TPM chip to use
311 * @space: a TPM space
312 * @buf: a TPM command buffer
313 * @bufsiz: length of the TPM command buffer
314 * @flags: TPM transmit flags
316 * A wrapper around tpm_try_transmit() that handles TPM2_RC_RETRY returns from
317 * the TPM and retransmits the command after a delay up to a maximum wait of
318 * TPM2_DURATION_LONG.
320 * Note that TPM 1.x never returns TPM2_RC_RETRY so the retry logic is TPM 2.0
324 * * The response length - OK
325 * * -errno - A system error
327 ssize_t tpm_transmit(struct tpm_chip *chip, struct tpm_space *space,
328 u8 *buf, size_t bufsiz, unsigned int flags)
330 struct tpm_output_header *header = (struct tpm_output_header *)buf;
331 /* space for header and handles */
332 u8 save[TPM_HEADER_SIZE + 3*sizeof(u32)];
333 unsigned int delay_msec = TPM2_DURATION_SHORT;
336 const size_t save_size = min(space ? sizeof(save) : TPM_HEADER_SIZE,
338 /* the command code is where the return code will be */
339 u32 cc = be32_to_cpu(header->return_code);
342 * Subtlety here: if we have a space, the handles will be
343 * transformed, so when we restore the header we also have to
344 * restore the handles.
346 memcpy(save, buf, save_size);
349 ret = tpm_try_transmit(chip, space, buf, bufsiz, flags);
352 rc = be32_to_cpu(header->return_code);
353 if (rc != TPM2_RC_RETRY && rc != TPM2_RC_TESTING)
356 * return immediately if self test returns test
357 * still running to shorten boot time.
359 if (rc == TPM2_RC_TESTING && cc == TPM2_CC_SELF_TEST)
362 if (delay_msec > TPM2_DURATION_LONG) {
363 if (rc == TPM2_RC_RETRY)
364 dev_err(&chip->dev, "in retry loop\n");
367 "self test is still running\n");
370 tpm_msleep(delay_msec);
372 memcpy(buf, save, save_size);
378 * tpm_transmit_cmd - send a tpm command to the device
379 * @chip: a TPM chip to use
380 * @space: a TPM space
381 * @buf: a TPM command buffer
382 * @min_rsp_body_length: minimum expected length of response body
383 * @flags: TPM transmit flags
384 * @desc: command description used in the error message
388 * * -errno - A system error
389 * * TPM_RC - A TPM error
391 ssize_t tpm_transmit_cmd(struct tpm_chip *chip, struct tpm_space *space,
392 struct tpm_buf *buf, size_t min_rsp_body_length,
393 unsigned int flags, const char *desc)
395 const struct tpm_output_header *header =
396 (struct tpm_output_header *)buf->data;
400 len = tpm_transmit(chip, space, buf->data, PAGE_SIZE, flags);
404 err = be32_to_cpu(header->return_code);
405 if (err != 0 && err != TPM_ERR_DISABLED && err != TPM_ERR_DEACTIVATED
406 && err != TPM2_RC_TESTING && desc)
407 dev_err(&chip->dev, "A TPM error (%d) occurred %s\n", err,
412 if (len < min_rsp_body_length + TPM_HEADER_SIZE)
417 EXPORT_SYMBOL_GPL(tpm_transmit_cmd);
419 int tpm_get_timeouts(struct tpm_chip *chip)
421 if (chip->flags & TPM_CHIP_FLAG_HAVE_TIMEOUTS)
424 if (chip->flags & TPM_CHIP_FLAG_TPM2)
425 return tpm2_get_timeouts(chip);
427 return tpm1_get_timeouts(chip);
429 EXPORT_SYMBOL_GPL(tpm_get_timeouts);
432 * tpm_is_tpm2 - do we a have a TPM2 chip?
433 * @chip: a &struct tpm_chip instance, %NULL for the default chip
436 * 1 if we have a TPM2 chip.
437 * 0 if we don't have a TPM2 chip.
438 * A negative number for system errors (errno).
440 int tpm_is_tpm2(struct tpm_chip *chip)
444 chip = tpm_find_get_ops(chip);
448 rc = (chip->flags & TPM_CHIP_FLAG_TPM2) != 0;
454 EXPORT_SYMBOL_GPL(tpm_is_tpm2);
457 * tpm_pcr_read - read a PCR value from SHA1 bank
458 * @chip: a &struct tpm_chip instance, %NULL for the default chip
459 * @pcr_idx: the PCR to be retrieved
460 * @res_buf: the value of the PCR
462 * Return: same as with tpm_transmit_cmd()
464 int tpm_pcr_read(struct tpm_chip *chip, u32 pcr_idx, u8 *res_buf)
468 chip = tpm_find_get_ops(chip);
472 if (chip->flags & TPM_CHIP_FLAG_TPM2)
473 rc = tpm2_pcr_read(chip, pcr_idx, res_buf);
475 rc = tpm1_pcr_read(chip, pcr_idx, res_buf);
480 EXPORT_SYMBOL_GPL(tpm_pcr_read);
483 * tpm_pcr_extend - extend a PCR value in SHA1 bank.
484 * @chip: a &struct tpm_chip instance, %NULL for the default chip
485 * @pcr_idx: the PCR to be retrieved
486 * @hash: the hash value used to extend the PCR value
488 * Note: with TPM 2.0 extends also those banks with a known digest size to the
489 * cryto subsystem in order to prevent malicious use of those PCR banks. In the
490 * future we should dynamically determine digest sizes.
492 * Return: same as with tpm_transmit_cmd()
494 int tpm_pcr_extend(struct tpm_chip *chip, u32 pcr_idx, const u8 *hash)
497 struct tpm2_digest digest_list[ARRAY_SIZE(chip->active_banks)];
501 chip = tpm_find_get_ops(chip);
505 if (chip->flags & TPM_CHIP_FLAG_TPM2) {
506 memset(digest_list, 0, sizeof(digest_list));
508 for (i = 0; i < ARRAY_SIZE(chip->active_banks) &&
509 chip->active_banks[i] != TPM2_ALG_ERROR; i++) {
510 digest_list[i].alg_id = chip->active_banks[i];
511 memcpy(digest_list[i].digest, hash, TPM_DIGEST_SIZE);
515 rc = tpm2_pcr_extend(chip, pcr_idx, count, digest_list);
520 rc = tpm1_pcr_extend(chip, pcr_idx, hash,
521 "attempting extend a PCR value");
525 EXPORT_SYMBOL_GPL(tpm_pcr_extend);
528 * tpm_send - send a TPM command
529 * @chip: a &struct tpm_chip instance, %NULL for the default chip
530 * @cmd: a TPM command buffer
531 * @buflen: the length of the TPM command buffer
533 * Return: same as with tpm_transmit_cmd()
535 int tpm_send(struct tpm_chip *chip, void *cmd, size_t buflen)
540 chip = tpm_find_get_ops(chip);
544 rc = tpm_buf_init(&buf, 0, 0);
548 memcpy(buf.data, cmd, buflen);
549 rc = tpm_transmit_cmd(chip, NULL, &buf, 0, 0,
550 "attempting to a send a command");
551 tpm_buf_destroy(&buf);
556 EXPORT_SYMBOL_GPL(tpm_send);
558 int tpm_auto_startup(struct tpm_chip *chip)
562 if (!(chip->ops->flags & TPM_OPS_AUTO_STARTUP))
565 if (chip->flags & TPM_CHIP_FLAG_TPM2)
566 rc = tpm2_auto_startup(chip);
568 rc = tpm1_auto_startup(chip);
574 * We are about to suspend. Save the TPM state
575 * so that it can be restored.
577 int tpm_pm_suspend(struct device *dev)
579 struct tpm_chip *chip = dev_get_drvdata(dev);
585 if (chip->flags & TPM_CHIP_FLAG_ALWAYS_POWERED)
588 if (chip->flags & TPM_CHIP_FLAG_TPM2)
589 tpm2_shutdown(chip, TPM2_SU_STATE);
591 rc = tpm1_pm_suspend(chip, tpm_suspend_pcr);
595 EXPORT_SYMBOL_GPL(tpm_pm_suspend);
598 * Resume from a power safe. The BIOS already restored
601 int tpm_pm_resume(struct device *dev)
603 struct tpm_chip *chip = dev_get_drvdata(dev);
610 EXPORT_SYMBOL_GPL(tpm_pm_resume);
613 * tpm_get_random() - get random bytes from the TPM's RNG
614 * @chip: a &struct tpm_chip instance, %NULL for the default chip
615 * @out: destination buffer for the random bytes
616 * @max: the max number of bytes to write to @out
618 * Return: number of random bytes read or a negative error value.
620 int tpm_get_random(struct tpm_chip *chip, u8 *out, size_t max)
624 if (!out || max > TPM_MAX_RNG_DATA)
627 chip = tpm_find_get_ops(chip);
631 if (chip->flags & TPM_CHIP_FLAG_TPM2)
632 rc = tpm2_get_random(chip, out, max);
634 rc = tpm1_get_random(chip, out, max);
639 EXPORT_SYMBOL_GPL(tpm_get_random);
642 * tpm_seal_trusted() - seal a trusted key payload
643 * @chip: a &struct tpm_chip instance, %NULL for the default chip
644 * @options: authentication values and other options
645 * @payload: the key data in clear and encrypted form
647 * Note: only TPM 2.0 chip are supported. TPM 1.x implementation is located in
648 * the keyring subsystem.
650 * Return: same as with tpm_transmit_cmd()
652 int tpm_seal_trusted(struct tpm_chip *chip, struct trusted_key_payload *payload,
653 struct trusted_key_options *options)
657 chip = tpm_find_get_ops(chip);
658 if (!chip || !(chip->flags & TPM_CHIP_FLAG_TPM2))
661 rc = tpm2_seal_trusted(chip, payload, options);
666 EXPORT_SYMBOL_GPL(tpm_seal_trusted);
669 * tpm_unseal_trusted() - unseal a trusted key
670 * @chip: a &struct tpm_chip instance, %NULL for the default chip
671 * @options: authentication values and other options
672 * @payload: the key data in clear and encrypted form
674 * Note: only TPM 2.0 chip are supported. TPM 1.x implementation is located in
675 * the keyring subsystem.
677 * Return: same as with tpm_transmit_cmd()
679 int tpm_unseal_trusted(struct tpm_chip *chip,
680 struct trusted_key_payload *payload,
681 struct trusted_key_options *options)
685 chip = tpm_find_get_ops(chip);
686 if (!chip || !(chip->flags & TPM_CHIP_FLAG_TPM2))
689 rc = tpm2_unseal_trusted(chip, payload, options);
695 EXPORT_SYMBOL_GPL(tpm_unseal_trusted);
697 static int __init tpm_init(void)
701 tpm_class = class_create(THIS_MODULE, "tpm");
702 if (IS_ERR(tpm_class)) {
703 pr_err("couldn't create tpm class\n");
704 return PTR_ERR(tpm_class);
707 tpmrm_class = class_create(THIS_MODULE, "tpmrm");
708 if (IS_ERR(tpmrm_class)) {
709 pr_err("couldn't create tpmrm class\n");
710 rc = PTR_ERR(tpmrm_class);
711 goto out_destroy_tpm_class;
714 rc = alloc_chrdev_region(&tpm_devt, 0, 2*TPM_NUM_DEVICES, "tpm");
716 pr_err("tpm: failed to allocate char dev region\n");
717 goto out_destroy_tpmrm_class;
720 rc = tpm_dev_common_init();
722 pr_err("tpm: failed to allocate char dev region\n");
723 goto out_unreg_chrdev;
729 unregister_chrdev_region(tpm_devt, 2 * TPM_NUM_DEVICES);
730 out_destroy_tpmrm_class:
731 class_destroy(tpmrm_class);
732 out_destroy_tpm_class:
733 class_destroy(tpm_class);
738 static void __exit tpm_exit(void)
740 idr_destroy(&dev_nums_idr);
741 class_destroy(tpm_class);
742 class_destroy(tpmrm_class);
743 unregister_chrdev_region(tpm_devt, 2*TPM_NUM_DEVICES);
744 tpm_dev_common_exit();
747 subsys_initcall(tpm_init);
748 module_exit(tpm_exit);
750 MODULE_AUTHOR("Leendert van Doorn (leendert@watson.ibm.com)");
751 MODULE_DESCRIPTION("TPM Driver");
752 MODULE_VERSION("2.0");
753 MODULE_LICENSE("GPL");