Daisuke Nojiri | efb5cde | 2014-07-02 08:37:23 -0700 | [diff] [blame] | 1 | /* 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 Shaikh | 8b5d04e | 2016-11-10 09:49:05 -0800 | [diff] [blame] | 17 | #include <arch/early_variables.h> |
Daisuke Nojiri | 5799097 | 2014-07-15 19:47:32 -0700 | [diff] [blame] | 18 | #include <assert.h> |
Randall Spangler | 144c228 | 2014-12-03 17:35:53 -0800 | [diff] [blame] | 19 | #include <string.h> |
Daisuke Nojiri | 5799097 | 2014-07-15 19:47:32 -0700 | [diff] [blame] | 20 | #include <tpm_lite/tlcl.h> |
| 21 | #include <tpm.h> |
Randall Spangler | 144c228 | 2014-12-03 17:35:53 -0800 | [diff] [blame] | 22 | #include <vb2_api.h> |
Daisuke Nojiri | efb5cde | 2014-07-02 08:37:23 -0700 | [diff] [blame] | 23 | #include "tlcl_internal.h" |
| 24 | #include "tlcl_structures.h" |
Daisuke Nojiri | efb5cde | 2014-07-02 08:37:23 -0700 | [diff] [blame] | 25 | |
| 26 | #ifdef FOR_TEST |
Daisuke Nojiri | 5799097 | 2014-07-15 19:47:32 -0700 | [diff] [blame] | 27 | #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 Nojiri | efb5cde | 2014-07-02 08:37:23 -0700 | [diff] [blame] | 32 | #endif |
| 33 | |
Daisuke Nojiri | 5799097 | 2014-07-15 19:47:32 -0700 | [diff] [blame] | 34 | static 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 Nojiri | efb5cde | 2014-07-02 08:37:23 -0700 | [diff] [blame] | 49 | /* Sets the size field of a TPM command. */ |
Lee Leahy | 342f8d6 | 2017-03-10 15:31:56 -0800 | [diff] [blame] | 50 | static inline void set_tpm_command_size(uint8_t *buffer, uint32_t size) |
| 51 | { |
Daisuke Nojiri | 5799097 | 2014-07-15 19:47:32 -0700 | [diff] [blame] | 52 | to_tpm_uint32(buffer + sizeof(uint16_t), size); |
Daisuke Nojiri | efb5cde | 2014-07-02 08:37:23 -0700 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | /* Gets the size field of a TPM command. */ |
| 56 | __attribute__((unused)) |
Lee Leahy | 342f8d6 | 2017-03-10 15:31:56 -0800 | [diff] [blame] | 57 | static inline int tpm_command_size(const uint8_t *buffer) |
| 58 | { |
Daisuke Nojiri | 5799097 | 2014-07-15 19:47:32 -0700 | [diff] [blame] | 59 | uint32_t size; |
| 60 | from_tpm_uint32(buffer + sizeof(uint16_t), &size); |
| 61 | return (int) size; |
Daisuke Nojiri | efb5cde | 2014-07-02 08:37:23 -0700 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | /* Gets the code field of a TPM command. */ |
Lee Leahy | 342f8d6 | 2017-03-10 15:31:56 -0800 | [diff] [blame] | 65 | static inline int tpm_command_code(const uint8_t *buffer) |
| 66 | { |
Daisuke Nojiri | 5799097 | 2014-07-15 19:47:32 -0700 | [diff] [blame] | 67 | uint32_t code; |
| 68 | from_tpm_uint32(buffer + sizeof(uint16_t) + sizeof(uint32_t), &code); |
| 69 | return code; |
Daisuke Nojiri | efb5cde | 2014-07-02 08:37:23 -0700 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | /* Gets the return code field of a TPM result. */ |
Lee Leahy | 342f8d6 | 2017-03-10 15:31:56 -0800 | [diff] [blame] | 73 | static inline int tpm_return_code(const uint8_t *buffer) |
| 74 | { |
Daisuke Nojiri | 5799097 | 2014-07-15 19:47:32 -0700 | [diff] [blame] | 75 | return tpm_command_code(buffer); |
Daisuke Nojiri | efb5cde | 2014-07-02 08:37:23 -0700 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | /* Like TlclSendReceive below, but do not retry if NEEDS_SELFTEST or |
| 79 | * DOING_SELFTEST errors are returned. |
| 80 | */ |
Lee Leahy | b2d834a | 2017-03-08 16:52:22 -0800 | [diff] [blame] | 81 | static uint32_t tlcl_send_receive_no_retry(const uint8_t *request, |
Lee Leahy | 342f8d6 | 2017-03-10 15:31:56 -0800 | [diff] [blame] | 82 | uint8_t *response, int max_length) |
| 83 | { |
Daisuke Nojiri | 5799097 | 2014-07-15 19:47:32 -0700 | [diff] [blame] | 84 | uint32_t response_length = max_length; |
| 85 | uint32_t result; |
Daisuke Nojiri | efb5cde | 2014-07-02 08:37:23 -0700 | [diff] [blame] | 86 | |
Daisuke Nojiri | 5799097 | 2014-07-15 19:47:32 -0700 | [diff] [blame] | 87 | result = tpm_send_receive(request, tpm_command_size(request), |
| 88 | response, &response_length); |
| 89 | if (0 != result) { |
| 90 | /* Communication with TPM failed, so response is garbage */ |
| 91 | VBDEBUG("TPM: command 0x%x send/receive failed: 0x%x\n", |
| 92 | tpm_command_code(request), result); |
| 93 | return result; |
| 94 | } |
| 95 | /* Otherwise, use the result code from the response */ |
| 96 | result = tpm_return_code(response); |
Daisuke Nojiri | efb5cde | 2014-07-02 08:37:23 -0700 | [diff] [blame] | 97 | |
Daisuke Nojiri | 5799097 | 2014-07-15 19:47:32 -0700 | [diff] [blame] | 98 | /* TODO: add paranoia about returned response_length vs. max_length |
| 99 | * (and possibly expected length from the response header). See |
| 100 | * crosbug.com/17017 */ |
Daisuke Nojiri | efb5cde | 2014-07-02 08:37:23 -0700 | [diff] [blame] | 101 | |
Daisuke Nojiri | 5799097 | 2014-07-15 19:47:32 -0700 | [diff] [blame] | 102 | VBDEBUG("TPM: command 0x%x returned 0x%x\n", |
| 103 | tpm_command_code(request), result); |
Daisuke Nojiri | efb5cde | 2014-07-02 08:37:23 -0700 | [diff] [blame] | 104 | |
Daisuke Nojiri | 5799097 | 2014-07-15 19:47:32 -0700 | [diff] [blame] | 105 | return result; |
Daisuke Nojiri | efb5cde | 2014-07-02 08:37:23 -0700 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | |
| 109 | /* Sends a TPM command and gets a response. Returns 0 if success or the TPM |
Daisuke Nojiri | 5799097 | 2014-07-15 19:47:32 -0700 | [diff] [blame] | 110 | * error code if error. Waits for the self test to complete if needed. */ |
Lee Leahy | b2d834a | 2017-03-08 16:52:22 -0800 | [diff] [blame] | 111 | uint32_t tlcl_send_receive(const uint8_t *request, uint8_t *response, |
Lee Leahy | 342f8d6 | 2017-03-10 15:31:56 -0800 | [diff] [blame] | 112 | int max_length) |
| 113 | { |
Daisuke Nojiri | 5799097 | 2014-07-15 19:47:32 -0700 | [diff] [blame] | 114 | uint32_t result = tlcl_send_receive_no_retry(request, response, |
| 115 | max_length); |
| 116 | /* If the command fails because the self test has not completed, try it |
| 117 | * again after attempting to ensure that the self test has completed. */ |
| 118 | if (result == TPM_E_NEEDS_SELFTEST || result == TPM_E_DOING_SELFTEST) { |
| 119 | result = tlcl_continue_self_test(); |
| 120 | if (result != TPM_SUCCESS) |
| 121 | return result; |
Daisuke Nojiri | efb5cde | 2014-07-02 08:37:23 -0700 | [diff] [blame] | 122 | #if defined(TPM_BLOCKING_CONTINUESELFTEST) || defined(VB_RECOVERY_MODE) |
Daisuke Nojiri | 5799097 | 2014-07-15 19:47:32 -0700 | [diff] [blame] | 123 | /* Retry only once */ |
| 124 | result = tlcl_send_receive_no_retry(request, response, |
Lee Leahy | e20a319 | 2017-03-09 16:21:34 -0800 | [diff] [blame] | 125 | max_length); |
Daisuke Nojiri | efb5cde | 2014-07-02 08:37:23 -0700 | [diff] [blame] | 126 | #else |
Daisuke Nojiri | 5799097 | 2014-07-15 19:47:32 -0700 | [diff] [blame] | 127 | /* This needs serious testing. The TPM specification says: "iii. |
| 128 | * The caller MUST wait for the actions of TPM_ContinueSelfTest |
| 129 | * to complete before reissuing the command C1." But, if |
| 130 | * ContinueSelfTest is non-blocking, how do we know that the |
| 131 | * actions have completed other than trying again? */ |
| 132 | do { |
| 133 | result = tlcl_send_receive_no_retry(request, response, |
Lee Leahy | e20a319 | 2017-03-09 16:21:34 -0800 | [diff] [blame] | 134 | max_length); |
Daisuke Nojiri | 5799097 | 2014-07-15 19:47:32 -0700 | [diff] [blame] | 135 | } while (result == TPM_E_DOING_SELFTEST); |
Daisuke Nojiri | efb5cde | 2014-07-02 08:37:23 -0700 | [diff] [blame] | 136 | #endif |
Daisuke Nojiri | 5799097 | 2014-07-15 19:47:32 -0700 | [diff] [blame] | 137 | } |
| 138 | return result; |
Daisuke Nojiri | efb5cde | 2014-07-02 08:37:23 -0700 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | /* Sends a command and returns the error code. */ |
Lee Leahy | 342f8d6 | 2017-03-10 15:31:56 -0800 | [diff] [blame] | 142 | static uint32_t send(const uint8_t *command) |
| 143 | { |
Daisuke Nojiri | 5799097 | 2014-07-15 19:47:32 -0700 | [diff] [blame] | 144 | uint8_t response[TPM_LARGE_ENOUGH_COMMAND_SIZE]; |
| 145 | return tlcl_send_receive(command, response, sizeof(response)); |
Daisuke Nojiri | efb5cde | 2014-07-02 08:37:23 -0700 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | /* Exported functions. */ |
| 149 | |
Furquan Shaikh | 8b5d04e | 2016-11-10 09:49:05 -0800 | [diff] [blame] | 150 | static uint8_t tlcl_init_done CAR_GLOBAL; |
| 151 | |
Lee Leahy | 342f8d6 | 2017-03-10 15:31:56 -0800 | [diff] [blame] | 152 | uint32_t tlcl_lib_init(void) |
| 153 | { |
Furquan Shaikh | 8b5d04e | 2016-11-10 09:49:05 -0800 | [diff] [blame] | 154 | uint8_t done = car_get_var(tlcl_init_done); |
| 155 | if (done) |
| 156 | return VB2_SUCCESS; |
| 157 | |
Daisuke Nojiri | 5799097 | 2014-07-15 19:47:32 -0700 | [diff] [blame] | 158 | if (tis_init()) |
| 159 | return VB2_ERROR_UNKNOWN; |
| 160 | if (tis_open()) |
| 161 | return VB2_ERROR_UNKNOWN; |
Furquan Shaikh | 8b5d04e | 2016-11-10 09:49:05 -0800 | [diff] [blame] | 162 | |
| 163 | car_set_var(tlcl_init_done, 1); |
| 164 | |
Daisuke Nojiri | 5799097 | 2014-07-15 19:47:32 -0700 | [diff] [blame] | 165 | return VB2_SUCCESS; |
Daisuke Nojiri | efb5cde | 2014-07-02 08:37:23 -0700 | [diff] [blame] | 166 | } |
| 167 | |
Lee Leahy | 342f8d6 | 2017-03-10 15:31:56 -0800 | [diff] [blame] | 168 | uint32_t tlcl_startup(void) |
| 169 | { |
Daisuke Nojiri | 5799097 | 2014-07-15 19:47:32 -0700 | [diff] [blame] | 170 | VBDEBUG("TPM: Startup\n"); |
| 171 | return send(tpm_startup_cmd.buffer); |
Daisuke Nojiri | efb5cde | 2014-07-02 08:37:23 -0700 | [diff] [blame] | 172 | } |
| 173 | |
Lee Leahy | 342f8d6 | 2017-03-10 15:31:56 -0800 | [diff] [blame] | 174 | uint32_t tlcl_resume(void) |
| 175 | { |
Lee Leahy | e20a319 | 2017-03-09 16:21:34 -0800 | [diff] [blame] | 176 | VBDEBUG("TPM: Resume\n"); |
| 177 | return send(tpm_resume_cmd.buffer); |
Daisuke Nojiri | efb5cde | 2014-07-02 08:37:23 -0700 | [diff] [blame] | 178 | } |
| 179 | |
Daisuke Nojiri | 5799097 | 2014-07-15 19:47:32 -0700 | [diff] [blame] | 180 | uint32_t tlcl_self_test_full(void) |
| 181 | { |
| 182 | VBDEBUG("TPM: Self test full\n"); |
| 183 | return send(tpm_selftestfull_cmd.buffer); |
Daisuke Nojiri | efb5cde | 2014-07-02 08:37:23 -0700 | [diff] [blame] | 184 | } |
| 185 | |
Daisuke Nojiri | 5799097 | 2014-07-15 19:47:32 -0700 | [diff] [blame] | 186 | uint32_t tlcl_continue_self_test(void) |
| 187 | { |
| 188 | uint8_t response[TPM_LARGE_ENOUGH_COMMAND_SIZE]; |
| 189 | VBDEBUG("TPM: Continue self test\n"); |
| 190 | /* Call the No Retry version of SendReceive to avoid recursion. */ |
| 191 | return tlcl_send_receive_no_retry(tpm_continueselftest_cmd.buffer, |
Lee Leahy | e20a319 | 2017-03-09 16:21:34 -0800 | [diff] [blame] | 192 | response, sizeof(response)); |
Daisuke Nojiri | efb5cde | 2014-07-02 08:37:23 -0700 | [diff] [blame] | 193 | } |
| 194 | |
Daisuke Nojiri | 5799097 | 2014-07-15 19:47:32 -0700 | [diff] [blame] | 195 | uint32_t tlcl_define_space(uint32_t index, uint32_t perm, uint32_t size) |
| 196 | { |
| 197 | struct s_tpm_nv_definespace_cmd cmd; |
| 198 | VBDEBUG("TPM: TlclDefineSpace(0x%x, 0x%x, %d)\n", index, perm, size); |
| 199 | memcpy(&cmd, &tpm_nv_definespace_cmd, sizeof(cmd)); |
| 200 | to_tpm_uint32(cmd.buffer + tpm_nv_definespace_cmd.index, index); |
| 201 | to_tpm_uint32(cmd.buffer + tpm_nv_definespace_cmd.perm, perm); |
| 202 | to_tpm_uint32(cmd.buffer + tpm_nv_definespace_cmd.size, size); |
| 203 | return send(cmd.buffer); |
Daisuke Nojiri | efb5cde | 2014-07-02 08:37:23 -0700 | [diff] [blame] | 204 | } |
| 205 | |
Lee Leahy | b2d834a | 2017-03-08 16:52:22 -0800 | [diff] [blame] | 206 | uint32_t tlcl_write(uint32_t index, const void *data, uint32_t length) |
Daisuke Nojiri | 5799097 | 2014-07-15 19:47:32 -0700 | [diff] [blame] | 207 | { |
| 208 | struct s_tpm_nv_write_cmd cmd; |
| 209 | uint8_t response[TPM_LARGE_ENOUGH_COMMAND_SIZE]; |
| 210 | const int total_length = |
| 211 | kTpmRequestHeaderLength + kWriteInfoLength + length; |
| 212 | |
| 213 | VBDEBUG("TPM: tlcl_write(0x%x, %d)\n", index, length); |
| 214 | memcpy(&cmd, &tpm_nv_write_cmd, sizeof(cmd)); |
| 215 | assert(total_length <= TPM_LARGE_ENOUGH_COMMAND_SIZE); |
| 216 | set_tpm_command_size(cmd.buffer, total_length); |
| 217 | |
| 218 | to_tpm_uint32(cmd.buffer + tpm_nv_write_cmd.index, index); |
| 219 | to_tpm_uint32(cmd.buffer + tpm_nv_write_cmd.length, length); |
| 220 | memcpy(cmd.buffer + tpm_nv_write_cmd.data, data, length); |
| 221 | |
| 222 | return tlcl_send_receive(cmd.buffer, response, sizeof(response)); |
Daisuke Nojiri | efb5cde | 2014-07-02 08:37:23 -0700 | [diff] [blame] | 223 | } |
| 224 | |
Lee Leahy | b2d834a | 2017-03-08 16:52:22 -0800 | [diff] [blame] | 225 | uint32_t tlcl_read(uint32_t index, void *data, uint32_t length) |
Daisuke Nojiri | 5799097 | 2014-07-15 19:47:32 -0700 | [diff] [blame] | 226 | { |
| 227 | struct s_tpm_nv_read_cmd cmd; |
| 228 | uint8_t response[TPM_LARGE_ENOUGH_COMMAND_SIZE]; |
| 229 | uint32_t result_length; |
| 230 | uint32_t result; |
| 231 | |
| 232 | VBDEBUG("TPM: tlcl_read(0x%x, %d)\n", index, length); |
| 233 | memcpy(&cmd, &tpm_nv_read_cmd, sizeof(cmd)); |
| 234 | to_tpm_uint32(cmd.buffer + tpm_nv_read_cmd.index, index); |
| 235 | to_tpm_uint32(cmd.buffer + tpm_nv_read_cmd.length, length); |
| 236 | |
| 237 | result = tlcl_send_receive(cmd.buffer, response, sizeof(response)); |
| 238 | if (result == TPM_SUCCESS && length > 0) { |
Lee Leahy | b2d834a | 2017-03-08 16:52:22 -0800 | [diff] [blame] | 239 | uint8_t *nv_read_cursor = response + kTpmResponseHeaderLength; |
Daisuke Nojiri | 5799097 | 2014-07-15 19:47:32 -0700 | [diff] [blame] | 240 | from_tpm_uint32(nv_read_cursor, &result_length); |
| 241 | nv_read_cursor += sizeof(uint32_t); |
| 242 | memcpy(data, nv_read_cursor, result_length); |
| 243 | } |
| 244 | |
| 245 | return result; |
Daisuke Nojiri | efb5cde | 2014-07-02 08:37:23 -0700 | [diff] [blame] | 246 | } |
| 247 | |
Daisuke Nojiri | efb5cde | 2014-07-02 08:37:23 -0700 | [diff] [blame] | 248 | |
Lee Leahy | 342f8d6 | 2017-03-10 15:31:56 -0800 | [diff] [blame] | 249 | uint32_t tlcl_assert_physical_presence(void) |
| 250 | { |
Daisuke Nojiri | 5799097 | 2014-07-15 19:47:32 -0700 | [diff] [blame] | 251 | VBDEBUG("TPM: Asserting physical presence\n"); |
| 252 | return send(tpm_ppassert_cmd.buffer); |
Daisuke Nojiri | efb5cde | 2014-07-02 08:37:23 -0700 | [diff] [blame] | 253 | } |
| 254 | |
Lee Leahy | 342f8d6 | 2017-03-10 15:31:56 -0800 | [diff] [blame] | 255 | uint32_t tlcl_physical_presence_cmd_enable(void) |
| 256 | { |
Daisuke Nojiri | 5799097 | 2014-07-15 19:47:32 -0700 | [diff] [blame] | 257 | VBDEBUG("TPM: Enable the physical presence command\n"); |
| 258 | return send(tpm_ppenable_cmd.buffer); |
Daisuke Nojiri | efb5cde | 2014-07-02 08:37:23 -0700 | [diff] [blame] | 259 | } |
| 260 | |
Lee Leahy | 342f8d6 | 2017-03-10 15:31:56 -0800 | [diff] [blame] | 261 | uint32_t tlcl_finalize_physical_presence(void) |
| 262 | { |
Daisuke Nojiri | 5799097 | 2014-07-15 19:47:32 -0700 | [diff] [blame] | 263 | VBDEBUG("TPM: Enable PP cmd, disable HW pp, and set lifetime lock\n"); |
| 264 | return send(tpm_finalizepp_cmd.buffer); |
Daisuke Nojiri | efb5cde | 2014-07-02 08:37:23 -0700 | [diff] [blame] | 265 | } |
| 266 | |
Lee Leahy | 342f8d6 | 2017-03-10 15:31:56 -0800 | [diff] [blame] | 267 | uint32_t tlcl_set_nv_locked(void) |
| 268 | { |
Daisuke Nojiri | 5799097 | 2014-07-15 19:47:32 -0700 | [diff] [blame] | 269 | VBDEBUG("TPM: Set NV locked\n"); |
| 270 | return tlcl_define_space(TPM_NV_INDEX_LOCK, 0, 0); |
Daisuke Nojiri | efb5cde | 2014-07-02 08:37:23 -0700 | [diff] [blame] | 271 | } |
| 272 | |
Lee Leahy | 342f8d6 | 2017-03-10 15:31:56 -0800 | [diff] [blame] | 273 | uint32_t tlcl_force_clear(void) |
| 274 | { |
Daisuke Nojiri | 5799097 | 2014-07-15 19:47:32 -0700 | [diff] [blame] | 275 | VBDEBUG("TPM: Force clear\n"); |
| 276 | return send(tpm_forceclear_cmd.buffer); |
Daisuke Nojiri | efb5cde | 2014-07-02 08:37:23 -0700 | [diff] [blame] | 277 | } |
| 278 | |
Lee Leahy | 342f8d6 | 2017-03-10 15:31:56 -0800 | [diff] [blame] | 279 | uint32_t tlcl_set_enable(void) |
| 280 | { |
Daisuke Nojiri | 5799097 | 2014-07-15 19:47:32 -0700 | [diff] [blame] | 281 | VBDEBUG("TPM: Enabling TPM\n"); |
| 282 | return send(tpm_physicalenable_cmd.buffer); |
Daisuke Nojiri | efb5cde | 2014-07-02 08:37:23 -0700 | [diff] [blame] | 283 | } |
| 284 | |
Daisuke Nojiri | 5799097 | 2014-07-15 19:47:32 -0700 | [diff] [blame] | 285 | uint32_t tlcl_set_deactivated(uint8_t flag) |
| 286 | { |
| 287 | struct s_tpm_physicalsetdeactivated_cmd cmd; |
| 288 | VBDEBUG("TPM: SetDeactivated(%d)\n", flag); |
| 289 | memcpy(&cmd, &tpm_physicalsetdeactivated_cmd, sizeof(cmd)); |
| 290 | *(cmd.buffer + cmd.deactivated) = flag; |
| 291 | return send(cmd.buffer); |
Daisuke Nojiri | efb5cde | 2014-07-02 08:37:23 -0700 | [diff] [blame] | 292 | } |
| 293 | |
Lee Leahy | b2d834a | 2017-03-08 16:52:22 -0800 | [diff] [blame] | 294 | uint32_t tlcl_get_permanent_flags(TPM_PERMANENT_FLAGS *pflags) |
Daisuke Nojiri | 5799097 | 2014-07-15 19:47:32 -0700 | [diff] [blame] | 295 | { |
| 296 | uint8_t response[TPM_LARGE_ENOUGH_COMMAND_SIZE]; |
| 297 | uint32_t size; |
| 298 | uint32_t result = tlcl_send_receive(tpm_getflags_cmd.buffer, response, |
Lee Leahy | e20a319 | 2017-03-09 16:21:34 -0800 | [diff] [blame] | 299 | sizeof(response)); |
Daisuke Nojiri | 5799097 | 2014-07-15 19:47:32 -0700 | [diff] [blame] | 300 | if (result != TPM_SUCCESS) |
| 301 | return result; |
| 302 | from_tpm_uint32(response + kTpmResponseHeaderLength, &size); |
| 303 | assert(size == sizeof(TPM_PERMANENT_FLAGS)); |
| 304 | memcpy(pflags, response + kTpmResponseHeaderLength + sizeof(size), |
| 305 | sizeof(TPM_PERMANENT_FLAGS)); |
| 306 | return result; |
Daisuke Nojiri | efb5cde | 2014-07-02 08:37:23 -0700 | [diff] [blame] | 307 | } |
| 308 | |
Lee Leahy | b2d834a | 2017-03-08 16:52:22 -0800 | [diff] [blame] | 309 | uint32_t tlcl_get_flags(uint8_t *disable, uint8_t *deactivated, |
Lee Leahy | e20a319 | 2017-03-09 16:21:34 -0800 | [diff] [blame] | 310 | uint8_t *nvlocked) |
Daisuke Nojiri | 5799097 | 2014-07-15 19:47:32 -0700 | [diff] [blame] | 311 | { |
| 312 | TPM_PERMANENT_FLAGS pflags; |
| 313 | uint32_t result = tlcl_get_permanent_flags(&pflags); |
| 314 | if (result == TPM_SUCCESS) { |
| 315 | if (disable) |
| 316 | *disable = pflags.disable; |
| 317 | if (deactivated) |
| 318 | *deactivated = pflags.deactivated; |
| 319 | if (nvlocked) |
| 320 | *nvlocked = pflags.nvLocked; |
| 321 | VBDEBUG("TPM: flags disable=%d, deactivated=%d, nvlocked=%d\n", |
| 322 | pflags.disable, pflags.deactivated, pflags.nvLocked); |
| 323 | } |
| 324 | return result; |
Daisuke Nojiri | efb5cde | 2014-07-02 08:37:23 -0700 | [diff] [blame] | 325 | } |
| 326 | |
Daisuke Nojiri | 5799097 | 2014-07-15 19:47:32 -0700 | [diff] [blame] | 327 | uint32_t tlcl_set_global_lock(void) |
| 328 | { |
| 329 | uint32_t x; |
| 330 | VBDEBUG("TPM: Set global lock\n"); |
Lee Leahy | b2d834a | 2017-03-08 16:52:22 -0800 | [diff] [blame] | 331 | return tlcl_write(TPM_NV_INDEX0, (uint8_t *) &x, 0); |
Daisuke Nojiri | efb5cde | 2014-07-02 08:37:23 -0700 | [diff] [blame] | 332 | } |
| 333 | |
Lee Leahy | b2d834a | 2017-03-08 16:52:22 -0800 | [diff] [blame] | 334 | uint32_t tlcl_extend(int pcr_num, const uint8_t *in_digest, |
Lee Leahy | e20a319 | 2017-03-09 16:21:34 -0800 | [diff] [blame] | 335 | uint8_t *out_digest) |
Daisuke Nojiri | 5799097 | 2014-07-15 19:47:32 -0700 | [diff] [blame] | 336 | { |
| 337 | struct s_tpm_extend_cmd cmd; |
| 338 | uint8_t response[kTpmResponseHeaderLength + kPcrDigestLength]; |
| 339 | uint32_t result; |
Daisuke Nojiri | efb5cde | 2014-07-02 08:37:23 -0700 | [diff] [blame] | 340 | |
Daisuke Nojiri | 5799097 | 2014-07-15 19:47:32 -0700 | [diff] [blame] | 341 | memcpy(&cmd, &tpm_extend_cmd, sizeof(cmd)); |
| 342 | to_tpm_uint32(cmd.buffer + tpm_extend_cmd.pcrNum, pcr_num); |
| 343 | memcpy(cmd.buffer + cmd.inDigest, in_digest, kPcrDigestLength); |
Daisuke Nojiri | efb5cde | 2014-07-02 08:37:23 -0700 | [diff] [blame] | 344 | |
Daisuke Nojiri | 5799097 | 2014-07-15 19:47:32 -0700 | [diff] [blame] | 345 | result = tlcl_send_receive(cmd.buffer, response, sizeof(response)); |
| 346 | if (result != TPM_SUCCESS) |
| 347 | return result; |
Daisuke Nojiri | efb5cde | 2014-07-02 08:37:23 -0700 | [diff] [blame] | 348 | |
Julius Werner | 76e3303 | 2015-01-30 18:45:27 -0800 | [diff] [blame] | 349 | if (out_digest) |
| 350 | memcpy(out_digest, response + kTpmResponseHeaderLength, |
| 351 | kPcrDigestLength); |
Daisuke Nojiri | 5799097 | 2014-07-15 19:47:32 -0700 | [diff] [blame] | 352 | return result; |
Daisuke Nojiri | efb5cde | 2014-07-02 08:37:23 -0700 | [diff] [blame] | 353 | } |