blob: 0874c4d7baff044e5d802e27b398fe6201740103 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Vadim Bendebury576fb1e2011-10-15 15:13:34 +00002/*
Che-liang Chiou8732b072013-02-28 09:34:57 +00003 * Copyright (c) 2013 The Chromium OS Authors.
Vadim Bendebury576fb1e2011-10-15 15:13:34 +00004 */
5
6#include <common.h>
Che-liang Chiou8732b072013-02-28 09:34:57 +00007#include <malloc.h>
Che-liang Chiou8732b072013-02-28 09:34:57 +00008#include <asm/unaligned.h>
Miquel Raynald677bfe2018-05-15 11:57:06 +02009#include <tpm-common.h>
10#include <tpm-v1.h>
11#include "tpm-user-utils.h"
Che-liang Chiou8732b072013-02-28 09:34:57 +000012
Miquel Raynalc6179182018-05-15 11:57:00 +020013static int do_tpm_startup(cmd_tbl_t *cmdtp, int flag, int argc,
14 char * const argv[])
Che-liang Chiou8732b072013-02-28 09:34:57 +000015{
16 enum tpm_startup_type mode;
17
18 if (argc != 2)
19 return CMD_RET_USAGE;
20 if (!strcasecmp("TPM_ST_CLEAR", argv[1])) {
21 mode = TPM_ST_CLEAR;
22 } else if (!strcasecmp("TPM_ST_STATE", argv[1])) {
23 mode = TPM_ST_STATE;
24 } else if (!strcasecmp("TPM_ST_DEACTIVATED", argv[1])) {
25 mode = TPM_ST_DEACTIVATED;
26 } else {
27 printf("Couldn't recognize mode string: %s\n", argv[1]);
28 return CMD_RET_FAILURE;
29 }
30
Simon Glassf8f1fe12015-08-22 18:31:34 -060031 return report_return_code(tpm_startup(mode));
Che-liang Chiou8732b072013-02-28 09:34:57 +000032}
33
Miquel Raynalc6179182018-05-15 11:57:00 +020034static int do_tpm_nv_define_space(cmd_tbl_t *cmdtp, int flag, int argc,
35 char * const argv[])
Che-liang Chiou8732b072013-02-28 09:34:57 +000036{
Miquel Raynalb9804e52018-05-15 11:56:59 +020037 u32 index, perm, size;
Che-liang Chiou8732b072013-02-28 09:34:57 +000038
39 if (argc != 4)
40 return CMD_RET_USAGE;
41 index = simple_strtoul(argv[1], NULL, 0);
42 perm = simple_strtoul(argv[2], NULL, 0);
43 size = simple_strtoul(argv[3], NULL, 0);
44
Simon Glassf8f1fe12015-08-22 18:31:34 -060045 return report_return_code(tpm_nv_define_space(index, perm, size));
Che-liang Chiou8732b072013-02-28 09:34:57 +000046}
47
Miquel Raynalc6179182018-05-15 11:57:00 +020048static int do_tpm_nv_read_value(cmd_tbl_t *cmdtp, int flag, int argc,
49 char * const argv[])
Che-liang Chiou8732b072013-02-28 09:34:57 +000050{
Miquel Raynalb9804e52018-05-15 11:56:59 +020051 u32 index, count, rc;
Che-liang Chiou8732b072013-02-28 09:34:57 +000052 void *data;
53
54 if (argc != 4)
55 return CMD_RET_USAGE;
56 index = simple_strtoul(argv[1], NULL, 0);
57 data = (void *)simple_strtoul(argv[2], NULL, 0);
58 count = simple_strtoul(argv[3], NULL, 0);
59
60 rc = tpm_nv_read_value(index, data, count);
61 if (!rc) {
62 puts("area content:\n");
63 print_byte_string(data, count);
64 }
65
Simon Glassf8f1fe12015-08-22 18:31:34 -060066 return report_return_code(rc);
Che-liang Chiou8732b072013-02-28 09:34:57 +000067}
68
Miquel Raynalc6179182018-05-15 11:57:00 +020069static int do_tpm_nv_write_value(cmd_tbl_t *cmdtp, int flag, int argc,
70 char * const argv[])
Che-liang Chiou8732b072013-02-28 09:34:57 +000071{
Miquel Raynalb9804e52018-05-15 11:56:59 +020072 u32 index, rc;
Che-liang Chiou8732b072013-02-28 09:34:57 +000073 size_t count;
74 void *data;
75
76 if (argc != 3)
77 return CMD_RET_USAGE;
78 index = simple_strtoul(argv[1], NULL, 0);
79 data = parse_byte_string(argv[2], NULL, &count);
80 if (!data) {
81 printf("Couldn't parse byte string %s\n", argv[2]);
82 return CMD_RET_FAILURE;
83 }
84
85 rc = tpm_nv_write_value(index, data, count);
86 free(data);
87
Simon Glassf8f1fe12015-08-22 18:31:34 -060088 return report_return_code(rc);
Che-liang Chiou8732b072013-02-28 09:34:57 +000089}
90
Miquel Raynalc6179182018-05-15 11:57:00 +020091static int do_tpm_extend(cmd_tbl_t *cmdtp, int flag, int argc,
92 char * const argv[])
Che-liang Chiou8732b072013-02-28 09:34:57 +000093{
Miquel Raynalb9804e52018-05-15 11:56:59 +020094 u32 index, rc;
95 u8 in_digest[20], out_digest[20];
Che-liang Chiou8732b072013-02-28 09:34:57 +000096
97 if (argc != 3)
98 return CMD_RET_USAGE;
99 index = simple_strtoul(argv[1], NULL, 0);
100 if (!parse_byte_string(argv[2], in_digest, NULL)) {
101 printf("Couldn't parse byte string %s\n", argv[2]);
102 return CMD_RET_FAILURE;
103 }
104
105 rc = tpm_extend(index, in_digest, out_digest);
106 if (!rc) {
107 puts("PCR value after execution of the command:\n");
108 print_byte_string(out_digest, sizeof(out_digest));
109 }
110
Simon Glassf8f1fe12015-08-22 18:31:34 -0600111 return report_return_code(rc);
Che-liang Chiou8732b072013-02-28 09:34:57 +0000112}
113
Miquel Raynalc6179182018-05-15 11:57:00 +0200114static int do_tpm_pcr_read(cmd_tbl_t *cmdtp, int flag, int argc,
115 char * const argv[])
Che-liang Chiou8732b072013-02-28 09:34:57 +0000116{
Miquel Raynalb9804e52018-05-15 11:56:59 +0200117 u32 index, count, rc;
Che-liang Chiou8732b072013-02-28 09:34:57 +0000118 void *data;
119
120 if (argc != 4)
121 return CMD_RET_USAGE;
122 index = simple_strtoul(argv[1], NULL, 0);
123 data = (void *)simple_strtoul(argv[2], NULL, 0);
124 count = simple_strtoul(argv[3], NULL, 0);
125
126 rc = tpm_pcr_read(index, data, count);
127 if (!rc) {
128 puts("Named PCR content:\n");
129 print_byte_string(data, count);
130 }
131
Simon Glassf8f1fe12015-08-22 18:31:34 -0600132 return report_return_code(rc);
Che-liang Chiou8732b072013-02-28 09:34:57 +0000133}
134
Miquel Raynalc6179182018-05-15 11:57:00 +0200135static int do_tpm_tsc_physical_presence(cmd_tbl_t *cmdtp, int flag, int argc,
136 char * const argv[])
Che-liang Chiou8732b072013-02-28 09:34:57 +0000137{
Miquel Raynalb9804e52018-05-15 11:56:59 +0200138 u16 presence;
Che-liang Chiou8732b072013-02-28 09:34:57 +0000139
140 if (argc != 2)
141 return CMD_RET_USAGE;
Miquel Raynalb9804e52018-05-15 11:56:59 +0200142 presence = (u16)simple_strtoul(argv[1], NULL, 0);
Che-liang Chiou8732b072013-02-28 09:34:57 +0000143
Simon Glassf8f1fe12015-08-22 18:31:34 -0600144 return report_return_code(tpm_tsc_physical_presence(presence));
Che-liang Chiou8732b072013-02-28 09:34:57 +0000145}
146
Miquel Raynalc6179182018-05-15 11:57:00 +0200147static int do_tpm_read_pubek(cmd_tbl_t *cmdtp, int flag, int argc,
148 char * const argv[])
Che-liang Chiou8732b072013-02-28 09:34:57 +0000149{
Miquel Raynalb9804e52018-05-15 11:56:59 +0200150 u32 count, rc;
Che-liang Chiou8732b072013-02-28 09:34:57 +0000151 void *data;
152
153 if (argc != 3)
154 return CMD_RET_USAGE;
155 data = (void *)simple_strtoul(argv[1], NULL, 0);
156 count = simple_strtoul(argv[2], NULL, 0);
157
158 rc = tpm_read_pubek(data, count);
159 if (!rc) {
160 puts("pubek value:\n");
161 print_byte_string(data, count);
162 }
163
Simon Glassf8f1fe12015-08-22 18:31:34 -0600164 return report_return_code(rc);
Che-liang Chiou8732b072013-02-28 09:34:57 +0000165}
166
Miquel Raynalc6179182018-05-15 11:57:00 +0200167static int do_tpm_physical_set_deactivated(cmd_tbl_t *cmdtp, int flag, int argc,
168 char * const argv[])
Che-liang Chiou8732b072013-02-28 09:34:57 +0000169{
Miquel Raynalb9804e52018-05-15 11:56:59 +0200170 u8 state;
Che-liang Chiou8732b072013-02-28 09:34:57 +0000171
172 if (argc != 2)
173 return CMD_RET_USAGE;
Miquel Raynalb9804e52018-05-15 11:56:59 +0200174 state = (u8)simple_strtoul(argv[1], NULL, 0);
Che-liang Chiou8732b072013-02-28 09:34:57 +0000175
Simon Glassf8f1fe12015-08-22 18:31:34 -0600176 return report_return_code(tpm_physical_set_deactivated(state));
Che-liang Chiou8732b072013-02-28 09:34:57 +0000177}
178
Miquel Raynalc6179182018-05-15 11:57:00 +0200179static int do_tpm_get_capability(cmd_tbl_t *cmdtp, int flag, int argc,
180 char * const argv[])
Che-liang Chiou8732b072013-02-28 09:34:57 +0000181{
Miquel Raynalb9804e52018-05-15 11:56:59 +0200182 u32 cap_area, sub_cap, rc;
Che-liang Chiou8732b072013-02-28 09:34:57 +0000183 void *cap;
184 size_t count;
185
186 if (argc != 5)
187 return CMD_RET_USAGE;
188 cap_area = simple_strtoul(argv[1], NULL, 0);
189 sub_cap = simple_strtoul(argv[2], NULL, 0);
190 cap = (void *)simple_strtoul(argv[3], NULL, 0);
191 count = simple_strtoul(argv[4], NULL, 0);
192
193 rc = tpm_get_capability(cap_area, sub_cap, cap, count);
194 if (!rc) {
195 puts("capability information:\n");
196 print_byte_string(cap, count);
197 }
198
Simon Glassf8f1fe12015-08-22 18:31:34 -0600199 return report_return_code(rc);
Che-liang Chiou8732b072013-02-28 09:34:57 +0000200}
201
Miquel Raynalc6179182018-05-15 11:57:00 +0200202static int do_tpm_raw_transfer(cmd_tbl_t *cmdtp, int flag, int argc,
203 char * const argv[])
Che-liang Chiou8732b072013-02-28 09:34:57 +0000204{
Christophe Ricardc2b0f602015-10-06 22:54:43 +0200205 struct udevice *dev;
Che-liang Chiou8732b072013-02-28 09:34:57 +0000206 void *command;
Miquel Raynalb9804e52018-05-15 11:56:59 +0200207 u8 response[1024];
Che-liang Chiou8732b072013-02-28 09:34:57 +0000208 size_t count, response_length = sizeof(response);
Miquel Raynalb9804e52018-05-15 11:56:59 +0200209 u32 rc;
Che-liang Chiou8732b072013-02-28 09:34:57 +0000210
211 command = parse_byte_string(argv[1], NULL, &count);
212 if (!command) {
213 printf("Couldn't parse byte string %s\n", argv[1]);
214 return CMD_RET_FAILURE;
215 }
216
Simon Glassc8a8c512015-08-22 18:31:32 -0600217 rc = get_tpm(&dev);
218 if (rc)
219 return rc;
220
221 rc = tpm_xfer(dev, command, count, response, &response_length);
Che-liang Chiou8732b072013-02-28 09:34:57 +0000222 free(command);
223 if (!rc) {
224 puts("tpm response:\n");
225 print_byte_string(response, response_length);
226 }
227
Simon Glassf8f1fe12015-08-22 18:31:34 -0600228 return report_return_code(rc);
Che-liang Chiou8732b072013-02-28 09:34:57 +0000229}
230
Miquel Raynalc6179182018-05-15 11:57:00 +0200231static int do_tpm_nv_define(cmd_tbl_t *cmdtp, int flag, int argc,
232 char * const argv[])
Che-liang Chiou8732b072013-02-28 09:34:57 +0000233{
Miquel Raynalb9804e52018-05-15 11:56:59 +0200234 u32 index, perm, size;
Che-liang Chiou8732b072013-02-28 09:34:57 +0000235
236 if (argc != 4)
237 return CMD_RET_USAGE;
238 size = type_string_get_space_size(argv[1]);
239 if (!size) {
240 printf("Couldn't parse arguments\n");
241 return CMD_RET_USAGE;
242 }
243 index = simple_strtoul(argv[2], NULL, 0);
244 perm = simple_strtoul(argv[3], NULL, 0);
245
Simon Glassf8f1fe12015-08-22 18:31:34 -0600246 return report_return_code(tpm_nv_define_space(index, perm, size));
Che-liang Chiou8732b072013-02-28 09:34:57 +0000247}
248
Miquel Raynalc6179182018-05-15 11:57:00 +0200249static int do_tpm_nv_read(cmd_tbl_t *cmdtp, int flag, int argc,
250 char * const argv[])
Che-liang Chiou8732b072013-02-28 09:34:57 +0000251{
Miquel Raynalb9804e52018-05-15 11:56:59 +0200252 u32 index, count, err;
Che-liang Chiou8732b072013-02-28 09:34:57 +0000253 void *data;
254
255 if (argc < 3)
256 return CMD_RET_USAGE;
257 if (argc != 3 + type_string_get_num_values(argv[1]))
258 return CMD_RET_USAGE;
259 index = simple_strtoul(argv[2], NULL, 0);
260 data = type_string_alloc(argv[1], &count);
261 if (!data) {
262 printf("Couldn't parse arguments\n");
263 return CMD_RET_USAGE;
264 }
265
266 err = tpm_nv_read_value(index, data, count);
267 if (!err) {
268 if (type_string_write_vars(argv[1], data, argv + 3)) {
269 printf("Couldn't write to variables\n");
270 err = ~0;
271 }
272 }
273 free(data);
274
Simon Glassf8f1fe12015-08-22 18:31:34 -0600275 return report_return_code(err);
Che-liang Chiou8732b072013-02-28 09:34:57 +0000276}
277
Miquel Raynalc6179182018-05-15 11:57:00 +0200278static int do_tpm_nv_write(cmd_tbl_t *cmdtp, int flag, int argc,
279 char * const argv[])
Che-liang Chiou8732b072013-02-28 09:34:57 +0000280{
Miquel Raynalb9804e52018-05-15 11:56:59 +0200281 u32 index, count, err;
Che-liang Chiou8732b072013-02-28 09:34:57 +0000282 void *data;
283
284 if (argc < 3)
285 return CMD_RET_USAGE;
286 if (argc != 3 + type_string_get_num_values(argv[1]))
287 return CMD_RET_USAGE;
288 index = simple_strtoul(argv[2], NULL, 0);
289 data = type_string_alloc(argv[1], &count);
290 if (!data) {
291 printf("Couldn't parse arguments\n");
292 return CMD_RET_USAGE;
293 }
294 if (type_string_pack(argv[1], argv + 3, data)) {
295 printf("Couldn't parse arguments\n");
296 free(data);
297 return CMD_RET_USAGE;
298 }
299
300 err = tpm_nv_write_value(index, data, count);
301 free(data);
302
Simon Glassf8f1fe12015-08-22 18:31:34 -0600303 return report_return_code(err);
Che-liang Chiou8732b072013-02-28 09:34:57 +0000304}
305
Reinhard Pfaube6c1522013-06-26 15:55:13 +0200306#ifdef CONFIG_TPM_AUTH_SESSIONS
307
Miquel Raynalc6179182018-05-15 11:57:00 +0200308static int do_tpm_oiap(cmd_tbl_t *cmdtp, int flag, int argc,
309 char * const argv[])
Reinhard Pfaube6c1522013-06-26 15:55:13 +0200310{
Miquel Raynalb9804e52018-05-15 11:56:59 +0200311 u32 auth_handle, err;
Reinhard Pfaube6c1522013-06-26 15:55:13 +0200312
313 err = tpm_oiap(&auth_handle);
314
Simon Glassf8f1fe12015-08-22 18:31:34 -0600315 return report_return_code(err);
Reinhard Pfaube6c1522013-06-26 15:55:13 +0200316}
317
mario.six@gdsys.cc0f4b2ba2017-03-20 10:28:28 +0100318#ifdef CONFIG_TPM_LOAD_KEY_BY_SHA1
319static int do_tpm_load_key_by_sha1(cmd_tbl_t *cmdtp, int flag, int argc, char *
320 const argv[])
321{
Miquel Raynalb9804e52018-05-15 11:56:59 +0200322 u32 parent_handle = 0;
323 u32 key_len, key_handle, err;
324 u8 usage_auth[DIGEST_LENGTH];
325 u8 parent_hash[DIGEST_LENGTH];
mario.six@gdsys.cc0f4b2ba2017-03-20 10:28:28 +0100326 void *key;
327
328 if (argc < 5)
329 return CMD_RET_USAGE;
330
331 parse_byte_string(argv[1], parent_hash, NULL);
332 key = (void *)simple_strtoul(argv[2], NULL, 0);
333 key_len = simple_strtoul(argv[3], NULL, 0);
334 if (strlen(argv[4]) != 2 * DIGEST_LENGTH)
335 return CMD_RET_FAILURE;
336 parse_byte_string(argv[4], usage_auth, NULL);
337
338 err = tpm_find_key_sha1(usage_auth, parent_hash, &parent_handle);
339 if (err) {
340 printf("Could not find matching parent key (err = %d)\n", err);
341 return CMD_RET_FAILURE;
342 }
343
344 printf("Found parent key %08x\n", parent_handle);
345
346 err = tpm_load_key2_oiap(parent_handle, key, key_len, usage_auth,
347 &key_handle);
348 if (!err) {
349 printf("Key handle is 0x%x\n", key_handle);
Simon Glass018f5302017-08-03 12:22:10 -0600350 env_set_hex("key_handle", key_handle);
mario.six@gdsys.cc0f4b2ba2017-03-20 10:28:28 +0100351 }
352
353 return report_return_code(err);
354}
355#endif /* CONFIG_TPM_LOAD_KEY_BY_SHA1 */
356
Miquel Raynalc6179182018-05-15 11:57:00 +0200357static int do_tpm_load_key2_oiap(cmd_tbl_t *cmdtp, int flag, int argc,
358 char * const argv[])
Reinhard Pfaube6c1522013-06-26 15:55:13 +0200359{
Miquel Raynalb9804e52018-05-15 11:56:59 +0200360 u32 parent_handle, key_len, key_handle, err;
361 u8 usage_auth[DIGEST_LENGTH];
Reinhard Pfaube6c1522013-06-26 15:55:13 +0200362 void *key;
363
364 if (argc < 5)
365 return CMD_RET_USAGE;
366
367 parent_handle = simple_strtoul(argv[1], NULL, 0);
368 key = (void *)simple_strtoul(argv[2], NULL, 0);
369 key_len = simple_strtoul(argv[3], NULL, 0);
370 if (strlen(argv[4]) != 2 * DIGEST_LENGTH)
371 return CMD_RET_FAILURE;
372 parse_byte_string(argv[4], usage_auth, NULL);
373
374 err = tpm_load_key2_oiap(parent_handle, key, key_len, usage_auth,
Miquel Raynalc6179182018-05-15 11:57:00 +0200375 &key_handle);
Reinhard Pfaube6c1522013-06-26 15:55:13 +0200376 if (!err)
377 printf("Key handle is 0x%x\n", key_handle);
378
Simon Glassf8f1fe12015-08-22 18:31:34 -0600379 return report_return_code(err);
Reinhard Pfaube6c1522013-06-26 15:55:13 +0200380}
381
Miquel Raynalc6179182018-05-15 11:57:00 +0200382static int do_tpm_get_pub_key_oiap(cmd_tbl_t *cmdtp, int flag, int argc,
383 char * const argv[])
Reinhard Pfaube6c1522013-06-26 15:55:13 +0200384{
Miquel Raynalb9804e52018-05-15 11:56:59 +0200385 u32 key_handle, err;
386 u8 usage_auth[DIGEST_LENGTH];
387 u8 pub_key_buffer[TPM_PUBKEY_MAX_LENGTH];
Reinhard Pfaube6c1522013-06-26 15:55:13 +0200388 size_t pub_key_len = sizeof(pub_key_buffer);
389
390 if (argc < 3)
391 return CMD_RET_USAGE;
392
393 key_handle = simple_strtoul(argv[1], NULL, 0);
394 if (strlen(argv[2]) != 2 * DIGEST_LENGTH)
395 return CMD_RET_FAILURE;
396 parse_byte_string(argv[2], usage_auth, NULL);
397
Miquel Raynalc6179182018-05-15 11:57:00 +0200398 err = tpm_get_pub_key_oiap(key_handle, usage_auth, pub_key_buffer,
399 &pub_key_len);
Reinhard Pfaube6c1522013-06-26 15:55:13 +0200400 if (!err) {
401 printf("dump of received pub key structure:\n");
402 print_byte_string(pub_key_buffer, pub_key_len);
403 }
Simon Glassf8f1fe12015-08-22 18:31:34 -0600404 return report_return_code(err);
Reinhard Pfaube6c1522013-06-26 15:55:13 +0200405}
406
407TPM_COMMAND_NO_ARG(tpm_end_oiap)
408
409#endif /* CONFIG_TPM_AUTH_SESSIONS */
410
Mario Six7690be32017-01-11 16:00:50 +0100411#ifdef CONFIG_TPM_FLUSH_RESOURCES
412static int do_tpm_flush(cmd_tbl_t *cmdtp, int flag, int argc,
413 char * const argv[])
414{
415 int type = 0;
416
mario.six@gdsys.cc1c08b212017-03-20 10:28:29 +0100417 if (argc != 3)
Mario Six7690be32017-01-11 16:00:50 +0100418 return CMD_RET_USAGE;
419
mario.six@gdsys.cc1c08b212017-03-20 10:28:29 +0100420 if (!strcasecmp(argv[1], "key"))
Mario Six7690be32017-01-11 16:00:50 +0100421 type = TPM_RT_KEY;
mario.six@gdsys.cc1c08b212017-03-20 10:28:29 +0100422 else if (!strcasecmp(argv[1], "auth"))
Mario Six7690be32017-01-11 16:00:50 +0100423 type = TPM_RT_AUTH;
mario.six@gdsys.cc1c08b212017-03-20 10:28:29 +0100424 else if (!strcasecmp(argv[1], "hash"))
Mario Six7690be32017-01-11 16:00:50 +0100425 type = TPM_RT_HASH;
mario.six@gdsys.cc1c08b212017-03-20 10:28:29 +0100426 else if (!strcasecmp(argv[1], "trans"))
Mario Six7690be32017-01-11 16:00:50 +0100427 type = TPM_RT_TRANS;
mario.six@gdsys.cc1c08b212017-03-20 10:28:29 +0100428 else if (!strcasecmp(argv[1], "context"))
Mario Six7690be32017-01-11 16:00:50 +0100429 type = TPM_RT_CONTEXT;
mario.six@gdsys.cc1c08b212017-03-20 10:28:29 +0100430 else if (!strcasecmp(argv[1], "counter"))
Mario Six7690be32017-01-11 16:00:50 +0100431 type = TPM_RT_COUNTER;
mario.six@gdsys.cc1c08b212017-03-20 10:28:29 +0100432 else if (!strcasecmp(argv[1], "delegate"))
Mario Six7690be32017-01-11 16:00:50 +0100433 type = TPM_RT_DELEGATE;
mario.six@gdsys.cc1c08b212017-03-20 10:28:29 +0100434 else if (!strcasecmp(argv[1], "daa_tpm"))
Mario Six7690be32017-01-11 16:00:50 +0100435 type = TPM_RT_DAA_TPM;
mario.six@gdsys.cc1c08b212017-03-20 10:28:29 +0100436 else if (!strcasecmp(argv[1], "daa_v0"))
Mario Six7690be32017-01-11 16:00:50 +0100437 type = TPM_RT_DAA_V0;
mario.six@gdsys.cc1c08b212017-03-20 10:28:29 +0100438 else if (!strcasecmp(argv[1], "daa_v1"))
Mario Six7690be32017-01-11 16:00:50 +0100439 type = TPM_RT_DAA_V1;
440
mario.six@gdsys.cc1c08b212017-03-20 10:28:29 +0100441 if (!type) {
442 printf("Resource type %s unknown.\n", argv[1]);
443 return -1;
444 }
445
446 if (!strcasecmp(argv[2], "all")) {
Miquel Raynalb9804e52018-05-15 11:56:59 +0200447 u16 res_count;
448 u8 buf[288];
449 u8 *ptr;
Mario Six7690be32017-01-11 16:00:50 +0100450 int err;
451 uint i;
452
453 /* fetch list of already loaded resources in the TPM */
454 err = tpm_get_capability(TPM_CAP_HANDLE, type, buf,
455 sizeof(buf));
mario.six@gdsys.cc1c08b212017-03-20 10:28:29 +0100456 if (err) {
457 printf("tpm_get_capability returned error %d.\n", err);
Mario Six7690be32017-01-11 16:00:50 +0100458 return -1;
mario.six@gdsys.cc1c08b212017-03-20 10:28:29 +0100459 }
Mario Six7690be32017-01-11 16:00:50 +0100460 res_count = get_unaligned_be16(buf);
461 ptr = buf + 2;
462 for (i = 0; i < res_count; ++i, ptr += 4)
463 tpm_flush_specific(get_unaligned_be32(ptr), type);
464 } else {
Miquel Raynalb9804e52018-05-15 11:56:59 +0200465 u32 handle = simple_strtoul(argv[2], NULL, 0);
Mario Six7690be32017-01-11 16:00:50 +0100466
mario.six@gdsys.cc1c08b212017-03-20 10:28:29 +0100467 if (!handle) {
468 printf("Illegal resource handle %s\n", argv[2]);
Mario Six7690be32017-01-11 16:00:50 +0100469 return -1;
mario.six@gdsys.cc1c08b212017-03-20 10:28:29 +0100470 }
Mario Six7690be32017-01-11 16:00:50 +0100471 tpm_flush_specific(cpu_to_be32(handle), type);
472 }
473
474 return 0;
475}
476#endif /* CONFIG_TPM_FLUSH_RESOURCES */
477
mario.six@gdsys.cc3d1df0e2017-03-20 10:28:30 +0100478#ifdef CONFIG_TPM_LIST_RESOURCES
479static int do_tpm_list(cmd_tbl_t *cmdtp, int flag, int argc,
480 char * const argv[])
481{
482 int type = 0;
Miquel Raynalb9804e52018-05-15 11:56:59 +0200483 u16 res_count;
484 u8 buf[288];
485 u8 *ptr;
mario.six@gdsys.cc3d1df0e2017-03-20 10:28:30 +0100486 int err;
487 uint i;
488
489 if (argc != 2)
490 return CMD_RET_USAGE;
491
492 if (!strcasecmp(argv[1], "key"))
493 type = TPM_RT_KEY;
494 else if (!strcasecmp(argv[1], "auth"))
495 type = TPM_RT_AUTH;
496 else if (!strcasecmp(argv[1], "hash"))
497 type = TPM_RT_HASH;
498 else if (!strcasecmp(argv[1], "trans"))
499 type = TPM_RT_TRANS;
500 else if (!strcasecmp(argv[1], "context"))
501 type = TPM_RT_CONTEXT;
502 else if (!strcasecmp(argv[1], "counter"))
503 type = TPM_RT_COUNTER;
504 else if (!strcasecmp(argv[1], "delegate"))
505 type = TPM_RT_DELEGATE;
506 else if (!strcasecmp(argv[1], "daa_tpm"))
507 type = TPM_RT_DAA_TPM;
508 else if (!strcasecmp(argv[1], "daa_v0"))
509 type = TPM_RT_DAA_V0;
510 else if (!strcasecmp(argv[1], "daa_v1"))
511 type = TPM_RT_DAA_V1;
512
513 if (!type) {
514 printf("Resource type %s unknown.\n", argv[1]);
515 return -1;
516 }
517
518 /* fetch list of already loaded resources in the TPM */
519 err = tpm_get_capability(TPM_CAP_HANDLE, type, buf,
520 sizeof(buf));
521 if (err) {
522 printf("tpm_get_capability returned error %d.\n", err);
523 return -1;
524 }
525 res_count = get_unaligned_be16(buf);
526 ptr = buf + 2;
527
528 printf("Resources of type %s (%02x):\n", argv[1], type);
529 if (!res_count) {
530 puts("None\n");
531 } else {
532 for (i = 0; i < res_count; ++i, ptr += 4)
533 printf("Index %d: %08x\n", i, get_unaligned_be32(ptr));
534 }
535
536 return 0;
537}
538#endif /* CONFIG_TPM_LIST_RESOURCES */
539
Miquel Raynald677bfe2018-05-15 11:57:06 +0200540TPM_COMMAND_NO_ARG(tpm_self_test_full)
541TPM_COMMAND_NO_ARG(tpm_continue_self_test)
542TPM_COMMAND_NO_ARG(tpm_force_clear)
543TPM_COMMAND_NO_ARG(tpm_physical_enable)
544TPM_COMMAND_NO_ARG(tpm_physical_disable)
Che-liang Chiou8732b072013-02-28 09:34:57 +0000545
Miquel Raynald677bfe2018-05-15 11:57:06 +0200546static cmd_tbl_t tpm1_commands[] = {
Simon Glassad776942015-08-22 18:31:40 -0600547 U_BOOT_CMD_MKENT(info, 0, 1, do_tpm_info, "", ""),
Miquel Raynalc6179182018-05-15 11:57:00 +0200548 U_BOOT_CMD_MKENT(init, 0, 1, do_tpm_init, "", ""),
Che-liang Chiou8732b072013-02-28 09:34:57 +0000549 U_BOOT_CMD_MKENT(startup, 0, 1,
Miquel Raynalc6179182018-05-15 11:57:00 +0200550 do_tpm_startup, "", ""),
Che-liang Chiou8732b072013-02-28 09:34:57 +0000551 U_BOOT_CMD_MKENT(self_test_full, 0, 1,
Miquel Raynalc6179182018-05-15 11:57:00 +0200552 do_tpm_self_test_full, "", ""),
Che-liang Chiou8732b072013-02-28 09:34:57 +0000553 U_BOOT_CMD_MKENT(continue_self_test, 0, 1,
Miquel Raynalc6179182018-05-15 11:57:00 +0200554 do_tpm_continue_self_test, "", ""),
Che-liang Chiou8732b072013-02-28 09:34:57 +0000555 U_BOOT_CMD_MKENT(force_clear, 0, 1,
Miquel Raynalc6179182018-05-15 11:57:00 +0200556 do_tpm_force_clear, "", ""),
Che-liang Chiou8732b072013-02-28 09:34:57 +0000557 U_BOOT_CMD_MKENT(physical_enable, 0, 1,
Miquel Raynalc6179182018-05-15 11:57:00 +0200558 do_tpm_physical_enable, "", ""),
Che-liang Chiou8732b072013-02-28 09:34:57 +0000559 U_BOOT_CMD_MKENT(physical_disable, 0, 1,
Miquel Raynalc6179182018-05-15 11:57:00 +0200560 do_tpm_physical_disable, "", ""),
Che-liang Chiou8732b072013-02-28 09:34:57 +0000561 U_BOOT_CMD_MKENT(nv_define_space, 0, 1,
Miquel Raynalc6179182018-05-15 11:57:00 +0200562 do_tpm_nv_define_space, "", ""),
Che-liang Chiou8732b072013-02-28 09:34:57 +0000563 U_BOOT_CMD_MKENT(nv_read_value, 0, 1,
Miquel Raynalc6179182018-05-15 11:57:00 +0200564 do_tpm_nv_read_value, "", ""),
Che-liang Chiou8732b072013-02-28 09:34:57 +0000565 U_BOOT_CMD_MKENT(nv_write_value, 0, 1,
Miquel Raynalc6179182018-05-15 11:57:00 +0200566 do_tpm_nv_write_value, "", ""),
Che-liang Chiou8732b072013-02-28 09:34:57 +0000567 U_BOOT_CMD_MKENT(extend, 0, 1,
Miquel Raynalc6179182018-05-15 11:57:00 +0200568 do_tpm_extend, "", ""),
Che-liang Chiou8732b072013-02-28 09:34:57 +0000569 U_BOOT_CMD_MKENT(pcr_read, 0, 1,
Miquel Raynalc6179182018-05-15 11:57:00 +0200570 do_tpm_pcr_read, "", ""),
Che-liang Chiou8732b072013-02-28 09:34:57 +0000571 U_BOOT_CMD_MKENT(tsc_physical_presence, 0, 1,
Miquel Raynalc6179182018-05-15 11:57:00 +0200572 do_tpm_tsc_physical_presence, "", ""),
Che-liang Chiou8732b072013-02-28 09:34:57 +0000573 U_BOOT_CMD_MKENT(read_pubek, 0, 1,
Miquel Raynalc6179182018-05-15 11:57:00 +0200574 do_tpm_read_pubek, "", ""),
Che-liang Chiou8732b072013-02-28 09:34:57 +0000575 U_BOOT_CMD_MKENT(physical_set_deactivated, 0, 1,
Miquel Raynalc6179182018-05-15 11:57:00 +0200576 do_tpm_physical_set_deactivated, "", ""),
Che-liang Chiou8732b072013-02-28 09:34:57 +0000577 U_BOOT_CMD_MKENT(get_capability, 0, 1,
Miquel Raynalc6179182018-05-15 11:57:00 +0200578 do_tpm_get_capability, "", ""),
Che-liang Chiou8732b072013-02-28 09:34:57 +0000579 U_BOOT_CMD_MKENT(raw_transfer, 0, 1,
Miquel Raynalc6179182018-05-15 11:57:00 +0200580 do_tpm_raw_transfer, "", ""),
Che-liang Chiou8732b072013-02-28 09:34:57 +0000581 U_BOOT_CMD_MKENT(nv_define, 0, 1,
Miquel Raynalc6179182018-05-15 11:57:00 +0200582 do_tpm_nv_define, "", ""),
Che-liang Chiou8732b072013-02-28 09:34:57 +0000583 U_BOOT_CMD_MKENT(nv_read, 0, 1,
Miquel Raynalc6179182018-05-15 11:57:00 +0200584 do_tpm_nv_read, "", ""),
Che-liang Chiou8732b072013-02-28 09:34:57 +0000585 U_BOOT_CMD_MKENT(nv_write, 0, 1,
Miquel Raynalc6179182018-05-15 11:57:00 +0200586 do_tpm_nv_write, "", ""),
Reinhard Pfaube6c1522013-06-26 15:55:13 +0200587#ifdef CONFIG_TPM_AUTH_SESSIONS
588 U_BOOT_CMD_MKENT(oiap, 0, 1,
589 do_tpm_oiap, "", ""),
590 U_BOOT_CMD_MKENT(end_oiap, 0, 1,
591 do_tpm_end_oiap, "", ""),
592 U_BOOT_CMD_MKENT(load_key2_oiap, 0, 1,
593 do_tpm_load_key2_oiap, "", ""),
mario.six@gdsys.cc0f4b2ba2017-03-20 10:28:28 +0100594#ifdef CONFIG_TPM_LOAD_KEY_BY_SHA1
595 U_BOOT_CMD_MKENT(load_key_by_sha1, 0, 1,
596 do_tpm_load_key_by_sha1, "", ""),
597#endif /* CONFIG_TPM_LOAD_KEY_BY_SHA1 */
Reinhard Pfaube6c1522013-06-26 15:55:13 +0200598 U_BOOT_CMD_MKENT(get_pub_key_oiap, 0, 1,
599 do_tpm_get_pub_key_oiap, "", ""),
600#endif /* CONFIG_TPM_AUTH_SESSIONS */
Mario Six7690be32017-01-11 16:00:50 +0100601#ifdef CONFIG_TPM_FLUSH_RESOURCES
602 U_BOOT_CMD_MKENT(flush, 0, 1,
603 do_tpm_flush, "", ""),
604#endif /* CONFIG_TPM_FLUSH_RESOURCES */
mario.six@gdsys.cc3d1df0e2017-03-20 10:28:30 +0100605#ifdef CONFIG_TPM_LIST_RESOURCES
606 U_BOOT_CMD_MKENT(list, 0, 1,
607 do_tpm_list, "", ""),
608#endif /* CONFIG_TPM_LIST_RESOURCES */
Che-liang Chiou8732b072013-02-28 09:34:57 +0000609};
Luigi Semenzatoeea3f4d2012-12-05 14:46:44 +0000610
Miquel Raynald677bfe2018-05-15 11:57:06 +0200611cmd_tbl_t *get_tpm_commands(unsigned int *size)
Luigi Semenzatoeea3f4d2012-12-05 14:46:44 +0000612{
Miquel Raynald677bfe2018-05-15 11:57:06 +0200613 *size = ARRAY_SIZE(tpm1_commands);
Che-liang Chiou8732b072013-02-28 09:34:57 +0000614
Miquel Raynald677bfe2018-05-15 11:57:06 +0200615 return tpm1_commands;
Luigi Semenzatoeea3f4d2012-12-05 14:46:44 +0000616}
617
Che-liang Chiou8732b072013-02-28 09:34:57 +0000618U_BOOT_CMD(tpm, CONFIG_SYS_MAXARGS, 1, do_tpm,
Miquel Raynald677bfe2018-05-15 11:57:06 +0200619"Issue a TPMv1.x command",
Che-liang Chiou8732b072013-02-28 09:34:57 +0000620"cmd args...\n"
621" - Issue TPM command <cmd> with arguments <args...>.\n"
622"Admin Startup and State Commands:\n"
Simon Glassad776942015-08-22 18:31:40 -0600623" info - Show information about the TPM\n"
Che-liang Chiou8732b072013-02-28 09:34:57 +0000624" init\n"
625" - Put TPM into a state where it waits for 'startup' command.\n"
626" startup mode\n"
627" - Issue TPM_Starup command. <mode> is one of TPM_ST_CLEAR,\n"
628" TPM_ST_STATE, and TPM_ST_DEACTIVATED.\n"
629"Admin Testing Commands:\n"
630" self_test_full\n"
631" - Test all of the TPM capabilities.\n"
632" continue_self_test\n"
633" - Inform TPM that it should complete the self-test.\n"
634"Admin Opt-in Commands:\n"
635" physical_enable\n"
636" - Set the PERMANENT disable flag to FALSE using physical presence as\n"
637" authorization.\n"
638" physical_disable\n"
639" - Set the PERMANENT disable flag to TRUE using physical presence as\n"
640" authorization.\n"
641" physical_set_deactivated 0|1\n"
642" - Set deactivated flag.\n"
643"Admin Ownership Commands:\n"
644" force_clear\n"
645" - Issue TPM_ForceClear command.\n"
646" tsc_physical_presence flags\n"
647" - Set TPM device's Physical Presence flags to <flags>.\n"
648"The Capability Commands:\n"
649" get_capability cap_area sub_cap addr count\n"
650" - Read <count> bytes of TPM capability indexed by <cap_area> and\n"
651" <sub_cap> to memory address <addr>.\n"
mario.six@gdsys.cc3d1df0e2017-03-20 10:28:30 +0100652#if defined(CONFIG_TPM_FLUSH_RESOURCES) || defined(CONFIG_TPM_LIST_RESOURCES)
Mario Six7690be32017-01-11 16:00:50 +0100653"Resource management functions\n"
mario.six@gdsys.cc3d1df0e2017-03-20 10:28:30 +0100654#endif
655#ifdef CONFIG_TPM_FLUSH_RESOURCES
Mario Six7690be32017-01-11 16:00:50 +0100656" flush resource_type id\n"
657" - flushes a resource of type <resource_type> (may be one of key, auth,\n"
658" hash, trans, context, counter, delegate, daa_tpm, daa_v0, daa_v1),\n"
659" and id <id> from the TPM. Use an <id> of \"all\" to flush all\n"
660" resources of that type.\n"
661#endif /* CONFIG_TPM_FLUSH_RESOURCES */
mario.six@gdsys.cc3d1df0e2017-03-20 10:28:30 +0100662#ifdef CONFIG_TPM_LIST_RESOURCES
663" list resource_type\n"
664" - lists resources of type <resource_type> (may be one of key, auth,\n"
665" hash, trans, context, counter, delegate, daa_tpm, daa_v0, daa_v1),\n"
666" contained in the TPM.\n"
667#endif /* CONFIG_TPM_LIST_RESOURCES */
Reinhard Pfaube6c1522013-06-26 15:55:13 +0200668#ifdef CONFIG_TPM_AUTH_SESSIONS
669"Storage functions\n"
670" loadkey2_oiap parent_handle key_addr key_len usage_auth\n"
671" - loads a key data from memory address <key_addr>, <key_len> bytes\n"
672" into TPM using the parent key <parent_handle> with authorization\n"
673" <usage_auth> (20 bytes hex string).\n"
mario.six@gdsys.cc0f4b2ba2017-03-20 10:28:28 +0100674#ifdef CONFIG_TPM_LOAD_KEY_BY_SHA1
675" load_key_by_sha1 parent_hash key_addr key_len usage_auth\n"
676" - loads a key data from memory address <key_addr>, <key_len> bytes\n"
677" into TPM using the parent hash <parent_hash> (20 bytes hex string)\n"
678" with authorization <usage_auth> (20 bytes hex string).\n"
679#endif /* CONFIG_TPM_LOAD_KEY_BY_SHA1 */
Reinhard Pfaube6c1522013-06-26 15:55:13 +0200680" get_pub_key_oiap key_handle usage_auth\n"
681" - get the public key portion of a loaded key <key_handle> using\n"
682" authorization <usage auth> (20 bytes hex string)\n"
683#endif /* CONFIG_TPM_AUTH_SESSIONS */
Che-liang Chiou8732b072013-02-28 09:34:57 +0000684"Endorsement Key Handling Commands:\n"
685" read_pubek addr count\n"
686" - Read <count> bytes of the public endorsement key to memory\n"
687" address <addr>\n"
688"Integrity Collection and Reporting Commands:\n"
689" extend index digest_hex_string\n"
690" - Add a new measurement to a PCR. Update PCR <index> with the 20-bytes\n"
691" <digest_hex_string>\n"
692" pcr_read index addr count\n"
693" - Read <count> bytes from PCR <index> to memory address <addr>.\n"
Reinhard Pfaube6c1522013-06-26 15:55:13 +0200694#ifdef CONFIG_TPM_AUTH_SESSIONS
695"Authorization Sessions\n"
696" oiap\n"
697" - setup an OIAP session\n"
698" end_oiap\n"
699" - terminates an active OIAP session\n"
700#endif /* CONFIG_TPM_AUTH_SESSIONS */
Che-liang Chiou8732b072013-02-28 09:34:57 +0000701"Non-volatile Storage Commands:\n"
702" nv_define_space index permission size\n"
703" - Establish a space at index <index> with <permission> of <size> bytes.\n"
704" nv_read_value index addr count\n"
705" - Read <count> bytes from space <index> to memory address <addr>.\n"
706" nv_write_value index addr count\n"
707" - Write <count> bytes from memory address <addr> to space <index>.\n"
708"Miscellaneous helper functions:\n"
709" raw_transfer byte_string\n"
710" - Send a byte string <byte_string> to TPM and print the response.\n"
711" Non-volatile storage helper functions:\n"
712" These helper functions treat a non-volatile space as a non-padded\n"
713" sequence of integer values. These integer values are defined by a type\n"
714" string, which is a text string of 'bwd' characters: 'b' means a 8-bit\n"
715" value, 'w' 16-bit value, 'd' 32-bit value. All helper functions take\n"
716" a type string as their first argument.\n"
717" nv_define type_string index perm\n"
718" - Define a space <index> with permission <perm>.\n"
719" nv_read types_string index vars...\n"
720" - Read from space <index> to environment variables <vars...>.\n"
721" nv_write types_string index values...\n"
722" - Write to space <index> from values <values...>.\n"
Luigi Semenzatoeea3f4d2012-12-05 14:46:44 +0000723);