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 #define TSC_MAX_ORDINAL 12 37 #define TPM_PROTECTED_COMMAND 0x00 38 #define TPM_CONNECTION_COMMAND 0x40 39 40 /* 41 * Bug workaround - some TPM's don't flush the most 42 * recently changed pcr on suspend, so force the flush 43 * with an extend to the selected _unused_ non-volatile pcr. 44 */ 45 static int tpm_suspend_pcr; 46 module_param_named(suspend_pcr, tpm_suspend_pcr, uint, 0644); 47 MODULE_PARM_DESC(suspend_pcr, 48 "PCR to use for dummy writes to facilitate flush on suspend."); 49 50 /** 51 * tpm_calc_ordinal_duration() - calculate the maximum command duration 52 * @chip: TPM chip to use. 53 * @ordinal: TPM command ordinal. 54 * 55 * The function returns the maximum amount of time the chip could take 56 * to return the result for a particular ordinal in jiffies. 57 * 58 * Return: A maximal duration time for an ordinal in jiffies. 59 */ 60 unsigned long tpm_calc_ordinal_duration(struct tpm_chip *chip, u32 ordinal) 61 { 62 if (chip->flags & TPM_CHIP_FLAG_TPM2) 63 return tpm2_calc_ordinal_duration(chip, ordinal); 64 else 65 return tpm1_calc_ordinal_duration(chip, ordinal); 66 } 67 EXPORT_SYMBOL_GPL(tpm_calc_ordinal_duration); 68 69 static int tpm_validate_command(struct tpm_chip *chip, 70 struct tpm_space *space, 71 const u8 *cmd, 72 size_t len) 73 { 74 const struct tpm_input_header *header = (const void *)cmd; 75 int i; 76 u32 cc; 77 u32 attrs; 78 unsigned int nr_handles; 79 80 if (len < TPM_HEADER_SIZE) 81 return -EINVAL; 82 83 if (!space) 84 return 0; 85 86 if (chip->flags & TPM_CHIP_FLAG_TPM2 && chip->nr_commands) { 87 cc = be32_to_cpu(header->ordinal); 88 89 i = tpm2_find_cc(chip, cc); 90 if (i < 0) { 91 dev_dbg(&chip->dev, "0x%04X is an invalid command\n", 92 cc); 93 return -EOPNOTSUPP; 94 } 95 96 attrs = chip->cc_attrs_tbl[i]; 97 nr_handles = 98 4 * ((attrs >> TPM2_CC_ATTR_CHANDLES) & GENMASK(2, 0)); 99 if (len < TPM_HEADER_SIZE + 4 * nr_handles) 100 goto err_len; 101 } 102 103 return 0; 104 err_len: 105 dev_dbg(&chip->dev, 106 "%s: insufficient command length %zu", __func__, len); 107 return -EINVAL; 108 } 109 110 static int tpm_request_locality(struct tpm_chip *chip, unsigned int flags) 111 { 112 int rc; 113 114 if (flags & TPM_TRANSMIT_NESTED) 115 return 0; 116 117 if (!chip->ops->request_locality) 118 return 0; 119 120 rc = chip->ops->request_locality(chip, 0); 121 if (rc < 0) 122 return rc; 123 124 chip->locality = rc; 125 126 return 0; 127 } 128 129 static void tpm_relinquish_locality(struct tpm_chip *chip, unsigned int flags) 130 { 131 int rc; 132 133 if (flags & TPM_TRANSMIT_NESTED) 134 return; 135 136 if (!chip->ops->relinquish_locality) 137 return; 138 139 rc = chip->ops->relinquish_locality(chip, chip->locality); 140 if (rc) 141 dev_err(&chip->dev, "%s: : error %d\n", __func__, rc); 142 143 chip->locality = -1; 144 } 145 146 static int tpm_cmd_ready(struct tpm_chip *chip, unsigned int flags) 147 { 148 if (flags & TPM_TRANSMIT_NESTED) 149 return 0; 150 151 if (!chip->ops->cmd_ready) 152 return 0; 153 154 return chip->ops->cmd_ready(chip); 155 } 156 157 static int tpm_go_idle(struct tpm_chip *chip, unsigned int flags) 158 { 159 if (flags & TPM_TRANSMIT_NESTED) 160 return 0; 161 162 if (!chip->ops->go_idle) 163 return 0; 164 165 return chip->ops->go_idle(chip); 166 } 167 168 static ssize_t tpm_try_transmit(struct tpm_chip *chip, 169 struct tpm_space *space, 170 u8 *buf, size_t bufsiz, 171 unsigned int flags) 172 { 173 struct tpm_output_header *header = (void *)buf; 174 int rc; 175 ssize_t len = 0; 176 u32 count, ordinal; 177 unsigned long stop; 178 bool need_locality; 179 180 rc = tpm_validate_command(chip, space, buf, bufsiz); 181 if (rc == -EINVAL) 182 return rc; 183 /* 184 * If the command is not implemented by the TPM, synthesize a 185 * response with a TPM2_RC_COMMAND_CODE return for user-space. 186 */ 187 if (rc == -EOPNOTSUPP) { 188 header->length = cpu_to_be32(sizeof(*header)); 189 header->tag = cpu_to_be16(TPM2_ST_NO_SESSIONS); 190 header->return_code = cpu_to_be32(TPM2_RC_COMMAND_CODE | 191 TSS2_RESMGR_TPM_RC_LAYER); 192 return sizeof(*header); 193 } 194 195 if (bufsiz > TPM_BUFSIZE) 196 bufsiz = TPM_BUFSIZE; 197 198 count = be32_to_cpu(*((__be32 *) (buf + 2))); 199 ordinal = be32_to_cpu(*((__be32 *) (buf + 6))); 200 if (count == 0) 201 return -ENODATA; 202 if (count > bufsiz) { 203 dev_err(&chip->dev, 204 "invalid count value %x %zx\n", count, bufsiz); 205 return -E2BIG; 206 } 207 208 if (!(flags & TPM_TRANSMIT_UNLOCKED) && !(flags & TPM_TRANSMIT_NESTED)) 209 mutex_lock(&chip->tpm_mutex); 210 211 if (chip->ops->clk_enable != NULL) 212 chip->ops->clk_enable(chip, true); 213 214 /* Store the decision as chip->locality will be changed. */ 215 need_locality = chip->locality == -1; 216 217 if (need_locality) { 218 rc = tpm_request_locality(chip, flags); 219 if (rc < 0) 220 goto out_no_locality; 221 } 222 223 rc = tpm_cmd_ready(chip, flags); 224 if (rc) 225 goto out; 226 227 rc = tpm2_prepare_space(chip, space, ordinal, buf); 228 if (rc) 229 goto out; 230 231 rc = chip->ops->send(chip, buf, count); 232 if (rc < 0) { 233 if (rc != -EPIPE) 234 dev_err(&chip->dev, 235 "%s: tpm_send: error %d\n", __func__, rc); 236 goto out; 237 } 238 239 if (chip->flags & TPM_CHIP_FLAG_IRQ) 240 goto out_recv; 241 242 stop = jiffies + tpm_calc_ordinal_duration(chip, ordinal); 243 do { 244 u8 status = chip->ops->status(chip); 245 if ((status & chip->ops->req_complete_mask) == 246 chip->ops->req_complete_val) 247 goto out_recv; 248 249 if (chip->ops->req_canceled(chip, status)) { 250 dev_err(&chip->dev, "Operation Canceled\n"); 251 rc = -ECANCELED; 252 goto out; 253 } 254 255 tpm_msleep(TPM_TIMEOUT_POLL); 256 rmb(); 257 } while (time_before(jiffies, stop)); 258 259 chip->ops->cancel(chip); 260 dev_err(&chip->dev, "Operation Timed out\n"); 261 rc = -ETIME; 262 goto out; 263 264 out_recv: 265 len = chip->ops->recv(chip, buf, bufsiz); 266 if (len < 0) { 267 rc = len; 268 dev_err(&chip->dev, 269 "tpm_transmit: tpm_recv: error %d\n", rc); 270 goto out; 271 } else if (len < TPM_HEADER_SIZE) { 272 rc = -EFAULT; 273 goto out; 274 } 275 276 if (len != be32_to_cpu(header->length)) { 277 rc = -EFAULT; 278 goto out; 279 } 280 281 rc = tpm2_commit_space(chip, space, ordinal, buf, &len); 282 if (rc) 283 dev_err(&chip->dev, "tpm2_commit_space: error %d\n", rc); 284 285 out: 286 rc = tpm_go_idle(chip, flags); 287 if (rc) 288 goto out; 289 290 if (need_locality) 291 tpm_relinquish_locality(chip, flags); 292 293 out_no_locality: 294 if (chip->ops->clk_enable != NULL) 295 chip->ops->clk_enable(chip, false); 296 297 if (!(flags & TPM_TRANSMIT_UNLOCKED) && !(flags & TPM_TRANSMIT_NESTED)) 298 mutex_unlock(&chip->tpm_mutex); 299 return rc ? rc : len; 300 } 301 302 /** 303 * tpm_transmit - Internal kernel interface to transmit TPM commands. 304 * 305 * @chip: TPM chip to use 306 * @space: tpm space 307 * @buf: TPM command buffer 308 * @bufsiz: length of the TPM command buffer 309 * @flags: tpm transmit flags - bitmap 310 * 311 * A wrapper around tpm_try_transmit that handles TPM2_RC_RETRY 312 * returns from the TPM and retransmits the command after a delay up 313 * to a maximum wait of TPM2_DURATION_LONG. 314 * 315 * Note: TPM1 never returns TPM2_RC_RETRY so the retry logic is TPM2 316 * only 317 * 318 * Return: 319 * the length of the return when the operation is successful. 320 * A negative number for system errors (errno). 321 */ 322 ssize_t tpm_transmit(struct tpm_chip *chip, struct tpm_space *space, 323 u8 *buf, size_t bufsiz, unsigned int flags) 324 { 325 struct tpm_output_header *header = (struct tpm_output_header *)buf; 326 /* space for header and handles */ 327 u8 save[TPM_HEADER_SIZE + 3*sizeof(u32)]; 328 unsigned int delay_msec = TPM2_DURATION_SHORT; 329 u32 rc = 0; 330 ssize_t ret; 331 const size_t save_size = min(space ? sizeof(save) : TPM_HEADER_SIZE, 332 bufsiz); 333 /* the command code is where the return code will be */ 334 u32 cc = be32_to_cpu(header->return_code); 335 336 /* 337 * Subtlety here: if we have a space, the handles will be 338 * transformed, so when we restore the header we also have to 339 * restore the handles. 340 */ 341 memcpy(save, buf, save_size); 342 343 for (;;) { 344 ret = tpm_try_transmit(chip, space, buf, bufsiz, flags); 345 if (ret < 0) 346 break; 347 rc = be32_to_cpu(header->return_code); 348 if (rc != TPM2_RC_RETRY && rc != TPM2_RC_TESTING) 349 break; 350 /* 351 * return immediately if self test returns test 352 * still running to shorten boot time. 353 */ 354 if (rc == TPM2_RC_TESTING && cc == TPM2_CC_SELF_TEST) 355 break; 356 357 if (delay_msec > TPM2_DURATION_LONG) { 358 if (rc == TPM2_RC_RETRY) 359 dev_err(&chip->dev, "in retry loop\n"); 360 else 361 dev_err(&chip->dev, 362 "self test is still running\n"); 363 break; 364 } 365 tpm_msleep(delay_msec); 366 delay_msec *= 2; 367 memcpy(buf, save, save_size); 368 } 369 return ret; 370 } 371 /** 372 * tpm_transmit_cmd - send a tpm command to the device 373 * The function extracts tpm out header return code 374 * 375 * @chip: TPM chip to use 376 * @space: tpm space 377 * @buf: TPM command buffer 378 * @bufsiz: length of the buffer 379 * @min_rsp_body_length: minimum expected length of response body 380 * @flags: tpm transmit flags - bitmap 381 * @desc: command description used in the error message 382 * 383 * Return: 384 * 0 when the operation is successful. 385 * A negative number for system errors (errno). 386 * A positive number for a TPM error. 387 */ 388 ssize_t tpm_transmit_cmd(struct tpm_chip *chip, struct tpm_space *space, 389 void *buf, size_t bufsiz, 390 size_t min_rsp_body_length, unsigned int flags, 391 const char *desc) 392 { 393 const struct tpm_output_header *header = buf; 394 int err; 395 ssize_t len; 396 397 len = tpm_transmit(chip, space, buf, bufsiz, flags); 398 if (len < 0) 399 return len; 400 401 err = be32_to_cpu(header->return_code); 402 if (err != 0 && err != TPM_ERR_DISABLED && err != TPM_ERR_DEACTIVATED 403 && desc) 404 dev_err(&chip->dev, "A TPM error (%d) occurred %s\n", err, 405 desc); 406 if (err) 407 return err; 408 409 if (len < min_rsp_body_length + TPM_HEADER_SIZE) 410 return -EFAULT; 411 412 return 0; 413 } 414 EXPORT_SYMBOL_GPL(tpm_transmit_cmd); 415 416 #define TPM_ORD_STARTUP 153 417 #define TPM_ST_CLEAR 1 418 419 /** 420 * tpm_startup - turn on the TPM 421 * @chip: TPM chip to use 422 * 423 * Normally the firmware should start the TPM. This function is provided as a 424 * workaround if this does not happen. A legal case for this could be for 425 * example when a TPM emulator is used. 426 * 427 * Return: same as tpm_transmit_cmd() 428 */ 429 int tpm_startup(struct tpm_chip *chip) 430 { 431 struct tpm_buf buf; 432 int rc; 433 434 dev_info(&chip->dev, "starting up the TPM manually\n"); 435 436 if (chip->flags & TPM_CHIP_FLAG_TPM2) { 437 rc = tpm_buf_init(&buf, TPM2_ST_NO_SESSIONS, TPM2_CC_STARTUP); 438 if (rc < 0) 439 return rc; 440 441 tpm_buf_append_u16(&buf, TPM2_SU_CLEAR); 442 } else { 443 rc = tpm_buf_init(&buf, TPM_TAG_RQU_COMMAND, TPM_ORD_STARTUP); 444 if (rc < 0) 445 return rc; 446 447 tpm_buf_append_u16(&buf, TPM_ST_CLEAR); 448 } 449 450 rc = tpm_transmit_cmd(chip, NULL, buf.data, PAGE_SIZE, 0, 0, 451 "attempting to start the TPM"); 452 453 tpm_buf_destroy(&buf); 454 return rc; 455 } 456 457 int tpm_get_timeouts(struct tpm_chip *chip) 458 { 459 if (chip->flags & TPM_CHIP_FLAG_HAVE_TIMEOUTS) 460 return 0; 461 462 if (chip->flags & TPM_CHIP_FLAG_TPM2) 463 return tpm2_get_timeouts(chip); 464 else 465 return tpm1_get_timeouts(chip); 466 } 467 EXPORT_SYMBOL_GPL(tpm_get_timeouts); 468 469 #define TPM_ORD_CONTINUE_SELFTEST 83 470 #define CONTINUE_SELFTEST_RESULT_SIZE 10 471 472 static const struct tpm_input_header continue_selftest_header = { 473 .tag = cpu_to_be16(TPM_TAG_RQU_COMMAND), 474 .length = cpu_to_be32(10), 475 .ordinal = cpu_to_be32(TPM_ORD_CONTINUE_SELFTEST), 476 }; 477 478 /** 479 * tpm_continue_selftest -- run TPM's selftest 480 * @chip: TPM chip to use 481 * 482 * Returns 0 on success, < 0 in case of fatal error or a value > 0 representing 483 * a TPM error code. 484 */ 485 static int tpm_continue_selftest(struct tpm_chip *chip) 486 { 487 int rc; 488 struct tpm_cmd_t cmd; 489 490 cmd.header.in = continue_selftest_header; 491 rc = tpm_transmit_cmd(chip, NULL, &cmd, CONTINUE_SELFTEST_RESULT_SIZE, 492 0, 0, "continue selftest"); 493 return rc; 494 } 495 496 #define TPM_ORDINAL_PCRREAD 21 497 #define READ_PCR_RESULT_SIZE 30 498 #define READ_PCR_RESULT_BODY_SIZE 20 499 static const struct tpm_input_header pcrread_header = { 500 .tag = cpu_to_be16(TPM_TAG_RQU_COMMAND), 501 .length = cpu_to_be32(14), 502 .ordinal = cpu_to_be32(TPM_ORDINAL_PCRREAD) 503 }; 504 505 int tpm_pcr_read_dev(struct tpm_chip *chip, int pcr_idx, u8 *res_buf) 506 { 507 int rc; 508 struct tpm_cmd_t cmd; 509 510 cmd.header.in = pcrread_header; 511 cmd.params.pcrread_in.pcr_idx = cpu_to_be32(pcr_idx); 512 rc = tpm_transmit_cmd(chip, NULL, &cmd, READ_PCR_RESULT_SIZE, 513 READ_PCR_RESULT_BODY_SIZE, 0, 514 "attempting to read a pcr value"); 515 516 if (rc == 0) 517 memcpy(res_buf, cmd.params.pcrread_out.pcr_result, 518 TPM_DIGEST_SIZE); 519 return rc; 520 } 521 522 /** 523 * tpm_is_tpm2 - do we a have a TPM2 chip? 524 * @chip: a &struct tpm_chip instance, %NULL for the default chip 525 * 526 * Return: 527 * 1 if we have a TPM2 chip. 528 * 0 if we don't have a TPM2 chip. 529 * A negative number for system errors (errno). 530 */ 531 int tpm_is_tpm2(struct tpm_chip *chip) 532 { 533 int rc; 534 535 chip = tpm_find_get_ops(chip); 536 if (!chip) 537 return -ENODEV; 538 539 rc = (chip->flags & TPM_CHIP_FLAG_TPM2) != 0; 540 541 tpm_put_ops(chip); 542 543 return rc; 544 } 545 EXPORT_SYMBOL_GPL(tpm_is_tpm2); 546 547 /** 548 * tpm_pcr_read - read a PCR value from SHA1 bank 549 * @chip: a &struct tpm_chip instance, %NULL for the default chip 550 * @pcr_idx: the PCR to be retrieved 551 * @res_buf: the value of the PCR 552 * 553 * Return: same as with tpm_transmit_cmd() 554 */ 555 int tpm_pcr_read(struct tpm_chip *chip, int pcr_idx, u8 *res_buf) 556 { 557 int rc; 558 559 chip = tpm_find_get_ops(chip); 560 if (!chip) 561 return -ENODEV; 562 if (chip->flags & TPM_CHIP_FLAG_TPM2) 563 rc = tpm2_pcr_read(chip, pcr_idx, res_buf); 564 else 565 rc = tpm_pcr_read_dev(chip, pcr_idx, res_buf); 566 tpm_put_ops(chip); 567 return rc; 568 } 569 EXPORT_SYMBOL_GPL(tpm_pcr_read); 570 571 /** 572 * tpm_pcr_extend - extend a PCR value in SHA1 bank. 573 * @chip: a &struct tpm_chip instance, %NULL for the default chip 574 * @pcr_idx: the PCR to be retrieved 575 * @hash: the hash value used to extend the PCR value 576 * 577 * Note: with TPM 2.0 extends also those banks with a known digest size to the 578 * cryto subsystem in order to prevent malicious use of those PCR banks. In the 579 * future we should dynamically determine digest sizes. 580 * 581 * Return: same as with tpm_transmit_cmd() 582 */ 583 int tpm_pcr_extend(struct tpm_chip *chip, int pcr_idx, const u8 *hash) 584 { 585 int rc; 586 struct tpm2_digest digest_list[ARRAY_SIZE(chip->active_banks)]; 587 u32 count = 0; 588 int i; 589 590 chip = tpm_find_get_ops(chip); 591 if (!chip) 592 return -ENODEV; 593 594 if (chip->flags & TPM_CHIP_FLAG_TPM2) { 595 memset(digest_list, 0, sizeof(digest_list)); 596 597 for (i = 0; i < ARRAY_SIZE(chip->active_banks) && 598 chip->active_banks[i] != TPM2_ALG_ERROR; i++) { 599 digest_list[i].alg_id = chip->active_banks[i]; 600 memcpy(digest_list[i].digest, hash, TPM_DIGEST_SIZE); 601 count++; 602 } 603 604 rc = tpm2_pcr_extend(chip, pcr_idx, count, digest_list); 605 tpm_put_ops(chip); 606 return rc; 607 } 608 609 rc = tpm1_pcr_extend(chip, pcr_idx, hash, 610 "attempting extend a PCR value"); 611 tpm_put_ops(chip); 612 return rc; 613 } 614 EXPORT_SYMBOL_GPL(tpm_pcr_extend); 615 616 /** 617 * tpm_do_selftest - have the TPM continue its selftest and wait until it 618 * can receive further commands 619 * @chip: TPM chip to use 620 * 621 * Returns 0 on success, < 0 in case of fatal error or a value > 0 representing 622 * a TPM error code. 623 */ 624 int tpm_do_selftest(struct tpm_chip *chip) 625 { 626 int rc; 627 unsigned int loops; 628 unsigned int delay_msec = 100; 629 unsigned long duration; 630 u8 dummy[TPM_DIGEST_SIZE]; 631 632 duration = tpm1_calc_ordinal_duration(chip, TPM_ORD_CONTINUE_SELFTEST); 633 634 loops = jiffies_to_msecs(duration) / delay_msec; 635 636 rc = tpm_continue_selftest(chip); 637 if (rc == TPM_ERR_INVALID_POSTINIT) { 638 chip->flags |= TPM_CHIP_FLAG_ALWAYS_POWERED; 639 dev_info(&chip->dev, "TPM not ready (%d)\n", rc); 640 } 641 /* This may fail if there was no TPM driver during a suspend/resume 642 * cycle; some may return 10 (BAD_ORDINAL), others 28 (FAILEDSELFTEST) 643 */ 644 if (rc) 645 return rc; 646 647 do { 648 /* Attempt to read a PCR value */ 649 rc = tpm_pcr_read_dev(chip, 0, dummy); 650 651 /* Some buggy TPMs will not respond to tpm_tis_ready() for 652 * around 300ms while the self test is ongoing, keep trying 653 * until the self test duration expires. */ 654 if (rc == -ETIME) { 655 dev_info( 656 &chip->dev, HW_ERR 657 "TPM command timed out during continue self test"); 658 tpm_msleep(delay_msec); 659 continue; 660 } 661 662 if (rc == TPM_ERR_DISABLED || rc == TPM_ERR_DEACTIVATED) { 663 dev_info(&chip->dev, 664 "TPM is disabled/deactivated (0x%X)\n", rc); 665 /* TPM is disabled and/or deactivated; driver can 666 * proceed and TPM does handle commands for 667 * suspend/resume correctly 668 */ 669 return 0; 670 } 671 if (rc != TPM_WARN_DOING_SELFTEST) 672 return rc; 673 tpm_msleep(delay_msec); 674 } while (--loops > 0); 675 676 return rc; 677 } 678 EXPORT_SYMBOL_GPL(tpm_do_selftest); 679 680 /** 681 * tpm1_auto_startup - Perform the standard automatic TPM initialization 682 * sequence 683 * @chip: TPM chip to use 684 * 685 * Returns 0 on success, < 0 in case of fatal error. 686 */ 687 int tpm1_auto_startup(struct tpm_chip *chip) 688 { 689 int rc; 690 691 rc = tpm_get_timeouts(chip); 692 if (rc) 693 goto out; 694 rc = tpm_do_selftest(chip); 695 if (rc) { 696 dev_err(&chip->dev, "TPM self test failed\n"); 697 goto out; 698 } 699 700 return rc; 701 out: 702 if (rc > 0) 703 rc = -ENODEV; 704 return rc; 705 } 706 707 /** 708 * tpm_send - send a TPM command 709 * @chip: a &struct tpm_chip instance, %NULL for the default chip 710 * @cmd: a TPM command buffer 711 * @buflen: the length of the TPM command buffer 712 * 713 * Return: same as with tpm_transmit_cmd() 714 */ 715 int tpm_send(struct tpm_chip *chip, void *cmd, size_t buflen) 716 { 717 int rc; 718 719 chip = tpm_find_get_ops(chip); 720 if (!chip) 721 return -ENODEV; 722 723 rc = tpm_transmit_cmd(chip, NULL, cmd, buflen, 0, 0, 724 "attempting to a send a command"); 725 tpm_put_ops(chip); 726 return rc; 727 } 728 EXPORT_SYMBOL_GPL(tpm_send); 729 730 #define TPM_ORD_SAVESTATE 152 731 #define SAVESTATE_RESULT_SIZE 10 732 733 static const struct tpm_input_header savestate_header = { 734 .tag = cpu_to_be16(TPM_TAG_RQU_COMMAND), 735 .length = cpu_to_be32(10), 736 .ordinal = cpu_to_be32(TPM_ORD_SAVESTATE) 737 }; 738 739 /* 740 * We are about to suspend. Save the TPM state 741 * so that it can be restored. 742 */ 743 int tpm_pm_suspend(struct device *dev) 744 { 745 struct tpm_chip *chip = dev_get_drvdata(dev); 746 struct tpm_cmd_t cmd; 747 int rc, try; 748 749 u8 dummy_hash[TPM_DIGEST_SIZE] = { 0 }; 750 751 if (chip == NULL) 752 return -ENODEV; 753 754 if (chip->flags & TPM_CHIP_FLAG_ALWAYS_POWERED) 755 return 0; 756 757 if (chip->flags & TPM_CHIP_FLAG_TPM2) { 758 tpm2_shutdown(chip, TPM2_SU_STATE); 759 return 0; 760 } 761 762 /* for buggy tpm, flush pcrs with extend to selected dummy */ 763 if (tpm_suspend_pcr) 764 rc = tpm1_pcr_extend(chip, tpm_suspend_pcr, dummy_hash, 765 "extending dummy pcr before suspend"); 766 767 /* now do the actual savestate */ 768 for (try = 0; try < TPM_RETRY; try++) { 769 cmd.header.in = savestate_header; 770 rc = tpm_transmit_cmd(chip, NULL, &cmd, SAVESTATE_RESULT_SIZE, 771 0, 0, NULL); 772 773 /* 774 * If the TPM indicates that it is too busy to respond to 775 * this command then retry before giving up. It can take 776 * several seconds for this TPM to be ready. 777 * 778 * This can happen if the TPM has already been sent the 779 * SaveState command before the driver has loaded. TCG 1.2 780 * specification states that any communication after SaveState 781 * may cause the TPM to invalidate previously saved state. 782 */ 783 if (rc != TPM_WARN_RETRY) 784 break; 785 tpm_msleep(TPM_TIMEOUT_RETRY); 786 } 787 788 if (rc) 789 dev_err(&chip->dev, 790 "Error (%d) sending savestate before suspend\n", rc); 791 else if (try > 0) 792 dev_warn(&chip->dev, "TPM savestate took %dms\n", 793 try * TPM_TIMEOUT_RETRY); 794 795 return rc; 796 } 797 EXPORT_SYMBOL_GPL(tpm_pm_suspend); 798 799 /* 800 * Resume from a power safe. The BIOS already restored 801 * the TPM state. 802 */ 803 int tpm_pm_resume(struct device *dev) 804 { 805 struct tpm_chip *chip = dev_get_drvdata(dev); 806 807 if (chip == NULL) 808 return -ENODEV; 809 810 return 0; 811 } 812 EXPORT_SYMBOL_GPL(tpm_pm_resume); 813 814 /** 815 * tpm_get_random() - get random bytes from the TPM's RNG 816 * @chip: a &struct tpm_chip instance, %NULL for the default chip 817 * @out: destination buffer for the random bytes 818 * @max: the max number of bytes to write to @out 819 * 820 * Return: same as with tpm_transmit_cmd() 821 */ 822 int tpm_get_random(struct tpm_chip *chip, u8 *out, size_t max) 823 { 824 int rc; 825 826 if (!out || max > TPM_MAX_RNG_DATA) 827 return -EINVAL; 828 829 chip = tpm_find_get_ops(chip); 830 if (!chip) 831 return -ENODEV; 832 833 if (chip->flags & TPM_CHIP_FLAG_TPM2) 834 rc = tpm2_get_random(chip, out, max); 835 else 836 rc = tpm1_get_random(chip, out, max); 837 838 tpm_put_ops(chip); 839 return rc; 840 } 841 EXPORT_SYMBOL_GPL(tpm_get_random); 842 843 /** 844 * tpm_seal_trusted() - seal a trusted key payload 845 * @chip: a &struct tpm_chip instance, %NULL for the default chip 846 * @options: authentication values and other options 847 * @payload: the key data in clear and encrypted form 848 * 849 * Note: only TPM 2.0 chip are supported. TPM 1.x implementation is located in 850 * the keyring subsystem. 851 * 852 * Return: same as with tpm_transmit_cmd() 853 */ 854 int tpm_seal_trusted(struct tpm_chip *chip, struct trusted_key_payload *payload, 855 struct trusted_key_options *options) 856 { 857 int rc; 858 859 chip = tpm_find_get_ops(chip); 860 if (!chip || !(chip->flags & TPM_CHIP_FLAG_TPM2)) 861 return -ENODEV; 862 863 rc = tpm2_seal_trusted(chip, payload, options); 864 865 tpm_put_ops(chip); 866 return rc; 867 } 868 EXPORT_SYMBOL_GPL(tpm_seal_trusted); 869 870 /** 871 * tpm_unseal_trusted() - unseal a trusted key 872 * @chip: a &struct tpm_chip instance, %NULL for the default chip 873 * @options: authentication values and other options 874 * @payload: the key data in clear and encrypted form 875 * 876 * Note: only TPM 2.0 chip are supported. TPM 1.x implementation is located in 877 * the keyring subsystem. 878 * 879 * Return: same as with tpm_transmit_cmd() 880 */ 881 int tpm_unseal_trusted(struct tpm_chip *chip, 882 struct trusted_key_payload *payload, 883 struct trusted_key_options *options) 884 { 885 int rc; 886 887 chip = tpm_find_get_ops(chip); 888 if (!chip || !(chip->flags & TPM_CHIP_FLAG_TPM2)) 889 return -ENODEV; 890 891 rc = tpm2_unseal_trusted(chip, payload, options); 892 893 tpm_put_ops(chip); 894 895 return rc; 896 } 897 EXPORT_SYMBOL_GPL(tpm_unseal_trusted); 898 899 static int __init tpm_init(void) 900 { 901 int rc; 902 903 tpm_class = class_create(THIS_MODULE, "tpm"); 904 if (IS_ERR(tpm_class)) { 905 pr_err("couldn't create tpm class\n"); 906 return PTR_ERR(tpm_class); 907 } 908 909 tpmrm_class = class_create(THIS_MODULE, "tpmrm"); 910 if (IS_ERR(tpmrm_class)) { 911 pr_err("couldn't create tpmrm class\n"); 912 rc = PTR_ERR(tpmrm_class); 913 goto out_destroy_tpm_class; 914 } 915 916 rc = alloc_chrdev_region(&tpm_devt, 0, 2*TPM_NUM_DEVICES, "tpm"); 917 if (rc < 0) { 918 pr_err("tpm: failed to allocate char dev region\n"); 919 goto out_destroy_tpmrm_class; 920 } 921 922 rc = tpm_dev_common_init(); 923 if (rc) { 924 pr_err("tpm: failed to allocate char dev region\n"); 925 goto out_unreg_chrdev; 926 } 927 928 return 0; 929 930 out_unreg_chrdev: 931 unregister_chrdev_region(tpm_devt, 2 * TPM_NUM_DEVICES); 932 out_destroy_tpmrm_class: 933 class_destroy(tpmrm_class); 934 out_destroy_tpm_class: 935 class_destroy(tpm_class); 936 937 return rc; 938 } 939 940 static void __exit tpm_exit(void) 941 { 942 idr_destroy(&dev_nums_idr); 943 class_destroy(tpm_class); 944 class_destroy(tpmrm_class); 945 unregister_chrdev_region(tpm_devt, 2*TPM_NUM_DEVICES); 946 tpm_dev_common_exit(); 947 } 948 949 subsys_initcall(tpm_init); 950 module_exit(tpm_exit); 951 952 MODULE_AUTHOR("Leendert van Doorn (leendert@watson.ibm.com)"); 953 MODULE_DESCRIPTION("TPM Driver"); 954 MODULE_VERSION("2.0"); 955 MODULE_LICENSE("GPL"); 956