X-Git-Url: https://git.librecmc.org/?a=blobdiff_plain;f=cmd%2Ftpm-common.c;h=88c9e08e318d129316dd2d206d4049fd0060b1e8;hb=358b6f72c28c80699536f7137063095116e1675e;hp=38900fb1590bbfc2bee376961f7e89dffd1a51c9;hpb=5939afc9611e8ba4a86b96e67670b765ee27668e;p=oweals%2Fu-boot.git diff --git a/cmd/tpm-common.c b/cmd/tpm-common.c index 38900fb159..88c9e08e31 100644 --- a/cmd/tpm-common.c +++ b/cmd/tpm-common.c @@ -7,11 +7,14 @@ #include #include #include +#include #include #include #include #include "tpm-user-utils.h" +static struct udevice *tpm_dev; + /** * Print a byte string in hexdecimal format, 16-bytes per line. * @@ -231,20 +234,87 @@ 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_info(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[]) +int do_tpm_device(struct cmd_tbl *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(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[]) { struct udevice *dev; char buf[80]; @@ -263,7 +333,7 @@ int do_tpm_info(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[]) return 0; } -int do_tpm_init(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) +int do_tpm_init(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[]) { struct udevice *dev; int rc; @@ -277,9 +347,9 @@ int do_tpm_init(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) return report_return_code(tpm_init(dev)); } -int do_tpm(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) +int do_tpm(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[]) { - cmd_tbl_t *tpm_commands, *cmd; + struct cmd_tbl *tpm_commands, *cmd; struct tpm_chip_priv *priv; struct udevice *dev; unsigned int size;