blob: cd0b0ac77b6af0b38ac0b35bcab41e5003593cc2 [file] [log] [blame]
Sughosh Ganuff0dada2019-12-28 23:58:31 +05301// SPDX-License-Identifier: GPL-2.0-or-later
2/*
3 * Copyright (c) 2019, Linaro Limited
4 */
5
6#include <common.h>
7#include <dm.h>
8#include <rng.h>
9
10#include <linux/string.h>
11
12static int sandbox_rng_read(struct udevice *dev, void *data, size_t len)
13{
14 unsigned int i, seed, random;
15 unsigned char *buf = data;
16 size_t nrem, nloops;
17
18 if (!len)
19 return 0;
20
21 nloops = len / sizeof(random);
22 seed = get_timer(0) ^ rand();
23 srand(seed);
24
25 for (i = 0, nrem = len; i < nloops; i++) {
26 random = rand();
27 memcpy(buf, &random, sizeof(random));
28 buf += sizeof(random);
29 nrem -= sizeof(random);
30 }
31
32 if (nrem) {
33 random = rand();
34 memcpy(buf, &random, nrem);
35 }
36
37 return 0;
38}
39
40static const struct dm_rng_ops sandbox_rng_ops = {
41 .read = sandbox_rng_read,
42};
43
44static const struct udevice_id sandbox_rng_match[] = {
45 {
46 .compatible = "sandbox,sandbox-rng",
47 },
48 {},
49};
50
51U_BOOT_DRIVER(sandbox_rng) = {
52 .name = "sandbox-rng",
53 .id = UCLASS_RNG,
54 .of_match = sandbox_rng_match,
55 .ops = &sandbox_rng_ops,
56};