26 #include <linux/poll.h>
27 #include <linux/slab.h>
42 #define TPM_MAX_ORDINAL 243
43 #define TPM_MAX_PROTECTED_ORDINAL 12
44 #define TPM_PROTECTED_ORDINAL_MASK 0xFF
51 static int tpm_suspend_pcr;
54 "PCR to use for dummy writes to faciltate flush on suspend.");
329 static void user_reader_timeout(
unsigned long ptr)
356 duration_idx = tpm_ordinal_duration[
ordinal];
360 tpm_protected_ordinal_duration[ordinal &
364 duration = chip->
vendor.duration[duration_idx];
389 if (count > bufsiz) {
391 "invalid count value %x %zx \n", count, bufsiz);
397 if ((rc = chip->
vendor.send(chip, (
u8 *) buf, count)) < 0) {
399 "tpm_transmit: tpm_send: error %zd\n", rc);
409 if ((status & chip->
vendor.req_complete_mask) ==
410 chip->
vendor.req_complete_val)
413 if ((status == chip->
vendor.req_canceled)) {
423 chip->
vendor.cancel(chip);
429 rc = chip->
vendor.recv(chip, (
u8 *) buf, bufsiz);
432 "tpm_transmit: tpm_recv: error %zd\n", rc);
438 #define TPM_DIGEST_SIZE 20
439 #define TPM_RET_CODE_IDX 6
460 int len,
const char *
desc)
464 len = tpm_transmit(chip,(
u8 *) cmd, len);
472 dev_err(chip->
dev,
"A TPM error (%d) occurred %s\n", err, desc);
477 #define TPM_INTERNAL_RESULT_SIZE 200
478 #define TPM_TAG_RQU_COMMAND cpu_to_be16(193)
479 #define TPM_ORD_GET_CAP cpu_to_be32(101)
480 #define TPM_ORD_GET_RANDOM cpu_to_be32(70)
495 tpm_cmd.
header.
in = tpm_getcap_header;
521 tpm_cmd.
header.
in = tpm_getcap_header;
527 "attempting to determine the timeouts");
538 unsigned int scale = 1;
540 tpm_cmd.
header.
in = tpm_getcap_header;
546 "attempting to determine the timeouts");
558 if (timeout && timeout < 1000) {
561 chip->
vendor.timeout_adjusted =
true;
576 tpm_cmd.
header.
in = tpm_getcap_header;
582 "attempting to determine the durations");
608 chip->
vendor.duration_adjusted =
true;
609 dev_info(chip->
dev,
"Adjusting TPM timeout parameters.");
615 #define TPM_ORD_CONTINUE_SELFTEST 83
616 #define CONTINUE_SELFTEST_RESULT_SIZE 10
631 static int tpm_continue_selftest(
struct tpm_chip *chip)
636 cmd.header.in = continue_selftest_header;
638 "continue selftest");
649 "attempting to determine the permanent enabled state");
665 "attempting to determine the permanent active state");
681 "attempting to determine the owner state");
697 "attempting to determine the temporary state");
709 static struct tpm_chip *tpm_chip_find_get(
int chip_num)
714 list_for_each_entry_rcu(pos, &tpm_chip_list,
list) {
718 if (try_module_get(pos->
dev->driver->owner)) {
727 #define TPM_ORDINAL_PCRREAD cpu_to_be32(21)
728 #define READ_PCR_RESULT_SIZE 30
740 cmd.header.in = pcrread_header;
743 "attempting to read a pcr value");
746 memcpy(res_buf,
cmd.params.pcrread_out.pcr_result,
767 chip = tpm_chip_find_get(chip_num);
770 rc = __tpm_pcr_read(chip, pcr_idx, res_buf);
786 #define TPM_ORD_PCR_EXTEND cpu_to_be32(20)
787 #define EXTEND_PCR_RESULT_SIZE 34
800 chip = tpm_chip_find_get(chip_num);
808 "attempting extend a PCR value");
827 unsigned int delay_msec = 1000;
836 rc = tpm_continue_selftest(chip);
855 "TPM is disabled/deactivated (0x%X)\n", rc);
865 }
while (--loops > 0);
876 chip = tpm_chip_find_get(chip_num);
880 rc = transmit_cmd(chip, cmd, buflen,
"attempting tpm_cmd");
898 "attempting to determine the number of PCRS");
903 for (i = 0; i < num_pcrs; i++) {
904 rc = __tpm_pcr_read(chip, i, digest);
907 str +=
sprintf(str,
"PCR-%02d: ", i);
909 str +=
sprintf(str,
"%02X ", digest[j]);
916 #define READ_PUBEK_RESULT_SIZE 314
917 #define TPM_ORD_READPUBEK cpu_to_be32(124)
935 tpm_cmd.
header.
in = tpm_readpubek_header;
937 "attempting to read the PUBEK");
954 "Algorithm: %02X %02X %02X %02X\n"
955 "Encscheme: %02X %02X\n"
956 "Sigscheme: %02X %02X\n"
957 "Parameters: %02X %02X %02X %02X "
958 "%02X %02X %02X %02X "
959 "%02X %02X %02X %02X\n"
960 "Modulus length: %d\n"
962 data[0], data[1], data[2], data[3],
965 data[12], data[13], data[14], data[15],
966 data[16], data[17], data[18], data[19],
967 data[20], data[21], data[22], data[23],
970 for (i = 0; i < 256; i++) {
971 str +=
sprintf(str,
"%02X ", data[i + 28]);
972 if ((i + 1) % 16 == 0)
990 "attempting to determine the manufacturer");
993 str +=
sprintf(str,
"Manufacturer: 0x%x\n",
997 "attempting to determine the 1.1 version");
1001 "TCG version: %d.%d\nFirmware version: %d.%d\n",
1016 "attempting to determine the manufacturer");
1019 str +=
sprintf(str,
"Manufacturer: 0x%x\n",
1022 "attempting to determine the 1.2 version");
1026 "TCG version: %d.%d\nFirmware version: %d.%d\n",
1042 return sprintf(buf,
"%d %d %d [%s]\n",
1046 chip->
vendor.duration_adjusted
1047 ?
"adjusted" :
"original");
1056 return sprintf(buf,
"%d %d %d %d [%s]\n",
1061 chip->
vendor.timeout_adjusted
1062 ?
"adjusted" :
"original");
1067 const char *buf,
size_t count)
1073 chip->
vendor.cancel(chip);
1086 status = chip->
vendor.status(chip);
1087 if ((status & mask) == mask)
1095 if ((
long)timeout <= 0)
1098 ((chip->
vendor.status(chip)
1110 status = chip->
vendor.status(chip);
1111 if ((status & mask) == mask)
1127 int minor = iminor(inode);
1131 list_for_each_entry_rcu(pos, &tpm_chip_list,
list) {
1132 if (pos->vendor.miscdev.minor == minor) {
1144 dev_dbg(chip->
dev,
"Another process owns this TPM\n");
1182 size_t size, loff_t *off)
1185 size_t in_size =
size;
1224 size_t size, loff_t *off)
1234 ssize_t orig_ret_size = ret_size;
1235 if (size < ret_size)
1258 dev_err(dev,
"No device data found\n");
1262 spin_lock(&driver_lock);
1263 list_del_rcu(&chip->
list);
1264 spin_unlock(&driver_lock);
1277 #define TPM_ORD_SAVESTATE cpu_to_be32(152)
1278 #define SAVESTATE_RESULT_SIZE 10
1302 if (tpm_suspend_pcr) {
1308 "extending dummy pcr before suspend");
1314 "sending savestate before suspend");
1334 #define TPM_GETRANDOM_RESULT_SIZE 18
1357 chip = tpm_chip_find_get(chip_num);
1365 tpm_cmd.
header.
in = tpm_getrandom_header;
1368 err = transmit_cmd(chip, &tpm_cmd,
1370 "attempting get random");
1380 }
while (
retries-- && total < max);
1382 return total ? total : -
EIO;
1393 if (chip->
vendor.release)
1406 static void tpm_dev_release(
struct device *
dev)
1430 #define DEVNAME_SIZE 7
1439 if (chip ==
NULL || devname ==
NULL)
1444 INIT_LIST_HEAD(&chip->
list);
1449 (
unsigned long)chip);
1456 dev_err(dev,
"No available tpm device numbers\n");
1458 }
else if (chip->dev_num == 0)
1463 set_bit(chip->dev_num, dev_mask);
1466 chip->vendor.miscdev.name = devname;
1468 chip->vendor.miscdev.parent =
dev;
1471 dev->
release = tpm_dev_release;
1476 "unable to misc_register %s, minor %d\n",
1477 chip->vendor.miscdev.name,
1478 chip->vendor.miscdev.minor);
1495 spin_lock(&driver_lock);
1496 list_add_rcu(&chip->list, &tpm_chip_list);
1497 spin_unlock(&driver_lock);