1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (c) 2015 Google, Inc
11 #include "tpm-user-utils.h"
13 /* Prints error and returns on failure */
14 #define TPM_CHECK(tpm_command) do { \
17 result = (tpm_command); \
18 if (result != TPM_SUCCESS) { \
19 printf("TEST FAILED: line %d: " #tpm_command ": 0x%x\n", \
29 #define INDEX_INITIALISED 0xda80
30 #define PHYS_PRESENCE 4
33 static uint32_t TlclStartupIfNeeded(struct udevice *dev)
35 uint32_t result = tpm_startup(dev, TPM_ST_CLEAR);
37 return result == TPM_INVALID_POSTINIT ? TPM_SUCCESS : result;
40 static int test_timer(struct udevice *dev)
42 printf("get_timer(0) = %lu\n", get_timer(0));
46 static uint32_t tpm_get_flags(struct udevice *dev, uint8_t *disable,
47 uint8_t *deactivated, uint8_t *nvlocked)
49 struct tpm_permanent_flags pflags;
52 result = tpm_get_permanent_flags(dev, &pflags);
56 *disable = pflags.disable;
58 *deactivated = pflags.deactivated;
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);
67 static uint32_t tpm_nv_write_value_lock(struct udevice *dev, uint32_t index)
69 debug("TPM: Write lock 0x%x\n", index);
71 return tpm_nv_write_value(dev, index, NULL, 0);
74 static int tpm_is_owned(struct udevice *dev)
76 uint8_t response[TPM_PUBEK_SIZE];
79 result = tpm_read_pubek(dev, response, sizeof(response));
81 return result != TPM_SUCCESS;
84 static int test_early_extend(struct udevice *dev)
87 uint8_t value_out[20];
89 printf("Testing earlyextend ...");
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));
98 static int test_early_nvram(struct udevice *dev)
102 printf("Testing earlynvram ...");
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)));
112 static int test_early_nvram2(struct udevice *dev)
116 printf("Testing earlynvram2 ...");
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)));
126 static int test_enable(struct udevice *dev)
128 uint8_t disable = 0, deactivated = 0;
130 printf("Testing enable ...\n");
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));
136 printf("\tdisable is %d, deactivated is %d\n", disable, deactivated);
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));
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");
147 #define reboot() do { \
148 printf("\trebooting...\n"); \
152 static int test_fast_enable(struct udevice *dev)
154 uint8_t disable = 0, deactivated = 0;
157 printf("Testing fastenable ...\n");
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));
163 printf("\tdisable is %d, deactivated is %d\n", disable, deactivated);
164 for (i = 0; i < 2; i++) {
165 TPM_CHECK(tpm_force_clear(dev));
166 TPM_CHECK(tpm_get_flags(dev, &disable, &deactivated, NULL));
167 printf("\tdisable is %d, deactivated is %d\n", disable,
169 assert(disable == 1 && deactivated == 1);
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));
173 printf("\tdisable is %d, deactivated is %d\n", disable,
175 assert(disable == 0 && deactivated == 0);
181 static int test_global_lock(struct udevice *dev)
187 printf("Testing globallock ...\n");
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,
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,
198 TPM_CHECK(tpm_set_global_lock(dev));
199 /* Verifies that write to index0 fails */
201 result = tpm_nv_write_value(dev, INDEX0, (uint8_t *)&x, sizeof(x));
202 assert(result == TPM_AREA_LOCKED);
203 TPM_CHECK(tpm_nv_read_value(dev, INDEX0, (uint8_t *)&x, sizeof(x)));
205 /* Verifies that write to index1 is still possible */
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)));
211 tpm_tsc_physical_presence(dev, PHYS_PRESENCE);
212 /* Verifies that write to index1 fails */
214 result = tpm_nv_write_value(dev, INDEX1, (uint8_t *)&x, sizeof(x));
215 assert(result == TPM_BAD_PRESENCE);
216 TPM_CHECK(tpm_nv_read_value(dev, INDEX1, (uint8_t *)&x, sizeof(x)));
222 static int test_lock(struct udevice *dev)
224 printf("Testing lock ...\n");
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);
230 printf("\tLocked 0x%x\n", INDEX0);
235 static void initialise_spaces(struct udevice *dev)
238 uint32_t perm = TPM_NV_PER_WRITE_STCLEAR | TPM_NV_PER_PPWRITE;
240 printf("\tInitialising spaces\n");
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);
250 perm = TPM_NV_PER_READ_STCLEAR | TPM_NV_PER_WRITE_STCLEAR |
252 tpm_nv_define_space(dev, INDEX_INITIALISED, perm, 1);
255 static int test_readonly(struct udevice *dev)
258 uint32_t index_0, index_1, index_2, index_3;
259 int read0, read1, read2, read3;
261 printf("Testing readonly ...\n");
263 tpm_startup(dev, TPM_ST_CLEAR);
264 tpm_self_test_full(dev);
265 tpm_tsc_physical_presence(dev, PRESENCE);
267 * Checks if initialisation has completed by trying to read-lock a
268 * space that's created at the end of initialisation
270 if (tpm_nv_read_value(dev, INDEX_INITIALISED, &c, 0) == TPM_BADINDEX) {
271 /* The initialisation did not complete */
272 initialise_spaces(dev);
275 /* Checks if spaces are OK or messed up */
276 read0 = tpm_nv_read_value(dev, INDEX0, (uint8_t *)&index_0,
278 read1 = tpm_nv_read_value(dev, INDEX1, (uint8_t *)&index_1,
280 read2 = tpm_nv_read_value(dev, INDEX2, (uint8_t *)&index_2,
282 read3 = tpm_nv_read_value(dev, INDEX3, (uint8_t *)&index_3,
284 if (read0 || read1 || read2 || read3) {
285 printf("Invalid contents\n");
290 * Writes space, and locks it. Then attempts to write again.
291 * I really wish I could use the imperative.
294 if (tpm_nv_write_value(dev, INDEX0, (uint8_t *)&index_0,
297 pr_err("\tcould not write index 0\n");
299 tpm_nv_write_value_lock(dev, INDEX0);
300 if (tpm_nv_write_value(dev, INDEX0, (uint8_t *)&index_0,
303 pr_err("\tindex 0 is not locked\n");
309 static int test_redefine_unowned(struct udevice *dev)
315 printf("Testing redefine_unowned ...");
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));
322 /* Ensures spaces exist. */
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)));
326 /* Redefines spaces a couple of times. */
327 perm = TPM_NV_PER_PPWRITE | TPM_NV_PER_GLOBALLOCK;
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)));
330 perm = TPM_NV_PER_PPWRITE;
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)));
334 /* Sets the global lock */
335 tpm_set_global_lock(dev);
337 /* Verifies that index0 cannot be redefined */
338 result = tpm_nv_define_space(dev, INDEX0, perm, sizeof(uint32_t));
339 assert(result == TPM_AREA_LOCKED);
341 /* Checks that index1 can */
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)));
346 tpm_tsc_physical_presence(dev, PHYS_PRESENCE);
348 /* Verifies that neither index0 nor index1 can be redefined */
349 result = tpm_nv_define_space(dev, INDEX0, perm, sizeof(uint32_t));
350 assert(result == TPM_BAD_PRESENCE);
351 result = tpm_nv_define_space(dev, INDEX1, perm, sizeof(uint32_t));
352 assert(result == TPM_BAD_PRESENCE);
358 #define PERMPPGL (TPM_NV_PER_PPWRITE | TPM_NV_PER_GLOBALLOCK)
359 #define PERMPP TPM_NV_PER_PPWRITE
361 static int test_space_perm(struct udevice *dev)
365 printf("Testing spaceperm ...");
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));
371 assert((perm & PERMPPGL) == PERMPPGL);
372 TPM_CHECK(tpm_get_permissions(dev, INDEX1, &perm));
373 assert((perm & PERMPP) == PERMPP);
378 static int test_startup(struct udevice *dev)
382 printf("Testing startup ...\n");
385 result = tpm_startup(dev, TPM_ST_CLEAR);
386 if (result != 0 && result != TPM_INVALID_POSTINIT)
387 printf("\ttpm startup failed with 0x%x\n", result);
388 result = tpm_get_flags(dev, NULL, NULL, NULL);
390 printf("\ttpm getflags failed with 0x%x\n", result);
391 printf("\texecuting SelfTestFull\n");
392 tpm_self_test_full(dev);
393 result = tpm_get_flags(dev, NULL, NULL, NULL);
395 printf("\ttpm getflags failed with 0x%x\n", result);
401 * Runs [op] and ensures it returns success and doesn't run longer than
402 * [time_limit] in milliseconds.
404 #define TTPM_CHECK(op, time_limit) do { \
408 start = get_timer(0); \
410 if (__result != TPM_SUCCESS) { \
411 printf("\t" #op ": error 0x%x\n", __result); \
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"); \
422 static int test_timing(struct udevice *dev)
424 uint8_t in[20], out[20];
427 printf("Testing timing ...");
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)),
435 TTPM_CHECK(tpm_nv_read_value(dev, INDEX0, (uint8_t *)&x, sizeof(x)),
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);
444 #define TPM_MAX_NV_WRITES_NOOWNER 64
446 static int test_write_limit(struct udevice *dev)
451 printf("Testing writelimit ...\n");
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));
460 for (i = 0; i < TPM_MAX_NV_WRITES_NOOWNER + 2; i++) {
461 printf("\twriting %d\n", i);
462 result = tpm_nv_write_value(dev, INDEX0, (uint8_t *)&i,
467 case TPM_MAXNVWRITES:
468 assert(i >= TPM_MAX_NV_WRITES_NOOWNER);
470 pr_err("\tunexpected error code %d (0x%x)\n",
475 /* Reset write count */
476 TPM_CHECK(tpm_force_clear(dev));
477 TPM_CHECK(tpm_physical_enable(dev));
478 TPM_CHECK(tpm_physical_set_deactivated(dev, 0));
480 /* Try writing again. */
481 TPM_CHECK(tpm_nv_write_value(dev, INDEX0, (uint8_t *)&i, sizeof(i)));
486 #define VOIDTEST(XFUNC) \
487 int do_test_##XFUNC(struct cmd_tbl *cmd_tbl, int flag, int argc, \
488 char *const argv[]) \
490 struct udevice *dev; \
493 ret = get_tpm(&dev); \
496 return test_##XFUNC(dev); \
499 #define VOIDENT(XNAME) \
500 U_BOOT_CMD_MKENT(XNAME, 0, 1, do_test_##XNAME, "", ""),
502 VOIDTEST(early_extend)
503 VOIDTEST(early_nvram)
504 VOIDTEST(early_nvram2)
506 VOIDTEST(fast_enable)
507 VOIDTEST(global_lock)
510 VOIDTEST(redefine_unowned)
514 VOIDTEST(write_limit)
517 static struct cmd_tbl cmd_cros_tpm_sub[] = {
518 VOIDENT(early_extend)
520 VOIDENT(early_nvram2)
526 VOIDENT(redefine_unowned)
534 static int do_tpmtest(struct cmd_tbl *cmdtp, int flag, int argc,
540 printf("argc = %d, argv = ", argc);
542 for (i = 0; i < argc; i++)
543 printf(" %s", argv[i]);
545 printf("\n------\n");
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);
554 U_BOOT_CMD(tpmtest, 2, 1, do_tpmtest, "TPM tests",
563 "\tredefine_unowned\n"