All of lore.kernel.org
 help / color / mirror / Atom feed
From: Patrick DELAUNAY <patrick.delaunay@st.com>
To: u-boot@lists.denx.de
Subject: [PATCH v3 5/8] sandbox: rng: Add a random number generator(rng) driver
Date: Mon, 16 Dec 2019 12:30:35 +0000	[thread overview]
Message-ID: <33d09c289aad421db3f1fb4b04d07a6a@SFHDAG6NODE3.st.com> (raw)
In-Reply-To: <1576221267-5948-6-git-send-email-sughosh.ganu@linaro.org>

Hi,

> From: U-Boot <u-boot-bounces@lists.denx.de> On Behalf Of Sughosh Ganu
> Sent: vendredi 13 décembre 2019 08:14
> 
> Add a sandbox driver for random number generation. Mostly aimed at providing a
> unit test for rng uclass.
> 
> Signed-off-by: Sughosh Ganu <sughosh.ganu@linaro.org>
> Reviewed-by: Patrice Chotard <patrice.chotard@st.com>
> ---
>  arch/sandbox/dts/test.dts |  4 ++++
>  drivers/rng/Kconfig       |  7 +++++++
>  drivers/rng/Makefile      |  1 +
>  drivers/rng/sandbox_rng.c | 36 ++++++++++++++++++++++++++++++++++++
>  4 files changed, 48 insertions(+)
>  create mode 100644 drivers/rng/sandbox_rng.c
> 
> diff --git a/arch/sandbox/dts/test.dts b/arch/sandbox/dts/test.dts index
> fdb08f2..2c85540 100644
> --- a/arch/sandbox/dts/test.dts
> +++ b/arch/sandbox/dts/test.dts
> @@ -599,6 +599,10 @@
>  		reset-names = "other", "test";
>  	};
> 
> +	rng at 0 {
> +		compatible = "sandbox,sandbox-rng";
> +	};
> +
>  	rproc_1: rproc at 1 {
>  		compatible = "sandbox,test-processor";
>  		remoteproc-name = "remoteproc-test-dev1"; diff --git
> a/drivers/rng/Kconfig b/drivers/rng/Kconfig index 5fc11db..3a1d3f0 100644
> --- a/drivers/rng/Kconfig
> +++ b/drivers/rng/Kconfig
> @@ -6,6 +6,13 @@ config DM_RNG
>  	  This interface is used to initialise the rng device and to
>  	  read the random seed from the device.
> 
> +config RNG_SANDBOX
> +       bool "Sandbox random number generator"
> +       depends on SANDBOX && DM_RNG
> +       help
> +         Enable random number generator for sandbox. This is an
> +	 emulation of a rng device.
> +
>  config RNG_STM32MP1
>         bool "Enable random number generator for STM32MP1"
>         depends on ARCH_STM32MP && DM_RNG diff --git a/drivers/rng/Makefile
> b/drivers/rng/Makefile index 699beb3..3517005 100644
> --- a/drivers/rng/Makefile
> +++ b/drivers/rng/Makefile
> @@ -4,4 +4,5 @@
>  #
> 
>  obj-$(CONFIG_DM_RNG) += rng-uclass.o
> +obj-$(CONFIG_RNG_SANDBOX) += sandbox_rng.o
>  obj-$(CONFIG_RNG_STM32MP1) += stm32mp1_rng.o diff --git
> a/drivers/rng/sandbox_rng.c b/drivers/rng/sandbox_rng.c new file mode 100644
> index 0000000..c5be552
> --- /dev/null
> +++ b/drivers/rng/sandbox_rng.c
> @@ -0,0 +1,36 @@
> +// SPDX-License-Identifier: GPL-2.0-or-later
> +/*
> + * Copyright (c) 2019, Linaro Limited
> + */
> +
> +#include <common.h>
> +#include <dm.h>
> +#include <rng.h>
> +
> +static unsigned long random = 0xdeadbeef;
> +
> +static int sandbox_rng_read(struct udevice *dev, void *data, size_t
> +len) {

Add protection on length  I think: 
	If (len != sizeof(random))
		retrun - EINVAL;

or treat the case len > 4 with loop ?

> +	random ^= ~0UL;
> +	*(unsigned long *)data = random;
> +
> +	return sizeof(random);

Read is OK, so I think the correct return value is 0:

	return 0;

NB: result (int) can be not enough to return the value read (size_t)

PS: it not really a random generator here but simple sequence
       0xdeadbeef -> 0x21524110 -> 0xdeadbeef

      It is enough for unitary test, but is is enough for sandbox ?
      we could reused PRNG code from lib/rand.c ?

> +}
> +
> +static const struct dm_rng_ops sandbox_rng_ops = {
> +	.read = sandbox_rng_read,
> +};
> +
> +static const struct udevice_id sandbox_rng_match[] = {
> +	{
> +		.compatible = "sandbox,sandbox-rng",
> +	},
> +	{},
> +};
> +
> +U_BOOT_DRIVER(sandbox_rng) = {
> +	.name = "sandbox-rng",
> +	.id = UCLASS_RNG,
> +	.of_match = sandbox_rng_match,
> +	.ops = &sandbox_rng_ops,
> +};
> --
> 2.7.4

  reply	other threads:[~2019-12-16 12:30 UTC|newest]

Thread overview: 22+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-12-13  7:14 [PATCH v3 0/8] Add a random number generator uclass Sughosh Ganu
2019-12-13  7:14 ` [PATCH v3 1/8] dm: rng: Add random number generator(rng) uclass Sughosh Ganu
2019-12-13 12:21   ` Ilias Apalodimas
2019-12-16 11:59   ` Patrick DELAUNAY
2019-12-16 12:13   ` Patrick DELAUNAY
2019-12-16 18:55     ` Sughosh Ganu
2019-12-13  7:14 ` [PATCH v3 2/8] clk: stm32mp1: Add a clock entry for RNG1 device Sughosh Ganu
2019-12-16  8:55   ` Patrick DELAUNAY
2019-12-13  7:14 ` [PATCH v3 3/8] stm32mp1: rng: Add a driver for random number generator(rng) device Sughosh Ganu
2019-12-16 11:58   ` Patrick DELAUNAY
2019-12-13  7:14 ` [PATCH v3 4/8] configs: stm32mp15: Enable " Sughosh Ganu
2019-12-16 12:00   ` Patrick DELAUNAY
2019-12-13  7:14 ` [PATCH v3 5/8] sandbox: rng: Add a random number generator(rng) driver Sughosh Ganu
2019-12-16 12:30   ` Patrick DELAUNAY [this message]
2019-12-16 18:59     ` Sughosh Ganu
2019-12-13  7:14 ` [PATCH v3 6/8] configs: sandbox: Enable random number generator(rng) device Sughosh Ganu
2019-12-16 12:07   ` Patrick DELAUNAY
2019-12-13  7:14 ` [PATCH v3 7/8] test: rng: Add basic test for random number generator(rng) uclass Sughosh Ganu
2019-12-16 12:42   ` Patrick DELAUNAY
2019-12-16 19:01     ` Sughosh Ganu
2019-12-13  7:14 ` [PATCH v3 8/8] virtio: rng: Add a random number generator(rng) driver Sughosh Ganu
2019-12-13 12:21   ` Ilias Apalodimas

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=33d09c289aad421db3f1fb4b04d07a6a@SFHDAG6NODE3.st.com \
    --to=patrick.delaunay@st.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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.