mail archive of the barebox mailing list
 help / color / mirror / Atom feed
From: Ahmad Fatoum <ahmad@a3f.at>
To: barebox@lists.infradead.org
Subject: [PATCH v2 3/3] hwrng: add sandbox driver interface to host /dev/random
Date: Mon,  9 Sep 2019 11:31:30 +0200	[thread overview]
Message-ID: <20190909093130.30666-4-ahmad@a3f.at> (raw)
In-Reply-To: <20190909093130.30666-1-ahmad@a3f.at>

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>
---
 arch/sandbox/board/Makefile                   |  1 +
 arch/sandbox/board/board.c                    |  7 +++
 arch/sandbox/board/dev-random.c               | 24 +++++++
 .../sandbox/mach-sandbox/include/mach/linux.h |  7 +++
 drivers/hw_random/Kconfig                     |  8 +++
 drivers/hw_random/Makefile                    |  1 +
 drivers/hw_random/dev-random.c                | 63 +++++++++++++++++++
 7 files changed, 111 insertions(+)
 create mode 100644 arch/sandbox/board/dev-random.c
 create mode 100644 drivers/hw_random/dev-random.c

diff --git a/arch/sandbox/board/Makefile b/arch/sandbox/board/Makefile
index b6c271c858c6..26f6cb192269 100644
--- a/arch/sandbox/board/Makefile
+++ b/arch/sandbox/board/Makefile
@@ -5,5 +5,6 @@ obj-y += console.o
 obj-y += devices.o
 obj-y += dtb.o
 obj-y += poweroff.o
+obj-y += dev-random.o
 
 extra-y += barebox.lds
diff --git a/arch/sandbox/board/board.c b/arch/sandbox/board/board.c
index 1f6392d15ed3..ad2bc910c342 100644
--- a/arch/sandbox/board/board.c
+++ b/arch/sandbox/board/board.c
@@ -42,6 +42,11 @@ static struct device_d sdl_device = {
 	.platform_data = &mode,
 };
 
+static struct device_d devrandom_device = {
+	.id	  = DEVICE_ID_DYNAMIC,
+	.name     = "devrandom",
+};
+
 static int devices_init(void)
 {
 	platform_device_register(&tap_device);
@@ -54,6 +59,8 @@ static int devices_init(void)
 
 	platform_device_register(&sdl_device);
 
+	platform_device_register(&devrandom_device);
+
 	return 0;
 }
 
diff --git a/arch/sandbox/board/dev-random.c b/arch/sandbox/board/dev-random.c
new file mode 100644
index 000000000000..f65e5ef6e5ed
--- /dev/null
+++ b/arch/sandbox/board/dev-random.c
@@ -0,0 +1,24 @@
+#include <common.h>
+#include <mach/linux.h>
+
+devrandom_t *devrandom_init(void) {
+	devrandom_t *fds = xzalloc(sizeof(*fds));
+
+	fds->randomfd = linux_open("/dev/random", false);
+	if (fds->randomfd < 0)
+		return ERR_PTR(-EPERM);
+
+	fds->urandomfd = linux_open("/dev/urandom", false);
+	if (fds->urandomfd < 0)
+		return ERR_PTR(-EPERM);
+
+	return fds;
+}
+
+int devrandom_read(devrandom_t *devrandom, void *buf, size_t len, int wait)
+{
+	if (wait)
+		return linux_read(devrandom->randomfd, buf, len);
+
+	return linux_read(devrandom->urandomfd, buf, len);
+}
diff --git a/arch/sandbox/mach-sandbox/include/mach/linux.h b/arch/sandbox/mach-sandbox/include/mach/linux.h
index c344285473e6..1e64d41c6a5e 100644
--- a/arch/sandbox/mach-sandbox/include/mach/linux.h
+++ b/arch/sandbox/mach-sandbox/include/mach/linux.h
@@ -51,4 +51,11 @@ void barebox_libftdi1_gpio_set_value(struct ft2232_bitbang *ftbb,
 int barebox_libftdi1_update(struct ft2232_bitbang *ftbb);
 void barebox_libftdi1_close(void);
 
+typedef struct {
+	int randomfd;
+	int urandomfd;
+} devrandom_t;
+devrandom_t *devrandom_init(void);
+int devrandom_read(devrandom_t *devrandom, void *buf, size_t len, int wait);
+
 #endif /* __ASM_ARCH_LINUX_H */
diff --git a/drivers/hw_random/Kconfig b/drivers/hw_random/Kconfig
index d1297a48d0bb..c57928204d30 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 79c238c48d60..8be62f38b740 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 000000000000..2170db7437ce
--- /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);
-- 
2.20.1


_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox

  parent reply	other threads:[~2019-09-09  9:31 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-09-09  9:31 [PATCH v2 0/3] sandbox: add HWRNG driver for " Ahmad Fatoum
2019-09-09  9:31 ` [PATCH v2 1/3] sandbox: hostfile: allow probing from device tree Ahmad Fatoum
2019-09-09  9:31 ` [PATCH v2 2/3] sandbox: dts: add example for barebox, hostfile backed state Ahmad Fatoum
2019-09-09  9:31 ` Ahmad Fatoum [this message]
2019-09-09 13:23 ` [PATCH v2 0/3] sandbox: add HWRNG driver for host /dev/random Sascha Hauer

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=20190909093130.30666-4-ahmad@a3f.at \
    --to=ahmad@a3f.at \
    --cc=barebox@lists.infradead.org \
    /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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox