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