blob: 071a3228e8a3eeaae47c16ba7d2e018446e330f7 [file] [log] [blame]
Sughosh Ganu23e37512019-12-28 23:58:31 +05301// SPDX-License-Identifier: GPL-2.0-or-later
2/*
3 * Copyright (c) 2019, Linaro Limited
4 */
5
Sughosh Ganu23e37512019-12-28 23:58:31 +05306#include <dm.h>
Simon Glass274e0b02020-05-10 11:39:56 -06007#include <rand.h>
Sughosh Ganu23e37512019-12-28 23:58:31 +05308#include <rng.h>
Heinrich Schuchardt10a45cc2024-02-13 00:44:47 +01009#include <time.h>
Sughosh Ganu23e37512019-12-28 23:58:31 +053010#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};