1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (c) 2015 Google, Inc
8 #include <environment.h>
11 /* Prints error and returns on failure */
12 #define TPM_CHECK(tpm_command) do { \
15 result = (tpm_command); \
16 if (result != TPM_SUCCESS) { \
17 printf("TEST FAILED: line %d: " #tpm_command ": 0x%x\n", \
27 #define INDEX_INITIALISED 0xda80
28 #define PHYS_PRESENCE 4
31 static uint32_t TlclStartupIfNeeded(void)
33 uint32_t result
= tpm_startup(TPM_ST_CLEAR
);
35 return result
== TPM_INVALID_POSTINIT
? TPM_SUCCESS
: result
;
38 static int test_timer(void)
40 printf("get_timer(0) = %lu\n", get_timer(0));
44 static uint32_t tpm_get_flags(uint8_t *disable
, uint8_t *deactivated
,
47 struct tpm_permanent_flags pflags
;
50 result
= tpm_get_permanent_flags(&pflags
);
54 *disable
= pflags
.disable
;
56 *deactivated
= pflags
.deactivated
;
58 *nvlocked
= pflags
.nv_locked
;
59 debug("TPM: Got flags disable=%d, deactivated=%d, nvlocked=%d\n",
60 pflags
.disable
, pflags
.deactivated
, pflags
.nv_locked
);
65 static uint32_t tpm_set_global_lock(void)
69 debug("TPM: Set global lock\n");
70 return tpm_nv_write_value(INDEX0
, (uint8_t *)&x
, 0);
73 static uint32_t tpm_nv_write_value_lock(uint32_t index
)
75 debug("TPM: Write lock 0x%x\n", index
);
77 return tpm_nv_write_value(index
, NULL
, 0);
80 static uint32_t tpm_nv_set_locked(void)
82 debug("TPM: Set NV locked\n");
84 return tpm_nv_define_space(TPM_NV_INDEX_LOCK
, 0, 0);
87 static int tpm_is_owned(void)
89 uint8_t response
[TPM_PUBEK_SIZE
];
92 result
= tpm_read_pubek(response
, sizeof(response
));
94 return result
!= TPM_SUCCESS
;
97 static int test_early_extend(void)
100 uint8_t value_out
[20];
102 printf("Testing earlyextend ...");
104 TPM_CHECK(tpm_startup(TPM_ST_CLEAR
));
105 TPM_CHECK(tpm_continue_self_test());
106 TPM_CHECK(tpm_extend(1, value_in
, value_out
));
111 static int test_early_nvram(void)
115 printf("Testing earlynvram ...");
117 TPM_CHECK(tpm_startup(TPM_ST_CLEAR
));
118 TPM_CHECK(tpm_continue_self_test());
119 TPM_CHECK(tpm_tsc_physical_presence(PRESENCE
));
120 TPM_CHECK(tpm_nv_read_value(INDEX0
, (uint8_t *)&x
, sizeof(x
)));
125 static int test_early_nvram2(void)
129 printf("Testing earlynvram2 ...");
131 TPM_CHECK(tpm_startup(TPM_ST_CLEAR
));
132 TPM_CHECK(tpm_continue_self_test());
133 TPM_CHECK(tpm_tsc_physical_presence(PRESENCE
));
134 TPM_CHECK(tpm_nv_write_value(INDEX0
, (uint8_t *)&x
, sizeof(x
)));
139 static int test_enable(void)
141 uint8_t disable
= 0, deactivated
= 0;
143 printf("Testing enable ...\n");
145 TPM_CHECK(TlclStartupIfNeeded());
146 TPM_CHECK(tpm_self_test_full());
147 TPM_CHECK(tpm_tsc_physical_presence(PRESENCE
));
148 TPM_CHECK(tpm_get_flags(&disable
, &deactivated
, NULL
));
149 printf("\tdisable is %d, deactivated is %d\n", disable
, deactivated
);
150 TPM_CHECK(tpm_physical_enable());
151 TPM_CHECK(tpm_physical_set_deactivated(0));
152 TPM_CHECK(tpm_get_flags(&disable
, &deactivated
, NULL
));
153 printf("\tdisable is %d, deactivated is %d\n", disable
, deactivated
);
154 if (disable
== 1 || deactivated
== 1)
155 printf("\tfailed to enable or activate\n");
160 #define reboot() do { \
161 printf("\trebooting...\n"); \
165 static int test_fast_enable(void)
167 uint8_t disable
= 0, deactivated
= 0;
170 printf("Testing fastenable ...\n");
172 TPM_CHECK(TlclStartupIfNeeded());
173 TPM_CHECK(tpm_self_test_full());
174 TPM_CHECK(tpm_tsc_physical_presence(PRESENCE
));
175 TPM_CHECK(tpm_get_flags(&disable
, &deactivated
, NULL
));
176 printf("\tdisable is %d, deactivated is %d\n", disable
, deactivated
);
177 for (i
= 0; i
< 2; i
++) {
178 TPM_CHECK(tpm_force_clear());
179 TPM_CHECK(tpm_get_flags(&disable
, &deactivated
, NULL
));
180 printf("\tdisable is %d, deactivated is %d\n", disable
,
182 assert(disable
== 1 && deactivated
== 1);
183 TPM_CHECK(tpm_physical_enable());
184 TPM_CHECK(tpm_physical_set_deactivated(0));
185 TPM_CHECK(tpm_get_flags(&disable
, &deactivated
, NULL
));
186 printf("\tdisable is %d, deactivated is %d\n", disable
,
188 assert(disable
== 0 && deactivated
== 0);
194 static int test_global_lock(void)
200 printf("Testing globallock ...\n");
202 TPM_CHECK(TlclStartupIfNeeded());
203 TPM_CHECK(tpm_self_test_full());
204 TPM_CHECK(tpm_tsc_physical_presence(PRESENCE
));
205 TPM_CHECK(tpm_nv_read_value(INDEX0
, (uint8_t *)&x
, sizeof(x
)));
206 TPM_CHECK(tpm_nv_write_value(INDEX0
, (uint8_t *)&zero
,
208 TPM_CHECK(tpm_nv_read_value(INDEX1
, (uint8_t *)&x
, sizeof(x
)));
209 TPM_CHECK(tpm_nv_write_value(INDEX1
, (uint8_t *)&zero
,
211 TPM_CHECK(tpm_set_global_lock());
212 /* Verifies that write to index0 fails */
214 result
= tpm_nv_write_value(INDEX0
, (uint8_t *)&x
, sizeof(x
));
215 assert(result
== TPM_AREA_LOCKED
);
216 TPM_CHECK(tpm_nv_read_value(INDEX0
, (uint8_t *)&x
, sizeof(x
)));
218 /* Verifies that write to index1 is still possible */
220 TPM_CHECK(tpm_nv_write_value(INDEX1
, (uint8_t *)&x
, sizeof(x
)));
221 TPM_CHECK(tpm_nv_read_value(INDEX1
, (uint8_t *)&x
, sizeof(x
)));
224 tpm_tsc_physical_presence(PHYS_PRESENCE
);
225 /* Verifies that write to index1 fails */
227 result
= tpm_nv_write_value(INDEX1
, (uint8_t *)&x
, sizeof(x
));
228 assert(result
== TPM_BAD_PRESENCE
);
229 TPM_CHECK(tpm_nv_read_value(INDEX1
, (uint8_t *)&x
, sizeof(x
)));
235 static int test_lock(void)
237 printf("Testing lock ...\n");
239 tpm_startup(TPM_ST_CLEAR
);
240 tpm_self_test_full();
241 tpm_tsc_physical_presence(PRESENCE
);
242 tpm_nv_write_value_lock(INDEX0
);
243 printf("\tLocked 0x%x\n", INDEX0
);
248 static void initialise_spaces(void)
251 uint32_t perm
= TPM_NV_PER_WRITE_STCLEAR
| TPM_NV_PER_PPWRITE
;
253 printf("\tInitialising spaces\n");
254 tpm_nv_set_locked(); /* useful only the first time */
255 tpm_nv_define_space(INDEX0
, perm
, 4);
256 tpm_nv_write_value(INDEX0
, (uint8_t *)&zero
, 4);
257 tpm_nv_define_space(INDEX1
, perm
, 4);
258 tpm_nv_write_value(INDEX1
, (uint8_t *)&zero
, 4);
259 tpm_nv_define_space(INDEX2
, perm
, 4);
260 tpm_nv_write_value(INDEX2
, (uint8_t *)&zero
, 4);
261 tpm_nv_define_space(INDEX3
, perm
, 4);
262 tpm_nv_write_value(INDEX3
, (uint8_t *)&zero
, 4);
263 perm
= TPM_NV_PER_READ_STCLEAR
| TPM_NV_PER_WRITE_STCLEAR
|
265 tpm_nv_define_space(INDEX_INITIALISED
, perm
, 1);
268 static int test_readonly(void)
271 uint32_t index_0
, index_1
, index_2
, index_3
;
272 int read0
, read1
, read2
, read3
;
274 printf("Testing readonly ...\n");
276 tpm_startup(TPM_ST_CLEAR
);
277 tpm_self_test_full();
278 tpm_tsc_physical_presence(PRESENCE
);
280 * Checks if initialisation has completed by trying to read-lock a
281 * space that's created at the end of initialisation
283 if (tpm_nv_read_value(INDEX_INITIALISED
, &c
, 0) == TPM_BADINDEX
) {
284 /* The initialisation did not complete */
288 /* Checks if spaces are OK or messed up */
289 read0
= tpm_nv_read_value(INDEX0
, (uint8_t *)&index_0
, sizeof(index_0
));
290 read1
= tpm_nv_read_value(INDEX1
, (uint8_t *)&index_1
, sizeof(index_1
));
291 read2
= tpm_nv_read_value(INDEX2
, (uint8_t *)&index_2
, sizeof(index_2
));
292 read3
= tpm_nv_read_value(INDEX3
, (uint8_t *)&index_3
, sizeof(index_3
));
293 if (read0
|| read1
|| read2
|| read3
) {
294 printf("Invalid contents\n");
299 * Writes space, and locks it. Then attempts to write again.
300 * I really wish I could use the imperative.
303 if (tpm_nv_write_value(INDEX0
, (uint8_t *)&index_0
, sizeof(index_0
) !=
305 pr_err("\tcould not write index 0\n");
307 tpm_nv_write_value_lock(INDEX0
);
308 if (tpm_nv_write_value(INDEX0
, (uint8_t *)&index_0
, sizeof(index_0
)) ==
310 pr_err("\tindex 0 is not locked\n");
316 static int test_redefine_unowned(void)
322 printf("Testing redefine_unowned ...");
324 TPM_CHECK(TlclStartupIfNeeded());
325 TPM_CHECK(tpm_self_test_full());
326 TPM_CHECK(tpm_tsc_physical_presence(PRESENCE
));
327 assert(!tpm_is_owned());
329 /* Ensures spaces exist. */
330 TPM_CHECK(tpm_nv_read_value(INDEX0
, (uint8_t *)&x
, sizeof(x
)));
331 TPM_CHECK(tpm_nv_read_value(INDEX1
, (uint8_t *)&x
, sizeof(x
)));
333 /* Redefines spaces a couple of times. */
334 perm
= TPM_NV_PER_PPWRITE
| TPM_NV_PER_GLOBALLOCK
;
335 TPM_CHECK(tpm_nv_define_space(INDEX0
, perm
, 2 * sizeof(uint32_t)));
336 TPM_CHECK(tpm_nv_define_space(INDEX0
, perm
, sizeof(uint32_t)));
337 perm
= TPM_NV_PER_PPWRITE
;
338 TPM_CHECK(tpm_nv_define_space(INDEX1
, perm
, 2 * sizeof(uint32_t)));
339 TPM_CHECK(tpm_nv_define_space(INDEX1
, perm
, sizeof(uint32_t)));
341 /* Sets the global lock */
342 tpm_set_global_lock();
344 /* Verifies that index0 cannot be redefined */
345 result
= tpm_nv_define_space(INDEX0
, perm
, sizeof(uint32_t));
346 assert(result
== TPM_AREA_LOCKED
);
348 /* Checks that index1 can */
349 TPM_CHECK(tpm_nv_define_space(INDEX1
, perm
, 2 * sizeof(uint32_t)));
350 TPM_CHECK(tpm_nv_define_space(INDEX1
, perm
, sizeof(uint32_t)));
353 tpm_tsc_physical_presence(PHYS_PRESENCE
);
355 /* Verifies that neither index0 nor index1 can be redefined */
356 result
= tpm_nv_define_space(INDEX0
, perm
, sizeof(uint32_t));
357 assert(result
== TPM_BAD_PRESENCE
);
358 result
= tpm_nv_define_space(INDEX1
, perm
, sizeof(uint32_t));
359 assert(result
== TPM_BAD_PRESENCE
);
365 #define PERMPPGL (TPM_NV_PER_PPWRITE | TPM_NV_PER_GLOBALLOCK)
366 #define PERMPP TPM_NV_PER_PPWRITE
368 static int test_space_perm(void)
372 printf("Testing spaceperm ...");
374 TPM_CHECK(TlclStartupIfNeeded());
375 TPM_CHECK(tpm_continue_self_test());
376 TPM_CHECK(tpm_tsc_physical_presence(PRESENCE
));
377 TPM_CHECK(tpm_get_permissions(INDEX0
, &perm
));
378 assert((perm
& PERMPPGL
) == PERMPPGL
);
379 TPM_CHECK(tpm_get_permissions(INDEX1
, &perm
));
380 assert((perm
& PERMPP
) == PERMPP
);
385 static int test_startup(void)
388 printf("Testing startup ...\n");
391 result
= tpm_startup(TPM_ST_CLEAR
);
392 if (result
!= 0 && result
!= TPM_INVALID_POSTINIT
)
393 printf("\ttpm startup failed with 0x%x\n", result
);
394 result
= tpm_get_flags(NULL
, NULL
, NULL
);
396 printf("\ttpm getflags failed with 0x%x\n", result
);
397 printf("\texecuting SelfTestFull\n");
398 tpm_self_test_full();
399 result
= tpm_get_flags(NULL
, NULL
, NULL
);
401 printf("\ttpm getflags failed with 0x%x\n", result
);
407 * Runs [op] and ensures it returns success and doesn't run longer than
408 * [time_limit] in milliseconds.
410 #define TTPM_CHECK(op, time_limit) do { \
414 start = get_timer(0); \
416 if (__result != TPM_SUCCESS) { \
417 printf("\t" #op ": error 0x%x\n", __result); \
420 time = get_timer(start); \
421 printf("\t" #op ": %lu ms\n", time); \
422 if (time > (ulong)time_limit) { \
423 printf("\t" #op " exceeded " #time_limit " ms\n"); \
428 static int test_timing(void)
431 uint8_t in
[20], out
[20];
433 printf("Testing timing ...");
435 TTPM_CHECK(TlclStartupIfNeeded(), 50);
436 TTPM_CHECK(tpm_continue_self_test(), 100);
437 TTPM_CHECK(tpm_self_test_full(), 1000);
438 TTPM_CHECK(tpm_tsc_physical_presence(PRESENCE
), 100);
439 TTPM_CHECK(tpm_nv_write_value(INDEX0
, (uint8_t *)&x
, sizeof(x
)), 100);
440 TTPM_CHECK(tpm_nv_read_value(INDEX0
, (uint8_t *)&x
, sizeof(x
)), 100);
441 TTPM_CHECK(tpm_extend(0, in
, out
), 200);
442 TTPM_CHECK(tpm_set_global_lock(), 50);
443 TTPM_CHECK(tpm_tsc_physical_presence(PHYS_PRESENCE
), 100);
448 #define TPM_MAX_NV_WRITES_NOOWNER 64
450 static int test_write_limit(void)
452 printf("Testing writelimit ...\n");
457 TPM_CHECK(TlclStartupIfNeeded());
458 TPM_CHECK(tpm_self_test_full());
459 TPM_CHECK(tpm_tsc_physical_presence(PRESENCE
));
460 TPM_CHECK(tpm_force_clear());
461 TPM_CHECK(tpm_physical_enable());
462 TPM_CHECK(tpm_physical_set_deactivated(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(INDEX0
, (uint8_t *)&i
, sizeof(i
));
470 case TPM_MAXNVWRITES
:
471 assert(i
>= TPM_MAX_NV_WRITES_NOOWNER
);
473 pr_err("\tunexpected error code %d (0x%x)\n",
478 /* Reset write count */
479 TPM_CHECK(tpm_force_clear());
480 TPM_CHECK(tpm_physical_enable());
481 TPM_CHECK(tpm_physical_set_deactivated(0));
483 /* Try writing again. */
484 TPM_CHECK(tpm_nv_write_value(INDEX0
, (uint8_t *)&i
, sizeof(i
)));
489 #define VOIDTEST(XFUNC) \
490 int do_test_##XFUNC(cmd_tbl_t *cmd_tbl, int flag, int argc, \
491 char * const argv[]) \
493 return test_##XFUNC(); \
496 #define VOIDENT(XNAME) \
497 U_BOOT_CMD_MKENT(XNAME, 0, 1, do_test_##XNAME, "", ""),
499 VOIDTEST(early_extend
)
500 VOIDTEST(early_nvram
)
501 VOIDTEST(early_nvram2
)
503 VOIDTEST(fast_enable
)
504 VOIDTEST(global_lock
)
507 VOIDTEST(redefine_unowned
)
511 VOIDTEST(write_limit
)
514 static cmd_tbl_t cmd_cros_tpm_sub
[] = {
515 VOIDENT(early_extend
)
517 VOIDENT(early_nvram2
)
523 VOIDENT(redefine_unowned
)
531 static int do_tpmtest(cmd_tbl_t
*cmdtp
, int flag
, int argc
, char * const argv
[])
536 printf("argc = %d, argv = ", argc
);
538 for (i
= 0; i
< argc
; i
++)
539 printf(" %s", argv
[i
]);
541 printf("\n------\n");
545 c
= find_cmd_tbl(argv
[0], cmd_cros_tpm_sub
,
546 ARRAY_SIZE(cmd_cros_tpm_sub
));
547 return c
? c
->cmd(cmdtp
, flag
, argc
, argv
) : cmd_usage(cmdtp
);
550 U_BOOT_CMD(tpmtest
, 2, 1, do_tpmtest
, "TPM tests",
559 "\tredefine_unowned\n"