mail archive of the barebox mailing list
 help / color / mirror / Atom feed
* [PATCH] Add readmem command to read values out of memory.
@ 2014-05-09 22:59 Owen Kirby
  2014-05-10 13:32 ` Antony Pavlov
  2014-05-12  5:59 ` Sascha Hauer
  0 siblings, 2 replies; 4+ messages in thread
From: Owen Kirby @ 2014-05-09 22:59 UTC (permalink / raw)
  To: barebox

From 299ffc6a961c807b118605bdd9e736c7b096fec9 Mon Sep 17 00:00:00 2001
From: Owen Kirby <osk@exegin.com>
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.

Signed-off-by: Owen Kirby <osk@exegin.com>
---
 commands/Kconfig   |    8 ++++
 commands/Makefile  |    1 +
 commands/readmem.c |  103 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 112 insertions(+)
 create mode 100644 commands/readmem.c

diff --git a/commands/Kconfig b/commands/Kconfig
index cc014f3..52fa0df 100644
--- a/commands/Kconfig
+++ b/commands/Kconfig
@@ -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
diff --git a/commands/Makefile b/commands/Makefile
index e463031..a01e642 100644
--- a/commands/Makefile
+++ b/commands/Makefile
@@ -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
diff --git a/commands/readmem.c b/commands/readmem.c
new file mode 100644
index 0000000..7070637
--- /dev/null
+++ b/commands/readmem.c
@@ -0,0 +1,103 @@
+/*
+ * (C) Copyright 2014
+ * Owen Kirby, Exegin Technologies Limited, osk@exegin.com
+ *
+ * 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
+
-- 
1.7.9.5


_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] Add readmem command to read values out of memory.
  2014-05-09 22:59 [PATCH] Add readmem command to read values out of memory Owen Kirby
@ 2014-05-10 13:32 ` Antony Pavlov
  2014-05-12  5:59 ` Sascha Hauer
  1 sibling, 0 replies; 4+ messages in thread
From: Antony Pavlov @ 2014-05-10 13:32 UTC (permalink / raw)
  To: Owen Kirby; +Cc: barebox

On Fri, 09 May 2014 15:59:11 -0700
Owen Kirby <osk@exegin.com> wrote:

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

> From 299ffc6a961c807b118605bdd9e736c7b096fec9 Mon Sep 17 00:00:00 2001
> From: Owen Kirby <osk@exegin.com>
> 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.
> 
> Signed-off-by: Owen Kirby <osk@exegin.com>
> ---
>  commands/Kconfig   |    8 ++++
>  commands/Makefile  |    1 +
>  commands/readmem.c |  103 ++++++++++++++++++++++++++++++++++++++++++++++++++++
>  3 files changed, 112 insertions(+)
>  create mode 100644 commands/readmem.c
> 
> diff --git a/commands/Kconfig b/commands/Kconfig
> index cc014f3..52fa0df 100644
> --- a/commands/Kconfig
> +++ b/commands/Kconfig
> @@ -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
> diff --git a/commands/Makefile b/commands/Makefile
> index e463031..a01e642 100644
> --- a/commands/Makefile
> +++ b/commands/Makefile
> @@ -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
> diff --git a/commands/readmem.c b/commands/readmem.c
> new file mode 100644
> index 0000000..7070637
> --- /dev/null
> +++ b/commands/readmem.c
> @@ -0,0 +1,103 @@
> +/*
> + * (C) Copyright 2014
> + * Owen Kirby, Exegin Technologies Limited, osk@exegin.com
> + *
> + * 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
> +
> -- 
> 1.7.9.5
> 
> 
> _______________________________________________
> barebox mailing list
> barebox@lists.infradead.org
> http://lists.infradead.org/mailman/listinfo/barebox


-- 
-- 
Best regards,
  Antony Pavlov

_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] Add readmem command to read values out of memory.
  2014-05-09 22:59 [PATCH] Add readmem command to read values out of memory Owen Kirby
  2014-05-10 13:32 ` Antony Pavlov
@ 2014-05-12  5:59 ` Sascha Hauer
  2014-05-12 18:36   ` Owen Kirby
  1 sibling, 1 reply; 4+ messages in thread
From: Sascha Hauer @ 2014-05-12  5:59 UTC (permalink / raw)
  To: Owen Kirby; +Cc: barebox

On Fri, May 09, 2014 at 03:59:11PM -0700, Owen Kirby wrote:
> From 299ffc6a961c807b118605bdd9e736c7b096fec9 Mon Sep 17 00:00:00 2001
> From: Owen Kirby <osk@exegin.com>
> 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.

> +config CMD_READMEM
> +	tristate
> +	default y

No default y please, just drop this line.

Sascha

-- 
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
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] Add readmem command to read values out of memory.
  2014-05-12  5:59 ` Sascha Hauer
@ 2014-05-12 18:36   ` Owen Kirby
  0 siblings, 0 replies; 4+ messages in thread
From: Owen Kirby @ 2014-05-12 18:36 UTC (permalink / raw)
  To: Sascha Hauer; +Cc: barebox

On 14-05-11 10:59 PM, Sascha Hauer wrote:
> On Fri, May 09, 2014 at 03:59:11PM -0700, Owen Kirby wrote:
>> From 299ffc6a961c807b118605bdd9e736c7b096fec9 Mon Sep 17 00:00:00 2001
>> From: Owen Kirby <osk@exegin.com>
>> 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.
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.
>
>> +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
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2014-05-12 18:36 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2014-05-09 22:59 [PATCH] Add readmem command to read values out of memory Owen Kirby
2014-05-10 13:32 ` Antony Pavlov
2014-05-12  5:59 ` Sascha Hauer
2014-05-12 18:36   ` Owen Kirby

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox