Skip to contentSkip to conversation

sandbox: add HWRNG driver for host /dev/random

PATCH Applied Ahmad Fatoum 2 participants 5 messages Sep 9, 2019

lore
Share & tools
lore
#0 lore raw

v2 cover sandbox: add HWRNG driver for host /dev/random

This series is useful for testing/using barebox,state on sandbox and for use of host entropy while generating passwords for the target system. They are in the same series, because the first commit adds linux_open, which the last depends on.

v1 -> v2:
- split off example sandbox state device tree into seperate commit
- renamed device tree example to end with -example.dts

Ahmad Fatoum (3):
sandbox: hostfile: allow probing from device tree
sandbox: dts: add example for barebox,hostfile backed state
hwrng: add sandbox driver interface to host /dev/random

 arch/sandbox/board/Makefile                   |  1 +
 arch/sandbox/board/board.c                    |  7 +++
 arch/sandbox/board/dev-random.c               | 24 +++++++
 arch/sandbox/board/hostfile.c                 | 16 +++--
 arch/sandbox/dts/sandbox-state-example.dtsi   | 50 +++++++++++++++
 .../sandbox/mach-sandbox/include/mach/linux.h |  8 +++
 arch/sandbox/os/common.c                      |  5 ++
 drivers/hw_random/Kconfig                     |  8 +++
 drivers/hw_random/Makefile                    |  1 +
 drivers/hw_random/dev-random.c                | 63 +++++++++++++++++++
 10 files changed, 179 insertions(+), 4 deletions(-)
 create mode 100644 arch/sandbox/board/dev-random.c
 create mode 100644 arch/sandbox/dts/sandbox-state-example.dtsi
 create mode 100644 drivers/hw_random/dev-random.c
Signature · 7 lines
2.20.1


_______________________________________________
barebox mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/barebox
#1 lore raw

PATCH v2 1/3 sandbox: hostfile: allow probing from device tree

When testing things like barebox state in sandbox, it's nice to be able to refer to a partition on a hostfile by phandle. Support this by checking for reading the barebox,filename property.

---
 arch/sandbox/board/hostfile.c                  | 16 ++++++++++++----
 arch/sandbox/mach-sandbox/include/mach/linux.h |  1 +
 arch/sandbox/os/common.c                       |  5 +++++
 3 files changed, 18 insertions(+), 4 deletions(-)
arch/sandbox/board/hostfile.c +12 -4
index 745f078d1a79..e5a7580d0792 100644
@@ -84,15 +84,16 @@ static int hf_probe(struct device_d *dev)
 	if (!dev->device_node)
 		return -ENODEV;
 
-	err = of_property_read_u32(dev->device_node, "barebox,fd", &priv->fd);
-	if (err)
-		return err;
+	of_property_read_u32(dev->device_node, "barebox,fd", &priv->fd);
 
 	err = of_property_read_string(dev->device_node, "barebox,filename",
 				      &priv->filename);
 	if (err)
 		return err;
 
+	if (!priv->fd)
+		priv->fd = linux_open(priv->filename, true);
+
 	priv->cdev.name = dev->device_node->name;
 	priv->cdev.dev = dev;
 	priv->cdev.ops = &hf_fops;
@@ -101,7 +102,14 @@ static int hf_probe(struct device_d *dev)
 	dev->info = hf_info;
 	dev->priv = priv;
 
-	return devfs_create(&priv->cdev);
+	err = devfs_create(&priv->cdev);
+	if (err)
+		return err;
+
+	of_parse_partitions(&priv->cdev, dev->device_node);
+	of_partitions_register_fixup(&priv->cdev);
+
+	return 0;
 }
 
 static __maybe_unused struct of_device_id hostfile_dt_ids[] = {
arch/sandbox/mach-sandbox/include/mach/linux.h +1 -0
index 7d0ed55735e7..c344285473e6 100644
@@ -10,6 +10,7 @@ struct fb_bitfield;
 int linux_register_device(const char *name, void *start, void *end);
 int tap_alloc(const char *dev);
 uint64_t linux_get_time(void);
+int linux_open(const char *filename, int readwrite);
 int linux_read(int fd, void *buf, size_t count);
 int linux_read_nonblock(int fd, void *buf, size_t count);
 ssize_t linux_write(int fd, const void *buf, size_t count);
arch/sandbox/os/common.c +5 -0
index a7ea8f2d3b18..86118822a177 100644
@@ -121,6 +121,11 @@ void __attribute__((noreturn)) linux_exit(void)
 	exit(0);
 }
 
+int linux_open(const char *filename, int readwrite)
+{
+	return open(filename, readwrite ? O_RDWR : O_RDONLY);
+}
+
 int linux_read(int fd, void *buf, size_t count)
 {
 	ssize_t ret;
Signature · 7 lines
2.20.1


_______________________________________________
barebox mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/barebox
#2 lore raw

PATCH v2 2/3 sandbox: dts: add example for barebox, hostfile backed state

In reply to Ahmad Fatoum

Previous commit now allows probing barebox,hostfile to be probed from device tree. Add a barebox state node that shows how to use it. It's not included by default, same as barebox-libftdi-example.dtsi in the same directory.

---
 arch/sandbox/dts/sandbox-state-example.dtsi | 50 +++++++++++++++++++++
 1 file changed, 50 insertions(+)
 create mode 100644 arch/sandbox/dts/sandbox-state-example.dtsi
arch/sandbox/dts/sandbox-state-example.dtsi new file +50 -0
new file mode 100644
index 000000000000..fc17bd078899
@@ -0,0 +1,50 @@
+/ {
+	aliases {
+		state = &state;
+	};
+
+	disk {
+		compatible = "barebox,hostfile";
+		barebox,filename = "disk";
+		reg = <0x0 0x0 0x100000>;
+
+		partitions {
+			compatible = "fixed-partitions";
+			#address-cells = <1>;
+			#size-cells = <1>;
+
+			hostfile_state: state@0 {
+				reg = <0x0 0x1000>;
+				label = "state";
+			};
+		};
+	};
+
+	state: state {
+		magic = <0xaa3b86a6>;
+		compatible = "barebox,state";
+		backend-type = "raw";
+		backend = <&hostfile_state>;
+		backend-storage-type = "direct";
+		backend-stridesize = <64>;
+
+		#address-cells = <1>;
+		#size-cells = <1>;
+		vars {
+			#address-cells = <1>;
+			#size-cells = <1>;
+
+			x {
+				reg = <0x0 0x4>;
+				type = "uint32";
+				default = <1>;
+			};
+
+			y {
+				reg = <0x4 0x4>;
+				type = "uint32";
+				default = <3>;
+			};
+		};
+	};
+};
Signature · 7 lines
2.20.1


_______________________________________________
barebox mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/barebox
#3 lore raw

PATCH v2 3/3 hwrng: add sandbox driver interface to host /dev/random

In reply to Ahmad Fatoum

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.

---
 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
arch/sandbox/board/Makefile +1 -0
index b6c271c858c6..26f6cb192269 100644
@@ -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
arch/sandbox/board/board.c +7 -0
index 1f6392d15ed3..ad2bc910c342 100644
@@ -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;
 }
 
arch/sandbox/board/dev-random.c new file +24 -0
new file mode 100644
index 000000000000..f65e5ef6e5ed
@@ -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);
+}
arch/sandbox/mach-sandbox/include/mach/linux.h +7 -0
index c344285473e6..1e64d41c6a5e 100644
@@ -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 */
drivers/hw_random/Kconfig +8 -0
index d1297a48d0bb..c57928204d30 100644
@@ -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
drivers/hw_random/Makefile +1 -0
index 79c238c48d60..8be62f38b740 100644
@@ -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
drivers/hw_random/dev-random.c new file +63 -0

63 lines not on this page · raw message

Signature · 7 lines
2.20.1


_______________________________________________
barebox mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/barebox
Applied
#4 lore raw
In reply to Ahmad Fatoum
Quoted 5 lines · Ahmad Fatoum

This series is useful for testing/using barebox,state on sandbox and for use of host entropy while generating passwords for the target system. They are in the same series, because the first commit adds linux_open, which the last depends on.

Quoted 8 lines

v1 -> v2:
- split off example sandbox state device tree into seperate commit
- renamed device tree example to end with -example.dts

Ahmad Fatoum (3):
sandbox: hostfile: allow probing from device tree
sandbox: dts: add example for barebox,hostfile backed state
hwrng: add sandbox driver interface to host /dev/random

Applied, thanks

Sascha

Signature · 9 lines
Pengutronix e.K.                           |                             |
Industrial Linux Solutions                 | http://www.pengutronix.de/  |
Peiner Str. 6-8, 31137 Hildesheim, Germany | Phone: +49-5121-206917-0    |
Amtsgericht Hildesheim, HRA 2686           | Fax:   +49-5121-206917-5555 |

_______________________________________________
barebox mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/barebox