On Dec 16, 2005, at 8:12 AM, Marco Gerards wrote:

Hollis Blanchard <[EMAIL PROTECTED]> writes:

This patch adds a "memory" module, which currently contains two
commands:
        Usage: memdump [address [len]]
        Dump the contents of memory.

        Usage: memset address (value [len] | trap)
        Write a 32-bit quantity to memory.

Future commands could include "memload" (load a file into memory) and
"memsearch" (search for a given value or string in a specified area of
memory. I haven't needed those yet.

Personally I prefer to call this module `memdebug'.  In that case it
is clear that it is intended for debugging, not for other memory
related commands (which we might need someday, but are not related at
all).

Comments?

I'd like to do a full review later in the weekend, if possible.

Here is the patch with the module renamed to "memdebug".

-Hollis
Index: ChangeLog
===================================================================
RCS file: /sources/grub/grub2/ChangeLog,v
retrieving revision 1.210
diff -u -p -r1.210 ChangeLog
--- ChangeLog   10 Dec 2005 05:24:58 -0000      1.210
+++ ChangeLog   31 Dec 2005 16:58:56 -0000
@@ -1,3 +1,22 @@
+2005-12-12  Hollis Blanchard  <[EMAIL PROTECTED]>
+
+       * commands/memdebug.c: New file.
+       * kern/powerpc/misc.c: Likewise.
+       * include/grub/powerpc/misc.h: Likewise.
+       * include/grub/i386/misc.h: Likewise.
+       * include/grub/sparc64/misc.h: Likewise.
+       * conf/common.rmk (pkgdata_MODULES): Add memdebug.mod.
+       (memdebug_mod_SOURCES): New variable.
+       (memdebug_mod_CFLAGS): Likewise.
+       (memdebug_mod_LDFLAGS): Likewise.
+       * conf/powerpc-ieee1275.rmk (grub_emu_SOURCES): Add
+       kern/powerpc/misc.c.
+       (grubof_SOURCES): Likewise.
+       (grubof_HEADERS): Add cache.h and cpu/misc.h.
+       * conf/i386-pc.rmk (kernel_img_HEADERS): Likewise.
+       * conf/sparc64-ieee1275.rmk (grubof_HEADERS): Likewise.
+       * include/grub/misc.h (grub_hexdump): New prototype.
+
 2005-12-09  Hollis Blanchard  <[EMAIL PROTECTED]>
 
        * configure.ac: Accept `powerpc64' as host_cpu.
Index: conf/common.rmk
===================================================================
RCS file: /sources/grub/grub2/conf/common.rmk,v
retrieving revision 1.3
diff -u -p -r1.3 common.rmk
--- conf/common.rmk     3 Dec 2005 17:54:55 -0000       1.3
+++ conf/common.rmk     31 Dec 2005 16:58:56 -0000
@@ -111,10 +111,23 @@ gpt_mod_LDFLAGS = $(COMMON_LDFLAGS)
 
 
 # Commands.
-pkgdata_MODULES += hello.mod boot.mod terminal.mod ls.mod      \
-       cmp.mod cat.mod help.mod font.mod search.mod            \
-       loopback.mod default.mod timeout.mod configfile.mod     \
-       terminfo.mod test.mod
+pkgdata_MODULES += \
+       boot.mod \
+       cat.mod \
+       cmp.mod \
+       configfile.mod \
+       default.mod \
+       font.mod \
+       hello.mod \
+       help.mod \
+       ls.mod \
+       loopback.mod \
+       memdebug.mod \
+       timeout.mod \
+       search.mod \
+       terminal.mod \
+       terminfo.mod \
+       test.mod
 
 # For hello.mod.
 hello_mod_SOURCES = hello/hello.c
@@ -191,6 +204,11 @@ terminfo_mod_SOURCES = term/terminfo.c t
 terminfo_mod_CFLAGS = $(COMMON_CFLAGS)
 terminfo_mod_LDFLAGS = $(COMMON_LDFLAGS)
 
+# For memdebug.mod.
+memdebug_mod_SOURCES = commands/memdebug.c
+memdebug_mod_CFLAGS = $(COMMON_CFLAGS)
+memdebug_mod_LDFLAGS = $(COMMON_LDFLAGS)
+
 
 # Misc.
 pkgdata_MODULES += gzio.mod 
Index: conf/i386-pc.rmk
===================================================================
RCS file: /sources/grub/grub2/conf/i386-pc.rmk,v
retrieving revision 1.57
diff -u -p -r1.57 i386-pc.rmk
--- conf/i386-pc.rmk    3 Dec 2005 17:54:55 -0000       1.57
+++ conf/i386-pc.rmk    31 Dec 2005 16:58:57 -0000
@@ -30,9 +30,10 @@ kernel_img_SOURCES = kern/i386/pc/startu
        kern/env.c disk/i386/pc/biosdisk.c \
        term/i386/pc/console.c \
        symlist.c
-kernel_img_HEADERS = arg.h boot.h device.h disk.h dl.h elf.h env.h err.h \
-       file.h fs.h kernel.h loader.h misc.h mm.h net.h parser.h partition.h \
-       pc_partition.h rescue.h symbol.h term.h types.h \
+kernel_img_HEADERS = arg.h boot.h cache.h device.h disk.h dl.h elf.h env.h \
+       err.h file.h fs.h kernel.h loader.h misc.h mm.h net.h parser.h \
+       partition.h pc_partition.h rescue.h symbol.h term.h types.h \
+       cpu/misc.h \
        machine/biosdisk.h machine/boot.h machine/console.h machine/init.h \
        machine/memory.h machine/loader.h machine/time.h machine/vga.h \
        machine/vbe.h
Index: conf/powerpc-ieee1275.rmk
===================================================================
RCS file: /sources/grub/grub2/conf/powerpc-ieee1275.rmk,v
retrieving revision 1.51
diff -u -p -r1.51 powerpc-ieee1275.rmk
--- conf/powerpc-ieee1275.rmk   23 Nov 2005 02:23:08 -0000      1.51
+++ conf/powerpc-ieee1275.rmk   31 Dec 2005 16:58:57 -0000
@@ -9,11 +9,11 @@ COMMON_CFLAGS = -ffreestanding -msoft-fl
 MOSTLYCLEANFILES += grubof_symlist.c kernel_syms.lst
 DEFSYMFILES += kernel_syms.lst
 
-grubof_HEADERS = arg.h boot.h device.h disk.h dl.h elf.h env.h err.h \
+grubof_HEADERS = arg.h boot.h cache.h device.h disk.h dl.h elf.h env.h err.h \
        file.h fs.h kernel.h misc.h mm.h net.h parser.h rescue.h symbol.h \
        term.h types.h powerpc/libgcc.h loader.h \
        partition.h pc_partition.h ieee1275/ieee1275.h machine/time.h \
-       machine/kernel.h
+       machine/kernel.h cpu/misc.h
 
 grubof_symlist.c: $(addprefix include/grub/,$(grubof_HEADERS)) gensymlist.sh
        sh $(srcdir)/gensymlist.sh $(filter %.h,$^) > $@
@@ -54,7 +54,8 @@ grub_emu_SOURCES = commands/boot.c comma
        partmap/acorn.c                                                 \
        util/console.c util/grub-emu.c util/misc.c                      \
        util/i386/pc/biosdisk.c util/i386/pc/getroot.c                  \
-       util/powerpc/ieee1275/misc.c grub_script.tab.c grub_emu_init.c
+       util/powerpc/ieee1275/misc.c grub_script.tab.c grub_emu_init.c  \
+       kern/powerpc/misc.c
 
 grub_emu_LDFLAGS = $(LIBCURSES)
 
@@ -65,7 +66,7 @@ grubof_SOURCES = kern/powerpc/ieee1275/c
        kern/powerpc/ieee1275/init.c term/ieee1275/ofconsole.c          \
        kern/powerpc/ieee1275/openfw.c disk/ieee1275/ofdisk.c           \
        kern/parser.c kern/partition.c kern/env.c kern/powerpc/dl.c     \
-       grubof_symlist.c kern/powerpc/cache.S
+       grubof_symlist.c kern/powerpc/cache.S kern/powerpc/misc.c
 grubof_HEADERS = grub/powerpc/ieee1275/ieee1275.h
 grubof_CFLAGS = $(COMMON_CFLAGS)
 grubof_ASFLAGS = $(COMMON_ASFLAGS)
Index: conf/sparc64-ieee1275.rmk
===================================================================
RCS file: /sources/grub/grub2/conf/sparc64-ieee1275.rmk,v
retrieving revision 1.8
diff -u -p -r1.8 sparc64-ieee1275.rmk
--- conf/sparc64-ieee1275.rmk   18 Nov 2005 10:51:00 -0000      1.8
+++ conf/sparc64-ieee1275.rmk   31 Dec 2005 16:58:57 -0000
@@ -10,11 +10,11 @@ COMMON_LDFLAGS = -melf64_sparc
 MOSTLYCLEANFILES += grubof_symlist.c kernel_syms.lst
 DEFSYMFILES += kernel_syms.lst
 
-grubof_HEADERS = arg.h boot.h device.h disk.h dl.h elf.h env.h err.h \
+grubof_HEADERS = arg.h boot.h cache.h device.h disk.h dl.h elf.h env.h err.h \
        file.h fs.h kernel.h misc.h mm.h net.h parser.h rescue.h symbol.h \
        term.h types.h sparc64/libgcc.h loader.h \
        partition.h pc_partition.h ieee1275/ieee1275.h machine/time.h \
-       machine/kernel.h
+       machine/kernel.h cpu/misc.h
 
 grubof_symlist.c: $(addprefix include/grub/,$(grubof_HEADERS)) gensymlist.sh
        sh $(srcdir)/gensymlist.sh $(filter %.h,$^) > $@
Index: include/grub/misc.h
===================================================================
RCS file: /sources/grub/grub2/include/grub/misc.h,v
retrieving revision 1.17
diff -u -p -r1.17 misc.h
--- include/grub/misc.h 24 Oct 2005 10:23:46 -0000      1.17
+++ include/grub/misc.h 31 Dec 2005 16:58:57 -0000
@@ -76,4 +76,6 @@ grub_ssize_t EXPORT_FUNC(grub_utf8_to_uc
                                             const grub_uint8_t *src,
                                             grub_size_t size);
 
+void EXPORT_FUNC(grub_hexdump) (const void *ptr, unsigned long bytes);
+
 #endif /* ! GRUB_MISC_HEADER */
Index: include/grub/i386/misc.h
===================================================================
RCS file: include/grub/i386/misc.h
diff -N include/grub/i386/misc.h
--- /dev/null   1 Jan 1970 00:00:00 -0000
+++ include/grub/i386/misc.h    31 Dec 2005 16:58:57 -0000
@@ -0,0 +1,25 @@
+/*
+ *  GRUB  --  GRand Unified Bootloader
+ *  Copyright (C) 2005  Free Software Foundation, Inc.
+ *
+ *  GRUB 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.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with GRUB; if not, write to the Free Software
+ *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ */
+
+#ifndef GRUB_MISC_CPU_HEADER
+#define GRUB_MISC_CPU_HEADER   1
+
+#undef HAVE_CPU_INSERT_TRAP
+
+#endif /* ! GRUB_MISC_CPU_HEADER */
Index: include/grub/powerpc/misc.h
===================================================================
RCS file: include/grub/powerpc/misc.h
diff -N include/grub/powerpc/misc.h
--- /dev/null   1 Jan 1970 00:00:00 -0000
+++ include/grub/powerpc/misc.h 31 Dec 2005 16:58:57 -0000
@@ -0,0 +1,26 @@
+/*
+ *  GRUB  --  GRand Unified Bootloader
+ *  Copyright (C) 2005  Free Software Foundation, Inc.
+ *
+ *  GRUB 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.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with GRUB; if not, write to the Free Software
+ *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ */
+
+#ifndef GRUB_MISC_CPU_HEADER
+#define GRUB_MISC_CPU_HEADER   1
+
+#define HAVE_CPU_INSERT_TRAP
+int EXPORT_FUNC(grub_cpu_insert_trap) (void *address);
+
+#endif /* ! GRUB_MISC_CPU_HEADER */
Index: include/grub/sparc64/misc.h
===================================================================
RCS file: include/grub/sparc64/misc.h
diff -N include/grub/sparc64/misc.h
--- /dev/null   1 Jan 1970 00:00:00 -0000
+++ include/grub/sparc64/misc.h 31 Dec 2005 16:58:57 -0000
@@ -0,0 +1,25 @@
+/*
+ *  GRUB  --  GRand Unified Bootloader
+ *  Copyright (C) 2005  Free Software Foundation, Inc.
+ *
+ *  GRUB 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.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with GRUB; if not, write to the Free Software
+ *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ */
+
+#ifndef GRUB_MISC_CPU_HEADER
+#define GRUB_MISC_CPU_HEADER   1
+
+#undef HAVE_CPU_INSERT_TRAP
+
+#endif /* ! GRUB_MISC_CPU_HEADER */
Index: kern/misc.c
===================================================================
RCS file: /sources/grub/grub2/kern/misc.c,v
retrieving revision 1.25
diff -u -p -r1.25 misc.c
--- kern/misc.c 28 Oct 2005 03:14:33 -0000      1.25
+++ kern/misc.c 31 Dec 2005 16:58:57 -0000
@@ -875,3 +875,29 @@ grub_utf8_to_ucs4 (grub_uint32_t *dest, 
 
   return p - dest;
 }
+
+void
+grub_hexdump (const void *ptr, unsigned long bytes)
+{
+  const char *chr;
+  unsigned long i;
+  unsigned long j;
+  const unsigned int stride = 4 * sizeof (grub_uint32_t);
+
+  /* Word-align `ptr'.  */
+  chr = (char *) ((unsigned long) ptr & ~0x3);
+
+  for (i = 0; i < bytes; i += stride)
+    {
+      int *word = (int *) (chr + i);
+      grub_printf ("%08x: %08x %08x %08x %08x  ", (grub_uint32_t) word,
+                  word[0], word[1], word[2], word[3]);
+
+      for (j = 0; j < stride; j++)
+       if (grub_isprint (chr[i+j]))
+         grub_putchar (chr[i+j]);
+       else
+         grub_putchar ('.');
+      grub_putchar ('\n');
+    }
+}
Index: kern/powerpc/misc.c
===================================================================
RCS file: kern/powerpc/misc.c
diff -N kern/powerpc/misc.c
--- /dev/null   1 Jan 1970 00:00:00 -0000
+++ kern/powerpc/misc.c 31 Dec 2005 16:58:57 -0000
@@ -0,0 +1,31 @@
+/*
+ *  GRUB  --  GRand Unified Bootloader
+ *  Copyright (C) 2005  Free Software Foundation, Inc.
+ *
+ *  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.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with this program; if not, write to the Free Software
+ *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ */
+
+#include <grub/misc.h>
+#include <grub/cache.h>
+
+int grub_cpu_insert_trap (void *address)
+{
+  grub_uint32_t *instruction = address;
+
+  *instruction = 0x7fe00008; /* trap */
+  grub_arch_sync_caches ((void *) address, sizeof (*instruction));
+
+  return 0;
+}
Index: commands/memdebug.c
===================================================================
RCS file: commands/memdebug.c
diff -N commands/memdebug.c
--- /dev/null   1 Jan 1970 00:00:00 -0000
+++ commands/memdebug.c 31 Dec 2005 17:02:52 -0000
@@ -0,0 +1,116 @@
+/* memdebug.c - commands to manipulate the contents of memory  */
+/*
+ *  GRUB  --  GRand Unified Bootloader
+ *  Copyright (C) 2003,2005  Free Software Foundation, Inc.
+ *
+ *  GRUB 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.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with GRUB; if not, write to the Free Software
+ *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ */
+
+#include <grub/normal.h>
+#include <grub/dl.h>
+#include <grub/arg.h>
+#include <grub/term.h>
+#include <grub/misc.h>
+#include <grub/cache.h>
+#include <grub/cpu/misc.h>
+
+static grub_err_t
+grub_cmd_memdump (struct grub_arg_list *state __attribute__ ((unused)),
+             int argc, char **args)
+
+{
+  static unsigned long address = 0;
+  static unsigned long bytes = 64;
+
+  if (argc > 2)
+    return grub_error (GRUB_ERR_BAD_ARGUMENT, "too many arguments");
+
+  if (argc > 0)
+    address = grub_strtoul (args[0], 0, 0);
+
+  if (argc > 1)
+    bytes = grub_strtoul (args[1], 0, 0);
+
+  grub_hexdump ((void *) address, bytes);
+
+  address += bytes; /* Remember position for next memdump.  */
+
+  return 0;
+}
+
+static grub_err_t
+grub_cmd_memset (struct grub_arg_list *state __attribute__ ((unused)),
+             int argc, char **args)
+
+{
+  grub_uint32_t *ptr;
+  grub_uint32_t value;
+  int repeat = 1;
+  int i;
+
+  if (argc < 2)
+    return grub_error (GRUB_ERR_BAD_ARGUMENT, "too few arguments");
+
+  if (argc > 3)
+    return grub_error (GRUB_ERR_BAD_ARGUMENT, "too many arguments");
+
+  ptr = (grub_uint32_t *) grub_strtoul (args[0], 0, 0);
+
+#ifdef HAVE_CPU_INSERT_TRAP
+  if (0 == grub_strcmp ("trap", args[1]))
+    return grub_cpu_insert_trap (ptr);
+#endif /* HAVE_CPU_INSERT_TRAP */
+
+  value = grub_strtoul (args[1], 0, 0);
+
+  if (argc > 2)
+    {
+      int bytes = grub_strtoul (args[2], 0, 0);
+      repeat = bytes / sizeof (grub_uint32_t);
+    }
+
+  for (i = 0; i < repeat; i++)
+    {
+      *ptr = value;
+      ptr++;
+    }
+  grub_arch_sync_caches (ptr, repeat * sizeof (grub_uint32_t));
+
+  return 0;
+}
+
+
+GRUB_MOD_INIT(memdebug)
+{
+  (void) mod;
+  grub_register_command ("memdump", grub_cmd_memdump, GRUB_COMMAND_FLAG_BOTH,
+                        "memdump [address [len]]",
+                        "Dump the contents of memory.", 0);
+#ifdef HAVE_CPU_INSERT_TRAP
+  grub_register_command ("memset", grub_cmd_memset, GRUB_COMMAND_FLAG_BOTH,
+                        "memset address (value [len] | trap)",
+                        "Write a 32-bit quantity to memory.", 0);
+#else /* HAVE_CPU_INSERT_TRAP */
+  grub_register_command ("memset", grub_cmd_memset, GRUB_COMMAND_FLAG_BOTH,
+                        "memset address value [len]",
+                        "Write a 32-bit quantity to memory.", 0);
+#endif /* HAVE_CPU_INSERT_TRAP */
+}
+
+GRUB_MOD_FINI(memdebug)
+{
+  grub_unregister_command ("memdump");
+  grub_unregister_command ("memset");
+}
_______________________________________________
Grub-devel mailing list
Grub-devel@gnu.org
http://lists.gnu.org/mailman/listinfo/grub-devel

Reply via email to