blob: ebfb25cab0a3c8ea0a0c76077d665534a37ce9e0 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Simon Glass45158dd2015-08-22 18:31:42 -06002/*
3 * Copyright (c) 2015 Google, Inc
Simon Glass45158dd2015-08-22 18:31:42 -06004 */
5
6#include <common.h>
7#include <command.h>
Simon Glassafb02152019-12-28 10:45:01 -07008#include <cpu_func.h>
Simon Glass0f2af882020-05-10 11:40:05 -06009#include <log.h>
Miquel Raynal4c6759e2018-05-15 11:57:06 +020010#include <tpm-v1.h>
Simon Glass8ceca1d2018-11-18 14:22:27 -070011#include "tpm-user-utils.h"
Simon Glass45158dd2015-08-22 18:31:42 -060012
13/* Prints error and returns on failure */
14#define TPM_CHECK(tpm_command) do { \
15 uint32_t result; \
16 \
17 result = (tpm_command); \
18 if (result != TPM_SUCCESS) { \
19 printf("TEST FAILED: line %d: " #tpm_command ": 0x%x\n", \
20 __LINE__, result); \
21 return result; \
22 } \
23} while (0)
24
25#define INDEX0 0xda70
26#define INDEX1 0xda71
27#define INDEX2 0xda72
28#define INDEX3 0xda73
29#define INDEX_INITIALISED 0xda80
30#define PHYS_PRESENCE 4
31#define PRESENCE 8
32
Simon Glass8ceca1d2018-11-18 14:22:27 -070033static uint32_t TlclStartupIfNeeded(struct udevice *dev)
Simon Glass45158dd2015-08-22 18:31:42 -060034{
Simon Glass8ceca1d2018-11-18 14:22:27 -070035 uint32_t result = tpm_startup(dev, TPM_ST_CLEAR);
Simon Glass45158dd2015-08-22 18:31:42 -060036
37 return result == TPM_INVALID_POSTINIT ? TPM_SUCCESS : result;
38}
39
Simon Glass8ceca1d2018-11-18 14:22:27 -070040static int test_timer(struct udevice *dev)
Simon Glass45158dd2015-08-22 18:31:42 -060041{
42 printf("get_timer(0) = %lu\n", get_timer(0));
43 return 0;
44}
45
Simon Glass8ceca1d2018-11-18 14:22:27 -070046static uint32_t tpm_get_flags(struct udevice *dev, uint8_t *disable,
47 uint8_t *deactivated, uint8_t *nvlocked)
Simon Glass45158dd2015-08-22 18:31:42 -060048{
49 struct tpm_permanent_flags pflags;
50 uint32_t result;
51
Simon Glass8ceca1d2018-11-18 14:22:27 -070052 result = tpm_get_permanent_flags(dev, &pflags);
Simon Glass45158dd2015-08-22 18:31:42 -060053 if (result)
54 return result;
55 if (disable)
56 *disable = pflags.disable;
57 if (deactivated)
58 *deactivated = pflags.deactivated;
59 if (nvlocked)
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);
63
64 return 0;
65}
66
Simon Glass8ceca1d2018-11-18 14:22:27 -070067static uint32_t tpm_nv_write_value_lock(struct udevice *dev, uint32_t index)
Simon Glass45158dd2015-08-22 18:31:42 -060068{
69 debug("TPM: Write lock 0x%x\n", index);
70
Simon Glass8ceca1d2018-11-18 14:22:27 -070071 return tpm_nv_write_value(dev, index, NULL, 0);
Simon Glass45158dd2015-08-22 18:31:42 -060072}
73
Simon Glass8ceca1d2018-11-18 14:22:27 -070074static int tpm_is_owned(struct udevice *dev)
Simon Glass45158dd2015-08-22 18:31:42 -060075{
76 uint8_t response[TPM_PUBEK_SIZE];
77 uint32_t result;
78
Simon Glass8ceca1d2018-11-18 14:22:27 -070079 result = tpm_read_pubek(dev, response, sizeof(response));
Simon Glass45158dd2015-08-22 18:31:42 -060080
81 return result != TPM_SUCCESS;
82}
83
Simon Glass8ceca1d2018-11-18 14:22:27 -070084static int test_early_extend(struct udevice *dev)
Simon Glass45158dd2015-08-22 18:31:42 -060085{
86 uint8_t value_in[20];
87 uint8_t value_out[20];
88
89 printf("Testing earlyextend ...");
Simon Glass8ceca1d2018-11-18 14:22:27 -070090 tpm_init(dev);
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));
Simon Glass45158dd2015-08-22 18:31:42 -060094 printf("done\n");
95 return 0;
96}
97
Simon Glass8ceca1d2018-11-18 14:22:27 -070098static int test_early_nvram(struct udevice *dev)
Simon Glass45158dd2015-08-22 18:31:42 -060099{
100 uint32_t x;
101
102 printf("Testing earlynvram ...");
Simon Glass8ceca1d2018-11-18 14:22:27 -0700103 tpm_init(dev);
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)));
Simon Glass45158dd2015-08-22 18:31:42 -0600108 printf("done\n");
109 return 0;
110}
111
Simon Glass8ceca1d2018-11-18 14:22:27 -0700112static int test_early_nvram2(struct udevice *dev)
Simon Glass45158dd2015-08-22 18:31:42 -0600113{
114 uint32_t x;
115
116 printf("Testing earlynvram2 ...");
Simon Glass8ceca1d2018-11-18 14:22:27 -0700117 tpm_init(dev);
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)));
Simon Glass45158dd2015-08-22 18:31:42 -0600122 printf("done\n");
123 return 0;
124}
125
Simon Glass8ceca1d2018-11-18 14:22:27 -0700126static int test_enable(struct udevice *dev)
Simon Glass45158dd2015-08-22 18:31:42 -0600127{
128 uint8_t disable = 0, deactivated = 0;
129
130 printf("Testing enable ...\n");
Simon Glass8ceca1d2018-11-18 14:22:27 -0700131 tpm_init(dev);
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));
Simon Glass45158dd2015-08-22 18:31:42 -0600136 printf("\tdisable is %d, deactivated is %d\n", disable, deactivated);
Simon Glass8ceca1d2018-11-18 14:22:27 -0700137 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));
Simon Glass45158dd2015-08-22 18:31:42 -0600140 printf("\tdisable is %d, deactivated is %d\n", disable, deactivated);
141 if (disable == 1 || deactivated == 1)
142 printf("\tfailed to enable or activate\n");
143 printf("\tdone\n");
144 return 0;
145}
146
147#define reboot() do { \
148 printf("\trebooting...\n"); \
149 reset_cpu(0); \
150} while (0)
151
Simon Glass8ceca1d2018-11-18 14:22:27 -0700152static int test_fast_enable(struct udevice *dev)
Simon Glass45158dd2015-08-22 18:31:42 -0600153{
154 uint8_t disable = 0, deactivated = 0;
155 int i;
156
157 printf("Testing fastenable ...\n");
Simon Glass8ceca1d2018-11-18 14:22:27 -0700158 tpm_init(dev);
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));
Simon Glass45158dd2015-08-22 18:31:42 -0600163 printf("\tdisable is %d, deactivated is %d\n", disable, deactivated);
164 for (i = 0; i < 2; i++) {
Simon Glass8ceca1d2018-11-18 14:22:27 -0700165 TPM_CHECK(tpm_force_clear(dev));
166 TPM_CHECK(tpm_get_flags(dev, &disable, &deactivated, NULL));
Simon Glass45158dd2015-08-22 18:31:42 -0600167 printf("\tdisable is %d, deactivated is %d\n", disable,
168 deactivated);
169 assert(disable == 1 && deactivated == 1);
Simon Glass8ceca1d2018-11-18 14:22:27 -0700170 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));
Simon Glass45158dd2015-08-22 18:31:42 -0600173 printf("\tdisable is %d, deactivated is %d\n", disable,
174 deactivated);
175 assert(disable == 0 && deactivated == 0);
176 }
177 printf("\tdone\n");
178 return 0;
179}
180
Simon Glass8ceca1d2018-11-18 14:22:27 -0700181static int test_global_lock(struct udevice *dev)
Simon Glass45158dd2015-08-22 18:31:42 -0600182{
183 uint32_t zero = 0;
184 uint32_t result;
185 uint32_t x;
186
187 printf("Testing globallock ...\n");
Simon Glass8ceca1d2018-11-18 14:22:27 -0700188 tpm_init(dev);
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,
Simon Glass45158dd2015-08-22 18:31:42 -0600194 sizeof(uint32_t)));
Simon Glass8ceca1d2018-11-18 14:22:27 -0700195 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,
Simon Glass45158dd2015-08-22 18:31:42 -0600197 sizeof(uint32_t)));
Simon Glass8ceca1d2018-11-18 14:22:27 -0700198 TPM_CHECK(tpm_set_global_lock(dev));
Simon Glass45158dd2015-08-22 18:31:42 -0600199 /* Verifies that write to index0 fails */
200 x = 1;
Simon Glass8ceca1d2018-11-18 14:22:27 -0700201 result = tpm_nv_write_value(dev, INDEX0, (uint8_t *)&x, sizeof(x));
Simon Glass45158dd2015-08-22 18:31:42 -0600202 assert(result == TPM_AREA_LOCKED);
Simon Glass8ceca1d2018-11-18 14:22:27 -0700203 TPM_CHECK(tpm_nv_read_value(dev, INDEX0, (uint8_t *)&x, sizeof(x)));
Simon Glass45158dd2015-08-22 18:31:42 -0600204 assert(x == 0);
205 /* Verifies that write to index1 is still possible */
206 x = 2;
Simon Glass8ceca1d2018-11-18 14:22:27 -0700207 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)));
Simon Glass45158dd2015-08-22 18:31:42 -0600209 assert(x == 2);
210 /* Turns off PP */
Simon Glass8ceca1d2018-11-18 14:22:27 -0700211 tpm_tsc_physical_presence(dev, PHYS_PRESENCE);
Simon Glass45158dd2015-08-22 18:31:42 -0600212 /* Verifies that write to index1 fails */
213 x = 3;
Simon Glass8ceca1d2018-11-18 14:22:27 -0700214 result = tpm_nv_write_value(dev, INDEX1, (uint8_t *)&x, sizeof(x));
Simon Glass45158dd2015-08-22 18:31:42 -0600215 assert(result == TPM_BAD_PRESENCE);
Simon Glass8ceca1d2018-11-18 14:22:27 -0700216 TPM_CHECK(tpm_nv_read_value(dev, INDEX1, (uint8_t *)&x, sizeof(x)));
Simon Glass45158dd2015-08-22 18:31:42 -0600217 assert(x == 2);
218 printf("\tdone\n");
219 return 0;
220}
221
Simon Glass8ceca1d2018-11-18 14:22:27 -0700222static int test_lock(struct udevice *dev)
Simon Glass45158dd2015-08-22 18:31:42 -0600223{
224 printf("Testing lock ...\n");
Simon Glass8ceca1d2018-11-18 14:22:27 -0700225 tpm_init(dev);
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);
Simon Glass45158dd2015-08-22 18:31:42 -0600230 printf("\tLocked 0x%x\n", INDEX0);
231 printf("\tdone\n");
232 return 0;
233}
234
Simon Glass8ceca1d2018-11-18 14:22:27 -0700235static void initialise_spaces(struct udevice *dev)
Simon Glass45158dd2015-08-22 18:31:42 -0600236{
237 uint32_t zero = 0;
238 uint32_t perm = TPM_NV_PER_WRITE_STCLEAR | TPM_NV_PER_PPWRITE;
239
240 printf("\tInitialising spaces\n");
Simon Glass8ceca1d2018-11-18 14:22:27 -0700241 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);
Simon Glass45158dd2015-08-22 18:31:42 -0600250 perm = TPM_NV_PER_READ_STCLEAR | TPM_NV_PER_WRITE_STCLEAR |
251 TPM_NV_PER_PPWRITE;
Simon Glass8ceca1d2018-11-18 14:22:27 -0700252 tpm_nv_define_space(dev, INDEX_INITIALISED, perm, 1);
Simon Glass45158dd2015-08-22 18:31:42 -0600253}
254
Simon Glass8ceca1d2018-11-18 14:22:27 -0700255static int test_readonly(struct udevice *dev)
Simon Glass45158dd2015-08-22 18:31:42 -0600256{
257 uint8_t c;
258 uint32_t index_0, index_1, index_2, index_3;
259 int read0, read1, read2, read3;
260
261 printf("Testing readonly ...\n");
Simon Glass8ceca1d2018-11-18 14:22:27 -0700262 tpm_init(dev);
263 tpm_startup(dev, TPM_ST_CLEAR);
264 tpm_self_test_full(dev);
265 tpm_tsc_physical_presence(dev, PRESENCE);
Simon Glass45158dd2015-08-22 18:31:42 -0600266 /*
267 * Checks if initialisation has completed by trying to read-lock a
268 * space that's created at the end of initialisation
269 */
Simon Glass8ceca1d2018-11-18 14:22:27 -0700270 if (tpm_nv_read_value(dev, INDEX_INITIALISED, &c, 0) == TPM_BADINDEX) {
Simon Glass45158dd2015-08-22 18:31:42 -0600271 /* The initialisation did not complete */
Simon Glass8ceca1d2018-11-18 14:22:27 -0700272 initialise_spaces(dev);
Simon Glass45158dd2015-08-22 18:31:42 -0600273 }
274
275 /* Checks if spaces are OK or messed up */
Simon Glass8ceca1d2018-11-18 14:22:27 -0700276 read0 = tpm_nv_read_value(dev, INDEX0, (uint8_t *)&index_0,
277 sizeof(index_0));
278 read1 = tpm_nv_read_value(dev, INDEX1, (uint8_t *)&index_1,
279 sizeof(index_1));
280 read2 = tpm_nv_read_value(dev, INDEX2, (uint8_t *)&index_2,
281 sizeof(index_2));
282 read3 = tpm_nv_read_value(dev, INDEX3, (uint8_t *)&index_3,
283 sizeof(index_3));
Simon Glass45158dd2015-08-22 18:31:42 -0600284 if (read0 || read1 || read2 || read3) {
285 printf("Invalid contents\n");
286 return 0;
287 }
288
289 /*
290 * Writes space, and locks it. Then attempts to write again.
291 * I really wish I could use the imperative.
292 */
293 index_0 += 1;
Simon Glass8ceca1d2018-11-18 14:22:27 -0700294 if (tpm_nv_write_value(dev, INDEX0, (uint8_t *)&index_0,
295 sizeof(index_0) !=
Simon Glass45158dd2015-08-22 18:31:42 -0600296 TPM_SUCCESS)) {
Masahiro Yamada81e10422017-09-16 14:10:41 +0900297 pr_err("\tcould not write index 0\n");
Simon Glass45158dd2015-08-22 18:31:42 -0600298 }
Simon Glass8ceca1d2018-11-18 14:22:27 -0700299 tpm_nv_write_value_lock(dev, INDEX0);
300 if (tpm_nv_write_value(dev, INDEX0, (uint8_t *)&index_0,
301 sizeof(index_0)) ==
Simon Glass45158dd2015-08-22 18:31:42 -0600302 TPM_SUCCESS)
Masahiro Yamada81e10422017-09-16 14:10:41 +0900303 pr_err("\tindex 0 is not locked\n");
Simon Glass45158dd2015-08-22 18:31:42 -0600304
305 printf("\tdone\n");
306 return 0;
307}
308
Simon Glass8ceca1d2018-11-18 14:22:27 -0700309static int test_redefine_unowned(struct udevice *dev)
Simon Glass45158dd2015-08-22 18:31:42 -0600310{
311 uint32_t perm;
312 uint32_t result;
313 uint32_t x;
314
315 printf("Testing redefine_unowned ...");
Simon Glass8ceca1d2018-11-18 14:22:27 -0700316 tpm_init(dev);
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));
Simon Glass45158dd2015-08-22 18:31:42 -0600321
322 /* Ensures spaces exist. */
Simon Glass8ceca1d2018-11-18 14:22:27 -0700323 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)));
Simon Glass45158dd2015-08-22 18:31:42 -0600325
326 /* Redefines spaces a couple of times. */
327 perm = TPM_NV_PER_PPWRITE | TPM_NV_PER_GLOBALLOCK;
Simon Glass8ceca1d2018-11-18 14:22:27 -0700328 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)));
Simon Glass45158dd2015-08-22 18:31:42 -0600330 perm = TPM_NV_PER_PPWRITE;
Simon Glass8ceca1d2018-11-18 14:22:27 -0700331 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)));
Simon Glass45158dd2015-08-22 18:31:42 -0600333
334 /* Sets the global lock */
Simon Glass8ceca1d2018-11-18 14:22:27 -0700335 tpm_set_global_lock(dev);
Simon Glass45158dd2015-08-22 18:31:42 -0600336
337 /* Verifies that index0 cannot be redefined */
Simon Glass8ceca1d2018-11-18 14:22:27 -0700338 result = tpm_nv_define_space(dev, INDEX0, perm, sizeof(uint32_t));
Simon Glass45158dd2015-08-22 18:31:42 -0600339 assert(result == TPM_AREA_LOCKED);
340
341 /* Checks that index1 can */
Simon Glass8ceca1d2018-11-18 14:22:27 -0700342 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)));
Simon Glass45158dd2015-08-22 18:31:42 -0600344
345 /* Turns off PP */
Simon Glass8ceca1d2018-11-18 14:22:27 -0700346 tpm_tsc_physical_presence(dev, PHYS_PRESENCE);
Simon Glass45158dd2015-08-22 18:31:42 -0600347
348 /* Verifies that neither index0 nor index1 can be redefined */
Simon Glass8ceca1d2018-11-18 14:22:27 -0700349 result = tpm_nv_define_space(dev, INDEX0, perm, sizeof(uint32_t));
Simon Glass45158dd2015-08-22 18:31:42 -0600350 assert(result == TPM_BAD_PRESENCE);
Simon Glass8ceca1d2018-11-18 14:22:27 -0700351 result = tpm_nv_define_space(dev, INDEX1, perm, sizeof(uint32_t));
Simon Glass45158dd2015-08-22 18:31:42 -0600352 assert(result == TPM_BAD_PRESENCE);
353
354 printf("done\n");
355 return 0;
356}
357
358#define PERMPPGL (TPM_NV_PER_PPWRITE | TPM_NV_PER_GLOBALLOCK)
359#define PERMPP TPM_NV_PER_PPWRITE
360
Simon Glass8ceca1d2018-11-18 14:22:27 -0700361static int test_space_perm(struct udevice *dev)
Simon Glass45158dd2015-08-22 18:31:42 -0600362{
363 uint32_t perm;
364
365 printf("Testing spaceperm ...");
Simon Glass8ceca1d2018-11-18 14:22:27 -0700366 tpm_init(dev);
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));
Simon Glass45158dd2015-08-22 18:31:42 -0600371 assert((perm & PERMPPGL) == PERMPPGL);
Simon Glass8ceca1d2018-11-18 14:22:27 -0700372 TPM_CHECK(tpm_get_permissions(dev, INDEX1, &perm));
Simon Glass45158dd2015-08-22 18:31:42 -0600373 assert((perm & PERMPP) == PERMPP);
374 printf("done\n");
375 return 0;
376}
377
Simon Glass8ceca1d2018-11-18 14:22:27 -0700378static int test_startup(struct udevice *dev)
Simon Glass45158dd2015-08-22 18:31:42 -0600379{
380 uint32_t result;
Simon Glass8ceca1d2018-11-18 14:22:27 -0700381
Simon Glass45158dd2015-08-22 18:31:42 -0600382 printf("Testing startup ...\n");
383
Simon Glass8ceca1d2018-11-18 14:22:27 -0700384 tpm_init(dev);
385 result = tpm_startup(dev, TPM_ST_CLEAR);
Simon Glass45158dd2015-08-22 18:31:42 -0600386 if (result != 0 && result != TPM_INVALID_POSTINIT)
387 printf("\ttpm startup failed with 0x%x\n", result);
Simon Glass8ceca1d2018-11-18 14:22:27 -0700388 result = tpm_get_flags(dev, NULL, NULL, NULL);
Simon Glass45158dd2015-08-22 18:31:42 -0600389 if (result != 0)
390 printf("\ttpm getflags failed with 0x%x\n", result);
391 printf("\texecuting SelfTestFull\n");
Simon Glass8ceca1d2018-11-18 14:22:27 -0700392 tpm_self_test_full(dev);
393 result = tpm_get_flags(dev, NULL, NULL, NULL);
Simon Glass45158dd2015-08-22 18:31:42 -0600394 if (result != 0)
395 printf("\ttpm getflags failed with 0x%x\n", result);
396 printf("\tdone\n");
397 return 0;
398}
399
400/*
401 * Runs [op] and ensures it returns success and doesn't run longer than
402 * [time_limit] in milliseconds.
403 */
404#define TTPM_CHECK(op, time_limit) do { \
405 ulong start, time; \
406 uint32_t __result; \
407 \
408 start = get_timer(0); \
409 __result = op; \
410 if (__result != TPM_SUCCESS) { \
411 printf("\t" #op ": error 0x%x\n", __result); \
412 return -1; \
413 } \
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"); \
418 } \
419} while (0)
420
421
Simon Glass8ceca1d2018-11-18 14:22:27 -0700422static int test_timing(struct udevice *dev)
Simon Glass45158dd2015-08-22 18:31:42 -0600423{
Simon Glass45158dd2015-08-22 18:31:42 -0600424 uint8_t in[20], out[20];
Simon Glass8ceca1d2018-11-18 14:22:27 -0700425 uint32_t x;
Simon Glass45158dd2015-08-22 18:31:42 -0600426
427 printf("Testing timing ...");
Simon Glass8ceca1d2018-11-18 14:22:27 -0700428 tpm_init(dev);
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)),
434 100);
435 TTPM_CHECK(tpm_nv_read_value(dev, INDEX0, (uint8_t *)&x, sizeof(x)),
436 100);
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);
Simon Glass45158dd2015-08-22 18:31:42 -0600440 printf("done\n");
441 return 0;
442}
443
444#define TPM_MAX_NV_WRITES_NOOWNER 64
445
Simon Glass8ceca1d2018-11-18 14:22:27 -0700446static int test_write_limit(struct udevice *dev)
Simon Glass45158dd2015-08-22 18:31:42 -0600447{
Simon Glass45158dd2015-08-22 18:31:42 -0600448 uint32_t result;
Simon Glass8ceca1d2018-11-18 14:22:27 -0700449 int i;
Simon Glass45158dd2015-08-22 18:31:42 -0600450
Simon Glass8ceca1d2018-11-18 14:22:27 -0700451 printf("Testing writelimit ...\n");
452 tpm_init(dev);
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));
Simon Glass45158dd2015-08-22 18:31:42 -0600459
460 for (i = 0; i < TPM_MAX_NV_WRITES_NOOWNER + 2; i++) {
461 printf("\twriting %d\n", i);
Simon Glass8ceca1d2018-11-18 14:22:27 -0700462 result = tpm_nv_write_value(dev, INDEX0, (uint8_t *)&i,
463 sizeof(i));
Simon Glass45158dd2015-08-22 18:31:42 -0600464 switch (result) {
465 case TPM_SUCCESS:
466 break;
467 case TPM_MAXNVWRITES:
468 assert(i >= TPM_MAX_NV_WRITES_NOOWNER);
469 default:
Masahiro Yamada81e10422017-09-16 14:10:41 +0900470 pr_err("\tunexpected error code %d (0x%x)\n",
Simon Glass45158dd2015-08-22 18:31:42 -0600471 result, result);
472 }
473 }
474
475 /* Reset write count */
Simon Glass8ceca1d2018-11-18 14:22:27 -0700476 TPM_CHECK(tpm_force_clear(dev));
477 TPM_CHECK(tpm_physical_enable(dev));
478 TPM_CHECK(tpm_physical_set_deactivated(dev, 0));
Simon Glass45158dd2015-08-22 18:31:42 -0600479
480 /* Try writing again. */
Simon Glass8ceca1d2018-11-18 14:22:27 -0700481 TPM_CHECK(tpm_nv_write_value(dev, INDEX0, (uint8_t *)&i, sizeof(i)));
Simon Glass45158dd2015-08-22 18:31:42 -0600482 printf("\tdone\n");
483 return 0;
484}
485
486#define VOIDTEST(XFUNC) \
Simon Glassed38aef2020-05-10 11:40:03 -0600487 int do_test_##XFUNC(struct cmd_tbl *cmd_tbl, int flag, int argc, \
488 char *const argv[]) \
Simon Glass45158dd2015-08-22 18:31:42 -0600489 { \
Simon Glass8ceca1d2018-11-18 14:22:27 -0700490 struct udevice *dev; \
491 int ret; \
492\
493 ret = get_tpm(&dev); \
494 if (ret) \
495 return ret; \
496 return test_##XFUNC(dev); \
Simon Glass45158dd2015-08-22 18:31:42 -0600497 }
498
499#define VOIDENT(XNAME) \
500 U_BOOT_CMD_MKENT(XNAME, 0, 1, do_test_##XNAME, "", ""),
501
502VOIDTEST(early_extend)
503VOIDTEST(early_nvram)
504VOIDTEST(early_nvram2)
505VOIDTEST(enable)
506VOIDTEST(fast_enable)
507VOIDTEST(global_lock)
508VOIDTEST(lock)
509VOIDTEST(readonly)
510VOIDTEST(redefine_unowned)
511VOIDTEST(space_perm)
512VOIDTEST(startup)
513VOIDTEST(timing)
514VOIDTEST(write_limit)
515VOIDTEST(timer)
516
Simon Glassed38aef2020-05-10 11:40:03 -0600517static struct cmd_tbl cmd_cros_tpm_sub[] = {
Simon Glass45158dd2015-08-22 18:31:42 -0600518 VOIDENT(early_extend)
519 VOIDENT(early_nvram)
520 VOIDENT(early_nvram2)
521 VOIDENT(enable)
522 VOIDENT(fast_enable)
523 VOIDENT(global_lock)
524 VOIDENT(lock)
525 VOIDENT(readonly)
526 VOIDENT(redefine_unowned)
527 VOIDENT(space_perm)
528 VOIDENT(startup)
529 VOIDENT(timing)
530 VOIDENT(write_limit)
531 VOIDENT(timer)
532};
533
Simon Glassed38aef2020-05-10 11:40:03 -0600534static int do_tpmtest(struct cmd_tbl *cmdtp, int flag, int argc,
535 char *const argv[])
Simon Glass45158dd2015-08-22 18:31:42 -0600536{
Simon Glassed38aef2020-05-10 11:40:03 -0600537 struct cmd_tbl *c;
Stefan Brünseefa3022016-10-16 17:13:55 +0200538 int i;
Simon Glass45158dd2015-08-22 18:31:42 -0600539
540 printf("argc = %d, argv = ", argc);
Stefan Brünseefa3022016-10-16 17:13:55 +0200541
542 for (i = 0; i < argc; i++)
543 printf(" %s", argv[i]);
544
545 printf("\n------\n");
Simon Glass45158dd2015-08-22 18:31:42 -0600546
Simon Glass45158dd2015-08-22 18:31:42 -0600547 argc--;
548 argv++;
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);
552}
553
554U_BOOT_CMD(tpmtest, 2, 1, do_tpmtest, "TPM tests",
555 "\n\tearly_extend\n"
556 "\tearly_nvram\n"
557 "\tearly_nvram2\n"
558 "\tenable\n"
559 "\tfast_enable\n"
560 "\tglobal_lock\n"
561 "\tlock\n"
562 "\treadonly\n"
563 "\tredefine_unowned\n"
564 "\tspace_perm\n"
565 "\tstartup\n"
566 "\ttiming\n"
567 "\twrite_limit\n");