xref: /openbmc/linux/drivers/char/tpm/tpm.h (revision 5b628549)
1 /*
2  * Copyright (C) 2004 IBM Corporation
3  * Copyright (C) 2015 Intel Corporation
4  *
5  * Authors:
6  * Leendert van Doorn <leendert@watson.ibm.com>
7  * Dave Safford <safford@watson.ibm.com>
8  * Reiner Sailer <sailer@watson.ibm.com>
9  * Kylene Hall <kjhall@us.ibm.com>
10  *
11  * Maintained by: <tpmdd-devel@lists.sourceforge.net>
12  *
13  * Device driver for TCG/TCPA TPM (trusted platform module).
14  * Specifications at www.trustedcomputinggroup.org
15  *
16  * This program is free software; you can redistribute it and/or
17  * modify it under the terms of the GNU General Public License as
18  * published by the Free Software Foundation, version 2 of the
19  * License.
20  *
21  */
22 
23 #ifndef __TPM_H__
24 #define __TPM_H__
25 
26 #include <linux/module.h>
27 #include <linux/delay.h>
28 #include <linux/mutex.h>
29 #include <linux/sched.h>
30 #include <linux/platform_device.h>
31 #include <linux/io.h>
32 #include <linux/tpm.h>
33 #include <linux/highmem.h>
34 #include <linux/tpm_eventlog.h>
35 
36 #ifdef CONFIG_X86
37 #include <asm/intel-family.h>
38 #endif
39 
40 #define TPM_MINOR		224	/* officially assigned */
41 #define TPM_BUFSIZE		4096
42 #define TPM_NUM_DEVICES		65536
43 #define TPM_RETRY		50
44 
45 enum tpm_timeout {
46 	TPM_TIMEOUT = 5,	/* msecs */
47 	TPM_TIMEOUT_RETRY = 100, /* msecs */
48 	TPM_TIMEOUT_RANGE_US = 300,	/* usecs */
49 	TPM_TIMEOUT_POLL = 1,	/* msecs */
50 	TPM_TIMEOUT_USECS_MIN = 100,      /* usecs */
51 	TPM_TIMEOUT_USECS_MAX = 500      /* usecs */
52 };
53 
54 /* TPM addresses */
55 enum tpm_addr {
56 	TPM_SUPERIO_ADDR = 0x2E,
57 	TPM_ADDR = 0x4E,
58 };
59 
60 #define TPM_WARN_RETRY          0x800
61 #define TPM_WARN_DOING_SELFTEST 0x802
62 #define TPM_ERR_DEACTIVATED     0x6
63 #define TPM_ERR_DISABLED        0x7
64 #define TPM_ERR_INVALID_POSTINIT 38
65 
66 #define TPM_HEADER_SIZE		10
67 
68 enum tpm2_const {
69 	TPM2_PLATFORM_PCR       =     24,
70 	TPM2_PCR_SELECT_MIN     = ((TPM2_PLATFORM_PCR + 7) / 8),
71 };
72 
73 enum tpm2_timeouts {
74 	TPM2_TIMEOUT_A          =    750,
75 	TPM2_TIMEOUT_B          =   2000,
76 	TPM2_TIMEOUT_C          =    200,
77 	TPM2_TIMEOUT_D          =     30,
78 	TPM2_DURATION_SHORT     =     20,
79 	TPM2_DURATION_MEDIUM    =    750,
80 	TPM2_DURATION_LONG      =   2000,
81 	TPM2_DURATION_LONG_LONG = 300000,
82 	TPM2_DURATION_DEFAULT   = 120000,
83 };
84 
85 enum tpm2_structures {
86 	TPM2_ST_NO_SESSIONS	= 0x8001,
87 	TPM2_ST_SESSIONS	= 0x8002,
88 };
89 
90 /* Indicates from what layer of the software stack the error comes from */
91 #define TSS2_RC_LAYER_SHIFT	 16
92 #define TSS2_RESMGR_TPM_RC_LAYER (11 << TSS2_RC_LAYER_SHIFT)
93 
94 enum tpm2_return_codes {
95 	TPM2_RC_SUCCESS		= 0x0000,
96 	TPM2_RC_HASH		= 0x0083, /* RC_FMT1 */
97 	TPM2_RC_HANDLE		= 0x008B,
98 	TPM2_RC_INITIALIZE	= 0x0100, /* RC_VER1 */
99 	TPM2_RC_FAILURE		= 0x0101,
100 	TPM2_RC_DISABLED	= 0x0120,
101 	TPM2_RC_COMMAND_CODE    = 0x0143,
102 	TPM2_RC_TESTING		= 0x090A, /* RC_WARN */
103 	TPM2_RC_REFERENCE_H0	= 0x0910,
104 	TPM2_RC_RETRY		= 0x0922,
105 };
106 
107 enum tpm2_command_codes {
108 	TPM2_CC_FIRST		        = 0x011F,
109 	TPM2_CC_HIERARCHY_CONTROL       = 0x0121,
110 	TPM2_CC_HIERARCHY_CHANGE_AUTH   = 0x0129,
111 	TPM2_CC_CREATE_PRIMARY          = 0x0131,
112 	TPM2_CC_SEQUENCE_COMPLETE       = 0x013E,
113 	TPM2_CC_SELF_TEST	        = 0x0143,
114 	TPM2_CC_STARTUP		        = 0x0144,
115 	TPM2_CC_SHUTDOWN	        = 0x0145,
116 	TPM2_CC_NV_READ                 = 0x014E,
117 	TPM2_CC_CREATE		        = 0x0153,
118 	TPM2_CC_LOAD		        = 0x0157,
119 	TPM2_CC_SEQUENCE_UPDATE         = 0x015C,
120 	TPM2_CC_UNSEAL		        = 0x015E,
121 	TPM2_CC_CONTEXT_LOAD	        = 0x0161,
122 	TPM2_CC_CONTEXT_SAVE	        = 0x0162,
123 	TPM2_CC_FLUSH_CONTEXT	        = 0x0165,
124 	TPM2_CC_VERIFY_SIGNATURE        = 0x0177,
125 	TPM2_CC_GET_CAPABILITY	        = 0x017A,
126 	TPM2_CC_GET_RANDOM	        = 0x017B,
127 	TPM2_CC_PCR_READ	        = 0x017E,
128 	TPM2_CC_PCR_EXTEND	        = 0x0182,
129 	TPM2_CC_EVENT_SEQUENCE_COMPLETE = 0x0185,
130 	TPM2_CC_HASH_SEQUENCE_START     = 0x0186,
131 	TPM2_CC_CREATE_LOADED           = 0x0191,
132 	TPM2_CC_LAST		        = 0x0193, /* Spec 1.36 */
133 };
134 
135 enum tpm2_permanent_handles {
136 	TPM2_RS_PW		= 0x40000009,
137 };
138 
139 enum tpm2_capabilities {
140 	TPM2_CAP_HANDLES	= 1,
141 	TPM2_CAP_COMMANDS	= 2,
142 	TPM2_CAP_PCRS		= 5,
143 	TPM2_CAP_TPM_PROPERTIES = 6,
144 };
145 
146 enum tpm2_properties {
147 	TPM_PT_TOTAL_COMMANDS	= 0x0129,
148 };
149 
150 enum tpm2_startup_types {
151 	TPM2_SU_CLEAR	= 0x0000,
152 	TPM2_SU_STATE	= 0x0001,
153 };
154 
155 enum tpm2_cc_attrs {
156 	TPM2_CC_ATTR_CHANDLES	= 25,
157 	TPM2_CC_ATTR_RHANDLE	= 28,
158 };
159 
160 #define TPM_VID_INTEL    0x8086
161 #define TPM_VID_WINBOND  0x1050
162 #define TPM_VID_STM      0x104A
163 
164 enum tpm_chip_flags {
165 	TPM_CHIP_FLAG_TPM2		= BIT(1),
166 	TPM_CHIP_FLAG_IRQ		= BIT(2),
167 	TPM_CHIP_FLAG_VIRTUAL		= BIT(3),
168 	TPM_CHIP_FLAG_HAVE_TIMEOUTS	= BIT(4),
169 	TPM_CHIP_FLAG_ALWAYS_POWERED	= BIT(5),
170 };
171 
172 #define to_tpm_chip(d) container_of(d, struct tpm_chip, dev)
173 
174 struct tpm_header {
175 	__be16 tag;
176 	__be32 length;
177 	union {
178 		__be32 ordinal;
179 		__be32 return_code;
180 	};
181 } __packed;
182 
183 #define TPM_TAG_RQU_COMMAND 193
184 
185 struct	stclear_flags_t {
186 	__be16	tag;
187 	u8	deactivated;
188 	u8	disableForceClear;
189 	u8	physicalPresence;
190 	u8	physicalPresenceLock;
191 	u8	bGlobalLock;
192 } __packed;
193 
194 struct	tpm_version_t {
195 	u8	Major;
196 	u8	Minor;
197 	u8	revMajor;
198 	u8	revMinor;
199 } __packed;
200 
201 struct	tpm_version_1_2_t {
202 	__be16	tag;
203 	u8	Major;
204 	u8	Minor;
205 	u8	revMajor;
206 	u8	revMinor;
207 } __packed;
208 
209 struct	timeout_t {
210 	__be32	a;
211 	__be32	b;
212 	__be32	c;
213 	__be32	d;
214 } __packed;
215 
216 struct duration_t {
217 	__be32	tpm_short;
218 	__be32	tpm_medium;
219 	__be32	tpm_long;
220 } __packed;
221 
222 struct permanent_flags_t {
223 	__be16	tag;
224 	u8	disable;
225 	u8	ownership;
226 	u8	deactivated;
227 	u8	readPubek;
228 	u8	disableOwnerClear;
229 	u8	allowMaintenance;
230 	u8	physicalPresenceLifetimeLock;
231 	u8	physicalPresenceHWEnable;
232 	u8	physicalPresenceCMDEnable;
233 	u8	CEKPUsed;
234 	u8	TPMpost;
235 	u8	TPMpostLock;
236 	u8	FIPS;
237 	u8	operator;
238 	u8	enableRevokeEK;
239 	u8	nvLocked;
240 	u8	readSRKPub;
241 	u8	tpmEstablished;
242 	u8	maintenanceDone;
243 	u8	disableFullDALogicInfo;
244 } __packed;
245 
246 typedef union {
247 	struct	permanent_flags_t perm_flags;
248 	struct	stclear_flags_t	stclear_flags;
249 	__u8	owned;
250 	__be32	num_pcrs;
251 	struct	tpm_version_t	tpm_version;
252 	struct	tpm_version_1_2_t tpm_version_1_2;
253 	__be32	manufacturer_id;
254 	struct timeout_t  timeout;
255 	struct duration_t duration;
256 } cap_t;
257 
258 enum tpm_capabilities {
259 	TPM_CAP_FLAG = 4,
260 	TPM_CAP_PROP = 5,
261 	TPM_CAP_VERSION_1_1 = 0x06,
262 	TPM_CAP_VERSION_1_2 = 0x1A,
263 };
264 
265 enum tpm_sub_capabilities {
266 	TPM_CAP_PROP_PCR = 0x101,
267 	TPM_CAP_PROP_MANUFACTURER = 0x103,
268 	TPM_CAP_FLAG_PERM = 0x108,
269 	TPM_CAP_FLAG_VOL = 0x109,
270 	TPM_CAP_PROP_OWNER = 0x111,
271 	TPM_CAP_PROP_TIS_TIMEOUT = 0x115,
272 	TPM_CAP_PROP_TIS_DURATION = 0x120,
273 };
274 
275 
276 /* 128 bytes is an arbitrary cap. This could be as large as TPM_BUFSIZE - 18
277  * bytes, but 128 is still a relatively large number of random bytes and
278  * anything much bigger causes users of struct tpm_cmd_t to start getting
279  * compiler warnings about stack frame size. */
280 #define TPM_MAX_RNG_DATA	128
281 
282 /* A string buffer type for constructing TPM commands. This is based on the
283  * ideas of string buffer code in security/keys/trusted.h but is heap based
284  * in order to keep the stack usage minimal.
285  */
286 
287 enum tpm_buf_flags {
288 	TPM_BUF_OVERFLOW	= BIT(0),
289 };
290 
291 struct tpm_buf {
292 	struct page *data_page;
293 	unsigned int flags;
294 	u8 *data;
295 };
296 
297 static inline void tpm_buf_reset(struct tpm_buf *buf, u16 tag, u32 ordinal)
298 {
299 	struct tpm_header *head = (struct tpm_header *)buf->data;
300 
301 	head->tag = cpu_to_be16(tag);
302 	head->length = cpu_to_be32(sizeof(*head));
303 	head->ordinal = cpu_to_be32(ordinal);
304 }
305 
306 static inline int tpm_buf_init(struct tpm_buf *buf, u16 tag, u32 ordinal)
307 {
308 	buf->data_page = alloc_page(GFP_HIGHUSER);
309 	if (!buf->data_page)
310 		return -ENOMEM;
311 
312 	buf->flags = 0;
313 	buf->data = kmap(buf->data_page);
314 	tpm_buf_reset(buf, tag, ordinal);
315 	return 0;
316 }
317 
318 static inline void tpm_buf_destroy(struct tpm_buf *buf)
319 {
320 	kunmap(buf->data_page);
321 	__free_page(buf->data_page);
322 }
323 
324 static inline u32 tpm_buf_length(struct tpm_buf *buf)
325 {
326 	struct tpm_header *head = (struct tpm_header *)buf->data;
327 
328 	return be32_to_cpu(head->length);
329 }
330 
331 static inline u16 tpm_buf_tag(struct tpm_buf *buf)
332 {
333 	struct tpm_header *head = (struct tpm_header *)buf->data;
334 
335 	return be16_to_cpu(head->tag);
336 }
337 
338 static inline void tpm_buf_append(struct tpm_buf *buf,
339 				  const unsigned char *new_data,
340 				  unsigned int new_len)
341 {
342 	struct tpm_header *head = (struct tpm_header *)buf->data;
343 	u32 len = tpm_buf_length(buf);
344 
345 	/* Return silently if overflow has already happened. */
346 	if (buf->flags & TPM_BUF_OVERFLOW)
347 		return;
348 
349 	if ((len + new_len) > PAGE_SIZE) {
350 		WARN(1, "tpm_buf: overflow\n");
351 		buf->flags |= TPM_BUF_OVERFLOW;
352 		return;
353 	}
354 
355 	memcpy(&buf->data[len], new_data, new_len);
356 	head->length = cpu_to_be32(len + new_len);
357 }
358 
359 static inline void tpm_buf_append_u8(struct tpm_buf *buf, const u8 value)
360 {
361 	tpm_buf_append(buf, &value, 1);
362 }
363 
364 static inline void tpm_buf_append_u16(struct tpm_buf *buf, const u16 value)
365 {
366 	__be16 value2 = cpu_to_be16(value);
367 
368 	tpm_buf_append(buf, (u8 *) &value2, 2);
369 }
370 
371 static inline void tpm_buf_append_u32(struct tpm_buf *buf, const u32 value)
372 {
373 	__be32 value2 = cpu_to_be32(value);
374 
375 	tpm_buf_append(buf, (u8 *) &value2, 4);
376 }
377 
378 extern struct class *tpm_class;
379 extern struct class *tpmrm_class;
380 extern dev_t tpm_devt;
381 extern const struct file_operations tpm_fops;
382 extern const struct file_operations tpmrm_fops;
383 extern struct idr dev_nums_idr;
384 
385 ssize_t tpm_transmit(struct tpm_chip *chip, u8 *buf, size_t bufsiz);
386 ssize_t tpm_transmit_cmd(struct tpm_chip *chip, struct tpm_buf *buf,
387 			 size_t min_rsp_body_length, const char *desc);
388 int tpm_get_timeouts(struct tpm_chip *);
389 int tpm_auto_startup(struct tpm_chip *chip);
390 
391 int tpm1_pm_suspend(struct tpm_chip *chip, u32 tpm_suspend_pcr);
392 int tpm1_auto_startup(struct tpm_chip *chip);
393 int tpm1_do_selftest(struct tpm_chip *chip);
394 int tpm1_get_timeouts(struct tpm_chip *chip);
395 unsigned long tpm1_calc_ordinal_duration(struct tpm_chip *chip, u32 ordinal);
396 int tpm1_pcr_extend(struct tpm_chip *chip, u32 pcr_idx, const u8 *hash,
397 		    const char *log_msg);
398 int tpm1_pcr_read(struct tpm_chip *chip, u32 pcr_idx, u8 *res_buf);
399 ssize_t tpm1_getcap(struct tpm_chip *chip, u32 subcap_id, cap_t *cap,
400 		    const char *desc, size_t min_cap_length);
401 int tpm1_get_random(struct tpm_chip *chip, u8 *out, size_t max);
402 unsigned long tpm_calc_ordinal_duration(struct tpm_chip *chip, u32 ordinal);
403 int tpm_pm_suspend(struct device *dev);
404 int tpm_pm_resume(struct device *dev);
405 
406 static inline void tpm_msleep(unsigned int delay_msec)
407 {
408 	usleep_range((delay_msec * 1000) - TPM_TIMEOUT_RANGE_US,
409 		     delay_msec * 1000);
410 };
411 
412 int tpm_chip_start(struct tpm_chip *chip);
413 void tpm_chip_stop(struct tpm_chip *chip);
414 struct tpm_chip *tpm_find_get_ops(struct tpm_chip *chip);
415 __must_check int tpm_try_get_ops(struct tpm_chip *chip);
416 void tpm_put_ops(struct tpm_chip *chip);
417 
418 struct tpm_chip *tpm_chip_alloc(struct device *dev,
419 				const struct tpm_class_ops *ops);
420 struct tpm_chip *tpmm_chip_alloc(struct device *pdev,
421 				 const struct tpm_class_ops *ops);
422 int tpm_chip_register(struct tpm_chip *chip);
423 void tpm_chip_unregister(struct tpm_chip *chip);
424 
425 void tpm_sysfs_add_device(struct tpm_chip *chip);
426 
427 
428 #ifdef CONFIG_ACPI
429 extern void tpm_add_ppi(struct tpm_chip *chip);
430 #else
431 static inline void tpm_add_ppi(struct tpm_chip *chip)
432 {
433 }
434 #endif
435 
436 static inline u32 tpm2_rc_value(u32 rc)
437 {
438 	return (rc & BIT(7)) ? rc & 0xff : rc;
439 }
440 
441 int tpm2_get_timeouts(struct tpm_chip *chip);
442 int tpm2_pcr_read(struct tpm_chip *chip, u32 pcr_idx,
443 		  struct tpm_digest *digest, u16 *digest_size_ptr);
444 int tpm2_pcr_extend(struct tpm_chip *chip, u32 pcr_idx,
445 		    struct tpm_digest *digests);
446 int tpm2_get_random(struct tpm_chip *chip, u8 *dest, size_t max);
447 void tpm2_flush_context(struct tpm_chip *chip, u32 handle);
448 int tpm2_seal_trusted(struct tpm_chip *chip,
449 		      struct trusted_key_payload *payload,
450 		      struct trusted_key_options *options);
451 int tpm2_unseal_trusted(struct tpm_chip *chip,
452 			struct trusted_key_payload *payload,
453 			struct trusted_key_options *options);
454 ssize_t tpm2_get_tpm_pt(struct tpm_chip *chip, u32 property_id,
455 			u32 *value, const char *desc);
456 
457 int tpm2_auto_startup(struct tpm_chip *chip);
458 void tpm2_shutdown(struct tpm_chip *chip, u16 shutdown_type);
459 unsigned long tpm2_calc_ordinal_duration(struct tpm_chip *chip, u32 ordinal);
460 int tpm2_probe(struct tpm_chip *chip);
461 int tpm2_find_cc(struct tpm_chip *chip, u32 cc);
462 int tpm2_init_space(struct tpm_space *space);
463 void tpm2_del_space(struct tpm_chip *chip, struct tpm_space *space);
464 void tpm2_flush_space(struct tpm_chip *chip);
465 int tpm2_prepare_space(struct tpm_chip *chip, struct tpm_space *space, u8 *cmd,
466 		       size_t cmdsiz);
467 int tpm2_commit_space(struct tpm_chip *chip, struct tpm_space *space, void *buf,
468 		      size_t *bufsiz);
469 
470 int tpm_bios_log_setup(struct tpm_chip *chip);
471 void tpm_bios_log_teardown(struct tpm_chip *chip);
472 int tpm_dev_common_init(void);
473 void tpm_dev_common_exit(void);
474 #endif
475