blob: 00c8f7d9f977b8015becf5478b03684bc6be54a6 [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
Vadim Bendebury4c0851c2016-07-03 17:08:10 -0700306static void marshal_nv_write_lock(void **buffer,
307 struct tpm2_nv_write_lock_cmd *command_body,
308 size_t *buffer_space)
309{
310 uint32_t handles[] = { TPM_RH_PLATFORM, command_body->nvIndex };
311 marshal_common_session_header(buffer, handles,
312 ARRAY_SIZE(handles), buffer_space);
313}
314
Vadim Bendebury627afc22016-06-19 12:13:18 -0700315static void marshal_nv_read(void **buffer,
316 struct tpm2_nv_read_cmd *command_body,
Vadim Bendeburybc927102016-07-07 10:52:46 -0700317 size_t *buffer_space)
Vadim Bendebury627afc22016-06-19 12:13:18 -0700318{
Vadim Bendeburyebba4d72016-07-07 11:04:06 -0700319 uint32_t handles[] = { TPM_RH_PLATFORM, command_body->nvIndex };
Vadim Bendebury627afc22016-06-19 12:13:18 -0700320
Vadim Bendeburyebba4d72016-07-07 11:04:06 -0700321 marshal_common_session_header(buffer, handles,
322 ARRAY_SIZE(handles), buffer_space);
Vadim Bendebury627afc22016-06-19 12:13:18 -0700323 marshal_u16(buffer, command_body->size, buffer_space);
324 marshal_u16(buffer, command_body->offset, buffer_space);
325}
326
Vadim Bendebury6acb9a62016-06-30 20:50:49 -0700327/* TPM2_Clear command does not require paramaters. */
328static void marshal_clear(void **buffer, size_t *buffer_space)
329{
330 const uint32_t handle[] = { TPM_RH_PLATFORM };
331
332 marshal_common_session_header(buffer, handle,
333 ARRAY_SIZE(handle), buffer_space);
334}
335
Vadim Bendebury627afc22016-06-19 12:13:18 -0700336static void marshal_selftest(void **buffer,
337 struct tpm2_self_test *command_body,
Vadim Bendeburybc927102016-07-07 10:52:46 -0700338 size_t *buffer_space)
Vadim Bendebury627afc22016-06-19 12:13:18 -0700339{
340 marshal_u8(buffer, command_body->yes_no, buffer_space);
341}
342
343int tpm_marshal_command(TPM_CC command, void *tpm_command_body,
Vadim Bendeburybc927102016-07-07 10:52:46 -0700344 void *buffer, size_t buffer_size)
Vadim Bendebury627afc22016-06-19 12:13:18 -0700345{
346 void *cmd_body = (uint8_t *)buffer + sizeof(struct tpm_header);
Vadim Bendeburybc927102016-07-07 10:52:46 -0700347 size_t max_body_size = buffer_size - sizeof(struct tpm_header);
348 size_t body_size = max_body_size;
Vadim Bendebury627afc22016-06-19 12:13:18 -0700349
350 /* Will be modified when marshaling some commands. */
351 tpm_tag = TPM_ST_NO_SESSIONS;
352
353 switch (command) {
354 case TPM2_Startup:
355 marshal_startup(&cmd_body, tpm_command_body, &body_size);
356 break;
357
358 case TPM2_GetCapability:
359 marshal_get_capability(&cmd_body, tpm_command_body,
360 &body_size);
361 break;
362
363 case TPM2_NV_Read:
364 marshal_nv_read(&cmd_body, tpm_command_body, &body_size);
365 break;
366
367 case TPM2_NV_DefineSpace:
368 marshal_nv_define_space(&cmd_body,
369 tpm_command_body, &body_size);
370 break;
371
372 case TPM2_NV_Write:
373 marshal_nv_write(&cmd_body, tpm_command_body, &body_size);
374 break;
375
Vadim Bendebury4c0851c2016-07-03 17:08:10 -0700376 case TPM2_NV_WriteLock:
377 marshal_nv_write_lock(&cmd_body, tpm_command_body, &body_size);
378 break;
379
Vadim Bendebury627afc22016-06-19 12:13:18 -0700380 case TPM2_SelfTest:
381 marshal_selftest(&cmd_body, tpm_command_body, &body_size);
382 break;
383
Vadim Bendebury6acb9a62016-06-30 20:50:49 -0700384 case TPM2_Clear:
385 marshal_clear(&cmd_body, &body_size);
386 break;
387
Vadim Bendebury627afc22016-06-19 12:13:18 -0700388 default:
Vadim Bendeburybc927102016-07-07 10:52:46 -0700389 body_size = 0;
Vadim Bendebury627afc22016-06-19 12:13:18 -0700390 printk(BIOS_INFO, "%s:%d:Request to marshal unsupported command %#x\n",
391 __FILE__, __LINE__, command);
392 }
393
394 if (body_size > 0) {
Vadim Bendeburyd9137d52016-07-07 14:27:41 -0700395 size_t marshaled_size;
396 size_t header_room = sizeof(struct tpm_header);
Vadim Bendebury627afc22016-06-19 12:13:18 -0700397
398 /* See how much room was taken by marshaling. */
Vadim Bendeburyd9137d52016-07-07 14:27:41 -0700399 marshaled_size = max_body_size - body_size;
Vadim Bendebury627afc22016-06-19 12:13:18 -0700400
Vadim Bendeburyd9137d52016-07-07 14:27:41 -0700401 /* Total size includes the header size. */
402 marshaled_size += sizeof(struct tpm_header);
Vadim Bendebury627afc22016-06-19 12:13:18 -0700403
Vadim Bendeburyd9137d52016-07-07 14:27:41 -0700404 marshal_u16(&buffer, tpm_tag, &header_room);
405 marshal_u32(&buffer, marshaled_size, &header_room);
406 marshal_u32(&buffer, command, &header_room);
407 return marshaled_size;
Vadim Bendebury627afc22016-06-19 12:13:18 -0700408 }
409
Vadim Bendeburyd9137d52016-07-07 14:27:41 -0700410 return -1;
Vadim Bendebury627afc22016-06-19 12:13:18 -0700411}
412
413static void unmarshal_get_capability(void **buffer, int *size,
414 struct get_cap_response *gcr)
415{
416 int i;
417
418 gcr->more_data = unmarshal_u8(buffer, size);
419
420 gcr->cd.capability = unmarshal_TPM_CAP(buffer, size);
421 if (*size < 0)
422 return;
423
424 switch (gcr->cd.capability) {
425 case TPM_CAP_TPM_PROPERTIES:
426 gcr->cd.data.tpmProperties.count =
427 unmarshal_u32(buffer, size);
428 if (*size < 0)
429 return;
430 if (gcr->cd.data.tpmProperties.count > ARRAY_SIZE
431 (gcr->cd.data.tpmProperties.tpmProperty)) {
432 printk(BIOS_INFO, "%s:%s:%d - %d - too many properties\n",
433 __FILE__, __func__, __LINE__,
434 gcr->cd.data.tpmProperties.count);
435 *size = -1;
436 return;
437 }
438 for (i = 0; i < gcr->cd.data.tpmProperties.count; i++) {
439 TPMS_TAGGED_PROPERTY *pp;
440
441 pp = gcr->cd.data.tpmProperties.tpmProperty + i;
442 pp->property = unmarshal_TPM_PT(buffer, size);
443 pp->value = unmarshal_u32(buffer, size);
444 }
445 break;
446 default:
447 printk(BIOS_ERR,
448 "%s:%d - unable to unmarshal capability response",
449 __func__, __LINE__);
450 printk(BIOS_ERR, " for %d\n", gcr->cd.capability);
451 *size = -1;
452 break;
453 }
454}
455
456static void unmarshal_TPM2B_MAX_NV_BUFFER(void **buffer,
457 int *size,
458 TPM2B_MAX_NV_BUFFER *nv_buffer)
459{
460 nv_buffer->t.size = unmarshal_u16(buffer, size);
Vadim Bendeburybc927102016-07-07 10:52:46 -0700461 if ((*size < 0) || (nv_buffer->t.size > *size)) {
Vadim Bendebury627afc22016-06-19 12:13:18 -0700462 printk(BIOS_ERR, "%s:%d - "
463 "size mismatch: expected %d, remaining %d\n",
464 __func__, __LINE__, nv_buffer->t.size, *size);
Vadim Bendeburybc927102016-07-07 10:52:46 -0700465 *size = -1;
Vadim Bendebury627afc22016-06-19 12:13:18 -0700466 return;
467 }
468
469 nv_buffer->t.buffer = *buffer;
470
471 *buffer = ((uint8_t *)(*buffer)) + nv_buffer->t.size;
472 *size -= nv_buffer->t.size;
473}
474
475static void unmarshal_nv_read(void **buffer, int *size,
476 struct nv_read_response *nvr)
477{
478 /* Total size of the parameter field. */
479 nvr->params_size = unmarshal_u32(buffer, size);
480 unmarshal_TPM2B_MAX_NV_BUFFER(buffer, size, &nvr->buffer);
481
482 if (nvr->params_size !=
483 (nvr->buffer.t.size + sizeof(nvr->buffer.t.size))) {
484 printk(BIOS_ERR,
485 "%s:%d - parameter/buffer %d/%d size mismatch",
486 __func__, __LINE__, nvr->params_size,
487 nvr->buffer.t.size);
488 return;
489 }
490
491 if (*size < 0)
492 return;
493 /*
494 * Let's ignore the authorisation section. It should be 5 bytes total,
495 * just confirm that this is the case and report any discrepancy.
496 */
497 if (*size != 5)
498 printk(BIOS_ERR,
499 "%s:%d - unexpected authorisation seciton size %d\n",
500 __func__, __LINE__, *size);
501
502 *buffer = ((uint8_t *)(*buffer)) + *size;
503 *size = 0;
504}
505
506struct tpm2_response *tpm_unmarshal_response(TPM_CC command,
507 void *response_body,
Vadim Bendeburybc927102016-07-07 10:52:46 -0700508 size_t in_size)
Vadim Bendebury627afc22016-06-19 12:13:18 -0700509{
510 static struct tpm2_response tpm2_resp;
Vadim Bendeburybc927102016-07-07 10:52:46 -0700511 /*
512 * Should be 0 when done, positive and negaitive values indicate
513 * unmarshaling errors.
514 */
515 int cr_size = in_size;
Vadim Bendebury627afc22016-06-19 12:13:18 -0700516
Vadim Bendeburybc927102016-07-07 10:52:46 -0700517 if ((cr_size < 0) || (in_size < sizeof(struct tpm_header)))
Vadim Bendebury627afc22016-06-19 12:13:18 -0700518 return NULL;
519
520 tpm2_resp.hdr.tpm_tag = unmarshal_u16(&response_body, &cr_size);
521 tpm2_resp.hdr.tpm_size = unmarshal_u32(&response_body, &cr_size);
522 tpm2_resp.hdr.tpm_code = unmarshal_TPM_CC(&response_body, &cr_size);
523
524 if (!cr_size) {
525 if (tpm2_resp.hdr.tpm_size != sizeof(tpm2_resp.hdr))
526 printk(BIOS_ERR,
527 "%s: size mismatch in response to command %#x\n",
528 __func__, command);
529 return &tpm2_resp;
530 }
531
532 switch (command) {
533 case TPM2_Startup:
534 break;
535
536 case TPM2_GetCapability:
537 unmarshal_get_capability(&response_body, &cr_size,
538 &tpm2_resp.gc);
539 break;
540
541 case TPM2_NV_Read:
542 unmarshal_nv_read(&response_body, &cr_size,
543 &tpm2_resp.nvr);
544 break;
545
Vadim Bendebury6acb9a62016-06-30 20:50:49 -0700546 case TPM2_Clear:
Vadim Bendebury627afc22016-06-19 12:13:18 -0700547 case TPM2_NV_DefineSpace:
548 case TPM2_NV_Write:
Vadim Bendebury4c0851c2016-07-03 17:08:10 -0700549 case TPM2_NV_WriteLock:
Vadim Bendebury627afc22016-06-19 12:13:18 -0700550 /* Session data included in response can be safely ignored. */
551 cr_size = 0;
552 break;
553
554 default:
555 {
556 int i;
557
558 printk(BIOS_INFO, "%s:%d:"
559 "Request to unmarshal unexpected command %#x,"
560 " code %#x",
561 __func__, __LINE__, command,
562 tpm2_resp.hdr.tpm_code);
563
564 for (i = 0; i < cr_size; i++) {
565 if (!(i % 16))
566 printk(BIOS_INFO, "\n");
567 printk(BIOS_INFO, "%2.2x ",
568 ((uint8_t *)response_body)[i]);
569 }
570 }
571 printk(BIOS_INFO, "\n");
572 return NULL;
573 }
574
575 if (cr_size) {
576 printk(BIOS_INFO,
577 "%s:%d got %d bytes back in response to %#x,"
578 " failed to parse (%d)\n",
579 __func__, __LINE__, tpm2_resp.hdr.tpm_size,
580 command, cr_size);
581 return NULL;
582 }
583
584 /* The entire message have been parsed. */
585 return &tpm2_resp;
586}