public inbox for u-boot@lists.denx.de
 help / color / mirror / Atom feed
From: Miquel Raynal <miquel.raynal@bootlin.com>
To: u-boot@lists.denx.de
Subject: [U-Boot] [PATCH 3/6] tpm: allow TPM v1 and v2 to be compiled at the same time
Date: Thu, 19 Jul 2018 22:31:27 +0200	[thread overview]
Message-ID: <20180719223127.6e24da32@xps13> (raw)
In-Reply-To: <CAPnjgZ2j-ojrea18L5uY0LFAMDhoMtprcxT=iMp4=2dSEkU_8w@mail.gmail.com>

Hi Simon,

Simon Glass <sjg@chromium.org> wrote on Wed, 18 Jul 2018 19:31:41 -0600:

> Hi Miquel,
> 
> On 14 July 2018 at 06:16, Miquel Raynal <miquel.raynal@bootlin.com> wrote:
> > While there is probably no reason to do so in a real life situation, it
> > will allow to compile test both stacks with the same sandbox defconfig.
> >
> > As we cannot define two 'tpm' commands at the same time, the command for
> > TPM v1 is still called 'tpm' and the one for TPM v2 'tpm2'. While this
> > is the exact command name that must be written into eg. test files, any
> > user already using the TPM v2 stack can continue using just 'tpm' as
> > command as long as TPM v1 support is not compiled (because U-Boot prompt
> > will search for the closest command named after 'tpm'.b)
> >
> > The command set can also be changed at runtime (not supported yet, but
> > ready to be), but as one can compile only either one stack or the other,
> > there is still one spot in the code where conditionals are used: to
> > retrieve the v1 or v2 command set.
> >
> > Signed-off-by: Miquel Raynal <miquel.raynal@bootlin.com>
> > ---
> >  cmd/tpm-common.c               | 24 +++++++++++++++++++++++-
> >  cmd/tpm-v1.c                   |  2 +-
> >  cmd/tpm-v2.c                   |  4 ++--
> >  drivers/tpm/Kconfig            |  7 ++-----
> >  drivers/tpm/tpm-uclass.c       | 13 ++++++++++---
> >  drivers/tpm/tpm2_tis_sandbox.c | 11 +++++++++++
> >  drivers/tpm/tpm2_tis_spi.c     | 15 +++++++++++++++
> >  include/tpm-common.h           | 36 ++++++++++++++++++++++++++++++------
> >  lib/tpm-common.c               |  4 ++++
> >  lib/tpm-utils.h                |  9 +++++++++
> >  10 files changed, 107 insertions(+), 18 deletions(-)
> >  
> 
> Reviewed-by: Simon Glass <sjg@chromium.org>
> 
> Some comments below.
> 
> > diff --git a/cmd/tpm-common.c b/cmd/tpm-common.c
> > index 6cf9fcc9ac..69cc02b599 100644
> > --- a/cmd/tpm-common.c
> > +++ b/cmd/tpm-common.c
> > @@ -273,12 +273,34 @@ int do_tpm_init(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
> >  int do_tpm(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
> >  {
> >         cmd_tbl_t *tpm_commands, *cmd;
> > +       struct tpm_chip_priv *priv;
> > +       struct udevice *dev;
> >         unsigned int size;
> > +       int rc;  
> 
> Can we use 'ret'? That is more common.

Of course.

> 
> >
> >         if (argc < 2)
> >                 return CMD_RET_USAGE;
> >
> > -       tpm_commands = get_tpm_commands(&size);
> > +       rc = get_tpm(&dev);
> > +       if (rc)
> > +               return rc;
> > +
> > +       priv = dev_get_uclass_priv(dev);
> > +
> > +       switch (priv->version) {
> > +#if defined(CONFIG_TPM_V1)
> > +       case TPM_V1:  
> 
> if (IS_ENABLED(CONFIG_TPM_V1))
> 
> (I think #ifdef is worse)

Actually, in the present state, get_tpmx_commands symbol is only
available if the file if TPM_Vx is selected in Kconfig.

I created two dummy functions in tpm-common.h which return NULL for
each symbol if their respective TPM version is not compiled. This way it
removes any conditionals in the code.

> 
> > +               tpm_commands = get_tpm1_commands(&size);
> > +               break;
> > +#endif
> > +#if defined(CONFIG_TPM_V2)
> > +       case TPM_V2:
> > +               tpm_commands = get_tpm2_commands(&size);
> > +               break;
> > +#endif
> > +       default:
> > +               return CMD_RET_USAGE;
> > +       }

[...]

> >
> > +static int sandbox_tpm2_set_version(struct udevice *dev)
> > +{
> > +       struct tpm_chip_priv *priv = dev_get_uclass_priv(dev);
> > +
> > +       /* Use the TPM v2 stack */
> > +       priv->version = TPM_V2;  
> 
> Don't you think this could be done in the probe() method?

When I first wrote this I faced some issues. As I did not remembered
why it would fail I tried again and indeed, moving this
selection in the probe works.

> 
> > +
> > +       return 0;
> > +}
> > +

[...]

> > @@ -65,6 +78,16 @@ struct tpm_chip_priv {
> >   * to bytes which are sent and received.
> >   */
> >  struct tpm_ops {
> > +       /**
> > +        * set_version() - Set the right TPM stack version to use
> > +        *
> > +        * Default is TPM_V1. TPM of newer versions can implement this
> > +        * optional hook to set another value.
> > +        *
> > +        * @dev:        TPM device
> > +        */
> > +       int (*set_version)(struct udevice *dev);  
> 
> I think this could just be a helper function provided by the uclass,
> rather than a device method.

Actually there is no need for this hook or any helper anymore, I
removed them all as the version selection is done in the driver
->probe().

I'll let other people that would need to change the version at runtime
adding support for that if they want, it should not be too tricky
anyway.

Thanks,
Miquèl

  reply	other threads:[~2018-07-19 20:31 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-07-14 12:16 [U-Boot] [PATCH 0/6] Allow both TPM stacks to be compiled at the same time Miquel Raynal
2018-07-14 12:16 ` [U-Boot] [PATCH 1/6] tpm: fix typo in kernel doc Miquel Raynal
2018-07-19  1:31   ` Simon Glass
2018-07-14 12:16 ` [U-Boot] [PATCH 2/6] tpm: compile Sandbox driver by default Miquel Raynal
2018-07-19  1:31   ` Simon Glass
2018-07-14 12:16 ` [U-Boot] [PATCH 3/6] tpm: allow TPM v1 and v2 to be compiled at the same time Miquel Raynal
2018-07-19  1:31   ` Simon Glass
2018-07-19 20:31     ` Miquel Raynal [this message]
2018-07-14 12:16 ` [U-Boot] [PATCH 4/6] test/py: tpm2: switch from 'tpm' to 'tpm2' command Miquel Raynal
2018-07-19  1:31   ` Simon Glass
2018-07-14 12:16 ` [U-Boot] [PATCH 5/6] tpm: make TPM_V2 be compiled by default Miquel Raynal
2018-07-19  1:31   ` Simon Glass
2018-07-14 12:16 ` [U-Boot] [PATCH 6/6] sandbox: compile both TPM stack versions and drivers Miquel Raynal
2018-07-19  1:31   ` 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=20180719223127.6e24da32@xps13 \
    --to=miquel.raynal@bootlin.com \
    --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