[PATCH v3 4/4] fs: add random device

Oleksij Rempel o.rempel at pengutronix.de
Fri Mar 17 08:46:38 PDT 2017


this should provide easy access to get_random_bytes() interfaces.

Signed-off-by: Oleksij Rempel <o.rempel at pengutronix.de>
---
 commands/stddev.c | 29 +++++++++++++++++++++++++++++
 1 file changed, 29 insertions(+)

diff --git a/commands/stddev.c b/commands/stddev.c
index 318d05741..99d60e5de 100644
--- a/commands/stddev.c
+++ b/commands/stddev.c
@@ -17,6 +17,7 @@
 
 #include <common.h>
 #include <init.h>
+#include <stdlib.h>
 
 static ssize_t zero_read(struct cdev *cdev, void *buf, size_t count, loff_t offset, ulong flags)
 {
@@ -100,3 +101,31 @@ static int null_init(void)
 }
 
 device_initcall(null_init);
+
+static ssize_t random_read(struct cdev *cdev, void *buf, size_t count, loff_t offset, ulong flags)
+{
+	get_random_bytes(buf, count);
+	return count;
+}
+
+static struct file_operations randomops = {
+	.read  = random_read,
+	.lseek = dev_lseek_default,
+};
+
+static int random_init(void)
+{
+	struct cdev *cdev;
+
+	cdev = xzalloc(sizeof (*cdev));
+
+	cdev->name = "random";
+	cdev->flags = DEVFS_IS_CHARACTER_DEV;
+	cdev->ops = &randomops;
+
+	devfs_create(cdev);
+
+	return 0;
+}
+
+device_initcall(random_init);
-- 
2.11.0




More information about the barebox mailing list