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