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