Extracting values from returned TPM buffers can be hard. Add cursor based (moving poiner) functions that make it easier to extract TPM returned values from a buffer. Signed-off-by: James Bottomley <James.Bottomley@xxxxxxxxxxxxxxxxxxxxx> --- v4: add kernel doc and reword commit --- drivers/char/tpm/tpm-buf.c | 48 ++++++++++++++++++++++++++++++++++++++ include/linux/tpm.h | 3 +++ 2 files changed, 51 insertions(+) diff --git a/drivers/char/tpm/tpm-buf.c b/drivers/char/tpm/tpm-buf.c index b7e42fb6266c..da0f6e725c3f 100644 --- a/drivers/char/tpm/tpm-buf.c +++ b/drivers/char/tpm/tpm-buf.c @@ -6,6 +6,8 @@ #include <linux/module.h> #include <linux/tpm.h> +#include <asm/unaligned.h> + static int __tpm_buf_init(struct tpm_buf *buf) { buf->data = (u8 *)__get_free_page(GFP_KERNEL); @@ -229,3 +231,49 @@ void tpm_buf_append_2b(struct tpm_buf *buf, struct tpm_buf *tpm2b) tpm_buf_reset_int(tpm2b); } EXPORT_SYMBOL_GPL(tpm_buf_append_2b); + +/* functions for unmarshalling data and moving the cursor */ + +/** + * tpm_get_inc_u8 - read a u8 and move pointer beyond it + * @ptr: pointer to pointer + * + * @return: value read + */ +u8 tpm_get_inc_u8(const u8 **ptr) +{ + return *((*ptr)++); +} +EXPORT_SYMBOL_GPL(tpm_get_inc_u8); + +/** + * tpm_get_inc_u16 - read a u16 and move pointer beyond it + * @ptr: pointer to pointer + * + * @return: value read (converted from big endian) + */ +u16 tpm_get_inc_u16(const u8 **ptr) +{ + u16 val; + + val = get_unaligned_be16(*ptr); + *ptr += sizeof(val); + return val; +} +EXPORT_SYMBOL_GPL(tpm_get_inc_u16); + +/** + * tpm_get_inc_u32 - read a u32 and move pointer beyond it + * @ptr: pointer to pointer + * + * @return: value read (converted from big endian) + */ +u32 tpm_get_inc_u32(const u8 **ptr) +{ + u32 val; + + val = get_unaligned_be32(*ptr); + *ptr += sizeof(val); + return val; +} +EXPORT_SYMBOL_GPL(tpm_get_inc_u32); diff --git a/include/linux/tpm.h b/include/linux/tpm.h index 76d495cb5b08..845eadfed715 100644 --- a/include/linux/tpm.h +++ b/include/linux/tpm.h @@ -336,6 +336,9 @@ void tpm_buf_append_u8(struct tpm_buf *buf, const u8 value); void tpm_buf_append_u16(struct tpm_buf *buf, const u16 value); void tpm_buf_append_u32(struct tpm_buf *buf, const u32 value); void tpm_buf_append_2b(struct tpm_buf *buf, struct tpm_buf *tpm2b); +u8 tpm_get_inc_u8(const u8 **ptr); +u16 tpm_get_inc_u16(const u8 **ptr); +u32 tpm_get_inc_u32(const u8 **ptr); /* * Check if TPM device is in the firmware upgrade mode. -- 2.35.3