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 | |
Sergii Dmytruk | 4ee0317 | 2022-12-22 19:35:25 +0200 | [diff] [blame^] | 22 | int tis_open(void) |
| 23 | { |
| 24 | int rc; |
| 25 | |
| 26 | if (chip.is_open) { |
| 27 | printk(BIOS_DEBUG, "%s() called twice.\n", __func__); |
| 28 | return -1; |
| 29 | } |
| 30 | |
| 31 | rc = tpm_vendor_init(&chip, CONFIG_DRIVER_TPM_I2C_BUS, |
| 32 | CONFIG_DRIVER_TPM_I2C_ADDR); |
| 33 | if (rc < 0) |
| 34 | chip.is_open = 0; |
| 35 | |
| 36 | if (rc) |
| 37 | return -1; |
| 38 | |
| 39 | return 0; |
| 40 | } |
| 41 | |
| 42 | int tis_init(void) |
| 43 | { |
| 44 | return tpm_vendor_probe(CONFIG_DRIVER_TPM_I2C_BUS, |
| 45 | CONFIG_DRIVER_TPM_I2C_ADDR); |
| 46 | } |
| 47 | |
Aaron Durbin | 9219019 | 2017-03-27 16:20:17 -0500 | [diff] [blame] | 48 | static ssize_t tpm_transmit(const uint8_t *sbuf, size_t sbufsiz, void *rbuf, |
| 49 | size_t rbufsiz) |
Stefan Reinauer | d518c7a | 2013-11-04 17:38:32 -0800 | [diff] [blame] | 50 | { |
| 51 | int rc; |
Aaron Durbin | 9b87844 | 2017-03-27 16:40:03 -0500 | [diff] [blame] | 52 | uint32_t count; |
Stefan Reinauer | d518c7a | 2013-11-04 17:38:32 -0800 | [diff] [blame] | 53 | |
Aaron Durbin | 9219019 | 2017-03-27 16:20:17 -0500 | [diff] [blame] | 54 | memcpy(&count, sbuf + TPM_CMD_COUNT_BYTE, sizeof(count)); |
Stefan Reinauer | d518c7a | 2013-11-04 17:38:32 -0800 | [diff] [blame] | 55 | count = be32_to_cpu(count); |
Stefan Reinauer | d518c7a | 2013-11-04 17:38:32 -0800 | [diff] [blame] | 56 | |
Sergii Dmytruk | 86f845a | 2022-10-29 18:55:24 +0300 | [diff] [blame] | 57 | if (!chip.send || !chip.status || !chip.cancel) |
Duncan Laurie | 112ab91 | 2016-09-02 14:45:53 -0700 | [diff] [blame] | 58 | return -1; |
| 59 | |
Stefan Reinauer | d518c7a | 2013-11-04 17:38:32 -0800 | [diff] [blame] | 60 | if (count == 0) { |
Elyes HAOUAS | 421285e | 2021-01-16 17:30:08 +0100 | [diff] [blame] | 61 | printk(BIOS_DEBUG, "%s: no data\n", __func__); |
Stefan Reinauer | d518c7a | 2013-11-04 17:38:32 -0800 | [diff] [blame] | 62 | return -1; |
| 63 | } |
Aaron Durbin | 9219019 | 2017-03-27 16:20:17 -0500 | [diff] [blame] | 64 | if (count > sbufsiz) { |
Elyes HAOUAS | 421285e | 2021-01-16 17:30:08 +0100 | [diff] [blame] | 65 | printk(BIOS_DEBUG, "%s: invalid count value %x %zx\n", __func__, |
Aaron Durbin | 9219019 | 2017-03-27 16:20:17 -0500 | [diff] [blame] | 66 | count, sbufsiz); |
Stefan Reinauer | d518c7a | 2013-11-04 17:38:32 -0800 | [diff] [blame] | 67 | return -1; |
| 68 | } |
| 69 | |
Sergii Dmytruk | 86f845a | 2022-10-29 18:55:24 +0300 | [diff] [blame] | 70 | ASSERT(chip.send); |
| 71 | rc = chip.send((uint8_t *)sbuf, count); |
Stefan Reinauer | d518c7a | 2013-11-04 17:38:32 -0800 | [diff] [blame] | 72 | if (rc < 0) { |
Elyes HAOUAS | 421285e | 2021-01-16 17:30:08 +0100 | [diff] [blame] | 73 | printk(BIOS_DEBUG, "%s: tpm_send error\n", __func__); |
Stefan Reinauer | d518c7a | 2013-11-04 17:38:32 -0800 | [diff] [blame] | 74 | goto out; |
| 75 | } |
| 76 | |
Stefan Reinauer | d518c7a | 2013-11-04 17:38:32 -0800 | [diff] [blame] | 77 | int timeout = 2 * 60 * 1000; /* two minutes timeout */ |
| 78 | while (timeout) { |
Sergii Dmytruk | 86f845a | 2022-10-29 18:55:24 +0300 | [diff] [blame] | 79 | ASSERT(chip.status); |
| 80 | uint8_t status = chip.status(); |
| 81 | if ((status & chip.req_complete_mask) == chip.req_complete_val) { |
Stefan Reinauer | d518c7a | 2013-11-04 17:38:32 -0800 | [diff] [blame] | 82 | goto out_recv; |
| 83 | } |
| 84 | |
Sergii Dmytruk | 86f845a | 2022-10-29 18:55:24 +0300 | [diff] [blame] | 85 | if (status == chip.req_canceled) { |
Lee Leahy | 52ab30b | 2017-03-15 09:22:11 -0700 | [diff] [blame] | 86 | printk(BIOS_DEBUG, |
Elyes HAOUAS | 421285e | 2021-01-16 17:30:08 +0100 | [diff] [blame] | 87 | "%s: Operation Canceled\n", __func__); |
Stefan Reinauer | d518c7a | 2013-11-04 17:38:32 -0800 | [diff] [blame] | 88 | rc = -1; |
| 89 | goto out; |
| 90 | } |
| 91 | mdelay(TPM_TIMEOUT); |
| 92 | timeout--; |
| 93 | } |
| 94 | |
Sergii Dmytruk | 86f845a | 2022-10-29 18:55:24 +0300 | [diff] [blame] | 95 | ASSERT(chip.cancel); |
| 96 | chip.cancel(); |
Elyes HAOUAS | 421285e | 2021-01-16 17:30:08 +0100 | [diff] [blame] | 97 | printk(BIOS_DEBUG, "%s: Operation Timed out\n", __func__); |
Stefan Reinauer | d518c7a | 2013-11-04 17:38:32 -0800 | [diff] [blame] | 98 | rc = -1; //ETIME; |
| 99 | goto out; |
| 100 | |
| 101 | out_recv: |
| 102 | |
Sergii Dmytruk | 86f845a | 2022-10-29 18:55:24 +0300 | [diff] [blame] | 103 | rc = chip.recv((uint8_t *)rbuf, rbufsiz); |
Stefan Reinauer | d518c7a | 2013-11-04 17:38:32 -0800 | [diff] [blame] | 104 | if (rc < 0) |
Elyes HAOUAS | 421285e | 2021-01-16 17:30:08 +0100 | [diff] [blame] | 105 | printk(BIOS_DEBUG, "%s: tpm_recv: error %d\n", __func__, rc); |
Stefan Reinauer | d518c7a | 2013-11-04 17:38:32 -0800 | [diff] [blame] | 106 | out: |
| 107 | return rc; |
| 108 | } |
| 109 | |
Sergii Dmytruk | 4ee0317 | 2022-12-22 19:35:25 +0200 | [diff] [blame^] | 110 | int tis_sendrecv(const uint8_t *sendbuf, size_t sbuf_size, |
| 111 | uint8_t *recvbuf, size_t *rbuf_len) |
Stefan Reinauer | d518c7a | 2013-11-04 17:38:32 -0800 | [diff] [blame] | 112 | { |
Lee Leahy | e0668e4 | 2017-01-08 11:38:14 -0800 | [diff] [blame] | 113 | ASSERT(sbuf_size >= 10); |
Stefan Reinauer | d518c7a | 2013-11-04 17:38:32 -0800 | [diff] [blame] | 114 | |
Lee Leahy | e0668e4 | 2017-01-08 11:38:14 -0800 | [diff] [blame] | 115 | /* Display the TPM command */ |
Julius Werner | cd49cce | 2019-03-05 16:53:33 -0800 | [diff] [blame] | 116 | if (CONFIG(DRIVER_TPM_DISPLAY_TIS_BYTES)) { |
Lee Leahy | e0668e4 | 2017-01-08 11:38:14 -0800 | [diff] [blame] | 117 | printk(BIOS_DEBUG, "TPM Command: 0x%08x\n", |
| 118 | read_at_be32(sendbuf, sizeof(uint16_t) |
| 119 | + sizeof(uint32_t))); |
| 120 | hexdump(sendbuf, sbuf_size); |
| 121 | } |
Stefan Reinauer | d518c7a | 2013-11-04 17:38:32 -0800 | [diff] [blame] | 122 | |
Aaron Durbin | 9219019 | 2017-03-27 16:20:17 -0500 | [diff] [blame] | 123 | int len = tpm_transmit(sendbuf, sbuf_size, recvbuf, *rbuf_len); |
Stefan Reinauer | d518c7a | 2013-11-04 17:38:32 -0800 | [diff] [blame] | 124 | |
| 125 | if (len < 10) { |
| 126 | *rbuf_len = 0; |
| 127 | return -1; |
| 128 | } |
| 129 | |
| 130 | if (len > *rbuf_len) { |
| 131 | *rbuf_len = len; |
| 132 | return -1; |
| 133 | } |
| 134 | |
Stefan Reinauer | d518c7a | 2013-11-04 17:38:32 -0800 | [diff] [blame] | 135 | *rbuf_len = len; |
| 136 | |
Lee Leahy | e0668e4 | 2017-01-08 11:38:14 -0800 | [diff] [blame] | 137 | /* Display the TPM response */ |
Julius Werner | cd49cce | 2019-03-05 16:53:33 -0800 | [diff] [blame] | 138 | if (CONFIG(DRIVER_TPM_DISPLAY_TIS_BYTES)) { |
Lee Leahy | e0668e4 | 2017-01-08 11:38:14 -0800 | [diff] [blame] | 139 | printk(BIOS_DEBUG, "TPM Response: 0x%08x\n", |
| 140 | read_at_be32(recvbuf, sizeof(uint16_t) |
| 141 | + sizeof(uint32_t))); |
| 142 | hexdump(recvbuf, *rbuf_len); |
| 143 | } |
| 144 | |
Stefan Reinauer | d518c7a | 2013-11-04 17:38:32 -0800 | [diff] [blame] | 145 | return 0; |
| 146 | } |