tpm1-cmd.c (433d390f005d3a89dba5a03a87b5a6c242748de3) tpm1-cmd.c (d4a317563207163ddcf677e5965ffc56ef073514)
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>

--- 510 unchanged lines hidden (view full) ---

519
520 dest += recd;
521 total += recd;
522 num_bytes -= recd;
523 } while (retries-- && (size_t)total < max);
524
525 return total ? total : -EIO;
526}
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>

--- 510 unchanged lines hidden (view full) ---

519
520 dest += recd;
521 total += recd;
522 num_bytes -= recd;
523 } while (retries-- && (size_t)total < max);
524
525 return total ? total : -EIO;
526}
527
528#define TPM_ORDINAL_PCRREAD 21
529#define READ_PCR_RESULT_SIZE 30
530#define READ_PCR_RESULT_BODY_SIZE 20
531static 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
537int 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
556static 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 */
569static 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 */
588int 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}
641EXPORT_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 */
650int 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;
664out:
665 if (rc > 0)
666 rc = -ENODEV;
667 return rc;
668}