Re: [PATCH v3 07/16] tpm: access command header through struct in tpm_try_transmit()

Jarkko Sakkinen
2018-11-06T06:08:49Z

On Mon, Nov 05, 2018 at 05:26:30PM -0500, Stefan Berger wrote:
> On 11/4/18 8:45 PM, Jarkko Sakkinen wrote:
> > Instead of accessing fields of the command header through offsets to
> > the raw buffer, it is a better idea to use the header struct pointer
> > that is already used elsewhere in the function.
> >
> > Signed-off-by: Jarkko Sakkinen <jarkko.sakkinen@linux.intel.com>
> > ---
> > drivers/char/tpm/tpm-interface.c | 4 ++--
> > 1 file changed, 2 insertions(+), 2 deletions(-)
> >
> > diff --git a/drivers/char/tpm/tpm-interface.c b/drivers/char/tpm/tpm-interface.c
> > index 0f343407daf8..422e3bb0bd3d 100644
> > --- a/drivers/char/tpm/tpm-interface.c
> > +++ b/drivers/char/tpm/tpm-interface.c
> > @@ -190,8 +190,8 @@ static ssize_t tpm_try_transmit(struct tpm_chip *chip,
> > if (bufsiz > TPM_BUFSIZE)
> > bufsiz = TPM_BUFSIZE;
> >
> > - count = be32_to_cpu(*((__be32 *) (buf + 2)));
> > - ordinal = be32_to_cpu(*((__be32 *) (buf + 6)));
> > + count = be32_to_cpu(header->length);
> > + ordinal = be32_to_cpu(header->return_code);
>
> Hm. This should use the proper type of header and use in_header->ordinal.

Well, the fuction has output header already declared. What I could do
as a prequel commit is to take these:

struct tpm_input_header {
__be16 tag;
__be32 length;
__be32 ordinal;
} __packed;

struct tpm_output_header {
__be16 tag;
__be32 length;
__be32 return_code;
} __packed;

and replace them with this:

struct tpm_header {
__be16 tag;
__be32 length;
union {
__be32 ordinal;
__be32 return_code;
};
} __packed;

/Jarkko