xref: /openbmc/linux/drivers/char/tpm/tpm1-cmd.c (revision 433d390f005d3a89dba5a03a87b5a6c242748de3)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (C) 2004 IBM Corporation
4  * Copyright (C) 2014 Intel Corporation
5  *
6  * Authors:
7  * Leendert van Doorn <leendert@watson.ibm.com>
8  * Dave Safford <safford@watson.ibm.com>
9  * Reiner Sailer <sailer@watson.ibm.com>
10  * Kylene Hall <kjhall@us.ibm.com>
11  *
12  * Device driver for TCG/TCPA TPM (trusted platform module).
13  * Specifications at www.trustedcomputinggroup.org
14  */
15 
16 #include <linux/poll.h>
17 #include <linux/slab.h>
18 #include <linux/mutex.h>
19 #include <linux/spinlock.h>
20 #include <linux/freezer.h>
21 #include <linux/tpm_eventlog.h>
22 
23 #include "tpm.h"
24 
25 #define TPM_MAX_ORDINAL 243
26 
27 /*
28  * Array with one entry per ordinal defining the maximum amount
29  * of time the chip could take to return the result.  The ordinal
30  * designation of short, medium or long is defined in a table in
31  * TCG Specification TPM Main Part 2 TPM Structures Section 17. The
32  * values of the SHORT, MEDIUM, and LONG durations are retrieved
33  * from the chip during initialization with a call to tpm_get_timeouts.
34  */
35 static const u8 tpm1_ordinal_duration[TPM_MAX_ORDINAL] = {
36 	TPM_UNDEFINED,		/* 0 */
37 	TPM_UNDEFINED,
38 	TPM_UNDEFINED,
39 	TPM_UNDEFINED,
40 	TPM_UNDEFINED,
41 	TPM_UNDEFINED,		/* 5 */
42 	TPM_UNDEFINED,
43 	TPM_UNDEFINED,
44 	TPM_UNDEFINED,
45 	TPM_UNDEFINED,
46 	TPM_SHORT,		/* 10 */
47 	TPM_SHORT,
48 	TPM_MEDIUM,
49 	TPM_LONG,
50 	TPM_LONG,
51 	TPM_MEDIUM,		/* 15 */
52 	TPM_SHORT,
53 	TPM_SHORT,
54 	TPM_MEDIUM,
55 	TPM_LONG,
56 	TPM_SHORT,		/* 20 */
57 	TPM_SHORT,
58 	TPM_MEDIUM,
59 	TPM_MEDIUM,
60 	TPM_MEDIUM,
61 	TPM_SHORT,		/* 25 */
62 	TPM_SHORT,
63 	TPM_MEDIUM,
64 	TPM_SHORT,
65 	TPM_SHORT,
66 	TPM_MEDIUM,		/* 30 */
67 	TPM_LONG,
68 	TPM_MEDIUM,
69 	TPM_SHORT,
70 	TPM_SHORT,
71 	TPM_SHORT,		/* 35 */
72 	TPM_MEDIUM,
73 	TPM_MEDIUM,
74 	TPM_UNDEFINED,
75 	TPM_UNDEFINED,
76 	TPM_MEDIUM,		/* 40 */
77 	TPM_LONG,
78 	TPM_MEDIUM,
79 	TPM_SHORT,
80 	TPM_SHORT,
81 	TPM_SHORT,		/* 45 */
82 	TPM_SHORT,
83 	TPM_SHORT,
84 	TPM_SHORT,
85 	TPM_LONG,
86 	TPM_MEDIUM,		/* 50 */
87 	TPM_MEDIUM,
88 	TPM_UNDEFINED,
89 	TPM_UNDEFINED,
90 	TPM_UNDEFINED,
91 	TPM_UNDEFINED,		/* 55 */
92 	TPM_UNDEFINED,
93 	TPM_UNDEFINED,
94 	TPM_UNDEFINED,
95 	TPM_UNDEFINED,
96 	TPM_MEDIUM,		/* 60 */
97 	TPM_MEDIUM,
98 	TPM_MEDIUM,
99 	TPM_SHORT,
100 	TPM_SHORT,
101 	TPM_MEDIUM,		/* 65 */
102 	TPM_UNDEFINED,
103 	TPM_UNDEFINED,
104 	TPM_UNDEFINED,
105 	TPM_UNDEFINED,
106 	TPM_SHORT,		/* 70 */
107 	TPM_SHORT,
108 	TPM_UNDEFINED,
109 	TPM_UNDEFINED,
110 	TPM_UNDEFINED,
111 	TPM_UNDEFINED,		/* 75 */
112 	TPM_UNDEFINED,
113 	TPM_UNDEFINED,
114 	TPM_UNDEFINED,
115 	TPM_UNDEFINED,
116 	TPM_LONG,		/* 80 */
117 	TPM_UNDEFINED,
118 	TPM_MEDIUM,
119 	TPM_LONG,
120 	TPM_SHORT,
121 	TPM_UNDEFINED,		/* 85 */
122 	TPM_UNDEFINED,
123 	TPM_UNDEFINED,
124 	TPM_UNDEFINED,
125 	TPM_UNDEFINED,
126 	TPM_SHORT,		/* 90 */
127 	TPM_SHORT,
128 	TPM_SHORT,
129 	TPM_SHORT,
130 	TPM_SHORT,
131 	TPM_UNDEFINED,		/* 95 */
132 	TPM_UNDEFINED,
133 	TPM_UNDEFINED,
134 	TPM_UNDEFINED,
135 	TPM_UNDEFINED,
136 	TPM_MEDIUM,		/* 100 */
137 	TPM_SHORT,
138 	TPM_SHORT,
139 	TPM_UNDEFINED,
140 	TPM_UNDEFINED,
141 	TPM_UNDEFINED,		/* 105 */
142 	TPM_UNDEFINED,
143 	TPM_UNDEFINED,
144 	TPM_UNDEFINED,
145 	TPM_UNDEFINED,
146 	TPM_SHORT,		/* 110 */
147 	TPM_SHORT,
148 	TPM_SHORT,
149 	TPM_SHORT,
150 	TPM_SHORT,
151 	TPM_SHORT,		/* 115 */
152 	TPM_SHORT,
153 	TPM_SHORT,
154 	TPM_UNDEFINED,
155 	TPM_UNDEFINED,
156 	TPM_LONG,		/* 120 */
157 	TPM_LONG,
158 	TPM_MEDIUM,
159 	TPM_UNDEFINED,
160 	TPM_SHORT,
161 	TPM_SHORT,		/* 125 */
162 	TPM_SHORT,
163 	TPM_LONG,
164 	TPM_SHORT,
165 	TPM_SHORT,
166 	TPM_SHORT,		/* 130 */
167 	TPM_MEDIUM,
168 	TPM_UNDEFINED,
169 	TPM_SHORT,
170 	TPM_MEDIUM,
171 	TPM_UNDEFINED,		/* 135 */
172 	TPM_UNDEFINED,
173 	TPM_UNDEFINED,
174 	TPM_UNDEFINED,
175 	TPM_UNDEFINED,
176 	TPM_SHORT,		/* 140 */
177 	TPM_SHORT,
178 	TPM_UNDEFINED,
179 	TPM_UNDEFINED,
180 	TPM_UNDEFINED,
181 	TPM_UNDEFINED,		/* 145 */
182 	TPM_UNDEFINED,
183 	TPM_UNDEFINED,
184 	TPM_UNDEFINED,
185 	TPM_UNDEFINED,
186 	TPM_SHORT,		/* 150 */
187 	TPM_MEDIUM,
188 	TPM_MEDIUM,
189 	TPM_SHORT,
190 	TPM_SHORT,
191 	TPM_UNDEFINED,		/* 155 */
192 	TPM_UNDEFINED,
193 	TPM_UNDEFINED,
194 	TPM_UNDEFINED,
195 	TPM_UNDEFINED,
196 	TPM_SHORT,		/* 160 */
197 	TPM_SHORT,
198 	TPM_SHORT,
199 	TPM_SHORT,
200 	TPM_UNDEFINED,
201 	TPM_UNDEFINED,		/* 165 */
202 	TPM_UNDEFINED,
203 	TPM_UNDEFINED,
204 	TPM_UNDEFINED,
205 	TPM_UNDEFINED,
206 	TPM_LONG,		/* 170 */
207 	TPM_UNDEFINED,
208 	TPM_UNDEFINED,
209 	TPM_UNDEFINED,
210 	TPM_UNDEFINED,
211 	TPM_UNDEFINED,		/* 175 */
212 	TPM_UNDEFINED,
213 	TPM_UNDEFINED,
214 	TPM_UNDEFINED,
215 	TPM_UNDEFINED,
216 	TPM_MEDIUM,		/* 180 */
217 	TPM_SHORT,
218 	TPM_MEDIUM,
219 	TPM_MEDIUM,
220 	TPM_MEDIUM,
221 	TPM_MEDIUM,		/* 185 */
222 	TPM_SHORT,
223 	TPM_UNDEFINED,
224 	TPM_UNDEFINED,
225 	TPM_UNDEFINED,
226 	TPM_UNDEFINED,		/* 190 */
227 	TPM_UNDEFINED,
228 	TPM_UNDEFINED,
229 	TPM_UNDEFINED,
230 	TPM_UNDEFINED,
231 	TPM_UNDEFINED,		/* 195 */
232 	TPM_UNDEFINED,
233 	TPM_UNDEFINED,
234 	TPM_UNDEFINED,
235 	TPM_UNDEFINED,
236 	TPM_SHORT,		/* 200 */
237 	TPM_UNDEFINED,
238 	TPM_UNDEFINED,
239 	TPM_UNDEFINED,
240 	TPM_SHORT,
241 	TPM_SHORT,		/* 205 */
242 	TPM_SHORT,
243 	TPM_SHORT,
244 	TPM_SHORT,
245 	TPM_SHORT,
246 	TPM_MEDIUM,		/* 210 */
247 	TPM_UNDEFINED,
248 	TPM_MEDIUM,
249 	TPM_MEDIUM,
250 	TPM_MEDIUM,
251 	TPM_UNDEFINED,		/* 215 */
252 	TPM_MEDIUM,
253 	TPM_UNDEFINED,
254 	TPM_UNDEFINED,
255 	TPM_SHORT,
256 	TPM_SHORT,		/* 220 */
257 	TPM_SHORT,
258 	TPM_SHORT,
259 	TPM_SHORT,
260 	TPM_SHORT,
261 	TPM_UNDEFINED,		/* 225 */
262 	TPM_UNDEFINED,
263 	TPM_UNDEFINED,
264 	TPM_UNDEFINED,
265 	TPM_UNDEFINED,
266 	TPM_SHORT,		/* 230 */
267 	TPM_LONG,
268 	TPM_MEDIUM,
269 	TPM_UNDEFINED,
270 	TPM_UNDEFINED,
271 	TPM_UNDEFINED,		/* 235 */
272 	TPM_UNDEFINED,
273 	TPM_UNDEFINED,
274 	TPM_UNDEFINED,
275 	TPM_UNDEFINED,
276 	TPM_SHORT,		/* 240 */
277 	TPM_UNDEFINED,
278 	TPM_MEDIUM,
279 };
280 
281 /**
282  * tpm1_calc_ordinal_duration() - calculate the maximum command duration
283  * @chip:    TPM chip to use.
284  * @ordinal: TPM command ordinal.
285  *
286  * The function returns the maximum amount of time the chip could take
287  * to return the result for a particular ordinal in jiffies.
288  *
289  * Return: A maximal duration time for an ordinal in jiffies.
290  */
291 unsigned long tpm1_calc_ordinal_duration(struct tpm_chip *chip, u32 ordinal)
292 {
293 	int duration_idx = TPM_UNDEFINED;
294 	int duration = 0;
295 
296 	/*
297 	 * We only have a duration table for protected commands, where the upper
298 	 * 16 bits are 0. For the few other ordinals the fallback will be used.
299 	 */
300 	if (ordinal < TPM_MAX_ORDINAL)
301 		duration_idx = tpm1_ordinal_duration[ordinal];
302 
303 	if (duration_idx != TPM_UNDEFINED)
304 		duration = chip->duration[duration_idx];
305 	if (duration <= 0)
306 		return 2 * 60 * HZ;
307 	else
308 		return duration;
309 }
310 
311 int tpm1_get_timeouts(struct tpm_chip *chip)
312 {
313 	cap_t cap;
314 	unsigned long timeout_old[4], timeout_chip[4], timeout_eff[4];
315 	ssize_t rc;
316 
317 	rc = tpm1_getcap(chip, TPM_CAP_PROP_TIS_TIMEOUT, &cap, NULL,
318 			 sizeof(cap.timeout));
319 	if (rc == TPM_ERR_INVALID_POSTINIT) {
320 		if (tpm_startup(chip))
321 			return rc;
322 
323 		rc = tpm1_getcap(chip, TPM_CAP_PROP_TIS_TIMEOUT, &cap,
324 				 "attempting to determine the timeouts",
325 				 sizeof(cap.timeout));
326 	}
327 
328 	if (rc) {
329 		dev_err(&chip->dev, "A TPM error (%zd) occurred attempting to determine the timeouts\n",
330 			rc);
331 		return rc;
332 	}
333 
334 	timeout_old[0] = jiffies_to_usecs(chip->timeout_a);
335 	timeout_old[1] = jiffies_to_usecs(chip->timeout_b);
336 	timeout_old[2] = jiffies_to_usecs(chip->timeout_c);
337 	timeout_old[3] = jiffies_to_usecs(chip->timeout_d);
338 	timeout_chip[0] = be32_to_cpu(cap.timeout.a);
339 	timeout_chip[1] = be32_to_cpu(cap.timeout.b);
340 	timeout_chip[2] = be32_to_cpu(cap.timeout.c);
341 	timeout_chip[3] = be32_to_cpu(cap.timeout.d);
342 	memcpy(timeout_eff, timeout_chip, sizeof(timeout_eff));
343 
344 	/*
345 	 * Provide ability for vendor overrides of timeout values in case
346 	 * of misreporting.
347 	 */
348 	if (chip->ops->update_timeouts)
349 		chip->timeout_adjusted =
350 			chip->ops->update_timeouts(chip, timeout_eff);
351 
352 	if (!chip->timeout_adjusted) {
353 		/* Restore default if chip reported 0 */
354 		unsigned int i;
355 
356 		for (i = 0; i < ARRAY_SIZE(timeout_eff); i++) {
357 			if (timeout_eff[i])
358 				continue;
359 
360 			timeout_eff[i] = timeout_old[i];
361 			chip->timeout_adjusted = true;
362 		}
363 
364 		if (timeout_eff[0] != 0 && timeout_eff[0] < 1000) {
365 			/* timeouts in msec rather usec */
366 			for (i = 0; i != ARRAY_SIZE(timeout_eff); i++)
367 				timeout_eff[i] *= 1000;
368 			chip->timeout_adjusted = true;
369 		}
370 	}
371 
372 	/* Report adjusted timeouts */
373 	if (chip->timeout_adjusted) {
374 		dev_info(&chip->dev, HW_ERR "Adjusting reported timeouts: A %lu->%luus B %lu->%luus C %lu->%luus D %lu->%luus\n",
375 			 timeout_chip[0], timeout_eff[0],
376 			 timeout_chip[1], timeout_eff[1],
377 			 timeout_chip[2], timeout_eff[2],
378 			 timeout_chip[3], timeout_eff[3]);
379 	}
380 
381 	chip->timeout_a = usecs_to_jiffies(timeout_eff[0]);
382 	chip->timeout_b = usecs_to_jiffies(timeout_eff[1]);
383 	chip->timeout_c = usecs_to_jiffies(timeout_eff[2]);
384 	chip->timeout_d = usecs_to_jiffies(timeout_eff[3]);
385 
386 	rc = tpm1_getcap(chip, TPM_CAP_PROP_TIS_DURATION, &cap,
387 			 "attempting to determine the durations",
388 			  sizeof(cap.duration));
389 	if (rc)
390 		return rc;
391 
392 	chip->duration[TPM_SHORT] =
393 		usecs_to_jiffies(be32_to_cpu(cap.duration.tpm_short));
394 	chip->duration[TPM_MEDIUM] =
395 		usecs_to_jiffies(be32_to_cpu(cap.duration.tpm_medium));
396 	chip->duration[TPM_LONG] =
397 		usecs_to_jiffies(be32_to_cpu(cap.duration.tpm_long));
398 	chip->duration[TPM_LONG_LONG] = 0; /* not used under 1.2 */
399 
400 	/* The Broadcom BCM0102 chipset in a Dell Latitude D820 gets the above
401 	 * value wrong and apparently reports msecs rather than usecs. So we
402 	 * fix up the resulting too-small TPM_SHORT value to make things work.
403 	 * We also scale the TPM_MEDIUM and -_LONG values by 1000.
404 	 */
405 	if (chip->duration[TPM_SHORT] < (HZ / 100)) {
406 		chip->duration[TPM_SHORT] = HZ;
407 		chip->duration[TPM_MEDIUM] *= 1000;
408 		chip->duration[TPM_LONG] *= 1000;
409 		chip->duration_adjusted = true;
410 		dev_info(&chip->dev, "Adjusting TPM timeout parameters.");
411 	}
412 
413 	chip->flags |= TPM_CHIP_FLAG_HAVE_TIMEOUTS;
414 	return 0;
415 }
416 
417 #define TPM_ORD_PCR_EXTEND 20
418 int tpm1_pcr_extend(struct tpm_chip *chip, int pcr_idx, const u8 *hash,
419 		    const char *log_msg)
420 {
421 	struct tpm_buf buf;
422 	int rc;
423 
424 	rc = tpm_buf_init(&buf, TPM_TAG_RQU_COMMAND, TPM_ORD_PCR_EXTEND);
425 	if (rc)
426 		return rc;
427 
428 	tpm_buf_append_u32(&buf, pcr_idx);
429 	tpm_buf_append(&buf, hash, TPM_DIGEST_SIZE);
430 
431 	rc = tpm_transmit_cmd(chip, NULL, buf.data, PAGE_SIZE,
432 			      TPM_DIGEST_SIZE, 0, log_msg);
433 
434 	tpm_buf_destroy(&buf);
435 	return rc;
436 }
437 
438 #define TPM_ORD_GET_CAP 101
439 ssize_t tpm1_getcap(struct tpm_chip *chip, u32 subcap_id, cap_t *cap,
440 		    const char *desc, size_t min_cap_length)
441 {
442 	struct tpm_buf buf;
443 	int rc;
444 
445 	rc = tpm_buf_init(&buf, TPM_TAG_RQU_COMMAND, TPM_ORD_GET_CAP);
446 	if (rc)
447 		return rc;
448 
449 	if (subcap_id == TPM_CAP_VERSION_1_1 ||
450 	    subcap_id == TPM_CAP_VERSION_1_2) {
451 		tpm_buf_append_u32(&buf, subcap_id);
452 		tpm_buf_append_u32(&buf, 0);
453 	} else {
454 		if (subcap_id == TPM_CAP_FLAG_PERM ||
455 		    subcap_id == TPM_CAP_FLAG_VOL)
456 			tpm_buf_append_u32(&buf, TPM_CAP_FLAG);
457 		else
458 			tpm_buf_append_u32(&buf, TPM_CAP_PROP);
459 
460 		tpm_buf_append_u32(&buf, 4);
461 		tpm_buf_append_u32(&buf, subcap_id);
462 	}
463 	rc = tpm_transmit_cmd(chip, NULL, buf.data, PAGE_SIZE,
464 			      min_cap_length, 0, desc);
465 	if (!rc)
466 		*cap = *(cap_t *)&buf.data[TPM_HEADER_SIZE + 4];
467 
468 	tpm_buf_destroy(&buf);
469 	return rc;
470 }
471 EXPORT_SYMBOL_GPL(tpm1_getcap);
472 
473 #define TPM_ORD_GET_RANDOM 70
474 #define TPM_GETRANDOM_RESULT_SIZE	18
475 static const struct tpm_input_header tpm_getrandom_header = {
476 	.tag = cpu_to_be16(TPM_TAG_RQU_COMMAND),
477 	.length = cpu_to_be32(14),
478 	.ordinal = cpu_to_be32(TPM_ORD_GET_RANDOM)
479 };
480 
481 int tpm1_get_random(struct tpm_chip *chip, u8 *out, size_t max)
482 {
483 	struct tpm_cmd_t tpm_cmd;
484 	u32 recd;
485 	u32 num_bytes = min_t(u32, max, TPM_MAX_RNG_DATA);
486 	u32 rlength;
487 	int err, total = 0, retries = 5;
488 	u8 *dest = out;
489 
490 	if (!out || !num_bytes || max > TPM_MAX_RNG_DATA)
491 		return -EINVAL;
492 
493 	do {
494 		tpm_cmd.header.in = tpm_getrandom_header;
495 		tpm_cmd.params.getrandom_in.num_bytes = cpu_to_be32(num_bytes);
496 
497 		err = tpm_transmit_cmd(chip, NULL, &tpm_cmd,
498 				       TPM_GETRANDOM_RESULT_SIZE + num_bytes,
499 				       offsetof(struct tpm_getrandom_out,
500 						rng_data),
501 				       0, "attempting get random");
502 		if (err)
503 			break;
504 
505 		recd = be32_to_cpu(tpm_cmd.params.getrandom_out.rng_data_len);
506 		if (recd > num_bytes) {
507 			total = -EFAULT;
508 			break;
509 		}
510 
511 		rlength = be32_to_cpu(tpm_cmd.header.out.length);
512 		if (rlength < TPM_HEADER_SIZE +
513 			      offsetof(struct tpm_getrandom_out, rng_data) +
514 			      recd) {
515 			total = -EFAULT;
516 			break;
517 		}
518 		memcpy(dest, tpm_cmd.params.getrandom_out.rng_data, recd);
519 
520 		dest += recd;
521 		total += recd;
522 		num_bytes -= recd;
523 	} while (retries-- && (size_t)total < max);
524 
525 	return total ? total : -EIO;
526 }
527