1/* 2 * Copyright (C) 2004 IBM Corporation 3 * 4 * Authors: 5 * Leendert van Doorn <leendert@watson.ibm.com> 6 * Dave Safford <safford@watson.ibm.com> 7 * Reiner Sailer <sailer@watson.ibm.com> 8 * Kylene Hall <kjhall@us.ibm.com> 9 * 10 * Maintained by: <tpmdd-devel@lists.sourceforge.net> 11 * 12 * Device driver for TCG/TCPA TPM (trusted platform module). 13 * Specifications at www.trustedcomputinggroup.org 14 * 15 * This program is free software; you can redistribute it and/or 16 * modify it under the terms of the GNU General Public License as 17 * published by the Free Software Foundation, version 2 of the 18 * License. 19 * 20 * Note, the TPM chip is not interrupt driven (only polling) 21 * and can have very long timeouts (minutes!). Hence the unusual 22 * calls to msleep. 23 * 24 */ 25 26#include <linux/poll.h> 27#include <linux/slab.h> 28#include <linux/mutex.h> 29#include <linux/spinlock.h> 30 31#include "tpm.h" 32 33enum tpm_const { 34 TPM_MINOR = 224, /* officially assigned */ 35 TPM_BUFSIZE = 4096, 36 TPM_NUM_DEVICES = 256, 37}; 38 39enum tpm_duration { 40 TPM_SHORT = 0, 41 TPM_MEDIUM = 1, 42 TPM_LONG = 2, 43 TPM_UNDEFINED, 44}; 45 46#define TPM_MAX_ORDINAL 243 47#define TPM_MAX_PROTECTED_ORDINAL 12 48#define TPM_PROTECTED_ORDINAL_MASK 0xFF 49 50static LIST_HEAD(tpm_chip_list); 51static DEFINE_SPINLOCK(driver_lock); 52static DECLARE_BITMAP(dev_mask, TPM_NUM_DEVICES); 53 54/* 55 * Array with one entry per ordinal defining the maximum amount 56 * of time the chip could take to return the result. The ordinal 57 * designation of short, medium or long is defined in a table in 58 * TCG Specification TPM Main Part 2 TPM Structures Section 17. The 59 * values of the SHORT, MEDIUM, and LONG durations are retrieved 60 * from the chip during initialization with a call to tpm_get_timeouts. 61 */ 62static const u8 tpm_protected_ordinal_duration[TPM_MAX_PROTECTED_ORDINAL] = { 63 TPM_UNDEFINED, /* 0 */ 64 TPM_UNDEFINED, 65 TPM_UNDEFINED, 66 TPM_UNDEFINED, 67 TPM_UNDEFINED, 68 TPM_UNDEFINED, /* 5 */ 69 TPM_UNDEFINED, 70 TPM_UNDEFINED, 71 TPM_UNDEFINED, 72 TPM_UNDEFINED, 73 TPM_SHORT, /* 10 */ 74 TPM_SHORT, 75}; 76 77static const u8 tpm_ordinal_duration[TPM_MAX_ORDINAL] = { 78 TPM_UNDEFINED, /* 0 */ 79 TPM_UNDEFINED, 80 TPM_UNDEFINED, 81 TPM_UNDEFINED, 82 TPM_UNDEFINED, 83 TPM_UNDEFINED, /* 5 */ 84 TPM_UNDEFINED, 85 TPM_UNDEFINED, 86 TPM_UNDEFINED, 87 TPM_UNDEFINED, 88 TPM_SHORT, /* 10 */ 89 TPM_SHORT, 90 TPM_MEDIUM, 91 TPM_LONG, 92 TPM_LONG, 93 TPM_MEDIUM, /* 15 */ 94 TPM_SHORT, 95 TPM_SHORT, 96 TPM_MEDIUM, 97 TPM_LONG, 98 TPM_SHORT, /* 20 */ 99 TPM_SHORT, 100 TPM_MEDIUM, 101 TPM_MEDIUM, 102 TPM_MEDIUM, 103 TPM_SHORT, /* 25 */ 104 TPM_SHORT, 105 TPM_MEDIUM, 106 TPM_SHORT, 107 TPM_SHORT, 108 TPM_MEDIUM, /* 30 */ 109 TPM_LONG, 110 TPM_MEDIUM, 111 TPM_SHORT, 112 TPM_SHORT, 113 TPM_SHORT, /* 35 */ 114 TPM_MEDIUM, 115 TPM_MEDIUM, 116 TPM_UNDEFINED, 117 TPM_UNDEFINED, 118 TPM_MEDIUM, /* 40 */ 119 TPM_LONG, 120 TPM_MEDIUM, 121 TPM_SHORT, 122 TPM_SHORT, 123 TPM_SHORT, /* 45 */ 124 TPM_SHORT, 125 TPM_SHORT, 126 TPM_SHORT, 127 TPM_LONG, 128 TPM_MEDIUM, /* 50 */ 129 TPM_MEDIUM, 130 TPM_UNDEFINED, 131 TPM_UNDEFINED, 132 TPM_UNDEFINED, 133 TPM_UNDEFINED, /* 55 */ 134 TPM_UNDEFINED, 135 TPM_UNDEFINED, 136 TPM_UNDEFINED, 137 TPM_UNDEFINED, 138 TPM_MEDIUM, /* 60 */ 139 TPM_MEDIUM, 140 TPM_MEDIUM, 141 TPM_SHORT, 142 TPM_SHORT, 143 TPM_MEDIUM, /* 65 */ 144 TPM_UNDEFINED, 145 TPM_UNDEFINED, 146 TPM_UNDEFINED, 147 TPM_UNDEFINED, 148 TPM_SHORT, /* 70 */ 149 TPM_SHORT, 150 TPM_UNDEFINED, 151 TPM_UNDEFINED, 152 TPM_UNDEFINED, 153 TPM_UNDEFINED, /* 75 */ 154 TPM_UNDEFINED, 155 TPM_UNDEFINED, 156 TPM_UNDEFINED, 157 TPM_UNDEFINED, 158 TPM_LONG, /* 80 */ 159 TPM_UNDEFINED, 160 TPM_MEDIUM, 161 TPM_LONG, 162 TPM_SHORT, 163 TPM_UNDEFINED, /* 85 */ 164 TPM_UNDEFINED, 165 TPM_UNDEFINED, 166 TPM_UNDEFINED, 167 TPM_UNDEFINED, 168 TPM_SHORT, /* 90 */ 169 TPM_SHORT, 170 TPM_SHORT, 171 TPM_SHORT, 172 TPM_SHORT, 173 TPM_UNDEFINED, /* 95 */ 174 TPM_UNDEFINED, 175 TPM_UNDEFINED, 176 TPM_UNDEFINED, 177 TPM_UNDEFINED, 178 TPM_MEDIUM, /* 100 */ 179 TPM_SHORT, 180 TPM_SHORT, 181 TPM_UNDEFINED, 182 TPM_UNDEFINED, 183 TPM_UNDEFINED, /* 105 */ 184 TPM_UNDEFINED, 185 TPM_UNDEFINED, 186 TPM_UNDEFINED, 187 TPM_UNDEFINED, 188 TPM_SHORT, /* 110 */ 189 TPM_SHORT, 190 TPM_SHORT, 191 TPM_SHORT, 192 TPM_SHORT, 193 TPM_SHORT, /* 115 */ 194 TPM_SHORT, 195 TPM_SHORT, 196 TPM_UNDEFINED, 197 TPM_UNDEFINED, 198 TPM_LONG, /* 120 */ 199 TPM_LONG, 200 TPM_MEDIUM, 201 TPM_UNDEFINED, 202 TPM_SHORT, 203 TPM_SHORT, /* 125 */ 204 TPM_SHORT, 205 TPM_LONG, 206 TPM_SHORT, 207 TPM_SHORT, 208 TPM_SHORT, /* 130 */ 209 TPM_MEDIUM, 210 TPM_UNDEFINED, 211 TPM_SHORT, 212 TPM_MEDIUM, 213 TPM_UNDEFINED, /* 135 */ 214 TPM_UNDEFINED, 215 TPM_UNDEFINED, 216 TPM_UNDEFINED, 217 TPM_UNDEFINED, 218 TPM_SHORT, /* 140 */ 219 TPM_SHORT, 220 TPM_UNDEFINED, 221 TPM_UNDEFINED, 222 TPM_UNDEFINED, 223 TPM_UNDEFINED, /* 145 */ 224 TPM_UNDEFINED, 225 TPM_UNDEFINED, 226 TPM_UNDEFINED, 227 TPM_UNDEFINED, 228 TPM_SHORT, /* 150 */ 229 TPM_MEDIUM, 230 TPM_MEDIUM, 231 TPM_SHORT, 232 TPM_SHORT, 233 TPM_UNDEFINED, /* 155 */ 234 TPM_UNDEFINED, 235 TPM_UNDEFINED, 236 TPM_UNDEFINED, 237 TPM_UNDEFINED, 238 TPM_SHORT, /* 160 */ 239 TPM_SHORT, 240 TPM_SHORT, 241 TPM_SHORT, 242 TPM_UNDEFINED, 243 TPM_UNDEFINED, /* 165 */ 244 TPM_UNDEFINED, 245 TPM_UNDEFINED, 246 TPM_UNDEFINED, 247 TPM_UNDEFINED, 248 TPM_LONG, /* 170 */ 249 TPM_UNDEFINED, 250 TPM_UNDEFINED, 251 TPM_UNDEFINED, 252 TPM_UNDEFINED, 253 TPM_UNDEFINED, /* 175 */ 254 TPM_UNDEFINED, 255 TPM_UNDEFINED, 256 TPM_UNDEFINED, 257 TPM_UNDEFINED, 258 TPM_MEDIUM, /* 180 */ 259 TPM_SHORT, 260 TPM_MEDIUM, 261 TPM_MEDIUM, 262 TPM_MEDIUM, 263 TPM_MEDIUM, /* 185 */ 264 TPM_SHORT, 265 TPM_UNDEFINED, 266 TPM_UNDEFINED, 267 TPM_UNDEFINED, 268 TPM_UNDEFINED, /* 190 */ 269 TPM_UNDEFINED, 270 TPM_UNDEFINED, 271 TPM_UNDEFINED, 272 TPM_UNDEFINED, 273 TPM_UNDEFINED, /* 195 */ 274 TPM_UNDEFINED, 275 TPM_UNDEFINED, 276 TPM_UNDEFINED, 277 TPM_UNDEFINED, 278 TPM_SHORT, /* 200 */ 279 TPM_UNDEFINED, 280 TPM_UNDEFINED, 281 TPM_UNDEFINED, 282 TPM_SHORT, 283 TPM_SHORT, /* 205 */ 284 TPM_SHORT, 285 TPM_SHORT, 286 TPM_SHORT, 287 TPM_SHORT, 288 TPM_MEDIUM, /* 210 */ 289 TPM_UNDEFINED, 290 TPM_MEDIUM, 291 TPM_MEDIUM, 292 TPM_MEDIUM, 293 TPM_UNDEFINED, /* 215 */ 294 TPM_MEDIUM, 295 TPM_UNDEFINED, 296 TPM_UNDEFINED, 297 TPM_SHORT, 298 TPM_SHORT, /* 220 */ 299 TPM_SHORT, 300 TPM_SHORT, 301 TPM_SHORT, 302 TPM_SHORT, 303 TPM_UNDEFINED, /* 225 */ 304 TPM_UNDEFINED, 305 TPM_UNDEFINED, 306 TPM_UNDEFINED, 307 TPM_UNDEFINED, 308 TPM_SHORT, /* 230 */ 309 TPM_LONG, 310 TPM_MEDIUM, 311 TPM_UNDEFINED, 312 TPM_UNDEFINED, 313 TPM_UNDEFINED, /* 235 */ 314 TPM_UNDEFINED, 315 TPM_UNDEFINED, 316 TPM_UNDEFINED, 317 TPM_UNDEFINED, 318 TPM_SHORT, /* 240 */ 319 TPM_UNDEFINED, 320 TPM_MEDIUM, 321}; 322 323static void user_reader_timeout(unsigned long ptr) 324{ 325 struct tpm_chip *chip = (struct tpm_chip *) ptr; 326 327 schedule_work(&chip->work); 328} 329 330static void timeout_work(struct work_struct *work) 331{ 332 struct tpm_chip *chip = container_of(work, struct tpm_chip, work); 333 334 mutex_lock(&chip->buffer_mutex); 335 atomic_set(&chip->data_pending, 0); 336 memset(chip->data_buffer, 0, TPM_BUFSIZE); 337 mutex_unlock(&chip->buffer_mutex); 338} 339 340/* 341 * Returns max number of jiffies to wait 342 */ 343unsigned long tpm_calc_ordinal_duration(struct tpm_chip *chip, 344 u32 ordinal) 345{ 346 int duration_idx = TPM_UNDEFINED; 347 int duration = 0; 348 349 if (ordinal < TPM_MAX_ORDINAL) 350 duration_idx = tpm_ordinal_duration[ordinal]; 351 else if ((ordinal & TPM_PROTECTED_ORDINAL_MASK) < 352 TPM_MAX_PROTECTED_ORDINAL) 353 duration_idx = 354 tpm_protected_ordinal_duration[ordinal & 355 TPM_PROTECTED_ORDINAL_MASK]; 356 357 if (duration_idx != TPM_UNDEFINED) { 358 duration = chip->vendor.duration[duration_idx]; 359 /* if duration is 0, it's because chip->vendor.duration wasn't */ 360 /* filled yet, so we set the lowest timeout just to give enough */ 361 /* time for tpm_get_timeouts() to succeed */ 362 return (duration <= 0 ? HZ : duration); 363 } else 364 return 2 * 60 * HZ; 365} 366EXPORT_SYMBOL_GPL(tpm_calc_ordinal_duration); 367 368/* 369 * Internal kernel interface to transmit TPM commands 370 */ 371static ssize_t tpm_transmit(struct tpm_chip *chip, const char *buf, 372 size_t bufsiz) 373{ 374 ssize_t rc; 375 u32 count, ordinal; 376 unsigned long stop; 377 378 count = be32_to_cpu(*((__be32 *) (buf + 2))); 379 ordinal = be32_to_cpu(*((__be32 *) (buf + 6))); 380 if (count == 0) 381 return -ENODATA; 382 if (count > bufsiz) { 383 dev_err(chip->dev, 384 "invalid count value %x %zx \n", count, bufsiz); 385 return -E2BIG; 386 } 387 388 mutex_lock(&chip->tpm_mutex); 389 390 if ((rc = chip->vendor.send(chip, (u8 *) buf, count)) < 0) { 391 dev_err(chip->dev, 392 "tpm_transmit: tpm_send: error %zd\n", rc); 393 goto out; 394 } 395 396 if (chip->vendor.irq) 397 goto out_recv; 398 399 stop = jiffies + tpm_calc_ordinal_duration(chip, ordinal); 400 do { 401 u8 status = chip->vendor.status(chip); 402 if ((status & chip->vendor.req_complete_mask) == 403 chip->vendor.req_complete_val) 404 goto out_recv; 405 406 if ((status == chip->vendor.req_canceled)) { 407 dev_err(chip->dev, "Operation Canceled\n"); 408 rc = -ECANCELED; 409 goto out; 410 } 411 412 msleep(TPM_TIMEOUT); /* CHECK */ 413 rmb(); 414 } while (time_before(jiffies, stop)); 415 416 chip->vendor.cancel(chip); 417 dev_err(chip->dev, "Operation Timed out\n"); 418 rc = -ETIME; 419 goto out; 420 421out_recv: 422 rc = chip->vendor.recv(chip, (u8 *) buf, bufsiz); 423 if (rc < 0) 424 dev_err(chip->dev, 425 "tpm_transmit: tpm_recv: error %zd\n", rc); 426out: 427 mutex_unlock(&chip->tpm_mutex); 428 return rc; 429} 430 431#define TPM_DIGEST_SIZE 20 432#define TPM_ERROR_SIZE 10 433#define TPM_RET_CODE_IDX 6 434 435enum tpm_capabilities { 436 TPM_CAP_FLAG = cpu_to_be32(4), 437 TPM_CAP_PROP = cpu_to_be32(5), 438 CAP_VERSION_1_1 = cpu_to_be32(0x06), 439 CAP_VERSION_1_2 = cpu_to_be32(0x1A) 440}; 441 442enum tpm_sub_capabilities { 443 TPM_CAP_PROP_PCR = cpu_to_be32(0x101), 444 TPM_CAP_PROP_MANUFACTURER = cpu_to_be32(0x103), 445 TPM_CAP_FLAG_PERM = cpu_to_be32(0x108), 446 TPM_CAP_FLAG_VOL = cpu_to_be32(0x109), 447 TPM_CAP_PROP_OWNER = cpu_to_be32(0x111), 448 TPM_CAP_PROP_TIS_TIMEOUT = cpu_to_be32(0x115), 449 TPM_CAP_PROP_TIS_DURATION = cpu_to_be32(0x120), 450 451}; 452 453static ssize_t transmit_cmd(struct tpm_chip *chip, struct tpm_cmd_t *cmd, 454 int len, const char *desc) 455{ 456 int err; 457 458 len = tpm_transmit(chip,(u8 *) cmd, len); 459 if (len < 0) 460 return len; 461 if (len == TPM_ERROR_SIZE) { 462 err = be32_to_cpu(cmd->header.out.return_code); 463 dev_dbg(chip->dev, "A TPM error (%d) occurred %s\n", err, desc); 464 return err; 465 } 466 return 0; 467} 468 469#define TPM_INTERNAL_RESULT_SIZE 200 470#define TPM_TAG_RQU_COMMAND cpu_to_be16(193) 471#define TPM_ORD_GET_CAP cpu_to_be32(101) 472 473static const struct tpm_input_header tpm_getcap_header = { 474 .tag = TPM_TAG_RQU_COMMAND, 475 .length = cpu_to_be32(22), 476 .ordinal = TPM_ORD_GET_CAP 477}; 478 479ssize_t tpm_getcap(struct device *dev, __be32 subcap_id, cap_t *cap, 480 const char *desc) 481{ 482 struct tpm_cmd_t tpm_cmd; 483 int rc; 484 struct tpm_chip *chip = dev_get_drvdata(dev); 485 486 tpm_cmd.header.in = tpm_getcap_header; 487 if (subcap_id == CAP_VERSION_1_1 || subcap_id == CAP_VERSION_1_2) { 488 tpm_cmd.params.getcap_in.cap = subcap_id; 489 /*subcap field not necessary */ 490 tpm_cmd.params.getcap_in.subcap_size = cpu_to_be32(0); 491 tpm_cmd.header.in.length -= cpu_to_be32(sizeof(__be32)); 492 } else { 493 if (subcap_id == TPM_CAP_FLAG_PERM || 494 subcap_id == TPM_CAP_FLAG_VOL) 495 tpm_cmd.params.getcap_in.cap = TPM_CAP_FLAG; 496 else 497 tpm_cmd.params.getcap_in.cap = TPM_CAP_PROP; 498 tpm_cmd.params.getcap_in.subcap_size = cpu_to_be32(4); 499 tpm_cmd.params.getcap_in.subcap = subcap_id; 500 } 501 rc = transmit_cmd(chip, &tpm_cmd, TPM_INTERNAL_RESULT_SIZE, desc); 502 if (!rc) 503 *cap = tpm_cmd.params.getcap_out.cap; 504 return rc; 505} 506 507void tpm_gen_interrupt(struct tpm_chip *chip) 508{ 509 struct tpm_cmd_t tpm_cmd; 510 ssize_t rc; 511 512 tpm_cmd.header.in = tpm_getcap_header; 513 tpm_cmd.params.getcap_in.cap = TPM_CAP_PROP; 514 tpm_cmd.params.getcap_in.subcap_size = cpu_to_be32(4); 515 tpm_cmd.params.getcap_in.subcap = TPM_CAP_PROP_TIS_TIMEOUT; 516 517 rc = transmit_cmd(chip, &tpm_cmd, TPM_INTERNAL_RESULT_SIZE, 518 "attempting to determine the timeouts"); 519} 520EXPORT_SYMBOL_GPL(tpm_gen_interrupt); 521 522void tpm_get_timeouts(struct tpm_chip *chip) 523{ 524 struct tpm_cmd_t tpm_cmd; 525 struct timeout_t *timeout_cap; 526 struct duration_t *duration_cap; 527 ssize_t rc; 528 u32 timeout; 529 530 tpm_cmd.header.in = tpm_getcap_header; 531 tpm_cmd.params.getcap_in.cap = TPM_CAP_PROP; 532 tpm_cmd.params.getcap_in.subcap_size = cpu_to_be32(4); 533 tpm_cmd.params.getcap_in.subcap = TPM_CAP_PROP_TIS_TIMEOUT; 534 535 rc = transmit_cmd(chip, &tpm_cmd, TPM_INTERNAL_RESULT_SIZE, 536 "attempting to determine the timeouts"); 537 if (rc) 538 goto duration; 539 540 if (be32_to_cpu(tpm_cmd.header.out.length) 541 != 4 * sizeof(u32)) 542 goto duration; 543 544 timeout_cap = &tpm_cmd.params.getcap_out.cap.timeout; 545 /* Don't overwrite default if value is 0 */ 546 timeout = be32_to_cpu(timeout_cap->a); 547 if (timeout) 548 chip->vendor.timeout_a = usecs_to_jiffies(timeout); 549 timeout = be32_to_cpu(timeout_cap->b); 550 if (timeout) 551 chip->vendor.timeout_b = usecs_to_jiffies(timeout); 552 timeout = be32_to_cpu(timeout_cap->c); 553 if (timeout) 554 chip->vendor.timeout_c = usecs_to_jiffies(timeout); 555 timeout = be32_to_cpu(timeout_cap->d); 556 if (timeout) 557 chip->vendor.timeout_d = usecs_to_jiffies(timeout); 558 559duration: 560 tpm_cmd.header.in = tpm_getcap_header; 561 tpm_cmd.params.getcap_in.cap = TPM_CAP_PROP; 562 tpm_cmd.params.getcap_in.subcap_size = cpu_to_be32(4); 563 tpm_cmd.params.getcap_in.subcap = TPM_CAP_PROP_TIS_DURATION; 564 565 rc = transmit_cmd(chip, &tpm_cmd, TPM_INTERNAL_RESULT_SIZE, 566 "attempting to determine the durations"); 567 if (rc) 568 return; 569 570 if (be32_to_cpu(tpm_cmd.header.out.return_code) != 0 || 571 be32_to_cpu(tpm_cmd.header.out.length) 572 != sizeof(tpm_cmd.header.out) + sizeof(u32) + 3 * sizeof(u32)) 573 return; 574 575 duration_cap = &tpm_cmd.params.getcap_out.cap.duration; 576 chip->vendor.duration[TPM_SHORT] = 577 usecs_to_jiffies(be32_to_cpu(duration_cap->tpm_short)); 578 /* The Broadcom BCM0102 chipset in a Dell Latitude D820 gets the above 579 * value wrong and apparently reports msecs rather than usecs. So we 580 * fix up the resulting too-small TPM_SHORT value to make things work. 581 */ 582 if (chip->vendor.duration[TPM_SHORT] < (HZ/100)) 583 chip->vendor.duration[TPM_SHORT] = HZ; 584 585 chip->vendor.duration[TPM_MEDIUM] = 586 usecs_to_jiffies(be32_to_cpu(duration_cap->tpm_medium)); 587 chip->vendor.duration[TPM_LONG] = 588 usecs_to_jiffies(be32_to_cpu(duration_cap->tpm_long)); 589} 590EXPORT_SYMBOL_GPL(tpm_get_timeouts); 591 592void tpm_continue_selftest(struct tpm_chip *chip) 593{ 594 u8 data[] = { 595 0, 193, /* TPM_TAG_RQU_COMMAND */ 596 0, 0, 0, 10, /* length */ 597 0, 0, 0, 83, /* TPM_ORD_GetCapability */ 598 }; 599 600 tpm_transmit(chip, data, sizeof(data)); 601} 602EXPORT_SYMBOL_GPL(tpm_continue_selftest); 603 604ssize_t tpm_show_enabled(struct device * dev, struct device_attribute * attr, 605 char *buf) 606{ 607 cap_t cap; 608 ssize_t rc; 609 610 rc = tpm_getcap(dev, TPM_CAP_FLAG_PERM, &cap, 611 "attempting to determine the permanent enabled state"); 612 if (rc) 613 return 0; 614 615 rc = sprintf(buf, "%d\n", !cap.perm_flags.disable); 616 return rc; 617} 618EXPORT_SYMBOL_GPL(tpm_show_enabled); 619 620ssize_t tpm_show_active(struct device * dev, struct device_attribute * attr, 621 char *buf) 622{ 623 cap_t cap; 624 ssize_t rc; 625 626 rc = tpm_getcap(dev, TPM_CAP_FLAG_PERM, &cap, 627 "attempting to determine the permanent active state"); 628 if (rc) 629 return 0; 630 631 rc = sprintf(buf, "%d\n", !cap.perm_flags.deactivated); 632 return rc; 633} 634EXPORT_SYMBOL_GPL(tpm_show_active); 635 636ssize_t tpm_show_owned(struct device * dev, struct device_attribute * attr, 637 char *buf) 638{ 639 cap_t cap; 640 ssize_t rc; 641 642 rc = tpm_getcap(dev, TPM_CAP_PROP_OWNER, &cap, 643 "attempting to determine the owner state"); 644 if (rc) 645 return 0; 646 647 rc = sprintf(buf, "%d\n", cap.owned); 648 return rc; 649} 650EXPORT_SYMBOL_GPL(tpm_show_owned); 651 652ssize_t tpm_show_temp_deactivated(struct device * dev, 653 struct device_attribute * attr, char *buf) 654{ 655 cap_t cap; 656 ssize_t rc; 657 658 rc = tpm_getcap(dev, TPM_CAP_FLAG_VOL, &cap, 659 "attempting to determine the temporary state"); 660 if (rc) 661 return 0; 662 663 rc = sprintf(buf, "%d\n", cap.stclear_flags.deactivated); 664 return rc; 665} 666EXPORT_SYMBOL_GPL(tpm_show_temp_deactivated); 667 668/* 669 * tpm_chip_find_get - return tpm_chip for given chip number 670 */ 671static struct tpm_chip *tpm_chip_find_get(int chip_num) 672{ 673 struct tpm_chip *pos, *chip = NULL; 674 675 rcu_read_lock(); 676 list_for_each_entry_rcu(pos, &tpm_chip_list, list) { 677 if (chip_num != TPM_ANY_NUM && chip_num != pos->dev_num) 678 continue; 679 680 if (try_module_get(pos->dev->driver->owner)) { 681 chip = pos; 682 break; 683 } 684 } 685 rcu_read_unlock(); 686 return chip; 687} 688 689#define TPM_ORDINAL_PCRREAD cpu_to_be32(21) 690#define READ_PCR_RESULT_SIZE 30 691static struct tpm_input_header pcrread_header = { 692 .tag = TPM_TAG_RQU_COMMAND, 693 .length = cpu_to_be32(14), 694 .ordinal = TPM_ORDINAL_PCRREAD 695}; 696 697int __tpm_pcr_read(struct tpm_chip *chip, int pcr_idx, u8 *res_buf) 698{ 699 int rc; 700 struct tpm_cmd_t cmd; 701 702 cmd.header.in = pcrread_header; 703 cmd.params.pcrread_in.pcr_idx = cpu_to_be32(pcr_idx); 704 rc = transmit_cmd(chip, &cmd, READ_PCR_RESULT_SIZE, 705 "attempting to read a pcr value"); 706 707 if (rc == 0) 708 memcpy(res_buf, cmd.params.pcrread_out.pcr_result, 709 TPM_DIGEST_SIZE); 710 return rc; 711} 712 713/** 714 * tpm_pcr_read - read a pcr value 715 * @chip_num: tpm idx # or ANY 716 * @pcr_idx: pcr idx to retrieve 717 * @res_buf: TPM_PCR value 718 * size of res_buf is 20 bytes (or NULL if you don't care) 719 * 720 * The TPM driver should be built-in, but for whatever reason it 721 * isn't, protect against the chip disappearing, by incrementing 722 * the module usage count. 723 */ 724int tpm_pcr_read(u32 chip_num, int pcr_idx, u8 *res_buf) 725{ 726 struct tpm_chip *chip; 727 int rc; 728 729 chip = tpm_chip_find_get(chip_num); 730 if (chip == NULL) 731 return -ENODEV; 732 rc = __tpm_pcr_read(chip, pcr_idx, res_buf); 733 module_put(chip->dev->driver->owner); 734 return rc; 735} 736EXPORT_SYMBOL_GPL(tpm_pcr_read); 737 738/** 739 * tpm_pcr_extend - extend pcr value with hash 740 * @chip_num: tpm idx # or AN& 741 * @pcr_idx: pcr idx to extend 742 * @hash: hash value used to extend pcr value 743 * 744 * The TPM driver should be built-in, but for whatever reason it 745 * isn't, protect against the chip disappearing, by incrementing 746 * the module usage count. 747 */ 748#define TPM_ORD_PCR_EXTEND cpu_to_be32(20) 749#define EXTEND_PCR_RESULT_SIZE 34 750static struct tpm_input_header pcrextend_header = { 751 .tag = TPM_TAG_RQU_COMMAND, 752 .length = cpu_to_be32(34), 753 .ordinal = TPM_ORD_PCR_EXTEND 754}; 755 756int tpm_pcr_extend(u32 chip_num, int pcr_idx, const u8 *hash) 757{ 758 struct tpm_cmd_t cmd; 759 int rc; 760 struct tpm_chip *chip; 761 762 chip = tpm_chip_find_get(chip_num); 763 if (chip == NULL) 764 return -ENODEV; 765 766 cmd.header.in = pcrextend_header; 767 cmd.params.pcrextend_in.pcr_idx = cpu_to_be32(pcr_idx); 768 memcpy(cmd.params.pcrextend_in.hash, hash, TPM_DIGEST_SIZE); 769 rc = transmit_cmd(chip, &cmd, EXTEND_PCR_RESULT_SIZE, 770 "attempting extend a PCR value"); 771 772 module_put(chip->dev->driver->owner); 773 return rc; 774} 775EXPORT_SYMBOL_GPL(tpm_pcr_extend); 776 777ssize_t tpm_show_pcrs(struct device *dev, struct device_attribute *attr, 778 char *buf) 779{ 780 cap_t cap; 781 u8 digest[TPM_DIGEST_SIZE]; 782 ssize_t rc; 783 int i, j, num_pcrs; 784 char *str = buf; 785 struct tpm_chip *chip = dev_get_drvdata(dev); 786 787 rc = tpm_getcap(dev, TPM_CAP_PROP_PCR, &cap, 788 "attempting to determine the number of PCRS"); 789 if (rc) 790 return 0; 791 792 num_pcrs = be32_to_cpu(cap.num_pcrs); 793 for (i = 0; i < num_pcrs; i++) { 794 rc = __tpm_pcr_read(chip, i, digest); 795 if (rc) 796 break; 797 str += sprintf(str, "PCR-%02d: ", i); 798 for (j = 0; j < TPM_DIGEST_SIZE; j++) 799 str += sprintf(str, "%02X ", digest[j]); 800 str += sprintf(str, "\n"); 801 } 802 return str - buf; 803} 804EXPORT_SYMBOL_GPL(tpm_show_pcrs); 805 806#define READ_PUBEK_RESULT_SIZE 314 807#define TPM_ORD_READPUBEK cpu_to_be32(124) 808struct tpm_input_header tpm_readpubek_header = { 809 .tag = TPM_TAG_RQU_COMMAND, 810 .length = cpu_to_be32(30), 811 .ordinal = TPM_ORD_READPUBEK 812}; 813 814ssize_t tpm_show_pubek(struct device *dev, struct device_attribute *attr, 815 char *buf) 816{ 817 u8 *data; 818 struct tpm_cmd_t tpm_cmd; 819 ssize_t err; 820 int i, rc; 821 char *str = buf; 822 823 struct tpm_chip *chip = dev_get_drvdata(dev); 824 825 tpm_cmd.header.in = tpm_readpubek_header; 826 err = transmit_cmd(chip, &tpm_cmd, READ_PUBEK_RESULT_SIZE, 827 "attempting to read the PUBEK"); 828 if (err) 829 goto out; 830 831 /* 832 ignore header 10 bytes 833 algorithm 32 bits (1 == RSA ) 834 encscheme 16 bits 835 sigscheme 16 bits 836 parameters (RSA 12->bytes: keybit, #primes, expbit) 837 keylenbytes 32 bits 838 256 byte modulus 839 ignore checksum 20 bytes 840 */ 841 data = tpm_cmd.params.readpubek_out_buffer; 842 str += 843 sprintf(str, 844 "Algorithm: %02X %02X %02X %02X\nEncscheme: %02X %02X\n" 845 "Sigscheme: %02X %02X\nParameters: %02X %02X %02X %02X" 846 " %02X %02X %02X %02X %02X %02X %02X %02X\n" 847 "Modulus length: %d\nModulus: \n", 848 data[10], data[11], data[12], data[13], data[14], 849 data[15], data[16], data[17], data[22], data[23], 850 data[24], data[25], data[26], data[27], data[28], 851 data[29], data[30], data[31], data[32], data[33], 852 be32_to_cpu(*((__be32 *) (data + 34)))); 853 854 for (i = 0; i < 256; i++) { 855 str += sprintf(str, "%02X ", data[i + 38]); 856 if ((i + 1) % 16 == 0) 857 str += sprintf(str, "\n"); 858 } 859out: 860 rc = str - buf; 861 return rc; 862} 863EXPORT_SYMBOL_GPL(tpm_show_pubek); 864 865 866ssize_t tpm_show_caps(struct device *dev, struct device_attribute *attr, 867 char *buf) 868{ 869 cap_t cap; 870 ssize_t rc; 871 char *str = buf; 872 873 rc = tpm_getcap(dev, TPM_CAP_PROP_MANUFACTURER, &cap, 874 "attempting to determine the manufacturer"); 875 if (rc) 876 return 0; 877 str += sprintf(str, "Manufacturer: 0x%x\n", 878 be32_to_cpu(cap.manufacturer_id)); 879 880 rc = tpm_getcap(dev, CAP_VERSION_1_1, &cap, 881 "attempting to determine the 1.1 version"); 882 if (rc) 883 return 0; 884 str += sprintf(str, 885 "TCG version: %d.%d\nFirmware version: %d.%d\n", 886 cap.tpm_version.Major, cap.tpm_version.Minor, 887 cap.tpm_version.revMajor, cap.tpm_version.revMinor); 888 return str - buf; 889} 890EXPORT_SYMBOL_GPL(tpm_show_caps); 891 892ssize_t tpm_show_caps_1_2(struct device * dev, 893 struct device_attribute * attr, char *buf) 894{ 895 cap_t cap; 896 ssize_t rc; 897 char *str = buf; 898 899 rc = tpm_getcap(dev, TPM_CAP_PROP_MANUFACTURER, &cap, 900 "attempting to determine the manufacturer"); 901 if (rc) 902 return 0; 903 str += sprintf(str, "Manufacturer: 0x%x\n", 904 be32_to_cpu(cap.manufacturer_id)); 905 rc = tpm_getcap(dev, CAP_VERSION_1_2, &cap, 906 "attempting to determine the 1.2 version"); 907 if (rc) 908 return 0; 909 str += sprintf(str, 910 "TCG version: %d.%d\nFirmware version: %d.%d\n", 911 cap.tpm_version_1_2.Major, cap.tpm_version_1_2.Minor, 912 cap.tpm_version_1_2.revMajor, 913 cap.tpm_version_1_2.revMinor); 914 return str - buf; 915} 916EXPORT_SYMBOL_GPL(tpm_show_caps_1_2); 917 918ssize_t tpm_show_timeouts(struct device *dev, struct device_attribute *attr, 919 char *buf) 920{ 921 struct tpm_chip *chip = dev_get_drvdata(dev); 922 923 return sprintf(buf, "%d %d %d\n", 924 jiffies_to_usecs(chip->vendor.duration[TPM_SHORT]), 925 jiffies_to_usecs(chip->vendor.duration[TPM_MEDIUM]), 926 jiffies_to_usecs(chip->vendor.duration[TPM_LONG])); 927} 928EXPORT_SYMBOL_GPL(tpm_show_timeouts); 929 930ssize_t tpm_store_cancel(struct device *dev, struct device_attribute *attr, 931 const char *buf, size_t count) 932{ 933 struct tpm_chip *chip = dev_get_drvdata(dev); 934 if (chip == NULL) 935 return 0; 936 937 chip->vendor.cancel(chip); 938 return count; 939} 940EXPORT_SYMBOL_GPL(tpm_store_cancel); 941 942/* 943 * Device file system interface to the TPM 944 * 945 * It's assured that the chip will be opened just once, 946 * by the check of is_open variable, which is protected 947 * by driver_lock. 948 */ 949int tpm_open(struct inode *inode, struct file *file) 950{ 951 int minor = iminor(inode); 952 struct tpm_chip *chip = NULL, *pos; 953 954 rcu_read_lock(); 955 list_for_each_entry_rcu(pos, &tpm_chip_list, list) { 956 if (pos->vendor.miscdev.minor == minor) { 957 chip = pos; 958 get_device(chip->dev); 959 break; 960 } 961 } 962 rcu_read_unlock(); 963 964 if (!chip) 965 return -ENODEV; 966 967 if (test_and_set_bit(0, &chip->is_open)) { 968 dev_dbg(chip->dev, "Another process owns this TPM\n"); 969 put_device(chip->dev); 970 return -EBUSY; 971 } 972 973 chip->data_buffer = kmalloc(TPM_BUFSIZE * sizeof(u8), GFP_KERNEL); 974 if (chip->data_buffer == NULL) { 975 clear_bit(0, &chip->is_open); 976 put_device(chip->dev); 977 return -ENOMEM; 978 } 979 980 atomic_set(&chip->data_pending, 0); 981 982 file->private_data = chip; 983 return 0; 984} 985EXPORT_SYMBOL_GPL(tpm_open); 986 987/* 988 * Called on file close 989 */ 990int tpm_release(struct inode *inode, struct file *file) 991{ 992 struct tpm_chip *chip = file->private_data; 993 994 del_singleshot_timer_sync(&chip->user_read_timer); 995 flush_scheduled_work(); 996 file->private_data = NULL; 997 atomic_set(&chip->data_pending, 0); 998 kfree(chip->data_buffer); 999 clear_bit(0, &chip->is_open); 1000 put_device(chip->dev); 1001 return 0; 1002} 1003EXPORT_SYMBOL_GPL(tpm_release); 1004 1005ssize_t tpm_write(struct file *file, const char __user *buf, 1006 size_t size, loff_t *off) 1007{ 1008 struct tpm_chip *chip = file->private_data; 1009 size_t in_size = size, out_size; 1010 1011 /* cannot perform a write until the read has cleared 1012 either via tpm_read or a user_read_timer timeout */ 1013 while (atomic_read(&chip->data_pending) != 0) 1014 msleep(TPM_TIMEOUT); 1015 1016 mutex_lock(&chip->buffer_mutex); 1017 1018 if (in_size > TPM_BUFSIZE) 1019 in_size = TPM_BUFSIZE; 1020 1021 if (copy_from_user 1022 (chip->data_buffer, (void __user *) buf, in_size)) { 1023 mutex_unlock(&chip->buffer_mutex); 1024 return -EFAULT; 1025 } 1026 1027 /* atomic tpm command send and result receive */ 1028 out_size = tpm_transmit(chip, chip->data_buffer, TPM_BUFSIZE); 1029 1030 atomic_set(&chip->data_pending, out_size); 1031 mutex_unlock(&chip->buffer_mutex); 1032 1033 /* Set a timeout by which the reader must come claim the result */ 1034 mod_timer(&chip->user_read_timer, jiffies + (60 * HZ)); 1035 1036 return in_size; 1037} 1038EXPORT_SYMBOL_GPL(tpm_write); 1039 1040ssize_t tpm_read(struct file *file, char __user *buf, 1041 size_t size, loff_t *off) 1042{ 1043 struct tpm_chip *chip = file->private_data; 1044 ssize_t ret_size; 1045 1046 del_singleshot_timer_sync(&chip->user_read_timer); 1047 flush_scheduled_work(); 1048 ret_size = atomic_read(&chip->data_pending); 1049 atomic_set(&chip->data_pending, 0); 1050 if (ret_size > 0) { /* relay data */ 1051 if (size < ret_size) 1052 ret_size = size; 1053 1054 mutex_lock(&chip->buffer_mutex); 1055 if (copy_to_user(buf, chip->data_buffer, ret_size)) 1056 ret_size = -EFAULT; 1057 mutex_unlock(&chip->buffer_mutex); 1058 } 1059 1060 return ret_size; 1061} 1062EXPORT_SYMBOL_GPL(tpm_read); 1063 1064void tpm_remove_hardware(struct device *dev) 1065{ 1066 struct tpm_chip *chip = dev_get_drvdata(dev); 1067 1068 if (chip == NULL) { 1069 dev_err(dev, "No device data found\n"); 1070 return; 1071 } 1072 1073 spin_lock(&driver_lock); 1074 list_del_rcu(&chip->list); 1075 spin_unlock(&driver_lock); 1076 synchronize_rcu(); 1077 1078 misc_deregister(&chip->vendor.miscdev); 1079 sysfs_remove_group(&dev->kobj, chip->vendor.attr_group); 1080 tpm_bios_log_teardown(chip->bios_dir); 1081 1082 /* write it this way to be explicit (chip->dev == dev) */ 1083 put_device(chip->dev); 1084} 1085EXPORT_SYMBOL_GPL(tpm_remove_hardware); 1086 1087#define TPM_ORD_SAVESTATE cpu_to_be32(152) 1088#define SAVESTATE_RESULT_SIZE 10 1089 1090static struct tpm_input_header savestate_header = { 1091 .tag = TPM_TAG_RQU_COMMAND, 1092 .length = cpu_to_be32(10), 1093 .ordinal = TPM_ORD_SAVESTATE 1094}; 1095 1096static int tpm_suspend_pcr; 1097static int __init tpm_suspend_setup(char *str) 1098{ 1099 get_option(&str, &tpm_suspend_pcr); 1100 return 1; 1101} 1102__setup("tpm_suspend_pcr=", tpm_suspend_setup); 1103 1104/* 1105 * We are about to suspend. Save the TPM state 1106 * so that it can be restored. 1107 */ 1108int tpm_pm_suspend(struct device *dev, pm_message_t pm_state) 1109{ 1110 struct tpm_chip *chip = dev_get_drvdata(dev); 1111 struct tpm_cmd_t cmd; 1112 int rc; 1113 1114 u8 dummy_hash[TPM_DIGEST_SIZE] = { 0 }; 1115 1116 if (chip == NULL) 1117 return -ENODEV; 1118 1119 /* for buggy tpm, flush pcrs with extend to selected dummy */ 1120 if (tpm_suspend_pcr) { 1121 cmd.header.in = pcrextend_header; 1122 cmd.params.pcrextend_in.pcr_idx = cpu_to_be32(tpm_suspend_pcr); 1123 memcpy(cmd.params.pcrextend_in.hash, dummy_hash, 1124 TPM_DIGEST_SIZE); 1125 rc = transmit_cmd(chip, &cmd, EXTEND_PCR_RESULT_SIZE, 1126 "extending dummy pcr before suspend"); 1127 } 1128 1129 /* now do the actual savestate */ 1130 cmd.header.in = savestate_header; 1131 rc = transmit_cmd(chip, &cmd, SAVESTATE_RESULT_SIZE, 1132 "sending savestate before suspend"); 1133 return rc; 1134} 1135EXPORT_SYMBOL_GPL(tpm_pm_suspend); 1136 1137/* 1138 * Resume from a power safe. The BIOS already restored 1139 * the TPM state. 1140 */ 1141int tpm_pm_resume(struct device *dev) 1142{ 1143 struct tpm_chip *chip = dev_get_drvdata(dev); 1144 1145 if (chip == NULL) 1146 return -ENODEV; 1147 1148 return 0; 1149} 1150EXPORT_SYMBOL_GPL(tpm_pm_resume); 1151 1152/* In case vendor provided release function, call it too.*/ 1153 1154void tpm_dev_vendor_release(struct tpm_chip *chip) 1155{ 1156 if (chip->vendor.release) 1157 chip->vendor.release(chip->dev); 1158 1159 clear_bit(chip->dev_num, dev_mask); 1160 kfree(chip->vendor.miscdev.name); 1161} 1162EXPORT_SYMBOL_GPL(tpm_dev_vendor_release); 1163 1164 1165/* 1166 * Once all references to platform device are down to 0, 1167 * release all allocated structures. 1168 */ 1169void tpm_dev_release(struct device *dev) 1170{ 1171 struct tpm_chip *chip = dev_get_drvdata(dev); 1172 1173 tpm_dev_vendor_release(chip); 1174 1175 chip->release(dev); 1176 kfree(chip); 1177} 1178EXPORT_SYMBOL_GPL(tpm_dev_release); 1179 1180/* 1181 * Called from tpm_<specific>.c probe function only for devices 1182 * the driver has determined it should claim. Prior to calling 1183 * this function the specific probe function has called pci_enable_device 1184 * upon errant exit from this function specific probe function should call 1185 * pci_disable_device 1186 */ 1187struct tpm_chip *tpm_register_hardware(struct device *dev, 1188 const struct tpm_vendor_specific *entry) 1189{ 1190#define DEVNAME_SIZE 7 1191 1192 char *devname; 1193 struct tpm_chip *chip; 1194 1195 /* Driver specific per-device data */ 1196 chip = kzalloc(sizeof(*chip), GFP_KERNEL); 1197 devname = kmalloc(DEVNAME_SIZE, GFP_KERNEL); 1198 1199 if (chip == NULL || devname == NULL) 1200 goto out_free; 1201 1202 mutex_init(&chip->buffer_mutex); 1203 mutex_init(&chip->tpm_mutex); 1204 INIT_LIST_HEAD(&chip->list); 1205 1206 INIT_WORK(&chip->work, timeout_work); 1207 1208 setup_timer(&chip->user_read_timer, user_reader_timeout, 1209 (unsigned long)chip); 1210 1211 memcpy(&chip->vendor, entry, sizeof(struct tpm_vendor_specific)); 1212 1213 chip->dev_num = find_first_zero_bit(dev_mask, TPM_NUM_DEVICES); 1214 1215 if (chip->dev_num >= TPM_NUM_DEVICES) { 1216 dev_err(dev, "No available tpm device numbers\n"); 1217 goto out_free; 1218 } else if (chip->dev_num == 0) 1219 chip->vendor.miscdev.minor = TPM_MINOR; 1220 else 1221 chip->vendor.miscdev.minor = MISC_DYNAMIC_MINOR; 1222 1223 set_bit(chip->dev_num, dev_mask); 1224 1225 scnprintf(devname, DEVNAME_SIZE, "%s%d", "tpm", chip->dev_num); 1226 chip->vendor.miscdev.name = devname; 1227 1228 chip->vendor.miscdev.parent = dev; 1229 chip->dev = get_device(dev); 1230 chip->release = dev->release; 1231 dev->release = tpm_dev_release; 1232 dev_set_drvdata(dev, chip); 1233 1234 if (misc_register(&chip->vendor.miscdev)) { 1235 dev_err(chip->dev, 1236 "unable to misc_register %s, minor %d\n", 1237 chip->vendor.miscdev.name, 1238 chip->vendor.miscdev.minor); 1239 put_device(chip->dev); 1240 return NULL; 1241 } 1242 1243 if (sysfs_create_group(&dev->kobj, chip->vendor.attr_group)) { 1244 misc_deregister(&chip->vendor.miscdev); 1245 put_device(chip->dev); 1246 1247 return NULL; 1248 } 1249 1250 chip->bios_dir = tpm_bios_log_setup(devname); 1251 1252 /* Make chip available */ 1253 spin_lock(&driver_lock); 1254 list_add_rcu(&chip->list, &tpm_chip_list); 1255 spin_unlock(&driver_lock); 1256 1257 return chip; 1258 1259out_free: 1260 kfree(chip); 1261 kfree(devname); 1262 return NULL; 1263} 1264EXPORT_SYMBOL_GPL(tpm_register_hardware); 1265 1266MODULE_AUTHOR("Leendert van Doorn (leendert@watson.ibm.com)"); 1267MODULE_DESCRIPTION("TPM Driver"); 1268MODULE_VERSION("2.0"); 1269MODULE_LICENSE("GPL"); 1270