]>
git.ipfire.org Git - thirdparty/systemd.git/blob - hwdb/ids_parser.py
5 from pyparsing
import (Word
, White
, Literal
, Regex
,
7 ZeroOrMore
, OneOrMore
, Combine
, Optional
, Suppress
,
8 stringEnd
, pythonStyleComment
)
10 EOL
= LineEnd().suppress()
11 NUM1
= Word('0123456789abcdefABCDEF', exact
=1)
12 NUM2
= Word('0123456789abcdefABCDEF', exact
=2)
13 NUM3
= Word('0123456789abcdefABCDEF', exact
=3)
14 NUM4
= Word('0123456789abcdefABCDEF', exact
=4)
15 NUM6
= Word('0123456789abcdefABCDEF', exact
=6)
16 TAB
= White('\t', exact
=1).suppress()
17 COMMENTLINE
= pythonStyleComment
+ EOL
19 text_eol
= lambda name
: Regex(r
'[^\n]+')(name
) + EOL
22 klass_line
= Literal('C ').suppress() + NUM2('klass') + text_eol('text')
23 subclass_line
= TAB
+ NUM2('subclass') + text_eol('text')
24 protocol_line
= TAB
+ TAB
+ NUM2('protocol') + text_eol('name')
25 subclass
= (subclass_line('SUBCLASS') -
26 ZeroOrMore(protocol_line('PROTOCOLS*')
27 ^ COMMENTLINE
.suppress()))
28 klass
= (klass_line('KLASS') -
29 ZeroOrMore(subclass('SUBCLASSES*')
30 ^ COMMENTLINE
.suppress()))
33 def usb_ids_grammar():
34 vendor_line
= NUM4('vendor') + text_eol('text')
35 device_line
= TAB
+ NUM4('device') + text_eol('text')
36 vendor
= (vendor_line('VENDOR') +
37 ZeroOrMore(device_line('VENDOR_DEV*') ^ COMMENTLINE
.suppress()))
39 klass
= klass_grammar()
41 other_line
= (Literal('AT ') ^
Literal('HID ') ^
Literal('R ')
42 ^
Literal('PHY ') ^
Literal('BIAS ') ^
Literal('HUT ')
43 ^
Literal('L ') ^
Literal('VT ') ^
Literal('HCC ')) + text_eol('text')
44 other_group
= (other_line
- ZeroOrMore(TAB
+ text_eol('text')))
46 commentgroup
= OneOrMore(COMMENTLINE
).suppress() ^ EMPTYLINE
.suppress()
47 grammar
= OneOrMore(vendor('VENDORS*') ^
klass('CLASSES*')
48 ^ other_group
.suppress() ^ commentgroup
) + stringEnd()
50 grammar
.parseWithTabs()
53 def pci_ids_grammar():
54 vendor_line
= NUM4('vendor') + text_eol('text')
55 device_line
= TAB
+ NUM4('device') + text_eol('text')
56 subvendor_line
= TAB
+ TAB
+ NUM4('a') + White(' ') + NUM4('b') + text_eol('name')
58 device
= (device_line('DEVICE') +
59 ZeroOrMore(subvendor_line('SUBVENDORS*') ^ COMMENTLINE
.suppress()))
60 vendor
= (vendor_line('VENDOR') +
61 ZeroOrMore(device('DEVICES*') ^ COMMENTLINE
.suppress()))
63 klass
= klass_grammar()
65 commentgroup
= OneOrMore(COMMENTLINE
).suppress() ^ EMPTYLINE
.suppress()
66 grammar
= OneOrMore(vendor('VENDORS*') ^
klass('CLASSES*')
67 ^ commentgroup
) + stringEnd()
69 grammar
.parseWithTabs()
72 def sdio_ids_grammar():
73 vendor_line
= NUM4('vendor') + text_eol('text')
74 device_line
= TAB
+ NUM4('device') + text_eol('text')
75 vendor
= (vendor_line('VENDOR') +
76 ZeroOrMore(device_line('DEVICES*') ^ COMMENTLINE
.suppress()))
78 klass
= klass_grammar()
80 commentgroup
= OneOrMore(COMMENTLINE
).suppress() ^ EMPTYLINE
.suppress()
81 grammar
= OneOrMore(vendor('VENDORS*') ^
klass('CLASSES*') ^ commentgroup
) + stringEnd()
83 grammar
.parseWithTabs()
86 def oui_grammar(type):
87 prefix_line
= (Combine(NUM2
- Suppress('-') - NUM2
- Suppress('-') - NUM2
)('prefix')
88 - Literal('(hex)') - text_eol('text'))
90 vendor_line
= (NUM3('start') - '000-' - NUM3('end') - 'FFF'
91 - Literal('(base 16)') - text_eol('text2'))
92 elif type == 'medium':
93 vendor_line
= (NUM1('start') - '00000-' - NUM1('end') - 'FFFFF'
94 - Literal('(base 16)') - text_eol('text2'))
96 assert type == 'large'
97 vendor_line
= (NUM6('start')
98 - Literal('(base 16)') - text_eol('text2'))
100 extra_line
= TAB
- TAB
- TAB
- TAB
- SkipTo(EOL
)
101 vendor
= prefix_line
+ vendor_line
+ ZeroOrMore(extra_line
) + Optional(EMPTYLINE
)
103 grammar
= (Literal('OUI') + text_eol('header')
104 + text_eol('header') + text_eol('header') + EMPTYLINE
105 + OneOrMore(vendor('VENDORS*')) + stringEnd())
107 grammar
.parseWithTabs()
111 def header(file, *sources
):
113 # This file is part of systemd.
115 # Data imported from:{}{}'''.format(' ' if len(sources
) == 1 else '\n# ',
116 '\n# '.join(sources
)),
119 def add_item(items
, key
, value
):
121 print(f
'Ignoring duplicate entry: {key} = "{items[key]}", "{value}"')
125 def usb_vendor_model(p
):
128 for vendor_group
in p
.VENDORS
:
129 vendor
= vendor_group
.VENDOR
.vendor
.upper()
130 text
= vendor_group
.VENDOR
.text
.strip()
131 add_item(items
, (vendor
,), text
)
133 for vendor_dev
in vendor_group
.VENDOR_DEV
:
134 device
= vendor_dev
.device
.upper()
135 text
= vendor_dev
.text
.strip()
136 add_item(items
, (vendor
, device
), text
)
138 with
open('20-usb-vendor-model.hwdb', 'wt') as out
:
139 header(out
, 'http://www.linux-usb.org/usb.ids')
141 for key
in sorted(items
):
143 p
, n
= 'usb:v{}*', 'VENDOR'
145 p
, n
= 'usb:v{}p{}*', 'MODEL',
146 print('', p
.format(*key
),
147 f
' ID_{n}_FROM_DATABASE={items[key]}', sep
='\n', file=out
)
149 print(f
'Wrote {out.name}')
154 for klass_group
in p
.CLASSES
:
155 klass
= klass_group
.KLASS
.klass
.upper()
156 text
= klass_group
.KLASS
.text
.strip()
158 if klass
!= '00' and not re
.match(r
'(\?|None|Unused)\s*$', text
):
159 add_item(items
, (klass
,), text
)
161 for subclass_group
in klass_group
.SUBCLASSES
:
162 subclass
= subclass_group
.subclass
.upper()
163 text
= subclass_group
.text
.strip()
164 if subclass
!= '00' and not re
.match(r
'(\?|None|Unused)\s*$', text
):
165 add_item(items
, (klass
, subclass
), text
)
167 for protocol_group
in subclass_group
.PROTOCOLS
:
168 protocol
= protocol_group
.protocol
.upper()
169 text
= protocol_group
.name
.strip()
170 if klass
!= '00' and not re
.match(r
'(\?|None|Unused)\s*$', text
):
171 add_item(items
, (klass
, subclass
, protocol
), text
)
173 with
open('20-usb-classes.hwdb', 'wt') as out
:
174 header(out
, 'http://www.linux-usb.org/usb.ids')
176 for key
in sorted(items
):
178 p
, n
= 'usb:v*p*d*dc{}*', 'CLASS'
180 p
, n
= 'usb:v*p*d*dc{}dsc{}*', 'SUBCLASS'
182 p
, n
= 'usb:v*p*d*dc{}dsc{}dp{}*', 'PROTOCOL'
183 print('', p
.format(*key
),
184 f
' ID_USB_{n}_FROM_DATABASE={items[key]}', sep
='\n', file=out
)
186 print(f
'Wrote {out.name}')
188 def pci_vendor_model(p
):
191 for vendor_group
in p
.VENDORS
:
192 vendor
= vendor_group
.VENDOR
.vendor
.upper()
193 text
= vendor_group
.VENDOR
.text
.strip()
194 add_item(items
, (vendor
,), text
)
196 for device_group
in vendor_group
.DEVICES
:
197 device
= device_group
.device
.upper()
198 text
= device_group
.text
.strip()
199 add_item(items
, (vendor
, device
), text
)
201 for subvendor_group
in device_group
.SUBVENDORS
:
202 sub_vendor
= subvendor_group
.a
.upper()
203 sub_model
= subvendor_group
.b
.upper()
204 sub_text
= subvendor_group
.name
.strip()
205 if sub_text
.startswith(text
):
206 sub_text
= sub_text
[len(text
):].lstrip()
208 sub_text
= f
' ({sub_text})'
209 add_item(items
, (vendor
, device
, sub_vendor
, sub_model
), text
+ sub_text
)
211 with
open('20-pci-vendor-model.hwdb', 'wt') as out
:
212 header(out
, 'http://pci-ids.ucw.cz/v2.2/pci.ids')
214 for key
in sorted(items
):
216 p
, n
= 'pci:v0000{}*', 'VENDOR'
218 p
, n
= 'pci:v0000{}d0000{}*', 'MODEL'
220 p
, n
= 'pci:v0000{}d0000{}sv0000{}sd0000{}*', 'MODEL'
221 print('', p
.format(*key
),
222 f
' ID_{n}_FROM_DATABASE={items[key]}', sep
='\n', file=out
)
224 print(f
'Wrote {out.name}')
229 for klass_group
in p
.CLASSES
:
230 klass
= klass_group
.KLASS
.klass
.upper()
231 text
= klass_group
.KLASS
.text
.strip()
232 add_item(items
, (klass
,), text
)
234 for subclass_group
in klass_group
.SUBCLASSES
:
235 subclass
= subclass_group
.subclass
.upper()
236 text
= subclass_group
.text
.strip()
237 add_item(items
, (klass
, subclass
), text
)
239 for protocol_group
in subclass_group
.PROTOCOLS
:
240 protocol
= protocol_group
.protocol
.upper()
241 text
= protocol_group
.name
.strip()
242 add_item(items
, (klass
, subclass
, protocol
), text
)
244 with
open('20-pci-classes.hwdb', 'wt') as out
:
245 header(out
, 'http://pci-ids.ucw.cz/v2.2/pci.ids')
247 for key
in sorted(items
):
249 p
, n
= 'pci:v*d*sv*sd*bc{}*', 'CLASS'
251 p
, n
= 'pci:v*d*sv*sd*bc{}sc{}*', 'SUBCLASS'
253 p
, n
= 'pci:v*d*sv*sd*bc{}sc{}i{}*', 'INTERFACE'
254 print('', p
.format(*key
),
255 f
' ID_PCI_{n}_FROM_DATABASE={items[key]}', sep
='\n', file=out
)
257 print(f
'Wrote {out.name}')
259 def sdio_vendor_model(p
):
262 for vendor_group
in p
.VENDORS
:
263 vendor
= vendor_group
.VENDOR
.vendor
.upper()
264 text
= vendor_group
.VENDOR
.text
.strip()
265 add_item(items
, (vendor
,), text
)
267 for device_group
in vendor_group
.DEVICES
:
268 device
= device_group
.device
.upper()
269 text
= device_group
.text
.strip()
270 add_item(items
, (vendor
, device
), text
)
272 with
open('20-sdio-vendor-model.hwdb', 'wt') as out
:
273 header(out
, 'hwdb/sdio.ids')
275 for key
in sorted(items
):
277 p
, n
= 'sdio:c*v{}*', 'VENDOR'
279 p
, n
= 'sdio:c*v{}d{}*', 'MODEL'
280 print('', p
.format(*key
),
281 f
' ID_{n}_FROM_DATABASE={items[key]}', sep
='\n', file=out
)
283 print(f
'Wrote {out.name}')
288 for klass_group
in p
.CLASSES
:
289 klass
= klass_group
.KLASS
.klass
.upper()
290 text
= klass_group
.KLASS
.text
.strip()
291 add_item(items
, klass
, text
)
293 with
open('20-sdio-classes.hwdb', 'wt') as out
:
294 header(out
, 'hwdb/sdio.ids')
296 for klass
in sorted(items
):
298 f
'sdio:c{klass}v*d*',
299 f
' ID_SDIO_CLASS_FROM_DATABASE={items[klass]}', sep
='\n', file=out
)
301 print(f
'Wrote {out.name}')
303 # MAC Address Block Large/Medium/Small
304 # Large MA-L 24/24 bit (OUI)
305 # Medium MA-M 28/20 bit (OUI prefix owned by IEEE)
306 # Small MA-S 36/12 bit (OUI prefix owned by IEEE)
311 for p
, check
in ((p1
, False), (p2
, False), (p3
, True)):
312 for vendor_group
in p
.VENDORS
:
313 prefix
= vendor_group
.prefix
.upper()
315 if prefix
in prefixes
:
319 start
= vendor_group
.start
.upper()
320 end
= vendor_group
.end
.upper()
322 if end
and start
!= end
:
323 print(f
'{prefix:} {start} != {end}', file=sys
.stderr
)
324 text
= vendor_group
.text
.strip()
326 key
= prefix
+ start
if end
else prefix
327 add_item(items
, key
, text
)
329 with
open('20-OUI.hwdb', 'wt') as out
:
331 'https://services13.ieee.org/RST/standards-ra-web/rest/assignments/download/?registry=MA-L&format=txt',
332 'https://services13.ieee.org/RST/standards-ra-web/rest/assignments/download/?registry=MA-M&format=txt',
333 'https://services13.ieee.org/RST/standards-ra-web/rest/assignments/download/?registry=MA-S&format=txt')
335 for pattern
in sorted(items
):
338 f
' ID_OUI_FROM_DATABASE={items[pattern]}', sep
='\n', file=out
)
340 print(f
'Wrote {out.name}')
342 if __name__
== '__main__':
345 if not args
or 'usb' in args
:
346 p
= usb_ids_grammar().parseFile(open('usb.ids', errors
='replace'))
350 if not args
or 'pci' in args
:
351 p
= pci_ids_grammar().parseFile(open('pci.ids', errors
='replace'))
355 if not args
or 'sdio' in args
:
356 p
= pci_ids_grammar().parseFile(open('sdio.ids', errors
='replace'))
360 if not args
or 'oui' in args
:
361 p
= oui_grammar('small').parseFile(open('ma-small.txt'))
362 p2
= oui_grammar('medium').parseFile(open('ma-medium.txt'))
363 p3
= oui_grammar('large').parseFile(open('ma-large.txt'))