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