blob: ab9a928444f028afffcf3236072b533552c311db [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
Vadim Bendeburyf5ef6992016-07-03 22:20:17 -0700204static void marshal_TPMT_HA(void **buffer,
205 TPMT_HA *tpmtha,
206 size_t *buffer_space)
207{
208 marshal_TPMI_ALG_HASH(buffer, tpmtha->hashAlg, buffer_space);
209 marshal_blob(buffer, tpmtha->digest.sha256,
210 sizeof(tpmtha->digest.sha256),
211 buffer_space);
212}
213
214static void marshal_TPML_DIGEST_VALUES(void **buffer,
215 TPML_DIGEST_VALUES *dvalues,
216 size_t *buffer_space)
217{
218 int i;
219
220 marshal_u32(buffer, dvalues->count, buffer_space);
221 for (i = 0; i < dvalues->count; i++)
222 marshal_TPMT_HA(buffer, &dvalues->digests[i], buffer_space);
223}
224
Vadim Bendebury627afc22016-06-19 12:13:18 -0700225static void marshal_session_header(void **buffer,
226 struct tpm2_session_header *session_header,
Vadim Bendeburybc927102016-07-07 10:52:46 -0700227 size_t *buffer_space)
Vadim Bendebury627afc22016-06-19 12:13:18 -0700228{
Vadim Bendeburybc927102016-07-07 10:52:46 -0700229 size_t base_size;
Vadim Bendebury627afc22016-06-19 12:13:18 -0700230 void *size_location = *buffer;
231
232 /* Skip room for the session header size. */
Vadim Bendeburybc927102016-07-07 10:52:46 -0700233 if (*buffer_space < sizeof(uint32_t)) {
234 *buffer_space = 0;
235 return;
236 }
237
Vadim Bendebury627afc22016-06-19 12:13:18 -0700238 *buffer_space -= sizeof(uint32_t);
239 *buffer = (void *)(((uintptr_t) *buffer) + sizeof(uint32_t));
240
241 base_size = *buffer_space;
242
243 marshal_u32(buffer, session_header->session_handle, buffer_space);
244 marshal_u16(buffer, session_header->nonce_size, buffer_space);
245 marshal_blob(buffer, session_header->nonce,
246 session_header->nonce_size, buffer_space);
247 marshal_u8(buffer, session_header->session_attrs, buffer_space);
248 marshal_u16(buffer, session_header->auth_size, buffer_space);
249 marshal_blob(buffer, session_header->auth,
250 session_header->auth_size, buffer_space);
251
Vadim Bendeburybc927102016-07-07 10:52:46 -0700252 if (!*buffer_space)
Vadim Bendebury627afc22016-06-19 12:13:18 -0700253 return; /* The structure did not fit. */
254
255 /* Paste in the session size. */
256 marshal_u32(&size_location, base_size - *buffer_space, &base_size);
257}
258
Vadim Bendeburyebba4d72016-07-07 11:04:06 -0700259/*
260 * Common session header can include one or two handles and an empty
261 * session_header structure.
262 */
263static void marshal_common_session_header(void **buffer,
264 const uint32_t *handles,
265 size_t handle_count,
266 size_t *buffer_space)
267{
268 int i;
269 struct tpm2_session_header session_header;
270
271 tpm_tag = TPM_ST_SESSIONS;
272
273 for (i = 0; i < handle_count; i++)
274 marshal_TPM_HANDLE(buffer, handles[i], buffer_space);
275
276 memset(&session_header, 0, sizeof(session_header));
277 session_header.session_handle = TPM_RS_PW;
278 marshal_session_header(buffer, &session_header, buffer_space);
279}
280
Vadim Bendebury627afc22016-06-19 12:13:18 -0700281static void marshal_nv_define_space(void **buffer,
282 struct tpm2_nv_define_space_cmd *nvd_in,
Vadim Bendeburybc927102016-07-07 10:52:46 -0700283 size_t *buffer_space)
Vadim Bendebury627afc22016-06-19 12:13:18 -0700284{
285 void *size_location;
Vadim Bendeburybc927102016-07-07 10:52:46 -0700286 size_t base_size;
287 size_t sizeof_nv_public_size = sizeof(uint16_t);
Vadim Bendeburyebba4d72016-07-07 11:04:06 -0700288 const uint32_t handle[] = { TPM_RH_PLATFORM };
Vadim Bendebury627afc22016-06-19 12:13:18 -0700289
Vadim Bendeburyebba4d72016-07-07 11:04:06 -0700290 marshal_common_session_header(buffer, handle,
291 ARRAY_SIZE(handle), buffer_space);
Vadim Bendebury627afc22016-06-19 12:13:18 -0700292 marshal_TPM2B(buffer, &nvd_in->auth.b, buffer_space);
293
294 /* This is where the TPMS_NV_PUBLIC size will be stored. */
295 size_location = *buffer;
296
297 /* Allocate room for the size. */
298 *buffer = ((uint8_t *)(*buffer)) + sizeof(uint16_t);
Vadim Bendeburybc927102016-07-07 10:52:46 -0700299
300 if (*buffer_space < sizeof_nv_public_size) {
301 *buffer_space = 0;
302 return;
303 }
304 *buffer_space -= sizeof_nv_public_size;
Vadim Bendebury627afc22016-06-19 12:13:18 -0700305 base_size = *buffer_space;
306
307 marshal_TPMS_NV_PUBLIC(buffer, &nvd_in->publicInfo, buffer_space);
Vadim Bendeburybc927102016-07-07 10:52:46 -0700308 if (!*buffer_space)
309 return;
310
Vadim Bendebury627afc22016-06-19 12:13:18 -0700311 base_size = base_size - *buffer_space;
Vadim Bendeburybc927102016-07-07 10:52:46 -0700312 marshal_u16(&size_location, base_size, &sizeof_nv_public_size);
Vadim Bendebury627afc22016-06-19 12:13:18 -0700313}
314
315static void marshal_nv_write(void **buffer,
316 struct tpm2_nv_write_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_TPM2B(buffer, &command_body->data.b, buffer_space);
324 marshal_u16(buffer, command_body->offset, buffer_space);
325}
326
Vadim Bendebury4c0851c2016-07-03 17:08:10 -0700327static void marshal_nv_write_lock(void **buffer,
328 struct tpm2_nv_write_lock_cmd *command_body,
329 size_t *buffer_space)
330{
331 uint32_t handles[] = { TPM_RH_PLATFORM, command_body->nvIndex };
332 marshal_common_session_header(buffer, handles,
333 ARRAY_SIZE(handles), buffer_space);
334}
335
Vadim Bendeburyf5ef6992016-07-03 22:20:17 -0700336static void marshal_pcr_extend(void **buffer,
337 struct tpm2_pcr_extend_cmd *command_body,
338 size_t *buffer_space)
339{
340 uint32_t handle = command_body->pcrHandle;
341
342 marshal_common_session_header(buffer, &handle, 1, buffer_space);
343 marshal_TPML_DIGEST_VALUES(buffer,
344 &command_body->digests, buffer_space);
345}
346
Vadim Bendebury627afc22016-06-19 12:13:18 -0700347static void marshal_nv_read(void **buffer,
348 struct tpm2_nv_read_cmd *command_body,
Vadim Bendeburybc927102016-07-07 10:52:46 -0700349 size_t *buffer_space)
Vadim Bendebury627afc22016-06-19 12:13:18 -0700350{
Vadim Bendeburyebba4d72016-07-07 11:04:06 -0700351 uint32_t handles[] = { TPM_RH_PLATFORM, command_body->nvIndex };
Vadim Bendebury627afc22016-06-19 12:13:18 -0700352
Vadim Bendeburyebba4d72016-07-07 11:04:06 -0700353 marshal_common_session_header(buffer, handles,
354 ARRAY_SIZE(handles), buffer_space);
Vadim Bendebury627afc22016-06-19 12:13:18 -0700355 marshal_u16(buffer, command_body->size, buffer_space);
356 marshal_u16(buffer, command_body->offset, buffer_space);
357}
358
Vadim Bendebury6acb9a62016-06-30 20:50:49 -0700359/* TPM2_Clear command does not require paramaters. */
360static void marshal_clear(void **buffer, size_t *buffer_space)
361{
362 const uint32_t handle[] = { TPM_RH_PLATFORM };
363
364 marshal_common_session_header(buffer, handle,
365 ARRAY_SIZE(handle), buffer_space);
366}
367
Vadim Bendebury627afc22016-06-19 12:13:18 -0700368static void marshal_selftest(void **buffer,
369 struct tpm2_self_test *command_body,
Vadim Bendeburybc927102016-07-07 10:52:46 -0700370 size_t *buffer_space)
Vadim Bendebury627afc22016-06-19 12:13:18 -0700371{
372 marshal_u8(buffer, command_body->yes_no, buffer_space);
373}
374
375int tpm_marshal_command(TPM_CC command, void *tpm_command_body,
Vadim Bendeburybc927102016-07-07 10:52:46 -0700376 void *buffer, size_t buffer_size)
Vadim Bendebury627afc22016-06-19 12:13:18 -0700377{
378 void *cmd_body = (uint8_t *)buffer + sizeof(struct tpm_header);
Vadim Bendeburybc927102016-07-07 10:52:46 -0700379 size_t max_body_size = buffer_size - sizeof(struct tpm_header);
380 size_t body_size = max_body_size;
Vadim Bendebury627afc22016-06-19 12:13:18 -0700381
382 /* Will be modified when marshaling some commands. */
383 tpm_tag = TPM_ST_NO_SESSIONS;
384
385 switch (command) {
386 case TPM2_Startup:
387 marshal_startup(&cmd_body, tpm_command_body, &body_size);
388 break;
389
390 case TPM2_GetCapability:
391 marshal_get_capability(&cmd_body, tpm_command_body,
392 &body_size);
393 break;
394
395 case TPM2_NV_Read:
396 marshal_nv_read(&cmd_body, tpm_command_body, &body_size);
397 break;
398
399 case TPM2_NV_DefineSpace:
400 marshal_nv_define_space(&cmd_body,
401 tpm_command_body, &body_size);
402 break;
403
404 case TPM2_NV_Write:
405 marshal_nv_write(&cmd_body, tpm_command_body, &body_size);
406 break;
407
Vadim Bendebury4c0851c2016-07-03 17:08:10 -0700408 case TPM2_NV_WriteLock:
409 marshal_nv_write_lock(&cmd_body, tpm_command_body, &body_size);
410 break;
411
Vadim Bendebury627afc22016-06-19 12:13:18 -0700412 case TPM2_SelfTest:
413 marshal_selftest(&cmd_body, tpm_command_body, &body_size);
414 break;
415
Vadim Bendebury6acb9a62016-06-30 20:50:49 -0700416 case TPM2_Clear:
417 marshal_clear(&cmd_body, &body_size);
418 break;
419
Vadim Bendeburyf5ef6992016-07-03 22:20:17 -0700420 case TPM2_PCR_Extend:
421 marshal_pcr_extend(&cmd_body, tpm_command_body, &body_size);
422 break;
423
Vadim Bendebury627afc22016-06-19 12:13:18 -0700424 default:
Vadim Bendeburybc927102016-07-07 10:52:46 -0700425 body_size = 0;
Vadim Bendebury627afc22016-06-19 12:13:18 -0700426 printk(BIOS_INFO, "%s:%d:Request to marshal unsupported command %#x\n",
427 __FILE__, __LINE__, command);
428 }
429
430 if (body_size > 0) {
Vadim Bendeburyd9137d52016-07-07 14:27:41 -0700431 size_t marshaled_size;
432 size_t header_room = sizeof(struct tpm_header);
Vadim Bendebury627afc22016-06-19 12:13:18 -0700433
434 /* See how much room was taken by marshaling. */
Vadim Bendeburyd9137d52016-07-07 14:27:41 -0700435 marshaled_size = max_body_size - body_size;
Vadim Bendebury627afc22016-06-19 12:13:18 -0700436
Vadim Bendeburyd9137d52016-07-07 14:27:41 -0700437 /* Total size includes the header size. */
438 marshaled_size += sizeof(struct tpm_header);
Vadim Bendebury627afc22016-06-19 12:13:18 -0700439
Vadim Bendeburyd9137d52016-07-07 14:27:41 -0700440 marshal_u16(&buffer, tpm_tag, &header_room);
441 marshal_u32(&buffer, marshaled_size, &header_room);
442 marshal_u32(&buffer, command, &header_room);
443 return marshaled_size;
Vadim Bendebury627afc22016-06-19 12:13:18 -0700444 }
445
Vadim Bendeburyd9137d52016-07-07 14:27:41 -0700446 return -1;
Vadim Bendebury627afc22016-06-19 12:13:18 -0700447}
448
449static void unmarshal_get_capability(void **buffer, int *size,
450 struct get_cap_response *gcr)
451{
452 int i;
453
454 gcr->more_data = unmarshal_u8(buffer, size);
455
456 gcr->cd.capability = unmarshal_TPM_CAP(buffer, size);
457 if (*size < 0)
458 return;
459
460 switch (gcr->cd.capability) {
461 case TPM_CAP_TPM_PROPERTIES:
462 gcr->cd.data.tpmProperties.count =
463 unmarshal_u32(buffer, size);
464 if (*size < 0)
465 return;
466 if (gcr->cd.data.tpmProperties.count > ARRAY_SIZE
467 (gcr->cd.data.tpmProperties.tpmProperty)) {
468 printk(BIOS_INFO, "%s:%s:%d - %d - too many properties\n",
469 __FILE__, __func__, __LINE__,
470 gcr->cd.data.tpmProperties.count);
471 *size = -1;
472 return;
473 }
474 for (i = 0; i < gcr->cd.data.tpmProperties.count; i++) {
475 TPMS_TAGGED_PROPERTY *pp;
476
477 pp = gcr->cd.data.tpmProperties.tpmProperty + i;
478 pp->property = unmarshal_TPM_PT(buffer, size);
479 pp->value = unmarshal_u32(buffer, size);
480 }
481 break;
482 default:
483 printk(BIOS_ERR,
484 "%s:%d - unable to unmarshal capability response",
485 __func__, __LINE__);
486 printk(BIOS_ERR, " for %d\n", gcr->cd.capability);
487 *size = -1;
488 break;
489 }
490}
491
492static void unmarshal_TPM2B_MAX_NV_BUFFER(void **buffer,
493 int *size,
494 TPM2B_MAX_NV_BUFFER *nv_buffer)
495{
496 nv_buffer->t.size = unmarshal_u16(buffer, size);
Vadim Bendeburybc927102016-07-07 10:52:46 -0700497 if ((*size < 0) || (nv_buffer->t.size > *size)) {
Vadim Bendebury627afc22016-06-19 12:13:18 -0700498 printk(BIOS_ERR, "%s:%d - "
499 "size mismatch: expected %d, remaining %d\n",
500 __func__, __LINE__, nv_buffer->t.size, *size);
Vadim Bendeburybc927102016-07-07 10:52:46 -0700501 *size = -1;
Vadim Bendebury627afc22016-06-19 12:13:18 -0700502 return;
503 }
504
505 nv_buffer->t.buffer = *buffer;
506
507 *buffer = ((uint8_t *)(*buffer)) + nv_buffer->t.size;
508 *size -= nv_buffer->t.size;
509}
510
511static void unmarshal_nv_read(void **buffer, int *size,
512 struct nv_read_response *nvr)
513{
514 /* Total size of the parameter field. */
515 nvr->params_size = unmarshal_u32(buffer, size);
516 unmarshal_TPM2B_MAX_NV_BUFFER(buffer, size, &nvr->buffer);
517
518 if (nvr->params_size !=
519 (nvr->buffer.t.size + sizeof(nvr->buffer.t.size))) {
520 printk(BIOS_ERR,
521 "%s:%d - parameter/buffer %d/%d size mismatch",
522 __func__, __LINE__, nvr->params_size,
523 nvr->buffer.t.size);
524 return;
525 }
526
527 if (*size < 0)
528 return;
529 /*
530 * Let's ignore the authorisation section. It should be 5 bytes total,
531 * just confirm that this is the case and report any discrepancy.
532 */
533 if (*size != 5)
534 printk(BIOS_ERR,
535 "%s:%d - unexpected authorisation seciton size %d\n",
536 __func__, __LINE__, *size);
537
538 *buffer = ((uint8_t *)(*buffer)) + *size;
539 *size = 0;
540}
541
542struct tpm2_response *tpm_unmarshal_response(TPM_CC command,
543 void *response_body,
Vadim Bendeburybc927102016-07-07 10:52:46 -0700544 size_t in_size)
Vadim Bendebury627afc22016-06-19 12:13:18 -0700545{
546 static struct tpm2_response tpm2_resp;
Vadim Bendeburybc927102016-07-07 10:52:46 -0700547 /*
548 * Should be 0 when done, positive and negaitive values indicate
549 * unmarshaling errors.
550 */
551 int cr_size = in_size;
Vadim Bendebury627afc22016-06-19 12:13:18 -0700552
Vadim Bendeburybc927102016-07-07 10:52:46 -0700553 if ((cr_size < 0) || (in_size < sizeof(struct tpm_header)))
Vadim Bendebury627afc22016-06-19 12:13:18 -0700554 return NULL;
555
556 tpm2_resp.hdr.tpm_tag = unmarshal_u16(&response_body, &cr_size);
557 tpm2_resp.hdr.tpm_size = unmarshal_u32(&response_body, &cr_size);
558 tpm2_resp.hdr.tpm_code = unmarshal_TPM_CC(&response_body, &cr_size);
559
560 if (!cr_size) {
561 if (tpm2_resp.hdr.tpm_size != sizeof(tpm2_resp.hdr))
562 printk(BIOS_ERR,
563 "%s: size mismatch in response to command %#x\n",
564 __func__, command);
565 return &tpm2_resp;
566 }
567
568 switch (command) {
569 case TPM2_Startup:
570 break;
571
572 case TPM2_GetCapability:
573 unmarshal_get_capability(&response_body, &cr_size,
574 &tpm2_resp.gc);
575 break;
576
577 case TPM2_NV_Read:
578 unmarshal_nv_read(&response_body, &cr_size,
579 &tpm2_resp.nvr);
580 break;
581
Vadim Bendebury6acb9a62016-06-30 20:50:49 -0700582 case TPM2_Clear:
Vadim Bendebury627afc22016-06-19 12:13:18 -0700583 case TPM2_NV_DefineSpace:
584 case TPM2_NV_Write:
Vadim Bendebury4c0851c2016-07-03 17:08:10 -0700585 case TPM2_NV_WriteLock:
Vadim Bendeburyf5ef6992016-07-03 22:20:17 -0700586 case TPM2_PCR_Extend:
Vadim Bendebury627afc22016-06-19 12:13:18 -0700587 /* Session data included in response can be safely ignored. */
588 cr_size = 0;
589 break;
590
591 default:
592 {
593 int i;
594
595 printk(BIOS_INFO, "%s:%d:"
596 "Request to unmarshal unexpected command %#x,"
597 " code %#x",
598 __func__, __LINE__, command,
599 tpm2_resp.hdr.tpm_code);
600
601 for (i = 0; i < cr_size; i++) {
602 if (!(i % 16))
603 printk(BIOS_INFO, "\n");
604 printk(BIOS_INFO, "%2.2x ",
605 ((uint8_t *)response_body)[i]);
606 }
607 }
608 printk(BIOS_INFO, "\n");
609 return NULL;
610 }
611
612 if (cr_size) {
613 printk(BIOS_INFO,
614 "%s:%d got %d bytes back in response to %#x,"
615 " failed to parse (%d)\n",
616 __func__, __LINE__, tpm2_resp.hdr.tpm_size,
617 command, cr_size);
618 return NULL;
619 }
620
621 /* The entire message have been parsed. */
622 return &tpm2_resp;
623}