]> mj.ucw.cz Git - pciutils.git/blob - lib/names.c
c7b1abe94c83d7c963517ec77a698f76a479d956
[pciutils.git] / lib / names.c
1 /*
2  *      The PCI Library -- ID to Name Translation
3  *
4  *      Copyright (c) 1997--2008 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, "rb");
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, "rb");
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   char name[256], dnsname[256], txt[256];
138   byte answer[4096];
139   const byte *data;
140   int res, i, j, dlen;
141   ns_msg m;
142   ns_rr rr;
143
144   switch (cat)
145     {
146     case ID_VENDOR:
147       sprintf(name, "%04x", id1);
148       break;
149     case ID_DEVICE:
150       sprintf(name, "%04x.%04x", id2, id1);
151       break;
152     case ID_SUBSYSTEM:
153       sprintf(name, "%04x.%04x.%04x.%04x", id4, id3, id2, id1);
154       break;
155     case ID_GEN_SUBSYSTEM:
156       sprintf(name, "%04x.%04x.s", id2, id1);
157       break;
158     case ID_CLASS:
159       sprintf(name, "%02x.c", id1);
160       break;
161     case ID_SUBCLASS:
162       sprintf(name, "%02x.%02x.c", id2, id1);
163       break;
164     case ID_PROGIF:
165       sprintf(name, "%02x.%02x.%02x.c", id3, id2, id1);
166       break;
167     default:
168       return NULL;
169     }
170   sprintf(dnsname, "%s.%s", name, a->id_domain);
171
172   a->debug("Resolving %s\n", dnsname);
173   res_init();
174   res = res_query(dnsname, ns_c_in, ns_t_txt, answer, sizeof(answer));
175   if (res < 0)
176     {
177       a->debug("\tfailed, h_errno=%d\n", _res.res_h_errno);
178       return NULL;
179     }
180   if (ns_initparse(answer, res, &m) < 0)
181     {
182       a->debug("\tinitparse failed\n");
183       return NULL;
184     }
185   for (i=0; ns_parserr(&m, ns_s_an, i, &rr) >= 0; i++)
186     {
187       a->debug("\tanswer %d (class %d, type %d)\n", i, ns_rr_class(rr), ns_rr_type(rr));
188       if (ns_rr_class(rr) != ns_c_in || ns_rr_type(rr) != ns_t_txt)
189         continue;
190       data = ns_rr_rdata(rr);
191       dlen = ns_rr_rdlen(rr);
192       j = 0;
193       while (j < dlen && j+1+data[j] <= dlen)
194         {
195           memcpy(txt, &data[j+1], data[j]);
196           txt[data[j]] = 0;
197           j += 1+data[j];
198           a->debug("\t\t%s\n", txt);
199           if (txt[0] == 'i' && txt[1] == '=')
200             return strdup(txt+2);       /* FIXME */
201         }
202     }
203
204   return NULL;
205 }
206
207 static int id_insert(struct pci_access *a, int cat, int id1, int id2, int id3, int id4, char *text)
208 {
209   u32 id12 = id_pair(id1, id2);
210   u32 id34 = id_pair(id3, id4);
211   unsigned int h = id_hash(cat, id12, id34);
212   struct id_entry *n = a->id_hash ? a->id_hash[h] : NULL;
213   int len = strlen(text);
214
215   while (n && (n->id12 != id12 || n->id34 != id34 || n->cat != cat))
216     n = n->next;
217   if (n)
218     return 1;
219   n = id_alloc(a, sizeof(struct id_entry) + len);
220   n->id12 = id12;
221   n->id34 = id34;
222   n->cat = cat;
223   memcpy(n->name, text, len+1);
224   n->next = a->id_hash[h];
225   a->id_hash[h] = n;
226   return 0;
227 }
228
229 static char *id_lookup(struct pci_access *a, int cat, int id1, int id2, int id3, int id4)
230 {
231   struct id_entry *n;
232   u32 id12 = id_pair(id1, id2);
233   u32 id34 = id_pair(id3, id4);
234   char *name;
235
236   if (a->id_hash)
237     {
238       n = a->id_hash[id_hash(cat, id12, id34)];
239       while (n && (n->id12 != id12 || n->id34 != id34 || n->cat != cat))
240         n = n->next;
241       if (n)
242         return n->name;
243     }
244   if (name = id_net_lookup(a, cat, id1, id2, id3, id4))
245     {
246       id_insert(a, cat, id1, id2, id3, id4, name);
247       return name;
248     }
249   return NULL;
250 }
251
252 static int id_hex(char *p, int cnt)
253 {
254   int x = 0;
255   while (cnt--)
256     {
257       x <<= 4;
258       if (*p >= '0' && *p <= '9')
259         x += (*p - '0');
260       else if (*p >= 'a' && *p <= 'f')
261         x += (*p - 'a' + 10);
262       else if (*p >= 'A' && *p <= 'F')
263         x += (*p - 'A' + 10);
264       else
265         return -1;
266       p++;
267     }
268   return x;
269 }
270
271 static inline int id_white_p(int c)
272 {
273   return (c == ' ') || (c == '\t');
274 }
275
276 static const char *id_parse_list(struct pci_access *a, pci_file f, int *lino)
277 {
278   char line[MAX_LINE];
279   char *p;
280   int id1=0, id2=0, id3=0, id4=0;
281   int cat = -1;
282   int nest;
283   static const char parse_error[] = "Parse error";
284
285   *lino = 0;
286   while (pci_gets(f, line, sizeof(line)))
287     {
288       (*lino)++;
289       p = line;
290       while (*p && *p != '\n' && *p != '\r')
291         p++;
292       if (!*p && !pci_eof(f))
293         return "Line too long";
294       *p = 0;
295       if (p > line && (p[-1] == ' ' || p[-1] == '\t'))
296         *--p = 0;
297
298       p = line;
299       while (id_white_p(*p))
300         p++;
301       if (!*p || *p == '#')
302         continue;
303
304       p = line;
305       while (*p == '\t')
306         p++;
307       nest = p - line;
308
309       if (!nest)                                        /* Top-level entries */
310         {
311           if (p[0] == 'C' && p[1] == ' ')               /* Class block */
312             {
313               if ((id1 = id_hex(p+2, 2)) < 0 || !id_white_p(p[4]))
314                 return parse_error;
315               cat = ID_CLASS;
316               p += 5;
317             }
318           else if (p[0] == 'S' && p[1] == ' ')
319             {                                           /* Generic subsystem block */
320               if ((id1 = id_hex(p+2, 4)) < 0 || p[6])
321                 return parse_error;
322               if (!id_lookup(a, ID_VENDOR, id1, 0, 0, 0))
323                 return "Vendor does not exist";
324               cat = ID_GEN_SUBSYSTEM;
325               continue;
326             }
327           else if (p[0] >= 'A' && p[0] <= 'Z' && p[1] == ' ')
328             {                                           /* Unrecognized block (RFU) */
329               cat = ID_UNKNOWN;
330               continue;
331             }
332           else                                          /* Vendor ID */
333             {
334               if ((id1 = id_hex(p, 4)) < 0 || !id_white_p(p[4]))
335                 return parse_error;
336               cat = ID_VENDOR;
337               p += 5;
338             }
339           id2 = id3 = id4 = 0;
340         }
341       else if (cat == ID_UNKNOWN)                       /* Nested entries in RFU blocks are skipped */
342         continue;
343       else if (nest == 1)                               /* Nesting level 1 */
344         switch (cat)
345           {
346           case ID_VENDOR:
347           case ID_DEVICE:
348           case ID_SUBSYSTEM:
349             if ((id2 = id_hex(p, 4)) < 0 || !id_white_p(p[4]))
350               return parse_error;
351             p += 5;
352             cat = ID_DEVICE;
353             id3 = id4 = 0;
354             break;
355           case ID_GEN_SUBSYSTEM:
356             if ((id2 = id_hex(p, 4)) < 0 || !id_white_p(p[4]))
357               return parse_error;
358             p += 5;
359             id3 = id4 = 0;
360             break;
361           case ID_CLASS:
362           case ID_SUBCLASS:
363           case ID_PROGIF:
364             if ((id2 = id_hex(p, 2)) < 0 || !id_white_p(p[2]))
365               return parse_error;
366             p += 3;
367             cat = ID_SUBCLASS;
368             id3 = id4 = 0;
369             break;
370           default:
371             return parse_error;
372           }
373       else if (nest == 2)                               /* Nesting level 2 */
374         switch (cat)
375           {
376           case ID_DEVICE:
377           case ID_SUBSYSTEM:
378             if ((id3 = id_hex(p, 4)) < 0 || !id_white_p(p[4]) || (id4 = id_hex(p+5, 4)) < 0 || !id_white_p(p[9]))
379               return parse_error;
380             p += 10;
381             cat = ID_SUBSYSTEM;
382             break;
383           case ID_CLASS:
384           case ID_SUBCLASS:
385           case ID_PROGIF:
386             if ((id3 = id_hex(p, 2)) < 0 || !id_white_p(p[2]))
387               return parse_error;
388             p += 3;
389             cat = ID_PROGIF;
390             id4 = 0;
391             break;
392           default:
393             return parse_error;
394           }
395       else                                              /* Nesting level 3 or more */
396         return parse_error;
397       while (id_white_p(*p))
398         p++;
399       if (!*p)
400         return parse_error;
401       if (id_insert(a, cat, id1, id2, id3, id4, p))
402         return "Duplicate entry";
403     }
404   return NULL;
405 }
406
407 int
408 pci_load_name_list(struct pci_access *a)
409 {
410   pci_file f;
411   int lino;
412   const char *err;
413
414   pci_free_name_list(a);
415   a->hash_load_failed = 1;
416   if (!(f = pci_open(a)))
417     return 0;
418   err = id_parse_list(a, f, &lino);
419   PCI_ERROR(f, err);
420   pci_close(f);
421   if (err)
422     a->error("%s at %s, line %d\n", err, a->id_file_name, lino);
423   a->hash_load_failed = 0;
424   return 1;
425 }
426
427 void
428 pci_free_name_list(struct pci_access *a)
429 {
430   pci_mfree(a->id_hash);
431   a->id_hash = NULL;
432   while (a->current_id_bucket)
433     {
434       struct id_bucket *buck = a->current_id_bucket;
435       a->current_id_bucket = buck->next;
436       pci_mfree(buck);
437     }
438 }
439
440 static char *
441 id_lookup_subsys(struct pci_access *a, int iv, int id, int isv, int isd)
442 {
443   char *d = NULL;
444   if (iv > 0 && id > 0)                                         /* Per-device lookup */
445     d = id_lookup(a, ID_SUBSYSTEM, iv, id, isv, isd);
446   if (!d)                                                       /* Generic lookup */
447     d = id_lookup(a, ID_GEN_SUBSYSTEM, isv, isd, 0, 0);
448   if (!d && iv == isv && id == isd)                             /* Check for subsystem == device */
449     d = id_lookup(a, ID_DEVICE, iv, id, 0, 0);
450   return d;
451 }
452
453 static char *
454 format_name(char *buf, int size, int flags, char *name, char *num, char *unknown)
455 {
456   int res;
457   if ((flags & PCI_LOOKUP_NO_NUMBERS) && !name)
458     return NULL;
459   else if (flags & PCI_LOOKUP_NUMERIC)
460     res = snprintf(buf, size, "%s", num);
461   else if (!name)
462     res = snprintf(buf, size, ((flags & PCI_LOOKUP_MIXED) ? "%s [%s]" : "%s %s"), unknown, num);
463   else if (!(flags & PCI_LOOKUP_MIXED))
464     res = snprintf(buf, size, "%s", name);
465   else
466     res = snprintf(buf, size, "%s [%s]", name, num);
467   if (res < 0 || res >= size)
468     return "<pci_lookup_name: buffer too small>";
469   else
470     return buf;
471 }
472
473 static char *
474 format_name_pair(char *buf, int size, int flags, char *v, char *d, char *num)
475 {
476   int res;
477   if ((flags & PCI_LOOKUP_NO_NUMBERS) && (!v || !d))
478     return NULL;
479   if (flags & PCI_LOOKUP_NUMERIC)
480     res = snprintf(buf, size, "%s", num);
481   else if (flags & PCI_LOOKUP_MIXED)
482     {
483       if (v && d)
484         res = snprintf(buf, size, "%s %s [%s]", v, d, num);
485       else if (!v)
486         res = snprintf(buf, size, "Unknown device [%s]", num);
487       else /* v && !d */
488         res = snprintf(buf, size, "%s Unknown device [%s]", v, num);
489     }
490   else
491     {
492       if (v && d)
493         res = snprintf(buf, size, "%s %s", v, d);
494       else if (!v)
495         res = snprintf(buf, size, "Unknown device %s", num);
496       else /* v && !d */
497         res = snprintf(buf, size, "%s Unknown device %s", v, num+5);
498     }
499   if (res < 0 || res >= size)
500     return "<pci_lookup_name: buffer too small>";
501   else
502     return buf;
503 }
504
505 char *
506 pci_lookup_name(struct pci_access *a, char *buf, int size, int flags, ...)
507 {
508   va_list args;
509   char *v, *d, *cls, *pif;
510   int iv, id, isv, isd, icls, ipif;
511   char numbuf[16], pifbuf[32];
512
513   va_start(args, flags);
514
515   if (!(flags & PCI_LOOKUP_NO_NUMBERS))
516     {
517       if (a->numeric_ids > 1)
518         flags |= PCI_LOOKUP_MIXED;
519       else if (a->numeric_ids)
520         flags |= PCI_LOOKUP_NUMERIC;
521     }
522   if (flags & PCI_LOOKUP_MIXED)
523     flags &= ~PCI_LOOKUP_NUMERIC;
524
525   if (!a->id_hash && !(flags & PCI_LOOKUP_NUMERIC) && !a->hash_load_failed)
526     pci_load_name_list(a);
527
528   switch (flags & 0xffff)
529     {
530     case PCI_LOOKUP_VENDOR:
531       iv = va_arg(args, int);
532       sprintf(numbuf, "%04x", iv);
533       return format_name(buf, size, flags, id_lookup(a, ID_VENDOR, iv, 0, 0, 0), numbuf, "Unknown vendor");
534     case PCI_LOOKUP_DEVICE:
535       iv = va_arg(args, int);
536       id = va_arg(args, int);
537       sprintf(numbuf, "%04x", id);
538       return format_name(buf, size, flags, id_lookup(a, ID_DEVICE, iv, id, 0, 0), numbuf, "Unknown device");
539     case PCI_LOOKUP_VENDOR | PCI_LOOKUP_DEVICE:
540       iv = va_arg(args, int);
541       id = va_arg(args, int);
542       sprintf(numbuf, "%04x:%04x", iv, id);
543       v = id_lookup(a, ID_VENDOR, iv, 0, 0, 0);
544       d = id_lookup(a, ID_DEVICE, iv, id, 0, 0);
545       return format_name_pair(buf, size, flags, v, d, numbuf);
546     case PCI_LOOKUP_SUBSYSTEM | PCI_LOOKUP_VENDOR:
547       isv = va_arg(args, int);
548       sprintf(numbuf, "%04x", isv);
549       v = id_lookup(a, ID_VENDOR, isv, 0, 0, 0);
550       return format_name(buf, size, flags, v, numbuf, "Unknown vendor");
551     case PCI_LOOKUP_SUBSYSTEM | PCI_LOOKUP_DEVICE:
552       iv = va_arg(args, int);
553       id = va_arg(args, int);
554       isv = va_arg(args, int);
555       isd = va_arg(args, int);
556       sprintf(numbuf, "%04x", isd);
557       return format_name(buf, size, flags, id_lookup_subsys(a, iv, id, isv, isd), numbuf, "Unknown device");
558     case PCI_LOOKUP_VENDOR | PCI_LOOKUP_DEVICE | PCI_LOOKUP_SUBSYSTEM:
559       iv = va_arg(args, int);
560       id = va_arg(args, int);
561       isv = va_arg(args, int);
562       isd = va_arg(args, int);
563       v = id_lookup(a, ID_VENDOR, isv, 0, 0, 0);
564       d = id_lookup_subsys(a, iv, id, isv, isd);
565       sprintf(numbuf, "%04x:%04x", isv, isd);
566       return format_name_pair(buf, size, flags, v, d, numbuf);
567     case PCI_LOOKUP_CLASS:
568       icls = va_arg(args, int);
569       sprintf(numbuf, "%04x", icls);
570       cls = id_lookup(a, ID_SUBCLASS, icls >> 8, icls & 0xff, 0, 0);
571       if (!cls && (cls = id_lookup(a, ID_CLASS, icls >> 8, 0, 0, 0)))
572         {
573           if (!(flags & PCI_LOOKUP_NUMERIC)) /* Include full class number */
574             flags |= PCI_LOOKUP_MIXED;
575         }
576       return format_name(buf, size, flags, cls, numbuf, ((flags & PCI_LOOKUP_MIXED) ? "Unknown class" : "Class"));
577     case PCI_LOOKUP_PROGIF:
578       icls = va_arg(args, int);
579       ipif = va_arg(args, int);
580       sprintf(numbuf, "%02x", ipif);
581       pif = id_lookup(a, ID_PROGIF, icls >> 8, icls & 0xff, ipif, 0);
582       if (!pif && icls == 0x0101 && !(ipif & 0x70))
583         {
584           /* IDE controllers have complex prog-if semantics */
585           sprintf(pifbuf, "%s%s%s%s%s",
586                   (ipif & 0x80) ? " Master" : "",
587                   (ipif & 0x08) ? " SecP" : "",
588                   (ipif & 0x04) ? " SecO" : "",
589                   (ipif & 0x02) ? " PriP" : "",
590                   (ipif & 0x01) ? " PriO" : "");
591           pif = pifbuf;
592           if (*pif)
593             pif++;
594         }
595       return format_name(buf, size, flags, pif, numbuf, "ProgIf");
596     default:
597       return "<pci_lookup_name: invalid request>";
598     }
599 }
600
601 void pci_set_name_list_path(struct pci_access *a, char *name, int to_be_freed)
602 {
603   if (a->free_id_name)
604     free(a->id_file_name);
605   a->id_file_name = name;
606   a->free_id_name = to_be_freed;
607 }