]> mj.ucw.cz Git - pciutils.git/blob - lib/names.c
Names: Fixed a rare bug in loading of pci.ids
[pciutils.git] / lib / names.c
1 /*
2  *      The PCI Library -- ID to Name Translation
3  *
4  *      Copyright (c) 1997--2014 Martin Mares <mj@ucw.cz>
5  *
6  *      Can be freely distributed and used under the terms of the GNU GPL v2+.
7  *
8  *      SPDX-License-Identifier: GPL-2.0-or-later
9  */
10
11 #include <stdio.h>
12 #include <stdarg.h>
13 #include <string.h>
14
15 #include "internal.h"
16 #include "names.h"
17
18 static char *id_lookup(struct pci_access *a, int flags, int cat, int id1, int id2, int id3, int id4)
19 {
20   char *name;
21   int tried_hwdb = 0;
22
23   while (!(name = pci_id_lookup(a, flags, cat, id1, id2, id3, id4)))
24     {
25       if ((flags & PCI_LOOKUP_CACHE) && !a->id_cache_status)
26         {
27           if (pci_id_cache_load(a, flags))
28             continue;
29         }
30       if (!tried_hwdb && !(flags & (PCI_LOOKUP_SKIP_LOCAL | PCI_LOOKUP_NO_HWDB)))
31         {
32           tried_hwdb = 1;
33           if (name = pci_id_hwdb_lookup(a, cat, id1, id2, id3, id4))
34             {
35               pci_id_insert(a, cat, id1, id2, id3, id4, name, SRC_HWDB);
36               pci_mfree(name);
37               continue;
38             }
39         }
40       if (flags & PCI_LOOKUP_NETWORK)
41         {
42           if (name = pci_id_net_lookup(a, cat, id1, id2, id3, id4))
43             {
44               pci_id_insert(a, cat, id1, id2, id3, id4, name, SRC_NET);
45               pci_mfree(name);
46               pci_id_cache_dirty(a);
47             }
48           else
49             pci_id_insert(a, cat, id1, id2, id3, id4, "", SRC_NET);
50           /* We want to iterate the lookup to get the allocated ID entry from the hash */
51           continue;
52         }
53       return NULL;
54     }
55   return (name[0] ? name : NULL);
56 }
57
58 static char *
59 id_lookup_subsys(struct pci_access *a, int flags, int iv, int id, int isv, int isd)
60 {
61   char *d = NULL;
62   if (iv > 0 && id > 0)                                         /* Per-device lookup */
63     d = id_lookup(a, flags, ID_SUBSYSTEM, iv, id, isv, isd);
64   if (!d)                                                       /* Generic lookup */
65     d = id_lookup(a, flags, ID_GEN_SUBSYSTEM, isv, isd, 0, 0);
66   if (!d && iv == isv && id == isd)                             /* Check for subsystem == device */
67     d = id_lookup(a, flags, ID_DEVICE, iv, id, 0, 0);
68   return d;
69 }
70
71 static char *
72 format_name(char *buf, int size, int flags, char *name, char *num, char *unknown)
73 {
74   int res;
75   if ((flags & PCI_LOOKUP_NO_NUMBERS) && !name)
76     return NULL;
77   else if (flags & PCI_LOOKUP_NUMERIC)
78     res = snprintf(buf, size, "%s", num);
79   else if (!name)
80     res = snprintf(buf, size, ((flags & PCI_LOOKUP_MIXED) ? "%s [%s]" : "%s %s"), unknown, num);
81   else if (!(flags & PCI_LOOKUP_MIXED))
82     res = snprintf(buf, size, "%s", name);
83   else
84     res = snprintf(buf, size, "%s [%s]", name, num);
85   if (res >= size && size >= 4)
86     buf[size-2] = buf[size-3] = buf[size-4] = '.';
87   else if (res < 0 || res >= size)
88     return "<pci_lookup_name: buffer too small>";
89   return buf;
90 }
91
92 static char *
93 format_name_pair(char *buf, int size, int flags, char *v, char *d, char *num)
94 {
95   int res;
96   if ((flags & PCI_LOOKUP_NO_NUMBERS) && (!v || !d))
97     return NULL;
98   if (flags & PCI_LOOKUP_NUMERIC)
99     res = snprintf(buf, size, "%s", num);
100   else if (flags & PCI_LOOKUP_MIXED)
101     {
102       if (v && d)
103         res = snprintf(buf, size, "%s %s [%s]", v, d, num);
104       else if (!v)
105         res = snprintf(buf, size, "Device [%s]", num);
106       else /* v && !d */
107         res = snprintf(buf, size, "%s Device [%s]", v, num);
108     }
109   else
110     {
111       if (v && d)
112         res = snprintf(buf, size, "%s %s", v, d);
113       else if (!v)
114         res = snprintf(buf, size, "Device %s", num);
115       else /* v && !d */
116         res = snprintf(buf, size, "%s Device %s", v, num+5);
117     }
118   if (res >= size && size >= 4)
119     buf[size-2] = buf[size-3] = buf[size-4] = '.';
120   else if (res < 0 || res >= size)
121     return "<pci_lookup_name: buffer too small>";
122   return buf;
123 }
124
125 char *
126 pci_lookup_name(struct pci_access *a, char *buf, int size, int flags, ...)
127 {
128   va_list args;
129   char *v, *d, *cls, *pif;
130   int iv, id, isv, isd, icls, ipif;
131   char numbuf[16], pifbuf[32];
132
133   va_start(args, flags);
134
135   flags |= a->id_lookup_mode;
136   if (!(flags & PCI_LOOKUP_NO_NUMBERS))
137     {
138       if (a->numeric_ids > 1)
139         flags |= PCI_LOOKUP_MIXED;
140       else if (a->numeric_ids)
141         flags |= PCI_LOOKUP_NUMERIC;
142     }
143   if (flags & PCI_LOOKUP_MIXED)
144     flags &= ~PCI_LOOKUP_NUMERIC;
145
146   if (!a->id_load_attempted && !(flags & (PCI_LOOKUP_NUMERIC | PCI_LOOKUP_SKIP_LOCAL)))
147     pci_load_name_list(a);
148
149   switch (flags & 0xffff)
150     {
151     case PCI_LOOKUP_VENDOR:
152       iv = va_arg(args, int);
153       sprintf(numbuf, "%04x", iv);
154       va_end(args);
155       return format_name(buf, size, flags, id_lookup(a, flags, ID_VENDOR, iv, 0, 0, 0), numbuf, "Vendor");
156     case PCI_LOOKUP_DEVICE:
157       iv = va_arg(args, int);
158       id = va_arg(args, int);
159       sprintf(numbuf, "%04x", id);
160       va_end(args);
161       return format_name(buf, size, flags, id_lookup(a, flags, ID_DEVICE, iv, id, 0, 0), numbuf, "Device");
162     case PCI_LOOKUP_VENDOR | PCI_LOOKUP_DEVICE:
163       iv = va_arg(args, int);
164       id = va_arg(args, int);
165       sprintf(numbuf, "%04x:%04x", iv, id);
166       v = id_lookup(a, flags, ID_VENDOR, iv, 0, 0, 0);
167       d = id_lookup(a, flags, ID_DEVICE, iv, id, 0, 0);
168       va_end(args);
169       return format_name_pair(buf, size, flags, v, d, numbuf);
170     case PCI_LOOKUP_SUBSYSTEM | PCI_LOOKUP_VENDOR:
171       isv = va_arg(args, int);
172       sprintf(numbuf, "%04x", isv);
173       v = id_lookup(a, flags, ID_VENDOR, isv, 0, 0, 0);
174       va_end(args);
175       return format_name(buf, size, flags, v, numbuf, "Unknown vendor");
176     case PCI_LOOKUP_SUBSYSTEM | PCI_LOOKUP_DEVICE:
177       iv = va_arg(args, int);
178       id = va_arg(args, int);
179       isv = va_arg(args, int);
180       isd = va_arg(args, int);
181       sprintf(numbuf, "%04x", isd);
182       va_end(args);
183       return format_name(buf, size, flags, id_lookup_subsys(a, flags, iv, id, isv, isd), numbuf, "Device");
184     case PCI_LOOKUP_VENDOR | PCI_LOOKUP_DEVICE | PCI_LOOKUP_SUBSYSTEM:
185       iv = va_arg(args, int);
186       id = va_arg(args, int);
187       isv = va_arg(args, int);
188       isd = va_arg(args, int);
189       v = id_lookup(a, flags, ID_VENDOR, isv, 0, 0, 0);
190       d = id_lookup_subsys(a, flags, iv, id, isv, isd);
191       sprintf(numbuf, "%04x:%04x", isv, isd);
192       va_end(args);
193       return format_name_pair(buf, size, flags, v, d, numbuf);
194     case PCI_LOOKUP_CLASS:
195       icls = va_arg(args, int);
196       sprintf(numbuf, "%04x", icls);
197       cls = id_lookup(a, flags, ID_SUBCLASS, icls >> 8, icls & 0xff, 0, 0);
198       if (!cls && (cls = id_lookup(a, flags, ID_CLASS, icls >> 8, 0, 0, 0)))
199         {
200           if (!(flags & PCI_LOOKUP_NUMERIC)) /* Include full class number */
201             flags |= PCI_LOOKUP_MIXED;
202         }
203       va_end(args);
204       return format_name(buf, size, flags, cls, numbuf, "Class");
205     case PCI_LOOKUP_PROGIF:
206       icls = va_arg(args, int);
207       ipif = va_arg(args, int);
208       sprintf(numbuf, "%02x", ipif);
209       pif = id_lookup(a, flags, ID_PROGIF, icls >> 8, icls & 0xff, ipif, 0);
210       if (!pif && icls == 0x0101 && !(ipif & 0x70))
211         {
212           /* IDE controllers have complex prog-if semantics */
213           sprintf(pifbuf, "%s%s%s%s%s",
214                   (ipif & 0x80) ? " Master" : "",
215                   (ipif & 0x08) ? " SecP" : "",
216                   (ipif & 0x04) ? " SecO" : "",
217                   (ipif & 0x02) ? " PriP" : "",
218                   (ipif & 0x01) ? " PriO" : "");
219           pif = pifbuf;
220           if (*pif)
221             pif++;
222         }
223       va_end(args);
224       return format_name(buf, size, flags, pif, numbuf, "ProgIf");
225     default:
226       va_end(args);
227       return "<pci_lookup_name: invalid request>";
228     }
229 }