Add TPM support to coreboot
[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 "chromeos.h"
25
26 //#define EXTRA_LOGGING
27
28 #define TPM_LARGE_ENOUGH_COMMAND_SIZE 256       /* saves space in the firmware */
29
30 #define TPM_SUCCESS               ((u32)0x00000000)
31
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)
36
37 #define TPM_E_NEEDS_SELFTEST     ((u32)(TPM_E_NON_FATAL + 1))
38 #define TPM_E_DOING_SELFTEST     ((u32)(TPM_E_NON_FATAL + 2))
39
40 static const struct {
41         u8 buffer[12];
42 } tpm_resume_cmd = {
43         { 0x0, 0xc1, 0x0, 0x0, 0x0, 0xc, 0x0, 0x0, 0x0, 0x99, 0x0, 0x2 }
44 };
45
46 static const struct {
47         u8 buffer[10];
48 } tpm_continueselftest_cmd = {
49         { 0x0, 0xc1, 0x0, 0x0, 0x0, 0xa, 0x0, 0x0, 0x0, 0x53 }
50 };
51
52 static inline void FromTpmUint32(const u8 * buffer, u32 * x)
53 {
54         *x = ((buffer[0] << 24) |
55               (buffer[1] << 16) | (buffer[2] << 8) | buffer[3]);
56 }
57
58 static inline int TpmCommandSize(const u8 * buffer)
59 {
60         u32 size;
61         FromTpmUint32(buffer + sizeof(u16), &size);
62         return (int)size;
63 }
64
65 /* Gets the code field of a TPM command. */
66 static inline int TpmCommandCode(const u8 * buffer)
67 {
68         u32 code;
69         FromTpmUint32(buffer + sizeof(u16) + sizeof(u32), &code);
70         return code;
71 }
72
73 /* Gets the return code field of a TPM result. */
74 static inline int TpmReturnCode(const u8 * buffer)
75 {
76         return TpmCommandCode(buffer);
77 }
78
79 /* Like TlclSendReceive below, but do not retry if NEEDS_SELFTEST or
80  * DOING_SELFTEST errors are returned.
81  */
82 static u32 TlclSendReceiveNoRetry(const u8 * request,
83                                   u8 * response, int max_length)
84 {
85         size_t response_length = max_length;
86         u32 result;
87
88 #ifdef EXTRA_LOGGING
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]);
93 #endif
94
95         result = TPM_SUCCESS;
96         if (tis_sendrecv
97             (request, TpmCommandSize(request), response, &response_length))
98                 result = TPM_E_IOERROR;
99
100         if (0 != result) {
101                 /* Communication with TPM failed, so response is garbage */
102                 printk(BIOS_DEBUG,
103                        "TPM: command 0x%x send/receive failed: 0x%x\n",
104                        TpmCommandCode(request), result);
105                 return TPM_E_COMMUNICATION_ERROR;
106         }
107         /* Otherwise, use the result code from the response */
108         result = TpmReturnCode(response);
109
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 */
113
114 #ifdef EXTRA_LOGGING
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]);
119 #endif
120
121         printk(BIOS_DEBUG, "TPM: command 0x%x returned 0x%x\n",
122                TpmCommandCode(request), result);
123
124         return result;
125 }
126
127 static inline u32 TlclContinueSelfTest(void)
128 {
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));
134 }
135
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)
140 {
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) {
149                         return result;
150                 }
151 #if defined(TPM_BLOCKING_CONTINUESELFTEST) || defined(VB_RECOVERY_MODE)
152                 /* Retry only once */
153                 result = TlclSendReceiveNoRetry(request, response, max_length);
154 #else
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
160                  * again? */
161                 do {
162                         result =
163                             TlclSendReceiveNoRetry(request, response,
164                                                    max_length);
165                 } while (result == TPM_E_DOING_SELFTEST);
166 #endif
167         }
168
169         return result;
170 }
171
172 void init_vboot(void)
173 {
174         u32 result;
175         u8 response[TPM_LARGE_ENOUGH_COMMAND_SIZE];
176
177         printk(BIOS_DEBUG, "TPM: Init\n");
178         if (tis_init())
179                 return;
180
181         printk(BIOS_DEBUG, "TPM: Open\n");
182         if (tis_open())
183                 return;
184
185         printk(BIOS_DEBUG, "TPM: Resume\n");
186
187         result =
188             TlclSendReceive(tpm_resume_cmd.buffer, response, sizeof(response));
189
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");
194                 return;
195         }
196         if (result == TPM_SUCCESS) {
197                 printk(BIOS_DEBUG, "TPM: OK.\n");
198                 return;
199         }
200         // TODO(reinauer) hard reboot?
201 }