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"
16 * Print a byte string in hexdecimal format, 16-bytes per line.
18 * @param data byte string to be printed
19 * @param count number of bytes to be printed
21 void print_byte_string(u8 *data, size_t count)
23 int i, print_newline = 0;
25 for (i = 0; i < count; i++) {
26 printf(" %02x", data[i]);
27 print_newline = (i % 16 == 15);
31 /* Avoid duplicated newline at the end */
37 * Convert a text string of hexdecimal values into a byte string.
39 * @param bytes text string of hexdecimal values with no space
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
48 void *parse_byte_string(char *bytes, u8 *data, size_t *count_ptr)
56 length = strlen(bytes);
65 for (i = 0; i < length; i += 2) {
67 byte[1] = bytes[i + 1];
68 data[i / 2] = (u8)simple_strtoul(byte, NULL, 16);
78 * report_return_code() - Report any error and return failure or success
80 * @param return_code TPM command return code
81 * @return value of enum command_ret_t
83 int report_return_code(int return_code)
86 printf("Error: %d\n", return_code);
87 return CMD_RET_FAILURE;
89 return CMD_RET_SUCCESS;
94 * Return number of values defined by a type string.
96 * @param type_str type string
97 * @return number of values of type string
99 int type_string_get_num_values(const char *type_str)
101 return strlen(type_str);
105 * Return total size of values defined by a type string.
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.
111 size_t type_string_get_space_size(const char *type_str)
115 for (size = 0; *type_str; type_str++) {
135 * Allocate a buffer large enough to hold values defined by a type
136 * string. The caller has to free the buffer.
138 * @param type_str type string
139 * @param count pointer for storing size of buffer
140 * @return pointer to buffer or NULL on error
142 void *type_string_alloc(const char *type_str, u32 *count)
147 size = type_string_get_space_size(type_str);
158 * Pack values defined by a type string into a buffer. The buffer must have
159 * large enough space.
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
166 int type_string_pack(const char *type_str, char * const values[],
172 for (offset = 0; *type_str; type_str++, values++) {
173 value = simple_strtoul(values[0], NULL, 0);
176 data[offset] = value;
180 put_unaligned_be16(value, data + offset);
184 put_unaligned_be32(value, data + offset);
196 * Read values defined by a type string from a buffer, and write these values
197 * to environment variables.
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
204 int type_string_write_vars(const char *type_str, u8 *data,
210 for (offset = 0; *type_str; type_str++, vars++) {
213 value = data[offset];
217 value = get_unaligned_be16(data + offset);
221 value = get_unaligned_be32(data + offset);
227 if (env_set_ulong(*vars, value))
234 int get_tpm(struct udevice **devp)
238 rc = uclass_first_device_err(UCLASS_TPM, devp);
240 printf("Could not find TPM (ret=%d)\n", rc);
241 return CMD_RET_FAILURE;
247 int do_tpm_info(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[])
256 rc = tpm_get_desc(dev, buf, sizeof(buf));
258 printf("Couldn't get TPM info (%d)\n", rc);
259 return CMD_RET_FAILURE;
266 int do_tpm_init(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
272 return CMD_RET_USAGE;
277 return report_return_code(tpm_init(dev));
280 int do_tpm(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
282 cmd_tbl_t *tpm_commands, *cmd;
283 struct tpm_chip_priv *priv;
289 return CMD_RET_USAGE;
295 priv = dev_get_uclass_priv(dev);
297 /* Below getters return NULL if the desired stack is not built */
298 switch (priv->version) {
300 tpm_commands = get_tpm1_commands(&size);
303 tpm_commands = get_tpm2_commands(&size);
310 return CMD_RET_USAGE;
312 cmd = find_cmd_tbl(argv[1], tpm_commands, size);
314 return CMD_RET_USAGE;
316 return cmd->cmd(cmdtp, flag, argc - 1, argv + 1);