blob: 36ca7a101c1fd5b2cc4dc261aecf5dd0ebe60ec8 [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>
11#include <rng.h>
12
13static int do_rng(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
14{
15 size_t n = 0x40;
16 struct udevice *dev;
17 void *buf;
18 int ret = CMD_RET_SUCCESS;
19
20 if (uclass_get_device(UCLASS_RNG, 0, &dev) || !dev) {
21 printf("No RNG device\n");
22 return CMD_RET_FAILURE;
23 }
24
25 if (argc >= 2)
26 n = simple_strtoul(argv[1], NULL, 16);
27
28 buf = malloc(n);
29 if (!buf) {
30 printf("Out of memory\n");
31 return CMD_RET_FAILURE;
32 }
33
34 if (dm_rng_read(dev, buf, n)) {
35 printf("Reading RNG failed\n");
36 ret = CMD_RET_FAILURE;
37 } else {
38 print_hex_dump_bytes("", DUMP_PREFIX_OFFSET, buf, n);
39 }
40
41 free(buf);
42
43 return ret;
44}
45
46#ifdef CONFIG_SYS_LONGHELP
47static char rng_help_text[] =
48 "[n]\n"
49 " - print n random bytes\n";
50#endif
51
52U_BOOT_CMD(
53 rng, 2, 0, do_rng,
54 "print bytes from the hardware random number generator",
55 rng_help_text
56);