blob: 6d4d622d912c7e3dfa9cc3fa5baee1ccb2aed4e9 [file] [log] [blame]
Vadim Bendebury627afc22016-06-19 12:13:18 -07001/*
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
Victor Prupisf7060202016-08-19 10:45:04 -07007#include <arch/early_variables.h>
Vadim Bendebury627afc22016-06-19 12:13:18 -07008#include <commonlib/endian.h>
9#include <console/console.h>
10#include <stdlib.h>
11#include <string.h>
12
13#include "tpm2_marshaling.h"
14
Victor Prupisf7060202016-08-19 10:45:04 -070015static uint16_t tpm_tag CAR_GLOBAL; /* Depends on the command type. */
Vadim Bendebury627afc22016-06-19 12:13:18 -070016
17/*
18 * Each unmarshaling function receives a pointer to the buffer pointer and a
19 * pointer to the size of data still in the buffer. The function extracts data
20 * from the buffer and adjusts both buffer pointer and remaining data size.
21 *
22 * Should there be not enough data in the buffer to unmarshal the required
23 * object, the remaining data size is set to -1 to indicate the error. The
24 * remaining data size is expected to be set to zero once the last data item
Vadim Bendeburybc927102016-07-07 10:52:46 -070025 * has been extracted from the receive buffer.
Vadim Bendebury627afc22016-06-19 12:13:18 -070026 */
27static uint16_t unmarshal_u16(void **buffer, int *buffer_space)
28{
29 uint16_t value;
30
Vadim Bendeburybc927102016-07-07 10:52:46 -070031 if (*buffer_space < 0)
32 return 0;
33
Vadim Bendebury627afc22016-06-19 12:13:18 -070034 if (*buffer_space < sizeof(value)) {
35 *buffer_space = -1; /* Indicate a failure. */
36 return 0;
37 }
38
39 value = read_be16(*buffer);
40 *buffer = (void *) ((uintptr_t) (*buffer) + sizeof(value));
41 *buffer_space -= sizeof(value);
42
43 return value;
44}
45
46static uint16_t unmarshal_u32(void **buffer, int *buffer_space)
47{
48 uint32_t value;
49
Vadim Bendeburybc927102016-07-07 10:52:46 -070050 if (*buffer_space < 0)
51 return 0;
52
Vadim Bendebury627afc22016-06-19 12:13:18 -070053 if (*buffer_space < sizeof(value)) {
54 *buffer_space = -1; /* Indicate a failure. */
55 return 0;
56 }
57
58 value = read_be32(*buffer);
59 *buffer = (void *) ((uintptr_t) (*buffer) + sizeof(value));
60 *buffer_space -= sizeof(value);
61
62 return value;
63}
64
65static uint8_t unmarshal_u8(void **buffer, int *buffer_space)
66{
67 uint8_t value;
68
Vadim Bendeburybc927102016-07-07 10:52:46 -070069 if (*buffer_space < 0)
70 return 0;
71
Vadim Bendebury627afc22016-06-19 12:13:18 -070072 if (*buffer_space < sizeof(value)) {
73 *buffer_space = -1; /* Indicate a failure. */
74 return 0;
75 }
76
77 value = ((uint8_t *)(*buffer))[0];
78 *buffer = (void *) ((uintptr_t) (*buffer) + sizeof(value));
79 *buffer_space -= sizeof(value);
80
81 return value;
82}
83
84#define unmarshal_TPM_CAP(a, b) unmarshal_u32(a, b)
85#define unmarshal_TPM_CC(a, b) unmarshal_u32(a, b)
86#define unmarshal_TPM_PT(a, b) unmarshal_u32(a, b)
87#define unmarshal_TPM_HANDLE(a, b) unmarshal_u32(a, b)
88
89/*
90 * Each marshaling function receives a pointer to the buffer to marshal into,
91 * a pointer to the data item to be marshaled, and a pointer to the remaining
92 * room in the buffer.
93 */
94
95 /*
96 * Marshaling an arbitrary blob requires its size in addition to common
97 * parameter set.
98 */
99static void marshal_blob(void **buffer, void *blob,
Vadim Bendeburybc927102016-07-07 10:52:46 -0700100 size_t blob_size, size_t *buffer_space)
Vadim Bendebury627afc22016-06-19 12:13:18 -0700101{
102 if (*buffer_space < blob_size) {
Aaron Durbin06f12f92017-03-06 16:33:57 -0600103 *buffer_space = 0;
Vadim Bendebury627afc22016-06-19 12:13:18 -0700104 return;
105 }
106
107 memcpy(*buffer, blob, blob_size);
Vadim Bendeburybc927102016-07-07 10:52:46 -0700108 *buffer_space -= blob_size;
Vadim Bendebury627afc22016-06-19 12:13:18 -0700109 *buffer = (void *)((uintptr_t)(*buffer) + blob_size);
110}
111
Vadim Bendeburybc927102016-07-07 10:52:46 -0700112static void marshal_u8(void **buffer, uint8_t value, size_t *buffer_space)
Vadim Bendebury627afc22016-06-19 12:13:18 -0700113{
114 uint8_t *bp = *buffer;
115
116 if (*buffer_space < sizeof(value)) {
Vadim Bendeburybc927102016-07-07 10:52:46 -0700117 *buffer_space = 0;
Vadim Bendebury627afc22016-06-19 12:13:18 -0700118 return;
119 }
120
121 *bp++ = value;
122 *buffer = bp;
123 *buffer_space -= sizeof(value);
124}
125
Vadim Bendeburybc927102016-07-07 10:52:46 -0700126static void marshal_u16(void **buffer, uint16_t value, size_t *buffer_space)
Vadim Bendebury627afc22016-06-19 12:13:18 -0700127{
128 if (*buffer_space < sizeof(value)) {
Vadim Bendeburybc927102016-07-07 10:52:46 -0700129 *buffer_space = 0;
Vadim Bendebury627afc22016-06-19 12:13:18 -0700130 return;
131 }
132 write_be16(*buffer, value);
133 *buffer = (void *)((uintptr_t)(*buffer) + sizeof(value));
134 *buffer_space -= sizeof(value);
135}
136
Vadim Bendeburybc927102016-07-07 10:52:46 -0700137static void marshal_u32(void **buffer, uint32_t value, size_t *buffer_space)
Vadim Bendebury627afc22016-06-19 12:13:18 -0700138{
139 if (*buffer_space < sizeof(value)) {
Vadim Bendeburybc927102016-07-07 10:52:46 -0700140 *buffer_space = 0;
Vadim Bendebury627afc22016-06-19 12:13:18 -0700141 return;
142 }
143
144 write_be32(*buffer, value);
145 *buffer = (void *)((uintptr_t)(*buffer) + sizeof(value));
146 *buffer_space -= sizeof(value);
147}
148
149#define marshal_TPM_HANDLE(a, b, c) marshal_u32(a, b, c)
150#define marshal_TPMI_RH_NV_AUTH(a, b, c) marshal_TPM_HANDLE(a, b, c)
151#define marshal_TPMI_RH_NV_INDEX(a, b, c) marshal_TPM_HANDLE(a, b, c)
152#define marshal_TPMI_SH_AUTH_SESSION(a, b, c) marshal_TPM_HANDLE(a, b, c)
153#define marshal_TPMI_ALG_HASH(a, b, c) marshal_u16(a, b, c)
154
155static void marshal_startup(void **buffer,
156 struct tpm2_startup *cmd_body,
Vadim Bendeburybc927102016-07-07 10:52:46 -0700157 size_t *buffer_space)
Vadim Bendebury627afc22016-06-19 12:13:18 -0700158{
159 marshal_u16(buffer, cmd_body->startup_type, buffer_space);
160}
161
162static void marshal_get_capability(void **buffer,
163 struct tpm2_get_capability *cmd_body,
Vadim Bendeburybc927102016-07-07 10:52:46 -0700164 size_t *buffer_space)
Vadim Bendebury627afc22016-06-19 12:13:18 -0700165{
166 marshal_u32(buffer, cmd_body->capability, buffer_space);
167 marshal_u32(buffer, cmd_body->property, buffer_space);
168 marshal_u32(buffer, cmd_body->propertyCount, buffer_space);
169}
170
171static void marshal_TPM2B(void **buffer,
172 TPM2B *data,
Vadim Bendeburybc927102016-07-07 10:52:46 -0700173 size_t *buffer_space)
Vadim Bendebury627afc22016-06-19 12:13:18 -0700174{
175 size_t total_size = data->size + sizeof(data->size);
176
177 if (total_size > *buffer_space) {
Vadim Bendeburybc927102016-07-07 10:52:46 -0700178 *buffer_space = 0;
Vadim Bendebury627afc22016-06-19 12:13:18 -0700179 return;
180 }
181 marshal_u16(buffer, data->size, buffer_space);
182 memcpy(*buffer, data->buffer, data->size);
183 *buffer = ((uint8_t *)(*buffer)) + data->size;
184 *buffer_space -= data->size;
185}
186
187static void marshal_TPMA_NV(void **buffer,
188 TPMA_NV *nv,
Vadim Bendeburybc927102016-07-07 10:52:46 -0700189 size_t *buffer_space)
Vadim Bendebury627afc22016-06-19 12:13:18 -0700190{
191 marshal_u32(buffer, *((uint32_t *)nv), buffer_space);
192}
193
194static void marshal_TPMS_NV_PUBLIC(void **buffer,
195 TPMS_NV_PUBLIC *nvpub,
Vadim Bendeburybc927102016-07-07 10:52:46 -0700196 size_t *buffer_space)
Vadim Bendebury627afc22016-06-19 12:13:18 -0700197{
198 marshal_TPM_HANDLE(buffer, nvpub->nvIndex, buffer_space);
199 marshal_TPMI_ALG_HASH(buffer, nvpub->nameAlg, buffer_space);
200 marshal_TPMA_NV(buffer, &nvpub->attributes, buffer_space);
201 marshal_TPM2B(buffer, &nvpub->authPolicy.b, buffer_space);
202 marshal_u16(buffer, nvpub->dataSize, buffer_space);
203}
204
Vadim Bendeburyf5ef6992016-07-03 22:20:17 -0700205static void marshal_TPMT_HA(void **buffer,
206 TPMT_HA *tpmtha,
207 size_t *buffer_space)
208{
209 marshal_TPMI_ALG_HASH(buffer, tpmtha->hashAlg, buffer_space);
210 marshal_blob(buffer, tpmtha->digest.sha256,
211 sizeof(tpmtha->digest.sha256),
212 buffer_space);
213}
214
215static void marshal_TPML_DIGEST_VALUES(void **buffer,
216 TPML_DIGEST_VALUES *dvalues,
217 size_t *buffer_space)
218{
219 int i;
220
221 marshal_u32(buffer, dvalues->count, buffer_space);
222 for (i = 0; i < dvalues->count; i++)
223 marshal_TPMT_HA(buffer, &dvalues->digests[i], buffer_space);
224}
225
Vadim Bendebury627afc22016-06-19 12:13:18 -0700226static void marshal_session_header(void **buffer,
227 struct tpm2_session_header *session_header,
Vadim Bendeburybc927102016-07-07 10:52:46 -0700228 size_t *buffer_space)
Vadim Bendebury627afc22016-06-19 12:13:18 -0700229{
Vadim Bendeburybc927102016-07-07 10:52:46 -0700230 size_t base_size;
Vadim Bendebury627afc22016-06-19 12:13:18 -0700231 void *size_location = *buffer;
232
233 /* Skip room for the session header size. */
Vadim Bendeburybc927102016-07-07 10:52:46 -0700234 if (*buffer_space < sizeof(uint32_t)) {
235 *buffer_space = 0;
236 return;
237 }
238
Vadim Bendebury627afc22016-06-19 12:13:18 -0700239 *buffer_space -= sizeof(uint32_t);
240 *buffer = (void *)(((uintptr_t) *buffer) + sizeof(uint32_t));
241
242 base_size = *buffer_space;
243
244 marshal_u32(buffer, session_header->session_handle, buffer_space);
245 marshal_u16(buffer, session_header->nonce_size, buffer_space);
246 marshal_blob(buffer, session_header->nonce,
247 session_header->nonce_size, buffer_space);
248 marshal_u8(buffer, session_header->session_attrs, buffer_space);
249 marshal_u16(buffer, session_header->auth_size, buffer_space);
250 marshal_blob(buffer, session_header->auth,
251 session_header->auth_size, buffer_space);
252
Vadim Bendeburybc927102016-07-07 10:52:46 -0700253 if (!*buffer_space)
Vadim Bendebury627afc22016-06-19 12:13:18 -0700254 return; /* The structure did not fit. */
255
256 /* Paste in the session size. */
257 marshal_u32(&size_location, base_size - *buffer_space, &base_size);
258}
259
Vadim Bendeburyebba4d72016-07-07 11:04:06 -0700260/*
261 * Common session header can include one or two handles and an empty
262 * session_header structure.
263 */
264static void marshal_common_session_header(void **buffer,
265 const uint32_t *handles,
266 size_t handle_count,
267 size_t *buffer_space)
268{
269 int i;
270 struct tpm2_session_header session_header;
271
Victor Prupisf7060202016-08-19 10:45:04 -0700272 car_set_var(tpm_tag, TPM_ST_SESSIONS);
Vadim Bendeburyebba4d72016-07-07 11:04:06 -0700273
274 for (i = 0; i < handle_count; i++)
275 marshal_TPM_HANDLE(buffer, handles[i], buffer_space);
276
277 memset(&session_header, 0, sizeof(session_header));
278 session_header.session_handle = TPM_RS_PW;
279 marshal_session_header(buffer, &session_header, buffer_space);
280}
281
Vadim Bendebury627afc22016-06-19 12:13:18 -0700282static void marshal_nv_define_space(void **buffer,
283 struct tpm2_nv_define_space_cmd *nvd_in,
Vadim Bendeburybc927102016-07-07 10:52:46 -0700284 size_t *buffer_space)
Vadim Bendebury627afc22016-06-19 12:13:18 -0700285{
286 void *size_location;
Vadim Bendeburybc927102016-07-07 10:52:46 -0700287 size_t base_size;
288 size_t sizeof_nv_public_size = sizeof(uint16_t);
Vadim Bendeburyebba4d72016-07-07 11:04:06 -0700289 const uint32_t handle[] = { TPM_RH_PLATFORM };
Vadim Bendebury627afc22016-06-19 12:13:18 -0700290
Vadim Bendeburyebba4d72016-07-07 11:04:06 -0700291 marshal_common_session_header(buffer, handle,
292 ARRAY_SIZE(handle), buffer_space);
Vadim Bendebury627afc22016-06-19 12:13:18 -0700293 marshal_TPM2B(buffer, &nvd_in->auth.b, buffer_space);
294
295 /* This is where the TPMS_NV_PUBLIC size will be stored. */
296 size_location = *buffer;
297
298 /* Allocate room for the size. */
299 *buffer = ((uint8_t *)(*buffer)) + sizeof(uint16_t);
Vadim Bendeburybc927102016-07-07 10:52:46 -0700300
301 if (*buffer_space < sizeof_nv_public_size) {
302 *buffer_space = 0;
303 return;
304 }
305 *buffer_space -= sizeof_nv_public_size;
Vadim Bendebury627afc22016-06-19 12:13:18 -0700306 base_size = *buffer_space;
307
308 marshal_TPMS_NV_PUBLIC(buffer, &nvd_in->publicInfo, buffer_space);
Vadim Bendeburybc927102016-07-07 10:52:46 -0700309 if (!*buffer_space)
310 return;
311
Vadim Bendebury627afc22016-06-19 12:13:18 -0700312 base_size = base_size - *buffer_space;
Vadim Bendeburybc927102016-07-07 10:52:46 -0700313 marshal_u16(&size_location, base_size, &sizeof_nv_public_size);
Vadim Bendebury627afc22016-06-19 12:13:18 -0700314}
315
316static void marshal_nv_write(void **buffer,
317 struct tpm2_nv_write_cmd *command_body,
Vadim Bendeburybc927102016-07-07 10:52:46 -0700318 size_t *buffer_space)
Vadim Bendebury627afc22016-06-19 12:13:18 -0700319{
Vadim Bendeburyebba4d72016-07-07 11:04:06 -0700320 uint32_t handles[] = { TPM_RH_PLATFORM, command_body->nvIndex };
Vadim Bendebury627afc22016-06-19 12:13:18 -0700321
Vadim Bendeburyebba4d72016-07-07 11:04:06 -0700322 marshal_common_session_header(buffer, handles,
323 ARRAY_SIZE(handles), buffer_space);
Vadim Bendebury627afc22016-06-19 12:13:18 -0700324 marshal_TPM2B(buffer, &command_body->data.b, buffer_space);
325 marshal_u16(buffer, command_body->offset, buffer_space);
326}
327
Vadim Bendebury4c0851c2016-07-03 17:08:10 -0700328static void marshal_nv_write_lock(void **buffer,
329 struct tpm2_nv_write_lock_cmd *command_body,
330 size_t *buffer_space)
331{
332 uint32_t handles[] = { TPM_RH_PLATFORM, command_body->nvIndex };
333 marshal_common_session_header(buffer, handles,
334 ARRAY_SIZE(handles), buffer_space);
335}
336
Vadim Bendeburyf5ef6992016-07-03 22:20:17 -0700337static void marshal_pcr_extend(void **buffer,
338 struct tpm2_pcr_extend_cmd *command_body,
339 size_t *buffer_space)
340{
341 uint32_t handle = command_body->pcrHandle;
342
343 marshal_common_session_header(buffer, &handle, 1, buffer_space);
344 marshal_TPML_DIGEST_VALUES(buffer,
345 &command_body->digests, buffer_space);
346}
347
Vadim Bendebury627afc22016-06-19 12:13:18 -0700348static void marshal_nv_read(void **buffer,
349 struct tpm2_nv_read_cmd *command_body,
Vadim Bendeburybc927102016-07-07 10:52:46 -0700350 size_t *buffer_space)
Vadim Bendebury627afc22016-06-19 12:13:18 -0700351{
Vadim Bendeburyebba4d72016-07-07 11:04:06 -0700352 uint32_t handles[] = { TPM_RH_PLATFORM, command_body->nvIndex };
Vadim Bendebury627afc22016-06-19 12:13:18 -0700353
Vadim Bendeburyebba4d72016-07-07 11:04:06 -0700354 marshal_common_session_header(buffer, handles,
355 ARRAY_SIZE(handles), buffer_space);
Vadim Bendebury627afc22016-06-19 12:13:18 -0700356 marshal_u16(buffer, command_body->size, buffer_space);
357 marshal_u16(buffer, command_body->offset, buffer_space);
358}
359
Vadim Bendebury6acb9a62016-06-30 20:50:49 -0700360/* TPM2_Clear command does not require paramaters. */
361static void marshal_clear(void **buffer, size_t *buffer_space)
362{
363 const uint32_t handle[] = { TPM_RH_PLATFORM };
364
365 marshal_common_session_header(buffer, handle,
366 ARRAY_SIZE(handle), buffer_space);
367}
368
Vadim Bendebury627afc22016-06-19 12:13:18 -0700369static void marshal_selftest(void **buffer,
370 struct tpm2_self_test *command_body,
Vadim Bendeburybc927102016-07-07 10:52:46 -0700371 size_t *buffer_space)
Vadim Bendebury627afc22016-06-19 12:13:18 -0700372{
373 marshal_u8(buffer, command_body->yes_no, buffer_space);
374}
375
Aaron Durbinf56c7782017-01-10 17:44:42 -0600376static void marshal_hierarchy_control(void **buffer,
377 struct tpm2_hierarchy_control_cmd *command_body,
378 size_t *buffer_space)
379{
380 struct tpm2_session_header session_header;
381
382 car_set_var(tpm_tag, TPM_ST_SESSIONS);
383
384 marshal_TPM_HANDLE(buffer, TPM_RH_PLATFORM, buffer_space);
385 memset(&session_header, 0, sizeof(session_header));
386 session_header.session_handle = TPM_RS_PW;
387 marshal_session_header(buffer, &session_header, buffer_space);
388
389 marshal_TPM_HANDLE(buffer, command_body->enable, buffer_space);
390 marshal_u8(buffer, command_body->state, buffer_space);
391}
392
Aaron Durbineeb77372017-03-08 11:23:11 -0600393static void marshal_cr50_vendor_command(void **buffer, void *command_body,
394 size_t *buffer_space)
395{
396 uint16_t *sub_command;
397
398 /* Ensure at least the sub command can fit in the body and there's
399 valid pointer. Could be reading past the buffer... */
400 if (command_body == NULL || *buffer_space < sizeof(uint16_t)) {
401 *buffer_space = 0;
402 return;
403 }
404
405 sub_command = command_body;
406
407 switch (*sub_command) {
408 case TPM2_CR50_SUB_CMD_NVMEM_ENABLE_COMMITS:
409 marshal_u16(buffer, *sub_command, buffer_space);
410 break;
411 default:
412 /* Unsupported subcommand. */
413 printk(BIOS_WARNING, "Unsupported cr50 subcommand: 0x%04x\n",
414 *sub_command);
415 *buffer_space = 0;
416 break;
417 }
418}
419
Vadim Bendebury627afc22016-06-19 12:13:18 -0700420int tpm_marshal_command(TPM_CC command, void *tpm_command_body,
Vadim Bendeburybc927102016-07-07 10:52:46 -0700421 void *buffer, size_t buffer_size)
Vadim Bendebury627afc22016-06-19 12:13:18 -0700422{
423 void *cmd_body = (uint8_t *)buffer + sizeof(struct tpm_header);
Vadim Bendeburybc927102016-07-07 10:52:46 -0700424 size_t max_body_size = buffer_size - sizeof(struct tpm_header);
425 size_t body_size = max_body_size;
Vadim Bendebury627afc22016-06-19 12:13:18 -0700426
427 /* Will be modified when marshaling some commands. */
Victor Prupisf7060202016-08-19 10:45:04 -0700428 car_set_var(tpm_tag, TPM_ST_NO_SESSIONS);
Vadim Bendebury627afc22016-06-19 12:13:18 -0700429
430 switch (command) {
431 case TPM2_Startup:
432 marshal_startup(&cmd_body, tpm_command_body, &body_size);
433 break;
434
435 case TPM2_GetCapability:
436 marshal_get_capability(&cmd_body, tpm_command_body,
437 &body_size);
438 break;
439
440 case TPM2_NV_Read:
441 marshal_nv_read(&cmd_body, tpm_command_body, &body_size);
442 break;
443
444 case TPM2_NV_DefineSpace:
445 marshal_nv_define_space(&cmd_body,
446 tpm_command_body, &body_size);
447 break;
448
449 case TPM2_NV_Write:
450 marshal_nv_write(&cmd_body, tpm_command_body, &body_size);
451 break;
452
Vadim Bendebury4c0851c2016-07-03 17:08:10 -0700453 case TPM2_NV_WriteLock:
454 marshal_nv_write_lock(&cmd_body, tpm_command_body, &body_size);
455 break;
456
Vadim Bendebury627afc22016-06-19 12:13:18 -0700457 case TPM2_SelfTest:
458 marshal_selftest(&cmd_body, tpm_command_body, &body_size);
459 break;
460
Aaron Durbinf56c7782017-01-10 17:44:42 -0600461 case TPM2_Hierarchy_Control:
462 marshal_hierarchy_control(&cmd_body, tpm_command_body,
463 &body_size);
464 break;
465
Vadim Bendebury6acb9a62016-06-30 20:50:49 -0700466 case TPM2_Clear:
467 marshal_clear(&cmd_body, &body_size);
468 break;
469
Vadim Bendeburyf5ef6992016-07-03 22:20:17 -0700470 case TPM2_PCR_Extend:
471 marshal_pcr_extend(&cmd_body, tpm_command_body, &body_size);
472 break;
473
Aaron Durbineeb77372017-03-08 11:23:11 -0600474 case TPM2_CR50_VENDOR_COMMAND:
475 marshal_cr50_vendor_command(&cmd_body, tpm_command_body,
476 &body_size);
477 break;
478
Vadim Bendebury627afc22016-06-19 12:13:18 -0700479 default:
Vadim Bendeburybc927102016-07-07 10:52:46 -0700480 body_size = 0;
Vadim Bendebury627afc22016-06-19 12:13:18 -0700481 printk(BIOS_INFO, "%s:%d:Request to marshal unsupported command %#x\n",
482 __FILE__, __LINE__, command);
483 }
484
485 if (body_size > 0) {
Vadim Bendeburyd9137d52016-07-07 14:27:41 -0700486 size_t marshaled_size;
487 size_t header_room = sizeof(struct tpm_header);
Vadim Bendebury627afc22016-06-19 12:13:18 -0700488
489 /* See how much room was taken by marshaling. */
Vadim Bendeburyd9137d52016-07-07 14:27:41 -0700490 marshaled_size = max_body_size - body_size;
Vadim Bendebury627afc22016-06-19 12:13:18 -0700491
Vadim Bendeburyd9137d52016-07-07 14:27:41 -0700492 /* Total size includes the header size. */
493 marshaled_size += sizeof(struct tpm_header);
Vadim Bendebury627afc22016-06-19 12:13:18 -0700494
Victor Prupisf7060202016-08-19 10:45:04 -0700495 uint16_t tpm_tag_value = car_get_var(tpm_tag);
496
497 marshal_u16(&buffer, tpm_tag_value, &header_room);
Vadim Bendeburyd9137d52016-07-07 14:27:41 -0700498 marshal_u32(&buffer, marshaled_size, &header_room);
499 marshal_u32(&buffer, command, &header_room);
500 return marshaled_size;
Vadim Bendebury627afc22016-06-19 12:13:18 -0700501 }
502
Vadim Bendeburyd9137d52016-07-07 14:27:41 -0700503 return -1;
Vadim Bendebury627afc22016-06-19 12:13:18 -0700504}
505
506static void unmarshal_get_capability(void **buffer, int *size,
507 struct get_cap_response *gcr)
508{
509 int i;
510
511 gcr->more_data = unmarshal_u8(buffer, size);
512
513 gcr->cd.capability = unmarshal_TPM_CAP(buffer, size);
514 if (*size < 0)
515 return;
516
517 switch (gcr->cd.capability) {
518 case TPM_CAP_TPM_PROPERTIES:
519 gcr->cd.data.tpmProperties.count =
520 unmarshal_u32(buffer, size);
521 if (*size < 0)
522 return;
523 if (gcr->cd.data.tpmProperties.count > ARRAY_SIZE
524 (gcr->cd.data.tpmProperties.tpmProperty)) {
525 printk(BIOS_INFO, "%s:%s:%d - %d - too many properties\n",
526 __FILE__, __func__, __LINE__,
527 gcr->cd.data.tpmProperties.count);
528 *size = -1;
529 return;
530 }
531 for (i = 0; i < gcr->cd.data.tpmProperties.count; i++) {
532 TPMS_TAGGED_PROPERTY *pp;
533
534 pp = gcr->cd.data.tpmProperties.tpmProperty + i;
535 pp->property = unmarshal_TPM_PT(buffer, size);
536 pp->value = unmarshal_u32(buffer, size);
537 }
538 break;
539 default:
540 printk(BIOS_ERR,
541 "%s:%d - unable to unmarshal capability response",
542 __func__, __LINE__);
543 printk(BIOS_ERR, " for %d\n", gcr->cd.capability);
544 *size = -1;
545 break;
546 }
547}
548
549static void unmarshal_TPM2B_MAX_NV_BUFFER(void **buffer,
550 int *size,
551 TPM2B_MAX_NV_BUFFER *nv_buffer)
552{
553 nv_buffer->t.size = unmarshal_u16(buffer, size);
Vadim Bendeburybc927102016-07-07 10:52:46 -0700554 if ((*size < 0) || (nv_buffer->t.size > *size)) {
Vadim Bendebury627afc22016-06-19 12:13:18 -0700555 printk(BIOS_ERR, "%s:%d - "
556 "size mismatch: expected %d, remaining %d\n",
557 __func__, __LINE__, nv_buffer->t.size, *size);
Vadim Bendeburybc927102016-07-07 10:52:46 -0700558 *size = -1;
Vadim Bendebury627afc22016-06-19 12:13:18 -0700559 return;
560 }
561
562 nv_buffer->t.buffer = *buffer;
563
564 *buffer = ((uint8_t *)(*buffer)) + nv_buffer->t.size;
565 *size -= nv_buffer->t.size;
566}
567
568static void unmarshal_nv_read(void **buffer, int *size,
569 struct nv_read_response *nvr)
570{
571 /* Total size of the parameter field. */
572 nvr->params_size = unmarshal_u32(buffer, size);
573 unmarshal_TPM2B_MAX_NV_BUFFER(buffer, size, &nvr->buffer);
574
575 if (nvr->params_size !=
576 (nvr->buffer.t.size + sizeof(nvr->buffer.t.size))) {
577 printk(BIOS_ERR,
578 "%s:%d - parameter/buffer %d/%d size mismatch",
579 __func__, __LINE__, nvr->params_size,
580 nvr->buffer.t.size);
581 return;
582 }
583
584 if (*size < 0)
585 return;
586 /*
587 * Let's ignore the authorisation section. It should be 5 bytes total,
588 * just confirm that this is the case and report any discrepancy.
589 */
590 if (*size != 5)
591 printk(BIOS_ERR,
592 "%s:%d - unexpected authorisation seciton size %d\n",
593 __func__, __LINE__, *size);
594
595 *buffer = ((uint8_t *)(*buffer)) + *size;
596 *size = 0;
597}
598
599struct tpm2_response *tpm_unmarshal_response(TPM_CC command,
600 void *response_body,
Vadim Bendeburybc927102016-07-07 10:52:46 -0700601 size_t in_size)
Vadim Bendebury627afc22016-06-19 12:13:18 -0700602{
Duncan Laurie4a560762016-09-01 16:09:43 -0700603 static struct tpm2_response tpm2_static_resp CAR_GLOBAL;
604 struct tpm2_response *tpm2_resp = car_get_var_ptr(&tpm2_static_resp);
Vadim Bendeburybc927102016-07-07 10:52:46 -0700605 /*
606 * Should be 0 when done, positive and negaitive values indicate
607 * unmarshaling errors.
608 */
609 int cr_size = in_size;
Vadim Bendebury627afc22016-06-19 12:13:18 -0700610
Vadim Bendeburybc927102016-07-07 10:52:46 -0700611 if ((cr_size < 0) || (in_size < sizeof(struct tpm_header)))
Vadim Bendebury627afc22016-06-19 12:13:18 -0700612 return NULL;
613
Duncan Laurie4a560762016-09-01 16:09:43 -0700614 tpm2_resp->hdr.tpm_tag = unmarshal_u16(&response_body, &cr_size);
615 tpm2_resp->hdr.tpm_size = unmarshal_u32(&response_body, &cr_size);
616 tpm2_resp->hdr.tpm_code = unmarshal_TPM_CC(&response_body, &cr_size);
Vadim Bendebury627afc22016-06-19 12:13:18 -0700617
618 if (!cr_size) {
Duncan Laurie4a560762016-09-01 16:09:43 -0700619 if (tpm2_resp->hdr.tpm_size != sizeof(tpm2_resp->hdr))
Vadim Bendebury627afc22016-06-19 12:13:18 -0700620 printk(BIOS_ERR,
621 "%s: size mismatch in response to command %#x\n",
622 __func__, command);
Duncan Laurie4a560762016-09-01 16:09:43 -0700623 return tpm2_resp;
Vadim Bendebury627afc22016-06-19 12:13:18 -0700624 }
625
626 switch (command) {
627 case TPM2_Startup:
628 break;
629
630 case TPM2_GetCapability:
631 unmarshal_get_capability(&response_body, &cr_size,
Duncan Laurie4a560762016-09-01 16:09:43 -0700632 &tpm2_resp->gc);
Vadim Bendebury627afc22016-06-19 12:13:18 -0700633 break;
634
635 case TPM2_NV_Read:
636 unmarshal_nv_read(&response_body, &cr_size,
Duncan Laurie4a560762016-09-01 16:09:43 -0700637 &tpm2_resp->nvr);
Vadim Bendebury627afc22016-06-19 12:13:18 -0700638 break;
639
Aaron Durbinf56c7782017-01-10 17:44:42 -0600640 case TPM2_Hierarchy_Control:
Vadim Bendebury6acb9a62016-06-30 20:50:49 -0700641 case TPM2_Clear:
Vadim Bendebury627afc22016-06-19 12:13:18 -0700642 case TPM2_NV_DefineSpace:
643 case TPM2_NV_Write:
Vadim Bendebury4c0851c2016-07-03 17:08:10 -0700644 case TPM2_NV_WriteLock:
Vadim Bendeburyf5ef6992016-07-03 22:20:17 -0700645 case TPM2_PCR_Extend:
Vadim Bendebury627afc22016-06-19 12:13:18 -0700646 /* Session data included in response can be safely ignored. */
647 cr_size = 0;
648 break;
649
Aaron Durbineeb77372017-03-08 11:23:11 -0600650 case TPM2_CR50_VENDOR_COMMAND:
651 /* Assume no other data returned for the time being. */
652 cr_size = 0;
653 break;
654
Vadim Bendebury627afc22016-06-19 12:13:18 -0700655 default:
656 {
657 int i;
658
659 printk(BIOS_INFO, "%s:%d:"
660 "Request to unmarshal unexpected command %#x,"
661 " code %#x",
662 __func__, __LINE__, command,
Duncan Laurie4a560762016-09-01 16:09:43 -0700663 tpm2_resp->hdr.tpm_code);
Vadim Bendebury627afc22016-06-19 12:13:18 -0700664
665 for (i = 0; i < cr_size; i++) {
666 if (!(i % 16))
667 printk(BIOS_INFO, "\n");
668 printk(BIOS_INFO, "%2.2x ",
669 ((uint8_t *)response_body)[i]);
670 }
671 }
672 printk(BIOS_INFO, "\n");
673 return NULL;
674 }
675
676 if (cr_size) {
677 printk(BIOS_INFO,
678 "%s:%d got %d bytes back in response to %#x,"
679 " failed to parse (%d)\n",
Duncan Laurie4a560762016-09-01 16:09:43 -0700680 __func__, __LINE__, tpm2_resp->hdr.tpm_size,
Vadim Bendebury627afc22016-06-19 12:13:18 -0700681 command, cr_size);
682 return NULL;
683 }
684
685 /* The entire message have been parsed. */
Duncan Laurie4a560762016-09-01 16:09:43 -0700686 return tpm2_resp;
Vadim Bendebury627afc22016-06-19 12:13:18 -0700687}