Patrick Georgi | 593124d | 2020-05-10 19:44:08 +0200 | [diff] [blame] | 1 | /* SPDX-License-Identifier: BSD-3-Clause */ |
Vadim Bendebury | e31d243 | 2016-04-09 18:33:49 -0700 | [diff] [blame] | 2 | |
Vadim Bendebury | e31d243 | 2016-04-09 18:33:49 -0700 | [diff] [blame] | 3 | #include <console/console.h> |
Philipp Deppenwiese | d88fb36 | 2017-10-18 20:26:18 +0200 | [diff] [blame] | 4 | #include <security/tpm/tis.h> |
Vadim Bendebury | e31d243 | 2016-04-09 18:33:49 -0700 | [diff] [blame] | 5 | |
| 6 | #include "tpm.h" |
| 7 | |
Sergii Dmytruk | 4ee0317 | 2022-12-22 19:35:25 +0200 | [diff] [blame^] | 8 | static unsigned int tpm_is_open; |
| 9 | |
Vadim Bendebury | e31d243 | 2016-04-09 18:33:49 -0700 | [diff] [blame] | 10 | static const struct { |
| 11 | uint16_t vid; |
| 12 | uint16_t did; |
| 13 | const char *device_name; |
| 14 | } dev_map[] = { |
| 15 | { 0x15d1, 0x001b, "SLB9670" }, |
| 16 | { 0x1ae0, 0x0028, "CR50" }, |
Patrick Rudolph | 7bcd9a1 | 2020-03-20 09:55:43 +0100 | [diff] [blame] | 17 | { 0x104a, 0x0000, "ST33HTPH2E32" }, |
Yidi Lin | 93447c4 | 2022-08-16 15:19:32 +0800 | [diff] [blame] | 18 | { 0x6666, 0x504a, "TI50" }, |
Vadim Bendebury | e31d243 | 2016-04-09 18:33:49 -0700 | [diff] [blame] | 19 | }; |
| 20 | |
| 21 | static const char *tis_get_dev_name(struct tpm2_info *info) |
| 22 | { |
| 23 | int i; |
| 24 | |
| 25 | for (i = 0; i < ARRAY_SIZE(dev_map); i++) |
| 26 | if ((dev_map[i].vid == info->vendor_id) && |
| 27 | (dev_map[i].did == info->device_id)) |
| 28 | return dev_map[i].device_name; |
| 29 | return "Unknown"; |
| 30 | } |
| 31 | |
Sergii Dmytruk | 4ee0317 | 2022-12-22 19:35:25 +0200 | [diff] [blame^] | 32 | int tis_open(void) |
| 33 | { |
| 34 | if (tpm_is_open) { |
| 35 | printk(BIOS_ERR, "%s() called twice.\n", __func__); |
| 36 | return -1; |
| 37 | } |
| 38 | return 0; |
| 39 | } |
| 40 | |
| 41 | int tis_init(void) |
| 42 | { |
| 43 | struct spi_slave spi; |
| 44 | struct tpm2_info info; |
| 45 | |
| 46 | if (spi_setup_slave(CONFIG_DRIVER_TPM_SPI_BUS, |
| 47 | CONFIG_DRIVER_TPM_SPI_CHIP, &spi)) { |
| 48 | printk(BIOS_ERR, "Failed to setup TPM SPI slave\n"); |
| 49 | return -1; |
| 50 | } |
| 51 | |
| 52 | if (tpm2_init(&spi)) { |
| 53 | printk(BIOS_ERR, "Failed to initialize TPM SPI interface\n"); |
| 54 | return -1; |
| 55 | } |
| 56 | |
| 57 | tpm2_get_info(&info); |
| 58 | |
| 59 | printk(BIOS_INFO, "Initialized TPM device %s revision %d\n", |
| 60 | tis_get_dev_name(&info), info.revision); |
| 61 | |
| 62 | return 0; |
| 63 | } |
| 64 | |
| 65 | int tis_sendrecv(const uint8_t *sendbuf, size_t sbuf_size, |
| 66 | uint8_t *recvbuf, size_t *rbuf_len) |
Vadim Bendebury | e31d243 | 2016-04-09 18:33:49 -0700 | [diff] [blame] | 67 | { |
| 68 | int len = tpm2_process_command(sendbuf, sbuf_size, recvbuf, *rbuf_len); |
Aaron Durbin | 6ef52cd | 2017-03-27 17:04:26 -0500 | [diff] [blame] | 69 | |
| 70 | if (len == 0) |
| 71 | return -1; |
| 72 | |
Vadim Bendebury | e31d243 | 2016-04-09 18:33:49 -0700 | [diff] [blame] | 73 | *rbuf_len = len; |
Aaron Durbin | 6ef52cd | 2017-03-27 17:04:26 -0500 | [diff] [blame] | 74 | |
Vadim Bendebury | e31d243 | 2016-04-09 18:33:49 -0700 | [diff] [blame] | 75 | return 0; |
| 76 | } |