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 ssize_t tpm_try_transmit(struct tpm_chip *chip, void *buf, size_t bufsiz)
67 struct tpm_header *header = buf;
73 if (bufsiz < TPM_HEADER_SIZE)
76 if (bufsiz > TPM_BUFSIZE)
79 count = be32_to_cpu(header->length);
80 ordinal = be32_to_cpu(header->ordinal);
85 "invalid count value %x %zx\n", count, bufsiz);
89 rc = chip->ops->send(chip, buf, count);
93 "%s: send(): error %d\n", __func__, rc);
97 /* A sanity check. send() should just return zero on success e.g.
98 * not the command length.
102 "%s: send(): invalid value %d\n", __func__, rc);
106 if (chip->flags & TPM_CHIP_FLAG_IRQ)
109 stop = jiffies + tpm_calc_ordinal_duration(chip, ordinal);
111 u8 status = chip->ops->status(chip);
112 if ((status & chip->ops->req_complete_mask) ==
113 chip->ops->req_complete_val)
116 if (chip->ops->req_canceled(chip, status)) {
117 dev_err(&chip->dev, "Operation Canceled\n");
121 tpm_msleep(TPM_TIMEOUT_POLL);
123 } while (time_before(jiffies, stop));
125 chip->ops->cancel(chip);
126 dev_err(&chip->dev, "Operation Timed out\n");
130 len = chip->ops->recv(chip, buf, bufsiz);
133 dev_err(&chip->dev, "tpm_transmit: tpm_recv: error %d\n", rc);
134 } else if (len < TPM_HEADER_SIZE || len != be32_to_cpu(header->length))
137 return rc ? rc : len;
141 * tpm_transmit - Internal kernel interface to transmit TPM commands.
142 * @chip: a TPM chip to use
143 * @buf: a TPM command buffer
144 * @bufsiz: length of the TPM command buffer
146 * A wrapper around tpm_try_transmit() that handles TPM2_RC_RETRY returns from
147 * the TPM and retransmits the command after a delay up to a maximum wait of
148 * TPM2_DURATION_LONG.
150 * Note that TPM 1.x never returns TPM2_RC_RETRY so the retry logic is TPM 2.0
154 * * The response length - OK
155 * * -errno - A system error
157 ssize_t tpm_transmit(struct tpm_chip *chip, u8 *buf, size_t bufsiz)
159 struct tpm_header *header = (struct tpm_header *)buf;
160 /* space for header and handles */
161 u8 save[TPM_HEADER_SIZE + 3*sizeof(u32)];
162 unsigned int delay_msec = TPM2_DURATION_SHORT;
165 const size_t save_size = min(sizeof(save), bufsiz);
166 /* the command code is where the return code will be */
167 u32 cc = be32_to_cpu(header->return_code);
170 * Subtlety here: if we have a space, the handles will be
171 * transformed, so when we restore the header we also have to
172 * restore the handles.
174 memcpy(save, buf, save_size);
177 ret = tpm_try_transmit(chip, buf, bufsiz);
180 rc = be32_to_cpu(header->return_code);
181 if (rc != TPM2_RC_RETRY && rc != TPM2_RC_TESTING)
184 * return immediately if self test returns test
185 * still running to shorten boot time.
187 if (rc == TPM2_RC_TESTING && cc == TPM2_CC_SELF_TEST)
190 if (delay_msec > TPM2_DURATION_LONG) {
191 if (rc == TPM2_RC_RETRY)
192 dev_err(&chip->dev, "in retry loop\n");
195 "self test is still running\n");
198 tpm_msleep(delay_msec);
200 memcpy(buf, save, save_size);
206 * tpm_transmit_cmd - send a tpm command to the device
207 * @chip: a TPM chip to use
208 * @buf: a TPM command buffer
209 * @min_rsp_body_length: minimum expected length of response body
210 * @desc: command description used in the error message
214 * * -errno - A system error
215 * * TPM_RC - A TPM error
217 ssize_t tpm_transmit_cmd(struct tpm_chip *chip, struct tpm_buf *buf,
218 size_t min_rsp_body_length, const char *desc)
220 const struct tpm_header *header = (struct tpm_header *)buf->data;
224 len = tpm_transmit(chip, buf->data, PAGE_SIZE);
228 err = be32_to_cpu(header->return_code);
229 if (err != 0 && err != TPM_ERR_DISABLED && err != TPM_ERR_DEACTIVATED
230 && err != TPM2_RC_TESTING && desc)
231 dev_err(&chip->dev, "A TPM error (%d) occurred %s\n", err,
236 if (len < min_rsp_body_length + TPM_HEADER_SIZE)
241 EXPORT_SYMBOL_GPL(tpm_transmit_cmd);
243 int tpm_get_timeouts(struct tpm_chip *chip)
245 if (chip->flags & TPM_CHIP_FLAG_HAVE_TIMEOUTS)
248 if (chip->flags & TPM_CHIP_FLAG_TPM2)
249 return tpm2_get_timeouts(chip);
251 return tpm1_get_timeouts(chip);
253 EXPORT_SYMBOL_GPL(tpm_get_timeouts);
256 * tpm_is_tpm2 - do we a have a TPM2 chip?
257 * @chip: a &struct tpm_chip instance, %NULL for the default chip
260 * 1 if we have a TPM2 chip.
261 * 0 if we don't have a TPM2 chip.
262 * A negative number for system errors (errno).
264 int tpm_is_tpm2(struct tpm_chip *chip)
268 chip = tpm_find_get_ops(chip);
272 rc = (chip->flags & TPM_CHIP_FLAG_TPM2) != 0;
278 EXPORT_SYMBOL_GPL(tpm_is_tpm2);
281 * tpm_pcr_read - read a PCR value from SHA1 bank
282 * @chip: a &struct tpm_chip instance, %NULL for the default chip
283 * @pcr_idx: the PCR to be retrieved
284 * @digest: the PCR bank and buffer current PCR value is written to
286 * Return: same as with tpm_transmit_cmd()
288 int tpm_pcr_read(struct tpm_chip *chip, u32 pcr_idx,
289 struct tpm_digest *digest)
293 chip = tpm_find_get_ops(chip);
297 if (chip->flags & TPM_CHIP_FLAG_TPM2)
298 rc = tpm2_pcr_read(chip, pcr_idx, digest, NULL);
300 rc = tpm1_pcr_read(chip, pcr_idx, digest->digest);
305 EXPORT_SYMBOL_GPL(tpm_pcr_read);
308 * tpm_pcr_extend - extend a PCR value in SHA1 bank.
309 * @chip: a &struct tpm_chip instance, %NULL for the default chip
310 * @pcr_idx: the PCR to be retrieved
311 * @digests: array of tpm_digest structures used to extend PCRs
313 * Note: callers must pass a digest for every allocated PCR bank, in the same
314 * order of the banks in chip->allocated_banks.
316 * Return: same as with tpm_transmit_cmd()
318 int tpm_pcr_extend(struct tpm_chip *chip, u32 pcr_idx,
319 struct tpm_digest *digests)
324 chip = tpm_find_get_ops(chip);
328 for (i = 0; i < chip->nr_allocated_banks; i++)
329 if (digests[i].alg_id != chip->allocated_banks[i].alg_id)
332 if (chip->flags & TPM_CHIP_FLAG_TPM2) {
333 rc = tpm2_pcr_extend(chip, pcr_idx, digests);
338 rc = tpm1_pcr_extend(chip, pcr_idx, digests[0].digest,
339 "attempting extend a PCR value");
343 EXPORT_SYMBOL_GPL(tpm_pcr_extend);
346 * tpm_send - send a TPM command
347 * @chip: a &struct tpm_chip instance, %NULL for the default chip
348 * @cmd: a TPM command buffer
349 * @buflen: the length of the TPM command buffer
351 * Return: same as with tpm_transmit_cmd()
353 int tpm_send(struct tpm_chip *chip, void *cmd, size_t buflen)
358 chip = tpm_find_get_ops(chip);
362 rc = tpm_buf_init(&buf, 0, 0);
366 memcpy(buf.data, cmd, buflen);
367 rc = tpm_transmit_cmd(chip, &buf, 0, "attempting to a send a command");
368 tpm_buf_destroy(&buf);
373 EXPORT_SYMBOL_GPL(tpm_send);
375 int tpm_auto_startup(struct tpm_chip *chip)
379 if (!(chip->ops->flags & TPM_OPS_AUTO_STARTUP))
382 if (chip->flags & TPM_CHIP_FLAG_TPM2)
383 rc = tpm2_auto_startup(chip);
385 rc = tpm1_auto_startup(chip);
391 * We are about to suspend. Save the TPM state
392 * so that it can be restored.
394 int tpm_pm_suspend(struct device *dev)
396 struct tpm_chip *chip = dev_get_drvdata(dev);
402 if (chip->flags & TPM_CHIP_FLAG_ALWAYS_POWERED)
405 if (!tpm_chip_start(chip)) {
406 if (chip->flags & TPM_CHIP_FLAG_TPM2)
407 tpm2_shutdown(chip, TPM2_SU_STATE);
409 rc = tpm1_pm_suspend(chip, tpm_suspend_pcr);
416 EXPORT_SYMBOL_GPL(tpm_pm_suspend);
419 * Resume from a power safe. The BIOS already restored
422 int tpm_pm_resume(struct device *dev)
424 struct tpm_chip *chip = dev_get_drvdata(dev);
431 EXPORT_SYMBOL_GPL(tpm_pm_resume);
434 * tpm_get_random() - get random bytes from the TPM's RNG
435 * @chip: a &struct tpm_chip instance, %NULL for the default chip
436 * @out: destination buffer for the random bytes
437 * @max: the max number of bytes to write to @out
439 * Return: number of random bytes read or a negative error value.
441 int tpm_get_random(struct tpm_chip *chip, u8 *out, size_t max)
445 if (!out || max > TPM_MAX_RNG_DATA)
448 chip = tpm_find_get_ops(chip);
452 if (chip->flags & TPM_CHIP_FLAG_TPM2)
453 rc = tpm2_get_random(chip, out, max);
455 rc = tpm1_get_random(chip, out, max);
460 EXPORT_SYMBOL_GPL(tpm_get_random);
463 * tpm_seal_trusted() - seal a trusted key payload
464 * @chip: a &struct tpm_chip instance, %NULL for the default chip
465 * @options: authentication values and other options
466 * @payload: the key data in clear and encrypted form
468 * Note: only TPM 2.0 chip are supported. TPM 1.x implementation is located in
469 * the keyring subsystem.
471 * Return: same as with tpm_transmit_cmd()
473 int tpm_seal_trusted(struct tpm_chip *chip, struct trusted_key_payload *payload,
474 struct trusted_key_options *options)
478 chip = tpm_find_get_ops(chip);
479 if (!chip || !(chip->flags & TPM_CHIP_FLAG_TPM2))
482 rc = tpm2_seal_trusted(chip, payload, options);
487 EXPORT_SYMBOL_GPL(tpm_seal_trusted);
490 * tpm_unseal_trusted() - unseal a trusted key
491 * @chip: a &struct tpm_chip instance, %NULL for the default chip
492 * @options: authentication values and other options
493 * @payload: the key data in clear and encrypted form
495 * Note: only TPM 2.0 chip are supported. TPM 1.x implementation is located in
496 * the keyring subsystem.
498 * Return: same as with tpm_transmit_cmd()
500 int tpm_unseal_trusted(struct tpm_chip *chip,
501 struct trusted_key_payload *payload,
502 struct trusted_key_options *options)
506 chip = tpm_find_get_ops(chip);
507 if (!chip || !(chip->flags & TPM_CHIP_FLAG_TPM2))
510 rc = tpm2_unseal_trusted(chip, payload, options);
516 EXPORT_SYMBOL_GPL(tpm_unseal_trusted);
518 static int __init tpm_init(void)
522 tpm_class = class_create(THIS_MODULE, "tpm");
523 if (IS_ERR(tpm_class)) {
524 pr_err("couldn't create tpm class\n");
525 return PTR_ERR(tpm_class);
528 tpmrm_class = class_create(THIS_MODULE, "tpmrm");
529 if (IS_ERR(tpmrm_class)) {
530 pr_err("couldn't create tpmrm class\n");
531 rc = PTR_ERR(tpmrm_class);
532 goto out_destroy_tpm_class;
535 rc = alloc_chrdev_region(&tpm_devt, 0, 2*TPM_NUM_DEVICES, "tpm");
537 pr_err("tpm: failed to allocate char dev region\n");
538 goto out_destroy_tpmrm_class;
541 rc = tpm_dev_common_init();
543 pr_err("tpm: failed to allocate char dev region\n");
544 goto out_unreg_chrdev;
550 unregister_chrdev_region(tpm_devt, 2 * TPM_NUM_DEVICES);
551 out_destroy_tpmrm_class:
552 class_destroy(tpmrm_class);
553 out_destroy_tpm_class:
554 class_destroy(tpm_class);
559 static void __exit tpm_exit(void)
561 idr_destroy(&dev_nums_idr);
562 class_destroy(tpm_class);
563 class_destroy(tpmrm_class);
564 unregister_chrdev_region(tpm_devt, 2*TPM_NUM_DEVICES);
565 tpm_dev_common_exit();
568 subsys_initcall(tpm_init);
569 module_exit(tpm_exit);
571 MODULE_AUTHOR("Leendert van Doorn (leendert@watson.ibm.com)");
572 MODULE_DESCRIPTION("TPM Driver");
573 MODULE_VERSION("2.0");
574 MODULE_LICENSE("GPL");