Indentation fix, cleanup.
[AROS.git] / arch / all-pc / boot / grub2-aros / grub-core / commands / setpci.c
blobd5bc97d60b29dc1b9186f3c1de361e0fa8eb971c
1 /* lspci.c - List PCI devices. */
2 /*
3 * GRUB -- GRand Unified Bootloader
4 * Copyright (C) 2008, 2009 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 pci_register
32 const char *name;
33 grub_uint16_t addr;
34 unsigned size;
37 static struct pci_register pci_registers[] =
39 {"VENDOR_ID", GRUB_PCI_REG_VENDOR , 2},
40 {"DEVICE_ID", GRUB_PCI_REG_DEVICE , 2},
41 {"COMMAND", GRUB_PCI_REG_COMMAND , 2},
42 {"STATUS", GRUB_PCI_REG_STATUS , 2},
43 {"REVISION", GRUB_PCI_REG_REVISION , 1},
44 {"CLASS_PROG", GRUB_PCI_REG_CLASS + 1 , 1},
45 {"CLASS_DEVICE", GRUB_PCI_REG_CLASS + 2 , 2},
46 {"CACHE_LINE_SIZE", GRUB_PCI_REG_CACHELINE , 1},
47 {"LATENCY_TIMER", GRUB_PCI_REG_LAT_TIMER , 1},
48 {"HEADER_TYPE", GRUB_PCI_REG_HEADER_TYPE , 1},
49 {"BIST", GRUB_PCI_REG_BIST , 1},
50 {"BASE_ADDRESS_0", GRUB_PCI_REG_ADDRESS_REG0, 4},
51 {"BASE_ADDRESS_1", GRUB_PCI_REG_ADDRESS_REG1, 4},
52 {"BASE_ADDRESS_2", GRUB_PCI_REG_ADDRESS_REG2, 4},
53 {"BASE_ADDRESS_3", GRUB_PCI_REG_ADDRESS_REG3, 4},
54 {"BASE_ADDRESS_4", GRUB_PCI_REG_ADDRESS_REG4, 4},
55 {"BASE_ADDRESS_5", GRUB_PCI_REG_ADDRESS_REG5, 4},
56 {"CARDBUS_CIS", GRUB_PCI_REG_CIS_POINTER , 4},
57 {"SUBVENDOR_ID", GRUB_PCI_REG_SUBVENDOR , 2},
58 {"SUBSYSTEM_ID", GRUB_PCI_REG_SUBSYSTEM , 2},
59 {"ROM_ADDRESS", GRUB_PCI_REG_ROM_ADDRESS , 4},
60 {"CAP_POINTER", GRUB_PCI_REG_CAP_POINTER , 1},
61 {"INTERRUPT_LINE", GRUB_PCI_REG_IRQ_LINE , 1},
62 {"INTERRUPT_PIN", GRUB_PCI_REG_IRQ_PIN , 1},
63 {"MIN_GNT", GRUB_PCI_REG_MIN_GNT , 1},
64 {"MAX_LAT", GRUB_PCI_REG_MIN_GNT , 1},
67 static const struct grub_arg_option options[] =
69 {0, 'd', 0, N_("Select device by vendor and device IDs."),
70 N_("[vendor]:[device]"), ARG_TYPE_STRING},
71 {0, 's', 0, N_("Select device by its position on the bus."),
72 N_("[bus]:[slot][.func]"), ARG_TYPE_STRING},
73 {0, 'v', 0, N_("Save read value into variable VARNAME."),
74 N_("VARNAME"), ARG_TYPE_STRING},
75 {0, 0, 0, 0, 0, 0}
78 static grub_uint32_t pciid_check_mask, pciid_check_value;
79 static int bus, device, function;
80 static int check_bus, check_device, check_function;
81 static grub_uint32_t write_mask, regwrite;
82 static int regsize;
83 static grub_uint16_t regaddr;
84 static const char *varname;
86 static int
87 grub_setpci_iter (grub_pci_device_t dev, grub_pci_id_t pciid,
88 void *data __attribute__ ((unused)))
90 grub_uint32_t regval = 0;
91 grub_pci_address_t addr;
93 if ((pciid & pciid_check_mask) != pciid_check_value)
94 return 0;
96 if (check_bus && grub_pci_get_bus (dev) != bus)
97 return 0;
99 if (check_device && grub_pci_get_device (dev) != device)
100 return 0;
102 if (check_function && grub_pci_get_function (dev) != function)
103 return 0;
105 addr = grub_pci_make_address (dev, regaddr);
107 switch (regsize)
109 case 1:
110 regval = grub_pci_read_byte (addr);
111 break;
113 case 2:
114 regval = grub_pci_read_word (addr);
115 break;
117 case 4:
118 regval = grub_pci_read (addr);
119 break;
122 if (varname)
124 char buf[sizeof ("XXXXXXXX")];
125 grub_snprintf (buf, sizeof (buf), "%x", regval);
126 grub_env_set (varname, buf);
127 return 1;
130 if (!write_mask)
132 grub_printf (_("Register %x of %x:%02x.%x is %x\n"), regaddr,
133 grub_pci_get_bus (dev),
134 grub_pci_get_device (dev),
135 grub_pci_get_function (dev),
136 regval);
137 return 0;
140 regval = (regval & ~write_mask) | regwrite;
142 switch (regsize)
144 case 1:
145 grub_pci_write_byte (addr, regval);
146 break;
148 case 2:
149 grub_pci_write_word (addr, regval);
150 break;
152 case 4:
153 grub_pci_write (addr, regval);
154 break;
157 return 0;
160 static grub_err_t
161 grub_cmd_setpci (grub_extcmd_context_t ctxt, int argc, char **argv)
163 const char *ptr;
164 unsigned i;
166 pciid_check_value = 0;
167 pciid_check_mask = 0;
169 if (ctxt->state[0].set)
171 ptr = ctxt->state[0].arg;
172 pciid_check_value |= (grub_strtoul (ptr, (char **) &ptr, 16) & 0xffff);
173 if (grub_errno == GRUB_ERR_BAD_NUMBER)
175 grub_errno = GRUB_ERR_NONE;
176 ptr = ctxt->state[0].arg;
178 else
179 pciid_check_mask |= 0xffff;
180 if (grub_errno)
181 return grub_errno;
182 if (*ptr != ':')
183 return grub_error (GRUB_ERR_BAD_ARGUMENT, N_("missing `%c' symbol"), ':');
184 ptr++;
185 pciid_check_value |= (grub_strtoul (ptr, (char **) &ptr, 16) & 0xffff)
186 << 16;
187 if (grub_errno == GRUB_ERR_BAD_NUMBER)
188 grub_errno = GRUB_ERR_NONE;
189 else
190 pciid_check_mask |= 0xffff0000;
193 pciid_check_value &= pciid_check_mask;
195 check_bus = check_device = check_function = 0;
197 if (ctxt->state[1].set)
199 const char *optr;
201 ptr = ctxt->state[1].arg;
202 optr = ptr;
203 bus = grub_strtoul (ptr, (char **) &ptr, 16);
204 if (grub_errno == GRUB_ERR_BAD_NUMBER)
206 grub_errno = GRUB_ERR_NONE;
207 ptr = optr;
209 else
210 check_bus = 1;
211 if (grub_errno)
212 return grub_errno;
213 if (*ptr != ':')
214 return grub_error (GRUB_ERR_BAD_ARGUMENT, N_("missing `%c' symbol"), ':');
215 ptr++;
216 optr = ptr;
217 device = grub_strtoul (ptr, (char **) &ptr, 16);
218 if (grub_errno == GRUB_ERR_BAD_NUMBER)
220 grub_errno = GRUB_ERR_NONE;
221 ptr = optr;
223 else
224 check_device = 1;
225 if (*ptr == '.')
227 ptr++;
228 function = grub_strtoul (ptr, (char **) &ptr, 16);
229 if (grub_errno)
230 return grub_errno;
231 check_function = 1;
235 if (ctxt->state[2].set)
236 varname = ctxt->state[2].arg;
237 else
238 varname = NULL;
240 write_mask = 0;
242 if (argc != 1)
243 return grub_error (GRUB_ERR_BAD_ARGUMENT, N_("one argument expected"));
245 ptr = argv[0];
247 for (i = 0; i < ARRAY_SIZE (pci_registers); i++)
249 if (grub_strncmp (ptr, pci_registers[i].name,
250 grub_strlen (pci_registers[i].name)) == 0)
251 break;
253 if (i == ARRAY_SIZE (pci_registers))
255 regsize = 0;
256 regaddr = grub_strtoul (ptr, (char **) &ptr, 16);
257 if (grub_errno)
258 return grub_error (GRUB_ERR_BAD_ARGUMENT, "unknown register");
260 else
262 regaddr = pci_registers[i].addr;
263 regsize = pci_registers[i].size;
264 ptr += grub_strlen (pci_registers[i].name);
267 if (grub_errno)
268 return grub_errno;
270 if (*ptr == '+')
272 ptr++;
273 regaddr += grub_strtoul (ptr, (char **) &ptr, 16);
274 if (grub_errno)
275 return grub_errno;
278 if (grub_memcmp (ptr, ".L", sizeof (".L") - 1) == 0
279 || grub_memcmp (ptr, ".l", sizeof (".l") - 1) == 0)
281 regsize = 4;
282 ptr += sizeof (".l") - 1;
284 else if (grub_memcmp (ptr, ".W", sizeof (".W") - 1) == 0
285 || grub_memcmp (ptr, ".w", sizeof (".w") - 1) == 0)
287 regsize = 2;
288 ptr += sizeof (".w") - 1;
290 else if (grub_memcmp (ptr, ".B", sizeof (".B") - 1) == 0
291 || grub_memcmp (ptr, ".b", sizeof (".b") - 1) == 0)
293 regsize = 1;
294 ptr += sizeof (".b") - 1;
297 if (!regsize)
298 return grub_error (GRUB_ERR_BAD_ARGUMENT,
299 "unknown register size");
301 write_mask = 0;
302 if (*ptr == '=')
304 ptr++;
305 regwrite = grub_strtoul (ptr, (char **) &ptr, 16);
306 if (grub_errno)
307 return grub_errno;
308 write_mask = 0xffffffff;
309 if (*ptr == ':')
311 ptr++;
312 write_mask = grub_strtoul (ptr, (char **) &ptr, 16);
313 if (grub_errno)
314 return grub_errno;
315 write_mask = 0xffffffff;
317 regwrite &= write_mask;
320 if (write_mask && varname)
321 return grub_error (GRUB_ERR_BAD_ARGUMENT,
322 "option -v isn't valid for writes");
324 grub_pci_iterate (grub_setpci_iter, NULL);
325 return GRUB_ERR_NONE;
328 static grub_extcmd_t cmd;
330 GRUB_MOD_INIT(setpci)
332 cmd = grub_register_extcmd ("setpci", grub_cmd_setpci, 0,
333 N_("[-s POSITION] [-d DEVICE] [-v VAR] "
334 "REGISTER[=VALUE[:MASK]]"),
335 N_("Manipulate PCI devices."), options);
338 GRUB_MOD_FINI(setpci)
340 grub_unregister_extcmd (cmd);