blob: 52dac9c5538156e9f340dd14d1647876977bda8e [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;
Aaron Durbineeb77372017-03-08 11:23:11 -0600397 sub_command = command_body;
398
399 switch (*sub_command) {
400 case TPM2_CR50_SUB_CMD_NVMEM_ENABLE_COMMITS:
401 marshal_u16(buffer, *sub_command, buffer_space);
402 break;
Vadim Bendebury021ec282017-03-22 16:01:53 -0700403 case TPM2_CR50_SUB_CMD_TURN_UPDATE_ON:
404 marshal_u16(buffer, sub_command[0], buffer_space);
405 marshal_u16(buffer, sub_command[1], buffer_space);
406 break;
Aaron Durbineeb77372017-03-08 11:23:11 -0600407 default:
408 /* Unsupported subcommand. */
409 printk(BIOS_WARNING, "Unsupported cr50 subcommand: 0x%04x\n",
410 *sub_command);
411 *buffer_space = 0;
412 break;
413 }
414}
415
Vadim Bendebury627afc22016-06-19 12:13:18 -0700416int tpm_marshal_command(TPM_CC command, void *tpm_command_body,
Vadim Bendeburybc927102016-07-07 10:52:46 -0700417 void *buffer, size_t buffer_size)
Vadim Bendebury627afc22016-06-19 12:13:18 -0700418{
419 void *cmd_body = (uint8_t *)buffer + sizeof(struct tpm_header);
Vadim Bendeburybc927102016-07-07 10:52:46 -0700420 size_t max_body_size = buffer_size - sizeof(struct tpm_header);
421 size_t body_size = max_body_size;
Vadim Bendebury627afc22016-06-19 12:13:18 -0700422
423 /* Will be modified when marshaling some commands. */
Victor Prupisf7060202016-08-19 10:45:04 -0700424 car_set_var(tpm_tag, TPM_ST_NO_SESSIONS);
Vadim Bendebury627afc22016-06-19 12:13:18 -0700425
426 switch (command) {
427 case TPM2_Startup:
428 marshal_startup(&cmd_body, tpm_command_body, &body_size);
429 break;
430
431 case TPM2_GetCapability:
432 marshal_get_capability(&cmd_body, tpm_command_body,
433 &body_size);
434 break;
435
436 case TPM2_NV_Read:
437 marshal_nv_read(&cmd_body, tpm_command_body, &body_size);
438 break;
439
440 case TPM2_NV_DefineSpace:
441 marshal_nv_define_space(&cmd_body,
442 tpm_command_body, &body_size);
443 break;
444
445 case TPM2_NV_Write:
446 marshal_nv_write(&cmd_body, tpm_command_body, &body_size);
447 break;
448
Vadim Bendebury4c0851c2016-07-03 17:08:10 -0700449 case TPM2_NV_WriteLock:
450 marshal_nv_write_lock(&cmd_body, tpm_command_body, &body_size);
451 break;
452
Vadim Bendebury627afc22016-06-19 12:13:18 -0700453 case TPM2_SelfTest:
454 marshal_selftest(&cmd_body, tpm_command_body, &body_size);
455 break;
456
Aaron Durbinf56c7782017-01-10 17:44:42 -0600457 case TPM2_Hierarchy_Control:
458 marshal_hierarchy_control(&cmd_body, tpm_command_body,
459 &body_size);
460 break;
461
Vadim Bendebury6acb9a62016-06-30 20:50:49 -0700462 case TPM2_Clear:
463 marshal_clear(&cmd_body, &body_size);
464 break;
465
Vadim Bendeburyf5ef6992016-07-03 22:20:17 -0700466 case TPM2_PCR_Extend:
467 marshal_pcr_extend(&cmd_body, tpm_command_body, &body_size);
468 break;
469
Aaron Durbineeb77372017-03-08 11:23:11 -0600470 case TPM2_CR50_VENDOR_COMMAND:
471 marshal_cr50_vendor_command(&cmd_body, tpm_command_body,
472 &body_size);
473 break;
474
Vadim Bendebury627afc22016-06-19 12:13:18 -0700475 default:
Vadim Bendeburybc927102016-07-07 10:52:46 -0700476 body_size = 0;
Vadim Bendebury627afc22016-06-19 12:13:18 -0700477 printk(BIOS_INFO, "%s:%d:Request to marshal unsupported command %#x\n",
478 __FILE__, __LINE__, command);
479 }
480
481 if (body_size > 0) {
Vadim Bendeburyd9137d52016-07-07 14:27:41 -0700482 size_t marshaled_size;
483 size_t header_room = sizeof(struct tpm_header);
Vadim Bendebury627afc22016-06-19 12:13:18 -0700484
485 /* See how much room was taken by marshaling. */
Vadim Bendeburyd9137d52016-07-07 14:27:41 -0700486 marshaled_size = max_body_size - body_size;
Vadim Bendebury627afc22016-06-19 12:13:18 -0700487
Vadim Bendeburyd9137d52016-07-07 14:27:41 -0700488 /* Total size includes the header size. */
489 marshaled_size += sizeof(struct tpm_header);
Vadim Bendebury627afc22016-06-19 12:13:18 -0700490
Victor Prupisf7060202016-08-19 10:45:04 -0700491 uint16_t tpm_tag_value = car_get_var(tpm_tag);
492
493 marshal_u16(&buffer, tpm_tag_value, &header_room);
Vadim Bendeburyd9137d52016-07-07 14:27:41 -0700494 marshal_u32(&buffer, marshaled_size, &header_room);
495 marshal_u32(&buffer, command, &header_room);
496 return marshaled_size;
Vadim Bendebury627afc22016-06-19 12:13:18 -0700497 }
498
Vadim Bendeburyd9137d52016-07-07 14:27:41 -0700499 return -1;
Vadim Bendebury627afc22016-06-19 12:13:18 -0700500}
501
502static void unmarshal_get_capability(void **buffer, int *size,
503 struct get_cap_response *gcr)
504{
505 int i;
506
507 gcr->more_data = unmarshal_u8(buffer, size);
508
509 gcr->cd.capability = unmarshal_TPM_CAP(buffer, size);
510 if (*size < 0)
511 return;
512
513 switch (gcr->cd.capability) {
514 case TPM_CAP_TPM_PROPERTIES:
515 gcr->cd.data.tpmProperties.count =
516 unmarshal_u32(buffer, size);
517 if (*size < 0)
518 return;
519 if (gcr->cd.data.tpmProperties.count > ARRAY_SIZE
520 (gcr->cd.data.tpmProperties.tpmProperty)) {
521 printk(BIOS_INFO, "%s:%s:%d - %d - too many properties\n",
522 __FILE__, __func__, __LINE__,
523 gcr->cd.data.tpmProperties.count);
524 *size = -1;
525 return;
526 }
527 for (i = 0; i < gcr->cd.data.tpmProperties.count; i++) {
528 TPMS_TAGGED_PROPERTY *pp;
529
530 pp = gcr->cd.data.tpmProperties.tpmProperty + i;
531 pp->property = unmarshal_TPM_PT(buffer, size);
532 pp->value = unmarshal_u32(buffer, size);
533 }
534 break;
535 default:
536 printk(BIOS_ERR,
537 "%s:%d - unable to unmarshal capability response",
538 __func__, __LINE__);
539 printk(BIOS_ERR, " for %d\n", gcr->cd.capability);
540 *size = -1;
541 break;
542 }
543}
544
545static void unmarshal_TPM2B_MAX_NV_BUFFER(void **buffer,
546 int *size,
547 TPM2B_MAX_NV_BUFFER *nv_buffer)
548{
549 nv_buffer->t.size = unmarshal_u16(buffer, size);
Vadim Bendeburybc927102016-07-07 10:52:46 -0700550 if ((*size < 0) || (nv_buffer->t.size > *size)) {
Vadim Bendebury627afc22016-06-19 12:13:18 -0700551 printk(BIOS_ERR, "%s:%d - "
552 "size mismatch: expected %d, remaining %d\n",
553 __func__, __LINE__, nv_buffer->t.size, *size);
Vadim Bendeburybc927102016-07-07 10:52:46 -0700554 *size = -1;
Vadim Bendebury627afc22016-06-19 12:13:18 -0700555 return;
556 }
557
558 nv_buffer->t.buffer = *buffer;
559
560 *buffer = ((uint8_t *)(*buffer)) + nv_buffer->t.size;
561 *size -= nv_buffer->t.size;
562}
563
564static void unmarshal_nv_read(void **buffer, int *size,
565 struct nv_read_response *nvr)
566{
567 /* Total size of the parameter field. */
568 nvr->params_size = unmarshal_u32(buffer, size);
569 unmarshal_TPM2B_MAX_NV_BUFFER(buffer, size, &nvr->buffer);
570
571 if (nvr->params_size !=
572 (nvr->buffer.t.size + sizeof(nvr->buffer.t.size))) {
573 printk(BIOS_ERR,
574 "%s:%d - parameter/buffer %d/%d size mismatch",
575 __func__, __LINE__, nvr->params_size,
576 nvr->buffer.t.size);
577 return;
578 }
579
580 if (*size < 0)
581 return;
582 /*
583 * Let's ignore the authorisation section. It should be 5 bytes total,
584 * just confirm that this is the case and report any discrepancy.
585 */
586 if (*size != 5)
587 printk(BIOS_ERR,
588 "%s:%d - unexpected authorisation seciton size %d\n",
589 __func__, __LINE__, *size);
590
591 *buffer = ((uint8_t *)(*buffer)) + *size;
592 *size = 0;
593}
594
Vadim Bendebury021ec282017-03-22 16:01:53 -0700595static void unmarshal_vendor_command(void **buffer, int *size,
596 struct vendor_command_response *vcr)
597{
598 vcr->vc_subcommand = unmarshal_u16(buffer, size);
599
600 switch (vcr->vc_subcommand) {
601 case TPM2_CR50_SUB_CMD_NVMEM_ENABLE_COMMITS:
602 break;
603 case TPM2_CR50_SUB_CMD_TURN_UPDATE_ON:
604 vcr->num_restored_headers = unmarshal_u8(buffer, size);
605 break;
606 default:
607 printk(BIOS_ERR,
608 "%s:%d - unsupported vendor command %#04x!\n",
609 __func__, __LINE__, vcr->vc_subcommand);
610 break;
611 }
612}
613
Vadim Bendebury627afc22016-06-19 12:13:18 -0700614struct tpm2_response *tpm_unmarshal_response(TPM_CC command,
615 void *response_body,
Vadim Bendeburybc927102016-07-07 10:52:46 -0700616 size_t in_size)
Vadim Bendebury627afc22016-06-19 12:13:18 -0700617{
Duncan Laurie4a560762016-09-01 16:09:43 -0700618 static struct tpm2_response tpm2_static_resp CAR_GLOBAL;
619 struct tpm2_response *tpm2_resp = car_get_var_ptr(&tpm2_static_resp);
Vadim Bendeburybc927102016-07-07 10:52:46 -0700620 /*
621 * Should be 0 when done, positive and negaitive values indicate
622 * unmarshaling errors.
623 */
624 int cr_size = in_size;
Vadim Bendebury627afc22016-06-19 12:13:18 -0700625
Vadim Bendeburybc927102016-07-07 10:52:46 -0700626 if ((cr_size < 0) || (in_size < sizeof(struct tpm_header)))
Vadim Bendebury627afc22016-06-19 12:13:18 -0700627 return NULL;
628
Duncan Laurie4a560762016-09-01 16:09:43 -0700629 tpm2_resp->hdr.tpm_tag = unmarshal_u16(&response_body, &cr_size);
630 tpm2_resp->hdr.tpm_size = unmarshal_u32(&response_body, &cr_size);
631 tpm2_resp->hdr.tpm_code = unmarshal_TPM_CC(&response_body, &cr_size);
Vadim Bendebury627afc22016-06-19 12:13:18 -0700632
633 if (!cr_size) {
Duncan Laurie4a560762016-09-01 16:09:43 -0700634 if (tpm2_resp->hdr.tpm_size != sizeof(tpm2_resp->hdr))
Vadim Bendebury627afc22016-06-19 12:13:18 -0700635 printk(BIOS_ERR,
636 "%s: size mismatch in response to command %#x\n",
637 __func__, command);
Duncan Laurie4a560762016-09-01 16:09:43 -0700638 return tpm2_resp;
Vadim Bendebury627afc22016-06-19 12:13:18 -0700639 }
640
641 switch (command) {
642 case TPM2_Startup:
643 break;
644
645 case TPM2_GetCapability:
646 unmarshal_get_capability(&response_body, &cr_size,
Duncan Laurie4a560762016-09-01 16:09:43 -0700647 &tpm2_resp->gc);
Vadim Bendebury627afc22016-06-19 12:13:18 -0700648 break;
649
650 case TPM2_NV_Read:
651 unmarshal_nv_read(&response_body, &cr_size,
Duncan Laurie4a560762016-09-01 16:09:43 -0700652 &tpm2_resp->nvr);
Vadim Bendebury627afc22016-06-19 12:13:18 -0700653 break;
654
Aaron Durbinf56c7782017-01-10 17:44:42 -0600655 case TPM2_Hierarchy_Control:
Vadim Bendebury6acb9a62016-06-30 20:50:49 -0700656 case TPM2_Clear:
Vadim Bendebury627afc22016-06-19 12:13:18 -0700657 case TPM2_NV_DefineSpace:
658 case TPM2_NV_Write:
Vadim Bendebury4c0851c2016-07-03 17:08:10 -0700659 case TPM2_NV_WriteLock:
Vadim Bendeburyf5ef6992016-07-03 22:20:17 -0700660 case TPM2_PCR_Extend:
Vadim Bendebury627afc22016-06-19 12:13:18 -0700661 /* Session data included in response can be safely ignored. */
662 cr_size = 0;
663 break;
664
Aaron Durbineeb77372017-03-08 11:23:11 -0600665 case TPM2_CR50_VENDOR_COMMAND:
Vadim Bendebury021ec282017-03-22 16:01:53 -0700666 unmarshal_vendor_command(&response_body, &cr_size,
667 &tpm2_resp->vcr);
Aaron Durbineeb77372017-03-08 11:23:11 -0600668 break;
669
Vadim Bendebury627afc22016-06-19 12:13:18 -0700670 default:
671 {
672 int i;
673
674 printk(BIOS_INFO, "%s:%d:"
675 "Request to unmarshal unexpected command %#x,"
676 " code %#x",
677 __func__, __LINE__, command,
Duncan Laurie4a560762016-09-01 16:09:43 -0700678 tpm2_resp->hdr.tpm_code);
Vadim Bendebury627afc22016-06-19 12:13:18 -0700679
680 for (i = 0; i < cr_size; i++) {
681 if (!(i % 16))
682 printk(BIOS_INFO, "\n");
683 printk(BIOS_INFO, "%2.2x ",
684 ((uint8_t *)response_body)[i]);
685 }
686 }
687 printk(BIOS_INFO, "\n");
688 return NULL;
689 }
690
691 if (cr_size) {
692 printk(BIOS_INFO,
693 "%s:%d got %d bytes back in response to %#x,"
694 " failed to parse (%d)\n",
Duncan Laurie4a560762016-09-01 16:09:43 -0700695 __func__, __LINE__, tpm2_resp->hdr.tpm_size,
Vadim Bendebury627afc22016-06-19 12:13:18 -0700696 command, cr_size);
697 return NULL;
698 }
699
700 /* The entire message have been parsed. */
Duncan Laurie4a560762016-09-01 16:09:43 -0700701 return tpm2_resp;
Vadim Bendebury627afc22016-06-19 12:13:18 -0700702}