]> git.ipfire.org Git - thirdparty/u-boot.git/blame - cmd/tpm.c
tpm: substitute deprecated uint<x>_t types with their u<x> equivalent
[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
8732b070
CC
240static int do_tpm_startup(cmd_tbl_t *cmdtp, int flag,
241 int argc, char * const argv[])
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
261static int do_tpm_nv_define_space(cmd_tbl_t *cmdtp, int flag,
262 int argc, char * const argv[])
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
8732b070
CC
275static int do_tpm_nv_read_value(cmd_tbl_t *cmdtp, int flag,
276 int argc, 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
296static int do_tpm_nv_write_value(cmd_tbl_t *cmdtp, int flag,
297 int argc, char * const argv[])
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
318static int do_tpm_extend(cmd_tbl_t *cmdtp, int flag,
319 int argc, char * const argv[])
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
341static int do_tpm_pcr_read(cmd_tbl_t *cmdtp, int flag,
342 int argc, char * const argv[])
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
8732b070
CC
362static int do_tpm_tsc_physical_presence(cmd_tbl_t *cmdtp, int flag,
363 int argc, char * const argv[])
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
374static int do_tpm_read_pubek(cmd_tbl_t *cmdtp, int flag,
375 int argc, 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
8732b070
CC
394static int do_tpm_physical_set_deactivated(cmd_tbl_t *cmdtp, int flag,
395 int argc, char * const argv[])
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
406static int do_tpm_get_capability(cmd_tbl_t *cmdtp, int flag,
407 int argc, char * const argv[])
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, \
431 int argc, char * const argv[]) \
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
SG
457
458static int do_tpm_info(cmd_tbl_t *cmdtp, int flag, int argc,
459 char *const argv[])
460{
461 struct udevice *dev;
462 char buf[80];
463 int rc;
464
465 rc = get_tpm(&dev);
466 if (rc)
467 return rc;
468 rc = tpm_get_desc(dev, buf, sizeof(buf));
469 if (rc < 0) {
470 printf("Couldn't get TPM info (%d)\n", rc);
471 return CMD_RET_FAILURE;
472 }
473 printf("%s\n", buf);
474
475 return 0;
476}
c8a8c510 477
8732b070
CC
478static int do_tpm_raw_transfer(cmd_tbl_t *cmdtp, int flag,
479 int argc, char * const argv[])
480{
c2b0f600 481 struct udevice *dev;
8732b070 482 void *command;
b9804e5b 483 u8 response[1024];
8732b070 484 size_t count, response_length = sizeof(response);
b9804e5b 485 u32 rc;
8732b070
CC
486
487 command = parse_byte_string(argv[1], NULL, &count);
488 if (!command) {
489 printf("Couldn't parse byte string %s\n", argv[1]);
490 return CMD_RET_FAILURE;
491 }
492
c8a8c510
SG
493 rc = get_tpm(&dev);
494 if (rc)
495 return rc;
496
497 rc = tpm_xfer(dev, command, count, response, &response_length);
8732b070
CC
498 free(command);
499 if (!rc) {
500 puts("tpm response:\n");
501 print_byte_string(response, response_length);
502 }
503
f8f1fe1d 504 return report_return_code(rc);
8732b070
CC
505}
506
507static int do_tpm_nv_define(cmd_tbl_t *cmdtp, int flag,
508 int argc, char * const argv[])
509{
b9804e5b 510 u32 index, perm, size;
8732b070
CC
511
512 if (argc != 4)
513 return CMD_RET_USAGE;
514 size = type_string_get_space_size(argv[1]);
515 if (!size) {
516 printf("Couldn't parse arguments\n");
517 return CMD_RET_USAGE;
518 }
519 index = simple_strtoul(argv[2], NULL, 0);
520 perm = simple_strtoul(argv[3], NULL, 0);
521
f8f1fe1d 522 return report_return_code(tpm_nv_define_space(index, perm, size));
8732b070
CC
523}
524
525static int do_tpm_nv_read(cmd_tbl_t *cmdtp, int flag,
526 int argc, char * const argv[])
eea3f4d3 527{
b9804e5b 528 u32 index, count, err;
8732b070 529 void *data;
eea3f4d3 530
8732b070
CC
531 if (argc < 3)
532 return CMD_RET_USAGE;
533 if (argc != 3 + type_string_get_num_values(argv[1]))
534 return CMD_RET_USAGE;
535 index = simple_strtoul(argv[2], NULL, 0);
536 data = type_string_alloc(argv[1], &count);
537 if (!data) {
538 printf("Couldn't parse arguments\n");
539 return CMD_RET_USAGE;
eea3f4d3
LS
540 }
541
8732b070
CC
542 err = tpm_nv_read_value(index, data, count);
543 if (!err) {
544 if (type_string_write_vars(argv[1], data, argv + 3)) {
545 printf("Couldn't write to variables\n");
546 err = ~0;
547 }
eea3f4d3 548 }
8732b070
CC
549 free(data);
550
f8f1fe1d 551 return report_return_code(err);
8732b070
CC
552}
553
554static int do_tpm_nv_write(cmd_tbl_t *cmdtp, int flag,
555 int argc, char * const argv[])
556{
b9804e5b 557 u32 index, count, err;
8732b070
CC
558 void *data;
559
560 if (argc < 3)
561 return CMD_RET_USAGE;
562 if (argc != 3 + type_string_get_num_values(argv[1]))
563 return CMD_RET_USAGE;
564 index = simple_strtoul(argv[2], NULL, 0);
565 data = type_string_alloc(argv[1], &count);
566 if (!data) {
567 printf("Couldn't parse arguments\n");
568 return CMD_RET_USAGE;
569 }
570 if (type_string_pack(argv[1], argv + 3, data)) {
571 printf("Couldn't parse arguments\n");
572 free(data);
573 return CMD_RET_USAGE;
574 }
575
576 err = tpm_nv_write_value(index, data, count);
577 free(data);
578
f8f1fe1d 579 return report_return_code(err);
8732b070
CC
580}
581
be6c1529
RP
582#ifdef CONFIG_TPM_AUTH_SESSIONS
583
584static int do_tpm_oiap(cmd_tbl_t *cmdtp, int flag,
585 int argc, char * const argv[])
586{
b9804e5b 587 u32 auth_handle, err;
be6c1529
RP
588
589 err = tpm_oiap(&auth_handle);
590
f8f1fe1d 591 return report_return_code(err);
be6c1529
RP
592}
593
0f4b2ba1 594#ifdef CONFIG_TPM_LOAD_KEY_BY_SHA1
595static int do_tpm_load_key_by_sha1(cmd_tbl_t *cmdtp, int flag, int argc, char *
596 const argv[])
597{
b9804e5b
MR
598 u32 parent_handle = 0;
599 u32 key_len, key_handle, err;
600 u8 usage_auth[DIGEST_LENGTH];
601 u8 parent_hash[DIGEST_LENGTH];
0f4b2ba1 602 void *key;
603
604 if (argc < 5)
605 return CMD_RET_USAGE;
606
607 parse_byte_string(argv[1], parent_hash, NULL);
608 key = (void *)simple_strtoul(argv[2], NULL, 0);
609 key_len = simple_strtoul(argv[3], NULL, 0);
610 if (strlen(argv[4]) != 2 * DIGEST_LENGTH)
611 return CMD_RET_FAILURE;
612 parse_byte_string(argv[4], usage_auth, NULL);
613
614 err = tpm_find_key_sha1(usage_auth, parent_hash, &parent_handle);
615 if (err) {
616 printf("Could not find matching parent key (err = %d)\n", err);
617 return CMD_RET_FAILURE;
618 }
619
620 printf("Found parent key %08x\n", parent_handle);
621
622 err = tpm_load_key2_oiap(parent_handle, key, key_len, usage_auth,
623 &key_handle);
624 if (!err) {
625 printf("Key handle is 0x%x\n", key_handle);
018f5303 626 env_set_hex("key_handle", key_handle);
0f4b2ba1 627 }
628
629 return report_return_code(err);
630}
631#endif /* CONFIG_TPM_LOAD_KEY_BY_SHA1 */
632
be6c1529
RP
633static int do_tpm_load_key2_oiap(cmd_tbl_t *cmdtp, int flag,
634 int argc, char * const argv[])
635{
b9804e5b
MR
636 u32 parent_handle, key_len, key_handle, err;
637 u8 usage_auth[DIGEST_LENGTH];
be6c1529
RP
638 void *key;
639
640 if (argc < 5)
641 return CMD_RET_USAGE;
642
643 parent_handle = simple_strtoul(argv[1], NULL, 0);
644 key = (void *)simple_strtoul(argv[2], NULL, 0);
645 key_len = simple_strtoul(argv[3], NULL, 0);
646 if (strlen(argv[4]) != 2 * DIGEST_LENGTH)
647 return CMD_RET_FAILURE;
648 parse_byte_string(argv[4], usage_auth, NULL);
649
650 err = tpm_load_key2_oiap(parent_handle, key, key_len, usage_auth,
651 &key_handle);
652 if (!err)
653 printf("Key handle is 0x%x\n", key_handle);
654
f8f1fe1d 655 return report_return_code(err);
be6c1529
RP
656}
657
658static int do_tpm_get_pub_key_oiap(cmd_tbl_t *cmdtp, int flag,
659 int argc, char * const argv[])
660{
b9804e5b
MR
661 u32 key_handle, err;
662 u8 usage_auth[DIGEST_LENGTH];
663 u8 pub_key_buffer[TPM_PUBKEY_MAX_LENGTH];
be6c1529
RP
664 size_t pub_key_len = sizeof(pub_key_buffer);
665
666 if (argc < 3)
667 return CMD_RET_USAGE;
668
669 key_handle = simple_strtoul(argv[1], NULL, 0);
670 if (strlen(argv[2]) != 2 * DIGEST_LENGTH)
671 return CMD_RET_FAILURE;
672 parse_byte_string(argv[2], usage_auth, NULL);
673
674 err = tpm_get_pub_key_oiap(key_handle, usage_auth,
675 pub_key_buffer, &pub_key_len);
676 if (!err) {
677 printf("dump of received pub key structure:\n");
678 print_byte_string(pub_key_buffer, pub_key_len);
679 }
f8f1fe1d 680 return report_return_code(err);
be6c1529
RP
681}
682
683TPM_COMMAND_NO_ARG(tpm_end_oiap)
684
685#endif /* CONFIG_TPM_AUTH_SESSIONS */
686
7690be35
MS
687#ifdef CONFIG_TPM_FLUSH_RESOURCES
688static int do_tpm_flush(cmd_tbl_t *cmdtp, int flag, int argc,
689 char * const argv[])
690{
691 int type = 0;
692
1c08b210 693 if (argc != 3)
7690be35
MS
694 return CMD_RET_USAGE;
695
1c08b210 696 if (!strcasecmp(argv[1], "key"))
7690be35 697 type = TPM_RT_KEY;
1c08b210 698 else if (!strcasecmp(argv[1], "auth"))
7690be35 699 type = TPM_RT_AUTH;
1c08b210 700 else if (!strcasecmp(argv[1], "hash"))
7690be35 701 type = TPM_RT_HASH;
1c08b210 702 else if (!strcasecmp(argv[1], "trans"))
7690be35 703 type = TPM_RT_TRANS;
1c08b210 704 else if (!strcasecmp(argv[1], "context"))
7690be35 705 type = TPM_RT_CONTEXT;
1c08b210 706 else if (!strcasecmp(argv[1], "counter"))
7690be35 707 type = TPM_RT_COUNTER;
1c08b210 708 else if (!strcasecmp(argv[1], "delegate"))
7690be35 709 type = TPM_RT_DELEGATE;
1c08b210 710 else if (!strcasecmp(argv[1], "daa_tpm"))
7690be35 711 type = TPM_RT_DAA_TPM;
1c08b210 712 else if (!strcasecmp(argv[1], "daa_v0"))
7690be35 713 type = TPM_RT_DAA_V0;
1c08b210 714 else if (!strcasecmp(argv[1], "daa_v1"))
7690be35
MS
715 type = TPM_RT_DAA_V1;
716
1c08b210 717 if (!type) {
718 printf("Resource type %s unknown.\n", argv[1]);
719 return -1;
720 }
721
722 if (!strcasecmp(argv[2], "all")) {
b9804e5b
MR
723 u16 res_count;
724 u8 buf[288];
725 u8 *ptr;
7690be35
MS
726 int err;
727 uint i;
728
729 /* fetch list of already loaded resources in the TPM */
730 err = tpm_get_capability(TPM_CAP_HANDLE, type, buf,
731 sizeof(buf));
1c08b210 732 if (err) {
733 printf("tpm_get_capability returned error %d.\n", err);
7690be35 734 return -1;
1c08b210 735 }
7690be35
MS
736 res_count = get_unaligned_be16(buf);
737 ptr = buf + 2;
738 for (i = 0; i < res_count; ++i, ptr += 4)
739 tpm_flush_specific(get_unaligned_be32(ptr), type);
740 } else {
b9804e5b 741 u32 handle = simple_strtoul(argv[2], NULL, 0);
7690be35 742
1c08b210 743 if (!handle) {
744 printf("Illegal resource handle %s\n", argv[2]);
7690be35 745 return -1;
1c08b210 746 }
7690be35
MS
747 tpm_flush_specific(cpu_to_be32(handle), type);
748 }
749
750 return 0;
751}
752#endif /* CONFIG_TPM_FLUSH_RESOURCES */
753
3d1df0e3 754#ifdef CONFIG_TPM_LIST_RESOURCES
755static int do_tpm_list(cmd_tbl_t *cmdtp, int flag, int argc,
756 char * const argv[])
757{
758 int type = 0;
b9804e5b
MR
759 u16 res_count;
760 u8 buf[288];
761 u8 *ptr;
3d1df0e3 762 int err;
763 uint i;
764
765 if (argc != 2)
766 return CMD_RET_USAGE;
767
768 if (!strcasecmp(argv[1], "key"))
769 type = TPM_RT_KEY;
770 else if (!strcasecmp(argv[1], "auth"))
771 type = TPM_RT_AUTH;
772 else if (!strcasecmp(argv[1], "hash"))
773 type = TPM_RT_HASH;
774 else if (!strcasecmp(argv[1], "trans"))
775 type = TPM_RT_TRANS;
776 else if (!strcasecmp(argv[1], "context"))
777 type = TPM_RT_CONTEXT;
778 else if (!strcasecmp(argv[1], "counter"))
779 type = TPM_RT_COUNTER;
780 else if (!strcasecmp(argv[1], "delegate"))
781 type = TPM_RT_DELEGATE;
782 else if (!strcasecmp(argv[1], "daa_tpm"))
783 type = TPM_RT_DAA_TPM;
784 else if (!strcasecmp(argv[1], "daa_v0"))
785 type = TPM_RT_DAA_V0;
786 else if (!strcasecmp(argv[1], "daa_v1"))
787 type = TPM_RT_DAA_V1;
788
789 if (!type) {
790 printf("Resource type %s unknown.\n", argv[1]);
791 return -1;
792 }
793
794 /* fetch list of already loaded resources in the TPM */
795 err = tpm_get_capability(TPM_CAP_HANDLE, type, buf,
796 sizeof(buf));
797 if (err) {
798 printf("tpm_get_capability returned error %d.\n", err);
799 return -1;
800 }
801 res_count = get_unaligned_be16(buf);
802 ptr = buf + 2;
803
804 printf("Resources of type %s (%02x):\n", argv[1], type);
805 if (!res_count) {
806 puts("None\n");
807 } else {
808 for (i = 0; i < res_count; ++i, ptr += 4)
809 printf("Index %d: %08x\n", i, get_unaligned_be32(ptr));
810 }
811
812 return 0;
813}
814#endif /* CONFIG_TPM_LIST_RESOURCES */
815
8732b070
CC
816#define MAKE_TPM_CMD_ENTRY(cmd) \
817 U_BOOT_CMD_MKENT(cmd, 0, 1, do_tpm_ ## cmd, "", "")
818
819static cmd_tbl_t tpm_commands[] = {
ad77694e 820 U_BOOT_CMD_MKENT(info, 0, 1, do_tpm_info, "", ""),
8732b070
CC
821 U_BOOT_CMD_MKENT(init, 0, 1,
822 do_tpm_init, "", ""),
823 U_BOOT_CMD_MKENT(startup, 0, 1,
824 do_tpm_startup, "", ""),
825 U_BOOT_CMD_MKENT(self_test_full, 0, 1,
826 do_tpm_self_test_full, "", ""),
827 U_BOOT_CMD_MKENT(continue_self_test, 0, 1,
828 do_tpm_continue_self_test, "", ""),
829 U_BOOT_CMD_MKENT(force_clear, 0, 1,
830 do_tpm_force_clear, "", ""),
831 U_BOOT_CMD_MKENT(physical_enable, 0, 1,
832 do_tpm_physical_enable, "", ""),
833 U_BOOT_CMD_MKENT(physical_disable, 0, 1,
834 do_tpm_physical_disable, "", ""),
835 U_BOOT_CMD_MKENT(nv_define_space, 0, 1,
836 do_tpm_nv_define_space, "", ""),
837 U_BOOT_CMD_MKENT(nv_read_value, 0, 1,
838 do_tpm_nv_read_value, "", ""),
839 U_BOOT_CMD_MKENT(nv_write_value, 0, 1,
840 do_tpm_nv_write_value, "", ""),
841 U_BOOT_CMD_MKENT(extend, 0, 1,
842 do_tpm_extend, "", ""),
843 U_BOOT_CMD_MKENT(pcr_read, 0, 1,
844 do_tpm_pcr_read, "", ""),
845 U_BOOT_CMD_MKENT(tsc_physical_presence, 0, 1,
846 do_tpm_tsc_physical_presence, "", ""),
847 U_BOOT_CMD_MKENT(read_pubek, 0, 1,
848 do_tpm_read_pubek, "", ""),
849 U_BOOT_CMD_MKENT(physical_set_deactivated, 0, 1,
850 do_tpm_physical_set_deactivated, "", ""),
851 U_BOOT_CMD_MKENT(get_capability, 0, 1,
852 do_tpm_get_capability, "", ""),
853 U_BOOT_CMD_MKENT(raw_transfer, 0, 1,
854 do_tpm_raw_transfer, "", ""),
855 U_BOOT_CMD_MKENT(nv_define, 0, 1,
856 do_tpm_nv_define, "", ""),
857 U_BOOT_CMD_MKENT(nv_read, 0, 1,
858 do_tpm_nv_read, "", ""),
859 U_BOOT_CMD_MKENT(nv_write, 0, 1,
860 do_tpm_nv_write, "", ""),
be6c1529
RP
861#ifdef CONFIG_TPM_AUTH_SESSIONS
862 U_BOOT_CMD_MKENT(oiap, 0, 1,
863 do_tpm_oiap, "", ""),
864 U_BOOT_CMD_MKENT(end_oiap, 0, 1,
865 do_tpm_end_oiap, "", ""),
866 U_BOOT_CMD_MKENT(load_key2_oiap, 0, 1,
867 do_tpm_load_key2_oiap, "", ""),
0f4b2ba1 868#ifdef CONFIG_TPM_LOAD_KEY_BY_SHA1
869 U_BOOT_CMD_MKENT(load_key_by_sha1, 0, 1,
870 do_tpm_load_key_by_sha1, "", ""),
871#endif /* CONFIG_TPM_LOAD_KEY_BY_SHA1 */
be6c1529
RP
872 U_BOOT_CMD_MKENT(get_pub_key_oiap, 0, 1,
873 do_tpm_get_pub_key_oiap, "", ""),
874#endif /* CONFIG_TPM_AUTH_SESSIONS */
7690be35
MS
875#ifdef CONFIG_TPM_FLUSH_RESOURCES
876 U_BOOT_CMD_MKENT(flush, 0, 1,
877 do_tpm_flush, "", ""),
878#endif /* CONFIG_TPM_FLUSH_RESOURCES */
3d1df0e3 879#ifdef CONFIG_TPM_LIST_RESOURCES
880 U_BOOT_CMD_MKENT(list, 0, 1,
881 do_tpm_list, "", ""),
882#endif /* CONFIG_TPM_LIST_RESOURCES */
8732b070
CC
883};
884
885static int do_tpm(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
886{
887 cmd_tbl_t *tpm_cmd;
888
889 if (argc < 2)
890 return CMD_RET_USAGE;
891 tpm_cmd = find_cmd_tbl(argv[1], tpm_commands, ARRAY_SIZE(tpm_commands));
892 if (!tpm_cmd)
893 return CMD_RET_USAGE;
eea3f4d3 894
8732b070 895 return tpm_cmd->cmd(cmdtp, flag, argc - 1, argv + 1);
eea3f4d3
LS
896}
897
8732b070
CC
898U_BOOT_CMD(tpm, CONFIG_SYS_MAXARGS, 1, do_tpm,
899"Issue a TPM command",
900"cmd args...\n"
901" - Issue TPM command <cmd> with arguments <args...>.\n"
902"Admin Startup and State Commands:\n"
ad77694e 903" info - Show information about the TPM\n"
8732b070
CC
904" init\n"
905" - Put TPM into a state where it waits for 'startup' command.\n"
906" startup mode\n"
907" - Issue TPM_Starup command. <mode> is one of TPM_ST_CLEAR,\n"
908" TPM_ST_STATE, and TPM_ST_DEACTIVATED.\n"
909"Admin Testing Commands:\n"
910" self_test_full\n"
911" - Test all of the TPM capabilities.\n"
912" continue_self_test\n"
913" - Inform TPM that it should complete the self-test.\n"
914"Admin Opt-in Commands:\n"
915" physical_enable\n"
916" - Set the PERMANENT disable flag to FALSE using physical presence as\n"
917" authorization.\n"
918" physical_disable\n"
919" - Set the PERMANENT disable flag to TRUE using physical presence as\n"
920" authorization.\n"
921" physical_set_deactivated 0|1\n"
922" - Set deactivated flag.\n"
923"Admin Ownership Commands:\n"
924" force_clear\n"
925" - Issue TPM_ForceClear command.\n"
926" tsc_physical_presence flags\n"
927" - Set TPM device's Physical Presence flags to <flags>.\n"
928"The Capability Commands:\n"
929" get_capability cap_area sub_cap addr count\n"
930" - Read <count> bytes of TPM capability indexed by <cap_area> and\n"
931" <sub_cap> to memory address <addr>.\n"
3d1df0e3 932#if defined(CONFIG_TPM_FLUSH_RESOURCES) || defined(CONFIG_TPM_LIST_RESOURCES)
7690be35 933"Resource management functions\n"
3d1df0e3 934#endif
935#ifdef CONFIG_TPM_FLUSH_RESOURCES
7690be35
MS
936" flush resource_type id\n"
937" - flushes a resource of type <resource_type> (may be one of key, auth,\n"
938" hash, trans, context, counter, delegate, daa_tpm, daa_v0, daa_v1),\n"
939" and id <id> from the TPM. Use an <id> of \"all\" to flush all\n"
940" resources of that type.\n"
941#endif /* CONFIG_TPM_FLUSH_RESOURCES */
3d1df0e3 942#ifdef CONFIG_TPM_LIST_RESOURCES
943" list resource_type\n"
944" - lists resources of type <resource_type> (may be one of key, auth,\n"
945" hash, trans, context, counter, delegate, daa_tpm, daa_v0, daa_v1),\n"
946" contained in the TPM.\n"
947#endif /* CONFIG_TPM_LIST_RESOURCES */
be6c1529
RP
948#ifdef CONFIG_TPM_AUTH_SESSIONS
949"Storage functions\n"
950" loadkey2_oiap parent_handle key_addr key_len usage_auth\n"
951" - loads a key data from memory address <key_addr>, <key_len> bytes\n"
952" into TPM using the parent key <parent_handle> with authorization\n"
953" <usage_auth> (20 bytes hex string).\n"
0f4b2ba1 954#ifdef CONFIG_TPM_LOAD_KEY_BY_SHA1
955" load_key_by_sha1 parent_hash key_addr key_len usage_auth\n"
956" - loads a key data from memory address <key_addr>, <key_len> bytes\n"
957" into TPM using the parent hash <parent_hash> (20 bytes hex string)\n"
958" with authorization <usage_auth> (20 bytes hex string).\n"
959#endif /* CONFIG_TPM_LOAD_KEY_BY_SHA1 */
be6c1529
RP
960" get_pub_key_oiap key_handle usage_auth\n"
961" - get the public key portion of a loaded key <key_handle> using\n"
962" authorization <usage auth> (20 bytes hex string)\n"
963#endif /* CONFIG_TPM_AUTH_SESSIONS */
8732b070
CC
964"Endorsement Key Handling Commands:\n"
965" read_pubek addr count\n"
966" - Read <count> bytes of the public endorsement key to memory\n"
967" address <addr>\n"
968"Integrity Collection and Reporting Commands:\n"
969" extend index digest_hex_string\n"
970" - Add a new measurement to a PCR. Update PCR <index> with the 20-bytes\n"
971" <digest_hex_string>\n"
972" pcr_read index addr count\n"
973" - Read <count> bytes from PCR <index> to memory address <addr>.\n"
be6c1529
RP
974#ifdef CONFIG_TPM_AUTH_SESSIONS
975"Authorization Sessions\n"
976" oiap\n"
977" - setup an OIAP session\n"
978" end_oiap\n"
979" - terminates an active OIAP session\n"
980#endif /* CONFIG_TPM_AUTH_SESSIONS */
8732b070
CC
981"Non-volatile Storage Commands:\n"
982" nv_define_space index permission size\n"
983" - Establish a space at index <index> with <permission> of <size> bytes.\n"
984" nv_read_value index addr count\n"
985" - Read <count> bytes from space <index> to memory address <addr>.\n"
986" nv_write_value index addr count\n"
987" - Write <count> bytes from memory address <addr> to space <index>.\n"
988"Miscellaneous helper functions:\n"
989" raw_transfer byte_string\n"
990" - Send a byte string <byte_string> to TPM and print the response.\n"
991" Non-volatile storage helper functions:\n"
992" These helper functions treat a non-volatile space as a non-padded\n"
993" sequence of integer values. These integer values are defined by a type\n"
994" string, which is a text string of 'bwd' characters: 'b' means a 8-bit\n"
995" value, 'w' 16-bit value, 'd' 32-bit value. All helper functions take\n"
996" a type string as their first argument.\n"
997" nv_define type_string index perm\n"
998" - Define a space <index> with permission <perm>.\n"
999" nv_read types_string index vars...\n"
1000" - Read from space <index> to environment variables <vars...>.\n"
1001" nv_write types_string index values...\n"
1002" - Write to space <index> from values <values...>.\n"
eea3f4d3 1003);