1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (c) 2013 The Chromium OS Authors.
11 #include <asm/unaligned.h>
12 #include <linux/string.h>
13 #include <tpm-common.h>
14 #include "tpm-user-utils.h"
16 static struct udevice *tpm_dev;
19 * Print a byte string in hexdecimal format, 16-bytes per line.
21 * @param data byte string to be printed
22 * @param count number of bytes to be printed
24 void print_byte_string(u8 *data, size_t count)
26 int i, print_newline = 0;
28 for (i = 0; i < count; i++) {
29 printf(" %02x", data[i]);
30 print_newline = (i % 16 == 15);
34 /* Avoid duplicated newline at the end */
40 * Convert a text string of hexdecimal values into a byte string.
42 * @param bytes text string of hexdecimal values with no space
44 * @param data output buffer for byte string. The caller has to make
45 * sure it is large enough for storing the output. If
46 * NULL is passed, a large enough buffer will be allocated,
47 * and the caller must free it.
48 * @param count_ptr output variable for the length of byte string
49 * @return pointer to output buffer
51 void *parse_byte_string(char *bytes, u8 *data, size_t *count_ptr)
59 length = strlen(bytes);
68 for (i = 0; i < length; i += 2) {
70 byte[1] = bytes[i + 1];
71 data[i / 2] = (u8)simple_strtoul(byte, NULL, 16);
81 * report_return_code() - Report any error and return failure or success
83 * @param return_code TPM command return code
84 * @return value of enum command_ret_t
86 int report_return_code(int return_code)
89 printf("Error: %d\n", return_code);
90 return CMD_RET_FAILURE;
92 return CMD_RET_SUCCESS;
97 * Return number of values defined by a type string.
99 * @param type_str type string
100 * @return number of values of type string
102 int type_string_get_num_values(const char *type_str)
104 return strlen(type_str);
108 * Return total size of values defined by a type string.
110 * @param type_str type string
111 * @return total size of values of type string, or 0 if type string
112 * contains illegal type character.
114 size_t type_string_get_space_size(const char *type_str)
118 for (size = 0; *type_str; type_str++) {
138 * Allocate a buffer large enough to hold values defined by a type
139 * string. The caller has to free the buffer.
141 * @param type_str type string
142 * @param count pointer for storing size of buffer
143 * @return pointer to buffer or NULL on error
145 void *type_string_alloc(const char *type_str, u32 *count)
150 size = type_string_get_space_size(type_str);
161 * Pack values defined by a type string into a buffer. The buffer must have
162 * large enough space.
164 * @param type_str type string
165 * @param values text strings of values to be packed
166 * @param data output buffer of values
167 * @return 0 on success, non-0 on error
169 int type_string_pack(const char *type_str, char * const values[],
175 for (offset = 0; *type_str; type_str++, values++) {
176 value = simple_strtoul(values[0], NULL, 0);
179 data[offset] = value;
183 put_unaligned_be16(value, data + offset);
187 put_unaligned_be32(value, data + offset);
199 * Read values defined by a type string from a buffer, and write these values
200 * to environment variables.
202 * @param type_str type string
203 * @param data input buffer of values
204 * @param vars names of environment variables
205 * @return 0 on success, non-0 on error
207 int type_string_write_vars(const char *type_str, u8 *data,
213 for (offset = 0; *type_str; type_str++, vars++) {
216 value = data[offset];
220 value = get_unaligned_be16(data + offset);
224 value = get_unaligned_be32(data + offset);
230 if (env_set_ulong(*vars, value))
237 static int tpm_show_device(void)
243 for_each_tpm_device(dev) {
244 rc = tpm_get_desc(dev, buf, sizeof(buf));
246 printf("device %d: can't get info\n", n);
248 printf("device %d: %s\n", n, buf);
256 static int tpm_set_device(unsigned long num)
260 int rc = CMD_RET_FAILURE;
262 for_each_tpm_device(dev) {
277 int get_tpm(struct udevice **devp)
282 * To keep a backward compatibility with previous code,
283 * if a tpm device is not explicitly set, we set the first one.
286 rc = tpm_set_device(0);
288 printf("Couldn't set TPM 0 (rc = %d)\n", rc);
289 return CMD_RET_FAILURE;
299 int do_tpm_device(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
305 num = simple_strtoul(argv[1], NULL, 10);
307 rc = tpm_set_device(num);
309 printf("Couldn't set TPM %lu (rc = %d)\n", num, rc);
311 rc = tpm_show_device();
317 int do_tpm_info(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
326 rc = tpm_get_desc(dev, buf, sizeof(buf));
328 printf("Couldn't get TPM info (%d)\n", rc);
329 return CMD_RET_FAILURE;
336 int do_tpm_init(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
342 return CMD_RET_USAGE;
347 return report_return_code(tpm_init(dev));
350 int do_tpm(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
352 struct cmd_tbl *tpm_commands, *cmd;
353 struct tpm_chip_priv *priv;
359 return CMD_RET_USAGE;
365 priv = dev_get_uclass_priv(dev);
367 /* Below getters return NULL if the desired stack is not built */
368 switch (priv->version) {
370 tpm_commands = get_tpm1_commands(&size);
373 tpm_commands = get_tpm2_commands(&size);
380 return CMD_RET_USAGE;
382 cmd = find_cmd_tbl(argv[1], tpm_commands, size);
384 return CMD_RET_USAGE;
386 return cmd->cmd(cmdtp, flag, argc - 1, argv + 1);