1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (c) 2018 Bootlin
4 * Author: Miquel Raynal <miquel.raynal@bootlin.com>
10 #include <tpm-common.h>
12 #include "tpm-user-utils.h"
14 static int do_tpm2_startup(cmd_tbl_t *cmdtp, int flag, int argc,
17 enum tpm2_startup_types mode;
22 if (!strcasecmp("TPM2_SU_CLEAR", argv[1])) {
24 } else if (!strcasecmp("TPM2_SU_STATE", argv[1])) {
27 printf("Couldn't recognize mode string: %s\n", argv[1]);
28 return CMD_RET_FAILURE;
31 return report_return_code(tpm2_startup(mode));
34 static int do_tpm2_self_test(cmd_tbl_t *cmdtp, int flag, int argc,
37 enum tpm2_yes_no full_test;
42 if (!strcasecmp("full", argv[1])) {
44 } else if (!strcasecmp("continue", argv[1])) {
47 printf("Couldn't recognize test mode: %s\n", argv[1]);
48 return CMD_RET_FAILURE;
51 return report_return_code(tpm2_self_test(full_test));
54 static int do_tpm2_clear(cmd_tbl_t *cmdtp, int flag, int argc,
58 const char *pw = (argc < 3) ? NULL : argv[2];
59 const ssize_t pw_sz = pw ? strlen(pw) : 0;
61 if (argc < 2 || argc > 3)
64 if (pw_sz > TPM2_DIGEST_LEN)
67 if (!strcasecmp("TPM2_RH_LOCKOUT", argv[1]))
68 handle = TPM2_RH_LOCKOUT;
69 else if (!strcasecmp("TPM2_RH_PLATFORM", argv[1]))
70 handle = TPM2_RH_PLATFORM;
74 return report_return_code(tpm2_clear(handle, pw, pw_sz));
77 static cmd_tbl_t tpm2_commands[] = {
78 U_BOOT_CMD_MKENT(info, 0, 1, do_tpm_info, "", ""),
79 U_BOOT_CMD_MKENT(init, 0, 1, do_tpm_init, "", ""),
80 U_BOOT_CMD_MKENT(startup, 0, 1, do_tpm2_startup, "", ""),
81 U_BOOT_CMD_MKENT(self_test, 0, 1, do_tpm2_self_test, "", ""),
82 U_BOOT_CMD_MKENT(clear, 0, 1, do_tpm2_clear, "", ""),
85 cmd_tbl_t *get_tpm_commands(unsigned int *size)
87 *size = ARRAY_SIZE(tpm2_commands);
92 U_BOOT_CMD(tpm, CONFIG_SYS_MAXARGS, 1, do_tpm, "Issue a TPMv2.x command",
93 "<command> [<arguments>]\n"
96 " Show information about the TPM.\n"
98 " Initialize the software stack. Always the first command to issue.\n"
100 " Issue a TPM2_Startup command.\n"
101 " <mode> is one of:\n"
102 " * TPM2_SU_CLEAR (reset state)\n"
103 " * TPM2_SU_STATE (preserved state)\n"
105 " Test the TPM capabilities.\n"
106 " <type> is one of:\n"
107 " * full (perform all tests)\n"
108 " * continue (only check untested tests)\n"
109 "clear <hierarchy>\n"
110 " Issue a TPM2_Clear command.\n"
111 " <hierarchy> is one of:\n"
112 " * TPM2_RH_LOCKOUT\n"
113 " * TPM2_RH_PLATFORM\n"