aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/char/tpm
diff options
context:
space:
mode:
authorJarkko Sakkinen <jarkko.sakkinen@linux.intel.com>2014-12-12 14:46:33 -0500
committerPeter Huewe <peterhuewe@gmx.de>2015-01-17 08:00:09 -0500
commit87155b7311bfec75b590b823b11f77adf2a16412 (patch)
treea57ebb7fd4dc1f2b73f72f9759c424939b208b03 /drivers/char/tpm
parent1a0f1b279c2c4e5747a682b055d27d88e35bb632 (diff)
tpm: merge duplicate transmit_cmd() functions
Merged transmit_cmd() functions in tpm-interface.c and tpm-sysfs.c. Added "tpm_" prefix for consistency sake. Changed cmd parameter as opaque. This enables to use separate command structures for TPM1 and TPM2 commands in future. Loose coupling works fine here. Signed-off-by: Jarkko Sakkinen <jarkko.sakkinen@linux.intel.com> Reviewed-by: Jasob Gunthorpe <jason.gunthorpe@obsidianresearch.com> Reviewed-by: Stefan Berger <stefanb@linux.vnet.ibm.com> Reviewed-by: Peter Huewe <peterhuewe@gmx.de> Tested-by: Scot Doyle <lkml14@scotdoyle.com> Tested-by: Peter Huewe <peterhuewe@gmx.de> Signed-off-by: Peter Huewe <peterhuewe@gmx.de>
Diffstat (limited to 'drivers/char/tpm')
-rw-r--r--drivers/char/tpm/tpm-interface.c49
-rw-r--r--drivers/char/tpm/tpm-sysfs.c23
-rw-r--r--drivers/char/tpm/tpm.h3
3 files changed, 30 insertions, 45 deletions
diff --git a/drivers/char/tpm/tpm-interface.c b/drivers/char/tpm/tpm-interface.c
index cfb9089887bd..c17aa45024aa 100644
--- a/drivers/char/tpm/tpm-interface.c
+++ b/drivers/char/tpm/tpm-interface.c
@@ -398,9 +398,10 @@ out:
398#define TPM_DIGEST_SIZE 20 398#define TPM_DIGEST_SIZE 20
399#define TPM_RET_CODE_IDX 6 399#define TPM_RET_CODE_IDX 6
400 400
401static ssize_t transmit_cmd(struct tpm_chip *chip, struct tpm_cmd_t *cmd, 401ssize_t tpm_transmit_cmd(struct tpm_chip *chip, void *cmd,
402 int len, const char *desc) 402 int len, const char *desc)
403{ 403{
404 struct tpm_output_header *header;
404 int err; 405 int err;
405 406
406 len = tpm_transmit(chip, (u8 *) cmd, len); 407 len = tpm_transmit(chip, (u8 *) cmd, len);
@@ -409,7 +410,9 @@ static ssize_t transmit_cmd(struct tpm_chip *chip, struct tpm_cmd_t *cmd,
409 else if (len < TPM_HEADER_SIZE) 410 else if (len < TPM_HEADER_SIZE)
410 return -EFAULT; 411 return -EFAULT;
411 412
412 err = be32_to_cpu(cmd->header.out.return_code); 413 header = cmd;
414
415 err = be32_to_cpu(header->return_code);
413 if (err != 0 && desc) 416 if (err != 0 && desc)
414 dev_err(chip->dev, "A TPM error (%d) occurred %s\n", err, desc); 417 dev_err(chip->dev, "A TPM error (%d) occurred %s\n", err, desc);
415 418
@@ -448,7 +451,7 @@ ssize_t tpm_getcap(struct device *dev, __be32 subcap_id, cap_t *cap,
448 tpm_cmd.params.getcap_in.subcap_size = cpu_to_be32(4); 451 tpm_cmd.params.getcap_in.subcap_size = cpu_to_be32(4);
449 tpm_cmd.params.getcap_in.subcap = subcap_id; 452 tpm_cmd.params.getcap_in.subcap = subcap_id;
450 } 453 }
451 rc = transmit_cmd(chip, &tpm_cmd, TPM_INTERNAL_RESULT_SIZE, desc); 454 rc = tpm_transmit_cmd(chip, &tpm_cmd, TPM_INTERNAL_RESULT_SIZE, desc);
452 if (!rc) 455 if (!rc)
453 *cap = tpm_cmd.params.getcap_out.cap; 456 *cap = tpm_cmd.params.getcap_out.cap;
454 return rc; 457 return rc;
@@ -464,8 +467,8 @@ void tpm_gen_interrupt(struct tpm_chip *chip)
464 tpm_cmd.params.getcap_in.subcap_size = cpu_to_be32(4); 467 tpm_cmd.params.getcap_in.subcap_size = cpu_to_be32(4);
465 tpm_cmd.params.getcap_in.subcap = TPM_CAP_PROP_TIS_TIMEOUT; 468 tpm_cmd.params.getcap_in.subcap = TPM_CAP_PROP_TIS_TIMEOUT;
466 469
467 rc = transmit_cmd(chip, &tpm_cmd, TPM_INTERNAL_RESULT_SIZE, 470 rc = tpm_transmit_cmd(chip, &tpm_cmd, TPM_INTERNAL_RESULT_SIZE,
468 "attempting to determine the timeouts"); 471 "attempting to determine the timeouts");
469} 472}
470EXPORT_SYMBOL_GPL(tpm_gen_interrupt); 473EXPORT_SYMBOL_GPL(tpm_gen_interrupt);
471 474
@@ -484,8 +487,8 @@ static int tpm_startup(struct tpm_chip *chip, __be16 startup_type)
484 struct tpm_cmd_t start_cmd; 487 struct tpm_cmd_t start_cmd;
485 start_cmd.header.in = tpm_startup_header; 488 start_cmd.header.in = tpm_startup_header;
486 start_cmd.params.startup_in.startup_type = startup_type; 489 start_cmd.params.startup_in.startup_type = startup_type;
487 return transmit_cmd(chip, &start_cmd, TPM_INTERNAL_RESULT_SIZE, 490 return tpm_transmit_cmd(chip, &start_cmd, TPM_INTERNAL_RESULT_SIZE,
488 "attempting to start the TPM"); 491 "attempting to start the TPM");
489} 492}
490 493
491int tpm_get_timeouts(struct tpm_chip *chip) 494int tpm_get_timeouts(struct tpm_chip *chip)
@@ -500,7 +503,7 @@ int tpm_get_timeouts(struct tpm_chip *chip)
500 tpm_cmd.params.getcap_in.cap = TPM_CAP_PROP; 503 tpm_cmd.params.getcap_in.cap = TPM_CAP_PROP;
501 tpm_cmd.params.getcap_in.subcap_size = cpu_to_be32(4); 504 tpm_cmd.params.getcap_in.subcap_size = cpu_to_be32(4);
502 tpm_cmd.params.getcap_in.subcap = TPM_CAP_PROP_TIS_TIMEOUT; 505 tpm_cmd.params.getcap_in.subcap = TPM_CAP_PROP_TIS_TIMEOUT;
503 rc = transmit_cmd(chip, &tpm_cmd, TPM_INTERNAL_RESULT_SIZE, NULL); 506 rc = tpm_transmit_cmd(chip, &tpm_cmd, TPM_INTERNAL_RESULT_SIZE, NULL);
504 507
505 if (rc == TPM_ERR_INVALID_POSTINIT) { 508 if (rc == TPM_ERR_INVALID_POSTINIT) {
506 /* The TPM is not started, we are the first to talk to it. 509 /* The TPM is not started, we are the first to talk to it.
@@ -513,7 +516,7 @@ int tpm_get_timeouts(struct tpm_chip *chip)
513 tpm_cmd.params.getcap_in.cap = TPM_CAP_PROP; 516 tpm_cmd.params.getcap_in.cap = TPM_CAP_PROP;
514 tpm_cmd.params.getcap_in.subcap_size = cpu_to_be32(4); 517 tpm_cmd.params.getcap_in.subcap_size = cpu_to_be32(4);
515 tpm_cmd.params.getcap_in.subcap = TPM_CAP_PROP_TIS_TIMEOUT; 518 tpm_cmd.params.getcap_in.subcap = TPM_CAP_PROP_TIS_TIMEOUT;
516 rc = transmit_cmd(chip, &tpm_cmd, TPM_INTERNAL_RESULT_SIZE, 519 rc = tpm_transmit_cmd(chip, &tpm_cmd, TPM_INTERNAL_RESULT_SIZE,
517 NULL); 520 NULL);
518 } 521 }
519 if (rc) { 522 if (rc) {
@@ -575,8 +578,8 @@ duration:
575 tpm_cmd.params.getcap_in.subcap_size = cpu_to_be32(4); 578 tpm_cmd.params.getcap_in.subcap_size = cpu_to_be32(4);
576 tpm_cmd.params.getcap_in.subcap = TPM_CAP_PROP_TIS_DURATION; 579 tpm_cmd.params.getcap_in.subcap = TPM_CAP_PROP_TIS_DURATION;
577 580
578 rc = transmit_cmd(chip, &tpm_cmd, TPM_INTERNAL_RESULT_SIZE, 581 rc = tpm_transmit_cmd(chip, &tpm_cmd, TPM_INTERNAL_RESULT_SIZE,
579 "attempting to determine the durations"); 582 "attempting to determine the durations");
580 if (rc) 583 if (rc)
581 return rc; 584 return rc;
582 585
@@ -631,8 +634,8 @@ static int tpm_continue_selftest(struct tpm_chip *chip)
631 struct tpm_cmd_t cmd; 634 struct tpm_cmd_t cmd;
632 635
633 cmd.header.in = continue_selftest_header; 636 cmd.header.in = continue_selftest_header;
634 rc = transmit_cmd(chip, &cmd, CONTINUE_SELFTEST_RESULT_SIZE, 637 rc = tpm_transmit_cmd(chip, &cmd, CONTINUE_SELFTEST_RESULT_SIZE,
635 "continue selftest"); 638 "continue selftest");
636 return rc; 639 return rc;
637} 640}
638 641
@@ -672,8 +675,8 @@ int tpm_pcr_read_dev(struct tpm_chip *chip, int pcr_idx, u8 *res_buf)
672 675
673 cmd.header.in = pcrread_header; 676 cmd.header.in = pcrread_header;
674 cmd.params.pcrread_in.pcr_idx = cpu_to_be32(pcr_idx); 677 cmd.params.pcrread_in.pcr_idx = cpu_to_be32(pcr_idx);
675 rc = transmit_cmd(chip, &cmd, READ_PCR_RESULT_SIZE, 678 rc = tpm_transmit_cmd(chip, &cmd, READ_PCR_RESULT_SIZE,
676 "attempting to read a pcr value"); 679 "attempting to read a pcr value");
677 680
678 if (rc == 0) 681 if (rc == 0)
679 memcpy(res_buf, cmd.params.pcrread_out.pcr_result, 682 memcpy(res_buf, cmd.params.pcrread_out.pcr_result,
@@ -737,8 +740,8 @@ int tpm_pcr_extend(u32 chip_num, int pcr_idx, const u8 *hash)
737 cmd.header.in = pcrextend_header; 740 cmd.header.in = pcrextend_header;
738 cmd.params.pcrextend_in.pcr_idx = cpu_to_be32(pcr_idx); 741 cmd.params.pcrextend_in.pcr_idx = cpu_to_be32(pcr_idx);
739 memcpy(cmd.params.pcrextend_in.hash, hash, TPM_DIGEST_SIZE); 742 memcpy(cmd.params.pcrextend_in.hash, hash, TPM_DIGEST_SIZE);
740 rc = transmit_cmd(chip, &cmd, EXTEND_PCR_RESULT_SIZE, 743 rc = tpm_transmit_cmd(chip, &cmd, EXTEND_PCR_RESULT_SIZE,
741 "attempting extend a PCR value"); 744 "attempting extend a PCR value");
742 745
743 tpm_chip_put(chip); 746 tpm_chip_put(chip);
744 return rc; 747 return rc;
@@ -817,7 +820,7 @@ int tpm_send(u32 chip_num, void *cmd, size_t buflen)
817 if (chip == NULL) 820 if (chip == NULL)
818 return -ENODEV; 821 return -ENODEV;
819 822
820 rc = transmit_cmd(chip, cmd, buflen, "attempting tpm_cmd"); 823 rc = tpm_transmit_cmd(chip, cmd, buflen, "attempting tpm_cmd");
821 824
822 tpm_chip_put(chip); 825 tpm_chip_put(chip);
823 return rc; 826 return rc;
@@ -938,14 +941,14 @@ int tpm_pm_suspend(struct device *dev)
938 cmd.params.pcrextend_in.pcr_idx = cpu_to_be32(tpm_suspend_pcr); 941 cmd.params.pcrextend_in.pcr_idx = cpu_to_be32(tpm_suspend_pcr);
939 memcpy(cmd.params.pcrextend_in.hash, dummy_hash, 942 memcpy(cmd.params.pcrextend_in.hash, dummy_hash,
940 TPM_DIGEST_SIZE); 943 TPM_DIGEST_SIZE);
941 rc = transmit_cmd(chip, &cmd, EXTEND_PCR_RESULT_SIZE, 944 rc = tpm_transmit_cmd(chip, &cmd, EXTEND_PCR_RESULT_SIZE,
942 "extending dummy pcr before suspend"); 945 "extending dummy pcr before suspend");
943 } 946 }
944 947
945 /* now do the actual savestate */ 948 /* now do the actual savestate */
946 for (try = 0; try < TPM_RETRY; try++) { 949 for (try = 0; try < TPM_RETRY; try++) {
947 cmd.header.in = savestate_header; 950 cmd.header.in = savestate_header;
948 rc = transmit_cmd(chip, &cmd, SAVESTATE_RESULT_SIZE, NULL); 951 rc = tpm_transmit_cmd(chip, &cmd, SAVESTATE_RESULT_SIZE, NULL);
949 952
950 /* 953 /*
951 * If the TPM indicates that it is too busy to respond to 954 * If the TPM indicates that it is too busy to respond to
@@ -1022,7 +1025,7 @@ int tpm_get_random(u32 chip_num, u8 *out, size_t max)
1022 tpm_cmd.header.in = tpm_getrandom_header; 1025 tpm_cmd.header.in = tpm_getrandom_header;
1023 tpm_cmd.params.getrandom_in.num_bytes = cpu_to_be32(num_bytes); 1026 tpm_cmd.params.getrandom_in.num_bytes = cpu_to_be32(num_bytes);
1024 1027
1025 err = transmit_cmd(chip, &tpm_cmd, 1028 err = tpm_transmit_cmd(chip, &tpm_cmd,
1026 TPM_GETRANDOM_RESULT_SIZE + num_bytes, 1029 TPM_GETRANDOM_RESULT_SIZE + num_bytes,
1027 "attempting get random"); 1030 "attempting get random");
1028 if (err) 1031 if (err)
diff --git a/drivers/char/tpm/tpm-sysfs.c b/drivers/char/tpm/tpm-sysfs.c
index 01730a27ae07..8ecb052e290d 100644
--- a/drivers/char/tpm/tpm-sysfs.c
+++ b/drivers/char/tpm/tpm-sysfs.c
@@ -20,25 +20,6 @@
20#include <linux/device.h> 20#include <linux/device.h>
21#include "tpm.h" 21#include "tpm.h"
22 22
23/* XXX for now this helper is duplicated in tpm-interface.c */
24static ssize_t transmit_cmd(struct tpm_chip *chip, struct tpm_cmd_t *cmd,
25 int len, const char *desc)
26{
27 int err;
28
29 len = tpm_transmit(chip, (u8 *) cmd, len);
30 if (len < 0)
31 return len;
32 else if (len < TPM_HEADER_SIZE)
33 return -EFAULT;
34
35 err = be32_to_cpu(cmd->header.out.return_code);
36 if (err != 0 && desc)
37 dev_err(chip->dev, "A TPM error (%d) occurred %s\n", err, desc);
38
39 return err;
40}
41
42#define READ_PUBEK_RESULT_SIZE 314 23#define READ_PUBEK_RESULT_SIZE 314
43#define TPM_ORD_READPUBEK cpu_to_be32(124) 24#define TPM_ORD_READPUBEK cpu_to_be32(124)
44static struct tpm_input_header tpm_readpubek_header = { 25static struct tpm_input_header tpm_readpubek_header = {
@@ -58,8 +39,8 @@ static ssize_t pubek_show(struct device *dev, struct device_attribute *attr,
58 struct tpm_chip *chip = dev_get_drvdata(dev); 39 struct tpm_chip *chip = dev_get_drvdata(dev);
59 40
60 tpm_cmd.header.in = tpm_readpubek_header; 41 tpm_cmd.header.in = tpm_readpubek_header;
61 err = transmit_cmd(chip, &tpm_cmd, READ_PUBEK_RESULT_SIZE, 42 err = tpm_transmit_cmd(chip, &tpm_cmd, READ_PUBEK_RESULT_SIZE,
62 "attempting to read the PUBEK"); 43 "attempting to read the PUBEK");
63 if (err) 44 if (err)
64 goto out; 45 goto out;
65 46
diff --git a/drivers/char/tpm/tpm.h b/drivers/char/tpm/tpm.h
index e4d0888d2eab..e638eb016b90 100644
--- a/drivers/char/tpm/tpm.h
+++ b/drivers/char/tpm/tpm.h
@@ -314,9 +314,10 @@ struct tpm_cmd_t {
314} __packed; 314} __packed;
315 315
316ssize_t tpm_getcap(struct device *, __be32, cap_t *, const char *); 316ssize_t tpm_getcap(struct device *, __be32, cap_t *, const char *);
317
318ssize_t tpm_transmit(struct tpm_chip *chip, const char *buf, 317ssize_t tpm_transmit(struct tpm_chip *chip, const char *buf,
319 size_t bufsiz); 318 size_t bufsiz);
319ssize_t tpm_transmit_cmd(struct tpm_chip *chip, void *cmd, int len,
320 const char *desc);
320extern int tpm_get_timeouts(struct tpm_chip *); 321extern int tpm_get_timeouts(struct tpm_chip *);
321extern void tpm_gen_interrupt(struct tpm_chip *); 322extern void tpm_gen_interrupt(struct tpm_chip *);
322extern int tpm_do_selftest(struct tpm_chip *); 323extern int tpm_do_selftest(struct tpm_chip *);