Skip to contentSkip to conversation

Add readmem command to read values out of memory.

PATCH In review Owen Kirby 3 participants 4 messages May 9, 2014 – May 12, 2014

lore
Share & tools
lore

Owen Kirby

author
#0 lore raw

PATCH 1/1 Add readmem command to read values out of memory.

From 299ffc6a961c807b118605bdd9e736c7b096fec9 Mon Sep 17 00:00:00 2001
From: Owen Kirby <[email protected]>
Date: Fri, 9 May 2014 15:14:30 -0700
Subject: [PATCH] Add readmem command to read values out of memory.

I was trying to do something conditionally from a shell script based on the type of image present in flash, and finding it rather cumbersome to do. So I added a command that reads values out of memory and into shell variables as hexadecimal.

---
 commands/Kconfig   |    8 ++++
 commands/Makefile  |    1 +
 commands/readmem.c |  103 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 112 insertions(+)
 create mode 100644 commands/readmem.c
commands/Kconfig +8 -0
index cc014f3..52fa0df 100644
@@ -367,6 +367,14 @@ config CMD_MEMSET
 	  the memset command allows to set regions of memory and files to
 	  a specific value.
 
+config CMD_READMEM
+	tristate
+	default y
+	select CMD_MEMORY
+	prompt "readmem"
+	help
+	  the readmem command reads values from memory into a shell variable.
+
 config CMD_CRC
 	tristate
 	select CRC32
commands/Makefile +1 -0
index e463031..a01e642 100644
@@ -13,6 +13,7 @@ obj-$(CONFIG_CMD_MW)		+= mw.o
 obj-$(CONFIG_CMD_MEMCMP)	+= memcmp.o
 obj-$(CONFIG_CMD_MEMCPY)	+= memcpy.o
 obj-$(CONFIG_CMD_MEMSET)	+= memset.o
+obj-$(CONFIG_CMD_READMEM)	+= readmem.o
 obj-$(CONFIG_CMD_EDIT)		+= edit.o
 obj-$(CONFIG_CMD_EXEC)		+= exec.o
 obj-$(CONFIG_CMD_SLEEP)		+= sleep.o
commands/readmem.c new file +103 -0

103 lines not on this page · raw message

Signature · 7 lines
1.7.9.5


_______________________________________________
barebox mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/barebox
#1 lore raw

On Fri, 09 May 2014 15:59:11 -0700
Owen Kirby <[email protected]> wrote:

Can we simply add an addition option to existing 'md' command?

Quoted 165 lines

From 299ffc6a961c807b118605bdd9e736c7b096fec9 Mon Sep 17 00:00:00 2001
From: Owen Kirby <[email protected]>
Date: Fri, 9 May 2014 15:14:30 -0700
Subject: [PATCH] Add readmem command to read values out of memory.

I was trying to do something conditionally from a shell script based on the type of image present in flash, and finding it rather cumbersome to do. So I added a command that reads values out of memory and into shell variables as hexadecimal.

---
 commands/Kconfig   |    8 ++++
 commands/Makefile  |    1 +
 commands/readmem.c |  103 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 112 insertions(+)
 create mode 100644 commands/readmem.c
commands/Kconfig +8 -0
index cc014f3..52fa0df 100644
@@ -367,6 +367,14 @@ config CMD_MEMSET
 	  the memset command allows to set regions of memory and files to
 	  a specific value.
 
+config CMD_READMEM
+	tristate
+	default y
+	select CMD_MEMORY
+	prompt "readmem"
+	help
+	  the readmem command reads values from memory into a shell variable.
+
 config CMD_CRC
 	tristate
 	select CRC32
commands/Makefile +1 -0
index e463031..a01e642 100644
@@ -13,6 +13,7 @@ obj-$(CONFIG_CMD_MW)		+= mw.o
 obj-$(CONFIG_CMD_MEMCMP)	+= memcmp.o
 obj-$(CONFIG_CMD_MEMCPY)	+= memcpy.o
 obj-$(CONFIG_CMD_MEMSET)	+= memset.o
+obj-$(CONFIG_CMD_READMEM)	+= readmem.o
 obj-$(CONFIG_CMD_EDIT)		+= edit.o
 obj-$(CONFIG_CMD_EXEC)		+= exec.o
 obj-$(CONFIG_CMD_SLEEP)		+= sleep.o
commands/readmem.c new file +103 -0
new file mode 100644
index 0000000..7070637
@@ -0,0 +1,103 @@
+/*
+ * (C) Copyright 2014
+ * Owen Kirby, Exegin Technologies Limited, [email protected]
+ *
+ * See file CREDITS for list of people who contributed to this
+ * project.
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License as
+ * published by the Free Software Foundation; either version 2 of
+ * the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ */
+
+#include <common.h>
+#include <command.h>
+#include <init.h>
+#include <driver.h>
+#include <malloc.h>
+#include <errno.h>
+#include <fs.h>
+#include <fcntl.h>
+#include <getopt.h>
+#include <linux/stat.h>
+#include <xfuncs.h>
+#include <environment.h>
+
+extern char *mem_rw_buf;
+
+static int do_readmem(int argc, char *argv[])
+{
+	loff_t src;
+	int	ret = 0;
+	int fd;
+	char *filename = "/dev/mem";
+	char *varname;
+	int mode = O_RWSIZE_4;
+	int swab = 0;
+
+	if (argc < 2)
+		return COMMAND_ERROR_USAGE;
+
+	if (mem_parse_options(argc, argv, "bwls:x", &mode, &filename, NULL,
+			&swab) < 0)
+		return 1;
+
+	if (optind + 2 > argc)
+		return COMMAND_ERROR_USAGE;
+
+	src = strtoull_suffix(argv[optind], NULL, 0);
+	varname = argv[optind+1];
+	fd = open_and_lseek(filename, mode | O_RDONLY, src);
+	if (fd < 0)
+		return 1;
+
+	/* Read the magic var. */
+	if (read(fd, mem_rw_buf, mode >> O_RWSIZE_SHIFT) < 0) {
+		perror("read");
+		ret = 1;
+	}
+	/* Set the environment variable. */
+	else if (mode == O_RWSIZE_4) {
+		u32 res = *(uint *)mem_rw_buf;
+		char buf[sizeof(res)*2+1];
+		sprintf(buf, "%08x", swab ? __swab32(res) : res);
+		setenv(varname, buf);
+	} else if (mode == O_RWSIZE_2) {
+		u16 res = *(ushort *)mem_rw_buf;
+		char buf[sizeof(res)*2+1];
+		sprintf(buf, "%04x", swab ? __swab16(res) : res);
+		setenv(varname, buf);
+	} else {
+		char buf[sizeof(u_char)*2+1];
+		sprintf(buf, "%02x", *(u_char *)mem_rw_buf);
+		setenv(varname, buf);
+	}
+	close(fd);
+	return ret;
+}
+
+static const __maybe_unused char cmd_readmem_help[] =
+"Usage: readmem [OPTIONS] <src> VAR\n"
+"\n"
+"options:\n"
+"  -s <file>   source file (default /dev/mem)\n"
+"  -b          read a byte\n"
+"  -w          read a halfword (16bit)\n"
+"  -l          read a word (32bit)\n"
+"  -x          swap bytes\n"
+"\n"
+"read a magic value from <src> into variable VAR.\n";
+
+BAREBOX_CMD_START(readmem)
+	.cmd		= do_readmem,
+	.usage		= "read magic values from memory",
+	BAREBOX_CMD_HELP(cmd_readmem_help)
+BAREBOX_CMD_END
+
Signature · 7 lines
1.7.9.5


_______________________________________________
barebox mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/barebox
Signature · 8 lines
-- 
Best regards,
  Antony Pavlov

_______________________________________________
barebox mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/barebox
#2 lore raw
In reply to Owen Kirby
Quoted 8 lines · Owen Kirby

From 299ffc6a961c807b118605bdd9e736c7b096fec9 Mon Sep 17 00:00:00 2001
From: Owen Kirby <[email protected]>
Date: Fri, 9 May 2014 15:14:30 -0700
Subject: [PATCH] Add readmem command to read values out of memory.

I was trying to do something conditionally from a shell script based on the type of image present in flash, and finding it rather cumbersome to do. So I added a command that reads values out of memory and into shell variables as hexadecimal.

Such a functionality might come in handy and indeed it seems to be hard to archieve with other commands.

Have you looked into the filetype stuff? This could solve your problem without having to interpret hex values from shell. See common/filetype.c and commands/filetype.c.

commands/Kconfig config CMD_MEMSET · line 372

Quoted 3 lines
commands/Kconfig +3 -0
+config CMD_READMEM
+	tristate
+	default y

No default y please, just drop this line.

Sascha

Signature · 9 lines
Pengutronix e.K.                           |                             |
Industrial Linux Solutions                 | http://www.pengutronix.de/  |
Peiner Str. 6-8, 31137 Hildesheim, Germany | Phone: +49-5121-206917-0    |
Amtsgericht Hildesheim, HRA 2686           | Fax:   +49-5121-206917-5555 |

_______________________________________________
barebox mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/barebox
author
#3 lore raw
Quoted 9 lines · Sascha Hauer
Quoted 8 lines · Owen Kirby

From 299ffc6a961c807b118605bdd9e736c7b096fec9 Mon Sep 17 00:00:00 2001
From: Owen Kirby <[email protected]>
Date: Fri, 9 May 2014 15:14:30 -0700
Subject: [PATCH] Add readmem command to read values out of memory.

I was trying to do something conditionally from a shell script based on the type of image present in flash, and finding it rather cumbersome to do. So I added a command that reads values out of memory and into shell variables as hexadecimal.

Quoted 6 lines

Such a functionality might come in handy and indeed it seems to be hard to archieve with other commands.

Have you looked into the filetype stuff? This could solve your problem without having to interpret hex values from shell. See common/filetype.c and commands/filetype.c.

Ooh! That's exactly what I needed, and it looks much more sophisticated than what I threw together. I really should have spent more time to RTFM first. Please feel free to drop this patchsince it's no longer needed.

Quoted 5 lines

commands/Kconfig config CMD_MEMSET · line 372

Quoted 4 lines
commands/Kconfig +3 -0
+config CMD_READMEM
+	tristate
+	default y

No default y please, just drop this line.

My bad, that was a copy/paste error.

Thanks,
Owen

_______________________________________________
barebox mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/barebox