blob: d9b433582c1a4cad5dfb401b230e6861fed6256d [file] [log] [blame]
Vadim Bendebury576fb1e2011-10-15 15:13:34 +00001/*
Che-liang Chiou8732b072013-02-28 09:34:57 +00002 * Copyright (c) 2013 The Chromium OS Authors.
Vadim Bendebury576fb1e2011-10-15 15:13:34 +00003 *
Wolfgang Denk1a459662013-07-08 09:37:19 +02004 * SPDX-License-Identifier: GPL-2.0+
Vadim Bendebury576fb1e2011-10-15 15:13:34 +00005 */
6
7#include <common.h>
8#include <command.h>
Simon Glassc8a8c512015-08-22 18:31:32 -06009#include <dm.h>
Che-liang Chiou8732b072013-02-28 09:34:57 +000010#include <malloc.h>
Vadim Bendebury576fb1e2011-10-15 15:13:34 +000011#include <tpm.h>
Che-liang Chiou8732b072013-02-28 09:34:57 +000012#include <asm/unaligned.h>
13#include <linux/string.h>
Vadim Bendebury576fb1e2011-10-15 15:13:34 +000014
Reinhard Pfaube6c1522013-06-26 15:55:13 +020015/* Useful constants */
16enum {
17 DIGEST_LENGTH = 20,
18 /* max lengths, valid for RSA keys <= 2048 bits */
19 TPM_PUBKEY_MAX_LENGTH = 288,
20};
21
Che-liang Chiou8732b072013-02-28 09:34:57 +000022/**
23 * Print a byte string in hexdecimal format, 16-bytes per line.
Vadim Bendebury576fb1e2011-10-15 15:13:34 +000024 *
Che-liang Chiou8732b072013-02-28 09:34:57 +000025 * @param data byte string to be printed
26 * @param count number of bytes to be printed
Vadim Bendebury576fb1e2011-10-15 15:13:34 +000027 */
Che-liang Chiou8732b072013-02-28 09:34:57 +000028static void print_byte_string(uint8_t *data, size_t count)
Vadim Bendebury576fb1e2011-10-15 15:13:34 +000029{
Che-liang Chiou8732b072013-02-28 09:34:57 +000030 int i, print_newline = 0;
Vadim Bendebury576fb1e2011-10-15 15:13:34 +000031
Che-liang Chiou8732b072013-02-28 09:34:57 +000032 for (i = 0; i < count; i++) {
33 printf(" %02x", data[i]);
34 print_newline = (i % 16 == 15);
35 if (print_newline)
36 putc('\n');
Vadim Bendebury576fb1e2011-10-15 15:13:34 +000037 }
Che-liang Chiou8732b072013-02-28 09:34:57 +000038 /* Avoid duplicated newline at the end */
39 if (!print_newline)
40 putc('\n');
Vadim Bendebury576fb1e2011-10-15 15:13:34 +000041}
42
Che-liang Chiou8732b072013-02-28 09:34:57 +000043/**
44 * Convert a text string of hexdecimal values into a byte string.
45 *
46 * @param bytes text string of hexdecimal values with no space
47 * between them
48 * @param data output buffer for byte string. The caller has to make
49 * sure it is large enough for storing the output. If
50 * NULL is passed, a large enough buffer will be allocated,
51 * and the caller must free it.
52 * @param count_ptr output variable for the length of byte string
53 * @return pointer to output buffer
54 */
55static void *parse_byte_string(char *bytes, uint8_t *data, size_t *count_ptr)
Luigi Semenzatoeea3f4d2012-12-05 14:46:44 +000056{
Che-liang Chiou8732b072013-02-28 09:34:57 +000057 char byte[3];
58 size_t count, length;
Luigi Semenzatoeea3f4d2012-12-05 14:46:44 +000059 int i;
Luigi Semenzatoeea3f4d2012-12-05 14:46:44 +000060
Simon Glass5c51d8a2015-08-22 18:31:36 -060061 if (!bytes)
62 return NULL;
Che-liang Chiou8732b072013-02-28 09:34:57 +000063 length = strlen(bytes);
64 count = length / 2;
Luigi Semenzatoeea3f4d2012-12-05 14:46:44 +000065
Che-liang Chiou8732b072013-02-28 09:34:57 +000066 if (!data)
67 data = malloc(count);
68 if (!data)
69 return NULL;
70
71 byte[2] = '\0';
72 for (i = 0; i < length; i += 2) {
73 byte[0] = bytes[i];
74 byte[1] = bytes[i + 1];
75 data[i / 2] = (uint8_t)simple_strtoul(byte, NULL, 16);
Luigi Semenzatoeea3f4d2012-12-05 14:46:44 +000076 }
Che-liang Chiou8732b072013-02-28 09:34:57 +000077
78 if (count_ptr)
79 *count_ptr = count;
80
81 return data;
Luigi Semenzatoeea3f4d2012-12-05 14:46:44 +000082}
83
Che-liang Chiou8732b072013-02-28 09:34:57 +000084/**
Simon Glassf8f1fe12015-08-22 18:31:34 -060085 * report_return_code() - Report any error and return failure or success
Che-liang Chiou8732b072013-02-28 09:34:57 +000086 *
87 * @param return_code TPM command return code
88 * @return value of enum command_ret_t
89 */
Simon Glassf8f1fe12015-08-22 18:31:34 -060090static int report_return_code(int return_code)
Vadim Bendebury576fb1e2011-10-15 15:13:34 +000091{
Simon Glassf8f1fe12015-08-22 18:31:34 -060092 if (return_code) {
93 printf("Error: %d\n", return_code);
Che-liang Chiou8732b072013-02-28 09:34:57 +000094 return CMD_RET_FAILURE;
Simon Glassf8f1fe12015-08-22 18:31:34 -060095 } else {
Che-liang Chiou8732b072013-02-28 09:34:57 +000096 return CMD_RET_SUCCESS;
Simon Glassf8f1fe12015-08-22 18:31:34 -060097 }
Vadim Bendebury576fb1e2011-10-15 15:13:34 +000098}
99
Che-liang Chiou8732b072013-02-28 09:34:57 +0000100/**
101 * Return number of values defined by a type string.
102 *
103 * @param type_str type string
104 * @return number of values of type string
105 */
106static int type_string_get_num_values(const char *type_str)
107{
108 return strlen(type_str);
109}
110
111/**
112 * Return total size of values defined by a type string.
113 *
114 * @param type_str type string
115 * @return total size of values of type string, or 0 if type string
116 * contains illegal type character.
117 */
118static size_t type_string_get_space_size(const char *type_str)
119{
120 size_t size;
121
122 for (size = 0; *type_str; type_str++) {
123 switch (*type_str) {
124 case 'b':
125 size += 1;
126 break;
127 case 'w':
128 size += 2;
129 break;
130 case 'd':
131 size += 4;
132 break;
133 default:
134 return 0;
135 }
136 }
137
138 return size;
139}
140
141/**
142 * Allocate a buffer large enough to hold values defined by a type
143 * string. The caller has to free the buffer.
144 *
145 * @param type_str type string
146 * @param count pointer for storing size of buffer
147 * @return pointer to buffer or NULL on error
148 */
149static void *type_string_alloc(const char *type_str, uint32_t *count)
150{
151 void *data;
152 size_t size;
153
154 size = type_string_get_space_size(type_str);
155 if (!size)
156 return NULL;
157 data = malloc(size);
158 if (data)
159 *count = size;
160
161 return data;
162}
163
164/**
165 * Pack values defined by a type string into a buffer. The buffer must have
166 * large enough space.
167 *
168 * @param type_str type string
169 * @param values text strings of values to be packed
170 * @param data output buffer of values
171 * @return 0 on success, non-0 on error
172 */
173static int type_string_pack(const char *type_str, char * const values[],
174 uint8_t *data)
175{
176 size_t offset;
177 uint32_t value;
178
179 for (offset = 0; *type_str; type_str++, values++) {
180 value = simple_strtoul(values[0], NULL, 0);
181 switch (*type_str) {
182 case 'b':
183 data[offset] = value;
184 offset += 1;
185 break;
186 case 'w':
187 put_unaligned_be16(value, data + offset);
188 offset += 2;
189 break;
190 case 'd':
191 put_unaligned_be32(value, data + offset);
192 offset += 4;
193 break;
194 default:
195 return -1;
196 }
197 }
198
199 return 0;
200}
201
202/**
203 * Read values defined by a type string from a buffer, and write these values
204 * to environment variables.
205 *
206 * @param type_str type string
207 * @param data input buffer of values
208 * @param vars names of environment variables
209 * @return 0 on success, non-0 on error
210 */
211static int type_string_write_vars(const char *type_str, uint8_t *data,
212 char * const vars[])
213{
214 size_t offset;
215 uint32_t value;
216
217 for (offset = 0; *type_str; type_str++, vars++) {
218 switch (*type_str) {
219 case 'b':
220 value = data[offset];
221 offset += 1;
222 break;
223 case 'w':
224 value = get_unaligned_be16(data + offset);
225 offset += 2;
226 break;
227 case 'd':
228 value = get_unaligned_be32(data + offset);
229 offset += 4;
230 break;
231 default:
232 return -1;
233 }
Simon Glass018f5302017-08-03 12:22:10 -0600234 if (env_set_ulong(*vars, value))
Che-liang Chiou8732b072013-02-28 09:34:57 +0000235 return -1;
236 }
237
238 return 0;
239}
240
241static int do_tpm_startup(cmd_tbl_t *cmdtp, int flag,
242 int argc, char * const argv[])
243{
244 enum tpm_startup_type mode;
245
246 if (argc != 2)
247 return CMD_RET_USAGE;
248 if (!strcasecmp("TPM_ST_CLEAR", argv[1])) {
249 mode = TPM_ST_CLEAR;
250 } else if (!strcasecmp("TPM_ST_STATE", argv[1])) {
251 mode = TPM_ST_STATE;
252 } else if (!strcasecmp("TPM_ST_DEACTIVATED", argv[1])) {
253 mode = TPM_ST_DEACTIVATED;
254 } else {
255 printf("Couldn't recognize mode string: %s\n", argv[1]);
256 return CMD_RET_FAILURE;
257 }
258
Simon Glassf8f1fe12015-08-22 18:31:34 -0600259 return report_return_code(tpm_startup(mode));
Che-liang Chiou8732b072013-02-28 09:34:57 +0000260}
261
262static int do_tpm_nv_define_space(cmd_tbl_t *cmdtp, int flag,
263 int argc, char * const argv[])
264{
265 uint32_t index, perm, size;
266
267 if (argc != 4)
268 return CMD_RET_USAGE;
269 index = simple_strtoul(argv[1], NULL, 0);
270 perm = simple_strtoul(argv[2], NULL, 0);
271 size = simple_strtoul(argv[3], NULL, 0);
272
Simon Glassf8f1fe12015-08-22 18:31:34 -0600273 return report_return_code(tpm_nv_define_space(index, perm, size));
Che-liang Chiou8732b072013-02-28 09:34:57 +0000274}
275
276static int do_tpm_nv_read_value(cmd_tbl_t *cmdtp, int flag,
277 int argc, char * const argv[])
278{
279 uint32_t index, count, rc;
280 void *data;
281
282 if (argc != 4)
283 return CMD_RET_USAGE;
284 index = simple_strtoul(argv[1], NULL, 0);
285 data = (void *)simple_strtoul(argv[2], NULL, 0);
286 count = simple_strtoul(argv[3], NULL, 0);
287
288 rc = tpm_nv_read_value(index, data, count);
289 if (!rc) {
290 puts("area content:\n");
291 print_byte_string(data, count);
292 }
293
Simon Glassf8f1fe12015-08-22 18:31:34 -0600294 return report_return_code(rc);
Che-liang Chiou8732b072013-02-28 09:34:57 +0000295}
296
297static int do_tpm_nv_write_value(cmd_tbl_t *cmdtp, int flag,
298 int argc, char * const argv[])
299{
300 uint32_t index, rc;
301 size_t count;
302 void *data;
303
304 if (argc != 3)
305 return CMD_RET_USAGE;
306 index = simple_strtoul(argv[1], NULL, 0);
307 data = parse_byte_string(argv[2], NULL, &count);
308 if (!data) {
309 printf("Couldn't parse byte string %s\n", argv[2]);
310 return CMD_RET_FAILURE;
311 }
312
313 rc = tpm_nv_write_value(index, data, count);
314 free(data);
315
Simon Glassf8f1fe12015-08-22 18:31:34 -0600316 return report_return_code(rc);
Che-liang Chiou8732b072013-02-28 09:34:57 +0000317}
318
319static int do_tpm_extend(cmd_tbl_t *cmdtp, int flag,
320 int argc, char * const argv[])
321{
322 uint32_t index, rc;
323 uint8_t in_digest[20], out_digest[20];
324
325 if (argc != 3)
326 return CMD_RET_USAGE;
327 index = simple_strtoul(argv[1], NULL, 0);
328 if (!parse_byte_string(argv[2], in_digest, NULL)) {
329 printf("Couldn't parse byte string %s\n", argv[2]);
330 return CMD_RET_FAILURE;
331 }
332
333 rc = tpm_extend(index, in_digest, out_digest);
334 if (!rc) {
335 puts("PCR value after execution of the command:\n");
336 print_byte_string(out_digest, sizeof(out_digest));
337 }
338
Simon Glassf8f1fe12015-08-22 18:31:34 -0600339 return report_return_code(rc);
Che-liang Chiou8732b072013-02-28 09:34:57 +0000340}
341
342static int do_tpm_pcr_read(cmd_tbl_t *cmdtp, int flag,
343 int argc, char * const argv[])
344{
345 uint32_t index, count, rc;
346 void *data;
347
348 if (argc != 4)
349 return CMD_RET_USAGE;
350 index = simple_strtoul(argv[1], NULL, 0);
351 data = (void *)simple_strtoul(argv[2], NULL, 0);
352 count = simple_strtoul(argv[3], NULL, 0);
353
354 rc = tpm_pcr_read(index, data, count);
355 if (!rc) {
356 puts("Named PCR content:\n");
357 print_byte_string(data, count);
358 }
359
Simon Glassf8f1fe12015-08-22 18:31:34 -0600360 return report_return_code(rc);
Che-liang Chiou8732b072013-02-28 09:34:57 +0000361}
362
363static int do_tpm_tsc_physical_presence(cmd_tbl_t *cmdtp, int flag,
364 int argc, char * const argv[])
365{
366 uint16_t presence;
367
368 if (argc != 2)
369 return CMD_RET_USAGE;
370 presence = (uint16_t)simple_strtoul(argv[1], NULL, 0);
371
Simon Glassf8f1fe12015-08-22 18:31:34 -0600372 return report_return_code(tpm_tsc_physical_presence(presence));
Che-liang Chiou8732b072013-02-28 09:34:57 +0000373}
374
375static int do_tpm_read_pubek(cmd_tbl_t *cmdtp, int flag,
376 int argc, char * const argv[])
377{
378 uint32_t count, rc;
379 void *data;
380
381 if (argc != 3)
382 return CMD_RET_USAGE;
383 data = (void *)simple_strtoul(argv[1], NULL, 0);
384 count = simple_strtoul(argv[2], NULL, 0);
385
386 rc = tpm_read_pubek(data, count);
387 if (!rc) {
388 puts("pubek value:\n");
389 print_byte_string(data, count);
390 }
391
Simon Glassf8f1fe12015-08-22 18:31:34 -0600392 return report_return_code(rc);
Che-liang Chiou8732b072013-02-28 09:34:57 +0000393}
394
395static int do_tpm_physical_set_deactivated(cmd_tbl_t *cmdtp, int flag,
396 int argc, char * const argv[])
397{
398 uint8_t state;
399
400 if (argc != 2)
401 return CMD_RET_USAGE;
402 state = (uint8_t)simple_strtoul(argv[1], NULL, 0);
403
Simon Glassf8f1fe12015-08-22 18:31:34 -0600404 return report_return_code(tpm_physical_set_deactivated(state));
Che-liang Chiou8732b072013-02-28 09:34:57 +0000405}
406
407static int do_tpm_get_capability(cmd_tbl_t *cmdtp, int flag,
408 int argc, char * const argv[])
409{
410 uint32_t cap_area, sub_cap, rc;
411 void *cap;
412 size_t count;
413
414 if (argc != 5)
415 return CMD_RET_USAGE;
416 cap_area = simple_strtoul(argv[1], NULL, 0);
417 sub_cap = simple_strtoul(argv[2], NULL, 0);
418 cap = (void *)simple_strtoul(argv[3], NULL, 0);
419 count = simple_strtoul(argv[4], NULL, 0);
420
421 rc = tpm_get_capability(cap_area, sub_cap, cap, count);
422 if (!rc) {
423 puts("capability information:\n");
424 print_byte_string(cap, count);
425 }
426
Simon Glassf8f1fe12015-08-22 18:31:34 -0600427 return report_return_code(rc);
Che-liang Chiou8732b072013-02-28 09:34:57 +0000428}
429
430#define TPM_COMMAND_NO_ARG(cmd) \
431static int do_##cmd(cmd_tbl_t *cmdtp, int flag, \
432 int argc, char * const argv[]) \
433{ \
434 if (argc != 1) \
435 return CMD_RET_USAGE; \
Simon Glassf8f1fe12015-08-22 18:31:34 -0600436 return report_return_code(cmd()); \
Che-liang Chiou8732b072013-02-28 09:34:57 +0000437}
438
439TPM_COMMAND_NO_ARG(tpm_init)
440TPM_COMMAND_NO_ARG(tpm_self_test_full)
441TPM_COMMAND_NO_ARG(tpm_continue_self_test)
442TPM_COMMAND_NO_ARG(tpm_force_clear)
443TPM_COMMAND_NO_ARG(tpm_physical_enable)
444TPM_COMMAND_NO_ARG(tpm_physical_disable)
445
Simon Glassc8a8c512015-08-22 18:31:32 -0600446static int get_tpm(struct udevice **devp)
447{
448 int rc;
449
Simon Glass3f603cb2016-02-11 13:23:26 -0700450 rc = uclass_first_device_err(UCLASS_TPM, devp);
451 if (rc) {
Simon Glassc8a8c512015-08-22 18:31:32 -0600452 printf("Could not find TPM (ret=%d)\n", rc);
453 return CMD_RET_FAILURE;
454 }
455
456 return 0;
457}
Simon Glassad776942015-08-22 18:31:40 -0600458
459static int do_tpm_info(cmd_tbl_t *cmdtp, int flag, int argc,
460 char *const argv[])
461{
462 struct udevice *dev;
463 char buf[80];
464 int rc;
465
466 rc = get_tpm(&dev);
467 if (rc)
468 return rc;
469 rc = tpm_get_desc(dev, buf, sizeof(buf));
470 if (rc < 0) {
471 printf("Couldn't get TPM info (%d)\n", rc);
472 return CMD_RET_FAILURE;
473 }
474 printf("%s\n", buf);
475
476 return 0;
477}
Simon Glassc8a8c512015-08-22 18:31:32 -0600478
Che-liang Chiou8732b072013-02-28 09:34:57 +0000479static int do_tpm_raw_transfer(cmd_tbl_t *cmdtp, int flag,
480 int argc, char * const argv[])
481{
Christophe Ricardc2b0f602015-10-06 22:54:43 +0200482 struct udevice *dev;
Che-liang Chiou8732b072013-02-28 09:34:57 +0000483 void *command;
484 uint8_t response[1024];
485 size_t count, response_length = sizeof(response);
486 uint32_t rc;
487
488 command = parse_byte_string(argv[1], NULL, &count);
489 if (!command) {
490 printf("Couldn't parse byte string %s\n", argv[1]);
491 return CMD_RET_FAILURE;
492 }
493
Simon Glassc8a8c512015-08-22 18:31:32 -0600494 rc = get_tpm(&dev);
495 if (rc)
496 return rc;
497
498 rc = tpm_xfer(dev, command, count, response, &response_length);
Che-liang Chiou8732b072013-02-28 09:34:57 +0000499 free(command);
500 if (!rc) {
501 puts("tpm response:\n");
502 print_byte_string(response, response_length);
503 }
504
Simon Glassf8f1fe12015-08-22 18:31:34 -0600505 return report_return_code(rc);
Che-liang Chiou8732b072013-02-28 09:34:57 +0000506}
507
508static int do_tpm_nv_define(cmd_tbl_t *cmdtp, int flag,
509 int argc, char * const argv[])
510{
511 uint32_t index, perm, size;
512
513 if (argc != 4)
514 return CMD_RET_USAGE;
515 size = type_string_get_space_size(argv[1]);
516 if (!size) {
517 printf("Couldn't parse arguments\n");
518 return CMD_RET_USAGE;
519 }
520 index = simple_strtoul(argv[2], NULL, 0);
521 perm = simple_strtoul(argv[3], NULL, 0);
522
Simon Glassf8f1fe12015-08-22 18:31:34 -0600523 return report_return_code(tpm_nv_define_space(index, perm, size));
Che-liang Chiou8732b072013-02-28 09:34:57 +0000524}
525
526static int do_tpm_nv_read(cmd_tbl_t *cmdtp, int flag,
527 int argc, char * const argv[])
528{
529 uint32_t index, count, err;
530 void *data;
531
532 if (argc < 3)
533 return CMD_RET_USAGE;
534 if (argc != 3 + type_string_get_num_values(argv[1]))
535 return CMD_RET_USAGE;
536 index = simple_strtoul(argv[2], NULL, 0);
537 data = type_string_alloc(argv[1], &count);
538 if (!data) {
539 printf("Couldn't parse arguments\n");
540 return CMD_RET_USAGE;
541 }
542
543 err = tpm_nv_read_value(index, data, count);
544 if (!err) {
545 if (type_string_write_vars(argv[1], data, argv + 3)) {
546 printf("Couldn't write to variables\n");
547 err = ~0;
548 }
549 }
550 free(data);
551
Simon Glassf8f1fe12015-08-22 18:31:34 -0600552 return report_return_code(err);
Che-liang Chiou8732b072013-02-28 09:34:57 +0000553}
554
555static int do_tpm_nv_write(cmd_tbl_t *cmdtp, int flag,
556 int argc, char * const argv[])
557{
558 uint32_t index, count, err;
559 void *data;
560
561 if (argc < 3)
562 return CMD_RET_USAGE;
563 if (argc != 3 + type_string_get_num_values(argv[1]))
564 return CMD_RET_USAGE;
565 index = simple_strtoul(argv[2], NULL, 0);
566 data = type_string_alloc(argv[1], &count);
567 if (!data) {
568 printf("Couldn't parse arguments\n");
569 return CMD_RET_USAGE;
570 }
571 if (type_string_pack(argv[1], argv + 3, data)) {
572 printf("Couldn't parse arguments\n");
573 free(data);
574 return CMD_RET_USAGE;
575 }
576
577 err = tpm_nv_write_value(index, data, count);
578 free(data);
579
Simon Glassf8f1fe12015-08-22 18:31:34 -0600580 return report_return_code(err);
Che-liang Chiou8732b072013-02-28 09:34:57 +0000581}
582
Reinhard Pfaube6c1522013-06-26 15:55:13 +0200583#ifdef CONFIG_TPM_AUTH_SESSIONS
584
585static int do_tpm_oiap(cmd_tbl_t *cmdtp, int flag,
586 int argc, char * const argv[])
587{
588 uint32_t auth_handle, err;
589
590 err = tpm_oiap(&auth_handle);
591
Simon Glassf8f1fe12015-08-22 18:31:34 -0600592 return report_return_code(err);
Reinhard Pfaube6c1522013-06-26 15:55:13 +0200593}
594
mario.six@gdsys.cc0f4b2ba2017-03-20 10:28:28 +0100595#ifdef CONFIG_TPM_LOAD_KEY_BY_SHA1
596static int do_tpm_load_key_by_sha1(cmd_tbl_t *cmdtp, int flag, int argc, char *
597 const argv[])
598{
599 uint32_t parent_handle = 0;
600 uint32_t key_len, key_handle, err;
601 uint8_t usage_auth[DIGEST_LENGTH];
602 uint8_t parent_hash[DIGEST_LENGTH];
603 void *key;
604
605 if (argc < 5)
606 return CMD_RET_USAGE;
607
608 parse_byte_string(argv[1], parent_hash, NULL);
609 key = (void *)simple_strtoul(argv[2], NULL, 0);
610 key_len = simple_strtoul(argv[3], NULL, 0);
611 if (strlen(argv[4]) != 2 * DIGEST_LENGTH)
612 return CMD_RET_FAILURE;
613 parse_byte_string(argv[4], usage_auth, NULL);
614
615 err = tpm_find_key_sha1(usage_auth, parent_hash, &parent_handle);
616 if (err) {
617 printf("Could not find matching parent key (err = %d)\n", err);
618 return CMD_RET_FAILURE;
619 }
620
621 printf("Found parent key %08x\n", parent_handle);
622
623 err = tpm_load_key2_oiap(parent_handle, key, key_len, usage_auth,
624 &key_handle);
625 if (!err) {
626 printf("Key handle is 0x%x\n", key_handle);
Simon Glass018f5302017-08-03 12:22:10 -0600627 env_set_hex("key_handle", key_handle);
mario.six@gdsys.cc0f4b2ba2017-03-20 10:28:28 +0100628 }
629
630 return report_return_code(err);
631}
632#endif /* CONFIG_TPM_LOAD_KEY_BY_SHA1 */
633
Reinhard Pfaube6c1522013-06-26 15:55:13 +0200634static int do_tpm_load_key2_oiap(cmd_tbl_t *cmdtp, int flag,
635 int argc, char * const argv[])
636{
637 uint32_t parent_handle, key_len, key_handle, err;
638 uint8_t usage_auth[DIGEST_LENGTH];
639 void *key;
640
641 if (argc < 5)
642 return CMD_RET_USAGE;
643
644 parent_handle = simple_strtoul(argv[1], NULL, 0);
645 key = (void *)simple_strtoul(argv[2], NULL, 0);
646 key_len = simple_strtoul(argv[3], NULL, 0);
647 if (strlen(argv[4]) != 2 * DIGEST_LENGTH)
648 return CMD_RET_FAILURE;
649 parse_byte_string(argv[4], usage_auth, NULL);
650
651 err = tpm_load_key2_oiap(parent_handle, key, key_len, usage_auth,
652 &key_handle);
653 if (!err)
654 printf("Key handle is 0x%x\n", key_handle);
655
Simon Glassf8f1fe12015-08-22 18:31:34 -0600656 return report_return_code(err);
Reinhard Pfaube6c1522013-06-26 15:55:13 +0200657}
658
659static int do_tpm_get_pub_key_oiap(cmd_tbl_t *cmdtp, int flag,
660 int argc, char * const argv[])
661{
662 uint32_t key_handle, err;
663 uint8_t usage_auth[DIGEST_LENGTH];
664 uint8_t pub_key_buffer[TPM_PUBKEY_MAX_LENGTH];
665 size_t pub_key_len = sizeof(pub_key_buffer);
666
667 if (argc < 3)
668 return CMD_RET_USAGE;
669
670 key_handle = simple_strtoul(argv[1], NULL, 0);
671 if (strlen(argv[2]) != 2 * DIGEST_LENGTH)
672 return CMD_RET_FAILURE;
673 parse_byte_string(argv[2], usage_auth, NULL);
674
675 err = tpm_get_pub_key_oiap(key_handle, usage_auth,
676 pub_key_buffer, &pub_key_len);
677 if (!err) {
678 printf("dump of received pub key structure:\n");
679 print_byte_string(pub_key_buffer, pub_key_len);
680 }
Simon Glassf8f1fe12015-08-22 18:31:34 -0600681 return report_return_code(err);
Reinhard Pfaube6c1522013-06-26 15:55:13 +0200682}
683
684TPM_COMMAND_NO_ARG(tpm_end_oiap)
685
686#endif /* CONFIG_TPM_AUTH_SESSIONS */
687
Mario Six7690be32017-01-11 16:00:50 +0100688#ifdef CONFIG_TPM_FLUSH_RESOURCES
689static int do_tpm_flush(cmd_tbl_t *cmdtp, int flag, int argc,
690 char * const argv[])
691{
692 int type = 0;
693
mario.six@gdsys.cc1c08b212017-03-20 10:28:29 +0100694 if (argc != 3)
Mario Six7690be32017-01-11 16:00:50 +0100695 return CMD_RET_USAGE;
696
mario.six@gdsys.cc1c08b212017-03-20 10:28:29 +0100697 if (!strcasecmp(argv[1], "key"))
Mario Six7690be32017-01-11 16:00:50 +0100698 type = TPM_RT_KEY;
mario.six@gdsys.cc1c08b212017-03-20 10:28:29 +0100699 else if (!strcasecmp(argv[1], "auth"))
Mario Six7690be32017-01-11 16:00:50 +0100700 type = TPM_RT_AUTH;
mario.six@gdsys.cc1c08b212017-03-20 10:28:29 +0100701 else if (!strcasecmp(argv[1], "hash"))
Mario Six7690be32017-01-11 16:00:50 +0100702 type = TPM_RT_HASH;
mario.six@gdsys.cc1c08b212017-03-20 10:28:29 +0100703 else if (!strcasecmp(argv[1], "trans"))
Mario Six7690be32017-01-11 16:00:50 +0100704 type = TPM_RT_TRANS;
mario.six@gdsys.cc1c08b212017-03-20 10:28:29 +0100705 else if (!strcasecmp(argv[1], "context"))
Mario Six7690be32017-01-11 16:00:50 +0100706 type = TPM_RT_CONTEXT;
mario.six@gdsys.cc1c08b212017-03-20 10:28:29 +0100707 else if (!strcasecmp(argv[1], "counter"))
Mario Six7690be32017-01-11 16:00:50 +0100708 type = TPM_RT_COUNTER;
mario.six@gdsys.cc1c08b212017-03-20 10:28:29 +0100709 else if (!strcasecmp(argv[1], "delegate"))
Mario Six7690be32017-01-11 16:00:50 +0100710 type = TPM_RT_DELEGATE;
mario.six@gdsys.cc1c08b212017-03-20 10:28:29 +0100711 else if (!strcasecmp(argv[1], "daa_tpm"))
Mario Six7690be32017-01-11 16:00:50 +0100712 type = TPM_RT_DAA_TPM;
mario.six@gdsys.cc1c08b212017-03-20 10:28:29 +0100713 else if (!strcasecmp(argv[1], "daa_v0"))
Mario Six7690be32017-01-11 16:00:50 +0100714 type = TPM_RT_DAA_V0;
mario.six@gdsys.cc1c08b212017-03-20 10:28:29 +0100715 else if (!strcasecmp(argv[1], "daa_v1"))
Mario Six7690be32017-01-11 16:00:50 +0100716 type = TPM_RT_DAA_V1;
717
mario.six@gdsys.cc1c08b212017-03-20 10:28:29 +0100718 if (!type) {
719 printf("Resource type %s unknown.\n", argv[1]);
720 return -1;
721 }
722
723 if (!strcasecmp(argv[2], "all")) {
Mario Six7690be32017-01-11 16:00:50 +0100724 uint16_t res_count;
725 uint8_t buf[288];
726 uint8_t *ptr;
727 int err;
728 uint i;
729
730 /* fetch list of already loaded resources in the TPM */
731 err = tpm_get_capability(TPM_CAP_HANDLE, type, buf,
732 sizeof(buf));
mario.six@gdsys.cc1c08b212017-03-20 10:28:29 +0100733 if (err) {
734 printf("tpm_get_capability returned error %d.\n", err);
Mario Six7690be32017-01-11 16:00:50 +0100735 return -1;
mario.six@gdsys.cc1c08b212017-03-20 10:28:29 +0100736 }
Mario Six7690be32017-01-11 16:00:50 +0100737 res_count = get_unaligned_be16(buf);
738 ptr = buf + 2;
739 for (i = 0; i < res_count; ++i, ptr += 4)
740 tpm_flush_specific(get_unaligned_be32(ptr), type);
741 } else {
742 uint32_t handle = simple_strtoul(argv[2], NULL, 0);
743
mario.six@gdsys.cc1c08b212017-03-20 10:28:29 +0100744 if (!handle) {
745 printf("Illegal resource handle %s\n", argv[2]);
Mario Six7690be32017-01-11 16:00:50 +0100746 return -1;
mario.six@gdsys.cc1c08b212017-03-20 10:28:29 +0100747 }
Mario Six7690be32017-01-11 16:00:50 +0100748 tpm_flush_specific(cpu_to_be32(handle), type);
749 }
750
751 return 0;
752}
753#endif /* CONFIG_TPM_FLUSH_RESOURCES */
754
mario.six@gdsys.cc3d1df0e2017-03-20 10:28:30 +0100755#ifdef CONFIG_TPM_LIST_RESOURCES
756static int do_tpm_list(cmd_tbl_t *cmdtp, int flag, int argc,
757 char * const argv[])
758{
759 int type = 0;
760 uint16_t res_count;
761 uint8_t buf[288];
762 uint8_t *ptr;
763 int err;
764 uint i;
765
766 if (argc != 2)
767 return CMD_RET_USAGE;
768
769 if (!strcasecmp(argv[1], "key"))
770 type = TPM_RT_KEY;
771 else if (!strcasecmp(argv[1], "auth"))
772 type = TPM_RT_AUTH;
773 else if (!strcasecmp(argv[1], "hash"))
774 type = TPM_RT_HASH;
775 else if (!strcasecmp(argv[1], "trans"))
776 type = TPM_RT_TRANS;
777 else if (!strcasecmp(argv[1], "context"))
778 type = TPM_RT_CONTEXT;
779 else if (!strcasecmp(argv[1], "counter"))
780 type = TPM_RT_COUNTER;
781 else if (!strcasecmp(argv[1], "delegate"))
782 type = TPM_RT_DELEGATE;
783 else if (!strcasecmp(argv[1], "daa_tpm"))
784 type = TPM_RT_DAA_TPM;
785 else if (!strcasecmp(argv[1], "daa_v0"))
786 type = TPM_RT_DAA_V0;
787 else if (!strcasecmp(argv[1], "daa_v1"))
788 type = TPM_RT_DAA_V1;
789
790 if (!type) {
791 printf("Resource type %s unknown.\n", argv[1]);
792 return -1;
793 }
794
795 /* fetch list of already loaded resources in the TPM */
796 err = tpm_get_capability(TPM_CAP_HANDLE, type, buf,
797 sizeof(buf));
798 if (err) {
799 printf("tpm_get_capability returned error %d.\n", err);
800 return -1;
801 }
802 res_count = get_unaligned_be16(buf);
803 ptr = buf + 2;
804
805 printf("Resources of type %s (%02x):\n", argv[1], type);
806 if (!res_count) {
807 puts("None\n");
808 } else {
809 for (i = 0; i < res_count; ++i, ptr += 4)
810 printf("Index %d: %08x\n", i, get_unaligned_be32(ptr));
811 }
812
813 return 0;
814}
815#endif /* CONFIG_TPM_LIST_RESOURCES */
816
Che-liang Chiou8732b072013-02-28 09:34:57 +0000817#define MAKE_TPM_CMD_ENTRY(cmd) \
818 U_BOOT_CMD_MKENT(cmd, 0, 1, do_tpm_ ## cmd, "", "")
819
820static cmd_tbl_t tpm_commands[] = {
Simon Glassad776942015-08-22 18:31:40 -0600821 U_BOOT_CMD_MKENT(info, 0, 1, do_tpm_info, "", ""),
Che-liang Chiou8732b072013-02-28 09:34:57 +0000822 U_BOOT_CMD_MKENT(init, 0, 1,
823 do_tpm_init, "", ""),
824 U_BOOT_CMD_MKENT(startup, 0, 1,
825 do_tpm_startup, "", ""),
826 U_BOOT_CMD_MKENT(self_test_full, 0, 1,
827 do_tpm_self_test_full, "", ""),
828 U_BOOT_CMD_MKENT(continue_self_test, 0, 1,
829 do_tpm_continue_self_test, "", ""),
830 U_BOOT_CMD_MKENT(force_clear, 0, 1,
831 do_tpm_force_clear, "", ""),
832 U_BOOT_CMD_MKENT(physical_enable, 0, 1,
833 do_tpm_physical_enable, "", ""),
834 U_BOOT_CMD_MKENT(physical_disable, 0, 1,
835 do_tpm_physical_disable, "", ""),
836 U_BOOT_CMD_MKENT(nv_define_space, 0, 1,
837 do_tpm_nv_define_space, "", ""),
838 U_BOOT_CMD_MKENT(nv_read_value, 0, 1,
839 do_tpm_nv_read_value, "", ""),
840 U_BOOT_CMD_MKENT(nv_write_value, 0, 1,
841 do_tpm_nv_write_value, "", ""),
842 U_BOOT_CMD_MKENT(extend, 0, 1,
843 do_tpm_extend, "", ""),
844 U_BOOT_CMD_MKENT(pcr_read, 0, 1,
845 do_tpm_pcr_read, "", ""),
846 U_BOOT_CMD_MKENT(tsc_physical_presence, 0, 1,
847 do_tpm_tsc_physical_presence, "", ""),
848 U_BOOT_CMD_MKENT(read_pubek, 0, 1,
849 do_tpm_read_pubek, "", ""),
850 U_BOOT_CMD_MKENT(physical_set_deactivated, 0, 1,
851 do_tpm_physical_set_deactivated, "", ""),
852 U_BOOT_CMD_MKENT(get_capability, 0, 1,
853 do_tpm_get_capability, "", ""),
854 U_BOOT_CMD_MKENT(raw_transfer, 0, 1,
855 do_tpm_raw_transfer, "", ""),
856 U_BOOT_CMD_MKENT(nv_define, 0, 1,
857 do_tpm_nv_define, "", ""),
858 U_BOOT_CMD_MKENT(nv_read, 0, 1,
859 do_tpm_nv_read, "", ""),
860 U_BOOT_CMD_MKENT(nv_write, 0, 1,
861 do_tpm_nv_write, "", ""),
Reinhard Pfaube6c1522013-06-26 15:55:13 +0200862#ifdef CONFIG_TPM_AUTH_SESSIONS
863 U_BOOT_CMD_MKENT(oiap, 0, 1,
864 do_tpm_oiap, "", ""),
865 U_BOOT_CMD_MKENT(end_oiap, 0, 1,
866 do_tpm_end_oiap, "", ""),
867 U_BOOT_CMD_MKENT(load_key2_oiap, 0, 1,
868 do_tpm_load_key2_oiap, "", ""),
mario.six@gdsys.cc0f4b2ba2017-03-20 10:28:28 +0100869#ifdef CONFIG_TPM_LOAD_KEY_BY_SHA1
870 U_BOOT_CMD_MKENT(load_key_by_sha1, 0, 1,
871 do_tpm_load_key_by_sha1, "", ""),
872#endif /* CONFIG_TPM_LOAD_KEY_BY_SHA1 */
Reinhard Pfaube6c1522013-06-26 15:55:13 +0200873 U_BOOT_CMD_MKENT(get_pub_key_oiap, 0, 1,
874 do_tpm_get_pub_key_oiap, "", ""),
875#endif /* CONFIG_TPM_AUTH_SESSIONS */
Mario Six7690be32017-01-11 16:00:50 +0100876#ifdef CONFIG_TPM_FLUSH_RESOURCES
877 U_BOOT_CMD_MKENT(flush, 0, 1,
878 do_tpm_flush, "", ""),
879#endif /* CONFIG_TPM_FLUSH_RESOURCES */
mario.six@gdsys.cc3d1df0e2017-03-20 10:28:30 +0100880#ifdef CONFIG_TPM_LIST_RESOURCES
881 U_BOOT_CMD_MKENT(list, 0, 1,
882 do_tpm_list, "", ""),
883#endif /* CONFIG_TPM_LIST_RESOURCES */
Che-liang Chiou8732b072013-02-28 09:34:57 +0000884};
Luigi Semenzatoeea3f4d2012-12-05 14:46:44 +0000885
886static int do_tpm(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
887{
Che-liang Chiou8732b072013-02-28 09:34:57 +0000888 cmd_tbl_t *tpm_cmd;
889
890 if (argc < 2)
891 return CMD_RET_USAGE;
892 tpm_cmd = find_cmd_tbl(argv[1], tpm_commands, ARRAY_SIZE(tpm_commands));
893 if (!tpm_cmd)
894 return CMD_RET_USAGE;
895
896 return tpm_cmd->cmd(cmdtp, flag, argc - 1, argv + 1);
Luigi Semenzatoeea3f4d2012-12-05 14:46:44 +0000897}
898
Che-liang Chiou8732b072013-02-28 09:34:57 +0000899U_BOOT_CMD(tpm, CONFIG_SYS_MAXARGS, 1, do_tpm,
900"Issue a TPM command",
901"cmd args...\n"
902" - Issue TPM command <cmd> with arguments <args...>.\n"
903"Admin Startup and State Commands:\n"
Simon Glassad776942015-08-22 18:31:40 -0600904" info - Show information about the TPM\n"
Che-liang Chiou8732b072013-02-28 09:34:57 +0000905" init\n"
906" - Put TPM into a state where it waits for 'startup' command.\n"
907" startup mode\n"
908" - Issue TPM_Starup command. <mode> is one of TPM_ST_CLEAR,\n"
909" TPM_ST_STATE, and TPM_ST_DEACTIVATED.\n"
910"Admin Testing Commands:\n"
911" self_test_full\n"
912" - Test all of the TPM capabilities.\n"
913" continue_self_test\n"
914" - Inform TPM that it should complete the self-test.\n"
915"Admin Opt-in Commands:\n"
916" physical_enable\n"
917" - Set the PERMANENT disable flag to FALSE using physical presence as\n"
918" authorization.\n"
919" physical_disable\n"
920" - Set the PERMANENT disable flag to TRUE using physical presence as\n"
921" authorization.\n"
922" physical_set_deactivated 0|1\n"
923" - Set deactivated flag.\n"
924"Admin Ownership Commands:\n"
925" force_clear\n"
926" - Issue TPM_ForceClear command.\n"
927" tsc_physical_presence flags\n"
928" - Set TPM device's Physical Presence flags to <flags>.\n"
929"The Capability Commands:\n"
930" get_capability cap_area sub_cap addr count\n"
931" - Read <count> bytes of TPM capability indexed by <cap_area> and\n"
932" <sub_cap> to memory address <addr>.\n"
mario.six@gdsys.cc3d1df0e2017-03-20 10:28:30 +0100933#if defined(CONFIG_TPM_FLUSH_RESOURCES) || defined(CONFIG_TPM_LIST_RESOURCES)
Mario Six7690be32017-01-11 16:00:50 +0100934"Resource management functions\n"
mario.six@gdsys.cc3d1df0e2017-03-20 10:28:30 +0100935#endif
936#ifdef CONFIG_TPM_FLUSH_RESOURCES
Mario Six7690be32017-01-11 16:00:50 +0100937" flush resource_type id\n"
938" - flushes a resource of type <resource_type> (may be one of key, auth,\n"
939" hash, trans, context, counter, delegate, daa_tpm, daa_v0, daa_v1),\n"
940" and id <id> from the TPM. Use an <id> of \"all\" to flush all\n"
941" resources of that type.\n"
942#endif /* CONFIG_TPM_FLUSH_RESOURCES */
mario.six@gdsys.cc3d1df0e2017-03-20 10:28:30 +0100943#ifdef CONFIG_TPM_LIST_RESOURCES
944" list resource_type\n"
945" - lists resources of type <resource_type> (may be one of key, auth,\n"
946" hash, trans, context, counter, delegate, daa_tpm, daa_v0, daa_v1),\n"
947" contained in the TPM.\n"
948#endif /* CONFIG_TPM_LIST_RESOURCES */
Reinhard Pfaube6c1522013-06-26 15:55:13 +0200949#ifdef CONFIG_TPM_AUTH_SESSIONS
950"Storage functions\n"
951" loadkey2_oiap parent_handle key_addr key_len usage_auth\n"
952" - loads a key data from memory address <key_addr>, <key_len> bytes\n"
953" into TPM using the parent key <parent_handle> with authorization\n"
954" <usage_auth> (20 bytes hex string).\n"
mario.six@gdsys.cc0f4b2ba2017-03-20 10:28:28 +0100955#ifdef CONFIG_TPM_LOAD_KEY_BY_SHA1
956" load_key_by_sha1 parent_hash key_addr key_len usage_auth\n"
957" - loads a key data from memory address <key_addr>, <key_len> bytes\n"
958" into TPM using the parent hash <parent_hash> (20 bytes hex string)\n"
959" with authorization <usage_auth> (20 bytes hex string).\n"
960#endif /* CONFIG_TPM_LOAD_KEY_BY_SHA1 */
Reinhard Pfaube6c1522013-06-26 15:55:13 +0200961" get_pub_key_oiap key_handle usage_auth\n"
962" - get the public key portion of a loaded key <key_handle> using\n"
963" authorization <usage auth> (20 bytes hex string)\n"
964#endif /* CONFIG_TPM_AUTH_SESSIONS */
Che-liang Chiou8732b072013-02-28 09:34:57 +0000965"Endorsement Key Handling Commands:\n"
966" read_pubek addr count\n"
967" - Read <count> bytes of the public endorsement key to memory\n"
968" address <addr>\n"
969"Integrity Collection and Reporting Commands:\n"
970" extend index digest_hex_string\n"
971" - Add a new measurement to a PCR. Update PCR <index> with the 20-bytes\n"
972" <digest_hex_string>\n"
973" pcr_read index addr count\n"
974" - Read <count> bytes from PCR <index> to memory address <addr>.\n"
Reinhard Pfaube6c1522013-06-26 15:55:13 +0200975#ifdef CONFIG_TPM_AUTH_SESSIONS
976"Authorization Sessions\n"
977" oiap\n"
978" - setup an OIAP session\n"
979" end_oiap\n"
980" - terminates an active OIAP session\n"
981#endif /* CONFIG_TPM_AUTH_SESSIONS */
Che-liang Chiou8732b072013-02-28 09:34:57 +0000982"Non-volatile Storage Commands:\n"
983" nv_define_space index permission size\n"
984" - Establish a space at index <index> with <permission> of <size> bytes.\n"
985" nv_read_value index addr count\n"
986" - Read <count> bytes from space <index> to memory address <addr>.\n"
987" nv_write_value index addr count\n"
988" - Write <count> bytes from memory address <addr> to space <index>.\n"
989"Miscellaneous helper functions:\n"
990" raw_transfer byte_string\n"
991" - Send a byte string <byte_string> to TPM and print the response.\n"
992" Non-volatile storage helper functions:\n"
993" These helper functions treat a non-volatile space as a non-padded\n"
994" sequence of integer values. These integer values are defined by a type\n"
995" string, which is a text string of 'bwd' characters: 'b' means a 8-bit\n"
996" value, 'w' 16-bit value, 'd' 32-bit value. All helper functions take\n"
997" a type string as their first argument.\n"
998" nv_define type_string index perm\n"
999" - Define a space <index> with permission <perm>.\n"
1000" nv_read types_string index vars...\n"
1001" - Read from space <index> to environment variables <vars...>.\n"
1002" nv_write types_string index values...\n"
1003" - Write to space <index> from values <values...>.\n"
Luigi Semenzatoeea3f4d2012-12-05 14:46:44 +00001004);