]>
Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | * Copyright (C) 2004 IBM Corporation | |
afb5abc2 | 3 | * Copyright (C) 2014 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). | |
0a418269 | 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. | |
0a418269 | 20 | * |
1da177e4 LT |
21 | * Note, the TPM chip is not interrupt driven (only polling) |
22 | * and can have very long timeouts (minutes!). Hence the unusual | |
700d8bdc | 23 | * calls to msleep. |
1da177e4 LT |
24 | * |
25 | */ | |
26 | ||
1da177e4 | 27 | #include <linux/poll.h> |
5a0e3ad6 | 28 | #include <linux/slab.h> |
d081d470 | 29 | #include <linux/mutex.h> |
1da177e4 | 30 | #include <linux/spinlock.h> |
fd048866 | 31 | #include <linux/freezer.h> |
fd3ec366 | 32 | #include <linux/tpm_eventlog.h> |
d081d470 | 33 | |
1da177e4 | 34 | #include "tpm.h" |
1da177e4 | 35 | |
9b3056cc DT |
36 | /* |
37 | * Bug workaround - some TPM's don't flush the most | |
38 | * recently changed pcr on suspend, so force the flush | |
39 | * with an extend to the selected _unused_ non-volatile pcr. | |
40 | */ | |
95adc6b4 | 41 | static u32 tpm_suspend_pcr; |
9b3056cc DT |
42 | module_param_named(suspend_pcr, tpm_suspend_pcr, uint, 0644); |
43 | MODULE_PARM_DESC(suspend_pcr, | |
39f5712b | 44 | "PCR to use for dummy writes to facilitate flush on suspend."); |
9b3056cc | 45 | |
d856c00f TW |
46 | /** |
47 | * tpm_calc_ordinal_duration() - calculate the maximum command duration | |
48 | * @chip: TPM chip to use. | |
49 | * @ordinal: TPM command ordinal. | |
50 | * | |
51 | * The function returns the maximum amount of time the chip could take | |
52 | * to return the result for a particular ordinal in jiffies. | |
53 | * | |
54 | * Return: A maximal duration time for an ordinal in jiffies. | |
55 | */ | |
56 | unsigned long tpm_calc_ordinal_duration(struct tpm_chip *chip, u32 ordinal) | |
57 | { | |
58 | if (chip->flags & TPM_CHIP_FLAG_TPM2) | |
59 | return tpm2_calc_ordinal_duration(chip, ordinal); | |
60 | else | |
61 | return tpm1_calc_ordinal_duration(chip, ordinal); | |
62 | } | |
63 | EXPORT_SYMBOL_GPL(tpm_calc_ordinal_duration); | |
64 | ||
47a6c28b | 65 | static ssize_t tpm_try_transmit(struct tpm_chip *chip, void *buf, size_t bufsiz) |
1da177e4 | 66 | { |
b34b77a9 | 67 | struct tpm_header *header = buf; |
745b361e JS |
68 | int rc; |
69 | ssize_t len = 0; | |
9e18ee19 | 70 | u32 count, ordinal; |
700d8bdc | 71 | unsigned long stop; |
1da177e4 | 72 | |
c3465a37 JS |
73 | if (bufsiz < TPM_HEADER_SIZE) |
74 | return -EINVAL; | |
ebfd7532 | 75 | |
6b07d30a PH |
76 | if (bufsiz > TPM_BUFSIZE) |
77 | bufsiz = TPM_BUFSIZE; | |
78 | ||
720b0711 JS |
79 | count = be32_to_cpu(header->length); |
80 | ordinal = be32_to_cpu(header->ordinal); | |
1da177e4 LT |
81 | if (count == 0) |
82 | return -ENODATA; | |
83 | if (count > bufsiz) { | |
8cfffc9d | 84 | dev_err(&chip->dev, |
0a418269 | 85 | "invalid count value %x %zx\n", count, bufsiz); |
1da177e4 LT |
86 | return -E2BIG; |
87 | } | |
88 | ||
62c09e12 | 89 | rc = chip->ops->send(chip, buf, count); |
0a418269 | 90 | if (rc < 0) { |
402149c6 SB |
91 | if (rc != -EPIPE) |
92 | dev_err(&chip->dev, | |
f5595f5b | 93 | "%s: send(): error %d\n", __func__, rc); |
29b47ce9 | 94 | return rc; |
1da177e4 LT |
95 | } |
96 | ||
f5595f5b JS |
97 | /* A sanity check. send() should just return zero on success e.g. |
98 | * not the command length. | |
99 | */ | |
100 | if (rc > 0) { | |
101 | dev_warn(&chip->dev, | |
102 | "%s: send(): invalid value %d\n", __func__, rc); | |
103 | rc = 0; | |
104 | } | |
105 | ||
570a3609 | 106 | if (chip->flags & TPM_CHIP_FLAG_IRQ) |
27084efe LD |
107 | goto out_recv; |
108 | ||
d856c00f | 109 | stop = jiffies + tpm_calc_ordinal_duration(chip, ordinal); |
1da177e4 | 110 | do { |
5f82e9f0 JG |
111 | u8 status = chip->ops->status(chip); |
112 | if ((status & chip->ops->req_complete_mask) == | |
113 | chip->ops->req_complete_val) | |
1da177e4 | 114 | goto out_recv; |
d9e5b6bf | 115 | |
5f82e9f0 | 116 | if (chip->ops->req_canceled(chip, status)) { |
8cfffc9d | 117 | dev_err(&chip->dev, "Operation Canceled\n"); |
29b47ce9 | 118 | return -ECANCELED; |
d9e5b6bf KH |
119 | } |
120 | ||
59f5a6b0 | 121 | tpm_msleep(TPM_TIMEOUT_POLL); |
1da177e4 | 122 | rmb(); |
700d8bdc | 123 | } while (time_before(jiffies, stop)); |
1da177e4 | 124 | |
5f82e9f0 | 125 | chip->ops->cancel(chip); |
8cfffc9d | 126 | dev_err(&chip->dev, "Operation Timed out\n"); |
29b47ce9 | 127 | return -ETIME; |
1da177e4 LT |
128 | |
129 | out_recv: | |
62c09e12 | 130 | len = chip->ops->recv(chip, buf, bufsiz); |
745b361e JS |
131 | if (len < 0) { |
132 | rc = len; | |
304ff672 JS |
133 | dev_err(&chip->dev, "tpm_transmit: tpm_recv: error %d\n", rc); |
134 | } else if (len < TPM_HEADER_SIZE || len != be32_to_cpu(header->length)) | |
a147918e | 135 | rc = -EFAULT; |
745b361e | 136 | |
745b361e | 137 | return rc ? rc : len; |
1da177e4 LT |
138 | } |
139 | ||
e2fb992d JB |
140 | /** |
141 | * tpm_transmit - Internal kernel interface to transmit TPM commands. | |
412eb585 | 142 | * @chip: a TPM chip to use |
412eb585 JS |
143 | * @buf: a TPM command buffer |
144 | * @bufsiz: length of the TPM command buffer | |
e2fb992d | 145 | * |
412eb585 JS |
146 | * A wrapper around tpm_try_transmit() that handles TPM2_RC_RETRY returns from |
147 | * the TPM and retransmits the command after a delay up to a maximum wait of | |
148 | * TPM2_DURATION_LONG. | |
e2fb992d | 149 | * |
412eb585 JS |
150 | * Note that TPM 1.x never returns TPM2_RC_RETRY so the retry logic is TPM 2.0 |
151 | * only. | |
e2fb992d JB |
152 | * |
153 | * Return: | |
412eb585 JS |
154 | * * The response length - OK |
155 | * * -errno - A system error | |
e2fb992d | 156 | */ |
47a6c28b | 157 | ssize_t tpm_transmit(struct tpm_chip *chip, u8 *buf, size_t bufsiz) |
e2fb992d | 158 | { |
b34b77a9 | 159 | struct tpm_header *header = (struct tpm_header *)buf; |
e2fb992d JB |
160 | /* space for header and handles */ |
161 | u8 save[TPM_HEADER_SIZE + 3*sizeof(u32)]; | |
162 | unsigned int delay_msec = TPM2_DURATION_SHORT; | |
163 | u32 rc = 0; | |
164 | ssize_t ret; | |
5faafbab | 165 | const size_t save_size = min(sizeof(save), bufsiz); |
2be8ffed JB |
166 | /* the command code is where the return code will be */ |
167 | u32 cc = be32_to_cpu(header->return_code); | |
e2fb992d JB |
168 | |
169 | /* | |
170 | * Subtlety here: if we have a space, the handles will be | |
171 | * transformed, so when we restore the header we also have to | |
172 | * restore the handles. | |
173 | */ | |
174 | memcpy(save, buf, save_size); | |
175 | ||
176 | for (;;) { | |
47a6c28b | 177 | ret = tpm_try_transmit(chip, buf, bufsiz); |
e2fb992d JB |
178 | if (ret < 0) |
179 | break; | |
180 | rc = be32_to_cpu(header->return_code); | |
2be8ffed JB |
181 | if (rc != TPM2_RC_RETRY && rc != TPM2_RC_TESTING) |
182 | break; | |
183 | /* | |
184 | * return immediately if self test returns test | |
185 | * still running to shorten boot time. | |
186 | */ | |
187 | if (rc == TPM2_RC_TESTING && cc == TPM2_CC_SELF_TEST) | |
e2fb992d | 188 | break; |
92980756 | 189 | |
e2fb992d | 190 | if (delay_msec > TPM2_DURATION_LONG) { |
2be8ffed JB |
191 | if (rc == TPM2_RC_RETRY) |
192 | dev_err(&chip->dev, "in retry loop\n"); | |
193 | else | |
194 | dev_err(&chip->dev, | |
195 | "self test is still running\n"); | |
e2fb992d JB |
196 | break; |
197 | } | |
198 | tpm_msleep(delay_msec); | |
92980756 | 199 | delay_msec *= 2; |
e2fb992d JB |
200 | memcpy(buf, save, save_size); |
201 | } | |
202 | return ret; | |
203 | } | |
412eb585 | 204 | |
f865c196 | 205 | /** |
65520d46 | 206 | * tpm_transmit_cmd - send a tpm command to the device |
412eb585 | 207 | * @chip: a TPM chip to use |
412eb585 JS |
208 | * @buf: a TPM command buffer |
209 | * @min_rsp_body_length: minimum expected length of response body | |
412eb585 | 210 | * @desc: command description used in the error message |
f865c196 WT |
211 | * |
212 | * Return: | |
412eb585 JS |
213 | * * 0 - OK |
214 | * * -errno - A system error | |
215 | * * TPM_RC - A TPM error | |
f865c196 | 216 | */ |
5faafbab | 217 | ssize_t tpm_transmit_cmd(struct tpm_chip *chip, struct tpm_buf *buf, |
47a6c28b | 218 | size_t min_rsp_body_length, const char *desc) |
beed53a1 | 219 | { |
b34b77a9 | 220 | const struct tpm_header *header = (struct tpm_header *)buf->data; |
beed53a1 | 221 | int err; |
c659af78 | 222 | ssize_t len; |
beed53a1 | 223 | |
47a6c28b | 224 | len = tpm_transmit(chip, buf->data, PAGE_SIZE); |
beed53a1 KJH |
225 | if (len < 0) |
226 | return len; | |
87155b73 JS |
227 | |
228 | err = be32_to_cpu(header->return_code); | |
0d6d0d62 | 229 | if (err != 0 && err != TPM_ERR_DISABLED && err != TPM_ERR_DEACTIVATED |
08a8112a | 230 | && err != TPM2_RC_TESTING && desc) |
8cfffc9d | 231 | dev_err(&chip->dev, "A TPM error (%d) occurred %s\n", err, |
71ed848f | 232 | desc); |
c659af78 SB |
233 | if (err) |
234 | return err; | |
235 | ||
236 | if (len < min_rsp_body_length + TPM_HEADER_SIZE) | |
237 | return -EFAULT; | |
b9e3238a | 238 | |
c659af78 | 239 | return 0; |
beed53a1 | 240 | } |
be4c9acf | 241 | EXPORT_SYMBOL_GPL(tpm_transmit_cmd); |
beed53a1 | 242 | |
2b30a90f | 243 | int tpm_get_timeouts(struct tpm_chip *chip) |
08e96e48 | 244 | { |
d1d253cf JG |
245 | if (chip->flags & TPM_CHIP_FLAG_HAVE_TIMEOUTS) |
246 | return 0; | |
247 | ||
70a3199a TW |
248 | if (chip->flags & TPM_CHIP_FLAG_TPM2) |
249 | return tpm2_get_timeouts(chip); | |
250 | else | |
251 | return tpm1_get_timeouts(chip); | |
08e96e48 KJH |
252 | } |
253 | EXPORT_SYMBOL_GPL(tpm_get_timeouts); | |
254 | ||
954650ef | 255 | /** |
aad887f6 JS |
256 | * tpm_is_tpm2 - do we a have a TPM2 chip? |
257 | * @chip: a &struct tpm_chip instance, %NULL for the default chip | |
954650ef | 258 | * |
aad887f6 JS |
259 | * Return: |
260 | * 1 if we have a TPM2 chip. | |
261 | * 0 if we don't have a TPM2 chip. | |
262 | * A negative number for system errors (errno). | |
954650ef | 263 | */ |
aad887f6 | 264 | int tpm_is_tpm2(struct tpm_chip *chip) |
954650ef | 265 | { |
954650ef JS |
266 | int rc; |
267 | ||
fc1d52b7 | 268 | chip = tpm_find_get_ops(chip); |
aad887f6 | 269 | if (!chip) |
954650ef JS |
270 | return -ENODEV; |
271 | ||
272 | rc = (chip->flags & TPM_CHIP_FLAG_TPM2) != 0; | |
273 | ||
4e26195f | 274 | tpm_put_ops(chip); |
954650ef JS |
275 | |
276 | return rc; | |
277 | } | |
278 | EXPORT_SYMBOL_GPL(tpm_is_tpm2); | |
279 | ||
659aaf2b | 280 | /** |
aad887f6 JS |
281 | * tpm_pcr_read - read a PCR value from SHA1 bank |
282 | * @chip: a &struct tpm_chip instance, %NULL for the default chip | |
283 | * @pcr_idx: the PCR to be retrieved | |
879b5892 | 284 | * @digest: the PCR bank and buffer current PCR value is written to |
659aaf2b | 285 | * |
aad887f6 | 286 | * Return: same as with tpm_transmit_cmd() |
659aaf2b | 287 | */ |
879b5892 RS |
288 | int tpm_pcr_read(struct tpm_chip *chip, u32 pcr_idx, |
289 | struct tpm_digest *digest) | |
659aaf2b | 290 | { |
659aaf2b RA |
291 | int rc; |
292 | ||
fc1d52b7 | 293 | chip = tpm_find_get_ops(chip); |
aad887f6 | 294 | if (!chip) |
659aaf2b | 295 | return -ENODEV; |
d4a31756 | 296 | |
7a1d7e6d | 297 | if (chip->flags & TPM_CHIP_FLAG_TPM2) |
879b5892 | 298 | rc = tpm2_pcr_read(chip, pcr_idx, digest, NULL); |
7a1d7e6d | 299 | else |
879b5892 | 300 | rc = tpm1_pcr_read(chip, pcr_idx, digest->digest); |
d4a31756 | 301 | |
4e26195f | 302 | tpm_put_ops(chip); |
659aaf2b RA |
303 | return rc; |
304 | } | |
305 | EXPORT_SYMBOL_GPL(tpm_pcr_read); | |
306 | ||
307 | /** | |
aad887f6 JS |
308 | * tpm_pcr_extend - extend a PCR value in SHA1 bank. |
309 | * @chip: a &struct tpm_chip instance, %NULL for the default chip | |
310 | * @pcr_idx: the PCR to be retrieved | |
0b6cf6b9 | 311 | * @digests: array of tpm_digest structures used to extend PCRs |
659aaf2b | 312 | * |
0b6cf6b9 RS |
313 | * Note: callers must pass a digest for every allocated PCR bank, in the same |
314 | * order of the banks in chip->allocated_banks. | |
aad887f6 JS |
315 | * |
316 | * Return: same as with tpm_transmit_cmd() | |
659aaf2b | 317 | */ |
0b6cf6b9 RS |
318 | int tpm_pcr_extend(struct tpm_chip *chip, u32 pcr_idx, |
319 | struct tpm_digest *digests) | |
659aaf2b | 320 | { |
659aaf2b | 321 | int rc; |
c1f92b4b | 322 | int i; |
659aaf2b | 323 | |
fc1d52b7 | 324 | chip = tpm_find_get_ops(chip); |
aad887f6 | 325 | if (!chip) |
659aaf2b RA |
326 | return -ENODEV; |
327 | ||
0b6cf6b9 RS |
328 | for (i = 0; i < chip->nr_allocated_banks; i++) |
329 | if (digests[i].alg_id != chip->allocated_banks[i].alg_id) | |
330 | return -EINVAL; | |
c1f92b4b | 331 | |
0b6cf6b9 RS |
332 | if (chip->flags & TPM_CHIP_FLAG_TPM2) { |
333 | rc = tpm2_pcr_extend(chip, pcr_idx, digests); | |
4e26195f | 334 | tpm_put_ops(chip); |
7a1d7e6d JS |
335 | return rc; |
336 | } | |
337 | ||
0b6cf6b9 | 338 | rc = tpm1_pcr_extend(chip, pcr_idx, digests[0].digest, |
175d5b2a | 339 | "attempting extend a PCR value"); |
4e26195f | 340 | tpm_put_ops(chip); |
659aaf2b RA |
341 | return rc; |
342 | } | |
343 | EXPORT_SYMBOL_GPL(tpm_pcr_extend); | |
344 | ||
aad887f6 JS |
345 | /** |
346 | * tpm_send - send a TPM command | |
347 | * @chip: a &struct tpm_chip instance, %NULL for the default chip | |
348 | * @cmd: a TPM command buffer | |
349 | * @buflen: the length of the TPM command buffer | |
350 | * | |
351 | * Return: same as with tpm_transmit_cmd() | |
352 | */ | |
353 | int tpm_send(struct tpm_chip *chip, void *cmd, size_t buflen) | |
c749ba91 | 354 | { |
412eb585 | 355 | struct tpm_buf buf; |
c749ba91 MZ |
356 | int rc; |
357 | ||
fc1d52b7 | 358 | chip = tpm_find_get_ops(chip); |
aad887f6 | 359 | if (!chip) |
c749ba91 MZ |
360 | return -ENODEV; |
361 | ||
412eb585 JS |
362 | rc = tpm_buf_init(&buf, 0, 0); |
363 | if (rc) | |
364 | goto out; | |
365 | ||
366 | memcpy(buf.data, cmd, buflen); | |
47a6c28b | 367 | rc = tpm_transmit_cmd(chip, &buf, 0, "attempting to a send a command"); |
412eb585 JS |
368 | tpm_buf_destroy(&buf); |
369 | out: | |
4e26195f | 370 | tpm_put_ops(chip); |
c749ba91 MZ |
371 | return rc; |
372 | } | |
373 | EXPORT_SYMBOL_GPL(tpm_send); | |
374 | ||
b03c4370 TW |
375 | int tpm_auto_startup(struct tpm_chip *chip) |
376 | { | |
377 | int rc; | |
378 | ||
379 | if (!(chip->ops->flags & TPM_OPS_AUTO_STARTUP)) | |
380 | return 0; | |
381 | ||
382 | if (chip->flags & TPM_CHIP_FLAG_TPM2) | |
383 | rc = tpm2_auto_startup(chip); | |
384 | else | |
385 | rc = tpm1_auto_startup(chip); | |
386 | ||
387 | return rc; | |
388 | } | |
389 | ||
1da177e4 LT |
390 | /* |
391 | * We are about to suspend. Save the TPM state | |
392 | * so that it can be restored. | |
393 | */ | |
035e2ce8 | 394 | int tpm_pm_suspend(struct device *dev) |
1da177e4 | 395 | { |
ec03c50b | 396 | struct tpm_chip *chip = dev_get_drvdata(dev); |
c82a330c | 397 | int rc = 0; |
2490c681 | 398 | |
c82a330c | 399 | if (!chip) |
1da177e4 LT |
400 | return -ENODEV; |
401 | ||
b5d0ebc9 EBS |
402 | if (chip->flags & TPM_CHIP_FLAG_ALWAYS_POWERED) |
403 | return 0; | |
404 | ||
a3fbfae8 JS |
405 | if (chip->flags & TPM_CHIP_FLAG_TPM2) { |
406 | mutex_lock(&chip->tpm_mutex); | |
47a6c28b | 407 | if (!tpm_chip_start(chip)) { |
a3fbfae8 | 408 | tpm2_shutdown(chip, TPM2_SU_STATE); |
47a6c28b | 409 | tpm_chip_stop(chip); |
a3fbfae8 JS |
410 | } |
411 | mutex_unlock(&chip->tpm_mutex); | |
412 | } else { | |
c82a330c | 413 | rc = tpm1_pm_suspend(chip, tpm_suspend_pcr); |
a3fbfae8 | 414 | } |
32d33b29 | 415 | |
225a9be2 | 416 | return rc; |
1da177e4 | 417 | } |
1da177e4 LT |
418 | EXPORT_SYMBOL_GPL(tpm_pm_suspend); |
419 | ||
420 | /* | |
421 | * Resume from a power safe. The BIOS already restored | |
422 | * the TPM state. | |
423 | */ | |
ce2c87d4 | 424 | int tpm_pm_resume(struct device *dev) |
1da177e4 | 425 | { |
ec03c50b | 426 | struct tpm_chip *chip = dev_get_drvdata(dev); |
1da177e4 LT |
427 | |
428 | if (chip == NULL) | |
429 | return -ENODEV; | |
430 | ||
1da177e4 LT |
431 | return 0; |
432 | } | |
1da177e4 LT |
433 | EXPORT_SYMBOL_GPL(tpm_pm_resume); |
434 | ||
41ab999c | 435 | /** |
aad887f6 JS |
436 | * tpm_get_random() - get random bytes from the TPM's RNG |
437 | * @chip: a &struct tpm_chip instance, %NULL for the default chip | |
438 | * @out: destination buffer for the random bytes | |
439 | * @max: the max number of bytes to write to @out | |
41ab999c | 440 | * |
7aee9c52 | 441 | * Return: number of random bytes read or a negative error value. |
41ab999c | 442 | */ |
aad887f6 | 443 | int tpm_get_random(struct tpm_chip *chip, u8 *out, size_t max) |
41ab999c | 444 | { |
433d390f | 445 | int rc; |
41ab999c | 446 | |
433d390f | 447 | if (!out || max > TPM_MAX_RNG_DATA) |
3e14d83e JS |
448 | return -EINVAL; |
449 | ||
fc1d52b7 | 450 | chip = tpm_find_get_ops(chip); |
aad887f6 | 451 | if (!chip) |
41ab999c KY |
452 | return -ENODEV; |
453 | ||
433d390f TW |
454 | if (chip->flags & TPM_CHIP_FLAG_TPM2) |
455 | rc = tpm2_get_random(chip, out, max); | |
456 | else | |
457 | rc = tpm1_get_random(chip, out, max); | |
41ab999c | 458 | |
4e26195f | 459 | tpm_put_ops(chip); |
433d390f | 460 | return rc; |
41ab999c KY |
461 | } |
462 | EXPORT_SYMBOL_GPL(tpm_get_random); | |
463 | ||
954650ef | 464 | /** |
aad887f6 JS |
465 | * tpm_seal_trusted() - seal a trusted key payload |
466 | * @chip: a &struct tpm_chip instance, %NULL for the default chip | |
467 | * @options: authentication values and other options | |
468 | * @payload: the key data in clear and encrypted form | |
469 | * | |
470 | * Note: only TPM 2.0 chip are supported. TPM 1.x implementation is located in | |
471 | * the keyring subsystem. | |
954650ef | 472 | * |
aad887f6 | 473 | * Return: same as with tpm_transmit_cmd() |
954650ef | 474 | */ |
aad887f6 | 475 | int tpm_seal_trusted(struct tpm_chip *chip, struct trusted_key_payload *payload, |
954650ef JS |
476 | struct trusted_key_options *options) |
477 | { | |
954650ef JS |
478 | int rc; |
479 | ||
fc1d52b7 | 480 | chip = tpm_find_get_ops(chip); |
aad887f6 | 481 | if (!chip || !(chip->flags & TPM_CHIP_FLAG_TPM2)) |
954650ef JS |
482 | return -ENODEV; |
483 | ||
484 | rc = tpm2_seal_trusted(chip, payload, options); | |
485 | ||
4e26195f | 486 | tpm_put_ops(chip); |
954650ef JS |
487 | return rc; |
488 | } | |
489 | EXPORT_SYMBOL_GPL(tpm_seal_trusted); | |
490 | ||
491 | /** | |
492 | * tpm_unseal_trusted() - unseal a trusted key | |
aad887f6 JS |
493 | * @chip: a &struct tpm_chip instance, %NULL for the default chip |
494 | * @options: authentication values and other options | |
495 | * @payload: the key data in clear and encrypted form | |
496 | * | |
497 | * Note: only TPM 2.0 chip are supported. TPM 1.x implementation is located in | |
498 | * the keyring subsystem. | |
954650ef | 499 | * |
aad887f6 | 500 | * Return: same as with tpm_transmit_cmd() |
954650ef | 501 | */ |
aad887f6 JS |
502 | int tpm_unseal_trusted(struct tpm_chip *chip, |
503 | struct trusted_key_payload *payload, | |
954650ef JS |
504 | struct trusted_key_options *options) |
505 | { | |
954650ef JS |
506 | int rc; |
507 | ||
fc1d52b7 | 508 | chip = tpm_find_get_ops(chip); |
aad887f6 | 509 | if (!chip || !(chip->flags & TPM_CHIP_FLAG_TPM2)) |
954650ef JS |
510 | return -ENODEV; |
511 | ||
512 | rc = tpm2_unseal_trusted(chip, payload, options); | |
513 | ||
4e26195f JG |
514 | tpm_put_ops(chip); |
515 | ||
954650ef JS |
516 | return rc; |
517 | } | |
518 | EXPORT_SYMBOL_GPL(tpm_unseal_trusted); | |
519 | ||
313d21ee JS |
520 | static int __init tpm_init(void) |
521 | { | |
522 | int rc; | |
523 | ||
524 | tpm_class = class_create(THIS_MODULE, "tpm"); | |
525 | if (IS_ERR(tpm_class)) { | |
526 | pr_err("couldn't create tpm class\n"); | |
527 | return PTR_ERR(tpm_class); | |
528 | } | |
529 | ||
fdc915f7 JB |
530 | tpmrm_class = class_create(THIS_MODULE, "tpmrm"); |
531 | if (IS_ERR(tpmrm_class)) { | |
532 | pr_err("couldn't create tpmrm class\n"); | |
9e1b74a6 TS |
533 | rc = PTR_ERR(tpmrm_class); |
534 | goto out_destroy_tpm_class; | |
fdc915f7 JB |
535 | } |
536 | ||
537 | rc = alloc_chrdev_region(&tpm_devt, 0, 2*TPM_NUM_DEVICES, "tpm"); | |
313d21ee JS |
538 | if (rc < 0) { |
539 | pr_err("tpm: failed to allocate char dev region\n"); | |
9e1b74a6 TS |
540 | goto out_destroy_tpmrm_class; |
541 | } | |
542 | ||
543 | rc = tpm_dev_common_init(); | |
544 | if (rc) { | |
545 | pr_err("tpm: failed to allocate char dev region\n"); | |
546 | goto out_unreg_chrdev; | |
313d21ee JS |
547 | } |
548 | ||
549 | return 0; | |
9e1b74a6 TS |
550 | |
551 | out_unreg_chrdev: | |
552 | unregister_chrdev_region(tpm_devt, 2 * TPM_NUM_DEVICES); | |
553 | out_destroy_tpmrm_class: | |
554 | class_destroy(tpmrm_class); | |
555 | out_destroy_tpm_class: | |
556 | class_destroy(tpm_class); | |
557 | ||
558 | return rc; | |
313d21ee JS |
559 | } |
560 | ||
561 | static void __exit tpm_exit(void) | |
562 | { | |
15516788 | 563 | idr_destroy(&dev_nums_idr); |
313d21ee | 564 | class_destroy(tpm_class); |
fdc915f7 JB |
565 | class_destroy(tpmrm_class); |
566 | unregister_chrdev_region(tpm_devt, 2*TPM_NUM_DEVICES); | |
9e1b74a6 | 567 | tpm_dev_common_exit(); |
313d21ee JS |
568 | } |
569 | ||
570 | subsys_initcall(tpm_init); | |
571 | module_exit(tpm_exit); | |
572 | ||
1da177e4 LT |
573 | MODULE_AUTHOR("Leendert van Doorn (leendert@watson.ibm.com)"); |
574 | MODULE_DESCRIPTION("TPM Driver"); | |
575 | MODULE_VERSION("2.0"); | |
576 | MODULE_LICENSE("GPL"); |