1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (c) 2013 Google, Inc
7 #include <display_options.h>
10 #include <asm/state.h>
11 #include <asm/unaligned.h>
12 #include <u-boot/crc.h>
13 #include "sandbox_common.h"
15 #define NV_DATA_PUBLIC_PERMISSIONS_OFFSET 60
18 * Information about our TPM emulation. This is preserved in the sandbox
19 * state file if enabled.
21 static struct tpm_state {
23 struct nvdata_state nvdata[NV_SEQ_COUNT];
27 * sandbox_tpm_read_state() - read the sandbox EC state from the state file
29 * If data is available, then blob and node will provide access to it. If
30 * not this function sets up an empty TPM.
32 * @blob: Pointer to device tree blob, or NULL if no data to read
33 * @node: Node offset to read from
35 static int sandbox_tpm_read_state(const void *blob, int node)
37 struct tpm_state *state = &s_state;
45 for (i = 0; i < NV_SEQ_COUNT; i++) {
46 struct nvdata_state *nvd = &state->nvdata[i];
49 sprintf(prop_name, "nvdata%d", i);
50 prop = fdt_getprop(blob, node, prop_name, &len);
51 if (len >= NV_DATA_SIZE)
52 return log_msg_ret("nvd", -E2BIG);
54 memcpy(nvd->data, prop, len);
66 * sandbox_tpm_write_state() - Write out our state to the state file
68 * The caller will ensure that there is a node ready for the state. The node
69 * may already contain the old state, in which case it is overridden.
71 * @blob: Device tree blob holding state
72 * @node: Node to write our state into
74 static int sandbox_tpm_write_state(void *blob, int node)
76 const struct tpm_state *state = g_state;
83 * We are guaranteed enough space to write basic properties.
84 * We could use fdt_add_subnode() to put each set of data in its
85 * own node - perhaps useful if we add access informaiton to each.
87 for (i = 0; i < NV_SEQ_COUNT; i++) {
88 const struct nvdata_state *nvd = &state->nvdata[i];
92 snprintf(prop_name, sizeof(prop_name), "nvdata%d", i);
93 fdt_setprop(blob, node, prop_name, nvd->data,
101 SANDBOX_STATE_IO(sandbox_tpm, "google,sandbox-tpm", sandbox_tpm_read_state,
102 sandbox_tpm_write_state);
104 static void handle_cap_flag_space(u8 **datap, uint index)
106 struct tpm_nv_data_public pub;
108 /* TPM_NV_PER_PPWRITE */
109 memset(&pub, '\0', sizeof(pub));
110 pub.nv_index = __cpu_to_be32(index);
111 pub.pcr_info_read.pcr_selection.size_of_select = __cpu_to_be16(
112 sizeof(pub.pcr_info_read.pcr_selection.pcr_select));
113 pub.permission.attributes = __cpu_to_be32(1);
114 pub.pcr_info_write = pub.pcr_info_read;
115 memcpy(*datap, &pub, sizeof(pub));
116 *datap += sizeof(pub);
119 static int sandbox_tpm_xfer(struct udevice *dev, const uint8_t *sendbuf,
120 size_t send_size, uint8_t *recvbuf,
123 struct tpm_state *tpm = dev_get_priv(dev);
124 uint32_t code, index, length, type;
128 code = get_unaligned_be32(sendbuf + sizeof(uint16_t) +
131 printf("tpm: %zd bytes, recv_len %zd, cmd = %x\n", send_size,
133 print_buffer(0, sendbuf, 1, send_size, 0);
136 case TPM_CMD_GET_CAPABILITY:
137 type = get_unaligned_be32(sendbuf + 14);
140 index = get_unaligned_be32(sendbuf + 18);
141 printf("Get flags index %#02x\n", index);
143 memset(recvbuf, '\0', *recv_len);
144 data = recvbuf + TPM_HDR_LEN + sizeof(uint32_t);
146 case FIRMWARE_NV_INDEX:
148 case KERNEL_NV_INDEX:
149 handle_cap_flag_space(&data, index);
150 *recv_len = data - recvbuf;
152 case TPM_CAP_FLAG_PERMANENT: {
153 struct tpm_permanent_flags *pflags;
155 pflags = (struct tpm_permanent_flags *)data;
156 memset(pflags, '\0', sizeof(*pflags));
157 put_unaligned_be32(TPM_TAG_PERMANENT_FLAGS,
159 *recv_len = TPM_HEADER_SIZE + 4 +
164 printf(" ** Unknown flags index %x\n", index);
167 put_unaligned_be32(*recv_len, recvbuf + TPM_HDR_LEN);
169 case TPM_CAP_NV_INDEX:
170 index = get_unaligned_be32(sendbuf + 18);
171 printf("Get cap nv index %#02x\n", index);
172 put_unaligned_be32(22, recvbuf + TPM_HDR_LEN);
175 printf(" ** Unknown 0x65 command type %#02x\n",
180 case TPM_CMD_NV_WRITE_VALUE:
181 index = get_unaligned_be32(sendbuf + 10);
182 length = get_unaligned_be32(sendbuf + 18);
183 seq = sb_tpm_index_to_seq(index);
186 printf("tpm: nvwrite index=%#02x, len=%#02x\n", index, length);
187 sb_tpm_write_data(tpm->nvdata, seq, sendbuf, 22, length);
189 case TPM_CMD_NV_READ_VALUE: /* nvread */
190 index = get_unaligned_be32(sendbuf + 10);
191 length = get_unaligned_be32(sendbuf + 18);
192 seq = sb_tpm_index_to_seq(index);
195 printf("tpm: nvread index=%#02x, len=%#02x, seq=%#02x\n", index,
197 *recv_len = TPM_HDR_LEN + sizeof(uint32_t) + length;
198 memset(recvbuf, '\0', *recv_len);
199 put_unaligned_be32(length, recvbuf + TPM_HDR_LEN);
200 sb_tpm_read_data(tpm->nvdata, seq, recvbuf, TPM_HDR_LEN + 4,
205 memset(recvbuf, '\0', *recv_len);
207 case TPM_CMD_NV_DEFINE_SPACE:
208 index = get_unaligned_be32(sendbuf + 12);
209 length = get_unaligned_be32(sendbuf + 77);
210 seq = sb_tpm_index_to_seq(index);
213 printf("tpm: define_space index=%#02x, len=%#02x, seq=%#02x\n",
215 sb_tpm_define_data(tpm->nvdata, seq, length);
217 memset(recvbuf, '\0', *recv_len);
219 case 0x15: /* pcr read */
220 case 0x5d: /* force clear */
221 case 0x6f: /* physical enable */
222 case 0x72: /* physical set deactivated */
223 case 0x99: /* startup */
224 case 0x50: /* self test full */
225 case 0x4000000a: /* assert physical presence */
227 memset(recvbuf, '\0', *recv_len);
230 printf("Unknown tpm command %02x\n", code);
234 printf("tpm: rx recv_len %zd\n", *recv_len);
235 print_buffer(0, recvbuf, 1, *recv_len, 0);
241 static int sandbox_tpm_get_desc(struct udevice *dev, char *buf, int size)
246 return snprintf(buf, size, "sandbox TPM");
249 static int sandbox_tpm_probe(struct udevice *dev)
251 struct tpm_state *tpm = dev_get_priv(dev);
254 memcpy(tpm, &s_state, sizeof(*tpm));
260 static int sandbox_tpm_open(struct udevice *dev)
265 static int sandbox_tpm_close(struct udevice *dev)
270 static const struct tpm_ops sandbox_tpm_ops = {
271 .open = sandbox_tpm_open,
272 .close = sandbox_tpm_close,
273 .get_desc = sandbox_tpm_get_desc,
274 .xfer = sandbox_tpm_xfer,
277 static const struct udevice_id sandbox_tpm_ids[] = {
278 { .compatible = "google,sandbox-tpm" },
282 U_BOOT_DRIVER(google_sandbox_tpm) = {
283 .name = "google_sandbox_tpm",
285 .of_match = sandbox_tpm_ids,
286 .ops = &sandbox_tpm_ops,
287 .probe = sandbox_tpm_probe,
288 .priv_auto = sizeof(struct tpm_state),