]> git.ipfire.org Git - thirdparty/u-boot.git/blame - cmd/rng.c
Revert "Merge patch series "arm: dts: am62-beagleplay: Fix Beagleplay Ethernet""
[thirdparty/u-boot.git] / cmd / rng.c
CommitLineData
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 <xypron.glpk@gmx.de>
6 */
d678a59d 7#include <common.h>
4f24ac08
HS
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 14static 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;
144c0167 20 int ret = CMD_RET_SUCCESS, err;
4f24ac08 21
9a6e975c
WO
22 if (argc == 2 && !strcmp(argv[1], "list")) {
23 int idx = 0;
24
25 uclass_foreach_dev_probe(UCLASS_RNG, dev) {
26 idx++;
27 printf("RNG #%d - %s\n", dev->seq_, dev->name);
28 }
29
30 if (!idx) {
31 log_err("No RNG device\n");
32 return CMD_RET_FAILURE;
33 }
34
35 return CMD_RET_SUCCESS;
36 }
37
87ab234c
SG
38 switch (argc) {
39 case 1:
40 devnum = 0;
41 n = 0x40;
42 break;
43 case 2:
44 devnum = hextoul(argv[1], NULL);
45 n = 0x40;
46 break;
47 case 3:
48 devnum = hextoul(argv[1], NULL);
49 n = hextoul(argv[2], NULL);
50 break;
51 default:
52 return CMD_RET_USAGE;
53 }
54
55 if (uclass_get_device_by_seq(UCLASS_RNG, devnum, &dev) || !dev) {
4f24ac08
HS
56 printf("No RNG device\n");
57 return CMD_RET_FAILURE;
58 }
59
c79e274c
SG
60 if (!n)
61 return 0;
62
63 n = min(n, sizeof(buf));
4f24ac08 64
144c0167
MB
65 err = dm_rng_read(dev, buf, n);
66 if (err) {
67 puts(err == -EINTR ? "Abort\n" : "Reading RNG failed\n");
4f24ac08
HS
68 ret = CMD_RET_FAILURE;
69 } else {
70 print_hex_dump_bytes("", DUMP_PREFIX_OFFSET, buf, n);
71 }
72
4f24ac08
HS
73 return ret;
74}
75
4f24ac08 76U_BOOT_CMD(
87ab234c 77 rng, 3, 0, do_rng,
4f24ac08 78 "print bytes from the hardware random number generator",
9a6e975c
WO
79 "list - list all the probed rng devices\n"
80 "rng [dev] [n] - print n random bytes(max 64) read from dev\n"
4f24ac08 81);