[PATCH v1] rng: add dm_rng_read_default() helper

Heinrich Schuchardt xypron.glpk at gmx.de
Wed Nov 1 15:31:19 CET 2023


On 11/1/23 15:49, Alexey Romanov wrote:
> Add dm_rng_read_default() function, which obtain a series
> of random bytes. In some cases, such function would be
> useful because it allows the caller to abstract away from
> RNG device.
>
> Signed-off-by: Alexey Romanov <avromanov at salutedevices.com>
> ---
>   drivers/rng/rng-uclass.c | 20 ++++++++++++++++++++
>   include/rng.h            | 10 ++++++++++
>   2 files changed, 30 insertions(+)
>
> diff --git a/drivers/rng/rng-uclass.c b/drivers/rng/rng-uclass.c
> index 53108e1b31..3a8fb7d276 100644
> --- a/drivers/rng/rng-uclass.c
> +++ b/drivers/rng/rng-uclass.c
> @@ -19,6 +19,26 @@ int dm_rng_read(struct udevice *dev, void *buffer, size_t size)
>   	return ops->read(dev, buffer, size);
>   }
>
> +int dm_rng_read_default(void *buffer, size_t size)
> +{
> +	struct udevice *rng;
> +	int ret;
> +
> +	ret = uclass_get_device(UCLASS_RNG, 0, &rng);

This function will give you the first RNG device and not the first
successfully probed RNG device.

We already have a weak function platform_get_rng_device() which allows a
board to provide a different device. I think we should move
platform_get_rng_device() to rng-uclass.c and use it here.

platform_get_rng_device should be re-implemented to provide the first
successfully probed RNG device.

> +	if (ret) {
> +		pr_err("Can't get RNG device (%d)\n", ret);
> +		return ret;
> +	}
> +
> +	ret = dm_rng_read(rng, buffer, size);

Here you are using a device which may not have been probed yet.

Best regards

Heinrich

> +	if (ret) {
> +		pr_err("Can't read from RNG device (%d)\n", ret);
> +		return ret;
> +	}
> +
> +	return 0;
> +}
> +
>   UCLASS_DRIVER(rng) = {
>   	.name = "rng",
>   	.id = UCLASS_RNG,
> diff --git a/include/rng.h b/include/rng.h
> index 37af554363..5537daae88 100644
> --- a/include/rng.h
> +++ b/include/rng.h
> @@ -20,6 +20,16 @@ struct udevice;
>    */
>   int dm_rng_read(struct udevice *dev, void *buffer, size_t size);
>
> +/**
> + * dm_rng_read_default() - same as dm_rng_read(), except that caller
> + * don't need to pass an argument with RNG udevice.
> + * @buffer:	input buffer to put the read random seed into
> + * @size:	number of bytes of random seed read
> + *
> + * Return: 0 if OK, -ve on error
> + */
> +int dm_rng_read_default(void *buffer, size_t size);
> +
>   /**
>    * struct dm_rng_ops - operations for the hwrng uclass
>    *



More information about the U-Boot mailing list