File: pci_config.py

package info (click to toggle)
checkbox-support 0.22-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 5,620 kB
  • sloc: xml: 14,952; python: 7,921; sh: 4; makefile: 3
file content (104 lines) | stat: -rw-r--r-- 3,620 bytes parent folder | download
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
# This file is part of Checkbox.
#
# Copyright 2015 Canonical Ltd.
#
# Checkbox is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 3,
# as published by the Free Software Foundation.
#
# Checkbox is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Checkbox.  If not, see <http://www.gnu.org/licenses/>.

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from __future__ import unicode_literals

import io
import re


class PciSubsystemIdResult():

    """Simple class to hold PCI Subsystem ID parsing results."""

    def __init__(self):
        self.pci_subsystem_id = None

    def setPciSubsystemId(self, pci_subsys_id):
        self.pci_subsystem_id = pci_subsys_id


class PciSubsystemIdParser():

    """
    Parser for lspci subsystem ID for devices.

    This takes the output from lspci -x (which shows the standard part of
    configuration space) and parses only the subsystem ID (4 hex digits)
    for the first device.
    """

    bdf_re = re.compile(r'[0-9a-fA-F]{2}:[0-9a-fA-F]{2}.[0-0a-fA-F] .*$')
    config_re = re.compile(r'([0-9a-fA-F]{2}): (([0-9a-fA-F]{2} ).+)$')

    def __init__(self, stream):
        self.stream = stream

    def run(self, result):
        """
        Parse the initialized lspci output.

        Returns nothing, but will call the result object's
        setPciSubsystemId method once with the susbystem ID for the
        first device found.
        """
        for line_no, line in enumerate(self.stream):
            if not line:
                return None
            line = line.strip()
            if line_no == 0:
                # This should be the BDF and device type and name
                if not re.match(self.bdf_re, line):
                    return None
            if line_no == 3:
                # The fourth line should contain the value we want
                matches = re.match(self.config_re, line)
                if matches:
                    # The first group marks the offset which we expect
                    # to be 20 for the fourth line (first line is the
                    # BDF, second and third lines are offsets 00 and 10
                    # respectively but we don't care about them).
                    if matches.group(1) != "20":
                        return None
                    octets = matches.group(2).split(" ")
                    # After the offset marker, we expect exactly 16
                    # hex octets of data.
                    if not len(octets) == 16:
                        return None
                    # The ID we want consists of the last and next-to-last
                    # octets (in that order, hence the index reversal).
                    result.setPciSubsystemId(octets[-1] + octets[-2])
                    break
                else:
                    return None


def parse_pci_subsys_id(output):
    """
    Parse output of `lspci -x`.

    :returns: an instance of PciSubsystemIdResult which will
    have a pci_subsystem_id attribute. This can either contain the
    actual subsystem ID, or None, if no valid subsystem ID could
    be extracted.
    """
    parser = PciSubsystemIdParser(io.StringIO(output))
    result = PciSubsystemIdResult()
    parser.run(result)
    return result