]> mj.ucw.cz Git - pciutils.git/blob - lib/names.c
22d050263c372715af55d4fd9f6daaf75d79dc32
[pciutils.git] / lib / names.c
1 /*
2  *      The PCI Library -- ID to Name Translation
3  *
4  *      Copyright (c) 1997--2007 Martin Mares <mj@ucw.cz>
5  *
6  *      Can be freely distributed and used under the terms of the GNU GPL.
7  */
8
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <stdarg.h>
12 #include <string.h>
13 #include <errno.h>
14 #include <arpa/nameser.h>
15 #include <resolv.h>
16
17 #include "internal.h"
18
19 #ifdef PCI_COMPRESSED_IDS
20 #include <zlib.h>
21 typedef gzFile pci_file;
22 #define pci_gets(f, l, s)       gzgets(f, l, s)
23 #define pci_eof(f)              gzeof(f)
24
25 static pci_file pci_open(struct pci_access *a)
26 {
27   pci_file result;
28   size_t len;
29   char *new_name;
30
31   result = gzopen(a->id_file_name, "r");
32   if (result)
33     return result;
34   len = strlen(a->id_file_name);
35   if (len >= 3 && memcmp(a->id_file_name + len - 3, ".gz", 3) != 0)
36     return result;
37   new_name = malloc(len - 2);
38   memcpy(new_name, a->id_file_name, len - 3);
39   new_name[len - 3] = 0;
40   pci_set_name_list_path(a, new_name, 1);
41   return gzopen(a->id_file_name, "r");
42 }
43
44 #define pci_close(f)            gzclose(f)
45 #define PCI_ERROR(f, err)                                               \
46         if (!err) {                                                     \
47                 int errnum;                                             \
48                 err = gzerror(f, &errnum);                              \
49                 if (errnum == Z_ERRNO)  err = "I/O error";              \
50                 else if (errnum >= 0)   err = NULL;                     \
51         }
52 #else
53 typedef FILE * pci_file;
54 #define pci_gets(f, l, s)       fgets(l, s, f)
55 #define pci_eof(f)              feof(f)
56 #define pci_open(a)             fopen(a->id_file_name, "r")
57 #define pci_close(f)            fclose(f)
58 #define PCI_ERROR(f, err)       if (!err && ferror(f))  err = "I/O error";
59 #endif
60
61 struct id_entry {
62   struct id_entry *next;
63   u32 id12, id34;
64   byte cat;
65   char name[1];
66 };
67
68 enum id_entry_type {
69   ID_UNKNOWN,
70   ID_VENDOR,
71   ID_DEVICE,
72   ID_SUBSYSTEM,
73   ID_GEN_SUBSYSTEM,
74   ID_CLASS,
75   ID_SUBCLASS,
76   ID_PROGIF
77 };
78
79 struct id_bucket {
80   struct id_bucket *next;
81   unsigned int full;
82 };
83
84 #define MAX_LINE 1024
85 #define BUCKET_SIZE 8192
86 #define HASH_SIZE 4099
87
88 #ifdef __GNUC__
89 #define BUCKET_ALIGNMENT __alignof__(struct id_bucket)
90 #else
91 union id_align {
92   struct id_bucket *next;
93   unsigned int full;
94 };
95 #define BUCKET_ALIGNMENT sizeof(union id_align)
96 #endif
97 #define BUCKET_ALIGN(n) ((n)+BUCKET_ALIGNMENT-(n)%BUCKET_ALIGNMENT)
98
99 static void *id_alloc(struct pci_access *a, unsigned int size)
100 {
101   struct id_bucket *buck = a->current_id_bucket;
102   unsigned int pos;
103
104   if (!a->id_hash)
105     {
106       a->id_hash = pci_malloc(a, sizeof(struct id_entry *) * HASH_SIZE);
107       memset(a->id_hash, 0, sizeof(struct id_entry *) * HASH_SIZE);
108     }
109
110   if (!buck || buck->full + size > BUCKET_SIZE)
111     {
112       buck = pci_malloc(a, BUCKET_SIZE);
113       buck->next = a->current_id_bucket;
114       a->current_id_bucket = buck;
115       buck->full = BUCKET_ALIGN(sizeof(struct id_bucket));
116     }
117   pos = buck->full;
118   buck->full = BUCKET_ALIGN(buck->full + size);
119   return (byte *)buck + pos;
120 }
121
122 static inline u32 id_pair(unsigned int x, unsigned int y)
123 {
124   return ((x << 16) | y);
125 }
126
127 static inline unsigned int id_hash(int cat, u32 id12, u32 id34)
128 {
129   unsigned int h;
130
131   h = id12 ^ (id34 << 3) ^ (cat << 5);
132   return h % HASH_SIZE;
133 }
134
135 static char *id_net_lookup(struct pci_access *a, int cat, int id1, int id2, int id3, int id4)
136 {
137   byte name[256], dnsname[256], answer[4096], txt[256];
138   const byte *data;
139   int res, i, j, dlen;
140   ns_msg m;
141   ns_rr rr;
142
143   switch (cat)
144     {
145     case ID_VENDOR:
146       sprintf(name, "%04x", id1);
147       break;
148     case ID_DEVICE:
149       sprintf(name, "%04x.%04x", id2, id1);
150       break;
151     case ID_SUBSYSTEM:
152       sprintf(name, "%04x.%04x.%04x.%04x", id4, id3, id2, id1);
153       break;
154     case ID_GEN_SUBSYSTEM:
155       sprintf(name, "%04x.%04x.s", id2, id1);
156       break;
157     case ID_CLASS:
158       sprintf(name, "%02x.c", id1);
159       break;
160     case ID_SUBCLASS:
161       sprintf(name, "%02x.%02x.c", id2, id1);
162       break;
163     case ID_PROGIF:
164       sprintf(name, "%02x.%02x.%02x.c", id3, id2, id1);
165       break;
166     default:
167       return NULL;
168     }
169   sprintf(dnsname, "%s.%s", name, a->id_domain);
170
171   a->debug("Resolving %s\n", dnsname);
172   res_init();
173   res = res_query(dnsname, ns_c_in, ns_t_txt, answer, sizeof(answer));
174   if (res < 0)
175     {
176       a->debug("\tfailed, h_errno=%d\n", _res.res_h_errno);
177       return NULL;
178     }
179   if (ns_initparse(answer, res, &m) < 0)
180     {
181       a->debug("\tinitparse failed\n");
182       return NULL;
183     }
184   for (i=0; ns_parserr(&m, ns_s_an, i, &rr) >= 0; i++)
185     {
186       a->debug("\tanswer %d (class %d, type %d)\n", i, ns_rr_class(rr), ns_rr_type(rr));
187       if (ns_rr_class(rr) != ns_c_in || ns_rr_type(rr) != ns_t_txt)
188         continue;
189       data = ns_rr_rdata(rr);
190       dlen = ns_rr_rdlen(rr);
191       j = 0;
192       while (j < dlen && j+1+data[j] <= dlen)
193         {
194           memcpy(txt, &data[j+1], data[j]);
195           txt[data[j]] = 0;
196           j += 1+data[j];
197           a->debug("\t\t%s\n", txt);
198           if (txt[0] == 'i' && txt[1] == '=')
199             return strdup(txt+2);       /* FIXME */
200         }
201     }
202
203   return NULL;
204 }
205
206 static int id_insert(struct pci_access *a, int cat, int id1, int id2, int id3, int id4, char *text)
207 {
208   u32 id12 = id_pair(id1, id2);
209   u32 id34 = id_pair(id3, id4);
210   unsigned int h = id_hash(cat, id12, id34);
211   struct id_entry *n = a->id_hash ? a->id_hash[h] : NULL;
212   int len = strlen(text);
213
214   while (n && (n->id12 != id12 || n->id34 != id34 || n->cat != cat))
215     n = n->next;
216   if (n)
217     return 1;
218   n = id_alloc(a, sizeof(struct id_entry) + len);
219   n->id12 = id12;
220   n->id34 = id34;
221   n->cat = cat;
222   memcpy(n->name, text, len+1);
223   n->next = a->id_hash[h];
224   a->id_hash[h] = n;
225   return 0;
226 }
227
228 static char *id_lookup(struct pci_access *a, int cat, int id1, int id2, int id3, int id4)
229 {
230   struct id_entry *n;
231   u32 id12 = id_pair(id1, id2);
232   u32 id34 = id_pair(id3, id4);
233   char *name;
234
235   if (a->id_hash)
236     {
237       n = a->id_hash[id_hash(cat, id12, id34)];
238       while (n && (n->id12 != id12 || n->id34 != id34 || n->cat != cat))
239         n = n->next;
240       if (n)
241         return n->name;
242     }
243   if (name = id_net_lookup(a, cat, id1, id2, id3, id4))
244     {
245       id_insert(a, cat, id1, id2, id3, id4, name);
246       return name;
247     }
248   return NULL;
249 }
250
251 static int id_hex(char *p, int cnt)
252 {
253   int x = 0;
254   while (cnt--)
255     {
256       x <<= 4;
257       if (*p >= '0' && *p <= '9')
258         x += (*p - '0');
259       else if (*p >= 'a' && *p <= 'f')
260         x += (*p - 'a' + 10);
261       else if (*p >= 'A' && *p <= 'F')
262         x += (*p - 'A' + 10);
263       else
264         return -1;
265       p++;
266     }
267   return x;
268 }
269
270 static inline int id_white_p(int c)
271 {
272   return (c == ' ') || (c == '\t');
273 }
274
275 static const char *id_parse_list(struct pci_access *a, pci_file f, int *lino)
276 {
277   char line[MAX_LINE];
278   char *p;
279   int id1=0, id2=0, id3=0, id4=0;
280   int cat = -1;
281   int nest;
282   static const char parse_error[] = "Parse error";
283
284   *lino = 0;
285   while (pci_gets(f, line, sizeof(line)))
286     {
287       (*lino)++;
288       p = line;
289       while (*p && *p != '\n' && *p != '\r')
290         p++;
291       if (!*p && !pci_eof(f))
292         return "Line too long";
293       *p = 0;
294       if (p > line && (p[-1] == ' ' || p[-1] == '\t'))
295         *--p = 0;
296
297       p = line;
298       while (id_white_p(*p))
299         p++;
300       if (!*p || *p == '#')
301         continue;
302
303       p = line;
304       while (*p == '\t')
305         p++;
306       nest = p - line;
307
308       if (!nest)                                        /* Top-level entries */
309         {
310           if (p[0] == 'C' && p[1] == ' ')               /* Class block */
311             {
312               if ((id1 = id_hex(p+2, 2)) < 0 || !id_white_p(p[4]))
313                 return parse_error;
314               cat = ID_CLASS;
315               p += 5;
316             }
317           else if (p[0] == 'S' && p[1] == ' ')
318             {                                           /* Generic subsystem block */
319               if ((id1 = id_hex(p+2, 4)) < 0 || p[6])
320                 return parse_error;
321               if (!id_lookup(a, ID_VENDOR, id1, 0, 0, 0))
322                 return "Vendor does not exist";
323               cat = ID_GEN_SUBSYSTEM;
324               continue;
325             }
326           else if (p[0] >= 'A' && p[0] <= 'Z' && p[1] == ' ')
327             {                                           /* Unrecognized block (RFU) */
328               cat = ID_UNKNOWN;
329               continue;
330             }
331           else                                          /* Vendor ID */
332             {
333               if ((id1 = id_hex(p, 4)) < 0 || !id_white_p(p[4]))
334                 return parse_error;
335               cat = ID_VENDOR;
336               p += 5;
337             }
338           id2 = id3 = id4 = 0;
339         }
340       else if (cat == ID_UNKNOWN)                       /* Nested entries in RFU blocks are skipped */
341         continue;
342       else if (nest == 1)                               /* Nesting level 1 */
343         switch (cat)
344           {
345           case ID_VENDOR:
346           case ID_DEVICE:
347           case ID_SUBSYSTEM:
348             if ((id2 = id_hex(p, 4)) < 0 || !id_white_p(p[4]))
349               return parse_error;
350             p += 5;
351             cat = ID_DEVICE;
352             id3 = id4 = 0;
353             break;
354           case ID_GEN_SUBSYSTEM:
355             if ((id2 = id_hex(p, 4)) < 0 || !id_white_p(p[4]))
356               return parse_error;
357             p += 5;
358             id3 = id4 = 0;
359             break;
360           case ID_CLASS:
361           case ID_SUBCLASS:
362           case ID_PROGIF:
363             if ((id2 = id_hex(p, 2)) < 0 || !id_white_p(p[2]))
364               return parse_error;
365             p += 3;
366             cat = ID_SUBCLASS;
367             id3 = id4 = 0;
368             break;
369           default:
370             return parse_error;
371           }
372       else if (nest == 2)                               /* Nesting level 2 */
373         switch (cat)
374           {
375           case ID_DEVICE:
376           case ID_SUBSYSTEM:
377             if ((id3 = id_hex(p, 4)) < 0 || !id_white_p(p[4]) || (id4 = id_hex(p+5, 4)) < 0 || !id_white_p(p[9]))
378               return parse_error;
379             p += 10;
380             cat = ID_SUBSYSTEM;
381             break;
382           case ID_CLASS:
383           case ID_SUBCLASS:
384           case ID_PROGIF:
385             if ((id3 = id_hex(p, 2)) < 0 || !id_white_p(p[2]))
386               return parse_error;
387             p += 3;
388             cat = ID_PROGIF;
389             id4 = 0;
390             break;
391           default:
392             return parse_error;
393           }
394       else                                              /* Nesting level 3 or more */
395         return parse_error;
396       while (id_white_p(*p))
397         p++;
398       if (!*p)
399         return parse_error;
400       if (id_insert(a, cat, id1, id2, id3, id4, p))
401         return "Duplicate entry";
402     }
403   return NULL;
404 }
405
406 int
407 pci_load_name_list(struct pci_access *a)
408 {
409   pci_file f;
410   int lino;
411   const char *err;
412
413   pci_free_name_list(a);
414   a->hash_load_failed = 1;
415   if (!(f = pci_open(a)))
416     return 0;
417   err = id_parse_list(a, f, &lino);
418   PCI_ERROR(f, err);
419   pci_close(f);
420   if (err)
421     a->error("%s at %s, line %d\n", err, a->id_file_name, lino);
422   a->hash_load_failed = 0;
423   return 1;
424 }
425
426 void
427 pci_free_name_list(struct pci_access *a)
428 {
429   pci_mfree(a->id_hash);
430   a->id_hash = NULL;
431   while (a->current_id_bucket)
432     {
433       struct id_bucket *buck = a->current_id_bucket;
434       a->current_id_bucket = buck->next;
435       pci_mfree(buck);
436     }
437 }
438
439 static char *
440 id_lookup_subsys(struct pci_access *a, int iv, int id, int isv, int isd)
441 {
442   char *d = NULL;
443   if (iv > 0 && id > 0)                                         /* Per-device lookup */
444     d = id_lookup(a, ID_SUBSYSTEM, iv, id, isv, isd);
445   if (!d)                                                       /* Generic lookup */
446     d = id_lookup(a, ID_GEN_SUBSYSTEM, isv, isd, 0, 0);
447   if (!d && iv == isv && id == isd)                             /* Check for subsystem == device */
448     d = id_lookup(a, ID_DEVICE, iv, id, 0, 0);
449   return d;
450 }
451
452 static char *
453 format_name(char *buf, int size, int flags, char *name, char *num, char *unknown)
454 {
455   int res;
456   if ((flags & PCI_LOOKUP_NO_NUMBERS) && !name)
457     return NULL;
458   else if (flags & PCI_LOOKUP_NUMERIC)
459     res = snprintf(buf, size, "%s", num);
460   else if (!name)
461     res = snprintf(buf, size, ((flags & PCI_LOOKUP_MIXED) ? "%s [%s]" : "%s %s"), unknown, num);
462   else if (!(flags & PCI_LOOKUP_MIXED))
463     res = snprintf(buf, size, "%s", name);
464   else
465     res = snprintf(buf, size, "%s [%s]", name, num);
466   if (res < 0 || res >= size)
467     return "<pci_lookup_name: buffer too small>";
468   else
469     return buf;
470 }
471
472 static char *
473 format_name_pair(char *buf, int size, int flags, char *v, char *d, char *num)
474 {
475   int res;
476   if ((flags & PCI_LOOKUP_NO_NUMBERS) && (!v || !d))
477     return NULL;
478   if (flags & PCI_LOOKUP_NUMERIC)
479     res = snprintf(buf, size, "%s", num);
480   else if (flags & PCI_LOOKUP_MIXED)
481     {
482       if (v && d)
483         res = snprintf(buf, size, "%s %s [%s]", v, d, num);
484       else if (!v)
485         res = snprintf(buf, size, "Unknown device [%s]", num);
486       else /* v && !d */
487         res = snprintf(buf, size, "%s Unknown device [%s]", v, num);
488     }
489   else
490     {
491       if (v && d)
492         res = snprintf(buf, size, "%s %s", v, d);
493       else if (!v)
494         res = snprintf(buf, size, "Unknown device %s", num);
495       else /* v && !d */
496         res = snprintf(buf, size, "%s Unknown device %s", v, num+5);
497     }
498   if (res < 0 || res >= size)
499     return "<pci_lookup_name: buffer too small>";
500   else
501     return buf;
502 }
503
504 char *
505 pci_lookup_name(struct pci_access *a, char *buf, int size, int flags, ...)
506 {
507   va_list args;
508   char *v, *d, *cls, *pif;
509   int iv, id, isv, isd, icls, ipif;
510   char numbuf[16], pifbuf[32];
511
512   va_start(args, flags);
513
514   if (!(flags & PCI_LOOKUP_NO_NUMBERS))
515     {
516       if (a->numeric_ids > 1)
517         flags |= PCI_LOOKUP_MIXED;
518       else if (a->numeric_ids)
519         flags |= PCI_LOOKUP_NUMERIC;
520     }
521   if (flags & PCI_LOOKUP_MIXED)
522     flags &= ~PCI_LOOKUP_NUMERIC;
523
524   if (!a->id_hash && !(flags & PCI_LOOKUP_NUMERIC) && !a->hash_load_failed)
525     pci_load_name_list(a);
526
527   switch (flags & 0xffff)
528     {
529     case PCI_LOOKUP_VENDOR:
530       iv = va_arg(args, int);
531       sprintf(numbuf, "%04x", iv);
532       return format_name(buf, size, flags, id_lookup(a, ID_VENDOR, iv, 0, 0, 0), numbuf, "Unknown vendor");
533     case PCI_LOOKUP_DEVICE:
534       iv = va_arg(args, int);
535       id = va_arg(args, int);
536       sprintf(numbuf, "%04x", id);
537       return format_name(buf, size, flags, id_lookup(a, ID_DEVICE, iv, id, 0, 0), numbuf, "Unknown device");
538     case PCI_LOOKUP_VENDOR | PCI_LOOKUP_DEVICE:
539       iv = va_arg(args, int);
540       id = va_arg(args, int);
541       sprintf(numbuf, "%04x:%04x", iv, id);
542       v = id_lookup(a, ID_VENDOR, iv, 0, 0, 0);
543       d = id_lookup(a, ID_DEVICE, iv, id, 0, 0);
544       return format_name_pair(buf, size, flags, v, d, numbuf);
545     case PCI_LOOKUP_SUBSYSTEM | PCI_LOOKUP_VENDOR:
546       isv = va_arg(args, int);
547       sprintf(numbuf, "%04x", isv);
548       v = id_lookup(a, ID_VENDOR, isv, 0, 0, 0);
549       return format_name(buf, size, flags, v, numbuf, "Unknown vendor");
550     case PCI_LOOKUP_SUBSYSTEM | PCI_LOOKUP_DEVICE:
551       iv = va_arg(args, int);
552       id = va_arg(args, int);
553       isv = va_arg(args, int);
554       isd = va_arg(args, int);
555       sprintf(numbuf, "%04x", isd);
556       return format_name(buf, size, flags, id_lookup_subsys(a, iv, id, isv, isd), numbuf, "Unknown device");
557     case PCI_LOOKUP_VENDOR | PCI_LOOKUP_DEVICE | PCI_LOOKUP_SUBSYSTEM:
558       iv = va_arg(args, int);
559       id = va_arg(args, int);
560       isv = va_arg(args, int);
561       isd = va_arg(args, int);
562       v = id_lookup(a, ID_VENDOR, isv, 0, 0, 0);
563       d = id_lookup_subsys(a, iv, id, isv, isd);
564       sprintf(numbuf, "%04x:%04x", isv, isd);
565       return format_name_pair(buf, size, flags, v, d, numbuf);
566     case PCI_LOOKUP_CLASS:
567       icls = va_arg(args, int);
568       sprintf(numbuf, "%04x", icls);
569       cls = id_lookup(a, ID_SUBCLASS, icls >> 8, icls & 0xff, 0, 0);
570       if (!cls && (cls = id_lookup(a, ID_CLASS, icls >> 8, 0, 0, 0)))
571         {
572           if (!(flags & PCI_LOOKUP_NUMERIC)) /* Include full class number */
573             flags |= PCI_LOOKUP_MIXED;
574         }
575       return format_name(buf, size, flags, cls, numbuf, ((flags & PCI_LOOKUP_MIXED) ? "Unknown class" : "Class"));
576     case PCI_LOOKUP_PROGIF:
577       icls = va_arg(args, int);
578       ipif = va_arg(args, int);
579       sprintf(numbuf, "%02x", ipif);
580       pif = id_lookup(a, ID_PROGIF, icls >> 8, icls & 0xff, ipif, 0);
581       if (!pif && icls == 0x0101 && !(ipif & 0x70))
582         {
583           /* IDE controllers have complex prog-if semantics */
584           sprintf(pifbuf, "%s%s%s%s%s",
585                   (ipif & 0x80) ? " Master" : "",
586                   (ipif & 0x08) ? " SecP" : "",
587                   (ipif & 0x04) ? " SecO" : "",
588                   (ipif & 0x02) ? " PriP" : "",
589                   (ipif & 0x01) ? " PriO" : "");
590           pif = pifbuf;
591           if (*pif)
592             pif++;
593         }
594       return format_name(buf, size, flags, pif, numbuf, "ProgIf");
595     default:
596       return "<pci_lookup_name: invalid request>";
597     }
598 }
599
600 void pci_set_name_list_path(struct pci_access *a, char *name, int to_be_freed)
601 {
602   if (a->free_id_name)
603     free(a->id_file_name);
604   a->id_file_name = name;
605   a->free_id_name = to_be_freed;
606 }