blob: 65e7371e8074f6d3129218469fc6cb666067a2aa [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
Che-liang Chiou8732b072013-02-28 09:34:57 +000061 length = strlen(bytes);
62 count = length / 2;
Luigi Semenzatoeea3f4d2012-12-05 14:46:44 +000063
Che-liang Chiou8732b072013-02-28 09:34:57 +000064 if (!data)
65 data = malloc(count);
66 if (!data)
67 return NULL;
68
69 byte[2] = '\0';
70 for (i = 0; i < length; i += 2) {
71 byte[0] = bytes[i];
72 byte[1] = bytes[i + 1];
73 data[i / 2] = (uint8_t)simple_strtoul(byte, NULL, 16);
Luigi Semenzatoeea3f4d2012-12-05 14:46:44 +000074 }
Che-liang Chiou8732b072013-02-28 09:34:57 +000075
76 if (count_ptr)
77 *count_ptr = count;
78
79 return data;
Luigi Semenzatoeea3f4d2012-12-05 14:46:44 +000080}
81
Che-liang Chiou8732b072013-02-28 09:34:57 +000082/**
Simon Glassf8f1fe12015-08-22 18:31:34 -060083 * report_return_code() - Report any error and return failure or success
Che-liang Chiou8732b072013-02-28 09:34:57 +000084 *
85 * @param return_code TPM command return code
86 * @return value of enum command_ret_t
87 */
Simon Glassf8f1fe12015-08-22 18:31:34 -060088static int report_return_code(int return_code)
Vadim Bendebury576fb1e2011-10-15 15:13:34 +000089{
Simon Glassf8f1fe12015-08-22 18:31:34 -060090 if (return_code) {
91 printf("Error: %d\n", return_code);
Che-liang Chiou8732b072013-02-28 09:34:57 +000092 return CMD_RET_FAILURE;
Simon Glassf8f1fe12015-08-22 18:31:34 -060093 } else {
Che-liang Chiou8732b072013-02-28 09:34:57 +000094 return CMD_RET_SUCCESS;
Simon Glassf8f1fe12015-08-22 18:31:34 -060095 }
Vadim Bendebury576fb1e2011-10-15 15:13:34 +000096}
97
Che-liang Chiou8732b072013-02-28 09:34:57 +000098/**
99 * Return number of values defined by a type string.
100 *
101 * @param type_str type string
102 * @return number of values of type string
103 */
104static int type_string_get_num_values(const char *type_str)
105{
106 return strlen(type_str);
107}
108
109/**
110 * Return total size of values defined by a type string.
111 *
112 * @param type_str type string
113 * @return total size of values of type string, or 0 if type string
114 * contains illegal type character.
115 */
116static size_t type_string_get_space_size(const char *type_str)
117{
118 size_t size;
119
120 for (size = 0; *type_str; type_str++) {
121 switch (*type_str) {
122 case 'b':
123 size += 1;
124 break;
125 case 'w':
126 size += 2;
127 break;
128 case 'd':
129 size += 4;
130 break;
131 default:
132 return 0;
133 }
134 }
135
136 return size;
137}
138
139/**
140 * Allocate a buffer large enough to hold values defined by a type
141 * string. The caller has to free the buffer.
142 *
143 * @param type_str type string
144 * @param count pointer for storing size of buffer
145 * @return pointer to buffer or NULL on error
146 */
147static void *type_string_alloc(const char *type_str, uint32_t *count)
148{
149 void *data;
150 size_t size;
151
152 size = type_string_get_space_size(type_str);
153 if (!size)
154 return NULL;
155 data = malloc(size);
156 if (data)
157 *count = size;
158
159 return data;
160}
161
162/**
163 * Pack values defined by a type string into a buffer. The buffer must have
164 * large enough space.
165 *
166 * @param type_str type string
167 * @param values text strings of values to be packed
168 * @param data output buffer of values
169 * @return 0 on success, non-0 on error
170 */
171static int type_string_pack(const char *type_str, char * const values[],
172 uint8_t *data)
173{
174 size_t offset;
175 uint32_t value;
176
177 for (offset = 0; *type_str; type_str++, values++) {
178 value = simple_strtoul(values[0], NULL, 0);
179 switch (*type_str) {
180 case 'b':
181 data[offset] = value;
182 offset += 1;
183 break;
184 case 'w':
185 put_unaligned_be16(value, data + offset);
186 offset += 2;
187 break;
188 case 'd':
189 put_unaligned_be32(value, data + offset);
190 offset += 4;
191 break;
192 default:
193 return -1;
194 }
195 }
196
197 return 0;
198}
199
200/**
201 * Read values defined by a type string from a buffer, and write these values
202 * to environment variables.
203 *
204 * @param type_str type string
205 * @param data input buffer of values
206 * @param vars names of environment variables
207 * @return 0 on success, non-0 on error
208 */
209static int type_string_write_vars(const char *type_str, uint8_t *data,
210 char * const vars[])
211{
212 size_t offset;
213 uint32_t value;
214
215 for (offset = 0; *type_str; type_str++, vars++) {
216 switch (*type_str) {
217 case 'b':
218 value = data[offset];
219 offset += 1;
220 break;
221 case 'w':
222 value = get_unaligned_be16(data + offset);
223 offset += 2;
224 break;
225 case 'd':
226 value = get_unaligned_be32(data + offset);
227 offset += 4;
228 break;
229 default:
230 return -1;
231 }
232 if (setenv_ulong(*vars, value))
233 return -1;
234 }
235
236 return 0;
237}
238
239static int do_tpm_startup(cmd_tbl_t *cmdtp, int flag,
240 int argc, char * const argv[])
241{
242 enum tpm_startup_type mode;
243
244 if (argc != 2)
245 return CMD_RET_USAGE;
246 if (!strcasecmp("TPM_ST_CLEAR", argv[1])) {
247 mode = TPM_ST_CLEAR;
248 } else if (!strcasecmp("TPM_ST_STATE", argv[1])) {
249 mode = TPM_ST_STATE;
250 } else if (!strcasecmp("TPM_ST_DEACTIVATED", argv[1])) {
251 mode = TPM_ST_DEACTIVATED;
252 } else {
253 printf("Couldn't recognize mode string: %s\n", argv[1]);
254 return CMD_RET_FAILURE;
255 }
256
Simon Glassf8f1fe12015-08-22 18:31:34 -0600257 return report_return_code(tpm_startup(mode));
Che-liang Chiou8732b072013-02-28 09:34:57 +0000258}
259
260static int do_tpm_nv_define_space(cmd_tbl_t *cmdtp, int flag,
261 int argc, char * const argv[])
262{
263 uint32_t index, perm, size;
264
265 if (argc != 4)
266 return CMD_RET_USAGE;
267 index = simple_strtoul(argv[1], NULL, 0);
268 perm = simple_strtoul(argv[2], NULL, 0);
269 size = simple_strtoul(argv[3], NULL, 0);
270
Simon Glassf8f1fe12015-08-22 18:31:34 -0600271 return report_return_code(tpm_nv_define_space(index, perm, size));
Che-liang Chiou8732b072013-02-28 09:34:57 +0000272}
273
274static int do_tpm_nv_read_value(cmd_tbl_t *cmdtp, int flag,
275 int argc, char * const argv[])
276{
277 uint32_t index, count, rc;
278 void *data;
279
280 if (argc != 4)
281 return CMD_RET_USAGE;
282 index = simple_strtoul(argv[1], NULL, 0);
283 data = (void *)simple_strtoul(argv[2], NULL, 0);
284 count = simple_strtoul(argv[3], NULL, 0);
285
286 rc = tpm_nv_read_value(index, data, count);
287 if (!rc) {
288 puts("area content:\n");
289 print_byte_string(data, count);
290 }
291
Simon Glassf8f1fe12015-08-22 18:31:34 -0600292 return report_return_code(rc);
Che-liang Chiou8732b072013-02-28 09:34:57 +0000293}
294
295static int do_tpm_nv_write_value(cmd_tbl_t *cmdtp, int flag,
296 int argc, char * const argv[])
297{
298 uint32_t index, rc;
299 size_t count;
300 void *data;
301
302 if (argc != 3)
303 return CMD_RET_USAGE;
304 index = simple_strtoul(argv[1], NULL, 0);
305 data = parse_byte_string(argv[2], NULL, &count);
306 if (!data) {
307 printf("Couldn't parse byte string %s\n", argv[2]);
308 return CMD_RET_FAILURE;
309 }
310
311 rc = tpm_nv_write_value(index, data, count);
312 free(data);
313
Simon Glassf8f1fe12015-08-22 18:31:34 -0600314 return report_return_code(rc);
Che-liang Chiou8732b072013-02-28 09:34:57 +0000315}
316
317static int do_tpm_extend(cmd_tbl_t *cmdtp, int flag,
318 int argc, char * const argv[])
319{
320 uint32_t index, rc;
321 uint8_t in_digest[20], out_digest[20];
322
323 if (argc != 3)
324 return CMD_RET_USAGE;
325 index = simple_strtoul(argv[1], NULL, 0);
326 if (!parse_byte_string(argv[2], in_digest, NULL)) {
327 printf("Couldn't parse byte string %s\n", argv[2]);
328 return CMD_RET_FAILURE;
329 }
330
331 rc = tpm_extend(index, in_digest, out_digest);
332 if (!rc) {
333 puts("PCR value after execution of the command:\n");
334 print_byte_string(out_digest, sizeof(out_digest));
335 }
336
Simon Glassf8f1fe12015-08-22 18:31:34 -0600337 return report_return_code(rc);
Che-liang Chiou8732b072013-02-28 09:34:57 +0000338}
339
340static int do_tpm_pcr_read(cmd_tbl_t *cmdtp, int flag,
341 int argc, char * const argv[])
342{
343 uint32_t index, count, rc;
344 void *data;
345
346 if (argc != 4)
347 return CMD_RET_USAGE;
348 index = simple_strtoul(argv[1], NULL, 0);
349 data = (void *)simple_strtoul(argv[2], NULL, 0);
350 count = simple_strtoul(argv[3], NULL, 0);
351
352 rc = tpm_pcr_read(index, data, count);
353 if (!rc) {
354 puts("Named PCR content:\n");
355 print_byte_string(data, count);
356 }
357
Simon Glassf8f1fe12015-08-22 18:31:34 -0600358 return report_return_code(rc);
Che-liang Chiou8732b072013-02-28 09:34:57 +0000359}
360
361static int do_tpm_tsc_physical_presence(cmd_tbl_t *cmdtp, int flag,
362 int argc, char * const argv[])
363{
364 uint16_t presence;
365
366 if (argc != 2)
367 return CMD_RET_USAGE;
368 presence = (uint16_t)simple_strtoul(argv[1], NULL, 0);
369
Simon Glassf8f1fe12015-08-22 18:31:34 -0600370 return report_return_code(tpm_tsc_physical_presence(presence));
Che-liang Chiou8732b072013-02-28 09:34:57 +0000371}
372
373static int do_tpm_read_pubek(cmd_tbl_t *cmdtp, int flag,
374 int argc, char * const argv[])
375{
376 uint32_t count, rc;
377 void *data;
378
379 if (argc != 3)
380 return CMD_RET_USAGE;
381 data = (void *)simple_strtoul(argv[1], NULL, 0);
382 count = simple_strtoul(argv[2], NULL, 0);
383
384 rc = tpm_read_pubek(data, count);
385 if (!rc) {
386 puts("pubek value:\n");
387 print_byte_string(data, count);
388 }
389
Simon Glassf8f1fe12015-08-22 18:31:34 -0600390 return report_return_code(rc);
Che-liang Chiou8732b072013-02-28 09:34:57 +0000391}
392
393static int do_tpm_physical_set_deactivated(cmd_tbl_t *cmdtp, int flag,
394 int argc, char * const argv[])
395{
396 uint8_t state;
397
398 if (argc != 2)
399 return CMD_RET_USAGE;
400 state = (uint8_t)simple_strtoul(argv[1], NULL, 0);
401
Simon Glassf8f1fe12015-08-22 18:31:34 -0600402 return report_return_code(tpm_physical_set_deactivated(state));
Che-liang Chiou8732b072013-02-28 09:34:57 +0000403}
404
405static int do_tpm_get_capability(cmd_tbl_t *cmdtp, int flag,
406 int argc, char * const argv[])
407{
408 uint32_t cap_area, sub_cap, rc;
409 void *cap;
410 size_t count;
411
412 if (argc != 5)
413 return CMD_RET_USAGE;
414 cap_area = simple_strtoul(argv[1], NULL, 0);
415 sub_cap = simple_strtoul(argv[2], NULL, 0);
416 cap = (void *)simple_strtoul(argv[3], NULL, 0);
417 count = simple_strtoul(argv[4], NULL, 0);
418
419 rc = tpm_get_capability(cap_area, sub_cap, cap, count);
420 if (!rc) {
421 puts("capability information:\n");
422 print_byte_string(cap, count);
423 }
424
Simon Glassf8f1fe12015-08-22 18:31:34 -0600425 return report_return_code(rc);
Che-liang Chiou8732b072013-02-28 09:34:57 +0000426}
427
428#define TPM_COMMAND_NO_ARG(cmd) \
429static int do_##cmd(cmd_tbl_t *cmdtp, int flag, \
430 int argc, char * const argv[]) \
431{ \
432 if (argc != 1) \
433 return CMD_RET_USAGE; \
Simon Glassf8f1fe12015-08-22 18:31:34 -0600434 return report_return_code(cmd()); \
Che-liang Chiou8732b072013-02-28 09:34:57 +0000435}
436
437TPM_COMMAND_NO_ARG(tpm_init)
438TPM_COMMAND_NO_ARG(tpm_self_test_full)
439TPM_COMMAND_NO_ARG(tpm_continue_self_test)
440TPM_COMMAND_NO_ARG(tpm_force_clear)
441TPM_COMMAND_NO_ARG(tpm_physical_enable)
442TPM_COMMAND_NO_ARG(tpm_physical_disable)
443
Simon Glassc8a8c512015-08-22 18:31:32 -0600444#ifdef CONFIG_DM_TPM
445static int get_tpm(struct udevice **devp)
446{
447 int rc;
448
449 rc = uclass_first_device(UCLASS_TPM, devp);
450 if (rc) {
451 printf("Could not find TPM (ret=%d)\n", rc);
452 return CMD_RET_FAILURE;
453 }
454
455 return 0;
456}
457#endif
458
Che-liang Chiou8732b072013-02-28 09:34:57 +0000459static int do_tpm_raw_transfer(cmd_tbl_t *cmdtp, int flag,
460 int argc, char * const argv[])
461{
462 void *command;
463 uint8_t response[1024];
464 size_t count, response_length = sizeof(response);
465 uint32_t rc;
466
467 command = parse_byte_string(argv[1], NULL, &count);
468 if (!command) {
469 printf("Couldn't parse byte string %s\n", argv[1]);
470 return CMD_RET_FAILURE;
471 }
472
Simon Glassc8a8c512015-08-22 18:31:32 -0600473#ifdef CONFIG_DM_TPM
474 struct udevice *dev;
475
476 rc = get_tpm(&dev);
477 if (rc)
478 return rc;
479
480 rc = tpm_xfer(dev, command, count, response, &response_length);
481#else
Che-liang Chiou8732b072013-02-28 09:34:57 +0000482 rc = tis_sendrecv(command, count, response, &response_length);
Simon Glassc8a8c512015-08-22 18:31:32 -0600483#endif
Che-liang Chiou8732b072013-02-28 09:34:57 +0000484 free(command);
485 if (!rc) {
486 puts("tpm response:\n");
487 print_byte_string(response, response_length);
488 }
489
Simon Glassf8f1fe12015-08-22 18:31:34 -0600490 return report_return_code(rc);
Che-liang Chiou8732b072013-02-28 09:34:57 +0000491}
492
493static int do_tpm_nv_define(cmd_tbl_t *cmdtp, int flag,
494 int argc, char * const argv[])
495{
496 uint32_t index, perm, size;
497
498 if (argc != 4)
499 return CMD_RET_USAGE;
500 size = type_string_get_space_size(argv[1]);
501 if (!size) {
502 printf("Couldn't parse arguments\n");
503 return CMD_RET_USAGE;
504 }
505 index = simple_strtoul(argv[2], NULL, 0);
506 perm = simple_strtoul(argv[3], NULL, 0);
507
Simon Glassf8f1fe12015-08-22 18:31:34 -0600508 return report_return_code(tpm_nv_define_space(index, perm, size));
Che-liang Chiou8732b072013-02-28 09:34:57 +0000509}
510
511static int do_tpm_nv_read(cmd_tbl_t *cmdtp, int flag,
512 int argc, char * const argv[])
513{
514 uint32_t index, count, err;
515 void *data;
516
517 if (argc < 3)
518 return CMD_RET_USAGE;
519 if (argc != 3 + type_string_get_num_values(argv[1]))
520 return CMD_RET_USAGE;
521 index = simple_strtoul(argv[2], NULL, 0);
522 data = type_string_alloc(argv[1], &count);
523 if (!data) {
524 printf("Couldn't parse arguments\n");
525 return CMD_RET_USAGE;
526 }
527
528 err = tpm_nv_read_value(index, data, count);
529 if (!err) {
530 if (type_string_write_vars(argv[1], data, argv + 3)) {
531 printf("Couldn't write to variables\n");
532 err = ~0;
533 }
534 }
535 free(data);
536
Simon Glassf8f1fe12015-08-22 18:31:34 -0600537 return report_return_code(err);
Che-liang Chiou8732b072013-02-28 09:34:57 +0000538}
539
540static int do_tpm_nv_write(cmd_tbl_t *cmdtp, int flag,
541 int argc, char * const argv[])
542{
543 uint32_t index, count, err;
544 void *data;
545
546 if (argc < 3)
547 return CMD_RET_USAGE;
548 if (argc != 3 + type_string_get_num_values(argv[1]))
549 return CMD_RET_USAGE;
550 index = simple_strtoul(argv[2], NULL, 0);
551 data = type_string_alloc(argv[1], &count);
552 if (!data) {
553 printf("Couldn't parse arguments\n");
554 return CMD_RET_USAGE;
555 }
556 if (type_string_pack(argv[1], argv + 3, data)) {
557 printf("Couldn't parse arguments\n");
558 free(data);
559 return CMD_RET_USAGE;
560 }
561
562 err = tpm_nv_write_value(index, data, count);
563 free(data);
564
Simon Glassf8f1fe12015-08-22 18:31:34 -0600565 return report_return_code(err);
Che-liang Chiou8732b072013-02-28 09:34:57 +0000566}
567
Reinhard Pfaube6c1522013-06-26 15:55:13 +0200568#ifdef CONFIG_TPM_AUTH_SESSIONS
569
570static int do_tpm_oiap(cmd_tbl_t *cmdtp, int flag,
571 int argc, char * const argv[])
572{
573 uint32_t auth_handle, err;
574
575 err = tpm_oiap(&auth_handle);
576
Simon Glassf8f1fe12015-08-22 18:31:34 -0600577 return report_return_code(err);
Reinhard Pfaube6c1522013-06-26 15:55:13 +0200578}
579
580static int do_tpm_load_key2_oiap(cmd_tbl_t *cmdtp, int flag,
581 int argc, char * const argv[])
582{
583 uint32_t parent_handle, key_len, key_handle, err;
584 uint8_t usage_auth[DIGEST_LENGTH];
585 void *key;
586
587 if (argc < 5)
588 return CMD_RET_USAGE;
589
590 parent_handle = simple_strtoul(argv[1], NULL, 0);
591 key = (void *)simple_strtoul(argv[2], NULL, 0);
592 key_len = simple_strtoul(argv[3], NULL, 0);
593 if (strlen(argv[4]) != 2 * DIGEST_LENGTH)
594 return CMD_RET_FAILURE;
595 parse_byte_string(argv[4], usage_auth, NULL);
596
597 err = tpm_load_key2_oiap(parent_handle, key, key_len, usage_auth,
598 &key_handle);
599 if (!err)
600 printf("Key handle is 0x%x\n", key_handle);
601
Simon Glassf8f1fe12015-08-22 18:31:34 -0600602 return report_return_code(err);
Reinhard Pfaube6c1522013-06-26 15:55:13 +0200603}
604
605static int do_tpm_get_pub_key_oiap(cmd_tbl_t *cmdtp, int flag,
606 int argc, char * const argv[])
607{
608 uint32_t key_handle, err;
609 uint8_t usage_auth[DIGEST_LENGTH];
610 uint8_t pub_key_buffer[TPM_PUBKEY_MAX_LENGTH];
611 size_t pub_key_len = sizeof(pub_key_buffer);
612
613 if (argc < 3)
614 return CMD_RET_USAGE;
615
616 key_handle = simple_strtoul(argv[1], NULL, 0);
617 if (strlen(argv[2]) != 2 * DIGEST_LENGTH)
618 return CMD_RET_FAILURE;
619 parse_byte_string(argv[2], usage_auth, NULL);
620
621 err = tpm_get_pub_key_oiap(key_handle, usage_auth,
622 pub_key_buffer, &pub_key_len);
623 if (!err) {
624 printf("dump of received pub key structure:\n");
625 print_byte_string(pub_key_buffer, pub_key_len);
626 }
Simon Glassf8f1fe12015-08-22 18:31:34 -0600627 return report_return_code(err);
Reinhard Pfaube6c1522013-06-26 15:55:13 +0200628}
629
630TPM_COMMAND_NO_ARG(tpm_end_oiap)
631
632#endif /* CONFIG_TPM_AUTH_SESSIONS */
633
Che-liang Chiou8732b072013-02-28 09:34:57 +0000634#define MAKE_TPM_CMD_ENTRY(cmd) \
635 U_BOOT_CMD_MKENT(cmd, 0, 1, do_tpm_ ## cmd, "", "")
636
637static cmd_tbl_t tpm_commands[] = {
638 U_BOOT_CMD_MKENT(init, 0, 1,
639 do_tpm_init, "", ""),
640 U_BOOT_CMD_MKENT(startup, 0, 1,
641 do_tpm_startup, "", ""),
642 U_BOOT_CMD_MKENT(self_test_full, 0, 1,
643 do_tpm_self_test_full, "", ""),
644 U_BOOT_CMD_MKENT(continue_self_test, 0, 1,
645 do_tpm_continue_self_test, "", ""),
646 U_BOOT_CMD_MKENT(force_clear, 0, 1,
647 do_tpm_force_clear, "", ""),
648 U_BOOT_CMD_MKENT(physical_enable, 0, 1,
649 do_tpm_physical_enable, "", ""),
650 U_BOOT_CMD_MKENT(physical_disable, 0, 1,
651 do_tpm_physical_disable, "", ""),
652 U_BOOT_CMD_MKENT(nv_define_space, 0, 1,
653 do_tpm_nv_define_space, "", ""),
654 U_BOOT_CMD_MKENT(nv_read_value, 0, 1,
655 do_tpm_nv_read_value, "", ""),
656 U_BOOT_CMD_MKENT(nv_write_value, 0, 1,
657 do_tpm_nv_write_value, "", ""),
658 U_BOOT_CMD_MKENT(extend, 0, 1,
659 do_tpm_extend, "", ""),
660 U_BOOT_CMD_MKENT(pcr_read, 0, 1,
661 do_tpm_pcr_read, "", ""),
662 U_BOOT_CMD_MKENT(tsc_physical_presence, 0, 1,
663 do_tpm_tsc_physical_presence, "", ""),
664 U_BOOT_CMD_MKENT(read_pubek, 0, 1,
665 do_tpm_read_pubek, "", ""),
666 U_BOOT_CMD_MKENT(physical_set_deactivated, 0, 1,
667 do_tpm_physical_set_deactivated, "", ""),
668 U_BOOT_CMD_MKENT(get_capability, 0, 1,
669 do_tpm_get_capability, "", ""),
670 U_BOOT_CMD_MKENT(raw_transfer, 0, 1,
671 do_tpm_raw_transfer, "", ""),
672 U_BOOT_CMD_MKENT(nv_define, 0, 1,
673 do_tpm_nv_define, "", ""),
674 U_BOOT_CMD_MKENT(nv_read, 0, 1,
675 do_tpm_nv_read, "", ""),
676 U_BOOT_CMD_MKENT(nv_write, 0, 1,
677 do_tpm_nv_write, "", ""),
Reinhard Pfaube6c1522013-06-26 15:55:13 +0200678#ifdef CONFIG_TPM_AUTH_SESSIONS
679 U_BOOT_CMD_MKENT(oiap, 0, 1,
680 do_tpm_oiap, "", ""),
681 U_BOOT_CMD_MKENT(end_oiap, 0, 1,
682 do_tpm_end_oiap, "", ""),
683 U_BOOT_CMD_MKENT(load_key2_oiap, 0, 1,
684 do_tpm_load_key2_oiap, "", ""),
685 U_BOOT_CMD_MKENT(get_pub_key_oiap, 0, 1,
686 do_tpm_get_pub_key_oiap, "", ""),
687#endif /* CONFIG_TPM_AUTH_SESSIONS */
Che-liang Chiou8732b072013-02-28 09:34:57 +0000688};
Luigi Semenzatoeea3f4d2012-12-05 14:46:44 +0000689
690static int do_tpm(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
691{
Che-liang Chiou8732b072013-02-28 09:34:57 +0000692 cmd_tbl_t *tpm_cmd;
693
694 if (argc < 2)
695 return CMD_RET_USAGE;
696 tpm_cmd = find_cmd_tbl(argv[1], tpm_commands, ARRAY_SIZE(tpm_commands));
697 if (!tpm_cmd)
698 return CMD_RET_USAGE;
699
700 return tpm_cmd->cmd(cmdtp, flag, argc - 1, argv + 1);
Luigi Semenzatoeea3f4d2012-12-05 14:46:44 +0000701}
702
Che-liang Chiou8732b072013-02-28 09:34:57 +0000703U_BOOT_CMD(tpm, CONFIG_SYS_MAXARGS, 1, do_tpm,
704"Issue a TPM command",
705"cmd args...\n"
706" - Issue TPM command <cmd> with arguments <args...>.\n"
707"Admin Startup and State Commands:\n"
708" init\n"
709" - Put TPM into a state where it waits for 'startup' command.\n"
710" startup mode\n"
711" - Issue TPM_Starup command. <mode> is one of TPM_ST_CLEAR,\n"
712" TPM_ST_STATE, and TPM_ST_DEACTIVATED.\n"
713"Admin Testing Commands:\n"
714" self_test_full\n"
715" - Test all of the TPM capabilities.\n"
716" continue_self_test\n"
717" - Inform TPM that it should complete the self-test.\n"
718"Admin Opt-in Commands:\n"
719" physical_enable\n"
720" - Set the PERMANENT disable flag to FALSE using physical presence as\n"
721" authorization.\n"
722" physical_disable\n"
723" - Set the PERMANENT disable flag to TRUE using physical presence as\n"
724" authorization.\n"
725" physical_set_deactivated 0|1\n"
726" - Set deactivated flag.\n"
727"Admin Ownership Commands:\n"
728" force_clear\n"
729" - Issue TPM_ForceClear command.\n"
730" tsc_physical_presence flags\n"
731" - Set TPM device's Physical Presence flags to <flags>.\n"
732"The Capability Commands:\n"
733" get_capability cap_area sub_cap addr count\n"
734" - Read <count> bytes of TPM capability indexed by <cap_area> and\n"
735" <sub_cap> to memory address <addr>.\n"
Reinhard Pfaube6c1522013-06-26 15:55:13 +0200736#ifdef CONFIG_TPM_AUTH_SESSIONS
737"Storage functions\n"
738" loadkey2_oiap parent_handle key_addr key_len usage_auth\n"
739" - loads a key data from memory address <key_addr>, <key_len> bytes\n"
740" into TPM using the parent key <parent_handle> with authorization\n"
741" <usage_auth> (20 bytes hex string).\n"
742" get_pub_key_oiap key_handle usage_auth\n"
743" - get the public key portion of a loaded key <key_handle> using\n"
744" authorization <usage auth> (20 bytes hex string)\n"
745#endif /* CONFIG_TPM_AUTH_SESSIONS */
Che-liang Chiou8732b072013-02-28 09:34:57 +0000746"Endorsement Key Handling Commands:\n"
747" read_pubek addr count\n"
748" - Read <count> bytes of the public endorsement key to memory\n"
749" address <addr>\n"
750"Integrity Collection and Reporting Commands:\n"
751" extend index digest_hex_string\n"
752" - Add a new measurement to a PCR. Update PCR <index> with the 20-bytes\n"
753" <digest_hex_string>\n"
754" pcr_read index addr count\n"
755" - Read <count> bytes from PCR <index> to memory address <addr>.\n"
Reinhard Pfaube6c1522013-06-26 15:55:13 +0200756#ifdef CONFIG_TPM_AUTH_SESSIONS
757"Authorization Sessions\n"
758" oiap\n"
759" - setup an OIAP session\n"
760" end_oiap\n"
761" - terminates an active OIAP session\n"
762#endif /* CONFIG_TPM_AUTH_SESSIONS */
Che-liang Chiou8732b072013-02-28 09:34:57 +0000763"Non-volatile Storage Commands:\n"
764" nv_define_space index permission size\n"
765" - Establish a space at index <index> with <permission> of <size> bytes.\n"
766" nv_read_value index addr count\n"
767" - Read <count> bytes from space <index> to memory address <addr>.\n"
768" nv_write_value index addr count\n"
769" - Write <count> bytes from memory address <addr> to space <index>.\n"
770"Miscellaneous helper functions:\n"
771" raw_transfer byte_string\n"
772" - Send a byte string <byte_string> to TPM and print the response.\n"
773" Non-volatile storage helper functions:\n"
774" These helper functions treat a non-volatile space as a non-padded\n"
775" sequence of integer values. These integer values are defined by a type\n"
776" string, which is a text string of 'bwd' characters: 'b' means a 8-bit\n"
777" value, 'w' 16-bit value, 'd' 32-bit value. All helper functions take\n"
778" a type string as their first argument.\n"
779" nv_define type_string index perm\n"
780" - Define a space <index> with permission <perm>.\n"
781" nv_read types_string index vars...\n"
782" - Read from space <index> to environment variables <vars...>.\n"
783" nv_write types_string index values...\n"
784" - Write to space <index> from values <values...>.\n"
Luigi Semenzatoeea3f4d2012-12-05 14:46:44 +0000785);