| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
#include <assert.h> |
| 16 |
#include <string.h> |
| 17 |
#include <security/tpm/tis.h> |
| 18 |
#include <vb2_api.h> |
| 19 |
#include <security/tpm/tss.h> |
| 20 |
|
| 21 |
#include "tss_internal.h" |
| 22 |
#include "tss_commands.h" |
| 23 |
|
| 24 |
#include <console/console.h> |
| 25 |
#define VBDEBUG(format, args...) printk(BIOS_DEBUG, format, ## args) |
| 26 |
|
| 27 |
static tpm_result_t tpm_send_receive(const uint8_t *request, |
| 28 |
uint32_t request_length, |
| 29 |
uint8_t *response, |
| 30 |
uint32_t *response_length) |
| 31 |
{ |
| 32 |
size_t len = *response_length; |
| 33 |
tpm_result_t rc; |
| 34 |
|
| 35 |
if (tlcl_tis_sendrecv == NULL) { |
| 36 |
printk(BIOS_ERR, "Attempted use of uninitialized TSS 1.2 stack\n"); |
| 37 |
return TPM_FAIL; |
| 38 |
} |
| 39 |
|
| 40 |
rc = tlcl_tis_sendrecv(request, request_length, response, &len); |
| 41 |
if (rc) |
| 42 |
return rc; |
| 43 |
|
| 44 |
if (len > *response_length) |
| 45 |
rc = TPM_CB_FAIL; |
| 46 |
else |
| 47 |
*response_length = len; |
| 48 |
return rc; |
| 49 |
} |
| 50 |
|
| 51 |
|
| 52 |
static inline void set_tpm_command_size(uint8_t *buffer, uint32_t size) |
| 53 |
{ |
| 54 |
to_tpm_uint32(buffer + sizeof(uint16_t), size); |
| 55 |
} |
| 56 |
|
| 57 |
|
| 58 |
__attribute__((unused)) |
| 59 |
static inline int tpm_command_size(const uint8_t *buffer) |
| 60 |
{ |
| 61 |
uint32_t size; |
| 62 |
from_tpm_uint32(buffer + sizeof(uint16_t), &size); |
| 63 |
return (int)size; |
| 64 |
} |
| 65 |
|
| 66 |
|
| 67 |
static inline tpm_result_t tpm_command_code(const uint8_t *buffer) |
| 68 |
{ |
| 69 |
tpm_result_t rc; |
| 70 |
from_tpm_uint32(buffer + sizeof(uint16_t) + sizeof(uint32_t), &rc); |
| 71 |
return rc; |
| 72 |
} |
| 73 |
|
| 74 |
|
| 75 |
static inline tpm_result_t tpm_return_code(const uint8_t *buffer) |
| 76 |
{ |
| 77 |
return tpm_command_code(buffer); |
| 78 |
} |
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
static tpm_result_t tlcl1_send_receive_no_retry(const uint8_t *request, |
| 85 |
uint8_t *response, int max_length) |
| 86 |
{ |
| 87 |
uint32_t response_length = max_length; |
| 88 |
tpm_result_t rc; |
| 89 |
|
| 90 |
rc = tpm_send_receive(request, tpm_command_size(request), |
| 91 |
response, &response_length); |
| 92 |
if (rc != TPM_SUCCESS) { |
| 93 |
|
| 94 |
VBDEBUG("TPM: command %#x send/receive failed: %#x\n", |
| 95 |
tpm_command_code(request), rc); |
| 96 |
return rc; |
| 97 |
} |
| 98 |
|
| 99 |
rc = tpm_return_code(response); |
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
VBDEBUG("TPM: command %#x returned %#x\n", |
| 106 |
tpm_command_code(request), rc); |
| 107 |
|
| 108 |
return rc; |
| 109 |
} |
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
tpm_result_t tlcl1_send_receive(const uint8_t *request, uint8_t *response, int max_length) |
| 114 |
{ |
| 115 |
tpm_result_t rc = tlcl1_send_receive_no_retry(request, response, max_length); |
| 116 |
|
| 117 |
|
| 118 |
if (rc == TPM_NEEDS_SELFTEST || rc == TPM_DOING_SELFTEST) { |
| 119 |
rc = tlcl1_continue_self_test(); |
| 120 |
if (rc != TPM_SUCCESS) |
| 121 |
return rc; |
| 122 |
#if defined(TPM_BLOCKING_CONTINUESELFTEST) || defined(VB_RECOVERY_MODE) |
| 123 |
|
| 124 |
rc = tlcl1_send_receive_no_retry(request, response, max_length); |
| 125 |
#else |
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
do { |
| 132 |
rc = tlcl1_send_receive_no_retry(request, response, max_length); |
| 133 |
} while (rc == TPM_DOING_SELFTEST); |
| 134 |
#endif |
| 135 |
} |
| 136 |
return rc; |
| 137 |
} |
| 138 |
|
| 139 |
|
| 140 |
static tpm_result_t send(const uint8_t *command) |
| 141 |
{ |
| 142 |
uint8_t response[TPM_LARGE_ENOUGH_COMMAND_SIZE]; |
| 143 |
return tlcl1_send_receive(command, response, sizeof(response)); |
| 144 |
} |
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
tpm_result_t tlcl1_startup(void) |
| 149 |
{ |
| 150 |
VBDEBUG("TPM: Startup\n"); |
| 151 |
return send(tpm_startup_cmd.buffer); |
| 152 |
} |
| 153 |
|
| 154 |
tpm_result_t tlcl1_resume(void) |
| 155 |
{ |
| 156 |
VBDEBUG("TPM: Resume\n"); |
| 157 |
return send(tpm_resume_cmd.buffer); |
| 158 |
} |
| 159 |
|
| 160 |
tpm_result_t tlcl1_save_state(void) |
| 161 |
{ |
| 162 |
VBDEBUG("TPM: Save state\n"); |
| 163 |
return send(tpm_savestate_cmd.buffer); |
| 164 |
} |
| 165 |
|
| 166 |
tpm_result_t tlcl1_self_test_full(void) |
| 167 |
{ |
| 168 |
VBDEBUG("TPM: Self test full\n"); |
| 169 |
return send(tpm_selftestfull_cmd.buffer); |
| 170 |
} |
| 171 |
|
| 172 |
tpm_result_t tlcl1_continue_self_test(void) |
| 173 |
{ |
| 174 |
uint8_t response[TPM_LARGE_ENOUGH_COMMAND_SIZE]; |
| 175 |
VBDEBUG("TPM: Continue self test\n"); |
| 176 |
|
| 177 |
return tlcl1_send_receive_no_retry(tpm_continueselftest_cmd.buffer, |
| 178 |
response, sizeof(response)); |
| 179 |
} |
| 180 |
|
| 181 |
tpm_result_t tlcl1_define_space(uint32_t index, uint32_t perm, uint32_t size) |
| 182 |
{ |
| 183 |
struct s_tpm_nv_definespace_cmd cmd; |
| 184 |
VBDEBUG("TPM: TlclDefineSpace(%#x, %#x, %d)\n", index, perm, size); |
| 185 |
memcpy(&cmd, &tpm_nv_definespace_cmd, sizeof(cmd)); |
| 186 |
to_tpm_uint32(cmd.buffer + tpm_nv_definespace_cmd.index, index); |
| 187 |
to_tpm_uint32(cmd.buffer + tpm_nv_definespace_cmd.perm, perm); |
| 188 |
to_tpm_uint32(cmd.buffer + tpm_nv_definespace_cmd.size, size); |
| 189 |
return send(cmd.buffer); |
| 190 |
} |
| 191 |
|
| 192 |
tpm_result_t tlcl1_write(uint32_t index, const void *data, uint32_t length) |
| 193 |
{ |
| 194 |
struct s_tpm_nv_write_cmd cmd; |
| 195 |
uint8_t response[TPM_LARGE_ENOUGH_COMMAND_SIZE]; |
| 196 |
const int total_length = |
| 197 |
kTpmRequestHeaderLength + kWriteInfoLength + length; |
| 198 |
|
| 199 |
VBDEBUG("TPM: %s(%#x, %d)\n", __func__, index, length); |
| 200 |
memcpy(&cmd, &tpm_nv_write_cmd, sizeof(cmd)); |
| 201 |
assert(total_length <= TPM_LARGE_ENOUGH_COMMAND_SIZE); |
| 202 |
set_tpm_command_size(cmd.buffer, total_length); |
| 203 |
|
| 204 |
to_tpm_uint32(cmd.buffer + tpm_nv_write_cmd.index, index); |
| 205 |
to_tpm_uint32(cmd.buffer + tpm_nv_write_cmd.length, length); |
| 206 |
if (length > 0) |
| 207 |
memcpy(cmd.buffer + tpm_nv_write_cmd.data, data, length); |
| 208 |
|
| 209 |
return tlcl1_send_receive(cmd.buffer, response, sizeof(response)); |
| 210 |
} |
| 211 |
|
| 212 |
tpm_result_t tlcl1_read(uint32_t index, void *data, uint32_t length) |
| 213 |
{ |
| 214 |
struct s_tpm_nv_read_cmd cmd; |
| 215 |
uint8_t response[TPM_LARGE_ENOUGH_COMMAND_SIZE]; |
| 216 |
uint32_t result_length; |
| 217 |
tpm_result_t rc; |
| 218 |
|
| 219 |
VBDEBUG("TPM: %s(%#x, %d)\n", __func__, index, length); |
| 220 |
memcpy(&cmd, &tpm_nv_read_cmd, sizeof(cmd)); |
| 221 |
to_tpm_uint32(cmd.buffer + tpm_nv_read_cmd.index, index); |
| 222 |
to_tpm_uint32(cmd.buffer + tpm_nv_read_cmd.length, length); |
| 223 |
|
| 224 |
rc = tlcl1_send_receive(cmd.buffer, response, sizeof(response)); |
| 225 |
if (rc == TPM_SUCCESS && length > 0) { |
| 226 |
uint8_t *nv_read_cursor = response + kTpmResponseHeaderLength; |
| 227 |
from_tpm_uint32(nv_read_cursor, &result_length); |
| 228 |
if (result_length > length) |
| 229 |
return TPM_IOERROR; |
| 230 |
nv_read_cursor += sizeof(uint32_t); |
| 231 |
memcpy(data, nv_read_cursor, result_length); |
| 232 |
} |
| 233 |
|
| 234 |
return rc; |
| 235 |
} |
| 236 |
|
| 237 |
tpm_result_t tlcl1_assert_physical_presence(void) |
| 238 |
{ |
| 239 |
VBDEBUG("TPM: Asserting physical presence\n"); |
| 240 |
return send(tpm_ppassert_cmd.buffer); |
| 241 |
} |
| 242 |
|
| 243 |
tpm_result_t tlcl1_physical_presence_cmd_enable(void) |
| 244 |
{ |
| 245 |
VBDEBUG("TPM: Enable the physical presence command\n"); |
| 246 |
return send(tpm_ppenable_cmd.buffer); |
| 247 |
} |
| 248 |
|
| 249 |
tpm_result_t tlcl1_finalize_physical_presence(void) |
| 250 |
{ |
| 251 |
VBDEBUG("TPM: Enable PP cmd, disable HW pp, and set lifetime lock\n"); |
| 252 |
return send(tpm_finalizepp_cmd.buffer); |
| 253 |
} |
| 254 |
|
| 255 |
tpm_result_t tlcl1_set_nv_locked(void) |
| 256 |
{ |
| 257 |
VBDEBUG("TPM: Set NV locked\n"); |
| 258 |
return tlcl1_define_space(TPM_NV_INDEX_LOCK, 0, 0); |
| 259 |
} |
| 260 |
|
| 261 |
tpm_result_t tlcl1_force_clear(void) |
| 262 |
{ |
| 263 |
VBDEBUG("TPM: Force clear\n"); |
| 264 |
return send(tpm_forceclear_cmd.buffer); |
| 265 |
} |
| 266 |
|
| 267 |
tpm_result_t tlcl1_set_enable(void) |
| 268 |
{ |
| 269 |
VBDEBUG("TPM: Enabling TPM\n"); |
| 270 |
return send(tpm_physicalenable_cmd.buffer); |
| 271 |
} |
| 272 |
|
| 273 |
tpm_result_t tlcl1_set_deactivated(uint8_t flag) |
| 274 |
{ |
| 275 |
struct s_tpm_physicalsetdeactivated_cmd cmd; |
| 276 |
VBDEBUG("TPM: SetDeactivated(%d)\n", flag); |
| 277 |
memcpy(&cmd, &tpm_physicalsetdeactivated_cmd, sizeof(cmd)); |
| 278 |
*(cmd.buffer + cmd.deactivated) = flag; |
| 279 |
return send(cmd.buffer); |
| 280 |
} |
| 281 |
|
| 282 |
tpm_result_t tlcl1_get_permanent_flags(TPM_PERMANENT_FLAGS *pflags) |
| 283 |
{ |
| 284 |
uint8_t response[TPM_LARGE_ENOUGH_COMMAND_SIZE]; |
| 285 |
uint32_t size; |
| 286 |
tpm_result_t rc = |
| 287 |
tlcl1_send_receive(tpm_getflags_cmd.buffer, response, sizeof(response)); |
| 288 |
if (rc != TPM_SUCCESS) |
| 289 |
return rc; |
| 290 |
from_tpm_uint32(response + kTpmResponseHeaderLength, &size); |
| 291 |
if (size != sizeof(TPM_PERMANENT_FLAGS)) |
| 292 |
return TPM_IOERROR; |
| 293 |
memcpy(pflags, response + kTpmResponseHeaderLength + sizeof(size), |
| 294 |
sizeof(TPM_PERMANENT_FLAGS)); |
| 295 |
return rc; |
| 296 |
} |
| 297 |
|
| 298 |
tpm_result_t tlcl1_get_flags(uint8_t *disable, uint8_t *deactivated, uint8_t *nvlocked) |
| 299 |
{ |
| 300 |
TPM_PERMANENT_FLAGS pflags; |
| 301 |
tpm_result_t rc = tlcl1_get_permanent_flags(&pflags); |
| 302 |
if (rc == TPM_SUCCESS) { |
| 303 |
if (disable) |
| 304 |
*disable = pflags.disable; |
| 305 |
if (deactivated) |
| 306 |
*deactivated = pflags.deactivated; |
| 307 |
if (nvlocked) |
| 308 |
*nvlocked = pflags.nvLocked; |
| 309 |
VBDEBUG("TPM: flags disable=%d, deactivated=%d, nvlocked=%d\n", |
| 310 |
pflags.disable, pflags.deactivated, pflags.nvLocked); |
| 311 |
} |
| 312 |
return rc; |
| 313 |
} |
| 314 |
|
| 315 |
tpm_result_t tlcl1_set_global_lock(void) |
| 316 |
{ |
| 317 |
VBDEBUG("TPM: Set global lock\n"); |
| 318 |
return tlcl1_write(TPM_NV_INDEX0, NULL, 0); |
| 319 |
} |
| 320 |
|
| 321 |
tpm_result_t tlcl1_extend(int pcr_num, const uint8_t *digest_data, |
| 322 |
enum vb2_hash_algorithm digest_algo) |
| 323 |
{ |
| 324 |
struct s_tpm_extend_cmd cmd; |
| 325 |
uint8_t response[kTpmResponseHeaderLength + kPcrDigestLength]; |
| 326 |
|
| 327 |
if (digest_algo != VB2_HASH_SHA1) |
| 328 |
return TPM_CB_INVALID_ARG; |
| 329 |
|
| 330 |
memcpy(&cmd, &tpm_extend_cmd, sizeof(cmd)); |
| 331 |
to_tpm_uint32(cmd.buffer + tpm_extend_cmd.pcrNum, pcr_num); |
| 332 |
memcpy(cmd.buffer + cmd.inDigest, digest_data, kPcrDigestLength); |
| 333 |
|
| 334 |
return tlcl1_send_receive(cmd.buffer, response, sizeof(response)); |
| 335 |
} |
| 336 |
|
| 337 |
tpm_result_t tlcl1_get_permissions(uint32_t index, uint32_t *permissions) |
| 338 |
{ |
| 339 |
struct s_tpm_getpermissions_cmd cmd; |
| 340 |
uint8_t response[TPM_LARGE_ENOUGH_COMMAND_SIZE]; |
| 341 |
uint8_t *nvdata; |
| 342 |
tpm_result_t rc; |
| 343 |
uint32_t size; |
| 344 |
|
| 345 |
memcpy(&cmd, &tpm_getpermissions_cmd, sizeof(cmd)); |
| 346 |
to_tpm_uint32(cmd.buffer + tpm_getpermissions_cmd.index, index); |
| 347 |
rc = tlcl1_send_receive(cmd.buffer, response, sizeof(response)); |
| 348 |
if (rc != TPM_SUCCESS) |
| 349 |
return rc; |
| 350 |
|
| 351 |
nvdata = response + kTpmResponseHeaderLength + sizeof(size); |
| 352 |
from_tpm_uint32(nvdata + kNvDataPublicPermissionsOffset, permissions); |
| 353 |
return rc; |
| 354 |
} |