From 025272a2d2c9d3a1841e130b4202d5997ede5cc2 Mon Sep 17 00:00:00 2001 From: Heinrich Schuchardt Date: Tue, 24 Dec 2019 22:17:37 +0100 Subject: [PATCH] UPSTREAM: cmd: add rng command For the RNG uclass we currently only have a test working on the sandbox. Provide a command to test the hardware random number generator on non-sandbox systems. Signed-off-by: Heinrich Schuchardt (cherry picked from commit 4f24ac08afccd5f51f0b4c7023fc8cf45efe8163) Change-Id: Icb2a9359417a8168a38e1fc009960e8c6df924e4 Signed-off-by: Lin Jinhan --- cmd/Kconfig | 7 +++++++ cmd/Makefile | 1 + cmd/rng.c | 56 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 64 insertions(+) create mode 100644 cmd/rng.c diff --git a/cmd/Kconfig b/cmd/Kconfig index b522771ce0..f8afa9c26c 100644 --- a/cmd/Kconfig +++ b/cmd/Kconfig @@ -1260,6 +1260,13 @@ config CMD_GETTIME milliseconds. See also the 'bootstage' command which provides more flexibility for boot timing. +config CMD_RNG + bool "rng command" + depends on DM_RNG + select HEXDUMP + help + Print bytes from the hardware random number generator. + # TODO: rename to CMD_SLEEP config CMD_MISC bool "sleep" diff --git a/cmd/Makefile b/cmd/Makefile index 4744a1490e..afab39477f 100644 --- a/cmd/Makefile +++ b/cmd/Makefile @@ -118,6 +118,7 @@ obj-$(CONFIG_CMD_READ) += read.o obj-$(CONFIG_CMD_REGINFO) += reginfo.o obj-$(CONFIG_CMD_REISER) += reiser.o obj-$(CONFIG_CMD_REMOTEPROC) += remoteproc.o +obj-$(CONFIG_CMD_RNG) += rng.o obj-$(CONFIG_SANDBOX) += host.o obj-$(CONFIG_CMD_SATA) += sata.o obj-$(CONFIG_CMD_NVME) += nvme.o diff --git a/cmd/rng.c b/cmd/rng.c new file mode 100644 index 0000000000..36ca7a101c --- /dev/null +++ b/cmd/rng.c @@ -0,0 +1,56 @@ +// SPDX-License-Identifier: GPL-2.0+ +/* + * The 'rng' command prints bytes from the hardware random number generator. + * + * Copyright (c) 2019, Heinrich Schuchardt + */ +#include +#include +#include +#include +#include + +static int do_rng(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) +{ + size_t n = 0x40; + struct udevice *dev; + void *buf; + int ret = CMD_RET_SUCCESS; + + if (uclass_get_device(UCLASS_RNG, 0, &dev) || !dev) { + printf("No RNG device\n"); + return CMD_RET_FAILURE; + } + + if (argc >= 2) + n = simple_strtoul(argv[1], NULL, 16); + + buf = malloc(n); + if (!buf) { + printf("Out of memory\n"); + return CMD_RET_FAILURE; + } + + if (dm_rng_read(dev, buf, n)) { + printf("Reading RNG failed\n"); + ret = CMD_RET_FAILURE; + } else { + print_hex_dump_bytes("", DUMP_PREFIX_OFFSET, buf, n); + } + + free(buf); + + return ret; +} + +#ifdef CONFIG_SYS_LONGHELP +static char rng_help_text[] = + "[n]\n" + " - print n random bytes\n"; +#endif + +U_BOOT_CMD( + rng, 2, 0, do_rng, + "print bytes from the hardware random number generator", + rng_help_text +);