blob: fc989dea328e0c474e03dd2b982854dbd82650ac [file] [log] [blame]
Daisuke Nojiriefb5cde2014-07-02 08:37:23 -07001/* Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
2 * Use of this source code is governed by a BSD-style license that can be
3 * found in the LICENSE file.
4 */
5
6/* A lightweight TPM command library.
7 *
8 * The general idea is that TPM commands are array of bytes whose
9 * fields are mostly compile-time constant. The goal is to build much
10 * of the commands at compile time (or build time) and change some of
11 * the fields at run time as needed. The code in
12 * utility/tlcl_generator.c builds structures containing the commands,
13 * as well as the offsets of the fields that need to be set at run
14 * time.
15 */
16
Furquan Shaikh8b5d04e2016-11-10 09:49:05 -080017#include <arch/early_variables.h>
Daisuke Nojiri57990972014-07-15 19:47:32 -070018#include <assert.h>
Randall Spangler144c2282014-12-03 17:35:53 -080019#include <string.h>
Daisuke Nojiri57990972014-07-15 19:47:32 -070020#include <tpm_lite/tlcl.h>
21#include <tpm.h>
Randall Spangler144c2282014-12-03 17:35:53 -080022#include <vb2_api.h>
Daisuke Nojiriefb5cde2014-07-02 08:37:23 -070023#include "tlcl_internal.h"
24#include "tlcl_structures.h"
Daisuke Nojiriefb5cde2014-07-02 08:37:23 -070025
26#ifdef FOR_TEST
Daisuke Nojiri57990972014-07-15 19:47:32 -070027#include <stdio.h>
28#define VBDEBUG(format, args...) printf(format, ## args)
29#else
30#include <console/console.h>
31#define VBDEBUG(format, args...) printk(BIOS_DEBUG, format, ## args)
Daisuke Nojiriefb5cde2014-07-02 08:37:23 -070032#endif
33
Daisuke Nojiri57990972014-07-15 19:47:32 -070034static int tpm_send_receive(const uint8_t *request,
35 uint32_t request_length,
36 uint8_t *response,
37 uint32_t *response_length)
38{
39 size_t len = *response_length;
40 if (tis_sendrecv(request, request_length, response, &len))
41 return VB2_ERROR_UNKNOWN;
42 /* check 64->32bit overflow and (re)check response buffer overflow */
43 if (len > *response_length)
44 return VB2_ERROR_UNKNOWN;
45 *response_length = len;
46 return VB2_SUCCESS;
47}
48
Daisuke Nojiriefb5cde2014-07-02 08:37:23 -070049/* Sets the size field of a TPM command. */
Lee Leahyb2d834a2017-03-08 16:52:22 -080050static inline void set_tpm_command_size(uint8_t *buffer, uint32_t size) {
Daisuke Nojiri57990972014-07-15 19:47:32 -070051 to_tpm_uint32(buffer + sizeof(uint16_t), size);
Daisuke Nojiriefb5cde2014-07-02 08:37:23 -070052}
53
54/* Gets the size field of a TPM command. */
55__attribute__((unused))
Lee Leahyb2d834a2017-03-08 16:52:22 -080056static inline int tpm_command_size(const uint8_t *buffer) {
Daisuke Nojiri57990972014-07-15 19:47:32 -070057 uint32_t size;
58 from_tpm_uint32(buffer + sizeof(uint16_t), &size);
59 return (int) size;
Daisuke Nojiriefb5cde2014-07-02 08:37:23 -070060}
61
62/* Gets the code field of a TPM command. */
Lee Leahyb2d834a2017-03-08 16:52:22 -080063static inline int tpm_command_code(const uint8_t *buffer) {
Daisuke Nojiri57990972014-07-15 19:47:32 -070064 uint32_t code;
65 from_tpm_uint32(buffer + sizeof(uint16_t) + sizeof(uint32_t), &code);
66 return code;
Daisuke Nojiriefb5cde2014-07-02 08:37:23 -070067}
68
69/* Gets the return code field of a TPM result. */
Lee Leahyb2d834a2017-03-08 16:52:22 -080070static inline int tpm_return_code(const uint8_t *buffer) {
Daisuke Nojiri57990972014-07-15 19:47:32 -070071 return tpm_command_code(buffer);
Daisuke Nojiriefb5cde2014-07-02 08:37:23 -070072}
73
74/* Like TlclSendReceive below, but do not retry if NEEDS_SELFTEST or
75 * DOING_SELFTEST errors are returned.
76 */
Lee Leahyb2d834a2017-03-08 16:52:22 -080077static uint32_t tlcl_send_receive_no_retry(const uint8_t *request,
Lee Leahye20a3192017-03-09 16:21:34 -080078 uint8_t *response, int max_length) {
Daisuke Nojiri57990972014-07-15 19:47:32 -070079 uint32_t response_length = max_length;
80 uint32_t result;
Daisuke Nojiriefb5cde2014-07-02 08:37:23 -070081
Daisuke Nojiri57990972014-07-15 19:47:32 -070082 result = tpm_send_receive(request, tpm_command_size(request),
83 response, &response_length);
84 if (0 != result) {
85 /* Communication with TPM failed, so response is garbage */
86 VBDEBUG("TPM: command 0x%x send/receive failed: 0x%x\n",
87 tpm_command_code(request), result);
88 return result;
89 }
90 /* Otherwise, use the result code from the response */
91 result = tpm_return_code(response);
Daisuke Nojiriefb5cde2014-07-02 08:37:23 -070092
Daisuke Nojiri57990972014-07-15 19:47:32 -070093 /* TODO: add paranoia about returned response_length vs. max_length
94 * (and possibly expected length from the response header). See
95 * crosbug.com/17017 */
Daisuke Nojiriefb5cde2014-07-02 08:37:23 -070096
Daisuke Nojiri57990972014-07-15 19:47:32 -070097 VBDEBUG("TPM: command 0x%x returned 0x%x\n",
98 tpm_command_code(request), result);
Daisuke Nojiriefb5cde2014-07-02 08:37:23 -070099
Daisuke Nojiri57990972014-07-15 19:47:32 -0700100return result;
Daisuke Nojiriefb5cde2014-07-02 08:37:23 -0700101}
102
103
104/* Sends a TPM command and gets a response. Returns 0 if success or the TPM
Daisuke Nojiri57990972014-07-15 19:47:32 -0700105 * error code if error. Waits for the self test to complete if needed. */
Lee Leahyb2d834a2017-03-08 16:52:22 -0800106uint32_t tlcl_send_receive(const uint8_t *request, uint8_t *response,
Daisuke Nojiri57990972014-07-15 19:47:32 -0700107 int max_length) {
108 uint32_t result = tlcl_send_receive_no_retry(request, response,
109 max_length);
110 /* If the command fails because the self test has not completed, try it
111 * again after attempting to ensure that the self test has completed. */
112 if (result == TPM_E_NEEDS_SELFTEST || result == TPM_E_DOING_SELFTEST) {
113 result = tlcl_continue_self_test();
114 if (result != TPM_SUCCESS)
115 return result;
Daisuke Nojiriefb5cde2014-07-02 08:37:23 -0700116#if defined(TPM_BLOCKING_CONTINUESELFTEST) || defined(VB_RECOVERY_MODE)
Daisuke Nojiri57990972014-07-15 19:47:32 -0700117 /* Retry only once */
118 result = tlcl_send_receive_no_retry(request, response,
Lee Leahye20a3192017-03-09 16:21:34 -0800119 max_length);
Daisuke Nojiriefb5cde2014-07-02 08:37:23 -0700120#else
Daisuke Nojiri57990972014-07-15 19:47:32 -0700121 /* This needs serious testing. The TPM specification says: "iii.
122 * The caller MUST wait for the actions of TPM_ContinueSelfTest
123 * to complete before reissuing the command C1." But, if
124 * ContinueSelfTest is non-blocking, how do we know that the
125 * actions have completed other than trying again? */
126 do {
127 result = tlcl_send_receive_no_retry(request, response,
Lee Leahye20a3192017-03-09 16:21:34 -0800128 max_length);
Daisuke Nojiri57990972014-07-15 19:47:32 -0700129 } while (result == TPM_E_DOING_SELFTEST);
Daisuke Nojiriefb5cde2014-07-02 08:37:23 -0700130#endif
Daisuke Nojiri57990972014-07-15 19:47:32 -0700131 }
132 return result;
Daisuke Nojiriefb5cde2014-07-02 08:37:23 -0700133}
134
135/* Sends a command and returns the error code. */
Lee Leahyb2d834a2017-03-08 16:52:22 -0800136static uint32_t send(const uint8_t *command) {
Daisuke Nojiri57990972014-07-15 19:47:32 -0700137 uint8_t response[TPM_LARGE_ENOUGH_COMMAND_SIZE];
138 return tlcl_send_receive(command, response, sizeof(response));
Daisuke Nojiriefb5cde2014-07-02 08:37:23 -0700139}
140
141/* Exported functions. */
142
Furquan Shaikh8b5d04e2016-11-10 09:49:05 -0800143static uint8_t tlcl_init_done CAR_GLOBAL;
144
Daisuke Nojiri57990972014-07-15 19:47:32 -0700145uint32_t tlcl_lib_init(void) {
Furquan Shaikh8b5d04e2016-11-10 09:49:05 -0800146 uint8_t done = car_get_var(tlcl_init_done);
147 if (done)
148 return VB2_SUCCESS;
149
Daisuke Nojiri57990972014-07-15 19:47:32 -0700150 if (tis_init())
151 return VB2_ERROR_UNKNOWN;
152 if (tis_open())
153 return VB2_ERROR_UNKNOWN;
Furquan Shaikh8b5d04e2016-11-10 09:49:05 -0800154
155 car_set_var(tlcl_init_done, 1);
156
Daisuke Nojiri57990972014-07-15 19:47:32 -0700157 return VB2_SUCCESS;
Daisuke Nojiriefb5cde2014-07-02 08:37:23 -0700158}
159
Daisuke Nojiri57990972014-07-15 19:47:32 -0700160uint32_t tlcl_startup(void) {
161 VBDEBUG("TPM: Startup\n");
162 return send(tpm_startup_cmd.buffer);
Daisuke Nojiriefb5cde2014-07-02 08:37:23 -0700163}
164
Daisuke Nojiri57990972014-07-15 19:47:32 -0700165uint32_t tlcl_resume(void) {
Lee Leahye20a3192017-03-09 16:21:34 -0800166 VBDEBUG("TPM: Resume\n");
167 return send(tpm_resume_cmd.buffer);
Daisuke Nojiriefb5cde2014-07-02 08:37:23 -0700168}
169
Daisuke Nojiri57990972014-07-15 19:47:32 -0700170uint32_t tlcl_self_test_full(void)
171{
172 VBDEBUG("TPM: Self test full\n");
173 return send(tpm_selftestfull_cmd.buffer);
Daisuke Nojiriefb5cde2014-07-02 08:37:23 -0700174}
175
Daisuke Nojiri57990972014-07-15 19:47:32 -0700176uint32_t tlcl_continue_self_test(void)
177{
178 uint8_t response[TPM_LARGE_ENOUGH_COMMAND_SIZE];
179 VBDEBUG("TPM: Continue self test\n");
180 /* Call the No Retry version of SendReceive to avoid recursion. */
181 return tlcl_send_receive_no_retry(tpm_continueselftest_cmd.buffer,
Lee Leahye20a3192017-03-09 16:21:34 -0800182 response, sizeof(response));
Daisuke Nojiriefb5cde2014-07-02 08:37:23 -0700183}
184
Daisuke Nojiri57990972014-07-15 19:47:32 -0700185uint32_t tlcl_define_space(uint32_t index, uint32_t perm, uint32_t size)
186{
187 struct s_tpm_nv_definespace_cmd cmd;
188 VBDEBUG("TPM: TlclDefineSpace(0x%x, 0x%x, %d)\n", index, perm, size);
189 memcpy(&cmd, &tpm_nv_definespace_cmd, sizeof(cmd));
190 to_tpm_uint32(cmd.buffer + tpm_nv_definespace_cmd.index, index);
191 to_tpm_uint32(cmd.buffer + tpm_nv_definespace_cmd.perm, perm);
192 to_tpm_uint32(cmd.buffer + tpm_nv_definespace_cmd.size, size);
193 return send(cmd.buffer);
Daisuke Nojiriefb5cde2014-07-02 08:37:23 -0700194}
195
Lee Leahyb2d834a2017-03-08 16:52:22 -0800196uint32_t tlcl_write(uint32_t index, const void *data, uint32_t length)
Daisuke Nojiri57990972014-07-15 19:47:32 -0700197{
198 struct s_tpm_nv_write_cmd cmd;
199 uint8_t response[TPM_LARGE_ENOUGH_COMMAND_SIZE];
200 const int total_length =
201 kTpmRequestHeaderLength + kWriteInfoLength + length;
202
203 VBDEBUG("TPM: tlcl_write(0x%x, %d)\n", index, length);
204 memcpy(&cmd, &tpm_nv_write_cmd, sizeof(cmd));
205 assert(total_length <= TPM_LARGE_ENOUGH_COMMAND_SIZE);
206 set_tpm_command_size(cmd.buffer, total_length);
207
208 to_tpm_uint32(cmd.buffer + tpm_nv_write_cmd.index, index);
209 to_tpm_uint32(cmd.buffer + tpm_nv_write_cmd.length, length);
210 memcpy(cmd.buffer + tpm_nv_write_cmd.data, data, length);
211
212 return tlcl_send_receive(cmd.buffer, response, sizeof(response));
Daisuke Nojiriefb5cde2014-07-02 08:37:23 -0700213}
214
Lee Leahyb2d834a2017-03-08 16:52:22 -0800215uint32_t tlcl_read(uint32_t index, void *data, uint32_t length)
Daisuke Nojiri57990972014-07-15 19:47:32 -0700216{
217 struct s_tpm_nv_read_cmd cmd;
218 uint8_t response[TPM_LARGE_ENOUGH_COMMAND_SIZE];
219 uint32_t result_length;
220 uint32_t result;
221
222 VBDEBUG("TPM: tlcl_read(0x%x, %d)\n", index, length);
223 memcpy(&cmd, &tpm_nv_read_cmd, sizeof(cmd));
224 to_tpm_uint32(cmd.buffer + tpm_nv_read_cmd.index, index);
225 to_tpm_uint32(cmd.buffer + tpm_nv_read_cmd.length, length);
226
227 result = tlcl_send_receive(cmd.buffer, response, sizeof(response));
228 if (result == TPM_SUCCESS && length > 0) {
Lee Leahyb2d834a2017-03-08 16:52:22 -0800229 uint8_t *nv_read_cursor = response + kTpmResponseHeaderLength;
Daisuke Nojiri57990972014-07-15 19:47:32 -0700230 from_tpm_uint32(nv_read_cursor, &result_length);
231 nv_read_cursor += sizeof(uint32_t);
232 memcpy(data, nv_read_cursor, result_length);
233 }
234
235 return result;
Daisuke Nojiriefb5cde2014-07-02 08:37:23 -0700236}
237
Daisuke Nojiriefb5cde2014-07-02 08:37:23 -0700238
Daisuke Nojiri57990972014-07-15 19:47:32 -0700239uint32_t tlcl_assert_physical_presence(void) {
240 VBDEBUG("TPM: Asserting physical presence\n");
241 return send(tpm_ppassert_cmd.buffer);
Daisuke Nojiriefb5cde2014-07-02 08:37:23 -0700242}
243
Daisuke Nojiri57990972014-07-15 19:47:32 -0700244uint32_t tlcl_physical_presence_cmd_enable(void) {
245 VBDEBUG("TPM: Enable the physical presence command\n");
246 return send(tpm_ppenable_cmd.buffer);
Daisuke Nojiriefb5cde2014-07-02 08:37:23 -0700247}
248
Daisuke Nojiri57990972014-07-15 19:47:32 -0700249uint32_t tlcl_finalize_physical_presence(void) {
250 VBDEBUG("TPM: Enable PP cmd, disable HW pp, and set lifetime lock\n");
251 return send(tpm_finalizepp_cmd.buffer);
Daisuke Nojiriefb5cde2014-07-02 08:37:23 -0700252}
253
Daisuke Nojiri57990972014-07-15 19:47:32 -0700254uint32_t tlcl_set_nv_locked(void) {
255 VBDEBUG("TPM: Set NV locked\n");
256 return tlcl_define_space(TPM_NV_INDEX_LOCK, 0, 0);
Daisuke Nojiriefb5cde2014-07-02 08:37:23 -0700257}
258
Daisuke Nojiri57990972014-07-15 19:47:32 -0700259uint32_t tlcl_force_clear(void) {
260 VBDEBUG("TPM: Force clear\n");
261 return send(tpm_forceclear_cmd.buffer);
Daisuke Nojiriefb5cde2014-07-02 08:37:23 -0700262}
263
Daisuke Nojiri57990972014-07-15 19:47:32 -0700264uint32_t tlcl_set_enable(void) {
265 VBDEBUG("TPM: Enabling TPM\n");
266 return send(tpm_physicalenable_cmd.buffer);
Daisuke Nojiriefb5cde2014-07-02 08:37:23 -0700267}
268
Daisuke Nojiri57990972014-07-15 19:47:32 -0700269uint32_t tlcl_set_deactivated(uint8_t flag)
270{
271 struct s_tpm_physicalsetdeactivated_cmd cmd;
272 VBDEBUG("TPM: SetDeactivated(%d)\n", flag);
273 memcpy(&cmd, &tpm_physicalsetdeactivated_cmd, sizeof(cmd));
274 *(cmd.buffer + cmd.deactivated) = flag;
275 return send(cmd.buffer);
Daisuke Nojiriefb5cde2014-07-02 08:37:23 -0700276}
277
Lee Leahyb2d834a2017-03-08 16:52:22 -0800278uint32_t tlcl_get_permanent_flags(TPM_PERMANENT_FLAGS *pflags)
Daisuke Nojiri57990972014-07-15 19:47:32 -0700279{
280 uint8_t response[TPM_LARGE_ENOUGH_COMMAND_SIZE];
281 uint32_t size;
282 uint32_t result = tlcl_send_receive(tpm_getflags_cmd.buffer, response,
Lee Leahye20a3192017-03-09 16:21:34 -0800283 sizeof(response));
Daisuke Nojiri57990972014-07-15 19:47:32 -0700284 if (result != TPM_SUCCESS)
285 return result;
286 from_tpm_uint32(response + kTpmResponseHeaderLength, &size);
287 assert(size == sizeof(TPM_PERMANENT_FLAGS));
288 memcpy(pflags, response + kTpmResponseHeaderLength + sizeof(size),
289 sizeof(TPM_PERMANENT_FLAGS));
290 return result;
Daisuke Nojiriefb5cde2014-07-02 08:37:23 -0700291}
292
Lee Leahyb2d834a2017-03-08 16:52:22 -0800293uint32_t tlcl_get_flags(uint8_t *disable, uint8_t *deactivated,
Lee Leahye20a3192017-03-09 16:21:34 -0800294 uint8_t *nvlocked)
Daisuke Nojiri57990972014-07-15 19:47:32 -0700295{
296 TPM_PERMANENT_FLAGS pflags;
297 uint32_t result = tlcl_get_permanent_flags(&pflags);
298 if (result == TPM_SUCCESS) {
299 if (disable)
300 *disable = pflags.disable;
301 if (deactivated)
302 *deactivated = pflags.deactivated;
303 if (nvlocked)
304 *nvlocked = pflags.nvLocked;
305 VBDEBUG("TPM: flags disable=%d, deactivated=%d, nvlocked=%d\n",
306 pflags.disable, pflags.deactivated, pflags.nvLocked);
307 }
308 return result;
Daisuke Nojiriefb5cde2014-07-02 08:37:23 -0700309}
310
Daisuke Nojiri57990972014-07-15 19:47:32 -0700311uint32_t tlcl_set_global_lock(void)
312{
313 uint32_t x;
314 VBDEBUG("TPM: Set global lock\n");
Lee Leahyb2d834a2017-03-08 16:52:22 -0800315 return tlcl_write(TPM_NV_INDEX0, (uint8_t *) &x, 0);
Daisuke Nojiriefb5cde2014-07-02 08:37:23 -0700316}
317
Lee Leahyb2d834a2017-03-08 16:52:22 -0800318uint32_t tlcl_extend(int pcr_num, const uint8_t *in_digest,
Lee Leahye20a3192017-03-09 16:21:34 -0800319 uint8_t *out_digest)
Daisuke Nojiri57990972014-07-15 19:47:32 -0700320{
321 struct s_tpm_extend_cmd cmd;
322 uint8_t response[kTpmResponseHeaderLength + kPcrDigestLength];
323 uint32_t result;
Daisuke Nojiriefb5cde2014-07-02 08:37:23 -0700324
Daisuke Nojiri57990972014-07-15 19:47:32 -0700325 memcpy(&cmd, &tpm_extend_cmd, sizeof(cmd));
326 to_tpm_uint32(cmd.buffer + tpm_extend_cmd.pcrNum, pcr_num);
327 memcpy(cmd.buffer + cmd.inDigest, in_digest, kPcrDigestLength);
Daisuke Nojiriefb5cde2014-07-02 08:37:23 -0700328
Daisuke Nojiri57990972014-07-15 19:47:32 -0700329 result = tlcl_send_receive(cmd.buffer, response, sizeof(response));
330 if (result != TPM_SUCCESS)
331 return result;
Daisuke Nojiriefb5cde2014-07-02 08:37:23 -0700332
Julius Werner76e33032015-01-30 18:45:27 -0800333 if (out_digest)
334 memcpy(out_digest, response + kTpmResponseHeaderLength,
335 kPcrDigestLength);
Daisuke Nojiri57990972014-07-15 19:47:32 -0700336 return result;
Daisuke Nojiriefb5cde2014-07-02 08:37:23 -0700337}