1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (c) 2013 The Chromium OS Authors.
9 #include <asm/unaligned.h>
10 #include <linux/string.h>
11 #include <tpm-common.h>
12 #include "tpm-user-utils.h"
15 * Print a byte string in hexdecimal format, 16-bytes per line.
17 * @param data byte string to be printed
18 * @param count number of bytes to be printed
20 void print_byte_string(u8 *data, size_t count)
22 int i, print_newline = 0;
24 for (i = 0; i < count; i++) {
25 printf(" %02x", data[i]);
26 print_newline = (i % 16 == 15);
30 /* Avoid duplicated newline at the end */
36 * Convert a text string of hexdecimal values into a byte string.
38 * @param bytes text string of hexdecimal values with no space
40 * @param data output buffer for byte string. The caller has to make
41 * sure it is large enough for storing the output. If
42 * NULL is passed, a large enough buffer will be allocated,
43 * and the caller must free it.
44 * @param count_ptr output variable for the length of byte string
45 * @return pointer to output buffer
47 void *parse_byte_string(char *bytes, u8 *data, size_t *count_ptr)
55 length = strlen(bytes);
64 for (i = 0; i < length; i += 2) {
66 byte[1] = bytes[i + 1];
67 data[i / 2] = (u8)simple_strtoul(byte, NULL, 16);
77 * report_return_code() - Report any error and return failure or success
79 * @param return_code TPM command return code
80 * @return value of enum command_ret_t
82 int report_return_code(int return_code)
85 printf("Error: %d\n", return_code);
86 return CMD_RET_FAILURE;
88 return CMD_RET_SUCCESS;
93 * Return number of values defined by a type string.
95 * @param type_str type string
96 * @return number of values of type string
98 int type_string_get_num_values(const char *type_str)
100 return strlen(type_str);
104 * Return total size of values defined by a type string.
106 * @param type_str type string
107 * @return total size of values of type string, or 0 if type string
108 * contains illegal type character.
110 size_t type_string_get_space_size(const char *type_str)
114 for (size = 0; *type_str; type_str++) {
134 * Allocate a buffer large enough to hold values defined by a type
135 * string. The caller has to free the buffer.
137 * @param type_str type string
138 * @param count pointer for storing size of buffer
139 * @return pointer to buffer or NULL on error
141 void *type_string_alloc(const char *type_str, u32 *count)
146 size = type_string_get_space_size(type_str);
157 * Pack values defined by a type string into a buffer. The buffer must have
158 * large enough space.
160 * @param type_str type string
161 * @param values text strings of values to be packed
162 * @param data output buffer of values
163 * @return 0 on success, non-0 on error
165 int type_string_pack(const char *type_str, char * const values[],
171 for (offset = 0; *type_str; type_str++, values++) {
172 value = simple_strtoul(values[0], NULL, 0);
175 data[offset] = value;
179 put_unaligned_be16(value, data + offset);
183 put_unaligned_be32(value, data + offset);
195 * Read values defined by a type string from a buffer, and write these values
196 * to environment variables.
198 * @param type_str type string
199 * @param data input buffer of values
200 * @param vars names of environment variables
201 * @return 0 on success, non-0 on error
203 int type_string_write_vars(const char *type_str, u8 *data,
209 for (offset = 0; *type_str; type_str++, vars++) {
212 value = data[offset];
216 value = get_unaligned_be16(data + offset);
220 value = get_unaligned_be32(data + offset);
226 if (env_set_ulong(*vars, value))
233 int get_tpm(struct udevice **devp)
237 rc = uclass_first_device_err(UCLASS_TPM, devp);
239 printf("Could not find TPM (ret=%d)\n", rc);
240 return CMD_RET_FAILURE;
246 int do_tpm_info(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[])
255 rc = tpm_get_desc(dev, buf, sizeof(buf));
257 printf("Couldn't get TPM info (%d)\n", rc);
258 return CMD_RET_FAILURE;
265 int do_tpm_init(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
268 return CMD_RET_USAGE;
270 return report_return_code(tpm_init());
273 int do_tpm(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
275 cmd_tbl_t *tpm_commands, *cmd;
279 return CMD_RET_USAGE;
281 tpm_commands = get_tpm_commands(&size);
283 cmd = find_cmd_tbl(argv[1], tpm_commands, size);
285 return CMD_RET_USAGE;
287 return cmd->cmd(cmdtp, flag, argc - 1, argv + 1);