[PATCH v5 1/8] dm: rng: Add random number generator(rng) uclass

Sughosh Ganu sughosh.ganu at linaro.org
Fri Dec 27 12:38:49 CET 2019


On Fri, 27 Dec 2019 at 12:39, Heinrich Schuchardt <xypron.glpk at gmx.de>
wrote:

> On 12/26/19 6:25 PM, Sughosh Ganu wrote:
> > Add a uclass for reading a random number seed from a random number
> > generator device.
> >
> > Signed-off-by: Sughosh Ganu <sughosh.ganu at linaro.org>
> > Reviewed-by: Patrice Chotard <patrice.chotard at st.com>
> > ---
> > Changes since V4:
> > * Use Sphinx syntax for Return value in the read function
> > * Return number of bytes read on a successful read, and a -ve value on
> >    error.
> >
> >   drivers/Kconfig          |  2 ++
> >   drivers/Makefile         |  1 +
> >   drivers/rng/Kconfig      |  7 +++++++
> >   drivers/rng/Makefile     |  6 ++++++
> >   drivers/rng/rng-uclass.c | 23 +++++++++++++++++++++++
> >   include/dm/uclass-id.h   |  1 +
> >   include/rng.h            | 33 +++++++++++++++++++++++++++++++++
> >   7 files changed, 73 insertions(+)
> >   create mode 100644 drivers/rng/Kconfig
> >   create mode 100644 drivers/rng/Makefile
> >   create mode 100644 drivers/rng/rng-uclass.c
> >   create mode 100644 include/rng.h
>

<snip>


> > diff --git a/include/rng.h b/include/rng.h
> > new file mode 100644
> > index 0000000..9a71e81
> > --- /dev/null
> > +++ b/include/rng.h
> > @@ -0,0 +1,33 @@
> > +// SPDX-License-Identifier: GPL-2.0+
> > +/*
> > + * Copyright (c) 2019, Linaro Limited
> > + */
> > +
> > +#if !defined _RNG_H_
> > +#define _RNG_H_
> > +
> > +#include <dm.h>
> > +
> > +/**
> > + * dm_rng_read() - read a random number seed from the rng device
> > + * @buffer:  input buffer to put the read random seed into
> > + * @size:    number of bytes of random seed read
> > + *
> > + * Return: number of bytes read if OK, -ve on error
> > + */
> > +int dm_rng_read(struct udevice *dev, void *buffer, size_t size);
> > +
> > +/* struct dm_rng_ops - Operations for the hwrng uclass */
> > +struct dm_rng_ops {
> > +     /**
> > +      * @read() - read a random number seed
> > +      *
> > +      * @data:       input buffer to read the random seed
> > +      * @max:        total number of bytes to read
> > +      *
> > +      * Return: number of bytes read if OK, -ve on error
> > +      */
> > +     int (*read)(struct udevice *dev, void *data, size_t max);
>
> With this interface definition the caller has to check the return value
> and to call read() again and again until all bytes needed are requested.
> The EFI_RNG_PROTOCOL will not be the only consumer of the hardware RNG
> so we will have to rewrite that logic in multiple places. In U-Boot we
> are very tight on the binary size. So I would really appreciate to avoid
> such code duplication by making it the drivers duty to return exactly
> the number of bytes requested.
>

Ok. I had tested the code with the loop inside the virtio driver
yesterday,  and with more than max-bytes requested, the system would freeze
for a pretty longish bit before returning the data. I mistook this for a
system hang, which is why I moved the loop out of the driver. Testing
again, after putting the loop inside the driver, i see that this works. I
will send an updated version with the loop inside the driver as you ask.
The read function will return 0 on a successful read, and a -ve error on
failure.

-sughosh


More information about the U-Boot mailing list