1 /* lspci.c - List PCI devices. */
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/>.
22 #include <grub/misc.h>
23 #include <grub/extcmd.h>
26 #include <grub/i18n.h>
28 GRUB_MOD_LICENSE ("GPLv3+");
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
},
47 grub_pcidump_iter (grub_pci_device_t dev
, grub_pci_id_t pciid
,
50 struct iter_cxt
*ctx
= data
;
51 grub_pci_address_t addr
;
54 if ((pciid
& ctx
->pciid_check_mask
) != ctx
->pciid_check_value
)
57 if (ctx
->check_bus
&& grub_pci_get_bus (dev
) != ctx
->bus
)
60 if (ctx
->check_device
&& grub_pci_get_device (dev
) != ctx
->device
)
63 if (ctx
->check_function
&& grub_pci_get_function (dev
) != ctx
->function
)
66 for (i
= 0; i
< 256; i
+= 4)
68 addr
= grub_pci_make_address (dev
, i
);
69 grub_printf ("%08x ", grub_pci_read (addr
));
78 grub_cmd_pcidump (grub_extcmd_context_t ctxt
,
79 int argc
__attribute__ ((unused
)),
80 char **argv
__attribute__ ((unused
)))
85 .pciid_check_value
= 0,
86 .pciid_check_mask
= 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
;
105 ctx
.pciid_check_mask
|= 0xffff;
109 return grub_error (GRUB_ERR_BAD_ARGUMENT
, N_("missing `%c' symbol"), ':');
111 ctx
.pciid_check_value
|= (grub_strtoul (ptr
, (char **) &ptr
, 16) & 0xffff)
113 if (grub_errno
== GRUB_ERR_BAD_NUMBER
)
114 grub_errno
= GRUB_ERR_NONE
;
116 ctx
.pciid_check_mask
|= 0xffff0000;
119 ctx
.pciid_check_value
&= ctx
.pciid_check_mask
;
121 if (ctxt
->state
[1].set
)
125 ptr
= ctxt
->state
[1].arg
;
127 ctx
.bus
= grub_strtoul (ptr
, (char **) &ptr
, 16);
128 if (grub_errno
== GRUB_ERR_BAD_NUMBER
)
130 grub_errno
= GRUB_ERR_NONE
;
138 return grub_error (GRUB_ERR_BAD_ARGUMENT
, N_("missing `%c' symbol"), ':');
141 ctx
.device
= grub_strtoul (ptr
, (char **) &ptr
, 16);
142 if (grub_errno
== GRUB_ERR_BAD_NUMBER
)
144 grub_errno
= GRUB_ERR_NONE
;
148 ctx
.check_device
= 1;
152 ctx
.function
= grub_strtoul (ptr
, (char **) &ptr
, 16);
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
);