]>
Commit | Line | Data |
---|---|---|
4f24ac08 HS |
1 | // SPDX-License-Identifier: GPL-2.0+ |
2 | /* | |
3 | * The 'rng' command prints bytes from the hardware random number generator. | |
4 | * | |
5 | * Copyright (c) 2019, Heinrich Schuchardt <[email protected]> | |
6 | */ | |
7 | #include <common.h> | |
8 | #include <command.h> | |
9 | #include <dm.h> | |
10 | #include <hexdump.h> | |
336d4615 | 11 | #include <malloc.h> |
4f24ac08 HS |
12 | #include <rng.h> |
13 | ||
09140113 | 14 | static int do_rng(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[]) |
4f24ac08 | 15 | { |
87ab234c | 16 | size_t n; |
c79e274c | 17 | u8 buf[64]; |
87ab234c | 18 | int devnum; |
c79e274c | 19 | struct udevice *dev; |
4f24ac08 HS |
20 | int ret = CMD_RET_SUCCESS; |
21 | ||
87ab234c SG |
22 | switch (argc) { |
23 | case 1: | |
24 | devnum = 0; | |
25 | n = 0x40; | |
26 | break; | |
27 | case 2: | |
28 | devnum = hextoul(argv[1], NULL); | |
29 | n = 0x40; | |
30 | break; | |
31 | case 3: | |
32 | devnum = hextoul(argv[1], NULL); | |
33 | n = hextoul(argv[2], NULL); | |
34 | break; | |
35 | default: | |
36 | return CMD_RET_USAGE; | |
37 | } | |
38 | ||
39 | if (uclass_get_device_by_seq(UCLASS_RNG, devnum, &dev) || !dev) { | |
4f24ac08 HS |
40 | printf("No RNG device\n"); |
41 | return CMD_RET_FAILURE; | |
42 | } | |
43 | ||
c79e274c SG |
44 | if (!n) |
45 | return 0; | |
46 | ||
47 | n = min(n, sizeof(buf)); | |
4f24ac08 HS |
48 | |
49 | if (dm_rng_read(dev, buf, n)) { | |
50 | printf("Reading RNG failed\n"); | |
51 | ret = CMD_RET_FAILURE; | |
52 | } else { | |
53 | print_hex_dump_bytes("", DUMP_PREFIX_OFFSET, buf, n); | |
54 | } | |
55 | ||
4f24ac08 HS |
56 | return ret; |
57 | } | |
58 | ||
59 | #ifdef CONFIG_SYS_LONGHELP | |
60 | static char rng_help_text[] = | |
87ab234c | 61 | "[dev [n]]\n" |
c79e274c | 62 | " - print n random bytes(max 64) read from dev\n"; |
4f24ac08 HS |
63 | #endif |
64 | ||
65 | U_BOOT_CMD( | |
87ab234c | 66 | rng, 3, 0, do_rng, |
4f24ac08 HS |
67 | "print bytes from the hardware random number generator", |
68 | rng_help_text | |
69 | ); |