Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Simon Glass | ed3f5a3 | 2013-11-10 10:27:05 -0700 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (c) 2013 Google, Inc |
Simon Glass | ed3f5a3 | 2013-11-10 10:27:05 -0700 | [diff] [blame] | 4 | */ |
| 5 | |
| 6 | #include <common.h> |
Simon Glass | 4e4bf94 | 2022-07-31 12:28:48 -0600 | [diff] [blame] | 7 | #include <display_options.h> |
Simon Glass | 21baf15 | 2015-08-22 18:31:35 -0600 | [diff] [blame] | 8 | #include <dm.h> |
Miquel Raynal | d677bfe | 2018-05-15 11:57:06 +0200 | [diff] [blame] | 9 | #include <tpm-v1.h> |
Simon Glass | ed3f5a3 | 2013-11-10 10:27:05 -0700 | [diff] [blame] | 10 | #include <asm/state.h> |
| 11 | #include <asm/unaligned.h> |
Simon Glass | c3a4d1c | 2019-11-14 12:57:14 -0700 | [diff] [blame] | 12 | #include <u-boot/crc.h> |
Simon Glass | 974c98f | 2021-07-18 14:17:57 -0600 | [diff] [blame] | 13 | #include "sandbox_common.h" |
Simon Glass | ed3f5a3 | 2013-11-10 10:27:05 -0700 | [diff] [blame] | 14 | |
| 15 | #define NV_DATA_PUBLIC_PERMISSIONS_OFFSET 60 |
| 16 | |
Simon Glass | ed3f5a3 | 2013-11-10 10:27:05 -0700 | [diff] [blame] | 17 | /* |
| 18 | * Information about our TPM emulation. This is preserved in the sandbox |
| 19 | * state file if enabled. |
| 20 | */ |
| 21 | static struct tpm_state { |
Simon Glass | ef8a250 | 2018-10-01 11:55:18 -0600 | [diff] [blame] | 22 | bool valid; |
| 23 | struct nvdata_state nvdata[NV_SEQ_COUNT]; |
Simon Glass | 1db235a | 2021-07-18 14:17:58 -0600 | [diff] [blame] | 24 | } s_state, *g_state; |
Simon Glass | ed3f5a3 | 2013-11-10 10:27:05 -0700 | [diff] [blame] | 25 | |
| 26 | /** |
| 27 | * sandbox_tpm_read_state() - read the sandbox EC state from the state file |
| 28 | * |
| 29 | * If data is available, then blob and node will provide access to it. If |
| 30 | * not this function sets up an empty TPM. |
| 31 | * |
| 32 | * @blob: Pointer to device tree blob, or NULL if no data to read |
| 33 | * @node: Node offset to read from |
| 34 | */ |
| 35 | static int sandbox_tpm_read_state(const void *blob, int node) |
| 36 | { |
Simon Glass | 1db235a | 2021-07-18 14:17:58 -0600 | [diff] [blame] | 37 | struct tpm_state *state = &s_state; |
Simon Glass | ed3f5a3 | 2013-11-10 10:27:05 -0700 | [diff] [blame] | 38 | const char *prop; |
| 39 | int len; |
| 40 | int i; |
| 41 | |
| 42 | if (!blob) |
| 43 | return 0; |
| 44 | |
| 45 | for (i = 0; i < NV_SEQ_COUNT; i++) { |
Simon Glass | 1db235a | 2021-07-18 14:17:58 -0600 | [diff] [blame] | 46 | struct nvdata_state *nvd = &state->nvdata[i]; |
Simon Glass | ed3f5a3 | 2013-11-10 10:27:05 -0700 | [diff] [blame] | 47 | char prop_name[20]; |
| 48 | |
| 49 | sprintf(prop_name, "nvdata%d", i); |
| 50 | prop = fdt_getprop(blob, node, prop_name, &len); |
Simon Glass | 1db235a | 2021-07-18 14:17:58 -0600 | [diff] [blame] | 51 | if (len >= NV_DATA_SIZE) |
| 52 | return log_msg_ret("nvd", -E2BIG); |
| 53 | if (prop) { |
| 54 | memcpy(nvd->data, prop, len); |
| 55 | nvd->length = len; |
| 56 | nvd->present = true; |
Simon Glass | ef8a250 | 2018-10-01 11:55:18 -0600 | [diff] [blame] | 57 | } |
Simon Glass | ed3f5a3 | 2013-11-10 10:27:05 -0700 | [diff] [blame] | 58 | } |
Simon Glass | 1db235a | 2021-07-18 14:17:58 -0600 | [diff] [blame] | 59 | |
| 60 | s_state.valid = true; |
Simon Glass | ed3f5a3 | 2013-11-10 10:27:05 -0700 | [diff] [blame] | 61 | |
| 62 | return 0; |
| 63 | } |
| 64 | |
| 65 | /** |
Simon Glass | 1db235a | 2021-07-18 14:17:58 -0600 | [diff] [blame] | 66 | * sandbox_tpm_write_state() - Write out our state to the state file |
Simon Glass | ed3f5a3 | 2013-11-10 10:27:05 -0700 | [diff] [blame] | 67 | * |
| 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. |
| 70 | * |
| 71 | * @blob: Device tree blob holding state |
| 72 | * @node: Node to write our state into |
| 73 | */ |
| 74 | static int sandbox_tpm_write_state(void *blob, int node) |
| 75 | { |
Simon Glass | 1db235a | 2021-07-18 14:17:58 -0600 | [diff] [blame] | 76 | const struct tpm_state *state = g_state; |
Simon Glass | ed3f5a3 | 2013-11-10 10:27:05 -0700 | [diff] [blame] | 77 | int i; |
| 78 | |
Simon Glass | 1db235a | 2021-07-18 14:17:58 -0600 | [diff] [blame] | 79 | if (!state) |
| 80 | return 0; |
| 81 | |
Simon Glass | ed3f5a3 | 2013-11-10 10:27:05 -0700 | [diff] [blame] | 82 | /* |
| 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. |
| 86 | */ |
| 87 | for (i = 0; i < NV_SEQ_COUNT; i++) { |
Simon Glass | 1db235a | 2021-07-18 14:17:58 -0600 | [diff] [blame] | 88 | const struct nvdata_state *nvd = &state->nvdata[i]; |
Simon Glass | ed3f5a3 | 2013-11-10 10:27:05 -0700 | [diff] [blame] | 89 | char prop_name[20]; |
| 90 | |
Simon Glass | 1db235a | 2021-07-18 14:17:58 -0600 | [diff] [blame] | 91 | if (nvd->present) { |
| 92 | snprintf(prop_name, sizeof(prop_name), "nvdata%d", i); |
| 93 | fdt_setprop(blob, node, prop_name, nvd->data, |
| 94 | nvd->length); |
Simon Glass | ef8a250 | 2018-10-01 11:55:18 -0600 | [diff] [blame] | 95 | } |
Simon Glass | ed3f5a3 | 2013-11-10 10:27:05 -0700 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | return 0; |
| 99 | } |
| 100 | |
| 101 | SANDBOX_STATE_IO(sandbox_tpm, "google,sandbox-tpm", sandbox_tpm_read_state, |
| 102 | sandbox_tpm_write_state); |
| 103 | |
Simon Glass | ef8a250 | 2018-10-01 11:55:18 -0600 | [diff] [blame] | 104 | static void handle_cap_flag_space(u8 **datap, uint index) |
| 105 | { |
| 106 | struct tpm_nv_data_public pub; |
| 107 | |
| 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); |
| 117 | } |
| 118 | |
Simon Glass | 21baf15 | 2015-08-22 18:31:35 -0600 | [diff] [blame] | 119 | static int sandbox_tpm_xfer(struct udevice *dev, const uint8_t *sendbuf, |
| 120 | size_t send_size, uint8_t *recvbuf, |
| 121 | size_t *recv_len) |
Simon Glass | ed3f5a3 | 2013-11-10 10:27:05 -0700 | [diff] [blame] | 122 | { |
Simon Glass | 21baf15 | 2015-08-22 18:31:35 -0600 | [diff] [blame] | 123 | struct tpm_state *tpm = dev_get_priv(dev); |
Simon Glass | ed3f5a3 | 2013-11-10 10:27:05 -0700 | [diff] [blame] | 124 | uint32_t code, index, length, type; |
| 125 | uint8_t *data; |
| 126 | int seq; |
| 127 | |
| 128 | code = get_unaligned_be32(sendbuf + sizeof(uint16_t) + |
| 129 | sizeof(uint32_t)); |
Simon Glass | 04488c4 | 2018-11-06 15:21:23 -0700 | [diff] [blame] | 130 | #ifdef DEBUG |
Simon Glass | ed3f5a3 | 2013-11-10 10:27:05 -0700 | [diff] [blame] | 131 | printf("tpm: %zd bytes, recv_len %zd, cmd = %x\n", send_size, |
| 132 | *recv_len, code); |
| 133 | print_buffer(0, sendbuf, 1, send_size, 0); |
Simon Glass | 04488c4 | 2018-11-06 15:21:23 -0700 | [diff] [blame] | 134 | #endif |
Simon Glass | ed3f5a3 | 2013-11-10 10:27:05 -0700 | [diff] [blame] | 135 | switch (code) { |
Simon Glass | 998af31 | 2018-10-01 11:55:17 -0600 | [diff] [blame] | 136 | case TPM_CMD_GET_CAPABILITY: |
Simon Glass | ed3f5a3 | 2013-11-10 10:27:05 -0700 | [diff] [blame] | 137 | type = get_unaligned_be32(sendbuf + 14); |
| 138 | switch (type) { |
Simon Glass | 998af31 | 2018-10-01 11:55:17 -0600 | [diff] [blame] | 139 | case TPM_CAP_FLAG: |
Simon Glass | ed3f5a3 | 2013-11-10 10:27:05 -0700 | [diff] [blame] | 140 | index = get_unaligned_be32(sendbuf + 18); |
| 141 | printf("Get flags index %#02x\n", index); |
| 142 | *recv_len = 22; |
| 143 | memset(recvbuf, '\0', *recv_len); |
Simon Glass | 7f350a9 | 2021-07-18 14:18:00 -0600 | [diff] [blame] | 144 | data = recvbuf + TPM_HDR_LEN + sizeof(uint32_t); |
Simon Glass | ed3f5a3 | 2013-11-10 10:27:05 -0700 | [diff] [blame] | 145 | switch (index) { |
| 146 | case FIRMWARE_NV_INDEX: |
| 147 | break; |
| 148 | case KERNEL_NV_INDEX: |
Simon Glass | ef8a250 | 2018-10-01 11:55:18 -0600 | [diff] [blame] | 149 | handle_cap_flag_space(&data, index); |
Simon Glass | 7f350a9 | 2021-07-18 14:18:00 -0600 | [diff] [blame] | 150 | *recv_len = data - recvbuf; |
Simon Glass | ef8a250 | 2018-10-01 11:55:18 -0600 | [diff] [blame] | 151 | break; |
| 152 | case TPM_CAP_FLAG_PERMANENT: { |
| 153 | struct tpm_permanent_flags *pflags; |
| 154 | |
| 155 | pflags = (struct tpm_permanent_flags *)data; |
| 156 | memset(pflags, '\0', sizeof(*pflags)); |
| 157 | put_unaligned_be32(TPM_TAG_PERMANENT_FLAGS, |
| 158 | &pflags->tag); |
| 159 | *recv_len = TPM_HEADER_SIZE + 4 + |
| 160 | sizeof(*pflags); |
Simon Glass | ed3f5a3 | 2013-11-10 10:27:05 -0700 | [diff] [blame] | 161 | break; |
| 162 | } |
Simon Glass | ef8a250 | 2018-10-01 11:55:18 -0600 | [diff] [blame] | 163 | default: |
| 164 | printf(" ** Unknown flags index %x\n", index); |
| 165 | return -ENOSYS; |
| 166 | } |
Simon Glass | 7f350a9 | 2021-07-18 14:18:00 -0600 | [diff] [blame] | 167 | put_unaligned_be32(*recv_len, recvbuf + TPM_HDR_LEN); |
Simon Glass | ed3f5a3 | 2013-11-10 10:27:05 -0700 | [diff] [blame] | 168 | break; |
Simon Glass | 998af31 | 2018-10-01 11:55:17 -0600 | [diff] [blame] | 169 | case TPM_CAP_NV_INDEX: |
Simon Glass | ed3f5a3 | 2013-11-10 10:27:05 -0700 | [diff] [blame] | 170 | index = get_unaligned_be32(sendbuf + 18); |
| 171 | printf("Get cap nv index %#02x\n", index); |
Simon Glass | 7f350a9 | 2021-07-18 14:18:00 -0600 | [diff] [blame] | 172 | put_unaligned_be32(22, recvbuf + TPM_HDR_LEN); |
Simon Glass | ed3f5a3 | 2013-11-10 10:27:05 -0700 | [diff] [blame] | 173 | break; |
| 174 | default: |
| 175 | printf(" ** Unknown 0x65 command type %#02x\n", |
| 176 | type); |
Simon Glass | 998af31 | 2018-10-01 11:55:17 -0600 | [diff] [blame] | 177 | return -ENOSYS; |
Simon Glass | ed3f5a3 | 2013-11-10 10:27:05 -0700 | [diff] [blame] | 178 | } |
| 179 | break; |
Simon Glass | 998af31 | 2018-10-01 11:55:17 -0600 | [diff] [blame] | 180 | case TPM_CMD_NV_WRITE_VALUE: |
Simon Glass | ed3f5a3 | 2013-11-10 10:27:05 -0700 | [diff] [blame] | 181 | index = get_unaligned_be32(sendbuf + 10); |
| 182 | length = get_unaligned_be32(sendbuf + 18); |
Simon Glass | 974c98f | 2021-07-18 14:17:57 -0600 | [diff] [blame] | 183 | seq = sb_tpm_index_to_seq(index); |
Simon Glass | ed3f5a3 | 2013-11-10 10:27:05 -0700 | [diff] [blame] | 184 | if (seq < 0) |
Simon Glass | 998af31 | 2018-10-01 11:55:17 -0600 | [diff] [blame] | 185 | return -EINVAL; |
Simon Glass | ed3f5a3 | 2013-11-10 10:27:05 -0700 | [diff] [blame] | 186 | printf("tpm: nvwrite index=%#02x, len=%#02x\n", index, length); |
Simon Glass | 974c98f | 2021-07-18 14:17:57 -0600 | [diff] [blame] | 187 | sb_tpm_write_data(tpm->nvdata, seq, sendbuf, 22, length); |
Simon Glass | ed3f5a3 | 2013-11-10 10:27:05 -0700 | [diff] [blame] | 188 | break; |
Simon Glass | 998af31 | 2018-10-01 11:55:17 -0600 | [diff] [blame] | 189 | case TPM_CMD_NV_READ_VALUE: /* nvread */ |
Simon Glass | ed3f5a3 | 2013-11-10 10:27:05 -0700 | [diff] [blame] | 190 | index = get_unaligned_be32(sendbuf + 10); |
| 191 | length = get_unaligned_be32(sendbuf + 18); |
Simon Glass | 974c98f | 2021-07-18 14:17:57 -0600 | [diff] [blame] | 192 | seq = sb_tpm_index_to_seq(index); |
Simon Glass | ed3f5a3 | 2013-11-10 10:27:05 -0700 | [diff] [blame] | 193 | if (seq < 0) |
Simon Glass | 998af31 | 2018-10-01 11:55:17 -0600 | [diff] [blame] | 194 | return -EINVAL; |
Simon Glass | ef8a250 | 2018-10-01 11:55:18 -0600 | [diff] [blame] | 195 | printf("tpm: nvread index=%#02x, len=%#02x, seq=%#02x\n", index, |
| 196 | length, seq); |
Simon Glass | 974c98f | 2021-07-18 14:17:57 -0600 | [diff] [blame] | 197 | *recv_len = TPM_HDR_LEN + sizeof(uint32_t) + length; |
Simon Glass | ed3f5a3 | 2013-11-10 10:27:05 -0700 | [diff] [blame] | 198 | memset(recvbuf, '\0', *recv_len); |
Simon Glass | 974c98f | 2021-07-18 14:17:57 -0600 | [diff] [blame] | 199 | put_unaligned_be32(length, recvbuf + TPM_HDR_LEN); |
| 200 | sb_tpm_read_data(tpm->nvdata, seq, recvbuf, TPM_HDR_LEN + 4, |
| 201 | length); |
Simon Glass | ed3f5a3 | 2013-11-10 10:27:05 -0700 | [diff] [blame] | 202 | break; |
Simon Glass | ef8a250 | 2018-10-01 11:55:18 -0600 | [diff] [blame] | 203 | case TPM_CMD_EXTEND: |
| 204 | *recv_len = 30; |
| 205 | memset(recvbuf, '\0', *recv_len); |
| 206 | break; |
| 207 | case TPM_CMD_NV_DEFINE_SPACE: |
Simon Glass | f9143c1 | 2021-07-18 14:17:59 -0600 | [diff] [blame] | 208 | index = get_unaligned_be32(sendbuf + 12); |
| 209 | length = get_unaligned_be32(sendbuf + 77); |
| 210 | seq = sb_tpm_index_to_seq(index); |
| 211 | if (seq < 0) |
| 212 | return -EINVAL; |
| 213 | printf("tpm: define_space index=%#02x, len=%#02x, seq=%#02x\n", |
| 214 | index, length, seq); |
| 215 | sb_tpm_define_data(tpm->nvdata, seq, length); |
| 216 | *recv_len = 12; |
| 217 | memset(recvbuf, '\0', *recv_len); |
| 218 | break; |
Simon Glass | ed3f5a3 | 2013-11-10 10:27:05 -0700 | [diff] [blame] | 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 */ |
Simon Glass | ef8a250 | 2018-10-01 11:55:18 -0600 | [diff] [blame] | 224 | case 0x50: /* self test full */ |
Simon Glass | ed3f5a3 | 2013-11-10 10:27:05 -0700 | [diff] [blame] | 225 | case 0x4000000a: /* assert physical presence */ |
| 226 | *recv_len = 12; |
| 227 | memset(recvbuf, '\0', *recv_len); |
| 228 | break; |
| 229 | default: |
| 230 | printf("Unknown tpm command %02x\n", code); |
Simon Glass | 998af31 | 2018-10-01 11:55:17 -0600 | [diff] [blame] | 231 | return -ENOSYS; |
Simon Glass | ed3f5a3 | 2013-11-10 10:27:05 -0700 | [diff] [blame] | 232 | } |
Simon Glass | 04488c4 | 2018-11-06 15:21:23 -0700 | [diff] [blame] | 233 | #ifdef DEBUG |
| 234 | printf("tpm: rx recv_len %zd\n", *recv_len); |
| 235 | print_buffer(0, recvbuf, 1, *recv_len, 0); |
| 236 | #endif |
Simon Glass | ed3f5a3 | 2013-11-10 10:27:05 -0700 | [diff] [blame] | 237 | |
| 238 | return 0; |
| 239 | } |
| 240 | |
Simon Glass | 21baf15 | 2015-08-22 18:31:35 -0600 | [diff] [blame] | 241 | static int sandbox_tpm_get_desc(struct udevice *dev, char *buf, int size) |
Simon Glass | ed3f5a3 | 2013-11-10 10:27:05 -0700 | [diff] [blame] | 242 | { |
Simon Glass | 21baf15 | 2015-08-22 18:31:35 -0600 | [diff] [blame] | 243 | if (size < 15) |
| 244 | return -ENOSPC; |
| 245 | |
| 246 | return snprintf(buf, size, "sandbox TPM"); |
| 247 | } |
| 248 | |
| 249 | static int sandbox_tpm_probe(struct udevice *dev) |
| 250 | { |
| 251 | struct tpm_state *tpm = dev_get_priv(dev); |
| 252 | |
Simon Glass | 1db235a | 2021-07-18 14:17:58 -0600 | [diff] [blame] | 253 | if (s_state.valid) |
| 254 | memcpy(tpm, &s_state, sizeof(*tpm)); |
| 255 | g_state = tpm; |
Simon Glass | 21baf15 | 2015-08-22 18:31:35 -0600 | [diff] [blame] | 256 | |
Simon Glass | ed3f5a3 | 2013-11-10 10:27:05 -0700 | [diff] [blame] | 257 | return 0; |
| 258 | } |
| 259 | |
Simon Glass | 21baf15 | 2015-08-22 18:31:35 -0600 | [diff] [blame] | 260 | static int sandbox_tpm_open(struct udevice *dev) |
Simon Glass | ed3f5a3 | 2013-11-10 10:27:05 -0700 | [diff] [blame] | 261 | { |
Simon Glass | ed3f5a3 | 2013-11-10 10:27:05 -0700 | [diff] [blame] | 262 | return 0; |
| 263 | } |
| 264 | |
Simon Glass | 21baf15 | 2015-08-22 18:31:35 -0600 | [diff] [blame] | 265 | static int sandbox_tpm_close(struct udevice *dev) |
Simon Glass | ed3f5a3 | 2013-11-10 10:27:05 -0700 | [diff] [blame] | 266 | { |
Simon Glass | ed3f5a3 | 2013-11-10 10:27:05 -0700 | [diff] [blame] | 267 | return 0; |
| 268 | } |
Simon Glass | 21baf15 | 2015-08-22 18:31:35 -0600 | [diff] [blame] | 269 | |
| 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, |
| 275 | }; |
| 276 | |
| 277 | static const struct udevice_id sandbox_tpm_ids[] = { |
| 278 | { .compatible = "google,sandbox-tpm" }, |
| 279 | { } |
| 280 | }; |
| 281 | |
Walter Lozano | e3e2470 | 2020-06-25 01:10:04 -0300 | [diff] [blame] | 282 | U_BOOT_DRIVER(google_sandbox_tpm) = { |
| 283 | .name = "google_sandbox_tpm", |
Simon Glass | 21baf15 | 2015-08-22 18:31:35 -0600 | [diff] [blame] | 284 | .id = UCLASS_TPM, |
| 285 | .of_match = sandbox_tpm_ids, |
| 286 | .ops = &sandbox_tpm_ops, |
| 287 | .probe = sandbox_tpm_probe, |
Simon Glass | 41575d8 | 2020-12-03 16:55:17 -0700 | [diff] [blame] | 288 | .priv_auto = sizeof(struct tpm_state), |
Simon Glass | 21baf15 | 2015-08-22 18:31:35 -0600 | [diff] [blame] | 289 | }; |