blob: 5e29e3a14d63b54c8f65829656193e049e981e02 [file] [log] [blame]
Stefan Reinauer3008bbad2011-10-11 14:46:25 -07001/*
2 * This file is part of the coreboot project.
3 *
4 * Copyright (C) 2011 The ChromiumOS Authors. All rights reserved.
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; version 2 of the License.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
Patrick Georgib890a122015-03-26 15:17:45 +010017 * Foundation, Inc.
Stefan Reinauer3008bbad2011-10-11 14:46:25 -070018 */
19
20#include <types.h>
Patrick Georgi3e18aca2015-04-29 18:59:04 +020021#include <console/cbmem_console.h>
Stefan Reinauer3008bbad2011-10-11 14:46:25 -070022#include <console/console.h>
23#include <arch/acpi.h>
Stefan Reinauerd518c7a2013-11-04 17:38:32 -080024#include <tpm.h>
Stefan Reinauer9aea04a2012-03-30 12:01:06 -070025#include <reset.h>
Stefan Reinauer3008bbad2011-10-11 14:46:25 -070026
27//#define EXTRA_LOGGING
28
29#define TPM_LARGE_ENOUGH_COMMAND_SIZE 256 /* saves space in the firmware */
30
31#define TPM_SUCCESS ((u32)0x00000000)
32
33#define TPM_E_IOERROR ((u32)0x0000001f)
34#define TPM_E_COMMUNICATION_ERROR ((u32)0x00005004)
35#define TPM_E_NON_FATAL ((u32)0x00000800)
36#define TPM_E_INVALID_POSTINIT ((u32)0x00000026)
37
38#define TPM_E_NEEDS_SELFTEST ((u32)(TPM_E_NON_FATAL + 1))
39#define TPM_E_DOING_SELFTEST ((u32)(TPM_E_NON_FATAL + 2))
40
41static const struct {
42 u8 buffer[12];
43} tpm_resume_cmd = {
44 { 0x0, 0xc1, 0x0, 0x0, 0x0, 0xc, 0x0, 0x0, 0x0, 0x99, 0x0, 0x2 }
45};
46
47static const struct {
Stefan Reinauer9aea04a2012-03-30 12:01:06 -070048 u8 buffer[12];
49} tpm_startup_cmd = {
50 {0x0, 0xc1, 0x0, 0x0, 0x0, 0xc, 0x0, 0x0, 0x0, 0x99, 0x0, 0x1 }
51};
52
53static const struct {
Stefan Reinauer3008bbad2011-10-11 14:46:25 -070054 u8 buffer[10];
55} tpm_continueselftest_cmd = {
56 { 0x0, 0xc1, 0x0, 0x0, 0x0, 0xa, 0x0, 0x0, 0x0, 0x53 }
57};
58
59static inline void FromTpmUint32(const u8 * buffer, u32 * x)
60{
61 *x = ((buffer[0] << 24) |
62 (buffer[1] << 16) | (buffer[2] << 8) | buffer[3]);
63}
64
65static inline int TpmCommandSize(const u8 * buffer)
66{
67 u32 size;
68 FromTpmUint32(buffer + sizeof(u16), &size);
69 return (int)size;
70}
71
72/* Gets the code field of a TPM command. */
73static inline int TpmCommandCode(const u8 * buffer)
74{
75 u32 code;
76 FromTpmUint32(buffer + sizeof(u16) + sizeof(u32), &code);
77 return code;
78}
79
80/* Gets the return code field of a TPM result. */
81static inline int TpmReturnCode(const u8 * buffer)
82{
83 return TpmCommandCode(buffer);
84}
85
86/* Like TlclSendReceive below, but do not retry if NEEDS_SELFTEST or
87 * DOING_SELFTEST errors are returned.
88 */
89static u32 TlclSendReceiveNoRetry(const u8 * request,
90 u8 * response, int max_length)
91{
92 size_t response_length = max_length;
93 u32 result;
94
95#ifdef EXTRA_LOGGING
96 printk(BIOS_DEBUG, "TPM: command: %x%x %x%x%x%x %x%x%x%x\n",
97 request[0], request[1],
98 request[2], request[3], request[4], request[5],
99 request[6], request[7], request[8], request[9]);
100#endif
101
102 result = TPM_SUCCESS;
103 if (tis_sendrecv
104 (request, TpmCommandSize(request), response, &response_length))
105 result = TPM_E_IOERROR;
106
107 if (0 != result) {
108 /* Communication with TPM failed, so response is garbage */
109 printk(BIOS_DEBUG,
110 "TPM: command 0x%x send/receive failed: 0x%x\n",
111 TpmCommandCode(request), result);
112 return TPM_E_COMMUNICATION_ERROR;
113 }
114 /* Otherwise, use the result code from the response */
115 result = TpmReturnCode(response);
116
117/* TODO: add paranoia about returned response_length vs. max_length
118 * (and possibly expected length from the response header). See
119 * crosbug.com/17017 */
120
121#ifdef EXTRA_LOGGING
122 printk(BIOS_DEBUG, "TPM: response: %x%x %x%x%x%x %x%x%x%x\n",
123 response[0], response[1],
124 response[2], response[3], response[4], response[5],
125 response[6], response[7], response[8], response[9]);
126#endif
127
128 printk(BIOS_DEBUG, "TPM: command 0x%x returned 0x%x\n",
129 TpmCommandCode(request), result);
130
131 return result;
132}
133
134static inline u32 TlclContinueSelfTest(void)
135{
136 u8 response[TPM_LARGE_ENOUGH_COMMAND_SIZE];
137 printk(BIOS_DEBUG, "TPM: Continue self test\n");
138 /* Call the No Retry version of SendReceive to avoid recursion. */
139 return TlclSendReceiveNoRetry(tpm_continueselftest_cmd.buffer,
140 response, sizeof(response));
141}
142
143/* Sends a TPM command and gets a response. Returns 0 if success or the TPM
144 * error code if error. In the firmware, waits for the self test to complete
145 * if needed. In the host, reports the first error without retries. */
146static u32 TlclSendReceive(const u8 * request, u8 * response, int max_length)
147{
148 u32 result = TlclSendReceiveNoRetry(request, response, max_length);
149 /* When compiling for the firmware, hide command failures due to the self
150 * test not having run or completed. */
151 /* If the command fails because the self test has not completed, try it
152 * again after attempting to ensure that the self test has completed. */
153 if (result == TPM_E_NEEDS_SELFTEST || result == TPM_E_DOING_SELFTEST) {
154 result = TlclContinueSelfTest();
155 if (result != TPM_SUCCESS) {
156 return result;
157 }
158#if defined(TPM_BLOCKING_CONTINUESELFTEST) || defined(VB_RECOVERY_MODE)
159 /* Retry only once */
160 result = TlclSendReceiveNoRetry(request, response, max_length);
161#else
162 /* This needs serious testing. The TPM specification says:
163 * "iii. The caller MUST wait for the actions of
164 * TPM_ContinueSelfTest to complete before reissuing the
165 * command C1." But, if ContinueSelfTest is non-blocking, how
166 * do we know that the actions have completed other than trying
167 * again? */
168 do {
169 result =
170 TlclSendReceiveNoRetry(request, response,
171 max_length);
172 } while (result == TPM_E_DOING_SELFTEST);
173#endif
174 }
175
176 return result;
177}
178
Vladimir Serbinenko0e90dae2015-05-18 10:29:06 +0200179void init_tpm(int s3resume)
Stefan Reinauer3008bbad2011-10-11 14:46:25 -0700180{
181 u32 result;
182 u8 response[TPM_LARGE_ENOUGH_COMMAND_SIZE];
183
Stefan Reinauer9aea04a2012-03-30 12:01:06 -0700184 /* Doing TPM startup when we're not coming in on the S3 resume path
185 * saves us roughly 20ms in boot time only. This does not seem to
186 * be worth an API change to vboot_reference-firmware right now, so
187 * let's keep the code around, but just bail out early:
188 */
Vladimir Serbinenko0e90dae2015-05-18 10:29:06 +0200189 if (s3resume ? CONFIG_NO_TPM_RESUME
190 : CONFIG_SKIP_TPM_STARTUP_ON_NORMAL_BOOT)
Stefan Reinauer9aea04a2012-03-30 12:01:06 -0700191 return;
Stefan Reinauer9aea04a2012-03-30 12:01:06 -0700192
Vladimir Serbinenko0e90dae2015-05-18 10:29:06 +0200193 printk(BIOS_DEBUG, "TPM initialization.\n");
Stefan Reinauer9aea04a2012-03-30 12:01:06 -0700194
195 printk(BIOS_SPEW, "TPM: Init\n");
Stefan Reinauer3008bbad2011-10-11 14:46:25 -0700196 if (tis_init())
197 return;
198
Stefan Reinauer9aea04a2012-03-30 12:01:06 -0700199 printk(BIOS_SPEW, "TPM: Open\n");
Stefan Reinauer3008bbad2011-10-11 14:46:25 -0700200 if (tis_open())
201 return;
202
Stefan Reinauer3008bbad2011-10-11 14:46:25 -0700203
Vladimir Serbinenko0e90dae2015-05-18 10:29:06 +0200204 if (s3resume) {
Stefan Reinauer9aea04a2012-03-30 12:01:06 -0700205 /* S3 Resume */
206 printk(BIOS_SPEW, "TPM: Resume\n");
207 result = TlclSendReceive(tpm_resume_cmd.buffer,
208 response, sizeof(response));
209 if (result == TPM_E_INVALID_POSTINIT) {
210 /* We're on a platform where the TPM maintains power
211 * in S3, so it's already initialized.
212 */
213 printk(BIOS_DEBUG, "TPM: Already initialized.\n");
214 return;
215 }
216 } else {
217 printk(BIOS_SPEW, "TPM: Startup\n");
218 result = TlclSendReceive(tpm_startup_cmd.buffer,
219 response, sizeof(response));
Stefan Reinauer3008bbad2011-10-11 14:46:25 -0700220 }
Stefan Reinauer9aea04a2012-03-30 12:01:06 -0700221
Stefan Reinauer3008bbad2011-10-11 14:46:25 -0700222 if (result == TPM_SUCCESS) {
Stefan Reinauer9aea04a2012-03-30 12:01:06 -0700223 printk(BIOS_SPEW, "TPM: OK.\n");
Stefan Reinauer3008bbad2011-10-11 14:46:25 -0700224 return;
225 }
Stefan Reinauer9aea04a2012-03-30 12:01:06 -0700226
Vladimir Serbinenko0e90dae2015-05-18 10:29:06 +0200227 printk(BIOS_ERR, "TPM: Error code 0x%x.\n", result);
Stefan Reinauer9aea04a2012-03-30 12:01:06 -0700228
Vladimir Serbinenko0e90dae2015-05-18 10:29:06 +0200229 if (CONFIG_TPM_INIT_FAILURE_IS_FATAL) {
230 printk(BIOS_ERR, "Hard reset!\n");
231 post_code(POST_TPM_FAILURE);
232 if (IS_ENABLED(CONFIG_CONSOLE_CBMEM_DUMP_TO_UART))
233 cbmem_dump_console();
234 hard_reset();
235 }
Stefan Reinauer3008bbad2011-10-11 14:46:25 -0700236}