Skip to content

Commit 6b1be7c

Browse files
tmediccixiaoxiang781216
authored andcommitted
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.
1 parent 893c5e9 commit 6b1be7c

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

drivers/virtio/virtio-rng.c

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,9 @@ struct virtio_rng_priv_s
5353
{
5454
FAR struct virtio_device *vdev;
5555
char name[NAME_MAX];
56+
#ifdef CONFIG_DEV_URANDOM
57+
char uname[NAME_MAX];
58+
#endif
5659
spinlock_t lock;
5760
};
5861

@@ -234,10 +237,16 @@ static int virtio_rng_probe(FAR struct virtio_device *vdev)
234237
if (g_virtio_rng_idx == 0)
235238
{
236239
strlcpy(priv->name, "/dev/random", NAME_MAX);
240+
#ifdef CONFIG_DEV_URANDOM
241+
strlcpy(priv->uname, "/dev/urandom", NAME_MAX);
242+
#endif
237243
}
238244
else
239245
{
240246
snprintf(priv->name, NAME_MAX, "/dev/random%d", g_virtio_rng_idx);
247+
#ifdef CONFIG_DEV_URANDOM
248+
snprintf(priv->uname, NAME_MAX, "/dev/urandom%d", g_virtio_rng_idx);
249+
#endif
241250
}
242251

243252
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)
247256
goto err_with_virtqueue;
248257
}
249258

259+
#ifdef CONFIG_DEV_URANDOM
260+
ret = register_driver(priv->uname, &g_virtio_rng_ops, 0444, priv);
261+
if (ret < 0)
262+
{
263+
vrterr("Register NuttX driver failed, ret=%d\n", ret);
264+
goto err_with_virtqueue;
265+
}
266+
#endif
267+
250268
g_virtio_rng_idx++;
251269
return ret;
252270

@@ -293,6 +311,23 @@ void weak_function devrandom_register(void)
293311
}
294312
#endif
295313

314+
/****************************************************************************
315+
* Name: devurandom_register
316+
*
317+
* Description:
318+
* Initialize the RNG hardware and register the /dev/urandom driver.
319+
*
320+
****************************************************************************/
321+
322+
#ifdef CONFIG_DEV_URANDOM
323+
void weak_function devurandom_register(void)
324+
{
325+
/* Nothing, implement it here just avoid the compile error, the driver
326+
* /dev/urandom will be registered in the virtio rng driver.
327+
*/
328+
}
329+
#endif
330+
296331
/****************************************************************************
297332
* Name: virtio_register_rng_driver
298333
****************************************************************************/

0 commit comments

Comments
 (0)