summaryrefslogtreecommitdiff
path: root/lib/tpm-v2.c
diff options
context:
space:
mode:
authorMiquel Raynal <miquel.raynal@bootlin.com>2018-05-15 11:57:16 +0200
committerTom Rini <trini@konsulko.com>2018-05-25 20:12:58 -0400
commit1c4ea8f496b42c5c34634d78524937476539a8bd (patch)
tree19941a966f3e7a1253b8afee1f52351e8723fa8c /lib/tpm-v2.c
parent6284be5a904ef808c939c3d926ee720d1b988264 (diff)
tpm: add TPM2_PCR_Read command support
Add support for the TPM2_PCR_Read command. Change the command file and the help accordingly. Signed-off-by: Miquel Raynal <miquel.raynal@bootlin.com> Reviewed-by: Simon Glass <sjg@chromium.org> Reviewed-by: Tom Rini <trini@konsulko.com>
Diffstat (limited to 'lib/tpm-v2.c')
-rw-r--r--lib/tpm-v2.c42
1 files changed, 42 insertions, 0 deletions
diff --git a/lib/tpm-v2.c b/lib/tpm-v2.c
index 476b4ee0d0..7d3834c0e4 100644
--- a/lib/tpm-v2.c
+++ b/lib/tpm-v2.c
@@ -115,3 +115,45 @@ u32 tpm2_pcr_extend(u32 index, const uint8_t *digest)
return tpm_sendrecv_command(command_v2, NULL, NULL);
}
+
+u32 tpm2_pcr_read(u32 idx, unsigned int idx_min_sz, void *data,
+ unsigned int *updates)
+{
+ u8 idx_array_sz = max(idx_min_sz, DIV_ROUND_UP(idx, 8));
+ u8 command_v2[COMMAND_BUFFER_SIZE] = {
+ tpm_u16(TPM2_ST_NO_SESSIONS), /* TAG */
+ tpm_u32(17 + idx_array_sz), /* Length */
+ tpm_u32(TPM2_CC_PCR_READ), /* Command code */
+
+ /* TPML_PCR_SELECTION */
+ tpm_u32(1), /* Number of selections */
+ tpm_u16(TPM2_ALG_SHA256), /* Algorithm of the hash */
+ idx_array_sz, /* Array size for selection */
+ /* bitmap(idx) Selected PCR bitmap */
+ };
+ size_t response_len = COMMAND_BUFFER_SIZE;
+ u8 response[COMMAND_BUFFER_SIZE];
+ unsigned int pcr_sel_idx = idx / 8;
+ u8 pcr_sel_bit = BIT(idx % 8);
+ unsigned int counter = 0;
+ int ret;
+
+ if (pack_byte_string(command_v2, COMMAND_BUFFER_SIZE, "b",
+ 17 + pcr_sel_idx, pcr_sel_bit))
+ return TPM_LIB_ERROR;
+
+ ret = tpm_sendrecv_command(command_v2, response, &response_len);
+ if (ret)
+ return ret;
+
+ if (unpack_byte_string(response, response_len, "ds",
+ 10, &counter,
+ response_len - TPM2_DIGEST_LEN, data,
+ TPM2_DIGEST_LEN))
+ return TPM_LIB_ERROR;
+
+ if (updates)
+ *updates = counter;
+
+ return 0;
+}