U-Boot Archive on lore.kernel.org
 help / color / mirror / Atom feed
From: Ilias Apalodimas <ilias.apalodimas@linaro.org>
To: u-boot@lists.denx.de
Subject: [PATCH 08/11] tpm: Add an implementation of define_space
Date: Tue, 19 Jan 2021 10:24:45 +0200	[thread overview]
Message-ID: <YAaXTSQx6tEh64Zp@apalos.home> (raw)
In-Reply-To: <20210116215138.1218256-8-sjg@chromium.org>

Hi Simon,

On Sat, Jan 16, 2021 at 02:51:35PM -0700, Simon Glass wrote:
> Add support for this so that the TPM can be set up for use with
> Chromium OS verified boot.
> 
> Signed-off-by: Simon Glass <sjg@chromium.org>
> ---
> 
>  include/tpm-v2.h | 18 ++++++++++++++++++
>  lib/tpm-v2.c     | 44 ++++++++++++++++++++++++++++++++++++++++++++
>  2 files changed, 62 insertions(+)
> 
> diff --git a/include/tpm-v2.h b/include/tpm-v2.h
> index fab6b86ca2f..2766dc72a65 100644
> --- a/include/tpm-v2.h
> +++ b/include/tpm-v2.h
> @@ -238,6 +238,7 @@ enum tpm2_command_codes {
>  	TPM2_CC_CLEAR		= 0x0126,
>  	TPM2_CC_CLEARCONTROL	= 0x0127,
>  	TPM2_CC_HIERCHANGEAUTH	= 0x0129,
> +	TPM2_CC_NV_DEFINE_SPACE	= 0x012a,
>  	TPM2_CC_PCR_SETAUTHPOL	= 0x012C,
>  	TPM2_CC_DAM_RESET	= 0x0139,
>  	TPM2_CC_DAM_PARAMETERS	= 0x013A,
> @@ -386,6 +387,23 @@ u32 tpm2_self_test(struct udevice *dev, enum tpm2_yes_no full_test);
>  u32 tpm2_clear(struct udevice *dev, u32 handle, const char *pw,
>  	       const ssize_t pw_sz);
>  
> +/**
> + * Issue a TPM_NV_DefineSpace command
> + *
> + * This allows a space to be defined with given attributes and policy
> + *
> + * @dev			TPM device
> + * @space_index		index of the area
> + * @space_size		size of area in bytes
> + * @nv_attributes	TPM_NV_ATTRIBUTES of the area
> + * @nv_policy		policy to use
> + * @nv_policy_size	size of the policy
> + * @return return code of the operation
> + */
> +u32 tpm2_nv_define_space(struct udevice *dev, u32 space_index,
> +			 size_t space_size, u32 nv_attributes,
> +			 const u8 *nv_policy, size_t nv_policy_size);
> +
>  /**
>   * Issue a TPM2_PCR_Extend command.
>   *
> diff --git a/lib/tpm-v2.c b/lib/tpm-v2.c
> index b148081afd7..22c56d475c8 100644
> --- a/lib/tpm-v2.c
> +++ b/lib/tpm-v2.c
> @@ -80,6 +80,50 @@ u32 tpm2_clear(struct udevice *dev, u32 handle, const char *pw,
>  	return tpm_sendrecv_command(dev, command_v2, NULL, NULL);
>  }
>  
> +u32 tpm2_nv_define_space(struct udevice *dev, u32 space_index,
> +			 size_t space_size, u32 nv_attributes,
> +			 const u8 *nv_policy, size_t nv_policy_size)
> +{
> +	uint offset = 10 + 8 + 9 + 14;

Can we have a comment and/or link to the documentation for what the offsets
mean. I know we lack the overall structures in U-Boot to calculate this
properly, but at least let's add some explanation of the offset, so anyone
reading the code can get a hint what to look for.

> +	u8 command_v2[COMMAND_BUFFER_SIZE] = {
> +		/* header 10 bytes */
> +		tpm_u16(TPM2_ST_SESSIONS),	/* TAG */
> +		tpm_u32(offset + nv_policy_size),/* Length */
> +		tpm_u32(TPM2_CC_NV_DEFINE_SPACE),/* Command code */
> +
> +		/* handles 8 bytes */
> +		tpm_u32(TPM2_RH_PLATFORM),	/* Primary platform seed */
> +
> +		/* session header 9 bytes */
> +		tpm_u32(9),			/* Header size */
> +		tpm_u32(TPM2_RS_PW),		/* Password authorisation */
> +		tpm_u16(0),			/* nonce_size */
> +		0,				/* session_attrs */
> +		tpm_u16(0),			/* auth_size */
> +
> +		/* message 14 bytes + policy */
> +		tpm_u16(12 + nv_policy_size),	/* size */
> +		tpm_u32(space_index),
> +		tpm_u16(TPM2_ALG_SHA256),
> +		tpm_u32(nv_attributes),
> +		tpm_u16(nv_policy_size),
> +		/* nv_policy */
> +	};
> +	int ret;
> +
> +	/*
> +	 * Fill the command structure starting from the first buffer:
> +	 *     - the password (if any)
> +	 */
> +	ret = pack_byte_string(command_v2, sizeof(command_v2), "s",
> +			       offset, nv_policy, nv_policy_size);
> +	offset += nv_policy_size;
> +	if (ret)
> +		return TPM_LIB_ERROR;
> +
> +	return tpm_sendrecv_command(dev, command_v2, NULL, NULL);
> +}
> +
>  u32 tpm2_pcr_extend(struct udevice *dev, u32 index, u32 algorithm,
>  		    const u8 *digest, u32 digest_len)
>  {
> -- 
> 2.30.0.284.gd98b1dd5eaa7-goog
> 

Regards
/Ilias

  reply	other threads:[~2021-01-19  8:24 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-01-16 21:51 [PATCH 00/11] tpm: Support using TPM1 and TPM2 from a single API Simon Glass
2021-01-16 21:51 ` [PATCH 01/11] tpm: Don't include cr50 in TPL/SPL Simon Glass
2021-01-16 21:51 ` [PATCH 02/11] tpm: Use logging in the uclass Simon Glass
2021-01-16 21:51 ` [PATCH 03/11] tpm: Add debugging of request in tpm_sendrecv_command() Simon Glass
2021-01-16 21:51 ` [PATCH 04/11] tpm: Add an API that can support v1.2 and v2 Simon Glass
2021-01-16 21:51 ` [PATCH 05/11] tpm: Switch TPMv1 over to use the new API Simon Glass
2021-01-16 21:51 ` [PATCH 06/11] tpm: Add a basic API implementation for TPMv2 Simon Glass
2021-01-16 21:51 ` [PATCH 07/11] tpm: Reduce duplication in a few functions Simon Glass
2021-01-19  8:22   ` Ilias Apalodimas
2021-01-16 21:51 ` [PATCH 08/11] tpm: Add an implementation of define_space Simon Glass
2021-01-19  8:24   ` Ilias Apalodimas [this message]
2021-01-23 15:37     ` Simon Glass
2021-01-16 21:51 ` [PATCH 09/11] tpm: Add TPM2 support for read/write values Simon Glass
2021-01-16 21:51 ` [PATCH 10/11] tpm: Add TPM2 support for write_lock Simon Glass
2021-01-16 21:51 ` [PATCH 11/11] tpm: Allow disabling platform hierarchy with TPM2 Simon Glass

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=YAaXTSQx6tEh64Zp@apalos.home \
    --to=ilias.apalodimas@linaro.org \
    --cc=u-boot@lists.denx.de \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox