Skip to content

Commit d4a3175

Browse files
Tomas WinklerJarkko Sakkinen
authored andcommitted
tpm: move tpm 1.x selftest code from tpm-interface.c tpm1-cmd.c
Move the tpm1 selftest code functions to tpm1-cmd.c and adjust callers to use the new function names. 1. tpm_pcr_read_dev() to tpm1_pcr_read_dev(). 2. tpm_continue_selftest() to tpm1_continue_selftest(). 3. tpm_do_selftest() to tpm1_do_selftest() Signed-off-by: Tomas Winkler <tomas.winkler@intel.com> Tested-by: Jarkko Sakkinen <jarkko.sakkinen@linux.intel.com> Reviewed-by: Jarkko Sakkinen <jarkko.sakkinen@linux.intel.com> Signed-off-by: Jarkko Sakkinen <jarkko.sakkinen@linux.intel.com>
1 parent 433d390 commit d4a3175

6 files changed

Lines changed: 150 additions & 151 deletions

File tree

‎drivers/char/tpm/st33zp24/st33zp24.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -649,7 +649,7 @@ int st33zp24_pm_resume(struct device *dev)
649649
} else {
650650
ret = tpm_pm_resume(dev);
651651
if (!ret)
652-
tpm_do_selftest(chip);
652+
tpm1_do_selftest(chip);
653653
}
654654
return ret;
655655
} /* st33zp24_pm_resume() */

‎drivers/char/tpm/tpm-interface.c‎

Lines changed: 3 additions & 145 deletions
Original file line numberDiff line numberDiff line change
@@ -466,59 +466,6 @@ int tpm_get_timeouts(struct tpm_chip *chip)
466466
}
467467
EXPORT_SYMBOL_GPL(tpm_get_timeouts);
468468

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-
522469
/**
523470
* tpm_is_tpm2 - do we a have a TPM2 chip?
524471
* @chip: a &struct tpm_chip instance, %NULL for the default chip
@@ -559,10 +506,12 @@ int tpm_pcr_read(struct tpm_chip *chip, int pcr_idx, u8 *res_buf)
559506
chip = tpm_find_get_ops(chip);
560507
if (!chip)
561508
return -ENODEV;
509+
562510
if (chip->flags & TPM_CHIP_FLAG_TPM2)
563511
rc = tpm2_pcr_read(chip, pcr_idx, res_buf);
564512
else
565-
rc = tpm_pcr_read_dev(chip, pcr_idx, res_buf);
513+
rc = tpm1_pcr_read_dev(chip, pcr_idx, res_buf);
514+
566515
tpm_put_ops(chip);
567516
return rc;
568517
}
@@ -613,97 +562,6 @@ int tpm_pcr_extend(struct tpm_chip *chip, int pcr_idx, const u8 *hash)
613562
}
614563
EXPORT_SYMBOL_GPL(tpm_pcr_extend);
615564

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-
707565
/**
708566
* tpm_send - send a TPM command
709567
* @chip: a &struct tpm_chip instance, %NULL for the default chip

‎drivers/char/tpm/tpm-sysfs.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ static ssize_t pcrs_show(struct device *dev, struct device_attribute *attr,
114114

115115
num_pcrs = be32_to_cpu(cap.num_pcrs);
116116
for (i = 0; i < num_pcrs; i++) {
117-
rc = tpm_pcr_read_dev(chip, i, digest);
117+
rc = tpm1_pcr_read_dev(chip, i, digest);
118118
if (rc)
119119
break;
120120
str += sprintf(str, "PCR-%02d: ", i);

‎drivers/char/tpm/tpm.h‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -543,12 +543,12 @@ ssize_t tpm_transmit_cmd(struct tpm_chip *chip, struct tpm_space *space,
543543
int tpm_startup(struct tpm_chip *chip);
544544
int tpm_get_timeouts(struct tpm_chip *);
545545
int tpm1_auto_startup(struct tpm_chip *chip);
546-
int tpm_do_selftest(struct tpm_chip *chip);
547-
546+
int tpm1_do_selftest(struct tpm_chip *chip);
548547
int tpm1_get_timeouts(struct tpm_chip *chip);
549548
unsigned long tpm1_calc_ordinal_duration(struct tpm_chip *chip, u32 ordinal);
550549
int tpm1_pcr_extend(struct tpm_chip *chip, int pcr_idx, const u8 *hash,
551550
const char *log_msg);
551+
int tpm1_pcr_read_dev(struct tpm_chip *chip, int pcr_idx, u8 *res_buf);
552552
ssize_t tpm1_getcap(struct tpm_chip *chip, u32 subcap_id, cap_t *cap,
553553
const char *desc, size_t min_cap_length);
554554
int tpm1_get_random(struct tpm_chip *chip, u8 *out, size_t max);
@@ -575,7 +575,6 @@ void tpm_chip_unregister(struct tpm_chip *chip);
575575

576576
void tpm_sysfs_add_device(struct tpm_chip *chip);
577577

578-
int tpm_pcr_read_dev(struct tpm_chip *chip, int pcr_idx, u8 *res_buf);
579578

580579
#ifdef CONFIG_ACPI
581580
extern void tpm_add_ppi(struct tpm_chip *chip);

‎drivers/char/tpm/tpm1-cmd.c‎

Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -524,3 +524,145 @@ int tpm1_get_random(struct tpm_chip *chip, u8 *out, size_t max)
524524

525525
return total ? total : -EIO;
526526
}
527+
528+
#define TPM_ORDINAL_PCRREAD 21
529+
#define READ_PCR_RESULT_SIZE 30
530+
#define READ_PCR_RESULT_BODY_SIZE 20
531+
static const struct tpm_input_header pcrread_header = {
532+
.tag = cpu_to_be16(TPM_TAG_RQU_COMMAND),
533+
.length = cpu_to_be32(14),
534+
.ordinal = cpu_to_be32(TPM_ORDINAL_PCRREAD)
535+
};
536+
537+
int tpm1_pcr_read_dev(struct tpm_chip *chip, int pcr_idx, u8 *res_buf)
538+
{
539+
int rc;
540+
struct tpm_cmd_t cmd;
541+
542+
cmd.header.in = pcrread_header;
543+
cmd.params.pcrread_in.pcr_idx = cpu_to_be32(pcr_idx);
544+
rc = tpm_transmit_cmd(chip, NULL, &cmd, READ_PCR_RESULT_SIZE,
545+
READ_PCR_RESULT_BODY_SIZE, 0,
546+
"attempting to read a pcr value");
547+
548+
if (rc == 0)
549+
memcpy(res_buf, cmd.params.pcrread_out.pcr_result,
550+
TPM_DIGEST_SIZE);
551+
return rc;
552+
}
553+
554+
#define TPM_ORD_CONTINUE_SELFTEST 83
555+
#define CONTINUE_SELFTEST_RESULT_SIZE 10
556+
static const struct tpm_input_header continue_selftest_header = {
557+
.tag = cpu_to_be16(TPM_TAG_RQU_COMMAND),
558+
.length = cpu_to_be32(10),
559+
.ordinal = cpu_to_be32(TPM_ORD_CONTINUE_SELFTEST),
560+
};
561+
562+
/**
563+
* tpm_continue_selftest -- run TPM's selftest
564+
* @chip: TPM chip to use
565+
*
566+
* Returns 0 on success, < 0 in case of fatal error or a value > 0 representing
567+
* a TPM error code.
568+
*/
569+
static int tpm1_continue_selftest(struct tpm_chip *chip)
570+
{
571+
int rc;
572+
struct tpm_cmd_t cmd;
573+
574+
cmd.header.in = continue_selftest_header;
575+
rc = tpm_transmit_cmd(chip, NULL, &cmd, CONTINUE_SELFTEST_RESULT_SIZE,
576+
0, 0, "continue selftest");
577+
return rc;
578+
}
579+
580+
/**
581+
* tpm1_do_selftest - have the TPM continue its selftest and wait until it
582+
* can receive further commands
583+
* @chip: TPM chip to use
584+
*
585+
* Returns 0 on success, < 0 in case of fatal error or a value > 0 representing
586+
* a TPM error code.
587+
*/
588+
int tpm1_do_selftest(struct tpm_chip *chip)
589+
{
590+
int rc;
591+
unsigned int loops;
592+
unsigned int delay_msec = 100;
593+
unsigned long duration;
594+
u8 dummy[TPM_DIGEST_SIZE];
595+
596+
duration = tpm1_calc_ordinal_duration(chip, TPM_ORD_CONTINUE_SELFTEST);
597+
598+
loops = jiffies_to_msecs(duration) / delay_msec;
599+
600+
rc = tpm1_continue_selftest(chip);
601+
if (rc == TPM_ERR_INVALID_POSTINIT) {
602+
chip->flags |= TPM_CHIP_FLAG_ALWAYS_POWERED;
603+
dev_info(&chip->dev, "TPM not ready (%d)\n", rc);
604+
}
605+
/* This may fail if there was no TPM driver during a suspend/resume
606+
* cycle; some may return 10 (BAD_ORDINAL), others 28 (FAILEDSELFTEST)
607+
*/
608+
if (rc)
609+
return rc;
610+
611+
do {
612+
/* Attempt to read a PCR value */
613+
rc = tpm1_pcr_read_dev(chip, 0, dummy);
614+
615+
/* Some buggy TPMs will not respond to tpm_tis_ready() for
616+
* around 300ms while the self test is ongoing, keep trying
617+
* until the self test duration expires.
618+
*/
619+
if (rc == -ETIME) {
620+
dev_info(&chip->dev, HW_ERR "TPM command timed out during continue self test");
621+
tpm_msleep(delay_msec);
622+
continue;
623+
}
624+
625+
if (rc == TPM_ERR_DISABLED || rc == TPM_ERR_DEACTIVATED) {
626+
dev_info(&chip->dev, "TPM is disabled/deactivated (0x%X)\n",
627+
rc);
628+
/* TPM is disabled and/or deactivated; driver can
629+
* proceed and TPM does handle commands for
630+
* suspend/resume correctly
631+
*/
632+
return 0;
633+
}
634+
if (rc != TPM_WARN_DOING_SELFTEST)
635+
return rc;
636+
tpm_msleep(delay_msec);
637+
} while (--loops > 0);
638+
639+
return rc;
640+
}
641+
EXPORT_SYMBOL_GPL(tpm1_do_selftest);
642+
643+
/**
644+
* tpm1_auto_startup - Perform the standard automatic TPM initialization
645+
* sequence
646+
* @chip: TPM chip to use
647+
*
648+
* Returns 0 on success, < 0 in case of fatal error.
649+
*/
650+
int tpm1_auto_startup(struct tpm_chip *chip)
651+
{
652+
int rc;
653+
654+
rc = tpm1_get_timeouts(chip);
655+
if (rc)
656+
goto out;
657+
rc = tpm1_do_selftest(chip);
658+
if (rc) {
659+
dev_err(&chip->dev, "TPM self test failed\n");
660+
goto out;
661+
}
662+
663+
return rc;
664+
out:
665+
if (rc > 0)
666+
rc = -ENODEV;
667+
return rc;
668+
}

‎drivers/char/tpm/tpm_tis_core.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1056,7 +1056,7 @@ int tpm_tis_resume(struct device *dev)
10561056
* an error code but for unknown reason it isn't handled.
10571057
*/
10581058
if (!(chip->flags & TPM_CHIP_FLAG_TPM2))
1059-
tpm_do_selftest(chip);
1059+
tpm1_do_selftest(chip);
10601060

10611061
return 0;
10621062
}

0 commit comments

Comments
 (0)