summaryrefslogtreecommitdiffstats
path: root/drivers/hw_random
diff options
context:
space:
mode:
authorAhmad Fatoum <ahmad@a3f.at>2019-09-09 11:31:30 +0200
committerSascha Hauer <s.hauer@pengutronix.de>2019-09-09 15:23:42 +0200
commit9feb86e11d491981c0848a8923d0407d2f9abf0d (patch)
treebea1e5885737372d4b036ec7d771c9cb771960d9 /drivers/hw_random
parentc7d68aad12e739be7793b37ff4e02479883d7707 (diff)
downloadbarebox-9feb86e11d491981c0848a8923d0407d2f9abf0d.tar.gz
barebox-9feb86e11d491981c0848a8923d0407d2f9abf0d.tar.xz
hwrng: add sandbox driver interface to host /dev/random
Linux as well as other operating systems can provide /dev/random and /dev/urandom device to service userspace need for randomness. Add a driver to use /dev/random for blocking and /dev/urandom for non-blocking barebox random numbers. Signed-off-by: Ahmad Fatoum <ahmad@a3f.at> Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
Diffstat (limited to 'drivers/hw_random')
-rw-r--r--drivers/hw_random/Kconfig8
-rw-r--r--drivers/hw_random/Makefile1
-rw-r--r--drivers/hw_random/dev-random.c63
3 files changed, 72 insertions, 0 deletions
diff --git a/drivers/hw_random/Kconfig b/drivers/hw_random/Kconfig
index d1297a48d0..c57928204d 100644
--- a/drivers/hw_random/Kconfig
+++ b/drivers/hw_random/Kconfig
@@ -14,4 +14,12 @@ config HWRNG_MXC_RNGC
This driver provides kernel-side support for the Random Number
Generator hardware found on some Freescale i.MX processors.
+config HWRNG_DEV_RANDOM
+ tristate "Linux /dev/random and /dev/urandom RNG"
+ depends on SANDBOX
+ default y
+ help
+ This driver allows use of the host provided /dev/random
+ and /dev/urandom as barebox HWRNGs.
+
endif
diff --git a/drivers/hw_random/Makefile b/drivers/hw_random/Makefile
index 79c238c48d..8be62f38b7 100644
--- a/drivers/hw_random/Makefile
+++ b/drivers/hw_random/Makefile
@@ -1,2 +1,3 @@
obj-$(CONFIG_HWRNG) += core.o
obj-$(CONFIG_HWRNG_MXC_RNGC) += mxc-rngc.o
+obj-$(CONFIG_HWRNG_DEV_RANDOM) += dev-random.o
diff --git a/drivers/hw_random/dev-random.c b/drivers/hw_random/dev-random.c
new file mode 100644
index 0000000000..2170db7437
--- /dev/null
+++ b/drivers/hw_random/dev-random.c
@@ -0,0 +1,63 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Copyright (C) 2019 Ahmad Fatoum, Pengutronix
+ */
+
+#include <common.h>
+#include <init.h>
+#include <linux/hw_random.h>
+#include <mach/linux.h>
+
+struct devrandom_rnd {
+ struct hwrng hwrng;
+ devrandom_t *priv;
+};
+
+static inline struct devrandom_rnd *to_rnd(struct hwrng *hwrng)
+{
+ return container_of(hwrng, struct devrandom_rnd, hwrng);
+}
+
+static int devrandom_rnd_read(struct hwrng *hwrng, void *buf, size_t max, bool wait)
+{
+ return devrandom_read(to_rnd(hwrng)->priv, buf, max, wait);
+}
+
+static int devrandom_rnd_init(struct hwrng *hwrng)
+{
+ devrandom_t *devrandom = devrandom_init();
+ if (IS_ERR(devrandom))
+ return PTR_ERR(devrandom);
+
+ to_rnd(hwrng)->priv = devrandom;
+ return 0;
+}
+
+static int devrandom_rnd_probe(struct device_d *dev)
+{
+ struct devrandom_rnd *rnd;
+ int ret;
+
+ rnd = xzalloc(sizeof(*rnd));
+
+ rnd->hwrng.name = dev->name;
+ rnd->hwrng.read = devrandom_rnd_read;
+ rnd->hwrng.init = devrandom_rnd_init;
+
+ ret = hwrng_register(dev, &rnd->hwrng);
+ if (ret) {
+ dev_err(dev, "failed to register: %s\n", strerror(-ret));
+
+ return ret;
+ }
+
+ dev_info(dev, "Registered.\n");
+
+ return 0;
+}
+
+static struct driver_d devrandom_rnd_driver = {
+ .name = "devrandom",
+ .probe = devrandom_rnd_probe,
+};
+device_platform_driver(devrandom_rnd_driver);