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