Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Simon Glass | ba93f4e | 2013-11-10 10:27:05 -0700 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (c) 2013 Google, Inc |
Simon Glass | ba93f4e | 2013-11-10 10:27:05 -0700 | [diff] [blame] | 4 | */ |
| 5 | |
Simon Glass | 1ab1692 | 2022-07-31 12:28:48 -0600 | [diff] [blame] | 6 | #include <display_options.h> |
Simon Glass | a425f76 | 2015-08-22 18:31:35 -0600 | [diff] [blame] | 7 | #include <dm.h> |
Miquel Raynal | 4c6759e | 2018-05-15 11:57:06 +0200 | [diff] [blame] | 8 | #include <tpm-v1.h> |
Simon Glass | ba93f4e | 2013-11-10 10:27:05 -0700 | [diff] [blame] | 9 | #include <asm/state.h> |
| 10 | #include <asm/unaligned.h> |
Simon Glass | ca9b0af | 2019-11-14 12:57:14 -0700 | [diff] [blame] | 11 | #include <u-boot/crc.h> |
Simon Glass | df884ec | 2021-07-18 14:17:57 -0600 | [diff] [blame] | 12 | #include "sandbox_common.h" |
Simon Glass | ba93f4e | 2013-11-10 10:27:05 -0700 | [diff] [blame] | 13 | |
| 14 | #define NV_DATA_PUBLIC_PERMISSIONS_OFFSET 60 |
| 15 | |
Simon Glass | ba93f4e | 2013-11-10 10:27:05 -0700 | [diff] [blame] | 16 | /* |
| 17 | * Information about our TPM emulation. This is preserved in the sandbox |
| 18 | * state file if enabled. |
| 19 | */ |
| 20 | static struct tpm_state { |
Simon Glass | f99c329 | 2018-10-01 11:55:18 -0600 | [diff] [blame] | 21 | bool valid; |
| 22 | struct nvdata_state nvdata[NV_SEQ_COUNT]; |
Simon Glass | 689cde0 | 2021-07-18 14:17:58 -0600 | [diff] [blame] | 23 | } s_state, *g_state; |
Simon Glass | ba93f4e | 2013-11-10 10:27:05 -0700 | [diff] [blame] | 24 | |
| 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 | */ |
| 34 | static int sandbox_tpm_read_state(const void *blob, int node) |
| 35 | { |
Simon Glass | 689cde0 | 2021-07-18 14:17:58 -0600 | [diff] [blame] | 36 | struct tpm_state *state = &s_state; |
Simon Glass | ba93f4e | 2013-11-10 10:27:05 -0700 | [diff] [blame] | 37 | 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 Glass | 689cde0 | 2021-07-18 14:17:58 -0600 | [diff] [blame] | 45 | struct nvdata_state *nvd = &state->nvdata[i]; |
Simon Glass | ba93f4e | 2013-11-10 10:27:05 -0700 | [diff] [blame] | 46 | char prop_name[20]; |
| 47 | |
| 48 | sprintf(prop_name, "nvdata%d", i); |
| 49 | prop = fdt_getprop(blob, node, prop_name, &len); |
Simon Glass | 689cde0 | 2021-07-18 14:17:58 -0600 | [diff] [blame] | 50 | 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 Glass | f99c329 | 2018-10-01 11:55:18 -0600 | [diff] [blame] | 56 | } |
Simon Glass | ba93f4e | 2013-11-10 10:27:05 -0700 | [diff] [blame] | 57 | } |
Simon Glass | 689cde0 | 2021-07-18 14:17:58 -0600 | [diff] [blame] | 58 | |
| 59 | s_state.valid = true; |
Simon Glass | ba93f4e | 2013-11-10 10:27:05 -0700 | [diff] [blame] | 60 | |
| 61 | return 0; |
| 62 | } |
| 63 | |
| 64 | /** |
Simon Glass | 689cde0 | 2021-07-18 14:17:58 -0600 | [diff] [blame] | 65 | * sandbox_tpm_write_state() - Write out our state to the state file |
Simon Glass | ba93f4e | 2013-11-10 10:27:05 -0700 | [diff] [blame] | 66 | * |
| 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 | */ |
| 73 | static int sandbox_tpm_write_state(void *blob, int node) |
| 74 | { |
Simon Glass | 689cde0 | 2021-07-18 14:17:58 -0600 | [diff] [blame] | 75 | const struct tpm_state *state = g_state; |
Simon Glass | ba93f4e | 2013-11-10 10:27:05 -0700 | [diff] [blame] | 76 | int i; |
| 77 | |
Simon Glass | 689cde0 | 2021-07-18 14:17:58 -0600 | [diff] [blame] | 78 | if (!state) |
| 79 | return 0; |
| 80 | |
Simon Glass | ba93f4e | 2013-11-10 10:27:05 -0700 | [diff] [blame] | 81 | /* |
| 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 Glass | 689cde0 | 2021-07-18 14:17:58 -0600 | [diff] [blame] | 87 | const struct nvdata_state *nvd = &state->nvdata[i]; |
Simon Glass | ba93f4e | 2013-11-10 10:27:05 -0700 | [diff] [blame] | 88 | char prop_name[20]; |
| 89 | |
Simon Glass | 689cde0 | 2021-07-18 14:17:58 -0600 | [diff] [blame] | 90 | 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 Glass | f99c329 | 2018-10-01 11:55:18 -0600 | [diff] [blame] | 94 | } |
Simon Glass | ba93f4e | 2013-11-10 10:27:05 -0700 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | return 0; |
| 98 | } |
| 99 | |
| 100 | SANDBOX_STATE_IO(sandbox_tpm, "google,sandbox-tpm", sandbox_tpm_read_state, |
| 101 | sandbox_tpm_write_state); |
| 102 | |
Simon Glass | f99c329 | 2018-10-01 11:55:18 -0600 | [diff] [blame] | 103 | static 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 Glass | a425f76 | 2015-08-22 18:31:35 -0600 | [diff] [blame] | 118 | static 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 Glass | ba93f4e | 2013-11-10 10:27:05 -0700 | [diff] [blame] | 121 | { |
Simon Glass | a425f76 | 2015-08-22 18:31:35 -0600 | [diff] [blame] | 122 | struct tpm_state *tpm = dev_get_priv(dev); |
Simon Glass | ba93f4e | 2013-11-10 10:27:05 -0700 | [diff] [blame] | 123 | 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 Glass | 2b45c48 | 2018-11-06 15:21:23 -0700 | [diff] [blame] | 129 | #ifdef DEBUG |
Simon Glass | ba93f4e | 2013-11-10 10:27:05 -0700 | [diff] [blame] | 130 | 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 Glass | 2b45c48 | 2018-11-06 15:21:23 -0700 | [diff] [blame] | 133 | #endif |
Simon Glass | ba93f4e | 2013-11-10 10:27:05 -0700 | [diff] [blame] | 134 | switch (code) { |
Simon Glass | 5ff3f16 | 2018-10-01 11:55:17 -0600 | [diff] [blame] | 135 | case TPM_CMD_GET_CAPABILITY: |
Simon Glass | ba93f4e | 2013-11-10 10:27:05 -0700 | [diff] [blame] | 136 | type = get_unaligned_be32(sendbuf + 14); |
| 137 | switch (type) { |
Simon Glass | 5ff3f16 | 2018-10-01 11:55:17 -0600 | [diff] [blame] | 138 | case TPM_CAP_FLAG: |
Simon Glass | ba93f4e | 2013-11-10 10:27:05 -0700 | [diff] [blame] | 139 | 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 Glass | 51c5066 | 2021-07-18 14:18:00 -0600 | [diff] [blame] | 143 | data = recvbuf + TPM_HDR_LEN + sizeof(uint32_t); |
Simon Glass | ba93f4e | 2013-11-10 10:27:05 -0700 | [diff] [blame] | 144 | switch (index) { |
| 145 | case FIRMWARE_NV_INDEX: |
| 146 | break; |
| 147 | case KERNEL_NV_INDEX: |
Simon Glass | f99c329 | 2018-10-01 11:55:18 -0600 | [diff] [blame] | 148 | handle_cap_flag_space(&data, index); |
Simon Glass | 51c5066 | 2021-07-18 14:18:00 -0600 | [diff] [blame] | 149 | *recv_len = data - recvbuf; |
Simon Glass | f99c329 | 2018-10-01 11:55:18 -0600 | [diff] [blame] | 150 | break; |
| 151 | case TPM_CAP_FLAG_PERMANENT: { |
| 152 | struct tpm_permanent_flags *pflags; |
| 153 | |
| 154 | pflags = (struct tpm_permanent_flags *)data; |
| 155 | memset(pflags, '\0', sizeof(*pflags)); |
| 156 | put_unaligned_be32(TPM_TAG_PERMANENT_FLAGS, |
| 157 | &pflags->tag); |
| 158 | *recv_len = TPM_HEADER_SIZE + 4 + |
| 159 | sizeof(*pflags); |
Simon Glass | ba93f4e | 2013-11-10 10:27:05 -0700 | [diff] [blame] | 160 | break; |
| 161 | } |
Simon Glass | f99c329 | 2018-10-01 11:55:18 -0600 | [diff] [blame] | 162 | default: |
| 163 | printf(" ** Unknown flags index %x\n", index); |
| 164 | return -ENOSYS; |
| 165 | } |
Simon Glass | 51c5066 | 2021-07-18 14:18:00 -0600 | [diff] [blame] | 166 | put_unaligned_be32(*recv_len, recvbuf + TPM_HDR_LEN); |
Simon Glass | ba93f4e | 2013-11-10 10:27:05 -0700 | [diff] [blame] | 167 | break; |
Simon Glass | 5ff3f16 | 2018-10-01 11:55:17 -0600 | [diff] [blame] | 168 | case TPM_CAP_NV_INDEX: |
Simon Glass | ba93f4e | 2013-11-10 10:27:05 -0700 | [diff] [blame] | 169 | index = get_unaligned_be32(sendbuf + 18); |
| 170 | printf("Get cap nv index %#02x\n", index); |
Simon Glass | 51c5066 | 2021-07-18 14:18:00 -0600 | [diff] [blame] | 171 | put_unaligned_be32(22, recvbuf + TPM_HDR_LEN); |
Simon Glass | ba93f4e | 2013-11-10 10:27:05 -0700 | [diff] [blame] | 172 | break; |
| 173 | default: |
| 174 | printf(" ** Unknown 0x65 command type %#02x\n", |
| 175 | type); |
Simon Glass | 5ff3f16 | 2018-10-01 11:55:17 -0600 | [diff] [blame] | 176 | return -ENOSYS; |
Simon Glass | ba93f4e | 2013-11-10 10:27:05 -0700 | [diff] [blame] | 177 | } |
| 178 | break; |
Simon Glass | 5ff3f16 | 2018-10-01 11:55:17 -0600 | [diff] [blame] | 179 | case TPM_CMD_NV_WRITE_VALUE: |
Simon Glass | ba93f4e | 2013-11-10 10:27:05 -0700 | [diff] [blame] | 180 | index = get_unaligned_be32(sendbuf + 10); |
| 181 | length = get_unaligned_be32(sendbuf + 18); |
Simon Glass | df884ec | 2021-07-18 14:17:57 -0600 | [diff] [blame] | 182 | seq = sb_tpm_index_to_seq(index); |
Simon Glass | ba93f4e | 2013-11-10 10:27:05 -0700 | [diff] [blame] | 183 | if (seq < 0) |
Simon Glass | 5ff3f16 | 2018-10-01 11:55:17 -0600 | [diff] [blame] | 184 | return -EINVAL; |
Simon Glass | ba93f4e | 2013-11-10 10:27:05 -0700 | [diff] [blame] | 185 | printf("tpm: nvwrite index=%#02x, len=%#02x\n", index, length); |
Simon Glass | df884ec | 2021-07-18 14:17:57 -0600 | [diff] [blame] | 186 | sb_tpm_write_data(tpm->nvdata, seq, sendbuf, 22, length); |
Simon Glass | ba93f4e | 2013-11-10 10:27:05 -0700 | [diff] [blame] | 187 | break; |
Simon Glass | 5ff3f16 | 2018-10-01 11:55:17 -0600 | [diff] [blame] | 188 | case TPM_CMD_NV_READ_VALUE: /* nvread */ |
Simon Glass | ba93f4e | 2013-11-10 10:27:05 -0700 | [diff] [blame] | 189 | index = get_unaligned_be32(sendbuf + 10); |
| 190 | length = get_unaligned_be32(sendbuf + 18); |
Simon Glass | df884ec | 2021-07-18 14:17:57 -0600 | [diff] [blame] | 191 | seq = sb_tpm_index_to_seq(index); |
Simon Glass | ba93f4e | 2013-11-10 10:27:05 -0700 | [diff] [blame] | 192 | if (seq < 0) |
Simon Glass | 5ff3f16 | 2018-10-01 11:55:17 -0600 | [diff] [blame] | 193 | return -EINVAL; |
Simon Glass | f99c329 | 2018-10-01 11:55:18 -0600 | [diff] [blame] | 194 | printf("tpm: nvread index=%#02x, len=%#02x, seq=%#02x\n", index, |
| 195 | length, seq); |
Simon Glass | df884ec | 2021-07-18 14:17:57 -0600 | [diff] [blame] | 196 | *recv_len = TPM_HDR_LEN + sizeof(uint32_t) + length; |
Simon Glass | ba93f4e | 2013-11-10 10:27:05 -0700 | [diff] [blame] | 197 | memset(recvbuf, '\0', *recv_len); |
Simon Glass | df884ec | 2021-07-18 14:17:57 -0600 | [diff] [blame] | 198 | put_unaligned_be32(length, recvbuf + TPM_HDR_LEN); |
| 199 | sb_tpm_read_data(tpm->nvdata, seq, recvbuf, TPM_HDR_LEN + 4, |
| 200 | length); |
Simon Glass | ba93f4e | 2013-11-10 10:27:05 -0700 | [diff] [blame] | 201 | break; |
Simon Glass | f99c329 | 2018-10-01 11:55:18 -0600 | [diff] [blame] | 202 | case TPM_CMD_EXTEND: |
| 203 | *recv_len = 30; |
| 204 | memset(recvbuf, '\0', *recv_len); |
| 205 | break; |
| 206 | case TPM_CMD_NV_DEFINE_SPACE: |
Simon Glass | f700496 | 2021-07-18 14:17:59 -0600 | [diff] [blame] | 207 | index = get_unaligned_be32(sendbuf + 12); |
| 208 | length = get_unaligned_be32(sendbuf + 77); |
| 209 | seq = sb_tpm_index_to_seq(index); |
| 210 | if (seq < 0) |
| 211 | return -EINVAL; |
| 212 | printf("tpm: define_space index=%#02x, len=%#02x, seq=%#02x\n", |
| 213 | index, length, seq); |
| 214 | sb_tpm_define_data(tpm->nvdata, seq, length); |
| 215 | *recv_len = 12; |
| 216 | memset(recvbuf, '\0', *recv_len); |
| 217 | break; |
Simon Glass | ba93f4e | 2013-11-10 10:27:05 -0700 | [diff] [blame] | 218 | case 0x15: /* pcr read */ |
| 219 | case 0x5d: /* force clear */ |
| 220 | case 0x6f: /* physical enable */ |
| 221 | case 0x72: /* physical set deactivated */ |
| 222 | case 0x99: /* startup */ |
Simon Glass | f99c329 | 2018-10-01 11:55:18 -0600 | [diff] [blame] | 223 | case 0x50: /* self test full */ |
Simon Glass | ba93f4e | 2013-11-10 10:27:05 -0700 | [diff] [blame] | 224 | case 0x4000000a: /* assert physical presence */ |
| 225 | *recv_len = 12; |
| 226 | memset(recvbuf, '\0', *recv_len); |
| 227 | break; |
| 228 | default: |
| 229 | printf("Unknown tpm command %02x\n", code); |
Simon Glass | 5ff3f16 | 2018-10-01 11:55:17 -0600 | [diff] [blame] | 230 | return -ENOSYS; |
Simon Glass | ba93f4e | 2013-11-10 10:27:05 -0700 | [diff] [blame] | 231 | } |
Simon Glass | 2b45c48 | 2018-11-06 15:21:23 -0700 | [diff] [blame] | 232 | #ifdef DEBUG |
| 233 | printf("tpm: rx recv_len %zd\n", *recv_len); |
| 234 | print_buffer(0, recvbuf, 1, *recv_len, 0); |
| 235 | #endif |
Simon Glass | ba93f4e | 2013-11-10 10:27:05 -0700 | [diff] [blame] | 236 | |
| 237 | return 0; |
| 238 | } |
| 239 | |
Simon Glass | a425f76 | 2015-08-22 18:31:35 -0600 | [diff] [blame] | 240 | static int sandbox_tpm_get_desc(struct udevice *dev, char *buf, int size) |
Simon Glass | ba93f4e | 2013-11-10 10:27:05 -0700 | [diff] [blame] | 241 | { |
Simon Glass | a425f76 | 2015-08-22 18:31:35 -0600 | [diff] [blame] | 242 | if (size < 15) |
| 243 | return -ENOSPC; |
| 244 | |
| 245 | return snprintf(buf, size, "sandbox TPM"); |
| 246 | } |
| 247 | |
| 248 | static int sandbox_tpm_probe(struct udevice *dev) |
| 249 | { |
| 250 | struct tpm_state *tpm = dev_get_priv(dev); |
| 251 | |
Simon Glass | 689cde0 | 2021-07-18 14:17:58 -0600 | [diff] [blame] | 252 | if (s_state.valid) |
| 253 | memcpy(tpm, &s_state, sizeof(*tpm)); |
| 254 | g_state = tpm; |
Simon Glass | a425f76 | 2015-08-22 18:31:35 -0600 | [diff] [blame] | 255 | |
Simon Glass | ba93f4e | 2013-11-10 10:27:05 -0700 | [diff] [blame] | 256 | return 0; |
| 257 | } |
| 258 | |
Simon Glass | a425f76 | 2015-08-22 18:31:35 -0600 | [diff] [blame] | 259 | static int sandbox_tpm_open(struct udevice *dev) |
Simon Glass | ba93f4e | 2013-11-10 10:27:05 -0700 | [diff] [blame] | 260 | { |
Simon Glass | ba93f4e | 2013-11-10 10:27:05 -0700 | [diff] [blame] | 261 | return 0; |
| 262 | } |
| 263 | |
Simon Glass | a425f76 | 2015-08-22 18:31:35 -0600 | [diff] [blame] | 264 | static int sandbox_tpm_close(struct udevice *dev) |
Simon Glass | ba93f4e | 2013-11-10 10:27:05 -0700 | [diff] [blame] | 265 | { |
Simon Glass | ba93f4e | 2013-11-10 10:27:05 -0700 | [diff] [blame] | 266 | return 0; |
| 267 | } |
Simon Glass | a425f76 | 2015-08-22 18:31:35 -0600 | [diff] [blame] | 268 | |
| 269 | static const struct tpm_ops sandbox_tpm_ops = { |
| 270 | .open = sandbox_tpm_open, |
| 271 | .close = sandbox_tpm_close, |
| 272 | .get_desc = sandbox_tpm_get_desc, |
| 273 | .xfer = sandbox_tpm_xfer, |
| 274 | }; |
| 275 | |
| 276 | static const struct udevice_id sandbox_tpm_ids[] = { |
| 277 | { .compatible = "google,sandbox-tpm" }, |
| 278 | { } |
| 279 | }; |
| 280 | |
Walter Lozano | 2901ac6 | 2020-06-25 01:10:04 -0300 | [diff] [blame] | 281 | U_BOOT_DRIVER(google_sandbox_tpm) = { |
| 282 | .name = "google_sandbox_tpm", |
Simon Glass | a425f76 | 2015-08-22 18:31:35 -0600 | [diff] [blame] | 283 | .id = UCLASS_TPM, |
| 284 | .of_match = sandbox_tpm_ids, |
| 285 | .ops = &sandbox_tpm_ops, |
| 286 | .probe = sandbox_tpm_probe, |
Simon Glass | 8a2b47f | 2020-12-03 16:55:17 -0700 | [diff] [blame] | 287 | .priv_auto = sizeof(struct tpm_state), |
Simon Glass | a425f76 | 2015-08-22 18:31:35 -0600 | [diff] [blame] | 288 | }; |