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, struct tpm_space *space,
66 const void *cmd, size_t len)
68 const struct tpm_header *header = cmd;
72 unsigned int nr_handles;
74 if (len < TPM_HEADER_SIZE)
80 if (chip->flags & TPM_CHIP_FLAG_TPM2 && chip->nr_commands) {
81 cc = be32_to_cpu(header->ordinal);
83 i = tpm2_find_cc(chip, cc);
85 dev_dbg(&chip->dev, "0x%04X is an invalid command\n",
90 attrs = chip->cc_attrs_tbl[i];
92 4 * ((attrs >> TPM2_CC_ATTR_CHANDLES) & GENMASK(2, 0));
93 if (len < TPM_HEADER_SIZE + 4 * nr_handles)
100 "%s: insufficient command length %zu", __func__, len);
104 static int tpm_request_locality(struct tpm_chip *chip, unsigned int flags)
108 if (flags & TPM_TRANSMIT_NESTED)
111 if (!chip->ops->request_locality)
114 rc = chip->ops->request_locality(chip, 0);
123 static void tpm_relinquish_locality(struct tpm_chip *chip, unsigned int flags)
127 if (flags & TPM_TRANSMIT_NESTED)
130 if (!chip->ops->relinquish_locality)
133 rc = chip->ops->relinquish_locality(chip, chip->locality);
135 dev_err(&chip->dev, "%s: : error %d\n", __func__, rc);
140 static int tpm_cmd_ready(struct tpm_chip *chip, unsigned int flags)
142 if (flags & TPM_TRANSMIT_NESTED)
145 if (!chip->ops->cmd_ready)
148 return chip->ops->cmd_ready(chip);
151 static int tpm_go_idle(struct tpm_chip *chip, unsigned int flags)
153 if (flags & TPM_TRANSMIT_NESTED)
156 if (!chip->ops->go_idle)
159 return chip->ops->go_idle(chip);
162 static ssize_t tpm_try_transmit(struct tpm_chip *chip, struct tpm_space *space,
163 void *buf, size_t bufsiz, unsigned int flags)
165 struct tpm_header *header = buf;
171 rc = tpm_validate_command(chip, space, buf, bufsiz);
175 * If the command is not implemented by the TPM, synthesize a
176 * response with a TPM2_RC_COMMAND_CODE return for user-space.
178 if (rc == -EOPNOTSUPP) {
179 header->length = cpu_to_be32(sizeof(*header));
180 header->tag = cpu_to_be16(TPM2_ST_NO_SESSIONS);
181 header->return_code = cpu_to_be32(TPM2_RC_COMMAND_CODE |
182 TSS2_RESMGR_TPM_RC_LAYER);
183 return sizeof(*header);
186 if (bufsiz > TPM_BUFSIZE)
187 bufsiz = TPM_BUFSIZE;
189 count = be32_to_cpu(header->length);
190 ordinal = be32_to_cpu(header->ordinal);
193 if (count > bufsiz) {
195 "invalid count value %x %zx\n", count, bufsiz);
199 rc = tpm2_prepare_space(chip, space, ordinal, buf);
203 rc = chip->ops->send(chip, buf, count);
207 "%s: send(): error %d\n", __func__, rc);
211 /* A sanity check. send() should just return zero on success e.g.
212 * not the command length.
216 "%s: send(): invalid value %d\n", __func__, rc);
220 if (chip->flags & TPM_CHIP_FLAG_IRQ)
223 stop = jiffies + tpm_calc_ordinal_duration(chip, ordinal);
225 u8 status = chip->ops->status(chip);
226 if ((status & chip->ops->req_complete_mask) ==
227 chip->ops->req_complete_val)
230 if (chip->ops->req_canceled(chip, status)) {
231 dev_err(&chip->dev, "Operation Canceled\n");
236 tpm_msleep(TPM_TIMEOUT_POLL);
238 } while (time_before(jiffies, stop));
240 chip->ops->cancel(chip);
241 dev_err(&chip->dev, "Operation Timed out\n");
246 len = chip->ops->recv(chip, buf, bufsiz);
249 dev_err(&chip->dev, "tpm_transmit: tpm_recv: error %d\n", rc);
250 } else if (len < TPM_HEADER_SIZE || len != be32_to_cpu(header->length))
255 rc = tpm2_commit_space(chip, space, ordinal, buf, &len);
257 return rc ? rc : len;
261 * tpm_transmit - Internal kernel interface to transmit TPM commands.
262 * @chip: a TPM chip to use
263 * @space: a TPM space
264 * @buf: a TPM command buffer
265 * @bufsiz: length of the TPM command buffer
266 * @flags: TPM transmit flags
268 * A wrapper around tpm_try_transmit() that handles TPM2_RC_RETRY returns from
269 * the TPM and retransmits the command after a delay up to a maximum wait of
270 * TPM2_DURATION_LONG.
272 * Note that TPM 1.x never returns TPM2_RC_RETRY so the retry logic is TPM 2.0
276 * * The response length - OK
277 * * -errno - A system error
279 ssize_t tpm_transmit(struct tpm_chip *chip, struct tpm_space *space,
280 u8 *buf, size_t bufsiz, unsigned int flags)
282 struct tpm_header *header = (struct tpm_header *)buf;
283 /* space for header and handles */
284 u8 save[TPM_HEADER_SIZE + 3*sizeof(u32)];
285 unsigned int delay_msec = TPM2_DURATION_SHORT;
286 bool has_locality = false;
289 const size_t save_size = min(space ? sizeof(save) : TPM_HEADER_SIZE,
291 /* the command code is where the return code will be */
292 u32 cc = be32_to_cpu(header->return_code);
295 * Subtlety here: if we have a space, the handles will be
296 * transformed, so when we restore the header we also have to
297 * restore the handles.
299 memcpy(save, buf, save_size);
302 if (!(flags & TPM_TRANSMIT_UNLOCKED) &&
303 !(flags & TPM_TRANSMIT_NESTED))
304 mutex_lock(&chip->tpm_mutex);
306 if (chip->ops->clk_enable != NULL)
307 chip->ops->clk_enable(chip, true);
309 if (chip->locality == -1) {
310 ret = tpm_request_locality(chip, flags);
316 ret = tpm_cmd_ready(chip, flags);
320 ret = tpm_try_transmit(chip, space, buf, bufsiz, flags);
322 /* This may fail but do not override ret. */
323 tpm_go_idle(chip, flags);
327 tpm_relinquish_locality(chip, flags);
329 if (chip->ops->clk_enable != NULL)
330 chip->ops->clk_enable(chip, false);
332 if (!(flags & TPM_TRANSMIT_UNLOCKED) &&
333 !(flags & TPM_TRANSMIT_NESTED))
334 mutex_unlock(&chip->tpm_mutex);
338 rc = be32_to_cpu(header->return_code);
339 if (rc != TPM2_RC_RETRY && rc != TPM2_RC_TESTING)
342 * return immediately if self test returns test
343 * still running to shorten boot time.
345 if (rc == TPM2_RC_TESTING && cc == TPM2_CC_SELF_TEST)
348 if (delay_msec > TPM2_DURATION_LONG) {
349 if (rc == TPM2_RC_RETRY)
350 dev_err(&chip->dev, "in retry loop\n");
353 "self test is still running\n");
356 tpm_msleep(delay_msec);
358 memcpy(buf, save, save_size);
364 * tpm_transmit_cmd - send a tpm command to the device
365 * @chip: a TPM chip to use
366 * @space: a TPM space
367 * @buf: a TPM command buffer
368 * @min_rsp_body_length: minimum expected length of response body
369 * @flags: TPM transmit flags
370 * @desc: command description used in the error message
374 * * -errno - A system error
375 * * TPM_RC - A TPM error
377 ssize_t tpm_transmit_cmd(struct tpm_chip *chip, struct tpm_space *space,
378 struct tpm_buf *buf, size_t min_rsp_body_length,
379 unsigned int flags, const char *desc)
381 const struct tpm_header *header = (struct tpm_header *)buf->data;
385 len = tpm_transmit(chip, space, buf->data, PAGE_SIZE, flags);
389 err = be32_to_cpu(header->return_code);
390 if (err != 0 && err != TPM_ERR_DISABLED && err != TPM_ERR_DEACTIVATED
391 && err != TPM2_RC_TESTING && desc)
392 dev_err(&chip->dev, "A TPM error (%d) occurred %s\n", err,
397 if (len < min_rsp_body_length + TPM_HEADER_SIZE)
402 EXPORT_SYMBOL_GPL(tpm_transmit_cmd);
404 int tpm_get_timeouts(struct tpm_chip *chip)
406 if (chip->flags & TPM_CHIP_FLAG_HAVE_TIMEOUTS)
409 if (chip->flags & TPM_CHIP_FLAG_TPM2)
410 return tpm2_get_timeouts(chip);
412 return tpm1_get_timeouts(chip);
414 EXPORT_SYMBOL_GPL(tpm_get_timeouts);
417 * tpm_is_tpm2 - do we a have a TPM2 chip?
418 * @chip: a &struct tpm_chip instance, %NULL for the default chip
421 * 1 if we have a TPM2 chip.
422 * 0 if we don't have a TPM2 chip.
423 * A negative number for system errors (errno).
425 int tpm_is_tpm2(struct tpm_chip *chip)
429 chip = tpm_find_get_ops(chip);
433 rc = (chip->flags & TPM_CHIP_FLAG_TPM2) != 0;
439 EXPORT_SYMBOL_GPL(tpm_is_tpm2);
442 * tpm_pcr_read - read a PCR value from SHA1 bank
443 * @chip: a &struct tpm_chip instance, %NULL for the default chip
444 * @pcr_idx: the PCR to be retrieved
445 * @res_buf: the value of the PCR
447 * Return: same as with tpm_transmit_cmd()
449 int tpm_pcr_read(struct tpm_chip *chip, u32 pcr_idx, u8 *res_buf)
453 chip = tpm_find_get_ops(chip);
457 if (chip->flags & TPM_CHIP_FLAG_TPM2)
458 rc = tpm2_pcr_read(chip, pcr_idx, res_buf);
460 rc = tpm1_pcr_read(chip, pcr_idx, res_buf);
465 EXPORT_SYMBOL_GPL(tpm_pcr_read);
468 * tpm_pcr_extend - extend a PCR value in SHA1 bank.
469 * @chip: a &struct tpm_chip instance, %NULL for the default chip
470 * @pcr_idx: the PCR to be retrieved
471 * @hash: the hash value used to extend the PCR value
473 * Note: with TPM 2.0 extends also those banks with a known digest size to the
474 * cryto subsystem in order to prevent malicious use of those PCR banks. In the
475 * future we should dynamically determine digest sizes.
477 * Return: same as with tpm_transmit_cmd()
479 int tpm_pcr_extend(struct tpm_chip *chip, u32 pcr_idx, const u8 *hash)
482 struct tpm2_digest digest_list[ARRAY_SIZE(chip->active_banks)];
486 chip = tpm_find_get_ops(chip);
490 if (chip->flags & TPM_CHIP_FLAG_TPM2) {
491 memset(digest_list, 0, sizeof(digest_list));
493 for (i = 0; i < ARRAY_SIZE(chip->active_banks) &&
494 chip->active_banks[i] != TPM2_ALG_ERROR; i++) {
495 digest_list[i].alg_id = chip->active_banks[i];
496 memcpy(digest_list[i].digest, hash, TPM_DIGEST_SIZE);
500 rc = tpm2_pcr_extend(chip, pcr_idx, count, digest_list);
505 rc = tpm1_pcr_extend(chip, pcr_idx, hash,
506 "attempting extend a PCR value");
510 EXPORT_SYMBOL_GPL(tpm_pcr_extend);
513 * tpm_send - send a TPM command
514 * @chip: a &struct tpm_chip instance, %NULL for the default chip
515 * @cmd: a TPM command buffer
516 * @buflen: the length of the TPM command buffer
518 * Return: same as with tpm_transmit_cmd()
520 int tpm_send(struct tpm_chip *chip, void *cmd, size_t buflen)
525 chip = tpm_find_get_ops(chip);
529 rc = tpm_buf_init(&buf, 0, 0);
533 memcpy(buf.data, cmd, buflen);
534 rc = tpm_transmit_cmd(chip, NULL, &buf, 0, 0,
535 "attempting to a send a command");
536 tpm_buf_destroy(&buf);
541 EXPORT_SYMBOL_GPL(tpm_send);
543 int tpm_auto_startup(struct tpm_chip *chip)
547 if (!(chip->ops->flags & TPM_OPS_AUTO_STARTUP))
550 if (chip->flags & TPM_CHIP_FLAG_TPM2)
551 rc = tpm2_auto_startup(chip);
553 rc = tpm1_auto_startup(chip);
559 * We are about to suspend. Save the TPM state
560 * so that it can be restored.
562 int tpm_pm_suspend(struct device *dev)
564 struct tpm_chip *chip = dev_get_drvdata(dev);
570 if (chip->flags & TPM_CHIP_FLAG_ALWAYS_POWERED)
573 if (chip->flags & TPM_CHIP_FLAG_TPM2)
574 tpm2_shutdown(chip, TPM2_SU_STATE);
576 rc = tpm1_pm_suspend(chip, tpm_suspend_pcr);
580 EXPORT_SYMBOL_GPL(tpm_pm_suspend);
583 * Resume from a power safe. The BIOS already restored
586 int tpm_pm_resume(struct device *dev)
588 struct tpm_chip *chip = dev_get_drvdata(dev);
595 EXPORT_SYMBOL_GPL(tpm_pm_resume);
598 * tpm_get_random() - get random bytes from the TPM's RNG
599 * @chip: a &struct tpm_chip instance, %NULL for the default chip
600 * @out: destination buffer for the random bytes
601 * @max: the max number of bytes to write to @out
603 * Return: number of random bytes read or a negative error value.
605 int tpm_get_random(struct tpm_chip *chip, u8 *out, size_t max)
609 if (!out || max > TPM_MAX_RNG_DATA)
612 chip = tpm_find_get_ops(chip);
616 if (chip->flags & TPM_CHIP_FLAG_TPM2)
617 rc = tpm2_get_random(chip, out, max);
619 rc = tpm1_get_random(chip, out, max);
624 EXPORT_SYMBOL_GPL(tpm_get_random);
627 * tpm_seal_trusted() - seal a trusted key payload
628 * @chip: a &struct tpm_chip instance, %NULL for the default chip
629 * @options: authentication values and other options
630 * @payload: the key data in clear and encrypted form
632 * Note: only TPM 2.0 chip are supported. TPM 1.x implementation is located in
633 * the keyring subsystem.
635 * Return: same as with tpm_transmit_cmd()
637 int tpm_seal_trusted(struct tpm_chip *chip, struct trusted_key_payload *payload,
638 struct trusted_key_options *options)
642 chip = tpm_find_get_ops(chip);
643 if (!chip || !(chip->flags & TPM_CHIP_FLAG_TPM2))
646 rc = tpm2_seal_trusted(chip, payload, options);
651 EXPORT_SYMBOL_GPL(tpm_seal_trusted);
654 * tpm_unseal_trusted() - unseal a trusted key
655 * @chip: a &struct tpm_chip instance, %NULL for the default chip
656 * @options: authentication values and other options
657 * @payload: the key data in clear and encrypted form
659 * Note: only TPM 2.0 chip are supported. TPM 1.x implementation is located in
660 * the keyring subsystem.
662 * Return: same as with tpm_transmit_cmd()
664 int tpm_unseal_trusted(struct tpm_chip *chip,
665 struct trusted_key_payload *payload,
666 struct trusted_key_options *options)
670 chip = tpm_find_get_ops(chip);
671 if (!chip || !(chip->flags & TPM_CHIP_FLAG_TPM2))
674 rc = tpm2_unseal_trusted(chip, payload, options);
680 EXPORT_SYMBOL_GPL(tpm_unseal_trusted);
682 static int __init tpm_init(void)
686 tpm_class = class_create(THIS_MODULE, "tpm");
687 if (IS_ERR(tpm_class)) {
688 pr_err("couldn't create tpm class\n");
689 return PTR_ERR(tpm_class);
692 tpmrm_class = class_create(THIS_MODULE, "tpmrm");
693 if (IS_ERR(tpmrm_class)) {
694 pr_err("couldn't create tpmrm class\n");
695 rc = PTR_ERR(tpmrm_class);
696 goto out_destroy_tpm_class;
699 rc = alloc_chrdev_region(&tpm_devt, 0, 2*TPM_NUM_DEVICES, "tpm");
701 pr_err("tpm: failed to allocate char dev region\n");
702 goto out_destroy_tpmrm_class;
705 rc = tpm_dev_common_init();
707 pr_err("tpm: failed to allocate char dev region\n");
708 goto out_unreg_chrdev;
714 unregister_chrdev_region(tpm_devt, 2 * TPM_NUM_DEVICES);
715 out_destroy_tpmrm_class:
716 class_destroy(tpmrm_class);
717 out_destroy_tpm_class:
718 class_destroy(tpm_class);
723 static void __exit tpm_exit(void)
725 idr_destroy(&dev_nums_idr);
726 class_destroy(tpm_class);
727 class_destroy(tpmrm_class);
728 unregister_chrdev_region(tpm_devt, 2*TPM_NUM_DEVICES);
729 tpm_dev_common_exit();
732 subsys_initcall(tpm_init);
733 module_exit(tpm_exit);
735 MODULE_AUTHOR("Leendert van Doorn (leendert@watson.ibm.com)");
736 MODULE_DESCRIPTION("TPM Driver");
737 MODULE_VERSION("2.0");
738 MODULE_LICENSE("GPL");