summaryrefslogtreecommitdiff
path: root/drivers/rng/sandbox_rng.c
blob: cc5e1f6e25b67af9cbe6aa9afb2a52691940e323 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
// SPDX-License-Identifier: GPL-2.0-or-later
/*
 * Copyright (c) 2019, Linaro Limited
 */

#include <common.h>
#include <dm.h>
#include <rand.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,
};