Patrick Georgi | ac95903 | 2020-05-05 22:49:26 +0200 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0-or-later */ |
Lee Leahy | 1b39f17 | 2017-01-05 17:59:38 -0800 | [diff] [blame] | 2 | |
Lee Leahy | 1b39f17 | 2017-01-05 17:59:38 -0800 | [diff] [blame] | 3 | #include <assert.h> |
| 4 | #include <commonlib/endian.h> |
Elyes HAOUAS | 361a935 | 2019-12-18 21:26:33 +0100 | [diff] [blame] | 5 | #include <commonlib/helpers.h> |
Lee Leahy | 1b39f17 | 2017-01-05 17:59:38 -0800 | [diff] [blame] | 6 | #include <console/console.h> |
| 7 | #include <delay.h> |
Nico Huber | 0f2dd1e | 2017-08-01 14:02:40 +0200 | [diff] [blame] | 8 | #include <device/i2c_simple.h> |
Lee Leahy | 1b39f17 | 2017-01-05 17:59:38 -0800 | [diff] [blame] | 9 | #include <endian.h> |
| 10 | #include <lib.h> |
Philipp Deppenwiese | d88fb36 | 2017-10-18 20:26:18 +0200 | [diff] [blame] | 11 | #include <security/tpm/tis.h> |
Lee Leahy | 1b39f17 | 2017-01-05 17:59:38 -0800 | [diff] [blame] | 12 | #include <timer.h> |
Elyes HAOUAS | ede8dd0 | 2019-06-23 06:57:53 +0200 | [diff] [blame] | 13 | #include <types.h> |
Lee Leahy | 1b39f17 | 2017-01-05 17:59:38 -0800 | [diff] [blame] | 14 | |
| 15 | #define RECV_TIMEOUT (1 * 1000) /* 1 second */ |
| 16 | #define XMIT_TIMEOUT (1 * 1000) /* 1 second */ |
| 17 | #define SLEEP_DURATION 1000 /* microseconds */ |
| 18 | |
| 19 | struct tpm_output_header { |
| 20 | uint16_t tag; |
| 21 | uint32_t length; |
| 22 | uint32_t return_code; |
Stefan Reinauer | 6a00113 | 2017-07-13 02:20:27 +0200 | [diff] [blame] | 23 | } __packed; |
Lee Leahy | 1b39f17 | 2017-01-05 17:59:38 -0800 | [diff] [blame] | 24 | |
Sergii Dmytruk | 963f7b9 | 2022-10-29 20:42:28 +0300 | [diff] [blame] | 25 | static tpm_result_t i2c_tis_sendrecv(const uint8_t *sendbuf, size_t sbuf_size, |
| 26 | uint8_t *recvbuf, size_t *rbuf_len) |
Lee Leahy | 1b39f17 | 2017-01-05 17:59:38 -0800 | [diff] [blame] | 27 | { |
| 28 | size_t hdr_bytes; |
| 29 | struct tpm_output_header *header; |
| 30 | size_t max_recv_bytes; |
| 31 | size_t recv_bytes; |
| 32 | int status; |
| 33 | struct stopwatch sw; |
| 34 | |
| 35 | ASSERT(sbuf_size >= 10); |
Julius Werner | cd49cce | 2019-03-05 16:53:33 -0800 | [diff] [blame] | 36 | if (CONFIG(DRIVER_TPM_DISPLAY_TIS_BYTES)) { |
Lee Leahy | 1b39f17 | 2017-01-05 17:59:38 -0800 | [diff] [blame] | 37 | /* Display the TPM command */ |
| 38 | if (sbuf_size >= 10) |
| 39 | printk(BIOS_DEBUG, "TPM Command: 0x%08x\n", |
| 40 | read_at_be32(sendbuf, sizeof(uint16_t) |
| 41 | + sizeof(uint32_t))); |
| 42 | hexdump(sendbuf, sbuf_size); |
| 43 | } |
| 44 | |
| 45 | /* Send the command to the TPM */ |
| 46 | stopwatch_init_msecs_expire(&sw, XMIT_TIMEOUT); |
| 47 | while (1) { |
| 48 | status = i2c_write_raw(CONFIG_DRIVER_TPM_I2C_BUS, |
| 49 | CONFIG_DRIVER_TPM_I2C_ADDR, (uint8_t *)sendbuf, |
| 50 | sbuf_size); |
| 51 | if ((status < 0) && (!stopwatch_expired(&sw))) |
| 52 | continue; |
Jon Murphy | d7b8dc9 | 2023-09-05 11:36:43 -0600 | [diff] [blame] | 53 | if (status < 0) { |
| 54 | printk(BIOS_ERR, "I2C write error: %d\n", status); |
| 55 | return TPM_CB_COMMUNICATION_ERROR; |
| 56 | } |
Lee Leahy | 1b39f17 | 2017-01-05 17:59:38 -0800 | [diff] [blame] | 57 | break; |
| 58 | } |
| 59 | |
| 60 | /* Read the TPM response header */ |
| 61 | max_recv_bytes = *rbuf_len; |
| 62 | ASSERT(max_recv_bytes >= sizeof(*header)); |
| 63 | hdr_bytes = sizeof(*header); |
| 64 | header = (struct tpm_output_header *)recvbuf; |
| 65 | stopwatch_init_msecs_expire(&sw, RECV_TIMEOUT); |
| 66 | do { |
| 67 | status = i2c_read_raw(CONFIG_DRIVER_TPM_I2C_BUS, |
| 68 | CONFIG_DRIVER_TPM_I2C_ADDR, recvbuf, hdr_bytes); |
| 69 | if (status > 0) |
| 70 | break; |
| 71 | udelay(SLEEP_DURATION); |
| 72 | } while (!stopwatch_expired(&sw)); |
| 73 | if (status != sizeof(*header)) |
Jon Murphy | d7b8dc9 | 2023-09-05 11:36:43 -0600 | [diff] [blame] | 74 | return TPM_CB_COMMUNICATION_ERROR; |
Lee Leahy | 1b39f17 | 2017-01-05 17:59:38 -0800 | [diff] [blame] | 75 | |
| 76 | /* Determine the number of bytes remaining */ |
Elyes HAOUAS | 361a935 | 2019-12-18 21:26:33 +0100 | [diff] [blame] | 77 | recv_bytes = MIN(be32_to_cpu(*(uint32_t *)&header->length), |
Lee Leahy | 1b39f17 | 2017-01-05 17:59:38 -0800 | [diff] [blame] | 78 | max_recv_bytes); |
| 79 | |
| 80 | /* Determine if there is additional response data */ |
| 81 | if (recv_bytes > hdr_bytes) { |
| 82 | /* Display the TPM response */ |
Julius Werner | cd49cce | 2019-03-05 16:53:33 -0800 | [diff] [blame] | 83 | if (CONFIG(DRIVER_TPM_DISPLAY_TIS_BYTES)) |
Lee Leahy | 1b39f17 | 2017-01-05 17:59:38 -0800 | [diff] [blame] | 84 | hexdump(recvbuf, hdr_bytes); |
| 85 | |
| 86 | /* Read the full TPM response */ |
| 87 | status = i2c_read_raw(CONFIG_DRIVER_TPM_I2C_BUS, |
| 88 | CONFIG_DRIVER_TPM_I2C_ADDR, recvbuf, recv_bytes); |
Jon Murphy | d7b8dc9 | 2023-09-05 11:36:43 -0600 | [diff] [blame] | 89 | if (status < 0) { |
| 90 | printk(BIOS_ERR, "I2C read error: %d\n", status); |
| 91 | return TPM_CB_COMMUNICATION_ERROR; |
| 92 | } |
Lee Leahy | 1b39f17 | 2017-01-05 17:59:38 -0800 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | /* Return the number of bytes received */ |
| 96 | *rbuf_len = status; |
| 97 | |
| 98 | /* Display the TPM response */ |
Julius Werner | cd49cce | 2019-03-05 16:53:33 -0800 | [diff] [blame] | 99 | if (CONFIG(DRIVER_TPM_DISPLAY_TIS_BYTES)) { |
Lee Leahy | 1b39f17 | 2017-01-05 17:59:38 -0800 | [diff] [blame] | 100 | printk(BIOS_DEBUG, "TPM Response: 0x%08x\n", |
| 101 | read_at_be32(recvbuf, sizeof(uint16_t) |
| 102 | + sizeof(uint32_t))); |
| 103 | hexdump(recvbuf, *rbuf_len); |
| 104 | } |
| 105 | |
| 106 | /* Successful transfer */ |
Jon Murphy | d7b8dc9 | 2023-09-05 11:36:43 -0600 | [diff] [blame] | 107 | return TPM_SUCCESS; |
Lee Leahy | 1b39f17 | 2017-01-05 17:59:38 -0800 | [diff] [blame] | 108 | } |
Sergii Dmytruk | 963f7b9 | 2022-10-29 20:42:28 +0300 | [diff] [blame] | 109 | |
Sergii Dmytruk | febf9b9 | 2022-10-31 15:30:15 +0200 | [diff] [blame^] | 110 | tis_sendrecv_fn tis_probe(enum tpm_family *family) |
Sergii Dmytruk | 963f7b9 | 2022-10-29 20:42:28 +0300 | [diff] [blame] | 111 | { |
Sergii Dmytruk | febf9b9 | 2022-10-31 15:30:15 +0200 | [diff] [blame^] | 112 | /* |
| 113 | * Can't query version or really anything as the device doesn't support |
| 114 | * much through this interface (can't specify address on accesses). |
| 115 | * |
| 116 | * Hence the assumption is that whatever TPM version is enabled at |
| 117 | * compile-time defines what the device supports. The check is written |
| 118 | * in a way to give TPM 1 preference even if support for both versions |
| 119 | * is compiled in. |
| 120 | */ |
| 121 | if (family != NULL) |
| 122 | *family = CONFIG(TPM1) ? TPM_1 : TPM_2; |
| 123 | |
Sergii Dmytruk | 963f7b9 | 2022-10-29 20:42:28 +0300 | [diff] [blame] | 124 | return &i2c_tis_sendrecv; |
| 125 | } |