From f0c49e07a466d40d0ed7c9ff47352e8c59e6e3e6 Mon Sep 17 00:00:00 2001 From: Tiago Medicci Serrano Date: Thu, 2 Jan 2025 10:33:43 -0300 Subject: [PATCH] virtio-rng: Register /dev/urandom driver if CONFIG_DEV_URANDOM=y Virtio RNG support (CONFIG_DRIVERS_VIRTIO_RNG=y) selects CONFIG_ARCH_HAVE_RNG. On the other hand, if CONFIG_DEV_URANDOM=y, it defaults to CONFIG_DEV_URANDOM_ARCH if CONFIG_ARCH_HAVE_RNG=y. DEV_URANDOM_ARCH definition states that the implementation of the /dev/urandom should be provided by the architecture-specifig logic, including the function devurandom_register(). In this case, the /dev/urandom may refer to the same driver as /dev/random that is provided by the Virtio RNG driver, which is implemented by this commit. --- drivers/virtio/virtio-rng.c | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/drivers/virtio/virtio-rng.c b/drivers/virtio/virtio-rng.c index 9d81dccc3b96c..276975676a7bc 100644 --- a/drivers/virtio/virtio-rng.c +++ b/drivers/virtio/virtio-rng.c @@ -53,6 +53,9 @@ struct virtio_rng_priv_s { FAR struct virtio_device *vdev; char name[NAME_MAX]; +#ifdef CONFIG_DEV_URANDOM + char uname[NAME_MAX]; +#endif spinlock_t lock; }; @@ -234,10 +237,16 @@ static int virtio_rng_probe(FAR struct virtio_device *vdev) if (g_virtio_rng_idx == 0) { strlcpy(priv->name, "/dev/random", NAME_MAX); +#ifdef CONFIG_DEV_URANDOM + strlcpy(priv->uname, "/dev/urandom", NAME_MAX); +#endif } else { snprintf(priv->name, NAME_MAX, "/dev/random%d", g_virtio_rng_idx); +#ifdef CONFIG_DEV_URANDOM + snprintf(priv->uname, NAME_MAX, "/dev/urandom%d", g_virtio_rng_idx); +#endif } ret = register_driver(priv->name, &g_virtio_rng_ops, 0444, priv); @@ -247,6 +256,15 @@ static int virtio_rng_probe(FAR struct virtio_device *vdev) goto err_with_virtqueue; } +#ifdef CONFIG_DEV_URANDOM + ret = register_driver(priv->uname, &g_virtio_rng_ops, 0444, priv); + if (ret < 0) + { + vrterr("Register NuttX driver failed, ret=%d\n", ret); + goto err_with_virtqueue; + } +#endif + g_virtio_rng_idx++; return ret; @@ -293,6 +311,23 @@ void weak_function devrandom_register(void) } #endif +/**************************************************************************** + * Name: devurandom_register + * + * Description: + * Initialize the RNG hardware and register the /dev/urandom driver. + * + ****************************************************************************/ + +#ifdef CONFIG_DEV_URANDOM +void weak_function devurandom_register(void) +{ + /* Nothing, implement it here just avoid the compile error, the driver + * /dev/urandom will be registered in the virtio rng driver. + */ +} +#endif + /**************************************************************************** * Name: virtio_register_rng_driver ****************************************************************************/