1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * Copyright (C) 2004 IBM Corporation 4 * Copyright (C) 2014 Intel Corporation 5 * 6 * Authors: 7 * Leendert van Doorn <leendert@watson.ibm.com> 8 * Dave Safford <safford@watson.ibm.com> 9 * Reiner Sailer <sailer@watson.ibm.com> 10 * Kylene Hall <kjhall@us.ibm.com> 11 * 12 * Device driver for TCG/TCPA TPM (trusted platform module). 13 * Specifications at www.trustedcomputinggroup.org 14 */ 15 16 #include <linux/poll.h> 17 #include <linux/slab.h> 18 #include <linux/mutex.h> 19 #include <linux/spinlock.h> 20 #include <linux/freezer.h> 21 #include <linux/tpm_eventlog.h> 22 23 #include "tpm.h" 24 25 /* 26 * Array with one entry per ordinal defining the maximum amount 27 * of time the chip could take to return the result. The ordinal 28 * designation of short, medium or long is defined in a table in 29 * TCG Specification TPM Main Part 2 TPM Structures Section 17. The 30 * values of the SHORT, MEDIUM, and LONG durations are retrieved 31 * from the chip during initialization with a call to tpm_get_timeouts. 32 */ 33 static const u8 tpm1_ordinal_duration[TPM_MAX_ORDINAL] = { 34 TPM_UNDEFINED, /* 0 */ 35 TPM_UNDEFINED, 36 TPM_UNDEFINED, 37 TPM_UNDEFINED, 38 TPM_UNDEFINED, 39 TPM_UNDEFINED, /* 5 */ 40 TPM_UNDEFINED, 41 TPM_UNDEFINED, 42 TPM_UNDEFINED, 43 TPM_UNDEFINED, 44 TPM_SHORT, /* 10 */ 45 TPM_SHORT, 46 TPM_MEDIUM, 47 TPM_LONG, 48 TPM_LONG, 49 TPM_MEDIUM, /* 15 */ 50 TPM_SHORT, 51 TPM_SHORT, 52 TPM_MEDIUM, 53 TPM_LONG, 54 TPM_SHORT, /* 20 */ 55 TPM_SHORT, 56 TPM_MEDIUM, 57 TPM_MEDIUM, 58 TPM_MEDIUM, 59 TPM_SHORT, /* 25 */ 60 TPM_SHORT, 61 TPM_MEDIUM, 62 TPM_SHORT, 63 TPM_SHORT, 64 TPM_MEDIUM, /* 30 */ 65 TPM_LONG, 66 TPM_MEDIUM, 67 TPM_SHORT, 68 TPM_SHORT, 69 TPM_SHORT, /* 35 */ 70 TPM_MEDIUM, 71 TPM_MEDIUM, 72 TPM_UNDEFINED, 73 TPM_UNDEFINED, 74 TPM_MEDIUM, /* 40 */ 75 TPM_LONG, 76 TPM_MEDIUM, 77 TPM_SHORT, 78 TPM_SHORT, 79 TPM_SHORT, /* 45 */ 80 TPM_SHORT, 81 TPM_SHORT, 82 TPM_SHORT, 83 TPM_LONG, 84 TPM_MEDIUM, /* 50 */ 85 TPM_MEDIUM, 86 TPM_UNDEFINED, 87 TPM_UNDEFINED, 88 TPM_UNDEFINED, 89 TPM_UNDEFINED, /* 55 */ 90 TPM_UNDEFINED, 91 TPM_UNDEFINED, 92 TPM_UNDEFINED, 93 TPM_UNDEFINED, 94 TPM_MEDIUM, /* 60 */ 95 TPM_MEDIUM, 96 TPM_MEDIUM, 97 TPM_SHORT, 98 TPM_SHORT, 99 TPM_MEDIUM, /* 65 */ 100 TPM_UNDEFINED, 101 TPM_UNDEFINED, 102 TPM_UNDEFINED, 103 TPM_UNDEFINED, 104 TPM_SHORT, /* 70 */ 105 TPM_SHORT, 106 TPM_UNDEFINED, 107 TPM_UNDEFINED, 108 TPM_UNDEFINED, 109 TPM_UNDEFINED, /* 75 */ 110 TPM_UNDEFINED, 111 TPM_UNDEFINED, 112 TPM_UNDEFINED, 113 TPM_UNDEFINED, 114 TPM_LONG, /* 80 */ 115 TPM_UNDEFINED, 116 TPM_MEDIUM, 117 TPM_LONG, 118 TPM_SHORT, 119 TPM_UNDEFINED, /* 85 */ 120 TPM_UNDEFINED, 121 TPM_UNDEFINED, 122 TPM_UNDEFINED, 123 TPM_UNDEFINED, 124 TPM_SHORT, /* 90 */ 125 TPM_SHORT, 126 TPM_SHORT, 127 TPM_SHORT, 128 TPM_SHORT, 129 TPM_UNDEFINED, /* 95 */ 130 TPM_UNDEFINED, 131 TPM_UNDEFINED, 132 TPM_UNDEFINED, 133 TPM_UNDEFINED, 134 TPM_MEDIUM, /* 100 */ 135 TPM_SHORT, 136 TPM_SHORT, 137 TPM_UNDEFINED, 138 TPM_UNDEFINED, 139 TPM_UNDEFINED, /* 105 */ 140 TPM_UNDEFINED, 141 TPM_UNDEFINED, 142 TPM_UNDEFINED, 143 TPM_UNDEFINED, 144 TPM_SHORT, /* 110 */ 145 TPM_SHORT, 146 TPM_SHORT, 147 TPM_SHORT, 148 TPM_SHORT, 149 TPM_SHORT, /* 115 */ 150 TPM_SHORT, 151 TPM_SHORT, 152 TPM_UNDEFINED, 153 TPM_UNDEFINED, 154 TPM_LONG, /* 120 */ 155 TPM_LONG, 156 TPM_MEDIUM, 157 TPM_UNDEFINED, 158 TPM_SHORT, 159 TPM_SHORT, /* 125 */ 160 TPM_SHORT, 161 TPM_LONG, 162 TPM_SHORT, 163 TPM_SHORT, 164 TPM_SHORT, /* 130 */ 165 TPM_MEDIUM, 166 TPM_UNDEFINED, 167 TPM_SHORT, 168 TPM_MEDIUM, 169 TPM_UNDEFINED, /* 135 */ 170 TPM_UNDEFINED, 171 TPM_UNDEFINED, 172 TPM_UNDEFINED, 173 TPM_UNDEFINED, 174 TPM_SHORT, /* 140 */ 175 TPM_SHORT, 176 TPM_UNDEFINED, 177 TPM_UNDEFINED, 178 TPM_UNDEFINED, 179 TPM_UNDEFINED, /* 145 */ 180 TPM_UNDEFINED, 181 TPM_UNDEFINED, 182 TPM_UNDEFINED, 183 TPM_UNDEFINED, 184 TPM_SHORT, /* 150 */ 185 TPM_MEDIUM, 186 TPM_MEDIUM, 187 TPM_SHORT, 188 TPM_SHORT, 189 TPM_UNDEFINED, /* 155 */ 190 TPM_UNDEFINED, 191 TPM_UNDEFINED, 192 TPM_UNDEFINED, 193 TPM_UNDEFINED, 194 TPM_SHORT, /* 160 */ 195 TPM_SHORT, 196 TPM_SHORT, 197 TPM_SHORT, 198 TPM_UNDEFINED, 199 TPM_UNDEFINED, /* 165 */ 200 TPM_UNDEFINED, 201 TPM_UNDEFINED, 202 TPM_UNDEFINED, 203 TPM_UNDEFINED, 204 TPM_LONG, /* 170 */ 205 TPM_UNDEFINED, 206 TPM_UNDEFINED, 207 TPM_UNDEFINED, 208 TPM_UNDEFINED, 209 TPM_UNDEFINED, /* 175 */ 210 TPM_UNDEFINED, 211 TPM_UNDEFINED, 212 TPM_UNDEFINED, 213 TPM_UNDEFINED, 214 TPM_MEDIUM, /* 180 */ 215 TPM_SHORT, 216 TPM_MEDIUM, 217 TPM_MEDIUM, 218 TPM_MEDIUM, 219 TPM_MEDIUM, /* 185 */ 220 TPM_SHORT, 221 TPM_UNDEFINED, 222 TPM_UNDEFINED, 223 TPM_UNDEFINED, 224 TPM_UNDEFINED, /* 190 */ 225 TPM_UNDEFINED, 226 TPM_UNDEFINED, 227 TPM_UNDEFINED, 228 TPM_UNDEFINED, 229 TPM_UNDEFINED, /* 195 */ 230 TPM_UNDEFINED, 231 TPM_UNDEFINED, 232 TPM_UNDEFINED, 233 TPM_UNDEFINED, 234 TPM_SHORT, /* 200 */ 235 TPM_UNDEFINED, 236 TPM_UNDEFINED, 237 TPM_UNDEFINED, 238 TPM_SHORT, 239 TPM_SHORT, /* 205 */ 240 TPM_SHORT, 241 TPM_SHORT, 242 TPM_SHORT, 243 TPM_SHORT, 244 TPM_MEDIUM, /* 210 */ 245 TPM_UNDEFINED, 246 TPM_MEDIUM, 247 TPM_MEDIUM, 248 TPM_MEDIUM, 249 TPM_UNDEFINED, /* 215 */ 250 TPM_MEDIUM, 251 TPM_UNDEFINED, 252 TPM_UNDEFINED, 253 TPM_SHORT, 254 TPM_SHORT, /* 220 */ 255 TPM_SHORT, 256 TPM_SHORT, 257 TPM_SHORT, 258 TPM_SHORT, 259 TPM_UNDEFINED, /* 225 */ 260 TPM_UNDEFINED, 261 TPM_UNDEFINED, 262 TPM_UNDEFINED, 263 TPM_UNDEFINED, 264 TPM_SHORT, /* 230 */ 265 TPM_LONG, 266 TPM_MEDIUM, 267 TPM_UNDEFINED, 268 TPM_UNDEFINED, 269 TPM_UNDEFINED, /* 235 */ 270 TPM_UNDEFINED, 271 TPM_UNDEFINED, 272 TPM_UNDEFINED, 273 TPM_UNDEFINED, 274 TPM_SHORT, /* 240 */ 275 TPM_UNDEFINED, 276 TPM_MEDIUM, 277 }; 278 279 /** 280 * tpm1_calc_ordinal_duration() - calculate the maximum command duration 281 * @chip: TPM chip to use. 282 * @ordinal: TPM command ordinal. 283 * 284 * The function returns the maximum amount of time the chip could take 285 * to return the result for a particular ordinal in jiffies. 286 * 287 * Return: A maximal duration time for an ordinal in jiffies. 288 */ 289 unsigned long tpm1_calc_ordinal_duration(struct tpm_chip *chip, u32 ordinal) 290 { 291 int duration_idx = TPM_UNDEFINED; 292 int duration = 0; 293 294 /* 295 * We only have a duration table for protected commands, where the upper 296 * 16 bits are 0. For the few other ordinals the fallback will be used. 297 */ 298 if (ordinal < TPM_MAX_ORDINAL) 299 duration_idx = tpm1_ordinal_duration[ordinal]; 300 301 if (duration_idx != TPM_UNDEFINED) 302 duration = chip->duration[duration_idx]; 303 if (duration <= 0) 304 return 2 * 60 * HZ; 305 else 306 return duration; 307 } 308 309 /** 310 * tpm1_startup() - turn on the TPM 311 * @chip: TPM chip to use 312 * 313 * Normally the firmware should start the TPM. This function is provided as a 314 * workaround if this does not happen. A legal case for this could be for 315 * example when a TPM emulator is used. 316 * 317 * Return: same as tpm_transmit_cmd() 318 */ 319 static int tpm1_startup(struct tpm_chip *chip) 320 { 321 struct tpm_buf *buf __free(kfree) = NULL; 322 323 dev_info(&chip->dev, "starting up the TPM manually\n"); 324 325 buf = kzalloc(TPM_BUFSIZE, GFP_KERNEL); 326 if (!buf) 327 return -ENOMEM; 328 329 tpm_buf_init(buf, TPM_BUFSIZE); 330 tpm_buf_reset(buf, TPM_TAG_RQU_COMMAND, TPM_ORD_STARTUP); 331 tpm_buf_append_u16(buf, TPM_ST_CLEAR); 332 return tpm_transmit_cmd(chip, buf, 0, "attempting to start the TPM"); 333 } 334 335 int tpm1_get_timeouts(struct tpm_chip *chip) 336 { 337 cap_t cap; 338 unsigned long timeout_old[4], timeout_chip[4], timeout_eff[4]; 339 unsigned long durations[3]; 340 ssize_t rc; 341 342 rc = tpm1_getcap(chip, TPM_CAP_PROP_TIS_TIMEOUT, &cap, NULL, 343 sizeof(cap.timeout)); 344 if (rc == TPM_ERR_INVALID_POSTINIT) { 345 if (tpm1_startup(chip)) 346 return rc; 347 348 rc = tpm1_getcap(chip, TPM_CAP_PROP_TIS_TIMEOUT, &cap, 349 "attempting to determine the timeouts", 350 sizeof(cap.timeout)); 351 } 352 353 if (rc) { 354 dev_err(&chip->dev, "A TPM error (%zd) occurred attempting to determine the timeouts\n", 355 rc); 356 return rc; 357 } 358 359 timeout_old[0] = jiffies_to_usecs(chip->timeout_a); 360 timeout_old[1] = jiffies_to_usecs(chip->timeout_b); 361 timeout_old[2] = jiffies_to_usecs(chip->timeout_c); 362 timeout_old[3] = jiffies_to_usecs(chip->timeout_d); 363 timeout_chip[0] = be32_to_cpu(cap.timeout.a); 364 timeout_chip[1] = be32_to_cpu(cap.timeout.b); 365 timeout_chip[2] = be32_to_cpu(cap.timeout.c); 366 timeout_chip[3] = be32_to_cpu(cap.timeout.d); 367 memcpy(timeout_eff, timeout_chip, sizeof(timeout_eff)); 368 369 /* 370 * Provide ability for vendor overrides of timeout values in case 371 * of misreporting. 372 */ 373 if (chip->ops->update_timeouts) 374 chip->ops->update_timeouts(chip, timeout_eff); 375 376 if (!chip->timeout_adjusted) { 377 /* Restore default if chip reported 0 */ 378 unsigned int i; 379 380 for (i = 0; i < ARRAY_SIZE(timeout_eff); i++) { 381 if (timeout_eff[i]) 382 continue; 383 384 timeout_eff[i] = timeout_old[i]; 385 chip->timeout_adjusted = true; 386 } 387 388 if (timeout_eff[0] != 0 && timeout_eff[0] < 1000) { 389 /* timeouts in msec rather usec */ 390 for (i = 0; i != ARRAY_SIZE(timeout_eff); i++) 391 timeout_eff[i] *= 1000; 392 chip->timeout_adjusted = true; 393 } 394 } 395 396 /* Report adjusted timeouts */ 397 if (chip->timeout_adjusted) { 398 dev_info(&chip->dev, HW_ERR "Adjusting reported timeouts: A %lu->%luus B %lu->%luus C %lu->%luus D %lu->%luus\n", 399 timeout_chip[0], timeout_eff[0], 400 timeout_chip[1], timeout_eff[1], 401 timeout_chip[2], timeout_eff[2], 402 timeout_chip[3], timeout_eff[3]); 403 } 404 405 chip->timeout_a = usecs_to_jiffies(timeout_eff[0]); 406 chip->timeout_b = usecs_to_jiffies(timeout_eff[1]); 407 chip->timeout_c = usecs_to_jiffies(timeout_eff[2]); 408 chip->timeout_d = usecs_to_jiffies(timeout_eff[3]); 409 410 rc = tpm1_getcap(chip, TPM_CAP_PROP_TIS_DURATION, &cap, 411 "attempting to determine the durations", 412 sizeof(cap.duration)); 413 if (rc) 414 return rc; 415 416 chip->duration[TPM_SHORT] = 417 usecs_to_jiffies(be32_to_cpu(cap.duration.tpm_short)); 418 chip->duration[TPM_MEDIUM] = 419 usecs_to_jiffies(be32_to_cpu(cap.duration.tpm_medium)); 420 chip->duration[TPM_LONG] = 421 usecs_to_jiffies(be32_to_cpu(cap.duration.tpm_long)); 422 chip->duration[TPM_LONG_LONG] = 0; /* not used under 1.2 */ 423 424 /* 425 * Provide the ability for vendor overrides of duration values in case 426 * of misreporting. 427 */ 428 if (chip->ops->update_durations) 429 chip->ops->update_durations(chip, durations); 430 431 if (chip->duration_adjusted) { 432 dev_info(&chip->dev, HW_ERR "Adjusting reported durations."); 433 chip->duration[TPM_SHORT] = durations[0]; 434 chip->duration[TPM_MEDIUM] = durations[1]; 435 chip->duration[TPM_LONG] = durations[2]; 436 } 437 438 /* The Broadcom BCM0102 chipset in a Dell Latitude D820 gets the above 439 * value wrong and apparently reports msecs rather than usecs. So we 440 * fix up the resulting too-small TPM_SHORT value to make things work. 441 * We also scale the TPM_MEDIUM and -_LONG values by 1000. 442 */ 443 if (chip->duration[TPM_SHORT] < (HZ / 100)) { 444 chip->duration[TPM_SHORT] = HZ; 445 chip->duration[TPM_MEDIUM] *= 1000; 446 chip->duration[TPM_LONG] *= 1000; 447 chip->duration_adjusted = true; 448 dev_info(&chip->dev, "Adjusting TPM timeout parameters."); 449 } 450 451 chip->flags |= TPM_CHIP_FLAG_HAVE_TIMEOUTS; 452 return 0; 453 } 454 455 int tpm1_pcr_extend(struct tpm_chip *chip, u32 pcr_idx, const u8 *hash, 456 const char *log_msg) 457 { 458 struct tpm_buf *buf __free(kfree) = kzalloc(TPM_BUFSIZE, GFP_KERNEL); 459 if (!buf) 460 return -ENOMEM; 461 462 tpm_buf_init(buf, TPM_BUFSIZE); 463 tpm_buf_reset(buf, TPM_TAG_RQU_COMMAND, TPM_ORD_PCR_EXTEND); 464 tpm_buf_append_u32(buf, pcr_idx); 465 tpm_buf_append(buf, hash, TPM_DIGEST_SIZE); 466 467 return tpm_transmit_cmd(chip, buf, TPM_DIGEST_SIZE, log_msg); 468 } 469 470 ssize_t tpm1_getcap(struct tpm_chip *chip, u32 subcap_id, cap_t *cap, 471 const char *desc, size_t min_cap_length) 472 { 473 int rc; 474 475 struct tpm_buf *buf __free(kfree) = kzalloc(TPM_BUFSIZE, GFP_KERNEL); 476 if (!buf) 477 return -ENOMEM; 478 479 tpm_buf_init(buf, TPM_BUFSIZE); 480 tpm_buf_reset(buf, TPM_TAG_RQU_COMMAND, TPM_ORD_GET_CAP); 481 482 if (subcap_id == TPM_CAP_VERSION_1_1 || 483 subcap_id == TPM_CAP_VERSION_1_2) { 484 tpm_buf_append_u32(buf, subcap_id); 485 tpm_buf_append_u32(buf, 0); 486 } else { 487 if (subcap_id == TPM_CAP_FLAG_PERM || 488 subcap_id == TPM_CAP_FLAG_VOL) 489 tpm_buf_append_u32(buf, TPM_CAP_FLAG); 490 else 491 tpm_buf_append_u32(buf, TPM_CAP_PROP); 492 493 tpm_buf_append_u32(buf, 4); 494 tpm_buf_append_u32(buf, subcap_id); 495 } 496 497 rc = tpm_transmit_cmd(chip, buf, min_cap_length, desc); 498 if (!rc) 499 *cap = *(cap_t *)&buf->data[TPM_HEADER_SIZE + 4]; 500 501 return rc; 502 } 503 EXPORT_SYMBOL_GPL(tpm1_getcap); 504 505 /** 506 * tpm1_get_random() - get random bytes from the TPM's RNG 507 * @chip: a &struct tpm_chip instance 508 * @dest: destination buffer for the random bytes 509 * @max: the maximum number of bytes to write to @dest 510 * 511 * Return: 512 * * number of bytes read 513 * * -errno (positive TPM return codes are masked to -EIO) 514 */ 515 int tpm1_get_random(struct tpm_chip *chip, u8 *dest, size_t max) 516 { 517 struct tpm1_get_random_out *out; 518 struct tpm_buf *buf __free(kfree) = NULL; 519 u32 num_bytes = min_t(u32, max, TPM_MAX_RNG_DATA); 520 u32 total = 0; 521 int retries = 5; 522 u32 recd; 523 int rc; 524 525 buf = kzalloc(TPM_BUFSIZE, GFP_KERNEL); 526 if (!buf) 527 return -ENOMEM; 528 529 tpm_buf_init(buf, TPM_BUFSIZE); 530 tpm_buf_reset(buf, TPM_TAG_RQU_COMMAND, TPM_ORD_GET_RANDOM); 531 532 do { 533 tpm_buf_append_u32(buf, num_bytes); 534 535 rc = tpm_transmit_cmd(chip, buf, sizeof(out->rng_data_len), 536 "attempting get random"); 537 if (rc) { 538 if (rc > 0) 539 rc = -EIO; 540 return rc; 541 } 542 543 out = (struct tpm1_get_random_out *)&buf->data[TPM_HEADER_SIZE]; 544 545 recd = be32_to_cpu(out->rng_data_len); 546 if (recd > num_bytes) 547 return -EFAULT; 548 549 if (tpm_buf_length(buf) < TPM_HEADER_SIZE + 550 sizeof(out->rng_data_len) + recd) 551 return -EFAULT; 552 553 memcpy(dest, out->rng_data, recd); 554 555 dest += recd; 556 total += recd; 557 num_bytes -= recd; 558 559 tpm_buf_reset(buf, TPM_TAG_RQU_COMMAND, TPM_ORD_GET_RANDOM); 560 } while (retries-- && total < max); 561 562 rc = total ? (int)total : -EIO; 563 564 return rc; 565 } 566 567 int tpm1_pcr_read(struct tpm_chip *chip, u32 pcr_idx, u8 *res_buf) 568 { 569 int rc; 570 571 struct tpm_buf *buf __free(kfree) = kzalloc(TPM_BUFSIZE, GFP_KERNEL); 572 if (!buf) 573 return -ENOMEM; 574 575 tpm_buf_init(buf, TPM_BUFSIZE); 576 tpm_buf_reset(buf, TPM_TAG_RQU_COMMAND, TPM_ORD_PCR_READ); 577 tpm_buf_append_u32(buf, pcr_idx); 578 579 rc = tpm_transmit_cmd(chip, buf, TPM_DIGEST_SIZE, 580 "attempting to read a pcr value"); 581 if (rc) 582 return rc; 583 584 if (tpm_buf_length(buf) < TPM_DIGEST_SIZE) 585 return -EFAULT; 586 587 memcpy(res_buf, &buf->data[TPM_HEADER_SIZE], TPM_DIGEST_SIZE); 588 589 return rc; 590 } 591 592 /** 593 * tpm1_continue_selftest() - run TPM's selftest 594 * @chip: TPM chip to use 595 * 596 * Returns 0 on success, < 0 in case of fatal error or a value > 0 representing 597 * a TPM error code. 598 */ 599 static int tpm1_continue_selftest(struct tpm_chip *chip) 600 { 601 struct tpm_buf *buf __free(kfree) = kzalloc(TPM_BUFSIZE, GFP_KERNEL); 602 if (!buf) 603 return -ENOMEM; 604 605 tpm_buf_init(buf, TPM_BUFSIZE); 606 tpm_buf_reset(buf, TPM_TAG_RQU_COMMAND, TPM_ORD_CONTINUE_SELFTEST); 607 return tpm_transmit_cmd(chip, buf, 0, "continue selftest"); 608 } 609 610 /** 611 * tpm1_do_selftest - have the TPM continue its selftest and wait until it 612 * can receive further commands 613 * @chip: TPM chip to use 614 * 615 * Returns 0 on success, < 0 in case of fatal error or a value > 0 representing 616 * a TPM error code. 617 */ 618 int tpm1_do_selftest(struct tpm_chip *chip) 619 { 620 int rc; 621 unsigned int loops; 622 unsigned int delay_msec = 100; 623 unsigned long duration; 624 u8 dummy[TPM_DIGEST_SIZE]; 625 626 duration = tpm1_calc_ordinal_duration(chip, TPM_ORD_CONTINUE_SELFTEST); 627 628 loops = jiffies_to_msecs(duration) / delay_msec; 629 630 rc = tpm1_continue_selftest(chip); 631 if (rc == TPM_ERR_INVALID_POSTINIT) { 632 chip->flags |= TPM_CHIP_FLAG_ALWAYS_POWERED; 633 dev_info(&chip->dev, "TPM not ready (%d)\n", rc); 634 } 635 /* This may fail if there was no TPM driver during a suspend/resume 636 * cycle; some may return 10 (BAD_ORDINAL), others 28 (FAILEDSELFTEST) 637 */ 638 if (rc) 639 return rc; 640 641 do { 642 /* Attempt to read a PCR value */ 643 rc = tpm1_pcr_read(chip, 0, dummy); 644 645 /* Some buggy TPMs will not respond to tpm_tis_ready() for 646 * around 300ms while the self test is ongoing, keep trying 647 * until the self test duration expires. 648 */ 649 if (rc == -ETIME) { 650 dev_info(&chip->dev, HW_ERR "TPM command timed out during continue self test"); 651 tpm_msleep(delay_msec); 652 continue; 653 } 654 655 if (rc == TPM_ERR_DISABLED || rc == TPM_ERR_DEACTIVATED) { 656 dev_info(&chip->dev, "TPM is disabled/deactivated (0x%X)\n", 657 rc); 658 /* TPM is disabled and/or deactivated; driver can 659 * proceed and TPM does handle commands for 660 * suspend/resume correctly 661 */ 662 return 0; 663 } 664 if (rc != TPM_WARN_DOING_SELFTEST) 665 return rc; 666 tpm_msleep(delay_msec); 667 } while (--loops > 0); 668 669 return rc; 670 } 671 EXPORT_SYMBOL_GPL(tpm1_do_selftest); 672 673 /** 674 * tpm1_auto_startup - Perform the standard automatic TPM initialization 675 * sequence 676 * @chip: TPM chip to use 677 * 678 * Returns 0 on success, < 0 in case of fatal error. 679 */ 680 int tpm1_auto_startup(struct tpm_chip *chip) 681 { 682 int rc; 683 684 rc = tpm1_get_timeouts(chip); 685 if (rc) 686 goto out; 687 rc = tpm1_do_selftest(chip); 688 if (rc == TPM_ERR_FAILEDSELFTEST) { 689 dev_warn(&chip->dev, "TPM self test failed, switching to the firmware upgrade mode\n"); 690 /* A TPM in this state possibly allows or needs a firmware upgrade */ 691 chip->flags |= TPM_CHIP_FLAG_FIRMWARE_UPGRADE; 692 return 0; 693 } else if (rc) { 694 dev_err(&chip->dev, "TPM self test failed\n"); 695 goto out; 696 } 697 698 return rc; 699 out: 700 if (rc > 0) 701 rc = -ENODEV; 702 return rc; 703 } 704 705 /** 706 * tpm1_pm_suspend() - pm suspend handler 707 * @chip: TPM chip to use. 708 * @tpm_suspend_pcr: flush pcr for buggy TPM chips. 709 * 710 * The functions saves the TPM state to be restored on resume. 711 * 712 * Return: 713 * * 0 on success, 714 * * < 0 on error. 715 */ 716 int tpm1_pm_suspend(struct tpm_chip *chip, u32 tpm_suspend_pcr) 717 { 718 u8 dummy_hash[TPM_DIGEST_SIZE] = { 0 }; 719 unsigned int try; 720 struct tpm_buf *buf __free(kfree) = NULL; 721 int rc; 722 723 /* for buggy tpm, flush pcrs with extend to selected dummy */ 724 if (tpm_suspend_pcr) 725 rc = tpm1_pcr_extend(chip, tpm_suspend_pcr, dummy_hash, 726 "extending dummy pcr before suspend"); 727 buf = kzalloc(TPM_BUFSIZE, GFP_KERNEL); 728 if (!buf) 729 return -ENOMEM; 730 731 tpm_buf_init(buf, TPM_BUFSIZE); 732 tpm_buf_reset(buf, TPM_TAG_RQU_COMMAND, TPM_ORD_SAVESTATE); 733 734 /* now do the actual savestate */ 735 for (try = 0; try < TPM_RETRY; try++) { 736 rc = tpm_transmit_cmd(chip, buf, 0, NULL); 737 /* 738 * If the TPM indicates that it is too busy to respond to 739 * this command then retry before giving up. It can take 740 * several seconds for this TPM to be ready. 741 * 742 * This can happen if the TPM has already been sent the 743 * SaveState command before the driver has loaded. TCG 1.2 744 * specification states that any communication after SaveState 745 * may cause the TPM to invalidate previously saved state. 746 */ 747 if (rc != TPM_WARN_RETRY) 748 break; 749 tpm_msleep(TPM_TIMEOUT_RETRY); 750 751 tpm_buf_reset(buf, TPM_TAG_RQU_COMMAND, TPM_ORD_SAVESTATE); 752 } 753 754 if (rc) 755 dev_err(&chip->dev, "Error (%d) sending savestate before suspend\n", 756 rc); 757 else if (try > 0) 758 dev_warn(&chip->dev, "TPM savestate took %dms\n", 759 try * TPM_TIMEOUT_RETRY); 760 761 return rc; 762 } 763 764 /** 765 * tpm1_get_pcr_allocation() - initialize the allocated bank 766 * @chip: TPM chip to use. 767 * 768 * The function initializes the SHA1 allocated bank to extend PCR 769 * 770 * Return: 771 * * 0 on success, 772 * * < 0 on error. 773 */ 774 int tpm1_get_pcr_allocation(struct tpm_chip *chip) 775 { 776 chip->allocated_banks[0].alg_id = TPM_ALG_SHA1; 777 chip->allocated_banks[0].digest_size = hash_digest_size[HASH_ALGO_SHA1]; 778 chip->allocated_banks[0].crypto_id = HASH_ALGO_SHA1; 779 chip->nr_allocated_banks = 1; 780 781 return 0; 782 } 783