tpm: add TPM2_PCR_Extend command support
[platform/kernel/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 <mapmem.h>
11 #include <tpm-common.h>
12 #include <tpm-v2.h>
13 #include "tpm-user-utils.h"
14
15 static int do_tpm2_startup(cmd_tbl_t *cmdtp, int flag, int argc,
16                            char * const argv[])
17 {
18         enum tpm2_startup_types mode;
19
20         if (argc != 2)
21                 return CMD_RET_USAGE;
22
23         if (!strcasecmp("TPM2_SU_CLEAR", argv[1])) {
24                 mode = TPM2_SU_CLEAR;
25         } else if (!strcasecmp("TPM2_SU_STATE", argv[1])) {
26                 mode = TPM2_SU_STATE;
27         } else {
28                 printf("Couldn't recognize mode string: %s\n", argv[1]);
29                 return CMD_RET_FAILURE;
30         }
31
32         return report_return_code(tpm2_startup(mode));
33 }
34
35 static int do_tpm2_self_test(cmd_tbl_t *cmdtp, int flag, int argc,
36                              char * const argv[])
37 {
38         enum tpm2_yes_no full_test;
39
40         if (argc != 2)
41                 return CMD_RET_USAGE;
42
43         if (!strcasecmp("full", argv[1])) {
44                 full_test = TPMI_YES;
45         } else if (!strcasecmp("continue", argv[1])) {
46                 full_test = TPMI_NO;
47         } else {
48                 printf("Couldn't recognize test mode: %s\n", argv[1]);
49                 return CMD_RET_FAILURE;
50         }
51
52         return report_return_code(tpm2_self_test(full_test));
53 }
54
55 static int do_tpm2_clear(cmd_tbl_t *cmdtp, int flag, int argc,
56                          char * const argv[])
57 {
58         u32 handle = 0;
59         const char *pw = (argc < 3) ? NULL : argv[2];
60         const ssize_t pw_sz = pw ? strlen(pw) : 0;
61
62         if (argc < 2 || argc > 3)
63                 return CMD_RET_USAGE;
64
65         if (pw_sz > TPM2_DIGEST_LEN)
66                 return -EINVAL;
67
68         if (!strcasecmp("TPM2_RH_LOCKOUT", argv[1]))
69                 handle = TPM2_RH_LOCKOUT;
70         else if (!strcasecmp("TPM2_RH_PLATFORM", argv[1]))
71                 handle = TPM2_RH_PLATFORM;
72         else
73                 return CMD_RET_USAGE;
74
75         return report_return_code(tpm2_clear(handle, pw, pw_sz));
76 }
77
78 static int do_tpm2_pcr_extend(cmd_tbl_t *cmdtp, int flag, int argc,
79                               char * const argv[])
80 {
81         struct udevice *dev;
82         struct tpm_chip_priv *priv;
83         u32 index = simple_strtoul(argv[1], NULL, 0);
84         void *digest = map_sysmem(simple_strtoul(argv[2], NULL, 0), 0);
85         int ret;
86         u32 rc;
87
88         if (argc != 3)
89                 return CMD_RET_USAGE;
90
91         ret = uclass_first_device_err(UCLASS_TPM, &dev);
92         if (ret)
93                 return ret;
94
95         priv = dev_get_uclass_priv(dev);
96         if (!priv)
97                 return -EINVAL;
98
99         if (index >= priv->pcr_count)
100                 return -EINVAL;
101
102         rc = tpm2_pcr_extend(index, digest);
103
104         unmap_sysmem(digest);
105
106         return report_return_code(rc);
107 }
108
109 static cmd_tbl_t tpm2_commands[] = {
110         U_BOOT_CMD_MKENT(info, 0, 1, do_tpm_info, "", ""),
111         U_BOOT_CMD_MKENT(init, 0, 1, do_tpm_init, "", ""),
112         U_BOOT_CMD_MKENT(startup, 0, 1, do_tpm2_startup, "", ""),
113         U_BOOT_CMD_MKENT(self_test, 0, 1, do_tpm2_self_test, "", ""),
114         U_BOOT_CMD_MKENT(clear, 0, 1, do_tpm2_clear, "", ""),
115         U_BOOT_CMD_MKENT(pcr_extend, 0, 1, do_tpm2_pcr_extend, "", ""),
116 };
117
118 cmd_tbl_t *get_tpm_commands(unsigned int *size)
119 {
120         *size = ARRAY_SIZE(tpm2_commands);
121
122         return tpm2_commands;
123 }
124
125 U_BOOT_CMD(tpm, CONFIG_SYS_MAXARGS, 1, do_tpm, "Issue a TPMv2.x command",
126 "<command> [<arguments>]\n"
127 "\n"
128 "info\n"
129 "    Show information about the TPM.\n"
130 "init\n"
131 "    Initialize the software stack. Always the first command to issue.\n"
132 "startup <mode>\n"
133 "    Issue a TPM2_Startup command.\n"
134 "    <mode> is one of:\n"
135 "        * TPM2_SU_CLEAR (reset state)\n"
136 "        * TPM2_SU_STATE (preserved state)\n"
137 "self_test <type>\n"
138 "    Test the TPM capabilities.\n"
139 "    <type> is one of:\n"
140 "        * full (perform all tests)\n"
141 "        * continue (only check untested tests)\n"
142 "clear <hierarchy>\n"
143 "    Issue a TPM2_Clear command.\n"
144 "    <hierarchy> is one of:\n"
145 "        * TPM2_RH_LOCKOUT\n"
146 "        * TPM2_RH_PLATFORM\n"
147 "pcr_extend <pcr> <digest_addr>\n"
148 "    Extend PCR #<pcr> with digest at <digest_addr>.\n"
149 "    <pcr>: index of the PCR\n"
150 "    <digest_addr>: address of a 32-byte SHA256 digest\n"
151 );