cmd: add rng command
authorHeinrich Schuchardt <xypron.glpk@gmx.de>
Tue, 24 Dec 2019 21:17:37 +0000 (22:17 +0100)
committerHeinrich Schuchardt <xypron.glpk@gmx.de>
Tue, 7 Jan 2020 17:08:21 +0000 (18:08 +0100)
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 <xypron.glpk@gmx.de>
cmd/Kconfig
cmd/Makefile
cmd/rng.c [new file with mode: 0644]

index a6bfa87e081e2a8fe7a001ef7af8c9cc3c0d6c19..7a78343e6b324f9b93a8135d91b897305ae7410f 100644 (file)
@@ -1672,6 +1672,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"
index 12e898d96205d74523753e04147629f4697559ac..8df39f3a19f1737d84f8a966d40d8bb70197d50b 100644 (file)
@@ -117,6 +117,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_CMD_ROCKUSB) += rockusb.o
 obj-$(CONFIG_SANDBOX) += host.o
 obj-$(CONFIG_CMD_SATA) += sata.o
diff --git a/cmd/rng.c b/cmd/rng.c
new file mode 100644 (file)
index 0000000..36ca7a1
--- /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 <xypron.glpk@gmx.de>
+ */
+#include <common.h>
+#include <command.h>
+#include <dm.h>
+#include <hexdump.h>
+#include <rng.h>
+
+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
+);