[U-Boot] [PATCH] Add a memory get command

Joe Hershberger joe.hershberger at ni.com
Fri Aug 17 22:58:41 CEST 2012


This command allows you to read the value of a memory address and store
it in an environment variable.

Signed-off-by: Joe Hershberger <joe.hershberger at ni.com>
---
 common/cmd_mem.c | 47 +++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 47 insertions(+)

diff --git a/common/cmd_mem.c b/common/cmd_mem.c
index 18f0a3f..08c7d69 100644
--- a/common/cmd_mem.c
+++ b/common/cmd_mem.c
@@ -32,6 +32,7 @@
 #ifdef CONFIG_HAS_DATAFLASH
 #include <dataflash.h>
 #endif
+#include <asm/io.h>
 #include <watchdog.h>
 
 static int mod_mem(cmd_tbl_t *, int, int, int, char * const []);
@@ -147,6 +148,45 @@ int do_mem_md ( cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
 	return (rc);
 }
 
+#ifdef CONFIG_CMD_MEM_GET
+int do_mem_mg(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
+{
+	ulong addr;
+	int size;
+	char *var;
+	char buf[11];
+
+	if (argc < 3)
+		return CMD_RET_USAGE;
+
+	/* Check for a size specification.
+	 * Defaults to long if no or incorrect specification.
+	 */
+	size = cmd_get_data_size(argv[0], 4);
+	if (size < 0)
+		return 1;
+
+	var = argv[1];
+
+	addr = simple_strtoul(argv[2], NULL, 16);
+	addr += base_address;
+
+	if (size == 4) {
+		uint32_t value = readl(addr);
+		sprintf(buf, "0x%08x", value);
+	} else if (size == 2) {
+		uint16_t value = readw(addr);
+		sprintf(buf, "0x%04x", value);
+	} else {
+		uint8_t value = readb(addr);
+		sprintf(buf, "0x%02x", value);
+	}
+	setenv(var, buf);
+
+	return 0;
+}
+#endif
+
 int do_mem_mm ( cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
 {
 	return mod_mem (cmdtp, 1, flag, argc, argv);
@@ -1145,6 +1185,13 @@ U_BOOT_CMD(
 	"[.b, .w, .l] address [# of objects]"
 );
 
+#ifdef CONFIG_CMD_MEM_GET
+U_BOOT_CMD(
+	mg,	3,	1,	do_mem_mg,
+	"memory get",
+	"[.b, .w, .l] variable address"
+);
+#endif
 
 U_BOOT_CMD(
 	mm,	2,	1,	do_mem_mm,
-- 
1.7.11.5



More information about the U-Boot mailing list