diff options
Diffstat (limited to 'cmd/tpm-common.c')
| -rw-r--r-- | cmd/tpm-common.c | 78 | 
1 files changed, 74 insertions, 4 deletions
| diff --git a/cmd/tpm-common.c b/cmd/tpm-common.c index 38900fb1590..30142552290 100644 --- a/cmd/tpm-common.c +++ b/cmd/tpm-common.c @@ -7,11 +7,14 @@  #include <command.h>  #include <dm.h>  #include <env.h> +#include <malloc.h>  #include <asm/unaligned.h>  #include <linux/string.h>  #include <tpm-common.h>  #include "tpm-user-utils.h" +static struct udevice *tpm_dev; +  /**   * Print a byte string in hexdecimal format, 16-bytes per line.   * @@ -231,19 +234,86 @@ int type_string_write_vars(const char *type_str, u8 *data,  	return 0;  } +static int tpm_show_device(void) +{ +	struct udevice *dev; +	char buf[80]; +	int n = 0, rc; + +	for_each_tpm_device(dev) { +		rc = tpm_get_desc(dev, buf, sizeof(buf)); +		if (rc < 0) +			printf("device %d: can't get info\n", n); +		else +			printf("device %d: %s\n", n, buf); + +		n++; +	}; + +	return 0; +} + +static int tpm_set_device(unsigned long num) +{ +	struct udevice *dev; +	unsigned long n = 0; +	int rc = CMD_RET_FAILURE; + +	for_each_tpm_device(dev) { +		if (n == num) { +			rc = 0; +			break; +		} + +		n++; +	} + +	if (!rc) +		tpm_dev = dev; + +	return rc; +} +  int get_tpm(struct udevice **devp)  {  	int rc; -	rc = uclass_first_device_err(UCLASS_TPM, devp); -	if (rc) { -		printf("Could not find TPM (ret=%d)\n", rc); -		return CMD_RET_FAILURE; +	/* +	 * To keep a backward compatibility with previous code, +	 * if a tpm device is not explicitly set, we set the first one. +	 */ +	if (!tpm_dev) { +		rc = tpm_set_device(0); +		if (rc) { +			printf("Couldn't set TPM 0 (rc = %d)\n", rc); +			return CMD_RET_FAILURE; +		}  	} +	if (devp) +		*devp = tpm_dev; +  	return 0;  } +int do_tpm_device(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[]) +{ +	unsigned long num; +	int rc; + +	if (argc == 2) { +		num = simple_strtoul(argv[1], NULL, 10); + +		rc = tpm_set_device(num); +		if (rc) +			printf("Couldn't set TPM %lu (rc = %d)\n", num, rc); +	} else { +		rc = tpm_show_device(); +	} + +	return rc; +} +  int do_tpm_info(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[])  {  	struct udevice *dev; | 
