blob: 5aad276dbb9c948ea21273525627945ec9490f1b [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
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
14static 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 Bendeburybc927102016-07-07 10:52:46 -070024 * has been extracted from the receive buffer.
Vadim Bendebury627afc22016-06-19 12:13:18 -070025 */
26static uint16_t unmarshal_u16(void **buffer, int *buffer_space)
27{
28 uint16_t value;
29
Vadim Bendeburybc927102016-07-07 10:52:46 -070030 if (*buffer_space < 0)
31 return 0;
32
Vadim Bendebury627afc22016-06-19 12:13:18 -070033 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
45static uint16_t unmarshal_u32(void **buffer, int *buffer_space)
46{
47 uint32_t value;
48
Vadim Bendeburybc927102016-07-07 10:52:46 -070049 if (*buffer_space < 0)
50 return 0;
51
Vadim Bendebury627afc22016-06-19 12:13:18 -070052 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
64static uint8_t unmarshal_u8(void **buffer, int *buffer_space)
65{
66 uint8_t value;
67
Vadim Bendeburybc927102016-07-07 10:52:46 -070068 if (*buffer_space < 0)
69 return 0;
70
Vadim Bendebury627afc22016-06-19 12:13:18 -070071 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 */
98static void marshal_blob(void **buffer, void *blob,
Vadim Bendeburybc927102016-07-07 10:52:46 -070099 size_t blob_size, size_t *buffer_space)
Vadim Bendebury627afc22016-06-19 12:13:18 -0700100{
101 if (*buffer_space < blob_size) {
102 *buffer_space = -1;
103 return;
104 }
105
106 memcpy(*buffer, blob, blob_size);
Vadim Bendeburybc927102016-07-07 10:52:46 -0700107 *buffer_space -= blob_size;
Vadim Bendebury627afc22016-06-19 12:13:18 -0700108 *buffer = (void *)((uintptr_t)(*buffer) + blob_size);
109}
110
Vadim Bendeburybc927102016-07-07 10:52:46 -0700111static void marshal_u8(void **buffer, uint8_t value, size_t *buffer_space)
Vadim Bendebury627afc22016-06-19 12:13:18 -0700112{
113 uint8_t *bp = *buffer;
114
115 if (*buffer_space < sizeof(value)) {
Vadim Bendeburybc927102016-07-07 10:52:46 -0700116 *buffer_space = 0;
Vadim Bendebury627afc22016-06-19 12:13:18 -0700117 return;
118 }
119
120 *bp++ = value;
121 *buffer = bp;
122 *buffer_space -= sizeof(value);
123}
124
Vadim Bendeburybc927102016-07-07 10:52:46 -0700125static void marshal_u16(void **buffer, uint16_t value, size_t *buffer_space)
Vadim Bendebury627afc22016-06-19 12:13:18 -0700126{
127 if (*buffer_space < sizeof(value)) {
Vadim Bendeburybc927102016-07-07 10:52:46 -0700128 *buffer_space = 0;
Vadim Bendebury627afc22016-06-19 12:13:18 -0700129 return;
130 }
131 write_be16(*buffer, value);
132 *buffer = (void *)((uintptr_t)(*buffer) + sizeof(value));
133 *buffer_space -= sizeof(value);
134}
135
Vadim Bendeburybc927102016-07-07 10:52:46 -0700136static void marshal_u32(void **buffer, uint32_t value, size_t *buffer_space)
Vadim Bendebury627afc22016-06-19 12:13:18 -0700137{
138 if (*buffer_space < sizeof(value)) {
Vadim Bendeburybc927102016-07-07 10:52:46 -0700139 *buffer_space = 0;
Vadim Bendebury627afc22016-06-19 12:13:18 -0700140 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
154static void marshal_startup(void **buffer,
155 struct tpm2_startup *cmd_body,
Vadim Bendeburybc927102016-07-07 10:52:46 -0700156 size_t *buffer_space)
Vadim Bendebury627afc22016-06-19 12:13:18 -0700157{
158 marshal_u16(buffer, cmd_body->startup_type, buffer_space);
159}
160
161static void marshal_get_capability(void **buffer,
162 struct tpm2_get_capability *cmd_body,
Vadim Bendeburybc927102016-07-07 10:52:46 -0700163 size_t *buffer_space)
Vadim Bendebury627afc22016-06-19 12:13:18 -0700164{
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
170static void marshal_TPM2B(void **buffer,
171 TPM2B *data,
Vadim Bendeburybc927102016-07-07 10:52:46 -0700172 size_t *buffer_space)
Vadim Bendebury627afc22016-06-19 12:13:18 -0700173{
174 size_t total_size = data->size + sizeof(data->size);
175
176 if (total_size > *buffer_space) {
Vadim Bendeburybc927102016-07-07 10:52:46 -0700177 *buffer_space = 0;
Vadim Bendebury627afc22016-06-19 12:13:18 -0700178 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
186static void marshal_TPMA_NV(void **buffer,
187 TPMA_NV *nv,
Vadim Bendeburybc927102016-07-07 10:52:46 -0700188 size_t *buffer_space)
Vadim Bendebury627afc22016-06-19 12:13:18 -0700189{
190 marshal_u32(buffer, *((uint32_t *)nv), buffer_space);
191}
192
193static void marshal_TPMS_NV_PUBLIC(void **buffer,
194 TPMS_NV_PUBLIC *nvpub,
Vadim Bendeburybc927102016-07-07 10:52:46 -0700195 size_t *buffer_space)
Vadim Bendebury627afc22016-06-19 12:13:18 -0700196{
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
204static void marshal_session_header(void **buffer,
205 struct tpm2_session_header *session_header,
Vadim Bendeburybc927102016-07-07 10:52:46 -0700206 size_t *buffer_space)
Vadim Bendebury627afc22016-06-19 12:13:18 -0700207{
Vadim Bendeburybc927102016-07-07 10:52:46 -0700208 size_t base_size;
Vadim Bendebury627afc22016-06-19 12:13:18 -0700209 void *size_location = *buffer;
210
211 /* Skip room for the session header size. */
Vadim Bendeburybc927102016-07-07 10:52:46 -0700212 if (*buffer_space < sizeof(uint32_t)) {
213 *buffer_space = 0;
214 return;
215 }
216
Vadim Bendebury627afc22016-06-19 12:13:18 -0700217 *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 Bendeburybc927102016-07-07 10:52:46 -0700231 if (!*buffer_space)
Vadim Bendebury627afc22016-06-19 12:13:18 -0700232 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
Vadim Bendeburyebba4d72016-07-07 11:04:06 -0700238/*
239 * Common session header can include one or two handles and an empty
240 * session_header structure.
241 */
242static void marshal_common_session_header(void **buffer,
243 const uint32_t *handles,
244 size_t handle_count,
245 size_t *buffer_space)
246{
247 int i;
248 struct tpm2_session_header session_header;
249
250 tpm_tag = TPM_ST_SESSIONS;
251
252 for (i = 0; i < handle_count; i++)
253 marshal_TPM_HANDLE(buffer, handles[i], buffer_space);
254
255 memset(&session_header, 0, sizeof(session_header));
256 session_header.session_handle = TPM_RS_PW;
257 marshal_session_header(buffer, &session_header, buffer_space);
258}
259
Vadim Bendebury627afc22016-06-19 12:13:18 -0700260static void marshal_nv_define_space(void **buffer,
261 struct tpm2_nv_define_space_cmd *nvd_in,
Vadim Bendeburybc927102016-07-07 10:52:46 -0700262 size_t *buffer_space)
Vadim Bendebury627afc22016-06-19 12:13:18 -0700263{
264 void *size_location;
Vadim Bendeburybc927102016-07-07 10:52:46 -0700265 size_t base_size;
266 size_t sizeof_nv_public_size = sizeof(uint16_t);
Vadim Bendeburyebba4d72016-07-07 11:04:06 -0700267 const uint32_t handle[] = { TPM_RH_PLATFORM };
Vadim Bendebury627afc22016-06-19 12:13:18 -0700268
Vadim Bendeburyebba4d72016-07-07 11:04:06 -0700269 marshal_common_session_header(buffer, handle,
270 ARRAY_SIZE(handle), buffer_space);
Vadim Bendebury627afc22016-06-19 12:13:18 -0700271 marshal_TPM2B(buffer, &nvd_in->auth.b, buffer_space);
272
273 /* This is where the TPMS_NV_PUBLIC size will be stored. */
274 size_location = *buffer;
275
276 /* Allocate room for the size. */
277 *buffer = ((uint8_t *)(*buffer)) + sizeof(uint16_t);
Vadim Bendeburybc927102016-07-07 10:52:46 -0700278
279 if (*buffer_space < sizeof_nv_public_size) {
280 *buffer_space = 0;
281 return;
282 }
283 *buffer_space -= sizeof_nv_public_size;
Vadim Bendebury627afc22016-06-19 12:13:18 -0700284 base_size = *buffer_space;
285
286 marshal_TPMS_NV_PUBLIC(buffer, &nvd_in->publicInfo, buffer_space);
Vadim Bendeburybc927102016-07-07 10:52:46 -0700287 if (!*buffer_space)
288 return;
289
Vadim Bendebury627afc22016-06-19 12:13:18 -0700290 base_size = base_size - *buffer_space;
Vadim Bendeburybc927102016-07-07 10:52:46 -0700291 marshal_u16(&size_location, base_size, &sizeof_nv_public_size);
Vadim Bendebury627afc22016-06-19 12:13:18 -0700292}
293
294static void marshal_nv_write(void **buffer,
295 struct tpm2_nv_write_cmd *command_body,
Vadim Bendeburybc927102016-07-07 10:52:46 -0700296 size_t *buffer_space)
Vadim Bendebury627afc22016-06-19 12:13:18 -0700297{
Vadim Bendeburyebba4d72016-07-07 11:04:06 -0700298 uint32_t handles[] = { TPM_RH_PLATFORM, command_body->nvIndex };
Vadim Bendebury627afc22016-06-19 12:13:18 -0700299
Vadim Bendeburyebba4d72016-07-07 11:04:06 -0700300 marshal_common_session_header(buffer, handles,
301 ARRAY_SIZE(handles), buffer_space);
Vadim Bendebury627afc22016-06-19 12:13:18 -0700302 marshal_TPM2B(buffer, &command_body->data.b, buffer_space);
303 marshal_u16(buffer, command_body->offset, buffer_space);
304}
305
306static void marshal_nv_read(void **buffer,
307 struct tpm2_nv_read_cmd *command_body,
Vadim Bendeburybc927102016-07-07 10:52:46 -0700308 size_t *buffer_space)
Vadim Bendebury627afc22016-06-19 12:13:18 -0700309{
Vadim Bendeburyebba4d72016-07-07 11:04:06 -0700310 uint32_t handles[] = { TPM_RH_PLATFORM, command_body->nvIndex };
Vadim Bendebury627afc22016-06-19 12:13:18 -0700311
Vadim Bendeburyebba4d72016-07-07 11:04:06 -0700312 marshal_common_session_header(buffer, handles,
313 ARRAY_SIZE(handles), buffer_space);
Vadim Bendebury627afc22016-06-19 12:13:18 -0700314 marshal_u16(buffer, command_body->size, buffer_space);
315 marshal_u16(buffer, command_body->offset, buffer_space);
316}
317
318static void marshal_selftest(void **buffer,
319 struct tpm2_self_test *command_body,
Vadim Bendeburybc927102016-07-07 10:52:46 -0700320 size_t *buffer_space)
Vadim Bendebury627afc22016-06-19 12:13:18 -0700321{
322 marshal_u8(buffer, command_body->yes_no, buffer_space);
323}
324
325int tpm_marshal_command(TPM_CC command, void *tpm_command_body,
Vadim Bendeburybc927102016-07-07 10:52:46 -0700326 void *buffer, size_t buffer_size)
Vadim Bendebury627afc22016-06-19 12:13:18 -0700327{
328 void *cmd_body = (uint8_t *)buffer + sizeof(struct tpm_header);
Vadim Bendeburybc927102016-07-07 10:52:46 -0700329 size_t max_body_size = buffer_size - sizeof(struct tpm_header);
330 size_t body_size = max_body_size;
Vadim Bendebury627afc22016-06-19 12:13:18 -0700331
332 /* Will be modified when marshaling some commands. */
333 tpm_tag = TPM_ST_NO_SESSIONS;
334
335 switch (command) {
336 case TPM2_Startup:
337 marshal_startup(&cmd_body, tpm_command_body, &body_size);
338 break;
339
340 case TPM2_GetCapability:
341 marshal_get_capability(&cmd_body, tpm_command_body,
342 &body_size);
343 break;
344
345 case TPM2_NV_Read:
346 marshal_nv_read(&cmd_body, tpm_command_body, &body_size);
347 break;
348
349 case TPM2_NV_DefineSpace:
350 marshal_nv_define_space(&cmd_body,
351 tpm_command_body, &body_size);
352 break;
353
354 case TPM2_NV_Write:
355 marshal_nv_write(&cmd_body, tpm_command_body, &body_size);
356 break;
357
358 case TPM2_SelfTest:
359 marshal_selftest(&cmd_body, tpm_command_body, &body_size);
360 break;
361
362 default:
Vadim Bendeburybc927102016-07-07 10:52:46 -0700363 body_size = 0;
Vadim Bendebury627afc22016-06-19 12:13:18 -0700364 printk(BIOS_INFO, "%s:%d:Request to marshal unsupported command %#x\n",
365 __FILE__, __LINE__, command);
366 }
367
368 if (body_size > 0) {
Vadim Bendeburyd9137d52016-07-07 14:27:41 -0700369 size_t marshaled_size;
370 size_t header_room = sizeof(struct tpm_header);
Vadim Bendebury627afc22016-06-19 12:13:18 -0700371
372 /* See how much room was taken by marshaling. */
Vadim Bendeburyd9137d52016-07-07 14:27:41 -0700373 marshaled_size = max_body_size - body_size;
Vadim Bendebury627afc22016-06-19 12:13:18 -0700374
Vadim Bendeburyd9137d52016-07-07 14:27:41 -0700375 /* Total size includes the header size. */
376 marshaled_size += sizeof(struct tpm_header);
Vadim Bendebury627afc22016-06-19 12:13:18 -0700377
Vadim Bendeburyd9137d52016-07-07 14:27:41 -0700378 marshal_u16(&buffer, tpm_tag, &header_room);
379 marshal_u32(&buffer, marshaled_size, &header_room);
380 marshal_u32(&buffer, command, &header_room);
381 return marshaled_size;
Vadim Bendebury627afc22016-06-19 12:13:18 -0700382 }
383
Vadim Bendeburyd9137d52016-07-07 14:27:41 -0700384 return -1;
Vadim Bendebury627afc22016-06-19 12:13:18 -0700385}
386
387static void unmarshal_get_capability(void **buffer, int *size,
388 struct get_cap_response *gcr)
389{
390 int i;
391
392 gcr->more_data = unmarshal_u8(buffer, size);
393
394 gcr->cd.capability = unmarshal_TPM_CAP(buffer, size);
395 if (*size < 0)
396 return;
397
398 switch (gcr->cd.capability) {
399 case TPM_CAP_TPM_PROPERTIES:
400 gcr->cd.data.tpmProperties.count =
401 unmarshal_u32(buffer, size);
402 if (*size < 0)
403 return;
404 if (gcr->cd.data.tpmProperties.count > ARRAY_SIZE
405 (gcr->cd.data.tpmProperties.tpmProperty)) {
406 printk(BIOS_INFO, "%s:%s:%d - %d - too many properties\n",
407 __FILE__, __func__, __LINE__,
408 gcr->cd.data.tpmProperties.count);
409 *size = -1;
410 return;
411 }
412 for (i = 0; i < gcr->cd.data.tpmProperties.count; i++) {
413 TPMS_TAGGED_PROPERTY *pp;
414
415 pp = gcr->cd.data.tpmProperties.tpmProperty + i;
416 pp->property = unmarshal_TPM_PT(buffer, size);
417 pp->value = unmarshal_u32(buffer, size);
418 }
419 break;
420 default:
421 printk(BIOS_ERR,
422 "%s:%d - unable to unmarshal capability response",
423 __func__, __LINE__);
424 printk(BIOS_ERR, " for %d\n", gcr->cd.capability);
425 *size = -1;
426 break;
427 }
428}
429
430static void unmarshal_TPM2B_MAX_NV_BUFFER(void **buffer,
431 int *size,
432 TPM2B_MAX_NV_BUFFER *nv_buffer)
433{
434 nv_buffer->t.size = unmarshal_u16(buffer, size);
Vadim Bendeburybc927102016-07-07 10:52:46 -0700435 if ((*size < 0) || (nv_buffer->t.size > *size)) {
Vadim Bendebury627afc22016-06-19 12:13:18 -0700436 printk(BIOS_ERR, "%s:%d - "
437 "size mismatch: expected %d, remaining %d\n",
438 __func__, __LINE__, nv_buffer->t.size, *size);
Vadim Bendeburybc927102016-07-07 10:52:46 -0700439 *size = -1;
Vadim Bendebury627afc22016-06-19 12:13:18 -0700440 return;
441 }
442
443 nv_buffer->t.buffer = *buffer;
444
445 *buffer = ((uint8_t *)(*buffer)) + nv_buffer->t.size;
446 *size -= nv_buffer->t.size;
447}
448
449static void unmarshal_nv_read(void **buffer, int *size,
450 struct nv_read_response *nvr)
451{
452 /* Total size of the parameter field. */
453 nvr->params_size = unmarshal_u32(buffer, size);
454 unmarshal_TPM2B_MAX_NV_BUFFER(buffer, size, &nvr->buffer);
455
456 if (nvr->params_size !=
457 (nvr->buffer.t.size + sizeof(nvr->buffer.t.size))) {
458 printk(BIOS_ERR,
459 "%s:%d - parameter/buffer %d/%d size mismatch",
460 __func__, __LINE__, nvr->params_size,
461 nvr->buffer.t.size);
462 return;
463 }
464
465 if (*size < 0)
466 return;
467 /*
468 * Let's ignore the authorisation section. It should be 5 bytes total,
469 * just confirm that this is the case and report any discrepancy.
470 */
471 if (*size != 5)
472 printk(BIOS_ERR,
473 "%s:%d - unexpected authorisation seciton size %d\n",
474 __func__, __LINE__, *size);
475
476 *buffer = ((uint8_t *)(*buffer)) + *size;
477 *size = 0;
478}
479
480struct tpm2_response *tpm_unmarshal_response(TPM_CC command,
481 void *response_body,
Vadim Bendeburybc927102016-07-07 10:52:46 -0700482 size_t in_size)
Vadim Bendebury627afc22016-06-19 12:13:18 -0700483{
484 static struct tpm2_response tpm2_resp;
Vadim Bendeburybc927102016-07-07 10:52:46 -0700485 /*
486 * Should be 0 when done, positive and negaitive values indicate
487 * unmarshaling errors.
488 */
489 int cr_size = in_size;
Vadim Bendebury627afc22016-06-19 12:13:18 -0700490
Vadim Bendeburybc927102016-07-07 10:52:46 -0700491 if ((cr_size < 0) || (in_size < sizeof(struct tpm_header)))
Vadim Bendebury627afc22016-06-19 12:13:18 -0700492 return NULL;
493
494 tpm2_resp.hdr.tpm_tag = unmarshal_u16(&response_body, &cr_size);
495 tpm2_resp.hdr.tpm_size = unmarshal_u32(&response_body, &cr_size);
496 tpm2_resp.hdr.tpm_code = unmarshal_TPM_CC(&response_body, &cr_size);
497
498 if (!cr_size) {
499 if (tpm2_resp.hdr.tpm_size != sizeof(tpm2_resp.hdr))
500 printk(BIOS_ERR,
501 "%s: size mismatch in response to command %#x\n",
502 __func__, command);
503 return &tpm2_resp;
504 }
505
506 switch (command) {
507 case TPM2_Startup:
508 break;
509
510 case TPM2_GetCapability:
511 unmarshal_get_capability(&response_body, &cr_size,
512 &tpm2_resp.gc);
513 break;
514
515 case TPM2_NV_Read:
516 unmarshal_nv_read(&response_body, &cr_size,
517 &tpm2_resp.nvr);
518 break;
519
520 case TPM2_NV_DefineSpace:
521 case TPM2_NV_Write:
522 /* Session data included in response can be safely ignored. */
523 cr_size = 0;
524 break;
525
526 default:
527 {
528 int i;
529
530 printk(BIOS_INFO, "%s:%d:"
531 "Request to unmarshal unexpected command %#x,"
532 " code %#x",
533 __func__, __LINE__, command,
534 tpm2_resp.hdr.tpm_code);
535
536 for (i = 0; i < cr_size; i++) {
537 if (!(i % 16))
538 printk(BIOS_INFO, "\n");
539 printk(BIOS_INFO, "%2.2x ",
540 ((uint8_t *)response_body)[i]);
541 }
542 }
543 printk(BIOS_INFO, "\n");
544 return NULL;
545 }
546
547 if (cr_size) {
548 printk(BIOS_INFO,
549 "%s:%d got %d bytes back in response to %#x,"
550 " failed to parse (%d)\n",
551 __func__, __LINE__, tpm2_resp.hdr.tpm_size,
552 command, cr_size);
553 return NULL;
554 }
555
556 /* The entire message have been parsed. */
557 return &tpm2_resp;
558}