| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 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 |
|
| 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 |
| 38 |
|
| 39 |
|
| 40 |
static struct spi_slave spi_slave; |
| 41 |
|
| 42 |
|
| 43 |
static struct tpm2_info tpm_info; |
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
static const int debug_level_ = CONFIG(DEBUG_TPM); |
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
typedef struct { |
| 56 |
unsigned char body[4]; |
| 57 |
} spi_frame_header; |
| 58 |
|
| 59 |
void tpm2_get_info(struct tpm2_info *info) |
| 60 |
{ |
| 61 |
*info = tpm_info; |
| 62 |
} |
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 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 |
|
| 81 |
|
| 82 |
|
| 83 |
int wakeup_needed = 1; |
| 84 |
|
| 85 |
|
| 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 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 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 |
|
| 104 |
spi_claim_bus(&spi_slave); |
| 105 |
udelay(1); |
| 106 |
spi_release_bus(&spi_slave); |
| 107 |
udelay(100); |
| 108 |
} |
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
stopwatch_init_msecs_expire(&wake_up_sw, 900); |
| 116 |
} |
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
header.body[0] = (read_write ? 0x80 : 0) | 0x40 | (bytes - 1); |
| 124 |
|
| 125 |
|
| 126 |
for (i = 0; i < 3; i++) |
| 127 |
header.body[i + 1] = (addr >> (8 * (2 - i))) & 0xff; |
| 128 |
|
| 129 |
|
| 130 |
spi_claim_bus(&spi_slave); |
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 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 |
|
| 176 |
|
| 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 |
|
| 193 |
|
| 194 |
|
| 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 |
|
| 214 |
|
| 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 |
|
| 226 |
|
| 227 |
|
| 228 |
printk(BIOS_DEBUG, " %8.8x", *(const uint32_t *)buffer); |
| 229 |
} else { |
| 230 |
int i; |
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 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 |
|
| 250 |
|
| 251 |
|
| 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 |
|
| 259 |
|
| 260 |
|
| 261 |
static void read_bytes(void *buffer, size_t bytes) |
| 262 |
{ |
| 263 |
spi_xfer(&spi_slave, NULL, 0, buffer, bytes); |
| 264 |
} |
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 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 |
|
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
|
| 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 |
|
| 300 |
|
| 301 |
|
| 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 |
|
| 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 |
|
| 314 |
|
| 315 |
|
| 316 |
|
| 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 |
|
| 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 |
|
| 330 |
return access & ~TPM_ACCESS_ESTABLISHMENT; |
| 331 |
} |
| 332 |
|
| 333 |
static void tpm2_write_access_reg(uint8_t cmd) |
| 334 |
{ |
| 335 |
|
| 336 |
assert(!(cmd & (cmd - 1))); |
| 337 |
|
| 338 |
tpm2_write_reg(TPM_ACCESS_REG, &cmd, sizeof(cmd)); |
| 339 |
} |
| 340 |
|
| 341 |
static enum cb_err tpm2_claim_locality(void) |
| 342 |
{ |
| 343 |
uint8_t access; |
| 344 |
struct stopwatch sw; |
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
|
| 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 |
|
| 360 |
|
| 361 |
|
| 362 |
mdelay(2); |
| 363 |
continue; |
| 364 |
} |
| 365 |
|
| 366 |
|
| 367 |
|
| 368 |
|
| 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 |
|
| 391 |
static const uint32_t supported_did_vids[] = { |
| 392 |
0x00281ae0, |
| 393 |
0x504a6666, |
| 394 |
0x50666666, |
| 395 |
0x0000104a |
| 396 |
}; |
| 397 |
|
| 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 |
|
| 407 |
if (CONFIG(TPM_GOOGLE)) |
| 408 |
cr50_plat_irq_status(); |
| 409 |
|
| 410 |
|
| 411 |
|
| 412 |
|
| 413 |
|
| 414 |
|
| 415 |
printk(BIOS_INFO, "Probing TPM: "); |
| 416 |
for (retries = 15; retries > 0; retries--) { |
| 417 |
int i; |
| 418 |
|
| 419 |
|
| 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; |
| 425 |
|
| 426 |
if (i < ARRAY_SIZE(supported_did_vids)) |
| 427 |
break; |
| 428 |
|
| 429 |
|
| 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 |
|
| 443 |
|
| 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 |
|
| 457 |
if (tpm_first_access_this_boot()) |
| 458 |
|
| 459 |
|
| 460 |
|
| 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 |
|
| 477 |
|
| 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 |
|
| 488 |
if (CONFIG(TPM_GOOGLE)) { |
| 489 |
if (tpm_first_access_this_boot()) { |
| 490 |
|
| 491 |
|
| 492 |
cr50_get_firmware_version(NULL); |
| 493 |
cr50_set_board_cfg(); |
| 494 |
} |
| 495 |
} |
| 496 |
return TPM_SUCCESS; |
| 497 |
} |
| 498 |
|
| 499 |
|
| 500 |
|
| 501 |
|
| 502 |
|
| 503 |
#define MAX_STATUS_TIMEOUT 120 |
| 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 |
|
| 529 |
union fifo_transfer_buffer { |
| 530 |
uint8_t *rx_buffer; |
| 531 |
const uint8_t *tx_buffer; |
| 532 |
}; |
| 533 |
|
| 534 |
|
| 535 |
|
| 536 |
|
| 537 |
|
| 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 |
|
| 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 |
|
| 557 |
|
| 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 |
|
| 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 |
|
| 593 |
if (!tpm_info.vendor_id) |
| 594 |
return 0; |
| 595 |
|
| 596 |
|
| 597 |
payload_size = read_be32(cmd_body + 2); |
| 598 |
|
| 599 |
|
| 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 |
|
| 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 |
|
| 617 |
|
| 618 |
|
| 619 |
|
| 620 |
|
| 621 |
|
| 622 |
|
| 623 |
|
| 624 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 644 |
|
| 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 |
|
| 653 |
|
| 654 |
|
| 655 |
|
| 656 |
|
| 657 |
tpm2_read_reg(TPM_DATA_FIFO_REG, rsp_body, HEADER_SIZE); |
| 658 |
|
| 659 |
|
| 660 |
payload_size = read_be32(rsp_body + 2); |
| 661 |
|
| 662 |
if (payload_size > max_response) { |
| 663 |
|
| 664 |
|
| 665 |
|
| 666 |
|
| 667 |
printk(BIOS_ERR, " TPM response too long (%zd bytes)", |
| 668 |
payload_size); |
| 669 |
return 0; |
| 670 |
} |
| 671 |
|
| 672 |
|
| 673 |
|
| 674 |
|
| 675 |
|
| 676 |
|
| 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 |
|
| 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 |
|
| 695 |
tpm2_read_reg(TPM_DATA_FIFO_REG, rsp_body + payload_size - 1, 1); |
| 696 |
|
| 697 |
|
| 698 |
if (debug_level_) |
| 699 |
printk(BIOS_DEBUG, "\n"); |
| 700 |
|
| 701 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
} |