Miquel Raynal | 4c6759e | 2018-05-15 11:57:06 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
| 2 | /* |
| 3 | * Copyright (c) 2013 The Chromium OS Authors. |
| 4 | */ |
| 5 | |
| 6 | #include <common.h> |
| 7 | #include <command.h> |
| 8 | #include <dm.h> |
Simon Glass | 07dc93c | 2019-08-01 09:46:47 -0600 | [diff] [blame] | 9 | #include <env.h> |
Miquel Raynal | 4c6759e | 2018-05-15 11:57:06 +0200 | [diff] [blame] | 10 | #include <asm/unaligned.h> |
| 11 | #include <linux/string.h> |
| 12 | #include <tpm-common.h> |
| 13 | #include "tpm-user-utils.h" |
| 14 | |
| 15 | /** |
| 16 | * Print a byte string in hexdecimal format, 16-bytes per line. |
| 17 | * |
| 18 | * @param data byte string to be printed |
| 19 | * @param count number of bytes to be printed |
| 20 | */ |
| 21 | void print_byte_string(u8 *data, size_t count) |
| 22 | { |
| 23 | int i, print_newline = 0; |
| 24 | |
| 25 | for (i = 0; i < count; i++) { |
| 26 | printf(" %02x", data[i]); |
| 27 | print_newline = (i % 16 == 15); |
| 28 | if (print_newline) |
| 29 | putc('\n'); |
| 30 | } |
| 31 | /* Avoid duplicated newline at the end */ |
| 32 | if (!print_newline) |
| 33 | putc('\n'); |
| 34 | } |
| 35 | |
| 36 | /** |
| 37 | * Convert a text string of hexdecimal values into a byte string. |
| 38 | * |
| 39 | * @param bytes text string of hexdecimal values with no space |
| 40 | * between them |
| 41 | * @param data output buffer for byte string. The caller has to make |
| 42 | * sure it is large enough for storing the output. If |
| 43 | * NULL is passed, a large enough buffer will be allocated, |
| 44 | * and the caller must free it. |
| 45 | * @param count_ptr output variable for the length of byte string |
| 46 | * @return pointer to output buffer |
| 47 | */ |
| 48 | void *parse_byte_string(char *bytes, u8 *data, size_t *count_ptr) |
| 49 | { |
| 50 | char byte[3]; |
| 51 | size_t count, length; |
| 52 | int i; |
| 53 | |
| 54 | if (!bytes) |
| 55 | return NULL; |
| 56 | length = strlen(bytes); |
| 57 | count = length / 2; |
| 58 | |
| 59 | if (!data) |
| 60 | data = malloc(count); |
| 61 | if (!data) |
| 62 | return NULL; |
| 63 | |
| 64 | byte[2] = '\0'; |
| 65 | for (i = 0; i < length; i += 2) { |
| 66 | byte[0] = bytes[i]; |
| 67 | byte[1] = bytes[i + 1]; |
| 68 | data[i / 2] = (u8)simple_strtoul(byte, NULL, 16); |
| 69 | } |
| 70 | |
| 71 | if (count_ptr) |
| 72 | *count_ptr = count; |
| 73 | |
| 74 | return data; |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * report_return_code() - Report any error and return failure or success |
| 79 | * |
| 80 | * @param return_code TPM command return code |
| 81 | * @return value of enum command_ret_t |
| 82 | */ |
| 83 | int report_return_code(int return_code) |
| 84 | { |
| 85 | if (return_code) { |
| 86 | printf("Error: %d\n", return_code); |
| 87 | return CMD_RET_FAILURE; |
| 88 | } else { |
| 89 | return CMD_RET_SUCCESS; |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | /** |
| 94 | * Return number of values defined by a type string. |
| 95 | * |
| 96 | * @param type_str type string |
| 97 | * @return number of values of type string |
| 98 | */ |
| 99 | int type_string_get_num_values(const char *type_str) |
| 100 | { |
| 101 | return strlen(type_str); |
| 102 | } |
| 103 | |
| 104 | /** |
| 105 | * Return total size of values defined by a type string. |
| 106 | * |
| 107 | * @param type_str type string |
| 108 | * @return total size of values of type string, or 0 if type string |
| 109 | * contains illegal type character. |
| 110 | */ |
| 111 | size_t type_string_get_space_size(const char *type_str) |
| 112 | { |
| 113 | size_t size; |
| 114 | |
| 115 | for (size = 0; *type_str; type_str++) { |
| 116 | switch (*type_str) { |
| 117 | case 'b': |
| 118 | size += 1; |
| 119 | break; |
| 120 | case 'w': |
| 121 | size += 2; |
| 122 | break; |
| 123 | case 'd': |
| 124 | size += 4; |
| 125 | break; |
| 126 | default: |
| 127 | return 0; |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | return size; |
| 132 | } |
| 133 | |
| 134 | /** |
| 135 | * Allocate a buffer large enough to hold values defined by a type |
| 136 | * string. The caller has to free the buffer. |
| 137 | * |
| 138 | * @param type_str type string |
| 139 | * @param count pointer for storing size of buffer |
| 140 | * @return pointer to buffer or NULL on error |
| 141 | */ |
| 142 | void *type_string_alloc(const char *type_str, u32 *count) |
| 143 | { |
| 144 | void *data; |
| 145 | size_t size; |
| 146 | |
| 147 | size = type_string_get_space_size(type_str); |
| 148 | if (!size) |
| 149 | return NULL; |
| 150 | data = malloc(size); |
| 151 | if (data) |
| 152 | *count = size; |
| 153 | |
| 154 | return data; |
| 155 | } |
| 156 | |
| 157 | /** |
| 158 | * Pack values defined by a type string into a buffer. The buffer must have |
| 159 | * large enough space. |
| 160 | * |
| 161 | * @param type_str type string |
| 162 | * @param values text strings of values to be packed |
| 163 | * @param data output buffer of values |
| 164 | * @return 0 on success, non-0 on error |
| 165 | */ |
| 166 | int type_string_pack(const char *type_str, char * const values[], |
| 167 | u8 *data) |
| 168 | { |
| 169 | size_t offset; |
| 170 | u32 value; |
| 171 | |
| 172 | for (offset = 0; *type_str; type_str++, values++) { |
| 173 | value = simple_strtoul(values[0], NULL, 0); |
| 174 | switch (*type_str) { |
| 175 | case 'b': |
| 176 | data[offset] = value; |
| 177 | offset += 1; |
| 178 | break; |
| 179 | case 'w': |
| 180 | put_unaligned_be16(value, data + offset); |
| 181 | offset += 2; |
| 182 | break; |
| 183 | case 'd': |
| 184 | put_unaligned_be32(value, data + offset); |
| 185 | offset += 4; |
| 186 | break; |
| 187 | default: |
| 188 | return -1; |
| 189 | } |
| 190 | } |
| 191 | |
| 192 | return 0; |
| 193 | } |
| 194 | |
| 195 | /** |
| 196 | * Read values defined by a type string from a buffer, and write these values |
| 197 | * to environment variables. |
| 198 | * |
| 199 | * @param type_str type string |
| 200 | * @param data input buffer of values |
| 201 | * @param vars names of environment variables |
| 202 | * @return 0 on success, non-0 on error |
| 203 | */ |
| 204 | int type_string_write_vars(const char *type_str, u8 *data, |
| 205 | char * const vars[]) |
| 206 | { |
| 207 | size_t offset; |
| 208 | u32 value; |
| 209 | |
| 210 | for (offset = 0; *type_str; type_str++, vars++) { |
| 211 | switch (*type_str) { |
| 212 | case 'b': |
| 213 | value = data[offset]; |
| 214 | offset += 1; |
| 215 | break; |
| 216 | case 'w': |
| 217 | value = get_unaligned_be16(data + offset); |
| 218 | offset += 2; |
| 219 | break; |
| 220 | case 'd': |
| 221 | value = get_unaligned_be32(data + offset); |
| 222 | offset += 4; |
| 223 | break; |
| 224 | default: |
| 225 | return -1; |
| 226 | } |
| 227 | if (env_set_ulong(*vars, value)) |
| 228 | return -1; |
| 229 | } |
| 230 | |
| 231 | return 0; |
| 232 | } |
| 233 | |
| 234 | int get_tpm(struct udevice **devp) |
| 235 | { |
| 236 | int rc; |
| 237 | |
| 238 | rc = uclass_first_device_err(UCLASS_TPM, devp); |
| 239 | if (rc) { |
| 240 | printf("Could not find TPM (ret=%d)\n", rc); |
| 241 | return CMD_RET_FAILURE; |
| 242 | } |
| 243 | |
| 244 | return 0; |
| 245 | } |
| 246 | |
| 247 | int do_tpm_info(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[]) |
| 248 | { |
| 249 | struct udevice *dev; |
| 250 | char buf[80]; |
| 251 | int rc; |
| 252 | |
| 253 | rc = get_tpm(&dev); |
| 254 | if (rc) |
| 255 | return rc; |
| 256 | rc = tpm_get_desc(dev, buf, sizeof(buf)); |
| 257 | if (rc < 0) { |
| 258 | printf("Couldn't get TPM info (%d)\n", rc); |
| 259 | return CMD_RET_FAILURE; |
| 260 | } |
| 261 | printf("%s\n", buf); |
| 262 | |
| 263 | return 0; |
| 264 | } |
| 265 | |
| 266 | int do_tpm_init(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) |
| 267 | { |
Simon Glass | 8ceca1d | 2018-11-18 14:22:27 -0700 | [diff] [blame] | 268 | struct udevice *dev; |
| 269 | int rc; |
| 270 | |
Miquel Raynal | 4c6759e | 2018-05-15 11:57:06 +0200 | [diff] [blame] | 271 | if (argc != 1) |
| 272 | return CMD_RET_USAGE; |
Simon Glass | 8ceca1d | 2018-11-18 14:22:27 -0700 | [diff] [blame] | 273 | rc = get_tpm(&dev); |
| 274 | if (rc) |
| 275 | return rc; |
Miquel Raynal | 4c6759e | 2018-05-15 11:57:06 +0200 | [diff] [blame] | 276 | |
Simon Glass | 8ceca1d | 2018-11-18 14:22:27 -0700 | [diff] [blame] | 277 | return report_return_code(tpm_init(dev)); |
Miquel Raynal | 4c6759e | 2018-05-15 11:57:06 +0200 | [diff] [blame] | 278 | } |
| 279 | |
| 280 | int do_tpm(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) |
| 281 | { |
| 282 | cmd_tbl_t *tpm_commands, *cmd; |
Miquel Raynal | 79920d1 | 2018-07-19 22:35:09 +0200 | [diff] [blame] | 283 | struct tpm_chip_priv *priv; |
| 284 | struct udevice *dev; |
Miquel Raynal | 4c6759e | 2018-05-15 11:57:06 +0200 | [diff] [blame] | 285 | unsigned int size; |
Miquel Raynal | 79920d1 | 2018-07-19 22:35:09 +0200 | [diff] [blame] | 286 | int ret; |
Miquel Raynal | 4c6759e | 2018-05-15 11:57:06 +0200 | [diff] [blame] | 287 | |
| 288 | if (argc < 2) |
| 289 | return CMD_RET_USAGE; |
| 290 | |
Miquel Raynal | 79920d1 | 2018-07-19 22:35:09 +0200 | [diff] [blame] | 291 | ret = get_tpm(&dev); |
| 292 | if (ret) |
| 293 | return ret; |
| 294 | |
| 295 | priv = dev_get_uclass_priv(dev); |
| 296 | |
| 297 | /* Below getters return NULL if the desired stack is not built */ |
| 298 | switch (priv->version) { |
| 299 | case TPM_V1: |
| 300 | tpm_commands = get_tpm1_commands(&size); |
| 301 | break; |
| 302 | case TPM_V2: |
| 303 | tpm_commands = get_tpm2_commands(&size); |
| 304 | break; |
| 305 | default: |
| 306 | tpm_commands = NULL; |
| 307 | } |
| 308 | |
| 309 | if (!tpm_commands) |
| 310 | return CMD_RET_USAGE; |
Miquel Raynal | 4c6759e | 2018-05-15 11:57:06 +0200 | [diff] [blame] | 311 | |
| 312 | cmd = find_cmd_tbl(argv[1], tpm_commands, size); |
| 313 | if (!cmd) |
| 314 | return CMD_RET_USAGE; |
| 315 | |
| 316 | return cmd->cmd(cmdtp, flag, argc - 1, argv + 1); |
| 317 | } |