a5bd9f6
From d230dcf6c99b852adcee4a26c4c81dd15b83ce56 Mon Sep 17 00:00:00 2001
a5bd9f6
From: Vladimir 'phcoder' Serbinenko <phcoder@gmail.com>
a5bd9f6
Date: Wed, 16 Jan 2013 20:44:11 +0100
a5bd9f6
Subject: [PATCH 111/364] 	Add new command pcidump.
a5bd9f6
a5bd9f6
---
a5bd9f6
 ChangeLog                    |   4 ++
a5bd9f6
 grub-core/Makefile.core.def  |   6 ++
a5bd9f6
 grub-core/commands/pcidump.c | 165 +++++++++++++++++++++++++++++++++++++++++++
a5bd9f6
 grub-core/commands/setpci.c  |   2 +-
a5bd9f6
 4 files changed, 176 insertions(+), 1 deletion(-)
a5bd9f6
 create mode 100644 grub-core/commands/pcidump.c
a5bd9f6
a5bd9f6
diff --git a/ChangeLog b/ChangeLog
a5bd9f6
index f1c53e3..9922c06 100644
a5bd9f6
--- a/ChangeLog
a5bd9f6
+++ b/ChangeLog
a5bd9f6
@@ -1,5 +1,9 @@
a5bd9f6
 2013-01-16  Vladimir Serbinenko  <phcoder@gmail.com>
a5bd9f6
 
a5bd9f6
+	Add new command pcidump.
a5bd9f6
+
a5bd9f6
+2013-01-16  Vladimir Serbinenko  <phcoder@gmail.com>
a5bd9f6
+
a5bd9f6
 	New terminal outputs using serial: morse and spkmodem.
a5bd9f6
 
a5bd9f6
 2013-01-16  Vladimir Serbinenko  <phcoder@gmail.com>
a5bd9f6
diff --git a/grub-core/Makefile.core.def b/grub-core/Makefile.core.def
a5bd9f6
index baf80b8..4609a4b 100644
a5bd9f6
--- a/grub-core/Makefile.core.def
a5bd9f6
+++ b/grub-core/Makefile.core.def
a5bd9f6
@@ -828,6 +828,12 @@ module = {
a5bd9f6
 };
a5bd9f6
 
a5bd9f6
 module = {
a5bd9f6
+  name = pcidump;
a5bd9f6
+  common = commands/pcidump.c;
a5bd9f6
+  enable = pci;
a5bd9f6
+};
a5bd9f6
+
a5bd9f6
+module = {
a5bd9f6
   name = sleep;
a5bd9f6
   common = commands/sleep.c;
a5bd9f6
 };
a5bd9f6
diff --git a/grub-core/commands/pcidump.c b/grub-core/commands/pcidump.c
a5bd9f6
new file mode 100644
a5bd9f6
index 0000000..a49cf93
a5bd9f6
--- /dev/null
a5bd9f6
+++ b/grub-core/commands/pcidump.c
a5bd9f6
@@ -0,0 +1,165 @@
a5bd9f6
+/* lspci.c - List PCI devices.  */
a5bd9f6
+/*
a5bd9f6
+ *  GRUB  --  GRand Unified Bootloader
a5bd9f6
+ *  Copyright (C) 2013  Free Software Foundation, Inc.
a5bd9f6
+ *
a5bd9f6
+ *  GRUB is free software: you can redistribute it and/or modify
a5bd9f6
+ *  it under the terms of the GNU General Public License as published by
a5bd9f6
+ *  the Free Software Foundation, either version 3 of the License, or
a5bd9f6
+ *  (at your option) any later version.
a5bd9f6
+ *
a5bd9f6
+ *  GRUB is distributed in the hope that it will be useful,
a5bd9f6
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
a5bd9f6
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
a5bd9f6
+ *  GNU General Public License for more details.
a5bd9f6
+ *
a5bd9f6
+ *  You should have received a copy of the GNU General Public License
a5bd9f6
+ *  along with GRUB.  If not, see <http://www.gnu.org/licenses/>.
a5bd9f6
+ */
a5bd9f6
+
a5bd9f6
+#include <grub/pci.h>
a5bd9f6
+#include <grub/dl.h>
a5bd9f6
+#include <grub/misc.h>
a5bd9f6
+#include <grub/extcmd.h>
a5bd9f6
+#include <grub/env.h>
a5bd9f6
+#include <grub/mm.h>
a5bd9f6
+#include <grub/i18n.h>
a5bd9f6
+
a5bd9f6
+GRUB_MOD_LICENSE ("GPLv3+");
a5bd9f6
+
a5bd9f6
+static grub_uint32_t pciid_check_mask, pciid_check_value;
a5bd9f6
+static int bus, device, function;
a5bd9f6
+static int check_bus, check_device, check_function;
a5bd9f6
+
a5bd9f6
+static const struct grub_arg_option options[] =
a5bd9f6
+  {
a5bd9f6
+    {0, 'd', 0, N_("Select device by vendor and device IDs."),
a5bd9f6
+     N_("[vendor]:[device]"), ARG_TYPE_STRING},
a5bd9f6
+    {0, 's', 0, N_("Select device by its position on the bus."),
a5bd9f6
+     N_("[bus]:[slot][.func]"), ARG_TYPE_STRING},
a5bd9f6
+    {0, 0, 0, 0, 0, 0}
a5bd9f6
+  };
a5bd9f6
+
a5bd9f6
+static int
a5bd9f6
+grub_pcidump_iter (grub_pci_device_t dev, grub_pci_id_t pciid,
a5bd9f6
+		  void *data __attribute__ ((unused)))
a5bd9f6
+{
a5bd9f6
+  grub_pci_address_t addr;
a5bd9f6
+  int i;
a5bd9f6
+
a5bd9f6
+  if ((pciid & pciid_check_mask) != pciid_check_value)
a5bd9f6
+    return 0;
a5bd9f6
+
a5bd9f6
+  if (check_bus && grub_pci_get_bus (dev) != bus)
a5bd9f6
+    return 0;
a5bd9f6
+
a5bd9f6
+  if (check_device && grub_pci_get_device (dev) != device)
a5bd9f6
+    return 0;
a5bd9f6
+
a5bd9f6
+  if (check_function && grub_pci_get_function (dev) != function)
a5bd9f6
+    return 0;
a5bd9f6
+
a5bd9f6
+  for (i = 0; i < 256; i += 4)
a5bd9f6
+    {
a5bd9f6
+      addr = grub_pci_make_address (dev, i);
a5bd9f6
+      grub_printf ("%08x ", grub_pci_read (addr));
a5bd9f6
+      if ((i & 0xc) == 0xc)
a5bd9f6
+	grub_printf ("\n");
a5bd9f6
+    }
a5bd9f6
+
a5bd9f6
+  return 0;
a5bd9f6
+}
a5bd9f6
+
a5bd9f6
+static grub_err_t
a5bd9f6
+grub_cmd_pcidump (grub_extcmd_context_t ctxt,
a5bd9f6
+		  int argc __attribute__ ((unused)),
a5bd9f6
+		  char **argv __attribute__ ((unused)))
a5bd9f6
+{
a5bd9f6
+  const char *ptr;
a5bd9f6
+
a5bd9f6
+  pciid_check_value = 0;
a5bd9f6
+  pciid_check_mask = 0;
a5bd9f6
+
a5bd9f6
+  if (ctxt->state[0].set)
a5bd9f6
+    {
a5bd9f6
+      ptr = ctxt->state[0].arg;
a5bd9f6
+      pciid_check_value |= (grub_strtoul (ptr, (char **) &ptr, 16) & 0xffff);
a5bd9f6
+      if (grub_errno == GRUB_ERR_BAD_NUMBER)
a5bd9f6
+	{
a5bd9f6
+	  grub_errno = GRUB_ERR_NONE;
a5bd9f6
+	  ptr = ctxt->state[0].arg;
a5bd9f6
+	}
a5bd9f6
+      else
a5bd9f6
+	pciid_check_mask |= 0xffff;
a5bd9f6
+      if (grub_errno)
a5bd9f6
+	return grub_errno;
a5bd9f6
+      if (*ptr != ':')
a5bd9f6
+	return grub_error (GRUB_ERR_BAD_ARGUMENT, N_("missing `%c' symbol"), ':');
a5bd9f6
+      ptr++;
a5bd9f6
+      pciid_check_value |= (grub_strtoul (ptr, (char **) &ptr, 16) & 0xffff)
a5bd9f6
+	<< 16;
a5bd9f6
+      if (grub_errno == GRUB_ERR_BAD_NUMBER)
a5bd9f6
+	grub_errno = GRUB_ERR_NONE;
a5bd9f6
+      else
a5bd9f6
+	pciid_check_mask |= 0xffff0000;
a5bd9f6
+    }
a5bd9f6
+
a5bd9f6
+  pciid_check_value &= pciid_check_mask;
a5bd9f6
+
a5bd9f6
+  check_bus = check_device = check_function = 0;
a5bd9f6
+
a5bd9f6
+  if (ctxt->state[1].set)
a5bd9f6
+    {
a5bd9f6
+      const char *optr;
a5bd9f6
+      
a5bd9f6
+      ptr = ctxt->state[1].arg;
a5bd9f6
+      optr = ptr;
a5bd9f6
+      bus = grub_strtoul (ptr, (char **) &ptr, 16);
a5bd9f6
+      if (grub_errno == GRUB_ERR_BAD_NUMBER)
a5bd9f6
+	{
a5bd9f6
+	  grub_errno = GRUB_ERR_NONE;
a5bd9f6
+	  ptr = optr;
a5bd9f6
+	}
a5bd9f6
+      else
a5bd9f6
+	check_bus = 1;
a5bd9f6
+      if (grub_errno)
a5bd9f6
+	return grub_errno;
a5bd9f6
+      if (*ptr != ':')
a5bd9f6
+	return grub_error (GRUB_ERR_BAD_ARGUMENT, N_("missing `%c' symbol"), ':');
a5bd9f6
+      ptr++;
a5bd9f6
+      optr = ptr;
a5bd9f6
+      device = grub_strtoul (ptr, (char **) &ptr, 16);
a5bd9f6
+      if (grub_errno == GRUB_ERR_BAD_NUMBER)
a5bd9f6
+	{
a5bd9f6
+	  grub_errno = GRUB_ERR_NONE;
a5bd9f6
+	  ptr = optr;
a5bd9f6
+	}
a5bd9f6
+      else
a5bd9f6
+	check_device = 1;
a5bd9f6
+      if (*ptr == '.')
a5bd9f6
+	{
a5bd9f6
+	  ptr++;
a5bd9f6
+	  function = grub_strtoul (ptr, (char **) &ptr, 16);
a5bd9f6
+	  if (grub_errno)
a5bd9f6
+	    return grub_errno;
a5bd9f6
+	  check_function = 1;
a5bd9f6
+	}
a5bd9f6
+    }
a5bd9f6
+
a5bd9f6
+  grub_pci_iterate (grub_pcidump_iter, NULL);
a5bd9f6
+  return GRUB_ERR_NONE;
a5bd9f6
+}
a5bd9f6
+
a5bd9f6
+static grub_extcmd_t cmd;
a5bd9f6
+
a5bd9f6
+GRUB_MOD_INIT(setpci)
a5bd9f6
+{
a5bd9f6
+  cmd = grub_register_extcmd ("pcidump", grub_cmd_pcidump, 0,
a5bd9f6
+			      N_("[-s POSITION] [-d DEVICE]"),
a5bd9f6
+			      N_("Dump PCI configuration space."), options);
a5bd9f6
+}
a5bd9f6
+
a5bd9f6
+GRUB_MOD_FINI(setpci)
a5bd9f6
+{
a5bd9f6
+  grub_unregister_extcmd (cmd);
a5bd9f6
+}
a5bd9f6
diff --git a/grub-core/commands/setpci.c b/grub-core/commands/setpci.c
a5bd9f6
index 6fdf0e0..4eaba7c 100644
a5bd9f6
--- a/grub-core/commands/setpci.c
a5bd9f6
+++ b/grub-core/commands/setpci.c
a5bd9f6
@@ -331,7 +331,7 @@ GRUB_MOD_INIT(setpci)
a5bd9f6
 {
a5bd9f6
   cmd = grub_register_extcmd ("setpci", grub_cmd_setpci, 0,
a5bd9f6
 			      N_("[-s POSITION] [-d DEVICE] [-v VAR] "
a5bd9f6
-				 "[REGISTER][=VALUE[:MASK]]"),
a5bd9f6
+				 "REGISTER[=VALUE[:MASK]]"),
a5bd9f6
 			      N_("Manipulate PCI devices."), options);
a5bd9f6
 }
a5bd9f6
 
a5bd9f6
-- 
a5bd9f6
1.8.1.4
a5bd9f6