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