blob: 52f722c7af897b5aac434567b58bbaff4226cea4 [file] [log] [blame]
Heinrich Schuchardta31a5942019-12-24 22:17:37 +01001// 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 */
7#include <common.h>
8#include <command.h>
9#include <dm.h>
10#include <hexdump.h>
Simon Glass9bc15642020-02-03 07:36:16 -070011#include <malloc.h>
Heinrich Schuchardta31a5942019-12-24 22:17:37 +010012#include <rng.h>
13
Simon Glassed38aef2020-05-10 11:40:03 -060014static int do_rng(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
Heinrich Schuchardta31a5942019-12-24 22:17:37 +010015{
Sughosh Ganub746dee2022-07-22 21:32:06 +053016 size_t n;
Sughosh Ganu407ec092022-07-22 21:32:07 +053017 u8 buf[64];
Sughosh Ganub746dee2022-07-22 21:32:06 +053018 int devnum;
Sughosh Ganu407ec092022-07-22 21:32:07 +053019 struct udevice *dev;
Heinrich Schuchardta31a5942019-12-24 22:17:37 +010020 int ret = CMD_RET_SUCCESS;
21
Sughosh Ganub746dee2022-07-22 21:32:06 +053022 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) {
Heinrich Schuchardta31a5942019-12-24 22:17:37 +010040 printf("No RNG device\n");
41 return CMD_RET_FAILURE;
42 }
43
Sughosh Ganu407ec092022-07-22 21:32:07 +053044 if (!n)
45 return 0;
46
47 n = min(n, sizeof(buf));
Heinrich Schuchardta31a5942019-12-24 22:17:37 +010048
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
Heinrich Schuchardta31a5942019-12-24 22:17:37 +010056 return ret;
57}
58
Tom Rini03f146c2023-10-07 15:13:08 -040059U_BOOT_LONGHELP(rng,
Sughosh Ganub746dee2022-07-22 21:32:06 +053060 "[dev [n]]\n"
Tom Rini03f146c2023-10-07 15:13:08 -040061 " - print n random bytes(max 64) read from dev\n");
Heinrich Schuchardta31a5942019-12-24 22:17:37 +010062
63U_BOOT_CMD(
Sughosh Ganub746dee2022-07-22 21:32:06 +053064 rng, 3, 0, do_rng,
Heinrich Schuchardta31a5942019-12-24 22:17:37 +010065 "print bytes from the hardware random number generator",
66 rng_help_text
67);