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