[PATCH v2 2/2] rng: add dm_rng_read_default() helper

Alexey Romanov avromanov at salutedevices.com
Thu Nov 2 10:16:45 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 | 18 ++++++++++++++++++
 include/rng.h            | 10 ++++++++++
 2 files changed, 28 insertions(+)

diff --git a/drivers/rng/rng-uclass.c b/drivers/rng/rng-uclass.c
index d7236b9335..e298623e1d 100644
--- a/drivers/rng/rng-uclass.c
+++ b/drivers/rng/rng-uclass.c
@@ -35,6 +35,24 @@ 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 = platform_get_rng_device(&rng);
+	if (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 255c85d3e8..6412fedad2 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);
+
 /**
  * platform_get_rng_device() - retrieve random number generator
  *
-- 
2.30.1



More information about the U-Boot mailing list