]> git.ipfire.org Git - thirdparty/pciutils.git/blob - setpci.c
setpci: Better tracing and reporting of runtime errors.
[thirdparty/pciutils.git] / setpci.c
1 /*
2 * The PCI Utilities -- Manipulate PCI Configuration Registers
3 *
4 * Copyright (c) 1998--2008 Martin Mares <mj@ucw.cz>
5 *
6 * Can be freely distributed and used under the terms of the GNU GPL.
7 */
8
9 #include <stdio.h>
10 #include <string.h>
11 #include <stdlib.h>
12 #include <stdarg.h>
13 #include <unistd.h>
14 #include <errno.h>
15
16 #define PCIUTILS_SETPCI
17 #include "pciutils.h"
18
19 static int force; /* Don't complain if no devices match */
20 static int verbose; /* Verbosity level */
21 static int demo_mode; /* Only show */
22
23 const char program_name[] = "setpci";
24
25 static struct pci_access *pacc;
26
27 struct value {
28 unsigned int value;
29 unsigned int mask;
30 };
31
32 struct op {
33 struct op *next;
34 struct pci_dev **dev_vector;
35 u16 cap_type; /* PCI_CAP_xxx or 0 */
36 u16 cap_id;
37 unsigned int addr;
38 unsigned int width; /* Byte width of the access */
39 unsigned int num_values; /* Number of values to write; 0=read */
40 struct value values[0];
41 };
42
43 static struct op *first_op, **last_op = &first_op;
44 static unsigned int max_values[] = { 0, 0xff, 0xffff, 0, 0xffffffff };
45
46 static struct pci_dev **
47 select_devices(struct pci_filter *filt)
48 {
49 struct pci_dev *z, **a, **b;
50 int cnt = 1;
51
52 for (z=pacc->devices; z; z=z->next)
53 if (pci_filter_match(filt, z))
54 cnt++;
55 a = b = xmalloc(sizeof(struct device *) * cnt);
56 for (z=pacc->devices; z; z=z->next)
57 if (pci_filter_match(filt, z))
58 *a++ = z;
59 *a = NULL;
60 return b;
61 }
62
63 static void
64 trace(const char *fmt, ...)
65 {
66 va_list args;
67 va_start(args, fmt);
68 if (verbose)
69 vprintf(fmt, args);
70 va_end(args);
71 }
72
73 static void
74 exec_op(struct op *op, struct pci_dev *dev)
75 {
76 const char * const formats[] = { NULL, " %02x", " %04x", NULL, " %08x" };
77 const char * const mask_formats[] = { NULL, " %02x->(%02x:%02x)->%02x", " %04x->(%04x:%04x)->%04x", NULL, " %08x->(%08x:%08x)->%08x" };
78 unsigned int i, x, y;
79 int addr = 0;
80 int width = op->width;
81 char slot[16];
82
83 sprintf(slot, "%04x:%02x:%02x.%x", dev->domain, dev->bus, dev->dev, dev->func);
84 trace("%s", slot);
85 if (op->cap_type)
86 {
87 struct pci_cap *cap;
88 trace(((op->cap_type == PCI_CAP_NORMAL) ? "(cap %02x)" : "(ecap %04x)"), op->cap_id);
89 cap = pci_find_cap(dev, op->cap_id, op->cap_type);
90 if (cap)
91 addr = cap->addr;
92 else
93 die("%s: %s %04x not found", slot, ((op->cap_type == PCI_CAP_NORMAL) ? "Capability" : "Extended capability"), op->cap_id);
94 }
95 addr += op->addr;
96 trace(":%02x", addr);
97
98 /* We have already checked it when parsing, but addressing relative to capabilities can change the address. */
99 if (addr & (width-1))
100 die("%s: Unaligned access of width %d to register %04x", slot, width, addr);
101 if (addr + width > 0x1000)
102 die("%s: Access of width %d to register %04x out of range", slot, width, addr);
103
104 if (op->num_values)
105 {
106 for (i=0; i<op->num_values; i++)
107 {
108 if ((op->values[i].mask & max_values[width]) == max_values[width])
109 {
110 x = op->values[i].value;
111 trace(formats[width], op->values[i].value);
112 }
113 else
114 {
115 switch (width)
116 {
117 case 1:
118 y = pci_read_byte(dev, addr);
119 break;
120 case 2:
121 y = pci_read_word(dev, addr);
122 break;
123 default:
124 y = pci_read_long(dev, addr);
125 break;
126 }
127 x = (y & ~op->values[i].mask) | op->values[i].value;
128 trace(mask_formats[width], y, op->values[i].value, op->values[i].mask, x);
129 }
130 if (!demo_mode)
131 {
132 switch (width)
133 {
134 case 1:
135 pci_write_byte(dev, addr, x);
136 break;
137 case 2:
138 pci_write_word(dev, addr, x);
139 break;
140 default:
141 pci_write_long(dev, addr, x);
142 break;
143 }
144 }
145 addr += width;
146 }
147 trace("\n");
148 }
149 else
150 {
151 trace(" = ");
152 switch (width)
153 {
154 case 1:
155 x = pci_read_byte(dev, addr);
156 break;
157 case 2:
158 x = pci_read_word(dev, addr);
159 break;
160 default:
161 x = pci_read_long(dev, addr);
162 break;
163 }
164 printf(formats[width]+1, x);
165 putchar('\n');
166 }
167 }
168
169 static void
170 execute(struct op *op)
171 {
172 struct pci_dev **vec = NULL;
173 struct pci_dev **pdev, *dev;
174 struct op *oops;
175
176 while (op)
177 {
178 pdev = vec = op->dev_vector;
179 while (dev = *pdev++)
180 for (oops=op; oops && oops->dev_vector == vec; oops=oops->next)
181 exec_op(oops, dev);
182 while (op && op->dev_vector == vec)
183 op = op->next;
184 }
185 }
186
187 static void
188 scan_ops(struct op *op)
189 {
190 while (op)
191 {
192 if (op->num_values)
193 pacc->writeable = 1;
194 op = op->next;
195 }
196 }
197
198 struct reg_name {
199 unsigned int cap;
200 unsigned int offset;
201 unsigned int width;
202 const char *name;
203 };
204
205 static const struct reg_name pci_reg_names[] = {
206 { 0, 0x00, 2, "VENDOR_ID" },
207 { 0, 0x02, 2, "DEVICE_ID" },
208 { 0, 0x04, 2, "COMMAND" },
209 { 0, 0x06, 2, "STATUS" },
210 { 0, 0x08, 1, "REVISION" },
211 { 0, 0x09, 1, "CLASS_PROG" },
212 { 0, 0x0a, 2, "CLASS_DEVICE" },
213 { 0, 0x0c, 1, "CACHE_LINE_SIZE" },
214 { 0, 0x0d, 1, "LATENCY_TIMER" },
215 { 0, 0x0e, 1, "HEADER_TYPE" },
216 { 0, 0x0f, 1, "BIST" },
217 { 0, 0x10, 4, "BASE_ADDRESS_0" },
218 { 0, 0x14, 4, "BASE_ADDRESS_1" },
219 { 0, 0x18, 4, "BASE_ADDRESS_2" },
220 { 0, 0x1c, 4, "BASE_ADDRESS_3" },
221 { 0, 0x20, 4, "BASE_ADDRESS_4" },
222 { 0, 0x24, 4, "BASE_ADDRESS_5" },
223 { 0, 0x28, 4, "CARDBUS_CIS" },
224 { 0, 0x2c, 4, "SUBSYSTEM_VENDOR_ID" },
225 { 0, 0x2e, 2, "SUBSYSTEM_ID" },
226 { 0, 0x30, 4, "ROM_ADDRESS" },
227 { 0, 0x3c, 1, "INTERRUPT_LINE" },
228 { 0, 0x3d, 1, "INTERRUPT_PIN" },
229 { 0, 0x3e, 1, "MIN_GNT" },
230 { 0, 0x3f, 1, "MAX_LAT" },
231 { 0, 0x18, 1, "PRIMARY_BUS" },
232 { 0, 0x19, 1, "SECONDARY_BUS" },
233 { 0, 0x1a, 1, "SUBORDINATE_BUS" },
234 { 0, 0x1b, 1, "SEC_LATENCY_TIMER" },
235 { 0, 0x1c, 1, "IO_BASE" },
236 { 0, 0x1d, 1, "IO_LIMIT" },
237 { 0, 0x1e, 2, "SEC_STATUS" },
238 { 0, 0x20, 2, "MEMORY_BASE" },
239 { 0, 0x22, 2, "MEMORY_LIMIT" },
240 { 0, 0x24, 2, "PREF_MEMORY_BASE" },
241 { 0, 0x26, 2, "PREF_MEMORY_LIMIT" },
242 { 0, 0x28, 4, "PREF_BASE_UPPER32" },
243 { 0, 0x2c, 4, "PREF_LIMIT_UPPER32" },
244 { 0, 0x30, 2, "IO_BASE_UPPER16" },
245 { 0, 0x32, 2, "IO_LIMIT_UPPER16" },
246 { 0, 0x38, 4, "BRIDGE_ROM_ADDRESS" },
247 { 0, 0x3e, 2, "BRIDGE_CONTROL" },
248 { 0, 0x10, 4, "CB_CARDBUS_BASE" },
249 { 0, 0x14, 2, "CB_CAPABILITIES" },
250 { 0, 0x16, 2, "CB_SEC_STATUS" },
251 { 0, 0x18, 1, "CB_BUS_NUMBER" },
252 { 0, 0x19, 1, "CB_CARDBUS_NUMBER" },
253 { 0, 0x1a, 1, "CB_SUBORDINATE_BUS" },
254 { 0, 0x1b, 1, "CB_CARDBUS_LATENCY" },
255 { 0, 0x1c, 4, "CB_MEMORY_BASE_0" },
256 { 0, 0x20, 4, "CB_MEMORY_LIMIT_0" },
257 { 0, 0x24, 4, "CB_MEMORY_BASE_1" },
258 { 0, 0x28, 4, "CB_MEMORY_LIMIT_1" },
259 { 0, 0x2c, 2, "CB_IO_BASE_0" },
260 { 0, 0x2e, 2, "CB_IO_BASE_0_HI" },
261 { 0, 0x30, 2, "CB_IO_LIMIT_0" },
262 { 0, 0x32, 2, "CB_IO_LIMIT_0_HI" },
263 { 0, 0x34, 2, "CB_IO_BASE_1" },
264 { 0, 0x36, 2, "CB_IO_BASE_1_HI" },
265 { 0, 0x38, 2, "CB_IO_LIMIT_1" },
266 { 0, 0x3a, 2, "CB_IO_LIMIT_1_HI" },
267 { 0, 0x40, 2, "CB_SUBSYSTEM_VENDOR_ID" },
268 { 0, 0x42, 2, "CB_SUBSYSTEM_ID" },
269 { 0, 0x44, 4, "CB_LEGACY_MODE_BASE" },
270 { 0x10001, 0, 0, "CAP_PM" },
271 { 0x10002, 0, 0, "CAP_AGP" },
272 { 0x10003, 0, 0, "CAP_VPD" },
273 { 0x10004, 0, 0, "CAP_SLOTID" },
274 { 0x10005, 0, 0, "CAP_MSI" },
275 { 0x10006, 0, 0, "CAP_CHSWP" },
276 { 0x10007, 0, 0, "CAP_PCIX" },
277 { 0x10008, 0, 0, "CAP_HT" },
278 { 0x10009, 0, 0, "CAP_VNDR" },
279 { 0x1000a, 0, 0, "CAP_DBG" },
280 { 0x1000b, 0, 0, "CAP_CCRC" },
281 { 0x1000c, 0, 0, "CAP_HOTPLUG" },
282 { 0x1000d, 0, 0, "CAP_SSVID" },
283 { 0x1000e, 0, 0, "CAP_AGP3" },
284 { 0x1000f, 0, 0, "CAP_SECURE" },
285 { 0x10010, 0, 0, "CAP_EXP" },
286 { 0x10011, 0, 0, "CAP_MSIX" },
287 { 0x10012, 0, 0, "CAP_SATA" },
288 { 0x10013, 0, 0, "CAP_AF" },
289 { 0x20001, 0, 0, "ECAP_AER" },
290 { 0x20002, 0, 0, "ECAP_VC" },
291 { 0x20003, 0, 0, "ECAP_DSN" },
292 { 0x20004, 0, 0, "ECAP_PB" },
293 { 0x20005, 0, 0, "ECAP_RCLINK" },
294 { 0x20006, 0, 0, "ECAP_RCILINK" },
295 { 0x20007, 0, 0, "ECAP_RCECOLL" },
296 { 0x20008, 0, 0, "ECAP_MFVC" },
297 { 0x2000a, 0, 0, "ECAP_RBCB" },
298 { 0x2000b, 0, 0, "ECAP_VNDR" },
299 { 0x2000d, 0, 0, "ECAP_ACS" },
300 { 0x2000e, 0, 0, "ECAP_ARI" },
301 { 0x2000f, 0, 0, "ECAP_ATS" },
302 { 0x20010, 0, 0, "ECAP_SRIOV" },
303 { 0, 0, 0, NULL }
304 };
305
306 static void
307 dump_registers(void)
308 {
309 const struct reg_name *r;
310
311 printf("cap pos w name\n");
312 for (r = pci_reg_names; r->name; r++)
313 {
314 if (r->cap >= 0x20000)
315 printf("%04x", r->cap - 0x20000);
316 else if (r->cap)
317 printf(" %02x", r->cap - 0x10000);
318 else
319 printf(" ");
320 printf(" %02x %c %s\n", r->offset, "-BW?L"[r->width], r->name);
321 }
322 }
323
324 static void NONRET PCI_PRINTF(1,2)
325 usage(char *msg, ...)
326 {
327 va_list args;
328 va_start(args, msg);
329 if (msg)
330 {
331 fprintf(stderr, "setpci: ");
332 vfprintf(stderr, msg, args);
333 fprintf(stderr, "\n\n");
334 }
335 fprintf(stderr,
336 "Usage: setpci [<options>] (<device>+ <reg>[=<values>]*)*\n"
337 "\n"
338 "General options:\n"
339 "-f\t\tDon't complain if there's nothing to do\n"
340 "-v\t\tBe verbose\n"
341 "-D\t\tList changes, don't commit them\n"
342 "--dumpregs\tDump all known register names and exit\n"
343 "\n"
344 "PCI access options:\n"
345 GENERIC_HELP
346 "\n"
347 "Setting commands:\n"
348 "<device>:\t-s [[[<domain>]:][<bus>]:][<slot>][.[<func>]]\n"
349 "\t\t-d [<vendor>]:[<device>]\n"
350 "<reg>:\t\t<base>[+<offset>][.(B|W|L)]\n"
351 "<base>:\t\t<address>\n"
352 "\t\t<named-register>\n"
353 "\t\t[E]CAP_<capability-name>\n"
354 "\t\t[E]CAP<capability-number>\n"
355 "<values>:\t<value>[,<value>...]\n"
356 "<value>:\t<hex>\n"
357 "\t\t<hex>:<mask>\n");
358 exit(1);
359 }
360
361 static int
362 parse_options(int argc, char **argv)
363 {
364 char *opts = GENERIC_OPTIONS;
365 int i=1;
366
367 if (argc == 2 && !strcmp(argv[1], "--version"))
368 {
369 puts("setpci version " PCIUTILS_VERSION);
370 exit(0);
371 }
372 if (argc == 2 && !strcmp(argv[1], "--dumpregs"))
373 {
374 dump_registers();
375 exit(0);
376 }
377
378 while (i < argc && argv[i][0] == '-')
379 {
380 char *c = argv[i++] + 1;
381 char *d = c;
382 char *e;
383 while (*c)
384 switch (*c)
385 {
386 case 0:
387 break;
388 case 'v':
389 verbose++;
390 c++;
391 break;
392 case 'f':
393 force++;
394 c++;
395 break;
396 case 'D':
397 demo_mode++;
398 c++;
399 break;
400 default:
401 if (e = strchr(opts, *c))
402 {
403 char *arg;
404 c++;
405 if (e[1] == ':')
406 {
407 if (*c)
408 arg = c;
409 else if (i < argc)
410 arg = argv[i++];
411 else
412 usage(NULL);
413 c = "";
414 }
415 else
416 arg = NULL;
417 if (!parse_generic_option(*e, pacc, arg))
418 usage(NULL);
419 }
420 else
421 {
422 if (c != d)
423 usage(NULL);
424 return i-1;
425 }
426 }
427 }
428
429 return i;
430 }
431
432 static int parse_filter(int argc, char **argv, int i, struct pci_filter *filter)
433 {
434 char *c = argv[i++];
435 char *d;
436
437 if (!c[1] || !strchr("sd", c[1]))
438 usage(NULL);
439 if (c[2])
440 d = (c[2] == '=') ? c+3 : c+2;
441 else if (i < argc)
442 d = argv[i++];
443 else
444 usage(NULL);
445 switch (c[1])
446 {
447 case 's':
448 if (d = pci_filter_parse_slot(filter, d))
449 die("-s: %s", d);
450 break;
451 case 'd':
452 if (d = pci_filter_parse_id(filter, d))
453 die("-d: %s", d);
454 break;
455 default:
456 usage(NULL);
457 }
458
459 return i;
460 }
461
462 static const struct reg_name *parse_reg_name(char *name)
463 {
464 const struct reg_name *r;
465
466 for (r = pci_reg_names; r->name; r++)
467 if (!strcasecmp(r->name, name))
468 return r;
469 return NULL;
470 }
471
472 static int parse_x32(char *c, char **stopp, unsigned int *resp)
473 {
474 char *stop;
475
476 if (!*c)
477 return -1;
478 errno = 0;
479 unsigned long int l = strtoul(c, &stop, 16);
480 if (errno)
481 return -1;
482 if ((l & ~0U) != l)
483 return -1;
484 *resp = l;
485 if (*stop)
486 {
487 if (stopp)
488 *stopp = stop;
489 return 0;
490 }
491 else
492 return 1;
493 }
494
495 static void parse_register(struct op *op, char *base)
496 {
497 const struct reg_name *r;
498 unsigned int cap;
499
500 op->cap_type = op->cap_id = 0;
501 if (parse_x32(base, NULL, &op->addr) > 0)
502 return;
503 else if (r = parse_reg_name(base))
504 {
505 switch (r->cap & 0xff0000)
506 {
507 case 0x10000:
508 op->cap_type = PCI_CAP_NORMAL;
509 break;
510 case 0x20000:
511 op->cap_type = PCI_CAP_EXTENDED;
512 break;
513 }
514 op->cap_id = r->cap & 0xffff;
515 op->addr = r->offset;
516 if (r->width && !op->width)
517 op->width = r->width;
518 return;
519 }
520 else if (!strncasecmp(base, "CAP", 3))
521 {
522 if (parse_x32(base+3, NULL, &cap) > 0 && cap < 0x100)
523 {
524 op->cap_type = PCI_CAP_NORMAL;
525 op->cap_id = cap;
526 op->addr = 0;
527 return;
528 }
529 }
530 else if (!strncasecmp(base, "ECAP", 4))
531 {
532 if (parse_x32(base+4, NULL, &cap) > 0 && cap < 0x1000)
533 {
534 op->cap_type = PCI_CAP_EXTENDED;
535 op->cap_id = cap;
536 op->addr = 0;
537 return;
538 }
539 }
540 usage("Unknown register \"%s\"", base);
541 }
542
543 static void parse_op(char *c, struct pci_dev **selected_devices)
544 {
545 char *base, *offset, *width, *value;
546 char *e, *f;
547 int n, j;
548 struct op *op;
549
550 /* Split the argument */
551 base = xstrdup(c);
552 if (value = strchr(base, '='))
553 *value++ = 0;
554 if (width = strchr(base, '.'))
555 *width++ = 0;
556 if (offset = strchr(base, '+'))
557 *offset++ = 0;
558
559 /* Look for setting of values and count how many */
560 n = 0;
561 if (value)
562 {
563 if (!*value)
564 usage("Missing value");
565 n++;
566 for (e=value; *e; e++)
567 if (*e == ',')
568 n++;
569 }
570
571 /* Allocate the operation */
572 op = xmalloc(sizeof(struct op) + n*sizeof(struct value));
573 op->dev_vector = selected_devices;
574 op->num_values = n;
575
576 /* What is the width suffix? */
577 if (width)
578 {
579 if (width[1])
580 usage("Invalid width \"%s\"", width);
581 switch (*width & 0xdf)
582 {
583 case 'B':
584 op->width = 1; break;
585 case 'W':
586 op->width = 2; break;
587 case 'L':
588 op->width = 4; break;
589 default:
590 usage("Invalid width \"%c\"", *width);
591 }
592 }
593 else
594 op->width = 0;
595
596 /* Find the register */
597 parse_register(op, base);
598 if (!op->width)
599 usage("Missing width");
600
601 /* Add offset */
602 if (offset)
603 {
604 unsigned int off;
605 if (parse_x32(offset, NULL, &off) <= 0 || off >= 0x1000)
606 die("Invalid offset \"%s\"", offset);
607 op->addr += off;
608 }
609
610 /* Check range */
611 if (op->addr >= 0x1000 || op->addr + op->width*(n ? n : 1) > 0x1000)
612 die("Register number out of range!");
613 if (op->addr & (op->width - 1))
614 die("Unaligned register address!");
615
616 /* Parse the values */
617 for (j=0; j<n; j++)
618 {
619 unsigned int ll, lim;
620 e = strchr(value, ',');
621 if (e)
622 *e++ = 0;
623 if (parse_x32(value, &f, &ll) < 0 || *f && *f != ':')
624 usage("Invalid value \"%s\"", value);
625 lim = max_values[op->width];
626 if (ll > lim && ll < ~0UL - lim)
627 usage("Value \"%s\" is out of range", value);
628 op->values[j].value = ll;
629 if (*f == ':')
630 {
631 if (parse_x32(f+1, NULL, &ll) <= 0)
632 usage("Invalid mask \"%s\"", f+1);
633 if (ll > lim && ll < ~0UL - lim)
634 usage("Mask \"%s\" is out of range", f+1);
635 op->values[j].mask = ll;
636 op->values[j].value &= ll;
637 }
638 else
639 op->values[j].mask = ~0U;
640 value = e;
641 }
642
643 *last_op = op;
644 last_op = &op->next;
645 op->next = NULL;
646 }
647
648 static void parse_ops(int argc, char **argv, int i)
649 {
650 enum { STATE_INIT, STATE_GOT_FILTER, STATE_GOT_OP } state = STATE_INIT;
651 struct pci_filter filter;
652 struct pci_dev **selected_devices = NULL;
653
654 while (i < argc)
655 {
656 char *c = argv[i++];
657
658 if (*c == '-')
659 {
660 if (state != STATE_GOT_FILTER)
661 pci_filter_init(pacc, &filter);
662 i = parse_filter(argc, argv, i-1, &filter);
663 state = STATE_GOT_FILTER;
664 }
665 else
666 {
667 if (state == STATE_INIT)
668 usage(NULL);
669 if (state == STATE_GOT_FILTER)
670 selected_devices = select_devices(&filter);
671 if (!selected_devices[0] && !force)
672 fprintf(stderr, "setpci: Warning: No devices selected for `%s'.\n", c);
673 parse_op(c, selected_devices);
674 state = STATE_GOT_OP;
675 }
676 }
677 if (state == STATE_INIT)
678 usage("No operation specified");
679 }
680
681 int
682 main(int argc, char **argv)
683 {
684 int i;
685
686 pacc = pci_alloc();
687 pacc->error = die;
688 i = parse_options(argc, argv);
689
690 pci_init(pacc);
691 pci_scan_bus(pacc);
692
693 parse_ops(argc, argv, i);
694 scan_ops(first_op);
695 execute(first_op);
696
697 return 0;
698 }