Skip to content

Commit 1db1534

Browse files
naynajainJarkko Sakkinen
authored andcommitted
tpm: implement TPM 2.0 capability to get active PCR banks
This patch implements the TPM 2.0 capability TPM_CAP_PCRS to retrieve the active PCR banks from the TPM. This is needed to enable extending all active banks as recommended by TPM 2.0 TCG Specification. Signed-off-by: Nayna Jain <nayna@linux.vnet.ibm.com> Reviewed-by: Jarkko Sakkinen <jarkko.sakkinen@linux.intel.com> Tested-by: Jarkko Sakkinen <jarkko.sakkinen@linux.intel.com> Tested-by: Kenneth Goldman <kgold@linux.vnet.ibm.com> Signed-off-by: Jarkko Sakkinen <jarkko.sakkinen@linux.intel.com>
1 parent 7d76111 commit 1db1534

File tree

2 files changed

+82
-0
lines changed

2 files changed

+82
-0
lines changed

drivers/char/tpm/tpm.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ enum tpm2_return_codes {
9797
};
9898

9999
enum tpm2_algorithms {
100+
TPM2_ALG_ERROR = 0x0000,
100101
TPM2_ALG_SHA1 = 0x0004,
101102
TPM2_ALG_KEYEDHASH = 0x0008,
102103
TPM2_ALG_SHA256 = 0x000B,
@@ -127,6 +128,7 @@ enum tpm2_permanent_handles {
127128
};
128129

129130
enum tpm2_capabilities {
131+
TPM2_CAP_PCRS = 5,
130132
TPM2_CAP_TPM_PROPERTIES = 6,
131133
};
132134

@@ -187,6 +189,8 @@ struct tpm_chip {
187189

188190
const struct attribute_group *groups[3];
189191
unsigned int groups_cnt;
192+
193+
u16 active_banks[7];
190194
#ifdef CONFIG_ACPI
191195
acpi_handle acpi_dev_handle;
192196
char ppi_version[TPM_PPI_VERSION_LEN + 1];
@@ -540,4 +544,5 @@ int tpm2_auto_startup(struct tpm_chip *chip);
540544
void tpm2_shutdown(struct tpm_chip *chip, u16 shutdown_type);
541545
unsigned long tpm2_calc_ordinal_duration(struct tpm_chip *chip, u32 ordinal);
542546
int tpm2_probe(struct tpm_chip *chip);
547+
ssize_t tpm2_get_pcr_allocation(struct tpm_chip *chip);
543548
#endif

drivers/char/tpm/tpm2-cmd.c

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1030,3 +1030,80 @@ int tpm2_auto_startup(struct tpm_chip *chip)
10301030
rc = -ENODEV;
10311031
return rc;
10321032
}
1033+
1034+
struct tpm2_pcr_selection {
1035+
__be16 hash_alg;
1036+
u8 size_of_select;
1037+
u8 pcr_select[3];
1038+
} __packed;
1039+
1040+
/**
1041+
* tpm2_get_pcr_allocation() - get TPM active PCR banks.
1042+
*
1043+
* @chip: TPM chip to use.
1044+
*
1045+
* Return: Same as with tpm_transmit_cmd.
1046+
*/
1047+
ssize_t tpm2_get_pcr_allocation(struct tpm_chip *chip)
1048+
{
1049+
struct tpm2_pcr_selection pcr_selection;
1050+
struct tpm_buf buf;
1051+
void *marker;
1052+
void *end;
1053+
void *pcr_select_offset;
1054+
unsigned int count;
1055+
u32 sizeof_pcr_selection;
1056+
u32 rsp_len;
1057+
int rc;
1058+
int i = 0;
1059+
1060+
rc = tpm_buf_init(&buf, TPM2_ST_NO_SESSIONS, TPM2_CC_GET_CAPABILITY);
1061+
if (rc)
1062+
return rc;
1063+
1064+
tpm_buf_append_u32(&buf, TPM2_CAP_PCRS);
1065+
tpm_buf_append_u32(&buf, 0);
1066+
tpm_buf_append_u32(&buf, 1);
1067+
1068+
rc = tpm_transmit_cmd(chip, buf.data, PAGE_SIZE, 9, 0,
1069+
"get tpm pcr allocation");
1070+
if (rc)
1071+
goto out;
1072+
1073+
count = be32_to_cpup(
1074+
(__be32 *)&buf.data[TPM_HEADER_SIZE + 5]);
1075+
1076+
if (count > ARRAY_SIZE(chip->active_banks)) {
1077+
rc = -ENODEV;
1078+
goto out;
1079+
}
1080+
1081+
marker = &buf.data[TPM_HEADER_SIZE + 9];
1082+
1083+
rsp_len = be32_to_cpup((__be32 *)&buf.data[2]);
1084+
end = &buf.data[rsp_len];
1085+
1086+
for (i = 0; i < count; i++) {
1087+
pcr_select_offset = marker +
1088+
offsetof(struct tpm2_pcr_selection, size_of_select);
1089+
if (pcr_select_offset >= end) {
1090+
rc = -EFAULT;
1091+
break;
1092+
}
1093+
1094+
memcpy(&pcr_selection, marker, sizeof(pcr_selection));
1095+
chip->active_banks[i] = be16_to_cpu(pcr_selection.hash_alg);
1096+
sizeof_pcr_selection = sizeof(pcr_selection.hash_alg) +
1097+
sizeof(pcr_selection.size_of_select) +
1098+
pcr_selection.size_of_select;
1099+
marker = marker + sizeof_pcr_selection;
1100+
}
1101+
1102+
out:
1103+
if (i < ARRAY_SIZE(chip->active_banks))
1104+
chip->active_banks[i] = TPM2_ALG_ERROR;
1105+
1106+
tpm_buf_destroy(&buf);
1107+
1108+
return rc;
1109+
}

0 commit comments

Comments
 (0)