2 * This file is part of the coreboot project.
4 * Copyright (C) 2011 The ChromiumOS Authors. All rights reserved.
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.
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.
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
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21 #include <console/console.h>
22 #include <arch/acpi.h>
27 //#define EXTRA_LOGGING
28 #define UBOOT_DOES_TPM_STARTUP
30 #define TPM_LARGE_ENOUGH_COMMAND_SIZE 256 /* saves space in the firmware */
32 #define TPM_SUCCESS ((u32)0x00000000)
34 #define TPM_E_IOERROR ((u32)0x0000001f)
35 #define TPM_E_COMMUNICATION_ERROR ((u32)0x00005004)
36 #define TPM_E_NON_FATAL ((u32)0x00000800)
37 #define TPM_E_INVALID_POSTINIT ((u32)0x00000026)
39 #define TPM_E_NEEDS_SELFTEST ((u32)(TPM_E_NON_FATAL + 1))
40 #define TPM_E_DOING_SELFTEST ((u32)(TPM_E_NON_FATAL + 2))
45 { 0x0, 0xc1, 0x0, 0x0, 0x0, 0xc, 0x0, 0x0, 0x0, 0x99, 0x0, 0x2 }
51 {0x0, 0xc1, 0x0, 0x0, 0x0, 0xc, 0x0, 0x0, 0x0, 0x99, 0x0, 0x1 }
56 } tpm_continueselftest_cmd = {
57 { 0x0, 0xc1, 0x0, 0x0, 0x0, 0xa, 0x0, 0x0, 0x0, 0x53 }
60 static inline void FromTpmUint32(const u8 * buffer, u32 * x)
62 *x = ((buffer[0] << 24) |
63 (buffer[1] << 16) | (buffer[2] << 8) | buffer[3]);
66 static inline int TpmCommandSize(const u8 * buffer)
69 FromTpmUint32(buffer + sizeof(u16), &size);
73 /* Gets the code field of a TPM command. */
74 static inline int TpmCommandCode(const u8 * buffer)
77 FromTpmUint32(buffer + sizeof(u16) + sizeof(u32), &code);
81 /* Gets the return code field of a TPM result. */
82 static inline int TpmReturnCode(const u8 * buffer)
84 return TpmCommandCode(buffer);
87 /* Like TlclSendReceive below, but do not retry if NEEDS_SELFTEST or
88 * DOING_SELFTEST errors are returned.
90 static u32 TlclSendReceiveNoRetry(const u8 * request,
91 u8 * response, int max_length)
93 size_t response_length = max_length;
97 printk(BIOS_DEBUG, "TPM: command: %x%x %x%x%x%x %x%x%x%x\n",
98 request[0], request[1],
99 request[2], request[3], request[4], request[5],
100 request[6], request[7], request[8], request[9]);
103 result = TPM_SUCCESS;
105 (request, TpmCommandSize(request), response, &response_length))
106 result = TPM_E_IOERROR;
109 /* Communication with TPM failed, so response is garbage */
111 "TPM: command 0x%x send/receive failed: 0x%x\n",
112 TpmCommandCode(request), result);
113 return TPM_E_COMMUNICATION_ERROR;
115 /* Otherwise, use the result code from the response */
116 result = TpmReturnCode(response);
118 /* TODO: add paranoia about returned response_length vs. max_length
119 * (and possibly expected length from the response header). See
120 * crosbug.com/17017 */
123 printk(BIOS_DEBUG, "TPM: response: %x%x %x%x%x%x %x%x%x%x\n",
124 response[0], response[1],
125 response[2], response[3], response[4], response[5],
126 response[6], response[7], response[8], response[9]);
129 printk(BIOS_DEBUG, "TPM: command 0x%x returned 0x%x\n",
130 TpmCommandCode(request), result);
135 static inline u32 TlclContinueSelfTest(void)
137 u8 response[TPM_LARGE_ENOUGH_COMMAND_SIZE];
138 printk(BIOS_DEBUG, "TPM: Continue self test\n");
139 /* Call the No Retry version of SendReceive to avoid recursion. */
140 return TlclSendReceiveNoRetry(tpm_continueselftest_cmd.buffer,
141 response, sizeof(response));
144 /* Sends a TPM command and gets a response. Returns 0 if success or the TPM
145 * error code if error. In the firmware, waits for the self test to complete
146 * if needed. In the host, reports the first error without retries. */
147 static u32 TlclSendReceive(const u8 * request, u8 * response, int max_length)
149 u32 result = TlclSendReceiveNoRetry(request, response, max_length);
150 /* When compiling for the firmware, hide command failures due to the self
151 * test not having run or completed. */
152 /* If the command fails because the self test has not completed, try it
153 * again after attempting to ensure that the self test has completed. */
154 if (result == TPM_E_NEEDS_SELFTEST || result == TPM_E_DOING_SELFTEST) {
155 result = TlclContinueSelfTest();
156 if (result != TPM_SUCCESS) {
159 #if defined(TPM_BLOCKING_CONTINUESELFTEST) || defined(VB_RECOVERY_MODE)
160 /* Retry only once */
161 result = TlclSendReceiveNoRetry(request, response, max_length);
163 /* This needs serious testing. The TPM specification says:
164 * "iii. The caller MUST wait for the actions of
165 * TPM_ContinueSelfTest to complete before reissuing the
166 * command C1." But, if ContinueSelfTest is non-blocking, how
167 * do we know that the actions have completed other than trying
171 TlclSendReceiveNoRetry(request, response,
173 } while (result == TPM_E_DOING_SELFTEST);
180 static void init_vboot(int bootmode)
183 u8 response[TPM_LARGE_ENOUGH_COMMAND_SIZE];
185 #ifdef UBOOT_DOES_TPM_STARTUP
186 /* Doing TPM startup when we're not coming in on the S3 resume path
187 * saves us roughly 20ms in boot time only. This does not seem to
188 * be worth an API change to vboot_reference-firmware right now, so
189 * let's keep the code around, but just bail out early:
195 printk(BIOS_DEBUG, "Verified boot TPM initialization.\n");
197 printk(BIOS_SPEW, "TPM: Init\n");
201 printk(BIOS_SPEW, "TPM: Open\n");
208 printk(BIOS_SPEW, "TPM: Resume\n");
209 result = TlclSendReceive(tpm_resume_cmd.buffer,
210 response, sizeof(response));
211 if (result == TPM_E_INVALID_POSTINIT) {
212 /* We're on a platform where the TPM maintains power
213 * in S3, so it's already initialized.
215 printk(BIOS_DEBUG, "TPM: Already initialized.\n");
219 printk(BIOS_SPEW, "TPM: Startup\n");
220 result = TlclSendReceive(tpm_startup_cmd.buffer,
221 response, sizeof(response));
224 if (result == TPM_SUCCESS) {
225 printk(BIOS_SPEW, "TPM: OK.\n");
229 printk(BIOS_ERR, "TPM: Error code 0x%x. Hard reset!\n", result);
233 void init_chromeos(int bootmode)
235 init_vboot(bootmode);