blob: f21fe3d315dfeb00f9a3a92dc1ffe63179bc67f0 [file] [log] [blame]
Jacob Garberfa8f5672020-05-18 13:18:19 -06001/* SPDX-License-Identifier: BSD-3-Clause */
Vadim Bendebury627afc22016-06-19 12:13:18 -07002
Aaron Durbinee049fa2017-03-25 00:38:45 -05003#include <commonlib/iobuf.h>
Vadim Bendebury627afc22016-06-19 12:13:18 -07004#include <console/console.h>
Vadim Bendebury627afc22016-06-19 12:13:18 -07005#include <string.h>
6
Philipp Deppenwiesed88fb362017-10-18 20:26:18 +02007#include "tss_marshaling.h"
Philipp Deppenwiesec07f8fb2018-02-27 19:40:52 +01008#include <security/tpm/tss/vendor/cr50/cr50.h>
Frans Hendriks589eff72019-06-26 10:43:40 +02009#include <security/tpm/tss.h>
Vadim Bendebury627afc22016-06-19 12:13:18 -070010
Arthur Heymans0ca944b2019-11-20 19:51:06 +010011static uint16_t tpm_tag; /* Depends on the command type. */
Vadim Bendebury627afc22016-06-19 12:13:18 -070012
Aaron Durbinee049fa2017-03-25 00:38:45 -050013#define unmarshal_TPM_CAP(a, b) ibuf_read_be32(a, b)
14#define unmarshal_TPM_CC(a, b) ibuf_read_be32(a, b)
15#define unmarshal_TPM_PT(a, b) ibuf_read_be32(a, b)
16#define unmarshal_TPM_HANDLE(a, b) ibuf_read_be32(a, b)
17
18#define marshal_TPM_HANDLE(a, b) obuf_write_be32(a, b)
19#define marshal_TPMI_ALG_HASH(a, b) obuf_write_be16(a, b)
20
Caveh Jalalic07fb752020-09-11 22:10:55 -070021static int marshal_startup(struct obuf *ob, const struct tpm2_startup *cmd_body)
Vadim Bendebury627afc22016-06-19 12:13:18 -070022{
Aaron Durbinee049fa2017-03-25 00:38:45 -050023 return obuf_write_be16(ob, cmd_body->startup_type);
Vadim Bendebury627afc22016-06-19 12:13:18 -070024}
25
Caveh Jalalic07fb752020-09-11 22:10:55 -070026static int marshal_shutdown(struct obuf *ob, const struct tpm2_shutdown *cmd_body)
Joel Kitching2e690ee2018-11-15 16:48:53 +080027{
28 return obuf_write_be16(ob, cmd_body->shutdown_type);
29}
30
Aaron Durbinee049fa2017-03-25 00:38:45 -050031static int marshal_get_capability(struct obuf *ob,
Caveh Jalalic07fb752020-09-11 22:10:55 -070032 const struct tpm2_get_capability *cmd_body)
Vadim Bendebury627afc22016-06-19 12:13:18 -070033{
Aaron Durbinee049fa2017-03-25 00:38:45 -050034 int rc = 0;
Vadim Bendebury627afc22016-06-19 12:13:18 -070035
Aaron Durbinee049fa2017-03-25 00:38:45 -050036 rc |= obuf_write_be32(ob, cmd_body->capability);
37 rc |= obuf_write_be32(ob, cmd_body->property);
38 rc |= obuf_write_be32(ob, cmd_body->propertyCount);
Vadim Bendeburybc927102016-07-07 10:52:46 -070039
Aaron Durbinee049fa2017-03-25 00:38:45 -050040 return rc;
Vadim Bendebury627afc22016-06-19 12:13:18 -070041}
42
Caveh Jalalic07fb752020-09-11 22:10:55 -070043static int marshal_TPM2B(struct obuf *ob, const TPM2B *data)
Vadim Bendebury627afc22016-06-19 12:13:18 -070044{
Aaron Durbinee049fa2017-03-25 00:38:45 -050045 int rc = 0;
Vadim Bendebury627afc22016-06-19 12:13:18 -070046
Aaron Durbinee049fa2017-03-25 00:38:45 -050047 rc |= obuf_write_be16(ob, data->size);
48 rc |= obuf_write(ob, data->buffer, data->size);
Vadim Bendeburybc927102016-07-07 10:52:46 -070049
Aaron Durbinee049fa2017-03-25 00:38:45 -050050 return rc;
Vadim Bendebury627afc22016-06-19 12:13:18 -070051}
52
Caveh Jalalic07fb752020-09-11 22:10:55 -070053static int marshal_TPMA_NV(struct obuf *ob, const TPMA_NV *nv)
Vadim Bendebury627afc22016-06-19 12:13:18 -070054{
Aaron Durbinee049fa2017-03-25 00:38:45 -050055 uint32_t v;
Vadim Bendebury627afc22016-06-19 12:13:18 -070056
Aaron Durbinee049fa2017-03-25 00:38:45 -050057 memcpy(&v, nv, sizeof(v));
58 return obuf_write_be32(ob, v);
Vadim Bendebury627afc22016-06-19 12:13:18 -070059}
60
Caveh Jalalic07fb752020-09-11 22:10:55 -070061static int marshal_TPMS_NV_PUBLIC(struct obuf *ob, const TPMS_NV_PUBLIC *nvpub)
Vadim Bendebury627afc22016-06-19 12:13:18 -070062{
Aaron Durbinee049fa2017-03-25 00:38:45 -050063 int rc = 0;
Vadim Bendebury627afc22016-06-19 12:13:18 -070064
Aaron Durbinee049fa2017-03-25 00:38:45 -050065 rc |= marshal_TPM_HANDLE(ob, nvpub->nvIndex);
66 rc |= marshal_TPMI_ALG_HASH(ob, nvpub->nameAlg);
67 rc |= marshal_TPMA_NV(ob, &nvpub->attributes);
68 rc |= marshal_TPM2B(ob, &nvpub->authPolicy.b);
69 rc |= obuf_write_be16(ob, nvpub->dataSize);
Vadim Bendebury627afc22016-06-19 12:13:18 -070070
Aaron Durbinee049fa2017-03-25 00:38:45 -050071 return rc;
Vadim Bendebury627afc22016-06-19 12:13:18 -070072}
73
Caveh Jalalic07fb752020-09-11 22:10:55 -070074static int marshal_TPMT_HA(struct obuf *ob, const TPMT_HA *tpmtha)
Vadim Bendebury627afc22016-06-19 12:13:18 -070075{
Aaron Durbinee049fa2017-03-25 00:38:45 -050076 int rc = 0;
77
78 rc |= marshal_TPMI_ALG_HASH(ob, tpmtha->hashAlg);
Frans Hendriksaa771cb2019-09-04 11:10:27 +020079 switch (tpmtha->hashAlg) {
80 case TPM_ALG_SHA1:
81 rc |= obuf_write(ob, tpmtha->digest.sha1,
Frans Hendriks7e220ca2019-06-28 10:18:22 +020082 tlcl_get_hash_size_from_algo(tpmtha->hashAlg));
Frans Hendriksaa771cb2019-09-04 11:10:27 +020083 break;
84 case TPM_ALG_SHA256:
85 rc |= obuf_write(ob, tpmtha->digest.sha256,
86 tlcl_get_hash_size_from_algo(tpmtha->hashAlg));
87 break;
88 case TPM_ALG_SM3_256:
89 rc |= obuf_write(ob, tpmtha->digest.sm3_256,
90 tlcl_get_hash_size_from_algo(tpmtha->hashAlg));
91 break;
92 case TPM_ALG_SHA384:
93 rc |= obuf_write(ob, tpmtha->digest.sha384,
94 tlcl_get_hash_size_from_algo(tpmtha->hashAlg));
95 break;
96 case TPM_ALG_SHA512:
97 rc |= obuf_write(ob, tpmtha->digest.sha512,
98 tlcl_get_hash_size_from_algo(tpmtha->hashAlg));
99 break;
100 default:
101 rc = -1;
102 }
Aaron Durbinee049fa2017-03-25 00:38:45 -0500103 return rc;
Vadim Bendebury627afc22016-06-19 12:13:18 -0700104}
105
Aaron Durbinee049fa2017-03-25 00:38:45 -0500106static int marshal_TPML_DIGEST_VALUES(struct obuf *ob,
Caveh Jalalic07fb752020-09-11 22:10:55 -0700107 const TPML_DIGEST_VALUES *dvalues)
Vadim Bendeburyf5ef6992016-07-03 22:20:17 -0700108{
109 int i;
Aaron Durbinee049fa2017-03-25 00:38:45 -0500110 int rc = 0;
Vadim Bendeburyf5ef6992016-07-03 22:20:17 -0700111
Aaron Durbinee049fa2017-03-25 00:38:45 -0500112 rc |= obuf_write_be32(ob, dvalues->count);
Vadim Bendeburyf5ef6992016-07-03 22:20:17 -0700113 for (i = 0; i < dvalues->count; i++)
Aaron Durbinee049fa2017-03-25 00:38:45 -0500114 rc |= marshal_TPMT_HA(ob, &dvalues->digests[i]);
115
116 return rc;
Vadim Bendeburyf5ef6992016-07-03 22:20:17 -0700117}
118
Aaron Durbinee049fa2017-03-25 00:38:45 -0500119static int marshal_session_header(struct obuf *ob,
Caveh Jalalic07fb752020-09-11 22:10:55 -0700120 const struct tpm2_session_header *session_header)
Vadim Bendebury627afc22016-06-19 12:13:18 -0700121{
Aaron Durbinee049fa2017-03-25 00:38:45 -0500122 int rc = 0;
123 struct obuf ob_sz;
124 size_t prev_written;
Vadim Bendebury627afc22016-06-19 12:13:18 -0700125
Aaron Durbinee049fa2017-03-25 00:38:45 -0500126 /* Snapshot current location to place size of header. */
127 if (obuf_splice_current(ob, &ob_sz, sizeof(uint32_t)) < 0)
128 return -1;
Vadim Bendeburybc927102016-07-07 10:52:46 -0700129
Aaron Durbinee049fa2017-03-25 00:38:45 -0500130 /* Write a size placeholder. */
131 rc |= obuf_write_be32(ob, 0);
Vadim Bendebury627afc22016-06-19 12:13:18 -0700132
Aaron Durbinee049fa2017-03-25 00:38:45 -0500133 /* Keep track of session header data size by tracking num written. */
134 prev_written = obuf_nr_written(ob);
Vadim Bendebury627afc22016-06-19 12:13:18 -0700135
Aaron Durbinee049fa2017-03-25 00:38:45 -0500136 rc |= obuf_write_be32(ob, session_header->session_handle);
137 rc |= obuf_write_be16(ob, session_header->nonce_size);
138 rc |= obuf_write(ob, session_header->nonce, session_header->nonce_size);
139 rc |= obuf_write_be8(ob, session_header->session_attrs);
140 rc |= obuf_write_be16(ob, session_header->auth_size);
141 rc |= obuf_write(ob, session_header->auth, session_header->auth_size);
Vadim Bendebury627afc22016-06-19 12:13:18 -0700142
Aaron Durbinee049fa2017-03-25 00:38:45 -0500143 /* Fill back in proper size of session header. */
144 rc |= obuf_write_be32(&ob_sz, obuf_nr_written(ob) - prev_written);
Vadim Bendebury627afc22016-06-19 12:13:18 -0700145
Aaron Durbinee049fa2017-03-25 00:38:45 -0500146 return rc;
Vadim Bendebury627afc22016-06-19 12:13:18 -0700147}
148
Vadim Bendeburyebba4d72016-07-07 11:04:06 -0700149/*
150 * Common session header can include one or two handles and an empty
151 * session_header structure.
152 */
Aaron Durbinee049fa2017-03-25 00:38:45 -0500153static int marshal_common_session_header(struct obuf *ob,
Caveh Jalalic07fb752020-09-11 22:10:55 -0700154 const uint32_t *handles,
155 size_t handle_count)
Vadim Bendeburyebba4d72016-07-07 11:04:06 -0700156{
Aaron Durbinee049fa2017-03-25 00:38:45 -0500157 size_t i;
Vadim Bendeburyebba4d72016-07-07 11:04:06 -0700158 struct tpm2_session_header session_header;
Aaron Durbinee049fa2017-03-25 00:38:45 -0500159 int rc = 0;
Vadim Bendeburyebba4d72016-07-07 11:04:06 -0700160
Arthur Heymans0ca944b2019-11-20 19:51:06 +0100161 tpm_tag = TPM_ST_SESSIONS;
Vadim Bendeburyebba4d72016-07-07 11:04:06 -0700162
163 for (i = 0; i < handle_count; i++)
Aaron Durbinee049fa2017-03-25 00:38:45 -0500164 rc |= marshal_TPM_HANDLE(ob, handles[i]);
Vadim Bendeburyebba4d72016-07-07 11:04:06 -0700165
166 memset(&session_header, 0, sizeof(session_header));
167 session_header.session_handle = TPM_RS_PW;
Aaron Durbinee049fa2017-03-25 00:38:45 -0500168 rc |= marshal_session_header(ob, &session_header);
169
170 return rc;
Vadim Bendeburyebba4d72016-07-07 11:04:06 -0700171}
172
Aaron Durbinee049fa2017-03-25 00:38:45 -0500173static int marshal_nv_define_space(struct obuf *ob,
Caveh Jalalic07fb752020-09-11 22:10:55 -0700174 const struct tpm2_nv_define_space_cmd *nvd_in)
Vadim Bendebury627afc22016-06-19 12:13:18 -0700175{
Vadim Bendeburyebba4d72016-07-07 11:04:06 -0700176 const uint32_t handle[] = { TPM_RH_PLATFORM };
Aaron Durbinee049fa2017-03-25 00:38:45 -0500177 struct obuf ob_sz;
178 size_t prev_written;
179 int rc = 0;
Vadim Bendebury627afc22016-06-19 12:13:18 -0700180
Aaron Durbinee049fa2017-03-25 00:38:45 -0500181 rc |= marshal_common_session_header(ob, handle, ARRAY_SIZE(handle));
182 rc |= marshal_TPM2B(ob, &nvd_in->auth.b);
Vadim Bendebury627afc22016-06-19 12:13:18 -0700183
Aaron Durbinee049fa2017-03-25 00:38:45 -0500184 /* Snapshot current location to place size field. */
185 if (obuf_splice_current(ob, &ob_sz, sizeof(uint16_t)) < 0)
186 return -1;
Vadim Bendebury627afc22016-06-19 12:13:18 -0700187
Aaron Durbinee049fa2017-03-25 00:38:45 -0500188 /* Put placeholder for size */
189 rc |= obuf_write_be16(ob, 0);
Vadim Bendeburybc927102016-07-07 10:52:46 -0700190
Aaron Durbinee049fa2017-03-25 00:38:45 -0500191 /* Keep track of nv define space data size by tracking num written. */
192 prev_written = obuf_nr_written(ob);
Vadim Bendebury627afc22016-06-19 12:13:18 -0700193
Aaron Durbinee049fa2017-03-25 00:38:45 -0500194 rc |= marshal_TPMS_NV_PUBLIC(ob, &nvd_in->publicInfo);
195 rc |= obuf_write_be16(&ob_sz, obuf_nr_written(ob) - prev_written);
Vadim Bendeburybc927102016-07-07 10:52:46 -0700196
Aaron Durbinee049fa2017-03-25 00:38:45 -0500197 return rc;
Vadim Bendebury627afc22016-06-19 12:13:18 -0700198}
199
Aaron Durbinee049fa2017-03-25 00:38:45 -0500200static int marshal_nv_write(struct obuf *ob,
Caveh Jalalic07fb752020-09-11 22:10:55 -0700201 const struct tpm2_nv_write_cmd *command_body)
Aaron Durbinee049fa2017-03-25 00:38:45 -0500202{
203 int rc = 0;
Caveh Jalalic07fb752020-09-11 22:10:55 -0700204 const uint32_t handles[] = { TPM_RH_PLATFORM, command_body->nvIndex };
Aaron Durbinee049fa2017-03-25 00:38:45 -0500205
206 rc |= marshal_common_session_header(ob, handles, ARRAY_SIZE(handles));
207 rc |= marshal_TPM2B(ob, &command_body->data.b);
208 rc |= obuf_write_be16(ob, command_body->offset);
209
210 return rc;
211}
212
213static int marshal_nv_write_lock(struct obuf *ob,
Caveh Jalalic07fb752020-09-11 22:10:55 -0700214 const struct tpm2_nv_write_lock_cmd *command_body)
Vadim Bendebury627afc22016-06-19 12:13:18 -0700215{
Caveh Jalalic07fb752020-09-11 22:10:55 -0700216 const uint32_t handles[] = { TPM_RH_PLATFORM, command_body->nvIndex };
Vadim Bendebury627afc22016-06-19 12:13:18 -0700217
Aaron Durbinee049fa2017-03-25 00:38:45 -0500218 return marshal_common_session_header(ob, handles, ARRAY_SIZE(handles));
Vadim Bendebury627afc22016-06-19 12:13:18 -0700219}
220
Aaron Durbinee049fa2017-03-25 00:38:45 -0500221static int marshal_pcr_extend(struct obuf *ob,
Caveh Jalalic07fb752020-09-11 22:10:55 -0700222 const struct tpm2_pcr_extend_cmd *command_body)
Vadim Bendebury4c0851c2016-07-03 17:08:10 -0700223{
Aaron Durbinee049fa2017-03-25 00:38:45 -0500224 int rc = 0;
Caveh Jalalic07fb752020-09-11 22:10:55 -0700225 const uint32_t handles[] = { command_body->pcrHandle };
Aaron Durbinee049fa2017-03-25 00:38:45 -0500226
227 rc |= marshal_common_session_header(ob, handles, ARRAY_SIZE(handles));
228 rc |= marshal_TPML_DIGEST_VALUES(ob, &command_body->digests);
229
230 return rc;
Vadim Bendebury4c0851c2016-07-03 17:08:10 -0700231}
232
Aaron Durbinee049fa2017-03-25 00:38:45 -0500233static int marshal_nv_read(struct obuf *ob,
Caveh Jalalic07fb752020-09-11 22:10:55 -0700234 const struct tpm2_nv_read_cmd *command_body)
Vadim Bendeburyf5ef6992016-07-03 22:20:17 -0700235{
Aaron Durbinee049fa2017-03-25 00:38:45 -0500236 int rc = 0;
Caveh Jalalic07fb752020-09-11 22:10:55 -0700237 const uint32_t handles[] = { TPM_RH_PLATFORM, command_body->nvIndex };
Vadim Bendebury627afc22016-06-19 12:13:18 -0700238
Aaron Durbinee049fa2017-03-25 00:38:45 -0500239 rc |= marshal_common_session_header(ob, handles, ARRAY_SIZE(handles));
240 rc |= obuf_write_be16(ob, command_body->size);
241 rc |= obuf_write_be16(ob, command_body->offset);
242
243 return rc;
Vadim Bendebury627afc22016-06-19 12:13:18 -0700244}
245
Elyes HAOUAS3d450002018-08-09 18:55:58 +0200246/* TPM2_Clear command does not require parameters. */
Aaron Durbinee049fa2017-03-25 00:38:45 -0500247static int marshal_clear(struct obuf *ob)
Vadim Bendebury6acb9a62016-06-30 20:50:49 -0700248{
249 const uint32_t handle[] = { TPM_RH_PLATFORM };
250
Aaron Durbinee049fa2017-03-25 00:38:45 -0500251 return marshal_common_session_header(ob, handle, ARRAY_SIZE(handle));
Vadim Bendebury6acb9a62016-06-30 20:50:49 -0700252}
253
Aaron Durbinee049fa2017-03-25 00:38:45 -0500254static int marshal_selftest(struct obuf *ob,
Caveh Jalalic07fb752020-09-11 22:10:55 -0700255 const struct tpm2_self_test *command_body)
Vadim Bendebury627afc22016-06-19 12:13:18 -0700256{
Aaron Durbinee049fa2017-03-25 00:38:45 -0500257 return obuf_write_be8(ob, command_body->yes_no);
Vadim Bendebury627afc22016-06-19 12:13:18 -0700258}
259
Aaron Durbinee049fa2017-03-25 00:38:45 -0500260static int marshal_hierarchy_control(struct obuf *ob,
Caveh Jalalic07fb752020-09-11 22:10:55 -0700261 const struct tpm2_hierarchy_control_cmd *command_body)
Aaron Durbinf56c7782017-01-10 17:44:42 -0600262{
Aaron Durbinee049fa2017-03-25 00:38:45 -0500263 int rc = 0;
Aaron Durbinf56c7782017-01-10 17:44:42 -0600264 struct tpm2_session_header session_header;
265
Arthur Heymans0ca944b2019-11-20 19:51:06 +0100266 tpm_tag = TPM_ST_SESSIONS;
Aaron Durbinf56c7782017-01-10 17:44:42 -0600267
Aaron Durbinee049fa2017-03-25 00:38:45 -0500268 rc |= marshal_TPM_HANDLE(ob, TPM_RH_PLATFORM);
Aaron Durbinf56c7782017-01-10 17:44:42 -0600269 memset(&session_header, 0, sizeof(session_header));
270 session_header.session_handle = TPM_RS_PW;
Aaron Durbinee049fa2017-03-25 00:38:45 -0500271 rc |= marshal_session_header(ob, &session_header);
Aaron Durbinf56c7782017-01-10 17:44:42 -0600272
Aaron Durbinee049fa2017-03-25 00:38:45 -0500273 rc |= marshal_TPM_HANDLE(ob, command_body->enable);
274 rc |= obuf_write_be8(ob, command_body->state);
275
276 return rc;
Aaron Durbinf56c7782017-01-10 17:44:42 -0600277}
278
Christian Walterc9ac0bc2020-01-28 19:54:33 +0100279static int marshal_clear_control(struct obuf *ob,
Caveh Jalalic07fb752020-09-11 22:10:55 -0700280 const struct tpm2_clear_control_cmd *command_body)
Christian Walterc9ac0bc2020-01-28 19:54:33 +0100281{
282 int rc = 0;
283 struct tpm2_session_header session_header;
284
285 tpm_tag = TPM_ST_SESSIONS;
286
287 rc |= marshal_TPM_HANDLE(ob, TPM_RH_PLATFORM);
288 memset(&session_header, 0, sizeof(session_header));
289 session_header.session_handle = TPM_RS_PW;
290 rc |= marshal_session_header(ob, &session_header);
291
292 rc |= obuf_write_be8(ob, command_body->disable);
293
294 return rc;
295}
296
Caveh Jalalic07fb752020-09-11 22:10:55 -0700297static int marshal_cr50_vendor_command(struct obuf *ob, const void *command_body)
Aaron Durbineeb77372017-03-08 11:23:11 -0600298{
Aaron Durbinee049fa2017-03-25 00:38:45 -0500299 int rc = 0;
Caveh Jalalic07fb752020-09-11 22:10:55 -0700300 const uint16_t *sub_command = command_body;
Aaron Durbineeb77372017-03-08 11:23:11 -0600301
302 switch (*sub_command) {
Keith Shorte0f34002019-02-05 16:15:10 -0700303 case TPM2_CR50_SUB_CMD_IMMEDIATE_RESET:
304 /* The 16-bit timeout parameter is optional for the
305 * IMMEDIATE_RESET command. However in coreboot, the timeout
306 * parameter must be specified.
307 */
308 rc |= obuf_write_be16(ob, sub_command[0]);
309 rc |= obuf_write_be16(ob, sub_command[1]);
310 break;
Aaron Durbineeb77372017-03-08 11:23:11 -0600311 case TPM2_CR50_SUB_CMD_NVMEM_ENABLE_COMMITS:
Aaron Durbinee049fa2017-03-25 00:38:45 -0500312 rc |= obuf_write_be16(ob, *sub_command);
Aaron Durbineeb77372017-03-08 11:23:11 -0600313 break;
Vadim Bendebury021ec282017-03-22 16:01:53 -0700314 case TPM2_CR50_SUB_CMD_TURN_UPDATE_ON:
Aaron Durbinee049fa2017-03-25 00:38:45 -0500315 rc |= obuf_write_be16(ob, sub_command[0]);
316 rc |= obuf_write_be16(ob, sub_command[1]);
Vadim Bendebury021ec282017-03-22 16:01:53 -0700317 break;
Keith Shorte371d422019-01-11 07:52:32 -0700318 case TPM2_CR50_SUB_CMD_GET_REC_BTN:
319 rc |= obuf_write_be16(ob, *sub_command);
320 break;
Keith Shorte0f34002019-02-05 16:15:10 -0700321 case TPM2_CR50_SUB_CMD_TPM_MODE:
322 /* The Cr50 TPM_MODE command supports an optional parameter.
323 * When the parameter is present the Cr50 will attempt to change
324 * the TPM state (enable or disable) and returns the new state
325 * in the response. When the parameter is absent, the Cr50
326 * returns the current TPM state.
327 *
328 * coreboot currently only uses the TPM get capability and does
329 * not set a new TPM state with the Cr50.
330 */
331 rc |= obuf_write_be16(ob, *sub_command);
332 break;
dnojiri622c6b82020-04-03 10:51:50 -0700333 case TPM2_CR50_SUB_CMD_GET_BOOT_MODE:
334 rc |= obuf_write_be16(ob, *sub_command);
335 break;
Karthikeyan Ramasubramanianba7b90e2021-03-01 13:50:20 -0700336 case TPM2_CR50_SUB_CMD_RESET_EC:
337 rc |= obuf_write_be16(ob, *sub_command);
338 break;
Aaron Durbineeb77372017-03-08 11:23:11 -0600339 default:
340 /* Unsupported subcommand. */
341 printk(BIOS_WARNING, "Unsupported cr50 subcommand: 0x%04x\n",
342 *sub_command);
Aaron Durbinee049fa2017-03-25 00:38:45 -0500343 rc = -1;
Aaron Durbineeb77372017-03-08 11:23:11 -0600344 break;
345 }
Aaron Durbinee049fa2017-03-25 00:38:45 -0500346 return rc;
Aaron Durbineeb77372017-03-08 11:23:11 -0600347}
348
Caveh Jalalic07fb752020-09-11 22:10:55 -0700349int tpm_marshal_command(TPM_CC command, const void *tpm_command_body, struct obuf *ob)
Vadim Bendebury627afc22016-06-19 12:13:18 -0700350{
Aaron Durbinee049fa2017-03-25 00:38:45 -0500351 struct obuf ob_hdr;
352 const size_t hdr_sz = sizeof(uint16_t) + 2 * sizeof(uint32_t);
353 int rc = 0;
Vadim Bendebury627afc22016-06-19 12:13:18 -0700354
Arthur Heymans0ca944b2019-11-20 19:51:06 +0100355 tpm_tag = TPM_ST_NO_SESSIONS;
Vadim Bendebury627afc22016-06-19 12:13:18 -0700356
Aaron Durbinee049fa2017-03-25 00:38:45 -0500357 if (obuf_splice_current(ob, &ob_hdr, hdr_sz) < 0)
358 return -1;
359
360 /* Write TPM command header with placeholder field values. */
361 rc |= obuf_write_be16(ob, 0);
362 rc |= obuf_write_be32(ob, 0);
363 rc |= obuf_write_be32(ob, command);
364
365 if (rc != 0)
366 return rc;
367
Vadim Bendebury627afc22016-06-19 12:13:18 -0700368 switch (command) {
369 case TPM2_Startup:
Aaron Durbinee049fa2017-03-25 00:38:45 -0500370 rc |= marshal_startup(ob, tpm_command_body);
Vadim Bendebury627afc22016-06-19 12:13:18 -0700371 break;
372
Joel Kitching2e690ee2018-11-15 16:48:53 +0800373 case TPM2_Shutdown:
374 rc |= marshal_shutdown(ob, tpm_command_body);
375 break;
376
Vadim Bendebury627afc22016-06-19 12:13:18 -0700377 case TPM2_GetCapability:
Aaron Durbinee049fa2017-03-25 00:38:45 -0500378 rc |= marshal_get_capability(ob, tpm_command_body);
Vadim Bendebury627afc22016-06-19 12:13:18 -0700379 break;
380
381 case TPM2_NV_Read:
Aaron Durbinee049fa2017-03-25 00:38:45 -0500382 rc |= marshal_nv_read(ob, tpm_command_body);
Vadim Bendebury627afc22016-06-19 12:13:18 -0700383 break;
384
385 case TPM2_NV_DefineSpace:
Aaron Durbinee049fa2017-03-25 00:38:45 -0500386 rc |= marshal_nv_define_space(ob, tpm_command_body);
Vadim Bendebury627afc22016-06-19 12:13:18 -0700387 break;
388
389 case TPM2_NV_Write:
Aaron Durbinee049fa2017-03-25 00:38:45 -0500390 rc |= marshal_nv_write(ob, tpm_command_body);
Vadim Bendebury627afc22016-06-19 12:13:18 -0700391 break;
392
Vadim Bendebury4c0851c2016-07-03 17:08:10 -0700393 case TPM2_NV_WriteLock:
Aaron Durbinee049fa2017-03-25 00:38:45 -0500394 rc |= marshal_nv_write_lock(ob, tpm_command_body);
Vadim Bendebury4c0851c2016-07-03 17:08:10 -0700395 break;
396
Vadim Bendebury627afc22016-06-19 12:13:18 -0700397 case TPM2_SelfTest:
Aaron Durbinee049fa2017-03-25 00:38:45 -0500398 rc |= marshal_selftest(ob, tpm_command_body);
Vadim Bendebury627afc22016-06-19 12:13:18 -0700399 break;
400
Aaron Durbinf56c7782017-01-10 17:44:42 -0600401 case TPM2_Hierarchy_Control:
Aaron Durbinee049fa2017-03-25 00:38:45 -0500402 rc |= marshal_hierarchy_control(ob, tpm_command_body);
Aaron Durbinf56c7782017-01-10 17:44:42 -0600403 break;
404
Christian Walterc9ac0bc2020-01-28 19:54:33 +0100405 case TPM2_ClearControl:
406 rc |= marshal_clear_control(ob, tpm_command_body);
407 break;
408
Vadim Bendebury6acb9a62016-06-30 20:50:49 -0700409 case TPM2_Clear:
Aaron Durbinee049fa2017-03-25 00:38:45 -0500410 rc |= marshal_clear(ob);
Vadim Bendebury6acb9a62016-06-30 20:50:49 -0700411 break;
412
Vadim Bendeburyf5ef6992016-07-03 22:20:17 -0700413 case TPM2_PCR_Extend:
Aaron Durbinee049fa2017-03-25 00:38:45 -0500414 rc |= marshal_pcr_extend(ob, tpm_command_body);
Vadim Bendeburyf5ef6992016-07-03 22:20:17 -0700415 break;
416
Aaron Durbineeb77372017-03-08 11:23:11 -0600417 case TPM2_CR50_VENDOR_COMMAND:
Aaron Durbinee049fa2017-03-25 00:38:45 -0500418 rc |= marshal_cr50_vendor_command(ob, tpm_command_body);
Aaron Durbineeb77372017-03-08 11:23:11 -0600419 break;
420
Vadim Bendebury627afc22016-06-19 12:13:18 -0700421 default:
Vadim Bendebury627afc22016-06-19 12:13:18 -0700422 printk(BIOS_INFO, "%s:%d:Request to marshal unsupported command %#x\n",
423 __FILE__, __LINE__, command);
Aaron Durbinee049fa2017-03-25 00:38:45 -0500424 rc = -1;
Vadim Bendebury627afc22016-06-19 12:13:18 -0700425 }
426
Aaron Durbinee049fa2017-03-25 00:38:45 -0500427 if (rc != 0)
428 return rc;
Vadim Bendebury627afc22016-06-19 12:13:18 -0700429
Aaron Durbinee049fa2017-03-25 00:38:45 -0500430 /* Fix up the command header with known values. */
Arthur Heymans0ca944b2019-11-20 19:51:06 +0100431 rc |= obuf_write_be16(&ob_hdr, tpm_tag);
Aaron Durbinee049fa2017-03-25 00:38:45 -0500432 rc |= obuf_write_be32(&ob_hdr, obuf_nr_written(ob));
Vadim Bendebury627afc22016-06-19 12:13:18 -0700433
Aaron Durbinee049fa2017-03-25 00:38:45 -0500434 return rc;
Vadim Bendebury627afc22016-06-19 12:13:18 -0700435}
436
Aaron Durbinee049fa2017-03-25 00:38:45 -0500437static int unmarshal_get_capability(struct ibuf *ib,
Caveh Jalalic07fb752020-09-11 22:10:55 -0700438 struct get_cap_response *gcr)
Vadim Bendebury627afc22016-06-19 12:13:18 -0700439{
440 int i;
Aaron Durbinee049fa2017-03-25 00:38:45 -0500441 int rc = 0;
Vadim Bendebury627afc22016-06-19 12:13:18 -0700442
Aaron Durbinee049fa2017-03-25 00:38:45 -0500443 rc |= ibuf_read_be8(ib, &gcr->more_data);
444 rc |= unmarshal_TPM_CAP(ib, &gcr->cd.capability);
Vadim Bendebury627afc22016-06-19 12:13:18 -0700445
Aaron Durbinee049fa2017-03-25 00:38:45 -0500446 if (rc != 0)
447 return rc;
Vadim Bendebury627afc22016-06-19 12:13:18 -0700448
449 switch (gcr->cd.capability) {
450 case TPM_CAP_TPM_PROPERTIES:
Aaron Durbinee049fa2017-03-25 00:38:45 -0500451 if (ibuf_read_be32(ib, &gcr->cd.data.tpmProperties.count))
452 return -1;
Vadim Bendebury627afc22016-06-19 12:13:18 -0700453 if (gcr->cd.data.tpmProperties.count > ARRAY_SIZE
454 (gcr->cd.data.tpmProperties.tpmProperty)) {
455 printk(BIOS_INFO, "%s:%s:%d - %d - too many properties\n",
456 __FILE__, __func__, __LINE__,
457 gcr->cd.data.tpmProperties.count);
Aaron Durbinee049fa2017-03-25 00:38:45 -0500458 return -1;
Vadim Bendebury627afc22016-06-19 12:13:18 -0700459 }
460 for (i = 0; i < gcr->cd.data.tpmProperties.count; i++) {
461 TPMS_TAGGED_PROPERTY *pp;
462
463 pp = gcr->cd.data.tpmProperties.tpmProperty + i;
Aaron Durbinee049fa2017-03-25 00:38:45 -0500464 rc |= unmarshal_TPM_PT(ib, &pp->property);
465 rc |= ibuf_read_be32(ib, &pp->value);
Vadim Bendebury627afc22016-06-19 12:13:18 -0700466 }
467 break;
Frans Hendriks589eff72019-06-26 10:43:40 +0200468 case TPM_CAP_PCRS:
469 if (ibuf_read_be32(ib, &gcr->cd.data.assignedPCR.count))
470 return -1;
471 if (gcr->cd.data.assignedPCR.count >
472 ARRAY_SIZE(gcr->cd.data.assignedPCR.pcrSelections)) {
473 printk(BIOS_INFO, "%s:%s:%d - %d - too many properties\n",
474 __FILE__, __func__, __LINE__,
475 gcr->cd.data.assignedPCR.count);
476 return -1;
477 }
478 for (i = 0; i < gcr->cd.data.assignedPCR.count; i++) {
479 TPMS_PCR_SELECTION *pp =
480 &gcr->cd.data.assignedPCR.pcrSelections[i];
481 rc |= ibuf_read(ib, pp, sizeof(TPMS_PCR_SELECTION));
482 }
483 break;
Vadim Bendebury627afc22016-06-19 12:13:18 -0700484 default:
485 printk(BIOS_ERR,
486 "%s:%d - unable to unmarshal capability response",
487 __func__, __LINE__);
488 printk(BIOS_ERR, " for %d\n", gcr->cd.capability);
Aaron Durbinee049fa2017-03-25 00:38:45 -0500489 rc = -1;
Vadim Bendebury627afc22016-06-19 12:13:18 -0700490 break;
491 }
Aaron Durbinee049fa2017-03-25 00:38:45 -0500492
493 return rc;
Vadim Bendebury627afc22016-06-19 12:13:18 -0700494}
495
Aaron Durbinee049fa2017-03-25 00:38:45 -0500496static int unmarshal_TPM2B_MAX_NV_BUFFER(struct ibuf *ib,
Caveh Jalalic07fb752020-09-11 22:10:55 -0700497 TPM2B_MAX_NV_BUFFER *nv_buffer)
Vadim Bendebury627afc22016-06-19 12:13:18 -0700498{
Aaron Durbinee049fa2017-03-25 00:38:45 -0500499 if (ibuf_read_be16(ib, &nv_buffer->t.size))
500 return -1;
501
502 nv_buffer->t.buffer = ibuf_oob_drain(ib, nv_buffer->t.size);
503
504 if (nv_buffer->t.buffer == NULL) {
Vadim Bendebury627afc22016-06-19 12:13:18 -0700505 printk(BIOS_ERR, "%s:%d - "
Aaron Durbinee049fa2017-03-25 00:38:45 -0500506 "size mismatch: expected %d, remaining %zd\n",
507 __func__, __LINE__, nv_buffer->t.size,
508 ibuf_remaining(ib));
509 return -1;
Vadim Bendebury627afc22016-06-19 12:13:18 -0700510 }
511
Aaron Durbinee049fa2017-03-25 00:38:45 -0500512 return 0;
Vadim Bendebury627afc22016-06-19 12:13:18 -0700513}
514
Aaron Durbinee049fa2017-03-25 00:38:45 -0500515static int unmarshal_nv_read(struct ibuf *ib, struct nv_read_response *nvr)
Vadim Bendebury627afc22016-06-19 12:13:18 -0700516{
517 /* Total size of the parameter field. */
Aaron Durbinee049fa2017-03-25 00:38:45 -0500518 if (ibuf_read_be32(ib, &nvr->params_size))
519 return -1;
520
521 if (unmarshal_TPM2B_MAX_NV_BUFFER(ib, &nvr->buffer))
522 return -1;
Vadim Bendebury627afc22016-06-19 12:13:18 -0700523
524 if (nvr->params_size !=
525 (nvr->buffer.t.size + sizeof(nvr->buffer.t.size))) {
526 printk(BIOS_ERR,
527 "%s:%d - parameter/buffer %d/%d size mismatch",
528 __func__, __LINE__, nvr->params_size,
529 nvr->buffer.t.size);
Aaron Durbinee049fa2017-03-25 00:38:45 -0500530 return -1;
Vadim Bendebury627afc22016-06-19 12:13:18 -0700531 }
532
Vadim Bendebury627afc22016-06-19 12:13:18 -0700533 /*
Frans Hendriks8bd5c992018-10-29 10:47:52 +0100534 * Let's ignore the authorization section. It should be 5 bytes total,
Vadim Bendebury627afc22016-06-19 12:13:18 -0700535 * just confirm that this is the case and report any discrepancy.
536 */
Aaron Durbinee049fa2017-03-25 00:38:45 -0500537 if (ibuf_remaining(ib) != 5)
Vadim Bendebury627afc22016-06-19 12:13:18 -0700538 printk(BIOS_ERR,
Frans Hendriks8bd5c992018-10-29 10:47:52 +0100539 "%s:%d - unexpected authorization section size %zd\n",
Aaron Durbinee049fa2017-03-25 00:38:45 -0500540 __func__, __LINE__, ibuf_remaining(ib));
Vadim Bendebury627afc22016-06-19 12:13:18 -0700541
Aaron Durbinee049fa2017-03-25 00:38:45 -0500542 ibuf_oob_drain(ib, ibuf_remaining(ib));
543
544 return 0;
Vadim Bendebury627afc22016-06-19 12:13:18 -0700545}
546
Aaron Durbinee049fa2017-03-25 00:38:45 -0500547static int unmarshal_vendor_command(struct ibuf *ib,
Caveh Jalalic07fb752020-09-11 22:10:55 -0700548 struct vendor_command_response *vcr)
Vadim Bendebury021ec282017-03-22 16:01:53 -0700549{
Aaron Durbinee049fa2017-03-25 00:38:45 -0500550 if (ibuf_read_be16(ib, &vcr->vc_subcommand))
551 return -1;
Vadim Bendebury021ec282017-03-22 16:01:53 -0700552
553 switch (vcr->vc_subcommand) {
Keith Shorte0f34002019-02-05 16:15:10 -0700554 case TPM2_CR50_SUB_CMD_IMMEDIATE_RESET:
555 break;
Vadim Bendebury021ec282017-03-22 16:01:53 -0700556 case TPM2_CR50_SUB_CMD_NVMEM_ENABLE_COMMITS:
557 break;
558 case TPM2_CR50_SUB_CMD_TURN_UPDATE_ON:
Aaron Durbinee049fa2017-03-25 00:38:45 -0500559 return ibuf_read_be8(ib, &vcr->num_restored_headers);
Keith Shorte371d422019-01-11 07:52:32 -0700560 case TPM2_CR50_SUB_CMD_GET_REC_BTN:
561 return ibuf_read_be8(ib, &vcr->recovery_button_state);
Keith Shorte0f34002019-02-05 16:15:10 -0700562 case TPM2_CR50_SUB_CMD_TPM_MODE:
563 return ibuf_read_be8(ib, &vcr->tpm_mode);
dnojiri622c6b82020-04-03 10:51:50 -0700564 case TPM2_CR50_SUB_CMD_GET_BOOT_MODE:
565 return ibuf_read_be8(ib, &vcr->boot_mode);
Karthikeyan Ramasubramanianba7b90e2021-03-01 13:50:20 -0700566 case TPM2_CR50_SUB_CMD_RESET_EC:
567 break;
Vadim Bendebury021ec282017-03-22 16:01:53 -0700568 default:
569 printk(BIOS_ERR,
570 "%s:%d - unsupported vendor command %#04x!\n",
571 __func__, __LINE__, vcr->vc_subcommand);
Aaron Durbinee049fa2017-03-25 00:38:45 -0500572 return -1;
Vadim Bendebury021ec282017-03-22 16:01:53 -0700573 }
Aaron Durbinee049fa2017-03-25 00:38:45 -0500574
575 return 0;
Vadim Bendebury021ec282017-03-22 16:01:53 -0700576}
577
Aaron Durbinee049fa2017-03-25 00:38:45 -0500578struct tpm2_response *tpm_unmarshal_response(TPM_CC command, struct ibuf *ib)
Vadim Bendebury627afc22016-06-19 12:13:18 -0700579{
Arthur Heymans0ca944b2019-11-20 19:51:06 +0100580 static struct tpm2_response tpm2_static_resp;
Aaron Durbinee049fa2017-03-25 00:38:45 -0500581 int rc = 0;
Vadim Bendebury627afc22016-06-19 12:13:18 -0700582
Arthur Heymans0ca944b2019-11-20 19:51:06 +0100583 rc |= ibuf_read_be16(ib, &tpm2_static_resp.hdr.tpm_tag);
584 rc |= ibuf_read_be32(ib, &tpm2_static_resp.hdr.tpm_size);
585 rc |= unmarshal_TPM_CC(ib, &tpm2_static_resp.hdr.tpm_code);
Aaron Durbinee049fa2017-03-25 00:38:45 -0500586
587 if (rc != 0)
Vadim Bendebury627afc22016-06-19 12:13:18 -0700588 return NULL;
589
Julius Werner71a13142020-05-06 11:11:03 -0700590 if (ibuf_capacity(ib) != tpm2_static_resp.hdr.tpm_size) {
591 printk(BIOS_ERR,
592 "%s: size mismatch in response to command %#x\n",
593 __func__, command);
594 return NULL;
Vadim Bendebury627afc22016-06-19 12:13:18 -0700595 }
596
Julius Werner71a13142020-05-06 11:11:03 -0700597 /* On errors, we're not sure what the TPM is returning. None of the
598 commands we use actually expect useful data payloads for errors, so
599 just ignore any data after the header. */
600 if (tpm2_static_resp.hdr.tpm_code != TPM2_RC_SUCCESS)
601 return &tpm2_static_resp;
602
Vadim Bendebury627afc22016-06-19 12:13:18 -0700603 switch (command) {
604 case TPM2_Startup:
Joel Kitching2e690ee2018-11-15 16:48:53 +0800605 case TPM2_Shutdown:
Julius Werner71a13142020-05-06 11:11:03 -0700606 case TPM2_SelfTest:
Vadim Bendebury627afc22016-06-19 12:13:18 -0700607 break;
608
609 case TPM2_GetCapability:
Arthur Heymans0ca944b2019-11-20 19:51:06 +0100610 rc |= unmarshal_get_capability(ib, &tpm2_static_resp.gc);
Vadim Bendebury627afc22016-06-19 12:13:18 -0700611 break;
612
613 case TPM2_NV_Read:
Arthur Heymans0ca944b2019-11-20 19:51:06 +0100614 rc |= unmarshal_nv_read(ib, &tpm2_static_resp.nvr);
Vadim Bendebury627afc22016-06-19 12:13:18 -0700615 break;
616
Aaron Durbinf56c7782017-01-10 17:44:42 -0600617 case TPM2_Hierarchy_Control:
Vadim Bendebury6acb9a62016-06-30 20:50:49 -0700618 case TPM2_Clear:
Christian Walterc9ac0bc2020-01-28 19:54:33 +0100619 case TPM2_ClearControl:
Vadim Bendebury627afc22016-06-19 12:13:18 -0700620 case TPM2_NV_DefineSpace:
621 case TPM2_NV_Write:
Vadim Bendebury4c0851c2016-07-03 17:08:10 -0700622 case TPM2_NV_WriteLock:
Vadim Bendeburyf5ef6992016-07-03 22:20:17 -0700623 case TPM2_PCR_Extend:
Vadim Bendebury627afc22016-06-19 12:13:18 -0700624 /* Session data included in response can be safely ignored. */
Aaron Durbinee049fa2017-03-25 00:38:45 -0500625 ibuf_oob_drain(ib, ibuf_remaining(ib));
Vadim Bendebury627afc22016-06-19 12:13:18 -0700626 break;
627
Aaron Durbineeb77372017-03-08 11:23:11 -0600628 case TPM2_CR50_VENDOR_COMMAND:
Arthur Heymans0ca944b2019-11-20 19:51:06 +0100629 rc |= unmarshal_vendor_command(ib, &tpm2_static_resp.vcr);
Aaron Durbineeb77372017-03-08 11:23:11 -0600630 break;
631
Vadim Bendebury627afc22016-06-19 12:13:18 -0700632 default:
633 {
Aaron Durbinee049fa2017-03-25 00:38:45 -0500634 size_t i;
635 size_t sz_left;
636 const uint8_t *data;
Vadim Bendebury627afc22016-06-19 12:13:18 -0700637
638 printk(BIOS_INFO, "%s:%d:"
639 "Request to unmarshal unexpected command %#x,"
640 " code %#x",
641 __func__, __LINE__, command,
Arthur Heymans0ca944b2019-11-20 19:51:06 +0100642 tpm2_static_resp.hdr.tpm_code);
Vadim Bendebury627afc22016-06-19 12:13:18 -0700643
Aaron Durbinee049fa2017-03-25 00:38:45 -0500644 sz_left = ibuf_remaining(ib);
645 data = ibuf_oob_drain(ib, sz_left);
646
647 for (i = 0; i < sz_left; i++) {
Vadim Bendebury627afc22016-06-19 12:13:18 -0700648 if (!(i % 16))
649 printk(BIOS_INFO, "\n");
Aaron Durbinee049fa2017-03-25 00:38:45 -0500650 printk(BIOS_INFO, "%2.2x ", data[i]);
Vadim Bendebury627afc22016-06-19 12:13:18 -0700651 }
652 }
653 printk(BIOS_INFO, "\n");
654 return NULL;
655 }
656
Aaron Durbinee049fa2017-03-25 00:38:45 -0500657 if (ibuf_remaining(ib)) {
Vadim Bendebury627afc22016-06-19 12:13:18 -0700658 printk(BIOS_INFO,
659 "%s:%d got %d bytes back in response to %#x,"
Aaron Durbinee049fa2017-03-25 00:38:45 -0500660 " failed to parse (%zd)\n",
Arthur Heymans0ca944b2019-11-20 19:51:06 +0100661 __func__, __LINE__, tpm2_static_resp.hdr.tpm_size,
Aaron Durbinee049fa2017-03-25 00:38:45 -0500662 command, ibuf_remaining(ib));
Vadim Bendebury627afc22016-06-19 12:13:18 -0700663 return NULL;
664 }
Richard Spiegel248c60a2018-08-07 09:24:14 -0700665 if (rc)
666 printk(BIOS_WARNING, "Warning: %s had one or more failures.\n",
667 __func__);
Vadim Bendebury627afc22016-06-19 12:13:18 -0700668
669 /* The entire message have been parsed. */
Arthur Heymans0ca944b2019-11-20 19:51:06 +0100670 return &tpm2_static_resp;
Vadim Bendebury627afc22016-06-19 12:13:18 -0700671}