1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (c) 2015 Google, Inc
11 #include "tpm-user-utils.h"
14 /* Prints error and returns on failure */
15 #define TPM_CHECK(tpm_command) do { \
18 result = (tpm_command); \
19 if (result != TPM_SUCCESS) { \
20 printf("TEST FAILED: line %d: " #tpm_command ": 0x%x\n", \
30 #define INDEX_INITIALISED 0xda80
31 #define PHYS_PRESENCE 4
34 static uint32_t TlclStartupIfNeeded(struct udevice *dev)
36 uint32_t result = tpm_startup(dev, TPM_ST_CLEAR);
38 return result == TPM_INVALID_POSTINIT ? TPM_SUCCESS : result;
41 static int test_timer(struct udevice *dev)
43 printf("get_timer(0) = %lu\n", get_timer(0));
47 static uint32_t tpm_get_flags(struct udevice *dev, uint8_t *disable,
48 uint8_t *deactivated, uint8_t *nvlocked)
50 struct tpm_permanent_flags pflags;
53 result = tpm1_get_permanent_flags(dev, &pflags);
57 *disable = pflags.disable;
59 *deactivated = pflags.deactivated;
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);
68 static uint32_t tpm_nv_write_value_lock(struct udevice *dev, uint32_t index)
70 debug("TPM: Write lock 0x%x\n", index);
72 return tpm_nv_write_value(dev, index, NULL, 0);
75 static int tpm_is_owned(struct udevice *dev)
77 uint8_t response[TPM_PUBEK_SIZE];
80 result = tpm_read_pubek(dev, response, sizeof(response));
82 return result != TPM_SUCCESS;
85 static int test_early_extend(struct udevice *dev)
88 uint8_t value_out[20];
90 printf("Testing earlyextend ...");
92 TPM_CHECK(tpm_startup(dev, TPM_ST_CLEAR));
93 TPM_CHECK(tpm_continue_self_test(dev));
94 TPM_CHECK(tpm_pcr_extend(dev, 1, value_in, value_out));
99 static int test_early_nvram(struct udevice *dev)
103 printf("Testing earlynvram ...");
105 TPM_CHECK(tpm_startup(dev, TPM_ST_CLEAR));
106 TPM_CHECK(tpm_continue_self_test(dev));
107 TPM_CHECK(tpm_tsc_physical_presence(dev, PRESENCE));
108 TPM_CHECK(tpm_nv_read_value(dev, INDEX0, (uint8_t *)&x, sizeof(x)));
113 static int test_early_nvram2(struct udevice *dev)
117 printf("Testing earlynvram2 ...");
119 TPM_CHECK(tpm_startup(dev, TPM_ST_CLEAR));
120 TPM_CHECK(tpm_continue_self_test(dev));
121 TPM_CHECK(tpm_tsc_physical_presence(dev, PRESENCE));
122 TPM_CHECK(tpm_nv_write_value(dev, INDEX0, (uint8_t *)&x, sizeof(x)));
127 static int test_enable(struct udevice *dev)
129 uint8_t disable = 0, deactivated = 0;
131 printf("Testing enable ...\n");
133 TPM_CHECK(TlclStartupIfNeeded(dev));
134 TPM_CHECK(tpm_self_test_full(dev));
135 TPM_CHECK(tpm_tsc_physical_presence(dev, PRESENCE));
136 TPM_CHECK(tpm_get_flags(dev, &disable, &deactivated, NULL));
137 printf("\tdisable is %d, deactivated is %d\n", disable, deactivated);
138 TPM_CHECK(tpm_physical_enable(dev));
139 TPM_CHECK(tpm_physical_set_deactivated(dev, 0));
140 TPM_CHECK(tpm_get_flags(dev, &disable, &deactivated, NULL));
141 printf("\tdisable is %d, deactivated is %d\n", disable, deactivated);
142 if (disable == 1 || deactivated == 1)
143 printf("\tfailed to enable or activate\n");
148 #define reboot() do { \
149 printf("\trebooting...\n"); \
153 static int test_fast_enable(struct udevice *dev)
155 uint8_t disable = 0, deactivated = 0;
158 printf("Testing fastenable ...\n");
160 TPM_CHECK(TlclStartupIfNeeded(dev));
161 TPM_CHECK(tpm_self_test_full(dev));
162 TPM_CHECK(tpm_tsc_physical_presence(dev, PRESENCE));
163 TPM_CHECK(tpm_get_flags(dev, &disable, &deactivated, NULL));
164 printf("\tdisable is %d, deactivated is %d\n", disable, deactivated);
165 for (i = 0; i < 2; i++) {
166 TPM_CHECK(tpm_force_clear(dev));
167 TPM_CHECK(tpm_get_flags(dev, &disable, &deactivated, NULL));
168 printf("\tdisable is %d, deactivated is %d\n", disable,
170 assert(disable == 1 && deactivated == 1);
171 TPM_CHECK(tpm_physical_enable(dev));
172 TPM_CHECK(tpm_physical_set_deactivated(dev, 0));
173 TPM_CHECK(tpm_get_flags(dev, &disable, &deactivated, NULL));
174 printf("\tdisable is %d, deactivated is %d\n", disable,
176 assert(disable == 0 && deactivated == 0);
182 static int test_global_lock(struct udevice *dev)
188 printf("Testing globallock ...\n");
190 TPM_CHECK(TlclStartupIfNeeded(dev));
191 TPM_CHECK(tpm_self_test_full(dev));
192 TPM_CHECK(tpm_tsc_physical_presence(dev, PRESENCE));
193 TPM_CHECK(tpm_nv_read_value(dev, INDEX0, (uint8_t *)&x, sizeof(x)));
194 TPM_CHECK(tpm_nv_write_value(dev, INDEX0, (uint8_t *)&zero,
196 TPM_CHECK(tpm_nv_read_value(dev, INDEX1, (uint8_t *)&x, sizeof(x)));
197 TPM_CHECK(tpm_nv_write_value(dev, INDEX1, (uint8_t *)&zero,
199 TPM_CHECK(tpm_set_global_lock(dev));
200 /* Verifies that write to index0 fails */
202 result = tpm_nv_write_value(dev, INDEX0, (uint8_t *)&x, sizeof(x));
203 assert(result == TPM_AREA_LOCKED);
204 TPM_CHECK(tpm_nv_read_value(dev, INDEX0, (uint8_t *)&x, sizeof(x)));
206 /* Verifies that write to index1 is still possible */
208 TPM_CHECK(tpm_nv_write_value(dev, INDEX1, (uint8_t *)&x, sizeof(x)));
209 TPM_CHECK(tpm_nv_read_value(dev, INDEX1, (uint8_t *)&x, sizeof(x)));
212 tpm_tsc_physical_presence(dev, PHYS_PRESENCE);
213 /* Verifies that write to index1 fails */
215 result = tpm_nv_write_value(dev, INDEX1, (uint8_t *)&x, sizeof(x));
216 assert(result == TPM_BAD_PRESENCE);
217 TPM_CHECK(tpm_nv_read_value(dev, INDEX1, (uint8_t *)&x, sizeof(x)));
223 static int test_lock(struct udevice *dev)
225 printf("Testing lock ...\n");
227 tpm_startup(dev, TPM_ST_CLEAR);
228 tpm_self_test_full(dev);
229 tpm_tsc_physical_presence(dev, PRESENCE);
230 tpm_nv_write_value_lock(dev, INDEX0);
231 printf("\tLocked 0x%x\n", INDEX0);
236 static void initialise_spaces(struct udevice *dev)
239 uint32_t perm = TPM_NV_PER_WRITE_STCLEAR | TPM_NV_PER_PPWRITE;
241 printf("\tInitialising spaces\n");
242 tpm1_nv_set_locked(dev); /* useful only the first time */
243 tpm1_nv_define_space(dev, INDEX0, perm, 4);
244 tpm_nv_write_value(dev, INDEX0, (uint8_t *)&zero, 4);
245 tpm1_nv_define_space(dev, INDEX1, perm, 4);
246 tpm_nv_write_value(dev, INDEX1, (uint8_t *)&zero, 4);
247 tpm1_nv_define_space(dev, INDEX2, perm, 4);
248 tpm_nv_write_value(dev, INDEX2, (uint8_t *)&zero, 4);
249 tpm1_nv_define_space(dev, INDEX3, perm, 4);
250 tpm_nv_write_value(dev, INDEX3, (uint8_t *)&zero, 4);
251 perm = TPM_NV_PER_READ_STCLEAR | TPM_NV_PER_WRITE_STCLEAR |
253 tpm1_nv_define_space(dev, INDEX_INITIALISED, perm, 1);
256 static int test_readonly(struct udevice *dev)
259 uint32_t index_0, index_1, index_2, index_3;
260 int read0, read1, read2, read3;
262 printf("Testing readonly ...\n");
264 tpm_startup(dev, TPM_ST_CLEAR);
265 tpm_self_test_full(dev);
266 tpm_tsc_physical_presence(dev, PRESENCE);
268 * Checks if initialisation has completed by trying to read-lock a
269 * space that's created at the end of initialisation
271 if (tpm_nv_read_value(dev, INDEX_INITIALISED, &c, 0) == TPM_BADINDEX) {
272 /* The initialisation did not complete */
273 initialise_spaces(dev);
276 /* Checks if spaces are OK or messed up */
277 read0 = tpm_nv_read_value(dev, INDEX0, (uint8_t *)&index_0,
279 read1 = tpm_nv_read_value(dev, INDEX1, (uint8_t *)&index_1,
281 read2 = tpm_nv_read_value(dev, INDEX2, (uint8_t *)&index_2,
283 read3 = tpm_nv_read_value(dev, INDEX3, (uint8_t *)&index_3,
285 if (read0 || read1 || read2 || read3) {
286 printf("Invalid contents\n");
291 * Writes space, and locks it. Then attempts to write again.
292 * I really wish I could use the imperative.
295 if (tpm_nv_write_value(dev, INDEX0, (uint8_t *)&index_0,
298 pr_err("\tcould not write index 0\n");
300 tpm_nv_write_value_lock(dev, INDEX0);
301 if (tpm_nv_write_value(dev, INDEX0, (uint8_t *)&index_0,
304 pr_err("\tindex 0 is not locked\n");
310 static int test_redefine_unowned(struct udevice *dev)
316 printf("Testing redefine_unowned ...");
318 TPM_CHECK(TlclStartupIfNeeded(dev));
319 TPM_CHECK(tpm_self_test_full(dev));
320 TPM_CHECK(tpm_tsc_physical_presence(dev, PRESENCE));
321 assert(!tpm_is_owned(dev));
323 /* Ensures spaces exist. */
324 TPM_CHECK(tpm_nv_read_value(dev, INDEX0, (uint8_t *)&x, sizeof(x)));
325 TPM_CHECK(tpm_nv_read_value(dev, INDEX1, (uint8_t *)&x, sizeof(x)));
327 /* Redefines spaces a couple of times. */
328 perm = TPM_NV_PER_PPWRITE | TPM_NV_PER_GLOBALLOCK;
329 TPM_CHECK(tpm1_nv_define_space(dev, INDEX0, perm,
330 2 * sizeof(uint32_t)));
331 TPM_CHECK(tpm1_nv_define_space(dev, INDEX0, perm, sizeof(uint32_t)));
332 perm = TPM_NV_PER_PPWRITE;
333 TPM_CHECK(tpm1_nv_define_space(dev, INDEX1, perm,
334 2 * sizeof(uint32_t)));
335 TPM_CHECK(tpm1_nv_define_space(dev, INDEX1, perm, sizeof(uint32_t)));
337 /* Sets the global lock */
338 tpm_set_global_lock(dev);
340 /* Verifies that index0 cannot be redefined */
341 result = tpm1_nv_define_space(dev, INDEX0, perm, sizeof(uint32_t));
342 assert(result == TPM_AREA_LOCKED);
344 /* Checks that index1 can */
345 TPM_CHECK(tpm1_nv_define_space(dev, INDEX1, perm,
346 2 * sizeof(uint32_t)));
347 TPM_CHECK(tpm1_nv_define_space(dev, INDEX1, perm, sizeof(uint32_t)));
350 tpm_tsc_physical_presence(dev, PHYS_PRESENCE);
352 /* Verifies that neither index0 nor index1 can be redefined */
353 result = tpm1_nv_define_space(dev, INDEX0, perm, sizeof(uint32_t));
354 assert(result == TPM_BAD_PRESENCE);
355 result = tpm1_nv_define_space(dev, INDEX1, perm, sizeof(uint32_t));
356 assert(result == TPM_BAD_PRESENCE);
362 #define PERMPPGL (TPM_NV_PER_PPWRITE | TPM_NV_PER_GLOBALLOCK)
363 #define PERMPP TPM_NV_PER_PPWRITE
365 static int test_space_perm(struct udevice *dev)
369 printf("Testing spaceperm ...");
371 TPM_CHECK(TlclStartupIfNeeded(dev));
372 TPM_CHECK(tpm_continue_self_test(dev));
373 TPM_CHECK(tpm_tsc_physical_presence(dev, PRESENCE));
374 TPM_CHECK(tpm_get_permissions(dev, INDEX0, &perm));
375 assert((perm & PERMPPGL) == PERMPPGL);
376 TPM_CHECK(tpm_get_permissions(dev, INDEX1, &perm));
377 assert((perm & PERMPP) == PERMPP);
382 static int test_startup(struct udevice *dev)
386 printf("Testing startup ...\n");
389 result = tpm_startup(dev, TPM_ST_CLEAR);
390 if (result != 0 && result != TPM_INVALID_POSTINIT)
391 printf("\ttpm startup failed with 0x%x\n", result);
392 result = tpm_get_flags(dev, NULL, NULL, NULL);
394 printf("\ttpm getflags failed with 0x%x\n", result);
395 printf("\texecuting SelfTestFull\n");
396 tpm_self_test_full(dev);
397 result = tpm_get_flags(dev, NULL, NULL, NULL);
399 printf("\ttpm getflags failed with 0x%x\n", result);
405 * Runs [op] and ensures it returns success and doesn't run longer than
406 * [time_limit] in milliseconds.
408 #define TTPM_CHECK(op, time_limit) do { \
412 start = get_timer(0); \
414 if (__result != TPM_SUCCESS) { \
415 printf("\t" #op ": error 0x%x\n", __result); \
418 time = get_timer(start); \
419 printf("\t" #op ": %lu ms\n", time); \
420 if (time > (ulong)time_limit) { \
421 printf("\t" #op " exceeded " #time_limit " ms\n"); \
426 static int test_timing(struct udevice *dev)
428 uint8_t in[20], out[20];
431 printf("Testing timing ...");
433 TTPM_CHECK(TlclStartupIfNeeded(dev), 50);
434 TTPM_CHECK(tpm_continue_self_test(dev), 100);
435 TTPM_CHECK(tpm_self_test_full(dev), 1000);
436 TTPM_CHECK(tpm_tsc_physical_presence(dev, PRESENCE), 100);
437 TTPM_CHECK(tpm_nv_write_value(dev, INDEX0, (uint8_t *)&x, sizeof(x)),
439 TTPM_CHECK(tpm_nv_read_value(dev, INDEX0, (uint8_t *)&x, sizeof(x)),
441 TTPM_CHECK(tpm_pcr_extend(dev, 0, in, out), 200);
442 TTPM_CHECK(tpm_set_global_lock(dev), 50);
443 TTPM_CHECK(tpm_tsc_physical_presence(dev, PHYS_PRESENCE), 100);
448 #define TPM_MAX_NV_WRITES_NOOWNER 64
450 static int test_write_limit(struct udevice *dev)
455 printf("Testing writelimit ...\n");
457 TPM_CHECK(TlclStartupIfNeeded(dev));
458 TPM_CHECK(tpm_self_test_full(dev));
459 TPM_CHECK(tpm_tsc_physical_presence(dev, PRESENCE));
460 TPM_CHECK(tpm_force_clear(dev));
461 TPM_CHECK(tpm_physical_enable(dev));
462 TPM_CHECK(tpm_physical_set_deactivated(dev, 0));
464 for (i = 0; i < TPM_MAX_NV_WRITES_NOOWNER + 2; i++) {
465 printf("\twriting %d\n", i);
466 result = tpm_nv_write_value(dev, INDEX0, (uint8_t *)&i,
471 case TPM_MAXNVWRITES:
472 assert(i >= TPM_MAX_NV_WRITES_NOOWNER);
474 pr_err("\tunexpected error code %d (0x%x)\n",
479 /* Reset write count */
480 TPM_CHECK(tpm_force_clear(dev));
481 TPM_CHECK(tpm_physical_enable(dev));
482 TPM_CHECK(tpm_physical_set_deactivated(dev, 0));
484 /* Try writing again. */
485 TPM_CHECK(tpm_nv_write_value(dev, INDEX0, (uint8_t *)&i, sizeof(i)));
490 #define VOIDTEST(XFUNC) \
491 int do_test_##XFUNC(struct cmd_tbl *cmd_tbl, int flag, int argc, \
492 char *const argv[]) \
494 struct udevice *dev; \
497 ret = get_tpm(&dev); \
500 return test_##XFUNC(dev); \
503 #define VOIDENT(XNAME) \
504 U_BOOT_CMD_MKENT(XNAME, 0, 1, do_test_##XNAME, "", ""),
506 VOIDTEST(early_extend)
507 VOIDTEST(early_nvram)
508 VOIDTEST(early_nvram2)
510 VOIDTEST(fast_enable)
511 VOIDTEST(global_lock)
514 VOIDTEST(redefine_unowned)
518 VOIDTEST(write_limit)
521 static struct cmd_tbl cmd_cros_tpm_sub[] = {
522 VOIDENT(early_extend)
524 VOIDENT(early_nvram2)
530 VOIDENT(redefine_unowned)
538 static int do_tpmtest(struct cmd_tbl *cmdtp, int flag, int argc,
544 printf("argc = %d, argv = ", argc);
546 for (i = 0; i < argc; i++)
547 printf(" %s", argv[i]);
549 printf("\n------\n");
553 c = find_cmd_tbl(argv[0], cmd_cros_tpm_sub,
554 ARRAY_SIZE(cmd_cros_tpm_sub));
555 return c ? c->cmd(cmdtp, flag, argc, argv) : cmd_usage(cmdtp);
558 U_BOOT_CMD(tpmtest, 2, 1, do_tpmtest, "TPM tests",
567 "\tredefine_unowned\n"