1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376
|
#!/usr/bin/env python3
# SPDX-License-Identifier: LGPL-2.1-or-later
import re
import sys
from pyparsing import (Word, White, Literal, Regex,
LineEnd, SkipTo,
ZeroOrMore, OneOrMore, Combine, Optional, Suppress,
Group, ParserElement,
stringEnd, pythonStyleComment)
EOL = LineEnd().suppress()
NUM1 = Word('0123456789abcdefABCDEF', exact=1)
NUM2 = Word('0123456789abcdefABCDEF', exact=2)
NUM3 = Word('0123456789abcdefABCDEF', exact=3)
NUM4 = Word('0123456789abcdefABCDEF', exact=4)
NUM6 = Word('0123456789abcdefABCDEF', exact=6)
TAB = White('\t', exact=1).suppress()
COMMENTLINE = pythonStyleComment + EOL
EMPTYLINE = LineEnd()
text_eol = lambda name: Regex(r'[^\n]+')(name) + EOL
ParserElement.setDefaultWhitespaceChars(' \n')
def klass_grammar():
klass_line = Literal('C ').suppress() + NUM2('klass') + text_eol('text')
subclass_line = TAB + NUM2('subclass') + text_eol('text')
protocol_line = TAB + TAB + NUM2('protocol') + text_eol('name')
subclass = (subclass_line('SUBCLASS') -
ZeroOrMore(Group(protocol_line)('PROTOCOLS*')
^ COMMENTLINE.suppress()))
klass = (klass_line('KLASS') -
ZeroOrMore(Group(subclass)('SUBCLASSES*')
^ COMMENTLINE.suppress()))
return klass
def usb_ids_grammar():
vendor_line = NUM4('vendor') + text_eol('text')
device_line = TAB + NUM4('device') + text_eol('text')
interface_line = TAB + TAB + NUM4('interface') + NUM4('interface2') + text_eol('text')
device = (device_line +
ZeroOrMore(Group(interface_line)
^ COMMENTLINE.suppress()))
vendor = (vendor_line('VENDOR') +
ZeroOrMore(Group(device)('VENDOR_DEV*') ^ COMMENTLINE.suppress()))
klass = klass_grammar()
other_line = (Literal('AT ') ^ Literal('HID ') ^ Literal('R ')
^ Literal('PHY ') ^ Literal('BIAS ') ^ Literal('HUT ')
^ Literal('L ') ^ Literal('VT ') ^ Literal('HCC ')) + text_eol('text')
other_group = (other_line - ZeroOrMore(TAB + text_eol('text')))
commentgroup = OneOrMore(COMMENTLINE).suppress() ^ EMPTYLINE.suppress()
grammar = OneOrMore(Group(vendor)('VENDORS*')
^ Group(klass)('CLASSES*')
^ other_group.suppress() ^ commentgroup) + stringEnd()
grammar.parseWithTabs()
return grammar
def pci_ids_grammar():
vendor_line = NUM4('vendor') + text_eol('text')
device_line = TAB + NUM4('device') + text_eol('text')
subvendor_line = TAB + TAB + NUM4('a') + White(' ') + NUM4('b') + text_eol('name')
device = (device_line('DEVICE') +
ZeroOrMore(Group(subvendor_line)('SUBVENDORS*') ^ COMMENTLINE.suppress()))
vendor = (vendor_line('VENDOR') +
ZeroOrMore(Group(device)('DEVICES*') ^ COMMENTLINE.suppress()))
klass = klass_grammar()
commentgroup = OneOrMore(COMMENTLINE).suppress() ^ EMPTYLINE.suppress()
grammar = OneOrMore(Group(vendor)('VENDORS*')
^ Group(klass)('CLASSES*')
^ commentgroup) + stringEnd()
grammar.parseWithTabs()
return grammar
def sdio_ids_grammar():
vendor_line = NUM4('vendor') + text_eol('text')
device_line = TAB + NUM4('device') + text_eol('text')
vendor = (vendor_line('VENDOR') +
ZeroOrMore(Group(device_line)('DEVICES*') ^ COMMENTLINE.suppress()))
klass = klass_grammar()
commentgroup = OneOrMore(COMMENTLINE).suppress() ^ EMPTYLINE.suppress()
grammar = OneOrMore(Group(vendor)('VENDORS*')
^ Group(klass)('CLASSES*')
^ commentgroup) + stringEnd()
grammar.parseWithTabs()
return grammar
def oui_grammar(type):
prefix_line = (Combine(NUM2 - Suppress('-') - NUM2 - Suppress('-') - NUM2)('prefix')
- Literal('(hex)') - text_eol('text'))
if type == 'small':
vendor_line = (NUM3('start') - '000-' - NUM3('end') - 'FFF'
- Literal('(base 16)') - text_eol('text2'))
elif type == 'medium':
vendor_line = (NUM1('start') - '00000-' - NUM1('end') - 'FFFFF'
- Literal('(base 16)') - text_eol('text2'))
else:
assert type == 'large'
vendor_line = (NUM6('start')
- Literal('(base 16)') - text_eol('text2'))
extra_line = TAB - TAB - TAB - TAB - SkipTo(EOL)
vendor = prefix_line + vendor_line + ZeroOrMore(extra_line) + Optional(EMPTYLINE)
grammar = (Literal('OUI') + text_eol('header')
+ text_eol('header') + text_eol('header') + EMPTYLINE
+ OneOrMore(Group(vendor)('VENDORS*')) + stringEnd())
grammar.parseWithTabs()
return grammar
def header(file, *sources):
print('''\
# This file is part of systemd.
#
# Data imported from:{}{}'''.format(' ' if len(sources) == 1 else '\n# ',
'\n# '.join(sources)),
file=file)
def add_item(items, key, value):
if key in items:
print(f'Ignoring duplicate entry: {key} = "{items[key]}", "{value}"')
else:
items[key] = value
def usb_vendor_model(p):
items = {}
for vendor_group in p.VENDORS:
vendor = vendor_group.vendor.upper()
text = vendor_group.text.strip()
add_item(items, (vendor,), text)
for vendor_dev in vendor_group.VENDOR_DEV:
device = vendor_dev.device.upper()
text = vendor_dev.text.strip()
add_item(items, (vendor, device), text)
with open('20-usb-vendor-model.hwdb', 'wt') as out:
header(out, 'http://www.linux-usb.org/usb.ids')
for key in sorted(items):
if len(key) == 1:
p, n = 'usb:v{}*', 'VENDOR'
else:
p, n = 'usb:v{}p{}*', 'MODEL',
print('', p.format(*key),
f' ID_{n}_FROM_DATABASE={items[key]}', sep='\n', file=out)
print(f'Wrote {out.name}')
def usb_classes(p):
items = {}
for klass_group in p.CLASSES:
klass = klass_group.klass.upper()
text = klass_group.text.strip()
if klass != '00' and not re.match(r'(\?|None|Unused)\s*$', text):
add_item(items, (klass,), text)
for subclass_group in klass_group.SUBCLASSES:
subclass = subclass_group.subclass.upper()
text = subclass_group.text.strip()
if subclass != '00' and not re.match(r'(\?|None|Unused)\s*$', text):
add_item(items, (klass, subclass), text)
for protocol_group in subclass_group.PROTOCOLS:
protocol = protocol_group.protocol.upper()
text = protocol_group.name.strip()
if klass != '00' and not re.match(r'(\?|None|Unused)\s*$', text):
add_item(items, (klass, subclass, protocol), text)
with open('20-usb-classes.hwdb', 'wt') as out:
header(out, 'http://www.linux-usb.org/usb.ids')
for key in sorted(items):
if len(key) == 1:
p, n = 'usb:v*p*d*dc{}*', 'CLASS'
elif len(key) == 2:
p, n = 'usb:v*p*d*dc{}dsc{}*', 'SUBCLASS'
else:
p, n = 'usb:v*p*d*dc{}dsc{}dp{}*', 'PROTOCOL'
print('', p.format(*key),
f' ID_USB_{n}_FROM_DATABASE={items[key]}', sep='\n', file=out)
print(f'Wrote {out.name}')
def pci_vendor_model(p):
items = {}
for vendor_group in p.VENDORS:
vendor = vendor_group.vendor.upper()
text = vendor_group.text.strip()
add_item(items, (vendor,), text)
for device_group in vendor_group.DEVICES:
device = device_group.device.upper()
text = device_group.text.strip()
add_item(items, (vendor, device), text)
for subvendor_group in device_group.SUBVENDORS:
sub_vendor = subvendor_group.a.upper()
sub_model = subvendor_group.b.upper()
sub_text = subvendor_group.name.strip()
if sub_text.startswith(text):
sub_text = sub_text[len(text):].lstrip()
if sub_text:
sub_text = f' ({sub_text})'
add_item(items, (vendor, device, sub_vendor, sub_model), text + sub_text)
with open('20-pci-vendor-model.hwdb', 'wt') as out:
header(out, 'http://pci-ids.ucw.cz/v2.2/pci.ids')
for key in sorted(items):
if len(key) == 1:
p, n = 'pci:v0000{}*', 'VENDOR'
elif len(key) == 2:
p, n = 'pci:v0000{}d0000{}*', 'MODEL'
else:
p, n = 'pci:v0000{}d0000{}sv0000{}sd0000{}*', 'MODEL'
print('', p.format(*key),
f' ID_{n}_FROM_DATABASE={items[key]}', sep='\n', file=out)
print(f'Wrote {out.name}')
def pci_classes(p):
items = {}
for klass_group in p.CLASSES:
klass = klass_group.klass.upper()
text = klass_group.text.strip()
add_item(items, (klass,), text)
for subclass_group in klass_group.SUBCLASSES:
subclass = subclass_group.subclass.upper()
text = subclass_group.text.strip()
add_item(items, (klass, subclass), text)
for protocol_group in subclass_group.PROTOCOLS:
protocol = protocol_group.protocol.upper()
text = protocol_group.name.strip()
add_item(items, (klass, subclass, protocol), text)
with open('20-pci-classes.hwdb', 'wt') as out:
header(out, 'http://pci-ids.ucw.cz/v2.2/pci.ids')
for key in sorted(items):
if len(key) == 1:
p, n = 'pci:v*d*sv*sd*bc{}*', 'CLASS'
elif len(key) == 2:
p, n = 'pci:v*d*sv*sd*bc{}sc{}*', 'SUBCLASS'
else:
p, n = 'pci:v*d*sv*sd*bc{}sc{}i{}*', 'INTERFACE'
print('', p.format(*key),
f' ID_PCI_{n}_FROM_DATABASE={items[key]}', sep='\n', file=out)
print(f'Wrote {out.name}')
def sdio_vendor_model(p):
items = {}
for vendor_group in p.VENDORS:
vendor = vendor_group.vendor.upper()
text = vendor_group.text.strip()
add_item(items, (vendor,), text)
for device_group in vendor_group.DEVICES:
device = device_group.device.upper()
text = device_group.text.strip()
add_item(items, (vendor, device), text)
with open('20-sdio-vendor-model.hwdb', 'wt') as out:
header(out, 'hwdb.d/sdio.ids')
for key in sorted(items):
if len(key) == 1:
p, n = 'sdio:c*v{}*', 'VENDOR'
else:
p, n = 'sdio:c*v{}d{}*', 'MODEL'
print('', p.format(*key),
f' ID_{n}_FROM_DATABASE={items[key]}', sep='\n', file=out)
print(f'Wrote {out.name}')
def sdio_classes(p):
items = {}
for klass_group in p.CLASSES:
klass = klass_group.klass.upper()
text = klass_group.text.strip()
add_item(items, klass, text)
with open('20-sdio-classes.hwdb', 'wt') as out:
header(out, 'hwdb.d/sdio.ids')
for klass in sorted(items):
print(f'',
f'sdio:c{klass}v*d*',
f' ID_SDIO_CLASS_FROM_DATABASE={items[klass]}', sep='\n', file=out)
print(f'Wrote {out.name}')
# MAC Address Block Large/Medium/Small
# Large MA-L 24/24 bit (OUI)
# Medium MA-M 28/20 bit (OUI prefix owned by IEEE)
# Small MA-S 36/12 bit (OUI prefix owned by IEEE)
def oui(p1, p2, p3):
prefixes = set()
items = {}
for p, check in ((p1, False), (p2, False), (p3, True)):
for vendor_group in p.VENDORS:
prefix = vendor_group.prefix.upper()
if check:
if prefix in prefixes:
continue
else:
prefixes.add(prefix)
start = vendor_group.start.upper()
end = vendor_group.end.upper()
if end and start != end:
print(f'{prefix:} {start} != {end}', file=sys.stderr)
text = vendor_group.text.strip()
key = prefix + start if end else prefix
add_item(items, key, text)
with open('20-OUI.hwdb', 'wt') as out:
header(out,
'https://services13.ieee.org/RST/standards-ra-web/rest/assignments/download/?registry=MA-L&format=txt',
'https://services13.ieee.org/RST/standards-ra-web/rest/assignments/download/?registry=MA-M&format=txt',
'https://services13.ieee.org/RST/standards-ra-web/rest/assignments/download/?registry=MA-S&format=txt')
for pattern in sorted(items):
print(f'',
f'OUI:{pattern}*',
f' ID_OUI_FROM_DATABASE={items[pattern]}', sep='\n', file=out)
print(f'Wrote {out.name}')
if __name__ == '__main__':
args = sys.argv[1:]
if not args or 'usb' in args:
p = usb_ids_grammar().parseFile(open('usb.ids', errors='replace'))
usb_vendor_model(p)
usb_classes(p)
if not args or 'pci' in args:
p = pci_ids_grammar().parseFile(open('pci.ids', errors='replace'))
pci_vendor_model(p)
pci_classes(p)
if not args or 'sdio' in args:
p = pci_ids_grammar().parseFile(open('sdio.ids', errors='replace'))
sdio_vendor_model(p)
sdio_classes(p)
if not args or 'oui' in args:
p = oui_grammar('small').parseFile(open('ma-small.txt'))
p2 = oui_grammar('medium').parseFile(open('ma-medium.txt'))
p3 = oui_grammar('large').parseFile(open('ma-large.txt'))
oui(p, p2, p3)
|