From b0c6f5bde64466a5eb137a50910b6817e3d4c77f Mon Sep 17 00:00:00 2001 From: Duncan Palmer Date: Fri, 9 Feb 2018 10:01:36 +1000 Subject: [PATCH] tpm2_nv(read|write): use a default buffer size if TPM reports 0. On a NUC5i7RYH, the TPM reports it's max NV buffer size as 0, which means we can neither read nor write. In this case, default to 512 as that is generally considered a safe size. Signed-off-by: William Roberts --- lib/tpm2_nv_util.h | 5 +++++ tools/tpm2_nvread.c | 3 +++ tools/tpm2_nvwrite.c | 3 +++ 3 files changed, 11 insertions(+) diff --git a/lib/tpm2_nv_util.h b/lib/tpm2_nv_util.h index 9e2b085df..9ff644832 100644 --- a/lib/tpm2_nv_util.h +++ b/lib/tpm2_nv_util.h @@ -36,6 +36,11 @@ #include "log.h" #include "tpm2_util.h" +/* + * The default buffer size when one cannot be determined via get capability. + */ +#define NV_DEFAULT_BUFFER_SIZE 512 + /** * Reads the public portion of a Non-Volatile (nv) index. * @param sapi_context diff --git a/tools/tpm2_nvread.c b/tools/tpm2_nvread.c index e71deda56..9d592f538 100644 --- a/tools/tpm2_nvread.c +++ b/tools/tpm2_nvread.c @@ -123,6 +123,9 @@ static bool nv_read(TSS2_SYS_CONTEXT *sapi_context, tpm2_option_flags flags) { if (max_data_size > MAX_NV_BUFFER_SIZE) { max_data_size = MAX_NV_BUFFER_SIZE; } + else if (max_data_size == 0) { + max_data_size = NV_DEFAULT_BUFFER_SIZE; + } UINT8 *data_buffer = malloc(data_size); if (!data_buffer) { diff --git a/tools/tpm2_nvwrite.c b/tools/tpm2_nvwrite.c index 278e73396..966404029 100644 --- a/tools/tpm2_nvwrite.c +++ b/tools/tpm2_nvwrite.c @@ -119,6 +119,9 @@ static bool nv_write(TSS2_SYS_CONTEXT *sapi_context) { if (max_data_size > MAX_NV_BUFFER_SIZE) { max_data_size = MAX_NV_BUFFER_SIZE; } + else if (max_data_size == 0) { + max_data_size = NV_DEFAULT_BUFFER_SIZE; + } UINT16 data_offset = 0; UINT16 bytes_left = ctx.nv_buffer.t.size;