3 # make-pci-ids - Creates a file containing PCI IDs.
4 # It use the databases from
5 # https://github.com/pciutils/pciids/raw/master/pci.ids
6 # to create our file epan/dissectors/pci-ids.c
8 # Wireshark - Network traffic analyzer
10 # By Caleb Chiu <caleb.chiu@macnica.com>
13 # SPDX-License-Identifier: GPL-2.0-or-later
18 import urllib
.request
, urllib
.error
, urllib
.parse
20 OUTPUT_FILE
= "epan/pci-ids.c"
22 MIN_VENDOR_COUNT
= 2250 # 2261 on 2021-11-01
23 MIN_DEVICE_COUNT
= 33000 # 33724 on 2021-11-01
27 * Generated by tools/make-pci-ids.py
28 * By Caleb Chiu <caleb.chiu@macnica.com>
32 * SPDX-License-Identifier: GPL-2.0-or-later
55 pci_id_t const *ids_ptr;
62 static pci_vid_index_t const *get_vid_index(uint16_t vid)
64 uint32_t start_index = 0;
65 uint32_t end_index = 0;
68 end_index = sizeof(pci_vid_index)/sizeof(pci_vid_index[0]);
70 while(start_index != end_index)
72 if(end_index - start_index == 1)
74 if(pci_vid_index[start_index].vid == vid)
75 return &pci_vid_index[start_index];
80 idx = (start_index + end_index)/2;
82 if(pci_vid_index[idx].vid < vid)
85 if(pci_vid_index[idx].vid > vid)
88 return &pci_vid_index[idx];
96 const char *pci_id_str(uint16_t vid, uint16_t did, uint16_t svid, uint16_t ssid)
99 static const char *not_found = \"Not found\";
100 pci_vid_index_t const *index_ptr;
101 pci_id_t const *ids_ptr;
103 index_ptr = get_vid_index(vid);
105 if(index_ptr == NULL)
108 ids_ptr = index_ptr->ids_ptr;
109 for(i = 0; i < index_ptr->count; ids_ptr++, i++)
110 if(vid == ids_ptr->vid &&
111 did == ids_ptr->did &&
112 svid == ids_ptr->svid &&
113 ssid == ids_ptr->ssid)
114 return ids_ptr->name;
125 def exit_msg(msg
=None, status
=1):
127 sys
.stderr
.write(msg
+ '\n')
132 req_headers
= { 'User-Agent': 'Wireshark make-pci-ids' }
133 req
= urllib
.request
.Request('https://github.com/pciutils/pciids/raw/master/pci.ids', headers
=req_headers
)
134 response
= urllib
.request
.urlopen(req
)
135 lines
= response
.read().decode('UTF-8', 'replace').splitlines()
140 * pci-ids.c is based on the pci.ids of The PCI ID Repository at
141 * https://pci-ids.ucw.cz/, fetched indirectly via
142 * https://github.com/pciutils/pciids
151 line
= line
.strip('\n')
155 line
= line
.replace('#', ' ', 1)
157 line
= line
.replace("GNU General Public License", "GPL")
162 out_lines
+= line
+ '\n'
164 out_lines
+= CODE_PREFIX
166 line
= line
.replace("\\","\\\\")
167 line
= line
.replace("\"","\\\"")
168 line
= line
.replace("?","?-")
169 tabs
= len(line
) - len(line
.lstrip('\t'))
172 words
= line
.split(" ", 1)
175 if len(words
[0]) != 4:
177 if all(c
in string
.hexdigits
for c
in words
[0]):
178 hex_int
= int(words
[0], 16)
180 out_lines
+= "}; /* pci_vid_%04X[] */\n\n" % (vid
)
181 count_list
.append(entries
)
187 out_lines
+= "static pci_id_t const pci_vid_%04X[] = {\n" % (vid
)
188 out_lines
+= "{0x%04X, 0xFFFF, 0xFFFF, 0xFFFF, \"%s(0x%04X)\"},\n" % (vid
, words
[1].strip(), vid
)
193 line
= line
.strip('\t')
194 words
= line
.split(" ", 1)
197 if len(words
[0]) != 4:
199 if all(c
in string
.hexdigits
for c
in words
[0]):
200 hex_int
= int(words
[0], 16)
204 out_lines
+= "{0x%04X, 0x%04X, 0xFFFF, 0xFFFF, \"%s(0x%04X)\"},\n" % (vid
, did
, words
[1].strip(), did
)
209 line
= line
.strip('\t')
210 words
= line
.split(" ", 2)
211 if len(words
[0]) != 4:
213 if all(c
in string
.hexdigits
for c
in words
[0]):
214 hex_int
= int(words
[0], 16)
217 if all(c
in string
.hexdigits
for c
in words
[1]):
218 hex_int
= int(words
[1], 16)
221 out_lines
+= "{0x%04X, 0x%04X, 0x%04X, 0x%04X, \"%s(0x%04X-0x%04X)\"},\n" % (vid
, did
, svid
, ssid
, words
[2].strip(), svid
, ssid
)
227 out_lines
+= "}; /* pci_vid_%04X[] */\n" % (vid
)
228 count_list
.append(entries
)
230 out_lines
+= "\nstatic pci_vid_index_t const pci_vid_index[] = {\n"
232 vendor_count
= len(id_list
)
234 for i
in range(vendor_count
):
235 out_lines
+= "{0x%04X, %d, pci_vid_%04X },\n" % (id_list
[i
], count_list
[i
], id_list
[i
])
236 device_count
+= count_list
[i
]
238 out_lines
+= "}; /* We have %d VIDs */\n" % (vendor_count
)
240 out_lines
+= CODE_POSTFIX
242 if vendor_count
< MIN_VENDOR_COUNT
:
243 exit_msg(f
'Too few vendors. Wanted {MIN_VENDOR_COUNT}, got {vendor_count}.')
245 if device_count
< MIN_DEVICE_COUNT
:
246 exit_msg(f
'Too few devices. Wanted {MIN_DEVICE_COUNT}, got {device_count}.')
248 with
open(OUTPUT_FILE
, "w", encoding
="utf-8") as pci_ids_f
:
249 pci_ids_f
.write(out_lines
)
251 if __name__
== '__main__':