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_nv_write_value_lock(uint32_t index
)
67 debug("TPM: Write lock 0x%x\n", index
);
69 return tpm_nv_write_value(index
, NULL
, 0);
72 static int tpm_is_owned(void)
74 uint8_t response
[TPM_PUBEK_SIZE
];
77 result
= tpm_read_pubek(response
, sizeof(response
));
79 return result
!= TPM_SUCCESS
;
82 static int test_early_extend(void)
85 uint8_t value_out
[20];
87 printf("Testing earlyextend ...");
89 TPM_CHECK(tpm_startup(TPM_ST_CLEAR
));
90 TPM_CHECK(tpm_continue_self_test());
91 TPM_CHECK(tpm_extend(1, value_in
, value_out
));
96 static int test_early_nvram(void)
100 printf("Testing earlynvram ...");
102 TPM_CHECK(tpm_startup(TPM_ST_CLEAR
));
103 TPM_CHECK(tpm_continue_self_test());
104 TPM_CHECK(tpm_tsc_physical_presence(PRESENCE
));
105 TPM_CHECK(tpm_nv_read_value(INDEX0
, (uint8_t *)&x
, sizeof(x
)));
110 static int test_early_nvram2(void)
114 printf("Testing earlynvram2 ...");
116 TPM_CHECK(tpm_startup(TPM_ST_CLEAR
));
117 TPM_CHECK(tpm_continue_self_test());
118 TPM_CHECK(tpm_tsc_physical_presence(PRESENCE
));
119 TPM_CHECK(tpm_nv_write_value(INDEX0
, (uint8_t *)&x
, sizeof(x
)));
124 static int test_enable(void)
126 uint8_t disable
= 0, deactivated
= 0;
128 printf("Testing enable ...\n");
130 TPM_CHECK(TlclStartupIfNeeded());
131 TPM_CHECK(tpm_self_test_full());
132 TPM_CHECK(tpm_tsc_physical_presence(PRESENCE
));
133 TPM_CHECK(tpm_get_flags(&disable
, &deactivated
, NULL
));
134 printf("\tdisable is %d, deactivated is %d\n", disable
, deactivated
);
135 TPM_CHECK(tpm_physical_enable());
136 TPM_CHECK(tpm_physical_set_deactivated(0));
137 TPM_CHECK(tpm_get_flags(&disable
, &deactivated
, NULL
));
138 printf("\tdisable is %d, deactivated is %d\n", disable
, deactivated
);
139 if (disable
== 1 || deactivated
== 1)
140 printf("\tfailed to enable or activate\n");
145 #define reboot() do { \
146 printf("\trebooting...\n"); \
150 static int test_fast_enable(void)
152 uint8_t disable
= 0, deactivated
= 0;
155 printf("Testing fastenable ...\n");
157 TPM_CHECK(TlclStartupIfNeeded());
158 TPM_CHECK(tpm_self_test_full());
159 TPM_CHECK(tpm_tsc_physical_presence(PRESENCE
));
160 TPM_CHECK(tpm_get_flags(&disable
, &deactivated
, NULL
));
161 printf("\tdisable is %d, deactivated is %d\n", disable
, deactivated
);
162 for (i
= 0; i
< 2; i
++) {
163 TPM_CHECK(tpm_force_clear());
164 TPM_CHECK(tpm_get_flags(&disable
, &deactivated
, NULL
));
165 printf("\tdisable is %d, deactivated is %d\n", disable
,
167 assert(disable
== 1 && deactivated
== 1);
168 TPM_CHECK(tpm_physical_enable());
169 TPM_CHECK(tpm_physical_set_deactivated(0));
170 TPM_CHECK(tpm_get_flags(&disable
, &deactivated
, NULL
));
171 printf("\tdisable is %d, deactivated is %d\n", disable
,
173 assert(disable
== 0 && deactivated
== 0);
179 static int test_global_lock(void)
185 printf("Testing globallock ...\n");
187 TPM_CHECK(TlclStartupIfNeeded());
188 TPM_CHECK(tpm_self_test_full());
189 TPM_CHECK(tpm_tsc_physical_presence(PRESENCE
));
190 TPM_CHECK(tpm_nv_read_value(INDEX0
, (uint8_t *)&x
, sizeof(x
)));
191 TPM_CHECK(tpm_nv_write_value(INDEX0
, (uint8_t *)&zero
,
193 TPM_CHECK(tpm_nv_read_value(INDEX1
, (uint8_t *)&x
, sizeof(x
)));
194 TPM_CHECK(tpm_nv_write_value(INDEX1
, (uint8_t *)&zero
,
196 TPM_CHECK(tpm_set_global_lock());
197 /* Verifies that write to index0 fails */
199 result
= tpm_nv_write_value(INDEX0
, (uint8_t *)&x
, sizeof(x
));
200 assert(result
== TPM_AREA_LOCKED
);
201 TPM_CHECK(tpm_nv_read_value(INDEX0
, (uint8_t *)&x
, sizeof(x
)));
203 /* Verifies that write to index1 is still possible */
205 TPM_CHECK(tpm_nv_write_value(INDEX1
, (uint8_t *)&x
, sizeof(x
)));
206 TPM_CHECK(tpm_nv_read_value(INDEX1
, (uint8_t *)&x
, sizeof(x
)));
209 tpm_tsc_physical_presence(PHYS_PRESENCE
);
210 /* Verifies that write to index1 fails */
212 result
= tpm_nv_write_value(INDEX1
, (uint8_t *)&x
, sizeof(x
));
213 assert(result
== TPM_BAD_PRESENCE
);
214 TPM_CHECK(tpm_nv_read_value(INDEX1
, (uint8_t *)&x
, sizeof(x
)));
220 static int test_lock(void)
222 printf("Testing lock ...\n");
224 tpm_startup(TPM_ST_CLEAR
);
225 tpm_self_test_full();
226 tpm_tsc_physical_presence(PRESENCE
);
227 tpm_nv_write_value_lock(INDEX0
);
228 printf("\tLocked 0x%x\n", INDEX0
);
233 static void initialise_spaces(void)
236 uint32_t perm
= TPM_NV_PER_WRITE_STCLEAR
| TPM_NV_PER_PPWRITE
;
238 printf("\tInitialising spaces\n");
239 tpm_nv_set_locked(); /* useful only the first time */
240 tpm_nv_define_space(INDEX0
, perm
, 4);
241 tpm_nv_write_value(INDEX0
, (uint8_t *)&zero
, 4);
242 tpm_nv_define_space(INDEX1
, perm
, 4);
243 tpm_nv_write_value(INDEX1
, (uint8_t *)&zero
, 4);
244 tpm_nv_define_space(INDEX2
, perm
, 4);
245 tpm_nv_write_value(INDEX2
, (uint8_t *)&zero
, 4);
246 tpm_nv_define_space(INDEX3
, perm
, 4);
247 tpm_nv_write_value(INDEX3
, (uint8_t *)&zero
, 4);
248 perm
= TPM_NV_PER_READ_STCLEAR
| TPM_NV_PER_WRITE_STCLEAR
|
250 tpm_nv_define_space(INDEX_INITIALISED
, perm
, 1);
253 static int test_readonly(void)
256 uint32_t index_0
, index_1
, index_2
, index_3
;
257 int read0
, read1
, read2
, read3
;
259 printf("Testing readonly ...\n");
261 tpm_startup(TPM_ST_CLEAR
);
262 tpm_self_test_full();
263 tpm_tsc_physical_presence(PRESENCE
);
265 * Checks if initialisation has completed by trying to read-lock a
266 * space that's created at the end of initialisation
268 if (tpm_nv_read_value(INDEX_INITIALISED
, &c
, 0) == TPM_BADINDEX
) {
269 /* The initialisation did not complete */
273 /* Checks if spaces are OK or messed up */
274 read0
= tpm_nv_read_value(INDEX0
, (uint8_t *)&index_0
, sizeof(index_0
));
275 read1
= tpm_nv_read_value(INDEX1
, (uint8_t *)&index_1
, sizeof(index_1
));
276 read2
= tpm_nv_read_value(INDEX2
, (uint8_t *)&index_2
, sizeof(index_2
));
277 read3
= tpm_nv_read_value(INDEX3
, (uint8_t *)&index_3
, sizeof(index_3
));
278 if (read0
|| read1
|| read2
|| read3
) {
279 printf("Invalid contents\n");
284 * Writes space, and locks it. Then attempts to write again.
285 * I really wish I could use the imperative.
288 if (tpm_nv_write_value(INDEX0
, (uint8_t *)&index_0
, sizeof(index_0
) !=
290 pr_err("\tcould not write index 0\n");
292 tpm_nv_write_value_lock(INDEX0
);
293 if (tpm_nv_write_value(INDEX0
, (uint8_t *)&index_0
, sizeof(index_0
)) ==
295 pr_err("\tindex 0 is not locked\n");
301 static int test_redefine_unowned(void)
307 printf("Testing redefine_unowned ...");
309 TPM_CHECK(TlclStartupIfNeeded());
310 TPM_CHECK(tpm_self_test_full());
311 TPM_CHECK(tpm_tsc_physical_presence(PRESENCE
));
312 assert(!tpm_is_owned());
314 /* Ensures spaces exist. */
315 TPM_CHECK(tpm_nv_read_value(INDEX0
, (uint8_t *)&x
, sizeof(x
)));
316 TPM_CHECK(tpm_nv_read_value(INDEX1
, (uint8_t *)&x
, sizeof(x
)));
318 /* Redefines spaces a couple of times. */
319 perm
= TPM_NV_PER_PPWRITE
| TPM_NV_PER_GLOBALLOCK
;
320 TPM_CHECK(tpm_nv_define_space(INDEX0
, perm
, 2 * sizeof(uint32_t)));
321 TPM_CHECK(tpm_nv_define_space(INDEX0
, perm
, sizeof(uint32_t)));
322 perm
= TPM_NV_PER_PPWRITE
;
323 TPM_CHECK(tpm_nv_define_space(INDEX1
, perm
, 2 * sizeof(uint32_t)));
324 TPM_CHECK(tpm_nv_define_space(INDEX1
, perm
, sizeof(uint32_t)));
326 /* Sets the global lock */
327 tpm_set_global_lock();
329 /* Verifies that index0 cannot be redefined */
330 result
= tpm_nv_define_space(INDEX0
, perm
, sizeof(uint32_t));
331 assert(result
== TPM_AREA_LOCKED
);
333 /* Checks that index1 can */
334 TPM_CHECK(tpm_nv_define_space(INDEX1
, perm
, 2 * sizeof(uint32_t)));
335 TPM_CHECK(tpm_nv_define_space(INDEX1
, perm
, sizeof(uint32_t)));
338 tpm_tsc_physical_presence(PHYS_PRESENCE
);
340 /* Verifies that neither index0 nor index1 can be redefined */
341 result
= tpm_nv_define_space(INDEX0
, perm
, sizeof(uint32_t));
342 assert(result
== TPM_BAD_PRESENCE
);
343 result
= tpm_nv_define_space(INDEX1
, perm
, sizeof(uint32_t));
344 assert(result
== TPM_BAD_PRESENCE
);
350 #define PERMPPGL (TPM_NV_PER_PPWRITE | TPM_NV_PER_GLOBALLOCK)
351 #define PERMPP TPM_NV_PER_PPWRITE
353 static int test_space_perm(void)
357 printf("Testing spaceperm ...");
359 TPM_CHECK(TlclStartupIfNeeded());
360 TPM_CHECK(tpm_continue_self_test());
361 TPM_CHECK(tpm_tsc_physical_presence(PRESENCE
));
362 TPM_CHECK(tpm_get_permissions(INDEX0
, &perm
));
363 assert((perm
& PERMPPGL
) == PERMPPGL
);
364 TPM_CHECK(tpm_get_permissions(INDEX1
, &perm
));
365 assert((perm
& PERMPP
) == PERMPP
);
370 static int test_startup(void)
373 printf("Testing startup ...\n");
376 result
= tpm_startup(TPM_ST_CLEAR
);
377 if (result
!= 0 && result
!= TPM_INVALID_POSTINIT
)
378 printf("\ttpm startup failed with 0x%x\n", result
);
379 result
= tpm_get_flags(NULL
, NULL
, NULL
);
381 printf("\ttpm getflags failed with 0x%x\n", result
);
382 printf("\texecuting SelfTestFull\n");
383 tpm_self_test_full();
384 result
= tpm_get_flags(NULL
, NULL
, NULL
);
386 printf("\ttpm getflags failed with 0x%x\n", result
);
392 * Runs [op] and ensures it returns success and doesn't run longer than
393 * [time_limit] in milliseconds.
395 #define TTPM_CHECK(op, time_limit) do { \
399 start = get_timer(0); \
401 if (__result != TPM_SUCCESS) { \
402 printf("\t" #op ": error 0x%x\n", __result); \
405 time = get_timer(start); \
406 printf("\t" #op ": %lu ms\n", time); \
407 if (time > (ulong)time_limit) { \
408 printf("\t" #op " exceeded " #time_limit " ms\n"); \
413 static int test_timing(void)
416 uint8_t in
[20], out
[20];
418 printf("Testing timing ...");
420 TTPM_CHECK(TlclStartupIfNeeded(), 50);
421 TTPM_CHECK(tpm_continue_self_test(), 100);
422 TTPM_CHECK(tpm_self_test_full(), 1000);
423 TTPM_CHECK(tpm_tsc_physical_presence(PRESENCE
), 100);
424 TTPM_CHECK(tpm_nv_write_value(INDEX0
, (uint8_t *)&x
, sizeof(x
)), 100);
425 TTPM_CHECK(tpm_nv_read_value(INDEX0
, (uint8_t *)&x
, sizeof(x
)), 100);
426 TTPM_CHECK(tpm_extend(0, in
, out
), 200);
427 TTPM_CHECK(tpm_set_global_lock(), 50);
428 TTPM_CHECK(tpm_tsc_physical_presence(PHYS_PRESENCE
), 100);
433 #define TPM_MAX_NV_WRITES_NOOWNER 64
435 static int test_write_limit(void)
437 printf("Testing writelimit ...\n");
442 TPM_CHECK(TlclStartupIfNeeded());
443 TPM_CHECK(tpm_self_test_full());
444 TPM_CHECK(tpm_tsc_physical_presence(PRESENCE
));
445 TPM_CHECK(tpm_force_clear());
446 TPM_CHECK(tpm_physical_enable());
447 TPM_CHECK(tpm_physical_set_deactivated(0));
449 for (i
= 0; i
< TPM_MAX_NV_WRITES_NOOWNER
+ 2; i
++) {
450 printf("\twriting %d\n", i
);
451 result
= tpm_nv_write_value(INDEX0
, (uint8_t *)&i
, sizeof(i
));
455 case TPM_MAXNVWRITES
:
456 assert(i
>= TPM_MAX_NV_WRITES_NOOWNER
);
458 pr_err("\tunexpected error code %d (0x%x)\n",
463 /* Reset write count */
464 TPM_CHECK(tpm_force_clear());
465 TPM_CHECK(tpm_physical_enable());
466 TPM_CHECK(tpm_physical_set_deactivated(0));
468 /* Try writing again. */
469 TPM_CHECK(tpm_nv_write_value(INDEX0
, (uint8_t *)&i
, sizeof(i
)));
474 #define VOIDTEST(XFUNC) \
475 int do_test_##XFUNC(cmd_tbl_t *cmd_tbl, int flag, int argc, \
476 char * const argv[]) \
478 return test_##XFUNC(); \
481 #define VOIDENT(XNAME) \
482 U_BOOT_CMD_MKENT(XNAME, 0, 1, do_test_##XNAME, "", ""),
484 VOIDTEST(early_extend
)
485 VOIDTEST(early_nvram
)
486 VOIDTEST(early_nvram2
)
488 VOIDTEST(fast_enable
)
489 VOIDTEST(global_lock
)
492 VOIDTEST(redefine_unowned
)
496 VOIDTEST(write_limit
)
499 static cmd_tbl_t cmd_cros_tpm_sub
[] = {
500 VOIDENT(early_extend
)
502 VOIDENT(early_nvram2
)
508 VOIDENT(redefine_unowned
)
516 static int do_tpmtest(cmd_tbl_t
*cmdtp
, int flag
, int argc
, char * const argv
[])
521 printf("argc = %d, argv = ", argc
);
523 for (i
= 0; i
< argc
; i
++)
524 printf(" %s", argv
[i
]);
526 printf("\n------\n");
530 c
= find_cmd_tbl(argv
[0], cmd_cros_tpm_sub
,
531 ARRAY_SIZE(cmd_cros_tpm_sub
));
532 return c
? c
->cmd(cmdtp
, flag
, argc
, argv
) : cmd_usage(cmdtp
);
535 U_BOOT_CMD(tpmtest
, 2, 1, do_tpmtest
, "TPM tests",
544 "\tredefine_unowned\n"