2 * Copyright (c) 2015 Google, Inc
4 * SPDX-License-Identifier: GPL-2.0+
9 #include <environment.h>
12 /* Prints error and returns on failure */
13 #define TPM_CHECK(tpm_command) do { \
16 result = (tpm_command); \
17 if (result != TPM_SUCCESS) { \
18 printf("TEST FAILED: line %d: " #tpm_command ": 0x%x\n", \
28 #define INDEX_INITIALISED 0xda80
29 #define PHYS_PRESENCE 4
32 static uint32_t TlclStartupIfNeeded(void)
34 uint32_t result = tpm_startup(TPM_ST_CLEAR);
36 return result == TPM_INVALID_POSTINIT ? TPM_SUCCESS : result;
39 static int test_timer(void)
41 printf("get_timer(0) = %lu\n", get_timer(0));
45 static uint32_t tpm_get_flags(uint8_t *disable, uint8_t *deactivated,
48 struct tpm_permanent_flags pflags;
51 result = tpm_get_permanent_flags(&pflags);
55 *disable = pflags.disable;
57 *deactivated = pflags.deactivated;
59 *nvlocked = pflags.nv_locked;
60 debug("TPM: Got flags disable=%d, deactivated=%d, nvlocked=%d\n",
61 pflags.disable, pflags.deactivated, pflags.nv_locked);
66 static uint32_t tpm_set_global_lock(void)
70 debug("TPM: Set global lock\n");
71 return tpm_nv_write_value(INDEX0, (uint8_t *)&x, 0);
74 static uint32_t tpm_nv_write_value_lock(uint32_t index)
76 debug("TPM: Write lock 0x%x\n", index);
78 return tpm_nv_write_value(index, NULL, 0);
81 static uint32_t tpm_nv_set_locked(void)
83 debug("TPM: Set NV locked\n");
85 return tpm_nv_define_space(TPM_NV_INDEX_LOCK, 0, 0);
88 static int tpm_is_owned(void)
90 uint8_t response[TPM_PUBEK_SIZE];
93 result = tpm_read_pubek(response, sizeof(response));
95 return result != TPM_SUCCESS;
98 static int test_early_extend(void)
100 uint8_t value_in[20];
101 uint8_t value_out[20];
103 printf("Testing earlyextend ...");
105 TPM_CHECK(tpm_startup(TPM_ST_CLEAR));
106 TPM_CHECK(tpm_continue_self_test());
107 TPM_CHECK(tpm_extend(1, value_in, value_out));
112 static int test_early_nvram(void)
116 printf("Testing earlynvram ...");
118 TPM_CHECK(tpm_startup(TPM_ST_CLEAR));
119 TPM_CHECK(tpm_continue_self_test());
120 TPM_CHECK(tpm_tsc_physical_presence(PRESENCE));
121 TPM_CHECK(tpm_nv_read_value(INDEX0, (uint8_t *)&x, sizeof(x)));
126 static int test_early_nvram2(void)
130 printf("Testing earlynvram2 ...");
132 TPM_CHECK(tpm_startup(TPM_ST_CLEAR));
133 TPM_CHECK(tpm_continue_self_test());
134 TPM_CHECK(tpm_tsc_physical_presence(PRESENCE));
135 TPM_CHECK(tpm_nv_write_value(INDEX0, (uint8_t *)&x, sizeof(x)));
140 static int test_enable(void)
142 uint8_t disable = 0, deactivated = 0;
144 printf("Testing enable ...\n");
146 TPM_CHECK(TlclStartupIfNeeded());
147 TPM_CHECK(tpm_self_test_full());
148 TPM_CHECK(tpm_tsc_physical_presence(PRESENCE));
149 TPM_CHECK(tpm_get_flags(&disable, &deactivated, NULL));
150 printf("\tdisable is %d, deactivated is %d\n", disable, deactivated);
151 TPM_CHECK(tpm_physical_enable());
152 TPM_CHECK(tpm_physical_set_deactivated(0));
153 TPM_CHECK(tpm_get_flags(&disable, &deactivated, NULL));
154 printf("\tdisable is %d, deactivated is %d\n", disable, deactivated);
155 if (disable == 1 || deactivated == 1)
156 printf("\tfailed to enable or activate\n");
161 #define reboot() do { \
162 printf("\trebooting...\n"); \
166 static int test_fast_enable(void)
168 uint8_t disable = 0, deactivated = 0;
171 printf("Testing fastenable ...\n");
173 TPM_CHECK(TlclStartupIfNeeded());
174 TPM_CHECK(tpm_self_test_full());
175 TPM_CHECK(tpm_tsc_physical_presence(PRESENCE));
176 TPM_CHECK(tpm_get_flags(&disable, &deactivated, NULL));
177 printf("\tdisable is %d, deactivated is %d\n", disable, deactivated);
178 for (i = 0; i < 2; i++) {
179 TPM_CHECK(tpm_force_clear());
180 TPM_CHECK(tpm_get_flags(&disable, &deactivated, NULL));
181 printf("\tdisable is %d, deactivated is %d\n", disable,
183 assert(disable == 1 && deactivated == 1);
184 TPM_CHECK(tpm_physical_enable());
185 TPM_CHECK(tpm_physical_set_deactivated(0));
186 TPM_CHECK(tpm_get_flags(&disable, &deactivated, NULL));
187 printf("\tdisable is %d, deactivated is %d\n", disable,
189 assert(disable == 0 && deactivated == 0);
195 static int test_global_lock(void)
201 printf("Testing globallock ...\n");
203 TPM_CHECK(TlclStartupIfNeeded());
204 TPM_CHECK(tpm_self_test_full());
205 TPM_CHECK(tpm_tsc_physical_presence(PRESENCE));
206 TPM_CHECK(tpm_nv_read_value(INDEX0, (uint8_t *)&x, sizeof(x)));
207 TPM_CHECK(tpm_nv_write_value(INDEX0, (uint8_t *)&zero,
209 TPM_CHECK(tpm_nv_read_value(INDEX1, (uint8_t *)&x, sizeof(x)));
210 TPM_CHECK(tpm_nv_write_value(INDEX1, (uint8_t *)&zero,
212 TPM_CHECK(tpm_set_global_lock());
213 /* Verifies that write to index0 fails */
215 result = tpm_nv_write_value(INDEX0, (uint8_t *)&x, sizeof(x));
216 assert(result == TPM_AREA_LOCKED);
217 TPM_CHECK(tpm_nv_read_value(INDEX0, (uint8_t *)&x, sizeof(x)));
219 /* Verifies that write to index1 is still possible */
221 TPM_CHECK(tpm_nv_write_value(INDEX1, (uint8_t *)&x, sizeof(x)));
222 TPM_CHECK(tpm_nv_read_value(INDEX1, (uint8_t *)&x, sizeof(x)));
225 tpm_tsc_physical_presence(PHYS_PRESENCE);
226 /* Verifies that write to index1 fails */
228 result = tpm_nv_write_value(INDEX1, (uint8_t *)&x, sizeof(x));
229 assert(result == TPM_BAD_PRESENCE);
230 TPM_CHECK(tpm_nv_read_value(INDEX1, (uint8_t *)&x, sizeof(x)));
236 static int test_lock(void)
238 printf("Testing lock ...\n");
240 tpm_startup(TPM_ST_CLEAR);
241 tpm_self_test_full();
242 tpm_tsc_physical_presence(PRESENCE);
243 tpm_nv_write_value_lock(INDEX0);
244 printf("\tLocked 0x%x\n", INDEX0);
249 static void initialise_spaces(void)
252 uint32_t perm = TPM_NV_PER_WRITE_STCLEAR | TPM_NV_PER_PPWRITE;
254 printf("\tInitialising spaces\n");
255 tpm_nv_set_locked(); /* useful only the first time */
256 tpm_nv_define_space(INDEX0, perm, 4);
257 tpm_nv_write_value(INDEX0, (uint8_t *)&zero, 4);
258 tpm_nv_define_space(INDEX1, perm, 4);
259 tpm_nv_write_value(INDEX1, (uint8_t *)&zero, 4);
260 tpm_nv_define_space(INDEX2, perm, 4);
261 tpm_nv_write_value(INDEX2, (uint8_t *)&zero, 4);
262 tpm_nv_define_space(INDEX3, perm, 4);
263 tpm_nv_write_value(INDEX3, (uint8_t *)&zero, 4);
264 perm = TPM_NV_PER_READ_STCLEAR | TPM_NV_PER_WRITE_STCLEAR |
266 tpm_nv_define_space(INDEX_INITIALISED, perm, 1);
269 static int test_readonly(void)
272 uint32_t index_0, index_1, index_2, index_3;
273 int read0, read1, read2, read3;
275 printf("Testing readonly ...\n");
277 tpm_startup(TPM_ST_CLEAR);
278 tpm_self_test_full();
279 tpm_tsc_physical_presence(PRESENCE);
281 * Checks if initialisation has completed by trying to read-lock a
282 * space that's created at the end of initialisation
284 if (tpm_nv_read_value(INDEX_INITIALISED, &c, 0) == TPM_BADINDEX) {
285 /* The initialisation did not complete */
289 /* Checks if spaces are OK or messed up */
290 read0 = tpm_nv_read_value(INDEX0, (uint8_t *)&index_0, sizeof(index_0));
291 read1 = tpm_nv_read_value(INDEX1, (uint8_t *)&index_1, sizeof(index_1));
292 read2 = tpm_nv_read_value(INDEX2, (uint8_t *)&index_2, sizeof(index_2));
293 read3 = tpm_nv_read_value(INDEX3, (uint8_t *)&index_3, sizeof(index_3));
294 if (read0 || read1 || read2 || read3) {
295 printf("Invalid contents\n");
300 * Writes space, and locks it. Then attempts to write again.
301 * I really wish I could use the imperative.
304 if (tpm_nv_write_value(INDEX0, (uint8_t *)&index_0, sizeof(index_0) !=
306 error("\tcould not write index 0\n");
308 tpm_nv_write_value_lock(INDEX0);
309 if (tpm_nv_write_value(INDEX0, (uint8_t *)&index_0, sizeof(index_0)) ==
311 error("\tindex 0 is not locked\n");
317 static int test_redefine_unowned(void)
323 printf("Testing redefine_unowned ...");
325 TPM_CHECK(TlclStartupIfNeeded());
326 TPM_CHECK(tpm_self_test_full());
327 TPM_CHECK(tpm_tsc_physical_presence(PRESENCE));
328 assert(!tpm_is_owned());
330 /* Ensures spaces exist. */
331 TPM_CHECK(tpm_nv_read_value(INDEX0, (uint8_t *)&x, sizeof(x)));
332 TPM_CHECK(tpm_nv_read_value(INDEX1, (uint8_t *)&x, sizeof(x)));
334 /* Redefines spaces a couple of times. */
335 perm = TPM_NV_PER_PPWRITE | TPM_NV_PER_GLOBALLOCK;
336 TPM_CHECK(tpm_nv_define_space(INDEX0, perm, 2 * sizeof(uint32_t)));
337 TPM_CHECK(tpm_nv_define_space(INDEX0, perm, sizeof(uint32_t)));
338 perm = TPM_NV_PER_PPWRITE;
339 TPM_CHECK(tpm_nv_define_space(INDEX1, perm, 2 * sizeof(uint32_t)));
340 TPM_CHECK(tpm_nv_define_space(INDEX1, perm, sizeof(uint32_t)));
342 /* Sets the global lock */
343 tpm_set_global_lock();
345 /* Verifies that index0 cannot be redefined */
346 result = tpm_nv_define_space(INDEX0, perm, sizeof(uint32_t));
347 assert(result == TPM_AREA_LOCKED);
349 /* Checks that index1 can */
350 TPM_CHECK(tpm_nv_define_space(INDEX1, perm, 2 * sizeof(uint32_t)));
351 TPM_CHECK(tpm_nv_define_space(INDEX1, perm, sizeof(uint32_t)));
354 tpm_tsc_physical_presence(PHYS_PRESENCE);
356 /* Verifies that neither index0 nor index1 can be redefined */
357 result = tpm_nv_define_space(INDEX0, perm, sizeof(uint32_t));
358 assert(result == TPM_BAD_PRESENCE);
359 result = tpm_nv_define_space(INDEX1, perm, sizeof(uint32_t));
360 assert(result == TPM_BAD_PRESENCE);
366 #define PERMPPGL (TPM_NV_PER_PPWRITE | TPM_NV_PER_GLOBALLOCK)
367 #define PERMPP TPM_NV_PER_PPWRITE
369 static int test_space_perm(void)
373 printf("Testing spaceperm ...");
375 TPM_CHECK(TlclStartupIfNeeded());
376 TPM_CHECK(tpm_continue_self_test());
377 TPM_CHECK(tpm_tsc_physical_presence(PRESENCE));
378 TPM_CHECK(tpm_get_permissions(INDEX0, &perm));
379 assert((perm & PERMPPGL) == PERMPPGL);
380 TPM_CHECK(tpm_get_permissions(INDEX1, &perm));
381 assert((perm & PERMPP) == PERMPP);
386 static int test_startup(void)
389 printf("Testing startup ...\n");
392 result = tpm_startup(TPM_ST_CLEAR);
393 if (result != 0 && result != TPM_INVALID_POSTINIT)
394 printf("\ttpm startup failed with 0x%x\n", result);
395 result = tpm_get_flags(NULL, NULL, NULL);
397 printf("\ttpm getflags failed with 0x%x\n", result);
398 printf("\texecuting SelfTestFull\n");
399 tpm_self_test_full();
400 result = tpm_get_flags(NULL, NULL, NULL);
402 printf("\ttpm getflags failed with 0x%x\n", result);
408 * Runs [op] and ensures it returns success and doesn't run longer than
409 * [time_limit] in milliseconds.
411 #define TTPM_CHECK(op, time_limit) do { \
415 start = get_timer(0); \
417 if (__result != TPM_SUCCESS) { \
418 printf("\t" #op ": error 0x%x\n", __result); \
421 time = get_timer(start); \
422 printf("\t" #op ": %lu ms\n", time); \
423 if (time > (ulong)time_limit) { \
424 printf("\t" #op " exceeded " #time_limit " ms\n"); \
429 static int test_timing(void)
432 uint8_t in[20], out[20];
434 printf("Testing timing ...");
436 TTPM_CHECK(TlclStartupIfNeeded(), 50);
437 TTPM_CHECK(tpm_continue_self_test(), 100);
438 TTPM_CHECK(tpm_self_test_full(), 1000);
439 TTPM_CHECK(tpm_tsc_physical_presence(PRESENCE), 100);
440 TTPM_CHECK(tpm_nv_write_value(INDEX0, (uint8_t *)&x, sizeof(x)), 100);
441 TTPM_CHECK(tpm_nv_read_value(INDEX0, (uint8_t *)&x, sizeof(x)), 100);
442 TTPM_CHECK(tpm_extend(0, in, out), 200);
443 TTPM_CHECK(tpm_set_global_lock(), 50);
444 TTPM_CHECK(tpm_tsc_physical_presence(PHYS_PRESENCE), 100);
449 #define TPM_MAX_NV_WRITES_NOOWNER 64
451 static int test_write_limit(void)
453 printf("Testing writelimit ...\n");
458 TPM_CHECK(TlclStartupIfNeeded());
459 TPM_CHECK(tpm_self_test_full());
460 TPM_CHECK(tpm_tsc_physical_presence(PRESENCE));
461 TPM_CHECK(tpm_force_clear());
462 TPM_CHECK(tpm_physical_enable());
463 TPM_CHECK(tpm_physical_set_deactivated(0));
465 for (i = 0; i < TPM_MAX_NV_WRITES_NOOWNER + 2; i++) {
466 printf("\twriting %d\n", i);
467 result = tpm_nv_write_value(INDEX0, (uint8_t *)&i, sizeof(i));
471 case TPM_MAXNVWRITES:
472 assert(i >= TPM_MAX_NV_WRITES_NOOWNER);
474 error("\tunexpected error code %d (0x%x)\n",
479 /* Reset write count */
480 TPM_CHECK(tpm_force_clear());
481 TPM_CHECK(tpm_physical_enable());
482 TPM_CHECK(tpm_physical_set_deactivated(0));
484 /* Try writing again. */
485 TPM_CHECK(tpm_nv_write_value(INDEX0, (uint8_t *)&i, sizeof(i)));
490 #define VOIDTEST(XFUNC) \
491 int do_test_##XFUNC(cmd_tbl_t *cmd_tbl, int flag, int argc, \
492 char * const argv[]) \
494 return test_##XFUNC(); \
497 #define VOIDENT(XNAME) \
498 U_BOOT_CMD_MKENT(XNAME, 0, 1, do_test_##XNAME, "", ""),
500 VOIDTEST(early_extend)
501 VOIDTEST(early_nvram)
502 VOIDTEST(early_nvram2)
504 VOIDTEST(fast_enable)
505 VOIDTEST(global_lock)
508 VOIDTEST(redefine_unowned)
512 VOIDTEST(write_limit)
515 static cmd_tbl_t cmd_cros_tpm_sub[] = {
516 VOIDENT(early_extend)
518 VOIDENT(early_nvram2)
524 VOIDENT(redefine_unowned)
532 static int do_tpmtest(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
536 printf("argc = %d, argv = ", argc);
540 for (i = 0; i < argc; i++)
541 printf(" %s", argv[i]);
542 printf("\n------\n");
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);
551 U_BOOT_CMD(tpmtest, 2, 1, do_tpmtest, "TPM tests",
560 "\tredefine_unowned\n"