]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blame - drivers/char/tpm/tpm.h
KEYS: trusted: explicitly use tpm_chip structure from tpm_default_chip()
[mirror_ubuntu-jammy-kernel.git] / drivers / char / tpm / tpm.h
CommitLineData
1da177e4
LT
1/*
2 * Copyright (C) 2004 IBM Corporation
a74f8b36 3 * Copyright (C) 2015 Intel Corporation
1da177e4
LT
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 *
8e81cc13 11 * Maintained by: <tpmdd-devel@lists.sourceforge.net>
1da177e4
LT
12 *
13 * Device driver for TCG/TCPA TPM (trusted platform module).
3b09825d 14 * Specifications at www.trustedcomputinggroup.org
1da177e4
LT
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.
3b09825d 20 *
1da177e4 21 */
d2efee6e
CR
22
23#ifndef __TPM_H__
24#define __TPM_H__
25
1da177e4 26#include <linux/module.h>
1da177e4 27#include <linux/delay.h>
d081d470 28#include <linux/mutex.h>
914e2637 29#include <linux/sched.h>
bbc5b212 30#include <linux/platform_device.h>
276ad0c1 31#include <linux/io.h>
659aaf2b 32#include <linux/tpm.h>
a74f8b36 33#include <linux/highmem.h>
fd3ec366 34#include <linux/tpm_eventlog.h>
1da177e4 35
5e572cab
AS
36#ifdef CONFIG_X86
37#include <asm/intel-family.h>
38#endif
39
901615cb
RS
40#define TPM_MINOR 224 /* officially assigned */
41#define TPM_BUFSIZE 4096
42#define TPM_NUM_DEVICES 65536
43#define TPM_RETRY 50
41ab999c 44
3122a88a
KH
45enum tpm_timeout {
46 TPM_TIMEOUT = 5, /* msecs */
9f3fc7bc 47 TPM_TIMEOUT_RETRY = 100, /* msecs */
59f5a6b0 48 TPM_TIMEOUT_RANGE_US = 300, /* usecs */
424eaf91
NJ
49 TPM_TIMEOUT_POLL = 1, /* msecs */
50 TPM_TIMEOUT_USECS_MIN = 100, /* usecs */
51 TPM_TIMEOUT_USECS_MAX = 500 /* usecs */
3122a88a 52};
1da177e4
LT
53
54/* TPM addresses */
3122a88a 55enum tpm_addr {
daacdfa6 56 TPM_SUPERIO_ADDR = 0x2E,
3122a88a 57 TPM_ADDR = 0x4E,
3122a88a
KH
58};
59
32d33b29 60#define TPM_WARN_RETRY 0x800
68d6e671 61#define TPM_WARN_DOING_SELFTEST 0x802
be405411
SB
62#define TPM_ERR_DEACTIVATED 0x6
63#define TPM_ERR_DISABLED 0x7
c584af19 64#define TPM_ERR_INVALID_POSTINIT 38
be405411 65
b9e3238a 66#define TPM_HEADER_SIZE 10
7a1d7e6d
JS
67
68enum tpm2_const {
076d3564
TW
69 TPM2_PLATFORM_PCR = 24,
70 TPM2_PCR_SELECT_MIN = ((TPM2_PLATFORM_PCR + 7) / 8),
71};
72
73enum 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,
7a1d7e6d
JS
83};
84
85enum tpm2_structures {
86 TPM2_ST_NO_SESSIONS = 0x8001,
87 TPM2_ST_SESSIONS = 0x8002,
88};
89
095531f8
JMC
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
7a1d7e6d 94enum tpm2_return_codes {
745b361e 95 TPM2_RC_SUCCESS = 0x0000,
5ca4c20c 96 TPM2_RC_HASH = 0x0083, /* RC_FMT1 */
745b361e 97 TPM2_RC_HANDLE = 0x008B,
5ca4c20c 98 TPM2_RC_INITIALIZE = 0x0100, /* RC_VER1 */
2be8ffed 99 TPM2_RC_FAILURE = 0x0101,
7a1d7e6d 100 TPM2_RC_DISABLED = 0x0120,
095531f8 101 TPM2_RC_COMMAND_CODE = 0x0143,
5ca4c20c 102 TPM2_RC_TESTING = 0x090A, /* RC_WARN */
745b361e 103 TPM2_RC_REFERENCE_H0 = 0x0910,
e2fb992d 104 TPM2_RC_RETRY = 0x0922,
7a1d7e6d
JS
105};
106
7a1d7e6d 107enum tpm2_command_codes {
899102bc
TW
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 */
7a1d7e6d
JS
133};
134
135enum tpm2_permanent_handles {
136 TPM2_RS_PW = 0x40000009,
137};
138
139enum tpm2_capabilities {
745b361e 140 TPM2_CAP_HANDLES = 1,
58472f5c 141 TPM2_CAP_COMMANDS = 2,
1db15344 142 TPM2_CAP_PCRS = 5,
7a1d7e6d
JS
143 TPM2_CAP_TPM_PROPERTIES = 6,
144};
145
58472f5c
JS
146enum tpm2_properties {
147 TPM_PT_TOTAL_COMMANDS = 0x0129,
148};
149
7a1d7e6d
JS
150enum tpm2_startup_types {
151 TPM2_SU_CLEAR = 0x0000,
152 TPM2_SU_STATE = 0x0001,
153};
154
58472f5c
JS
155enum tpm2_cc_attrs {
156 TPM2_CC_ATTR_CHANDLES = 25,
157 TPM2_CC_ATTR_RHANDLE = 28,
158};
159
4e401fb0 160#define TPM_VID_INTEL 0x8086
1f866057
SB
161#define TPM_VID_WINBOND 0x1050
162#define TPM_VID_STM 0x104A
4e401fb0 163
afb5abc2 164enum tpm_chip_flags {
9b774d5c 165 TPM_CHIP_FLAG_TPM2 = BIT(1),
570a3609 166 TPM_CHIP_FLAG_IRQ = BIT(2),
2f9f5377 167 TPM_CHIP_FLAG_VIRTUAL = BIT(3),
d1d253cf 168 TPM_CHIP_FLAG_HAVE_TIMEOUTS = BIT(4),
b5d0ebc9 169 TPM_CHIP_FLAG_ALWAYS_POWERED = BIT(5),
afb5abc2
JS
170};
171
9b774d5c 172#define to_tpm_chip(d) container_of(d, struct tpm_chip, dev)
27084efe 173
b34b77a9
JS
174struct tpm_header {
175 __be16 tag;
176 __be32 length;
177 union {
178 __be32 ordinal;
179 __be32 return_code;
180 };
348df8db 181} __packed;
08837438 182
06e93279 183#define TPM_TAG_RQU_COMMAND 193
000a07b0 184
08837438
RA
185struct stclear_flags_t {
186 __be16 tag;
187 u8 deactivated;
188 u8 disableForceClear;
189 u8 physicalPresence;
190 u8 physicalPresenceLock;
191 u8 bGlobalLock;
348df8db 192} __packed;
08837438
RA
193
194struct tpm_version_t {
195 u8 Major;
196 u8 Minor;
197 u8 revMajor;
198 u8 revMinor;
348df8db 199} __packed;
08837438
RA
200
201struct tpm_version_1_2_t {
202 __be16 tag;
203 u8 Major;
204 u8 Minor;
205 u8 revMajor;
206 u8 revMinor;
348df8db 207} __packed;
08837438
RA
208
209struct timeout_t {
210 __be32 a;
211 __be32 b;
212 __be32 c;
213 __be32 d;
348df8db 214} __packed;
08837438
RA
215
216struct duration_t {
217 __be32 tpm_short;
218 __be32 tpm_medium;
219 __be32 tpm_long;
348df8db 220} __packed;
08837438
RA
221
222struct 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;
348df8db 244} __packed;
08837438
RA
245
246typedef union {
247 struct permanent_flags_t perm_flags;
248 struct stclear_flags_t stclear_flags;
a118cf2a 249 __u8 owned;
08837438
RA
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
000a07b0 258enum tpm_capabilities {
84fda152
JS
259 TPM_CAP_FLAG = 4,
260 TPM_CAP_PROP = 5,
261 TPM_CAP_VERSION_1_1 = 0x06,
262 TPM_CAP_VERSION_1_2 = 0x1A,
000a07b0
JG
263};
264
265enum tpm_sub_capabilities {
84fda152
JS
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,
000a07b0
JG
273};
274
659aaf2b 275
41ab999c
KY
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
a74f8b36
JS
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
287enum tpm_buf_flags {
288 TPM_BUF_OVERFLOW = BIT(0),
289};
290
291struct tpm_buf {
292 struct page *data_page;
293 unsigned int flags;
294 u8 *data;
295};
296
ce63c05b 297static inline void tpm_buf_reset(struct tpm_buf *buf, u16 tag, u32 ordinal)
a74f8b36 298{
b34b77a9
JS
299 struct tpm_header *head = (struct tpm_header *)buf->data;
300
ce63c05b
JS
301 head->tag = cpu_to_be16(tag);
302 head->length = cpu_to_be32(sizeof(*head));
303 head->ordinal = cpu_to_be32(ordinal);
304}
a74f8b36 305
ce63c05b
JS
306static inline int tpm_buf_init(struct tpm_buf *buf, u16 tag, u32 ordinal)
307{
a74f8b36
JS
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);
ce63c05b 314 tpm_buf_reset(buf, tag, ordinal);
a74f8b36
JS
315 return 0;
316}
317
318static inline void tpm_buf_destroy(struct tpm_buf *buf)
319{
320 kunmap(buf->data_page);
321 __free_page(buf->data_page);
322}
323
324static inline u32 tpm_buf_length(struct tpm_buf *buf)
325{
b34b77a9 326 struct tpm_header *head = (struct tpm_header *)buf->data;
a74f8b36
JS
327
328 return be32_to_cpu(head->length);
329}
330
331static inline u16 tpm_buf_tag(struct tpm_buf *buf)
332{
b34b77a9 333 struct tpm_header *head = (struct tpm_header *)buf->data;
a74f8b36
JS
334
335 return be16_to_cpu(head->tag);
336}
337
338static inline void tpm_buf_append(struct tpm_buf *buf,
339 const unsigned char *new_data,
340 unsigned int new_len)
341{
b34b77a9 342 struct tpm_header *head = (struct tpm_header *)buf->data;
a74f8b36
JS
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
359static inline void tpm_buf_append_u8(struct tpm_buf *buf, const u8 value)
360{
361 tpm_buf_append(buf, &value, 1);
362}
363
364static 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
371static 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
313d21ee 378extern struct class *tpm_class;
fdc915f7 379extern struct class *tpmrm_class;
313d21ee
JS
380extern dev_t tpm_devt;
381extern const struct file_operations tpm_fops;
fdc915f7 382extern const struct file_operations tpmrm_fops;
15516788 383extern struct idr dev_nums_idr;
313d21ee 384
47a6c28b 385ssize_t tpm_transmit(struct tpm_chip *chip, u8 *buf, size_t bufsiz);
5faafbab 386ssize_t tpm_transmit_cmd(struct tpm_chip *chip, struct tpm_buf *buf,
47a6c28b 387 size_t min_rsp_body_length, const char *desc);
d4abd956 388int tpm_get_timeouts(struct tpm_chip *);
b03c4370 389int tpm_auto_startup(struct tpm_chip *chip);
c82a330c 390
95adc6b4 391int tpm1_pm_suspend(struct tpm_chip *chip, u32 tpm_suspend_pcr);
cae8b441 392int tpm1_auto_startup(struct tpm_chip *chip);
d4a31756 393int tpm1_do_selftest(struct tpm_chip *chip);
70a3199a 394int tpm1_get_timeouts(struct tpm_chip *chip);
b2d6e6de 395unsigned long tpm1_calc_ordinal_duration(struct tpm_chip *chip, u32 ordinal);
95adc6b4 396int tpm1_pcr_extend(struct tpm_chip *chip, u32 pcr_idx, const u8 *hash,
1ad6640c 397 const char *log_msg);
95adc6b4 398int tpm1_pcr_read(struct tpm_chip *chip, u32 pcr_idx, u8 *res_buf);
f4d916b7
TW
399ssize_t tpm1_getcap(struct tpm_chip *chip, u32 subcap_id, cap_t *cap,
400 const char *desc, size_t min_cap_length);
433d390f 401int tpm1_get_random(struct tpm_chip *chip, u8 *out, size_t max);
d856c00f 402unsigned long tpm_calc_ordinal_duration(struct tpm_chip *chip, u32 ordinal);
d4abd956
JS
403int tpm_pm_suspend(struct device *dev);
404int tpm_pm_resume(struct device *dev);
f84fdff0 405
9f3fc7bc
HA
406static inline void tpm_msleep(unsigned int delay_msec)
407{
5ef924d9
NJ
408 usleep_range((delay_msec * 1000) - TPM_TIMEOUT_RANGE_US,
409 delay_msec * 1000);
9f3fc7bc
HA
410};
411
47a6c28b
JS
412int tpm_chip_start(struct tpm_chip *chip);
413void tpm_chip_stop(struct tpm_chip *chip);
fc1d52b7 414struct tpm_chip *tpm_find_get_ops(struct tpm_chip *chip);
4e26195f
JG
415__must_check int tpm_try_get_ops(struct tpm_chip *chip);
416void tpm_put_ops(struct tpm_chip *chip);
417
d4abd956
JS
418struct tpm_chip *tpm_chip_alloc(struct device *dev,
419 const struct tpm_class_ops *ops);
420struct tpm_chip *tpmm_chip_alloc(struct device *pdev,
421 const struct tpm_class_ops *ops);
422int tpm_chip_register(struct tpm_chip *chip);
423void tpm_chip_unregister(struct tpm_chip *chip);
afb5abc2 424
062807f2 425void tpm_sysfs_add_device(struct tpm_chip *chip);
afdba32e 426
000a07b0 427
f84fdff0 428#ifdef CONFIG_ACPI
9b774d5c 429extern void tpm_add_ppi(struct tpm_chip *chip);
f84fdff0 430#else
9b774d5c 431static inline void tpm_add_ppi(struct tpm_chip *chip)
1631cfb7
GW
432{
433}
f84fdff0 434#endif
7a1d7e6d 435
2d56c718 436static inline u32 tpm2_rc_value(u32 rc)
7d761119
JS
437{
438 return (rc & BIT(7)) ? rc & 0xff : rc;
439}
440
70a3199a 441int tpm2_get_timeouts(struct tpm_chip *chip);
879b5892
RS
442int tpm2_pcr_read(struct tpm_chip *chip, u32 pcr_idx,
443 struct tpm_digest *digest, u16 *digest_size_ptr);
95adc6b4 444int tpm2_pcr_extend(struct tpm_chip *chip, u32 pcr_idx, u32 count,
aa042475 445 struct tpm_digest *digests);
ce63c05b 446int tpm2_get_random(struct tpm_chip *chip, u8 *dest, size_t max);
47a6c28b 447void tpm2_flush_context(struct tpm_chip *chip, u32 handle);
954650ef
JS
448int tpm2_seal_trusted(struct tpm_chip *chip,
449 struct trusted_key_payload *payload,
450 struct trusted_key_options *options);
451int tpm2_unseal_trusted(struct tpm_chip *chip,
452 struct trusted_key_payload *payload,
453 struct trusted_key_options *options);
7a1d7e6d
JS
454ssize_t tpm2_get_tpm_pt(struct tpm_chip *chip, u32 property_id,
455 u32 *value, const char *desc);
456
cae8b441 457int tpm2_auto_startup(struct tpm_chip *chip);
d4abd956
JS
458void tpm2_shutdown(struct tpm_chip *chip, u16 shutdown_type);
459unsigned long tpm2_calc_ordinal_duration(struct tpm_chip *chip, u32 ordinal);
d4abd956 460int tpm2_probe(struct tpm_chip *chip);
58472f5c 461int tpm2_find_cc(struct tpm_chip *chip, u32 cc);
745b361e 462int tpm2_init_space(struct tpm_space *space);
4d57856a 463void tpm2_del_space(struct tpm_chip *chip, struct tpm_space *space);
304ff672 464void tpm2_flush_space(struct tpm_chip *chip);
c3465a37
JS
465int tpm2_prepare_space(struct tpm_chip *chip, struct tpm_space *space, u8 *cmd,
466 size_t cmdsiz);
467int tpm2_commit_space(struct tpm_chip *chip, struct tpm_space *space, void *buf,
468 size_t *bufsiz);
fd3ec366 469
fd3ec366
TW
470int tpm_bios_log_setup(struct tpm_chip *chip);
471void tpm_bios_log_teardown(struct tpm_chip *chip);
9e1b74a6
TS
472int tpm_dev_common_init(void);
473void tpm_dev_common_exit(void);
d2efee6e 474#endif