blob: c173bcfe42a9881ce80346ea46648bb76c40b102 [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>
7#include <command.h>
Simon Glassc8a8c512015-08-22 18:31:32 -06008#include <dm.h>
Che-liang Chiou8732b072013-02-28 09:34:57 +00009#include <malloc.h>
Vadim Bendebury576fb1e2011-10-15 15:13:34 +000010#include <tpm.h>
Che-liang Chiou8732b072013-02-28 09:34:57 +000011#include <asm/unaligned.h>
12#include <linux/string.h>
Vadim Bendebury576fb1e2011-10-15 15:13:34 +000013
Reinhard Pfaube6c1522013-06-26 15:55:13 +020014/* Useful constants */
15enum {
16 DIGEST_LENGTH = 20,
17 /* max lengths, valid for RSA keys <= 2048 bits */
18 TPM_PUBKEY_MAX_LENGTH = 288,
19};
20
Che-liang Chiou8732b072013-02-28 09:34:57 +000021/**
22 * Print a byte string in hexdecimal format, 16-bytes per line.
Vadim Bendebury576fb1e2011-10-15 15:13:34 +000023 *
Che-liang Chiou8732b072013-02-28 09:34:57 +000024 * @param data byte string to be printed
25 * @param count number of bytes to be printed
Vadim Bendebury576fb1e2011-10-15 15:13:34 +000026 */
Che-liang Chiou8732b072013-02-28 09:34:57 +000027static void print_byte_string(uint8_t *data, size_t count)
Vadim Bendebury576fb1e2011-10-15 15:13:34 +000028{
Che-liang Chiou8732b072013-02-28 09:34:57 +000029 int i, print_newline = 0;
Vadim Bendebury576fb1e2011-10-15 15:13:34 +000030
Che-liang Chiou8732b072013-02-28 09:34:57 +000031 for (i = 0; i < count; i++) {
32 printf(" %02x", data[i]);
33 print_newline = (i % 16 == 15);
34 if (print_newline)
35 putc('\n');
Vadim Bendebury576fb1e2011-10-15 15:13:34 +000036 }
Che-liang Chiou8732b072013-02-28 09:34:57 +000037 /* Avoid duplicated newline at the end */
38 if (!print_newline)
39 putc('\n');
Vadim Bendebury576fb1e2011-10-15 15:13:34 +000040}
41
Che-liang Chiou8732b072013-02-28 09:34:57 +000042/**
43 * Convert a text string of hexdecimal values into a byte string.
44 *
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
53 */
54static void *parse_byte_string(char *bytes, uint8_t *data, size_t *count_ptr)
Luigi Semenzatoeea3f4d2012-12-05 14:46:44 +000055{
Che-liang Chiou8732b072013-02-28 09:34:57 +000056 char byte[3];
57 size_t count, length;
Luigi Semenzatoeea3f4d2012-12-05 14:46:44 +000058 int i;
Luigi Semenzatoeea3f4d2012-12-05 14:46:44 +000059
Simon Glass5c51d8a2015-08-22 18:31:36 -060060 if (!bytes)
61 return NULL;
Che-liang Chiou8732b072013-02-28 09:34:57 +000062 length = strlen(bytes);
63 count = length / 2;
Luigi Semenzatoeea3f4d2012-12-05 14:46:44 +000064
Che-liang Chiou8732b072013-02-28 09:34:57 +000065 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];
74 data[i / 2] = (uint8_t)simple_strtoul(byte, NULL, 16);
Luigi Semenzatoeea3f4d2012-12-05 14:46:44 +000075 }
Che-liang Chiou8732b072013-02-28 09:34:57 +000076
77 if (count_ptr)
78 *count_ptr = count;
79
80 return data;
Luigi Semenzatoeea3f4d2012-12-05 14:46:44 +000081}
82
Che-liang Chiou8732b072013-02-28 09:34:57 +000083/**
Simon Glassf8f1fe12015-08-22 18:31:34 -060084 * report_return_code() - Report any error and return failure or success
Che-liang Chiou8732b072013-02-28 09:34:57 +000085 *
86 * @param return_code TPM command return code
87 * @return value of enum command_ret_t
88 */
Simon Glassf8f1fe12015-08-22 18:31:34 -060089static int report_return_code(int return_code)
Vadim Bendebury576fb1e2011-10-15 15:13:34 +000090{
Simon Glassf8f1fe12015-08-22 18:31:34 -060091 if (return_code) {
92 printf("Error: %d\n", return_code);
Che-liang Chiou8732b072013-02-28 09:34:57 +000093 return CMD_RET_FAILURE;
Simon Glassf8f1fe12015-08-22 18:31:34 -060094 } else {
Che-liang Chiou8732b072013-02-28 09:34:57 +000095 return CMD_RET_SUCCESS;
Simon Glassf8f1fe12015-08-22 18:31:34 -060096 }
Vadim Bendebury576fb1e2011-10-15 15:13:34 +000097}
98
Che-liang Chiou8732b072013-02-28 09:34:57 +000099/**
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 }
135 }
136
137 return size;
138}
139
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 */
148static void *type_string_alloc(const char *type_str, uint32_t *count)
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;
159
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[],
173 uint8_t *data)
174{
175 size_t offset;
176 uint32_t value;
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;
192 break;
193 default:
194 return -1;
195 }
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 */
210static int type_string_write_vars(const char *type_str, uint8_t *data,
211 char * const vars[])
212{
213 size_t offset;
214 uint32_t value;
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;
225 break;
226 case 'd':
227 value = get_unaligned_be32(data + offset);
228 offset += 4;
229 break;
230 default:
231 return -1;
232 }
Simon Glass018f5302017-08-03 12:22:10 -0600233 if (env_set_ulong(*vars, value))
Che-liang Chiou8732b072013-02-28 09:34:57 +0000234 return -1;
235 }
236
237 return 0;
238}
239
240static int do_tpm_startup(cmd_tbl_t *cmdtp, int flag,
241 int argc, char * const argv[])
242{
243 enum tpm_startup_type mode;
244
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
Simon Glassf8f1fe12015-08-22 18:31:34 -0600258 return report_return_code(tpm_startup(mode));
Che-liang Chiou8732b072013-02-28 09:34:57 +0000259}
260
261static int do_tpm_nv_define_space(cmd_tbl_t *cmdtp, int flag,
262 int argc, char * const argv[])
263{
264 uint32_t index, perm, size;
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
Simon Glassf8f1fe12015-08-22 18:31:34 -0600272 return report_return_code(tpm_nv_define_space(index, perm, size));
Che-liang Chiou8732b072013-02-28 09:34:57 +0000273}
274
275static int do_tpm_nv_read_value(cmd_tbl_t *cmdtp, int flag,
276 int argc, char * const argv[])
277{
278 uint32_t index, count, rc;
279 void *data;
280
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);
291 }
292
Simon Glassf8f1fe12015-08-22 18:31:34 -0600293 return report_return_code(rc);
Che-liang Chiou8732b072013-02-28 09:34:57 +0000294}
295
296static int do_tpm_nv_write_value(cmd_tbl_t *cmdtp, int flag,
297 int argc, char * const argv[])
298{
299 uint32_t index, rc;
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;
310 }
311
312 rc = tpm_nv_write_value(index, data, count);
313 free(data);
314
Simon Glassf8f1fe12015-08-22 18:31:34 -0600315 return report_return_code(rc);
Che-liang Chiou8732b072013-02-28 09:34:57 +0000316}
317
318static int do_tpm_extend(cmd_tbl_t *cmdtp, int flag,
319 int argc, char * const argv[])
320{
321 uint32_t index, rc;
322 uint8_t in_digest[20], out_digest[20];
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;
330 }
331
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));
336 }
337
Simon Glassf8f1fe12015-08-22 18:31:34 -0600338 return report_return_code(rc);
Che-liang Chiou8732b072013-02-28 09:34:57 +0000339}
340
341static int do_tpm_pcr_read(cmd_tbl_t *cmdtp, int flag,
342 int argc, char * const argv[])
343{
344 uint32_t index, count, rc;
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);
352
353 rc = tpm_pcr_read(index, data, count);
354 if (!rc) {
355 puts("Named PCR content:\n");
356 print_byte_string(data, count);
357 }
358
Simon Glassf8f1fe12015-08-22 18:31:34 -0600359 return report_return_code(rc);
Che-liang Chiou8732b072013-02-28 09:34:57 +0000360}
361
362static int do_tpm_tsc_physical_presence(cmd_tbl_t *cmdtp, int flag,
363 int argc, char * const argv[])
364{
365 uint16_t presence;
366
367 if (argc != 2)
368 return CMD_RET_USAGE;
369 presence = (uint16_t)simple_strtoul(argv[1], NULL, 0);
370
Simon Glassf8f1fe12015-08-22 18:31:34 -0600371 return report_return_code(tpm_tsc_physical_presence(presence));
Che-liang Chiou8732b072013-02-28 09:34:57 +0000372}
373
374static int do_tpm_read_pubek(cmd_tbl_t *cmdtp, int flag,
375 int argc, char * const argv[])
376{
377 uint32_t count, rc;
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
Simon Glassf8f1fe12015-08-22 18:31:34 -0600391 return report_return_code(rc);
Che-liang Chiou8732b072013-02-28 09:34:57 +0000392}
393
394static int do_tpm_physical_set_deactivated(cmd_tbl_t *cmdtp, int flag,
395 int argc, char * const argv[])
396{
397 uint8_t state;
398
399 if (argc != 2)
400 return CMD_RET_USAGE;
401 state = (uint8_t)simple_strtoul(argv[1], NULL, 0);
402
Simon Glassf8f1fe12015-08-22 18:31:34 -0600403 return report_return_code(tpm_physical_set_deactivated(state));
Che-liang Chiou8732b072013-02-28 09:34:57 +0000404}
405
406static int do_tpm_get_capability(cmd_tbl_t *cmdtp, int flag,
407 int argc, char * const argv[])
408{
409 uint32_t cap_area, sub_cap, rc;
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
Simon Glassf8f1fe12015-08-22 18:31:34 -0600426 return report_return_code(rc);
Che-liang Chiou8732b072013-02-28 09:34:57 +0000427}
428
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; \
Simon Glassf8f1fe12015-08-22 18:31:34 -0600435 return report_return_code(cmd()); \
Che-liang Chiou8732b072013-02-28 09:34:57 +0000436}
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
Simon Glassc8a8c512015-08-22 18:31:32 -0600445static int get_tpm(struct udevice **devp)
446{
447 int rc;
448
Simon Glass3f603cb2016-02-11 13:23:26 -0700449 rc = uclass_first_device_err(UCLASS_TPM, devp);
450 if (rc) {
Simon Glassc8a8c512015-08-22 18:31:32 -0600451 printf("Could not find TPM (ret=%d)\n", rc);
452 return CMD_RET_FAILURE;
453 }
454
455 return 0;
456}
Simon Glassad776942015-08-22 18:31:40 -0600457
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}
Simon Glassc8a8c512015-08-22 18:31:32 -0600477
Che-liang Chiou8732b072013-02-28 09:34:57 +0000478static int do_tpm_raw_transfer(cmd_tbl_t *cmdtp, int flag,
479 int argc, char * const argv[])
480{
Christophe Ricardc2b0f602015-10-06 22:54:43 +0200481 struct udevice *dev;
Che-liang Chiou8732b072013-02-28 09:34:57 +0000482 void *command;
483 uint8_t response[1024];
484 size_t count, response_length = sizeof(response);
485 uint32_t rc;
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
Simon Glassc8a8c512015-08-22 18:31:32 -0600493 rc = get_tpm(&dev);
494 if (rc)
495 return rc;
496
497 rc = tpm_xfer(dev, command, count, response, &response_length);
Che-liang Chiou8732b072013-02-28 09:34:57 +0000498 free(command);
499 if (!rc) {
500 puts("tpm response:\n");
501 print_byte_string(response, response_length);
502 }
503
Simon Glassf8f1fe12015-08-22 18:31:34 -0600504 return report_return_code(rc);
Che-liang Chiou8732b072013-02-28 09:34:57 +0000505}
506
507static int do_tpm_nv_define(cmd_tbl_t *cmdtp, int flag,
508 int argc, char * const argv[])
509{
510 uint32_t index, perm, size;
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
Simon Glassf8f1fe12015-08-22 18:31:34 -0600522 return report_return_code(tpm_nv_define_space(index, perm, size));
Che-liang Chiou8732b072013-02-28 09:34:57 +0000523}
524
525static int do_tpm_nv_read(cmd_tbl_t *cmdtp, int flag,
526 int argc, char * const argv[])
527{
528 uint32_t index, count, err;
529 void *data;
530
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;
540 }
541
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 }
548 }
549 free(data);
550
Simon Glassf8f1fe12015-08-22 18:31:34 -0600551 return report_return_code(err);
Che-liang Chiou8732b072013-02-28 09:34:57 +0000552}
553
554static int do_tpm_nv_write(cmd_tbl_t *cmdtp, int flag,
555 int argc, char * const argv[])
556{
557 uint32_t index, count, err;
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
Simon Glassf8f1fe12015-08-22 18:31:34 -0600579 return report_return_code(err);
Che-liang Chiou8732b072013-02-28 09:34:57 +0000580}
581
Reinhard Pfaube6c1522013-06-26 15:55:13 +0200582#ifdef CONFIG_TPM_AUTH_SESSIONS
583
584static int do_tpm_oiap(cmd_tbl_t *cmdtp, int flag,
585 int argc, char * const argv[])
586{
587 uint32_t auth_handle, err;
588
589 err = tpm_oiap(&auth_handle);
590
Simon Glassf8f1fe12015-08-22 18:31:34 -0600591 return report_return_code(err);
Reinhard Pfaube6c1522013-06-26 15:55:13 +0200592}
593
mario.six@gdsys.cc0f4b2ba2017-03-20 10:28:28 +0100594#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{
598 uint32_t parent_handle = 0;
599 uint32_t key_len, key_handle, err;
600 uint8_t usage_auth[DIGEST_LENGTH];
601 uint8_t parent_hash[DIGEST_LENGTH];
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);
Simon Glass018f5302017-08-03 12:22:10 -0600626 env_set_hex("key_handle", key_handle);
mario.six@gdsys.cc0f4b2ba2017-03-20 10:28:28 +0100627 }
628
629 return report_return_code(err);
630}
631#endif /* CONFIG_TPM_LOAD_KEY_BY_SHA1 */
632
Reinhard Pfaube6c1522013-06-26 15:55:13 +0200633static int do_tpm_load_key2_oiap(cmd_tbl_t *cmdtp, int flag,
634 int argc, char * const argv[])
635{
636 uint32_t parent_handle, key_len, key_handle, err;
637 uint8_t usage_auth[DIGEST_LENGTH];
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
Simon Glassf8f1fe12015-08-22 18:31:34 -0600655 return report_return_code(err);
Reinhard Pfaube6c1522013-06-26 15:55:13 +0200656}
657
658static int do_tpm_get_pub_key_oiap(cmd_tbl_t *cmdtp, int flag,
659 int argc, char * const argv[])
660{
661 uint32_t key_handle, err;
662 uint8_t usage_auth[DIGEST_LENGTH];
663 uint8_t pub_key_buffer[TPM_PUBKEY_MAX_LENGTH];
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 }
Simon Glassf8f1fe12015-08-22 18:31:34 -0600680 return report_return_code(err);
Reinhard Pfaube6c1522013-06-26 15:55:13 +0200681}
682
683TPM_COMMAND_NO_ARG(tpm_end_oiap)
684
685#endif /* CONFIG_TPM_AUTH_SESSIONS */
686
Mario Six7690be32017-01-11 16:00:50 +0100687#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
mario.six@gdsys.cc1c08b212017-03-20 10:28:29 +0100693 if (argc != 3)
Mario Six7690be32017-01-11 16:00:50 +0100694 return CMD_RET_USAGE;
695
mario.six@gdsys.cc1c08b212017-03-20 10:28:29 +0100696 if (!strcasecmp(argv[1], "key"))
Mario Six7690be32017-01-11 16:00:50 +0100697 type = TPM_RT_KEY;
mario.six@gdsys.cc1c08b212017-03-20 10:28:29 +0100698 else if (!strcasecmp(argv[1], "auth"))
Mario Six7690be32017-01-11 16:00:50 +0100699 type = TPM_RT_AUTH;
mario.six@gdsys.cc1c08b212017-03-20 10:28:29 +0100700 else if (!strcasecmp(argv[1], "hash"))
Mario Six7690be32017-01-11 16:00:50 +0100701 type = TPM_RT_HASH;
mario.six@gdsys.cc1c08b212017-03-20 10:28:29 +0100702 else if (!strcasecmp(argv[1], "trans"))
Mario Six7690be32017-01-11 16:00:50 +0100703 type = TPM_RT_TRANS;
mario.six@gdsys.cc1c08b212017-03-20 10:28:29 +0100704 else if (!strcasecmp(argv[1], "context"))
Mario Six7690be32017-01-11 16:00:50 +0100705 type = TPM_RT_CONTEXT;
mario.six@gdsys.cc1c08b212017-03-20 10:28:29 +0100706 else if (!strcasecmp(argv[1], "counter"))
Mario Six7690be32017-01-11 16:00:50 +0100707 type = TPM_RT_COUNTER;
mario.six@gdsys.cc1c08b212017-03-20 10:28:29 +0100708 else if (!strcasecmp(argv[1], "delegate"))
Mario Six7690be32017-01-11 16:00:50 +0100709 type = TPM_RT_DELEGATE;
mario.six@gdsys.cc1c08b212017-03-20 10:28:29 +0100710 else if (!strcasecmp(argv[1], "daa_tpm"))
Mario Six7690be32017-01-11 16:00:50 +0100711 type = TPM_RT_DAA_TPM;
mario.six@gdsys.cc1c08b212017-03-20 10:28:29 +0100712 else if (!strcasecmp(argv[1], "daa_v0"))
Mario Six7690be32017-01-11 16:00:50 +0100713 type = TPM_RT_DAA_V0;
mario.six@gdsys.cc1c08b212017-03-20 10:28:29 +0100714 else if (!strcasecmp(argv[1], "daa_v1"))
Mario Six7690be32017-01-11 16:00:50 +0100715 type = TPM_RT_DAA_V1;
716
mario.six@gdsys.cc1c08b212017-03-20 10:28:29 +0100717 if (!type) {
718 printf("Resource type %s unknown.\n", argv[1]);
719 return -1;
720 }
721
722 if (!strcasecmp(argv[2], "all")) {
Mario Six7690be32017-01-11 16:00:50 +0100723 uint16_t res_count;
724 uint8_t buf[288];
725 uint8_t *ptr;
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));
mario.six@gdsys.cc1c08b212017-03-20 10:28:29 +0100732 if (err) {
733 printf("tpm_get_capability returned error %d.\n", err);
Mario Six7690be32017-01-11 16:00:50 +0100734 return -1;
mario.six@gdsys.cc1c08b212017-03-20 10:28:29 +0100735 }
Mario Six7690be32017-01-11 16:00:50 +0100736 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 {
741 uint32_t handle = simple_strtoul(argv[2], NULL, 0);
742
mario.six@gdsys.cc1c08b212017-03-20 10:28:29 +0100743 if (!handle) {
744 printf("Illegal resource handle %s\n", argv[2]);
Mario Six7690be32017-01-11 16:00:50 +0100745 return -1;
mario.six@gdsys.cc1c08b212017-03-20 10:28:29 +0100746 }
Mario Six7690be32017-01-11 16:00:50 +0100747 tpm_flush_specific(cpu_to_be32(handle), type);
748 }
749
750 return 0;
751}
752#endif /* CONFIG_TPM_FLUSH_RESOURCES */
753
mario.six@gdsys.cc3d1df0e2017-03-20 10:28:30 +0100754#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;
759 uint16_t res_count;
760 uint8_t buf[288];
761 uint8_t *ptr;
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
Che-liang Chiou8732b072013-02-28 09:34:57 +0000816#define MAKE_TPM_CMD_ENTRY(cmd) \
817 U_BOOT_CMD_MKENT(cmd, 0, 1, do_tpm_ ## cmd, "", "")
818
819static cmd_tbl_t tpm_commands[] = {
Simon Glassad776942015-08-22 18:31:40 -0600820 U_BOOT_CMD_MKENT(info, 0, 1, do_tpm_info, "", ""),
Che-liang Chiou8732b072013-02-28 09:34:57 +0000821 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, "", ""),
Reinhard Pfaube6c1522013-06-26 15:55:13 +0200861#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, "", ""),
mario.six@gdsys.cc0f4b2ba2017-03-20 10:28:28 +0100868#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 */
Reinhard Pfaube6c1522013-06-26 15:55:13 +0200872 U_BOOT_CMD_MKENT(get_pub_key_oiap, 0, 1,
873 do_tpm_get_pub_key_oiap, "", ""),
874#endif /* CONFIG_TPM_AUTH_SESSIONS */
Mario Six7690be32017-01-11 16:00:50 +0100875#ifdef CONFIG_TPM_FLUSH_RESOURCES
876 U_BOOT_CMD_MKENT(flush, 0, 1,
877 do_tpm_flush, "", ""),
878#endif /* CONFIG_TPM_FLUSH_RESOURCES */
mario.six@gdsys.cc3d1df0e2017-03-20 10:28:30 +0100879#ifdef CONFIG_TPM_LIST_RESOURCES
880 U_BOOT_CMD_MKENT(list, 0, 1,
881 do_tpm_list, "", ""),
882#endif /* CONFIG_TPM_LIST_RESOURCES */
Che-liang Chiou8732b072013-02-28 09:34:57 +0000883};
Luigi Semenzatoeea3f4d2012-12-05 14:46:44 +0000884
885static int do_tpm(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
886{
Che-liang Chiou8732b072013-02-28 09:34:57 +0000887 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;
894
895 return tpm_cmd->cmd(cmdtp, flag, argc - 1, argv + 1);
Luigi Semenzatoeea3f4d2012-12-05 14:46:44 +0000896}
897
Che-liang Chiou8732b072013-02-28 09:34:57 +0000898U_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"
Simon Glassad776942015-08-22 18:31:40 -0600903" info - Show information about the TPM\n"
Che-liang Chiou8732b072013-02-28 09:34:57 +0000904" 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"
mario.six@gdsys.cc3d1df0e2017-03-20 10:28:30 +0100932#if defined(CONFIG_TPM_FLUSH_RESOURCES) || defined(CONFIG_TPM_LIST_RESOURCES)
Mario Six7690be32017-01-11 16:00:50 +0100933"Resource management functions\n"
mario.six@gdsys.cc3d1df0e2017-03-20 10:28:30 +0100934#endif
935#ifdef CONFIG_TPM_FLUSH_RESOURCES
Mario Six7690be32017-01-11 16:00:50 +0100936" 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 */
mario.six@gdsys.cc3d1df0e2017-03-20 10:28:30 +0100942#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 */
Reinhard Pfaube6c1522013-06-26 15:55:13 +0200948#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"
mario.six@gdsys.cc0f4b2ba2017-03-20 10:28:28 +0100954#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 */
Reinhard Pfaube6c1522013-06-26 15:55:13 +0200960" 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 */
Che-liang Chiou8732b072013-02-28 09:34:57 +0000964"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"
Reinhard Pfaube6c1522013-06-26 15:55:13 +0200974#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 */
Che-liang Chiou8732b072013-02-28 09:34:57 +0000981"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"
Luigi Semenzatoeea3f4d2012-12-05 14:46:44 +00001003);