1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (c) 2013 The Chromium OS Authors.
10 #include <asm/unaligned.h>
11 #include <linux/string.h>
12 #include <tpm-common.h>
13 #include "tpm-user-utils.h"
15 static struct udevice *tpm_dev;
18 * Print a byte string in hexdecimal format, 16-bytes per line.
20 * @param data byte string to be printed
21 * @param count number of bytes to be printed
23 void print_byte_string(u8 *data, size_t count)
25 int i, print_newline = 0;
27 for (i = 0; i < count; i++) {
28 printf(" %02x", data[i]);
29 print_newline = (i % 16 == 15);
33 /* Avoid duplicated newline at the end */
39 * Convert a text string of hexdecimal values into a byte string.
41 * @param bytes text string of hexdecimal values with no space
43 * @param data output buffer for byte string. The caller has to make
44 * sure it is large enough for storing the output. If
45 * NULL is passed, a large enough buffer will be allocated,
46 * and the caller must free it.
47 * @param count_ptr output variable for the length of byte string
48 * @return pointer to output buffer
50 void *parse_byte_string(char *bytes, u8 *data, size_t *count_ptr)
58 length = strlen(bytes);
67 for (i = 0; i < length; i += 2) {
69 byte[1] = bytes[i + 1];
70 data[i / 2] = (u8)simple_strtoul(byte, NULL, 16);
80 * report_return_code() - Report any error and return failure or success
82 * @param return_code TPM command return code
83 * @return value of enum command_ret_t
85 int report_return_code(int return_code)
88 printf("Error: %d\n", return_code);
89 return CMD_RET_FAILURE;
91 return CMD_RET_SUCCESS;
96 * Return number of values defined by a type string.
98 * @param type_str type string
99 * @return number of values of type string
101 int type_string_get_num_values(const char *type_str)
103 return strlen(type_str);
107 * Return total size of values defined by a type string.
109 * @param type_str type string
110 * @return total size of values of type string, or 0 if type string
111 * contains illegal type character.
113 size_t type_string_get_space_size(const char *type_str)
117 for (size = 0; *type_str; type_str++) {
137 * Allocate a buffer large enough to hold values defined by a type
138 * string. The caller has to free the buffer.
140 * @param type_str type string
141 * @param count pointer for storing size of buffer
142 * @return pointer to buffer or NULL on error
144 void *type_string_alloc(const char *type_str, u32 *count)
149 size = type_string_get_space_size(type_str);
160 * Pack values defined by a type string into a buffer. The buffer must have
161 * large enough space.
163 * @param type_str type string
164 * @param values text strings of values to be packed
165 * @param data output buffer of values
166 * @return 0 on success, non-0 on error
168 int type_string_pack(const char *type_str, char * const values[],
174 for (offset = 0; *type_str; type_str++, values++) {
175 value = simple_strtoul(values[0], NULL, 0);
178 data[offset] = value;
182 put_unaligned_be16(value, data + offset);
186 put_unaligned_be32(value, data + offset);
198 * Read values defined by a type string from a buffer, and write these values
199 * to environment variables.
201 * @param type_str type string
202 * @param data input buffer of values
203 * @param vars names of environment variables
204 * @return 0 on success, non-0 on error
206 int type_string_write_vars(const char *type_str, u8 *data,
212 for (offset = 0; *type_str; type_str++, vars++) {
215 value = data[offset];
219 value = get_unaligned_be16(data + offset);
223 value = get_unaligned_be32(data + offset);
229 if (env_set_ulong(*vars, value))
236 static int tpm_show_device(void)
242 for_each_tpm_device(dev) {
243 rc = tpm_get_desc(dev, buf, sizeof(buf));
245 printf("device %d: can't get info\n", n);
247 printf("device %d: %s\n", n, buf);
255 static int tpm_set_device(unsigned long num)
259 int rc = CMD_RET_FAILURE;
261 for_each_tpm_device(dev) {
276 int get_tpm(struct udevice **devp)
281 * To keep a backward compatibility with previous code,
282 * if a tpm device is not explicitly set, we set the first one.
285 rc = tpm_set_device(0);
287 printf("Couldn't set TPM 0 (rc = %d)\n", rc);
288 return CMD_RET_FAILURE;
298 int do_tpm_device(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[])
304 num = simple_strtoul(argv[1], NULL, 10);
306 rc = tpm_set_device(num);
308 printf("Couldn't set TPM %lu (rc = %d)\n", num, rc);
310 rc = tpm_show_device();
316 int do_tpm_info(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[])
325 rc = tpm_get_desc(dev, buf, sizeof(buf));
327 printf("Couldn't get TPM info (%d)\n", rc);
328 return CMD_RET_FAILURE;
335 int do_tpm_init(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
341 return CMD_RET_USAGE;
346 return report_return_code(tpm_init(dev));
349 int do_tpm(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
351 cmd_tbl_t *tpm_commands, *cmd;
352 struct tpm_chip_priv *priv;
358 return CMD_RET_USAGE;
364 priv = dev_get_uclass_priv(dev);
366 /* Below getters return NULL if the desired stack is not built */
367 switch (priv->version) {
369 tpm_commands = get_tpm1_commands(&size);
372 tpm_commands = get_tpm2_commands(&size);
379 return CMD_RET_USAGE;
381 cmd = find_cmd_tbl(argv[1], tpm_commands, size);
383 return CMD_RET_USAGE;
385 return cmd->cmd(cmdtp, flag, argc - 1, argv + 1);