summaryrefslogtreecommitdiff
path: root/cmd/kaslrseed.c
diff options
context:
space:
mode:
authorChris Morgan <macromorgan@hotmail.com>2021-08-25 11:22:57 -0500
committerKever Yang <kever.yang@rock-chips.com>2021-12-24 10:54:56 +0800
commitfe3dde3e7b0c01d081140fcb28e317a688440fbb (patch)
treef73572b0c5f863f55d6ac2efd3ddf81610cb696f /cmd/kaslrseed.c
parent734ad933766f0dbbeafe1b27211686940a5e6d16 (diff)
downloadu-boot-fe3dde3e7b0c01d081140fcb28e317a688440fbb.tar.gz
u-boot-fe3dde3e7b0c01d081140fcb28e317a688440fbb.tar.bz2
u-boot-fe3dde3e7b0c01d081140fcb28e317a688440fbb.zip
cmd: kaslrseed: add command to generate value from hwrng
Allow the kaslr-seed value in the chosen node to be set from a hardware rng source. Tested on a Rockchip PX30 (Odroid Go Advance), you must have loaded the devicetree first and prepared it for editing. On my device the workflow goes as follows: setenv dtb_loadaddr "0x01f00000" load mmc 0:1 ${dtb_loadaddr} rk3326-odroid-go2.dtb fdt addr ${dtb_loadaddr} fdt resize kaslrseed and the output can be seen here: fdt print /chosen chosen { kaslr-seed = <0x6f61df74 0x6f7b996c>; stdout-path = "serial2:115200n8"; }; Signed-off-by: Chris Morgan <macromorgan@hotmail.com> Reviewed-by: Kever Yang <kever.yang@rock-chips.com> Reviewed-by: Simon Glass <sjg@chromium.org>
Diffstat (limited to 'cmd/kaslrseed.c')
-rw-r--r--cmd/kaslrseed.c81
1 files changed, 81 insertions, 0 deletions
diff --git a/cmd/kaslrseed.c b/cmd/kaslrseed.c
new file mode 100644
index 0000000000..8a1d8120cd
--- /dev/null
+++ b/cmd/kaslrseed.c
@@ -0,0 +1,81 @@
+// SPDX-License-Identifier: GPL-2.0+
+/*
+ * The 'kaslrseed' command takes bytes from the hardware random number
+ * generator and uses them to set the kaslr-seed value in the chosen node.
+ *
+ * Copyright (c) 2021, Chris Morgan <macromorgan@hotmail.com>
+ */
+
+#include <common.h>
+#include <command.h>
+#include <dm.h>
+#include <hexdump.h>
+#include <malloc.h>
+#include <rng.h>
+#include <fdt_support.h>
+
+static int do_kaslr_seed(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
+{
+ size_t n = 0x8;
+ struct udevice *dev;
+ u64 *buf;
+ int nodeoffset;
+ int ret = CMD_RET_SUCCESS;
+
+ if (uclass_get_device(UCLASS_RNG, 0, &dev) || !dev) {
+ printf("No RNG device\n");
+ return CMD_RET_FAILURE;
+ }
+
+ 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");
+ return CMD_RET_FAILURE;
+ }
+
+ if (!working_fdt) {
+ printf("No FDT memory address configured. Please configure\n"
+ "the FDT address via \"fdt addr <address>\" command.\n"
+ "Aborting!\n");
+ return CMD_RET_FAILURE;
+ }
+
+ ret = fdt_check_header(working_fdt);
+ if (ret < 0) {
+ printf("fdt_chosen: %s\n", fdt_strerror(ret));
+ return CMD_RET_FAILURE;
+ }
+
+ nodeoffset = fdt_find_or_add_subnode(working_fdt, 0, "chosen");
+ if (nodeoffset < 0) {
+ printf("Reading chosen node failed\n");
+ return CMD_RET_FAILURE;
+ }
+
+ ret = fdt_setprop(working_fdt, nodeoffset, "kaslr-seed", buf, sizeof(buf));
+ if (ret < 0) {
+ printf("Unable to set kaslr-seed on chosen node: %s\n", fdt_strerror(ret));
+ return CMD_RET_FAILURE;
+ }
+
+ free(buf);
+
+ return ret;
+}
+
+#ifdef CONFIG_SYS_LONGHELP
+static char kaslrseed_help_text[] =
+ "[n]\n"
+ " - append random bytes to chosen kaslr-seed node\n";
+#endif
+
+U_BOOT_CMD(
+ kaslrseed, 1, 0, do_kaslr_seed,
+ "feed bytes from the hardware random number generator to the kaslr-seed",
+ kaslrseed_help_text
+);