1 /* SPDX-License-Identifier: BSD-3-Clause */
2 /* This is a driver for a SPI interfaced TPM2 device.
3 *
4 * It assumes that the required SPI interface has been initialized before the
5 * driver is started. A 'struct spi_slave' pointer passed at initialization is
6 * used to direct traffic to the correct SPI interface. This driver does not
7 * provide a way to instantiate multiple TPM devices. Also, to keep things
8 * simple, the driver unconditionally uses of TPM locality zero.
9 *
10 * References to documentation are based on the TCG issued "TPM Profile (PTP)
11 * Specification Revision 00.43".
12 */
13
14 #include <assert.h>
15 #include <commonlib/endian.h>
16 #include <console/console.h>
17 #include <delay.h>
18 #include <drivers/tpm/cr50.h>
19 #include <endian.h>
20 #include <security/tpm/tis.h>
21 #include <string.h>
22 #include <timer.h>
23 #include <types.h>
24
25 #include "tpm.h"
26
27 /* Assorted TPM2 registers for interface type FIFO. */
28 #define TPM_ACCESS_REG (TPM_LOCALITY_0_SPI_BASE + 0)
29 #define TPM_STS_REG (TPM_LOCALITY_0_SPI_BASE + 0x18)
30 #define TPM_DATA_FIFO_REG (TPM_LOCALITY_0_SPI_BASE + 0x24)
31 #define TPM_INTF_ID_REG (TPM_LOCALITY_0_SPI_BASE + 0x30)
32 #define TPM_DID_VID_REG (TPM_LOCALITY_0_SPI_BASE + 0xf00)
33 #define TPM_RID_REG (TPM_LOCALITY_0_SPI_BASE + 0xf04)
34 #define TPM_FW_VER (TPM_LOCALITY_0_SPI_BASE + 0xf90)
35 #define CR50_BOARD_CFG (TPM_LOCALITY_0_SPI_BASE + 0xfe0)
36
37 #define CR50_TIMEOUT_INIT_MS 30000 /* Very long timeout for TPM init */
38
39 /* SPI slave structure for TPM device. */
40 static struct spi_slave spi_slave;
41
42 /* Cached TPM device identification. */
43 static struct tpm2_info tpm_info;
44
45 /*
46 * TODO(vbendeb): make CONFIG(DEBUG_TPM) an int to allow different level of
47 * debug traces. Right now it is either 0 or 1.
48 */
49 static const int debug_level_ = CONFIG(DEBUG_TPM);
50
51 /*
52 * SPI frame header for TPM transactions is 4 bytes in size, it is described
53 * in section "6.4.6 Spi Bit Protocol".
54 */
55 typedef struct {
56 unsigned char body[4];
57 } spi_frame_header;
58
tpm2_get_info(struct tpm2_info * info)59 void tpm2_get_info(struct tpm2_info *info)
60 {
61 *info = tpm_info;
62 }
63
64 /*
65 * Each TPM2 SPI transaction starts the same: CS is asserted, the 4 byte
66 * header is sent to the TPM, the master waits til TPM is ready to continue.
67 */
start_transaction(int read_write,size_t bytes,unsigned int addr)68 static enum cb_err start_transaction(int read_write, size_t bytes, unsigned int addr)
69 {
70 spi_frame_header header, header_resp;
71 uint8_t byte;
72 int i;
73 int ret;
74 struct stopwatch sw;
75 static int tpm_sync_needed;
76 static struct stopwatch wake_up_sw;
77
78 if (CONFIG(TPM_GOOGLE)) {
79 /*
80 * First Cr50 access in each coreboot stage where TPM is used will be
81 * prepended by a wake up pulse on the CS line.
82 */
83 int wakeup_needed = 1;
84
85 /* Wait for TPM to finish previous transaction if needed */
86 if (tpm_sync_needed) {
87 if (cr50_wait_tpm_ready() != CB_SUCCESS)
88 printk(BIOS_ERR, "Timeout waiting for TPM IRQ!\n");
89
90 /*
91 * During the first invocation of this function on each stage
92 * this if () clause code does not run (as tpm_sync_needed
93 * value is zero), during all following invocations the
94 * stopwatch below is guaranteed to be started.
95 */
96 if (!stopwatch_expired(&wake_up_sw))
97 wakeup_needed = 0;
98 } else {
99 tpm_sync_needed = 1;
100 }
101
102 if (wakeup_needed) {
103 /* Just in case Cr50 is asleep. */
104 spi_claim_bus(&spi_slave);
105 udelay(1);
106 spi_release_bus(&spi_slave);
107 udelay(100);
108 }
109
110 /*
111 * The Cr50 on H1 does not go to sleep for 1 second after any
112 * SPI slave activity, let's be conservative and limit the
113 * window to 900 ms.
114 */
115 stopwatch_init_msecs_expire(&wake_up_sw, 900);
116 }
117
118 /*
119 * The first byte of the frame header encodes the transaction type
120 * (read or write) and transfer size (set to length - 1), limited to
121 * 64 bytes.
122 */
123 header.body[0] = (read_write ? 0x80 : 0) | 0x40 | (bytes - 1);
124
125 /* The rest of the frame header is the TPM register address. */
126 for (i = 0; i < 3; i++)
127 header.body[i + 1] = (addr >> (8 * (2 - i))) & 0xff;
128
129 /* CS assert wakes up the slave. */
130 spi_claim_bus(&spi_slave);
131
132 /*
133 * The TCG TPM over SPI specification introduces the notion of SPI
134 * flow control (Section "6.4.5 Flow Control").
135 *
136 * Again, the slave (TPM device) expects each transaction to start
137 * with a 4 byte header transmitted by master. The header indicates if
138 * the master needs to read or write a register, and the register
139 * address.
140 *
141 * If the slave needs to stall the transaction (for instance it is not
142 * ready to send the register value to the master), it sets the MOSI
143 * line to 0 during the last clock of the 4 byte header. In this case
144 * the master is supposed to start polling the SPI bus, one byte at
145 * time, until the last bit in the received byte (transferred during
146 * the last clock of the byte) is set to 1.
147 *
148 * Due to some SPI controllers' shortcomings (Rockchip comes to
149 * mind...) we transmit the 4 byte header without checking the byte
150 * transmitted by the TPM during the transaction's last byte.
151 *
152 * We know that cr50 is guaranteed to set the flow control bit to 0
153 * during the header transfer. Real TPM2 are fast enough to not require
154 * to stall the master. They might still use this feature, so test the
155 * last bit after shifting in the address bytes.
156 * crosbug.com/p/52132 has been opened to track this.
157 */
158
159 header_resp.body[3] = 0;
160 if (CONFIG(TPM_GOOGLE))
161 ret = spi_xfer(&spi_slave, header.body, sizeof(header.body), NULL, 0);
162 else
163 ret = spi_xfer(&spi_slave, header.body, sizeof(header.body),
164 header_resp.body, sizeof(header_resp.body));
165 if (ret) {
166 printk(BIOS_ERR, "SPI-TPM: transfer error\n");
167 spi_release_bus(&spi_slave);
168 return CB_ERR;
169 }
170
171 if (header_resp.body[3] & 1)
172 return CB_SUCCESS;
173
174 /*
175 * Now poll the bus until TPM removes the stall bit. Give it up to 100
176 * ms to sort it out - it could be saving stuff in nvram at some point.
177 */
178 stopwatch_init_msecs_expire(&sw, 100);
179 do {
180 if (stopwatch_expired(&sw)) {
181 printk(BIOS_ERR, "TPM flow control failure\n");
182 spi_release_bus(&spi_slave);
183 return CB_ERR;
184 }
185 spi_xfer(&spi_slave, NULL, 0, &byte, 1);
186 } while (!(byte & 1));
187
188 return CB_SUCCESS;
189 }
190
191 /*
192 * Print out the contents of a buffer, if debug is enabled. Skip registers
193 * other than FIFO, unless debug_level_ is 2.
194 */
trace_dump(const char * prefix,uint32_t reg,size_t bytes,const uint8_t * buffer,int force)195 static void trace_dump(const char *prefix, uint32_t reg,
196 size_t bytes, const uint8_t *buffer,
197 int force)
198 {
199 static char prev_prefix;
200 static unsigned int prev_reg;
201 static int current_char;
202 const int BYTES_PER_LINE = 32;
203
204 if (!force) {
205 if (!debug_level_)
206 return;
207
208 if ((debug_level_ < 2) && (reg != TPM_DATA_FIFO_REG))
209 return;
210 }
211
212 /*
213 * Do not print register address again if the last dump print was for
214 * that register.
215 */
216 if (prev_prefix != *prefix || (prev_reg != reg)) {
217 prev_prefix = *prefix;
218 prev_reg = reg;
219 printk(BIOS_DEBUG, "\n%s %2.2x:", prefix, reg);
220 current_char = 0;
221 }
222
223 if ((reg != TPM_DATA_FIFO_REG) && (bytes == 4)) {
224 /*
225 * This must be a regular register address, print the 32 bit
226 * value.
227 */
228 printk(BIOS_DEBUG, " %8.8x", *(const uint32_t *)buffer);
229 } else {
230 int i;
231
232 /*
233 * Data read from or written to FIFO or not in 4 byte
234 * quantities is printed byte at a time.
235 */
236 for (i = 0; i < bytes; i++) {
237 if (current_char &&
238 !(current_char % BYTES_PER_LINE)) {
239 printk(BIOS_DEBUG, "\n ");
240 current_char = 0;
241 }
242 (current_char)++;
243 printk(BIOS_DEBUG, " %2.2x", buffer[i]);
244 }
245 }
246 }
247
248 /*
249 * Once transaction is initiated and the TPM indicated that it is ready to go,
250 * write the actual bytes to the register.
251 */
write_bytes(const void * buffer,size_t bytes)252 static void write_bytes(const void *buffer, size_t bytes)
253 {
254 spi_xfer(&spi_slave, buffer, bytes, NULL, 0);
255 }
256
257 /*
258 * Once transaction is initiated and the TPM indicated that it is ready to go,
259 * read the actual bytes from the register.
260 */
read_bytes(void * buffer,size_t bytes)261 static void read_bytes(void *buffer, size_t bytes)
262 {
263 spi_xfer(&spi_slave, NULL, 0, buffer, bytes);
264 }
265
266 /*
267 * To write a register, start transaction, transfer data to the TPM, deassert
268 * CS when done.
269 */
tpm2_write_reg(unsigned int reg_number,const void * buffer,size_t bytes)270 static enum cb_err tpm2_write_reg(unsigned int reg_number, const void *buffer, size_t bytes)
271 {
272 trace_dump("W", reg_number, bytes, buffer, 0);
273 if (start_transaction(false, bytes, reg_number) != CB_SUCCESS)
274 return CB_ERR;
275 write_bytes(buffer, bytes);
276 spi_release_bus(&spi_slave);
277 return CB_SUCCESS;
278 }
279
280 /*
281 * To read a register, start transaction, transfer data from the TPM, deassert
282 * CS when done.
283 *
284 * In case of failure zero out the user buffer.
285 */
tpm2_read_reg(unsigned int reg_number,void * buffer,size_t bytes)286 static enum cb_err tpm2_read_reg(unsigned int reg_number, void *buffer, size_t bytes)
287 {
288 if (start_transaction(true, bytes, reg_number) != CB_SUCCESS) {
289 memset(buffer, 0, bytes);
290 return CB_ERR;
291 }
292 read_bytes(buffer, bytes);
293 spi_release_bus(&spi_slave);
294 trace_dump("R", reg_number, bytes, buffer, 0);
295 return CB_SUCCESS;
296 }
297
298 /*
299 * Status register is accessed often, wrap reading and writing it into
300 * dedicated functions.
301 */
read_tpm_sts(uint32_t * status)302 static enum cb_err read_tpm_sts(uint32_t *status)
303 {
304 return tpm2_read_reg(TPM_STS_REG, status, sizeof(*status));
305 }
306
write_tpm_sts(uint32_t status)307 static enum cb_err __must_check write_tpm_sts(uint32_t status)
308 {
309 return tpm2_write_reg(TPM_STS_REG, &status, sizeof(status));
310 }
311
312 /*
313 * The TPM may limit the transaction bytes count (burst count) below the 64
314 * bytes max. The current value is available as a field of the status
315 * register.
316 */
get_burst_count(void)317 static uint32_t get_burst_count(void)
318 {
319 uint32_t status;
320
321 read_tpm_sts(&status);
322 return (status & TPM_STS_BURST_COUNT_MASK) >> TPM_STS_BURST_COUNT_SHIFT;
323 }
324
tpm2_read_access_reg(void)325 static uint8_t tpm2_read_access_reg(void)
326 {
327 uint8_t access;
328 tpm2_read_reg(TPM_ACCESS_REG, &access, sizeof(access));
329 /* We do not care about access establishment bit state. Ignore it. */
330 return access & ~TPM_ACCESS_ESTABLISHMENT;
331 }
332
tpm2_write_access_reg(uint8_t cmd)333 static void tpm2_write_access_reg(uint8_t cmd)
334 {
335 /* Writes to access register can set only 1 bit at a time. */
336 assert(!(cmd & (cmd - 1)));
337
338 tpm2_write_reg(TPM_ACCESS_REG, &cmd, sizeof(cmd));
339 }
340
tpm2_claim_locality(void)341 static enum cb_err tpm2_claim_locality(void)
342 {
343 uint8_t access;
344 struct stopwatch sw;
345
346 /*
347 * Locality is released by TPM reset.
348 *
349 * If locality is taken at this point, this could be due to the fact
350 * that the TPM is performing a long operation and has not processed
351 * reset request yet. We'll wait up to CR50_TIMEOUT_INIT_MS and see if
352 * it releases locality when reset is processed.
353 */
354 stopwatch_init_msecs_expire(&sw, CR50_TIMEOUT_INIT_MS);
355 do {
356 access = tpm2_read_access_reg();
357 if (access & TPM_ACCESS_ACTIVE_LOCALITY) {
358 /*
359 * Don't bombard the chip with traffic, let it keep
360 * processing the command.
361 */
362 mdelay(2);
363 continue;
364 }
365
366 /*
367 * Ok, the locality is free, TPM must be reset, let's claim
368 * it.
369 */
370
371 tpm2_write_access_reg(TPM_ACCESS_REQUEST_USE);
372 access = tpm2_read_access_reg();
373 if (access != (TPM_ACCESS_VALID | TPM_ACCESS_ACTIVE_LOCALITY)) {
374 break;
375 }
376
377 printk(BIOS_INFO, "TPM ready after %lld ms\n",
378 stopwatch_duration_msecs(&sw));
379
380 return CB_SUCCESS;
381 } while (!stopwatch_expired(&sw));
382
383 printk(BIOS_ERR,
384 "Failed to claim locality 0 after %lld ms, status: %#x\n",
385 stopwatch_duration_msecs(&sw), access);
386
387 return CB_ERR;
388 }
389
390 /* Device/vendor ID values of the TPM devices this driver supports. */
391 static const uint32_t supported_did_vids[] = {
392 0x00281ae0, /* H1 based Cr50 security chip. */
393 0x504a6666, /* H1D3C based Ti50 security chip. */
394 0x50666666, /* OpenTitan based Ti50 security chip. */
395 0x0000104a /* ST33HTPH2E32 */
396 };
397
tpm2_init(struct spi_slave * spi_if)398 tpm_result_t tpm2_init(struct spi_slave *spi_if)
399 {
400 uint32_t did_vid, status, intf_id;
401 uint8_t cmd;
402 int retries;
403
404 memcpy(&spi_slave, spi_if, sizeof(*spi_if));
405
406 /* Clear any pending IRQs. */
407 if (CONFIG(TPM_GOOGLE))
408 cr50_plat_irq_status();
409
410 /*
411 * 150 ms should be enough to synchronize with the TPM even under the
412 * worst nested reset request conditions. In vast majority of cases
413 * there would be no wait at all.
414 */
415 printk(BIOS_INFO, "Probing TPM: ");
416 for (retries = 15; retries > 0; retries--) {
417 int i;
418
419 /* In case of failure to read div_vid is set to zero. */
420 tpm2_read_reg(TPM_DID_VID_REG, &did_vid, sizeof(did_vid));
421
422 for (i = 0; i < ARRAY_SIZE(supported_did_vids); i++)
423 if (did_vid == supported_did_vids[i])
424 break; /* TPM is up and ready. */
425
426 if (i < ARRAY_SIZE(supported_did_vids))
427 break;
428
429 /* TPM might be resetting, let's retry in a bit. */
430 mdelay(10);
431 printk(BIOS_INFO, ".");
432 }
433
434 if (!retries) {
435 printk(BIOS_ERR, "\n%s: Failed to connect to the TPM\n",
436 __func__);
437 return TPM_CB_FAIL;
438 }
439
440 printk(BIOS_INFO, " done!\n");
441
442 /* Google TPMs haven't always been 100% accurate in reflecting the spec (particularly
443 * on older versions) and are always TPM 2.0. */
444 if (!CONFIG(TPM_GOOGLE)) {
445 if (tpm2_read_reg(TPM_INTF_ID_REG, &intf_id, sizeof(intf_id)) != CB_SUCCESS) {
446 printk(BIOS_ERR, "\n%s: Failed to read interface ID register\n",
447 __func__);
448 return TPM_CB_FAIL;
449 }
450 if ((be32toh(intf_id) & 0xF) == 0xF) {
451 printk(BIOS_DEBUG, "\n%s: Not a TPM2 device\n", __func__);
452 return TPM_CB_FAIL;
453 }
454 }
455
456 // FIXME: Move this to tpm_setup()
457 if (tpm_first_access_this_boot())
458 /*
459 * Claim locality 0, do it only during the first
460 * initialization after reset.
461 */
462 if (tpm2_claim_locality() != CB_SUCCESS)
463 return TPM_CB_FAIL;
464
465 if (read_tpm_sts(&status) != CB_SUCCESS) {
466 printk(BIOS_ERR, "Reading status reg failed\n");
467 return TPM_CB_FAIL;
468 }
469 if ((status & TPM_STS_FAMILY_MASK) != TPM_STS_FAMILY_TPM_2_0) {
470 printk(BIOS_ERR, "unexpected TPM family value, status: %#x\n",
471 status);
472 return TPM_CB_FAIL;
473 }
474
475 /*
476 * Locality claimed, read the revision value and set up the tpm_info
477 * structure.
478 */
479 tpm2_read_reg(TPM_RID_REG, &cmd, sizeof(cmd));
480 tpm_info.vendor_id = did_vid & 0xffff;
481 tpm_info.device_id = did_vid >> 16;
482 tpm_info.revision = cmd;
483
484 printk(BIOS_INFO, "Connected to device vid:did:rid of %4.4x:%4.4x:%2.2x\n",
485 tpm_info.vendor_id, tpm_info.device_id, tpm_info.revision);
486
487 /* Do some GSC-specific things here. */
488 if (CONFIG(TPM_GOOGLE)) {
489 if (tpm_first_access_this_boot()) {
490 /* This is called for the side-effect of printing the firmware version
491 string */
492 cr50_get_firmware_version(NULL);
493 cr50_set_board_cfg();
494 }
495 }
496 return TPM_SUCCESS;
497 }
498
499 /*
500 * This is in seconds, certain TPM commands, like key generation, can take
501 * long time to complete.
502 */
503 #define MAX_STATUS_TIMEOUT 120
wait_for_status(uint32_t status_mask,uint32_t status_expected)504 static enum cb_err wait_for_status(uint32_t status_mask, uint32_t status_expected)
505 {
506 uint32_t status;
507 struct stopwatch sw;
508
509 stopwatch_init_usecs_expire(&sw, MAX_STATUS_TIMEOUT * 1000 * 1000);
510 do {
511 udelay(1000);
512 if (stopwatch_expired(&sw)) {
513 printk(BIOS_ERR, "failed to get expected status %#x\n",
514 status_expected);
515 return CB_ERR;
516 }
517 read_tpm_sts(&status);
518 } while ((status & status_mask) != status_expected);
519
520 return CB_SUCCESS;
521 }
522
523 enum fifo_transfer_direction {
524 fifo_transmit = 0,
525 fifo_receive = 1
526 };
527
528 /* Union allows to avoid casting away 'const' on transmit buffers. */
529 union fifo_transfer_buffer {
530 uint8_t *rx_buffer;
531 const uint8_t *tx_buffer;
532 };
533
534 /*
535 * Transfer requested number of bytes to or from TPM FIFO, accounting for the
536 * current burst count value.
537 */
fifo_transfer(size_t transfer_size,union fifo_transfer_buffer buffer,enum fifo_transfer_direction direction)538 static enum cb_err __must_check fifo_transfer(size_t transfer_size,
539 union fifo_transfer_buffer buffer,
540 enum fifo_transfer_direction direction)
541 {
542 size_t transaction_size;
543 size_t burst_count;
544 size_t handled_so_far = 0;
545
546 do {
547 do {
548 /* Could be zero when TPM is busy. */
549 burst_count = get_burst_count();
550 } while (!burst_count);
551
552 transaction_size = transfer_size - handled_so_far;
553 transaction_size = MIN(transaction_size, burst_count);
554
555 /*
556 * The SPI frame header does not allow to pass more than 64
557 * bytes.
558 */
559 transaction_size = MIN(transaction_size, 64);
560
561 if (direction == fifo_receive) {
562 if (tpm2_read_reg(TPM_DATA_FIFO_REG,
563 buffer.rx_buffer + handled_so_far,
564 transaction_size) != CB_SUCCESS)
565 return CB_ERR;
566 } else {
567 if (tpm2_write_reg(TPM_DATA_FIFO_REG,
568 buffer.tx_buffer + handled_so_far,
569 transaction_size) != CB_SUCCESS)
570 return CB_ERR;
571 }
572
573 handled_so_far += transaction_size;
574
575 } while (handled_so_far != transfer_size);
576
577 return CB_SUCCESS;
578 }
579
tpm2_process_command(const void * tpm2_command,size_t command_size,void * tpm2_response,size_t max_response)580 size_t tpm2_process_command(const void *tpm2_command, size_t command_size,
581 void *tpm2_response, size_t max_response)
582 {
583 uint32_t status;
584 uint32_t expected_status_bits;
585 size_t payload_size;
586 size_t bytes_to_go;
587 const uint8_t *cmd_body = tpm2_command;
588 uint8_t *rsp_body = tpm2_response;
589 union fifo_transfer_buffer fifo_buffer;
590 const int HEADER_SIZE = 6;
591
592 /* Do not try using an uninitialized TPM. */
593 if (!tpm_info.vendor_id)
594 return 0;
595
596 /* Skip the two byte tag, read the size field. */
597 payload_size = read_be32(cmd_body + 2);
598
599 /* Sanity check. */
600 if (payload_size != command_size) {
601 printk(BIOS_ERR,
602 "Command size mismatch: encoded %zd != requested %zd\n",
603 payload_size, command_size);
604 trace_dump("W", TPM_DATA_FIFO_REG, command_size, cmd_body, 1);
605 printk(BIOS_DEBUG, "\n");
606 return 0;
607 }
608
609 /* Let the TPM know that the command is coming. */
610 if (write_tpm_sts(TPM_STS_COMMAND_READY) != CB_SUCCESS) {
611 printk(BIOS_ERR, "TPM_STS_COMMAND_READY failed\n");
612 return 0;
613 }
614
615 /*
616 * TPM commands and responses written to and read from the FIFO
617 * register (0x24) are datagrams of variable size, prepended by a 6
618 * byte header.
619 *
620 * The specification description of the state machine is a bit vague,
621 * but from experience it looks like there is no need to wait for the
622 * sts.expect bit to be set, at least with the 9670 and cr50 devices.
623 * Just write the command into FIFO, making sure not to exceed the
624 * burst count or the maximum PDU size, whatever is smaller.
625 */
626 fifo_buffer.tx_buffer = cmd_body;
627 if (fifo_transfer(command_size, fifo_buffer, fifo_transmit) != CB_SUCCESS) {
628 printk(BIOS_ERR, "fifo_transfer %zd command bytes failed\n",
629 command_size);
630 return 0;
631 }
632
633 /* Now tell the TPM it can start processing the command. */
634 if (write_tpm_sts(TPM_STS_GO) != CB_SUCCESS) {
635 printk(BIOS_ERR, "TPM_STS_GO failed\n");
636 return 0;
637 }
638
639 /* Now wait for it to report that the response is ready. */
640 expected_status_bits = TPM_STS_VALID | TPM_STS_DATA_AVAIL;
641 if (wait_for_status(expected_status_bits, expected_status_bits) != CB_SUCCESS) {
642 /*
643 * If timed out, which should never happen, let's at least
644 * print out the offending command.
645 */
646 trace_dump("W", TPM_DATA_FIFO_REG, command_size, cmd_body, 1);
647 printk(BIOS_DEBUG, "\n");
648 return 0;
649 }
650
651 /*
652 * The response is ready, let's read it. First we read the FIFO
653 * payload header, to see how much data to expect. The response header
654 * size is fixed to six bytes, the total payload size is stored in
655 * network order in the last four bytes.
656 */
657 tpm2_read_reg(TPM_DATA_FIFO_REG, rsp_body, HEADER_SIZE);
658
659 /* Find out the total payload size, skipping the two byte tag. */
660 payload_size = read_be32(rsp_body + 2);
661
662 if (payload_size > max_response) {
663 /*
664 * TODO(vbendeb): at least drain the FIFO here or somehow let
665 * the TPM know that the response can be dropped.
666 */
667 printk(BIOS_ERR, " TPM response too long (%zd bytes)",
668 payload_size);
669 return 0;
670 }
671
672 /*
673 * Now let's read all but the last byte in the FIFO to make sure the
674 * status register is showing correct flow control bits: 'more data'
675 * until the last byte and then 'no more data' once the last byte is
676 * read.
677 */
678 bytes_to_go = payload_size - 1 - HEADER_SIZE;
679 fifo_buffer.rx_buffer = rsp_body + HEADER_SIZE;
680 if (fifo_transfer(bytes_to_go, fifo_buffer, fifo_receive) != CB_SUCCESS) {
681 printk(BIOS_ERR, "fifo_transfer %zd receive bytes failed\n",
682 bytes_to_go);
683 return 0;
684 }
685
686 /* Verify that there is still data to read. */
687 read_tpm_sts(&status);
688 if ((status & expected_status_bits) != expected_status_bits) {
689 printk(BIOS_ERR, "unexpected intermediate status %#x\n",
690 status);
691 return 0;
692 }
693
694 /* Read the last byte of the PDU. */
695 tpm2_read_reg(TPM_DATA_FIFO_REG, rsp_body + payload_size - 1, 1);
696
697 /* Terminate the dump, if enabled. */
698 if (debug_level_)
699 printk(BIOS_DEBUG, "\n");
700
701 /* Verify that 'data available' is not asserted any more. */
702 read_tpm_sts(&status);
703 if ((status & expected_status_bits) != TPM_STS_VALID) {
704 printk(BIOS_ERR, "unexpected final status %#x\n", status);
705 return 0;
706 }
707
708 /* Move the TPM back to idle state. */
709 if (write_tpm_sts(TPM_STS_COMMAND_READY) != CB_SUCCESS) {
710 printk(BIOS_ERR, "TPM_STS_COMMAND_READY failed\n");
711 return 0;
712 }
713
714 return payload_size;
715 }
716
tis_vendor_write(unsigned int addr,const void * buffer,size_t bytes)717 enum cb_err tis_vendor_write(unsigned int addr, const void *buffer, size_t bytes)
718 {
719 return tpm2_write_reg(addr, buffer, bytes);
720 }
721
tis_vendor_read(unsigned int addr,void * buffer,size_t bytes)722 enum cb_err tis_vendor_read(unsigned int addr, void *buffer, size_t bytes)
723 {
724 return tpm2_read_reg(addr, buffer, bytes);
725 }
726