Sughosh Ganu | 23e3751 | 2019-12-28 23:58:31 +0530 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
| 2 | /* |
| 3 | * Copyright (c) 2019, Linaro Limited |
| 4 | */ |
| 5 | |
Sughosh Ganu | 23e3751 | 2019-12-28 23:58:31 +0530 | [diff] [blame] | 6 | #include <dm.h> |
Simon Glass | 274e0b0 | 2020-05-10 11:39:56 -0600 | [diff] [blame] | 7 | #include <rand.h> |
Sughosh Ganu | 23e3751 | 2019-12-28 23:58:31 +0530 | [diff] [blame] | 8 | #include <rng.h> |
Heinrich Schuchardt | 10a45cc | 2024-02-13 00:44:47 +0100 | [diff] [blame] | 9 | #include <time.h> |
Sughosh Ganu | 23e3751 | 2019-12-28 23:58:31 +0530 | [diff] [blame] | 10 | #include <linux/string.h> |
| 11 | |
| 12 | static 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 | |
| 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 | }; |