Indentation fix, cleanup.
[AROS.git] / arch / all-pc / boot / grub2-aros / grub-core / commands / pcidump.c
blobf99ad4a216f485c99778277a8c83747d28c9682f
1 /* lspci.c - List PCI devices. */
2 /*
3 * GRUB -- GRand Unified Bootloader
4 * Copyright (C) 2013 Free Software Foundation, Inc.
6 * GRUB is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * GRUB is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
20 #include <grub/pci.h>
21 #include <grub/dl.h>
22 #include <grub/misc.h>
23 #include <grub/extcmd.h>
24 #include <grub/env.h>
25 #include <grub/mm.h>
26 #include <grub/i18n.h>
28 GRUB_MOD_LICENSE ("GPLv3+");
30 struct iter_cxt
32 grub_uint32_t pciid_check_mask, pciid_check_value;
33 int bus, device, function;
34 int check_bus, check_device, check_function;
37 static const struct grub_arg_option options[] =
39 {0, 'd', 0, N_("Select device by vendor and device IDs."),
40 N_("[vendor]:[device]"), ARG_TYPE_STRING},
41 {0, 's', 0, N_("Select device by its position on the bus."),
42 N_("[bus]:[slot][.func]"), ARG_TYPE_STRING},
43 {0, 0, 0, 0, 0, 0}
46 static int
47 grub_pcidump_iter (grub_pci_device_t dev, grub_pci_id_t pciid,
48 void *data)
50 struct iter_cxt *ctx = data;
51 grub_pci_address_t addr;
52 int i;
54 if ((pciid & ctx->pciid_check_mask) != ctx->pciid_check_value)
55 return 0;
57 if (ctx->check_bus && grub_pci_get_bus (dev) != ctx->bus)
58 return 0;
60 if (ctx->check_device && grub_pci_get_device (dev) != ctx->device)
61 return 0;
63 if (ctx->check_function && grub_pci_get_function (dev) != ctx->function)
64 return 0;
66 for (i = 0; i < 256; i += 4)
68 addr = grub_pci_make_address (dev, i);
69 grub_printf ("%08x ", grub_pci_read (addr));
70 if ((i & 0xc) == 0xc)
71 grub_printf ("\n");
74 return 0;
77 static grub_err_t
78 grub_cmd_pcidump (grub_extcmd_context_t ctxt,
79 int argc __attribute__ ((unused)),
80 char **argv __attribute__ ((unused)))
82 const char *ptr;
83 struct iter_cxt ctx =
85 .pciid_check_value = 0,
86 .pciid_check_mask = 0,
87 .check_bus = 0,
88 .check_device = 0,
89 .check_function = 0,
90 .bus = 0,
91 .function = 0,
92 .device = 0
95 if (ctxt->state[0].set)
97 ptr = ctxt->state[0].arg;
98 ctx.pciid_check_value |= (grub_strtoul (ptr, (char **) &ptr, 16) & 0xffff);
99 if (grub_errno == GRUB_ERR_BAD_NUMBER)
101 grub_errno = GRUB_ERR_NONE;
102 ptr = ctxt->state[0].arg;
104 else
105 ctx.pciid_check_mask |= 0xffff;
106 if (grub_errno)
107 return grub_errno;
108 if (*ptr != ':')
109 return grub_error (GRUB_ERR_BAD_ARGUMENT, N_("missing `%c' symbol"), ':');
110 ptr++;
111 ctx.pciid_check_value |= (grub_strtoul (ptr, (char **) &ptr, 16) & 0xffff)
112 << 16;
113 if (grub_errno == GRUB_ERR_BAD_NUMBER)
114 grub_errno = GRUB_ERR_NONE;
115 else
116 ctx.pciid_check_mask |= 0xffff0000;
119 ctx.pciid_check_value &= ctx.pciid_check_mask;
121 if (ctxt->state[1].set)
123 const char *optr;
125 ptr = ctxt->state[1].arg;
126 optr = ptr;
127 ctx.bus = grub_strtoul (ptr, (char **) &ptr, 16);
128 if (grub_errno == GRUB_ERR_BAD_NUMBER)
130 grub_errno = GRUB_ERR_NONE;
131 ptr = optr;
133 else
134 ctx.check_bus = 1;
135 if (grub_errno)
136 return grub_errno;
137 if (*ptr != ':')
138 return grub_error (GRUB_ERR_BAD_ARGUMENT, N_("missing `%c' symbol"), ':');
139 ptr++;
140 optr = ptr;
141 ctx.device = grub_strtoul (ptr, (char **) &ptr, 16);
142 if (grub_errno == GRUB_ERR_BAD_NUMBER)
144 grub_errno = GRUB_ERR_NONE;
145 ptr = optr;
147 else
148 ctx.check_device = 1;
149 if (*ptr == '.')
151 ptr++;
152 ctx.function = grub_strtoul (ptr, (char **) &ptr, 16);
153 if (grub_errno)
154 return grub_errno;
155 ctx.check_function = 1;
159 grub_pci_iterate (grub_pcidump_iter, &ctx);
160 return GRUB_ERR_NONE;
163 static grub_extcmd_t cmd;
165 GRUB_MOD_INIT(pcidump)
167 cmd = grub_register_extcmd ("pcidump", grub_cmd_pcidump, 0,
168 N_("[-s POSITION] [-d DEVICE]"),
169 N_("Show raw dump of the PCI configuration space."), options);
172 GRUB_MOD_FINI(pcidump)
174 grub_unregister_extcmd (cmd);