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>
15 #include "tpm-user-utils.h"
17 static struct udevice *tpm_dev;
20 * Print a byte string in hexdecimal format, 16-bytes per line.
22 * @param data byte string to be printed
23 * @param count number of bytes to be printed
25 void print_byte_string(u8 *data, size_t count)
27 int i, print_newline = 0;
29 for (i = 0; i < count; i++) {
30 printf(" %02x", data[i]);
31 print_newline = (i % 16 == 15);
35 /* Avoid duplicated newline at the end */
41 * Convert a text string of hexdecimal values into a byte string.
43 * @param bytes text string of hexdecimal values with no space
45 * @param data output buffer for byte string. The caller has to make
46 * sure it is large enough for storing the output. If
47 * NULL is passed, a large enough buffer will be allocated,
48 * and the caller must free it.
49 * @param count_ptr output variable for the length of byte string
50 * Return: pointer to output buffer
52 void *parse_byte_string(char *bytes, u8 *data, size_t *count_ptr)
60 length = strlen(bytes);
69 for (i = 0; i < length; i += 2) {
71 byte[1] = bytes[i + 1];
72 data[i / 2] = (u8)hextoul(byte, NULL);
82 * report_return_code() - Report any error and return failure or success
84 * @param return_code TPM command return code
85 * Return: value of enum command_ret_t
87 int report_return_code(int return_code)
90 printf("Error: %d\n", return_code);
91 return CMD_RET_FAILURE;
93 return CMD_RET_SUCCESS;
98 * Return number of values defined by a type string.
100 * @param type_str type string
101 * Return: number of values of type string
103 int type_string_get_num_values(const char *type_str)
105 return strlen(type_str);
109 * Return total size of values defined by a type string.
111 * @param type_str type string
112 * Return: total size of values of type string, or 0 if type string
113 * contains illegal type character.
115 size_t type_string_get_space_size(const char *type_str)
119 for (size = 0; *type_str; type_str++) {
139 * Allocate a buffer large enough to hold values defined by a type
140 * string. The caller has to free the buffer.
142 * @param type_str type string
143 * @param count pointer for storing size of buffer
144 * Return: pointer to buffer or NULL on error
146 void *type_string_alloc(const char *type_str, u32 *count)
151 size = type_string_get_space_size(type_str);
162 * Pack values defined by a type string into a buffer. The buffer must have
163 * large enough space.
165 * @param type_str type string
166 * @param values text strings of values to be packed
167 * @param data output buffer of values
168 * Return: 0 on success, non-0 on error
170 int type_string_pack(const char *type_str, char * const values[],
176 for (offset = 0; *type_str; type_str++, values++) {
177 value = simple_strtoul(values[0], NULL, 0);
180 data[offset] = value;
184 put_unaligned_be16(value, data + offset);
188 put_unaligned_be32(value, data + offset);
200 * Read values defined by a type string from a buffer, and write these values
201 * to environment variables.
203 * @param type_str type string
204 * @param data input buffer of values
205 * @param vars names of environment variables
206 * Return: 0 on success, non-0 on error
208 int type_string_write_vars(const char *type_str, u8 *data,
214 for (offset = 0; *type_str; type_str++, vars++) {
217 value = data[offset];
221 value = get_unaligned_be16(data + offset);
225 value = get_unaligned_be32(data + offset);
231 if (env_set_ulong(*vars, value))
238 static int tpm_show_device(void)
244 for_each_tpm_device(dev) {
245 rc = tpm_get_desc(dev, buf, sizeof(buf));
247 printf("device %d: can't get info\n", n);
249 printf("device %d: %s\n", n, buf);
257 static int tpm_set_device(unsigned long num)
261 int rc = CMD_RET_FAILURE;
263 for_each_tpm_device(dev) {
278 int get_tpm(struct udevice **devp)
283 * To keep a backward compatibility with previous code,
284 * if a tpm device is not explicitly set, we set the first one.
287 rc = tpm_set_device(0);
289 printf("Couldn't set TPM 0 (rc = %d)\n", rc);
290 return CMD_RET_FAILURE;
300 int do_tpm_device(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
306 num = dectoul(argv[1], NULL);
308 rc = tpm_set_device(num);
310 printf("Couldn't set TPM %lu (rc = %d)\n", num, rc);
312 rc = tpm_show_device();
318 int do_tpm_info(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
327 rc = tpm_get_desc(dev, buf, sizeof(buf));
329 printf("Couldn't get TPM info (%d)\n", rc);
330 return CMD_RET_FAILURE;
337 int do_tpm_report_state(struct cmd_tbl *cmdtp, int flag, int argc,
347 rc = tpm_report_state(dev, buf, sizeof(buf));
349 printf("Couldn't get TPM state (%d)\n", rc);
350 return CMD_RET_FAILURE;
357 int do_tpm_init(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
363 return CMD_RET_USAGE;
368 return report_return_code(tpm_init(dev));
371 int do_tpm_autostart(struct cmd_tbl *cmdtp, int flag, int argc,
378 return CMD_RET_USAGE;
383 return report_return_code(tpm_auto_start(dev));
386 int do_tpm(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
388 struct cmd_tbl *tpm_commands, *cmd;
389 struct tpm_chip_priv *priv;
395 return CMD_RET_USAGE;
401 priv = dev_get_uclass_priv(dev);
403 /* Below getters return NULL if the desired stack is not built */
404 switch (priv->version) {
406 tpm_commands = get_tpm1_commands(&size);
409 tpm_commands = get_tpm2_commands(&size);
416 return CMD_RET_USAGE;
418 cmd = find_cmd_tbl(argv[1], tpm_commands, size);
420 return CMD_RET_USAGE;
422 return cmd->cmd(cmdtp, flag, argc - 1, argv + 1);