Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Vadim Bendebury | 7acba0b | 2011-10-15 15:13:34 +0000 | [diff] [blame] | 2 | /* |
Che-liang Chiou | c18f901 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 3 | * Copyright (c) 2013 The Chromium OS Authors. |
Vadim Bendebury | 7acba0b | 2011-10-15 15:13:34 +0000 | [diff] [blame] | 4 | */ |
| 5 | |
| 6 | #include <common.h> |
Che-liang Chiou | c18f901 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 7 | #include <malloc.h> |
Che-liang Chiou | c18f901 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 8 | #include <asm/unaligned.h> |
Miquel Raynal | 4c6759e | 2018-05-15 11:57:06 +0200 | [diff] [blame] | 9 | #include <tpm-common.h> |
| 10 | #include <tpm-v1.h> |
| 11 | #include "tpm-user-utils.h" |
Luigi Semenzato | 04d1bd2 | 2012-12-05 14:46:44 +0000 | [diff] [blame] | 12 | |
Miquel Raynal | 886ccc0 | 2018-05-15 11:57:00 +0200 | [diff] [blame] | 13 | static int do_tpm_startup(cmd_tbl_t *cmdtp, int flag, int argc, |
| 14 | char * const argv[]) |
Vadim Bendebury | 7acba0b | 2011-10-15 15:13:34 +0000 | [diff] [blame] | 15 | { |
Che-liang Chiou | c18f901 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 16 | enum tpm_startup_type mode; |
Simon Glass | 8ceca1d | 2018-11-18 14:22:27 -0700 | [diff] [blame] | 17 | struct udevice *dev; |
| 18 | int rc; |
Vadim Bendebury | 7acba0b | 2011-10-15 15:13:34 +0000 | [diff] [blame] | 19 | |
Simon Glass | 8ceca1d | 2018-11-18 14:22:27 -0700 | [diff] [blame] | 20 | rc = get_tpm(&dev); |
| 21 | if (rc) |
| 22 | return rc; |
Che-liang Chiou | c18f901 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 23 | if (argc != 2) |
| 24 | return CMD_RET_USAGE; |
| 25 | if (!strcasecmp("TPM_ST_CLEAR", argv[1])) { |
| 26 | mode = TPM_ST_CLEAR; |
| 27 | } else if (!strcasecmp("TPM_ST_STATE", argv[1])) { |
| 28 | mode = TPM_ST_STATE; |
| 29 | } else if (!strcasecmp("TPM_ST_DEACTIVATED", argv[1])) { |
| 30 | mode = TPM_ST_DEACTIVATED; |
| 31 | } else { |
| 32 | printf("Couldn't recognize mode string: %s\n", argv[1]); |
| 33 | return CMD_RET_FAILURE; |
Vadim Bendebury | 7acba0b | 2011-10-15 15:13:34 +0000 | [diff] [blame] | 34 | } |
| 35 | |
Simon Glass | 8ceca1d | 2018-11-18 14:22:27 -0700 | [diff] [blame] | 36 | return report_return_code(tpm_startup(dev, mode)); |
Che-liang Chiou | c18f901 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 37 | } |
| 38 | |
Miquel Raynal | 886ccc0 | 2018-05-15 11:57:00 +0200 | [diff] [blame] | 39 | static int do_tpm_nv_define_space(cmd_tbl_t *cmdtp, int flag, int argc, |
| 40 | char * const argv[]) |
Che-liang Chiou | c18f901 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 41 | { |
Miquel Raynal | d790f55 | 2018-05-15 11:56:59 +0200 | [diff] [blame] | 42 | u32 index, perm, size; |
Simon Glass | 8ceca1d | 2018-11-18 14:22:27 -0700 | [diff] [blame] | 43 | struct udevice *dev; |
| 44 | int rc; |
| 45 | |
| 46 | rc = get_tpm(&dev); |
| 47 | if (rc) |
| 48 | return rc; |
Che-liang Chiou | c18f901 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 49 | |
| 50 | if (argc != 4) |
| 51 | return CMD_RET_USAGE; |
| 52 | index = simple_strtoul(argv[1], NULL, 0); |
| 53 | perm = simple_strtoul(argv[2], NULL, 0); |
| 54 | size = simple_strtoul(argv[3], NULL, 0); |
| 55 | |
Simon Glass | 8ceca1d | 2018-11-18 14:22:27 -0700 | [diff] [blame] | 56 | return report_return_code(tpm_nv_define_space(dev, index, perm, size)); |
Che-liang Chiou | c18f901 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 57 | } |
| 58 | |
Miquel Raynal | 886ccc0 | 2018-05-15 11:57:00 +0200 | [diff] [blame] | 59 | static int do_tpm_nv_read_value(cmd_tbl_t *cmdtp, int flag, int argc, |
| 60 | char * const argv[]) |
Che-liang Chiou | c18f901 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 61 | { |
Miquel Raynal | d790f55 | 2018-05-15 11:56:59 +0200 | [diff] [blame] | 62 | u32 index, count, rc; |
Simon Glass | 8ceca1d | 2018-11-18 14:22:27 -0700 | [diff] [blame] | 63 | struct udevice *dev; |
Che-liang Chiou | c18f901 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 64 | void *data; |
| 65 | |
Simon Glass | 8ceca1d | 2018-11-18 14:22:27 -0700 | [diff] [blame] | 66 | rc = get_tpm(&dev); |
| 67 | if (rc) |
| 68 | return rc; |
| 69 | |
Che-liang Chiou | c18f901 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 70 | if (argc != 4) |
| 71 | return CMD_RET_USAGE; |
| 72 | index = simple_strtoul(argv[1], NULL, 0); |
| 73 | data = (void *)simple_strtoul(argv[2], NULL, 0); |
| 74 | count = simple_strtoul(argv[3], NULL, 0); |
| 75 | |
Simon Glass | 8ceca1d | 2018-11-18 14:22:27 -0700 | [diff] [blame] | 76 | rc = tpm_nv_read_value(dev, index, data, count); |
Che-liang Chiou | c18f901 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 77 | if (!rc) { |
| 78 | puts("area content:\n"); |
| 79 | print_byte_string(data, count); |
Luigi Semenzato | 04d1bd2 | 2012-12-05 14:46:44 +0000 | [diff] [blame] | 80 | } |
| 81 | |
Simon Glass | 8105c44 | 2015-08-22 18:31:34 -0600 | [diff] [blame] | 82 | return report_return_code(rc); |
Che-liang Chiou | c18f901 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 83 | } |
| 84 | |
Miquel Raynal | 886ccc0 | 2018-05-15 11:57:00 +0200 | [diff] [blame] | 85 | static int do_tpm_nv_write_value(cmd_tbl_t *cmdtp, int flag, int argc, |
| 86 | char * const argv[]) |
Che-liang Chiou | c18f901 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 87 | { |
Simon Glass | 8ceca1d | 2018-11-18 14:22:27 -0700 | [diff] [blame] | 88 | struct udevice *dev; |
Miquel Raynal | d790f55 | 2018-05-15 11:56:59 +0200 | [diff] [blame] | 89 | u32 index, rc; |
Che-liang Chiou | c18f901 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 90 | size_t count; |
| 91 | void *data; |
| 92 | |
Simon Glass | 8ceca1d | 2018-11-18 14:22:27 -0700 | [diff] [blame] | 93 | rc = get_tpm(&dev); |
| 94 | if (rc) |
| 95 | return rc; |
| 96 | |
Che-liang Chiou | c18f901 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 97 | if (argc != 3) |
| 98 | return CMD_RET_USAGE; |
| 99 | index = simple_strtoul(argv[1], NULL, 0); |
| 100 | data = parse_byte_string(argv[2], NULL, &count); |
| 101 | if (!data) { |
| 102 | printf("Couldn't parse byte string %s\n", argv[2]); |
| 103 | return CMD_RET_FAILURE; |
Vadim Bendebury | 7acba0b | 2011-10-15 15:13:34 +0000 | [diff] [blame] | 104 | } |
| 105 | |
Simon Glass | 8ceca1d | 2018-11-18 14:22:27 -0700 | [diff] [blame] | 106 | rc = tpm_nv_write_value(dev, index, data, count); |
Che-liang Chiou | c18f901 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 107 | free(data); |
| 108 | |
Simon Glass | 8105c44 | 2015-08-22 18:31:34 -0600 | [diff] [blame] | 109 | return report_return_code(rc); |
Che-liang Chiou | c18f901 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 110 | } |
| 111 | |
Miquel Raynal | 886ccc0 | 2018-05-15 11:57:00 +0200 | [diff] [blame] | 112 | static int do_tpm_extend(cmd_tbl_t *cmdtp, int flag, int argc, |
| 113 | char * const argv[]) |
Che-liang Chiou | c18f901 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 114 | { |
Miquel Raynal | d790f55 | 2018-05-15 11:56:59 +0200 | [diff] [blame] | 115 | u8 in_digest[20], out_digest[20]; |
Simon Glass | 8ceca1d | 2018-11-18 14:22:27 -0700 | [diff] [blame] | 116 | struct udevice *dev; |
| 117 | u32 index, rc; |
| 118 | |
| 119 | rc = get_tpm(&dev); |
| 120 | if (rc) |
| 121 | return rc; |
Che-liang Chiou | c18f901 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 122 | |
| 123 | if (argc != 3) |
| 124 | return CMD_RET_USAGE; |
| 125 | index = simple_strtoul(argv[1], NULL, 0); |
| 126 | if (!parse_byte_string(argv[2], in_digest, NULL)) { |
| 127 | printf("Couldn't parse byte string %s\n", argv[2]); |
| 128 | return CMD_RET_FAILURE; |
Vadim Bendebury | 7acba0b | 2011-10-15 15:13:34 +0000 | [diff] [blame] | 129 | } |
| 130 | |
Simon Glass | 8ceca1d | 2018-11-18 14:22:27 -0700 | [diff] [blame] | 131 | rc = tpm_extend(dev, index, in_digest, out_digest); |
Che-liang Chiou | c18f901 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 132 | if (!rc) { |
| 133 | puts("PCR value after execution of the command:\n"); |
| 134 | print_byte_string(out_digest, sizeof(out_digest)); |
| 135 | } |
Vadim Bendebury | 7acba0b | 2011-10-15 15:13:34 +0000 | [diff] [blame] | 136 | |
Simon Glass | 8105c44 | 2015-08-22 18:31:34 -0600 | [diff] [blame] | 137 | return report_return_code(rc); |
Che-liang Chiou | c18f901 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 138 | } |
| 139 | |
Miquel Raynal | 886ccc0 | 2018-05-15 11:57:00 +0200 | [diff] [blame] | 140 | static int do_tpm_pcr_read(cmd_tbl_t *cmdtp, int flag, int argc, |
| 141 | char * const argv[]) |
Che-liang Chiou | c18f901 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 142 | { |
Miquel Raynal | d790f55 | 2018-05-15 11:56:59 +0200 | [diff] [blame] | 143 | u32 index, count, rc; |
Simon Glass | 8ceca1d | 2018-11-18 14:22:27 -0700 | [diff] [blame] | 144 | struct udevice *dev; |
Che-liang Chiou | c18f901 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 145 | void *data; |
| 146 | |
Simon Glass | 8ceca1d | 2018-11-18 14:22:27 -0700 | [diff] [blame] | 147 | rc = get_tpm(&dev); |
| 148 | if (rc) |
| 149 | return rc; |
| 150 | |
Che-liang Chiou | c18f901 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 151 | if (argc != 4) |
| 152 | return CMD_RET_USAGE; |
| 153 | index = simple_strtoul(argv[1], NULL, 0); |
| 154 | data = (void *)simple_strtoul(argv[2], NULL, 0); |
| 155 | count = simple_strtoul(argv[3], NULL, 0); |
| 156 | |
Simon Glass | 8ceca1d | 2018-11-18 14:22:27 -0700 | [diff] [blame] | 157 | rc = tpm_pcr_read(dev, index, data, count); |
Che-liang Chiou | c18f901 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 158 | if (!rc) { |
| 159 | puts("Named PCR content:\n"); |
| 160 | print_byte_string(data, count); |
Vadim Bendebury | 7acba0b | 2011-10-15 15:13:34 +0000 | [diff] [blame] | 161 | } |
| 162 | |
Simon Glass | 8105c44 | 2015-08-22 18:31:34 -0600 | [diff] [blame] | 163 | return report_return_code(rc); |
Vadim Bendebury | 7acba0b | 2011-10-15 15:13:34 +0000 | [diff] [blame] | 164 | } |
| 165 | |
Miquel Raynal | 886ccc0 | 2018-05-15 11:57:00 +0200 | [diff] [blame] | 166 | static int do_tpm_tsc_physical_presence(cmd_tbl_t *cmdtp, int flag, int argc, |
| 167 | char * const argv[]) |
Che-liang Chiou | c18f901 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 168 | { |
Simon Glass | 8ceca1d | 2018-11-18 14:22:27 -0700 | [diff] [blame] | 169 | struct udevice *dev; |
Miquel Raynal | d790f55 | 2018-05-15 11:56:59 +0200 | [diff] [blame] | 170 | u16 presence; |
Simon Glass | 8ceca1d | 2018-11-18 14:22:27 -0700 | [diff] [blame] | 171 | int rc; |
| 172 | |
| 173 | rc = get_tpm(&dev); |
| 174 | if (rc) |
| 175 | return rc; |
Luigi Semenzato | 04d1bd2 | 2012-12-05 14:46:44 +0000 | [diff] [blame] | 176 | |
Che-liang Chiou | c18f901 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 177 | if (argc != 2) |
| 178 | return CMD_RET_USAGE; |
Miquel Raynal | d790f55 | 2018-05-15 11:56:59 +0200 | [diff] [blame] | 179 | presence = (u16)simple_strtoul(argv[1], NULL, 0); |
Che-liang Chiou | c18f901 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 180 | |
Simon Glass | 8ceca1d | 2018-11-18 14:22:27 -0700 | [diff] [blame] | 181 | return report_return_code(tpm_tsc_physical_presence(dev, presence)); |
Che-liang Chiou | c18f901 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 182 | } |
| 183 | |
Miquel Raynal | 886ccc0 | 2018-05-15 11:57:00 +0200 | [diff] [blame] | 184 | static int do_tpm_read_pubek(cmd_tbl_t *cmdtp, int flag, int argc, |
| 185 | char * const argv[]) |
Che-liang Chiou | c18f901 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 186 | { |
Simon Glass | 8ceca1d | 2018-11-18 14:22:27 -0700 | [diff] [blame] | 187 | struct udevice *dev; |
Miquel Raynal | d790f55 | 2018-05-15 11:56:59 +0200 | [diff] [blame] | 188 | u32 count, rc; |
Che-liang Chiou | c18f901 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 189 | void *data; |
| 190 | |
Simon Glass | 8ceca1d | 2018-11-18 14:22:27 -0700 | [diff] [blame] | 191 | rc = get_tpm(&dev); |
| 192 | if (rc) |
| 193 | return rc; |
| 194 | |
Che-liang Chiou | c18f901 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 195 | if (argc != 3) |
| 196 | return CMD_RET_USAGE; |
| 197 | data = (void *)simple_strtoul(argv[1], NULL, 0); |
| 198 | count = simple_strtoul(argv[2], NULL, 0); |
| 199 | |
Simon Glass | 8ceca1d | 2018-11-18 14:22:27 -0700 | [diff] [blame] | 200 | rc = tpm_read_pubek(dev, data, count); |
Che-liang Chiou | c18f901 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 201 | if (!rc) { |
| 202 | puts("pubek value:\n"); |
| 203 | print_byte_string(data, count); |
| 204 | } |
| 205 | |
Simon Glass | 8105c44 | 2015-08-22 18:31:34 -0600 | [diff] [blame] | 206 | return report_return_code(rc); |
Che-liang Chiou | c18f901 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 207 | } |
| 208 | |
Miquel Raynal | 886ccc0 | 2018-05-15 11:57:00 +0200 | [diff] [blame] | 209 | static int do_tpm_physical_set_deactivated(cmd_tbl_t *cmdtp, int flag, int argc, |
| 210 | char * const argv[]) |
Che-liang Chiou | c18f901 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 211 | { |
Simon Glass | 8ceca1d | 2018-11-18 14:22:27 -0700 | [diff] [blame] | 212 | struct udevice *dev; |
Miquel Raynal | d790f55 | 2018-05-15 11:56:59 +0200 | [diff] [blame] | 213 | u8 state; |
Simon Glass | 8ceca1d | 2018-11-18 14:22:27 -0700 | [diff] [blame] | 214 | int rc; |
| 215 | |
| 216 | rc = get_tpm(&dev); |
| 217 | if (rc) |
| 218 | return rc; |
Che-liang Chiou | c18f901 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 219 | |
| 220 | if (argc != 2) |
| 221 | return CMD_RET_USAGE; |
Miquel Raynal | d790f55 | 2018-05-15 11:56:59 +0200 | [diff] [blame] | 222 | state = (u8)simple_strtoul(argv[1], NULL, 0); |
Che-liang Chiou | c18f901 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 223 | |
Simon Glass | 8ceca1d | 2018-11-18 14:22:27 -0700 | [diff] [blame] | 224 | return report_return_code(tpm_physical_set_deactivated(dev, state)); |
Che-liang Chiou | c18f901 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 225 | } |
| 226 | |
Miquel Raynal | 886ccc0 | 2018-05-15 11:57:00 +0200 | [diff] [blame] | 227 | static int do_tpm_get_capability(cmd_tbl_t *cmdtp, int flag, int argc, |
| 228 | char * const argv[]) |
Che-liang Chiou | c18f901 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 229 | { |
Miquel Raynal | d790f55 | 2018-05-15 11:56:59 +0200 | [diff] [blame] | 230 | u32 cap_area, sub_cap, rc; |
Che-liang Chiou | c18f901 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 231 | void *cap; |
| 232 | size_t count; |
Simon Glass | 8ceca1d | 2018-11-18 14:22:27 -0700 | [diff] [blame] | 233 | struct udevice *dev; |
| 234 | |
| 235 | rc = get_tpm(&dev); |
| 236 | if (rc) |
| 237 | return rc; |
Che-liang Chiou | c18f901 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 238 | |
| 239 | if (argc != 5) |
| 240 | return CMD_RET_USAGE; |
| 241 | cap_area = simple_strtoul(argv[1], NULL, 0); |
| 242 | sub_cap = simple_strtoul(argv[2], NULL, 0); |
| 243 | cap = (void *)simple_strtoul(argv[3], NULL, 0); |
| 244 | count = simple_strtoul(argv[4], NULL, 0); |
| 245 | |
Simon Glass | 8ceca1d | 2018-11-18 14:22:27 -0700 | [diff] [blame] | 246 | rc = tpm_get_capability(dev, cap_area, sub_cap, cap, count); |
Che-liang Chiou | c18f901 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 247 | if (!rc) { |
| 248 | puts("capability information:\n"); |
| 249 | print_byte_string(cap, count); |
| 250 | } |
| 251 | |
Simon Glass | 8105c44 | 2015-08-22 18:31:34 -0600 | [diff] [blame] | 252 | return report_return_code(rc); |
Che-liang Chiou | c18f901 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 253 | } |
| 254 | |
Miquel Raynal | 886ccc0 | 2018-05-15 11:57:00 +0200 | [diff] [blame] | 255 | static int do_tpm_raw_transfer(cmd_tbl_t *cmdtp, int flag, int argc, |
| 256 | char * const argv[]) |
Luigi Semenzato | 04d1bd2 | 2012-12-05 14:46:44 +0000 | [diff] [blame] | 257 | { |
Christophe Ricard | 52d309b | 2015-10-06 22:54:43 +0200 | [diff] [blame] | 258 | struct udevice *dev; |
Che-liang Chiou | c18f901 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 259 | void *command; |
Miquel Raynal | d790f55 | 2018-05-15 11:56:59 +0200 | [diff] [blame] | 260 | u8 response[1024]; |
Che-liang Chiou | c18f901 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 261 | size_t count, response_length = sizeof(response); |
Miquel Raynal | d790f55 | 2018-05-15 11:56:59 +0200 | [diff] [blame] | 262 | u32 rc; |
Che-liang Chiou | c18f901 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 263 | |
| 264 | command = parse_byte_string(argv[1], NULL, &count); |
| 265 | if (!command) { |
| 266 | printf("Couldn't parse byte string %s\n", argv[1]); |
| 267 | return CMD_RET_FAILURE; |
| 268 | } |
| 269 | |
Simon Glass | 3e4f2fd | 2015-08-22 18:31:32 -0600 | [diff] [blame] | 270 | rc = get_tpm(&dev); |
| 271 | if (rc) |
| 272 | return rc; |
| 273 | |
| 274 | rc = tpm_xfer(dev, command, count, response, &response_length); |
Che-liang Chiou | c18f901 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 275 | free(command); |
| 276 | if (!rc) { |
| 277 | puts("tpm response:\n"); |
| 278 | print_byte_string(response, response_length); |
| 279 | } |
| 280 | |
Simon Glass | 8105c44 | 2015-08-22 18:31:34 -0600 | [diff] [blame] | 281 | return report_return_code(rc); |
Luigi Semenzato | 04d1bd2 | 2012-12-05 14:46:44 +0000 | [diff] [blame] | 282 | } |
| 283 | |
Miquel Raynal | 886ccc0 | 2018-05-15 11:57:00 +0200 | [diff] [blame] | 284 | static int do_tpm_nv_define(cmd_tbl_t *cmdtp, int flag, int argc, |
| 285 | char * const argv[]) |
Che-liang Chiou | c18f901 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 286 | { |
Miquel Raynal | d790f55 | 2018-05-15 11:56:59 +0200 | [diff] [blame] | 287 | u32 index, perm, size; |
Simon Glass | 8ceca1d | 2018-11-18 14:22:27 -0700 | [diff] [blame] | 288 | struct udevice *dev; |
| 289 | int rc; |
| 290 | |
| 291 | rc = get_tpm(&dev); |
| 292 | if (rc) |
| 293 | return rc; |
Che-liang Chiou | c18f901 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 294 | |
| 295 | if (argc != 4) |
| 296 | return CMD_RET_USAGE; |
| 297 | size = type_string_get_space_size(argv[1]); |
| 298 | if (!size) { |
| 299 | printf("Couldn't parse arguments\n"); |
| 300 | return CMD_RET_USAGE; |
| 301 | } |
| 302 | index = simple_strtoul(argv[2], NULL, 0); |
| 303 | perm = simple_strtoul(argv[3], NULL, 0); |
Luigi Semenzato | 04d1bd2 | 2012-12-05 14:46:44 +0000 | [diff] [blame] | 304 | |
Simon Glass | 8ceca1d | 2018-11-18 14:22:27 -0700 | [diff] [blame] | 305 | return report_return_code(tpm_nv_define_space(dev, index, perm, size)); |
Che-liang Chiou | c18f901 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 306 | } |
Luigi Semenzato | 04d1bd2 | 2012-12-05 14:46:44 +0000 | [diff] [blame] | 307 | |
Miquel Raynal | 886ccc0 | 2018-05-15 11:57:00 +0200 | [diff] [blame] | 308 | static int do_tpm_nv_read(cmd_tbl_t *cmdtp, int flag, int argc, |
| 309 | char * const argv[]) |
Luigi Semenzato | 04d1bd2 | 2012-12-05 14:46:44 +0000 | [diff] [blame] | 310 | { |
Miquel Raynal | d790f55 | 2018-05-15 11:56:59 +0200 | [diff] [blame] | 311 | u32 index, count, err; |
Simon Glass | 8ceca1d | 2018-11-18 14:22:27 -0700 | [diff] [blame] | 312 | struct udevice *dev; |
Che-liang Chiou | c18f901 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 313 | void *data; |
Simon Glass | 8ceca1d | 2018-11-18 14:22:27 -0700 | [diff] [blame] | 314 | int rc; |
| 315 | |
| 316 | rc = get_tpm(&dev); |
| 317 | if (rc) |
| 318 | return rc; |
Luigi Semenzato | 04d1bd2 | 2012-12-05 14:46:44 +0000 | [diff] [blame] | 319 | |
Che-liang Chiou | c18f901 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 320 | if (argc < 3) |
| 321 | return CMD_RET_USAGE; |
| 322 | if (argc != 3 + type_string_get_num_values(argv[1])) |
| 323 | return CMD_RET_USAGE; |
| 324 | index = simple_strtoul(argv[2], NULL, 0); |
| 325 | data = type_string_alloc(argv[1], &count); |
| 326 | if (!data) { |
| 327 | printf("Couldn't parse arguments\n"); |
| 328 | return CMD_RET_USAGE; |
Luigi Semenzato | 04d1bd2 | 2012-12-05 14:46:44 +0000 | [diff] [blame] | 329 | } |
| 330 | |
Simon Glass | 8ceca1d | 2018-11-18 14:22:27 -0700 | [diff] [blame] | 331 | err = tpm_nv_read_value(dev, index, data, count); |
Che-liang Chiou | c18f901 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 332 | if (!err) { |
| 333 | if (type_string_write_vars(argv[1], data, argv + 3)) { |
| 334 | printf("Couldn't write to variables\n"); |
| 335 | err = ~0; |
| 336 | } |
Luigi Semenzato | 04d1bd2 | 2012-12-05 14:46:44 +0000 | [diff] [blame] | 337 | } |
Che-liang Chiou | c18f901 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 338 | free(data); |
Luigi Semenzato | 04d1bd2 | 2012-12-05 14:46:44 +0000 | [diff] [blame] | 339 | |
Simon Glass | 8105c44 | 2015-08-22 18:31:34 -0600 | [diff] [blame] | 340 | return report_return_code(err); |
Luigi Semenzato | 04d1bd2 | 2012-12-05 14:46:44 +0000 | [diff] [blame] | 341 | } |
| 342 | |
Miquel Raynal | 886ccc0 | 2018-05-15 11:57:00 +0200 | [diff] [blame] | 343 | static int do_tpm_nv_write(cmd_tbl_t *cmdtp, int flag, int argc, |
| 344 | char * const argv[]) |
Che-liang Chiou | c18f901 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 345 | { |
Miquel Raynal | d790f55 | 2018-05-15 11:56:59 +0200 | [diff] [blame] | 346 | u32 index, count, err; |
Simon Glass | 8ceca1d | 2018-11-18 14:22:27 -0700 | [diff] [blame] | 347 | struct udevice *dev; |
Che-liang Chiou | c18f901 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 348 | void *data; |
Simon Glass | 8ceca1d | 2018-11-18 14:22:27 -0700 | [diff] [blame] | 349 | int rc; |
| 350 | |
| 351 | rc = get_tpm(&dev); |
| 352 | if (rc) |
| 353 | return rc; |
Che-liang Chiou | c18f901 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 354 | |
| 355 | if (argc < 3) |
| 356 | return CMD_RET_USAGE; |
| 357 | if (argc != 3 + type_string_get_num_values(argv[1])) |
| 358 | return CMD_RET_USAGE; |
| 359 | index = simple_strtoul(argv[2], NULL, 0); |
| 360 | data = type_string_alloc(argv[1], &count); |
| 361 | if (!data) { |
| 362 | printf("Couldn't parse arguments\n"); |
| 363 | return CMD_RET_USAGE; |
| 364 | } |
| 365 | if (type_string_pack(argv[1], argv + 3, data)) { |
| 366 | printf("Couldn't parse arguments\n"); |
| 367 | free(data); |
| 368 | return CMD_RET_USAGE; |
| 369 | } |
| 370 | |
Simon Glass | 8ceca1d | 2018-11-18 14:22:27 -0700 | [diff] [blame] | 371 | err = tpm_nv_write_value(dev, index, data, count); |
Che-liang Chiou | c18f901 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 372 | free(data); |
| 373 | |
Simon Glass | 8105c44 | 2015-08-22 18:31:34 -0600 | [diff] [blame] | 374 | return report_return_code(err); |
Reinhard Pfau | 4fece43 | 2013-06-26 15:55:13 +0200 | [diff] [blame] | 375 | } |
| 376 | |
| 377 | #ifdef CONFIG_TPM_AUTH_SESSIONS |
| 378 | |
Miquel Raynal | 886ccc0 | 2018-05-15 11:57:00 +0200 | [diff] [blame] | 379 | static int do_tpm_oiap(cmd_tbl_t *cmdtp, int flag, int argc, |
| 380 | char * const argv[]) |
Reinhard Pfau | 4fece43 | 2013-06-26 15:55:13 +0200 | [diff] [blame] | 381 | { |
Miquel Raynal | d790f55 | 2018-05-15 11:56:59 +0200 | [diff] [blame] | 382 | u32 auth_handle, err; |
Simon Glass | 8ceca1d | 2018-11-18 14:22:27 -0700 | [diff] [blame] | 383 | struct udevice *dev; |
| 384 | int rc; |
| 385 | |
| 386 | rc = get_tpm(&dev); |
| 387 | if (rc) |
| 388 | return rc; |
Reinhard Pfau | 4fece43 | 2013-06-26 15:55:13 +0200 | [diff] [blame] | 389 | |
Simon Glass | 8ceca1d | 2018-11-18 14:22:27 -0700 | [diff] [blame] | 390 | err = tpm_oiap(dev, &auth_handle); |
Reinhard Pfau | 4fece43 | 2013-06-26 15:55:13 +0200 | [diff] [blame] | 391 | |
Simon Glass | 8105c44 | 2015-08-22 18:31:34 -0600 | [diff] [blame] | 392 | return report_return_code(err); |
Reinhard Pfau | 4fece43 | 2013-06-26 15:55:13 +0200 | [diff] [blame] | 393 | } |
| 394 | |
mario.six@gdsys.cc | a5a7ea2 | 2017-03-20 10:28:28 +0100 | [diff] [blame] | 395 | #ifdef CONFIG_TPM_LOAD_KEY_BY_SHA1 |
| 396 | static int do_tpm_load_key_by_sha1(cmd_tbl_t *cmdtp, int flag, int argc, char * |
| 397 | const argv[]) |
| 398 | { |
Miquel Raynal | d790f55 | 2018-05-15 11:56:59 +0200 | [diff] [blame] | 399 | u32 parent_handle = 0; |
| 400 | u32 key_len, key_handle, err; |
| 401 | u8 usage_auth[DIGEST_LENGTH]; |
| 402 | u8 parent_hash[DIGEST_LENGTH]; |
mario.six@gdsys.cc | a5a7ea2 | 2017-03-20 10:28:28 +0100 | [diff] [blame] | 403 | void *key; |
Simon Glass | 8ceca1d | 2018-11-18 14:22:27 -0700 | [diff] [blame] | 404 | struct udevice *dev; |
| 405 | |
| 406 | rc = get_tpm(&dev); |
| 407 | if (rc) |
| 408 | return rc; |
mario.six@gdsys.cc | a5a7ea2 | 2017-03-20 10:28:28 +0100 | [diff] [blame] | 409 | |
| 410 | if (argc < 5) |
| 411 | return CMD_RET_USAGE; |
| 412 | |
| 413 | parse_byte_string(argv[1], parent_hash, NULL); |
| 414 | key = (void *)simple_strtoul(argv[2], NULL, 0); |
| 415 | key_len = simple_strtoul(argv[3], NULL, 0); |
| 416 | if (strlen(argv[4]) != 2 * DIGEST_LENGTH) |
| 417 | return CMD_RET_FAILURE; |
| 418 | parse_byte_string(argv[4], usage_auth, NULL); |
| 419 | |
| 420 | err = tpm_find_key_sha1(usage_auth, parent_hash, &parent_handle); |
| 421 | if (err) { |
| 422 | printf("Could not find matching parent key (err = %d)\n", err); |
| 423 | return CMD_RET_FAILURE; |
| 424 | } |
| 425 | |
| 426 | printf("Found parent key %08x\n", parent_handle); |
| 427 | |
| 428 | err = tpm_load_key2_oiap(parent_handle, key, key_len, usage_auth, |
| 429 | &key_handle); |
| 430 | if (!err) { |
| 431 | printf("Key handle is 0x%x\n", key_handle); |
Simon Glass | 4d949a2 | 2017-08-03 12:22:10 -0600 | [diff] [blame] | 432 | env_set_hex("key_handle", key_handle); |
mario.six@gdsys.cc | a5a7ea2 | 2017-03-20 10:28:28 +0100 | [diff] [blame] | 433 | } |
| 434 | |
| 435 | return report_return_code(err); |
| 436 | } |
| 437 | #endif /* CONFIG_TPM_LOAD_KEY_BY_SHA1 */ |
| 438 | |
Miquel Raynal | 886ccc0 | 2018-05-15 11:57:00 +0200 | [diff] [blame] | 439 | static int do_tpm_load_key2_oiap(cmd_tbl_t *cmdtp, int flag, int argc, |
| 440 | char * const argv[]) |
Reinhard Pfau | 4fece43 | 2013-06-26 15:55:13 +0200 | [diff] [blame] | 441 | { |
Miquel Raynal | d790f55 | 2018-05-15 11:56:59 +0200 | [diff] [blame] | 442 | u32 parent_handle, key_len, key_handle, err; |
| 443 | u8 usage_auth[DIGEST_LENGTH]; |
Reinhard Pfau | 4fece43 | 2013-06-26 15:55:13 +0200 | [diff] [blame] | 444 | void *key; |
Simon Glass | 8ceca1d | 2018-11-18 14:22:27 -0700 | [diff] [blame] | 445 | struct udevice *dev; |
| 446 | int rc; |
| 447 | |
| 448 | rc = get_tpm(&dev); |
| 449 | if (rc) |
| 450 | return rc; |
Reinhard Pfau | 4fece43 | 2013-06-26 15:55:13 +0200 | [diff] [blame] | 451 | |
| 452 | if (argc < 5) |
| 453 | return CMD_RET_USAGE; |
| 454 | |
| 455 | parent_handle = simple_strtoul(argv[1], NULL, 0); |
| 456 | key = (void *)simple_strtoul(argv[2], NULL, 0); |
| 457 | key_len = simple_strtoul(argv[3], NULL, 0); |
| 458 | if (strlen(argv[4]) != 2 * DIGEST_LENGTH) |
| 459 | return CMD_RET_FAILURE; |
| 460 | parse_byte_string(argv[4], usage_auth, NULL); |
| 461 | |
Simon Glass | 8ceca1d | 2018-11-18 14:22:27 -0700 | [diff] [blame] | 462 | err = tpm_load_key2_oiap(dev, parent_handle, key, key_len, usage_auth, |
Miquel Raynal | 886ccc0 | 2018-05-15 11:57:00 +0200 | [diff] [blame] | 463 | &key_handle); |
Reinhard Pfau | 4fece43 | 2013-06-26 15:55:13 +0200 | [diff] [blame] | 464 | if (!err) |
| 465 | printf("Key handle is 0x%x\n", key_handle); |
| 466 | |
Simon Glass | 8105c44 | 2015-08-22 18:31:34 -0600 | [diff] [blame] | 467 | return report_return_code(err); |
Reinhard Pfau | 4fece43 | 2013-06-26 15:55:13 +0200 | [diff] [blame] | 468 | } |
| 469 | |
Miquel Raynal | 886ccc0 | 2018-05-15 11:57:00 +0200 | [diff] [blame] | 470 | static int do_tpm_get_pub_key_oiap(cmd_tbl_t *cmdtp, int flag, int argc, |
| 471 | char * const argv[]) |
Reinhard Pfau | 4fece43 | 2013-06-26 15:55:13 +0200 | [diff] [blame] | 472 | { |
Miquel Raynal | d790f55 | 2018-05-15 11:56:59 +0200 | [diff] [blame] | 473 | u32 key_handle, err; |
| 474 | u8 usage_auth[DIGEST_LENGTH]; |
| 475 | u8 pub_key_buffer[TPM_PUBKEY_MAX_LENGTH]; |
Reinhard Pfau | 4fece43 | 2013-06-26 15:55:13 +0200 | [diff] [blame] | 476 | size_t pub_key_len = sizeof(pub_key_buffer); |
Simon Glass | 8ceca1d | 2018-11-18 14:22:27 -0700 | [diff] [blame] | 477 | struct udevice *dev; |
| 478 | int rc; |
| 479 | |
| 480 | rc = get_tpm(&dev); |
| 481 | if (rc) |
| 482 | return rc; |
Reinhard Pfau | 4fece43 | 2013-06-26 15:55:13 +0200 | [diff] [blame] | 483 | |
| 484 | if (argc < 3) |
| 485 | return CMD_RET_USAGE; |
| 486 | |
| 487 | key_handle = simple_strtoul(argv[1], NULL, 0); |
| 488 | if (strlen(argv[2]) != 2 * DIGEST_LENGTH) |
| 489 | return CMD_RET_FAILURE; |
| 490 | parse_byte_string(argv[2], usage_auth, NULL); |
| 491 | |
Simon Glass | 8ceca1d | 2018-11-18 14:22:27 -0700 | [diff] [blame] | 492 | err = tpm_get_pub_key_oiap(dev, key_handle, usage_auth, pub_key_buffer, |
Miquel Raynal | 886ccc0 | 2018-05-15 11:57:00 +0200 | [diff] [blame] | 493 | &pub_key_len); |
Reinhard Pfau | 4fece43 | 2013-06-26 15:55:13 +0200 | [diff] [blame] | 494 | if (!err) { |
| 495 | printf("dump of received pub key structure:\n"); |
| 496 | print_byte_string(pub_key_buffer, pub_key_len); |
| 497 | } |
Simon Glass | 8105c44 | 2015-08-22 18:31:34 -0600 | [diff] [blame] | 498 | return report_return_code(err); |
Che-liang Chiou | c18f901 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 499 | } |
| 500 | |
Reinhard Pfau | 4fece43 | 2013-06-26 15:55:13 +0200 | [diff] [blame] | 501 | TPM_COMMAND_NO_ARG(tpm_end_oiap) |
| 502 | |
| 503 | #endif /* CONFIG_TPM_AUTH_SESSIONS */ |
| 504 | |
Mario Six | 4eceb6c | 2017-01-11 16:00:50 +0100 | [diff] [blame] | 505 | #ifdef CONFIG_TPM_FLUSH_RESOURCES |
| 506 | static int do_tpm_flush(cmd_tbl_t *cmdtp, int flag, int argc, |
| 507 | char * const argv[]) |
| 508 | { |
Simon Glass | 8ceca1d | 2018-11-18 14:22:27 -0700 | [diff] [blame] | 509 | struct udevice *dev; |
Mario Six | 4eceb6c | 2017-01-11 16:00:50 +0100 | [diff] [blame] | 510 | int type = 0; |
Simon Glass | 8ceca1d | 2018-11-18 14:22:27 -0700 | [diff] [blame] | 511 | int rc; |
| 512 | |
| 513 | rc = get_tpm(&dev); |
| 514 | if (rc) |
| 515 | return rc; |
Mario Six | 4eceb6c | 2017-01-11 16:00:50 +0100 | [diff] [blame] | 516 | |
mario.six@gdsys.cc | 9ae8814 | 2017-03-20 10:28:29 +0100 | [diff] [blame] | 517 | if (argc != 3) |
Mario Six | 4eceb6c | 2017-01-11 16:00:50 +0100 | [diff] [blame] | 518 | return CMD_RET_USAGE; |
| 519 | |
mario.six@gdsys.cc | 9ae8814 | 2017-03-20 10:28:29 +0100 | [diff] [blame] | 520 | if (!strcasecmp(argv[1], "key")) |
Mario Six | 4eceb6c | 2017-01-11 16:00:50 +0100 | [diff] [blame] | 521 | type = TPM_RT_KEY; |
mario.six@gdsys.cc | 9ae8814 | 2017-03-20 10:28:29 +0100 | [diff] [blame] | 522 | else if (!strcasecmp(argv[1], "auth")) |
Mario Six | 4eceb6c | 2017-01-11 16:00:50 +0100 | [diff] [blame] | 523 | type = TPM_RT_AUTH; |
mario.six@gdsys.cc | 9ae8814 | 2017-03-20 10:28:29 +0100 | [diff] [blame] | 524 | else if (!strcasecmp(argv[1], "hash")) |
Mario Six | 4eceb6c | 2017-01-11 16:00:50 +0100 | [diff] [blame] | 525 | type = TPM_RT_HASH; |
mario.six@gdsys.cc | 9ae8814 | 2017-03-20 10:28:29 +0100 | [diff] [blame] | 526 | else if (!strcasecmp(argv[1], "trans")) |
Mario Six | 4eceb6c | 2017-01-11 16:00:50 +0100 | [diff] [blame] | 527 | type = TPM_RT_TRANS; |
mario.six@gdsys.cc | 9ae8814 | 2017-03-20 10:28:29 +0100 | [diff] [blame] | 528 | else if (!strcasecmp(argv[1], "context")) |
Mario Six | 4eceb6c | 2017-01-11 16:00:50 +0100 | [diff] [blame] | 529 | type = TPM_RT_CONTEXT; |
mario.six@gdsys.cc | 9ae8814 | 2017-03-20 10:28:29 +0100 | [diff] [blame] | 530 | else if (!strcasecmp(argv[1], "counter")) |
Mario Six | 4eceb6c | 2017-01-11 16:00:50 +0100 | [diff] [blame] | 531 | type = TPM_RT_COUNTER; |
mario.six@gdsys.cc | 9ae8814 | 2017-03-20 10:28:29 +0100 | [diff] [blame] | 532 | else if (!strcasecmp(argv[1], "delegate")) |
Mario Six | 4eceb6c | 2017-01-11 16:00:50 +0100 | [diff] [blame] | 533 | type = TPM_RT_DELEGATE; |
mario.six@gdsys.cc | 9ae8814 | 2017-03-20 10:28:29 +0100 | [diff] [blame] | 534 | else if (!strcasecmp(argv[1], "daa_tpm")) |
Mario Six | 4eceb6c | 2017-01-11 16:00:50 +0100 | [diff] [blame] | 535 | type = TPM_RT_DAA_TPM; |
mario.six@gdsys.cc | 9ae8814 | 2017-03-20 10:28:29 +0100 | [diff] [blame] | 536 | else if (!strcasecmp(argv[1], "daa_v0")) |
Mario Six | 4eceb6c | 2017-01-11 16:00:50 +0100 | [diff] [blame] | 537 | type = TPM_RT_DAA_V0; |
mario.six@gdsys.cc | 9ae8814 | 2017-03-20 10:28:29 +0100 | [diff] [blame] | 538 | else if (!strcasecmp(argv[1], "daa_v1")) |
Mario Six | 4eceb6c | 2017-01-11 16:00:50 +0100 | [diff] [blame] | 539 | type = TPM_RT_DAA_V1; |
| 540 | |
mario.six@gdsys.cc | 9ae8814 | 2017-03-20 10:28:29 +0100 | [diff] [blame] | 541 | if (!type) { |
| 542 | printf("Resource type %s unknown.\n", argv[1]); |
| 543 | return -1; |
| 544 | } |
| 545 | |
| 546 | if (!strcasecmp(argv[2], "all")) { |
Miquel Raynal | d790f55 | 2018-05-15 11:56:59 +0200 | [diff] [blame] | 547 | u16 res_count; |
| 548 | u8 buf[288]; |
| 549 | u8 *ptr; |
Mario Six | 4eceb6c | 2017-01-11 16:00:50 +0100 | [diff] [blame] | 550 | int err; |
| 551 | uint i; |
| 552 | |
| 553 | /* fetch list of already loaded resources in the TPM */ |
Simon Glass | 8ceca1d | 2018-11-18 14:22:27 -0700 | [diff] [blame] | 554 | err = tpm_get_capability(dev, TPM_CAP_HANDLE, type, buf, |
Mario Six | 4eceb6c | 2017-01-11 16:00:50 +0100 | [diff] [blame] | 555 | sizeof(buf)); |
mario.six@gdsys.cc | 9ae8814 | 2017-03-20 10:28:29 +0100 | [diff] [blame] | 556 | if (err) { |
| 557 | printf("tpm_get_capability returned error %d.\n", err); |
Mario Six | 4eceb6c | 2017-01-11 16:00:50 +0100 | [diff] [blame] | 558 | return -1; |
mario.six@gdsys.cc | 9ae8814 | 2017-03-20 10:28:29 +0100 | [diff] [blame] | 559 | } |
Mario Six | 4eceb6c | 2017-01-11 16:00:50 +0100 | [diff] [blame] | 560 | res_count = get_unaligned_be16(buf); |
| 561 | ptr = buf + 2; |
| 562 | for (i = 0; i < res_count; ++i, ptr += 4) |
Simon Glass | 8ceca1d | 2018-11-18 14:22:27 -0700 | [diff] [blame] | 563 | tpm_flush_specific(dev, get_unaligned_be32(ptr), type); |
Mario Six | 4eceb6c | 2017-01-11 16:00:50 +0100 | [diff] [blame] | 564 | } else { |
Miquel Raynal | d790f55 | 2018-05-15 11:56:59 +0200 | [diff] [blame] | 565 | u32 handle = simple_strtoul(argv[2], NULL, 0); |
Mario Six | 4eceb6c | 2017-01-11 16:00:50 +0100 | [diff] [blame] | 566 | |
mario.six@gdsys.cc | 9ae8814 | 2017-03-20 10:28:29 +0100 | [diff] [blame] | 567 | if (!handle) { |
| 568 | printf("Illegal resource handle %s\n", argv[2]); |
Mario Six | 4eceb6c | 2017-01-11 16:00:50 +0100 | [diff] [blame] | 569 | return -1; |
mario.six@gdsys.cc | 9ae8814 | 2017-03-20 10:28:29 +0100 | [diff] [blame] | 570 | } |
Simon Glass | 8ceca1d | 2018-11-18 14:22:27 -0700 | [diff] [blame] | 571 | tpm_flush_specific(dev, cpu_to_be32(handle), type); |
Mario Six | 4eceb6c | 2017-01-11 16:00:50 +0100 | [diff] [blame] | 572 | } |
| 573 | |
| 574 | return 0; |
| 575 | } |
| 576 | #endif /* CONFIG_TPM_FLUSH_RESOURCES */ |
| 577 | |
mario.six@gdsys.cc | a5bfcc5 | 2017-03-20 10:28:30 +0100 | [diff] [blame] | 578 | #ifdef CONFIG_TPM_LIST_RESOURCES |
| 579 | static int do_tpm_list(cmd_tbl_t *cmdtp, int flag, int argc, |
| 580 | char * const argv[]) |
| 581 | { |
| 582 | int type = 0; |
Miquel Raynal | d790f55 | 2018-05-15 11:56:59 +0200 | [diff] [blame] | 583 | u16 res_count; |
| 584 | u8 buf[288]; |
| 585 | u8 *ptr; |
mario.six@gdsys.cc | a5bfcc5 | 2017-03-20 10:28:30 +0100 | [diff] [blame] | 586 | int err; |
| 587 | uint i; |
| 588 | |
| 589 | if (argc != 2) |
| 590 | return CMD_RET_USAGE; |
| 591 | |
| 592 | if (!strcasecmp(argv[1], "key")) |
| 593 | type = TPM_RT_KEY; |
| 594 | else if (!strcasecmp(argv[1], "auth")) |
| 595 | type = TPM_RT_AUTH; |
| 596 | else if (!strcasecmp(argv[1], "hash")) |
| 597 | type = TPM_RT_HASH; |
| 598 | else if (!strcasecmp(argv[1], "trans")) |
| 599 | type = TPM_RT_TRANS; |
| 600 | else if (!strcasecmp(argv[1], "context")) |
| 601 | type = TPM_RT_CONTEXT; |
| 602 | else if (!strcasecmp(argv[1], "counter")) |
| 603 | type = TPM_RT_COUNTER; |
| 604 | else if (!strcasecmp(argv[1], "delegate")) |
| 605 | type = TPM_RT_DELEGATE; |
| 606 | else if (!strcasecmp(argv[1], "daa_tpm")) |
| 607 | type = TPM_RT_DAA_TPM; |
| 608 | else if (!strcasecmp(argv[1], "daa_v0")) |
| 609 | type = TPM_RT_DAA_V0; |
| 610 | else if (!strcasecmp(argv[1], "daa_v1")) |
| 611 | type = TPM_RT_DAA_V1; |
| 612 | |
| 613 | if (!type) { |
| 614 | printf("Resource type %s unknown.\n", argv[1]); |
| 615 | return -1; |
| 616 | } |
| 617 | |
| 618 | /* fetch list of already loaded resources in the TPM */ |
| 619 | err = tpm_get_capability(TPM_CAP_HANDLE, type, buf, |
| 620 | sizeof(buf)); |
| 621 | if (err) { |
| 622 | printf("tpm_get_capability returned error %d.\n", err); |
| 623 | return -1; |
| 624 | } |
| 625 | res_count = get_unaligned_be16(buf); |
| 626 | ptr = buf + 2; |
| 627 | |
| 628 | printf("Resources of type %s (%02x):\n", argv[1], type); |
| 629 | if (!res_count) { |
| 630 | puts("None\n"); |
| 631 | } else { |
| 632 | for (i = 0; i < res_count; ++i, ptr += 4) |
| 633 | printf("Index %d: %08x\n", i, get_unaligned_be32(ptr)); |
| 634 | } |
| 635 | |
| 636 | return 0; |
| 637 | } |
| 638 | #endif /* CONFIG_TPM_LIST_RESOURCES */ |
| 639 | |
Miquel Raynal | 4c6759e | 2018-05-15 11:57:06 +0200 | [diff] [blame] | 640 | TPM_COMMAND_NO_ARG(tpm_self_test_full) |
| 641 | TPM_COMMAND_NO_ARG(tpm_continue_self_test) |
| 642 | TPM_COMMAND_NO_ARG(tpm_force_clear) |
| 643 | TPM_COMMAND_NO_ARG(tpm_physical_enable) |
| 644 | TPM_COMMAND_NO_ARG(tpm_physical_disable) |
Che-liang Chiou | c18f901 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 645 | |
Miquel Raynal | 4c6759e | 2018-05-15 11:57:06 +0200 | [diff] [blame] | 646 | static cmd_tbl_t tpm1_commands[] = { |
Simon Glass | aa6ac63 | 2015-08-22 18:31:40 -0600 | [diff] [blame] | 647 | U_BOOT_CMD_MKENT(info, 0, 1, do_tpm_info, "", ""), |
Miquel Raynal | 886ccc0 | 2018-05-15 11:57:00 +0200 | [diff] [blame] | 648 | U_BOOT_CMD_MKENT(init, 0, 1, do_tpm_init, "", ""), |
Che-liang Chiou | c18f901 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 649 | U_BOOT_CMD_MKENT(startup, 0, 1, |
Miquel Raynal | 886ccc0 | 2018-05-15 11:57:00 +0200 | [diff] [blame] | 650 | do_tpm_startup, "", ""), |
Che-liang Chiou | c18f901 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 651 | U_BOOT_CMD_MKENT(self_test_full, 0, 1, |
Miquel Raynal | 886ccc0 | 2018-05-15 11:57:00 +0200 | [diff] [blame] | 652 | do_tpm_self_test_full, "", ""), |
Che-liang Chiou | c18f901 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 653 | U_BOOT_CMD_MKENT(continue_self_test, 0, 1, |
Miquel Raynal | 886ccc0 | 2018-05-15 11:57:00 +0200 | [diff] [blame] | 654 | do_tpm_continue_self_test, "", ""), |
Che-liang Chiou | c18f901 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 655 | U_BOOT_CMD_MKENT(force_clear, 0, 1, |
Miquel Raynal | 886ccc0 | 2018-05-15 11:57:00 +0200 | [diff] [blame] | 656 | do_tpm_force_clear, "", ""), |
Che-liang Chiou | c18f901 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 657 | U_BOOT_CMD_MKENT(physical_enable, 0, 1, |
Miquel Raynal | 886ccc0 | 2018-05-15 11:57:00 +0200 | [diff] [blame] | 658 | do_tpm_physical_enable, "", ""), |
Che-liang Chiou | c18f901 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 659 | U_BOOT_CMD_MKENT(physical_disable, 0, 1, |
Miquel Raynal | 886ccc0 | 2018-05-15 11:57:00 +0200 | [diff] [blame] | 660 | do_tpm_physical_disable, "", ""), |
Che-liang Chiou | c18f901 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 661 | U_BOOT_CMD_MKENT(nv_define_space, 0, 1, |
Miquel Raynal | 886ccc0 | 2018-05-15 11:57:00 +0200 | [diff] [blame] | 662 | do_tpm_nv_define_space, "", ""), |
Che-liang Chiou | c18f901 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 663 | U_BOOT_CMD_MKENT(nv_read_value, 0, 1, |
Miquel Raynal | 886ccc0 | 2018-05-15 11:57:00 +0200 | [diff] [blame] | 664 | do_tpm_nv_read_value, "", ""), |
Che-liang Chiou | c18f901 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 665 | U_BOOT_CMD_MKENT(nv_write_value, 0, 1, |
Miquel Raynal | 886ccc0 | 2018-05-15 11:57:00 +0200 | [diff] [blame] | 666 | do_tpm_nv_write_value, "", ""), |
Che-liang Chiou | c18f901 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 667 | U_BOOT_CMD_MKENT(extend, 0, 1, |
Miquel Raynal | 886ccc0 | 2018-05-15 11:57:00 +0200 | [diff] [blame] | 668 | do_tpm_extend, "", ""), |
Che-liang Chiou | c18f901 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 669 | U_BOOT_CMD_MKENT(pcr_read, 0, 1, |
Miquel Raynal | 886ccc0 | 2018-05-15 11:57:00 +0200 | [diff] [blame] | 670 | do_tpm_pcr_read, "", ""), |
Che-liang Chiou | c18f901 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 671 | U_BOOT_CMD_MKENT(tsc_physical_presence, 0, 1, |
Miquel Raynal | 886ccc0 | 2018-05-15 11:57:00 +0200 | [diff] [blame] | 672 | do_tpm_tsc_physical_presence, "", ""), |
Che-liang Chiou | c18f901 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 673 | U_BOOT_CMD_MKENT(read_pubek, 0, 1, |
Miquel Raynal | 886ccc0 | 2018-05-15 11:57:00 +0200 | [diff] [blame] | 674 | do_tpm_read_pubek, "", ""), |
Che-liang Chiou | c18f901 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 675 | U_BOOT_CMD_MKENT(physical_set_deactivated, 0, 1, |
Miquel Raynal | 886ccc0 | 2018-05-15 11:57:00 +0200 | [diff] [blame] | 676 | do_tpm_physical_set_deactivated, "", ""), |
Che-liang Chiou | c18f901 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 677 | U_BOOT_CMD_MKENT(get_capability, 0, 1, |
Miquel Raynal | 886ccc0 | 2018-05-15 11:57:00 +0200 | [diff] [blame] | 678 | do_tpm_get_capability, "", ""), |
Che-liang Chiou | c18f901 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 679 | U_BOOT_CMD_MKENT(raw_transfer, 0, 1, |
Miquel Raynal | 886ccc0 | 2018-05-15 11:57:00 +0200 | [diff] [blame] | 680 | do_tpm_raw_transfer, "", ""), |
Che-liang Chiou | c18f901 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 681 | U_BOOT_CMD_MKENT(nv_define, 0, 1, |
Miquel Raynal | 886ccc0 | 2018-05-15 11:57:00 +0200 | [diff] [blame] | 682 | do_tpm_nv_define, "", ""), |
Che-liang Chiou | c18f901 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 683 | U_BOOT_CMD_MKENT(nv_read, 0, 1, |
Miquel Raynal | 886ccc0 | 2018-05-15 11:57:00 +0200 | [diff] [blame] | 684 | do_tpm_nv_read, "", ""), |
Che-liang Chiou | c18f901 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 685 | U_BOOT_CMD_MKENT(nv_write, 0, 1, |
Miquel Raynal | 886ccc0 | 2018-05-15 11:57:00 +0200 | [diff] [blame] | 686 | do_tpm_nv_write, "", ""), |
Reinhard Pfau | 4fece43 | 2013-06-26 15:55:13 +0200 | [diff] [blame] | 687 | #ifdef CONFIG_TPM_AUTH_SESSIONS |
| 688 | U_BOOT_CMD_MKENT(oiap, 0, 1, |
| 689 | do_tpm_oiap, "", ""), |
| 690 | U_BOOT_CMD_MKENT(end_oiap, 0, 1, |
| 691 | do_tpm_end_oiap, "", ""), |
| 692 | U_BOOT_CMD_MKENT(load_key2_oiap, 0, 1, |
| 693 | do_tpm_load_key2_oiap, "", ""), |
mario.six@gdsys.cc | a5a7ea2 | 2017-03-20 10:28:28 +0100 | [diff] [blame] | 694 | #ifdef CONFIG_TPM_LOAD_KEY_BY_SHA1 |
| 695 | U_BOOT_CMD_MKENT(load_key_by_sha1, 0, 1, |
| 696 | do_tpm_load_key_by_sha1, "", ""), |
| 697 | #endif /* CONFIG_TPM_LOAD_KEY_BY_SHA1 */ |
Reinhard Pfau | 4fece43 | 2013-06-26 15:55:13 +0200 | [diff] [blame] | 698 | U_BOOT_CMD_MKENT(get_pub_key_oiap, 0, 1, |
| 699 | do_tpm_get_pub_key_oiap, "", ""), |
| 700 | #endif /* CONFIG_TPM_AUTH_SESSIONS */ |
Mario Six | 4eceb6c | 2017-01-11 16:00:50 +0100 | [diff] [blame] | 701 | #ifdef CONFIG_TPM_FLUSH_RESOURCES |
| 702 | U_BOOT_CMD_MKENT(flush, 0, 1, |
| 703 | do_tpm_flush, "", ""), |
| 704 | #endif /* CONFIG_TPM_FLUSH_RESOURCES */ |
mario.six@gdsys.cc | a5bfcc5 | 2017-03-20 10:28:30 +0100 | [diff] [blame] | 705 | #ifdef CONFIG_TPM_LIST_RESOURCES |
| 706 | U_BOOT_CMD_MKENT(list, 0, 1, |
| 707 | do_tpm_list, "", ""), |
| 708 | #endif /* CONFIG_TPM_LIST_RESOURCES */ |
Che-liang Chiou | c18f901 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 709 | }; |
| 710 | |
Miquel Raynal | 79920d1 | 2018-07-19 22:35:09 +0200 | [diff] [blame] | 711 | cmd_tbl_t *get_tpm1_commands(unsigned int *size) |
Che-liang Chiou | c18f901 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 712 | { |
Miquel Raynal | 4c6759e | 2018-05-15 11:57:06 +0200 | [diff] [blame] | 713 | *size = ARRAY_SIZE(tpm1_commands); |
Che-liang Chiou | c18f901 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 714 | |
Miquel Raynal | 4c6759e | 2018-05-15 11:57:06 +0200 | [diff] [blame] | 715 | return tpm1_commands; |
Che-liang Chiou | c18f901 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 716 | } |
| 717 | |
| 718 | U_BOOT_CMD(tpm, CONFIG_SYS_MAXARGS, 1, do_tpm, |
Miquel Raynal | 4c6759e | 2018-05-15 11:57:06 +0200 | [diff] [blame] | 719 | "Issue a TPMv1.x command", |
Che-liang Chiou | c18f901 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 720 | "cmd args...\n" |
| 721 | " - Issue TPM command <cmd> with arguments <args...>.\n" |
| 722 | "Admin Startup and State Commands:\n" |
Simon Glass | aa6ac63 | 2015-08-22 18:31:40 -0600 | [diff] [blame] | 723 | " info - Show information about the TPM\n" |
Che-liang Chiou | c18f901 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 724 | " init\n" |
| 725 | " - Put TPM into a state where it waits for 'startup' command.\n" |
| 726 | " startup mode\n" |
| 727 | " - Issue TPM_Starup command. <mode> is one of TPM_ST_CLEAR,\n" |
| 728 | " TPM_ST_STATE, and TPM_ST_DEACTIVATED.\n" |
| 729 | "Admin Testing Commands:\n" |
| 730 | " self_test_full\n" |
| 731 | " - Test all of the TPM capabilities.\n" |
| 732 | " continue_self_test\n" |
| 733 | " - Inform TPM that it should complete the self-test.\n" |
| 734 | "Admin Opt-in Commands:\n" |
| 735 | " physical_enable\n" |
| 736 | " - Set the PERMANENT disable flag to FALSE using physical presence as\n" |
| 737 | " authorization.\n" |
| 738 | " physical_disable\n" |
| 739 | " - Set the PERMANENT disable flag to TRUE using physical presence as\n" |
| 740 | " authorization.\n" |
| 741 | " physical_set_deactivated 0|1\n" |
| 742 | " - Set deactivated flag.\n" |
| 743 | "Admin Ownership Commands:\n" |
| 744 | " force_clear\n" |
| 745 | " - Issue TPM_ForceClear command.\n" |
| 746 | " tsc_physical_presence flags\n" |
| 747 | " - Set TPM device's Physical Presence flags to <flags>.\n" |
| 748 | "The Capability Commands:\n" |
| 749 | " get_capability cap_area sub_cap addr count\n" |
| 750 | " - Read <count> bytes of TPM capability indexed by <cap_area> and\n" |
| 751 | " <sub_cap> to memory address <addr>.\n" |
mario.six@gdsys.cc | a5bfcc5 | 2017-03-20 10:28:30 +0100 | [diff] [blame] | 752 | #if defined(CONFIG_TPM_FLUSH_RESOURCES) || defined(CONFIG_TPM_LIST_RESOURCES) |
Mario Six | 4eceb6c | 2017-01-11 16:00:50 +0100 | [diff] [blame] | 753 | "Resource management functions\n" |
mario.six@gdsys.cc | a5bfcc5 | 2017-03-20 10:28:30 +0100 | [diff] [blame] | 754 | #endif |
| 755 | #ifdef CONFIG_TPM_FLUSH_RESOURCES |
Mario Six | 4eceb6c | 2017-01-11 16:00:50 +0100 | [diff] [blame] | 756 | " flush resource_type id\n" |
| 757 | " - flushes a resource of type <resource_type> (may be one of key, auth,\n" |
| 758 | " hash, trans, context, counter, delegate, daa_tpm, daa_v0, daa_v1),\n" |
| 759 | " and id <id> from the TPM. Use an <id> of \"all\" to flush all\n" |
| 760 | " resources of that type.\n" |
| 761 | #endif /* CONFIG_TPM_FLUSH_RESOURCES */ |
mario.six@gdsys.cc | a5bfcc5 | 2017-03-20 10:28:30 +0100 | [diff] [blame] | 762 | #ifdef CONFIG_TPM_LIST_RESOURCES |
| 763 | " list resource_type\n" |
| 764 | " - lists resources of type <resource_type> (may be one of key, auth,\n" |
| 765 | " hash, trans, context, counter, delegate, daa_tpm, daa_v0, daa_v1),\n" |
| 766 | " contained in the TPM.\n" |
| 767 | #endif /* CONFIG_TPM_LIST_RESOURCES */ |
Reinhard Pfau | 4fece43 | 2013-06-26 15:55:13 +0200 | [diff] [blame] | 768 | #ifdef CONFIG_TPM_AUTH_SESSIONS |
| 769 | "Storage functions\n" |
| 770 | " loadkey2_oiap parent_handle key_addr key_len usage_auth\n" |
| 771 | " - loads a key data from memory address <key_addr>, <key_len> bytes\n" |
| 772 | " into TPM using the parent key <parent_handle> with authorization\n" |
| 773 | " <usage_auth> (20 bytes hex string).\n" |
mario.six@gdsys.cc | a5a7ea2 | 2017-03-20 10:28:28 +0100 | [diff] [blame] | 774 | #ifdef CONFIG_TPM_LOAD_KEY_BY_SHA1 |
| 775 | " load_key_by_sha1 parent_hash key_addr key_len usage_auth\n" |
| 776 | " - loads a key data from memory address <key_addr>, <key_len> bytes\n" |
| 777 | " into TPM using the parent hash <parent_hash> (20 bytes hex string)\n" |
| 778 | " with authorization <usage_auth> (20 bytes hex string).\n" |
| 779 | #endif /* CONFIG_TPM_LOAD_KEY_BY_SHA1 */ |
Reinhard Pfau | 4fece43 | 2013-06-26 15:55:13 +0200 | [diff] [blame] | 780 | " get_pub_key_oiap key_handle usage_auth\n" |
| 781 | " - get the public key portion of a loaded key <key_handle> using\n" |
| 782 | " authorization <usage auth> (20 bytes hex string)\n" |
| 783 | #endif /* CONFIG_TPM_AUTH_SESSIONS */ |
Che-liang Chiou | c18f901 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 784 | "Endorsement Key Handling Commands:\n" |
| 785 | " read_pubek addr count\n" |
| 786 | " - Read <count> bytes of the public endorsement key to memory\n" |
| 787 | " address <addr>\n" |
| 788 | "Integrity Collection and Reporting Commands:\n" |
| 789 | " extend index digest_hex_string\n" |
| 790 | " - Add a new measurement to a PCR. Update PCR <index> with the 20-bytes\n" |
| 791 | " <digest_hex_string>\n" |
| 792 | " pcr_read index addr count\n" |
| 793 | " - Read <count> bytes from PCR <index> to memory address <addr>.\n" |
Reinhard Pfau | 4fece43 | 2013-06-26 15:55:13 +0200 | [diff] [blame] | 794 | #ifdef CONFIG_TPM_AUTH_SESSIONS |
| 795 | "Authorization Sessions\n" |
| 796 | " oiap\n" |
| 797 | " - setup an OIAP session\n" |
| 798 | " end_oiap\n" |
| 799 | " - terminates an active OIAP session\n" |
| 800 | #endif /* CONFIG_TPM_AUTH_SESSIONS */ |
Che-liang Chiou | c18f901 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 801 | "Non-volatile Storage Commands:\n" |
| 802 | " nv_define_space index permission size\n" |
| 803 | " - Establish a space at index <index> with <permission> of <size> bytes.\n" |
| 804 | " nv_read_value index addr count\n" |
| 805 | " - Read <count> bytes from space <index> to memory address <addr>.\n" |
| 806 | " nv_write_value index addr count\n" |
| 807 | " - Write <count> bytes from memory address <addr> to space <index>.\n" |
| 808 | "Miscellaneous helper functions:\n" |
| 809 | " raw_transfer byte_string\n" |
| 810 | " - Send a byte string <byte_string> to TPM and print the response.\n" |
| 811 | " Non-volatile storage helper functions:\n" |
| 812 | " These helper functions treat a non-volatile space as a non-padded\n" |
| 813 | " sequence of integer values. These integer values are defined by a type\n" |
| 814 | " string, which is a text string of 'bwd' characters: 'b' means a 8-bit\n" |
| 815 | " value, 'w' 16-bit value, 'd' 32-bit value. All helper functions take\n" |
| 816 | " a type string as their first argument.\n" |
| 817 | " nv_define type_string index perm\n" |
| 818 | " - Define a space <index> with permission <perm>.\n" |
| 819 | " nv_read types_string index vars...\n" |
| 820 | " - Read from space <index> to environment variables <vars...>.\n" |
| 821 | " nv_write types_string index values...\n" |
| 822 | " - Write to space <index> from values <values...>.\n" |
Luigi Semenzato | 04d1bd2 | 2012-12-05 14:46:44 +0000 | [diff] [blame] | 823 | ); |