]> git.proxmox.com Git - grub2.git/blob - grub-core/commands/pcidump.c
malloc: Use overflow checking primitives where we do complex allocations
[grub2.git] / grub-core / commands / pcidump.c
1 /* lspci.c - List PCI devices. */
2 /*
3 * GRUB -- GRand Unified Bootloader
4 * Copyright (C) 2013 Free Software Foundation, Inc.
5 *
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.
10 *
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.
15 *
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/>.
18 */
19
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>
27
28 GRUB_MOD_LICENSE ("GPLv3+");
29
30 struct iter_cxt
31 {
32 grub_uint32_t pciid_check_mask, pciid_check_value;
33 int bus, device, function;
34 int check_bus, check_device, check_function;
35 };
36
37 static const struct grub_arg_option options[] =
38 {
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}
44 };
45
46 static int
47 grub_pcidump_iter (grub_pci_device_t dev, grub_pci_id_t pciid,
48 void *data)
49 {
50 struct iter_cxt *ctx = data;
51 grub_pci_address_t addr;
52 int i;
53
54 if ((pciid & ctx->pciid_check_mask) != ctx->pciid_check_value)
55 return 0;
56
57 if (ctx->check_bus && grub_pci_get_bus (dev) != ctx->bus)
58 return 0;
59
60 if (ctx->check_device && grub_pci_get_device (dev) != ctx->device)
61 return 0;
62
63 if (ctx->check_function && grub_pci_get_function (dev) != ctx->function)
64 return 0;
65
66 for (i = 0; i < 256; i += 4)
67 {
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");
72 }
73
74 return 0;
75 }
76
77 static grub_err_t
78 grub_cmd_pcidump (grub_extcmd_context_t ctxt,
79 int argc __attribute__ ((unused)),
80 char **argv __attribute__ ((unused)))
81 {
82 const char *ptr;
83 struct iter_cxt ctx =
84 {
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
93 };
94
95 if (ctxt->state[0].set)
96 {
97 ptr = ctxt->state[0].arg;
98 ctx.pciid_check_value |= (grub_strtoul (ptr, &ptr, 16) & 0xffff);
99 if (grub_errno == GRUB_ERR_BAD_NUMBER)
100 {
101 grub_errno = GRUB_ERR_NONE;
102 ptr = ctxt->state[0].arg;
103 }
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, &ptr, 16) & 0xffff) << 16;
112 if (grub_errno == GRUB_ERR_BAD_NUMBER)
113 grub_errno = GRUB_ERR_NONE;
114 else
115 ctx.pciid_check_mask |= 0xffff0000;
116 }
117
118 ctx.pciid_check_value &= ctx.pciid_check_mask;
119
120 if (ctxt->state[1].set)
121 {
122 const char *optr;
123
124 ptr = ctxt->state[1].arg;
125 optr = ptr;
126 ctx.bus = grub_strtoul (ptr, &ptr, 16);
127 if (grub_errno == GRUB_ERR_BAD_NUMBER)
128 {
129 grub_errno = GRUB_ERR_NONE;
130 ptr = optr;
131 }
132 else
133 ctx.check_bus = 1;
134 if (grub_errno)
135 return grub_errno;
136 if (*ptr != ':')
137 return grub_error (GRUB_ERR_BAD_ARGUMENT, N_("missing `%c' symbol"), ':');
138 ptr++;
139 optr = ptr;
140 ctx.device = grub_strtoul (ptr, &ptr, 16);
141 if (grub_errno == GRUB_ERR_BAD_NUMBER)
142 {
143 grub_errno = GRUB_ERR_NONE;
144 ptr = optr;
145 }
146 else
147 ctx.check_device = 1;
148 if (*ptr == '.')
149 {
150 ptr++;
151 ctx.function = grub_strtoul (ptr, &ptr, 16);
152 if (grub_errno)
153 return grub_errno;
154 ctx.check_function = 1;
155 }
156 }
157
158 grub_pci_iterate (grub_pcidump_iter, &ctx);
159 return GRUB_ERR_NONE;
160 }
161
162 static grub_extcmd_t cmd;
163
164 GRUB_MOD_INIT(pcidump)
165 {
166 cmd = grub_register_extcmd ("pcidump", grub_cmd_pcidump, 0,
167 N_("[-s POSITION] [-d DEVICE]"),
168 N_("Show raw dump of the PCI configuration space."), options);
169 }
170
171 GRUB_MOD_FINI(pcidump)
172 {
173 grub_unregister_extcmd (cmd);
174 }