]> git.ipfire.org Git - thirdparty/pciutils.git/blame - setpci.c
Moved the new field at the end of struct pci_dev to keep the ABI.
[thirdparty/pciutils.git] / setpci.c
CommitLineData
e4842ff3 1/*
4284af58 2 * The PCI Utilities -- Manipulate PCI Configuration Registers
e4842ff3 3 *
1b99a704 4 * Copyright (c) 1998--2008 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
1b99a704 15#define PCIUTILS_SETPCI
e4842ff3
MM
16#include "pciutils.h"
17
18static int force; /* Don't complain if no devices match */
19static int verbose; /* Verbosity level */
b2c9b373 20static int demo_mode; /* Only show */
e4842ff3 21
81afa98c
MM
22const char program_name[] = "setpci";
23
727ce158 24static struct pci_access *pacc;
e4842ff3 25
b7351143
MM
26struct value {
27 unsigned int value;
28 unsigned int mask;
29};
30
e4842ff3
MM
31struct op {
32 struct op *next;
727ce158 33 struct pci_dev **dev_vector;
e4842ff3
MM
34 unsigned int addr;
35 unsigned int width; /* Byte width of the access */
b69fad0d 36 int num_values; /* Number of values to write; 0=read */
b7351143 37 struct value values[0];
e4842ff3
MM
38};
39
40static struct op *first_op, **last_op = &first_op;
b7351143 41static unsigned int max_values[] = { 0, 0xff, 0xffff, 0, 0xffffffff };
e4842ff3 42
727ce158 43static struct pci_dev **
e4842ff3
MM
44select_devices(struct pci_filter *filt)
45{
727ce158 46 struct pci_dev *z, **a, **b;
e4842ff3
MM
47 int cnt = 1;
48
de7ef8bc 49 for (z=pacc->devices; z; z=z->next)
727ce158 50 if (pci_filter_match(filt, z))
e4842ff3
MM
51 cnt++;
52 a = b = xmalloc(sizeof(struct device *) * cnt);
de7ef8bc 53 for (z=pacc->devices; z; z=z->next)
727ce158 54 if (pci_filter_match(filt, z))
e4842ff3
MM
55 *a++ = z;
56 *a = NULL;
57 return b;
58}
59
60static void
727ce158 61exec_op(struct op *op, struct pci_dev *dev)
e4842ff3 62{
b7351143
MM
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;
33bc28a5 66 int i, addr;
b7351143 67 int width = op->width;
e4842ff3
MM
68
69 if (verbose)
727ce158 70 printf("%02x:%02x.%x:%02x", dev->bus, dev->dev, dev->func, op->addr);
33bc28a5 71 addr = op->addr;
b69fad0d 72 if (op->num_values > 0)
b7351143 73 {
de7ef8bc 74 for (i=0; i<op->num_values; i++)
b7351143
MM
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 }
e4842ff3
MM
126 else
127 {
128 if (verbose)
b2c9b373 129 printf(" = ");
b251f40b 130 switch (width)
b2c9b373 131 {
b251f40b
MM
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;
b2c9b373 141 }
b251f40b 142 printf(formats[width], x);
b7351143 143 putchar('\n');
e4842ff3
MM
144 }
145}
146
147static void
148execute(struct op *op)
149{
727ce158
MM
150 struct pci_dev **vec = NULL;
151 struct pci_dev **pdev, *dev;
e4842ff3
MM
152 struct op *oops;
153
154 while (op)
155 {
156 pdev = vec = op->dev_vector;
157 while (dev = *pdev++)
de7ef8bc 158 for (oops=op; oops && oops->dev_vector == vec; oops=oops->next)
e4842ff3
MM
159 exec_op(oops, dev);
160 while (op && op->dev_vector == vec)
161 op = op->next;
162 }
163}
164
b2c9b373
MM
165static void
166scan_ops(struct op *op)
167{
b2c9b373
MM
168 while (op)
169 {
170 if (op->num_values >= 0)
727ce158 171 pacc->writeable = 1;
b2c9b373
MM
172 op = op->next;
173 }
174}
175
176struct reg_name {
3fe8a38d
MM
177 unsigned int offset;
178 unsigned int width;
b7351143 179 const char *name;
b2c9b373
MM
180};
181
b7351143 182static const struct reg_name pci_reg_names[] = {
b2c9b373
MM
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
4f6b38ca 250static void NONRET PCI_PRINTF(1,2)
3fe8a38d 251usage(char *msg, ...)
e4842ff3 252{
3fe8a38d
MM
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 }
e4842ff3 261 fprintf(stderr,
6add52f5 262"Usage: setpci [<options>] (<device>+ <reg>[=<values>]*)*\n"
1b99a704
MM
263"\n"
264"General options:\n"
6add52f5
MM
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"
1b99a704
MM
268"\n"
269"PCI access options:\n"
727ce158 270GENERIC_HELP
1b99a704
MM
271"\n"
272"Setting commands:\n"
84c8d1bb 273"<device>:\t-s [[[<domain>]:][<bus>]:][<slot>][.[<func>]]\n"
b7351143
MM
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");
e4842ff3
MM
280 exit(1);
281}
282
7b08ebab
MM
283static int
284parse_options(int argc, char **argv)
e4842ff3 285{
7b08ebab
MM
286 char *opts = GENERIC_OPTIONS;
287 int i=1;
e4842ff3 288
7aad822b
MM
289 if (argc == 2 && !strcmp(argv[1], "--version"))
290 {
291 puts("setpci version " PCIUTILS_VERSION);
292 return 0;
293 }
727ce158 294
7b08ebab 295 while (i < argc && argv[i][0] == '-')
e4842ff3 296 {
b69fad0d 297 char *c = argv[i++] + 1;
e4842ff3 298 char *d = c;
727ce158 299 char *e;
e4842ff3
MM
300 while (*c)
301 switch (*c)
302 {
7b08ebab
MM
303 case 0:
304 break;
e4842ff3
MM
305 case 'v':
306 verbose++;
307 c++;
308 break;
309 case 'f':
310 force++;
311 c++;
312 break;
b2c9b373
MM
313 case 'D':
314 demo_mode++;
315 c++;
316 break;
e4842ff3 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;
b69fad0d
MM
326 else if (i < argc)
327 arg = argv[i++];
727ce158 328 else
3fe8a38d 329 usage(NULL);
727ce158
MM
330 c = "";
331 }
332 else
333 arg = NULL;
334 if (!parse_generic_option(*e, pacc, arg))
3fe8a38d 335 usage(NULL);
727ce158
MM
336 }
337 else
338 {
339 if (c != d)
3fe8a38d 340 usage(NULL);
b69fad0d 341 return i-1;
727ce158 342 }
e4842ff3 343 }
e4842ff3 344 }
e4842ff3 345
7b08ebab
MM
346 return i;
347}
e4842ff3 348
b69fad0d
MM
349static 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
379static 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
7b08ebab
MM
478static 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)
e4842ff3 485 {
b69fad0d 486 char *c = argv[i++];
e4842ff3
MM
487
488 if (*c == '-')
489 {
e4842ff3 490 if (state != STATE_GOT_FILTER)
b69fad0d
MM
491 pci_filter_init(pacc, &filter);
492 i = parse_filter(argc, argv, i-1, &filter);
493 state = STATE_GOT_FILTER;
e4842ff3 494 }
e4842ff3
MM
495 else
496 {
b69fad0d
MM
497 if (state == STATE_INIT)
498 usage(NULL);
e4842ff3
MM
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);
b69fad0d 503 parse_op(c, selected_devices);
e4842ff3 504 state = STATE_GOT_OP;
e4842ff3 505 }
e4842ff3
MM
506 }
507 if (state == STATE_INIT)
3fe8a38d 508 usage("No operation specified");
7b08ebab
MM
509}
510
511int
512main(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);
e4842ff3 522
7b08ebab 523 parse_ops(argc, argv, i);
b2c9b373 524 scan_ops(first_op);
e4842ff3
MM
525 execute(first_op);
526
527 return 0;
528}