]> git.ipfire.org Git - thirdparty/pciutils.git/blob - setpci.c
2cbcfefff95cae3ff4757d566623ff18a45ba986
[thirdparty/pciutils.git] / setpci.c
1 /*
2 * $Id: setpci.c,v 1.8 1998/10/24 13:39:20 mj Exp $
3 *
4 * Linux PCI Utilities -- Manipulate PCI Configuration Registers
5 *
6 * Copyright (c) 1998 Martin Mares <mj@atrey.karlin.mff.cuni.cz>
7 *
8 * Can be freely distributed and used under the terms of the GNU GPL.
9 */
10
11 #define _GNU_SOURCE
12
13 #include <stdio.h>
14 #include <string.h>
15 #include <stdlib.h>
16 #include <fcntl.h>
17 #include <unistd.h>
18 #include <errno.h>
19 #include <asm/byteorder.h>
20
21 #include <asm/unistd.h>
22 #if defined(__GLIBC__) && __GLIBC__ == 2 && __GLIBC_MINOR__ < 1
23 #include <syscall-list.h>
24 #endif
25
26 #include "pciutils.h"
27
28 static int force; /* Don't complain if no devices match */
29 static int verbose; /* Verbosity level */
30 static int demo_mode; /* Only show */
31
32 struct device {
33 struct device *next;
34 byte bus, devfn, mark;
35 word vendid, devid;
36 int fd, need_write;
37 };
38
39 static struct device *first_dev;
40
41 struct op {
42 struct op *next;
43 struct device **dev_vector;
44 unsigned int addr;
45 unsigned int width; /* Byte width of the access */
46 int num_values; /* Number of values to write; <0=read */
47 unsigned int values[0];
48 };
49
50 static struct op *first_op, **last_op = &first_op;
51
52 void *
53 xmalloc(unsigned int howmuch)
54 {
55 void *p = malloc(howmuch);
56 if (!p)
57 {
58 fprintf(stderr, "setpci: Unable to allocate %d bytes of memory\n", howmuch);
59 exit(1);
60 }
61 return p;
62 }
63
64 /*
65 * As libc doesn't support pread/pwrite yet, we have to call them directly
66 * or use lseek/read/write instead.
67 */
68 #if !(defined(__GLIBC__) && __GLIBC__ == 2 && __GLIBC_MINOR__ > 0)
69
70 #if defined(__GLIBC__) && !(defined(__powerpc__) && __GLIBC__ == 2 && __GLIBC_MINOR__ == 0)
71 #ifndef SYS_pread
72 #define SYS_pread __NR_pread
73 #endif
74 static int
75 pread(unsigned int fd, void *buf, size_t size, loff_t where)
76 {
77 return syscall(SYS_pread, fd, buf, size, where);
78 }
79
80 #ifndef SYS_pwrite
81 #define SYS_pwrite __NR_pwrite
82 #endif
83 static int
84 pwrite(unsigned int fd, void *buf, size_t size, loff_t where)
85 {
86 return syscall(SYS_pwrite, fd, buf, size, where);
87 }
88 #else
89 static _syscall4(int, pread, unsigned int, fd, void *, buf, size_t, size, loff_t, where);
90 static _syscall4(int, pwrite, unsigned int, fd, void *, buf, size_t, size, loff_t, where);
91 #endif
92
93 #endif
94
95 static void
96 scan_devices(void)
97 {
98 struct device **last = &first_dev;
99 byte line[256];
100 FILE *f;
101
102 if (!(f = fopen(PROC_BUS_PCI "/devices", "r")))
103 {
104 perror(PROC_BUS_PCI "/devices");
105 exit(1);
106 }
107 while (fgets(line, sizeof(line), f))
108 {
109 struct device *d = xmalloc(sizeof(struct device));
110 unsigned int dfn, vend;
111
112 sscanf(line, "%x %x", &dfn, &vend);
113 d->bus = dfn >> 8U;
114 d->devfn = dfn & 0xff;
115 d->vendid = vend >> 16U;
116 d->devid = vend & 0xffff;
117 d->fd = -1;
118 *last = d;
119 last = &d->next;
120 }
121 fclose(f);
122 *last = NULL;
123 }
124
125 static struct device **
126 select_devices(struct pci_filter *filt)
127 {
128 struct device *z, **a, **b;
129 int cnt = 1;
130
131 for(z=first_dev; z; z=z->next)
132 if (z->mark = filter_match(filt, z->bus, z->devfn, z->vendid, z->devid))
133 cnt++;
134 a = b = xmalloc(sizeof(struct device *) * cnt);
135 for(z=first_dev; z; z=z->next)
136 if (z->mark)
137 *a++ = z;
138 *a = NULL;
139 return b;
140 }
141
142 static void
143 exec_op(struct op *op, struct device *dev)
144 {
145 char *mm[] = { NULL, "%02x", "%04x", NULL, "%08x" };
146 char *m = mm[op->width];
147 unsigned int x;
148 int i;
149 __u32 x32;
150 __u16 x16;
151 __u8 x8;
152
153 if (!demo_mode && dev->fd < 0)
154 {
155 char name[64];
156 sprintf(name, PROC_BUS_PCI "/%02x/%02x.%x", dev->bus, PCI_SLOT(dev->devfn), PCI_FUNC(dev->devfn));
157 if ((dev->fd = open(name, dev->need_write ? O_RDWR : O_RDONLY)) < 0)
158 {
159 perror(name);
160 exit(1);
161 }
162 }
163
164 if (verbose)
165 printf("%02x:%02x.%x:%02x", dev->bus, PCI_SLOT(dev->devfn), PCI_FUNC(dev->devfn), op->addr);
166 if (op->num_values >= 0)
167 for(i=0; i<op->num_values; i++)
168 {
169 if (verbose)
170 {
171 putchar(' ');
172 printf(m, op->values[i]);
173 }
174 if (demo_mode)
175 continue;
176 switch (op->width)
177 {
178 case 1:
179 x8 = op->values[i];
180 i = pwrite(dev->fd, &x8, 1, op->addr);
181 break;
182 case 2:
183 x16 = __cpu_to_le16(op->values[i]);
184 i = pwrite(dev->fd, &x16, 2, op->addr);
185 break;
186 default:
187 x32 = __cpu_to_le32(op->values[i]);
188 i = pwrite(dev->fd, &x32, 4, op->addr);
189 break;
190 }
191 if (i != (int) op->width)
192 {
193 fprintf(stderr, "Error writing to %02x:%02x.%d: %m\n", dev->bus, PCI_SLOT(dev->devfn), PCI_FUNC(dev->devfn));
194 exit(1);
195 }
196 }
197 else
198 {
199 if (verbose)
200 printf(" = ");
201 if (!demo_mode)
202 {
203 switch (op->width)
204 {
205 case 1:
206 i = pread(dev->fd, &x8, 1, op->addr);
207 x = x8;
208 break;
209 case 2:
210 i = pread(dev->fd, &x16, 2, op->addr);
211 x = __le16_to_cpu(x16);
212 break;
213 default:
214 i = pread(dev->fd, &x32, 4, op->addr);
215 x = __le32_to_cpu(x32);
216 break;
217 }
218 if (i != (int) op->width)
219 {
220 fprintf(stderr, "Error reading from %02x:%02x.%d: %m\n", dev->bus, PCI_SLOT(dev->devfn), PCI_FUNC(dev->devfn));
221 exit(1);
222 }
223 printf(m, x);
224 }
225 else
226 putchar('?');
227 }
228 putchar('\n');
229 }
230
231 static void
232 execute(struct op *op)
233 {
234 struct device **vec = NULL;
235 struct device **pdev, *dev;
236 struct op *oops;
237
238 while (op)
239 {
240 pdev = vec = op->dev_vector;
241 while (dev = *pdev++)
242 for(oops=op; oops && oops->dev_vector == vec; oops=oops->next)
243 exec_op(oops, dev);
244 while (op && op->dev_vector == vec)
245 op = op->next;
246 }
247 }
248
249 static void
250 scan_ops(struct op *op)
251 {
252 struct device **pdev, *dev;
253
254 while (op)
255 {
256 if (op->num_values >= 0)
257 {
258 pdev = op->dev_vector;
259 while (dev = *pdev++)
260 dev->need_write = 1;
261 }
262 op = op->next;
263 }
264 }
265
266 struct reg_name {
267 int offset;
268 int width;
269 char *name;
270 };
271
272 static struct reg_name pci_reg_names[] = {
273 { 0x00, 2, "VENDOR_ID", },
274 { 0x02, 2, "DEVICE_ID", },
275 { 0x04, 2, "COMMAND", },
276 { 0x06, 2, "STATUS", },
277 { 0x08, 1, "REVISION", },
278 { 0x09, 1, "CLASS_PROG", },
279 { 0x0a, 2, "CLASS_DEVICE", },
280 { 0x0c, 1, "CACHE_LINE_SIZE", },
281 { 0x0d, 1, "LATENCY_TIMER", },
282 { 0x0e, 1, "HEADER_TYPE", },
283 { 0x0f, 1, "BIST", },
284 { 0x10, 4, "BASE_ADDRESS_0", },
285 { 0x14, 4, "BASE_ADDRESS_1", },
286 { 0x18, 4, "BASE_ADDRESS_2", },
287 { 0x1c, 4, "BASE_ADDRESS_3", },
288 { 0x20, 4, "BASE_ADDRESS_4", },
289 { 0x24, 4, "BASE_ADDRESS_5", },
290 { 0x28, 4, "CARDBUS_CIS", },
291 { 0x2c, 4, "SUBSYSTEM_VENDOR_ID", },
292 { 0x2e, 2, "SUBSYSTEM_ID", },
293 { 0x30, 4, "ROM_ADDRESS", },
294 { 0x3c, 1, "INTERRUPT_LINE", },
295 { 0x3d, 1, "INTERRUPT_PIN", },
296 { 0x3e, 1, "MIN_GNT", },
297 { 0x3f, 1, "MAX_LAT", },
298 { 0x18, 1, "PRIMARY_BUS", },
299 { 0x19, 1, "SECONDARY_BUS", },
300 { 0x1a, 1, "SUBORDINATE_BUS", },
301 { 0x1b, 1, "SEC_LATENCY_TIMER", },
302 { 0x1c, 1, "IO_BASE", },
303 { 0x1d, 1, "IO_LIMIT", },
304 { 0x1e, 2, "SEC_STATUS", },
305 { 0x20, 2, "MEMORY_BASE", },
306 { 0x22, 2, "MEMORY_LIMIT", },
307 { 0x24, 2, "PREF_MEMORY_BASE", },
308 { 0x26, 2, "PREF_MEMORY_LIMIT", },
309 { 0x28, 4, "PREF_BASE_UPPER32", },
310 { 0x2c, 4, "PREF_LIMIT_UPPER32", },
311 { 0x30, 2, "IO_BASE_UPPER16", },
312 { 0x32, 2, "IO_LIMIT_UPPER16", },
313 { 0x38, 4, "BRIDGE_ROM_ADDRESS", },
314 { 0x3e, 2, "BRIDGE_CONTROL", },
315 { 0x10, 4, "CB_CARDBUS_BASE", },
316 { 0x14, 2, "CB_CAPABILITIES", },
317 { 0x16, 2, "CB_SEC_STATUS", },
318 { 0x18, 1, "CB_BUS_NUMBER", },
319 { 0x19, 1, "CB_CARDBUS_NUMBER", },
320 { 0x1a, 1, "CB_SUBORDINATE_BUS", },
321 { 0x1b, 1, "CB_CARDBUS_LATENCY", },
322 { 0x1c, 4, "CB_MEMORY_BASE_0", },
323 { 0x20, 4, "CB_MEMORY_LIMIT_0", },
324 { 0x24, 4, "CB_MEMORY_BASE_1", },
325 { 0x28, 4, "CB_MEMORY_LIMIT_1", },
326 { 0x2c, 2, "CB_IO_BASE_0", },
327 { 0x2e, 2, "CB_IO_BASE_0_HI", },
328 { 0x30, 2, "CB_IO_LIMIT_0", },
329 { 0x32, 2, "CB_IO_LIMIT_0_HI", },
330 { 0x34, 2, "CB_IO_BASE_1", },
331 { 0x36, 2, "CB_IO_BASE_1_HI", },
332 { 0x38, 2, "CB_IO_LIMIT_1", },
333 { 0x3a, 2, "CB_IO_LIMIT_1_HI", },
334 { 0x40, 2, "CB_SUBSYSTEM_VENDOR_ID", },
335 { 0x42, 2, "CB_SUBSYSTEM_ID", },
336 { 0x44, 4, "CB_LEGACY_MODE_BASE", },
337 { 0x00, 0, NULL }
338 };
339
340 static void usage(void) __attribute__((noreturn));
341
342 static void
343 usage(void)
344 {
345 fprintf(stderr,
346 "Usage: setpci [-fvD] (<device>+ <reg>[=<values>]*)*\n\
347 -f\t\tDon't complain if there's nothing to do\n\
348 -v\t\tBe verbose\n\
349 -D\t\tList changes, don't commit them\n\
350 <device>:\t-s [[<bus>]:][<slot>][.[<func>]]\n\
351 \t|\t-d [<vendor>]:[<device>]\n\
352 <reg>:\t\t<number>[.(B|W|L)]\n\
353 |\t\t<name>\n\
354 <values>:\t<value>[,<value>...]\n\
355 ");
356 exit(1);
357 }
358
359 int
360 main(int argc, char **argv)
361 {
362 enum { STATE_INIT, STATE_GOT_FILTER, STATE_GOT_OP } state = STATE_INIT;
363 struct pci_filter filter;
364 struct device **selected_devices = NULL;
365
366 if (argc == 2 && !strcmp(argv[1], "--version"))
367 {
368 puts("setpci version " PCIUTILS_VERSION);
369 return 0;
370 }
371 argc--;
372 argv++;
373 while (argc && argv[0][0] == '-')
374 {
375 char *c = argv[0]+1;
376 char *d = c;
377 while (*c)
378 switch (*c)
379 {
380 case 'v':
381 verbose++;
382 c++;
383 break;
384 case 'f':
385 force++;
386 c++;
387 break;
388 case 'D':
389 demo_mode++;
390 c++;
391 break;
392 case 0:
393 break;
394 default:
395 if (c != d)
396 usage();
397 goto next;
398 }
399 argc--;
400 argv++;
401 }
402 next:
403
404 scan_devices();
405
406 while (argc)
407 {
408 char *c = argv[0];
409 char *d, *e, *f;
410 int n, i;
411 struct op *op;
412 unsigned long ll, lim;
413
414 if (*c == '-')
415 {
416 if (!c[1] || !strchr("sd", c[1]))
417 usage();
418 if (c[2])
419 d = (c[2] == '=') ? c+3 : c+2;
420 else if (argc)
421 {
422 argc--;
423 argv++;
424 d = argv[0];
425 }
426 else
427 usage();
428 if (state != STATE_GOT_FILTER)
429 {
430 filter_init(&filter);
431 state = STATE_GOT_FILTER;
432 }
433 switch (c[1])
434 {
435 case 's':
436 if (d = filter_parse_slot(&filter, d))
437 {
438 fprintf(stderr, "setpci: -s: %s\n", d);
439 return 1;
440 }
441 break;
442 case 'd':
443 if (d = filter_parse_id(&filter, d))
444 {
445 fprintf(stderr, "setpci: -d: %s\n", d);
446 return 1;
447 }
448 break;
449 default:
450 usage();
451 }
452 }
453 else if (state == STATE_INIT)
454 usage();
455 else
456 {
457 if (state == STATE_GOT_FILTER)
458 selected_devices = select_devices(&filter);
459 if (!selected_devices[0] && !force)
460 fprintf(stderr, "setpci: Warning: No devices selected for `%s'.\n", c);
461 state = STATE_GOT_OP;
462 d = strchr(c, '=');
463 if (d)
464 {
465 *d++ = 0;
466 if (!*d)
467 usage();
468 for(e=d, n=1; *e; e++)
469 if (*e == ',')
470 n++;
471 op = xmalloc(sizeof(struct op) + n*sizeof(unsigned int));
472 }
473 else
474 {
475 n = -1;
476 op = xmalloc(sizeof(struct op));
477 }
478 op->dev_vector = selected_devices;
479 op->num_values = n;
480 e = strchr(c, '.');
481 if (e)
482 {
483 *e++ = 0;
484 if (e[1])
485 usage();
486 switch (*e & 0xdf)
487 {
488 case 'B':
489 op->width = 1; break;
490 case 'W':
491 op->width = 2; break;
492 case 'L':
493 op->width = 4; break;
494 default:
495 usage();
496 }
497 }
498 else
499 op->width = 1;
500 ll = strtol(c, &f, 16);
501 if (f && *f)
502 {
503 struct reg_name *r;
504 for(r = pci_reg_names; r->name; r++)
505 if (!strcasecmp(r->name, c))
506 break;
507 if (!r->name || e)
508 usage();
509 ll = r->offset;
510 op->width = r->width;
511 }
512 if (ll > 0x100 || ll + op->width*((n < 0) ? 1 : n) > 0x100)
513 {
514 fprintf(stderr, "setpci: Register number out of range!\n");
515 return 1;
516 }
517 if (ll & (op->width - 1))
518 {
519 fprintf(stderr, "setpci: Unaligned register address!\n");
520 return 1;
521 }
522 op->addr = ll;
523 for(i=0; i<n; i++)
524 {
525 e = strchr(d, ',');
526 if (e)
527 *e++ = 0;
528 ll = strtoul(d, &f, 16);
529 lim = (2 << ((op->width << 3) - 1)) - 1;
530 if (f && *f ||
531 (ll > lim && ll < ~0UL - lim))
532 usage();
533 op->values[i] = ll;
534 d = e;
535 }
536 *last_op = op;
537 last_op = &op->next;
538 op->next = NULL;
539 }
540 argc--;
541 argv++;
542 }
543 if (state == STATE_INIT)
544 usage();
545
546 scan_ops(first_op);
547 execute(first_op);
548
549 return 0;
550 }