xref: /freebsd/usr.sbin/pciconf/pciconf.c (revision 7aa383846770374466b1dcb2cefd71bde9acf463)
1 /*
2  * Copyright 1996 Massachusetts Institute of Technology
3  *
4  * Permission to use, copy, modify, and distribute this software and
5  * its documentation for any purpose and without fee is hereby
6  * granted, provided that both the above copyright notice and this
7  * permission notice appear in all copies, that both the above
8  * copyright notice and this permission notice appear in all
9  * supporting documentation, and that the name of M.I.T. not be used
10  * in advertising or publicity pertaining to distribution of the
11  * software without specific, written prior permission.  M.I.T. makes
12  * no representations about the suitability of this software for any
13  * purpose.  It is provided "as is" without express or implied
14  * warranty.
15  *
16  * THIS SOFTWARE IS PROVIDED BY M.I.T. ``AS IS''.  M.I.T. DISCLAIMS
17  * ALL EXPRESS OR IMPLIED WARRANTIES WITH REGARD TO THIS SOFTWARE,
18  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
19  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT
20  * SHALL M.I.T. BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
23  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
25  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
26  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  */
29 
30 #ifndef lint
31 static const char rcsid[] =
32   "$FreeBSD$";
33 #endif /* not lint */
34 
35 #include <sys/types.h>
36 #include <sys/fcntl.h>
37 
38 #include <ctype.h>
39 #include <err.h>
40 #include <inttypes.h>
41 #include <stdlib.h>
42 #include <stdio.h>
43 #include <string.h>
44 #include <unistd.h>
45 #include <sys/pciio.h>
46 #include <sys/queue.h>
47 
48 #include <dev/pci/pcireg.h>
49 
50 #include "pathnames.h"
51 #include "pciconf.h"
52 
53 struct pci_device_info
54 {
55     TAILQ_ENTRY(pci_device_info)	link;
56     int					id;
57     char				*desc;
58 };
59 
60 struct pci_vendor_info
61 {
62     TAILQ_ENTRY(pci_vendor_info)	link;
63     TAILQ_HEAD(,pci_device_info)	devs;
64     int					id;
65     char				*desc;
66 };
67 
68 TAILQ_HEAD(,pci_vendor_info)	pci_vendors;
69 
70 static void list_bars(int fd, struct pci_conf *p);
71 static void list_devs(int verbose, int bars, int caps);
72 static void list_verbose(struct pci_conf *p);
73 static const char *guess_class(struct pci_conf *p);
74 static const char *guess_subclass(struct pci_conf *p);
75 static int load_vendors(void);
76 static void readit(const char *, const char *, int);
77 static void writeit(const char *, const char *, const char *, int);
78 static void chkattached(const char *, int);
79 
80 static int exitstatus = 0;
81 
82 static void
83 usage(void)
84 {
85 	fprintf(stderr, "%s\n%s\n%s\n%s\n",
86 		"usage: pciconf -l [-bcv]",
87 		"       pciconf -a selector",
88 		"       pciconf -r [-b | -h] selector addr[:addr2]",
89 		"       pciconf -w [-b | -h] selector addr value");
90 	exit (1);
91 }
92 
93 int
94 main(int argc, char **argv)
95 {
96 	int c;
97 	int listmode, readmode, writemode, attachedmode, bars, caps, verbose;
98 	int byte, isshort;
99 
100 	listmode = readmode = writemode = attachedmode = bars = caps = verbose = byte = isshort = 0;
101 
102 	while ((c = getopt(argc, argv, "abchlrwv")) != -1) {
103 		switch(c) {
104 		case 'a':
105 			attachedmode = 1;
106 			break;
107 
108 		case 'b':
109 			bars = 1;
110 			byte = 1;
111 			break;
112 
113 		case 'c':
114 			caps = 1;
115 			break;
116 
117 		case 'h':
118 			isshort = 1;
119 			break;
120 
121 		case 'l':
122 			listmode = 1;
123 			break;
124 
125 		case 'r':
126 			readmode = 1;
127 			break;
128 
129 		case 'w':
130 			writemode = 1;
131 			break;
132 
133 		case 'v':
134 			verbose = 1;
135 			break;
136 
137 		default:
138 			usage();
139 		}
140 	}
141 
142 	if ((listmode && optind != argc)
143 	    || (writemode && optind + 3 != argc)
144 	    || (readmode && optind + 2 != argc)
145 	    || (attachedmode && optind + 1 != argc))
146 		usage();
147 
148 	if (listmode) {
149 		list_devs(verbose, bars, caps);
150 	} else if (attachedmode) {
151 		chkattached(argv[optind],
152 		    byte ? 1 : isshort ? 2 : 4);
153 	} else if (readmode) {
154 		readit(argv[optind], argv[optind + 1],
155 		    byte ? 1 : isshort ? 2 : 4);
156 	} else if (writemode) {
157 		writeit(argv[optind], argv[optind + 1], argv[optind + 2],
158 		    byte ? 1 : isshort ? 2 : 4);
159 	} else {
160 		usage();
161 	}
162 
163 	return exitstatus;
164 }
165 
166 static void
167 list_devs(int verbose, int bars, int caps)
168 {
169 	int fd;
170 	struct pci_conf_io pc;
171 	struct pci_conf conf[255], *p;
172 	int none_count = 0;
173 
174 	if (verbose)
175 		load_vendors();
176 
177 	fd = open(_PATH_DEVPCI, caps ? O_RDWR : O_RDONLY, 0);
178 	if (fd < 0)
179 		err(1, "%s", _PATH_DEVPCI);
180 
181 	bzero(&pc, sizeof(struct pci_conf_io));
182 	pc.match_buf_len = sizeof(conf);
183 	pc.matches = conf;
184 
185 	do {
186 		if (ioctl(fd, PCIOCGETCONF, &pc) == -1)
187 			err(1, "ioctl(PCIOCGETCONF)");
188 
189 		/*
190 		 * 255 entries should be more than enough for most people,
191 		 * but if someone has more devices, and then changes things
192 		 * around between ioctls, we'll do the cheezy thing and
193 		 * just bail.  The alternative would be to go back to the
194 		 * beginning of the list, and print things twice, which may
195 		 * not be desireable.
196 		 */
197 		if (pc.status == PCI_GETCONF_LIST_CHANGED) {
198 			warnx("PCI device list changed, please try again");
199 			exitstatus = 1;
200 			close(fd);
201 			return;
202 		} else if (pc.status ==  PCI_GETCONF_ERROR) {
203 			warnx("error returned from PCIOCGETCONF ioctl");
204 			exitstatus = 1;
205 			close(fd);
206 			return;
207 		}
208 		for (p = conf; p < &conf[pc.num_matches]; p++) {
209 			printf("%s%d@pci%d:%d:%d:%d:\tclass=0x%06x card=0x%08x "
210 			    "chip=0x%08x rev=0x%02x hdr=0x%02x\n",
211 			    (p->pd_name && *p->pd_name) ? p->pd_name :
212 			    "none",
213 			    (p->pd_name && *p->pd_name) ? (int)p->pd_unit :
214 			    none_count++, p->pc_sel.pc_domain,
215 			    p->pc_sel.pc_bus, p->pc_sel.pc_dev,
216 			    p->pc_sel.pc_func, (p->pc_class << 16) |
217 			    (p->pc_subclass << 8) | p->pc_progif,
218 			    (p->pc_subdevice << 16) | p->pc_subvendor,
219 			    (p->pc_device << 16) | p->pc_vendor,
220 			    p->pc_revid, p->pc_hdr);
221 			if (verbose)
222 				list_verbose(p);
223 			if (bars)
224 				list_bars(fd, p);
225 			if (caps)
226 				list_caps(fd, p);
227 		}
228 	} while (pc.status == PCI_GETCONF_MORE_DEVS);
229 
230 	close(fd);
231 }
232 
233 static void
234 list_bars(int fd, struct pci_conf *p)
235 {
236 	struct pci_bar_io bar;
237 	uint64_t base;
238 	const char *type;
239 	int i, range, max;
240 
241 	switch (p->pc_hdr & PCIM_HDRTYPE) {
242 	case PCIM_HDRTYPE_NORMAL:
243 		max = PCIR_MAX_BAR_0;
244 		break;
245 	case PCIM_HDRTYPE_BRIDGE:
246 		max = PCIR_MAX_BAR_1;
247 		break;
248 	case PCIM_HDRTYPE_CARDBUS:
249 		max = PCIR_MAX_BAR_2;
250 		break;
251 	default:
252 		return;
253 	}
254 
255 	for (i = 0; i <= max; i++) {
256 		bar.pbi_sel = p->pc_sel;
257 		bar.pbi_reg = PCIR_BAR(i);
258 		if (ioctl(fd, PCIOCGETBAR, &bar) < 0)
259 			continue;
260 		if (PCI_BAR_IO(bar.pbi_base)) {
261 			type = "I/O Port";
262 			range = 32;
263 			base = bar.pbi_base & PCIM_BAR_IO_BASE;
264 		} else {
265 			if (bar.pbi_base & PCIM_BAR_MEM_PREFETCH)
266 				type = "Prefetchable Memory";
267 			else
268 				type = "Memory";
269 			switch (bar.pbi_base & PCIM_BAR_MEM_TYPE) {
270 			case PCIM_BAR_MEM_32:
271 				range = 32;
272 				break;
273 			case PCIM_BAR_MEM_1MB:
274 				range = 20;
275 				break;
276 			case PCIM_BAR_MEM_64:
277 				range = 64;
278 				break;
279 			default:
280 				range = -1;
281 			}
282 			base = bar.pbi_base & ~((uint64_t)0xf);
283 		}
284 		printf("    bar   [%02x] = type %s, range %2d, base %#jx, ",
285 		    PCIR_BAR(i), type, range, (uintmax_t)base);
286 		printf("size %2d, %s\n", (int)bar.pbi_length,
287 		    bar.pbi_enabled ? "enabled" : "disabled");
288 	}
289 }
290 
291 static void
292 list_verbose(struct pci_conf *p)
293 {
294 	struct pci_vendor_info	*vi;
295 	struct pci_device_info	*di;
296 	const char *dp;
297 
298 	TAILQ_FOREACH(vi, &pci_vendors, link) {
299 		if (vi->id == p->pc_vendor) {
300 			printf("    vendor     = '%s'\n", vi->desc);
301 			break;
302 		}
303 	}
304 	if (vi == NULL) {
305 		di = NULL;
306 	} else {
307 		TAILQ_FOREACH(di, &vi->devs, link) {
308 			if (di->id == p->pc_device) {
309 				printf("    device     = '%s'\n", di->desc);
310 				break;
311 			}
312 		}
313 	}
314 	if ((dp = guess_class(p)) != NULL)
315 		printf("    class      = %s\n", dp);
316 	if ((dp = guess_subclass(p)) != NULL)
317 		printf("    subclass   = %s\n", dp);
318 }
319 
320 /*
321  * This is a direct cut-and-paste from the table in sys/dev/pci/pci.c.
322  */
323 static struct
324 {
325 	int	class;
326 	int	subclass;
327 	const char *desc;
328 } pci_nomatch_tab[] = {
329 	{PCIC_OLD,		-1,			"old"},
330 	{PCIC_OLD,		PCIS_OLD_NONVGA,	"non-VGA display device"},
331 	{PCIC_OLD,		PCIS_OLD_VGA,		"VGA-compatible display device"},
332 	{PCIC_STORAGE,		-1,			"mass storage"},
333 	{PCIC_STORAGE,		PCIS_STORAGE_SCSI,	"SCSI"},
334 	{PCIC_STORAGE,		PCIS_STORAGE_IDE,	"ATA"},
335 	{PCIC_STORAGE,		PCIS_STORAGE_FLOPPY,	"floppy disk"},
336 	{PCIC_STORAGE,		PCIS_STORAGE_IPI,	"IPI"},
337 	{PCIC_STORAGE,		PCIS_STORAGE_RAID,	"RAID"},
338 	{PCIC_STORAGE,		PCIS_STORAGE_ATA_ADMA,	"ATA (ADMA)"},
339 	{PCIC_STORAGE,		PCIS_STORAGE_SATA,	"SATA"},
340 	{PCIC_STORAGE,		PCIS_STORAGE_SAS,	"SAS"},
341 	{PCIC_NETWORK,		-1,			"network"},
342 	{PCIC_NETWORK,		PCIS_NETWORK_ETHERNET,	"ethernet"},
343 	{PCIC_NETWORK,		PCIS_NETWORK_TOKENRING,	"token ring"},
344 	{PCIC_NETWORK,		PCIS_NETWORK_FDDI,	"fddi"},
345 	{PCIC_NETWORK,		PCIS_NETWORK_ATM,	"ATM"},
346 	{PCIC_NETWORK,		PCIS_NETWORK_ISDN,	"ISDN"},
347 	{PCIC_DISPLAY,		-1,			"display"},
348 	{PCIC_DISPLAY,		PCIS_DISPLAY_VGA,	"VGA"},
349 	{PCIC_DISPLAY,		PCIS_DISPLAY_XGA,	"XGA"},
350 	{PCIC_DISPLAY,		PCIS_DISPLAY_3D,	"3D"},
351 	{PCIC_MULTIMEDIA,	-1,			"multimedia"},
352 	{PCIC_MULTIMEDIA,	PCIS_MULTIMEDIA_VIDEO,	"video"},
353 	{PCIC_MULTIMEDIA,	PCIS_MULTIMEDIA_AUDIO,	"audio"},
354 	{PCIC_MULTIMEDIA,	PCIS_MULTIMEDIA_TELE,	"telephony"},
355 	{PCIC_MULTIMEDIA,	PCIS_MULTIMEDIA_HDA,	"HDA"},
356 	{PCIC_MEMORY,		-1,			"memory"},
357 	{PCIC_MEMORY,		PCIS_MEMORY_RAM,	"RAM"},
358 	{PCIC_MEMORY,		PCIS_MEMORY_FLASH,	"flash"},
359 	{PCIC_BRIDGE,		-1,			"bridge"},
360 	{PCIC_BRIDGE,		PCIS_BRIDGE_HOST,	"HOST-PCI"},
361 	{PCIC_BRIDGE,		PCIS_BRIDGE_ISA,	"PCI-ISA"},
362 	{PCIC_BRIDGE,		PCIS_BRIDGE_EISA,	"PCI-EISA"},
363 	{PCIC_BRIDGE,		PCIS_BRIDGE_MCA,	"PCI-MCA"},
364 	{PCIC_BRIDGE,		PCIS_BRIDGE_PCI,	"PCI-PCI"},
365 	{PCIC_BRIDGE,		PCIS_BRIDGE_PCMCIA,	"PCI-PCMCIA"},
366 	{PCIC_BRIDGE,		PCIS_BRIDGE_NUBUS,	"PCI-NuBus"},
367 	{PCIC_BRIDGE,		PCIS_BRIDGE_CARDBUS,	"PCI-CardBus"},
368 	{PCIC_BRIDGE,		PCIS_BRIDGE_RACEWAY,	"PCI-RACEway"},
369 	{PCIC_SIMPLECOMM,	-1,			"simple comms"},
370 	{PCIC_SIMPLECOMM,	PCIS_SIMPLECOMM_UART,	"UART"},	/* could detect 16550 */
371 	{PCIC_SIMPLECOMM,	PCIS_SIMPLECOMM_PAR,	"parallel port"},
372 	{PCIC_SIMPLECOMM,	PCIS_SIMPLECOMM_MULSER,	"multiport serial"},
373 	{PCIC_SIMPLECOMM,	PCIS_SIMPLECOMM_MODEM,	"generic modem"},
374 	{PCIC_BASEPERIPH,	-1,			"base peripheral"},
375 	{PCIC_BASEPERIPH,	PCIS_BASEPERIPH_PIC,	"interrupt controller"},
376 	{PCIC_BASEPERIPH,	PCIS_BASEPERIPH_DMA,	"DMA controller"},
377 	{PCIC_BASEPERIPH,	PCIS_BASEPERIPH_TIMER,	"timer"},
378 	{PCIC_BASEPERIPH,	PCIS_BASEPERIPH_RTC,	"realtime clock"},
379 	{PCIC_BASEPERIPH,	PCIS_BASEPERIPH_PCIHOT,	"PCI hot-plug controller"},
380 	{PCIC_BASEPERIPH,	PCIS_BASEPERIPH_SDHC,	"SD host controller"},
381 	{PCIC_INPUTDEV,		-1,			"input device"},
382 	{PCIC_INPUTDEV,		PCIS_INPUTDEV_KEYBOARD,	"keyboard"},
383 	{PCIC_INPUTDEV,		PCIS_INPUTDEV_DIGITIZER,"digitizer"},
384 	{PCIC_INPUTDEV,		PCIS_INPUTDEV_MOUSE,	"mouse"},
385 	{PCIC_INPUTDEV,		PCIS_INPUTDEV_SCANNER,	"scanner"},
386 	{PCIC_INPUTDEV,		PCIS_INPUTDEV_GAMEPORT,	"gameport"},
387 	{PCIC_DOCKING,		-1,			"docking station"},
388 	{PCIC_PROCESSOR,	-1,			"processor"},
389 	{PCIC_SERIALBUS,	-1,			"serial bus"},
390 	{PCIC_SERIALBUS,	PCIS_SERIALBUS_FW,	"FireWire"},
391 	{PCIC_SERIALBUS,	PCIS_SERIALBUS_ACCESS,	"AccessBus"},
392 	{PCIC_SERIALBUS,	PCIS_SERIALBUS_SSA,	"SSA"},
393 	{PCIC_SERIALBUS,	PCIS_SERIALBUS_USB,	"USB"},
394 	{PCIC_SERIALBUS,	PCIS_SERIALBUS_FC,	"Fibre Channel"},
395 	{PCIC_SERIALBUS,	PCIS_SERIALBUS_SMBUS,	"SMBus"},
396 	{PCIC_WIRELESS,		-1,			"wireless controller"},
397 	{PCIC_WIRELESS,		PCIS_WIRELESS_IRDA,	"iRDA"},
398 	{PCIC_WIRELESS,		PCIS_WIRELESS_IR,	"IR"},
399 	{PCIC_WIRELESS,		PCIS_WIRELESS_RF,	"RF"},
400 	{PCIC_INTELLIIO,	-1,			"intelligent I/O controller"},
401 	{PCIC_INTELLIIO,	PCIS_INTELLIIO_I2O,	"I2O"},
402 	{PCIC_SATCOM,		-1,			"satellite communication"},
403 	{PCIC_SATCOM,		PCIS_SATCOM_TV,		"sat TV"},
404 	{PCIC_SATCOM,		PCIS_SATCOM_AUDIO,	"sat audio"},
405 	{PCIC_SATCOM,		PCIS_SATCOM_VOICE,	"sat voice"},
406 	{PCIC_SATCOM,		PCIS_SATCOM_DATA,	"sat data"},
407 	{PCIC_CRYPTO,		-1,			"encrypt/decrypt"},
408 	{PCIC_CRYPTO,		PCIS_CRYPTO_NETCOMP,	"network/computer crypto"},
409 	{PCIC_CRYPTO,		PCIS_CRYPTO_NETCOMP,	"entertainment crypto"},
410 	{PCIC_DASP,		-1,			"dasp"},
411 	{PCIC_DASP,		PCIS_DASP_DPIO,		"DPIO module"},
412 	{0, 0,		NULL}
413 };
414 
415 static const char *
416 guess_class(struct pci_conf *p)
417 {
418 	int	i;
419 
420 	for (i = 0; pci_nomatch_tab[i].desc != NULL; i++) {
421 		if (pci_nomatch_tab[i].class == p->pc_class)
422 			return(pci_nomatch_tab[i].desc);
423 	}
424 	return(NULL);
425 }
426 
427 static const char *
428 guess_subclass(struct pci_conf *p)
429 {
430 	int	i;
431 
432 	for (i = 0; pci_nomatch_tab[i].desc != NULL; i++) {
433 		if ((pci_nomatch_tab[i].class == p->pc_class) &&
434 		    (pci_nomatch_tab[i].subclass == p->pc_subclass))
435 			return(pci_nomatch_tab[i].desc);
436 	}
437 	return(NULL);
438 }
439 
440 static int
441 load_vendors(void)
442 {
443 	const char *dbf;
444 	FILE *db;
445 	struct pci_vendor_info *cv;
446 	struct pci_device_info *cd;
447 	char buf[1024], str[1024];
448 	char *ch;
449 	int id, error;
450 
451 	/*
452 	 * Locate the database and initialise.
453 	 */
454 	TAILQ_INIT(&pci_vendors);
455 	if ((dbf = getenv("PCICONF_VENDOR_DATABASE")) == NULL)
456 		dbf = _PATH_PCIVDB;
457 	if ((db = fopen(dbf, "r")) == NULL)
458 		return(1);
459 	cv = NULL;
460 	cd = NULL;
461 	error = 0;
462 
463 	/*
464 	 * Scan input lines from the database
465 	 */
466 	for (;;) {
467 		if (fgets(buf, sizeof(buf), db) == NULL)
468 			break;
469 
470 		if ((ch = strchr(buf, '#')) != NULL)
471 			*ch = '\0';
472 		ch = strchr(buf, '\0') - 1;
473 		while (ch > buf && isspace(*ch))
474 			*ch-- = '\0';
475 		if (ch <= buf)
476 			continue;
477 
478 		/* Can't handle subvendor / subdevice entries yet */
479 		if (buf[0] == '\t' && buf[1] == '\t')
480 			continue;
481 
482 		/* Check for vendor entry */
483 		if (buf[0] != '\t' && sscanf(buf, "%04x %[^\n]", &id, str) == 2) {
484 			if ((id == 0) || (strlen(str) < 1))
485 				continue;
486 			if ((cv = malloc(sizeof(struct pci_vendor_info))) == NULL) {
487 				warn("allocating vendor entry");
488 				error = 1;
489 				break;
490 			}
491 			if ((cv->desc = strdup(str)) == NULL) {
492 				free(cv);
493 				warn("allocating vendor description");
494 				error = 1;
495 				break;
496 			}
497 			cv->id = id;
498 			TAILQ_INIT(&cv->devs);
499 			TAILQ_INSERT_TAIL(&pci_vendors, cv, link);
500 			continue;
501 		}
502 
503 		/* Check for device entry */
504 		if (buf[0] == '\t' && sscanf(buf + 1, "%04x %[^\n]", &id, str) == 2) {
505 			if ((id == 0) || (strlen(str) < 1))
506 				continue;
507 			if (cv == NULL) {
508 				warnx("device entry with no vendor!");
509 				continue;
510 			}
511 			if ((cd = malloc(sizeof(struct pci_device_info))) == NULL) {
512 				warn("allocating device entry");
513 				error = 1;
514 				break;
515 			}
516 			if ((cd->desc = strdup(str)) == NULL) {
517 				free(cd);
518 				warn("allocating device description");
519 				error = 1;
520 				break;
521 			}
522 			cd->id = id;
523 			TAILQ_INSERT_TAIL(&cv->devs, cd, link);
524 			continue;
525 		}
526 
527 		/* It's a comment or junk, ignore it */
528 	}
529 	if (ferror(db))
530 		error = 1;
531 	fclose(db);
532 
533 	return(error);
534 }
535 
536 uint32_t
537 read_config(int fd, struct pcisel *sel, long reg, int width)
538 {
539 	struct pci_io pi;
540 
541 	pi.pi_sel = *sel;
542 	pi.pi_reg = reg;
543 	pi.pi_width = width;
544 
545 	if (ioctl(fd, PCIOCREAD, &pi) < 0)
546 		err(1, "ioctl(PCIOCREAD)");
547 
548 	return (pi.pi_data);
549 }
550 
551 static struct pcisel
552 getsel(const char *str)
553 {
554 	char *ep = strchr(str, '@');
555 	char *epbase;
556 	struct pcisel sel;
557 	unsigned long selarr[4];
558 	int i;
559 
560 	if (ep == NULL)
561 		ep = (char *)str;
562 	else
563 		ep++;
564 
565 	epbase = ep;
566 
567 	if (strncmp(ep, "pci", 3) == 0) {
568 		ep += 3;
569 		i = 0;
570 		do {
571 			selarr[i++] = strtoul(ep, &ep, 10);
572 		} while ((*ep == ':' || *ep == '.') && *++ep != '\0' && i < 4);
573 
574 		if (i > 2)
575 			sel.pc_func = selarr[--i];
576 		else
577 			sel.pc_func = 0;
578 		sel.pc_dev = selarr[--i];
579 		sel.pc_bus = selarr[--i];
580 		if (i > 0)
581 			sel.pc_domain = selarr[--i];
582 		else
583 			sel.pc_domain = 0;
584 	}
585 	if (*ep != '\x0' || ep == epbase)
586 		errx(1, "cannot parse selector %s", str);
587 	return sel;
588 }
589 
590 static void
591 readone(int fd, struct pcisel *sel, long reg, int width)
592 {
593 
594 	printf("%0*x", width*2, read_config(fd, sel, reg, width));
595 }
596 
597 static void
598 readit(const char *name, const char *reg, int width)
599 {
600 	long rstart;
601 	long rend;
602 	long r;
603 	char *end;
604 	int i;
605 	int fd;
606 	struct pcisel sel;
607 
608 	fd = open(_PATH_DEVPCI, O_RDWR, 0);
609 	if (fd < 0)
610 		err(1, "%s", _PATH_DEVPCI);
611 
612 	rend = rstart = strtol(reg, &end, 0);
613 	if (end && *end == ':') {
614 		end++;
615 		rend = strtol(end, (char **) 0, 0);
616 	}
617 	sel = getsel(name);
618 	for (i = 1, r = rstart; r <= rend; i++, r += width) {
619 		readone(fd, &sel, r, width);
620 		if (i && !(i % 8))
621 			putchar(' ');
622 		putchar(i % (16/width) ? ' ' : '\n');
623 	}
624 	if (i % (16/width) != 1)
625 		putchar('\n');
626 	close(fd);
627 }
628 
629 static void
630 writeit(const char *name, const char *reg, const char *data, int width)
631 {
632 	int fd;
633 	struct pci_io pi;
634 
635 	pi.pi_sel = getsel(name);
636 	pi.pi_reg = strtoul(reg, (char **)0, 0); /* XXX error check */
637 	pi.pi_width = width;
638 	pi.pi_data = strtoul(data, (char **)0, 0); /* XXX error check */
639 
640 	fd = open(_PATH_DEVPCI, O_RDWR, 0);
641 	if (fd < 0)
642 		err(1, "%s", _PATH_DEVPCI);
643 
644 	if (ioctl(fd, PCIOCWRITE, &pi) < 0)
645 		err(1, "ioctl(PCIOCWRITE)");
646 }
647 
648 static void
649 chkattached(const char *name, int width)
650 {
651 	int fd;
652 	struct pci_io pi;
653 
654 	pi.pi_sel = getsel(name);
655 	pi.pi_reg = 0;
656 	pi.pi_width = width;
657 	pi.pi_data = 0;
658 
659 	fd = open(_PATH_DEVPCI, O_RDWR, 0);
660 	if (fd < 0)
661 		err(1, "%s", _PATH_DEVPCI);
662 
663 	if (ioctl(fd, PCIOCATTACHED, &pi) < 0)
664 		err(1, "ioctl(PCIOCATTACHED)");
665 
666 	exitstatus = pi.pi_data ? 0 : 2; /* exit(2), if NOT attached */
667 	printf("%s: %s%s\n", name, pi.pi_data == 0 ? "not " : "", "attached");
668 }
669