2 * This file is part of the coreboot project.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License as
6 * published by the Free Software Foundation; either version 2 of
7 * the License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but without any warranty; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
16 #include <commonlib/endian.h>
17 #include <commonlib/helpers.h>
18 #include <console/console.h>
20 #include <device/i2c_simple.h>
23 #include <security/tpm/tis.h>
27 #define RECV_TIMEOUT (1 * 1000) /* 1 second */
28 #define XMIT_TIMEOUT (1 * 1000) /* 1 second */
29 #define SLEEP_DURATION 1000 /* microseconds */
31 struct tpm_output_header
{
52 int tis_sendrecv(const uint8_t *sendbuf
, size_t sbuf_size
,
53 uint8_t *recvbuf
, size_t *rbuf_len
)
56 struct tpm_output_header
*header
;
57 size_t max_recv_bytes
;
62 ASSERT(sbuf_size
>= 10);
63 if (CONFIG(DRIVER_TPM_DISPLAY_TIS_BYTES
)) {
64 /* Display the TPM command */
66 printk(BIOS_DEBUG
, "TPM Command: 0x%08x\n",
67 read_at_be32(sendbuf
, sizeof(uint16_t)
69 hexdump(sendbuf
, sbuf_size
);
72 /* Send the command to the TPM */
73 stopwatch_init_msecs_expire(&sw
, XMIT_TIMEOUT
);
75 status
= i2c_write_raw(CONFIG_DRIVER_TPM_I2C_BUS
,
76 CONFIG_DRIVER_TPM_I2C_ADDR
, (uint8_t *)sendbuf
,
78 if ((status
< 0) && (!stopwatch_expired(&sw
)))
85 /* Read the TPM response header */
86 max_recv_bytes
= *rbuf_len
;
87 ASSERT(max_recv_bytes
>= sizeof(*header
));
88 hdr_bytes
= sizeof(*header
);
89 header
= (struct tpm_output_header
*)recvbuf
;
90 stopwatch_init_msecs_expire(&sw
, RECV_TIMEOUT
);
92 status
= i2c_read_raw(CONFIG_DRIVER_TPM_I2C_BUS
,
93 CONFIG_DRIVER_TPM_I2C_ADDR
, recvbuf
, hdr_bytes
);
96 udelay(SLEEP_DURATION
);
97 } while (!stopwatch_expired(&sw
));
98 if (status
!= sizeof(*header
))
101 /* Determine the number of bytes remaining */
102 recv_bytes
= MIN(be32_to_cpu(*(uint32_t *)&header
->length
),
105 /* Determine if there is additional response data */
106 if (recv_bytes
> hdr_bytes
) {
107 /* Display the TPM response */
108 if (CONFIG(DRIVER_TPM_DISPLAY_TIS_BYTES
))
109 hexdump(recvbuf
, hdr_bytes
);
111 /* Read the full TPM response */
112 status
= i2c_read_raw(CONFIG_DRIVER_TPM_I2C_BUS
,
113 CONFIG_DRIVER_TPM_I2C_ADDR
, recvbuf
, recv_bytes
);
118 /* Return the number of bytes received */
121 /* Display the TPM response */
122 if (CONFIG(DRIVER_TPM_DISPLAY_TIS_BYTES
)) {
123 printk(BIOS_DEBUG
, "TPM Response: 0x%08x\n",
124 read_at_be32(recvbuf
, sizeof(uint16_t)
125 + sizeof(uint32_t)));
126 hexdump(recvbuf
, *rbuf_len
);
129 /* Successful transfer */