blob: f22ed846f0ab8729490df51277abfdb0675a25b6 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Simon Glassba93f4e2013-11-10 10:27:05 -07002/*
3 * Copyright (c) 2013 Google, Inc
Simon Glassba93f4e2013-11-10 10:27:05 -07004 */
5
6#include <common.h>
Simon Glassa425f762015-08-22 18:31:35 -06007#include <dm.h>
Miquel Raynal4c6759e2018-05-15 11:57:06 +02008#include <tpm-v1.h>
Simon Glassba93f4e2013-11-10 10:27:05 -07009#include <asm/state.h>
10#include <asm/unaligned.h>
Simon Glassca9b0af2019-11-14 12:57:14 -070011#include <u-boot/crc.h>
Simon Glassdf884ec2021-07-18 14:17:57 -060012#include "sandbox_common.h"
Simon Glassba93f4e2013-11-10 10:27:05 -070013
14#define NV_DATA_PUBLIC_PERMISSIONS_OFFSET 60
15
Simon Glassba93f4e2013-11-10 10:27:05 -070016/*
17 * Information about our TPM emulation. This is preserved in the sandbox
18 * state file if enabled.
19 */
20static struct tpm_state {
Simon Glassf99c3292018-10-01 11:55:18 -060021 bool valid;
22 struct nvdata_state nvdata[NV_SEQ_COUNT];
Simon Glass689cde02021-07-18 14:17:58 -060023} s_state, *g_state;
Simon Glassba93f4e2013-11-10 10:27:05 -070024
25/**
26 * sandbox_tpm_read_state() - read the sandbox EC state from the state file
27 *
28 * If data is available, then blob and node will provide access to it. If
29 * not this function sets up an empty TPM.
30 *
31 * @blob: Pointer to device tree blob, or NULL if no data to read
32 * @node: Node offset to read from
33 */
34static int sandbox_tpm_read_state(const void *blob, int node)
35{
Simon Glass689cde02021-07-18 14:17:58 -060036 struct tpm_state *state = &s_state;
Simon Glassba93f4e2013-11-10 10:27:05 -070037 const char *prop;
38 int len;
39 int i;
40
41 if (!blob)
42 return 0;
43
44 for (i = 0; i < NV_SEQ_COUNT; i++) {
Simon Glass689cde02021-07-18 14:17:58 -060045 struct nvdata_state *nvd = &state->nvdata[i];
Simon Glassba93f4e2013-11-10 10:27:05 -070046 char prop_name[20];
47
48 sprintf(prop_name, "nvdata%d", i);
49 prop = fdt_getprop(blob, node, prop_name, &len);
Simon Glass689cde02021-07-18 14:17:58 -060050 if (len >= NV_DATA_SIZE)
51 return log_msg_ret("nvd", -E2BIG);
52 if (prop) {
53 memcpy(nvd->data, prop, len);
54 nvd->length = len;
55 nvd->present = true;
Simon Glassf99c3292018-10-01 11:55:18 -060056 }
Simon Glassba93f4e2013-11-10 10:27:05 -070057 }
Simon Glass689cde02021-07-18 14:17:58 -060058
59 s_state.valid = true;
Simon Glassba93f4e2013-11-10 10:27:05 -070060
61 return 0;
62}
63
64/**
Simon Glass689cde02021-07-18 14:17:58 -060065 * sandbox_tpm_write_state() - Write out our state to the state file
Simon Glassba93f4e2013-11-10 10:27:05 -070066 *
67 * The caller will ensure that there is a node ready for the state. The node
68 * may already contain the old state, in which case it is overridden.
69 *
70 * @blob: Device tree blob holding state
71 * @node: Node to write our state into
72 */
73static int sandbox_tpm_write_state(void *blob, int node)
74{
Simon Glass689cde02021-07-18 14:17:58 -060075 const struct tpm_state *state = g_state;
Simon Glassba93f4e2013-11-10 10:27:05 -070076 int i;
77
Simon Glass689cde02021-07-18 14:17:58 -060078 if (!state)
79 return 0;
80
Simon Glassba93f4e2013-11-10 10:27:05 -070081 /*
82 * We are guaranteed enough space to write basic properties.
83 * We could use fdt_add_subnode() to put each set of data in its
84 * own node - perhaps useful if we add access informaiton to each.
85 */
86 for (i = 0; i < NV_SEQ_COUNT; i++) {
Simon Glass689cde02021-07-18 14:17:58 -060087 const struct nvdata_state *nvd = &state->nvdata[i];
Simon Glassba93f4e2013-11-10 10:27:05 -070088 char prop_name[20];
89
Simon Glass689cde02021-07-18 14:17:58 -060090 if (nvd->present) {
91 snprintf(prop_name, sizeof(prop_name), "nvdata%d", i);
92 fdt_setprop(blob, node, prop_name, nvd->data,
93 nvd->length);
Simon Glassf99c3292018-10-01 11:55:18 -060094 }
Simon Glassba93f4e2013-11-10 10:27:05 -070095 }
96
97 return 0;
98}
99
100SANDBOX_STATE_IO(sandbox_tpm, "google,sandbox-tpm", sandbox_tpm_read_state,
101 sandbox_tpm_write_state);
102
Simon Glassf99c3292018-10-01 11:55:18 -0600103static void handle_cap_flag_space(u8 **datap, uint index)
104{
105 struct tpm_nv_data_public pub;
106
107 /* TPM_NV_PER_PPWRITE */
108 memset(&pub, '\0', sizeof(pub));
109 pub.nv_index = __cpu_to_be32(index);
110 pub.pcr_info_read.pcr_selection.size_of_select = __cpu_to_be16(
111 sizeof(pub.pcr_info_read.pcr_selection.pcr_select));
112 pub.permission.attributes = __cpu_to_be32(1);
113 pub.pcr_info_write = pub.pcr_info_read;
114 memcpy(*datap, &pub, sizeof(pub));
115 *datap += sizeof(pub);
116}
117
Simon Glassa425f762015-08-22 18:31:35 -0600118static int sandbox_tpm_xfer(struct udevice *dev, const uint8_t *sendbuf,
119 size_t send_size, uint8_t *recvbuf,
120 size_t *recv_len)
Simon Glassba93f4e2013-11-10 10:27:05 -0700121{
Simon Glassa425f762015-08-22 18:31:35 -0600122 struct tpm_state *tpm = dev_get_priv(dev);
Simon Glassba93f4e2013-11-10 10:27:05 -0700123 uint32_t code, index, length, type;
124 uint8_t *data;
125 int seq;
126
127 code = get_unaligned_be32(sendbuf + sizeof(uint16_t) +
128 sizeof(uint32_t));
Simon Glass2b45c482018-11-06 15:21:23 -0700129#ifdef DEBUG
Simon Glassba93f4e2013-11-10 10:27:05 -0700130 printf("tpm: %zd bytes, recv_len %zd, cmd = %x\n", send_size,
131 *recv_len, code);
132 print_buffer(0, sendbuf, 1, send_size, 0);
Simon Glass2b45c482018-11-06 15:21:23 -0700133#endif
Simon Glassba93f4e2013-11-10 10:27:05 -0700134 switch (code) {
Simon Glass5ff3f162018-10-01 11:55:17 -0600135 case TPM_CMD_GET_CAPABILITY:
Simon Glassba93f4e2013-11-10 10:27:05 -0700136 type = get_unaligned_be32(sendbuf + 14);
137 switch (type) {
Simon Glass5ff3f162018-10-01 11:55:17 -0600138 case TPM_CAP_FLAG:
Simon Glassba93f4e2013-11-10 10:27:05 -0700139 index = get_unaligned_be32(sendbuf + 18);
140 printf("Get flags index %#02x\n", index);
141 *recv_len = 22;
142 memset(recvbuf, '\0', *recv_len);
Simon Glassba93f4e2013-11-10 10:27:05 -0700143 data = recvbuf + TPM_RESPONSE_HEADER_LENGTH +
144 sizeof(uint32_t);
145 switch (index) {
146 case FIRMWARE_NV_INDEX:
147 break;
148 case KERNEL_NV_INDEX:
Simon Glassf99c3292018-10-01 11:55:18 -0600149 handle_cap_flag_space(&data, index);
150 *recv_len = data - recvbuf -
151 TPM_RESPONSE_HEADER_LENGTH -
152 sizeof(uint32_t);
153 break;
154 case TPM_CAP_FLAG_PERMANENT: {
155 struct tpm_permanent_flags *pflags;
156
157 pflags = (struct tpm_permanent_flags *)data;
158 memset(pflags, '\0', sizeof(*pflags));
159 put_unaligned_be32(TPM_TAG_PERMANENT_FLAGS,
160 &pflags->tag);
161 *recv_len = TPM_HEADER_SIZE + 4 +
162 sizeof(*pflags);
Simon Glassba93f4e2013-11-10 10:27:05 -0700163 break;
164 }
Simon Glassf99c3292018-10-01 11:55:18 -0600165 default:
166 printf(" ** Unknown flags index %x\n", index);
167 return -ENOSYS;
168 }
169 put_unaligned_be32(*recv_len,
170 recvbuf +
171 TPM_RESPONSE_HEADER_LENGTH);
Simon Glassba93f4e2013-11-10 10:27:05 -0700172 break;
Simon Glass5ff3f162018-10-01 11:55:17 -0600173 case TPM_CAP_NV_INDEX:
Simon Glassba93f4e2013-11-10 10:27:05 -0700174 index = get_unaligned_be32(sendbuf + 18);
175 printf("Get cap nv index %#02x\n", index);
176 put_unaligned_be32(22, recvbuf +
177 TPM_RESPONSE_HEADER_LENGTH);
178 break;
179 default:
180 printf(" ** Unknown 0x65 command type %#02x\n",
181 type);
Simon Glass5ff3f162018-10-01 11:55:17 -0600182 return -ENOSYS;
Simon Glassba93f4e2013-11-10 10:27:05 -0700183 }
184 break;
Simon Glass5ff3f162018-10-01 11:55:17 -0600185 case TPM_CMD_NV_WRITE_VALUE:
Simon Glassba93f4e2013-11-10 10:27:05 -0700186 index = get_unaligned_be32(sendbuf + 10);
187 length = get_unaligned_be32(sendbuf + 18);
Simon Glassdf884ec2021-07-18 14:17:57 -0600188 seq = sb_tpm_index_to_seq(index);
Simon Glassba93f4e2013-11-10 10:27:05 -0700189 if (seq < 0)
Simon Glass5ff3f162018-10-01 11:55:17 -0600190 return -EINVAL;
Simon Glassba93f4e2013-11-10 10:27:05 -0700191 printf("tpm: nvwrite index=%#02x, len=%#02x\n", index, length);
Simon Glassdf884ec2021-07-18 14:17:57 -0600192 sb_tpm_write_data(tpm->nvdata, seq, sendbuf, 22, length);
Simon Glassba93f4e2013-11-10 10:27:05 -0700193 break;
Simon Glass5ff3f162018-10-01 11:55:17 -0600194 case TPM_CMD_NV_READ_VALUE: /* nvread */
Simon Glassba93f4e2013-11-10 10:27:05 -0700195 index = get_unaligned_be32(sendbuf + 10);
196 length = get_unaligned_be32(sendbuf + 18);
Simon Glassdf884ec2021-07-18 14:17:57 -0600197 seq = sb_tpm_index_to_seq(index);
Simon Glassba93f4e2013-11-10 10:27:05 -0700198 if (seq < 0)
Simon Glass5ff3f162018-10-01 11:55:17 -0600199 return -EINVAL;
Simon Glassf99c3292018-10-01 11:55:18 -0600200 printf("tpm: nvread index=%#02x, len=%#02x, seq=%#02x\n", index,
201 length, seq);
Simon Glassdf884ec2021-07-18 14:17:57 -0600202 *recv_len = TPM_HDR_LEN + sizeof(uint32_t) + length;
Simon Glassba93f4e2013-11-10 10:27:05 -0700203 memset(recvbuf, '\0', *recv_len);
Simon Glassdf884ec2021-07-18 14:17:57 -0600204 put_unaligned_be32(length, recvbuf + TPM_HDR_LEN);
205 sb_tpm_read_data(tpm->nvdata, seq, recvbuf, TPM_HDR_LEN + 4,
206 length);
Simon Glassba93f4e2013-11-10 10:27:05 -0700207 break;
Simon Glassf99c3292018-10-01 11:55:18 -0600208 case TPM_CMD_EXTEND:
209 *recv_len = 30;
210 memset(recvbuf, '\0', *recv_len);
211 break;
212 case TPM_CMD_NV_DEFINE_SPACE:
Simon Glassba93f4e2013-11-10 10:27:05 -0700213 case 0x15: /* pcr read */
214 case 0x5d: /* force clear */
215 case 0x6f: /* physical enable */
216 case 0x72: /* physical set deactivated */
217 case 0x99: /* startup */
Simon Glassf99c3292018-10-01 11:55:18 -0600218 case 0x50: /* self test full */
Simon Glassba93f4e2013-11-10 10:27:05 -0700219 case 0x4000000a: /* assert physical presence */
220 *recv_len = 12;
221 memset(recvbuf, '\0', *recv_len);
222 break;
223 default:
224 printf("Unknown tpm command %02x\n", code);
Simon Glass5ff3f162018-10-01 11:55:17 -0600225 return -ENOSYS;
Simon Glassba93f4e2013-11-10 10:27:05 -0700226 }
Simon Glass2b45c482018-11-06 15:21:23 -0700227#ifdef DEBUG
228 printf("tpm: rx recv_len %zd\n", *recv_len);
229 print_buffer(0, recvbuf, 1, *recv_len, 0);
230#endif
Simon Glassba93f4e2013-11-10 10:27:05 -0700231
232 return 0;
233}
234
Simon Glassa425f762015-08-22 18:31:35 -0600235static int sandbox_tpm_get_desc(struct udevice *dev, char *buf, int size)
Simon Glassba93f4e2013-11-10 10:27:05 -0700236{
Simon Glassa425f762015-08-22 18:31:35 -0600237 if (size < 15)
238 return -ENOSPC;
239
240 return snprintf(buf, size, "sandbox TPM");
241}
242
243static int sandbox_tpm_probe(struct udevice *dev)
244{
245 struct tpm_state *tpm = dev_get_priv(dev);
246
Simon Glass689cde02021-07-18 14:17:58 -0600247 if (s_state.valid)
248 memcpy(tpm, &s_state, sizeof(*tpm));
249 g_state = tpm;
Simon Glassa425f762015-08-22 18:31:35 -0600250
Simon Glassba93f4e2013-11-10 10:27:05 -0700251 return 0;
252}
253
Simon Glassa425f762015-08-22 18:31:35 -0600254static int sandbox_tpm_open(struct udevice *dev)
Simon Glassba93f4e2013-11-10 10:27:05 -0700255{
Simon Glassba93f4e2013-11-10 10:27:05 -0700256 return 0;
257}
258
Simon Glassa425f762015-08-22 18:31:35 -0600259static int sandbox_tpm_close(struct udevice *dev)
Simon Glassba93f4e2013-11-10 10:27:05 -0700260{
Simon Glassba93f4e2013-11-10 10:27:05 -0700261 return 0;
262}
Simon Glassa425f762015-08-22 18:31:35 -0600263
264static const struct tpm_ops sandbox_tpm_ops = {
265 .open = sandbox_tpm_open,
266 .close = sandbox_tpm_close,
267 .get_desc = sandbox_tpm_get_desc,
268 .xfer = sandbox_tpm_xfer,
269};
270
271static const struct udevice_id sandbox_tpm_ids[] = {
272 { .compatible = "google,sandbox-tpm" },
273 { }
274};
275
Walter Lozano2901ac62020-06-25 01:10:04 -0300276U_BOOT_DRIVER(google_sandbox_tpm) = {
277 .name = "google_sandbox_tpm",
Simon Glassa425f762015-08-22 18:31:35 -0600278 .id = UCLASS_TPM,
279 .of_match = sandbox_tpm_ids,
280 .ops = &sandbox_tpm_ops,
281 .probe = sandbox_tpm_probe,
Simon Glass8a2b47f2020-12-03 16:55:17 -0700282 .priv_auto = sizeof(struct tpm_state),
Simon Glassa425f762015-08-22 18:31:35 -0600283};