ia64/xen-unstable

view linux-2.6-xen-sparse/drivers/char/tpm/tpm.c @ 8786:e9d78d8bd568

Clean up the TPM stack a bit.

Signed-off-by: Stefan Berger <stefanb@us.ibm.com>
author cl349@firebug.cl.cam.ac.uk
date Tue Feb 07 20:38:44 2006 +0000 (2006-02-07)
parents 514f2585d102
children 2401f414d1ad
line source
1 /*
2 * Copyright (C) 2004 IBM Corporation
3 *
4 * Authors:
5 * Leendert van Doorn <leendert@watson.ibm.com>
6 * Dave Safford <safford@watson.ibm.com>
7 * Reiner Sailer <sailer@watson.ibm.com>
8 * Kylene Hall <kjhall@us.ibm.com>
9 *
10 * Maintained by: <tpmdd_devel@lists.sourceforge.net>
11 *
12 * Device driver for TCG/TCPA TPM (trusted platform module).
13 * Specifications at www.trustedcomputinggroup.org
14 *
15 * This program is free software; you can redistribute it and/or
16 * modify it under the terms of the GNU General Public License as
17 * published by the Free Software Foundation, version 2 of the
18 * License.
19 *
20 * Note, the TPM chip is not interrupt driven (only polling)
21 * and can have very long timeouts (minutes!). Hence the unusual
22 * calls to msleep.
23 *
24 */
26 #include <linux/sched.h>
27 #include <linux/poll.h>
28 #include <linux/spinlock.h>
29 #include "tpm.h"
31 enum tpm_const {
32 TPM_MINOR = 224, /* officially assigned */
33 TPM_MIN_BUFSIZE = 2048,
34 TPM_MAX_BUFSIZE = 64 * 1024,
35 TPM_NUM_DEVICES = 256,
36 TPM_NUM_MASK_ENTRIES = TPM_NUM_DEVICES / (8 * sizeof(int))
37 };
39 static LIST_HEAD(tpm_chip_list);
40 static DEFINE_SPINLOCK(driver_lock);
41 static int dev_mask[TPM_NUM_MASK_ENTRIES];
43 static void user_reader_timeout(unsigned long ptr)
44 {
45 struct tpm_chip *chip = (struct tpm_chip *) ptr;
47 schedule_work(&chip->work);
48 }
50 static void timeout_work(void * ptr)
51 {
52 struct tpm_chip *chip = ptr;
54 down(&chip->buffer_mutex);
55 atomic_set(&chip->data_pending, 0);
56 memset(chip->data_buffer, 0, chip->vendor->buffersize);
57 up(&chip->buffer_mutex);
58 }
60 /*
61 * Internal kernel interface to transmit TPM commands
62 */
63 static ssize_t tpm_transmit(struct tpm_chip * chip, const char *buf,
64 size_t bufsiz)
65 {
66 ssize_t rc;
67 u32 count;
68 unsigned long stop;
70 count = be32_to_cpu(*((__be32 *) (buf + 2)));
72 if (count == 0)
73 return -ENODATA;
74 if (count > bufsiz) {
75 dev_err(chip->dev,
76 "invalid count value %x %zx \n", count, bufsiz);
77 return -E2BIG;
78 }
80 down(&chip->tpm_mutex);
82 if ((rc = chip->vendor->send(chip, (u8 *) buf, count)) < 0) {
83 dev_err(chip->dev,
84 "tpm_transmit: tpm_send: error %zd\n", rc);
85 goto out;
86 }
88 stop = jiffies + 2 * 60 * HZ;
89 do {
90 u8 status = chip->vendor->status(chip);
91 if ((status & chip->vendor->req_complete_mask) ==
92 chip->vendor->req_complete_val) {
93 goto out_recv;
94 }
96 if ((status == chip->vendor->req_canceled)) {
97 dev_err(chip->dev, "Operation Canceled\n");
98 rc = -ECANCELED;
99 goto out;
100 }
102 msleep(TPM_TIMEOUT); /* CHECK */
103 rmb();
104 } while (time_before(jiffies, stop));
107 chip->vendor->cancel(chip);
108 dev_err(chip->dev, "Operation Timed out\n");
109 rc = -ETIME;
110 goto out;
112 out_recv:
113 rc = chip->vendor->recv(chip, (u8 *) buf, bufsiz);
114 if (rc < 0)
115 dev_err(chip->dev,
116 "tpm_transmit: tpm_recv: error %zd\n", rc);
117 out:
118 up(&chip->tpm_mutex);
119 return rc;
120 }
122 #define TPM_DIGEST_SIZE 20
123 #define CAP_PCR_RESULT_SIZE 18
124 static const u8 cap_pcr[] = {
125 0, 193, /* TPM_TAG_RQU_COMMAND */
126 0, 0, 0, 22, /* length */
127 0, 0, 0, 101, /* TPM_ORD_GetCapability */
128 0, 0, 0, 5,
129 0, 0, 0, 4,
130 0, 0, 1, 1
131 };
133 #define READ_PCR_RESULT_SIZE 30
134 static const u8 pcrread[] = {
135 0, 193, /* TPM_TAG_RQU_COMMAND */
136 0, 0, 0, 14, /* length */
137 0, 0, 0, 21, /* TPM_ORD_PcrRead */
138 0, 0, 0, 0 /* PCR index */
139 };
141 ssize_t tpm_show_pcrs(struct device *dev, struct device_attribute *attr,
142 char *buf)
143 {
144 u8 data[READ_PCR_RESULT_SIZE];
145 ssize_t len;
146 int i, j, num_pcrs;
147 __be32 index;
148 char *str = buf;
150 struct tpm_chip *chip = dev_get_drvdata(dev);
151 if (chip == NULL)
152 return -ENODEV;
154 memcpy(data, cap_pcr, sizeof(cap_pcr));
155 if ((len = tpm_transmit(chip, data, sizeof(data)))
156 < CAP_PCR_RESULT_SIZE) {
157 dev_dbg(chip->dev, "A TPM error (%d) occurred "
158 "attempting to determine the number of PCRS\n",
159 be32_to_cpu(*((__be32 *) (data + 6))));
160 return 0;
161 }
163 num_pcrs = be32_to_cpu(*((__be32 *) (data + 14)));
165 for (i = 0; i < num_pcrs; i++) {
166 memcpy(data, pcrread, sizeof(pcrread));
167 index = cpu_to_be32(i);
168 memcpy(data + 10, &index, 4);
169 if ((len = tpm_transmit(chip, data, sizeof(data)))
170 < READ_PCR_RESULT_SIZE){
171 dev_dbg(chip->dev, "A TPM error (%d) occurred"
172 " attempting to read PCR %d of %d\n",
173 be32_to_cpu(*((__be32 *) (data + 6))),
174 i, num_pcrs);
175 goto out;
176 }
177 str += sprintf(str, "PCR-%02d: ", i);
178 for (j = 0; j < TPM_DIGEST_SIZE; j++)
179 str += sprintf(str, "%02X ", *(data + 10 + j));
180 str += sprintf(str, "\n");
181 }
182 out:
183 return str - buf;
184 }
185 EXPORT_SYMBOL_GPL(tpm_show_pcrs);
187 #define READ_PUBEK_RESULT_SIZE 314
188 static const u8 readpubek[] = {
189 0, 193, /* TPM_TAG_RQU_COMMAND */
190 0, 0, 0, 30, /* length */
191 0, 0, 0, 124, /* TPM_ORD_ReadPubek */
192 };
194 ssize_t tpm_show_pubek(struct device *dev, struct device_attribute *attr,
195 char *buf)
196 {
197 u8 *data;
198 ssize_t len;
199 int i, rc;
200 char *str = buf;
202 struct tpm_chip *chip = dev_get_drvdata(dev);
203 if (chip == NULL)
204 return -ENODEV;
206 data = kzalloc(READ_PUBEK_RESULT_SIZE, GFP_KERNEL);
207 if (!data)
208 return -ENOMEM;
210 memcpy(data, readpubek, sizeof(readpubek));
212 if ((len = tpm_transmit(chip, data, READ_PUBEK_RESULT_SIZE)) <
213 READ_PUBEK_RESULT_SIZE) {
214 dev_dbg(chip->dev, "A TPM error (%d) occurred "
215 "attempting to read the PUBEK\n",
216 be32_to_cpu(*((__be32 *) (data + 6))));
217 rc = 0;
218 goto out;
219 }
221 /*
222 ignore header 10 bytes
223 algorithm 32 bits (1 == RSA )
224 encscheme 16 bits
225 sigscheme 16 bits
226 parameters (RSA 12->bytes: keybit, #primes, expbit)
227 keylenbytes 32 bits
228 256 byte modulus
229 ignore checksum 20 bytes
230 */
232 str +=
233 sprintf(str,
234 "Algorithm: %02X %02X %02X %02X\nEncscheme: %02X %02X\n"
235 "Sigscheme: %02X %02X\nParameters: %02X %02X %02X %02X"
236 " %02X %02X %02X %02X %02X %02X %02X %02X\n"
237 "Modulus length: %d\nModulus: \n",
238 data[10], data[11], data[12], data[13], data[14],
239 data[15], data[16], data[17], data[22], data[23],
240 data[24], data[25], data[26], data[27], data[28],
241 data[29], data[30], data[31], data[32], data[33],
242 be32_to_cpu(*((__be32 *) (data + 34))));
244 for (i = 0; i < 256; i++) {
245 str += sprintf(str, "%02X ", data[i + 38]);
246 if ((i + 1) % 16 == 0)
247 str += sprintf(str, "\n");
248 }
249 rc = str - buf;
250 out:
251 kfree(data);
252 return rc;
253 }
254 EXPORT_SYMBOL_GPL(tpm_show_pubek);
256 #define CAP_VER_RESULT_SIZE 18
257 static const u8 cap_version[] = {
258 0, 193, /* TPM_TAG_RQU_COMMAND */
259 0, 0, 0, 18, /* length */
260 0, 0, 0, 101, /* TPM_ORD_GetCapability */
261 0, 0, 0, 6,
262 0, 0, 0, 0
263 };
265 #define CAP_MANUFACTURER_RESULT_SIZE 18
266 static const u8 cap_manufacturer[] = {
267 0, 193, /* TPM_TAG_RQU_COMMAND */
268 0, 0, 0, 22, /* length */
269 0, 0, 0, 101, /* TPM_ORD_GetCapability */
270 0, 0, 0, 5,
271 0, 0, 0, 4,
272 0, 0, 1, 3
273 };
275 ssize_t tpm_show_caps(struct device *dev, struct device_attribute *attr,
276 char *buf)
277 {
278 u8 data[sizeof(cap_manufacturer)];
279 ssize_t len;
280 char *str = buf;
282 struct tpm_chip *chip = dev_get_drvdata(dev);
283 if (chip == NULL)
284 return -ENODEV;
286 memcpy(data, cap_manufacturer, sizeof(cap_manufacturer));
288 if ((len = tpm_transmit(chip, data, sizeof(data))) <
289 CAP_MANUFACTURER_RESULT_SIZE)
290 return len;
292 str += sprintf(str, "Manufacturer: 0x%x\n",
293 be32_to_cpu(*((__be32 *) (data + 14))));
295 memcpy(data, cap_version, sizeof(cap_version));
297 if ((len = tpm_transmit(chip, data, sizeof(data))) <
298 CAP_VER_RESULT_SIZE)
299 return len;
301 str +=
302 sprintf(str, "TCG version: %d.%d\nFirmware version: %d.%d\n",
303 (int) data[14], (int) data[15], (int) data[16],
304 (int) data[17]);
306 return str - buf;
307 }
308 EXPORT_SYMBOL_GPL(tpm_show_caps);
310 ssize_t tpm_store_cancel(struct device *dev, struct device_attribute *attr,
311 const char *buf, size_t count)
312 {
313 struct tpm_chip *chip = dev_get_drvdata(dev);
314 if (chip == NULL)
315 return 0;
317 chip->vendor->cancel(chip);
318 return count;
319 }
320 EXPORT_SYMBOL_GPL(tpm_store_cancel);
322 /*
323 * Device file system interface to the TPM
324 */
325 int tpm_open(struct inode *inode, struct file *file)
326 {
327 int rc = 0, minor = iminor(inode);
328 struct tpm_chip *chip = NULL, *pos;
330 spin_lock(&driver_lock);
332 list_for_each_entry(pos, &tpm_chip_list, list) {
333 if (pos->vendor->miscdev.minor == minor) {
334 chip = pos;
335 break;
336 }
337 }
339 if (chip == NULL) {
340 rc = -ENODEV;
341 goto err_out;
342 }
344 if (chip->num_opens) {
345 dev_dbg(chip->dev, "Another process owns this TPM\n");
346 rc = -EBUSY;
347 goto err_out;
348 }
350 chip->num_opens++;
351 get_device(chip->dev);
353 spin_unlock(&driver_lock);
355 chip->data_buffer = kmalloc(chip->vendor->buffersize * sizeof(u8), GFP_KERNEL);
356 if (chip->data_buffer == NULL) {
357 chip->num_opens--;
358 put_device(chip->dev);
359 return -ENOMEM;
360 }
362 atomic_set(&chip->data_pending, 0);
364 file->private_data = chip;
365 return 0;
367 err_out:
368 spin_unlock(&driver_lock);
369 return rc;
370 }
371 EXPORT_SYMBOL_GPL(tpm_open);
373 int tpm_release(struct inode *inode, struct file *file)
374 {
375 struct tpm_chip *chip = file->private_data;
377 spin_lock(&driver_lock);
378 file->private_data = NULL;
379 chip->num_opens--;
380 del_singleshot_timer_sync(&chip->user_read_timer);
381 flush_scheduled_work();
382 atomic_set(&chip->data_pending, 0);
383 put_device(chip->dev);
384 kfree(chip->data_buffer);
385 spin_unlock(&driver_lock);
386 return 0;
387 }
388 EXPORT_SYMBOL_GPL(tpm_release);
390 ssize_t tpm_write(struct file *file, const char __user *buf,
391 size_t size, loff_t * off)
392 {
393 struct tpm_chip *chip = file->private_data;
394 int in_size = size, out_size;
396 /* cannot perform a write until the read has cleared
397 either via tpm_read or a user_read_timer timeout */
398 while (atomic_read(&chip->data_pending) != 0)
399 msleep(TPM_TIMEOUT);
401 down(&chip->buffer_mutex);
403 if (in_size > chip->vendor->buffersize)
404 in_size = chip->vendor->buffersize;
406 if (copy_from_user
407 (chip->data_buffer, (void __user *) buf, in_size)) {
408 up(&chip->buffer_mutex);
409 return -EFAULT;
410 }
412 /* atomic tpm command send and result receive */
413 out_size = tpm_transmit(chip, chip->data_buffer,
414 chip->vendor->buffersize);
416 atomic_set(&chip->data_pending, out_size);
417 atomic_set(&chip->data_position, 0);
418 up(&chip->buffer_mutex);
420 /* Set a timeout by which the reader must come claim the result */
421 mod_timer(&chip->user_read_timer, jiffies + (60 * HZ));
423 return in_size;
424 }
426 EXPORT_SYMBOL_GPL(tpm_write);
428 ssize_t tpm_read(struct file * file, char __user *buf,
429 size_t size, loff_t * off)
430 {
431 struct tpm_chip *chip = file->private_data;
432 int ret_size;
433 int pos, pending = 0;
435 del_singleshot_timer_sync(&chip->user_read_timer);
436 flush_scheduled_work();
437 ret_size = atomic_read(&chip->data_pending);
438 if (ret_size > 0) { /* relay data */
439 if (size < ret_size)
440 ret_size = size;
442 pos = atomic_read(&chip->data_position);
444 down(&chip->buffer_mutex);
445 if (copy_to_user(buf, &chip->data_buffer[pos], ret_size)) {
446 ret_size = -EFAULT;
447 } else {
448 pending = atomic_read(&chip->data_pending) - ret_size;
449 if ( pending ) {
450 atomic_set( &chip->data_pending, pending );
451 atomic_set( &chip->data_position, pos+ret_size );
452 }
453 }
454 up(&chip->buffer_mutex);
455 }
457 if ( ret_size <= 0 || pending == 0 ) {
458 atomic_set( &chip->data_pending, 0 );
459 del_singleshot_timer_sync(&chip->user_read_timer);
460 }
462 return ret_size;
463 }
464 EXPORT_SYMBOL_GPL(tpm_read);
466 void tpm_remove_hardware(struct device *dev)
467 {
468 struct tpm_chip *chip = dev_get_drvdata(dev);
470 if (chip == NULL) {
471 dev_err(dev, "No device data found\n");
472 return;
473 }
475 spin_lock(&driver_lock);
477 list_del(&chip->list);
479 spin_unlock(&driver_lock);
481 dev_set_drvdata(dev, NULL);
482 misc_deregister(&chip->vendor->miscdev);
483 kfree(chip->vendor->miscdev.name);
485 sysfs_remove_group(&dev->kobj, chip->vendor->attr_group);
486 tpm_bios_log_teardown(chip->bios_dir);
488 dev_mask[chip->dev_num / TPM_NUM_MASK_ENTRIES ] &=
489 ~(1 << (chip->dev_num % TPM_NUM_MASK_ENTRIES));
491 kfree(chip);
493 put_device(dev);
494 }
495 EXPORT_SYMBOL_GPL(tpm_remove_hardware);
497 static u8 savestate[] = {
498 0, 193, /* TPM_TAG_RQU_COMMAND */
499 0, 0, 0, 10, /* blob length (in bytes) */
500 0, 0, 0, 152 /* TPM_ORD_SaveState */
501 };
503 /*
504 * We are about to suspend. Save the TPM state
505 * so that it can be restored.
506 */
507 int tpm_pm_suspend(struct device *dev, pm_message_t pm_state)
508 {
509 struct tpm_chip *chip = dev_get_drvdata(dev);
510 if (chip == NULL)
511 return -ENODEV;
513 tpm_transmit(chip, savestate, sizeof(savestate));
514 return 0;
515 }
516 EXPORT_SYMBOL_GPL(tpm_pm_suspend);
518 /*
519 * Resume from a power safe. The BIOS already restored
520 * the TPM state.
521 */
522 int tpm_pm_resume(struct device *dev)
523 {
524 struct tpm_chip *chip = dev_get_drvdata(dev);
526 if (chip == NULL)
527 return -ENODEV;
529 return 0;
530 }
531 EXPORT_SYMBOL_GPL(tpm_pm_resume);
533 /*
534 * Called from tpm_<specific>.c probe function only for devices
535 * the driver has determined it should claim. Prior to calling
536 * this function the specific probe function has called pci_enable_device
537 * upon errant exit from this function specific probe function should call
538 * pci_disable_device
539 */
540 int tpm_register_hardware(struct device *dev, struct tpm_vendor_specific *entry)
541 {
542 #define DEVNAME_SIZE 7
544 char *devname;
545 struct tpm_chip *chip;
546 int i, j;
548 /* Driver specific per-device data */
549 chip = kzalloc(sizeof(*chip), GFP_KERNEL);
550 if (chip == NULL)
551 return -ENOMEM;
553 init_MUTEX(&chip->buffer_mutex);
554 init_MUTEX(&chip->tpm_mutex);
555 INIT_LIST_HEAD(&chip->list);
557 INIT_WORK(&chip->work, timeout_work, chip);
559 init_timer(&chip->user_read_timer);
560 chip->user_read_timer.function = user_reader_timeout;
561 chip->user_read_timer.data = (unsigned long) chip;
563 chip->vendor = entry;
565 if (entry->buffersize < TPM_MIN_BUFSIZE) {
566 entry->buffersize = TPM_MIN_BUFSIZE;
567 } else if (entry->buffersize > TPM_MAX_BUFSIZE) {
568 entry->buffersize = TPM_MAX_BUFSIZE;
569 }
571 chip->dev_num = -1;
573 for (i = 0; i < TPM_NUM_MASK_ENTRIES; i++)
574 for (j = 0; j < 8 * sizeof(int); j++)
575 if ((dev_mask[i] & (1 << j)) == 0) {
576 chip->dev_num =
577 i * TPM_NUM_MASK_ENTRIES + j;
578 dev_mask[i] |= 1 << j;
579 goto dev_num_search_complete;
580 }
582 dev_num_search_complete:
583 if (chip->dev_num < 0) {
584 dev_err(dev, "No available tpm device numbers\n");
585 kfree(chip);
586 return -ENODEV;
587 } else if (chip->dev_num == 0)
588 chip->vendor->miscdev.minor = TPM_MINOR;
589 else
590 chip->vendor->miscdev.minor = MISC_DYNAMIC_MINOR;
592 devname = kmalloc(DEVNAME_SIZE, GFP_KERNEL);
593 scnprintf(devname, DEVNAME_SIZE, "%s%d", "tpm", chip->dev_num);
594 chip->vendor->miscdev.name = devname;
596 chip->vendor->miscdev.dev = dev;
597 chip->dev = get_device(dev);
599 if (misc_register(&chip->vendor->miscdev)) {
600 dev_err(chip->dev,
601 "unable to misc_register %s, minor %d\n",
602 chip->vendor->miscdev.name,
603 chip->vendor->miscdev.minor);
604 put_device(dev);
605 kfree(chip);
606 dev_mask[i] &= !(1 << j);
607 return -ENODEV;
608 }
610 spin_lock(&driver_lock);
612 dev_set_drvdata(dev, chip);
614 list_add(&chip->list, &tpm_chip_list);
616 spin_unlock(&driver_lock);
618 sysfs_create_group(&dev->kobj, chip->vendor->attr_group);
620 chip->bios_dir = tpm_bios_log_setup(devname);
622 return 0;
623 }
624 EXPORT_SYMBOL_GPL(tpm_register_hardware);
626 MODULE_AUTHOR("Leendert van Doorn (leendert@watson.ibm.com)");
627 MODULE_DESCRIPTION("TPM Driver");
628 MODULE_VERSION("2.0");
629 MODULE_LICENSE("GPL");