tpm: add TPM2_SelfTest command support
[oweals/u-boot.git] / cmd / tpm-v2.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright (c) 2018 Bootlin
4  * Author: Miquel Raynal <miquel.raynal@bootlin.com>
5  */
6
7 #include <common.h>
8 #include <dm.h>
9 #include <log.h>
10 #include <tpm-common.h>
11 #include <tpm-v2.h>
12 #include "tpm-user-utils.h"
13
14 static int do_tpm2_startup(cmd_tbl_t *cmdtp, int flag, int argc,
15                            char * const argv[])
16 {
17         enum tpm2_startup_types mode;
18
19         if (argc != 2)
20                 return CMD_RET_USAGE;
21
22         if (!strcasecmp("TPM2_SU_CLEAR", argv[1])) {
23                 mode = TPM2_SU_CLEAR;
24         } else if (!strcasecmp("TPM2_SU_STATE", argv[1])) {
25                 mode = TPM2_SU_STATE;
26         } else {
27                 printf("Couldn't recognize mode string: %s\n", argv[1]);
28                 return CMD_RET_FAILURE;
29         }
30
31         return report_return_code(tpm2_startup(mode));
32 }
33
34 static int do_tpm2_self_test(cmd_tbl_t *cmdtp, int flag, int argc,
35                              char * const argv[])
36 {
37         enum tpm2_yes_no full_test;
38
39         if (argc != 2)
40                 return CMD_RET_USAGE;
41
42         if (!strcasecmp("full", argv[1])) {
43                 full_test = TPMI_YES;
44         } else if (!strcasecmp("continue", argv[1])) {
45                 full_test = TPMI_NO;
46         } else {
47                 printf("Couldn't recognize test mode: %s\n", argv[1]);
48                 return CMD_RET_FAILURE;
49         }
50
51         return report_return_code(tpm2_self_test(full_test));
52 }
53
54 static cmd_tbl_t tpm2_commands[] = {
55         U_BOOT_CMD_MKENT(info, 0, 1, do_tpm_info, "", ""),
56         U_BOOT_CMD_MKENT(init, 0, 1, do_tpm_init, "", ""),
57         U_BOOT_CMD_MKENT(startup, 0, 1, do_tpm2_startup, "", ""),
58         U_BOOT_CMD_MKENT(self_test, 0, 1, do_tpm2_self_test, "", ""),
59 };
60
61 cmd_tbl_t *get_tpm_commands(unsigned int *size)
62 {
63         *size = ARRAY_SIZE(tpm2_commands);
64
65         return tpm2_commands;
66 }
67
68 U_BOOT_CMD(tpm, CONFIG_SYS_MAXARGS, 1, do_tpm, "Issue a TPMv2.x command",
69 "<command> [<arguments>]\n"
70 "\n"
71 "info\n"
72 "    Show information about the TPM.\n"
73 "init\n"
74 "    Initialize the software stack. Always the first command to issue.\n"
75 "startup <mode>\n"
76 "    Issue a TPM2_Startup command.\n"
77 "    <mode> is one of:\n"
78 "        * TPM2_SU_CLEAR (reset state)\n"
79 "        * TPM2_SU_STATE (preserved state)\n"
80 "self_test <type>\n"
81 "    Test the TPM capabilities.\n"
82 "    <type> is one of:\n"
83 "        * full (perform all tests)\n"
84 "        * continue (only check untested tests)\n"
85 );