Vadim Bendebury | 627afc2 | 2016-06-19 12:13:18 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016 The Chromium OS Authors. All rights reserved. |
| 3 | * Use of this source code is governed by a BSD-style license that can be |
| 4 | * found in the LICENSE file. |
| 5 | */ |
| 6 | |
| 7 | #include <commonlib/endian.h> |
| 8 | #include <console/console.h> |
| 9 | #include <stdlib.h> |
| 10 | #include <string.h> |
| 11 | |
| 12 | #include "tpm2_marshaling.h" |
| 13 | |
| 14 | static uint16_t tpm_tag; /* Depends on the command type. */ |
| 15 | |
| 16 | /* |
| 17 | * Each unmarshaling function receives a pointer to the buffer pointer and a |
| 18 | * pointer to the size of data still in the buffer. The function extracts data |
| 19 | * from the buffer and adjusts both buffer pointer and remaining data size. |
| 20 | * |
| 21 | * Should there be not enough data in the buffer to unmarshal the required |
| 22 | * object, the remaining data size is set to -1 to indicate the error. The |
| 23 | * remaining data size is expected to be set to zero once the last data item |
Vadim Bendebury | bc92710 | 2016-07-07 10:52:46 -0700 | [diff] [blame] | 24 | * has been extracted from the receive buffer. |
Vadim Bendebury | 627afc2 | 2016-06-19 12:13:18 -0700 | [diff] [blame] | 25 | */ |
| 26 | static uint16_t unmarshal_u16(void **buffer, int *buffer_space) |
| 27 | { |
| 28 | uint16_t value; |
| 29 | |
Vadim Bendebury | bc92710 | 2016-07-07 10:52:46 -0700 | [diff] [blame] | 30 | if (*buffer_space < 0) |
| 31 | return 0; |
| 32 | |
Vadim Bendebury | 627afc2 | 2016-06-19 12:13:18 -0700 | [diff] [blame] | 33 | if (*buffer_space < sizeof(value)) { |
| 34 | *buffer_space = -1; /* Indicate a failure. */ |
| 35 | return 0; |
| 36 | } |
| 37 | |
| 38 | value = read_be16(*buffer); |
| 39 | *buffer = (void *) ((uintptr_t) (*buffer) + sizeof(value)); |
| 40 | *buffer_space -= sizeof(value); |
| 41 | |
| 42 | return value; |
| 43 | } |
| 44 | |
| 45 | static uint16_t unmarshal_u32(void **buffer, int *buffer_space) |
| 46 | { |
| 47 | uint32_t value; |
| 48 | |
Vadim Bendebury | bc92710 | 2016-07-07 10:52:46 -0700 | [diff] [blame] | 49 | if (*buffer_space < 0) |
| 50 | return 0; |
| 51 | |
Vadim Bendebury | 627afc2 | 2016-06-19 12:13:18 -0700 | [diff] [blame] | 52 | if (*buffer_space < sizeof(value)) { |
| 53 | *buffer_space = -1; /* Indicate a failure. */ |
| 54 | return 0; |
| 55 | } |
| 56 | |
| 57 | value = read_be32(*buffer); |
| 58 | *buffer = (void *) ((uintptr_t) (*buffer) + sizeof(value)); |
| 59 | *buffer_space -= sizeof(value); |
| 60 | |
| 61 | return value; |
| 62 | } |
| 63 | |
| 64 | static uint8_t unmarshal_u8(void **buffer, int *buffer_space) |
| 65 | { |
| 66 | uint8_t value; |
| 67 | |
Vadim Bendebury | bc92710 | 2016-07-07 10:52:46 -0700 | [diff] [blame] | 68 | if (*buffer_space < 0) |
| 69 | return 0; |
| 70 | |
Vadim Bendebury | 627afc2 | 2016-06-19 12:13:18 -0700 | [diff] [blame] | 71 | if (*buffer_space < sizeof(value)) { |
| 72 | *buffer_space = -1; /* Indicate a failure. */ |
| 73 | return 0; |
| 74 | } |
| 75 | |
| 76 | value = ((uint8_t *)(*buffer))[0]; |
| 77 | *buffer = (void *) ((uintptr_t) (*buffer) + sizeof(value)); |
| 78 | *buffer_space -= sizeof(value); |
| 79 | |
| 80 | return value; |
| 81 | } |
| 82 | |
| 83 | #define unmarshal_TPM_CAP(a, b) unmarshal_u32(a, b) |
| 84 | #define unmarshal_TPM_CC(a, b) unmarshal_u32(a, b) |
| 85 | #define unmarshal_TPM_PT(a, b) unmarshal_u32(a, b) |
| 86 | #define unmarshal_TPM_HANDLE(a, b) unmarshal_u32(a, b) |
| 87 | |
| 88 | /* |
| 89 | * Each marshaling function receives a pointer to the buffer to marshal into, |
| 90 | * a pointer to the data item to be marshaled, and a pointer to the remaining |
| 91 | * room in the buffer. |
| 92 | */ |
| 93 | |
| 94 | /* |
| 95 | * Marshaling an arbitrary blob requires its size in addition to common |
| 96 | * parameter set. |
| 97 | */ |
| 98 | static void marshal_blob(void **buffer, void *blob, |
Vadim Bendebury | bc92710 | 2016-07-07 10:52:46 -0700 | [diff] [blame] | 99 | size_t blob_size, size_t *buffer_space) |
Vadim Bendebury | 627afc2 | 2016-06-19 12:13:18 -0700 | [diff] [blame] | 100 | { |
| 101 | if (*buffer_space < blob_size) { |
| 102 | *buffer_space = -1; |
| 103 | return; |
| 104 | } |
| 105 | |
| 106 | memcpy(*buffer, blob, blob_size); |
Vadim Bendebury | bc92710 | 2016-07-07 10:52:46 -0700 | [diff] [blame] | 107 | *buffer_space -= blob_size; |
Vadim Bendebury | 627afc2 | 2016-06-19 12:13:18 -0700 | [diff] [blame] | 108 | *buffer = (void *)((uintptr_t)(*buffer) + blob_size); |
| 109 | } |
| 110 | |
Vadim Bendebury | bc92710 | 2016-07-07 10:52:46 -0700 | [diff] [blame] | 111 | static void marshal_u8(void **buffer, uint8_t value, size_t *buffer_space) |
Vadim Bendebury | 627afc2 | 2016-06-19 12:13:18 -0700 | [diff] [blame] | 112 | { |
| 113 | uint8_t *bp = *buffer; |
| 114 | |
| 115 | if (*buffer_space < sizeof(value)) { |
Vadim Bendebury | bc92710 | 2016-07-07 10:52:46 -0700 | [diff] [blame] | 116 | *buffer_space = 0; |
Vadim Bendebury | 627afc2 | 2016-06-19 12:13:18 -0700 | [diff] [blame] | 117 | return; |
| 118 | } |
| 119 | |
| 120 | *bp++ = value; |
| 121 | *buffer = bp; |
| 122 | *buffer_space -= sizeof(value); |
| 123 | } |
| 124 | |
Vadim Bendebury | bc92710 | 2016-07-07 10:52:46 -0700 | [diff] [blame] | 125 | static void marshal_u16(void **buffer, uint16_t value, size_t *buffer_space) |
Vadim Bendebury | 627afc2 | 2016-06-19 12:13:18 -0700 | [diff] [blame] | 126 | { |
| 127 | if (*buffer_space < sizeof(value)) { |
Vadim Bendebury | bc92710 | 2016-07-07 10:52:46 -0700 | [diff] [blame] | 128 | *buffer_space = 0; |
Vadim Bendebury | 627afc2 | 2016-06-19 12:13:18 -0700 | [diff] [blame] | 129 | return; |
| 130 | } |
| 131 | write_be16(*buffer, value); |
| 132 | *buffer = (void *)((uintptr_t)(*buffer) + sizeof(value)); |
| 133 | *buffer_space -= sizeof(value); |
| 134 | } |
| 135 | |
Vadim Bendebury | bc92710 | 2016-07-07 10:52:46 -0700 | [diff] [blame] | 136 | static void marshal_u32(void **buffer, uint32_t value, size_t *buffer_space) |
Vadim Bendebury | 627afc2 | 2016-06-19 12:13:18 -0700 | [diff] [blame] | 137 | { |
| 138 | if (*buffer_space < sizeof(value)) { |
Vadim Bendebury | bc92710 | 2016-07-07 10:52:46 -0700 | [diff] [blame] | 139 | *buffer_space = 0; |
Vadim Bendebury | 627afc2 | 2016-06-19 12:13:18 -0700 | [diff] [blame] | 140 | return; |
| 141 | } |
| 142 | |
| 143 | write_be32(*buffer, value); |
| 144 | *buffer = (void *)((uintptr_t)(*buffer) + sizeof(value)); |
| 145 | *buffer_space -= sizeof(value); |
| 146 | } |
| 147 | |
| 148 | #define marshal_TPM_HANDLE(a, b, c) marshal_u32(a, b, c) |
| 149 | #define marshal_TPMI_RH_NV_AUTH(a, b, c) marshal_TPM_HANDLE(a, b, c) |
| 150 | #define marshal_TPMI_RH_NV_INDEX(a, b, c) marshal_TPM_HANDLE(a, b, c) |
| 151 | #define marshal_TPMI_SH_AUTH_SESSION(a, b, c) marshal_TPM_HANDLE(a, b, c) |
| 152 | #define marshal_TPMI_ALG_HASH(a, b, c) marshal_u16(a, b, c) |
| 153 | |
| 154 | static void marshal_startup(void **buffer, |
| 155 | struct tpm2_startup *cmd_body, |
Vadim Bendebury | bc92710 | 2016-07-07 10:52:46 -0700 | [diff] [blame] | 156 | size_t *buffer_space) |
Vadim Bendebury | 627afc2 | 2016-06-19 12:13:18 -0700 | [diff] [blame] | 157 | { |
| 158 | marshal_u16(buffer, cmd_body->startup_type, buffer_space); |
| 159 | } |
| 160 | |
| 161 | static void marshal_get_capability(void **buffer, |
| 162 | struct tpm2_get_capability *cmd_body, |
Vadim Bendebury | bc92710 | 2016-07-07 10:52:46 -0700 | [diff] [blame] | 163 | size_t *buffer_space) |
Vadim Bendebury | 627afc2 | 2016-06-19 12:13:18 -0700 | [diff] [blame] | 164 | { |
| 165 | marshal_u32(buffer, cmd_body->capability, buffer_space); |
| 166 | marshal_u32(buffer, cmd_body->property, buffer_space); |
| 167 | marshal_u32(buffer, cmd_body->propertyCount, buffer_space); |
| 168 | } |
| 169 | |
| 170 | static void marshal_TPM2B(void **buffer, |
| 171 | TPM2B *data, |
Vadim Bendebury | bc92710 | 2016-07-07 10:52:46 -0700 | [diff] [blame] | 172 | size_t *buffer_space) |
Vadim Bendebury | 627afc2 | 2016-06-19 12:13:18 -0700 | [diff] [blame] | 173 | { |
| 174 | size_t total_size = data->size + sizeof(data->size); |
| 175 | |
| 176 | if (total_size > *buffer_space) { |
Vadim Bendebury | bc92710 | 2016-07-07 10:52:46 -0700 | [diff] [blame] | 177 | *buffer_space = 0; |
Vadim Bendebury | 627afc2 | 2016-06-19 12:13:18 -0700 | [diff] [blame] | 178 | return; |
| 179 | } |
| 180 | marshal_u16(buffer, data->size, buffer_space); |
| 181 | memcpy(*buffer, data->buffer, data->size); |
| 182 | *buffer = ((uint8_t *)(*buffer)) + data->size; |
| 183 | *buffer_space -= data->size; |
| 184 | } |
| 185 | |
| 186 | static void marshal_TPMA_NV(void **buffer, |
| 187 | TPMA_NV *nv, |
Vadim Bendebury | bc92710 | 2016-07-07 10:52:46 -0700 | [diff] [blame] | 188 | size_t *buffer_space) |
Vadim Bendebury | 627afc2 | 2016-06-19 12:13:18 -0700 | [diff] [blame] | 189 | { |
| 190 | marshal_u32(buffer, *((uint32_t *)nv), buffer_space); |
| 191 | } |
| 192 | |
| 193 | static void marshal_TPMS_NV_PUBLIC(void **buffer, |
| 194 | TPMS_NV_PUBLIC *nvpub, |
Vadim Bendebury | bc92710 | 2016-07-07 10:52:46 -0700 | [diff] [blame] | 195 | size_t *buffer_space) |
Vadim Bendebury | 627afc2 | 2016-06-19 12:13:18 -0700 | [diff] [blame] | 196 | { |
| 197 | marshal_TPM_HANDLE(buffer, nvpub->nvIndex, buffer_space); |
| 198 | marshal_TPMI_ALG_HASH(buffer, nvpub->nameAlg, buffer_space); |
| 199 | marshal_TPMA_NV(buffer, &nvpub->attributes, buffer_space); |
| 200 | marshal_TPM2B(buffer, &nvpub->authPolicy.b, buffer_space); |
| 201 | marshal_u16(buffer, nvpub->dataSize, buffer_space); |
| 202 | } |
| 203 | |
| 204 | static void marshal_session_header(void **buffer, |
| 205 | struct tpm2_session_header *session_header, |
Vadim Bendebury | bc92710 | 2016-07-07 10:52:46 -0700 | [diff] [blame] | 206 | size_t *buffer_space) |
Vadim Bendebury | 627afc2 | 2016-06-19 12:13:18 -0700 | [diff] [blame] | 207 | { |
Vadim Bendebury | bc92710 | 2016-07-07 10:52:46 -0700 | [diff] [blame] | 208 | size_t base_size; |
Vadim Bendebury | 627afc2 | 2016-06-19 12:13:18 -0700 | [diff] [blame] | 209 | void *size_location = *buffer; |
| 210 | |
| 211 | /* Skip room for the session header size. */ |
Vadim Bendebury | bc92710 | 2016-07-07 10:52:46 -0700 | [diff] [blame] | 212 | if (*buffer_space < sizeof(uint32_t)) { |
| 213 | *buffer_space = 0; |
| 214 | return; |
| 215 | } |
| 216 | |
Vadim Bendebury | 627afc2 | 2016-06-19 12:13:18 -0700 | [diff] [blame] | 217 | *buffer_space -= sizeof(uint32_t); |
| 218 | *buffer = (void *)(((uintptr_t) *buffer) + sizeof(uint32_t)); |
| 219 | |
| 220 | base_size = *buffer_space; |
| 221 | |
| 222 | marshal_u32(buffer, session_header->session_handle, buffer_space); |
| 223 | marshal_u16(buffer, session_header->nonce_size, buffer_space); |
| 224 | marshal_blob(buffer, session_header->nonce, |
| 225 | session_header->nonce_size, buffer_space); |
| 226 | marshal_u8(buffer, session_header->session_attrs, buffer_space); |
| 227 | marshal_u16(buffer, session_header->auth_size, buffer_space); |
| 228 | marshal_blob(buffer, session_header->auth, |
| 229 | session_header->auth_size, buffer_space); |
| 230 | |
Vadim Bendebury | bc92710 | 2016-07-07 10:52:46 -0700 | [diff] [blame] | 231 | if (!*buffer_space) |
Vadim Bendebury | 627afc2 | 2016-06-19 12:13:18 -0700 | [diff] [blame] | 232 | return; /* The structure did not fit. */ |
| 233 | |
| 234 | /* Paste in the session size. */ |
| 235 | marshal_u32(&size_location, base_size - *buffer_space, &base_size); |
| 236 | } |
| 237 | |
| 238 | static void marshal_nv_define_space(void **buffer, |
| 239 | struct tpm2_nv_define_space_cmd *nvd_in, |
Vadim Bendebury | bc92710 | 2016-07-07 10:52:46 -0700 | [diff] [blame] | 240 | size_t *buffer_space) |
Vadim Bendebury | 627afc2 | 2016-06-19 12:13:18 -0700 | [diff] [blame] | 241 | { |
| 242 | void *size_location; |
Vadim Bendebury | bc92710 | 2016-07-07 10:52:46 -0700 | [diff] [blame] | 243 | size_t base_size; |
| 244 | size_t sizeof_nv_public_size = sizeof(uint16_t); |
Vadim Bendebury | 627afc2 | 2016-06-19 12:13:18 -0700 | [diff] [blame] | 245 | struct tpm2_session_header session_header; |
| 246 | |
| 247 | marshal_TPM_HANDLE(buffer, TPM_RH_PLATFORM, buffer_space); |
| 248 | memset(&session_header, 0, sizeof(session_header)); |
| 249 | session_header.session_handle = TPM_RS_PW; |
| 250 | marshal_session_header(buffer, &session_header, buffer_space); |
| 251 | tpm_tag = TPM_ST_SESSIONS; |
| 252 | |
| 253 | marshal_TPM2B(buffer, &nvd_in->auth.b, buffer_space); |
| 254 | |
| 255 | /* This is where the TPMS_NV_PUBLIC size will be stored. */ |
| 256 | size_location = *buffer; |
| 257 | |
| 258 | /* Allocate room for the size. */ |
| 259 | *buffer = ((uint8_t *)(*buffer)) + sizeof(uint16_t); |
Vadim Bendebury | bc92710 | 2016-07-07 10:52:46 -0700 | [diff] [blame] | 260 | |
| 261 | if (*buffer_space < sizeof_nv_public_size) { |
| 262 | *buffer_space = 0; |
| 263 | return; |
| 264 | } |
| 265 | *buffer_space -= sizeof_nv_public_size; |
Vadim Bendebury | 627afc2 | 2016-06-19 12:13:18 -0700 | [diff] [blame] | 266 | base_size = *buffer_space; |
| 267 | |
| 268 | marshal_TPMS_NV_PUBLIC(buffer, &nvd_in->publicInfo, buffer_space); |
Vadim Bendebury | bc92710 | 2016-07-07 10:52:46 -0700 | [diff] [blame] | 269 | if (!*buffer_space) |
| 270 | return; |
| 271 | |
Vadim Bendebury | 627afc2 | 2016-06-19 12:13:18 -0700 | [diff] [blame] | 272 | base_size = base_size - *buffer_space; |
Vadim Bendebury | bc92710 | 2016-07-07 10:52:46 -0700 | [diff] [blame] | 273 | marshal_u16(&size_location, base_size, &sizeof_nv_public_size); |
Vadim Bendebury | 627afc2 | 2016-06-19 12:13:18 -0700 | [diff] [blame] | 274 | } |
| 275 | |
| 276 | static void marshal_nv_write(void **buffer, |
| 277 | struct tpm2_nv_write_cmd *command_body, |
Vadim Bendebury | bc92710 | 2016-07-07 10:52:46 -0700 | [diff] [blame] | 278 | size_t *buffer_space) |
Vadim Bendebury | 627afc2 | 2016-06-19 12:13:18 -0700 | [diff] [blame] | 279 | { |
| 280 | struct tpm2_session_header session_header; |
| 281 | |
| 282 | marshal_TPM_HANDLE(buffer, TPM_RH_PLATFORM, buffer_space); |
| 283 | marshal_TPM_HANDLE(buffer, command_body->nvIndex, buffer_space); |
| 284 | memset(&session_header, 0, sizeof(session_header)); |
| 285 | session_header.session_handle = TPM_RS_PW; |
| 286 | marshal_session_header(buffer, &session_header, buffer_space); |
| 287 | tpm_tag = TPM_ST_SESSIONS; |
| 288 | |
| 289 | marshal_TPM2B(buffer, &command_body->data.b, buffer_space); |
| 290 | marshal_u16(buffer, command_body->offset, buffer_space); |
| 291 | } |
| 292 | |
| 293 | static void marshal_nv_read(void **buffer, |
| 294 | struct tpm2_nv_read_cmd *command_body, |
Vadim Bendebury | bc92710 | 2016-07-07 10:52:46 -0700 | [diff] [blame] | 295 | size_t *buffer_space) |
Vadim Bendebury | 627afc2 | 2016-06-19 12:13:18 -0700 | [diff] [blame] | 296 | { |
| 297 | struct tpm2_session_header session_header; |
| 298 | |
| 299 | marshal_TPM_HANDLE(buffer, TPM_RH_PLATFORM, buffer_space); |
| 300 | marshal_TPM_HANDLE(buffer, command_body->nvIndex, buffer_space); |
| 301 | memset(&session_header, 0, sizeof(session_header)); |
| 302 | session_header.session_handle = TPM_RS_PW; |
| 303 | marshal_session_header(buffer, &session_header, buffer_space); |
| 304 | tpm_tag = TPM_ST_SESSIONS; |
| 305 | marshal_u16(buffer, command_body->size, buffer_space); |
| 306 | marshal_u16(buffer, command_body->offset, buffer_space); |
| 307 | } |
| 308 | |
| 309 | static void marshal_selftest(void **buffer, |
| 310 | struct tpm2_self_test *command_body, |
Vadim Bendebury | bc92710 | 2016-07-07 10:52:46 -0700 | [diff] [blame] | 311 | size_t *buffer_space) |
Vadim Bendebury | 627afc2 | 2016-06-19 12:13:18 -0700 | [diff] [blame] | 312 | { |
| 313 | marshal_u8(buffer, command_body->yes_no, buffer_space); |
| 314 | } |
| 315 | |
| 316 | int tpm_marshal_command(TPM_CC command, void *tpm_command_body, |
Vadim Bendebury | bc92710 | 2016-07-07 10:52:46 -0700 | [diff] [blame] | 317 | void *buffer, size_t buffer_size) |
Vadim Bendebury | 627afc2 | 2016-06-19 12:13:18 -0700 | [diff] [blame] | 318 | { |
| 319 | void *cmd_body = (uint8_t *)buffer + sizeof(struct tpm_header); |
Vadim Bendebury | bc92710 | 2016-07-07 10:52:46 -0700 | [diff] [blame] | 320 | size_t max_body_size = buffer_size - sizeof(struct tpm_header); |
| 321 | size_t body_size = max_body_size; |
Vadim Bendebury | 627afc2 | 2016-06-19 12:13:18 -0700 | [diff] [blame] | 322 | |
| 323 | /* Will be modified when marshaling some commands. */ |
| 324 | tpm_tag = TPM_ST_NO_SESSIONS; |
| 325 | |
| 326 | switch (command) { |
| 327 | case TPM2_Startup: |
| 328 | marshal_startup(&cmd_body, tpm_command_body, &body_size); |
| 329 | break; |
| 330 | |
| 331 | case TPM2_GetCapability: |
| 332 | marshal_get_capability(&cmd_body, tpm_command_body, |
| 333 | &body_size); |
| 334 | break; |
| 335 | |
| 336 | case TPM2_NV_Read: |
| 337 | marshal_nv_read(&cmd_body, tpm_command_body, &body_size); |
| 338 | break; |
| 339 | |
| 340 | case TPM2_NV_DefineSpace: |
| 341 | marshal_nv_define_space(&cmd_body, |
| 342 | tpm_command_body, &body_size); |
| 343 | break; |
| 344 | |
| 345 | case TPM2_NV_Write: |
| 346 | marshal_nv_write(&cmd_body, tpm_command_body, &body_size); |
| 347 | break; |
| 348 | |
| 349 | case TPM2_SelfTest: |
| 350 | marshal_selftest(&cmd_body, tpm_command_body, &body_size); |
| 351 | break; |
| 352 | |
| 353 | default: |
Vadim Bendebury | bc92710 | 2016-07-07 10:52:46 -0700 | [diff] [blame] | 354 | body_size = 0; |
Vadim Bendebury | 627afc2 | 2016-06-19 12:13:18 -0700 | [diff] [blame] | 355 | printk(BIOS_INFO, "%s:%d:Request to marshal unsupported command %#x\n", |
| 356 | __FILE__, __LINE__, command); |
| 357 | } |
| 358 | |
| 359 | if (body_size > 0) { |
Vadim Bendebury | d9137d5 | 2016-07-07 14:27:41 -0700 | [diff] [blame^] | 360 | size_t marshaled_size; |
| 361 | size_t header_room = sizeof(struct tpm_header); |
Vadim Bendebury | 627afc2 | 2016-06-19 12:13:18 -0700 | [diff] [blame] | 362 | |
| 363 | /* See how much room was taken by marshaling. */ |
Vadim Bendebury | d9137d5 | 2016-07-07 14:27:41 -0700 | [diff] [blame^] | 364 | marshaled_size = max_body_size - body_size; |
Vadim Bendebury | 627afc2 | 2016-06-19 12:13:18 -0700 | [diff] [blame] | 365 | |
Vadim Bendebury | d9137d5 | 2016-07-07 14:27:41 -0700 | [diff] [blame^] | 366 | /* Total size includes the header size. */ |
| 367 | marshaled_size += sizeof(struct tpm_header); |
Vadim Bendebury | 627afc2 | 2016-06-19 12:13:18 -0700 | [diff] [blame] | 368 | |
Vadim Bendebury | d9137d5 | 2016-07-07 14:27:41 -0700 | [diff] [blame^] | 369 | marshal_u16(&buffer, tpm_tag, &header_room); |
| 370 | marshal_u32(&buffer, marshaled_size, &header_room); |
| 371 | marshal_u32(&buffer, command, &header_room); |
| 372 | return marshaled_size; |
Vadim Bendebury | 627afc2 | 2016-06-19 12:13:18 -0700 | [diff] [blame] | 373 | } |
| 374 | |
Vadim Bendebury | d9137d5 | 2016-07-07 14:27:41 -0700 | [diff] [blame^] | 375 | return -1; |
Vadim Bendebury | 627afc2 | 2016-06-19 12:13:18 -0700 | [diff] [blame] | 376 | } |
| 377 | |
| 378 | static void unmarshal_get_capability(void **buffer, int *size, |
| 379 | struct get_cap_response *gcr) |
| 380 | { |
| 381 | int i; |
| 382 | |
| 383 | gcr->more_data = unmarshal_u8(buffer, size); |
| 384 | |
| 385 | gcr->cd.capability = unmarshal_TPM_CAP(buffer, size); |
| 386 | if (*size < 0) |
| 387 | return; |
| 388 | |
| 389 | switch (gcr->cd.capability) { |
| 390 | case TPM_CAP_TPM_PROPERTIES: |
| 391 | gcr->cd.data.tpmProperties.count = |
| 392 | unmarshal_u32(buffer, size); |
| 393 | if (*size < 0) |
| 394 | return; |
| 395 | if (gcr->cd.data.tpmProperties.count > ARRAY_SIZE |
| 396 | (gcr->cd.data.tpmProperties.tpmProperty)) { |
| 397 | printk(BIOS_INFO, "%s:%s:%d - %d - too many properties\n", |
| 398 | __FILE__, __func__, __LINE__, |
| 399 | gcr->cd.data.tpmProperties.count); |
| 400 | *size = -1; |
| 401 | return; |
| 402 | } |
| 403 | for (i = 0; i < gcr->cd.data.tpmProperties.count; i++) { |
| 404 | TPMS_TAGGED_PROPERTY *pp; |
| 405 | |
| 406 | pp = gcr->cd.data.tpmProperties.tpmProperty + i; |
| 407 | pp->property = unmarshal_TPM_PT(buffer, size); |
| 408 | pp->value = unmarshal_u32(buffer, size); |
| 409 | } |
| 410 | break; |
| 411 | default: |
| 412 | printk(BIOS_ERR, |
| 413 | "%s:%d - unable to unmarshal capability response", |
| 414 | __func__, __LINE__); |
| 415 | printk(BIOS_ERR, " for %d\n", gcr->cd.capability); |
| 416 | *size = -1; |
| 417 | break; |
| 418 | } |
| 419 | } |
| 420 | |
| 421 | static void unmarshal_TPM2B_MAX_NV_BUFFER(void **buffer, |
| 422 | int *size, |
| 423 | TPM2B_MAX_NV_BUFFER *nv_buffer) |
| 424 | { |
| 425 | nv_buffer->t.size = unmarshal_u16(buffer, size); |
Vadim Bendebury | bc92710 | 2016-07-07 10:52:46 -0700 | [diff] [blame] | 426 | if ((*size < 0) || (nv_buffer->t.size > *size)) { |
Vadim Bendebury | 627afc2 | 2016-06-19 12:13:18 -0700 | [diff] [blame] | 427 | printk(BIOS_ERR, "%s:%d - " |
| 428 | "size mismatch: expected %d, remaining %d\n", |
| 429 | __func__, __LINE__, nv_buffer->t.size, *size); |
Vadim Bendebury | bc92710 | 2016-07-07 10:52:46 -0700 | [diff] [blame] | 430 | *size = -1; |
Vadim Bendebury | 627afc2 | 2016-06-19 12:13:18 -0700 | [diff] [blame] | 431 | return; |
| 432 | } |
| 433 | |
| 434 | nv_buffer->t.buffer = *buffer; |
| 435 | |
| 436 | *buffer = ((uint8_t *)(*buffer)) + nv_buffer->t.size; |
| 437 | *size -= nv_buffer->t.size; |
| 438 | } |
| 439 | |
| 440 | static void unmarshal_nv_read(void **buffer, int *size, |
| 441 | struct nv_read_response *nvr) |
| 442 | { |
| 443 | /* Total size of the parameter field. */ |
| 444 | nvr->params_size = unmarshal_u32(buffer, size); |
| 445 | unmarshal_TPM2B_MAX_NV_BUFFER(buffer, size, &nvr->buffer); |
| 446 | |
| 447 | if (nvr->params_size != |
| 448 | (nvr->buffer.t.size + sizeof(nvr->buffer.t.size))) { |
| 449 | printk(BIOS_ERR, |
| 450 | "%s:%d - parameter/buffer %d/%d size mismatch", |
| 451 | __func__, __LINE__, nvr->params_size, |
| 452 | nvr->buffer.t.size); |
| 453 | return; |
| 454 | } |
| 455 | |
| 456 | if (*size < 0) |
| 457 | return; |
| 458 | /* |
| 459 | * Let's ignore the authorisation section. It should be 5 bytes total, |
| 460 | * just confirm that this is the case and report any discrepancy. |
| 461 | */ |
| 462 | if (*size != 5) |
| 463 | printk(BIOS_ERR, |
| 464 | "%s:%d - unexpected authorisation seciton size %d\n", |
| 465 | __func__, __LINE__, *size); |
| 466 | |
| 467 | *buffer = ((uint8_t *)(*buffer)) + *size; |
| 468 | *size = 0; |
| 469 | } |
| 470 | |
| 471 | struct tpm2_response *tpm_unmarshal_response(TPM_CC command, |
| 472 | void *response_body, |
Vadim Bendebury | bc92710 | 2016-07-07 10:52:46 -0700 | [diff] [blame] | 473 | size_t in_size) |
Vadim Bendebury | 627afc2 | 2016-06-19 12:13:18 -0700 | [diff] [blame] | 474 | { |
| 475 | static struct tpm2_response tpm2_resp; |
Vadim Bendebury | bc92710 | 2016-07-07 10:52:46 -0700 | [diff] [blame] | 476 | /* |
| 477 | * Should be 0 when done, positive and negaitive values indicate |
| 478 | * unmarshaling errors. |
| 479 | */ |
| 480 | int cr_size = in_size; |
Vadim Bendebury | 627afc2 | 2016-06-19 12:13:18 -0700 | [diff] [blame] | 481 | |
Vadim Bendebury | bc92710 | 2016-07-07 10:52:46 -0700 | [diff] [blame] | 482 | if ((cr_size < 0) || (in_size < sizeof(struct tpm_header))) |
Vadim Bendebury | 627afc2 | 2016-06-19 12:13:18 -0700 | [diff] [blame] | 483 | return NULL; |
| 484 | |
| 485 | tpm2_resp.hdr.tpm_tag = unmarshal_u16(&response_body, &cr_size); |
| 486 | tpm2_resp.hdr.tpm_size = unmarshal_u32(&response_body, &cr_size); |
| 487 | tpm2_resp.hdr.tpm_code = unmarshal_TPM_CC(&response_body, &cr_size); |
| 488 | |
| 489 | if (!cr_size) { |
| 490 | if (tpm2_resp.hdr.tpm_size != sizeof(tpm2_resp.hdr)) |
| 491 | printk(BIOS_ERR, |
| 492 | "%s: size mismatch in response to command %#x\n", |
| 493 | __func__, command); |
| 494 | return &tpm2_resp; |
| 495 | } |
| 496 | |
| 497 | switch (command) { |
| 498 | case TPM2_Startup: |
| 499 | break; |
| 500 | |
| 501 | case TPM2_GetCapability: |
| 502 | unmarshal_get_capability(&response_body, &cr_size, |
| 503 | &tpm2_resp.gc); |
| 504 | break; |
| 505 | |
| 506 | case TPM2_NV_Read: |
| 507 | unmarshal_nv_read(&response_body, &cr_size, |
| 508 | &tpm2_resp.nvr); |
| 509 | break; |
| 510 | |
| 511 | case TPM2_NV_DefineSpace: |
| 512 | case TPM2_NV_Write: |
| 513 | /* Session data included in response can be safely ignored. */ |
| 514 | cr_size = 0; |
| 515 | break; |
| 516 | |
| 517 | default: |
| 518 | { |
| 519 | int i; |
| 520 | |
| 521 | printk(BIOS_INFO, "%s:%d:" |
| 522 | "Request to unmarshal unexpected command %#x," |
| 523 | " code %#x", |
| 524 | __func__, __LINE__, command, |
| 525 | tpm2_resp.hdr.tpm_code); |
| 526 | |
| 527 | for (i = 0; i < cr_size; i++) { |
| 528 | if (!(i % 16)) |
| 529 | printk(BIOS_INFO, "\n"); |
| 530 | printk(BIOS_INFO, "%2.2x ", |
| 531 | ((uint8_t *)response_body)[i]); |
| 532 | } |
| 533 | } |
| 534 | printk(BIOS_INFO, "\n"); |
| 535 | return NULL; |
| 536 | } |
| 537 | |
| 538 | if (cr_size) { |
| 539 | printk(BIOS_INFO, |
| 540 | "%s:%d got %d bytes back in response to %#x," |
| 541 | " failed to parse (%d)\n", |
| 542 | __func__, __LINE__, tpm2_resp.hdr.tpm_size, |
| 543 | command, cr_size); |
| 544 | return NULL; |
| 545 | } |
| 546 | |
| 547 | /* The entire message have been parsed. */ |
| 548 | return &tpm2_resp; |
| 549 | } |