[PATCH v1] rng: add dm_rng_read_default() helper

Alexey Romanov avromanov at salutedevices.com
Wed Nov 1 14:49:02 CET 2023


Add dm_rng_read_default() function, which obtain a series
of random bytes. In some cases, such function would be
useful because it allows the caller to abstract away from
RNG device.

Signed-off-by: Alexey Romanov <avromanov at salutedevices.com>
---
 drivers/rng/rng-uclass.c | 20 ++++++++++++++++++++
 include/rng.h            | 10 ++++++++++
 2 files changed, 30 insertions(+)

diff --git a/drivers/rng/rng-uclass.c b/drivers/rng/rng-uclass.c
index 53108e1b31..3a8fb7d276 100644
--- a/drivers/rng/rng-uclass.c
+++ b/drivers/rng/rng-uclass.c
@@ -19,6 +19,26 @@ int dm_rng_read(struct udevice *dev, void *buffer, size_t size)
 	return ops->read(dev, buffer, size);
 }
 
+int dm_rng_read_default(void *buffer, size_t size)
+{
+	struct udevice *rng;
+	int ret;
+
+	ret = uclass_get_device(UCLASS_RNG, 0, &rng);
+	if (ret) {
+		pr_err("Can't get RNG device (%d)\n", ret);
+		return ret;
+	}
+
+	ret = dm_rng_read(rng, buffer, size);
+	if (ret) {
+		pr_err("Can't read from RNG device (%d)\n", ret);
+		return ret;
+	}
+
+	return 0;
+}
+
 UCLASS_DRIVER(rng) = {
 	.name = "rng",
 	.id = UCLASS_RNG,
diff --git a/include/rng.h b/include/rng.h
index 37af554363..5537daae88 100644
--- a/include/rng.h
+++ b/include/rng.h
@@ -20,6 +20,16 @@ struct udevice;
  */
 int dm_rng_read(struct udevice *dev, void *buffer, size_t size);
 
+/**
+ * dm_rng_read_default() - same as dm_rng_read(), except that caller
+ * don't need to pass an argument with RNG udevice.
+ * @buffer:	input buffer to put the read random seed into
+ * @size:	number of bytes of random seed read
+ *
+ * Return: 0 if OK, -ve on error
+ */
+int dm_rng_read_default(void *buffer, size_t size);
+
 /**
  * struct dm_rng_ops - operations for the hwrng uclass
  *
-- 
2.25.1



More information about the U-Boot mailing list