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