]>
Commit | Line | Data |
---|---|---|
83d290c5 | 1 | // SPDX-License-Identifier: GPL-2.0+ |
e76cb927 SG |
2 | /* |
3 | * Copyright (c) 2015 Google, Inc | |
e76cb927 SG |
4 | */ |
5 | ||
6 | #include <common.h> | |
7 | #include <command.h> | |
d677bfe2 | 8 | #include <tpm-v1.h> |
abdc7b8a | 9 | #include "tpm-user-utils.h" |
e76cb927 SG |
10 | |
11 | /* Prints error and returns on failure */ | |
12 | #define TPM_CHECK(tpm_command) do { \ | |
13 | uint32_t result; \ | |
14 | \ | |
15 | result = (tpm_command); \ | |
16 | if (result != TPM_SUCCESS) { \ | |
17 | printf("TEST FAILED: line %d: " #tpm_command ": 0x%x\n", \ | |
18 | __LINE__, result); \ | |
19 | return result; \ | |
20 | } \ | |
21 | } while (0) | |
22 | ||
23 | #define INDEX0 0xda70 | |
24 | #define INDEX1 0xda71 | |
25 | #define INDEX2 0xda72 | |
26 | #define INDEX3 0xda73 | |
27 | #define INDEX_INITIALISED 0xda80 | |
28 | #define PHYS_PRESENCE 4 | |
29 | #define PRESENCE 8 | |
30 | ||
abdc7b8a | 31 | static uint32_t TlclStartupIfNeeded(struct udevice *dev) |
e76cb927 | 32 | { |
abdc7b8a | 33 | uint32_t result = tpm_startup(dev, TPM_ST_CLEAR); |
e76cb927 SG |
34 | |
35 | return result == TPM_INVALID_POSTINIT ? TPM_SUCCESS : result; | |
36 | } | |
37 | ||
abdc7b8a | 38 | static int test_timer(struct udevice *dev) |
e76cb927 SG |
39 | { |
40 | printf("get_timer(0) = %lu\n", get_timer(0)); | |
41 | return 0; | |
42 | } | |
43 | ||
abdc7b8a SG |
44 | static uint32_t tpm_get_flags(struct udevice *dev, uint8_t *disable, |
45 | uint8_t *deactivated, uint8_t *nvlocked) | |
e76cb927 SG |
46 | { |
47 | struct tpm_permanent_flags pflags; | |
48 | uint32_t result; | |
49 | ||
abdc7b8a | 50 | result = tpm_get_permanent_flags(dev, &pflags); |
e76cb927 SG |
51 | if (result) |
52 | return result; | |
53 | if (disable) | |
54 | *disable = pflags.disable; | |
55 | if (deactivated) | |
56 | *deactivated = pflags.deactivated; | |
57 | if (nvlocked) | |
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); | |
61 | ||
62 | return 0; | |
63 | } | |
64 | ||
abdc7b8a | 65 | static uint32_t tpm_nv_write_value_lock(struct udevice *dev, uint32_t index) |
e76cb927 SG |
66 | { |
67 | debug("TPM: Write lock 0x%x\n", index); | |
68 | ||
abdc7b8a | 69 | return tpm_nv_write_value(dev, index, NULL, 0); |
e76cb927 SG |
70 | } |
71 | ||
abdc7b8a | 72 | static int tpm_is_owned(struct udevice *dev) |
e76cb927 SG |
73 | { |
74 | uint8_t response[TPM_PUBEK_SIZE]; | |
75 | uint32_t result; | |
76 | ||
abdc7b8a | 77 | result = tpm_read_pubek(dev, response, sizeof(response)); |
e76cb927 SG |
78 | |
79 | return result != TPM_SUCCESS; | |
80 | } | |
81 | ||
abdc7b8a | 82 | static int test_early_extend(struct udevice *dev) |
e76cb927 SG |
83 | { |
84 | uint8_t value_in[20]; | |
85 | uint8_t value_out[20]; | |
86 | ||
87 | printf("Testing earlyextend ..."); | |
abdc7b8a SG |
88 | tpm_init(dev); |
89 | TPM_CHECK(tpm_startup(dev, TPM_ST_CLEAR)); | |
90 | TPM_CHECK(tpm_continue_self_test(dev)); | |
91 | TPM_CHECK(tpm_extend(dev, 1, value_in, value_out)); | |
e76cb927 SG |
92 | printf("done\n"); |
93 | return 0; | |
94 | } | |
95 | ||
abdc7b8a | 96 | static int test_early_nvram(struct udevice *dev) |
e76cb927 SG |
97 | { |
98 | uint32_t x; | |
99 | ||
100 | printf("Testing earlynvram ..."); | |
abdc7b8a SG |
101 | tpm_init(dev); |
102 | TPM_CHECK(tpm_startup(dev, TPM_ST_CLEAR)); | |
103 | TPM_CHECK(tpm_continue_self_test(dev)); | |
104 | TPM_CHECK(tpm_tsc_physical_presence(dev, PRESENCE)); | |
105 | TPM_CHECK(tpm_nv_read_value(dev, INDEX0, (uint8_t *)&x, sizeof(x))); | |
e76cb927 SG |
106 | printf("done\n"); |
107 | return 0; | |
108 | } | |
109 | ||
abdc7b8a | 110 | static int test_early_nvram2(struct udevice *dev) |
e76cb927 SG |
111 | { |
112 | uint32_t x; | |
113 | ||
114 | printf("Testing earlynvram2 ..."); | |
abdc7b8a SG |
115 | tpm_init(dev); |
116 | TPM_CHECK(tpm_startup(dev, TPM_ST_CLEAR)); | |
117 | TPM_CHECK(tpm_continue_self_test(dev)); | |
118 | TPM_CHECK(tpm_tsc_physical_presence(dev, PRESENCE)); | |
119 | TPM_CHECK(tpm_nv_write_value(dev, INDEX0, (uint8_t *)&x, sizeof(x))); | |
e76cb927 SG |
120 | printf("done\n"); |
121 | return 0; | |
122 | } | |
123 | ||
abdc7b8a | 124 | static int test_enable(struct udevice *dev) |
e76cb927 SG |
125 | { |
126 | uint8_t disable = 0, deactivated = 0; | |
127 | ||
128 | printf("Testing enable ...\n"); | |
abdc7b8a SG |
129 | tpm_init(dev); |
130 | TPM_CHECK(TlclStartupIfNeeded(dev)); | |
131 | TPM_CHECK(tpm_self_test_full(dev)); | |
132 | TPM_CHECK(tpm_tsc_physical_presence(dev, PRESENCE)); | |
133 | TPM_CHECK(tpm_get_flags(dev, &disable, &deactivated, NULL)); | |
e76cb927 | 134 | printf("\tdisable is %d, deactivated is %d\n", disable, deactivated); |
abdc7b8a SG |
135 | TPM_CHECK(tpm_physical_enable(dev)); |
136 | TPM_CHECK(tpm_physical_set_deactivated(dev, 0)); | |
137 | TPM_CHECK(tpm_get_flags(dev, &disable, &deactivated, NULL)); | |
e76cb927 SG |
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"); | |
141 | printf("\tdone\n"); | |
142 | return 0; | |
143 | } | |
144 | ||
145 | #define reboot() do { \ | |
146 | printf("\trebooting...\n"); \ | |
147 | reset_cpu(0); \ | |
148 | } while (0) | |
149 | ||
abdc7b8a | 150 | static int test_fast_enable(struct udevice *dev) |
e76cb927 SG |
151 | { |
152 | uint8_t disable = 0, deactivated = 0; | |
153 | int i; | |
154 | ||
155 | printf("Testing fastenable ...\n"); | |
abdc7b8a SG |
156 | tpm_init(dev); |
157 | TPM_CHECK(TlclStartupIfNeeded(dev)); | |
158 | TPM_CHECK(tpm_self_test_full(dev)); | |
159 | TPM_CHECK(tpm_tsc_physical_presence(dev, PRESENCE)); | |
160 | TPM_CHECK(tpm_get_flags(dev, &disable, &deactivated, NULL)); | |
e76cb927 SG |
161 | printf("\tdisable is %d, deactivated is %d\n", disable, deactivated); |
162 | for (i = 0; i < 2; i++) { | |
abdc7b8a SG |
163 | TPM_CHECK(tpm_force_clear(dev)); |
164 | TPM_CHECK(tpm_get_flags(dev, &disable, &deactivated, NULL)); | |
e76cb927 SG |
165 | printf("\tdisable is %d, deactivated is %d\n", disable, |
166 | deactivated); | |
167 | assert(disable == 1 && deactivated == 1); | |
abdc7b8a SG |
168 | TPM_CHECK(tpm_physical_enable(dev)); |
169 | TPM_CHECK(tpm_physical_set_deactivated(dev, 0)); | |
170 | TPM_CHECK(tpm_get_flags(dev, &disable, &deactivated, NULL)); | |
e76cb927 SG |
171 | printf("\tdisable is %d, deactivated is %d\n", disable, |
172 | deactivated); | |
173 | assert(disable == 0 && deactivated == 0); | |
174 | } | |
175 | printf("\tdone\n"); | |
176 | return 0; | |
177 | } | |
178 | ||
abdc7b8a | 179 | static int test_global_lock(struct udevice *dev) |
e76cb927 SG |
180 | { |
181 | uint32_t zero = 0; | |
182 | uint32_t result; | |
183 | uint32_t x; | |
184 | ||
185 | printf("Testing globallock ...\n"); | |
abdc7b8a SG |
186 | tpm_init(dev); |
187 | TPM_CHECK(TlclStartupIfNeeded(dev)); | |
188 | TPM_CHECK(tpm_self_test_full(dev)); | |
189 | TPM_CHECK(tpm_tsc_physical_presence(dev, PRESENCE)); | |
190 | TPM_CHECK(tpm_nv_read_value(dev, INDEX0, (uint8_t *)&x, sizeof(x))); | |
191 | TPM_CHECK(tpm_nv_write_value(dev, INDEX0, (uint8_t *)&zero, | |
e76cb927 | 192 | sizeof(uint32_t))); |
abdc7b8a SG |
193 | TPM_CHECK(tpm_nv_read_value(dev, INDEX1, (uint8_t *)&x, sizeof(x))); |
194 | TPM_CHECK(tpm_nv_write_value(dev, INDEX1, (uint8_t *)&zero, | |
e76cb927 | 195 | sizeof(uint32_t))); |
abdc7b8a | 196 | TPM_CHECK(tpm_set_global_lock(dev)); |
e76cb927 SG |
197 | /* Verifies that write to index0 fails */ |
198 | x = 1; | |
abdc7b8a | 199 | result = tpm_nv_write_value(dev, INDEX0, (uint8_t *)&x, sizeof(x)); |
e76cb927 | 200 | assert(result == TPM_AREA_LOCKED); |
abdc7b8a | 201 | TPM_CHECK(tpm_nv_read_value(dev, INDEX0, (uint8_t *)&x, sizeof(x))); |
e76cb927 SG |
202 | assert(x == 0); |
203 | /* Verifies that write to index1 is still possible */ | |
204 | x = 2; | |
abdc7b8a SG |
205 | TPM_CHECK(tpm_nv_write_value(dev, INDEX1, (uint8_t *)&x, sizeof(x))); |
206 | TPM_CHECK(tpm_nv_read_value(dev, INDEX1, (uint8_t *)&x, sizeof(x))); | |
e76cb927 SG |
207 | assert(x == 2); |
208 | /* Turns off PP */ | |
abdc7b8a | 209 | tpm_tsc_physical_presence(dev, PHYS_PRESENCE); |
e76cb927 SG |
210 | /* Verifies that write to index1 fails */ |
211 | x = 3; | |
abdc7b8a | 212 | result = tpm_nv_write_value(dev, INDEX1, (uint8_t *)&x, sizeof(x)); |
e76cb927 | 213 | assert(result == TPM_BAD_PRESENCE); |
abdc7b8a | 214 | TPM_CHECK(tpm_nv_read_value(dev, INDEX1, (uint8_t *)&x, sizeof(x))); |
e76cb927 SG |
215 | assert(x == 2); |
216 | printf("\tdone\n"); | |
217 | return 0; | |
218 | } | |
219 | ||
abdc7b8a | 220 | static int test_lock(struct udevice *dev) |
e76cb927 SG |
221 | { |
222 | printf("Testing lock ...\n"); | |
abdc7b8a SG |
223 | tpm_init(dev); |
224 | tpm_startup(dev, TPM_ST_CLEAR); | |
225 | tpm_self_test_full(dev); | |
226 | tpm_tsc_physical_presence(dev, PRESENCE); | |
227 | tpm_nv_write_value_lock(dev, INDEX0); | |
e76cb927 SG |
228 | printf("\tLocked 0x%x\n", INDEX0); |
229 | printf("\tdone\n"); | |
230 | return 0; | |
231 | } | |
232 | ||
abdc7b8a | 233 | static void initialise_spaces(struct udevice *dev) |
e76cb927 SG |
234 | { |
235 | uint32_t zero = 0; | |
236 | uint32_t perm = TPM_NV_PER_WRITE_STCLEAR | TPM_NV_PER_PPWRITE; | |
237 | ||
238 | printf("\tInitialising spaces\n"); | |
abdc7b8a SG |
239 | tpm_nv_set_locked(dev); /* useful only the first time */ |
240 | tpm_nv_define_space(dev, INDEX0, perm, 4); | |
241 | tpm_nv_write_value(dev, INDEX0, (uint8_t *)&zero, 4); | |
242 | tpm_nv_define_space(dev, INDEX1, perm, 4); | |
243 | tpm_nv_write_value(dev, INDEX1, (uint8_t *)&zero, 4); | |
244 | tpm_nv_define_space(dev, INDEX2, perm, 4); | |
245 | tpm_nv_write_value(dev, INDEX2, (uint8_t *)&zero, 4); | |
246 | tpm_nv_define_space(dev, INDEX3, perm, 4); | |
247 | tpm_nv_write_value(dev, INDEX3, (uint8_t *)&zero, 4); | |
e76cb927 SG |
248 | perm = TPM_NV_PER_READ_STCLEAR | TPM_NV_PER_WRITE_STCLEAR | |
249 | TPM_NV_PER_PPWRITE; | |
abdc7b8a | 250 | tpm_nv_define_space(dev, INDEX_INITIALISED, perm, 1); |
e76cb927 SG |
251 | } |
252 | ||
abdc7b8a | 253 | static int test_readonly(struct udevice *dev) |
e76cb927 SG |
254 | { |
255 | uint8_t c; | |
256 | uint32_t index_0, index_1, index_2, index_3; | |
257 | int read0, read1, read2, read3; | |
258 | ||
259 | printf("Testing readonly ...\n"); | |
abdc7b8a SG |
260 | tpm_init(dev); |
261 | tpm_startup(dev, TPM_ST_CLEAR); | |
262 | tpm_self_test_full(dev); | |
263 | tpm_tsc_physical_presence(dev, PRESENCE); | |
e76cb927 SG |
264 | /* |
265 | * Checks if initialisation has completed by trying to read-lock a | |
266 | * space that's created at the end of initialisation | |
267 | */ | |
abdc7b8a | 268 | if (tpm_nv_read_value(dev, INDEX_INITIALISED, &c, 0) == TPM_BADINDEX) { |
e76cb927 | 269 | /* The initialisation did not complete */ |
abdc7b8a | 270 | initialise_spaces(dev); |
e76cb927 SG |
271 | } |
272 | ||
273 | /* Checks if spaces are OK or messed up */ | |
abdc7b8a SG |
274 | read0 = tpm_nv_read_value(dev, INDEX0, (uint8_t *)&index_0, |
275 | sizeof(index_0)); | |
276 | read1 = tpm_nv_read_value(dev, INDEX1, (uint8_t *)&index_1, | |
277 | sizeof(index_1)); | |
278 | read2 = tpm_nv_read_value(dev, INDEX2, (uint8_t *)&index_2, | |
279 | sizeof(index_2)); | |
280 | read3 = tpm_nv_read_value(dev, INDEX3, (uint8_t *)&index_3, | |
281 | sizeof(index_3)); | |
e76cb927 SG |
282 | if (read0 || read1 || read2 || read3) { |
283 | printf("Invalid contents\n"); | |
284 | return 0; | |
285 | } | |
286 | ||
287 | /* | |
288 | * Writes space, and locks it. Then attempts to write again. | |
289 | * I really wish I could use the imperative. | |
290 | */ | |
291 | index_0 += 1; | |
abdc7b8a SG |
292 | if (tpm_nv_write_value(dev, INDEX0, (uint8_t *)&index_0, |
293 | sizeof(index_0) != | |
e76cb927 | 294 | TPM_SUCCESS)) { |
9b643e31 | 295 | pr_err("\tcould not write index 0\n"); |
e76cb927 | 296 | } |
abdc7b8a SG |
297 | tpm_nv_write_value_lock(dev, INDEX0); |
298 | if (tpm_nv_write_value(dev, INDEX0, (uint8_t *)&index_0, | |
299 | sizeof(index_0)) == | |
e76cb927 | 300 | TPM_SUCCESS) |
9b643e31 | 301 | pr_err("\tindex 0 is not locked\n"); |
e76cb927 SG |
302 | |
303 | printf("\tdone\n"); | |
304 | return 0; | |
305 | } | |
306 | ||
abdc7b8a | 307 | static int test_redefine_unowned(struct udevice *dev) |
e76cb927 SG |
308 | { |
309 | uint32_t perm; | |
310 | uint32_t result; | |
311 | uint32_t x; | |
312 | ||
313 | printf("Testing redefine_unowned ..."); | |
abdc7b8a SG |
314 | tpm_init(dev); |
315 | TPM_CHECK(TlclStartupIfNeeded(dev)); | |
316 | TPM_CHECK(tpm_self_test_full(dev)); | |
317 | TPM_CHECK(tpm_tsc_physical_presence(dev, PRESENCE)); | |
318 | assert(!tpm_is_owned(dev)); | |
e76cb927 SG |
319 | |
320 | /* Ensures spaces exist. */ | |
abdc7b8a SG |
321 | TPM_CHECK(tpm_nv_read_value(dev, INDEX0, (uint8_t *)&x, sizeof(x))); |
322 | TPM_CHECK(tpm_nv_read_value(dev, INDEX1, (uint8_t *)&x, sizeof(x))); | |
e76cb927 SG |
323 | |
324 | /* Redefines spaces a couple of times. */ | |
325 | perm = TPM_NV_PER_PPWRITE | TPM_NV_PER_GLOBALLOCK; | |
abdc7b8a SG |
326 | TPM_CHECK(tpm_nv_define_space(dev, INDEX0, perm, 2 * sizeof(uint32_t))); |
327 | TPM_CHECK(tpm_nv_define_space(dev, INDEX0, perm, sizeof(uint32_t))); | |
e76cb927 | 328 | perm = TPM_NV_PER_PPWRITE; |
abdc7b8a SG |
329 | TPM_CHECK(tpm_nv_define_space(dev, INDEX1, perm, 2 * sizeof(uint32_t))); |
330 | TPM_CHECK(tpm_nv_define_space(dev, INDEX1, perm, sizeof(uint32_t))); | |
e76cb927 SG |
331 | |
332 | /* Sets the global lock */ | |
abdc7b8a | 333 | tpm_set_global_lock(dev); |
e76cb927 SG |
334 | |
335 | /* Verifies that index0 cannot be redefined */ | |
abdc7b8a | 336 | result = tpm_nv_define_space(dev, INDEX0, perm, sizeof(uint32_t)); |
e76cb927 SG |
337 | assert(result == TPM_AREA_LOCKED); |
338 | ||
339 | /* Checks that index1 can */ | |
abdc7b8a SG |
340 | TPM_CHECK(tpm_nv_define_space(dev, INDEX1, perm, 2 * sizeof(uint32_t))); |
341 | TPM_CHECK(tpm_nv_define_space(dev, INDEX1, perm, sizeof(uint32_t))); | |
e76cb927 SG |
342 | |
343 | /* Turns off PP */ | |
abdc7b8a | 344 | tpm_tsc_physical_presence(dev, PHYS_PRESENCE); |
e76cb927 SG |
345 | |
346 | /* Verifies that neither index0 nor index1 can be redefined */ | |
abdc7b8a | 347 | result = tpm_nv_define_space(dev, INDEX0, perm, sizeof(uint32_t)); |
e76cb927 | 348 | assert(result == TPM_BAD_PRESENCE); |
abdc7b8a | 349 | result = tpm_nv_define_space(dev, INDEX1, perm, sizeof(uint32_t)); |
e76cb927 SG |
350 | assert(result == TPM_BAD_PRESENCE); |
351 | ||
352 | printf("done\n"); | |
353 | return 0; | |
354 | } | |
355 | ||
356 | #define PERMPPGL (TPM_NV_PER_PPWRITE | TPM_NV_PER_GLOBALLOCK) | |
357 | #define PERMPP TPM_NV_PER_PPWRITE | |
358 | ||
abdc7b8a | 359 | static int test_space_perm(struct udevice *dev) |
e76cb927 SG |
360 | { |
361 | uint32_t perm; | |
362 | ||
363 | printf("Testing spaceperm ..."); | |
abdc7b8a SG |
364 | tpm_init(dev); |
365 | TPM_CHECK(TlclStartupIfNeeded(dev)); | |
366 | TPM_CHECK(tpm_continue_self_test(dev)); | |
367 | TPM_CHECK(tpm_tsc_physical_presence(dev, PRESENCE)); | |
368 | TPM_CHECK(tpm_get_permissions(dev, INDEX0, &perm)); | |
e76cb927 | 369 | assert((perm & PERMPPGL) == PERMPPGL); |
abdc7b8a | 370 | TPM_CHECK(tpm_get_permissions(dev, INDEX1, &perm)); |
e76cb927 SG |
371 | assert((perm & PERMPP) == PERMPP); |
372 | printf("done\n"); | |
373 | return 0; | |
374 | } | |
375 | ||
abdc7b8a | 376 | static int test_startup(struct udevice *dev) |
e76cb927 SG |
377 | { |
378 | uint32_t result; | |
abdc7b8a | 379 | |
e76cb927 SG |
380 | printf("Testing startup ...\n"); |
381 | ||
abdc7b8a SG |
382 | tpm_init(dev); |
383 | result = tpm_startup(dev, TPM_ST_CLEAR); | |
e76cb927 SG |
384 | if (result != 0 && result != TPM_INVALID_POSTINIT) |
385 | printf("\ttpm startup failed with 0x%x\n", result); | |
abdc7b8a | 386 | result = tpm_get_flags(dev, NULL, NULL, NULL); |
e76cb927 SG |
387 | if (result != 0) |
388 | printf("\ttpm getflags failed with 0x%x\n", result); | |
389 | printf("\texecuting SelfTestFull\n"); | |
abdc7b8a SG |
390 | tpm_self_test_full(dev); |
391 | result = tpm_get_flags(dev, NULL, NULL, NULL); | |
e76cb927 SG |
392 | if (result != 0) |
393 | printf("\ttpm getflags failed with 0x%x\n", result); | |
394 | printf("\tdone\n"); | |
395 | return 0; | |
396 | } | |
397 | ||
398 | /* | |
399 | * Runs [op] and ensures it returns success and doesn't run longer than | |
400 | * [time_limit] in milliseconds. | |
401 | */ | |
402 | #define TTPM_CHECK(op, time_limit) do { \ | |
403 | ulong start, time; \ | |
404 | uint32_t __result; \ | |
405 | \ | |
406 | start = get_timer(0); \ | |
407 | __result = op; \ | |
408 | if (__result != TPM_SUCCESS) { \ | |
409 | printf("\t" #op ": error 0x%x\n", __result); \ | |
410 | return -1; \ | |
411 | } \ | |
412 | time = get_timer(start); \ | |
413 | printf("\t" #op ": %lu ms\n", time); \ | |
414 | if (time > (ulong)time_limit) { \ | |
415 | printf("\t" #op " exceeded " #time_limit " ms\n"); \ | |
416 | } \ | |
417 | } while (0) | |
418 | ||
419 | ||
abdc7b8a | 420 | static int test_timing(struct udevice *dev) |
e76cb927 | 421 | { |
e76cb927 | 422 | uint8_t in[20], out[20]; |
abdc7b8a | 423 | uint32_t x; |
e76cb927 SG |
424 | |
425 | printf("Testing timing ..."); | |
abdc7b8a SG |
426 | tpm_init(dev); |
427 | TTPM_CHECK(TlclStartupIfNeeded(dev), 50); | |
428 | TTPM_CHECK(tpm_continue_self_test(dev), 100); | |
429 | TTPM_CHECK(tpm_self_test_full(dev), 1000); | |
430 | TTPM_CHECK(tpm_tsc_physical_presence(dev, PRESENCE), 100); | |
431 | TTPM_CHECK(tpm_nv_write_value(dev, INDEX0, (uint8_t *)&x, sizeof(x)), | |
432 | 100); | |
433 | TTPM_CHECK(tpm_nv_read_value(dev, INDEX0, (uint8_t *)&x, sizeof(x)), | |
434 | 100); | |
435 | TTPM_CHECK(tpm_extend(dev, 0, in, out), 200); | |
436 | TTPM_CHECK(tpm_set_global_lock(dev), 50); | |
437 | TTPM_CHECK(tpm_tsc_physical_presence(dev, PHYS_PRESENCE), 100); | |
e76cb927 SG |
438 | printf("done\n"); |
439 | return 0; | |
440 | } | |
441 | ||
442 | #define TPM_MAX_NV_WRITES_NOOWNER 64 | |
443 | ||
abdc7b8a | 444 | static int test_write_limit(struct udevice *dev) |
e76cb927 | 445 | { |
e76cb927 | 446 | uint32_t result; |
abdc7b8a | 447 | int i; |
e76cb927 | 448 | |
abdc7b8a SG |
449 | printf("Testing writelimit ...\n"); |
450 | tpm_init(dev); | |
451 | TPM_CHECK(TlclStartupIfNeeded(dev)); | |
452 | TPM_CHECK(tpm_self_test_full(dev)); | |
453 | TPM_CHECK(tpm_tsc_physical_presence(dev, PRESENCE)); | |
454 | TPM_CHECK(tpm_force_clear(dev)); | |
455 | TPM_CHECK(tpm_physical_enable(dev)); | |
456 | TPM_CHECK(tpm_physical_set_deactivated(dev, 0)); | |
e76cb927 SG |
457 | |
458 | for (i = 0; i < TPM_MAX_NV_WRITES_NOOWNER + 2; i++) { | |
459 | printf("\twriting %d\n", i); | |
abdc7b8a SG |
460 | result = tpm_nv_write_value(dev, INDEX0, (uint8_t *)&i, |
461 | sizeof(i)); | |
e76cb927 SG |
462 | switch (result) { |
463 | case TPM_SUCCESS: | |
464 | break; | |
465 | case TPM_MAXNVWRITES: | |
466 | assert(i >= TPM_MAX_NV_WRITES_NOOWNER); | |
467 | default: | |
9b643e31 | 468 | pr_err("\tunexpected error code %d (0x%x)\n", |
e76cb927 SG |
469 | result, result); |
470 | } | |
471 | } | |
472 | ||
473 | /* Reset write count */ | |
abdc7b8a SG |
474 | TPM_CHECK(tpm_force_clear(dev)); |
475 | TPM_CHECK(tpm_physical_enable(dev)); | |
476 | TPM_CHECK(tpm_physical_set_deactivated(dev, 0)); | |
e76cb927 SG |
477 | |
478 | /* Try writing again. */ | |
abdc7b8a | 479 | TPM_CHECK(tpm_nv_write_value(dev, INDEX0, (uint8_t *)&i, sizeof(i))); |
e76cb927 SG |
480 | printf("\tdone\n"); |
481 | return 0; | |
482 | } | |
483 | ||
484 | #define VOIDTEST(XFUNC) \ | |
485 | int do_test_##XFUNC(cmd_tbl_t *cmd_tbl, int flag, int argc, \ | |
486 | char * const argv[]) \ | |
487 | { \ | |
abdc7b8a SG |
488 | struct udevice *dev; \ |
489 | int ret; \ | |
490 | \ | |
491 | ret = get_tpm(&dev); \ | |
492 | if (ret) \ | |
493 | return ret; \ | |
494 | return test_##XFUNC(dev); \ | |
e76cb927 SG |
495 | } |
496 | ||
497 | #define VOIDENT(XNAME) \ | |
498 | U_BOOT_CMD_MKENT(XNAME, 0, 1, do_test_##XNAME, "", ""), | |
499 | ||
500 | VOIDTEST(early_extend) | |
501 | VOIDTEST(early_nvram) | |
502 | VOIDTEST(early_nvram2) | |
503 | VOIDTEST(enable) | |
504 | VOIDTEST(fast_enable) | |
505 | VOIDTEST(global_lock) | |
506 | VOIDTEST(lock) | |
507 | VOIDTEST(readonly) | |
508 | VOIDTEST(redefine_unowned) | |
509 | VOIDTEST(space_perm) | |
510 | VOIDTEST(startup) | |
511 | VOIDTEST(timing) | |
512 | VOIDTEST(write_limit) | |
513 | VOIDTEST(timer) | |
514 | ||
515 | static cmd_tbl_t cmd_cros_tpm_sub[] = { | |
516 | VOIDENT(early_extend) | |
517 | VOIDENT(early_nvram) | |
518 | VOIDENT(early_nvram2) | |
519 | VOIDENT(enable) | |
520 | VOIDENT(fast_enable) | |
521 | VOIDENT(global_lock) | |
522 | VOIDENT(lock) | |
523 | VOIDENT(readonly) | |
524 | VOIDENT(redefine_unowned) | |
525 | VOIDENT(space_perm) | |
526 | VOIDENT(startup) | |
527 | VOIDENT(timing) | |
528 | VOIDENT(write_limit) | |
529 | VOIDENT(timer) | |
530 | }; | |
531 | ||
532 | static int do_tpmtest(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) | |
533 | { | |
534 | cmd_tbl_t *c; | |
0427b9c5 | 535 | int i; |
e76cb927 SG |
536 | |
537 | printf("argc = %d, argv = ", argc); | |
e76cb927 | 538 | |
0427b9c5 SB |
539 | for (i = 0; i < argc; i++) |
540 | printf(" %s", argv[i]); | |
541 | ||
542 | printf("\n------\n"); | |
543 | ||
e76cb927 SG |
544 | argc--; |
545 | argv++; | |
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); | |
549 | } | |
550 | ||
551 | U_BOOT_CMD(tpmtest, 2, 1, do_tpmtest, "TPM tests", | |
552 | "\n\tearly_extend\n" | |
553 | "\tearly_nvram\n" | |
554 | "\tearly_nvram2\n" | |
555 | "\tenable\n" | |
556 | "\tfast_enable\n" | |
557 | "\tglobal_lock\n" | |
558 | "\tlock\n" | |
559 | "\treadonly\n" | |
560 | "\tredefine_unowned\n" | |
561 | "\tspace_perm\n" | |
562 | "\tstartup\n" | |
563 | "\ttiming\n" | |
564 | "\twrite_limit\n"); |