Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Simon Glass | 45158dd | 2015-08-22 18:31:42 -0600 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (c) 2015 Google, Inc |
Simon Glass | 45158dd | 2015-08-22 18:31:42 -0600 | [diff] [blame] | 4 | */ |
| 5 | |
| 6 | #include <common.h> |
| 7 | #include <command.h> |
Miquel Raynal | 4c6759e | 2018-05-15 11:57:06 +0200 | [diff] [blame] | 8 | #include <tpm-v1.h> |
Simon Glass | 8ceca1d | 2018-11-18 14:22:27 -0700 | [diff] [blame] | 9 | #include "tpm-user-utils.h" |
Simon Glass | 45158dd | 2015-08-22 18:31:42 -0600 | [diff] [blame] | 10 | |
| 11 | /* Prints error and returns on failure */ |
| 12 | #define TPM_CHECK(tpm_command) do { \ |
| 13 | uint32_t result; \ |
| 14 | \ |
| 15 | result = (tpm_command); \ |
| 16 | if (result != TPM_SUCCESS) { \ |
| 17 | printf("TEST FAILED: line %d: " #tpm_command ": 0x%x\n", \ |
| 18 | __LINE__, result); \ |
| 19 | return result; \ |
| 20 | } \ |
| 21 | } while (0) |
| 22 | |
| 23 | #define INDEX0 0xda70 |
| 24 | #define INDEX1 0xda71 |
| 25 | #define INDEX2 0xda72 |
| 26 | #define INDEX3 0xda73 |
| 27 | #define INDEX_INITIALISED 0xda80 |
| 28 | #define PHYS_PRESENCE 4 |
| 29 | #define PRESENCE 8 |
| 30 | |
Simon Glass | 8ceca1d | 2018-11-18 14:22:27 -0700 | [diff] [blame] | 31 | static uint32_t TlclStartupIfNeeded(struct udevice *dev) |
Simon Glass | 45158dd | 2015-08-22 18:31:42 -0600 | [diff] [blame] | 32 | { |
Simon Glass | 8ceca1d | 2018-11-18 14:22:27 -0700 | [diff] [blame] | 33 | uint32_t result = tpm_startup(dev, TPM_ST_CLEAR); |
Simon Glass | 45158dd | 2015-08-22 18:31:42 -0600 | [diff] [blame] | 34 | |
| 35 | return result == TPM_INVALID_POSTINIT ? TPM_SUCCESS : result; |
| 36 | } |
| 37 | |
Simon Glass | 8ceca1d | 2018-11-18 14:22:27 -0700 | [diff] [blame] | 38 | static int test_timer(struct udevice *dev) |
Simon Glass | 45158dd | 2015-08-22 18:31:42 -0600 | [diff] [blame] | 39 | { |
| 40 | printf("get_timer(0) = %lu\n", get_timer(0)); |
| 41 | return 0; |
| 42 | } |
| 43 | |
Simon Glass | 8ceca1d | 2018-11-18 14:22:27 -0700 | [diff] [blame] | 44 | static uint32_t tpm_get_flags(struct udevice *dev, uint8_t *disable, |
| 45 | uint8_t *deactivated, uint8_t *nvlocked) |
Simon Glass | 45158dd | 2015-08-22 18:31:42 -0600 | [diff] [blame] | 46 | { |
| 47 | struct tpm_permanent_flags pflags; |
| 48 | uint32_t result; |
| 49 | |
Simon Glass | 8ceca1d | 2018-11-18 14:22:27 -0700 | [diff] [blame] | 50 | result = tpm_get_permanent_flags(dev, &pflags); |
Simon Glass | 45158dd | 2015-08-22 18:31:42 -0600 | [diff] [blame] | 51 | if (result) |
| 52 | return result; |
| 53 | if (disable) |
| 54 | *disable = pflags.disable; |
| 55 | if (deactivated) |
| 56 | *deactivated = pflags.deactivated; |
| 57 | if (nvlocked) |
| 58 | *nvlocked = pflags.nv_locked; |
| 59 | debug("TPM: Got flags disable=%d, deactivated=%d, nvlocked=%d\n", |
| 60 | pflags.disable, pflags.deactivated, pflags.nv_locked); |
| 61 | |
| 62 | return 0; |
| 63 | } |
| 64 | |
Simon Glass | 8ceca1d | 2018-11-18 14:22:27 -0700 | [diff] [blame] | 65 | static uint32_t tpm_nv_write_value_lock(struct udevice *dev, uint32_t index) |
Simon Glass | 45158dd | 2015-08-22 18:31:42 -0600 | [diff] [blame] | 66 | { |
| 67 | debug("TPM: Write lock 0x%x\n", index); |
| 68 | |
Simon Glass | 8ceca1d | 2018-11-18 14:22:27 -0700 | [diff] [blame] | 69 | return tpm_nv_write_value(dev, index, NULL, 0); |
Simon Glass | 45158dd | 2015-08-22 18:31:42 -0600 | [diff] [blame] | 70 | } |
| 71 | |
Simon Glass | 8ceca1d | 2018-11-18 14:22:27 -0700 | [diff] [blame] | 72 | static int tpm_is_owned(struct udevice *dev) |
Simon Glass | 45158dd | 2015-08-22 18:31:42 -0600 | [diff] [blame] | 73 | { |
| 74 | uint8_t response[TPM_PUBEK_SIZE]; |
| 75 | uint32_t result; |
| 76 | |
Simon Glass | 8ceca1d | 2018-11-18 14:22:27 -0700 | [diff] [blame] | 77 | result = tpm_read_pubek(dev, response, sizeof(response)); |
Simon Glass | 45158dd | 2015-08-22 18:31:42 -0600 | [diff] [blame] | 78 | |
| 79 | return result != TPM_SUCCESS; |
| 80 | } |
| 81 | |
Simon Glass | 8ceca1d | 2018-11-18 14:22:27 -0700 | [diff] [blame] | 82 | static int test_early_extend(struct udevice *dev) |
Simon Glass | 45158dd | 2015-08-22 18:31:42 -0600 | [diff] [blame] | 83 | { |
| 84 | uint8_t value_in[20]; |
| 85 | uint8_t value_out[20]; |
| 86 | |
| 87 | printf("Testing earlyextend ..."); |
Simon Glass | 8ceca1d | 2018-11-18 14:22:27 -0700 | [diff] [blame] | 88 | tpm_init(dev); |
| 89 | TPM_CHECK(tpm_startup(dev, TPM_ST_CLEAR)); |
| 90 | TPM_CHECK(tpm_continue_self_test(dev)); |
| 91 | TPM_CHECK(tpm_extend(dev, 1, value_in, value_out)); |
Simon Glass | 45158dd | 2015-08-22 18:31:42 -0600 | [diff] [blame] | 92 | printf("done\n"); |
| 93 | return 0; |
| 94 | } |
| 95 | |
Simon Glass | 8ceca1d | 2018-11-18 14:22:27 -0700 | [diff] [blame] | 96 | static int test_early_nvram(struct udevice *dev) |
Simon Glass | 45158dd | 2015-08-22 18:31:42 -0600 | [diff] [blame] | 97 | { |
| 98 | uint32_t x; |
| 99 | |
| 100 | printf("Testing earlynvram ..."); |
Simon Glass | 8ceca1d | 2018-11-18 14:22:27 -0700 | [diff] [blame] | 101 | tpm_init(dev); |
| 102 | TPM_CHECK(tpm_startup(dev, TPM_ST_CLEAR)); |
| 103 | TPM_CHECK(tpm_continue_self_test(dev)); |
| 104 | TPM_CHECK(tpm_tsc_physical_presence(dev, PRESENCE)); |
| 105 | TPM_CHECK(tpm_nv_read_value(dev, INDEX0, (uint8_t *)&x, sizeof(x))); |
Simon Glass | 45158dd | 2015-08-22 18:31:42 -0600 | [diff] [blame] | 106 | printf("done\n"); |
| 107 | return 0; |
| 108 | } |
| 109 | |
Simon Glass | 8ceca1d | 2018-11-18 14:22:27 -0700 | [diff] [blame] | 110 | static int test_early_nvram2(struct udevice *dev) |
Simon Glass | 45158dd | 2015-08-22 18:31:42 -0600 | [diff] [blame] | 111 | { |
| 112 | uint32_t x; |
| 113 | |
| 114 | printf("Testing earlynvram2 ..."); |
Simon Glass | 8ceca1d | 2018-11-18 14:22:27 -0700 | [diff] [blame] | 115 | tpm_init(dev); |
| 116 | TPM_CHECK(tpm_startup(dev, TPM_ST_CLEAR)); |
| 117 | TPM_CHECK(tpm_continue_self_test(dev)); |
| 118 | TPM_CHECK(tpm_tsc_physical_presence(dev, PRESENCE)); |
| 119 | TPM_CHECK(tpm_nv_write_value(dev, INDEX0, (uint8_t *)&x, sizeof(x))); |
Simon Glass | 45158dd | 2015-08-22 18:31:42 -0600 | [diff] [blame] | 120 | printf("done\n"); |
| 121 | return 0; |
| 122 | } |
| 123 | |
Simon Glass | 8ceca1d | 2018-11-18 14:22:27 -0700 | [diff] [blame] | 124 | static int test_enable(struct udevice *dev) |
Simon Glass | 45158dd | 2015-08-22 18:31:42 -0600 | [diff] [blame] | 125 | { |
| 126 | uint8_t disable = 0, deactivated = 0; |
| 127 | |
| 128 | printf("Testing enable ...\n"); |
Simon Glass | 8ceca1d | 2018-11-18 14:22:27 -0700 | [diff] [blame] | 129 | tpm_init(dev); |
| 130 | TPM_CHECK(TlclStartupIfNeeded(dev)); |
| 131 | TPM_CHECK(tpm_self_test_full(dev)); |
| 132 | TPM_CHECK(tpm_tsc_physical_presence(dev, PRESENCE)); |
| 133 | TPM_CHECK(tpm_get_flags(dev, &disable, &deactivated, NULL)); |
Simon Glass | 45158dd | 2015-08-22 18:31:42 -0600 | [diff] [blame] | 134 | printf("\tdisable is %d, deactivated is %d\n", disable, deactivated); |
Simon Glass | 8ceca1d | 2018-11-18 14:22:27 -0700 | [diff] [blame] | 135 | TPM_CHECK(tpm_physical_enable(dev)); |
| 136 | TPM_CHECK(tpm_physical_set_deactivated(dev, 0)); |
| 137 | TPM_CHECK(tpm_get_flags(dev, &disable, &deactivated, NULL)); |
Simon Glass | 45158dd | 2015-08-22 18:31:42 -0600 | [diff] [blame] | 138 | printf("\tdisable is %d, deactivated is %d\n", disable, deactivated); |
| 139 | if (disable == 1 || deactivated == 1) |
| 140 | printf("\tfailed to enable or activate\n"); |
| 141 | printf("\tdone\n"); |
| 142 | return 0; |
| 143 | } |
| 144 | |
| 145 | #define reboot() do { \ |
| 146 | printf("\trebooting...\n"); \ |
| 147 | reset_cpu(0); \ |
| 148 | } while (0) |
| 149 | |
Simon Glass | 8ceca1d | 2018-11-18 14:22:27 -0700 | [diff] [blame] | 150 | static int test_fast_enable(struct udevice *dev) |
Simon Glass | 45158dd | 2015-08-22 18:31:42 -0600 | [diff] [blame] | 151 | { |
| 152 | uint8_t disable = 0, deactivated = 0; |
| 153 | int i; |
| 154 | |
| 155 | printf("Testing fastenable ...\n"); |
Simon Glass | 8ceca1d | 2018-11-18 14:22:27 -0700 | [diff] [blame] | 156 | tpm_init(dev); |
| 157 | TPM_CHECK(TlclStartupIfNeeded(dev)); |
| 158 | TPM_CHECK(tpm_self_test_full(dev)); |
| 159 | TPM_CHECK(tpm_tsc_physical_presence(dev, PRESENCE)); |
| 160 | TPM_CHECK(tpm_get_flags(dev, &disable, &deactivated, NULL)); |
Simon Glass | 45158dd | 2015-08-22 18:31:42 -0600 | [diff] [blame] | 161 | printf("\tdisable is %d, deactivated is %d\n", disable, deactivated); |
| 162 | for (i = 0; i < 2; i++) { |
Simon Glass | 8ceca1d | 2018-11-18 14:22:27 -0700 | [diff] [blame] | 163 | TPM_CHECK(tpm_force_clear(dev)); |
| 164 | TPM_CHECK(tpm_get_flags(dev, &disable, &deactivated, NULL)); |
Simon Glass | 45158dd | 2015-08-22 18:31:42 -0600 | [diff] [blame] | 165 | printf("\tdisable is %d, deactivated is %d\n", disable, |
| 166 | deactivated); |
| 167 | assert(disable == 1 && deactivated == 1); |
Simon Glass | 8ceca1d | 2018-11-18 14:22:27 -0700 | [diff] [blame] | 168 | TPM_CHECK(tpm_physical_enable(dev)); |
| 169 | TPM_CHECK(tpm_physical_set_deactivated(dev, 0)); |
| 170 | TPM_CHECK(tpm_get_flags(dev, &disable, &deactivated, NULL)); |
Simon Glass | 45158dd | 2015-08-22 18:31:42 -0600 | [diff] [blame] | 171 | printf("\tdisable is %d, deactivated is %d\n", disable, |
| 172 | deactivated); |
| 173 | assert(disable == 0 && deactivated == 0); |
| 174 | } |
| 175 | printf("\tdone\n"); |
| 176 | return 0; |
| 177 | } |
| 178 | |
Simon Glass | 8ceca1d | 2018-11-18 14:22:27 -0700 | [diff] [blame] | 179 | static int test_global_lock(struct udevice *dev) |
Simon Glass | 45158dd | 2015-08-22 18:31:42 -0600 | [diff] [blame] | 180 | { |
| 181 | uint32_t zero = 0; |
| 182 | uint32_t result; |
| 183 | uint32_t x; |
| 184 | |
| 185 | printf("Testing globallock ...\n"); |
Simon Glass | 8ceca1d | 2018-11-18 14:22:27 -0700 | [diff] [blame] | 186 | tpm_init(dev); |
| 187 | TPM_CHECK(TlclStartupIfNeeded(dev)); |
| 188 | TPM_CHECK(tpm_self_test_full(dev)); |
| 189 | TPM_CHECK(tpm_tsc_physical_presence(dev, PRESENCE)); |
| 190 | TPM_CHECK(tpm_nv_read_value(dev, INDEX0, (uint8_t *)&x, sizeof(x))); |
| 191 | TPM_CHECK(tpm_nv_write_value(dev, INDEX0, (uint8_t *)&zero, |
Simon Glass | 45158dd | 2015-08-22 18:31:42 -0600 | [diff] [blame] | 192 | sizeof(uint32_t))); |
Simon Glass | 8ceca1d | 2018-11-18 14:22:27 -0700 | [diff] [blame] | 193 | TPM_CHECK(tpm_nv_read_value(dev, INDEX1, (uint8_t *)&x, sizeof(x))); |
| 194 | TPM_CHECK(tpm_nv_write_value(dev, INDEX1, (uint8_t *)&zero, |
Simon Glass | 45158dd | 2015-08-22 18:31:42 -0600 | [diff] [blame] | 195 | sizeof(uint32_t))); |
Simon Glass | 8ceca1d | 2018-11-18 14:22:27 -0700 | [diff] [blame] | 196 | TPM_CHECK(tpm_set_global_lock(dev)); |
Simon Glass | 45158dd | 2015-08-22 18:31:42 -0600 | [diff] [blame] | 197 | /* Verifies that write to index0 fails */ |
| 198 | x = 1; |
Simon Glass | 8ceca1d | 2018-11-18 14:22:27 -0700 | [diff] [blame] | 199 | result = tpm_nv_write_value(dev, INDEX0, (uint8_t *)&x, sizeof(x)); |
Simon Glass | 45158dd | 2015-08-22 18:31:42 -0600 | [diff] [blame] | 200 | assert(result == TPM_AREA_LOCKED); |
Simon Glass | 8ceca1d | 2018-11-18 14:22:27 -0700 | [diff] [blame] | 201 | TPM_CHECK(tpm_nv_read_value(dev, INDEX0, (uint8_t *)&x, sizeof(x))); |
Simon Glass | 45158dd | 2015-08-22 18:31:42 -0600 | [diff] [blame] | 202 | assert(x == 0); |
| 203 | /* Verifies that write to index1 is still possible */ |
| 204 | x = 2; |
Simon Glass | 8ceca1d | 2018-11-18 14:22:27 -0700 | [diff] [blame] | 205 | TPM_CHECK(tpm_nv_write_value(dev, INDEX1, (uint8_t *)&x, sizeof(x))); |
| 206 | TPM_CHECK(tpm_nv_read_value(dev, INDEX1, (uint8_t *)&x, sizeof(x))); |
Simon Glass | 45158dd | 2015-08-22 18:31:42 -0600 | [diff] [blame] | 207 | assert(x == 2); |
| 208 | /* Turns off PP */ |
Simon Glass | 8ceca1d | 2018-11-18 14:22:27 -0700 | [diff] [blame] | 209 | tpm_tsc_physical_presence(dev, PHYS_PRESENCE); |
Simon Glass | 45158dd | 2015-08-22 18:31:42 -0600 | [diff] [blame] | 210 | /* Verifies that write to index1 fails */ |
| 211 | x = 3; |
Simon Glass | 8ceca1d | 2018-11-18 14:22:27 -0700 | [diff] [blame] | 212 | result = tpm_nv_write_value(dev, INDEX1, (uint8_t *)&x, sizeof(x)); |
Simon Glass | 45158dd | 2015-08-22 18:31:42 -0600 | [diff] [blame] | 213 | assert(result == TPM_BAD_PRESENCE); |
Simon Glass | 8ceca1d | 2018-11-18 14:22:27 -0700 | [diff] [blame] | 214 | TPM_CHECK(tpm_nv_read_value(dev, INDEX1, (uint8_t *)&x, sizeof(x))); |
Simon Glass | 45158dd | 2015-08-22 18:31:42 -0600 | [diff] [blame] | 215 | assert(x == 2); |
| 216 | printf("\tdone\n"); |
| 217 | return 0; |
| 218 | } |
| 219 | |
Simon Glass | 8ceca1d | 2018-11-18 14:22:27 -0700 | [diff] [blame] | 220 | static int test_lock(struct udevice *dev) |
Simon Glass | 45158dd | 2015-08-22 18:31:42 -0600 | [diff] [blame] | 221 | { |
| 222 | printf("Testing lock ...\n"); |
Simon Glass | 8ceca1d | 2018-11-18 14:22:27 -0700 | [diff] [blame] | 223 | tpm_init(dev); |
| 224 | tpm_startup(dev, TPM_ST_CLEAR); |
| 225 | tpm_self_test_full(dev); |
| 226 | tpm_tsc_physical_presence(dev, PRESENCE); |
| 227 | tpm_nv_write_value_lock(dev, INDEX0); |
Simon Glass | 45158dd | 2015-08-22 18:31:42 -0600 | [diff] [blame] | 228 | printf("\tLocked 0x%x\n", INDEX0); |
| 229 | printf("\tdone\n"); |
| 230 | return 0; |
| 231 | } |
| 232 | |
Simon Glass | 8ceca1d | 2018-11-18 14:22:27 -0700 | [diff] [blame] | 233 | static void initialise_spaces(struct udevice *dev) |
Simon Glass | 45158dd | 2015-08-22 18:31:42 -0600 | [diff] [blame] | 234 | { |
| 235 | uint32_t zero = 0; |
| 236 | uint32_t perm = TPM_NV_PER_WRITE_STCLEAR | TPM_NV_PER_PPWRITE; |
| 237 | |
| 238 | printf("\tInitialising spaces\n"); |
Simon Glass | 8ceca1d | 2018-11-18 14:22:27 -0700 | [diff] [blame] | 239 | tpm_nv_set_locked(dev); /* useful only the first time */ |
| 240 | tpm_nv_define_space(dev, INDEX0, perm, 4); |
| 241 | tpm_nv_write_value(dev, INDEX0, (uint8_t *)&zero, 4); |
| 242 | tpm_nv_define_space(dev, INDEX1, perm, 4); |
| 243 | tpm_nv_write_value(dev, INDEX1, (uint8_t *)&zero, 4); |
| 244 | tpm_nv_define_space(dev, INDEX2, perm, 4); |
| 245 | tpm_nv_write_value(dev, INDEX2, (uint8_t *)&zero, 4); |
| 246 | tpm_nv_define_space(dev, INDEX3, perm, 4); |
| 247 | tpm_nv_write_value(dev, INDEX3, (uint8_t *)&zero, 4); |
Simon Glass | 45158dd | 2015-08-22 18:31:42 -0600 | [diff] [blame] | 248 | perm = TPM_NV_PER_READ_STCLEAR | TPM_NV_PER_WRITE_STCLEAR | |
| 249 | TPM_NV_PER_PPWRITE; |
Simon Glass | 8ceca1d | 2018-11-18 14:22:27 -0700 | [diff] [blame] | 250 | tpm_nv_define_space(dev, INDEX_INITIALISED, perm, 1); |
Simon Glass | 45158dd | 2015-08-22 18:31:42 -0600 | [diff] [blame] | 251 | } |
| 252 | |
Simon Glass | 8ceca1d | 2018-11-18 14:22:27 -0700 | [diff] [blame] | 253 | static int test_readonly(struct udevice *dev) |
Simon Glass | 45158dd | 2015-08-22 18:31:42 -0600 | [diff] [blame] | 254 | { |
| 255 | uint8_t c; |
| 256 | uint32_t index_0, index_1, index_2, index_3; |
| 257 | int read0, read1, read2, read3; |
| 258 | |
| 259 | printf("Testing readonly ...\n"); |
Simon Glass | 8ceca1d | 2018-11-18 14:22:27 -0700 | [diff] [blame] | 260 | tpm_init(dev); |
| 261 | tpm_startup(dev, TPM_ST_CLEAR); |
| 262 | tpm_self_test_full(dev); |
| 263 | tpm_tsc_physical_presence(dev, PRESENCE); |
Simon Glass | 45158dd | 2015-08-22 18:31:42 -0600 | [diff] [blame] | 264 | /* |
| 265 | * Checks if initialisation has completed by trying to read-lock a |
| 266 | * space that's created at the end of initialisation |
| 267 | */ |
Simon Glass | 8ceca1d | 2018-11-18 14:22:27 -0700 | [diff] [blame] | 268 | if (tpm_nv_read_value(dev, INDEX_INITIALISED, &c, 0) == TPM_BADINDEX) { |
Simon Glass | 45158dd | 2015-08-22 18:31:42 -0600 | [diff] [blame] | 269 | /* The initialisation did not complete */ |
Simon Glass | 8ceca1d | 2018-11-18 14:22:27 -0700 | [diff] [blame] | 270 | initialise_spaces(dev); |
Simon Glass | 45158dd | 2015-08-22 18:31:42 -0600 | [diff] [blame] | 271 | } |
| 272 | |
| 273 | /* Checks if spaces are OK or messed up */ |
Simon Glass | 8ceca1d | 2018-11-18 14:22:27 -0700 | [diff] [blame] | 274 | read0 = tpm_nv_read_value(dev, INDEX0, (uint8_t *)&index_0, |
| 275 | sizeof(index_0)); |
| 276 | read1 = tpm_nv_read_value(dev, INDEX1, (uint8_t *)&index_1, |
| 277 | sizeof(index_1)); |
| 278 | read2 = tpm_nv_read_value(dev, INDEX2, (uint8_t *)&index_2, |
| 279 | sizeof(index_2)); |
| 280 | read3 = tpm_nv_read_value(dev, INDEX3, (uint8_t *)&index_3, |
| 281 | sizeof(index_3)); |
Simon Glass | 45158dd | 2015-08-22 18:31:42 -0600 | [diff] [blame] | 282 | if (read0 || read1 || read2 || read3) { |
| 283 | printf("Invalid contents\n"); |
| 284 | return 0; |
| 285 | } |
| 286 | |
| 287 | /* |
| 288 | * Writes space, and locks it. Then attempts to write again. |
| 289 | * I really wish I could use the imperative. |
| 290 | */ |
| 291 | index_0 += 1; |
Simon Glass | 8ceca1d | 2018-11-18 14:22:27 -0700 | [diff] [blame] | 292 | if (tpm_nv_write_value(dev, INDEX0, (uint8_t *)&index_0, |
| 293 | sizeof(index_0) != |
Simon Glass | 45158dd | 2015-08-22 18:31:42 -0600 | [diff] [blame] | 294 | TPM_SUCCESS)) { |
Masahiro Yamada | 81e1042 | 2017-09-16 14:10:41 +0900 | [diff] [blame] | 295 | pr_err("\tcould not write index 0\n"); |
Simon Glass | 45158dd | 2015-08-22 18:31:42 -0600 | [diff] [blame] | 296 | } |
Simon Glass | 8ceca1d | 2018-11-18 14:22:27 -0700 | [diff] [blame] | 297 | tpm_nv_write_value_lock(dev, INDEX0); |
| 298 | if (tpm_nv_write_value(dev, INDEX0, (uint8_t *)&index_0, |
| 299 | sizeof(index_0)) == |
Simon Glass | 45158dd | 2015-08-22 18:31:42 -0600 | [diff] [blame] | 300 | TPM_SUCCESS) |
Masahiro Yamada | 81e1042 | 2017-09-16 14:10:41 +0900 | [diff] [blame] | 301 | pr_err("\tindex 0 is not locked\n"); |
Simon Glass | 45158dd | 2015-08-22 18:31:42 -0600 | [diff] [blame] | 302 | |
| 303 | printf("\tdone\n"); |
| 304 | return 0; |
| 305 | } |
| 306 | |
Simon Glass | 8ceca1d | 2018-11-18 14:22:27 -0700 | [diff] [blame] | 307 | static int test_redefine_unowned(struct udevice *dev) |
Simon Glass | 45158dd | 2015-08-22 18:31:42 -0600 | [diff] [blame] | 308 | { |
| 309 | uint32_t perm; |
| 310 | uint32_t result; |
| 311 | uint32_t x; |
| 312 | |
| 313 | printf("Testing redefine_unowned ..."); |
Simon Glass | 8ceca1d | 2018-11-18 14:22:27 -0700 | [diff] [blame] | 314 | tpm_init(dev); |
| 315 | TPM_CHECK(TlclStartupIfNeeded(dev)); |
| 316 | TPM_CHECK(tpm_self_test_full(dev)); |
| 317 | TPM_CHECK(tpm_tsc_physical_presence(dev, PRESENCE)); |
| 318 | assert(!tpm_is_owned(dev)); |
Simon Glass | 45158dd | 2015-08-22 18:31:42 -0600 | [diff] [blame] | 319 | |
| 320 | /* Ensures spaces exist. */ |
Simon Glass | 8ceca1d | 2018-11-18 14:22:27 -0700 | [diff] [blame] | 321 | TPM_CHECK(tpm_nv_read_value(dev, INDEX0, (uint8_t *)&x, sizeof(x))); |
| 322 | TPM_CHECK(tpm_nv_read_value(dev, INDEX1, (uint8_t *)&x, sizeof(x))); |
Simon Glass | 45158dd | 2015-08-22 18:31:42 -0600 | [diff] [blame] | 323 | |
| 324 | /* Redefines spaces a couple of times. */ |
| 325 | perm = TPM_NV_PER_PPWRITE | TPM_NV_PER_GLOBALLOCK; |
Simon Glass | 8ceca1d | 2018-11-18 14:22:27 -0700 | [diff] [blame] | 326 | TPM_CHECK(tpm_nv_define_space(dev, INDEX0, perm, 2 * sizeof(uint32_t))); |
| 327 | TPM_CHECK(tpm_nv_define_space(dev, INDEX0, perm, sizeof(uint32_t))); |
Simon Glass | 45158dd | 2015-08-22 18:31:42 -0600 | [diff] [blame] | 328 | perm = TPM_NV_PER_PPWRITE; |
Simon Glass | 8ceca1d | 2018-11-18 14:22:27 -0700 | [diff] [blame] | 329 | TPM_CHECK(tpm_nv_define_space(dev, INDEX1, perm, 2 * sizeof(uint32_t))); |
| 330 | TPM_CHECK(tpm_nv_define_space(dev, INDEX1, perm, sizeof(uint32_t))); |
Simon Glass | 45158dd | 2015-08-22 18:31:42 -0600 | [diff] [blame] | 331 | |
| 332 | /* Sets the global lock */ |
Simon Glass | 8ceca1d | 2018-11-18 14:22:27 -0700 | [diff] [blame] | 333 | tpm_set_global_lock(dev); |
Simon Glass | 45158dd | 2015-08-22 18:31:42 -0600 | [diff] [blame] | 334 | |
| 335 | /* Verifies that index0 cannot be redefined */ |
Simon Glass | 8ceca1d | 2018-11-18 14:22:27 -0700 | [diff] [blame] | 336 | result = tpm_nv_define_space(dev, INDEX0, perm, sizeof(uint32_t)); |
Simon Glass | 45158dd | 2015-08-22 18:31:42 -0600 | [diff] [blame] | 337 | assert(result == TPM_AREA_LOCKED); |
| 338 | |
| 339 | /* Checks that index1 can */ |
Simon Glass | 8ceca1d | 2018-11-18 14:22:27 -0700 | [diff] [blame] | 340 | TPM_CHECK(tpm_nv_define_space(dev, INDEX1, perm, 2 * sizeof(uint32_t))); |
| 341 | TPM_CHECK(tpm_nv_define_space(dev, INDEX1, perm, sizeof(uint32_t))); |
Simon Glass | 45158dd | 2015-08-22 18:31:42 -0600 | [diff] [blame] | 342 | |
| 343 | /* Turns off PP */ |
Simon Glass | 8ceca1d | 2018-11-18 14:22:27 -0700 | [diff] [blame] | 344 | tpm_tsc_physical_presence(dev, PHYS_PRESENCE); |
Simon Glass | 45158dd | 2015-08-22 18:31:42 -0600 | [diff] [blame] | 345 | |
| 346 | /* Verifies that neither index0 nor index1 can be redefined */ |
Simon Glass | 8ceca1d | 2018-11-18 14:22:27 -0700 | [diff] [blame] | 347 | result = tpm_nv_define_space(dev, INDEX0, perm, sizeof(uint32_t)); |
Simon Glass | 45158dd | 2015-08-22 18:31:42 -0600 | [diff] [blame] | 348 | assert(result == TPM_BAD_PRESENCE); |
Simon Glass | 8ceca1d | 2018-11-18 14:22:27 -0700 | [diff] [blame] | 349 | result = tpm_nv_define_space(dev, INDEX1, perm, sizeof(uint32_t)); |
Simon Glass | 45158dd | 2015-08-22 18:31:42 -0600 | [diff] [blame] | 350 | assert(result == TPM_BAD_PRESENCE); |
| 351 | |
| 352 | printf("done\n"); |
| 353 | return 0; |
| 354 | } |
| 355 | |
| 356 | #define PERMPPGL (TPM_NV_PER_PPWRITE | TPM_NV_PER_GLOBALLOCK) |
| 357 | #define PERMPP TPM_NV_PER_PPWRITE |
| 358 | |
Simon Glass | 8ceca1d | 2018-11-18 14:22:27 -0700 | [diff] [blame] | 359 | static int test_space_perm(struct udevice *dev) |
Simon Glass | 45158dd | 2015-08-22 18:31:42 -0600 | [diff] [blame] | 360 | { |
| 361 | uint32_t perm; |
| 362 | |
| 363 | printf("Testing spaceperm ..."); |
Simon Glass | 8ceca1d | 2018-11-18 14:22:27 -0700 | [diff] [blame] | 364 | tpm_init(dev); |
| 365 | TPM_CHECK(TlclStartupIfNeeded(dev)); |
| 366 | TPM_CHECK(tpm_continue_self_test(dev)); |
| 367 | TPM_CHECK(tpm_tsc_physical_presence(dev, PRESENCE)); |
| 368 | TPM_CHECK(tpm_get_permissions(dev, INDEX0, &perm)); |
Simon Glass | 45158dd | 2015-08-22 18:31:42 -0600 | [diff] [blame] | 369 | assert((perm & PERMPPGL) == PERMPPGL); |
Simon Glass | 8ceca1d | 2018-11-18 14:22:27 -0700 | [diff] [blame] | 370 | TPM_CHECK(tpm_get_permissions(dev, INDEX1, &perm)); |
Simon Glass | 45158dd | 2015-08-22 18:31:42 -0600 | [diff] [blame] | 371 | assert((perm & PERMPP) == PERMPP); |
| 372 | printf("done\n"); |
| 373 | return 0; |
| 374 | } |
| 375 | |
Simon Glass | 8ceca1d | 2018-11-18 14:22:27 -0700 | [diff] [blame] | 376 | static int test_startup(struct udevice *dev) |
Simon Glass | 45158dd | 2015-08-22 18:31:42 -0600 | [diff] [blame] | 377 | { |
| 378 | uint32_t result; |
Simon Glass | 8ceca1d | 2018-11-18 14:22:27 -0700 | [diff] [blame] | 379 | |
Simon Glass | 45158dd | 2015-08-22 18:31:42 -0600 | [diff] [blame] | 380 | printf("Testing startup ...\n"); |
| 381 | |
Simon Glass | 8ceca1d | 2018-11-18 14:22:27 -0700 | [diff] [blame] | 382 | tpm_init(dev); |
| 383 | result = tpm_startup(dev, TPM_ST_CLEAR); |
Simon Glass | 45158dd | 2015-08-22 18:31:42 -0600 | [diff] [blame] | 384 | if (result != 0 && result != TPM_INVALID_POSTINIT) |
| 385 | printf("\ttpm startup failed with 0x%x\n", result); |
Simon Glass | 8ceca1d | 2018-11-18 14:22:27 -0700 | [diff] [blame] | 386 | result = tpm_get_flags(dev, NULL, NULL, NULL); |
Simon Glass | 45158dd | 2015-08-22 18:31:42 -0600 | [diff] [blame] | 387 | if (result != 0) |
| 388 | printf("\ttpm getflags failed with 0x%x\n", result); |
| 389 | printf("\texecuting SelfTestFull\n"); |
Simon Glass | 8ceca1d | 2018-11-18 14:22:27 -0700 | [diff] [blame] | 390 | tpm_self_test_full(dev); |
| 391 | result = tpm_get_flags(dev, NULL, NULL, NULL); |
Simon Glass | 45158dd | 2015-08-22 18:31:42 -0600 | [diff] [blame] | 392 | if (result != 0) |
| 393 | printf("\ttpm getflags failed with 0x%x\n", result); |
| 394 | printf("\tdone\n"); |
| 395 | return 0; |
| 396 | } |
| 397 | |
| 398 | /* |
| 399 | * Runs [op] and ensures it returns success and doesn't run longer than |
| 400 | * [time_limit] in milliseconds. |
| 401 | */ |
| 402 | #define TTPM_CHECK(op, time_limit) do { \ |
| 403 | ulong start, time; \ |
| 404 | uint32_t __result; \ |
| 405 | \ |
| 406 | start = get_timer(0); \ |
| 407 | __result = op; \ |
| 408 | if (__result != TPM_SUCCESS) { \ |
| 409 | printf("\t" #op ": error 0x%x\n", __result); \ |
| 410 | return -1; \ |
| 411 | } \ |
| 412 | time = get_timer(start); \ |
| 413 | printf("\t" #op ": %lu ms\n", time); \ |
| 414 | if (time > (ulong)time_limit) { \ |
| 415 | printf("\t" #op " exceeded " #time_limit " ms\n"); \ |
| 416 | } \ |
| 417 | } while (0) |
| 418 | |
| 419 | |
Simon Glass | 8ceca1d | 2018-11-18 14:22:27 -0700 | [diff] [blame] | 420 | static int test_timing(struct udevice *dev) |
Simon Glass | 45158dd | 2015-08-22 18:31:42 -0600 | [diff] [blame] | 421 | { |
Simon Glass | 45158dd | 2015-08-22 18:31:42 -0600 | [diff] [blame] | 422 | uint8_t in[20], out[20]; |
Simon Glass | 8ceca1d | 2018-11-18 14:22:27 -0700 | [diff] [blame] | 423 | uint32_t x; |
Simon Glass | 45158dd | 2015-08-22 18:31:42 -0600 | [diff] [blame] | 424 | |
| 425 | printf("Testing timing ..."); |
Simon Glass | 8ceca1d | 2018-11-18 14:22:27 -0700 | [diff] [blame] | 426 | tpm_init(dev); |
| 427 | TTPM_CHECK(TlclStartupIfNeeded(dev), 50); |
| 428 | TTPM_CHECK(tpm_continue_self_test(dev), 100); |
| 429 | TTPM_CHECK(tpm_self_test_full(dev), 1000); |
| 430 | TTPM_CHECK(tpm_tsc_physical_presence(dev, PRESENCE), 100); |
| 431 | TTPM_CHECK(tpm_nv_write_value(dev, INDEX0, (uint8_t *)&x, sizeof(x)), |
| 432 | 100); |
| 433 | TTPM_CHECK(tpm_nv_read_value(dev, INDEX0, (uint8_t *)&x, sizeof(x)), |
| 434 | 100); |
| 435 | TTPM_CHECK(tpm_extend(dev, 0, in, out), 200); |
| 436 | TTPM_CHECK(tpm_set_global_lock(dev), 50); |
| 437 | TTPM_CHECK(tpm_tsc_physical_presence(dev, PHYS_PRESENCE), 100); |
Simon Glass | 45158dd | 2015-08-22 18:31:42 -0600 | [diff] [blame] | 438 | printf("done\n"); |
| 439 | return 0; |
| 440 | } |
| 441 | |
| 442 | #define TPM_MAX_NV_WRITES_NOOWNER 64 |
| 443 | |
Simon Glass | 8ceca1d | 2018-11-18 14:22:27 -0700 | [diff] [blame] | 444 | static int test_write_limit(struct udevice *dev) |
Simon Glass | 45158dd | 2015-08-22 18:31:42 -0600 | [diff] [blame] | 445 | { |
Simon Glass | 45158dd | 2015-08-22 18:31:42 -0600 | [diff] [blame] | 446 | uint32_t result; |
Simon Glass | 8ceca1d | 2018-11-18 14:22:27 -0700 | [diff] [blame] | 447 | int i; |
Simon Glass | 45158dd | 2015-08-22 18:31:42 -0600 | [diff] [blame] | 448 | |
Simon Glass | 8ceca1d | 2018-11-18 14:22:27 -0700 | [diff] [blame] | 449 | printf("Testing writelimit ...\n"); |
| 450 | tpm_init(dev); |
| 451 | TPM_CHECK(TlclStartupIfNeeded(dev)); |
| 452 | TPM_CHECK(tpm_self_test_full(dev)); |
| 453 | TPM_CHECK(tpm_tsc_physical_presence(dev, PRESENCE)); |
| 454 | TPM_CHECK(tpm_force_clear(dev)); |
| 455 | TPM_CHECK(tpm_physical_enable(dev)); |
| 456 | TPM_CHECK(tpm_physical_set_deactivated(dev, 0)); |
Simon Glass | 45158dd | 2015-08-22 18:31:42 -0600 | [diff] [blame] | 457 | |
| 458 | for (i = 0; i < TPM_MAX_NV_WRITES_NOOWNER + 2; i++) { |
| 459 | printf("\twriting %d\n", i); |
Simon Glass | 8ceca1d | 2018-11-18 14:22:27 -0700 | [diff] [blame] | 460 | result = tpm_nv_write_value(dev, INDEX0, (uint8_t *)&i, |
| 461 | sizeof(i)); |
Simon Glass | 45158dd | 2015-08-22 18:31:42 -0600 | [diff] [blame] | 462 | switch (result) { |
| 463 | case TPM_SUCCESS: |
| 464 | break; |
| 465 | case TPM_MAXNVWRITES: |
| 466 | assert(i >= TPM_MAX_NV_WRITES_NOOWNER); |
| 467 | default: |
Masahiro Yamada | 81e1042 | 2017-09-16 14:10:41 +0900 | [diff] [blame] | 468 | pr_err("\tunexpected error code %d (0x%x)\n", |
Simon Glass | 45158dd | 2015-08-22 18:31:42 -0600 | [diff] [blame] | 469 | result, result); |
| 470 | } |
| 471 | } |
| 472 | |
| 473 | /* Reset write count */ |
Simon Glass | 8ceca1d | 2018-11-18 14:22:27 -0700 | [diff] [blame] | 474 | TPM_CHECK(tpm_force_clear(dev)); |
| 475 | TPM_CHECK(tpm_physical_enable(dev)); |
| 476 | TPM_CHECK(tpm_physical_set_deactivated(dev, 0)); |
Simon Glass | 45158dd | 2015-08-22 18:31:42 -0600 | [diff] [blame] | 477 | |
| 478 | /* Try writing again. */ |
Simon Glass | 8ceca1d | 2018-11-18 14:22:27 -0700 | [diff] [blame] | 479 | TPM_CHECK(tpm_nv_write_value(dev, INDEX0, (uint8_t *)&i, sizeof(i))); |
Simon Glass | 45158dd | 2015-08-22 18:31:42 -0600 | [diff] [blame] | 480 | printf("\tdone\n"); |
| 481 | return 0; |
| 482 | } |
| 483 | |
| 484 | #define VOIDTEST(XFUNC) \ |
| 485 | int do_test_##XFUNC(cmd_tbl_t *cmd_tbl, int flag, int argc, \ |
| 486 | char * const argv[]) \ |
| 487 | { \ |
Simon Glass | 8ceca1d | 2018-11-18 14:22:27 -0700 | [diff] [blame] | 488 | struct udevice *dev; \ |
| 489 | int ret; \ |
| 490 | \ |
| 491 | ret = get_tpm(&dev); \ |
| 492 | if (ret) \ |
| 493 | return ret; \ |
| 494 | return test_##XFUNC(dev); \ |
Simon Glass | 45158dd | 2015-08-22 18:31:42 -0600 | [diff] [blame] | 495 | } |
| 496 | |
| 497 | #define VOIDENT(XNAME) \ |
| 498 | U_BOOT_CMD_MKENT(XNAME, 0, 1, do_test_##XNAME, "", ""), |
| 499 | |
| 500 | VOIDTEST(early_extend) |
| 501 | VOIDTEST(early_nvram) |
| 502 | VOIDTEST(early_nvram2) |
| 503 | VOIDTEST(enable) |
| 504 | VOIDTEST(fast_enable) |
| 505 | VOIDTEST(global_lock) |
| 506 | VOIDTEST(lock) |
| 507 | VOIDTEST(readonly) |
| 508 | VOIDTEST(redefine_unowned) |
| 509 | VOIDTEST(space_perm) |
| 510 | VOIDTEST(startup) |
| 511 | VOIDTEST(timing) |
| 512 | VOIDTEST(write_limit) |
| 513 | VOIDTEST(timer) |
| 514 | |
| 515 | static cmd_tbl_t cmd_cros_tpm_sub[] = { |
| 516 | VOIDENT(early_extend) |
| 517 | VOIDENT(early_nvram) |
| 518 | VOIDENT(early_nvram2) |
| 519 | VOIDENT(enable) |
| 520 | VOIDENT(fast_enable) |
| 521 | VOIDENT(global_lock) |
| 522 | VOIDENT(lock) |
| 523 | VOIDENT(readonly) |
| 524 | VOIDENT(redefine_unowned) |
| 525 | VOIDENT(space_perm) |
| 526 | VOIDENT(startup) |
| 527 | VOIDENT(timing) |
| 528 | VOIDENT(write_limit) |
| 529 | VOIDENT(timer) |
| 530 | }; |
| 531 | |
| 532 | static int do_tpmtest(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) |
| 533 | { |
| 534 | cmd_tbl_t *c; |
Stefan Brüns | eefa302 | 2016-10-16 17:13:55 +0200 | [diff] [blame] | 535 | int i; |
Simon Glass | 45158dd | 2015-08-22 18:31:42 -0600 | [diff] [blame] | 536 | |
| 537 | printf("argc = %d, argv = ", argc); |
Stefan Brüns | eefa302 | 2016-10-16 17:13:55 +0200 | [diff] [blame] | 538 | |
| 539 | for (i = 0; i < argc; i++) |
| 540 | printf(" %s", argv[i]); |
| 541 | |
| 542 | printf("\n------\n"); |
Simon Glass | 45158dd | 2015-08-22 18:31:42 -0600 | [diff] [blame] | 543 | |
Simon Glass | 45158dd | 2015-08-22 18:31:42 -0600 | [diff] [blame] | 544 | argc--; |
| 545 | argv++; |
| 546 | c = find_cmd_tbl(argv[0], cmd_cros_tpm_sub, |
| 547 | ARRAY_SIZE(cmd_cros_tpm_sub)); |
| 548 | return c ? c->cmd(cmdtp, flag, argc, argv) : cmd_usage(cmdtp); |
| 549 | } |
| 550 | |
| 551 | U_BOOT_CMD(tpmtest, 2, 1, do_tpmtest, "TPM tests", |
| 552 | "\n\tearly_extend\n" |
| 553 | "\tearly_nvram\n" |
| 554 | "\tearly_nvram2\n" |
| 555 | "\tenable\n" |
| 556 | "\tfast_enable\n" |
| 557 | "\tglobal_lock\n" |
| 558 | "\tlock\n" |
| 559 | "\treadonly\n" |
| 560 | "\tredefine_unowned\n" |
| 561 | "\tspace_perm\n" |
| 562 | "\tstartup\n" |
| 563 | "\ttiming\n" |
| 564 | "\twrite_limit\n"); |