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.
This commit is contained in:
Tiago Medicci Serrano 2025-01-02 10:33:43 -03:00 committed by Xiang Xiao
parent 893c5e92c2
commit 6b1be7c66c

View file

@ -53,6 +53,9 @@ struct virtio_rng_priv_s
{ {
FAR struct virtio_device *vdev; FAR struct virtio_device *vdev;
char name[NAME_MAX]; char name[NAME_MAX];
#ifdef CONFIG_DEV_URANDOM
char uname[NAME_MAX];
#endif
spinlock_t lock; spinlock_t lock;
}; };
@ -234,10 +237,16 @@ static int virtio_rng_probe(FAR struct virtio_device *vdev)
if (g_virtio_rng_idx == 0) if (g_virtio_rng_idx == 0)
{ {
strlcpy(priv->name, "/dev/random", NAME_MAX); strlcpy(priv->name, "/dev/random", NAME_MAX);
#ifdef CONFIG_DEV_URANDOM
strlcpy(priv->uname, "/dev/urandom", NAME_MAX);
#endif
} }
else else
{ {
snprintf(priv->name, NAME_MAX, "/dev/random%d", g_virtio_rng_idx); 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); 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; 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++; g_virtio_rng_idx++;
return ret; return ret;
@ -293,6 +311,23 @@ void weak_function devrandom_register(void)
} }
#endif #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 * Name: virtio_register_rng_driver
****************************************************************************/ ****************************************************************************/