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