[U-Boot] [PATCH] cmd: mem: Add a command to fill the memory with random data

Jean-Jacques Hiblot jjhiblot at ti.com
Fri Jun 14 15:32:03 UTC 2019


This command fills the memory with data produced by rand().

Signed-off-by: Jean-Jacques Hiblot <jjhiblot at ti.com>
---
 cmd/Kconfig |  3 ++-
 cmd/mem.c   | 48 ++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 50 insertions(+), 1 deletion(-)

diff --git a/cmd/Kconfig b/cmd/Kconfig
index ea1a325eb3..ff96ac5d14 100644
--- a/cmd/Kconfig
+++ b/cmd/Kconfig
@@ -517,7 +517,7 @@ config CMD_MEMINFO
 	  Display memory information.
 
 config CMD_MEMORY
-	bool "md, mm, nm, mw, cp, cmp, base, loop"
+	bool "md, mm, nm, mw, cp, cmp, base, loop, random"
 	default y
 	help
 	  Memory commands.
@@ -529,6 +529,7 @@ config CMD_MEMORY
 	    cmp - memory compare
 	    base - print or set address offset
 	    loop - initialize loop on address range
+	    random - fill memory with random data
 
 config CMD_MEMTEST
 	bool "memtest"
diff --git a/cmd/mem.c b/cmd/mem.c
index 392ed1756b..54e65be347 100644
--- a/cmd/mem.c
+++ b/cmd/mem.c
@@ -1082,6 +1082,47 @@ static int do_mem_crc(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
 
 #endif
 
+int do_random(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
+{
+	unsigned long addr, len;
+	unsigned long seed; // NOT INITIALIZED ON PURPOSE
+	unsigned int *buf, *start;
+	unsigned char *buf8;
+	unsigned int i;
+
+	if (argc < 3 || argc > 4) {
+		printf("usage: %s <addr> <len> [<seed>]\n", argv[0]);
+		return 0;
+	}
+
+	len = simple_strtoul(argv[2], NULL, 16);
+	addr = simple_strtoul(argv[1], NULL, 16);
+
+	if (argc == 4) {
+		seed = simple_strtoul(argv[3], NULL, 16);
+		if (seed == 0) {
+			printf("The seed cannot be 0. Using 0xDEADBEEF.\n");
+			seed = 0xDEADBEEF;
+		}
+	} else {
+		seed = get_timer(0) ^ rand();
+	}
+
+	srand(seed);
+	start = map_sysmem(addr, len);
+	buf = start;
+	for (i = 0; i < (len / 4); i++)
+		*buf++ = rand();
+
+	buf8 = (unsigned char *)buf;
+	for (i = 0; i < (len % 4); i++)
+		*buf8++ = rand() & 0xFF;
+
+	unmap_sysmem(start);
+	printf("%lu bytes filled with random data\n", len);
+	return 1;
+}
+
 /**************************************************/
 U_BOOT_CMD(
 	md,	3,	1,	do_mem_md,
@@ -1250,3 +1291,10 @@ U_BOOT_CMD(
 	""
 );
 #endif
+
+U_BOOT_CMD(
+	random,	4,	0,	do_random,
+	"fill memory with random pattern",
+	"<addr> <len> [<seed>]\n"
+	"   - Fill 'len' bytes of memory starting at 'addr' with random data\n"
+);
-- 
2.17.1



More information about the U-Boot mailing list