Linux kernel mirror (for testing)
git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel
os
linux
1/*
2 * Copyright (C) 2004 IBM Corporation
3 * Copyright (C) 2014 Intel Corporation
4 *
5 * Authors:
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>
10 *
11 * Maintained by: <tpmdd-devel@lists.sourceforge.net>
12 *
13 * Device driver for TCG/TCPA TPM (trusted platform module).
14 * Specifications at www.trustedcomputinggroup.org
15 *
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
19 * License.
20 *
21 * Note, the TPM chip is not interrupt driven (only polling)
22 * and can have very long timeouts (minutes!). Hence the unusual
23 * calls to msleep.
24 *
25 */
26
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>
33
34#include "tpm.h"
35
36/*
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.
40 */
41static u32 tpm_suspend_pcr;
42module_param_named(suspend_pcr, tpm_suspend_pcr, uint, 0644);
43MODULE_PARM_DESC(suspend_pcr,
44 "PCR to use for dummy writes to facilitate flush on suspend.");
45
46/**
47 * tpm_calc_ordinal_duration() - calculate the maximum command duration
48 * @chip: TPM chip to use.
49 * @ordinal: TPM command ordinal.
50 *
51 * The function returns the maximum amount of time the chip could take
52 * to return the result for a particular ordinal in jiffies.
53 *
54 * Return: A maximal duration time for an ordinal in jiffies.
55 */
56unsigned long tpm_calc_ordinal_duration(struct tpm_chip *chip, u32 ordinal)
57{
58 if (chip->flags & TPM_CHIP_FLAG_TPM2)
59 return tpm2_calc_ordinal_duration(chip, ordinal);
60 else
61 return tpm1_calc_ordinal_duration(chip, ordinal);
62}
63EXPORT_SYMBOL_GPL(tpm_calc_ordinal_duration);
64
65static ssize_t tpm_try_transmit(struct tpm_chip *chip, void *buf, size_t bufsiz)
66{
67 struct tpm_header *header = buf;
68 int rc;
69 ssize_t len = 0;
70 u32 count, ordinal;
71 unsigned long stop;
72
73 if (bufsiz < TPM_HEADER_SIZE)
74 return -EINVAL;
75
76 if (bufsiz > TPM_BUFSIZE)
77 bufsiz = TPM_BUFSIZE;
78
79 count = be32_to_cpu(header->length);
80 ordinal = be32_to_cpu(header->ordinal);
81 if (count == 0)
82 return -ENODATA;
83 if (count > bufsiz) {
84 dev_err(&chip->dev,
85 "invalid count value %x %zx\n", count, bufsiz);
86 return -E2BIG;
87 }
88
89 rc = chip->ops->send(chip, buf, count);
90 if (rc < 0) {
91 if (rc != -EPIPE)
92 dev_err(&chip->dev,
93 "%s: send(): error %d\n", __func__, rc);
94 return rc;
95 }
96
97 /* A sanity check. send() should just return zero on success e.g.
98 * not the command length.
99 */
100 if (rc > 0) {
101 dev_warn(&chip->dev,
102 "%s: send(): invalid value %d\n", __func__, rc);
103 rc = 0;
104 }
105
106 if (chip->flags & TPM_CHIP_FLAG_IRQ)
107 goto out_recv;
108
109 stop = jiffies + tpm_calc_ordinal_duration(chip, ordinal);
110 do {
111 u8 status = chip->ops->status(chip);
112 if ((status & chip->ops->req_complete_mask) ==
113 chip->ops->req_complete_val)
114 goto out_recv;
115
116 if (chip->ops->req_canceled(chip, status)) {
117 dev_err(&chip->dev, "Operation Canceled\n");
118 return -ECANCELED;
119 }
120
121 tpm_msleep(TPM_TIMEOUT_POLL);
122 rmb();
123 } while (time_before(jiffies, stop));
124
125 chip->ops->cancel(chip);
126 dev_err(&chip->dev, "Operation Timed out\n");
127 return -ETIME;
128
129out_recv:
130 len = chip->ops->recv(chip, buf, bufsiz);
131 if (len < 0) {
132 rc = len;
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))
135 rc = -EFAULT;
136
137 return rc ? rc : len;
138}
139
140/**
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
145 *
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.
149 *
150 * Note that TPM 1.x never returns TPM2_RC_RETRY so the retry logic is TPM 2.0
151 * only.
152 *
153 * Return:
154 * * The response length - OK
155 * * -errno - A system error
156 */
157ssize_t tpm_transmit(struct tpm_chip *chip, u8 *buf, size_t bufsiz)
158{
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;
163 u32 rc = 0;
164 ssize_t ret;
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);
168
169 /*
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.
173 */
174 memcpy(save, buf, save_size);
175
176 for (;;) {
177 ret = tpm_try_transmit(chip, buf, bufsiz);
178 if (ret < 0)
179 break;
180 rc = be32_to_cpu(header->return_code);
181 if (rc != TPM2_RC_RETRY && rc != TPM2_RC_TESTING)
182 break;
183 /*
184 * return immediately if self test returns test
185 * still running to shorten boot time.
186 */
187 if (rc == TPM2_RC_TESTING && cc == TPM2_CC_SELF_TEST)
188 break;
189
190 if (delay_msec > TPM2_DURATION_LONG) {
191 if (rc == TPM2_RC_RETRY)
192 dev_err(&chip->dev, "in retry loop\n");
193 else
194 dev_err(&chip->dev,
195 "self test is still running\n");
196 break;
197 }
198 tpm_msleep(delay_msec);
199 delay_msec *= 2;
200 memcpy(buf, save, save_size);
201 }
202 return ret;
203}
204
205/**
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
211 *
212 * Return:
213 * * 0 - OK
214 * * -errno - A system error
215 * * TPM_RC - A TPM error
216 */
217ssize_t tpm_transmit_cmd(struct tpm_chip *chip, struct tpm_buf *buf,
218 size_t min_rsp_body_length, const char *desc)
219{
220 const struct tpm_header *header = (struct tpm_header *)buf->data;
221 int err;
222 ssize_t len;
223
224 len = tpm_transmit(chip, buf->data, PAGE_SIZE);
225 if (len < 0)
226 return len;
227
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,
232 desc);
233 if (err)
234 return err;
235
236 if (len < min_rsp_body_length + TPM_HEADER_SIZE)
237 return -EFAULT;
238
239 return 0;
240}
241EXPORT_SYMBOL_GPL(tpm_transmit_cmd);
242
243int tpm_get_timeouts(struct tpm_chip *chip)
244{
245 if (chip->flags & TPM_CHIP_FLAG_HAVE_TIMEOUTS)
246 return 0;
247
248 if (chip->flags & TPM_CHIP_FLAG_TPM2)
249 return tpm2_get_timeouts(chip);
250 else
251 return tpm1_get_timeouts(chip);
252}
253EXPORT_SYMBOL_GPL(tpm_get_timeouts);
254
255/**
256 * tpm_is_tpm2 - do we a have a TPM2 chip?
257 * @chip: a &struct tpm_chip instance, %NULL for the default chip
258 *
259 * Return:
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).
263 */
264int tpm_is_tpm2(struct tpm_chip *chip)
265{
266 int rc;
267
268 chip = tpm_find_get_ops(chip);
269 if (!chip)
270 return -ENODEV;
271
272 rc = (chip->flags & TPM_CHIP_FLAG_TPM2) != 0;
273
274 tpm_put_ops(chip);
275
276 return rc;
277}
278EXPORT_SYMBOL_GPL(tpm_is_tpm2);
279
280/**
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
285 *
286 * Return: same as with tpm_transmit_cmd()
287 */
288int tpm_pcr_read(struct tpm_chip *chip, u32 pcr_idx,
289 struct tpm_digest *digest)
290{
291 int rc;
292
293 chip = tpm_find_get_ops(chip);
294 if (!chip)
295 return -ENODEV;
296
297 if (chip->flags & TPM_CHIP_FLAG_TPM2)
298 rc = tpm2_pcr_read(chip, pcr_idx, digest, NULL);
299 else
300 rc = tpm1_pcr_read(chip, pcr_idx, digest->digest);
301
302 tpm_put_ops(chip);
303 return rc;
304}
305EXPORT_SYMBOL_GPL(tpm_pcr_read);
306
307/**
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
312 *
313 * Note: callers must pass a digest for every allocated PCR bank, in the same
314 * order of the banks in chip->allocated_banks.
315 *
316 * Return: same as with tpm_transmit_cmd()
317 */
318int tpm_pcr_extend(struct tpm_chip *chip, u32 pcr_idx,
319 struct tpm_digest *digests)
320{
321 int rc;
322 int i;
323
324 chip = tpm_find_get_ops(chip);
325 if (!chip)
326 return -ENODEV;
327
328 for (i = 0; i < chip->nr_allocated_banks; i++)
329 if (digests[i].alg_id != chip->allocated_banks[i].alg_id)
330 return -EINVAL;
331
332 if (chip->flags & TPM_CHIP_FLAG_TPM2) {
333 rc = tpm2_pcr_extend(chip, pcr_idx, digests);
334 tpm_put_ops(chip);
335 return rc;
336 }
337
338 rc = tpm1_pcr_extend(chip, pcr_idx, digests[0].digest,
339 "attempting extend a PCR value");
340 tpm_put_ops(chip);
341 return rc;
342}
343EXPORT_SYMBOL_GPL(tpm_pcr_extend);
344
345/**
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
350 *
351 * Return: same as with tpm_transmit_cmd()
352 */
353int tpm_send(struct tpm_chip *chip, void *cmd, size_t buflen)
354{
355 struct tpm_buf buf;
356 int rc;
357
358 chip = tpm_find_get_ops(chip);
359 if (!chip)
360 return -ENODEV;
361
362 rc = tpm_buf_init(&buf, 0, 0);
363 if (rc)
364 goto out;
365
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);
369out:
370 tpm_put_ops(chip);
371 return rc;
372}
373EXPORT_SYMBOL_GPL(tpm_send);
374
375int tpm_auto_startup(struct tpm_chip *chip)
376{
377 int rc;
378
379 if (!(chip->ops->flags & TPM_OPS_AUTO_STARTUP))
380 return 0;
381
382 if (chip->flags & TPM_CHIP_FLAG_TPM2)
383 rc = tpm2_auto_startup(chip);
384 else
385 rc = tpm1_auto_startup(chip);
386
387 return rc;
388}
389
390/*
391 * We are about to suspend. Save the TPM state
392 * so that it can be restored.
393 */
394int tpm_pm_suspend(struct device *dev)
395{
396 struct tpm_chip *chip = dev_get_drvdata(dev);
397 int rc = 0;
398
399 if (!chip)
400 return -ENODEV;
401
402 if (chip->flags & TPM_CHIP_FLAG_ALWAYS_POWERED)
403 return 0;
404
405 if (!tpm_chip_start(chip)) {
406 if (chip->flags & TPM_CHIP_FLAG_TPM2)
407 tpm2_shutdown(chip, TPM2_SU_STATE);
408 else
409 rc = tpm1_pm_suspend(chip, tpm_suspend_pcr);
410
411 tpm_chip_stop(chip);
412 }
413
414 return rc;
415}
416EXPORT_SYMBOL_GPL(tpm_pm_suspend);
417
418/*
419 * Resume from a power safe. The BIOS already restored
420 * the TPM state.
421 */
422int tpm_pm_resume(struct device *dev)
423{
424 struct tpm_chip *chip = dev_get_drvdata(dev);
425
426 if (chip == NULL)
427 return -ENODEV;
428
429 return 0;
430}
431EXPORT_SYMBOL_GPL(tpm_pm_resume);
432
433/**
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
438 *
439 * Return: number of random bytes read or a negative error value.
440 */
441int tpm_get_random(struct tpm_chip *chip, u8 *out, size_t max)
442{
443 int rc;
444
445 if (!out || max > TPM_MAX_RNG_DATA)
446 return -EINVAL;
447
448 chip = tpm_find_get_ops(chip);
449 if (!chip)
450 return -ENODEV;
451
452 if (chip->flags & TPM_CHIP_FLAG_TPM2)
453 rc = tpm2_get_random(chip, out, max);
454 else
455 rc = tpm1_get_random(chip, out, max);
456
457 tpm_put_ops(chip);
458 return rc;
459}
460EXPORT_SYMBOL_GPL(tpm_get_random);
461
462/**
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
467 *
468 * Note: only TPM 2.0 chip are supported. TPM 1.x implementation is located in
469 * the keyring subsystem.
470 *
471 * Return: same as with tpm_transmit_cmd()
472 */
473int tpm_seal_trusted(struct tpm_chip *chip, struct trusted_key_payload *payload,
474 struct trusted_key_options *options)
475{
476 int rc;
477
478 chip = tpm_find_get_ops(chip);
479 if (!chip || !(chip->flags & TPM_CHIP_FLAG_TPM2))
480 return -ENODEV;
481
482 rc = tpm2_seal_trusted(chip, payload, options);
483
484 tpm_put_ops(chip);
485 return rc;
486}
487EXPORT_SYMBOL_GPL(tpm_seal_trusted);
488
489/**
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
494 *
495 * Note: only TPM 2.0 chip are supported. TPM 1.x implementation is located in
496 * the keyring subsystem.
497 *
498 * Return: same as with tpm_transmit_cmd()
499 */
500int tpm_unseal_trusted(struct tpm_chip *chip,
501 struct trusted_key_payload *payload,
502 struct trusted_key_options *options)
503{
504 int rc;
505
506 chip = tpm_find_get_ops(chip);
507 if (!chip || !(chip->flags & TPM_CHIP_FLAG_TPM2))
508 return -ENODEV;
509
510 rc = tpm2_unseal_trusted(chip, payload, options);
511
512 tpm_put_ops(chip);
513
514 return rc;
515}
516EXPORT_SYMBOL_GPL(tpm_unseal_trusted);
517
518static int __init tpm_init(void)
519{
520 int rc;
521
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);
526 }
527
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;
533 }
534
535 rc = alloc_chrdev_region(&tpm_devt, 0, 2*TPM_NUM_DEVICES, "tpm");
536 if (rc < 0) {
537 pr_err("tpm: failed to allocate char dev region\n");
538 goto out_destroy_tpmrm_class;
539 }
540
541 rc = tpm_dev_common_init();
542 if (rc) {
543 pr_err("tpm: failed to allocate char dev region\n");
544 goto out_unreg_chrdev;
545 }
546
547 return 0;
548
549out_unreg_chrdev:
550 unregister_chrdev_region(tpm_devt, 2 * TPM_NUM_DEVICES);
551out_destroy_tpmrm_class:
552 class_destroy(tpmrm_class);
553out_destroy_tpm_class:
554 class_destroy(tpm_class);
555
556 return rc;
557}
558
559static void __exit tpm_exit(void)
560{
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();
566}
567
568subsys_initcall(tpm_init);
569module_exit(tpm_exit);
570
571MODULE_AUTHOR("Leendert van Doorn (leendert@watson.ibm.com)");
572MODULE_DESCRIPTION("TPM Driver");
573MODULE_VERSION("2.0");
574MODULE_LICENSE("GPL");