]> git.ipfire.org Git - thirdparty/u-boot.git/blame - cmd/tpm.c
tpm: align arguments with open parenthesis
[thirdparty/u-boot.git] / cmd / tpm.c
CommitLineData
83d290c5 1// SPDX-License-Identifier: GPL-2.0+
576fb1ed 2/*
8732b070 3 * Copyright (c) 2013 The Chromium OS Authors.
576fb1ed
VB
4 */
5
6#include <common.h>
7#include <command.h>
c8a8c510 8#include <dm.h>
8732b070 9#include <malloc.h>
576fb1ed 10#include <tpm.h>
8732b070
CC
11#include <asm/unaligned.h>
12#include <linux/string.h>
576fb1ed 13
be6c1529
RP
14/* Useful constants */
15enum {
16 DIGEST_LENGTH = 20,
17 /* max lengths, valid for RSA keys <= 2048 bits */
18 TPM_PUBKEY_MAX_LENGTH = 288,
19};
20
8732b070
CC
21/**
22 * Print a byte string in hexdecimal format, 16-bytes per line.
23 *
24 * @param data byte string to be printed
25 * @param count number of bytes to be printed
26 */
b9804e5b 27static void print_byte_string(u8 *data, size_t count)
8732b070
CC
28{
29 int i, print_newline = 0;
576fb1ed 30
8732b070
CC
31 for (i = 0; i < count; i++) {
32 printf(" %02x", data[i]);
33 print_newline = (i % 16 == 15);
34 if (print_newline)
35 putc('\n');
36 }
37 /* Avoid duplicated newline at the end */
38 if (!print_newline)
39 putc('\n');
40}
41
42/**
43 * Convert a text string of hexdecimal values into a byte string.
576fb1ed 44 *
8732b070
CC
45 * @param bytes text string of hexdecimal values with no space
46 * between them
47 * @param data output buffer for byte string. The caller has to make
48 * sure it is large enough for storing the output. If
49 * NULL is passed, a large enough buffer will be allocated,
50 * and the caller must free it.
51 * @param count_ptr output variable for the length of byte string
52 * @return pointer to output buffer
576fb1ed 53 */
b9804e5b 54static void *parse_byte_string(char *bytes, u8 *data, size_t *count_ptr)
8732b070
CC
55{
56 char byte[3];
57 size_t count, length;
58 int i;
59
5c51d8aa
SG
60 if (!bytes)
61 return NULL;
8732b070
CC
62 length = strlen(bytes);
63 count = length / 2;
64
65 if (!data)
66 data = malloc(count);
67 if (!data)
68 return NULL;
69
70 byte[2] = '\0';
71 for (i = 0; i < length; i += 2) {
72 byte[0] = bytes[i];
73 byte[1] = bytes[i + 1];
b9804e5b 74 data[i / 2] = (u8)simple_strtoul(byte, NULL, 16);
576fb1ed
VB
75 }
76
8732b070
CC
77 if (count_ptr)
78 *count_ptr = count;
79
80 return data;
81}
82
83/**
f8f1fe1d 84 * report_return_code() - Report any error and return failure or success
8732b070
CC
85 *
86 * @param return_code TPM command return code
87 * @return value of enum command_ret_t
88 */
f8f1fe1d 89static int report_return_code(int return_code)
8732b070 90{
f8f1fe1d
SG
91 if (return_code) {
92 printf("Error: %d\n", return_code);
8732b070 93 return CMD_RET_FAILURE;
f8f1fe1d 94 } else {
8732b070 95 return CMD_RET_SUCCESS;
f8f1fe1d 96 }
8732b070
CC
97}
98
99/**
100 * Return number of values defined by a type string.
101 *
102 * @param type_str type string
103 * @return number of values of type string
104 */
105static int type_string_get_num_values(const char *type_str)
106{
107 return strlen(type_str);
108}
109
110/**
111 * Return total size of values defined by a type string.
112 *
113 * @param type_str type string
114 * @return total size of values of type string, or 0 if type string
115 * contains illegal type character.
116 */
117static size_t type_string_get_space_size(const char *type_str)
118{
119 size_t size;
120
121 for (size = 0; *type_str; type_str++) {
122 switch (*type_str) {
123 case 'b':
124 size += 1;
125 break;
126 case 'w':
127 size += 2;
128 break;
129 case 'd':
130 size += 4;
131 break;
132 default:
133 return 0;
134 }
576fb1ed 135 }
8732b070
CC
136
137 return size;
576fb1ed
VB
138}
139
8732b070
CC
140/**
141 * Allocate a buffer large enough to hold values defined by a type
142 * string. The caller has to free the buffer.
143 *
144 * @param type_str type string
145 * @param count pointer for storing size of buffer
146 * @return pointer to buffer or NULL on error
147 */
b9804e5b 148static void *type_string_alloc(const char *type_str, u32 *count)
8732b070
CC
149{
150 void *data;
151 size_t size;
152
153 size = type_string_get_space_size(type_str);
154 if (!size)
155 return NULL;
156 data = malloc(size);
157 if (data)
158 *count = size;
eea3f4d3 159
8732b070
CC
160 return data;
161}
162
163/**
164 * Pack values defined by a type string into a buffer. The buffer must have
165 * large enough space.
166 *
167 * @param type_str type string
168 * @param values text strings of values to be packed
169 * @param data output buffer of values
170 * @return 0 on success, non-0 on error
171 */
172static int type_string_pack(const char *type_str, char * const values[],
b9804e5b 173 u8 *data)
576fb1ed 174{
8732b070 175 size_t offset;
b9804e5b 176 u32 value;
8732b070
CC
177
178 for (offset = 0; *type_str; type_str++, values++) {
179 value = simple_strtoul(values[0], NULL, 0);
180 switch (*type_str) {
181 case 'b':
182 data[offset] = value;
183 offset += 1;
184 break;
185 case 'w':
186 put_unaligned_be16(value, data + offset);
187 offset += 2;
188 break;
189 case 'd':
190 put_unaligned_be32(value, data + offset);
191 offset += 4;
eea3f4d3 192 break;
8732b070
CC
193 default:
194 return -1;
eea3f4d3 195 }
8732b070
CC
196 }
197
198 return 0;
199}
200
201/**
202 * Read values defined by a type string from a buffer, and write these values
203 * to environment variables.
204 *
205 * @param type_str type string
206 * @param data input buffer of values
207 * @param vars names of environment variables
208 * @return 0 on success, non-0 on error
209 */
b9804e5b
MR
210static int type_string_write_vars(const char *type_str, u8 *data,
211 char * const vars[])
8732b070
CC
212{
213 size_t offset;
b9804e5b 214 u32 value;
8732b070
CC
215
216 for (offset = 0; *type_str; type_str++, vars++) {
217 switch (*type_str) {
218 case 'b':
219 value = data[offset];
220 offset += 1;
221 break;
222 case 'w':
223 value = get_unaligned_be16(data + offset);
224 offset += 2;
eea3f4d3 225 break;
8732b070
CC
226 case 'd':
227 value = get_unaligned_be32(data + offset);
228 offset += 4;
229 break;
230 default:
231 return -1;
eea3f4d3 232 }
018f5303 233 if (env_set_ulong(*vars, value))
8732b070 234 return -1;
eea3f4d3 235 }
8732b070
CC
236
237 return 0;
eea3f4d3 238}
576fb1ed 239
c6179187
MR
240static int do_tpm_startup(cmd_tbl_t *cmdtp, int flag, int argc,
241 char * const argv[])
8732b070
CC
242{
243 enum tpm_startup_type mode;
eea3f4d3 244
8732b070
CC
245 if (argc != 2)
246 return CMD_RET_USAGE;
247 if (!strcasecmp("TPM_ST_CLEAR", argv[1])) {
248 mode = TPM_ST_CLEAR;
249 } else if (!strcasecmp("TPM_ST_STATE", argv[1])) {
250 mode = TPM_ST_STATE;
251 } else if (!strcasecmp("TPM_ST_DEACTIVATED", argv[1])) {
252 mode = TPM_ST_DEACTIVATED;
253 } else {
254 printf("Couldn't recognize mode string: %s\n", argv[1]);
255 return CMD_RET_FAILURE;
256 }
257
f8f1fe1d 258 return report_return_code(tpm_startup(mode));
8732b070
CC
259}
260
c6179187
MR
261static int do_tpm_nv_define_space(cmd_tbl_t *cmdtp, int flag, int argc,
262 char * const argv[])
8732b070 263{
b9804e5b 264 u32 index, perm, size;
8732b070
CC
265
266 if (argc != 4)
267 return CMD_RET_USAGE;
268 index = simple_strtoul(argv[1], NULL, 0);
269 perm = simple_strtoul(argv[2], NULL, 0);
270 size = simple_strtoul(argv[3], NULL, 0);
271
f8f1fe1d 272 return report_return_code(tpm_nv_define_space(index, perm, size));
8732b070 273}
eea3f4d3 274
c6179187
MR
275static int do_tpm_nv_read_value(cmd_tbl_t *cmdtp, int flag, int argc,
276 char * const argv[])
eea3f4d3 277{
b9804e5b 278 u32 index, count, rc;
8732b070 279 void *data;
eea3f4d3 280
8732b070
CC
281 if (argc != 4)
282 return CMD_RET_USAGE;
283 index = simple_strtoul(argv[1], NULL, 0);
284 data = (void *)simple_strtoul(argv[2], NULL, 0);
285 count = simple_strtoul(argv[3], NULL, 0);
286
287 rc = tpm_nv_read_value(index, data, count);
288 if (!rc) {
289 puts("area content:\n");
290 print_byte_string(data, count);
576fb1ed
VB
291 }
292
f8f1fe1d 293 return report_return_code(rc);
8732b070
CC
294}
295
c6179187
MR
296static int do_tpm_nv_write_value(cmd_tbl_t *cmdtp, int flag, int argc,
297 char * const argv[])
8732b070 298{
b9804e5b 299 u32 index, rc;
8732b070
CC
300 size_t count;
301 void *data;
302
303 if (argc != 3)
304 return CMD_RET_USAGE;
305 index = simple_strtoul(argv[1], NULL, 0);
306 data = parse_byte_string(argv[2], NULL, &count);
307 if (!data) {
308 printf("Couldn't parse byte string %s\n", argv[2]);
309 return CMD_RET_FAILURE;
eea3f4d3
LS
310 }
311
8732b070
CC
312 rc = tpm_nv_write_value(index, data, count);
313 free(data);
314
f8f1fe1d 315 return report_return_code(rc);
8732b070
CC
316}
317
c6179187
MR
318static int do_tpm_extend(cmd_tbl_t *cmdtp, int flag, int argc,
319 char * const argv[])
8732b070 320{
b9804e5b
MR
321 u32 index, rc;
322 u8 in_digest[20], out_digest[20];
8732b070
CC
323
324 if (argc != 3)
325 return CMD_RET_USAGE;
326 index = simple_strtoul(argv[1], NULL, 0);
327 if (!parse_byte_string(argv[2], in_digest, NULL)) {
328 printf("Couldn't parse byte string %s\n", argv[2]);
329 return CMD_RET_FAILURE;
576fb1ed
VB
330 }
331
8732b070
CC
332 rc = tpm_extend(index, in_digest, out_digest);
333 if (!rc) {
334 puts("PCR value after execution of the command:\n");
335 print_byte_string(out_digest, sizeof(out_digest));
576fb1ed
VB
336 }
337
f8f1fe1d 338 return report_return_code(rc);
8732b070
CC
339}
340
c6179187
MR
341static int do_tpm_pcr_read(cmd_tbl_t *cmdtp, int flag, int argc,
342 char * const argv[])
8732b070 343{
b9804e5b 344 u32 index, count, rc;
8732b070
CC
345 void *data;
346
347 if (argc != 4)
348 return CMD_RET_USAGE;
349 index = simple_strtoul(argv[1], NULL, 0);
350 data = (void *)simple_strtoul(argv[2], NULL, 0);
351 count = simple_strtoul(argv[3], NULL, 0);
576fb1ed 352
8732b070
CC
353 rc = tpm_pcr_read(index, data, count);
354 if (!rc) {
355 puts("Named PCR content:\n");
356 print_byte_string(data, count);
576fb1ed
VB
357 }
358
f8f1fe1d 359 return report_return_code(rc);
576fb1ed
VB
360}
361
c6179187
MR
362static int do_tpm_tsc_physical_presence(cmd_tbl_t *cmdtp, int flag, int argc,
363 char * const argv[])
8732b070 364{
b9804e5b 365 u16 presence;
8732b070
CC
366
367 if (argc != 2)
368 return CMD_RET_USAGE;
b9804e5b 369 presence = (u16)simple_strtoul(argv[1], NULL, 0);
eea3f4d3 370
f8f1fe1d 371 return report_return_code(tpm_tsc_physical_presence(presence));
8732b070
CC
372}
373
c6179187
MR
374static int do_tpm_read_pubek(cmd_tbl_t *cmdtp, int flag, int argc,
375 char * const argv[])
eea3f4d3 376{
b9804e5b 377 u32 count, rc;
8732b070
CC
378 void *data;
379
380 if (argc != 3)
381 return CMD_RET_USAGE;
382 data = (void *)simple_strtoul(argv[1], NULL, 0);
383 count = simple_strtoul(argv[2], NULL, 0);
384
385 rc = tpm_read_pubek(data, count);
386 if (!rc) {
387 puts("pubek value:\n");
388 print_byte_string(data, count);
389 }
390
f8f1fe1d 391 return report_return_code(rc);
eea3f4d3
LS
392}
393
c6179187
MR
394static int do_tpm_physical_set_deactivated(cmd_tbl_t *cmdtp, int flag, int argc,
395 char * const argv[])
8732b070 396{
b9804e5b 397 u8 state;
8732b070
CC
398
399 if (argc != 2)
400 return CMD_RET_USAGE;
b9804e5b 401 state = (u8)simple_strtoul(argv[1], NULL, 0);
eea3f4d3 402
f8f1fe1d 403 return report_return_code(tpm_physical_set_deactivated(state));
8732b070
CC
404}
405
c6179187
MR
406static int do_tpm_get_capability(cmd_tbl_t *cmdtp, int flag, int argc,
407 char * const argv[])
8732b070 408{
b9804e5b 409 u32 cap_area, sub_cap, rc;
8732b070
CC
410 void *cap;
411 size_t count;
412
413 if (argc != 5)
414 return CMD_RET_USAGE;
415 cap_area = simple_strtoul(argv[1], NULL, 0);
416 sub_cap = simple_strtoul(argv[2], NULL, 0);
417 cap = (void *)simple_strtoul(argv[3], NULL, 0);
418 count = simple_strtoul(argv[4], NULL, 0);
419
420 rc = tpm_get_capability(cap_area, sub_cap, cap, count);
421 if (!rc) {
422 puts("capability information:\n");
423 print_byte_string(cap, count);
424 }
425
f8f1fe1d 426 return report_return_code(rc);
8732b070 427}
eea3f4d3 428
8732b070
CC
429#define TPM_COMMAND_NO_ARG(cmd) \
430static int do_##cmd(cmd_tbl_t *cmdtp, int flag, \
c6179187 431 int argc, char * const argv[]) \
8732b070
CC
432{ \
433 if (argc != 1) \
434 return CMD_RET_USAGE; \
f8f1fe1d 435 return report_return_code(cmd()); \
8732b070
CC
436}
437
438TPM_COMMAND_NO_ARG(tpm_init)
439TPM_COMMAND_NO_ARG(tpm_self_test_full)
440TPM_COMMAND_NO_ARG(tpm_continue_self_test)
441TPM_COMMAND_NO_ARG(tpm_force_clear)
442TPM_COMMAND_NO_ARG(tpm_physical_enable)
443TPM_COMMAND_NO_ARG(tpm_physical_disable)
444
c8a8c510
SG
445static int get_tpm(struct udevice **devp)
446{
447 int rc;
448
3f603cbb
SG
449 rc = uclass_first_device_err(UCLASS_TPM, devp);
450 if (rc) {
c8a8c510
SG
451 printf("Could not find TPM (ret=%d)\n", rc);
452 return CMD_RET_FAILURE;
453 }
454
455 return 0;
456}
ad77694e 457
c6179187 458static int do_tpm_info(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[])
ad77694e
SG
459{
460 struct udevice *dev;
461 char buf[80];
462 int rc;
463
464 rc = get_tpm(&dev);
465 if (rc)
466 return rc;
467 rc = tpm_get_desc(dev, buf, sizeof(buf));
468 if (rc < 0) {
469 printf("Couldn't get TPM info (%d)\n", rc);
470 return CMD_RET_FAILURE;
471 }
472 printf("%s\n", buf);
473
474 return 0;
475}
c8a8c510 476
c6179187
MR
477static int do_tpm_raw_transfer(cmd_tbl_t *cmdtp, int flag, int argc,
478 char * const argv[])
8732b070 479{
c2b0f600 480 struct udevice *dev;
8732b070 481 void *command;
b9804e5b 482 u8 response[1024];
8732b070 483 size_t count, response_length = sizeof(response);
b9804e5b 484 u32 rc;
8732b070
CC
485
486 command = parse_byte_string(argv[1], NULL, &count);
487 if (!command) {
488 printf("Couldn't parse byte string %s\n", argv[1]);
489 return CMD_RET_FAILURE;
490 }
491
c8a8c510
SG
492 rc = get_tpm(&dev);
493 if (rc)
494 return rc;
495
496 rc = tpm_xfer(dev, command, count, response, &response_length);
8732b070
CC
497 free(command);
498 if (!rc) {
499 puts("tpm response:\n");
500 print_byte_string(response, response_length);
501 }
502
f8f1fe1d 503 return report_return_code(rc);
8732b070
CC
504}
505
c6179187
MR
506static int do_tpm_nv_define(cmd_tbl_t *cmdtp, int flag, int argc,
507 char * const argv[])
8732b070 508{
b9804e5b 509 u32 index, perm, size;
8732b070
CC
510
511 if (argc != 4)
512 return CMD_RET_USAGE;
513 size = type_string_get_space_size(argv[1]);
514 if (!size) {
515 printf("Couldn't parse arguments\n");
516 return CMD_RET_USAGE;
517 }
518 index = simple_strtoul(argv[2], NULL, 0);
519 perm = simple_strtoul(argv[3], NULL, 0);
520
f8f1fe1d 521 return report_return_code(tpm_nv_define_space(index, perm, size));
8732b070
CC
522}
523
c6179187
MR
524static int do_tpm_nv_read(cmd_tbl_t *cmdtp, int flag, int argc,
525 char * const argv[])
eea3f4d3 526{
b9804e5b 527 u32 index, count, err;
8732b070 528 void *data;
eea3f4d3 529
8732b070
CC
530 if (argc < 3)
531 return CMD_RET_USAGE;
532 if (argc != 3 + type_string_get_num_values(argv[1]))
533 return CMD_RET_USAGE;
534 index = simple_strtoul(argv[2], NULL, 0);
535 data = type_string_alloc(argv[1], &count);
536 if (!data) {
537 printf("Couldn't parse arguments\n");
538 return CMD_RET_USAGE;
eea3f4d3
LS
539 }
540
8732b070
CC
541 err = tpm_nv_read_value(index, data, count);
542 if (!err) {
543 if (type_string_write_vars(argv[1], data, argv + 3)) {
544 printf("Couldn't write to variables\n");
545 err = ~0;
546 }
eea3f4d3 547 }
8732b070
CC
548 free(data);
549
f8f1fe1d 550 return report_return_code(err);
8732b070
CC
551}
552
c6179187
MR
553static int do_tpm_nv_write(cmd_tbl_t *cmdtp, int flag, int argc,
554 char * const argv[])
8732b070 555{
b9804e5b 556 u32 index, count, err;
8732b070
CC
557 void *data;
558
559 if (argc < 3)
560 return CMD_RET_USAGE;
561 if (argc != 3 + type_string_get_num_values(argv[1]))
562 return CMD_RET_USAGE;
563 index = simple_strtoul(argv[2], NULL, 0);
564 data = type_string_alloc(argv[1], &count);
565 if (!data) {
566 printf("Couldn't parse arguments\n");
567 return CMD_RET_USAGE;
568 }
569 if (type_string_pack(argv[1], argv + 3, data)) {
570 printf("Couldn't parse arguments\n");
571 free(data);
572 return CMD_RET_USAGE;
573 }
574
575 err = tpm_nv_write_value(index, data, count);
576 free(data);
577
f8f1fe1d 578 return report_return_code(err);
8732b070
CC
579}
580
be6c1529
RP
581#ifdef CONFIG_TPM_AUTH_SESSIONS
582
c6179187
MR
583static int do_tpm_oiap(cmd_tbl_t *cmdtp, int flag, int argc,
584 char * const argv[])
be6c1529 585{
b9804e5b 586 u32 auth_handle, err;
be6c1529
RP
587
588 err = tpm_oiap(&auth_handle);
589
f8f1fe1d 590 return report_return_code(err);
be6c1529
RP
591}
592
0f4b2ba1 593#ifdef CONFIG_TPM_LOAD_KEY_BY_SHA1
594static int do_tpm_load_key_by_sha1(cmd_tbl_t *cmdtp, int flag, int argc, char *
595 const argv[])
596{
b9804e5b
MR
597 u32 parent_handle = 0;
598 u32 key_len, key_handle, err;
599 u8 usage_auth[DIGEST_LENGTH];
600 u8 parent_hash[DIGEST_LENGTH];
0f4b2ba1 601 void *key;
602
603 if (argc < 5)
604 return CMD_RET_USAGE;
605
606 parse_byte_string(argv[1], parent_hash, NULL);
607 key = (void *)simple_strtoul(argv[2], NULL, 0);
608 key_len = simple_strtoul(argv[3], NULL, 0);
609 if (strlen(argv[4]) != 2 * DIGEST_LENGTH)
610 return CMD_RET_FAILURE;
611 parse_byte_string(argv[4], usage_auth, NULL);
612
613 err = tpm_find_key_sha1(usage_auth, parent_hash, &parent_handle);
614 if (err) {
615 printf("Could not find matching parent key (err = %d)\n", err);
616 return CMD_RET_FAILURE;
617 }
618
619 printf("Found parent key %08x\n", parent_handle);
620
621 err = tpm_load_key2_oiap(parent_handle, key, key_len, usage_auth,
622 &key_handle);
623 if (!err) {
624 printf("Key handle is 0x%x\n", key_handle);
018f5303 625 env_set_hex("key_handle", key_handle);
0f4b2ba1 626 }
627
628 return report_return_code(err);
629}
630#endif /* CONFIG_TPM_LOAD_KEY_BY_SHA1 */
631
c6179187
MR
632static int do_tpm_load_key2_oiap(cmd_tbl_t *cmdtp, int flag, int argc,
633 char * const argv[])
be6c1529 634{
b9804e5b
MR
635 u32 parent_handle, key_len, key_handle, err;
636 u8 usage_auth[DIGEST_LENGTH];
be6c1529
RP
637 void *key;
638
639 if (argc < 5)
640 return CMD_RET_USAGE;
641
642 parent_handle = simple_strtoul(argv[1], NULL, 0);
643 key = (void *)simple_strtoul(argv[2], NULL, 0);
644 key_len = simple_strtoul(argv[3], NULL, 0);
645 if (strlen(argv[4]) != 2 * DIGEST_LENGTH)
646 return CMD_RET_FAILURE;
647 parse_byte_string(argv[4], usage_auth, NULL);
648
649 err = tpm_load_key2_oiap(parent_handle, key, key_len, usage_auth,
c6179187 650 &key_handle);
be6c1529
RP
651 if (!err)
652 printf("Key handle is 0x%x\n", key_handle);
653
f8f1fe1d 654 return report_return_code(err);
be6c1529
RP
655}
656
c6179187
MR
657static int do_tpm_get_pub_key_oiap(cmd_tbl_t *cmdtp, int flag, int argc,
658 char * const argv[])
be6c1529 659{
b9804e5b
MR
660 u32 key_handle, err;
661 u8 usage_auth[DIGEST_LENGTH];
662 u8 pub_key_buffer[TPM_PUBKEY_MAX_LENGTH];
be6c1529
RP
663 size_t pub_key_len = sizeof(pub_key_buffer);
664
665 if (argc < 3)
666 return CMD_RET_USAGE;
667
668 key_handle = simple_strtoul(argv[1], NULL, 0);
669 if (strlen(argv[2]) != 2 * DIGEST_LENGTH)
670 return CMD_RET_FAILURE;
671 parse_byte_string(argv[2], usage_auth, NULL);
672
c6179187
MR
673 err = tpm_get_pub_key_oiap(key_handle, usage_auth, pub_key_buffer,
674 &pub_key_len);
be6c1529
RP
675 if (!err) {
676 printf("dump of received pub key structure:\n");
677 print_byte_string(pub_key_buffer, pub_key_len);
678 }
f8f1fe1d 679 return report_return_code(err);
be6c1529
RP
680}
681
682TPM_COMMAND_NO_ARG(tpm_end_oiap)
683
684#endif /* CONFIG_TPM_AUTH_SESSIONS */
685
7690be35
MS
686#ifdef CONFIG_TPM_FLUSH_RESOURCES
687static int do_tpm_flush(cmd_tbl_t *cmdtp, int flag, int argc,
688 char * const argv[])
689{
690 int type = 0;
691
1c08b210 692 if (argc != 3)
7690be35
MS
693 return CMD_RET_USAGE;
694
1c08b210 695 if (!strcasecmp(argv[1], "key"))
7690be35 696 type = TPM_RT_KEY;
1c08b210 697 else if (!strcasecmp(argv[1], "auth"))
7690be35 698 type = TPM_RT_AUTH;
1c08b210 699 else if (!strcasecmp(argv[1], "hash"))
7690be35 700 type = TPM_RT_HASH;
1c08b210 701 else if (!strcasecmp(argv[1], "trans"))
7690be35 702 type = TPM_RT_TRANS;
1c08b210 703 else if (!strcasecmp(argv[1], "context"))
7690be35 704 type = TPM_RT_CONTEXT;
1c08b210 705 else if (!strcasecmp(argv[1], "counter"))
7690be35 706 type = TPM_RT_COUNTER;
1c08b210 707 else if (!strcasecmp(argv[1], "delegate"))
7690be35 708 type = TPM_RT_DELEGATE;
1c08b210 709 else if (!strcasecmp(argv[1], "daa_tpm"))
7690be35 710 type = TPM_RT_DAA_TPM;
1c08b210 711 else if (!strcasecmp(argv[1], "daa_v0"))
7690be35 712 type = TPM_RT_DAA_V0;
1c08b210 713 else if (!strcasecmp(argv[1], "daa_v1"))
7690be35
MS
714 type = TPM_RT_DAA_V1;
715
1c08b210 716 if (!type) {
717 printf("Resource type %s unknown.\n", argv[1]);
718 return -1;
719 }
720
721 if (!strcasecmp(argv[2], "all")) {
b9804e5b
MR
722 u16 res_count;
723 u8 buf[288];
724 u8 *ptr;
7690be35
MS
725 int err;
726 uint i;
727
728 /* fetch list of already loaded resources in the TPM */
729 err = tpm_get_capability(TPM_CAP_HANDLE, type, buf,
730 sizeof(buf));
1c08b210 731 if (err) {
732 printf("tpm_get_capability returned error %d.\n", err);
7690be35 733 return -1;
1c08b210 734 }
7690be35
MS
735 res_count = get_unaligned_be16(buf);
736 ptr = buf + 2;
737 for (i = 0; i < res_count; ++i, ptr += 4)
738 tpm_flush_specific(get_unaligned_be32(ptr), type);
739 } else {
b9804e5b 740 u32 handle = simple_strtoul(argv[2], NULL, 0);
7690be35 741
1c08b210 742 if (!handle) {
743 printf("Illegal resource handle %s\n", argv[2]);
7690be35 744 return -1;
1c08b210 745 }
7690be35
MS
746 tpm_flush_specific(cpu_to_be32(handle), type);
747 }
748
749 return 0;
750}
751#endif /* CONFIG_TPM_FLUSH_RESOURCES */
752
3d1df0e3 753#ifdef CONFIG_TPM_LIST_RESOURCES
754static int do_tpm_list(cmd_tbl_t *cmdtp, int flag, int argc,
755 char * const argv[])
756{
757 int type = 0;
b9804e5b
MR
758 u16 res_count;
759 u8 buf[288];
760 u8 *ptr;
3d1df0e3 761 int err;
762 uint i;
763
764 if (argc != 2)
765 return CMD_RET_USAGE;
766
767 if (!strcasecmp(argv[1], "key"))
768 type = TPM_RT_KEY;
769 else if (!strcasecmp(argv[1], "auth"))
770 type = TPM_RT_AUTH;
771 else if (!strcasecmp(argv[1], "hash"))
772 type = TPM_RT_HASH;
773 else if (!strcasecmp(argv[1], "trans"))
774 type = TPM_RT_TRANS;
775 else if (!strcasecmp(argv[1], "context"))
776 type = TPM_RT_CONTEXT;
777 else if (!strcasecmp(argv[1], "counter"))
778 type = TPM_RT_COUNTER;
779 else if (!strcasecmp(argv[1], "delegate"))
780 type = TPM_RT_DELEGATE;
781 else if (!strcasecmp(argv[1], "daa_tpm"))
782 type = TPM_RT_DAA_TPM;
783 else if (!strcasecmp(argv[1], "daa_v0"))
784 type = TPM_RT_DAA_V0;
785 else if (!strcasecmp(argv[1], "daa_v1"))
786 type = TPM_RT_DAA_V1;
787
788 if (!type) {
789 printf("Resource type %s unknown.\n", argv[1]);
790 return -1;
791 }
792
793 /* fetch list of already loaded resources in the TPM */
794 err = tpm_get_capability(TPM_CAP_HANDLE, type, buf,
795 sizeof(buf));
796 if (err) {
797 printf("tpm_get_capability returned error %d.\n", err);
798 return -1;
799 }
800 res_count = get_unaligned_be16(buf);
801 ptr = buf + 2;
802
803 printf("Resources of type %s (%02x):\n", argv[1], type);
804 if (!res_count) {
805 puts("None\n");
806 } else {
807 for (i = 0; i < res_count; ++i, ptr += 4)
808 printf("Index %d: %08x\n", i, get_unaligned_be32(ptr));
809 }
810
811 return 0;
812}
813#endif /* CONFIG_TPM_LIST_RESOURCES */
814
8732b070
CC
815#define MAKE_TPM_CMD_ENTRY(cmd) \
816 U_BOOT_CMD_MKENT(cmd, 0, 1, do_tpm_ ## cmd, "", "")
817
818static cmd_tbl_t tpm_commands[] = {
ad77694e 819 U_BOOT_CMD_MKENT(info, 0, 1, do_tpm_info, "", ""),
c6179187 820 U_BOOT_CMD_MKENT(init, 0, 1, do_tpm_init, "", ""),
8732b070 821 U_BOOT_CMD_MKENT(startup, 0, 1,
c6179187 822 do_tpm_startup, "", ""),
8732b070 823 U_BOOT_CMD_MKENT(self_test_full, 0, 1,
c6179187 824 do_tpm_self_test_full, "", ""),
8732b070 825 U_BOOT_CMD_MKENT(continue_self_test, 0, 1,
c6179187 826 do_tpm_continue_self_test, "", ""),
8732b070 827 U_BOOT_CMD_MKENT(force_clear, 0, 1,
c6179187 828 do_tpm_force_clear, "", ""),
8732b070 829 U_BOOT_CMD_MKENT(physical_enable, 0, 1,
c6179187 830 do_tpm_physical_enable, "", ""),
8732b070 831 U_BOOT_CMD_MKENT(physical_disable, 0, 1,
c6179187 832 do_tpm_physical_disable, "", ""),
8732b070 833 U_BOOT_CMD_MKENT(nv_define_space, 0, 1,
c6179187 834 do_tpm_nv_define_space, "", ""),
8732b070 835 U_BOOT_CMD_MKENT(nv_read_value, 0, 1,
c6179187 836 do_tpm_nv_read_value, "", ""),
8732b070 837 U_BOOT_CMD_MKENT(nv_write_value, 0, 1,
c6179187 838 do_tpm_nv_write_value, "", ""),
8732b070 839 U_BOOT_CMD_MKENT(extend, 0, 1,
c6179187 840 do_tpm_extend, "", ""),
8732b070 841 U_BOOT_CMD_MKENT(pcr_read, 0, 1,
c6179187 842 do_tpm_pcr_read, "", ""),
8732b070 843 U_BOOT_CMD_MKENT(tsc_physical_presence, 0, 1,
c6179187 844 do_tpm_tsc_physical_presence, "", ""),
8732b070 845 U_BOOT_CMD_MKENT(read_pubek, 0, 1,
c6179187 846 do_tpm_read_pubek, "", ""),
8732b070 847 U_BOOT_CMD_MKENT(physical_set_deactivated, 0, 1,
c6179187 848 do_tpm_physical_set_deactivated, "", ""),
8732b070 849 U_BOOT_CMD_MKENT(get_capability, 0, 1,
c6179187 850 do_tpm_get_capability, "", ""),
8732b070 851 U_BOOT_CMD_MKENT(raw_transfer, 0, 1,
c6179187 852 do_tpm_raw_transfer, "", ""),
8732b070 853 U_BOOT_CMD_MKENT(nv_define, 0, 1,
c6179187 854 do_tpm_nv_define, "", ""),
8732b070 855 U_BOOT_CMD_MKENT(nv_read, 0, 1,
c6179187 856 do_tpm_nv_read, "", ""),
8732b070 857 U_BOOT_CMD_MKENT(nv_write, 0, 1,
c6179187 858 do_tpm_nv_write, "", ""),
be6c1529
RP
859#ifdef CONFIG_TPM_AUTH_SESSIONS
860 U_BOOT_CMD_MKENT(oiap, 0, 1,
861 do_tpm_oiap, "", ""),
862 U_BOOT_CMD_MKENT(end_oiap, 0, 1,
863 do_tpm_end_oiap, "", ""),
864 U_BOOT_CMD_MKENT(load_key2_oiap, 0, 1,
865 do_tpm_load_key2_oiap, "", ""),
0f4b2ba1 866#ifdef CONFIG_TPM_LOAD_KEY_BY_SHA1
867 U_BOOT_CMD_MKENT(load_key_by_sha1, 0, 1,
868 do_tpm_load_key_by_sha1, "", ""),
869#endif /* CONFIG_TPM_LOAD_KEY_BY_SHA1 */
be6c1529
RP
870 U_BOOT_CMD_MKENT(get_pub_key_oiap, 0, 1,
871 do_tpm_get_pub_key_oiap, "", ""),
872#endif /* CONFIG_TPM_AUTH_SESSIONS */
7690be35
MS
873#ifdef CONFIG_TPM_FLUSH_RESOURCES
874 U_BOOT_CMD_MKENT(flush, 0, 1,
875 do_tpm_flush, "", ""),
876#endif /* CONFIG_TPM_FLUSH_RESOURCES */
3d1df0e3 877#ifdef CONFIG_TPM_LIST_RESOURCES
878 U_BOOT_CMD_MKENT(list, 0, 1,
879 do_tpm_list, "", ""),
880#endif /* CONFIG_TPM_LIST_RESOURCES */
8732b070
CC
881};
882
883static int do_tpm(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
884{
885 cmd_tbl_t *tpm_cmd;
886
887 if (argc < 2)
888 return CMD_RET_USAGE;
889 tpm_cmd = find_cmd_tbl(argv[1], tpm_commands, ARRAY_SIZE(tpm_commands));
890 if (!tpm_cmd)
891 return CMD_RET_USAGE;
eea3f4d3 892
8732b070 893 return tpm_cmd->cmd(cmdtp, flag, argc - 1, argv + 1);
eea3f4d3
LS
894}
895
8732b070
CC
896U_BOOT_CMD(tpm, CONFIG_SYS_MAXARGS, 1, do_tpm,
897"Issue a TPM command",
898"cmd args...\n"
899" - Issue TPM command <cmd> with arguments <args...>.\n"
900"Admin Startup and State Commands:\n"
ad77694e 901" info - Show information about the TPM\n"
8732b070
CC
902" init\n"
903" - Put TPM into a state where it waits for 'startup' command.\n"
904" startup mode\n"
905" - Issue TPM_Starup command. <mode> is one of TPM_ST_CLEAR,\n"
906" TPM_ST_STATE, and TPM_ST_DEACTIVATED.\n"
907"Admin Testing Commands:\n"
908" self_test_full\n"
909" - Test all of the TPM capabilities.\n"
910" continue_self_test\n"
911" - Inform TPM that it should complete the self-test.\n"
912"Admin Opt-in Commands:\n"
913" physical_enable\n"
914" - Set the PERMANENT disable flag to FALSE using physical presence as\n"
915" authorization.\n"
916" physical_disable\n"
917" - Set the PERMANENT disable flag to TRUE using physical presence as\n"
918" authorization.\n"
919" physical_set_deactivated 0|1\n"
920" - Set deactivated flag.\n"
921"Admin Ownership Commands:\n"
922" force_clear\n"
923" - Issue TPM_ForceClear command.\n"
924" tsc_physical_presence flags\n"
925" - Set TPM device's Physical Presence flags to <flags>.\n"
926"The Capability Commands:\n"
927" get_capability cap_area sub_cap addr count\n"
928" - Read <count> bytes of TPM capability indexed by <cap_area> and\n"
929" <sub_cap> to memory address <addr>.\n"
3d1df0e3 930#if defined(CONFIG_TPM_FLUSH_RESOURCES) || defined(CONFIG_TPM_LIST_RESOURCES)
7690be35 931"Resource management functions\n"
3d1df0e3 932#endif
933#ifdef CONFIG_TPM_FLUSH_RESOURCES
7690be35
MS
934" flush resource_type id\n"
935" - flushes a resource of type <resource_type> (may be one of key, auth,\n"
936" hash, trans, context, counter, delegate, daa_tpm, daa_v0, daa_v1),\n"
937" and id <id> from the TPM. Use an <id> of \"all\" to flush all\n"
938" resources of that type.\n"
939#endif /* CONFIG_TPM_FLUSH_RESOURCES */
3d1df0e3 940#ifdef CONFIG_TPM_LIST_RESOURCES
941" list resource_type\n"
942" - lists resources of type <resource_type> (may be one of key, auth,\n"
943" hash, trans, context, counter, delegate, daa_tpm, daa_v0, daa_v1),\n"
944" contained in the TPM.\n"
945#endif /* CONFIG_TPM_LIST_RESOURCES */
be6c1529
RP
946#ifdef CONFIG_TPM_AUTH_SESSIONS
947"Storage functions\n"
948" loadkey2_oiap parent_handle key_addr key_len usage_auth\n"
949" - loads a key data from memory address <key_addr>, <key_len> bytes\n"
950" into TPM using the parent key <parent_handle> with authorization\n"
951" <usage_auth> (20 bytes hex string).\n"
0f4b2ba1 952#ifdef CONFIG_TPM_LOAD_KEY_BY_SHA1
953" load_key_by_sha1 parent_hash key_addr key_len usage_auth\n"
954" - loads a key data from memory address <key_addr>, <key_len> bytes\n"
955" into TPM using the parent hash <parent_hash> (20 bytes hex string)\n"
956" with authorization <usage_auth> (20 bytes hex string).\n"
957#endif /* CONFIG_TPM_LOAD_KEY_BY_SHA1 */
be6c1529
RP
958" get_pub_key_oiap key_handle usage_auth\n"
959" - get the public key portion of a loaded key <key_handle> using\n"
960" authorization <usage auth> (20 bytes hex string)\n"
961#endif /* CONFIG_TPM_AUTH_SESSIONS */
8732b070
CC
962"Endorsement Key Handling Commands:\n"
963" read_pubek addr count\n"
964" - Read <count> bytes of the public endorsement key to memory\n"
965" address <addr>\n"
966"Integrity Collection and Reporting Commands:\n"
967" extend index digest_hex_string\n"
968" - Add a new measurement to a PCR. Update PCR <index> with the 20-bytes\n"
969" <digest_hex_string>\n"
970" pcr_read index addr count\n"
971" - Read <count> bytes from PCR <index> to memory address <addr>.\n"
be6c1529
RP
972#ifdef CONFIG_TPM_AUTH_SESSIONS
973"Authorization Sessions\n"
974" oiap\n"
975" - setup an OIAP session\n"
976" end_oiap\n"
977" - terminates an active OIAP session\n"
978#endif /* CONFIG_TPM_AUTH_SESSIONS */
8732b070
CC
979"Non-volatile Storage Commands:\n"
980" nv_define_space index permission size\n"
981" - Establish a space at index <index> with <permission> of <size> bytes.\n"
982" nv_read_value index addr count\n"
983" - Read <count> bytes from space <index> to memory address <addr>.\n"
984" nv_write_value index addr count\n"
985" - Write <count> bytes from memory address <addr> to space <index>.\n"
986"Miscellaneous helper functions:\n"
987" raw_transfer byte_string\n"
988" - Send a byte string <byte_string> to TPM and print the response.\n"
989" Non-volatile storage helper functions:\n"
990" These helper functions treat a non-volatile space as a non-padded\n"
991" sequence of integer values. These integer values are defined by a type\n"
992" string, which is a text string of 'bwd' characters: 'b' means a 8-bit\n"
993" value, 'w' 16-bit value, 'd' 32-bit value. All helper functions take\n"
994" a type string as their first argument.\n"
995" nv_define type_string index perm\n"
996" - Define a space <index> with permission <perm>.\n"
997" nv_read types_string index vars...\n"
998" - Read from space <index> to environment variables <vars...>.\n"
999" nv_write types_string index values...\n"
1000" - Write to space <index> from values <values...>.\n"
eea3f4d3 1001);