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
40 #include "wsutil/array.h"
58 pci_id_t const *ids_ptr;
65 static int vid_search(const void *key, const void *tbl_entry)
67 return (int)*(const uint16_t *)key -
68 (int)((const pci_vid_index_t *)tbl_entry)->vid;
71 const char *pci_id_str(uint16_t vid, uint16_t did, uint16_t svid, uint16_t ssid)
74 static const char *not_found = \"Not found\";
75 pci_vid_index_t const *index_ptr;
76 pci_id_t const *ids_ptr;
78 index_ptr = bsearch(&vid, pci_vid_index, array_length(pci_vid_index), sizeof pci_vid_index[0], vid_search);
83 ids_ptr = index_ptr->ids_ptr;
84 for(i = 0; i < index_ptr->count; ids_ptr++, i++)
85 if(vid == ids_ptr->vid &&
86 did == ids_ptr->did &&
87 svid == ids_ptr->svid &&
88 ssid == ids_ptr->ssid)
100 def exit_msg(msg
=None, status
=1):
102 sys
.stderr
.write(msg
+ '\n')
107 req_headers
= { 'User-Agent': 'Wireshark make-pci-ids' }
108 req
= urllib
.request
.Request('https://github.com/pciutils/pciids/raw/master/pci.ids', headers
=req_headers
)
109 response
= urllib
.request
.urlopen(req
)
110 lines
= response
.read().decode('UTF-8', 'replace').splitlines()
115 * pci-ids.c is based on the pci.ids of The PCI ID Repository at
116 * https://pci-ids.ucw.cz/, fetched indirectly via
117 * https://github.com/pciutils/pciids
126 line
= line
.strip('\n')
130 line
= line
.replace('#', ' ', 1)
132 line
= line
.replace("GNU General Public License", "GPL")
137 out_lines
+= line
+ '\n'
139 out_lines
+= CODE_PREFIX
141 line
= line
.replace("\\","\\\\")
142 line
= line
.replace("\"","\\\"")
143 line
= line
.replace("?","?-")
144 tabs
= len(line
) - len(line
.lstrip('\t'))
147 words
= line
.split(" ", 1)
150 if len(words
[0]) != 4:
152 if all(c
in string
.hexdigits
for c
in words
[0]):
153 hex_int
= int(words
[0], 16)
155 out_lines
+= "}; /* pci_vid_%04X[] */\n\n" % (vid
)
156 count_list
.append(entries
)
162 out_lines
+= "static pci_id_t const pci_vid_%04X[] = {\n" % (vid
)
163 out_lines
+= "{0x%04X, 0xFFFF, 0xFFFF, 0xFFFF, \"%s(0x%04X)\"},\n" % (vid
, words
[1].strip(), vid
)
168 line
= line
.strip('\t')
169 words
= line
.split(" ", 1)
172 if len(words
[0]) != 4:
174 if all(c
in string
.hexdigits
for c
in words
[0]):
175 hex_int
= int(words
[0], 16)
179 out_lines
+= "{0x%04X, 0x%04X, 0xFFFF, 0xFFFF, \"%s(0x%04X)\"},\n" % (vid
, did
, words
[1].strip(), did
)
184 line
= line
.strip('\t')
185 words
= line
.split(" ", 2)
186 if len(words
[0]) != 4:
188 if all(c
in string
.hexdigits
for c
in words
[0]):
189 hex_int
= int(words
[0], 16)
192 if all(c
in string
.hexdigits
for c
in words
[1]):
193 hex_int
= int(words
[1], 16)
196 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
)
202 out_lines
+= "}; /* pci_vid_%04X[] */\n" % (vid
)
203 count_list
.append(entries
)
205 out_lines
+= "\nstatic pci_vid_index_t const pci_vid_index[] = {\n"
207 vendor_count
= len(id_list
)
209 for i
in range(vendor_count
):
210 out_lines
+= "{0x%04X, %d, pci_vid_%04X },\n" % (id_list
[i
], count_list
[i
], id_list
[i
])
211 device_count
+= count_list
[i
]
213 out_lines
+= "}; /* We have %d VIDs */\n" % (vendor_count
)
215 out_lines
+= CODE_POSTFIX
217 if vendor_count
< MIN_VENDOR_COUNT
:
218 exit_msg(f
'Too few vendors. Wanted {MIN_VENDOR_COUNT}, got {vendor_count}.')
220 if device_count
< MIN_DEVICE_COUNT
:
221 exit_msg(f
'Too few devices. Wanted {MIN_DEVICE_COUNT}, got {device_count}.')
223 with
open(OUTPUT_FILE
, "w", encoding
="utf-8") as pci_ids_f
:
224 pci_ids_f
.write(out_lines
)
226 if __name__
== '__main__':