Vadim Bendebury | e31d243 | 2016-04-09 18:33:49 -0700 | [diff] [blame] | 1 | /* |
| 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 Bendebury | 05155c0 | 2016-06-23 12:03:18 -0700 | [diff] [blame] | 27 | #define TPM_LOCALITY_0_SPI_BASE 0x00d40000 |
| 28 | |
Vadim Bendebury | e31d243 | 2016-04-09 18:33:49 -0700 | [diff] [blame] | 29 | /* Assorted TPM2 registers for interface type FIFO. */ |
Vadim Bendebury | 05155c0 | 2016-06-23 12:03:18 -0700 | [diff] [blame] | 30 | #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 Bendebury | 58826fc | 2016-06-23 18:17:33 -0700 | [diff] [blame] | 35 | #define TPM_FW_VER (TPM_LOCALITY_0_SPI_BASE + 0xf90) |
Vadim Bendebury | e31d243 | 2016-04-09 18:33:49 -0700 | [diff] [blame] | 36 | |
| 37 | /* SPI Interface descriptor used by the driver. */ |
| 38 | struct tpm_spi_if { |
| 39 | struct spi_slave *slave; |
| 40 | int (*cs_assert)(struct spi_slave *slave); |
| 41 | void (*cs_deassert)(struct spi_slave *slave); |
| 42 | int (*xfer)(struct spi_slave *slave, const void *dout, |
| 43 | unsigned bytesout, void *din, |
| 44 | unsigned bytesin); |
| 45 | }; |
| 46 | |
| 47 | /* Use the common SPI driver wrapper as the interface callbacks. */ |
| 48 | static 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. */ |
| 55 | struct tpm2_info tpm_info; |
| 56 | |
| 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 | */ |
| 61 | static const int debug_level_ = CONFIG_DEBUG_TPM; |
| 62 | |
| 63 | /* Locality management bits (in TPM_ACCESS_REG) */ |
| 64 | enum 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 | */ |
| 75 | enum 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 | */ |
| 96 | typedef struct { |
| 97 | unsigned char body[4]; |
| 98 | } spi_frame_header; |
| 99 | |
| 100 | void 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. |
| 108 | */ |
| 109 | static void start_transaction(int read_write, size_t bytes, unsigned addr) |
| 110 | { |
| 111 | spi_frame_header header; |
| 112 | uint8_t byte; |
| 113 | int i; |
| 114 | |
| 115 | /* |
| 116 | * Give it 10 ms. TODO(vbendeb): remove this once cr50 SPS TPM driver |
| 117 | * performance is fixed. |
| 118 | */ |
| 119 | mdelay(10); |
| 120 | |
| 121 | /* |
| 122 | * The first byte of the frame header encodes the transaction type |
| 123 | * (read or write) and transfer size (set to lentgh - 1), limited to |
| 124 | * 64 bytes. |
| 125 | */ |
| 126 | header.body[0] = (read_write ? 0x80 : 0) | 0x40 | (bytes - 1); |
| 127 | |
| 128 | /* The rest of the frame header is the TPM register address. */ |
| 129 | for (i = 0; i < 3; i++) |
| 130 | header.body[i + 1] = (addr >> (8 * (2 - i))) & 0xff; |
| 131 | |
| 132 | /* CS assert wakes up the slave. */ |
| 133 | tpm_if.cs_assert(tpm_if.slave); |
| 134 | |
| 135 | /* |
| 136 | * The TCG TPM over SPI specification introduces the notion of SPI |
| 137 | * flow control (Section "6.4.5 Flow Control"). |
| 138 | * |
| 139 | * Again, the slave (TPM device) expects each transaction to start |
| 140 | * with a 4 byte header trasmitted by master. The header indicates if |
| 141 | * the master needs to read or write a register, and the register |
| 142 | * address. |
| 143 | * |
| 144 | * If the slave needs to stall the transaction (for instance it is not |
| 145 | * ready to send the register value to the master), it sets the MOSI |
| 146 | * line to 0 during the last clock of the 4 byte header. In this case |
| 147 | * the master is supposed to start polling the SPI bus, one byte at |
| 148 | * time, until the last bit in the received byte (transferred during |
| 149 | * the last clock of the byte) is set to 1. |
| 150 | * |
| 151 | * Due to some SPI controllers' shortcomings (Rockchip comes to |
| 152 | * mind...) we trasmit the 4 byte header without checking the byte |
| 153 | * transmitted by the TPM during the transaction's last byte. |
| 154 | * |
| 155 | * We know that cr50 is guaranteed to set the flow control bit to 0 |
| 156 | * during the header transfer, but real TPM2 might be fast enough not |
| 157 | * to require to stall the master, this would present an issue. |
| 158 | * crosbug.com/p/52132 has been opened to track this. |
| 159 | */ |
| 160 | tpm_if.xfer(tpm_if.slave, header.body, sizeof(header.body), NULL, 0); |
| 161 | |
| 162 | /* Now poll the bus until TPM removes the stall bit. */ |
| 163 | do { |
| 164 | tpm_if.xfer(tpm_if.slave, NULL, 0, &byte, 1); |
| 165 | } while (!(byte & 1)); |
| 166 | } |
| 167 | |
| 168 | /* |
| 169 | * Print out the contents of a buffer, if debug is enabled. Skip registers |
| 170 | * other than FIFO, unless debug_level_ is 2. |
| 171 | */ |
| 172 | static void trace_dump(const char *prefix, uint32_t reg, |
| 173 | size_t bytes, const uint8_t *buffer, |
| 174 | int force) |
| 175 | { |
| 176 | static char prev_prefix; |
| 177 | static unsigned prev_reg; |
| 178 | static int current_char; |
| 179 | const int BYTES_PER_LINE = 32; |
| 180 | |
| 181 | if (!force) { |
| 182 | if (!debug_level_) |
| 183 | return; |
| 184 | |
| 185 | if ((debug_level_ < 2) && (reg != TPM_DATA_FIFO_REG)) |
| 186 | return; |
| 187 | } |
| 188 | |
| 189 | /* |
| 190 | * Do not print register address again if the last dump print was for |
| 191 | * that register. |
| 192 | */ |
| 193 | if ((prev_prefix != *prefix) || (prev_reg != reg)) { |
| 194 | prev_prefix = *prefix; |
| 195 | prev_reg = reg; |
| 196 | printk(BIOS_DEBUG, "\n%s %2.2x:", prefix, reg); |
| 197 | current_char = 0; |
| 198 | } |
| 199 | |
| 200 | if ((reg != TPM_DATA_FIFO_REG) && (bytes == 4)) { |
| 201 | /* |
| 202 | * This must be a regular register address, print the 32 bit |
| 203 | * value. |
| 204 | */ |
| 205 | printk(BIOS_DEBUG, " %8.8x", *(const uint32_t *)buffer); |
| 206 | } else { |
| 207 | int i; |
| 208 | |
| 209 | /* |
| 210 | * Data read from or written to FIFO or not in 4 byte |
| 211 | * quantiites is printed byte at a time. |
| 212 | */ |
| 213 | for (i = 0; i < bytes; i++) { |
| 214 | if (current_char && !(current_char % BYTES_PER_LINE)) { |
| 215 | printk(BIOS_DEBUG, "\n "); |
| 216 | current_char = 0; |
| 217 | } |
| 218 | current_char++; |
| 219 | printk(BIOS_DEBUG, " %2.2x", buffer[i]); |
| 220 | } |
| 221 | } |
| 222 | } |
| 223 | |
| 224 | /* |
| 225 | * Once transaction is initiated and the TPM indicated that it is ready to go, |
| 226 | * write the actual bytes to the register. |
| 227 | */ |
| 228 | static void write_bytes(const void *buffer, size_t bytes) |
| 229 | { |
| 230 | tpm_if.xfer(tpm_if.slave, buffer, bytes, NULL, 0); |
| 231 | } |
| 232 | |
| 233 | /* |
| 234 | * Once transaction is initiated and the TPM indicated that it is ready to go, |
| 235 | * read the actual bytes from the register. |
| 236 | */ |
| 237 | static void read_bytes(void *buffer, size_t bytes) |
| 238 | { |
| 239 | tpm_if.xfer(tpm_if.slave, NULL, 0, buffer, bytes); |
| 240 | } |
| 241 | |
| 242 | /* |
| 243 | * To write a register, start transaction, transfer data to the TPM, deassert |
| 244 | * CS when done. |
| 245 | * |
| 246 | * Returns one to indicate success, zero (not yet implemented) to indicate |
| 247 | * failure. |
| 248 | */ |
| 249 | static int tpm2_write_reg(unsigned reg_number, const void *buffer, size_t bytes) |
| 250 | { |
| 251 | trace_dump("W", reg_number, bytes, buffer, 0); |
| 252 | start_transaction(false, bytes, reg_number); |
| 253 | write_bytes(buffer, bytes); |
| 254 | tpm_if.cs_deassert(tpm_if.slave); |
| 255 | return 1; |
| 256 | } |
| 257 | |
| 258 | /* |
| 259 | * To read a register, start transaction, transfer data from the TPM, deassert |
| 260 | * CS when done. |
| 261 | * |
| 262 | * Returns one to indicate success, zero (not yet implemented) to indicate |
| 263 | * failure. |
| 264 | */ |
| 265 | static int tpm2_read_reg(unsigned reg_number, void *buffer, size_t bytes) |
| 266 | { |
| 267 | start_transaction(true, bytes, reg_number); |
| 268 | read_bytes(buffer, bytes); |
| 269 | tpm_if.cs_deassert(tpm_if.slave); |
| 270 | trace_dump("R", reg_number, bytes, buffer, 0); |
| 271 | return 1; |
| 272 | } |
| 273 | |
| 274 | /* |
| 275 | * Status register is accessed often, wrap reading and writing it into |
| 276 | * dedicated functions. |
| 277 | */ |
| 278 | static int read_tpm_sts(uint32_t *status) |
| 279 | { |
| 280 | return tpm2_read_reg(TPM_STS_REG, status, sizeof(*status)); |
| 281 | } |
| 282 | |
| 283 | static int write_tpm_sts(uint32_t status) |
| 284 | { |
| 285 | return tpm2_write_reg(TPM_STS_REG, &status, sizeof(status)); |
| 286 | } |
| 287 | |
| 288 | /* |
| 289 | * The TPM may limit the transaction bytes count (burst count) below the 64 |
| 290 | * bytes max. The current value is available as a field of the status |
| 291 | * register. |
| 292 | */ |
| 293 | static uint32_t get_burst_count(void) |
| 294 | { |
| 295 | uint32_t status; |
| 296 | |
| 297 | read_tpm_sts(&status); |
| 298 | return (status >> burst_count_shift) & burst_count_mask; |
| 299 | } |
| 300 | |
| 301 | int tpm2_init(struct spi_slave *spi_if) |
| 302 | { |
| 303 | uint32_t did_vid, status; |
| 304 | uint8_t cmd; |
| 305 | |
| 306 | tpm_if.slave = spi_if; |
| 307 | |
| 308 | tpm2_read_reg(TPM_DID_VID_REG, &did_vid, sizeof(did_vid)); |
| 309 | |
| 310 | /* Try claiming locality zero. */ |
| 311 | tpm2_read_reg(TPM_ACCESS_REG, &cmd, sizeof(cmd)); |
| 312 | if ((cmd & (active_locality & tpm_reg_valid_sts)) == |
| 313 | (active_locality & tpm_reg_valid_sts)) { |
| 314 | /* |
| 315 | * Locality active - maybe reset line is not connected? |
| 316 | * Release the locality and try again |
| 317 | */ |
| 318 | cmd = active_locality; |
| 319 | tpm2_write_reg(TPM_ACCESS_REG, &cmd, sizeof(cmd)); |
| 320 | tpm2_read_reg(TPM_ACCESS_REG, &cmd, sizeof(cmd)); |
| 321 | } |
| 322 | |
| 323 | /* The tpm_establishment bit can be either set or not, ignore it. */ |
| 324 | if ((cmd & ~tpm_establishment) != tpm_reg_valid_sts) { |
| 325 | printk(BIOS_ERR, "invalid reset status: %#x\n", cmd); |
| 326 | return -1; |
| 327 | } |
| 328 | |
| 329 | cmd = request_use; |
| 330 | tpm2_write_reg(TPM_ACCESS_REG, &cmd, sizeof(cmd)); |
| 331 | tpm2_read_reg(TPM_ACCESS_REG, &cmd, sizeof(cmd)); |
| 332 | if ((cmd & ~tpm_establishment) != |
| 333 | (tpm_reg_valid_sts | active_locality)) { |
| 334 | printk(BIOS_ERR, "failed to claim locality 0, status: %#x\n", |
| 335 | cmd); |
| 336 | return -1; |
| 337 | } |
| 338 | |
| 339 | read_tpm_sts(&status); |
| 340 | if (((status >> tpm_family_shift) & tpm_family_mask) != |
| 341 | tpm_family_tpm2) { |
| 342 | printk(BIOS_ERR, "unexpected TPM family value, status: %#x\n", |
| 343 | status); |
| 344 | return -1; |
| 345 | } |
| 346 | |
| 347 | /* |
| 348 | * Locality claimed, read the revision value and set up the tpm_info |
| 349 | * structure. |
| 350 | */ |
| 351 | tpm2_read_reg(TPM_RID_REG, &cmd, sizeof(cmd)); |
| 352 | tpm_info.vendor_id = did_vid & 0xffff; |
| 353 | tpm_info.device_id = did_vid >> 16; |
| 354 | tpm_info.revision = cmd; |
| 355 | |
| 356 | printk(BIOS_INFO, "Connected to device vid:did:rid of %4.4x:%4.4x:%2.2x\n", |
| 357 | tpm_info.vendor_id, tpm_info.device_id, tpm_info.revision); |
| 358 | |
Vadim Bendebury | 58826fc | 2016-06-23 18:17:33 -0700 | [diff] [blame] | 359 | /* Let's report device FW version if available. */ |
| 360 | if (tpm_info.vendor_id == 0x1ae0) { |
| 361 | int chunk_count = 0; |
Vadim Bendebury | 9e561f8 | 2016-07-31 11:19:20 -0700 | [diff] [blame^] | 362 | size_t chunk_size; |
| 363 | /* |
| 364 | * let's read 50 bytes at a time; leave room for the trailing |
| 365 | * zero. |
| 366 | */ |
| 367 | char vstr[51]; |
| 368 | |
| 369 | chunk_size = sizeof(vstr) - 1; |
Vadim Bendebury | 58826fc | 2016-06-23 18:17:33 -0700 | [diff] [blame] | 370 | |
| 371 | printk(BIOS_INFO, "Firmware version: "); |
| 372 | |
| 373 | /* |
| 374 | * Does not really matter what's written, this just makes sure |
| 375 | * the version is reported from the beginning. |
| 376 | */ |
Vadim Bendebury | 9e561f8 | 2016-07-31 11:19:20 -0700 | [diff] [blame^] | 377 | tpm2_write_reg(TPM_FW_VER, &chunk_size, 1); |
Vadim Bendebury | 58826fc | 2016-06-23 18:17:33 -0700 | [diff] [blame] | 378 | |
Vadim Bendebury | 9e561f8 | 2016-07-31 11:19:20 -0700 | [diff] [blame^] | 379 | /* Print it out in sizeof(vstr) - 1 byte chunks. */ |
| 380 | vstr[chunk_size] = 0; |
Vadim Bendebury | 58826fc | 2016-06-23 18:17:33 -0700 | [diff] [blame] | 381 | do { |
Vadim Bendebury | 9e561f8 | 2016-07-31 11:19:20 -0700 | [diff] [blame^] | 382 | tpm2_read_reg(TPM_FW_VER, vstr, chunk_size); |
Vadim Bendebury | 58826fc | 2016-06-23 18:17:33 -0700 | [diff] [blame] | 383 | printk(BIOS_INFO, "%s", vstr); |
| 384 | |
| 385 | /* |
Vadim Bendebury | 9e561f8 | 2016-07-31 11:19:20 -0700 | [diff] [blame^] | 386 | * While string is not over, and is no longer than 300 |
Vadim Bendebury | 58826fc | 2016-06-23 18:17:33 -0700 | [diff] [blame] | 387 | * characters. |
Vadim Bendebury | 58826fc | 2016-06-23 18:17:33 -0700 | [diff] [blame] | 388 | */ |
Vadim Bendebury | 9e561f8 | 2016-07-31 11:19:20 -0700 | [diff] [blame^] | 389 | } while (vstr[chunk_size - 1] && |
| 390 | (chunk_count++ < (300 / chunk_size))); |
Vadim Bendebury | 58826fc | 2016-06-23 18:17:33 -0700 | [diff] [blame] | 391 | |
| 392 | printk(BIOS_INFO, "\n"); |
| 393 | } |
Vadim Bendebury | e31d243 | 2016-04-09 18:33:49 -0700 | [diff] [blame] | 394 | return 0; |
| 395 | } |
| 396 | |
| 397 | /* |
| 398 | * This is in seconds, certain TPM commands, like key generation, can take |
| 399 | * long time to complete. |
| 400 | * |
| 401 | * Returns one to indicate success, zero (not yet implemented) to indicate |
| 402 | * failure. |
| 403 | */ |
| 404 | #define MAX_STATUS_TIMEOUT 120 |
| 405 | static int wait_for_status(uint32_t status_mask, uint32_t status_expected) |
| 406 | { |
| 407 | uint32_t status; |
| 408 | struct stopwatch sw; |
| 409 | |
| 410 | stopwatch_init_usecs_expire(&sw, MAX_STATUS_TIMEOUT * 1000 * 1000); |
| 411 | do { |
| 412 | udelay(1000); |
| 413 | if (stopwatch_expired(&sw)) { |
| 414 | printk(BIOS_ERR, "failed to get expected status %x\n", |
| 415 | status_expected); |
| 416 | return false; |
| 417 | } |
| 418 | read_tpm_sts(&status); |
| 419 | } while ((status & status_mask) != status_expected); |
| 420 | |
| 421 | return 1; |
| 422 | } |
| 423 | |
| 424 | enum fifo_transfer_direction { |
| 425 | fifo_transmit = 0, |
| 426 | fifo_receive = 1 |
| 427 | }; |
| 428 | |
| 429 | /* Union allows to avoid casting away 'const' on transmit buffers. */ |
| 430 | union fifo_transfer_buffer { |
| 431 | uint8_t *rx_buffer; |
| 432 | const uint8_t *tx_buffer; |
| 433 | }; |
| 434 | |
| 435 | /* |
| 436 | * Transfer requested number of bytes to or from TPM FIFO, accounting for the |
| 437 | * current burst count value. |
| 438 | */ |
| 439 | static void fifo_transfer(size_t transfer_size, |
| 440 | union fifo_transfer_buffer buffer, |
| 441 | enum fifo_transfer_direction direction) |
| 442 | { |
| 443 | size_t transaction_size; |
| 444 | size_t burst_count; |
| 445 | size_t handled_so_far = 0; |
| 446 | |
| 447 | do { |
| 448 | do { |
| 449 | /* Could be zero when TPM is busy. */ |
| 450 | burst_count = get_burst_count(); |
| 451 | } while (!burst_count); |
| 452 | |
| 453 | transaction_size = transfer_size - handled_so_far; |
| 454 | transaction_size = MIN(transaction_size, burst_count); |
| 455 | |
| 456 | /* |
| 457 | * The SPI frame header does not allow to pass more than 64 |
| 458 | * bytes. |
| 459 | */ |
| 460 | transaction_size = MIN(transaction_size, 64); |
| 461 | |
| 462 | if (direction == fifo_receive) |
| 463 | tpm2_read_reg(TPM_DATA_FIFO_REG, |
| 464 | buffer.rx_buffer + handled_so_far, |
| 465 | transaction_size); |
| 466 | else |
| 467 | tpm2_write_reg(TPM_DATA_FIFO_REG, |
| 468 | buffer.tx_buffer + handled_so_far, |
| 469 | transaction_size); |
| 470 | |
| 471 | handled_so_far += transaction_size; |
| 472 | |
| 473 | } while (handled_so_far != transfer_size); |
| 474 | } |
| 475 | |
| 476 | size_t tpm2_process_command(const void *tpm2_command, size_t command_size, |
| 477 | void *tpm2_response, size_t max_response) |
| 478 | { |
| 479 | uint32_t status; |
| 480 | uint32_t expected_status_bits; |
| 481 | size_t payload_size; |
| 482 | size_t bytes_to_go; |
| 483 | const uint8_t *cmd_body = tpm2_command; |
| 484 | uint8_t *rsp_body = tpm2_response; |
| 485 | union fifo_transfer_buffer fifo_buffer; |
| 486 | const int HEADER_SIZE = 6; |
| 487 | |
| 488 | /* Skip the two byte tag, read the size field. */ |
| 489 | payload_size = read_be32(cmd_body + 2); |
| 490 | |
| 491 | /* Sanity check. */ |
| 492 | if (payload_size != command_size) { |
| 493 | printk(BIOS_ERR, |
| 494 | "Command size mismatch: encoded %zd != requested %zd\n", |
| 495 | payload_size, command_size); |
| 496 | trace_dump("W", TPM_DATA_FIFO_REG, command_size, cmd_body, 1); |
| 497 | printk(BIOS_DEBUG, "\n"); |
| 498 | return 0; |
| 499 | } |
| 500 | |
| 501 | /* Let the TPM know that the command is coming. */ |
| 502 | write_tpm_sts(command_ready); |
| 503 | |
| 504 | /* |
| 505 | * Tpm commands and responses written to and read from the FIFO |
| 506 | * register (0x24) are datagrams of variable size, prepended by a 6 |
| 507 | * byte header. |
| 508 | * |
| 509 | * The specification description of the state machine is a bit vague, |
| 510 | * but from experience it looks like there is no need to wait for the |
| 511 | * sts.expect bit to be set, at least with the 9670 and cr50 devices. |
| 512 | * Just write the command into FIFO, making sure not to exceed the |
| 513 | * burst count or the maximum PDU size, whatever is smaller. |
| 514 | */ |
| 515 | fifo_buffer.tx_buffer = cmd_body; |
| 516 | fifo_transfer(command_size, fifo_buffer, fifo_transmit); |
| 517 | |
| 518 | /* Now tell the TPM it can start processing the command. */ |
| 519 | write_tpm_sts(tpm_go); |
| 520 | |
| 521 | /* Now wait for it to report that the response is ready. */ |
| 522 | expected_status_bits = sts_valid | data_avail; |
| 523 | if (!wait_for_status(expected_status_bits, expected_status_bits)) { |
| 524 | /* |
| 525 | * If timed out, which should never happen, let's at least |
| 526 | * print out the offending command. |
| 527 | */ |
| 528 | trace_dump("W", TPM_DATA_FIFO_REG, command_size, cmd_body, 1); |
| 529 | printk(BIOS_DEBUG, "\n"); |
| 530 | return 0; |
| 531 | } |
| 532 | |
| 533 | /* |
| 534 | * The response is ready, let's read it. First we read the FIFO |
| 535 | * payload header, to see how much data to expect. The response header |
| 536 | * size is fixed to six bytes, the total payload size is stored in |
| 537 | * network order in the last four bytes. |
| 538 | */ |
| 539 | tpm2_read_reg(TPM_DATA_FIFO_REG, rsp_body, HEADER_SIZE); |
| 540 | |
| 541 | /* Find out the total payload size, skipping the two byte tag. */ |
| 542 | payload_size = read_be32(rsp_body + 2); |
| 543 | |
| 544 | if (payload_size > max_response) { |
| 545 | /* |
| 546 | * TODO(vbendeb): at least drain the FIFO here or somehow let |
| 547 | * the TPM know that the response can be dropped. |
| 548 | */ |
| 549 | printk(BIOS_ERR, " tpm response too long (%zd bytes)", |
| 550 | payload_size); |
| 551 | return 0; |
| 552 | } |
| 553 | |
| 554 | /* |
| 555 | * Now let's read all but the last byte in the FIFO to make sure the |
| 556 | * status register is showing correct flow control bits: 'more data' |
| 557 | * until the last byte and then 'no more data' once the last byte is |
| 558 | * read. |
| 559 | */ |
| 560 | bytes_to_go = payload_size - 1 - HEADER_SIZE; |
| 561 | fifo_buffer.rx_buffer = rsp_body + HEADER_SIZE; |
| 562 | fifo_transfer(bytes_to_go, fifo_buffer, fifo_receive); |
| 563 | |
| 564 | /* Verify that there is still data to read. */ |
| 565 | read_tpm_sts(&status); |
| 566 | if ((status & expected_status_bits) != expected_status_bits) { |
| 567 | printk(BIOS_ERR, "unexpected intermediate status %#x\n", |
| 568 | status); |
| 569 | return 0; |
| 570 | } |
| 571 | |
| 572 | /* Read the last byte of the PDU. */ |
| 573 | tpm2_read_reg(TPM_DATA_FIFO_REG, rsp_body + payload_size - 1, 1); |
| 574 | |
| 575 | /* Terminate the dump, if enabled. */ |
| 576 | if (debug_level_) |
| 577 | printk(BIOS_DEBUG, "\n"); |
| 578 | |
| 579 | /* Verify that 'data available' is not asseretd any more. */ |
| 580 | read_tpm_sts(&status); |
| 581 | if ((status & expected_status_bits) != sts_valid) { |
| 582 | printk(BIOS_ERR, "unexpected final status %#x\n", status); |
| 583 | return 0; |
| 584 | } |
| 585 | |
| 586 | /* Move the TPM back to idle state. */ |
| 587 | write_tpm_sts(command_ready); |
| 588 | |
| 589 | return payload_size; |
| 590 | } |