blob: 29b85c4716a23a26ed650540fb3a7d015d10e210 [file] [log] [blame]
Vadim Bendeburye31d2432016-04-09 18:33:49 -07001/*
2 * Copyright 2016 The Chromium OS Authors. All rights reserved.
3 * Use of this source code is governed by a BSD-style license that can be
4 * found in the LICENSE file.
5 *
6 * This is a driver for a SPI interfaced TPM2 device.
7 *
8 * It assumes that the required SPI interface has been initialized before the
9 * driver is started. A 'sruct spi_slave' pointer passed at initialization is
10 * used to direct traffic to the correct SPI interface. This dirver does not
11 * provide a way to instantiate multiple TPM devices. Also, to keep things
12 * simple, the driver unconditionally uses of TPM locality zero.
13 *
14 * References to documentation are based on the TCG issued "TPM Profile (PTP)
15 * Specification Revision 00.43".
16 */
17
18#include <commonlib/endian.h>
19#include <console/console.h>
20#include <delay.h>
21#include <endian.h>
22#include <string.h>
23#include <timer.h>
24
25#include "tpm.h"
26
Vadim Bendebury05155c02016-06-23 12:03:18 -070027#define TPM_LOCALITY_0_SPI_BASE 0x00d40000
28
Vadim Bendeburye31d2432016-04-09 18:33:49 -070029/* Assorted TPM2 registers for interface type FIFO. */
Vadim Bendebury05155c02016-06-23 12:03:18 -070030#define TPM_ACCESS_REG (TPM_LOCALITY_0_SPI_BASE + 0)
31#define TPM_STS_REG (TPM_LOCALITY_0_SPI_BASE + 0x18)
32#define TPM_DATA_FIFO_REG (TPM_LOCALITY_0_SPI_BASE + 0x24)
33#define TPM_DID_VID_REG (TPM_LOCALITY_0_SPI_BASE + 0xf00)
34#define TPM_RID_REG (TPM_LOCALITY_0_SPI_BASE + 0xf04)
Vadim Bendebury58826fc2016-06-23 18:17:33 -070035#define TPM_FW_VER (TPM_LOCALITY_0_SPI_BASE + 0xf90)
Vadim Bendeburye31d2432016-04-09 18:33:49 -070036
37/* SPI Interface descriptor used by the driver. */
38struct tpm_spi_if {
Furquan Shaikh36b81af2016-12-01 01:02:44 -080039 struct spi_slave slave;
Furquan Shaikh0dba0252016-11-30 04:34:22 -080040 int (*cs_assert)(const struct spi_slave *slave);
41 void (*cs_deassert)(const struct spi_slave *slave);
42 int (*xfer)(const struct spi_slave *slave, const void *dout,
43 size_t bytesout, void *din,
44 size_t bytesin);
Vadim Bendeburye31d2432016-04-09 18:33:49 -070045};
46
47/* Use the common SPI driver wrapper as the interface callbacks. */
48static struct tpm_spi_if tpm_if = {
49 .cs_assert = spi_claim_bus,
50 .cs_deassert = spi_release_bus,
51 .xfer = spi_xfer
52};
53
54/* Cached TPM device identification. */
Aaron Durbin445c13f2017-03-27 17:19:18 -050055static struct tpm2_info tpm_info;
Vadim Bendeburye31d2432016-04-09 18:33:49 -070056
57/*
58 * TODO(vbendeb): make CONFIG_DEBUG_TPM an int to allow different level of
59 * debug traces. Right now it is either 0 or 1.
60 */
61static const int debug_level_ = CONFIG_DEBUG_TPM;
62
63/* Locality management bits (in TPM_ACCESS_REG) */
64enum tpm_access_bits {
65 tpm_reg_valid_sts = (1 << 7),
66 active_locality = (1 << 5),
67 request_use = (1 << 1),
68 tpm_establishment = (1 << 0),
69};
70
71/*
72 * Variuous fields of the TPM status register, arguably the most important
73 * register when interfacing to a TPM.
74 */
75enum tpm_sts_bits {
76 tpm_family_shift = 26,
77 tpm_family_mask = ((1 << 2) - 1), /* 2 bits wide. */
78 tpm_family_tpm2 = 1,
79 reset_establishment_bit = (1 << 25),
80 command_cancel = (1 << 24),
81 burst_count_shift = 8,
82 burst_count_mask = ((1 << 16) - 1), /* 16 bits wide. */
83 sts_valid = (1 << 7),
84 command_ready = (1 << 6),
85 tpm_go = (1 << 5),
86 data_avail = (1 << 4),
87 expect = (1 << 3),
88 self_test_done = (1 << 2),
89 response_retry = (1 << 1),
90};
91
92/*
93 * SPI frame header for TPM transactions is 4 bytes in size, it is described
94 * in section "6.4.6 Spi Bit Protocol".
95 */
96typedef struct {
97 unsigned char body[4];
98} spi_frame_header;
99
100void tpm2_get_info(struct tpm2_info *info)
101{
102 *info = tpm_info;
103}
104
105/*
106 * Each TPM2 SPI transaction starts the same: CS is asserted, the 4 byte
107 * header is sent to the TPM, the master waits til TPM is ready to continue.
Vadim Bendebury731ef9b2016-12-15 21:49:23 -0800108 *
109 * Returns 1 on success, 0 on failure (TPM SPI flow control timeout.)
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700110 */
Vadim Bendebury731ef9b2016-12-15 21:49:23 -0800111static int start_transaction(int read_write, size_t bytes, unsigned addr)
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700112{
113 spi_frame_header header;
114 uint8_t byte;
115 int i;
Vadim Bendebury731ef9b2016-12-15 21:49:23 -0800116 struct stopwatch sw;
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700117
118 /*
119 * Give it 10 ms. TODO(vbendeb): remove this once cr50 SPS TPM driver
120 * performance is fixed.
121 */
122 mdelay(10);
123
Jeffy Chenf9a40ea2017-03-03 18:24:02 +0800124 /* Try to wake cr50 if it is asleep. */
125 tpm_if.cs_assert(&tpm_if.slave);
126 udelay(1);
127 tpm_if.cs_deassert(&tpm_if.slave);
128 udelay(100);
129
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700130 /*
131 * The first byte of the frame header encodes the transaction type
132 * (read or write) and transfer size (set to lentgh - 1), limited to
133 * 64 bytes.
134 */
135 header.body[0] = (read_write ? 0x80 : 0) | 0x40 | (bytes - 1);
136
137 /* The rest of the frame header is the TPM register address. */
138 for (i = 0; i < 3; i++)
139 header.body[i + 1] = (addr >> (8 * (2 - i))) & 0xff;
140
141 /* CS assert wakes up the slave. */
Furquan Shaikh36b81af2016-12-01 01:02:44 -0800142 tpm_if.cs_assert(&tpm_if.slave);
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700143
144 /*
145 * The TCG TPM over SPI specification introduces the notion of SPI
146 * flow control (Section "6.4.5 Flow Control").
147 *
148 * Again, the slave (TPM device) expects each transaction to start
149 * with a 4 byte header trasmitted by master. The header indicates if
150 * the master needs to read or write a register, and the register
151 * address.
152 *
153 * If the slave needs to stall the transaction (for instance it is not
154 * ready to send the register value to the master), it sets the MOSI
155 * line to 0 during the last clock of the 4 byte header. In this case
156 * the master is supposed to start polling the SPI bus, one byte at
157 * time, until the last bit in the received byte (transferred during
158 * the last clock of the byte) is set to 1.
159 *
160 * Due to some SPI controllers' shortcomings (Rockchip comes to
161 * mind...) we trasmit the 4 byte header without checking the byte
162 * transmitted by the TPM during the transaction's last byte.
163 *
164 * We know that cr50 is guaranteed to set the flow control bit to 0
165 * during the header transfer, but real TPM2 might be fast enough not
166 * to require to stall the master, this would present an issue.
167 * crosbug.com/p/52132 has been opened to track this.
168 */
Furquan Shaikh36b81af2016-12-01 01:02:44 -0800169 tpm_if.xfer(&tpm_if.slave, header.body, sizeof(header.body), NULL, 0);
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700170
Vadim Bendebury731ef9b2016-12-15 21:49:23 -0800171 /*
172 * Now poll the bus until TPM removes the stall bit. Give it up to 100
173 * ms to sort it out - it could be saving stuff in nvram at some
174 * point.
175 */
176 stopwatch_init_msecs_expire(&sw, 100);
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700177 do {
Vadim Bendebury731ef9b2016-12-15 21:49:23 -0800178 if (stopwatch_expired(&sw)) {
179 printk(BIOS_ERR, "TPM flow control failure\n");
Aaron Durbin5cf1fad2017-03-27 17:25:06 -0500180 tpm_if.cs_deassert(&tpm_if.slave);
Vadim Bendebury731ef9b2016-12-15 21:49:23 -0800181 return 0;
182 }
Furquan Shaikh36b81af2016-12-01 01:02:44 -0800183 tpm_if.xfer(&tpm_if.slave, NULL, 0, &byte, 1);
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700184 } while (!(byte & 1));
Vadim Bendebury731ef9b2016-12-15 21:49:23 -0800185 return 1;
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700186}
187
188/*
189 * Print out the contents of a buffer, if debug is enabled. Skip registers
190 * other than FIFO, unless debug_level_ is 2.
191 */
192static void trace_dump(const char *prefix, uint32_t reg,
193 size_t bytes, const uint8_t *buffer,
194 int force)
195{
196 static char prev_prefix;
197 static unsigned prev_reg;
198 static int current_char;
199 const int BYTES_PER_LINE = 32;
200
201 if (!force) {
202 if (!debug_level_)
203 return;
204
205 if ((debug_level_ < 2) && (reg != TPM_DATA_FIFO_REG))
206 return;
207 }
208
209 /*
210 * Do not print register address again if the last dump print was for
211 * that register.
212 */
213 if ((prev_prefix != *prefix) || (prev_reg != reg)) {
214 prev_prefix = *prefix;
215 prev_reg = reg;
216 printk(BIOS_DEBUG, "\n%s %2.2x:", prefix, reg);
217 current_char = 0;
218 }
219
220 if ((reg != TPM_DATA_FIFO_REG) && (bytes == 4)) {
221 /*
222 * This must be a regular register address, print the 32 bit
223 * value.
224 */
225 printk(BIOS_DEBUG, " %8.8x", *(const uint32_t *)buffer);
226 } else {
227 int i;
228
229 /*
230 * Data read from or written to FIFO or not in 4 byte
231 * quantiites is printed byte at a time.
232 */
233 for (i = 0; i < bytes; i++) {
234 if (current_char && !(current_char % BYTES_PER_LINE)) {
235 printk(BIOS_DEBUG, "\n ");
236 current_char = 0;
237 }
238 current_char++;
239 printk(BIOS_DEBUG, " %2.2x", buffer[i]);
240 }
241 }
242}
243
244/*
245 * Once transaction is initiated and the TPM indicated that it is ready to go,
246 * write the actual bytes to the register.
247 */
248static void write_bytes(const void *buffer, size_t bytes)
249{
Furquan Shaikh36b81af2016-12-01 01:02:44 -0800250 tpm_if.xfer(&tpm_if.slave, buffer, bytes, NULL, 0);
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700251}
252
253/*
254 * Once transaction is initiated and the TPM indicated that it is ready to go,
255 * read the actual bytes from the register.
256 */
257static void read_bytes(void *buffer, size_t bytes)
258{
Furquan Shaikh36b81af2016-12-01 01:02:44 -0800259 tpm_if.xfer(&tpm_if.slave, NULL, 0, buffer, bytes);
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700260}
261
262/*
263 * To write a register, start transaction, transfer data to the TPM, deassert
264 * CS when done.
265 *
Vadim Bendebury731ef9b2016-12-15 21:49:23 -0800266 * Returns one to indicate success, zero to indicate failure.
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700267 */
268static int tpm2_write_reg(unsigned reg_number, const void *buffer, size_t bytes)
269{
270 trace_dump("W", reg_number, bytes, buffer, 0);
Vadim Bendebury731ef9b2016-12-15 21:49:23 -0800271 if (!start_transaction(false, bytes, reg_number))
272 return 0;
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700273 write_bytes(buffer, bytes);
Furquan Shaikh36b81af2016-12-01 01:02:44 -0800274 tpm_if.cs_deassert(&tpm_if.slave);
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700275 return 1;
276}
277
278/*
279 * To read a register, start transaction, transfer data from the TPM, deassert
280 * CS when done.
281 *
Vadim Bendebury731ef9b2016-12-15 21:49:23 -0800282 * Returns one to indicate success, zero to indicate failure.
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700283 */
284static int tpm2_read_reg(unsigned reg_number, void *buffer, size_t bytes)
285{
Vadim Bendebury731ef9b2016-12-15 21:49:23 -0800286 if (!start_transaction(true, bytes, reg_number)) {
287 memset(buffer, 0, bytes);
288 return 0;
289 }
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700290 read_bytes(buffer, bytes);
Furquan Shaikh36b81af2016-12-01 01:02:44 -0800291 tpm_if.cs_deassert(&tpm_if.slave);
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700292 trace_dump("R", reg_number, bytes, buffer, 0);
293 return 1;
294}
295
296/*
297 * Status register is accessed often, wrap reading and writing it into
298 * dedicated functions.
299 */
300static int read_tpm_sts(uint32_t *status)
301{
302 return tpm2_read_reg(TPM_STS_REG, status, sizeof(*status));
303}
304
305static int write_tpm_sts(uint32_t status)
306{
307 return tpm2_write_reg(TPM_STS_REG, &status, sizeof(status));
308}
309
310/*
311 * The TPM may limit the transaction bytes count (burst count) below the 64
312 * bytes max. The current value is available as a field of the status
313 * register.
314 */
315static uint32_t get_burst_count(void)
316{
317 uint32_t status;
318
319 read_tpm_sts(&status);
320 return (status >> burst_count_shift) & burst_count_mask;
321}
322
323int tpm2_init(struct spi_slave *spi_if)
324{
325 uint32_t did_vid, status;
326 uint8_t cmd;
327
Furquan Shaikh36b81af2016-12-01 01:02:44 -0800328 memcpy(&tpm_if.slave, spi_if, sizeof(*spi_if));
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700329
Vadim Bendebury731ef9b2016-12-15 21:49:23 -0800330 /*
331 * It is enough to check the first register read error status to bail
332 * out in case of malfunctioning TPM.
333 */
334 if (!tpm2_read_reg(TPM_DID_VID_REG, &did_vid, sizeof(did_vid)))
335 return -1;
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700336
337 /* Try claiming locality zero. */
338 tpm2_read_reg(TPM_ACCESS_REG, &cmd, sizeof(cmd));
339 if ((cmd & (active_locality & tpm_reg_valid_sts)) ==
340 (active_locality & tpm_reg_valid_sts)) {
341 /*
342 * Locality active - maybe reset line is not connected?
343 * Release the locality and try again
344 */
345 cmd = active_locality;
346 tpm2_write_reg(TPM_ACCESS_REG, &cmd, sizeof(cmd));
347 tpm2_read_reg(TPM_ACCESS_REG, &cmd, sizeof(cmd));
348 }
349
350 /* The tpm_establishment bit can be either set or not, ignore it. */
351 if ((cmd & ~tpm_establishment) != tpm_reg_valid_sts) {
352 printk(BIOS_ERR, "invalid reset status: %#x\n", cmd);
353 return -1;
354 }
355
356 cmd = request_use;
357 tpm2_write_reg(TPM_ACCESS_REG, &cmd, sizeof(cmd));
358 tpm2_read_reg(TPM_ACCESS_REG, &cmd, sizeof(cmd));
359 if ((cmd & ~tpm_establishment) !=
360 (tpm_reg_valid_sts | active_locality)) {
361 printk(BIOS_ERR, "failed to claim locality 0, status: %#x\n",
362 cmd);
363 return -1;
364 }
365
366 read_tpm_sts(&status);
367 if (((status >> tpm_family_shift) & tpm_family_mask) !=
368 tpm_family_tpm2) {
369 printk(BIOS_ERR, "unexpected TPM family value, status: %#x\n",
370 status);
371 return -1;
372 }
373
374 /*
375 * Locality claimed, read the revision value and set up the tpm_info
376 * structure.
377 */
378 tpm2_read_reg(TPM_RID_REG, &cmd, sizeof(cmd));
379 tpm_info.vendor_id = did_vid & 0xffff;
380 tpm_info.device_id = did_vid >> 16;
381 tpm_info.revision = cmd;
382
383 printk(BIOS_INFO, "Connected to device vid:did:rid of %4.4x:%4.4x:%2.2x\n",
384 tpm_info.vendor_id, tpm_info.device_id, tpm_info.revision);
385
Vadim Bendebury58826fc2016-06-23 18:17:33 -0700386 /* Let's report device FW version if available. */
387 if (tpm_info.vendor_id == 0x1ae0) {
388 int chunk_count = 0;
Vadim Bendebury9e561f82016-07-31 11:19:20 -0700389 size_t chunk_size;
390 /*
391 * let's read 50 bytes at a time; leave room for the trailing
392 * zero.
393 */
394 char vstr[51];
395
396 chunk_size = sizeof(vstr) - 1;
Vadim Bendebury58826fc2016-06-23 18:17:33 -0700397
398 printk(BIOS_INFO, "Firmware version: ");
399
400 /*
401 * Does not really matter what's written, this just makes sure
402 * the version is reported from the beginning.
403 */
Vadim Bendebury9e561f82016-07-31 11:19:20 -0700404 tpm2_write_reg(TPM_FW_VER, &chunk_size, 1);
Vadim Bendebury58826fc2016-06-23 18:17:33 -0700405
Vadim Bendebury9e561f82016-07-31 11:19:20 -0700406 /* Print it out in sizeof(vstr) - 1 byte chunks. */
407 vstr[chunk_size] = 0;
Vadim Bendebury58826fc2016-06-23 18:17:33 -0700408 do {
Vadim Bendebury9e561f82016-07-31 11:19:20 -0700409 tpm2_read_reg(TPM_FW_VER, vstr, chunk_size);
Vadim Bendebury58826fc2016-06-23 18:17:33 -0700410 printk(BIOS_INFO, "%s", vstr);
411
412 /*
Vadim Bendebury9e561f82016-07-31 11:19:20 -0700413 * While string is not over, and is no longer than 300
Vadim Bendebury58826fc2016-06-23 18:17:33 -0700414 * characters.
Vadim Bendebury58826fc2016-06-23 18:17:33 -0700415 */
Vadim Bendebury9e561f82016-07-31 11:19:20 -0700416 } while (vstr[chunk_size - 1] &&
417 (chunk_count++ < (300 / chunk_size)));
Vadim Bendebury58826fc2016-06-23 18:17:33 -0700418
419 printk(BIOS_INFO, "\n");
420 }
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700421 return 0;
422}
423
424/*
425 * This is in seconds, certain TPM commands, like key generation, can take
426 * long time to complete.
427 *
428 * Returns one to indicate success, zero (not yet implemented) to indicate
429 * failure.
430 */
431#define MAX_STATUS_TIMEOUT 120
432static int wait_for_status(uint32_t status_mask, uint32_t status_expected)
433{
434 uint32_t status;
435 struct stopwatch sw;
436
437 stopwatch_init_usecs_expire(&sw, MAX_STATUS_TIMEOUT * 1000 * 1000);
438 do {
439 udelay(1000);
440 if (stopwatch_expired(&sw)) {
441 printk(BIOS_ERR, "failed to get expected status %x\n",
442 status_expected);
443 return false;
444 }
445 read_tpm_sts(&status);
446 } while ((status & status_mask) != status_expected);
447
448 return 1;
449}
450
451enum fifo_transfer_direction {
452 fifo_transmit = 0,
453 fifo_receive = 1
454};
455
456/* Union allows to avoid casting away 'const' on transmit buffers. */
457union fifo_transfer_buffer {
458 uint8_t *rx_buffer;
459 const uint8_t *tx_buffer;
460};
461
462/*
463 * Transfer requested number of bytes to or from TPM FIFO, accounting for the
464 * current burst count value.
465 */
466static void fifo_transfer(size_t transfer_size,
467 union fifo_transfer_buffer buffer,
468 enum fifo_transfer_direction direction)
469{
470 size_t transaction_size;
471 size_t burst_count;
472 size_t handled_so_far = 0;
473
474 do {
475 do {
476 /* Could be zero when TPM is busy. */
477 burst_count = get_burst_count();
478 } while (!burst_count);
479
480 transaction_size = transfer_size - handled_so_far;
481 transaction_size = MIN(transaction_size, burst_count);
482
483 /*
484 * The SPI frame header does not allow to pass more than 64
485 * bytes.
486 */
487 transaction_size = MIN(transaction_size, 64);
488
489 if (direction == fifo_receive)
490 tpm2_read_reg(TPM_DATA_FIFO_REG,
491 buffer.rx_buffer + handled_so_far,
492 transaction_size);
493 else
494 tpm2_write_reg(TPM_DATA_FIFO_REG,
495 buffer.tx_buffer + handled_so_far,
496 transaction_size);
497
498 handled_so_far += transaction_size;
499
500 } while (handled_so_far != transfer_size);
501}
502
503size_t tpm2_process_command(const void *tpm2_command, size_t command_size,
504 void *tpm2_response, size_t max_response)
505{
506 uint32_t status;
507 uint32_t expected_status_bits;
508 size_t payload_size;
509 size_t bytes_to_go;
510 const uint8_t *cmd_body = tpm2_command;
511 uint8_t *rsp_body = tpm2_response;
512 union fifo_transfer_buffer fifo_buffer;
513 const int HEADER_SIZE = 6;
514
Vadim Bendebury731ef9b2016-12-15 21:49:23 -0800515 /* Do not try using an uninitialized TPM. */
516 if (!tpm_info.vendor_id)
517 return 0;
518
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700519 /* Skip the two byte tag, read the size field. */
520 payload_size = read_be32(cmd_body + 2);
521
522 /* Sanity check. */
523 if (payload_size != command_size) {
524 printk(BIOS_ERR,
525 "Command size mismatch: encoded %zd != requested %zd\n",
526 payload_size, command_size);
527 trace_dump("W", TPM_DATA_FIFO_REG, command_size, cmd_body, 1);
528 printk(BIOS_DEBUG, "\n");
529 return 0;
530 }
531
532 /* Let the TPM know that the command is coming. */
533 write_tpm_sts(command_ready);
534
535 /*
536 * Tpm commands and responses written to and read from the FIFO
537 * register (0x24) are datagrams of variable size, prepended by a 6
538 * byte header.
539 *
540 * The specification description of the state machine is a bit vague,
541 * but from experience it looks like there is no need to wait for the
542 * sts.expect bit to be set, at least with the 9670 and cr50 devices.
543 * Just write the command into FIFO, making sure not to exceed the
544 * burst count or the maximum PDU size, whatever is smaller.
545 */
546 fifo_buffer.tx_buffer = cmd_body;
547 fifo_transfer(command_size, fifo_buffer, fifo_transmit);
548
549 /* Now tell the TPM it can start processing the command. */
550 write_tpm_sts(tpm_go);
551
552 /* Now wait for it to report that the response is ready. */
553 expected_status_bits = sts_valid | data_avail;
554 if (!wait_for_status(expected_status_bits, expected_status_bits)) {
555 /*
556 * If timed out, which should never happen, let's at least
557 * print out the offending command.
558 */
559 trace_dump("W", TPM_DATA_FIFO_REG, command_size, cmd_body, 1);
560 printk(BIOS_DEBUG, "\n");
561 return 0;
562 }
563
564 /*
565 * The response is ready, let's read it. First we read the FIFO
566 * payload header, to see how much data to expect. The response header
567 * size is fixed to six bytes, the total payload size is stored in
568 * network order in the last four bytes.
569 */
570 tpm2_read_reg(TPM_DATA_FIFO_REG, rsp_body, HEADER_SIZE);
571
572 /* Find out the total payload size, skipping the two byte tag. */
573 payload_size = read_be32(rsp_body + 2);
574
575 if (payload_size > max_response) {
576 /*
577 * TODO(vbendeb): at least drain the FIFO here or somehow let
578 * the TPM know that the response can be dropped.
579 */
580 printk(BIOS_ERR, " tpm response too long (%zd bytes)",
581 payload_size);
582 return 0;
583 }
584
585 /*
586 * Now let's read all but the last byte in the FIFO to make sure the
587 * status register is showing correct flow control bits: 'more data'
588 * until the last byte and then 'no more data' once the last byte is
589 * read.
590 */
591 bytes_to_go = payload_size - 1 - HEADER_SIZE;
592 fifo_buffer.rx_buffer = rsp_body + HEADER_SIZE;
593 fifo_transfer(bytes_to_go, fifo_buffer, fifo_receive);
594
595 /* Verify that there is still data to read. */
596 read_tpm_sts(&status);
597 if ((status & expected_status_bits) != expected_status_bits) {
598 printk(BIOS_ERR, "unexpected intermediate status %#x\n",
599 status);
600 return 0;
601 }
602
603 /* Read the last byte of the PDU. */
604 tpm2_read_reg(TPM_DATA_FIFO_REG, rsp_body + payload_size - 1, 1);
605
606 /* Terminate the dump, if enabled. */
607 if (debug_level_)
608 printk(BIOS_DEBUG, "\n");
609
610 /* Verify that 'data available' is not asseretd any more. */
611 read_tpm_sts(&status);
612 if ((status & expected_status_bits) != sts_valid) {
613 printk(BIOS_ERR, "unexpected final status %#x\n", status);
614 return 0;
615 }
616
617 /* Move the TPM back to idle state. */
618 write_tpm_sts(command_ready);
619
620 return payload_size;
621}