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>
26 //#define EXTRA_LOGGING
28 #define TPM_LARGE_ENOUGH_COMMAND_SIZE 256 /* saves space in the firmware */
30 #define TPM_SUCCESS ((u32)0x00000000)
32 #define TPM_E_IOERROR ((u32)0x0000001f)
33 #define TPM_E_COMMUNICATION_ERROR ((u32)0x00005004)
34 #define TPM_E_NON_FATAL ((u32)0x00000800)
35 #define TPM_E_INVALID_POSTINIT ((u32)0x00000026)
37 #define TPM_E_NEEDS_SELFTEST ((u32)(TPM_E_NON_FATAL + 1))
38 #define TPM_E_DOING_SELFTEST ((u32)(TPM_E_NON_FATAL + 2))
43 { 0x0, 0xc1, 0x0, 0x0, 0x0, 0xc, 0x0, 0x0, 0x0, 0x99, 0x0, 0x2 }
48 } tpm_continueselftest_cmd = {
49 { 0x0, 0xc1, 0x0, 0x0, 0x0, 0xa, 0x0, 0x0, 0x0, 0x53 }
52 static inline void FromTpmUint32(const u8 * buffer, u32 * x)
54 *x = ((buffer[0] << 24) |
55 (buffer[1] << 16) | (buffer[2] << 8) | buffer[3]);
58 static inline int TpmCommandSize(const u8 * buffer)
61 FromTpmUint32(buffer + sizeof(u16), &size);
65 /* Gets the code field of a TPM command. */
66 static inline int TpmCommandCode(const u8 * buffer)
69 FromTpmUint32(buffer + sizeof(u16) + sizeof(u32), &code);
73 /* Gets the return code field of a TPM result. */
74 static inline int TpmReturnCode(const u8 * buffer)
76 return TpmCommandCode(buffer);
79 /* Like TlclSendReceive below, but do not retry if NEEDS_SELFTEST or
80 * DOING_SELFTEST errors are returned.
82 static u32 TlclSendReceiveNoRetry(const u8 * request,
83 u8 * response, int max_length)
85 size_t response_length = max_length;
89 printk(BIOS_DEBUG, "TPM: command: %x%x %x%x%x%x %x%x%x%x\n",
90 request[0], request[1],
91 request[2], request[3], request[4], request[5],
92 request[6], request[7], request[8], request[9]);
97 (request, TpmCommandSize(request), response, &response_length))
98 result = TPM_E_IOERROR;
101 /* Communication with TPM failed, so response is garbage */
103 "TPM: command 0x%x send/receive failed: 0x%x\n",
104 TpmCommandCode(request), result);
105 return TPM_E_COMMUNICATION_ERROR;
107 /* Otherwise, use the result code from the response */
108 result = TpmReturnCode(response);
110 /* TODO: add paranoia about returned response_length vs. max_length
111 * (and possibly expected length from the response header). See
112 * crosbug.com/17017 */
115 printk(BIOS_DEBUG, "TPM: response: %x%x %x%x%x%x %x%x%x%x\n",
116 response[0], response[1],
117 response[2], response[3], response[4], response[5],
118 response[6], response[7], response[8], response[9]);
121 printk(BIOS_DEBUG, "TPM: command 0x%x returned 0x%x\n",
122 TpmCommandCode(request), result);
127 static inline u32 TlclContinueSelfTest(void)
129 u8 response[TPM_LARGE_ENOUGH_COMMAND_SIZE];
130 printk(BIOS_DEBUG, "TPM: Continue self test\n");
131 /* Call the No Retry version of SendReceive to avoid recursion. */
132 return TlclSendReceiveNoRetry(tpm_continueselftest_cmd.buffer,
133 response, sizeof(response));
136 /* Sends a TPM command and gets a response. Returns 0 if success or the TPM
137 * error code if error. In the firmware, waits for the self test to complete
138 * if needed. In the host, reports the first error without retries. */
139 static u32 TlclSendReceive(const u8 * request, u8 * response, int max_length)
141 u32 result = TlclSendReceiveNoRetry(request, response, max_length);
142 /* When compiling for the firmware, hide command failures due to the self
143 * test not having run or completed. */
144 /* If the command fails because the self test has not completed, try it
145 * again after attempting to ensure that the self test has completed. */
146 if (result == TPM_E_NEEDS_SELFTEST || result == TPM_E_DOING_SELFTEST) {
147 result = TlclContinueSelfTest();
148 if (result != TPM_SUCCESS) {
151 #if defined(TPM_BLOCKING_CONTINUESELFTEST) || defined(VB_RECOVERY_MODE)
152 /* Retry only once */
153 result = TlclSendReceiveNoRetry(request, response, max_length);
155 /* This needs serious testing. The TPM specification says:
156 * "iii. The caller MUST wait for the actions of
157 * TPM_ContinueSelfTest to complete before reissuing the
158 * command C1." But, if ContinueSelfTest is non-blocking, how
159 * do we know that the actions have completed other than trying
163 TlclSendReceiveNoRetry(request, response,
165 } while (result == TPM_E_DOING_SELFTEST);
172 void init_vboot(void)
175 u8 response[TPM_LARGE_ENOUGH_COMMAND_SIZE];
177 printk(BIOS_DEBUG, "TPM: Init\n");
181 printk(BIOS_DEBUG, "TPM: Open\n");
185 printk(BIOS_DEBUG, "TPM: Resume\n");
188 TlclSendReceive(tpm_resume_cmd.buffer, response, sizeof(response));
190 if (result == TPM_E_INVALID_POSTINIT) {
191 /* We're on a platform where the TPM maintains power in S3, so
192 * it's already initialized. */
193 printk(BIOS_DEBUG, "TPM: Already initialized.\n");
196 if (result == TPM_SUCCESS) {
197 printk(BIOS_DEBUG, "TPM: OK.\n");
200 // TODO(reinauer) hard reboot?