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