Skip to content
Snippets Groups Projects
Commit ff0dada9 authored by Sughosh Ganu's avatar Sughosh Ganu Committed by Heinrich Schuchardt
Browse files

sandbox: rng: Add a random number generator(rng) driver


Add a sandbox driver for random number generation. Mostly aimed at
providing a unit test for rng uclass.

Signed-off-by: default avatarSughosh Ganu <sughosh.ganu@linaro.org>
Reviewed-by: default avatarPatrice Chotard <patrice.chotard@st.com>
Reviewed-by: default avatarSimon Glass <sjg@chromium.org>
parent 90950eec
No related branches found
No related tags found
No related merge requests found
...@@ -630,6 +630,10 @@ ...@@ -630,6 +630,10 @@
reset-names = "other", "test"; reset-names = "other", "test";
}; };
rng {
compatible = "sandbox,sandbox-rng";
};
rproc_1: rproc@1 { rproc_1: rproc@1 {
compatible = "sandbox,test-processor"; compatible = "sandbox,test-processor";
remoteproc-name = "remoteproc-test-dev1"; remoteproc-name = "remoteproc-test-dev1";
......
...@@ -6,6 +6,14 @@ config DM_RNG ...@@ -6,6 +6,14 @@ config DM_RNG
This interface is used to initialise the rng device and to This interface is used to initialise the rng device and to
read the random seed from the device. read the random seed from the device.
config RNG_SANDBOX
bool "Sandbox random number generator"
depends on SANDBOX && DM_RNG
select CONFIG_LIB_RAND
help
Enable random number generator for sandbox. This is an
emulation of a rng device.
config RNG_STM32MP1 config RNG_STM32MP1
bool "Enable random number generator for STM32MP1" bool "Enable random number generator for STM32MP1"
depends on ARCH_STM32MP && DM_RNG depends on ARCH_STM32MP && DM_RNG
......
...@@ -4,4 +4,5 @@ ...@@ -4,4 +4,5 @@
# #
obj-$(CONFIG_DM_RNG) += rng-uclass.o obj-$(CONFIG_DM_RNG) += rng-uclass.o
obj-$(CONFIG_RNG_SANDBOX) += sandbox_rng.o
obj-$(CONFIG_RNG_STM32MP1) += stm32mp1_rng.o obj-$(CONFIG_RNG_STM32MP1) += stm32mp1_rng.o
// SPDX-License-Identifier: GPL-2.0-or-later
/*
* Copyright (c) 2019, Linaro Limited
*/
#include <common.h>
#include <dm.h>
#include <rng.h>
#include <linux/string.h>
static int sandbox_rng_read(struct udevice *dev, void *data, size_t len)
{
unsigned int i, seed, random;
unsigned char *buf = data;
size_t nrem, nloops;
if (!len)
return 0;
nloops = len / sizeof(random);
seed = get_timer(0) ^ rand();
srand(seed);
for (i = 0, nrem = len; i < nloops; i++) {
random = rand();
memcpy(buf, &random, sizeof(random));
buf += sizeof(random);
nrem -= sizeof(random);
}
if (nrem) {
random = rand();
memcpy(buf, &random, nrem);
}
return 0;
}
static const struct dm_rng_ops sandbox_rng_ops = {
.read = sandbox_rng_read,
};
static const struct udevice_id sandbox_rng_match[] = {
{
.compatible = "sandbox,sandbox-rng",
},
{},
};
U_BOOT_DRIVER(sandbox_rng) = {
.name = "sandbox-rng",
.id = UCLASS_RNG,
.of_match = sandbox_rng_match,
.ops = &sandbox_rng_ops,
};
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment