Patrick Georgi | ac95903 | 2020-05-05 22:49:26 +0200 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0-or-later */ |
Stefan Reinauer | d518c7a | 2013-11-04 17:38:32 -0800 | [diff] [blame] | 2 | |
| 3 | #include <stdint.h> |
| 4 | #include <string.h> |
| 5 | #include <assert.h> |
Lee Leahy | e0668e4 | 2017-01-08 11:38:14 -0800 | [diff] [blame] | 6 | #include <commonlib/endian.h> |
Elyes HAOUAS | add76f9 | 2019-03-21 09:55:49 +0100 | [diff] [blame] | 7 | #include <console/console.h> |
Stefan Reinauer | d518c7a | 2013-11-04 17:38:32 -0800 | [diff] [blame] | 8 | #include <delay.h> |
Nico Huber | 0f2dd1e | 2017-08-01 14:02:40 +0200 | [diff] [blame] | 9 | #include <device/i2c_simple.h> |
Julius Werner | 9ff8f6f | 2015-02-23 14:31:09 -0800 | [diff] [blame] | 10 | #include <endian.h> |
Lee Leahy | e0668e4 | 2017-01-08 11:38:14 -0800 | [diff] [blame] | 11 | #include <lib.h> |
Philipp Deppenwiese | d88fb36 | 2017-10-18 20:26:18 +0200 | [diff] [blame] | 12 | #include <security/tpm/tis.h> |
Stefan Reinauer | d518c7a | 2013-11-04 17:38:32 -0800 | [diff] [blame] | 13 | |
Elyes HAOUAS | add76f9 | 2019-03-21 09:55:49 +0100 | [diff] [blame] | 14 | #include "tpm.h" |
Stefan Reinauer | d518c7a | 2013-11-04 17:38:32 -0800 | [diff] [blame] | 15 | |
| 16 | /* global structure for tpm chip data */ |
Patrick Georgi | c9b1359 | 2019-11-29 11:47:47 +0100 | [diff] [blame] | 17 | static struct tpm_chip chip; |
Stefan Reinauer | d518c7a | 2013-11-04 17:38:32 -0800 | [diff] [blame] | 18 | |
| 19 | #define TPM_CMD_COUNT_BYTE 2 |
| 20 | #define TPM_CMD_ORDINAL_BYTE 6 |
| 21 | |
| 22 | int tis_open(void) |
| 23 | { |
| 24 | int rc; |
| 25 | |
Patrick Georgi | c9b1359 | 2019-11-29 11:47:47 +0100 | [diff] [blame] | 26 | if (chip.is_open) { |
Elyes HAOUAS | 421285e | 2021-01-16 17:30:08 +0100 | [diff] [blame] | 27 | printk(BIOS_DEBUG, "%s() called twice.\n", __func__); |
Stefan Reinauer | d518c7a | 2013-11-04 17:38:32 -0800 | [diff] [blame] | 28 | return -1; |
| 29 | } |
| 30 | |
Patrick Georgi | c9b1359 | 2019-11-29 11:47:47 +0100 | [diff] [blame] | 31 | rc = tpm_vendor_init(&chip, CONFIG_DRIVER_TPM_I2C_BUS, |
Duncan Laurie | 40ae170 | 2016-08-31 13:51:14 -0700 | [diff] [blame] | 32 | CONFIG_DRIVER_TPM_I2C_ADDR); |
Stefan Reinauer | d518c7a | 2013-11-04 17:38:32 -0800 | [diff] [blame] | 33 | if (rc < 0) |
Patrick Georgi | c9b1359 | 2019-11-29 11:47:47 +0100 | [diff] [blame] | 34 | chip.is_open = 0; |
Stefan Reinauer | d518c7a | 2013-11-04 17:38:32 -0800 | [diff] [blame] | 35 | |
Lee Leahy | 52ab30b | 2017-03-15 09:22:11 -0700 | [diff] [blame] | 36 | if (rc) |
Stefan Reinauer | d518c7a | 2013-11-04 17:38:32 -0800 | [diff] [blame] | 37 | return -1; |
Stefan Reinauer | d518c7a | 2013-11-04 17:38:32 -0800 | [diff] [blame] | 38 | |
| 39 | return 0; |
| 40 | } |
| 41 | |
| 42 | int tis_close(void) |
| 43 | { |
Patrick Georgi | c9b1359 | 2019-11-29 11:47:47 +0100 | [diff] [blame] | 44 | if (chip.is_open) { |
| 45 | tpm_vendor_cleanup(&chip); |
| 46 | chip.is_open = 0; |
Stefan Reinauer | d518c7a | 2013-11-04 17:38:32 -0800 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | return 0; |
| 50 | } |
| 51 | |
| 52 | int tis_init(void) |
| 53 | { |
Duncan Laurie | 1ca1968 | 2016-09-07 10:52:12 -0700 | [diff] [blame] | 54 | return tpm_vendor_probe(CONFIG_DRIVER_TPM_I2C_BUS, |
| 55 | CONFIG_DRIVER_TPM_I2C_ADDR); |
Stefan Reinauer | d518c7a | 2013-11-04 17:38:32 -0800 | [diff] [blame] | 56 | } |
| 57 | |
Aaron Durbin | 9219019 | 2017-03-27 16:20:17 -0500 | [diff] [blame] | 58 | static ssize_t tpm_transmit(const uint8_t *sbuf, size_t sbufsiz, void *rbuf, |
| 59 | size_t rbufsiz) |
Stefan Reinauer | d518c7a | 2013-11-04 17:38:32 -0800 | [diff] [blame] | 60 | { |
| 61 | int rc; |
Aaron Durbin | 9b87844 | 2017-03-27 16:40:03 -0500 | [diff] [blame] | 62 | uint32_t count; |
Stefan Reinauer | d518c7a | 2013-11-04 17:38:32 -0800 | [diff] [blame] | 63 | |
Aaron Durbin | 9219019 | 2017-03-27 16:20:17 -0500 | [diff] [blame] | 64 | memcpy(&count, sbuf + TPM_CMD_COUNT_BYTE, sizeof(count)); |
Stefan Reinauer | d518c7a | 2013-11-04 17:38:32 -0800 | [diff] [blame] | 65 | count = be32_to_cpu(count); |
Stefan Reinauer | d518c7a | 2013-11-04 17:38:32 -0800 | [diff] [blame] | 66 | |
Patrick Georgi | c9b1359 | 2019-11-29 11:47:47 +0100 | [diff] [blame] | 67 | if (!chip.vendor.send || !chip.vendor.status || !chip.vendor.cancel) |
Duncan Laurie | 112ab91 | 2016-09-02 14:45:53 -0700 | [diff] [blame] | 68 | return -1; |
| 69 | |
Stefan Reinauer | d518c7a | 2013-11-04 17:38:32 -0800 | [diff] [blame] | 70 | if (count == 0) { |
Elyes HAOUAS | 421285e | 2021-01-16 17:30:08 +0100 | [diff] [blame] | 71 | printk(BIOS_DEBUG, "%s: no data\n", __func__); |
Stefan Reinauer | d518c7a | 2013-11-04 17:38:32 -0800 | [diff] [blame] | 72 | return -1; |
| 73 | } |
Aaron Durbin | 9219019 | 2017-03-27 16:20:17 -0500 | [diff] [blame] | 74 | if (count > sbufsiz) { |
Elyes HAOUAS | 421285e | 2021-01-16 17:30:08 +0100 | [diff] [blame] | 75 | printk(BIOS_DEBUG, "%s: invalid count value %x %zx\n", __func__, |
Aaron Durbin | 9219019 | 2017-03-27 16:20:17 -0500 | [diff] [blame] | 76 | count, sbufsiz); |
Stefan Reinauer | d518c7a | 2013-11-04 17:38:32 -0800 | [diff] [blame] | 77 | return -1; |
| 78 | } |
| 79 | |
Patrick Georgi | c9b1359 | 2019-11-29 11:47:47 +0100 | [diff] [blame] | 80 | ASSERT(chip.vendor.send); |
Elyes Haouas | 1ef547e | 2022-11-18 15:05:39 +0100 | [diff] [blame^] | 81 | rc = chip.vendor.send(&chip, (uint8_t *)sbuf, count); |
Stefan Reinauer | d518c7a | 2013-11-04 17:38:32 -0800 | [diff] [blame] | 82 | if (rc < 0) { |
Elyes HAOUAS | 421285e | 2021-01-16 17:30:08 +0100 | [diff] [blame] | 83 | printk(BIOS_DEBUG, "%s: tpm_send error\n", __func__); |
Stefan Reinauer | d518c7a | 2013-11-04 17:38:32 -0800 | [diff] [blame] | 84 | goto out; |
| 85 | } |
| 86 | |
Stefan Reinauer | d518c7a | 2013-11-04 17:38:32 -0800 | [diff] [blame] | 87 | int timeout = 2 * 60 * 1000; /* two minutes timeout */ |
| 88 | while (timeout) { |
Patrick Georgi | c9b1359 | 2019-11-29 11:47:47 +0100 | [diff] [blame] | 89 | ASSERT(chip.vendor.status); |
| 90 | uint8_t status = chip.vendor.status(&chip); |
| 91 | if ((status & chip.vendor.req_complete_mask) == |
| 92 | chip.vendor.req_complete_val) { |
Stefan Reinauer | d518c7a | 2013-11-04 17:38:32 -0800 | [diff] [blame] | 93 | goto out_recv; |
| 94 | } |
| 95 | |
Patrick Georgi | c9b1359 | 2019-11-29 11:47:47 +0100 | [diff] [blame] | 96 | if (status == chip.vendor.req_canceled) { |
Lee Leahy | 52ab30b | 2017-03-15 09:22:11 -0700 | [diff] [blame] | 97 | printk(BIOS_DEBUG, |
Elyes HAOUAS | 421285e | 2021-01-16 17:30:08 +0100 | [diff] [blame] | 98 | "%s: Operation Canceled\n", __func__); |
Stefan Reinauer | d518c7a | 2013-11-04 17:38:32 -0800 | [diff] [blame] | 99 | rc = -1; |
| 100 | goto out; |
| 101 | } |
| 102 | mdelay(TPM_TIMEOUT); |
| 103 | timeout--; |
| 104 | } |
| 105 | |
Patrick Georgi | c9b1359 | 2019-11-29 11:47:47 +0100 | [diff] [blame] | 106 | ASSERT(chip.vendor.cancel); |
| 107 | chip.vendor.cancel(&chip); |
Elyes HAOUAS | 421285e | 2021-01-16 17:30:08 +0100 | [diff] [blame] | 108 | printk(BIOS_DEBUG, "%s: Operation Timed out\n", __func__); |
Stefan Reinauer | d518c7a | 2013-11-04 17:38:32 -0800 | [diff] [blame] | 109 | rc = -1; //ETIME; |
| 110 | goto out; |
| 111 | |
| 112 | out_recv: |
| 113 | |
Elyes Haouas | 1ef547e | 2022-11-18 15:05:39 +0100 | [diff] [blame^] | 114 | rc = chip.vendor.recv(&chip, (uint8_t *)rbuf, rbufsiz); |
Stefan Reinauer | d518c7a | 2013-11-04 17:38:32 -0800 | [diff] [blame] | 115 | if (rc < 0) |
Elyes HAOUAS | 421285e | 2021-01-16 17:30:08 +0100 | [diff] [blame] | 116 | printk(BIOS_DEBUG, "%s: tpm_recv: error %d\n", __func__, rc); |
Stefan Reinauer | d518c7a | 2013-11-04 17:38:32 -0800 | [diff] [blame] | 117 | out: |
| 118 | return rc; |
| 119 | } |
| 120 | |
| 121 | int tis_sendrecv(const uint8_t *sendbuf, size_t sbuf_size, |
| 122 | uint8_t *recvbuf, size_t *rbuf_len) |
| 123 | { |
Lee Leahy | e0668e4 | 2017-01-08 11:38:14 -0800 | [diff] [blame] | 124 | ASSERT(sbuf_size >= 10); |
Stefan Reinauer | d518c7a | 2013-11-04 17:38:32 -0800 | [diff] [blame] | 125 | |
Lee Leahy | e0668e4 | 2017-01-08 11:38:14 -0800 | [diff] [blame] | 126 | /* Display the TPM command */ |
Julius Werner | cd49cce | 2019-03-05 16:53:33 -0800 | [diff] [blame] | 127 | if (CONFIG(DRIVER_TPM_DISPLAY_TIS_BYTES)) { |
Lee Leahy | e0668e4 | 2017-01-08 11:38:14 -0800 | [diff] [blame] | 128 | printk(BIOS_DEBUG, "TPM Command: 0x%08x\n", |
| 129 | read_at_be32(sendbuf, sizeof(uint16_t) |
| 130 | + sizeof(uint32_t))); |
| 131 | hexdump(sendbuf, sbuf_size); |
| 132 | } |
Stefan Reinauer | d518c7a | 2013-11-04 17:38:32 -0800 | [diff] [blame] | 133 | |
Aaron Durbin | 9219019 | 2017-03-27 16:20:17 -0500 | [diff] [blame] | 134 | int len = tpm_transmit(sendbuf, sbuf_size, recvbuf, *rbuf_len); |
Stefan Reinauer | d518c7a | 2013-11-04 17:38:32 -0800 | [diff] [blame] | 135 | |
| 136 | if (len < 10) { |
| 137 | *rbuf_len = 0; |
| 138 | return -1; |
| 139 | } |
| 140 | |
| 141 | if (len > *rbuf_len) { |
| 142 | *rbuf_len = len; |
| 143 | return -1; |
| 144 | } |
| 145 | |
Stefan Reinauer | d518c7a | 2013-11-04 17:38:32 -0800 | [diff] [blame] | 146 | *rbuf_len = len; |
| 147 | |
Lee Leahy | e0668e4 | 2017-01-08 11:38:14 -0800 | [diff] [blame] | 148 | /* Display the TPM response */ |
Julius Werner | cd49cce | 2019-03-05 16:53:33 -0800 | [diff] [blame] | 149 | if (CONFIG(DRIVER_TPM_DISPLAY_TIS_BYTES)) { |
Lee Leahy | e0668e4 | 2017-01-08 11:38:14 -0800 | [diff] [blame] | 150 | printk(BIOS_DEBUG, "TPM Response: 0x%08x\n", |
| 151 | read_at_be32(recvbuf, sizeof(uint16_t) |
| 152 | + sizeof(uint32_t))); |
| 153 | hexdump(recvbuf, *rbuf_len); |
| 154 | } |
| 155 | |
Stefan Reinauer | d518c7a | 2013-11-04 17:38:32 -0800 | [diff] [blame] | 156 | return 0; |
| 157 | } |