1 // SPDX-License-Identifier: GPL-2.0+
3 * The 'rng' command prints bytes from the hardware random number generator.
5 * Copyright (c) 2019, Heinrich Schuchardt <xypron.glpk@gmx.de>
13 static int do_rng(struct cmd_tbl
*cmdtp
, int flag
, int argc
, char *const argv
[])
19 int ret
= CMD_RET_SUCCESS
, err
;
21 if (argc
== 2 && !strcmp(argv
[1], "list")) {
24 uclass_foreach_dev_probe(UCLASS_RNG
, dev
) {
26 printf("RNG #%d - %s\n", dev
->seq_
, dev
->name
);
30 log_err("No RNG device\n");
31 return CMD_RET_FAILURE
;
34 return CMD_RET_SUCCESS
;
43 devnum
= hextoul(argv
[1], NULL
);
47 devnum
= hextoul(argv
[1], NULL
);
48 n
= hextoul(argv
[2], NULL
);
54 if (uclass_get_device_by_seq(UCLASS_RNG
, devnum
, &dev
) || !dev
) {
55 printf("No RNG device\n");
56 return CMD_RET_FAILURE
;
62 n
= min(n
, sizeof(buf
));
64 err
= dm_rng_read(dev
, buf
, n
);
66 puts(err
== -EINTR
? "Abort\n" : "Reading RNG failed\n");
67 ret
= CMD_RET_FAILURE
;
69 print_hex_dump_bytes("", DUMP_PREFIX_OFFSET
, buf
, n
);
77 "print bytes from the hardware random number generator",
78 "list - list all the probed rng devices\n"
79 "rng [dev] [n] - print n random bytes(max 64) read from dev\n"