aboutsummaryrefslogtreecommitdiff
path: root/drivers/rng
diff options
context:
space:
mode:
authorSughosh Ganu <sughosh.ganu@linaro.org>2019-12-28 23:58:31 +0530
committerHeinrich Schuchardt <xypron.glpk@gmx.de>2020-01-07 18:08:21 +0100
commitff0dada9b863ab33a7c4b2d9955db1205f55cd01 (patch)
treeaa89525c53517b97226b57001ef32046706f64e3 /drivers/rng
parent90950eec58f7b550db91588144e0e9cbaf9a49b7 (diff)
downloadu-boot-ff0dada9b863ab33a7c4b2d9955db1205f55cd01.zip
u-boot-ff0dada9b863ab33a7c4b2d9955db1205f55cd01.tar.gz
u-boot-ff0dada9b863ab33a7c4b2d9955db1205f55cd01.tar.bz2
sandbox: rng: Add a random number generator(rng) driver
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> Reviewed-by: Simon Glass <sjg@chromium.org>
Diffstat (limited to 'drivers/rng')
-rw-r--r--drivers/rng/Kconfig8
-rw-r--r--drivers/rng/Makefile1
-rw-r--r--drivers/rng/sandbox_rng.c56
3 files changed, 65 insertions, 0 deletions
diff --git a/drivers/rng/Kconfig b/drivers/rng/Kconfig
index c9c4751..35a3bd1 100644
--- a/drivers/rng/Kconfig
+++ b/drivers/rng/Kconfig
@@ -6,6 +6,14 @@ 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
+ select CONFIG_LIB_RAND
+ 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..cd0b0ac
--- /dev/null
+++ b/drivers/rng/sandbox_rng.c
@@ -0,0 +1,56 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (c) 2019, Linaro Limited
+ */
+
+#include <common.h>
+#include <dm.h>
+#include <rng.h>
+
+#include <linux/string.h>
+
+static int sandbox_rng_read(struct udevice *dev, void *data, size_t len)
+{
+ unsigned int i, seed, random;
+ unsigned char *buf = data;
+ size_t nrem, nloops;
+
+ if (!len)
+ return 0;
+
+ nloops = len / sizeof(random);
+ seed = get_timer(0) ^ rand();
+ srand(seed);
+
+ for (i = 0, nrem = len; i < nloops; i++) {
+ random = rand();
+ memcpy(buf, &random, sizeof(random));
+ buf += sizeof(random);
+ nrem -= sizeof(random);
+ }
+
+ if (nrem) {
+ random = rand();
+ memcpy(buf, &random, nrem);
+ }
+
+ return 0;
+}
+
+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,
+};