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