Add Google ChromeOS vendor support
[coreboot.git] / src / vendorcode / google / chromeos / vboot.c
1 /*
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
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18  */
19
20 #include <types.h>
21 #include <console/console.h>
22 #include <arch/acpi.h>
23 #include <pc80/tpm.h>
24 #include <reset.h>
25 #include "chromeos.h"
26
27 //#define EXTRA_LOGGING
28 #define UBOOT_DOES_TPM_STARTUP
29
30 #define TPM_LARGE_ENOUGH_COMMAND_SIZE 256       /* saves space in the firmware */
31
32 #define TPM_SUCCESS               ((u32)0x00000000)
33
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)
38
39 #define TPM_E_NEEDS_SELFTEST     ((u32)(TPM_E_NON_FATAL + 1))
40 #define TPM_E_DOING_SELFTEST     ((u32)(TPM_E_NON_FATAL + 2))
41
42 static const struct {
43         u8 buffer[12];
44 } tpm_resume_cmd = {
45         { 0x0, 0xc1, 0x0, 0x0, 0x0, 0xc, 0x0, 0x0, 0x0, 0x99, 0x0, 0x2 }
46 };
47
48 static const struct {
49         u8 buffer[12];
50 } tpm_startup_cmd = {
51         {0x0, 0xc1, 0x0, 0x0, 0x0, 0xc, 0x0, 0x0, 0x0, 0x99, 0x0, 0x1 }
52 };
53
54 static const struct {
55         u8 buffer[10];
56 } tpm_continueselftest_cmd = {
57         { 0x0, 0xc1, 0x0, 0x0, 0x0, 0xa, 0x0, 0x0, 0x0, 0x53 }
58 };
59
60 static inline void FromTpmUint32(const u8 * buffer, u32 * x)
61 {
62         *x = ((buffer[0] << 24) |
63               (buffer[1] << 16) | (buffer[2] << 8) | buffer[3]);
64 }
65
66 static inline int TpmCommandSize(const u8 * buffer)
67 {
68         u32 size;
69         FromTpmUint32(buffer + sizeof(u16), &size);
70         return (int)size;
71 }
72
73 /* Gets the code field of a TPM command. */
74 static inline int TpmCommandCode(const u8 * buffer)
75 {
76         u32 code;
77         FromTpmUint32(buffer + sizeof(u16) + sizeof(u32), &code);
78         return code;
79 }
80
81 /* Gets the return code field of a TPM result. */
82 static inline int TpmReturnCode(const u8 * buffer)
83 {
84         return TpmCommandCode(buffer);
85 }
86
87 /* Like TlclSendReceive below, but do not retry if NEEDS_SELFTEST or
88  * DOING_SELFTEST errors are returned.
89  */
90 static u32 TlclSendReceiveNoRetry(const u8 * request,
91                                   u8 * response, int max_length)
92 {
93         size_t response_length = max_length;
94         u32 result;
95
96 #ifdef EXTRA_LOGGING
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]);
101 #endif
102
103         result = TPM_SUCCESS;
104         if (tis_sendrecv
105             (request, TpmCommandSize(request), response, &response_length))
106                 result = TPM_E_IOERROR;
107
108         if (0 != result) {
109                 /* Communication with TPM failed, so response is garbage */
110                 printk(BIOS_DEBUG,
111                        "TPM: command 0x%x send/receive failed: 0x%x\n",
112                        TpmCommandCode(request), result);
113                 return TPM_E_COMMUNICATION_ERROR;
114         }
115         /* Otherwise, use the result code from the response */
116         result = TpmReturnCode(response);
117
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 */
121
122 #ifdef EXTRA_LOGGING
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]);
127 #endif
128
129         printk(BIOS_DEBUG, "TPM: command 0x%x returned 0x%x\n",
130                TpmCommandCode(request), result);
131
132         return result;
133 }
134
135 static inline u32 TlclContinueSelfTest(void)
136 {
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));
142 }
143
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)
148 {
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) {
157                         return result;
158                 }
159 #if defined(TPM_BLOCKING_CONTINUESELFTEST) || defined(VB_RECOVERY_MODE)
160                 /* Retry only once */
161                 result = TlclSendReceiveNoRetry(request, response, max_length);
162 #else
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
168                  * again? */
169                 do {
170                         result =
171                             TlclSendReceiveNoRetry(request, response,
172                                                    max_length);
173                 } while (result == TPM_E_DOING_SELFTEST);
174 #endif
175         }
176
177         return result;
178 }
179
180 static void init_vboot(int bootmode)
181 {
182         u32 result;
183         u8 response[TPM_LARGE_ENOUGH_COMMAND_SIZE];
184
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:
190          */
191         if (bootmode != 2)
192                 return;
193 #endif
194
195         printk(BIOS_DEBUG, "Verified boot TPM initialization.\n");
196
197         printk(BIOS_SPEW, "TPM: Init\n");
198         if (tis_init())
199                 return;
200
201         printk(BIOS_SPEW, "TPM: Open\n");
202         if (tis_open())
203                 return;
204
205
206         if (bootmode == 2) {
207                 /* S3 Resume */
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.
214                          */
215                         printk(BIOS_DEBUG, "TPM: Already initialized.\n");
216                         return;
217                 }
218         } else {
219                 printk(BIOS_SPEW, "TPM: Startup\n");
220                 result = TlclSendReceive(tpm_startup_cmd.buffer,
221                                         response, sizeof(response));
222         }
223
224         if (result == TPM_SUCCESS) {
225                 printk(BIOS_SPEW, "TPM: OK.\n");
226                 return;
227         }
228
229         printk(BIOS_ERR, "TPM: Error code 0x%x. Hard reset!\n", result);
230         hard_reset();
231 }
232
233 void init_chromeos(int bootmode)
234 {
235         init_vboot(bootmode);
236 }