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